From 7d2c8f67ffd64bbc7a2a50af6c05c03c64527e76 Mon Sep 17 00:00:00 2001 From: lizeth Date: Fri, 18 Oct 2019 12:12:39 +0200 Subject: [PATCH 0001/1018] testing sparsity parameters done --- src/mdict/d_mesh_denoising.cpp | 16 +++++++++------- src/mdict/dictionary.cpp | 2 +- src/mdict/patch.cpp | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/mdict/d_mesh_denoising.cpp b/src/mdict/d_mesh_denoising.cpp index 54ba18c6..5686d799 100644 --- a/src/mdict/d_mesh_denoising.cpp +++ b/src/mdict/d_mesh_denoising.cpp @@ -15,15 +15,15 @@ void test_mesh_denoising(const string & file) size_t n = 4; size_t m = 16; size_t M = 0; - distance_t f = 1; + distance_t f = 1.2; bool learn = false; distance_t error; //dictionary::L = 20; basis * phi = new basis_dct(n); - denoising dict(mesh, phi, m, M, f, learn,0); - dict.execute(); -/* + //denoising dict(mesh, phi, m, M, f, learn,0); + //dict.execute(); + ofstream os("../tmp/test.txt"); for(; f<1.4; f+=0.1) @@ -32,17 +32,19 @@ void test_mesh_denoising(const string & file) for(size_t i = 10; i<26; i+=5) { dictionary::L = i; + mesh = new che_off(file.c_str()); denoising dict(mesh, phi, m, M, f, learn,0); error = dict.execute(); os<< "\t"< Date: Fri, 18 Oct 2019 13:31:54 +0200 Subject: [PATCH 0002/1018] new hybrid test added --- CMakeLists.txt | 2 + include/mdict/d_hybrid_denoising.h | 16 ++++ src/mdict/d_hybrid_denoising.cpp | 130 +++++++++++++++++++++++++++++ test_hybrid_denoising.cpp | 9 ++ 4 files changed, 157 insertions(+) create mode 100644 include/mdict/d_hybrid_denoising.h create mode 100644 src/mdict/d_hybrid_denoising.cpp create mode 100644 test_hybrid_denoising.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bdd7fc8..f0b7c887 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,11 +70,13 @@ add_executable(gproshan gproshan.cpp) add_executable(test_geodesics test_geodesics.cpp) add_executable(test_image_denoising test_image_denoising.cpp) add_executable(test_mesh_denoising test_mesh_denoising.cpp) +add_executable(test_hybrid_denoising test_hybrid_denoising.cpp) target_link_libraries(gproshan gproshan_cpp) target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) target_link_libraries(test_mesh_denoising gproshan_cpp) +target_link_libraries(test_hybrid_denoising gproshan_cpp) if(CUDA_FOUND) target_link_libraries(gproshan gproshan_cu) diff --git a/include/mdict/d_hybrid_denoising.h b/include/mdict/d_hybrid_denoising.h new file mode 100644 index 00000000..08782f53 --- /dev/null +++ b/include/mdict/d_hybrid_denoising.h @@ -0,0 +1,16 @@ +#ifndef D_HYBRID_DENOISING_H +#define D_HYBRID_DENOISING_H + +#include "mdict.h" + + +// geometry processing and shape analysis framework +// mesh dictionary learning and sparse coding namespace +namespace gproshan::mdict { + +void test_hybrid_denoising(const string & file); + +} // namespace gproshan::mdict + +#endif // D_HYBRID_DENOISING_H + diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp new file mode 100644 index 00000000..20db1ea4 --- /dev/null +++ b/src/mdict/d_hybrid_denoising.cpp @@ -0,0 +1,130 @@ +#include "d_hybrid_denoising.h" + +#include + +using namespace cimg_library; + + +// geometry processing and shape analysis framework +// mesh dictionary learning and sparse coding namespace +namespace gproshan::mdict { + + +void test_hybrid_denoising(const string & file) +{ + CImg image(file.c_str()); + image.resize(128, 128); + image = image.get_normalize(0, 1); + + size_t p = 8; // square side of each patche + size_t rows = image.width() - p + 1; + size_t cols = image.height() - p + 1; + size_t n = p * p; // size of each patche + size_t m = 256; // number of atoms + size_t M = rows * cols; // number of patches + size_t L = 10; // sparsity OMP norm L_0 + size_t K = 10; // KSVD iterations + + a_mat X(n, M); + + for(index_t x = 0; x < rows; x++) + for(index_t y = 0; y < cols; y++) + { + index_t i = x + y * rows; + index_t k = 0; + + for(index_t b = y; b < y + p; b++) + for(index_t a = x; a < x + p; a++) + { + X(k, i) = image(a, b); + k++; + } + } + + a_mat D(n, m, arma::fill::randu); + D = normalise(D); + + CImg imdict; + for(index_t i = 0; i < 16; i++) + { + CImg imrow; + for(index_t j = 0; j < 16; j++) + imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); + + imdict.append(imrow, 'y'); + } + imdict.display(); + + gproshan_log(KSVD); + + double time; + + TIC(time) + KSVD(D, X, L, K); + TOC(time) + + gproshan_log_var(time); + + CImg imdictlearned; + for(index_t i = 0; i < 16; i++) + { + CImg imrow; + for(index_t j = 0; j < 16; j++) + imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); + + imdictlearned.append(imrow, 'y'); + } + (imdict, imdictlearned).display(); + + a_mat alpha(m, M); + + gproshan_log(OMP); + + TIC(time) + #pragma omp parallel for + for(index_t i = 0; i < M; i++) + alpha.col(i) = OMP(X.col(i), D, L); + TOC(time) + + gproshan_log_var(time); + + a_mat Y = D * alpha; + + CImg image_out = image; + image_out.fill(0); + + for(index_t x = 0; x < rows; x++) + for(index_t y = 0; y < cols; y++) + { + index_t i = x + y * rows; + index_t k = 0; + + for(index_t b = y; b < y + p; b++) + for(index_t a = x; a < x + p; a++) + { + image_out(a, b) += Y(k, i); + k++; + } + } + + rows = image.width(); + cols = image.height(); + for(index_t x = 0; x < rows; x++) + for(index_t y = 0; y < cols; y++) + { + index_t dx = p, dy = p; + if(x < p && x < dx) dx = x + 1; + if(y < p && y < dy) dy = y + 1; + if((rows - x) < p && (rows - x) < dx) dx = rows - x; + if((cols - y) < p && (cols - y) < dy) dy = cols - y; + + image_out(x, y) /= dx * dy; + } + + CImg diff = abs(image - image_out); + (image, image_out, diff).display(); +} + + +} // namespace gproshan::mdict + diff --git a/test_hybrid_denoising.cpp b/test_hybrid_denoising.cpp new file mode 100644 index 00000000..427dd6bd --- /dev/null +++ b/test_hybrid_denoising.cpp @@ -0,0 +1,9 @@ +#include "mdict/d_hybrid_denoising.h" + +int main(int nargs, const char ** args) +{ + gproshan::mdict::test_hybrid_denoising(args[1]); + + return 0; +} + From 7d80d0cac8610a22350e51ce47224478d308ef44 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Oct 2019 13:49:54 +0200 Subject: [PATCH 0003/1018] Revert "Merge branch 'master' into Synthesis" This reverts commit cf00e882414c7a9a1439c23030085e0e3fdfdee2. --- .gitignore | 4 ++ CMakeLists.txt | 4 +- include/app_viewer.h | 2 + include/geodesics_ptp.h | 8 ++- include/heat_flow.h | 4 ++ include/mdict/d_mesh.h | 8 +-- include/mdict/d_mesh_denoising.h | 20 +++++++ include/mdict/denoising.h | 4 +- include/mdict/dictionary.h | 10 +++- include/mdict/inpainting.h | 4 +- include/mdict/mdict.h | 4 -- include/mdict/patch.h | 17 ++++++ include/mdict/super_resolution.h | 4 +- include/mdict/synthesis.h | 25 ++++++++ include/test_geodesics_ptp.h | 4 ++ src/app_viewer.cpp | 37 ++++++++++-- src/geodesics.cpp | 4 ++ src/heat_flow.cpp | 8 +++ src/mdict/d_mesh.cpp | 21 ++++--- src/mdict/denoising.cpp | 8 ++- src/mdict/dictionary.cpp | 99 +++++++++++++++++++++++++++++--- src/mdict/image_denoising.cpp | 26 ++------- src/mdict/inpainting.cpp | 8 ++- src/mdict/mdict.cpp | 82 +++++++++++--------------- src/mdict/patch.cpp | 77 ++++++++++++++++++++++++- src/mdict/super_resolution.cpp | 4 +- src/mdict/synthesis.cpp | 42 ++++++++++++++ src/sampling.cpp | 6 ++ src/test_geodesics_ptp.cpp | 14 ++++- src/viewer/viewer.cpp | 5 +- test_mesh_denoising.cpp | 12 ++++ 31 files changed, 455 insertions(+), 120 deletions(-) create mode 100644 include/mdict/d_mesh_denoising.h create mode 100644 include/mdict/synthesis.h create mode 100644 src/mdict/synthesis.cpp create mode 100644 test_mesh_denoising.cpp diff --git a/.gitignore b/.gitignore index 3d308cdd..63ae1d55 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,8 @@ gproshan test_geodesics test_geodesics_double +.DS_Store + +test_image_denoising + *.user diff --git a/CMakeLists.txt b/CMakeLists.txt index 500aa13a..f0b7c887 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,8 +16,9 @@ find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) find_package(Eigen3 REQUIRED) find_package(SuiteSparse REQUIRED) +find_package(Boost COMPONENTS thread system) -set(CMAKE_BUILD_TYPE Release) +set(CMAKE_BUILD_TYPE Debug) set(CMAKE_CXX_FLAGS "-fopenmp -Wall") set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") @@ -81,6 +82,7 @@ if(CUDA_FOUND) target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) + target_link_libraries(test_mesh_denoising gproshan_cu) endif(CUDA_FOUND) file(MAKE_DIRECTORY tmp) diff --git a/include/app_viewer.h b/include/app_viewer.h index b2081cba..661a84e2 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -21,6 +21,7 @@ #include "mdict/super_resolution.h" #include "mdict/inpainting.h" #include "mdict/basis_dct.h" +#include "mdict/synthesis.h" #include "mdict/patch.h" #include "key_points.h" #include "key_components.h" @@ -47,6 +48,7 @@ void viewer_process_mdict_patch(); void viewer_process_denoising(); void viewer_process_super_resolution(); void viewer_process_inpaiting(); +void viewer_process_synthesis(); void viewer_process_iterative_inpaiting(); void viewer_process_functional_maps(); diff --git a/include/geodesics_ptp.h b/include/geodesics_ptp.h index 16c19c31..f3240fc9 100644 --- a/include/geodesics_ptp.h +++ b/include/geodesics_ptp.h @@ -31,16 +31,20 @@ struct toplesets_t che * ptp_coalescence(index_t * & inv, che * mesh, const toplesets_t & toplesets); +#ifdef CUDA_SUPPORT + double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf = 1); double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); +distance_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, distance_t radio = 0); + +#endif + void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); -distance_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, distance_t radio = 0); - distance_t update_step(che * mesh, const distance_t * dist, const index_t & he); void normalize_ptp(distance_t * dist, const size_t & n); diff --git a/include/heat_flow.h b/include/heat_flow.h index 8c18e72c..3c09156b 100644 --- a/include/heat_flow.h +++ b/include/heat_flow.h @@ -37,6 +37,8 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & m, cholmod_common * context); /// double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b); +#ifdef CUDA_SUPPORT + /// host and device support /// https://docs.nvidia.com/cuda/cusolver/index.html#cusolver-lt-t-gt-csrlsvchol double solve_positive_definite_cusolver(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host = 0); @@ -50,6 +52,8 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t /// no documentation, code base on cuda/samples/7_CUDALibraries/cuSolverSp_LowlevelCholesky double solve_positive_definite_cusolver_preview(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host = 0); +#endif + } // namespace gproshan diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index 620d17b9..840f8f11 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,17 +111,17 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -void mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); +distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); -/// DEPRECATED +[[deprecated]] void mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); -/// DEPRECATED +[[deprecated]] a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); -a_vec simple_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); +a_vec simple_means_vertex( const index_t & v, std::vector & patches, std::vector & patches_map); } // namespace gproshan::mdict diff --git a/include/mdict/d_mesh_denoising.h b/include/mdict/d_mesh_denoising.h new file mode 100644 index 00000000..0f834b67 --- /dev/null +++ b/include/mdict/d_mesh_denoising.h @@ -0,0 +1,20 @@ +#ifndef D_MESH_DENOISING_H +#define D_MESH_DENOISING_H + +#include "mdict/denoising.h" + +#include "che_off.h" + + +// geometry processing and shape analysis framework +// mesh dictionary learning and sparse coding namespace +namespace gproshan::mdict { + + +void test_mesh_denoising(const string & file); + + +} // namespace gproshan::mdict + +#endif // D_MESH_DENOISING_H + diff --git a/include/mdict/denoising.h b/include/mdict/denoising.h index 505b0eb0..e738e827 100644 --- a/include/mdict/denoising.h +++ b/include/mdict/denoising.h @@ -12,10 +12,10 @@ namespace gproshan::mdict { class denoising : public dictionary { public: - denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = true); virtual ~denoising() = default; - void execute(); + distance_t execute(); }; diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index a943a520..7f905a53 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -35,7 +35,9 @@ class dictionary std::vector patches_map; ///< invert index vertex to patches. double d_time; ///< time of operations. - bool d_plot; ///< plot atoms and basis with gnuplot. + bool d_plot; + bool learn; + ///< plot atoms and basis with gnuplot. public: static size_t L; ///< sparsity, norm L_0, default 10. @@ -47,12 +49,13 @@ class dictionary const size_t & _m, ///< number of dictionary atoms. const size_t & _M, ///< number of patches. const distance_t & _f, ///< deprecated + const bool & _learn, const bool & _plot ///< flag to plot basis and atoms with gnuplot. ); virtual ~dictionary(); - virtual void execute() = 0; + virtual distance_t execute() = 0; void learning(); void sparse_coding(); @@ -61,7 +64,8 @@ class dictionary const fmask_t & mask = nullptr ); - void mesh_reconstruction(); + distance_t mesh_reconstruction(); + void update_alphas(a_mat & alpha, size_t threshold); index_t sample(const index_t & s); }; diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index d43608a8..a1f9f66d 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -14,10 +14,10 @@ namespace gproshan::mdict { class inpainting : public dictionary { public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = true); virtual ~inpainting() = default; - void execute(); + distance_t execute(); }; diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index d7e8b81f..944e23d8 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -25,8 +25,6 @@ struct locval_t void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); -a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L); - void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); // DENSE @@ -35,8 +33,6 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L); -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L); - void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 1483a7c3..62582191 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -7,6 +7,14 @@ #include #include "include_arma.h" +#include + +#ifdef Success + #undef Success +#endif + +using namespace cimg_library; + using namespace std; @@ -53,6 +61,11 @@ class patch const index_t & p, const fmask_t & mask = nullptr ); + + const a_vec normal(); + + void save(const real_t & radio, const size_t & imsize, CImgList & imlist); + void update_heights(real_t & min, real_t & max, bool flag); private: /// Gather the vertices needed to compute the jet_fit_directions of the patch. @@ -73,6 +86,10 @@ class patch void jet_fit_directions(che * mesh, const index_t & v ); + real_t get_min_z(); + real_t get_max_z(); + + void save_z(ostream & os); friend class dictionary; diff --git a/include/mdict/super_resolution.h b/include/mdict/super_resolution.h index 228085ba..7fd1b25c 100644 --- a/include/mdict/super_resolution.h +++ b/include/mdict/super_resolution.h @@ -12,10 +12,10 @@ namespace gproshan::mdict { class super_resolution : public dictionary { public: - super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool &_learn, const bool & _plot = true); virtual ~super_resolution() = default; - void execute(); + distance_t execute(); }; diff --git a/include/mdict/synthesis.h b/include/mdict/synthesis.h new file mode 100644 index 00000000..440bdbec --- /dev/null +++ b/include/mdict/synthesis.h @@ -0,0 +1,25 @@ +#ifndef SYNTHESIS_H +#define SYNTHESIS_H + +#include "dictionary.h" + + +// geometry processing and shape analysis framework +// mesh dictionary learning and sparse coding namespace +namespace gproshan::mdict { + + +class synthesis : public dictionary +{ + public: + synthesis(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = true); + virtual ~synthesis() = default; + + distance_t execute(); +}; + + +} // namespace gproshan::mdict + +#endif // SYNTHESIS_H + diff --git a/include/test_geodesics_ptp.h b/include/test_geodesics_ptp.h index 39710d01..3252de47 100644 --- a/include/test_geodesics_ptp.h +++ b/include/test_geodesics_ptp.h @@ -22,6 +22,8 @@ double test_heat_method_cholmod(distance_t & error, double & stime, const distan double test_heat_method_gpu(distance_t & error, double & stime, const distance_t * exact, che * mesh, const std::vector & source, const int & n_test); +#ifdef CUDA_SUPPORT + /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const distance_t * exact_dist, double & time_ptp); @@ -36,6 +38,8 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector /// Return an array with the time per iteration. double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, distance_t radio = 0); +#endif + /// Exact geodesics computed using MeshLP https://github.com/areslp/matlab/tree/master/MeshLP/MeshLP, /// Geodesics code: http://code.google.com/p/geodesic/ distance_t * load_exact_geodesics(const std::string & file, const size_t & n); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index bd0e6a5a..7836e629 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -64,6 +64,7 @@ int viewer_main(int nargs, const char ** args) viewer::add_process('D', "Denoising", viewer_process_denoising); viewer::add_process('R', "Super Resolution", viewer_process_super_resolution); viewer::add_process('I', "Inpainting", viewer_process_inpaiting); + viewer::add_process('s', "Synthesis", viewer_process_synthesis); viewer::add_process('A', "IT Inpainting", viewer_process_iterative_inpaiting); viewer::sub_menus.push_back("Signatures"); @@ -457,9 +458,9 @@ void viewer_process_denoising() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - denoising dict(viewer::mesh(), phi, m, M, f); + denoising dict(viewer::mesh(), phi, m, M, f, learn); dict.execute(); - + delete phi; viewer::mesh().update_normals(); } @@ -477,7 +478,7 @@ void viewer_process_super_resolution() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - super_resolution dict(viewer::mesh(), phi, m, M, f); + super_resolution dict(viewer::mesh(), phi, m, M, f, learn); dict.execute(); delete phi; @@ -497,7 +498,7 @@ void viewer_process_inpaiting() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - inpainting dict(viewer::mesh(), phi, m, M, f); + inpainting dict(viewer::mesh(), phi, m, M, f, learn); dict.execute(); delete phi; @@ -505,6 +506,28 @@ void viewer_process_inpaiting() } +void viewer_process_synthesis() +{ + gproshan_log(APP_VIEWER); + + size_t n; // dct + size_t m, M; + distance_t f; + bool learn; + + gproshan_log(parameters: (n, m, M, f, learn)); + cin >> n >> m >> M >> f >> learn; + + basis * phi = new basis_dct(n); + synthesis dict(viewer::mesh(), phi, m, M, f, learn); + dict.execute(); + + delete phi; + viewer::mesh().update_normals(); +} + + + void viewer_process_iterative_inpaiting() { gproshan_log(APP_VIEWER); @@ -574,7 +597,13 @@ void viewer_process_farthest_point_sampling_radio() double time_fps; TIC(load_time) + +#ifdef CUDA_SUPPORT radio = farthest_point_sampling_ptp_gpu(viewer::mesh(), viewer::select_vertices, time_fps, NIL, radio); +#else + +#endif + TOC(load_time) gproshan_log_var(time_fps); diff --git a/src/geodesics.cpp b/src/geodesics.cpp index a7c74a32..adfbc0a1 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -205,6 +205,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const size_t & n_iter, const distance_t & radio) { +#ifdef CUDA_SUPPORT index_t * toplesets = new index_t[n_vertices]; vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); @@ -218,6 +219,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector< gproshan_log_var(time_ptp); delete [] toplesets; +#endif } void geodesics::run_heat_flow(che * mesh, const vector & sources) @@ -235,6 +237,7 @@ void geodesics::run_heat_flow(che * mesh, const vector & sources) void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) { +#ifdef CUDA_SUPPORT if(dist) delete [] dist; double time_total, solve_time; @@ -244,6 +247,7 @@ void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) gproshan_debug_var(time_total - solve_time); gproshan_debug_var(solve_time); +#endif } //d = {NIL, 0, 1} cross edge, next, prev diff --git a/src/heat_flow.cpp b/src/heat_flow.cpp index 0cdfaafd..a4cf53ef 100644 --- a/src/heat_flow.cpp +++ b/src/heat_flow.cpp @@ -172,6 +172,8 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b) { +#ifdef CUDA_SUPPORT + int * hA_col_ptrs = new int[A.n_cols + 1]; int * hA_row_indices = new int[A.n_nonzero]; @@ -189,6 +191,12 @@ double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & delete [] hA_row_indices; return solve_time; + +#else + + return 0; + +#endif } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 1f61197e..cd576440 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -1,4 +1,5 @@ -#include "d_mesh.h" + #include "d_mesh.h" + #include "che_off.h" #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS @@ -240,10 +241,11 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i) +distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); + //Returning to the original coordinates. #pragma omp parallel for for(index_t p = 0; p < M; p++) { @@ -259,12 +261,13 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorn_vertices(); v++) { if(patches_map[v].size()) - V.col(v) = non_local_means_vertex(alpha, v, patches, patches_map, h); + V.col(v) = simple_means_vertex(v, patches, patches_map); + //V.col(v) = non_local_means_vertex(alpha, v, patches, patches_map, h); else { V(0, v) = mesh->gt(v).x; @@ -272,6 +275,7 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorgt(v).z; } } + // ------------------------------------------------------------------------ vertex * new_vertices = (vertex *) V.memptr(); @@ -287,6 +291,8 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorset_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); + che_off::write_file(mesh,"../tmp/recon_mesh"); + return error; } a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const distance_t & h) @@ -357,6 +363,8 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector V(2, v) = mesh->gt(v).z; } } + + // ------------------------------------------------------------------------ vertex * new_vertices = (vertex *) V.memptr(); @@ -397,7 +405,6 @@ a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & i++; } - i = 0; for(auto p: patches_map[v]) { w[i] /= sum; @@ -407,10 +414,10 @@ a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & delete [] w; - return n_a_vec; + return n_a_vec/i; } -a_vec simple_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const distance_t & h) +a_vec simple_means_vertex( const index_t & v, vector & patches, vector & patches_map) { a_vec n_a_vec(3, arma::fill::zeros); diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index f0f41b22..6b7118da 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -6,11 +6,11 @@ namespace gproshan::mdict { -denoising::denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) +denoising::denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { } -void denoising::execute() +distance_t denoising::execute() { TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); @@ -24,8 +24,10 @@ void denoising::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - TIC(d_time) mesh_reconstruction(); TOC(d_time) + TIC(d_time) + distance_t error = mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); + return error; } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index b5b59517..4ca924bb 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -5,7 +5,13 @@ #include "che_poisson.h" #include "che_fill_hole.h" +#include "viewer/viewer.h" + #include +#include + + +using namespace cimg_library; // geometry processing and shape analysis framework @@ -16,8 +22,8 @@ namespace gproshan::mdict { size_t dictionary::L = 10; size_t dictionary::T = 5; -dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _d_plot): - mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), d_plot(_d_plot) +dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _d_plot): + mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), learn(_learn), d_plot(_d_plot) { A.eye(phi_basis->dim, m); } @@ -31,18 +37,20 @@ void dictionary::learning() { gproshan_debug(MDICT); - string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim) + '_' + to_string(m) + ".dict"); + string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim) + '_' + to_string(m) + '_' + to_string(f) + '_' + to_string(L) + ".dict"); gproshan_debug_var(f_dict); - if(!A.load(f_dict)) + if(learn && !A.load(f_dict)) { A.eye(phi_basis->dim, m); // A.random(phi_basis->dim, m); - KSVDT(A, patches, M, L); + + gproshan_debug(Ok); + A.save(f_dict); } - + assert(A.n_rows == phi_basis->dim); assert(A.n_cols == m); @@ -81,7 +89,9 @@ void dictionary::init_sampling() } s_radio = phi_basis->radio; - //phi_basis->radio *= f; + phi_basis->radio *= f; + gproshan_debug_var(s_radio); + gproshan_debug_var(phi_basis->radio); } void dictionary::init_patches(const bool & reset, const fmask_t & mask) @@ -141,14 +151,85 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) p.phi.set_size(p.xyz.n_cols, phi_basis->dim); phi_basis->discrete(p.phi, p.xyz); } + +/* +#ifndef NDEBUG + CImgList imlist; + for(index_t s = 0; s < M; s++) + patches[s].save(phi_basis->radio, 16, imlist); + imlist.save_ffmpeg_external("tmp/patches.mpg", 5); +#endif + +*/ + + /*Saving Patches*/ +/* + ofstream os(tmp_file_path("patch-mat")); + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + p.save_z(os); + } + os.close(); + /* + // DRAW NORMALS DEBUG + for(index_t s = 0; s < M; s++) + { + viewer::vectors.push_back({patches[s].x(0), patches[s].x(1), patches[s].x(2)}); + a_vec r = patches[s].x + 0.02 * patches[s].normal(); + viewer::vectors.push_back({r(0), r(1), r(2)}); + } + */ } -void dictionary::mesh_reconstruction() +distance_t dictionary::mesh_reconstruction() { gproshan_debug(MDICT); assert(n_vertices == mesh->n_vertices()); - mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha); + return mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha); +} +void dictionary::update_alphas(a_mat & alpha, size_t threshold) +{ + size_t np_new = M - threshold; + bool patches_covered[np_new]; + memset(patches_covered, 0, sizeof(patches_covered)); + size_t count = 0; + + // Choose the border patches using the threshold + while(count < threshold) + { + #pragma omp parallel for + for(index_t s = threshold; s < M; s++) + { + + if(!patches_covered[s-threshold]) + { + a_vec sum; + sum.zeros(); + size_t c = 0; + // Here updating alphas, we need a structure between patches and neighboor patches + //We can simulate that structure by using patches map + for(auto p: patches_map[s]) + { + if(p.first < threshold || patches_covered[p.first-threshold]) + { + sum += alpha.col(p.first); + } + sum /= c; + + } + alpha.col(s) = sum; + patches_covered[s-threshold] = 1; + count++; + } + + } + } + + // update alphas of choosed patches + // update the threshold + // repeat until threshold reachs all patches } index_t dictionary::sample(const index_t & s) diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 1a76c4d2..14d15efb 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -43,8 +43,6 @@ void test_image_denoising(const string & file) a_mat D(n, m, arma::fill::randu); D = normalise(D); - - a_mat spD = D; CImg imdict; for(index_t i = 0; i < 16; i++) @@ -62,18 +60,10 @@ void test_image_denoising(const string & file) double time; TIC(time) - KSVD(D, X, L, K); - TOC(time) - - gproshan_log_var(time); - - TIC(time) - sp_KSVD(spD, X, L, K); + sp_KSVD(D, X, L, K); TOC(time) gproshan_log_var(time); - - gproshan_log_var(norm(D - spD)); CImg imdictlearned; for(index_t i = 0; i < 16; i++) @@ -86,23 +76,19 @@ void test_image_denoising(const string & file) } (imdict, imdictlearned).display(); + a_mat alpha(m, M); gproshan_log(OMP); TIC(time) - a_mat Y = D * OMP_all(X, D, L); - TOC(time) - - gproshan_log_var(time); - - TIC(time) - vector locval; - a_mat spY = D * OMP_all(locval, X, D, L); + #pragma omp parallel for + for(index_t i = 0; i < M; i++) + alpha.col(i) = OMP(X.col(i), D, L); TOC(time) gproshan_log_var(time); - gproshan_log_var(norm(Y - spY)); + a_mat Y = D * alpha; CImg image_out = image; image_out.fill(0); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 7b23fbde..4a264d49 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -6,11 +6,11 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { } -void inpainting::execute() +distance_t inpainting::execute() { // fill holes size_t threshold = mesh->n_vertices(); @@ -37,6 +37,10 @@ void inpainting::execute() TIC(d_time) init_patches(0); TOC(d_time) gproshan_debug_var(d_time); + // Update new alphas, propagating the info towards the center + TIC(d_time) update_alphas(alpha, threshold); TOC(d_time) + gproshan_debug_var(d_time); + // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 14a14fda..49f5a1f2 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -6,6 +6,7 @@ #include #include #include +#include // geometry processing and shape analysis framework @@ -41,34 +42,11 @@ void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_m } } -a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L) -{ - locval.clear(); - - #pragma omp parallel for - for(index_t i = 0; i < X.n_cols; i++) - OMP(locval, X.col(i), i, D, L); - - arma::umat DI(2, locval.size()); - a_vec DV(locval.size()); - - #pragma omp parallel for - for(index_t k = 0; k < locval.size(); k++) - { - DI(0, k) = locval[k].i; // row - DI(1, k) = locval[k].j; // column - DV(k) = locval[k].val; - } - - return a_sp_mat(DI, DV, D.n_cols, X.n_cols); -} - void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { size_t m = D.n_cols; size_t M = X.n_cols; - arma::uvec omega(M); a_mat R, E, U, V; a_vec s; @@ -78,8 +56,10 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) while(k--) { - a_sp_mat alpha = OMP_all(locval, X, D, L); - + #pragma omp parallel for + for(index_t i = 0; i < M; i++) + OMP(locval, X.col(i), i, D, L); + sort(locval.begin(), locval.end()); rows[r = 0] = 0; @@ -88,19 +68,33 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) rows[++r] = k; rows[++r] = locval.size(); + + arma::umat DI(2, locval.size()); + a_vec DV(locval.size()); + + #pragma omp parallel for + for(index_t k = 0; k < locval.size(); k++) + { + DI(0, k) = locval[k].i; // row + DI(1, k) = locval[k].j; // column + DV(k) = locval[k].val; + } + + a_sp_mat alpha(DI, DV, m, M); R = X - D * alpha; - #pragma omp parallel for firstprivate(omega, E, U, V, s) + //#pragma omp parallel for private(E, U, V, s) for(index_t j = 0; j < m; j++) { + arma::uvec omega(rows[j + 1] - rows[j]); for(index_t r = rows[j]; r < rows[j + 1]; r++) omega(r - rows[j]) = locval[r].j; - if(rows[j + 1] - rows[j]) + if(omega.n_elem) { E = R + D.col(j) * alpha.row(j); - E = E.cols(omega.head(rows[j + 1] - rows[j])); + E = E.cols(omega); svd(U, s, V, E); D.col(j) = U.col(0); } @@ -148,33 +142,29 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) return alpha; } -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) -{ - a_mat alpha(D.n_cols, X.n_cols); - - #pragma omp parallel for - for(index_t i = 0; i < X.n_cols; i++) - alpha.col(i) = OMP(X.col(i), D, L); - - return alpha; -} - void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { + size_t m = D.n_cols; + size_t M = X.n_cols; + + a_mat alpha(m, M); + arma::uvec omega; - a_mat alpha, R, E, U, V; + a_mat R, E, U, V; a_vec s; while(k--) { - alpha = OMP_all(X, D, L); - + #pragma omp parallel for + for(index_t i = 0; i < M; i++) + alpha.col(i) = OMP(X.col(i), D, L); + R = X - D * alpha; #pragma omp parallel for private(omega, E, U, V, s) - for(index_t j = 0; j < D.n_cols; j++) + for(index_t j = 0; j < m; j++) { - omega = find(abs(alpha.row(j)) > 0); + arma::uvec omega = find(abs(alpha.row(j)) > 0); if(omega.n_elem) { E = R + D.col(j) * alpha.row(j); @@ -186,8 +176,6 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) } } -// MESH - void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L) { alpha.col(i) = OMP(p.xyz.row(2).t(), p.phi * A, L); @@ -210,8 +198,8 @@ void KSVDT(a_mat & A, vector & patches, size_t M, size_t L) size_t iter = L; while(iter--) { + OMP_all_patches_ksvt(alpha, A, patches, M, L); - #pragma omp parallel for for(index_t j = 0; j < m; j++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d8238dbf..30b55392 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -45,6 +45,7 @@ void patch::transform() { xyz.each_col() -= x; xyz = T.t() * xyz; +// xyz.row(2).zeros(); } void patch::itransform() @@ -78,6 +79,33 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } +const a_vec patch::normal() +{ + return T.col(2); +} + +void patch::save(const real_t & radio, const size_t & imsize, CImgList & imlist) +{ + // Create images with the patches info + + //building the grid + CImg img(imsize, imsize); + size_t x, y; + img.fill(0); + // for each x y plus 1, multiply by delta and floor, get i and j + for(index_t i = 0; i < vertices.size(); i++) + { + x = floor((xyz.col(i)[0] + radio) * (imsize - 1) / (2 * radio)); + y = floor((xyz.col(i)[1] + radio) * (imsize - 1) / (2 * radio)); + img(x,y) = xyz.col(i)[2]; + } + + img.resize(128, 128); + imlist.insert(img.normalize(0, 255)); + //img.save("tmp/images/test_image.jpg"); + +} + void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_toplevels, index_t * toplevel) { if(vertices.size()) vertices.clear(); @@ -107,6 +135,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl link.clear(); } + } void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & radio, index_t * toplevel) @@ -134,9 +163,8 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra p(1) = mesh->gt(v).y; p(2) = mesh->gt(v).z; p = T.t() * (p - x); - p(2) = 0; - if(vertices.size() > expected_nv) break; + //if(vertices.size() > expected_nv) break; if(toplevel[v] != current_toplevel) { if(count_toplevel == 0) break; @@ -159,7 +187,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra toplevel[u] = toplevel[v] + 1; } } - + //gproshan_debug_var(vertices.size()); link.clear(); } } @@ -200,6 +228,49 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) T(0, 2) = monge_form.normal_direction()[0]; T(1, 2) = monge_form.normal_direction()[1]; T(2, 2) = monge_form.normal_direction()[2]; + +} + +real_t patch::get_min_z() +{ + return xyz.row(2).min(); +} + +real_t patch::get_max_z() +{ + return xyz.row(2).max(); +} + +void patch::update_heights(real_t & min, real_t & max, bool flag) +{ + real_t tmp; + if(flag) + { + for(index_t i = 0; i < xyz.n_cols; i++) + { + xyz(2, i) = (xyz(2, i) - min) / (max - min); + } + } + else + { + for(index_t i = 0; i < vertices.size(); i++) + { + tmp = xyz.col(i)[2]; + tmp = (max - min) * tmp + min; + xyz.col(i)[2] = tmp; + } + } + +} + +void patch::save_z(ostream & os) +{ + index_t i; + for( i = 0; i < vertices.size()-1; i++) + { + os<>name; + string f_dict = "tmp/" + name + ".dict"; + debug(f_dict) + d_message(loading dictionary) + if(!A.load(f_dict)) + { + d_message(This dictionary does not exist Bye) return; + } + gproshan_debug_var(d_time);*/ + TIC(d_time) sparse_coding(); TOC(d_time) + gproshan_debug_var(d_time); + + TIC(d_time) mesh_reconstruction(); TOC(d_time) + gproshan_debug_var(d_time); +} + +} // namespace gproshan::mdict + diff --git a/src/sampling.cpp b/src/sampling.cpp index d3d24e84..72329193 100644 --- a/src/sampling.cpp +++ b/src/sampling.cpp @@ -67,7 +67,13 @@ bool load_sampling(vector & points, distance_t & radio, che * mesh, siz points.push_back(0); double time_fps; + + #ifdef CUDA_SUPPORT radio = farthest_point_sampling_ptp_gpu(mesh, points, time_fps, n); + #else + radio = 0; + #endif + gproshan_debug_var(time_fps); ofstream os(file); diff --git a/src/test_geodesics_ptp.cpp b/src/test_geodesics_ptp.cpp index 23aa8788..0fb968a3 100644 --- a/src/test_geodesics_ptp.cpp +++ b/src/test_geodesics_ptp.cpp @@ -180,8 +180,12 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // PTP ITERATION ERROR _____________________________________________________________________ - + + #ifdef CUDA_SUPPORT vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); + #else + vector > iter_error;// = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); + #endif system(("mv band " + (test_path + filename + ".band")).c_str()); @@ -200,7 +204,12 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) size_t i_samples = source.size(); size_t n_samples = 1001; + + #ifdef CUDA_SUPPORT double * times_fps = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); + #else + double * times_fps = new double[n_samples]; // = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); + #endif os.open(test_path + filename + ".fps"); for(index_t i = i_samples; i < n_samples; i++) @@ -244,7 +253,10 @@ double test_ptp_gpu(distance_t & error, const distance_t * exact, che * mesh, co distance_t * dist = new distance_t[mesh->n_vertices()]; for(int i = 0; i < n_test; i++) { + #ifdef CUDA_SUPPORT t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); + #endif + seconds = min(seconds, t); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0ef1cb41..e94f40a4 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -120,7 +120,7 @@ void viewer::init_glut() glutMouseFunc(mouse); glutMotionFunc(motion); - glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); +// glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); } void viewer::init_menus() @@ -326,7 +326,8 @@ void viewer::menu_save_mesh() void viewer::menu_exit() { // storeviewerState(); - glutLeaveMainLoop(); +// glutLeaveMainLoop(); + exit(0); } void viewer::menu_zoom_in() diff --git a/test_mesh_denoising.cpp b/test_mesh_denoising.cpp new file mode 100644 index 00000000..a3cdead5 --- /dev/null +++ b/test_mesh_denoising.cpp @@ -0,0 +1,12 @@ +#include "mdict/d_mesh_denoising.h" + +int main(int nargs, const char ** args) +{ + + + string file = args[1]; + gproshan::mdict::test_mesh_denoising(args[1]); + + + return 0; +} From 7f2e4a591ec02898ce28b2c50245d20f8ad8d4bd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Oct 2019 15:07:09 +0200 Subject: [PATCH 0004/1018] saving changes from master --- include/mdict/mdict.h | 4 ++ src/mdict/image_denoising.cpp | 26 ++++++++--- src/mdict/mdict.cpp | 82 ++++++++++++++++++++--------------- 3 files changed, 71 insertions(+), 41 deletions(-) diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index 944e23d8..d7e8b81f 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -25,6 +25,8 @@ struct locval_t void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); +a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L); + void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); // DENSE @@ -33,6 +35,8 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L); +a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L); + void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L); diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 14d15efb..1a76c4d2 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -43,6 +43,8 @@ void test_image_denoising(const string & file) a_mat D(n, m, arma::fill::randu); D = normalise(D); + + a_mat spD = D; CImg imdict; for(index_t i = 0; i < 16; i++) @@ -60,10 +62,18 @@ void test_image_denoising(const string & file) double time; TIC(time) - sp_KSVD(D, X, L, K); + KSVD(D, X, L, K); + TOC(time) + + gproshan_log_var(time); + + TIC(time) + sp_KSVD(spD, X, L, K); TOC(time) gproshan_log_var(time); + + gproshan_log_var(norm(D - spD)); CImg imdictlearned; for(index_t i = 0; i < 16; i++) @@ -76,19 +86,23 @@ void test_image_denoising(const string & file) } (imdict, imdictlearned).display(); - a_mat alpha(m, M); gproshan_log(OMP); TIC(time) - #pragma omp parallel for - for(index_t i = 0; i < M; i++) - alpha.col(i) = OMP(X.col(i), D, L); + a_mat Y = D * OMP_all(X, D, L); + TOC(time) + + gproshan_log_var(time); + + TIC(time) + vector locval; + a_mat spY = D * OMP_all(locval, X, D, L); TOC(time) gproshan_log_var(time); - a_mat Y = D * alpha; + gproshan_log_var(norm(Y - spY)); CImg image_out = image; image_out.fill(0); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 49f5a1f2..14a14fda 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -6,7 +6,6 @@ #include #include #include -#include // geometry processing and shape analysis framework @@ -42,11 +41,34 @@ void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_m } } +a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L) +{ + locval.clear(); + + #pragma omp parallel for + for(index_t i = 0; i < X.n_cols; i++) + OMP(locval, X.col(i), i, D, L); + + arma::umat DI(2, locval.size()); + a_vec DV(locval.size()); + + #pragma omp parallel for + for(index_t k = 0; k < locval.size(); k++) + { + DI(0, k) = locval[k].i; // row + DI(1, k) = locval[k].j; // column + DV(k) = locval[k].val; + } + + return a_sp_mat(DI, DV, D.n_cols, X.n_cols); +} + void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { size_t m = D.n_cols; size_t M = X.n_cols; + arma::uvec omega(M); a_mat R, E, U, V; a_vec s; @@ -56,10 +78,8 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) while(k--) { - #pragma omp parallel for - for(index_t i = 0; i < M; i++) - OMP(locval, X.col(i), i, D, L); - + a_sp_mat alpha = OMP_all(locval, X, D, L); + sort(locval.begin(), locval.end()); rows[r = 0] = 0; @@ -68,33 +88,19 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) rows[++r] = k; rows[++r] = locval.size(); - - arma::umat DI(2, locval.size()); - a_vec DV(locval.size()); - - #pragma omp parallel for - for(index_t k = 0; k < locval.size(); k++) - { - DI(0, k) = locval[k].i; // row - DI(1, k) = locval[k].j; // column - DV(k) = locval[k].val; - } - - a_sp_mat alpha(DI, DV, m, M); R = X - D * alpha; - //#pragma omp parallel for private(E, U, V, s) + #pragma omp parallel for firstprivate(omega, E, U, V, s) for(index_t j = 0; j < m; j++) { - arma::uvec omega(rows[j + 1] - rows[j]); for(index_t r = rows[j]; r < rows[j + 1]; r++) omega(r - rows[j]) = locval[r].j; - if(omega.n_elem) + if(rows[j + 1] - rows[j]) { E = R + D.col(j) * alpha.row(j); - E = E.cols(omega); + E = E.cols(omega.head(rows[j + 1] - rows[j])); svd(U, s, V, E); D.col(j) = U.col(0); } @@ -142,29 +148,33 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) return alpha; } -void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) +a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { - size_t m = D.n_cols; - size_t M = X.n_cols; + a_mat alpha(D.n_cols, X.n_cols); - a_mat alpha(m, M); - + #pragma omp parallel for + for(index_t i = 0; i < X.n_cols; i++) + alpha.col(i) = OMP(X.col(i), D, L); + + return alpha; +} + +void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) +{ arma::uvec omega; - a_mat R, E, U, V; + a_mat alpha, R, E, U, V; a_vec s; while(k--) { - #pragma omp parallel for - for(index_t i = 0; i < M; i++) - alpha.col(i) = OMP(X.col(i), D, L); - + alpha = OMP_all(X, D, L); + R = X - D * alpha; #pragma omp parallel for private(omega, E, U, V, s) - for(index_t j = 0; j < m; j++) + for(index_t j = 0; j < D.n_cols; j++) { - arma::uvec omega = find(abs(alpha.row(j)) > 0); + omega = find(abs(alpha.row(j)) > 0); if(omega.n_elem) { E = R + D.col(j) * alpha.row(j); @@ -176,6 +186,8 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) } } +// MESH + void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L) { alpha.col(i) = OMP(p.xyz.row(2).t(), p.phi * A, L); @@ -198,8 +210,8 @@ void KSVDT(a_mat & A, vector & patches, size_t M, size_t L) size_t iter = L; while(iter--) { - OMP_all_patches_ksvt(alpha, A, patches, M, L); + #pragma omp parallel for for(index_t j = 0; j < m; j++) { From da57162e48bc2595ac162e6abcb6ff7534cc695b Mon Sep 17 00:00:00 2001 From: lizeth Date: Fri, 18 Oct 2019 15:31:08 +0200 Subject: [PATCH 0005/1018] updating test --- include/mdict/d_hybrid_denoising.h | 3 ++- src/mdict/d_hybrid_denoising.cpp | 36 ++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/include/mdict/d_hybrid_denoising.h b/include/mdict/d_hybrid_denoising.h index 08782f53..7b028623 100644 --- a/include/mdict/d_hybrid_denoising.h +++ b/include/mdict/d_hybrid_denoising.h @@ -2,7 +2,8 @@ #define D_HYBRID_DENOISING_H #include "mdict.h" - +#include "che_off.h" +#include "che_img.h" // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 20db1ea4..4d8134ff 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -14,11 +14,14 @@ void test_hybrid_denoising(const string & file) { CImg image(file.c_str()); image.resize(128, 128); + image.save("../tmp/image_128.jpg"); image = image.get_normalize(0, 1); size_t p = 8; // square side of each patche size_t rows = image.width() - p + 1; + size_t r = image.width(); size_t cols = image.height() - p + 1; + size_t c = image.height(); size_t n = p * p; // size of each patche size_t m = 256; // number of atoms size_t M = rows * cols; // number of patches @@ -26,7 +29,9 @@ void test_hybrid_denoising(const string & file) size_t K = 10; // KSVD iterations a_mat X(n, M); - + che * mesh = new che_img(file.c_str()); + che_off::write_file(mesh,"../tmp/image_128"); +/* for(index_t x = 0; x < rows; x++) for(index_t y = 0; y < cols; y++) { @@ -39,8 +44,34 @@ void test_hybrid_denoising(const string & file) X(k, i) = image(a, b); k++; } + + } +*/ +/* + ofstream os("../tmp/mesh.off"); + os << "OFF" << endl; + os << r * c << " " << (c - 1) * 2 *(r - 1) << " 0" << endl; + size_t i; + for(index_t x = 0; x < r; x++) + for(index_t y = 0; y < c; y++) + { + os<< x << " " << y << " " << image(x, y) << endl; + } + for(index_t x = 0; x < r-1; x++) + { + i = x * c; + for(index_t y = 0; y < c-1; y++) + { + + os<< "3 " << i+1 << " " << i+1+1 << " " << i+c +1<< endl; + os<< "3 " << i+1+1 << " " << i +1 +c +1 <<" " << i+c+1 << endl; + i++; + + } } + os.close();*/ + /* a_mat D(n, m, arma::fill::randu); D = normalise(D); @@ -122,7 +153,8 @@ void test_hybrid_denoising(const string & file) } CImg diff = abs(image - image_out); - (image, image_out, diff).display(); + (image, image_out, diff).display();*/ + (image).display(); } From 2c3a0469821e2f0f980c098ffc88e1ab79cb1593 Mon Sep 17 00:00:00 2001 From: lizeth Date: Fri, 18 Oct 2019 16:15:31 +0200 Subject: [PATCH 0006/1018] testing patches omp --- include/mdict/d_hybrid_denoising.h | 4 +++ src/mdict/d_hybrid_denoising.cpp | 58 +++++++++++++----------------- 2 files changed, 28 insertions(+), 34 deletions(-) diff --git a/include/mdict/d_hybrid_denoising.h b/include/mdict/d_hybrid_denoising.h index 7b028623..f9ddc1d5 100644 --- a/include/mdict/d_hybrid_denoising.h +++ b/include/mdict/d_hybrid_denoising.h @@ -4,6 +4,10 @@ #include "mdict.h" #include "che_off.h" #include "che_img.h" +#include "patch.h" +#include "mdict.h" +#include "basis.h" +#include "d_mesh.h" // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 4d8134ff..ae21c6c2 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -13,15 +13,14 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { CImg image(file.c_str()); - image.resize(128, 128); + image.resize(6, 6); image.save("../tmp/image_128.jpg"); image = image.get_normalize(0, 1); - size_t p = 8; // square side of each patche + size_t p = 3; // square side of each patche size_t rows = image.width() - p + 1; - size_t r = image.width(); - size_t cols = image.height() - p + 1; - size_t c = image.height(); + size_t cols = image.height() - p + 1; + size_t col = image.height(); size_t n = p * p; // size of each patche size_t m = 256; // number of atoms size_t M = rows * cols; // number of patches @@ -29,48 +28,39 @@ void test_hybrid_denoising(const string & file) size_t K = 10; // KSVD iterations a_mat X(n, M); - che * mesh = new che_img(file.c_str()); + che * mesh = new che_img("../tmp/image_128.jpg"); che_off::write_file(mesh,"../tmp/image_128"); + std::vector patches; ///< vector of patches. + std::vector patches_map; ///< invert index vertex to patches. + patches.resize(M); +// patches_map.resize(n_vertices); /* + #pragma omp for + for(index_t s = 0; s < M; s++) + { + // push back + // jet fit directions + patches[s].vertices + } + */ + index_t s = 0; + for(index_t x = 0; x < rows; x++) for(index_t y = 0; y < cols; y++) { index_t i = x + y * rows; - index_t k = 0; for(index_t b = y; b < y + p; b++) for(index_t a = x; a < x + p; a++) { - X(k, i) = image(a, b); - k++; + patches[s].vertices.push_back(a * col + b); + cout<< a*col+b<<" "; + s++; } - + cout< Date: Fri, 18 Oct 2019 16:40:27 +0200 Subject: [PATCH 0007/1018] Testing --- include/mdict/d_hybrid_denoising.h | 1 + src/mdict/d_hybrid_denoising.cpp | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/mdict/d_hybrid_denoising.h b/include/mdict/d_hybrid_denoising.h index f9ddc1d5..0049a243 100644 --- a/include/mdict/d_hybrid_denoising.h +++ b/include/mdict/d_hybrid_denoising.h @@ -7,6 +7,7 @@ #include "patch.h" #include "mdict.h" #include "basis.h" +#include "basis_dct.h" #include "d_mesh.h" // geometry processing and shape analysis framework diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index ae21c6c2..6b9040fa 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -13,16 +13,16 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { CImg image(file.c_str()); - image.resize(6, 6); + image.resize(64, 64); image.save("../tmp/image_128.jpg"); image = image.get_normalize(0, 1); - size_t p = 3; // square side of each patche + size_t p = 8; // square side of each patche size_t rows = image.width() - p + 1; size_t cols = image.height() - p + 1; size_t col = image.height(); size_t n = p * p; // size of each patche - size_t m = 256; // number of atoms + size_t m = 16; // number of atoms size_t M = rows * cols; // number of patches size_t L = 10; // sparsity OMP norm L_0 size_t K = 10; // KSVD iterations @@ -33,16 +33,8 @@ void test_hybrid_denoising(const string & file) std::vector patches; ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. patches.resize(M); -// patches_map.resize(n_vertices); -/* - #pragma omp for - for(index_t s = 0; s < M; s++) - { - // push back - // jet fit directions - patches[s].vertices - } - */ + + index_t s = 0; for(index_t x = 0; x < rows; x++) @@ -57,10 +49,15 @@ void test_hybrid_denoising(const string & file) cout<< a*col+b<<" "; s++; } - cout< Date: Fri, 18 Oct 2019 16:57:29 +0200 Subject: [PATCH 0008/1018] loading patches hybrid denoising --- src/mdict/d_hybrid_denoising.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 6b9040fa..89407042 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -12,8 +12,10 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { + size_t N = 64; + CImg image(file.c_str()); - image.resize(64, 64); + image.resize(N, N); image.save("../tmp/image_128.jpg"); image = image.get_normalize(0, 1); @@ -22,7 +24,7 @@ void test_hybrid_denoising(const string & file) size_t cols = image.height() - p + 1; size_t col = image.height(); size_t n = p * p; // size of each patche - size_t m = 16; // number of atoms + size_t m = 2 * N; // number of atoms size_t M = rows * cols; // number of patches size_t L = 10; // sparsity OMP norm L_0 size_t K = 10; // KSVD iterations @@ -30,10 +32,10 @@ void test_hybrid_denoising(const string & file) a_mat X(n, M); che * mesh = new che_img("../tmp/image_128.jpg"); che_off::write_file(mesh,"../tmp/image_128"); - std::vector patches; ///< vector of patches. + std::vector patches(M); ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. - patches.resize(M); + gproshan_debug(patches); index_t s = 0; @@ -46,18 +48,20 @@ void test_hybrid_denoising(const string & file) for(index_t a = x; a < x + p; a++) { patches[s].vertices.push_back(a * col + b); - cout<< a*col+b<<" "; - s++; + //cout<< a*col+b<<" "; } + + s++; } -/* + a_mat A; a_mat alpha; basis * phi = new basis_dct(n); A.eye(n, m); alpha.zeros(m, M); - //OMP_all_patches_ksvt(alpha, A, patches, M, L); -*/ + + OMP_all_patches_ksvt(alpha, A, patches, M, L); + /* a_mat D(n, m, arma::fill::randu); D = normalise(D); From f4d39eed91be03493e58246fb2a342ae830d91eb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Oct 2019 11:59:38 +0200 Subject: [PATCH 0009/1018] creating private dev remove --- MDICT.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 MDICT.md diff --git a/MDICT.md b/MDICT.md new file mode 100644 index 00000000..0fbea8fc --- /dev/null +++ b/MDICT.md @@ -0,0 +1 @@ +# Private development remote for Mesh Dictionary Learning framework. From a90dfc2f1286d8a9955ed177433a7389284c9974 Mon Sep 17 00:00:00 2001 From: lizeth Date: Tue, 22 Oct 2019 13:20:06 +0200 Subject: [PATCH 0010/1018] Test hybrid almost done --- src/mdict/basis_dct.cpp | 4 ++- src/mdict/d_hybrid_denoising.cpp | 56 ++++++++++++++++++++++++++++---- src/mdict/mdict.cpp | 6 ++-- src/mdict/patch.cpp | 2 +- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index a9735e1f..a2f4745c 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -21,10 +21,12 @@ void basis_dct::discrete(a_mat & phi, const a_mat & xy) a_vec x = xy.row(0).t(); a_vec y = xy.row(1).t(); - for(index_t k = 0, nx = 0; nx < n; nx++) for(index_t ny = 0; ny < n; ny++, k++) + { phi.col(k) = dct(x, y, nx, ny); +// gproshan_debug_var(k); + } } void basis_dct::plot_basis(ostream & os) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 89407042..6ba9ee85 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -12,7 +12,7 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { - size_t N = 64; + size_t N = 128; CImg image(file.c_str()); image.resize(N, N); @@ -24,17 +24,19 @@ void test_hybrid_denoising(const string & file) size_t cols = image.height() - p + 1; size_t col = image.height(); size_t n = p * p; // size of each patche - size_t m = 2 * N; // number of atoms + size_t n_basis = 4; + size_t m = n_basis * n_basis; // number of atoms size_t M = rows * cols; // number of patches size_t L = 10; // sparsity OMP norm L_0 size_t K = 10; // KSVD iterations + a_mat X(n, M); che * mesh = new che_img("../tmp/image_128.jpg"); che_off::write_file(mesh,"../tmp/image_128"); std::vector patches(M); ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. - + patches_map.resize(M); gproshan_debug(patches); index_t s = 0; @@ -48,6 +50,7 @@ void test_hybrid_denoising(const string & file) for(index_t a = x; a < x + p; a++) { patches[s].vertices.push_back(a * col + b); + //cout<< a*col+b<<" "; } @@ -56,12 +59,53 @@ void test_hybrid_denoising(const string & file) a_mat A; a_mat alpha; - basis * phi = new basis_dct(n); - A.eye(n, m); + basis * phi_basis = new basis_dct(n_basis); + A.eye(m, m); alpha.zeros(m, M); - OMP_all_patches_ksvt(alpha, A, patches, M, L); + + for(index_t s = 0; s < M; s++) + { + patches[s].reset_xyz(mesh, patches_map, s, nullptr); + } + + //#pragma omp parallel for + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + //p.T.eye(3, 3); + //p.transform(); + p.phi.set_size(p.xyz.n_cols, n_basis*n_basis); + // gproshan_debug_var(p.xyz); + phi_basis->discrete(p.phi, p.xyz); + } + + OMP_all_patches_ksvt(alpha, A, patches, M, L); + gproshan_debug_var(size(alpha)); + + //Mesh reconstruction + for(index_t p = 0; p < M; p++) + { + patch & rp = patches[p]; + + if(rp.phi.n_rows) + { + a_vec x = rp.phi * A * alpha.col(p); + // gproshan_debug_var(x); + rp.xyz.row(2) = x.t(); + // rp.itransform(); + } + } + a_mat V(3, mesh->n_vertices(), arma::fill::zeros); + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + if(patches_map[v].size()) + V.col(v) = mdict::simple_means_vertex(v, patches, patches_map); + } + vertex * new_vertices = (vertex *) V.memptr(); + mesh->set_vertices(new_vertices, mesh->n_vertices(), 0); /* a_mat D(n, m, arma::fill::randu); D = normalise(D); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 14a14fda..3cc40ee1 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -115,7 +115,6 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; a_mat DD; @@ -129,7 +128,6 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L DD = D.cols(selected_atoms.head(l + 1)); aa = pinv(DD) * x; r = x - DD * aa; - l++; } @@ -144,7 +142,7 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) tie(aa, selected_atoms) = _OMP(x, D, L); alpha.elem(selected_atoms) = aa; - +// gproshan_debug_var(size(alpha)); return alpha; } @@ -152,7 +150,7 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { a_mat alpha(D.n_cols, X.n_cols); - #pragma omp parallel for +// #pragma omp parallel for for(index_t i = 0; i < X.n_cols; i++) alpha.col(i) = OMP(X.col(i), D, L); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 30b55392..6688bfdb 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -73,7 +73,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & xyz(0, j) = v.x; xyz(1, j) = v.y; xyz(2, j) = v.z; - + vpatches[vertices[i]].push_back({p, j++}); } } From 74a85144b5783f6773347b99f012085334ce45e3 Mon Sep 17 00:00:00 2001 From: lizeth Date: Tue, 22 Oct 2019 14:26:56 +0200 Subject: [PATCH 0011/1018] test reconstruction --- src/mdict/d_hybrid_denoising.cpp | 75 +++++--------------------------- 1 file changed, 12 insertions(+), 63 deletions(-) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 6ba9ee85..4b7b6172 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -50,8 +50,7 @@ void test_hybrid_denoising(const string & file) for(index_t a = x; a < x + p; a++) { patches[s].vertices.push_back(a * col + b); - - //cout<< a*col+b<<" "; + // cout<< a*col+b<<" "; } s++; @@ -92,7 +91,7 @@ void test_hybrid_denoising(const string & file) if(rp.phi.n_rows) { a_vec x = rp.phi * A * alpha.col(p); - // gproshan_debug_var(x); + // gproshan_debug_var(x); rp.xyz.row(2) = x.t(); // rp.itransform(); } @@ -106,55 +105,7 @@ void test_hybrid_denoising(const string & file) } vertex * new_vertices = (vertex *) V.memptr(); mesh->set_vertices(new_vertices, mesh->n_vertices(), 0); - /* - a_mat D(n, m, arma::fill::randu); - D = normalise(D); - - CImg imdict; - for(index_t i = 0; i < 16; i++) - { - CImg imrow; - for(index_t j = 0; j < 16; j++) - imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); - - imdict.append(imrow, 'y'); - } - imdict.display(); - - gproshan_log(KSVD); - - double time; - - TIC(time) - KSVD(D, X, L, K); - TOC(time) - - gproshan_log_var(time); - - CImg imdictlearned; - for(index_t i = 0; i < 16; i++) - { - CImg imrow; - for(index_t j = 0; j < 16; j++) - imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); - - imdictlearned.append(imrow, 'y'); - } - (imdict, imdictlearned).display(); - a_mat alpha(m, M); - - gproshan_log(OMP); - - TIC(time) - #pragma omp parallel for - for(index_t i = 0; i < M; i++) - alpha.col(i) = OMP(X.col(i), D, L); - TOC(time) - - gproshan_log_var(time); - - a_mat Y = D * alpha; CImg image_out = image; image_out.fill(0); @@ -163,16 +114,12 @@ void test_hybrid_denoising(const string & file) for(index_t y = 0; y < cols; y++) { index_t i = x + y * rows; - index_t k = 0; - for(index_t b = y; b < y + p; b++) - for(index_t a = x; a < x + p; a++) - { - image_out(a, b) += Y(k, i); - k++; - } - } + image_out(x, y) = mesh->gt(i).z; + gproshan_debug_var(mesh->gt(i).z); + } +/* rows = image.width(); cols = image.height(); for(index_t x = 0; x < rows; x++) @@ -186,10 +133,12 @@ void test_hybrid_denoising(const string & file) image_out(x, y) /= dx * dy; } - - CImg diff = abs(image - image_out); - (image, image_out, diff).display();*/ - (image).display(); +*/ +// CImg diff = abs(image - image_out); +// (image, image_out, diff).display(); +// (image_out).display(); +// image_out = image_out.get_normalize(0, 255); + (image, image_out).display(); } From d5ae119a3b515a9bfe8e6c5d7ed7fb45f165f02b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Oct 2019 15:04:32 +0200 Subject: [PATCH 0012/1018] updating patches hybrid denoising --- CMakeLists.txt | 1 + src/mdict/d_hybrid_denoising.cpp | 29 +++++++---------------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c543657..76bf8d4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ if(CUDA_FOUND) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) target_link_libraries(test_mesh_denoising gproshan_cu) + target_link_libraries(test_hybrid_denoising gproshan_cu) endif(CUDA_FOUND) file(MAKE_DIRECTORY tmp) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 4b7b6172..1f5c1d53 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -20,9 +20,8 @@ void test_hybrid_denoising(const string & file) image = image.get_normalize(0, 1); size_t p = 8; // square side of each patche - size_t rows = image.width() - p + 1; - size_t cols = image.height() - p + 1; - size_t col = image.height(); + size_t rows = image.width(); + size_t cols = image.height(); size_t n = p * p; // size of each patche size_t n_basis = 4; size_t m = n_basis * n_basis; // number of atoms @@ -35,25 +34,16 @@ void test_hybrid_denoising(const string & file) che * mesh = new che_img("../tmp/image_128.jpg"); che_off::write_file(mesh,"../tmp/image_128"); std::vector patches(M); ///< vector of patches. - std::vector patches_map; ///< invert index vertex to patches. - patches_map.resize(M); - gproshan_debug(patches); - - index_t s = 0; + std::vector patches_map(M); ///< invert index vertex to patches. for(index_t x = 0; x < rows; x++) for(index_t y = 0; y < cols; y++) { index_t i = x + y * rows; - for(index_t b = y; b < y + p; b++) - for(index_t a = x; a < x + p; a++) - { - patches[s].vertices.push_back(a * col + b); - // cout<< a*col+b<<" "; - } - - s++; + for(index_t b = y; b < cols && b < y + p; b++) + for(index_t a = x; a < rows && a < x + p; a++) + patches[i].vertices.push_back(a + b * rows); } a_mat A; @@ -62,12 +52,8 @@ void test_hybrid_denoising(const string & file) A.eye(m, m); alpha.zeros(m, M); - for(index_t s = 0; s < M; s++) - { patches[s].reset_xyz(mesh, patches_map, s, nullptr); - } - //#pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -117,7 +103,6 @@ void test_hybrid_denoising(const string & file) image_out(x, y) = mesh->gt(i).z; gproshan_debug_var(mesh->gt(i).z); - } /* rows = image.width(); @@ -138,7 +123,7 @@ void test_hybrid_denoising(const string & file) // (image, image_out, diff).display(); // (image_out).display(); // image_out = image_out.get_normalize(0, 255); - (image, image_out).display(); +// (image, image_out).display(); } From 967c2f8620a62b1752c745ec1e5835381e218f4b Mon Sep 17 00:00:00 2001 From: lizeth Date: Tue, 22 Oct 2019 17:05:24 +0200 Subject: [PATCH 0013/1018] testing on images --- src/mdict/d_hybrid_denoising.cpp | 11 +++++++---- src/mdict/d_mesh.cpp | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 1f5c1d53..33dbc8b0 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -12,7 +12,7 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { - size_t N = 128; + size_t N = 64; CImg image(file.c_str()); image.resize(N, N); @@ -53,7 +53,9 @@ void test_hybrid_denoising(const string & file) alpha.zeros(m, M); for(index_t s = 0; s < M; s++) + { patches[s].reset_xyz(mesh, patches_map, s, nullptr); + } //#pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -62,7 +64,7 @@ void test_hybrid_denoising(const string & file) //p.T.eye(3, 3); //p.transform(); p.phi.set_size(p.xyz.n_cols, n_basis*n_basis); - // gproshan_debug_var(p.xyz); + // gproshan_debug_var(p.xyz); phi_basis->discrete(p.phi, p.xyz); } @@ -77,11 +79,12 @@ void test_hybrid_denoising(const string & file) if(rp.phi.n_rows) { a_vec x = rp.phi * A * alpha.col(p); - // gproshan_debug_var(x); + gproshan_debug_var(alpha.col(p)); rp.xyz.row(2) = x.t(); // rp.itransform(); } } + a_mat V(3, mesh->n_vertices(), arma::fill::zeros); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) @@ -102,7 +105,7 @@ void test_hybrid_denoising(const string & file) index_t i = x + y * rows; image_out(x, y) = mesh->gt(i).z; - gproshan_debug_var(mesh->gt(i).z); + // gproshan_debug_var(mesh->gt(i).z); } /* rows = image.width(); diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index cd576440..c8cf8323 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -292,6 +292,26 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve gproshan_debug_var(v_i); mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); che_off::write_file(mesh,"../tmp/recon_mesh"); + + size_t N = 64; + CImg image("../../../barbara.jpg"); + image.resize(N, N); + CImg image_out = image; + size_t rows = image.width(); + size_t cols = image.height(); + image_out.fill(0); + + for(index_t x = 0; x < rows; x++) + for(index_t y = 0; y < cols; y++) + { + index_t i = x + y * rows; + + image_out(x, y) = mesh->gt(i).z; + // gproshan_debug_var(mesh->gt(i).z); + } + image = image.get_normalize(0, 255); + (image_out).display(); + return error; } From cb6a5fb93a20f1afa5bc307f078e722bc8eea712 Mon Sep 17 00:00:00 2001 From: lizeth Date: Wed, 23 Oct 2019 14:09:46 +0200 Subject: [PATCH 0014/1018] test straight image ok --- include/che_img.h | 2 ++ src/che_img.cpp | 10 ++++++++- src/mdict/d_hybrid_denoising.cpp | 38 +++++++++++++++----------------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/include/che_img.h b/include/che_img.h index a18af037..dde0814d 100644 --- a/include/che_img.h +++ b/include/che_img.h @@ -15,6 +15,8 @@ class che_img : public che che_img(const che_img & mesh); virtual ~che_img(); void write_file(const std::string & file) const; + //void save_img(const std::string & file, size_t tam) const; + static void save_img(const che * mesh, const std::string & file, size_t tam); private: void read_file(const std::string & file); diff --git a/src/che_img.cpp b/src/che_img.cpp index b5978011..918e3d24 100644 --- a/src/che_img.cpp +++ b/src/che_img.cpp @@ -50,7 +50,7 @@ void che_img::read_file(const string & file) VT[he++] = (i - 1) * img.height() + j - 1; } - GT[v++] = vertex(i, img.height() - j - 1, -img(i, j)); + GT[v++] = vertex(i,j, img(i, j)); } thread([](CImg img) { img.display(); }, img).detach(); @@ -75,7 +75,15 @@ void che_img::write_file(const string & file) const os.close(); } +void save_img(const che * mesh, const std::string & file, size_t tam) +{ + CImg image_out(tam,tam); + + for(size_t v = 0; v < mesh->n_vertices(); v++) + image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; + image_out.save(file.c_str()); +} } // namespace gproshan diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 33dbc8b0..4bd9a17f 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -12,13 +12,14 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { - size_t N = 64; + size_t N = 4; CImg image(file.c_str()); image.resize(N, N); image.save("../tmp/image_128.jpg"); + (image).display(); image = image.get_normalize(0, 1); - + (image).display(); size_t p = 8; // square side of each patche size_t rows = image.width(); size_t cols = image.height(); @@ -32,10 +33,12 @@ void test_hybrid_denoising(const string & file) a_mat X(n, M); che * mesh = new che_img("../tmp/image_128.jpg"); + //mesh->write_file("../tmp/image_128"); che_off::write_file(mesh,"../tmp/image_128"); + std::vector patches(M); ///< vector of patches. std::vector patches_map(M); ///< invert index vertex to patches. - +/* for(index_t x = 0; x < rows; x++) for(index_t y = 0; y < cols; y++) { @@ -94,8 +97,16 @@ void test_hybrid_denoising(const string & file) } vertex * new_vertices = (vertex *) V.memptr(); mesh->set_vertices(new_vertices, mesh->n_vertices(), 0); +*/ + string input_str = "barbara_input.jpg"; + CImg image_out=image; + for(size_t v = 0; v < mesh->n_vertices(); v++) + image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; + image_out.save(file.c_str()); + //che_img::save_img(mesh, "../tmp/barbara_input.jpg", rows); +/* CImg image_out = image; image_out.fill(0); @@ -107,26 +118,13 @@ void test_hybrid_denoising(const string & file) image_out(x, y) = mesh->gt(i).z; // gproshan_debug_var(mesh->gt(i).z); } -/* - rows = image.width(); - cols = image.height(); - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) - { - index_t dx = p, dy = p; - if(x < p && x < dx) dx = x + 1; - if(y < p && y < dy) dy = y + 1; - if((rows - x) < p && (rows - x) < dx) dx = rows - x; - if((cols - y) < p && (cols - y) < dy) dy = cols - y; - - image_out(x, y) /= dx * dy; - } -*/ + image_out.save(tmp_file_path("barbara_input.jpg").c_str()); // CImg diff = abs(image - image_out); -// (image, image_out, diff).display(); +// (image, image_out, diff).display();*/ // (image_out).display(); // image_out = image_out.get_normalize(0, 255); -// (image, image_out).display(); +// CImg image_out("../tmp/barbara_input.jpg"); + (image, image_out).display(); } From c4fe9edf42b46120f3228e20f18f22e389cc6931 Mon Sep 17 00:00:00 2001 From: lizeth Date: Wed, 23 Oct 2019 17:25:57 +0200 Subject: [PATCH 0015/1018] testing with images donde --- src/mdict/d_hybrid_denoising.cpp | 8 ++++---- src/mdict/d_mesh.cpp | 25 ++++++++----------------- src/mdict/d_mesh_denoising.cpp | 16 +++++++++------- src/mdict/patch.cpp | 8 ++++---- 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index 4bd9a17f..d28a4e5a 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -12,7 +12,7 @@ namespace gproshan::mdict { void test_hybrid_denoising(const string & file) { - size_t N = 4; + size_t N = 128; CImg image(file.c_str()); image.resize(N, N); @@ -34,7 +34,7 @@ void test_hybrid_denoising(const string & file) a_mat X(n, M); che * mesh = new che_img("../tmp/image_128.jpg"); //mesh->write_file("../tmp/image_128"); - che_off::write_file(mesh,"../tmp/image_128"); + che_off::write_file(mesh,"../tmp/barbara_input_img"); std::vector patches(M); ///< vector of patches. std::vector patches_map(M); ///< invert index vertex to patches. @@ -98,13 +98,13 @@ void test_hybrid_denoising(const string & file) vertex * new_vertices = (vertex *) V.memptr(); mesh->set_vertices(new_vertices, mesh->n_vertices(), 0); */ - string input_str = "barbara_input.jpg"; + char * input_str = "barbara_input.jpg"; CImg image_out=image; for(size_t v = 0; v < mesh->n_vertices(); v++) image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; - image_out.save(file.c_str()); + image_out.save("../tmp/barbara_input.jpg"); //che_img::save_img(mesh, "../tmp/barbara_input.jpg", rows); /* CImg image_out = image; diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index c8cf8323..7f19afcf 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -293,25 +293,16 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); che_off::write_file(mesh,"../tmp/recon_mesh"); - size_t N = 64; - CImg image("../../../barbara.jpg"); - image.resize(N, N); - CImg image_out = image; - size_t rows = image.width(); - size_t cols = image.height(); - image_out.fill(0); - - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) - { - index_t i = x + y * rows; +////// Images test + CImg image("../tmp/barbara_input.jpg"); - image_out(x, y) = mesh->gt(i).z; - // gproshan_debug_var(mesh->gt(i).z); - } - image = image.get_normalize(0, 255); - (image_out).display(); + CImg image_out=image; + + for(size_t v = 0; v < mesh->n_vertices(); v++) + image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; + image_out.save("../tmp/barbara_output.jpg"); + (image,image_out).display(); return error; } diff --git a/src/mdict/d_mesh_denoising.cpp b/src/mdict/d_mesh_denoising.cpp index 5686d799..531ea3bf 100644 --- a/src/mdict/d_mesh_denoising.cpp +++ b/src/mdict/d_mesh_denoising.cpp @@ -12,18 +12,20 @@ void test_mesh_denoising(const string & file) { che * mesh = new che_off(file.c_str()); - size_t n = 4; - size_t m = 16; + size_t n = 16; + size_t m = 256; size_t M = 0; - distance_t f = 1.2; + distance_t f = 1; bool learn = false; distance_t error; - //dictionary::L = 20; + dictionary::L = 20; basis * phi = new basis_dct(n); - + denoising dict(mesh, phi, m, M, f, learn,0); + error = dict.execute(); + dictionary::T = 3; //denoising dict(mesh, phi, m, M, f, learn,0); //dict.execute(); - +/* ofstream os("../tmp/test.txt"); for(; f<1.4; f+=0.1) @@ -44,7 +46,7 @@ void test_mesh_denoising(const string & file) os.close(); system("gnuplot -persist ../tmp/test_mesh.gp &"); - + */ delete phi; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 6688bfdb..6489e83f 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -164,18 +164,18 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra p(2) = mesh->gt(v).z; p = T.t() * (p - x); - //if(vertices.size() > expected_nv) break; + if(vertices.size() > expected_nv) break; if(toplevel[v] != current_toplevel) { if(count_toplevel == 0) break; current_toplevel++; count_toplevel = 0; } - if(norm(p) <= radio) - { + //if(norm(p) <= radio) + //{ vertices.push_back(v); count_toplevel++; - } + //} mesh->link(link, v); for(const index_t & he: link) From d800f536aac8ffc2ab2912e49aa949b63fb3fb30 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 24 Oct 2019 10:47:36 +0200 Subject: [PATCH 0016/1018] how I think that we can gather vertices planar disk projection --- src/mdict/patch.cpp | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 6489e83f..540fbfba 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -144,14 +144,12 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra if(vertices.size()) vertices.clear(); vector qvertices; + qvertices.reserve(expected_nv); - vertices.reserve(expected_nv); + memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices()); - size_t count_toplevel = 0; - size_t current_toplevel = 0; - a_vec p(3); link_t link; toplevel[v] = 0; @@ -163,32 +161,24 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra p(1) = mesh->gt(v).y; p(2) = mesh->gt(v).z; p = T.t() * (p - x); - - if(vertices.size() > expected_nv) break; - if(toplevel[v] != current_toplevel) + p(2) = 0; + + if(norm(p) <= radio) { - if(count_toplevel == 0) break; - current_toplevel++; - count_toplevel = 0; - } - //if(norm(p) <= radio) - //{ vertices.push_back(v); - count_toplevel++; - //} - mesh->link(link, v); - for(const index_t & he: link) - { - const index_t & u = mesh->vt(he); - if(toplevel[u] == NIL) + mesh->link(link, v); + for(const index_t & he: link) { - qvertices.push_back(u); - toplevel[u] = toplevel[v] + 1; + const index_t & u = mesh->vt(he); + if(toplevel[u] == NIL) + { + qvertices.push_back(u); + toplevel[u] = toplevel[v] + 1; + } } + link.clear(); } - //gproshan_debug_var(vertices.size()); - link.clear(); } } From 3510775741d24365ca1925a560bcebe240cdc9c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 24 Oct 2019 17:04:58 +0200 Subject: [PATCH 0017/1018] gathering vertices patch debugging --- src/mdict/dictionary.cpp | 7 +++--- src/mdict/patch.cpp | 52 +++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 4ca924bb..b7ff7b45 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -79,7 +79,7 @@ void dictionary::init_sampling() if(M == 0) { M = mesh->n_vertices(); - phi_basis->radio = T * mesh->mean_edge(); + phi_basis->radio = mesh->mean_edge(); } else { @@ -103,16 +103,17 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) patches.resize(M); patches_map.resize(n_vertices); - #pragma omp parallel + // #pragma omp parallel { index_t * toplevel = new index_t[n_vertices]; - #pragma omp for + // #pragma omp for for(index_t s = 0; s < M; s++) { index_t v = sample(s); patches[s].init(mesh, v, dictionary::T, phi_basis->radio); } + delete [] toplevel; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 540fbfba..8bf31a57 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -13,6 +13,7 @@ #include #include +#include // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -135,51 +136,52 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl link.clear(); } - } void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & radio, index_t * toplevel) { + gproshan_debug(PATCH); + assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); if(vertices.size()) vertices.clear(); - - vector qvertices; - - qvertices.reserve(expected_nv); vertices.reserve(expected_nv); + + priority_queue > qvertices; memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices()); a_vec p(3); link_t link; toplevel[v] = 0; - qvertices.push_back(v); - for(index_t i = 0; i < qvertices.size(); i++) + qvertices.push({0, v}); + + gproshan_debug(PATCH); + while(!qvertices.empty()) { - const index_t & v = qvertices[i]; - p(0) = mesh->gt(v).x; - p(1) = mesh->gt(v).y; - p(2) = mesh->gt(v).z; - p = T.t() * (p - x); - p(2) = 0; - - if(norm(p) <= radio) - { - vertices.push_back(v); + if(-qvertices.top().first > radio) break; + + const index_t & v = qvertices.top().second; + qvertices.pop(); - mesh->link(link, v); - for(const index_t & he: link) + vertices.push_back(v); + + mesh->link(link, v); + for(const index_t & he: link) + { + const index_t & u = mesh->vt(he); + if(toplevel[u] == NIL) { - const index_t & u = mesh->vt(he); - if(toplevel[u] == NIL) - { - qvertices.push_back(u); - toplevel[u] = toplevel[v] + 1; - } + p(0) = mesh->gt(u).x; + p(1) = mesh->gt(u).y; + p(2) = mesh->gt(u).z; + p = T.t() * (p - x); + qvertices.push({-norm(p), u}); + toplevel[u] = toplevel[v] + 1; } link.clear(); } } + gproshan_debug(PATCH); } /// Compute the principal directions of the patch, centering in the vertex \f$v\f$. From 76776c02515c7ef35887d40dd53c55fbab84195a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Oct 2019 11:18:05 +0200 Subject: [PATCH 0018/1018] fix gather patches --- src/mdict/dictionary.cpp | 6 +++--- src/mdict/patch.cpp | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index b7ff7b45..2ea7294e 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -103,15 +103,15 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) patches.resize(M); patches_map.resize(n_vertices); - // #pragma omp parallel + #pragma omp parallel { index_t * toplevel = new index_t[n_vertices]; - // #pragma omp for + #pragma omp for for(index_t s = 0; s < M; s++) { index_t v = sample(s); - patches[s].init(mesh, v, dictionary::T, phi_basis->radio); + patches[s].init(mesh, v, dictionary::T, phi_basis->radio, toplevel); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8bf31a57..8818742e 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -140,9 +140,8 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & radio, index_t * toplevel) { - gproshan_debug(PATCH); - assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); + if(vertices.size()) vertices.clear(); vertices.reserve(expected_nv); @@ -152,15 +151,13 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra a_vec p(3); link_t link; + toplevel[v] = 0; qvertices.push({0, v}); - gproshan_debug(PATCH); while(!qvertices.empty()) { - if(-qvertices.top().first > radio) break; - - const index_t & v = qvertices.top().second; + index_t v = qvertices.top().second; qvertices.pop(); vertices.push_back(v); @@ -175,13 +172,15 @@ void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & ra p(1) = mesh->gt(u).y; p(2) = mesh->gt(u).z; p = T.t() * (p - x); - qvertices.push({-norm(p), u}); + toplevel[u] = toplevel[v] + 1; + + if(norm(p) < radio) + qvertices.push({-norm(p), u}); } link.clear(); } } - gproshan_debug(PATCH); } /// Compute the principal directions of the patch, centering in the vertex \f$v\f$. From c923d7f1047ed97b52371f985fccb5cd17fce598 Mon Sep 17 00:00:00 2001 From: lizeth Date: Tue, 29 Oct 2019 13:18:35 +0100 Subject: [PATCH 0019/1018] fixing image reconstruction --- src/mdict/d_mesh.cpp | 9 +++++++-- src/mdict/d_mesh_denoising.cpp | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 7f19afcf..64dd7c90 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -297,9 +297,14 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve CImg image("../tmp/barbara_input.jpg"); CImg image_out=image; + //image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; - for(size_t v = 0; v < mesh->n_vertices(); v++) - image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; + index_t v = 0; + for(int i = 0; i < image.width(); i++) + for(int j = 0; j < image.height(); j++) + { + image_out(i,j) = mesh->gt(v++).z; + } image_out.save("../tmp/barbara_output.jpg"); (image,image_out).display(); diff --git a/src/mdict/d_mesh_denoising.cpp b/src/mdict/d_mesh_denoising.cpp index 531ea3bf..c33e4aa9 100644 --- a/src/mdict/d_mesh_denoising.cpp +++ b/src/mdict/d_mesh_denoising.cpp @@ -15,14 +15,14 @@ void test_mesh_denoising(const string & file) size_t n = 16; size_t m = 256; size_t M = 0; - distance_t f = 1; + distance_t f = 1.3; bool learn = false; distance_t error; - dictionary::L = 20; +// dictionary::L = 10; basis * phi = new basis_dct(n); denoising dict(mesh, phi, m, M, f, learn,0); error = dict.execute(); - dictionary::T = 3; + //dictionary::T = 3; //denoising dict(mesh, phi, m, M, f, learn,0); //dict.execute(); /* From 3628eb2d676512d1639a393c65833180fc1a0887 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Oct 2019 13:38:39 +0100 Subject: [PATCH 0020/1018] image out --- src/mdict/d_mesh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 64dd7c90..e4bb3ba6 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -296,7 +296,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve ////// Images test CImg image("../tmp/barbara_input.jpg"); - CImg image_out=image; + CImg image_out(image.width(), image.height()); //image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; index_t v = 0; From 5dde90a3517d0796de67176b00659ed847a7a604 Mon Sep 17 00:00:00 2001 From: lizeth Date: Tue, 29 Oct 2019 15:49:05 +0100 Subject: [PATCH 0021/1018] coloring mse error v1 --- include/mdict/d_mesh.h | 2 +- include/mdict/dictionary.h | 2 ++ src/app_viewer.cpp | 1 + src/mdict/d_mesh.cpp | 12 ++++++++---- src/mdict/dictionary.cpp | 6 ++++++ 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index 840f8f11..c6ea33f4 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,7 +111,7 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); +distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, , distance_t & dist, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 7f905a53..9d008407 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -38,10 +38,12 @@ class dictionary bool d_plot; bool learn; ///< plot atoms and basis with gnuplot. + distance_t * dist; public: static size_t L; ///< sparsity, norm L_0, default 10. static size_t T; ///< factor of patches' size, default 5 toplesets. + const distance_t & operator[](const index_t & i) const; protected: dictionary( che *const & _mesh, ///< pointer to input mesh. diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index d4ea097c..dab86dba 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -466,6 +466,7 @@ void viewer_process_denoising() dict.execute(); delete phi; + //viewer::mesh().update_colors(&fm[0]); viewer::mesh().update_normals(); } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index e4bb3ba6..bc2ed651 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -241,7 +241,7 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i) +distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t & dist, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); @@ -283,7 +283,11 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve distance_t error = 0; #pragma omp parallel for reduction(+: error) for(index_t v = v_i; v < mesh->n_vertices(); v++) - error += *(new_vertices[v] - mesh->get_vertex(v)); + { + dist[v] = *(new_vertices[v] - mesh->get_vertex(v)) + // error += dist[v]; + } + gproshan_debug_var(mesh->n_vertices()); error /= mesh->n_vertices(); @@ -292,7 +296,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve gproshan_debug_var(v_i); mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); che_off::write_file(mesh,"../tmp/recon_mesh"); - +/* ////// Images test CImg image("../tmp/barbara_input.jpg"); @@ -307,7 +311,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve } image_out.save("../tmp/barbara_output.jpg"); - (image,image_out).display(); + (image,image_out).display();*/ return error; } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 2ea7294e..986dca15 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -26,6 +26,7 @@ dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), learn(_learn), d_plot(_d_plot) { A.eye(phi_basis->dim, m); + dist = new distance_t[mesh->n_vertices()]; } dictionary::~dictionary() @@ -240,6 +241,11 @@ index_t dictionary::sample(const index_t & s) return s; } +const distance_t & dictionary::operator[](const index_t & i) const +{ + assert(i < mesh->n_vertices()); + return dist[i]; +} } // namespace gproshan::mdict From 7bb2e90c277d8046efd7604d0cba8db6aa6c28cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Oct 2019 15:58:57 +0100 Subject: [PATCH 0022/1018] coloring error v2 --- include/mdict/d_mesh.h | 2 +- src/mdict/d_mesh.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index c6ea33f4..18a348dc 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,7 +111,7 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, , distance_t & dist, const index_t & v_i = 0); +distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index bc2ed651..5d71d030 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -241,7 +241,7 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t & dist, const index_t & v_i) +distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); @@ -284,7 +284,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve #pragma omp parallel for reduction(+: error) for(index_t v = v_i; v < mesh->n_vertices(); v++) { - dist[v] = *(new_vertices[v] - mesh->get_vertex(v)) + dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); // error += dist[v]; } From 4ec0d1a49bf52db2a34d84b8734f271f66a60d4c Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 30 Oct 2019 14:23:54 +0100 Subject: [PATCH 0023/1018] fixing coloring --- src/app_viewer.cpp | 2 +- src/mdict/dictionary.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index dab86dba..fd05ba77 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -466,7 +466,7 @@ void viewer_process_denoising() dict.execute(); delete phi; - //viewer::mesh().update_colors(&fm[0]); + //viewer::mesh().update_colors(&dict[0]); viewer::mesh().update_normals(); } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 986dca15..13614e8e 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -189,7 +189,7 @@ distance_t dictionary::mesh_reconstruction() gproshan_debug(MDICT); assert(n_vertices == mesh->n_vertices()); - return mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha); + return mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha, dist); } void dictionary::update_alphas(a_mat & alpha, size_t threshold) { From f636f1b23b6d52c60a882d4ba2b651dc15cb1ee7 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 30 Oct 2019 15:00:48 +0100 Subject: [PATCH 0024/1018] coliring error finish --- src/app_viewer.cpp | 2 +- src/mdict/d_mesh.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index fd05ba77..bf2a5911 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -466,7 +466,7 @@ void viewer_process_denoising() dict.execute(); delete phi; - //viewer::mesh().update_colors(&dict[0]); + viewer::mesh().update_colors(&dict[0]); viewer::mesh().update_normals(); } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 5d71d030..08dbbb0b 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -285,7 +285,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve for(index_t v = v_i; v < mesh->n_vertices(); v++) { dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); - // error += dist[v]; + error += *(new_vertices[v] - mesh->get_vertex(v)); } From 024129c29308148335ad89341709d30a40c1ab6c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 1 Nov 2019 14:43:53 +0100 Subject: [PATCH 0025/1018] atan error display --- src/app_viewer.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index bf2a5911..c4a02341 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -467,6 +467,10 @@ void viewer_process_denoising() delete phi; viewer::mesh().update_colors(&dict[0]); + + #pragma omp parallel for + for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + viewer::vcolor(v) = 2 * atan(viewer::vcolor(v) * 10) / M_PI; viewer::mesh().update_normals(); } From cbecd8a8bbf759e4a0b306068740df2b3baa03b8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 8 Nov 2019 12:04:36 +0100 Subject: [PATCH 0026/1018] review KSVD dense for mesh --- include/mdict/dictionary.h | 18 ++++++++++-------- include/mdict/mdict.h | 10 +++++++--- src/mdict/dictionary.cpp | 6 +++--- src/mdict/mdict.cpp | 37 +++++++++++++++++++++---------------- 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 9d008407..c04e39c3 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -25,24 +25,26 @@ class dictionary size_t m; ///< number of dictionary atoms. size_t M; ///< number of patches. - a_mat A; ///< dictionary continuous matrix. - a_mat alpha; ///< sparse coding matrix. + a_mat A; ///< dictionary continuous matrix. + a_mat alpha; ///< sparse coding matrix. distance_t f; distance_t s_radio; ///< sampling geodesic radio. - std::vector sampling; ///< samples, center of patches if sampling. + std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. - std::vector patches_map; ///< invert index vertex to patches. + std::vector patches_map; ///< invert index vertex to patches. double d_time; ///< time of operations. - bool d_plot; + bool d_plot; ///< plot atoms and basis with gnuplot. bool learn; - ///< plot atoms and basis with gnuplot. distance_t * dist; public: - static size_t L; ///< sparsity, norm L_0, default 10. - static size_t T; ///< factor of patches' size, default 5 toplesets. + static size_t K; ///< number of iterations KSVD. + static size_t L; ///< sparsity, norm L_0, default 10. + static size_t T; ///< factor of patches' size, default 5 toplesets. + + public: const distance_t & operator[](const index_t & i) const; protected: diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index d7e8b81f..f78ef801 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -29,6 +29,7 @@ a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, co void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); + // DENSE tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L); @@ -39,11 +40,14 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L); void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); -void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L); -void OMP_all_patches_ksvt(a_mat & alpha, a_mat & A, std::vector & patches, size_t M, size_t L); +// MESH DENSE + +a_vec OMP(const a_mat & A, const patch & p, const size_t & L); + +a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L); -void KSVDT(a_mat & A, std::vector & patches, size_t M, size_t L); +void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); } // namespace gproshan::mdict diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 13614e8e..4d947f81 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -20,6 +20,7 @@ namespace gproshan::mdict { size_t dictionary::L = 10; +size_t dictionary::K = 10; size_t dictionary::T = 5; dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _d_plot): @@ -45,7 +46,7 @@ void dictionary::learning() { A.eye(phi_basis->dim, m); // A.random(phi_basis->dim, m); - KSVDT(A, patches, M, L); + KSVD(A, patches, L, K); gproshan_debug(Ok); @@ -66,8 +67,7 @@ void dictionary::sparse_coding() { gproshan_debug(MDICT); - alpha.zeros(m, M); - OMP_all_patches_ksvt(alpha, A, patches, M, L); + alpha = OMP_all(patches, A, L); } void dictionary::init_sampling() diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 3cc40ee1..66f7409e 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -14,6 +14,7 @@ namespace gproshan::mdict { const real_t sigma = 0.001; + // SPARSE bool locval_t::operator < (const locval_t & lc) @@ -37,7 +38,7 @@ void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_m for(index_t k = 0; k < selected_atoms.size(); k++) { #pragma omp critical - alpha.push_back({selected_atoms(k), i, aa(k)}); + alpha.push_back({selected_atoms(k), i, aa(k)}); } } @@ -110,6 +111,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) } } + // DENSE tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) @@ -142,7 +144,7 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) tie(aa, selected_atoms) = _OMP(x, D, L); alpha.elem(selected_atoms) = aa; -// gproshan_debug_var(size(alpha)); + return alpha; } @@ -150,7 +152,7 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { a_mat alpha(D.n_cols, X.n_cols); -// #pragma omp parallel for + #pragma omp parallel for for(index_t i = 0; i < X.n_cols; i++) alpha.col(i) = OMP(X.col(i), D, L); @@ -184,34 +186,37 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) } } -// MESH -void OMP_patch(a_mat & alpha, const a_mat & A, const index_t & i, patch & p, const size_t & L) +// MESH DENSE + +a_vec OMP(const a_mat & A, const patch & p, const size_t & L) { - alpha.col(i) = OMP(p.xyz.row(2).t(), p.phi * A, L); + return OMP(p.xyz.row(2).t(), p.phi * A, L); } -void OMP_all_patches_ksvt(a_mat & alpha, a_mat & A, vector & patches, size_t M, size_t L) +a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) { + a_mat alpha(A.n_cols, patches.size()); + #pragma omp parallel for - for(index_t i = 0; i < M; i++) - OMP_patch(alpha, A, i, patches[i], L); + for(index_t i = 0; i < patches.size(); i++) + alpha.col(i) = OMP(A, patches[i], L); + + return alpha; } -void KSVDT(a_mat & A, vector & patches, size_t M, size_t L) +void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) { size_t K = A.n_rows; - size_t m = A.n_cols; - a_mat alpha(m, M); + a_mat alpha; - size_t iter = L; - while(iter--) + while(k--) { - OMP_all_patches_ksvt(alpha, A, patches, M, L); + alpha = OMP_all(patches, A, L); #pragma omp parallel for - for(index_t j = 0; j < m; j++) + for(index_t j = 0; j < A.n_cols; j++) { arma::uvec omega = find(abs(alpha.row(j)) > 0); From cb770405ca8aeefc9beebe33856a5288338a74dc Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 11 Nov 2019 16:11:28 +0100 Subject: [PATCH 0027/1018] adding new inpainting execute functionn : mask --- include/mdict/basis.h | 1 + include/mdict/denoising.h | 2 +- include/mdict/inpainting.h | 4 ++ include/mdict/patch.h | 12 ++++- src/mdict/basis.cpp | 4 ++ src/mdict/inpainting.cpp | 92 ++++++++++++++++++++++++++++++++++++++ src/mdict/patch.cpp | 37 +++++++++++++++ 7 files changed, 150 insertions(+), 2 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 9d4e8d0e..9f594fe5 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -27,6 +27,7 @@ class basis virtual void discrete(a_mat & phi, const a_mat & xy) = 0; void plot_basis(); void plot_atoms(const a_mat & A); + size_t get_dim(); private: virtual void plot_basis(std::ostream & os) = 0; diff --git a/include/mdict/denoising.h b/include/mdict/denoising.h index e738e827..70c68887 100644 --- a/include/mdict/denoising.h +++ b/include/mdict/denoising.h @@ -12,7 +12,7 @@ namespace gproshan::mdict { class denoising : public dictionary { public: - denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = true); + denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = false); virtual ~denoising() = default; distance_t execute(); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index a1f9f66d..2c5e7b2b 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -4,6 +4,9 @@ #include "dictionary.h" #include "../che_poisson.h" #include "../che_fill_hole.h" +#include "sampling.h" +#include "geodesics.h" +#include "geodesics_ptp.h" // geometry processing and shape analysis framework @@ -18,6 +21,7 @@ class inpainting : public dictionary virtual ~inpainting() = default; distance_t execute(); + distance_t execute_tmp(); }; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 62582191..8de8548c 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -26,6 +26,7 @@ namespace gproshan::mdict { class dictionary; typedef function fmask_t; +typedef function fmask_local_t; typedef std::vector > vpatches_t; /// @@ -51,6 +52,11 @@ class patch const distance_t & radio, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); + void init_disjoint(che * mesh, + const index_t & v, + const size_t & n_toplevels, + vector & _vertices, + index_t * _toplevel = nullptr); void transform(); @@ -61,7 +67,11 @@ class patch const index_t & p, const fmask_t & mask = nullptr ); - + void reset_xyz_disjoint( che * mesh, + std::vector & vpatches, + const index_t & p, + const fmask_local_t & mask = nullptr + ); const a_vec normal(); void save(const real_t & radio, const size_t & imsize, CImgList & imlist); diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index dd90ccf7..ec492996 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -67,6 +67,10 @@ void basis::plot_atoms(const a_mat & A) system(file.c_str()); } +size_t basis::get_dim() +{ + return dim; +} } // namespace gproshan::mdict diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 4a264d49..c770de72 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -11,6 +11,98 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size } distance_t inpainting::execute() +{ + size_t M = mesh->n_vertices()/36; + gproshan_log(Sampling); + + distance_t radio; + static std::vector select_vertices; + TIC(d_time) + load_sampling(select_vertices, radio, mesh, M); + TOC(d_time) + gproshan_log_var(d_time); + TIC(d_time) +#ifdef GPROSHAN_CUDA + geodesics ptp( mesh, select_vertices, geodesics::PTP_GPU, nullptr, 1); +#else + geodesics ptp( mesh, select_vertices, geodesics::FM, nullptr, 1); +#endif + TOC(d_time) + gproshan_log_var(d_time); + + // creating new patches + std::vector vertices[M]; + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(select_vertices[s]); + } + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + vertices[ ptp.clusters[i] ].push_back(i) ; + } + //initializing patch + #pragma omp parallel + { + index_t * toplevel = new index_t[n_vertices]; + + #pragma omp for + for(index_t s = 0; s < M; s++) + { + patches[s].init_disjoint(mesh, select_vertices[s], dictionary::T, vertices[s], toplevel); + } + + delete [] toplevel; + } + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; + + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < M; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < M; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < M; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= M; + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); + #endif + + size_t percent = 30; + + for(index_t s = 0; s < M; s++) + patches[s].reset_xyz_disjoint(mesh, patches_map, s, [&percent](const index_t & i, size_t tam) -> bool { return i < ceil(tam * percent/ 100); }); + + #pragma omp parallel for + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + + p.transform(); + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + + } + // patches[s].init(mesh, v, dictionary::T, phi_basis->radio, toplevel); + /* p(0) = mesh->gt(u).x; + p(1) = mesh->gt(u).y; + p(2) = mesh->gt(u).z; + p = T.t() * (p - x);*/ + +} + +distance_t inpainting::execute_tmp() { // fill holes size_t threshold = mesh->n_vertices(); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8818742e..d4a69cfa 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -41,6 +41,18 @@ void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, cons if(!_toplevel) delete [] toplevel; } +void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +{ + index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; + + gather_vertices(mesh, v, n_toplevels, toplevel); + jet_fit_directions(mesh, v); + vertices = std::move(_vertices); +// gather_vertices(mesh, v, radio, toplevel); + + if(!_toplevel) delete [] toplevel; +} + // xyz = E.t * (xyz - avg) void patch::transform() { @@ -80,6 +92,31 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } +void patch::reset_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p, const fmask_local_t & mask) +{ + size_t m = vertices.size(); + if(mask) + { + m = 0; + for(index_t i = 0; i < vertices.size(); i++) + if(mask(i, vertices.size())) m++; + } + + xyz.set_size(3, m); + for(index_t j = 0, i = 0; i < vertices.size(); i++) + { + if(!mask || mask(i, vertices.size())) + { + const vertex & v = mesh->gt(vertices[i]); + xyz(0, j) = v.x; + xyz(1, j) = v.y; + xyz(2, j) = v.z; + + vpatches[vertices[i]].push_back({p, j++}); + } + } +} + const a_vec patch::normal() { return T.col(2); From bf4ad5a964a2e2ee4b5eed92d1ebbddf817857b3 Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 11 Nov 2019 16:49:50 +0100 Subject: [PATCH 0028/1018] coloring mask --- include/mdict/patch.h | 2 ++ src/app_viewer.cpp | 2 ++ src/mdict/inpainting.cpp | 8 ++------ src/mdict/patch.cpp | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 8de8548c..edb19c3a 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -68,6 +68,7 @@ class patch const fmask_t & mask = nullptr ); void reset_xyz_disjoint( che * mesh, + distance_t * dist, std::vector & vpatches, const index_t & p, const fmask_local_t & mask = nullptr @@ -76,6 +77,7 @@ class patch void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); + private: /// Gather the vertices needed to compute the jet_fit_directions of the patch. diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c4a02341..4a6d05ff 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -511,6 +511,8 @@ void viewer_process_inpaiting() dict.execute(); delete phi; + viewer::mesh().update_colors(&dict[0]); + viewer::mesh().update_normals(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index c770de72..ec88e558 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -82,7 +82,7 @@ distance_t inpainting::execute() size_t percent = 30; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, patches_map, s, [&percent](const index_t & i, size_t tam) -> bool { return i < ceil(tam * percent/ 100); }); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, [&percent](const index_t & i, size_t tam) -> bool { return i < ceil(tam * percent/ 100); }); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -94,11 +94,7 @@ distance_t inpainting::execute() phi_basis->discrete(p.phi, p.xyz); } - // patches[s].init(mesh, v, dictionary::T, phi_basis->radio, toplevel); - /* p(0) = mesh->gt(u).x; - p(1) = mesh->gt(u).y; - p(2) = mesh->gt(u).z; - p = T.t() * (p - x);*/ + } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d4a69cfa..9960977e 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -92,14 +92,14 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } -void patch::reset_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p, const fmask_local_t & mask) +void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_local_t & mask) { size_t m = vertices.size(); if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) - if(mask(i, vertices.size())) m++; + if(mask(i, vertices.size())) { dist[vertices[i] ] = 1; m++; } else {dist[vertices[i] ] = 0;}; } xyz.set_size(3, m); From 7668b9bb1b7ff57548ed8bac7dbfd1fb6072aff4 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 12 Nov 2019 15:22:28 +0100 Subject: [PATCH 0029/1018] Mask inpainting ready --- .gitignore | 2 ++ src/app_viewer.cpp | 2 +- src/mdict/dictionary.cpp | 2 +- src/mdict/inpainting.cpp | 59 ++++++++++++++++++++++------------------ src/mdict/patch.cpp | 7 +++-- 5 files changed, 41 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index 609552d3..c5b6cd07 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ tmp/* *.user .vscode/* .DS_Store + +src/.vscode/settings.json diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 4a6d05ff..bf3c0009 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -503,7 +503,7 @@ void viewer_process_inpaiting() distance_t f; bool learn; - gproshan_log(parameters: (n, m, M, f, learn)); + gproshan_input(n m M f learn); cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 4d947f81..5090502e 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 10; +size_t dictionary::L = 20; size_t dictionary::K = 10; size_t dictionary::T = 5; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ec88e558..bc18eb91 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -12,20 +12,18 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size distance_t inpainting::execute() { - size_t M = mesh->n_vertices()/36; - gproshan_log(Sampling); + M = mesh->n_vertices()/36; + gproshan_log_var(M); + + + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + - distance_t radio; - static std::vector select_vertices; - TIC(d_time) - load_sampling(select_vertices, radio, mesh, M); - TOC(d_time) - gproshan_log_var(d_time); - TIC(d_time) #ifdef GPROSHAN_CUDA - geodesics ptp( mesh, select_vertices, geodesics::PTP_GPU, nullptr, 1); + geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); #else - geodesics ptp( mesh, select_vertices, geodesics::FM, nullptr, 1); + geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); #endif TOC(d_time) gproshan_log_var(d_time); @@ -37,27 +35,36 @@ distance_t inpainting::execute() #pragma omp for for(index_t s = 0; s < M; s++) { - vertices[s].push_back(select_vertices[s]); + vertices[s].push_back(sample(s)); } - #pragma omp parallel for + + //#pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - vertices[ ptp.clusters[i] ].push_back(i) ; + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ ptp.clusters[i] ].push_back(i) ; } + + + + + gproshan_log(seed vertices); + + patches.resize(M); + patches_map.resize(n_vertices); //initializing patch #pragma omp parallel - { - index_t * toplevel = new index_t[n_vertices]; + { + index_t * toplevel = new index_t[mesh->n_vertices()]; - #pragma omp for - for(index_t s = 0; s < M; s++) - { - patches[s].init_disjoint(mesh, select_vertices[s], dictionary::T, vertices[s], toplevel); - } + #pragma omp for + for(index_t s = 0; s < M; s++) + { + patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); - delete [] toplevel; - } + } #ifndef NDEBUG size_t patch_avg_size = 0; size_t patch_min_size = NIL; @@ -78,8 +85,8 @@ distance_t inpainting::execute() gproshan_debug_var(patch_min_size); gproshan_debug_var(patch_max_size); #endif - - size_t percent = 30; + } + size_t percent = 90; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, [&percent](const index_t & i, size_t tam) -> bool { return i < ceil(tam * percent/ 100); }); @@ -93,7 +100,7 @@ distance_t inpainting::execute() p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - } + } } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 9960977e..7d476a4e 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -47,10 +47,10 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); + //vertices = _vertices; vertices = std::move(_vertices); -// gather_vertices(mesh, v, radio, toplevel); - if(!_toplevel) delete [] toplevel; + if(!_toplevel) delete [] toplevel; // If it is null } // xyz = E.t * (xyz - avg) @@ -99,7 +99,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector { m = 0; for(index_t i = 0; i < vertices.size(); i++) - if(mask(i, vertices.size())) { dist[vertices[i] ] = 1; m++; } else {dist[vertices[i] ] = 0;}; + if(mask(i, vertices.size())) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } xyz.set_size(3, m); @@ -154,6 +154,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl link_t link; toplevel[v] = 0; vertices.push_back(v); + for(index_t i = 0; i < vertices.size(); i++) { const index_t & v = vertices[i]; From f115d18e32517402d2830ee67be94664d1d6c609 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 13 Nov 2019 12:09:05 +0100 Subject: [PATCH 0030/1018] review KSVD update code similar to the proposed by https://github.com/orlitany/Cloud_dictionary/blob/master/Code/trainDictionary.m --- src/mdict/mdict.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 66f7409e..08574c15 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -209,13 +209,16 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) { size_t K = A.n_rows; - a_mat alpha; + a_mat & new_A = A; + a_mat alpha, D, E, sum, sum_error; + real_t aj; + a_vec a; while(k--) { alpha = OMP_all(patches, A, L); - #pragma omp parallel for + #pragma omp parallel for private(a, aj, D, E, sum, sum_error) for(index_t j = 0; j < A.n_cols; j++) { arma::uvec omega = find(abs(alpha.row(j)) > 0); @@ -223,23 +226,24 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) a_mat sum(K, K, arma::fill::zeros); a_vec sum_error(K, arma::fill::zeros); - for(arma::uword o: omega) + for(arma::uword & i: omega) { - sum += alpha(j, o) * patches[o].phi.t() * patches[o].phi; + a = alpha.col(i); + a(j) = 0; - a_mat D = patches[o].phi * A; - D.col(j).zeros(); - a_mat e = patches[o].xyz.row(2).t() - D * alpha.col(o); + D = patches[i].phi * A; + E = patches[i].xyz.row(2).t() - D * alpha.col(i); + aj = as_scalar(E.t() * D.col(j) / (D.col(j).t() * D.col(j))); - sum_error += alpha(j, o) * patches[o].phi.t() * e; + sum += aj * aj * patches[i].phi.t() * patches[i].phi; + sum_error += aj * patches[i].phi.t() * E; } + if(omega.size()) - { - a_vec X; - solve(X, sum, sum_error); - A.col(j) = X; - } + new_A.col(j) = solve(sum, sum_error); } + + A = new_A; } } From 446f4909ee4eeb3325823447f0ff3d25edc28511 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 13 Nov 2019 13:41:40 +0100 Subject: [PATCH 0031/1018] update ksvd mesh patches algorithm --- src/mdict/mdict.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 08574c15..ace51686 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -209,22 +209,25 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) { size_t K = A.n_rows; - a_mat & new_A = A; - a_mat alpha, D, E, sum, sum_error; + arma::uvec omega; + + a_mat new_A = A; + a_mat alpha, D, sum, sum_error; + a_vec a, e; + real_t aj; - a_vec a; while(k--) { alpha = OMP_all(patches, A, L); - #pragma omp parallel for private(a, aj, D, E, sum, sum_error) + #pragma omp parallel for private(omega, a, aj, D, e, sum, sum_error) for(index_t j = 0; j < A.n_cols; j++) { arma::uvec omega = find(abs(alpha.row(j)) > 0); - a_mat sum(K, K, arma::fill::zeros); - a_vec sum_error(K, arma::fill::zeros); + sum.zeros(K, K); + sum_error.zeros(K); for(arma::uword & i: omega) { @@ -232,11 +235,11 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) a(j) = 0; D = patches[i].phi * A; - E = patches[i].xyz.row(2).t() - D * alpha.col(i); - aj = as_scalar(E.t() * D.col(j) / (D.col(j).t() * D.col(j))); + e = patches[i].xyz.row(2).t() - D * a; + aj = as_scalar(e.t() * D.col(j) / (D.col(j).t() * D.col(j))); sum += aj * aj * patches[i].phi.t() * patches[i].phi; - sum_error += aj * patches[i].phi.t() * E; + sum_error += aj * patches[i].phi.t() * e; } if(omega.size()) From b1b1156d33448b9104ca20a812a5e067f538bd0a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 13 Nov 2019 14:17:14 +0100 Subject: [PATCH 0032/1018] globall mask is ready --- include/mdict/inpainting.h | 1 + src/mdict/inpainting.cpp | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 2c5e7b2b..3ad92cc1 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -7,6 +7,7 @@ #include "sampling.h" #include "geodesics.h" #include "geodesics_ptp.h" +#include // geometry processing and shape analysis framework diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bc18eb91..a0f3a397 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -38,17 +38,50 @@ distance_t inpainting::execute() vertices[s].push_back(sample(s)); } + bool * mask = new bool[mesh->n_vertices()]; + size_t * percentages_size = new size_t[M]; //#pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { + mask[i] = 0; ptp.clusters[i]--; if(sample(ptp.clusters[i]) != i) vertices[ ptp.clusters[i] ].push_back(i) ; } + + // create initial desired percentage sizes + size_t percent = 50; + #pragma omp for + for(index_t s = 0; s < M; s++) + { + percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; + } + std::default_random_engine generator; + std::uniform_int_distribution distribution(0,M); + int k = 0; + size_t rn=0; + while( k < M ) + { + rn = distribution(generator); + if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) + { + mask[rn] = 1; + percentages_size[ ptp.clusters[rn] ]--; + + } + if(percentages_size[ptp.clusters[rn] ] == 0) + k++; + + gproshan_log_var(mask[rn]); + + + } + + gproshan_log(our mask is ready); gproshan_log(seed vertices); @@ -86,10 +119,10 @@ distance_t inpainting::execute() gproshan_debug_var(patch_max_size); #endif } - size_t percent = 90; + for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, [&percent](const index_t & i, size_t tam) -> bool { return i < ceil(tam * percent/ 100); }); + patches[s].reset_xyz(mesh, patches_map, s, [&mask](const index_t & i) -> bool { return mask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) From 309d0eb979288061d8e2f4218e73a2805677155c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 13 Nov 2019 14:49:23 +0100 Subject: [PATCH 0033/1018] setting sparsity parameter L = 8, to high is bad possible error of float/double precision --- include/mdict/dictionary.h | 2 +- src/mdict/dictionary.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index c04e39c3..b29c90f6 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -35,8 +35,8 @@ class dictionary std::vector patches_map; ///< invert index vertex to patches. double d_time; ///< time of operations. - bool d_plot; ///< plot atoms and basis with gnuplot. bool learn; + bool d_plot; ///< plot atoms and basis with gnuplot. distance_t * dist; public: diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 5090502e..6e859e2a 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 20; +size_t dictionary::L = 8; size_t dictionary::K = 10; size_t dictionary::T = 5; @@ -173,7 +173,6 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) p.save_z(os); } os.close(); - /* // DRAW NORMALS DEBUG for(index_t s = 0; s < M; s++) { From 70d1234019775acec688aae2656e81d62dba8f72 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 13 Nov 2019 15:09:07 +0100 Subject: [PATCH 0034/1018] Inpaiting almost ready --- include/mdict/patch.h | 2 +- src/mdict/inpainting.cpp | 27 +++++++++++++++------------ src/mdict/patch.cpp | 6 +++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index edb19c3a..1cc6c3d3 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -71,7 +71,7 @@ class patch distance_t * dist, std::vector & vpatches, const index_t & p, - const fmask_local_t & mask = nullptr + const fmask_t & mask = nullptr ); const a_vec normal(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a0f3a397..62ca397f 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -51,7 +51,7 @@ distance_t inpainting::execute() } // create initial desired percentage sizes - size_t percent = 50; + size_t percent = 35; #pragma omp for for(index_t s = 0; s < M; s++) @@ -70,20 +70,15 @@ distance_t inpainting::execute() if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) { mask[rn] = 1; - percentages_size[ ptp.clusters[rn] ]--; - + percentages_size[ ptp.clusters[rn] ]--; } if(percentages_size[ptp.clusters[rn] ] == 0) k++; - - gproshan_log_var(mask[rn]); - - } gproshan_log(our mask is ready); - gproshan_log(seed vertices); + gproshan_log(initializing patches); patches.resize(M); patches_map.resize(n_vertices); @@ -114,15 +109,15 @@ distance_t inpainting::execute() patch_max_size = max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= M; - gproshan_debug_var(patch_avg_size); - gproshan_debug_var(patch_min_size); - gproshan_debug_var(patch_max_size); + //gproshan_debug_var(patch_avg_size); + //gproshan_debug_var(patch_min_size); + //gproshan_debug_var(patch_max_size); #endif } for(index_t s = 0; s < M; s++) - patches[s].reset_xyz(mesh, patches_map, s, [&mask](const index_t & i) -> bool { return mask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, [&mask](const index_t & i) -> bool { return mask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -135,6 +130,14 @@ distance_t inpainting::execute() } + gproshan_log(our patches are ready); + + // sparse coding and reconstruction with all patches + TIC(d_time) sparse_coding(); TOC(d_time) + gproshan_debug_var(d_time); + + TIC(d_time) mesh_reconstruction(); TOC(d_time) + gproshan_debug_var(d_time); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 7d476a4e..a5d490c1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -92,20 +92,20 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } -void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_local_t & mask) +void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) - if(mask(i, vertices.size())) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; + if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } xyz.set_size(3, m); for(index_t j = 0, i = 0; i < vertices.size(); i++) { - if(!mask || mask(i, vertices.size())) + if(!mask || mask(i)) { const vertex & v = mesh->gt(vertices[i]); xyz(0, j) = v.x; From c99b81dcd37b653f36a96d948ae369c518c02dd9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 14 Nov 2019 17:24:06 +0100 Subject: [PATCH 0035/1018] inpainting method setting reconstruction details --- src/mdict/inpainting.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 62ca397f..aaae59f9 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -14,7 +14,7 @@ distance_t inpainting::execute() { M = mesh->n_vertices()/36; gproshan_log_var(M); - + gproshan_debug_var(M); TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); @@ -136,6 +136,9 @@ distance_t inpainting::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); + for(index_t s = 0; s < M; s++) + patches[s].reset_xyz(mesh, patches_map, s); + TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); From dd3c842245d3f858715fa95450a806ff722ab2c9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 15 Nov 2019 15:58:59 +0100 Subject: [PATCH 0036/1018] inpainting reconstruction --- include/mdict/patch.h | 2 +- src/mdict/d_mesh.cpp | 5 +++-- src/mdict/inpainting.cpp | 29 +++++++++++++++++++++++------ src/mdict/patch.cpp | 9 ++++++--- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 1cc6c3d3..8bc34b05 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -70,7 +70,7 @@ class patch void reset_xyz_disjoint( che * mesh, distance_t * dist, std::vector & vpatches, - const index_t & p, + const index_t & p, bool reverse, const fmask_t & mask = nullptr ); const a_vec normal(); diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 08dbbb0b..c656009b 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -254,7 +254,6 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve if(rp.phi.n_rows) { a_vec x = rp.phi * A * alpha.col(p); - rp.xyz.row(2) = x.t(); rp.itransform(); } @@ -312,6 +311,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve image_out.save("../tmp/barbara_output.jpg"); (image,image_out).display();*/ + //distance_t error = 0; return error; } @@ -444,7 +444,8 @@ a_vec simple_means_vertex( const index_t & v, vector & patches, vector 1) { gproshan_debug_var(v); } + return n_a_vec/ patches_map[v].size(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index aaae59f9..55f9d73d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -12,14 +12,16 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size distance_t inpainting::execute() { + // M: the number of seeds M = mesh->n_vertices()/36; gproshan_log_var(M); gproshan_debug_var(M); + //FPS sampling with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); - + // creating disjoint clusters with geodesics aka voronoi #ifdef GPROSHAN_CUDA geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); #else @@ -28,6 +30,7 @@ distance_t inpainting::execute() TOC(d_time) gproshan_log_var(d_time); + //mapping the vertices to disjoint patches 1 vertex belongs only to one patch // creating new patches std::vector vertices[M]; @@ -41,6 +44,7 @@ distance_t inpainting::execute() bool * mask = new bool[mesh->n_vertices()]; size_t * percentages_size = new size_t[M]; + // vertices contains the mapping. //#pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { @@ -49,9 +53,9 @@ distance_t inpainting::execute() if(sample(ptp.clusters[i]) != i) vertices[ ptp.clusters[i] ].push_back(i) ; } - + //Randomly remove a percentage of points for each patch // create initial desired percentage sizes - size_t percent = 35; + size_t percent = 50; #pragma omp for for(index_t s = 0; s < M; s++) @@ -75,7 +79,7 @@ distance_t inpainting::execute() if(percentages_size[ptp.clusters[rn] ] == 0) k++; } - + //Initializing patches gproshan_log(our mask is ready); gproshan_log(initializing patches); @@ -117,7 +121,7 @@ distance_t inpainting::execute() for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, [&mask](const index_t & i) -> bool { return mask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 0 ,[&mask](const index_t & i) -> bool { return mask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -136,8 +140,21 @@ distance_t inpainting::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); + + +for(index_t s = 0; s < M; s++) +// patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); + patches[s].reset_xyz(mesh, patches_map, s, 0); + + #pragma omp parallel for for(index_t s = 0; s < M; s++) - patches[s].reset_xyz(mesh, patches_map, s); + { + patch & p = patches[s]; + + p.transform(); + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + } TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a5d490c1..b0d26d1e 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -86,13 +86,16 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & xyz(0, j) = v.x; xyz(1, j) = v.y; xyz(2, j) = v.z; - + //p idx patche where belongs to + //j: local index + //i: global index + //if(vpatches[vertices[i]].size() == 0) vpatches[vertices[i]].push_back({p, j++}); } } } -void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, bool reverse, const fmask_t & mask) { size_t m = vertices.size(); if(mask) @@ -101,7 +104,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector for(index_t i = 0; i < vertices.size(); i++) if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } - + if(reverse) m = vertices.size(); xyz.set_size(3, m); for(index_t j = 0, i = 0; i < vertices.size(); i++) { From fc0eb30a4fdfce6f64afede6400ca51166c17f1a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 15 Nov 2019 17:16:33 +0100 Subject: [PATCH 0037/1018] adding reset mapping vertex to patch --- src/mdict/patch.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index b0d26d1e..30b5dcf4 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -70,6 +70,11 @@ void patch::itransform() void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); + for(index_t j = 0, i = 0; i < vertices.size(); i++) + { + vpatches[j].clear(); + } + if(mask) { m = 0; @@ -98,6 +103,10 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, bool reverse, const fmask_t & mask) { size_t m = vertices.size(); + for(index_t j = 0, i = 0; i < vertices.size(); i++) + { + vpatches[j].clear(); + } if(mask) { m = 0; From 70a6d1f26f090767fc4ae83d391df838438f582e Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 18 Nov 2019 11:52:10 +0100 Subject: [PATCH 0038/1018] reset_disjoint to reconstruct --- src/mdict/inpainting.cpp | 4 ++-- src/mdict/patch.cpp | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 55f9d73d..bd598453 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -143,8 +143,8 @@ distance_t inpainting::execute() for(index_t s = 0; s < M; s++) -// patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); - patches[s].reset_xyz(mesh, patches_map, s, 0); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); +// patches[s].reset_xyz(mesh, patches_map, s, 0); #pragma omp parallel for for(index_t s = 0; s < M; s++) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 30b5dcf4..8a1ec021 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -70,10 +70,10 @@ void patch::itransform() void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); - for(index_t j = 0, i = 0; i < vertices.size(); i++) + /*for(index_t j = 0, i = 0; i < vertices.size(); i++) { vpatches[j].clear(); - } + }*/ if(mask) { @@ -103,19 +103,20 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, bool reverse, const fmask_t & mask) { size_t m = vertices.size(); - for(index_t j = 0, i = 0; i < vertices.size(); i++) + /*for(index_t j = 0, i = 0; i < vertices.size(); i++) { vpatches[j].clear(); - } + }*/ if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } - if(reverse) m = vertices.size(); + index_t j = 0; + if(reverse) { m = vertices.size(); j = vertices.size(); } xyz.set_size(3, m); - for(index_t j = 0, i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); i++) { if(!mask || mask(i)) { From 19cec8af860b301ecedb40abdcfd69969a9a98b8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Nov 2019 12:11:32 +0100 Subject: [PATCH 0039/1018] add sp_ksvd for mesh dictionary learning --- include/mdict/mdict.h | 11 ++++- src/mdict/mdict.cpp | 100 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index f78ef801..5ff943b3 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -43,13 +43,22 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); // MESH DENSE -a_vec OMP(const a_mat & A, const patch & p, const size_t & L); +a_vec OMP(const patch & p, const a_mat & A, const size_t & L); a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L); void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); +// MESH SPARSE + +void OMP(vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L); + +a_sp_mat OMP_all(vector & locval, const vector & patches, const a_mat & A, const size_t & L); + +void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); + + } // namespace gproshan::mdict #endif // MDICT_H diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index ace51686..923ca03e 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -74,8 +74,8 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec s; vector locval; - vector rows(m + 1); - index_t r; + vector rows; + rows.reserve(m + 1); while(k--) { @@ -83,12 +83,12 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) sort(locval.begin(), locval.end()); - rows[r = 0] = 0; + rows.push_back(0); for(index_t k = 1; k < locval.size(); k++) if(locval[k].i != locval[k - 1].i) - rows[++r] = k; + rows.push_back(k); - rows[++r] = locval.size(); + rows.push_back(locval.size()); R = X - D * alpha; @@ -189,7 +189,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) // MESH DENSE -a_vec OMP(const a_mat & A, const patch & p, const size_t & L) +a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { return OMP(p.xyz.row(2).t(), p.phi * A, L); } @@ -200,7 +200,7 @@ a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) #pragma omp parallel for for(index_t i = 0; i < patches.size(); i++) - alpha.col(i) = OMP(A, patches[i], L); + alpha.col(i) = OMP(patches[i], A, L); return alpha; } @@ -251,5 +251,91 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) } +// MESH SPARSE + +void OMP(vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L) +{ + OMP(alpha, p.xyz.col(2).t(), i, p.phi * A, L); +} + +a_sp_mat OMP_all(vector & locval, const vector & patches, const a_mat & A, const size_t & L) +{ + locval.clear(); + + #pragma omp parallel for + for(index_t i = 0; i < patches.size(); i++) + OMP(locval, patches[i], i, A, L); + + arma::umat DI(2, locval.size()); + a_vec DV(locval.size()); + + #pragma omp parallel for + for(index_t k = 0; k < locval.size(); k++) + { + DI(0, k) = locval[k].i; // row + DI(1, k) = locval[k].j; // column + DV(k) = locval[k].val; + } + + return a_sp_mat(DI, DV, A.n_cols, patches.size()); +} + +void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) +{ + size_t K = A.n_rows; + + a_mat new_A = A; + a_mat D, sum, sum_error; + a_vec a, e; + + real_t aj; + + vector locval; + vector rows; + rows.reserve(A.n_cols + 1); + + while(k--) + { + a_sp_mat alpha = OMP_all(locval, patches, A, L); + + sort(locval.begin(), locval.end()); + + rows.push_back(0); + for(index_t k = 1; k < locval.size(); k++) + if(locval[k].i != locval[k - 1].i) + rows.push_back(k); + + rows.push_back(locval.size()); + + #pragma omp parallel for private(a, aj, D, e, sum, sum_error) + for(index_t j = 0; j < A.n_cols; j++) + { + sum.zeros(K, K); + sum_error.zeros(K); + + for(index_t r = rows[j]; r < rows[j + 1]; r++) + { + const index_t & i = locval[r].j; + + a = alpha.col(i); + a(j) = 0; + + D = patches[i].phi * A; + e = patches[i].xyz.row(2).t() - D * a; + aj = as_scalar(e.t() * D.col(j) / (D.col(j).t() * D.col(j))); + + sum += aj * aj * patches[i].phi.t() * patches[i].phi; + sum_error += aj * patches[i].phi.t() * e; + } + + if(rows[j + 1] - rows[j]) + new_A.col(j) = solve(sum, sum_error); + } + + A = new_A; + } +} + + } // namespace gproshan::mdict From e1bb4435cff4b8360bbaada91f05decedf46367a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 18 Nov 2019 12:26:08 +0100 Subject: [PATCH 0040/1018] ready inpainting --- src/mdict/d_mesh.cpp | 2 +- src/mdict/inpainting.cpp | 12 ++++++++---- src/mdict/patch.cpp | 10 ++-------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index c656009b..7841f0e2 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -444,7 +444,7 @@ a_vec simple_means_vertex( const index_t & v, vector & patches, vector 1) { gproshan_debug_var(v); } + if (patches_map[v].size() > 1) { gproshan_debug_var(patches_map[v].size()); } return n_a_vec/ patches_map[v].size(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bd598453..96d03e28 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -140,11 +140,15 @@ distance_t inpainting::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); + //patches_map.resize(n_vertices); + for(index_t i = 0; i < n_vertices; i++) + { + patches_map[i].clear(); + } - -for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); -// patches[s].reset_xyz(mesh, patches_map, s, 0); + for(index_t s = 0; s < M; s++) +// patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); + patches[s].reset_xyz(mesh, patches_map, s, 0); #pragma omp parallel for for(index_t s = 0; s < M; s++) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8a1ec021..32f8cd10 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -70,10 +70,7 @@ void patch::itransform() void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); - /*for(index_t j = 0, i = 0; i < vertices.size(); i++) - { - vpatches[j].clear(); - }*/ + if(mask) { @@ -103,10 +100,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, bool reverse, const fmask_t & mask) { size_t m = vertices.size(); - /*for(index_t j = 0, i = 0; i < vertices.size(); i++) - { - vpatches[j].clear(); - }*/ + if(mask) { m = 0; From 39bab1137b824f852b6a421cb1a0e712a6d2c67e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Nov 2019 13:46:11 +0100 Subject: [PATCH 0041/1018] sparse ksvd mesh --- src/mdict/dictionary.cpp | 7 ++++--- src/mdict/mdict.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 6e859e2a..169bb63b 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -46,7 +46,7 @@ void dictionary::learning() { A.eye(phi_basis->dim, m); // A.random(phi_basis->dim, m); - KSVD(A, patches, L, K); + sp_KSVD(A, patches, L, K); gproshan_debug(Ok); @@ -66,8 +66,9 @@ void dictionary::learning() void dictionary::sparse_coding() { gproshan_debug(MDICT); - - alpha = OMP_all(patches, A, L); + + vector locval; + alpha = OMP_all(locval, patches, A, L); } void dictionary::init_sampling() diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 923ca03e..2dd3a32e 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -255,7 +255,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) void OMP(vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L) { - OMP(alpha, p.xyz.col(2).t(), i, p.phi * A, L); + OMP(alpha, p.xyz.row(2).t(), i, p.phi * A, L); } a_sp_mat OMP_all(vector & locval, const vector & patches, const a_mat & A, const size_t & L) From 030aeec171ca00991b19086346531610fc504bec Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 18 Nov 2019 18:48:12 +0100 Subject: [PATCH 0042/1018] improvin visualization --- include/mdict/inpainting.h | 2 + src/mdict/inpainting.cpp | 108 +++++++++++++++++++++++++------------ 2 files changed, 75 insertions(+), 35 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 3ad92cc1..795e9f1d 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -22,7 +22,9 @@ class inpainting : public dictionary virtual ~inpainting() = default; distance_t execute(); + void create_save_mask(); distance_t execute_tmp(); + }; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 96d03e28..4a946d25 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,4 +1,5 @@ #include "inpainting.h" +#include // geometry processing and shape analysis framework @@ -12,15 +13,25 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size distance_t inpainting::execute() { + //size avg of number of vertices per patch + size_t avg_p = 36; // M: the number of seeds - M = mesh->n_vertices()/36; + M = mesh->n_vertices()/avg_p; + size_t percent = 50; gproshan_log_var(M); gproshan_debug_var(M); - //FPS sampling with desired number of sources + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); + a_vec vdist; + + bool * mask = new bool[mesh->n_vertices()]; + + //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); + + // creating disjoint clusters with geodesics aka voronoi #ifdef GPROSHAN_CUDA geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); @@ -41,47 +52,73 @@ distance_t inpainting::execute() vertices[s].push_back(sample(s)); } - bool * mask = new bool[mesh->n_vertices()]; - size_t * percentages_size = new size_t[M]; - - // vertices contains the mapping. - //#pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - mask[i] = 0; - ptp.clusters[i]--; - if(sample(ptp.clusters[i]) != i) - vertices[ ptp.clusters[i] ].push_back(i) ; - } - //Randomly remove a percentage of points for each patch - // create initial desired percentage sizes - size_t percent = 50; + a_vec V(mesh->n_vertices()); + + if(!V.load(f_mask)) + { + V.zeros(); + //bool * mask = new bool[mesh->n_vertices()]; + size_t * percentages_size = new size_t[M]; + + // vertices contains the mapping. + //#pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + mask[i] = 0; + + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ ptp.clusters[i] ].push_back(i) ; + } + //Randomly remove a percentage of points for each patch + // create initial desired percentage sizes - #pragma omp for - for(index_t s = 0; s < M; s++) - { - percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; - } - std::default_random_engine generator; - std::uniform_int_distribution distribution(0,M); + #pragma omp for + for(index_t s = 0; s < M; s++) + { + percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; + } + + std::default_random_engine generator; + std::uniform_int_distribution distribution(0,M); - int k = 0; - size_t rn=0; - while( k < M ) + int k = 0; + size_t rn=0; + + //gproshan_debug(); + while( k < M ) + { + rn = distribution(generator); + if(rn < V.n_elem) {gproshan_debug_var(rn); } + if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) + { + + //gproshan_debug(); + mask[rn] = 1; + //V(rn) = 1; + + //gproshan_debug(); + percentages_size[ ptp.clusters[rn] ]--; + } + if(percentages_size[ptp.clusters[rn] ] == 0) + k++; + } + + //gproshan_debug(); + V.save(f_mask); + gproshan_log(our mask is ready); + } + else { - rn = distribution(generator); - if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) + gproshan_debug(here not reading); + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) { - mask[rn] = 1; - percentages_size[ ptp.clusters[rn] ]--; + mask[i] = V(i); } - if(percentages_size[ptp.clusters[rn] ] == 0) - k++; } //Initializing patches - gproshan_log(our mask is ready); - gproshan_log(initializing patches); patches.resize(M); @@ -135,6 +172,7 @@ distance_t inpainting::execute() } gproshan_log(our patches are ready); + // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) From 388a4cf82833a7d8a425c1bd375cc018a4eea8ec Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 19 Nov 2019 15:55:48 +0100 Subject: [PATCH 0043/1018] mask and inpainting ready !! --- include/app_viewer.h | 1 + include/mdict/inpainting.h | 9 ++- src/app_viewer.cpp | 29 ++++++++ src/mdict/inpainting.cpp | 147 +++++++++++++++++-------------------- 4 files changed, 105 insertions(+), 81 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index cebdb933..d322dd91 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -52,6 +52,7 @@ void viewer_process_mdict_patch(); void viewer_process_denoising(); void viewer_process_super_resolution(); void viewer_process_inpaiting(); +void viewer_process_mask(); void viewer_process_synthesis(); void viewer_process_iterative_inpaiting(); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 795e9f1d..74bcf2c0 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -18,12 +18,17 @@ namespace gproshan::mdict { class inpainting : public dictionary { public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot = true); + inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, const bool & _plot = false); virtual ~inpainting() = default; distance_t execute(); - void create_save_mask(); + void load_mask(std::vector vertices[], geodesics & ptp ); + void init_patches_disjoint(); distance_t execute_tmp(); + private: + size_t avg_p; + size_t percent; + bool * mask; }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index bf3c0009..f4f5c085 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -68,6 +68,8 @@ int viewer_main(int nargs, const char ** args) viewer::add_process('D', "Denoising", viewer_process_denoising); viewer::add_process('R', "Super Resolution", viewer_process_super_resolution); viewer::add_process('I', "Inpainting", viewer_process_inpaiting); + viewer::add_process('z', "Load mask", viewer_process_mask); + viewer::add_process('s', "Synthesis", viewer_process_synthesis); viewer::add_process('A', "IT Inpainting", viewer_process_iterative_inpaiting); @@ -516,6 +518,33 @@ void viewer_process_inpaiting() viewer::mesh().update_normals(); } +void viewer_process_mask() +{ + gproshan_log(APP_VIEWER); + + size_t avg_p; + size_t percentage; + + size_t n=12; // dct + size_t m = 144, M = 0; + distance_t f = 3; + bool learn = 0; + + + gproshan_input(avg_p percentage ); + cin >> avg_p >> percentage; + + basis * phi = new basis_dct(n); + inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); + dict.init_patches_disjoint(); + + delete phi; + viewer::mesh().update_colors(&dict[0]); + + viewer::mesh().update_normals(); +} + + void viewer_process_synthesis() { diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 4a946d25..8ec96a36 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,123 +1,107 @@ #include "inpainting.h" #include - +#include // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p, size_t _perc, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { + avg_p = _avg_p; //size avg of number of vertices per patch + percent = _perc; // mask percentage + M = mesh->n_vertices()/avg_p; + mask = new bool[mesh->n_vertices()]; } -distance_t inpainting::execute() +void inpainting::load_mask(std::vector vertices[], geodesics & ptp ) { - //size avg of number of vertices per patch - size_t avg_p = 36; - // M: the number of seeds - M = mesh->n_vertices()/avg_p; - size_t percent = 50; - gproshan_log_var(M); - gproshan_debug_var(M); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); - a_vec vdist; - - bool * mask = new bool[mesh->n_vertices()]; - - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - - - // creating disjoint clusters with geodesics aka voronoi -#ifdef GPROSHAN_CUDA - geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); -#else - geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); -#endif - TOC(d_time) - gproshan_log_var(d_time); - - //mapping the vertices to disjoint patches 1 vertex belongs only to one patch - // creating new patches - std::vector vertices[M]; + arma::uvec V; - //saving first vertex aka seed vertices - #pragma omp for - for(index_t s = 0; s < M; s++) + if(V.load(f_mask)) { - vertices[s].push_back(sample(s)); - } - - a_vec V(mesh->n_vertices()); - - if(!V.load(f_mask)) - { - V.zeros(); - //bool * mask = new bool[mesh->n_vertices()]; - size_t * percentages_size = new size_t[M]; - - // vertices contains the mapping. - //#pragma omp parallel for + #pragma omp for for(index_t i = 0; i < mesh->n_vertices(); i++) { - mask[i] = 0; - - ptp.clusters[i]--; - if(sample(ptp.clusters[i]) != i) - vertices[ ptp.clusters[i] ].push_back(i) ; + mask[i] = V(i); } - //Randomly remove a percentage of points for each patch - // create initial desired percentage sizes - + } + else + { + V.zeros(mesh->n_vertices()); + size_t * percentages_size = new size_t[M]; + + // create initial desired percentage sizes #pragma omp for for(index_t s = 0; s < M; s++) { percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; } - + //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0,M); int k = 0; size_t rn=0; - //gproshan_debug(); while( k < M ) { rn = distribution(generator); - if(rn < V.n_elem) {gproshan_debug_var(rn); } if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) { - //gproshan_debug(); mask[rn] = 1; - //V(rn) = 1; + V(rn) = 1; - //gproshan_debug(); percentages_size[ ptp.clusters[rn] ]--; } if(percentages_size[ptp.clusters[rn] ] == 0) k++; } - - //gproshan_debug(); V.save(f_mask); - gproshan_log(our mask is ready); } - else + +} + +void inpainting::init_patches_disjoint() +{ + gproshan_log_var(M); + + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + // creating disjoint clusters with geodesics aka voronoi +#ifdef GPROSHAN_CUDA + geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); +#else + geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); +#endif + TOC(d_time) + gproshan_log_var(d_time); + + std::vector vertices[M]; + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) { - gproshan_debug(here not reading); - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - mask[i] = V(i); - } + vertices[s].push_back(sample(s)); } + + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ ptp.clusters[i] ].push_back(i) ; + } + + load_mask(vertices, ptp); + + //Initializing patches gproshan_log(initializing patches); @@ -156,9 +140,9 @@ distance_t inpainting::execute() #endif } - + bool * pmask = mask; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 0 ,[&mask](const index_t & i) -> bool { return mask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 0 ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -171,8 +155,14 @@ distance_t inpainting::execute() } - gproshan_log(our patches are ready); - + gproshan_log(our patches are ready); +} + +distance_t inpainting::execute() +{ + + TIC(d_time) init_patches_disjoint(); TOC(d_time) + gproshan_debug_var(d_time); // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) @@ -185,7 +175,6 @@ distance_t inpainting::execute() } for(index_t s = 0; s < M; s++) -// patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 1 ,[&mask](const index_t & i) -> bool { return !mask[i]; } ); patches[s].reset_xyz(mesh, patches_map, s, 0); #pragma omp parallel for @@ -200,9 +189,9 @@ distance_t inpainting::execute() TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); - } + distance_t inpainting::execute_tmp() { // fill holes From 814e1dd6ca1181c338e88dfb729a17957cd41f42 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 20 Nov 2019 12:32:29 +0100 Subject: [PATCH 0044/1018] inpainting with dct ready for test --- src/app_viewer.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index f4f5c085..7a7f5e30 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -504,12 +504,14 @@ void viewer_process_inpaiting() size_t m, M; distance_t f; bool learn; + size_t avg_p; + size_t percentage; - gproshan_input(n m M f learn); - cin >> n >> m >> M >> f >> learn; + gproshan_input(n m M f learn avg_p percentage); + cin >> n >> m >> M >> f >> learn >> avg_p >> percentage; basis * phi = new basis_dct(n); - inpainting dict(viewer::mesh(), phi, m, M, f, learn); + inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); dict.execute(); delete phi; From 535a462bc0e0bc315bbd95efccd846e1358ded58 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 20 Nov 2019 13:23:10 +0100 Subject: [PATCH 0045/1018] Removing unnecesary addons --- include/mdict/patch.h | 2 +- src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 8bc34b05..1cc6c3d3 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -70,7 +70,7 @@ class patch void reset_xyz_disjoint( che * mesh, distance_t * dist, std::vector & vpatches, - const index_t & p, bool reverse, + const index_t & p, const fmask_t & mask = nullptr ); const a_vec normal(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 8ec96a36..e73ff018 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -142,7 +142,7 @@ void inpainting::init_patches_disjoint() bool * pmask = mask; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s, 0 ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 32f8cd10..3862cc74 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -97,7 +97,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } -void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, bool reverse, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); @@ -107,10 +107,9 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector for(index_t i = 0; i < vertices.size(); i++) if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } - index_t j = 0; - if(reverse) { m = vertices.size(); j = vertices.size(); } + xyz.set_size(3, m); - for(index_t i = 0; i < vertices.size(); i++) + for(index_t j = 0, i = 0; i < vertices.size(); i++) { if(!mask || mask(i)) { From 3ec622ecc26fe4024378dcb671439a5848e36868 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Nov 2019 14:47:53 +0100 Subject: [PATCH 0046/1018] fix learning --- src/mdict/dictionary.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 169bb63b..f1c11181 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -37,21 +37,22 @@ dictionary::~dictionary() void dictionary::learning() { - gproshan_debug(MDICT); + gproshan_log(MDICT); string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim) + '_' + to_string(m) + '_' + to_string(f) + '_' + to_string(L) + ".dict"); - gproshan_debug_var(f_dict); - if(learn && !A.load(f_dict)) + if(learn) { - A.eye(phi_basis->dim, m); - // A.random(phi_basis->dim, m); - sp_KSVD(A, patches, L, K); + gproshan_log_var(f_dict); - gproshan_debug(Ok); - - A.save(f_dict); + if(!A.load(f_dict)) + { + A.randu(phi_basis->dim, m); + KSVD(A, patches, L, K); + A.save(f_dict); + } } + else A.eye(phi_basis->dim, m); assert(A.n_rows == phi_basis->dim); assert(A.n_cols == m); @@ -65,15 +66,15 @@ void dictionary::learning() void dictionary::sparse_coding() { - gproshan_debug(MDICT); + gproshan_log(MDICT); vector locval; - alpha = OMP_all(locval, patches, A, L); + alpha = OMP_all(patches, A, L); } void dictionary::init_sampling() { - gproshan_debug(MDICT); + gproshan_log(MDICT); n_vertices = mesh->n_vertices(); @@ -92,13 +93,14 @@ void dictionary::init_sampling() s_radio = phi_basis->radio; phi_basis->radio *= f; + gproshan_debug_var(s_radio); gproshan_debug_var(phi_basis->radio); } void dictionary::init_patches(const bool & reset, const fmask_t & mask) { - gproshan_debug(MDICT); + gproshan_log(MDICT); if(reset) { @@ -186,7 +188,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) distance_t dictionary::mesh_reconstruction() { - gproshan_debug(MDICT); + gproshan_log(MDICT); assert(n_vertices == mesh->n_vertices()); return mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha, dist); @@ -247,5 +249,6 @@ const distance_t & dictionary::operator[](const index_t & i) const return dist[i]; } + } // namespace gproshan::mdict From 19776c9ee6c84e88a7a276da3b42e57317cb2cf9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 6 Dec 2019 11:13:58 +0100 Subject: [PATCH 0047/1018] inpainting masks ready --- include/mdict/d_mesh.h | 2 +- include/mdict/dictionary.h | 2 +- src/mdict/d_mesh.cpp | 6 ++---- src/mdict/dictionary.cpp | 4 ++-- src/mdict/inpainting.cpp | 9 ++++++++- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index 18a348dc..a408b4fe 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,7 +111,7 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const index_t & v_i = 0); +distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index b29c90f6..b936b9d5 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -68,7 +68,7 @@ class dictionary const fmask_t & mask = nullptr ); - distance_t mesh_reconstruction(); + distance_t mesh_reconstruction(const fmask_t & mask = nullptr); void update_alphas(a_mat & alpha, size_t threshold); index_t sample(const index_t & s); diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 7841f0e2..f9259114 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -241,7 +241,7 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const index_t & v_i) +distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist,const fmask_t & mask, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); @@ -264,7 +264,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve #pragma omp parallel for for(index_t v = v_i; v < mesh->n_vertices(); v++) { - if(patches_map[v].size()) + if(patches_map[v].size() && (!mask || mask(v)) ) V.col(v) = simple_means_vertex(v, patches, patches_map); //V.col(v) = non_local_means_vertex(alpha, v, patches, patches_map, h); else @@ -443,8 +443,6 @@ a_vec simple_means_vertex( const index_t & v, vector & patches, vector 1) { gproshan_debug_var(patches_map[v].size()); } return n_a_vec/ patches_map[v].size(); } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index f1c11181..6c10f5e3 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 8; +size_t dictionary::L = 10; size_t dictionary::K = 10; size_t dictionary::T = 5; @@ -186,7 +186,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) */ } -distance_t dictionary::mesh_reconstruction() +distance_t dictionary::mesh_reconstruction(const fmask_t & mask) { gproshan_log(MDICT); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e73ff018..026f1224 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -164,6 +164,11 @@ distance_t inpainting::execute() TIC(d_time) init_patches_disjoint(); TOC(d_time) gproshan_debug_var(d_time); + //L = 15; + + //TIC(d_time) learning(); TOC(d_time) + //gproshan_debug_var(d_time); + // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); @@ -187,7 +192,9 @@ distance_t inpainting::execute() phi_basis->discrete(p.phi, p.xyz); } - TIC(d_time) mesh_reconstruction(); TOC(d_time) + bool *pmask; + + TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); } From 11f58760dd02e1bb6f1919f383a7b2b4cc4f50da Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 6 Dec 2019 12:06:07 +0100 Subject: [PATCH 0048/1018] adding dl to inpainting --- src/mdict/dictionary.cpp | 2 +- src/mdict/inpainting.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 6c10f5e3..ba461496 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 10; +size_t dictionary::L = 8; size_t dictionary::K = 10; size_t dictionary::T = 5; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 026f1224..ae09f578 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -164,10 +164,10 @@ distance_t inpainting::execute() TIC(d_time) init_patches_disjoint(); TOC(d_time) gproshan_debug_var(d_time); - //L = 15; +// L = 15; - //TIC(d_time) learning(); TOC(d_time) - //gproshan_debug_var(d_time); + TIC(d_time) learning(); TOC(d_time) + gproshan_debug_var(d_time); // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) From 9ce4b5d4108eb04177b8a29785c63814041408dd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 6 Dec 2019 15:22:38 +0100 Subject: [PATCH 0049/1018] glfw --- CMakeLists.txt | 15 +-- include/viewer/include_opengl.h | 2 +- include/viewer/viewer.h | 8 +- src/viewer/camera.cpp | 4 +- src/viewer/che_viewer.cpp | 2 + src/viewer/viewer.cpp | 163 ++++++++++++++------------------ 6 files changed, 89 insertions(+), 105 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e3a505b2..33e39478 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ endif(CUDA_FOUND) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) -find_package(GLEW REQUIRED) -find_package(GLUT REQUIRED) +find_package(GLEW 2.1 REQUIRED) +find_package(glfw3 3.3 REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) @@ -39,7 +39,7 @@ endif(CUDA_FOUND) include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${GLEW_INCLUDE_DIRS}) -include_directories(${GLUT_INCLUDE_DIR}) +include_directories(${GLFW3_INCLUDE_DIRS}) include_directories(${X11_INCLUDE_DIR}) include_directories(${AMADILLO_INCLUDE_DIR}) include_directories(${EIGEN3_INCLUDE_DIR}) @@ -55,12 +55,13 @@ FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) add_library(gproshan_cpp STATIC ${cpp_sources}) target_link_libraries(gproshan_cpp ${OpenMP_CXX_LIBRARIES}) -target_link_libraries(gproshan_cpp ${OPENGL_LIBRARIES}) -target_link_libraries(gproshan_cpp ${GLEW_LIBRARIES}) -target_link_libraries(gproshan_cpp ${GLUT_LIBRARIES}) +target_link_libraries(gproshan_cpp OpenGL::GL) +target_link_libraries(gproshan_cpp OpenGL::GLU) +target_link_libraries(gproshan_cpp GLEW::GLEW) +target_link_libraries(gproshan_cpp glfw) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp ${CGAL_LIBRARIES}) +target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) if(CUDA_FOUND) diff --git a/include/viewer/include_opengl.h b/include/viewer/include_opengl.h index 89224284..5c840f0c 100644 --- a/include/viewer/include_opengl.h +++ b/include/viewer/include_opengl.h @@ -2,7 +2,7 @@ #define INCLUDE_OPENGL_H #include -#include +#include #endif // INCLUDE_OPENGL_H diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 06aaefa5..4513deac 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -93,9 +93,8 @@ class viewer // GLUT callbacks static void display(); static void idle(); - static void keyboard(unsigned char c, int x, int y); - static void special(int i, int x, int y); - static void mouse(int button, int state, int x, int y); + static void keyboard(GLFWwindow * w, int key, int scancode, int action, int mods); + static void mouse(GLFWwindow* window, int button, int action, int mods); static void motion(int x, int y); // menu functions @@ -135,7 +134,8 @@ class viewer static void draw_vectors(); static void draw_isolated_vertices(); static void pick_vertex(int x, int y); - + + static GLFWwindow * window; static int window_size[2]; static double ww, wh; static int m_window_size[N_MESHES][2]; diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 45f77190..5028e301 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -75,12 +75,12 @@ void camera::setView() const void camera::mouse(int , int state, int x, int y) { - if(state == GLUT_DOWN) + if(state == GLFW_PRESS) { pClick = pDrag = pLast = clickToSphere(x, y); momentum = 1.; } - if(state == GLUT_UP) + if(state == GLFW_RELEASE) { double timeSinceDrag = (clock() - tLast) / (double) CLOCKS_PER_SEC; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index ad7fb8c9..4a7d0b18 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -249,6 +249,7 @@ void che_viewer::draw_gradient_field() void che_viewer::draw_mesh_info() { +/* if(!mesh) return; glMatrixMode(GL_PROJECTION); @@ -288,6 +289,7 @@ void che_viewer::draw_mesh_info() glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); +*/ } const size_t & che_viewer::n_vertices() const diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8533de9e..17a31ea8 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -32,6 +32,7 @@ vector viewer::sub_menus; char * viewer::share = nullptr; +GLFWwindow * viewer::window = nullptr; int viewer::window_size[2] = {1366, 768}; int viewer::m_window_size[N_MESHES][2] = { {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {2, 3}, @@ -76,7 +77,7 @@ void viewer::init(const vector & _meshes) init_glut(); add_mesh(_meshes); - glutSetWindowTitle(mesh()->filename().c_str()); + glfwSetWindowTitle(window, mesh()->filename().c_str()); init_menus(); debug_info(); @@ -85,7 +86,13 @@ void viewer::init(const vector & _meshes) set_gl(); init_glsl(); - glutMainLoop(); + while(!glfwWindowShouldClose(window)) + { + display(); + } + + glfwDestroyWindow(window); + glfwTerminate(); } void viewer::debug_info() @@ -100,38 +107,34 @@ void viewer::debug_info() fprintf(stderr, "GLSL Version %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); } +void error_callback(int error, const char* description) +{ + fprintf(stderr, "Error %d: %s\n", error, description); +} + void viewer::init_glut() { - int argc = 0; - vector< vector > argv(1); + glfwSetErrorCallback(error_callback); - // initialize window - glutInitWindowSize(window_size[0], window_size[1]); - glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); - glutInit(&argc, (char**)&argv); - glutCreateWindow("gproshan"); + glfwInit(); + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); - glewInit(); + window = glfwCreateWindow(window_size[0], window_size[1], "gproshan", NULL, NULL); + + glfwSetKeyCallback(window, keyboard); + glfwSetMouseButtonCallback(window, mouse); - // specify callbacks - glutDisplayFunc(display); - glutIdleFunc(idle); - glutKeyboardFunc(keyboard); - glutSpecialFunc(special); - glutMouseFunc(mouse); - glutMotionFunc(motion); + glfwMakeContextCurrent(window); - glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); + glewInit(); + + glfwSwapInterval(1); } void viewer::init_menus() { - // set current mesh menu - int mesh_menu = glutCreateMenu(viewer::menu_meshes); - glutSetMenu(mesh_menu); - for(index_t i = 0; i < n_meshes; i++) - glutAddMenuEntry(meshes[i]->filename().c_str(), i); - // init viewer menu sub_menus.push_back("viewer"); add_process('i', "Invert Orientation", invert_orientation); @@ -150,33 +153,6 @@ void viewer::init_menus() add_process('<', "Zoom In", menu_zoom_in); add_process('>', "Zoom Out", menu_zoom_out); add_process(27, "Exit", menu_exit); - - - // init - // process sub menus - int * sub_menu = new int[sub_menus.size()]; - - for(index_t sm = 0; sm < sub_menus.size(); sm++) - sub_menu[sm] = glutCreateMenu(viewer::menu_process); - - char ss[128]; - for(auto mp: viewer::processes) - { - sprintf(ss, "[%c] %s", mp.first, mp.second.name_function.c_str()); - glutSetMenu(sub_menu[mp.second.sub_menu]); - glutAddMenuEntry(ss, mp.first); - } - - int mainMenu = glutCreateMenu(viewer::menu); - glutSetMenu(mainMenu); - - glutAddSubMenu("Select Mesh", mesh_menu); - for(index_t sm = 0; sm < sub_menus.size(); sm++) - glutAddSubMenu(sub_menus[sm].c_str(), sub_menu[sm]); - - glutAttachMenu(GLUT_RIGHT_BUTTON); - - delete [] sub_menu; } void viewer::init_glsl() @@ -234,49 +210,50 @@ void viewer::add_mesh(const vector & _meshes) } } -void viewer::keyboard(unsigned char c, int , int ) +void viewer::keyboard(GLFWwindow * w, int key, int scancode, int action, int mods) { - if(c >= '0' && c <= '9') + if(action == GLFW_RELEASE) return; + + switch(key) { - bgc = (c - '0') / 9.; + case GLFW_KEY_UP: + cam.zoomIn(); + break; + case GLFW_KEY_DOWN: + cam.zoomOut(); + break; + case GLFW_KEY_ESCAPE: + glfwSetWindowShouldClose(w, GLFW_TRUE); + break; + } + + if(key >= '0' && key <= '9') + { + bgc = (key - '0') / 9.; glClearColor(bgc, bgc, bgc, 1.); } - menu_process(processes[c].function); + menu_process(processes[key].function); } void viewer::menu_meshes(int value) { current = value; select_vertices.clear(); - glutSetWindowTitle(mesh()->filename().c_str()); + glfwSetWindowTitle(window, mesh()->filename().c_str()); } -void viewer::special(int i, int , int ) +void viewer::mouse(GLFWwindow* window, int button, int action, int mods) { - switch(i) - { - case GLUT_KEY_UP: - cam.zoomIn(); - break; - case GLUT_KEY_DOWN: - cam.zoomOut(); - break; - case 27: - menu_exit(); - break; - default: - break; - } -} - -void viewer::mouse(int button, int state, int x, int y) -{ - if((glutGetModifiers() & GLUT_ACTIVE_SHIFT) and state == GLUT_UP) - pick_vertex(x, y); - else if(button == 6 || button == 4) cam.zoomIn(); - else if(button == 5 || button == 3) cam.zoomOut(); - else cam.mouse(button, state, x, y); + double xpos, ypos; + glfwGetCursorPos(window, &xpos, &ypos); + + if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) + pick_vertex(xpos, ypos); + + //else if(button == 6 || button == 4) cam.zoomIn(); + //else if(button == 5 || button == 3) cam.zoomOut(); + else cam.mouse(button, action, xpos, ypos); } void viewer::motion(int x, int y) @@ -287,7 +264,7 @@ void viewer::motion(int x, int y) void viewer::idle() { cam.idle(); - glutPostRedisplay(); + //glutPostRedisplay(); } void viewer::menu_process(function_t pro) @@ -326,7 +303,6 @@ void viewer::menu_save_mesh() void viewer::menu_exit() { - glutLeaveMainLoop(); } void viewer::menu_zoom_in() @@ -447,14 +423,18 @@ void viewer::display() set_mesh_materia(); - ww = (double) glutGet(GLUT_WINDOW_WIDTH) / m_window_size[n_meshes - 1][1]; - wh = (double) glutGet(GLUT_WINDOW_HEIGHT) / m_window_size[n_meshes - 1][0]; + int width, height; + glfwGetFramebufferSize(window, &width, &height); + ww = width; + wh = height; draw_scene(); //glPopAttrib(); shader_program.disable(); - glutSwapBuffers(); + + glfwSwapBuffers(window); + glfwPollEvents(); } void viewer::set_gl() @@ -581,7 +561,7 @@ void viewer::draw_isolated_vertices() { glPushMatrix(); glTranslated(v.x, v.y, v.z); - glutSolidSphere(h, 10, 10); + //glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -645,7 +625,7 @@ void viewer::draw_corr() glPushMatrix(); corr_v = meshes[corr_mesh[current].mesh_i]->corr_vertex(corr_mesh[current][v]); glTranslated(corr_v.x, corr_v.y, corr_v.z); - glutSolidSphere(h, 10, 10); +// glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -690,7 +670,7 @@ void viewer::draw_selected_vertices() glPushMatrix(); glTranslated(mesh()->gt(v).x, mesh()->gt(v).y, mesh()->gt(v).z); - glutSolidSphere(h, 10, 10); + // glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -723,8 +703,9 @@ void viewer::pick_vertex(int x, int y) { gproshan_debug(VIEWER); - int width = glutGet(GLUT_WINDOW_WIDTH); - int height = glutGet(GLUT_WINDOW_HEIGHT); + int width, height; + glfwGetFramebufferSize(window, &width, &height); + if(x < 0 || x >= width || y < 0 || y >= height) return; int bufSize = mesh()->n_vertices(); @@ -792,7 +773,7 @@ void draw_str(const char * str, int x, int y, float color[4], void * font) glColor4fv(color); glRasterPos2i(x, y); - while(*str) glutBitmapCharacter(font, *str++); + //while(*str) glutBitmapCharacter(font, *str++); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); From 97b47fe40508af742fe45f47ddbae3b2362aa6b0 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 6 Dec 2019 16:32:34 +0100 Subject: [PATCH 0050/1018] Adding results drawing patch --- include/mdict/basis.h | 1 + include/mdict/dictionary.h | 2 ++ src/app_viewer.cpp | 2 +- src/mdict/basis.cpp | 42 ++++++++++++++++++++++++++++++++++++++ src/mdict/dictionary.cpp | 5 +++++ src/mdict/inpainting.cpp | 2 ++ 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 9f594fe5..df107260 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -27,6 +27,7 @@ class basis virtual void discrete(a_mat & phi, const a_mat & xy) = 0; void plot_basis(); void plot_atoms(const a_mat & A); + void plot_patch(const a_mat & A, const a_mat & xyz ); size_t get_dim(); private: diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index b936b9d5..d633f45c 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -46,6 +46,7 @@ class dictionary public: const distance_t & operator[](const index_t & i) const; + void draw_patches(index_t i); protected: dictionary( che *const & _mesh, ///< pointer to input mesh. @@ -72,6 +73,7 @@ class dictionary void update_alphas(a_mat & alpha, size_t threshold); index_t sample(const index_t & s); + }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7a7f5e30..6a494a4b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -513,7 +513,6 @@ void viewer_process_inpaiting() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); dict.execute(); - delete phi; viewer::mesh().update_colors(&dict[0]); @@ -538,6 +537,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); + dict.init_patches_disjoint(); delete phi; diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index ec492996..431f6d12 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -72,5 +72,47 @@ size_t basis::get_dim() return dim; } +void basis::plot_patch(const a_mat & A, const a_mat & xyz ) +{ + string data = tmp_file_path("xyz.dat"); + a_mat tmp = xyz.t(); + tmp.save(data.c_str(),arma::arma_ascii); + + size_t K = A.n_rows; + size_t m = A.n_cols; + size_t s = sqrt(m); + s += !(s * s == K); + + string file = tmp_file_path("atoms.gpi"); + ofstream os(file); + + os << "set term qt size 1000,1000;" << endl; + os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << endl; + os << "set isosamples 25,25;" << endl; + os << "set parametric;" << endl; + os << "set vrange [-"<< 0 << ":" << radio <<"];" << endl; + os << "set urange [-pi:pi];" << endl; + os << "unset key;" << endl; + os << "set pm3d at b;" << endl; + os << "unset colorbox;" << endl; + os << "splot \"xyz.dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; + + for(index_t i = 0; i < m; i++) + { + os << " v * cos(u), v * sin(u), 0 "; + plot_atoms(os, A.col(i)); + os << ";" << endl; + } + os << "unset multiplot;" << endl; + os << "pause -1;" << endl; + + os.close(); + + file = "gnuplot -persist " + file + " &"; + + system(file.c_str()); + +} + } // namespace gproshan::mdict diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index ba461496..b7ad2ae1 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -249,6 +249,11 @@ const distance_t & dictionary::operator[](const index_t & i) const return dist[i]; } +void dictionary::draw_patches(index_t i) +{ + phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz); +} + } // namespace gproshan::mdict diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ae09f578..1c80d9ec 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -193,6 +193,8 @@ distance_t inpainting::execute() } bool *pmask; + + draw_patches(7); TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); From ed7728e69adc003d83e7bc1dea9d0b5802946b09 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 7 Dec 2019 12:17:42 +0100 Subject: [PATCH 0051/1018] macos, in progress --- CMakeLists.txt | 2 +- shaders/new_fragment.glsl | 2 +- shaders/new_vertex.glsl | 2 +- src/viewer/che_viewer.cpp | 16 ++++++---------- src/viewer/viewer.cpp | 15 ++++++++++----- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 33e39478..f8b9a3bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_link_libraries(gproshan_cpp GLEW::GLEW) target_link_libraries(gproshan_cpp glfw) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp CGAL::CGAL) +target_link_libraries(gproshan_cpp ${CGAL_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) if(CUDA_FOUND) diff --git a/shaders/new_fragment.glsl b/shaders/new_fragment.glsl index be591d16..b4b123ea 100644 --- a/shaders/new_fragment.glsl +++ b/shaders/new_fragment.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 330 core uniform vec3 eye; uniform vec3 light; diff --git a/shaders/new_vertex.glsl b/shaders/new_vertex.glsl index 3307f05e..f75cb92d 100644 --- a/shaders/new_vertex.glsl +++ b/shaders/new_vertex.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 330 core layout (location=0) in vec3 VertexPosition; layout (location=1) in vec3 VertexNormal; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 4a7d0b18..6380c9be 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -170,20 +170,16 @@ void che_viewer::update_colors(const color_t *const c) void che_viewer::draw() { + glBindVertexArray(vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); + if(mesh->n_faces()) - { - glBindVertexArray(vao); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } else - { - glBindVertexArray(vao); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); glDrawElements(GL_POINTS, mesh->n_vertices(), GL_UNSIGNED_INT, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); } void che_viewer::draw_normal_field() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 17a31ea8..16f2692f 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -117,9 +117,15 @@ void viewer::init_glut() glfwSetErrorCallback(error_callback); glfwInit(); - - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); + + #ifdef __APPLE__ + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + #endif window = glfwCreateWindow(window_size[0], window_size[1], "gproshan", NULL, NULL); @@ -127,10 +133,9 @@ void viewer::init_glut() glfwSetMouseButtonCallback(window, mouse); glfwMakeContextCurrent(window); + glfwSwapInterval(0); glewInit(); - - glfwSwapInterval(1); } void viewer::init_menus() From 6d286f3025dedc347696e2a19e85547e6f875c3f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 12:46:31 +0100 Subject: [PATCH 0052/1018] setting app_viewer class glfw user pointer --- gproshan.cpp | 3 +- include/app_viewer.h | 123 ++++---- include/viewer/viewer.h | 180 ++++++----- src/app_viewer.cpp | 683 ++++++++++++++++++++++------------------ src/che_fill_hole.cpp | 2 +- src/viewer/viewer.cpp | 212 ++++++------- 6 files changed, 641 insertions(+), 562 deletions(-) diff --git a/gproshan.cpp b/gproshan.cpp index e5dfb447..a0c142af 100644 --- a/gproshan.cpp +++ b/gproshan.cpp @@ -2,6 +2,7 @@ int main(int nargs, const char ** args) { - return viewer_main(nargs, args); + gproshan::app_viewer app; + return app.main(nargs, args); } diff --git a/include/app_viewer.h b/include/app_viewer.h index 629fff75..ac1f32ab 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -26,59 +26,76 @@ #include "key_components.h" -using namespace gproshan; - - -void viewer_process_fairing_taubin(); -void viewer_process_fairing_spectral(); - -void viewer_process_fastmarching(); -void viewer_process_geodesics_fm(); -void viewer_process_geodesics_ptp_cpu(); -void viewer_process_geodesics_heat_flow(); - -#ifdef GPROSHAN_CUDA -void viewer_process_geodesics_ptp_gpu(); -void viewer_process_geodesics_heat_flow_gpu(); -#endif // GPROSHAN_CUDA - -void viewer_process_farthest_point_sampling(); -void viewer_process_farthest_point_sampling_radio(); -void viewer_compute_toplesets(); -void viewer_process_voronoi(); - -void viewer_process_mdict_patch(); -void viewer_process_denoising(); -void viewer_process_super_resolution(); -void viewer_process_inpaiting(); -void viewer_process_iterative_inpaiting(); - -void viewer_process_functional_maps(); -void viewer_process_gps(); -void viewer_process_hks(); -void viewer_process_wks(); -void viewer_process_key_points(); -void viewer_process_key_components(); - -void viewer_process_poisson(const index_t & lk); -void viewer_process_poisson_laplacian_1(); -void viewer_process_poisson_laplacian_2(); -void viewer_process_poisson_laplacian_3(); - -void viewer_process_thresold(); -void viewer_process_noise(); -void viewer_process_black_noise(); -void viewer_process_multiplicate_vertices(); -void viewer_process_fill_holes(); -void viewer_process_delete_vertices(); -void viewer_process_fill_holes_test(); -void viewer_process_delete_non_manifold_vertices(); -void viewer_process_fill_holes_biharmonic_splines(); -void viewer_process_gaussian_curvature(); -void viewer_process_edge_collapse(); -void viewer_select_multiple(); - -int viewer_main(int nargs, const char ** args); +// geometry processing and shape analysis framework +namespace gproshan { + + +class app_viewer : public viewer +{ + private: + double time; + distance_t * dist; + size_t n_dist; + + public: + app_viewer(); + ~app_viewer(); + + int main(int nargs, const char ** args); + + private: + static void process_fairing_taubin(viewer * p_view); + static void process_fairing_spectral(viewer * p_view); + + static void process_fastmarching(viewer * p_view); + static void process_geodesics_fm(viewer * p_view); + static void process_geodesics_ptp_cpu(viewer * p_view); + static void process_geodesics_heat_flow(viewer * p_view); + + #ifdef GPROSHAN_CUDA + static void process_geodesics_ptp_gpu(viewer * p_view); + static void process_geodesics_heat_flow_gpu(viewer * p_view); + #endif // GPROSHAN_CUDA + + static void process_farthest_point_sampling(viewer * p_view); + static void process_farthest_point_sampling_radio(viewer * p_view); + static void compute_toplesets(viewer * p_view); + static void process_voronoi(viewer * p_view); + + static void process_mdict_patch(viewer * p_view); + static void process_denoising(viewer * p_view); + static void process_super_resolution(viewer * p_view); + static void process_inpaiting(viewer * p_view); + static void process_iterative_inpaiting(viewer * p_view); + + static void process_functional_maps(viewer * p_view); + static void process_gps(viewer * p_view); + static void process_hks(viewer * p_view); + static void process_wks(viewer * p_view); + static void process_key_points(viewer * p_view); + static void process_key_components(viewer * p_view); + + static void process_poisson(viewer * p_view, const index_t & k); + static void process_poisson_laplacian_1(viewer * p_view); + static void process_poisson_laplacian_2(viewer * p_view); + static void process_poisson_laplacian_3(viewer * p_view); + + static void process_thresold(viewer * p_view); + static void process_noise(viewer * p_view); + static void process_black_noise(viewer * p_view); + static void process_multiplicate_vertices(viewer * p_view); + static void process_fill_holes(viewer * p_view); + static void process_delete_vertices(viewer * p_view); + static void process_fill_holes_test(viewer * p_view); + static void process_delete_non_manifold_vertices(viewer * p_view); + static void process_fill_holes_biharmonic_splines(viewer * p_view); + static void process_gaussian_curvature(viewer * p_view); + static void process_edge_collapse(viewer * p_view); + static void select_multiple(viewer * p_view); +}; + + +} // namespace gproshan #endif //APP_VIEWER_H diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 4513deac..9f1eeece 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -17,7 +17,8 @@ namespace gproshan { -typedef void (*function_t) (void); +class viewer; + struct vcorr_t { @@ -50,112 +51,117 @@ struct vcorr_t } }; -struct process_t -{ - index_t sub_menu; - std::string name_function; - function_t function; -}; class viewer { - public: - static void init(const std::vector & _meshes); - - static che_viewer meshes[N_MESHES]; - static vcorr_t corr_mesh[N_MESHES]; - static size_t n_meshes; - static index_t current; // current mesh + protected: - static std::vector select_vertices; - static std::vector other_vertices; - static std::vector vectors; - static std::vector sub_menus; + typedef void (*function_t) (viewer *); + + struct process_t + { + index_t sub_menu; + std::string name_function; + function_t function; + }; + + static const int m_window_size[N_MESHES][2]; + + + GLFWwindow * window; + shader shader_program; + camera cam; + + che_viewer meshes[N_MESHES]; + vcorr_t corr_mesh[N_MESHES]; + size_t n_meshes; + index_t current; // current mesh + + char * share; + + bool render_wireframe; + bool render_gradient_field; + bool render_normal_field; + bool render_border; + bool render_corr; + bool render_lines; + bool is_flat; + float bgc; + + std::map processes; + + public: - static char * share; + std::vector select_vertices; + std::vector other_vertices; + std::vector vectors; + std::vector sub_menus; - static const int & window_width(); - static const int & window_height(); + public: - static che_viewer & mesh(); //get current che_viewer mesh - static color_t & vcolor(const index_t & i); - static void add_process(const char & key, const std::string & name, function_t function); - static void add_mesh(const std::vector & _meshes); + viewer(); + virtual ~viewer(); + + bool run(); + + che_viewer & mesh(); + void add_process(const char & key, const std::string & name, function_t function); + void add_mesh(const std::vector & _meshes); + + private: - protected: // init - static void debug_info(); - static void init_glut(); - static void init_menus(); - static void init_glsl(); - static void update_vbo(); + void debug_info(); + void init_glut(); + void init_menus(); + void init_glsl(); + void update_vbo(); + + void display(); // GLUT callbacks - static void display(); static void idle(); - static void keyboard(GLFWwindow * w, int key, int scancode, int action, int mods); - static void mouse(GLFWwindow* window, int button, int action, int mods); + static void keyboard(GLFWwindow * window, int key, int scancode, int action, int mods); + static void mouse(GLFWwindow * window, int button, int action, int mods); static void motion(int x, int y); // menu functions - static void menu(int value); - static void menu_process(int value); - static void menu_meshes(int value); - static void menu_process(function_t pro); - static void menu_reset_mesh(); - static void menu_save_mesh(); - static void menu_exit(); - static void menu_zoom_in(); - static void menu_zoom_out(); + void menu_meshes(int value); + void menu_process(int value); + void menu_process(function_t pro); + + static void menu_reset_mesh(viewer * view); + static void menu_save_mesh(viewer * view); + static void menu_zoom_in(viewer * view); + static void menu_zoom_out(viewer * view); // render options - static void invert_orientation(); - static void set_render_wireframe(); - static void set_render_gradient_field(); - static void set_render_normal_field(); - static void set_render_border(); - static void set_render_lines(); - static void set_render_corr(); - static void set_is_flat(); + static void invert_orientation(viewer * view); + static void set_render_wireframe(viewer * view); + static void set_render_gradient_field(viewer * view); + static void set_render_normal_field(viewer * view); + static void set_render_border(viewer * view); + static void set_render_lines(viewer * view); + static void set_render_corr(viewer * view); + static void set_is_flat(viewer * view); // draw routines - static void set_gl(); - static void set_lighting(); - static void set_mesh_materia(); - static void draw_scene(); - static void draw_corr(); - static void draw_polygons(); - static void draw_wireframe(); - static void draw_gradient_field(); - static void draw_normal_field(); - static void draw_vertices(); - static void draw_border(); - static void draw_selected_vertices(); - static void draw_vectors(); - static void draw_isolated_vertices(); - static void pick_vertex(int x, int y); + void set_gl(); + void set_lighting(); + void set_mesh_materia(); + void draw_scene(); + void draw_corr(); + void draw_polygons(); + void draw_wireframe(); + void draw_gradient_field(); + void draw_normal_field(); + void draw_vertices(); + void draw_border(); + void draw_selected_vertices(); + void draw_vectors(); + void draw_isolated_vertices(); - static GLFWwindow * window; - static int window_size[2]; - static double ww, wh; - static int m_window_size[N_MESHES][2]; - - static camera cam; - // keeps track of view state - - static shader shader_program; - // shader used to determine appearance of surface - - static bool render_wireframe; - static bool render_gradient_field; - static bool render_normal_field; - static bool render_border; - static bool render_corr; - static bool render_lines; - static bool is_flat; - static float bgc; - - static std::map processes; + void pick_vertex(int x, int y); }; void draw_str(const char * str, int x, int y, float color[4], void * font); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6949790d..92da8f3c 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -1,13 +1,13 @@ #include "app_viewer.h" +#include + using namespace std; using namespace gproshan::mdict; +// geometry processing and shape analysis framework +namespace gproshan { -// elapsed time in seconds -double load_time; -distance_t * dist; -size_t n_dist; che * load_mesh(const string & file_path) { @@ -23,7 +23,21 @@ che * load_mesh(const string & file_path) return new che_img(file_path); } -int viewer_main(int nargs, const char ** args) +app_viewer::app_viewer() +{ + dist = nullptr; + n_dist = 0; +} + +app_viewer::~app_viewer() +{ + if(dist) delete [] dist; + + for(che * mesh: meshes) + delete mesh; +} + +int app_viewer::main(int nargs, const char ** args) { if(nargs < 2) { @@ -31,324 +45,346 @@ int viewer_main(int nargs, const char ** args) return 0; } - TIC(load_time) + TIC(time) vector meshes; for(int i = 1; i < nargs; i++) meshes.push_back(load_mesh(args[i])); - TOC(load_time) + TOC(time) gproshan_log_var(sizeof(real_t)); - gproshan_log_var(load_time); - - viewer::sub_menus.push_back("Fairing"); - viewer::add_process('T', "Fairing Taubin", viewer_process_fairing_taubin); - viewer::add_process('E', "Fairing Spectral", viewer_process_fairing_spectral); - - viewer::sub_menus.push_back("Geodesics"); - viewer::add_process('F', "Geodesics (FM)", viewer_process_geodesics_fm); - viewer::add_process('U', "Geodesics (PTP_CPU)", viewer_process_geodesics_ptp_cpu); - #ifndef SINGLE_P - viewer::add_process('l', "Geodesics (HEAT_FLOW)", viewer_process_geodesics_heat_flow); - #endif + gproshan_log_var(time); + + //init mesher + add_mesh(meshes); + + sub_menus.push_back("Fairing"); + add_process('T', "Fairing Taubin", process_fairing_taubin); + add_process('E', "Fairing Spectral", process_fairing_spectral); + + sub_menus.push_back("Geodesics"); + add_process('F', "Geodesics (FM)", process_geodesics_fm); + add_process('U', "Geodesics (PTP_CPU)", process_geodesics_ptp_cpu); +#ifndef SINGLE_P + add_process('l', "Geodesics (HEAT_FLOW)", process_geodesics_heat_flow); +#endif #ifdef GPROSHAN_CUDA - viewer::add_process('G', "Geodesics (PTP_GPU)", viewer_process_geodesics_ptp_gpu); - viewer::add_process('L', "Geodesics (HEAT_FLOW_GPU)", viewer_process_geodesics_heat_flow_gpu); + add_process('G', "Geodesics (PTP_GPU)", process_geodesics_ptp_gpu); + add_process('L', "Geodesics (HEAT_FLOW_GPU)", process_geodesics_heat_flow_gpu); #endif // GPROSHAN_CUDA - viewer::add_process('S', "Farthest Point Sampling", viewer_process_farthest_point_sampling); - viewer::add_process('Q', "Farthest Point Sampling radio", viewer_process_farthest_point_sampling_radio); - viewer::add_process('V', "Voronoi Regions", viewer_process_voronoi); - viewer::add_process('P', "Toplesets", viewer_compute_toplesets); - - viewer::sub_menus.push_back("Dictionary Learning"); - viewer::add_process('.', "Mark patch", viewer_process_mdict_patch); - viewer::add_process('D', "Denoising", viewer_process_denoising); - viewer::add_process('R', "Super Resolution", viewer_process_super_resolution); - viewer::add_process('I', "Inpainting", viewer_process_inpaiting); - viewer::add_process('A', "IT Inpainting", viewer_process_iterative_inpaiting); - - viewer::sub_menus.push_back("Signatures"); - viewer::add_process('s', "GPS (norm)", viewer_process_gps); - viewer::add_process('H', "HKS (norm)", viewer_process_hks); - viewer::add_process('W', "WKS (norm)", viewer_process_wks); - viewer::add_process('X', "Functional maps", viewer_process_functional_maps); - viewer::add_process('*', "Key Points (adaptive mesh)", viewer_process_key_points); - viewer::add_process('C', "Key Components", viewer_process_key_components); - - viewer::sub_menus.push_back("Repair Holes"); - viewer::add_process('o', "Membrane surface", viewer_process_poisson_laplacian_1); - viewer::add_process('p', "Thin-plate surface", viewer_process_poisson_laplacian_2); - viewer::add_process('q', "Minimum variation surface", viewer_process_poisson_laplacian_3); - viewer::add_process('h', "Fill Holes (mesh only)", viewer_process_fill_holes); - viewer::add_process('B', "Fill holes (biharmonic splines)", viewer_process_fill_holes_biharmonic_splines); - - viewer::sub_menus.push_back("Others"); - viewer::add_process('t', "Threshold", viewer_process_thresold); - viewer::add_process('N', "Noise", viewer_process_noise); - viewer::add_process('M', "Black Noise", viewer_process_black_noise); - viewer::add_process('m', "Multiplicate Vertices", viewer_process_multiplicate_vertices); - viewer::add_process('-', "Make holes", viewer_process_delete_vertices); - viewer::add_process('d', "Delete non manifolds vertices", viewer_process_delete_non_manifold_vertices); - viewer::add_process('K', "Gaussian curvature", viewer_process_gaussian_curvature); - viewer::add_process('/', "Decimation", viewer_process_edge_collapse); - viewer::add_process(':', "Select multiple vertices", viewer_select_multiple); + add_process('S', "Farthest Point Sampling", process_farthest_point_sampling); + add_process('Q', "Farthest Point Sampling radio", process_farthest_point_sampling_radio); + add_process('V', "Voronoi Regions", process_voronoi); + add_process('P', "Toplesets", compute_toplesets); + + sub_menus.push_back("Dictionary Learning"); + add_process('.', "Mark patch", process_mdict_patch); + add_process('D', "Denoising", process_denoising); + add_process('R', "Super Resolution", process_super_resolution); + add_process('I', "Inpainting", process_inpaiting); + add_process('A', "IT Inpainting", process_iterative_inpaiting); + + sub_menus.push_back("Signatures"); + add_process('s', "GPS (norm)", process_gps); + add_process('H', "HKS (norm)", process_hks); + add_process('W', "WKS (norm)", process_wks); + add_process('X', "Functional maps", process_functional_maps); + add_process('*', "Key Points (adaptive mesh)", process_key_points); + add_process('C', "Key Components", process_key_components); + + sub_menus.push_back("Repair Holes"); + add_process('o', "Membrane surface", process_poisson_laplacian_1); + add_process('p', "Thin-plate surface", process_poisson_laplacian_2); + add_process('q', "Minimum variation surface", process_poisson_laplacian_3); + add_process('h', "Fill Holes (mesh only)", process_fill_holes); + add_process('B', "Fill holes (biharmonic splines)", process_fill_holes_biharmonic_splines); + + sub_menus.push_back("Others"); + add_process('t', "Threshold", process_thresold); + add_process('N', "Noise", process_noise); + add_process('M', "Black Noise", process_black_noise); + add_process('m', "Multiplicate Vertices", process_multiplicate_vertices); + add_process('-', "Make holes", process_delete_vertices); + add_process('d', "Delete non manifolds vertices", process_delete_non_manifold_vertices); + add_process('K', "Gaussian curvature", process_gaussian_curvature); + add_process('/', "Decimation", process_edge_collapse); + add_process(':', "Select multiple vertices", select_multiple); + + + run(); - dist = nullptr; - n_dist = 0; - - //init viewer - viewer::init(meshes); - - if(dist) delete [] dist; - for(che * mesh: meshes) - delete mesh; return 0; } -void paint_holes_vertices() +void paint_holes_vertices(viewer * p_view) { - size_t nv = viewer::mesh().n_vertices(); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); + + size_t nv = mesh->n_vertices(); - viewer::mesh().update(); + mesh.update(); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - if(v >= nv) viewer::vcolor(v) = .25; + for(index_t v = 0; v < mesh->n_vertices(); v++) + if(v >= nv) mesh.color(v) = .25; } -void viewer_process_delete_non_manifold_vertices() +void app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_debug(removing vertex); - viewer::mesh()->remove_non_manifold_vertices(); + mesh->remove_non_manifold_vertices(); gproshan_debug(removing vertex); } -void viewer_process_delete_vertices() +void app_viewer::process_delete_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) return; + if(!view->select_vertices.size()) return; gproshan_debug(removing vertex); - viewer::mesh()->remove_vertices(viewer::select_vertices); - viewer::select_vertices.clear(); + mesh->remove_vertices(view->select_vertices); + view->select_vertices.clear(); gproshan_debug(removing vertex); } -void viewer_process_poisson(const index_t & k) +void app_viewer::process_poisson(viewer * p_view, const index_t & k) { - size_t old_n_vertices = viewer::mesh()->n_vertices(); - delete [] fill_all_holes(viewer::mesh()); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - TIC(load_time) poisson(viewer::mesh(), old_n_vertices, k); TOC(load_time) - gproshan_log_var(load_time); + size_t old_n_vertices = mesh->n_vertices(); + delete [] fill_all_holes(mesh); + + TIC(view->time) poisson(mesh, old_n_vertices, k); TOC(view->time) + gproshan_log_var(view->time); // paint_holes_vertices(); } -void viewer_process_poisson_laplacian_1() +void app_viewer::process_poisson_laplacian_1(viewer * p_view) { gproshan_log(APP_VIEWER); - viewer_process_poisson(1); + process_poisson(p_view, 1); } -void viewer_process_poisson_laplacian_2() +void app_viewer::process_poisson_laplacian_2(viewer * p_view) { gproshan_log(APP_VIEWER); - viewer_process_poisson(2); + process_poisson(p_view, 2); } -void viewer_process_poisson_laplacian_3() +void app_viewer::process_poisson_laplacian_3(viewer * p_view) { gproshan_log(APP_VIEWER); - viewer_process_poisson(3); + process_poisson(p_view, 3); } -void viewer_process_fill_holes() +void app_viewer::process_fill_holes(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - fill_all_holes(viewer::mesh()); + fill_all_holes(mesh); - paint_holes_vertices(); + paint_holes_vertices(p_view); } -void viewer_process_noise() +void app_viewer::process_noise(viewer * p_view) { - viewer::share = (char *) new vertex; - delete [] viewer::share; gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - srand(time(nullptr)); + std::default_random_engine generator; + std::uniform_int_distribution d_mod_5(0, 4); + std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { - distance_t r = distance_t( rand() % 1000 ) / 200000; - int p = rand() % 5; - viewer::mesh()->get_vertex(v) += (!p) * r * viewer::mesh()->normal(v); + distance_t r = distance_t(d_mod_1000(generator)) / 200000; + mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_black_noise() +void app_viewer::process_black_noise(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - srand(time(nullptr)); + std::default_random_engine generator; + std::uniform_int_distribution d_mod_5(0, 4); + std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { - distance_t r = distance_t( rand() % 1000 ) / 200000; - int p = rand() % 5; - viewer::mesh()->get_vertex(v) += (!p) * r * viewer::mesh()->normal(v); - if(!p) viewer::vcolor(v) = INFINITY; + distance_t r = distance_t(d_mod_1000(generator)) / 200000; + mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_thresold() +void app_viewer::process_thresold(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) = viewer::vcolor(v) > 0.5 ? 1 : 0.5; + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh.color(v) = mesh.color(v) > 0.5 ? 1 : 0.5; } -void viewer_process_functional_maps() +void app_viewer::process_functional_maps(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t K = 100; a_sp_mat L, A; - TIC(load_time) laplacian(viewer::mesh(), L, A); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); a_vec eigval; a_mat eigvec; - TIC(load_time) K = eigs_laplacian(eigval, eigvec, viewer::mesh(), L, A, K); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); gproshan_log_var(K); K = K < N_MESHES ? K : N_MESHES; for(index_t k = 0; k < N_MESHES; k++) { - if(k) viewer::add_mesh({new che(*viewer::mesh())}); - viewer::current = k; + if(k) view->add_mesh({new che(*mesh)}); + view->current = k; eigvec.col(k) -= eigvec.col(k).min(); eigvec.col(k) /= eigvec.col(k).max(); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) = eigvec(v, k); + for(index_t v = 0; v < mesh->n_vertices(); v++) + view->mesh().color(v) = eigvec(v, k); } - viewer::current = 0; + view->current = 0; } -void viewer_process_wks() +void app_viewer::process_wks(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t K = 50, T = 100; a_sp_mat L, A; - TIC(load_time) laplacian(viewer::mesh(), L, A); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); a_vec eigval; a_mat eigvec; - TIC(load_time) K = eigs_laplacian(eigval, eigvec, viewer::mesh(), L, A, K); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); distance_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { a_vec s(T, arma::fill::zeros); for(index_t t = 0; t < T; t++) for(index_t k = 1; k < K; k++) s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - viewer::vcolor(v) = norm(s); - max_s = max(max_s, viewer::vcolor(v)); + mesh.color(v) = norm(s); + max_s = max(max_s, mesh.color(v)); } #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) /= max_s; + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh.color(v) /= max_s; } -void viewer_process_hks() +void app_viewer::process_hks(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t K = 100; size_t T = 100; a_sp_mat L, A; - TIC(load_time) laplacian(viewer::mesh(), L, A); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); a_vec eigval; a_mat eigvec; - TIC(load_time) K = eigs_laplacian(eigval, eigvec, viewer::mesh(), L, A, K); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); if(!K) return; distance_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { a_vec s(T, arma::fill::zeros); for(index_t t = 0; t < T; t++) for(index_t k = 1; k < K; k++) s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); - viewer::vcolor(v) = norm(abs(arma::fft(s, 128))); - //viewer::vcolor(v) = norm(s); - max_s = max(max_s, viewer::vcolor(v)); + mesh.color(v) = norm(abs(arma::fft(s, 128))); + //mesh.color(v) = norm(s); + max_s = max(max_s, mesh.color(v)); } #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) /= max_s; + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh.color(v) /= max_s; } -void viewer_process_gps() +void app_viewer::process_gps(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t K = 50; a_sp_mat L, A; - TIC(load_time) laplacian(viewer::mesh(), L, A); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); a_vec eigval; a_mat eigvec; - TIC(load_time) K = eigs_laplacian(eigval, eigvec, viewer::mesh(), L, A, K); TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); eigvec = abs(eigvec); eigvec.col(0).zeros(); @@ -360,97 +396,104 @@ void viewer_process_gps() distance_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { - viewer::vcolor(v) = norm(eigvec.row(v)); - max_s = max(max_s, viewer::vcolor(v)); + mesh.color(v) = norm(eigvec.row(v)); + max_s = max(max_s, mesh.color(v)); } #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) /= max_s; + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh.color(v) /= max_s; } -void viewer_process_key_points() +void app_viewer::process_key_points(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - key_points kps(viewer::mesh()); + key_points kps(mesh); - viewer::select_vertices.clear(); - viewer::select_vertices.reserve(kps.size()); + view->select_vertices.clear(); + view->select_vertices.reserve(kps.size()); for(index_t i = 0; i < kps.size(); i++) - viewer::select_vertices.push_back(kps[i]); + view->select_vertices.push_back(kps[i]); } -void viewer_process_key_components() +void app_viewer::process_key_components(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - key_points kps(viewer::mesh()); - key_components kcs(viewer::mesh(), kps, .25); + key_points kps(mesh); + key_components kcs(mesh, kps, .25); gproshan_debug_var(kcs); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) - viewer::vcolor(v) = (real_t) kcs(v) / kcs; + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh.color(v) = (real_t) kcs(v) / kcs; } -void viewer_process_mdict_patch() +void app_viewer::process_mdict_patch(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - TIC(load_time) - che * mesh = viewer::mesh(); + TIC(view->time) index_t * toplevel = new index_t[mesh->n_vertices()]; size_t avg_nvp = 0; vertex vdir; patch p; distance_t mean_edge = mesh->mean_edge(); - for(auto & v: viewer::select_vertices) + for(auto & v: view->select_vertices) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) - viewer::vcolor(u) = 1; + mesh.color(u) = 1; vdir.x = p.T(0, 0); vdir.y = p.T(0, 1); vdir.z = p.T(0, 2); - viewer::vectors.push_back(mesh->gt(v)); - viewer::vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->gt(v)); + view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); vdir.x = p.T(1, 0); vdir.y = p.T(1, 1); vdir.z = p.T(1, 2); - viewer::vectors.push_back(mesh->gt(v)); - viewer::vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->gt(v)); + view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); vdir.x = p.T(2, 0); vdir.y = p.T(2, 1); vdir.z = p.T(2, 2); - viewer::vectors.push_back(mesh->gt(v)); - viewer::vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->gt(v)); + view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); - viewer::vectors.push_back(mesh->gt(v)); - viewer::vectors.push_back(mesh->gt(v) + 3 * mean_edge * mesh->normal(v)); + view->vectors.push_back(mesh->gt(v)); + view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * mesh->normal(v)); avg_nvp += p.vertices.size(); } - avg_nvp /= viewer::select_vertices.size(); + avg_nvp /= view->select_vertices.size(); gproshan_debug_var(avg_nvp); delete [] toplevel; - TOC(load_time) - gproshan_debug_var(load_time); + TOC(view->time) + gproshan_debug_var(view->time); } -void viewer_process_denoising() +void app_viewer::process_denoising(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t n; // dct size_t m, M; @@ -461,16 +504,18 @@ void viewer_process_denoising() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - denoising dict(viewer::mesh(), phi, m, M, f); + denoising dict(mesh, phi, m, M, f); dict.execute(); delete phi; - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_super_resolution() +void app_viewer::process_super_resolution(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t n; // dct size_t m, M; @@ -481,16 +526,18 @@ void viewer_process_super_resolution() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - super_resolution dict(viewer::mesh(), phi, m, M, f); + super_resolution dict(mesh, phi, m, M, f); dict.execute(); delete phi; - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_inpaiting() +void app_viewer::process_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t n; // dct size_t m, M; @@ -501,48 +548,52 @@ void viewer_process_inpaiting() cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - inpainting dict(viewer::mesh(), phi, m, M, f); + inpainting dict(mesh, phi, m, M, f); dict.execute(); delete phi; - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_iterative_inpaiting() +void app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); -// mesh_iterative_inpaiting(viewer::mesh(), viewer::select_vertices, freq, rt, m, M, f, learn); +// mesh_iterative_inpaiting(mesh, view->select_vertices, freq, rt, m, M, f, learn); } -void viewer_process_multiplicate_vertices() +void app_viewer::process_multiplicate_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - viewer::mesh()->multiplicate_vertices(); - viewer::mesh().log_info(); + mesh->multiplicate_vertices(); + mesh.log_info(); } -void viewer_compute_toplesets() +void app_viewer::compute_toplesets(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - index_t * toplesets = new index_t[viewer::mesh()->n_vertices()]; - index_t * sorted = new index_t[viewer::mesh()->n_vertices()]; + index_t * toplesets = new index_t[mesh->n_vertices()]; + index_t * sorted = new index_t[mesh->n_vertices()]; vector limites; - viewer::mesh()->compute_toplesets(toplesets, sorted, limites, viewer::select_vertices); + mesh->compute_toplesets(toplesets, sorted, limites, view->select_vertices); size_t n_toplesets = limites.size() - 1; #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh()->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { if(toplesets[v] < n_toplesets) - viewer::vcolor(v) = distance_t(toplesets[v]) / (n_toplesets); + mesh.color(v) = distance_t(toplesets[v]) / (n_toplesets); } gproshan_debug_var(n_toplesets); @@ -551,30 +602,34 @@ void viewer_compute_toplesets() delete [] sorted; } -void viewer_process_voronoi() +void app_viewer::process_voronoi(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - TIC(load_time) + TIC(view->time) #ifdef GPROSHAN_CUDA - geodesics ptp(viewer::mesh(), viewer::select_vertices, geodesics::PTP_GPU, nullptr, 1); + geodesics ptp(mesh, view->select_vertices, geodesics::PTP_GPU, nullptr, 1); #else - geodesics ptp(viewer::mesh(), viewer::select_vertices, geodesics::FM, nullptr, 1); + geodesics ptp(mesh, view->select_vertices, geodesics::FM, nullptr, 1); #endif - TOC(load_time) - gproshan_log_var(load_time); + TOC(view->time) + gproshan_log_var(view->time); #pragma omp parallel for - for(index_t i = 0; i < viewer::mesh()->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices(); i++) { - viewer::vcolor(i) = ptp.clusters[i]; - viewer::vcolor(i) /= viewer::select_vertices.size() + 1; + mesh.color(i) = ptp.clusters[i]; + mesh.color(i) /= view->select_vertices.size() + 1; } } -void viewer_process_farthest_point_sampling_radio() +void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_input(radio); distance_t radio; cin >> radio; @@ -582,167 +637,185 @@ void viewer_process_farthest_point_sampling_radio() #ifdef GPROSHAN_CUDA // IMPLEMENT/REVIEW double time_fps; - TIC(load_time) - radio = farthest_point_sampling_ptp_gpu(viewer::mesh(), viewer::select_vertices, time_fps, NIL, radio); - TOC(load_time) + TIC(view->time) + radio = farthest_point_sampling_ptp_gpu(mesh, view->select_vertices, time_fps, NIL, radio); + TOC(view->time) gproshan_log_var(time_fps); #endif // GPROSHAN_CUDA gproshan_log_var(radio); - gproshan_log_var(viewer::select_vertices.size()); - gproshan_log_var(load_time); + gproshan_log_var(view->select_vertices.size()); + gproshan_log_var(view->time); } -void viewer_process_farthest_point_sampling() +void app_viewer::process_farthest_point_sampling(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_input(samples_number); index_t n; cin >> n; distance_t radio; - TIC(load_time) - load_sampling(viewer::select_vertices, radio, viewer::mesh(), n); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + load_sampling(view->select_vertices, radio, mesh, n); + TOC(view->time) + gproshan_log_var(view->time); } -void viewer_process_fairing_spectral() +void app_viewer::process_fairing_spectral(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_input(k (eigenvectors number)); size_t k; cin >> k; fairing * fair = new fairing_spectral(k); - fair->run(viewer::mesh()); + fair->run(mesh); - viewer::mesh()->set_vertices(fair->get_postions()); + mesh->set_vertices(fair->get_postions()); delete fair; - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_fairing_taubin() +void app_viewer::process_fairing_taubin(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_input(step); real_t step; cin >> step; fairing * fair = new fairing_taubin(step); - fair->run(viewer::mesh()); + fair->run(mesh); - viewer::mesh()->set_vertices(fair->get_postions()); + mesh->set_vertices(fair->get_postions()); delete fair; - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_geodesics_fm() +void app_viewer::process_geodesics_fm(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - TIC(load_time) - geodesics fm(viewer::mesh(), viewer::select_vertices); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + geodesics fm(mesh, view->select_vertices); + TOC(view->time) + gproshan_log_var(view->time); - viewer::mesh().update_colors(&fm[0]); + mesh.update_colors(&fm[0]); } -void viewer_process_geodesics_ptp_cpu() +void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - TIC(load_time) - geodesics ptp(viewer::mesh(), viewer::select_vertices, geodesics::PTP_CPU); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + geodesics ptp(mesh, view->select_vertices, geodesics::PTP_CPU); + TOC(view->time) + gproshan_log_var(view->time); - viewer::mesh().update_colors(&ptp[0]); + mesh.update_colors(&ptp[0]); } -void viewer_process_geodesics_heat_flow() +void app_viewer::process_geodesics_heat_flow(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - TIC(load_time) - geodesics heat_flow(viewer::mesh(), viewer::select_vertices, geodesics::HEAT_FLOW); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + geodesics heat_flow(mesh, view->select_vertices, geodesics::HEAT_FLOW); + TOC(view->time) + gproshan_log_var(view->time); - viewer::mesh().update_colors(&heat_flow[0]); + mesh.update_colors(&heat_flow[0]); } #ifdef GPROSHAN_CUDA -void viewer_process_geodesics_ptp_gpu() +void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - if(dist && n_dist != viewer::mesh().n_vertices()) + if(view->dist && view->n_dist != mesh.n_vertices()) { - delete [] dist; - n_dist = 0; - dist = nullptr; + delete [] view->dist; + view->n_dist = 0; + view->dist = nullptr; } - if(!dist) + if(!view->dist) { - n_dist = viewer::mesh().n_vertices(); - dist = new distance_t[n_dist]; + view->n_dist = mesh.n_vertices(); + view->dist = new distance_t[view->n_dist]; } - TIC(load_time) - geodesics ptp(viewer::mesh(), viewer::select_vertices, geodesics::PTP_GPU, dist); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + geodesics ptp(mesh, view->select_vertices, geodesics::PTP_GPU, view->dist); + TOC(view->time) + gproshan_log_var(view->time); - viewer::mesh().update_colors(&ptp[0]); + mesh.update_colors(&ptp[0]); } -void viewer_process_geodesics_heat_flow_gpu() +void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - if(!viewer::select_vertices.size()) - viewer::select_vertices.push_back(0); + if(!view->select_vertices.size()) + view->select_vertices.push_back(0); - TIC(load_time) - geodesics heat_flow(viewer::mesh(), viewer::select_vertices, geodesics::HEAT_FLOW_GPU); - TOC(load_time) - gproshan_log_var(load_time); + TIC(view->time) + geodesics heat_flow(mesh, view->select_vertices, geodesics::HEAT_FLOW_GPU); + TOC(view->time) + gproshan_log_var(view->time); - viewer::mesh().update_colors(&heat_flow[0]); + mesh.update_colors(&heat_flow[0]); } #endif // GPROSHAN_CUDA -void viewer_process_fill_holes_biharmonic_splines() +void app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); - size_t old_n_vertices, n_vertices = viewer::mesh().n_vertices(); - size_t n_holes = viewer::mesh()->n_borders(); + size_t old_n_vertices, n_vertices = mesh.n_vertices(); + size_t n_holes = mesh->n_borders(); vector * border_vertices; che ** holes; - tie(border_vertices, holes) = fill_all_holes_meshes(viewer::mesh()); + tie(border_vertices, holes) = fill_all_holes_meshes(mesh); if(!holes) return; index_t k = 2; @@ -751,35 +824,37 @@ void viewer_process_fill_holes_biharmonic_splines() if(holes[h]) { old_n_vertices = n_vertices; - biharmonic_interp_2(viewer::mesh(), old_n_vertices, n_vertices += holes[h]->n_vertices() - border_vertices[h].size(), border_vertices[h], k); + biharmonic_interp_2(mesh, old_n_vertices, n_vertices += holes[h]->n_vertices() - border_vertices[h].size(), border_vertices[h], k); delete holes[h]; } delete [] holes; delete [] border_vertices; - paint_holes_vertices(); + paint_holes_vertices(p_view); } -void viewer_process_gaussian_curvature() +void app_viewer::process_gaussian_curvature(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; - a_vec gv(viewer::mesh().n_vertices()); + a_vec gv(mesh.n_vertices()); #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) - for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) + for(index_t v = 0; v < mesh.n_vertices(); v++) { g = 0; - for_star(he, viewer::mesh(), v) + for_star(he, mesh, v) { - a = viewer::mesh()->gt_vt(next(he)) - viewer::mesh()->gt(v); - b = viewer::mesh()->gt_vt(prev(he)) - viewer::mesh()->gt(v); + a = mesh->gt_vt(next(he)) - mesh->gt(v); + b = mesh->gt_vt(prev(he)) - mesh->gt(v); g += acos((a,b) / (*a * *b)); } - gv(v) = (2 * M_PI - g) / viewer::mesh()->area_vertex(v); + gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); g_max = max(g_max, gv(v)); g_min = min(g_min, gv(v)); } @@ -787,7 +862,7 @@ void viewer_process_gaussian_curvature() g = g_max - g_min; #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) + for(index_t v = 0; v < mesh.n_vertices(); v++) gv(v) = (gv(v) - g_min) / g; real_t gm = mean(gv); @@ -804,30 +879,33 @@ void viewer_process_gaussian_curvature() }; #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) - viewer::vcolor(v) = f(gv(v)); + for(index_t v = 0; v < mesh.n_vertices(); v++) + mesh.color(v) = f(gv(v)); } -void viewer_process_edge_collapse() +void app_viewer::process_edge_collapse(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); index_t levels; cin >> levels; - TIC(load_time) decimation sampling(viewer::mesh(), viewer::mesh().normals_ptr(), levels); TOC(load_time) - gproshan_debug_var(load_time); + TIC(view->time) decimation sampling(mesh, mesh.normals_ptr(), levels); TOC(view->time) + gproshan_debug_var(view->time); - if(viewer::n_meshes < 2) - viewer::add_mesh({new che(*viewer::mesh())}); + if(view->n_meshes < 2) + view->add_mesh({new che(*mesh)}); - viewer::corr_mesh[1].init(viewer::meshes[1]->n_vertices(), viewer::current, sampling); - viewer::current = 1; + view->corr_mesh[1].init(view->meshes[1]->n_vertices(), view->current, sampling); + view->current = 1; } -void viewer_select_multiple() +void app_viewer::select_multiple(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; char line[128]; if(fgets(line, 128, stdin)) @@ -835,7 +913,10 @@ void viewer_select_multiple() stringstream ss(line); index_t v; while(ss >> v) - viewer::select_vertices.push_back(v); + view->select_vertices.push_back(v); } } + +} // namespace gproshan + diff --git a/src/che_fill_hole.cpp b/src/che_fill_hole.cpp index d6c1f169..3978647e 100644 --- a/src/che_fill_hole.cpp +++ b/src/che_fill_hole.cpp @@ -56,7 +56,7 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, ve = E.t() * ve; vertices.push_back(*((vertex *) ve.memptr())); - viewer::other_vertices.push_back(vertices.back()); + //viewer::other_vertices.push_back(vertices.back()); } return fill_hole_front_angles(vertices, mesh->mean_edge(), normal, max_iter); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 16f2692f..edcddfa5 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -19,63 +19,16 @@ using namespace std; namespace gproshan { -// declare static member variables -che_viewer viewer::meshes[N_MESHES]; -vcorr_t viewer::corr_mesh[N_MESHES]; // zero initialization -size_t viewer::n_meshes = 0; -index_t viewer::current = 0; - -vector viewer::select_vertices; -vector viewer::other_vertices; -vector viewer::vectors; -vector viewer::sub_menus; - -char * viewer::share = nullptr; - -GLFWwindow * viewer::window = nullptr; -int viewer::window_size[2] = {1366, 768}; -int viewer::m_window_size[N_MESHES][2] = { {1, 1}, {1, 2}, {1, 3}, +const int viewer::m_window_size[N_MESHES][2] = { {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {2, 3}, {2, 4}, {2, 4}, {2, 5}, {2, 5}, {3, 4}, {3, 4} }; -double viewer::ww = 0; -double viewer::wh = 0; -camera viewer::cam; -shader viewer::shader_program; -bool viewer::render_wireframe = false; -bool viewer::render_gradient_field = false; -bool viewer::render_normal_field = false; -bool viewer::render_border = false; -bool viewer::render_corr = false; -bool viewer::render_lines = false; -bool viewer::is_flat = false; -float viewer::bgc = 0.; -map viewer::processes; - -const int & viewer::window_width() -{ - return window_size[0]; -} - -const int & viewer::window_height() -{ - return window_size[1]; -} - -che_viewer & viewer::mesh() +viewer::viewer() { - assert(n_meshes > 0); - return meshes[current]; -} - -void viewer::init(const vector & _meshes) -{ -// restoreviewerState(); - init_glut(); - add_mesh(_meshes); +// add_mesh(_meshes); glfwSetWindowTitle(window, mesh()->filename().c_str()); init_menus(); @@ -85,14 +38,27 @@ void viewer::init(const vector & _meshes) set_gl(); init_glsl(); +} +viewer::~viewer() +{ + glfwDestroyWindow(window); + glfwTerminate(); +} + +bool viewer::run() +{ while(!glfwWindowShouldClose(window)) { display(); } - glfwDestroyWindow(window); - glfwTerminate(); + return true; +} + +che_viewer & viewer::mesh() +{ + return meshes[current]; } void viewer::debug_info() @@ -127,7 +93,9 @@ void viewer::init_glut() glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); #endif - window = glfwCreateWindow(window_size[0], window_size[1], "gproshan", NULL, NULL); + window = glfwCreateWindow(1600, 900, "gproshan", NULL, NULL); + + glfwSetWindowUserPointer(window, this); glfwSetKeyCallback(window, keyboard); glfwSetMouseButtonCallback(window, mouse); @@ -157,7 +125,6 @@ void viewer::init_menus() add_process('w', "Write Mesh", menu_save_mesh); add_process('<', "Zoom In", menu_zoom_in); add_process('>', "Zoom Out", menu_zoom_out); - add_process(27, "Exit", menu_exit); } void viewer::init_glsl() @@ -166,21 +133,12 @@ void viewer::init_glsl() shader_program.load_fragment("../shaders/new_fragment.glsl"); } -color_t & viewer::vcolor(const index_t & i) -{ - return mesh().color(i); -} - void viewer::update_vbo() { for(index_t i = 0; i < n_meshes; i++) meshes[i].update(); } -void viewer::menu(int ) -{ -} - void viewer::menu_process(int value) { menu_process(processes[value].function); @@ -203,7 +161,7 @@ void viewer::add_mesh(const vector & _meshes) meshes[n_meshes++].init(_mesh); } - int * mw = m_window_size[n_meshes - 1]; + const int * mw = m_window_size[n_meshes - 1]; index_t m = n_meshes - 1; for(int i = mw[1] - 1; i >= 0; i--) @@ -215,30 +173,32 @@ void viewer::add_mesh(const vector & _meshes) } } -void viewer::keyboard(GLFWwindow * w, int key, int scancode, int action, int mods) +void viewer::keyboard(GLFWwindow * window, int key, int scancode, int action, int mods) { if(action == GLFW_RELEASE) return; + + viewer * view = (viewer *) glfwGetWindowUserPointer(window); switch(key) { case GLFW_KEY_UP: - cam.zoomIn(); + view->cam.zoomIn(); break; case GLFW_KEY_DOWN: - cam.zoomOut(); + view->cam.zoomOut(); break; case GLFW_KEY_ESCAPE: - glfwSetWindowShouldClose(w, GLFW_TRUE); + glfwSetWindowShouldClose(window, GLFW_TRUE); break; } if(key >= '0' && key <= '9') { - bgc = (key - '0') / 9.; - glClearColor(bgc, bgc, bgc, 1.); + view->bgc = (key - '0') / 9.; + glClearColor(view->bgc, view->bgc, view->bgc, 1.); } - - menu_process(processes[key].function); + + view->menu_process(view->processes[key].function); } void viewer::menu_meshes(int value) @@ -250,117 +210,117 @@ void viewer::menu_meshes(int value) void viewer::mouse(GLFWwindow* window, int button, int action, int mods) { + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) - pick_vertex(xpos, ypos); + view->pick_vertex(xpos, ypos); //else if(button == 6 || button == 4) cam.zoomIn(); //else if(button == 5 || button == 3) cam.zoomOut(); - else cam.mouse(button, action, xpos, ypos); + else view->cam.mouse(button, action, xpos, ypos); } void viewer::motion(int x, int y) { - cam.motion(x, y); + //viewer * view = (viewer *) glfwGetWindowUserPointer(window); + //view->cam.motion(x, y); } void viewer::idle() { - cam.idle(); + //cam.idle(); //glutPostRedisplay(); } void viewer::menu_process(function_t pro) { - if(pro) pro(); + if(pro) pro(this); + update_vbo(); } -void viewer::menu_reset_mesh() +void viewer::menu_reset_mesh(viewer * view) { - select_vertices.clear(); - other_vertices.clear(); - vectors.clear(); + view->select_vertices.clear(); + view->other_vertices.clear(); + view->vectors.clear(); - mesh().reload(); + view->mesh().reload(); // mesh().debug_info(); - update_vbo(); + view->update_vbo(); } -void viewer::menu_save_mesh() +void viewer::menu_save_mesh(viewer * view) { gproshan_log(APP_VIEWER); gproshan_log(format: [off obj ply]); string format; cin >> format; - string file = mesh()->filename() + "_new"; + string file = view->mesh()->filename() + "_new"; - if(format == "off") che_off::write_file(mesh(), file); - if(format == "obj") che_obj::write_file(mesh(), file); - if(format == "ply") che_ply::write_file(mesh(), file); + if(format == "off") che_off::write_file(view->mesh(), file); + if(format == "obj") che_obj::write_file(view->mesh(), file); + if(format == "ply") che_ply::write_file(view->mesh(), file); cerr << "saved: " << file + "." + format << endl; } -void viewer::menu_exit() +void viewer::menu_zoom_in(viewer * view) { + view->cam.zoomIn(); } -void viewer::menu_zoom_in() +void viewer::menu_zoom_out(viewer * view) { - cam.zoomIn(); + view->cam.zoomOut(); } -void viewer::menu_zoom_out() +void viewer::invert_orientation(viewer * view) { - cam.zoomOut(); + view->mesh().invert_orientation(); + view->mesh().update_normals(); + view->update_vbo(); } -void viewer::invert_orientation() +void viewer::set_render_wireframe(viewer * view) { - mesh().invert_orientation(); - mesh().update_normals(); - update_vbo(); -} - -void viewer::set_render_wireframe() -{ - render_wireframe = !render_wireframe; + view->render_wireframe = !view->render_wireframe; } -void viewer::set_render_gradient_field() +void viewer::set_render_gradient_field(viewer * view) { - render_gradient_field = !render_gradient_field; + view->render_gradient_field = !view->render_gradient_field; } -void viewer::set_render_normal_field() +void viewer::set_render_normal_field(viewer * view) { - render_normal_field = !render_normal_field; + view->render_normal_field = !view->render_normal_field; } -void viewer::set_render_border() +void viewer::set_render_border(viewer * view) { - render_border = !render_border; - if(!render_border) select_vertices.clear(); + view->render_border = !view->render_border; + if(!view->render_border) view->select_vertices.clear(); } -void viewer::set_render_lines() +void viewer::set_render_lines(viewer * view) { - render_lines = !render_lines; + view->render_lines = !view->render_lines; } -void viewer::set_render_corr() +void viewer::set_render_corr(viewer * view) { - render_corr = !render_corr; + view->render_corr = !view->render_corr; } -void viewer::set_is_flat() +void viewer::set_is_flat(viewer * view) { - is_flat = !is_flat; + view->is_flat = !view->is_flat; } void viewer::display() @@ -428,10 +388,6 @@ void viewer::display() set_mesh_materia(); - int width, height; - glfwGetFramebufferSize(window, &width, &height); - ww = width; - wh = height; draw_scene(); //glPopAttrib(); @@ -494,6 +450,9 @@ void viewer::draw_polygons() glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1., 1.); + int ww, wh; + glfwGetFramebufferSize(window, &ww, &wh); + for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); @@ -515,6 +474,9 @@ void viewer::draw_wireframe() glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + int ww, wh; + glfwGetFramebufferSize(window, &ww, &wh); + for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); @@ -669,6 +631,10 @@ void viewer::draw_selected_vertices() glColor3f(0., 0.5, 0.5); double h = 0.02 * cam.zoom; + + int ww, wh; + glfwGetFramebufferSize(window, &ww, &wh); + for(int v: select_vertices) { glViewport(mesh().vx * ww, mesh().vy * wh, ww, wh); @@ -687,6 +653,10 @@ void viewer::draw_selected_vertices() void viewer::draw_normal_field() { shader_program.disable(); + + int ww, wh; + glfwGetFramebufferSize(window, &ww, &wh); + for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); @@ -697,6 +667,10 @@ void viewer::draw_normal_field() void viewer::draw_gradient_field() { shader_program.disable(); + + int ww, wh; + glfwGetFramebufferSize(window, &ww, &wh); + for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); From 0c4e608ad9e0f0e5d64cb0aab32c93d382b39bab Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 13:05:27 +0100 Subject: [PATCH 0053/1018] cgal cmake debug, init values --- CMakeLists.txt | 2 +- src/viewer/viewer.cpp | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f8b9a3bf..33e39478 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_link_libraries(gproshan_cpp GLEW::GLEW) target_link_libraries(gproshan_cpp glfw) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp ${CGAL_LIBRARIES}) +target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) if(CUDA_FOUND) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index edcddfa5..6e4442b3 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -19,25 +19,33 @@ using namespace std; namespace gproshan { -const int viewer::m_window_size[N_MESHES][2] = { {1, 1}, {1, 2}, {1, 3}, - {2, 2}, {2, 3}, {2, 3}, - {2, 4}, {2, 4}, {2, 5}, - {2, 5}, {3, 4}, {3, 4} }; +const int viewer::m_window_size[N_MESHES][2] = {{1, 1}, {1, 2}, {1, 3}, + {2, 2}, {2, 3}, {2, 3}, + {2, 4}, {2, 4}, {2, 5}, + {2, 5}, {3, 4}, {3, 4}}; viewer::viewer() { - init_glut(); -// add_mesh(_meshes); - - glfwSetWindowTitle(window, mesh()->filename().c_str()); - init_menus(); + window = nullptr; + + n_meshes = current = 0; + + render_wireframe = false; + render_gradient_field = false; + render_normal_field = false; + render_border = false; + render_lines = false; + render_corr = false; - debug_info(); - mesh().log_info(); + bgc = 0; + init_glut(); + init_menus(); set_gl(); init_glsl(); + + debug_info(); } viewer::~viewer() @@ -161,6 +169,10 @@ void viewer::add_mesh(const vector & _meshes) meshes[n_meshes++].init(_mesh); } + if(!n_meshes) return; + + glfwSetWindowTitle(window, mesh()->filename().c_str()); + const int * mw = m_window_size[n_meshes - 1]; index_t m = n_meshes - 1; From 88de88d9fc626247bf425814aa09e1d5130776eb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 13:27:42 +0100 Subject: [PATCH 0054/1018] add cursor callback --- include/viewer/viewer.h | 2 +- src/viewer/viewer.cpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 9f1eeece..d93c1e96 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -123,7 +123,7 @@ class viewer static void idle(); static void keyboard(GLFWwindow * window, int key, int scancode, int action, int mods); static void mouse(GLFWwindow * window, int button, int action, int mods); - static void motion(int x, int y); + static void motion(GLFWwindow * window, double x, double y); // menu functions void menu_meshes(int value); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 6e4442b3..298f7670 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -107,6 +107,7 @@ void viewer::init_glut() glfwSetKeyCallback(window, keyboard); glfwSetMouseButtonCallback(window, mouse); + glfwSetCursorPosCallback(window, motion); glfwMakeContextCurrent(window); glfwSwapInterval(0); @@ -235,10 +236,14 @@ void viewer::mouse(GLFWwindow* window, int button, int action, int mods) else view->cam.mouse(button, action, xpos, ypos); } -void viewer::motion(int x, int y) +void viewer::motion(GLFWwindow * window, double x, double y) { - //viewer * view = (viewer *) glfwGetWindowUserPointer(window); - //view->cam.motion(x, y); + int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT); + if(state == GLFW_PRESS) + { + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + view->cam.motion(x, y); + } } void viewer::idle() @@ -261,7 +266,6 @@ void viewer::menu_reset_mesh(viewer * view) view->vectors.clear(); view->mesh().reload(); -// mesh().debug_info(); view->update_vbo(); } From f0ac9c1e1c1e33fd7568cd2cc981ea24cd85d604 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 13:56:25 +0100 Subject: [PATCH 0055/1018] maping keys to functions --- include/viewer/viewer.h | 8 +++-- src/viewer/viewer.cpp | 71 ++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index d93c1e96..f998fc8e 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -88,7 +88,7 @@ class viewer bool is_flat; float bgc; - std::map processes; + std::map processes; public: @@ -105,7 +105,7 @@ class viewer bool run(); che_viewer & mesh(); - void add_process(const char & key, const std::string & name, function_t function); + void add_process(const int & key, const std::string & name, function_t function); void add_mesh(const std::vector & _meshes); private: @@ -134,6 +134,10 @@ class viewer static void menu_save_mesh(viewer * view); static void menu_zoom_in(viewer * view); static void menu_zoom_out(viewer * view); + static void menu_bgc_inc(viewer * view); + static void menu_bgc_dec(viewer * view); + static void menu_bgc_white(viewer * view); + static void menu_bgc_black(viewer * view); // render options static void invert_orientation(viewer * view); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 298f7670..60c65776 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -119,21 +119,25 @@ void viewer::init_menus() { // init viewer menu sub_menus.push_back("viewer"); - add_process('i', "Invert Orientation", invert_orientation); - add_process('f', "Wireframe", set_render_wireframe); - add_process('g', "Gradient Field", set_render_gradient_field); - add_process('n', "Normal Field", set_render_normal_field); - add_process('b', "Show Border", set_render_border); - add_process(' ', "Lines", set_render_lines); + add_process(GLFW_KEY_PERIOD, "Invert Orientation", invert_orientation); + add_process(GLFW_KEY_F2, "Wireframe", set_render_wireframe); + add_process(GLFW_KEY_F3, "Gradient Field", set_render_gradient_field); + add_process(GLFW_KEY_F4, "Normal Field", set_render_normal_field); + add_process(GLFW_KEY_F5, "Show Border", set_render_border); + add_process(GLFW_KEY_SPACE, "Lines", set_render_lines); add_process('+', "Show Corr", set_render_corr); - add_process('\t', "Flat", set_is_flat); + add_process(GLFW_KEY_TAB, "Flat", set_is_flat); // init mesh menu sub_menus.push_back("Mesh"); - add_process('r', "Reset Mesh", menu_reset_mesh); - add_process('w', "Write Mesh", menu_save_mesh); - add_process('<', "Zoom In", menu_zoom_in); - add_process('>', "Zoom Out", menu_zoom_out); + add_process(GLFW_KEY_BACKSPACE, "Reset Mesh", menu_reset_mesh); + add_process(GLFW_KEY_W, "Write Mesh", menu_save_mesh); + add_process(GLFW_KEY_UP, "Zoom In", menu_zoom_in); + add_process(GLFW_KEY_DOWN, "Zoom Out", menu_zoom_out); + add_process(GLFW_KEY_RIGHT, "Bgc Inc", menu_bgc_inc); + add_process(GLFW_KEY_LEFT, "Bgc Dec", menu_bgc_dec); + add_process(GLFW_KEY_1, "Bgc White", menu_bgc_white); + add_process(GLFW_KEY_0, "Bgc Black", menu_bgc_black); } void viewer::init_glsl() @@ -153,7 +157,7 @@ void viewer::menu_process(int value) menu_process(processes[value].function); } -void viewer::add_process(const char & key, const string & name, function_t function) +void viewer::add_process(const int & key, const string & name, function_t function) { if(processes.find(key) == processes.end()) { @@ -190,28 +194,15 @@ void viewer::keyboard(GLFWwindow * window, int key, int scancode, int action, in { if(action == GLFW_RELEASE) return; - viewer * view = (viewer *) glfwGetWindowUserPointer(window); - - switch(key) - { - case GLFW_KEY_UP: - view->cam.zoomIn(); - break; - case GLFW_KEY_DOWN: - view->cam.zoomOut(); - break; - case GLFW_KEY_ESCAPE: - glfwSetWindowShouldClose(window, GLFW_TRUE); - break; - } - - if(key >= '0' && key <= '9') + if(key == GLFW_KEY_ESCAPE) { - view->bgc = (key - '0') / 9.; - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + glfwSetWindowShouldClose(window, GLFW_TRUE); + return; } + viewer * view = (viewer *) glfwGetWindowUserPointer(window); view->menu_process(view->processes[key].function); + glClearColor(view->bgc, view->bgc, view->bgc, 1.); } void viewer::menu_meshes(int value) @@ -296,6 +287,26 @@ void viewer::menu_zoom_out(viewer * view) view->cam.zoomOut(); } +void viewer::menu_bgc_inc(viewer * view) +{ + if(view->bgc < 1) view->bgc += 0.05; +} + +void viewer::menu_bgc_dec(viewer * view) +{ + if(view->bgc > 0) view->bgc -= 0.05; +} + +void viewer::menu_bgc_white(viewer * view) +{ + view->bgc = 1; +} + +void viewer::menu_bgc_black(viewer * view) +{ + view->bgc = 0; +} + void viewer::invert_orientation(viewer * view) { view->mesh().invert_orientation(); From 24b9e8fb6c4d8404a9666c8775d2646fcaefa675 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 14:36:28 +0100 Subject: [PATCH 0056/1018] cam zoom --- include/viewer/viewer.h | 2 +- src/viewer/camera.cpp | 4 ++-- src/viewer/viewer.cpp | 5 ++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index f998fc8e..280ef667 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -152,7 +152,7 @@ class viewer // draw routines void set_gl(); void set_lighting(); - void set_mesh_materia(); + void set_mesh_material(); void draw_scene(); void draw_corr(); void draw_polygons(); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 5028e301..2388e7af 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -124,12 +124,12 @@ void camera::idle() void camera::zoomIn() { - vZoom -= 0.2; + zoom -= 0.01; } void camera::zoomOut() { - vZoom += 0.2; + zoom += 0.01; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 60c65776..1a9314a7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -413,8 +413,7 @@ void viewer::display() glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); - set_mesh_materia(); - + set_mesh_material(); draw_scene(); //glPopAttrib(); @@ -439,7 +438,7 @@ void viewer::set_lighting() glEnable(GL_NORMALIZE); } -void viewer::set_mesh_materia() +void viewer::set_mesh_material() { GLfloat diffuse[4] = { .8, .5, .3, 1. }; GLfloat specular[4] = { .3, .3, .3, 1. }; From 39d71a64744b93ed9895aba7975456ed80fc4006 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 22:55:08 +0100 Subject: [PATCH 0057/1018] scroll callback --- include/viewer/viewer.h | 13 +++++++------ src/viewer/viewer.cpp | 30 ++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 280ef667..e3ccd4e0 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -111,19 +111,20 @@ class viewer private: // init - void debug_info(); - void init_glut(); + void info_gl(); + void init_gl(); void init_menus(); void init_glsl(); void update_vbo(); void display(); - // GLUT callbacks + // callbacks static void idle(); - static void keyboard(GLFWwindow * window, int key, int scancode, int action, int mods); - static void mouse(GLFWwindow * window, int button, int action, int mods); - static void motion(GLFWwindow * window, double x, double y); + static void keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods); + static void mouse_callback(GLFWwindow * window, int button, int action, int mods); + static void cursor_callback(GLFWwindow * window, double x, double y); + static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); // menu functions void menu_meshes(int value); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1a9314a7..8bc12ad7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -40,12 +40,13 @@ viewer::viewer() bgc = 0; - init_glut(); + init_gl(); init_menus(); + set_gl(); init_glsl(); - debug_info(); + info_gl(); } viewer::~viewer() @@ -69,7 +70,7 @@ che_viewer & viewer::mesh() return meshes[current]; } -void viewer::debug_info() +void viewer::info_gl() { GLint major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); @@ -86,7 +87,7 @@ void error_callback(int error, const char* description) fprintf(stderr, "Error %d: %s\n", error, description); } -void viewer::init_glut() +void viewer::init_gl() { glfwSetErrorCallback(error_callback); @@ -105,9 +106,10 @@ void viewer::init_glut() glfwSetWindowUserPointer(window, this); - glfwSetKeyCallback(window, keyboard); - glfwSetMouseButtonCallback(window, mouse); - glfwSetCursorPosCallback(window, motion); + glfwSetKeyCallback(window, keyboard_callback); + glfwSetMouseButtonCallback(window, mouse_callback); + glfwSetCursorPosCallback(window, cursor_callback); + glfwSetScrollCallback(window, scroll_callback); glfwMakeContextCurrent(window); glfwSwapInterval(0); @@ -190,7 +192,7 @@ void viewer::add_mesh(const vector & _meshes) } } -void viewer::keyboard(GLFWwindow * window, int key, int scancode, int action, int mods) +void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods) { if(action == GLFW_RELEASE) return; @@ -212,7 +214,7 @@ void viewer::menu_meshes(int value) glfwSetWindowTitle(window, mesh()->filename().c_str()); } -void viewer::mouse(GLFWwindow* window, int button, int action, int mods) +void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); @@ -227,7 +229,7 @@ void viewer::mouse(GLFWwindow* window, int button, int action, int mods) else view->cam.mouse(button, action, xpos, ypos); } -void viewer::motion(GLFWwindow * window, double x, double y) +void viewer::cursor_callback(GLFWwindow * window, double x, double y) { int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT); if(state == GLFW_PRESS) @@ -237,6 +239,14 @@ void viewer::motion(GLFWwindow * window, double x, double y) } } +void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset) +{ + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + + if(yoffset > 0) view->cam.zoomIn(); + if(yoffset < 0) view->cam.zoomOut(); +} + void viewer::idle() { //cam.idle(); From f9780073ab56a297b9714684cf287517e9f5204f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Dec 2019 23:09:41 +0100 Subject: [PATCH 0058/1018] printing help --- include/viewer/viewer.h | 1 + src/viewer/viewer.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index e3ccd4e0..a030115d 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -131,6 +131,7 @@ class viewer void menu_process(int value); void menu_process(function_t pro); + static void menu_help(viewer * view); static void menu_reset_mesh(viewer * view); static void menu_save_mesh(viewer * view); static void menu_zoom_in(viewer * view); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8bc12ad7..6789f90a 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -121,14 +121,15 @@ void viewer::init_menus() { // init viewer menu sub_menus.push_back("viewer"); - add_process(GLFW_KEY_PERIOD, "Invert Orientation", invert_orientation); + add_process(GLFW_KEY_F1, "Help", menu_help); add_process(GLFW_KEY_F2, "Wireframe", set_render_wireframe); add_process(GLFW_KEY_F3, "Gradient Field", set_render_gradient_field); add_process(GLFW_KEY_F4, "Normal Field", set_render_normal_field); add_process(GLFW_KEY_F5, "Show Border", set_render_border); add_process(GLFW_KEY_SPACE, "Lines", set_render_lines); - add_process('+', "Show Corr", set_render_corr); add_process(GLFW_KEY_TAB, "Flat", set_is_flat); + add_process(GLFW_KEY_PERIOD, "Invert Orientation", invert_orientation); + add_process('+', "Show Corr", set_render_corr); // init mesh menu sub_menus.push_back("Mesh"); @@ -260,6 +261,12 @@ void viewer::menu_process(function_t pro) update_vbo(); } +void viewer::menu_help(viewer * view) +{ + const char * key_name = glfwGetKeyName(GLFW_KEY_W, 0); + gproshan_log(key_name); +} + void viewer::menu_reset_mesh(viewer * view) { view->select_vertices.clear(); From 073668cabbb6c9bb6c37e06d3aece1d381fb7c8c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 10 Dec 2019 10:23:22 +0100 Subject: [PATCH 0059/1018] viewport size, display many meshes --- include/viewer/viewer.h | 2 ++ src/viewer/viewer.cpp | 32 +++++++++++--------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index a030115d..808f5a28 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -71,6 +71,8 @@ class viewer GLFWwindow * window; shader shader_program; camera cam; + int viewport_width; + int viewport_height; che_viewer meshes[N_MESHES]; vcorr_t corr_mesh[N_MESHES]; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 6789f90a..119f9f95 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -431,6 +431,11 @@ void viewer::display() glEnable(GL_LIGHTING); set_mesh_material(); + + glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + viewport_width /= m_window_size[n_meshes - 1][1]; + viewport_height /= m_window_size[n_meshes - 1][0]; + draw_scene(); //glPopAttrib(); @@ -493,12 +498,9 @@ void viewer::draw_polygons() glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1., 1.); - int ww, wh; - glfwGetFramebufferSize(window, &ww, &wh); - for(index_t i = 0; i < n_meshes; i++) { - glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); + glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw(); } @@ -517,12 +519,9 @@ void viewer::draw_wireframe() glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - int ww, wh; - glfwGetFramebufferSize(window, &ww, &wh); - for(index_t i = 0; i < n_meshes; i++) { - glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); + glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw(); } @@ -675,12 +674,9 @@ void viewer::draw_selected_vertices() double h = 0.02 * cam.zoom; - int ww, wh; - glfwGetFramebufferSize(window, &ww, &wh); - for(int v: select_vertices) { - glViewport(mesh().vx * ww, mesh().vy * wh, ww, wh); + glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); glPushMatrix(); glTranslated(mesh()->gt(v).x, mesh()->gt(v).y, mesh()->gt(v).z); @@ -697,12 +693,9 @@ void viewer::draw_normal_field() { shader_program.disable(); - int ww, wh; - glfwGetFramebufferSize(window, &ww, &wh); - for(index_t i = 0; i < n_meshes; i++) { - glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); + glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw_normal_field(); } } @@ -711,12 +704,9 @@ void viewer::draw_gradient_field() { shader_program.disable(); - int ww, wh; - glfwGetFramebufferSize(window, &ww, &wh); - for(index_t i = 0; i < n_meshes; i++) { - glViewport(meshes[i].vx * ww, meshes[i].vy * wh, ww, wh); + glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw_gradient_field(); } } @@ -795,7 +785,7 @@ void draw_str(const char * str, int x, int y, float color[4], void * font) glColor4fv(color); glRasterPos2i(x, y); - //while(*str) glutBitmapCharacter(font, *str++); + //viewport_heightile(*str) glutBitmapCharacter(font, *str++); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); From e68794a7c33eeba85840b1179595271921f9036a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 13 Dec 2019 17:58:32 +0100 Subject: [PATCH 0060/1018] inpaiting --- CMakeLists.txt | 2 +- src/mdict/denoising.cpp | 3 ++- src/mdict/dictionary.cpp | 3 ++- src/mdict/inpainting.cpp | 4 +++- src/mdict/mdict.cpp | 8 +++++--- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23c3a116..9526b96a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,7 @@ target_link_libraries(gproshan_cpp ${GLEW_LIBRARIES}) target_link_libraries(gproshan_cpp ${GLUT_LIBRARIES}) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp ${CGAL_LIBRARIES}) +target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) if(CUDA_FOUND) diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index 6b7118da..5c7e68f2 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -23,7 +23,8 @@ distance_t denoising::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - + draw_patches(128); + phi_basis->plot_basis(); TIC(d_time) distance_t error = mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index b7ad2ae1..fed238de 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 8; +size_t dictionary::L = 20; size_t dictionary::K = 10; size_t dictionary::T = 5; @@ -155,6 +155,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->dim); phi_basis->discrete(p.phi, p.xyz); + p.phi = normalise(p.phi); } /* diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 1c80d9ec..85d3e84e 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -193,8 +193,10 @@ distance_t inpainting::execute() } bool *pmask; + + draw_patches(10); + gproshan_debug_var(alpha.col(463)); - draw_patches(7); TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 2dd3a32e..7abdf987 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -12,7 +12,7 @@ // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -const real_t sigma = 0.001; +const real_t sigma = 0.5; // SPARSE @@ -118,6 +118,8 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L { arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; + gproshan_debug_var(threshold); + a_mat DD; a_vec aa, r = x; @@ -125,6 +127,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L index_t l = 0; while(norm(r) > threshold && l < L) { + gproshan_debug_var(norm(r)); selected_atoms(l) = index_max(abs(D.t() * r)); DD = D.cols(selected_atoms.head(l + 1)); @@ -132,7 +135,6 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L r = x - DD * aa; l++; } - return {aa, selected_atoms.head(l)}; } @@ -144,7 +146,7 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) tie(aa, selected_atoms) = _OMP(x, D, L); alpha.elem(selected_atoms) = aa; - + return alpha; } From 04f735909490396a95c9bf76efd5f464d33d8d2a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 17 Dec 2019 14:29:39 +0100 Subject: [PATCH 0061/1018] Drawing patch separatedly --- include/mdict/basis.h | 2 +- src/mdict/basis.cpp | 10 +++++----- src/mdict/basis_dct.cpp | 1 - src/mdict/denoising.cpp | 2 +- src/mdict/dictionary.cpp | 7 ++++--- src/mdict/inpainting.cpp | 2 +- src/mdict/mdict.cpp | 7 ++----- 7 files changed, 14 insertions(+), 17 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index df107260..7b605f9f 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -27,7 +27,7 @@ class basis virtual void discrete(a_mat & phi, const a_mat & xy) = 0; void plot_basis(); void plot_atoms(const a_mat & A); - void plot_patch(const a_mat & A, const a_mat & xyz ); + void plot_patch(const a_mat & A, const a_mat & xyz, index_t i); size_t get_dim(); private: diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 431f6d12..ea7f309e 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -72,9 +72,9 @@ size_t basis::get_dim() return dim; } -void basis::plot_patch(const a_mat & A, const a_mat & xyz ) +void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) { - string data = tmp_file_path("xyz.dat"); + string data = tmp_file_path("xyz_" + to_string(i) + ".dat"); a_mat tmp = xyz.t(); tmp.save(data.c_str(),arma::arma_ascii); @@ -83,7 +83,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz ) size_t s = sqrt(m); s += !(s * s == K); - string file = tmp_file_path("atoms.gpi"); + string file = tmp_file_path("atoms_patch_"+ to_string(i) + ".gpi"); ofstream os(file); os << "set term qt size 1000,1000;" << endl; @@ -108,9 +108,9 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz ) os.close(); - file = "gnuplot -persist " + file + " &"; + //file = "gnuplot -persist " + file + " &"; - system(file.c_str()); + //system(file.c_str()); } diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index a2f4745c..8bcd94d7 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -25,7 +25,6 @@ void basis_dct::discrete(a_mat & phi, const a_mat & xy) for(index_t ny = 0; ny < n; ny++, k++) { phi.col(k) = dct(x, y, nx, ny); -// gproshan_debug_var(k); } } diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index 5c7e68f2..f1f5e548 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -24,7 +24,7 @@ distance_t denoising::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); draw_patches(128); - phi_basis->plot_basis(); + phi_basis->plot_atoms(A); TIC(d_time) distance_t error = mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index fed238de..d9540917 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan::mdict { -size_t dictionary::L = 20; +size_t dictionary::L = 12; size_t dictionary::K = 10; size_t dictionary::T = 5; @@ -51,12 +51,13 @@ void dictionary::learning() KSVD(A, patches, L, K); A.save(f_dict); } + gproshan_debug(Dicti); + gproshan_debug_var(A); } else A.eye(phi_basis->dim, m); assert(A.n_rows == phi_basis->dim); assert(A.n_cols == m); - if(d_plot) { phi_basis->plot_basis(); @@ -252,7 +253,7 @@ const distance_t & dictionary::operator[](const index_t & i) const void dictionary::draw_patches(index_t i) { - phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz); + phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz, i); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 85d3e84e..934c8449 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -195,7 +195,7 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); - gproshan_debug_var(alpha.col(463)); + //gproshan_debug_var(alpha.col(463)); TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 7abdf987..e15435a6 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -12,7 +12,7 @@ // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -const real_t sigma = 0.5; +const real_t sigma = 0.01; // SPARSE @@ -117,9 +117,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; - gproshan_debug_var(threshold); - + real_t threshold = norm(x) * sigma; a_mat DD; a_vec aa, r = x; @@ -127,7 +125,6 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L index_t l = 0; while(norm(r) > threshold && l < L) { - gproshan_debug_var(norm(r)); selected_atoms(l) = index_max(abs(D.t() * r)); DD = D.cols(selected_atoms.head(l + 1)); From 2073b9515e28262c85127120312a4fc703abed64 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 18 Dec 2019 13:00:17 +0100 Subject: [PATCH 0062/1018] key input - bindings --- include/app_viewer.h | 2 +- shaders/fragment.glsl | 163 ++++++++++++++++++++++++++------------ shaders/new_fragment.glsl | 112 -------------------------- shaders/new_geometry.glsl | 25 ------ shaders/new_vertex.glsl | 21 ----- shaders/vertex.glsl | 32 +++++--- src/app_viewer.cpp | 74 ++++++++--------- src/viewer/shader.cpp | 6 +- src/viewer/viewer.cpp | 46 +++++------ 9 files changed, 194 insertions(+), 287 deletions(-) mode change 100755 => 100644 shaders/fragment.glsl delete mode 100644 shaders/new_fragment.glsl delete mode 100644 shaders/new_geometry.glsl delete mode 100644 shaders/new_vertex.glsl mode change 100755 => 100644 shaders/vertex.glsl diff --git a/include/app_viewer.h b/include/app_viewer.h index ac1f32ab..9f27a292 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -80,7 +80,7 @@ class app_viewer : public viewer static void process_poisson_laplacian_2(viewer * p_view); static void process_poisson_laplacian_3(viewer * p_view); - static void process_thresold(viewer * p_view); + static void process_threshold(viewer * p_view); static void process_noise(viewer * p_view); static void process_black_noise(viewer * p_view); static void process_multiplicate_vertices(viewer * p_view); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl old mode 100755 new mode 100644 index f8b19b74..be591d16 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,51 +1,112 @@ -uniform vec3 eye; -uniform vec3 light; -varying vec3 position; -varying vec3 normal; - -float diffuse( vec3 N, vec3 L ) -{ - return max( 0., dot( N, L )); -} - -float specular( vec3 N, vec3 L, vec3 E ) -{ - const float shininess = 8.; - vec3 R = 2.*dot(L,N)*N - L; - return pow( max( 0., dot( R, E )), shininess ); -} - -float fresnel( vec3 N, vec3 E ) -{ - const float sharpness = 10.; - float NE = max( 0., dot( N, E )); - return pow( sqrt( 1. - NE*NE ), sharpness ); -} - -void main() -{ - // color - float d = 1. - gl_Color.r; - float r = (1. - d * d) * .8; - float g = (1. - (2. * (d - .5)) * (2. * (d - .5))) * .7; - float b = (1. - (1. - d) * (1. - d)); - vec3 color = vec3(r, g, b); - - // lines - float h = gl_Color.r; - h = h * 40.; - h = h - floor( h ); - h = (1. / (1. + exp(-100.*(h - .55)))) + (1. / (1. + exp(-100.*(-h + .45)))); - h = 1. - h; - color.xyz = vec3(h, h, h) + (1. - h) * color.xyz; - - vec3 N = normalize( normal ); - vec3 L = normalize( light - position ); - vec3 E = normalize( eye - position ); - vec3 R = 2.*dot(L,N)*N - L; - vec3 one = vec3( 1., 1., 1. ); - - gl_FragColor.rgb = diffuse(N,L)*color + .5*specular(N,L,E)*one + .5*fresnel(N,E)*one; - gl_FragColor.a = 1.; -} - +#version 460 core + +uniform vec3 eye; +uniform vec3 light; +uniform bool is_flat; +uniform bool lines; + +in vec3 position; +in vec3 normal; +in float color; + +layout(location = 0) out vec4 FragColor; + +float diffuse( vec3 N, vec3 L ) +{ + return max( 0., dot( N, L )); +} + +float specular( vec3 N, vec3 L, vec3 E ) +{ + const float shininess = 4.; + vec3 R = 2.*dot(L,N)*N - L; + return pow( max( 0., dot( R, E )), shininess ); +} + +float fresnel( vec3 N, vec3 E ) +{ + const float sharpness = 10.; + float NE = max( 0., dot( N, E )); + return pow( sqrt( 1. - NE*NE ), sharpness ); +} + +//https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag +float colormap_red(float x) +{ + if (x < 0.7520372909206926) + return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; + else + return -1.20830453148990E+01 * x + 1.44337397593436E+01; +} + +float colormap_green(float x) +{ + if (x < 0.7485333535031721) + return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; + else + return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; +} + +float colormap_blue(float x) +{ + if (x < 0.7628468501376879) + return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; + else + return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; +} + +vec3 colormap(float x) +{ + float r = clamp(colormap_red(x) / 255.0, .0, .9); + float g = clamp(colormap_green(x) / 255.0, .0, .9); + float b = clamp(colormap_blue(x) / 255.0, .0, .9); + return vec3(r, g, b); +} + +void main() +{ + // color + /* + float d = 1. - color; + float r = (1. - d*d) * .8; + float g = (1. - (2. * (d - .5)) * (2. * (d - .5))) * .7; + float b = (1. - (1. - d) * (1. - d)); + vec3 vcolor = vec3(r, g, b); + */ + vec3 vcolor = colormap(color); + + + // lines + if(lines) + { + float h = color; + h = h * 40.; + h = h - floor( h ); + h = (1. / (1. + exp(-100.*(h - .55)))) + (1. / (1. + exp(-100.*(-h + .45)))); + h = 1. - h; + vcolor.xyz = vec3(0, 0, 0) + (1. - h) * vcolor.xyz; + } + + vec3 N; + + if(is_flat) + { + vec3 X = dFdx(position); + vec3 Y = dFdy(position); + vec3 normal_flat = cross(X,Y); + N = normalize( normal_flat ); + } + else + { + N = normalize( normal ); + } + + vec3 L = normalize( light - position ); + vec3 E = normalize( eye - position ); + vec3 R = 2.*dot(L,N)*N - L; + vec3 one = vec3( 1., 1., 1. ); + + FragColor.rgb = diffuse(N,L) * vcolor + .1 * specular(N,L,E) * one + .5 * fresnel(N,E) * one; + FragColor.a = 1.; +} + diff --git a/shaders/new_fragment.glsl b/shaders/new_fragment.glsl deleted file mode 100644 index b4b123ea..00000000 --- a/shaders/new_fragment.glsl +++ /dev/null @@ -1,112 +0,0 @@ -#version 330 core - -uniform vec3 eye; -uniform vec3 light; -uniform bool is_flat; -uniform bool lines; - -in vec3 position; -in vec3 normal; -in float color; - -layout(location = 0) out vec4 FragColor; - -float diffuse( vec3 N, vec3 L ) -{ - return max( 0., dot( N, L )); -} - -float specular( vec3 N, vec3 L, vec3 E ) -{ - const float shininess = 4.; - vec3 R = 2.*dot(L,N)*N - L; - return pow( max( 0., dot( R, E )), shininess ); -} - -float fresnel( vec3 N, vec3 E ) -{ - const float sharpness = 10.; - float NE = max( 0., dot( N, E )); - return pow( sqrt( 1. - NE*NE ), sharpness ); -} - -//https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag -float colormap_red(float x) -{ - if (x < 0.7520372909206926) - return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; - else - return -1.20830453148990E+01 * x + 1.44337397593436E+01; -} - -float colormap_green(float x) -{ - if (x < 0.7485333535031721) - return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; - else - return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; -} - -float colormap_blue(float x) -{ - if (x < 0.7628468501376879) - return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; - else - return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; -} - -vec3 colormap(float x) -{ - float r = clamp(colormap_red(x) / 255.0, .0, .9); - float g = clamp(colormap_green(x) / 255.0, .0, .9); - float b = clamp(colormap_blue(x) / 255.0, .0, .9); - return vec3(r, g, b); -} - -void main() -{ - // color - /* - float d = 1. - color; - float r = (1. - d*d) * .8; - float g = (1. - (2. * (d - .5)) * (2. * (d - .5))) * .7; - float b = (1. - (1. - d) * (1. - d)); - vec3 vcolor = vec3(r, g, b); - */ - vec3 vcolor = colormap(color); - - - // lines - if(lines) - { - float h = color; - h = h * 40.; - h = h - floor( h ); - h = (1. / (1. + exp(-100.*(h - .55)))) + (1. / (1. + exp(-100.*(-h + .45)))); - h = 1. - h; - vcolor.xyz = vec3(0, 0, 0) + (1. - h) * vcolor.xyz; - } - - vec3 N; - - if(is_flat) - { - vec3 X = dFdx(position); - vec3 Y = dFdy(position); - vec3 normal_flat = cross(X,Y); - N = normalize( normal_flat ); - } - else - { - N = normalize( normal ); - } - - vec3 L = normalize( light - position ); - vec3 E = normalize( eye - position ); - vec3 R = 2.*dot(L,N)*N - L; - vec3 one = vec3( 1., 1., 1. ); - - FragColor.rgb = diffuse(N,L) * vcolor + .1 * specular(N,L,E) * one + .5 * fresnel(N,E) * one; - FragColor.a = 1.; -} - diff --git a/shaders/new_geometry.glsl b/shaders/new_geometry.glsl deleted file mode 100644 index 213ff769..00000000 --- a/shaders/new_geometry.glsl +++ /dev/null @@ -1,25 +0,0 @@ -#version 450 core - -layout (triangles) in; -layout (triangle_strip, max_vertices = 3) out; -uniform mat4 projection; //from application program. -out vec3 normal; //to fragment shader. - -void main (void) { - vec3 vector1; - vec3 vector2; - - gl_Position = gl_in[0].gl_Position; - vector1 = gl_in[1].gl_Position.xyz - gl_Position.xyz; - vector2 = gl_in[2].gl_Position.xyz - gl_Position.xyz; - normal = normalize (cross (vector1, vector2)); - gl_Position = projection * gl_Position; - EmitVertex (); - - gl_Position = projection * gl_in[1].gl_Position; - EmitVertex (); - - gl_Position = projection * gl_in[2].gl_Position; - EmitVertex (); - EndPrimitive (); -} diff --git a/shaders/new_vertex.glsl b/shaders/new_vertex.glsl deleted file mode 100644 index f75cb92d..00000000 --- a/shaders/new_vertex.glsl +++ /dev/null @@ -1,21 +0,0 @@ -#version 330 core - -layout (location=0) in vec3 VertexPosition; -layout (location=1) in vec3 VertexNormal; -layout (location=2) in float VertexColor; - -out vec3 position; -out vec3 normal; -out float color; - -uniform mat4 ModelViewMatrix; -uniform mat4 ProjectionMatrix; - -void main() -{ - position = VertexPosition; - normal = VertexNormal; - color = VertexColor; - gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(VertexPosition, 1.); -} - diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl old mode 100755 new mode 100644 index 6fbfbd86..3307f05e --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,11 +1,21 @@ -varying vec3 position; -varying vec3 normal; - -void main() -{ - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; - gl_FrontColor = gl_Color; - - position = gl_Vertex.xyz; - normal = gl_Normal.xyz; -} +#version 460 core + +layout (location=0) in vec3 VertexPosition; +layout (location=1) in vec3 VertexNormal; +layout (location=2) in float VertexColor; + +out vec3 position; +out vec3 normal; +out float color; + +uniform mat4 ModelViewMatrix; +uniform mat4 ProjectionMatrix; + +void main() +{ + position = VertexPosition; + normal = VertexNormal; + color = VertexColor; + gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(VertexPosition, 1.); +} + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 92da8f3c..f701868a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -60,58 +60,58 @@ int app_viewer::main(int nargs, const char ** args) add_mesh(meshes); sub_menus.push_back("Fairing"); - add_process('T', "Fairing Taubin", process_fairing_taubin); - add_process('E', "Fairing Spectral", process_fairing_spectral); + add_process(GLFW_KEY_T, "[T] Fairing Taubin", process_fairing_taubin); + add_process(GLFW_KEY_E, "[E] Fairing Spectral", process_fairing_spectral); sub_menus.push_back("Geodesics"); - add_process('F', "Geodesics (FM)", process_geodesics_fm); - add_process('U', "Geodesics (PTP_CPU)", process_geodesics_ptp_cpu); + add_process(GLFW_KEY_F, "[F] Fast Marching", process_geodesics_fm); + add_process(GLFW_KEY_U, "[C] Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu); #ifndef SINGLE_P - add_process('l', "Geodesics (HEAT_FLOW)", process_geodesics_heat_flow); + add_process(GLFW_KEY_L, "[L] Heat Method", process_geodesics_heat_flow); #endif #ifdef GPROSHAN_CUDA - add_process('G', "Geodesics (PTP_GPU)", process_geodesics_ptp_gpu); - add_process('L', "Geodesics (HEAT_FLOW_GPU)", process_geodesics_heat_flow_gpu); + add_process(GLFW_KEY_G, "[G] Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu); +// add_process('L', "Geodesics (HEAT_FLOW_GPU)", process_geodesics_heat_flow_gpu); #endif // GPROSHAN_CUDA - add_process('S', "Farthest Point Sampling", process_farthest_point_sampling); - add_process('Q', "Farthest Point Sampling radio", process_farthest_point_sampling_radio); - add_process('V', "Voronoi Regions", process_voronoi); - add_process('P', "Toplesets", compute_toplesets); + add_process(GLFW_KEY_S, "[S] Geodesic Farthest Point Sampling", process_farthest_point_sampling); + add_process(GLFW_KEY_R, "[R] Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio); + add_process(GLFW_KEY_V, "[V] Geodesic Voronoi", process_voronoi); + add_process(GLFW_KEY_P, "[P] Toplesets", compute_toplesets); sub_menus.push_back("Dictionary Learning"); - add_process('.', "Mark patch", process_mdict_patch); - add_process('D', "Denoising", process_denoising); - add_process('R', "Super Resolution", process_super_resolution); - add_process('I', "Inpainting", process_inpaiting); - add_process('A', "IT Inpainting", process_iterative_inpaiting); + add_process(GLFW_KEY_J, "[J] MDICT Patch", process_mdict_patch); + add_process(GLFW_KEY_D, "[D] MDICT Denoising", process_denoising); + add_process(GLFW_KEY_A, "[A] MDICT Super Resolution", process_super_resolution); + add_process(GLFW_KEY_I, "[I] MDICT Inpaiting", process_inpaiting); +// add_process('A', "IT Inpainting", process_iterative_inpaiting); sub_menus.push_back("Signatures"); - add_process('s', "GPS (norm)", process_gps); - add_process('H', "HKS (norm)", process_hks); - add_process('W', "WKS (norm)", process_wks); - add_process('X', "Functional maps", process_functional_maps); - add_process('*', "Key Points (adaptive mesh)", process_key_points); - add_process('C', "Key Components", process_key_components); + add_process(GLFW_KEY_2, "[2] GPS", process_gps); + add_process(GLFW_KEY_3, "[3] HKS", process_hks); + add_process(GLFW_KEY_4, "[4] WKS", process_wks); + add_process(GLFW_KEY_5, "[5] Functional Maps", process_functional_maps); + add_process(GLFW_KEY_6, "[6] Key Points", process_key_points); + add_process(GLFW_KEY_7, "[7] Key Components", process_key_components); sub_menus.push_back("Repair Holes"); - add_process('o', "Membrane surface", process_poisson_laplacian_1); - add_process('p', "Thin-plate surface", process_poisson_laplacian_2); - add_process('q', "Minimum variation surface", process_poisson_laplacian_3); - add_process('h', "Fill Holes (mesh only)", process_fill_holes); - add_process('B', "Fill holes (biharmonic splines)", process_fill_holes_biharmonic_splines); + add_process(GLFW_KEY_X, "[X] Poisson Membrane surface", process_poisson_laplacian_1); + add_process(GLFW_KEY_Y, "[Y] Poisson Thin-plate surface", process_poisson_laplacian_2); + add_process(GLFW_KEY_Z, "[Z] Poisson Minimum variation surface", process_poisson_laplacian_3); + add_process(GLFW_KEY_H, "[H] Fill hole - planar mesh", process_fill_holes); + add_process(GLFW_KEY_B, "[B] Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines); sub_menus.push_back("Others"); - add_process('t', "Threshold", process_thresold); - add_process('N', "Noise", process_noise); - add_process('M', "Black Noise", process_black_noise); - add_process('m', "Multiplicate Vertices", process_multiplicate_vertices); - add_process('-', "Make holes", process_delete_vertices); - add_process('d', "Delete non manifolds vertices", process_delete_non_manifold_vertices); - add_process('K', "Gaussian curvature", process_gaussian_curvature); - add_process('/', "Decimation", process_edge_collapse); - add_process(':', "Select multiple vertices", select_multiple); + add_process(GLFW_KEY_SLASH, "[SLASH] Threshold", process_threshold); + add_process(GLFW_KEY_N, "[N] Noise", process_noise); + add_process(GLFW_KEY_COMMA, "[COMMA] Black noise", process_black_noise); + add_process(GLFW_KEY_M, "[M] Multiplicate", process_multiplicate_vertices); + add_process(GLFW_KEY_PERIOD, "[PERIOD] Delete vertices", process_delete_vertices); + add_process(GLFW_KEY_MINUS, "[MINUS] Delete non-manifold vertices", process_delete_non_manifold_vertices); + add_process(GLFW_KEY_K, "[K] Gaussian curvature", process_gaussian_curvature); + add_process(GLFW_KEY_9, "[9] Edge Collapse", process_edge_collapse); + add_process(GLFW_KEY_SEMICOLON, "[SEMICOLON] Select multiple vertices", select_multiple); run(); @@ -241,7 +241,7 @@ void app_viewer::process_black_noise(viewer * p_view) mesh.update_normals(); } -void app_viewer::process_thresold(viewer * p_view) +void app_viewer::process_threshold(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index bfa985a8..e51ced12 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -28,11 +28,7 @@ void shader::load_fragment(const char * filename) void shader::load_geometry(const char * filename) { - #ifdef GL_GEOMETRY_SHADER_EXT - load(GL_GEOMETRY_SHADER_EXT, filename); - #else - cerr << "Error: geometry shaders not supported!" << endl; - #endif + load(GL_GEOMETRY_SHADER_EXT, filename); } void shader::enable() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 119f9f95..9e6bd9a8 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -121,32 +121,32 @@ void viewer::init_menus() { // init viewer menu sub_menus.push_back("viewer"); - add_process(GLFW_KEY_F1, "Help", menu_help); - add_process(GLFW_KEY_F2, "Wireframe", set_render_wireframe); - add_process(GLFW_KEY_F3, "Gradient Field", set_render_gradient_field); - add_process(GLFW_KEY_F4, "Normal Field", set_render_normal_field); - add_process(GLFW_KEY_F5, "Show Border", set_render_border); - add_process(GLFW_KEY_SPACE, "Lines", set_render_lines); - add_process(GLFW_KEY_TAB, "Flat", set_is_flat); - add_process(GLFW_KEY_PERIOD, "Invert Orientation", invert_orientation); - add_process('+', "Show Corr", set_render_corr); + add_process(GLFW_KEY_F1, "[F1] Help", menu_help); + add_process(GLFW_KEY_F2, "[F2] Invert Orintation", invert_orientation); + add_process(GLFW_KEY_F3, "[F3] Render Wireframe", set_render_wireframe); + add_process(GLFW_KEY_F4, "[F4] Gradient Field", set_render_gradient_field); + add_process(GLFW_KEY_F5, "[F5] Normal Field", set_render_normal_field); + add_process(GLFW_KEY_F6, "[F6] Show Borders", set_render_border); + add_process(GLFW_KEY_SPACE, "[SPACE] Level Curves", set_render_lines); + add_process(GLFW_KEY_TAB, "[TAB] Render Flat", set_is_flat); +// add_process('+', "Show Corr", set_render_corr); // init mesh menu sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, "Reset Mesh", menu_reset_mesh); - add_process(GLFW_KEY_W, "Write Mesh", menu_save_mesh); - add_process(GLFW_KEY_UP, "Zoom In", menu_zoom_in); - add_process(GLFW_KEY_DOWN, "Zoom Out", menu_zoom_out); - add_process(GLFW_KEY_RIGHT, "Bgc Inc", menu_bgc_inc); - add_process(GLFW_KEY_LEFT, "Bgc Dec", menu_bgc_dec); - add_process(GLFW_KEY_1, "Bgc White", menu_bgc_white); - add_process(GLFW_KEY_0, "Bgc Black", menu_bgc_black); + add_process(GLFW_KEY_BACKSPACE, "[BACKSPACE] Reload/Reset", menu_reset_mesh); + add_process(GLFW_KEY_W, "[W] Save Mesh", menu_save_mesh); + add_process(GLFW_KEY_UP, "[UP] Zoom in", menu_zoom_in); + add_process(GLFW_KEY_DOWN, "[DOWN] Zoom out", menu_zoom_out); + add_process(GLFW_KEY_RIGHT, "[RIGHT] Background color inc", menu_bgc_inc); + add_process(GLFW_KEY_LEFT, "[LEFT] Background color dec", menu_bgc_dec); + add_process(GLFW_KEY_1, "[1] Background color white", menu_bgc_white); + add_process(GLFW_KEY_0, "[0] Background color black", menu_bgc_black); } void viewer::init_glsl() { - shader_program.load_vertex("../shaders/new_vertex.glsl"); - shader_program.load_fragment("../shaders/new_fragment.glsl"); + shader_program.load_vertex("../shaders/vertex.glsl"); + shader_program.load_fragment("../shaders/fragment.glsl"); } void viewer::update_vbo() @@ -224,9 +224,6 @@ void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); - - //else if(button == 6 || button == 4) cam.zoomIn(); - //else if(button == 5 || button == 3) cam.zoomOut(); else view->cam.mouse(button, action, xpos, ypos); } @@ -263,8 +260,9 @@ void viewer::menu_process(function_t pro) void viewer::menu_help(viewer * view) { - const char * key_name = glfwGetKeyName(GLFW_KEY_W, 0); - gproshan_log(key_name); + for(auto & p: view->processes) + if(p.second.name_function != "") + fprintf(stderr, "%s\n", p.second.name_function.c_str()); } void viewer::menu_reset_mesh(viewer * view) From 018daa8e907ea911f226d6267f98e7120309b40d Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 18 Dec 2019 13:38:58 +0100 Subject: [PATCH 0063/1018] Normalizing basis dictionary --- src/mdict/denoising.cpp | 1 + src/mdict/dictionary.cpp | 8 ++++++-- src/mdict/mdict.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index f1f5e548..dfef4b5c 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -25,6 +25,7 @@ distance_t denoising::execute() gproshan_debug_var(d_time); draw_patches(128); phi_basis->plot_atoms(A); + phi_basis->plot_basis(); TIC(d_time) distance_t error = mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index d9540917..f71742fc 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -47,7 +47,11 @@ void dictionary::learning() if(!A.load(f_dict)) { - A.randu(phi_basis->dim, m); + A.eye(phi_basis->dim, m); + A = normalise(A); + gproshan_debug_var(phi_basis->dim); + gproshan_debug_var(m); + phi_basis->plot_atoms(A); KSVD(A, patches, L, K); A.save(f_dict); } @@ -156,7 +160,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->dim); phi_basis->discrete(p.phi, p.xyz); - p.phi = normalise(p.phi); + //p.phi = normalise(p.phi); } /* diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index e15435a6..cbc1246a 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -190,7 +190,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { - return OMP(p.xyz.row(2).t(), p.phi * A, L); + return OMP(p.xyz.row(2).t(), normalise(p.phi * A), L); } a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) From d5b0c0a632f156fece8ca49d46aed9e2f28fed98 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 18 Dec 2019 15:27:05 +0100 Subject: [PATCH 0064/1018] adding imgui main menus --- CMakeLists.txt | 3 +- imgui/LICENSE.txt | 21 + imgui/README.md | 274 + imgui/imconfig.h | 93 + imgui/imgui.cpp | 9948 ++++++++++++++++++++++++++++++++++ imgui/imgui.h | 2241 ++++++++ imgui/imgui_demo.cpp | 4814 ++++++++++++++++ imgui/imgui_draw.cpp | 3367 ++++++++++++ imgui/imgui_impl_glfw.cpp | 332 ++ imgui/imgui_impl_glfw.h | 33 + imgui/imgui_impl_opengl3.cpp | 653 +++ imgui/imgui_impl_opengl3.h | 64 + imgui/imgui_internal.h | 1759 ++++++ imgui/imgui_widgets.cpp | 7613 ++++++++++++++++++++++++++ imgui/imstb_rectpack.h | 639 +++ imgui/imstb_textedit.h | 1417 +++++ imgui/imstb_truetype.h | 4903 +++++++++++++++++ include/viewer/viewer.h | 1 + src/viewer/viewer.cpp | 56 +- 19 files changed, 38225 insertions(+), 6 deletions(-) create mode 100644 imgui/LICENSE.txt create mode 100644 imgui/README.md create mode 100644 imgui/imconfig.h create mode 100644 imgui/imgui.cpp create mode 100644 imgui/imgui.h create mode 100644 imgui/imgui_demo.cpp create mode 100644 imgui/imgui_draw.cpp create mode 100644 imgui/imgui_impl_glfw.cpp create mode 100644 imgui/imgui_impl_glfw.h create mode 100644 imgui/imgui_impl_opengl3.cpp create mode 100644 imgui/imgui_impl_opengl3.h create mode 100644 imgui/imgui_internal.h create mode 100644 imgui/imgui_widgets.cpp create mode 100644 imgui/imstb_rectpack.h create mode 100644 imgui/imstb_textedit.h create mode 100644 imgui/imstb_truetype.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 33e39478..74e5a19e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,8 +50,9 @@ include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/include/viewer) include_directories(${gproshan_SOURCE_DIR}/include/mdict) include_directories(${gproshan_SOURCE_DIR}/include/cuda) +include_directories(${gproshan_SOURCE_DIR}/imgui) -FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) +FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(gproshan_cpp STATIC ${cpp_sources}) target_link_libraries(gproshan_cpp ${OpenMP_CXX_LIBRARIES}) diff --git a/imgui/LICENSE.txt b/imgui/LICENSE.txt new file mode 100644 index 00000000..3b439aa4 --- /dev/null +++ b/imgui/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014-2019 Omar Cornut + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/imgui/README.md b/imgui/README.md new file mode 100644 index 00000000..1991b954 --- /dev/null +++ b/imgui/README.md @@ -0,0 +1,274 @@ +dear imgui +===== +[![Build Status](https://api.travis-ci.com/ocornut/imgui.svg?branch=master)](https://travis-ci.com/ocornut/imgui) +[![Coverity Status](https://scan.coverity.com/projects/4720/badge.svg)](https://scan.coverity.com/projects/4720) + +(This library is available under a free and permissive license, but needs financial support to sustain its continued improvements. In addition to maintenance and stability there are many desirable features yet to be added. If your company is using dear imgui, please consider reaching out. If you are an individual using dear imgui, please consider supporting the project via Patreon or PayPal.) + +Businesses: support continued development via invoiced technical support, maintenance, sponsoring contracts: +
  _E-mail: contact @ dearimgui dot org_ + +Individuals/hobbyists: support continued maintenance and development via the monthly Patreon: +
  [![Patreon](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/patreon_02.png)](http://www.patreon.com/imgui) + +Individuals/hobbyists: support continued maintenance and development via PayPal: +
  [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WGHNC6MBFLZ2S) + +---- + +Dear ImGui is a **bloat-free graphical user interface library for C++**. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline enabled application. It is fast, portable, renderer agnostic and self-contained (no external dependencies). + +Dear ImGui is designed to **enable fast iterations** and to **empower programmers** to create **content creation tools and visualization / debug tools** (as opposed to UI for the average end-user). It favors simplicity and productivity toward this goal, and lacks certain features normally found in more high-level libraries. + +Dear ImGui is particularly suited to integration in games engine (for tooling), real-time 3D applications, fullscreen applications, embedded applications, or any applications on consoles platforms where operating system features are non-standard. + +| [Usage](#usage) - [How it works](#how-it-works) - [Demo](#demo) - [Integration](#integration) | +:----------------------------------------------------------: | +| [Upcoming changes](#upcoming-changes) - [Gallery](#gallery) - [Support, FAQ](#support-frequently-asked-questions-faq) - [How to help](#how-to-help) - [Sponsors](#sponsors) - [Credits](#credits) - [License](#license) | +| [Wiki](https://github.com/ocornut/imgui/wiki) - [Language & frameworks bindings](https://github.com/ocornut/imgui/wiki/Bindings) - [Software using Dear ImGui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) - [User quotes](https://github.com/ocornut/imgui/wiki/Quotes) | + +### Usage + +Dear ImGui is self-contained within a few files that you can easily copy and compile into your application/engine: +- imgui.cpp +- imgui.h +- imgui_demo.cpp +- imgui_draw.cpp +- imgui_widgets.cpp +- imgui_internal.h +- imconfig.h (empty by default, user-editable) +- imstb_rectpack.h +- imstb_textedit.h +- imstb_truetype.h + +No specific build process is required. You can add the .cpp files to your project or #include them from an existing file. + +Backends for a variety of graphics api and rendering platforms along with example applications are provided in the [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder. + +The backend passes mouse/keyboard/gamepad inputs and variety of settings to Dear ImGui, and is in charge of rendering the resulting vertices. After Dear ImGui is setup in your application, you can use it from \_anywhere\_ in your program loop: + +Code: +```cp +ImGui::Text("Hello, world %d", 123); +if (ImGui::Button("Save")) + MySaveFunction(); +ImGui::InputText("string", buf, IM_ARRAYSIZE(buf)); +ImGui::SliderFloat("float", &f, 0.0f, 1.0f); +``` +Result: +
![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_02.png) +
_(settings: Dark style (left), Light style (right) / Font: Roboto-Medium, 16px / Rounding: 5)_ + +Code: +```cpp +// Create a window called "My First Tool", with a menu bar. +ImGui::Begin("My First Tool", &my_tool_active, ImGuiWindowFlags_MenuBar); +if (ImGui::BeginMenuBar()) +{ + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */ } + if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */ } + if (ImGui::MenuItem("Close", "Ctrl+W")) { my_tool_active = false; } + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); +} + +// Edit a color (stored as ~4 floats) +ImGui::ColorEdit4("Color", my_color); + +// Plot some values +const float my_values[] = { 0.2f, 0.1f, 1.0f, 0.5f, 0.9f, 2.2f }; +ImGui::PlotLines("Frame Times", my_values, IM_ARRAYSIZE(my_values)); + +// Display contents in a scrolling region +ImGui::TextColored(ImVec4(1,1,0,1), "Important Stuff"); +ImGui::BeginChild("Scrolling"); +for (int n = 0; n < 50; n++) + ImGui::Text("%04d: Some text", n); +ImGui::EndChild(); +ImGui::End(); +``` +Result: +
![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_03_color.gif) + +Dear ImGui allows you **create elaborate tools** as well as very short-lived ones. On the extreme side of short-liveness: using the Edit&Continue (hot code reload) feature of modern compilers you can add a few widgets to tweaks variables while your application is running, and remove the code a minute later! Dear ImGui is not just for tweaking values. You can use it to trace a running algorithm by just emitting text commands. You can use it along with your own reflection data to browse your dataset live. You can use it to expose the internals of a subsystem in your engine, to create a logger, an inspection tool, a profiler, a debugger, an entire game making editor/framework, etc. + +### How it works + +Check out the Wiki's [About the IMGUI paradigm](https://github.com/ocornut/imgui/wiki#About-the-IMGUI-paradigm) section if you want to understand the core principles behind the IMGUI paradigm. An IMGUI tries to minimize superfluous state duplication, state synchronization and state retention from the user's point of view. It is less error prone (less code and less bugs) than traditional retained-mode interfaces, and lends itself to create dynamic user interfaces. + +Dear ImGui outputs vertex buffers and command lists that you can easily render in your application. The number of draw calls and state changes required to render them is fairly small. Because Dear ImGui doesn't know or touch graphics state directly, you can call its functions anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate dear imgui with your existing codebase. + +_A common misunderstanding is to mistake immediate mode gui for immediate mode rendering, which usually implies hammering your driver/GPU with a bunch of inefficient draw calls and state changes as the gui functions are called. This is NOT what Dear ImGui does. Dear ImGui outputs vertex buffers and a small list of draw calls batches. It never touches your GPU directly. The draw call batches are decently optimal and you can render them later, in your app or even remotely._ + +### Demo + +Calling the `ImGui::ShowDemoWindow()` function will create a demo window showcasing variety of features and examples. The code is always available for reference in `imgui_demo.cpp`. + +![screenshot demo](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/v167-misc.png) + +You should be able to build the examples from sources (tested on Windows/Mac/Linux). If you don't, let me know! If you want to have a quick look at some Dear ImGui features, you can download Windows binaries of the demo app here: +- [imgui-demo-binaries-20190715.zip](http://www.dearimgui.org/binaries/imgui-demo-binaries-20190715.zip) (Windows binaries, 1.72 WIP, built 2019/07/15, master branch, 5 executables) + +The demo applications are not DPI aware so expect some blurriness on a 4K screen. For DPI awareness in your application, you can load/reload your font at different scale, and scale your style with `style.ScaleAllSizes()`. + +### Integration + +On most platforms and when using C++, **you should be able to use a combination of the [imgui_impl_xxxx](https://github.com/ocornut/imgui/tree/master/examples) files without modification** (e.g. `imgui_impl_win32.cpp` + `imgui_impl_dx11.cpp`). If your engine supports multiple platforms, consider using more of the imgui_impl_xxxx files instead of rewriting them: this will be less work for you and you can get Dear ImGui running immediately. You can _later_ decide to rewrite a custom binding using your custom engine functions if you wish so. + +Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/keyboard/gamepad inputs 2) uploading one texture to your GPU/render engine 3) providing a render function that can bind textures and render textured triangles. The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications doing just that. If you are an experienced programmer at ease with those concepts, it should take you less than two hours to integrate Dear ImGui in your custom engine. **Make sure to spend time reading the [FAQ](https://github.com/ocornut/imgui/wiki/FAQ), comments, and one of the examples/ application!** + +Officially maintained bindings (in repository): +- Renderers: DirectX9, DirectX10, DirectX11, DirectX12, OpenGL (legacy), OpenGL3/ES/ES2 (modern), Vulkan, Metal. +- Platforms: GLFW, SDL2, Win32, Glut, OSX. +- Frameworks: Emscripten, Allegro5, Marmalade. + +Third-party bindings (see [Bindings](https://github.com/ocornut/imgui/wiki/Bindings/) page): +- Languages: C, C#/.Net, ChaiScript, D, Go, Haxe/hxcpp, Java, JavaScript, Julia, Lua, Odin, Pascal, PureBasic, Python, Ruby, Rust, Swift... +- Frameworks: Amethyst, bsf, Cinder, Cocos2d-x, Diligent Engine, Flexium, GML/GameMakerStudio2, Irrlicht, Ogre, OpenFrameworks, OpenSceneGraph/OSG, ORX, px_render, LÖVE+Lua, Magnum, NanoRT, Qt, QtDirect3D, SFML, Software Rasterizers, Unreal Engine 4... +- Note that C bindings ([cimgui](https://github.com/cimgui/cimgui)) are auto-generated, you can use its json/lua output to generate bindings for other languages. + +Also see [Wiki](https://github.com/ocornut/imgui/wiki) for more links and ideas. + +### Upcoming Changes + +Some of the goals for 2019 are: +- Finish work on docking, tabs. (see [#2109](https://github.com/ocornut/imgui/issues/2109), in public [docking](https://github.com/ocornut/imgui/tree/docking) branch looking for feedback) +- Finish work on multiple viewports / multiple OS windows. (see [#1542](https://github.com/ocornut/imgui/issues/1542), in public [docking](https://github.com/ocornut/imgui/tree/docking) branch looking for feedback) +- Finish work on gamepad/keyboard controls. (see [#787](https://github.com/ocornut/imgui/issues/787)) +- Add an automation and testing system, both to test the library and end-user apps. (see [#435](https://github.com/ocornut/imgui/issues/435)) +- Make Columns better. They are currently pretty terrible! New Tables API coming Q4 2019! +- Make the examples look better, improve styles, improve font support, make the examples hi-DPI and multi-DPI aware. + +### Gallery + +For more user-submitted screenshots of projects using Dear ImGui, check out the [Gallery Threads](https://github.com/ocornut/imgui/issues/2847)! + +Custom engine +[![screenshot game](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v149/gallery_TheDragonsTrap-01-thumb.jpg)](https://cloud.githubusercontent.com/assets/8225057/20628927/33e14cac-b329-11e6-80f6-9524e93b048a.png) + +Custom engine +[![screenshot tool](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white_preview.jpg)](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white.png) + +[Tracy Profiler](https://bitbucket.org/wolfpld/tracy) +![tracy profiler](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/tracy_profiler.png) + +### Support, Frequently Asked Questions (FAQ) + +Most common questions will be answered by the [Frequently Asked Questions (FAQ)](https://github.com/ocornut/imgui/wiki/FAQ) page, e.g.: + +**Basics** +- "Where is the documentation?" +- "Which version should I get?" +- "Why the names "Dear ImGui" vs "ImGui"?" + +**Community** +- "How can I help?" + +**Concerns** +- "Who uses Dear ImGui?" +- "Can you create elaborate/serious tools with Dear ImGui?" +- "Can you reskin the look of Dear ImGui?" +- "Why using C++ (as opposed to C)?" + +**Integration** +- "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application?" +- "How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display)" +- "I integrated Dear ImGui in my engine and the text or lines are blurry.." +- "I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.." + +**Usage** +- "Why are multiple widgets reacting when I interact with a single one? How can I have multiple widgets with the same label or with an empty label?" +- "How can I display an image? What is ImTextureID, how does it work?" +- "How can I use my own math types instead of ImVec2/ImVec4?" +- "How can I interact with standard C++ types (such as std::string and std::vector)?" +- "How can I use low-level drawing facilities? (using ImDrawList API)" + +**Fonts, Text** +- "How can I load a different font than the default?" +- "How can I easily use icons in my application?" +- "How can I load multiple fonts?" +- "How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic?" + +See: [Wiki](https://github.com/ocornut/imgui/wiki) for many links, references, articles. + +See: [Articles about the IMGUI paradigm](https://github.com/ocornut/imgui/wiki#Articles-about-the-IMGUI-paradigm) to read/learn about the Immediate Mode GUI paradigm. + +If you are new to Dear ImGui and have issues with: compiling, linking, adding fonts, wiring inputs, running or displaying Dear ImGui: you can use [Discord server](https://discord.gg/NgJ4SEP) or [Discourse forums](https://discourse.dearimgui.org). + +Otherwise, for any other questions, bug reports, requests, feedback, you may post on https://github.com/ocornut/imgui/issues. Please read and fill the New Issue template carefully. + +Paid private support is available for business customers (E-mail: _contact @ dearimgui dot org_). + +**Which version should I get?** + +I occasionally tag [Releases](https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported. + +You may also peak at the [Multi-Viewport](https://github.com/ocornut/imgui/issues/1542) and [Docking](https://github.com/ocornut/imgui/issues/2109) features in the `docking` branch. Many projects are using this branch and it is kept in sync with master regularly. + +**Who uses Dear ImGui?** + +See the [Quotes](https://github.com/ocornut/imgui/wiki/Quotes) and [Software using dear imgui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages for a list of games/software which are publicly known to use dear imgui. Please add yours if you can! Also see the [Gallery Threads](https://github.com/ocornut/imgui/issues/2847)! + +How to help +----------- + +**How can I help?** + +- You may participate in the [Discord server](https://discord.gg/NgJ4SEP), [Discourse forums](https://discourse.dearimgui.org), GitHub [issues tracker](https://github.com/ocornut/imgui/issues). +- You may help with development and submit pull requests! Please understand that by submitting a PR you are also submitting a request for the maintainer to review your code and then take over its maintenance forever. PR should be crafted both in the interest in the end-users and also to ease the maintainer into understanding and accepting it. +- See [Help wanted](https://github.com/ocornut/imgui/wiki/Help-Wanted) on the [Wiki](https://github.com/ocornut/imgui/wiki/) for some more ideas. +- Have your company financially support this project. + +**How can I help financing further development of Dear ImGui?** + +Your contributions are keeping this project alive. The library is available under a free and permissive license, but continued maintenance and development are a full-time endeavor and I would like to grow the team. In addition to maintenance and stability there are many desirable features yet to be added. If your company is using dear imgui, please consider reaching out for invoiced technical support and maintenance contracts. If you are an individual using dear imgui, please consider supporting the project via Patreon or PayPal. Thank you! + +Businesses: support continued development via invoiced technical support, maintenance, sponsoring contracts: +
  _E-mail: contact @ dearimgui dot org_ + +Individuals/hobbyists: support continued maintenance and development via the monthly Patreon: +
  [![Patreon](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/patreon_02.png)](http://www.patreon.com/imgui) + +Individuals/hobbyists: support continued maintenance and development via PayPal: +
  [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WGHNC6MBFLZ2S) + +### Sponsors + +Ongoing Dear ImGui development is financially supported by users and private sponsors, recently: + +*Platinum-chocolate sponsors* +- Blizzard Entertainment +- Google + +*Double-chocolate sponsors* +- Media Molecule, Mobigame, Aras Pranckevičius, Greggman, DotEmu, Nadeo, Supercell, Runner, Aiden Koss, Kylotonn. + +*Salty caramel supporters* +- Remedy Entertainment, Recognition Robotics, ikrima, Geoffrey Evans, Mercury Labs, Singularity Demo Group, Lionel Landwerlin, Ron Gilbert, Brandon Townsend, G3DVu, Cort Stratton, drudru, Harfang 3D, Jeff Roberts, Rainway inc, Ondra Voves, Mesh Consultants, Unit 2 Games, Neil Bickford, Bill Six, Graham Manders. + +*Caramel supporters* +- Jerome Lanquetot, Daniel Collin, Ctrl Alt Ninja, Neil Henning, Neil Blakey-Milner, Aleksei, NeiloGD, Eric, Game Atelier, Vincent Hamm, Morten Skaaning, Colin Riley, Sergio Gonzales, Andrew Berridge, Roy Eltham, Game Preservation Society, Josh Faust, Martin Donlon, Codecat, Doug McNabb, Emmanuel Julien, Guillaume Chereau, Jeffrey Slutter, Jeremiah Deckard, r-lyeh, Nekith, Joshua Fisher, Malte Hoffmann, Mustafa Karaalioglu, Merlyn Morgan-Graham, Per Vognsen, Fabian Giesen, Jan Staubach, Matt Hargett, John Shearer, Jesse Chounard, kingcoopa, Jonas Bernemann, Johan Andersson, Michael Labbe, Tomasz Golebiowski, Louis Schnellbach, Jimmy Andrews, Bojan Endrovski, Robin Berg Pettersen, Rachel Crawford, Andrew Johnson, Sean Hunter, Jordan Mellow, Nefarius Software Solutions, Laura Wieme, Robert Nix, Mick Honey, Steven Kah Hien Wong, Bartosz Bielecki, Oscar Penas, A M, Liam Moynihan, Artometa, Mark Lee, Dimitri Diakopoulos, Pete Goodwin, Johnathan Roatch, nyu lea, Oswald Hurlem, Semyon Smelyanskiy, Le Bach, Jeong MyeongSoo, Chris Matthews, Astrofra, Frederik De Bleser, Anticrisis, Matt Reyer. + +And all other past and present supporters; THANK YOU! +(Please contact me if you would like to be added or removed from this list) + +Credits +------- + +Developed by [Omar Cornut](http://www.miracleworld.net) and every direct or indirect contributors to the GitHub. The early version of this library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com) (Vita). + +I first discovered the IMGUI paradigm at [Q-Games](http://www.q-games.com) where Atman Binstock had dropped his own simple implementation in the codebase, which I spent quite some time improving and thinking about. It turned out that Atman was exposed to the concept directly by working with Casey. When I moved to Media Molecule I rewrote a new library trying to overcome the flaws and limitations of the first one I've worked with. It became this library and since then I have spent an unreasonable amount of time iterating and improving it. + +Embeds [ProggyClean.ttf](http://upperbounds.net) font by Tristan Grimmer (MIT license). + +Embeds [stb_textedit.h, stb_truetype.h, stb_rect_pack.h](https://github.com/nothings/stb/) by Sean Barrett (public domain). + +Inspiration, feedback, and testing for early versions: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. And everybody posting feedback, questions and patches on the GitHub. + +License +------- + +Dear ImGui is licensed under the MIT License, see [LICENSE.txt](https://github.com/ocornut/imgui/blob/master/LICENSE.txt) for more information. diff --git a/imgui/imconfig.h b/imgui/imconfig.h new file mode 100644 index 00000000..45e75ecf --- /dev/null +++ b/imgui/imconfig.h @@ -0,0 +1,93 @@ +//----------------------------------------------------------------------------- +// COMPILE-TIME OPTIONS FOR DEAR IMGUI +// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. +// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. +//----------------------------------------------------------------------------- +// A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/branch with your modifications to imconfig.h) +// B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG "myfilename.h" +// If you do so you need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include +// the imgui*.cpp files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. +// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. +// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using. +//----------------------------------------------------------------------------- + +#pragma once + +//---- Define assertion handler. Defaults to calling assert(). +//#define IM_ASSERT(_EXPR) MyAssert(_EXPR) +//#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts + +//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows +// Using dear imgui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +//#define IMGUI_API __declspec( dllexport ) +//#define IMGUI_API __declspec( dllimport ) + +//---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. +//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +//---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty) +// It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp. +//#define IMGUI_DISABLE_DEMO_WINDOWS +//#define IMGUI_DISABLE_METRICS_WINDOW + +//---- Don't implement some functions to reduce linkage requirements. +//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. +//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. +//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). +//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices'). +//#define IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself if you don't want to link with vsnprintf. +//#define IMGUI_DISABLE_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 wrapper so you can implement them yourself. Declare your prototypes in imconfig.h. +//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). + +//---- Include imgui_user.h at the end of imgui.h as a convenience +//#define IMGUI_INCLUDE_IMGUI_USER_H + +//---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) +//#define IMGUI_USE_BGRA_PACKED_COLOR + +//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version +// By default the embedded implementations are declared static and not available outside of imgui cpp files. +//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" +//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" +//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION +//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION + +//---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. +// This will be inlined as part of ImVec2 and ImVec4 class declarations. +/* +#define IM_VEC2_CLASS_EXTRA \ + ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \ + operator MyVec2() const { return MyVec2(x,y); } + +#define IM_VEC4_CLASS_EXTRA \ + ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \ + operator MyVec4() const { return MyVec4(x,y,z,w); } +*/ + +//---- Using 32-bits vertex indices (default is 16-bits) is one way to allow large meshes with more than 64K vertices. +// Your renderer back-end will need to support it (most example renderer back-ends support both 16/32-bits indices). +// Another way to allow large meshes while keeping 16-bits indices is to handle ImDrawCmd::VtxOffset in your renderer. +// Read about ImGuiBackendFlags_RendererHasVtxOffset for details. +//#define ImDrawIdx unsigned int + +//---- Override ImDrawCallback signature (will need to modify renderer back-ends accordingly) +//struct ImDrawList; +//struct ImDrawCmd; +//typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); +//#define ImDrawCallback MyImDrawCallback + +//---- Debug Tools +// Use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging. +//#define IM_DEBUG_BREAK IM_ASSERT(0) +//#define IM_DEBUG_BREAK __debugbreak() +// Have the Item Picker break in the ItemAdd() function instead of ItemHoverable() - which is earlier in the code, will catch a few extra items, allow picking items other than Hovered one. +// This adds a small runtime cost which is why it is not enabled by default. +//#define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX + +//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files. +/* +namespace ImGui +{ + void MyFunction(const char* name, const MyMatrix44& v); +} +*/ diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp new file mode 100644 index 00000000..fc155333 --- /dev/null +++ b/imgui/imgui.cpp @@ -0,0 +1,9948 @@ +// dear imgui, v1.74 WIP +// (main code and documentation) + +// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. +// Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. +// Get latest version at https://github.com/ocornut/imgui +// Releases change-log at https://github.com/ocornut/imgui/releases +// Technical Support for Getting Started https://discourse.dearimgui.org/c/getting-started +// Gallery (please post your screenshots/video there!): https://github.com/ocornut/imgui/issues/2847 + +// Developed by Omar Cornut and every direct or indirect contributors to the GitHub. +// See LICENSE.txt for copyright and licensing details (standard MIT License). +// This library is free but I need your support to sustain development and maintenance. +// Businesses: you can support continued maintenance and development via support contracts or sponsoring, see docs/README. +// Individuals: you can support continued maintenance and development via donations or Patreon https://www.patreon.com/imgui. + +// It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. +// Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without +// modifying imgui.h or imgui.cpp. You may include imgui_internal.h to access internal data structures, but it doesn't +// come with any guarantee of forward compatibility. Discussing your changes on the GitHub Issue Tracker may lead you +// to a better solution or official support for them. + +/* + +Index of this file: + +DOCUMENTATION + +- MISSION STATEMENT +- END-USER GUIDE +- PROGRAMMER GUIDE (read me!) + - Read first. + - How to update to a newer version of Dear ImGui. + - Getting started with integrating Dear ImGui in your code/engine. + - This is how a simple application may look like (2 variations). + - This is how a simple rendering function may look like. + - Using gamepad/keyboard navigation controls. +- API BREAKING CHANGES (read me when you update!) +- FREQUENTLY ASKED QUESTIONS (FAQ) + - All answers in https://github.com/ocornut/imgui/wiki/FAQ + - Where is the documentation? + - Which version should I get? + - Who uses Dear ImGui? + - Why the odd dual naming, "Dear ImGui" vs "ImGui"? + - How can I tell whether to dispatch mouse/keyboard to imgui or to my application? + - How can I display an image? What is ImTextureID, how does it works? + - Why are multiple widgets reacting when I interact with a single one? How can I have + multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack... + - How can I use my own math types instead of ImVec2/ImVec4? + - How can I load a different font than the default? + - How can I easily use icons in my application? + - How can I load multiple fonts? + - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic? + - How can I interact with standard C++ types (such as std::string and std::vector)? + - How can I use the drawing facilities without a Dear ImGui window? (using ImDrawList API) + - How can I use Dear ImGui on a platform that doesn't have a mouse or a keyboard? (input share, remoting, gamepad) + - I integrated Dear ImGui in my engine and the text or lines are blurry.. + - I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. + - How can I help? + +CODE +(search for "[SECTION]" in the code to find them) + +// [SECTION] FORWARD DECLARATIONS +// [SECTION] CONTEXT AND MEMORY ALLOCATORS +// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) +// [SECTION] MISC HELPERS/UTILITIES (ImText* functions) +// [SECTION] MISC HELPERS/UTILITIES (Color functions) +// [SECTION] ImGuiStorage +// [SECTION] ImGuiTextFilter +// [SECTION] ImGuiTextBuffer +// [SECTION] ImGuiListClipper +// [SECTION] RENDER HELPERS +// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +// [SECTION] SCROLLING +// [SECTION] TOOLTIPS +// [SECTION] POPUPS +// [SECTION] KEYBOARD/GAMEPAD NAVIGATION +// [SECTION] DRAG AND DROP +// [SECTION] LOGGING/CAPTURING +// [SECTION] SETTINGS +// [SECTION] PLATFORM DEPENDENT HELPERS +// [SECTION] METRICS/DEBUG WINDOW + +*/ + +//----------------------------------------------------------------------------- +// DOCUMENTATION +//----------------------------------------------------------------------------- + +/* + + MISSION STATEMENT + ================= + + - Easy to use to create code-driven and data-driven tools. + - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools. + - Easy to hack and improve. + - Minimize screen real-estate usage. + - Minimize setup and maintenance. + - Minimize state storage on user side. + - Portable, minimize dependencies, run on target (consoles, phones, etc.). + - Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window,. + opening a tree node for the first time, etc. but a typical frame should not allocate anything). + + Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes: + - Doesn't look fancy, doesn't animate. + - Limited layout features, intricate layouts are typically crafted in code. + + + END-USER GUIDE + ============== + + - Double-click on title bar to collapse window. + - Click upper right corner to close a window, available when 'bool* p_open' is passed to ImGui::Begin(). + - Click and drag on lower right corner to resize window (double-click to auto fit window to its contents). + - Click and drag on any empty space to move window. + - TAB/SHIFT+TAB to cycle through keyboard editable fields. + - CTRL+Click on a slider or drag box to input value as text. + - Use mouse wheel to scroll. + - Text editor: + - Hold SHIFT or use mouse to select text. + - CTRL+Left/Right to word jump. + - CTRL+Shift+Left/Right to select words. + - CTRL+A our Double-Click to select all. + - CTRL+X,CTRL+C,CTRL+V to use OS clipboard/ + - CTRL+Z,CTRL+Y to undo/redo. + - ESCAPE to revert text to its original value. + - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!) + - Controls are automatically adjusted for OSX to match standard OSX text editing operations. + - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard. + - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://goo.gl/9LgVZW + + + PROGRAMMER GUIDE + ================ + + READ FIRST: + + - Read the FAQ below this section! + - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction + or destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, less bugs. + - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. + - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build. + - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori). + You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links docs/README.md. + - Dear ImGui is a "single pass" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances. + For every application frame your UI code will be called only once. This is in contrast to e.g. Unity's own implementation of an IMGUI, + where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches. + - Our origin are on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right. + - This codebase is also optimized to yield decent performances with typical "Debug" builds settings. + - Please make sure you have asserts enabled (IM_ASSERT redirects to assert() by default, but can be redirected). + If you get an assert, read the messages and comments around the assert. + - C++: this is a very C-ish codebase: we don't rely on C++11, we don't include any C++ headers, and ImGui:: is a namespace. + - C++: ImVec2/ImVec4 do not expose math operators by default, because it is expected that you use your own math types. + See FAQ "How can I use my own math types instead of ImVec2/ImVec4?" for details about setting up imconfig.h for that. + However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase. + - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!). + + HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI: + + - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) + - Or maintain your own branch where you have imconfig.h modified. + - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. + If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed + from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will + likely be a comment about it. Please report any issue to the GitHub page! + - Try to keep your copy of dear imgui reasonably up to date. + + GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE: + + - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. + - Add the Dear ImGui source files to your projects or using your preferred build system. + It is recommended you build and statically link the .cpp files as part of your project and not as shared library (DLL). + - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. + - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. + - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide. + Effectively it means you can create widgets at any time in your code, regardless of considerations of being in "update" vs "render" + phases of your own application. All rendering information are stored into command-lists that you will retrieve after calling ImGui::Render(). + - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code. + - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder. + + HOW A SIMPLE APPLICATION MAY LOOK LIKE: + EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder). + + // Application init: create a dear imgui context, setup some options, load fonts + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls. + // TODO: Fill optional fields of the io structure later. + // TODO: Load TTF/OTF fonts if you don't want to use the default font. + + // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32 and imgui_impl_dx11) + ImGui_ImplWin32_Init(hwnd); + ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); + + // Application main loop + while (true) + { + // Feed inputs to dear imgui, start new frame + ImGui_ImplDX11_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); + + // Any application code here + ImGui::Text("Hello, world!"); + + // Render dear imgui into screen + ImGui::Render(); + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); + g_pSwapChain->Present(1, 0); + } + + // Shutdown + ImGui_ImplDX11_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); + + HOW A SIMPLE APPLICATION MAY LOOK LIKE: + EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE. + + // Application init: create a dear imgui context, setup some options, load fonts + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + // TODO: Set optional io.ConfigFlags values, e.g. 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard' to enable keyboard controls. + // TODO: Fill optional fields of the io structure later. + // TODO: Load TTF/OTF fonts if you don't want to use the default font. + + // Build and load the texture atlas into a texture + // (In the examples/ app this is usually done within the ImGui_ImplXXX_Init() function from one of the demo Renderer) + int width, height; + unsigned char* pixels = NULL; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + // At this point you've got the texture data and you need to upload that your your graphic system: + // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'. + // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ below for details about ImTextureID. + MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32) + io.Fonts->TexID = (void*)texture; + + // Application main loop + while (true) + { + // Setup low-level inputs, e.g. on Win32: calling GetKeyboardState(), or write to those fields from your Windows message handlers, etc. + // (In the examples/ app this is usually done within the ImGui_ImplXXX_NewFrame() function from one of the demo Platform bindings) + io.DeltaTime = 1.0f/60.0f; // set the time elapsed since the previous frame (in seconds) + io.DisplaySize.x = 1920.0f; // set the current display width + io.DisplaySize.y = 1280.0f; // set the current display height here + io.MousePos = my_mouse_pos; // set the mouse position + io.MouseDown[0] = my_mouse_buttons[0]; // set the mouse button states + io.MouseDown[1] = my_mouse_buttons[1]; + + // Call NewFrame(), after this point you can use ImGui::* functions anytime + // (So you want to try calling NewFrame() as early as you can in your mainloop to be able to use Dear ImGui everywhere) + ImGui::NewFrame(); + + // Most of your application code here + ImGui::Text("Hello, world!"); + MyGameUpdate(); // may use any Dear ImGui functions, e.g. ImGui::Begin("My window"); ImGui::Text("Hello, world!"); ImGui::End(); + MyGameRender(); // may use any Dear ImGui functions as well! + + // Render dear imgui, swap buffers + // (You want to try calling EndFrame/Render as late as you can, to be able to use Dear ImGui in your own game rendering code) + ImGui::EndFrame(); + ImGui::Render(); + ImDrawData* draw_data = ImGui::GetDrawData(); + MyImGuiRenderFunction(draw_data); + SwapBuffers(); + } + + // Shutdown + ImGui::DestroyContext(); + + HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE: + + void void MyImGuiRenderFunction(ImDrawData* draw_data) + { + // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled + // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize + // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize + // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color. + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + const ImDrawVert* vtx_buffer = cmd_list->VtxBuffer.Data; // vertex buffer generated by Dear ImGui + const ImDrawIdx* idx_buffer = cmd_list->IdxBuffer.Data; // index buffer generated by Dear ImGui + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + if (pcmd->UserCallback) + { + pcmd->UserCallback(cmd_list, pcmd); + } + else + { + // The texture for the draw call is specified by pcmd->TextureId. + // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization. + MyEngineBindTexture((MyTexture*)pcmd->TextureId); + + // We are using scissoring to clip some objects. All low-level graphics API should supports it. + // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches + // (some elements visible outside their bounds) but you can fix that once everything else works! + // - Clipping coordinates are provided in imgui coordinates space (from draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize) + // In a single viewport application, draw_data->DisplayPos will always be (0,0) and draw_data->DisplaySize will always be == io.DisplaySize. + // However, in the interest of supporting multi-viewport applications in the future (see 'viewport' branch on github), + // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space. + // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min) + ImVec2 pos = draw_data->DisplayPos; + MyEngineScissor((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pos.x), (int)(pcmd->ClipRect.w - pos.y)); + + // Render 'pcmd->ElemCount/3' indexed triangles. + // By default the indices ImDrawIdx are 16-bits, you can change them to 32-bits in imconfig.h if your engine doesn't support 16-bits indices. + MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer); + } + idx_buffer += pcmd->ElemCount; + } + } + } + + - The examples/ folders contains many actual implementation of the pseudo-codes above. + - When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated. + They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs from the + rest of your application. In every cases you need to pass on the inputs to Dear ImGui. Refer to the FAQ for more information. + - Please read the FAQ below!. Amusingly, it is called a FAQ because people frequently run into the same issues! + + USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS + + - The gamepad/keyboard navigation is fairly functional and keeps being improved. + - Gamepad support is particularly useful to use dear imgui on a console system (e.g. PS4, Switch, XB1) without a mouse! + - You can ask questions and report issues at https://github.com/ocornut/imgui/issues/787 + - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable. + - Gamepad: + - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. + - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame(). + Note that io.NavInputs[] is cleared by EndFrame(). + - See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values: + 0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks. + - We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. + Your code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). + - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://goo.gl/9LgVZW. + - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo + to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. + - Keyboard: + - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. + NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. + - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag + will be set. For more advanced uses, you may want to read from: + - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. + - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). + - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. + Please reach out if you think the game vs navigation input sharing could be improved. + - Mouse: + - PS4 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. + - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + uSynergy.c (on your console/tablet/phone app) to share your PC mouse/keyboard. + - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. + Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. + When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. + When that happens your back-end NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the binding in examples/ do that. + (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse as moving back and forth!) + (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want + to set a boolean to ignore your other external mouse positions until the external source is moved again.) + + + API BREAKING CHANGES + ==================== + + Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix. + Below is a change-log of API breaking changes only. If you are using one of the functions listed, expect to have to fix some code. + When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. + You can read releases logs https://github.com/ocornut/imgui/releases for more details. + + - 2019/10/14 (1.74) - inputs: Fixed a miscalculation in the keyboard/mouse "typematic" repeat delay/rate calculation, used by keys and e.g. repeating mouse buttons as well as the GetKeyPressedAmount() function. + if you were using a non-default value for io.KeyRepeatRate (previous default was 0.250), you can add +io.KeyRepeatDelay to it to compensate for the fix. + The function was triggering on: 0.0 and (delay+rate*N) where (N>=1). Fixed formula responds to (N>=0). Effectively it made io.KeyRepeatRate behave like it was set to (io.KeyRepeatRate + io.KeyRepeatDelay). + If you never altered io.KeyRepeatRate nor used GetKeyPressedAmount() this won't affect you. + - 2019/07/15 (1.72) - removed TreeAdvanceToLabelPos() which is rarely used and only does SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()). Kept redirection function (will obsolete). + - 2019/07/12 (1.72) - renamed ImFontAtlas::CustomRect to ImFontAtlasCustomRect. Kept redirection typedef (will obsolete). + - 2019/06/14 (1.72) - removed redirecting functions/enums names that were marked obsolete in 1.51 (June 2017): ImGuiCol_Column*, ImGuiSetCond_*, IsItemHoveredRect(), IsPosHoveringAnyWindow(), IsMouseHoveringAnyWindow(), IsMouseHoveringWindow(), IMGUI_ONCE_UPON_A_FRAME. Grep this log for details and new names. + - 2019/06/07 (1.71) - rendering of child window outer decorations (bg color, border, scrollbars) is now performed as part of the parent window. If you have + overlapping child windows in a same parent, and relied on their relative z-order to be mapped to their submission order, this will affect your rendering. + This optimization is disabled if the parent window has no visual output, because it appears to be the most common situation leading to the creation of overlapping child windows. + Please reach out if you are affected. + - 2019/05/13 (1.71) - renamed SetNextTreeNodeOpen() to SetNextItemOpen(). Kept inline redirection function (will obsolete). + - 2019/05/11 (1.71) - changed io.AddInputCharacter(unsigned short c) signature to io.AddInputCharacter(unsigned int c). + - 2019/04/29 (1.70) - improved ImDrawList thick strokes (>1.0f) preserving correct thickness up to 90 degrees angles (e.g. rectangles). If you have custom rendering using thick lines, they will appear thicker now. + - 2019/04/29 (1.70) - removed GetContentRegionAvailWidth(), use GetContentRegionAvail().x instead. Kept inline redirection function (will obsolete). + - 2019/03/04 (1.69) - renamed GetOverlayDrawList() to GetForegroundDrawList(). Kept redirection function (will obsolete). + - 2019/02/26 (1.69) - renamed ImGuiColorEditFlags_RGB/ImGuiColorEditFlags_HSV/ImGuiColorEditFlags_HEX to ImGuiColorEditFlags_DisplayRGB/ImGuiColorEditFlags_DisplayHSV/ImGuiColorEditFlags_DisplayHex. Kept redirection enums (will obsolete). + - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with a dummy small value! + - 2019/02/01 (1.68) - removed io.DisplayVisibleMin/DisplayVisibleMax (which were marked obsolete and removed from viewport/docking branch already). + - 2019/01/06 (1.67) - renamed io.InputCharacters[], marked internal as was always intended. Please don't access directly, and use AddInputCharacter() instead! + - 2019/01/06 (1.67) - renamed ImFontAtlas::GlyphRangesBuilder to ImFontGlyphRangesBuilder. Kept redirection typedef (will obsolete). + - 2018/12/20 (1.67) - made it illegal to call Begin("") with an empty string. This somehow half-worked before but had various undesirable side-effects. + - 2018/12/10 (1.67) - renamed io.ConfigResizeWindowsFromEdges to io.ConfigWindowsResizeFromEdges as we are doing a large pass on configuration flags. + - 2018/10/12 (1.66) - renamed misc/stl/imgui_stl.* to misc/cpp/imgui_stdlib.* in prevision for other C++ helper files. + - 2018/09/28 (1.66) - renamed SetScrollHere() to SetScrollHereY(). Kept redirection function (will obsolete). + - 2018/09/06 (1.65) - renamed stb_truetype.h to imstb_truetype.h, stb_textedit.h to imstb_textedit.h, and stb_rect_pack.h to imstb_rectpack.h. + If you were conveniently using the imgui copy of those STB headers in your project you will have to update your include paths. + - 2018/09/05 (1.65) - renamed io.OptCursorBlink/io.ConfigCursorBlink to io.ConfigInputTextCursorBlink. (#1427) + - 2018/08/31 (1.64) - added imgui_widgets.cpp file, extracted and moved widgets code out of imgui.cpp into imgui_widgets.cpp. Re-ordered some of the code remaining in imgui.cpp. + NONE OF THE FUNCTIONS HAVE CHANGED. THE CODE IS SEMANTICALLY 100% IDENTICAL, BUT _EVERY_ FUNCTION HAS BEEN MOVED. + Because of this, any local modifications to imgui.cpp will likely conflict when you update. Read docs/CHANGELOG.txt for suggestions. + - 2018/08/22 (1.63) - renamed IsItemDeactivatedAfterChange() to IsItemDeactivatedAfterEdit() for consistency with new IsItemEdited() API. Kept redirection function (will obsolete soonish as IsItemDeactivatedAfterChange() is very recent). + - 2018/08/21 (1.63) - renamed ImGuiTextEditCallback to ImGuiInputTextCallback, ImGuiTextEditCallbackData to ImGuiInputTextCallbackData for consistency. Kept redirection types (will obsolete). + - 2018/08/21 (1.63) - removed ImGuiInputTextCallbackData::ReadOnly since it is a duplication of (ImGuiInputTextCallbackData::Flags & ImGuiInputTextFlags_ReadOnly). + - 2018/08/01 (1.63) - removed per-window ImGuiWindowFlags_ResizeFromAnySide beta flag in favor of a global io.ConfigResizeWindowsFromEdges [update 1.67 renamed to ConfigWindowsResizeFromEdges] to enable the feature. + - 2018/08/01 (1.63) - renamed io.OptCursorBlink to io.ConfigCursorBlink [-> io.ConfigInputTextCursorBlink in 1.65], io.OptMacOSXBehaviors to ConfigMacOSXBehaviors for consistency. + - 2018/07/22 (1.63) - changed ImGui::GetTime() return value from float to double to avoid accumulating floating point imprecisions over time. + - 2018/07/08 (1.63) - style: renamed ImGuiCol_ModalWindowDarkening to ImGuiCol_ModalWindowDimBg for consistency with other features. Kept redirection enum (will obsolete). + - 2018/06/08 (1.62) - examples: the imgui_impl_xxx files have been split to separate platform (Win32, Glfw, SDL2, etc.) from renderer (DX11, OpenGL, Vulkan, etc.). + old bindings will still work as is, however prefer using the separated bindings as they will be updated to support multi-viewports. + when adopting new bindings follow the main.cpp code of your preferred examples/ folder to know which functions to call. + in particular, note that old bindings called ImGui::NewFrame() at the end of their ImGui_ImplXXXX_NewFrame() function. + - 2018/06/06 (1.62) - renamed GetGlyphRangesChinese() to GetGlyphRangesChineseFull() to distinguish other variants and discourage using the full set. + - 2018/06/06 (1.62) - TreeNodeEx()/TreeNodeBehavior(): the ImGuiTreeNodeFlags_CollapsingHeader helper now include the ImGuiTreeNodeFlags_NoTreePushOnOpen flag. See Changelog for details. + - 2018/05/03 (1.61) - DragInt(): the default compile-time format string has been changed from "%.0f" to "%d", as we are not using integers internally any more. + If you used DragInt() with custom format strings, make sure you change them to use %d or an integer-compatible format. + To honor backward-compatibility, the DragInt() code will currently parse and modify format strings to replace %*f with %d, giving time to users to upgrade their code. + If you have IMGUI_DISABLE_OBSOLETE_FUNCTIONS enabled, the code will instead assert! You may run a reg-exp search on your codebase for e.g. "DragInt.*%f" to help you find them. + - 2018/04/28 (1.61) - obsoleted InputFloat() functions taking an optional "int decimal_precision" in favor of an equivalent and more flexible "const char* format", + consistent with other functions. Kept redirection functions (will obsolete). + - 2018/04/09 (1.61) - IM_DELETE() helper function added in 1.60 doesn't clear the input _pointer_ reference, more consistent with expectation and allows passing r-value. + - 2018/03/20 (1.60) - renamed io.WantMoveMouse to io.WantSetMousePos for consistency and ease of understanding (was added in 1.52, _not_ used by core and only honored by some binding ahead of merging the Nav branch). + - 2018/03/12 (1.60) - removed ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered as the closing cross uses regular button colors now. + - 2018/03/08 (1.60) - changed ImFont::DisplayOffset.y to default to 0 instead of +1. Fixed rounding of Ascent/Descent to match TrueType renderer. If you were adding or subtracting to ImFont::DisplayOffset check if your fonts are correctly aligned vertically. + - 2018/03/03 (1.60) - renamed ImGuiStyleVar_Count_ to ImGuiStyleVar_COUNT and ImGuiMouseCursor_Count_ to ImGuiMouseCursor_COUNT for consistency with other public enums. + - 2018/02/18 (1.60) - BeginDragDropSource(): temporarily removed the optional mouse_button=0 parameter because it is not really usable in many situations at the moment. + - 2018/02/16 (1.60) - obsoleted the io.RenderDrawListsFn callback, you can call your graphics engine render function after ImGui::Render(). Use ImGui::GetDrawData() to retrieve the ImDrawData* to display. + - 2018/02/07 (1.60) - reorganized context handling to be more explicit, + - YOU NOW NEED TO CALL ImGui::CreateContext() AT THE BEGINNING OF YOUR APP, AND CALL ImGui::DestroyContext() AT THE END. + - removed Shutdown() function, as DestroyContext() serve this purpose. + - you may pass a ImFontAtlas* pointer to CreateContext() to share a font atlas between contexts. Otherwise CreateContext() will create its own font atlas instance. + - removed allocator parameters from CreateContext(), they are now setup with SetAllocatorFunctions(), and shared by all contexts. + - removed the default global context and font atlas instance, which were confusing for users of DLL reloading and users of multiple contexts. + - 2018/01/31 (1.60) - moved sample TTF files from extra_fonts/ to misc/fonts/. If you loaded files directly from the imgui repo you may need to update your paths. + - 2018/01/11 (1.60) - obsoleted IsAnyWindowHovered() in favor of IsWindowHovered(ImGuiHoveredFlags_AnyWindow). Kept redirection function (will obsolete). + - 2018/01/11 (1.60) - obsoleted IsAnyWindowFocused() in favor of IsWindowFocused(ImGuiFocusedFlags_AnyWindow). Kept redirection function (will obsolete). + - 2018/01/03 (1.60) - renamed ImGuiSizeConstraintCallback to ImGuiSizeCallback, ImGuiSizeConstraintCallbackData to ImGuiSizeCallbackData. + - 2017/12/29 (1.60) - removed CalcItemRectClosestPoint() which was weird and not really used by anyone except demo code. If you need it it's easy to replicate on your side. + - 2017/12/24 (1.53) - renamed the emblematic ShowTestWindow() function to ShowDemoWindow(). Kept redirection function (will obsolete). + - 2017/12/21 (1.53) - ImDrawList: renamed style.AntiAliasedShapes to style.AntiAliasedFill for consistency and as a way to explicitly break code that manipulate those flag at runtime. You can now manipulate ImDrawList::Flags + - 2017/12/21 (1.53) - ImDrawList: removed 'bool anti_aliased = true' final parameter of ImDrawList::AddPolyline() and ImDrawList::AddConvexPolyFilled(). Prefer manipulating ImDrawList::Flags if you need to toggle them during the frame. + - 2017/12/14 (1.53) - using the ImGuiWindowFlags_NoScrollWithMouse flag on a child window forwards the mouse wheel event to the parent window, unless either ImGuiWindowFlags_NoInputs or ImGuiWindowFlags_NoScrollbar are also set. + - 2017/12/13 (1.53) - renamed GetItemsLineHeightWithSpacing() to GetFrameHeightWithSpacing(). Kept redirection function (will obsolete). + - 2017/12/13 (1.53) - obsoleted IsRootWindowFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootWindow). Kept redirection function (will obsolete). + - obsoleted IsRootWindowOrAnyChildFocused() in favor of using IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows). Kept redirection function (will obsolete). + - 2017/12/12 (1.53) - renamed ImGuiTreeNodeFlags_AllowOverlapMode to ImGuiTreeNodeFlags_AllowItemOverlap. Kept redirection enum (will obsolete). + - 2017/12/10 (1.53) - removed SetNextWindowContentWidth(), prefer using SetNextWindowContentSize(). Kept redirection function (will obsolete). + - 2017/11/27 (1.53) - renamed ImGuiTextBuffer::append() helper to appendf(), appendv() to appendfv(). If you copied the 'Log' demo in your code, it uses appendv() so that needs to be renamed. + - 2017/11/18 (1.53) - Style, Begin: removed ImGuiWindowFlags_ShowBorders window flag. Borders are now fully set up in the ImGuiStyle structure (see e.g. style.FrameBorderSize, style.WindowBorderSize). Use ImGui::ShowStyleEditor() to look them up. + Please note that the style system will keep evolving (hopefully stabilizing in Q1 2018), and so custom styles will probably subtly break over time. It is recommended you use the StyleColorsClassic(), StyleColorsDark(), StyleColorsLight() functions. + - 2017/11/18 (1.53) - Style: removed ImGuiCol_ComboBg in favor of combo boxes using ImGuiCol_PopupBg for consistency. + - 2017/11/18 (1.53) - Style: renamed ImGuiCol_ChildWindowBg to ImGuiCol_ChildBg. + - 2017/11/18 (1.53) - Style: renamed style.ChildWindowRounding to style.ChildRounding, ImGuiStyleVar_ChildWindowRounding to ImGuiStyleVar_ChildRounding. + - 2017/11/02 (1.53) - obsoleted IsRootWindowOrAnyChildHovered() in favor of using IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); + - 2017/10/24 (1.52) - renamed IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS/IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS to IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS/IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS for consistency. + - 2017/10/20 (1.52) - changed IsWindowHovered() default parameters behavior to return false if an item is active in another window (e.g. click-dragging item from another window to this window). You can use the newly introduced IsWindowHovered() flags to requests this specific behavior if you need it. + - 2017/10/20 (1.52) - marked IsItemHoveredRect()/IsMouseHoveringWindow() as obsolete, in favor of using the newly introduced flags for IsItemHovered() and IsWindowHovered(). See https://github.com/ocornut/imgui/issues/1382 for details. + removed the IsItemRectHovered()/IsWindowRectHovered() names introduced in 1.51 since they were merely more consistent names for the two functions we are now obsoleting. + IsItemHoveredRect() --> IsItemHovered(ImGuiHoveredFlags_RectOnly) + IsMouseHoveringAnyWindow() --> IsWindowHovered(ImGuiHoveredFlags_AnyWindow) + IsMouseHoveringWindow() --> IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) [weird, old behavior] + - 2017/10/17 (1.52) - marked the old 5-parameters version of Begin() as obsolete (still available). Use SetNextWindowSize()+Begin() instead! + - 2017/10/11 (1.52) - renamed AlignFirstTextHeightToWidgets() to AlignTextToFramePadding(). Kept inline redirection function (will obsolete). + - 2017/09/26 (1.52) - renamed ImFont::Glyph to ImFontGlyph. Kept redirection typedef (will obsolete). + - 2017/09/25 (1.52) - removed SetNextWindowPosCenter() because SetNextWindowPos() now has the optional pivot information to do the same and more. Kept redirection function (will obsolete). + - 2017/08/25 (1.52) - io.MousePos needs to be set to ImVec2(-FLT_MAX,-FLT_MAX) when mouse is unavailable/missing. Previously ImVec2(-1,-1) was enough but we now accept negative mouse coordinates. In your binding if you need to support unavailable mouse, make sure to replace "io.MousePos = ImVec2(-1,-1)" with "io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX)". + - 2017/08/22 (1.51) - renamed IsItemHoveredRect() to IsItemRectHovered(). Kept inline redirection function (will obsolete). -> (1.52) use IsItemHovered(ImGuiHoveredFlags_RectOnly)! + - renamed IsMouseHoveringAnyWindow() to IsAnyWindowHovered() for consistency. Kept inline redirection function (will obsolete). + - renamed IsMouseHoveringWindow() to IsWindowRectHovered() for consistency. Kept inline redirection function (will obsolete). + - 2017/08/20 (1.51) - renamed GetStyleColName() to GetStyleColorName() for consistency. + - 2017/08/20 (1.51) - added PushStyleColor(ImGuiCol idx, ImU32 col) overload, which _might_ cause an "ambiguous call" compilation error if you are using ImColor() with implicit cast. Cast to ImU32 or ImVec4 explicily to fix. + - 2017/08/15 (1.51) - marked the weird IMGUI_ONCE_UPON_A_FRAME helper macro as obsolete. prefer using the more explicit ImGuiOnceUponAFrame type. + - 2017/08/15 (1.51) - changed parameter order for BeginPopupContextWindow() from (const char*,int buttons,bool also_over_items) to (const char*,int buttons,bool also_over_items). Note that most calls relied on default parameters completely. + - 2017/08/13 (1.51) - renamed ImGuiCol_Column to ImGuiCol_Separator, ImGuiCol_ColumnHovered to ImGuiCol_SeparatorHovered, ImGuiCol_ColumnActive to ImGuiCol_SeparatorActive. Kept redirection enums (will obsolete). + - 2017/08/11 (1.51) - renamed ImGuiSetCond_Always to ImGuiCond_Always, ImGuiSetCond_Once to ImGuiCond_Once, ImGuiSetCond_FirstUseEver to ImGuiCond_FirstUseEver, ImGuiSetCond_Appearing to ImGuiCond_Appearing. Kept redirection enums (will obsolete). + - 2017/08/09 (1.51) - removed ValueColor() helpers, they are equivalent to calling Text(label) + SameLine() + ColorButton(). + - 2017/08/08 (1.51) - removed ColorEditMode() and ImGuiColorEditMode in favor of ImGuiColorEditFlags and parameters to the various Color*() functions. The SetColorEditOptions() allows to initialize default but the user can still change them with right-click context menu. + - changed prototype of 'ColorEdit4(const char* label, float col[4], bool show_alpha = true)' to 'ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0)', where passing flags = 0x01 is a safe no-op (hello dodgy backward compatibility!). - check and run the demo window, under "Color/Picker Widgets", to understand the various new options. + - changed prototype of rarely used 'ColorButton(ImVec4 col, bool small_height = false, bool outline_border = true)' to 'ColorButton(const char* desc_id, ImVec4 col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0))' + - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse + - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset. + - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity. + - 2016/11/06 (1.50) - BeginChild(const char*) now applies the stack id to the provided label, consistently with other functions as it should always have been. It shouldn't affect you unless (extremely unlikely) you were appending multiple times to a same child from different locations of the stack id. If that's the case, generate an id with GetId() and use it instead of passing string to BeginChild(). + - 2016/10/15 (1.50) - avoid 'void* user_data' parameter to io.SetClipboardTextFn/io.GetClipboardTextFn pointers. We pass io.ClipboardUserData to it. + - 2016/09/25 (1.50) - style.WindowTitleAlign is now a ImVec2 (ImGuiAlign enum was removed). set to (0.5f,0.5f) for horizontal+vertical centering, (0.0f,0.0f) for upper-left, etc. + - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully breakage should be minimal. + - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore. + If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you. + If your TitleBg/TitleBgActive alpha was <1.0f you need to tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. + This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color. + ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) + { + float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; + return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a); + } + If this is confusing, pick the RGB value from title bar from an old screenshot and apply this as TitleBg/TitleBgActive. Or you may just create TitleBgActive from a tweaked TitleBg color. + - 2016/05/07 (1.49) - removed confusing set of GetInternalState(), GetInternalStateSize(), SetInternalState() functions. Now using CreateContext(), DestroyContext(), GetCurrentContext(), SetCurrentContext(). + - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection. + - 2016/05/01 (1.49) - obsoleted old signature of CollapsingHeader(const char* label, const char* str_id = NULL, bool display_frame = true, bool default_open = false) as extra parameters were badly designed and rarely used. You can replace the "default_open = true" flag in new API with CollapsingHeader(label, ImGuiTreeNodeFlags_DefaultOpen). + - 2016/04/26 (1.49) - changed ImDrawList::PushClipRect(ImVec4 rect) to ImDrawList::PushClipRect(Imvec2 min,ImVec2 max,bool intersect_with_current_clip_rect=false). Note that higher-level ImGui::PushClipRect() is preferable because it will clip at logic/widget level, whereas ImDrawList::PushClipRect() only affect your renderer. + - 2016/04/03 (1.48) - removed style.WindowFillAlphaDefault setting which was redundant. Bake default BG alpha inside style.Colors[ImGuiCol_WindowBg] and all other Bg color values. (ref github issue #337). + - 2016/04/03 (1.48) - renamed ImGuiCol_TooltipBg to ImGuiCol_PopupBg, used by popups/menus and tooltips. popups/menus were previously using ImGuiCol_WindowBg. (ref github issue #337) + - 2016/03/21 (1.48) - renamed GetWindowFont() to GetFont(), GetWindowFontSize() to GetFontSize(). Kept inline redirection function (will obsolete). + - 2016/03/02 (1.48) - InputText() completion/history/always callbacks: if you modify the text buffer manually (without using DeleteChars()/InsertChars() helper) you need to maintain the BufTextLen field. added an assert. + - 2016/01/23 (1.48) - fixed not honoring exact width passed to PushItemWidth(), previously it would add extra FramePadding.x*2 over that width. if you had manual pixel-perfect alignment in place it might affect you. + - 2015/12/27 (1.48) - fixed ImDrawList::AddRect() which used to render a rectangle 1 px too large on each axis. + - 2015/12/04 (1.47) - renamed Color() helpers to ValueColor() - dangerously named, rarely used and probably to be made obsolete. + - 2015/08/29 (1.45) - with the addition of horizontal scrollbar we made various fixes to inconsistencies with dealing with cursor position. + GetCursorPos()/SetCursorPos() functions now include the scrolled amount. It shouldn't affect the majority of users, but take note that SetCursorPosX(100.0f) puts you at +100 from the starting x position which may include scrolling, not at +100 from the window left side. + GetContentRegionMax()/GetWindowContentRegionMin()/GetWindowContentRegionMax() functions allow include the scrolled amount. Typically those were used in cases where no scrolling would happen so it may not be a problem, but watch out! + - 2015/08/29 (1.45) - renamed style.ScrollbarWidth to style.ScrollbarSize + - 2015/08/05 (1.44) - split imgui.cpp into extra files: imgui_demo.cpp imgui_draw.cpp imgui_internal.h that you need to add to your project. + - 2015/07/18 (1.44) - fixed angles in ImDrawList::PathArcTo(), PathArcToFast() (introduced in 1.43) being off by an extra PI for no justifiable reason + - 2015/07/14 (1.43) - add new ImFontAtlas::AddFont() API. For the old AddFont***, moved the 'font_no' parameter of ImFontAtlas::AddFont** functions to the ImFontConfig structure. + you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text. + - 2015/07/08 (1.43) - switched rendering data to use indexed rendering. this is saving a fair amount of CPU/GPU and enables us to get anti-aliasing for a marginal cost. + this necessary change will break your rendering function! the fix should be very easy. sorry for that :( + - if you are using a vanilla copy of one of the imgui_impl_XXXX.cpp provided in the example, you just need to update your copy and you can ignore the rest. + - the signature of the io.RenderDrawListsFn handler has changed! + old: ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) + new: ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data). + parameters: 'cmd_lists' becomes 'draw_data->CmdLists', 'cmd_lists_count' becomes 'draw_data->CmdListsCount' + ImDrawList: 'commands' becomes 'CmdBuffer', 'vtx_buffer' becomes 'VtxBuffer', 'IdxBuffer' is new. + ImDrawCmd: 'vtx_count' becomes 'ElemCount', 'clip_rect' becomes 'ClipRect', 'user_callback' becomes 'UserCallback', 'texture_id' becomes 'TextureId'. + - each ImDrawList now contains both a vertex buffer and an index buffer. For each command, render ElemCount/3 triangles using indices from the index buffer. + - if you REALLY cannot render indexed primitives, you can call the draw_data->DeIndexAllBuffers() method to de-index the buffers. This is slow and a waste of CPU/GPU. Prefer using indexed rendering! + - refer to code in the examples/ folder or ask on the GitHub if you are unsure of how to upgrade. please upgrade! + - 2015/07/10 (1.43) - changed SameLine() parameters from int to float. + - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete). + - 2015/07/02 (1.42) - renamed GetScrollPosY() to GetScrollY(). Necessary to reduce confusion along with other scrolling functions, because positions (e.g. cursor position) are not equivalent to scrolling amount. + - 2015/06/14 (1.41) - changed ImageButton() default bg_col parameter from (0,0,0,1) (black) to (0,0,0,0) (transparent) - makes a difference when texture have transparence + - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely be used. Sorry! + - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete). + - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete). + - 2015/05/27 (1.40) - removed the third 'repeat_if_held' parameter from Button() - sorry! it was rarely used and inconsistent. Use PushButtonRepeat(true) / PopButtonRepeat() to enable repeat on desired buttons. + - 2015/05/11 (1.40) - changed BeginPopup() API, takes a string identifier instead of a bool. ImGui needs to manage the open/closed state of popups. Call OpenPopup() to actually set the "open" state of a popup. BeginPopup() returns true if the popup is opened. + - 2015/05/03 (1.40) - removed style.AutoFitPadding, using style.WindowPadding makes more sense (the default values were already the same). + - 2015/04/13 (1.38) - renamed IsClipped() to IsRectClipped(). Kept inline redirection function until 1.50. + - 2015/04/09 (1.38) - renamed ImDrawList::AddArc() to ImDrawList::AddArcFast() for compatibility with future API + - 2015/04/03 (1.38) - removed ImGuiCol_CheckHovered, ImGuiCol_CheckActive, replaced with the more general ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive. + - 2014/04/03 (1.38) - removed support for passing -FLT_MAX..+FLT_MAX as the range for a SliderFloat(). Use DragFloat() or Inputfloat() instead. + - 2015/03/17 (1.36) - renamed GetItemBoxMin()/GetItemBoxMax()/IsMouseHoveringBox() to GetItemRectMin()/GetItemRectMax()/IsMouseHoveringRect(). Kept inline redirection function until 1.50. + - 2015/03/15 (1.36) - renamed style.TreeNodeSpacing to style.IndentSpacing, ImGuiStyleVar_TreeNodeSpacing to ImGuiStyleVar_IndentSpacing + - 2015/03/13 (1.36) - renamed GetWindowIsFocused() to IsWindowFocused(). Kept inline redirection function until 1.50. + - 2015/03/08 (1.35) - renamed style.ScrollBarWidth to style.ScrollbarWidth (casing) + - 2015/02/27 (1.34) - renamed OpenNextNode(bool) to SetNextTreeNodeOpened(bool, ImGuiSetCond). Kept inline redirection function until 1.50. + - 2015/02/27 (1.34) - renamed ImGuiSetCondition_*** to ImGuiSetCond_***, and _FirstUseThisSession becomes _Once. + - 2015/02/11 (1.32) - changed text input callback ImGuiTextEditCallback return type from void-->int. reserved for future use, return 0 for now. + - 2015/02/10 (1.32) - renamed GetItemWidth() to CalcItemWidth() to clarify its evolving behavior + - 2015/02/08 (1.31) - renamed GetTextLineSpacing() to GetTextLineHeightWithSpacing() + - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused) + - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions. + - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. + (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. + font init: { const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); <..Upload texture to GPU..>; } + became: { unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); <..Upload texture to GPU>; io.Fonts->TexId = YourTextureIdentifier; } + you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. + it is now recommended that you sample the font texture with bilinear interpolation. + (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. + (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) + (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets + - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) + - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph) + - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility + - 2014/11/07 (1.15) - renamed IsHovered() to IsItemHovered() + - 2014/10/02 (1.14) - renamed IMGUI_INCLUDE_IMGUI_USER_CPP to IMGUI_INCLUDE_IMGUI_USER_INL and imgui_user.cpp to imgui_user.inl (more IDE friendly) + - 2014/09/25 (1.13) - removed 'text_end' parameter from IO.SetClipboardTextFn (the string is now always zero-terminated for simplicity) + - 2014/09/24 (1.12) - renamed SetFontScale() to SetWindowFontScale() + - 2014/09/24 (1.12) - moved IM_MALLOC/IM_REALLOC/IM_FREE preprocessor defines to IO.MemAllocFn/IO.MemReallocFn/IO.MemFreeFn + - 2014/08/30 (1.09) - removed IO.FontHeight (now computed automatically) + - 2014/08/30 (1.09) - moved IMGUI_FONT_TEX_UV_FOR_WHITE preprocessor define to IO.FontTexUvForWhite + - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes + + + FREQUENTLY ASKED QUESTIONS (FAQ), TIPS + ====================================== + + All answers in: https://github.com/ocornut/imgui/wiki/FAQ + Some answers are copied down here to facilitate searching in code or because they are most likely to + be varying depending on your version of the code. + + Q&A: Basics + =========== + + Q: Where is the documentation? + A: This library is poorly documented at the moment and expects of the user to be acquainted with C/C++. + - Run the examples/ and explore them. + - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. + - The demo covers most features of Dear ImGui, so you can read the code and see its output. + - See documentation and comments at the top of imgui.cpp + effectively imgui.h. + - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ + folder to explain how to integrate Dear ImGui with your own engine/application. + - Your programming IDE is your friend, find the type or function declaration to find comments + associated to it. + + Q: Which version should I get? + Q: Why the names "Dear ImGui" vs "ImGui"? + A: See https://github.com/ocornut/imgui/wiki/FAQ + + Q&A: Concerns + ============= + + Q: Who uses Dear ImGui? + Q: Can you create elaborate/serious tools with Dear ImGui? + Q: Can you reskin the look of Dear ImGui? + Q: Why using C++ (as opposed to C)? + A: See https://github.com/ocornut/imgui/wiki/FAQ + + Q&A: Integration + ================ + + Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application? + A: You can read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags from the ImGuiIO structure (e.g. if (ImGui::GetIO().WantCaptureMouse) { ... } ) + - When 'io.WantCaptureMouse' is set, imgui wants to use your mouse state, and you may want to discard/hide the inputs from the rest of your application. + - When 'io.WantCaptureKeyboard' is set, imgui wants to use your keyboard state, and you may want to discard/hide the inputs from the rest of your application. + - When 'io.WantTextInput' is set to may want to notify your OS to popup an on-screen keyboard, if available (e.g. on a mobile phone, or console OS). + Note: you should always pass your mouse/keyboard inputs to imgui, even when the io.WantCaptureXXX flag are set false. + This is because imgui needs to detect that you clicked in the void to unfocus its own windows. + Note: The 'io.WantCaptureMouse' is more accurate that any attempt to "check if the mouse is hovering a window" (don't do that!). + It handle mouse dragging correctly (both dragging that started over your application or over an imgui window) and handle e.g. modal windows blocking inputs. + Those flags are updated by ImGui::NewFrame(). Preferably read the flags after calling NewFrame() if you can afford it, but reading them before is also + perfectly fine, as the bool toggle fairly rarely. If you have on a touch device, you might find use for an early call to UpdateHoveredWindowAndCaptureFlags(). + Note: Text input widget releases focus on "Return KeyDown", so the subsequent "Return KeyUp" event that your application receive will typically + have 'io.WantCaptureKeyboard=false'. Depending on your application logic it may or not be inconvenient. You might want to track which key-downs + were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.) + + Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) + Q: I integrated Dear ImGui in my engine and the text or lines are blurry.. + Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. + A: See https://github.com/ocornut/imgui/wiki/FAQ + + Q&A: Usage + ---------- + + Q: Why are multiple widgets reacting when I interact with a single one? + Q: How can I have multiple widgets with the same label or with an empty label? + A: A primer on labels and the ID Stack... + + Dear ImGui internally need to uniquely identify UI elements. + Elements that are typically not clickable (such as calls to the Text functions) don't need an ID. + Interactive widgets (such as calls to Button buttons) need a unique ID. + Unique ID are used internally to track active widgets and occasionally associate state to widgets. + Unique ID are implicitly built from the hash of multiple elements that identify the "path" to the UI element. + + - Unique ID are often derived from a string label: + + Button("OK"); // Label = "OK", ID = hash of (..., "OK") + Button("Cancel"); // Label = "Cancel", ID = hash of (..., "Cancel") + + - ID are uniquely scoped within windows, tree nodes, etc. which all pushes to the ID stack. Having + two buttons labeled "OK" in different windows or different tree locations is fine. + We used "..." above to signify whatever was already pushed to the ID stack previously: + + Begin("MyWindow"); + Button("OK"); // Label = "OK", ID = hash of ("MyWindow", "OK") + End(); + Begin("MyOtherWindow"); + Button("OK"); // Label = "OK", ID = hash of ("MyOtherWindow", "OK") + End(); + + - If you have a same ID twice in the same location, you'll have a conflict: + + Button("OK"); + Button("OK"); // ID collision! Interacting with either button will trigger the first one. + + Fear not! this is easy to solve and there are many ways to solve it! + + - Solving ID conflict in a simple/local context: + When passing a label you can optionally specify extra ID information within string itself. + Use "##" to pass a complement to the ID that won't be visible to the end-user. + This helps solving the simple collision cases when you know e.g. at compilation time which items + are going to be created: + + Begin("MyWindow"); + Button("Play"); // Label = "Play", ID = hash of ("MyWindow", "Play") + Button("Play##foo1"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo1") // Different from above + Button("Play##foo2"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo2") // Different from above + End(); + + - If you want to completely hide the label, but still need an ID: + + Checkbox("##On", &b); // Label = "", ID = hash of (..., "##On") // No visible label, just a checkbox! + + - Occasionally/rarely you might want change a label while preserving a constant ID. This allows + you to animate labels. For example you may want to include varying information in a window title bar, + but windows are uniquely identified by their ID. Use "###" to pass a label that isn't part of ID: + + Button("Hello###ID"); // Label = "Hello", ID = hash of (..., "###ID") + Button("World###ID"); // Label = "World", ID = hash of (..., "###ID") // Same as above, even though the label looks different + + sprintf(buf, "My game (%f FPS)###MyGame", fps); + Begin(buf); // Variable title, ID = hash of "MyGame" + + - Solving ID conflict in a more general manner: + Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts + within the same window. This is the most convenient way of distinguishing ID when iterating and + creating many UI elements programmatically. + You can push a pointer, a string or an integer value into the ID stack. + Remember that ID are formed from the concatenation of _everything_ pushed into the ID stack. + At each level of the stack we store the seed used for items at this level of the ID stack. + + Begin("Window"); + for (int i = 0; i < 100; i++) + { + PushID(i); // Push i to the id tack + Button("Click"); // Label = "Click", ID = hash of ("Window", i, "Click") + PopID(); + } + for (int i = 0; i < 100; i++) + { + MyObject* obj = Objects[i]; + PushID(obj); + Button("Click"); // Label = "Click", ID = hash of ("Window", obj pointer, "Click") + PopID(); + } + for (int i = 0; i < 100; i++) + { + MyObject* obj = Objects[i]; + PushID(obj->Name); + Button("Click"); // Label = "Click", ID = hash of ("Window", obj->Name, "Click") + PopID(); + } + End(); + + - You can stack multiple prefixes into the ID stack: + + Button("Click"); // Label = "Click", ID = hash of (..., "Click") + PushID("node"); + Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") + PushID(my_ptr); + Button("Click"); // Label = "Click", ID = hash of (..., "node", my_ptr, "Click") + PopID(); + PopID(); + + - Tree nodes implicitly creates a scope for you by calling PushID(). + + Button("Click"); // Label = "Click", ID = hash of (..., "Click") + if (TreeNode("node")) // <-- this function call will do a PushID() for you (unless instructed not to, with a special flag) + { + Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") + TreePop(); + } + + - When working with trees, ID are used to preserve the open/close state of each tree node. + Depending on your use cases you may want to use strings, indices or pointers as ID. + e.g. when following a single pointer that may change over time, using a static string as ID + will preserve your node open/closed state when the targeted object change. + e.g. when displaying a list of objects, using indices or pointers as ID will preserve the + node open/closed state differently. See what makes more sense in your situation! + + Q: How can I display an image? What is ImTextureID, how does it works? + A: See https://github.com/ocornut/imgui/wiki/FAQ and https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples + + Q: How can I use my own math types instead of ImVec2/ImVec4? + Q: How can I interact with standard C++ types (such as std::string and std::vector)? + Q: How can I use low-level drawing facilities? (using ImDrawList API) + A: See https://github.com/ocornut/imgui/wiki/FAQ + + Q&A: Fonts, Text + ================ + + Q: How can I load a different font than the default? + Q: How can I easily use icons in my application? + Q: How can I load multiple fonts? + Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? + A: See https://github.com/ocornut/imgui/wiki/FAQ and misc/fonts/README.txt + + Q&A: Community + ============== + + Q: How can I help? + A: - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt + and see how you want to help and can help! + - Businesses: convince your company to fund development via support contracts/sponsoring! This is among the most useful thing you can do for dear imgui. + - Individuals: you can also become a Patron (http://www.patreon.com/imgui) or donate on PayPal! See README. + - Disclose your usage of dear imgui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. + You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/2847). Visuals are ideal as they inspire other programmers. + But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. + - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately). + +*/ + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#ifndef IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_MATH_OPERATORS +#endif +#include "imgui_internal.h" + +#include // toupper +#include // vsnprintf, sscanf, printf +#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier +#include // intptr_t +#else +#include // intptr_t +#endif + +// Debug options +#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL +#define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window +#define IMGUI_DEBUG_INI_SETTINGS 0 // Save additional comments in .ini file + +// Visual Studio warnings +#ifdef _MSC_VER +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#endif + +// Clang/GCC warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning : unknown warning group '-Wformat-pedantic *' // not all warnings are known by all clang versions.. so ignoring warnings triggers new warnings on some configuration. great! +#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. +#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. +#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning : declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. +#pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // +#pragma clang diagnostic ignored "-Wformat-pedantic" // warning : format specifies type 'void *' but the argument has type 'xxxx *' // unreasonable, would lead to casting every %p arg to void*. probably enabled by -pedantic. +#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 +#endif +#if __has_warning("-Wdouble-promotion") +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#endif +#elif defined(__GNUC__) +// We disable -Wpragmas because GCC doesn't provide an has_warning equivalent and some forks/patches may not following the warning/version association. +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size +#pragma GCC diagnostic ignored "-Wformat" // warning: format '%p' expects argument of type 'void*', but argument 6 has type 'ImGuiWindow*' +#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function +#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value +#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked +#pragma GCC diagnostic ignored "-Wstrict-overflow" // warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +// When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch. +static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in +static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear + +// Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by back-end) +static const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f; // Extend outside and inside windows. Affect FindHoveredWindow(). +static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. +static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certaint time, unless mouse moved. + +//------------------------------------------------------------------------- +// [SECTION] FORWARD DECLARATIONS +//------------------------------------------------------------------------- + +static void SetCurrentWindow(ImGuiWindow* window); +static void FindHoveredWindow(); +static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags); +static void CheckStacksSize(ImGuiWindow* window, bool write); +static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges); + +static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); +static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window); + +static ImRect GetViewportRect(); + +// Settings +static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); +static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); +static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf); + +// Platform Dependents default implementation for IO functions +static const char* GetClipboardTextFn_DefaultImpl(void* user_data); +static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); +static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); + +namespace ImGui +{ +static bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); + +// Navigation +static void NavUpdate(); +static void NavUpdateWindowing(); +static void NavUpdateWindowingOverlay(); +static void NavUpdateMoveResult(); +static float NavUpdatePageUpPageDown(); +static inline void NavUpdateAnyRequestFlag(); +static bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand); +static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); +static ImVec2 NavCalcPreferredRefPos(); +static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); +static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); +static int FindWindowFocusIndex(ImGuiWindow* window); + +// Misc +static void UpdateMouseInputs(); +static void UpdateMouseWheel(); +static bool UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]); +static void UpdateDebugToolItemPicker(); +static void RenderWindowOuterBorders(ImGuiWindow* window); +static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); +static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); + +} + +//----------------------------------------------------------------------------- +// [SECTION] CONTEXT AND MEMORY ALLOCATORS +//----------------------------------------------------------------------------- + +// Current context pointer. Implicitly used by all Dear ImGui functions. Always assumed to be != NULL. +// ImGui::CreateContext() will automatically set this pointer if it is NULL. Change to a different context by calling ImGui::SetCurrentContext(). +// 1) Important: globals are not shared across DLL boundaries! If you use DLLs or any form of hot-reloading: you will need to call +// SetCurrentContext() (with the pointer you got from CreateContext) from each unique static/DLL boundary, and after each hot-reloading. +// In your debugger, add GImGui to your watch window and notice how its value changes depending on which location you are currently stepping into. +// 2) Important: Dear ImGui functions are not thread-safe because of this pointer. +// If you want thread-safety to allow N threads to access N different contexts, you can: +// - Change this variable to use thread local storage so each thread can refer to a different context, in imconfig.h: +// struct ImGuiContext; +// extern thread_local ImGuiContext* MyImGuiTLS; +// #define GImGui MyImGuiTLS +// And then define MyImGuiTLS in one of your cpp file. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword. +// - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 +// - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace. +#ifndef GImGui +ImGuiContext* GImGui = NULL; +#endif + +// Memory Allocator functions. Use SetAllocatorFunctions() to change them. +// If you use DLL hotreloading you might need to call SetAllocatorFunctions() after reloading code from this file. +// Otherwise, you probably don't want to modify them mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction. +#ifndef IMGUI_DISABLE_DEFAULT_ALLOCATORS +static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); return malloc(size); } +static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); free(ptr); } +#else +static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(size); IM_ASSERT(0); return NULL; } +static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(ptr); IM_ASSERT(0); } +#endif + +static void* (*GImAllocatorAllocFunc)(size_t size, void* user_data) = MallocWrapper; +static void (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper; +static void* GImAllocatorUserData = NULL; + +//----------------------------------------------------------------------------- +// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +//----------------------------------------------------------------------------- + +ImGuiStyle::ImGuiStyle() +{ + Alpha = 1.0f; // Global alpha applies to everything in ImGui + WindowPadding = ImVec2(8,8); // Padding within a window + WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows + WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. + WindowMinSize = ImVec2(32,32); // Minimum window size + WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text + WindowMenuButtonPosition= ImGuiDir_Left; // Position of the collapsing/docking button in the title bar (left/right). Defaults to ImGuiDir_Left. + ChildRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows + ChildBorderSize = 1.0f; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested. + PopupRounding = 0.0f; // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows + PopupBorderSize = 1.0f; // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested. + FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets) + FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets). + FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. + ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines + ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) + TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! + IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). + ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). + ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar + ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar + GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar + GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. + TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. + TabBorderSize = 0.0f; // Thickness of border around tabs. + ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. + ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. + SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text. + DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows. + DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. + MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. + AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU. + AntiAliasedFill = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) + CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. + + // Default theme + ImGui::StyleColorsDark(this); +} + +// To scale your entire UI (e.g. if you want your app to use High DPI or generally be DPI aware) you may use this helper function. Scaling the fonts is done separately and is up to you. +// Important: This operation is lossy because we round all sizes to integer. If you need to change your scale multiples, call this over a freshly initialized ImGuiStyle structure rather than scaling multiple times. +void ImGuiStyle::ScaleAllSizes(float scale_factor) +{ + WindowPadding = ImFloor(WindowPadding * scale_factor); + WindowRounding = ImFloor(WindowRounding * scale_factor); + WindowMinSize = ImFloor(WindowMinSize * scale_factor); + ChildRounding = ImFloor(ChildRounding * scale_factor); + PopupRounding = ImFloor(PopupRounding * scale_factor); + FramePadding = ImFloor(FramePadding * scale_factor); + FrameRounding = ImFloor(FrameRounding * scale_factor); + ItemSpacing = ImFloor(ItemSpacing * scale_factor); + ItemInnerSpacing = ImFloor(ItemInnerSpacing * scale_factor); + TouchExtraPadding = ImFloor(TouchExtraPadding * scale_factor); + IndentSpacing = ImFloor(IndentSpacing * scale_factor); + ColumnsMinSpacing = ImFloor(ColumnsMinSpacing * scale_factor); + ScrollbarSize = ImFloor(ScrollbarSize * scale_factor); + ScrollbarRounding = ImFloor(ScrollbarRounding * scale_factor); + GrabMinSize = ImFloor(GrabMinSize * scale_factor); + GrabRounding = ImFloor(GrabRounding * scale_factor); + TabRounding = ImFloor(TabRounding * scale_factor); + DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); + DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); + MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); +} + +ImGuiIO::ImGuiIO() +{ + // Most fields are initialized with zero + memset(this, 0, sizeof(*this)); + + // Settings + ConfigFlags = ImGuiConfigFlags_None; + BackendFlags = ImGuiBackendFlags_None; + DisplaySize = ImVec2(-1.0f, -1.0f); + DeltaTime = 1.0f/60.0f; + IniSavingRate = 5.0f; + IniFilename = "imgui.ini"; + LogFilename = "imgui_log.txt"; + MouseDoubleClickTime = 0.30f; + MouseDoubleClickMaxDist = 6.0f; + for (int i = 0; i < ImGuiKey_COUNT; i++) + KeyMap[i] = -1; + KeyRepeatDelay = 0.275f; + KeyRepeatRate = 0.050f; + UserData = NULL; + + Fonts = NULL; + FontGlobalScale = 1.0f; + FontDefault = NULL; + FontAllowUserScaling = false; + DisplayFramebufferScale = ImVec2(1.0f, 1.0f); + + // Miscellaneous options + MouseDrawCursor = false; +#ifdef __APPLE__ + ConfigMacOSXBehaviors = true; // Set Mac OS X style defaults based on __APPLE__ compile time flag +#else + ConfigMacOSXBehaviors = false; +#endif + ConfigInputTextCursorBlink = true; + ConfigWindowsResizeFromEdges = true; + ConfigWindowsMoveFromTitleBarOnly = false; + ConfigWindowsMemoryCompactTimer = 60.0f; + + // Platform Functions + BackendPlatformName = BackendRendererName = NULL; + BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; + GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations + SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; + ClipboardUserData = NULL; + ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl; + ImeWindowHandle = NULL; + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + RenderDrawListsFn = NULL; +#endif + + // Input (NB: we already have memset zero the entire structure!) + MousePos = ImVec2(-FLT_MAX, -FLT_MAX); + MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX); + MouseDragThreshold = 6.0f; + for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; + for (int i = 0; i < IM_ARRAYSIZE(KeysDownDuration); i++) KeysDownDuration[i] = KeysDownDurationPrev[i] = -1.0f; + for (int i = 0; i < IM_ARRAYSIZE(NavInputsDownDuration); i++) NavInputsDownDuration[i] = -1.0f; +} + +// Pass in translated ASCII characters for text input. +// - with glfw you can get those from the callback set in glfwSetCharCallback() +// - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message +void ImGuiIO::AddInputCharacter(unsigned int c) +{ + if (c > 0 && c < 0x10000) + InputQueueCharacters.push_back((ImWchar)c); +} + +void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) +{ + while (*utf8_chars != 0) + { + unsigned int c = 0; + utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); + if (c > 0 && c < 0x10000) + InputQueueCharacters.push_back((ImWchar)c); + } +} + +void ImGuiIO::ClearInputCharacters() +{ + InputQueueCharacters.resize(0); +} + +//----------------------------------------------------------------------------- +// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) +//----------------------------------------------------------------------------- + +ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p) +{ + ImVec2 ap = p - a; + ImVec2 ab_dir = b - a; + float dot = ap.x * ab_dir.x + ap.y * ab_dir.y; + if (dot < 0.0f) + return a; + float ab_len_sqr = ab_dir.x * ab_dir.x + ab_dir.y * ab_dir.y; + if (dot > ab_len_sqr) + return b; + return a + ab_dir * dot / ab_len_sqr; +} + +bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) +{ + bool b1 = ((p.x - b.x) * (a.y - b.y) - (p.y - b.y) * (a.x - b.x)) < 0.0f; + bool b2 = ((p.x - c.x) * (b.y - c.y) - (p.y - c.y) * (b.x - c.x)) < 0.0f; + bool b3 = ((p.x - a.x) * (c.y - a.y) - (p.y - a.y) * (c.x - a.x)) < 0.0f; + return ((b1 == b2) && (b2 == b3)); +} + +void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w) +{ + ImVec2 v0 = b - a; + ImVec2 v1 = c - a; + ImVec2 v2 = p - a; + const float denom = v0.x * v1.y - v1.x * v0.y; + out_v = (v2.x * v1.y - v1.x * v2.y) / denom; + out_w = (v0.x * v2.y - v2.x * v0.y) / denom; + out_u = 1.0f - out_v - out_w; +} + +ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p) +{ + ImVec2 proj_ab = ImLineClosestPoint(a, b, p); + ImVec2 proj_bc = ImLineClosestPoint(b, c, p); + ImVec2 proj_ca = ImLineClosestPoint(c, a, p); + float dist2_ab = ImLengthSqr(p - proj_ab); + float dist2_bc = ImLengthSqr(p - proj_bc); + float dist2_ca = ImLengthSqr(p - proj_ca); + float m = ImMin(dist2_ab, ImMin(dist2_bc, dist2_ca)); + if (m == dist2_ab) + return proj_ab; + if (m == dist2_bc) + return proj_bc; + return proj_ca; +} + +// Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more. +int ImStricmp(const char* str1, const char* str2) +{ + int d; + while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } + return d; +} + +int ImStrnicmp(const char* str1, const char* str2, size_t count) +{ + int d = 0; + while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; } + return d; +} + +void ImStrncpy(char* dst, const char* src, size_t count) +{ + if (count < 1) + return; + if (count > 1) + strncpy(dst, src, count - 1); + dst[count - 1] = 0; +} + +char* ImStrdup(const char* str) +{ + size_t len = strlen(str); + void* buf = IM_ALLOC(len + 1); + return (char*)memcpy(buf, (const void*)str, len + 1); +} + +char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src) +{ + size_t dst_buf_size = p_dst_size ? *p_dst_size : strlen(dst) + 1; + size_t src_size = strlen(src) + 1; + if (dst_buf_size < src_size) + { + IM_FREE(dst); + dst = (char*)IM_ALLOC(src_size); + if (p_dst_size) + *p_dst_size = src_size; + } + return (char*)memcpy(dst, (const void*)src, src_size); +} + +const char* ImStrchrRange(const char* str, const char* str_end, char c) +{ + const char* p = (const char*)memchr(str, (int)c, str_end - str); + return p; +} + +int ImStrlenW(const ImWchar* str) +{ + //return (int)wcslen((const wchar_t*)str); // FIXME-OPT: Could use this when wchar_t are 16-bits + int n = 0; + while (*str++) n++; + return n; +} + +// Find end-of-line. Return pointer will point to either first \n, either str_end. +const char* ImStreolRange(const char* str, const char* str_end) +{ + const char* p = (const char*)memchr(str, '\n', str_end - str); + return p ? p : str_end; +} + +const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line +{ + while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n') + buf_mid_line--; + return buf_mid_line; +} + +const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end) +{ + if (!needle_end) + needle_end = needle + strlen(needle); + + const char un0 = (char)toupper(*needle); + while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end)) + { + if (toupper(*haystack) == un0) + { + const char* b = needle + 1; + for (const char* a = haystack + 1; b < needle_end; a++, b++) + if (toupper(*a) != toupper(*b)) + break; + if (b == needle_end) + return haystack; + } + haystack++; + } + return NULL; +} + +// Trim str by offsetting contents when there's leading data + writing a \0 at the trailing position. We use this in situation where the cost is negligible. +void ImStrTrimBlanks(char* buf) +{ + char* p = buf; + while (p[0] == ' ' || p[0] == '\t') // Leading blanks + p++; + char* p_start = p; + while (*p != 0) // Find end of string + p++; + while (p > p_start && (p[-1] == ' ' || p[-1] == '\t')) // Trailing blanks + p--; + if (p_start != buf) // Copy memory if we had leading blanks + memmove(buf, p_start, p - p_start); + buf[p - p_start] = 0; // Zero terminate +} + +// A) MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size). +// Ideally we would test for only one of those limits at runtime depending on the behavior the vsnprintf(), but trying to deduct it at compile time sounds like a pandora can of worm. +// B) When buf==NULL vsnprintf() will return the output size. +#ifndef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS + +//#define IMGUI_USE_STB_SPRINTF +#ifdef IMGUI_USE_STB_SPRINTF +#define STB_SPRINTF_IMPLEMENTATION +#include "imstb_sprintf.h" +#endif + +#if defined(_MSC_VER) && !defined(vsnprintf) +#define vsnprintf _vsnprintf +#endif + +int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); +#ifdef IMGUI_USE_STB_SPRINTF + int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args); +#else + int w = vsnprintf(buf, buf_size, fmt, args); +#endif + va_end(args); + if (buf == NULL) + return w; + if (w == -1 || w >= (int)buf_size) + w = (int)buf_size - 1; + buf[w] = 0; + return w; +} + +int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) +{ +#ifdef IMGUI_USE_STB_SPRINTF + int w = stbsp_vsnprintf(buf, (int)buf_size, fmt, args); +#else + int w = vsnprintf(buf, buf_size, fmt, args); +#endif + if (buf == NULL) + return w; + if (w == -1 || w >= (int)buf_size) + w = (int)buf_size - 1; + buf[w] = 0; + return w; +} +#endif // #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS + +// CRC32 needs a 1KB lookup table (not cache friendly) +// Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily: +// - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe. +static const ImU32 GCrc32LookupTable[256] = +{ + 0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3,0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91, + 0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5, + 0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59, + 0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D, + 0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01, + 0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65, + 0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9, + 0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F,0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD, + 0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1, + 0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7,0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5, + 0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79, + 0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D, + 0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713,0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21, + 0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45, + 0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB,0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9, + 0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF,0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D, +}; + +// Known size hash +// It is ok to call ImHashData on a string with known length but the ### operator won't be supported. +// FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. +ImU32 ImHashData(const void* data_p, size_t data_size, ImU32 seed) +{ + ImU32 crc = ~seed; + const unsigned char* data = (const unsigned char*)data_p; + const ImU32* crc32_lut = GCrc32LookupTable; + while (data_size-- != 0) + crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ *data++]; + return ~crc; +} + +// Zero-terminated string hash, with support for ### to reset back to seed value +// We support a syntax of "label###id" where only "###id" is included in the hash, and only "label" gets displayed. +// Because this syntax is rarely used we are optimizing for the common case. +// - If we reach ### in the string we discard the hash so far and reset to the seed. +// - We don't do 'current += 2; continue;' after handling ### to keep the code smaller/faster (measured ~10% diff in Debug build) +// FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. +ImU32 ImHashStr(const char* data_p, size_t data_size, ImU32 seed) +{ + seed = ~seed; + ImU32 crc = seed; + const unsigned char* data = (const unsigned char*)data_p; + const ImU32* crc32_lut = GCrc32LookupTable; + if (data_size != 0) + { + while (data_size-- != 0) + { + unsigned char c = *data++; + if (c == '#' && data_size >= 2 && data[0] == '#' && data[1] == '#') + crc = seed; + crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c]; + } + } + else + { + while (unsigned char c = *data++) + { + if (c == '#' && data[0] == '#' && data[1] == '#') + crc = seed; + crc = (crc >> 8) ^ crc32_lut[(crc & 0xFF) ^ c]; + } + } + return ~crc; +} + +FILE* ImFileOpen(const char* filename, const char* mode) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__) + // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. Converting both strings from UTF-8 to wchar format (using a single allocation, because we can) + const int filename_wsize = ImTextCountCharsFromUtf8(filename, NULL) + 1; + const int mode_wsize = ImTextCountCharsFromUtf8(mode, NULL) + 1; + ImVector buf; + buf.resize(filename_wsize + mode_wsize); + ImTextStrFromUtf8(&buf[0], filename_wsize, filename, NULL); + ImTextStrFromUtf8(&buf[filename_wsize], mode_wsize, mode, NULL); + return _wfopen((wchar_t*)&buf[0], (wchar_t*)&buf[filename_wsize]); +#else + return fopen(filename, mode); +#endif +} + +// Load file content into memory +// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() +void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes) +{ + IM_ASSERT(filename && file_open_mode); + if (out_file_size) + *out_file_size = 0; + + FILE* f; + if ((f = ImFileOpen(filename, file_open_mode)) == NULL) + return NULL; + + long file_size_signed; + if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) + { + fclose(f); + return NULL; + } + + size_t file_size = (size_t)file_size_signed; + void* file_data = IM_ALLOC(file_size + padding_bytes); + if (file_data == NULL) + { + fclose(f); + return NULL; + } + if (fread(file_data, 1, file_size, f) != file_size) + { + fclose(f); + IM_FREE(file_data); + return NULL; + } + if (padding_bytes > 0) + memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); + + fclose(f); + if (out_file_size) + *out_file_size = file_size; + + return file_data; +} + +//----------------------------------------------------------------------------- +// [SECTION] MISC HELPERS/UTILITIES (ImText* functions) +//----------------------------------------------------------------------------- + +// Convert UTF-8 to 32-bits character, process single character input. +// Based on stb_from_utf8() from github.com/nothings/stb/ +// We handle UTF-8 decoding error by skipping forward. +int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) +{ + unsigned int c = (unsigned int)-1; + const unsigned char* str = (const unsigned char*)in_text; + if (!(*str & 0x80)) + { + c = (unsigned int)(*str++); + *out_char = c; + return 1; + } + if ((*str & 0xe0) == 0xc0) + { + *out_char = 0xFFFD; // will be invalid but not end of string + if (in_text_end && in_text_end - (const char*)str < 2) return 1; + if (*str < 0xc2) return 2; + c = (unsigned int)((*str++ & 0x1f) << 6); + if ((*str & 0xc0) != 0x80) return 2; + c += (*str++ & 0x3f); + *out_char = c; + return 2; + } + if ((*str & 0xf0) == 0xe0) + { + *out_char = 0xFFFD; // will be invalid but not end of string + if (in_text_end && in_text_end - (const char*)str < 3) return 1; + if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3; + if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below + c = (unsigned int)((*str++ & 0x0f) << 12); + if ((*str & 0xc0) != 0x80) return 3; + c += (unsigned int)((*str++ & 0x3f) << 6); + if ((*str & 0xc0) != 0x80) return 3; + c += (*str++ & 0x3f); + *out_char = c; + return 3; + } + if ((*str & 0xf8) == 0xf0) + { + *out_char = 0xFFFD; // will be invalid but not end of string + if (in_text_end && in_text_end - (const char*)str < 4) return 1; + if (*str > 0xf4) return 4; + if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4; + if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below + c = (unsigned int)((*str++ & 0x07) << 18); + if ((*str & 0xc0) != 0x80) return 4; + c += (unsigned int)((*str++ & 0x3f) << 12); + if ((*str & 0xc0) != 0x80) return 4; + c += (unsigned int)((*str++ & 0x3f) << 6); + if ((*str & 0xc0) != 0x80) return 4; + c += (*str++ & 0x3f); + // utf-8 encodings of values used in surrogate pairs are invalid + if ((c & 0xFFFFF800) == 0xD800) return 4; + *out_char = c; + return 4; + } + *out_char = 0; + return 0; +} + +int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) +{ + ImWchar* buf_out = buf; + ImWchar* buf_end = buf + buf_size; + while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) + { + unsigned int c; + in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); + if (c == 0) + break; + if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes + *buf_out++ = (ImWchar)c; + } + *buf_out = 0; + if (in_text_remaining) + *in_text_remaining = in_text; + return (int)(buf_out - buf); +} + +int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) +{ + int char_count = 0; + while ((!in_text_end || in_text < in_text_end) && *in_text) + { + unsigned int c; + in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); + if (c == 0) + break; + if (c < 0x10000) + char_count++; + } + return char_count; +} + +// Based on stb_to_utf8() from github.com/nothings/stb/ +static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) +{ + if (c < 0x80) + { + buf[0] = (char)c; + return 1; + } + if (c < 0x800) + { + if (buf_size < 2) return 0; + buf[0] = (char)(0xc0 + (c >> 6)); + buf[1] = (char)(0x80 + (c & 0x3f)); + return 2; + } + if (c >= 0xdc00 && c < 0xe000) + { + return 0; + } + if (c >= 0xd800 && c < 0xdc00) + { + if (buf_size < 4) return 0; + buf[0] = (char)(0xf0 + (c >> 18)); + buf[1] = (char)(0x80 + ((c >> 12) & 0x3f)); + buf[2] = (char)(0x80 + ((c >> 6) & 0x3f)); + buf[3] = (char)(0x80 + ((c ) & 0x3f)); + return 4; + } + //else if (c < 0x10000) + { + if (buf_size < 3) return 0; + buf[0] = (char)(0xe0 + (c >> 12)); + buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); + buf[2] = (char)(0x80 + ((c ) & 0x3f)); + return 3; + } +} + +// Not optimal but we very rarely use this function. +int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end) +{ + unsigned int dummy = 0; + return ImTextCharFromUtf8(&dummy, in_text, in_text_end); +} + +static inline int ImTextCountUtf8BytesFromChar(unsigned int c) +{ + if (c < 0x80) return 1; + if (c < 0x800) return 2; + if (c >= 0xdc00 && c < 0xe000) return 0; + if (c >= 0xd800 && c < 0xdc00) return 4; + return 3; +} + +int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end) +{ + char* buf_out = buf; + const char* buf_end = buf + buf_size; + while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) + { + unsigned int c = (unsigned int)(*in_text++); + if (c < 0x80) + *buf_out++ = (char)c; + else + buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c); + } + *buf_out = 0; + return (int)(buf_out - buf); +} + +int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end) +{ + int bytes_count = 0; + while ((!in_text_end || in_text < in_text_end) && *in_text) + { + unsigned int c = (unsigned int)(*in_text++); + if (c < 0x80) + bytes_count++; + else + bytes_count += ImTextCountUtf8BytesFromChar(c); + } + return bytes_count; +} + +//----------------------------------------------------------------------------- +// [SECTION] MISC HELPERS/UTILTIES (Color functions) +// Note: The Convert functions are early design which are not consistent with other API. +//----------------------------------------------------------------------------- + +ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in) +{ + float s = 1.0f/255.0f; + return ImVec4( + ((in >> IM_COL32_R_SHIFT) & 0xFF) * s, + ((in >> IM_COL32_G_SHIFT) & 0xFF) * s, + ((in >> IM_COL32_B_SHIFT) & 0xFF) * s, + ((in >> IM_COL32_A_SHIFT) & 0xFF) * s); +} + +ImU32 ImGui::ColorConvertFloat4ToU32(const ImVec4& in) +{ + ImU32 out; + out = ((ImU32)IM_F32_TO_INT8_SAT(in.x)) << IM_COL32_R_SHIFT; + out |= ((ImU32)IM_F32_TO_INT8_SAT(in.y)) << IM_COL32_G_SHIFT; + out |= ((ImU32)IM_F32_TO_INT8_SAT(in.z)) << IM_COL32_B_SHIFT; + out |= ((ImU32)IM_F32_TO_INT8_SAT(in.w)) << IM_COL32_A_SHIFT; + return out; +} + +// Convert rgb floats ([0-1],[0-1],[0-1]) to hsv floats ([0-1],[0-1],[0-1]), from Foley & van Dam p592 +// Optimized http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv +void ImGui::ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v) +{ + float K = 0.f; + if (g < b) + { + ImSwap(g, b); + K = -1.f; + } + if (r < g) + { + ImSwap(r, g); + K = -2.f / 6.f - K; + } + + const float chroma = r - (g < b ? g : b); + out_h = ImFabs(K + (g - b) / (6.f * chroma + 1e-20f)); + out_s = chroma / (r + 1e-20f); + out_v = r; +} + +// Convert hsv floats ([0-1],[0-1],[0-1]) to rgb floats ([0-1],[0-1],[0-1]), from Foley & van Dam p593 +// also http://en.wikipedia.org/wiki/HSL_and_HSV +void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b) +{ + if (s == 0.0f) + { + // gray + out_r = out_g = out_b = v; + return; + } + + h = ImFmod(h, 1.0f) / (60.0f/360.0f); + int i = (int)h; + float f = h - (float)i; + float p = v * (1.0f - s); + float q = v * (1.0f - s * f); + float t = v * (1.0f - s * (1.0f - f)); + + switch (i) + { + case 0: out_r = v; out_g = t; out_b = p; break; + case 1: out_r = q; out_g = v; out_b = p; break; + case 2: out_r = p; out_g = v; out_b = t; break; + case 3: out_r = p; out_g = q; out_b = v; break; + case 4: out_r = t; out_g = p; out_b = v; break; + case 5: default: out_r = v; out_g = p; out_b = q; break; + } +} + +ImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul) +{ + ImGuiStyle& style = GImGui->Style; + ImVec4 c = style.Colors[idx]; + c.w *= style.Alpha * alpha_mul; + return ColorConvertFloat4ToU32(c); +} + +ImU32 ImGui::GetColorU32(const ImVec4& col) +{ + ImGuiStyle& style = GImGui->Style; + ImVec4 c = col; + c.w *= style.Alpha; + return ColorConvertFloat4ToU32(c); +} + +const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) +{ + ImGuiStyle& style = GImGui->Style; + return style.Colors[idx]; +} + +ImU32 ImGui::GetColorU32(ImU32 col) +{ + float style_alpha = GImGui->Style.Alpha; + if (style_alpha >= 1.0f) + return col; + ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; + a = (ImU32)(a * style_alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. + return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); +} + +//----------------------------------------------------------------------------- +// [SECTION] ImGuiStorage +// Helper: Key->value storage +//----------------------------------------------------------------------------- + +// std::lower_bound but without the bullshit +static ImGuiStorage::ImGuiStoragePair* LowerBound(ImVector& data, ImGuiID key) +{ + ImGuiStorage::ImGuiStoragePair* first = data.Data; + ImGuiStorage::ImGuiStoragePair* last = data.Data + data.Size; + size_t count = (size_t)(last - first); + while (count > 0) + { + size_t count2 = count >> 1; + ImGuiStorage::ImGuiStoragePair* mid = first + count2; + if (mid->key < key) + { + first = ++mid; + count -= count2 + 1; + } + else + { + count = count2; + } + } + return first; +} + +// For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. +void ImGuiStorage::BuildSortByKey() +{ + struct StaticFunc + { + static int IMGUI_CDECL PairCompareByID(const void* lhs, const void* rhs) + { + // We can't just do a subtraction because qsort uses signed integers and subtracting our ID doesn't play well with that. + if (((const ImGuiStoragePair*)lhs)->key > ((const ImGuiStoragePair*)rhs)->key) return +1; + if (((const ImGuiStoragePair*)lhs)->key < ((const ImGuiStoragePair*)rhs)->key) return -1; + return 0; + } + }; + if (Data.Size > 1) + ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), StaticFunc::PairCompareByID); +} + +int ImGuiStorage::GetInt(ImGuiID key, int default_val) const +{ + ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); + if (it == Data.end() || it->key != key) + return default_val; + return it->val_i; +} + +bool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const +{ + return GetInt(key, default_val ? 1 : 0) != 0; +} + +float ImGuiStorage::GetFloat(ImGuiID key, float default_val) const +{ + ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); + if (it == Data.end() || it->key != key) + return default_val; + return it->val_f; +} + +void* ImGuiStorage::GetVoidPtr(ImGuiID key) const +{ + ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); + if (it == Data.end() || it->key != key) + return NULL; + return it->val_p; +} + +// References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer. +int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + it = Data.insert(it, ImGuiStoragePair(key, default_val)); + return &it->val_i; +} + +bool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val) +{ + return (bool*)GetIntRef(key, default_val ? 1 : 0); +} + +float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + it = Data.insert(it, ImGuiStoragePair(key, default_val)); + return &it->val_f; +} + +void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + it = Data.insert(it, ImGuiStoragePair(key, default_val)); + return &it->val_p; +} + +// FIXME-OPT: Need a way to reuse the result of lower_bound when doing GetInt()/SetInt() - not too bad because it only happens on explicit interaction (maximum one a frame) +void ImGuiStorage::SetInt(ImGuiID key, int val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + { + Data.insert(it, ImGuiStoragePair(key, val)); + return; + } + it->val_i = val; +} + +void ImGuiStorage::SetBool(ImGuiID key, bool val) +{ + SetInt(key, val ? 1 : 0); +} + +void ImGuiStorage::SetFloat(ImGuiID key, float val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + { + Data.insert(it, ImGuiStoragePair(key, val)); + return; + } + it->val_f = val; +} + +void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val) +{ + ImGuiStoragePair* it = LowerBound(Data, key); + if (it == Data.end() || it->key != key) + { + Data.insert(it, ImGuiStoragePair(key, val)); + return; + } + it->val_p = val; +} + +void ImGuiStorage::SetAllInt(int v) +{ + for (int i = 0; i < Data.Size; i++) + Data[i].val_i = v; +} + +//----------------------------------------------------------------------------- +// [SECTION] ImGuiTextFilter +//----------------------------------------------------------------------------- + +// Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" +ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) +{ + if (default_filter) + { + ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf)); + Build(); + } + else + { + InputBuf[0] = 0; + CountGrep = 0; + } +} + +bool ImGuiTextFilter::Draw(const char* label, float width) +{ + if (width != 0.0f) + ImGui::SetNextItemWidth(width); + bool value_changed = ImGui::InputText(label, InputBuf, IM_ARRAYSIZE(InputBuf)); + if (value_changed) + Build(); + return value_changed; +} + +void ImGuiTextFilter::ImGuiTextRange::split(char separator, ImVector* out) const +{ + out->resize(0); + const char* wb = b; + const char* we = wb; + while (we < e) + { + if (*we == separator) + { + out->push_back(ImGuiTextRange(wb, we)); + wb = we + 1; + } + we++; + } + if (wb != we) + out->push_back(ImGuiTextRange(wb, we)); +} + +void ImGuiTextFilter::Build() +{ + Filters.resize(0); + ImGuiTextRange input_range(InputBuf, InputBuf+strlen(InputBuf)); + input_range.split(',', &Filters); + + CountGrep = 0; + for (int i = 0; i != Filters.Size; i++) + { + ImGuiTextRange& f = Filters[i]; + while (f.b < f.e && ImCharIsBlankA(f.b[0])) + f.b++; + while (f.e > f.b && ImCharIsBlankA(f.e[-1])) + f.e--; + if (f.empty()) + continue; + if (Filters[i].b[0] != '-') + CountGrep += 1; + } +} + +bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const +{ + if (Filters.empty()) + return true; + + if (text == NULL) + text = ""; + + for (int i = 0; i != Filters.Size; i++) + { + const ImGuiTextRange& f = Filters[i]; + if (f.empty()) + continue; + if (f.b[0] == '-') + { + // Subtract + if (ImStristr(text, text_end, f.b + 1, f.e) != NULL) + return false; + } + else + { + // Grep + if (ImStristr(text, text_end, f.b, f.e) != NULL) + return true; + } + } + + // Implicit * grep + if (CountGrep == 0) + return true; + + return false; +} + +//----------------------------------------------------------------------------- +// [SECTION] ImGuiTextBuffer +//----------------------------------------------------------------------------- + +// On some platform vsnprintf() takes va_list by reference and modifies it. +// va_copy is the 'correct' way to copy a va_list but Visual Studio prior to 2013 doesn't have it. +#ifndef va_copy +#if defined(__GNUC__) || defined(__clang__) +#define va_copy(dest, src) __builtin_va_copy(dest, src) +#else +#define va_copy(dest, src) (dest = src) +#endif +#endif + +char ImGuiTextBuffer::EmptyString[1] = { 0 }; + +void ImGuiTextBuffer::append(const char* str, const char* str_end) +{ + int len = str_end ? (int)(str_end - str) : (int)strlen(str); + + // Add zero-terminator the first time + const int write_off = (Buf.Size != 0) ? Buf.Size : 1; + const int needed_sz = write_off + len; + if (write_off + len >= Buf.Capacity) + { + int new_capacity = Buf.Capacity * 2; + Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity); + } + + Buf.resize(needed_sz); + memcpy(&Buf[write_off - 1], str, (size_t)len); + Buf[write_off - 1 + len] = 0; +} + +void ImGuiTextBuffer::appendf(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + appendfv(fmt, args); + va_end(args); +} + +// Helper: Text buffer for logging/accumulating text +void ImGuiTextBuffer::appendfv(const char* fmt, va_list args) +{ + va_list args_copy; + va_copy(args_copy, args); + + int len = ImFormatStringV(NULL, 0, fmt, args); // FIXME-OPT: could do a first pass write attempt, likely successful on first pass. + if (len <= 0) + { + va_end(args_copy); + return; + } + + // Add zero-terminator the first time + const int write_off = (Buf.Size != 0) ? Buf.Size : 1; + const int needed_sz = write_off + len; + if (write_off + len >= Buf.Capacity) + { + int new_capacity = Buf.Capacity * 2; + Buf.reserve(needed_sz > new_capacity ? needed_sz : new_capacity); + } + + Buf.resize(needed_sz); + ImFormatStringV(&Buf[write_off - 1], (size_t)len + 1, fmt, args_copy); + va_end(args_copy); +} + +//----------------------------------------------------------------------------- +// [SECTION] ImGuiListClipper +// This is currently not as flexible/powerful as it should be and really confusing/spaghetti, mostly because we changed +// the API mid-way through development and support two ways to using the clipper, needs some rework (see TODO) +//----------------------------------------------------------------------------- + +// Helper to calculate coarse clipping of large list of evenly sized items. +// NB: Prefer using the ImGuiListClipper higher-level helper if you can! Read comments and instructions there on how those use this sort of pattern. +// NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX +void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (g.LogEnabled) + { + // If logging is active, do not perform any clipping + *out_items_display_start = 0; + *out_items_display_end = items_count; + return; + } + if (window->SkipItems) + { + *out_items_display_start = *out_items_display_end = 0; + return; + } + + // We create the union of the ClipRect and the NavScoringRect which at worst should be 1 page away from ClipRect + ImRect unclipped_rect = window->ClipRect; + if (g.NavMoveRequest) + unclipped_rect.Add(g.NavScoringRectScreen); + + const ImVec2 pos = window->DC.CursorPos; + int start = (int)((unclipped_rect.Min.y - pos.y) / items_height); + int end = (int)((unclipped_rect.Max.y - pos.y) / items_height); + + // When performing a navigation request, ensure we have one item extra in the direction we are moving to + if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Up) + start--; + if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Down) + end++; + + start = ImClamp(start, 0, items_count); + end = ImClamp(end + 1, start, items_count); + *out_items_display_start = start; + *out_items_display_end = end; +} + +static void SetCursorPosYAndSetupDummyPrevLine(float pos_y, float line_height) +{ + // Set cursor position and a few other things so that SetScrollHereY() and Columns() can work when seeking cursor. + // FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue. + // The clipper should probably have a 4th step to display the last item in a regular manner. + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + window->DC.CursorPos.y = pos_y; + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, pos_y); + window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height; // Setting those fields so that SetScrollHereY() can properly function after the end of our clipper usage. + window->DC.PrevLineSize.y = (line_height - g.Style.ItemSpacing.y); // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list. + if (ImGuiColumns* columns = window->DC.CurrentColumns) + columns->LineMinY = window->DC.CursorPos.y; // Setting this so that cell Y position are set properly +} + +// Use case A: Begin() called from constructor with items_height<0, then called again from Sync() in StepNo 1 +// Use case B: Begin() called from constructor with items_height>0 +// FIXME-LEGACY: Ideally we should remove the Begin/End functions but they are part of the legacy API we still support. This is why some of the code in Step() calling Begin() and reassign some fields, spaghetti style. +void ImGuiListClipper::Begin(int count, float items_height) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + StartPosY = window->DC.CursorPos.y; + ItemsHeight = items_height; + ItemsCount = count; + StepNo = 0; + DisplayEnd = DisplayStart = -1; + if (ItemsHeight > 0.0f) + { + ImGui::CalcListClipping(ItemsCount, ItemsHeight, &DisplayStart, &DisplayEnd); // calculate how many to clip/display + if (DisplayStart > 0) + SetCursorPosYAndSetupDummyPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor + StepNo = 2; + } +} + +void ImGuiListClipper::End() +{ + if (ItemsCount < 0) + return; + // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. + if (ItemsCount < INT_MAX) + SetCursorPosYAndSetupDummyPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor + ItemsCount = -1; + StepNo = 3; +} + +bool ImGuiListClipper::Step() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (ItemsCount == 0 || window->SkipItems) + { + ItemsCount = -1; + return false; + } + if (StepNo == 0) // Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height. + { + DisplayStart = 0; + DisplayEnd = 1; + StartPosY = window->DC.CursorPos.y; + StepNo = 1; + return true; + } + if (StepNo == 1) // Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. + { + if (ItemsCount == 1) { ItemsCount = -1; return false; } + float items_height = window->DC.CursorPos.y - StartPosY; + IM_ASSERT(items_height > 0.0f); // If this triggers, it means Item 0 hasn't moved the cursor vertically + Begin(ItemsCount - 1, items_height); + DisplayStart++; + DisplayEnd++; + StepNo = 3; + return true; + } + if (StepNo == 2) // Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3. + { + IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0); + StepNo = 3; + return true; + } + if (StepNo == 3) // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. + End(); + return false; +} + +//----------------------------------------------------------------------------- +// [SECTION] RENDER HELPERS +// Those (internal) functions are currently quite a legacy mess - their signature and behavior will change. +// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state. +//----------------------------------------------------------------------------- + +const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end) +{ + const char* text_display_end = text; + if (!text_end) + text_end = (const char*)-1; + + while (text_display_end < text_end && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#')) + text_display_end++; + return text_display_end; +} + +// Internal ImGui functions to render text +// RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText() +void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + // Hide anything after a '##' string + const char* text_display_end; + if (hide_text_after_hash) + { + text_display_end = FindRenderedTextEnd(text, text_end); + } + else + { + if (!text_end) + text_end = text + strlen(text); // FIXME-OPT + text_display_end = text_end; + } + + if (text != text_display_end) + { + window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end); + if (g.LogEnabled) + LogRenderedText(&pos, text, text_display_end); + } +} + +void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (!text_end) + text_end = text + strlen(text); // FIXME-OPT + + if (text != text_end) + { + window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width); + if (g.LogEnabled) + LogRenderedText(&pos, text, text_end); + } +} + +// Default clip_rect uses (pos_min,pos_max) +// Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges) +void ImGui::RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_display_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect) +{ + // Perform CPU side clipping for single clipped element to avoid using scissor state + ImVec2 pos = pos_min; + const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f); + + const ImVec2* clip_min = clip_rect ? &clip_rect->Min : &pos_min; + const ImVec2* clip_max = clip_rect ? &clip_rect->Max : &pos_max; + bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y); + if (clip_rect) // If we had no explicit clipping rectangle then pos==clip_min + need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y); + + // Align whole block. We should defer that to the better rendering function when we'll have support for individual line alignment. + if (align.x > 0.0f) pos.x = ImMax(pos.x, pos.x + (pos_max.x - pos.x - text_size.x) * align.x); + if (align.y > 0.0f) pos.y = ImMax(pos.y, pos.y + (pos_max.y - pos.y - text_size.y) * align.y); + + // Render + if (need_clipping) + { + ImVec4 fine_clip_rect(clip_min->x, clip_min->y, clip_max->x, clip_max->y); + draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, &fine_clip_rect); + } + else + { + draw_list->AddText(NULL, 0.0f, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, NULL); + } +} + +void ImGui::RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect) +{ + // Hide anything after a '##' string + const char* text_display_end = FindRenderedTextEnd(text, text_end); + const int text_len = (int)(text_display_end - text); + if (text_len == 0) + return; + + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + RenderTextClippedEx(window->DrawList, pos_min, pos_max, text, text_display_end, text_size_if_known, align, clip_rect); + if (g.LogEnabled) + LogRenderedText(&pos_min, text, text_display_end); +} + + +// Another overly complex function until we reorganize everything into a nice all-in-one helper. +// This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) which define _where_ the ellipsis is, from actual clipping of text and limit of the ellipsis display. +// This is because in the context of tabs we selectively hide part of the text when the Close Button appears, but we don't want the ellipsis to move. +void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end_full, const ImVec2* text_size_if_known) +{ + ImGuiContext& g = *GImGui; + if (text_end_full == NULL) + text_end_full = FindRenderedTextEnd(text); + const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_end_full, false, 0.0f); + + //draw_list->AddLine(ImVec2(pos_max.x, pos_min.y - 4), ImVec2(pos_max.x, pos_max.y + 4), IM_COL32(0, 0, 255, 255)); + //draw_list->AddLine(ImVec2(ellipsis_max_x, pos_min.y-2), ImVec2(ellipsis_max_x, pos_max.y+2), IM_COL32(0, 255, 0, 255)); + //draw_list->AddLine(ImVec2(clip_max_x, pos_min.y), ImVec2(clip_max_x, pos_max.y), IM_COL32(255, 0, 0, 255)); + // FIXME: We could technically remove (last_glyph->AdvanceX - last_glyph->X1) from text_size.x here and save a few pixels. + if (text_size.x > pos_max.x - pos_min.x) + { + // Hello wo... + // | | | + // min max ellipsis_max + // <-> this is generally some padding value + + const ImFont* font = draw_list->_Data->Font; + const float font_size = draw_list->_Data->FontSize; + const char* text_end_ellipsis = NULL; + + ImWchar ellipsis_char = font->EllipsisChar; + int ellipsis_char_count = 1; + if (ellipsis_char == (ImWchar)-1) + { + ellipsis_char = (ImWchar)'.'; + ellipsis_char_count = 3; + } + const ImFontGlyph* glyph = font->FindGlyph(ellipsis_char); + + float ellipsis_glyph_width = glyph->X1; // Width of the glyph with no padding on either side + float ellipsis_total_width = ellipsis_glyph_width; // Full width of entire ellipsis + + if (ellipsis_char_count > 1) + { + // Full ellipsis size without free spacing after it. + const float spacing_between_dots = 1.0f * (draw_list->_Data->FontSize / font->FontSize); + ellipsis_glyph_width = glyph->X1 - glyph->X0 + spacing_between_dots; + ellipsis_total_width = ellipsis_glyph_width * (float)ellipsis_char_count - spacing_between_dots; + } + + // We can now claim the space between pos_max.x and ellipsis_max.x + const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_total_width) - pos_min.x, 1.0f); + float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x; + if (text == text_end_ellipsis && text_end_ellipsis < text_end_full) + { + // Always display at least 1 character if there's no room for character + ellipsis + text_end_ellipsis = text + ImTextCountUtf8BytesFromChar(text, text_end_full); + text_size_clipped_x = font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text, text_end_ellipsis).x; + } + while (text_end_ellipsis > text && ImCharIsBlankA(text_end_ellipsis[-1])) + { + // Trim trailing space before ellipsis (FIXME: Supporting non-ascii blanks would be nice, for this we need a function to backtrack in UTF-8 text) + text_end_ellipsis--; + text_size_clipped_x -= font->CalcTextSizeA(font_size, FLT_MAX, 0.0f, text_end_ellipsis, text_end_ellipsis + 1).x; // Ascii blanks are always 1 byte + } + + // Render text, render ellipsis + RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f)); + float ellipsis_x = pos_min.x + text_size_clipped_x; + if (ellipsis_x + ellipsis_total_width <= ellipsis_max_x) + for (int i = 0; i < ellipsis_char_count; i++) + { + font->RenderChar(draw_list, font_size, ImVec2(ellipsis_x, pos_min.y), GetColorU32(ImGuiCol_Text), ellipsis_char); + ellipsis_x += ellipsis_glyph_width; + } + } + else + { + RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_full, &text_size, ImVec2(0.0f, 0.0f)); + } + + if (g.LogEnabled) + LogRenderedText(&pos_min, text, text_end_full); +} + +// Render a rectangle shaped with optional rounding and borders +void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding); + const float border_size = g.Style.FrameBorderSize; + if (border && border_size > 0.0f) + { + window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + } +} + +void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + const float border_size = g.Style.FrameBorderSize; + if (border_size > 0.0f) + { + window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + } +} + +// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state +void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale) +{ + const float h = draw_list->_Data->FontSize * 1.00f; + float r = h * 0.40f * scale; + ImVec2 center = pos + ImVec2(h * 0.50f, h * 0.50f * scale); + + ImVec2 a, b, c; + switch (dir) + { + case ImGuiDir_Up: + case ImGuiDir_Down: + if (dir == ImGuiDir_Up) r = -r; + a = ImVec2(+0.000f,+0.750f) * r; + b = ImVec2(-0.866f,-0.750f) * r; + c = ImVec2(+0.866f,-0.750f) * r; + break; + case ImGuiDir_Left: + case ImGuiDir_Right: + if (dir == ImGuiDir_Left) r = -r; + a = ImVec2(+0.750f,+0.000f) * r; + b = ImVec2(-0.750f,+0.866f) * r; + c = ImVec2(-0.750f,-0.866f) * r; + break; + case ImGuiDir_None: + case ImGuiDir_COUNT: + IM_ASSERT(0); + break; + } + draw_list->AddTriangleFilled(center + a, center + b, center + c, col); +} + +void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col) +{ + draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8); +} + +void ImGui::RenderCheckMark(ImVec2 pos, ImU32 col, float sz) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + float thickness = ImMax(sz / 5.0f, 1.0f); + sz -= thickness*0.5f; + pos += ImVec2(thickness*0.25f, thickness*0.25f); + + float third = sz / 3.0f; + float bx = pos.x + third; + float by = pos.y + sz - third*0.5f; + window->DrawList->PathLineTo(ImVec2(bx - third, by - third)); + window->DrawList->PathLineTo(ImVec2(bx, by)); + window->DrawList->PathLineTo(ImVec2(bx + third*2, by - third*2)); + window->DrawList->PathStroke(col, false, thickness); +} + +void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags) +{ + ImGuiContext& g = *GImGui; + if (id != g.NavId) + return; + if (g.NavDisableHighlight && !(flags & ImGuiNavHighlightFlags_AlwaysDraw)) + return; + ImGuiWindow* window = g.CurrentWindow; + if (window->DC.NavHideHighlightOneFrame) + return; + + float rounding = (flags & ImGuiNavHighlightFlags_NoRounding) ? 0.0f : g.Style.FrameRounding; + ImRect display_rect = bb; + display_rect.ClipWith(window->ClipRect); + if (flags & ImGuiNavHighlightFlags_TypeDefault) + { + const float THICKNESS = 2.0f; + const float DISTANCE = 3.0f + THICKNESS * 0.5f; + display_rect.Expand(ImVec2(DISTANCE,DISTANCE)); + bool fully_visible = window->ClipRect.Contains(display_rect); + if (!fully_visible) + window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); + window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), display_rect.Max - ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, ImDrawCornerFlags_All, THICKNESS); + if (!fully_visible) + window->DrawList->PopClipRect(); + } + if (flags & ImGuiNavHighlightFlags_TypeThin) + { + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, ~0, 1.0f); + } +} + +//----------------------------------------------------------------------------- +// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +//----------------------------------------------------------------------------- + +// ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods +ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) + : DrawListInst(&context->DrawListSharedData) +{ + Name = ImStrdup(name); + ID = ImHashStr(name); + IDStack.push_back(ID); + Flags = ImGuiWindowFlags_None; + Pos = ImVec2(0.0f, 0.0f); + Size = SizeFull = ImVec2(0.0f, 0.0f); + ContentSize = ContentSizeExplicit = ImVec2(0.0f, 0.0f); + WindowPadding = ImVec2(0.0f, 0.0f); + WindowRounding = 0.0f; + WindowBorderSize = 0.0f; + NameBufLen = (int)strlen(name) + 1; + MoveId = GetID("#MOVE"); + ChildId = 0; + Scroll = ImVec2(0.0f, 0.0f); + ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); + ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); + ScrollbarSizes = ImVec2(0.0f, 0.0f); + ScrollbarX = ScrollbarY = false; + Active = WasActive = false; + WriteAccessed = false; + Collapsed = false; + WantCollapseToggle = false; + SkipItems = false; + Appearing = false; + Hidden = false; + HasCloseButton = false; + ResizeBorderHeld = -1; + BeginCount = 0; + BeginOrderWithinParent = -1; + BeginOrderWithinContext = -1; + PopupId = 0; + AutoFitFramesX = AutoFitFramesY = -1; + AutoFitChildAxises = 0x00; + AutoFitOnlyGrows = false; + AutoPosLastDirection = ImGuiDir_None; + HiddenFramesCanSkipItems = HiddenFramesCannotSkipItems = 0; + SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; + SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); + + InnerRect = ImRect(0.0f, 0.0f, 0.0f, 0.0f); // Clear so the InnerRect.GetSize() code in Begin() doesn't lead to overflow even if the result isn't used. + + LastFrameActive = -1; + LastTimeActive = -1.0f; + ItemWidthDefault = 0.0f; + FontWindowScale = 1.0f; + SettingsIdx = -1; + + DrawList = &DrawListInst; + DrawList->_OwnerName = Name; + ParentWindow = NULL; + RootWindow = NULL; + RootWindowForTitleBarHighlight = NULL; + RootWindowForNav = NULL; + + NavLastIds[0] = NavLastIds[1] = 0; + NavRectRel[0] = NavRectRel[1] = ImRect(); + NavLastChildNavWindow = NULL; + + MemoryCompacted = false; + MemoryDrawListIdxCapacity = MemoryDrawListVtxCapacity = 0; +} + +ImGuiWindow::~ImGuiWindow() +{ + IM_ASSERT(DrawList == &DrawListInst); + IM_DELETE(Name); + for (int i = 0; i != ColumnsStorage.Size; i++) + ColumnsStorage[i].~ImGuiColumns(); +} + +ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); + ImGui::KeepAliveID(id); + return id; +} + +ImGuiID ImGuiWindow::GetID(const void* ptr) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); + ImGui::KeepAliveID(id); + return id; +} + +ImGuiID ImGuiWindow::GetID(int n) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&n, sizeof(n), seed); + ImGui::KeepAliveID(id); + return id; +} + +ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) +{ + ImGuiID seed = IDStack.back(); + return ImHashStr(str, str_end ? (str_end - str) : 0, seed); +} + +ImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr) +{ + ImGuiID seed = IDStack.back(); + return ImHashData(&ptr, sizeof(void*), seed); +} + +ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n) +{ + ImGuiID seed = IDStack.back(); + return ImHashData(&n, sizeof(n), seed); +} + +// This is only used in rare/specific situations to manufacture an ID out of nowhere. +ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) +{ + ImGuiID seed = IDStack.back(); + const int r_rel[4] = { (int)(r_abs.Min.x - Pos.x), (int)(r_abs.Min.y - Pos.y), (int)(r_abs.Max.x - Pos.x), (int)(r_abs.Max.y - Pos.y) }; + ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); + ImGui::KeepAliveID(id); + return id; +} + +static void SetCurrentWindow(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + g.CurrentWindow = window; + if (window) + g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); +} + +// Free up/compact internal window buffers, we can use this when a window becomes unused. +// This is currently unused by the library, but you may call this yourself for easy GC. +// Not freed: +// - ImGuiWindow, ImGuiWindowSettings, Name +// - StateStorage, ColumnsStorage (may hold useful data) +// This should have no noticeable visual effect. When the window reappear however, expect new allocation/buffer growth/copy cost. +void ImGui::GcCompactTransientWindowBuffers(ImGuiWindow* window) +{ + window->MemoryCompacted = true; + window->MemoryDrawListIdxCapacity = window->DrawList->IdxBuffer.Capacity; + window->MemoryDrawListVtxCapacity = window->DrawList->VtxBuffer.Capacity; + window->IDStack.clear(); + window->DrawList->ClearFreeMemory(); + window->DC.ChildWindows.clear(); + window->DC.ItemFlagsStack.clear(); + window->DC.ItemWidthStack.clear(); + window->DC.TextWrapPosStack.clear(); + window->DC.GroupStack.clear(); +} + +void ImGui::GcAwakeTransientWindowBuffers(ImGuiWindow* window) +{ + // We stored capacity of the ImDrawList buffer to reduce growth-caused allocation/copy when awakening. + // The other buffers tends to amortize much faster. + window->MemoryCompacted = false; + window->DrawList->IdxBuffer.reserve(window->MemoryDrawListIdxCapacity); + window->DrawList->VtxBuffer.reserve(window->MemoryDrawListVtxCapacity); + window->MemoryDrawListIdxCapacity = window->MemoryDrawListVtxCapacity = 0; +} + +void ImGui::SetNavID(ImGuiID id, int nav_layer) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavWindow); + IM_ASSERT(nav_layer == 0 || nav_layer == 1); + g.NavId = id; + g.NavWindow->NavLastIds[nav_layer] = id; +} + +void ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel) +{ + ImGuiContext& g = *GImGui; + SetNavID(id, nav_layer); + g.NavWindow->NavRectRel[nav_layer] = rect_rel; + g.NavMousePosDirty = true; + g.NavDisableHighlight = false; + g.NavDisableMouseHover = true; +} + +void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + g.ActiveIdIsJustActivated = (g.ActiveId != id); + if (g.ActiveIdIsJustActivated) + { + g.ActiveIdTimer = 0.0f; + g.ActiveIdHasBeenPressedBefore = false; + g.ActiveIdHasBeenEditedBefore = false; + if (id != 0) + { + g.LastActiveId = id; + g.LastActiveIdTimer = 0.0f; + } + } + g.ActiveId = id; + g.ActiveIdAllowOverlap = false; + g.ActiveIdWindow = window; + g.ActiveIdHasBeenEditedThisFrame = false; + if (id) + { + g.ActiveIdIsAlive = id; + g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + } + + // Clear declaration of inputs claimed by the widget + // (Please note that this is WIP and not all keys/inputs are thoroughly declared by all widgets yet) + g.ActiveIdUsingNavDirMask = 0x00; + g.ActiveIdUsingNavInputMask = 0x00; + g.ActiveIdUsingKeyInputMask = 0x00; +} + +// FIXME-NAV: The existence of SetNavID/SetNavIDWithRectRel/SetFocusID is incredibly messy and confusing and needs some explanation or refactoring. +void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(id != 0); + + // Assume that SetFocusID() is called in the context where its NavLayer is the current layer, which is the case everywhere we call it. + const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; + if (g.NavWindow != window) + g.NavInitRequest = false; + g.NavId = id; + g.NavWindow = window; + g.NavLayer = nav_layer; + window->NavLastIds[nav_layer] = id; + if (window->DC.LastItemId == id) + window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos); + + if (g.ActiveIdSource == ImGuiInputSource_Nav) + g.NavDisableMouseHover = true; + else + g.NavDisableHighlight = true; +} + +void ImGui::ClearActiveID() +{ + SetActiveID(0, NULL); +} + +void ImGui::SetHoveredID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.HoveredId = id; + g.HoveredIdAllowOverlap = false; + if (id != 0 && g.HoveredIdPreviousFrame != id) + g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f; +} + +ImGuiID ImGui::GetHoveredID() +{ + ImGuiContext& g = *GImGui; + return g.HoveredId ? g.HoveredId : g.HoveredIdPreviousFrame; +} + +void ImGui::KeepAliveID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + if (g.ActiveId == id) + g.ActiveIdIsAlive = id; + if (g.ActiveIdPreviousFrame == id) + g.ActiveIdPreviousFrameIsAlive = true; +} + +void ImGui::MarkItemEdited(ImGuiID id) +{ + // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). + // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need need to fill the data. + ImGuiContext& g = *GImGui; + IM_ASSERT(g.ActiveId == id || g.ActiveId == 0 || g.DragDropActive); + IM_UNUSED(id); // Avoid unused variable warnings when asserts are compiled out. + //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id); + g.ActiveIdHasBeenEditedThisFrame = true; + g.ActiveIdHasBeenEditedBefore = true; + g.CurrentWindow->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; +} + +static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags) +{ + // An active popup disable hovering on other windows (apart from its own children) + // FIXME-OPT: This could be cached/stored within the window. + ImGuiContext& g = *GImGui; + if (g.NavWindow) + if (ImGuiWindow* focused_root_window = g.NavWindow->RootWindow) + if (focused_root_window->WasActive && focused_root_window != window->RootWindow) + { + // For the purpose of those flags we differentiate "standard popup" from "modal popup" + // NB: The order of those two tests is important because Modal windows are also Popups. + if (focused_root_window->Flags & ImGuiWindowFlags_Modal) + return false; + if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + return false; + } + + return true; +} + +// Advance cursor given item size for layout. +void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + // Always align ourselves on pixel boundaries + const float line_height = ImMax(window->DC.CurrLineSize.y, size.y); + //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] + window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; + window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y; + window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line + window->DC.CursorPos.y = (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y); // Next line + window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); + //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] + + window->DC.PrevLineSize.y = line_height; + window->DC.CurrLineSize.y = 0.0f; + window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); + window->DC.CurrLineTextBaseOffset = 0.0f; + + // Horizontal layout mode + if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) + SameLine(); +} + +void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) +{ + ItemSize(bb.GetSize(), text_baseline_y); +} + +// Declare item bounding box for clipping and interaction. +// Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface +// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). +bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (id != 0) + { + // Navigation processing runs prior to clipping early-out + // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget + // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests + // unfortunately, but it is still limited to one window. It may not scale very well for windows with ten of + // thousands of item, but at least NavMoveRequest is only set on user interaction, aka maximum once a frame. + // We could early out with "if (is_clipped && !g.NavInitRequest) return false;" but when we wouldn't be able + // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). + // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. + // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. + window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask; + if (g.NavId == id || g.NavAnyRequest) + if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) + if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) + NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id); + + // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() +#ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX + if (id == g.DebugItemPickerBreakID) + { + IM_DEBUG_BREAK(); + g.DebugItemPickerBreakID = 0; + } +#endif + } + + window->DC.LastItemId = id; + window->DC.LastItemRect = bb; + window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; + g.NextItemData.Flags = ImGuiNextItemDataFlags_None; + +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (id != 0) + IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id); +#endif + + // Clipping test + const bool is_clipped = IsClippedEx(bb, id, false); + if (is_clipped) + return false; + //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] + + // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) + if (IsMouseHoveringRect(bb.Min, bb.Max)) + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect; + return true; +} + +// This is roughly matching the behavior of internal-facing ItemHoverable() +// - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered() +// - this should work even for non-interactive items that have no ID, so we cannot use LastItemId +bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (g.NavDisableMouseHover && !g.NavDisableHighlight) + return IsItemFocused(); + + // Test for bounding box overlap, as updated as ItemAdd() + if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) + return false; + IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0); // Flags not supported by this function + + // Test if we are hovering the right window (our window could be behind another window) + // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable to use IsItemHovered() after EndChild() itself. + // Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was the test that has been running for a long while. + //if (g.HoveredWindow != window) + // return false; + if (g.HoveredRootWindow != window->RootWindow && !(flags & ImGuiHoveredFlags_AllowWhenOverlapped)) + return false; + + // Test if another item is active (e.g. being dragged) + if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) + if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) + return false; + + // Test if interactions on this window are blocked by an active popup or modal. + // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. + if (!IsWindowContentHoverable(window, flags)) + return false; + + // Test if the item is disabled + if ((window->DC.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + return false; + + // Special handling for the dummy item after Begin() which represent the title bar or tab. + // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. + if (window->DC.LastItemId == window->MoveId && window->WriteAccessed) + return false; + return true; +} + +// Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered(). +bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) +{ + ImGuiContext& g = *GImGui; + if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap) + return false; + + ImGuiWindow* window = g.CurrentWindow; + if (g.HoveredWindow != window) + return false; + if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) + return false; + if (!IsMouseHoveringRect(bb.Min, bb.Max)) + return false; + if (g.NavDisableMouseHover || !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) + return false; + if (window->DC.ItemFlags & ImGuiItemFlags_Disabled) + return false; + + SetHoveredID(id); + + // [DEBUG] Item Picker tool! + // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making + // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered + // items if we perform the test in ItemAdd(), but that would incur a small runtime cost. + // #define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX in imconfig.h if you want this check to also be performed in ItemAdd(). + if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) + GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); + if (g.DebugItemPickerBreakID == id) + IM_DEBUG_BREAK(); + + return true; +} + +bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (!bb.Overlaps(window->ClipRect)) + if (id == 0 || id != g.ActiveId) + if (clip_even_when_logged || !g.LogEnabled) + return true; + return false; +} + +// Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. +bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) +{ + ImGuiContext& g = *GImGui; + + // Increment counters + const bool is_tab_stop = (window->DC.ItemFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; + window->DC.FocusCounterAll++; + if (is_tab_stop) + window->DC.FocusCounterTab++; + + // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. + // (Note that we can always TAB out of a widget that doesn't allow tabbing in) + if (g.ActiveId == id && g.FocusTabPressed && !IsActiveIdUsingKey(ImGuiKey_Tab) && g.FocusRequestNextWindow == NULL) + { + g.FocusRequestNextWindow = window; + g.FocusRequestNextCounterTab = window->DC.FocusCounterTab + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. + } + + // Handle focus requests + if (g.FocusRequestCurrWindow == window) + { + if (window->DC.FocusCounterAll == g.FocusRequestCurrCounterAll) + return true; + if (is_tab_stop && window->DC.FocusCounterTab == g.FocusRequestCurrCounterTab) + { + g.NavJustTabbedId = id; + return true; + } + + // If another item is about to be focused, we clear our own active id + if (g.ActiveId == id) + ClearActiveID(); + } + + return false; +} + +void ImGui::FocusableItemUnregister(ImGuiWindow* window) +{ + window->DC.FocusCounterAll--; + window->DC.FocusCounterTab--; +} + +float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) +{ + if (wrap_pos_x < 0.0f) + return 0.0f; + + ImGuiWindow* window = GImGui->CurrentWindow; + if (wrap_pos_x == 0.0f) + wrap_pos_x = window->WorkRect.Max.x; + else if (wrap_pos_x > 0.0f) + wrap_pos_x += window->Pos.x - window->Scroll.x; // wrap_pos_x is provided is window local space + + return ImMax(wrap_pos_x - pos.x, 1.0f); +} + +// IM_ALLOC() == ImGui::MemAlloc() +void* ImGui::MemAlloc(size_t size) +{ + if (ImGuiContext* ctx = GImGui) + ctx->IO.MetricsActiveAllocations++; + return GImAllocatorAllocFunc(size, GImAllocatorUserData); +} + +// IM_FREE() == ImGui::MemFree() +void ImGui::MemFree(void* ptr) +{ + if (ptr) + if (ImGuiContext* ctx = GImGui) + ctx->IO.MetricsActiveAllocations--; + return GImAllocatorFreeFunc(ptr, GImAllocatorUserData); +} + +const char* ImGui::GetClipboardText() +{ + ImGuiContext& g = *GImGui; + return g.IO.GetClipboardTextFn ? g.IO.GetClipboardTextFn(g.IO.ClipboardUserData) : ""; +} + +void ImGui::SetClipboardText(const char* text) +{ + ImGuiContext& g = *GImGui; + if (g.IO.SetClipboardTextFn) + g.IO.SetClipboardTextFn(g.IO.ClipboardUserData, text); +} + +const char* ImGui::GetVersion() +{ + return IMGUI_VERSION; +} + +// Internal state access - if you want to share Dear ImGui state between modules (e.g. DLL) or allocate it yourself +// Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module +ImGuiContext* ImGui::GetCurrentContext() +{ + return GImGui; +} + +void ImGui::SetCurrentContext(ImGuiContext* ctx) +{ +#ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC + IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. +#else + GImGui = ctx; +#endif +} + +// Helper function to verify ABI compatibility between caller code and compiled version of Dear ImGui. +// Verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit +// If the user has inconsistent compilation settings, imgui configuration #define, packing pragma, etc. your user code +// may see different structures than what imgui.cpp sees, which is problematic. +// We usually require settings to be in imconfig.h to make sure that they are accessible to all compilation units involved with Dear ImGui. +bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx) +{ + bool error = false; + if (strcmp(version, IMGUI_VERSION)!=0) { error = true; IM_ASSERT(strcmp(version,IMGUI_VERSION)==0 && "Mismatched version string!"); } + if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } + if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); } + if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } + if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } + if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } + if (sz_idx != sizeof(ImDrawIdx)) { error = true; IM_ASSERT(sz_idx == sizeof(ImDrawIdx) && "Mismatched struct layout!"); } + return !error; +} + +void ImGui::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data) +{ + GImAllocatorAllocFunc = alloc_func; + GImAllocatorFreeFunc = free_func; + GImAllocatorUserData = user_data; +} + +ImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas) +{ + ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas); + if (GImGui == NULL) + SetCurrentContext(ctx); + Initialize(ctx); + return ctx; +} + +void ImGui::DestroyContext(ImGuiContext* ctx) +{ + if (ctx == NULL) + ctx = GImGui; + Shutdown(ctx); + if (GImGui == ctx) + SetCurrentContext(NULL); + IM_DELETE(ctx); +} + +ImGuiIO& ImGui::GetIO() +{ + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); + return GImGui->IO; +} + +ImGuiStyle& ImGui::GetStyle() +{ + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); + return GImGui->Style; +} + +// Same value as passed to the old io.RenderDrawListsFn function. Valid after Render() and until the next call to NewFrame() +ImDrawData* ImGui::GetDrawData() +{ + ImGuiContext& g = *GImGui; + return g.DrawData.Valid ? &g.DrawData : NULL; +} + +double ImGui::GetTime() +{ + return GImGui->Time; +} + +int ImGui::GetFrameCount() +{ + return GImGui->FrameCount; +} + +ImDrawList* ImGui::GetBackgroundDrawList() +{ + return &GImGui->BackgroundDrawList; +} + +ImDrawList* ImGui::GetForegroundDrawList() +{ + return &GImGui->ForegroundDrawList; +} + +ImDrawListSharedData* ImGui::GetDrawListSharedData() +{ + return &GImGui->DrawListSharedData; +} + +void ImGui::StartMouseMovingWindow(ImGuiWindow* window) +{ + // Set ActiveId even if the _NoMove flag is set. Without it, dragging away from a window with _NoMove would activate hover on other windows. + // We _also_ call this when clicking in a window empty space when io.ConfigWindowsMoveFromTitleBarOnly is set, but clear g.MovingWindow afterward. + // This is because we want ActiveId to be set even when the window is not permitted to move. + ImGuiContext& g = *GImGui; + FocusWindow(window); + SetActiveID(window->MoveId, window); + g.NavDisableHighlight = true; + g.ActiveIdClickOffset = g.IO.MousePos - window->RootWindow->Pos; + + bool can_move_window = true; + if ((window->Flags & ImGuiWindowFlags_NoMove) || (window->RootWindow->Flags & ImGuiWindowFlags_NoMove)) + can_move_window = false; + if (can_move_window) + g.MovingWindow = window; +} + +// Handle mouse moving window +// Note: moving window with the navigation keys (Square + d-pad / CTRL+TAB + Arrows) are processed in NavUpdateWindowing() +void ImGui::UpdateMouseMovingWindowNewFrame() +{ + ImGuiContext& g = *GImGui; + if (g.MovingWindow != NULL) + { + // We actually want to move the root window. g.MovingWindow == window we clicked on (could be a child window). + // We track it to preserve Focus and so that generally ActiveIdWindow == MovingWindow and ActiveId == MovingWindow->MoveId for consistency. + KeepAliveID(g.ActiveId); + IM_ASSERT(g.MovingWindow && g.MovingWindow->RootWindow); + ImGuiWindow* moving_window = g.MovingWindow->RootWindow; + if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos)) + { + ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset; + if (moving_window->Pos.x != pos.x || moving_window->Pos.y != pos.y) + { + MarkIniSettingsDirty(moving_window); + SetWindowPos(moving_window, pos, ImGuiCond_Always); + } + FocusWindow(g.MovingWindow); + } + else + { + ClearActiveID(); + g.MovingWindow = NULL; + } + } + else + { + // When clicking/dragging from a window that has the _NoMove flag, we still set the ActiveId in order to prevent hovering others. + if (g.ActiveIdWindow && g.ActiveIdWindow->MoveId == g.ActiveId) + { + KeepAliveID(g.ActiveId); + if (!g.IO.MouseDown[0]) + ClearActiveID(); + } + } +} + +// Initiate moving window, handle left-click and right-click focus +void ImGui::UpdateMouseMovingWindowEndFrame() +{ + // Initiate moving window + ImGuiContext& g = *GImGui; + if (g.ActiveId != 0 || g.HoveredId != 0) + return; + + // Unless we just made a window/popup appear + if (g.NavWindow && g.NavWindow->Appearing) + return; + + // Click to focus window and start moving (after we're done with all our widgets) + if (g.IO.MouseClicked[0]) + { + if (g.HoveredRootWindow != NULL) + { + StartMouseMovingWindow(g.HoveredWindow); + if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoTitleBar)) + if (!g.HoveredRootWindow->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) + g.MovingWindow = NULL; + } + else if (g.NavWindow != NULL && GetTopMostPopupModal() == NULL) + { + // Clicking on void disable focus + FocusWindow(NULL); + } + } + + // With right mouse button we close popups without changing focus based on where the mouse is aimed + // Instead, focus will be restored to the window under the bottom-most closed popup. + // (The left mouse button path calls FocusWindow on the hovered window, which will lead NewFrame->ClosePopupsOverWindow to trigger) + if (g.IO.MouseClicked[1]) + { + // Find the top-most window between HoveredWindow and the top-most Modal Window. + // This is where we can trim the popup stack. + ImGuiWindow* modal = GetTopMostPopupModal(); + bool hovered_window_above_modal = false; + if (modal == NULL) + hovered_window_above_modal = true; + for (int i = g.Windows.Size - 1; i >= 0 && hovered_window_above_modal == false; i--) + { + ImGuiWindow* window = g.Windows[i]; + if (window == modal) + break; + if (window == g.HoveredWindow) + hovered_window_above_modal = true; + } + ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true); + } +} + +static bool IsWindowActiveAndVisible(ImGuiWindow* window) +{ + return (window->Active) && (!window->Hidden); +} + +static void ImGui::UpdateMouseInputs() +{ + ImGuiContext& g = *GImGui; + + // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) + if (IsMousePosValid(&g.IO.MousePos)) + g.IO.MousePos = g.LastValidMousePos = ImFloor(g.IO.MousePos); + + // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta + if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev)) + g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; + else + g.IO.MouseDelta = ImVec2(0.0f, 0.0f); + if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f) + g.NavDisableMouseHover = false; + + g.IO.MousePosPrev = g.IO.MousePos; + for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) + { + g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f; + g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f; + g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i]; + g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f; + g.IO.MouseDoubleClicked[i] = false; + if (g.IO.MouseClicked[i]) + { + if ((float)(g.Time - g.IO.MouseClickedTime[i]) < g.IO.MouseDoubleClickTime) + { + ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); + if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) + g.IO.MouseDoubleClicked[i] = true; + g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click + } + else + { + g.IO.MouseClickedTime[i] = g.Time; + } + g.IO.MouseClickedPos[i] = g.IO.MousePos; + g.IO.MouseDownWasDoubleClick[i] = g.IO.MouseDoubleClicked[i]; + g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f); + g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; + } + else if (g.IO.MouseDown[i]) + { + // Maintain the maximum distance we reaching from the initial click position, which is used with dragging threshold + ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); + g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(delta_from_click_pos)); + g.IO.MouseDragMaxDistanceAbs[i].x = ImMax(g.IO.MouseDragMaxDistanceAbs[i].x, delta_from_click_pos.x < 0.0f ? -delta_from_click_pos.x : delta_from_click_pos.x); + g.IO.MouseDragMaxDistanceAbs[i].y = ImMax(g.IO.MouseDragMaxDistanceAbs[i].y, delta_from_click_pos.y < 0.0f ? -delta_from_click_pos.y : delta_from_click_pos.y); + } + if (!g.IO.MouseDown[i] && !g.IO.MouseReleased[i]) + g.IO.MouseDownWasDoubleClick[i] = false; + if (g.IO.MouseClicked[i]) // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation + g.NavDisableMouseHover = false; + } +} + +static void StartLockWheelingWindow(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (g.WheelingWindow == window) + return; + g.WheelingWindow = window; + g.WheelingWindowRefMousePos = g.IO.MousePos; + g.WheelingWindowTimer = WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER; +} + +void ImGui::UpdateMouseWheel() +{ + ImGuiContext& g = *GImGui; + + // Reset the locked window if we move the mouse or after the timer elapses + if (g.WheelingWindow != NULL) + { + g.WheelingWindowTimer -= g.IO.DeltaTime; + if (IsMousePosValid() && ImLengthSqr(g.IO.MousePos - g.WheelingWindowRefMousePos) > g.IO.MouseDragThreshold * g.IO.MouseDragThreshold) + g.WheelingWindowTimer = 0.0f; + if (g.WheelingWindowTimer <= 0.0f) + { + g.WheelingWindow = NULL; + g.WheelingWindowTimer = 0.0f; + } + } + + if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f) + return; + + ImGuiWindow* window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow; + if (!window || window->Collapsed) + return; + + // Zoom / Scale window + // FIXME-OBSOLETE: This is an old feature, it still works but pretty much nobody is using it and may be best redesigned. + if (g.IO.MouseWheel != 0.0f && g.IO.KeyCtrl && g.IO.FontAllowUserScaling) + { + StartLockWheelingWindow(window); + const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); + const float scale = new_font_scale / window->FontWindowScale; + window->FontWindowScale = new_font_scale; + if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) + { + const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; + SetWindowPos(window, window->Pos + offset, 0); + window->Size = ImFloor(window->Size * scale); + window->SizeFull = ImFloor(window->SizeFull * scale); + } + return; + } + + // Mouse wheel scrolling + // If a child window has the ImGuiWindowFlags_NoScrollWithMouse flag, we give a chance to scroll its parent + + // Vertical Mouse Wheel scrolling + const float wheel_y = (g.IO.MouseWheel != 0.0f && !g.IO.KeyShift) ? g.IO.MouseWheel : 0.0f; + if (wheel_y != 0.0f && !g.IO.KeyCtrl) + { + StartLockWheelingWindow(window); + while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.y == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) + window = window->ParentWindow; + if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)) + { + float max_step = window->InnerRect.GetHeight() * 0.67f; + float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step)); + SetScrollY(window, window->Scroll.y - wheel_y * scroll_step); + } + } + + // Horizontal Mouse Wheel scrolling, or Vertical Mouse Wheel w/ Shift held + const float wheel_x = (g.IO.MouseWheelH != 0.0f && !g.IO.KeyShift) ? g.IO.MouseWheelH : (g.IO.MouseWheel != 0.0f && g.IO.KeyShift) ? g.IO.MouseWheel : 0.0f; + if (wheel_x != 0.0f && !g.IO.KeyCtrl) + { + StartLockWheelingWindow(window); + while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.x == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) + window = window->ParentWindow; + if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)) + { + float max_step = window->InnerRect.GetWidth() * 0.67f; + float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step)); + SetScrollX(window, window->Scroll.x - wheel_x * scroll_step); + } + } +} + +// The reason this is exposed in imgui_internal.h is: on touch-based system that don't have hovering, we want to dispatch inputs to the right target (imgui vs imgui+app) +void ImGui::UpdateHoveredWindowAndCaptureFlags() +{ + ImGuiContext& g = *GImGui; + + // Find the window hovered by mouse: + // - Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow. + // - When moving a window we can skip the search, which also conveniently bypasses the fact that window->WindowRectClipped is lagging as this point of the frame. + // - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms. + FindHoveredWindow(); + + // Modal windows prevents cursor from hovering behind them. + ImGuiWindow* modal_window = GetTopMostPopupModal(); + if (modal_window) + if (g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window)) + g.HoveredRootWindow = g.HoveredWindow = NULL; + + // Disabled mouse? + if (g.IO.ConfigFlags & ImGuiConfigFlags_NoMouse) + g.HoveredWindow = g.HoveredRootWindow = NULL; + + // We track click ownership. When clicked outside of a window the click is owned by the application and won't report hovering nor request capture even while dragging over our windows afterward. + int mouse_earliest_button_down = -1; + bool mouse_any_down = false; + for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) + { + if (g.IO.MouseClicked[i]) + g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenPopupStack.empty()); + mouse_any_down |= g.IO.MouseDown[i]; + if (g.IO.MouseDown[i]) + if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[i] < g.IO.MouseClickedTime[mouse_earliest_button_down]) + mouse_earliest_button_down = i; + } + const bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down]; + + // If mouse was first clicked outside of ImGui bounds we also cancel out hovering. + // FIXME: For patterns of drag and drop across OS windows, we may need to rework/remove this test (first committed 311c0ca9 on 2015/02) + const bool mouse_dragging_extern_payload = g.DragDropActive && (g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) != 0; + if (!mouse_avail_to_imgui && !mouse_dragging_extern_payload) + g.HoveredWindow = g.HoveredRootWindow = NULL; + + // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to Dear ImGui + app) + if (g.WantCaptureMouseNextFrame != -1) + g.IO.WantCaptureMouse = (g.WantCaptureMouseNextFrame != 0); + else + g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (!g.OpenPopupStack.empty()); + + // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to imgui, false = dispatch keyboard info to Dear ImGui + app) + if (g.WantCaptureKeyboardNextFrame != -1) + g.IO.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); + else + g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); + if (g.IO.NavActive && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) + g.IO.WantCaptureKeyboard = true; + + // Update io.WantTextInput flag, this is to allow systems without a keyboard (e.g. mobile, hand-held) to show a software keyboard if possible + g.IO.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; +} + +static void NewFrameSanityChecks() +{ + ImGuiContext& g = *GImGui; + + // Check user data + // (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument) + IM_ASSERT(g.Initialized); + IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); + IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); + IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); + IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); + IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); + IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!"); + IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); + IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); + for (int n = 0; n < ImGuiKey_COUNT; n++) + IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && "io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)"); + + // Perform simple check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only recently added in 1.60 WIP) + if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) + IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); + + // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. + if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) + g.IO.ConfigWindowsResizeFromEdges = false; +} + +void ImGui::NewFrame() +{ + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); + ImGuiContext& g = *GImGui; + +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiTestEngineHook_PreNewFrame(&g); +#endif + + // Check and assert for various common IO and Configuration mistakes + NewFrameSanityChecks(); + + // Load settings on first frame (if not explicitly loaded manually before) + if (!g.SettingsLoaded) + { + IM_ASSERT(g.SettingsWindows.empty()); + if (g.IO.IniFilename) + LoadIniSettingsFromDisk(g.IO.IniFilename); + g.SettingsLoaded = true; + } + + // Save settings (with a delay after the last modification, so we don't spam disk too much) + if (g.SettingsDirtyTimer > 0.0f) + { + g.SettingsDirtyTimer -= g.IO.DeltaTime; + if (g.SettingsDirtyTimer <= 0.0f) + { + if (g.IO.IniFilename != NULL) + SaveIniSettingsToDisk(g.IO.IniFilename); + else + g.IO.WantSaveIniSettings = true; // Let user know they can call SaveIniSettingsToMemory(). user will need to clear io.WantSaveIniSettings themselves. + g.SettingsDirtyTimer = 0.0f; + } + } + + g.Time += g.IO.DeltaTime; + g.FrameScopeActive = true; + g.FrameCount += 1; + g.TooltipOverrideCount = 0; + g.WindowsActiveCount = 0; + + // Setup current font and draw list shared data + g.IO.Fonts->Locked = true; + SetCurrentFont(GetDefaultFont()); + IM_ASSERT(g.Font->IsLoaded()); + g.DrawListSharedData.ClipRectFullscreen = ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); + g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; + g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; + if (g.Style.AntiAliasedLines) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; + if (g.Style.AntiAliasedFill) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedFill; + if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; + + g.BackgroundDrawList.Clear(); + g.BackgroundDrawList.PushTextureID(g.IO.Fonts->TexID); + g.BackgroundDrawList.PushClipRectFullScreen(); + + g.ForegroundDrawList.Clear(); + g.ForegroundDrawList.PushTextureID(g.IO.Fonts->TexID); + g.ForegroundDrawList.PushClipRectFullScreen(); + + // Mark rendering data as invalid to prevent user who may have a handle on it to use it. + g.DrawData.Clear(); + + // Drag and drop keep the source ID alive so even if the source disappear our state is consistent + if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) + KeepAliveID(g.DragDropPayload.SourceId); + + // Clear reference to active widget if the widget isn't alive anymore + if (!g.HoveredIdPreviousFrame) + g.HoveredIdTimer = 0.0f; + if (!g.HoveredIdPreviousFrame || (g.HoveredId && g.ActiveId == g.HoveredId)) + g.HoveredIdNotActiveTimer = 0.0f; + if (g.HoveredId) + g.HoveredIdTimer += g.IO.DeltaTime; + if (g.HoveredId && g.ActiveId != g.HoveredId) + g.HoveredIdNotActiveTimer += g.IO.DeltaTime; + g.HoveredIdPreviousFrame = g.HoveredId; + g.HoveredId = 0; + g.HoveredIdAllowOverlap = false; + if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) + ClearActiveID(); + if (g.ActiveId) + g.ActiveIdTimer += g.IO.DeltaTime; + g.LastActiveIdTimer += g.IO.DeltaTime; + g.ActiveIdPreviousFrame = g.ActiveId; + g.ActiveIdPreviousFrameWindow = g.ActiveIdWindow; + g.ActiveIdPreviousFrameHasBeenEditedBefore = g.ActiveIdHasBeenEditedBefore; + g.ActiveIdIsAlive = 0; + g.ActiveIdHasBeenEditedThisFrame = false; + g.ActiveIdPreviousFrameIsAlive = false; + g.ActiveIdIsJustActivated = false; + if (g.TempInputTextId != 0 && g.ActiveId != g.TempInputTextId) + g.TempInputTextId = 0; + if (g.ActiveId == 0) + { + g.ActiveIdUsingNavDirMask = g.ActiveIdUsingNavInputMask = 0; + g.ActiveIdUsingKeyInputMask = 0; + } + + // Drag and drop + g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr; + g.DragDropAcceptIdCurr = 0; + g.DragDropAcceptIdCurrRectSurface = FLT_MAX; + g.DragDropWithinSourceOrTarget = false; + + // Update keyboard input state + memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); + for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) + g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f; + + // Update gamepad/keyboard directional navigation + NavUpdate(); + + // Update mouse input state + UpdateMouseInputs(); + + // Calculate frame-rate for the user, as a purely luxurious feature + g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; + g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; + g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); + g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; + + // Find hovered window + // (needs to be before UpdateMouseMovingWindowNewFrame so we fill g.HoveredWindowUnderMovingWindow on the mouse release frame) + UpdateHoveredWindowAndCaptureFlags(); + + // Handle user moving window with mouse (at the beginning of the frame to avoid input lag or sheering) + UpdateMouseMovingWindowNewFrame(); + + // Background darkening/whitening + if (GetTopMostPopupModal() != NULL || (g.NavWindowingTarget != NULL && g.NavWindowingHighlightAlpha > 0.0f)) + g.DimBgRatio = ImMin(g.DimBgRatio + g.IO.DeltaTime * 6.0f, 1.0f); + else + g.DimBgRatio = ImMax(g.DimBgRatio - g.IO.DeltaTime * 10.0f, 0.0f); + + g.MouseCursor = ImGuiMouseCursor_Arrow; + g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1; + g.PlatformImePos = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default + + // Mouse wheel scrolling, scale + UpdateMouseWheel(); + + // Pressing TAB activate widget focus + g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); + if (g.ActiveId == 0 && g.FocusTabPressed) + { + // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also + // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. + g.FocusRequestNextWindow = g.NavWindow; + g.FocusRequestNextCounterAll = INT_MAX; + if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) + g.FocusRequestNextCounterTab = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); + else + g.FocusRequestNextCounterTab = g.IO.KeyShift ? -1 : 0; + } + + // Turn queued focus request into current one + g.FocusRequestCurrWindow = NULL; + g.FocusRequestCurrCounterAll = g.FocusRequestCurrCounterTab = INT_MAX; + if (g.FocusRequestNextWindow != NULL) + { + ImGuiWindow* window = g.FocusRequestNextWindow; + g.FocusRequestCurrWindow = window; + if (g.FocusRequestNextCounterAll != INT_MAX && window->DC.FocusCounterAll != -1) + g.FocusRequestCurrCounterAll = ImModPositive(g.FocusRequestNextCounterAll, window->DC.FocusCounterAll + 1); + if (g.FocusRequestNextCounterTab != INT_MAX && window->DC.FocusCounterTab != -1) + g.FocusRequestCurrCounterTab = ImModPositive(g.FocusRequestNextCounterTab, window->DC.FocusCounterTab + 1); + g.FocusRequestNextWindow = NULL; + g.FocusRequestNextCounterAll = g.FocusRequestNextCounterTab = INT_MAX; + } + + g.NavIdTabCounter = INT_MAX; + + // Mark all windows as not visible and compact unused memory. + IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size); + const float memory_compact_start_time = (g.IO.ConfigWindowsMemoryCompactTimer >= 0.0f) ? (float)g.Time - g.IO.ConfigWindowsMemoryCompactTimer : FLT_MAX; + for (int i = 0; i != g.Windows.Size; i++) + { + ImGuiWindow* window = g.Windows[i]; + window->WasActive = window->Active; + window->BeginCount = 0; + window->Active = false; + window->WriteAccessed = false; + + // Garbage collect (this is totally functional but we may need decide if the side-effects are desirable) + if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) + GcCompactTransientWindowBuffers(window); + } + + // Closing the focused window restore focus to the first active root window in descending z-order + if (g.NavWindow && !g.NavWindow->WasActive) + FocusTopMostWindowUnderOne(NULL, NULL); + + // No window should be open at the beginning of the frame. + // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. + g.CurrentWindowStack.resize(0); + g.BeginPopupStack.resize(0); + ClosePopupsOverWindow(g.NavWindow, false); + + // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. + UpdateDebugToolItemPicker(); + + // Create implicit/fallback window - which we will only render it if the user has added something to it. + // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. + // This fallback is particularly important as it avoid ImGui:: calls from crashing. + SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver); + Begin("Debug##Default"); + g.FrameScopePushedImplicitWindow = true; + +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiTestEngineHook_PostNewFrame(&g); +#endif +} + +// [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. +void ImGui::UpdateDebugToolItemPicker() +{ + ImGuiContext& g = *GImGui; + g.DebugItemPickerBreakID = 0; + if (g.DebugItemPickerActive) + { + const ImGuiID hovered_id = g.HoveredIdPreviousFrame; + ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); + if (ImGui::IsKeyPressedMap(ImGuiKey_Escape)) + g.DebugItemPickerActive = false; + if (ImGui::IsMouseClicked(0) && hovered_id) + { + g.DebugItemPickerBreakID = hovered_id; + g.DebugItemPickerActive = false; + } + ImGui::SetNextWindowBgAlpha(0.60f); + ImGui::BeginTooltip(); + ImGui::Text("HoveredId: 0x%08X", hovered_id); + ImGui::Text("Press ESC to abort picking."); + ImGui::TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); + ImGui::EndTooltip(); + } +} + +void ImGui::Initialize(ImGuiContext* context) +{ + ImGuiContext& g = *context; + IM_ASSERT(!g.Initialized && !g.SettingsLoaded); + + // Add .ini handle for ImGuiWindow type + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "Window"; + ini_handler.TypeHash = ImHashStr("Window"); + ini_handler.ReadOpenFn = SettingsHandlerWindow_ReadOpen; + ini_handler.ReadLineFn = SettingsHandlerWindow_ReadLine; + ini_handler.WriteAllFn = SettingsHandlerWindow_WriteAll; + g.SettingsHandlers.push_back(ini_handler); + + g.Initialized = true; +} + +// This function is merely here to free heap allocations. +void ImGui::Shutdown(ImGuiContext* context) +{ + // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) + ImGuiContext& g = *context; + if (g.IO.Fonts && g.FontAtlasOwnedByContext) + { + g.IO.Fonts->Locked = false; + IM_DELETE(g.IO.Fonts); + } + g.IO.Fonts = NULL; + + // Cleanup of other data are conditional on actually having initialized Dear ImGui. + if (!g.Initialized) + return; + + // Save settings (unless we haven't attempted to load them: CreateContext/DestroyContext without a call to NewFrame shouldn't save an empty file) + if (g.SettingsLoaded && g.IO.IniFilename != NULL) + { + ImGuiContext* backup_context = GImGui; + SetCurrentContext(context); + SaveIniSettingsToDisk(g.IO.IniFilename); + SetCurrentContext(backup_context); + } + + // Clear everything else + for (int i = 0; i < g.Windows.Size; i++) + IM_DELETE(g.Windows[i]); + g.Windows.clear(); + g.WindowsFocusOrder.clear(); + g.WindowsSortBuffer.clear(); + g.CurrentWindow = NULL; + g.CurrentWindowStack.clear(); + g.WindowsById.Clear(); + g.NavWindow = NULL; + g.HoveredWindow = g.HoveredRootWindow = NULL; + g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; + g.MovingWindow = NULL; + g.ColorModifiers.clear(); + g.StyleModifiers.clear(); + g.FontStack.clear(); + g.OpenPopupStack.clear(); + g.BeginPopupStack.clear(); + g.DrawDataBuilder.ClearFreeMemory(); + g.BackgroundDrawList.ClearFreeMemory(); + g.ForegroundDrawList.ClearFreeMemory(); + + g.TabBars.Clear(); + g.CurrentTabBarStack.clear(); + g.ShrinkWidthBuffer.clear(); + + g.PrivateClipboard.clear(); + g.InputTextState.ClearFreeMemory(); + + for (int i = 0; i < g.SettingsWindows.Size; i++) + IM_DELETE(g.SettingsWindows[i].Name); + g.SettingsWindows.clear(); + g.SettingsHandlers.clear(); + + if (g.LogFile && g.LogFile != stdout) + { + fclose(g.LogFile); + g.LogFile = NULL; + } + g.LogBuffer.clear(); + + g.Initialized = false; +} + +// FIXME: Add a more explicit sort order in the window structure. +static int IMGUI_CDECL ChildWindowComparer(const void* lhs, const void* rhs) +{ + const ImGuiWindow* const a = *(const ImGuiWindow* const *)lhs; + const ImGuiWindow* const b = *(const ImGuiWindow* const *)rhs; + if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup)) + return d; + if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip)) + return d; + return (a->BeginOrderWithinParent - b->BeginOrderWithinParent); +} + +static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window) +{ + out_sorted_windows->push_back(window); + if (window->Active) + { + int count = window->DC.ChildWindows.Size; + if (count > 1) + ImQsort(window->DC.ChildWindows.Data, (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); + for (int i = 0; i < count; i++) + { + ImGuiWindow* child = window->DC.ChildWindows[i]; + if (child->Active) + AddWindowToSortBuffer(out_sorted_windows, child); + } + } +} + +static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) +{ + if (draw_list->CmdBuffer.empty()) + return; + + // Remove trailing command if unused + ImDrawCmd& last_cmd = draw_list->CmdBuffer.back(); + if (last_cmd.ElemCount == 0 && last_cmd.UserCallback == NULL) + { + draw_list->CmdBuffer.pop_back(); + if (draw_list->CmdBuffer.empty()) + return; + } + + // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. + // May trigger for you if you are using PrimXXX functions incorrectly. + IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); + IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); + if (!(draw_list->Flags & ImDrawListFlags_AllowVtxOffset)) + IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); + + // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) + // If this assert triggers because you are drawing lots of stuff manually: + // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds. + // Be mindful that the ImDrawList API doesn't filter vertices. Use the Metrics window to inspect draw list contents. + // - If you want large meshes with more than 64K vertices, you can either: + // (A) Handle the ImDrawCmd::VtxOffset value in your renderer back-end, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. + // Most example back-ends already support this from 1.71. Pre-1.71 back-ends won't. + // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. + // (B) Or handle 32-bits indices in your renderer back-end, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. + // Most example back-ends already support this. For example, the OpenGL example code detect index size at compile-time: + // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); + // Your own engine or render API may use different parameters or function calls to specify index sizes. + // 2 and 4 bytes indices are generally supported by most graphics API. + // - If for some reason neither of those solutions works for you, a workaround is to call BeginChild()/EndChild() before reaching + // the 64K limit to split your draw commands in multiple draw lists. + if (sizeof(ImDrawIdx) == 2) + IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above"); + + out_list->push_back(draw_list); +} + +static void AddWindowToDrawData(ImVector* out_render_list, ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + g.IO.MetricsRenderWindows++; + AddDrawListToDrawData(out_render_list, window->DrawList); + for (int i = 0; i < window->DC.ChildWindows.Size; i++) + { + ImGuiWindow* child = window->DC.ChildWindows[i]; + if (IsWindowActiveAndVisible(child)) // clipped children may have been marked not active + AddWindowToDrawData(out_render_list, child); + } +} + +// Layer is locked for the root window, however child windows may use a different viewport (e.g. extruding menu) +static void AddRootWindowToDrawData(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (window->Flags & ImGuiWindowFlags_Tooltip) + AddWindowToDrawData(&g.DrawDataBuilder.Layers[1], window); + else + AddWindowToDrawData(&g.DrawDataBuilder.Layers[0], window); +} + +void ImDrawDataBuilder::FlattenIntoSingleLayer() +{ + int n = Layers[0].Size; + int size = n; + for (int i = 1; i < IM_ARRAYSIZE(Layers); i++) + size += Layers[i].Size; + Layers[0].resize(size); + for (int layer_n = 1; layer_n < IM_ARRAYSIZE(Layers); layer_n++) + { + ImVector& layer = Layers[layer_n]; + if (layer.empty()) + continue; + memcpy(&Layers[0][n], &layer[0], layer.Size * sizeof(ImDrawList*)); + n += layer.Size; + layer.resize(0); + } +} + +static void SetupDrawData(ImVector* draw_lists, ImDrawData* draw_data) +{ + ImGuiIO& io = ImGui::GetIO(); + draw_data->Valid = true; + draw_data->CmdLists = (draw_lists->Size > 0) ? draw_lists->Data : NULL; + draw_data->CmdListsCount = draw_lists->Size; + draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0; + draw_data->DisplayPos = ImVec2(0.0f, 0.0f); + draw_data->DisplaySize = io.DisplaySize; + draw_data->FramebufferScale = io.DisplayFramebufferScale; + for (int n = 0; n < draw_lists->Size; n++) + { + draw_data->TotalVtxCount += draw_lists->Data[n]->VtxBuffer.Size; + draw_data->TotalIdxCount += draw_lists->Data[n]->IdxBuffer.Size; + } +} + +// When using this function it is sane to ensure that float are perfectly rounded to integer values, to that e.g. (int)(max.x-min.x) in user's render produce correct result. +void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DrawList->PushClipRect(clip_rect_min, clip_rect_max, intersect_with_current_clip_rect); + window->ClipRect = window->DrawList->_ClipRectStack.back(); +} + +void ImGui::PopClipRect() +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DrawList->PopClipRect(); + window->ClipRect = window->DrawList->_ClipRectStack.back(); +} + +// This is normally called by Render(). You may want to call it directly if you want to avoid calling Render() but the gain will be very minimal. +void ImGui::EndFrame() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.Initialized); + if (g.FrameCountEnded == g.FrameCount) // Don't process EndFrame() multiple times. + return; + IM_ASSERT(g.FrameScopeActive && "Forgot to call ImGui::NewFrame()?"); + + // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) + if (g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f)) + { + g.IO.ImeSetInputScreenPosFn((int)g.PlatformImePos.x, (int)g.PlatformImePos.y); + g.PlatformImeLastPos = g.PlatformImePos; + } + + // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you + // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). + if (g.CurrentWindowStack.Size != 1) + { + if (g.CurrentWindowStack.Size > 1) + { + IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); + while (g.CurrentWindowStack.Size > 1) // FIXME-ERRORHANDLING + End(); + } + else + { + IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); + } + } + + // Hide implicit/fallback "Debug" window if it hasn't been used + g.FrameScopePushedImplicitWindow = false; + if (g.CurrentWindow && !g.CurrentWindow->WriteAccessed) + g.CurrentWindow->Active = false; + End(); + + // Show CTRL+TAB list window + if (g.NavWindowingTarget != NULL) + NavUpdateWindowingOverlay(); + + // Drag and Drop: Elapse payload (if delivered, or if source stops being submitted) + if (g.DragDropActive) + { + bool is_delivered = g.DragDropPayload.Delivery; + bool is_elapsed = (g.DragDropPayload.DataFrameCount + 1 < g.FrameCount) && ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceAutoExpirePayload) || !IsMouseDown(g.DragDropMouseButton)); + if (is_delivered || is_elapsed) + ClearDragDrop(); + } + + // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing. + if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount) + { + g.DragDropWithinSourceOrTarget = true; + SetTooltip("..."); + g.DragDropWithinSourceOrTarget = false; + } + + // End frame + g.FrameScopeActive = false; + g.FrameCountEnded = g.FrameCount; + + // Initiate moving window + handle left-click and right-click focus + UpdateMouseMovingWindowEndFrame(); + + // Sort the window list so that all child windows are after their parent + // We cannot do that on FocusWindow() because childs may not exist yet + g.WindowsSortBuffer.resize(0); + g.WindowsSortBuffer.reserve(g.Windows.Size); + for (int i = 0; i != g.Windows.Size; i++) + { + ImGuiWindow* window = g.Windows[i]; + if (window->Active && (window->Flags & ImGuiWindowFlags_ChildWindow)) // if a child is active its parent will add it + continue; + AddWindowToSortBuffer(&g.WindowsSortBuffer, window); + } + + // This usually assert if there is a mismatch between the ImGuiWindowFlags_ChildWindow / ParentWindow values and DC.ChildWindows[] in parents, aka we've done something wrong. + IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); + g.Windows.swap(g.WindowsSortBuffer); + g.IO.MetricsActiveWindows = g.WindowsActiveCount; + + // Unlock font atlas + g.IO.Fonts->Locked = false; + + // Clear Input data for next frame + g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; + g.IO.InputQueueCharacters.resize(0); + memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); +} + +void ImGui::Render() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.Initialized); + + if (g.FrameCountEnded != g.FrameCount) + EndFrame(); + g.FrameCountRendered = g.FrameCount; + + // Gather ImDrawList to render (for each active window) + g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = g.IO.MetricsRenderWindows = 0; + g.DrawDataBuilder.Clear(); + if (!g.BackgroundDrawList.VtxBuffer.empty()) + AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList); + + ImGuiWindow* windows_to_render_top_most[2]; + windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; + windows_to_render_top_most[1] = g.NavWindowingTarget ? g.NavWindowingList : NULL; + for (int n = 0; n != g.Windows.Size; n++) + { + ImGuiWindow* window = g.Windows[n]; + if (IsWindowActiveAndVisible(window) && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0 && window != windows_to_render_top_most[0] && window != windows_to_render_top_most[1]) + AddRootWindowToDrawData(window); + } + for (int n = 0; n < IM_ARRAYSIZE(windows_to_render_top_most); n++) + if (windows_to_render_top_most[n] && IsWindowActiveAndVisible(windows_to_render_top_most[n])) // NavWindowingTarget is always temporarily displayed as the top-most window + AddRootWindowToDrawData(windows_to_render_top_most[n]); + g.DrawDataBuilder.FlattenIntoSingleLayer(); + + // Draw software mouse cursor if requested + if (g.IO.MouseDrawCursor) + RenderMouseCursor(&g.ForegroundDrawList, g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); + + if (!g.ForegroundDrawList.VtxBuffer.empty()) + AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.ForegroundDrawList); + + // Setup ImDrawData structure for end-user + SetupDrawData(&g.DrawDataBuilder.Layers[0], &g.DrawData); + g.IO.MetricsRenderVertices = g.DrawData.TotalVtxCount; + g.IO.MetricsRenderIndices = g.DrawData.TotalIdxCount; + + // (Legacy) Call the Render callback function. The current prefer way is to let the user retrieve GetDrawData() and call the render function themselves. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (g.DrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL) + g.IO.RenderDrawListsFn(&g.DrawData); +#endif +} + +// Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker. +// CalcTextSize("") should return ImVec2(0.0f, g.FontSize) +ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_text_after_double_hash, float wrap_width) +{ + ImGuiContext& g = *GImGui; + + const char* text_display_end; + if (hide_text_after_double_hash) + text_display_end = FindRenderedTextEnd(text, text_end); // Hide anything after a '##' string + else + text_display_end = text_end; + + ImFont* font = g.Font; + const float font_size = g.FontSize; + if (text == text_display_end) + return ImVec2(0.0f, font_size); + ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL); + + // Round + text_size.x = (float)(int)(text_size.x + 0.95f); + + return text_size; +} + +// Find window given position, search front-to-back +// FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programatically +// with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is +// called, aka before the next Begin(). Moving window isn't affected. +static void FindHoveredWindow() +{ + ImGuiContext& g = *GImGui; + + ImGuiWindow* hovered_window = NULL; + if (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs)) + hovered_window = g.MovingWindow; + + ImVec2 padding_regular = g.Style.TouchExtraPadding; + ImVec2 padding_for_resize_from_edges = g.IO.ConfigWindowsResizeFromEdges ? ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS)) : padding_regular; + for (int i = g.Windows.Size - 1; i >= 0; i--) + { + ImGuiWindow* window = g.Windows[i]; + if (!window->Active || window->Hidden) + continue; + if (window->Flags & ImGuiWindowFlags_NoMouseInputs) + continue; + + // Using the clipped AABB, a child window will typically be clipped by its parent (not always) + ImRect bb(window->OuterRectClipped); + if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) + bb.Expand(padding_regular); + else + bb.Expand(padding_for_resize_from_edges); + if (!bb.Contains(g.IO.MousePos)) + continue; + + // Those seemingly unnecessary extra tests are because the code here is a little different in viewport/docking branches. + if (hovered_window == NULL) + hovered_window = window; + if (hovered_window) + break; + } + + g.HoveredWindow = hovered_window; + g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; + +} + +// Test if mouse cursor is hovering given rectangle +// NB- Rectangle is clipped by our current clip setting +// NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding) +bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip) +{ + ImGuiContext& g = *GImGui; + + // Clip + ImRect rect_clipped(r_min, r_max); + if (clip) + rect_clipped.ClipWith(g.CurrentWindow->ClipRect); + + // Expand for touch input + const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); + if (!rect_for_touch.Contains(g.IO.MousePos)) + return false; + return true; +} + +int ImGui::GetKeyIndex(ImGuiKey imgui_key) +{ + IM_ASSERT(imgui_key >= 0 && imgui_key < ImGuiKey_COUNT); + ImGuiContext& g = *GImGui; + return g.IO.KeyMap[imgui_key]; +} + +// Note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your back-end/engine stored them into io.KeysDown[]! +bool ImGui::IsKeyDown(int user_key_index) +{ + if (user_key_index < 0) + return false; + ImGuiContext& g = *GImGui; + IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); + return g.IO.KeysDown[user_key_index]; +} + +// t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) +// t1 = current time (e.g.: g.Time) +// An event is triggered at: +// t = 0.0f t = repeat_delay, t = repeat_delay + repeat_rate*N +int ImGui::CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate) +{ + if (t1 == 0.0f) + return 1; + if (t0 >= t1) + return 0; + if (repeat_rate <= 0.0f) + return (t0 < repeat_delay) && (t1 >= repeat_delay); + const int count_t0 = (t0 < repeat_delay) ? -1 : (int)((t0 - repeat_delay) / repeat_rate); + const int count_t1 = (t1 < repeat_delay) ? -1 : (int)((t1 - repeat_delay) / repeat_rate); + const int count = count_t1 - count_t0; + return count; +} + +int ImGui::GetKeyPressedAmount(int key_index, float repeat_delay, float repeat_rate) +{ + ImGuiContext& g = *GImGui; + if (key_index < 0) + return 0; + IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); + const float t = g.IO.KeysDownDuration[key_index]; + return CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, repeat_delay, repeat_rate); +} + +bool ImGui::IsKeyPressed(int user_key_index, bool repeat) +{ + ImGuiContext& g = *GImGui; + if (user_key_index < 0) + return false; + IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); + const float t = g.IO.KeysDownDuration[user_key_index]; + if (t == 0.0f) + return true; + if (repeat && t > g.IO.KeyRepeatDelay) + return GetKeyPressedAmount(user_key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; + return false; +} + +bool ImGui::IsKeyReleased(int user_key_index) +{ + ImGuiContext& g = *GImGui; + if (user_key_index < 0) return false; + IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); + return g.IO.KeysDownDurationPrev[user_key_index] >= 0.0f && !g.IO.KeysDown[user_key_index]; +} + +bool ImGui::IsMouseDown(int button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseDown[button]; +} + +bool ImGui::IsAnyMouseDown() +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) + if (g.IO.MouseDown[n]) + return true; + return false; +} + +bool ImGui::IsMouseClicked(int button, bool repeat) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + const float t = g.IO.MouseDownDuration[button]; + if (t == 0.0f) + return true; + + if (repeat && t > g.IO.KeyRepeatDelay) + { + // FIXME: 2019/05/03: Our old repeat code was wrong here and led to doubling the repeat rate, which made it an ok rate for repeat on mouse hold. + int amount = CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate * 0.50f); + if (amount > 0) + return true; + } + + return false; +} + +bool ImGui::IsMouseReleased(int button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseReleased[button]; +} + +bool ImGui::IsMouseDoubleClicked(int button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseDoubleClicked[button]; +} + +// [Internal] This doesn't test if the button is pressed +bool ImGui::IsMouseDragPastThreshold(int button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; +} + +bool ImGui::IsMouseDragging(int button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (!g.IO.MouseDown[button]) + return false; + return IsMouseDragPastThreshold(button, lock_threshold); +} + +ImVec2 ImGui::GetMousePos() +{ + return GImGui->IO.MousePos; +} + +// NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! +ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() +{ + ImGuiContext& g = *GImGui; + if (g.BeginPopupStack.Size > 0) + return g.OpenPopupStack[g.BeginPopupStack.Size-1].OpenMousePos; + return g.IO.MousePos; +} + +// We typically use ImVec2(-FLT_MAX,-FLT_MAX) to denote an invalid mouse position. +bool ImGui::IsMousePosValid(const ImVec2* mouse_pos) +{ + // The assert is only to silence a false-positive in XCode Static Analysis. + // Because GImGui is not dereferenced in every code path, the static analyzer assume that it may be NULL (which it doesn't for other functions). + IM_ASSERT(GImGui != NULL); + const float MOUSE_INVALID = -256000.0f; + ImVec2 p = mouse_pos ? *mouse_pos : GImGui->IO.MousePos; + return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID; +} + +// Return the delta from the initial clicking position while the mouse button is clicked or was just released. +// This is locked and return 0.0f until the mouse moves past a distance threshold at least once. +// NB: This is only valid if IsMousePosValid(). Back-ends in theory should always keep mouse position valid when dragging even outside the client window. +ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + if (g.IO.MouseDown[button] || g.IO.MouseReleased[button]) + if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold) + if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MouseClickedPos[button])) + return g.IO.MousePos - g.IO.MouseClickedPos[button]; + return ImVec2(0.0f, 0.0f); +} + +void ImGui::ResetMouseDragDelta(int button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr + g.IO.MouseClickedPos[button] = g.IO.MousePos; +} + +ImGuiMouseCursor ImGui::GetMouseCursor() +{ + return GImGui->MouseCursor; +} + +void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) +{ + GImGui->MouseCursor = cursor_type; +} + +void ImGui::CaptureKeyboardFromApp(bool capture) +{ + GImGui->WantCaptureKeyboardNextFrame = capture ? 1 : 0; +} + +void ImGui::CaptureMouseFromApp(bool capture) +{ + GImGui->WantCaptureMouseNextFrame = capture ? 1 : 0; +} + +bool ImGui::IsItemActive() +{ + ImGuiContext& g = *GImGui; + if (g.ActiveId) + { + ImGuiWindow* window = g.CurrentWindow; + return g.ActiveId == window->DC.LastItemId; + } + return false; +} + +bool ImGui::IsItemActivated() +{ + ImGuiContext& g = *GImGui; + if (g.ActiveId) + { + ImGuiWindow* window = g.CurrentWindow; + if (g.ActiveId == window->DC.LastItemId && g.ActiveIdPreviousFrame != window->DC.LastItemId) + return true; + } + return false; +} + +bool ImGui::IsItemDeactivated() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDeactivated) + return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Deactivated) != 0; + return (g.ActiveIdPreviousFrame == window->DC.LastItemId && g.ActiveIdPreviousFrame != 0 && g.ActiveId != window->DC.LastItemId); +} + +bool ImGui::IsItemDeactivatedAfterEdit() +{ + ImGuiContext& g = *GImGui; + return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEditedBefore || (g.ActiveId == 0 && g.ActiveIdHasBeenEditedBefore)); +} + +bool ImGui::IsItemFocused() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (g.NavId == 0 || g.NavDisableHighlight || g.NavId != window->DC.LastItemId) + return false; + return true; +} + +bool ImGui::IsItemClicked(int mouse_button) +{ + return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); +} + +bool ImGui::IsItemToggledSelection() +{ + ImGuiContext& g = *GImGui; + return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; +} + +bool ImGui::IsAnyItemHovered() +{ + ImGuiContext& g = *GImGui; + return g.HoveredId != 0 || g.HoveredIdPreviousFrame != 0; +} + +bool ImGui::IsAnyItemActive() +{ + ImGuiContext& g = *GImGui; + return g.ActiveId != 0; +} + +bool ImGui::IsAnyItemFocused() +{ + ImGuiContext& g = *GImGui; + return g.NavId != 0 && !g.NavDisableHighlight; +} + +bool ImGui::IsItemVisible() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->ClipRect.Overlaps(window->DC.LastItemRect); +} + +bool ImGui::IsItemEdited() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Edited) != 0; +} + +// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. +void ImGui::SetItemAllowOverlap() +{ + ImGuiContext& g = *GImGui; + if (g.HoveredId == g.CurrentWindow->DC.LastItemId) + g.HoveredIdAllowOverlap = true; + if (g.ActiveId == g.CurrentWindow->DC.LastItemId) + g.ActiveIdAllowOverlap = true; +} + +ImVec2 ImGui::GetItemRectMin() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.LastItemRect.Min; +} + +ImVec2 ImGui::GetItemRectMax() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.LastItemRect.Max; +} + +ImVec2 ImGui::GetItemRectSize() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.LastItemRect.GetSize(); +} + +static ImRect GetViewportRect() +{ + ImGuiContext& g = *GImGui; + return ImRect(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); +} + +static bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* parent_window = g.CurrentWindow; + + flags |= ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow; + flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag + + // Size + const ImVec2 content_avail = GetContentRegionAvail(); + ImVec2 size = ImFloor(size_arg); + const int auto_fit_axises = ((size.x == 0.0f) ? (1 << ImGuiAxis_X) : 0x00) | ((size.y == 0.0f) ? (1 << ImGuiAxis_Y) : 0x00); + if (size.x <= 0.0f) + size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues) + if (size.y <= 0.0f) + size.y = ImMax(content_avail.y + size.y, 4.0f); + SetNextWindowSize(size); + + // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. + char title[256]; + if (name) + ImFormatString(title, IM_ARRAYSIZE(title), "%s/%s_%08X", parent_window->Name, name, id); + else + ImFormatString(title, IM_ARRAYSIZE(title), "%s/%08X", parent_window->Name, id); + + const float backup_border_size = g.Style.ChildBorderSize; + if (!border) + g.Style.ChildBorderSize = 0.0f; + bool ret = Begin(title, NULL, flags); + g.Style.ChildBorderSize = backup_border_size; + + ImGuiWindow* child_window = g.CurrentWindow; + child_window->ChildId = id; + child_window->AutoFitChildAxises = (ImS8)auto_fit_axises; + + // Set the cursor to handle case where the user called SetNextWindowPos()+BeginChild() manually. + // While this is not really documented/defined, it seems that the expected thing to do. + if (child_window->BeginCount == 1) + parent_window->DC.CursorPos = child_window->Pos; + + // Process navigation-in immediately so NavInit can run on first frame + if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayerActiveMask != 0 || child_window->DC.NavHasScroll)) + { + FocusWindow(child_window); + NavInitWindow(child_window, false); + SetActiveID(id+1, child_window); // Steal ActiveId with a dummy id so that key-press won't activate child item + g.ActiveIdSource = ImGuiInputSource_Nav; + } + return ret; +} + +bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + return BeginChildEx(str_id, window->GetID(str_id), size_arg, border, extra_flags); +} + +bool ImGui::BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) +{ + IM_ASSERT(id != 0); + return BeginChildEx(NULL, id, size_arg, border, extra_flags); +} + +void ImGui::EndChild() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() callss + if (window->BeginCount > 1) + { + End(); + } + else + { + ImVec2 sz = window->Size; + if (window->AutoFitChildAxises & (1 << ImGuiAxis_X)) // Arbitrary minimum zero-ish child size of 4.0f causes less trouble than a 0.0f + sz.x = ImMax(4.0f, sz.x); + if (window->AutoFitChildAxises & (1 << ImGuiAxis_Y)) + sz.y = ImMax(4.0f, sz.y); + End(); + + ImGuiWindow* parent_window = g.CurrentWindow; + ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz); + ItemSize(sz); + if ((window->DC.NavLayerActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) + { + ItemAdd(bb, window->ChildId); + RenderNavHighlight(bb, window->ChildId); + + // When browsing a window that has no activable items (scroll only) we keep a highlight on the child + if (window->DC.NavLayerActiveMask == 0 && window == g.NavWindow) + RenderNavHighlight(ImRect(bb.Min - ImVec2(2,2), bb.Max + ImVec2(2,2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); + } + else + { + // Not navigable into + ItemAdd(bb, 0); + } + } +} + +// Helper to create a child window / scrolling region that looks like a normal widget frame. +bool ImGui::BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags extra_flags) +{ + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); + PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); + PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); + PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding); + bool ret = BeginChild(id, size, true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding | extra_flags); + PopStyleVar(3); + PopStyleColor(); + return ret; +} + +void ImGui::EndChildFrame() +{ + EndChild(); +} + +// Save and compare stack sizes on Begin()/End() to detect usage errors +static void CheckStacksSize(ImGuiWindow* window, bool write) +{ + // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) + ImGuiContext& g = *GImGui; + short* p_backup = &window->DC.StackSizesBackup[0]; + { int current = window->IDStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "PushID/PopID or TreeNode/TreePop Mismatch!"); p_backup++; } // Too few or too many PopID()/TreePop() + { int current = window->DC.GroupStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "BeginGroup/EndGroup Mismatch!"); p_backup++; } // Too few or too many EndGroup() + { int current = g.BeginPopupStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "BeginMenu/EndMenu or BeginPopup/EndPopup Mismatch"); p_backup++;}// Too few or too many EndMenu()/EndPopup() + // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. + { int current = g.ColorModifiers.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushStyleColor/PopStyleColor Mismatch!"); p_backup++; } // Too few or too many PopStyleColor() + { int current = g.StyleModifiers.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushStyleVar/PopStyleVar Mismatch!"); p_backup++; } // Too few or too many PopStyleVar() + { int current = g.FontStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushFont/PopFont Mismatch!"); p_backup++; } // Too few or too many PopFont() + IM_ASSERT(p_backup == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup)); +} + +static void SetWindowConditionAllowFlags(ImGuiWindow* window, ImGuiCond flags, bool enabled) +{ + window->SetWindowPosAllowFlags = enabled ? (window->SetWindowPosAllowFlags | flags) : (window->SetWindowPosAllowFlags & ~flags); + window->SetWindowSizeAllowFlags = enabled ? (window->SetWindowSizeAllowFlags | flags) : (window->SetWindowSizeAllowFlags & ~flags); + window->SetWindowCollapsedAllowFlags = enabled ? (window->SetWindowCollapsedAllowFlags | flags) : (window->SetWindowCollapsedAllowFlags & ~flags); +} + +ImGuiWindow* ImGui::FindWindowByID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + return (ImGuiWindow*)g.WindowsById.GetVoidPtr(id); +} + +ImGuiWindow* ImGui::FindWindowByName(const char* name) +{ + ImGuiID id = ImHashStr(name); + return FindWindowByID(id); +} + +static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags) +{ + ImGuiContext& g = *GImGui; + //IMGUI_DEBUG_LOG("CreateNewWindow '%s', flags = 0x%08X\n", name, flags); + + // Create window the first time + ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name); + window->Flags = flags; + g.WindowsById.SetVoidPtr(window->ID, window); + + // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. + window->Pos = ImVec2(60, 60); + + // User can disable loading and saving of settings. Tooltip and child windows also don't store settings. + if (!(flags & ImGuiWindowFlags_NoSavedSettings)) + if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID)) + { + // Retrieve settings from .ini file + window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); + SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); + window->Pos = ImVec2(settings->Pos.x, settings->Pos.y); + window->Collapsed = settings->Collapsed; + if (settings->Size.x > 0 && settings->Size.y > 0) + size = ImVec2(settings->Size.x, settings->Size.y); + } + window->Size = window->SizeFull = ImFloor(size); + window->DC.CursorStartPos = window->DC.CursorMaxPos = window->Pos; // So first call to CalcContentSize() doesn't return crazy values + + if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) + { + window->AutoFitFramesX = window->AutoFitFramesY = 2; + window->AutoFitOnlyGrows = false; + } + else + { + if (window->Size.x <= 0.0f) + window->AutoFitFramesX = 2; + if (window->Size.y <= 0.0f) + window->AutoFitFramesY = 2; + window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0); + } + + g.WindowsFocusOrder.push_back(window); + if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus) + g.Windows.push_front(window); // Quite slow but rare and only once + else + g.Windows.push_back(window); + return window; +} + +static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size) +{ + ImGuiContext& g = *GImGui; + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) + { + // Using -1,-1 on either X/Y axis to preserve the current size. + ImRect cr = g.NextWindowData.SizeConstraintRect; + new_size.x = (cr.Min.x >= 0 && cr.Max.x >= 0) ? ImClamp(new_size.x, cr.Min.x, cr.Max.x) : window->SizeFull.x; + new_size.y = (cr.Min.y >= 0 && cr.Max.y >= 0) ? ImClamp(new_size.y, cr.Min.y, cr.Max.y) : window->SizeFull.y; + if (g.NextWindowData.SizeCallback) + { + ImGuiSizeCallbackData data; + data.UserData = g.NextWindowData.SizeCallbackUserData; + data.Pos = window->Pos; + data.CurrentSize = window->SizeFull; + data.DesiredSize = new_size; + g.NextWindowData.SizeCallback(&data); + new_size = data.DesiredSize; + } + new_size.x = ImFloor(new_size.x); + new_size.y = ImFloor(new_size.y); + } + + // Minimum size + if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) + { + new_size = ImMax(new_size, g.Style.WindowMinSize); + new_size.y = ImMax(new_size.y, window->TitleBarHeight() + window->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows + } + return new_size; +} + +static ImVec2 CalcWindowContentSize(ImGuiWindow* window) +{ + if (window->Collapsed) + if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + return window->ContentSize; + if (window->Hidden && window->HiddenFramesCannotSkipItems == 0 && window->HiddenFramesCanSkipItems > 0) + return window->ContentSize; + + ImVec2 sz; + sz.x = (float)(int)((window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); + sz.y = (float)(int)((window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); + return sz; +} + +static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_contents) +{ + ImGuiContext& g = *GImGui; + ImGuiStyle& style = g.Style; + ImVec2 size_decorations = ImVec2(0.0f, window->TitleBarHeight() + window->MenuBarHeight()); + ImVec2 size_pad = window->WindowPadding * 2.0f; + ImVec2 size_desired = size_contents + size_pad + size_decorations; + if (window->Flags & ImGuiWindowFlags_Tooltip) + { + // Tooltip always resize + return size_desired; + } + else + { + // Maximum window size is determined by the viewport size or monitor size + const bool is_popup = (window->Flags & ImGuiWindowFlags_Popup) != 0; + const bool is_menu = (window->Flags & ImGuiWindowFlags_ChildMenu) != 0; + ImVec2 size_min = style.WindowMinSize; + if (is_popup || is_menu) // Popups and menus bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) + size_min = ImMin(size_min, ImVec2(4.0f, 4.0f)); + ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, g.IO.DisplaySize - style.DisplaySafeAreaPadding * 2.0f)); + + // When the window cannot fit all contents (either because of constraints, either because screen is too small), + // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. + ImVec2 size_auto_fit_after_constraint = CalcWindowSizeAfterConstraint(window, size_auto_fit); + bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - size_decorations.x < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); + bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - size_decorations.y < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); + if (will_have_scrollbar_x) + size_auto_fit.y += style.ScrollbarSize; + if (will_have_scrollbar_y) + size_auto_fit.x += style.ScrollbarSize; + return size_auto_fit; + } +} + +ImVec2 ImGui::CalcWindowExpectedSize(ImGuiWindow* window) +{ + ImVec2 size_contents = CalcWindowContentSize(window); + ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, size_contents); + ImVec2 size_final = CalcWindowSizeAfterConstraint(window, size_auto_fit); + return size_final; +} + +static ImGuiCol GetWindowBgColorIdxFromFlags(ImGuiWindowFlags flags) +{ + if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) + return ImGuiCol_PopupBg; + if (flags & ImGuiWindowFlags_ChildWindow) + return ImGuiCol_ChildBg; + return ImGuiCol_WindowBg; +} + +static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& corner_target, const ImVec2& corner_norm, ImVec2* out_pos, ImVec2* out_size) +{ + ImVec2 pos_min = ImLerp(corner_target, window->Pos, corner_norm); // Expected window upper-left + ImVec2 pos_max = ImLerp(window->Pos + window->Size, corner_target, corner_norm); // Expected window lower-right + ImVec2 size_expected = pos_max - pos_min; + ImVec2 size_constrained = CalcWindowSizeAfterConstraint(window, size_expected); + *out_pos = pos_min; + if (corner_norm.x == 0.0f) + out_pos->x -= (size_constrained.x - size_expected.x); + if (corner_norm.y == 0.0f) + out_pos->y -= (size_constrained.y - size_expected.y); + *out_size = size_constrained; +} + +struct ImGuiResizeGripDef +{ + ImVec2 CornerPosN; + ImVec2 InnerDir; + int AngleMin12, AngleMax12; +}; + +static const ImGuiResizeGripDef resize_grip_def[4] = +{ + { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower right + { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower left + { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper left + { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper right +}; + +static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) +{ + ImRect rect = window->Rect(); + if (thickness == 0.0f) rect.Max -= ImVec2(1,1); + if (border_n == 0) return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); // Top + if (border_n == 1) return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); // Right + if (border_n == 2) return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); // Bottom + if (border_n == 3) return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding); // Left + IM_ASSERT(0); + return ImRect(); +} + +// Handle resize for: Resize Grips, Borders, Gamepad +// Return true when using auto-fit (double click on resize grip) +static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]) +{ + ImGuiContext& g = *GImGui; + ImGuiWindowFlags flags = window->Flags; + + if ((flags & ImGuiWindowFlags_NoResize) || (flags & ImGuiWindowFlags_AlwaysAutoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) + return false; + if (window->WasActive == false) // Early out to avoid running this code for e.g. an hidden implicit/fallback Debug window. + return false; + + bool ret_auto_fit = false; + const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0; + const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); + const float grip_hover_inner_size = (float)(int)(grip_draw_size * 0.75f); + const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS : 0.0f; + + ImVec2 pos_target(FLT_MAX, FLT_MAX); + ImVec2 size_target(FLT_MAX, FLT_MAX); + + // Resize grips and borders are on layer 1 + window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); + + // Manual resize grips + PushID("#RESIZE"); + for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) + { + const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; + const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); + + // Using the FlattenChilds button flag we make the resize button accessible even if we are hovering over a child window + ImRect resize_rect(corner - grip.InnerDir * grip_hover_outer_size, corner + grip.InnerDir * grip_hover_inner_size); + if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x); + if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y); + bool hovered, held; + ButtonBehavior(resize_rect, window->GetID((void*)(intptr_t)resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); + //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); + if (hovered || held) + g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; + + if (held && g.IO.MouseDoubleClicked[0] && resize_grip_n == 0) + { + // Manual auto-fit when double-clicking + size_target = CalcWindowSizeAfterConstraint(window, size_auto_fit); + ret_auto_fit = true; + ClearActiveID(); + } + else if (held) + { + // Resize from any of the four corners + // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position + ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(grip.InnerDir * grip_hover_outer_size, grip.InnerDir * -grip_hover_inner_size, grip.CornerPosN); // Corner of the window corresponding to our corner grip + CalcResizePosSizeFromAnyCorner(window, corner_target, grip.CornerPosN, &pos_target, &size_target); + } + if (resize_grip_n == 0 || held || hovered) + resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip); + } + for (int border_n = 0; border_n < resize_border_count; border_n++) + { + bool hovered, held; + ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); + ButtonBehavior(border_rect, window->GetID((void*)(intptr_t)(border_n + 4)), &hovered, &held, ImGuiButtonFlags_FlattenChildren); + //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); + if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) + { + g.MouseCursor = (border_n & 1) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; + if (held) + *border_held = border_n; + } + if (held) + { + ImVec2 border_target = window->Pos; + ImVec2 border_posn; + if (border_n == 0) { border_posn = ImVec2(0, 0); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Top + if (border_n == 1) { border_posn = ImVec2(1, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Right + if (border_n == 2) { border_posn = ImVec2(0, 1); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Bottom + if (border_n == 3) { border_posn = ImVec2(0, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Left + CalcResizePosSizeFromAnyCorner(window, border_target, border_posn, &pos_target, &size_target); + } + } + PopID(); + + // Navigation resize (keyboard/gamepad) + if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) + { + ImVec2 nav_resize_delta; + if (g.NavInputSource == ImGuiInputSource_NavKeyboard && g.IO.KeyShift) + nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); + if (g.NavInputSource == ImGuiInputSource_NavGamepad) + nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down); + if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f) + { + const float NAV_RESIZE_SPEED = 600.0f; + nav_resize_delta *= ImFloor(NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); + g.NavWindowingToggleLayer = false; + g.NavDisableMouseHover = true; + resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); + // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck. + size_target = CalcWindowSizeAfterConstraint(window, window->SizeFull + nav_resize_delta); + } + } + + // Apply back modified position/size to window + if (size_target.x != FLT_MAX) + { + window->SizeFull = size_target; + MarkIniSettingsDirty(window); + } + if (pos_target.x != FLT_MAX) + { + window->Pos = ImFloor(pos_target); + MarkIniSettingsDirty(window); + } + + // Resize nav layer + window->DC.NavLayerCurrent = ImGuiNavLayer_Main; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + + window->Size = window->SizeFull; + return ret_auto_fit; +} + +static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& rect, const ImVec2& padding) +{ + ImGuiContext& g = *GImGui; + ImVec2 size_for_clamping = (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) ? ImVec2(window->Size.x, window->TitleBarHeight()) : window->Size; + window->Pos = ImMin(rect.Max - padding, ImMax(window->Pos + size_for_clamping, rect.Min + padding) - size_for_clamping); +} + +static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + float rounding = window->WindowRounding; + float border_size = window->WindowBorderSize; + if (border_size > 0.0f && !(window->Flags & ImGuiWindowFlags_NoBackground)) + window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + + int border_held = window->ResizeBorderHeld; + if (border_held != -1) + { + struct ImGuiResizeBorderDef + { + ImVec2 InnerDir; + ImVec2 CornerPosN1, CornerPosN2; + float OuterAngle; + }; + static const ImGuiResizeBorderDef resize_border_def[4] = + { + { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top + { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right + { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom + { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f } // Left + }; + const ImGuiResizeBorderDef& def = resize_border_def[border_held]; + ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI*0.25f, def.OuterAngle); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI*0.25f); + window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), false, ImMax(2.0f, border_size)); // Thicker than usual + } + if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) + { + float y = window->Pos.y + window->TitleBarHeight() - 1; + window->DrawList->AddLine(ImVec2(window->Pos.x + border_size, y), ImVec2(window->Pos.x + window->Size.x - border_size, y), GetColorU32(ImGuiCol_Border), g.Style.FrameBorderSize); + } +} + +void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size) +{ + ImGuiContext& g = *GImGui; + ImGuiStyle& style = g.Style; + ImGuiWindowFlags flags = window->Flags; + + // Draw window + handle manual resize + // As we highlight the title bar when want_focus is set, multiple reappearing windows will have have their title bar highlighted on their reappearing frame. + const float window_rounding = window->WindowRounding; + const float window_border_size = window->WindowBorderSize; + if (window->Collapsed) + { + // Title bar only + float backup_border_size = style.FrameBorderSize; + g.Style.FrameBorderSize = window->WindowBorderSize; + ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && !g.NavDisableHighlight) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed); + RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding); + g.Style.FrameBorderSize = backup_border_size; + } + else + { + // Window background + if (!(flags & ImGuiWindowFlags_NoBackground)) + { + ImU32 bg_col = GetColorU32(GetWindowBgColorIdxFromFlags(flags)); + float alpha = 1.0f; + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasBgAlpha) + alpha = g.NextWindowData.BgAlphaVal; + if (alpha != 1.0f) + bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT); + window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Bot); + } + + // Title bar + if (!(flags & ImGuiWindowFlags_NoTitleBar)) + { + ImU32 title_bar_col = GetColorU32(title_bar_is_highlight ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg); + window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, window_rounding, ImDrawCornerFlags_Top); + } + + // Menu bar + if (flags & ImGuiWindowFlags_MenuBar) + { + ImRect menu_bar_rect = window->MenuBarRect(); + menu_bar_rect.ClipWith(window->Rect()); // Soft clipping, in particular child window don't have minimum size covering the menu bar so this is useful for them. + window->DrawList->AddRectFilled(menu_bar_rect.Min + ImVec2(window_border_size, 0), menu_bar_rect.Max - ImVec2(window_border_size, 0), GetColorU32(ImGuiCol_MenuBarBg), (flags & ImGuiWindowFlags_NoTitleBar) ? window_rounding : 0.0f, ImDrawCornerFlags_Top); + if (style.FrameBorderSize > 0.0f && menu_bar_rect.Max.y < window->Pos.y + window->Size.y) + window->DrawList->AddLine(menu_bar_rect.GetBL(), menu_bar_rect.GetBR(), GetColorU32(ImGuiCol_Border), style.FrameBorderSize); + } + + // Scrollbars + if (window->ScrollbarX) + Scrollbar(ImGuiAxis_X); + if (window->ScrollbarY) + Scrollbar(ImGuiAxis_Y); + + // Render resize grips (after their input handling so we don't have a frame of latency) + if (!(flags & ImGuiWindowFlags_NoResize)) + { + for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) + { + const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; + const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); + window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(window_border_size, resize_grip_draw_size) : ImVec2(resize_grip_draw_size, window_border_size))); + window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(resize_grip_draw_size, window_border_size) : ImVec2(window_border_size, resize_grip_draw_size))); + window->DrawList->PathArcToFast(ImVec2(corner.x + grip.InnerDir.x * (window_rounding + window_border_size), corner.y + grip.InnerDir.y * (window_rounding + window_border_size)), window_rounding, grip.AngleMin12, grip.AngleMax12); + window->DrawList->PathFillConvex(resize_grip_col[resize_grip_n]); + } + } + + // Borders + RenderWindowOuterBorders(window); + } +} + +// Render title text, collapse button, close button +void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open) +{ + ImGuiContext& g = *GImGui; + ImGuiStyle& style = g.Style; + ImGuiWindowFlags flags = window->Flags; + + const bool has_close_button = (p_open != NULL); + const bool has_collapse_button = !(flags & ImGuiWindowFlags_NoCollapse) && (style.WindowMenuButtonPosition != ImGuiDir_None); + + // Close & Collapse button are on the Menu NavLayer and don't default focus (unless there's nothing else on that layer) + const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; + window->DC.ItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; + window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); + + // Layout buttons + // FIXME: Would be nice to generalize the subtleties expressed here into reusable code. + float pad_l = style.FramePadding.x; + float pad_r = style.FramePadding.x; + float button_sz = g.FontSize; + ImVec2 close_button_pos; + ImVec2 collapse_button_pos; + if (has_close_button) + { + pad_r += button_sz; + close_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - style.FramePadding.x, title_bar_rect.Min.y); + } + if (has_collapse_button && style.WindowMenuButtonPosition == ImGuiDir_Right) + { + pad_r += button_sz; + collapse_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - style.FramePadding.x, title_bar_rect.Min.y); + } + if (has_collapse_button && style.WindowMenuButtonPosition == ImGuiDir_Left) + { + collapse_button_pos = ImVec2(title_bar_rect.Min.x + pad_l - style.FramePadding.x, title_bar_rect.Min.y); + pad_l += button_sz; + } + + // Collapse button (submitting first so it gets priority when choosing a navigation init fallback) + if (has_collapse_button) + if (CollapseButton(window->GetID("#COLLAPSE"), collapse_button_pos)) + window->WantCollapseToggle = true; // Defer actual collapsing to next frame as we are too far in the Begin() function + + // Close button + if (has_close_button) + if (CloseButton(window->GetID("#CLOSE"), close_button_pos)) + *p_open = false; + + window->DC.NavLayerCurrent = ImGuiNavLayer_Main; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + window->DC.ItemFlags = item_flags_backup; + + // Title bar text (with: horizontal alignment, avoiding collapse/close button, optional "unsaved document" marker) + // FIXME: Refactor text alignment facilities along with RenderText helpers, this is WAY too much messy code.. + const char* UNSAVED_DOCUMENT_MARKER = "*"; + const float marker_size_x = (flags & ImGuiWindowFlags_UnsavedDocument) ? CalcTextSize(UNSAVED_DOCUMENT_MARKER, NULL, false).x : 0.0f; + const ImVec2 text_size = CalcTextSize(name, NULL, true) + ImVec2(marker_size_x, 0.0f); + + // As a nice touch we try to ensure that centered title text doesn't get affected by visibility of Close/Collapse button, + // while uncentered title text will still reach edges correct. + if (pad_l > style.FramePadding.x) + pad_l += g.Style.ItemInnerSpacing.x; + if (pad_r > style.FramePadding.x) + pad_r += g.Style.ItemInnerSpacing.x; + if (style.WindowTitleAlign.x > 0.0f && style.WindowTitleAlign.x < 1.0f) + { + float centerness = ImSaturate(1.0f - ImFabs(style.WindowTitleAlign.x - 0.5f) * 2.0f); // 0.0f on either edges, 1.0f on center + float pad_extend = ImMin(ImMax(pad_l, pad_r), title_bar_rect.GetWidth() - pad_l - pad_r - text_size.x); + pad_l = ImMax(pad_l, pad_extend * centerness); + pad_r = ImMax(pad_r, pad_extend * centerness); + } + + ImRect layout_r(title_bar_rect.Min.x + pad_l, title_bar_rect.Min.y, title_bar_rect.Max.x - pad_r, title_bar_rect.Max.y); + ImRect clip_r(layout_r.Min.x, layout_r.Min.y, layout_r.Max.x + g.Style.ItemInnerSpacing.x, layout_r.Max.y); + //if (g.IO.KeyCtrl) window->DrawList->AddRect(layout_r.Min, layout_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] + RenderTextClipped(layout_r.Min, layout_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_r); + if (flags & ImGuiWindowFlags_UnsavedDocument) + { + ImVec2 marker_pos = ImVec2(ImMax(layout_r.Min.x, layout_r.Min.x + (layout_r.GetWidth() - text_size.x) * style.WindowTitleAlign.x) + text_size.x, layout_r.Min.y) + ImVec2(2 - marker_size_x, 0.0f); + ImVec2 off = ImVec2(0.0f, (float)(int)(-g.FontSize * 0.25f)); + RenderTextClipped(marker_pos + off, layout_r.Max + off, UNSAVED_DOCUMENT_MARKER, NULL, NULL, ImVec2(0, style.WindowTitleAlign.y), &clip_r); + } +} + +void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window) +{ + window->ParentWindow = parent_window; + window->RootWindow = window->RootWindowForTitleBarHighlight = window->RootWindowForNav = window; + if (parent_window && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Tooltip)) + window->RootWindow = parent_window->RootWindow; + if (parent_window && !(flags & ImGuiWindowFlags_Modal) && (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup))) + window->RootWindowForTitleBarHighlight = parent_window->RootWindowForTitleBarHighlight; + while (window->RootWindowForNav->Flags & ImGuiWindowFlags_NavFlattened) + { + IM_ASSERT(window->RootWindowForNav->ParentWindow != NULL); + window->RootWindowForNav = window->RootWindowForNav->ParentWindow; + } +} + +// Push a new Dear ImGui window to add widgets to. +// - A default window called "Debug" is automatically stacked at the beginning of every frame so you can use widgets without explicitly calling a Begin/End pair. +// - Begin/End can be called multiple times during the frame with the same window name to append content. +// - The window name is used as a unique identifier to preserve window information across frames (and save rudimentary information to the .ini file). +// You can use the "##" or "###" markers to use the same label with different id, or same id with different label. See documentation at the top of this file. +// - Return false when window is collapsed, so you can early out in your code. You always need to call ImGui::End() even if false is returned. +// - Passing 'bool* p_open' displays a Close button on the upper-right corner of the window, the pointed value will be set to false when the button is pressed. +bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) +{ + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + IM_ASSERT(name != NULL && name[0] != '\0'); // Window name required + IM_ASSERT(g.FrameScopeActive); // Forgot to call ImGui::NewFrame() + IM_ASSERT(g.FrameCountEnded != g.FrameCount); // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet + + // Find or create + ImGuiWindow* window = FindWindowByName(name); + const bool window_just_created = (window == NULL); + if (window_just_created) + { + ImVec2 size_on_first_use = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) ? g.NextWindowData.SizeVal : ImVec2(0.0f, 0.0f); // Any condition flag will do since we are creating a new window here. + window = CreateNewWindow(name, size_on_first_use, flags); + } + + // Automatically disable manual moving/resizing when NoInputs is set + if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) + flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; + + if (flags & ImGuiWindowFlags_NavFlattened) + IM_ASSERT(flags & ImGuiWindowFlags_ChildWindow); + + const int current_frame = g.FrameCount; + const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame); + + // Update the Appearing flag + bool window_just_activated_by_user = (window->LastFrameActive < current_frame - 1); // Not using !WasActive because the implicit "Debug" window would always toggle off->on + const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); + if (flags & ImGuiWindowFlags_Popup) + { + ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; + window_just_activated_by_user |= (window->PopupId != popup_ref.PopupId); // We recycle popups so treat window as activated if popup id changed + window_just_activated_by_user |= (window != popup_ref.Window); + } + window->Appearing = (window_just_activated_by_user || window_just_appearing_after_hidden_for_resize); + if (window->Appearing) + SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, true); + + // Update Flags, LastFrameActive, BeginOrderXXX fields + if (first_begin_of_the_frame) + { + window->Flags = (ImGuiWindowFlags)flags; + window->LastFrameActive = current_frame; + window->LastTimeActive = (float)g.Time; + window->BeginOrderWithinParent = 0; + window->BeginOrderWithinContext = (short)(g.WindowsActiveCount++); + } + else + { + flags = window->Flags; + } + + // Parent window is latched only on the first call to Begin() of the frame, so further append-calls can be done from a different window stack + ImGuiWindow* parent_window_in_stack = g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back(); + ImGuiWindow* parent_window = first_begin_of_the_frame ? ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)) ? parent_window_in_stack : NULL) : window->ParentWindow; + IM_ASSERT(parent_window != NULL || !(flags & ImGuiWindowFlags_ChildWindow)); + + // We allow window memory to be compacted so recreate the base stack when needed. + if (window->IDStack.Size == 0) + window->IDStack.push_back(window->ID); + + // Add to stack + // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() + g.CurrentWindowStack.push_back(window); + g.CurrentWindow = NULL; + CheckStacksSize(window, true); + if (flags & ImGuiWindowFlags_Popup) + { + ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; + popup_ref.Window = window; + g.BeginPopupStack.push_back(popup_ref); + window->PopupId = popup_ref.PopupId; + } + + if (window_just_appearing_after_hidden_for_resize && !(flags & ImGuiWindowFlags_ChildWindow)) + window->NavLastIds[0] = 0; + + // Process SetNextWindow***() calls + bool window_pos_set_by_api = false; + bool window_size_x_set_by_api = false, window_size_y_set_by_api = false; + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) + { + window_pos_set_by_api = (window->SetWindowPosAllowFlags & g.NextWindowData.PosCond) != 0; + if (window_pos_set_by_api && ImLengthSqr(g.NextWindowData.PosPivotVal) > 0.00001f) + { + // May be processed on the next frame if this is our first frame and we are measuring size + // FIXME: Look into removing the branch so everything can go through this same code path for consistency. + window->SetWindowPosVal = g.NextWindowData.PosVal; + window->SetWindowPosPivot = g.NextWindowData.PosPivotVal; + window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); + } + else + { + SetWindowPos(window, g.NextWindowData.PosVal, g.NextWindowData.PosCond); + } + } + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) + { + window_size_x_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.x > 0.0f); + window_size_y_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.y > 0.0f); + SetWindowSize(window, g.NextWindowData.SizeVal, g.NextWindowData.SizeCond); + } + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasContentSize) + window->ContentSizeExplicit = g.NextWindowData.ContentSizeVal; + else if (first_begin_of_the_frame) + window->ContentSizeExplicit = ImVec2(0.0f, 0.0f); + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasCollapsed) + SetWindowCollapsed(window, g.NextWindowData.CollapsedVal, g.NextWindowData.CollapsedCond); + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasFocus) + FocusWindow(window); + if (window->Appearing) + SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false); + + // When reusing window again multiple times a frame, just append content (don't need to setup again) + if (first_begin_of_the_frame) + { + // Initialize + const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) + UpdateWindowParentAndRootLinks(window, flags, parent_window); + + window->Active = true; + window->HasCloseButton = (p_open != NULL); + window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX); + window->IDStack.resize(1); + + // Restore buffer capacity when woken from a compacted state, to avoid + if (window->MemoryCompacted) + GcAwakeTransientWindowBuffers(window); + + // Update stored window name when it changes (which can _only_ happen with the "###" operator, so the ID would stay unchanged). + // The title bar always display the 'name' parameter, so we only update the string storage if it needs to be visible to the end-user elsewhere. + bool window_title_visible_elsewhere = false; + if (g.NavWindowingList != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0) // Window titles visible when using CTRL+TAB + window_title_visible_elsewhere = true; + if (window_title_visible_elsewhere && !window_just_created && strcmp(name, window->Name) != 0) + { + size_t buf_len = (size_t)window->NameBufLen; + window->Name = ImStrdupcpy(window->Name, &buf_len, name); + window->NameBufLen = (int)buf_len; + } + + // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS + + // Update contents size from last frame for auto-fitting (or use explicit size) + window->ContentSize = CalcWindowContentSize(window); + if (window->HiddenFramesCanSkipItems > 0) + window->HiddenFramesCanSkipItems--; + if (window->HiddenFramesCannotSkipItems > 0) + window->HiddenFramesCannotSkipItems--; + + // Hide new windows for one frame until they calculate their size + if (window_just_created && (!window_size_x_set_by_api || !window_size_y_set_by_api)) + window->HiddenFramesCannotSkipItems = 1; + + // Hide popup/tooltip window when re-opening while we measure size (because we recycle the windows) + // We reset Size/ContentSize for reappearing popups/tooltips early in this function, so further code won't be tempted to use the old size. + if (window_just_activated_by_user && (flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) != 0) + { + window->HiddenFramesCannotSkipItems = 1; + if (flags & ImGuiWindowFlags_AlwaysAutoResize) + { + if (!window_size_x_set_by_api) + window->Size.x = window->SizeFull.x = 0.f; + if (!window_size_y_set_by_api) + window->Size.y = window->SizeFull.y = 0.f; + window->ContentSize = ImVec2(0.f, 0.f); + } + } + + // FIXME-VIEWPORT: In the docking/viewport branch, this is the point where we select the current viewport (which may affect the style) + SetCurrentWindow(window); + + // LOCK BORDER SIZE AND PADDING FOR THE FRAME (so that altering them doesn't cause inconsistencies) + + if (flags & ImGuiWindowFlags_ChildWindow) + window->WindowBorderSize = style.ChildBorderSize; + else + window->WindowBorderSize = ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupBorderSize : style.WindowBorderSize; + window->WindowPadding = style.WindowPadding; + if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f) + window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f); + window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); + window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; + + // Collapse window by double-clicking on title bar + // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing + if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse)) + { + // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), so verify that we don't have items over the title bar. + ImRect title_bar_rect = window->TitleBarRect(); + if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseDoubleClicked[0]) + window->WantCollapseToggle = true; + if (window->WantCollapseToggle) + { + window->Collapsed = !window->Collapsed; + MarkIniSettingsDirty(window); + FocusWindow(window); + } + } + else + { + window->Collapsed = false; + } + window->WantCollapseToggle = false; + + // SIZE + + // Calculate auto-fit size, handle automatic resize + const ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, window->ContentSize); + bool use_current_size_for_scrollbar_x = window_just_created; + bool use_current_size_for_scrollbar_y = window_just_created; + if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window->Collapsed) + { + // Using SetNextWindowSize() overrides ImGuiWindowFlags_AlwaysAutoResize, so it can be used on tooltips/popups, etc. + if (!window_size_x_set_by_api) + { + window->SizeFull.x = size_auto_fit.x; + use_current_size_for_scrollbar_x = true; + } + if (!window_size_y_set_by_api) + { + window->SizeFull.y = size_auto_fit.y; + use_current_size_for_scrollbar_y = true; + } + } + else if (window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) + { + // Auto-fit may only grow window during the first few frames + // We still process initial auto-fit on collapsed windows to get a window width, but otherwise don't honor ImGuiWindowFlags_AlwaysAutoResize when collapsed. + if (!window_size_x_set_by_api && window->AutoFitFramesX > 0) + { + window->SizeFull.x = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.x, size_auto_fit.x) : size_auto_fit.x; + use_current_size_for_scrollbar_x = true; + } + if (!window_size_y_set_by_api && window->AutoFitFramesY > 0) + { + window->SizeFull.y = window->AutoFitOnlyGrows ? ImMax(window->SizeFull.y, size_auto_fit.y) : size_auto_fit.y; + use_current_size_for_scrollbar_y = true; + } + if (!window->Collapsed) + MarkIniSettingsDirty(window); + } + + // Apply minimum/maximum window size constraints and final size + window->SizeFull = CalcWindowSizeAfterConstraint(window, window->SizeFull); + window->Size = window->Collapsed && !(flags & ImGuiWindowFlags_ChildWindow) ? window->TitleBarRect().GetSize() : window->SizeFull; + + // Decoration size + const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); + + // POSITION + + // Popup latch its initial position, will position itself when it appears next frame + if (window_just_activated_by_user) + { + window->AutoPosLastDirection = ImGuiDir_None; + if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api) + window->Pos = g.BeginPopupStack.back().OpenPopupPos; + } + + // Position child window + if (flags & ImGuiWindowFlags_ChildWindow) + { + IM_ASSERT(parent_window && parent_window->Active); + window->BeginOrderWithinParent = (short)parent_window->DC.ChildWindows.Size; + parent_window->DC.ChildWindows.push_back(window); + if (!(flags & ImGuiWindowFlags_Popup) && !window_pos_set_by_api && !window_is_child_tooltip) + window->Pos = parent_window->DC.CursorPos; + } + + const bool window_pos_with_pivot = (window->SetWindowPosVal.x != FLT_MAX && window->HiddenFramesCannotSkipItems == 0); + if (window_pos_with_pivot) + SetWindowPos(window, window->SetWindowPosVal - window->SizeFull * window->SetWindowPosPivot, 0); // Position given a pivot (e.g. for centering) + else if ((flags & ImGuiWindowFlags_ChildMenu) != 0) + window->Pos = FindBestWindowPosForPopup(window); + else if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api && window_just_appearing_after_hidden_for_resize) + window->Pos = FindBestWindowPosForPopup(window); + else if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api && !window_is_child_tooltip) + window->Pos = FindBestWindowPosForPopup(window); + + // Clamp position/size so window stays visible within its viewport or monitor + + // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. + ImRect viewport_rect(GetViewportRect()); + if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + { + if (g.IO.DisplaySize.x > 0.0f && g.IO.DisplaySize.y > 0.0f) // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. + { + ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); + ClampWindowRect(window, viewport_rect, clamp_padding); + } + } + window->Pos = ImFloor(window->Pos); + + // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) + window->WindowRounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildRounding : ((flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupRounding : style.WindowRounding; + + // Apply window focus (new and reactivated windows are moved to front) + bool want_focus = false; + if (window_just_activated_by_user && !(flags & ImGuiWindowFlags_NoFocusOnAppearing)) + { + if (flags & ImGuiWindowFlags_Popup) + want_focus = true; + else if ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) == 0) + want_focus = true; + } + + // Handle manual resize: Resize Grips, Borders, Gamepad + int border_held = -1; + ImU32 resize_grip_col[4] = { 0 }; + const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // 4 + const float resize_grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); + if (!window->Collapsed) + if (UpdateManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0])) + use_current_size_for_scrollbar_x = use_current_size_for_scrollbar_y = true; + window->ResizeBorderHeld = (signed char)border_held; + + // SCROLLBAR VISIBILITY + + // Update scrollbar visibility (based on the Size that was effective during last frame or the auto-resized Size). + if (!window->Collapsed) + { + // When reading the current size we need to read it after size constraints have been applied. + // When we use InnerRect here we are intentionally reading last frame size, same for ScrollbarSizes values before we set them again. + ImVec2 avail_size_from_current_frame = ImVec2(window->SizeFull.x, window->SizeFull.y - decoration_up_height); + ImVec2 avail_size_from_last_frame = window->InnerRect.GetSize() + window->ScrollbarSizes; + ImVec2 needed_size_from_last_frame = window_just_created ? ImVec2(0, 0) : window->ContentSize + window->WindowPadding * 2.0f; + float size_x_for_scrollbars = use_current_size_for_scrollbar_x ? avail_size_from_current_frame.x : avail_size_from_last_frame.x; + float size_y_for_scrollbars = use_current_size_for_scrollbar_y ? avail_size_from_current_frame.y : avail_size_from_last_frame.y; + //bool scrollbar_y_from_last_frame = window->ScrollbarY; // FIXME: May want to use that in the ScrollbarX expression? How many pros vs cons? + window->ScrollbarY = (flags & ImGuiWindowFlags_AlwaysVerticalScrollbar) || ((needed_size_from_last_frame.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar)); + window->ScrollbarX = (flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar) || ((needed_size_from_last_frame.x > size_x_for_scrollbars - (window->ScrollbarY ? style.ScrollbarSize : 0.0f)) && !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar)); + if (window->ScrollbarX && !window->ScrollbarY) + window->ScrollbarY = (needed_size_from_last_frame.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar); + window->ScrollbarSizes = ImVec2(window->ScrollbarY ? style.ScrollbarSize : 0.0f, window->ScrollbarX ? style.ScrollbarSize : 0.0f); + } + + // UPDATE RECTANGLES (1- THOSE NOT AFFECTED BY SCROLLING) + // Update various regions. Variables they depends on should be set above in this function. + // We set this up after processing the resize grip so that our rectangles doesn't lag by a frame. + + // Outer rectangle + // Not affected by window border size. Used by: + // - FindHoveredWindow() (w/ extra padding when border resize is enabled) + // - Begin() initial clipping rect for drawing window background and borders. + // - Begin() clipping whole child + const ImRect host_rect = ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) ? parent_window->ClipRect : viewport_rect; + const ImRect outer_rect = window->Rect(); + const ImRect title_bar_rect = window->TitleBarRect(); + window->OuterRectClipped = outer_rect; + window->OuterRectClipped.ClipWith(host_rect); + + // Inner rectangle + // Not affected by window border size. Used by: + // - InnerClipRect + // - ScrollToBringRectIntoView() + // - NavUpdatePageUpPageDown() + // - Scrollbar() + window->InnerRect.Min.x = window->Pos.x; + window->InnerRect.Min.y = window->Pos.y + decoration_up_height; + window->InnerRect.Max.x = window->Pos.x + window->Size.x - window->ScrollbarSizes.x; + window->InnerRect.Max.y = window->Pos.y + window->Size.y - window->ScrollbarSizes.y; + + // Inner clipping rectangle. + // Will extend a little bit outside the normal work region. + // This is to allow e.g. Selectable or CollapsingHeader or some separators to cover that space. + // Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result. + // Note that if our window is collapsed we will end up with an inverted (~null) clipping rectangle which is the correct behavior. + // Affected by window/frame border size. Used by: + // - Begin() initial clip rect + float top_border_size = (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize); + window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + ImMax(ImFloor(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); + window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + top_border_size); + window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerRect.Max.x - ImMax(ImFloor(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); + window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); + window->InnerClipRect.ClipWithFull(host_rect); + + // Default item width. Make it proportional to window size if window manually resizes + if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize)) + window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f); + else + window->ItemWidthDefault = (float)(int)(g.FontSize * 16.0f); + + // SCROLLING + + // Lock down maximum scrolling + // The value of ScrollMax are ahead from ScrollbarX/ScrollbarY which is intentionally using InnerRect from previous rect in order to accommodate + // for right/bottom aligned items without creating a scrollbar. + window->ScrollMax.x = ImMax(0.0f, window->ContentSize.x + window->WindowPadding.x * 2.0f - window->InnerRect.GetWidth()); + window->ScrollMax.y = ImMax(0.0f, window->ContentSize.y + window->WindowPadding.y * 2.0f - window->InnerRect.GetHeight()); + + // Apply scrolling + window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window, true); + window->ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); + + // DRAWING + + // Setup draw list and outer clipping rectangle + window->DrawList->Clear(); + window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID); + PushClipRect(host_rect.Min, host_rect.Max, false); + + // Draw modal window background (darkens what is behind them, all viewports) + const bool dim_bg_for_modal = (flags & ImGuiWindowFlags_Modal) && window == GetTopMostPopupModal() && window->HiddenFramesCannotSkipItems <= 0; + const bool dim_bg_for_window_list = g.NavWindowingTargetAnim && (window == g.NavWindowingTargetAnim->RootWindow); + if (dim_bg_for_modal || dim_bg_for_window_list) + { + const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio); + window->DrawList->AddRectFilled(viewport_rect.Min, viewport_rect.Max, dim_bg_col); + } + + // Draw navigation selection/windowing rectangle background + if (dim_bg_for_window_list && window == g.NavWindowingTargetAnim) + { + ImRect bb = window->Rect(); + bb.Expand(g.FontSize); + if (!bb.Contains(viewport_rect)) // Avoid drawing if the window covers all the viewport anyway + window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha * 0.25f), g.Style.WindowRounding); + } + + // Since 1.71, child window can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call. + // When using overlapping child windows, this will break the assumption that child z-order is mapped to submission order. + // We disable this when the parent window has zero vertices, which is a common pattern leading to laying out multiple overlapping child. + // We also disabled this when we have dimming overlay behind this specific one child. + // FIXME: More code may rely on explicit sorting of overlapping child window and would need to disable this somehow. Please get in contact if you are affected. + bool render_decorations_in_parent = false; + if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) + if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_window->DrawList->VtxBuffer.Size > 0) + render_decorations_in_parent = true; + if (render_decorations_in_parent) + window->DrawList = parent_window->DrawList; + + // Handle title bar, scrollbar, resize grips and resize borders + const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow; + const bool title_bar_is_highlight = want_focus || (window_to_highlight && window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight); + RenderWindowDecorations(window, title_bar_rect, title_bar_is_highlight, resize_grip_count, resize_grip_col, resize_grip_draw_size); + + if (render_decorations_in_parent) + window->DrawList = &window->DrawListInst; + + // Draw navigation selection/windowing rectangle border + if (g.NavWindowingTargetAnim == window) + { + float rounding = ImMax(window->WindowRounding, g.Style.WindowRounding); + ImRect bb = window->Rect(); + bb.Expand(g.FontSize); + if (bb.Contains(viewport_rect)) // If a window fits the entire viewport, adjust its highlight inward + { + bb.Expand(-g.FontSize - 1.0f); + rounding = window->WindowRounding; + } + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, ~0, 3.0f); + } + + // UPDATE RECTANGLES (2- THOSE AFFECTED BY SCROLLING) + + // Work rectangle. + // Affected by window padding and border size. Used by: + // - Columns() for right-most edge + // - TreeNode(), CollapsingHeader() for right-most edge + // - BeginTabBar() for right-most edge + const bool allow_scrollbar_x = !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar); + const bool allow_scrollbar_y = !(flags & ImGuiWindowFlags_NoScrollbar); + const float work_rect_size_x = (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : ImMax(allow_scrollbar_x ? window->ContentSize.x : 0.0f, window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); + const float work_rect_size_y = (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : ImMax(allow_scrollbar_y ? window->ContentSize.y : 0.0f, window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); + window->WorkRect.Min.x = ImFloor(window->InnerRect.Min.x - window->Scroll.x + ImMax(window->WindowPadding.x, window->WindowBorderSize)); + window->WorkRect.Min.y = ImFloor(window->InnerRect.Min.y - window->Scroll.y + ImMax(window->WindowPadding.y, window->WindowBorderSize)); + window->WorkRect.Max.x = window->WorkRect.Min.x + work_rect_size_x; + window->WorkRect.Max.y = window->WorkRect.Min.y + work_rect_size_y; + + // [LEGACY] Contents Region + // FIXME-OBSOLETE: window->ContentsRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. + // Used by: + // - Mouse wheel scrolling + many other things + window->ContentsRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x; + window->ContentsRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + decoration_up_height; + window->ContentsRegionRect.Max.x = window->ContentsRegionRect.Min.x + (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : (window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); + window->ContentsRegionRect.Max.y = window->ContentsRegionRect.Min.y + (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : (window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); + + // Setup drawing context + // (NB: That term "drawing context / DC" lost its meaning a long time ago. Initially was meant to hold transient data only. Nowadays difference between window-> and window->DC-> is dubious.) + window->DC.Indent.x = 0.0f + window->WindowPadding.x - window->Scroll.x; + window->DC.GroupOffset.x = 0.0f; + window->DC.ColumnsOffset.x = 0.0f; + window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.Indent.x + window->DC.ColumnsOffset.x, decoration_up_height + window->WindowPadding.y - window->Scroll.y); + window->DC.CursorPos = window->DC.CursorStartPos; + window->DC.CursorPosPrevLine = window->DC.CursorPos; + window->DC.CursorMaxPos = window->DC.CursorStartPos; + window->DC.CurrLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f); + window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; + window->DC.NavHideHighlightOneFrame = false; + window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); + window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; + window->DC.NavLayerActiveMaskNext = 0x00; + window->DC.MenuBarAppending = false; + window->DC.ChildWindows.resize(0); + window->DC.LayoutType = ImGuiLayoutType_Vertical; + window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical; + window->DC.FocusCounterAll = window->DC.FocusCounterTab = -1; + window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_; + window->DC.ItemWidth = window->ItemWidthDefault; + window->DC.TextWrapPos = -1.0f; // disabled + window->DC.ItemFlagsStack.resize(0); + window->DC.ItemWidthStack.resize(0); + window->DC.TextWrapPosStack.resize(0); + window->DC.CurrentColumns = NULL; + window->DC.TreeDepth = 0; + window->DC.TreeMayJumpToParentOnPopMask = 0x00; + window->DC.StateStorage = &window->StateStorage; + window->DC.GroupStack.resize(0); + window->MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); + + if ((flags & ImGuiWindowFlags_ChildWindow) && (window->DC.ItemFlags != parent_window->DC.ItemFlags)) + { + window->DC.ItemFlags = parent_window->DC.ItemFlags; + window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); + } + + if (window->AutoFitFramesX > 0) + window->AutoFitFramesX--; + if (window->AutoFitFramesY > 0) + window->AutoFitFramesY--; + + // Apply focus (we need to call FocusWindow() AFTER setting DC.CursorStartPos so our initial navigation reference rectangle can start around there) + if (want_focus) + { + FocusWindow(window); + NavInitWindow(window, false); + } + + // Title bar + if (!(flags & ImGuiWindowFlags_NoTitleBar)) + RenderWindowTitleBarContents(window, title_bar_rect, name, p_open); + + // Pressing CTRL+C while holding on a window copy its content to the clipboard + // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope. + // Maybe we can support CTRL+C on every element? + /* + if (g.ActiveId == move_id) + if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) + LogToClipboard(); + */ + + // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). + // This is useful to allow creating context menus on title bar only, etc. + window->DC.LastItemId = window->MoveId; + window->DC.LastItemStatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0; + window->DC.LastItemRect = title_bar_rect; +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) + IMGUI_TEST_ENGINE_ITEM_ADD(window->DC.LastItemRect, window->DC.LastItemId); +#endif + } + else + { + // Append + SetCurrentWindow(window); + } + + PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); + + // Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default "Debug" window is unused) + if (first_begin_of_the_frame) + window->WriteAccessed = false; + + window->BeginCount++; + g.NextWindowData.ClearFlags(); + + if (flags & ImGuiWindowFlags_ChildWindow) + { + // Child window can be out of sight and have "negative" clip windows. + // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). + IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); + if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + window->HiddenFramesCanSkipItems = 1; + + // Hide along with parent or if parent is collapsed + if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) + window->HiddenFramesCanSkipItems = 1; + if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCannotSkipItems > 0)) + window->HiddenFramesCannotSkipItems = 1; + } + + // Don't render if style alpha is 0.0 at the time of Begin(). This is arbitrary and inconsistent but has been there for a long while (may remove at some point) + if (style.Alpha <= 0.0f) + window->HiddenFramesCanSkipItems = 1; + + // Update the Hidden flag + window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); + + // Update the SkipItems flag, used to early out of all items functions (no layout required) + bool skip_items = false; + if (window->Collapsed || !window->Active || window->Hidden) + if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0) + skip_items = true; + window->SkipItems = skip_items; + + return !skip_items; +} + +// Old Begin() API with 5 parameters, avoid calling this version directly! Use SetNextWindowSize()/SetNextWindowBgAlpha() + Begin() instead. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_first_use, float bg_alpha_override, ImGuiWindowFlags flags) +{ + // Old API feature: we could pass the initial window size as a parameter. This was misleading because it only had an effect if the window didn't have data in the .ini file. + if (size_first_use.x != 0.0f || size_first_use.y != 0.0f) + SetNextWindowSize(size_first_use, ImGuiCond_FirstUseEver); + + // Old API feature: override the window background alpha with a parameter. + if (bg_alpha_override >= 0.0f) + SetNextWindowBgAlpha(bg_alpha_override); + + return Begin(name, p_open, flags); +} +#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +void ImGui::End() +{ + ImGuiContext& g = *GImGui; + + if (g.CurrentWindowStack.Size <= 1 && g.FrameScopePushedImplicitWindow) + { + IM_ASSERT(g.CurrentWindowStack.Size > 1 && "Calling End() too many times!"); + return; // FIXME-ERRORHANDLING + } + IM_ASSERT(g.CurrentWindowStack.Size > 0); + + ImGuiWindow* window = g.CurrentWindow; + + if (window->DC.CurrentColumns) + EndColumns(); + PopClipRect(); // Inner window clip rectangle + + // Stop logging + if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging + LogFinish(); + + // Pop from window stack + g.CurrentWindowStack.pop_back(); + if (window->Flags & ImGuiWindowFlags_Popup) + g.BeginPopupStack.pop_back(); + CheckStacksSize(window, false); + SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back()); +} + +void ImGui::BringWindowToFocusFront(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (g.WindowsFocusOrder.back() == window) + return; + for (int i = g.WindowsFocusOrder.Size - 2; i >= 0; i--) // We can ignore the top-most window + if (g.WindowsFocusOrder[i] == window) + { + memmove(&g.WindowsFocusOrder[i], &g.WindowsFocusOrder[i + 1], (size_t)(g.WindowsFocusOrder.Size - i - 1) * sizeof(ImGuiWindow*)); + g.WindowsFocusOrder[g.WindowsFocusOrder.Size - 1] = window; + break; + } +} + +void ImGui::BringWindowToDisplayFront(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* current_front_window = g.Windows.back(); + if (current_front_window == window || current_front_window->RootWindow == window) + return; + for (int i = g.Windows.Size - 2; i >= 0; i--) // We can ignore the top-most window + if (g.Windows[i] == window) + { + memmove(&g.Windows[i], &g.Windows[i + 1], (size_t)(g.Windows.Size - i - 1) * sizeof(ImGuiWindow*)); + g.Windows[g.Windows.Size - 1] = window; + break; + } +} + +void ImGui::BringWindowToDisplayBack(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (g.Windows[0] == window) + return; + for (int i = 0; i < g.Windows.Size; i++) + if (g.Windows[i] == window) + { + memmove(&g.Windows[1], &g.Windows[0], (size_t)i * sizeof(ImGuiWindow*)); + g.Windows[0] = window; + break; + } +} + +// Moving window to front of display and set focus (which happens to be back of our sorted list) +void ImGui::FocusWindow(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + + if (g.NavWindow != window) + { + g.NavWindow = window; + if (window && g.NavDisableMouseHover) + g.NavMousePosDirty = true; + g.NavInitRequest = false; + g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId + g.NavIdIsAlive = false; + g.NavLayer = ImGuiNavLayer_Main; + //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); + } + + // Close popups if any + ClosePopupsOverWindow(window, false); + + // Passing NULL allow to disable keyboard focus + if (!window) + return; + + // Move the root window to the top of the pile + if (window->RootWindow) + window = window->RootWindow; + + // Steal focus on active widgets + if (window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement should be unnecessary. Need further testing before removing it.. + if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != window) + ClearActiveID(); + + // Bring to front + BringWindowToFocusFront(window); + if (!(window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) + BringWindowToDisplayFront(window); +} + +void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window) +{ + ImGuiContext& g = *GImGui; + + int start_idx = g.WindowsFocusOrder.Size - 1; + if (under_this_window != NULL) + { + int under_this_window_idx = FindWindowFocusIndex(under_this_window); + if (under_this_window_idx != -1) + start_idx = under_this_window_idx - 1; + } + for (int i = start_idx; i >= 0; i--) + { + // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user. + ImGuiWindow* window = g.WindowsFocusOrder[i]; + if (window != ignore_window && window->WasActive && !(window->Flags & ImGuiWindowFlags_ChildWindow)) + if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) + { + ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window); + FocusWindow(focus_window); + return; + } + } + FocusWindow(NULL); +} + +void ImGui::SetNextItemWidth(float item_width) +{ + ImGuiContext& g = *GImGui; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasWidth; + g.NextItemData.Width = item_width; +} + +void ImGui::PushItemWidth(float item_width) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); + window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); + g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; +} + +void ImGui::PushMultiItemsWidths(int components, float w_full) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + const ImGuiStyle& style = g.Style; + const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); + const float w_item_last = ImMax(1.0f, (float)(int)(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + window->DC.ItemWidthStack.push_back(w_item_last); + for (int i = 0; i < components-1; i++) + window->DC.ItemWidthStack.push_back(w_item_one); + window->DC.ItemWidth = window->DC.ItemWidthStack.back(); + g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; +} + +void ImGui::PopItemWidth() +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.ItemWidthStack.pop_back(); + window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back(); +} + +// Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(). +// The SetNextItemWidth() data is generally cleared/consumed by ItemAdd() or NextItemData.ClearFlags() +float ImGui::CalcItemWidth() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + float w; + if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) + w = g.NextItemData.Width; + else + w = window->DC.ItemWidth; + if (w < 0.0f) + { + float region_max_x = GetContentRegionMaxAbs().x; + w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); + } + w = (float)(int)w; + return w; +} + +// [Internal] Calculate full item size given user provided 'size' parameter and default width/height. Default width is often == CalcItemWidth(). +// Those two functions CalcItemWidth vs CalcItemSize are awkwardly named because they are not fully symmetrical. +// Note that only CalcItemWidth() is publicly exposed. +// The 4.0f here may be changed to match CalcItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) +ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + + ImVec2 region_max; + if (size.x < 0.0f || size.y < 0.0f) + region_max = GetContentRegionMaxAbs(); + + if (size.x == 0.0f) + size.x = default_w; + else if (size.x < 0.0f) + size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x); + + if (size.y == 0.0f) + size.y = default_h; + else if (size.y < 0.0f) + size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y); + + return size; +} + +void ImGui::SetCurrentFont(ImFont* font) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(font && font->IsLoaded()); // Font Atlas not created. Did you call io.Fonts->GetTexDataAsRGBA32 / GetTexDataAsAlpha8 ? + IM_ASSERT(font->Scale > 0.0f); + g.Font = font; + g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale); + g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f; + + ImFontAtlas* atlas = g.Font->ContainerAtlas; + g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel; + g.DrawListSharedData.Font = g.Font; + g.DrawListSharedData.FontSize = g.FontSize; +} + +void ImGui::PushFont(ImFont* font) +{ + ImGuiContext& g = *GImGui; + if (!font) + font = GetDefaultFont(); + SetCurrentFont(font); + g.FontStack.push_back(font); + g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID); +} + +void ImGui::PopFont() +{ + ImGuiContext& g = *GImGui; + g.CurrentWindow->DrawList->PopTextureID(); + g.FontStack.pop_back(); + SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back()); +} + +void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (enabled) + window->DC.ItemFlags |= option; + else + window->DC.ItemFlags &= ~option; + window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); +} + +void ImGui::PopItemFlag() +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.ItemFlagsStack.pop_back(); + window->DC.ItemFlags = window->DC.ItemFlagsStack.empty() ? ImGuiItemFlags_Default_ : window->DC.ItemFlagsStack.back(); +} + +// FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. +void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) +{ + PushItemFlag(ImGuiItemFlags_NoTabStop, !allow_keyboard_focus); +} + +void ImGui::PopAllowKeyboardFocus() +{ + PopItemFlag(); +} + +void ImGui::PushButtonRepeat(bool repeat) +{ + PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); +} + +void ImGui::PopButtonRepeat() +{ + PopItemFlag(); +} + +void ImGui::PushTextWrapPos(float wrap_pos_x) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.TextWrapPos = wrap_pos_x; + window->DC.TextWrapPosStack.push_back(wrap_pos_x); +} + +void ImGui::PopTextWrapPos() +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.TextWrapPosStack.pop_back(); + window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back(); +} + +// FIXME: This may incur a round-trip (if the end user got their data from a float4) but eventually we aim to store the in-flight colors as ImU32 +void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) +{ + ImGuiContext& g = *GImGui; + ImGuiColorMod backup; + backup.Col = idx; + backup.BackupValue = g.Style.Colors[idx]; + g.ColorModifiers.push_back(backup); + g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); +} + +void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) +{ + ImGuiContext& g = *GImGui; + ImGuiColorMod backup; + backup.Col = idx; + backup.BackupValue = g.Style.Colors[idx]; + g.ColorModifiers.push_back(backup); + g.Style.Colors[idx] = col; +} + +void ImGui::PopStyleColor(int count) +{ + ImGuiContext& g = *GImGui; + while (count > 0) + { + ImGuiColorMod& backup = g.ColorModifiers.back(); + g.Style.Colors[backup.Col] = backup.BackupValue; + g.ColorModifiers.pop_back(); + count--; + } +} + +struct ImGuiStyleVarInfo +{ + ImGuiDataType Type; + ImU32 Count; + ImU32 Offset; + void* GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); } +}; + +static const ImGuiStyleVarInfo GStyleVarInfo[] = +{ + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign +}; + +static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx) +{ + IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT); + IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); + return &GStyleVarInfo[idx]; +} + +void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) +{ + const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) + { + ImGuiContext& g = *GImGui; + float* pvar = (float*)var_info->GetVarPtr(&g.Style); + g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; + return; + } + IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!"); +} + +void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) +{ + const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) + { + ImGuiContext& g = *GImGui; + ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); + g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; + return; + } + IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!"); +} + +void ImGui::PopStyleVar(int count) +{ + ImGuiContext& g = *GImGui; + while (count > 0) + { + // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. + ImGuiStyleMod& backup = g.StyleModifiers.back(); + const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); + void* data = info->GetVarPtr(&g.Style); + if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } + else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } + g.StyleModifiers.pop_back(); + count--; + } +} + +const char* ImGui::GetStyleColorName(ImGuiCol idx) +{ + // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1"; + switch (idx) + { + case ImGuiCol_Text: return "Text"; + case ImGuiCol_TextDisabled: return "TextDisabled"; + case ImGuiCol_WindowBg: return "WindowBg"; + case ImGuiCol_ChildBg: return "ChildBg"; + case ImGuiCol_PopupBg: return "PopupBg"; + case ImGuiCol_Border: return "Border"; + case ImGuiCol_BorderShadow: return "BorderShadow"; + case ImGuiCol_FrameBg: return "FrameBg"; + case ImGuiCol_FrameBgHovered: return "FrameBgHovered"; + case ImGuiCol_FrameBgActive: return "FrameBgActive"; + case ImGuiCol_TitleBg: return "TitleBg"; + case ImGuiCol_TitleBgActive: return "TitleBgActive"; + case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed"; + case ImGuiCol_MenuBarBg: return "MenuBarBg"; + case ImGuiCol_ScrollbarBg: return "ScrollbarBg"; + case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab"; + case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered"; + case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive"; + case ImGuiCol_CheckMark: return "CheckMark"; + case ImGuiCol_SliderGrab: return "SliderGrab"; + case ImGuiCol_SliderGrabActive: return "SliderGrabActive"; + case ImGuiCol_Button: return "Button"; + case ImGuiCol_ButtonHovered: return "ButtonHovered"; + case ImGuiCol_ButtonActive: return "ButtonActive"; + case ImGuiCol_Header: return "Header"; + case ImGuiCol_HeaderHovered: return "HeaderHovered"; + case ImGuiCol_HeaderActive: return "HeaderActive"; + case ImGuiCol_Separator: return "Separator"; + case ImGuiCol_SeparatorHovered: return "SeparatorHovered"; + case ImGuiCol_SeparatorActive: return "SeparatorActive"; + case ImGuiCol_ResizeGrip: return "ResizeGrip"; + case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; + case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; + case ImGuiCol_Tab: return "Tab"; + case ImGuiCol_TabHovered: return "TabHovered"; + case ImGuiCol_TabActive: return "TabActive"; + case ImGuiCol_TabUnfocused: return "TabUnfocused"; + case ImGuiCol_TabUnfocusedActive: return "TabUnfocusedActive"; + case ImGuiCol_PlotLines: return "PlotLines"; + case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; + case ImGuiCol_PlotHistogram: return "PlotHistogram"; + case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered"; + case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; + case ImGuiCol_DragDropTarget: return "DragDropTarget"; + case ImGuiCol_NavHighlight: return "NavHighlight"; + case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight"; + case ImGuiCol_NavWindowingDimBg: return "NavWindowingDimBg"; + case ImGuiCol_ModalWindowDimBg: return "ModalWindowDimBg"; + } + IM_ASSERT(0); + return "Unknown"; +} + +bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) +{ + if (window->RootWindow == potential_parent) + return true; + while (window != NULL) + { + if (window == potential_parent) + return true; + window = window->ParentWindow; + } + return false; +} + +bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) +{ + IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function + ImGuiContext& g = *GImGui; + + if (flags & ImGuiHoveredFlags_AnyWindow) + { + if (g.HoveredWindow == NULL) + return false; + } + else + { + switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) + { + case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: + if (g.HoveredRootWindow != g.CurrentWindow->RootWindow) + return false; + break; + case ImGuiHoveredFlags_RootWindow: + if (g.HoveredWindow != g.CurrentWindow->RootWindow) + return false; + break; + case ImGuiHoveredFlags_ChildWindows: + if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow)) + return false; + break; + default: + if (g.HoveredWindow != g.CurrentWindow) + return false; + break; + } + } + + if (!IsWindowContentHoverable(g.HoveredWindow, flags)) + return false; + if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) + if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId) + return false; + return true; +} + +bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) +{ + ImGuiContext& g = *GImGui; + + if (flags & ImGuiFocusedFlags_AnyWindow) + return g.NavWindow != NULL; + + IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End() + switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows)) + { + case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows: + return g.NavWindow && g.NavWindow->RootWindow == g.CurrentWindow->RootWindow; + case ImGuiFocusedFlags_RootWindow: + return g.NavWindow == g.CurrentWindow->RootWindow; + case ImGuiFocusedFlags_ChildWindows: + return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow); + default: + return g.NavWindow == g.CurrentWindow; + } +} + +// Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) +// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly. +// If you want a window to never be focused, you may use the e.g. NoInputs flag. +bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) +{ + return window->Active && window == window->RootWindow && !(window->Flags & ImGuiWindowFlags_NoNavFocus); +} + +float ImGui::GetWindowWidth() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Size.x; +} + +float ImGui::GetWindowHeight() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Size.y; +} + +ImVec2 ImGui::GetWindowPos() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + return window->Pos; +} + +void ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond) +{ + // Test condition (NB: bit 0 is always true) and clear flags for next time + if (cond && (window->SetWindowPosAllowFlags & cond) == 0) + return; + + IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. + window->SetWindowPosAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); + window->SetWindowPosVal = ImVec2(FLT_MAX, FLT_MAX); + + // Set + const ImVec2 old_pos = window->Pos; + window->Pos = ImFloor(pos); + ImVec2 offset = window->Pos - old_pos; + window->DC.CursorPos += offset; // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor + window->DC.CursorMaxPos += offset; // And more importantly we need to offset CursorMaxPos/CursorStartPos this so ContentSize calculation doesn't get affected. + window->DC.CursorStartPos += offset; +} + +void ImGui::SetWindowPos(const ImVec2& pos, ImGuiCond cond) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + SetWindowPos(window, pos, cond); +} + +void ImGui::SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond) +{ + if (ImGuiWindow* window = FindWindowByName(name)) + SetWindowPos(window, pos, cond); +} + +ImVec2 ImGui::GetWindowSize() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->Size; +} + +void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond) +{ + // Test condition (NB: bit 0 is always true) and clear flags for next time + if (cond && (window->SetWindowSizeAllowFlags & cond) == 0) + return; + + IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. + window->SetWindowSizeAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); + + // Set + if (size.x > 0.0f) + { + window->AutoFitFramesX = 0; + window->SizeFull.x = ImFloor(size.x); + } + else + { + window->AutoFitFramesX = 2; + window->AutoFitOnlyGrows = false; + } + if (size.y > 0.0f) + { + window->AutoFitFramesY = 0; + window->SizeFull.y = ImFloor(size.y); + } + else + { + window->AutoFitFramesY = 2; + window->AutoFitOnlyGrows = false; + } +} + +void ImGui::SetWindowSize(const ImVec2& size, ImGuiCond cond) +{ + SetWindowSize(GImGui->CurrentWindow, size, cond); +} + +void ImGui::SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond) +{ + if (ImGuiWindow* window = FindWindowByName(name)) + SetWindowSize(window, size, cond); +} + +void ImGui::SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond) +{ + // Test condition (NB: bit 0 is always true) and clear flags for next time + if (cond && (window->SetWindowCollapsedAllowFlags & cond) == 0) + return; + window->SetWindowCollapsedAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); + + // Set + window->Collapsed = collapsed; +} + +void ImGui::SetWindowCollapsed(bool collapsed, ImGuiCond cond) +{ + SetWindowCollapsed(GImGui->CurrentWindow, collapsed, cond); +} + +bool ImGui::IsWindowCollapsed() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->Collapsed; +} + +bool ImGui::IsWindowAppearing() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->Appearing; +} + +void ImGui::SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond) +{ + if (ImGuiWindow* window = FindWindowByName(name)) + SetWindowCollapsed(window, collapsed, cond); +} + +void ImGui::SetWindowFocus() +{ + FocusWindow(GImGui->CurrentWindow); +} + +void ImGui::SetWindowFocus(const char* name) +{ + if (name) + { + if (ImGuiWindow* window = FindWindowByName(name)) + FocusWindow(window); + } + else + { + FocusWindow(NULL); + } +} + +void ImGui::SetNextWindowPos(const ImVec2& pos, ImGuiCond cond, const ImVec2& pivot) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasPos; + g.NextWindowData.PosVal = pos; + g.NextWindowData.PosPivotVal = pivot; + g.NextWindowData.PosCond = cond ? cond : ImGuiCond_Always; +} + +void ImGui::SetNextWindowSize(const ImVec2& size, ImGuiCond cond) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasSize; + g.NextWindowData.SizeVal = size; + g.NextWindowData.SizeCond = cond ? cond : ImGuiCond_Always; +} + +void ImGui::SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback, void* custom_callback_user_data) +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasSizeConstraint; + g.NextWindowData.SizeConstraintRect = ImRect(size_min, size_max); + g.NextWindowData.SizeCallback = custom_callback; + g.NextWindowData.SizeCallbackUserData = custom_callback_user_data; +} + +// Content size = inner scrollable rectangle, padded with WindowPadding. +// SetNextWindowContentSize(ImVec2(100,100) + ImGuiWindowFlags_AlwaysAutoResize will always allow submitting a 100x100 item. +void ImGui::SetNextWindowContentSize(const ImVec2& size) +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasContentSize; + g.NextWindowData.ContentSizeVal = size; +} + +void ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiCond cond) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasCollapsed; + g.NextWindowData.CollapsedVal = collapsed; + g.NextWindowData.CollapsedCond = cond ? cond : ImGuiCond_Always; +} + +void ImGui::SetNextWindowFocus() +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasFocus; +} + +void ImGui::SetNextWindowBgAlpha(float alpha) +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasBgAlpha; + g.NextWindowData.BgAlphaVal = alpha; +} + +// FIXME: This is in window space (not screen space!). We should try to obsolete all those functions. +ImVec2 ImGui::GetContentRegionMax() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 mx = window->ContentsRegionRect.Max - window->Pos; + if (window->DC.CurrentColumns) + mx.x = window->WorkRect.Max.x - window->Pos.x; + return mx; +} + +// [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. +ImVec2 ImGui::GetContentRegionMaxAbs() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 mx = window->ContentsRegionRect.Max; + if (window->DC.CurrentColumns) + mx.x = window->WorkRect.Max.x; + return mx; +} + +ImVec2 ImGui::GetContentRegionAvail() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return GetContentRegionMaxAbs() - window->DC.CursorPos; +} + +// In window space (not screen space!) +ImVec2 ImGui::GetWindowContentRegionMin() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ContentsRegionRect.Min - window->Pos; +} + +ImVec2 ImGui::GetWindowContentRegionMax() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ContentsRegionRect.Max - window->Pos; +} + +float ImGui::GetWindowContentRegionWidth() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ContentsRegionRect.GetWidth(); +} + +float ImGui::GetTextLineHeight() +{ + ImGuiContext& g = *GImGui; + return g.FontSize; +} + +float ImGui::GetTextLineHeightWithSpacing() +{ + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.ItemSpacing.y; +} + +float ImGui::GetFrameHeight() +{ + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.FramePadding.y * 2.0f; +} + +float ImGui::GetFrameHeightWithSpacing() +{ + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y; +} + +ImDrawList* ImGui::GetWindowDrawList() +{ + ImGuiWindow* window = GetCurrentWindow(); + return window->DrawList; +} + +ImFont* ImGui::GetFont() +{ + return GImGui->Font; +} + +float ImGui::GetFontSize() +{ + return GImGui->FontSize; +} + +ImVec2 ImGui::GetFontTexUvWhitePixel() +{ + return GImGui->DrawListSharedData.TexUvWhitePixel; +} + +void ImGui::SetWindowFontScale(float scale) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->FontWindowScale = scale; + g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); +} + +// User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient. +// Conversion happens as we pass the value to user, but it makes our naming convention confusing because GetCursorPos() == (DC.CursorPos - window.Pos). May want to rename 'DC.CursorPos'. +ImVec2 ImGui::GetCursorPos() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorPos - window->Pos + window->Scroll; +} + +float ImGui::GetCursorPosX() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorPos.x - window->Pos.x + window->Scroll.x; +} + +float ImGui::GetCursorPosY() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorPos.y - window->Pos.y + window->Scroll.y; +} + +void ImGui::SetCursorPos(const ImVec2& local_pos) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.CursorPos = window->Pos - window->Scroll + local_pos; + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); +} + +void ImGui::SetCursorPosX(float x) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + x; + window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x); +} + +void ImGui::SetCursorPosY(float y) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.CursorPos.y = window->Pos.y - window->Scroll.y + y; + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y); +} + +ImVec2 ImGui::GetCursorStartPos() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorStartPos - window->Pos; +} + +ImVec2 ImGui::GetCursorScreenPos() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorPos; +} + +void ImGui::SetCursorScreenPos(const ImVec2& pos) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.CursorPos = pos; + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); +} + +void ImGui::ActivateItem(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.NavNextActivateId = id; +} + +void ImGui::SetKeyboardFocusHere(int offset) +{ + IM_ASSERT(offset >= -1); // -1 is allowed but not below + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + g.FocusRequestNextWindow = window; + g.FocusRequestNextCounterAll = window->DC.FocusCounterAll + 1 + offset; + g.FocusRequestNextCounterTab = INT_MAX; +} + +void ImGui::SetItemDefaultFocus() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (!window->Appearing) + return; + if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent) + { + g.NavInitRequest = false; + g.NavInitResultId = g.NavWindow->DC.LastItemId; + g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos); + NavUpdateAnyRequestFlag(); + if (!IsItemVisible()) + SetScrollHereY(); + } +} + +void ImGui::SetStateStorage(ImGuiStorage* tree) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->DC.StateStorage = tree ? tree : &window->StateStorage; +} + +ImGuiStorage* ImGui::GetStateStorage() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->DC.StateStorage; +} + +void ImGui::PushID(const char* str_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(str_id)); +} + +void ImGui::PushID(const char* str_id_begin, const char* str_id_end) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end)); +} + +void ImGui::PushID(const void* ptr_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); +} + +void ImGui::PushID(int int_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(int_id)); +} + +// Push a given id value ignoring the ID stack as a seed. +void ImGui::PushOverrideID(ImGuiID id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(id); +} + +void ImGui::PopID() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.pop_back(); +} + +ImGuiID ImGui::GetID(const char* str_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id); +} + +ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id_begin, str_id_end); +} + +ImGuiID ImGui::GetID(const void* ptr_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(ptr_id); +} + +bool ImGui::IsRectVisible(const ImVec2& size) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); +} + +bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); +} + +// Lock horizontal starting position + capture group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) +void ImGui::BeginGroup() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + + window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1); + ImGuiGroupData& group_data = window->DC.GroupStack.back(); + group_data.BackupCursorPos = window->DC.CursorPos; + group_data.BackupCursorMaxPos = window->DC.CursorMaxPos; + group_data.BackupIndent = window->DC.Indent; + group_data.BackupGroupOffset = window->DC.GroupOffset; + group_data.BackupCurrLineSize = window->DC.CurrLineSize; + group_data.BackupCurrLineTextBaseOffset = window->DC.CurrLineTextBaseOffset; + group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive; + group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive; + group_data.EmitItem = true; + + window->DC.GroupOffset.x = window->DC.CursorPos.x - window->Pos.x - window->DC.ColumnsOffset.x; + window->DC.Indent = window->DC.GroupOffset; + window->DC.CursorMaxPos = window->DC.CursorPos; + window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + if (g.LogEnabled) + g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return +} + +void ImGui::EndGroup() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + IM_ASSERT(!window->DC.GroupStack.empty()); // Mismatched BeginGroup()/EndGroup() calls + + ImGuiGroupData& group_data = window->DC.GroupStack.back(); + + ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos)); + + window->DC.CursorPos = group_data.BackupCursorPos; + window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos); + window->DC.Indent = group_data.BackupIndent; + window->DC.GroupOffset = group_data.BackupGroupOffset; + window->DC.CurrLineSize = group_data.BackupCurrLineSize; + window->DC.CurrLineTextBaseOffset = group_data.BackupCurrLineTextBaseOffset; + if (g.LogEnabled) + g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return + + if (!group_data.EmitItem) + { + window->DC.GroupStack.pop_back(); + return; + } + + window->DC.CurrLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now. + ItemSize(group_bb.GetSize(), 0.0f); + ItemAdd(group_bb, 0); + + // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group. + // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets. + // Also if you grep for LastItemId you'll notice it is only used in that context. + // (The tests not symmetrical because ActiveIdIsAlive is an ID itself, in order to be able to handle ActiveId being overwritten during the frame.) + const bool group_contains_curr_active_id = (group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId; + const bool group_contains_prev_active_id = !group_data.BackupActiveIdPreviousFrameIsAlive && g.ActiveIdPreviousFrameIsAlive; + if (group_contains_curr_active_id) + window->DC.LastItemId = g.ActiveId; + else if (group_contains_prev_active_id) + window->DC.LastItemId = g.ActiveIdPreviousFrame; + window->DC.LastItemRect = group_bb; + + // Forward Edited flag + if (group_contains_curr_active_id && g.ActiveIdHasBeenEditedThisFrame) + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; + + // Forward Deactivated flag + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDeactivated; + if (group_contains_prev_active_id && g.ActiveId != g.ActiveIdPreviousFrame) + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Deactivated; + + window->DC.GroupStack.pop_back(); + //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] +} + +// Gets back to previous line and continue with horizontal layout +// offset_from_start_x == 0 : follow right after previous item +// offset_from_start_x != 0 : align to specified x position (relative to window/group left) +// spacing_w < 0 : use default spacing if pos_x == 0, no spacing if pos_x != 0 +// spacing_w >= 0 : enforce spacing amount +void ImGui::SameLine(float offset_from_start_x, float spacing_w) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + if (offset_from_start_x != 0.0f) + { + if (spacing_w < 0.0f) spacing_w = 0.0f; + window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x; + window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; + } + else + { + if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x; + window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w; + window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; + } + window->DC.CurrLineSize = window->DC.PrevLineSize; + window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; +} + +void ImGui::Indent(float indent_w) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->DC.Indent.x += (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; + window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; +} + +void ImGui::Unindent(float indent_w) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->DC.Indent.x -= (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; + window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; +} + + +//----------------------------------------------------------------------------- +// [SECTION] SCROLLING +//----------------------------------------------------------------------------- + +static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges) +{ + ImGuiContext& g = *GImGui; + ImVec2 scroll = window->Scroll; + if (window->ScrollTarget.x < FLT_MAX) + { + float cr_x = window->ScrollTargetCenterRatio.x; + float target_x = window->ScrollTarget.x; + if (snap_on_edges && cr_x <= 0.0f && target_x <= window->WindowPadding.x) + target_x = 0.0f; + else if (snap_on_edges && cr_x >= 1.0f && target_x >= window->ContentSize.x + window->WindowPadding.x + g.Style.ItemSpacing.x) + target_x = window->ContentSize.x + window->WindowPadding.x * 2.0f; + scroll.x = target_x - cr_x * (window->SizeFull.x - window->ScrollbarSizes.x); + } + if (window->ScrollTarget.y < FLT_MAX) + { + // 'snap_on_edges' allows for a discontinuity at the edge of scrolling limits to take account of WindowPadding so that scrolling to make the last item visible scroll far enough to see the padding. + float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); + float cr_y = window->ScrollTargetCenterRatio.y; + float target_y = window->ScrollTarget.y; + if (snap_on_edges && cr_y <= 0.0f && target_y <= window->WindowPadding.y) + target_y = 0.0f; + if (snap_on_edges && cr_y >= 1.0f && target_y >= window->ContentSize.y + window->WindowPadding.y + g.Style.ItemSpacing.y) + target_y = window->ContentSize.y + window->WindowPadding.y * 2.0f; + scroll.y = target_y - cr_y * (window->SizeFull.y - window->ScrollbarSizes.y - decoration_up_height); + } + scroll = ImMax(scroll, ImVec2(0.0f, 0.0f)); + if (!window->Collapsed && !window->SkipItems) + { + scroll.x = ImMin(scroll.x, window->ScrollMax.x); + scroll.y = ImMin(scroll.y, window->ScrollMax.y); + } + return scroll; +} + +// Scroll to keep newly navigated item fully into view +ImVec2 ImGui::ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect) +{ + ImGuiContext& g = *GImGui; + ImRect window_rect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1)); + //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG] + + ImVec2 delta_scroll; + if (!window_rect.Contains(item_rect)) + { + if (window->ScrollbarX && item_rect.Min.x < window_rect.Min.x) + SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x + g.Style.ItemSpacing.x, 0.0f); + else if (window->ScrollbarX && item_rect.Max.x >= window_rect.Max.x) + SetScrollFromPosX(window, item_rect.Max.x - window->Pos.x + g.Style.ItemSpacing.x, 1.0f); + if (item_rect.Min.y < window_rect.Min.y) + SetScrollFromPosY(window, item_rect.Min.y - window->Pos.y - g.Style.ItemSpacing.y, 0.0f); + else if (item_rect.Max.y >= window_rect.Max.y) + SetScrollFromPosY(window, item_rect.Max.y - window->Pos.y + g.Style.ItemSpacing.y, 1.0f); + + ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window, false); + delta_scroll = next_scroll - window->Scroll; + } + + // Also scroll parent window to keep us into view if necessary + if (window->Flags & ImGuiWindowFlags_ChildWindow) + delta_scroll += ScrollToBringRectIntoView(window->ParentWindow, ImRect(item_rect.Min - delta_scroll, item_rect.Max - delta_scroll)); + + return delta_scroll; +} + +float ImGui::GetScrollX() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Scroll.x; +} + +float ImGui::GetScrollY() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Scroll.y; +} + +float ImGui::GetScrollMaxX() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ScrollMax.x; +} + +float ImGui::GetScrollMaxY() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ScrollMax.y; +} + +void ImGui::SetScrollX(float scroll_x) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->ScrollTarget.x = scroll_x; + window->ScrollTargetCenterRatio.x = 0.0f; +} + +void ImGui::SetScrollY(float scroll_y) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->ScrollTarget.y = scroll_y; + window->ScrollTargetCenterRatio.y = 0.0f; +} + +void ImGui::SetScrollX(ImGuiWindow* window, float new_scroll_x) +{ + window->ScrollTarget.x = new_scroll_x; + window->ScrollTargetCenterRatio.x = 0.0f; +} + +void ImGui::SetScrollY(ImGuiWindow* window, float new_scroll_y) +{ + window->ScrollTarget.y = new_scroll_y; + window->ScrollTargetCenterRatio.y = 0.0f; +} + + +void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio) +{ + // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size + IM_ASSERT(center_x_ratio >= 0.0f && center_x_ratio <= 1.0f); + window->ScrollTarget.x = (float)(int)(local_x + window->Scroll.x); + window->ScrollTargetCenterRatio.x = center_x_ratio; +} + +void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio) +{ + // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size + IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); + const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); + local_y -= decoration_up_height; + window->ScrollTarget.y = (float)(int)(local_y + window->Scroll.y); + window->ScrollTargetCenterRatio.y = center_y_ratio; +} + +void ImGui::SetScrollFromPosX(float local_x, float center_x_ratio) +{ + ImGuiContext& g = *GImGui; + SetScrollFromPosX(g.CurrentWindow, local_x, center_x_ratio); +} + +void ImGui::SetScrollFromPosY(float local_y, float center_y_ratio) +{ + ImGuiContext& g = *GImGui; + SetScrollFromPosY(g.CurrentWindow, local_y, center_y_ratio); +} + +// center_x_ratio: 0.0f left of last item, 0.5f horizontal center of last item, 1.0f right of last item. +void ImGui::SetScrollHereX(float center_x_ratio) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + float target_x = window->DC.LastItemRect.Min.x - window->Pos.x; // Left of last item, in window space + float last_item_width = window->DC.LastItemRect.GetWidth(); + target_x += (last_item_width * center_x_ratio) + (g.Style.ItemSpacing.x * (center_x_ratio - 0.5f) * 2.0f); // Precisely aim before, in the middle or after the last item. + SetScrollFromPosX(target_x, center_x_ratio); +} + +// center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item. +void ImGui::SetScrollHereY(float center_y_ratio) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + float target_y = window->DC.CursorPosPrevLine.y - window->Pos.y; // Top of last item, in window space + target_y += (window->DC.PrevLineSize.y * center_y_ratio) + (g.Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f); // Precisely aim above, in the middle or below the last line. + SetScrollFromPosY(target_y, center_y_ratio); +} + +//----------------------------------------------------------------------------- +// [SECTION] TOOLTIPS +//----------------------------------------------------------------------------- + +void ImGui::BeginTooltip() +{ + ImGuiContext& g = *GImGui; + if (g.DragDropWithinSourceOrTarget) + { + // The default tooltip position is a little offset to give space to see the context menu (it's also clamped within the current viewport/monitor) + // In the context of a dragging tooltip we try to reduce that offset and we enforce following the cursor. + // Whatever we do we want to call SetNextWindowPos() to enforce a tooltip position and disable clipping the tooltip without our display area, like regular tooltip do. + //ImVec2 tooltip_pos = g.IO.MousePos - g.ActiveIdClickOffset - g.Style.WindowPadding; + ImVec2 tooltip_pos = g.IO.MousePos + ImVec2(16 * g.Style.MouseCursorScale, 8 * g.Style.MouseCursorScale); + SetNextWindowPos(tooltip_pos); + SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f); + //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :( + BeginTooltipEx(0, true); + } + else + { + BeginTooltipEx(0, false); + } +} + +// Not exposed publicly as BeginTooltip() because bool parameters are evil. Let's see if other needs arise first. +void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip) +{ + ImGuiContext& g = *GImGui; + char window_name[16]; + ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", g.TooltipOverrideCount); + if (override_previous_tooltip) + if (ImGuiWindow* window = FindWindowByName(window_name)) + if (window->Active) + { + // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. + window->Hidden = true; + window->HiddenFramesCanSkipItems = 1; + ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); + } + ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip|ImGuiWindowFlags_NoInputs|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize; + Begin(window_name, NULL, flags | extra_flags); +} + +void ImGui::EndTooltip() +{ + IM_ASSERT(GetCurrentWindowRead()->Flags & ImGuiWindowFlags_Tooltip); // Mismatched BeginTooltip()/EndTooltip() calls + End(); +} + +void ImGui::SetTooltipV(const char* fmt, va_list args) +{ + ImGuiContext& g = *GImGui; + if (g.DragDropWithinSourceOrTarget) + BeginTooltip(); + else + BeginTooltipEx(0, true); + TextV(fmt, args); + EndTooltip(); +} + +void ImGui::SetTooltip(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + SetTooltipV(fmt, args); + va_end(args); +} + +//----------------------------------------------------------------------------- +// [SECTION] POPUPS +//----------------------------------------------------------------------------- + +bool ImGui::IsPopupOpen(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id; +} + +bool ImGui::IsPopupOpen(const char* str_id) +{ + ImGuiContext& g = *GImGui; + return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == g.CurrentWindow->GetID(str_id); +} + +ImGuiWindow* ImGui::GetTopMostPopupModal() +{ + ImGuiContext& g = *GImGui; + for (int n = g.OpenPopupStack.Size-1; n >= 0; n--) + if (ImGuiWindow* popup = g.OpenPopupStack.Data[n].Window) + if (popup->Flags & ImGuiWindowFlags_Modal) + return popup; + return NULL; +} + +void ImGui::OpenPopup(const char* str_id) +{ + ImGuiContext& g = *GImGui; + OpenPopupEx(g.CurrentWindow->GetID(str_id)); +} + +// Mark popup as open (toggle toward open state). +// Popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. +// Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). +// One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL) +void ImGui::OpenPopupEx(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* parent_window = g.CurrentWindow; + int current_stack_size = g.BeginPopupStack.Size; + ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. + popup_ref.PopupId = id; + popup_ref.Window = NULL; + popup_ref.SourceWindow = g.NavWindow; + popup_ref.OpenFrameCount = g.FrameCount; + popup_ref.OpenParentId = parent_window->IDStack.back(); + popup_ref.OpenPopupPos = NavCalcPreferredRefPos(); + popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos; + + //IMGUI_DEBUG_LOG("OpenPopupEx(0x%08X)\n", g.FrameCount, id); + if (g.OpenPopupStack.Size < current_stack_size + 1) + { + g.OpenPopupStack.push_back(popup_ref); + } + else + { + // Gently handle the user mistakenly calling OpenPopup() every frame. It is a programming mistake! However, if we were to run the regular code path, the ui + // would become completely unusable because the popup will always be in hidden-while-calculating-size state _while_ claiming focus. Which would be a very confusing + // situation for the programmer. Instead, we silently allow the popup to proceed, it will keep reappearing and the programming error will be more obvious to understand. + if (g.OpenPopupStack[current_stack_size].PopupId == id && g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1) + { + g.OpenPopupStack[current_stack_size].OpenFrameCount = popup_ref.OpenFrameCount; + } + else + { + // Close child popups if any, then flag popup for open/reopen + g.OpenPopupStack.resize(current_stack_size + 1); + g.OpenPopupStack[current_stack_size] = popup_ref; + } + + // When reopening a popup we first refocus its parent, otherwise if its parent is itself a popup it would get closed by ClosePopupsOverWindow(). + // This is equivalent to what ClosePopupToLevel() does. + //if (g.OpenPopupStack[current_stack_size].PopupId == id) + // FocusWindow(parent_window); + } +} + +void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup) +{ + ImGuiContext& g = *GImGui; + if (g.OpenPopupStack.empty()) + return; + + // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it. + // Don't close our own child popup windows. + int popup_count_to_keep = 0; + if (ref_window) + { + // Find the highest popup which is a descendant of the reference window (generally reference window = NavWindow) + for (; popup_count_to_keep < g.OpenPopupStack.Size; popup_count_to_keep++) + { + ImGuiPopupData& popup = g.OpenPopupStack[popup_count_to_keep]; + if (!popup.Window) + continue; + IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0); + if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow) + continue; + + // Trim the stack when popups are not direct descendant of the reference window (the reference window is often the NavWindow) + bool popup_or_descendent_is_ref_window = false; + for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_is_ref_window; m++) + if (ImGuiWindow* popup_window = g.OpenPopupStack[m].Window) + if (popup_window->RootWindow == ref_window->RootWindow) + popup_or_descendent_is_ref_window = true; + if (!popup_or_descendent_is_ref_window) + break; + } + } + if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below + { + //IMGUI_DEBUG_LOG("ClosePopupsOverWindow(%s) -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep); + ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup); + } +} + +void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); + ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow; + ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window; + g.OpenPopupStack.resize(remaining); + + if (restore_focus_to_window_under_popup) + { + if (focus_window && !focus_window->WasActive && popup_window) + { + // Fallback + FocusTopMostWindowUnderOne(popup_window, NULL); + } + else + { + if (g.NavLayer == 0 && focus_window) + focus_window = NavRestoreLastChildNavWindow(focus_window); + FocusWindow(focus_window); + } + } +} + +// Close the popup we have begin-ed into. +void ImGui::CloseCurrentPopup() +{ + ImGuiContext& g = *GImGui; + int popup_idx = g.BeginPopupStack.Size - 1; + if (popup_idx < 0 || popup_idx >= g.OpenPopupStack.Size || g.BeginPopupStack[popup_idx].PopupId != g.OpenPopupStack[popup_idx].PopupId) + return; + + // Closing a menu closes its top-most parent popup (unless a modal) + while (popup_idx > 0) + { + ImGuiWindow* popup_window = g.OpenPopupStack[popup_idx].Window; + ImGuiWindow* parent_popup_window = g.OpenPopupStack[popup_idx - 1].Window; + bool close_parent = false; + if (popup_window && (popup_window->Flags & ImGuiWindowFlags_ChildMenu)) + if (parent_popup_window == NULL || !(parent_popup_window->Flags & ImGuiWindowFlags_Modal)) + close_parent = true; + if (!close_parent) + break; + popup_idx--; + } + //IMGUI_DEBUG_LOG("CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); + ClosePopupToLevel(popup_idx, true); + + // A common pattern is to close a popup when selecting a menu item/selectable that will open another window. + // To improve this usage pattern, we avoid nav highlight for a single frame in the parent window. + // Similarly, we could avoid mouse hover highlight in this window but it is less visually problematic. + if (ImGuiWindow* window = g.NavWindow) + window->DC.NavHideHighlightOneFrame = true; +} + +bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags) +{ + ImGuiContext& g = *GImGui; + if (!IsPopupOpen(id)) + { + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values + return false; + } + + char name[20]; + if (extra_flags & ImGuiWindowFlags_ChildMenu) + ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth + else + ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame + + bool is_open = Begin(name, NULL, extra_flags | ImGuiWindowFlags_Popup); + if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display) + EndPopup(); + + return is_open; +} + +bool ImGui::BeginPopup(const char* str_id, ImGuiWindowFlags flags) +{ + ImGuiContext& g = *GImGui; + if (g.OpenPopupStack.Size <= g.BeginPopupStack.Size) // Early out for performance + { + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values + return false; + } + flags |= ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings; + return BeginPopupEx(g.CurrentWindow->GetID(str_id), flags); +} + +// If 'p_open' is specified for a modal popup window, the popup will have a regular close button which will close the popup. +// Note that popup visibility status is owned by Dear ImGui (and manipulated with e.g. OpenPopup) so the actual value of *p_open is meaningless here. +bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + const ImGuiID id = window->GetID(name); + if (!IsPopupOpen(id)) + { + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values + return false; + } + + // Center modal windows by default + // FIXME: Should test for (PosCond & window->SetWindowPosAllowFlags) with the upcoming window. + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) == 0) + SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + + flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings; + const bool is_open = Begin(name, p_open, flags); + if (!is_open || (p_open && !*p_open)) // NB: is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + { + EndPopup(); + if (is_open) + ClosePopupToLevel(g.BeginPopupStack.Size, true); + return false; + } + return is_open; +} + +void ImGui::EndPopup() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.CurrentWindow->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginPopup()/EndPopup() calls + IM_ASSERT(g.BeginPopupStack.Size > 0); + + // Make all menus and popups wrap around for now, may need to expose that policy. + NavMoveRequestTryWrapping(g.CurrentWindow, ImGuiNavMoveFlags_LoopY); + + End(); +} + +bool ImGui::OpenPopupOnItemClick(const char* str_id, int mouse_button) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + { + ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! + IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) + OpenPopupEx(id); + return true; + } + return false; +} + +// This is a helper to handle the simplest case of associating one named popup to one given widget. +// You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). +// You can pass a NULL str_id to use the identifier of the last item. +bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + if (window->SkipItems) + return false; + ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! + IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) + if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + OpenPopupEx(id); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); +} + +bool ImGui::BeginPopupContextWindow(const char* str_id, int mouse_button, bool also_over_items) +{ + if (!str_id) + str_id = "window_context"; + ImGuiID id = GImGui->CurrentWindow->GetID(str_id); + if (IsMouseReleased(mouse_button) && IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + if (also_over_items || !IsAnyItemHovered()) + OpenPopupEx(id); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); +} + +bool ImGui::BeginPopupContextVoid(const char* str_id, int mouse_button) +{ + if (!str_id) + str_id = "void_context"; + ImGuiID id = GImGui->CurrentWindow->GetID(str_id); + if (IsMouseReleased(mouse_button) && !IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) + OpenPopupEx(id); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); +} + +// r_avoid = the rectangle to avoid (e.g. for tooltip it is a rectangle around the mouse cursor which we want to avoid. for popups it's a small point around the cursor.) +// r_outer = the visible area rectangle, minus safe area padding. If our popup size won't fit because of safe area padding we ignore it. +ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy) +{ + ImVec2 base_pos_clamped = ImClamp(ref_pos, r_outer.Min, r_outer.Max - size); + //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255,0,0,255)); + //GetForegroundDrawList()->AddRect(r_outer.Min, r_outer.Max, IM_COL32(0,255,0,255)); + + // Combo Box policy (we want a connecting edge) + if (policy == ImGuiPopupPositionPolicy_ComboBox) + { + const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Down, ImGuiDir_Right, ImGuiDir_Left, ImGuiDir_Up }; + for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) + { + const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; + if (n != -1 && dir == *last_dir) // Already tried this direction? + continue; + ImVec2 pos; + if (dir == ImGuiDir_Down) pos = ImVec2(r_avoid.Min.x, r_avoid.Max.y); // Below, Toward Right (default) + if (dir == ImGuiDir_Right) pos = ImVec2(r_avoid.Min.x, r_avoid.Min.y - size.y); // Above, Toward Right + if (dir == ImGuiDir_Left) pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Max.y); // Below, Toward Left + if (dir == ImGuiDir_Up) pos = ImVec2(r_avoid.Max.x - size.x, r_avoid.Min.y - size.y); // Above, Toward Left + if (!r_outer.Contains(ImRect(pos, pos + size))) + continue; + *last_dir = dir; + return pos; + } + } + + // Default popup policy + const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left }; + for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) + { + const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; + if (n != -1 && dir == *last_dir) // Already tried this direction? + continue; + float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x); + float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y); + if (avail_w < size.x || avail_h < size.y) + continue; + ImVec2 pos; + pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x; + pos.y = (dir == ImGuiDir_Up) ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down) ? r_avoid.Max.y : base_pos_clamped.y; + *last_dir = dir; + return pos; + } + + // Fallback, try to keep within display + *last_dir = ImGuiDir_None; + ImVec2 pos = ref_pos; + pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x); + pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y); + return pos; +} + +ImRect ImGui::GetWindowAllowedExtentRect(ImGuiWindow* window) +{ + IM_UNUSED(window); + ImVec2 padding = GImGui->Style.DisplaySafeAreaPadding; + ImRect r_screen = GetViewportRect(); + r_screen.Expand(ImVec2((r_screen.GetWidth() > padding.x * 2) ? -padding.x : 0.0f, (r_screen.GetHeight() > padding.y * 2) ? -padding.y : 0.0f)); + return r_screen; +} + +ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + + ImRect r_outer = GetWindowAllowedExtentRect(window); + if (window->Flags & ImGuiWindowFlags_ChildMenu) + { + // Child menus typically request _any_ position within the parent menu item, and then we move the new menu outside the parent bounds. + // This is how we end up with child menus appearing (most-commonly) on the right of the parent menu. + IM_ASSERT(g.CurrentWindow == window); + ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.Size - 2]; + float horizontal_overlap = g.Style.ItemInnerSpacing.x; // We want some overlap to convey the relative depth of each menu (currently the amount of overlap is hard-coded to style.ItemSpacing.x). + ImRect r_avoid; + if (parent_window->DC.MenuBarAppending) + r_avoid = ImRect(-FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight(), FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight() + parent_window->MenuBarHeight()); + else + r_avoid = ImRect(parent_window->Pos.x + horizontal_overlap, -FLT_MAX, parent_window->Pos.x + parent_window->Size.x - horizontal_overlap - parent_window->ScrollbarSizes.x, FLT_MAX); + return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); + } + if (window->Flags & ImGuiWindowFlags_Popup) + { + ImRect r_avoid = ImRect(window->Pos.x - 1, window->Pos.y - 1, window->Pos.x + 1, window->Pos.y + 1); + return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); + } + if (window->Flags & ImGuiWindowFlags_Tooltip) + { + // Position tooltip (always follows mouse) + float sc = g.Style.MouseCursorScale; + ImVec2 ref_pos = NavCalcPreferredRefPos(); + ImRect r_avoid; + if (!g.NavDisableHighlight && g.NavDisableMouseHover && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos)) + r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8); + else + r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * sc, ref_pos.y + 24 * sc); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important. + ImVec2 pos = FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); + if (window->AutoPosLastDirection == ImGuiDir_None) + pos = ref_pos + ImVec2(2, 2); // If there's not enough room, for tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible. + return pos; + } + IM_ASSERT(0); + return window->Pos; +} + + +//----------------------------------------------------------------------------- +// [SECTION] KEYBOARD/GAMEPAD NAVIGATION +//----------------------------------------------------------------------------- + +ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy) +{ + if (ImFabs(dx) > ImFabs(dy)) + return (dx > 0.0f) ? ImGuiDir_Right : ImGuiDir_Left; + return (dy > 0.0f) ? ImGuiDir_Down : ImGuiDir_Up; +} + +static float inline NavScoreItemDistInterval(float a0, float a1, float b0, float b1) +{ + if (a1 < b0) + return a1 - b0; + if (b1 < a0) + return a0 - b1; + return 0.0f; +} + +static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect& r, const ImRect& clip_rect) +{ + if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) + { + r.Min.y = ImClamp(r.Min.y, clip_rect.Min.y, clip_rect.Max.y); + r.Max.y = ImClamp(r.Max.y, clip_rect.Min.y, clip_rect.Max.y); + } + else + { + r.Min.x = ImClamp(r.Min.x, clip_rect.Min.x, clip_rect.Max.x); + r.Max.x = ImClamp(r.Max.x, clip_rect.Min.x, clip_rect.Max.x); + } +} + +// Scoring function for directional navigation. Based on https://gist.github.com/rygorous/6981057 +static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (g.NavLayer != window->DC.NavLayerCurrent) + return false; + + const ImRect& curr = g.NavScoringRectScreen; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width) + g.NavScoringCount++; + + // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring + if (window->ParentWindow == g.NavWindow) + { + IM_ASSERT((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened); + if (!window->ClipRect.Overlaps(cand)) + return false; + cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window + } + + // We perform scoring on items bounding box clipped by the current clipping rectangle on the other axis (clipping on our movement axis would give us equal scores for all clipped items) + // For example, this ensure that items in one column are not reached when moving vertically from items in another column. + NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect); + + // Compute distance between boxes + // FIXME-NAV: Introducing biases for vertical navigation, needs to be removed. + float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x); + float dby = NavScoreItemDistInterval(ImLerp(cand.Min.y, cand.Max.y, 0.2f), ImLerp(cand.Min.y, cand.Max.y, 0.8f), ImLerp(curr.Min.y, curr.Max.y, 0.2f), ImLerp(curr.Min.y, curr.Max.y, 0.8f)); // Scale down on Y to keep using box-distance for vertically touching items + if (dby != 0.0f && dbx != 0.0f) + dbx = (dbx/1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f); + float dist_box = ImFabs(dbx) + ImFabs(dby); + + // Compute distance between centers (this is off by a factor of 2, but we only compare center distances with each other so it doesn't matter) + float dcx = (cand.Min.x + cand.Max.x) - (curr.Min.x + curr.Max.x); + float dcy = (cand.Min.y + cand.Max.y) - (curr.Min.y + curr.Max.y); + float dist_center = ImFabs(dcx) + ImFabs(dcy); // L1 metric (need this for our connectedness guarantee) + + // Determine which quadrant of 'curr' our candidate item 'cand' lies in based on distance + ImGuiDir quadrant; + float dax = 0.0f, day = 0.0f, dist_axial = 0.0f; + if (dbx != 0.0f || dby != 0.0f) + { + // For non-overlapping boxes, use distance between boxes + dax = dbx; + day = dby; + dist_axial = dist_box; + quadrant = ImGetDirQuadrantFromDelta(dbx, dby); + } + else if (dcx != 0.0f || dcy != 0.0f) + { + // For overlapping boxes with different centers, use distance between centers + dax = dcx; + day = dcy; + dist_axial = dist_center; + quadrant = ImGetDirQuadrantFromDelta(dcx, dcy); + } + else + { + // Degenerate case: two overlapping buttons with same center, break ties arbitrarily (note that LastItemId here is really the _previous_ item order, but it doesn't matter) + quadrant = (window->DC.LastItemId < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right; + } + +#if IMGUI_DEBUG_NAV_SCORING + char buf[128]; + if (IsMouseHoveringRect(cand.Min, cand.Max)) + { + ImFormatString(buf, IM_ARRAYSIZE(buf), "dbox (%.2f,%.2f->%.4f)\ndcen (%.2f,%.2f->%.4f)\nd (%.2f,%.2f->%.4f)\nnav %c, quadrant %c", dbx, dby, dist_box, dcx, dcy, dist_center, dax, day, dist_axial, "WENS"[g.NavMoveDir], "WENS"[quadrant]); + ImDrawList* draw_list = GetForegroundDrawList(window); + draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100)); + draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200)); + draw_list->AddRectFilled(cand.Max - ImVec2(4,4), cand.Max + CalcTextSize(buf) + ImVec2(4,4), IM_COL32(40,0,0,150)); + draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf); + } + else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate. + { + if (IsKeyPressedMap(ImGuiKey_C)) { g.NavMoveDirLast = (ImGuiDir)((g.NavMoveDirLast + 1) & 3); g.IO.KeysDownDuration[g.IO.KeyMap[ImGuiKey_C]] = 0.01f; } + if (quadrant == g.NavMoveDir) + { + ImFormatString(buf, IM_ARRAYSIZE(buf), "%.0f/%.0f", dist_box, dist_center); + ImDrawList* draw_list = GetForegroundDrawList(window); + draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200)); + draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf); + } + } + #endif + + // Is it in the quadrant we're interesting in moving to? + bool new_best = false; + if (quadrant == g.NavMoveDir) + { + // Does it beat the current best candidate? + if (dist_box < result->DistBox) + { + result->DistBox = dist_box; + result->DistCenter = dist_center; + return true; + } + if (dist_box == result->DistBox) + { + // Try using distance between center points to break ties + if (dist_center < result->DistCenter) + { + result->DistCenter = dist_center; + new_best = true; + } + else if (dist_center == result->DistCenter) + { + // Still tied! we need to be extra-careful to make sure everything gets linked properly. We consistently break ties by symbolically moving "later" items + // (with higher index) to the right/downwards by an infinitesimal amount since we the current "best" button already (so it must have a lower index), + // this is fairly easy. This rule ensures that all buttons with dx==dy==0 will end up being linked in order of appearance along the x axis. + if (((g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance + new_best = true; + } + } + } + + // Axial check: if 'curr' has no link at all in some direction and 'cand' lies roughly in that direction, add a tentative link. This will only be kept if no "real" matches + // are found, so it only augments the graph produced by the above method using extra links. (important, since it doesn't guarantee strong connectedness) + // This is just to avoid buttons having no links in a particular direction when there's a suitable neighbor. you get good graphs without this too. + // 2017/09/29: FIXME: This now currently only enabled inside menu bars, ideally we'd disable it everywhere. Menus in particular need to catch failure. For general navigation it feels awkward. + // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option? + if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial) // Check axial match + if (g.NavLayer == 1 && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) + if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f)) + { + result->DistAxial = dist_axial; + new_best = true; + } + + return new_best; +} + +// We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) +static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, const ImGuiID id) +{ + ImGuiContext& g = *GImGui; + //if (!g.IO.NavActive) // [2017/10/06] Removed this possibly redundant test but I am not sure of all the side-effects yet. Some of the feature here will need to work regardless of using a _NoNavInputs flag. + // return; + + const ImGuiItemFlags item_flags = window->DC.ItemFlags; + const ImRect nav_bb_rel(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); + + // Process Init Request + if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent) + { + // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback + if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus) || g.NavInitResultId == 0) + { + g.NavInitResultId = id; + g.NavInitResultRectRel = nav_bb_rel; + } + if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus)) + { + g.NavInitRequest = false; // Found a match, clear request + NavUpdateAnyRequestFlag(); + } + } + + // Process Move Request (scoring for navigation) + // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy) + if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled|ImGuiItemFlags_NoNav))) + { + ImGuiNavMoveResult* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; +#if IMGUI_DEBUG_NAV_SCORING + // [DEBUG] Score all items in NavWindow at all times + if (!g.NavMoveRequest) + g.NavMoveDir = g.NavMoveDirLast; + bool new_best = NavScoreItem(result, nav_bb) && g.NavMoveRequest; +#else + bool new_best = g.NavMoveRequest && NavScoreItem(result, nav_bb); +#endif + if (new_best) + { + result->ID = id; + result->SelectScopeId = g.MultiSelectScopeId; + result->Window = window; + result->RectRel = nav_bb_rel; + } + + const float VISIBLE_RATIO = 0.70f; + if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) + if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) + if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb)) + { + result = &g.NavMoveResultLocalVisibleSet; + result->ID = id; + result->SelectScopeId = g.MultiSelectScopeId; + result->Window = window; + result->RectRel = nav_bb_rel; + } + } + + // Update window-relative bounding box of navigated item + if (g.NavId == id) + { + g.NavWindow = window; // Always refresh g.NavWindow, because some operations such as FocusItem() don't have a window. + g.NavLayer = window->DC.NavLayerCurrent; + g.NavIdIsAlive = true; + g.NavIdTabCounter = window->DC.FocusCounterTab; + window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel; // Store item bounding box (relative to window position) + } +} + +bool ImGui::NavMoveRequestButNoResultYet() +{ + ImGuiContext& g = *GImGui; + return g.NavMoveRequest && g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0; +} + +void ImGui::NavMoveRequestCancel() +{ + ImGuiContext& g = *GImGui; + g.NavMoveRequest = false; + NavUpdateAnyRequestFlag(); +} + +void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_None); + NavMoveRequestCancel(); + g.NavMoveDir = move_dir; + g.NavMoveClipDir = clip_dir; + g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; + g.NavMoveRequestFlags = move_flags; + g.NavWindow->NavRectRel[g.NavLayer] = bb_rel; +} + +void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags) +{ + ImGuiContext& g = *GImGui; + if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != 0) + return; + IM_ASSERT(move_flags != 0); // No points calling this with no wrapping + ImRect bb_rel = window->NavRectRel[0]; + + ImGuiDir clip_dir = g.NavMoveDir; + if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + { + bb_rel.Min.x = bb_rel.Max.x = ImMax(window->SizeFull.x, window->ContentSize.x + window->WindowPadding.x * 2.0f) - window->Scroll.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(-bb_rel.GetHeight()); clip_dir = ImGuiDir_Up; } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + { + bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(+bb_rel.GetHeight()); clip_dir = ImGuiDir_Down; } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(-bb_rel.GetWidth()); clip_dir = ImGuiDir_Left; } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(+bb_rel.GetWidth()); clip_dir = ImGuiDir_Right; } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } +} + +// FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0). +// This way we could find the last focused window among our children. It would be much less confusing this way? +static void ImGui::NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window) +{ + ImGuiWindow* parent_window = nav_window; + while (parent_window && (parent_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) + parent_window = parent_window->ParentWindow; + if (parent_window && parent_window != nav_window) + parent_window->NavLastChildNavWindow = nav_window; +} + +// Restore the last focused child. +// Call when we are expected to land on the Main Layer (0) after FocusWindow() +static ImGuiWindow* ImGui::NavRestoreLastChildNavWindow(ImGuiWindow* window) +{ + return window->NavLastChildNavWindow ? window->NavLastChildNavWindow : window; +} + +static void NavRestoreLayer(ImGuiNavLayer layer) +{ + ImGuiContext& g = *GImGui; + g.NavLayer = layer; + if (layer == 0) + g.NavWindow = ImGui::NavRestoreLastChildNavWindow(g.NavWindow); + if (layer == 0 && g.NavWindow->NavLastIds[0] != 0) + ImGui::SetNavIDWithRectRel(g.NavWindow->NavLastIds[0], layer, g.NavWindow->NavRectRel[0]); + else + ImGui::NavInitWindow(g.NavWindow, true); +} + +static inline void ImGui::NavUpdateAnyRequestFlag() +{ + ImGuiContext& g = *GImGui; + g.NavAnyRequest = g.NavMoveRequest || g.NavInitRequest || (IMGUI_DEBUG_NAV_SCORING && g.NavWindow != NULL); + if (g.NavAnyRequest) + IM_ASSERT(g.NavWindow != NULL); +} + +// This needs to be called before we submit any widget (aka in or before Begin) +void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(window == g.NavWindow); + bool init_for_nav = false; + if (!(window->Flags & ImGuiWindowFlags_NoNavInputs)) + if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) + init_for_nav = true; + //IMGUI_DEBUG_LOG("[Nav] NavInitWindow() init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); + if (init_for_nav) + { + SetNavID(0, g.NavLayer); + g.NavInitRequest = true; + g.NavInitRequestFromMove = false; + g.NavInitResultId = 0; + g.NavInitResultRectRel = ImRect(); + NavUpdateAnyRequestFlag(); + } + else + { + g.NavId = window->NavLastIds[0]; + } +} + +static ImVec2 ImGui::NavCalcPreferredRefPos() +{ + ImGuiContext& g = *GImGui; + if (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow) + { + // Mouse (we need a fallback in case the mouse becomes invalid after being used) + if (IsMousePosValid(&g.IO.MousePos)) + return g.IO.MousePos; + return g.LastValidMousePos; + } + else + { + // When navigation is active and mouse is disabled, decide on an arbitrary position around the bottom left of the currently navigated item. + const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer]; + ImVec2 pos = g.NavWindow->Pos + ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); + ImRect visible_rect = GetViewportRect(); + return ImFloor(ImClamp(pos, visible_rect.Min, visible_rect.Max)); // ImFloor() is important because non-integer mouse position application in back-end might be lossy and result in undesirable non-zero delta. + } +} + +float ImGui::GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode) +{ + ImGuiContext& g = *GImGui; + if (mode == ImGuiInputReadMode_Down) + return g.IO.NavInputs[n]; // Instant, read analog input (0.0f..1.0f, as provided by user) + + const float t = g.IO.NavInputsDownDuration[n]; + if (t < 0.0f && mode == ImGuiInputReadMode_Released) // Return 1.0f when just released, no repeat, ignore analog input. + return (g.IO.NavInputsDownDurationPrev[n] >= 0.0f ? 1.0f : 0.0f); + if (t < 0.0f) + return 0.0f; + if (mode == ImGuiInputReadMode_Pressed) // Return 1.0f when just pressed, no repeat, ignore analog input. + return (t == 0.0f) ? 1.0f : 0.0f; + if (mode == ImGuiInputReadMode_Repeat) + return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 0.72f, g.IO.KeyRepeatRate * 0.80f); + if (mode == ImGuiInputReadMode_RepeatSlow) + return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 1.25f, g.IO.KeyRepeatRate * 2.00f); + if (mode == ImGuiInputReadMode_RepeatFast) + return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 0.72f, g.IO.KeyRepeatRate * 0.30f); + return 0.0f; +} + +ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor, float fast_factor) +{ + ImVec2 delta(0.0f, 0.0f); + if (dir_sources & ImGuiNavDirSourceFlags_Keyboard) + delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode) - GetNavInputAmount(ImGuiNavInput_KeyLeft_, mode), GetNavInputAmount(ImGuiNavInput_KeyDown_, mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_, mode)); + if (dir_sources & ImGuiNavDirSourceFlags_PadDPad) + delta += ImVec2(GetNavInputAmount(ImGuiNavInput_DpadRight, mode) - GetNavInputAmount(ImGuiNavInput_DpadLeft, mode), GetNavInputAmount(ImGuiNavInput_DpadDown, mode) - GetNavInputAmount(ImGuiNavInput_DpadUp, mode)); + if (dir_sources & ImGuiNavDirSourceFlags_PadLStick) + delta += ImVec2(GetNavInputAmount(ImGuiNavInput_LStickRight, mode) - GetNavInputAmount(ImGuiNavInput_LStickLeft, mode), GetNavInputAmount(ImGuiNavInput_LStickDown, mode) - GetNavInputAmount(ImGuiNavInput_LStickUp, mode)); + if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakSlow)) + delta *= slow_factor; + if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakFast)) + delta *= fast_factor; + return delta; +} + +static void ImGui::NavUpdate() +{ + ImGuiContext& g = *GImGui; + g.IO.WantSetMousePos = false; +#if 0 + if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG("NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.FrameCount, g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); +#endif + + // Set input source as Gamepad when buttons are pressed before we map Keyboard (some features differs when used with Gamepad vs Keyboard) + bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + if (nav_gamepad_active) + if (g.IO.NavInputs[ImGuiNavInput_Activate] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Input] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Cancel] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Menu] > 0.0f) + g.NavInputSource = ImGuiInputSource_NavGamepad; + + // Update Keyboard->Nav inputs mapping + if (nav_keyboard_active) + { + #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(g.IO.KeyMap[_KEY])) { g.IO.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; } } while (0) + NAV_MAP_KEY(ImGuiKey_Space, ImGuiNavInput_Activate ); + NAV_MAP_KEY(ImGuiKey_Enter, ImGuiNavInput_Input ); + NAV_MAP_KEY(ImGuiKey_Escape, ImGuiNavInput_Cancel ); + NAV_MAP_KEY(ImGuiKey_LeftArrow, ImGuiNavInput_KeyLeft_ ); + NAV_MAP_KEY(ImGuiKey_RightArrow,ImGuiNavInput_KeyRight_); + NAV_MAP_KEY(ImGuiKey_UpArrow, ImGuiNavInput_KeyUp_ ); + NAV_MAP_KEY(ImGuiKey_DownArrow, ImGuiNavInput_KeyDown_ ); + if (g.IO.KeyCtrl) + g.IO.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; + if (g.IO.KeyShift) + g.IO.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; + if (g.IO.KeyAlt && !g.IO.KeyCtrl) // AltGR is Alt+Ctrl, also even on keyboards without AltGR we don't want Alt+Ctrl to open menu. + g.IO.NavInputs[ImGuiNavInput_KeyMenu_] = 1.0f; + #undef NAV_MAP_KEY + } + memcpy(g.IO.NavInputsDownDurationPrev, g.IO.NavInputsDownDuration, sizeof(g.IO.NavInputsDownDuration)); + for (int i = 0; i < IM_ARRAYSIZE(g.IO.NavInputs); i++) + g.IO.NavInputsDownDuration[i] = (g.IO.NavInputs[i] > 0.0f) ? (g.IO.NavInputsDownDuration[i] < 0.0f ? 0.0f : g.IO.NavInputsDownDuration[i] + g.IO.DeltaTime) : -1.0f; + + // Process navigation init request (select first/default focus) + // In very rare cases g.NavWindow may be null (e.g. clearing focus after requesting an init request, which does happen when releasing Alt while clicking on void) + if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove) && g.NavWindow) + { + // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) + //IMGUI_DEBUG_LOG("[Nav] Apply NavInitRequest result: 0x%08X Layer %d in \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); + if (g.NavInitRequestFromMove) + SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, g.NavInitResultRectRel); + else + SetNavID(g.NavInitResultId, g.NavLayer); + g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; + } + g.NavInitRequest = false; + g.NavInitRequestFromMove = false; + g.NavInitResultId = 0; + g.NavJustMovedToId = 0; + + // Process navigation move request + if (g.NavMoveRequest) + NavUpdateMoveResult(); + + // When a forwarded move request failed, we restore the highlight that we disabled during the forward frame + if (g.NavMoveRequestForward == ImGuiNavForward_ForwardActive) + { + IM_ASSERT(g.NavMoveRequest); + if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) + g.NavDisableHighlight = false; + g.NavMoveRequestForward = ImGuiNavForward_None; + } + + // Apply application mouse position movement, after we had a chance to process move request result. + if (g.NavMousePosDirty && g.NavIdIsAlive) + { + // Set mouse position given our knowledge of the navigated item position from last frame + if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (g.IO.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) + { + if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) + { + g.IO.MousePos = g.IO.MousePosPrev = NavCalcPreferredRefPos(); + g.IO.WantSetMousePos = true; + } + } + g.NavMousePosDirty = false; + } + g.NavIdIsAlive = false; + g.NavJustTabbedId = 0; + IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1); + + // Store our return window (for returning from Layer 1 to Layer 0) and clear it as soon as we step back in our own Layer 0 + if (g.NavWindow) + NavSaveLastChildNavWindowIntoParent(g.NavWindow); + if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == 0) + g.NavWindow->NavLastChildNavWindow = NULL; + + // Update CTRL+TAB and Windowing features (hold Square to move/resize/etc.) + NavUpdateWindowing(); + + // Set output flags for user application + g.IO.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs); + g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); + + // Process NavCancel input (to close a popup, get back to parent, clear focus) + if (IsNavInputPressed(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) + { + if (g.ActiveId != 0) + { + if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) + ClearActiveID(); + } + else if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow) && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) + { + // Exit child window + ImGuiWindow* child_window = g.NavWindow; + ImGuiWindow* parent_window = g.NavWindow->ParentWindow; + IM_ASSERT(child_window->ChildId != 0); + FocusWindow(parent_window); + SetNavID(child_window->ChildId, 0); + g.NavIdIsAlive = false; + if (g.NavDisableMouseHover) + g.NavMousePosDirty = true; + } + else if (g.OpenPopupStack.Size > 0) + { + // Close open popup/menu + if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) + ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); + } + else if (g.NavLayer != 0) + { + // Leave the "menu" layer + NavRestoreLayer(ImGuiNavLayer_Main); + } + else + { + // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were + if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) + g.NavWindow->NavLastIds[0] = 0; + g.NavId = 0; + } + } + + // Process manual activation request + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = 0; + if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) + { + bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); + bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); + if (g.ActiveId == 0 && activate_pressed) + g.NavActivateId = g.NavId; + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) + g.NavActivateDownId = g.NavId; + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) + g.NavActivatePressedId = g.NavId; + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) + g.NavInputId = g.NavId; + } + if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) + g.NavDisableHighlight = true; + if (g.NavActivateId != 0) + IM_ASSERT(g.NavActivateDownId == g.NavActivateId); + g.NavMoveRequest = false; + + // Process programmatic activation request + if (g.NavNextActivateId != 0) + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = g.NavNextActivateId; + g.NavNextActivateId = 0; + + // Initiate directional inputs request + if (g.NavMoveRequestForward == ImGuiNavForward_None) + { + g.NavMoveDir = ImGuiDir_None; + g.NavMoveRequestFlags = ImGuiNavMoveFlags_None; + if (g.NavWindow && !g.NavWindowingTarget && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) + { + if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadLeft, ImGuiNavInput_KeyLeft_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Left; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadRight,ImGuiNavInput_KeyRight_,ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Right; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadUp, ImGuiNavInput_KeyUp_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Up; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadDown, ImGuiNavInput_KeyDown_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Down; } + } + g.NavMoveClipDir = g.NavMoveDir; + } + else + { + // Forwarding previous request (which has been modified, e.g. wrap around menus rewrite the requests with a starting rectangle at the other side of the window) + // (Preserve g.NavMoveRequestFlags, g.NavMoveClipDir which were set by the NavMoveRequestForward() function) + IM_ASSERT(g.NavMoveDir != ImGuiDir_None && g.NavMoveClipDir != ImGuiDir_None); + IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_ForwardQueued); + g.NavMoveRequestForward = ImGuiNavForward_ForwardActive; + } + + // Update PageUp/PageDown/Home/End scroll + // FIXME-NAV: Consider enabling those keys even without the master ImGuiConfigFlags_NavEnableKeyboard flag? + float nav_scoring_rect_offset_y = 0.0f; + if (nav_keyboard_active) + nav_scoring_rect_offset_y = NavUpdatePageUpPageDown(); + + // If we initiate a movement request and have no current NavId, we initiate a InitDefautRequest that will be used as a fallback if the direction fails to find a match + if (g.NavMoveDir != ImGuiDir_None) + { + g.NavMoveRequest = true; + g.NavMoveDirLast = g.NavMoveDir; + } + if (g.NavMoveRequest && g.NavId == 0) + { + //IMGUI_DEBUG_LOG("[Nav] NavInitRequest from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); + g.NavInitRequest = g.NavInitRequestFromMove = true; + g.NavInitResultId = 0; + g.NavDisableHighlight = false; + } + NavUpdateAnyRequestFlag(); + + // Scrolling + if (g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget) + { + // *Fallback* manual-scroll with Nav directional keys when window has no navigable item + ImGuiWindow* window = g.NavWindow; + const float scroll_speed = ImFloor(window->CalcFontSize() * 100 * g.IO.DeltaTime + 0.5f); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. + if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) + { + if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) + SetScrollX(window, ImFloor(window->Scroll.x + ((g.NavMoveDir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); + if (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) + SetScrollY(window, ImFloor(window->Scroll.y + ((g.NavMoveDir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); + } + + // *Normal* Manual scroll with NavScrollXXX keys + // Next movement request will clamp the NavId reference rectangle to the visible area, so navigation will resume within those bounds. + ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f/10.0f, 10.0f); + if (scroll_dir.x != 0.0f && window->ScrollbarX) + { + SetScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed)); + g.NavMoveFromClampedRefRect = true; + } + if (scroll_dir.y != 0.0f) + { + SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed)); + g.NavMoveFromClampedRefRect = true; + } + } + + // Reset search results + g.NavMoveResultLocal.Clear(); + g.NavMoveResultLocalVisibleSet.Clear(); + g.NavMoveResultOther.Clear(); + + // When we have manually scrolled (without using navigation) and NavId becomes out of bounds, we project its bounding box to the visible area to restart navigation within visible items + if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == 0) + { + ImGuiWindow* window = g.NavWindow; + ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1,1), window->InnerRect.Max - window->Pos + ImVec2(1,1)); + if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) + { + float pad = window->CalcFontSize() * 0.5f; + window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item + window->NavRectRel[g.NavLayer].ClipWith(window_rect_rel); + g.NavId = 0; + } + g.NavMoveFromClampedRefRect = false; + } + + // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) + ImRect nav_rect_rel = (g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted()) ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0,0,0,0); + g.NavScoringRectScreen = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : GetViewportRect(); + g.NavScoringRectScreen.TranslateY(nav_scoring_rect_offset_y); + g.NavScoringRectScreen.Min.x = ImMin(g.NavScoringRectScreen.Min.x + 1.0f, g.NavScoringRectScreen.Max.x); + g.NavScoringRectScreen.Max.x = g.NavScoringRectScreen.Min.x; + IM_ASSERT(!g.NavScoringRectScreen.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). + //GetForegroundDrawList()->AddRect(g.NavScoringRectScreen.Min, g.NavScoringRectScreen.Max, IM_COL32(255,200,0,255)); // [DEBUG] + g.NavScoringCount = 0; +#if IMGUI_DEBUG_NAV_RECTS + if (g.NavWindow) + { + ImDrawList* draw_list = GetForegroundDrawList(g.NavWindow); + if (1) { for (int layer = 0; layer < 2; layer++) draw_list->AddRect(g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Min, g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Max, IM_COL32(255,200,0,255)); } // [DEBUG] + if (1) { ImU32 col = (!g.NavWindow->Hidden) ? IM_COL32(255,0,255,255) : IM_COL32(255,0,0,255); ImVec2 p = NavCalcPreferredRefPos(); char buf[32]; ImFormatString(buf, 32, "%d", g.NavLayer); draw_list->AddCircleFilled(p, 3.0f, col); draw_list->AddText(NULL, 13.0f, p + ImVec2(8,-4), col, buf); } + } +#endif +} + +// Apply result from previous frame navigation directional move request +static void ImGui::NavUpdateMoveResult() +{ + ImGuiContext& g = *GImGui; + if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) + { + // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) + if (g.NavId != 0) + { + g.NavDisableHighlight = false; + g.NavDisableMouseHover = true; + } + return; + } + + // Select which result to use + ImGuiNavMoveResult* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; + + // PageUp/PageDown behavior first jumps to the bottom/top mostly visible item, _otherwise_ use the result from the previous/next page. + if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) + if (g.NavMoveResultLocalVisibleSet.ID != 0 && g.NavMoveResultLocalVisibleSet.ID != g.NavId) + result = &g.NavMoveResultLocalVisibleSet; + + // Maybe entering a flattened child from the outside? In this case solve the tie using the regular scoring rules. + if (result != &g.NavMoveResultOther && g.NavMoveResultOther.ID != 0 && g.NavMoveResultOther.Window->ParentWindow == g.NavWindow) + if ((g.NavMoveResultOther.DistBox < result->DistBox) || (g.NavMoveResultOther.DistBox == result->DistBox && g.NavMoveResultOther.DistCenter < result->DistCenter)) + result = &g.NavMoveResultOther; + IM_ASSERT(g.NavWindow && result->Window); + + // Scroll to keep newly navigated item fully into view. + if (g.NavLayer == 0) + { + ImVec2 delta_scroll; + if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_ScrollToEdge) + { + float scroll_target = (g.NavMoveDir == ImGuiDir_Up) ? result->Window->ScrollMax.y : 0.0f; + delta_scroll.y = result->Window->Scroll.y - scroll_target; + SetScrollY(result->Window, scroll_target); + } + else + { + ImRect rect_abs = ImRect(result->RectRel.Min + result->Window->Pos, result->RectRel.Max + result->Window->Pos); + delta_scroll = ScrollToBringRectIntoView(result->Window, rect_abs); + } + + // Offset our result position so mouse position can be applied immediately after in NavUpdate() + result->RectRel.TranslateX(-delta_scroll.x); + result->RectRel.TranslateY(-delta_scroll.y); + } + + ClearActiveID(); + g.NavWindow = result->Window; + if (g.NavId != result->ID) + { + // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) + g.NavJustMovedToId = result->ID; + g.NavJustMovedToMultiSelectScopeId = result->SelectScopeId; + } + SetNavIDWithRectRel(result->ID, g.NavLayer, result->RectRel); + g.NavMoveFromClampedRefRect = false; +} + +// Handle PageUp/PageDown/Home/End keys +static float ImGui::NavUpdatePageUpPageDown() +{ + ImGuiContext& g = *GImGui; + if (g.NavMoveDir != ImGuiDir_None || g.NavWindow == NULL) + return 0.0f; + if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != 0) + return 0.0f; + + ImGuiWindow* window = g.NavWindow; + const bool page_up_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); + const bool page_down_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageDown]) && !IsActiveIdUsingKey(ImGuiKey_PageDown); + const bool home_pressed = IsKeyPressed(g.IO.KeyMap[ImGuiKey_Home]) && !IsActiveIdUsingKey(ImGuiKey_Home); + const bool end_pressed = IsKeyPressed(g.IO.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); + if (page_up_held != page_down_held || home_pressed != end_pressed) // If either (not both) are pressed + { + if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll) + { + // Fallback manual-scroll when window has no navigable item + if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) + SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); + else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) + SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); + else if (home_pressed) + SetScrollY(window, 0.0f); + else if (end_pressed) + SetScrollY(window, window->ScrollMax.y); + } + else + { + ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; + const float page_offset_y = ImMax(0.0f, window->InnerRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); + float nav_scoring_rect_offset_y = 0.0f; + if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) + { + nav_scoring_rect_offset_y = -page_offset_y; + g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) + g.NavMoveClipDir = ImGuiDir_Up; + g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; + } + else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) + { + nav_scoring_rect_offset_y = +page_offset_y; + g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) + g.NavMoveClipDir = ImGuiDir_Down; + g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; + } + else if (home_pressed) + { + // FIXME-NAV: handling of Home/End is assuming that the top/bottom most item will be visible with Scroll.y == 0/ScrollMax.y + // Scrolling will be handled via the ImGuiNavMoveFlags_ScrollToEdge flag, we don't scroll immediately to avoid scrolling happening before nav result. + // Preserve current horizontal position if we have any. + nav_rect_rel.Min.y = nav_rect_rel.Max.y = -window->Scroll.y; + if (nav_rect_rel.IsInverted()) + nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; + g.NavMoveDir = ImGuiDir_Down; + g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdge; + } + else if (end_pressed) + { + nav_rect_rel.Min.y = nav_rect_rel.Max.y = window->ScrollMax.y + window->SizeFull.y - window->Scroll.y; + if (nav_rect_rel.IsInverted()) + nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; + g.NavMoveDir = ImGuiDir_Up; + g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdge; + } + return nav_scoring_rect_offset_y; + } + } + return 0.0f; +} + +static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N) +{ + ImGuiContext& g = *GImGui; + for (int i = g.WindowsFocusOrder.Size-1; i >= 0; i--) + if (g.WindowsFocusOrder[i] == window) + return i; + return -1; +} + +static ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // FIXME-OPT O(N) +{ + ImGuiContext& g = *GImGui; + for (int i = i_start; i >= 0 && i < g.WindowsFocusOrder.Size && i != i_stop; i += dir) + if (ImGui::IsWindowNavFocusable(g.WindowsFocusOrder[i])) + return g.WindowsFocusOrder[i]; + return NULL; +} + +static void NavUpdateWindowingHighlightWindow(int focus_change_dir) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavWindowingTarget); + if (g.NavWindowingTarget->Flags & ImGuiWindowFlags_Modal) + return; + + const int i_current = ImGui::FindWindowFocusIndex(g.NavWindowingTarget); + ImGuiWindow* window_target = FindWindowNavFocusable(i_current + focus_change_dir, -INT_MAX, focus_change_dir); + if (!window_target) + window_target = FindWindowNavFocusable((focus_change_dir < 0) ? (g.WindowsFocusOrder.Size - 1) : 0, i_current, focus_change_dir); + if (window_target) // Don't reset windowing target if there's a single window in the list + g.NavWindowingTarget = g.NavWindowingTargetAnim = window_target; + g.NavWindowingToggleLayer = false; +} + +// Windowing management mode +// Keyboard: CTRL+Tab (change focus/move/resize), Alt (toggle menu layer) +// Gamepad: Hold Menu/Square (change focus/move/resize), Tap Menu/Square (toggle menu layer) +static void ImGui::NavUpdateWindowing() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* apply_focus_window = NULL; + bool apply_toggle_layer = false; + + ImGuiWindow* modal_window = GetTopMostPopupModal(); + if (modal_window != NULL) + { + g.NavWindowingTarget = NULL; + return; + } + + // Fade out + if (g.NavWindowingTargetAnim && g.NavWindowingTarget == NULL) + { + g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha - g.IO.DeltaTime * 10.0f, 0.0f); + if (g.DimBgRatio <= 0.0f && g.NavWindowingHighlightAlpha <= 0.0f) + g.NavWindowingTargetAnim = NULL; + } + + // Start CTRL-TAB or Square+L/R window selection + bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); + bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); + if (start_windowing_with_gamepad || start_windowing_with_keyboard) + if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) + { + g.NavWindowingTarget = g.NavWindowingTargetAnim = window; + g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; + g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true; + g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_NavKeyboard : ImGuiInputSource_NavGamepad; + } + + // Gamepad update + g.NavWindowingTimer += g.IO.DeltaTime; + if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavGamepad) + { + // Highlight only appears after a brief time holding the button, so that a fast tap on PadMenu (to toggle NavLayer) doesn't add visual noise + g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); + + // Select window to focus + const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); + if (focus_change_dir != 0) + { + NavUpdateWindowingHighlightWindow(focus_change_dir); + g.NavWindowingHighlightAlpha = 1.0f; + } + + // Single press toggles NavLayer, long press with L/R apply actual focus on release (until then the window was merely rendered top-most) + if (!IsNavInputDown(ImGuiNavInput_Menu)) + { + g.NavWindowingToggleLayer &= (g.NavWindowingHighlightAlpha < 1.0f); // Once button was held long enough we don't consider it a tap-to-toggle-layer press anymore. + if (g.NavWindowingToggleLayer && g.NavWindow) + apply_toggle_layer = true; + else if (!g.NavWindowingToggleLayer) + apply_focus_window = g.NavWindowingTarget; + g.NavWindowingTarget = NULL; + } + } + + // Keyboard: Focus + if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavKeyboard) + { + // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise + g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f + if (IsKeyPressedMap(ImGuiKey_Tab, true)) + NavUpdateWindowingHighlightWindow(g.IO.KeyShift ? +1 : -1); + if (!g.IO.KeyCtrl) + apply_focus_window = g.NavWindowingTarget; + } + + // Keyboard: Press and Release ALT to toggle menu layer + // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of back-end clearing releases all keys on ALT-TAB + if (IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) + g.NavWindowingToggleLayer = true; + if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) + if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev)) + apply_toggle_layer = true; + + // Move window + if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove)) + { + ImVec2 move_delta; + if (g.NavInputSource == ImGuiInputSource_NavKeyboard && !g.IO.KeyShift) + move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); + if (g.NavInputSource == ImGuiInputSource_NavGamepad) + move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); + if (move_delta.x != 0.0f || move_delta.y != 0.0f) + { + const float NAV_MOVE_SPEED = 800.0f; + const float move_speed = ImFloor(NAV_MOVE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); // FIXME: Doesn't code variable framerate very well + SetWindowPos(g.NavWindowingTarget->RootWindow, g.NavWindowingTarget->RootWindow->Pos + move_delta * move_speed, ImGuiCond_Always); + g.NavDisableMouseHover = true; + MarkIniSettingsDirty(g.NavWindowingTarget); + } + } + + // Apply final focus + if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindow)) + { + ClearActiveID(); + g.NavDisableHighlight = false; + g.NavDisableMouseHover = true; + apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window); + ClosePopupsOverWindow(apply_focus_window, false); + FocusWindow(apply_focus_window); + if (apply_focus_window->NavLastIds[0] == 0) + NavInitWindow(apply_focus_window, false); + + // If the window only has a menu layer, select it directly + if (apply_focus_window->DC.NavLayerActiveMask == (1 << ImGuiNavLayer_Menu)) + g.NavLayer = ImGuiNavLayer_Menu; + } + if (apply_focus_window) + g.NavWindowingTarget = NULL; + + // Apply menu/layer toggle + if (apply_toggle_layer && g.NavWindow) + { + // Move to parent menu if necessary + ImGuiWindow* new_nav_window = g.NavWindow; + while (new_nav_window->ParentWindow + && (new_nav_window->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) == 0 + && (new_nav_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 + && (new_nav_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) + new_nav_window = new_nav_window->ParentWindow; + if (new_nav_window != g.NavWindow) + { + ImGuiWindow* old_nav_window = g.NavWindow; + FocusWindow(new_nav_window); + new_nav_window->NavLastChildNavWindow = old_nav_window; + } + g.NavDisableHighlight = false; + g.NavDisableMouseHover = true; + + // When entering a regular menu bar with the Alt key, we always reinitialize the navigation ID. + const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; + NavRestoreLayer(new_nav_layer); + } +} + +// Window has already passed the IsWindowNavFocusable() +static const char* GetFallbackWindowNameForWindowingList(ImGuiWindow* window) +{ + if (window->Flags & ImGuiWindowFlags_Popup) + return "(Popup)"; + if ((window->Flags & ImGuiWindowFlags_MenuBar) && strcmp(window->Name, "##MainMenuBar") == 0) + return "(Main menu bar)"; + return "(Untitled)"; +} + +// Overlay displayed when using CTRL+TAB. Called by EndFrame(). +void ImGui::NavUpdateWindowingOverlay() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavWindowingTarget != NULL); + + if (g.NavWindowingTimer < NAV_WINDOWING_LIST_APPEAR_DELAY) + return; + + if (g.NavWindowingList == NULL) + g.NavWindowingList = FindWindowByName("###NavWindowingList"); + SetNextWindowSizeConstraints(ImVec2(g.IO.DisplaySize.x * 0.20f, g.IO.DisplaySize.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX)); + SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f); + Begin("###NavWindowingList", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings); + for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--) + { + ImGuiWindow* window = g.WindowsFocusOrder[n]; + if (!IsWindowNavFocusable(window)) + continue; + const char* label = window->Name; + if (label == FindRenderedTextEnd(label)) + label = GetFallbackWindowNameForWindowingList(window); + Selectable(label, g.NavWindowingTarget == window); + } + End(); + PopStyleVar(); +} + + +//----------------------------------------------------------------------------- +// [SECTION] DRAG AND DROP +//----------------------------------------------------------------------------- + +void ImGui::ClearDragDrop() +{ + ImGuiContext& g = *GImGui; + g.DragDropActive = false; + g.DragDropPayload.Clear(); + g.DragDropAcceptFlags = ImGuiDragDropFlags_None; + g.DragDropAcceptIdCurr = g.DragDropAcceptIdPrev = 0; + g.DragDropAcceptIdCurrRectSurface = FLT_MAX; + g.DragDropAcceptFrameCount = -1; + + g.DragDropPayloadBufHeap.clear(); + memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); +} + +// Call when current ID is active. +// When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() +bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + bool source_drag_active = false; + ImGuiID source_id = 0; + ImGuiID source_parent_id = 0; + int mouse_button = 0; + if (!(flags & ImGuiDragDropFlags_SourceExtern)) + { + source_id = window->DC.LastItemId; + if (source_id != 0 && g.ActiveId != source_id) // Early out for most common case + return false; + if (g.IO.MouseDown[mouse_button] == false) + return false; + + if (source_id == 0) + { + // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: + // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride. + if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) + { + IM_ASSERT(0); + return false; + } + + // Early out + if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) + return false; + + // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() + // We build a throwaway ID based on current ID stack + relative AABB of items in window. + // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. + // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. + source_id = window->DC.LastItemId = window->GetIDFromRectangle(window->DC.LastItemRect); + bool is_hovered = ItemHoverable(window->DC.LastItemRect, source_id); + if (is_hovered && g.IO.MouseClicked[mouse_button]) + { + SetActiveID(source_id, window); + FocusWindow(window); + } + if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. + g.ActiveIdAllowOverlap = is_hovered; + } + else + { + g.ActiveIdAllowOverlap = false; + } + if (g.ActiveId != source_id) + return false; + source_parent_id = window->IDStack.back(); + source_drag_active = IsMouseDragging(mouse_button); + } + else + { + window = NULL; + source_id = ImHashStr("#SourceExtern"); + source_drag_active = true; + } + + if (source_drag_active) + { + if (!g.DragDropActive) + { + IM_ASSERT(source_id != 0); + ClearDragDrop(); + ImGuiPayload& payload = g.DragDropPayload; + payload.SourceId = source_id; + payload.SourceParentId = source_parent_id; + g.DragDropActive = true; + g.DragDropSourceFlags = flags; + g.DragDropMouseButton = mouse_button; + } + g.DragDropSourceFrameCount = g.FrameCount; + g.DragDropWithinSourceOrTarget = true; + + if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) + { + // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) + // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. + BeginTooltip(); + if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) + { + ImGuiWindow* tooltip_window = g.CurrentWindow; + tooltip_window->SkipItems = true; + tooltip_window->HiddenFramesCanSkipItems = 1; + } + } + + if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) + window->DC.LastItemStatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; + + return true; + } + return false; +} + +void ImGui::EndDragDropSource() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.DragDropActive); + IM_ASSERT(g.DragDropWithinSourceOrTarget && "Not after a BeginDragDropSource()?"); + + if (!(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) + EndTooltip(); + + // Discard the drag if have not called SetDragDropPayload() + if (g.DragDropPayload.DataFrameCount == -1) + ClearDragDrop(); + g.DragDropWithinSourceOrTarget = false; +} + +// Use 'cond' to choose to submit payload on drag start or every frame +bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_size, ImGuiCond cond) +{ + ImGuiContext& g = *GImGui; + ImGuiPayload& payload = g.DragDropPayload; + if (cond == 0) + cond = ImGuiCond_Always; + + IM_ASSERT(type != NULL); + IM_ASSERT(strlen(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long"); + IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0)); + IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once); + IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource() + + if (cond == ImGuiCond_Always || payload.DataFrameCount == -1) + { + // Copy payload + ImStrncpy(payload.DataType, type, IM_ARRAYSIZE(payload.DataType)); + g.DragDropPayloadBufHeap.resize(0); + if (data_size > sizeof(g.DragDropPayloadBufLocal)) + { + // Store in heap + g.DragDropPayloadBufHeap.resize((int)data_size); + payload.Data = g.DragDropPayloadBufHeap.Data; + memcpy(payload.Data, data, data_size); + } + else if (data_size > 0) + { + // Store locally + memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); + payload.Data = g.DragDropPayloadBufLocal; + memcpy(payload.Data, data, data_size); + } + else + { + payload.Data = NULL; + } + payload.DataSize = (int)data_size; + } + payload.DataFrameCount = g.FrameCount; + + return (g.DragDropAcceptFrameCount == g.FrameCount) || (g.DragDropAcceptFrameCount == g.FrameCount - 1); +} + +bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) +{ + ImGuiContext& g = *GImGui; + if (!g.DragDropActive) + return false; + + ImGuiWindow* window = g.CurrentWindow; + if (g.HoveredWindow == NULL || window->RootWindow != g.HoveredWindow->RootWindow) + return false; + IM_ASSERT(id != 0); + if (!IsMouseHoveringRect(bb.Min, bb.Max) || (id == g.DragDropPayload.SourceId)) + return false; + if (window->SkipItems) + return false; + + IM_ASSERT(g.DragDropWithinSourceOrTarget == false); + g.DragDropTargetRect = bb; + g.DragDropTargetId = id; + g.DragDropWithinSourceOrTarget = true; + return true; +} + +// We don't use BeginDragDropTargetCustom() and duplicate its code because: +// 1) we use LastItemRectHoveredRect which handles items that pushes a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. +// 2) and it's faster. as this code may be very frequently called, we want to early out as fast as we can. +// Also note how the HoveredWindow test is positioned differently in both functions (in both functions we optimize for the cheapest early out case) +bool ImGui::BeginDragDropTarget() +{ + ImGuiContext& g = *GImGui; + if (!g.DragDropActive) + return false; + + ImGuiWindow* window = g.CurrentWindow; + if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) + return false; + if (g.HoveredWindow == NULL || window->RootWindow != g.HoveredWindow->RootWindow) + return false; + + const ImRect& display_rect = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? window->DC.LastItemDisplayRect : window->DC.LastItemRect; + ImGuiID id = window->DC.LastItemId; + if (id == 0) + id = window->GetIDFromRectangle(display_rect); + if (g.DragDropPayload.SourceId == id) + return false; + + IM_ASSERT(g.DragDropWithinSourceOrTarget == false); + g.DragDropTargetRect = display_rect; + g.DragDropTargetId = id; + g.DragDropWithinSourceOrTarget = true; + return true; +} + +bool ImGui::IsDragDropPayloadBeingAccepted() +{ + ImGuiContext& g = *GImGui; + return g.DragDropActive && g.DragDropAcceptIdPrev != 0; +} + +const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiPayload& payload = g.DragDropPayload; + IM_ASSERT(g.DragDropActive); // Not called between BeginDragDropTarget() and EndDragDropTarget() ? + IM_ASSERT(payload.DataFrameCount != -1); // Forgot to call EndDragDropTarget() ? + if (type != NULL && !payload.IsDataType(type)) + return NULL; + + // Accept smallest drag target bounding box, this allows us to nest drag targets conveniently without ordering constraints. + // NB: We currently accept NULL id as target. However, overlapping targets requires a unique ID to function! + const bool was_accepted_previously = (g.DragDropAcceptIdPrev == g.DragDropTargetId); + ImRect r = g.DragDropTargetRect; + float r_surface = r.GetWidth() * r.GetHeight(); + if (r_surface < g.DragDropAcceptIdCurrRectSurface) + { + g.DragDropAcceptFlags = flags; + g.DragDropAcceptIdCurr = g.DragDropTargetId; + g.DragDropAcceptIdCurrRectSurface = r_surface; + } + + // Render default drop visuals + payload.Preview = was_accepted_previously; + flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame) + if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) + { + // FIXME-DRAG: Settle on a proper default visuals for drop target. + r.Expand(3.5f); + bool push_clip_rect = !window->ClipRect.Contains(r); + if (push_clip_rect) window->DrawList->PushClipRect(r.Min-ImVec2(1,1), r.Max+ImVec2(1,1)); + window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f); + if (push_clip_rect) window->DrawList->PopClipRect(); + } + + g.DragDropAcceptFrameCount = g.FrameCount; + payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting os window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() + if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery)) + return NULL; + + return &payload; +} + +const ImGuiPayload* ImGui::GetDragDropPayload() +{ + ImGuiContext& g = *GImGui; + return g.DragDropActive ? &g.DragDropPayload : NULL; +} + +// We don't really use/need this now, but added it for the sake of consistency and because we might need it later. +void ImGui::EndDragDropTarget() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.DragDropActive); + IM_ASSERT(g.DragDropWithinSourceOrTarget); + g.DragDropWithinSourceOrTarget = false; +} + + +//----------------------------------------------------------------------------- +// [SECTION] LOGGING/CAPTURING +//----------------------------------------------------------------------------- +// All text output from the interface can be captured into tty/file/clipboard. +// By default, tree nodes are automatically opened during logging. +//----------------------------------------------------------------------------- + +// Pass text data straight to log (without being displayed) +void ImGui::LogText(const char* fmt, ...) +{ + ImGuiContext& g = *GImGui; + if (!g.LogEnabled) + return; + + va_list args; + va_start(args, fmt); + if (g.LogFile) + vfprintf(g.LogFile, fmt, args); + else + g.LogBuffer.appendfv(fmt, args); + va_end(args); +} + +// Internal version that takes a position to decide on newline placement and pad items according to their depth. +// We split text into individual lines to add current tree level padding +void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (!text_end) + text_end = FindRenderedTextEnd(text, text_end); + + const bool log_new_line = ref_pos && (ref_pos->y > g.LogLinePosY + 1); + if (ref_pos) + g.LogLinePosY = ref_pos->y; + if (log_new_line) + g.LogLineFirstItem = true; + + const char* text_remaining = text; + if (g.LogDepthRef > window->DC.TreeDepth) // Re-adjust padding if we have popped out of our starting depth + g.LogDepthRef = window->DC.TreeDepth; + const int tree_depth = (window->DC.TreeDepth - g.LogDepthRef); + for (;;) + { + // Split the string. Each new line (after a '\n') is followed by spacing corresponding to the current depth of our log entry. + // We don't add a trailing \n to allow a subsequent item on the same line to be captured. + const char* line_start = text_remaining; + const char* line_end = ImStreolRange(line_start, text_end); + const bool is_first_line = (line_start == text); + const bool is_last_line = (line_end == text_end); + if (!is_last_line || (line_start != line_end)) + { + const int char_count = (int)(line_end - line_start); + if (log_new_line || !is_first_line) + LogText(IM_NEWLINE "%*s%.*s", tree_depth * 4, "", char_count, line_start); + else if (g.LogLineFirstItem) + LogText("%*s%.*s", tree_depth * 4, "", char_count, line_start); + else + LogText(" %.*s", char_count, line_start); + g.LogLineFirstItem = false; + } + else if (log_new_line) + { + // An empty "" string at a different Y position should output a carriage return. + LogText(IM_NEWLINE); + break; + } + + if (is_last_line) + break; + text_remaining = line_end + 1; + } +} + +// Start logging/capturing text output +void ImGui::LogBegin(ImGuiLogType type, int auto_open_depth) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(g.LogEnabled == false); + IM_ASSERT(g.LogFile == NULL); + IM_ASSERT(g.LogBuffer.empty()); + g.LogEnabled = true; + g.LogType = type; + g.LogDepthRef = window->DC.TreeDepth; + g.LogDepthToExpand = ((auto_open_depth >= 0) ? auto_open_depth : g.LogDepthToExpandDefault); + g.LogLinePosY = FLT_MAX; + g.LogLineFirstItem = true; +} + +void ImGui::LogToTTY(int auto_open_depth) +{ + ImGuiContext& g = *GImGui; + if (g.LogEnabled) + return; + LogBegin(ImGuiLogType_TTY, auto_open_depth); + g.LogFile = stdout; +} + +// Start logging/capturing text output to given file +void ImGui::LogToFile(int auto_open_depth, const char* filename) +{ + ImGuiContext& g = *GImGui; + if (g.LogEnabled) + return; + + // FIXME: We could probably open the file in text mode "at", however note that clipboard/buffer logging will still + // be subject to outputting OS-incompatible carriage return if within strings the user doesn't use IM_NEWLINE. + // By opening the file in binary mode "ab" we have consistent output everywhere. + if (!filename) + filename = g.IO.LogFilename; + if (!filename || !filename[0]) + return; + FILE* f = ImFileOpen(filename, "ab"); + if (f == NULL) + { + IM_ASSERT(0); + return; + } + + LogBegin(ImGuiLogType_File, auto_open_depth); + g.LogFile = f; +} + +// Start logging/capturing text output to clipboard +void ImGui::LogToClipboard(int auto_open_depth) +{ + ImGuiContext& g = *GImGui; + if (g.LogEnabled) + return; + LogBegin(ImGuiLogType_Clipboard, auto_open_depth); +} + +void ImGui::LogToBuffer(int auto_open_depth) +{ + ImGuiContext& g = *GImGui; + if (g.LogEnabled) + return; + LogBegin(ImGuiLogType_Buffer, auto_open_depth); +} + +void ImGui::LogFinish() +{ + ImGuiContext& g = *GImGui; + if (!g.LogEnabled) + return; + + LogText(IM_NEWLINE); + switch (g.LogType) + { + case ImGuiLogType_TTY: + fflush(g.LogFile); + break; + case ImGuiLogType_File: + fclose(g.LogFile); + break; + case ImGuiLogType_Buffer: + break; + case ImGuiLogType_Clipboard: + if (!g.LogBuffer.empty()) + SetClipboardText(g.LogBuffer.begin()); + break; + case ImGuiLogType_None: + IM_ASSERT(0); + break; + } + + g.LogEnabled = false; + g.LogType = ImGuiLogType_None; + g.LogFile = NULL; + g.LogBuffer.clear(); +} + +// Helper to display logging buttons +// FIXME-OBSOLETE: We should probably obsolete this and let the user have their own helper (this is one of the oldest function alive!) +void ImGui::LogButtons() +{ + ImGuiContext& g = *GImGui; + + PushID("LogButtons"); + const bool log_to_tty = Button("Log To TTY"); SameLine(); + const bool log_to_file = Button("Log To File"); SameLine(); + const bool log_to_clipboard = Button("Log To Clipboard"); SameLine(); + PushAllowKeyboardFocus(false); + SetNextItemWidth(80.0f); + SliderInt("Default Depth", &g.LogDepthToExpandDefault, 0, 9, NULL); + PopAllowKeyboardFocus(); + PopID(); + + // Start logging at the end of the function so that the buttons don't appear in the log + if (log_to_tty) + LogToTTY(); + if (log_to_file) + LogToFile(); + if (log_to_clipboard) + LogToClipboard(); +} + +//----------------------------------------------------------------------------- +// [SECTION] SETTINGS +//----------------------------------------------------------------------------- + +void ImGui::MarkIniSettingsDirty() +{ + ImGuiContext& g = *GImGui; + if (g.SettingsDirtyTimer <= 0.0f) + g.SettingsDirtyTimer = g.IO.IniSavingRate; +} + +void ImGui::MarkIniSettingsDirty(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (!(window->Flags & ImGuiWindowFlags_NoSavedSettings)) + if (g.SettingsDirtyTimer <= 0.0f) + g.SettingsDirtyTimer = g.IO.IniSavingRate; +} + +ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) +{ + ImGuiContext& g = *GImGui; + g.SettingsWindows.push_back(ImGuiWindowSettings()); + ImGuiWindowSettings* settings = &g.SettingsWindows.back(); +#if !IMGUI_DEBUG_INI_SETTINGS + // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() + // Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier. + if (const char* p = strstr(name, "###")) + name = p; +#endif + settings->Name = ImStrdup(name); + settings->ID = ImHashStr(name); + return settings; +} + +ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + for (int i = 0; i != g.SettingsWindows.Size; i++) + if (g.SettingsWindows[i].ID == id) + return &g.SettingsWindows[i]; + return NULL; +} + +ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name) +{ + if (ImGuiWindowSettings* settings = FindWindowSettings(ImHashStr(name))) + return settings; + return CreateNewWindowSettings(name); +} + +void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) +{ + size_t file_data_size = 0; + char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); + if (!file_data) + return; + LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); + IM_FREE(file_data); +} + +ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) +{ + ImGuiContext& g = *GImGui; + const ImGuiID type_hash = ImHashStr(type_name); + for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) + if (g.SettingsHandlers[handler_n].TypeHash == type_hash) + return &g.SettingsHandlers[handler_n]; + return NULL; +} + +// Zero-tolerance, no error reporting, cheap .ini parsing +void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.Initialized); + IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); + + // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter). + // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy.. + if (ini_size == 0) + ini_size = strlen(ini_data); + char* buf = (char*)IM_ALLOC(ini_size + 1); + char* buf_end = buf + ini_size; + memcpy(buf, ini_data, ini_size); + buf[ini_size] = 0; + + void* entry_data = NULL; + ImGuiSettingsHandler* entry_handler = NULL; + + char* line_end = NULL; + for (char* line = buf; line < buf_end; line = line_end + 1) + { + // Skip new lines markers, then find end of the line + while (*line == '\n' || *line == '\r') + line++; + line_end = line; + while (line_end < buf_end && *line_end != '\n' && *line_end != '\r') + line_end++; + line_end[0] = 0; + if (line[0] == ';') + continue; + if (line[0] == '[' && line_end > line && line_end[-1] == ']') + { + // Parse "[Type][Name]". Note that 'Name' can itself contains [] characters, which is acceptable with the current format and parsing code. + line_end[-1] = 0; + const char* name_end = line_end - 1; + const char* type_start = line + 1; + char* type_end = (char*)(intptr_t)ImStrchrRange(type_start, name_end, ']'); + const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; + if (!type_end || !name_start) + { + name_start = type_start; // Import legacy entries that have no type + type_start = "Window"; + } + else + { + *type_end = 0; // Overwrite first ']' + name_start++; // Skip second '[' + } + entry_handler = FindSettingsHandler(type_start); + entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; + } + else if (entry_handler != NULL && entry_data != NULL) + { + // Let type handler parse the line + entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); + } + } + IM_FREE(buf); + g.SettingsLoaded = true; +} + +void ImGui::SaveIniSettingsToDisk(const char* ini_filename) +{ + ImGuiContext& g = *GImGui; + g.SettingsDirtyTimer = 0.0f; + if (!ini_filename) + return; + + size_t ini_data_size = 0; + const char* ini_data = SaveIniSettingsToMemory(&ini_data_size); + FILE* f = ImFileOpen(ini_filename, "wt"); + if (!f) + return; + fwrite(ini_data, sizeof(char), ini_data_size, f); + fclose(f); +} + +// Call registered handlers (e.g. SettingsHandlerWindow_WriteAll() + custom handlers) to write their stuff into a text buffer +const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) +{ + ImGuiContext& g = *GImGui; + g.SettingsDirtyTimer = 0.0f; + g.SettingsIniData.Buf.resize(0); + g.SettingsIniData.Buf.push_back(0); + for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) + { + ImGuiSettingsHandler* handler = &g.SettingsHandlers[handler_n]; + handler->WriteAllFn(&g, handler, &g.SettingsIniData); + } + if (out_size) + *out_size = (size_t)g.SettingsIniData.size(); + return g.SettingsIniData.c_str(); +} + +static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) +{ + ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name)); + if (!settings) + settings = ImGui::CreateNewWindowSettings(name); + return (void*)settings; +} + +static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) +{ + ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry; + int x, y; + int i; + if (sscanf(line, "Pos=%i,%i", &x, &y) == 2) settings->Pos = ImVec2ih((short)x, (short)y); + else if (sscanf(line, "Size=%i,%i", &x, &y) == 2) settings->Size = ImVec2ih((short)x, (short)y); + else if (sscanf(line, "Collapsed=%d", &i) == 1) settings->Collapsed = (i != 0); +} + +static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) +{ + // Gather data from windows that were active during this session + // (if a window wasn't opened in this session we preserve its settings) + ImGuiContext& g = *ctx; + for (int i = 0; i != g.Windows.Size; i++) + { + ImGuiWindow* window = g.Windows[i]; + if (window->Flags & ImGuiWindowFlags_NoSavedSettings) + continue; + + ImGuiWindowSettings* settings = (window->SettingsIdx != -1) ? &g.SettingsWindows[window->SettingsIdx] : ImGui::FindWindowSettings(window->ID); + if (!settings) + { + settings = ImGui::CreateNewWindowSettings(window->Name); + window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); + } + IM_ASSERT(settings->ID == window->ID); + settings->Pos = ImVec2ih((short)window->Pos.x, (short)window->Pos.y); + settings->Size = ImVec2ih((short)window->SizeFull.x, (short)window->SizeFull.y); + settings->Collapsed = window->Collapsed; + } + + // Write to text buffer + buf->reserve(buf->size() + g.SettingsWindows.Size * 96); // ballpark reserve + for (int i = 0; i != g.SettingsWindows.Size; i++) + { + const ImGuiWindowSettings* settings = &g.SettingsWindows[i]; + buf->appendf("[%s][%s]\n", handler->TypeName, settings->Name); + buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y); + buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y); + buf->appendf("Collapsed=%d\n", settings->Collapsed); + buf->appendf("\n"); + } +} + + +//----------------------------------------------------------------------------- +// [SECTION] VIEWPORTS, PLATFORM WINDOWS +//----------------------------------------------------------------------------- + +// (this section is filled in the 'docking' branch) + + +//----------------------------------------------------------------------------- +// [SECTION] DOCKING +//----------------------------------------------------------------------------- + +// (this section is filled in the 'docking' branch) + + +//----------------------------------------------------------------------------- +// [SECTION] PLATFORM DEPENDENT HELPERS +//----------------------------------------------------------------------------- + +#if defined(_WIN32) && !defined(_WINDOWS_) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (!defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) || !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef __MINGW32__ +#include +#else +#include +#endif +#elif defined(__APPLE__) +#include +#endif + +#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) + +#ifdef _MSC_VER +#pragma comment(lib, "user32") +#endif + +// Win32 clipboard implementation +static const char* GetClipboardTextFn_DefaultImpl(void*) +{ + static ImVector buf_local; + buf_local.clear(); + if (!::OpenClipboard(NULL)) + return NULL; + HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); + if (wbuf_handle == NULL) + { + ::CloseClipboard(); + return NULL; + } + if (ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle)) + { + int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1; + buf_local.resize(buf_len); + ImTextStrToUtf8(buf_local.Data, buf_len, wbuf_global, NULL); + } + ::GlobalUnlock(wbuf_handle); + ::CloseClipboard(); + return buf_local.Data; +} + +static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +{ + if (!::OpenClipboard(NULL)) + return; + const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1; + HGLOBAL wbuf_handle = ::GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar)); + if (wbuf_handle == NULL) + { + ::CloseClipboard(); + return; + } + ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle); + ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL); + ::GlobalUnlock(wbuf_handle); + ::EmptyClipboard(); + if (::SetClipboardData(CF_UNICODETEXT, wbuf_handle) == NULL) + ::GlobalFree(wbuf_handle); + ::CloseClipboard(); +} + +#elif defined(__APPLE__) && TARGET_OS_OSX && defined(IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS) + +#include // Use old API to avoid need for separate .mm file +static PasteboardRef main_clipboard = 0; + +// OSX clipboard implementation +// If you enable this you will need to add '-framework ApplicationServices' to your linker command-line! +static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +{ + if (!main_clipboard) + PasteboardCreate(kPasteboardClipboard, &main_clipboard); + PasteboardClear(main_clipboard); + CFDataRef cf_data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text)); + if (cf_data) + { + PasteboardPutItemFlavor(main_clipboard, (PasteboardItemID)1, CFSTR("public.utf8-plain-text"), cf_data, 0); + CFRelease(cf_data); + } +} + +static const char* GetClipboardTextFn_DefaultImpl(void*) +{ + if (!main_clipboard) + PasteboardCreate(kPasteboardClipboard, &main_clipboard); + PasteboardSynchronize(main_clipboard); + + ItemCount item_count = 0; + PasteboardGetItemCount(main_clipboard, &item_count); + for (ItemCount i = 0; i < item_count; i++) + { + PasteboardItemID item_id = 0; + PasteboardGetItemIdentifier(main_clipboard, i + 1, &item_id); + CFArrayRef flavor_type_array = 0; + PasteboardCopyItemFlavors(main_clipboard, item_id, &flavor_type_array); + for (CFIndex j = 0, nj = CFArrayGetCount(flavor_type_array); j < nj; j++) + { + CFDataRef cf_data; + if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr) + { + static ImVector clipboard_text; + int length = (int)CFDataGetLength(cf_data); + clipboard_text.resize(length + 1); + CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)clipboard_text.Data); + clipboard_text[length] = 0; + CFRelease(cf_data); + return clipboard_text.Data; + } + } + } + return NULL; +} + +#else + +// Local Dear ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers. +static const char* GetClipboardTextFn_DefaultImpl(void*) +{ + ImGuiContext& g = *GImGui; + return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin(); +} + +static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +{ + ImGuiContext& g = *GImGui; + g.PrivateClipboard.clear(); + const char* text_end = text + strlen(text); + g.PrivateClipboard.resize((int)(text_end - text) + 1); + memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text)); + g.PrivateClipboard[(int)(text_end - text)] = 0; +} + +#endif + +// Win32 API IME support (for Asian languages, etc.) +#if defined(_WIN32) && !defined(__GNUC__) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) + +#include +#ifdef _MSC_VER +#pragma comment(lib, "imm32") +#endif + +static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y) +{ + // Notify OS Input Method Editor of text input position + ImGuiIO& io = ImGui::GetIO(); + if (HWND hwnd = (HWND)io.ImeWindowHandle) + if (HIMC himc = ::ImmGetContext(hwnd)) + { + COMPOSITIONFORM cf; + cf.ptCurrentPos.x = x; + cf.ptCurrentPos.y = y; + cf.dwStyle = CFS_FORCE_POSITION; + ::ImmSetCompositionWindow(himc, &cf); + ::ImmReleaseContext(hwnd, himc); + } +} + +#else + +static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} + +#endif + +//----------------------------------------------------------------------------- +// [SECTION] METRICS/DEBUG WINDOW +//----------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_METRICS_WINDOW +void ImGui::ShowMetricsWindow(bool* p_open) +{ + if (!ImGui::Begin("Dear ImGui Metrics", p_open)) + { + ImGui::End(); + return; + } + + // State + enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Contents, WRT_ContentsRegionRect, WRT_Count }; // Windows Rect Type + const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Contents", "ContentsRegionRect" }; + static bool show_windows_rects = false; + static int show_windows_rect_type = WRT_WorkRect; + static bool show_windows_begin_order = false; + static bool show_drawcmd_clip_rects = true; + + // Basic info + ImGuiContext& g = *GImGui; + ImGuiIO& io = ImGui::GetIO(); + ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + ImGui::Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); + ImGui::Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); + ImGui::Text("%d active allocations", io.MetricsActiveAllocations); + ImGui::Separator(); + + // Helper functions to display common structures: + // - NodeDrawList + // - NodeColumns + // - NodeWindow + // - NodeWindows + // - NodeTabBar + struct Funcs + { + static ImRect GetWindowRect(ImGuiWindow* window, int rect_type) + { + if (rect_type == WRT_OuterRect) { return window->Rect(); } + else if (rect_type == WRT_OuterRectClipped) { return window->OuterRectClipped; } + else if (rect_type == WRT_InnerRect) { return window->InnerRect; } + else if (rect_type == WRT_InnerClipRect) { return window->InnerClipRect; } + else if (rect_type == WRT_WorkRect) { return window->WorkRect; } + else if (rect_type == WRT_Contents) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } + else if (rect_type == WRT_ContentsRegionRect) { return window->ContentsRegionRect; } + IM_ASSERT(0); + return ImRect(); + } + + static void NodeDrawList(ImGuiWindow* window, ImDrawList* draw_list, const char* label) + { + bool node_open = ImGui::TreeNode(draw_list, "%s: '%s' %d vtx, %d indices, %d cmds", label, draw_list->_OwnerName ? draw_list->_OwnerName : "", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, draw_list->CmdBuffer.Size); + if (draw_list == ImGui::GetWindowDrawList()) + { + ImGui::SameLine(); + ImGui::TextColored(ImVec4(1.0f,0.4f,0.4f,1.0f), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered) + if (node_open) ImGui::TreePop(); + return; + } + + ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list + if (window && IsItemHovered()) + fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); + if (!node_open) + return; + + if (window && !window->WasActive) + ImGui::Text("(Note: owning Window is inactive: DrawList is not being rendered!)"); + + int elem_offset = 0; + for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++) + { + if (pcmd->UserCallback == NULL && pcmd->ElemCount == 0) + continue; + if (pcmd->UserCallback) + { + ImGui::BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); + continue; + } + ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; + char buf[300]; + ImFormatString(buf, IM_ARRAYSIZE(buf), "Draw %4d triangles, tex 0x%p, clip_rect (%4.0f,%4.0f)-(%4.0f,%4.0f)", + pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); + bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); + if (show_drawcmd_clip_rects && fg_draw_list && ImGui::IsItemHovered()) + { + ImRect clip_rect = pcmd->ClipRect; + ImRect vtxs_rect; + for (int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++) + vtxs_rect.Add(draw_list->VtxBuffer[idx_buffer ? idx_buffer[i] : i].pos); + clip_rect.Floor(); fg_draw_list->AddRect(clip_rect.Min, clip_rect.Max, IM_COL32(255,0,255,255)); + vtxs_rect.Floor(); fg_draw_list->AddRect(vtxs_rect.Min, vtxs_rect.Max, IM_COL32(255,255,0,255)); + } + if (!pcmd_node_open) + continue; + + // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. + ImGui::Text("ElemCount: %d, ElemCount/3: %d, VtxOffset: +%d, IdxOffset: +%d", pcmd->ElemCount, pcmd->ElemCount/3, pcmd->VtxOffset, pcmd->IdxOffset); + ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. + while (clipper.Step()) + for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart*3; prim < clipper.DisplayEnd; prim++) + { + char *buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); + ImVec2 triangles_pos[3]; + for (int n = 0; n < 3; n++, idx_i++) + { + int vtx_i = idx_buffer ? idx_buffer[idx_i] : idx_i; + ImDrawVert& v = draw_list->VtxBuffer[vtx_i]; + triangles_pos[n] = v.pos; + buf_p += ImFormatString(buf_p, buf_end - buf_p, "%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\n", + (n == 0) ? "elem" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); + } + ImGui::Selectable(buf, false); + if (fg_draw_list && ImGui::IsItemHovered()) + { + ImDrawListFlags backup_flags = fg_draw_list->Flags; + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines at is more readable for very large and thin triangles. + fg_draw_list->AddPolyline(triangles_pos, 3, IM_COL32(255,255,0,255), true, 1.0f); + fg_draw_list->Flags = backup_flags; + } + } + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + static void NodeColumns(const ImGuiColumns* columns) + { + if (!ImGui::TreeNode((void*)(uintptr_t)columns->ID, "Columns Id: 0x%08X, Count: %d, Flags: 0x%04X", columns->ID, columns->Count, columns->Flags)) + return; + ImGui::BulletText("Width: %.1f (MinX: %.1f, MaxX: %.1f)", columns->OffMaxX - columns->OffMinX, columns->OffMinX, columns->OffMaxX); + for (int column_n = 0; column_n < columns->Columns.Size; column_n++) + ImGui::BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", column_n, columns->Columns[column_n].OffsetNorm, GetColumnOffsetFromNorm(columns, columns->Columns[column_n].OffsetNorm)); + ImGui::TreePop(); + } + + static void NodeWindows(ImVector& windows, const char* label) + { + if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size)) + return; + for (int i = 0; i < windows.Size; i++) + Funcs::NodeWindow(windows[i], "Window"); + ImGui::TreePop(); + } + + static void NodeWindow(ImGuiWindow* window, const char* label) + { + if (window == NULL) + { + ImGui::BulletText("%s: NULL", label); + return; + } + if (!ImGui::TreeNode(window, "%s '%s', %d @ 0x%p", label, window->Name, (window->Active || window->WasActive), window)) + return; + ImGuiWindowFlags flags = window->Flags; + NodeDrawList(window, window->DrawList, "DrawList"); + ImGui::BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y); + ImGui::BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, + (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", + (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", + (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); + ImGui::BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f)", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y); + ImGui::BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); + ImGui::BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); + ImGui::BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); + ImGui::BulletText("NavLastChildNavWindow: %s", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); + if (!window->NavRectRel[0].IsInverted()) + ImGui::BulletText("NavRectRel[0]: (%.1f,%.1f)(%.1f,%.1f)", window->NavRectRel[0].Min.x, window->NavRectRel[0].Min.y, window->NavRectRel[0].Max.x, window->NavRectRel[0].Max.y); + else + ImGui::BulletText("NavRectRel[0]: "); + if (window->RootWindow != window) NodeWindow(window->RootWindow, "RootWindow"); + if (window->ParentWindow != NULL) NodeWindow(window->ParentWindow, "ParentWindow"); + if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, "ChildWindows"); + if (window->ColumnsStorage.Size > 0 && ImGui::TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) + { + for (int n = 0; n < window->ColumnsStorage.Size; n++) + NodeColumns(&window->ColumnsStorage[n]); + ImGui::TreePop(); + } + ImGui::BulletText("Storage: %d bytes", window->StateStorage.Data.size_in_bytes()); + ImGui::TreePop(); + } + + static void NodeTabBar(ImGuiTabBar* tab_bar) + { + // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings. + char buf[256]; + char* p = buf; + const char* buf_end = buf + IM_ARRAYSIZE(buf); + ImFormatString(p, buf_end - p, "TabBar (%d tabs)%s", tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? " *Inactive*" : ""); + if (ImGui::TreeNode(tab_bar, "%s", buf)) + { + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + ImGui::PushID(tab); + if (ImGui::SmallButton("<")) { TabBarQueueChangeTabOrder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); + if (ImGui::SmallButton(">")) { TabBarQueueChangeTabOrder(tab_bar, tab, +1); } ImGui::SameLine(); + ImGui::Text("%02d%c Tab 0x%08X", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID); + ImGui::PopID(); + } + ImGui::TreePop(); + } + } + }; + + Funcs::NodeWindows(g.Windows, "Windows"); + if (ImGui::TreeNode("DrawList", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) + { + for (int i = 0; i < g.DrawDataBuilder.Layers[0].Size; i++) + Funcs::NodeDrawList(NULL, g.DrawDataBuilder.Layers[0][i], "DrawList"); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) + { + for (int i = 0; i < g.OpenPopupStack.Size; i++) + { + ImGuiWindow* window = g.OpenPopupStack[i].Window; + ImGui::BulletText("PopupID: %08x, Window: '%s'%s%s", g.OpenPopupStack[i].PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? " ChildWindow" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? " ChildMenu" : ""); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.Data.Size)) + { + for (int n = 0; n < g.TabBars.Data.Size; n++) + Funcs::NodeTabBar(g.TabBars.GetByIndex(n)); + ImGui::TreePop(); + } + +#if 0 + if (ImGui::TreeNode("Docking")) + { + ImGui::TreePop(); + } +#endif + +#if 0 + if (ImGui::TreeNode("Tables", "Tables (%d)", g.Tables.Data.Size)) + { + ImGui::TreePop(); + } +#endif + + if (ImGui::TreeNode("Internal state")) + { + const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); + ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); + ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); + ImGui::Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + ImGui::Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); + ImGui::Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); + ImGui::Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); + ImGui::Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); + ImGui::Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); + ImGui::Text("NavInputSource: %s", input_source_names[g.NavInputSource]); + ImGui::Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); + ImGui::Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); + ImGui::Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); + ImGui::Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); + ImGui::Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Tools")) + { + // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. + if (ImGui::Button("Item Picker..")) + ImGui::DebugStartItemPicker(); + + ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); + ImGui::Checkbox("Show windows rectangles", &show_windows_rects); + ImGui::SameLine(); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); + show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count); + if (show_windows_rects && g.NavWindow) + { + ImGui::BulletText("'%s':", g.NavWindow->Name); + ImGui::Indent(); + for (int rect_n = 0; rect_n < WRT_Count; rect_n++) + { + ImRect r = Funcs::GetWindowRect(g.NavWindow, rect_n); + ImGui::Text("(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), wrt_rects_names[rect_n]); + } + ImGui::Unindent(); + } + ImGui::Checkbox("Show clipping rectangle when hovering ImDrawCmd node", &show_drawcmd_clip_rects); + ImGui::TreePop(); + } + + // Tool: Display windows Rectangles and Begin Order + if (show_windows_rects || show_windows_begin_order) + { + for (int n = 0; n < g.Windows.Size; n++) + { + ImGuiWindow* window = g.Windows[n]; + if (!window->WasActive) + continue; + ImDrawList* draw_list = GetForegroundDrawList(window); + if (show_windows_rects) + { + ImRect r = Funcs::GetWindowRect(window, show_windows_rect_type); + draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 0, 128, 255)); + } + if (show_windows_begin_order && !(window->Flags & ImGuiWindowFlags_ChildWindow)) + { + char buf[32]; + ImFormatString(buf, IM_ARRAYSIZE(buf), "%d", window->BeginOrderWithinContext); + float font_size = ImGui::GetFontSize(); + draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(font_size, font_size), IM_COL32(200, 100, 100, 255)); + draw_list->AddText(window->Pos, IM_COL32(255, 255, 255, 255), buf); + } + } + } + ImGui::End(); +} + +#else + +void ImGui::ShowMetricsWindow(bool*) { } + +#endif + +//----------------------------------------------------------------------------- + +// Include imgui_user.inl at the end of imgui.cpp to access private data/functions that aren't exposed. +// Prefer just including imgui_internal.h from your code rather than using this define. If a declaration is missing from imgui_internal.h add it or request it on the github. +#ifdef IMGUI_INCLUDE_IMGUI_USER_INL +#include "imgui_user.inl" +#endif + +//----------------------------------------------------------------------------- diff --git a/imgui/imgui.h b/imgui/imgui.h new file mode 100644 index 00000000..1675fc6c --- /dev/null +++ b/imgui/imgui.h @@ -0,0 +1,2241 @@ +// dear imgui, v1.74 WIP +// (headers) + +// See imgui.cpp file for documentation. +// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. +// Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. +// Get latest version at https://github.com/ocornut/imgui + +/* + +Index of this file: +// Header mess +// Forward declarations and basic types +// ImGui API (Dear ImGui end-user API) +// Flags & Enumerations +// Memory allocations macros +// ImVector<> +// ImGuiStyle +// ImGuiIO +// Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload) +// Obsolete functions +// Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) +// Draw List API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) +// Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) + +*/ + +#pragma once + +// Configuration file with compile-time options (edit imconfig.h or define IMGUI_USER_CONFIG to your own filename) +#ifdef IMGUI_USER_CONFIG +#include IMGUI_USER_CONFIG +#endif +#if !defined(IMGUI_DISABLE_INCLUDE_IMCONFIG_H) || defined(IMGUI_INCLUDE_IMCONFIG_H) +#include "imconfig.h" +#endif + +//----------------------------------------------------------------------------- +// Header mess +//----------------------------------------------------------------------------- + +#include // FLT_MAX +#include // va_list +#include // ptrdiff_t, NULL +#include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp + +// Version +// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) +#define IMGUI_VERSION "1.74 WIP" +#define IMGUI_VERSION_NUM 17301 +#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) + +// Define attributes of all API symbols declarations (e.g. for DLL under Windows) +// IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default bindings files (imgui_impl_xxx.h) +// Using dear imgui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +#ifndef IMGUI_API +#define IMGUI_API +#endif +#ifndef IMGUI_IMPL_API +#define IMGUI_IMPL_API IMGUI_API +#endif + +// Helper Macros +#ifndef IM_ASSERT +#include +#define IM_ASSERT(_EXPR) assert(_EXPR) // You can override the default assert handler by editing imconfig.h +#endif +#if defined(__clang__) || defined(__GNUC__) +#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to user functions. +#define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) +#else +#define IM_FMTARGS(FMT) +#define IM_FMTLIST(FMT) +#endif +#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR))) // Size of a static C-style array. Don't use on pointers! +#define IM_UNUSED(_VAR) ((void)_VAR) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. +#if (__cplusplus >= 201100) +#define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 +#else +#define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. +#endif + +// Warnings +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +#endif +#elif defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +//----------------------------------------------------------------------------- +// Forward declarations and basic types +//----------------------------------------------------------------------------- + +struct ImDrawChannel; // Temporary storage to output draw commands out of order, used by ImDrawListSplitter and ImDrawList::ChannelsSplit() +struct ImDrawCmd; // A single draw command within a parent ImDrawList (generally maps to 1 GPU draw call, unless it is a callback) +struct ImDrawData; // All draw command lists required to render the frame + pos/size coordinates to use for the projection matrix. +struct ImDrawList; // A single draw command list (generally one per window, conceptually you may see this as a dynamic "mesh" builder) +struct ImDrawListSharedData; // Data shared among multiple draw lists (typically owned by parent ImGui context, but you may create one yourself) +struct ImDrawListSplitter; // Helper to split a draw list into different layers which can be drawn into out of order, then flattened back. +struct ImDrawVert; // A single vertex (pos + uv + col = 20 bytes by default. Override layout with IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT) +struct ImFont; // Runtime data for a single font within a parent ImFontAtlas +struct ImFontAtlas; // Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader +struct ImFontConfig; // Configuration data when adding a font or merging fonts +struct ImFontGlyph; // A single font glyph (code point + coordinates within in ImFontAtlas + offset) +struct ImFontGlyphRangesBuilder; // Helper to build glyph ranges from text/string data +struct ImColor; // Helper functions to create a color that can be converted to either u32 or float4 (*OBSOLETE* please avoid using) +struct ImGuiContext; // Dear ImGui context (opaque structure, unless including imgui_internal.h) +struct ImGuiIO; // Main configuration and I/O between your application and ImGui +struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) +struct ImGuiListClipper; // Helper to manually clip large list of items +struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame, used by IMGUI_ONCE_UPON_A_FRAME macro +struct ImGuiPayload; // User data payload for drag and drop operations +struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) +struct ImGuiStorage; // Helper for key->value storage +struct ImGuiStyle; // Runtime data for styling/colors +struct ImGuiTextBuffer; // Helper to hold and append into a text buffer (~string builder) +struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbb][,ccccc]") + +// Typedefs and Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) +// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. +#ifndef ImTextureID +typedef void* ImTextureID; // User data to identify a texture (this is whatever to you want it to be! read the FAQ about ImTextureID in imgui.cpp) +#endif +typedef unsigned int ImGuiID; // Unique ID used by widgets (typically hashed from a stack of string) +typedef unsigned short ImWchar; // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings. +typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling +typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions +typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type +typedef int ImGuiDir; // -> enum ImGuiDir_ // Enum: A cardinal direction +typedef int ImGuiKey; // -> enum ImGuiKey_ // Enum: A key identifier (ImGui-side enum) +typedef int ImGuiNavInput; // -> enum ImGuiNavInput_ // Enum: An input identifier for navigation +typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor identifier +typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling +typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect(), AddRectFilled() etc. +typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList +typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas +typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags +typedef int ImGuiColorEditFlags; // -> enum ImGuiColorEditFlags_ // Flags: for ColorEdit4(), ColorPicker4() etc. +typedef int ImGuiConfigFlags; // -> enum ImGuiConfigFlags_ // Flags: for io.ConfigFlags +typedef int ImGuiComboFlags; // -> enum ImGuiComboFlags_ // Flags: for BeginCombo() +typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: for BeginDragDropSource(), AcceptDragDropPayload() +typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() +typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. +typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() +typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() +typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() +typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem() +typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: for TreeNode(), TreeNodeEx(), CollapsingHeader() +typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: for Begin(), BeginChild() +typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data); +typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); + +// Scalar data types +typedef signed char ImS8; // 8-bit signed integer == char +typedef unsigned char ImU8; // 8-bit unsigned integer +typedef signed short ImS16; // 16-bit signed integer +typedef unsigned short ImU16; // 16-bit unsigned integer +typedef signed int ImS32; // 32-bit signed integer == int +typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors) +#if defined(_MSC_VER) && !defined(__clang__) +typedef signed __int64 ImS64; // 64-bit signed integer (pre and post C++11 with Visual Studio) +typedef unsigned __int64 ImU64; // 64-bit unsigned integer (pre and post C++11 with Visual Studio) +#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100) +#include +typedef int64_t ImS64; // 64-bit signed integer (pre C++11) +typedef uint64_t ImU64; // 64-bit unsigned integer (pre C++11) +#else +typedef signed long long ImS64; // 64-bit signed integer (post C++11) +typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) +#endif + +// 2D vector (often used to store positions, sizes, etc.) +struct ImVec2 +{ + float x, y; + ImVec2() { x = y = 0.0f; } + ImVec2(float _x, float _y) { x = _x; y = _y; } + float operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + float& operator[] (size_t idx) { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. +#ifdef IM_VEC2_CLASS_EXTRA + IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2. +#endif +}; + +// 4D vector (often used to store floating-point colors) +struct ImVec4 +{ + float x, y, z, w; + ImVec4() { x = y = z = w = 0.0f; } + ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } +#ifdef IM_VEC4_CLASS_EXTRA + IM_VEC4_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4. +#endif +}; + +//----------------------------------------------------------------------------- +// ImGui: Dear ImGui end-user API +// (Inside a namespace so you can add extra functions in your own separate file. Please don't modify imgui.cpp/.h!) +//----------------------------------------------------------------------------- + +namespace ImGui +{ + // Context creation and access + // Each context create its own ImFontAtlas by default. You may instance one yourself and pass it to CreateContext() to share a font atlas between imgui contexts. + // All those functions are not reliant on the current context. + IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); + IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = destroy current context + IMGUI_API ImGuiContext* GetCurrentContext(); + IMGUI_API void SetCurrentContext(ImGuiContext* ctx); + IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); + + // Main + IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) + IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame. + IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). + IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(), you likely don't need to call that yourself directly. If you don't need to render data (skipping rendering) you may call EndFrame() but you'll have wasted CPU already! If you don't need to render, better to not create any imgui windows and not call NewFrame() at all! + IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can get call GetDrawData() to obtain it and run your rendering function. (Obsolete: this used to call io.RenderDrawListsFn(). Nowadays, we allow and prefer calling your render function yourself.) + IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render. + + // Demo, Debug, Information + IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window (previously called ShowTestWindow). demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! + IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. + IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debug window. display Dear ImGui internals: draw commands (with individual draw calls and vertices), window list, basic internal state, etc. + IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) + IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. + IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. + IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls). + IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.23" (essentially the compiled value for IMGUI_VERSION) + + // Styles + IMGUI_API void StyleColorsDark(ImGuiStyle* dst = NULL); // new, recommended style (default) + IMGUI_API void StyleColorsClassic(ImGuiStyle* dst = NULL); // classic imgui style + IMGUI_API void StyleColorsLight(ImGuiStyle* dst = NULL); // best used with borders and a custom, thicker font + + // Windows + // - Begin() = push window to the stack and start appending to it. End() = pop window from the stack. + // - You may append multiple times to the same window during the same frame. + // - Passing 'bool* p_open != NULL' shows a window-closing widget in the upper-right corner of the window, + // which clicking will set the boolean to false when clicked. + // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting + // anything to the window. Always call a matching End() for each Begin() call, regardless of its return value! + // [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. + // where the EndXXX call should only be called if the corresponding BeginXXX function returned true.] + // - Note that the bottom of window stack always contains a window called "Debug". + IMGUI_API bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); + IMGUI_API void End(); + + // Child Windows + // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. + // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400). + // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. + // Always call a matching EndChild() for each BeginChild() call, regardless of its return value [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function returned true.] + IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); + IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); + IMGUI_API void EndChild(); + + // Windows Utilities + // - "current window" = the window we are appending into while inside a Begin()/End() block. "next window" = next window we will Begin() into. + IMGUI_API bool IsWindowAppearing(); + IMGUI_API bool IsWindowCollapsed(); + IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options. + IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags=0); // is current window hovered (and typically: not blocked by a popup/modal)? see flags for options. NB: If you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that! Please read the FAQ! + IMGUI_API ImDrawList* GetWindowDrawList(); // get draw list associated to the current window, to append your own drawing primitives + IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (useful if you want to do your own drawing via the DrawList API) + IMGUI_API ImVec2 GetWindowSize(); // get current window size + IMGUI_API float GetWindowWidth(); // get current window width (shortcut for GetWindowSize().x) + IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) + + // Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). + IMGUI_API void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0,0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc. + IMGUI_API void SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0); // set next window size. set axis to 0.0f to force an auto-fit on this axis. call before Begin() + IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use -1,-1 on either X/Y axis to preserve the current size. Sizes will be rounded down. Use callback to apply non-trivial programmatic constraints. + IMGUI_API void SetNextWindowContentSize(const ImVec2& size); // set next window content size (~ scrollable client area, which enforce the range of scrollbars). Not including window decorations (title bar, menu bar, etc.) nor WindowPadding. set an axis to 0.0f to leave it automatic. call before Begin() + IMGUI_API void SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // set next window collapsed state. call before Begin() + IMGUI_API void SetNextWindowFocus(); // set next window to be focused / top-most. call before Begin() + IMGUI_API void SetNextWindowBgAlpha(float alpha); // set next window background color alpha. helper to easily modify ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground. + IMGUI_API void SetWindowPos(const ImVec2& pos, ImGuiCond cond = 0); // (not recommended) set current window position - call within Begin()/End(). prefer using SetNextWindowPos(), as this may incur tearing and side-effects. + IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0,0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. + IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed(). + IMGUI_API void SetWindowFocus(); // (not recommended) set current window to be focused / top-most. prefer using SetNextWindowFocus(). + IMGUI_API void SetWindowFontScale(float scale); // set font scale. Adjust IO.FontGlobalScale if you want to scale all windows. This is an old API! For correct scaling, prefer to reload font + rebuild ImFontAtlas + call style.ScaleAllSizes(). + IMGUI_API void SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond = 0); // set named window position. + IMGUI_API void SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond = 0); // set named window size. set axis to 0.0f to force an auto-fit on this axis. + IMGUI_API void SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0); // set named window collapsed state + IMGUI_API void SetWindowFocus(const char* name); // set named window to be focused / top-most. use NULL to remove focus. + + // Content region + // - Those functions are bound to be redesigned soon (they are confusing, incomplete and return values in local window coordinates which increases confusion) + IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates + IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() + IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min (roughly (0,0)-Scroll), in window coordinates + IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates + IMGUI_API float GetWindowContentRegionWidth(); // + + // Windows Scrolling + IMGUI_API float GetScrollX(); // get scrolling amount [0..GetScrollMaxX()] + IMGUI_API float GetScrollY(); // get scrolling amount [0..GetScrollMaxY()] + IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.X - WindowSize.X + IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.Y - WindowSize.Y + IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0..GetScrollMaxX()] + IMGUI_API void SetScrollY(float scroll_y); // set scrolling amount [0..GetScrollMaxY()] + IMGUI_API void SetScrollHereX(float center_x_ratio = 0.5f); // adjust scrolling amount to make current cursor position visible. center_x_ratio=0.0: left, 0.5: center, 1.0: right. When using to make a "default/current item" visible, consider using SetItemDefaultFocus() instead. + IMGUI_API void SetScrollHereY(float center_y_ratio = 0.5f); // adjust scrolling amount to make current cursor position visible. center_y_ratio=0.0: top, 0.5: center, 1.0: bottom. When using to make a "default/current item" visible, consider using SetItemDefaultFocus() instead. + IMGUI_API void SetScrollFromPosX(float local_x, float center_x_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position. + IMGUI_API void SetScrollFromPosY(float local_y, float center_y_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position. + + // Parameters stacks (shared) + IMGUI_API void PushFont(ImFont* font); // use NULL as a shortcut to push default font + IMGUI_API void PopFont(); + IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); + IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4& col); + IMGUI_API void PopStyleColor(int count = 1); + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); + IMGUI_API void PopStyleVar(int count = 1); + IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. + IMGUI_API ImFont* GetFont(); // get current font + IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied + IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API + IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier + IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied + IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied + + // Parameters stacks (current window) + IMGUI_API void PushItemWidth(float item_width); // set width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side). 0.0f = default to ~2/3 of windows width, + IMGUI_API void PopItemWidth(); + IMGUI_API void SetNextItemWidth(float item_width); // set width of the _next_ common large "item+label" widget. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side) + IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position. NOT necessarily the width of last item unlike most 'Item' functions. + IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // word-wrapping for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space + IMGUI_API void PopTextWrapPos(); + IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets + IMGUI_API void PopAllowKeyboardFocus(); + IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. + IMGUI_API void PopButtonRepeat(); + + // Cursor / Layout + // - By "cursor" we mean the current output position. + // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. + IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. + IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f); // call between widgets or groups to layout them horizontally. X position given in window coordinates. + IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in an horizontal-layout context. + IMGUI_API void Spacing(); // add vertical spacing. + IMGUI_API void Dummy(const ImVec2& size); // add a dummy item of given size. unlike InvisibleButton(), Dummy() won't take the mouse click or be navigable into. + IMGUI_API void Indent(float indent_w = 0.0f); // move content position toward the right, by style.IndentSpacing or indent_w if != 0 + IMGUI_API void Unindent(float indent_w = 0.0f); // move content position back to the left, by style.IndentSpacing or indent_w if != 0 + IMGUI_API void BeginGroup(); // lock horizontal starting position + IMGUI_API void EndGroup(); // unlock horizontal starting position + capture the whole group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) + IMGUI_API ImVec2 GetCursorPos(); // cursor position in window coordinates (relative to window position) + IMGUI_API float GetCursorPosX(); // (some functions are using window-relative coordinates, such as: GetCursorPos, GetCursorStartPos, GetContentRegionMax, GetWindowContentRegion* etc. + IMGUI_API float GetCursorPosY(); // other functions such as GetCursorScreenPos or everything in ImDrawList:: + IMGUI_API void SetCursorPos(const ImVec2& local_pos); // are using the main, absolute coordinate system. + IMGUI_API void SetCursorPosX(float local_x); // GetWindowPos() + GetCursorPos() == GetCursorScreenPos() etc.) + IMGUI_API void SetCursorPosY(float local_y); // + IMGUI_API ImVec2 GetCursorStartPos(); // initial cursor position in window coordinates + IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute screen coordinates [0..io.DisplaySize] (useful to work with ImDrawList API) + IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute screen coordinates [0..io.DisplaySize] + IMGUI_API void AlignTextToFramePadding(); // vertically align upcoming text baseline to FramePadding.y so that it will align properly to regularly framed items (call if you have text on a line before a framed item) + IMGUI_API float GetTextLineHeight(); // ~ FontSize + IMGUI_API float GetTextLineHeightWithSpacing(); // ~ FontSize + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of text) + IMGUI_API float GetFrameHeight(); // ~ FontSize + style.FramePadding.y * 2 + IMGUI_API float GetFrameHeightWithSpacing(); // ~ FontSize + style.FramePadding.y * 2 + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of framed widgets) + + // ID stack/scopes + // - Read the FAQ for more details about how ID are handled in dear imgui. If you are creating widgets in a loop you most + // likely want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them. + // - The resulting ID are hashes of the entire stack. + // - You can also use the "Label##foobar" syntax within widget label to distinguish them from each others. + // - In this header file we use the "label"/"name" terminology to denote a string that will be displayed and used as an ID, + // whereas "str_id" denote a string that is only used as an ID and not normally displayed. + IMGUI_API void PushID(const char* str_id); // push string into the ID stack (will hash string). + IMGUI_API void PushID(const char* str_id_begin, const char* str_id_end); // push string into the ID stack (will hash string). + IMGUI_API void PushID(const void* ptr_id); // push pointer into the ID stack (will hash pointer). + IMGUI_API void PushID(int int_id); // push integer into the ID stack (will hash integer). + IMGUI_API void PopID(); // pop from the ID stack. + IMGUI_API ImGuiID GetID(const char* str_id); // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself + IMGUI_API ImGuiID GetID(const char* str_id_begin, const char* str_id_end); + IMGUI_API ImGuiID GetID(const void* ptr_id); + + // Widgets: Text + IMGUI_API void TextUnformatted(const char* text, const char* text_end = NULL); // raw text without formatting. Roughly equivalent to Text("%s", text) but: A) doesn't require null terminated string if 'text_end' is specified, B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. + IMGUI_API void Text(const char* fmt, ...) IM_FMTARGS(1); // simple formatted text + IMGUI_API void TextV(const char* fmt, va_list args) IM_FMTLIST(1); + IMGUI_API void TextColored(const ImVec4& col, const char* fmt, ...) IM_FMTARGS(2); // shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor(); + IMGUI_API void TextColoredV(const ImVec4& col, const char* fmt, va_list args) IM_FMTLIST(2); + IMGUI_API void TextDisabled(const char* fmt, ...) IM_FMTARGS(1); // shortcut for PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); Text(fmt, ...); PopStyleColor(); + IMGUI_API void TextDisabledV(const char* fmt, va_list args) IM_FMTLIST(1); + IMGUI_API void TextWrapped(const char* fmt, ...) IM_FMTARGS(1); // shortcut for PushTextWrapPos(0.0f); Text(fmt, ...); PopTextWrapPos();. Note that this won't work on an auto-resizing window if there's no other widgets to extend the window width, yoy may need to set a size using SetNextWindowSize(). + IMGUI_API void TextWrappedV(const char* fmt, va_list args) IM_FMTLIST(1); + IMGUI_API void LabelText(const char* label, const char* fmt, ...) IM_FMTARGS(2); // display text+label aligned the same way as value+label widgets + IMGUI_API void LabelTextV(const char* label, const char* fmt, va_list args) IM_FMTLIST(2); + IMGUI_API void BulletText(const char* fmt, ...) IM_FMTARGS(1); // shortcut for Bullet()+Text() + IMGUI_API void BulletTextV(const char* fmt, va_list args) IM_FMTLIST(1); + + // Widgets: Main + // - Most widgets return true when the value has been changed or when pressed/selected + IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0,0)); // button + IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text + IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size); // button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) + IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape + IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); + IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding + IMGUI_API bool Checkbox(const char* label, bool* v); + IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); + IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } + IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer + IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1,0), const char* overlay = NULL); + IMGUI_API void Bullet(); // draw a small circle and keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses + + // Widgets: Combo Box + // - The new BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. + // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. + IMGUI_API bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0); + IMGUI_API void EndCombo(); // only call EndCombo() if BeginCombo() returns true! + IMGUI_API bool Combo(const char* label, int* current_item, const char* const items[], int items_count, int popup_max_height_in_items = -1); + IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_max_height_in_items = -1); // Separate items with \0 within a string, end item-list with \0\0. e.g. "One\0Two\0Three\0" + IMGUI_API bool Combo(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_max_height_in_items = -1); + + // Widgets: Drags + // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped and can go off-bounds. + // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every functions, note that a 'float v[X]' function argument is the same as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x + // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. + // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). + // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits. + // - Use v_min > v_max to lock edits. + IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); // If v_min >= v_max we have no bound + IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", const char* format_max = NULL, float power = 1.0f); + IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); // If v_min >= v_max we have no bound + IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); + IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); + IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); + IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL); + IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, float power = 1.0f); + IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, float power = 1.0f); + + // Widgets: Sliders + // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. + // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. + IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. Use power!=1.0 for power curve sliders + IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool SliderAngle(const char* label, float* v_rad, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f, const char* format = "%.0f deg"); + IMGUI_API bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = "%d"); + IMGUI_API bool SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format = "%d"); + IMGUI_API bool SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format = "%d"); + IMGUI_API bool SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format = "%d"); + IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); + IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); + IMGUI_API bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); + IMGUI_API bool VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format = "%d"); + IMGUI_API bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); + + // Widgets: Input with Keyboard + // - If you want to use InputText() with a dynamic string type such as std::string or your own, see misc/cpp/imgui_stdlib.h + // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. + IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0,0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, const char* format = "%.3f", ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputFloat2(const char* label, float v[2], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputFloat3(const char* label, float v[3], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputFloat4(const char* label, float v[4], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100, ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputDouble(const char* label, double* v, double step = 0.0, double step_fast = 0.0, const char* format = "%.6f", ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0); + + // Widgets: Color Editor/Picker (tip: the ColorEdit* functions have a little colored preview square that can be left-clicked to open a picker, and right-clicked to open an option menu.) + // - Note that in C++ a 'float v[X]' function argument is the _same_ as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. + // - You can pass the address of a first float element out of a contiguous structure, e.g. &myvector.x + IMGUI_API bool ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); + IMGUI_API bool ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0); + IMGUI_API bool ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); + IMGUI_API bool ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL); + IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0)); // display a colored square/button, hover for details, return true when pressed. + IMGUI_API void SetColorEditOptions(ImGuiColorEditFlags flags); // initialize current options (generally on application startup) if you want to select a default format, picker type, etc. User will be able to change many settings, unless you pass the _NoOptions flag to your calls. + + // Widgets: Trees + // - TreeNode functions return true when the node is open, in which case you need to also call TreePop() when you are finished displaying the tree node contents. + IMGUI_API bool TreeNode(const char* label); + IMGUI_API bool TreeNode(const char* str_id, const char* fmt, ...) IM_FMTARGS(2); // helper variation to easily decorelate the id from the displayed string. Read the FAQ about why and how to use ID. to align arbitrary text at the same level as a TreeNode() you can use Bullet(). + IMGUI_API bool TreeNode(const void* ptr_id, const char* fmt, ...) IM_FMTARGS(2); // " + IMGUI_API bool TreeNodeV(const char* str_id, const char* fmt, va_list args) IM_FMTLIST(2); + IMGUI_API bool TreeNodeV(const void* ptr_id, const char* fmt, va_list args) IM_FMTLIST(2); + IMGUI_API bool TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags = 0); + IMGUI_API bool TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3); + IMGUI_API bool TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3); + IMGUI_API bool TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); + IMGUI_API bool TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); + IMGUI_API void TreePush(const char* str_id); // ~ Indent()+PushId(). Already called by TreeNode() when returning true, but you can call TreePush/TreePop yourself if desired. + IMGUI_API void TreePush(const void* ptr_id = NULL); // " + IMGUI_API void TreePop(); // ~ Unindent()+PopId() + IMGUI_API float GetTreeNodeToLabelSpacing(); // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode + IMGUI_API bool CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0); // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop(). + IMGUI_API bool CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags = 0); // when 'p_open' isn't NULL, display an additional small close button on upper right of the header + IMGUI_API void SetNextItemOpen(bool is_open, ImGuiCond cond = 0); // set next TreeNode/CollapsingHeader open state. + + // Widgets: Selectables + // - A selectable highlights when hovered, and can display another color when selected. + // - Neighbors selectable extend their highlight bounds in order to leave no gap between them. + IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool selected" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height + IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. + + // Widgets: List Boxes + // - FIXME: To be consistent with all the newer API, ListBoxHeader/ListBoxFooter should in reality be called BeginListBox/EndListBox. Will rename them. + IMGUI_API bool ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1); + IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); + IMGUI_API bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0,0)); // use if you want to reimplement ListBox() will custom data or interactions. if the function return true, you can output elements then call ListBoxFooter() afterwards. + IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // " + IMGUI_API void ListBoxFooter(); // terminate the scrolling region. only call ListBoxFooter() if ListBoxHeader() returned true! + + // Widgets: Data Plotting + IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); + IMGUI_API void PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); + IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); + IMGUI_API void PlotHistogram(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); + + // Widgets: Value() Helpers. + // - Those are merely shortcut to calling Text() with a format string. Output single value in "name: value" format (tip: freely declare more in your code to handle your types. you can add functions to the ImGui namespace) + IMGUI_API void Value(const char* prefix, bool b); + IMGUI_API void Value(const char* prefix, int v); + IMGUI_API void Value(const char* prefix, unsigned int v); + IMGUI_API void Value(const char* prefix, float v, const char* float_format = NULL); + + // Widgets: Menus + IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. + IMGUI_API void EndMainMenuBar(); // only call EndMainMenuBar() if BeginMainMenuBar() returns true! + IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window). + IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true! + IMGUI_API bool BeginMenu(const char* label, bool enabled = true); // create a sub-menu entry. only call EndMenu() if this returns true! + IMGUI_API void EndMenu(); // only call EndMenu() if BeginMenu() returns true! + IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true); // return true when activated. shortcuts are displayed for convenience but not processed by ImGui at the moment + IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL + + // Tooltips + IMGUI_API void BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). + IMGUI_API void EndTooltip(); + IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip(). + IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); + + // Popups, Modals + // The properties of popups windows are: + // - They block normal mouse hovering detection outside them. (*) + // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. + // - Their visibility state (~bool) is held internally by imgui instead of being held by the programmer as we are used to with regular Begin() calls. + // User can manipulate the visibility state by calling OpenPopup(). + // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. + // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. + IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). popups are closed when user click outside, or if CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. By default, Selectable()/MenuItem() are calling CloseCurrentPopup(). Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). + IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. only call EndPopup() if BeginPopup() returns true! + IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! + IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, int mouse_button = 1, bool also_over_items = true); // helper to open and begin popup when clicked on current window. + IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked in void (where there are no imgui windows). + IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // modal dialog (regular window with title bar, block interactions behind the modal window, can't close the modal window by clicking outside) + IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! + IMGUI_API bool OpenPopupOnItemClick(const char* str_id = NULL, int mouse_button = 1); // helper to open popup when clicked on last item (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors). return true when just opened. + IMGUI_API bool IsPopupOpen(const char* str_id); // return true if the popup is open at the current begin-ed level of the popup stack. + IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup. + + // Columns + // - You can also use SameLine(pos_x) to mimic simplified columns. + // - The columns API is work-in-progress and rather lacking (columns are arguably the worst part of dear imgui at the moment!) + IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); + IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished + IMGUI_API int GetColumnIndex(); // get current column index + IMGUI_API float GetColumnWidth(int column_index = -1); // get column width (in pixels). pass -1 to use current column + IMGUI_API void SetColumnWidth(int column_index, float width); // set column width (in pixels). pass -1 to use current column + IMGUI_API float GetColumnOffset(int column_index = -1); // get position of column line (in pixels, from the left side of the contents region). pass -1 to use current column, otherwise 0..GetColumnsCount() inclusive. column 0 is typically 0.0f + IMGUI_API void SetColumnOffset(int column_index, float offset_x); // set position of column line (in pixels, from the left side of the contents region). pass -1 to use current column + IMGUI_API int GetColumnsCount(); + + // Tab Bars, Tabs + // [BETA API] API may evolve! + IMGUI_API bool BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0); // create and append into a TabBar + IMGUI_API void EndTabBar(); // only call EndTabBar() if BeginTabBar() returns true! + IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0);// create a Tab. Returns true if the Tab is selected. + IMGUI_API void EndTabItem(); // only call EndTabItem() if BeginTabItem() returns true! + IMGUI_API void SetTabItemClosed(const char* tab_or_docked_window_label); // notify TabBar or Docking system of a closed tab/window ahead (useful to reduce visual flicker on reorderable tab bars). For tab-bar: call after BeginTabBar() and before Tab submissions. Otherwise call with a window name. + + // Logging/Capture + // - All text output from the interface can be captured into tty/file/clipboard. By default, tree nodes are automatically opened during logging. + IMGUI_API void LogToTTY(int auto_open_depth = -1); // start logging to tty (stdout) + IMGUI_API void LogToFile(int auto_open_depth = -1, const char* filename = NULL); // start logging to file + IMGUI_API void LogToClipboard(int auto_open_depth = -1); // start logging to OS clipboard + IMGUI_API void LogFinish(); // stop logging (close file, etc.) + IMGUI_API void LogButtons(); // helper to display buttons for logging to tty/file/clipboard + IMGUI_API void LogText(const char* fmt, ...) IM_FMTARGS(1); // pass text data straight to log (without being displayed) + + // Drag and Drop + // [BETA API] API may evolve! + IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource() + IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. + IMGUI_API void EndDragDropSource(); // only call EndDragDropSource() if BeginDragDropSource() returns true! + IMGUI_API bool BeginDragDropTarget(); // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget() + IMGUI_API const ImGuiPayload* AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags = 0); // accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released. + IMGUI_API void EndDragDropTarget(); // only call EndDragDropTarget() if BeginDragDropTarget() returns true! + IMGUI_API const ImGuiPayload* GetDragDropPayload(); // peek directly into the current payload from anywhere. may return NULL. use ImGuiPayload::IsDataType() to test for the payload type. + + // Clipping + IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect); + IMGUI_API void PopClipRect(); + + // Focus, Activation + // - Prefer using "SetItemDefaultFocus()" over "if (IsWindowAppearing()) SetScrollHereY()" when applicable to signify "this is the default item" + IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of a window. + IMGUI_API void SetKeyboardFocusHere(int offset = 0); // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget. + + // Item/Widgets Utilities + // - Most of the functions are referring to the last/previous item we submitted. + // - See Demo Window under "Widgets->Querying Status" for an interactive visualization of most of those functions. + IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags = 0); // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options. + IMGUI_API bool IsItemActive(); // is the last item active? (e.g. button being held, text field being edited. This will continuously return true while holding mouse button on an item. Items that don't interact will always return false) + IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation? + IMGUI_API bool IsItemClicked(int mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered() + IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling) + IMGUI_API bool IsItemEdited(); // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the "bool" return value of many widgets. + IMGUI_API bool IsItemActivated(); // was the last item just made active (item was previously inactive). + IMGUI_API bool IsItemDeactivated(); // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that requires continuous editing. + IMGUI_API bool IsItemDeactivatedAfterEdit(); // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that requires continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item). + IMGUI_API bool IsAnyItemHovered(); // is any item hovered? + IMGUI_API bool IsAnyItemActive(); // is any item active? + IMGUI_API bool IsAnyItemFocused(); // is any item focused? + IMGUI_API ImVec2 GetItemRectMin(); // get upper-left bounding rectangle of the last item (screen space) + IMGUI_API ImVec2 GetItemRectMax(); // get lower-right bounding rectangle of the last item (screen space) + IMGUI_API ImVec2 GetItemRectSize(); // get size of last item + IMGUI_API void SetItemAllowOverlap(); // allow last item to be overlapped by a subsequent item. sometimes useful with invisible buttons, selectables, etc. to catch unused area. + + // Miscellaneous Utilities + IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped. + IMGUI_API bool IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max); // test if rectangle (in screen space) is visible / not clipped. to perform coarse clipping on user's side. + IMGUI_API double GetTime(); // get global imgui time. incremented by io.DeltaTime every frame. + IMGUI_API int GetFrameCount(); // get global imgui frame count. incremented by 1 every frame. + IMGUI_API ImDrawList* GetBackgroundDrawList(); // this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + IMGUI_API ImDrawList* GetForegroundDrawList(); // this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. + IMGUI_API ImDrawListSharedData* GetDrawListSharedData(); // you may use this when creating your own ImDrawList instances. + IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). + IMGUI_API void SetStateStorage(ImGuiStorage* storage); // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it) + IMGUI_API ImGuiStorage* GetStateStorage(); + IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); + IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // calculate coarse clipping for large list of evenly sized items. Prefer using the ImGuiListClipper higher-level helper if you can. + IMGUI_API bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags flags = 0); // helper to create a child window / scrolling region that looks like a normal widget frame + IMGUI_API void EndChildFrame(); // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window) + + // Color Utilities + IMGUI_API ImVec4 ColorConvertU32ToFloat4(ImU32 in); + IMGUI_API ImU32 ColorConvertFloat4ToU32(const ImVec4& in); + IMGUI_API void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v); + IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); + + // Inputs Utilities + IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] + IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your backend/engine stored them into io.KeysDown[]! + IMGUI_API bool IsKeyPressed(int user_key_index, bool repeat = true); // was key pressed (went from !Down to Down). if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate + IMGUI_API bool IsKeyReleased(int user_key_index); // was key released (went from Down to !Down).. + IMGUI_API int GetKeyPressedAmount(int key_index, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate + IMGUI_API bool IsMouseDown(int button); // is mouse button held (0=left, 1=right, 2=middle) + IMGUI_API bool IsAnyMouseDown(); // is any mouse button held + IMGUI_API bool IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down) (0=left, 1=right, 2=middle) + IMGUI_API bool IsMouseDoubleClicked(int button); // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime. + IMGUI_API bool IsMouseReleased(int button); // did mouse button released (went from Down to !Down) + IMGUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold + IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true); // is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. + IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse + IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls + IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve backup of mouse position at the time of opening popup we have BeginPopup() into + IMGUI_API ImVec2 GetMouseDragDelta(int button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once. If lock_threshold < -1.0f uses io.MouseDraggingThreshold. + IMGUI_API void ResetMouseDragDelta(int button = 0); // + IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired cursor type, reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you + IMGUI_API void SetMouseCursor(ImGuiMouseCursor type); // set desired cursor type + IMGUI_API void CaptureKeyboardFromApp(bool want_capture_keyboard_value = true); // attention: misleading name! manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application to handle). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard_value"; after the next NewFrame() call. + IMGUI_API void CaptureMouseFromApp(bool want_capture_mouse_value = true); // attention: misleading name! manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application to handle). This is equivalent to setting "io.WantCaptureMouse = want_capture_mouse_value;" after the next NewFrame() call. + + // Clipboard Utilities (also see the LogToClipboard() function to capture or output text data to the clipboard) + IMGUI_API const char* GetClipboardText(); + IMGUI_API void SetClipboardText(const char* text); + + // Settings/.Ini Utilities + // - The disk functions are automatically called if io.IniFilename != NULL (default is "imgui.ini"). + // - Set io.IniFilename to NULL to load/save manually. Read io.WantSaveIniSettings description about handling .ini saving manually. + IMGUI_API void LoadIniSettingsFromDisk(const char* ini_filename); // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename). + IMGUI_API void LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size=0); // call after CreateContext() and before the first call to NewFrame() to provide .ini data from your own data source. + IMGUI_API void SaveIniSettingsToDisk(const char* ini_filename); // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext). + IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. + + // Memory Allocators + // - All those functions are not reliant on the current context. + // - If you reload the contents of imgui.cpp at runtime, you may need to call SetCurrentContext() + SetAllocatorFunctions() again because we use global storage for those. + IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL); + IMGUI_API void* MemAlloc(size_t size); + IMGUI_API void MemFree(void* ptr); + +} // namespace ImGui + +//----------------------------------------------------------------------------- +// Flags & Enumerations +//----------------------------------------------------------------------------- + +// Flags for ImGui::Begin() +enum ImGuiWindowFlags_ +{ + ImGuiWindowFlags_None = 0, + ImGuiWindowFlags_NoTitleBar = 1 << 0, // Disable title-bar + ImGuiWindowFlags_NoResize = 1 << 1, // Disable user resizing with the lower-right grip + ImGuiWindowFlags_NoMove = 1 << 2, // Disable user moving the window + ImGuiWindowFlags_NoScrollbar = 1 << 3, // Disable scrollbars (window can still scroll with mouse or programmatically) + ImGuiWindowFlags_NoScrollWithMouse = 1 << 4, // Disable user vertically scrolling with mouse wheel. On child window, mouse wheel will be forwarded to the parent unless NoScrollbar is also set. + ImGuiWindowFlags_NoCollapse = 1 << 5, // Disable user collapsing window by double-clicking on it + ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, // Resize every window to its content every frame + ImGuiWindowFlags_NoBackground = 1 << 7, // Disable drawing background color (WindowBg, etc.) and outside border. Similar as using SetNextWindowBgAlpha(0.0f). + ImGuiWindowFlags_NoSavedSettings = 1 << 8, // Never load/save settings in .ini file + ImGuiWindowFlags_NoMouseInputs = 1 << 9, // Disable catching mouse, hovering test with pass through. + ImGuiWindowFlags_MenuBar = 1 << 10, // Has a menu-bar + ImGuiWindowFlags_HorizontalScrollbar = 1 << 11, // Allow horizontal scrollbar to appear (off by default). You may use SetNextWindowContentSize(ImVec2(width,0.0f)); prior to calling Begin() to specify width. Read code in imgui_demo in the "Horizontal Scrolling" section. + ImGuiWindowFlags_NoFocusOnAppearing = 1 << 12, // Disable taking focus when transitioning from hidden to visible state + ImGuiWindowFlags_NoBringToFrontOnFocus = 1 << 13, // Disable bringing window to front when taking focus (e.g. clicking on it or programmatically giving it focus) + ImGuiWindowFlags_AlwaysVerticalScrollbar= 1 << 14, // Always show vertical scrollbar (even if ContentSize.y < Size.y) + ImGuiWindowFlags_AlwaysHorizontalScrollbar=1<< 15, // Always show horizontal scrollbar (even if ContentSize.x < Size.x) + ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 16, // Ensure child windows without border uses style.WindowPadding (ignored by default for non-bordered child windows, because more convenient) + ImGuiWindowFlags_NoNavInputs = 1 << 18, // No gamepad/keyboard navigation within the window + ImGuiWindowFlags_NoNavFocus = 1 << 19, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) + ImGuiWindowFlags_UnsavedDocument = 1 << 20, // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. When used in a tab/docking context, tab is selected on closure and closure is deferred by one frame to allow code to cancel the closure (with a confirmation popup, etc.) without flicker. + ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, + ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse, + ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, + + // [Internal] + ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] Allow gamepad/keyboard navigation to cross over parent border to this child (only use on child that have no scrolling!) + ImGuiWindowFlags_ChildWindow = 1 << 24, // Don't use! For internal use by BeginChild() + ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() + ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() + ImGuiWindowFlags_Modal = 1 << 27, // Don't use! For internal use by BeginPopupModal() + ImGuiWindowFlags_ChildMenu = 1 << 28 // Don't use! For internal use by BeginMenu() + + // [Obsolete] + //ImGuiWindowFlags_ShowBorders = 1 << 7, // --> Set style.FrameBorderSize=1.0f / style.WindowBorderSize=1.0f to enable borders around windows and items + //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) +}; + +// Flags for ImGui::InputText() +enum ImGuiInputTextFlags_ +{ + ImGuiInputTextFlags_None = 0, + ImGuiInputTextFlags_CharsDecimal = 1 << 0, // Allow 0123456789.+-*/ + ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, // Allow 0123456789ABCDEFabcdef + ImGuiInputTextFlags_CharsUppercase = 1 << 2, // Turn a..z into A..Z + ImGuiInputTextFlags_CharsNoBlank = 1 << 3, // Filter out spaces, tabs + ImGuiInputTextFlags_AutoSelectAll = 1 << 4, // Select entire text when first taking mouse focus + ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider looking at the IsItemDeactivatedAfterEdit() function. + ImGuiInputTextFlags_CallbackCompletion = 1 << 6, // Callback on pressing TAB (for completion handling) + ImGuiInputTextFlags_CallbackHistory = 1 << 7, // Callback on pressing Up/Down arrows (for history handling) + ImGuiInputTextFlags_CallbackAlways = 1 << 8, // Callback on each iteration. User code may query cursor position, modify text buffer. + ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. + ImGuiInputTextFlags_AllowTabInput = 1 << 10, // Pressing TAB input a '\t' character into the text field + ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, // In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter (default is opposite: unfocus with Ctrl+Enter, add line with Enter). + ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, // Disable following the cursor horizontally + ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, // Insert mode + ImGuiInputTextFlags_ReadOnly = 1 << 14, // Read-only mode + ImGuiInputTextFlags_Password = 1 << 15, // Password mode, display all characters as '*' + ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). + ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) + ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) + // [Internal] + ImGuiInputTextFlags_Multiline = 1 << 20, // For internal use by InputTextMultiline() + ImGuiInputTextFlags_NoMarkEdited = 1 << 21 // For internal use by functions using InputText() before reformatting data +}; + +// Flags for ImGui::TreeNodeEx(), ImGui::CollapsingHeader*() +enum ImGuiTreeNodeFlags_ +{ + ImGuiTreeNodeFlags_None = 0, + ImGuiTreeNodeFlags_Selected = 1 << 0, // Draw as selected + ImGuiTreeNodeFlags_Framed = 1 << 1, // Full colored frame (e.g. for CollapsingHeader) + ImGuiTreeNodeFlags_AllowItemOverlap = 1 << 2, // Hit testing to allow subsequent widgets to overlap this one + ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack + ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes) + ImGuiTreeNodeFlags_DefaultOpen = 1 << 5, // Default node to be open + ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Need double-click to open node + ImGuiTreeNodeFlags_OpenOnArrow = 1 << 7, // Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open. + ImGuiTreeNodeFlags_Leaf = 1 << 8, // No collapsing, no arrow (use as a convenience for leaf nodes). + ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow + ImGuiTreeNodeFlags_FramePadding = 1 << 10, // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding(). + ImGuiTreeNodeFlags_SpanAvailWidth = 1 << 11, // Extend hit box to the right-most edge, even if not framed. This is not the default in order to allow adding other items on the same line. In the future we may refactor the hit system to be front-to-back, allowing natural overlaps and then this can become the default. + ImGuiTreeNodeFlags_SpanFullWidth = 1 << 12, // Extend hit box to the left-most and right-most edges (bypass the indented area). + ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) + //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 14, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible + ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiTreeNodeFlags_AllowOverlapMode = ImGuiTreeNodeFlags_AllowItemOverlap // [renamed in 1.53] +#endif +}; + +// Flags for ImGui::Selectable() +enum ImGuiSelectableFlags_ +{ + ImGuiSelectableFlags_None = 0, + ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this don't close parent popup window + ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Selectable frame can span all columns (text will still fit in current column) + ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too + ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text + ImGuiSelectableFlags_AllowItemOverlap = 1 << 4 // (WIP) Hit testing to allow subsequent widgets to overlap this one +}; + +// Flags for ImGui::BeginCombo() +enum ImGuiComboFlags_ +{ + ImGuiComboFlags_None = 0, + ImGuiComboFlags_PopupAlignLeft = 1 << 0, // Align the popup toward the left by default + ImGuiComboFlags_HeightSmall = 1 << 1, // Max ~4 items visible. Tip: If you want your combo popup to be a specific size you can use SetNextWindowSizeConstraints() prior to calling BeginCombo() + ImGuiComboFlags_HeightRegular = 1 << 2, // Max ~8 items visible (default) + ImGuiComboFlags_HeightLarge = 1 << 3, // Max ~20 items visible + ImGuiComboFlags_HeightLargest = 1 << 4, // As many fitting items as possible + ImGuiComboFlags_NoArrowButton = 1 << 5, // Display on the preview box without the square arrow button + ImGuiComboFlags_NoPreview = 1 << 6, // Display only a square arrow button + ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest +}; + +// Flags for ImGui::BeginTabBar() +enum ImGuiTabBarFlags_ +{ + ImGuiTabBarFlags_None = 0, + ImGuiTabBarFlags_Reorderable = 1 << 0, // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list + ImGuiTabBarFlags_AutoSelectNewTabs = 1 << 1, // Automatically select new tabs when they appear + ImGuiTabBarFlags_TabListPopupButton = 1 << 2, // Disable buttons to open the tab list popup + ImGuiTabBarFlags_NoCloseWithMiddleMouseButton = 1 << 3, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. + ImGuiTabBarFlags_NoTabListScrollingButtons = 1 << 4, // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll) + ImGuiTabBarFlags_NoTooltip = 1 << 5, // Disable tooltips when hovering a tab + ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 6, // Resize tabs when they don't fit + ImGuiTabBarFlags_FittingPolicyScroll = 1 << 7, // Add scroll buttons when tabs don't fit + ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll, + ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown +}; + +// Flags for ImGui::BeginTabItem() +enum ImGuiTabItemFlags_ +{ + ImGuiTabItemFlags_None = 0, + ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. Also: tab is selected on closure and closure is deferred by one frame to allow code to undo it without flicker. + ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() + ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. + ImGuiTabItemFlags_NoPushId = 1 << 3 // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() +}; + +// Flags for ImGui::IsWindowFocused() +enum ImGuiFocusedFlags_ +{ + ImGuiFocusedFlags_None = 0, + ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused + ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy) + ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use ImGui::GetIO().WantCaptureMouse instead. + ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows +}; + +// Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered() +// Note: if you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that. Please read the FAQ! +// Note: windows with the ImGuiWindowFlags_NoInputs flag are ignored by IsWindowHovered() calls. +enum ImGuiHoveredFlags_ +{ + ImGuiHoveredFlags_None = 0, // Return true if directly over the item/window, not obstructed by another window, not obstructed by an active popup or modal blocking inputs under them. + ImGuiHoveredFlags_ChildWindows = 1 << 0, // IsWindowHovered() only: Return true if any children of the window is hovered + ImGuiHoveredFlags_RootWindow = 1 << 1, // IsWindowHovered() only: Test from root window (top most parent of the current hierarchy) + ImGuiHoveredFlags_AnyWindow = 1 << 2, // IsWindowHovered() only: Return true if any window is hovered + ImGuiHoveredFlags_AllowWhenBlockedByPopup = 1 << 3, // Return true even if a popup window is normally blocking access to this item/window + //ImGuiHoveredFlags_AllowWhenBlockedByModal = 1 << 4, // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet. + ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 5, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. + ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 6, // Return true even if the position is obstructed or overlapped by another window + ImGuiHoveredFlags_AllowWhenDisabled = 1 << 7, // Return true even if the item is disabled + ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, + ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows +}; + +// Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload() +enum ImGuiDragDropFlags_ +{ + ImGuiDragDropFlags_None = 0, + // BeginDragDropSource() flags + ImGuiDragDropFlags_SourceNoPreviewTooltip = 1 << 0, // By default, a successful call to BeginDragDropSource opens a tooltip so you can display a preview or description of the source contents. This flag disable this behavior. + ImGuiDragDropFlags_SourceNoDisableHover = 1 << 1, // By default, when dragging we clear data so that IsItemHovered() will return false, to avoid subsequent user code submitting tooltips. This flag disable this behavior so you can still call IsItemHovered() on the source item. + ImGuiDragDropFlags_SourceNoHoldToOpenOthers = 1 << 2, // Disable the behavior that allows to open tree nodes and collapsing header by holding over them while dragging a source item. + ImGuiDragDropFlags_SourceAllowNullID = 1 << 3, // Allow items such as Text(), Image() that have no unique identifier to be used as drag source, by manufacturing a temporary identifier based on their window-relative position. This is extremely unusual within the dear imgui ecosystem and so we made it explicit. + ImGuiDragDropFlags_SourceExtern = 1 << 4, // External source (from outside of dear imgui), won't attempt to read current item/window info. Will always return true. Only one Extern source can be active simultaneously. + ImGuiDragDropFlags_SourceAutoExpirePayload = 1 << 5, // Automatically expire the payload if the source cease to be submitted (otherwise payloads are persisting while being dragged) + // AcceptDragDropPayload() flags + ImGuiDragDropFlags_AcceptBeforeDelivery = 1 << 10, // AcceptDragDropPayload() will returns true even before the mouse button is released. You can then call IsDelivery() to test if the payload needs to be delivered. + ImGuiDragDropFlags_AcceptNoDrawDefaultRect = 1 << 11, // Do not draw the default highlight rectangle when hovering over target. + ImGuiDragDropFlags_AcceptNoPreviewTooltip = 1 << 12, // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site. + ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect // For peeking ahead and inspecting the payload before delivery. +}; + +// Standard Drag and Drop payload types. You can define you own payload types using short strings. Types starting with '_' are defined by Dear ImGui. +#define IMGUI_PAYLOAD_TYPE_COLOR_3F "_COL3F" // float[3]: Standard type for colors, without alpha. User code may use this type. +#define IMGUI_PAYLOAD_TYPE_COLOR_4F "_COL4F" // float[4]: Standard type for colors. User code may use this type. + +// A primary data type +enum ImGuiDataType_ +{ + ImGuiDataType_S8, // signed char / char (with sensible compilers) + ImGuiDataType_U8, // unsigned char + ImGuiDataType_S16, // short + ImGuiDataType_U16, // unsigned short + ImGuiDataType_S32, // int + ImGuiDataType_U32, // unsigned int + ImGuiDataType_S64, // long long / __int64 + ImGuiDataType_U64, // unsigned long long / unsigned __int64 + ImGuiDataType_Float, // float + ImGuiDataType_Double, // double + ImGuiDataType_COUNT +}; + +// A cardinal direction +enum ImGuiDir_ +{ + ImGuiDir_None = -1, + ImGuiDir_Left = 0, + ImGuiDir_Right = 1, + ImGuiDir_Up = 2, + ImGuiDir_Down = 3, + ImGuiDir_COUNT +}; + +// User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array +enum ImGuiKey_ +{ + ImGuiKey_Tab, + ImGuiKey_LeftArrow, + ImGuiKey_RightArrow, + ImGuiKey_UpArrow, + ImGuiKey_DownArrow, + ImGuiKey_PageUp, + ImGuiKey_PageDown, + ImGuiKey_Home, + ImGuiKey_End, + ImGuiKey_Insert, + ImGuiKey_Delete, + ImGuiKey_Backspace, + ImGuiKey_Space, + ImGuiKey_Enter, + ImGuiKey_Escape, + ImGuiKey_KeyPadEnter, + ImGuiKey_A, // for text edit CTRL+A: select all + ImGuiKey_C, // for text edit CTRL+C: copy + ImGuiKey_V, // for text edit CTRL+V: paste + ImGuiKey_X, // for text edit CTRL+X: cut + ImGuiKey_Y, // for text edit CTRL+Y: redo + ImGuiKey_Z, // for text edit CTRL+Z: undo + ImGuiKey_COUNT +}; + +// Gamepad/Keyboard directional navigation +// Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. +// Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Back-end: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). +// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW. +enum ImGuiNavInput_ +{ + // Gamepad Mapping + ImGuiNavInput_Activate, // activate / open / toggle / tweak value // e.g. Cross (PS4), A (Xbox), A (Switch), Space (Keyboard) + ImGuiNavInput_Cancel, // cancel / close / exit // e.g. Circle (PS4), B (Xbox), B (Switch), Escape (Keyboard) + ImGuiNavInput_Input, // text input / on-screen keyboard // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard) + ImGuiNavInput_Menu, // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard) + ImGuiNavInput_DpadLeft, // move / tweak / resize window (w/ PadMenu) // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard) + ImGuiNavInput_DpadRight, // + ImGuiNavInput_DpadUp, // + ImGuiNavInput_DpadDown, // + ImGuiNavInput_LStickLeft, // scroll / move window (w/ PadMenu) // e.g. Left Analog Stick Left/Right/Up/Down + ImGuiNavInput_LStickRight, // + ImGuiNavInput_LStickUp, // + ImGuiNavInput_LStickDown, // + ImGuiNavInput_FocusPrev, // next window (w/ PadMenu) // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) + ImGuiNavInput_FocusNext, // prev window (w/ PadMenu) // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) + ImGuiNavInput_TweakSlow, // slower tweaks // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) + ImGuiNavInput_TweakFast, // faster tweaks // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) + + // [Internal] Don't use directly! This is used internally to differentiate keyboard from gamepad inputs for behaviors that require to differentiate them. + // Keyboard behavior that have no corresponding gamepad mapping (e.g. CTRL+TAB) will be directly reading from io.KeysDown[] instead of io.NavInputs[]. + ImGuiNavInput_KeyMenu_, // toggle menu // = io.KeyAlt + ImGuiNavInput_KeyLeft_, // move left // = Arrow keys + ImGuiNavInput_KeyRight_, // move right + ImGuiNavInput_KeyUp_, // move up + ImGuiNavInput_KeyDown_, // move down + ImGuiNavInput_COUNT, + ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyMenu_ +}; + +// Configuration flags stored in io.ConfigFlags. Set by user/application. +enum ImGuiConfigFlags_ +{ + ImGuiConfigFlags_None = 0, + ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. NewFrame() will automatically fill io.NavInputs[] based on io.KeysDown[]. + ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. This is mostly to instruct your imgui back-end to fill io.NavInputs[]. Back-end also needs to set ImGuiBackendFlags_HasGamepad. + ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your binding, otherwise ImGui will react as if the mouse is jumping around back and forth. + ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. + ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the back-end. + ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct back-end to not alter mouse cursor shape and visibility. Use if the back-end cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. + + // User storage (to allow your back-end/engine to communicate to code that may be shared between multiple projects. Those flags are not used by core Dear ImGui) + ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. + ImGuiConfigFlags_IsTouchScreen = 1 << 21 // Application is using a touch screen instead of a mouse. +}; + +// Back-end capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom back-end. +enum ImGuiBackendFlags_ +{ + ImGuiBackendFlags_None = 0, + ImGuiBackendFlags_HasGamepad = 1 << 0, // Back-end Platform supports gamepad and currently has one connected. + ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Back-end Platform supports honoring GetMouseCursor() value to change the OS cursor shape. + ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Back-end Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). + ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Back-end Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bits indices. +}; + +// Enumeration for PushStyleColor() / PopStyleColor() +enum ImGuiCol_ +{ + ImGuiCol_Text, + ImGuiCol_TextDisabled, + ImGuiCol_WindowBg, // Background of normal windows + ImGuiCol_ChildBg, // Background of child windows + ImGuiCol_PopupBg, // Background of popups, menus, tooltips windows + ImGuiCol_Border, + ImGuiCol_BorderShadow, + ImGuiCol_FrameBg, // Background of checkbox, radio button, plot, slider, text input + ImGuiCol_FrameBgHovered, + ImGuiCol_FrameBgActive, + ImGuiCol_TitleBg, + ImGuiCol_TitleBgActive, + ImGuiCol_TitleBgCollapsed, + ImGuiCol_MenuBarBg, + ImGuiCol_ScrollbarBg, + ImGuiCol_ScrollbarGrab, + ImGuiCol_ScrollbarGrabHovered, + ImGuiCol_ScrollbarGrabActive, + ImGuiCol_CheckMark, + ImGuiCol_SliderGrab, + ImGuiCol_SliderGrabActive, + ImGuiCol_Button, + ImGuiCol_ButtonHovered, + ImGuiCol_ButtonActive, + ImGuiCol_Header, // Header* colors are used for CollapsingHeader, TreeNode, Selectable, MenuItem + ImGuiCol_HeaderHovered, + ImGuiCol_HeaderActive, + ImGuiCol_Separator, + ImGuiCol_SeparatorHovered, + ImGuiCol_SeparatorActive, + ImGuiCol_ResizeGrip, + ImGuiCol_ResizeGripHovered, + ImGuiCol_ResizeGripActive, + ImGuiCol_Tab, + ImGuiCol_TabHovered, + ImGuiCol_TabActive, + ImGuiCol_TabUnfocused, + ImGuiCol_TabUnfocusedActive, + ImGuiCol_PlotLines, + ImGuiCol_PlotLinesHovered, + ImGuiCol_PlotHistogram, + ImGuiCol_PlotHistogramHovered, + ImGuiCol_TextSelectedBg, + ImGuiCol_DragDropTarget, + ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item + ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB + ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active + ImGuiCol_ModalWindowDimBg, // Darken/colorize entire screen behind a modal window, when one is active + ImGuiCol_COUNT + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg // [renamed in 1.63] + , ImGuiCol_ChildWindowBg = ImGuiCol_ChildBg // [renamed in 1.53] + //ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered, // [unused since 1.60+] the close button now uses regular button colors. + //ImGuiCol_ComboBg, // [unused since 1.53+] ComboBg has been merged with PopupBg, so a redirect isn't accurate. +#endif +}; + +// Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure. +// NB: the enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. During initialization, feel free to just poke into ImGuiStyle directly. +// NB: if changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type. +enum ImGuiStyleVar_ +{ + // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) + ImGuiStyleVar_Alpha, // float Alpha + ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding + ImGuiStyleVar_WindowRounding, // float WindowRounding + ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize + ImGuiStyleVar_WindowMinSize, // ImVec2 WindowMinSize + ImGuiStyleVar_WindowTitleAlign, // ImVec2 WindowTitleAlign + ImGuiStyleVar_ChildRounding, // float ChildRounding + ImGuiStyleVar_ChildBorderSize, // float ChildBorderSize + ImGuiStyleVar_PopupRounding, // float PopupRounding + ImGuiStyleVar_PopupBorderSize, // float PopupBorderSize + ImGuiStyleVar_FramePadding, // ImVec2 FramePadding + ImGuiStyleVar_FrameRounding, // float FrameRounding + ImGuiStyleVar_FrameBorderSize, // float FrameBorderSize + ImGuiStyleVar_ItemSpacing, // ImVec2 ItemSpacing + ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ItemInnerSpacing + ImGuiStyleVar_IndentSpacing, // float IndentSpacing + ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize + ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding + ImGuiStyleVar_GrabMinSize, // float GrabMinSize + ImGuiStyleVar_GrabRounding, // float GrabRounding + ImGuiStyleVar_TabRounding, // float TabRounding + ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign + ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign + ImGuiStyleVar_COUNT + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT // [renamed in 1.60] + , ImGuiStyleVar_ChildWindowRounding = ImGuiStyleVar_ChildRounding // [renamed in 1.53] +#endif +}; + +// Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() +enum ImGuiColorEditFlags_ +{ + ImGuiColorEditFlags_None = 0, + ImGuiColorEditFlags_NoAlpha = 1 << 1, // // ColorEdit, ColorPicker, ColorButton: ignore Alpha component (will only read 3 components from the input pointer). + ImGuiColorEditFlags_NoPicker = 1 << 2, // // ColorEdit: disable picker when clicking on colored square. + ImGuiColorEditFlags_NoOptions = 1 << 3, // // ColorEdit: disable toggling options menu when right-clicking on inputs/small preview. + ImGuiColorEditFlags_NoSmallPreview = 1 << 4, // // ColorEdit, ColorPicker: disable colored square preview next to the inputs. (e.g. to show only the inputs) + ImGuiColorEditFlags_NoInputs = 1 << 5, // // ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the small preview colored square). + ImGuiColorEditFlags_NoTooltip = 1 << 6, // // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview. + ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker). + ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small colored square preview instead. + ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source. + + // User Options (right-click on widget to change some of them). + ImGuiColorEditFlags_AlphaBar = 1 << 16, // // ColorEdit, ColorPicker: show vertical alpha bar/gradient in picker. + ImGuiColorEditFlags_AlphaPreview = 1 << 17, // // ColorEdit, ColorPicker, ColorButton: display preview as a transparent color over a checkerboard, instead of opaque. + ImGuiColorEditFlags_AlphaPreviewHalf= 1 << 18, // // ColorEdit, ColorPicker, ColorButton: display half opaque / half checkerboard, instead of opaque. + ImGuiColorEditFlags_HDR = 1 << 19, // // (WIP) ColorEdit: Currently only disable 0.0f..1.0f limits in RGBA edition (note: you probably want to use ImGuiColorEditFlags_Float flag as well). + ImGuiColorEditFlags_DisplayRGB = 1 << 20, // [Display] // ColorEdit: override _display_ type among RGB/HSV/Hex. ColorPicker: select any combination using one or more of RGB/HSV/Hex. + ImGuiColorEditFlags_DisplayHSV = 1 << 21, // [Display] // " + ImGuiColorEditFlags_DisplayHex = 1 << 22, // [Display] // " + ImGuiColorEditFlags_Uint8 = 1 << 23, // [DataType] // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0..255. + ImGuiColorEditFlags_Float = 1 << 24, // [DataType] // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0.0f..1.0f floats instead of 0..255 integers. No round-trip of value via integers. + ImGuiColorEditFlags_PickerHueBar = 1 << 25, // [Picker] // ColorPicker: bar for Hue, rectangle for Sat/Value. + ImGuiColorEditFlags_PickerHueWheel = 1 << 26, // [Picker] // ColorPicker: wheel for Hue, triangle for Sat/Value. + ImGuiColorEditFlags_InputRGB = 1 << 27, // [Input] // ColorEdit, ColorPicker: input and output data in RGB format. + ImGuiColorEditFlags_InputHSV = 1 << 28, // [Input] // ColorEdit, ColorPicker: input and output data in HSV format. + + // Defaults Options. You can set application defaults using SetColorEditOptions(). The intent is that you probably don't want to + // override them in most of your calls. Let the user choose via the option menu and/or call SetColorEditOptions() once during startup. + ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_PickerHueBar, + + // [Internal] Masks + ImGuiColorEditFlags__DisplayMask = ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_DisplayHSV|ImGuiColorEditFlags_DisplayHex, + ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_Float, + ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel|ImGuiColorEditFlags_PickerHueBar, + ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_InputHSV + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex // [renamed in 1.69] +#endif +}; + +// Enumeration for GetMouseCursor() +// User code may request binding to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here +enum ImGuiMouseCursor_ +{ + ImGuiMouseCursor_None = -1, + ImGuiMouseCursor_Arrow = 0, + ImGuiMouseCursor_TextInput, // When hovering over InputText, etc. + ImGuiMouseCursor_ResizeAll, // (Unused by Dear ImGui functions) + ImGuiMouseCursor_ResizeNS, // When hovering over an horizontal border + ImGuiMouseCursor_ResizeEW, // When hovering over a vertical border or a column + ImGuiMouseCursor_ResizeNESW, // When hovering over the bottom-left corner of a window + ImGuiMouseCursor_ResizeNWSE, // When hovering over the bottom-right corner of a window + ImGuiMouseCursor_Hand, // (Unused by Dear ImGui functions. Use for e.g. hyperlinks) + ImGuiMouseCursor_COUNT + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiMouseCursor_Count_ = ImGuiMouseCursor_COUNT // [renamed in 1.60] +#endif +}; + +// Enumateration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions +// Represent a condition. +// Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always. +enum ImGuiCond_ +{ + ImGuiCond_Always = 1 << 0, // Set the variable + ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call with succeed) + ImGuiCond_FirstUseEver = 1 << 2, // Set the variable if the object/window has no persistently saved data (no entry in .ini file) + ImGuiCond_Appearing = 1 << 3 // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) +}; + +//----------------------------------------------------------------------------- +// Helpers: Memory allocations macros +// IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() +// We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. +// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. +//----------------------------------------------------------------------------- + +struct ImNewDummy {}; +inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } +inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() +#define IM_ALLOC(_SIZE) ImGui::MemAlloc(_SIZE) +#define IM_FREE(_PTR) ImGui::MemFree(_PTR) +#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) +#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE +template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } + +//----------------------------------------------------------------------------- +// Helper: ImVector<> +// Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug). +// You generally do NOT need to care or use this ever. But we need to make it available in imgui.h because some of our data structures are relying on it. +// Important: clear() frees memory, resize(0) keep the allocated buffer. We use resize(0) a lot to intentionally recycle allocated buffers across frames and amortize our costs. +// Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that, +// do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset. +//----------------------------------------------------------------------------- + +template +struct ImVector +{ + int Size; + int Capacity; + T* Data; + + // Provide standard typedefs but we don't use them ourselves. + typedef T value_type; + typedef value_type* iterator; + typedef const value_type* const_iterator; + + // Constructors, destructor + inline ImVector() { Size = Capacity = 0; Data = NULL; } + inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } + inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } + inline ~ImVector() { if (Data) IM_FREE(Data); } + + inline bool empty() const { return Size == 0; } + inline int size() const { return Size; } + inline int size_in_bytes() const { return Size * (int)sizeof(T); } + inline int capacity() const { return Capacity; } + inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } + inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } + + inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } + inline T* begin() { return Data; } + inline const T* begin() const { return Data; } + inline T* end() { return Data + Size; } + inline const T* end() const { return Data + Size; } + inline T& front() { IM_ASSERT(Size > 0); return Data[0]; } + inline const T& front() const { IM_ASSERT(Size > 0); return Data[0]; } + inline T& back() { IM_ASSERT(Size > 0); return Data[Size - 1]; } + inline const T& back() const { IM_ASSERT(Size > 0); return Data[Size - 1]; } + inline void swap(ImVector& rhs) { int rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; int rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; T* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; } + + inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } + inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } + inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } + inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } + + // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. + inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } + inline void pop_back() { IM_ASSERT(Size > 0); Size--; } + inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } + inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data+Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } + inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data+Size && it_last > it && it_last <= Data+Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - count) * sizeof(T)); Size -= (int)count; return Data + off; } + inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data+Size); const ptrdiff_t off = it - Data; if (it < Data+Size-1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } + inline T* insert(const T* it, const T& v) { IM_ASSERT(it >= Data && it <= Data+Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; } + inline bool contains(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; } + inline T* find(const T& v) { T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } + inline const T* find(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } + inline bool find_erase(const T& v) { const T* it = find(v); if (it < Data + Size) { erase(it); return true; } return false; } + inline bool find_erase_unsorted(const T& v) { const T* it = find(v); if (it < Data + Size) { erase_unsorted(it); return true; } return false; } + inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; return (int)off; } +}; + +//----------------------------------------------------------------------------- +// ImGuiStyle +// You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame(). +// During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values, +// and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors. +//----------------------------------------------------------------------------- + +struct ImGuiStyle +{ + float Alpha; // Global alpha applies to everything in Dear ImGui. + ImVec2 WindowPadding; // Padding within a window. + float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. + float WindowBorderSize; // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). + ImVec2 WindowMinSize; // Minimum window size. This is a global setting. If you want to constraint individual windows, use SetNextWindowSizeConstraints(). + ImVec2 WindowTitleAlign; // Alignment for title bar text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered. + ImGuiDir WindowMenuButtonPosition; // Side of the collapsing/docking button in the title bar (None/Left/Right). Defaults to ImGuiDir_Left. + float ChildRounding; // Radius of child window corners rounding. Set to 0.0f to have rectangular windows. + float ChildBorderSize; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). + float PopupRounding; // Radius of popup window corners rounding. (Note that tooltip windows use WindowRounding) + float PopupBorderSize; // Thickness of border around popup/tooltip windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). + ImVec2 FramePadding; // Padding within a framed rectangle (used by most widgets). + float FrameRounding; // Radius of frame corners rounding. Set to 0.0f to have rectangular frame (used by most widgets). + float FrameBorderSize; // Thickness of border around frames. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). + ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines. + ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label). + ImVec2 TouchExtraPadding; // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! + float IndentSpacing; // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). + float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). + float ScrollbarSize; // Width of the vertical scrollbar, Height of the horizontal scrollbar. + float ScrollbarRounding; // Radius of grab corners for scrollbar. + float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar. + float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. + float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. + float TabBorderSize; // Thickness of border around tabs. + ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. + ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). + ImVec2 SelectableTextAlign; // Alignment of selectable text when selectable is larger than text. Defaults to (0.0f, 0.0f) (top-left aligned). + ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows. + ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! + float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. + bool AntiAliasedLines; // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU. + bool AntiAliasedFill; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) + float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. + ImVec4 Colors[ImGuiCol_COUNT]; + + IMGUI_API ImGuiStyle(); + IMGUI_API void ScaleAllSizes(float scale_factor); +}; + +//----------------------------------------------------------------------------- +// ImGuiIO +// Communicate most settings and inputs/outputs to Dear ImGui using this structure. +// Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. +//----------------------------------------------------------------------------- + +struct ImGuiIO +{ + //------------------------------------------------------------------ + // Configuration (fill once) // Default value + //------------------------------------------------------------------ + + ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. + ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by back-end (imgui_impl_xxx files or custom back-end) to communicate features supported by the back-end. + ImVec2 DisplaySize; // // Main display size, in pixels. + float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. + float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. + const char* IniFilename; // = "imgui.ini" // Path to .ini file. Set NULL to disable automatic .ini loading/saving, if e.g. you want to manually load/save from memory. + const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). + float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. + float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. + float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging. + int KeyMap[ImGuiKey_COUNT]; // // Map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. + float KeyRepeatDelay; // = 0.250f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). + float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds. + void* UserData; // = NULL // Store your own data for retrieval by callbacks. + + ImFontAtlas*Fonts; // // Font atlas: load, rasterize and pack one or more fonts into a single texture. + float FontGlobalScale; // = 1.0f // Global scale all fonts + bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. + ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0]. + ImVec2 DisplayFramebufferScale; // = (1, 1) // For retina display or other situations where window coordinates are different from framebuffer coordinates. This generally ends up in ImDrawData::FramebufferScale. + + // Miscellaneous options + bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by back-end implementations. + bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl (was called io.OptMacOSXBehaviors prior to 1.63) + bool ConfigInputTextCursorBlink; // = true // Set to false to disable blinking cursor, for users who consider it distracting. (was called: io.OptCursorBlink prior to 1.63) + bool ConfigWindowsResizeFromEdges; // = true // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag) + bool ConfigWindowsMoveFromTitleBarOnly; // = false // [BETA] Set to true to only allow moving windows when clicked+dragged from the title bar. Windows without a title bar are not affected. + float ConfigWindowsMemoryCompactTimer;// = 60.0f // [BETA] Compact window memory usage when unused. Set to -1.0f to disable. + + //------------------------------------------------------------------ + // Platform Functions + // (the imgui_impl_xxxx back-end files are setting those up for you) + //------------------------------------------------------------------ + + // Optional: Platform/Renderer back-end name (informational only! will be displayed in About Window) + User data for back-end/wrappers to store their own stuff. + const char* BackendPlatformName; // = NULL + const char* BackendRendererName; // = NULL + void* BackendPlatformUserData; // = NULL + void* BackendRendererUserData; // = NULL + void* BackendLanguageUserData; // = NULL + + // Optional: Access OS clipboard + // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) + const char* (*GetClipboardTextFn)(void* user_data); + void (*SetClipboardTextFn)(void* user_data, const char* text); + void* ClipboardUserData; + + // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) + // (default to use native imm32 api on Windows) + void (*ImeSetInputScreenPosFn)(int x, int y); + void* ImeWindowHandle; // = NULL // (Windows) Set this to your HWND to get automatic IME cursor positioning. + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // [OBSOLETE since 1.60+] Rendering function, will be automatically called in Render(). Please call your rendering function yourself now! + // You can obtain the ImDrawData* by calling ImGui::GetDrawData() after Render(). See example applications if you are unsure of how to implement this. + void (*RenderDrawListsFn)(ImDrawData* data); +#else + // This is only here to keep ImGuiIO the same size/layout, so that IMGUI_DISABLE_OBSOLETE_FUNCTIONS can exceptionally be used outside of imconfig.h. + void* RenderDrawListsFnUnused; +#endif + + //------------------------------------------------------------------ + // Input - Fill before calling NewFrame() + //------------------------------------------------------------------ + + ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX,-FLT_MAX) if mouse is unavailable (on another screen, etc.) + bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras. ImGui itself mostly only uses left button (BeginPopupContext** are using right button). Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. + float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. + float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends. + bool KeyCtrl; // Keyboard modifier pressed: Control + bool KeyShift; // Keyboard modifier pressed: Shift + bool KeyAlt; // Keyboard modifier pressed: Alt + bool KeySuper; // Keyboard modifier pressed: Cmd/Super/Windows + bool KeysDown[512]; // Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). + float NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame(). + + // Functions + IMGUI_API void AddInputCharacter(unsigned int c); // Queue new character input + IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string + IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually + + //------------------------------------------------------------------ + // Output - Retrieve after calling NewFrame() + //------------------------------------------------------------------ + + bool WantCaptureMouse; // When io.WantCaptureMouse is true, imgui will use the mouse inputs, do not dispatch them to your main game/application (in both cases, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). + bool WantCaptureKeyboard; // When io.WantCaptureKeyboard is true, imgui will use the keyboard inputs, do not dispatch them to your main game/application (in both cases, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). + bool WantTextInput; // Mobile/console: when io.WantTextInput is true, you may display an on-screen keyboard. This is set by ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). + bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. + bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. IMPORTANT: You need to clear io.WantSaveIniSettings yourself. + bool NavActive; // Directional navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. + bool NavVisible; // Directional navigation is visible and allowed (will handle ImGuiKey_NavXXX events). + float Framerate; // Application framerate estimation, in frame per second. Solely for convenience. Rolling average estimation based on IO.DeltaTime over 120 frames + int MetricsRenderVertices; // Vertices output during last call to Render() + int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 + int MetricsRenderWindows; // Number of visible windows + int MetricsActiveWindows; // Number of active windows + int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. + ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. + + //------------------------------------------------------------------ + // [Internal] ImGui will maintain those fields. Forward compatibility not guaranteed! + //------------------------------------------------------------------ + + ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) + ImVec2 MouseClickedPos[5]; // Position at time of clicking + double MouseClickedTime[5]; // Time of last click (used to figure out double-click) + bool MouseClicked[5]; // Mouse button went from !Down to Down + bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? + bool MouseReleased[5]; // Mouse button went from Down to !Down + bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window. We don't request mouse capture from the application if click started outside ImGui bounds. + bool MouseDownWasDoubleClick[5]; // Track if button down was a double-click + float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) + float MouseDownDurationPrev[5]; // Previous time the mouse button has been down + ImVec2 MouseDragMaxDistanceAbs[5]; // Maximum distance, absolute, on each axis, of how much mouse has traveled from the clicking point + float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point + float KeysDownDuration[512]; // Duration the keyboard key has been down (0.0f == just pressed) + float KeysDownDurationPrev[512]; // Previous duration the key has been down + float NavInputsDownDuration[ImGuiNavInput_COUNT]; + float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; + ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper. + + IMGUI_API ImGuiIO(); +}; + +//----------------------------------------------------------------------------- +// Misc data structures +//----------------------------------------------------------------------------- + +// Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used. +// The callback function should return 0 by default. +// Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details) +// - ImGuiInputTextFlags_CallbackCompletion: Callback on pressing TAB +// - ImGuiInputTextFlags_CallbackHistory: Callback on pressing Up/Down arrows +// - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration +// - ImGuiInputTextFlags_CallbackCharFilter: Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. +// - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. +struct ImGuiInputTextCallbackData +{ + ImGuiInputTextFlags EventFlag; // One ImGuiInputTextFlags_Callback* // Read-only + ImGuiInputTextFlags Flags; // What user passed to InputText() // Read-only + void* UserData; // What user passed to InputText() // Read-only + + // Arguments for the different callback events + // - To modify the text buffer in a callback, prefer using the InsertChars() / DeleteChars() function. InsertChars() will take care of calling the resize callback if necessary. + // - If you know your edits are not going to resize the underlying buffer allocation, you may modify the contents of 'Buf[]' directly. You need to update 'BufTextLen' accordingly (0 <= BufTextLen < BufSize) and set 'BufDirty'' to true so InputText can update its internal state. + ImWchar EventChar; // Character input // Read-write // [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0; + ImGuiKey EventKey; // Key pressed (Up/Down/TAB) // Read-only // [Completion,History] + char* Buf; // Text buffer // Read-write // [Resize] Can replace pointer / [Completion,History,Always] Only write to pointed data, don't replace the actual pointer! + int BufTextLen; // Text length (in bytes) // Read-write // [Resize,Completion,History,Always] Exclude zero-terminator storage. In C land: == strlen(some_text), in C++ land: string.length() + int BufSize; // Buffer size (in bytes) = capacity+1 // Read-only // [Resize,Completion,History,Always] Include zero-terminator storage. In C land == ARRAYSIZE(my_char_array), in C++ land: string.capacity()+1 + bool BufDirty; // Set if you modify Buf/BufTextLen! // Write // [Completion,History,Always] + int CursorPos; // // Read-write // [Completion,History,Always] + int SelectionStart; // // Read-write // [Completion,History,Always] == to SelectionEnd when no selection) + int SelectionEnd; // // Read-write // [Completion,History,Always] + + // Helper functions for text manipulation. + // Use those function to benefit from the CallbackResize behaviors. Calling those function reset the selection. + IMGUI_API ImGuiInputTextCallbackData(); + IMGUI_API void DeleteChars(int pos, int bytes_count); + IMGUI_API void InsertChars(int pos, const char* text, const char* text_end = NULL); + bool HasSelection() const { return SelectionStart != SelectionEnd; } +}; + +// Resizing callback data to apply custom constraint. As enabled by SetNextWindowSizeConstraints(). Callback is called during the next Begin(). +// NB: For basic min/max size constraint on each axis you don't need to use the callback! The SetNextWindowSizeConstraints() parameters are enough. +struct ImGuiSizeCallbackData +{ + void* UserData; // Read-only. What user passed to SetNextWindowSizeConstraints() + ImVec2 Pos; // Read-only. Window position, for reference. + ImVec2 CurrentSize; // Read-only. Current window size. + ImVec2 DesiredSize; // Read-write. Desired size, based on user's mouse position. Write to this field to restrain resizing. +}; + +// Data payload for Drag and Drop operations: AcceptDragDropPayload(), GetDragDropPayload() +struct ImGuiPayload +{ + // Members + void* Data; // Data (copied and owned by dear imgui) + int DataSize; // Data size + + // [Internal] + ImGuiID SourceId; // Source item id + ImGuiID SourceParentId; // Source parent id (if available) + int DataFrameCount; // Data timestamp + char DataType[32+1]; // Data type tag (short user-supplied string, 32 characters max) + bool Preview; // Set when AcceptDragDropPayload() was called and mouse has been hovering the target item (nb: handle overlapping drag targets) + bool Delivery; // Set when AcceptDragDropPayload() was called and mouse button is released over the target item. + + ImGuiPayload() { Clear(); } + void Clear() { SourceId = SourceParentId = 0; Data = NULL; DataSize = 0; memset(DataType, 0, sizeof(DataType)); DataFrameCount = -1; Preview = Delivery = false; } + bool IsDataType(const char* type) const { return DataFrameCount != -1 && strcmp(type, DataType) == 0; } + bool IsPreview() const { return Preview; } + bool IsDelivery() const { return Delivery; } +}; + +//----------------------------------------------------------------------------- +// Obsolete functions (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) +// Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. +//----------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +namespace ImGui +{ + // OBSOLETED in 1.72 (from July 2019) + static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } + // OBSOLETED in 1.71 (from June 2019) + static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } + // OBSOLETED in 1.70 (from May 2019) + static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } + // OBSOLETED in 1.69 (from Mar 2019) + static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } + // OBSOLETED in 1.66 (from Sep 2018) + static inline void SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); } + // OBSOLETED in 1.63 (between Aug 2018 and Sept 2018) + static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } + // OBSOLETED in 1.61 (between Apr 2018 and Aug 2018) + IMGUI_API bool InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags = 0); // Use the 'const char* format' version instead of 'decimal_precision'! + IMGUI_API bool InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags = 0); + IMGUI_API bool InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags = 0); + // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) + static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } + static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } + static inline ImVec2 CalcItemRectClosestPoint(const ImVec2& pos, bool on_edge = false, float outward = 0.f) { IM_UNUSED(on_edge); IM_UNUSED(outward); IM_ASSERT(0); return pos; } + // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + static inline void ShowTestWindow() { return ShowDemoWindow(); } + static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } + static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } + static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } + static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } + // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) + IMGUI_API bool Begin(const char* name, bool* p_open, const ImVec2& size_on_first_use, float bg_alpha_override = -1.0f, ImGuiWindowFlags flags = 0); // Use SetNextWindowSize(size, ImGuiCond_FirstUseEver) + SetNextWindowBgAlpha() instead. + static inline bool IsRootWindowOrAnyChildHovered() { return IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); } + static inline void AlignFirstTextHeightToWidgets() { AlignTextToFramePadding(); } + static inline void SetNextWindowPosCenter(ImGuiCond c=0) { ImGuiIO& io = GetIO(); SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), c, ImVec2(0.5f, 0.5f)); } +} +typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETED in 1.63 (from Aug 2018): made the names consistent +typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; +#endif + +//----------------------------------------------------------------------------- +// Helpers +//----------------------------------------------------------------------------- + +// Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. +// Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); +struct ImGuiOnceUponAFrame +{ + ImGuiOnceUponAFrame() { RefFrame = -1; } + mutable int RefFrame; + operator bool() const { int current_frame = ImGui::GetFrameCount(); if (RefFrame == current_frame) return false; RefFrame = current_frame; return true; } +}; + +// Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" +struct ImGuiTextFilter +{ + IMGUI_API ImGuiTextFilter(const char* default_filter = ""); + IMGUI_API bool Draw(const char* label = "Filter (inc,-exc)", float width = 0.0f); // Helper calling InputText+Build + IMGUI_API bool PassFilter(const char* text, const char* text_end = NULL) const; + IMGUI_API void Build(); + void Clear() { InputBuf[0] = 0; Build(); } + bool IsActive() const { return !Filters.empty(); } + + // [Internal] + struct ImGuiTextRange + { + const char* b; + const char* e; + + ImGuiTextRange() { b = e = NULL; } + ImGuiTextRange(const char* _b, const char* _e) { b = _b; e = _e; } + bool empty() const { return b == e; } + IMGUI_API void split(char separator, ImVector* out) const; + }; + char InputBuf[256]; + ImVectorFilters; + int CountGrep; +}; + +// Helper: Growable text buffer for logging/accumulating text +// (this could be called 'ImGuiTextBuilder' / 'ImGuiStringBuilder') +struct ImGuiTextBuffer +{ + ImVector Buf; + IMGUI_API static char EmptyString[1]; + + ImGuiTextBuffer() { } + inline char operator[](int i) { IM_ASSERT(Buf.Data != NULL); return Buf.Data[i]; } + const char* begin() const { return Buf.Data ? &Buf.front() : EmptyString; } + const char* end() const { return Buf.Data ? &Buf.back() : EmptyString; } // Buf is zero-terminated, so end() will point on the zero-terminator + int size() const { return Buf.Size ? Buf.Size - 1 : 0; } + bool empty() { return Buf.Size <= 1; } + void clear() { Buf.clear(); } + void reserve(int capacity) { Buf.reserve(capacity); } + const char* c_str() const { return Buf.Data ? Buf.Data : EmptyString; } + IMGUI_API void append(const char* str, const char* str_end = NULL); + IMGUI_API void appendf(const char* fmt, ...) IM_FMTARGS(2); + IMGUI_API void appendfv(const char* fmt, va_list args) IM_FMTLIST(2); +}; + +// Helper: Key->Value storage +// Typically you don't have to worry about this since a storage is held within each Window. +// We use it to e.g. store collapse state for a tree (Int 0/1) +// This is optimized for efficient lookup (dichotomy into a contiguous buffer) and rare insertion (typically tied to user interactions aka max once a frame) +// You can use it as custom user storage for temporary values. Declare your own storage if, for example: +// - You want to manipulate the open/close state of a particular sub-tree in your interface (tree node uses Int 0/1 to store their state). +// - You want to store custom debug data easily without adding or editing structures in your code (probably not efficient, but convenient) +// Types are NOT stored, so it is up to you to make sure your Key don't collide with different types. +struct ImGuiStorage +{ + // [Internal] + struct ImGuiStoragePair + { + ImGuiID key; + union { int val_i; float val_f; void* val_p; }; + ImGuiStoragePair(ImGuiID _key, int _val_i) { key = _key; val_i = _val_i; } + ImGuiStoragePair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; } + ImGuiStoragePair(ImGuiID _key, void* _val_p) { key = _key; val_p = _val_p; } + }; + + ImVector Data; + + // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) + // - Set***() functions find pair, insertion on demand if missing. + // - Sorted insertion is costly, paid once. A typical frame shouldn't need to insert any new pair. + void Clear() { Data.clear(); } + IMGUI_API int GetInt(ImGuiID key, int default_val = 0) const; + IMGUI_API void SetInt(ImGuiID key, int val); + IMGUI_API bool GetBool(ImGuiID key, bool default_val = false) const; + IMGUI_API void SetBool(ImGuiID key, bool val); + IMGUI_API float GetFloat(ImGuiID key, float default_val = 0.0f) const; + IMGUI_API void SetFloat(ImGuiID key, float val); + IMGUI_API void* GetVoidPtr(ImGuiID key) const; // default_val is NULL + IMGUI_API void SetVoidPtr(ImGuiID key, void* val); + + // - Get***Ref() functions finds pair, insert on demand if missing, return pointer. Useful if you intend to do Get+Set. + // - References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer. + // - A typical use case where this is convenient for quick hacking (e.g. add storage during a live Edit&Continue session if you can't modify existing struct) + // float* pvar = ImGui::GetFloatRef(key); ImGui::SliderFloat("var", pvar, 0, 100.0f); some_var += *pvar; + IMGUI_API int* GetIntRef(ImGuiID key, int default_val = 0); + IMGUI_API bool* GetBoolRef(ImGuiID key, bool default_val = false); + IMGUI_API float* GetFloatRef(ImGuiID key, float default_val = 0.0f); + IMGUI_API void** GetVoidPtrRef(ImGuiID key, void* default_val = NULL); + + // Use on your own storage if you know only integer are being stored (open/close all tree nodes) + IMGUI_API void SetAllInt(int val); + + // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. + IMGUI_API void BuildSortByKey(); +}; + +// Helper: Manually clip large list of items. +// If you are submitting lots of evenly spaced items and you have a random access to the list, you can perform coarse clipping based on visibility to save yourself from processing those items at all. +// The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped. +// ImGui already clip items based on their bounds but it needs to measure text size to do so. Coarse clipping before submission makes this cost and your own data fetching/submission cost null. +// Usage: +// ImGuiListClipper clipper(1000); // we have 1000 elements, evenly spaced. +// while (clipper.Step()) +// for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) +// ImGui::Text("line number %d", i); +// - Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height (step skipped if we passed a known height as second arg to constructor). +// - Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. +// - (Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user call Step(). Does nothing and switch to Step 3.) +// - Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. +struct ImGuiListClipper +{ + float StartPosY; + float ItemsHeight; + int ItemsCount, StepNo, DisplayStart, DisplayEnd; + + // items_count: Use -1 to ignore (you can call Begin later). Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step). + // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). + // If you don't specify an items_height, you NEED to call Step(). If you specify items_height you may call the old Begin()/End() api directly, but prefer calling Step(). + ImGuiListClipper(int items_count = -1, float items_height = -1.0f) { Begin(items_count, items_height); } // NB: Begin() initialize every fields (as we allow user to call Begin/End multiple times on a same instance if they want). + ~ImGuiListClipper() { IM_ASSERT(ItemsCount == -1); } // Assert if user forgot to call End() or Step() until false. + + IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. + IMGUI_API void Begin(int items_count, float items_height = -1.0f); // Automatically called by constructor if you passed 'items_count' or by Step() in Step 1. + IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. +}; + +// Helpers macros to generate 32-bits encoded colors +#ifdef IMGUI_USE_BGRA_PACKED_COLOR +#define IM_COL32_R_SHIFT 16 +#define IM_COL32_G_SHIFT 8 +#define IM_COL32_B_SHIFT 0 +#define IM_COL32_A_SHIFT 24 +#define IM_COL32_A_MASK 0xFF000000 +#else +#define IM_COL32_R_SHIFT 0 +#define IM_COL32_G_SHIFT 8 +#define IM_COL32_B_SHIFT 16 +#define IM_COL32_A_SHIFT 24 +#define IM_COL32_A_MASK 0xFF000000 +#endif +#define IM_COL32(R,G,B,A) (((ImU32)(A)<>IM_COL32_R_SHIFT)&0xFF) * sc; Value.y = (float)((rgba>>IM_COL32_G_SHIFT)&0xFF) * sc; Value.z = (float)((rgba>>IM_COL32_B_SHIFT)&0xFF) * sc; Value.w = (float)((rgba>>IM_COL32_A_SHIFT)&0xFF) * sc; } + ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } + ImColor(const ImVec4& col) { Value = col; } + inline operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } + inline operator ImVec4() const { return Value; } + + // FIXME-OBSOLETE: May need to obsolete/cleanup those helpers. + inline void SetHSV(float h, float s, float v, float a = 1.0f){ ImGui::ColorConvertHSVtoRGB(h, s, v, Value.x, Value.y, Value.z); Value.w = a; } + static ImColor HSV(float h, float s, float v, float a = 1.0f) { float r,g,b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r,g,b,a); } +}; + +//----------------------------------------------------------------------------- +// Draw List API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) +// Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. +//----------------------------------------------------------------------------- + +// Draw callbacks for advanced uses. +// NB: You most likely do NOT need to use draw callbacks just to create your own widget or customized UI rendering, +// you can poke into the draw list for that! Draw callback may be useful for example to: +// A) Change your GPU render state, +// B) render a complex 3D scene inside a UI element without an intermediate texture/render target, etc. +// The expected behavior from your rendering function is 'if (cmd.UserCallback != NULL) { cmd.UserCallback(parent_list, cmd); } else { RenderTriangles() }' +// If you want to override the signature of ImDrawCallback, you can simply use e.g. '#define ImDrawCallback MyDrawCallback' (in imconfig.h) + update rendering back-end accordingly. +#ifndef ImDrawCallback +typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd); +#endif + +// Special Draw callback value to request renderer back-end to reset the graphics/render state. +// The renderer back-end needs to handle this special value, otherwise it will crash trying to call a function at this address. +// This is useful for example if you submitted callbacks which you know have altered the render state and you want it to be restored. +// It is not done by default because they are many perfectly useful way of altering render state for imgui contents (e.g. changing shader/blending settings before an Image call). +#define ImDrawCallback_ResetRenderState (ImDrawCallback)(-1) + +// Typically, 1 command = 1 GPU draw call (unless command is a callback) +// Pre 1.71 back-ends will typically ignore the VtxOffset/IdxOffset fields. When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' +// is enabled, those fields allow us to render meshes larger than 64K vertices while keeping 16-bits indices. +struct ImDrawCmd +{ + unsigned int ElemCount; // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. + ImVec4 ClipRect; // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates + ImTextureID TextureId; // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. + unsigned int VtxOffset; // Start offset in vertex buffer. Pre-1.71 or without ImGuiBackendFlags_RendererHasVtxOffset: always 0. With ImGuiBackendFlags_RendererHasVtxOffset: may be >0 to support meshes larger than 64K vertices with 16-bits indices. + unsigned int IdxOffset; // Start offset in index buffer. Always equal to sum of ElemCount drawn so far. + ImDrawCallback UserCallback; // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. + void* UserCallbackData; // The draw callback code can access this. + + ImDrawCmd() { ElemCount = 0; TextureId = (ImTextureID)NULL; VtxOffset = IdxOffset = 0; UserCallback = NULL; UserCallbackData = NULL; } +}; + +// Vertex index +// (to allow large meshes with 16-bits indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end) +// (to use 32-bits indices: override with '#define ImDrawIdx unsigned int' in imconfig.h) +#ifndef ImDrawIdx +typedef unsigned short ImDrawIdx; +#endif + +// Vertex layout +#ifndef IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT +struct ImDrawVert +{ + ImVec2 pos; + ImVec2 uv; + ImU32 col; +}; +#else +// You can override the vertex format layout by defining IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT in imconfig.h +// The code expect ImVec2 pos (8 bytes), ImVec2 uv (8 bytes), ImU32 col (4 bytes), but you can re-order them or add other fields as needed to simplify integration in your engine. +// The type has to be described within the macro (you can either declare the struct or use a typedef). This is because ImVec2/ImU32 are likely not declared a the time you'd want to set your type up. +// NOTE: IMGUI DOESN'T CLEAR THE STRUCTURE AND DOESN'T CALL A CONSTRUCTOR SO ANY CUSTOM FIELD WILL BE UNINITIALIZED. IF YOU ADD EXTRA FIELDS (SUCH AS A 'Z' COORDINATES) YOU WILL NEED TO CLEAR THEM DURING RENDER OR TO IGNORE THEM. +IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT; +#endif + +// For use by ImDrawListSplitter. +struct ImDrawChannel +{ + ImVector _CmdBuffer; + ImVector _IdxBuffer; +}; + +// Split/Merge functions are used to split the draw list into different layers which can be drawn into out of order. +// This is used by the Columns api, so items of each column can be batched together in a same draw call. +struct ImDrawListSplitter +{ + int _Current; // Current channel number (0) + int _Count; // Number of active channels (1+) + ImVector _Channels; // Draw channels (not resized down so _Count might be < Channels.Size) + + inline ImDrawListSplitter() { Clear(); } + inline ~ImDrawListSplitter() { ClearFreeMemory(); } + inline void Clear() { _Current = 0; _Count = 1; } // Do not clear Channels[] so our allocations are reused next frame + IMGUI_API void ClearFreeMemory(); + IMGUI_API void Split(ImDrawList* draw_list, int count); + IMGUI_API void Merge(ImDrawList* draw_list); + IMGUI_API void SetCurrentChannel(ImDrawList* draw_list, int channel_idx); +}; + +enum ImDrawCornerFlags_ +{ + ImDrawCornerFlags_None = 0, + ImDrawCornerFlags_TopLeft = 1 << 0, // 0x1 + ImDrawCornerFlags_TopRight = 1 << 1, // 0x2 + ImDrawCornerFlags_BotLeft = 1 << 2, // 0x4 + ImDrawCornerFlags_BotRight = 1 << 3, // 0x8 + ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, // 0x3 + ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, // 0xC + ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, // 0x5 + ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, // 0xA + ImDrawCornerFlags_All = 0xF // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience +}; + +enum ImDrawListFlags_ +{ + ImDrawListFlags_None = 0, + ImDrawListFlags_AntiAliasedLines = 1 << 0, // Lines are anti-aliased (*2 the number of triangles for 1.0f wide line, otherwise *3 the number of triangles) + ImDrawListFlags_AntiAliasedFill = 1 << 1, // Filled shapes have anti-aliased edges (*2 the number of vertices) + ImDrawListFlags_AllowVtxOffset = 1 << 2 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. +}; + +// Draw command list +// This is the low-level list of polygons that ImGui:: functions are filling. At the end of the frame, +// all command lists are passed to your ImGuiIO::RenderDrawListFn function for rendering. +// Each dear imgui window contains its own ImDrawList. You can use ImGui::GetWindowDrawList() to +// access the current window draw list and draw custom primitives. +// You can interleave normal ImGui:: calls and adding primitives to the current draw list. +// All positions are generally in pixel coordinates (top-left at (0,0), bottom-right at io.DisplaySize), but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well) +// Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects. +struct ImDrawList +{ + // This is what you have to render + ImVector CmdBuffer; // Draw commands. Typically 1 command = 1 GPU draw call, unless the command is a callback. + ImVector IdxBuffer; // Index buffer. Each command consume ImDrawCmd::ElemCount of those + ImVector VtxBuffer; // Vertex buffer. + ImDrawListFlags Flags; // Flags, you may poke into these to adjust anti-aliasing settings per-primitive. + + // [Internal, used while building lists] + const ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) + const char* _OwnerName; // Pointer to owner window's name for debugging + unsigned int _VtxCurrentOffset; // [Internal] Always 0 unless 'Flags & ImDrawListFlags_AllowVtxOffset'. + unsigned int _VtxCurrentIdx; // [Internal] Generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. + ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) + ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) + ImVector _ClipRectStack; // [Internal] + ImVector _TextureIdStack; // [Internal] + ImVector _Path; // [Internal] current path building + ImDrawListSplitter _Splitter; // [Internal] for channels api + + // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) + ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; _OwnerName = NULL; Clear(); } + ~ImDrawList() { ClearFreeMemory(); } + IMGUI_API void PushClipRect(ImVec2 clip_rect_min, ImVec2 clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) + IMGUI_API void PushClipRectFullScreen(); + IMGUI_API void PopClipRect(); + IMGUI_API void PushTextureID(ImTextureID texture_id); + IMGUI_API void PopTextureID(); + inline ImVec2 GetClipRectMin() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.x, cr.y); } + inline ImVec2 GetClipRectMax() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.z, cr.w); } + + // Primitives + // - For rectangular primitives, "p_min" and "p_max" represent the upper-left and lower-right corners. + IMGUI_API void AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness = 1.0f); + IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size), rounding_corners_flags: 4-bits corresponding to which corner to round + IMGUI_API void AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); // a: upper-left, b: lower-right (== upper-left + size) + IMGUI_API void AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left); + IMGUI_API void AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness = 1.0f); + IMGUI_API void AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col); + IMGUI_API void AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness = 1.0f); + IMGUI_API void AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col); + IMGUI_API void AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments = 12, float thickness = 1.0f); + IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 12); + IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); + IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); + IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness); + IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. + IMGUI_API void AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments = 0); + + // Image primitives + // - Read FAQ to understand what ImTextureID is. + // - "p_min" and "p_max" represent the upper-left and lower-right corners of the rectangle. + // - "uv_min" and "uv_max" represent the normalized texture coordinates to use for those corners. Using (0,0)->(1,1) texture coordinates will generally display the entire texture. + IMGUI_API void AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min = ImVec2(0, 0), const ImVec2& uv_max = ImVec2(1, 1), ImU32 col = IM_COL32_WHITE); + IMGUI_API void AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1 = ImVec2(0, 0), const ImVec2& uv2 = ImVec2(1, 0), const ImVec2& uv3 = ImVec2(1, 1), const ImVec2& uv4 = ImVec2(0, 1), ImU32 col = IM_COL32_WHITE); + IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); + + // Stateful path API, add points then finish with PathFillConvex() or PathStroke() + inline void PathClear() { _Path.Size = 0; } + inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } + inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size-1], &pos, 8) != 0) _Path.push_back(pos); } + inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } // Note: Anti-aliased filling requires points to be in clockwise order. + inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } + IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 10); + IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle + IMGUI_API void PathBezierCurveTo(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, int num_segments = 0); + IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); + + // Advanced + IMGUI_API void AddCallback(ImDrawCallback callback, void* callback_data); // Your rendering function must check for 'UserCallback' in ImDrawCmd and call the function instead of rendering triangles. + IMGUI_API void AddDrawCmd(); // This is useful if you need to forcefully create a new draw call (to allow for dependent rendering / blending). Otherwise primitives are merged into the same draw-call as much as possible + IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer. + + // Advanced: Channels + // - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit foreground primitives before background primitives) + // - Use to minimize draw calls (e.g. if going back-and-forth between multiple non-overlapping clipping rectangles, prefer to append into separate channels then merge at the end) + inline void ChannelsSplit(int count) { _Splitter.Split(this, count); } + inline void ChannelsMerge() { _Splitter.Merge(this); } + inline void ChannelsSetCurrent(int n) { _Splitter.SetCurrentChannel(this, n); } + + // Internal helpers + // NB: all primitives needs to be reserved via PrimReserve() beforehand! + IMGUI_API void Clear(); + IMGUI_API void ClearFreeMemory(); + IMGUI_API void PrimReserve(int idx_count, int vtx_count); + IMGUI_API void PrimRect(const ImVec2& a, const ImVec2& b, ImU32 col); // Axis aligned rectangle (composed of two triangles) + IMGUI_API void PrimRectUV(const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col); + IMGUI_API void PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col); + inline void PrimWriteVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col){ _VtxWritePtr->pos = pos; _VtxWritePtr->uv = uv; _VtxWritePtr->col = col; _VtxWritePtr++; _VtxCurrentIdx++; } + inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } + inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } + IMGUI_API void UpdateClipRect(); + IMGUI_API void UpdateTextureID(); +}; + +// All draw data to render a Dear ImGui frame +// (NB: the style and the naming convention here is a little inconsistent, we currently preserve them for backward compatibility purpose, +// as this is one of the oldest structure exposed by the library! Basically, ImDrawList == CmdList) +struct ImDrawData +{ + bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. + ImDrawList** CmdLists; // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here. + int CmdListsCount; // Number of ImDrawList* to render + int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size + int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size + ImVec2 DisplayPos; // Upper-left position of the viewport to render (== upper-left of the orthogonal projection matrix to use) + ImVec2 DisplaySize; // Size of the viewport to render (== io.DisplaySize for the main viewport) (DisplayPos + DisplaySize == lower-right of the orthogonal projection matrix to use) + ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display. + + // Functions + ImDrawData() { Valid = false; Clear(); } + ~ImDrawData() { Clear(); } + void Clear() { Valid = false; CmdLists = NULL; CmdListsCount = TotalVtxCount = TotalIdxCount = 0; DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.f, 0.f); } // The ImDrawList are owned by ImGuiContext! + IMGUI_API void DeIndexAllBuffers(); // Helper to convert all buffers from indexed to non-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! + IMGUI_API void ScaleClipRects(const ImVec2& fb_scale); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than Dear ImGui expects, or if there is a difference between your window resolution and framebuffer resolution. +}; + +//----------------------------------------------------------------------------- +// Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont) +//----------------------------------------------------------------------------- + +struct ImFontConfig +{ + void* FontData; // // TTF/OTF data + int FontDataSize; // // TTF/OTF data size + bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself). + int FontNo; // 0 // Index of font within TTF/OTF file + float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height). + int OversampleH; // 3 // Rasterize at higher quality for sub-pixel positioning. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. + int OversampleV; // 1 // Rasterize at higher quality for sub-pixel positioning. We don't use sub-pixel positions on the Y axis. + bool PixelSnapH; // false // Align every glyph to pixel boundary. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1. + ImVec2 GlyphExtraSpacing; // 0, 0 // Extra spacing (in pixels) between glyphs. Only X axis is supported for now. + ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input. + const ImWchar* GlyphRanges; // NULL // Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. + float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font + float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs + bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights. + unsigned int RasterizerFlags; // 0x00 // Settings for custom font rasterizer (e.g. ImGuiFreeType). Leave as zero if you aren't using one. + float RasterizerMultiply; // 1.0f // Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. + ImWchar EllipsisChar; // -1 // Explicitly specify unicode codepoint of ellipsis character. When fonts are being merged first specified ellipsis will be used. + + // [Internal] + char Name[40]; // Name (strictly to ease debugging) + ImFont* DstFont; + + IMGUI_API ImFontConfig(); +}; + +struct ImFontGlyph +{ + ImWchar Codepoint; // 0x0000..0xFFFF + float AdvanceX; // Distance to next character (= data from font + ImFontConfig::GlyphExtraSpacing.x baked in) + float X0, Y0, X1, Y1; // Glyph corners + float U0, V0, U1, V1; // Texture coordinates +}; + +// Helper to build glyph ranges from text/string data. Feed your application strings/characters to it then call BuildRanges(). +// This is essentially a tightly packed of vector of 64k booleans = 8KB storage. +struct ImFontGlyphRangesBuilder +{ + ImVector UsedChars; // Store 1-bit per Unicode code point (0=unused, 1=used) + + ImFontGlyphRangesBuilder() { Clear(); } + inline void Clear() { int size_in_bytes = 0x10000 / 8; UsedChars.resize(size_in_bytes / (int)sizeof(ImU32)); memset(UsedChars.Data, 0, (size_t)size_in_bytes); } + inline bool GetBit(int n) const { int off = (n >> 5); ImU32 mask = 1u << (n & 31); return (UsedChars[off] & mask) != 0; } // Get bit n in the array + inline void SetBit(int n) { int off = (n >> 5); ImU32 mask = 1u << (n & 31); UsedChars[off] |= mask; } // Set bit n in the array + inline void AddChar(ImWchar c) { SetBit(c); } // Add character + IMGUI_API void AddText(const char* text, const char* text_end = NULL); // Add string (each character of the UTF-8 string are added) + IMGUI_API void AddRanges(const ImWchar* ranges); // Add ranges, e.g. builder.AddRanges(ImFontAtlas::GetGlyphRangesDefault()) to force add all of ASCII/Latin+Ext + IMGUI_API void BuildRanges(ImVector* out_ranges); // Output new ranges +}; + +// See ImFontAtlas::AddCustomRectXXX functions. +struct ImFontAtlasCustomRect +{ + unsigned int ID; // Input // User ID. Use <0x10000 to map into a font glyph, >=0x10000 for other/internal/custom texture data. + unsigned short Width, Height; // Input // Desired rectangle dimension + unsigned short X, Y; // Output // Packed position in Atlas + float GlyphAdvanceX; // Input // For custom font glyphs only (ID<0x10000): glyph xadvance + ImVec2 GlyphOffset; // Input // For custom font glyphs only (ID<0x10000): glyph display offset + ImFont* Font; // Input // For custom font glyphs only (ID<0x10000): target font + ImFontAtlasCustomRect() { ID = 0xFFFFFFFF; Width = Height = 0; X = Y = 0xFFFF; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0,0); Font = NULL; } + bool IsPacked() const { return X != 0xFFFF; } +}; + +enum ImFontAtlasFlags_ +{ + ImFontAtlasFlags_None = 0, + ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0, // Don't round the height to next power of two + ImFontAtlasFlags_NoMouseCursors = 1 << 1 // Don't build software mouse cursors into the atlas +}; + +// Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding: +// - One or more fonts. +// - Custom graphics data needed to render the shapes needed by Dear ImGui. +// - Mouse cursor shapes for software cursor rendering (unless setting 'Flags |= ImFontAtlasFlags_NoMouseCursors' in the font atlas). +// It is the user-code responsibility to setup/build the atlas, then upload the pixel data into a texture accessible by your graphics api. +// - Optionally, call any of the AddFont*** functions. If you don't call any, the default font embedded in the code will be loaded for you. +// - Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data. +// - Upload the pixels data into a texture within your graphics system (see imgui_impl_xxxx.cpp examples) +// - Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture in a format natural to your graphics API. +// This value will be passed back to you during rendering to identify the texture. Read FAQ entry about ImTextureID for more details. +// Common pitfalls: +// - If you pass a 'glyph_ranges' array to AddFont*** functions, you need to make sure that your array persist up until the +// atlas is build (when calling GetTexData*** or Build()). We only copy the pointer, not the data. +// - Important: By default, AddFontFromMemoryTTF() takes ownership of the data. Even though we are not writing to it, we will free the pointer on destruction. +// You can set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed, +// - Even though many functions are suffixed with "TTF", OTF data is supported just as well. +// - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future! +struct ImFontAtlas +{ + IMGUI_API ImFontAtlas(); + IMGUI_API ~ImFontAtlas(); + IMGUI_API ImFont* AddFont(const ImFontConfig* font_cfg); + IMGUI_API ImFont* AddFontDefault(const ImFontConfig* font_cfg = NULL); + IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); + IMGUI_API ImFont* AddFontFromMemoryTTF(void* font_data, int font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed. + IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp. + IMGUI_API ImFont* AddFontFromMemoryCompressedBase85TTF(const char* compressed_font_data_base85, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 parameter. + IMGUI_API void ClearInputData(); // Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts. + IMGUI_API void ClearTexData(); // Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory. + IMGUI_API void ClearFonts(); // Clear output font data (glyphs storage, UV coordinates). + IMGUI_API void Clear(); // Clear all input and output. + + // Build atlas, retrieve pixel data. + // User is in charge of copying the pixels into graphics memory (e.g. create a texture with your engine). Then store your texture handle with SetTexID(). + // The pitch is always = Width * BytesPerPixels (1 or 4) + // Building in RGBA32 format is provided for convenience and compatibility, but note that unless you manually manipulate or copy color data into + // the texture (e.g. when using the AddCustomRect*** api), then the RGB pixels emitted will always be white (~75% of memory/bandwidth waste. + IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions. + IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel + IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel + bool IsBuilt() { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); } + void SetTexID(ImTextureID id) { TexID = id; } + + //------------------------------------------- + // Glyph Ranges + //------------------------------------------- + + // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list) + // NB: Make sure that your string are UTF-8 and NOT in your local code page. In C++11, you can create UTF-8 string literal using the u8"Hello world" syntax. See FAQ for details. + // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data. + IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin + IMGUI_API const ImWchar* GetGlyphRangesKorean(); // Default + Korean characters + IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs + IMGUI_API const ImWchar* GetGlyphRangesChineseFull(); // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs + IMGUI_API const ImWchar* GetGlyphRangesChineseSimplifiedCommon();// Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK Unified Ideographs for common simplified Chinese + IMGUI_API const ImWchar* GetGlyphRangesCyrillic(); // Default + about 400 Cyrillic characters + IMGUI_API const ImWchar* GetGlyphRangesThai(); // Default + Thai characters + IMGUI_API const ImWchar* GetGlyphRangesVietnamese(); // Default + Vietnamese characters + + //------------------------------------------- + // [BETA] Custom Rectangles/Glyphs API + //------------------------------------------- + + // You can request arbitrary rectangles to be packed into the atlas, for your own purposes. + // After calling Build(), you can query the rectangle position and render your pixels. + // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), + // so you can render e.g. custom colorful icons and use them as regular glyphs. + // Read misc/fonts/README.txt for more details about using colorful icons. + IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList + IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x10000 to register a rectangle to map into a specific font. + const ImFontAtlasCustomRect*GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } + + // [Internal] + IMGUI_API void CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max); + IMGUI_API bool GetMouseCursorTexData(ImGuiMouseCursor cursor, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]); + + //------------------------------------------- + // Members + //------------------------------------------- + + bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert. + ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_) + ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure. + int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height. + int TexGlyphPadding; // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0. + + // [Internal] + // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you. + unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight + unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4 + int TexWidth; // Texture width calculated during Build(). + int TexHeight; // Texture height calculated during Build(). + ImVec2 TexUvScale; // = (1.0f/TexWidth, 1.0f/TexHeight) + ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel + ImVector Fonts; // Hold all the fonts returned by AddFont*. Fonts[0] is the default font upon calling ImGui::NewFrame(), use ImGui::PushFont()/PopFont() to change the current font. + ImVector CustomRects; // Rectangles for packing custom texture data into the atlas. + ImVector ConfigData; // Internal data + int CustomRectIds[1]; // Identifiers of custom texture rectangle used by ImFontAtlas/ImDrawList + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+ + typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+ +#endif +}; + +// Font runtime data and rendering +// ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32(). +struct ImFont +{ + // Members: Hot ~20/24 bytes (for CalcTextSize) + ImVector IndexAdvanceX; // 12-16 // out // // Sparse. Glyphs->AdvanceX in a directly indexable way (cache-friendly for CalcTextSize functions which only this this info, and are often bottleneck in large UI). + float FallbackAdvanceX; // 4 // out // = FallbackGlyph->AdvanceX + float FontSize; // 4 // in // // Height of characters/line, set during loading (don't change after loading) + + // Members: Hot ~36/48 bytes (for CalcTextSize + render loop) + ImVector IndexLookup; // 12-16 // out // // Sparse. Index glyphs by Unicode code-point. + ImVector Glyphs; // 12-16 // out // // All glyphs. + const ImFontGlyph* FallbackGlyph; // 4-8 // out // = FindGlyph(FontFallbackChar) + ImVec2 DisplayOffset; // 8 // in // = (0,0) // Offset font rendering by xx pixels + + // Members: Cold ~32/40 bytes + ImFontAtlas* ContainerAtlas; // 4-8 // out // // What we has been loaded into + const ImFontConfig* ConfigData; // 4-8 // in // // Pointer within ContainerAtlas->ConfigData + short ConfigDataCount; // 2 // in // ~ 1 // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont. + ImWchar FallbackChar; // 2 // in // = '?' // Replacement character if a glyph isn't found. Only set via SetFallbackChar() + ImWchar EllipsisChar; // 2 // out // = -1 // Character used for ellipsis rendering. + float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() + float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] + int MetricsTotalSurface;// 4 // out // // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs) + bool DirtyLookupTables; // 1 // out // + + // Methods + IMGUI_API ImFont(); + IMGUI_API ~ImFont(); + IMGUI_API const ImFontGlyph*FindGlyph(ImWchar c) const; + IMGUI_API const ImFontGlyph*FindGlyphNoFallback(ImWchar c) const; + float GetCharAdvance(ImWchar c) const { return ((int)c < IndexAdvanceX.Size) ? IndexAdvanceX[(int)c] : FallbackAdvanceX; } + bool IsLoaded() const { return ContainerAtlas != NULL; } + const char* GetDebugName() const { return ConfigData ? ConfigData->Name : ""; } + + // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable. + // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable. + IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8 + IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const; + IMGUI_API void RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const; + IMGUI_API void RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const; + + // [Internal] Don't use! + IMGUI_API void BuildLookupTable(); + IMGUI_API void ClearOutputData(); + IMGUI_API void GrowIndex(int new_size); + IMGUI_API void AddGlyph(ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x); + IMGUI_API void AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built. + IMGUI_API void SetFallbackChar(ImWchar c); + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + typedef ImFontGlyph Glyph; // OBSOLETED in 1.52+ +#endif +}; + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + +// Include imgui_user.h at the end of imgui.h (convenient for user to only explicitly include vanilla imgui.h) +#ifdef IMGUI_INCLUDE_IMGUI_USER_H +#include "imgui_user.h" +#endif diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp new file mode 100644 index 00000000..0eeec980 --- /dev/null +++ b/imgui/imgui_demo.cpp @@ -0,0 +1,4814 @@ +// dear imgui, v1.74 WIP +// (demo code) + +// Message to the person tempted to delete this file when integrating Dear ImGui into their code base: +// Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other coders +// will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of +// your game/app! Removing this file from your project is hindering access to documentation for everyone in your team, +// likely leading you to poorer usage of the library. +// Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow(). +// If you want to link core Dear ImGui in your shipped builds but want an easy guarantee that the demo will not be linked, +// you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. +// In other situation, whenever you have Dear ImGui available you probably want this to be available for reference. +// Thank you, +// -Your beloved friend, imgui_demo.cpp (that you won't delete) + +// Message to beginner C/C++ programmers about the meaning of the 'static' keyword: +// In this demo code, we frequently we use 'static' variables inside functions. A static variable persist across calls, so it is +// essentially like a global variable but declared inside the scope of the function. We do this as a way to gather code and data +// in the same place, to make the demo source code faster to read, faster to write, and smaller in size. +// It also happens to be a convenient way of storing simple UI related information as long as your function doesn't need to be +// reentrant or used in multiple threads. This might be a pattern you will want to use in your code, but most of the real data +// you would be editing is likely going to be stored outside your functions. + +// The Demo code is this file is designed to be easy to copy-and-paste in into your application! +// Because of this: +// - We never omit the ImGui:: namespace when calling functions, even though most of our code is already in the same namespace. +// - We try to declare static variables in the local scope, as close as possible to the code using them. +// - We never use any of the helpers/facilities used internally by dear imgui, unless it has been exposed in the public API (imgui.h). +// - We never use maths operators on ImVec2/ImVec4. For other imgui sources files, they are provided by imgui_internal.h w/ IMGUI_DEFINE_MATH_OPERATORS, +// for your own sources file they are optional and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. +// Because we don't want to assume anything about your support of maths operators, we don't use them in imgui_demo.cpp. + +/* + +Index of this file: + +// [SECTION] Forward Declarations, Helpers +// [SECTION] Demo Window / ShowDemoWindow() +// [SECTION] About Window / ShowAboutWindow() +// [SECTION] Style Editor / ShowStyleEditor() +// [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar() +// [SECTION] Example App: Debug Console / ShowExampleAppConsole() +// [SECTION] Example App: Debug Log / ShowExampleAppLog() +// [SECTION] Example App: Simple Layout / ShowExampleAppLayout() +// [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() +// [SECTION] Example App: Long Text / ShowExampleAppLongText() +// [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize() +// [SECTION] Example App: Constrained Resize / ShowExampleAppConstrainedResize() +// [SECTION] Example App: Simple Overlay / ShowExampleAppSimpleOverlay() +// [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles() +// [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering() +// [SECTION] Example App: Documents Handling / ShowExampleAppDocuments() + +*/ + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#include // toupper +#include // INT_MIN, INT_MAX +#include // sqrtf, powf, cosf, sinf, floorf, ceilf +#include // vsnprintf, sscanf, printf +#include // NULL, malloc, free, atoi +#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier +#include // intptr_t +#else +#include // intptr_t +#endif + +#ifdef _MSC_VER +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#endif +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning : 'xx' is deprecated: The POSIX name for this item.. // for strdup used in demo code (so user can copy & paste the code) +#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' +#pragma clang diagnostic ignored "-Wformat-security" // warning : warning: format string is not a string literal +#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning : declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. +#pragma clang diagnostic ignored "-Wunused-macros" // warning : warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 +#endif +#if __has_warning("-Wdouble-promotion") +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#endif +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning : macro name is a reserved identifier // +#endif +#elif defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size +#pragma GCC diagnostic ignored "-Wformat-security" // warning : format string is not a string literal (potentially insecure) +#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function +#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value +#pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. +#endif + +// Play it nice with Windows users. Notepad in 2017 still doesn't display text data with Unix-style \n. +#ifdef _WIN32 +#define IM_NEWLINE "\r\n" +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#else +#define IM_NEWLINE "\n" +#endif + +//----------------------------------------------------------------------------- +// [SECTION] Forward Declarations, Helpers +//----------------------------------------------------------------------------- + +#if !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && defined(IMGUI_DISABLE_TEST_WINDOWS) && !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Obsolete name since 1.53, TEST->DEMO +#define IMGUI_DISABLE_DEMO_WINDOWS +#endif + +#if !defined(IMGUI_DISABLE_DEMO_WINDOWS) + +// Forward Declarations +static void ShowExampleAppDocuments(bool* p_open); +static void ShowExampleAppMainMenuBar(); +static void ShowExampleAppConsole(bool* p_open); +static void ShowExampleAppLog(bool* p_open); +static void ShowExampleAppLayout(bool* p_open); +static void ShowExampleAppPropertyEditor(bool* p_open); +static void ShowExampleAppLongText(bool* p_open); +static void ShowExampleAppAutoResize(bool* p_open); +static void ShowExampleAppConstrainedResize(bool* p_open); +static void ShowExampleAppSimpleOverlay(bool* p_open); +static void ShowExampleAppWindowTitles(bool* p_open); +static void ShowExampleAppCustomRendering(bool* p_open); +static void ShowExampleMenuFile(); + +// Helper to display a little (?) mark which shows a tooltip when hovered. +// In your own code you may want to display an actual icon if you are using a merged icon fonts (see misc/fonts/README.txt) +static void HelpMarker(const char* desc) +{ + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } +} + +// Helper to display basic user controls. +void ImGui::ShowUserGuide() +{ + ImGuiIO& io = ImGui::GetIO(); + ImGui::BulletText("Double-click on title bar to collapse window."); + ImGui::BulletText("Click and drag on lower corner to resize window\n(double-click to auto fit window to its contents)."); + if (io.ConfigWindowsMoveFromTitleBarOnly) + ImGui::BulletText("Click and drag on title bar to move window."); + else + ImGui::BulletText("Click and drag on any empty space to move window."); + ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); + ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); + if (io.FontAllowUserScaling) + ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents."); + ImGui::BulletText("Mouse Wheel to scroll."); + ImGui::BulletText("While editing text:\n"); + ImGui::Indent(); + ImGui::BulletText("Hold SHIFT or use mouse to select text."); + ImGui::BulletText("CTRL+Left/Right to word jump."); + ImGui::BulletText("CTRL+A or double-click to select all."); + ImGui::BulletText("CTRL+X,CTRL+C,CTRL+V to use clipboard."); + ImGui::BulletText("CTRL+Z,CTRL+Y to undo/redo."); + ImGui::BulletText("ESCAPE to revert."); + ImGui::BulletText("You can apply arithmetic operators +,*,/ on numerical values.\nUse +- to subtract."); + ImGui::Unindent(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Demo Window / ShowDemoWindow() +//----------------------------------------------------------------------------- +// - ShowDemoWindowWidgets() +// - ShowDemoWindowLayout() +// - ShowDemoWindowPopups() +// - ShowDemoWindowColumns() +// - ShowDemoWindowMisc() +//----------------------------------------------------------------------------- + +// We split the contents of the big ShowDemoWindow() function into smaller functions (because the link time of very large functions grow non-linearly) +static void ShowDemoWindowWidgets(); +static void ShowDemoWindowLayout(); +static void ShowDemoWindowPopups(); +static void ShowDemoWindowColumns(); +static void ShowDemoWindowMisc(); + +// Demonstrate most Dear ImGui features (this is big function!) +// You may execute this function to experiment with the UI and understand what it does. You may then search for keywords in the code when you are interested by a specific feature. +void ImGui::ShowDemoWindow(bool* p_open) +{ + IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); // Exceptionally add an extra assert here for people confused with initial dear imgui setup + + // Examples Apps (accessible from the "Examples" menu) + static bool show_app_documents = false; + static bool show_app_main_menu_bar = false; + static bool show_app_console = false; + static bool show_app_log = false; + static bool show_app_layout = false; + static bool show_app_property_editor = false; + static bool show_app_long_text = false; + static bool show_app_auto_resize = false; + static bool show_app_constrained_resize = false; + static bool show_app_simple_overlay = false; + static bool show_app_window_titles = false; + static bool show_app_custom_rendering = false; + + if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); + if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); + if (show_app_console) ShowExampleAppConsole(&show_app_console); + if (show_app_log) ShowExampleAppLog(&show_app_log); + if (show_app_layout) ShowExampleAppLayout(&show_app_layout); + if (show_app_property_editor) ShowExampleAppPropertyEditor(&show_app_property_editor); + if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text); + if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize); + if (show_app_constrained_resize) ShowExampleAppConstrainedResize(&show_app_constrained_resize); + if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay); + if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); + if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); + + // Dear ImGui Apps (accessible from the "Tools" menu) + static bool show_app_metrics = false; + static bool show_app_style_editor = false; + static bool show_app_about = false; + + if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } + if (show_app_style_editor) { ImGui::Begin("Style Editor", &show_app_style_editor); ImGui::ShowStyleEditor(); ImGui::End(); } + if (show_app_about) { ImGui::ShowAboutWindow(&show_app_about); } + + // Demonstrate the various window flags. Typically you would just use the default! + static bool no_titlebar = false; + static bool no_scrollbar = false; + static bool no_menu = false; + static bool no_move = false; + static bool no_resize = false; + static bool no_collapse = false; + static bool no_close = false; + static bool no_nav = false; + static bool no_background = false; + static bool no_bring_to_front = false; + + ImGuiWindowFlags window_flags = 0; + if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar; + if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar; + if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar; + if (no_move) window_flags |= ImGuiWindowFlags_NoMove; + if (no_resize) window_flags |= ImGuiWindowFlags_NoResize; + if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse; + if (no_nav) window_flags |= ImGuiWindowFlags_NoNav; + if (no_background) window_flags |= ImGuiWindowFlags_NoBackground; + if (no_bring_to_front) window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus; + if (no_close) p_open = NULL; // Don't pass our bool* to Begin + + // We specify a default position/size in case there's no data in the .ini file. Typically this isn't required! We only do it to make the Demo applications a little more welcoming. + ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver); + + // Main body of the Demo window starts here. + if (!ImGui::Begin("Dear ImGui Demo", p_open, window_flags)) + { + // Early out if the window is collapsed, as an optimization. + ImGui::End(); + return; + } + + // Most "big" widgets share a common width settings by default. + //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); // Use 2/3 of the space for widgets and 1/3 for labels (default) + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // Use fixed width for labels (by passing a negative value), the rest goes to widgets. We choose a width proportional to our font size. + + // Menu Bar + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Menu")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Examples")) + { + ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar); + ImGui::MenuItem("Console", NULL, &show_app_console); + ImGui::MenuItem("Log", NULL, &show_app_log); + ImGui::MenuItem("Simple layout", NULL, &show_app_layout); + ImGui::MenuItem("Property editor", NULL, &show_app_property_editor); + ImGui::MenuItem("Long text display", NULL, &show_app_long_text); + ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize); + ImGui::MenuItem("Constrained-resizing window", NULL, &show_app_constrained_resize); + ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay); + ImGui::MenuItem("Manipulating window titles", NULL, &show_app_window_titles); + ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering); + ImGui::MenuItem("Documents", NULL, &show_app_documents); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Tools")) + { + ImGui::MenuItem("Metrics", NULL, &show_app_metrics); + ImGui::MenuItem("Style Editor", NULL, &show_app_style_editor); + ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + ImGui::Text("dear imgui says hello. (%s)", IMGUI_VERSION); + ImGui::Spacing(); + + if (ImGui::CollapsingHeader("Help")) + { + ImGui::Text("PROGRAMMER GUIDE:"); + ImGui::BulletText("Please see the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); + ImGui::BulletText("Please see the comments in imgui.cpp."); + ImGui::BulletText("Please see the examples/ application."); + ImGui::BulletText("Enable 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); + ImGui::BulletText("Enable 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); + ImGui::Separator(); + + ImGui::Text("USER GUIDE:"); + ImGui::ShowUserGuide(); + } + + if (ImGui::CollapsingHeader("Configuration")) + { + ImGuiIO& io = ImGui::GetIO(); + + if (ImGui::TreeNode("Configuration##2")) + { + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); + ImGui::SameLine(); HelpMarker("Required back-end to feed in gamepad inputs in io.NavInputs[] and set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); + ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); + ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) // Create a way to restore this flag otherwise we could be stuck completely! + { + if (fmodf((float)ImGui::GetTime(), 0.40f) < 0.20f) + { + ImGui::SameLine(); + ImGui::Text("<>"); + } + if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) + io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse; + } + ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); + ImGui::SameLine(); HelpMarker("Instruct back-end to not alter mouse cursor shape and visibility."); + ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); + ImGui::SameLine(); HelpMarker("Set to false to disable blinking cursor, for users who consider it distracting"); + ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); + ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); + ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); + ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); + ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor for you. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + ImGui::TreePop(); + ImGui::Separator(); + } + + if (ImGui::TreeNode("Backend Flags")) + { + HelpMarker("Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities."); + ImGuiBackendFlags backend_flags = io.BackendFlags; // Make a local copy to avoid modifying actual back-end flags. + ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasGamepad); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasMouseCursors); + ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasSetMousePos); + ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", (unsigned int *)&backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); + ImGui::TreePop(); + ImGui::Separator(); + } + + if (ImGui::TreeNode("Style")) + { + ImGui::ShowStyleEditor(); + ImGui::TreePop(); + ImGui::Separator(); + } + + if (ImGui::TreeNode("Capture/Logging")) + { + ImGui::TextWrapped("The logging API redirects all text output so you can easily capture the content of a window or a block. Tree nodes can be automatically expanded."); + HelpMarker("Try opening any of the contents below in this window and then click one of the \"Log To\" button."); + ImGui::LogButtons(); + ImGui::TextWrapped("You can also call ImGui::LogText() to output directly to the log without a visual output."); + if (ImGui::Button("Copy \"Hello, world!\" to clipboard")) + { + ImGui::LogToClipboard(); + ImGui::LogText("Hello, world!"); + ImGui::LogFinish(); + } + ImGui::TreePop(); + } + } + + if (ImGui::CollapsingHeader("Window options")) + { + ImGui::Checkbox("No titlebar", &no_titlebar); ImGui::SameLine(150); + ImGui::Checkbox("No scrollbar", &no_scrollbar); ImGui::SameLine(300); + ImGui::Checkbox("No menu", &no_menu); + ImGui::Checkbox("No move", &no_move); ImGui::SameLine(150); + ImGui::Checkbox("No resize", &no_resize); ImGui::SameLine(300); + ImGui::Checkbox("No collapse", &no_collapse); + ImGui::Checkbox("No close", &no_close); ImGui::SameLine(150); + ImGui::Checkbox("No nav", &no_nav); ImGui::SameLine(300); + ImGui::Checkbox("No background", &no_background); + ImGui::Checkbox("No bring to front", &no_bring_to_front); + } + + // All demo contents + ShowDemoWindowWidgets(); + ShowDemoWindowLayout(); + ShowDemoWindowPopups(); + ShowDemoWindowColumns(); + ShowDemoWindowMisc(); + + // End of ShowDemoWindow() + ImGui::End(); +} + +static void ShowDemoWindowWidgets() +{ + if (!ImGui::CollapsingHeader("Widgets")) + return; + + if (ImGui::TreeNode("Basic")) + { + static int clicked = 0; + if (ImGui::Button("Button")) + clicked++; + if (clicked & 1) + { + ImGui::SameLine(); + ImGui::Text("Thanks for clicking me!"); + } + + static bool check = true; + ImGui::Checkbox("checkbox", &check); + + static int e = 0; + ImGui::RadioButton("radio a", &e, 0); ImGui::SameLine(); + ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine(); + ImGui::RadioButton("radio c", &e, 2); + + // Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style. + for (int i = 0; i < 7; i++) + { + if (i > 0) + ImGui::SameLine(); + ImGui::PushID(i); + ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(i/7.0f, 0.6f, 0.6f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(i/7.0f, 0.7f, 0.7f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i/7.0f, 0.8f, 0.8f)); + ImGui::Button("Click"); + ImGui::PopStyleColor(3); + ImGui::PopID(); + } + + // Use AlignTextToFramePadding() to align text baseline to the baseline of framed elements (otherwise a Text+SameLine+Button sequence will have the text a little too high by default) + ImGui::AlignTextToFramePadding(); + ImGui::Text("Hold to repeat:"); + ImGui::SameLine(); + + // Arrow buttons with Repeater + static int counter = 0; + float spacing = ImGui::GetStyle().ItemInnerSpacing.x; + ImGui::PushButtonRepeat(true); + if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { counter--; } + ImGui::SameLine(0.0f, spacing); + if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { counter++; } + ImGui::PopButtonRepeat(); + ImGui::SameLine(); + ImGui::Text("%d", counter); + + ImGui::Text("Hover over me"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("I am a tooltip"); + + ImGui::SameLine(); + ImGui::Text("- or me"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::Text("I am a fancy tooltip"); + static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; + ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); + ImGui::EndTooltip(); + } + + ImGui::Separator(); + + ImGui::LabelText("label", "Value"); + + { + // Using the _simplified_ one-liner Combo() api here + // See "Combo" section for examples of how to use the more complete BeginCombo()/EndCombo() api. + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; + static int item_current = 0; + ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); + ImGui::SameLine(); HelpMarker("Refer to the \"Combo\" section below for an explanation of the full BeginCombo/EndCombo API, and demonstration of various flags.\n"); + } + + { + static char str0[128] = "Hello, world!"; + ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0)); + ImGui::SameLine(); HelpMarker("USER:\nHold SHIFT or use mouse to select text.\n" "CTRL+Left/Right to word jump.\n" "CTRL+A or double-click to select all.\n" "CTRL+X,CTRL+C,CTRL+V clipboard.\n" "CTRL+Z,CTRL+Y undo/redo.\n" "ESCAPE to revert.\n\nPROGRAMMER:\nYou can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated in imgui_demo.cpp)."); + + static char str1[128] = ""; + ImGui::InputTextWithHint("input text (w/ hint)", "enter text here", str1, IM_ARRAYSIZE(str1)); + + static int i0 = 123; + ImGui::InputInt("input int", &i0); + ImGui::SameLine(); HelpMarker("You can apply arithmetic operators +,*,/ on numerical values.\n e.g. [ 100 ], input \'*2\', result becomes [ 200 ]\nUse +- to subtract.\n"); + + static float f0 = 0.001f; + ImGui::InputFloat("input float", &f0, 0.01f, 1.0f, "%.3f"); + + static double d0 = 999999.00000001; + ImGui::InputDouble("input double", &d0, 0.01f, 1.0f, "%.8f"); + + static float f1 = 1.e10f; + ImGui::InputFloat("input scientific", &f1, 0.0f, 0.0f, "%e"); + ImGui::SameLine(); HelpMarker("You can input value using the scientific notation,\n e.g. \"1e+8\" becomes \"100000000\".\n"); + + static float vec4a[4] = { 0.10f, 0.20f, 0.30f, 0.44f }; + ImGui::InputFloat3("input float3", vec4a); + } + + { + static int i1 = 50, i2 = 42; + ImGui::DragInt("drag int", &i1, 1); + ImGui::SameLine(); HelpMarker("Click and drag to edit value.\nHold SHIFT/ALT for faster/slower edit.\nDouble-click or CTRL+click to input value."); + + ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%"); + + static float f1=1.00f, f2=0.0067f; + ImGui::DragFloat("drag float", &f1, 0.005f); + ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns"); + } + + { + static int i1=0; + ImGui::SliderInt("slider int", &i1, -1, 3); + ImGui::SameLine(); HelpMarker("CTRL+click to input value."); + + static float f1=0.123f, f2=0.0f; + ImGui::SliderFloat("slider float", &f1, 0.0f, 1.0f, "ratio = %.3f"); + ImGui::SliderFloat("slider float (curve)", &f2, -10.0f, 10.0f, "%.4f", 2.0f); + + static float angle = 0.0f; + ImGui::SliderAngle("slider angle", &angle); + + // Using the format string to display a name instead of an integer. + // Here we completely omit '%d' from the format string, so it'll only display a name. + // This technique can also be used with DragInt(). + enum Element { Element_Fire, Element_Earth, Element_Air, Element_Water, Element_COUNT }; + const char* element_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" }; + static int current_element = Element_Fire; + const char* current_element_name = (current_element >= 0 && current_element < Element_COUNT) ? element_names[current_element] : "Unknown"; + ImGui::SliderInt("slider enum", ¤t_element, 0, Element_COUNT - 1, current_element_name); + ImGui::SameLine(); HelpMarker("Using the format string parameter to display a name instead of the underlying integer."); + } + + { + static float col1[3] = { 1.0f,0.0f,0.2f }; + static float col2[4] = { 0.4f,0.7f,0.0f,0.5f }; + ImGui::ColorEdit3("color 1", col1); + ImGui::SameLine(); HelpMarker("Click on the colored square to open a color picker.\nClick and hold to use drag and drop.\nRight-click on the colored square to show options.\nCTRL+click on individual component to input value.\n"); + + ImGui::ColorEdit4("color 2", col2); + } + + { + // List box + const char* listbox_items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; + static int listbox_item_current = 1; + ImGui::ListBox("listbox\n(single select)", &listbox_item_current, listbox_items, IM_ARRAYSIZE(listbox_items), 4); + + //static int listbox_item_current2 = 2; + //ImGui::SetNextItemWidth(-1); + //ImGui::ListBox("##listbox2", &listbox_item_current2, listbox_items, IM_ARRAYSIZE(listbox_items), 4); + } + + ImGui::TreePop(); + } + + // Testing ImGuiOnceUponAFrame helper. + //static ImGuiOnceUponAFrame once; + //for (int i = 0; i < 5; i++) + // if (once) + // ImGui::Text("This will be displayed only once."); + + if (ImGui::TreeNode("Trees")) + { + if (ImGui::TreeNode("Basic trees")) + { + for (int i = 0; i < 5; i++) + { + // Use SetNextItemOpen() so set the default state of a node to be open. + // We could also use TreeNodeEx() with the ImGuiTreeNodeFlags_DefaultOpen flag to achieve the same thing! + if (i == 0) + ImGui::SetNextItemOpen(true, ImGuiCond_Once); + + if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i)) + { + ImGui::Text("blah blah"); + ImGui::SameLine(); + if (ImGui::SmallButton("button")) {}; + ImGui::TreePop(); + } + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Advanced, with Selectable nodes")) + { + HelpMarker("This is a more typical looking tree with selectable nodes.\nClick to select, CTRL+Click to toggle, click on arrows or double-click to open."); + static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth; + static bool align_label_with_current_x_position = false; + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnArrow); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::Checkbox("Align label with current X position)", &align_label_with_current_x_position); + ImGui::Text("Hello!"); + if (align_label_with_current_x_position) + ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing()); + + static int selection_mask = (1 << 2); // Dumb representation of what may be user-side selection state. You may carry selection state inside or outside your objects in whatever format you see fit. + int node_clicked = -1; // Temporary storage of what node we have clicked to process selection at the end of the loop. May be a pointer to your own node type, etc. + for (int i = 0; i < 6; i++) + { + // Disable the default open on single-click behavior and pass in Selected flag according to our selection state. + ImGuiTreeNodeFlags node_flags = base_flags; + const bool is_selected = (selection_mask & (1 << i)) != 0; + if (is_selected) + node_flags |= ImGuiTreeNodeFlags_Selected; + if (i < 3) + { + // Items 0..2 are Tree Node + bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i); + if (ImGui::IsItemClicked()) + node_clicked = i; + if (node_open) + { + ImGui::BulletText("Blah blah\nBlah Blah"); + ImGui::TreePop(); + } + } + else + { + // Items 3..5 are Tree Leaves + // The only reason we use TreeNode at all is to allow selection of the leaf. + // Otherwise we can use BulletText() or advance the cursor by GetTreeNodeToLabelSpacing() and call Text(). + node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet + ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Leaf %d", i); + if (ImGui::IsItemClicked()) + node_clicked = i; + } + } + if (node_clicked != -1) + { + // Update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame. + if (ImGui::GetIO().KeyCtrl) + selection_mask ^= (1 << node_clicked); // CTRL+click to toggle + else //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, this commented bit preserve selection when clicking on item that is part of the selection + selection_mask = (1 << node_clicked); // Click to single-select + } + if (align_label_with_current_x_position) + ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing()); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Collapsing Headers")) + { + static bool closable_group = true; + ImGui::Checkbox("Show 2nd header", &closable_group); + if (ImGui::CollapsingHeader("Header", ImGuiTreeNodeFlags_None)) + { + ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered()); + for (int i = 0; i < 5; i++) + ImGui::Text("Some content %d", i); + } + if (ImGui::CollapsingHeader("Header with a close button", &closable_group)) + { + ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered()); + for (int i = 0; i < 5; i++) + ImGui::Text("More content %d", i); + } + /* + if (ImGui::CollapsingHeader("Header with a bullet", ImGuiTreeNodeFlags_Bullet)) + ImGui::Text("IsItemHovered: %d", ImGui::IsItemHovered()); + */ + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Bullets")) + { + ImGui::BulletText("Bullet point 1"); + ImGui::BulletText("Bullet point 2\nOn multiple lines"); + if (ImGui::TreeNode("Tree node")) + { + ImGui::BulletText("Another bullet point"); + ImGui::TreePop(); + } + ImGui::Bullet(); ImGui::Text("Bullet point 3 (two calls)"); + ImGui::Bullet(); ImGui::SmallButton("Button"); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Text")) + { + if (ImGui::TreeNode("Colored Text")) + { + // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility. + ImGui::TextColored(ImVec4(1.0f,0.0f,1.0f,1.0f), "Pink"); + ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "Yellow"); + ImGui::TextDisabled("Disabled"); + ImGui::SameLine(); HelpMarker("The TextDisabled color is stored in ImGuiStyle."); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Word Wrapping")) + { + // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility. + ImGui::TextWrapped("This text should automatically wrap on the edge of the window. The current implementation for text wrapping follows simple rules suitable for English and possibly other languages."); + ImGui::Spacing(); + + static float wrap_width = 200.0f; + ImGui::SliderFloat("Wrap width", &wrap_width, -20, 600, "%.0f"); + + ImGui::Text("Test paragraph 1:"); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), IM_COL32(255,0,255,255)); + ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); + ImGui::Text("The lazy dog is a good dog. This paragraph is made to fit within %.0f pixels. Testing a 1 character word. The quick brown fox jumps over the lazy dog.", wrap_width); + ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255,255,0,255)); + ImGui::PopTextWrapPos(); + + ImGui::Text("Test paragraph 2:"); + pos = ImGui::GetCursorScreenPos(); + ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), IM_COL32(255,0,255,255)); + ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); + ImGui::Text("aaaaaaaa bbbbbbbb, c cccccccc,dddddddd. d eeeeeeee ffffffff. gggggggg!hhhhhhhh"); + ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255,255,0,255)); + ImGui::PopTextWrapPos(); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("UTF-8 Text")) + { + // UTF-8 test with Japanese characters + // (Needs a suitable font, try Noto, or Arial Unicode, or M+ fonts. Read misc/fonts/README.txt for details.) + // - From C++11 you can use the u8"my text" syntax to encode literal strings as UTF-8 + // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. Visual Studio save your file as 'UTF-8 without signature') + // - FOR THIS DEMO FILE ONLY, BECAUSE WE WANT TO SUPPORT OLD COMPILERS, WE ARE *NOT* INCLUDING RAW UTF-8 CHARACTERS IN THIS SOURCE FILE. + // Instead we are encoding a few strings with hexadecimal constants. Don't do this in your application! + // Please use u8"text in any language" in your application! + // Note that characters values are preserved even by InputText() if the font cannot be displayed, so you can safely copy & paste garbled characters into another application. + ImGui::TextWrapped("CJK text will only appears if the font was loaded with the appropriate CJK character ranges. Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. Read misc/fonts/README.txt for details."); + ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. + ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); + static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; + //static char buf[32] = u8"NIHONGO"; // <- this is how you would write it with C++11, using real kanjis + ImGui::InputText("UTF-8 input", buf, IM_ARRAYSIZE(buf)); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Images")) + { + ImGuiIO& io = ImGui::GetIO(); + ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!"); + + // Here we are grabbing the font texture because that's the only one we have access to inside the demo code. + // Remember that ImTextureID is just storage for whatever you want it to be, it is essentially a value that will be passed to the render function inside the ImDrawCmd structure. + // If you use one of the default imgui_impl_XXXX.cpp renderer, they all have comments at the top of their file to specify what they expect to be stored in ImTextureID. + // (for example, the imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer. The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier etc.) + // If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers to ImGui::Image(), and gather width/height through your own functions, etc. + // Using ShowMetricsWindow() as a "debugger" to inspect the draw data that are being passed to your render will help you debug issues if you are confused about this. + // Consider using the lower-level ImDrawList::AddImage() API, via ImGui::GetWindowDrawList()->AddImage(). + ImTextureID my_tex_id = io.Fonts->TexID; + float my_tex_w = (float)io.Fonts->TexWidth; + float my_tex_h = (float)io.Fonts->TexHeight; + + ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), ImVec2(0,0), ImVec2(1,1), ImVec4(1.0f,1.0f,1.0f,1.0f), ImVec4(1.0f,1.0f,1.0f,0.5f)); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + float region_sz = 32.0f; + float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; if (region_x < 0.0f) region_x = 0.0f; else if (region_x > my_tex_w - region_sz) region_x = my_tex_w - region_sz; + float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; if (region_y < 0.0f) region_y = 0.0f; else if (region_y > my_tex_h - region_sz) region_y = my_tex_h - region_sz; + float zoom = 4.0f; + ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y); + ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz); + ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h); + ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h); + ImGui::Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImVec4(1.0f, 1.0f, 1.0f, 1.0f), ImVec4(1.0f, 1.0f, 1.0f, 0.5f)); + ImGui::EndTooltip(); + } + ImGui::TextWrapped("And now some textured buttons.."); + static int pressed_count = 0; + for (int i = 0; i < 8; i++) + { + ImGui::PushID(i); + int frame_padding = -1 + i; // -1 = uses default padding + if (ImGui::ImageButton(my_tex_id, ImVec2(32,32), ImVec2(0,0), ImVec2(32.0f/my_tex_w,32/my_tex_h), frame_padding, ImVec4(0.0f,0.0f,0.0f,1.0f))) + pressed_count += 1; + ImGui::PopID(); + ImGui::SameLine(); + } + ImGui::NewLine(); + ImGui::Text("Pressed %d times.", pressed_count); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Combo")) + { + // Expose flags as checkbox for the demo + static ImGuiComboFlags flags = 0; + ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", (unsigned int*)&flags, ImGuiComboFlags_PopupAlignLeft); + ImGui::SameLine(); HelpMarker("Only makes a difference if the popup is larger than the combo"); + if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", (unsigned int*)&flags, ImGuiComboFlags_NoArrowButton)) + flags &= ~ImGuiComboFlags_NoPreview; // Clear the other flag, as we cannot combine both + if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", (unsigned int*)&flags, ImGuiComboFlags_NoPreview)) + flags &= ~ImGuiComboFlags_NoArrowButton; // Clear the other flag, as we cannot combine both + + // General BeginCombo() API, you have full control over your selection data and display type. + // (your selection data could be an index, a pointer to the object, an id for the object, a flag stored in the object itself, etc.) + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; + static const char* item_current = items[0]; // Here our selection is a single pointer stored outside the object. + if (ImGui::BeginCombo("combo 1", item_current, flags)) // The second parameter is the label previewed before opening the combo. + { + for (int n = 0; n < IM_ARRAYSIZE(items); n++) + { + bool is_selected = (item_current == items[n]); + if (ImGui::Selectable(items[n], is_selected)) + item_current = items[n]; + if (is_selected) + ImGui::SetItemDefaultFocus(); // Set the initial focus when opening the combo (scrolling + for keyboard navigation support in the upcoming navigation branch) + } + ImGui::EndCombo(); + } + + // Simplified one-liner Combo() API, using values packed in a single constant string + static int item_current_2 = 0; + ImGui::Combo("combo 2 (one-liner)", &item_current_2, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); + + // Simplified one-liner Combo() using an array of const char* + static int item_current_3 = -1; // If the selection isn't within 0..count, Combo won't display a preview + ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items)); + + // Simplified one-liner Combo() using an accessor function + struct FuncHolder { static bool ItemGetter(void* data, int idx, const char** out_str) { *out_str = ((const char**)data)[idx]; return true; } }; + static int item_current_4 = 0; + ImGui::Combo("combo 4 (function)", &item_current_4, &FuncHolder::ItemGetter, items, IM_ARRAYSIZE(items)); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Selectables")) + { + // Selectable() has 2 overloads: + // - The one taking "bool selected" as a read-only selection information. When Selectable() has been clicked is returns true and you can alter selection state accordingly. + // - The one taking "bool* p_selected" as a read-write selection information (convenient in some cases) + // The earlier is more flexible, as in real application your selection may be stored in a different manner (in flags within objects, as an external list, etc). + if (ImGui::TreeNode("Basic")) + { + static bool selection[5] = { false, true, false, false, false }; + ImGui::Selectable("1. I am selectable", &selection[0]); + ImGui::Selectable("2. I am selectable", &selection[1]); + ImGui::Text("3. I am not selectable"); + ImGui::Selectable("4. I am selectable", &selection[3]); + if (ImGui::Selectable("5. I am double clickable", selection[4], ImGuiSelectableFlags_AllowDoubleClick)) + if (ImGui::IsMouseDoubleClicked(0)) + selection[4] = !selection[4]; + ImGui::TreePop(); + } + if (ImGui::TreeNode("Selection State: Single Selection")) + { + static int selected = -1; + for (int n = 0; n < 5; n++) + { + char buf[32]; + sprintf(buf, "Object %d", n); + if (ImGui::Selectable(buf, selected == n)) + selected = n; + } + ImGui::TreePop(); + } + if (ImGui::TreeNode("Selection State: Multiple Selection")) + { + HelpMarker("Hold CTRL and click to select multiple items."); + static bool selection[5] = { false, false, false, false, false }; + for (int n = 0; n < 5; n++) + { + char buf[32]; + sprintf(buf, "Object %d", n); + if (ImGui::Selectable(buf, selection[n])) + { + if (!ImGui::GetIO().KeyCtrl) // Clear selection when CTRL is not held + memset(selection, 0, sizeof(selection)); + selection[n] ^= 1; + } + } + ImGui::TreePop(); + } + if (ImGui::TreeNode("Rendering more text into the same line")) + { + // Using the Selectable() override that takes "bool* p_selected" parameter and toggle your booleans automatically. + static bool selected[3] = { false, false, false }; + ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); + ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(300); ImGui::Text("12,345 bytes"); + ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); + ImGui::TreePop(); + } + if (ImGui::TreeNode("In columns")) + { + ImGui::Columns(3, NULL, false); + static bool selected[16] = { 0 }; + for (int i = 0; i < 16; i++) + { + char label[32]; sprintf(label, "Item %d", i); + if (ImGui::Selectable(label, &selected[i])) {} + ImGui::NextColumn(); + } + ImGui::Columns(1); + ImGui::TreePop(); + } + if (ImGui::TreeNode("Grid")) + { + static bool selected[4*4] = { true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true }; + for (int i = 0; i < 4*4; i++) + { + ImGui::PushID(i); + if (ImGui::Selectable("Sailor", &selected[i], 0, ImVec2(50,50))) + { + // Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer. The second part of each test is unnecessary. + int x = i % 4; + int y = i / 4; + if (x > 0) { selected[i - 1] ^= 1; } + if (x < 3 && i < 15) { selected[i + 1] ^= 1; } + if (y > 0 && i > 3) { selected[i - 4] ^= 1; } + if (y < 3 && i < 12) { selected[i + 4] ^= 1; } + } + if ((i % 4) < 3) ImGui::SameLine(); + ImGui::PopID(); + } + ImGui::TreePop(); + } + if (ImGui::TreeNode("Alignment")) + { + HelpMarker("Alignment applies when a selectable is larger than its text content.\nBy default, Selectables uses style.SelectableTextAlign but it can be overriden on a per-item basis using PushStyleVar()."); + static bool selected[3*3] = { true, false, true, false, true, false, true, false, true }; + for (int y = 0; y < 3; y++) + { + for (int x = 0; x < 3; x++) + { + ImVec2 alignment = ImVec2((float)x / 2.0f, (float)y / 2.0f); + char name[32]; + sprintf(name, "(%.1f,%.1f)", alignment.x, alignment.y); + if (x > 0) ImGui::SameLine(); + ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, alignment); + ImGui::Selectable(name, &selected[3*y+x], ImGuiSelectableFlags_None, ImVec2(80,80)); + ImGui::PopStyleVar(); + } + } + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Text Input")) + { + if (ImGui::TreeNode("Multi-line Text Input")) + { + // Note: we are using a fixed-sized buffer for simplicity here. See ImGuiInputTextFlags_CallbackResize + // and the code in misc/cpp/imgui_stdlib.h for how to setup InputText() for dynamically resizing strings. + static char text[1024 * 16] = + "/*\n" + " The Pentium F00F bug, shorthand for F0 0F C7 C8,\n" + " the hexadecimal encoding of one offending instruction,\n" + " more formally, the invalid operand with locked CMPXCHG8B\n" + " instruction bug, is a design flaw in the majority of\n" + " Intel Pentium, Pentium MMX, and Pentium OverDrive\n" + " processors (all in the P5 microarchitecture).\n" + "*/\n\n" + "label:\n" + "\tlock cmpxchg8b eax\n"; + + static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput; + HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp)"); + ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", (unsigned int*)&flags, ImGuiInputTextFlags_ReadOnly); + ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", (unsigned int*)&flags, ImGuiInputTextFlags_AllowTabInput); + ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", (unsigned int*)&flags, ImGuiInputTextFlags_CtrlEnterForNewLine); + ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Filtered Text Input")) + { + static char buf1[64] = ""; ImGui::InputText("default", buf1, 64); + static char buf2[64] = ""; ImGui::InputText("decimal", buf2, 64, ImGuiInputTextFlags_CharsDecimal); + static char buf3[64] = ""; ImGui::InputText("hexadecimal", buf3, 64, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase); + static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase); + static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank); + struct TextFilters { static int FilterImGuiLetters(ImGuiInputTextCallbackData* data) { if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar)) return 0; return 1; } }; + static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); + + ImGui::Text("Password input"); + static char bufpass[64] = "password123"; + ImGui::InputText("password", bufpass, 64, ImGuiInputTextFlags_Password | ImGuiInputTextFlags_CharsNoBlank); + ImGui::SameLine(); HelpMarker("Display all characters as '*'.\nDisable clipboard cut and copy.\nDisable logging.\n"); + ImGui::InputTextWithHint("password (w/ hint)", "", bufpass, 64, ImGuiInputTextFlags_Password | ImGuiInputTextFlags_CharsNoBlank); + ImGui::InputText("password (clear)", bufpass, 64, ImGuiInputTextFlags_CharsNoBlank); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Resize Callback")) + { + // If you have a custom string type you would typically create a ImGui::InputText() wrapper than takes your type as input. + // See misc/cpp/imgui_stdlib.h and .cpp for an implementation of this using std::string. + HelpMarker("Demonstrate using ImGuiInputTextFlags_CallbackResize to wire your resizable string type to InputText().\n\nSee misc/cpp/imgui_stdlib.h for an implementation of this for std::string."); + struct Funcs + { + static int MyResizeCallback(ImGuiInputTextCallbackData* data) + { + if (data->EventFlag == ImGuiInputTextFlags_CallbackResize) + { + ImVector* my_str = (ImVector*)data->UserData; + IM_ASSERT(my_str->begin() == data->Buf); + my_str->resize(data->BufSize); // NB: On resizing calls, generally data->BufSize == data->BufTextLen + 1 + data->Buf = my_str->begin(); + } + return 0; + } + + // Tip: Because ImGui:: is a namespace you would typicall add your own function into the namespace in your own source files. + // For example, you may add a function called ImGui::InputText(const char* label, MyString* my_str). + static bool MyInputTextMultiline(const char* label, ImVector* my_str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0) + { + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + return ImGui::InputTextMultiline(label, my_str->begin(), (size_t)my_str->size(), size, flags | ImGuiInputTextFlags_CallbackResize, Funcs::MyResizeCallback, (void*)my_str); + } + }; + + // For this demo we are using ImVector as a string container. + // Note that because we need to store a terminating zero character, our size/capacity are 1 more than usually reported by a typical string class. + static ImVector my_str; + if (my_str.empty()) + my_str.push_back(0); + Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16)); + ImGui::Text("Data: %p\nSize: %d\nCapacity: %d", (void*)my_str.begin(), my_str.size(), my_str.capacity()); + ImGui::TreePop(); + } + + ImGui::TreePop(); + } + + // Plot/Graph widgets are currently fairly limited. + // Consider writing your own plotting widget, or using a third-party one (see "Wiki->Useful Widgets", or github.com/ocornut/imgui/issues/2747) + if (ImGui::TreeNode("Plots Widgets")) + { + static bool animate = true; + ImGui::Checkbox("Animate", &animate); + + static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; + ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); + + // Create a dummy array of contiguous float values to plot + // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float and the sizeof() of your structure in the Stride parameter. + static float values[90] = { 0 }; + static int values_offset = 0; + static double refresh_time = 0.0; + if (!animate || refresh_time == 0.0) + refresh_time = ImGui::GetTime(); + while (refresh_time < ImGui::GetTime()) // Create dummy data at fixed 60 hz rate for the demo + { + static float phase = 0.0f; + values[values_offset] = cosf(phase); + values_offset = (values_offset+1) % IM_ARRAYSIZE(values); + phase += 0.10f*values_offset; + refresh_time += 1.0f/60.0f; + } + + // Plots can display overlay texts + // (in this example, we will display an average value) + { + float average = 0.0f; + for (int n = 0; n < IM_ARRAYSIZE(values); n++) + average += values[n]; + average /= (float)IM_ARRAYSIZE(values); + char overlay[32]; + sprintf(overlay, "avg %f", average); + ImGui::PlotLines("Lines", values, IM_ARRAYSIZE(values), values_offset, overlay, -1.0f, 1.0f, ImVec2(0,80)); + } + ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0,80)); + + // Use functions to generate output + // FIXME: This is rather awkward because current plot API only pass in indices. We probably want an API passing floats and user provide sample rate/count. + struct Funcs + { + static float Sin(void*, int i) { return sinf(i * 0.1f); } + static float Saw(void*, int i) { return (i & 1) ? 1.0f : -1.0f; } + }; + static int func_type = 0, display_count = 70; + ImGui::Separator(); + ImGui::SetNextItemWidth(100); + ImGui::Combo("func", &func_type, "Sin\0Saw\0"); + ImGui::SameLine(); + ImGui::SliderInt("Sample count", &display_count, 1, 400); + float (*func)(void*, int) = (func_type == 0) ? Funcs::Sin : Funcs::Saw; + ImGui::PlotLines("Lines", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0,80)); + ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0,80)); + ImGui::Separator(); + + // Animate a simple progress bar + static float progress = 0.0f, progress_dir = 1.0f; + if (animate) + { + progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime; + if (progress >= +1.1f) { progress = +1.1f; progress_dir *= -1.0f; } + if (progress <= -0.1f) { progress = -0.1f; progress_dir *= -1.0f; } + } + + // Typically we would use ImVec2(-1.0f,0.0f) or ImVec2(-FLT_MIN,0.0f) to use all available width, + // or ImVec2(width,0.0f) for a specified width. ImVec2(0.0f,0.0f) uses ItemWidth. + ImGui::ProgressBar(progress, ImVec2(0.0f,0.0f)); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + ImGui::Text("Progress Bar"); + + float progress_saturated = (progress < 0.0f) ? 0.0f : (progress > 1.0f) ? 1.0f : progress; + char buf[32]; + sprintf(buf, "%d/%d", (int)(progress_saturated*1753), 1753); + ImGui::ProgressBar(progress, ImVec2(0.f,0.f), buf); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Color/Picker Widgets")) + { + static ImVec4 color = ImVec4(114.0f/255.0f, 144.0f/255.0f, 154.0f/255.0f, 200.0f/255.0f); + + static bool alpha_preview = true; + static bool alpha_half_preview = false; + static bool drag_and_drop = true; + static bool options_menu = true; + static bool hdr = false; + ImGui::Checkbox("With Alpha Preview", &alpha_preview); + ImGui::Checkbox("With Half Alpha Preview", &alpha_half_preview); + ImGui::Checkbox("With Drag and Drop", &drag_and_drop); + ImGui::Checkbox("With Options Menu", &options_menu); ImGui::SameLine(); HelpMarker("Right-click on the individual color widget to show options."); + ImGui::Checkbox("With HDR", &hdr); ImGui::SameLine(); HelpMarker("Currently all this does is to lift the 0..1 limits on dragging widgets."); + ImGuiColorEditFlags misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions); + + ImGui::Text("Color widget:"); + ImGui::SameLine(); HelpMarker("Click on the colored square to open a color picker.\nCTRL+click on individual component to input value.\n"); + ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags); + + ImGui::Text("Color widget HSV with Alpha:"); + ImGui::ColorEdit4("MyColor##2", (float*)&color, ImGuiColorEditFlags_DisplayHSV | misc_flags); + + ImGui::Text("Color widget with Float Display:"); + ImGui::ColorEdit4("MyColor##2f", (float*)&color, ImGuiColorEditFlags_Float | misc_flags); + + ImGui::Text("Color button with Picker:"); + ImGui::SameLine(); HelpMarker("With the ImGuiColorEditFlags_NoInputs flag you can hide all the slider/text inputs.\nWith the ImGuiColorEditFlags_NoLabel flag you can pass a non-empty label which will only be used for the tooltip and picker popup."); + ImGui::ColorEdit4("MyColor##3", (float*)&color, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | misc_flags); + + ImGui::Text("Color button with Custom Picker Popup:"); + + // Generate a dummy default palette. The palette will persist and can be edited. + static bool saved_palette_init = true; + static ImVec4 saved_palette[32] = { }; + if (saved_palette_init) + { + for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) + { + ImGui::ColorConvertHSVtoRGB(n / 31.0f, 0.8f, 0.8f, saved_palette[n].x, saved_palette[n].y, saved_palette[n].z); + saved_palette[n].w = 1.0f; // Alpha + } + saved_palette_init = false; + } + + static ImVec4 backup_color; + bool open_popup = ImGui::ColorButton("MyColor##3b", color, misc_flags); + ImGui::SameLine(0, ImGui::GetStyle().ItemInnerSpacing.x); + open_popup |= ImGui::Button("Palette"); + if (open_popup) + { + ImGui::OpenPopup("mypicker"); + backup_color = color; + } + if (ImGui::BeginPopup("mypicker")) + { + ImGui::Text("MY CUSTOM COLOR PICKER WITH AN AMAZING PALETTE!"); + ImGui::Separator(); + ImGui::ColorPicker4("##picker", (float*)&color, misc_flags | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoSmallPreview); + ImGui::SameLine(); + + ImGui::BeginGroup(); // Lock X position + ImGui::Text("Current"); + ImGui::ColorButton("##current", color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60,40)); + ImGui::Text("Previous"); + if (ImGui::ColorButton("##previous", backup_color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60,40))) + color = backup_color; + ImGui::Separator(); + ImGui::Text("Palette"); + for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) + { + ImGui::PushID(n); + if ((n % 8) != 0) + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); + if (ImGui::ColorButton("##palette", saved_palette[n], ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip, ImVec2(20,20))) + color = ImVec4(saved_palette[n].x, saved_palette[n].y, saved_palette[n].z, color.w); // Preserve alpha! + + // Allow user to drop colors into each palette entry + // (Note that ColorButton is already a drag source by default, unless using ImGuiColorEditFlags_NoDragDrop) + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) + memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 3); + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F)) + memcpy((float*)&saved_palette[n], payload->Data, sizeof(float) * 4); + ImGui::EndDragDropTarget(); + } + + ImGui::PopID(); + } + ImGui::EndGroup(); + ImGui::EndPopup(); + } + + ImGui::Text("Color button only:"); + ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags, ImVec2(80,80)); + + ImGui::Text("Color picker:"); + static bool alpha = true; + static bool alpha_bar = true; + static bool side_preview = true; + static bool ref_color = false; + static ImVec4 ref_color_v(1.0f,0.0f,1.0f,0.5f); + static int display_mode = 0; + static int picker_mode = 0; + ImGui::Checkbox("With Alpha", &alpha); + ImGui::Checkbox("With Alpha Bar", &alpha_bar); + ImGui::Checkbox("With Side Preview", &side_preview); + if (side_preview) + { + ImGui::SameLine(); + ImGui::Checkbox("With Ref Color", &ref_color); + if (ref_color) + { + ImGui::SameLine(); + ImGui::ColorEdit4("##RefColor", &ref_color_v.x, ImGuiColorEditFlags_NoInputs | misc_flags); + } + } + ImGui::Combo("Display Mode", &display_mode, "Auto/Current\0None\0RGB Only\0HSV Only\0Hex Only\0"); + ImGui::SameLine(); HelpMarker("ColorEdit defaults to displaying RGB inputs if you don't specify a display mode, but the user can change it with a right-click.\n\nColorPicker defaults to displaying RGB+HSV+Hex if you don't specify a display mode.\n\nYou can change the defaults using SetColorEditOptions()."); + ImGui::Combo("Picker Mode", &picker_mode, "Auto/Current\0Hue bar + SV rect\0Hue wheel + SV triangle\0"); + ImGui::SameLine(); HelpMarker("User can right-click the picker to change mode."); + ImGuiColorEditFlags flags = misc_flags; + if (!alpha) flags |= ImGuiColorEditFlags_NoAlpha; // This is by default if you call ColorPicker3() instead of ColorPicker4() + if (alpha_bar) flags |= ImGuiColorEditFlags_AlphaBar; + if (!side_preview) flags |= ImGuiColorEditFlags_NoSidePreview; + if (picker_mode == 1) flags |= ImGuiColorEditFlags_PickerHueBar; + if (picker_mode == 2) flags |= ImGuiColorEditFlags_PickerHueWheel; + if (display_mode == 1) flags |= ImGuiColorEditFlags_NoInputs; // Disable all RGB/HSV/Hex displays + if (display_mode == 2) flags |= ImGuiColorEditFlags_DisplayRGB; // Override display mode + if (display_mode == 3) flags |= ImGuiColorEditFlags_DisplayHSV; + if (display_mode == 4) flags |= ImGuiColorEditFlags_DisplayHex; + ImGui::ColorPicker4("MyColor##4", (float*)&color, flags, ref_color ? &ref_color_v.x : NULL); + + ImGui::Text("Programmatically set defaults:"); + ImGui::SameLine(); HelpMarker("SetColorEditOptions() is designed to allow you to set boot-time default.\nWe don't have Push/Pop functions because you can force options on a per-widget basis if needed, and the user can change non-forced ones with the options menu.\nWe don't have a getter to avoid encouraging you to persistently save values that aren't forward-compatible."); + if (ImGui::Button("Default: Uint8 + HSV + Hue Bar")) + ImGui::SetColorEditOptions(ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_PickerHueBar); + if (ImGui::Button("Default: Float + HDR + Hue Wheel")) + ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel); + + // HSV encoded support (to avoid RGB<>HSV round trips and singularities when S==0 or V==0) + static ImVec4 color_stored_as_hsv(0.23f, 1.0f, 1.0f, 1.0f); + ImGui::Spacing(); + ImGui::Text("HSV encoded colors"); + ImGui::SameLine(); HelpMarker("By default, colors are given to ColorEdit and ColorPicker in RGB, but ImGuiColorEditFlags_InputHSV allows you to store colors as HSV and pass them to ColorEdit and ColorPicker as HSV. This comes with the added benefit that you can manipulate hue values with the picker even when saturation or value are zero."); + ImGui::Text("Color widget with InputHSV:"); + ImGui::ColorEdit4("HSV shown as RGB##1", (float*)&color_stored_as_hsv, ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); + ImGui::ColorEdit4("HSV shown as HSV##1", (float*)&color_stored_as_hsv, ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); + ImGui::DragFloat4("Raw HSV values", (float*)&color_stored_as_hsv, 0.01f, 0.0f, 1.0f); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Range Widgets")) + { + static float begin = 10, end = 90; + static int begin_i = 100, end_i = 1000; + ImGui::DragFloatRange2("range", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%"); + ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %d units", "Max: %d units"); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Data Types")) + { + // The DragScalar/InputScalar/SliderScalar functions allow various data types: signed/unsigned int/long long and float/double + // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum to pass the type, + // and passing all arguments by address. + // This is the reason the test code below creates local variables to hold "zero" "one" etc. for each types. + // In practice, if you frequently use a given type that is not covered by the normal API entry points, you can wrap it + // yourself inside a 1 line function which can take typed argument as value instead of void*, and then pass their address + // to the generic function. For example: + // bool MySliderU64(const char *label, u64* value, u64 min = 0, u64 max = 0, const char* format = "%lld") + // { + // return SliderScalar(label, ImGuiDataType_U64, value, &min, &max, format); + // } + + // Limits (as helper variables that we can take the address of) + // Note that the SliderScalar function has a maximum usable range of half the natural type maximum, hence the /2 below. + #ifndef LLONG_MIN + ImS64 LLONG_MIN = -9223372036854775807LL - 1; + ImS64 LLONG_MAX = 9223372036854775807LL; + ImU64 ULLONG_MAX = (2ULL * 9223372036854775807LL + 1); + #endif + const char s8_zero = 0, s8_one = 1, s8_fifty = 50, s8_min = -128, s8_max = 127; + const ImU8 u8_zero = 0, u8_one = 1, u8_fifty = 50, u8_min = 0, u8_max = 255; + const short s16_zero = 0, s16_one = 1, s16_fifty = 50, s16_min = -32768, s16_max = 32767; + const ImU16 u16_zero = 0, u16_one = 1, u16_fifty = 50, u16_min = 0, u16_max = 65535; + const ImS32 s32_zero = 0, s32_one = 1, s32_fifty = 50, s32_min = INT_MIN/2, s32_max = INT_MAX/2, s32_hi_a = INT_MAX/2 - 100, s32_hi_b = INT_MAX/2; + const ImU32 u32_zero = 0, u32_one = 1, u32_fifty = 50, u32_min = 0, u32_max = UINT_MAX/2, u32_hi_a = UINT_MAX/2 - 100, u32_hi_b = UINT_MAX/2; + const ImS64 s64_zero = 0, s64_one = 1, s64_fifty = 50, s64_min = LLONG_MIN/2, s64_max = LLONG_MAX/2, s64_hi_a = LLONG_MAX/2 - 100, s64_hi_b = LLONG_MAX/2; + const ImU64 u64_zero = 0, u64_one = 1, u64_fifty = 50, u64_min = 0, u64_max = ULLONG_MAX/2, u64_hi_a = ULLONG_MAX/2 - 100, u64_hi_b = ULLONG_MAX/2; + const float f32_zero = 0.f, f32_one = 1.f, f32_lo_a = -10000000000.0f, f32_hi_a = +10000000000.0f; + const double f64_zero = 0., f64_one = 1., f64_lo_a = -1000000000000000.0, f64_hi_a = +1000000000000000.0; + + // State + static char s8_v = 127; + static ImU8 u8_v = 255; + static short s16_v = 32767; + static ImU16 u16_v = 65535; + static ImS32 s32_v = -1; + static ImU32 u32_v = (ImU32)-1; + static ImS64 s64_v = -1; + static ImU64 u64_v = (ImU64)-1; + static float f32_v = 0.123f; + static double f64_v = 90000.01234567890123456789; + + const float drag_speed = 0.2f; + static bool drag_clamp = false; + ImGui::Text("Drags:"); + ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); HelpMarker("As with every widgets in dear imgui, we never modify values unless there is a user interaction.\nYou can override the clamping limits by using CTRL+Click to input a value."); + ImGui::DragScalar("drag s8", ImGuiDataType_S8, &s8_v, drag_speed, drag_clamp ? &s8_zero : NULL, drag_clamp ? &s8_fifty : NULL); + ImGui::DragScalar("drag u8", ImGuiDataType_U8, &u8_v, drag_speed, drag_clamp ? &u8_zero : NULL, drag_clamp ? &u8_fifty : NULL, "%u ms"); + ImGui::DragScalar("drag s16", ImGuiDataType_S16, &s16_v, drag_speed, drag_clamp ? &s16_zero : NULL, drag_clamp ? &s16_fifty : NULL); + ImGui::DragScalar("drag u16", ImGuiDataType_U16, &u16_v, drag_speed, drag_clamp ? &u16_zero : NULL, drag_clamp ? &u16_fifty : NULL, "%u ms"); + ImGui::DragScalar("drag s32", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL); + ImGui::DragScalar("drag u32", ImGuiDataType_U32, &u32_v, drag_speed, drag_clamp ? &u32_zero : NULL, drag_clamp ? &u32_fifty : NULL, "%u ms"); + ImGui::DragScalar("drag s64", ImGuiDataType_S64, &s64_v, drag_speed, drag_clamp ? &s64_zero : NULL, drag_clamp ? &s64_fifty : NULL); + ImGui::DragScalar("drag u64", ImGuiDataType_U64, &u64_v, drag_speed, drag_clamp ? &u64_zero : NULL, drag_clamp ? &u64_fifty : NULL); + ImGui::DragScalar("drag float", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", 1.0f); + ImGui::DragScalar("drag float ^2", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", 2.0f); ImGui::SameLine(); HelpMarker("You can use the 'power' parameter to increase tweaking precision on one side of the range."); + ImGui::DragScalar("drag double", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, NULL, "%.10f grams", 1.0f); + ImGui::DragScalar("drag double ^2", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", 2.0f); + + ImGui::Text("Sliders"); + ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d"); + ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u"); + ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d"); + ImGui::SliderScalar("slider u16 full", ImGuiDataType_U16, &u16_v, &u16_min, &u16_max, "%u"); + ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d"); + ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d"); + ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d"); + ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); + ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); + ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); + ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%I64d"); + ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%I64d"); + ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%I64d"); + ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%I64u ms"); + ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%I64u ms"); + ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%I64u ms"); + ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one); + ImGui::SliderScalar("slider float low^2", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", 2.0f); + ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e"); + ImGui::SliderScalar("slider double low", ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f grams", 1.0f); + ImGui::SliderScalar("slider double low^2",ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f", 2.0f); + ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams", 1.0f); + + static bool inputs_step = true; + ImGui::Text("Inputs"); + ImGui::Checkbox("Show step buttons", &inputs_step); + ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d"); + ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u"); + ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d"); + ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u"); + ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d"); + ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%08X", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u"); + ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::InputScalar("input s64", ImGuiDataType_S64, &s64_v, inputs_step ? &s64_one : NULL); + ImGui::InputScalar("input u64", ImGuiDataType_U64, &u64_v, inputs_step ? &u64_one : NULL); + ImGui::InputScalar("input float", ImGuiDataType_Float, &f32_v, inputs_step ? &f32_one : NULL); + ImGui::InputScalar("input double", ImGuiDataType_Double, &f64_v, inputs_step ? &f64_one : NULL); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Multi-component Widgets")) + { + static float vec4f[4] = { 0.10f, 0.20f, 0.30f, 0.44f }; + static int vec4i[4] = { 1, 5, 100, 255 }; + + ImGui::InputFloat2("input float2", vec4f); + ImGui::DragFloat2("drag float2", vec4f, 0.01f, 0.0f, 1.0f); + ImGui::SliderFloat2("slider float2", vec4f, 0.0f, 1.0f); + ImGui::InputInt2("input int2", vec4i); + ImGui::DragInt2("drag int2", vec4i, 1, 0, 255); + ImGui::SliderInt2("slider int2", vec4i, 0, 255); + ImGui::Spacing(); + + ImGui::InputFloat3("input float3", vec4f); + ImGui::DragFloat3("drag float3", vec4f, 0.01f, 0.0f, 1.0f); + ImGui::SliderFloat3("slider float3", vec4f, 0.0f, 1.0f); + ImGui::InputInt3("input int3", vec4i); + ImGui::DragInt3("drag int3", vec4i, 1, 0, 255); + ImGui::SliderInt3("slider int3", vec4i, 0, 255); + ImGui::Spacing(); + + ImGui::InputFloat4("input float4", vec4f); + ImGui::DragFloat4("drag float4", vec4f, 0.01f, 0.0f, 1.0f); + ImGui::SliderFloat4("slider float4", vec4f, 0.0f, 1.0f); + ImGui::InputInt4("input int4", vec4i); + ImGui::DragInt4("drag int4", vec4i, 1, 0, 255); + ImGui::SliderInt4("slider int4", vec4i, 0, 255); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Vertical Sliders")) + { + const float spacing = 4; + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing, spacing)); + + static int int_value = 0; + ImGui::VSliderInt("##int", ImVec2(18,160), &int_value, 0, 5); + ImGui::SameLine(); + + static float values[7] = { 0.0f, 0.60f, 0.35f, 0.9f, 0.70f, 0.20f, 0.0f }; + ImGui::PushID("set1"); + for (int i = 0; i < 7; i++) + { + if (i > 0) ImGui::SameLine(); + ImGui::PushID(i); + ImGui::PushStyleColor(ImGuiCol_FrameBg, (ImVec4)ImColor::HSV(i/7.0f, 0.5f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, (ImVec4)ImColor::HSV(i/7.0f, 0.6f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_FrameBgActive, (ImVec4)ImColor::HSV(i/7.0f, 0.7f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_SliderGrab, (ImVec4)ImColor::HSV(i/7.0f, 0.9f, 0.9f)); + ImGui::VSliderFloat("##v", ImVec2(18,160), &values[i], 0.0f, 1.0f, ""); + if (ImGui::IsItemActive() || ImGui::IsItemHovered()) + ImGui::SetTooltip("%.3f", values[i]); + ImGui::PopStyleColor(4); + ImGui::PopID(); + } + ImGui::PopID(); + + ImGui::SameLine(); + ImGui::PushID("set2"); + static float values2[4] = { 0.20f, 0.80f, 0.40f, 0.25f }; + const int rows = 3; + const ImVec2 small_slider_size(18, (float)(int)((160.0f - (rows - 1) * spacing) / rows)); + for (int nx = 0; nx < 4; nx++) + { + if (nx > 0) ImGui::SameLine(); + ImGui::BeginGroup(); + for (int ny = 0; ny < rows; ny++) + { + ImGui::PushID(nx*rows+ny); + ImGui::VSliderFloat("##v", small_slider_size, &values2[nx], 0.0f, 1.0f, ""); + if (ImGui::IsItemActive() || ImGui::IsItemHovered()) + ImGui::SetTooltip("%.3f", values2[nx]); + ImGui::PopID(); + } + ImGui::EndGroup(); + } + ImGui::PopID(); + + ImGui::SameLine(); + ImGui::PushID("set3"); + for (int i = 0; i < 4; i++) + { + if (i > 0) ImGui::SameLine(); + ImGui::PushID(i); + ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40); + ImGui::VSliderFloat("##v", ImVec2(40,160), &values[i], 0.0f, 1.0f, "%.2f\nsec"); + ImGui::PopStyleVar(); + ImGui::PopID(); + } + ImGui::PopID(); + ImGui::PopStyleVar(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Drag and Drop")) + { + if (ImGui::TreeNode("Drag and drop in standard widgets")) + { + // ColorEdit widgets automatically act as drag source and drag target. + // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F to allow your own widgets + // to use colors in their drag and drop interaction. Also see the demo in Color Picker -> Palette demo. + HelpMarker("You can drag from the colored squares."); + static float col1[3] = { 1.0f, 0.0f, 0.2f }; + static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; + ImGui::ColorEdit3("color 1", col1); + ImGui::ColorEdit4("color 2", col2); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Drag and drop to copy/swap items")) + { + enum Mode + { + Mode_Copy, + Mode_Move, + Mode_Swap + }; + static int mode = 0; + if (ImGui::RadioButton("Copy", mode == Mode_Copy)) { mode = Mode_Copy; } ImGui::SameLine(); + if (ImGui::RadioButton("Move", mode == Mode_Move)) { mode = Mode_Move; } ImGui::SameLine(); + if (ImGui::RadioButton("Swap", mode == Mode_Swap)) { mode = Mode_Swap; } + static const char* names[9] = { "Bobby", "Beatrice", "Betty", "Brianna", "Barry", "Bernard", "Bibi", "Blaine", "Bryn" }; + for (int n = 0; n < IM_ARRAYSIZE(names); n++) + { + ImGui::PushID(n); + if ((n % 3) != 0) + ImGui::SameLine(); + ImGui::Button(names[n], ImVec2(60,60)); + + // Our buttons are both drag sources and drag targets here! + if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) + { + ImGui::SetDragDropPayload("DND_DEMO_CELL", &n, sizeof(int)); // Set payload to carry the index of our item (could be anything) + if (mode == Mode_Copy) { ImGui::Text("Copy %s", names[n]); } // Display preview (could be anything, e.g. when dragging an image we could decide to display the filename and a small preview of the image, etc.) + if (mode == Mode_Move) { ImGui::Text("Move %s", names[n]); } + if (mode == Mode_Swap) { ImGui::Text("Swap %s", names[n]); } + ImGui::EndDragDropSource(); + } + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("DND_DEMO_CELL")) + { + IM_ASSERT(payload->DataSize == sizeof(int)); + int payload_n = *(const int*)payload->Data; + if (mode == Mode_Copy) + { + names[n] = names[payload_n]; + } + if (mode == Mode_Move) + { + names[n] = names[payload_n]; + names[payload_n] = ""; + } + if (mode == Mode_Swap) + { + const char* tmp = names[n]; + names[n] = names[payload_n]; + names[payload_n] = tmp; + } + } + ImGui::EndDragDropTarget(); + } + ImGui::PopID(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Drag to reorder items (simple)")) + { + // Simple reordering + HelpMarker("We don't use the drag and drop api at all here! Instead we query when the item is held but not hovered, and order items accordingly."); + static const char* item_names[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" }; + for (int n = 0; n < IM_ARRAYSIZE(item_names); n++) + { + const char* item = item_names[n]; + ImGui::Selectable(item); + + if (ImGui::IsItemActive() && !ImGui::IsItemHovered()) + { + int n_next = n + (ImGui::GetMouseDragDelta(0).y < 0.f ? -1 : 1); + if (n_next >= 0 && n_next < IM_ARRAYSIZE(item_names)) + { + item_names[n] = item_names[n_next]; + item_names[n_next] = item; + ImGui::ResetMouseDragDelta(); + } + } + } + ImGui::TreePop(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Querying Status (Active/Focused/Hovered etc.)")) + { + // Submit an item (various types available) so we can query their status in the following block. + static int item_type = 1; + ImGui::Combo("Item Type", &item_type, "Text\0Button\0Button (w/ repeat)\0Checkbox\0SliderFloat\0InputText\0InputFloat\0InputFloat3\0ColorEdit4\0MenuItem\0TreeNode (w/ double-click)\0ListBox\0"); + ImGui::SameLine(); + HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions."); + bool ret = false; + static bool b = false; + static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f }; + static char str[16] = {}; + if (item_type == 0) { ImGui::Text("ITEM: Text"); } // Testing text items with no identifier/interaction + if (item_type == 1) { ret = ImGui::Button("ITEM: Button"); } // Testing button + if (item_type == 2) { ImGui::PushButtonRepeat(true); ret = ImGui::Button("ITEM: Button"); ImGui::PopButtonRepeat(); } // Testing button (with repeater) + if (item_type == 3) { ret = ImGui::Checkbox("ITEM: Checkbox", &b); } // Testing checkbox + if (item_type == 4) { ret = ImGui::SliderFloat("ITEM: SliderFloat", &col4f[0], 0.0f, 1.0f); } // Testing basic item + if (item_type == 5) { ret = ImGui::InputText("ITEM: InputText", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which handles tabbing) + if (item_type == 6) { ret = ImGui::InputFloat("ITEM: InputFloat", col4f, 1.0f); } // Testing +/- buttons on scalar input + if (item_type == 7) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) + if (item_type == 8) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) + if (item_type == 9) { ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) + if (item_type == 10){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. + if (item_type == 11){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + + // Display the value of IsItemHovered() and other common item state functions. + // Note that the ImGuiHoveredFlags_XXX flags can be combined. + // Because BulletText is an item itself and that would affect the output of IsItemXXX functions, + // we query every state in a single call to avoid storing them and to simplify the code + ImGui::BulletText( + "Return value = %d\n" + "IsItemFocused() = %d\n" + "IsItemHovered() = %d\n" + "IsItemHovered(_AllowWhenBlockedByPopup) = %d\n" + "IsItemHovered(_AllowWhenBlockedByActiveItem) = %d\n" + "IsItemHovered(_AllowWhenOverlapped) = %d\n" + "IsItemHovered(_RectOnly) = %d\n" + "IsItemActive() = %d\n" + "IsItemEdited() = %d\n" + "IsItemActivated() = %d\n" + "IsItemDeactivated() = %d\n" + "IsItemDeactivatedAfterEdit() = %d\n" + "IsItemVisible() = %d\n" + "IsItemClicked() = %d\n" + "GetItemRectMin() = (%.1f, %.1f)\n" + "GetItemRectMax() = (%.1f, %.1f)\n" + "GetItemRectSize() = (%.1f, %.1f)", + ret, + ImGui::IsItemFocused(), + ImGui::IsItemHovered(), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlapped), + ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly), + ImGui::IsItemActive(), + ImGui::IsItemEdited(), + ImGui::IsItemActivated(), + ImGui::IsItemDeactivated(), + ImGui::IsItemDeactivatedAfterEdit(), + ImGui::IsItemVisible(), + ImGui::IsItemClicked(), + ImGui::GetItemRectMin().x, ImGui::GetItemRectMin().y, + ImGui::GetItemRectMax().x, ImGui::GetItemRectMax().y, + ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y + ); + + static bool embed_all_inside_a_child_window = false; + ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window); + if (embed_all_inside_a_child_window) + ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20), true); + + // Testing IsWindowFocused() function with its various flags. + // Note that the ImGuiFocusedFlags_XXX flags can be combined. + ImGui::BulletText( + "IsWindowFocused() = %d\n" + "IsWindowFocused(_ChildWindows) = %d\n" + "IsWindowFocused(_ChildWindows|_RootWindow) = %d\n" + "IsWindowFocused(_RootWindow) = %d\n" + "IsWindowFocused(_AnyWindow) = %d\n", + ImGui::IsWindowFocused(), + ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows), + ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_RootWindow), + ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow), + ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow)); + + // Testing IsWindowHovered() function with its various flags. + // Note that the ImGuiHoveredFlags_XXX flags can be combined. + ImGui::BulletText( + "IsWindowHovered() = %d\n" + "IsWindowHovered(_AllowWhenBlockedByPopup) = %d\n" + "IsWindowHovered(_AllowWhenBlockedByActiveItem) = %d\n" + "IsWindowHovered(_ChildWindows) = %d\n" + "IsWindowHovered(_ChildWindows|_RootWindow) = %d\n" + "IsWindowHovered(_ChildWindows|_AllowWhenBlockedByPopup) = %d\n" + "IsWindowHovered(_RootWindow) = %d\n" + "IsWindowHovered(_AnyWindow) = %d\n", + ImGui::IsWindowHovered(), + ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), + ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByPopup), + ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow), + ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)); + + ImGui::BeginChild("child", ImVec2(0, 50), true); + ImGui::Text("This is another child window for testing the _ChildWindows flag."); + ImGui::EndChild(); + if (embed_all_inside_a_child_window) + ImGui::EndChild(); + + static char dummy_str[] = "This is a dummy field to be able to tab-out of the widgets above."; + ImGui::InputText("dummy", dummy_str, IM_ARRAYSIZE(dummy_str), ImGuiInputTextFlags_ReadOnly); + + // Calling IsItemHovered() after begin returns the hovered status of the title bar. + // This is useful in particular if you want to create a context menu (with BeginPopupContextItem) associated to the title bar of a window. + static bool test_window = false; + ImGui::Checkbox("Hovered/Active tests after Begin() for title bar testing", &test_window); + if (test_window) + { + ImGui::Begin("Title bar Hovered/Active tests", &test_window); + if (ImGui::BeginPopupContextItem()) // <-- This is using IsItemHovered() + { + if (ImGui::MenuItem("Close")) { test_window = false; } + ImGui::EndPopup(); + } + ImGui::Text( + "IsItemHovered() after begin = %d (== is title bar hovered)\n" + "IsItemActive() after begin = %d (== is window being clicked/moved)\n", + ImGui::IsItemHovered(), ImGui::IsItemActive()); + ImGui::End(); + } + + ImGui::TreePop(); + } +} + +static void ShowDemoWindowLayout() +{ + if (!ImGui::CollapsingHeader("Layout")) + return; + + if (ImGui::TreeNode("Child windows")) + { + HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window."); + static bool disable_mouse_wheel = false; + static bool disable_menu = false; + ImGui::Checkbox("Disable Mouse Wheel", &disable_mouse_wheel); + ImGui::Checkbox("Disable Menu", &disable_menu); + + static int line = 50; + bool goto_line = ImGui::Button("Goto"); + ImGui::SameLine(); + ImGui::SetNextItemWidth(100); + goto_line |= ImGui::InputInt("##Line", &line, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue); + + // Child 1: no border, enable horizontal scrollbar + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar | (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0); + ImGui::BeginChild("Child1", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); + for (int i = 0; i < 100; i++) + { + ImGui::Text("%04d: scrollable region", i); + if (goto_line && line == i) + ImGui::SetScrollHereY(); + } + if (goto_line && line >= 100) + ImGui::SetScrollHereY(); + ImGui::EndChild(); + } + + ImGui::SameLine(); + + // Child 2: rounded border + { + ImGuiWindowFlags window_flags = (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0) | (disable_menu ? 0 : ImGuiWindowFlags_MenuBar); + ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); + ImGui::BeginChild("Child2", ImVec2(0, 260), true, window_flags); + if (!disable_menu && ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Menu")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + ImGui::Columns(2); + for (int i = 0; i < 100; i++) + { + char buf[32]; + sprintf(buf, "%03d", i); + ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + ImGui::NextColumn(); + } + ImGui::EndChild(); + ImGui::PopStyleVar(); + } + + ImGui::Separator(); + + // Demonstrate a few extra things + // - Changing ImGuiCol_ChildBg (which is transparent black in default styles) + // - Using SetCursorPos() to position the child window (because the child window is an item from the POV of the parent window) + // You can also call SetNextWindowPos() to position the child window. The parent window will effectively layout from this position. + // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from the POV of the parent window) + // See "Widgets" -> "Querying Status (Active/Focused/Hovered etc.)" section for more details about this. + { + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); + ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); + ImGui::BeginChild("blah", ImVec2(200, 100), true, ImGuiWindowFlags_None); + for (int n = 0; n < 50; n++) + ImGui::Text("Some test %d", n); + ImGui::EndChild(); + ImVec2 child_rect_min = ImGui::GetItemRectMin(); + ImVec2 child_rect_max = ImGui::GetItemRectMax(); + ImGui::PopStyleColor(); + ImGui::Text("Rect of child window is: (%.0f,%.0f) (%.0f,%.0f)", child_rect_min.x, child_rect_min.y, child_rect_max.x, child_rect_max.y); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Widgets Width")) + { + // Use SetNextItemWidth() to set the width of a single upcoming item. + // Use PushItemWidth()/PopItemWidth() to set the width of a group of items. + static float f = 0.0f; + ImGui::Text("SetNextItemWidth/PushItemWidth(100)"); + ImGui::SameLine(); HelpMarker("Fixed width."); + ImGui::SetNextItemWidth(100); + ImGui::DragFloat("float##1", &f); + + ImGui::Text("SetNextItemWidth/PushItemWidth(GetWindowWidth() * 0.5f)"); + ImGui::SameLine(); HelpMarker("Half of window width."); + ImGui::SetNextItemWidth(ImGui::GetWindowWidth() * 0.5f); + ImGui::DragFloat("float##2", &f); + + ImGui::Text("SetNextItemWidth/PushItemWidth(GetContentRegionAvail().x * 0.5f)"); + ImGui::SameLine(); HelpMarker("Half of available width.\n(~ right-cursor_pos)\n(works within a column set)"); + ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x * 0.5f); + ImGui::DragFloat("float##3", &f); + + ImGui::Text("SetNextItemWidth/PushItemWidth(-100)"); + ImGui::SameLine(); HelpMarker("Align to right edge minus 100"); + ImGui::SetNextItemWidth(-100); + ImGui::DragFloat("float##4", &f); + + // Demonstrate using PushItemWidth to surround three items. Calling SetNextItemWidth() before each of them would have the same effect. + ImGui::Text("SetNextItemWidth/PushItemWidth(-1)"); + ImGui::SameLine(); HelpMarker("Align to right edge"); + ImGui::PushItemWidth(-1); + ImGui::DragFloat("##float5a", &f); + ImGui::DragFloat("##float5b", &f); + ImGui::DragFloat("##float5c", &f); + ImGui::PopItemWidth(); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Basic Horizontal Layout")) + { + ImGui::TextWrapped("(Use ImGui::SameLine() to keep adding items to the right of the preceding item)"); + + // Text + ImGui::Text("Two items: Hello"); ImGui::SameLine(); + ImGui::TextColored(ImVec4(1,1,0,1), "Sailor"); + + // Adjust spacing + ImGui::Text("More spacing: Hello"); ImGui::SameLine(0, 20); + ImGui::TextColored(ImVec4(1,1,0,1), "Sailor"); + + // Button + ImGui::AlignTextToFramePadding(); + ImGui::Text("Normal buttons"); ImGui::SameLine(); + ImGui::Button("Banana"); ImGui::SameLine(); + ImGui::Button("Apple"); ImGui::SameLine(); + ImGui::Button("Corniflower"); + + // Button + ImGui::Text("Small buttons"); ImGui::SameLine(); + ImGui::SmallButton("Like this one"); ImGui::SameLine(); + ImGui::Text("can fit within a text block."); + + // Aligned to arbitrary position. Easy/cheap column. + ImGui::Text("Aligned"); + ImGui::SameLine(150); ImGui::Text("x=150"); + ImGui::SameLine(300); ImGui::Text("x=300"); + ImGui::Text("Aligned"); + ImGui::SameLine(150); ImGui::SmallButton("x=150"); + ImGui::SameLine(300); ImGui::SmallButton("x=300"); + + // Checkbox + static bool c1 = false, c2 = false, c3 = false, c4 = false; + ImGui::Checkbox("My", &c1); ImGui::SameLine(); + ImGui::Checkbox("Tailor", &c2); ImGui::SameLine(); + ImGui::Checkbox("Is", &c3); ImGui::SameLine(); + ImGui::Checkbox("Rich", &c4); + + // Various + static float f0 = 1.0f, f1 = 2.0f, f2 = 3.0f; + ImGui::PushItemWidth(80); + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD" }; + static int item = -1; + ImGui::Combo("Combo", &item, items, IM_ARRAYSIZE(items)); ImGui::SameLine(); + ImGui::SliderFloat("X", &f0, 0.0f, 5.0f); ImGui::SameLine(); + ImGui::SliderFloat("Y", &f1, 0.0f, 5.0f); ImGui::SameLine(); + ImGui::SliderFloat("Z", &f2, 0.0f, 5.0f); + ImGui::PopItemWidth(); + + ImGui::PushItemWidth(80); + ImGui::Text("Lists:"); + static int selection[4] = { 0, 1, 2, 3 }; + for (int i = 0; i < 4; i++) + { + if (i > 0) ImGui::SameLine(); + ImGui::PushID(i); + ImGui::ListBox("", &selection[i], items, IM_ARRAYSIZE(items)); + ImGui::PopID(); + //if (ImGui::IsItemHovered()) ImGui::SetTooltip("ListBox %d hovered", i); + } + ImGui::PopItemWidth(); + + // Dummy + ImVec2 button_sz(40, 40); + ImGui::Button("A", button_sz); ImGui::SameLine(); + ImGui::Dummy(button_sz); ImGui::SameLine(); + ImGui::Button("B", button_sz); + + // Manually wrapping (we should eventually provide this as an automatic layout feature, but for now you can do it manually) + ImGui::Text("Manually wrapping:"); + ImGuiStyle& style = ImGui::GetStyle(); + int buttons_count = 20; + float window_visible_x2 = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x; + for (int n = 0; n < buttons_count; n++) + { + ImGui::PushID(n); + ImGui::Button("Box", button_sz); + float last_button_x2 = ImGui::GetItemRectMax().x; + float next_button_x2 = last_button_x2 + style.ItemSpacing.x + button_sz.x; // Expected position if next button was on same line + if (n + 1 < buttons_count && next_button_x2 < window_visible_x2) + ImGui::SameLine(); + ImGui::PopID(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Tabs")) + { + if (ImGui::TreeNode("Basic")) + { + ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None; + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + if (ImGui::BeginTabItem("Avocado")) + { + ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Broccoli")) + { + ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Cucumber")) + { + ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Advanced & Close Button")) + { + // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). + static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable; + ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_Reorderable); + ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); + ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); + ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); + if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) + tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_; + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); + + // Tab Bar + const char* names[4] = { "Artichoke", "Beetroot", "Celery", "Daikon" }; + static bool opened[4] = { true, true, true, true }; // Persistent user state + for (int n = 0; n < IM_ARRAYSIZE(opened); n++) + { + if (n > 0) { ImGui::SameLine(); } + ImGui::Checkbox(names[n], &opened[n]); + } + + // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + for (int n = 0; n < IM_ARRAYSIZE(opened); n++) + if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n])) + { + ImGui::Text("This is the %s tab!", names[n]); + if (n & 1) + ImGui::Text("I am an odd tab."); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Groups")) + { + HelpMarker("BeginGroup() basically locks the horizontal position for new line. EndGroup() bundles the whole group so that you can use \"item\" functions such as IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group."); + ImGui::BeginGroup(); + { + ImGui::BeginGroup(); + ImGui::Button("AAA"); + ImGui::SameLine(); + ImGui::Button("BBB"); + ImGui::SameLine(); + ImGui::BeginGroup(); + ImGui::Button("CCC"); + ImGui::Button("DDD"); + ImGui::EndGroup(); + ImGui::SameLine(); + ImGui::Button("EEE"); + ImGui::EndGroup(); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("First group hovered"); + } + // Capture the group size and create widgets using the same size + ImVec2 size = ImGui::GetItemRectSize(); + const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f }; + ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size); + + ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f, size.y)); + ImGui::SameLine(); + ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f, size.y)); + ImGui::EndGroup(); + ImGui::SameLine(); + + ImGui::Button("LEVERAGE\nBUZZWORD", size); + ImGui::SameLine(); + + if (ImGui::ListBoxHeader("List", size)) + { + ImGui::Selectable("Selected", true); + ImGui::Selectable("Not Selected", false); + ImGui::ListBoxFooter(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Text Baseline Alignment")) + { + { + ImGui::BulletText("Text baseline:"); + ImGui::SameLine(); + HelpMarker("This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. Lines only composed of text or \"small\" widgets fit in less vertical spaces than lines with normal widgets."); + ImGui::Indent(); + + ImGui::Text("KO Blahblah"); ImGui::SameLine(); + ImGui::Button("Some framed item"); ImGui::SameLine(); + HelpMarker("Baseline of button will look misaligned with text.."); + + // If your line starts with text, call AlignTextToFramePadding() to align text to upcoming widgets. + // Because we don't know what's coming after the Text() statement, we need to move the text baseline down by FramePadding.y + ImGui::AlignTextToFramePadding(); + ImGui::Text("OK Blahblah"); ImGui::SameLine(); + ImGui::Button("Some framed item"); ImGui::SameLine(); + HelpMarker("We call AlignTextToFramePadding() to vertically align the text baseline by +FramePadding.y"); + + // SmallButton() uses the same vertical padding as Text + ImGui::Button("TEST##1"); ImGui::SameLine(); + ImGui::Text("TEST"); ImGui::SameLine(); + ImGui::SmallButton("TEST##2"); + + // If your line starts with text, call AlignTextToFramePadding() to align text to upcoming widgets. + ImGui::AlignTextToFramePadding(); + ImGui::Text("Text aligned to framed item"); ImGui::SameLine(); + ImGui::Button("Item##1"); ImGui::SameLine(); + ImGui::Text("Item"); ImGui::SameLine(); + ImGui::SmallButton("Item##2"); ImGui::SameLine(); + ImGui::Button("Item##3"); + + ImGui::Unindent(); + } + + ImGui::Spacing(); + + { + ImGui::BulletText("Multi-line text:"); + ImGui::Indent(); + ImGui::Text("One\nTwo\nThree"); ImGui::SameLine(); + ImGui::Text("Hello\nWorld"); ImGui::SameLine(); + ImGui::Text("Banana"); + + ImGui::Text("Banana"); ImGui::SameLine(); + ImGui::Text("Hello\nWorld"); ImGui::SameLine(); + ImGui::Text("One\nTwo\nThree"); + + ImGui::Button("HOP##1"); ImGui::SameLine(); + ImGui::Text("Banana"); ImGui::SameLine(); + ImGui::Text("Hello\nWorld"); ImGui::SameLine(); + ImGui::Text("Banana"); + + ImGui::Button("HOP##2"); ImGui::SameLine(); + ImGui::Text("Hello\nWorld"); ImGui::SameLine(); + ImGui::Text("Banana"); + ImGui::Unindent(); + } + + ImGui::Spacing(); + + { + ImGui::BulletText("Misc items:"); + ImGui::Indent(); + + // SmallButton() sets FramePadding to zero. Text baseline is aligned to match baseline of previous Button + ImGui::Button("80x80", ImVec2(80, 80)); + ImGui::SameLine(); + ImGui::Button("50x50", ImVec2(50, 50)); + ImGui::SameLine(); + ImGui::Button("Button()"); + ImGui::SameLine(); + ImGui::SmallButton("SmallButton()"); + + // Tree + const float spacing = ImGui::GetStyle().ItemInnerSpacing.x; + ImGui::Button("Button##1"); + ImGui::SameLine(0.0f, spacing); + if (ImGui::TreeNode("Node##1")) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data + + ImGui::AlignTextToFramePadding(); // Vertically align text node a bit lower so it'll be vertically centered with upcoming widget. Otherwise you can use SmallButton (smaller fit). + bool node_open = ImGui::TreeNode("Node##2");// Common mistake to avoid: if we want to SameLine after TreeNode we need to do it before we add child content. + ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2"); + if (node_open) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data + + // Bullet + ImGui::Button("Button##3"); + ImGui::SameLine(0.0f, spacing); + ImGui::BulletText("Bullet text"); + + ImGui::AlignTextToFramePadding(); + ImGui::BulletText("Node"); + ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##4"); + ImGui::Unindent(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Scrolling")) + { + // Vertical scroll functions + HelpMarker("Use SetScrollHereY() or SetScrollFromPosY() to scroll to a given vertical position."); + + static int track_item = 50; + static bool enable_track = true; + static bool enable_extra_decorations = false; + static float scroll_to_off_px = 0.0f; + static float scroll_to_pos_px = 200.0f; + + ImGui::Checkbox("Decoration", &enable_extra_decorations); + ImGui::SameLine(); + HelpMarker("We expose this for testing because scrolling sometimes had issues with window decoration such as menu-bars."); + + ImGui::Checkbox("Track", &enable_track); + ImGui::PushItemWidth(100); + ImGui::SameLine(140); enable_track |= ImGui::DragInt("##item", &track_item, 0.25f, 0, 99, "Item = %d"); + + bool scroll_to_off = ImGui::Button("Scroll Offset"); + ImGui::SameLine(140); scroll_to_off |= ImGui::DragFloat("##off", &scroll_to_off_px, 1.00f, 0, 9999, "+%.0f px"); + + bool scroll_to_pos = ImGui::Button("Scroll To Pos"); + ImGui::SameLine(140); scroll_to_pos |= ImGui::DragFloat("##pos", &scroll_to_pos_px, 1.00f, -10, 9999, "X/Y = %.0f px"); + ImGui::PopItemWidth(); + + if (scroll_to_off || scroll_to_pos) + enable_track = false; + + ImGuiStyle& style = ImGui::GetStyle(); + float child_w = (ImGui::GetContentRegionAvail().x - 4 * style.ItemSpacing.x) / 5; + if (child_w < 1.0f) + child_w = 1.0f; + ImGui::PushID("##VerticalScrolling"); + for (int i = 0; i < 5; i++) + { + if (i > 0) ImGui::SameLine(); + ImGui::BeginGroup(); + const char* names[] = { "Top", "25%", "Center", "75%", "Bottom" }; + ImGui::TextUnformatted(names[i]); + + ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; + ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(child_w, 200.0f), true, child_flags); + if (ImGui::BeginMenuBar()) + { + ImGui::TextUnformatted("abc"); + ImGui::EndMenuBar(); + } + if (scroll_to_off) + ImGui::SetScrollY(scroll_to_off_px); + if (scroll_to_pos) + ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scroll_to_pos_px, i * 0.25f); + for (int item = 0; item < 100; item++) + { + if (enable_track && item == track_item) + { + ImGui::TextColored(ImVec4(1,1,0,1), "Item %d", item); + ImGui::SetScrollHereY(i * 0.25f); // 0.0f:top, 0.5f:center, 1.0f:bottom + } + else + { + ImGui::Text("Item %d", item); + } + } + float scroll_y = ImGui::GetScrollY(); + float scroll_max_y = ImGui::GetScrollMaxY(); + ImGui::EndChild(); + ImGui::Text("%.0f/%.0f", scroll_y, scroll_max_y); + ImGui::EndGroup(); + } + ImGui::PopID(); + + // Horizontal scroll functions + ImGui::Spacing(); + HelpMarker("Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\nUsing the \"Scroll To Pos\" button above will make the discontinuity at edges visible: scrolling to the top/bottom/left/right-most item will add an additional WindowPadding to reflect on reaching the edge of the list.\n\nBecause the clipping rectangle of most window hides half worth of WindowPadding on the left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the equivalent SetScrollFromPosY(+1) wouldn't."); + ImGui::PushID("##HorizontalScrolling"); + for (int i = 0; i < 5; i++) + { + float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f; + ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0); + ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(-100, child_height), true, child_flags); + if (scroll_to_off) + ImGui::SetScrollX(scroll_to_off_px); + if (scroll_to_pos) + ImGui::SetScrollFromPosX(ImGui::GetCursorStartPos().x + scroll_to_pos_px, i * 0.25f); + for (int item = 0; item < 100; item++) + { + if (enable_track && item == track_item) + { + ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item); + ImGui::SetScrollHereX(i * 0.25f); // 0.0f:left, 0.5f:center, 1.0f:right + } + else + { + ImGui::Text("Item %d", item); + } + ImGui::SameLine(); + } + float scroll_x = ImGui::GetScrollX(); + float scroll_max_x = ImGui::GetScrollMaxX(); + ImGui::EndChild(); + ImGui::SameLine(); + const char* names[] = { "Left", "25%", "Center", "75%", "Right" }; + ImGui::Text("%s\n%.0f/%.0f", names[i], scroll_x, scroll_max_x); + ImGui::Spacing(); + } + ImGui::PopID(); + + // Miscellaneous Horizontal Scrolling Demo + HelpMarker("Horizontal scrolling for a window has to be enabled explicitly via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\nYou may want to explicitly specify content width by calling SetNextWindowContentWidth() before Begin()."); + static int lines = 7; + ImGui::SliderInt("Lines", &lines, 1, 15); + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2.0f, 1.0f)); + ImGui::BeginChild("scrolling", ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30), true, ImGuiWindowFlags_HorizontalScrollbar); + for (int line = 0; line < lines; line++) + { + // Display random stuff (for the sake of this trivial demo we are using basic Button+SameLine. If you want to create your own time line for a real application you may be better off + // manipulating the cursor position yourself, aka using SetCursorPos/SetCursorScreenPos to position the widgets yourself. You may also want to use the lower-level ImDrawList API) + int num_buttons = 10 + ((line & 1) ? line * 9 : line * 3); + for (int n = 0; n < num_buttons; n++) + { + if (n > 0) ImGui::SameLine(); + ImGui::PushID(n + line * 1000); + char num_buf[16]; + sprintf(num_buf, "%d", n); + const char* label = (!(n%15)) ? "FizzBuzz" : (!(n%3)) ? "Fizz" : (!(n%5)) ? "Buzz" : num_buf; + float hue = n*0.05f; + ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(hue, 0.6f, 0.6f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(hue, 0.7f, 0.7f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(hue, 0.8f, 0.8f)); + ImGui::Button(label, ImVec2(40.0f + sinf((float)(line + n)) * 20.0f, 0.0f)); + ImGui::PopStyleColor(3); + ImGui::PopID(); + } + } + float scroll_x = ImGui::GetScrollX(); + float scroll_max_x = ImGui::GetScrollMaxX(); + ImGui::EndChild(); + ImGui::PopStyleVar(2); + float scroll_x_delta = 0.0f; + ImGui::SmallButton("<<"); if (ImGui::IsItemActive()) { scroll_x_delta = -ImGui::GetIO().DeltaTime * 1000.0f; } ImGui::SameLine(); + ImGui::Text("Scroll from code"); ImGui::SameLine(); + ImGui::SmallButton(">>"); if (ImGui::IsItemActive()) { scroll_x_delta = +ImGui::GetIO().DeltaTime * 1000.0f; } ImGui::SameLine(); + ImGui::Text("%.0f/%.0f", scroll_x, scroll_max_x); + if (scroll_x_delta != 0.0f) + { + ImGui::BeginChild("scrolling"); // Demonstrate a trick: you can use Begin to set yourself in the context of another window (here we are already out of your child window) + ImGui::SetScrollX(ImGui::GetScrollX() + scroll_x_delta); + ImGui::EndChild(); + } + ImGui::Spacing(); + + static bool show_horizontal_contents_size_demo_window = false; + ImGui::Checkbox("Show Horizontal contents size demo window", &show_horizontal_contents_size_demo_window); + + if (show_horizontal_contents_size_demo_window) + { + static bool show_h_scrollbar = true; + static bool show_button = true; + static bool show_tree_nodes = true; + static bool show_text_wrapped = false; + static bool show_columns = true; + static bool show_tab_bar = true; + static bool show_child = false; + static bool explicit_content_size = false; + static float contents_size_x = 300.0f; + if (explicit_content_size) + ImGui::SetNextWindowContentSize(ImVec2(contents_size_x, 0.0f)); + ImGui::Begin("Horizontal contents size demo window", &show_horizontal_contents_size_demo_window, show_h_scrollbar ? ImGuiWindowFlags_HorizontalScrollbar : 0); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 0)); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 0)); + HelpMarker("Test of different widgets react and impact the work rectangle growing when horizontal scrolling is enabled.\n\nUse 'Metrics->Tools->Show windows rectangles' to visualize rectangles."); + ImGui::Checkbox("H-scrollbar", &show_h_scrollbar); + ImGui::Checkbox("Button", &show_button); // Will grow contents size (unless explicitly overwritten) + ImGui::Checkbox("Tree nodes", &show_tree_nodes); // Will grow contents size and display highlight over full width + ImGui::Checkbox("Text wrapped", &show_text_wrapped);// Will grow and use contents size + ImGui::Checkbox("Columns", &show_columns); // Will use contents size + ImGui::Checkbox("Tab bar", &show_tab_bar); // Will use contents size + ImGui::Checkbox("Child", &show_child); // Will grow and use contents size + ImGui::Checkbox("Explicit content size", &explicit_content_size); + ImGui::Text("Scroll %.1f/%.1f %.1f/%.1f", ImGui::GetScrollX(), ImGui::GetScrollMaxX(), ImGui::GetScrollY(), ImGui::GetScrollMaxY()); + if (explicit_content_size) + { + ImGui::SameLine(); + ImGui::SetNextItemWidth(100); + ImGui::DragFloat("##csx", &contents_size_x); + ImVec2 p = ImGui::GetCursorScreenPos(); + ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x + 10, p.y + 10), IM_COL32_WHITE); + ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(p.x + contents_size_x - 10, p.y), ImVec2(p.x + contents_size_x, p.y + 10), IM_COL32_WHITE); + ImGui::Dummy(ImVec2(0, 10)); + } + ImGui::PopStyleVar(2); + ImGui::Separator(); + if (show_button) + { + ImGui::Button("this is a 300-wide button", ImVec2(300, 0)); + } + if (show_tree_nodes) + { + bool open = true; + if (ImGui::TreeNode("this is a tree node")) + { + if (ImGui::TreeNode("another one of those tree node...")) + { + ImGui::Text("Some tree contents"); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + ImGui::CollapsingHeader("CollapsingHeader", &open); + } + if (show_text_wrapped) + { + ImGui::TextWrapped("This text should automatically wrap on the edge of the work rectangle."); + } + if (show_columns) + { + ImGui::Columns(4); + for (int n = 0; n < 4; n++) + { + ImGui::Text("Width %.2f", ImGui::GetColumnWidth()); + ImGui::NextColumn(); + } + ImGui::Columns(1); + } + if (show_tab_bar && ImGui::BeginTabBar("Hello")) + { + if (ImGui::BeginTabItem("OneOneOne")) { ImGui::EndTabItem(); } + if (ImGui::BeginTabItem("TwoTwoTwo")) { ImGui::EndTabItem(); } + if (ImGui::BeginTabItem("ThreeThreeThree")) { ImGui::EndTabItem(); } + if (ImGui::BeginTabItem("FourFourFour")) { ImGui::EndTabItem(); } + ImGui::EndTabBar(); + } + if (show_child) + { + ImGui::BeginChild("child", ImVec2(0,0), true); + ImGui::EndChild(); + } + ImGui::End(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Clipping")) + { + static ImVec2 size(100, 100), offset(50, 20); + ImGui::TextWrapped("On a per-widget basis we are occasionally clipping text CPU-side if it won't fit in its frame. Otherwise we are doing coarser clipping + passing a scissor rectangle to the renderer. The system is designed to try minimizing both execution and CPU/GPU rendering cost."); + ImGui::DragFloat2("size", (float*)&size, 0.5f, 1.0f, 200.0f, "%.0f"); + ImGui::TextWrapped("(Click and drag)"); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImVec4 clip_rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); + ImGui::InvisibleButton("##dummy", size); + if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { offset.x += ImGui::GetIO().MouseDelta.x; offset.y += ImGui::GetIO().MouseDelta.y; } + ImGui::GetWindowDrawList()->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(90, 90, 120, 255)); + ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.0f, ImVec2(pos.x + offset.x, pos.y + offset.y), IM_COL32(255, 255, 255, 255), "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect); + ImGui::TreePop(); + } +} + +static void ShowDemoWindowPopups() +{ + if (!ImGui::CollapsingHeader("Popups & Modal windows")) + return; + + // The properties of popups windows are: + // - They block normal mouse hovering detection outside them. (*) + // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. + // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as we are used to with regular Begin() calls. + // User can manipulate the visibility state by calling OpenPopup(). + // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. + // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. + + // Typical use for regular windows: + // bool my_tool_is_active = false; if (ImGui::Button("Open")) my_tool_is_active = true; [...] if (my_tool_is_active) Begin("My Tool", &my_tool_is_active) { [...] } End(); + // Typical use for popups: + // if (ImGui::Button("Open")) ImGui::OpenPopup("MyPopup"); if (ImGui::BeginPopup("MyPopup") { [...] EndPopup(); } + + // With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state. + // This may be a bit confusing at first but it should quickly make sense. Follow on the examples below. + + if (ImGui::TreeNode("Popups")) + { + ImGui::TextWrapped("When a popup is active, it inhibits interacting with windows that are behind the popup. Clicking outside the popup closes it."); + + static int selected_fish = -1; + const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" }; + static bool toggles[] = { true, false, false, false, false }; + + // Simple selection popup + // (If you want to show the current selection inside the Button itself, you may want to build a string using the "###" operator to preserve a constant ID with a variable label) + if (ImGui::Button("Select..")) + ImGui::OpenPopup("my_select_popup"); + ImGui::SameLine(); + ImGui::TextUnformatted(selected_fish == -1 ? "" : names[selected_fish]); + if (ImGui::BeginPopup("my_select_popup")) + { + ImGui::Text("Aquarium"); + ImGui::Separator(); + for (int i = 0; i < IM_ARRAYSIZE(names); i++) + if (ImGui::Selectable(names[i])) + selected_fish = i; + ImGui::EndPopup(); + } + + // Showing a menu with toggles + if (ImGui::Button("Toggle..")) + ImGui::OpenPopup("my_toggle_popup"); + if (ImGui::BeginPopup("my_toggle_popup")) + { + for (int i = 0; i < IM_ARRAYSIZE(names); i++) + ImGui::MenuItem(names[i], "", &toggles[i]); + if (ImGui::BeginMenu("Sub-menu")) + { + ImGui::MenuItem("Click me"); + ImGui::EndMenu(); + } + + ImGui::Separator(); + ImGui::Text("Tooltip here"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("I am a tooltip over a popup"); + + if (ImGui::Button("Stacked Popup")) + ImGui::OpenPopup("another popup"); + if (ImGui::BeginPopup("another popup")) + { + for (int i = 0; i < IM_ARRAYSIZE(names); i++) + ImGui::MenuItem(names[i], "", &toggles[i]); + if (ImGui::BeginMenu("Sub-menu")) + { + ImGui::MenuItem("Click me"); + if (ImGui::Button("Stacked Popup")) + ImGui::OpenPopup("another popup"); + if (ImGui::BeginPopup("another popup")) + { + ImGui::Text("I am the last one here."); + ImGui::EndPopup(); + } + ImGui::EndMenu(); + } + ImGui::EndPopup(); + } + ImGui::EndPopup(); + } + + // Call the more complete ShowExampleMenuFile which we use in various places of this demo + if (ImGui::Button("File Menu..")) + ImGui::OpenPopup("my_file_popup"); + if (ImGui::BeginPopup("my_file_popup")) + { + ShowExampleMenuFile(); + ImGui::EndPopup(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Context menus")) + { + // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing: + // if (IsItemHovered() && IsMouseReleased(0)) + // OpenPopup(id); + // return BeginPopup(id); + // For more advanced uses you may want to replicate and cuztomize this code. This the comments inside BeginPopupContextItem() implementation. + static float value = 0.5f; + ImGui::Text("Value = %.3f (<-- right-click here)", value); + if (ImGui::BeginPopupContextItem("item context menu")) + { + if (ImGui::Selectable("Set to zero")) value = 0.0f; + if (ImGui::Selectable("Set to PI")) value = 3.1415f; + ImGui::SetNextItemWidth(-1); + ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); + ImGui::EndPopup(); + } + + // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the Begin call. + // So here we will make it that clicking on the text field with the right mouse button (1) will toggle the visibility of the popup above. + ImGui::Text("(You can also right-click me to open the same popup as above.)"); + ImGui::OpenPopupOnItemClick("item context menu", 1); + + // When used after an item that has an ID (here the Button), we can skip providing an ID to BeginPopupContextItem(). + // BeginPopupContextItem() will use the last item ID as the popup ID. + // In addition here, we want to include your editable label inside the button label. We use the ### operator to override the ID (read FAQ about ID for details) + static char name[32] = "Label1"; + char buf[64]; sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label + ImGui::Button(buf); + if (ImGui::BeginPopupContextItem()) + { + ImGui::Text("Edit name:"); + ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + ImGui::SameLine(); ImGui::Text("(<-- right-click here)"); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Modals")) + { + ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside the window."); + + if (ImGui::Button("Delete..")) + ImGui::OpenPopup("Delete?"); + + if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) + { + ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); + ImGui::Separator(); + + //static int dummy_i = 0; + //ImGui::Combo("Combo", &dummy_i, "Delete\0Delete harder\0"); + + static bool dont_ask_me_next_time = false; + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); + ImGui::Checkbox("Don't ask me next time", &dont_ask_me_next_time); + ImGui::PopStyleVar(); + + if (ImGui::Button("OK", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } + ImGui::SetItemDefaultFocus(); + ImGui::SameLine(); + if (ImGui::Button("Cancel", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } + ImGui::EndPopup(); + } + + if (ImGui::Button("Stacked modals..")) + ImGui::OpenPopup("Stacked 1"); + if (ImGui::BeginPopupModal("Stacked 1", NULL, ImGuiWindowFlags_MenuBar)) + { + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Dummy menu item")) {} + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + ImGui::Text("Hello from Stacked The First\nUsing style.Colors[ImGuiCol_ModalWindowDimBg] behind it."); + + // Testing behavior of widgets stacking their own regular popups over the modal. + static int item = 1; + static float color[4] = { 0.4f,0.7f,0.0f,0.5f }; + ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); + ImGui::ColorEdit4("color", color); + + if (ImGui::Button("Add another modal..")) + ImGui::OpenPopup("Stacked 2"); + + // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which will close the popup. + // Note that the visibility state of popups is owned by imgui, so the input value of the bool actually doesn't matter here. + bool dummy_open = true; + if (ImGui::BeginPopupModal("Stacked 2", &dummy_open)) + { + ImGui::Text("Hello from Stacked The Second!"); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Menus inside a regular window")) + { + ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); + ImGui::Separator(); + // NB: As a quirk in this very specific example, we want to differentiate the parent of this menu from the parent of the various popup menus above. + // To do so we are encloding the items in a PushID()/PopID() block to make them two different menusets. If we don't, opening any popup above and hovering our menu here + // would open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, which is the desired behavior for regular menus. + ImGui::PushID("foo"); + ImGui::MenuItem("Menu item", "CTRL+M"); + if (ImGui::BeginMenu("Menu inside a regular window")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + ImGui::PopID(); + ImGui::Separator(); + ImGui::TreePop(); + } +} + +static void ShowDemoWindowColumns() +{ + if (!ImGui::CollapsingHeader("Columns")) + return; + + ImGui::PushID("Columns"); + + static bool disable_indent = false; + ImGui::Checkbox("Disable tree indentation", &disable_indent); + ImGui::SameLine(); + HelpMarker("Disable the indenting of tree nodes so demo columns can use the full window width."); + if (disable_indent) + ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, 0.0f); + + // Basic columns + if (ImGui::TreeNode("Basic")) + { + ImGui::Text("Without border:"); + ImGui::Columns(3, "mycolumns3", false); // 3-ways, no border + ImGui::Separator(); + for (int n = 0; n < 14; n++) + { + char label[32]; + sprintf(label, "Item %d", n); + if (ImGui::Selectable(label)) {} + //if (ImGui::Button(label, ImVec2(-FLT_MIN,0.0f))) {} + ImGui::NextColumn(); + } + ImGui::Columns(1); + ImGui::Separator(); + + ImGui::Text("With border:"); + ImGui::Columns(4, "mycolumns"); // 4-ways, with border + ImGui::Separator(); + ImGui::Text("ID"); ImGui::NextColumn(); + ImGui::Text("Name"); ImGui::NextColumn(); + ImGui::Text("Path"); ImGui::NextColumn(); + ImGui::Text("Hovered"); ImGui::NextColumn(); + ImGui::Separator(); + const char* names[3] = { "One", "Two", "Three" }; + const char* paths[3] = { "/path/one", "/path/two", "/path/three" }; + static int selected = -1; + for (int i = 0; i < 3; i++) + { + char label[32]; + sprintf(label, "%04d", i); + if (ImGui::Selectable(label, selected == i, ImGuiSelectableFlags_SpanAllColumns)) + selected = i; + bool hovered = ImGui::IsItemHovered(); + ImGui::NextColumn(); + ImGui::Text(names[i]); ImGui::NextColumn(); + ImGui::Text(paths[i]); ImGui::NextColumn(); + ImGui::Text("%d", hovered); ImGui::NextColumn(); + } + ImGui::Columns(1); + ImGui::Separator(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Borders")) + { + // NB: Future columns API should allow automatic horizontal borders. + static bool h_borders = true; + static bool v_borders = true; + static int columns_count = 4; + const int lines_count = 3; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::DragInt("##columns_count", &columns_count, 0.1f, 2, 10, "%d columns"); + if (columns_count < 2) + columns_count = 2; + ImGui::SameLine(); + ImGui::Checkbox("horizontal", &h_borders); + ImGui::SameLine(); + ImGui::Checkbox("vertical", &v_borders); + ImGui::Columns(columns_count, NULL, v_borders); + for (int i = 0; i < columns_count * lines_count; i++) + { + if (h_borders && ImGui::GetColumnIndex() == 0) + ImGui::Separator(); + ImGui::Text("%c%c%c", 'a' + i, 'a' + i, 'a' + i); + ImGui::Text("Width %.2f", ImGui::GetColumnWidth()); + ImGui::Text("Avail %.2f", ImGui::GetContentRegionAvail().x); + ImGui::Text("Offset %.2f", ImGui::GetColumnOffset()); + ImGui::Text("Long text that is likely to clip"); + ImGui::Button("Button", ImVec2(-FLT_MIN, 0.0f)); + ImGui::NextColumn(); + } + ImGui::Columns(1); + if (h_borders) + ImGui::Separator(); + ImGui::TreePop(); + } + + // Create multiple items in a same cell before switching to next column + if (ImGui::TreeNode("Mixed items")) + { + ImGui::Columns(3, "mixed"); + ImGui::Separator(); + + ImGui::Text("Hello"); + ImGui::Button("Banana"); + ImGui::NextColumn(); + + ImGui::Text("ImGui"); + ImGui::Button("Apple"); + static float foo = 1.0f; + ImGui::InputFloat("red", &foo, 0.05f, 0, "%.3f"); + ImGui::Text("An extra line here."); + ImGui::NextColumn(); + + ImGui::Text("Sailor"); + ImGui::Button("Corniflower"); + static float bar = 1.0f; + ImGui::InputFloat("blue", &bar, 0.05f, 0, "%.3f"); + ImGui::NextColumn(); + + if (ImGui::CollapsingHeader("Category A")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn(); + if (ImGui::CollapsingHeader("Category B")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn(); + if (ImGui::CollapsingHeader("Category C")) { ImGui::Text("Blah blah blah"); } ImGui::NextColumn(); + ImGui::Columns(1); + ImGui::Separator(); + ImGui::TreePop(); + } + + // Word wrapping + if (ImGui::TreeNode("Word-wrapping")) + { + ImGui::Columns(2, "word-wrapping"); + ImGui::Separator(); + ImGui::TextWrapped("The quick brown fox jumps over the lazy dog."); + ImGui::TextWrapped("Hello Left"); + ImGui::NextColumn(); + ImGui::TextWrapped("The quick brown fox jumps over the lazy dog."); + ImGui::TextWrapped("Hello Right"); + ImGui::Columns(1); + ImGui::Separator(); + ImGui::TreePop(); + } + + // Scrolling columns + /* + if (ImGui::TreeNode("Vertical Scrolling")) + { + ImGui::BeginChild("##header", ImVec2(0, ImGui::GetTextLineHeightWithSpacing()+ImGui::GetStyle().ItemSpacing.y)); + ImGui::Columns(3); + ImGui::Text("ID"); ImGui::NextColumn(); + ImGui::Text("Name"); ImGui::NextColumn(); + ImGui::Text("Path"); ImGui::NextColumn(); + ImGui::Columns(1); + ImGui::Separator(); + ImGui::EndChild(); + ImGui::BeginChild("##scrollingregion", ImVec2(0, 60)); + ImGui::Columns(3); + for (int i = 0; i < 10; i++) + { + ImGui::Text("%04d", i); ImGui::NextColumn(); + ImGui::Text("Foobar"); ImGui::NextColumn(); + ImGui::Text("/path/foobar/%04d/", i); ImGui::NextColumn(); + } + ImGui::Columns(1); + ImGui::EndChild(); + ImGui::TreePop(); + } + */ + + if (ImGui::TreeNode("Horizontal Scrolling")) + { + ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f)); + ImGui::BeginChild("##ScrollingRegion", ImVec2(0, ImGui::GetFontSize() * 20), false, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::Columns(10); + int ITEMS_COUNT = 2000; + ImGuiListClipper clipper(ITEMS_COUNT); // Also demonstrate using the clipper for large list + while (clipper.Step()) + { + for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) + for (int j = 0; j < 10; j++) + { + ImGui::Text("Line %d Column %d...", i, j); + ImGui::NextColumn(); + } + } + ImGui::Columns(1); + ImGui::EndChild(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Tree")) + { + ImGui::Columns(2, "tree", true); + for (int x = 0; x < 3; x++) + { + bool open1 = ImGui::TreeNode((void*)(intptr_t)x, "Node%d", x); + ImGui::NextColumn(); + ImGui::Text("Node contents"); + ImGui::NextColumn(); + if (open1) + { + for (int y = 0; y < 3; y++) + { + bool open2 = ImGui::TreeNode((void*)(intptr_t)y, "Node%d.%d", x, y); + ImGui::NextColumn(); + ImGui::Text("Node contents"); + if (open2) + { + ImGui::Text("Even more contents"); + if (ImGui::TreeNode("Tree in column")) + { + ImGui::Text("The quick brown fox jumps over the lazy dog"); + ImGui::TreePop(); + } + } + ImGui::NextColumn(); + if (open2) + ImGui::TreePop(); + } + ImGui::TreePop(); + } + } + ImGui::Columns(1); + ImGui::TreePop(); + } + + if (disable_indent) + ImGui::PopStyleVar(); + ImGui::PopID(); +} + +static void ShowDemoWindowMisc() +{ + if (ImGui::CollapsingHeader("Filtering")) + { + // Helper class to easy setup a text filter. + // You may want to implement a more feature-full filtering scheme in your own application. + static ImGuiTextFilter filter; + ImGui::Text("Filter usage:\n" + " \"\" display all lines\n" + " \"xxx\" display lines containing \"xxx\"\n" + " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n" + " \"-xxx\" hide lines containing \"xxx\""); + filter.Draw(); + const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" }; + for (int i = 0; i < IM_ARRAYSIZE(lines); i++) + if (filter.PassFilter(lines[i])) + ImGui::BulletText("%s", lines[i]); + } + + if (ImGui::CollapsingHeader("Inputs, Navigation & Focus")) + { + ImGuiIO& io = ImGui::GetIO(); + + // Display ImGuiIO output flags + ImGui::Text("WantCaptureMouse: %d", io.WantCaptureMouse); + ImGui::Text("WantCaptureKeyboard: %d", io.WantCaptureKeyboard); + ImGui::Text("WantTextInput: %d", io.WantTextInput); + ImGui::Text("WantSetMousePos: %d", io.WantSetMousePos); + ImGui::Text("NavActive: %d, NavVisible: %d", io.NavActive, io.NavVisible); + + // Display Keyboard/Mouse state + if (ImGui::TreeNode("Keyboard, Mouse & Navigation State")) + { + if (ImGui::IsMousePosValid()) + ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); + else + ImGui::Text("Mouse pos: "); + ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); + ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (io.MouseDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } + ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse dbl-clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); + + ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (io.KeysDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("%d (0x%X) (%.02f secs)", i, i, io.KeysDownDuration[i]); } + ImGui::Text("Keys pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyPressed(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } + ImGui::Text("Keys release:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyReleased(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } + ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); + ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. + + ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputs[i]); } + ImGui::Text("NavInputs pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] == 0.0f) { ImGui::SameLine(); ImGui::Text("[%d]", i); } + ImGui::Text("NavInputs duration:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputsDownDuration[i]); } + + ImGui::Button("Hovering me sets the\nkeyboard capture flag"); + if (ImGui::IsItemHovered()) + ImGui::CaptureKeyboardFromApp(true); + ImGui::SameLine(); + ImGui::Button("Holding me clears the\nthe keyboard capture flag"); + if (ImGui::IsItemActive()) + ImGui::CaptureKeyboardFromApp(false); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Tabbing")) + { + ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields."); + static char buf[32] = "dummy"; + ImGui::InputText("1", buf, IM_ARRAYSIZE(buf)); + ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); + ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); + ImGui::PushAllowKeyboardFocus(false); + ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf)); + //ImGui::SameLine(); HelpMarker("Use ImGui::PushAllowKeyboardFocus(bool)\nto disable tabbing through certain widgets."); + ImGui::PopAllowKeyboardFocus(); + ImGui::InputText("5", buf, IM_ARRAYSIZE(buf)); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Focus from code")) + { + bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine(); + bool focus_2 = ImGui::Button("Focus on 2"); ImGui::SameLine(); + bool focus_3 = ImGui::Button("Focus on 3"); + int has_focus = 0; + static char buf[128] = "click on a button to set focus"; + + if (focus_1) ImGui::SetKeyboardFocusHere(); + ImGui::InputText("1", buf, IM_ARRAYSIZE(buf)); + if (ImGui::IsItemActive()) has_focus = 1; + + if (focus_2) ImGui::SetKeyboardFocusHere(); + ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); + if (ImGui::IsItemActive()) has_focus = 2; + + ImGui::PushAllowKeyboardFocus(false); + if (focus_3) ImGui::SetKeyboardFocusHere(); + ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf)); + if (ImGui::IsItemActive()) has_focus = 3; + ImGui::PopAllowKeyboardFocus(); + + if (has_focus) + ImGui::Text("Item with focus: %d", has_focus); + else + ImGui::Text("Item with focus: "); + + // Use >= 0 parameter to SetKeyboardFocusHere() to focus an upcoming item + static float f3[3] = { 0.0f, 0.0f, 0.0f }; + int focus_ahead = -1; + if (ImGui::Button("Focus on X")) { focus_ahead = 0; } ImGui::SameLine(); + if (ImGui::Button("Focus on Y")) { focus_ahead = 1; } ImGui::SameLine(); + if (ImGui::Button("Focus on Z")) { focus_ahead = 2; } + if (focus_ahead != -1) ImGui::SetKeyboardFocusHere(focus_ahead); + ImGui::SliderFloat3("Float3", &f3[0], 0.0f, 1.0f); + + ImGui::TextWrapped("NB: Cursor & selection are preserved when refocusing last used item in code."); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Dragging")) + { + ImGui::TextWrapped("You can use ImGui::GetMouseDragDelta(0) to query for the dragged amount on any widget."); + for (int button = 0; button < 3; button++) + ImGui::Text("IsMouseDragging(%d):\n w/ default threshold: %d,\n w/ zero threshold: %d\n w/ large threshold: %d", + button, ImGui::IsMouseDragging(button), ImGui::IsMouseDragging(button, 0.0f), ImGui::IsMouseDragging(button, 20.0f)); + + ImGui::Button("Drag Me"); + if (ImGui::IsItemActive()) + ImGui::GetForegroundDrawList()->AddLine(io.MouseClickedPos[0], io.MousePos, ImGui::GetColorU32(ImGuiCol_Button), 4.0f); // Draw a line between the button and the mouse cursor + + // Drag operations gets "unlocked" when the mouse has moved past a certain threshold (the default threshold is stored in io.MouseDragThreshold) + // You can request a lower or higher threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta() + ImVec2 value_raw = ImGui::GetMouseDragDelta(0, 0.0f); + ImVec2 value_with_lock_threshold = ImGui::GetMouseDragDelta(0); + ImVec2 mouse_delta = io.MouseDelta; + ImGui::Text("GetMouseDragDelta(0):\n w/ default threshold: (%.1f, %.1f),\n w/ zero threshold: (%.1f, %.1f)\nMouseDelta: (%.1f, %.1f)", value_with_lock_threshold.x, value_with_lock_threshold.y, value_raw.x, value_raw.y, mouse_delta.x, mouse_delta.y); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Mouse cursors")) + { + const char* mouse_cursors_names[] = { "Arrow", "TextInput", "Move", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand" }; + IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); + + ImGui::Text("Current mouse cursor = %d: %s", ImGui::GetMouseCursor(), mouse_cursors_names[ImGui::GetMouseCursor()]); + ImGui::Text("Hover to see mouse cursors:"); + ImGui::SameLine(); HelpMarker("Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, otherwise your backend needs to handle it."); + for (int i = 0; i < ImGuiMouseCursor_COUNT; i++) + { + char label[32]; + sprintf(label, "Mouse cursor %d: %s", i, mouse_cursors_names[i]); + ImGui::Bullet(); ImGui::Selectable(label, false); + if (ImGui::IsItemHovered() || ImGui::IsItemFocused()) + ImGui::SetMouseCursor(i); + } + ImGui::TreePop(); + } + } +} + +//----------------------------------------------------------------------------- +// [SECTION] About Window / ShowAboutWindow() +// Access from Dear ImGui Demo -> Tools -> About +//----------------------------------------------------------------------------- + +void ImGui::ShowAboutWindow(bool* p_open) +{ + if (!ImGui::Begin("About Dear ImGui", p_open, ImGuiWindowFlags_AlwaysAutoResize)) + { + ImGui::End(); + return; + } + ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); + ImGui::Separator(); + ImGui::Text("By Omar Cornut and all Dear ImGui contributors."); + ImGui::Text("Dear ImGui is licensed under the MIT License, see LICENSE for more information."); + + static bool show_config_info = false; + ImGui::Checkbox("Config/Build Information", &show_config_info); + if (show_config_info) + { + ImGuiIO& io = ImGui::GetIO(); + ImGuiStyle& style = ImGui::GetStyle(); + + bool copy_to_clipboard = ImGui::Button("Copy to clipboard"); + ImGui::BeginChildFrame(ImGui::GetID("cfginfos"), ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 18), ImGuiWindowFlags_NoMove); + if (copy_to_clipboard) + { + ImGui::LogToClipboard(); + ImGui::LogText("```\n"); // Back quotes will make the text appears without formatting when pasting to GitHub + } + + ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); + ImGui::Separator(); + ImGui::Text("sizeof(size_t): %d, sizeof(ImDrawIdx): %d, sizeof(ImDrawVert): %d", (int)sizeof(size_t), (int)sizeof(ImDrawIdx), (int)sizeof(ImDrawVert)); + ImGui::Text("define: __cplusplus=%d", (int)__cplusplus); +#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_WIN32_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_WIN32_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_MATH_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_MATH_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_DEFAULT_ALLOCATORS + ImGui::Text("define: IMGUI_DISABLE_DEFAULT_ALLOCATORS"); +#endif +#ifdef IMGUI_USE_BGRA_PACKED_COLOR + ImGui::Text("define: IMGUI_USE_BGRA_PACKED_COLOR"); +#endif +#ifdef _WIN32 + ImGui::Text("define: _WIN32"); +#endif +#ifdef _WIN64 + ImGui::Text("define: _WIN64"); +#endif +#ifdef __linux__ + ImGui::Text("define: __linux__"); +#endif +#ifdef __APPLE__ + ImGui::Text("define: __APPLE__"); +#endif +#ifdef _MSC_VER + ImGui::Text("define: _MSC_VER=%d", _MSC_VER); +#endif +#ifdef __MINGW32__ + ImGui::Text("define: __MINGW32__"); +#endif +#ifdef __MINGW64__ + ImGui::Text("define: __MINGW64__"); +#endif +#ifdef __GNUC__ + ImGui::Text("define: __GNUC__=%d", (int)__GNUC__); +#endif +#ifdef __clang_version__ + ImGui::Text("define: __clang_version__=%s", __clang_version__); +#endif + ImGui::Separator(); + ImGui::Text("io.BackendPlatformName: %s", io.BackendPlatformName ? io.BackendPlatformName : "NULL"); + ImGui::Text("io.BackendRendererName: %s", io.BackendRendererName ? io.BackendRendererName : "NULL"); + ImGui::Text("io.ConfigFlags: 0x%08X", io.ConfigFlags); + if (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) ImGui::Text(" NavEnableKeyboard"); + if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) ImGui::Text(" NavEnableGamepad"); + if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) ImGui::Text(" NavEnableSetMousePos"); + if (io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard) ImGui::Text(" NavNoCaptureKeyboard"); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) ImGui::Text(" NoMouse"); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) ImGui::Text(" NoMouseCursorChange"); + if (io.MouseDrawCursor) ImGui::Text("io.MouseDrawCursor"); + if (io.ConfigMacOSXBehaviors) ImGui::Text("io.ConfigMacOSXBehaviors"); + if (io.ConfigInputTextCursorBlink) ImGui::Text("io.ConfigInputTextCursorBlink"); + if (io.ConfigWindowsResizeFromEdges) ImGui::Text("io.ConfigWindowsResizeFromEdges"); + if (io.ConfigWindowsMoveFromTitleBarOnly) ImGui::Text("io.ConfigWindowsMoveFromTitleBarOnly"); + if (io.ConfigWindowsMemoryCompactTimer >= 0.0f) ImGui::Text("io.ConfigWindowsMemoryCompactTimer = %.1ff", io.ConfigWindowsMemoryCompactTimer); + ImGui::Text("io.BackendFlags: 0x%08X", io.BackendFlags); + if (io.BackendFlags & ImGuiBackendFlags_HasGamepad) ImGui::Text(" HasGamepad"); + if (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) ImGui::Text(" HasMouseCursors"); + if (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos) ImGui::Text(" HasSetMousePos"); + if (io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) ImGui::Text(" RendererHasVtxOffset"); + ImGui::Separator(); + ImGui::Text("io.Fonts: %d fonts, Flags: 0x%08X, TexSize: %d,%d", io.Fonts->Fonts.Size, io.Fonts->Flags, io.Fonts->TexWidth, io.Fonts->TexHeight); + ImGui::Text("io.DisplaySize: %.2f,%.2f", io.DisplaySize.x, io.DisplaySize.y); + ImGui::Text("io.DisplayFramebufferScale: %.2f,%.2f", io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); + ImGui::Separator(); + ImGui::Text("style.WindowPadding: %.2f,%.2f", style.WindowPadding.x, style.WindowPadding.y); + ImGui::Text("style.WindowBorderSize: %.2f", style.WindowBorderSize); + ImGui::Text("style.FramePadding: %.2f,%.2f", style.FramePadding.x, style.FramePadding.y); + ImGui::Text("style.FrameRounding: %.2f", style.FrameRounding); + ImGui::Text("style.FrameBorderSize: %.2f", style.FrameBorderSize); + ImGui::Text("style.ItemSpacing: %.2f,%.2f", style.ItemSpacing.x, style.ItemSpacing.y); + ImGui::Text("style.ItemInnerSpacing: %.2f,%.2f", style.ItemInnerSpacing.x, style.ItemInnerSpacing.y); + + if (copy_to_clipboard) + { + ImGui::LogText("\n```\n"); + ImGui::LogFinish(); + } + ImGui::EndChildFrame(); + } + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Style Editor / ShowStyleEditor() +//----------------------------------------------------------------------------- +// - ShowStyleSelector() +// - ShowFontSelector() +// - ShowStyleEditor() +//----------------------------------------------------------------------------- + +// Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options. +// Here we use the simplified Combo() api that packs items into a single literal string. Useful for quick combo boxes where the choices are known locally. +bool ImGui::ShowStyleSelector(const char* label) +{ + static int style_idx = -1; + if (ImGui::Combo(label, &style_idx, "Classic\0Dark\0Light\0")) + { + switch (style_idx) + { + case 0: ImGui::StyleColorsClassic(); break; + case 1: ImGui::StyleColorsDark(); break; + case 2: ImGui::StyleColorsLight(); break; + } + return true; + } + return false; +} + +// Demo helper function to select among loaded fonts. +// Here we use the regular BeginCombo()/EndCombo() api which is more the more flexible one. +void ImGui::ShowFontSelector(const char* label) +{ + ImGuiIO& io = ImGui::GetIO(); + ImFont* font_current = ImGui::GetFont(); + if (ImGui::BeginCombo(label, font_current->GetDebugName())) + { + for (int n = 0; n < io.Fonts->Fonts.Size; n++) + { + ImFont* font = io.Fonts->Fonts[n]; + ImGui::PushID((void*)font); + if (ImGui::Selectable(font->GetDebugName(), font == font_current)) + io.FontDefault = font; + ImGui::PopID(); + } + ImGui::EndCombo(); + } + ImGui::SameLine(); + HelpMarker( + "- Load additional fonts with io.Fonts->AddFontFromFileTTF().\n" + "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n" + "- Read FAQ and documentation in misc/fonts/ for more details.\n" + "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame()."); +} + +void ImGui::ShowStyleEditor(ImGuiStyle* ref) +{ + // You can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it compares to an internally stored reference) + ImGuiStyle& style = ImGui::GetStyle(); + static ImGuiStyle ref_saved_style; + + // Default to using internal storage as reference + static bool init = true; + if (init && ref == NULL) + ref_saved_style = style; + init = false; + if (ref == NULL) + ref = &ref_saved_style; + + ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.50f); + + if (ImGui::ShowStyleSelector("Colors##Selector")) + ref_saved_style = style; + ImGui::ShowFontSelector("Fonts##Selector"); + + // Simplified Settings + if (ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f")) + style.GrabRounding = style.FrameRounding; // Make GrabRounding always the same value as FrameRounding + { bool window_border = (style.WindowBorderSize > 0.0f); if (ImGui::Checkbox("WindowBorder", &window_border)) style.WindowBorderSize = window_border ? 1.0f : 0.0f; } + ImGui::SameLine(); + { bool frame_border = (style.FrameBorderSize > 0.0f); if (ImGui::Checkbox("FrameBorder", &frame_border)) style.FrameBorderSize = frame_border ? 1.0f : 0.0f; } + ImGui::SameLine(); + { bool popup_border = (style.PopupBorderSize > 0.0f); if (ImGui::Checkbox("PopupBorder", &popup_border)) style.PopupBorderSize = popup_border ? 1.0f : 0.0f; } + + // Save/Revert button + if (ImGui::Button("Save Ref")) + *ref = ref_saved_style = style; + ImGui::SameLine(); + if (ImGui::Button("Revert Ref")) + style = *ref; + ImGui::SameLine(); + HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. Use \"Export Colors\" below to save them somewhere."); + + ImGui::Separator(); + + if (ImGui::BeginTabBar("##tabs", ImGuiTabBarFlags_None)) + { + if (ImGui::BeginTabItem("Sizes")) + { + ImGui::Text("Main"); + ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f"); + ImGui::SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f"); + ImGui::SliderFloat("ScrollbarSize", &style.ScrollbarSize, 1.0f, 20.0f, "%.0f"); + ImGui::SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f"); + ImGui::Text("Borders"); + ImGui::SliderFloat("WindowBorderSize", &style.WindowBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::SliderFloat("ChildBorderSize", &style.ChildBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::Text("Rounding"); + ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("ChildRounding", &style.ChildRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f"); + ImGui::Text("Alignment"); + ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); + int window_menu_button_position = style.WindowMenuButtonPosition + 1; + if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0")) + style.WindowMenuButtonPosition = window_menu_button_position - 1; + ImGui::Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0"); + ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content."); + ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content."); + ImGui::Text("Safe Area Padding"); ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); + ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Colors")) + { + static int output_dest = 0; + static bool output_only_modified = true; + if (ImGui::Button("Export Unsaved")) + { + if (output_dest == 0) + ImGui::LogToClipboard(); + else + ImGui::LogToTTY(); + ImGui::LogText("ImVec4* colors = ImGui::GetStyle().Colors;" IM_NEWLINE); + for (int i = 0; i < ImGuiCol_COUNT; i++) + { + const ImVec4& col = style.Colors[i]; + const char* name = ImGui::GetStyleColorName(i); + if (!output_only_modified || memcmp(&col, &ref->Colors[i], sizeof(ImVec4)) != 0) + ImGui::LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE, name, 23 - (int)strlen(name), "", col.x, col.y, col.z, col.w); + } + ImGui::LogFinish(); + } + ImGui::SameLine(); ImGui::SetNextItemWidth(120); ImGui::Combo("##output_type", &output_dest, "To Clipboard\0To TTY\0"); + ImGui::SameLine(); ImGui::Checkbox("Only Modified Colors", &output_only_modified); + + static ImGuiTextFilter filter; + filter.Draw("Filter colors", ImGui::GetFontSize() * 16); + + static ImGuiColorEditFlags alpha_flags = 0; + ImGui::RadioButton("Opaque", &alpha_flags, 0); ImGui::SameLine(); + ImGui::RadioButton("Alpha", &alpha_flags, ImGuiColorEditFlags_AlphaPreview); ImGui::SameLine(); + ImGui::RadioButton("Both", &alpha_flags, ImGuiColorEditFlags_AlphaPreviewHalf); ImGui::SameLine(); + HelpMarker("In the color list:\nLeft-click on colored square to open color picker,\nRight-click to open edit options menu."); + + ImGui::BeginChild("##colors", ImVec2(0, 0), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); + ImGui::PushItemWidth(-160); + for (int i = 0; i < ImGuiCol_COUNT; i++) + { + const char* name = ImGui::GetStyleColorName(i); + if (!filter.PassFilter(name)) + continue; + ImGui::PushID(i); + ImGui::ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags); + if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0) + { + // Tips: in a real user application, you may want to merge and use an icon font into the main font, so instead of "Save"/"Revert" you'd use icons. + // Read the FAQ and misc/fonts/README.txt about using icon fonts. It's really easy and super convenient! + ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) ref->Colors[i] = style.Colors[i]; + ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) style.Colors[i] = ref->Colors[i]; + } + ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); + ImGui::TextUnformatted(name); + ImGui::PopID(); + } + ImGui::PopItemWidth(); + ImGui::EndChild(); + + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Fonts")) + { + ImGuiIO& io = ImGui::GetIO(); + ImFontAtlas* atlas = io.Fonts; + HelpMarker("Read FAQ and misc/fonts/README.txt for details on font loading."); + ImGui::PushItemWidth(120); + for (int i = 0; i < atlas->Fonts.Size; i++) + { + ImFont* font = atlas->Fonts[i]; + ImGui::PushID(font); + bool font_details_opened = ImGui::TreeNode(font, "Font %d: \"%s\"\n%.2f px, %d glyphs, %d file(s)", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size, font->ConfigDataCount); + ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { io.FontDefault = font; } + if (font_details_opened) + { + ImGui::PushFont(font); + ImGui::Text("The quick brown fox jumps over the lazy dog"); + ImGui::PopFont(); + ImGui::DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); // Scale only this font + ImGui::SameLine(); HelpMarker("Note than the default embedded font is NOT meant to be scaled.\n\nFont are currently rendered into bitmaps at a given size at the time of building the atlas. You may oversample them to get some flexibility with scaling. You can also render at multiple sizes and select which one to use at runtime.\n\n(Glimmer of hope: the atlas system should hopefully be rewritten in the future to make scaling more natural and automatic.)"); + ImGui::InputFloat("Font offset", &font->DisplayOffset.y, 1, 1, "%.0f"); + ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); + ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); + ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); + const float surface_sqrt = sqrtf((float)font->MetricsTotalSurface); + ImGui::Text("Texture surface: %d pixels (approx) ~ %dx%d", font->MetricsTotalSurface, (int)surface_sqrt, (int)surface_sqrt); + for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) + if (const ImFontConfig* cfg = &font->ConfigData[config_i]) + ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); + if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) + { + // Display all glyphs of the fonts in separate pages of 256 characters + for (int base = 0; base < 0x10000; base += 256) + { + int count = 0; + for (int n = 0; n < 256; n++) + count += font->FindGlyphNoFallback((ImWchar)(base + n)) ? 1 : 0; + if (count > 0 && ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) + { + float cell_size = font->FontSize * 1; + float cell_spacing = style.ItemSpacing.y; + ImVec2 base_pos = ImGui::GetCursorScreenPos(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + for (int n = 0; n < 256; n++) + { + ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); + ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); + const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); + draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); + if (glyph) + font->RenderChar(draw_list, cell_size, cell_p1, ImGui::GetColorU32(ImGuiCol_Text), (ImWchar)(base + n)); // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions available to generate a string. + if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2)) + { + ImGui::BeginTooltip(); + ImGui::Text("Codepoint: U+%04X", base + n); + ImGui::Separator(); + ImGui::Text("AdvanceX: %.1f", glyph->AdvanceX); + ImGui::Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); + ImGui::Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); + ImGui::EndTooltip(); + } + } + ImGui::Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16)); + ImGui::TreePop(); + } + } + ImGui::TreePop(); + } + ImGui::TreePop(); + } + ImGui::PopID(); + } + if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) + { + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); + ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0, 0), ImVec2(1, 1), tint_col, border_col); + ImGui::TreePop(); + } + + HelpMarker("Those are old settings provided for convenience.\nHowever, the _correct_ way of scaling your UI is currently to reload your font at the designed size, rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure."); + static float window_scale = 1.0f; + if (ImGui::DragFloat("window scale", &window_scale, 0.005f, 0.3f, 2.0f, "%.2f")) // scale only this window + ImGui::SetWindowFontScale(window_scale); + ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, 0.3f, 2.0f, "%.2f"); // scale everything + ImGui::PopItemWidth(); + + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Rendering")) + { + ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines); ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); + ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); + ImGui::PushItemWidth(100); + ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, FLT_MAX, "%.2f", 2.0f); + if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f; + ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero. + ImGui::PopItemWidth(); + + ImGui::EndTabItem(); + } + + ImGui::EndTabBar(); + } + + ImGui::PopItemWidth(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar() +//----------------------------------------------------------------------------- +// - ShowExampleAppMainMenuBar() +// - ShowExampleMenuFile() +//----------------------------------------------------------------------------- + +// Demonstrate creating a "main" fullscreen menu bar and populating it. +// Note the difference between BeginMainMenuBar() and BeginMenuBar(): +// - BeginMenuBar() = menu-bar inside current window we Begin()-ed into (the window needs the ImGuiWindowFlags_MenuBar flag) +// - BeginMainMenuBar() = helper to create menu-bar-sized window at the top of the main viewport + call BeginMenuBar() into it. +static void ShowExampleAppMainMenuBar() +{ + if (ImGui::BeginMainMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Edit")) + { + if (ImGui::MenuItem("Undo", "CTRL+Z")) {} + if (ImGui::MenuItem("Redo", "CTRL+Y", false, false)) {} // Disabled item + ImGui::Separator(); + if (ImGui::MenuItem("Cut", "CTRL+X")) {} + if (ImGui::MenuItem("Copy", "CTRL+C")) {} + if (ImGui::MenuItem("Paste", "CTRL+V")) {} + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } +} + +// Note that shortcuts are currently provided for display only (future version will add flags to BeginMenu to process shortcuts) +static void ShowExampleMenuFile() +{ + ImGui::MenuItem("(dummy menu)", NULL, false, false); + if (ImGui::MenuItem("New")) {} + if (ImGui::MenuItem("Open", "Ctrl+O")) {} + if (ImGui::BeginMenu("Open Recent")) + { + ImGui::MenuItem("fish_hat.c"); + ImGui::MenuItem("fish_hat.inl"); + ImGui::MenuItem("fish_hat.h"); + if (ImGui::BeginMenu("More..")) + { + ImGui::MenuItem("Hello"); + ImGui::MenuItem("Sailor"); + if (ImGui::BeginMenu("Recurse..")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + ImGui::EndMenu(); + } + ImGui::EndMenu(); + } + if (ImGui::MenuItem("Save", "Ctrl+S")) {} + if (ImGui::MenuItem("Save As..")) {} + ImGui::Separator(); + if (ImGui::BeginMenu("Options")) + { + static bool enabled = true; + ImGui::MenuItem("Enabled", "", &enabled); + ImGui::BeginChild("child", ImVec2(0, 60), true); + for (int i = 0; i < 10; i++) + ImGui::Text("Scrolling Text %d", i); + ImGui::EndChild(); + static float f = 0.5f; + static int n = 0; + static bool b = true; + ImGui::SliderFloat("Value", &f, 0.0f, 1.0f); + ImGui::InputFloat("Input", &f, 0.1f); + ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0"); + ImGui::Checkbox("Check", &b); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Colors")) + { + float sz = ImGui::GetTextLineHeight(); + for (int i = 0; i < ImGuiCol_COUNT; i++) + { + const char* name = ImGui::GetStyleColorName((ImGuiCol)i); + ImVec2 p = ImGui::GetCursorScreenPos(); + ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x+sz, p.y+sz), ImGui::GetColorU32((ImGuiCol)i)); + ImGui::Dummy(ImVec2(sz, sz)); + ImGui::SameLine(); + ImGui::MenuItem(name); + } + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Disabled", false)) // Disabled + { + IM_ASSERT(0); + } + if (ImGui::MenuItem("Checked", NULL, true)) {} + if (ImGui::MenuItem("Quit", "Alt+F4")) {} +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Debug Console / ShowExampleAppConsole() +//----------------------------------------------------------------------------- + +// Demonstrate creating a simple console window, with scrolling, filtering, completion and history. +// For the console example, here we are using a more C++ like approach of declaring a class to hold the data and the functions. +struct ExampleAppConsole +{ + char InputBuf[256]; + ImVector Items; + ImVector Commands; + ImVector History; + int HistoryPos; // -1: new line, 0..History.Size-1 browsing history. + ImGuiTextFilter Filter; + bool AutoScroll; + bool ScrollToBottom; + + ExampleAppConsole() + { + ClearLog(); + memset(InputBuf, 0, sizeof(InputBuf)); + HistoryPos = -1; + Commands.push_back("HELP"); + Commands.push_back("HISTORY"); + Commands.push_back("CLEAR"); + Commands.push_back("CLASSIFY"); // "classify" is only here to provide an example of "C"+[tab] completing to "CL" and displaying matches. + AutoScroll = true; + ScrollToBottom = false; + AddLog("Welcome to Dear ImGui!"); + } + ~ExampleAppConsole() + { + ClearLog(); + for (int i = 0; i < History.Size; i++) + free(History[i]); + } + + // Portable helpers + static int Stricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; } + static int Strnicmp(const char* str1, const char* str2, int n) { int d = 0; while (n > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; n--; } return d; } + static char* Strdup(const char *str) { size_t len = strlen(str) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)str, len); } + static void Strtrim(char* str) { char* str_end = str + strlen(str); while (str_end > str && str_end[-1] == ' ') str_end--; *str_end = 0; } + + void ClearLog() + { + for (int i = 0; i < Items.Size; i++) + free(Items[i]); + Items.clear(); + } + + void AddLog(const char* fmt, ...) IM_FMTARGS(2) + { + // FIXME-OPT + char buf[1024]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args); + buf[IM_ARRAYSIZE(buf)-1] = 0; + va_end(args); + Items.push_back(Strdup(buf)); + } + + void Draw(const char* title, bool* p_open) + { + ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiCond_FirstUseEver); + if (!ImGui::Begin(title, p_open)) + { + ImGui::End(); + return; + } + + // As a specific feature guaranteed by the library, after calling Begin() the last Item represent the title bar. So e.g. IsItemHovered() will return true when hovering the title bar. + // Here we create a context menu only available from the title bar. + if (ImGui::BeginPopupContextItem()) + { + if (ImGui::MenuItem("Close Console")) + *p_open = false; + ImGui::EndPopup(); + } + + ImGui::TextWrapped("This example implements a console with basic coloring, completion and history. A more elaborate implementation may want to store entries along with extra data such as timestamp, emitter, etc."); + ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion."); + + // TODO: display items starting from the bottom + + if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine(); + if (ImGui::SmallButton("Add Dummy Error")) { AddLog("[error] something went wrong"); } ImGui::SameLine(); + if (ImGui::SmallButton("Clear")) { ClearLog(); } ImGui::SameLine(); + bool copy_to_clipboard = ImGui::SmallButton("Copy"); + //static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); } + + ImGui::Separator(); + + // Options menu + if (ImGui::BeginPopup("Options")) + { + ImGui::Checkbox("Auto-scroll", &AutoScroll); + ImGui::EndPopup(); + } + + // Options, Filter + if (ImGui::Button("Options")) + ImGui::OpenPopup("Options"); + ImGui::SameLine(); + Filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180); + ImGui::Separator(); + + const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); // 1 separator, 1 input text + ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::Selectable("Clear")) ClearLog(); + ImGui::EndPopup(); + } + + // Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end()); + // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping to only process visible items. + // You can seek and display only the lines that are visible using the ImGuiListClipper helper, if your elements are evenly spaced and you have cheap random access to the elements. + // To use the clipper we could replace the 'for (int i = 0; i < Items.Size; i++)' loop with: + // ImGuiListClipper clipper(Items.Size); + // while (clipper.Step()) + // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) + // However, note that you can not use this code as is if a filter is active because it breaks the 'cheap random-access' property. We would need random-access on the post-filtered list. + // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices that passed the filtering test, recomputing this array when user changes the filter, + // and appending newly elements as they are inserted. This is left as a task to the user until we can manage to improve this example code! + // If your items are of variable size you may want to implement code similar to what ImGuiListClipper does. Or split your data into fixed height items to allow random-seeking into your list. + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // Tighten spacing + if (copy_to_clipboard) + ImGui::LogToClipboard(); + for (int i = 0; i < Items.Size; i++) + { + const char* item = Items[i]; + if (!Filter.PassFilter(item)) + continue; + + // Normally you would store more information in your item (e.g. make Items[] an array of structure, store color/type etc.) + bool pop_color = false; + if (strstr(item, "[error]")) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.4f, 0.4f, 1.0f)); pop_color = true; } + else if (strncmp(item, "# ", 2) == 0) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.8f, 0.6f, 1.0f)); pop_color = true; } + ImGui::TextUnformatted(item); + if (pop_color) + ImGui::PopStyleColor(); + } + if (copy_to_clipboard) + ImGui::LogFinish(); + + if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) + ImGui::SetScrollHereY(1.0f); + ScrollToBottom = false; + + ImGui::PopStyleVar(); + ImGui::EndChild(); + ImGui::Separator(); + + // Command-line + bool reclaim_focus = false; + if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue|ImGuiInputTextFlags_CallbackCompletion|ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this)) + { + char* s = InputBuf; + Strtrim(s); + if (s[0]) + ExecCommand(s); + strcpy(s, ""); + reclaim_focus = true; + } + + // Auto-focus on window apparition + ImGui::SetItemDefaultFocus(); + if (reclaim_focus) + ImGui::SetKeyboardFocusHere(-1); // Auto focus previous widget + + ImGui::End(); + } + + void ExecCommand(const char* command_line) + { + AddLog("# %s\n", command_line); + + // Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal. + HistoryPos = -1; + for (int i = History.Size-1; i >= 0; i--) + if (Stricmp(History[i], command_line) == 0) + { + free(History[i]); + History.erase(History.begin() + i); + break; + } + History.push_back(Strdup(command_line)); + + // Process command + if (Stricmp(command_line, "CLEAR") == 0) + { + ClearLog(); + } + else if (Stricmp(command_line, "HELP") == 0) + { + AddLog("Commands:"); + for (int i = 0; i < Commands.Size; i++) + AddLog("- %s", Commands[i]); + } + else if (Stricmp(command_line, "HISTORY") == 0) + { + int first = History.Size - 10; + for (int i = first > 0 ? first : 0; i < History.Size; i++) + AddLog("%3d: %s\n", i, History[i]); + } + else + { + AddLog("Unknown command: '%s'\n", command_line); + } + + // On commad input, we scroll to bottom even if AutoScroll==false + ScrollToBottom = true; + } + + static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks + { + ExampleAppConsole* console = (ExampleAppConsole*)data->UserData; + return console->TextEditCallback(data); + } + + int TextEditCallback(ImGuiInputTextCallbackData* data) + { + //AddLog("cursor: %d, selection: %d-%d", data->CursorPos, data->SelectionStart, data->SelectionEnd); + switch (data->EventFlag) + { + case ImGuiInputTextFlags_CallbackCompletion: + { + // Example of TEXT COMPLETION + + // Locate beginning of current word + const char* word_end = data->Buf + data->CursorPos; + const char* word_start = word_end; + while (word_start > data->Buf) + { + const char c = word_start[-1]; + if (c == ' ' || c == '\t' || c == ',' || c == ';') + break; + word_start--; + } + + // Build a list of candidates + ImVector candidates; + for (int i = 0; i < Commands.Size; i++) + if (Strnicmp(Commands[i], word_start, (int)(word_end-word_start)) == 0) + candidates.push_back(Commands[i]); + + if (candidates.Size == 0) + { + // No match + AddLog("No match for \"%.*s\"!\n", (int)(word_end-word_start), word_start); + } + else if (candidates.Size == 1) + { + // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing + data->DeleteChars((int)(word_start-data->Buf), (int)(word_end-word_start)); + data->InsertChars(data->CursorPos, candidates[0]); + data->InsertChars(data->CursorPos, " "); + } + else + { + // Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY" + int match_len = (int)(word_end - word_start); + for (;;) + { + int c = 0; + bool all_candidates_matches = true; + for (int i = 0; i < candidates.Size && all_candidates_matches; i++) + if (i == 0) + c = toupper(candidates[i][match_len]); + else if (c == 0 || c != toupper(candidates[i][match_len])) + all_candidates_matches = false; + if (!all_candidates_matches) + break; + match_len++; + } + + if (match_len > 0) + { + data->DeleteChars((int)(word_start - data->Buf), (int)(word_end-word_start)); + data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len); + } + + // List matches + AddLog("Possible matches:\n"); + for (int i = 0; i < candidates.Size; i++) + AddLog("- %s\n", candidates[i]); + } + + break; + } + case ImGuiInputTextFlags_CallbackHistory: + { + // Example of HISTORY + const int prev_history_pos = HistoryPos; + if (data->EventKey == ImGuiKey_UpArrow) + { + if (HistoryPos == -1) + HistoryPos = History.Size - 1; + else if (HistoryPos > 0) + HistoryPos--; + } + else if (data->EventKey == ImGuiKey_DownArrow) + { + if (HistoryPos != -1) + if (++HistoryPos >= History.Size) + HistoryPos = -1; + } + + // A better implementation would preserve the data on the current input line along with cursor position. + if (prev_history_pos != HistoryPos) + { + const char* history_str = (HistoryPos >= 0) ? History[HistoryPos] : ""; + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, history_str); + } + } + } + return 0; + } +}; + +static void ShowExampleAppConsole(bool* p_open) +{ + static ExampleAppConsole console; + console.Draw("Example: Console", p_open); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Debug Log / ShowExampleAppLog() +//----------------------------------------------------------------------------- + +// Usage: +// static ExampleAppLog my_log; +// my_log.AddLog("Hello %d world\n", 123); +// my_log.Draw("title"); +struct ExampleAppLog +{ + ImGuiTextBuffer Buf; + ImGuiTextFilter Filter; + ImVector LineOffsets; // Index to lines offset. We maintain this with AddLog() calls, allowing us to have a random access on lines + bool AutoScroll; // Keep scrolling if already at the bottom + + ExampleAppLog() + { + AutoScroll = true; + Clear(); + } + + void Clear() + { + Buf.clear(); + LineOffsets.clear(); + LineOffsets.push_back(0); + } + + void AddLog(const char* fmt, ...) IM_FMTARGS(2) + { + int old_size = Buf.size(); + va_list args; + va_start(args, fmt); + Buf.appendfv(fmt, args); + va_end(args); + for (int new_size = Buf.size(); old_size < new_size; old_size++) + if (Buf[old_size] == '\n') + LineOffsets.push_back(old_size + 1); + } + + void Draw(const char* title, bool* p_open = NULL) + { + if (!ImGui::Begin(title, p_open)) + { + ImGui::End(); + return; + } + + // Options menu + if (ImGui::BeginPopup("Options")) + { + ImGui::Checkbox("Auto-scroll", &AutoScroll); + ImGui::EndPopup(); + } + + // Main window + if (ImGui::Button("Options")) + ImGui::OpenPopup("Options"); + ImGui::SameLine(); + bool clear = ImGui::Button("Clear"); + ImGui::SameLine(); + bool copy = ImGui::Button("Copy"); + ImGui::SameLine(); + Filter.Draw("Filter", -100.0f); + + ImGui::Separator(); + ImGui::BeginChild("scrolling", ImVec2(0,0), false, ImGuiWindowFlags_HorizontalScrollbar); + + if (clear) + Clear(); + if (copy) + ImGui::LogToClipboard(); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + const char* buf = Buf.begin(); + const char* buf_end = Buf.end(); + if (Filter.IsActive()) + { + // In this example we don't use the clipper when Filter is enabled. + // This is because we don't have a random access on the result on our filter. + // A real application processing logs with ten of thousands of entries may want to store the result of search/filter. + // especially if the filtering function is not trivial (e.g. reg-exp). + for (int line_no = 0; line_no < LineOffsets.Size; line_no++) + { + const char* line_start = buf + LineOffsets[line_no]; + const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end; + if (Filter.PassFilter(line_start, line_end)) + ImGui::TextUnformatted(line_start, line_end); + } + } + else + { + // The simplest and easy way to display the entire buffer: + // ImGui::TextUnformatted(buf_begin, buf_end); + // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward to skip non-visible lines. + // Here we instead demonstrate using the clipper to only process lines that are within the visible area. + // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them on your side is recommended. + // Using ImGuiListClipper requires A) random access into your data, and B) items all being the same height, + // both of which we can handle since we an array pointing to the beginning of each line of text. + // When using the filter (in the block of code above) we don't have random access into the data to display anymore, which is why we don't use the clipper. + // Storing or skimming through the search result would make it possible (and would be recommended if you want to search through tens of thousands of entries) + ImGuiListClipper clipper; + clipper.Begin(LineOffsets.Size); + while (clipper.Step()) + { + for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) + { + const char* line_start = buf + LineOffsets[line_no]; + const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end; + ImGui::TextUnformatted(line_start, line_end); + } + } + clipper.End(); + } + ImGui::PopStyleVar(); + + if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); + + ImGui::EndChild(); + ImGui::End(); + } +}; + +// Demonstrate creating a simple log window with basic filtering. +static void ShowExampleAppLog(bool* p_open) +{ + static ExampleAppLog log; + + // For the demo: add a debug button _BEFORE_ the normal log window contents + // We take advantage of a rarely used feature: multiple calls to Begin()/End() are appending to the _same_ window. + // Most of the contents of the window will be added by the log.Draw() call. + ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); + ImGui::Begin("Example: Log", p_open); + if (ImGui::SmallButton("[Debug] Add 5 entries")) + { + static int counter = 0; + for (int n = 0; n < 5; n++) + { + const char* categories[3] = { "info", "warn", "error" }; + const char* words[] = { "Bumfuzzled", "Cattywampus", "Snickersnee", "Abibliophobia", "Absquatulate", "Nincompoop", "Pauciloquent" }; + log.AddLog("[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n", + ImGui::GetFrameCount(), categories[counter % IM_ARRAYSIZE(categories)], ImGui::GetTime(), words[counter % IM_ARRAYSIZE(words)]); + counter++; + } + } + ImGui::End(); + + // Actually call in the regular Log helper (which will Begin() into the same window as we just did) + log.Draw("Example: Log", p_open); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Simple Layout / ShowExampleAppLayout() +//----------------------------------------------------------------------------- + +// Demonstrate create a window with multiple child windows. +static void ShowExampleAppLayout(bool* p_open) +{ + ImGui::SetNextWindowSize(ImVec2(500, 440), ImGuiCond_FirstUseEver); + if (ImGui::Begin("Example: Simple layout", p_open, ImGuiWindowFlags_MenuBar)) + { + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Close")) *p_open = false; + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + // left + static int selected = 0; + ImGui::BeginChild("left pane", ImVec2(150, 0), true); + for (int i = 0; i < 100; i++) + { + char label[128]; + sprintf(label, "MyObject %d", i); + if (ImGui::Selectable(label, selected == i)) + selected = i; + } + ImGui::EndChild(); + ImGui::SameLine(); + + // right + ImGui::BeginGroup(); + ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing())); // Leave room for 1 line below us + ImGui::Text("MyObject: %d", selected); + ImGui::Separator(); + if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None)) + { + if (ImGui::BeginTabItem("Description")) + { + ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Details")) + { + ImGui::Text("ID: 0123456789"); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } + ImGui::EndChild(); + if (ImGui::Button("Revert")) {} + ImGui::SameLine(); + if (ImGui::Button("Save")) {} + ImGui::EndGroup(); + } + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() +//----------------------------------------------------------------------------- + +// Demonstrate create a simple property editor. +static void ShowExampleAppPropertyEditor(bool* p_open) +{ + ImGui::SetNextWindowSize(ImVec2(430,450), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("Example: Property editor", p_open)) + { + ImGui::End(); + return; + } + + HelpMarker("This example shows how you may implement a property editor using two columns.\nAll objects/fields data are dummies here.\nRemember that in many simple cases, you can use ImGui::SameLine(xxx) to position\nyour cursor horizontally instead of using the Columns() API."); + + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2,2)); + ImGui::Columns(2); + ImGui::Separator(); + + struct funcs + { + static void ShowDummyObject(const char* prefix, int uid) + { + ImGui::PushID(uid); // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID. + ImGui::AlignTextToFramePadding(); // Text and Tree nodes are less high than regular widgets, here we add vertical spacing to make the tree lines equal high. + bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); + ImGui::NextColumn(); + ImGui::AlignTextToFramePadding(); + ImGui::Text("my sailor is rich"); + ImGui::NextColumn(); + if (node_open) + { + static float dummy_members[8] = { 0.0f,0.0f,1.0f,3.1416f,100.0f,999.0f }; + for (int i = 0; i < 8; i++) + { + ImGui::PushID(i); // Use field index as identifier. + if (i < 2) + { + ShowDummyObject("Child", 424242); + } + else + { + // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well) + ImGui::AlignTextToFramePadding(); + ImGui::TreeNodeEx("Field", ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet, "Field_%d", i); + ImGui::NextColumn(); + ImGui::SetNextItemWidth(-1); + if (i >= 5) + ImGui::InputFloat("##value", &dummy_members[i], 1.0f); + else + ImGui::DragFloat("##value", &dummy_members[i], 0.01f); + ImGui::NextColumn(); + } + ImGui::PopID(); + } + ImGui::TreePop(); + } + ImGui::PopID(); + } + }; + + // Iterate dummy objects with dummy members (all the same data) + for (int obj_i = 0; obj_i < 3; obj_i++) + funcs::ShowDummyObject("Object", obj_i); + + ImGui::Columns(1); + ImGui::Separator(); + ImGui::PopStyleVar(); + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Long Text / ShowExampleAppLongText() +//----------------------------------------------------------------------------- + +// Demonstrate/test rendering huge amount of text, and the incidence of clipping. +static void ShowExampleAppLongText(bool* p_open) +{ + ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiCond_FirstUseEver); + if (!ImGui::Begin("Example: Long text display", p_open)) + { + ImGui::End(); + return; + } + + static int test_type = 0; + static ImGuiTextBuffer log; + static int lines = 0; + ImGui::Text("Printing unusually long amount of text."); + ImGui::Combo("Test type", &test_type, "Single call to TextUnformatted()\0Multiple calls to Text(), clipped\0Multiple calls to Text(), not clipped (slow)\0"); + ImGui::Text("Buffer contents: %d lines, %d bytes", lines, log.size()); + if (ImGui::Button("Clear")) { log.clear(); lines = 0; } + ImGui::SameLine(); + if (ImGui::Button("Add 1000 lines")) + { + for (int i = 0; i < 1000; i++) + log.appendf("%i The quick brown fox jumps over the lazy dog\n", lines+i); + lines += 1000; + } + ImGui::BeginChild("Log"); + switch (test_type) + { + case 0: + // Single call to TextUnformatted() with a big buffer + ImGui::TextUnformatted(log.begin(), log.end()); + break; + case 1: + { + // Multiple calls to Text(), manually coarsely clipped - demonstrate how to use the ImGuiListClipper helper. + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0)); + ImGuiListClipper clipper(lines); + while (clipper.Step()) + for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) + ImGui::Text("%i The quick brown fox jumps over the lazy dog", i); + ImGui::PopStyleVar(); + break; + } + case 2: + // Multiple calls to Text(), not clipped (slow) + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0)); + for (int i = 0; i < lines; i++) + ImGui::Text("%i The quick brown fox jumps over the lazy dog", i); + ImGui::PopStyleVar(); + break; + } + ImGui::EndChild(); + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize() +//----------------------------------------------------------------------------- + +// Demonstrate creating a window which gets auto-resized according to its content. +static void ShowExampleAppAutoResize(bool* p_open) +{ + if (!ImGui::Begin("Example: Auto-resizing window", p_open, ImGuiWindowFlags_AlwaysAutoResize)) + { + ImGui::End(); + return; + } + + static int lines = 10; + ImGui::Text("Window will resize every-frame to the size of its content.\nNote that you probably don't want to query the window size to\noutput your content because that would create a feedback loop."); + ImGui::SliderInt("Number of lines", &lines, 1, 20); + for (int i = 0; i < lines; i++) + ImGui::Text("%*sThis is line %d", i * 4, "", i); // Pad with space to extend size horizontally + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Constrained Resize / ShowExampleAppConstrainedResize() +//----------------------------------------------------------------------------- + +// Demonstrate creating a window with custom resize constraints. +static void ShowExampleAppConstrainedResize(bool* p_open) +{ + struct CustomConstraints // Helper functions to demonstrate programmatic constraints + { + static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = (data->DesiredSize.x > data->DesiredSize.y ? data->DesiredSize.x : data->DesiredSize.y); } + static void Step(ImGuiSizeCallbackData* data) { float step = (float)(int)(intptr_t)data->UserData; data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step); } + }; + + static bool auto_resize = false; + static int type = 0; + static int display_lines = 10; + if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Vertical only + if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Horizontal only + if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); // Width > 100, Height > 100 + if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width 400-500 + if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 400), ImVec2(-1, 500)); // Height 400-500 + if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square + if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)(intptr_t)100); // Fixed Step + + ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0; + if (ImGui::Begin("Example: Constrained Resize", p_open, flags)) + { + const char* desc[] = + { + "Resize vertical only", + "Resize horizontal only", + "Width > 100, Height > 100", + "Width 400-500", + "Height 400-500", + "Custom: Always Square", + "Custom: Fixed Steps (100)", + }; + if (ImGui::Button("200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine(); + if (ImGui::Button("500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine(); + if (ImGui::Button("800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); } + ImGui::SetNextItemWidth(200); + ImGui::Combo("Constraint", &type, desc, IM_ARRAYSIZE(desc)); + ImGui::SetNextItemWidth(200); + ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100); + ImGui::Checkbox("Auto-resize", &auto_resize); + for (int i = 0; i < display_lines; i++) + ImGui::Text("%*sHello, sailor! Making this line long enough for the example.", i * 4, ""); + } + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Simple Overlay / ShowExampleAppSimpleOverlay() +//----------------------------------------------------------------------------- + +// Demonstrate creating a simple static window with no decoration + a context-menu to choose which corner of the screen to use. +static void ShowExampleAppSimpleOverlay(bool* p_open) +{ + const float DISTANCE = 10.0f; + static int corner = 0; + ImGuiIO& io = ImGui::GetIO(); + if (corner != -1) + { + ImVec2 window_pos = ImVec2((corner & 1) ? io.DisplaySize.x - DISTANCE : DISTANCE, (corner & 2) ? io.DisplaySize.y - DISTANCE : DISTANCE); + ImVec2 window_pos_pivot = ImVec2((corner & 1) ? 1.0f : 0.0f, (corner & 2) ? 1.0f : 0.0f); + ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); + } + ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background + if (ImGui::Begin("Example: Simple overlay", p_open, (corner != -1 ? ImGuiWindowFlags_NoMove : 0) | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav)) + { + ImGui::Text("Simple overlay\n" "in the corner of the screen.\n" "(right-click to change position)"); + ImGui::Separator(); + if (ImGui::IsMousePosValid()) + ImGui::Text("Mouse Position: (%.1f,%.1f)", io.MousePos.x, io.MousePos.y); + else + ImGui::Text("Mouse Position: "); + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::MenuItem("Custom", NULL, corner == -1)) corner = -1; + if (ImGui::MenuItem("Top-left", NULL, corner == 0)) corner = 0; + if (ImGui::MenuItem("Top-right", NULL, corner == 1)) corner = 1; + if (ImGui::MenuItem("Bottom-left", NULL, corner == 2)) corner = 2; + if (ImGui::MenuItem("Bottom-right", NULL, corner == 3)) corner = 3; + if (p_open && ImGui::MenuItem("Close")) *p_open = false; + ImGui::EndPopup(); + } + } + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles() +//----------------------------------------------------------------------------- + +// Demonstrate using "##" and "###" in identifiers to manipulate ID generation. +// This apply to all regular items as well. Read FAQ section "How can I have multiple widgets with the same label? Can I have widget without a label? (Yes). A primer on the purpose of labels/IDs." for details. +static void ShowExampleAppWindowTitles(bool*) +{ + // By default, Windows are uniquely identified by their title. + // You can use the "##" and "###" markers to manipulate the display/ID. + + // Using "##" to display same title but have unique identifier. + ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver); + ImGui::Begin("Same title as another window##1"); + ImGui::Text("This is window 1.\nMy title is the same as window 2, but my identifier is unique."); + ImGui::End(); + + ImGui::SetNextWindowPos(ImVec2(100, 200), ImGuiCond_FirstUseEver); + ImGui::Begin("Same title as another window##2"); + ImGui::Text("This is window 2.\nMy title is the same as window 1, but my identifier is unique."); + ImGui::End(); + + // Using "###" to display a changing title but keep a static identifier "AnimatedTitle" + char buf[128]; + sprintf(buf, "Animated title %c %d###AnimatedTitle", "|/-\\"[(int)(ImGui::GetTime() / 0.25f) & 3], ImGui::GetFrameCount()); + ImGui::SetNextWindowPos(ImVec2(100, 300), ImGuiCond_FirstUseEver); + ImGui::Begin(buf); + ImGui::Text("This window has a changing title."); + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering() +//----------------------------------------------------------------------------- + +// Demonstrate using the low-level ImDrawList to draw custom shapes. +static void ShowExampleAppCustomRendering(bool* p_open) +{ + if (!ImGui::Begin("Example: Custom rendering", p_open)) + { + ImGui::End(); + return; + } + + // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of overloaded operators, etc. + // Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your types and ImVec2/ImVec4. + // ImGui defines overloaded operators but they are internal to imgui.cpp and not exposed outside (to avoid messing with your types) + // In this example we are not using the maths operators! + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + + if (ImGui::BeginTabBar("##TabBar")) + { + // Primitives + if (ImGui::BeginTabItem("Primitives")) + { + static float sz = 36.0f; + static float thickness = 3.0f; + static ImVec4 colf = ImVec4(1.0f, 1.0f, 0.4f, 1.0f); + ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 72.0f, "%.0f"); + ImGui::DragFloat("Thickness", &thickness, 0.05f, 1.0f, 8.0f, "%.02f"); + ImGui::ColorEdit4("Color", &colf.x); + const ImVec2 p = ImGui::GetCursorScreenPos(); + const ImU32 col = ImColor(colf); + float x = p.x + 4.0f, y = p.y + 4.0f; + float spacing = 10.0f; + ImDrawCornerFlags corners_none = 0; + ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; + ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; + for (int n = 0; n < 2; n++) + { + // First line uses a thickness of 1.0f, second line uses the configurable thickness + float th = (n == 0) ? 1.0f : thickness; + draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 6, th); x += sz + spacing; // Hexagon + draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 20, th); x += sz + spacing; // Circle + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners + draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th); x += sz + spacing; // Triangle + draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th); x += sz*0.4f + spacing; // Thin triangle + draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) + draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) + draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line + draw_list->AddBezierCurve(ImVec2(x, y), ImVec2(x + sz*1.3f, y + sz*0.3f), ImVec2(x + sz - sz*1.3f, y + sz - sz*0.3f), ImVec2(x + sz, y + sz), col, th); + x = p.x + 4; + y += sz + spacing; + } + draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 6); x += sz + spacing; // Hexagon + draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 32); x += sz + spacing; // Circle + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners + draw_list->AddTriangleFilled(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col); x += sz + spacing; // Triangle + draw_list->AddTriangleFilled(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col); x += sz*0.4f + spacing; // Thin triangle + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness) + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing*2.0f; // Vertical line (faster than AddLine, but only handle integer thickness) + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) + draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); + ImGui::Dummy(ImVec2((sz + spacing) * 9.8f, (sz + spacing) * 3)); + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("Canvas")) + { + static ImVector points; + static bool adding_line = false; + if (ImGui::Button("Clear")) points.clear(); + if (points.Size >= 2) { ImGui::SameLine(); if (ImGui::Button("Undo")) { points.pop_back(); points.pop_back(); } } + ImGui::Text("Left-click and drag to add lines,\nRight-click to undo"); + + // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use IsItemHovered() + // But you can also draw directly and poll mouse/keyboard by yourself. You can manipulate the cursor using GetCursorPos() and SetCursorPos(). + // If you only use the ImDrawList API, you can notify the owner window of its extends by using SetCursorPos(max). + ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! + ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available + if (canvas_size.x < 50.0f) canvas_size.x = 50.0f; + if (canvas_size.y < 50.0f) canvas_size.y = 50.0f; + draw_list->AddRectFilledMultiColor(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), IM_COL32(50, 50, 50, 255), IM_COL32(50, 50, 60, 255), IM_COL32(60, 60, 70, 255), IM_COL32(50, 50, 60, 255)); + draw_list->AddRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), IM_COL32(255, 255, 255, 255)); + + bool adding_preview = false; + ImGui::InvisibleButton("canvas", canvas_size); + ImVec2 mouse_pos_in_canvas = ImVec2(ImGui::GetIO().MousePos.x - canvas_pos.x, ImGui::GetIO().MousePos.y - canvas_pos.y); + if (adding_line) + { + adding_preview = true; + points.push_back(mouse_pos_in_canvas); + if (!ImGui::IsMouseDown(0)) + adding_line = adding_preview = false; + } + if (ImGui::IsItemHovered()) + { + if (!adding_line && ImGui::IsMouseClicked(0)) + { + points.push_back(mouse_pos_in_canvas); + adding_line = true; + } + if (ImGui::IsMouseClicked(1) && !points.empty()) + { + adding_line = adding_preview = false; + points.pop_back(); + points.pop_back(); + } + } + draw_list->PushClipRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), true); // clip lines within the canvas (if we resize it, etc.) + for (int i = 0; i < points.Size - 1; i += 2) + draw_list->AddLine(ImVec2(canvas_pos.x + points[i].x, canvas_pos.y + points[i].y), ImVec2(canvas_pos.x + points[i + 1].x, canvas_pos.y + points[i + 1].y), IM_COL32(255, 255, 0, 255), 2.0f); + draw_list->PopClipRect(); + if (adding_preview) + points.pop_back(); + ImGui::EndTabItem(); + } + + if (ImGui::BeginTabItem("BG/FG draw lists")) + { + static bool draw_bg = true; + static bool draw_fg = true; + ImGui::Checkbox("Draw in Background draw list", &draw_bg); + ImGui::SameLine(); HelpMarker("The Background draw list will be rendered below every Dear ImGui windows."); + ImGui::Checkbox("Draw in Foreground draw list", &draw_fg); + ImGui::SameLine(); HelpMarker("The Foreground draw list will be rendered over every Dear ImGui windows."); + ImVec2 window_pos = ImGui::GetWindowPos(); + ImVec2 window_size = ImGui::GetWindowSize(); + ImVec2 window_center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f); + if (draw_bg) + ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 48, 10+4); + if (draw_fg) + ImGui::GetForegroundDrawList()->AddCircle(window_center, window_size.y * 0.6f, IM_COL32(0, 255, 0, 200), 48, 10); + ImGui::EndTabItem(); + } + + ImGui::EndTabBar(); + } + + ImGui::End(); +} + +//----------------------------------------------------------------------------- +// [SECTION] Example App: Documents Handling / ShowExampleAppDocuments() +//----------------------------------------------------------------------------- + +// Simplified structure to mimic a Document model +struct MyDocument +{ + const char* Name; // Document title + bool Open; // Set when the document is open (in this demo, we keep an array of all available documents to simplify the demo) + bool OpenPrev; // Copy of Open from last update. + bool Dirty; // Set when the document has been modified + bool WantClose; // Set when the document + ImVec4 Color; // An arbitrary variable associated to the document + + MyDocument(const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f,1.0f,1.0f,1.0f)) + { + Name = name; + Open = OpenPrev = open; + Dirty = false; + WantClose = false; + Color = color; + } + void DoOpen() { Open = true; } + void DoQueueClose() { WantClose = true; } + void DoForceClose() { Open = false; Dirty = false; } + void DoSave() { Dirty = false; } + + // Display dummy contents for the Document + static void DisplayContents(MyDocument* doc) + { + ImGui::PushID(doc); + ImGui::Text("Document \"%s\"", doc->Name); + ImGui::PushStyleColor(ImGuiCol_Text, doc->Color); + ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + ImGui::PopStyleColor(); + if (ImGui::Button("Modify", ImVec2(100, 0))) + doc->Dirty = true; + ImGui::SameLine(); + if (ImGui::Button("Save", ImVec2(100, 0))) + doc->DoSave(); + ImGui::ColorEdit3("color", &doc->Color.x); // Useful to test drag and drop and hold-dragged-to-open-tab behavior. + ImGui::PopID(); + } + + // Display context menu for the Document + static void DisplayContextMenu(MyDocument* doc) + { + if (!ImGui::BeginPopupContextItem()) + return; + + char buf[256]; + sprintf(buf, "Save %s", doc->Name); + if (ImGui::MenuItem(buf, "CTRL+S", false, doc->Open)) + doc->DoSave(); + if (ImGui::MenuItem("Close", "CTRL+W", false, doc->Open)) + doc->DoQueueClose(); + ImGui::EndPopup(); + } +}; + +struct ExampleAppDocuments +{ + ImVector Documents; + + ExampleAppDocuments() + { + Documents.push_back(MyDocument("Lettuce", true, ImVec4(0.4f, 0.8f, 0.4f, 1.0f))); + Documents.push_back(MyDocument("Eggplant", true, ImVec4(0.8f, 0.5f, 1.0f, 1.0f))); + Documents.push_back(MyDocument("Carrot", true, ImVec4(1.0f, 0.8f, 0.5f, 1.0f))); + Documents.push_back(MyDocument("Tomato", false, ImVec4(1.0f, 0.3f, 0.4f, 1.0f))); + Documents.push_back(MyDocument("A Rather Long Title", false)); + Documents.push_back(MyDocument("Some Document", false)); + } +}; + +// [Optional] Notify the system of Tabs/Windows closure that happened outside the regular tab interface. +// If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo, as opposed +// to clicking on the regular tab closing button) and stops being submitted, it will take a frame for the tab bar to notice its absence. +// During this frame there will be a gap in the tab bar, and if the tab that has disappeared was the selected one, the tab bar +// will report no selected tab during the frame. This will effectively give the impression of a flicker for one frame. +// We call SetTabItemClosed() to manually notify the Tab Bar or Docking system of removed tabs to avoid this glitch. +// Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag. +static void NotifyOfDocumentsClosedElsewhere(ExampleAppDocuments& app) +{ + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + { + MyDocument* doc = &app.Documents[doc_n]; + if (!doc->Open && doc->OpenPrev) + ImGui::SetTabItemClosed(doc->Name); + doc->OpenPrev = doc->Open; + } +} + +void ShowExampleAppDocuments(bool* p_open) +{ + static ExampleAppDocuments app; + + // Options + static bool opt_reorderable = true; + static ImGuiTabBarFlags opt_fitting_flags = ImGuiTabBarFlags_FittingPolicyDefault_; + + if (!ImGui::Begin("Example: Documents", p_open, ImGuiWindowFlags_MenuBar)) + { + ImGui::End(); + return; + } + + // Menu + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + int open_count = 0; + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + open_count += app.Documents[doc_n].Open ? 1 : 0; + + if (ImGui::BeginMenu("Open", open_count < app.Documents.Size)) + { + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + { + MyDocument* doc = &app.Documents[doc_n]; + if (!doc->Open) + if (ImGui::MenuItem(doc->Name)) + doc->DoOpen(); + } + ImGui::EndMenu(); + } + if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0)) + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + app.Documents[doc_n].DoQueueClose(); + if (ImGui::MenuItem("Exit", "Alt+F4")) {} + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + // [Debug] List documents with one checkbox for each + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + { + MyDocument* doc = &app.Documents[doc_n]; + if (doc_n > 0) + ImGui::SameLine(); + ImGui::PushID(doc); + if (ImGui::Checkbox(doc->Name, &doc->Open)) + if (!doc->Open) + doc->DoForceClose(); + ImGui::PopID(); + } + + ImGui::Separator(); + + // Submit Tab Bar and Tabs + { + ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0); + if (ImGui::BeginTabBar("##tabs", tab_bar_flags)) + { + if (opt_reorderable) + NotifyOfDocumentsClosedElsewhere(app); + + // [DEBUG] Stress tests + //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1; // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on. + //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name); // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway.. + + // Submit Tabs + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + { + MyDocument* doc = &app.Documents[doc_n]; + if (!doc->Open) + continue; + + ImGuiTabItemFlags tab_flags = (doc->Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); + bool visible = ImGui::BeginTabItem(doc->Name, &doc->Open, tab_flags); + + // Cancel attempt to close when unsaved add to save queue so we can display a popup. + if (!doc->Open && doc->Dirty) + { + doc->Open = true; + doc->DoQueueClose(); + } + + MyDocument::DisplayContextMenu(doc); + if (visible) + { + MyDocument::DisplayContents(doc); + ImGui::EndTabItem(); + } + } + + ImGui::EndTabBar(); + } + } + + // Update closing queue + static ImVector close_queue; + if (close_queue.empty()) + { + // Close queue is locked once we started a popup + for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + { + MyDocument* doc = &app.Documents[doc_n]; + if (doc->WantClose) + { + doc->WantClose = false; + close_queue.push_back(doc); + } + } + } + + // Display closing confirmation UI + if (!close_queue.empty()) + { + int close_queue_unsaved_documents = 0; + for (int n = 0; n < close_queue.Size; n++) + if (close_queue[n]->Dirty) + close_queue_unsaved_documents++; + + if (close_queue_unsaved_documents == 0) + { + // Close documents when all are unsaved + for (int n = 0; n < close_queue.Size; n++) + close_queue[n]->DoForceClose(); + close_queue.clear(); + } + else + { + if (!ImGui::IsPopupOpen("Save?")) + ImGui::OpenPopup("Save?"); + if (ImGui::BeginPopupModal("Save?")) + { + ImGui::Text("Save change to the following items?"); + ImGui::SetNextItemWidth(-1.0f); + if (ImGui::ListBoxHeader("##", close_queue_unsaved_documents, 6)) + { + for (int n = 0; n < close_queue.Size; n++) + if (close_queue[n]->Dirty) + ImGui::Text("%s", close_queue[n]->Name); + ImGui::ListBoxFooter(); + } + + if (ImGui::Button("Yes", ImVec2(80, 0))) + { + for (int n = 0; n < close_queue.Size; n++) + { + if (close_queue[n]->Dirty) + close_queue[n]->DoSave(); + close_queue[n]->DoForceClose(); + } + close_queue.clear(); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("No", ImVec2(80, 0))) + { + for (int n = 0; n < close_queue.Size; n++) + close_queue[n]->DoForceClose(); + close_queue.clear(); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button("Cancel", ImVec2(80, 0))) + { + close_queue.clear(); + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } + } + } + + ImGui::End(); +} + +// End of Demo code +#else + +void ImGui::ShowAboutWindow(bool*) {} +void ImGui::ShowDemoWindow(bool*) {} +void ImGui::ShowUserGuide() {} +void ImGui::ShowStyleEditor(ImGuiStyle*) {} + +#endif diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp new file mode 100644 index 00000000..4a6dce6d --- /dev/null +++ b/imgui/imgui_draw.cpp @@ -0,0 +1,3367 @@ +// dear imgui, v1.74 WIP +// (drawing and font code) + +/* + +Index of this file: + +// [SECTION] STB libraries implementation +// [SECTION] Style functions +// [SECTION] ImDrawList +// [SECTION] ImDrawListSplitter +// [SECTION] ImDrawData +// [SECTION] Helpers ShadeVertsXXX functions +// [SECTION] ImFontConfig +// [SECTION] ImFontAtlas +// [SECTION] ImFontAtlas glyph ranges helpers +// [SECTION] ImFontGlyphRangesBuilder +// [SECTION] ImFont +// [SECTION] Internal Render Helpers +// [SECTION] Decompression code +// [SECTION] Default font data (ProggyClean.ttf) + +*/ + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#ifndef IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_MATH_OPERATORS +#endif +#include "imgui_internal.h" + +#include // vsnprintf, sscanf, printf +#if !defined(alloca) +#if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__SWITCH__) +#include // alloca (glibc uses . Note that Cygwin may have _WIN32 defined, so the order matters here) +#elif defined(_WIN32) +#include // alloca +#if !defined(alloca) +#define alloca _alloca // for clang with MS Codegen +#endif +#else +#include // alloca +#endif +#endif + +// Visual Studio warnings +#ifdef _MSC_VER +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#endif + +// Clang/GCC warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok. +#pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 +#endif +#if __has_warning("-Wcomma") +#pragma clang diagnostic ignored "-Wcomma" // warning : possible misuse of comma operator here // +#endif +#if __has_warning("-Wreserved-id-macro") +#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning : macro name is a reserved identifier // +#endif +#if __has_warning("-Wdouble-promotion") +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#endif +#elif defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used +#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function +#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value +#pragma GCC diagnostic ignored "-Wstack-protector" // warning: stack protector not protecting local variables: variable length buffer +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +//------------------------------------------------------------------------- +// [SECTION] STB libraries implementation +//------------------------------------------------------------------------- + +// Compile time options: +//#define IMGUI_STB_NAMESPACE ImStb +//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" +//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" +//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION +//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION + +#ifdef IMGUI_STB_NAMESPACE +namespace IMGUI_STB_NAMESPACE +{ +#endif + +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration +#endif + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" +#pragma clang diagnostic ignored "-Wmissing-prototypes" +#pragma clang diagnostic ignored "-Wimplicit-fallthrough" +#pragma clang diagnostic ignored "-Wcast-qual" // warning : cast from 'const xxxx *' to 'xxx *' drops const qualifier // +#endif + +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wtype-limits" // warning: comparison is always true due to limited range of data type [-Wtype-limits] +#pragma GCC diagnostic ignored "-Wcast-qual" // warning: cast from type 'const xxxx *' to type 'xxxx *' casts away qualifiers +#endif + +#ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) +#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION +#define STBRP_STATIC +#define STBRP_ASSERT(x) IM_ASSERT(x) +#define STBRP_SORT ImQsort +#define STB_RECT_PACK_IMPLEMENTATION +#endif +#ifdef IMGUI_STB_RECT_PACK_FILENAME +#include IMGUI_STB_RECT_PACK_FILENAME +#else +#include "imstb_rectpack.h" +#endif +#endif + +#ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) +#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION +#define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) +#define STBTT_free(x,u) ((void)(u), IM_FREE(x)) +#define STBTT_assert(x) IM_ASSERT(x) +#define STBTT_fmod(x,y) ImFmod(x,y) +#define STBTT_sqrt(x) ImSqrt(x) +#define STBTT_pow(x,y) ImPow(x,y) +#define STBTT_fabs(x) ImFabs(x) +#define STBTT_ifloor(x) ((int)ImFloorStd(x)) +#define STBTT_iceil(x) ((int)ImCeil(x)) +#define STBTT_STATIC +#define STB_TRUETYPE_IMPLEMENTATION +#else +#define STBTT_DEF extern +#endif +#ifdef IMGUI_STB_TRUETYPE_FILENAME +#include IMGUI_STB_TRUETYPE_FILENAME +#else +#include "imstb_truetype.h" +#endif +#endif + +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif + +#ifdef IMGUI_STB_NAMESPACE +} // namespace ImStb +using namespace IMGUI_STB_NAMESPACE; +#endif + +//----------------------------------------------------------------------------- +// [SECTION] Style functions +//----------------------------------------------------------------------------- + +void ImGui::StyleColorsDark(ImGuiStyle* dst) +{ + ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); + ImVec4* colors = style->Colors; + + colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); + colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); + colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); + colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); + colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_FrameBg] = ImVec4(0.16f, 0.29f, 0.48f, 0.54f); + colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); + colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); + colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); + colors[ImGuiCol_TitleBgActive] = ImVec4(0.16f, 0.29f, 0.48f, 1.00f); + colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); + colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); + colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); + colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); + colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); + colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); + colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f); + colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); + colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); + colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); + colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); + colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; + colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); + colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.25f); + colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); + colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); + colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); + colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); + colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); + colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); + colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); + colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); + colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); + colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); +} + +void ImGui::StyleColorsClassic(ImGuiStyle* dst) +{ + ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); + ImVec4* colors = style->Colors; + + colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); + colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); + colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.70f); + colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f); + colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f); + colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_FrameBg] = ImVec4(0.43f, 0.43f, 0.43f, 0.39f); + colors[ImGuiCol_FrameBgHovered] = ImVec4(0.47f, 0.47f, 0.69f, 0.40f); + colors[ImGuiCol_FrameBgActive] = ImVec4(0.42f, 0.41f, 0.64f, 0.69f); + colors[ImGuiCol_TitleBg] = ImVec4(0.27f, 0.27f, 0.54f, 0.83f); + colors[ImGuiCol_TitleBgActive] = ImVec4(0.32f, 0.32f, 0.63f, 0.87f); + colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.40f, 0.40f, 0.80f, 0.20f); + colors[ImGuiCol_MenuBarBg] = ImVec4(0.40f, 0.40f, 0.55f, 0.80f); + colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.25f, 0.30f, 0.60f); + colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.40f, 0.40f, 0.80f, 0.30f); + colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.80f, 0.40f); + colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); + colors[ImGuiCol_CheckMark] = ImVec4(0.90f, 0.90f, 0.90f, 0.50f); + colors[ImGuiCol_SliderGrab] = ImVec4(1.00f, 1.00f, 1.00f, 0.30f); + colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.39f, 0.80f, 0.60f); + colors[ImGuiCol_Button] = ImVec4(0.35f, 0.40f, 0.61f, 0.62f); + colors[ImGuiCol_ButtonHovered] = ImVec4(0.40f, 0.48f, 0.71f, 0.79f); + colors[ImGuiCol_ButtonActive] = ImVec4(0.46f, 0.54f, 0.80f, 1.00f); + colors[ImGuiCol_Header] = ImVec4(0.40f, 0.40f, 0.90f, 0.45f); + colors[ImGuiCol_HeaderHovered] = ImVec4(0.45f, 0.45f, 0.90f, 0.80f); + colors[ImGuiCol_HeaderActive] = ImVec4(0.53f, 0.53f, 0.87f, 0.80f); + colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f); + colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); + colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f); + colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.16f); + colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); + colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); + colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); + colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); + colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); + colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); + colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); + colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); + colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); +} + +// Those light colors are better suited with a thicker font than the default one + FrameBorder +void ImGui::StyleColorsLight(ImGuiStyle* dst) +{ + ImGuiStyle* style = dst ? dst : &ImGui::GetStyle(); + ImVec4* colors = style->Colors; + + colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f); + colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); + colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); + colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.98f); + colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.30f); + colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); + colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); + colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f); + colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f); + colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f); + colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); + colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f); + colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f); + colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f); + colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f); + colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f); + colors[ImGuiCol_SliderGrabActive] = ImVec4(0.46f, 0.54f, 0.80f, 0.60f); + colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); + colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f); + colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f); + colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); + colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 0.62f); + colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); + colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.80f, 0.80f, 0.80f, 0.56f); + colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); + colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); + colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); + colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); + colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); + colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f); + colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); + colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); + colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); + colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f); + colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); +} + +//----------------------------------------------------------------------------- +// ImDrawList +//----------------------------------------------------------------------------- + +ImDrawListSharedData::ImDrawListSharedData() +{ + Font = NULL; + FontSize = 0.0f; + CurveTessellationTol = 0.0f; + ClipRectFullscreen = ImVec4(-8192.0f, -8192.0f, +8192.0f, +8192.0f); + InitialFlags = ImDrawListFlags_None; + + // Const data + for (int i = 0; i < IM_ARRAYSIZE(CircleVtx12); i++) + { + const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(CircleVtx12); + CircleVtx12[i] = ImVec2(ImCos(a), ImSin(a)); + } +} + +void ImDrawList::Clear() +{ + CmdBuffer.resize(0); + IdxBuffer.resize(0); + VtxBuffer.resize(0); + Flags = _Data ? _Data->InitialFlags : ImDrawListFlags_None; + _VtxCurrentOffset = 0; + _VtxCurrentIdx = 0; + _VtxWritePtr = NULL; + _IdxWritePtr = NULL; + _ClipRectStack.resize(0); + _TextureIdStack.resize(0); + _Path.resize(0); + _Splitter.Clear(); +} + +void ImDrawList::ClearFreeMemory() +{ + CmdBuffer.clear(); + IdxBuffer.clear(); + VtxBuffer.clear(); + _VtxCurrentIdx = 0; + _VtxWritePtr = NULL; + _IdxWritePtr = NULL; + _ClipRectStack.clear(); + _TextureIdStack.clear(); + _Path.clear(); + _Splitter.ClearFreeMemory(); +} + +ImDrawList* ImDrawList::CloneOutput() const +{ + ImDrawList* dst = IM_NEW(ImDrawList(_Data)); + dst->CmdBuffer = CmdBuffer; + dst->IdxBuffer = IdxBuffer; + dst->VtxBuffer = VtxBuffer; + dst->Flags = Flags; + return dst; +} + +// Using macros because C++ is a terrible language, we want guaranteed inline, no code in header, and no overhead in Debug builds +#define GetCurrentClipRect() (_ClipRectStack.Size ? _ClipRectStack.Data[_ClipRectStack.Size-1] : _Data->ClipRectFullscreen) +#define GetCurrentTextureId() (_TextureIdStack.Size ? _TextureIdStack.Data[_TextureIdStack.Size-1] : (ImTextureID)NULL) + +void ImDrawList::AddDrawCmd() +{ + ImDrawCmd draw_cmd; + draw_cmd.ClipRect = GetCurrentClipRect(); + draw_cmd.TextureId = GetCurrentTextureId(); + draw_cmd.VtxOffset = _VtxCurrentOffset; + draw_cmd.IdxOffset = IdxBuffer.Size; + + IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); + CmdBuffer.push_back(draw_cmd); +} + +void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) +{ + ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; + if (!current_cmd || current_cmd->ElemCount != 0 || current_cmd->UserCallback != NULL) + { + AddDrawCmd(); + current_cmd = &CmdBuffer.back(); + } + current_cmd->UserCallback = callback; + current_cmd->UserCallbackData = callback_data; + + AddDrawCmd(); // Force a new command after us (see comment below) +} + +// Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack. +// The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only. +void ImDrawList::UpdateClipRect() +{ + // If current command is used with different settings we need to add a new command + const ImVec4 curr_clip_rect = GetCurrentClipRect(); + ImDrawCmd* curr_cmd = CmdBuffer.Size > 0 ? &CmdBuffer.Data[CmdBuffer.Size-1] : NULL; + if (!curr_cmd || (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) != 0) || curr_cmd->UserCallback != NULL) + { + AddDrawCmd(); + return; + } + + // Try to merge with previous command if it matches, else use current command + ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; + if (curr_cmd->ElemCount == 0 && prev_cmd && memcmp(&prev_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) == 0 && prev_cmd->TextureId == GetCurrentTextureId() && prev_cmd->UserCallback == NULL) + CmdBuffer.pop_back(); + else + curr_cmd->ClipRect = curr_clip_rect; +} + +void ImDrawList::UpdateTextureID() +{ + // If current command is used with different settings we need to add a new command + const ImTextureID curr_texture_id = GetCurrentTextureId(); + ImDrawCmd* curr_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; + if (!curr_cmd || (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != curr_texture_id) || curr_cmd->UserCallback != NULL) + { + AddDrawCmd(); + return; + } + + // Try to merge with previous command if it matches, else use current command + ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; + if (curr_cmd->ElemCount == 0 && prev_cmd && prev_cmd->TextureId == curr_texture_id && memcmp(&prev_cmd->ClipRect, &GetCurrentClipRect(), sizeof(ImVec4)) == 0 && prev_cmd->UserCallback == NULL) + CmdBuffer.pop_back(); + else + curr_cmd->TextureId = curr_texture_id; +} + +#undef GetCurrentClipRect +#undef GetCurrentTextureId + +// Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) +void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) +{ + ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); + if (intersect_with_current_clip_rect && _ClipRectStack.Size) + { + ImVec4 current = _ClipRectStack.Data[_ClipRectStack.Size-1]; + if (cr.x < current.x) cr.x = current.x; + if (cr.y < current.y) cr.y = current.y; + if (cr.z > current.z) cr.z = current.z; + if (cr.w > current.w) cr.w = current.w; + } + cr.z = ImMax(cr.x, cr.z); + cr.w = ImMax(cr.y, cr.w); + + _ClipRectStack.push_back(cr); + UpdateClipRect(); +} + +void ImDrawList::PushClipRectFullScreen() +{ + PushClipRect(ImVec2(_Data->ClipRectFullscreen.x, _Data->ClipRectFullscreen.y), ImVec2(_Data->ClipRectFullscreen.z, _Data->ClipRectFullscreen.w)); +} + +void ImDrawList::PopClipRect() +{ + IM_ASSERT(_ClipRectStack.Size > 0); + _ClipRectStack.pop_back(); + UpdateClipRect(); +} + +void ImDrawList::PushTextureID(ImTextureID texture_id) +{ + _TextureIdStack.push_back(texture_id); + UpdateTextureID(); +} + +void ImDrawList::PopTextureID() +{ + IM_ASSERT(_TextureIdStack.Size > 0); + _TextureIdStack.pop_back(); + UpdateTextureID(); +} + +// NB: this can be called with negative count for removing primitives (as long as the result does not underflow) +void ImDrawList::PrimReserve(int idx_count, int vtx_count) +{ + // Large mesh support (when enabled) + if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) + { + _VtxCurrentOffset = VtxBuffer.Size; + _VtxCurrentIdx = 0; + AddDrawCmd(); + } + + ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size-1]; + draw_cmd.ElemCount += idx_count; + + int vtx_buffer_old_size = VtxBuffer.Size; + VtxBuffer.resize(vtx_buffer_old_size + vtx_count); + _VtxWritePtr = VtxBuffer.Data + vtx_buffer_old_size; + + int idx_buffer_old_size = IdxBuffer.Size; + IdxBuffer.resize(idx_buffer_old_size + idx_count); + _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; +} + +// Fully unrolled with inline call to keep our debug builds decently fast. +void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col) +{ + ImVec2 b(c.x, a.y), d(a.x, c.y), uv(_Data->TexUvWhitePixel); + ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; + _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); + _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); + _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; + _VtxWritePtr += 4; + _VtxCurrentIdx += 4; + _IdxWritePtr += 6; +} + +void ImDrawList::PrimRectUV(const ImVec2& a, const ImVec2& c, const ImVec2& uv_a, const ImVec2& uv_c, ImU32 col) +{ + ImVec2 b(c.x, a.y), d(a.x, c.y), uv_b(uv_c.x, uv_a.y), uv_d(uv_a.x, uv_c.y); + ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; + _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); + _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); + _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; + _VtxWritePtr += 4; + _VtxCurrentIdx += 4; + _IdxWritePtr += 6; +} + +void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col) +{ + ImDrawIdx idx = (ImDrawIdx)_VtxCurrentIdx; + _IdxWritePtr[0] = idx; _IdxWritePtr[1] = (ImDrawIdx)(idx+1); _IdxWritePtr[2] = (ImDrawIdx)(idx+2); + _IdxWritePtr[3] = idx; _IdxWritePtr[4] = (ImDrawIdx)(idx+2); _IdxWritePtr[5] = (ImDrawIdx)(idx+3); + _VtxWritePtr[0].pos = a; _VtxWritePtr[0].uv = uv_a; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos = b; _VtxWritePtr[1].uv = uv_b; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos = c; _VtxWritePtr[2].uv = uv_c; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos = d; _VtxWritePtr[3].uv = uv_d; _VtxWritePtr[3].col = col; + _VtxWritePtr += 4; + _VtxCurrentIdx += 4; + _IdxWritePtr += 6; +} + +// On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superflous function calls to optimize debug/non-inlined builds. +// Those macros expects l-values. +#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } } +#define IM_FIXNORMAL2F(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; } + +// TODO: Thickness anti-aliased lines cap are missing their AA fringe. +// We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. +void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, bool closed, float thickness) +{ + if (points_count < 2) + return; + + const ImVec2 uv = _Data->TexUvWhitePixel; + + int count = points_count; + if (!closed) + count = points_count-1; + + const bool thick_line = thickness > 1.0f; + if (Flags & ImDrawListFlags_AntiAliasedLines) + { + // Anti-aliased stroke + const float AA_SIZE = 1.0f; + const ImU32 col_trans = col & ~IM_COL32_A_MASK; + + const int idx_count = thick_line ? count*18 : count*12; + const int vtx_count = thick_line ? points_count*4 : points_count*3; + PrimReserve(idx_count, vtx_count); + + // Temporary buffer + ImVec2* temp_normals = (ImVec2*)alloca(points_count * (thick_line ? 5 : 3) * sizeof(ImVec2)); //-V630 + ImVec2* temp_points = temp_normals + points_count; + + for (int i1 = 0; i1 < count; i1++) + { + const int i2 = (i1+1) == points_count ? 0 : i1+1; + float dx = points[i2].x - points[i1].x; + float dy = points[i2].y - points[i1].y; + IM_NORMALIZE2F_OVER_ZERO(dx, dy); + temp_normals[i1].x = dy; + temp_normals[i1].y = -dx; + } + if (!closed) + temp_normals[points_count-1] = temp_normals[points_count-2]; + + if (!thick_line) + { + if (!closed) + { + temp_points[0] = points[0] + temp_normals[0] * AA_SIZE; + temp_points[1] = points[0] - temp_normals[0] * AA_SIZE; + temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * AA_SIZE; + temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * AA_SIZE; + } + + // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. + unsigned int idx1 = _VtxCurrentIdx; + for (int i1 = 0; i1 < count; i1++) + { + const int i2 = (i1+1) == points_count ? 0 : i1+1; + unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+3; + + // Average normals + float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; + float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; + IM_FIXNORMAL2F(dm_x, dm_y) + dm_x *= AA_SIZE; + dm_y *= AA_SIZE; + + // Add temporary vertexes + ImVec2* out_vtx = &temp_points[i2*2]; + out_vtx[0].x = points[i2].x + dm_x; + out_vtx[0].y = points[i2].y + dm_y; + out_vtx[1].x = points[i2].x - dm_x; + out_vtx[1].y = points[i2].y - dm_y; + + // Add indexes + _IdxWritePtr[0] = (ImDrawIdx)(idx2+0); _IdxWritePtr[1] = (ImDrawIdx)(idx1+0); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); + _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+0); + _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); + _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10]= (ImDrawIdx)(idx2+0); _IdxWritePtr[11]= (ImDrawIdx)(idx2+1); + _IdxWritePtr += 12; + + idx1 = idx2; + } + + // Add vertexes + for (int i = 0; i < points_count; i++) + { + _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; + _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col_trans; + _VtxWritePtr += 3; + } + } + else + { + const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; + if (!closed) + { + temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE); + temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness); + temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness); + temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE); + temp_points[(points_count-1)*4+0] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); + temp_points[(points_count-1)*4+1] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness); + temp_points[(points_count-1)*4+2] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness); + temp_points[(points_count-1)*4+3] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); + } + + // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. + unsigned int idx1 = _VtxCurrentIdx; + for (int i1 = 0; i1 < count; i1++) + { + const int i2 = (i1+1) == points_count ? 0 : i1+1; + unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+4; + + // Average normals + float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; + float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; + IM_FIXNORMAL2F(dm_x, dm_y); + float dm_out_x = dm_x * (half_inner_thickness + AA_SIZE); + float dm_out_y = dm_y * (half_inner_thickness + AA_SIZE); + float dm_in_x = dm_x * half_inner_thickness; + float dm_in_y = dm_y * half_inner_thickness; + + // Add temporary vertexes + ImVec2* out_vtx = &temp_points[i2*4]; + out_vtx[0].x = points[i2].x + dm_out_x; + out_vtx[0].y = points[i2].y + dm_out_y; + out_vtx[1].x = points[i2].x + dm_in_x; + out_vtx[1].y = points[i2].y + dm_in_y; + out_vtx[2].x = points[i2].x - dm_in_x; + out_vtx[2].y = points[i2].y - dm_in_y; + out_vtx[3].x = points[i2].x - dm_out_x; + out_vtx[3].y = points[i2].y - dm_out_y; + + // Add indexes + _IdxWritePtr[0] = (ImDrawIdx)(idx2+1); _IdxWritePtr[1] = (ImDrawIdx)(idx1+1); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); + _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+1); + _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); + _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10] = (ImDrawIdx)(idx2+0); _IdxWritePtr[11] = (ImDrawIdx)(idx2+1); + _IdxWritePtr[12] = (ImDrawIdx)(idx2+2); _IdxWritePtr[13] = (ImDrawIdx)(idx1+2); _IdxWritePtr[14] = (ImDrawIdx)(idx1+3); + _IdxWritePtr[15] = (ImDrawIdx)(idx1+3); _IdxWritePtr[16] = (ImDrawIdx)(idx2+3); _IdxWritePtr[17] = (ImDrawIdx)(idx2+2); + _IdxWritePtr += 18; + + idx1 = idx2; + } + + // Add vertexes + for (int i = 0; i < points_count; i++) + { + _VtxWritePtr[0].pos = temp_points[i*4+0]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col_trans; + _VtxWritePtr[1].pos = temp_points[i*4+1]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos = temp_points[i*4+2]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos = temp_points[i*4+3]; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col_trans; + _VtxWritePtr += 4; + } + } + _VtxCurrentIdx += (ImDrawIdx)vtx_count; + } + else + { + // Non Anti-aliased Stroke + const int idx_count = count*6; + const int vtx_count = count*4; // FIXME-OPT: Not sharing edges + PrimReserve(idx_count, vtx_count); + + for (int i1 = 0; i1 < count; i1++) + { + const int i2 = (i1+1) == points_count ? 0 : i1+1; + const ImVec2& p1 = points[i1]; + const ImVec2& p2 = points[i2]; + + float dx = p2.x - p1.x; + float dy = p2.y - p1.y; + IM_NORMALIZE2F_OVER_ZERO(dx, dy); + dx *= (thickness * 0.5f); + dy *= (thickness * 0.5f); + + _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; + _VtxWritePtr += 4; + + _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2); + _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3); + _IdxWritePtr += 6; + _VtxCurrentIdx += 4; + } + } +} + +// We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. +void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) +{ + if (points_count < 3) + return; + + const ImVec2 uv = _Data->TexUvWhitePixel; + + if (Flags & ImDrawListFlags_AntiAliasedFill) + { + // Anti-aliased Fill + const float AA_SIZE = 1.0f; + const ImU32 col_trans = col & ~IM_COL32_A_MASK; + const int idx_count = (points_count-2)*3 + points_count*6; + const int vtx_count = (points_count*2); + PrimReserve(idx_count, vtx_count); + + // Add indexes for fill + unsigned int vtx_inner_idx = _VtxCurrentIdx; + unsigned int vtx_outer_idx = _VtxCurrentIdx+1; + for (int i = 2; i < points_count; i++) + { + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+((i-1)<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx+(i<<1)); + _IdxWritePtr += 3; + } + + // Compute normals + ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2)); //-V630 + for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) + { + const ImVec2& p0 = points[i0]; + const ImVec2& p1 = points[i1]; + float dx = p1.x - p0.x; + float dy = p1.y - p0.y; + IM_NORMALIZE2F_OVER_ZERO(dx, dy); + temp_normals[i0].x = dy; + temp_normals[i0].y = -dx; + } + + for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) + { + // Average normals + const ImVec2& n0 = temp_normals[i0]; + const ImVec2& n1 = temp_normals[i1]; + float dm_x = (n0.x + n1.x) * 0.5f; + float dm_y = (n0.y + n1.y) * 0.5f; + IM_FIXNORMAL2F(dm_x, dm_y); + dm_x *= AA_SIZE * 0.5f; + dm_y *= AA_SIZE * 0.5f; + + // Add vertices + _VtxWritePtr[0].pos.x = (points[i1].x - dm_x); _VtxWritePtr[0].pos.y = (points[i1].y - dm_y); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; // Inner + _VtxWritePtr[1].pos.x = (points[i1].x + dm_x); _VtxWritePtr[1].pos.y = (points[i1].y + dm_y); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; // Outer + _VtxWritePtr += 2; + + // Add indexes for fringes + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+(i0<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); + _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx+(i1<<1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); + _IdxWritePtr += 6; + } + _VtxCurrentIdx += (ImDrawIdx)vtx_count; + } + else + { + // Non Anti-aliased Fill + const int idx_count = (points_count-2)*3; + const int vtx_count = points_count; + PrimReserve(idx_count, vtx_count); + for (int i = 0; i < vtx_count; i++) + { + _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; + _VtxWritePtr++; + } + for (int i = 2; i < points_count; i++) + { + _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+i-1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+i); + _IdxWritePtr += 3; + } + _VtxCurrentIdx += (ImDrawIdx)vtx_count; + } +} + +void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) +{ + if (radius == 0.0f || a_min_of_12 > a_max_of_12) + { + _Path.push_back(center); + return; + } + _Path.reserve(_Path.Size + (a_max_of_12 - a_min_of_12 + 1)); + for (int a = a_min_of_12; a <= a_max_of_12; a++) + { + const ImVec2& c = _Data->CircleVtx12[a % IM_ARRAYSIZE(_Data->CircleVtx12)]; + _Path.push_back(ImVec2(center.x + c.x * radius, center.y + c.y * radius)); + } +} + +void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) +{ + if (radius == 0.0f) + { + _Path.push_back(center); + return; + } + + // Note that we are adding a point at both a_min and a_max. + // If you are trying to draw a full closed circle you don't want the overlapping points! + _Path.reserve(_Path.Size + (num_segments + 1)); + for (int i = 0; i <= num_segments; i++) + { + const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); + _Path.push_back(ImVec2(center.x + ImCos(a) * radius, center.y + ImSin(a) * radius)); + } +} + +static void PathBezierToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) +{ + float dx = x4 - x1; + float dy = y4 - y1; + float d2 = ((x2 - x4) * dy - (y2 - y4) * dx); + float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); + d2 = (d2 >= 0) ? d2 : -d2; + d3 = (d3 >= 0) ? d3 : -d3; + if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy)) + { + path->push_back(ImVec2(x4, y4)); + } + else if (level < 10) + { + float x12 = (x1+x2)*0.5f, y12 = (y1+y2)*0.5f; + float x23 = (x2+x3)*0.5f, y23 = (y2+y3)*0.5f; + float x34 = (x3+x4)*0.5f, y34 = (y3+y4)*0.5f; + float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; + float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; + float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; + + PathBezierToCasteljau(path, x1,y1, x12,y12, x123,y123, x1234,y1234, tess_tol, level+1); + PathBezierToCasteljau(path, x1234,y1234, x234,y234, x34,y34, x4,y4, tess_tol, level+1); + } +} + +void ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) +{ + ImVec2 p1 = _Path.back(); + if (num_segments == 0) + { + // Auto-tessellated + PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); + } + else + { + float t_step = 1.0f / (float)num_segments; + for (int i_step = 1; i_step <= num_segments; i_step++) + { + float t = t_step * i_step; + float u = 1.0f - t; + float w1 = u*u*u; + float w2 = 3*u*u*t; + float w3 = 3*u*t*t; + float w4 = t*t*t; + _Path.push_back(ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y)); + } + } +} + +void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawCornerFlags rounding_corners) +{ + rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((rounding_corners & ImDrawCornerFlags_Top) == ImDrawCornerFlags_Top) || ((rounding_corners & ImDrawCornerFlags_Bot) == ImDrawCornerFlags_Bot) ? 0.5f : 1.0f ) - 1.0f); + rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((rounding_corners & ImDrawCornerFlags_Left) == ImDrawCornerFlags_Left) || ((rounding_corners & ImDrawCornerFlags_Right) == ImDrawCornerFlags_Right) ? 0.5f : 1.0f ) - 1.0f); + + if (rounding <= 0.0f || rounding_corners == 0) + { + PathLineTo(a); + PathLineTo(ImVec2(b.x, a.y)); + PathLineTo(b); + PathLineTo(ImVec2(a.x, b.y)); + } + else + { + const float rounding_tl = (rounding_corners & ImDrawCornerFlags_TopLeft) ? rounding : 0.0f; + const float rounding_tr = (rounding_corners & ImDrawCornerFlags_TopRight) ? rounding : 0.0f; + const float rounding_br = (rounding_corners & ImDrawCornerFlags_BotRight) ? rounding : 0.0f; + const float rounding_bl = (rounding_corners & ImDrawCornerFlags_BotLeft) ? rounding : 0.0f; + PathArcToFast(ImVec2(a.x + rounding_tl, a.y + rounding_tl), rounding_tl, 6, 9); + PathArcToFast(ImVec2(b.x - rounding_tr, a.y + rounding_tr), rounding_tr, 9, 12); + PathArcToFast(ImVec2(b.x - rounding_br, b.y - rounding_br), rounding_br, 0, 3); + PathArcToFast(ImVec2(a.x + rounding_bl, b.y - rounding_bl), rounding_bl, 3, 6); + } +} + +void ImDrawList::AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + PathLineTo(p1 + ImVec2(0.5f, 0.5f)); + PathLineTo(p2 + ImVec2(0.5f, 0.5f)); + PathStroke(col, false, thickness); +} + +// p_min = upper-left, p_max = lower-right +// Note we don't render 1 pixels sized rectangles properly. +void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + if (Flags & ImDrawListFlags_AntiAliasedLines) + PathRect(p_min + ImVec2(0.50f,0.50f), p_max - ImVec2(0.50f,0.50f), rounding, rounding_corners); + else + PathRect(p_min + ImVec2(0.50f,0.50f), p_max - ImVec2(0.49f,0.49f), rounding, rounding_corners); // Better looking lower-right corner and rounded non-AA shapes. + PathStroke(col, true, thickness); +} + +void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + if (rounding > 0.0f) + { + PathRect(p_min, p_max, rounding, rounding_corners); + PathFillConvex(col); + } + else + { + PrimReserve(6, 4); + PrimRect(p_min, p_max, col); + } +} + +// p_min = upper-left, p_max = lower-right +void ImDrawList::AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left) +{ + if (((col_upr_left | col_upr_right | col_bot_right | col_bot_left) & IM_COL32_A_MASK) == 0) + return; + + const ImVec2 uv = _Data->TexUvWhitePixel; + PrimReserve(6, 4); + PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); + PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+3)); + PrimWriteVtx(p_min, uv, col_upr_left); + PrimWriteVtx(ImVec2(p_max.x, p_min.y), uv, col_upr_right); + PrimWriteVtx(p_max, uv, col_bot_right); + PrimWriteVtx(ImVec2(p_min.x, p_max.y), uv, col_bot_left); +} + +void ImDrawList::AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(p1); + PathLineTo(p2); + PathLineTo(p3); + PathLineTo(p4); + PathStroke(col, true, thickness); +} + +void ImDrawList::AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(p1); + PathLineTo(p2); + PathLineTo(p3); + PathLineTo(p4); + PathFillConvex(col); +} + +void ImDrawList::AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(p1); + PathLineTo(p2); + PathLineTo(p3); + PathStroke(col, true, thickness); +} + +void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(p1); + PathLineTo(p2); + PathLineTo(p3); + PathFillConvex(col); +} + +void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) + return; + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; + PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); + PathStroke(col, true, thickness); +} + +void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) +{ + if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) + return; + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; + PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); + PathFillConvex(col); +} + +void ImDrawList::AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(pos0); + PathBezierCurveTo(cp0, cp1, pos1, num_segments); + PathStroke(col, false, thickness); +} + +void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + if (text_end == NULL) + text_end = text_begin + strlen(text_begin); + if (text_begin == text_end) + return; + + // Pull default font/size from the shared ImDrawListSharedData instance + if (font == NULL) + font = _Data->Font; + if (font_size == 0.0f) + font_size = _Data->FontSize; + + IM_ASSERT(font->ContainerAtlas->TexID == _TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. + + ImVec4 clip_rect = _ClipRectStack.back(); + if (cpu_fine_clip_rect) + { + clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x); + clip_rect.y = ImMax(clip_rect.y, cpu_fine_clip_rect->y); + clip_rect.z = ImMin(clip_rect.z, cpu_fine_clip_rect->z); + clip_rect.w = ImMin(clip_rect.w, cpu_fine_clip_rect->w); + } + font->RenderText(this, font_size, pos, col, clip_rect, text_begin, text_end, wrap_width, cpu_fine_clip_rect != NULL); +} + +void ImDrawList::AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end) +{ + AddText(NULL, 0.0f, pos, col, text_begin, text_end); +} + +void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + if (push_texture_id) + PushTextureID(user_texture_id); + + PrimReserve(6, 4); + PrimRectUV(p_min, p_max, uv_min, uv_max, col); + + if (push_texture_id) + PopTextureID(); +} + +void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1, const ImVec2& uv2, const ImVec2& uv3, const ImVec2& uv4, ImU32 col) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + if (push_texture_id) + PushTextureID(user_texture_id); + + PrimReserve(6, 4); + PrimQuadUV(p1, p2, p3, p4, uv1, uv2, uv3, uv4, col); + + if (push_texture_id) + PopTextureID(); +} + +void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + if (rounding <= 0.0f || (rounding_corners & ImDrawCornerFlags_All) == 0) + { + AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col); + return; + } + + const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + if (push_texture_id) + PushTextureID(user_texture_id); + + int vert_start_idx = VtxBuffer.Size; + PathRect(p_min, p_max, rounding, rounding_corners); + PathFillConvex(col); + int vert_end_idx = VtxBuffer.Size; + ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, p_min, p_max, uv_min, uv_max, true); + + if (push_texture_id) + PopTextureID(); +} + + +//----------------------------------------------------------------------------- +// ImDrawListSplitter +//----------------------------------------------------------------------------- +// FIXME: This may be a little confusing, trying to be a little too low-level/optimal instead of just doing vector swap.. +//----------------------------------------------------------------------------- + +void ImDrawListSplitter::ClearFreeMemory() +{ + for (int i = 0; i < _Channels.Size; i++) + { + if (i == _Current) + memset(&_Channels[i], 0, sizeof(_Channels[i])); // Current channel is a copy of CmdBuffer/IdxBuffer, don't destruct again + _Channels[i]._CmdBuffer.clear(); + _Channels[i]._IdxBuffer.clear(); + } + _Current = 0; + _Count = 1; + _Channels.clear(); +} + +void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) +{ + IM_ASSERT(_Current == 0 && _Count <= 1); + int old_channels_count = _Channels.Size; + if (old_channels_count < channels_count) + _Channels.resize(channels_count); + _Count = channels_count; + + // Channels[] (24/32 bytes each) hold storage that we'll swap with draw_list->_CmdBuffer/_IdxBuffer + // The content of Channels[0] at this point doesn't matter. We clear it to make state tidy in a debugger but we don't strictly need to. + // When we switch to the next channel, we'll copy draw_list->_CmdBuffer/_IdxBuffer into Channels[0] and then Channels[1] into draw_list->CmdBuffer/_IdxBuffer + memset(&_Channels[0], 0, sizeof(ImDrawChannel)); + for (int i = 1; i < channels_count; i++) + { + if (i >= old_channels_count) + { + IM_PLACEMENT_NEW(&_Channels[i]) ImDrawChannel(); + } + else + { + _Channels[i]._CmdBuffer.resize(0); + _Channels[i]._IdxBuffer.resize(0); + } + if (_Channels[i]._CmdBuffer.Size == 0) + { + ImDrawCmd draw_cmd; + draw_cmd.ClipRect = draw_list->_ClipRectStack.back(); + draw_cmd.TextureId = draw_list->_TextureIdStack.back(); + _Channels[i]._CmdBuffer.push_back(draw_cmd); + } + } +} + +static inline bool CanMergeDrawCommands(ImDrawCmd* a, ImDrawCmd* b) +{ + return memcmp(&a->ClipRect, &b->ClipRect, sizeof(a->ClipRect)) == 0 && a->TextureId == b->TextureId && a->VtxOffset == b->VtxOffset && !a->UserCallback && !b->UserCallback; +} + +void ImDrawListSplitter::Merge(ImDrawList* draw_list) +{ + // Note that we never use or rely on channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use. + if (_Count <= 1) + return; + + SetCurrentChannel(draw_list, 0); + if (draw_list->CmdBuffer.Size != 0 && draw_list->CmdBuffer.back().ElemCount == 0) + draw_list->CmdBuffer.pop_back(); + + // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command. + int new_cmd_buffer_count = 0; + int new_idx_buffer_count = 0; + ImDrawCmd* last_cmd = (_Count > 0 && draw_list->CmdBuffer.Size > 0) ? &draw_list->CmdBuffer.back() : NULL; + int idx_offset = last_cmd ? last_cmd->IdxOffset + last_cmd->ElemCount : 0; + for (int i = 1; i < _Count; i++) + { + ImDrawChannel& ch = _Channels[i]; + if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0) + ch._CmdBuffer.pop_back(); + if (ch._CmdBuffer.Size > 0 && last_cmd != NULL && CanMergeDrawCommands(last_cmd, &ch._CmdBuffer[0])) + { + // Merge previous channel last draw command with current channel first draw command if matching. + last_cmd->ElemCount += ch._CmdBuffer[0].ElemCount; + idx_offset += ch._CmdBuffer[0].ElemCount; + ch._CmdBuffer.erase(ch._CmdBuffer.Data); + } + if (ch._CmdBuffer.Size > 0) + last_cmd = &ch._CmdBuffer.back(); + new_cmd_buffer_count += ch._CmdBuffer.Size; + new_idx_buffer_count += ch._IdxBuffer.Size; + for (int cmd_n = 0; cmd_n < ch._CmdBuffer.Size; cmd_n++) + { + ch._CmdBuffer.Data[cmd_n].IdxOffset = idx_offset; + idx_offset += ch._CmdBuffer.Data[cmd_n].ElemCount; + } + } + draw_list->CmdBuffer.resize(draw_list->CmdBuffer.Size + new_cmd_buffer_count); + draw_list->IdxBuffer.resize(draw_list->IdxBuffer.Size + new_idx_buffer_count); + + // Write commands and indices in order (they are fairly small structures, we don't copy vertices only indices) + ImDrawCmd* cmd_write = draw_list->CmdBuffer.Data + draw_list->CmdBuffer.Size - new_cmd_buffer_count; + ImDrawIdx* idx_write = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size - new_idx_buffer_count; + for (int i = 1; i < _Count; i++) + { + ImDrawChannel& ch = _Channels[i]; + if (int sz = ch._CmdBuffer.Size) { memcpy(cmd_write, ch._CmdBuffer.Data, sz * sizeof(ImDrawCmd)); cmd_write += sz; } + if (int sz = ch._IdxBuffer.Size) { memcpy(idx_write, ch._IdxBuffer.Data, sz * sizeof(ImDrawIdx)); idx_write += sz; } + } + draw_list->_IdxWritePtr = idx_write; + draw_list->UpdateClipRect(); // We call this instead of AddDrawCmd(), so that empty channels won't produce an extra draw call. + draw_list->UpdateTextureID(); + _Count = 1; +} + +void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) +{ + IM_ASSERT(idx >= 0 && idx < _Count); + if (_Current == idx) + return; + // Overwrite ImVector (12/16 bytes), four times. This is merely a silly optimization instead of doing .swap() + memcpy(&_Channels.Data[_Current]._CmdBuffer, &draw_list->CmdBuffer, sizeof(draw_list->CmdBuffer)); + memcpy(&_Channels.Data[_Current]._IdxBuffer, &draw_list->IdxBuffer, sizeof(draw_list->IdxBuffer)); + _Current = idx; + memcpy(&draw_list->CmdBuffer, &_Channels.Data[idx]._CmdBuffer, sizeof(draw_list->CmdBuffer)); + memcpy(&draw_list->IdxBuffer, &_Channels.Data[idx]._IdxBuffer, sizeof(draw_list->IdxBuffer)); + draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size; +} + +//----------------------------------------------------------------------------- +// [SECTION] ImDrawData +//----------------------------------------------------------------------------- + +// For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! +void ImDrawData::DeIndexAllBuffers() +{ + ImVector new_vtx_buffer; + TotalVtxCount = TotalIdxCount = 0; + for (int i = 0; i < CmdListsCount; i++) + { + ImDrawList* cmd_list = CmdLists[i]; + if (cmd_list->IdxBuffer.empty()) + continue; + new_vtx_buffer.resize(cmd_list->IdxBuffer.Size); + for (int j = 0; j < cmd_list->IdxBuffer.Size; j++) + new_vtx_buffer[j] = cmd_list->VtxBuffer[cmd_list->IdxBuffer[j]]; + cmd_list->VtxBuffer.swap(new_vtx_buffer); + cmd_list->IdxBuffer.resize(0); + TotalVtxCount += cmd_list->VtxBuffer.Size; + } +} + +// Helper to scale the ClipRect field of each ImDrawCmd. +// Use if your final output buffer is at a different scale than draw_data->DisplaySize, +// or if there is a difference between your window resolution and framebuffer resolution. +void ImDrawData::ScaleClipRects(const ImVec2& fb_scale) +{ + for (int i = 0; i < CmdListsCount; i++) + { + ImDrawList* cmd_list = CmdLists[i]; + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + ImDrawCmd* cmd = &cmd_list->CmdBuffer[cmd_i]; + cmd->ClipRect = ImVec4(cmd->ClipRect.x * fb_scale.x, cmd->ClipRect.y * fb_scale.y, cmd->ClipRect.z * fb_scale.x, cmd->ClipRect.w * fb_scale.y); + } + } +} + +//----------------------------------------------------------------------------- +// [SECTION] Helpers ShadeVertsXXX functions +//----------------------------------------------------------------------------- + +// Generic linear color gradient, write to RGB fields, leave A untouched. +void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1) +{ + ImVec2 gradient_extent = gradient_p1 - gradient_p0; + float gradient_inv_length2 = 1.0f / ImLengthSqr(gradient_extent); + ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; + ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; + for (ImDrawVert* vert = vert_start; vert < vert_end; vert++) + { + float d = ImDot(vert->pos - gradient_p0, gradient_extent); + float t = ImClamp(d * gradient_inv_length2, 0.0f, 1.0f); + int r = ImLerp((int)(col0 >> IM_COL32_R_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_R_SHIFT) & 0xFF, t); + int g = ImLerp((int)(col0 >> IM_COL32_G_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_G_SHIFT) & 0xFF, t); + int b = ImLerp((int)(col0 >> IM_COL32_B_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_B_SHIFT) & 0xFF, t); + vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK); + } +} + +// Distribute UV over (a, b) rectangle +void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp) +{ + const ImVec2 size = b - a; + const ImVec2 uv_size = uv_b - uv_a; + const ImVec2 scale = ImVec2( + size.x != 0.0f ? (uv_size.x / size.x) : 0.0f, + size.y != 0.0f ? (uv_size.y / size.y) : 0.0f); + + ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; + ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; + if (clamp) + { + const ImVec2 min = ImMin(uv_a, uv_b); + const ImVec2 max = ImMax(uv_a, uv_b); + for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) + vertex->uv = ImClamp(uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale), min, max); + } + else + { + for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) + vertex->uv = uv_a + ImMul(ImVec2(vertex->pos.x, vertex->pos.y) - a, scale); + } +} + +//----------------------------------------------------------------------------- +// [SECTION] ImFontConfig +//----------------------------------------------------------------------------- + +ImFontConfig::ImFontConfig() +{ + FontData = NULL; + FontDataSize = 0; + FontDataOwnedByAtlas = true; + FontNo = 0; + SizePixels = 0.0f; + OversampleH = 3; // FIXME: 2 may be a better default? + OversampleV = 1; + PixelSnapH = false; + GlyphExtraSpacing = ImVec2(0.0f, 0.0f); + GlyphOffset = ImVec2(0.0f, 0.0f); + GlyphRanges = NULL; + GlyphMinAdvanceX = 0.0f; + GlyphMaxAdvanceX = FLT_MAX; + MergeMode = false; + RasterizerFlags = 0x00; + RasterizerMultiply = 1.0f; + EllipsisChar = (ImWchar)-1; + memset(Name, 0, sizeof(Name)); + DstFont = NULL; +} + +//----------------------------------------------------------------------------- +// [SECTION] ImFontAtlas +//----------------------------------------------------------------------------- + +// A work of art lies ahead! (. = white layer, X = black layer, others are blank) +// The white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. +const int FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF = 108; +const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; +const unsigned int FONT_ATLAS_DEFAULT_TEX_DATA_ID = 0x80000000; +static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = +{ + "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX " + "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X " + "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X " + "X - X.X - X.....X - X.....X -X...X - X...X- X..X " + "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X " + "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX " + "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX " + "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX " + "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X " + "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X" + "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X" + "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X" + "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X" + "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X" + "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X" + "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X" + "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X " + "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X " + "X.X X..X - -X.......X- X.......X - XX XX - - X..........X " + "XX X..X - - X.....X - X.....X - X.X X.X - - X........X " + " X..X - X...X - X...X - X..X X..X - - X........X " + " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX " + "------------ - X - X -X.....................X- ------------------" + " ----------------------------------- X...XXXXXXXXXXXXX...X - " + " - X..X X..X - " + " - X.X X.X - " + " - XX XX - " +}; + +static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] = +{ + // Pos ........ Size ......... Offset ...... + { ImVec2( 0,3), ImVec2(12,19), ImVec2( 0, 0) }, // ImGuiMouseCursor_Arrow + { ImVec2(13,0), ImVec2( 7,16), ImVec2( 1, 8) }, // ImGuiMouseCursor_TextInput + { ImVec2(31,0), ImVec2(23,23), ImVec2(11,11) }, // ImGuiMouseCursor_ResizeAll + { ImVec2(21,0), ImVec2( 9,23), ImVec2( 4,11) }, // ImGuiMouseCursor_ResizeNS + { ImVec2(55,18),ImVec2(23, 9), ImVec2(11, 4) }, // ImGuiMouseCursor_ResizeEW + { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW + { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE + { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand +}; + +ImFontAtlas::ImFontAtlas() +{ + Locked = false; + Flags = ImFontAtlasFlags_None; + TexID = (ImTextureID)NULL; + TexDesiredWidth = 0; + TexGlyphPadding = 1; + + TexPixelsAlpha8 = NULL; + TexPixelsRGBA32 = NULL; + TexWidth = TexHeight = 0; + TexUvScale = ImVec2(0.0f, 0.0f); + TexUvWhitePixel = ImVec2(0.0f, 0.0f); + for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) + CustomRectIds[n] = -1; +} + +ImFontAtlas::~ImFontAtlas() +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + Clear(); +} + +void ImFontAtlas::ClearInputData() +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + for (int i = 0; i < ConfigData.Size; i++) + if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) + { + IM_FREE(ConfigData[i].FontData); + ConfigData[i].FontData = NULL; + } + + // When clearing this we lose access to the font name and other information used to build the font. + for (int i = 0; i < Fonts.Size; i++) + if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size) + { + Fonts[i]->ConfigData = NULL; + Fonts[i]->ConfigDataCount = 0; + } + ConfigData.clear(); + CustomRects.clear(); + for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) + CustomRectIds[n] = -1; +} + +void ImFontAtlas::ClearTexData() +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + if (TexPixelsAlpha8) + IM_FREE(TexPixelsAlpha8); + if (TexPixelsRGBA32) + IM_FREE(TexPixelsRGBA32); + TexPixelsAlpha8 = NULL; + TexPixelsRGBA32 = NULL; +} + +void ImFontAtlas::ClearFonts() +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + for (int i = 0; i < Fonts.Size; i++) + IM_DELETE(Fonts[i]); + Fonts.clear(); +} + +void ImFontAtlas::Clear() +{ + ClearInputData(); + ClearTexData(); + ClearFonts(); +} + +void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) +{ + // Build atlas on demand + if (TexPixelsAlpha8 == NULL) + { + if (ConfigData.empty()) + AddFontDefault(); + Build(); + } + + *out_pixels = TexPixelsAlpha8; + if (out_width) *out_width = TexWidth; + if (out_height) *out_height = TexHeight; + if (out_bytes_per_pixel) *out_bytes_per_pixel = 1; +} + +void ImFontAtlas::GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel) +{ + // Convert to RGBA32 format on demand + // Although it is likely to be the most commonly used format, our font rendering is 1 channel / 8 bpp + if (!TexPixelsRGBA32) + { + unsigned char* pixels = NULL; + GetTexDataAsAlpha8(&pixels, NULL, NULL); + if (pixels) + { + TexPixelsRGBA32 = (unsigned int*)IM_ALLOC((size_t)TexWidth * (size_t)TexHeight * 4); + const unsigned char* src = pixels; + unsigned int* dst = TexPixelsRGBA32; + for (int n = TexWidth * TexHeight; n > 0; n--) + *dst++ = IM_COL32(255, 255, 255, (unsigned int)(*src++)); + } + } + + *out_pixels = (unsigned char*)TexPixelsRGBA32; + if (out_width) *out_width = TexWidth; + if (out_height) *out_height = TexHeight; + if (out_bytes_per_pixel) *out_bytes_per_pixel = 4; +} + +ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); + IM_ASSERT(font_cfg->SizePixels > 0.0f); + + // Create new font + if (!font_cfg->MergeMode) + Fonts.push_back(IM_NEW(ImFont)); + else + IM_ASSERT(!Fonts.empty() && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font. + + ConfigData.push_back(*font_cfg); + ImFontConfig& new_font_cfg = ConfigData.back(); + if (new_font_cfg.DstFont == NULL) + new_font_cfg.DstFont = Fonts.back(); + if (!new_font_cfg.FontDataOwnedByAtlas) + { + new_font_cfg.FontData = IM_ALLOC(new_font_cfg.FontDataSize); + new_font_cfg.FontDataOwnedByAtlas = true; + memcpy(new_font_cfg.FontData, font_cfg->FontData, (size_t)new_font_cfg.FontDataSize); + } + + if (new_font_cfg.DstFont->EllipsisChar == (ImWchar)-1) + new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar; + + // Invalidate texture + ClearTexData(); + return new_font_cfg.DstFont; +} + +// Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder) +static unsigned int stb_decompress_length(const unsigned char *input); +static unsigned int stb_decompress(unsigned char *output, const unsigned char *input, unsigned int length); +static const char* GetDefaultCompressedFontDataTTFBase85(); +static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; } +static void Decode85(const unsigned char* src, unsigned char* dst) +{ + while (*src) + { + unsigned int tmp = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4])))); + dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF); // We can't assume little-endianness. + src += 5; + dst += 4; + } +} + +// Load embedded ProggyClean.ttf at size 13, disable oversampling +ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) +{ + ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); + if (!font_cfg_template) + { + font_cfg.OversampleH = font_cfg.OversampleV = 1; + font_cfg.PixelSnapH = true; + } + if (font_cfg.SizePixels <= 0.0f) + font_cfg.SizePixels = 13.0f * 1.0f; + if (font_cfg.Name[0] == '\0') + ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels); + font_cfg.EllipsisChar = (ImWchar)0x0085; + + const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85(); + const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); + ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_cfg.SizePixels, &font_cfg, glyph_ranges); + font->DisplayOffset.y = 1.0f; + return font; +} + +ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + size_t data_size = 0; + void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0); + if (!data) + { + IM_ASSERT(0); // Could not load file. + return NULL; + } + ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); + if (font_cfg.Name[0] == '\0') + { + // Store a short copy of filename into into the font name for convenience + const char* p; + for (p = filename + strlen(filename); p > filename && p[-1] != '/' && p[-1] != '\\'; p--) {} + ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "%s, %.0fpx", p, size_pixels); + } + return AddFontFromMemoryTTF(data, (int)data_size, size_pixels, &font_cfg, glyph_ranges); +} + +// NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). +ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); + IM_ASSERT(font_cfg.FontData == NULL); + font_cfg.FontData = ttf_data; + font_cfg.FontDataSize = ttf_size; + font_cfg.SizePixels = size_pixels; + if (glyph_ranges) + font_cfg.GlyphRanges = glyph_ranges; + return AddFont(&font_cfg); +} + +ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) +{ + const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); + unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size); + stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); + + ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); + IM_ASSERT(font_cfg.FontData == NULL); + font_cfg.FontDataOwnedByAtlas = true; + return AddFontFromMemoryTTF(buf_decompressed_data, (int)buf_decompressed_size, size_pixels, &font_cfg, glyph_ranges); +} + +ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed_ttf_data_base85, float size_pixels, const ImFontConfig* font_cfg, const ImWchar* glyph_ranges) +{ + int compressed_ttf_size = (((int)strlen(compressed_ttf_data_base85) + 4) / 5) * 4; + void* compressed_ttf = IM_ALLOC((size_t)compressed_ttf_size); + Decode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf); + ImFont* font = AddFontFromMemoryCompressedTTF(compressed_ttf, compressed_ttf_size, size_pixels, font_cfg, glyph_ranges); + IM_FREE(compressed_ttf); + return font; +} + +int ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height) +{ + IM_ASSERT(id >= 0x10000); + IM_ASSERT(width > 0 && width <= 0xFFFF); + IM_ASSERT(height > 0 && height <= 0xFFFF); + ImFontAtlasCustomRect r; + r.ID = id; + r.Width = (unsigned short)width; + r.Height = (unsigned short)height; + CustomRects.push_back(r); + return CustomRects.Size - 1; // Return index +} + +int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset) +{ + IM_ASSERT(font != NULL); + IM_ASSERT(width > 0 && width <= 0xFFFF); + IM_ASSERT(height > 0 && height <= 0xFFFF); + ImFontAtlasCustomRect r; + r.ID = id; + r.Width = (unsigned short)width; + r.Height = (unsigned short)height; + r.GlyphAdvanceX = advance_x; + r.GlyphOffset = offset; + r.Font = font; + CustomRects.push_back(r); + return CustomRects.Size - 1; // Return index +} + +void ImFontAtlas::CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) +{ + IM_ASSERT(TexWidth > 0 && TexHeight > 0); // Font atlas needs to be built before we can calculate UV coordinates + IM_ASSERT(rect->IsPacked()); // Make sure the rectangle has been packed + *out_uv_min = ImVec2((float)rect->X * TexUvScale.x, (float)rect->Y * TexUvScale.y); + *out_uv_max = ImVec2((float)(rect->X + rect->Width) * TexUvScale.x, (float)(rect->Y + rect->Height) * TexUvScale.y); +} + +bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]) +{ + if (cursor_type <= ImGuiMouseCursor_None || cursor_type >= ImGuiMouseCursor_COUNT) + return false; + if (Flags & ImFontAtlasFlags_NoMouseCursors) + return false; + + IM_ASSERT(CustomRectIds[0] != -1); + ImFontAtlasCustomRect& r = CustomRects[CustomRectIds[0]]; + IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); + ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y); + ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1]; + *out_size = size; + *out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2]; + out_uv_border[0] = (pos) * TexUvScale; + out_uv_border[1] = (pos + size) * TexUvScale; + pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; + out_uv_fill[0] = (pos) * TexUvScale; + out_uv_fill[1] = (pos + size) * TexUvScale; + return true; +} + +bool ImFontAtlas::Build() +{ + IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + return ImFontAtlasBuildWithStbTruetype(this); +} + +void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor) +{ + for (unsigned int i = 0; i < 256; i++) + { + unsigned int value = (unsigned int)(i * in_brighten_factor); + out_table[i] = value > 255 ? 255 : (value & 0xFF); + } +} + +void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride) +{ + unsigned char* data = pixels + x + y * stride; + for (int j = h; j > 0; j--, data += stride) + for (int i = 0; i < w; i++) + data[i] = table[data[i]]; +} + +// Temporary data for one source font (multiple source fonts can be merged into one destination ImFont) +// (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.) +struct ImFontBuildSrcData +{ + stbtt_fontinfo FontInfo; + stbtt_pack_range PackRange; // Hold the list of codepoints to pack (essentially points to Codepoints.Data) + stbrp_rect* Rects; // Rectangle to pack. We first fill in their size and the packer will give us their position. + stbtt_packedchar* PackedChars; // Output glyphs + const ImWchar* SrcRanges; // Ranges as requested by user (user is allowed to request too much, e.g. 0x0020..0xFFFF) + int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[] + int GlyphsHighest; // Highest requested codepoint + int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font) + ImBoolVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB) + ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsMap) +}; + +// Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont) +struct ImFontBuildDstData +{ + int SrcCount; // Number of source fonts targeting this destination font. + int GlyphsHighest; + int GlyphsCount; + ImBoolVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font. +}; + +static void UnpackBoolVectorToFlatIndexList(const ImBoolVector* in, ImVector* out) +{ + IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int)); + const int* it_begin = in->Storage.begin(); + const int* it_end = in->Storage.end(); + for (const int* it = it_begin; it < it_end; it++) + if (int entries_32 = *it) + for (int bit_n = 0; bit_n < 32; bit_n++) + if (entries_32 & (1u << bit_n)) + out->push_back((int)((it - it_begin) << 5) + bit_n); +} + +bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) +{ + IM_ASSERT(atlas->ConfigData.Size > 0); + + ImFontAtlasBuildRegisterDefaultCustomRects(atlas); + + // Clear atlas + atlas->TexID = (ImTextureID)NULL; + atlas->TexWidth = atlas->TexHeight = 0; + atlas->TexUvScale = ImVec2(0.0f, 0.0f); + atlas->TexUvWhitePixel = ImVec2(0.0f, 0.0f); + atlas->ClearTexData(); + + // Temporary storage for building + ImVector src_tmp_array; + ImVector dst_tmp_array; + src_tmp_array.resize(atlas->ConfigData.Size); + dst_tmp_array.resize(atlas->Fonts.Size); + memset(src_tmp_array.Data, 0, (size_t)src_tmp_array.size_in_bytes()); + memset(dst_tmp_array.Data, 0, (size_t)dst_tmp_array.size_in_bytes()); + + // 1. Initialize font loading structure, check font data validity + for (int src_i = 0; src_i < atlas->ConfigData.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + ImFontConfig& cfg = atlas->ConfigData[src_i]; + IM_ASSERT(cfg.DstFont && (!cfg.DstFont->IsLoaded() || cfg.DstFont->ContainerAtlas == atlas)); + + // Find index from cfg.DstFont (we allow the user to set cfg.DstFont. Also it makes casual debugging nicer than when storing indices) + src_tmp.DstIndex = -1; + for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++) + if (cfg.DstFont == atlas->Fonts[output_i]) + src_tmp.DstIndex = output_i; + IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array? + if (src_tmp.DstIndex == -1) + return false; + + // Initialize helper structure for font loading and verify that the TTF/OTF data is correct + const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo); + IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."); + if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset)) + return false; + + // Measure highest codepoints + ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; + src_tmp.SrcRanges = cfg.GlyphRanges ? cfg.GlyphRanges : atlas->GetGlyphRangesDefault(); + for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) + src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]); + dst_tmp.SrcCount++; + dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest); + } + + // 2. For every requested codepoint, check for their presence in the font data, and handle redundancy or overlaps between source fonts to avoid unused glyphs. + int total_glyphs_count = 0; + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; + src_tmp.GlyphsSet.Resize(src_tmp.GlyphsHighest + 1); + if (dst_tmp.GlyphsSet.Storage.empty()) + dst_tmp.GlyphsSet.Resize(dst_tmp.GlyphsHighest + 1); + + for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) + for (int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++) + { + if (dst_tmp.GlyphsSet.GetBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true) + continue; + if (!stbtt_FindGlyphIndex(&src_tmp.FontInfo, codepoint)) // It is actually in the font? + continue; + + // Add to avail set/counters + src_tmp.GlyphsCount++; + dst_tmp.GlyphsCount++; + src_tmp.GlyphsSet.SetBit(codepoint, true); + dst_tmp.GlyphsSet.SetBit(codepoint, true); + total_glyphs_count++; + } + } + + // 3. Unpack our bit map into a flat list (we now have all the Unicode points that we know are requested _and_ available _and_ not overlapping another) + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount); + UnpackBoolVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList); + src_tmp.GlyphsSet.Clear(); + IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount); + } + for (int dst_i = 0; dst_i < dst_tmp_array.Size; dst_i++) + dst_tmp_array[dst_i].GlyphsSet.Clear(); + dst_tmp_array.clear(); + + // Allocate packing character data and flag packed characters buffer as non-packed (x0=y0=x1=y1=0) + // (We technically don't need to zero-clear buf_rects, but let's do it for the sake of sanity) + ImVector buf_rects; + ImVector buf_packedchars; + buf_rects.resize(total_glyphs_count); + buf_packedchars.resize(total_glyphs_count); + memset(buf_rects.Data, 0, (size_t)buf_rects.size_in_bytes()); + memset(buf_packedchars.Data, 0, (size_t)buf_packedchars.size_in_bytes()); + + // 4. Gather glyphs sizes so we can pack them in our virtual canvas. + int total_surface = 0; + int buf_rects_out_n = 0; + int buf_packedchars_out_n = 0; + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + if (src_tmp.GlyphsCount == 0) + continue; + + src_tmp.Rects = &buf_rects[buf_rects_out_n]; + src_tmp.PackedChars = &buf_packedchars[buf_packedchars_out_n]; + buf_rects_out_n += src_tmp.GlyphsCount; + buf_packedchars_out_n += src_tmp.GlyphsCount; + + // Convert our ranges in the format stb_truetype wants + ImFontConfig& cfg = atlas->ConfigData[src_i]; + src_tmp.PackRange.font_size = cfg.SizePixels; + src_tmp.PackRange.first_unicode_codepoint_in_range = 0; + src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data; + src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size; + src_tmp.PackRange.chardata_for_range = src_tmp.PackedChars; + src_tmp.PackRange.h_oversample = (unsigned char)cfg.OversampleH; + src_tmp.PackRange.v_oversample = (unsigned char)cfg.OversampleV; + + // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects) + const float scale = (cfg.SizePixels > 0) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -cfg.SizePixels); + const int padding = atlas->TexGlyphPadding; + for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++) + { + int x0, y0, x1, y1; + const int glyph_index_in_font = stbtt_FindGlyphIndex(&src_tmp.FontInfo, src_tmp.GlyphsList[glyph_i]); + IM_ASSERT(glyph_index_in_font != 0); + stbtt_GetGlyphBitmapBoxSubpixel(&src_tmp.FontInfo, glyph_index_in_font, scale * cfg.OversampleH, scale * cfg.OversampleV, 0, 0, &x0, &y0, &x1, &y1); + src_tmp.Rects[glyph_i].w = (stbrp_coord)(x1 - x0 + padding + cfg.OversampleH - 1); + src_tmp.Rects[glyph_i].h = (stbrp_coord)(y1 - y0 + padding + cfg.OversampleV - 1); + total_surface += src_tmp.Rects[glyph_i].w * src_tmp.Rects[glyph_i].h; + } + } + + // We need a width for the skyline algorithm, any width! + // The exact width doesn't really matter much, but some API/GPU have texture size limitations and increasing width can decrease height. + // User can override TexDesiredWidth and TexGlyphPadding if they wish, otherwise we use a simple heuristic to select the width based on expected surface. + const int surface_sqrt = (int)ImSqrt((float)total_surface) + 1; + atlas->TexHeight = 0; + if (atlas->TexDesiredWidth > 0) + atlas->TexWidth = atlas->TexDesiredWidth; + else + atlas->TexWidth = (surface_sqrt >= 4096*0.7f) ? 4096 : (surface_sqrt >= 2048*0.7f) ? 2048 : (surface_sqrt >= 1024*0.7f) ? 1024 : 512; + + // 5. Start packing + // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values). + const int TEX_HEIGHT_MAX = 1024 * 32; + stbtt_pack_context spc = {}; + stbtt_PackBegin(&spc, NULL, atlas->TexWidth, TEX_HEIGHT_MAX, 0, atlas->TexGlyphPadding, NULL); + ImFontAtlasBuildPackCustomRects(atlas, spc.pack_info); + + // 6. Pack each source font. No rendering yet, we are working with rectangles in an infinitely tall texture at this point. + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + if (src_tmp.GlyphsCount == 0) + continue; + + stbrp_pack_rects((stbrp_context*)spc.pack_info, src_tmp.Rects, src_tmp.GlyphsCount); + + // Extend texture height and mark missing glyphs as non-packed so we won't render them. + // FIXME: We are not handling packing failure here (would happen if we got off TEX_HEIGHT_MAX or if a single if larger than TexWidth?) + for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) + if (src_tmp.Rects[glyph_i].was_packed) + atlas->TexHeight = ImMax(atlas->TexHeight, src_tmp.Rects[glyph_i].y + src_tmp.Rects[glyph_i].h); + } + + // 7. Allocate texture + atlas->TexHeight = (atlas->Flags & ImFontAtlasFlags_NoPowerOfTwoHeight) ? (atlas->TexHeight + 1) : ImUpperPowerOfTwo(atlas->TexHeight); + atlas->TexUvScale = ImVec2(1.0f / atlas->TexWidth, 1.0f / atlas->TexHeight); + atlas->TexPixelsAlpha8 = (unsigned char*)IM_ALLOC(atlas->TexWidth * atlas->TexHeight); + memset(atlas->TexPixelsAlpha8, 0, atlas->TexWidth * atlas->TexHeight); + spc.pixels = atlas->TexPixelsAlpha8; + spc.height = atlas->TexHeight; + + // 8. Render/rasterize font characters into the texture + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontConfig& cfg = atlas->ConfigData[src_i]; + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + if (src_tmp.GlyphsCount == 0) + continue; + + stbtt_PackFontRangesRenderIntoRects(&spc, &src_tmp.FontInfo, &src_tmp.PackRange, 1, src_tmp.Rects); + + // Apply multiply operator + if (cfg.RasterizerMultiply != 1.0f) + { + unsigned char multiply_table[256]; + ImFontAtlasBuildMultiplyCalcLookupTable(multiply_table, cfg.RasterizerMultiply); + stbrp_rect* r = &src_tmp.Rects[0]; + for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++, r++) + if (r->was_packed) + ImFontAtlasBuildMultiplyRectAlpha8(multiply_table, atlas->TexPixelsAlpha8, r->x, r->y, r->w, r->h, atlas->TexWidth * 1); + } + src_tmp.Rects = NULL; + } + + // End packing + stbtt_PackEnd(&spc); + buf_rects.clear(); + + // 9. Setup ImFont and glyphs for runtime + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + { + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; + if (src_tmp.GlyphsCount == 0) + continue; + + ImFontConfig& cfg = atlas->ConfigData[src_i]; + ImFont* dst_font = cfg.DstFont; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true) + + const float font_scale = stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels); + int unscaled_ascent, unscaled_descent, unscaled_line_gap; + stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap); + + const float ascent = ImFloor(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1)); + const float descent = ImFloor(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); + ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent); + const float font_off_x = cfg.GlyphOffset.x; + const float font_off_y = cfg.GlyphOffset.y + (float)(int)(dst_font->Ascent + 0.5f); + + for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) + { + const int codepoint = src_tmp.GlyphsList[glyph_i]; + const stbtt_packedchar& pc = src_tmp.PackedChars[glyph_i]; + + const float char_advance_x_org = pc.xadvance; + const float char_advance_x_mod = ImClamp(char_advance_x_org, cfg.GlyphMinAdvanceX, cfg.GlyphMaxAdvanceX); + float char_off_x = font_off_x; + if (char_advance_x_org != char_advance_x_mod) + char_off_x += cfg.PixelSnapH ? (float)(int)((char_advance_x_mod - char_advance_x_org) * 0.5f) : (char_advance_x_mod - char_advance_x_org) * 0.5f; + + // Register glyph + stbtt_aligned_quad q; + float dummy_x = 0.0f, dummy_y = 0.0f; + stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &dummy_x, &dummy_y, &q, 0); + dst_font->AddGlyph((ImWchar)codepoint, q.x0 + char_off_x, q.y0 + font_off_y, q.x1 + char_off_x, q.y1 + font_off_y, q.s0, q.t0, q.s1, q.t1, char_advance_x_mod); + } + } + + // Cleanup temporary (ImVector doesn't honor destructor) + for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) + src_tmp_array[src_i].~ImFontBuildSrcData(); + + ImFontAtlasBuildFinish(atlas); + return true; +} + +void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas) +{ + if (atlas->CustomRectIds[0] >= 0) + return; + if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) + atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF*2+1, FONT_ATLAS_DEFAULT_TEX_DATA_H); + else + atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, 2, 2); +} + +void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) +{ + if (!font_config->MergeMode) + { + font->ClearOutputData(); + font->FontSize = font_config->SizePixels; + font->ConfigData = font_config; + font->ContainerAtlas = atlas; + font->Ascent = ascent; + font->Descent = descent; + } + font->ConfigDataCount++; +} + +void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque) +{ + stbrp_context* pack_context = (stbrp_context*)stbrp_context_opaque; + IM_ASSERT(pack_context != NULL); + + ImVector& user_rects = atlas->CustomRects; + IM_ASSERT(user_rects.Size >= 1); // We expect at least the default custom rects to be registered, else something went wrong. + + ImVector pack_rects; + pack_rects.resize(user_rects.Size); + memset(pack_rects.Data, 0, (size_t)pack_rects.size_in_bytes()); + for (int i = 0; i < user_rects.Size; i++) + { + pack_rects[i].w = user_rects[i].Width; + pack_rects[i].h = user_rects[i].Height; + } + stbrp_pack_rects(pack_context, &pack_rects[0], pack_rects.Size); + for (int i = 0; i < pack_rects.Size; i++) + if (pack_rects[i].was_packed) + { + user_rects[i].X = pack_rects[i].x; + user_rects[i].Y = pack_rects[i].y; + IM_ASSERT(pack_rects[i].w == user_rects[i].Width && pack_rects[i].h == user_rects[i].Height); + atlas->TexHeight = ImMax(atlas->TexHeight, pack_rects[i].y + pack_rects[i].h); + } +} + +static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) +{ + IM_ASSERT(atlas->CustomRectIds[0] >= 0); + IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); + ImFontAtlasCustomRect& r = atlas->CustomRects[atlas->CustomRectIds[0]]; + IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); + IM_ASSERT(r.IsPacked()); + + const int w = atlas->TexWidth; + if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) + { + // Render/copy pixels + IM_ASSERT(r.Width == FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * 2 + 1 && r.Height == FONT_ATLAS_DEFAULT_TEX_DATA_H); + for (int y = 0, n = 0; y < FONT_ATLAS_DEFAULT_TEX_DATA_H; y++) + for (int x = 0; x < FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF; x++, n++) + { + const int offset0 = (int)(r.X + x) + (int)(r.Y + y) * w; + const int offset1 = offset0 + FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; + atlas->TexPixelsAlpha8[offset0] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == '.' ? 0xFF : 0x00; + atlas->TexPixelsAlpha8[offset1] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == 'X' ? 0xFF : 0x00; + } + } + else + { + IM_ASSERT(r.Width == 2 && r.Height == 2); + const int offset = (int)(r.X) + (int)(r.Y) * w; + atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF; + } + atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * atlas->TexUvScale.x, (r.Y + 0.5f) * atlas->TexUvScale.y); +} + +void ImFontAtlasBuildFinish(ImFontAtlas* atlas) +{ + // Render into our custom data block + ImFontAtlasBuildRenderDefaultTexData(atlas); + + // Register custom rectangle glyphs + for (int i = 0; i < atlas->CustomRects.Size; i++) + { + const ImFontAtlasCustomRect& r = atlas->CustomRects[i]; + if (r.Font == NULL || r.ID > 0x10000) + continue; + + IM_ASSERT(r.Font->ContainerAtlas == atlas); + ImVec2 uv0, uv1; + atlas->CalcCustomRectUV(&r, &uv0, &uv1); + r.Font->AddGlyph((ImWchar)r.ID, r.GlyphOffset.x, r.GlyphOffset.y, r.GlyphOffset.x + r.Width, r.GlyphOffset.y + r.Height, uv0.x, uv0.y, uv1.x, uv1.y, r.GlyphAdvanceX); + } + + // Build all fonts lookup tables + for (int i = 0; i < atlas->Fonts.Size; i++) + if (atlas->Fonts[i]->DirtyLookupTables) + atlas->Fonts[i]->BuildLookupTable(); + + // Ellipsis character is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). + // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character. + // FIXME: Also note that 0x2026 is currently seldomly included in our font ranges. Because of this we are more likely to use three individual dots. + for (int i = 0; i < atlas->Fonts.size(); i++) + { + ImFont* font = atlas->Fonts[i]; + if (font->EllipsisChar != (ImWchar)-1) + continue; + const ImWchar ellipsis_variants[] = { (ImWchar)0x2026, (ImWchar)0x0085 }; + for (int j = 0; j < IM_ARRAYSIZE(ellipsis_variants); j++) + if (font->FindGlyphNoFallback(ellipsis_variants[j]) != NULL) // Verify glyph exists + { + font->EllipsisChar = ellipsis_variants[j]; + break; + } + } +} + +// Retrieve list of range (2 int per range, values are inclusive) +const ImWchar* ImFontAtlas::GetGlyphRangesDefault() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0, + }; + return &ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesKorean() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x3131, 0x3163, // Korean alphabets + 0xAC00, 0xD79D, // Korean characters + 0, + }; + return &ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesChineseFull() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x2000, 0x206F, // General Punctuation + 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana + 0x31F0, 0x31FF, // Katakana Phonetic Extensions + 0xFF00, 0xFFEF, // Half-width characters + 0x4e00, 0x9FAF, // CJK Ideograms + 0, + }; + return &ranges[0]; +} + +static void UnpackAccumulativeOffsetsIntoRanges(int base_codepoint, const short* accumulative_offsets, int accumulative_offsets_count, ImWchar* out_ranges) +{ + for (int n = 0; n < accumulative_offsets_count; n++, out_ranges += 2) + { + out_ranges[0] = out_ranges[1] = (ImWchar)(base_codepoint + accumulative_offsets[n]); + base_codepoint += accumulative_offsets[n]; + } + out_ranges[0] = 0; +} + +//------------------------------------------------------------------------- +// [SECTION] ImFontAtlas glyph ranges helpers +//------------------------------------------------------------------------- + +const ImWchar* ImFontAtlas::GetGlyphRangesChineseSimplifiedCommon() +{ + // Store 2500 regularly used characters for Simplified Chinese. + // Sourced from https://zh.wiktionary.org/wiki/%E9%99%84%E5%BD%95:%E7%8E%B0%E4%BB%A3%E6%B1%89%E8%AF%AD%E5%B8%B8%E7%94%A8%E5%AD%97%E8%A1%A8 + // This table covers 97.97% of all characters used during the month in July, 1987. + // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. + // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.) + static const short accumulative_offsets_from_0x4E00[] = + { + 0,1,2,4,1,1,1,1,2,1,3,2,1,2,2,1,1,1,1,1,5,2,1,2,3,3,3,2,2,4,1,1,1,2,1,5,2,3,1,2,1,2,1,1,2,1,1,2,2,1,4,1,1,1,1,5,10,1,2,19,2,1,2,1,2,1,2,1,2, + 1,5,1,6,3,2,1,2,2,1,1,1,4,8,5,1,1,4,1,1,3,1,2,1,5,1,2,1,1,1,10,1,1,5,2,4,6,1,4,2,2,2,12,2,1,1,6,1,1,1,4,1,1,4,6,5,1,4,2,2,4,10,7,1,1,4,2,4, + 2,1,4,3,6,10,12,5,7,2,14,2,9,1,1,6,7,10,4,7,13,1,5,4,8,4,1,1,2,28,5,6,1,1,5,2,5,20,2,2,9,8,11,2,9,17,1,8,6,8,27,4,6,9,20,11,27,6,68,2,2,1,1, + 1,2,1,2,2,7,6,11,3,3,1,1,3,1,2,1,1,1,1,1,3,1,1,8,3,4,1,5,7,2,1,4,4,8,4,2,1,2,1,1,4,5,6,3,6,2,12,3,1,3,9,2,4,3,4,1,5,3,3,1,3,7,1,5,1,1,1,1,2, + 3,4,5,2,3,2,6,1,1,2,1,7,1,7,3,4,5,15,2,2,1,5,3,22,19,2,1,1,1,1,2,5,1,1,1,6,1,1,12,8,2,9,18,22,4,1,1,5,1,16,1,2,7,10,15,1,1,6,2,4,1,2,4,1,6, + 1,1,3,2,4,1,6,4,5,1,2,1,1,2,1,10,3,1,3,2,1,9,3,2,5,7,2,19,4,3,6,1,1,1,1,1,4,3,2,1,1,1,2,5,3,1,1,1,2,2,1,1,2,1,1,2,1,3,1,1,1,3,7,1,4,1,1,2,1, + 1,2,1,2,4,4,3,8,1,1,1,2,1,3,5,1,3,1,3,4,6,2,2,14,4,6,6,11,9,1,15,3,1,28,5,2,5,5,3,1,3,4,5,4,6,14,3,2,3,5,21,2,7,20,10,1,2,19,2,4,28,28,2,3, + 2,1,14,4,1,26,28,42,12,40,3,52,79,5,14,17,3,2,2,11,3,4,6,3,1,8,2,23,4,5,8,10,4,2,7,3,5,1,1,6,3,1,2,2,2,5,28,1,1,7,7,20,5,3,29,3,17,26,1,8,4, + 27,3,6,11,23,5,3,4,6,13,24,16,6,5,10,25,35,7,3,2,3,3,14,3,6,2,6,1,4,2,3,8,2,1,1,3,3,3,4,1,1,13,2,2,4,5,2,1,14,14,1,2,2,1,4,5,2,3,1,14,3,12, + 3,17,2,16,5,1,2,1,8,9,3,19,4,2,2,4,17,25,21,20,28,75,1,10,29,103,4,1,2,1,1,4,2,4,1,2,3,24,2,2,2,1,1,2,1,3,8,1,1,1,2,1,1,3,1,1,1,6,1,5,3,1,1, + 1,3,4,1,1,5,2,1,5,6,13,9,16,1,1,1,1,3,2,3,2,4,5,2,5,2,2,3,7,13,7,2,2,1,1,1,1,2,3,3,2,1,6,4,9,2,1,14,2,14,2,1,18,3,4,14,4,11,41,15,23,15,23, + 176,1,3,4,1,1,1,1,5,3,1,2,3,7,3,1,1,2,1,2,4,4,6,2,4,1,9,7,1,10,5,8,16,29,1,1,2,2,3,1,3,5,2,4,5,4,1,1,2,2,3,3,7,1,6,10,1,17,1,44,4,6,2,1,1,6, + 5,4,2,10,1,6,9,2,8,1,24,1,2,13,7,8,8,2,1,4,1,3,1,3,3,5,2,5,10,9,4,9,12,2,1,6,1,10,1,1,7,7,4,10,8,3,1,13,4,3,1,6,1,3,5,2,1,2,17,16,5,2,16,6, + 1,4,2,1,3,3,6,8,5,11,11,1,3,3,2,4,6,10,9,5,7,4,7,4,7,1,1,4,2,1,3,6,8,7,1,6,11,5,5,3,24,9,4,2,7,13,5,1,8,82,16,61,1,1,1,4,2,2,16,10,3,8,1,1, + 6,4,2,1,3,1,1,1,4,3,8,4,2,2,1,1,1,1,1,6,3,5,1,1,4,6,9,2,1,1,1,2,1,7,2,1,6,1,5,4,4,3,1,8,1,3,3,1,3,2,2,2,2,3,1,6,1,2,1,2,1,3,7,1,8,2,1,2,1,5, + 2,5,3,5,10,1,2,1,1,3,2,5,11,3,9,3,5,1,1,5,9,1,2,1,5,7,9,9,8,1,3,3,3,6,8,2,3,2,1,1,32,6,1,2,15,9,3,7,13,1,3,10,13,2,14,1,13,10,2,1,3,10,4,15, + 2,15,15,10,1,3,9,6,9,32,25,26,47,7,3,2,3,1,6,3,4,3,2,8,5,4,1,9,4,2,2,19,10,6,2,3,8,1,2,2,4,2,1,9,4,4,4,6,4,8,9,2,3,1,1,1,1,3,5,5,1,3,8,4,6, + 2,1,4,12,1,5,3,7,13,2,5,8,1,6,1,2,5,14,6,1,5,2,4,8,15,5,1,23,6,62,2,10,1,1,8,1,2,2,10,4,2,2,9,2,1,1,3,2,3,1,5,3,3,2,1,3,8,1,1,1,11,3,1,1,4, + 3,7,1,14,1,2,3,12,5,2,5,1,6,7,5,7,14,11,1,3,1,8,9,12,2,1,11,8,4,4,2,6,10,9,13,1,1,3,1,5,1,3,2,4,4,1,18,2,3,14,11,4,29,4,2,7,1,3,13,9,2,2,5, + 3,5,20,7,16,8,5,72,34,6,4,22,12,12,28,45,36,9,7,39,9,191,1,1,1,4,11,8,4,9,2,3,22,1,1,1,1,4,17,1,7,7,1,11,31,10,2,4,8,2,3,2,1,4,2,16,4,32,2, + 3,19,13,4,9,1,5,2,14,8,1,1,3,6,19,6,5,1,16,6,2,10,8,5,1,2,3,1,5,5,1,11,6,6,1,3,3,2,6,3,8,1,1,4,10,7,5,7,7,5,8,9,2,1,3,4,1,1,3,1,3,3,2,6,16, + 1,4,6,3,1,10,6,1,3,15,2,9,2,10,25,13,9,16,6,2,2,10,11,4,3,9,1,2,6,6,5,4,30,40,1,10,7,12,14,33,6,3,6,7,3,1,3,1,11,14,4,9,5,12,11,49,18,51,31, + 140,31,2,2,1,5,1,8,1,10,1,4,4,3,24,1,10,1,3,6,6,16,3,4,5,2,1,4,2,57,10,6,22,2,22,3,7,22,6,10,11,36,18,16,33,36,2,5,5,1,1,1,4,10,1,4,13,2,7, + 5,2,9,3,4,1,7,43,3,7,3,9,14,7,9,1,11,1,1,3,7,4,18,13,1,14,1,3,6,10,73,2,2,30,6,1,11,18,19,13,22,3,46,42,37,89,7,3,16,34,2,2,3,9,1,7,1,1,1,2, + 2,4,10,7,3,10,3,9,5,28,9,2,6,13,7,3,1,3,10,2,7,2,11,3,6,21,54,85,2,1,4,2,2,1,39,3,21,2,2,5,1,1,1,4,1,1,3,4,15,1,3,2,4,4,2,3,8,2,20,1,8,7,13, + 4,1,26,6,2,9,34,4,21,52,10,4,4,1,5,12,2,11,1,7,2,30,12,44,2,30,1,1,3,6,16,9,17,39,82,2,2,24,7,1,7,3,16,9,14,44,2,1,2,1,2,3,5,2,4,1,6,7,5,3, + 2,6,1,11,5,11,2,1,18,19,8,1,3,24,29,2,1,3,5,2,2,1,13,6,5,1,46,11,3,5,1,1,5,8,2,10,6,12,6,3,7,11,2,4,16,13,2,5,1,1,2,2,5,2,28,5,2,23,10,8,4, + 4,22,39,95,38,8,14,9,5,1,13,5,4,3,13,12,11,1,9,1,27,37,2,5,4,4,63,211,95,2,2,2,1,3,5,2,1,1,2,2,1,1,1,3,2,4,1,2,1,1,5,2,2,1,1,2,3,1,3,1,1,1, + 3,1,4,2,1,3,6,1,1,3,7,15,5,3,2,5,3,9,11,4,2,22,1,6,3,8,7,1,4,28,4,16,3,3,25,4,4,27,27,1,4,1,2,2,7,1,3,5,2,28,8,2,14,1,8,6,16,25,3,3,3,14,3, + 3,1,1,2,1,4,6,3,8,4,1,1,1,2,3,6,10,6,2,3,18,3,2,5,5,4,3,1,5,2,5,4,23,7,6,12,6,4,17,11,9,5,1,1,10,5,12,1,1,11,26,33,7,3,6,1,17,7,1,5,12,1,11, + 2,4,1,8,14,17,23,1,2,1,7,8,16,11,9,6,5,2,6,4,16,2,8,14,1,11,8,9,1,1,1,9,25,4,11,19,7,2,15,2,12,8,52,7,5,19,2,16,4,36,8,1,16,8,24,26,4,6,2,9, + 5,4,36,3,28,12,25,15,37,27,17,12,59,38,5,32,127,1,2,9,17,14,4,1,2,1,1,8,11,50,4,14,2,19,16,4,17,5,4,5,26,12,45,2,23,45,104,30,12,8,3,10,2,2, + 3,3,1,4,20,7,2,9,6,15,2,20,1,3,16,4,11,15,6,134,2,5,59,1,2,2,2,1,9,17,3,26,137,10,211,59,1,2,4,1,4,1,1,1,2,6,2,3,1,1,2,3,2,3,1,3,4,4,2,3,3, + 1,4,3,1,7,2,2,3,1,2,1,3,3,3,2,2,3,2,1,3,14,6,1,3,2,9,6,15,27,9,34,145,1,1,2,1,1,1,1,2,1,1,1,1,2,2,2,3,1,2,1,1,1,2,3,5,8,3,5,2,4,1,3,2,2,2,12, + 4,1,1,1,10,4,5,1,20,4,16,1,15,9,5,12,2,9,2,5,4,2,26,19,7,1,26,4,30,12,15,42,1,6,8,172,1,1,4,2,1,1,11,2,2,4,2,1,2,1,10,8,1,2,1,4,5,1,2,5,1,8, + 4,1,3,4,2,1,6,2,1,3,4,1,2,1,1,1,1,12,5,7,2,4,3,1,1,1,3,3,6,1,2,2,3,3,3,2,1,2,12,14,11,6,6,4,12,2,8,1,7,10,1,35,7,4,13,15,4,3,23,21,28,52,5, + 26,5,6,1,7,10,2,7,53,3,2,1,1,1,2,163,532,1,10,11,1,3,3,4,8,2,8,6,2,2,23,22,4,2,2,4,2,1,3,1,3,3,5,9,8,2,1,2,8,1,10,2,12,21,20,15,105,2,3,1,1, + 3,2,3,1,1,2,5,1,4,15,11,19,1,1,1,1,5,4,5,1,1,2,5,3,5,12,1,2,5,1,11,1,1,15,9,1,4,5,3,26,8,2,1,3,1,1,15,19,2,12,1,2,5,2,7,2,19,2,20,6,26,7,5, + 2,2,7,34,21,13,70,2,128,1,1,2,1,1,2,1,1,3,2,2,2,15,1,4,1,3,4,42,10,6,1,49,85,8,1,2,1,1,4,4,2,3,6,1,5,7,4,3,211,4,1,2,1,2,5,1,2,4,2,2,6,5,6, + 10,3,4,48,100,6,2,16,296,5,27,387,2,2,3,7,16,8,5,38,15,39,21,9,10,3,7,59,13,27,21,47,5,21,6 + }; + static ImWchar base_ranges[] = // not zero-terminated + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x2000, 0x206F, // General Punctuation + 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana + 0x31F0, 0x31FF, // Katakana Phonetic Extensions + 0xFF00, 0xFFEF // Half-width characters + }; + static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00) * 2 + 1] = { 0 }; + if (!full_ranges[0]) + { + memcpy(full_ranges, base_ranges, sizeof(base_ranges)); + UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges)); + } + return &full_ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesJapanese() +{ + // 1946 common ideograms code points for Japanese + // Sourced from http://theinstructionlimit.com/common-kanji-character-ranges-for-xna-spritefont-rendering + // FIXME: Source a list of the revised 2136 Joyo Kanji list from 2010 and rebuild this. + // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. + // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.) + static const short accumulative_offsets_from_0x4E00[] = + { + 0,1,2,4,1,1,1,1,2,1,6,2,2,1,8,5,7,11,1,2,10,10,8,2,4,20,2,11,8,2,1,2,1,6,2,1,7,5,3,7,1,1,13,7,9,1,4,6,1,2,1,10,1,1,9,2,2,4,5,6,14,1,1,9,3,18, + 5,4,2,2,10,7,1,1,1,3,2,4,3,23,2,10,12,2,14,2,4,13,1,6,10,3,1,7,13,6,4,13,5,2,3,17,2,2,5,7,6,4,1,7,14,16,6,13,9,15,1,1,7,16,4,7,1,19,9,2,7,15, + 2,6,5,13,25,4,14,13,11,25,1,1,1,2,1,2,2,3,10,11,3,3,1,1,4,4,2,1,4,9,1,4,3,5,5,2,7,12,11,15,7,16,4,5,16,2,1,1,6,3,3,1,1,2,7,6,6,7,1,4,7,6,1,1, + 2,1,12,3,3,9,5,8,1,11,1,2,3,18,20,4,1,3,6,1,7,3,5,5,7,2,2,12,3,1,4,2,3,2,3,11,8,7,4,17,1,9,25,1,1,4,2,2,4,1,2,7,1,1,1,3,1,2,6,16,1,2,1,1,3,12, + 20,2,5,20,8,7,6,2,1,1,1,1,6,2,1,2,10,1,1,6,1,3,1,2,1,4,1,12,4,1,3,1,1,1,1,1,10,4,7,5,13,1,15,1,1,30,11,9,1,15,38,14,1,32,17,20,1,9,31,2,21,9, + 4,49,22,2,1,13,1,11,45,35,43,55,12,19,83,1,3,2,3,13,2,1,7,3,18,3,13,8,1,8,18,5,3,7,25,24,9,24,40,3,17,24,2,1,6,2,3,16,15,6,7,3,12,1,9,7,3,3, + 3,15,21,5,16,4,5,12,11,11,3,6,3,2,31,3,2,1,1,23,6,6,1,4,2,6,5,2,1,1,3,3,22,2,6,2,3,17,3,2,4,5,1,9,5,1,1,6,15,12,3,17,2,14,2,8,1,23,16,4,2,23, + 8,15,23,20,12,25,19,47,11,21,65,46,4,3,1,5,6,1,2,5,26,2,1,1,3,11,1,1,1,2,1,2,3,1,1,10,2,3,1,1,1,3,6,3,2,2,6,6,9,2,2,2,6,2,5,10,2,4,1,2,1,2,2, + 3,1,1,3,1,2,9,23,9,2,1,1,1,1,5,3,2,1,10,9,6,1,10,2,31,25,3,7,5,40,1,15,6,17,7,27,180,1,3,2,2,1,1,1,6,3,10,7,1,3,6,17,8,6,2,2,1,3,5,5,8,16,14, + 15,1,1,4,1,2,1,1,1,3,2,7,5,6,2,5,10,1,4,2,9,1,1,11,6,1,44,1,3,7,9,5,1,3,1,1,10,7,1,10,4,2,7,21,15,7,2,5,1,8,3,4,1,3,1,6,1,4,2,1,4,10,8,1,4,5, + 1,5,10,2,7,1,10,1,1,3,4,11,10,29,4,7,3,5,2,3,33,5,2,19,3,1,4,2,6,31,11,1,3,3,3,1,8,10,9,12,11,12,8,3,14,8,6,11,1,4,41,3,1,2,7,13,1,5,6,2,6,12, + 12,22,5,9,4,8,9,9,34,6,24,1,1,20,9,9,3,4,1,7,2,2,2,6,2,28,5,3,6,1,4,6,7,4,2,1,4,2,13,6,4,4,3,1,8,8,3,2,1,5,1,2,2,3,1,11,11,7,3,6,10,8,6,16,16, + 22,7,12,6,21,5,4,6,6,3,6,1,3,2,1,2,8,29,1,10,1,6,13,6,6,19,31,1,13,4,4,22,17,26,33,10,4,15,12,25,6,67,10,2,3,1,6,10,2,6,2,9,1,9,4,4,1,2,16,2, + 5,9,2,3,8,1,8,3,9,4,8,6,4,8,11,3,2,1,1,3,26,1,7,5,1,11,1,5,3,5,2,13,6,39,5,1,5,2,11,6,10,5,1,15,5,3,6,19,21,22,2,4,1,6,1,8,1,4,8,2,4,2,2,9,2, + 1,1,1,4,3,6,3,12,7,1,14,2,4,10,2,13,1,17,7,3,2,1,3,2,13,7,14,12,3,1,29,2,8,9,15,14,9,14,1,3,1,6,5,9,11,3,38,43,20,7,7,8,5,15,12,19,15,81,8,7, + 1,5,73,13,37,28,8,8,1,15,18,20,165,28,1,6,11,8,4,14,7,15,1,3,3,6,4,1,7,14,1,1,11,30,1,5,1,4,14,1,4,2,7,52,2,6,29,3,1,9,1,21,3,5,1,26,3,11,14, + 11,1,17,5,1,2,1,3,2,8,1,2,9,12,1,1,2,3,8,3,24,12,7,7,5,17,3,3,3,1,23,10,4,4,6,3,1,16,17,22,3,10,21,16,16,6,4,10,2,1,1,2,8,8,6,5,3,3,3,39,25, + 15,1,1,16,6,7,25,15,6,6,12,1,22,13,1,4,9,5,12,2,9,1,12,28,8,3,5,10,22,60,1,2,40,4,61,63,4,1,13,12,1,4,31,12,1,14,89,5,16,6,29,14,2,5,49,18,18, + 5,29,33,47,1,17,1,19,12,2,9,7,39,12,3,7,12,39,3,1,46,4,12,3,8,9,5,31,15,18,3,2,2,66,19,13,17,5,3,46,124,13,57,34,2,5,4,5,8,1,1,1,4,3,1,17,5, + 3,5,3,1,8,5,6,3,27,3,26,7,12,7,2,17,3,7,18,78,16,4,36,1,2,1,6,2,1,39,17,7,4,13,4,4,4,1,10,4,2,4,6,3,10,1,19,1,26,2,4,33,2,73,47,7,3,8,2,4,15, + 18,1,29,2,41,14,1,21,16,41,7,39,25,13,44,2,2,10,1,13,7,1,7,3,5,20,4,8,2,49,1,10,6,1,6,7,10,7,11,16,3,12,20,4,10,3,1,2,11,2,28,9,2,4,7,2,15,1, + 27,1,28,17,4,5,10,7,3,24,10,11,6,26,3,2,7,2,2,49,16,10,16,15,4,5,27,61,30,14,38,22,2,7,5,1,3,12,23,24,17,17,3,3,2,4,1,6,2,7,5,1,1,5,1,1,9,4, + 1,3,6,1,8,2,8,4,14,3,5,11,4,1,3,32,1,19,4,1,13,11,5,2,1,8,6,8,1,6,5,13,3,23,11,5,3,16,3,9,10,1,24,3,198,52,4,2,2,5,14,5,4,22,5,20,4,11,6,41, + 1,5,2,2,11,5,2,28,35,8,22,3,18,3,10,7,5,3,4,1,5,3,8,9,3,6,2,16,22,4,5,5,3,3,18,23,2,6,23,5,27,8,1,33,2,12,43,16,5,2,3,6,1,20,4,2,9,7,1,11,2, + 10,3,14,31,9,3,25,18,20,2,5,5,26,14,1,11,17,12,40,19,9,6,31,83,2,7,9,19,78,12,14,21,76,12,113,79,34,4,1,1,61,18,85,10,2,2,13,31,11,50,6,33,159, + 179,6,6,7,4,4,2,4,2,5,8,7,20,32,22,1,3,10,6,7,28,5,10,9,2,77,19,13,2,5,1,4,4,7,4,13,3,9,31,17,3,26,2,6,6,5,4,1,7,11,3,4,2,1,6,2,20,4,1,9,2,6, + 3,7,1,1,1,20,2,3,1,6,2,3,6,2,4,8,1,5,13,8,4,11,23,1,10,6,2,1,3,21,2,2,4,24,31,4,10,10,2,5,192,15,4,16,7,9,51,1,2,1,1,5,1,1,2,1,3,5,3,1,3,4,1, + 3,1,3,3,9,8,1,2,2,2,4,4,18,12,92,2,10,4,3,14,5,25,16,42,4,14,4,2,21,5,126,30,31,2,1,5,13,3,22,5,6,6,20,12,1,14,12,87,3,19,1,8,2,9,9,3,3,23,2, + 3,7,6,3,1,2,3,9,1,3,1,6,3,2,1,3,11,3,1,6,10,3,2,3,1,2,1,5,1,1,11,3,6,4,1,7,2,1,2,5,5,34,4,14,18,4,19,7,5,8,2,6,79,1,5,2,14,8,2,9,2,1,36,28,16, + 4,1,1,1,2,12,6,42,39,16,23,7,15,15,3,2,12,7,21,64,6,9,28,8,12,3,3,41,59,24,51,55,57,294,9,9,2,6,2,15,1,2,13,38,90,9,9,9,3,11,7,1,1,1,5,6,3,2, + 1,2,2,3,8,1,4,4,1,5,7,1,4,3,20,4,9,1,1,1,5,5,17,1,5,2,6,2,4,1,4,5,7,3,18,11,11,32,7,5,4,7,11,127,8,4,3,3,1,10,1,1,6,21,14,1,16,1,7,1,3,6,9,65, + 51,4,3,13,3,10,1,1,12,9,21,110,3,19,24,1,1,10,62,4,1,29,42,78,28,20,18,82,6,3,15,6,84,58,253,15,155,264,15,21,9,14,7,58,40,39, + }; + static ImWchar base_ranges[] = // not zero-terminated + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana + 0x31F0, 0x31FF, // Katakana Phonetic Extensions + 0xFF00, 0xFFEF // Half-width characters + }; + static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00)*2 + 1] = { 0 }; + if (!full_ranges[0]) + { + memcpy(full_ranges, base_ranges, sizeof(base_ranges)); + UnpackAccumulativeOffsetsIntoRanges(0x4E00, accumulative_offsets_from_0x4E00, IM_ARRAYSIZE(accumulative_offsets_from_0x4E00), full_ranges + IM_ARRAYSIZE(base_ranges)); + } + return &full_ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesCyrillic() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x0400, 0x052F, // Cyrillic + Cyrillic Supplement + 0x2DE0, 0x2DFF, // Cyrillic Extended-A + 0xA640, 0xA69F, // Cyrillic Extended-B + 0, + }; + return &ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesThai() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + 0x2010, 0x205E, // Punctuations + 0x0E00, 0x0E7F, // Thai + 0, + }; + return &ranges[0]; +} + +const ImWchar* ImFontAtlas::GetGlyphRangesVietnamese() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + 0x0102, 0x0103, + 0x0110, 0x0111, + 0x0128, 0x0129, + 0x0168, 0x0169, + 0x01A0, 0x01A1, + 0x01AF, 0x01B0, + 0x1EA0, 0x1EF9, + 0, + }; + return &ranges[0]; +} + +//----------------------------------------------------------------------------- +// [SECTION] ImFontGlyphRangesBuilder +//----------------------------------------------------------------------------- + +void ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end) +{ + while (text_end ? (text < text_end) : *text) + { + unsigned int c = 0; + int c_len = ImTextCharFromUtf8(&c, text, text_end); + text += c_len; + if (c_len == 0) + break; + if (c < 0x10000) + AddChar((ImWchar)c); + } +} + +void ImFontGlyphRangesBuilder::AddRanges(const ImWchar* ranges) +{ + for (; ranges[0]; ranges += 2) + for (ImWchar c = ranges[0]; c <= ranges[1]; c++) + AddChar(c); +} + +void ImFontGlyphRangesBuilder::BuildRanges(ImVector* out_ranges) +{ + int max_codepoint = 0x10000; + for (int n = 0; n < max_codepoint; n++) + if (GetBit(n)) + { + out_ranges->push_back((ImWchar)n); + while (n < max_codepoint - 1 && GetBit(n + 1)) + n++; + out_ranges->push_back((ImWchar)n); + } + out_ranges->push_back(0); +} + +//----------------------------------------------------------------------------- +// [SECTION] ImFont +//----------------------------------------------------------------------------- + +ImFont::ImFont() +{ + FontSize = 0.0f; + FallbackAdvanceX = 0.0f; + FallbackChar = (ImWchar)'?'; + EllipsisChar = (ImWchar)-1; + DisplayOffset = ImVec2(0.0f, 0.0f); + FallbackGlyph = NULL; + ContainerAtlas = NULL; + ConfigData = NULL; + ConfigDataCount = 0; + DirtyLookupTables = false; + Scale = 1.0f; + Ascent = Descent = 0.0f; + MetricsTotalSurface = 0; +} + +ImFont::~ImFont() +{ + ClearOutputData(); +} + +void ImFont::ClearOutputData() +{ + FontSize = 0.0f; + FallbackAdvanceX = 0.0f; + Glyphs.clear(); + IndexAdvanceX.clear(); + IndexLookup.clear(); + FallbackGlyph = NULL; + ContainerAtlas = NULL; + DirtyLookupTables = true; + Ascent = Descent = 0.0f; + MetricsTotalSurface = 0; +} + +void ImFont::BuildLookupTable() +{ + int max_codepoint = 0; + for (int i = 0; i != Glyphs.Size; i++) + max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint); + + IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved + IndexAdvanceX.clear(); + IndexLookup.clear(); + DirtyLookupTables = false; + GrowIndex(max_codepoint + 1); + for (int i = 0; i < Glyphs.Size; i++) + { + int codepoint = (int)Glyphs[i].Codepoint; + IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX; + IndexLookup[codepoint] = (ImWchar)i; + } + + // Create a glyph to handle TAB + // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?) + if (FindGlyph((ImWchar)' ')) + { + if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times + Glyphs.resize(Glyphs.Size + 1); + ImFontGlyph& tab_glyph = Glyphs.back(); + tab_glyph = *FindGlyph((ImWchar)' '); + tab_glyph.Codepoint = '\t'; + tab_glyph.AdvanceX *= IM_TABSIZE; + IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX; + IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size-1); + } + + FallbackGlyph = FindGlyphNoFallback(FallbackChar); + FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f; + for (int i = 0; i < max_codepoint + 1; i++) + if (IndexAdvanceX[i] < 0.0f) + IndexAdvanceX[i] = FallbackAdvanceX; +} + +void ImFont::SetFallbackChar(ImWchar c) +{ + FallbackChar = c; + BuildLookupTable(); +} + +void ImFont::GrowIndex(int new_size) +{ + IM_ASSERT(IndexAdvanceX.Size == IndexLookup.Size); + if (new_size <= IndexLookup.Size) + return; + IndexAdvanceX.resize(new_size, -1.0f); + IndexLookup.resize(new_size, (ImWchar)-1); +} + +// x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero. +// Not to be mistaken with texture coordinates, which are held by u0/v0/u1/v1 in normalized format (0.0..1.0 on each texture axis). +void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x) +{ + Glyphs.resize(Glyphs.Size + 1); + ImFontGlyph& glyph = Glyphs.back(); + glyph.Codepoint = (ImWchar)codepoint; + glyph.X0 = x0; + glyph.Y0 = y0; + glyph.X1 = x1; + glyph.Y1 = y1; + glyph.U0 = u0; + glyph.V0 = v0; + glyph.U1 = u1; + glyph.V1 = v1; + glyph.AdvanceX = advance_x + ConfigData->GlyphExtraSpacing.x; // Bake spacing into AdvanceX + + if (ConfigData->PixelSnapH) + glyph.AdvanceX = (float)(int)(glyph.AdvanceX + 0.5f); + + // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round) + DirtyLookupTables = true; + MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + 1.99f) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + 1.99f); +} + +void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst) +{ + IM_ASSERT(IndexLookup.Size > 0); // Currently this can only be called AFTER the font has been built, aka after calling ImFontAtlas::GetTexDataAs*() function. + int index_size = IndexLookup.Size; + + if (dst < index_size && IndexLookup.Data[dst] == (ImWchar)-1 && !overwrite_dst) // 'dst' already exists + return; + if (src >= index_size && dst >= index_size) // both 'dst' and 'src' don't exist -> no-op + return; + + GrowIndex(dst + 1); + IndexLookup[dst] = (src < index_size) ? IndexLookup.Data[src] : (ImWchar)-1; + IndexAdvanceX[dst] = (src < index_size) ? IndexAdvanceX.Data[src] : 1.0f; +} + +const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const +{ + if (c >= IndexLookup.Size) + return FallbackGlyph; + const ImWchar i = IndexLookup.Data[c]; + if (i == (ImWchar)-1) + return FallbackGlyph; + return &Glyphs.Data[i]; +} + +const ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const +{ + if (c >= IndexLookup.Size) + return NULL; + const ImWchar i = IndexLookup.Data[c]; + if (i == (ImWchar)-1) + return NULL; + return &Glyphs.Data[i]; +} + +const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const +{ + // Simple word-wrapping for English, not full-featured. Please submit failing cases! + // FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.) + + // For references, possible wrap point marked with ^ + // "aaa bbb, ccc,ddd. eee fff. ggg!" + // ^ ^ ^ ^ ^__ ^ ^ + + // List of hardcoded separators: .,;!?'" + + // Skip extra blanks after a line returns (that includes not counting them in width computation) + // e.g. "Hello world" --> "Hello" "World" + + // Cut words that cannot possibly fit within one line. + // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish" + + float line_width = 0.0f; + float word_width = 0.0f; + float blank_width = 0.0f; + wrap_width /= scale; // We work with unscaled widths to avoid scaling every characters + + const char* word_end = text; + const char* prev_word_end = NULL; + bool inside_word = true; + + const char* s = text; + while (s < text_end) + { + unsigned int c = (unsigned int)*s; + const char* next_s; + if (c < 0x80) + next_s = s + 1; + else + next_s = s + ImTextCharFromUtf8(&c, s, text_end); + if (c == 0) + break; + + if (c < 32) + { + if (c == '\n') + { + line_width = word_width = blank_width = 0.0f; + inside_word = true; + s = next_s; + continue; + } + if (c == '\r') + { + s = next_s; + continue; + } + } + + const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX); + if (ImCharIsBlankW(c)) + { + if (inside_word) + { + line_width += blank_width; + blank_width = 0.0f; + word_end = s; + } + blank_width += char_width; + inside_word = false; + } + else + { + word_width += char_width; + if (inside_word) + { + word_end = next_s; + } + else + { + prev_word_end = word_end; + line_width += word_width + blank_width; + word_width = blank_width = 0.0f; + } + + // Allow wrapping after punctuation. + inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\"'); + } + + // We ignore blank width at the end of the line (they can be skipped) + if (line_width + word_width > wrap_width) + { + // Words that cannot possibly fit within an entire line will be cut anywhere. + if (word_width < wrap_width) + s = prev_word_end ? prev_word_end : word_end; + break; + } + + s = next_s; + } + + return s; +} + +ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end, const char** remaining) const +{ + if (!text_end) + text_end = text_begin + strlen(text_begin); // FIXME-OPT: Need to avoid this. + + const float line_height = size; + const float scale = size / FontSize; + + ImVec2 text_size = ImVec2(0,0); + float line_width = 0.0f; + + const bool word_wrap_enabled = (wrap_width > 0.0f); + const char* word_wrap_eol = NULL; + + const char* s = text_begin; + while (s < text_end) + { + if (word_wrap_enabled) + { + // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature. + if (!word_wrap_eol) + { + word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width); + if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. + word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below + } + + if (s >= word_wrap_eol) + { + if (text_size.x < line_width) + text_size.x = line_width; + text_size.y += line_height; + line_width = 0.0f; + word_wrap_eol = NULL; + + // Wrapping skips upcoming blanks + while (s < text_end) + { + const char c = *s; + if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } + } + continue; + } + } + + // Decode and advance source + const char* prev_s = s; + unsigned int c = (unsigned int)*s; + if (c < 0x80) + { + s += 1; + } + else + { + s += ImTextCharFromUtf8(&c, s, text_end); + if (c == 0) // Malformed UTF-8? + break; + } + + if (c < 32) + { + if (c == '\n') + { + text_size.x = ImMax(text_size.x, line_width); + text_size.y += line_height; + line_width = 0.0f; + continue; + } + if (c == '\r') + continue; + } + + const float char_width = ((int)c < IndexAdvanceX.Size ? IndexAdvanceX.Data[c] : FallbackAdvanceX) * scale; + if (line_width + char_width >= max_width) + { + s = prev_s; + break; + } + + line_width += char_width; + } + + if (text_size.x < line_width) + text_size.x = line_width; + + if (line_width > 0 || text_size.y == 0.0f) + text_size.y += line_height; + + if (remaining) + *remaining = s; + + return text_size; +} + +void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const +{ + if (c == ' ' || c == '\t' || c == '\n' || c == '\r') // Match behavior of RenderText(), those 4 codepoints are hard-coded. + return; + if (const ImFontGlyph* glyph = FindGlyph(c)) + { + float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; + pos.x = (float)(int)pos.x + DisplayOffset.x; + pos.y = (float)(int)pos.y + DisplayOffset.y; + draw_list->PrimReserve(6, 4); + draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); + } +} + +void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const +{ + if (!text_end) + text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. + + // Align to be pixel perfect + pos.x = (float)(int)pos.x + DisplayOffset.x; + pos.y = (float)(int)pos.y + DisplayOffset.y; + float x = pos.x; + float y = pos.y; + if (y > clip_rect.w) + return; + + const float scale = size / FontSize; + const float line_height = FontSize * scale; + const bool word_wrap_enabled = (wrap_width > 0.0f); + const char* word_wrap_eol = NULL; + + // Fast-forward to first visible line + const char* s = text_begin; + if (y + line_height < clip_rect.y && !word_wrap_enabled) + while (y + line_height < clip_rect.y && s < text_end) + { + s = (const char*)memchr(s, '\n', text_end - s); + s = s ? s + 1 : text_end; + y += line_height; + } + + // For large text, scan for the last visible line in order to avoid over-reserving in the call to PrimReserve() + // Note that very large horizontal line will still be affected by the issue (e.g. a one megabyte string buffer without a newline will likely crash atm) + if (text_end - s > 10000 && !word_wrap_enabled) + { + const char* s_end = s; + float y_end = y; + while (y_end < clip_rect.w && s_end < text_end) + { + s_end = (const char*)memchr(s_end, '\n', text_end - s_end); + s_end = s_end ? s_end + 1 : text_end; + y_end += line_height; + } + text_end = s_end; + } + if (s == text_end) + return; + + // Reserve vertices for remaining worse case (over-reserving is useful and easily amortized) + const int vtx_count_max = (int)(text_end - s) * 4; + const int idx_count_max = (int)(text_end - s) * 6; + const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max; + draw_list->PrimReserve(idx_count_max, vtx_count_max); + + ImDrawVert* vtx_write = draw_list->_VtxWritePtr; + ImDrawIdx* idx_write = draw_list->_IdxWritePtr; + unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; + + while (s < text_end) + { + if (word_wrap_enabled) + { + // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature. + if (!word_wrap_eol) + { + word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x)); + if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. + word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below + } + + if (s >= word_wrap_eol) + { + x = pos.x; + y += line_height; + word_wrap_eol = NULL; + + // Wrapping skips upcoming blanks + while (s < text_end) + { + const char c = *s; + if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } + } + continue; + } + } + + // Decode and advance source + unsigned int c = (unsigned int)*s; + if (c < 0x80) + { + s += 1; + } + else + { + s += ImTextCharFromUtf8(&c, s, text_end); + if (c == 0) // Malformed UTF-8? + break; + } + + if (c < 32) + { + if (c == '\n') + { + x = pos.x; + y += line_height; + if (y > clip_rect.w) + break; // break out of main loop + continue; + } + if (c == '\r') + continue; + } + + float char_width = 0.0f; + if (const ImFontGlyph* glyph = FindGlyph((ImWchar)c)) + { + char_width = glyph->AdvanceX * scale; + + // Arbitrarily assume that both space and tabs are empty glyphs as an optimization + if (c != ' ' && c != '\t') + { + // We don't do a second finer clipping test on the Y axis as we've already skipped anything before clip_rect.y and exit once we pass clip_rect.w + float x1 = x + glyph->X0 * scale; + float x2 = x + glyph->X1 * scale; + float y1 = y + glyph->Y0 * scale; + float y2 = y + glyph->Y1 * scale; + if (x1 <= clip_rect.z && x2 >= clip_rect.x) + { + // Render a character + float u1 = glyph->U0; + float v1 = glyph->V0; + float u2 = glyph->U1; + float v2 = glyph->V1; + + // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads. + if (cpu_fine_clip) + { + if (x1 < clip_rect.x) + { + u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1); + x1 = clip_rect.x; + } + if (y1 < clip_rect.y) + { + v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1); + y1 = clip_rect.y; + } + if (x2 > clip_rect.z) + { + u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1); + x2 = clip_rect.z; + } + if (y2 > clip_rect.w) + { + v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1); + y2 = clip_rect.w; + } + if (y1 >= y2) + { + x += char_width; + continue; + } + } + + // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: + { + idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); + idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); + vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; + vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; + vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; + vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; + vtx_write += 4; + vtx_current_idx += 4; + idx_write += 6; + } + } + } + } + + x += char_width; + } + + // Give back unused vertices + draw_list->VtxBuffer.resize((int)(vtx_write - draw_list->VtxBuffer.Data)); + draw_list->IdxBuffer.resize((int)(idx_write - draw_list->IdxBuffer.Data)); + draw_list->CmdBuffer[draw_list->CmdBuffer.Size-1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); + draw_list->_VtxWritePtr = vtx_write; + draw_list->_IdxWritePtr = idx_write; + draw_list->_VtxCurrentIdx = vtx_current_idx; +} + +//----------------------------------------------------------------------------- +// [SECTION] Internal Render Helpers +// (progressively moved from imgui.cpp to here when they are redesigned to stop accessing ImGui global state) +//----------------------------------------------------------------------------- +// - RenderMouseCursor() +// - RenderArrowPointingAt() +// - RenderRectFilledRangeH() +//----------------------------------------------------------------------------- + +void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) +{ + if (mouse_cursor == ImGuiMouseCursor_None) + return; + IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); + + ImFontAtlas* font_atlas = draw_list->_Data->Font->ContainerAtlas; + ImVec2 offset, size, uv[4]; + if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) + { + pos -= offset; + const ImTextureID tex_id = font_atlas->TexID; + draw_list->PushTextureID(tex_id); + draw_list->AddImage(tex_id, pos + ImVec2(1,0)*scale, pos + ImVec2(1,0)*scale + size*scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos + ImVec2(2,0)*scale, pos + ImVec2(2,0)*scale + size*scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos, pos + size*scale, uv[2], uv[3], col_border); + draw_list->AddImage(tex_id, pos, pos + size*scale, uv[0], uv[1], col_fill); + draw_list->PopTextureID(); + } +} + +// Render an arrow. 'pos' is position of the arrow tip. half_sz.x is length from base to tip. half_sz.y is length on each side. +void ImGui::RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col) +{ + switch (direction) + { + case ImGuiDir_Left: draw_list->AddTriangleFilled(ImVec2(pos.x + half_sz.x, pos.y - half_sz.y), ImVec2(pos.x + half_sz.x, pos.y + half_sz.y), pos, col); return; + case ImGuiDir_Right: draw_list->AddTriangleFilled(ImVec2(pos.x - half_sz.x, pos.y + half_sz.y), ImVec2(pos.x - half_sz.x, pos.y - half_sz.y), pos, col); return; + case ImGuiDir_Up: draw_list->AddTriangleFilled(ImVec2(pos.x + half_sz.x, pos.y + half_sz.y), ImVec2(pos.x - half_sz.x, pos.y + half_sz.y), pos, col); return; + case ImGuiDir_Down: draw_list->AddTriangleFilled(ImVec2(pos.x - half_sz.x, pos.y - half_sz.y), ImVec2(pos.x + half_sz.x, pos.y - half_sz.y), pos, col); return; + case ImGuiDir_None: case ImGuiDir_COUNT: break; // Fix warnings + } +} + +static inline float ImAcos01(float x) +{ + if (x <= 0.0f) return IM_PI * 0.5f; + if (x >= 1.0f) return 0.0f; + return ImAcos(x); + //return (-0.69813170079773212f * x * x - 0.87266462599716477f) * x + 1.5707963267948966f; // Cheap approximation, may be enough for what we do. +} + +// FIXME: Cleanup and move code to ImDrawList. +void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding) +{ + if (x_end_norm == x_start_norm) + return; + if (x_start_norm > x_end_norm) + ImSwap(x_start_norm, x_end_norm); + + ImVec2 p0 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_start_norm), rect.Min.y); + ImVec2 p1 = ImVec2(ImLerp(rect.Min.x, rect.Max.x, x_end_norm), rect.Max.y); + if (rounding == 0.0f) + { + draw_list->AddRectFilled(p0, p1, col, 0.0f); + return; + } + + rounding = ImClamp(ImMin((rect.Max.x - rect.Min.x) * 0.5f, (rect.Max.y - rect.Min.y) * 0.5f) - 1.0f, 0.0f, rounding); + const float inv_rounding = 1.0f / rounding; + const float arc0_b = ImAcos01(1.0f - (p0.x - rect.Min.x) * inv_rounding); + const float arc0_e = ImAcos01(1.0f - (p1.x - rect.Min.x) * inv_rounding); + const float half_pi = IM_PI * 0.5f; // We will == compare to this because we know this is the exact value ImAcos01 can return. + const float x0 = ImMax(p0.x, rect.Min.x + rounding); + if (arc0_b == arc0_e) + { + draw_list->PathLineTo(ImVec2(x0, p1.y)); + draw_list->PathLineTo(ImVec2(x0, p0.y)); + } + else if (arc0_b == 0.0f && arc0_e == half_pi) + { + draw_list->PathArcToFast(ImVec2(x0, p1.y - rounding), rounding, 3, 6); // BL + draw_list->PathArcToFast(ImVec2(x0, p0.y + rounding), rounding, 6, 9); // TR + } + else + { + draw_list->PathArcTo(ImVec2(x0, p1.y - rounding), rounding, IM_PI - arc0_e, IM_PI - arc0_b, 3); // BL + draw_list->PathArcTo(ImVec2(x0, p0.y + rounding), rounding, IM_PI + arc0_b, IM_PI + arc0_e, 3); // TR + } + if (p1.x > rect.Min.x + rounding) + { + const float arc1_b = ImAcos01(1.0f - (rect.Max.x - p1.x) * inv_rounding); + const float arc1_e = ImAcos01(1.0f - (rect.Max.x - p0.x) * inv_rounding); + const float x1 = ImMin(p1.x, rect.Max.x - rounding); + if (arc1_b == arc1_e) + { + draw_list->PathLineTo(ImVec2(x1, p0.y)); + draw_list->PathLineTo(ImVec2(x1, p1.y)); + } + else if (arc1_b == 0.0f && arc1_e == half_pi) + { + draw_list->PathArcToFast(ImVec2(x1, p0.y + rounding), rounding, 9, 12); // TR + draw_list->PathArcToFast(ImVec2(x1, p1.y - rounding), rounding, 0, 3); // BR + } + else + { + draw_list->PathArcTo(ImVec2(x1, p0.y + rounding), rounding, -arc1_e, -arc1_b, 3); // TR + draw_list->PathArcTo(ImVec2(x1, p1.y - rounding), rounding, +arc1_b, +arc1_e, 3); // BR + } + } + draw_list->PathFillConvex(col); +} + +//----------------------------------------------------------------------------- +// [SECTION] Decompression code +//----------------------------------------------------------------------------- +// Compressed with stb_compress() then converted to a C array and encoded as base85. +// Use the program in misc/fonts/binary_to_compressed_c.cpp to create the array from a TTF file. +// The purpose of encoding as base85 instead of "0x00,0x01,..." style is only save on _source code_ size. +// Decompression from stb.h (public domain) by Sean Barrett https://github.com/nothings/stb/blob/master/stb.h +//----------------------------------------------------------------------------- + +static unsigned int stb_decompress_length(const unsigned char *input) +{ + return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]; +} + +static unsigned char *stb__barrier_out_e, *stb__barrier_out_b; +static const unsigned char *stb__barrier_in_b; +static unsigned char *stb__dout; +static void stb__match(const unsigned char *data, unsigned int length) +{ + // INVERSE of memmove... write each byte before copying the next... + IM_ASSERT(stb__dout + length <= stb__barrier_out_e); + if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; } + if (data < stb__barrier_out_b) { stb__dout = stb__barrier_out_e+1; return; } + while (length--) *stb__dout++ = *data++; +} + +static void stb__lit(const unsigned char *data, unsigned int length) +{ + IM_ASSERT(stb__dout + length <= stb__barrier_out_e); + if (stb__dout + length > stb__barrier_out_e) { stb__dout += length; return; } + if (data < stb__barrier_in_b) { stb__dout = stb__barrier_out_e+1; return; } + memcpy(stb__dout, data, length); + stb__dout += length; +} + +#define stb__in2(x) ((i[x] << 8) + i[(x)+1]) +#define stb__in3(x) ((i[x] << 16) + stb__in2((x)+1)) +#define stb__in4(x) ((i[x] << 24) + stb__in3((x)+1)) + +static const unsigned char *stb_decompress_token(const unsigned char *i) +{ + if (*i >= 0x20) { // use fewer if's for cases that expand small + if (*i >= 0x80) stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2; + else if (*i >= 0x40) stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3; + else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1); + } else { // more ifs for cases that expand large, since overhead is amortized + if (*i >= 0x18) stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4; + else if (*i >= 0x10) stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5; + else if (*i >= 0x08) stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1); + else if (*i == 0x07) stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1); + else if (*i == 0x06) stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5; + else if (*i == 0x04) stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6; + } + return i; +} + +static unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen) +{ + const unsigned long ADLER_MOD = 65521; + unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; + unsigned long blocklen, i; + + blocklen = buflen % 5552; + while (buflen) { + for (i=0; i + 7 < blocklen; i += 8) { + s1 += buffer[0], s2 += s1; + s1 += buffer[1], s2 += s1; + s1 += buffer[2], s2 += s1; + s1 += buffer[3], s2 += s1; + s1 += buffer[4], s2 += s1; + s1 += buffer[5], s2 += s1; + s1 += buffer[6], s2 += s1; + s1 += buffer[7], s2 += s1; + + buffer += 8; + } + + for (; i < blocklen; ++i) + s1 += *buffer++, s2 += s1; + + s1 %= ADLER_MOD, s2 %= ADLER_MOD; + buflen -= blocklen; + blocklen = 5552; + } + return (unsigned int)(s2 << 16) + (unsigned int)s1; +} + +static unsigned int stb_decompress(unsigned char *output, const unsigned char *i, unsigned int /*length*/) +{ + unsigned int olen; + if (stb__in4(0) != 0x57bC0000) return 0; + if (stb__in4(4) != 0) return 0; // error! stream is > 4GB + olen = stb_decompress_length(i); + stb__barrier_in_b = i; + stb__barrier_out_e = output + olen; + stb__barrier_out_b = output; + i += 16; + + stb__dout = output; + for (;;) { + const unsigned char *old_i = i; + i = stb_decompress_token(i); + if (i == old_i) { + if (*i == 0x05 && i[1] == 0xfa) { + IM_ASSERT(stb__dout == output + olen); + if (stb__dout != output + olen) return 0; + if (stb_adler32(1, output, olen) != (unsigned int) stb__in4(2)) + return 0; + return olen; + } else { + IM_ASSERT(0); /* NOTREACHED */ + return 0; + } + } + IM_ASSERT(stb__dout <= output + olen); + if (stb__dout > output + olen) + return 0; + } +} + +//----------------------------------------------------------------------------- +// [SECTION] Default font data (ProggyClean.ttf) +//----------------------------------------------------------------------------- +// ProggyClean.ttf +// Copyright (c) 2004, 2005 Tristan Grimmer +// MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip) +// Download and more information at http://upperbounds.net +//----------------------------------------------------------------------------- +// File: 'ProggyClean.ttf' (41208 bytes) +// Exported using misc/fonts/binary_to_compressed_c.cpp (with compression + base85 string encoding). +// The purpose of encoding as base85 instead of "0x00,0x01,..." style is only save on _source code_ size. +//----------------------------------------------------------------------------- +static const char proggy_clean_ttf_compressed_data_base85[11980+1] = + "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/" + "2*>]b(MC;$jPfY.;h^`IWM9Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1=Ke$$'5F%)]0^#0X@U.a$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;--VsM.M0rJfLH2eTM`*oJMHRC`N" + "kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`�j@'DbG&#^$PG.Ll+DNa&VZ>1i%h1S9u5o@YaaW$e+bROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc." + "x]Ip.PH^'/aqUO/$1WxLoW0[iLAw=4h(9.`G" + "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?Ggv:[7MI2k).'2($5FNP&EQ(,)" + "U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#" + "'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM" + "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu" + "Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/" + "/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[Ket`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO" + "j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%" + "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$MhLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]" + "%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et" + "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:" + "a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VBpqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<-+k?'(^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M" + "D?@f&1'BW-)Ju#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX(" + "P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs" + "bIu)'Z,*[>br5fX^:FPAWr-m2KgLQ_nN6'8uTGT5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q" + "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aege0jT6'N#(q%.O=?2S]u*(m<-" + "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i" + "sZ88+dKQ)W6>J%CL`.d*(B`-n8D9oK-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P r+$%CE=68>K8r0=dSC%%(@p7" + ".m7jilQ02'0-VWAgTlGW'b)Tq7VT9q^*^$$.:&N@@" + "$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*" + "hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u" + "@-W$U%VEQ/,,>>#)D#%8cY#YZ?=,`Wdxu/ae&#" + "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$so8lKN%5/$(vdfq7+ebA#" + "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoFDoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8" + "6e%B/:=>)N4xeW.*wft-;$'58-ESqr#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#" + "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjLV#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#SfD07&6D@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5" + "_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%" + "hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;" + "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmLq9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:" + "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3$U4O]GKx'm9)b@p7YsvK3w^YR-" + "CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*" + "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdFTi1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IXSsDiWP,##P`%/L-" + "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdFl*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj" + "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#$(>.Z-I&J(Q0Hd5Q%7Co-b`-cP)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8WlA2);Sa" + ">gXm8YB`1d@K#n]76-a$U,mF%Ul:#/'xoFM9QX-$.QN'>" + "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I" + "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-uW%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)" + "i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo" + "1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P" + "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*'IAO" + "URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#" + ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T" + "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4" + "A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#" + "/QHC#3^ZC#7jmC#;v)D#?,)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP" + "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp" + "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#"; + +static const char* GetDefaultCompressedFontDataTTFBase85() +{ + return proggy_clean_ttf_compressed_data_base85; +} diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp new file mode 100644 index 00000000..a9e72c6c --- /dev/null +++ b/imgui/imgui_impl_glfw.cpp @@ -0,0 +1,332 @@ +// dear imgui: Platform Binding for GLFW +// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) +// (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) +// (Requires: GLFW 3.1+) + +// Implemented features: +// [X] Platform: Clipboard support. +// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. +// [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. +// [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). + +// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. +// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. +// https://github.com/ocornut/imgui + +// CHANGELOG +// (minor and older changes stripped away, please see git history for details) +// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. +// 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter(). +// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. +// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. +// 2018-11-07: Inputs: When installing our GLFW callbacks, we save user's previously installed ones - if any - and chain call them. +// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. +// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. +// 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples. +// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. +// 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()). +// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. +// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. +// 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set. +// 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set). +// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. +// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. +// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). +// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. + +#include "imgui.h" +#include "imgui_impl_glfw.h" + +// GLFW +#include +#ifdef _WIN32 +#undef APIENTRY +#define GLFW_EXPOSE_NATIVE_WIN32 +#include // for glfwGetWin32Window +#endif +#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING +#define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED +#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity +#define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale +#define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface + +// Data +enum GlfwClientApi +{ + GlfwClientApi_Unknown, + GlfwClientApi_OpenGL, + GlfwClientApi_Vulkan +}; +static GLFWwindow* g_Window = NULL; // Main window +static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown; +static double g_Time = 0.0; +static bool g_MouseJustPressed[5] = { false, false, false, false, false }; +static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 }; + +// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. +static GLFWmousebuttonfun g_PrevUserCallbackMousebutton = NULL; +static GLFWscrollfun g_PrevUserCallbackScroll = NULL; +static GLFWkeyfun g_PrevUserCallbackKey = NULL; +static GLFWcharfun g_PrevUserCallbackChar = NULL; + +static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data) +{ + return glfwGetClipboardString((GLFWwindow*)user_data); +} + +static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) +{ + glfwSetClipboardString((GLFWwindow*)user_data, text); +} + +void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) +{ + if (g_PrevUserCallbackMousebutton != NULL) + g_PrevUserCallbackMousebutton(window, button, action, mods); + + if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed)) + g_MouseJustPressed[button] = true; +} + +void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) +{ + if (g_PrevUserCallbackScroll != NULL) + g_PrevUserCallbackScroll(window, xoffset, yoffset); + + ImGuiIO& io = ImGui::GetIO(); + io.MouseWheelH += (float)xoffset; + io.MouseWheel += (float)yoffset; +} + +void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + if (g_PrevUserCallbackKey != NULL) + g_PrevUserCallbackKey(window, key, scancode, action, mods); + + ImGuiIO& io = ImGui::GetIO(); + if (action == GLFW_PRESS) + io.KeysDown[key] = true; + if (action == GLFW_RELEASE) + io.KeysDown[key] = false; + + // Modifiers are not reliable across systems + io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL]; + io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT]; + io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT]; + io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER]; +} + +void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) +{ + if (g_PrevUserCallbackChar != NULL) + g_PrevUserCallbackChar(window, c); + + ImGuiIO& io = ImGui::GetIO(); + io.AddInputCharacter(c); +} + +static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) +{ + g_Window = window; + g_Time = 0.0; + + // Setup back-end capabilities flags + ImGuiIO& io = ImGui::GetIO(); + io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) + io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) + io.BackendPlatformName = "imgui_impl_glfw"; + + // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array. + io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; + io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT; + io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT; + io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; + io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; + io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP; + io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN; + io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; + io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; + io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT; + io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; + io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; + io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE; + io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; + io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; + io.KeyMap[ImGuiKey_KeyPadEnter] = GLFW_KEY_KP_ENTER; + io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; + io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; + io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; + io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; + io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; + io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; + + io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; + io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; + io.ClipboardUserData = g_Window; +#if defined(_WIN32) + io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window); +#endif + + g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. + g_MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); + + // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. + g_PrevUserCallbackMousebutton = NULL; + g_PrevUserCallbackScroll = NULL; + g_PrevUserCallbackKey = NULL; + g_PrevUserCallbackChar = NULL; + if (install_callbacks) + { + g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); + g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); + g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); + g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + } + + g_ClientApi = client_api; + return true; +} + +bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks) +{ + return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL); +} + +bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks) +{ + return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan); +} + +void ImGui_ImplGlfw_Shutdown() +{ + for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) + { + glfwDestroyCursor(g_MouseCursors[cursor_n]); + g_MouseCursors[cursor_n] = NULL; + } + g_ClientApi = GlfwClientApi_Unknown; +} + +static void ImGui_ImplGlfw_UpdateMousePosAndButtons() +{ + // Update buttons + ImGuiIO& io = ImGui::GetIO(); + for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) + { + // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. + io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0; + g_MouseJustPressed[i] = false; + } + + // Update mouse position + const ImVec2 mouse_pos_backup = io.MousePos; + io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); +#ifdef __EMSCRIPTEN__ + const bool focused = true; // Emscripten +#else + const bool focused = glfwGetWindowAttrib(g_Window, GLFW_FOCUSED) != 0; +#endif + if (focused) + { + if (io.WantSetMousePos) + { + glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y); + } + else + { + double mouse_x, mouse_y; + glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); + io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); + } + } +} + +static void ImGui_ImplGlfw_UpdateMouseCursor() +{ + ImGuiIO& io = ImGui::GetIO(); + if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) + return; + + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) + { + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + } + else + { + // Show OS mouse cursor + // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. + glfwSetCursor(g_Window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); + glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } +} + +static void ImGui_ImplGlfw_UpdateGamepads() +{ + ImGuiIO& io = ImGui::GetIO(); + memset(io.NavInputs, 0, sizeof(io.NavInputs)); + if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) + return; + + // Update gamepad inputs + #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; } + #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; } + int axes_count = 0, buttons_count = 0; + const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count); + const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count); + MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A + MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B + MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X + MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y + MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left + MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right + MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up + MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down + MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB + MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB + MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB + MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB + MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f); + MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f); + MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f); + MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f); + #undef MAP_BUTTON + #undef MAP_ANALOG + if (axes_count > 0 && buttons_count > 0) + io.BackendFlags |= ImGuiBackendFlags_HasGamepad; + else + io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; +} + +void ImGui_ImplGlfw_NewFrame() +{ + ImGuiIO& io = ImGui::GetIO(); + IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."); + + // Setup display size (every frame to accommodate for window resizing) + int w, h; + int display_w, display_h; + glfwGetWindowSize(g_Window, &w, &h); + glfwGetFramebufferSize(g_Window, &display_w, &display_h); + io.DisplaySize = ImVec2((float)w, (float)h); + if (w > 0 && h > 0) + io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); + + // Setup time step + double current_time = glfwGetTime(); + io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); + g_Time = current_time; + + ImGui_ImplGlfw_UpdateMousePosAndButtons(); + ImGui_ImplGlfw_UpdateMouseCursor(); + + // Update game controllers (if enabled and available) + ImGui_ImplGlfw_UpdateGamepads(); +} diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h new file mode 100644 index 00000000..ccbe840d --- /dev/null +++ b/imgui/imgui_impl_glfw.h @@ -0,0 +1,33 @@ +// dear imgui: Platform Binding for GLFW +// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) +// (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) + +// Implemented features: +// [X] Platform: Clipboard support. +// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. +// [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. +// [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). + +// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. +// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. +// https://github.com/ocornut/imgui + +// About GLSL version: +// The 'glsl_version' initialization parameter defaults to "#version 150" if NULL. +// Only override if your GL version doesn't handle this GLSL version. Keep NULL if unsure! + +#pragma once + +struct GLFWwindow; + +IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); +IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); +IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); + +// InitXXX function with 'install_callbacks=true': install GLFW callbacks. They will call user's previously installed callbacks, if any. +// InitXXX function with 'install_callbacks=false': do not install GLFW callbacks. You will need to call them yourself from your own GLFW callbacks. +IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); +IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); +IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); +IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp new file mode 100644 index 00000000..d7b108ef --- /dev/null +++ b/imgui/imgui_impl_opengl3.cpp @@ -0,0 +1,653 @@ +// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline +// - Desktop GL: 2.x 3.x 4.x +// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) +// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) + +// Implemented features: +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. +// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bits indices. + +// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. +// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. +// https://github.com/ocornut/imgui + +// CHANGELOG +// (minor and older changes stripped away, please see git history for details) +// 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility. +// 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call. +// 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. +// 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. +// 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop. +// 2019-03-15: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early. +// 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0). +// 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader. +// 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display. +// 2019-02-01: OpenGL: Using GLSL 410 shaders for any version over 410 (e.g. 430, 450). +// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. +// 2018-11-13: OpenGL: Support for GL 4.5's glClipControl(GL_UPPER_LEFT) / GL_CLIP_ORIGIN. +// 2018-08-29: OpenGL: Added support for more OpenGL loaders: glew and glad, with comments indicative that any loader can be used. +// 2018-08-09: OpenGL: Default to OpenGL ES 3 on iOS and Android. GLSL version default to "#version 300 ES". +// 2018-07-30: OpenGL: Support for GLSL 300 ES and 410 core. Fixes for Emscripten compilation. +// 2018-07-10: OpenGL: Support for more GLSL versions (based on the GLSL version string). Added error output when shaders fail to compile/link. +// 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples. +// 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. +// 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state. +// 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer. +// 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150". +// 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context. +// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself. +// 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150. +// 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode. +// 2017-05-01: OpenGL: Fixed save and restore of current blend func state. +// 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE. +// 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle. +// 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752) + +//---------------------------------------- +// OpenGL GLSL GLSL +// version version string +//---------------------------------------- +// 2.0 110 "#version 110" +// 2.1 120 "#version 120" +// 3.0 130 "#version 130" +// 3.1 140 "#version 140" +// 3.2 150 "#version 150" +// 3.3 330 "#version 330 core" +// 4.0 400 "#version 400 core" +// 4.1 410 "#version 410 core" +// 4.2 420 "#version 410 core" +// 4.3 430 "#version 430 core" +// ES 2.0 100 "#version 100" = WebGL 1.0 +// ES 3.0 300 "#version 300 es" = WebGL 2.0 +//---------------------------------------- + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#include "imgui_impl_opengl3.h" +#include +#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier +#include // intptr_t +#else +#include // intptr_t +#endif +#if defined(__APPLE__) +#include "TargetConditionals.h" +#endif + +// Auto-detect GL version +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) +#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) +#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" +#undef IMGUI_IMPL_OPENGL_LOADER_GL3W +#undef IMGUI_IMPL_OPENGL_LOADER_GLEW +#undef IMGUI_IMPL_OPENGL_LOADER_GLAD +#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#elif defined(__EMSCRIPTEN__) +#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" +#undef IMGUI_IMPL_OPENGL_LOADER_GL3W +#undef IMGUI_IMPL_OPENGL_LOADER_GLEW +#undef IMGUI_IMPL_OPENGL_LOADER_GLAD +#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#endif +#endif + +// GL includes +#if defined(IMGUI_IMPL_OPENGL_ES2) +#include +#elif defined(IMGUI_IMPL_OPENGL_ES3) +#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) +#include // Use GL ES 3 +#else +#include // Use GL ES 3 +#endif +#else +// About Desktop OpenGL function loaders: +// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. +// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). +// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. +#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) +#include // Needs to be initialized with gl3wInit() in user's code +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) +#include // Needs to be initialized with glewInit() in user's code +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) +#include // Needs to be initialized with gladLoadGL() in user's code +#else +#include IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#endif +#endif + +// Desktop GL has glDrawElementsBaseVertex() which GL ES and WebGL don't have. +#if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) +#define IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX 0 +#else +#define IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX 1 +#endif + +// OpenGL Data +static char g_GlslVersionString[32] = ""; +static GLuint g_FontTexture = 0; +static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; +static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location +static int g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location +static unsigned int g_VboHandle = 0, g_ElementsHandle = 0; + +// Functions +bool ImGui_ImplOpenGL3_Init(const char* glsl_version) +{ + // Setup back-end capabilities flags + ImGuiIO& io = ImGui::GetIO(); + io.BackendRendererName = "imgui_impl_opengl3"; +#if IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. +#endif + + // Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. +#if defined(IMGUI_IMPL_OPENGL_ES2) + if (glsl_version == NULL) + glsl_version = "#version 100"; +#elif defined(IMGUI_IMPL_OPENGL_ES3) + if (glsl_version == NULL) + glsl_version = "#version 300 es"; +#else + if (glsl_version == NULL) + glsl_version = "#version 130"; +#endif + IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString)); + strcpy(g_GlslVersionString, glsl_version); + strcat(g_GlslVersionString, "\n"); + + // Dummy construct to make it easily visible in the IDE and debugger which GL loader has been selected. + // The code actually never uses the 'gl_loader' variable! It is only here so you can read it! + // If auto-detection fails or doesn't select the same GL loader file as used by your application, + // you are likely to get a crash below. + // You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. + const char* gl_loader = "Unknown"; + IM_UNUSED(gl_loader); +#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) + gl_loader = "GL3W"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) + gl_loader = "GLEW"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) + gl_loader = "GLAD"; +#else // IMGUI_IMPL_OPENGL_LOADER_CUSTOM + gl_loader = "Custom"; +#endif + + // Make a dummy GL call (we don't actually need the result) + // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. + // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. + GLint current_texture; + glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); + + return true; +} + +void ImGui_ImplOpenGL3_Shutdown() +{ + ImGui_ImplOpenGL3_DestroyDeviceObjects(); +} + +void ImGui_ImplOpenGL3_NewFrame() +{ + if (!g_ShaderHandle) + ImGui_ImplOpenGL3_CreateDeviceObjects(); +} + +static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) +{ + // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill + glEnable(GL_BLEND); + glBlendEquation(GL_FUNC_ADD); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glEnable(GL_SCISSOR_TEST); +#ifdef GL_POLYGON_MODE + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#endif + + // Setup viewport, orthographic projection matrix + // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. + glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height); + float L = draw_data->DisplayPos.x; + float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; + float T = draw_data->DisplayPos.y; + float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; + const float ortho_projection[4][4] = + { + { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, + { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, + { 0.0f, 0.0f, -1.0f, 0.0f }, + { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f }, + }; + glUseProgram(g_ShaderHandle); + glUniform1i(g_AttribLocationTex, 0); + glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); +#ifdef GL_SAMPLER_BINDING + glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. +#endif + + (void)vertex_array_object; +#ifndef IMGUI_IMPL_OPENGL_ES2 + glBindVertexArray(vertex_array_object); +#endif + + // Bind vertex/index buffers and setup attributes for ImDrawVert + glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle); + glEnableVertexAttribArray(g_AttribLocationVtxPos); + glEnableVertexAttribArray(g_AttribLocationVtxUV); + glEnableVertexAttribArray(g_AttribLocationVtxColor); + glVertexAttribPointer(g_AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos)); + glVertexAttribPointer(g_AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv)); + glVertexAttribPointer(g_AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col)); +} + +// OpenGL3 Render function. +// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) +// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so. +void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) +{ + // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) + int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); + int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); + if (fb_width <= 0 || fb_height <= 0) + return; + + // Backup GL state + GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); + glActiveTexture(GL_TEXTURE0); + GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); + GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); +#ifdef GL_SAMPLER_BINDING + GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler); +#endif + GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); +#ifndef IMGUI_IMPL_OPENGL_ES2 + GLint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array_object); +#endif +#ifdef GL_POLYGON_MODE + GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); +#endif + GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); + GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); + GLenum last_blend_src_rgb; glGetIntegerv(GL_BLEND_SRC_RGB, (GLint*)&last_blend_src_rgb); + GLenum last_blend_dst_rgb; glGetIntegerv(GL_BLEND_DST_RGB, (GLint*)&last_blend_dst_rgb); + GLenum last_blend_src_alpha; glGetIntegerv(GL_BLEND_SRC_ALPHA, (GLint*)&last_blend_src_alpha); + GLenum last_blend_dst_alpha; glGetIntegerv(GL_BLEND_DST_ALPHA, (GLint*)&last_blend_dst_alpha); + GLenum last_blend_equation_rgb; glGetIntegerv(GL_BLEND_EQUATION_RGB, (GLint*)&last_blend_equation_rgb); + GLenum last_blend_equation_alpha; glGetIntegerv(GL_BLEND_EQUATION_ALPHA, (GLint*)&last_blend_equation_alpha); + GLboolean last_enable_blend = glIsEnabled(GL_BLEND); + GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); + GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); + GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); + bool clip_origin_lower_left = true; +#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) + GLenum last_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)&last_clip_origin); // Support for GL 4.5's glClipControl(GL_UPPER_LEFT) + if (last_clip_origin == GL_UPPER_LEFT) + clip_origin_lower_left = false; +#endif + + // Setup desired GL state + // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) + // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound. + GLuint vertex_array_object = 0; +#ifndef IMGUI_IMPL_OPENGL_ES2 + glGenVertexArrays(1, &vertex_array_object); +#endif + ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); + + // Will project scissor/clipping rectangles into framebuffer space + ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports + ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2) + + // Render command lists + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + + // Upload vertex/index buffers + glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW); + + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + if (pcmd->UserCallback != NULL) + { + // User callback, registered via ImDrawList::AddCallback() + // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) + if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) + ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); + else + pcmd->UserCallback(cmd_list, pcmd); + } + else + { + // Project scissor/clipping rectangles into framebuffer space + ImVec4 clip_rect; + clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x; + clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y; + clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x; + clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y; + + if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f) + { + // Apply scissor/clipping rectangle + if (clip_origin_lower_left) + glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); + else + glScissor((int)clip_rect.x, (int)clip_rect.y, (int)clip_rect.z, (int)clip_rect.w); // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) + + // Bind texture, Draw + glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); +#if IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX + glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); +#else + glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); +#endif + } + } + } + } + + // Destroy the temporary VAO +#ifndef IMGUI_IMPL_OPENGL_ES2 + glDeleteVertexArrays(1, &vertex_array_object); +#endif + + // Restore modified GL state + glUseProgram(last_program); + glBindTexture(GL_TEXTURE_2D, last_texture); +#ifdef GL_SAMPLER_BINDING + glBindSampler(0, last_sampler); +#endif + glActiveTexture(last_active_texture); +#ifndef IMGUI_IMPL_OPENGL_ES2 + glBindVertexArray(last_vertex_array_object); +#endif + glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); + glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha); + glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha); + if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); + if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); + if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); + if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); +#ifdef GL_POLYGON_MODE + glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); +#endif + glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); + glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); +} + +bool ImGui_ImplOpenGL3_CreateFontsTexture() +{ + // Build texture atlas + ImGuiIO& io = ImGui::GetIO(); + unsigned char* pixels; + int width, height; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + + // Upload texture to graphics system + GLint last_texture; + glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); + glGenTextures(1, &g_FontTexture); + glBindTexture(GL_TEXTURE_2D, g_FontTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +#ifdef GL_UNPACK_ROW_LENGTH + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); +#endif + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + // Store our identifier + io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontTexture; + + // Restore state + glBindTexture(GL_TEXTURE_2D, last_texture); + + return true; +} + +void ImGui_ImplOpenGL3_DestroyFontsTexture() +{ + if (g_FontTexture) + { + ImGuiIO& io = ImGui::GetIO(); + glDeleteTextures(1, &g_FontTexture); + io.Fonts->TexID = 0; + g_FontTexture = 0; + } +} + +// If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file. +static bool CheckShader(GLuint handle, const char* desc) +{ + GLint status = 0, log_length = 0; + glGetShaderiv(handle, GL_COMPILE_STATUS, &status); + glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); + if ((GLboolean)status == GL_FALSE) + fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc); + if (log_length > 1) + { + ImVector buf; + buf.resize((int)(log_length + 1)); + glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); + fprintf(stderr, "%s\n", buf.begin()); + } + return (GLboolean)status == GL_TRUE; +} + +// If you get an error please report on GitHub. You may try different GL context version or GLSL version. +static bool CheckProgram(GLuint handle, const char* desc) +{ + GLint status = 0, log_length = 0; + glGetProgramiv(handle, GL_LINK_STATUS, &status); + glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); + if ((GLboolean)status == GL_FALSE) + fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString); + if (log_length > 1) + { + ImVector buf; + buf.resize((int)(log_length + 1)); + glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); + fprintf(stderr, "%s\n", buf.begin()); + } + return (GLboolean)status == GL_TRUE; +} + +bool ImGui_ImplOpenGL3_CreateDeviceObjects() +{ + // Backup GL state + GLint last_texture, last_array_buffer; + glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); +#ifndef IMGUI_IMPL_OPENGL_ES2 + GLint last_vertex_array; + glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); +#endif + + // Parse GLSL version string + int glsl_version = 130; + sscanf(g_GlslVersionString, "#version %d", &glsl_version); + + const GLchar* vertex_shader_glsl_120 = + "uniform mat4 ProjMtx;\n" + "attribute vec2 Position;\n" + "attribute vec2 UV;\n" + "attribute vec4 Color;\n" + "varying vec2 Frag_UV;\n" + "varying vec4 Frag_Color;\n" + "void main()\n" + "{\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" + "}\n"; + + const GLchar* vertex_shader_glsl_130 = + "uniform mat4 ProjMtx;\n" + "in vec2 Position;\n" + "in vec2 UV;\n" + "in vec4 Color;\n" + "out vec2 Frag_UV;\n" + "out vec4 Frag_Color;\n" + "void main()\n" + "{\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" + "}\n"; + + const GLchar* vertex_shader_glsl_300_es = + "precision mediump float;\n" + "layout (location = 0) in vec2 Position;\n" + "layout (location = 1) in vec2 UV;\n" + "layout (location = 2) in vec4 Color;\n" + "uniform mat4 ProjMtx;\n" + "out vec2 Frag_UV;\n" + "out vec4 Frag_Color;\n" + "void main()\n" + "{\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" + "}\n"; + + const GLchar* vertex_shader_glsl_410_core = + "layout (location = 0) in vec2 Position;\n" + "layout (location = 1) in vec2 UV;\n" + "layout (location = 2) in vec4 Color;\n" + "uniform mat4 ProjMtx;\n" + "out vec2 Frag_UV;\n" + "out vec4 Frag_Color;\n" + "void main()\n" + "{\n" + " Frag_UV = UV;\n" + " Frag_Color = Color;\n" + " gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" + "}\n"; + + const GLchar* fragment_shader_glsl_120 = + "#ifdef GL_ES\n" + " precision mediump float;\n" + "#endif\n" + "uniform sampler2D Texture;\n" + "varying vec2 Frag_UV;\n" + "varying vec4 Frag_Color;\n" + "void main()\n" + "{\n" + " gl_FragColor = Frag_Color * texture2D(Texture, Frag_UV.st);\n" + "}\n"; + + const GLchar* fragment_shader_glsl_130 = + "uniform sampler2D Texture;\n" + "in vec2 Frag_UV;\n" + "in vec4 Frag_Color;\n" + "out vec4 Out_Color;\n" + "void main()\n" + "{\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n"; + + const GLchar* fragment_shader_glsl_300_es = + "precision mediump float;\n" + "uniform sampler2D Texture;\n" + "in vec2 Frag_UV;\n" + "in vec4 Frag_Color;\n" + "layout (location = 0) out vec4 Out_Color;\n" + "void main()\n" + "{\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n"; + + const GLchar* fragment_shader_glsl_410_core = + "in vec2 Frag_UV;\n" + "in vec4 Frag_Color;\n" + "uniform sampler2D Texture;\n" + "layout (location = 0) out vec4 Out_Color;\n" + "void main()\n" + "{\n" + " Out_Color = Frag_Color * texture(Texture, Frag_UV.st);\n" + "}\n"; + + // Select shaders matching our GLSL versions + const GLchar* vertex_shader = NULL; + const GLchar* fragment_shader = NULL; + if (glsl_version < 130) + { + vertex_shader = vertex_shader_glsl_120; + fragment_shader = fragment_shader_glsl_120; + } + else if (glsl_version >= 410) + { + vertex_shader = vertex_shader_glsl_410_core; + fragment_shader = fragment_shader_glsl_410_core; + } + else if (glsl_version == 300) + { + vertex_shader = vertex_shader_glsl_300_es; + fragment_shader = fragment_shader_glsl_300_es; + } + else + { + vertex_shader = vertex_shader_glsl_130; + fragment_shader = fragment_shader_glsl_130; + } + + // Create shaders + const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader }; + g_VertHandle = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL); + glCompileShader(g_VertHandle); + CheckShader(g_VertHandle, "vertex shader"); + + const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader }; + g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL); + glCompileShader(g_FragHandle); + CheckShader(g_FragHandle, "fragment shader"); + + g_ShaderHandle = glCreateProgram(); + glAttachShader(g_ShaderHandle, g_VertHandle); + glAttachShader(g_ShaderHandle, g_FragHandle); + glLinkProgram(g_ShaderHandle); + CheckProgram(g_ShaderHandle, "shader program"); + + g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); + g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); + g_AttribLocationVtxPos = glGetAttribLocation(g_ShaderHandle, "Position"); + g_AttribLocationVtxUV = glGetAttribLocation(g_ShaderHandle, "UV"); + g_AttribLocationVtxColor = glGetAttribLocation(g_ShaderHandle, "Color"); + + // Create buffers + glGenBuffers(1, &g_VboHandle); + glGenBuffers(1, &g_ElementsHandle); + + ImGui_ImplOpenGL3_CreateFontsTexture(); + + // Restore modified GL state + glBindTexture(GL_TEXTURE_2D, last_texture); + glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); +#ifndef IMGUI_IMPL_OPENGL_ES2 + glBindVertexArray(last_vertex_array); +#endif + + return true; +} + +void ImGui_ImplOpenGL3_DestroyDeviceObjects() +{ + if (g_VboHandle) { glDeleteBuffers(1, &g_VboHandle); g_VboHandle = 0; } + if (g_ElementsHandle) { glDeleteBuffers(1, &g_ElementsHandle); g_ElementsHandle = 0; } + if (g_ShaderHandle && g_VertHandle) { glDetachShader(g_ShaderHandle, g_VertHandle); } + if (g_ShaderHandle && g_FragHandle) { glDetachShader(g_ShaderHandle, g_FragHandle); } + if (g_VertHandle) { glDeleteShader(g_VertHandle); g_VertHandle = 0; } + if (g_FragHandle) { glDeleteShader(g_FragHandle); g_FragHandle = 0; } + if (g_ShaderHandle) { glDeleteProgram(g_ShaderHandle); g_ShaderHandle = 0; } + + ImGui_ImplOpenGL3_DestroyFontsTexture(); +} diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h new file mode 100644 index 00000000..75f8f8c8 --- /dev/null +++ b/imgui/imgui_impl_opengl3.h @@ -0,0 +1,64 @@ +// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline +// - Desktop GL: 2.x 3.x 4.x +// - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) +// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) + +// Implemented features: +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. +// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bits indices. + +// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. +// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. +// https://github.com/ocornut/imgui + +// About Desktop OpenGL function loaders: +// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. +// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). +// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. + +// About GLSL version: +// The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string. +// On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es" +// Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp. + +#pragma once + +// Backend API +IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL); +IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); +IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); + +// (Optional) Called by Init/NewFrame/Shutdown +IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); +IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); +IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); +IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); + +// Specific OpenGL versions +//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten +//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android + +// Desktop OpenGL: attempt to detect default GL loader based on available header files. +// If auto-detection fails or doesn't select the same GL loader file as used by your application, +// you are likely to get a crash in ImGui_ImplOpenGL3_Init(). +// You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. +#if !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) + #if defined(__has_include) + #if __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLEW + #elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLAD + #elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GL3W + #else + #error "Cannot detect OpenGL loader!" + #endif + #else + #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W + #endif +#endif + diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h new file mode 100644 index 00000000..e7868613 --- /dev/null +++ b/imgui/imgui_internal.h @@ -0,0 +1,1759 @@ +// dear imgui, v1.74 WIP +// (internal structures/api) + +// You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! +// Set: +// #define IMGUI_DEFINE_MATH_OPERATORS +// To implement maths operators for ImVec2 (disabled by default to not collide with using IM_VEC2_CLASS_EXTRA along with your own math types+operators) + +/* + +Index of this file: +// Header mess +// Forward declarations +// STB libraries includes +// Context pointer +// Generic helpers +// Misc data structures +// Main imgui context +// Tab bar, tab item +// Internal API + +*/ + +#pragma once + +//----------------------------------------------------------------------------- +// Header mess +//----------------------------------------------------------------------------- + +#ifndef IMGUI_VERSION +#error Must include imgui.h before imgui_internal.h +#endif + +#include // FILE* +#include // NULL, malloc, free, qsort, atoi, atof +#include // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf +#include // INT_MIN, INT_MAX + +// Visual Studio warnings +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport) +#endif + +// Clang/GCC warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h +#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h +#pragma clang diagnostic ignored "-Wold-style-cast" +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +#endif +#if __has_warning("-Wdouble-promotion") +#pragma clang diagnostic ignored "-Wdouble-promotion" +#endif +#elif defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +//----------------------------------------------------------------------------- +// Forward declarations +//----------------------------------------------------------------------------- + +struct ImRect; // An axis-aligned rectangle (2 points) +struct ImDrawDataBuilder; // Helper to build a ImDrawData instance +struct ImDrawListSharedData; // Data shared between all ImDrawList instances +struct ImGuiColorMod; // Stacked color modifier, backup of modified data so we can restore it +struct ImGuiColumnData; // Storage data for a single column +struct ImGuiColumns; // Storage data for a columns set +struct ImGuiContext; // Main Dear ImGui context +struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum +struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() +struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box +struct ImGuiItemHoveredDataBackup; // Backup and restore IsItemHovered() internal data +struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only +struct ImGuiNavMoveResult; // Result of a directional navigation move query result +struct ImGuiNextWindowData; // Storage for SetNextWindow** functions +struct ImGuiNextItemData; // Storage for SetNextItem** functions +struct ImGuiPopupData; // Storage for current popup stack +struct ImGuiSettingsHandler; // Storage for one type registered in the .ini file +struct ImGuiStyleMod; // Stacked style modifier, backup of modified data so we can restore it +struct ImGuiTabBar; // Storage for a tab bar +struct ImGuiTabItem; // Storage for a tab item (within a tab bar) +struct ImGuiWindow; // Storage for one window +struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame) +struct ImGuiWindowSettings; // Storage for window settings stored in .ini file (we keep one of those even if the actual window wasn't instanced during this session) + +// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. +typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical +typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for ButtonEx(), ButtonBehavior() +typedef int ImGuiColumnsFlags; // -> enum ImGuiColumnsFlags_ // Flags: BeginColumns() +typedef int ImGuiDragFlags; // -> enum ImGuiDragFlags_ // Flags: for DragBehavior() +typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() +typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags +typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() +typedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // Flags: for GetNavInputAmount2d() +typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests +typedef int ImGuiNextItemDataFlags; // -> enum ImGuiNextItemDataFlags_ // Flags: for SetNextItemXXX() functions +typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// Flags: for SetNextWindowXXX() functions +typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for SeparatorEx() +typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for SliderBehavior() +typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() + +//------------------------------------------------------------------------- +// STB libraries includes +//------------------------------------------------------------------------- + +namespace ImStb +{ + +#undef STB_TEXTEDIT_STRING +#undef STB_TEXTEDIT_CHARTYPE +#define STB_TEXTEDIT_STRING ImGuiInputTextState +#define STB_TEXTEDIT_CHARTYPE ImWchar +#define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f +#define STB_TEXTEDIT_UNDOSTATECOUNT 99 +#define STB_TEXTEDIT_UNDOCHARCOUNT 999 +#include "imstb_textedit.h" + +} // namespace ImStb + +//----------------------------------------------------------------------------- +// Context pointer +//----------------------------------------------------------------------------- + +#ifndef GImGui +extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer +#endif + +//----------------------------------------------------------------------------- +// Generic helpers +//----------------------------------------------------------------------------- + +#define IM_PI 3.14159265358979323846f +#ifdef _WIN32 +#define IM_NEWLINE "\r\n" // Play it nice with Windows users (2018/05 news: Microsoft announced that Notepad will finally display Unix-style carriage returns!) +#else +#define IM_NEWLINE "\n" +#endif +#define IM_TABSIZE (4) +#define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] +#define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose +#define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 + +// Debug Logging +#ifndef IMGUI_DEBUG_LOG +#define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) +#endif + +// Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall +#ifdef _MSC_VER +#define IMGUI_CDECL __cdecl +#else +#define IMGUI_CDECL +#endif + +// Helpers: UTF-8 <> wchar +IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count +IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count +IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count +IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) +IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 +IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 + +// Helpers: Misc +IMGUI_API ImU32 ImHashData(const void* data, size_t data_size, ImU32 seed = 0); +IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); +IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size = NULL, int padding_bytes = 0); +IMGUI_API FILE* ImFileOpen(const char* filename, const char* file_open_mode); +static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } +static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } +static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } +static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } +#define ImQsort qsort +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +static inline ImU32 ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] +#endif + +// Helpers: Geometry +IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p); +IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); +IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); +IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); +IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); + +// Helpers: String +IMGUI_API int ImStricmp(const char* str1, const char* str2); +IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); +IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); +IMGUI_API char* ImStrdup(const char* str); +IMGUI_API char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str); +IMGUI_API const char* ImStrchrRange(const char* str_begin, const char* str_end, char c); +IMGUI_API int ImStrlenW(const ImWchar* str); +IMGUI_API const char* ImStreolRange(const char* str, const char* str_end); // End end-of-line +IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line +IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); +IMGUI_API void ImStrTrimBlanks(char* str); +IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3); +IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3); +IMGUI_API const char* ImParseFormatFindStart(const char* format); +IMGUI_API const char* ImParseFormatFindEnd(const char* format); +IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size); +IMGUI_API int ImParseFormatPrecision(const char* format, int default_value); + +// Helpers: ImVec2/ImVec4 operators +// We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.) +// We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. +#ifdef IMGUI_DEFINE_MATH_OPERATORS +static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); } +static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); } +static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); } +static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); } +static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); } +static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); } +static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } +static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } +static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } +static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } +static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z, lhs.w+rhs.w); } +static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); } +static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x*rhs.x, lhs.y*rhs.y, lhs.z*rhs.z, lhs.w*rhs.w); } +#endif + +// Helpers: Maths +// - Wrapper for standard libs functions. (Note that imgui_demo.cpp does _not_ use them to keep the code easy to copy) +#ifndef IMGUI_DISABLE_MATH_FUNCTIONS +static inline float ImFabs(float x) { return fabsf(x); } +static inline float ImSqrt(float x) { return sqrtf(x); } +static inline float ImPow(float x, float y) { return powf(x, y); } +static inline double ImPow(double x, double y) { return pow(x, y); } +static inline float ImFmod(float x, float y) { return fmodf(x, y); } +static inline double ImFmod(double x, double y) { return fmod(x, y); } +static inline float ImCos(float x) { return cosf(x); } +static inline float ImSin(float x) { return sinf(x); } +static inline float ImAcos(float x) { return acosf(x); } +static inline float ImAtan2(float y, float x) { return atan2f(y, x); } +static inline double ImAtof(const char* s) { return atof(s); } +static inline float ImFloorStd(float x) { return floorf(x); } // we already uses our own ImFloor() { return (float)(int)v } internally so the standard one wrapper is named differently (it's used by stb_truetype) +static inline float ImCeil(float x) { return ceilf(x); } +#endif +// - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support for variety of types: signed/unsigned int/long long float/double +// (Exceptionally using templates here but we could also redefine them for variety of types) +template static inline T ImMin(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; } +template static inline T ImMax(T lhs, T rhs) { return lhs >= rhs ? lhs : rhs; } +template static inline T ImClamp(T v, T mn, T mx) { return (v < mn) ? mn : (v > mx) ? mx : v; } +template static inline T ImLerp(T a, T b, float t) { return (T)(a + (b - a) * t); } +template static inline void ImSwap(T& a, T& b) { T tmp = a; a = b; b = tmp; } +template static inline T ImAddClampOverflow(T a, T b, T mn, T mx) { if (b < 0 && (a < mn - b)) return mn; if (b > 0 && (a > mx - b)) return mx; return a + b; } +template static inline T ImSubClampOverflow(T a, T b, T mn, T mx) { if (b > 0 && (a < mn + b)) return mn; if (b < 0 && (a > mx + b)) return mx; return a - b; } +// - Misc maths helpers +static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x < rhs.x ? lhs.x : rhs.x, lhs.y < rhs.y ? lhs.y : rhs.y); } +static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x >= rhs.x ? lhs.x : rhs.x, lhs.y >= rhs.y ? lhs.y : rhs.y); } +static inline ImVec2 ImClamp(const ImVec2& v, const ImVec2& mn, ImVec2 mx) { return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); } +static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return ImVec2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); } +static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); } +static inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); } +static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } +static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; } +static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; } +static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; } +static inline float ImFloor(float f) { return (float)(int)f; } +static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)v.x, (float)(int)v.y); } +static inline int ImModPositive(int a, int b) { return (a + b) % b; } +static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } +static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } +static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } +static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } + +// Helper: ImBoolVector. Store 1-bit per value. +// Note that Resize() currently clears the whole vector. +struct ImBoolVector +{ + ImVector Storage; + ImBoolVector() { } + void Resize(int sz) { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); } + void Clear() { Storage.clear(); } + bool GetBit(int n) const { int off = (n >> 5); int mask = 1 << (n & 31); return (Storage[off] & mask) != 0; } + void SetBit(int n, bool v) { int off = (n >> 5); int mask = 1 << (n & 31); if (v) Storage[off] |= mask; else Storage[off] &= ~mask; } +}; + +// Helper: ImPool<>. Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer, +// Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. +typedef int ImPoolIdx; +template +struct IMGUI_API ImPool +{ + ImVector Data; // Contiguous data + ImGuiStorage Map; // ID->Index + ImPoolIdx FreeIdx; // Next free idx to use + + ImPool() { FreeIdx = 0; } + ~ImPool() { Clear(); } + T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Data[idx] : NULL; } + T* GetByIndex(ImPoolIdx n) { return &Data[n]; } + ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Data.Data && p < Data.Data + Data.Size); return (ImPoolIdx)(p - Data.Data); } + T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Data[*p_idx]; *p_idx = FreeIdx; return Add(); } + bool Contains(const T* p) const { return (p >= Data.Data && p < Data.Data + Data.Size); } + void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Data[idx].~T(); } Map.Clear(); Data.clear(); FreeIdx = 0; } + T* Add() { int idx = FreeIdx; if (idx == Data.Size) { Data.resize(Data.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Data[idx]; } IM_PLACEMENT_NEW(&Data[idx]) T(); return &Data[idx]; } + void Remove(ImGuiID key, const T* p) { Remove(key, GetIndex(p)); } + void Remove(ImGuiID key, ImPoolIdx idx) { Data[idx].~T(); *(int*)&Data[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); } + void Reserve(int capacity) { Data.reserve(capacity); Map.Data.reserve(capacity); } + int GetSize() const { return Data.Size; } +}; + +//----------------------------------------------------------------------------- +// Misc data structures +//----------------------------------------------------------------------------- + +enum ImGuiButtonFlags_ +{ + ImGuiButtonFlags_None = 0, + ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat + ImGuiButtonFlags_PressedOnClickRelease = 1 << 1, // [Default] return true on click + release on same item + ImGuiButtonFlags_PressedOnClick = 1 << 2, // return true on click (default requires click+release) + ImGuiButtonFlags_PressedOnRelease = 1 << 3, // return true on release (default requires click+release) + ImGuiButtonFlags_PressedOnDoubleClick = 1 << 4, // return true on double-click (default requires click+release) + ImGuiButtonFlags_FlattenChildren = 1 << 5, // allow interactions even if a child window is overlapping + ImGuiButtonFlags_AllowItemOverlap = 1 << 6, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() + ImGuiButtonFlags_DontClosePopups = 1 << 7, // disable automatically closing parent popup on press // [UNUSED] + ImGuiButtonFlags_Disabled = 1 << 8, // disable interactions + ImGuiButtonFlags_AlignTextBaseLine = 1 << 9, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine + ImGuiButtonFlags_NoKeyModifiers = 1 << 10, // disable interaction if a key modifier is held + ImGuiButtonFlags_NoHoldingActiveID = 1 << 11, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) + ImGuiButtonFlags_PressedOnDragDropHold = 1 << 12, // press when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) + ImGuiButtonFlags_NoNavFocus = 1 << 13, // don't override navigation focus when activated + ImGuiButtonFlags_NoHoveredOnNav = 1 << 14 // don't report as hovered when navigated on +}; + +enum ImGuiSliderFlags_ +{ + ImGuiSliderFlags_None = 0, + ImGuiSliderFlags_Vertical = 1 << 0 +}; + +enum ImGuiDragFlags_ +{ + ImGuiDragFlags_None = 0, + ImGuiDragFlags_Vertical = 1 << 0 +}; + +enum ImGuiColumnsFlags_ +{ + // Default: 0 + ImGuiColumnsFlags_None = 0, + ImGuiColumnsFlags_NoBorder = 1 << 0, // Disable column dividers + ImGuiColumnsFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers + ImGuiColumnsFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns + ImGuiColumnsFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window + ImGuiColumnsFlags_GrowParentContentsSize= 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. +}; + +// Extend ImGuiSelectableFlags_ +enum ImGuiSelectableFlagsPrivate_ +{ + // NB: need to be in sync with last value of ImGuiSelectableFlags_ + ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, + ImGuiSelectableFlags_PressedOnClick = 1 << 21, + ImGuiSelectableFlags_PressedOnRelease = 1 << 22, + ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 23, // FIXME: We may be able to remove this (added in 6251d379 for menus) + ImGuiSelectableFlags_DrawHoveredWhenHeld= 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. + ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25 +}; + +// Extend ImGuiTreeNodeFlags_ +enum ImGuiTreeNodeFlagsPrivate_ +{ + ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 20 +}; + +enum ImGuiSeparatorFlags_ +{ + ImGuiSeparatorFlags_None = 0, + ImGuiSeparatorFlags_Horizontal = 1 << 0, // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar + ImGuiSeparatorFlags_Vertical = 1 << 1, + ImGuiSeparatorFlags_SpanAllColumns = 1 << 2 +}; + +// Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin(). +// This is going to be exposed in imgui.h when stabilized enough. +enum ImGuiItemFlags_ +{ + ImGuiItemFlags_None = 0, + ImGuiItemFlags_NoTabStop = 1 << 0, // false + ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. + ImGuiItemFlags_Disabled = 1 << 2, // false // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211 + ImGuiItemFlags_NoNav = 1 << 3, // false + ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false + ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window + ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) + ImGuiItemFlags_Default_ = 0 +}; + +// Storage for LastItem data +enum ImGuiItemStatusFlags_ +{ + ImGuiItemStatusFlags_None = 0, + ImGuiItemStatusFlags_HoveredRect = 1 << 0, + ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, + ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) + ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. + ImGuiItemStatusFlags_HasDeactivated = 1 << 4, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. + ImGuiItemStatusFlags_Deactivated = 1 << 5 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. + +#ifdef IMGUI_ENABLE_TEST_ENGINE + , // [imgui_tests only] + ImGuiItemStatusFlags_Openable = 1 << 10, // + ImGuiItemStatusFlags_Opened = 1 << 11, // + ImGuiItemStatusFlags_Checkable = 1 << 12, // + ImGuiItemStatusFlags_Checked = 1 << 13 // +#endif +}; + +enum ImGuiTextFlags_ +{ + ImGuiTextFlags_None = 0, + ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0 +}; + +// FIXME: this is in development, not exposed/functional as a generic feature yet. +// Horizontal/Vertical enums are fixed to 0/1 so they may be used to index ImVec2 +enum ImGuiLayoutType_ +{ + ImGuiLayoutType_Horizontal = 0, + ImGuiLayoutType_Vertical = 1 +}; + +enum ImGuiLogType +{ + ImGuiLogType_None = 0, + ImGuiLogType_TTY, + ImGuiLogType_File, + ImGuiLogType_Buffer, + ImGuiLogType_Clipboard +}; + +// X/Y enums are fixed to 0/1 so they may be used to index ImVec2 +enum ImGuiAxis +{ + ImGuiAxis_None = -1, + ImGuiAxis_X = 0, + ImGuiAxis_Y = 1 +}; + +enum ImGuiPlotType +{ + ImGuiPlotType_Lines, + ImGuiPlotType_Histogram +}; + +enum ImGuiInputSource +{ + ImGuiInputSource_None = 0, + ImGuiInputSource_Mouse, + ImGuiInputSource_Nav, + ImGuiInputSource_NavKeyboard, // Only used occasionally for storage, not tested/handled by most code + ImGuiInputSource_NavGamepad, // " + ImGuiInputSource_COUNT +}; + +// FIXME-NAV: Clarify/expose various repeat delay/rate +enum ImGuiInputReadMode +{ + ImGuiInputReadMode_Down, + ImGuiInputReadMode_Pressed, + ImGuiInputReadMode_Released, + ImGuiInputReadMode_Repeat, + ImGuiInputReadMode_RepeatSlow, + ImGuiInputReadMode_RepeatFast +}; + +enum ImGuiNavHighlightFlags_ +{ + ImGuiNavHighlightFlags_None = 0, + ImGuiNavHighlightFlags_TypeDefault = 1 << 0, + ImGuiNavHighlightFlags_TypeThin = 1 << 1, + ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. + ImGuiNavHighlightFlags_NoRounding = 1 << 3 +}; + +enum ImGuiNavDirSourceFlags_ +{ + ImGuiNavDirSourceFlags_None = 0, + ImGuiNavDirSourceFlags_Keyboard = 1 << 0, + ImGuiNavDirSourceFlags_PadDPad = 1 << 1, + ImGuiNavDirSourceFlags_PadLStick = 1 << 2 +}; + +enum ImGuiNavMoveFlags_ +{ + ImGuiNavMoveFlags_None = 0, + ImGuiNavMoveFlags_LoopX = 1 << 0, // On failed request, restart from opposite side + ImGuiNavMoveFlags_LoopY = 1 << 1, + ImGuiNavMoveFlags_WrapX = 1 << 2, // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left) + ImGuiNavMoveFlags_WrapY = 1 << 3, // This is not super useful for provided for completeness + ImGuiNavMoveFlags_AllowCurrentNavId = 1 << 4, // Allow scoring and considering the current NavId as a move target candidate. This is used when the move source is offset (e.g. pressing PageDown actually needs to send a Up move request, if we are pressing PageDown from the bottom-most item we need to stay in place) + ImGuiNavMoveFlags_AlsoScoreVisibleSet = 1 << 5, // Store alternate result in NavMoveResultLocalVisibleSet that only comprise elements that are already fully visible. + ImGuiNavMoveFlags_ScrollToEdge = 1 << 6 +}; + +enum ImGuiNavForward +{ + ImGuiNavForward_None, + ImGuiNavForward_ForwardQueued, + ImGuiNavForward_ForwardActive +}; + +enum ImGuiNavLayer +{ + ImGuiNavLayer_Main = 0, // Main scrolling layer + ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt/ImGuiNavInput_Menu) + ImGuiNavLayer_COUNT +}; + +enum ImGuiPopupPositionPolicy +{ + ImGuiPopupPositionPolicy_Default, + ImGuiPopupPositionPolicy_ComboBox +}; + +// 1D vector (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) +struct ImVec1 +{ + float x; + ImVec1() { x = 0.0f; } + ImVec1(float _x) { x = _x; } +}; + +// 2D vector (half-size integer) +struct ImVec2ih +{ + short x, y; + ImVec2ih() { x = y = 0; } + ImVec2ih(short _x, short _y) { x = _x; y = _y; } +}; + +// 2D axis aligned bounding-box +// NB: we can't rely on ImVec2 math operators being available here +struct IMGUI_API ImRect +{ + ImVec2 Min; // Upper-left + ImVec2 Max; // Lower-right + + ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {} + ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} + ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} + ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} + + ImVec2 GetCenter() const { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); } + ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } + float GetWidth() const { return Max.x - Min.x; } + float GetHeight() const { return Max.y - Min.y; } + ImVec2 GetTL() const { return Min; } // Top-left + ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right + ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left + ImVec2 GetBR() const { return Max; } // Bottom-right + bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; } + bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; } + bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; } + void Add(const ImVec2& p) { if (Min.x > p.x) Min.x = p.x; if (Min.y > p.y) Min.y = p.y; if (Max.x < p.x) Max.x = p.x; if (Max.y < p.y) Max.y = p.y; } + void Add(const ImRect& r) { if (Min.x > r.Min.x) Min.x = r.Min.x; if (Min.y > r.Min.y) Min.y = r.Min.y; if (Max.x < r.Max.x) Max.x = r.Max.x; if (Max.y < r.Max.y) Max.y = r.Max.y; } + void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; } + void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; } + void Translate(const ImVec2& d) { Min.x += d.x; Min.y += d.y; Max.x += d.x; Max.y += d.y; } + void TranslateX(float dx) { Min.x += dx; Max.x += dx; } + void TranslateY(float dy) { Min.y += dy; Max.y += dy; } + void ClipWith(const ImRect& r) { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); } // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display. + void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. + void Floor() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; } + bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } +}; + +// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). +struct ImGuiDataTypeInfo +{ + size_t Size; // Size in byte + const char* PrintFmt; // Default printf format for the type + const char* ScanFmt; // Default scanf format for the type +}; + +// Stacked color modifier, backup of modified data so we can restore it +struct ImGuiColorMod +{ + ImGuiCol Col; + ImVec4 BackupValue; +}; + +// Stacked style modifier, backup of modified data so we can restore it. Data type inferred from the variable. +struct ImGuiStyleMod +{ + ImGuiStyleVar VarIdx; + union { int BackupInt[2]; float BackupFloat[2]; }; + ImGuiStyleMod(ImGuiStyleVar idx, int v) { VarIdx = idx; BackupInt[0] = v; } + ImGuiStyleMod(ImGuiStyleVar idx, float v) { VarIdx = idx; BackupFloat[0] = v; } + ImGuiStyleMod(ImGuiStyleVar idx, ImVec2 v) { VarIdx = idx; BackupFloat[0] = v.x; BackupFloat[1] = v.y; } +}; + +// Stacked storage data for BeginGroup()/EndGroup() +struct ImGuiGroupData +{ + ImVec2 BackupCursorPos; + ImVec2 BackupCursorMaxPos; + ImVec1 BackupIndent; + ImVec1 BackupGroupOffset; + ImVec2 BackupCurrLineSize; + float BackupCurrLineTextBaseOffset; + ImGuiID BackupActiveIdIsAlive; + bool BackupActiveIdPreviousFrameIsAlive; + bool EmitItem; +}; + +// Simple column measurement, currently used for MenuItem() only.. This is very short-sighted/throw-away code and NOT a generic helper. +struct IMGUI_API ImGuiMenuColumns +{ + float Spacing; + float Width, NextWidth; + float Pos[3], NextWidths[3]; + + ImGuiMenuColumns(); + void Update(int count, float spacing, bool clear); + float DeclColumns(float w0, float w1, float w2); + float CalcExtraSpace(float avail_w); +}; + +// Internal state of the currently focused/edited text input box +struct IMGUI_API ImGuiInputTextState +{ + ImGuiID ID; // widget id owning the text state + int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 len is valid even if TextA is not. + ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. + ImVector TextA; // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity. + ImVector InitialTextA; // backup of end-user buffer at the time of focus (in UTF-8, unaltered) + bool TextAIsValid; // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument) + int BufCapacityA; // end-user buffer capacity + float ScrollX; // horizontal scrolling/offset + ImStb::STB_TexteditState Stb; // state for stb_textedit.h + float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately + bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) + bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection + ImGuiInputTextFlags UserFlags; // Temporarily set while we call user's callback + ImGuiInputTextCallback UserCallback; // " + void* UserCallbackData; // " + + ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } + void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } + void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } + int GetUndoAvailCount() const { return Stb.undostate.undo_point; } + int GetRedoAvailCount() const { return STB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; } + void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation + + // Cursor & Selection + void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking + void CursorClamp() { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); } + bool HasSelection() const { return Stb.select_start != Stb.select_end; } + void ClearSelection() { Stb.select_start = Stb.select_end = Stb.cursor; } + void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } +}; + +// Windows data saved in imgui.ini file +struct ImGuiWindowSettings +{ + char* Name; + ImGuiID ID; + ImVec2ih Pos; + ImVec2ih Size; + bool Collapsed; + + ImGuiWindowSettings() { Name = NULL; ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; } +}; + +struct ImGuiSettingsHandler +{ + const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']' + ImGuiID TypeHash; // == ImHashStr(TypeName) + void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); // Read: Called when entering into a new ini entry e.g. "[Window][Name]" + void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry + void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); // Write: Output every entries into 'out_buf' + void* UserData; + + ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } +}; + +// Storage for current popup stack +struct ImGuiPopupData +{ + ImGuiID PopupId; // Set on OpenPopup() + ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() + ImGuiWindow* SourceWindow; // Set on OpenPopup() copy of NavWindow at the time of opening the popup + int OpenFrameCount; // Set on OpenPopup() + ImGuiID OpenParentId; // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) + ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) + ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup + + ImGuiPopupData() { PopupId = 0; Window = SourceWindow = NULL; OpenFrameCount = -1; OpenParentId = 0; } +}; + +struct ImGuiColumnData +{ + float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) + float OffsetNormBeforeResize; + ImGuiColumnsFlags Flags; // Not exposed + ImRect ClipRect; + + ImGuiColumnData() { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; } +}; + +struct ImGuiColumns +{ + ImGuiID ID; + ImGuiColumnsFlags Flags; + bool IsFirstFrame; + bool IsBeingResized; + int Current; + int Count; + float OffMinX, OffMaxX; // Offsets from HostWorkRect.Min.x + float LineMinY, LineMaxY; + float HostCursorPosY; // Backup of CursorPos at the time of BeginColumns() + float HostCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() + ImRect HostClipRect; // Backup of ClipRect at the time of BeginColumns() + ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() + ImVector Columns; + + ImGuiColumns() { Clear(); } + void Clear() + { + ID = 0; + Flags = ImGuiColumnsFlags_None; + IsFirstFrame = false; + IsBeingResized = false; + Current = 0; + Count = 1; + OffMinX = OffMaxX = 0.0f; + LineMinY = LineMaxY = 0.0f; + HostCursorPosY = 0.0f; + HostCursorMaxPosX = 0.0f; + Columns.clear(); + } +}; + +// Data shared between all ImDrawList instances +struct IMGUI_API ImDrawListSharedData +{ + ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas + ImFont* Font; // Current/default font (optional, for simplified AddText overload) + float FontSize; // Current/default font size (optional, for simplified AddText overload) + float CurveTessellationTol; + ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() + ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) + + // Const data + // FIXME: Bake rounded corners fill/borders in atlas + ImVec2 CircleVtx12[12]; + + ImDrawListSharedData(); +}; + +struct ImDrawDataBuilder +{ + ImVector Layers[2]; // Global layers for: regular, tooltip + + void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } + void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } + IMGUI_API void FlattenIntoSingleLayer(); +}; + +struct ImGuiNavMoveResult +{ + ImGuiID ID; // Best candidate + ImGuiID SelectScopeId;// Best candidate window current selectable group ID + ImGuiWindow* Window; // Best candidate window + float DistBox; // Best candidate box distance to current NavId + float DistCenter; // Best candidate center distance to current NavId + float DistAxial; + ImRect RectRel; // Best candidate bounding box in window relative space + + ImGuiNavMoveResult() { Clear(); } + void Clear() { ID = SelectScopeId = 0; Window = NULL; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); } +}; + +enum ImGuiNextWindowDataFlags_ +{ + ImGuiNextWindowDataFlags_None = 0, + ImGuiNextWindowDataFlags_HasPos = 1 << 0, + ImGuiNextWindowDataFlags_HasSize = 1 << 1, + ImGuiNextWindowDataFlags_HasContentSize = 1 << 2, + ImGuiNextWindowDataFlags_HasCollapsed = 1 << 3, + ImGuiNextWindowDataFlags_HasSizeConstraint = 1 << 4, + ImGuiNextWindowDataFlags_HasFocus = 1 << 5, + ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6 +}; + +// Storage for SetNexWindow** functions +struct ImGuiNextWindowData +{ + ImGuiNextWindowDataFlags Flags; + ImGuiCond PosCond; + ImGuiCond SizeCond; + ImGuiCond CollapsedCond; + ImVec2 PosVal; + ImVec2 PosPivotVal; + ImVec2 SizeVal; + ImVec2 ContentSizeVal; + bool CollapsedVal; + ImRect SizeConstraintRect; + ImGuiSizeCallback SizeCallback; + void* SizeCallbackUserData; + float BgAlphaVal; + ImVec2 MenuBarOffsetMinVal; // *Always on* This is not exposed publicly, so we don't clear it. + + ImGuiNextWindowData() { memset(this, 0, sizeof(*this)); } + inline void ClearFlags() { Flags = ImGuiNextWindowDataFlags_None; } +}; + +enum ImGuiNextItemDataFlags_ +{ + ImGuiNextItemDataFlags_None = 0, + ImGuiNextItemDataFlags_HasWidth = 1 << 0, + ImGuiNextItemDataFlags_HasOpen = 1 << 1 +}; + +struct ImGuiNextItemData +{ + ImGuiNextItemDataFlags Flags; + float Width; // Set by SetNextItemWidth(). + bool OpenVal; // Set by SetNextItemOpen() function. + ImGuiCond OpenCond; + + ImGuiNextItemData() { memset(this, 0, sizeof(*this)); } + inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } +}; + +//----------------------------------------------------------------------------- +// Tabs +//----------------------------------------------------------------------------- + +struct ImGuiShrinkWidthItem +{ + int Index; + float Width; +}; + +struct ImGuiPtrOrIndex +{ + void* Ptr; // Either field can be set, not both. e.g. Dock node tab bars are loose while BeginTabBar() ones are in a pool. + int Index; // Usually index in a main pool. + + ImGuiPtrOrIndex(void* ptr) { Ptr = ptr; Index = -1; } + ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } +}; + +//----------------------------------------------------------------------------- +// Main imgui context +//----------------------------------------------------------------------------- + +struct ImGuiContext +{ + bool Initialized; + bool FrameScopeActive; // Set by NewFrame(), cleared by EndFrame() + bool FrameScopePushedImplicitWindow; // Set by NewFrame(), cleared by EndFrame() + bool FontAtlasOwnedByContext; // Io.Fonts-> is owned by the ImGuiContext and will be destructed along with it. + ImGuiIO IO; + ImGuiStyle Style; + ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() + float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. + float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height. + ImDrawListSharedData DrawListSharedData; + double Time; + int FrameCount; + int FrameCountEnded; + int FrameCountRendered; + + // Windows state + ImVector Windows; // Windows, sorted in display order, back to front + ImVector WindowsFocusOrder; // Windows, sorted in focus order, back to front + ImVector WindowsSortBuffer; + ImVector CurrentWindowStack; + ImGuiStorage WindowsById; + int WindowsActiveCount; + ImGuiWindow* CurrentWindow; // Being drawn into + ImGuiWindow* HoveredWindow; // Will catch mouse inputs + ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only) + ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow. + ImGuiWindow* WheelingWindow; + ImVec2 WheelingWindowRefMousePos; + float WheelingWindowTimer; + + // Item/widgets state and tracking information + ImGuiID HoveredId; // Hovered widget + bool HoveredIdAllowOverlap; + ImGuiID HoveredIdPreviousFrame; + float HoveredIdTimer; // Measure contiguous hovering time + float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active + ImGuiID ActiveId; // Active widget + ImGuiID ActiveIdIsAlive; // Active widget has been seen this frame (we can't use a bool as the ActiveId may change within the frame) + float ActiveIdTimer; + bool ActiveIdIsJustActivated; // Set at the time of activation for one frame + bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always) + bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. + bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. + bool ActiveIdHasBeenEditedThisFrame; + ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those directional navigation requests (e.g. can activate a button and move away from it) + ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs. + ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array. + ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) + ImGuiWindow* ActiveIdWindow; + ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) + ImGuiID ActiveIdPreviousFrame; + bool ActiveIdPreviousFrameIsAlive; + bool ActiveIdPreviousFrameHasBeenEditedBefore; + ImGuiWindow* ActiveIdPreviousFrameWindow; + + ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. + float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. + + // Next window/item data + ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions + ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions + + // Shared stacks + ImVector ColorModifiers; // Stack for PushStyleColor()/PopStyleColor() + ImVector StyleModifiers; // Stack for PushStyleVar()/PopStyleVar() + ImVector FontStack; // Stack for PushFont()/PopFont() + ImVectorOpenPopupStack; // Which popups are open (persistent) + ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + + // Navigation data (for gamepad/keyboard) + ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' + ImGuiID NavId; // Focused item for navigation + ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() + ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 + ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 + ImGuiID NavInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0 + ImGuiID NavJustTabbedId; // Just tabbed to this id. + ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). + ImGuiID NavJustMovedToMultiSelectScopeId; // Just navigated to this select scope id (result of a successfully MoveRequest). + ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. + ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. + ImRect NavScoringRectScreen; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. + int NavScoringCount; // Metrics for debugging + ImGuiWindow* NavWindowingTarget; // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed top-most. + ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f + ImGuiWindow* NavWindowingList; + float NavWindowingTimer; + float NavWindowingHighlightAlpha; + bool NavWindowingToggleLayer; + ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. + int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing + bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRefRectRel is valid + bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) + bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) + bool NavDisableMouseHover; // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again. + bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest + bool NavInitRequest; // Init request for appearing window to select first item + bool NavInitRequestFromMove; + ImGuiID NavInitResultId; + ImRect NavInitResultRectRel; + bool NavMoveFromClampedRefRect; // Set by manual scrolling, if we scroll to a point where NavId isn't visible we reset navigation from visible items + bool NavMoveRequest; // Move request for this frame + ImGuiNavMoveFlags NavMoveRequestFlags; + ImGuiNavForward NavMoveRequestForward; // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu) + ImGuiDir NavMoveDir, NavMoveDirLast; // Direction of the move request (left/right/up/down), direction of the previous move request + ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? + ImGuiNavMoveResult NavMoveResultLocal; // Best move request candidate within NavWindow + ImGuiNavMoveResult NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) + ImGuiNavMoveResult NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) + + // Tabbing system (older than Nav, active even if Nav is disabled. FIXME-NAV: This needs a redesign!) + ImGuiWindow* FocusRequestCurrWindow; // + ImGuiWindow* FocusRequestNextWindow; // + int FocusRequestCurrCounterAll; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) + int FocusRequestCurrCounterTab; // Tab item being requested for focus, stored as an index + int FocusRequestNextCounterAll; // Stored for next frame + int FocusRequestNextCounterTab; // " + bool FocusTabPressed; // + + // Render + ImDrawData DrawData; // Main ImDrawData instance to pass render information to the user + ImDrawDataBuilder DrawDataBuilder; + float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) + ImDrawList BackgroundDrawList; // First draw list to be rendered. + ImDrawList ForegroundDrawList; // Last draw list to be rendered. This is where we the render software mouse cursor (if io.MouseDrawCursor is set) and most debug overlays. + ImGuiMouseCursor MouseCursor; + + // Drag and Drop + bool DragDropActive; + bool DragDropWithinSourceOrTarget; + ImGuiDragDropFlags DragDropSourceFlags; + int DragDropSourceFrameCount; + int DragDropMouseButton; + ImGuiPayload DragDropPayload; + ImRect DragDropTargetRect; + ImGuiID DragDropTargetId; + ImGuiDragDropFlags DragDropAcceptFlags; + float DragDropAcceptIdCurrRectSurface; // Target item surface (we resolve overlapping targets by prioritizing the smaller surface) + ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload) + ImGuiID DragDropAcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets) + int DragDropAcceptFrameCount; // Last time a target expressed a desire to accept the source + ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly + unsigned char DragDropPayloadBufLocal[8]; // Local buffer for small payloads + + // Tab bars + ImGuiTabBar* CurrentTabBar; + ImPool TabBars; + ImVector CurrentTabBarStack; + ImVector ShrinkWidthBuffer; + + // Widget state + ImVec2 LastValidMousePos; + ImGuiInputTextState InputTextState; + ImFont InputTextPasswordFont; + ImGuiID TempInputTextId; // Temporary text input when CTRL+clicking on a slider, etc. + ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets + float ColorEditLastHue; + float ColorEditLastColor[3]; + ImVec4 ColorPickerRef; + bool DragCurrentAccumDirty; + float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings + float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio + float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? + int TooltipOverrideCount; + ImVector PrivateClipboard; // If no custom clipboard handler is defined + + // Range-Select/Multi-Select + // [This is unused in this branch, but left here to facilitate merging/syncing multiple branches] + ImGuiID MultiSelectScopeId; + + // Platform support + ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor + ImVec2 PlatformImeLastPos; + + // Settings + bool SettingsLoaded; + float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero + ImGuiTextBuffer SettingsIniData; // In memory .ini settings + ImVector SettingsHandlers; // List of .ini settings handlers + ImVector SettingsWindows; // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving) + + // Logging + bool LogEnabled; + ImGuiLogType LogType; + FILE* LogFile; // If != NULL log to stdout/ file + ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. + float LogLinePosY; + bool LogLineFirstItem; + int LogDepthRef; + int LogDepthToExpand; + int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. + + // Debug Tools + bool DebugItemPickerActive; + ImGuiID DebugItemPickerBreakID; // Will call IM_DEBUG_BREAK() when encountering this id + + // Misc + float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. + int FramerateSecPerFrameIdx; + float FramerateSecPerFrameAccum; + int WantCaptureMouseNextFrame; // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags + int WantCaptureKeyboardNextFrame; + int WantTextInputNextFrame; + char TempBuffer[1024*3+1]; // Temporary text buffer + + ImGuiContext(ImFontAtlas* shared_font_atlas) : BackgroundDrawList(&DrawListSharedData), ForegroundDrawList(&DrawListSharedData) + { + Initialized = false; + FrameScopeActive = FrameScopePushedImplicitWindow = false; + Font = NULL; + FontSize = FontBaseSize = 0.0f; + FontAtlasOwnedByContext = shared_font_atlas ? false : true; + IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); + Time = 0.0f; + FrameCount = 0; + FrameCountEnded = FrameCountRendered = -1; + + WindowsActiveCount = 0; + CurrentWindow = NULL; + HoveredWindow = NULL; + HoveredRootWindow = NULL; + MovingWindow = NULL; + WheelingWindow = NULL; + WheelingWindowTimer = 0.0f; + + HoveredId = 0; + HoveredIdAllowOverlap = false; + HoveredIdPreviousFrame = 0; + HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; + ActiveId = 0; + ActiveIdIsAlive = 0; + ActiveIdTimer = 0.0f; + ActiveIdIsJustActivated = false; + ActiveIdAllowOverlap = false; + ActiveIdHasBeenPressedBefore = false; + ActiveIdHasBeenEditedBefore = false; + ActiveIdHasBeenEditedThisFrame = false; + ActiveIdUsingNavDirMask = 0x00; + ActiveIdUsingNavInputMask = 0x00; + ActiveIdUsingKeyInputMask = 0x00; + ActiveIdClickOffset = ImVec2(-1,-1); + ActiveIdWindow = NULL; + ActiveIdSource = ImGuiInputSource_None; + + ActiveIdPreviousFrame = 0; + ActiveIdPreviousFrameIsAlive = false; + ActiveIdPreviousFrameHasBeenEditedBefore = false; + ActiveIdPreviousFrameWindow = NULL; + + LastActiveId = 0; + LastActiveIdTimer = 0.0f; + + NavWindow = NULL; + NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; + NavJustTabbedId = NavJustMovedToId = NavJustMovedToMultiSelectScopeId = NavNextActivateId = 0; + NavInputSource = ImGuiInputSource_None; + NavScoringRectScreen = ImRect(); + NavScoringCount = 0; + NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL; + NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; + NavWindowingToggleLayer = false; + NavLayer = ImGuiNavLayer_Main; + NavIdTabCounter = INT_MAX; + NavIdIsAlive = false; + NavMousePosDirty = false; + NavDisableHighlight = true; + NavDisableMouseHover = false; + NavAnyRequest = false; + NavInitRequest = false; + NavInitRequestFromMove = false; + NavInitResultId = 0; + NavMoveFromClampedRefRect = false; + NavMoveRequest = false; + NavMoveRequestFlags = 0; + NavMoveRequestForward = ImGuiNavForward_None; + NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None; + + FocusRequestCurrWindow = FocusRequestNextWindow = NULL; + FocusRequestCurrCounterAll = FocusRequestCurrCounterTab = INT_MAX; + FocusRequestNextCounterAll = FocusRequestNextCounterTab = INT_MAX; + FocusTabPressed = false; + + DimBgRatio = 0.0f; + BackgroundDrawList._OwnerName = "##Background"; // Give it a name for debugging + ForegroundDrawList._OwnerName = "##Foreground"; // Give it a name for debugging + MouseCursor = ImGuiMouseCursor_Arrow; + + DragDropActive = DragDropWithinSourceOrTarget = false; + DragDropSourceFlags = 0; + DragDropSourceFrameCount = -1; + DragDropMouseButton = -1; + DragDropTargetId = 0; + DragDropAcceptFlags = 0; + DragDropAcceptIdCurrRectSurface = 0.0f; + DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; + DragDropAcceptFrameCount = -1; + memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); + + CurrentTabBar = NULL; + + LastValidMousePos = ImVec2(0.0f, 0.0f); + TempInputTextId = 0; + ColorEditOptions = ImGuiColorEditFlags__OptionsDefault; + ColorEditLastHue = 0.0f; + ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX; + DragCurrentAccumDirty = false; + DragCurrentAccum = 0.0f; + DragSpeedDefaultRatio = 1.0f / 100.0f; + ScrollbarClickDeltaToGrabCenter = 0.0f; + TooltipOverrideCount = 0; + + MultiSelectScopeId = 0; + + PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); + + SettingsLoaded = false; + SettingsDirtyTimer = 0.0f; + + LogEnabled = false; + LogType = ImGuiLogType_None; + LogFile = NULL; + LogLinePosY = FLT_MAX; + LogLineFirstItem = false; + LogDepthRef = 0; + LogDepthToExpand = LogDepthToExpandDefault = 2; + + DebugItemPickerActive = false; + DebugItemPickerBreakID = 0; + + memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); + FramerateSecPerFrameIdx = 0; + FramerateSecPerFrameAccum = 0.0f; + WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; + memset(TempBuffer, 0, sizeof(TempBuffer)); + } +}; + +//----------------------------------------------------------------------------- +// ImGuiWindow +//----------------------------------------------------------------------------- + +// Transient per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the DC variable name in ImGuiWindow. +// FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered. +struct IMGUI_API ImGuiWindowTempData +{ + ImVec2 CursorPos; // Current emitting position, in absolute coordinates. + ImVec2 CursorPosPrevLine; + ImVec2 CursorStartPos; // Initial position after Begin(), generally ~ window position + WindowPadding. + ImVec2 CursorMaxPos; // Used to implicitly calculate the size of our contents, always growing during the frame. Used to calculate window->ContentSize at the beginning of next frame + ImVec2 CurrLineSize; + ImVec2 PrevLineSize; + float CurrLineTextBaseOffset; // Baseline offset (0.0f by default on a new line, generally == style.FramePadding.y when a framed item has been added). + float PrevLineTextBaseOffset; + int TreeDepth; // Current tree depth. + ImU32 TreeMayJumpToParentOnPopMask; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. + ImGuiID LastItemId; // ID for last item + ImGuiItemStatusFlags LastItemStatusFlags; // Status flags for last item (see ImGuiItemStatusFlags_) + ImRect LastItemRect; // Interaction rect for last item + ImRect LastItemDisplayRect; // End-user display rect for last item (only valid if LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) + ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) + int NavLayerCurrentMask; // = (1 << NavLayerCurrent) used by ItemAdd prior to clipping. + int NavLayerActiveMask; // Which layer have been written to (result from previous frame) + int NavLayerActiveMaskNext; // Which layer have been written to (buffer for current frame) + bool NavHideHighlightOneFrame; + bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) + bool MenuBarAppending; // FIXME: Remove this + ImVec2 MenuBarOffset; // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs. + ImVector ChildWindows; + ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state) + ImGuiLayoutType LayoutType; + ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() + int FocusCounterAll; // Counter for focus/tabbing system. Start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) + int FocusCounterTab; // (same, but only count widgets which you can Tab through) + + // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. + ImGuiItemFlags ItemFlags; // == ItemFlagsStack.back() [empty == ImGuiItemFlags_Default] + float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window + float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f] + ImVectorItemFlagsStack; + ImVector ItemWidthStack; + ImVector TextWrapPosStack; + ImVectorGroupStack; + short StackSizesBackup[6]; // Store size of various stacks for asserting + + ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) + ImVec1 GroupOffset; + ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. + ImGuiColumns* CurrentColumns; // Current columns set + + ImGuiWindowTempData() + { + CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f); + CurrLineSize = PrevLineSize = ImVec2(0.0f, 0.0f); + CurrLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f; + TreeDepth = 0; + TreeMayJumpToParentOnPopMask = 0x00; + LastItemId = 0; + LastItemStatusFlags = 0; + LastItemRect = LastItemDisplayRect = ImRect(); + NavLayerActiveMask = NavLayerActiveMaskNext = 0x00; + NavLayerCurrent = ImGuiNavLayer_Main; + NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + NavHideHighlightOneFrame = false; + NavHasScroll = false; + MenuBarAppending = false; + MenuBarOffset = ImVec2(0.0f, 0.0f); + StateStorage = NULL; + LayoutType = ParentLayoutType = ImGuiLayoutType_Vertical; + FocusCounterAll = FocusCounterTab = -1; + + ItemFlags = ImGuiItemFlags_Default_; + ItemWidth = 0.0f; + TextWrapPos = -1.0f; + memset(StackSizesBackup, 0, sizeof(StackSizesBackup)); + + Indent = ImVec1(0.0f); + GroupOffset = ImVec1(0.0f); + ColumnsOffset = ImVec1(0.0f); + CurrentColumns = NULL; + } +}; + +// Storage for one window +struct IMGUI_API ImGuiWindow +{ + char* Name; + ImGuiID ID; // == ImHash(Name) + ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_ + ImVec2 Pos; // Position (always rounded-up to nearest pixel) + ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) + ImVec2 SizeFull; // Size when non collapsed + ImVec2 ContentSize; // Size of contents/scrollable client area (calculated from the extents reach of the cursor) from previous frame. Does not include window decoration or window padding. + ImVec2 ContentSizeExplicit; // Size of contents/scrollable client area explicitly request by the user via SetNextWindowContentSize(). + ImVec2 WindowPadding; // Window padding at the time of Begin(). + float WindowRounding; // Window rounding at the time of Begin(). + float WindowBorderSize; // Window border size at the time of Begin(). + int NameBufLen; // Size of buffer storing Name. May be larger than strlen(Name)! + ImGuiID MoveId; // == window->GetID("#MOVE") + ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window) + ImVec2 Scroll; + ImVec2 ScrollMax; + ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change) + ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered + ImVec2 ScrollbarSizes; // Size taken by scrollbars on each axis + bool ScrollbarX, ScrollbarY; // Are scrollbars visible? + bool Active; // Set to true on Begin(), unless Collapsed + bool WasActive; + bool WriteAccessed; // Set to true when any widget access the current window + bool Collapsed; // Set when collapsing window to become only title-bar + bool WantCollapseToggle; + bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed) + bool Appearing; // Set during the frame where the window is appearing (or re-appearing) + bool Hidden; // Do not display (== (HiddenFrames*** > 0)) + bool HasCloseButton; // Set when the window has a close button (p_open != NULL) + signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) + short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) + short BeginOrderWithinParent; // Order within immediate parent window, if we are a child window. Otherwise 0. + short BeginOrderWithinContext; // Order within entire imgui context. This is mostly used for debugging submission order related issues. + ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) + ImS8 AutoFitFramesX, AutoFitFramesY; + ImS8 AutoFitChildAxises; + bool AutoFitOnlyGrows; + ImGuiDir AutoPosLastDirection; + int HiddenFramesCanSkipItems; // Hide the window for N frames + int HiddenFramesCannotSkipItems; // Hide the window for N frames while allowing items to be submitted so we can measure their size + ImGuiCond SetWindowPosAllowFlags; // store acceptable condition flags for SetNextWindowPos() use. + ImGuiCond SetWindowSizeAllowFlags; // store acceptable condition flags for SetNextWindowSize() use. + ImGuiCond SetWindowCollapsedAllowFlags; // store acceptable condition flags for SetNextWindowCollapsed() use. + ImVec2 SetWindowPosVal; // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size) + ImVec2 SetWindowPosPivot; // store window pivot for positioning. ImVec2(0,0) when positioning from top-left corner; ImVec2(0.5f,0.5f) for centering; ImVec2(1,1) for bottom right. + + ImVector IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack. (In theory this should be in the TempData structure) + ImGuiWindowTempData DC; // Temporary per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the "DC" variable name. + + // The best way to understand what those rectangles are is to use the 'Metrics -> Tools -> Show windows rectangles' viewer. + // The main 'OuterRect', omitted as a field, is window->Rect(). + ImRect OuterRectClipped; // == Window->Rect() just after setup in Begin(). == window->Rect() for root window. + ImRect InnerRect; // Inner rectangle (omit title bar, menu bar, scroll bar) + ImRect InnerClipRect; // == InnerRect shrunk by WindowPadding*0.5f on each side, clipped within viewport or parent clip rect. + ImRect WorkRect; // Cover the whole scrolling region, shrunk by WindowPadding*1.0f on each side. This is meant to replace ContentsRegionRect over time (from 1.71+ onward). + ImRect ClipRect; // Current clipping/scissoring rectangle, evolve as we are using PushClipRect(), etc. == DrawList->clip_rect_stack.back(). + ImRect ContentsRegionRect; // FIXME: This is currently confusing/misleading. It is essentially WorkRect but not handling of scrolling. We currently rely on it as right/bottom aligned sizing operation need some size to rely on. + + int LastFrameActive; // Last frame number the window was Active. + float LastTimeActive; // Last timestamp the window was Active (using float as we don't need high precision there) + float ItemWidthDefault; + ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items + ImGuiStorage StateStorage; + ImVector ColumnsStorage; + float FontWindowScale; // User scale multiplier per-window, via SetWindowFontScale() + int SettingsIdx; // Index into SettingsWindow[] (indices are always valid as we only grow the array from the back) + + ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) + ImDrawList DrawListInst; + ImGuiWindow* ParentWindow; // If we are a child _or_ popup window, this is pointing to our parent. Otherwise NULL. + ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window. + ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. + ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag. + + ImGuiWindow* NavLastChildNavWindow; // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.) + ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) + ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space + + bool MemoryCompacted; + int MemoryDrawListIdxCapacity; + int MemoryDrawListVtxCapacity; + +public: + ImGuiWindow(ImGuiContext* context, const char* name); + ~ImGuiWindow(); + + ImGuiID GetID(const char* str, const char* str_end = NULL); + ImGuiID GetID(const void* ptr); + ImGuiID GetID(int n); + ImGuiID GetIDNoKeepAlive(const char* str, const char* str_end = NULL); + ImGuiID GetIDNoKeepAlive(const void* ptr); + ImGuiID GetIDNoKeepAlive(int n); + ImGuiID GetIDFromRectangle(const ImRect& r_abs); + + // We don't use g.FontSize because the window may be != g.CurrentWidow. + ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } + float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } + float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } + ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } + float MenuBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } + ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } +}; + +// Backup and restore just enough data to be able to use IsItemHovered() on item A after another B in the same window has overwritten the data. +struct ImGuiItemHoveredDataBackup +{ + ImGuiID LastItemId; + ImGuiItemStatusFlags LastItemStatusFlags; + ImRect LastItemRect; + ImRect LastItemDisplayRect; + + ImGuiItemHoveredDataBackup() { Backup(); } + void Backup() { ImGuiWindow* window = GImGui->CurrentWindow; LastItemId = window->DC.LastItemId; LastItemStatusFlags = window->DC.LastItemStatusFlags; LastItemRect = window->DC.LastItemRect; LastItemDisplayRect = window->DC.LastItemDisplayRect; } + void Restore() const { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.LastItemId = LastItemId; window->DC.LastItemStatusFlags = LastItemStatusFlags; window->DC.LastItemRect = LastItemRect; window->DC.LastItemDisplayRect = LastItemDisplayRect; } +}; + +//----------------------------------------------------------------------------- +// Tab bar, tab item +//----------------------------------------------------------------------------- + +// Extend ImGuiTabBarFlags_ +enum ImGuiTabBarFlagsPrivate_ +{ + ImGuiTabBarFlags_DockNode = 1 << 20, // Part of a dock node [we don't use this in the master branch but it facilitate branch syncing to keep this around] + ImGuiTabBarFlags_IsFocused = 1 << 21, + ImGuiTabBarFlags_SaveSettings = 1 << 22 // FIXME: Settings are handled by the docking system, this only request the tab bar to mark settings dirty when reordering tabs +}; + +// Extend ImGuiTabItemFlags_ +enum ImGuiTabItemFlagsPrivate_ +{ + ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Store whether p_open is set or not, which we need to recompute WidthContents during layout. +}; + +// Storage for one active tab item (sizeof() 26~32 bytes) +struct ImGuiTabItem +{ + ImGuiID ID; + ImGuiTabItemFlags Flags; + int LastFrameVisible; + int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance + int NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames + float Offset; // Position relative to beginning of tab + float Width; // Width currently displayed + float WidthContents; // Width of actual contents, stored during BeginTabItem() call + + ImGuiTabItem() { ID = Flags = 0; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = WidthContents = 0.0f; } +}; + +// Storage for a tab bar (sizeof() 92~96 bytes) +struct ImGuiTabBar +{ + ImVector Tabs; + ImGuiID ID; // Zero for tab-bars used by docking + ImGuiID SelectedTabId; // Selected tab + ImGuiID NextSelectedTabId; + ImGuiID VisibleTabId; // Can occasionally be != SelectedTabId (e.g. when previewing contents for CTRL+TAB preview) + int CurrFrameVisible; + int PrevFrameVisible; + ImRect BarRect; + float LastTabContentHeight; // Record the height of contents submitted below the tab bar + float OffsetMax; // Distance from BarRect.Min.x, locked during layout + float OffsetMaxIdeal; // Ideal offset if all tabs were visible and not clipped + float OffsetNextTab; // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set. + float ScrollingAnim; + float ScrollingTarget; + float ScrollingTargetDistToVisibility; + float ScrollingSpeed; + ImGuiTabBarFlags Flags; + ImGuiID ReorderRequestTabId; + ImS8 ReorderRequestDir; + bool WantLayout; + bool VisibleTabWasSubmitted; + short LastTabItemIdx; // For BeginTabItem()/EndTabItem() + ImVec2 FramePadding; // style.FramePadding locked at the time of BeginTabBar() + ImGuiTextBuffer TabsNames; // For non-docking tab bar we re-append names in a contiguous buffer. + + ImGuiTabBar(); + int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); } + const char* GetTabName(const ImGuiTabItem* tab) const + { + IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size); + return TabsNames.Buf.Data + tab->NameOffset; + } +}; + +//----------------------------------------------------------------------------- +// Internal API +// No guarantee of forward compatibility here. +//----------------------------------------------------------------------------- + +namespace ImGui +{ + // We should always have a CurrentWindow in the stack (there is an implicit "Debug" window) + // If this ever crash because g.CurrentWindow is NULL it means that either + // - ImGui::NewFrame() has never been called, which is illegal. + // - You are calling ImGui functions after ImGui::EndFrame()/ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal. + inline ImGuiWindow* GetCurrentWindowRead() { ImGuiContext& g = *GImGui; return g.CurrentWindow; } + inline ImGuiWindow* GetCurrentWindow() { ImGuiContext& g = *GImGui; g.CurrentWindow->WriteAccessed = true; return g.CurrentWindow; } + IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id); + IMGUI_API ImGuiWindow* FindWindowByName(const char* name); + IMGUI_API void FocusWindow(ImGuiWindow* window); + IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window); + IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); + IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); + IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); + IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); + IMGUI_API ImVec2 CalcWindowExpectedSize(ImGuiWindow* window); + IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); + IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); + IMGUI_API ImRect GetWindowAllowedExtentRect(ImGuiWindow* window); + IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); + IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); + IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); + IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); + IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); + + IMGUI_API void SetCurrentFont(ImFont* font); + inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; } + inline ImDrawList* GetForegroundDrawList(ImGuiWindow*) { ImGuiContext& g = *GImGui; return &g.ForegroundDrawList; } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. + + // Init + IMGUI_API void Initialize(ImGuiContext* context); + IMGUI_API void Shutdown(ImGuiContext* context); // Since 1.60 this is a _private_ function. You can call DestroyContext() to destroy the context created by CreateContext(). + + // NewFrame + IMGUI_API void UpdateHoveredWindowAndCaptureFlags(); + IMGUI_API void StartMouseMovingWindow(ImGuiWindow* window); + IMGUI_API void UpdateMouseMovingWindowNewFrame(); + IMGUI_API void UpdateMouseMovingWindowEndFrame(); + + // Settings + IMGUI_API void MarkIniSettingsDirty(); + IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); + IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); + IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); + IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name); + IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); + + // Scrolling + IMGUI_API void SetScrollX(ImGuiWindow* window, float new_scroll_x); + IMGUI_API void SetScrollY(ImGuiWindow* window, float new_scroll_y); + IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio = 0.5f); + IMGUI_API void SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio = 0.5f); + IMGUI_API ImVec2 ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect); + + // Basic Accessors + inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } + inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } + inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } + IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); + IMGUI_API void SetFocusID(ImGuiID id, ImGuiWindow* window); + IMGUI_API void ClearActiveID(); + IMGUI_API ImGuiID GetHoveredID(); + IMGUI_API void SetHoveredID(ImGuiID id); + IMGUI_API void KeepAliveID(ImGuiID id); + IMGUI_API void MarkItemEdited(ImGuiID id); + IMGUI_API void PushOverrideID(ImGuiID id); + + // Basic Helpers for widget code + IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = 0.0f); + IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = 0.0f); + IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL); + IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); + IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); + IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id); // Return true if focus is requested + IMGUI_API void FocusableItemUnregister(ImGuiWindow* window); + IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); + IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); + IMGUI_API void PushMultiItemsWidths(int components, float width_full); + IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); + IMGUI_API void PopItemFlag(); + IMGUI_API bool IsItemToggledSelection(); // Was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly) + IMGUI_API ImVec2 GetContentRegionMaxAbs(); + IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess); + + // Logging/Capture + IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. + IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer + + // Popups, Modals, Tooltips + IMGUI_API void OpenPopupEx(ImGuiID id); + IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); + IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); + IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id within current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack! + IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); + IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip = true); + IMGUI_API ImGuiWindow* GetTopMostPopupModal(); + IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); + IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy = ImGuiPopupPositionPolicy_Default); + + // Navigation + IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); + IMGUI_API bool NavMoveRequestButNoResultYet(); + IMGUI_API void NavMoveRequestCancel(); + IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags); + IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); + IMGUI_API float GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode); + IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); + IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); + IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. + IMGUI_API void SetNavID(ImGuiID id, int nav_layer); + IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel); + + // Inputs + // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. + inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } + inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; } + inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; } + IMGUI_API bool IsMouseDragPastThreshold(int button, float lock_threshold = -1.0f); + inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { ImGuiContext& g = *GImGui; const int key_index = g.IO.KeyMap[key]; return (key_index >= 0) ? IsKeyPressed(key_index, repeat) : false; } + inline bool IsNavInputDown(ImGuiNavInput n) { ImGuiContext& g = *GImGui; return g.IO.NavInputs[n] > 0.0f; } + inline bool IsNavInputPressed(ImGuiNavInput n, ImGuiInputReadMode mode) { return GetNavInputAmount(n, mode) > 0.0f; } + inline bool IsNavInputPressedAnyOfTwo(ImGuiNavInput n1, ImGuiNavInput n2, ImGuiInputReadMode mode) { return (GetNavInputAmount(n1, mode) + GetNavInputAmount(n2, mode)) > 0.0f; } + + // Drag and Drop + IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); + IMGUI_API void ClearDragDrop(); + IMGUI_API bool IsDragDropPayloadBeingAccepted(); + + // New Columns API (FIXME-WIP) + IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). + IMGUI_API void EndColumns(); // close columns + IMGUI_API void PushColumnClipRect(int column_index); + IMGUI_API void PushColumnsBackground(); + IMGUI_API void PopColumnsBackground(); + IMGUI_API ImGuiID GetColumnsID(const char* str_id, int count); + IMGUI_API ImGuiColumns* FindOrCreateColumns(ImGuiWindow* window, ImGuiID id); + IMGUI_API float GetColumnOffsetFromNorm(const ImGuiColumns* columns, float offset_norm); + IMGUI_API float GetColumnNormFromOffset(const ImGuiColumns* columns, float offset); + + // Tab Bars + IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags); + IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); + IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); + IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); + IMGUI_API void TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir); + IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); + IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); + IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); + IMGUI_API bool TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id); + + // Render helpers + // AVOID USING OUTSIDE OF IMGUI.CPP! NOT FOR PUBLIC CONSUMPTION. THOSE FUNCTIONS ARE A MESS. THEIR SIGNATURE AND BEHAVIOR WILL CHANGE, THEY NEED TO BE REFACTORED INTO SOMETHING DECENT. + // NB: All position are in absolute pixels coordinates (we are never using window coordinates internally) + IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true); + IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width); + IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0,0), const ImRect* clip_rect = NULL); + IMGUI_API void RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL); + IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known); + IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); + IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); + IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0); + IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col, float sz); + IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight + IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. + IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); + + // Render helpers (those functions don't access any ImGui state!) + IMGUI_API void RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale = 1.0f); + IMGUI_API void RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col); + IMGUI_API void RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); + IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col); + IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding); + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // [1.71: 2019/06/07: Updating prototypes of some of the internal functions. Leaving those for reference for a short while] + inline void RenderArrow(ImVec2 pos, ImGuiDir dir, float scale=1.0f) { ImGuiWindow* window = GetCurrentWindow(); RenderArrow(window->DrawList, pos, GetColorU32(ImGuiCol_Text), dir, scale); } + inline void RenderBullet(ImVec2 pos) { ImGuiWindow* window = GetCurrentWindow(); RenderBullet(window->DrawList, pos, GetColorU32(ImGuiCol_Text)); } +#endif + + // Widgets + IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); + IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0); + IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos); + IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); + IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags); + IMGUI_API void Scrollbar(ImGuiAxis axis); + IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawCornerFlags rounding_corners); + IMGUI_API ImGuiID GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis); + IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); + + // Widgets low-level behaviors + IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); + IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, float power, ImGuiDragFlags flags); + IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb); + IMGUI_API bool SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f); + IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL); + IMGUI_API bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextItemOpen() data, if any. May return true when logging + IMGUI_API void TreePushOverrideID(ImGuiID id); + + // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. + // To use them externally (for custom widget) you may need an "extern template" statement in your code in order to link to existing instances and silence Clang warnings (see #2036). + // e.g. " extern template IMGUI_API float RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, float v); " + template IMGUI_API bool DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, float power, ImGuiDragFlags flags); + template IMGUI_API bool SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb); + template IMGUI_API float SliderCalcRatioFromValueT(ImGuiDataType data_type, T v, T v_min, T v_max, float power, float linear_zero_pos); + template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); + + // Data type helpers + IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); + IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format); + IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg_1, const void* arg_2); + IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format); + + // InputText + IMGUI_API bool InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format); + inline bool TempInputTextIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputTextId == id); } + + // Color + IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags); + IMGUI_API void ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags); + IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags); + + // Plot + IMGUI_API void PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size); + + // Shade functions (write over already created vertices) + IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1); + IMGUI_API void ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp); + + // Debug Tools + inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max, col); } + inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } + +} // namespace ImGui + +// ImFontAtlas internals +IMGUI_API bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); +IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); +IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); +IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); + +// Debug Tools +// Use 'Metrics->Tools->Item Picker' to break into the call-stack of a specific item. +#ifndef IM_DEBUG_BREAK +#if defined(__clang__) +#define IM_DEBUG_BREAK() __builtin_debugtrap() +#elif defined (_MSC_VER) +#define IM_DEBUG_BREAK() __debugbreak() +#else +#define IM_DEBUG_BREAK() IM_ASSERT(0) // It is expected that you define IM_DEBUG_BREAK() into something that will break nicely in a debugger! +#endif +#endif // #ifndef IM_DEBUG_BREAK + +// Test Engine Hooks (imgui_tests) +//#define IMGUI_ENABLE_TEST_ENGINE +#ifdef IMGUI_ENABLE_TEST_ENGINE +extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx); +extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx); +extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); +extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); +extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); +#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) +#define IMGUI_TEST_ENGINE_LOG(_FMT, ...) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log +#else +#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) do { } while (0) +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) do { } while (0) +#define IMGUI_TEST_ENGINE_LOG(_FMT, ...) do { } while (0) +#endif + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + +#ifdef _MSC_VER +#pragma warning (pop) +#endif diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp new file mode 100644 index 00000000..b2398a00 --- /dev/null +++ b/imgui/imgui_widgets.cpp @@ -0,0 +1,7613 @@ +// dear imgui, v1.74 WIP +// (widgets code) + +/* + +Index of this file: + +// [SECTION] Forward Declarations +// [SECTION] Widgets: Text, etc. +// [SECTION] Widgets: Main (Button, Image, Checkbox, RadioButton, ProgressBar, Bullet, etc.) +// [SECTION] Widgets: Low-level Layout helpers (Spacing, Dummy, NewLine, Separator, etc.) +// [SECTION] Widgets: ComboBox +// [SECTION] Data Type and Data Formatting Helpers +// [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. +// [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. +// [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. +// [SECTION] Widgets: InputText, InputTextMultiline +// [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. +// [SECTION] Widgets: TreeNode, CollapsingHeader, etc. +// [SECTION] Widgets: Selectable +// [SECTION] Widgets: ListBox +// [SECTION] Widgets: PlotLines, PlotHistogram +// [SECTION] Widgets: Value helpers +// [SECTION] Widgets: MenuItem, BeginMenu, EndMenu, etc. +// [SECTION] Widgets: BeginTabBar, EndTabBar, etc. +// [SECTION] Widgets: BeginTabItem, EndTabItem, etc. +// [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc. + +*/ + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#ifndef IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_MATH_OPERATORS +#endif +#include "imgui_internal.h" + +#include // toupper +#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier +#include // intptr_t +#else +#include // intptr_t +#endif + +// Visual Studio warnings +#ifdef _MSC_VER +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#endif + +// Clang/GCC warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. +#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 +#endif +#if __has_warning("-Wdouble-promotion") +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#endif +#elif defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +//------------------------------------------------------------------------- +// Data +//------------------------------------------------------------------------- + +// Those MIN/MAX values are not define because we need to point to them +static const signed char IM_S8_MIN = -128; +static const signed char IM_S8_MAX = 127; +static const unsigned char IM_U8_MIN = 0; +static const unsigned char IM_U8_MAX = 0xFF; +static const signed short IM_S16_MIN = -32768; +static const signed short IM_S16_MAX = 32767; +static const unsigned short IM_U16_MIN = 0; +static const unsigned short IM_U16_MAX = 0xFFFF; +static const ImS32 IM_S32_MIN = INT_MIN; // (-2147483647 - 1), (0x80000000); +static const ImS32 IM_S32_MAX = INT_MAX; // (2147483647), (0x7FFFFFFF) +static const ImU32 IM_U32_MIN = 0; +static const ImU32 IM_U32_MAX = UINT_MAX; // (0xFFFFFFFF) +#ifdef LLONG_MIN +static const ImS64 IM_S64_MIN = LLONG_MIN; // (-9223372036854775807ll - 1ll); +static const ImS64 IM_S64_MAX = LLONG_MAX; // (9223372036854775807ll); +#else +static const ImS64 IM_S64_MIN = -9223372036854775807LL - 1; +static const ImS64 IM_S64_MAX = 9223372036854775807LL; +#endif +static const ImU64 IM_U64_MIN = 0; +#ifdef ULLONG_MAX +static const ImU64 IM_U64_MAX = ULLONG_MAX; // (0xFFFFFFFFFFFFFFFFull); +#else +static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); +#endif + +//------------------------------------------------------------------------- +// [SECTION] Forward Declarations +//------------------------------------------------------------------------- + +// For InputTextEx() +static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data); +static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); +static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Text, etc. +//------------------------------------------------------------------------- +// - TextEx() [Internal] +// - TextUnformatted() +// - Text() +// - TextV() +// - TextColored() +// - TextColoredV() +// - TextDisabled() +// - TextDisabledV() +// - TextWrapped() +// - TextWrappedV() +// - LabelText() +// - LabelTextV() +// - BulletText() +// - BulletTextV() +//------------------------------------------------------------------------- + +void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + IM_ASSERT(text != NULL); + const char* text_begin = text; + if (text_end == NULL) + text_end = text + strlen(text); // FIXME-OPT + + const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); + const float wrap_pos_x = window->DC.TextWrapPos; + const bool wrap_enabled = (wrap_pos_x >= 0.0f); + if (text_end - text > 2000 && !wrap_enabled) + { + // Long text! + // Perform manual coarse clipping to optimize for long multi-line text + // - From this point we will only compute the width of lines that are visible. Optimization only available when word-wrapping is disabled. + // - We also don't vertically center the text within the line full height, which is unlikely to matter because we are likely the biggest and only item on the line. + // - We use memchr(), pay attention that well optimized versions of those str/mem functions are much faster than a casually written loop. + const char* line = text; + const float line_height = GetTextLineHeight(); + ImVec2 text_size(0,0); + + // Lines to skip (can't skip when logging text) + ImVec2 pos = text_pos; + if (!g.LogEnabled) + { + int lines_skippable = (int)((window->ClipRect.Min.y - text_pos.y) / line_height); + if (lines_skippable > 0) + { + int lines_skipped = 0; + while (line < text_end && lines_skipped < lines_skippable) + { + const char* line_end = (const char*)memchr(line, '\n', text_end - line); + if (!line_end) + line_end = text_end; + if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) + text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); + line = line_end + 1; + lines_skipped++; + } + pos.y += lines_skipped * line_height; + } + } + + // Lines to render + if (line < text_end) + { + ImRect line_rect(pos, pos + ImVec2(FLT_MAX, line_height)); + while (line < text_end) + { + if (IsClippedEx(line_rect, 0, false)) + break; + + const char* line_end = (const char*)memchr(line, '\n', text_end - line); + if (!line_end) + line_end = text_end; + text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); + RenderText(pos, line, line_end, false); + line = line_end + 1; + line_rect.Min.y += line_height; + line_rect.Max.y += line_height; + pos.y += line_height; + } + + // Count remaining lines + int lines_skipped = 0; + while (line < text_end) + { + const char* line_end = (const char*)memchr(line, '\n', text_end - line); + if (!line_end) + line_end = text_end; + if ((flags & ImGuiTextFlags_NoWidthForLargeClippedText) == 0) + text_size.x = ImMax(text_size.x, CalcTextSize(line, line_end).x); + line = line_end + 1; + lines_skipped++; + } + pos.y += lines_skipped * line_height; + } + text_size.y = (pos - text_pos).y; + + ImRect bb(text_pos, text_pos + text_size); + ItemSize(text_size); + ItemAdd(bb, 0); + } + else + { + const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f; + const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); + + ImRect bb(text_pos, text_pos + text_size); + ItemSize(text_size); + if (!ItemAdd(bb, 0)) + return; + + // Render (we don't hide text after ## in this end-user function) + RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width); + } +} + +void ImGui::TextUnformatted(const char* text, const char* text_end) +{ + TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); +} + +void ImGui::Text(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + TextV(fmt, args); + va_end(args); +} + +void ImGui::TextV(const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); +} + +void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + TextColoredV(col, fmt, args); + va_end(args); +} + +void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args) +{ + PushStyleColor(ImGuiCol_Text, col); + TextV(fmt, args); + PopStyleColor(); +} + +void ImGui::TextDisabled(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + TextDisabledV(fmt, args); + va_end(args); +} + +void ImGui::TextDisabledV(const char* fmt, va_list args) +{ + PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]); + TextV(fmt, args); + PopStyleColor(); +} + +void ImGui::TextWrapped(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + TextWrappedV(fmt, args); + va_end(args); +} + +void ImGui::TextWrappedV(const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + bool need_backup = (window->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set + if (need_backup) + PushTextWrapPos(0.0f); + TextV(fmt, args); + if (need_backup) + PopTextWrapPos(); +} + +void ImGui::LabelText(const char* label, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + LabelTextV(label, fmt, args); + va_end(args); +} + +// Add a label+text combo aligned to other label+value widgets +void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const float w = CalcItemWidth(); + + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2)); + const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y*2) + label_size); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, 0)) + return; + + // Render + const char* value_text_begin = &g.TempBuffer[0]; + const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f,0.5f)); + if (label_size.x > 0.0f) + RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label); +} + +void ImGui::BulletText(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + BulletTextV(fmt, args); + va_end(args); +} + +// Text with a little bullet aligned to the typical tree node. +void ImGui::BulletTextV(const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + const char* text_begin = g.TempBuffer; + const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); + const float text_base_offset_y = ImMax(0.0f, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it + const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x*2) : 0.0f), ImMax(line_height, label_size.y))); // Empty text doesn't add padding + ItemSize(bb); + if (!ItemAdd(bb, 0)) + return; + + // Render + ImU32 text_col = GetColorU32(ImGuiCol_Text); + RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), text_col); + RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2, text_base_offset_y), text_begin, text_end, false); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Main +//------------------------------------------------------------------------- +// - ButtonBehavior() [Internal] +// - Button() +// - SmallButton() +// - InvisibleButton() +// - ArrowButton() +// - CloseButton() [Internal] +// - CollapseButton() [Internal] +// - ScrollbarEx() [Internal] +// - Scrollbar() [Internal] +// - Image() +// - ImageButton() +// - Checkbox() +// - CheckboxFlags() +// - RadioButton() +// - ProgressBar() +// - Bullet() +//------------------------------------------------------------------------- + +// The ButtonBehavior() function is key to many interactions and used by many/most widgets. +// Because we handle so many cases (keyboard/gamepad navigation, drag and drop) and many specific behavior (via ImGuiButtonFlags_), +// this code is a little complex. +// By far the most common path is interacting with the Mouse using the default ImGuiButtonFlags_PressedOnClickRelease button behavior. +// See the series of events below and the corresponding state reported by dear imgui: +//------------------------------------------------------------------------------------------------------------------------------------------------ +// with PressedOnClickRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() +// Frame N+0 (mouse is outside bb) - - - - - - +// Frame N+1 (mouse moves inside bb) - true - - - - +// Frame N+2 (mouse button is down) - true true true - true +// Frame N+3 (mouse button is down) - true true - - - +// Frame N+4 (mouse moves outside bb) - - true - - - +// Frame N+5 (mouse moves inside bb) - true true - - - +// Frame N+6 (mouse button is released) true true - - true - +// Frame N+7 (mouse button is released) - true - - - - +// Frame N+8 (mouse moves outside bb) - - - - - - +//------------------------------------------------------------------------------------------------------------------------------------------------ +// with PressedOnClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() +// Frame N+2 (mouse button is down) true true true true - true +// Frame N+3 (mouse button is down) - true true - - - +// Frame N+6 (mouse button is released) - true - - true - +// Frame N+7 (mouse button is released) - true - - - - +//------------------------------------------------------------------------------------------------------------------------------------------------ +// with PressedOnRelease: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() +// Frame N+2 (mouse button is down) - true - - - true +// Frame N+3 (mouse button is down) - true - - - - +// Frame N+6 (mouse button is released) true true - - - - +// Frame N+7 (mouse button is released) - true - - - - +//------------------------------------------------------------------------------------------------------------------------------------------------ +// with PressedOnDoubleClick: return-value IsItemHovered() IsItemActive() IsItemActivated() IsItemDeactivated() IsItemClicked() +// Frame N+0 (mouse button is down) - true - - - true +// Frame N+1 (mouse button is down) - true - - - - +// Frame N+2 (mouse button is released) - true - - - - +// Frame N+3 (mouse button is released) - true - - - - +// Frame N+4 (mouse button is down) true true true true - true +// Frame N+5 (mouse button is down) - true true - - - +// Frame N+6 (mouse button is released) - true - - true - +// Frame N+7 (mouse button is released) - true - - - - +//------------------------------------------------------------------------------------------------------------------------------------------------ +// Note that some combinations are supported, +// - PressedOnDragDropHold can generally be associated with any flag. +// - PressedOnDoubleClick can be associated by PressedOnClickRelease/PressedOnRelease, in which case the second release event won't be reported. +//------------------------------------------------------------------------------------------------------------------------------------------------ +// The behavior of the return-value changes when ImGuiButtonFlags_Repeat is set: +// Repeat+ Repeat+ Repeat+ Repeat+ +// PressedOnClickRelease PressedOnClick PressedOnRelease PressedOnDoubleClick +//------------------------------------------------------------------------------------------------------------------------------------------------- +// Frame N+0 (mouse button is down) - true - true +// ... - - - - +// Frame N + RepeatDelay true true - true +// ... - - - - +// Frame N + RepeatDelay + RepeatRate*N true true - true +//------------------------------------------------------------------------------------------------------------------------------------------------- + +bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + + if (flags & ImGuiButtonFlags_Disabled) + { + if (out_hovered) *out_hovered = false; + if (out_held) *out_held = false; + if (g.ActiveId == id) ClearActiveID(); + return false; + } + + // Default behavior requires click+release on same spot + if ((flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick)) == 0) + flags |= ImGuiButtonFlags_PressedOnClickRelease; + + ImGuiWindow* backup_hovered_window = g.HoveredWindow; + const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window; + if (flatten_hovered_children) + g.HoveredWindow = window; + +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (id != 0 && window->DC.LastItemId != id) + ImGuiTestEngineHook_ItemAdd(&g, bb, id); +#endif + + bool pressed = false; + bool hovered = ItemHoverable(bb, id); + + // Drag source doesn't report as hovered + if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover)) + hovered = false; + + // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button + if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) + if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) + { + hovered = true; + SetHoveredID(id); + if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, 0.70f, 0.00f)) + { + pressed = true; + FocusWindow(window); + } + } + + if (flatten_hovered_children) + g.HoveredWindow = backup_hovered_window; + + // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. This allows using patterns where a later submitted widget overlaps a previous one. + if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0)) + hovered = false; + + // Mouse + if (hovered) + { + if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) + { + if ((flags & ImGuiButtonFlags_PressedOnClickRelease) && g.IO.MouseClicked[0]) + { + SetActiveID(id, window); + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); + FocusWindow(window); + } + if (((flags & ImGuiButtonFlags_PressedOnClick) && g.IO.MouseClicked[0]) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[0])) + { + pressed = true; + if (flags & ImGuiButtonFlags_NoHoldingActiveID) + ClearActiveID(); + else + SetActiveID(id, window); // Hold on ID + FocusWindow(window); + } + if ((flags & ImGuiButtonFlags_PressedOnRelease) && g.IO.MouseReleased[0]) + { + if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay)) // Repeat mode trumps + pressed = true; + ClearActiveID(); + } + + // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). + // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. + if ((flags & ImGuiButtonFlags_Repeat) && g.ActiveId == id && g.IO.MouseDownDuration[0] > 0.0f && IsMouseClicked(0, true)) + pressed = true; + } + + if (pressed) + g.NavDisableHighlight = true; + } + + // Gamepad/Keyboard navigation + // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse. + if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId)) + if (!(flags & ImGuiButtonFlags_NoHoveredOnNav)) + hovered = true; + + if (g.NavActivateDownId == id) + { + bool nav_activated_by_code = (g.NavActivateId == id); + bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); + if (nav_activated_by_code || nav_activated_by_inputs) + pressed = true; + if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) + { + // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. + g.NavActivateId = id; // This is so SetActiveId assign a Nav source + SetActiveID(id, window); + if ((nav_activated_by_code || nav_activated_by_inputs) && !(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); + } + } + + bool held = false; + if (g.ActiveId == id) + { + if (pressed) + g.ActiveIdHasBeenPressedBefore = true; + if (g.ActiveIdSource == ImGuiInputSource_Mouse) + { + if (g.ActiveIdIsJustActivated) + g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; + if (g.IO.MouseDown[0]) + { + held = true; + } + else + { + if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease) && !g.DragDropActive) + { + bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[0]; + bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay; // Repeat mode trumps + if (!is_double_click_release && !is_repeating_already) + pressed = true; + } + ClearActiveID(); + } + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + g.NavDisableHighlight = true; + } + else if (g.ActiveIdSource == ImGuiInputSource_Nav) + { + if (g.NavActivateDownId != id) + ClearActiveID(); + } + } + + if (out_hovered) *out_hovered = hovered; + if (out_held) *out_held = held; + + return pressed; +} + +bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + + ImVec2 pos = window->DC.CursorPos; + if ((flags & ImGuiButtonFlags_AlignTextBaseLine) && style.FramePadding.y < window->DC.CurrLineTextBaseOffset) // Try to vertically align buttons that are smaller/have no padding so that text baseline matches (bit hacky, since it shouldn't be a flag) + pos.y += window->DC.CurrLineTextBaseOffset - style.FramePadding.y; + ImVec2 size = CalcItemSize(size_arg, label_size.x + style.FramePadding.x * 2.0f, label_size.y + style.FramePadding.y * 2.0f); + + const ImRect bb(pos, pos + size); + ItemSize(size, style.FramePadding.y); + if (!ItemAdd(bb, id)) + return false; + + if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) + flags |= ImGuiButtonFlags_Repeat; + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); + + // Render + const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + RenderNavHighlight(bb, id); + RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); + RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb); + + // Automatically close popups + //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup)) + // CloseCurrentPopup(); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); + return pressed; +} + +bool ImGui::Button(const char* label, const ImVec2& size_arg) +{ + return ButtonEx(label, size_arg, 0); +} + +// Small buttons fits within text without additional vertical spacing. +bool ImGui::SmallButton(const char* label) +{ + ImGuiContext& g = *GImGui; + float backup_padding_y = g.Style.FramePadding.y; + g.Style.FramePadding.y = 0.0f; + bool pressed = ButtonEx(label, ImVec2(0, 0), ImGuiButtonFlags_AlignTextBaseLine); + g.Style.FramePadding.y = backup_padding_y; + return pressed; +} + +// Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack. +// Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id) +bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + // Cannot use zero-size for InvisibleButton(). Unlike Button() there is not way to fallback using the label size. + IM_ASSERT(size_arg.x != 0.0f && size_arg.y != 0.0f); + + const ImGuiID id = window->GetID(str_id); + ImVec2 size = CalcItemSize(size_arg, 0.0f, 0.0f); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + ItemSize(size); + if (!ItemAdd(bb, id)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + + return pressed; +} + +bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiID id = window->GetID(str_id); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + const float default_size = GetFrameHeight(); + ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); + if (!ItemAdd(bb, id)) + return false; + + if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) + flags |= ImGuiButtonFlags_Repeat; + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); + + // Render + const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + const ImU32 text_col = GetColorU32(ImGuiCol_Text); + RenderNavHighlight(bb, id); + RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding); + RenderArrow(window->DrawList, bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), text_col, dir); + + return pressed; +} + +bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) +{ + float sz = GetFrameHeight(); + return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), 0); +} + +// Button to close a window +bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)//, float size) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window. + // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). + const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); + bool is_clipped = !ItemAdd(bb, id); + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + if (is_clipped) + return pressed; + + // Render + ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); + ImVec2 center = bb.GetCenter(); + if (hovered) + window->DrawList->AddCircleFilled(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col, 12); + + float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; + ImU32 cross_col = GetColorU32(ImGuiCol_Text); + center -= ImVec2(0.5f, 0.5f); + window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), cross_col, 1.0f); + window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), cross_col, 1.0f); + + return pressed; +} + +bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); + ItemAdd(bb, id); + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None); + + // Render + ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + ImU32 text_col = GetColorU32(ImGuiCol_Text); + ImVec2 center = bb.GetCenter(); + if (hovered || held) + window->DrawList->AddCircleFilled(center/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col, 12); + RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); + + // Switch to moving the window after mouse is moved beyond the initial drag threshold + if (IsItemActive() && IsMouseDragging()) + StartMouseMovingWindow(window); + + return pressed; +} + +ImGuiID ImGui::GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis) +{ + return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); +} + +// Vertical/Horizontal scrollbar +// The entire piece of code below is rather confusing because: +// - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab) +// - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar +// - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. +// Still, the code should probably be made simpler.. +bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float size_avail_v, float size_contents_v, ImDrawCornerFlags rounding_corners) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + const float bb_frame_width = bb_frame.GetWidth(); + const float bb_frame_height = bb_frame.GetHeight(); + if (bb_frame_width <= 0.0f || bb_frame_height <= 0.0f) + return false; + + // When we are too small, start hiding and disabling the grab (this reduce visual noise on very small window and facilitate using the resize grab) + float alpha = 1.0f; + if ((axis == ImGuiAxis_Y) && bb_frame_height < g.FontSize + g.Style.FramePadding.y * 2.0f) + alpha = ImSaturate((bb_frame_height - g.FontSize) / (g.Style.FramePadding.y * 2.0f)); + if (alpha <= 0.0f) + return false; + + const ImGuiStyle& style = g.Style; + const bool allow_interaction = (alpha >= 1.0f); + const bool horizontal = (axis == ImGuiAxis_X); + + ImRect bb = bb_frame; + bb.Expand(ImVec2(-ImClamp((float)(int)((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp((float)(int)((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); + + // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) + const float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight(); + + // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) + // But we maintain a minimum size in pixel to allow for the user to still aim inside. + IM_ASSERT(ImMax(size_contents_v, size_avail_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers. + const float win_size_v = ImMax(ImMax(size_contents_v, size_avail_v), 1.0f); + const float grab_h_pixels = ImClamp(scrollbar_size_v * (size_avail_v / win_size_v), style.GrabMinSize, scrollbar_size_v); + const float grab_h_norm = grab_h_pixels / scrollbar_size_v; + + // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). + bool held = false; + bool hovered = false; + ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); + + float scroll_max = ImMax(1.0f, size_contents_v - size_avail_v); + float scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); + float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; + if (held && allow_interaction && grab_h_norm < 1.0f) + { + float scrollbar_pos_v = horizontal ? bb.Min.x : bb.Min.y; + float mouse_pos_v = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y; + + // Click position in scrollbar normalized space (0.0f->1.0f) + const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); + SetHoveredID(id); + + bool seek_absolute = false; + if (g.ActiveIdIsJustActivated) + { + // On initial click calculate the distance between mouse and the center of the grab + seek_absolute = (clicked_v_norm < grab_v_norm || clicked_v_norm > grab_v_norm + grab_h_norm); + if (seek_absolute) + g.ScrollbarClickDeltaToGrabCenter = 0.0f; + else + g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; + } + + // Apply scroll + // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position + const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); + *p_scroll_v = (float)(int)(0.5f + scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); + + // Update values for rendering + scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); + grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; + + // Update distance to grab now that we have seeked and saturated + if (seek_absolute) + g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; + } + + // Render + window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, GetColorU32(ImGuiCol_ScrollbarBg), window->WindowRounding, rounding_corners); + const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha); + ImRect grab_rect; + if (horizontal) + grab_rect = ImRect(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm), bb.Min.y, ImLerp(bb.Min.x, bb.Max.x, grab_v_norm) + grab_h_pixels, bb.Max.y); + else + grab_rect = ImRect(bb.Min.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm), bb.Max.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm) + grab_h_pixels); + window->DrawList->AddRectFilled(grab_rect.Min, grab_rect.Max, grab_col, style.ScrollbarRounding); + + return held; +} + +void ImGui::Scrollbar(ImGuiAxis axis) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + const ImGuiID id = GetScrollbarID(window, axis); + KeepAliveID(id); + + // Calculate scrollbar bounding box + const ImRect outer_rect = window->Rect(); + const ImRect inner_rect = window->InnerRect; + const float border_size = window->WindowBorderSize; + const float scrollbar_size = window->ScrollbarSizes[axis ^ 1]; + IM_ASSERT(scrollbar_size > 0.0f); + const float other_scrollbar_size = window->ScrollbarSizes[axis]; + ImDrawCornerFlags rounding_corners = (other_scrollbar_size <= 0.0f) ? ImDrawCornerFlags_BotRight : 0; + ImRect bb; + if (axis == ImGuiAxis_X) + { + bb.Min = ImVec2(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size)); + bb.Max = ImVec2(inner_rect.Max.x, outer_rect.Max.y); + rounding_corners |= ImDrawCornerFlags_BotLeft; + } + else + { + bb.Min = ImVec2(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y); + bb.Max = ImVec2(outer_rect.Max.x, window->InnerRect.Max.y); + rounding_corners |= ((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) ? ImDrawCornerFlags_TopRight : 0; + } + ScrollbarEx(bb, id, axis, &window->Scroll[axis], inner_rect.Max[axis] - inner_rect.Min[axis], window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f, rounding_corners); +} + +void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + if (border_col.w > 0.0f) + bb.Max += ImVec2(2, 2); + ItemSize(bb); + if (!ItemAdd(bb, 0)) + return; + + if (border_col.w > 0.0f) + { + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f); + window->DrawList->AddImage(user_texture_id, bb.Min + ImVec2(1, 1), bb.Max - ImVec2(1, 1), uv0, uv1, GetColorU32(tint_col)); + } + else + { + window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col)); + } +} + +// frame_padding < 0: uses FramePadding from style (default) +// frame_padding = 0: no framing +// frame_padding > 0: set framing size +// The color used are the button colors. +bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + // Default to using texture ID as ID. User can still push string/integer prefixes. + // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV. + PushID((void*)(intptr_t)user_texture_id); + const ImGuiID id = window->GetID("#image"); + PopID(); + + const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding; + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2); + const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size); + ItemSize(bb); + if (!ItemAdd(bb, id)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + + // Render + const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + RenderNavHighlight(bb, id); + RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding)); + if (bg_col.w > 0.0f) + window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col)); + window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col)); + + return pressed; +} + +bool ImGui::Checkbox(const char* label, bool* v) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + + const float square_sz = GetFrameHeight(); + const ImVec2 pos = window->DC.CursorPos; + const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); + if (pressed) + { + *v = !(*v); + MarkItemEdited(id); + } + + const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); + RenderNavHighlight(total_bb, id); + RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); + ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); + if (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) + { + // Undocumented tristate/mixed/indeterminate checkbox (#2644) + ImVec2 pad(ImMax(1.0f, (float)(int)(square_sz / 3.6f)), ImMax(1.0f, (float)(int)(square_sz / 3.6f))); + window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); + } + else if (*v) + { + const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); + RenderCheckMark(check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); + } + + if (g.LogEnabled) + LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]"); + if (label_size.x > 0.0f) + RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); + return pressed; +} + +bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value) +{ + bool v = ((*flags & flags_value) == flags_value); + bool pressed = Checkbox(label, &v); + if (pressed) + { + if (v) + *flags |= flags_value; + else + *flags &= ~flags_value; + } + + return pressed; +} + +bool ImGui::RadioButton(const char* label, bool active) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + + const float square_sz = GetFrameHeight(); + const ImVec2 pos = window->DC.CursorPos; + const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); + const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id)) + return false; + + ImVec2 center = check_bb.GetCenter(); + center.x = (float)(int)center.x + 0.5f; + center.y = (float)(int)center.y + 0.5f; + const float radius = (square_sz - 1.0f) * 0.5f; + + bool hovered, held; + bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); + if (pressed) + MarkItemEdited(id); + + RenderNavHighlight(total_bb, id); + window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16); + if (active) + { + const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); + window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16); + } + + if (style.FrameBorderSize > 0.0f) + { + window->DrawList->AddCircle(center + ImVec2(1,1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); + window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); + } + + if (g.LogEnabled) + LogRenderedText(&total_bb.Min, active ? "(x)" : "( )"); + if (label_size.x > 0.0f) + RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); + + return pressed; +} + +// FIXME: This would work nicely if it was a public template, e.g. 'template RadioButton(const char* label, T* v, T v_button)', but I'm not sure how we would expose it.. +bool ImGui::RadioButton(const char* label, int* v, int v_button) +{ + const bool pressed = RadioButton(label, *v == v_button); + if (pressed) + *v = v_button; + return pressed; +} + +// size_arg (for each axis) < 0.0f: align to end, 0.0f: auto, > 0.0f: specified size +void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* overlay) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + ImVec2 pos = window->DC.CursorPos; + ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), g.FontSize + style.FramePadding.y*2.0f); + ImRect bb(pos, pos + size); + ItemSize(size, style.FramePadding.y); + if (!ItemAdd(bb, 0)) + return; + + // Render + fraction = ImSaturate(fraction); + RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); + bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize)); + const ImVec2 fill_br = ImVec2(ImLerp(bb.Min.x, bb.Max.x, fraction), bb.Max.y); + RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); + + // Default displaying the fraction as percentage string, but user can override it + char overlay_buf[32]; + if (!overlay) + { + ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction*100+0.01f); + overlay = overlay_buf; + } + + ImVec2 overlay_size = CalcTextSize(overlay, NULL); + if (overlay_size.x > 0.0f) + RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f,0.5f), &bb); +} + +void ImGui::Bullet() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height)); + ItemSize(bb); + if (!ItemAdd(bb, 0)) + { + SameLine(0, style.FramePadding.x*2); + return; + } + + // Render and stay on same line + ImU32 text_col = GetColorU32(ImGuiCol_Text); + RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), text_col); + SameLine(0, style.FramePadding.x * 2.0f); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Low-level Layout helpers +//------------------------------------------------------------------------- +// - Spacing() +// - Dummy() +// - NewLine() +// - AlignTextToFramePadding() +// - SeparatorEx() [Internal] +// - Separator() +// - SplitterBehavior() [Internal] +// - ShrinkWidths() [Internal] +//------------------------------------------------------------------------- + +void ImGui::Spacing() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + ItemSize(ImVec2(0,0)); +} + +void ImGui::Dummy(const ImVec2& size) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + ItemSize(size); + ItemAdd(bb, 0); +} + +void ImGui::NewLine() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiLayoutType backup_layout_type = window->DC.LayoutType; + window->DC.LayoutType = ImGuiLayoutType_Vertical; + if (window->DC.CurrLineSize.y > 0.0f) // In the event that we are on a line with items that is smaller that FontSize high, we will preserve its height. + ItemSize(ImVec2(0,0)); + else + ItemSize(ImVec2(0.0f, g.FontSize)); + window->DC.LayoutType = backup_layout_type; +} + +void ImGui::AlignTextToFramePadding() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + window->DC.CurrLineSize.y = ImMax(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y * 2); + window->DC.CurrLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, g.Style.FramePadding.y); +} + +// Horizontal/vertical separating line +void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical))); // Check that only 1 option is selected + + float thickness_draw = 1.0f; + float thickness_layout = 0.0f; + if (flags & ImGuiSeparatorFlags_Vertical) + { + // Vertical separator, for menu bars (use current line height). Not exposed because it is misleading and it doesn't have an effect on regular layout. + float y1 = window->DC.CursorPos.y; + float y2 = window->DC.CursorPos.y + window->DC.CurrLineSize.y; + const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + thickness_draw, y2)); + ItemSize(ImVec2(thickness_layout, 0.0f)); + if (!ItemAdd(bb, 0)) + return; + + // Draw + window->DrawList->AddLine(ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Min.x, bb.Max.y), GetColorU32(ImGuiCol_Separator)); + if (g.LogEnabled) + LogText(" |"); + } + else if (flags & ImGuiSeparatorFlags_Horizontal) + { + // Horizontal Separator + float x1 = window->Pos.x; + float x2 = window->Pos.x + window->Size.x; + if (!window->DC.GroupStack.empty()) + x1 += window->DC.Indent.x; + + ImGuiColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; + if (columns) + PushColumnsBackground(); + + // We don't provide our width to the layout so that it doesn't get feed back into AutoFit + const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness_draw)); + ItemSize(ImVec2(0.0f, thickness_layout)); + if (!ItemAdd(bb, 0)) + { + if (columns) + { + PopColumnsBackground(); + columns->LineMinY = window->DC.CursorPos.y; + } + return; + } + + // Draw + window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); + if (g.LogEnabled) + LogRenderedText(&bb.Min, "--------------------------------"); + + if (columns) + { + PopColumnsBackground(); + columns->LineMinY = window->DC.CursorPos.y; + } + } +} + +void ImGui::Separator() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + // Those flags should eventually be overridable by the user + ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; + flags |= ImGuiSeparatorFlags_SpanAllColumns; + SeparatorEx(flags); +} + +// Using 'hover_visibility_delay' allows us to hide the highlight and mouse cursor for a short time, which can be convenient to reduce visual noise. +bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; + window->DC.ItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; + bool item_add = ItemAdd(bb, id); + window->DC.ItemFlags = item_flags_backup; + if (!item_add) + return false; + + bool hovered, held; + ImRect bb_interact = bb; + bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f)); + ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap); + if (g.ActiveId != id) + SetItemAllowOverlap(); + + if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) + SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW); + + ImRect bb_render = bb; + if (held) + { + ImVec2 mouse_delta_2d = g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min; + float mouse_delta = (axis == ImGuiAxis_Y) ? mouse_delta_2d.y : mouse_delta_2d.x; + + // Minimum pane size + float size_1_maximum_delta = ImMax(0.0f, *size1 - min_size1); + float size_2_maximum_delta = ImMax(0.0f, *size2 - min_size2); + if (mouse_delta < -size_1_maximum_delta) + mouse_delta = -size_1_maximum_delta; + if (mouse_delta > size_2_maximum_delta) + mouse_delta = size_2_maximum_delta; + + // Apply resize + if (mouse_delta != 0.0f) + { + if (mouse_delta < 0.0f) + IM_ASSERT(*size1 + mouse_delta >= min_size1); + if (mouse_delta > 0.0f) + IM_ASSERT(*size2 - mouse_delta >= min_size2); + *size1 += mouse_delta; + *size2 -= mouse_delta; + bb_render.Translate((axis == ImGuiAxis_X) ? ImVec2(mouse_delta, 0.0f) : ImVec2(0.0f, mouse_delta)); + MarkItemEdited(id); + } + } + + // Render + const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); + window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, g.Style.FrameRounding); + + return held; +} + +static int IMGUI_CDECL ShrinkWidthItemComparer(const void* lhs, const void* rhs) +{ + const ImGuiShrinkWidthItem* a = (const ImGuiShrinkWidthItem*)lhs; + const ImGuiShrinkWidthItem* b = (const ImGuiShrinkWidthItem*)rhs; + if (int d = (int)(b->Width - a->Width)) + return d; + return (b->Index - a->Index); +} + +// Shrink excess width from a set of item, by removing width from the larger items first. +void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess) +{ + if (count == 1) + { + items[0].Width = ImMax(items[0].Width - width_excess, 1.0f); + return; + } + ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer); + int count_same_width = 1; + while (width_excess > 0.0f && count_same_width < count) + { + while (count_same_width < count && items[0].Width <= items[count_same_width].Width) + count_same_width++; + float max_width_to_remove_per_item = (count_same_width < count) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f); + float width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item); + for (int item_n = 0; item_n < count_same_width; item_n++) + items[item_n].Width -= width_to_remove_per_item; + width_excess -= width_to_remove_per_item * count_same_width; + } + + // Round width and redistribute remainder left-to-right (could make it an option of the function?) + // Ensure that e.g. the right-most tab of a shrunk tab-bar always reaches exactly at the same distance from the right-most edge of the tab bar separator. + width_excess = 0.0f; + for (int n = 0; n < count; n++) + { + float width_rounded = ImFloor(items[n].Width); + width_excess += items[n].Width - width_rounded; + items[n].Width = width_rounded; + } + if (width_excess > 0.0f) + for (int n = 0; n < count; n++) + if (items[n].Index < (int)(width_excess + 0.01f)) + items[n].Width += 1.0f; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: ComboBox +//------------------------------------------------------------------------- +// - BeginCombo() +// - EndCombo() +// - Combo() +//------------------------------------------------------------------------- + +static float CalcMaxPopupHeightFromItemCount(int items_count) +{ + ImGuiContext& g = *GImGui; + if (items_count <= 0) + return FLT_MAX; + return (g.FontSize + g.Style.ItemSpacing.y) * items_count - g.Style.ItemSpacing.y + (g.Style.WindowPadding.y * 2); +} + +bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags) +{ + // Always consume the SetNextWindowSizeConstraint() call in our early return paths + ImGuiContext& g = *GImGui; + bool has_window_size_constraint = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) != 0; + g.NextWindowData.Flags &= ~ImGuiNextWindowDataFlags_HasSizeConstraint; + + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together + + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + + const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight(); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const float expected_w = CalcItemWidth(); + const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w; + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id, &frame_bb)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held); + bool popup_open = IsPopupOpen(id); + + const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); + RenderNavHighlight(frame_bb, id); + if (!(flags & ImGuiComboFlags_NoPreview)) + window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Left); + if (!(flags & ImGuiComboFlags_NoArrowButton)) + { + ImU32 bg_col = GetColorU32((popup_open || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + ImU32 text_col = GetColorU32(ImGuiCol_Text); + window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Right); + if (value_x2 + arrow_size - style.FramePadding.x <= frame_bb.Max.x) + RenderArrow(window->DrawList, ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), text_col, ImGuiDir_Down, 1.0f); + } + RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); + if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) + RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f,0.0f)); + if (label_size.x > 0) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + if ((pressed || g.NavActivateId == id) && !popup_open) + { + if (window->DC.NavLayerCurrent == 0) + window->NavLastIds[0] = id; + OpenPopupEx(id); + popup_open = true; + } + + if (!popup_open) + return false; + + if (has_window_size_constraint) + { + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasSizeConstraint; + g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w); + } + else + { + if ((flags & ImGuiComboFlags_HeightMask_) == 0) + flags |= ImGuiComboFlags_HeightRegular; + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one + int popup_max_height_in_items = -1; + if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; + else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; + else if (flags & ImGuiComboFlags_HeightLarge) popup_max_height_in_items = 20; + SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); + } + + char name[16]; + ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth + + // Peak into expected window size so we can position it + if (ImGuiWindow* popup_window = FindWindowByName(name)) + if (popup_window->WasActive) + { + ImVec2 size_expected = CalcWindowExpectedSize(popup_window); + if (flags & ImGuiComboFlags_PopupAlignLeft) + popup_window->AutoPosLastDirection = ImGuiDir_Left; + ImRect r_outer = GetWindowAllowedExtentRect(popup_window); + ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox); + SetNextWindowPos(pos); + } + + // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx() + ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove; + + // Horizontally align ourselves with the framed text + PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(style.FramePadding.x, style.WindowPadding.y)); + bool ret = Begin(name, NULL, window_flags); + PopStyleVar(); + if (!ret) + { + EndPopup(); + IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above + return false; + } + return true; +} + +void ImGui::EndCombo() +{ + EndPopup(); +} + +// Getter for the old Combo() API: const char*[] +static bool Items_ArrayGetter(void* data, int idx, const char** out_text) +{ + const char* const* items = (const char* const*)data; + if (out_text) + *out_text = items[idx]; + return true; +} + +// Getter for the old Combo() API: "item1\0item2\0item3\0" +static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) +{ + // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited. + const char* items_separated_by_zeros = (const char*)data; + int items_count = 0; + const char* p = items_separated_by_zeros; + while (*p) + { + if (idx == items_count) + break; + p += strlen(p) + 1; + items_count++; + } + if (!*p) + return false; + if (out_text) + *out_text = p; + return true; +} + +// Old API, prefer using BeginCombo() nowadays if you can. +bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_max_height_in_items) +{ + ImGuiContext& g = *GImGui; + + // Call the getter to obtain the preview string which is a parameter to BeginCombo() + const char* preview_value = NULL; + if (*current_item >= 0 && *current_item < items_count) + items_getter(data, *current_item, &preview_value); + + // The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here. + if (popup_max_height_in_items != -1 && !(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint)) + SetNextWindowSizeConstraints(ImVec2(0,0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); + + if (!BeginCombo(label, preview_value, ImGuiComboFlags_None)) + return false; + + // Display items + // FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed) + bool value_changed = false; + for (int i = 0; i < items_count; i++) + { + PushID((void*)(intptr_t)i); + const bool item_selected = (i == *current_item); + const char* item_text; + if (!items_getter(data, i, &item_text)) + item_text = "*Unknown item*"; + if (Selectable(item_text, item_selected)) + { + value_changed = true; + *current_item = i; + } + if (item_selected) + SetItemDefaultFocus(); + PopID(); + } + + EndCombo(); + return value_changed; +} + +// Combo box helper allowing to pass an array of strings. +bool ImGui::Combo(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items) +{ + const bool value_changed = Combo(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_in_items); + return value_changed; +} + +// Combo box helper allowing to pass all items in a single string literal holding multiple zero-terminated items "item1\0item2\0" +bool ImGui::Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int height_in_items) +{ + int items_count = 0; + const char* p = items_separated_by_zeros; // FIXME-OPT: Avoid computing this, or at least only when combo is open + while (*p) + { + p += strlen(p) + 1; + items_count++; + } + bool value_changed = Combo(label, current_item, Items_SingleStringGetter, (void*)items_separated_by_zeros, items_count, height_in_items); + return value_changed; +} + +//------------------------------------------------------------------------- +// [SECTION] Data Type and Data Formatting Helpers [Internal] +//------------------------------------------------------------------------- +// - PatchFormatStringFloatToInt() +// - DataTypeGetInfo() +// - DataTypeFormatString() +// - DataTypeApplyOp() +// - DataTypeApplyOpFromText() +// - GetMinimumStepAtDecimalPrecision +// - RoundScalarWithFormat<>() +//------------------------------------------------------------------------- + +static const ImGuiDataTypeInfo GDataTypeInfo[] = +{ + { sizeof(char), "%d", "%d" }, // ImGuiDataType_S8 + { sizeof(unsigned char), "%u", "%u" }, + { sizeof(short), "%d", "%d" }, // ImGuiDataType_S16 + { sizeof(unsigned short), "%u", "%u" }, + { sizeof(int), "%d", "%d" }, // ImGuiDataType_S32 + { sizeof(unsigned int), "%u", "%u" }, +#ifdef _MSC_VER + { sizeof(ImS64), "%I64d","%I64d" }, // ImGuiDataType_S64 + { sizeof(ImU64), "%I64u","%I64u" }, +#else + { sizeof(ImS64), "%lld", "%lld" }, // ImGuiDataType_S64 + { sizeof(ImU64), "%llu", "%llu" }, +#endif + { sizeof(float), "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) + { sizeof(double), "%f", "%lf" }, // ImGuiDataType_Double +}; +IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); + +// FIXME-LEGACY: Prior to 1.61 our DragInt() function internally used floats and because of this the compile-time default value for format was "%.0f". +// Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls. +// To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?! +static const char* PatchFormatStringFloatToInt(const char* fmt) +{ + if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '0' && fmt[3] == 'f' && fmt[4] == 0) // Fast legacy path for "%.0f" which is expected to be the most common case. + return "%d"; + const char* fmt_start = ImParseFormatFindStart(fmt); // Find % (if any, and ignore %%) + const char* fmt_end = ImParseFormatFindEnd(fmt_start); // Find end of format specifier, which itself is an exercise of confidence/recklessness (because snprintf is dependent on libc or user). + if (fmt_end > fmt_start && fmt_end[-1] == 'f') + { +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (fmt_start == fmt && fmt_end[0] == 0) + return "%d"; + ImGuiContext& g = *GImGui; + ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision. + return g.TempBuffer; +#else + IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d" +#endif + } + return fmt; +} + +const ImGuiDataTypeInfo* ImGui::DataTypeGetInfo(ImGuiDataType data_type) +{ + IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); + return &GDataTypeInfo[data_type]; +} + +int ImGui::DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format) +{ + // Signedness doesn't matter when pushing integer arguments + if (data_type == ImGuiDataType_S32 || data_type == ImGuiDataType_U32) + return ImFormatString(buf, buf_size, format, *(const ImU32*)p_data); + if (data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) + return ImFormatString(buf, buf_size, format, *(const ImU64*)p_data); + if (data_type == ImGuiDataType_Float) + return ImFormatString(buf, buf_size, format, *(const float*)p_data); + if (data_type == ImGuiDataType_Double) + return ImFormatString(buf, buf_size, format, *(const double*)p_data); + if (data_type == ImGuiDataType_S8) + return ImFormatString(buf, buf_size, format, *(const ImS8*)p_data); + if (data_type == ImGuiDataType_U8) + return ImFormatString(buf, buf_size, format, *(const ImU8*)p_data); + if (data_type == ImGuiDataType_S16) + return ImFormatString(buf, buf_size, format, *(const ImS16*)p_data); + if (data_type == ImGuiDataType_U16) + return ImFormatString(buf, buf_size, format, *(const ImU16*)p_data); + IM_ASSERT(0); + return 0; +} + +void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg1, const void* arg2) +{ + IM_ASSERT(op == '+' || op == '-'); + switch (data_type) + { + case ImGuiDataType_S8: + if (op == '+') { *(ImS8*)output = ImAddClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } + if (op == '-') { *(ImS8*)output = ImSubClampOverflow(*(const ImS8*)arg1, *(const ImS8*)arg2, IM_S8_MIN, IM_S8_MAX); } + return; + case ImGuiDataType_U8: + if (op == '+') { *(ImU8*)output = ImAddClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } + if (op == '-') { *(ImU8*)output = ImSubClampOverflow(*(const ImU8*)arg1, *(const ImU8*)arg2, IM_U8_MIN, IM_U8_MAX); } + return; + case ImGuiDataType_S16: + if (op == '+') { *(ImS16*)output = ImAddClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } + if (op == '-') { *(ImS16*)output = ImSubClampOverflow(*(const ImS16*)arg1, *(const ImS16*)arg2, IM_S16_MIN, IM_S16_MAX); } + return; + case ImGuiDataType_U16: + if (op == '+') { *(ImU16*)output = ImAddClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } + if (op == '-') { *(ImU16*)output = ImSubClampOverflow(*(const ImU16*)arg1, *(const ImU16*)arg2, IM_U16_MIN, IM_U16_MAX); } + return; + case ImGuiDataType_S32: + if (op == '+') { *(ImS32*)output = ImAddClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } + if (op == '-') { *(ImS32*)output = ImSubClampOverflow(*(const ImS32*)arg1, *(const ImS32*)arg2, IM_S32_MIN, IM_S32_MAX); } + return; + case ImGuiDataType_U32: + if (op == '+') { *(ImU32*)output = ImAddClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } + if (op == '-') { *(ImU32*)output = ImSubClampOverflow(*(const ImU32*)arg1, *(const ImU32*)arg2, IM_U32_MIN, IM_U32_MAX); } + return; + case ImGuiDataType_S64: + if (op == '+') { *(ImS64*)output = ImAddClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } + if (op == '-') { *(ImS64*)output = ImSubClampOverflow(*(const ImS64*)arg1, *(const ImS64*)arg2, IM_S64_MIN, IM_S64_MAX); } + return; + case ImGuiDataType_U64: + if (op == '+') { *(ImU64*)output = ImAddClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } + if (op == '-') { *(ImU64*)output = ImSubClampOverflow(*(const ImU64*)arg1, *(const ImU64*)arg2, IM_U64_MIN, IM_U64_MAX); } + return; + case ImGuiDataType_Float: + if (op == '+') { *(float*)output = *(const float*)arg1 + *(const float*)arg2; } + if (op == '-') { *(float*)output = *(const float*)arg1 - *(const float*)arg2; } + return; + case ImGuiDataType_Double: + if (op == '+') { *(double*)output = *(const double*)arg1 + *(const double*)arg2; } + if (op == '-') { *(double*)output = *(const double*)arg1 - *(const double*)arg2; } + return; + case ImGuiDataType_COUNT: break; + } + IM_ASSERT(0); +} + +// User can input math operators (e.g. +100) to edit a numerical values. +// NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess.. +bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format) +{ + while (ImCharIsBlankA(*buf)) + buf++; + + // We don't support '-' op because it would conflict with inputing negative value. + // Instead you can use +-100 to subtract from an existing value + char op = buf[0]; + if (op == '+' || op == '*' || op == '/') + { + buf++; + while (ImCharIsBlankA(*buf)) + buf++; + } + else + { + op = 0; + } + if (!buf[0]) + return false; + + // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. + IM_ASSERT(data_type < ImGuiDataType_COUNT); + int data_backup[2]; + const ImGuiDataTypeInfo* type_info = ImGui::DataTypeGetInfo(data_type); + IM_ASSERT(type_info->Size <= sizeof(data_backup)); + memcpy(data_backup, p_data, type_info->Size); + + if (format == NULL) + format = type_info->ScanFmt; + + // FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point.. + int arg1i = 0; + if (data_type == ImGuiDataType_S32) + { + int* v = (int*)p_data; + int arg0i = *v; + float arg1f = 0.0f; + if (op && sscanf(initial_value_buf, format, &arg0i) < 1) + return false; + // Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision + if (op == '+') { if (sscanf(buf, "%d", &arg1i)) *v = (int)(arg0i + arg1i); } // Add (use "+-" to subtract) + else if (op == '*') { if (sscanf(buf, "%f", &arg1f)) *v = (int)(arg0i * arg1f); } // Multiply + else if (op == '/') { if (sscanf(buf, "%f", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); } // Divide + else { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; } // Assign constant + } + else if (data_type == ImGuiDataType_Float) + { + // For floats we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in + format = "%f"; + float* v = (float*)p_data; + float arg0f = *v, arg1f = 0.0f; + if (op && sscanf(initial_value_buf, format, &arg0f) < 1) + return false; + if (sscanf(buf, format, &arg1f) < 1) + return false; + if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) + else if (op == '*') { *v = arg0f * arg1f; } // Multiply + else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide + else { *v = arg1f; } // Assign constant + } + else if (data_type == ImGuiDataType_Double) + { + format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis + double* v = (double*)p_data; + double arg0f = *v, arg1f = 0.0; + if (op && sscanf(initial_value_buf, format, &arg0f) < 1) + return false; + if (sscanf(buf, format, &arg1f) < 1) + return false; + if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) + else if (op == '*') { *v = arg0f * arg1f; } // Multiply + else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide + else { *v = arg1f; } // Assign constant + } + else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) + { + // All other types assign constant + // We don't bother handling support for legacy operators since they are a little too crappy. Instead we will later implement a proper expression evaluator in the future. + sscanf(buf, format, p_data); + } + else + { + // Small types need a 32-bit buffer to receive the result from scanf() + int v32; + sscanf(buf, format, &v32); + if (data_type == ImGuiDataType_S8) + *(ImS8*)p_data = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX); + else if (data_type == ImGuiDataType_U8) + *(ImU8*)p_data = (ImU8)ImClamp(v32, (int)IM_U8_MIN, (int)IM_U8_MAX); + else if (data_type == ImGuiDataType_S16) + *(ImS16*)p_data = (ImS16)ImClamp(v32, (int)IM_S16_MIN, (int)IM_S16_MAX); + else if (data_type == ImGuiDataType_U16) + *(ImU16*)p_data = (ImU16)ImClamp(v32, (int)IM_U16_MIN, (int)IM_U16_MAX); + else + IM_ASSERT(0); + } + + return memcmp(data_backup, p_data, type_info->Size) != 0; +} + +static float GetMinimumStepAtDecimalPrecision(int decimal_precision) +{ + static const float min_steps[10] = { 1.0f, 0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 0.000000001f }; + if (decimal_precision < 0) + return FLT_MIN; + return (decimal_precision < IM_ARRAYSIZE(min_steps)) ? min_steps[decimal_precision] : ImPow(10.0f, (float)-decimal_precision); +} + +template +static const char* ImAtoi(const char* src, TYPE* output) +{ + int negative = 0; + if (*src == '-') { negative = 1; src++; } + if (*src == '+') { src++; } + TYPE v = 0; + while (*src >= '0' && *src <= '9') + v = (v * 10) + (*src++ - '0'); + *output = negative ? -v : v; + return src; +} + +template +TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v) +{ + const char* fmt_start = ImParseFormatFindStart(format); + if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string + return v; + char v_str[64]; + ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v); + const char* p = v_str; + while (*p == ' ') + p++; + if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) + v = (TYPE)ImAtof(p); + else + ImAtoi(p, (SIGNEDTYPE*)&v); + return v; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: DragScalar, DragFloat, DragInt, etc. +//------------------------------------------------------------------------- +// - DragBehaviorT<>() [Internal] +// - DragBehavior() [Internal] +// - DragScalar() +// - DragScalarN() +// - DragFloat() +// - DragFloat2() +// - DragFloat3() +// - DragFloat4() +// - DragFloatRange2() +// - DragInt() +// - DragInt2() +// - DragInt3() +// - DragInt4() +// - DragIntRange2() +//------------------------------------------------------------------------- + +// This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls) +template +bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiDragFlags flags) +{ + ImGuiContext& g = *GImGui; + const ImGuiAxis axis = (flags & ImGuiDragFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; + const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + const bool is_clamped = (v_min < v_max); + const bool is_power = (power != 1.0f && is_decimal && is_clamped && (v_max - v_min < FLT_MAX)); + const bool is_locked = (v_min > v_max); + if (is_locked) + return false; + + // Default tweak speed + if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) + v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio); + + // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings + float adjust_delta = 0.0f; + if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f*1.0f) + { + adjust_delta = g.IO.MouseDelta[axis]; + if (g.IO.KeyAlt) + adjust_delta *= 1.0f / 100.0f; + if (g.IO.KeyShift) + adjust_delta *= 10.0f; + } + else if (g.ActiveIdSource == ImGuiInputSource_Nav) + { + int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; + adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; + v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); + } + adjust_delta *= v_speed; + + // For vertical drag we currently assume that Up=higher value (like we do with vertical sliders). This may become a parameter. + if (axis == ImGuiAxis_Y) + adjust_delta = -adjust_delta; + + // Clear current value on activation + // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300. + bool is_just_activated = g.ActiveIdIsJustActivated; + bool is_already_past_limits_and_pushing_outward = is_clamped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f)); + bool is_drag_direction_change_with_power = is_power && ((adjust_delta < 0 && g.DragCurrentAccum > 0) || (adjust_delta > 0 && g.DragCurrentAccum < 0)); + if (is_just_activated || is_already_past_limits_and_pushing_outward || is_drag_direction_change_with_power) + { + g.DragCurrentAccum = 0.0f; + g.DragCurrentAccumDirty = false; + } + else if (adjust_delta != 0.0f) + { + g.DragCurrentAccum += adjust_delta; + g.DragCurrentAccumDirty = true; + } + + if (!g.DragCurrentAccumDirty) + return false; + + TYPE v_cur = *v; + FLOATTYPE v_old_ref_for_accum_remainder = (FLOATTYPE)0.0f; + + if (is_power) + { + // Offset + round to user desired precision, with a curve on the v_min..v_max range to get more precision on one side of the range + FLOATTYPE v_old_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); + FLOATTYPE v_new_norm_curved = v_old_norm_curved + (g.DragCurrentAccum / (v_max - v_min)); + v_cur = v_min + (SIGNEDTYPE)ImPow(ImSaturate((float)v_new_norm_curved), power) * (v_max - v_min); + v_old_ref_for_accum_remainder = v_old_norm_curved; + } + else + { + v_cur += (SIGNEDTYPE)g.DragCurrentAccum; + } + + // Round to user desired precision based on format string + v_cur = RoundScalarWithFormatT(format, data_type, v_cur); + + // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. + g.DragCurrentAccumDirty = false; + if (is_power) + { + FLOATTYPE v_cur_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); + g.DragCurrentAccum -= (float)(v_cur_norm_curved - v_old_ref_for_accum_remainder); + } + else + { + g.DragCurrentAccum -= (float)((SIGNEDTYPE)v_cur - (SIGNEDTYPE)*v); + } + + // Lose zero sign for float/double + if (v_cur == (TYPE)-0) + v_cur = (TYPE)0; + + // Clamp values (+ handle overflow/wrap-around for integer types) + if (*v != v_cur && is_clamped) + { + if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal)) + v_cur = v_min; + if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal)) + v_cur = v_max; + } + + // Apply result + if (*v == v_cur) + return false; + *v = v_cur; + return true; +} + +bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, float power, ImGuiDragFlags flags) +{ + ImGuiContext& g = *GImGui; + if (g.ActiveId == id) + { + if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0]) + ClearActiveID(); + else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) + ClearActiveID(); + } + if (g.ActiveId != id) + return false; + + switch (data_type) + { + case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS8*) p_min : IM_S8_MIN, p_max ? *(const ImS8*)p_max : IM_S8_MAX, format, power, flags); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } + case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU8*) p_min : IM_U8_MIN, p_max ? *(const ImU8*)p_max : IM_U8_MAX, format, power, flags); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } + case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS16*)p_min : IM_S16_MIN, p_max ? *(const ImS16*)p_max : IM_S16_MAX, format, power, flags); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } + case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU16*)p_min : IM_U16_MIN, p_max ? *(const ImU16*)p_max : IM_U16_MAX, format, power, flags); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } + case ImGuiDataType_S32: return DragBehaviorT(data_type, (ImS32*)p_v, v_speed, p_min ? *(const ImS32* )p_min : IM_S32_MIN, p_max ? *(const ImS32* )p_max : IM_S32_MAX, format, power, flags); + case ImGuiDataType_U32: return DragBehaviorT(data_type, (ImU32*)p_v, v_speed, p_min ? *(const ImU32* )p_min : IM_U32_MIN, p_max ? *(const ImU32* )p_max : IM_U32_MAX, format, power, flags); + case ImGuiDataType_S64: return DragBehaviorT(data_type, (ImS64*)p_v, v_speed, p_min ? *(const ImS64* )p_min : IM_S64_MIN, p_max ? *(const ImS64* )p_max : IM_S64_MAX, format, power, flags); + case ImGuiDataType_U64: return DragBehaviorT(data_type, (ImU64*)p_v, v_speed, p_min ? *(const ImU64* )p_min : IM_U64_MIN, p_max ? *(const ImU64* )p_max : IM_U64_MAX, format, power, flags); + case ImGuiDataType_Float: return DragBehaviorT(data_type, (float*)p_v, v_speed, p_min ? *(const float* )p_min : -FLT_MAX, p_max ? *(const float* )p_max : FLT_MAX, format, power, flags); + case ImGuiDataType_Double: return DragBehaviorT(data_type, (double*)p_v, v_speed, p_min ? *(const double*)p_min : -DBL_MAX, p_max ? *(const double*)p_max : DBL_MAX, format, power, flags); + case ImGuiDataType_COUNT: break; + } + IM_ASSERT(0); + return false; +} + +// Note: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a Drag widget, p_min and p_max are optional. +// Read code of e.g. SliderFloat(), SliderInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. +bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + if (power != 1.0f) + IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const float w = CalcItemWidth(); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id, &frame_bb)) + return false; + + // Default format string when passing NULL + if (format == NULL) + format = DataTypeGetInfo(data_type)->PrintFmt; + else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) + format = PatchFormatStringFloatToInt(format); + + // Tabbing or CTRL-clicking on Drag turns it into an input box + const bool hovered = ItemHoverable(frame_bb, id); + bool temp_input_is_active = TempInputTextIsActive(id); + bool temp_input_start = false; + if (!temp_input_is_active) + { + const bool focus_requested = FocusableItemRegister(window, id); + const bool clicked = (hovered && g.IO.MouseClicked[0]); + const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); + if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) + { + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); + if (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id) + { + temp_input_start = true; + FocusableItemUnregister(window); + } + } + } + if (temp_input_is_active || temp_input_start) + return TempInputTextScalar(frame_bb, id, label, data_type, p_data, format); + + // Draw frame + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + RenderNavHighlight(frame_bb, id); + RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); + + // Drag behavior + const bool value_changed = DragBehavior(id, data_type, p_data, v_speed, p_min, p_max, format, power, ImGuiDragFlags_None); + if (value_changed) + MarkItemEdited(id); + + // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. + char value_buf[64]; + const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); + RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); + + if (label_size.x > 0.0f) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + return value_changed; +} + +bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + bool value_changed = false; + BeginGroup(); + PushID(label); + PushMultiItemsWidths(components, CalcItemWidth()); + size_t type_size = GDataTypeInfo[data_type].Size; + for (int i = 0; i < components; i++) + { + PushID(i); + if (i > 0) + SameLine(0, g.Style.ItemInnerSpacing.x); + value_changed |= DragScalar("", data_type, p_data, v_speed, p_min, p_max, format, power); + PopID(); + PopItemWidth(); + p_data = (void*)((char*)p_data + type_size); + } + PopID(); + + const char* label_end = FindRenderedTextEnd(label); + if (label != label_end) + { + SameLine(0, g.Style.ItemInnerSpacing.x); + TextEx(label, label_end); + } + + EndGroup(); + return value_changed; +} + +bool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) +{ + return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); +} + +bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) +{ + return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); +} + +bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) +{ + return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); +} + +bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) +{ + return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); +} + +bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + PushID(label); + BeginGroup(); + PushMultiItemsWidths(2, CalcItemWidth()); + + bool value_changed = DragFloat("##min", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format, power); + PopItemWidth(); + SameLine(0, g.Style.ItemInnerSpacing.x); + value_changed |= DragFloat("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, format_max ? format_max : format, power); + PopItemWidth(); + SameLine(0, g.Style.ItemInnerSpacing.x); + + TextEx(label, FindRenderedTextEnd(label)); + EndGroup(); + PopID(); + return value_changed; +} + +// NB: v_speed is float to allow adjusting the drag speed with more precision +bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format) +{ + return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format); +} + +bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format) +{ + return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format); +} + +bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format) +{ + return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format); +} + +bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format) +{ + return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format); +} + +bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + PushID(label); + BeginGroup(); + PushMultiItemsWidths(2, CalcItemWidth()); + + bool value_changed = DragInt("##min", v_current_min, v_speed, (v_min >= v_max) ? INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format); + PopItemWidth(); + SameLine(0, g.Style.ItemInnerSpacing.x); + value_changed |= DragInt("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? INT_MAX : v_max, format_max ? format_max : format); + PopItemWidth(); + SameLine(0, g.Style.ItemInnerSpacing.x); + + TextEx(label, FindRenderedTextEnd(label)); + EndGroup(); + PopID(); + + return value_changed; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. +//------------------------------------------------------------------------- +// - SliderBehaviorT<>() [Internal] +// - SliderBehavior() [Internal] +// - SliderScalar() +// - SliderScalarN() +// - SliderFloat() +// - SliderFloat2() +// - SliderFloat3() +// - SliderFloat4() +// - SliderAngle() +// - SliderInt() +// - SliderInt2() +// - SliderInt3() +// - SliderInt4() +// - VSliderScalar() +// - VSliderFloat() +// - VSliderInt() +//------------------------------------------------------------------------- + +template +float ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos) +{ + if (v_min == v_max) + return 0.0f; + + const bool is_power = (power != 1.0f) && (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double); + const TYPE v_clamped = (v_min < v_max) ? ImClamp(v, v_min, v_max) : ImClamp(v, v_max, v_min); + if (is_power) + { + if (v_clamped < 0.0f) + { + const float f = 1.0f - (float)((v_clamped - v_min) / (ImMin((TYPE)0, v_max) - v_min)); + return (1.0f - ImPow(f, 1.0f/power)) * linear_zero_pos; + } + else + { + const float f = (float)((v_clamped - ImMax((TYPE)0, v_min)) / (v_max - ImMax((TYPE)0, v_min))); + return linear_zero_pos + ImPow(f, 1.0f/power) * (1.0f - linear_zero_pos); + } + } + + // Linear slider + return (float)((FLOATTYPE)(v_clamped - v_min) / (FLOATTYPE)(v_max - v_min)); +} + +// FIXME: Move some of the code into SliderBehavior(). Current responsability is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc. +template +bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) +{ + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; + const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + const bool is_power = (power != 1.0f) && is_decimal; + + const float grab_padding = 2.0f; + const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; + float grab_sz = style.GrabMinSize; + SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); + if (!is_decimal && v_range >= 0) // v_range < 0 may happen on integer overflows + grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit + grab_sz = ImMin(grab_sz, slider_sz); + const float slider_usable_sz = slider_sz - grab_sz; + const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f; + const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f; + + // For power curve sliders that cross over sign boundary we want the curve to be symmetric around 0.0f + float linear_zero_pos; // 0.0->1.0f + if (is_power && v_min * v_max < 0.0f) + { + // Different sign + const FLOATTYPE linear_dist_min_to_0 = ImPow(v_min >= 0 ? (FLOATTYPE)v_min : -(FLOATTYPE)v_min, (FLOATTYPE)1.0f / power); + const FLOATTYPE linear_dist_max_to_0 = ImPow(v_max >= 0 ? (FLOATTYPE)v_max : -(FLOATTYPE)v_max, (FLOATTYPE)1.0f / power); + linear_zero_pos = (float)(linear_dist_min_to_0 / (linear_dist_min_to_0 + linear_dist_max_to_0)); + } + else + { + // Same sign + linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f; + } + + // Process interacting with the slider + bool value_changed = false; + if (g.ActiveId == id) + { + bool set_new_value = false; + float clicked_t = 0.0f; + if (g.ActiveIdSource == ImGuiInputSource_Mouse) + { + if (!g.IO.MouseDown[0]) + { + ClearActiveID(); + } + else + { + const float mouse_abs_pos = g.IO.MousePos[axis]; + clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f; + if (axis == ImGuiAxis_Y) + clicked_t = 1.0f - clicked_t; + set_new_value = true; + } + } + else if (g.ActiveIdSource == ImGuiInputSource_Nav) + { + const ImVec2 delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f); + float delta = (axis == ImGuiAxis_X) ? delta2.x : -delta2.y; + if (g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) + { + ClearActiveID(); + } + else if (delta != 0.0f) + { + clicked_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); + const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; + if ((decimal_precision > 0) || is_power) + { + delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds + if (IsNavInputDown(ImGuiNavInput_TweakSlow)) + delta /= 10.0f; + } + else + { + if ((v_range >= -100.0f && v_range <= 100.0f) || IsNavInputDown(ImGuiNavInput_TweakSlow)) + delta = ((delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps + else + delta /= 100.0f; + } + if (IsNavInputDown(ImGuiNavInput_TweakFast)) + delta *= 10.0f; + set_new_value = true; + if ((clicked_t >= 1.0f && delta > 0.0f) || (clicked_t <= 0.0f && delta < 0.0f)) // This is to avoid applying the saturation when already past the limits + set_new_value = false; + else + clicked_t = ImSaturate(clicked_t + delta); + } + } + + if (set_new_value) + { + TYPE v_new; + if (is_power) + { + // Account for power curve scale on both sides of the zero + if (clicked_t < linear_zero_pos) + { + // Negative: rescale to the negative range before powering + float a = 1.0f - (clicked_t / linear_zero_pos); + a = ImPow(a, power); + v_new = ImLerp(ImMin(v_max, (TYPE)0), v_min, a); + } + else + { + // Positive: rescale to the positive range before powering + float a; + if (ImFabs(linear_zero_pos - 1.0f) > 1.e-6f) + a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos); + else + a = clicked_t; + a = ImPow(a, power); + v_new = ImLerp(ImMax(v_min, (TYPE)0), v_max, a); + } + } + else + { + // Linear slider + if (is_decimal) + { + v_new = ImLerp(v_min, v_max, clicked_t); + } + else + { + // For integer values we want the clicking position to match the grab box so we round above + // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property.. + FLOATTYPE v_new_off_f = (v_max - v_min) * clicked_t; + TYPE v_new_off_floor = (TYPE)(v_new_off_f); + TYPE v_new_off_round = (TYPE)(v_new_off_f + (FLOATTYPE)0.5); + if (v_new_off_floor < v_new_off_round) + v_new = v_min + v_new_off_round; + else + v_new = v_min + v_new_off_floor; + } + } + + // Round to user desired precision based on format string + v_new = RoundScalarWithFormatT(format, data_type, v_new); + + // Apply result + if (*v != v_new) + { + *v = v_new; + value_changed = true; + } + } + } + + if (slider_sz < 1.0f) + { + *out_grab_bb = ImRect(bb.Min, bb.Min); + } + else + { + // Output grab position so it can be displayed by the caller + float grab_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); + if (axis == ImGuiAxis_Y) + grab_t = 1.0f - grab_t; + const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t); + if (axis == ImGuiAxis_X) + *out_grab_bb = ImRect(grab_pos - grab_sz * 0.5f, bb.Min.y + grab_padding, grab_pos + grab_sz * 0.5f, bb.Max.y - grab_padding); + else + *out_grab_bb = ImRect(bb.Min.x + grab_padding, grab_pos - grab_sz * 0.5f, bb.Max.x - grab_padding, grab_pos + grab_sz * 0.5f); + } + + return value_changed; +} + +// For 32-bits and larger types, slider bounds are limited to half the natural type range. +// So e.g. an integer Slider between INT_MAX-10 and INT_MAX will fail, but an integer Slider between INT_MAX/2-10 and INT_MAX/2 will be ok. +// It would be possible to lift that limitation with some work but it doesn't seem to be worth it for sliders. +bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) +{ + switch (data_type) + { + case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS8*)p_min, *(const ImS8*)p_max, format, power, flags, out_grab_bb); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } + case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU8*)p_min, *(const ImU8*)p_max, format, power, flags, out_grab_bb); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } + case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS16*)p_min, *(const ImS16*)p_max, format, power, flags, out_grab_bb); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } + case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU16*)p_min, *(const ImU16*)p_max, format, power, flags, out_grab_bb); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } + case ImGuiDataType_S32: + IM_ASSERT(*(const ImS32*)p_min >= IM_S32_MIN/2 && *(const ImS32*)p_max <= IM_S32_MAX/2); + return SliderBehaviorT(bb, id, data_type, (ImS32*)p_v, *(const ImS32*)p_min, *(const ImS32*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_U32: + IM_ASSERT(*(const ImU32*)p_max <= IM_U32_MAX/2); + return SliderBehaviorT(bb, id, data_type, (ImU32*)p_v, *(const ImU32*)p_min, *(const ImU32*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_S64: + IM_ASSERT(*(const ImS64*)p_min >= IM_S64_MIN/2 && *(const ImS64*)p_max <= IM_S64_MAX/2); + return SliderBehaviorT(bb, id, data_type, (ImS64*)p_v, *(const ImS64*)p_min, *(const ImS64*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_U64: + IM_ASSERT(*(const ImU64*)p_max <= IM_U64_MAX/2); + return SliderBehaviorT(bb, id, data_type, (ImU64*)p_v, *(const ImU64*)p_min, *(const ImU64*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_Float: + IM_ASSERT(*(const float*)p_min >= -FLT_MAX/2.0f && *(const float*)p_max <= FLT_MAX/2.0f); + return SliderBehaviorT(bb, id, data_type, (float*)p_v, *(const float*)p_min, *(const float*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_Double: + IM_ASSERT(*(const double*)p_min >= -DBL_MAX/2.0f && *(const double*)p_max <= DBL_MAX/2.0f); + return SliderBehaviorT(bb, id, data_type, (double*)p_v, *(const double*)p_min, *(const double*)p_max, format, power, flags, out_grab_bb); + case ImGuiDataType_COUNT: break; + } + IM_ASSERT(0); + return false; +} + +// Note: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a slider, they are all required. +// Read code of e.g. SliderFloat(), SliderInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. +bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + const float w = CalcItemWidth(); + + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id, &frame_bb)) + return false; + + // Default format string when passing NULL + if (format == NULL) + format = DataTypeGetInfo(data_type)->PrintFmt; + else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) + format = PatchFormatStringFloatToInt(format); + + // Tabbing or CTRL-clicking on Slider turns it into an input box + const bool hovered = ItemHoverable(frame_bb, id); + bool temp_input_is_active = TempInputTextIsActive(id); + bool temp_input_start = false; + if (!temp_input_is_active) + { + const bool focus_requested = FocusableItemRegister(window, id); + const bool clicked = (hovered && g.IO.MouseClicked[0]); + if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id) + { + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); + if (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id) + { + temp_input_start = true; + FocusableItemUnregister(window); + } + } + } + if (temp_input_is_active || temp_input_start) + return TempInputTextScalar(frame_bb, id, label, data_type, p_data, format); + + // Draw frame + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + RenderNavHighlight(frame_bb, id); + RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); + + // Slider behavior + ImRect grab_bb; + const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, power, ImGuiSliderFlags_None, &grab_bb); + if (value_changed) + MarkItemEdited(id); + + // Render grab + if (grab_bb.Max.x > grab_bb.Min.x) + window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding); + + // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. + char value_buf[64]; + const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); + RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.5f)); + + if (label_size.x > 0.0f) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + return value_changed; +} + +// Add multiple sliders on 1 line for compact edition of multiple components +bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + bool value_changed = false; + BeginGroup(); + PushID(label); + PushMultiItemsWidths(components, CalcItemWidth()); + size_t type_size = GDataTypeInfo[data_type].Size; + for (int i = 0; i < components; i++) + { + PushID(i); + if (i > 0) + SameLine(0, g.Style.ItemInnerSpacing.x); + value_changed |= SliderScalar("", data_type, v, v_min, v_max, format, power); + PopID(); + PopItemWidth(); + v = (void*)((char*)v + type_size); + } + PopID(); + + const char* label_end = FindRenderedTextEnd(label); + if (label != label_end) + { + SameLine(0, g.Style.ItemInnerSpacing.x); + TextEx(label, label_end); + } + + EndGroup(); + return value_changed; +} + +bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) +{ + return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); +} + +bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) +{ + return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); +} + +bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) +{ + return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); +} + +bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) +{ + return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); +} + +bool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format) +{ + if (format == NULL) + format = "%.0f deg"; + float v_deg = (*v_rad) * 360.0f / (2*IM_PI); + bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, 1.0f); + *v_rad = v_deg * (2*IM_PI) / 360.0f; + return value_changed; +} + +bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format) +{ + return SliderScalar(label, ImGuiDataType_S32, v, &v_min, &v_max, format); +} + +bool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format) +{ + return SliderScalarN(label, ImGuiDataType_S32, v, 2, &v_min, &v_max, format); +} + +bool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format) +{ + return SliderScalarN(label, ImGuiDataType_S32, v, 3, &v_min, &v_max, format); +} + +bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format) +{ + return SliderScalarN(label, ImGuiDataType_S32, v, 4, &v_min, &v_max, format); +} + +bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size); + const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + + ItemSize(bb, style.FramePadding.y); + if (!ItemAdd(frame_bb, id)) + return false; + + // Default format string when passing NULL + if (format == NULL) + format = DataTypeGetInfo(data_type)->PrintFmt; + else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) + format = PatchFormatStringFloatToInt(format); + + const bool hovered = ItemHoverable(frame_bb, id); + if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id) + { + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); + } + + // Draw frame + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + RenderNavHighlight(frame_bb, id); + RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); + + // Slider behavior + ImRect grab_bb; + const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, power, ImGuiSliderFlags_Vertical, &grab_bb); + if (value_changed) + MarkItemEdited(id); + + // Render grab + if (grab_bb.Max.y > grab_bb.Min.y) + window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, GetColorU32(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab), style.GrabRounding); + + // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. + // For the vertical slider we allow centered text to overlap the frame padding + char value_buf[64]; + const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); + RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.0f)); + if (label_size.x > 0.0f) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + return value_changed; +} + +bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format, float power) +{ + return VSliderScalar(label, size, ImGuiDataType_Float, v, &v_min, &v_max, format, power); +} + +bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format) +{ + return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. +//------------------------------------------------------------------------- +// - ImParseFormatFindStart() [Internal] +// - ImParseFormatFindEnd() [Internal] +// - ImParseFormatTrimDecorations() [Internal] +// - ImParseFormatPrecision() [Internal] +// - TempInputTextScalar() [Internal] +// - InputScalar() +// - InputScalarN() +// - InputFloat() +// - InputFloat2() +// - InputFloat3() +// - InputFloat4() +// - InputInt() +// - InputInt2() +// - InputInt3() +// - InputInt4() +// - InputDouble() +//------------------------------------------------------------------------- + +// We don't use strchr() because our strings are usually very short and often start with '%' +const char* ImParseFormatFindStart(const char* fmt) +{ + while (char c = fmt[0]) + { + if (c == '%' && fmt[1] != '%') + return fmt; + else if (c == '%') + fmt++; + fmt++; + } + return fmt; +} + +const char* ImParseFormatFindEnd(const char* fmt) +{ + // Printf/scanf types modifiers: I/L/h/j/l/t/w/z. Other uppercase letters qualify as types aka end of the format. + if (fmt[0] != '%') + return fmt; + const unsigned int ignored_uppercase_mask = (1 << ('I'-'A')) | (1 << ('L'-'A')); + const unsigned int ignored_lowercase_mask = (1 << ('h'-'a')) | (1 << ('j'-'a')) | (1 << ('l'-'a')) | (1 << ('t'-'a')) | (1 << ('w'-'a')) | (1 << ('z'-'a')); + for (char c; (c = *fmt) != 0; fmt++) + { + if (c >= 'A' && c <= 'Z' && ((1 << (c - 'A')) & ignored_uppercase_mask) == 0) + return fmt + 1; + if (c >= 'a' && c <= 'z' && ((1 << (c - 'a')) & ignored_lowercase_mask) == 0) + return fmt + 1; + } + return fmt; +} + +// Extract the format out of a format string with leading or trailing decorations +// fmt = "blah blah" -> return fmt +// fmt = "%.3f" -> return fmt +// fmt = "hello %.3f" -> return fmt + 6 +// fmt = "%.3f hello" -> return buf written with "%.3f" +const char* ImParseFormatTrimDecorations(const char* fmt, char* buf, size_t buf_size) +{ + const char* fmt_start = ImParseFormatFindStart(fmt); + if (fmt_start[0] != '%') + return fmt; + const char* fmt_end = ImParseFormatFindEnd(fmt_start); + if (fmt_end[0] == 0) // If we only have leading decoration, we don't need to copy the data. + return fmt_start; + ImStrncpy(buf, fmt_start, ImMin((size_t)(fmt_end - fmt_start) + 1, buf_size)); + return buf; +} + +// Parse display precision back from the display format string +// FIXME: This is still used by some navigation code path to infer a minimum tweak step, but we should aim to rework widgets so it isn't needed. +int ImParseFormatPrecision(const char* fmt, int default_precision) +{ + fmt = ImParseFormatFindStart(fmt); + if (fmt[0] != '%') + return default_precision; + fmt++; + while (*fmt >= '0' && *fmt <= '9') + fmt++; + int precision = INT_MAX; + if (*fmt == '.') + { + fmt = ImAtoi(fmt + 1, &precision); + if (precision < 0 || precision > 99) + precision = default_precision; + } + if (*fmt == 'e' || *fmt == 'E') // Maximum precision with scientific notation + precision = -1; + if ((*fmt == 'g' || *fmt == 'G') && precision == INT_MAX) + precision = -1; + return (precision == INT_MAX) ? default_precision : precision; +} + +// Create text input in place of another active widget (e.g. used when doing a CTRL+Click on drag/slider widgets) +// FIXME: Facilitate using this in variety of other situations. +bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format) +{ + ImGuiContext& g = *GImGui; + + // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id. + // We clear ActiveID on the first frame to allow the InputText() taking it back. + const bool init = (g.TempInputTextId != id); + if (init) + ClearActiveID(); + + char fmt_buf[32]; + char data_buf[32]; + format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); + DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); + ImStrTrimBlanks(data_buf); + + g.CurrentWindow->DC.CursorPos = bb.Min; + ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; + flags |= ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); + bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags); + if (init) + { + // First frame we started displaying the InputText widget, we expect it to take the active id. + IM_ASSERT(g.ActiveId == id); + g.TempInputTextId = g.ActiveId; + } + if (value_changed) + { + value_changed = DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); + if (value_changed) + MarkItemEdited(id); + } + return value_changed; +} + +// Note: p_data, p_step, p_step_fast are _pointers_ to a memory address holding the data. For an Input widget, p_step and p_step_fast are optional. +// Read code of e.g. InputFloat(), InputInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. +bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + ImGuiStyle& style = g.Style; + + if (format == NULL) + format = DataTypeGetInfo(data_type)->PrintFmt; + + char buf[64]; + DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); + + bool value_changed = false; + if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) + flags |= ImGuiInputTextFlags_CharsDecimal; + flags |= ImGuiInputTextFlags_AutoSelectAll; + flags |= ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselve by comparing the actual data rather than the string. + + if (p_step != NULL) + { + const float button_size = GetFrameHeight(); + + BeginGroup(); // The only purpose of the group here is to allow the caller to query item data e.g. IsItemActive() + PushID(label); + SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); + if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view + value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format); + + // Step buttons + const ImVec2 backup_frame_padding = style.FramePadding; + style.FramePadding.x = style.FramePadding.y; + ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups; + if (flags & ImGuiInputTextFlags_ReadOnly) + button_flags |= ImGuiButtonFlags_Disabled; + SameLine(0, style.ItemInnerSpacing.x); + if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) + { + DataTypeApplyOp(data_type, '-', p_data, p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); + value_changed = true; + } + SameLine(0, style.ItemInnerSpacing.x); + if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) + { + DataTypeApplyOp(data_type, '+', p_data, p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); + value_changed = true; + } + + const char* label_end = FindRenderedTextEnd(label); + if (label != label_end) + { + SameLine(0, style.ItemInnerSpacing.x); + TextEx(label, label_end); + } + style.FramePadding = backup_frame_padding; + + PopID(); + EndGroup(); + } + else + { + if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) + value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format); + } + if (value_changed) + MarkItemEdited(window->DC.LastItemId); + + return value_changed; +} + +bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + bool value_changed = false; + BeginGroup(); + PushID(label); + PushMultiItemsWidths(components, CalcItemWidth()); + size_t type_size = GDataTypeInfo[data_type].Size; + for (int i = 0; i < components; i++) + { + PushID(i); + if (i > 0) + SameLine(0, g.Style.ItemInnerSpacing.x); + value_changed |= InputScalar("", data_type, p_data, p_step, p_step_fast, format, flags); + PopID(); + PopItemWidth(); + p_data = (void*)((char*)p_data + type_size); + } + PopID(); + + const char* label_end = FindRenderedTextEnd(label); + if (label != label_end) + { + SameLine(0.0f, g.Style.ItemInnerSpacing.x); + TextEx(label, label_end); + } + + EndGroup(); + return value_changed; +} + +bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags) +{ + flags |= ImGuiInputTextFlags_CharsScientific; + return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step>0.0f ? &step : NULL), (void*)(step_fast>0.0f ? &step_fast : NULL), format, flags); +} + +bool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags); +} + +bool ImGui::InputFloat3(const char* label, float v[3], const char* format, ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags); +} + +bool ImGui::InputFloat4(const char* label, float v[4], const char* format, ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); +} + +// Prefer using "const char* format" directly, which is more flexible and consistent with other API. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags) +{ + char format[16] = "%f"; + if (decimal_precision >= 0) + ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); + return InputFloat(label, v, step, step_fast, format, flags); +} + +bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags) +{ + char format[16] = "%f"; + if (decimal_precision >= 0) + ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); + return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags); +} + +bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags) +{ + char format[16] = "%f"; + if (decimal_precision >= 0) + ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); + return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags); +} + +bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags) +{ + char format[16] = "%f"; + if (decimal_precision >= 0) + ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); + return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); +} +#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +bool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiInputTextFlags flags) +{ + // Hexadecimal input provided as a convenience but the flag name is awkward. Typically you'd use InputText() to parse your own data, if you want to handle prefixes. + const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? "%08X" : "%d"; + return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step>0 ? &step : NULL), (void*)(step_fast>0 ? &step_fast : NULL), format, flags); +} + +bool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_S32, v, 2, NULL, NULL, "%d", flags); +} + +bool ImGui::InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_S32, v, 3, NULL, NULL, "%d", flags); +} + +bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags) +{ + return InputScalarN(label, ImGuiDataType_S32, v, 4, NULL, NULL, "%d", flags); +} + +bool ImGui::InputDouble(const char* label, double* v, double step, double step_fast, const char* format, ImGuiInputTextFlags flags) +{ + flags |= ImGuiInputTextFlags_CharsScientific; + return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step>0.0 ? &step : NULL), (void*)(step_fast>0.0 ? &step_fast : NULL), format, flags); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: InputText, InputTextMultiline, InputTextWithHint +//------------------------------------------------------------------------- +// - InputText() +// - InputTextWithHint() +// - InputTextMultiline() +// - InputTextEx() [Internal] +//------------------------------------------------------------------------- + +bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() + return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0,0), flags, callback, user_data); +} + +bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + return InputTextEx(label, NULL, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data); +} + +bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() + return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data); +} + +static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end) +{ + int line_count = 0; + const char* s = text_begin; + while (char c = *s++) // We are only matching for \n so we can ignore UTF-8 decoding + if (c == '\n') + line_count++; + s--; + if (s[0] != '\n' && s[0] != '\r') + line_count++; + *out_text_end = s; + return line_count; +} + +static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line) +{ + ImGuiContext& g = *GImGui; + ImFont* font = g.Font; + const float line_height = g.FontSize; + const float scale = line_height / font->FontSize; + + ImVec2 text_size = ImVec2(0,0); + float line_width = 0.0f; + + const ImWchar* s = text_begin; + while (s < text_end) + { + unsigned int c = (unsigned int)(*s++); + if (c == '\n') + { + text_size.x = ImMax(text_size.x, line_width); + text_size.y += line_height; + line_width = 0.0f; + if (stop_on_new_line) + break; + continue; + } + if (c == '\r') + continue; + + const float char_width = font->GetCharAdvance((ImWchar)c) * scale; + line_width += char_width; + } + + if (text_size.x < line_width) + text_size.x = line_width; + + if (out_offset) + *out_offset = ImVec2(line_width, text_size.y + line_height); // offset allow for the possibility of sitting after a trailing \n + + if (line_width > 0 || text_size.y == 0.0f) // whereas size.y will ignore the trailing \n + text_size.y += line_height; + + if (remaining) + *remaining = s; + + return text_size; +} + +// Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar) +namespace ImStb +{ + +static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return obj->CurLenW; } +static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->TextW[idx]; } +static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *GImGui; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } +static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; } +static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; +static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx) +{ + const ImWchar* text = obj->TextW.Data; + const ImWchar* text_remaining = NULL; + const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true); + r->x0 = 0.0f; + r->x1 = size.x; + r->baseline_y_delta = size.y; + r->ymin = 0.0f; + r->ymax = size.y; + r->num_chars = (int)(text_remaining - (text + line_start_idx)); +} + +static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } +static int is_word_boundary_from_right(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (is_separator( obj->TextW[idx-1] ) && !is_separator( obj->TextW[idx] ) ) : 1; } +static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } +#ifdef __APPLE__ // FIXME: Move setting to IO structure +static int is_word_boundary_from_left(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (!is_separator( obj->TextW[idx-1] ) && is_separator( obj->TextW[idx] ) ) : 1; } +static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } +#else +static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } +#endif +#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h +#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL + +static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) +{ + ImWchar* dst = obj->TextW.Data + pos; + + // We maintain our buffer length in both UTF-8 and wchar formats + obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n); + obj->CurLenW -= n; + + // Offset remaining text (FIXME-OPT: Use memmove) + const ImWchar* src = obj->TextW.Data + pos + n; + while (ImWchar c = *src++) + *dst++ = c; + *dst = '\0'; +} + +static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len) +{ + const bool is_resizable = (obj->UserFlags & ImGuiInputTextFlags_CallbackResize) != 0; + const int text_len = obj->CurLenW; + IM_ASSERT(pos <= text_len); + + const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text + new_text_len); + if (!is_resizable && (new_text_len_utf8 + obj->CurLenA + 1 > obj->BufCapacityA)) + return false; + + // Grow internal buffer if needed + if (new_text_len + text_len + 1 > obj->TextW.Size) + { + if (!is_resizable) + return false; + IM_ASSERT(text_len < obj->TextW.Size); + obj->TextW.resize(text_len + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1); + } + + ImWchar* text = obj->TextW.Data; + if (pos != text_len) + memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar)); + memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar)); + + obj->CurLenW += new_text_len; + obj->CurLenA += new_text_len_utf8; + obj->TextW[obj->CurLenW] = '\0'; + + return true; +} + +// We don't use an enum so we can build even with conflicting symbols (if another user of stb_textedit.h leak their STB_TEXTEDIT_K_* symbols) +#define STB_TEXTEDIT_K_LEFT 0x200000 // keyboard input to move cursor left +#define STB_TEXTEDIT_K_RIGHT 0x200001 // keyboard input to move cursor right +#define STB_TEXTEDIT_K_UP 0x200002 // keyboard input to move cursor up +#define STB_TEXTEDIT_K_DOWN 0x200003 // keyboard input to move cursor down +#define STB_TEXTEDIT_K_LINESTART 0x200004 // keyboard input to move cursor to start of line +#define STB_TEXTEDIT_K_LINEEND 0x200005 // keyboard input to move cursor to end of line +#define STB_TEXTEDIT_K_TEXTSTART 0x200006 // keyboard input to move cursor to start of text +#define STB_TEXTEDIT_K_TEXTEND 0x200007 // keyboard input to move cursor to end of text +#define STB_TEXTEDIT_K_DELETE 0x200008 // keyboard input to delete selection or character under cursor +#define STB_TEXTEDIT_K_BACKSPACE 0x200009 // keyboard input to delete selection or character left of cursor +#define STB_TEXTEDIT_K_UNDO 0x20000A // keyboard input to perform undo +#define STB_TEXTEDIT_K_REDO 0x20000B // keyboard input to perform redo +#define STB_TEXTEDIT_K_WORDLEFT 0x20000C // keyboard input to move cursor left one word +#define STB_TEXTEDIT_K_WORDRIGHT 0x20000D // keyboard input to move cursor right one word +#define STB_TEXTEDIT_K_SHIFT 0x400000 + +#define STB_TEXTEDIT_IMPLEMENTATION +#include "imstb_textedit.h" + +} + +void ImGuiInputTextState::OnKeyPressed(int key) +{ + stb_textedit_key(this, &Stb, key); + CursorFollow = true; + CursorAnimReset(); +} + +ImGuiInputTextCallbackData::ImGuiInputTextCallbackData() +{ + memset(this, 0, sizeof(*this)); +} + +// Public API to manipulate UTF-8 text +// We expose UTF-8 to the user (unlike the STB_TEXTEDIT_* functions which are manipulating wchar) +// FIXME: The existence of this rarely exercised code path is a bit of a nuisance. +void ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count) +{ + IM_ASSERT(pos + bytes_count <= BufTextLen); + char* dst = Buf + pos; + const char* src = Buf + pos + bytes_count; + while (char c = *src++) + *dst++ = c; + *dst = '\0'; + + if (CursorPos + bytes_count >= pos) + CursorPos -= bytes_count; + else if (CursorPos >= pos) + CursorPos = pos; + SelectionStart = SelectionEnd = CursorPos; + BufDirty = true; + BufTextLen -= bytes_count; +} + +void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end) +{ + const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0; + const int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)strlen(new_text); + if (new_text_len + BufTextLen >= BufSize) + { + if (!is_resizable) + return; + + // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the midly similar code (until we remove the U16 buffer alltogether!) + ImGuiContext& g = *GImGui; + ImGuiInputTextState* edit_state = &g.InputTextState; + IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID); + IM_ASSERT(Buf == edit_state->TextA.Data); + int new_buf_size = BufTextLen + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1; + edit_state->TextA.reserve(new_buf_size + 1); + Buf = edit_state->TextA.Data; + BufSize = edit_state->BufCapacityA = new_buf_size; + } + + if (BufTextLen != pos) + memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos)); + memcpy(Buf + pos, new_text, (size_t)new_text_len * sizeof(char)); + Buf[BufTextLen + new_text_len] = '\0'; + + if (CursorPos >= pos) + CursorPos += new_text_len; + SelectionStart = SelectionEnd = CursorPos; + BufDirty = true; + BufTextLen += new_text_len; +} + +// Return false to discard a character. +static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + unsigned int c = *p_char; + + // Filter non-printable (NB: isprint is unreliable! see #2467) + if (c < 0x20) + { + bool pass = false; + pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); + pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); + if (!pass) + return false; + } + + // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) + if (c == 127) + return false; + + // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) + if (c >= 0xE000 && c <= 0xF8FF) + return false; + + // Generic named filters + if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) + { + if (flags & ImGuiInputTextFlags_CharsDecimal) + if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/')) + return false; + + if (flags & ImGuiInputTextFlags_CharsScientific) + if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E')) + return false; + + if (flags & ImGuiInputTextFlags_CharsHexadecimal) + if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) + return false; + + if (flags & ImGuiInputTextFlags_CharsUppercase) + if (c >= 'a' && c <= 'z') + *p_char = (c += (unsigned int)('A'-'a')); + + if (flags & ImGuiInputTextFlags_CharsNoBlank) + if (ImCharIsBlankW(c)) + return false; + } + + // Custom callback filter + if (flags & ImGuiInputTextFlags_CallbackCharFilter) + { + ImGuiInputTextCallbackData callback_data; + memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); + callback_data.EventFlag = ImGuiInputTextFlags_CallbackCharFilter; + callback_data.EventChar = (ImWchar)c; + callback_data.Flags = flags; + callback_data.UserData = user_data; + if (callback(&callback_data) != 0) + return false; + *p_char = callback_data.EventChar; + if (!callback_data.EventChar) + return false; + } + + return true; +} + +// Edit a string of text +// - buf_size account for the zero-terminator, so a buf_size of 6 can hold "Hello" but not "Hello!". +// This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match +// Note that in std::string world, capacity() would omit 1 byte used by the zero-terminator. +// - When active, hold on a privately held copy of the text (and apply back to 'buf'). So changing 'buf' while the InputText is active has no effect. +// - If you want to use ImGui::InputText() with std::string, see misc/cpp/imgui_stdlib.h +// (FIXME: Rather confusing and messy function, among the worse part of our codebase, expecting to rewrite a V2 at some point.. Partly because we are +// doing UTF8 > U16 > UTF8 conversions on the go to easily interface with stb_textedit. Ideally should stay in UTF-8 all the time. See https://github.com/nothings/stb/issues/188) +bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* callback_user_data) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys) + IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key) + + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + const ImGuiStyle& style = g.Style; + + const bool RENDER_SELECTION_WHEN_INACTIVE = false; + const bool is_multiline = (flags & ImGuiInputTextFlags_Multiline) != 0; + const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0; + const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0; + const bool is_undoable = (flags & ImGuiInputTextFlags_NoUndoRedo) == 0; + const bool is_resizable = (flags & ImGuiInputTextFlags_CallbackResize) != 0; + if (is_resizable) + IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag! + + if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope, + BeginGroup(); + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + const ImVec2 frame_size = CalcItemSize(size_arg, CalcItemWidth(), (is_multiline ? g.FontSize * 8.0f : label_size.y) + style.FramePadding.y*2.0f); // Arbitrary default of 8 lines high for multi-line + const ImVec2 total_size = ImVec2(frame_size.x + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), frame_size.y); + + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); + const ImRect total_bb(frame_bb.Min, frame_bb.Min + total_size); + + ImGuiWindow* draw_window = window; + ImVec2 inner_size = frame_size; + if (is_multiline) + { + if (!ItemAdd(total_bb, id, &frame_bb)) + { + ItemSize(total_bb, style.FramePadding.y); + EndGroup(); + return false; + } + if (!BeginChildFrame(id, frame_bb.GetSize())) + { + EndChildFrame(); + EndGroup(); + return false; + } + draw_window = g.CurrentWindow; // Child window + draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight + inner_size.x -= draw_window->ScrollbarSizes.x; + } + else + { + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id, &frame_bb)) + return false; + } + const bool hovered = ItemHoverable(frame_bb, id); + if (hovered) + g.MouseCursor = ImGuiMouseCursor_TextInput; + + // NB: we are only allowed to access 'edit_state' if we are the active widget. + ImGuiInputTextState* state = NULL; + if (g.InputTextState.ID == id) + state = &g.InputTextState; + + const bool focus_requested = FocusableItemRegister(window, id); + const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterAll == window->DC.FocusCounterAll); + const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code; + + const bool user_clicked = hovered && io.MouseClicked[0]; + const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_NavKeyboard)); + const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetScrollbarID(draw_window, ImGuiAxis_Y); + const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetScrollbarID(draw_window, ImGuiAxis_Y); + + bool clear_active_id = false; + bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline); + + const bool init_make_active = (focus_requested || user_clicked || user_scroll_finish || user_nav_input_start); + const bool init_state = (init_make_active || user_scroll_active); + if (init_state && g.ActiveId != id) + { + // Access state even if we don't own it yet. + state = &g.InputTextState; + state->CursorAnimReset(); + + // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar) + // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode) + const int buf_len = (int)strlen(buf); + state->InitialTextA.resize(buf_len + 1); // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string. + memcpy(state->InitialTextA.Data, buf, buf_len + 1); + + // Start edition + const char* buf_end = NULL; + state->TextW.resize(buf_size + 1); // wchar count <= UTF-8 count. we use +1 to make sure that .Data is always pointing to at least an empty string. + state->TextA.resize(0); + state->TextAIsValid = false; // TextA is not valid yet (we will display buf until then) + state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, buf_size, buf, NULL, &buf_end); + state->CurLenA = (int)(buf_end - buf); // We can't get the result from ImStrncpy() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8. + + // Preserve cursor position and undo/redo stack if we come back to same widget + // FIXME: For non-readonly widgets we might be able to require that TextAIsValid && TextA == buf ? (untested) and discard undo stack if user buffer has changed. + const bool recycle_state = (state->ID == id); + if (recycle_state) + { + // Recycle existing cursor/selection/undo stack but clamp position + // Note a single mouse click will override the cursor/position immediately by calling stb_textedit_click handler. + state->CursorClamp(); + } + else + { + state->ID = id; + state->ScrollX = 0.0f; + stb_textedit_initialize_state(&state->Stb, !is_multiline); + if (!is_multiline && focus_requested_by_code) + select_all = true; + } + if (flags & ImGuiInputTextFlags_AlwaysInsertMode) + state->Stb.insert_mode = 1; + if (!is_multiline && (focus_requested_by_tab || (user_clicked && io.KeyCtrl))) + select_all = true; + } + + if (g.ActiveId != id && init_make_active) + { + IM_ASSERT(state && state->ID == id); + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + + // Declare our inputs + IM_ASSERT(ImGuiNavInput_COUNT < 32); + g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); + if (is_multiline || (flags & ImGuiInputTextFlags_CallbackHistory)) + g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); + g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); + g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Home) | ((ImU64)1 << ImGuiKey_End); + if (is_multiline) + g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_PageUp) | ((ImU64)1 << ImGuiKey_PageDown); // FIXME-NAV: Page up/down actually not supported yet by widget, but claim them ahead. + if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. + g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Tab); + } + + // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function) + if (g.ActiveId == id && state == NULL) + ClearActiveID(); + + // Release focus when we click outside + if (g.ActiveId == id && io.MouseClicked[0] && !init_state && !init_make_active) //-V560 + clear_active_id = true; + + // Lock the decision of whether we are going to take the path displaying the cursor or selection + const bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active); + bool render_selection = state && state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); + bool value_changed = false; + bool enter_pressed = false; + + // When read-only we always use the live data passed to the function + // FIXME-OPT: Because our selection/cursor code currently needs the wide text we need to convert it when active, which is not ideal :( + if (is_readonly && state != NULL && (render_cursor || render_selection)) + { + const char* buf_end = NULL; + state->TextW.resize(buf_size + 1); + state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, buf, NULL, &buf_end); + state->CurLenA = (int)(buf_end - buf); + state->CursorClamp(); + render_selection &= state->HasSelection(); + } + + // Select the buffer to render. + const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state && state->TextAIsValid; + const bool is_displaying_hint = (hint != NULL && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0); + + // Password pushes a temporary font with only a fallback glyph + if (is_password && !is_displaying_hint) + { + const ImFontGlyph* glyph = g.Font->FindGlyph('*'); + ImFont* password_font = &g.InputTextPasswordFont; + password_font->FontSize = g.Font->FontSize; + password_font->Scale = g.Font->Scale; + password_font->DisplayOffset = g.Font->DisplayOffset; + password_font->Ascent = g.Font->Ascent; + password_font->Descent = g.Font->Descent; + password_font->ContainerAtlas = g.Font->ContainerAtlas; + password_font->FallbackGlyph = glyph; + password_font->FallbackAdvanceX = glyph->AdvanceX; + IM_ASSERT(password_font->Glyphs.empty() && password_font->IndexAdvanceX.empty() && password_font->IndexLookup.empty()); + PushFont(password_font); + } + + // Process mouse inputs and character inputs + int backup_current_text_length = 0; + if (g.ActiveId == id) + { + IM_ASSERT(state != NULL); + backup_current_text_length = state->CurLenA; + state->BufCapacityA = buf_size; + state->UserFlags = flags; + state->UserCallback = callback; + state->UserCallbackData = callback_user_data; + + // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget. + // Down the line we should have a cleaner library-wide concept of Selected vs Active. + g.ActiveIdAllowOverlap = !io.MouseDown[0]; + g.WantTextInputNextFrame = 1; + + // Edit in progress + const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; + const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize*0.5f)); + + const bool is_osx = io.ConfigMacOSXBehaviors; + if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0])) + { + state->SelectAll(); + state->SelectedAllMouseLock = true; + } + else if (hovered && is_osx && io.MouseDoubleClicked[0]) + { + // Double-click select a word only, OS X style (by simulating keystrokes) + state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT); + state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); + } + else if (io.MouseClicked[0] && !state->SelectedAllMouseLock) + { + if (hovered) + { + stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); + state->CursorAnimReset(); + } + } + else if (io.MouseDown[0] && !state->SelectedAllMouseLock && (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f)) + { + stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y); + state->CursorAnimReset(); + state->CursorFollow = true; + } + if (state->SelectedAllMouseLock && !io.MouseDown[0]) + state->SelectedAllMouseLock = false; + + // It is ill-defined whether the back-end needs to send a \t character when pressing the TAB keys. + // Win32 and GLFW naturally do it but not SDL. + const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); + if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) + if (!io.InputQueueCharacters.contains('\t')) + { + unsigned int c = '\t'; // Insert TAB + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + state->OnKeyPressed((int)c); + } + + // Process regular text input (before we check for Return because using some IME will effectively send a Return?) + // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters. + if (io.InputQueueCharacters.Size > 0) + { + if (!ignore_char_inputs && !is_readonly && !user_nav_input_start) + for (int n = 0; n < io.InputQueueCharacters.Size; n++) + { + // Insert character if they pass filtering + unsigned int c = (unsigned int)io.InputQueueCharacters[n]; + if (c == '\t' && io.KeyShift) + continue; + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + state->OnKeyPressed((int)c); + } + + // Consume characters + io.InputQueueCharacters.resize(0); + } + } + + // Process other shortcuts/key-presses + bool cancel_edit = false; + if (g.ActiveId == id && !g.ActiveIdIsJustActivated && !clear_active_id) + { + IM_ASSERT(state != NULL); + const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); + const bool is_osx = io.ConfigMacOSXBehaviors; + const bool is_shortcut_key = (is_osx ? (io.KeySuper && !io.KeyCtrl) : (io.KeyCtrl && !io.KeySuper)) && !io.KeyAlt && !io.KeyShift; // OS X style: Shortcuts using Cmd/Super instead of Ctrl + const bool is_osx_shift_shortcut = is_osx && io.KeySuper && io.KeyShift && !io.KeyCtrl && !io.KeyAlt; + const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl + const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End + const bool is_ctrl_key_only = io.KeyCtrl && !io.KeyShift && !io.KeyAlt && !io.KeySuper; + const bool is_shift_key_only = io.KeyShift && !io.KeyCtrl && !io.KeyAlt && !io.KeySuper; + + const bool is_cut = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_X)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Delete))) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); + const bool is_copy = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_C)) || (is_ctrl_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_password && (!is_multiline || state->HasSelection()); + const bool is_paste = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_V)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_readonly; + const bool is_undo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Z)) && !is_readonly && is_undoable); + const bool is_redo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Y)) || (is_osx_shift_shortcut && IsKeyPressedMap(ImGuiKey_Z))) && !is_readonly && is_undoable; + + if (IsKeyPressedMap(ImGuiKey_LeftArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_DownArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMin(draw_window->Scroll.y + g.FontSize, GetScrollMaxY())); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTEND : STB_TEXTEDIT_K_DOWN) | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_Backspace) && !is_readonly) + { + if (!state->HasSelection()) + { + if (is_wordmove_key_down) + state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT|STB_TEXTEDIT_K_SHIFT); + else if (is_osx && io.KeySuper && !io.KeyAlt && !io.KeyCtrl) + state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART|STB_TEXTEDIT_K_SHIFT); + } + state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); + } + else if (IsKeyPressedMap(ImGuiKey_Enter) || IsKeyPressedMap(ImGuiKey_KeyPadEnter)) + { + bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0; + if (!is_multiline || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl)) + { + enter_pressed = clear_active_id = true; + } + else if (!is_readonly) + { + unsigned int c = '\n'; // Insert new line + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + state->OnKeyPressed((int)c); + } + } + else if (IsKeyPressedMap(ImGuiKey_Escape)) + { + clear_active_id = cancel_edit = true; + } + else if (is_undo || is_redo) + { + state->OnKeyPressed(is_undo ? STB_TEXTEDIT_K_UNDO : STB_TEXTEDIT_K_REDO); + state->ClearSelection(); + } + else if (is_shortcut_key && IsKeyPressedMap(ImGuiKey_A)) + { + state->SelectAll(); + state->CursorFollow = true; + } + else if (is_cut || is_copy) + { + // Cut, Copy + if (io.SetClipboardTextFn) + { + const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0; + const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW; + const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1; + char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char)); + ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie); + SetClipboardText(clipboard_data); + MemFree(clipboard_data); + } + if (is_cut) + { + if (!state->HasSelection()) + state->SelectAll(); + state->CursorFollow = true; + stb_textedit_cut(state, &state->Stb); + } + } + else if (is_paste) + { + if (const char* clipboard = GetClipboardText()) + { + // Filter pasted buffer + const int clipboard_len = (int)strlen(clipboard); + ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar)); + int clipboard_filtered_len = 0; + for (const char* s = clipboard; *s; ) + { + unsigned int c; + s += ImTextCharFromUtf8(&c, s, NULL); + if (c == 0) + break; + if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + continue; + clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; + } + clipboard_filtered[clipboard_filtered_len] = 0; + if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation + { + stb_textedit_paste(state, &state->Stb, clipboard_filtered, clipboard_filtered_len); + state->CursorFollow = true; + } + MemFree(clipboard_filtered); + } + } + + // Update render selection flag after events have been handled, so selection highlight can be displayed during the same frame. + render_selection |= state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); + } + + // Process callbacks and apply result back to user's buffer. + if (g.ActiveId == id) + { + IM_ASSERT(state != NULL); + const char* apply_new_text = NULL; + int apply_new_text_length = 0; + if (cancel_edit) + { + // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. + if (!is_readonly && strcmp(buf, state->InitialTextA.Data) != 0) + { + apply_new_text = state->InitialTextA.Data; + apply_new_text_length = state->InitialTextA.Size - 1; + } + } + + // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. + // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. Also this allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage. + bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); + if (apply_edit_back_to_user_buffer) + { + // Apply new value immediately - copy modified buffer back + // Note that as soon as the input box is active, the in-widget value gets priority over any underlying modification of the input buffer + // FIXME: We actually always render 'buf' when calling DrawList->AddText, making the comment above incorrect. + // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks. + if (!is_readonly) + { + state->TextAIsValid = true; + state->TextA.resize(state->TextW.Size * 4 + 1); + ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); + } + + // User callback + if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0) + { + IM_ASSERT(callback != NULL); + + // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment. + ImGuiInputTextFlags event_flag = 0; + ImGuiKey event_key = ImGuiKey_COUNT; + if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab)) + { + event_flag = ImGuiInputTextFlags_CallbackCompletion; + event_key = ImGuiKey_Tab; + } + else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow)) + { + event_flag = ImGuiInputTextFlags_CallbackHistory; + event_key = ImGuiKey_UpArrow; + } + else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow)) + { + event_flag = ImGuiInputTextFlags_CallbackHistory; + event_key = ImGuiKey_DownArrow; + } + else if (flags & ImGuiInputTextFlags_CallbackAlways) + event_flag = ImGuiInputTextFlags_CallbackAlways; + + if (event_flag) + { + ImGuiInputTextCallbackData callback_data; + memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); + callback_data.EventFlag = event_flag; + callback_data.Flags = flags; + callback_data.UserData = callback_user_data; + + callback_data.EventKey = event_key; + callback_data.Buf = state->TextA.Data; + callback_data.BufTextLen = state->CurLenA; + callback_data.BufSize = state->BufCapacityA; + callback_data.BufDirty = false; + + // We have to convert from wchar-positions to UTF-8-positions, which can be pretty slow (an incentive to ditch the ImWchar buffer, see https://github.com/nothings/stb/issues/188) + ImWchar* text = state->TextW.Data; + const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromStr(text, text + state->Stb.cursor); + const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_start); + const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_end); + + // Call user code + callback(&callback_data); + + // Read back what user may have modified + IM_ASSERT(callback_data.Buf == state->TextA.Data); // Invalid to modify those fields + IM_ASSERT(callback_data.BufSize == state->BufCapacityA); + IM_ASSERT(callback_data.Flags == flags); + if (callback_data.CursorPos != utf8_cursor_pos) { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; } + if (callback_data.SelectionStart != utf8_selection_start) { state->Stb.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); } + if (callback_data.SelectionEnd != utf8_selection_end) { state->Stb.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } + if (callback_data.BufDirty) + { + IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! + if (callback_data.BufTextLen > backup_current_text_length && is_resizable) + state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length)); + state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, callback_data.Buf, NULL); + state->CurLenA = callback_data.BufTextLen; // Assume correct length and valid UTF-8 from user, saves us an extra strlen() + state->CursorAnimReset(); + } + } + } + + // Will copy result string if modified + if (!is_readonly && strcmp(state->TextA.Data, buf) != 0) + { + apply_new_text = state->TextA.Data; + apply_new_text_length = state->CurLenA; + } + } + + // Copy result to user buffer + if (apply_new_text) + { + IM_ASSERT(apply_new_text_length >= 0); + if (backup_current_text_length != apply_new_text_length && is_resizable) + { + ImGuiInputTextCallbackData callback_data; + callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; + callback_data.Flags = flags; + callback_data.Buf = buf; + callback_data.BufTextLen = apply_new_text_length; + callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1); + callback_data.UserData = callback_user_data; + callback(&callback_data); + buf = callback_data.Buf; + buf_size = callback_data.BufSize; + apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); + IM_ASSERT(apply_new_text_length <= buf_size); + } + + // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. + ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); + value_changed = true; + } + + // Clear temporary user storage + state->UserFlags = 0; + state->UserCallback = NULL; + state->UserCallbackData = NULL; + } + + // Release active ID at the end of the function (so e.g. pressing Return still does a final application of the value) + if (clear_active_id && g.ActiveId == id) + ClearActiveID(); + + // Render frame + if (!is_multiline) + { + RenderNavHighlight(frame_bb, id); + RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); + } + + const ImVec4 clip_rect(frame_bb.Min.x, frame_bb.Min.y, frame_bb.Min.x + inner_size.x, frame_bb.Min.y + inner_size.y); // Not using frame_bb.Max because we have adjusted size + ImVec2 draw_pos = is_multiline ? draw_window->DC.CursorPos : frame_bb.Min + style.FramePadding; + ImVec2 text_size(0.0f, 0.0f); + + // Set upper limit of single-line InputTextEx() at 2 million characters strings. The current pathological worst case is a long line + // without any carriage return, which would makes ImFont::RenderText() reserve too many vertices and probably crash. Avoid it altogether. + // Note that we only use this limit on single-line InputText(), so a pathologically large line on a InputTextMultiline() would still crash. + const int buf_display_max_length = 2 * 1024 * 1024; + const char* buf_display = buf_display_from_state ? state->TextA.Data : buf; //-V595 + const char* buf_display_end = NULL; // We have specialized paths below for setting the length + if (is_displaying_hint) + { + buf_display = hint; + buf_display_end = hint + strlen(hint); + } + + // Render text. We currently only render selection when the widget is active or while scrolling. + // FIXME: We could remove the '&& render_cursor' to keep rendering selection when inactive. + if (render_cursor || render_selection) + { + IM_ASSERT(state != NULL); + if (!is_displaying_hint) + buf_display_end = buf_display + state->CurLenA; + + // Render text (with cursor and selection) + // This is going to be messy. We need to: + // - Display the text (this alone can be more easily clipped) + // - Handle scrolling, highlight selection, display cursor (those all requires some form of 1d->2d cursor position calculation) + // - Measure text height (for scrollbar) + // We are attempting to do most of that in **one main pass** to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort) + // FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8. + const ImWchar* text_begin = state->TextW.Data; + ImVec2 cursor_offset, select_start_offset; + + { + // Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions. + const ImWchar* searches_input_ptr[2] = { NULL, NULL }; + int searches_result_line_no[2] = { -1000, -1000 }; + int searches_remaining = 0; + if (render_cursor) + { + searches_input_ptr[0] = text_begin + state->Stb.cursor; + searches_result_line_no[0] = -1; + searches_remaining++; + } + if (render_selection) + { + searches_input_ptr[1] = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); + searches_result_line_no[1] = -1; + searches_remaining++; + } + + // Iterate all lines to find our line numbers + // In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter. + searches_remaining += is_multiline ? 1 : 0; + int line_count = 0; + //for (const ImWchar* s = text_begin; (s = (const ImWchar*)wcschr((const wchar_t*)s, (wchar_t)'\n')) != NULL; s++) // FIXME-OPT: Could use this when wchar_t are 16-bits + for (const ImWchar* s = text_begin; *s != 0; s++) + if (*s == '\n') + { + line_count++; + if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; if (--searches_remaining <= 0) break; } + if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; if (--searches_remaining <= 0) break; } + } + line_count++; + if (searches_result_line_no[0] == -1) + searches_result_line_no[0] = line_count; + if (searches_result_line_no[1] == -1) + searches_result_line_no[1] = line_count; + + // Calculate 2d position by finding the beginning of the line and measuring distance + cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x; + cursor_offset.y = searches_result_line_no[0] * g.FontSize; + if (searches_result_line_no[1] >= 0) + { + select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x; + select_start_offset.y = searches_result_line_no[1] * g.FontSize; + } + + // Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224) + if (is_multiline) + text_size = ImVec2(inner_size.x, line_count * g.FontSize); + } + + // Scroll + if (render_cursor && state->CursorFollow) + { + // Horizontal scroll in chunks of quarter width + if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll)) + { + const float scroll_increment_x = inner_size.x * 0.25f; + if (cursor_offset.x < state->ScrollX) + state->ScrollX = (float)(int)ImMax(0.0f, cursor_offset.x - scroll_increment_x); + else if (cursor_offset.x - inner_size.x >= state->ScrollX) + state->ScrollX = (float)(int)(cursor_offset.x - inner_size.x + scroll_increment_x); + } + else + { + state->ScrollX = 0.0f; + } + + // Vertical scroll + if (is_multiline) + { + float scroll_y = draw_window->Scroll.y; + if (cursor_offset.y - g.FontSize < scroll_y) + scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize); + else if (cursor_offset.y - inner_size.y >= scroll_y) + scroll_y = cursor_offset.y - inner_size.y; + draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag + draw_window->Scroll.y = scroll_y; + } + + state->CursorFollow = false; + } + + // Draw selection + const ImVec2 draw_scroll = ImVec2(state->ScrollX, 0.0f); + if (render_selection) + { + const ImWchar* text_selected_begin = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); + const ImWchar* text_selected_end = text_begin + ImMax(state->Stb.select_start, state->Stb.select_end); + + ImU32 bg_color = GetColorU32(ImGuiCol_TextSelectedBg, render_cursor ? 1.0f : 0.6f); // FIXME: current code flow mandate that render_cursor is always true here, we are leaving the transparent one for tests. + float bg_offy_up = is_multiline ? 0.0f : -1.0f; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection. + float bg_offy_dn = is_multiline ? 0.0f : 2.0f; + ImVec2 rect_pos = draw_pos + select_start_offset - draw_scroll; + for (const ImWchar* p = text_selected_begin; p < text_selected_end; ) + { + if (rect_pos.y > clip_rect.w + g.FontSize) + break; + if (rect_pos.y < clip_rect.y) + { + //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\n', text_selected_end - p); // FIXME-OPT: Could use this when wchar_t are 16-bits + //p = p ? p + 1 : text_selected_end; + while (p < text_selected_end) + if (*p++ == '\n') + break; + } + else + { + ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true); + if (rect_size.x <= 0.0f) rect_size.x = (float)(int)(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines + ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn)); + rect.ClipWith(clip_rect); + if (rect.Overlaps(clip_rect)) + draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color); + } + rect_pos.x = draw_pos.x - draw_scroll.x; + rect_pos.y += g.FontSize; + } + } + + // We test for 'buf_display_max_length' as a way to avoid some pathological cases (e.g. single-line 1 MB string) which would make ImDrawList crash. + if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length) + { + ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text); + draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect); + } + + // Draw blinking cursor + if (render_cursor) + { + state->CursorAnim += io.DeltaTime; + bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f; + ImVec2 cursor_screen_pos = draw_pos + cursor_offset - draw_scroll; + ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f); + if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect)) + draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_Text)); + + // Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.) + if (!is_readonly) + g.PlatformImePos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize); + } + } + else + { + // Render text only (no selection, no cursor) + if (is_multiline) + text_size = ImVec2(inner_size.x, InputTextCalcTextLenAndLineCount(buf_display, &buf_display_end) * g.FontSize); // We don't need width + else if (!is_displaying_hint && g.ActiveId == id) + buf_display_end = buf_display + state->CurLenA; + else if (!is_displaying_hint) + buf_display_end = buf_display + strlen(buf_display); + + if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length) + { + ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text); + draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos, col, buf_display, buf_display_end, 0.0f, is_multiline ? NULL : &clip_rect); + } + } + + if (is_multiline) + { + Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line + EndChildFrame(); + EndGroup(); + } + + if (is_password && !is_displaying_hint) + PopFont(); + + // Log as text + if (g.LogEnabled && !(is_password && !is_displaying_hint)) + LogRenderedText(&draw_pos, buf_display, buf_display_end); + + if (label_size.x > 0) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + if (value_changed && !(flags & ImGuiInputTextFlags_NoMarkEdited)) + MarkItemEdited(id); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0) + return enter_pressed; + else + return value_changed; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. +//------------------------------------------------------------------------- +// - ColorEdit3() +// - ColorEdit4() +// - ColorPicker3() +// - RenderColorRectWithAlphaCheckerboard() [Internal] +// - ColorPicker4() +// - ColorButton() +// - SetColorEditOptions() +// - ColorTooltip() [Internal] +// - ColorEditOptionsPopup() [Internal] +// - ColorPickerOptionsPopup() [Internal] +//------------------------------------------------------------------------- + +bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags) +{ + return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha); +} + +// Edit colors components (each component in 0.0f..1.0f range). +// See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. +// With typical options: Left-click on colored square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item. +bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const float square_sz = GetFrameHeight(); + const float w_full = CalcItemWidth(); + const float w_button = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x); + const float w_inputs = w_full - w_button; + const char* label_display_end = FindRenderedTextEnd(label); + g.NextItemData.ClearFlags(); + + BeginGroup(); + PushID(label); + + // If we're not showing any slider there's no point in doing any HSV conversions + const ImGuiColorEditFlags flags_untouched = flags; + if (flags & ImGuiColorEditFlags_NoInputs) + flags = (flags & (~ImGuiColorEditFlags__DisplayMask)) | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoOptions; + + // Context menu: display and modify options (before defaults are applied) + if (!(flags & ImGuiColorEditFlags_NoOptions)) + ColorEditOptionsPopup(col, flags); + + // Read stored options + if (!(flags & ImGuiColorEditFlags__DisplayMask)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DisplayMask); + if (!(flags & ImGuiColorEditFlags__DataTypeMask)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DataTypeMask); + if (!(flags & ImGuiColorEditFlags__PickerMask)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags__PickerMask); + if (!(flags & ImGuiColorEditFlags__InputMask)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags__InputMask); + flags |= (g.ColorEditOptions & ~(ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask)); + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check that only 1 is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected + + const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0; + const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0; + const int components = alpha ? 4 : 3; + + // Convert to the formats we need + float f[4] = { col[0], col[1], col[2], alpha ? col[3] : 1.0f }; + if ((flags & ImGuiColorEditFlags_InputHSV) && (flags & ImGuiColorEditFlags_DisplayRGB)) + ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); + else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV)) + { + // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. + ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); + if (f[1] == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) + f[0] = g.ColorEditLastHue; + } + int i[4] = { IM_F32_TO_INT8_UNBOUND(f[0]), IM_F32_TO_INT8_UNBOUND(f[1]), IM_F32_TO_INT8_UNBOUND(f[2]), IM_F32_TO_INT8_UNBOUND(f[3]) }; + + bool value_changed = false; + bool value_changed_as_float = false; + + const ImVec2 pos = window->DC.CursorPos; + const float inputs_offset_x = (style.ColorButtonPosition == ImGuiDir_Left) ? w_button : 0.0f; + window->DC.CursorPos.x = pos.x + inputs_offset_x; + + if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) + { + // RGB/HSV 0..255 Sliders + const float w_item_one = ImMax(1.0f, (float)(int)((w_inputs - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); + const float w_item_last = ImMax(1.0f, (float)(int)(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + + const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); + static const char* ids[4] = { "##X", "##Y", "##Z", "##W" }; + static const char* fmt_table_int[3][4] = + { + { "%3d", "%3d", "%3d", "%3d" }, // Short display + { "R:%3d", "G:%3d", "B:%3d", "A:%3d" }, // Long display for RGBA + { "H:%3d", "S:%3d", "V:%3d", "A:%3d" } // Long display for HSVA + }; + static const char* fmt_table_float[3][4] = + { + { "%0.3f", "%0.3f", "%0.3f", "%0.3f" }, // Short display + { "R:%0.3f", "G:%0.3f", "B:%0.3f", "A:%0.3f" }, // Long display for RGBA + { "H:%0.3f", "S:%0.3f", "V:%0.3f", "A:%0.3f" } // Long display for HSVA + }; + const int fmt_idx = hide_prefix ? 0 : (flags & ImGuiColorEditFlags_DisplayHSV) ? 2 : 1; + + for (int n = 0; n < components; n++) + { + if (n > 0) + SameLine(0, style.ItemInnerSpacing.x); + SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last); + + // Disable Hue edit when Saturation is zero + const bool disable_hue_edit = (n == 0 && (flags & ImGuiColorEditFlags_DisplayHSV) && i[1] == 0); + if (flags & ImGuiColorEditFlags_Float) + { + value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, disable_hue_edit ? +FLT_MAX : 0.0f, disable_hue_edit ? -FLT_MAX : hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); + value_changed_as_float |= value_changed; + } + else + { + value_changed |= DragInt(ids[n], &i[n], 1.0f, disable_hue_edit ? INT_MAX : 0, disable_hue_edit ? INT_MIN : hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); + } + if (!(flags & ImGuiColorEditFlags_NoOptions)) + OpenPopupOnItemClick("context"); + } + } + else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) + { + // RGB Hexadecimal Input + char buf[64]; + if (alpha) + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255), ImClamp(i[3],0,255)); + else + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255)); + SetNextItemWidth(w_inputs); + if (InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) + { + value_changed = true; + char* p = buf; + while (*p == '#' || ImCharIsBlankA(*p)) + p++; + i[0] = i[1] = i[2] = i[3] = 0; + if (alpha) + sscanf(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned) + else + sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); + } + if (!(flags & ImGuiColorEditFlags_NoOptions)) + OpenPopupOnItemClick("context"); + } + + ImGuiWindow* picker_active_window = NULL; + if (!(flags & ImGuiColorEditFlags_NoSmallPreview)) + { + const float button_offset_x = ((flags & ImGuiColorEditFlags_NoInputs) || (style.ColorButtonPosition == ImGuiDir_Left)) ? 0.0f : w_inputs + style.ItemInnerSpacing.x; + window->DC.CursorPos = ImVec2(pos.x + button_offset_x, pos.y); + + const ImVec4 col_v4(col[0], col[1], col[2], alpha ? col[3] : 1.0f); + if (ColorButton("##ColorButton", col_v4, flags)) + { + if (!(flags & ImGuiColorEditFlags_NoPicker)) + { + // Store current color and open a picker + g.ColorPickerRef = col_v4; + OpenPopup("picker"); + SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1,style.ItemSpacing.y)); + } + } + if (!(flags & ImGuiColorEditFlags_NoOptions)) + OpenPopupOnItemClick("context"); + + if (BeginPopup("picker")) + { + picker_active_window = g.CurrentWindow; + if (label != label_display_end) + { + TextEx(label, label_display_end); + Spacing(); + } + ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; + ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; + SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes? + value_changed |= ColorPicker4("##picker", col, picker_flags, &g.ColorPickerRef.x); + EndPopup(); + } + } + + if (label != label_display_end && !(flags & ImGuiColorEditFlags_NoLabel)) + { + window->DC.CursorPos = ImVec2(pos.x + w_full + style.ItemInnerSpacing.x, pos.y + style.FramePadding.y); + TextEx(label, label_display_end); + } + + // Convert back + if (value_changed && picker_active_window == NULL) + { + if (!value_changed_as_float) + for (int n = 0; n < 4; n++) + f[n] = i[n] / 255.0f; + if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB)) + { + g.ColorEditLastHue = f[0]; + ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); + memcpy(g.ColorEditLastColor, f, sizeof(float) * 3); + } + if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV)) + ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); + + col[0] = f[0]; + col[1] = f[1]; + col[2] = f[2]; + if (alpha) + col[3] = f[3]; + } + + PopID(); + EndGroup(); + + // Drag and Drop Target + // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test. + if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) + { + bool accepted_drag_drop = false; + if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) + { + memcpy((float*)col, payload->Data, sizeof(float) * 3); // Preserve alpha if any //-V512 + value_changed = accepted_drag_drop = true; + } + if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F)) + { + memcpy((float*)col, payload->Data, sizeof(float) * components); + value_changed = accepted_drag_drop = true; + } + + // Drag-drop payloads are always RGB + if (accepted_drag_drop && (flags & ImGuiColorEditFlags_InputHSV)) + ColorConvertRGBtoHSV(col[0], col[1], col[2], col[0], col[1], col[2]); + EndDragDropTarget(); + } + + // When picker is being actively used, use its active id so IsItemActive() will function on ColorEdit4(). + if (picker_active_window && g.ActiveId != 0 && g.ActiveIdWindow == picker_active_window) + window->DC.LastItemId = g.ActiveId; + + if (value_changed) + MarkItemEdited(window->DC.LastItemId); + + return value_changed; +} + +bool ImGui::ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags) +{ + float col4[4] = { col[0], col[1], col[2], 1.0f }; + if (!ColorPicker4(label, col4, flags | ImGuiColorEditFlags_NoAlpha)) + return false; + col[0] = col4[0]; col[1] = col4[1]; col[2] = col4[2]; + return true; +} + +static inline ImU32 ImAlphaBlendColor(ImU32 col_a, ImU32 col_b) +{ + float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f; + int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t); + int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t); + int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t); + return IM_COL32(r, g, b, 0xFF); +} + +// Helper for ColorPicker4() +// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. +// I spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding alltogether. +void ImGui::RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF) + { + ImU32 col_bg1 = GetColorU32(ImAlphaBlendColor(IM_COL32(204,204,204,255), col)); + ImU32 col_bg2 = GetColorU32(ImAlphaBlendColor(IM_COL32(128,128,128,255), col)); + window->DrawList->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags); + + int yi = 0; + for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++) + { + float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y); + if (y2 <= y1) + continue; + for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f) + { + float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x); + if (x2 <= x1) + continue; + int rounding_corners_flags_cell = 0; + if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; } + if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; } + rounding_corners_flags_cell &= rounding_corners_flags; + window->DrawList->AddRectFilled(ImVec2(x1,y1), ImVec2(x2,y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell); + } + } + } + else + { + window->DrawList->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags); + } +} + +// Helper for ColorPicker4() +static void RenderArrowsForVerticalBar(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, float bar_w, float alpha) +{ + ImU32 alpha8 = IM_F32_TO_INT8_SAT(alpha); + ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + half_sz.x + 1, pos.y), ImVec2(half_sz.x + 2, half_sz.y + 1), ImGuiDir_Right, IM_COL32(0,0,0,alpha8)); + ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + half_sz.x, pos.y), half_sz, ImGuiDir_Right, IM_COL32(255,255,255,alpha8)); + ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + bar_w - half_sz.x - 1, pos.y), ImVec2(half_sz.x + 2, half_sz.y + 1), ImGuiDir_Left, IM_COL32(0,0,0,alpha8)); + ImGui::RenderArrowPointingAt(draw_list, ImVec2(pos.x + bar_w - half_sz.x, pos.y), half_sz, ImGuiDir_Left, IM_COL32(255,255,255,alpha8)); +} + +// Note: ColorPicker4() only accesses 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. +// (In C++ the 'float col[4]' notation for a function argument is equivalent to 'float* col', we only specify a size to facilitate understanding of the code.) +// FIXME: we adjust the big color square height based on item width, which may cause a flickering feedback loop (if automatic height makes a vertical scrollbar appears, affecting automatic width..) +// FIXME: this is trying to be aware of style.Alpha but not fully correct. Also, the color wheel will have overlapping glitches with (style.Alpha < 1.0) +bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags, const float* ref_col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImDrawList* draw_list = window->DrawList; + ImGuiStyle& style = g.Style; + ImGuiIO& io = g.IO; + + const float width = CalcItemWidth(); + g.NextItemData.ClearFlags(); + + PushID(label); + BeginGroup(); + + if (!(flags & ImGuiColorEditFlags_NoSidePreview)) + flags |= ImGuiColorEditFlags_NoSmallPreview; + + // Context menu: display and store options. + if (!(flags & ImGuiColorEditFlags_NoOptions)) + ColorPickerOptionsPopup(col, flags); + + // Read stored options + if (!(flags & ImGuiColorEditFlags__PickerMask)) + flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__PickerMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__PickerMask; + if (!(flags & ImGuiColorEditFlags__InputMask)) + flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__InputMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__InputMask; + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check that only 1 is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected + if (!(flags & ImGuiColorEditFlags_NoOptions)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags_AlphaBar); + + // Setup + int components = (flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4; + bool alpha_bar = (flags & ImGuiColorEditFlags_AlphaBar) && !(flags & ImGuiColorEditFlags_NoAlpha); + ImVec2 picker_pos = window->DC.CursorPos; + float square_sz = GetFrameHeight(); + float bars_width = square_sz; // Arbitrary smallish width of Hue/Alpha picking bars + float sv_picker_size = ImMax(bars_width * 1, width - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box + float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x; + float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x; + float bars_triangles_half_sz = (float)(int)(bars_width * 0.20f); + + float backup_initial_col[4]; + memcpy(backup_initial_col, col, components * sizeof(float)); + + float wheel_thickness = sv_picker_size * 0.08f; + float wheel_r_outer = sv_picker_size * 0.50f; + float wheel_r_inner = wheel_r_outer - wheel_thickness; + ImVec2 wheel_center(picker_pos.x + (sv_picker_size + bars_width)*0.5f, picker_pos.y + sv_picker_size*0.5f); + + // Note: the triangle is displayed rotated with triangle_pa pointing to Hue, but most coordinates stays unrotated for logic. + float triangle_r = wheel_r_inner - (int)(sv_picker_size * 0.027f); + ImVec2 triangle_pa = ImVec2(triangle_r, 0.0f); // Hue point. + ImVec2 triangle_pb = ImVec2(triangle_r * -0.5f, triangle_r * -0.866025f); // Black point. + ImVec2 triangle_pc = ImVec2(triangle_r * -0.5f, triangle_r * +0.866025f); // White point. + + float H = col[0], S = col[1], V = col[2]; + float R = col[0], G = col[1], B = col[2]; + if (flags & ImGuiColorEditFlags_InputRGB) + { + // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. + ColorConvertRGBtoHSV(R, G, B, H, S, V); + if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) + H = g.ColorEditLastHue; + } + else if (flags & ImGuiColorEditFlags_InputHSV) + { + ColorConvertHSVtoRGB(H, S, V, R, G, B); + } + + bool value_changed = false, value_changed_h = false, value_changed_sv = false; + + PushItemFlag(ImGuiItemFlags_NoNav, true); + if (flags & ImGuiColorEditFlags_PickerHueWheel) + { + // Hue wheel + SV triangle logic + InvisibleButton("hsv", ImVec2(sv_picker_size + style.ItemInnerSpacing.x + bars_width, sv_picker_size)); + if (IsItemActive()) + { + ImVec2 initial_off = g.IO.MouseClickedPos[0] - wheel_center; + ImVec2 current_off = g.IO.MousePos - wheel_center; + float initial_dist2 = ImLengthSqr(initial_off); + if (initial_dist2 >= (wheel_r_inner-1)*(wheel_r_inner-1) && initial_dist2 <= (wheel_r_outer+1)*(wheel_r_outer+1)) + { + // Interactive with Hue wheel + H = ImAtan2(current_off.y, current_off.x) / IM_PI*0.5f; + if (H < 0.0f) + H += 1.0f; + value_changed = value_changed_h = true; + } + float cos_hue_angle = ImCos(-H * 2.0f * IM_PI); + float sin_hue_angle = ImSin(-H * 2.0f * IM_PI); + if (ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, ImRotate(initial_off, cos_hue_angle, sin_hue_angle))) + { + // Interacting with SV triangle + ImVec2 current_off_unrotated = ImRotate(current_off, cos_hue_angle, sin_hue_angle); + if (!ImTriangleContainsPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated)) + current_off_unrotated = ImTriangleClosestPoint(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated); + float uu, vv, ww; + ImTriangleBarycentricCoords(triangle_pa, triangle_pb, triangle_pc, current_off_unrotated, uu, vv, ww); + V = ImClamp(1.0f - vv, 0.0001f, 1.0f); + S = ImClamp(uu / V, 0.0001f, 1.0f); + value_changed = value_changed_sv = true; + } + } + if (!(flags & ImGuiColorEditFlags_NoOptions)) + OpenPopupOnItemClick("context"); + } + else if (flags & ImGuiColorEditFlags_PickerHueBar) + { + // SV rectangle logic + InvisibleButton("sv", ImVec2(sv_picker_size, sv_picker_size)); + if (IsItemActive()) + { + S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size-1)); + V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + value_changed = value_changed_sv = true; + } + if (!(flags & ImGuiColorEditFlags_NoOptions)) + OpenPopupOnItemClick("context"); + + // Hue bar logic + SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); + InvisibleButton("hue", ImVec2(bars_width, sv_picker_size)); + if (IsItemActive()) + { + H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + value_changed = value_changed_h = true; + } + } + + // Alpha bar logic + if (alpha_bar) + { + SetCursorScreenPos(ImVec2(bar1_pos_x, picker_pos.y)); + InvisibleButton("alpha", ImVec2(bars_width, sv_picker_size)); + if (IsItemActive()) + { + col[3] = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + value_changed = true; + } + } + PopItemFlag(); // ImGuiItemFlags_NoNav + + if (!(flags & ImGuiColorEditFlags_NoSidePreview)) + { + SameLine(0, style.ItemInnerSpacing.x); + BeginGroup(); + } + + if (!(flags & ImGuiColorEditFlags_NoLabel)) + { + const char* label_display_end = FindRenderedTextEnd(label); + if (label != label_display_end) + { + if ((flags & ImGuiColorEditFlags_NoSidePreview)) + SameLine(0, style.ItemInnerSpacing.x); + TextEx(label, label_display_end); + } + } + + if (!(flags & ImGuiColorEditFlags_NoSidePreview)) + { + PushItemFlag(ImGuiItemFlags_NoNavDefaultFocus, true); + ImVec4 col_v4(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); + if ((flags & ImGuiColorEditFlags_NoLabel)) + Text("Current"); + + ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoTooltip; + ColorButton("##current", col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2)); + if (ref_col != NULL) + { + Text("Original"); + ImVec4 ref_col_v4(ref_col[0], ref_col[1], ref_col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : ref_col[3]); + if (ColorButton("##original", ref_col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2))) + { + memcpy(col, ref_col, components * sizeof(float)); + value_changed = true; + } + } + PopItemFlag(); + EndGroup(); + } + + // Convert back color to RGB + if (value_changed_h || value_changed_sv) + { + if (flags & ImGuiColorEditFlags_InputRGB) + { + ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10*1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]); + g.ColorEditLastHue = H; + memcpy(g.ColorEditLastColor, col, sizeof(float) * 3); + } + else if (flags & ImGuiColorEditFlags_InputHSV) + { + col[0] = H; + col[1] = S; + col[2] = V; + } + } + + // R,G,B and H,S,V slider color editor + bool value_changed_fix_hue_wrap = false; + if ((flags & ImGuiColorEditFlags_NoInputs) == 0) + { + PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x); + ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; + ImGuiColorEditFlags sub_flags = (flags & sub_flags_to_forward) | ImGuiColorEditFlags_NoPicker; + if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + if (ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_DisplayRGB)) + { + // FIXME: Hackily differenciating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget. + // For the later we don't want to run the hue-wrap canceling code. If you are well versed in HSV picker please provide your input! (See #2050) + value_changed_fix_hue_wrap = (g.ActiveId != 0 && !g.ActiveIdAllowOverlap); + value_changed = true; + } + if (flags & ImGuiColorEditFlags_DisplayHSV || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + value_changed |= ColorEdit4("##hsv", col, sub_flags | ImGuiColorEditFlags_DisplayHSV); + if (flags & ImGuiColorEditFlags_DisplayHex || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + value_changed |= ColorEdit4("##hex", col, sub_flags | ImGuiColorEditFlags_DisplayHex); + PopItemWidth(); + } + + // Try to cancel hue wrap (after ColorEdit4 call), if any + if (value_changed_fix_hue_wrap && (flags & ImGuiColorEditFlags_InputRGB)) + { + float new_H, new_S, new_V; + ColorConvertRGBtoHSV(col[0], col[1], col[2], new_H, new_S, new_V); + if (new_H <= 0 && H > 0) + { + if (new_V <= 0 && V != new_V) + ColorConvertHSVtoRGB(H, S, new_V <= 0 ? V * 0.5f : new_V, col[0], col[1], col[2]); + else if (new_S <= 0) + ColorConvertHSVtoRGB(H, new_S <= 0 ? S * 0.5f : new_S, new_V, col[0], col[1], col[2]); + } + } + + if (value_changed) + { + if (flags & ImGuiColorEditFlags_InputRGB) + { + R = col[0]; + G = col[1]; + B = col[2]; + ColorConvertRGBtoHSV(R, G, B, H, S, V); + if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately. + H = g.ColorEditLastHue; + } + else if (flags & ImGuiColorEditFlags_InputHSV) + { + H = col[0]; + S = col[1]; + V = col[2]; + ColorConvertHSVtoRGB(H, S, V, R, G, B); + } + } + + const int style_alpha8 = IM_F32_TO_INT8_SAT(style.Alpha); + const ImU32 col_black = IM_COL32(0,0,0,style_alpha8); + const ImU32 col_white = IM_COL32(255,255,255,style_alpha8); + const ImU32 col_midgrey = IM_COL32(128,128,128,style_alpha8); + const ImU32 col_hues[6 + 1] = { IM_COL32(255,0,0,style_alpha8), IM_COL32(255,255,0,style_alpha8), IM_COL32(0,255,0,style_alpha8), IM_COL32(0,255,255,style_alpha8), IM_COL32(0,0,255,style_alpha8), IM_COL32(255,0,255,style_alpha8), IM_COL32(255,0,0,style_alpha8) }; + + ImVec4 hue_color_f(1, 1, 1, style.Alpha); ColorConvertHSVtoRGB(H, 1, 1, hue_color_f.x, hue_color_f.y, hue_color_f.z); + ImU32 hue_color32 = ColorConvertFloat4ToU32(hue_color_f); + ImU32 user_col32_striped_of_alpha = ColorConvertFloat4ToU32(ImVec4(R, G, B, style.Alpha)); // Important: this is still including the main rendering/style alpha!! + + ImVec2 sv_cursor_pos; + + if (flags & ImGuiColorEditFlags_PickerHueWheel) + { + // Render Hue Wheel + const float aeps = 0.5f / wheel_r_outer; // Half a pixel arc length in radians (2pi cancels out). + const int segment_per_arc = ImMax(4, (int)wheel_r_outer / 12); + for (int n = 0; n < 6; n++) + { + const float a0 = (n) /6.0f * 2.0f * IM_PI - aeps; + const float a1 = (n+1.0f)/6.0f * 2.0f * IM_PI + aeps; + const int vert_start_idx = draw_list->VtxBuffer.Size; + draw_list->PathArcTo(wheel_center, (wheel_r_inner + wheel_r_outer)*0.5f, a0, a1, segment_per_arc); + draw_list->PathStroke(col_white, false, wheel_thickness); + const int vert_end_idx = draw_list->VtxBuffer.Size; + + // Paint colors over existing vertices + ImVec2 gradient_p0(wheel_center.x + ImCos(a0) * wheel_r_inner, wheel_center.y + ImSin(a0) * wheel_r_inner); + ImVec2 gradient_p1(wheel_center.x + ImCos(a1) * wheel_r_inner, wheel_center.y + ImSin(a1) * wheel_r_inner); + ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col_hues[n], col_hues[n+1]); + } + + // Render Cursor + preview on Hue Wheel + float cos_hue_angle = ImCos(H * 2.0f * IM_PI); + float sin_hue_angle = ImSin(H * 2.0f * IM_PI); + ImVec2 hue_cursor_pos(wheel_center.x + cos_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f, wheel_center.y + sin_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f); + float hue_cursor_rad = value_changed_h ? wheel_thickness * 0.65f : wheel_thickness * 0.55f; + int hue_cursor_segments = ImClamp((int)(hue_cursor_rad / 1.4f), 9, 32); + draw_list->AddCircleFilled(hue_cursor_pos, hue_cursor_rad, hue_color32, hue_cursor_segments); + draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad+1, col_midgrey, hue_cursor_segments); + draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad, col_white, hue_cursor_segments); + + // Render SV triangle (rotated according to hue) + ImVec2 tra = wheel_center + ImRotate(triangle_pa, cos_hue_angle, sin_hue_angle); + ImVec2 trb = wheel_center + ImRotate(triangle_pb, cos_hue_angle, sin_hue_angle); + ImVec2 trc = wheel_center + ImRotate(triangle_pc, cos_hue_angle, sin_hue_angle); + ImVec2 uv_white = GetFontTexUvWhitePixel(); + draw_list->PrimReserve(6, 6); + draw_list->PrimVtx(tra, uv_white, hue_color32); + draw_list->PrimVtx(trb, uv_white, hue_color32); + draw_list->PrimVtx(trc, uv_white, col_white); + draw_list->PrimVtx(tra, uv_white, 0); + draw_list->PrimVtx(trb, uv_white, col_white); + draw_list->PrimVtx(trc, uv_white, 0); + draw_list->AddTriangle(tra, trb, trc, col_midgrey, 1.5f); + sv_cursor_pos = ImLerp(ImLerp(trc, tra, ImSaturate(S)), trb, ImSaturate(1 - V)); + } + else if (flags & ImGuiColorEditFlags_PickerHueBar) + { + // Render SV Square + draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), col_white, hue_color32, hue_color32, col_white); + draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), 0, 0, col_black, col_black); + RenderFrameBorder(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), 0.0f); + sv_cursor_pos.x = ImClamp((float)(int)(picker_pos.x + ImSaturate(S) * sv_picker_size + 0.5f), picker_pos.x + 2, picker_pos.x + sv_picker_size - 2); // Sneakily prevent the circle to stick out too much + sv_cursor_pos.y = ImClamp((float)(int)(picker_pos.y + ImSaturate(1 - V) * sv_picker_size + 0.5f), picker_pos.y + 2, picker_pos.y + sv_picker_size - 2); + + // Render Hue Bar + for (int i = 0; i < 6; ++i) + draw_list->AddRectFilledMultiColor(ImVec2(bar0_pos_x, picker_pos.y + i * (sv_picker_size / 6)), ImVec2(bar0_pos_x + bars_width, picker_pos.y + (i + 1) * (sv_picker_size / 6)), col_hues[i], col_hues[i], col_hues[i + 1], col_hues[i + 1]); + float bar0_line_y = (float)(int)(picker_pos.y + H * sv_picker_size + 0.5f); + RenderFrameBorder(ImVec2(bar0_pos_x, picker_pos.y), ImVec2(bar0_pos_x + bars_width, picker_pos.y + sv_picker_size), 0.0f); + RenderArrowsForVerticalBar(draw_list, ImVec2(bar0_pos_x - 1, bar0_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f, style.Alpha); + } + + // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range) + float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f; + draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, user_col32_striped_of_alpha, 12); + draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad+1, col_midgrey, 12); + draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, col_white, 12); + + // Render alpha bar + if (alpha_bar) + { + float alpha = ImSaturate(col[3]); + ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size); + RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f)); + draw_list->AddRectFilledMultiColor(bar1_bb.Min, bar1_bb.Max, user_col32_striped_of_alpha, user_col32_striped_of_alpha, user_col32_striped_of_alpha & ~IM_COL32_A_MASK, user_col32_striped_of_alpha & ~IM_COL32_A_MASK); + float bar1_line_y = (float)(int)(picker_pos.y + (1.0f - alpha) * sv_picker_size + 0.5f); + RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f); + RenderArrowsForVerticalBar(draw_list, ImVec2(bar1_pos_x - 1, bar1_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f, style.Alpha); + } + + EndGroup(); + + if (value_changed && memcmp(backup_initial_col, col, components * sizeof(float)) == 0) + value_changed = false; + if (value_changed) + MarkItemEdited(window->DC.LastItemId); + + PopID(); + + return value_changed; +} + +// A little colored square. Return true when clicked. +// FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip. +// 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. +// Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set. +bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, ImVec2 size) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiID id = window->GetID(desc_id); + float default_size = GetFrameHeight(); + if (size.x == 0.0f) + size.x = default_size; + if (size.y == 0.0f) + size.y = default_size; + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); + ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); + if (!ItemAdd(bb, id)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + + if (flags & ImGuiColorEditFlags_NoAlpha) + flags &= ~(ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf); + + ImVec4 col_rgb = col; + if (flags & ImGuiColorEditFlags_InputHSV) + ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z); + + ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f); + float grid_step = ImMin(size.x, size.y) / 2.99f; + float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f); + ImRect bb_inner = bb; + float off = -0.75f; // The border (using Col_FrameBg) tends to look off when color is near-opaque and rounding is enabled. This offset seemed like a good middle ground to reduce those artifacts. + bb_inner.Expand(off); + if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) + { + float mid_x = (float)(int)((bb_inner.Min.x + bb_inner.Max.x) * 0.5f + 0.5f); + RenderColorRectWithAlphaCheckerboard(ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight); + window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft); + } + else + { + // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha + ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha; + if (col_source.w < 1.0f) + RenderColorRectWithAlphaCheckerboard(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); + else + window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All); + } + RenderNavHighlight(bb, id); + if (g.Style.FrameBorderSize > 0.0f) + RenderFrameBorder(bb.Min, bb.Max, rounding); + else + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border + + // Drag and Drop Source + // NB: The ActiveId test is merely an optional micro-optimization, BeginDragDropSource() does the same test. + if (g.ActiveId == id && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropSource()) + { + if (flags & ImGuiColorEditFlags_NoAlpha) + SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F, &col_rgb, sizeof(float) * 3, ImGuiCond_Once); + else + SetDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F, &col_rgb, sizeof(float) * 4, ImGuiCond_Once); + ColorButton(desc_id, col, flags); + SameLine(); + TextEx("Color"); + EndDragDropSource(); + } + + // Tooltip + if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered) + ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)); + + return pressed; +} + +// Initialize/override default color options +void ImGui::SetColorEditOptions(ImGuiColorEditFlags flags) +{ + ImGuiContext& g = *GImGui; + if ((flags & ImGuiColorEditFlags__DisplayMask) == 0) + flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DisplayMask; + if ((flags & ImGuiColorEditFlags__DataTypeMask) == 0) + flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DataTypeMask; + if ((flags & ImGuiColorEditFlags__PickerMask) == 0) + flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__PickerMask; + if ((flags & ImGuiColorEditFlags__InputMask) == 0) + flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__InputMask; + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DataTypeMask)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check only 1 option is selected + g.ColorEditOptions = flags; +} + +// Note: only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. +void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags) +{ + ImGuiContext& g = *GImGui; + + BeginTooltipEx(0, true); + const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; + if (text_end > text) + { + TextEx(text, text_end); + Separator(); + } + + ImVec2 sz(g.FontSize * 3 + g.Style.FramePadding.y * 2, g.FontSize * 3 + g.Style.FramePadding.y * 2); + ImVec4 cf(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); + int cr = IM_F32_TO_INT8_SAT(col[0]), cg = IM_F32_TO_INT8_SAT(col[1]), cb = IM_F32_TO_INT8_SAT(col[2]), ca = (flags & ImGuiColorEditFlags_NoAlpha) ? 255 : IM_F32_TO_INT8_SAT(col[3]); + ColorButton("##preview", cf, (flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)) | ImGuiColorEditFlags_NoTooltip, sz); + SameLine(); + if ((flags & ImGuiColorEditFlags_InputRGB) || !(flags & ImGuiColorEditFlags__InputMask)) + { + if (flags & ImGuiColorEditFlags_NoAlpha) + Text("#%02X%02X%02X\nR: %d, G: %d, B: %d\n(%.3f, %.3f, %.3f)", cr, cg, cb, cr, cg, cb, col[0], col[1], col[2]); + else + Text("#%02X%02X%02X%02X\nR:%d, G:%d, B:%d, A:%d\n(%.3f, %.3f, %.3f, %.3f)", cr, cg, cb, ca, cr, cg, cb, ca, col[0], col[1], col[2], col[3]); + } + else if (flags & ImGuiColorEditFlags_InputHSV) + { + if (flags & ImGuiColorEditFlags_NoAlpha) + Text("H: %.3f, S: %.3f, V: %.3f", col[0], col[1], col[2]); + else + Text("H: %.3f, S: %.3f, V: %.3f, A: %.3f", col[0], col[1], col[2], col[3]); + } + EndTooltip(); +} + +void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) +{ + bool allow_opt_inputs = !(flags & ImGuiColorEditFlags__DisplayMask); + bool allow_opt_datatype = !(flags & ImGuiColorEditFlags__DataTypeMask); + if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup("context")) + return; + ImGuiContext& g = *GImGui; + ImGuiColorEditFlags opts = g.ColorEditOptions; + if (allow_opt_inputs) + { + if (RadioButton("RGB", (opts & ImGuiColorEditFlags_DisplayRGB) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayRGB; + if (RadioButton("HSV", (opts & ImGuiColorEditFlags_DisplayHSV) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHSV; + if (RadioButton("Hex", (opts & ImGuiColorEditFlags_DisplayHex) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHex; + } + if (allow_opt_datatype) + { + if (allow_opt_inputs) Separator(); + if (RadioButton("0..255", (opts & ImGuiColorEditFlags_Uint8) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Uint8; + if (RadioButton("0.00..1.00", (opts & ImGuiColorEditFlags_Float) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Float; + } + + if (allow_opt_inputs || allow_opt_datatype) + Separator(); + if (Button("Copy as..", ImVec2(-1,0))) + OpenPopup("Copy"); + if (BeginPopup("Copy")) + { + int cr = IM_F32_TO_INT8_SAT(col[0]), cg = IM_F32_TO_INT8_SAT(col[1]), cb = IM_F32_TO_INT8_SAT(col[2]), ca = (flags & ImGuiColorEditFlags_NoAlpha) ? 255 : IM_F32_TO_INT8_SAT(col[3]); + char buf[64]; + ImFormatString(buf, IM_ARRAYSIZE(buf), "(%.3ff, %.3ff, %.3ff, %.3ff)", col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); + if (Selectable(buf)) + SetClipboardText(buf); + ImFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d,%d)", cr, cg, cb, ca); + if (Selectable(buf)) + SetClipboardText(buf); + if (flags & ImGuiColorEditFlags_NoAlpha) + ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X", cr, cg, cb); + else + ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X%02X", cr, cg, cb, ca); + if (Selectable(buf)) + SetClipboardText(buf); + EndPopup(); + } + + g.ColorEditOptions = opts; + EndPopup(); +} + +void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags) +{ + bool allow_opt_picker = !(flags & ImGuiColorEditFlags__PickerMask); + bool allow_opt_alpha_bar = !(flags & ImGuiColorEditFlags_NoAlpha) && !(flags & ImGuiColorEditFlags_AlphaBar); + if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup("context")) + return; + ImGuiContext& g = *GImGui; + if (allow_opt_picker) + { + ImVec2 picker_size(g.FontSize * 8, ImMax(g.FontSize * 8 - (GetFrameHeight() + g.Style.ItemInnerSpacing.x), 1.0f)); // FIXME: Picker size copied from main picker function + PushItemWidth(picker_size.x); + for (int picker_type = 0; picker_type < 2; picker_type++) + { + // Draw small/thumbnail version of each picker type (over an invisible button for selection) + if (picker_type > 0) Separator(); + PushID(picker_type); + ImGuiColorEditFlags picker_flags = ImGuiColorEditFlags_NoInputs|ImGuiColorEditFlags_NoOptions|ImGuiColorEditFlags_NoLabel|ImGuiColorEditFlags_NoSidePreview|(flags & ImGuiColorEditFlags_NoAlpha); + if (picker_type == 0) picker_flags |= ImGuiColorEditFlags_PickerHueBar; + if (picker_type == 1) picker_flags |= ImGuiColorEditFlags_PickerHueWheel; + ImVec2 backup_pos = GetCursorScreenPos(); + if (Selectable("##selectable", false, 0, picker_size)) // By default, Selectable() is closing popup + g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags__PickerMask) | (picker_flags & ImGuiColorEditFlags__PickerMask); + SetCursorScreenPos(backup_pos); + ImVec4 dummy_ref_col; + memcpy(&dummy_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4)); + ColorPicker4("##dummypicker", &dummy_ref_col.x, picker_flags); + PopID(); + } + PopItemWidth(); + } + if (allow_opt_alpha_bar) + { + if (allow_opt_picker) Separator(); + CheckboxFlags("Alpha Bar", (unsigned int*)&g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); + } + EndPopup(); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: TreeNode, CollapsingHeader, etc. +//------------------------------------------------------------------------- +// - TreeNode() +// - TreeNodeV() +// - TreeNodeEx() +// - TreeNodeExV() +// - TreeNodeBehavior() [Internal] +// - TreePush() +// - TreePop() +// - GetTreeNodeToLabelSpacing() +// - SetNextItemOpen() +// - CollapsingHeader() +//------------------------------------------------------------------------- + +bool ImGui::TreeNode(const char* str_id, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + bool is_open = TreeNodeExV(str_id, 0, fmt, args); + va_end(args); + return is_open; +} + +bool ImGui::TreeNode(const void* ptr_id, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + bool is_open = TreeNodeExV(ptr_id, 0, fmt, args); + va_end(args); + return is_open; +} + +bool ImGui::TreeNode(const char* label) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + return TreeNodeBehavior(window->GetID(label), 0, label, NULL); +} + +bool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args) +{ + return TreeNodeExV(str_id, 0, fmt, args); +} + +bool ImGui::TreeNodeV(const void* ptr_id, const char* fmt, va_list args) +{ + return TreeNodeExV(ptr_id, 0, fmt, args); +} + +bool ImGui::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + return TreeNodeBehavior(window->GetID(label), flags, label, NULL); +} + +bool ImGui::TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + bool is_open = TreeNodeExV(str_id, flags, fmt, args); + va_end(args); + return is_open; +} + +bool ImGui::TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + bool is_open = TreeNodeExV(ptr_id, flags, fmt, args); + va_end(args); + return is_open; +} + +bool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + return TreeNodeBehavior(window->GetID(str_id), flags, g.TempBuffer, label_end); +} + +bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + return TreeNodeBehavior(window->GetID(ptr_id), flags, g.TempBuffer, label_end); +} + +bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) +{ + if (flags & ImGuiTreeNodeFlags_Leaf) + return true; + + // We only write to the tree storage if the user clicks (or explicitly use the SetNextItemOpen function) + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiStorage* storage = window->DC.StateStorage; + + bool is_open; + if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasOpen) + { + if (g.NextItemData.OpenCond & ImGuiCond_Always) + { + is_open = g.NextItemData.OpenVal; + storage->SetInt(id, is_open); + } + else + { + // We treat ImGuiCond_Once and ImGuiCond_FirstUseEver the same because tree node state are not saved persistently. + const int stored_value = storage->GetInt(id, -1); + if (stored_value == -1) + { + is_open = g.NextItemData.OpenVal; + storage->SetInt(id, is_open); + } + else + { + is_open = stored_value != 0; + } + } + } + else + { + is_open = storage->GetInt(id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0; + } + + // When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior). + // NB- If we are above max depth we still allow manually opened nodes to be logged. + if (g.LogEnabled && !(flags & ImGuiTreeNodeFlags_NoAutoOpenOnLog) && (window->DC.TreeDepth - g.LogDepthRef) < g.LogDepthToExpand) + is_open = true; + + return is_open; +} + +bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const bool display_frame = (flags & ImGuiTreeNodeFlags_Framed) != 0; + const ImVec2 padding = (display_frame || (flags & ImGuiTreeNodeFlags_FramePadding)) ? style.FramePadding : ImVec2(style.FramePadding.x, 0.0f); + + if (!label_end) + label_end = FindRenderedTextEnd(label); + const ImVec2 label_size = CalcTextSize(label, label_end, false); + + // We vertically grow up to current line height up the typical widget height. + const float frame_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y*2), label_size.y + padding.y*2); + ImRect frame_bb; + frame_bb.Min.x = (flags & ImGuiTreeNodeFlags_SpanFullWidth) ? window->WorkRect.Min.x : window->DC.CursorPos.x; + frame_bb.Min.y = window->DC.CursorPos.y; + frame_bb.Max.x = window->WorkRect.Max.x; + frame_bb.Max.y = window->DC.CursorPos.y + frame_height; + if (display_frame) + { + // Framed header expand a little outside the default padding, to the edge of InnerClipRect + // (FIXME: May remove this at some point and make InnerClipRect align with WindowPadding.x instead of WindowPadding.x*0.5f) + frame_bb.Min.x -= (float)(int)(window->WindowPadding.x * 0.5f - 1.0f); + frame_bb.Max.x += (float)(int)(window->WindowPadding.x * 0.5f); + } + + const float text_offset_x = g.FontSize + (display_frame ? padding.x*3 : padding.x*2); // Collapser arrow width + Spacing + const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it + const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x*2 : 0.0f); // Include collapser + ImVec2 text_pos(window->DC.CursorPos.x + text_offset_x, window->DC.CursorPos.y + text_offset_y); + ItemSize(ImVec2(text_width, frame_height), text_offset_y); + + // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing + ImRect interact_bb = frame_bb; + if (!display_frame && (flags & (ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth)) == 0) + interact_bb.Max.x = frame_bb.Min.x + text_width + style.ItemSpacing.x * 2.0f; + + // Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child. + // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). + // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero. + const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; + bool is_open = TreeNodeBehaviorIsOpen(id, flags); + if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) + window->DC.TreeMayJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); + + bool item_add = ItemAdd(interact_bb, id); + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; + window->DC.LastItemDisplayRect = frame_bb; + + if (!item_add) + { + if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) + TreePushOverrideID(id); + IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); + return is_open; + } + + // Flags that affects opening behavior: + // - 0 (default) .................... single-click anywhere to open + // - OpenOnDoubleClick .............. double-click anywhere to open + // - OpenOnArrow .................... single-click on arrow to open + // - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open + ImGuiButtonFlags button_flags = ImGuiButtonFlags_NoKeyModifiers; + if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) + button_flags |= ImGuiButtonFlags_AllowItemOverlap; + if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) + button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0); + if (!is_leaf) + button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; + + bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0; + const bool was_selected = selected; + + bool hovered, held; + bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags); + bool toggled = false; + if (!is_leaf) + { + if (pressed) + { + const float arrow_x1 = text_pos.x - text_offset_x; + const float arrow_x2 = arrow_x1 + g.FontSize + padding.x * 2.0f; + toggled = !(flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) || (g.NavActivateId == id); + if (flags & ImGuiTreeNodeFlags_OpenOnArrow) + toggled |= IsMouseHoveringRect(ImVec2(arrow_x1, interact_bb.Min.y), ImVec2(arrow_x2, interact_bb.Max.y)) && (!g.NavDisableMouseHover); + if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) + toggled |= g.IO.MouseDoubleClicked[0]; + if (g.DragDropActive && is_open) // When using Drag and Drop "hold to open" we keep the node highlighted after opening, but never close it again. + toggled = false; + } + + if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Left && is_open) + { + toggled = true; + NavMoveRequestCancel(); + } + if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right && !is_open) // If there's something upcoming on the line we may want to give it the priority? + { + toggled = true; + NavMoveRequestCancel(); + } + + if (toggled) + { + is_open = !is_open; + window->DC.StateStorage->SetInt(id, is_open); + } + } + if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) + SetItemAllowOverlap(); + + // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger. + if (selected != was_selected) //-V547 + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; + + // Render + const ImU32 text_col = GetColorU32(ImGuiCol_Text); + ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_TypeThin; + if (display_frame) + { + // Framed type + const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, true, style.FrameRounding); + RenderNavHighlight(frame_bb, id, nav_highlight_flags); + if (flags & ImGuiTreeNodeFlags_Bullet) + RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col); + else if (!is_leaf) + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y), text_col, is_open ? ImGuiDir_Down : ImGuiDir_Right, 1.0f); + else // Leaf without bullet, left-adjusted text + text_pos.x -= text_offset_x; + if (flags & ImGuiTreeNodeFlags_ClipLabelForTrailingButton) + frame_bb.Max.x -= g.FontSize + style.FramePadding.x; + if (g.LogEnabled) + { + // NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here. + const char log_prefix[] = "\n##"; + const char log_suffix[] = "##"; + LogRenderedText(&text_pos, log_prefix, log_prefix+3); + RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); + LogRenderedText(&text_pos, log_suffix, log_suffix+2); + } + else + { + RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); + } + } + else + { + // Unframed typed for tree nodes + if (hovered || selected) + { + const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false); + RenderNavHighlight(frame_bb, id, nav_highlight_flags); + } + if (flags & ImGuiTreeNodeFlags_Bullet) + RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); + else if (!is_leaf) + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f); + if (g.LogEnabled) + LogRenderedText(&text_pos, ">"); + RenderText(text_pos, label, label_end, false); + } + + if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) + TreePushOverrideID(id); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); + return is_open; +} + +void ImGui::TreePush(const char* str_id) +{ + ImGuiWindow* window = GetCurrentWindow(); + Indent(); + window->DC.TreeDepth++; + PushID(str_id ? str_id : "#TreePush"); +} + +void ImGui::TreePush(const void* ptr_id) +{ + ImGuiWindow* window = GetCurrentWindow(); + Indent(); + window->DC.TreeDepth++; + PushID(ptr_id ? ptr_id : (const void*)"#TreePush"); +} + +void ImGui::TreePushOverrideID(ImGuiID id) +{ + ImGuiWindow* window = GetCurrentWindow(); + Indent(); + window->DC.TreeDepth++; + window->IDStack.push_back(id); +} + +void ImGui::TreePop() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + Unindent(); + + window->DC.TreeDepth--; + ImU32 tree_depth_mask = (1 << window->DC.TreeDepth); + + // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled) + if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) + if (g.NavIdIsAlive && (window->DC.TreeMayJumpToParentOnPopMask & tree_depth_mask)) + { + SetNavID(window->IDStack.back(), g.NavLayer); + NavMoveRequestCancel(); + } + window->DC.TreeMayJumpToParentOnPopMask &= tree_depth_mask - 1; + + IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much. + PopID(); +} + +// Horizontal distance preceding label when using TreeNode() or Bullet() +float ImGui::GetTreeNodeToLabelSpacing() +{ + ImGuiContext& g = *GImGui; + return g.FontSize + (g.Style.FramePadding.x * 2.0f); +} + +// Set next TreeNode/CollapsingHeader open state. +void ImGui::SetNextItemOpen(bool is_open, ImGuiCond cond) +{ + ImGuiContext& g = *GImGui; + if (g.CurrentWindow->SkipItems) + return; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasOpen; + g.NextItemData.OpenVal = is_open; + g.NextItemData.OpenCond = cond ? cond : ImGuiCond_Always; +} + +// CollapsingHeader returns true when opened but do not indent nor push into the ID stack (because of the ImGuiTreeNodeFlags_NoTreePushOnOpen flag). +// This is basically the same as calling TreeNodeEx(label, ImGuiTreeNodeFlags_CollapsingHeader). You can remove the _NoTreePushOnOpen flag if you want behavior closer to normal TreeNode(). +bool ImGui::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + return TreeNodeBehavior(window->GetID(label), flags | ImGuiTreeNodeFlags_CollapsingHeader, label); +} + +bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + if (p_open && !*p_open) + return false; + + ImGuiID id = window->GetID(label); + flags |= ImGuiTreeNodeFlags_CollapsingHeader | (p_open ? ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton : 0); + bool is_open = TreeNodeBehavior(id, flags, label); + if (p_open) + { + // Create a small overlapping close button + // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc. + // FIXME: CloseButton can overlap into text, need find a way to clip the text somehow. + ImGuiContext& g = *GImGui; + ImGuiItemHoveredDataBackup last_item_backup; + float button_size = g.FontSize; + float button_x = ImMax(window->DC.LastItemRect.Min.x, window->DC.LastItemRect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); + float button_y = window->DC.LastItemRect.Min.y; + if (CloseButton(window->GetID((void*)((intptr_t)id + 1)), ImVec2(button_x, button_y))) + *p_open = false; + last_item_backup.Restore(); + } + + return is_open; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Selectable +//------------------------------------------------------------------------- +// - Selectable() +//------------------------------------------------------------------------- + +// Tip: pass a non-visible label (e.g. "##dummy") then you can use the space to draw other text or image. +// But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id. +bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + + if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. + PushColumnsBackground(); + + ImGuiID id = window->GetID(label); + ImVec2 label_size = CalcTextSize(label, NULL, true); + ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y); + ImVec2 pos = window->DC.CursorPos; + pos.y += window->DC.CurrLineTextBaseOffset; + ImRect bb_inner(pos, pos + size); + ItemSize(size); + + // Fill horizontal space. + ImVec2 window_padding = window->WindowPadding; + float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x : GetContentRegionMax().x; + float w_draw = ImMax(label_size.x, window->Pos.x + max_x - window_padding.x - pos.x); + ImVec2 size_draw((size_arg.x != 0 && !(flags & ImGuiSelectableFlags_DrawFillAvailWidth)) ? size_arg.x : w_draw, size_arg.y != 0.0f ? size_arg.y : size.y); + ImRect bb(pos, pos + size_draw); + if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth)) + bb.Max.x += window_padding.x; + + // Selectables are tightly packed together so we extend the box to cover spacing between selectable. + const float spacing_x = style.ItemSpacing.x; + const float spacing_y = style.ItemSpacing.y; + const float spacing_L = (float)(int)(spacing_x * 0.50f); + const float spacing_U = (float)(int)(spacing_y * 0.50f); + bb.Min.x -= spacing_L; + bb.Min.y -= spacing_U; + bb.Max.x += (spacing_x - spacing_L); + bb.Max.y += (spacing_y - spacing_U); + + bool item_add; + if (flags & ImGuiSelectableFlags_Disabled) + { + ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; + window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus; + item_add = ItemAdd(bb, id); + window->DC.ItemFlags = backup_item_flags; + } + else + { + item_add = ItemAdd(bb, id); + } + if (!item_add) + { + if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) + PopColumnsBackground(); + return false; + } + + // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries + ImGuiButtonFlags button_flags = 0; + if (flags & ImGuiSelectableFlags_NoHoldingActiveID) button_flags |= ImGuiButtonFlags_NoHoldingActiveID; + if (flags & ImGuiSelectableFlags_PressedOnClick) button_flags |= ImGuiButtonFlags_PressedOnClick; + if (flags & ImGuiSelectableFlags_PressedOnRelease) button_flags |= ImGuiButtonFlags_PressedOnRelease; + if (flags & ImGuiSelectableFlags_Disabled) button_flags |= ImGuiButtonFlags_Disabled; + if (flags & ImGuiSelectableFlags_AllowDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; + if (flags & ImGuiSelectableFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; + + if (flags & ImGuiSelectableFlags_Disabled) + selected = false; + + const bool was_selected = selected; + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); + + // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard + if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) + { + if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) + { + g.NavDisableHighlight = true; + SetNavID(id, window->DC.NavLayerCurrent); + } + } + if (pressed) + MarkItemEdited(id); + + if (flags & ImGuiSelectableFlags_AllowItemOverlap) + SetItemAllowOverlap(); + + // In this branch, Selectable() cannot toggle the selection so this will never trigger. + if (selected != was_selected) //-V547 + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; + + // Render + if (held && (flags & ImGuiSelectableFlags_DrawHoveredWhenHeld)) + hovered = true; + if (hovered || selected) + { + const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + RenderFrame(bb.Min, bb.Max, col, false, 0.0f); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + } + + if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) + { + PopColumnsBackground(); + bb.Max.x -= (GetContentRegionMax().x - max_x); + } + + if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); + RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb); + if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor(); + + // Automatically close popups + if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup)) + CloseCurrentPopup(); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + return pressed; +} + +bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) +{ + if (Selectable(label, *p_selected, flags, size_arg)) + { + *p_selected = !*p_selected; + return true; + } + return false; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: ListBox +//------------------------------------------------------------------------- +// - ListBox() +// - ListBoxHeader() +// - ListBoxFooter() +//------------------------------------------------------------------------- +// FIXME: This is an old API. We should redesign some of it, rename ListBoxHeader->BeginListBox, ListBoxFooter->EndListBox +// and promote using them over existing ListBox() functions, similarly to change with combo boxes. +//------------------------------------------------------------------------- + +// FIXME: In principle this function should be called BeginListBox(). We should rename it after re-evaluating if we want to keep the same signature. +// Helper to calculate the size of a listbox and display a label on the right. +// Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an non-visible label e.g. "##empty" +bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + const ImGuiStyle& style = g.Style; + const ImGuiID id = GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + + // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. + ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.4f + style.ItemSpacing.y); + ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); + ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); + ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + window->DC.LastItemRect = bb; // Forward storage for ListBoxFooter.. dodgy. + g.NextItemData.ClearFlags(); + + if (!IsRectVisible(bb.Min, bb.Max)) + { + ItemSize(bb.GetSize(), style.FramePadding.y); + ItemAdd(bb, 0, &frame_bb); + return false; + } + + BeginGroup(); + if (label_size.x > 0) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + + BeginChildFrame(id, frame_bb.GetSize()); + return true; +} + +// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. +bool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items) +{ + // Size default to hold ~7.25 items. + // We add +25% worth of item height to allow the user to see at a glance if there are more items up/down, without looking at the scrollbar. + // We don't add this extra bit if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size. + // I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution. + if (height_in_items < 0) + height_in_items = ImMin(items_count, 7); + const ImGuiStyle& style = GetStyle(); + float height_in_items_f = (height_in_items < items_count) ? (height_in_items + 0.25f) : (height_in_items + 0.00f); + + // We include ItemSpacing.y so that a list sized for the exact number of items doesn't make a scrollbar appears. We could also enforce that by passing a flag to BeginChild(). + ImVec2 size; + size.x = 0.0f; + size.y = ImFloor(GetTextLineHeightWithSpacing() * height_in_items_f + style.FramePadding.y * 2.0f); + return ListBoxHeader(label, size); +} + +// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. +void ImGui::ListBoxFooter() +{ + ImGuiWindow* parent_window = GetCurrentWindow()->ParentWindow; + const ImRect bb = parent_window->DC.LastItemRect; + const ImGuiStyle& style = GetStyle(); + + EndChildFrame(); + + // Redeclare item size so that it includes the label (we have stored the full size in LastItemRect) + // We call SameLine() to restore DC.CurrentLine* data + SameLine(); + parent_window->DC.CursorPos = bb.Min; + ItemSize(bb, style.FramePadding.y); + EndGroup(); +} + +bool ImGui::ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_items) +{ + const bool value_changed = ListBox(label, current_item, Items_ArrayGetter, (void*)items, items_count, height_items); + return value_changed; +} + +bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items) +{ + if (!ListBoxHeader(label, items_count, height_in_items)) + return false; + + // Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper. + ImGuiContext& g = *GImGui; + bool value_changed = false; + ImGuiListClipper clipper(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to. + while (clipper.Step()) + for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) + { + const bool item_selected = (i == *current_item); + const char* item_text; + if (!items_getter(data, i, &item_text)) + item_text = "*Unknown item*"; + + PushID(i); + if (Selectable(item_text, item_selected)) + { + *current_item = i; + value_changed = true; + } + if (item_selected) + SetItemDefaultFocus(); + PopID(); + } + ListBoxFooter(); + if (value_changed) + MarkItemEdited(g.CurrentWindow->DC.LastItemId); + + return value_changed; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: PlotLines, PlotHistogram +//------------------------------------------------------------------------- +// - PlotEx() [Internal] +// - PlotLines() +// - PlotHistogram() +//------------------------------------------------------------------------- + +void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + + const ImVec2 label_size = CalcTextSize(label, NULL, true); + if (frame_size.x == 0.0f) + frame_size.x = CalcItemWidth(); + if (frame_size.y == 0.0f) + frame_size.y = label_size.y + (style.FramePadding.y * 2); + + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); + const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); + const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, 0, &frame_bb)) + return; + const bool hovered = ItemHoverable(frame_bb, id); + + // Determine scale from values if not specified + if (scale_min == FLT_MAX || scale_max == FLT_MAX) + { + float v_min = FLT_MAX; + float v_max = -FLT_MAX; + for (int i = 0; i < values_count; i++) + { + const float v = values_getter(data, i); + if (v != v) // Ignore NaN values + continue; + v_min = ImMin(v_min, v); + v_max = ImMax(v_max, v); + } + if (scale_min == FLT_MAX) + scale_min = v_min; + if (scale_max == FLT_MAX) + scale_max = v_max; + } + + RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); + + const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1; + if (values_count >= values_count_min) + { + int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); + int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); + + // Tooltip on hover + int v_hovered = -1; + if (hovered && inner_bb.Contains(g.IO.MousePos)) + { + const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); + const int v_idx = (int)(t * item_count); + IM_ASSERT(v_idx >= 0 && v_idx < values_count); + + const float v0 = values_getter(data, (v_idx + values_offset) % values_count); + const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count); + if (plot_type == ImGuiPlotType_Lines) + SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1); + else if (plot_type == ImGuiPlotType_Histogram) + SetTooltip("%d: %8.4g", v_idx, v0); + v_hovered = v_idx; + } + + const float t_step = 1.0f / (float)res_w; + const float inv_scale = (scale_min == scale_max) ? 0.0f : (1.0f / (scale_max - scale_min)); + + float v0 = values_getter(data, (0 + values_offset) % values_count); + float t0 = 0.0f; + ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) ); // Point in the normalized space of our target rectangle + float histogram_zero_line_t = (scale_min * scale_max < 0.0f) ? (-scale_min * inv_scale) : (scale_min < 0.0f ? 0.0f : 1.0f); // Where does the zero line stands + + const ImU32 col_base = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram); + const ImU32 col_hovered = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered); + + for (int n = 0; n < res_w; n++) + { + const float t1 = t0 + t_step; + const int v1_idx = (int)(t0 * item_count + 0.5f); + IM_ASSERT(v1_idx >= 0 && v1_idx < values_count); + const float v1 = values_getter(data, (v1_idx + values_offset + 1) % values_count); + const ImVec2 tp1 = ImVec2( t1, 1.0f - ImSaturate((v1 - scale_min) * inv_scale) ); + + // NB: Draw calls are merged together by the DrawList system. Still, we should render our batch are lower level to save a bit of CPU. + ImVec2 pos0 = ImLerp(inner_bb.Min, inner_bb.Max, tp0); + ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, histogram_zero_line_t)); + if (plot_type == ImGuiPlotType_Lines) + { + window->DrawList->AddLine(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + } + else if (plot_type == ImGuiPlotType_Histogram) + { + if (pos1.x >= pos0.x + 2.0f) + pos1.x -= 1.0f; + window->DrawList->AddRectFilled(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + } + + t0 = t1; + tp0 = tp1; + } + } + + // Text overlay + if (overlay_text) + RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f,0.0f)); + + if (label_size.x > 0.0f) + RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); +} + +struct ImGuiPlotArrayGetterData +{ + const float* Values; + int Stride; + + ImGuiPlotArrayGetterData(const float* values, int stride) { Values = values; Stride = stride; } +}; + +static float Plot_ArrayGetter(void* data, int idx) +{ + ImGuiPlotArrayGetterData* plot_data = (ImGuiPlotArrayGetterData*)data; + const float v = *(const float*)(const void*)((const unsigned char*)plot_data->Values + (size_t)idx * plot_data->Stride); + return v; +} + +void ImGui::PlotLines(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride) +{ + ImGuiPlotArrayGetterData data(values, stride); + PlotEx(ImGuiPlotType_Lines, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); +} + +void ImGui::PlotLines(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size) +{ + PlotEx(ImGuiPlotType_Lines, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); +} + +void ImGui::PlotHistogram(const char* label, const float* values, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size, int stride) +{ + ImGuiPlotArrayGetterData data(values, stride); + PlotEx(ImGuiPlotType_Histogram, label, &Plot_ArrayGetter, (void*)&data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); +} + +void ImGui::PlotHistogram(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 graph_size) +{ + PlotEx(ImGuiPlotType_Histogram, label, values_getter, data, values_count, values_offset, overlay_text, scale_min, scale_max, graph_size); +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Value helpers +// Those is not very useful, legacy API. +//------------------------------------------------------------------------- +// - Value() +//------------------------------------------------------------------------- + +void ImGui::Value(const char* prefix, bool b) +{ + Text("%s: %s", prefix, (b ? "true" : "false")); +} + +void ImGui::Value(const char* prefix, int v) +{ + Text("%s: %d", prefix, v); +} + +void ImGui::Value(const char* prefix, unsigned int v) +{ + Text("%s: %d", prefix, v); +} + +void ImGui::Value(const char* prefix, float v, const char* float_format) +{ + if (float_format) + { + char fmt[64]; + ImFormatString(fmt, IM_ARRAYSIZE(fmt), "%%s: %s", float_format); + Text(fmt, prefix, v); + } + else + { + Text("%s: %.3f", prefix, v); + } +} + +//------------------------------------------------------------------------- +// [SECTION] MenuItem, BeginMenu, EndMenu, etc. +//------------------------------------------------------------------------- +// - ImGuiMenuColumns [Internal] +// - BeginMainMenuBar() +// - EndMainMenuBar() +// - BeginMenuBar() +// - EndMenuBar() +// - BeginMenu() +// - EndMenu() +// - MenuItem() +//------------------------------------------------------------------------- + +// Helpers for internal use +ImGuiMenuColumns::ImGuiMenuColumns() +{ + Spacing = Width = NextWidth = 0.0f; + memset(Pos, 0, sizeof(Pos)); + memset(NextWidths, 0, sizeof(NextWidths)); +} + +void ImGuiMenuColumns::Update(int count, float spacing, bool clear) +{ + IM_ASSERT(count == IM_ARRAYSIZE(Pos)); + IM_UNUSED(count); + Width = NextWidth = 0.0f; + Spacing = spacing; + if (clear) + memset(NextWidths, 0, sizeof(NextWidths)); + for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) + { + if (i > 0 && NextWidths[i] > 0.0f) + Width += Spacing; + Pos[i] = (float)(int)Width; + Width += NextWidths[i]; + NextWidths[i] = 0.0f; + } +} + +float ImGuiMenuColumns::DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double +{ + NextWidth = 0.0f; + NextWidths[0] = ImMax(NextWidths[0], w0); + NextWidths[1] = ImMax(NextWidths[1], w1); + NextWidths[2] = ImMax(NextWidths[2], w2); + for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) + NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f); + return ImMax(Width, NextWidth); +} + +float ImGuiMenuColumns::CalcExtraSpace(float avail_w) +{ + return ImMax(0.0f, avail_w - Width); +} + +// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. +bool ImGui::BeginMainMenuBar() +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); + SetNextWindowPos(ImVec2(0.0f, 0.0f)); + SetNextWindowSize(ImVec2(g.IO.DisplaySize.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y)); + PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0,0)); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; + bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); + PopStyleVar(2); + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); + if (!is_open) + { + End(); + return false; + } + return true; //-V1020 +} + +void ImGui::EndMainMenuBar() +{ + EndMenuBar(); + + // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window + // FIXME: With this strategy we won't be able to restore a NULL focus. + ImGuiContext& g = *GImGui; + if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0 && !g.NavAnyRequest) + FocusTopMostWindowUnderOne(g.NavWindow, NULL); + + End(); +} + +// FIXME: Provided a rectangle perhaps e.g. a BeginMenuBarEx() could be used anywhere.. +// Currently the main responsibility of this function being to setup clip-rect + horizontal layout + menu navigation layer. +// Ideally we also want this to be responsible for claiming space out of the main window scrolling rectangle, in which case ImGuiWindowFlags_MenuBar will become unnecessary. +// Then later the same system could be used for multiple menu-bars, scrollbars, side-bars. +bool ImGui::BeginMenuBar() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + if (!(window->Flags & ImGuiWindowFlags_MenuBar)) + return false; + + IM_ASSERT(!window->DC.MenuBarAppending); + BeginGroup(); // Backup position on layer 0 // FIXME: Misleading to use a group for that backup/restore + PushID("##menubar"); + + // We don't clip with current window clipping rectangle as it is already set to the area below. However we clip with window full rect. + // We remove 1 worth of rounding to Max.x to that text in long menus and small windows don't tend to display over the lower-right rounded area, which looks particularly glitchy. + ImRect bar_rect = window->MenuBarRect(); + ImRect clip_rect(ImFloor(bar_rect.Min.x + 0.5f), ImFloor(bar_rect.Min.y + window->WindowBorderSize + 0.5f), ImFloor(ImMax(bar_rect.Min.x, bar_rect.Max.x - window->WindowRounding) + 0.5f), ImFloor(bar_rect.Max.y + 0.5f)); + clip_rect.ClipWith(window->OuterRectClipped); + PushClipRect(clip_rect.Min, clip_rect.Max, false); + + window->DC.CursorPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); + window->DC.LayoutType = ImGuiLayoutType_Horizontal; + window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); + window->DC.MenuBarAppending = true; + AlignTextToFramePadding(); + return true; +} + +void ImGui::EndMenuBar() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + ImGuiContext& g = *GImGui; + + // Nav: When a move request within one of our child menu failed, capture the request to navigate among our siblings. + if (NavMoveRequestButNoResultYet() && (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) && (g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) + { + ImGuiWindow* nav_earliest_child = g.NavWindow; + while (nav_earliest_child->ParentWindow && (nav_earliest_child->ParentWindow->Flags & ImGuiWindowFlags_ChildMenu)) + nav_earliest_child = nav_earliest_child->ParentWindow; + if (nav_earliest_child->ParentWindow == window && nav_earliest_child->DC.ParentLayoutType == ImGuiLayoutType_Horizontal && g.NavMoveRequestForward == ImGuiNavForward_None) + { + // To do so we claim focus back, restore NavId and then process the movement request for yet another frame. + // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth the hassle/cost) + const ImGuiNavLayer layer = ImGuiNavLayer_Menu; + IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check + FocusWindow(window); + SetNavIDWithRectRel(window->NavLastIds[layer], layer, window->NavRectRel[layer]); + g.NavLayer = layer; + g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. + g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; + NavMoveRequestCancel(); + } + } + + IM_ASSERT(window->Flags & ImGuiWindowFlags_MenuBar); + IM_ASSERT(window->DC.MenuBarAppending); + PopClipRect(); + PopID(); + window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->MenuBarRect().Min.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos. + window->DC.GroupStack.back().EmitItem = false; + EndGroup(); // Restore position on layer 0 + window->DC.LayoutType = ImGuiLayoutType_Vertical; + window->DC.NavLayerCurrent = ImGuiNavLayer_Main; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + window->DC.MenuBarAppending = false; +} + +bool ImGui::BeginMenu(const char* label, bool enabled) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID(label); + + ImVec2 label_size = CalcTextSize(label, NULL, true); + + bool pressed; + bool menu_is_open = IsPopupOpen(id); + bool menuset_is_open = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].OpenParentId == window->IDStack.back()); + ImGuiWindow* backed_nav_window = g.NavWindow; + if (menuset_is_open) + g.NavWindow = window; // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent) + + // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu, + // However the final position is going to be different! It is choosen by FindBestWindowPosForPopup(). + // e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering. + ImVec2 popup_pos, pos = window->DC.CursorPos; + if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) + { + // Menu inside an horizontal menu bar + // Selectable extend their highlight by half ItemSpacing in each direction. + // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() + popup_pos = ImVec2(pos.x - 1.0f - (float)(int)(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); + window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); + PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); + float w = label_size.x; + pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + PopStyleVar(); + window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + } + else + { + // Menu inside a menu + popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); + float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame + float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); + pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + ImU32 text_col = GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled); + RenderArrow(window->DrawList, pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right); + } + + const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id); + if (menuset_is_open) + g.NavWindow = backed_nav_window; + + bool want_open = false; + bool want_close = false; + if (window->DC.LayoutType == ImGuiLayoutType_Vertical) // (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) + { + // Close menu when not hovering it anymore unless we are moving roughly in the direction of the menu + // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers, so menus feels more reactive. + bool moving_toward_other_child_menu = false; + + ImGuiWindow* child_menu_window = (g.BeginPopupStack.Size < g.OpenPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].SourceWindow == window) ? g.OpenPopupStack[g.BeginPopupStack.Size].Window : NULL; + if (g.HoveredWindow == window && child_menu_window != NULL && !(window->Flags & ImGuiWindowFlags_MenuBar)) + { + // FIXME-DPI: Values should be derived from a master "scale" factor. + ImRect next_window_rect = child_menu_window->Rect(); + ImVec2 ta = g.IO.MousePos - g.IO.MouseDelta; + ImVec2 tb = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR(); + ImVec2 tc = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR(); + float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, 5.0f, 30.0f); // add a bit of extra slack. + ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues + tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -100.0f); // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus // FIXME: Multiply by fb_scale? + tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +100.0f); + moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); + //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] + } + if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu) + want_close = true; + + if (!menu_is_open && hovered && pressed) // Click to open + want_open = true; + else if (!menu_is_open && hovered && !moving_toward_other_child_menu) // Hover to open + want_open = true; + + if (g.NavActivateId == id) + { + want_close = menu_is_open; + want_open = !menu_is_open; + } + if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open + { + want_open = true; + NavMoveRequestCancel(); + } + } + else + { + // Menu bar + if (menu_is_open && pressed && menuset_is_open) // Click an open menu again to close it + { + want_close = true; + want_open = menu_is_open = false; + } + else if (pressed || (hovered && menuset_is_open && !menu_is_open)) // First click to open, then hover to open others + { + want_open = true; + } + else if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Down) // Nav-Down to open + { + want_open = true; + NavMoveRequestCancel(); + } + } + + if (!enabled) // explicitly close if an open menu becomes disabled, facilitate users code a lot in pattern such as 'if (BeginMenu("options", has_object)) { ..use object.. }' + want_close = true; + if (want_close && IsPopupOpen(id)) + ClosePopupToLevel(g.BeginPopupStack.Size, true); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); + + if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) + { + // Don't recycle same menu level in the same frame, first close the other menu and yield for a frame. + OpenPopup(label); + return false; + } + + menu_is_open |= want_open; + if (want_open) + OpenPopup(label); + + if (menu_is_open) + { + // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) + SetNextWindowPos(popup_pos, ImGuiCond_Always); + ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; + if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) + flags |= ImGuiWindowFlags_ChildWindow; + menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + } + + return menu_is_open; +} + +void ImGui::EndMenu() +{ + // Nav: When a left move request _within our child menu_ failed, close ourselves (the _parent_ menu). + // A menu doesn't close itself because EndMenuBar() wants the catch the last Left<>Right inputs. + // However, it means that with the current code, a BeginMenu() from outside another menu or a menu-bar won't be closable with the Left direction. + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (g.NavWindow && g.NavWindow->ParentWindow == window && g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical) + { + ClosePopupToLevel(g.BeginPopupStack.Size, true); + NavMoveRequestCancel(); + } + + EndPopup(); +} + +bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + ImGuiStyle& style = g.Style; + ImVec2 pos = window->DC.CursorPos; + ImVec2 label_size = CalcTextSize(label, NULL, true); + + // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73), + // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only. + ImGuiSelectableFlags flags = ImGuiSelectableFlags_PressedOnRelease | ImGuiSelectableFlags_SetNavIdOnHover | (enabled ? 0 : ImGuiSelectableFlags_Disabled); + bool pressed; + if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) + { + // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful + // Note that in this situation we render neither the shortcut neither the selected tick mark + float w = label_size.x; + window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); + PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); + pressed = Selectable(label, false, flags, ImVec2(w, 0.0f)); + PopStyleVar(); + window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + } + else + { + ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f); + float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame + float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); + pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f)); + if (shortcut_size.x > 0.0f) + { + PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); + RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); + PopStyleColor(); + } + if (selected) + RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); + } + + IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); + return pressed; +} + +bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled) +{ + if (MenuItem(label, shortcut, p_selected ? *p_selected : false, enabled)) + { + if (p_selected) + *p_selected = !*p_selected; + return true; + } + return false; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: BeginTabBar, EndTabBar, etc. +//------------------------------------------------------------------------- +// [BETA API] API may evolve! This code has been extracted out of the Docking branch, +// and some of the construct which are not used in Master may be left here to facilitate merging. +//------------------------------------------------------------------------- +// - BeginTabBar() +// - BeginTabBarEx() [Internal] +// - EndTabBar() +// - TabBarLayout() [Internal] +// - TabBarCalcTabID() [Internal] +// - TabBarCalcMaxTabWidth() [Internal] +// - TabBarFindTabById() [Internal] +// - TabBarRemoveTab() [Internal] +// - TabBarCloseTab() [Internal] +// - TabBarScrollClamp()v +// - TabBarScrollToTab() [Internal] +// - TabBarQueueChangeTabOrder() [Internal] +// - TabBarScrollingButtons() [Internal] +// - TabBarTabListPopupButton() [Internal] +//------------------------------------------------------------------------- + +namespace ImGui +{ + static void TabBarLayout(ImGuiTabBar* tab_bar); + static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label); + static float TabBarCalcMaxTabWidth(); + static float TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling); + static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); + static ImGuiTabItem* TabBarScrollingButtons(ImGuiTabBar* tab_bar); + static ImGuiTabItem* TabBarTabListPopupButton(ImGuiTabBar* tab_bar); +} + +ImGuiTabBar::ImGuiTabBar() +{ + ID = 0; + SelectedTabId = NextSelectedTabId = VisibleTabId = 0; + CurrFrameVisible = PrevFrameVisible = -1; + LastTabContentHeight = 0.0f; + OffsetMax = OffsetMaxIdeal = OffsetNextTab = 0.0f; + ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f; + Flags = ImGuiTabBarFlags_None; + ReorderRequestTabId = 0; + ReorderRequestDir = 0; + WantLayout = VisibleTabWasSubmitted = false; + LastTabItemIdx = -1; +} + +static int IMGUI_CDECL TabItemComparerByVisibleOffset(const void* lhs, const void* rhs) +{ + const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; + const ImGuiTabItem* b = (const ImGuiTabItem*)rhs; + return (int)(a->Offset - b->Offset); +} + +static ImGuiTabBar* GetTabBarFromTabBarRef(const ImGuiPtrOrIndex& ref) +{ + ImGuiContext& g = *GImGui; + return ref.Ptr ? (ImGuiTabBar*)ref.Ptr : g.TabBars.GetByIndex(ref.Index); +} + +static ImGuiPtrOrIndex GetTabBarRefFromTabBar(ImGuiTabBar* tab_bar) +{ + ImGuiContext& g = *GImGui; + if (g.TabBars.Contains(tab_bar)) + return ImGuiPtrOrIndex(g.TabBars.GetIndex(tab_bar)); + return ImGuiPtrOrIndex(tab_bar); +} + +bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + ImGuiID id = window->GetID(str_id); + ImGuiTabBar* tab_bar = g.TabBars.GetOrAddByKey(id); + ImRect tab_bar_bb = ImRect(window->DC.CursorPos.x, window->DC.CursorPos.y, window->WorkRect.Max.x, window->DC.CursorPos.y + g.FontSize + g.Style.FramePadding.y * 2); + tab_bar->ID = id; + return BeginTabBarEx(tab_bar, tab_bar_bb, flags | ImGuiTabBarFlags_IsFocused); +} + +bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImGuiTabBarFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + if ((flags & ImGuiTabBarFlags_DockNode) == 0) + PushOverrideID(tab_bar->ID); + + // Add to stack + g.CurrentTabBarStack.push_back(GetTabBarRefFromTabBar(tab_bar)); + g.CurrentTabBar = tab_bar; + + if (tab_bar->CurrFrameVisible == g.FrameCount) + { + //IMGUI_DEBUG_LOG("BeginTabBarEx already called this frame\n", g.FrameCount); + IM_ASSERT(0); + return true; + } + + // When toggling back from ordered to manually-reorderable, shuffle tabs to enforce the last visible order. + // Otherwise, the most recently inserted tabs would move at the end of visible list which can be a little too confusing or magic for the user. + if ((flags & ImGuiTabBarFlags_Reorderable) && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1 && tab_bar->PrevFrameVisible != -1) + ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByVisibleOffset); + + // Flags + if ((flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) + flags |= ImGuiTabBarFlags_FittingPolicyDefault_; + + tab_bar->Flags = flags; + tab_bar->BarRect = tab_bar_bb; + tab_bar->WantLayout = true; // Layout will be done on the first call to ItemTab() + tab_bar->PrevFrameVisible = tab_bar->CurrFrameVisible; + tab_bar->CurrFrameVisible = g.FrameCount; + tab_bar->FramePadding = g.Style.FramePadding; + + // Layout + ItemSize(ImVec2(tab_bar->OffsetMaxIdeal, tab_bar->BarRect.GetHeight())); + window->DC.CursorPos.x = tab_bar->BarRect.Min.x; + + // Draw separator + const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); + const float y = tab_bar->BarRect.Max.y - 1.0f; + { + const float separator_min_x = tab_bar->BarRect.Min.x - ImFloor(window->WindowPadding.x * 0.5f); + const float separator_max_x = tab_bar->BarRect.Max.x + ImFloor(window->WindowPadding.x * 0.5f); + window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f); + } + return true; +} + +void ImGui::EndTabBar() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + ImGuiTabBar* tab_bar = g.CurrentTabBar; + if (tab_bar == NULL) + { + IM_ASSERT(tab_bar != NULL && "Mismatched BeginTabBar()/EndTabBar()!"); + return; // FIXME-ERRORHANDLING + } + if (tab_bar->WantLayout) + TabBarLayout(tab_bar); + + // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed(). + const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); + if (tab_bar->VisibleTabWasSubmitted || tab_bar->VisibleTabId == 0 || tab_bar_appearing) + tab_bar->LastTabContentHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f); + else + window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->LastTabContentHeight; + + if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) + PopID(); + + g.CurrentTabBarStack.pop_back(); + g.CurrentTabBar = g.CurrentTabBarStack.empty() ? NULL : GetTabBarFromTabBarRef(g.CurrentTabBarStack.back()); +} + +// This is called only once a frame before by the first call to ItemTab() +// The reason we're not calling it in BeginTabBar() is to leave a chance to the user to call the SetTabItemClosed() functions. +static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) +{ + ImGuiContext& g = *GImGui; + tab_bar->WantLayout = false; + + // Garbage collect + int tab_dst_n = 0; + for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n]; + if (tab->LastFrameVisible < tab_bar->PrevFrameVisible) + { + if (tab->ID == tab_bar->SelectedTabId) + tab_bar->SelectedTabId = 0; + continue; + } + if (tab_dst_n != tab_src_n) + tab_bar->Tabs[tab_dst_n] = tab_bar->Tabs[tab_src_n]; + tab_dst_n++; + } + if (tab_bar->Tabs.Size != tab_dst_n) + tab_bar->Tabs.resize(tab_dst_n); + + // Setup next selected tab + ImGuiID scroll_track_selected_tab_id = 0; + if (tab_bar->NextSelectedTabId) + { + tab_bar->SelectedTabId = tab_bar->NextSelectedTabId; + tab_bar->NextSelectedTabId = 0; + scroll_track_selected_tab_id = tab_bar->SelectedTabId; + } + + // Process order change request (we could probably process it when requested but it's just saner to do it in a single spot). + if (tab_bar->ReorderRequestTabId != 0) + { + if (ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId)) + { + //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools + int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir; + if (tab2_order >= 0 && tab2_order < tab_bar->Tabs.Size) + { + ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; + ImGuiTabItem item_tmp = *tab1; + *tab1 = *tab2; + *tab2 = item_tmp; + if (tab2->ID == tab_bar->SelectedTabId) + scroll_track_selected_tab_id = tab2->ID; + tab1 = tab2 = NULL; + } + if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) + MarkIniSettingsDirty(); + } + tab_bar->ReorderRequestTabId = 0; + } + + // Tab List Popup (will alter tab_bar->BarRect and therefore the available width!) + const bool tab_list_popup_button = (tab_bar->Flags & ImGuiTabBarFlags_TabListPopupButton) != 0; + if (tab_list_popup_button) + if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Max.x! + scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; + + // Compute ideal widths + g.ShrinkWidthBuffer.resize(tab_bar->Tabs.Size); + float width_total_contents = 0.0f; + ImGuiTabItem* most_recently_selected_tab = NULL; + bool found_selected_tab_id = false; + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + IM_ASSERT(tab->LastFrameVisible >= tab_bar->PrevFrameVisible); + + if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected) + most_recently_selected_tab = tab; + if (tab->ID == tab_bar->SelectedTabId) + found_selected_tab_id = true; + + // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar. + // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, + // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window. + const char* tab_name = tab_bar->GetTabName(tab); + const bool has_close_button = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) ? false : true; + tab->WidthContents = TabItemCalcSize(tab_name, has_close_button).x; + + width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->WidthContents; + + // Store data so we can build an array sorted by width if we need to shrink tabs down + g.ShrinkWidthBuffer[tab_n].Index = tab_n; + g.ShrinkWidthBuffer[tab_n].Width = tab->WidthContents; + } + + // Compute width + const float initial_offset_x = 0.0f; // g.Style.ItemInnerSpacing.x; + const float width_avail = ImMax(tab_bar->BarRect.GetWidth() - initial_offset_x, 0.0f); + float width_excess = (width_avail < width_total_contents) ? (width_total_contents - width_avail) : 0.0f; + if (width_excess > 0.0f && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown)) + { + // If we don't have enough room, resize down the largest tabs first + ShrinkWidths(g.ShrinkWidthBuffer.Data, g.ShrinkWidthBuffer.Size, width_excess); + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index].Width = (float)(int)g.ShrinkWidthBuffer[tab_n].Width; + } + else + { + const float tab_max_width = TabBarCalcMaxTabWidth(); + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + tab->Width = ImMin(tab->WidthContents, tab_max_width); + IM_ASSERT(tab->Width > 0.0f); + } + } + + // Layout all active tabs + float offset_x = initial_offset_x; + float offset_x_ideal = offset_x; + tab_bar->OffsetNextTab = offset_x; // This is used by non-reorderable tab bar where the submission order is always honored. + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + tab->Offset = offset_x; + if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) + scroll_track_selected_tab_id = tab->ID; + offset_x += tab->Width + g.Style.ItemInnerSpacing.x; + offset_x_ideal += tab->WidthContents + g.Style.ItemInnerSpacing.x; + } + tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); + tab_bar->OffsetMaxIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); + + // Horizontal scrolling buttons + const bool scrolling_buttons = (tab_bar->OffsetMax > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll); + if (scrolling_buttons) + if (ImGuiTabItem* tab_to_select = TabBarScrollingButtons(tab_bar)) // NB: Will alter BarRect.Max.x! + scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; + + // If we have lost the selected tab, select the next most recently active one + if (found_selected_tab_id == false) + tab_bar->SelectedTabId = 0; + if (tab_bar->SelectedTabId == 0 && tab_bar->NextSelectedTabId == 0 && most_recently_selected_tab != NULL) + scroll_track_selected_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID; + + // Lock in visible tab + tab_bar->VisibleTabId = tab_bar->SelectedTabId; + tab_bar->VisibleTabWasSubmitted = false; + + // Update scrolling + if (scroll_track_selected_tab_id) + if (ImGuiTabItem* scroll_track_selected_tab = TabBarFindTabByID(tab_bar, scroll_track_selected_tab_id)) + TabBarScrollToTab(tab_bar, scroll_track_selected_tab); + tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim); + tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget); + if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget) + { + // Scrolling speed adjust itself so we can always reach our target in 1/3 seconds. + // Teleport if we are aiming far off the visible line + tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, 70.0f * g.FontSize); + tab_bar->ScrollingSpeed = ImMax(tab_bar->ScrollingSpeed, ImFabs(tab_bar->ScrollingTarget - tab_bar->ScrollingAnim) / 0.3f); + const bool teleport = (tab_bar->PrevFrameVisible + 1 < g.FrameCount) || (tab_bar->ScrollingTargetDistToVisibility > 10.0f * g.FontSize); + tab_bar->ScrollingAnim = teleport ? tab_bar->ScrollingTarget : ImLinearSweep(tab_bar->ScrollingAnim, tab_bar->ScrollingTarget, g.IO.DeltaTime * tab_bar->ScrollingSpeed); + } + else + { + tab_bar->ScrollingSpeed = 0.0f; + } + + // Clear name buffers + if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) + tab_bar->TabsNames.Buf.resize(0); +} + +// Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack. +static ImU32 ImGui::TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label) +{ + if (tab_bar->Flags & ImGuiTabBarFlags_DockNode) + { + ImGuiID id = ImHashStr(label); + KeepAliveID(id); + return id; + } + else + { + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(label); + } +} + +static float ImGui::TabBarCalcMaxTabWidth() +{ + ImGuiContext& g = *GImGui; + return g.FontSize * 20.0f; +} + +ImGuiTabItem* ImGui::TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id) +{ + if (tab_id != 0) + for (int n = 0; n < tab_bar->Tabs.Size; n++) + if (tab_bar->Tabs[n].ID == tab_id) + return &tab_bar->Tabs[n]; + return NULL; +} + +// The *TabId fields be already set by the docking system _before_ the actual TabItem was created, so we clear them regardless. +void ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id) +{ + if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id)) + tab_bar->Tabs.erase(tab); + if (tab_bar->VisibleTabId == tab_id) { tab_bar->VisibleTabId = 0; } + if (tab_bar->SelectedTabId == tab_id) { tab_bar->SelectedTabId = 0; } + if (tab_bar->NextSelectedTabId == tab_id) { tab_bar->NextSelectedTabId = 0; } +} + +// Called on manual closure attempt +void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) +{ + if ((tab_bar->VisibleTabId == tab->ID) && !(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) + { + // This will remove a frame of lag for selecting another tab on closure. + // However we don't run it in the case where the 'Unsaved' flag is set, so user gets a chance to fully undo the closure + tab->LastFrameVisible = -1; + tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = 0; + } + else if ((tab_bar->VisibleTabId != tab->ID) && (tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) + { + // Actually select before expecting closure + tab_bar->NextSelectedTabId = tab->ID; + } +} + +static float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling) +{ + scrolling = ImMin(scrolling, tab_bar->OffsetMax - tab_bar->BarRect.GetWidth()); + return ImMax(scrolling, 0.0f); +} + +static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) +{ + ImGuiContext& g = *GImGui; + float margin = g.FontSize * 1.0f; // When to scroll to make Tab N+1 visible always make a bit of N visible to suggest more scrolling area (since we don't have a scrollbar) + int order = tab_bar->GetTabOrder(tab); + float tab_x1 = tab->Offset + (order > 0 ? -margin : 0.0f); + float tab_x2 = tab->Offset + tab->Width + (order + 1 < tab_bar->Tabs.Size ? margin : 1.0f); + tab_bar->ScrollingTargetDistToVisibility = 0.0f; + if (tab_bar->ScrollingTarget > tab_x1 || (tab_x2 - tab_x1 >= tab_bar->BarRect.GetWidth())) + { + tab_bar->ScrollingTargetDistToVisibility = ImMax(tab_bar->ScrollingAnim - tab_x2, 0.0f); + tab_bar->ScrollingTarget = tab_x1; + } + else if (tab_bar->ScrollingTarget < tab_x2 - tab_bar->BarRect.GetWidth()) + { + tab_bar->ScrollingTargetDistToVisibility = ImMax((tab_x1 - tab_bar->BarRect.GetWidth()) - tab_bar->ScrollingAnim, 0.0f); + tab_bar->ScrollingTarget = tab_x2 - tab_bar->BarRect.GetWidth(); + } +} + +void ImGui::TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir) +{ + IM_ASSERT(dir == -1 || dir == +1); + IM_ASSERT(tab_bar->ReorderRequestTabId == 0); + tab_bar->ReorderRequestTabId = tab->ID; + tab_bar->ReorderRequestDir = (ImS8)dir; +} + +static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + const ImVec2 arrow_button_size(g.FontSize - 2.0f, g.FontSize + g.Style.FramePadding.y * 2.0f); + const float scrolling_buttons_width = arrow_button_size.x * 2.0f; + + const ImVec2 backup_cursor_pos = window->DC.CursorPos; + //window->DrawList->AddRect(ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y), ImVec2(tab_bar->BarRect.Max.x, tab_bar->BarRect.Max.y), IM_COL32(255,0,0,255)); + + const ImRect avail_bar_rect = tab_bar->BarRect; + bool want_clip_rect = !avail_bar_rect.Contains(ImRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(scrolling_buttons_width, 0.0f))); + if (want_clip_rect) + PushClipRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max + ImVec2(g.Style.ItemInnerSpacing.x, 0.0f), true); + + ImGuiTabItem* tab_to_select = NULL; + + int select_dir = 0; + ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text]; + arrow_col.w *= 0.5f; + + PushStyleColor(ImGuiCol_Text, arrow_col); + PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + const float backup_repeat_delay = g.IO.KeyRepeatDelay; + const float backup_repeat_rate = g.IO.KeyRepeatRate; + g.IO.KeyRepeatDelay = 0.250f; + g.IO.KeyRepeatRate = 0.200f; + window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y); + if (ArrowButtonEx("##<", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) + select_dir = -1; + window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width + arrow_button_size.x, tab_bar->BarRect.Min.y); + if (ArrowButtonEx("##>", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) + select_dir = +1; + PopStyleColor(2); + g.IO.KeyRepeatRate = backup_repeat_rate; + g.IO.KeyRepeatDelay = backup_repeat_delay; + + if (want_clip_rect) + PopClipRect(); + + if (select_dir != 0) + if (ImGuiTabItem* tab_item = TabBarFindTabByID(tab_bar, tab_bar->SelectedTabId)) + { + int selected_order = tab_bar->GetTabOrder(tab_item); + int target_order = selected_order + select_dir; + tab_to_select = &tab_bar->Tabs[(target_order >= 0 && target_order < tab_bar->Tabs.Size) ? target_order : selected_order]; // If we are at the end of the list, still scroll to make our tab visible + } + window->DC.CursorPos = backup_cursor_pos; + tab_bar->BarRect.Max.x -= scrolling_buttons_width + 1.0f; + + return tab_to_select; +} + +static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + // We use g.Style.FramePadding.y to match the square ArrowButton size + const float tab_list_popup_button_width = g.FontSize + g.Style.FramePadding.y; + const ImVec2 backup_cursor_pos = window->DC.CursorPos; + window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x - g.Style.FramePadding.y, tab_bar->BarRect.Min.y); + tab_bar->BarRect.Min.x += tab_list_popup_button_width; + + ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text]; + arrow_col.w *= 0.5f; + PushStyleColor(ImGuiCol_Text, arrow_col); + PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview); + PopStyleColor(2); + + ImGuiTabItem* tab_to_select = NULL; + if (open) + { + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + const char* tab_name = tab_bar->GetTabName(tab); + if (Selectable(tab_name, tab_bar->SelectedTabId == tab->ID)) + tab_to_select = tab; + } + EndCombo(); + } + + window->DC.CursorPos = backup_cursor_pos; + return tab_to_select; +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: BeginTabItem, EndTabItem, etc. +//------------------------------------------------------------------------- +// [BETA API] API may evolve! This code has been extracted out of the Docking branch, +// and some of the construct which are not used in Master may be left here to facilitate merging. +//------------------------------------------------------------------------- +// - BeginTabItem() +// - EndTabItem() +// - TabItemEx() [Internal] +// - SetTabItemClosed() +// - TabItemCalcSize() [Internal] +// - TabItemBackground() [Internal] +// - TabItemLabelAndCloseButton() [Internal] +//------------------------------------------------------------------------- + +bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + ImGuiTabBar* tab_bar = g.CurrentTabBar; + if (tab_bar == NULL) + { + IM_ASSERT(tab_bar && "Needs to be called between BeginTabBar() and EndTabBar()!"); + return false; // FIXME-ERRORHANDLING + } + bool ret = TabItemEx(tab_bar, label, p_open, flags); + if (ret && !(flags & ImGuiTabItemFlags_NoPushId)) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; + PushOverrideID(tab->ID); // We already hashed 'label' so push into the ID stack directly instead of doing another hash through PushID(label) + } + return ret; +} + +void ImGui::EndTabItem() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + ImGuiTabBar* tab_bar = g.CurrentTabBar; + if (tab_bar == NULL) + { + IM_ASSERT(tab_bar != NULL && "Needs to be called between BeginTabBar() and EndTabBar()!"); + return; + } + IM_ASSERT(tab_bar->LastTabItemIdx >= 0); + ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; + if (!(tab->Flags & ImGuiTabItemFlags_NoPushId)) + window->IDStack.pop_back(); +} + +bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags) +{ + // Layout whole tab bar if not already done + if (tab_bar->WantLayout) + TabBarLayout(tab_bar); + + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + const ImGuiStyle& style = g.Style; + const ImGuiID id = TabBarCalcTabID(tab_bar, label); + + // If the user called us with *p_open == false, we early out and don't render. We make a dummy call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. + if (p_open && !*p_open) + { + PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); + ItemAdd(ImRect(), id); + PopItemFlag(); + return false; + } + + // Calculate tab contents size + ImVec2 size = TabItemCalcSize(label, p_open != NULL); + + // Acquire tab data + ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, id); + bool tab_is_new = false; + if (tab == NULL) + { + tab_bar->Tabs.push_back(ImGuiTabItem()); + tab = &tab_bar->Tabs.back(); + tab->ID = id; + tab->Width = size.x; + tab_is_new = true; + } + tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab); + tab->WidthContents = size.x; + + if (p_open == NULL) + flags |= ImGuiTabItemFlags_NoCloseButton; + + const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); + const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; + const bool tab_appearing = (tab->LastFrameVisible + 1 < g.FrameCount); + tab->LastFrameVisible = g.FrameCount; + tab->Flags = flags; + + // Append name with zero-terminator + tab->NameOffset = tab_bar->TabsNames.size(); + tab_bar->TabsNames.append(label, label + strlen(label) + 1); + + // If we are not reorderable, always reset offset based on submission order. + // (We already handled layout and sizing using the previous known order, but sizing is not affected by order!) + if (!tab_appearing && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable)) + { + tab->Offset = tab_bar->OffsetNextTab; + tab_bar->OffsetNextTab += tab->Width + g.Style.ItemInnerSpacing.x; + } + + // Update selected tab + if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) + if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) + tab_bar->NextSelectedTabId = id; // New tabs gets activated + if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // SetSelected can only be passed on explicit tab bar + tab_bar->NextSelectedTabId = id; + + // Lock visibility + bool tab_contents_visible = (tab_bar->VisibleTabId == id); + if (tab_contents_visible) + tab_bar->VisibleTabWasSubmitted = true; + + // On the very first frame of a tab bar we let first tab contents be visible to minimize appearing glitches + if (!tab_contents_visible && tab_bar->SelectedTabId == 0 && tab_bar_appearing) + if (tab_bar->Tabs.Size == 1 && !(tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs)) + tab_contents_visible = true; + + if (tab_appearing && !(tab_bar_appearing && !tab_is_new)) + { + PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); + ItemAdd(ImRect(), id); + PopItemFlag(); + return tab_contents_visible; + } + + if (tab_bar->SelectedTabId == id) + tab->LastFrameSelected = g.FrameCount; + + // Backup current layout position + const ImVec2 backup_main_cursor_pos = window->DC.CursorPos; + + // Layout + size.x = tab->Width; + window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2((float)(int)tab->Offset - tab_bar->ScrollingAnim, 0.0f); + ImVec2 pos = window->DC.CursorPos; + ImRect bb(pos, pos + size); + + // We don't have CPU clipping primitives to clip the CloseButton (until it becomes a texture), so need to add an extra draw call (temporary in the case of vertical animation) + bool want_clip_rect = (bb.Min.x < tab_bar->BarRect.Min.x) || (bb.Max.x > tab_bar->BarRect.Max.x); + if (want_clip_rect) + PushClipRect(ImVec2(ImMax(bb.Min.x, tab_bar->BarRect.Min.x), bb.Min.y - 1), ImVec2(tab_bar->BarRect.Max.x, bb.Max.y), true); + + ImVec2 backup_cursor_max_pos = window->DC.CursorMaxPos; + ItemSize(bb.GetSize(), style.FramePadding.y); + window->DC.CursorMaxPos = backup_cursor_max_pos; + + if (!ItemAdd(bb, id)) + { + if (want_clip_rect) + PopClipRect(); + window->DC.CursorPos = backup_main_cursor_pos; + return tab_contents_visible; + } + + // Click to Select a tab + ImGuiButtonFlags button_flags = (ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_AllowItemOverlap); + if (g.DragDropActive) + button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); + if (pressed) + tab_bar->NextSelectedTabId = id; + hovered |= (g.HoveredId == id); + + // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) + if (!held) + SetItemAllowOverlap(); + + // Drag and drop: re-order tabs + if (held && !tab_appearing && IsMouseDragging(0)) + { + if (!g.DragDropActive && (tab_bar->Flags & ImGuiTabBarFlags_Reorderable)) + { + // While moving a tab it will jump on the other side of the mouse, so we also test for MouseDelta.x + if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < bb.Min.x) + { + if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) + TabBarQueueChangeTabOrder(tab_bar, tab, -1); + } + else if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > bb.Max.x) + { + if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) + TabBarQueueChangeTabOrder(tab_bar, tab, +1); + } + } + } + +#if 0 + if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->WidthContents) + { + // Enlarge tab display when hovering + bb.Max.x = bb.Min.x + (float)(int)ImLerp(bb.GetWidth(), tab->WidthContents, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f)); + display_draw_list = GetForegroundDrawList(window); + TabItemBackground(display_draw_list, bb, flags, GetColorU32(ImGuiCol_TitleBgActive)); + } +#endif + + // Render tab shape + ImDrawList* display_draw_list = window->DrawList; + const ImU32 tab_col = GetColorU32((held || hovered) ? ImGuiCol_TabHovered : tab_contents_visible ? (tab_bar_focused ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive) : (tab_bar_focused ? ImGuiCol_Tab : ImGuiCol_TabUnfocused)); + TabItemBackground(display_draw_list, bb, flags, tab_col); + RenderNavHighlight(bb, id); + + // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. + const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); + if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1))) + tab_bar->NextSelectedTabId = id; + + if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) + flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton; + + // Render tab label, process close button + const ImGuiID close_button_id = p_open ? window->GetID((void*)((intptr_t)id + 1)) : 0; + bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id); + if (just_closed && p_open != NULL) + { + *p_open = false; + TabBarCloseTab(tab_bar, tab); + } + + // Restore main window position so user can draw there + if (want_clip_rect) + PopClipRect(); + window->DC.CursorPos = backup_main_cursor_pos; + + // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) + // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) + if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) + if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip)) + SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); + + return tab_contents_visible; +} + +// [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed. +// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem() +void ImGui::SetTabItemClosed(const char* label) +{ + ImGuiContext& g = *GImGui; + bool is_within_manual_tab_bar = g.CurrentTabBar && !(g.CurrentTabBar->Flags & ImGuiTabBarFlags_DockNode); + if (is_within_manual_tab_bar) + { + ImGuiTabBar* tab_bar = g.CurrentTabBar; + IM_ASSERT(tab_bar->WantLayout); // Needs to be called AFTER BeginTabBar() and BEFORE the first call to BeginTabItem() + ImGuiID tab_id = TabBarCalcTabID(tab_bar, label); + TabBarRemoveTab(tab_bar, tab_id); + } +} + +ImVec2 ImGui::TabItemCalcSize(const char* label, bool has_close_button) +{ + ImGuiContext& g = *GImGui; + ImVec2 label_size = CalcTextSize(label, NULL, true); + ImVec2 size = ImVec2(label_size.x + g.Style.FramePadding.x, label_size.y + g.Style.FramePadding.y * 2.0f); + if (has_close_button) + size.x += g.Style.FramePadding.x + (g.Style.ItemInnerSpacing.x + g.FontSize); // We use Y intentionally to fit the close button circle. + else + size.x += g.Style.FramePadding.x + 1.0f; + return ImVec2(ImMin(size.x, TabBarCalcMaxTabWidth()), size.y); +} + +void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col) +{ + // While rendering tabs, we trim 1 pixel off the top of our bounding box so they can fit within a regular frame height while looking "detached" from it. + ImGuiContext& g = *GImGui; + const float width = bb.GetWidth(); + IM_UNUSED(flags); + IM_ASSERT(width > 0.0f); + const float rounding = ImMax(0.0f, ImMin(g.Style.TabRounding, width * 0.5f - 1.0f)); + const float y1 = bb.Min.y + 1.0f; + const float y2 = bb.Max.y - 1.0f; + draw_list->PathLineTo(ImVec2(bb.Min.x, y2)); + draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding, y1 + rounding), rounding, 6, 9); + draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding, y1 + rounding), rounding, 9, 12); + draw_list->PathLineTo(ImVec2(bb.Max.x, y2)); + draw_list->PathFillConvex(col); + if (g.Style.TabBorderSize > 0.0f) + { + draw_list->PathLineTo(ImVec2(bb.Min.x + 0.5f, y2)); + draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding + 0.5f, y1 + rounding + 0.5f), rounding, 6, 9); + draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding - 0.5f, y1 + rounding + 0.5f), rounding, 9, 12); + draw_list->PathLineTo(ImVec2(bb.Max.x - 0.5f, y2)); + draw_list->PathStroke(GetColorU32(ImGuiCol_Border), false, g.Style.TabBorderSize); + } +} + +// Render text label (with custom clipping) + Unsaved Document marker + Close Button logic +// We tend to lock style.FramePadding for a given tab-bar, hence the 'frame_padding' parameter. +bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id) +{ + ImGuiContext& g = *GImGui; + ImVec2 label_size = CalcTextSize(label, NULL, true); + if (bb.GetWidth() <= 1.0f) + return false; + + // Render text label (with clipping + alpha gradient) + unsaved marker + const char* TAB_UNSAVED_MARKER = "*"; + ImRect text_pixel_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y); + if (flags & ImGuiTabItemFlags_UnsavedDocument) + { + text_pixel_clip_bb.Max.x -= CalcTextSize(TAB_UNSAVED_MARKER, NULL, false).x; + ImVec2 unsaved_marker_pos(ImMin(bb.Min.x + frame_padding.x + label_size.x + 2, text_pixel_clip_bb.Max.x), bb.Min.y + frame_padding.y + (float)(int)(-g.FontSize * 0.25f)); + RenderTextClippedEx(draw_list, unsaved_marker_pos, bb.Max - frame_padding, TAB_UNSAVED_MARKER, NULL, NULL); + } + ImRect text_ellipsis_clip_bb = text_pixel_clip_bb; + + // Close Button + // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap() + // 'hovered' will be true when hovering the Tab but NOT when hovering the close button + // 'g.HoveredId==id' will be true when hovering the Tab including when hovering the close button + // 'g.ActiveId==close_button_id' will be true when we are holding on the close button, in which case both hovered booleans are false + bool close_button_pressed = false; + bool close_button_visible = false; + if (close_button_id != 0) + if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id) + close_button_visible = true; + if (close_button_visible) + { + ImGuiItemHoveredDataBackup last_item_backup; + const float close_button_sz = g.FontSize; + PushStyleVar(ImGuiStyleVar_FramePadding, frame_padding); + if (CloseButton(close_button_id, ImVec2(bb.Max.x - frame_padding.x * 2.0f - close_button_sz, bb.Min.y))) + close_button_pressed = true; + PopStyleVar(); + last_item_backup.Restore(); + + // Close with middle mouse button + if (!(flags & ImGuiTabItemFlags_NoCloseWithMiddleMouseButton) && IsMouseClicked(2)) + close_button_pressed = true; + + text_pixel_clip_bb.Max.x -= close_button_sz; + } + + float ellipsis_max_x = close_button_visible ? text_pixel_clip_bb.Max.x : bb.Max.x - 1.0f; + RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, text_pixel_clip_bb.Max.x, ellipsis_max_x, label, NULL, &label_size); + + return close_button_pressed; +} + + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc. +// In the current version, Columns are very weak. Needs to be replaced with a more full-featured system. +//------------------------------------------------------------------------- +// - GetColumnIndex() +// - GetColumnCount() +// - GetColumnOffset() +// - GetColumnWidth() +// - SetColumnOffset() +// - SetColumnWidth() +// - PushColumnClipRect() [Internal] +// - PushColumnsBackground() [Internal] +// - PopColumnsBackground() [Internal] +// - FindOrCreateColumns() [Internal] +// - GetColumnsID() [Internal] +// - BeginColumns() +// - NextColumn() +// - EndColumns() +// - Columns() +//------------------------------------------------------------------------- + +int ImGui::GetColumnIndex() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0; +} + +int ImGui::GetColumnsCount() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1; +} + +float ImGui::GetColumnOffsetFromNorm(const ImGuiColumns* columns, float offset_norm) +{ + return offset_norm * (columns->OffMaxX - columns->OffMinX); +} + +float ImGui::GetColumnNormFromOffset(const ImGuiColumns* columns, float offset) +{ + return offset / (columns->OffMaxX - columns->OffMinX); +} + +static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; + +static float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index) +{ + // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing + // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning. + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(column_index > 0); // We are not supposed to drag column 0. + IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index)); + + float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; + x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); + if ((columns->Flags & ImGuiColumnsFlags_NoPreserveWidths)) + x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); + + return x; +} + +float ImGui::GetColumnOffset(int column_index) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiColumns* columns = window->DC.CurrentColumns; + if (columns == NULL) + return 0.0f; + + if (column_index < 0) + column_index = columns->Current; + IM_ASSERT(column_index < columns->Columns.Size); + + const float t = columns->Columns[column_index].OffsetNorm; + const float x_offset = ImLerp(columns->OffMinX, columns->OffMaxX, t); + return x_offset; +} + +static float GetColumnWidthEx(ImGuiColumns* columns, int column_index, bool before_resize = false) +{ + if (column_index < 0) + column_index = columns->Current; + + float offset_norm; + if (before_resize) + offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize; + else + offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm; + return ImGui::GetColumnOffsetFromNorm(columns, offset_norm); +} + +float ImGui::GetColumnWidth(int column_index) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiColumns* columns = window->DC.CurrentColumns; + if (columns == NULL) + return GetContentRegionAvail().x; + + if (column_index < 0) + column_index = columns->Current; + return GetColumnOffsetFromNorm(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm); +} + +void ImGui::SetColumnOffset(int column_index, float offset) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + if (column_index < 0) + column_index = columns->Current; + IM_ASSERT(column_index < columns->Columns.Size); + + const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count-1); + const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; + + if (!(columns->Flags & ImGuiColumnsFlags_NoForceWithinWindow)) + offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index)); + columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX); + + if (preserve_width) + SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width)); +} + +void ImGui::SetColumnWidth(int column_index, float width) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + if (column_index < 0) + column_index = columns->Current; + SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width); +} + +void ImGui::PushColumnClipRect(int column_index) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiColumns* columns = window->DC.CurrentColumns; + if (column_index < 0) + column_index = columns->Current; + + ImGuiColumnData* column = &columns->Columns[column_index]; + PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false); +} + +// Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns) +void ImGui::PushColumnsBackground() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiColumns* columns = window->DC.CurrentColumns; + if (columns->Count == 1) + return; + window->DrawList->ChannelsSetCurrent(0); + int cmd_size = window->DrawList->CmdBuffer.Size; + PushClipRect(columns->HostClipRect.Min, columns->HostClipRect.Max, false); + IM_UNUSED(cmd_size); + IM_ASSERT(cmd_size == window->DrawList->CmdBuffer.Size); // Being in channel 0 this should not have created an ImDrawCmd +} + +void ImGui::PopColumnsBackground() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiColumns* columns = window->DC.CurrentColumns; + if (columns->Count == 1) + return; + window->DrawList->ChannelsSetCurrent(columns->Current + 1); + PopClipRect(); +} + +ImGuiColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) +{ + // We have few columns per window so for now we don't need bother much with turning this into a faster lookup. + for (int n = 0; n < window->ColumnsStorage.Size; n++) + if (window->ColumnsStorage[n].ID == id) + return &window->ColumnsStorage[n]; + + window->ColumnsStorage.push_back(ImGuiColumns()); + ImGuiColumns* columns = &window->ColumnsStorage.back(); + columns->ID = id; + return columns; +} + +ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count) +{ + ImGuiWindow* window = GetCurrentWindow(); + + // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget. + // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer. + PushID(0x11223347 + (str_id ? 0 : columns_count)); + ImGuiID id = window->GetID(str_id ? str_id : "columns"); + PopID(); + + return id; +} + +void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + + IM_ASSERT(columns_count >= 1); + IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported + + // Acquire storage for the columns set + ImGuiID id = GetColumnsID(str_id, columns_count); + ImGuiColumns* columns = FindOrCreateColumns(window, id); + IM_ASSERT(columns->ID == id); + columns->Current = 0; + columns->Count = columns_count; + columns->Flags = flags; + window->DC.CurrentColumns = columns; + + columns->HostCursorPosY = window->DC.CursorPos.y; + columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; + columns->HostClipRect = window->ClipRect; + columns->HostWorkRect = window->WorkRect; + + // Set state for first column + // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect + const float column_padding = g.Style.ItemSpacing.x; + const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize)); + const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f); + const float max_2 = window->WorkRect.Max.x + half_clip_extend_x; + columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f); + columns->OffMaxX = ImMax(ImMin(max_1, max_2) - window->Pos.x, columns->OffMinX + 1.0f); + columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y; + + // Clear data if columns count changed + if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1) + columns->Columns.resize(0); + + // Initialize default widths + columns->IsFirstFrame = (columns->Columns.Size == 0); + if (columns->Columns.Size == 0) + { + columns->Columns.reserve(columns_count + 1); + for (int n = 0; n < columns_count + 1; n++) + { + ImGuiColumnData column; + column.OffsetNorm = n / (float)columns_count; + columns->Columns.push_back(column); + } + } + + for (int n = 0; n < columns_count; n++) + { + // Compute clipping rectangle + ImGuiColumnData* column = &columns->Columns[n]; + float clip_x1 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n)); + float clip_x2 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n + 1) - 1.0f); + column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); + column->ClipRect.ClipWith(window->ClipRect); + } + + if (columns->Count > 1) + { + window->DrawList->ChannelsSplit(1 + columns->Count); + window->DrawList->ChannelsSetCurrent(1); + PushColumnClipRect(0); + } + + // We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user. + float offset_0 = GetColumnOffset(columns->Current); + float offset_1 = GetColumnOffset(columns->Current + 1); + float width = offset_1 - offset_0; + PushItemWidth(width * 0.65f); + window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); + window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; +} + +void ImGui::NextColumn() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems || window->DC.CurrentColumns == NULL) + return; + + ImGuiContext& g = *GImGui; + ImGuiColumns* columns = window->DC.CurrentColumns; + + if (columns->Count == 1) + { + window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + IM_ASSERT(columns->Current == 0); + return; + } + PopItemWidth(); + PopClipRect(); + + const float column_padding = g.Style.ItemSpacing.x; + columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); + if (++columns->Current < columns->Count) + { + // Columns 1+ ignore IndentX (by canceling it out) + // FIXME-COLUMNS: Unnecessary, could be locked? + window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding; + window->DrawList->ChannelsSetCurrent(columns->Current + 1); + } + else + { + // New row/line + // Column 0 honor IndentX + window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); + window->DrawList->ChannelsSetCurrent(1); + columns->Current = 0; + columns->LineMinY = columns->LineMaxY; + } + window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.y = columns->LineMinY; + window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + window->DC.CurrLineTextBaseOffset = 0.0f; + + PushColumnClipRect(columns->Current); // FIXME-COLUMNS: Could it be an overwrite? + + // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup. + float offset_0 = GetColumnOffset(columns->Current); + float offset_1 = GetColumnOffset(columns->Current + 1); + float width = offset_1 - offset_0; + PushItemWidth(width * 0.65f); + window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; +} + +void ImGui::EndColumns() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + ImGuiColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + PopItemWidth(); + if (columns->Count > 1) + { + PopClipRect(); + window->DrawList->ChannelsMerge(); + } + + const ImGuiColumnsFlags flags = columns->Flags; + columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); + window->DC.CursorPos.y = columns->LineMaxY; + if (!(flags & ImGuiColumnsFlags_GrowParentContentsSize)) + window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent + + // Draw columns borders and handle resize + // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy + bool is_being_resized = false; + if (!(flags & ImGuiColumnsFlags_NoBorder) && !window->SkipItems) + { + // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers. + const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y); + const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y); + int dragging_column = -1; + for (int n = 1; n < columns->Count; n++) + { + ImGuiColumnData* column = &columns->Columns[n]; + float x = window->Pos.x + GetColumnOffset(n); + const ImGuiID column_id = columns->ID + ImGuiID(n); + const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; + const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); + KeepAliveID(column_id); + if (IsClippedEx(column_hit_rect, column_id, false)) + continue; + + bool hovered = false, held = false; + if (!(flags & ImGuiColumnsFlags_NoResize)) + { + ButtonBehavior(column_hit_rect, column_id, &hovered, &held); + if (hovered || held) + g.MouseCursor = ImGuiMouseCursor_ResizeEW; + if (held && !(column->Flags & ImGuiColumnsFlags_NoResize)) + dragging_column = n; + } + + // Draw column + const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); + const float xi = (float)(int)x; + window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); + } + + // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame. + if (dragging_column != -1) + { + if (!columns->IsBeingResized) + for (int n = 0; n < columns->Count + 1; n++) + columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm; + columns->IsBeingResized = is_being_resized = true; + float x = GetDraggedColumnOffset(columns, dragging_column); + SetColumnOffset(dragging_column, x); + } + } + columns->IsBeingResized = is_being_resized; + + window->WorkRect = columns->HostWorkRect; + window->DC.CurrentColumns = NULL; + window->DC.ColumnsOffset.x = 0.0f; + window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); +} + +// [2018-03: This is currently the only public API, while we are working on making BeginColumns/EndColumns user-facing] +void ImGui::Columns(int columns_count, const char* id, bool border) +{ + ImGuiWindow* window = GetCurrentWindow(); + IM_ASSERT(columns_count >= 1); + + ImGuiColumnsFlags flags = (border ? 0 : ImGuiColumnsFlags_NoBorder); + //flags |= ImGuiColumnsFlags_NoPreserveWidths; // NB: Legacy behavior + ImGuiColumns* columns = window->DC.CurrentColumns; + if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) + return; + + if (columns != NULL) + EndColumns(); + + if (columns_count != 1) + BeginColumns(id, columns_count, flags); +} + +//------------------------------------------------------------------------- diff --git a/imgui/imstb_rectpack.h b/imgui/imstb_rectpack.h new file mode 100644 index 00000000..ff2a85df --- /dev/null +++ b/imgui/imstb_rectpack.h @@ -0,0 +1,639 @@ +// [DEAR IMGUI] +// This is a slightly modified version of stb_rect_pack.h 1.00. +// Those changes would need to be pushed into nothings/stb: +// - Added STBRP__CDECL +// Grep for [DEAR IMGUI] to find the changes. + +// stb_rect_pack.h - v1.00 - public domain - rectangle packing +// Sean Barrett 2014 +// +// Useful for e.g. packing rectangular textures into an atlas. +// Does not do rotation. +// +// Not necessarily the awesomest packing method, but better than +// the totally naive one in stb_truetype (which is primarily what +// this is meant to replace). +// +// Has only had a few tests run, may have issues. +// +// More docs to come. +// +// No memory allocations; uses qsort() and assert() from stdlib. +// Can override those by defining STBRP_SORT and STBRP_ASSERT. +// +// This library currently uses the Skyline Bottom-Left algorithm. +// +// Please note: better rectangle packers are welcome! Please +// implement them to the same API, but with a different init +// function. +// +// Credits +// +// Library +// Sean Barrett +// Minor features +// Martins Mozeiko +// github:IntellectualKitty +// +// Bugfixes / warning fixes +// Jeremy Jaussaud +// Fabian Giesen +// +// Version history: +// +// 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles +// 0.99 (2019-02-07) warning fixes +// 0.11 (2017-03-03) return packing success/fail result +// 0.10 (2016-10-25) remove cast-away-const to avoid warnings +// 0.09 (2016-08-27) fix compiler warnings +// 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) +// 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) +// 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort +// 0.05: added STBRP_ASSERT to allow replacing assert +// 0.04: fixed minor bug in STBRP_LARGE_RECTS support +// 0.01: initial release +// +// LICENSE +// +// See end of file for license information. + +////////////////////////////////////////////////////////////////////////////// +// +// INCLUDE SECTION +// + +#ifndef STB_INCLUDE_STB_RECT_PACK_H +#define STB_INCLUDE_STB_RECT_PACK_H + +#define STB_RECT_PACK_VERSION 1 + +#ifdef STBRP_STATIC +#define STBRP_DEF static +#else +#define STBRP_DEF extern +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct stbrp_context stbrp_context; +typedef struct stbrp_node stbrp_node; +typedef struct stbrp_rect stbrp_rect; + +#ifdef STBRP_LARGE_RECTS +typedef int stbrp_coord; +#else +typedef unsigned short stbrp_coord; +#endif + +STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); +// Assign packed locations to rectangles. The rectangles are of type +// 'stbrp_rect' defined below, stored in the array 'rects', and there +// are 'num_rects' many of them. +// +// Rectangles which are successfully packed have the 'was_packed' flag +// set to a non-zero value and 'x' and 'y' store the minimum location +// on each axis (i.e. bottom-left in cartesian coordinates, top-left +// if you imagine y increasing downwards). Rectangles which do not fit +// have the 'was_packed' flag set to 0. +// +// You should not try to access the 'rects' array from another thread +// while this function is running, as the function temporarily reorders +// the array while it executes. +// +// To pack into another rectangle, you need to call stbrp_init_target +// again. To continue packing into the same rectangle, you can call +// this function again. Calling this multiple times with multiple rect +// arrays will probably produce worse packing results than calling it +// a single time with the full rectangle array, but the option is +// available. +// +// The function returns 1 if all of the rectangles were successfully +// packed and 0 otherwise. + +struct stbrp_rect +{ + // reserved for your use: + int id; + + // input: + stbrp_coord w, h; + + // output: + stbrp_coord x, y; + int was_packed; // non-zero if valid packing + +}; // 16 bytes, nominally + + +STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes); +// Initialize a rectangle packer to: +// pack a rectangle that is 'width' by 'height' in dimensions +// using temporary storage provided by the array 'nodes', which is 'num_nodes' long +// +// You must call this function every time you start packing into a new target. +// +// There is no "shutdown" function. The 'nodes' memory must stay valid for +// the following stbrp_pack_rects() call (or calls), but can be freed after +// the call (or calls) finish. +// +// Note: to guarantee best results, either: +// 1. make sure 'num_nodes' >= 'width' +// or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' +// +// If you don't do either of the above things, widths will be quantized to multiples +// of small integers to guarantee the algorithm doesn't run out of temporary storage. +// +// If you do #2, then the non-quantized algorithm will be used, but the algorithm +// may run out of temporary storage and be unable to pack some rectangles. + +STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem); +// Optionally call this function after init but before doing any packing to +// change the handling of the out-of-temp-memory scenario, described above. +// If you call init again, this will be reset to the default (false). + + +STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic); +// Optionally select which packing heuristic the library should use. Different +// heuristics will produce better/worse results for different data sets. +// If you call init again, this will be reset to the default. + +enum +{ + STBRP_HEURISTIC_Skyline_default=0, + STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, + STBRP_HEURISTIC_Skyline_BF_sortHeight +}; + + +////////////////////////////////////////////////////////////////////////////// +// +// the details of the following structures don't matter to you, but they must +// be visible so you can handle the memory allocations for them + +struct stbrp_node +{ + stbrp_coord x,y; + stbrp_node *next; +}; + +struct stbrp_context +{ + int width; + int height; + int align; + int init_mode; + int heuristic; + int num_nodes; + stbrp_node *active_head; + stbrp_node *free_head; + stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' +}; + +#ifdef __cplusplus +} +#endif + +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// IMPLEMENTATION SECTION +// + +#ifdef STB_RECT_PACK_IMPLEMENTATION +#ifndef STBRP_SORT +#include +#define STBRP_SORT qsort +#endif + +#ifndef STBRP_ASSERT +#include +#define STBRP_ASSERT assert +#endif + +// [DEAR IMGUI] Added STBRP__CDECL +#ifdef _MSC_VER +#define STBRP__NOTUSED(v) (void)(v) +#define STBRP__CDECL __cdecl +#else +#define STBRP__NOTUSED(v) (void)sizeof(v) +#define STBRP__CDECL +#endif + +enum +{ + STBRP__INIT_skyline = 1 +}; + +STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic) +{ + switch (context->init_mode) { + case STBRP__INIT_skyline: + STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); + context->heuristic = heuristic; + break; + default: + STBRP_ASSERT(0); + } +} + +STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem) +{ + if (allow_out_of_mem) + // if it's ok to run out of memory, then don't bother aligning them; + // this gives better packing, but may fail due to OOM (even though + // the rectangles easily fit). @TODO a smarter approach would be to only + // quantize once we've hit OOM, then we could get rid of this parameter. + context->align = 1; + else { + // if it's not ok to run out of memory, then quantize the widths + // so that num_nodes is always enough nodes. + // + // I.e. num_nodes * align >= width + // align >= width / num_nodes + // align = ceil(width/num_nodes) + + context->align = (context->width + context->num_nodes-1) / context->num_nodes; + } +} + +STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) +{ + int i; +#ifndef STBRP_LARGE_RECTS + STBRP_ASSERT(width <= 0xffff && height <= 0xffff); +#endif + + for (i=0; i < num_nodes-1; ++i) + nodes[i].next = &nodes[i+1]; + nodes[i].next = NULL; + context->init_mode = STBRP__INIT_skyline; + context->heuristic = STBRP_HEURISTIC_Skyline_default; + context->free_head = &nodes[0]; + context->active_head = &context->extra[0]; + context->width = width; + context->height = height; + context->num_nodes = num_nodes; + stbrp_setup_allow_out_of_mem(context, 0); + + // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) + context->extra[0].x = 0; + context->extra[0].y = 0; + context->extra[0].next = &context->extra[1]; + context->extra[1].x = (stbrp_coord) width; +#ifdef STBRP_LARGE_RECTS + context->extra[1].y = (1<<30); +#else + context->extra[1].y = 65535; +#endif + context->extra[1].next = NULL; +} + +// find minimum y position if it starts at x1 +static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste) +{ + stbrp_node *node = first; + int x1 = x0 + width; + int min_y, visited_width, waste_area; + + STBRP__NOTUSED(c); + + STBRP_ASSERT(first->x <= x0); + + #if 0 + // skip in case we're past the node + while (node->next->x <= x0) + ++node; + #else + STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency + #endif + + STBRP_ASSERT(node->x <= x0); + + min_y = 0; + waste_area = 0; + visited_width = 0; + while (node->x < x1) { + if (node->y > min_y) { + // raise min_y higher. + // we've accounted for all waste up to min_y, + // but we'll now add more waste for everything we've visted + waste_area += visited_width * (node->y - min_y); + min_y = node->y; + // the first time through, visited_width might be reduced + if (node->x < x0) + visited_width += node->next->x - x0; + else + visited_width += node->next->x - node->x; + } else { + // add waste area + int under_width = node->next->x - node->x; + if (under_width + visited_width > width) + under_width = width - visited_width; + waste_area += under_width * (min_y - node->y); + visited_width += under_width; + } + node = node->next; + } + + *pwaste = waste_area; + return min_y; +} + +typedef struct +{ + int x,y; + stbrp_node **prev_link; +} stbrp__findresult; + +static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) +{ + int best_waste = (1<<30), best_x, best_y = (1 << 30); + stbrp__findresult fr; + stbrp_node **prev, *node, *tail, **best = NULL; + + // align to multiple of c->align + width = (width + c->align - 1); + width -= width % c->align; + STBRP_ASSERT(width % c->align == 0); + + // if it can't possibly fit, bail immediately + if (width > c->width || height > c->height) { + fr.prev_link = NULL; + fr.x = fr.y = 0; + return fr; + } + + node = c->active_head; + prev = &c->active_head; + while (node->x + width <= c->width) { + int y,waste; + y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); + if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL + // bottom left + if (y < best_y) { + best_y = y; + best = prev; + } + } else { + // best-fit + if (y + height <= c->height) { + // can only use it if it first vertically + if (y < best_y || (y == best_y && waste < best_waste)) { + best_y = y; + best_waste = waste; + best = prev; + } + } + } + prev = &node->next; + node = node->next; + } + + best_x = (best == NULL) ? 0 : (*best)->x; + + // if doing best-fit (BF), we also have to try aligning right edge to each node position + // + // e.g, if fitting + // + // ____________________ + // |____________________| + // + // into + // + // | | + // | ____________| + // |____________| + // + // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned + // + // This makes BF take about 2x the time + + if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { + tail = c->active_head; + node = c->active_head; + prev = &c->active_head; + // find first node that's admissible + while (tail->x < width) + tail = tail->next; + while (tail) { + int xpos = tail->x - width; + int y,waste; + STBRP_ASSERT(xpos >= 0); + // find the left position that matches this + while (node->next->x <= xpos) { + prev = &node->next; + node = node->next; + } + STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); + y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); + if (y + height <= c->height) { + if (y <= best_y) { + if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { + best_x = xpos; + STBRP_ASSERT(y <= best_y); + best_y = y; + best_waste = waste; + best = prev; + } + } + } + tail = tail->next; + } + } + + fr.prev_link = best; + fr.x = best_x; + fr.y = best_y; + return fr; +} + +static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) +{ + // find best position according to heuristic + stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); + stbrp_node *node, *cur; + + // bail if: + // 1. it failed + // 2. the best node doesn't fit (we don't always check this) + // 3. we're out of memory + if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { + res.prev_link = NULL; + return res; + } + + // on success, create new node + node = context->free_head; + node->x = (stbrp_coord) res.x; + node->y = (stbrp_coord) (res.y + height); + + context->free_head = node->next; + + // insert the new node into the right starting point, and + // let 'cur' point to the remaining nodes needing to be + // stiched back in + + cur = *res.prev_link; + if (cur->x < res.x) { + // preserve the existing one, so start testing with the next one + stbrp_node *next = cur->next; + cur->next = node; + cur = next; + } else { + *res.prev_link = node; + } + + // from here, traverse cur and free the nodes, until we get to one + // that shouldn't be freed + while (cur->next && cur->next->x <= res.x + width) { + stbrp_node *next = cur->next; + // move the current node to the free list + cur->next = context->free_head; + context->free_head = cur; + cur = next; + } + + // stitch the list back in + node->next = cur; + + if (cur->x < res.x + width) + cur->x = (stbrp_coord) (res.x + width); + +#ifdef _DEBUG + cur = context->active_head; + while (cur->x < context->width) { + STBRP_ASSERT(cur->x < cur->next->x); + cur = cur->next; + } + STBRP_ASSERT(cur->next == NULL); + + { + int count=0; + cur = context->active_head; + while (cur) { + cur = cur->next; + ++count; + } + cur = context->free_head; + while (cur) { + cur = cur->next; + ++count; + } + STBRP_ASSERT(count == context->num_nodes+2); + } +#endif + + return res; +} + +// [DEAR IMGUI] Added STBRP__CDECL +static int STBRP__CDECL rect_height_compare(const void *a, const void *b) +{ + const stbrp_rect *p = (const stbrp_rect *) a; + const stbrp_rect *q = (const stbrp_rect *) b; + if (p->h > q->h) + return -1; + if (p->h < q->h) + return 1; + return (p->w > q->w) ? -1 : (p->w < q->w); +} + +// [DEAR IMGUI] Added STBRP__CDECL +static int STBRP__CDECL rect_original_order(const void *a, const void *b) +{ + const stbrp_rect *p = (const stbrp_rect *) a; + const stbrp_rect *q = (const stbrp_rect *) b; + return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); +} + +#ifdef STBRP_LARGE_RECTS +#define STBRP__MAXVAL 0xffffffff +#else +#define STBRP__MAXVAL 0xffff +#endif + +STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) +{ + int i, all_rects_packed = 1; + + // we use the 'was_packed' field internally to allow sorting/unsorting + for (i=0; i < num_rects; ++i) { + rects[i].was_packed = i; + } + + // sort according to heuristic + STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); + + for (i=0; i < num_rects; ++i) { + if (rects[i].w == 0 || rects[i].h == 0) { + rects[i].x = rects[i].y = 0; // empty rect needs no space + } else { + stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); + if (fr.prev_link) { + rects[i].x = (stbrp_coord) fr.x; + rects[i].y = (stbrp_coord) fr.y; + } else { + rects[i].x = rects[i].y = STBRP__MAXVAL; + } + } + } + + // unsort + STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); + + // set was_packed flags and all_rects_packed status + for (i=0; i < num_rects; ++i) { + rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); + if (!rects[i].was_packed) + all_rects_packed = 0; + } + + // return the all_rects_packed status + return all_rects_packed; +} +#endif + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/imgui/imstb_textedit.h b/imgui/imstb_textedit.h new file mode 100644 index 00000000..d7fcbd62 --- /dev/null +++ b/imgui/imstb_textedit.h @@ -0,0 +1,1417 @@ +// [DEAR IMGUI] +// This is a slightly modified version of stb_textedit.h 1.13. +// Those changes would need to be pushed into nothings/stb: +// - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) +// Grep for [DEAR IMGUI] to find the changes. + +// stb_textedit.h - v1.13 - public domain - Sean Barrett +// Development of this library was sponsored by RAD Game Tools +// +// This C header file implements the guts of a multi-line text-editing +// widget; you implement display, word-wrapping, and low-level string +// insertion/deletion, and stb_textedit will map user inputs into +// insertions & deletions, plus updates to the cursor position, +// selection state, and undo state. +// +// It is intended for use in games and other systems that need to build +// their own custom widgets and which do not have heavy text-editing +// requirements (this library is not recommended for use for editing large +// texts, as its performance does not scale and it has limited undo). +// +// Non-trivial behaviors are modelled after Windows text controls. +// +// +// LICENSE +// +// See end of file for license information. +// +// +// DEPENDENCIES +// +// Uses the C runtime function 'memmove', which you can override +// by defining STB_TEXTEDIT_memmove before the implementation. +// Uses no other functions. Performs no runtime allocations. +// +// +// VERSION HISTORY +// +// 1.13 (2019-02-07) fix bug in undo size management +// 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash +// 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield +// 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual +// 1.9 (2016-08-27) customizable move-by-word +// 1.8 (2016-04-02) better keyboard handling when mouse button is down +// 1.7 (2015-09-13) change y range handling in case baseline is non-0 +// 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove +// 1.5 (2014-09-10) add support for secondary keys for OS X +// 1.4 (2014-08-17) fix signed/unsigned warnings +// 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary +// 1.2 (2014-05-27) fix some RAD types that had crept into the new code +// 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE ) +// 1.0 (2012-07-26) improve documentation, initial public release +// 0.3 (2012-02-24) bugfixes, single-line mode; insert mode +// 0.2 (2011-11-28) fixes to undo/redo +// 0.1 (2010-07-08) initial version +// +// ADDITIONAL CONTRIBUTORS +// +// Ulf Winklemann: move-by-word in 1.1 +// Fabian Giesen: secondary key inputs in 1.5 +// Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6 +// +// Bugfixes: +// Scott Graham +// Daniel Keller +// Omar Cornut +// Dan Thompson +// +// USAGE +// +// This file behaves differently depending on what symbols you define +// before including it. +// +// +// Header-file mode: +// +// If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this, +// it will operate in "header file" mode. In this mode, it declares a +// single public symbol, STB_TexteditState, which encapsulates the current +// state of a text widget (except for the string, which you will store +// separately). +// +// To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a +// primitive type that defines a single character (e.g. char, wchar_t, etc). +// +// To save space or increase undo-ability, you can optionally define the +// following things that are used by the undo system: +// +// STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position +// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow +// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer +// +// If you don't define these, they are set to permissive types and +// moderate sizes. The undo system does no memory allocations, so +// it grows STB_TexteditState by the worst-case storage which is (in bytes): +// +// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT +// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT +// +// +// Implementation mode: +// +// If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it +// will compile the implementation of the text edit widget, depending +// on a large number of symbols which must be defined before the include. +// +// The implementation is defined only as static functions. You will then +// need to provide your own APIs in the same file which will access the +// static functions. +// +// The basic concept is that you provide a "string" object which +// behaves like an array of characters. stb_textedit uses indices to +// refer to positions in the string, implicitly representing positions +// in the displayed textedit. This is true for both plain text and +// rich text; even with rich text stb_truetype interacts with your +// code as if there was an array of all the displayed characters. +// +// Symbols that must be the same in header-file and implementation mode: +// +// STB_TEXTEDIT_CHARTYPE the character type +// STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position +// STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow +// STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer +// +// Symbols you must define for implementation mode: +// +// STB_TEXTEDIT_STRING the type of object representing a string being edited, +// typically this is a wrapper object with other data you need +// +// STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1)) +// STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters +// starting from character #n (see discussion below) +// STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character +// to the xpos of the i+1'th char for a line of characters +// starting at character #n (i.e. accounts for kerning +// with previous char) +// STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character +// (return type is int, -1 means not valid to insert) +// STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based +// STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize +// as manually wordwrapping for end-of-line positioning +// +// STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i +// STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*) +// +// STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key +// +// STB_TEXTEDIT_K_LEFT keyboard input to move cursor left +// STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right +// STB_TEXTEDIT_K_UP keyboard input to move cursor up +// STB_TEXTEDIT_K_DOWN keyboard input to move cursor down +// STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME +// STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END +// STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME +// STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END +// STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor +// STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor +// STB_TEXTEDIT_K_UNDO keyboard input to perform undo +// STB_TEXTEDIT_K_REDO keyboard input to perform redo +// +// Optional: +// STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode +// STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'), +// required for default WORDLEFT/WORDRIGHT handlers +// STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to +// STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to +// STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT +// STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT +// STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line +// STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line +// STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text +// STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text +// +// Todo: +// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page +// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page +// +// Keyboard input must be encoded as a single integer value; e.g. a character code +// and some bitflags that represent shift states. to simplify the interface, SHIFT must +// be a bitflag, so we can test the shifted state of cursor movements to allow selection, +// i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow. +// +// You can encode other things, such as CONTROL or ALT, in additional bits, and +// then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example, +// my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN +// bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit, +// and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the +// API below. The control keys will only match WM_KEYDOWN events because of the +// keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN +// bit so it only decodes WM_CHAR events. +// +// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed +// row of characters assuming they start on the i'th character--the width and +// the height and the number of characters consumed. This allows this library +// to traverse the entire layout incrementally. You need to compute word-wrapping +// here. +// +// Each textfield keeps its own insert mode state, which is not how normal +// applications work. To keep an app-wide insert mode, update/copy the +// "insert_mode" field of STB_TexteditState before/after calling API functions. +// +// API +// +// void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) +// +// void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +// void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +// int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +// int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) +// void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key) +// +// Each of these functions potentially updates the string and updates the +// state. +// +// initialize_state: +// set the textedit state to a known good default state when initially +// constructing the textedit. +// +// click: +// call this with the mouse x,y on a mouse down; it will update the cursor +// and reset the selection start/end to the cursor point. the x,y must +// be relative to the text widget, with (0,0) being the top left. +// +// drag: +// call this with the mouse x,y on a mouse drag/up; it will update the +// cursor and the selection end point +// +// cut: +// call this to delete the current selection; returns true if there was +// one. you should FIRST copy the current selection to the system paste buffer. +// (To copy, just copy the current selection out of the string yourself.) +// +// paste: +// call this to paste text at the current cursor point or over the current +// selection if there is one. +// +// key: +// call this for keyboard inputs sent to the textfield. you can use it +// for "key down" events or for "translated" key events. if you need to +// do both (as in Win32), or distinguish Unicode characters from control +// inputs, set a high bit to distinguish the two; then you can define the +// various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit +// set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is +// clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to +// anything other type you wante before including. +// +// +// When rendering, you can read the cursor position and selection state from +// the STB_TexteditState. +// +// +// Notes: +// +// This is designed to be usable in IMGUI, so it allows for the possibility of +// running in an IMGUI that has NOT cached the multi-line layout. For this +// reason, it provides an interface that is compatible with computing the +// layout incrementally--we try to make sure we make as few passes through +// as possible. (For example, to locate the mouse pointer in the text, we +// could define functions that return the X and Y positions of characters +// and binary search Y and then X, but if we're doing dynamic layout this +// will run the layout algorithm many times, so instead we manually search +// forward in one pass. Similar logic applies to e.g. up-arrow and +// down-arrow movement.) +// +// If it's run in a widget that *has* cached the layout, then this is less +// efficient, but it's not horrible on modern computers. But you wouldn't +// want to edit million-line files with it. + + +//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// +//// +//// Header-file mode +//// +//// + +#ifndef INCLUDE_STB_TEXTEDIT_H +#define INCLUDE_STB_TEXTEDIT_H + +//////////////////////////////////////////////////////////////////////// +// +// STB_TexteditState +// +// Definition of STB_TexteditState which you should store +// per-textfield; it includes cursor position, selection state, +// and undo state. +// + +#ifndef STB_TEXTEDIT_UNDOSTATECOUNT +#define STB_TEXTEDIT_UNDOSTATECOUNT 99 +#endif +#ifndef STB_TEXTEDIT_UNDOCHARCOUNT +#define STB_TEXTEDIT_UNDOCHARCOUNT 999 +#endif +#ifndef STB_TEXTEDIT_CHARTYPE +#define STB_TEXTEDIT_CHARTYPE int +#endif +#ifndef STB_TEXTEDIT_POSITIONTYPE +#define STB_TEXTEDIT_POSITIONTYPE int +#endif + +typedef struct +{ + // private data + STB_TEXTEDIT_POSITIONTYPE where; + STB_TEXTEDIT_POSITIONTYPE insert_length; + STB_TEXTEDIT_POSITIONTYPE delete_length; + int char_storage; +} StbUndoRecord; + +typedef struct +{ + // private data + StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]; + STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]; + short undo_point, redo_point; + int undo_char_point, redo_char_point; +} StbUndoState; + +typedef struct +{ + ///////////////////// + // + // public data + // + + int cursor; + // position of the text cursor within the string + + int select_start; // selection start point + int select_end; + // selection start and end point in characters; if equal, no selection. + // note that start may be less than or greater than end (e.g. when + // dragging the mouse, start is where the initial click was, and you + // can drag in either direction) + + unsigned char insert_mode; + // each textfield keeps its own insert mode state. to keep an app-wide + // insert mode, copy this value in/out of the app state + + ///////////////////// + // + // private data + // + unsigned char cursor_at_end_of_line; // not implemented yet + unsigned char initialized; + unsigned char has_preferred_x; + unsigned char single_line; + unsigned char padding1, padding2, padding3; + float preferred_x; // this determines where the cursor up/down tries to seek to along x + StbUndoState undostate; +} STB_TexteditState; + + +//////////////////////////////////////////////////////////////////////// +// +// StbTexteditRow +// +// Result of layout query, used by stb_textedit to determine where +// the text in each row is. + +// result of layout query +typedef struct +{ + float x0,x1; // starting x location, end x location (allows for align=right, etc) + float baseline_y_delta; // position of baseline relative to previous row's baseline + float ymin,ymax; // height of row above and below baseline + int num_chars; +} StbTexteditRow; +#endif //INCLUDE_STB_TEXTEDIT_H + + +//////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////// +//// +//// Implementation mode +//// +//// + + +// implementation isn't include-guarded, since it might have indirectly +// included just the "header" portion +#ifdef STB_TEXTEDIT_IMPLEMENTATION + +#ifndef STB_TEXTEDIT_memmove +#include +#define STB_TEXTEDIT_memmove memmove +#endif + + +///////////////////////////////////////////////////////////////////////////// +// +// Mouse input handling +// + +// traverse the layout to locate the nearest character to a display position +static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y) +{ + StbTexteditRow r; + int n = STB_TEXTEDIT_STRINGLEN(str); + float base_y = 0, prev_x; + int i=0, k; + + r.x0 = r.x1 = 0; + r.ymin = r.ymax = 0; + r.num_chars = 0; + + // search rows to find one that straddles 'y' + while (i < n) { + STB_TEXTEDIT_LAYOUTROW(&r, str, i); + if (r.num_chars <= 0) + return n; + + if (i==0 && y < base_y + r.ymin) + return 0; + + if (y < base_y + r.ymax) + break; + + i += r.num_chars; + base_y += r.baseline_y_delta; + } + + // below all text, return 'after' last character + if (i >= n) + return n; + + // check if it's before the beginning of the line + if (x < r.x0) + return i; + + // check if it's before the end of the line + if (x < r.x1) { + // search characters in row for one that straddles 'x' + prev_x = r.x0; + for (k=0; k < r.num_chars; ++k) { + float w = STB_TEXTEDIT_GETWIDTH(str, i, k); + if (x < prev_x+w) { + if (x < prev_x+w/2) + return k+i; + else + return k+i+1; + } + prev_x += w; + } + // shouldn't happen, but if it does, fall through to end-of-line case + } + + // if the last character is a newline, return that. otherwise return 'after' the last character + if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE) + return i+r.num_chars-1; + else + return i+r.num_chars; +} + +// API click: on mouse down, move the cursor to the clicked location, and reset the selection +static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +{ + // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse + // goes off the top or bottom of the text + if( state->single_line ) + { + StbTexteditRow r; + STB_TEXTEDIT_LAYOUTROW(&r, str, 0); + y = r.ymin; + } + + state->cursor = stb_text_locate_coord(str, x, y); + state->select_start = state->cursor; + state->select_end = state->cursor; + state->has_preferred_x = 0; +} + +// API drag: on mouse drag, move the cursor and selection endpoint to the clicked location +static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +{ + int p = 0; + + // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse + // goes off the top or bottom of the text + if( state->single_line ) + { + StbTexteditRow r; + STB_TEXTEDIT_LAYOUTROW(&r, str, 0); + y = r.ymin; + } + + if (state->select_start == state->select_end) + state->select_start = state->cursor; + + p = stb_text_locate_coord(str, x, y); + state->cursor = state->select_end = p; +} + +///////////////////////////////////////////////////////////////////////////// +// +// Keyboard input handling +// + +// forward declarations +static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); +static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); +static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); +static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length); +static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); + +typedef struct +{ + float x,y; // position of n'th character + float height; // height of line + int first_char, length; // first char of row, and length + int prev_first; // first char of previous row +} StbFindState; + +// find the x/y location of a character, and remember info about the previous row in +// case we get a move-up event (for page up, we'll have to rescan) +static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line) +{ + StbTexteditRow r; + int prev_start = 0; + int z = STB_TEXTEDIT_STRINGLEN(str); + int i=0, first; + + if (n == z) { + // if it's at the end, then find the last line -- simpler than trying to + // explicitly handle this case in the regular code + if (single_line) { + STB_TEXTEDIT_LAYOUTROW(&r, str, 0); + find->y = 0; + find->first_char = 0; + find->length = z; + find->height = r.ymax - r.ymin; + find->x = r.x1; + } else { + find->y = 0; + find->x = 0; + find->height = 1; + while (i < z) { + STB_TEXTEDIT_LAYOUTROW(&r, str, i); + prev_start = i; + i += r.num_chars; + } + find->first_char = i; + find->length = 0; + find->prev_first = prev_start; + } + return; + } + + // search rows to find the one that straddles character n + find->y = 0; + + for(;;) { + STB_TEXTEDIT_LAYOUTROW(&r, str, i); + if (n < i + r.num_chars) + break; + prev_start = i; + i += r.num_chars; + find->y += r.baseline_y_delta; + } + + find->first_char = first = i; + find->length = r.num_chars; + find->height = r.ymax - r.ymin; + find->prev_first = prev_start; + + // now scan to find xpos + find->x = r.x0; + for (i=0; first+i < n; ++i) + find->x += STB_TEXTEDIT_GETWIDTH(str, first, i); +} + +#define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) + +// make the selection/cursor state valid if client altered the string +static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + int n = STB_TEXTEDIT_STRINGLEN(str); + if (STB_TEXT_HAS_SELECTION(state)) { + if (state->select_start > n) state->select_start = n; + if (state->select_end > n) state->select_end = n; + // if clamping forced them to be equal, move the cursor to match + if (state->select_start == state->select_end) + state->cursor = state->select_start; + } + if (state->cursor > n) state->cursor = n; +} + +// delete characters while updating undo +static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) +{ + stb_text_makeundo_delete(str, state, where, len); + STB_TEXTEDIT_DELETECHARS(str, where, len); + state->has_preferred_x = 0; +} + +// delete the section +static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + stb_textedit_clamp(str, state); + if (STB_TEXT_HAS_SELECTION(state)) { + if (state->select_start < state->select_end) { + stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start); + state->select_end = state->cursor = state->select_start; + } else { + stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end); + state->select_start = state->cursor = state->select_end; + } + state->has_preferred_x = 0; + } +} + +// canoncialize the selection so start <= end +static void stb_textedit_sortselection(STB_TexteditState *state) +{ + if (state->select_end < state->select_start) { + int temp = state->select_end; + state->select_end = state->select_start; + state->select_start = temp; + } +} + +// move cursor to first character of selection +static void stb_textedit_move_to_first(STB_TexteditState *state) +{ + if (STB_TEXT_HAS_SELECTION(state)) { + stb_textedit_sortselection(state); + state->cursor = state->select_start; + state->select_end = state->select_start; + state->has_preferred_x = 0; + } +} + +// move cursor to last character of selection +static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + if (STB_TEXT_HAS_SELECTION(state)) { + stb_textedit_sortselection(state); + stb_textedit_clamp(str, state); + state->cursor = state->select_end; + state->select_start = state->select_end; + state->has_preferred_x = 0; + } +} + +#ifdef STB_TEXTEDIT_IS_SPACE +static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx ) +{ + return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1; +} + +#ifndef STB_TEXTEDIT_MOVEWORDLEFT +static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c ) +{ + --c; // always move at least one character + while( c >= 0 && !is_word_boundary( str, c ) ) + --c; + + if( c < 0 ) + c = 0; + + return c; +} +#define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous +#endif + +#ifndef STB_TEXTEDIT_MOVEWORDRIGHT +static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c ) +{ + const int len = STB_TEXTEDIT_STRINGLEN(str); + ++c; // always move at least one character + while( c < len && !is_word_boundary( str, c ) ) + ++c; + + if( c > len ) + c = len; + + return c; +} +#define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next +#endif + +#endif + +// update selection and cursor to match each other +static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state) +{ + if (!STB_TEXT_HAS_SELECTION(state)) + state->select_start = state->select_end = state->cursor; + else + state->cursor = state->select_end; +} + +// API cut: delete selection +static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + if (STB_TEXT_HAS_SELECTION(state)) { + stb_textedit_delete_selection(str,state); // implicitly clamps + state->has_preferred_x = 0; + return 1; + } + return 0; +} + +// API paste: replace existing selection with passed-in text +static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) +{ + // if there's a selection, the paste should delete it + stb_textedit_clamp(str, state); + stb_textedit_delete_selection(str,state); + // try to insert the characters + if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) { + stb_text_makeundo_insert(state, state->cursor, len); + state->cursor += len; + state->has_preferred_x = 0; + return 1; + } + // remove the undo since we didn't actually insert the characters + if (state->undostate.undo_point) + --state->undostate.undo_point; + return 0; +} + +#ifndef STB_TEXTEDIT_KEYTYPE +#define STB_TEXTEDIT_KEYTYPE int +#endif + +// API key: process a keyboard input +static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) +{ +retry: + switch (key) { + default: { + int c = STB_TEXTEDIT_KEYTOTEXT(key); + if (c > 0) { + STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c; + + // can't add newline in single-line mode + if (c == '\n' && state->single_line) + break; + + if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { + stb_text_makeundo_replace(str, state, state->cursor, 1, 1); + STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); + if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { + ++state->cursor; + state->has_preferred_x = 0; + } + } else { + stb_textedit_delete_selection(str,state); // implicitly clamps + if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { + stb_text_makeundo_insert(state, state->cursor, 1); + ++state->cursor; + state->has_preferred_x = 0; + } + } + } + break; + } + +#ifdef STB_TEXTEDIT_K_INSERT + case STB_TEXTEDIT_K_INSERT: + state->insert_mode = !state->insert_mode; + break; +#endif + + case STB_TEXTEDIT_K_UNDO: + stb_text_undo(str, state); + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_REDO: + stb_text_redo(str, state); + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_LEFT: + // if currently there's a selection, move cursor to start of selection + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_first(state); + else + if (state->cursor > 0) + --state->cursor; + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_RIGHT: + // if currently there's a selection, move cursor to end of selection + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_last(str, state); + else + ++state->cursor; + stb_textedit_clamp(str, state); + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT: + stb_textedit_clamp(str, state); + stb_textedit_prep_selection_at_cursor(state); + // move selection left + if (state->select_end > 0) + --state->select_end; + state->cursor = state->select_end; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_MOVEWORDLEFT + case STB_TEXTEDIT_K_WORDLEFT: + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_first(state); + else { + state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); + stb_textedit_clamp( str, state ); + } + break; + + case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT: + if( !STB_TEXT_HAS_SELECTION( state ) ) + stb_textedit_prep_selection_at_cursor(state); + + state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor); + state->select_end = state->cursor; + + stb_textedit_clamp( str, state ); + break; +#endif + +#ifdef STB_TEXTEDIT_MOVEWORDRIGHT + case STB_TEXTEDIT_K_WORDRIGHT: + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_last(str, state); + else { + state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); + stb_textedit_clamp( str, state ); + } + break; + + case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT: + if( !STB_TEXT_HAS_SELECTION( state ) ) + stb_textedit_prep_selection_at_cursor(state); + + state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); + state->select_end = state->cursor; + + stb_textedit_clamp( str, state ); + break; +#endif + + case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT: + stb_textedit_prep_selection_at_cursor(state); + // move selection right + ++state->select_end; + stb_textedit_clamp(str, state); + state->cursor = state->select_end; + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_DOWN: + case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: { + StbFindState find; + StbTexteditRow row; + int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + + if (state->single_line) { + // on windows, up&down in single-line behave like left&right + key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT); + goto retry; + } + + if (sel) + stb_textedit_prep_selection_at_cursor(state); + else if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_last(str,state); + + // compute current position of cursor point + stb_textedit_clamp(str, state); + stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); + + // now find character position down a row + if (find.length) { + float goal_x = state->has_preferred_x ? state->preferred_x : find.x; + float x; + int start = find.first_char + find.length; + state->cursor = start; + STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); + x = row.x0; + for (i=0; i < row.num_chars; ++i) { + float dx = STB_TEXTEDIT_GETWIDTH(str, start, i); + #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE + if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) + break; + #endif + x += dx; + if (x > goal_x) + break; + ++state->cursor; + } + stb_textedit_clamp(str, state); + + state->has_preferred_x = 1; + state->preferred_x = goal_x; + + if (sel) + state->select_end = state->cursor; + } + break; + } + + case STB_TEXTEDIT_K_UP: + case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: { + StbFindState find; + StbTexteditRow row; + int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + + if (state->single_line) { + // on windows, up&down become left&right + key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT); + goto retry; + } + + if (sel) + stb_textedit_prep_selection_at_cursor(state); + else if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_move_to_first(state); + + // compute current position of cursor point + stb_textedit_clamp(str, state); + stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); + + // can only go up if there's a previous row + if (find.prev_first != find.first_char) { + // now find character position up a row + float goal_x = state->has_preferred_x ? state->preferred_x : find.x; + float x; + state->cursor = find.prev_first; + STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); + x = row.x0; + for (i=0; i < row.num_chars; ++i) { + float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i); + #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE + if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) + break; + #endif + x += dx; + if (x > goal_x) + break; + ++state->cursor; + } + stb_textedit_clamp(str, state); + + state->has_preferred_x = 1; + state->preferred_x = goal_x; + + if (sel) + state->select_end = state->cursor; + } + break; + } + + case STB_TEXTEDIT_K_DELETE: + case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT: + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_delete_selection(str, state); + else { + int n = STB_TEXTEDIT_STRINGLEN(str); + if (state->cursor < n) + stb_textedit_delete(str, state, state->cursor, 1); + } + state->has_preferred_x = 0; + break; + + case STB_TEXTEDIT_K_BACKSPACE: + case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT: + if (STB_TEXT_HAS_SELECTION(state)) + stb_textedit_delete_selection(str, state); + else { + stb_textedit_clamp(str, state); + if (state->cursor > 0) { + stb_textedit_delete(str, state, state->cursor-1, 1); + --state->cursor; + } + } + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_TEXTSTART2 + case STB_TEXTEDIT_K_TEXTSTART2: +#endif + case STB_TEXTEDIT_K_TEXTSTART: + state->cursor = state->select_start = state->select_end = 0; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_TEXTEND2 + case STB_TEXTEDIT_K_TEXTEND2: +#endif + case STB_TEXTEDIT_K_TEXTEND: + state->cursor = STB_TEXTEDIT_STRINGLEN(str); + state->select_start = state->select_end = 0; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_TEXTSTART2 + case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT: +#endif + case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT: + stb_textedit_prep_selection_at_cursor(state); + state->cursor = state->select_end = 0; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_TEXTEND2 + case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT: +#endif + case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT: + stb_textedit_prep_selection_at_cursor(state); + state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str); + state->has_preferred_x = 0; + break; + + +#ifdef STB_TEXTEDIT_K_LINESTART2 + case STB_TEXTEDIT_K_LINESTART2: +#endif + case STB_TEXTEDIT_K_LINESTART: + stb_textedit_clamp(str, state); + stb_textedit_move_to_first(state); + if (state->single_line) + state->cursor = 0; + else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) + --state->cursor; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_LINEEND2 + case STB_TEXTEDIT_K_LINEEND2: +#endif + case STB_TEXTEDIT_K_LINEEND: { + int n = STB_TEXTEDIT_STRINGLEN(str); + stb_textedit_clamp(str, state); + stb_textedit_move_to_first(state); + if (state->single_line) + state->cursor = n; + else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) + ++state->cursor; + state->has_preferred_x = 0; + break; + } + +#ifdef STB_TEXTEDIT_K_LINESTART2 + case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT: +#endif + case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: + stb_textedit_clamp(str, state); + stb_textedit_prep_selection_at_cursor(state); + if (state->single_line) + state->cursor = 0; + else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE) + --state->cursor; + state->select_end = state->cursor; + state->has_preferred_x = 0; + break; + +#ifdef STB_TEXTEDIT_K_LINEEND2 + case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT: +#endif + case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: { + int n = STB_TEXTEDIT_STRINGLEN(str); + stb_textedit_clamp(str, state); + stb_textedit_prep_selection_at_cursor(state); + if (state->single_line) + state->cursor = n; + else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE) + ++state->cursor; + state->select_end = state->cursor; + state->has_preferred_x = 0; + break; + } + +// @TODO: +// STB_TEXTEDIT_K_PGUP - move cursor up a page +// STB_TEXTEDIT_K_PGDOWN - move cursor down a page + } +} + +///////////////////////////////////////////////////////////////////////////// +// +// Undo processing +// +// @OPTIMIZE: the undo/redo buffer should be circular + +static void stb_textedit_flush_redo(StbUndoState *state) +{ + state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; + state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; +} + +// discard the oldest entry in the undo list +static void stb_textedit_discard_undo(StbUndoState *state) +{ + if (state->undo_point > 0) { + // if the 0th undo state has characters, clean those up + if (state->undo_rec[0].char_storage >= 0) { + int n = state->undo_rec[0].insert_length, i; + // delete n characters from all other records + state->undo_char_point -= n; + STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE))); + for (i=0; i < state->undo_point; ++i) + if (state->undo_rec[i].char_storage >= 0) + state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it + } + --state->undo_point; + STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); + } +} + +// discard the oldest entry in the redo list--it's bad if this +// ever happens, but because undo & redo have to store the actual +// characters in different cases, the redo character buffer can +// fill up even though the undo buffer didn't +static void stb_textedit_discard_redo(StbUndoState *state) +{ + int k = STB_TEXTEDIT_UNDOSTATECOUNT-1; + + if (state->redo_point <= k) { + // if the k'th undo state has characters, clean those up + if (state->undo_rec[k].char_storage >= 0) { + int n = state->undo_rec[k].insert_length, i; + // move the remaining redo character data to the end of the buffer + state->redo_char_point += n; + STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE))); + // adjust the position of all the other records to account for above memmove + for (i=state->redo_point; i < k; ++i) + if (state->undo_rec[i].char_storage >= 0) + state->undo_rec[i].char_storage += n; + } + // now move all the redo records towards the end of the buffer; the first one is at 'redo_point' + // {DEAR IMGUI] + size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); + const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; + const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; + IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin); + IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end); + STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); + + // now move redo_point to point to the new one + ++state->redo_point; + } +} + +static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars) +{ + // any time we create a new undo record, we discard redo + stb_textedit_flush_redo(state); + + // if we have no free records, we have to make room, by sliding the + // existing records down + if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + stb_textedit_discard_undo(state); + + // if the characters to store won't possibly fit in the buffer, we can't undo + if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) { + state->undo_point = 0; + state->undo_char_point = 0; + return NULL; + } + + // if we don't have enough free characters in the buffer, we have to make room + while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT) + stb_textedit_discard_undo(state); + + return &state->undo_rec[state->undo_point++]; +} + +static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) +{ + StbUndoRecord *r = stb_text_create_undo_record(state, insert_len); + if (r == NULL) + return NULL; + + r->where = pos; + r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len; + r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len; + + if (insert_len == 0) { + r->char_storage = -1; + return NULL; + } else { + r->char_storage = state->undo_char_point; + state->undo_char_point += insert_len; + return &state->undo_char[r->char_storage]; + } +} + +static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + StbUndoState *s = &state->undostate; + StbUndoRecord u, *r; + if (s->undo_point == 0) + return; + + // we need to do two things: apply the undo record, and create a redo record + u = s->undo_rec[s->undo_point-1]; + r = &s->undo_rec[s->redo_point-1]; + r->char_storage = -1; + + r->insert_length = u.delete_length; + r->delete_length = u.insert_length; + r->where = u.where; + + if (u.delete_length) { + // if the undo record says to delete characters, then the redo record will + // need to re-insert the characters that get deleted, so we need to store + // them. + + // there are three cases: + // there's enough room to store the characters + // characters stored for *redoing* don't leave room for redo + // characters stored for *undoing* don't leave room for redo + // if the last is true, we have to bail + + if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) { + // the undo records take up too much character space; there's no space to store the redo characters + r->insert_length = 0; + } else { + int i; + + // there's definitely room to store the characters eventually + while (s->undo_char_point + u.delete_length > s->redo_char_point) { + // should never happen: + if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + return; + // there's currently not enough room, so discard a redo record + stb_textedit_discard_redo(s); + } + r = &s->undo_rec[s->redo_point-1]; + + r->char_storage = s->redo_char_point - u.delete_length; + s->redo_char_point = s->redo_char_point - u.delete_length; + + // now save the characters + for (i=0; i < u.delete_length; ++i) + s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i); + } + + // now we can carry out the deletion + STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length); + } + + // check type of recorded action: + if (u.insert_length) { + // easy case: was a deletion, so we need to insert n characters + STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length); + s->undo_char_point -= u.insert_length; + } + + state->cursor = u.where + u.insert_length; + + s->undo_point--; + s->redo_point--; +} + +static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +{ + StbUndoState *s = &state->undostate; + StbUndoRecord *u, r; + if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + return; + + // we need to do two things: apply the redo record, and create an undo record + u = &s->undo_rec[s->undo_point]; + r = s->undo_rec[s->redo_point]; + + // we KNOW there must be room for the undo record, because the redo record + // was derived from an undo record + + u->delete_length = r.insert_length; + u->insert_length = r.delete_length; + u->where = r.where; + u->char_storage = -1; + + if (r.delete_length) { + // the redo record requires us to delete characters, so the undo record + // needs to store the characters + + if (s->undo_char_point + u->insert_length > s->redo_char_point) { + u->insert_length = 0; + u->delete_length = 0; + } else { + int i; + u->char_storage = s->undo_char_point; + s->undo_char_point = s->undo_char_point + u->insert_length; + + // now save the characters + for (i=0; i < u->insert_length; ++i) + s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i); + } + + STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length); + } + + if (r.insert_length) { + // easy case: need to insert n characters + STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length); + s->redo_char_point += r.insert_length; + } + + state->cursor = r.where + r.insert_length; + + s->undo_point++; + s->redo_point++; +} + +static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length) +{ + stb_text_createundo(&state->undostate, where, 0, length); +} + +static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) +{ + int i; + STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); + if (p) { + for (i=0; i < length; ++i) + p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); + } +} + +static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) +{ + int i; + STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); + if (p) { + for (i=0; i < old_length; ++i) + p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); + } +} + +// reset the state to default +static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line) +{ + state->undostate.undo_point = 0; + state->undostate.undo_char_point = 0; + state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; + state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; + state->select_end = state->select_start = 0; + state->cursor = 0; + state->has_preferred_x = 0; + state->preferred_x = 0; + state->cursor_at_end_of_line = 0; + state->initialized = 1; + state->single_line = (unsigned char) is_single_line; + state->insert_mode = 0; +} + +// API initialize +static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line) +{ + stb_textedit_clear_state(state, is_single_line); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif + +static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len) +{ + return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +#endif//STB_TEXTEDIT_IMPLEMENTATION + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/imgui/imstb_truetype.h b/imgui/imstb_truetype.h new file mode 100644 index 00000000..c1cdb180 --- /dev/null +++ b/imgui/imstb_truetype.h @@ -0,0 +1,4903 @@ +// [DEAR IMGUI] +// This is a slightly modified version of stb_truetype.h 1.20. +// Mostly fixing for compiler and static analyzer warnings. +// Grep for [DEAR IMGUI] to find the changes. + +// stb_truetype.h - v1.20 - public domain +// authored from 2009-2016 by Sean Barrett / RAD Game Tools +// +// This library processes TrueType files: +// parse files +// extract glyph metrics +// extract glyph shapes +// render glyphs to one-channel bitmaps with antialiasing (box filter) +// render glyphs to one-channel SDF bitmaps (signed-distance field/function) +// +// Todo: +// non-MS cmaps +// crashproof on bad data +// hinting? (no longer patented) +// cleartype-style AA? +// optimize: use simple memory allocator for intermediates +// optimize: build edge-list directly from curves +// optimize: rasterize directly from curves? +// +// ADDITIONAL CONTRIBUTORS +// +// Mikko Mononen: compound shape support, more cmap formats +// Tor Andersson: kerning, subpixel rendering +// Dougall Johnson: OpenType / Type 2 font handling +// Daniel Ribeiro Maciel: basic GPOS-based kerning +// +// Misc other: +// Ryan Gordon +// Simon Glass +// github:IntellectualKitty +// Imanol Celaya +// Daniel Ribeiro Maciel +// +// Bug/warning reports/fixes: +// "Zer" on mollyrocket Fabian "ryg" Giesen +// Cass Everitt Martins Mozeiko +// stoiko (Haemimont Games) Cap Petschulat +// Brian Hook Omar Cornut +// Walter van Niftrik github:aloucks +// David Gow Peter LaValle +// David Given Sergey Popov +// Ivan-Assen Ivanov Giumo X. Clanjor +// Anthony Pesch Higor Euripedes +// Johan Duparc Thomas Fields +// Hou Qiming Derek Vinyard +// Rob Loach Cort Stratton +// Kenney Phillis Jr. github:oyvindjam +// Brian Costabile github:vassvik +// +// VERSION HISTORY +// +// 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() +// 1.19 (2018-02-11) GPOS kerning, STBTT_fmod +// 1.18 (2018-01-29) add missing function +// 1.17 (2017-07-23) make more arguments const; doc fix +// 1.16 (2017-07-12) SDF support +// 1.15 (2017-03-03) make more arguments const +// 1.14 (2017-01-16) num-fonts-in-TTC function +// 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts +// 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef +// 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly +// 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges +// 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; +// variant PackFontRanges to pack and render in separate phases; +// fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); +// fixed an assert() bug in the new rasterizer +// replace assert() with STBTT_assert() in new rasterizer +// +// Full history can be found at the end of this file. +// +// LICENSE +// +// See end of file for license information. +// +// USAGE +// +// Include this file in whatever places need to refer to it. In ONE C/C++ +// file, write: +// #define STB_TRUETYPE_IMPLEMENTATION +// before the #include of this file. This expands out the actual +// implementation into that C/C++ file. +// +// To make the implementation private to the file that generates the implementation, +// #define STBTT_STATIC +// +// Simple 3D API (don't ship this, but it's fine for tools and quick start) +// stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture +// stbtt_GetBakedQuad() -- compute quad to draw for a given char +// +// Improved 3D API (more shippable): +// #include "stb_rect_pack.h" -- optional, but you really want it +// stbtt_PackBegin() +// stbtt_PackSetOversampling() -- for improved quality on small fonts +// stbtt_PackFontRanges() -- pack and renders +// stbtt_PackEnd() +// stbtt_GetPackedQuad() +// +// "Load" a font file from a memory buffer (you have to keep the buffer loaded) +// stbtt_InitFont() +// stbtt_GetFontOffsetForIndex() -- indexing for TTC font collections +// stbtt_GetNumberOfFonts() -- number of fonts for TTC font collections +// +// Render a unicode codepoint to a bitmap +// stbtt_GetCodepointBitmap() -- allocates and returns a bitmap +// stbtt_MakeCodepointBitmap() -- renders into bitmap you provide +// stbtt_GetCodepointBitmapBox() -- how big the bitmap must be +// +// Character advance/positioning +// stbtt_GetCodepointHMetrics() +// stbtt_GetFontVMetrics() +// stbtt_GetFontVMetricsOS2() +// stbtt_GetCodepointKernAdvance() +// +// Starting with version 1.06, the rasterizer was replaced with a new, +// faster and generally-more-precise rasterizer. The new rasterizer more +// accurately measures pixel coverage for anti-aliasing, except in the case +// where multiple shapes overlap, in which case it overestimates the AA pixel +// coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If +// this turns out to be a problem, you can re-enable the old rasterizer with +// #define STBTT_RASTERIZER_VERSION 1 +// which will incur about a 15% speed hit. +// +// ADDITIONAL DOCUMENTATION +// +// Immediately after this block comment are a series of sample programs. +// +// After the sample programs is the "header file" section. This section +// includes documentation for each API function. +// +// Some important concepts to understand to use this library: +// +// Codepoint +// Characters are defined by unicode codepoints, e.g. 65 is +// uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is +// the hiragana for "ma". +// +// Glyph +// A visual character shape (every codepoint is rendered as +// some glyph) +// +// Glyph index +// A font-specific integer ID representing a glyph +// +// Baseline +// Glyph shapes are defined relative to a baseline, which is the +// bottom of uppercase characters. Characters extend both above +// and below the baseline. +// +// Current Point +// As you draw text to the screen, you keep track of a "current point" +// which is the origin of each character. The current point's vertical +// position is the baseline. Even "baked fonts" use this model. +// +// Vertical Font Metrics +// The vertical qualities of the font, used to vertically position +// and space the characters. See docs for stbtt_GetFontVMetrics. +// +// Font Size in Pixels or Points +// The preferred interface for specifying font sizes in stb_truetype +// is to specify how tall the font's vertical extent should be in pixels. +// If that sounds good enough, skip the next paragraph. +// +// Most font APIs instead use "points", which are a common typographic +// measurement for describing font size, defined as 72 points per inch. +// stb_truetype provides a point API for compatibility. However, true +// "per inch" conventions don't make much sense on computer displays +// since different monitors have different number of pixels per +// inch. For example, Windows traditionally uses a convention that +// there are 96 pixels per inch, thus making 'inch' measurements have +// nothing to do with inches, and thus effectively defining a point to +// be 1.333 pixels. Additionally, the TrueType font data provides +// an explicit scale factor to scale a given font's glyphs to points, +// but the author has observed that this scale factor is often wrong +// for non-commercial fonts, thus making fonts scaled in points +// according to the TrueType spec incoherently sized in practice. +// +// DETAILED USAGE: +// +// Scale: +// Select how high you want the font to be, in points or pixels. +// Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute +// a scale factor SF that will be used by all other functions. +// +// Baseline: +// You need to select a y-coordinate that is the baseline of where +// your text will appear. Call GetFontBoundingBox to get the baseline-relative +// bounding box for all characters. SF*-y0 will be the distance in pixels +// that the worst-case character could extend above the baseline, so if +// you want the top edge of characters to appear at the top of the +// screen where y=0, then you would set the baseline to SF*-y0. +// +// Current point: +// Set the current point where the first character will appear. The +// first character could extend left of the current point; this is font +// dependent. You can either choose a current point that is the leftmost +// point and hope, or add some padding, or check the bounding box or +// left-side-bearing of the first character to be displayed and set +// the current point based on that. +// +// Displaying a character: +// Compute the bounding box of the character. It will contain signed values +// relative to . I.e. if it returns x0,y0,x1,y1, +// then the character should be displayed in the rectangle from +// to = 32 && *text < 128) { + stbtt_aligned_quad q; + stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9 + glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0); + glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0); + glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1); + glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1); + } + ++text; + } + glEnd(); +} +#endif +// +// +////////////////////////////////////////////////////////////////////////////// +// +// Complete program (this compiles): get a single bitmap, print as ASCII art +// +#if 0 +#include +#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation +#include "stb_truetype.h" + +char ttf_buffer[1<<25]; + +int main(int argc, char **argv) +{ + stbtt_fontinfo font; + unsigned char *bitmap; + int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20); + + fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb")); + + stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0)); + bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0); + + for (j=0; j < h; ++j) { + for (i=0; i < w; ++i) + putchar(" .:ioVM@"[bitmap[j*w+i]>>5]); + putchar('\n'); + } + return 0; +} +#endif +// +// Output: +// +// .ii. +// @@@@@@. +// V@Mio@@o +// :i. V@V +// :oM@@M +// :@@@MM@M +// @@o o@M +// :@@. M@M +// @@@o@@@@ +// :M@@V:@@. +// +////////////////////////////////////////////////////////////////////////////// +// +// Complete program: print "Hello World!" banner, with bugs +// +#if 0 +char buffer[24<<20]; +unsigned char screen[20][79]; + +int main(int arg, char **argv) +{ + stbtt_fontinfo font; + int i,j,ascent,baseline,ch=0; + float scale, xpos=2; // leave a little padding in case the character extends left + char *text = "Heljo World!"; // intentionally misspelled to show 'lj' brokenness + + fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb")); + stbtt_InitFont(&font, buffer, 0); + + scale = stbtt_ScaleForPixelHeight(&font, 15); + stbtt_GetFontVMetrics(&font, &ascent,0,0); + baseline = (int) (ascent*scale); + + while (text[ch]) { + int advance,lsb,x0,y0,x1,y1; + float x_shift = xpos - (float) floor(xpos); + stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb); + stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1); + stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]); + // note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong + // because this API is really for baking character bitmaps into textures. if you want to render + // a sequence of characters, you really need to render each bitmap to a temp buffer, then + // "alpha blend" that into the working buffer + xpos += (advance * scale); + if (text[ch+1]) + xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]); + ++ch; + } + + for (j=0; j < 20; ++j) { + for (i=0; i < 78; ++i) + putchar(" .:ioVM@"[screen[j][i]>>5]); + putchar('\n'); + } + + return 0; +} +#endif + + +////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// +//// +//// INTEGRATION WITH YOUR CODEBASE +//// +//// The following sections allow you to supply alternate definitions +//// of C library functions used by stb_truetype, e.g. if you don't +//// link with the C runtime library. + +#ifdef STB_TRUETYPE_IMPLEMENTATION + // #define your own (u)stbtt_int8/16/32 before including to override this + #ifndef stbtt_uint8 + typedef unsigned char stbtt_uint8; + typedef signed char stbtt_int8; + typedef unsigned short stbtt_uint16; + typedef signed short stbtt_int16; + typedef unsigned int stbtt_uint32; + typedef signed int stbtt_int32; + #endif + + typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1]; + typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1]; + + // e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h + #ifndef STBTT_ifloor + #include + #define STBTT_ifloor(x) ((int) floor(x)) + #define STBTT_iceil(x) ((int) ceil(x)) + #endif + + #ifndef STBTT_sqrt + #include + #define STBTT_sqrt(x) sqrt(x) + #define STBTT_pow(x,y) pow(x,y) + #endif + + #ifndef STBTT_fmod + #include + #define STBTT_fmod(x,y) fmod(x,y) + #endif + + #ifndef STBTT_cos + #include + #define STBTT_cos(x) cos(x) + #define STBTT_acos(x) acos(x) + #endif + + #ifndef STBTT_fabs + #include + #define STBTT_fabs(x) fabs(x) + #endif + + // #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h + #ifndef STBTT_malloc + #include + #define STBTT_malloc(x,u) ((void)(u),malloc(x)) + #define STBTT_free(x,u) ((void)(u),free(x)) + #endif + + #ifndef STBTT_assert + #include + #define STBTT_assert(x) assert(x) + #endif + + #ifndef STBTT_strlen + #include + #define STBTT_strlen(x) strlen(x) + #endif + + #ifndef STBTT_memcpy + #include + #define STBTT_memcpy memcpy + #define STBTT_memset memset + #endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//// +//// INTERFACE +//// +//// + +#ifndef __STB_INCLUDE_STB_TRUETYPE_H__ +#define __STB_INCLUDE_STB_TRUETYPE_H__ + +#ifdef STBTT_STATIC +#define STBTT_DEF static +#else +#define STBTT_DEF extern +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// private structure +typedef struct +{ + unsigned char *data; + int cursor; + int size; +} stbtt__buf; + +////////////////////////////////////////////////////////////////////////////// +// +// TEXTURE BAKING API +// +// If you use this API, you only have to call two functions ever. +// + +typedef struct +{ + unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap + float xoff,yoff,xadvance; +} stbtt_bakedchar; + +STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) + float pixel_height, // height of font in pixels + unsigned char *pixels, int pw, int ph, // bitmap to be filled in + int first_char, int num_chars, // characters to bake + stbtt_bakedchar *chardata); // you allocate this, it's num_chars long +// if return is positive, the first unused row of the bitmap +// if return is negative, returns the negative of the number of characters that fit +// if return is 0, no characters fit and no rows were used +// This uses a very crappy packing. + +typedef struct +{ + float x0,y0,s0,t0; // top-left + float x1,y1,s1,t1; // bottom-right +} stbtt_aligned_quad; + +STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, // same data as above + int char_index, // character to display + float *xpos, float *ypos, // pointers to current position in screen pixel space + stbtt_aligned_quad *q, // output: quad to draw + int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier +// Call GetBakedQuad with char_index = 'character - first_char', and it +// creates the quad you need to draw and advances the current position. +// +// The coordinate system used assumes y increases downwards. +// +// Characters will extend both above and below the current position; +// see discussion of "BASELINE" above. +// +// It's inefficient; you might want to c&p it and optimize it. + +STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap); +// Query the font vertical metrics without having to create a font first. + + +////////////////////////////////////////////////////////////////////////////// +// +// NEW TEXTURE BAKING API +// +// This provides options for packing multiple fonts into one atlas, not +// perfectly but better than nothing. + +typedef struct +{ + unsigned short x0,y0,x1,y1; // coordinates of bbox in bitmap + float xoff,yoff,xadvance; + float xoff2,yoff2; +} stbtt_packedchar; + +typedef struct stbtt_pack_context stbtt_pack_context; +typedef struct stbtt_fontinfo stbtt_fontinfo; +#ifndef STB_RECT_PACK_VERSION +typedef struct stbrp_rect stbrp_rect; +#endif + +STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int width, int height, int stride_in_bytes, int padding, void *alloc_context); +// Initializes a packing context stored in the passed-in stbtt_pack_context. +// Future calls using this context will pack characters into the bitmap passed +// in here: a 1-channel bitmap that is width * height. stride_in_bytes is +// the distance from one row to the next (or 0 to mean they are packed tightly +// together). "padding" is the amount of padding to leave between each +// character (normally you want '1' for bitmaps you'll use as textures with +// bilinear filtering). +// +// Returns 0 on failure, 1 on success. + +STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc); +// Cleans up the packing context and frees all memory. + +#define STBTT_POINT_SIZE(x) (-(x)) + +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size, + int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range); +// Creates character bitmaps from the font_index'th font found in fontdata (use +// font_index=0 if you don't know what that is). It creates num_chars_in_range +// bitmaps for characters with unicode values starting at first_unicode_char_in_range +// and increasing. Data for how to render them is stored in chardata_for_range; +// pass these to stbtt_GetPackedQuad to get back renderable quads. +// +// font_size is the full height of the character from ascender to descender, +// as computed by stbtt_ScaleForPixelHeight. To use a point size as computed +// by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE() +// and pass that result as 'font_size': +// ..., 20 , ... // font max minus min y is 20 pixels tall +// ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall + +typedef struct +{ + float font_size; + int first_unicode_codepoint_in_range; // if non-zero, then the chars are continuous, and this is the first codepoint + int *array_of_unicode_codepoints; // if non-zero, then this is an array of unicode codepoints + int num_chars; + stbtt_packedchar *chardata_for_range; // output + unsigned char h_oversample, v_oversample; // don't set these, they're used internally +} stbtt_pack_range; + +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges); +// Creates character bitmaps from multiple ranges of characters stored in +// ranges. This will usually create a better-packed bitmap than multiple +// calls to stbtt_PackFontRange. Note that you can call this multiple +// times within a single PackBegin/PackEnd. + +STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample); +// Oversampling a font increases the quality by allowing higher-quality subpixel +// positioning, and is especially valuable at smaller text sizes. +// +// This function sets the amount of oversampling for all following calls to +// stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given +// pack context. The default (no oversampling) is achieved by h_oversample=1 +// and v_oversample=1. The total number of pixels required is +// h_oversample*v_oversample larger than the default; for example, 2x2 +// oversampling requires 4x the storage of 1x1. For best results, render +// oversampled textures with bilinear filtering. Look at the readme in +// stb/tests/oversample for information about oversampled fonts +// +// To use with PackFontRangesGather etc., you must set it before calls +// call to PackFontRangesGatherRects. + +STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip); +// If skip != 0, this tells stb_truetype to skip any codepoints for which +// there is no corresponding glyph. If skip=0, which is the default, then +// codepoints without a glyph recived the font's "missing character" glyph, +// typically an empty box by convention. + +STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, // same data as above + int char_index, // character to display + float *xpos, float *ypos, // pointers to current position in screen pixel space + stbtt_aligned_quad *q, // output: quad to draw + int align_to_integer); + +STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); +STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects); +STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects); +// Calling these functions in sequence is roughly equivalent to calling +// stbtt_PackFontRanges(). If you more control over the packing of multiple +// fonts, or if you want to pack custom data into a font texture, take a look +// at the source to of stbtt_PackFontRanges() and create a custom version +// using these functions, e.g. call GatherRects multiple times, +// building up a single array of rects, then call PackRects once, +// then call RenderIntoRects repeatedly. This may result in a +// better packing than calling PackFontRanges multiple times +// (or it may not). + +// this is an opaque structure that you shouldn't mess with which holds +// all the context needed from PackBegin to PackEnd. +struct stbtt_pack_context { + void *user_allocator_context; + void *pack_info; + int width; + int height; + int stride_in_bytes; + int padding; + int skip_missing; + unsigned int h_oversample, v_oversample; + unsigned char *pixels; + void *nodes; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// FONT LOADING +// +// + +STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data); +// This function will determine the number of fonts in a font file. TrueType +// collection (.ttc) files may contain multiple fonts, while TrueType font +// (.ttf) files only contain one font. The number of fonts can be used for +// indexing with the previous function where the index is between zero and one +// less than the total fonts. If an error occurs, -1 is returned. + +STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index); +// Each .ttf/.ttc file may have more than one font. Each font has a sequential +// index number starting from 0. Call this function to get the font offset for +// a given index; it returns -1 if the index is out of range. A regular .ttf +// file will only define one font and it always be at offset 0, so it will +// return '0' for index 0, and -1 for all other indices. + +// The following structure is defined publicly so you can declare one on +// the stack or as a global or etc, but you should treat it as opaque. +struct stbtt_fontinfo +{ + void * userdata; + unsigned char * data; // pointer to .ttf file + int fontstart; // offset of start of font + + int numGlyphs; // number of glyphs, needed for range checking + + int loca,head,glyf,hhea,hmtx,kern,gpos; // table locations as offset from start of .ttf + int index_map; // a cmap mapping for our chosen character encoding + int indexToLocFormat; // format needed to map from glyph index to glyph + + stbtt__buf cff; // cff font data + stbtt__buf charstrings; // the charstring index + stbtt__buf gsubrs; // global charstring subroutines index + stbtt__buf subrs; // private charstring subroutines index + stbtt__buf fontdicts; // array of font dicts + stbtt__buf fdselect; // map from glyph to fontdict +}; + +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset); +// Given an offset into the file that defines a font, this function builds +// the necessary cached info for the rest of the system. You must allocate +// the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't +// need to do anything special to free it, because the contents are pure +// value data with no additional data structures. Returns 0 on failure. + + +////////////////////////////////////////////////////////////////////////////// +// +// CHARACTER TO GLYPH-INDEX CONVERSIOn + +STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint); +// If you're going to perform multiple operations on the same character +// and you want a speed-up, call this function with the character you're +// going to process, then use glyph-based functions instead of the +// codepoint-based functions. +// Returns 0 if the character codepoint is not defined in the font. + + +////////////////////////////////////////////////////////////////////////////// +// +// CHARACTER PROPERTIES +// + +STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels); +// computes a scale factor to produce a font whose "height" is 'pixels' tall. +// Height is measured as the distance from the highest ascender to the lowest +// descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics +// and computing: +// scale = pixels / (ascent - descent) +// so if you prefer to measure height by the ascent only, use a similar calculation. + +STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels); +// computes a scale factor to produce a font whose EM size is mapped to +// 'pixels' tall. This is probably what traditional APIs compute, but +// I'm not positive. + +STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap); +// ascent is the coordinate above the baseline the font extends; descent +// is the coordinate below the baseline the font extends (i.e. it is typically negative) +// lineGap is the spacing between one row's descent and the next row's ascent... +// so you should advance the vertical position by "*ascent - *descent + *lineGap" +// these are expressed in unscaled coordinates, so you must multiply by +// the scale factor for a given size + +STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap); +// analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 +// table (specific to MS/Windows TTF files). +// +// Returns 1 on success (table present), 0 on failure. + +STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1); +// the bounding box around all possible characters + +STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing); +// leftSideBearing is the offset from the current horizontal position to the left edge of the character +// advanceWidth is the offset from the current horizontal position to the next horizontal position +// these are expressed in unscaled coordinates + +STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2); +// an additional amount to add to the 'advance' value between ch1 and ch2 + +STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1); +// Gets the bounding box of the visible part of the glyph, in unscaled coordinates + +STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing); +STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2); +STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); +// as above, but takes one or more glyph indices for greater efficiency + + +////////////////////////////////////////////////////////////////////////////// +// +// GLYPH SHAPES (you probably don't need these, but they have to go before +// the bitmaps for C declaration-order reasons) +// + +#ifndef STBTT_vmove // you can predefine these to use different values (but why?) + enum { + STBTT_vmove=1, + STBTT_vline, + STBTT_vcurve, + STBTT_vcubic + }; +#endif + +#ifndef stbtt_vertex // you can predefine this to use different values + // (we share this with other code at RAD) + #define stbtt_vertex_type short // can't use stbtt_int16 because that's not visible in the header file + typedef struct + { + stbtt_vertex_type x,y,cx,cy,cx1,cy1; + unsigned char type,padding; + } stbtt_vertex; +#endif + +STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index); +// returns non-zero if nothing is drawn for this glyph + +STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices); +STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices); +// returns # of vertices and fills *vertices with the pointer to them +// these are expressed in "unscaled" coordinates +// +// The shape is a series of contours. Each one starts with +// a STBTT_moveto, then consists of a series of mixed +// STBTT_lineto and STBTT_curveto segments. A lineto +// draws a line from previous endpoint to its x,y; a curveto +// draws a quadratic bezier from previous endpoint to +// its x,y, using cx,cy as the bezier control point. + +STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices); +// frees the data allocated above + +////////////////////////////////////////////////////////////////////////////// +// +// BITMAP RENDERING +// + +STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata); +// frees the bitmap allocated below + +STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff); +// allocates a large-enough single-channel 8bpp bitmap and renders the +// specified character/glyph at the specified scale into it, with +// antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). +// *width & *height are filled out with the width & height of the bitmap, +// which is stored left-to-right, top-to-bottom. +// +// xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap + +STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff); +// the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel +// shift for the character + +STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint); +// the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap +// in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap +// is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the +// width and height and positioning info for it first. + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint); +// same as stbtt_MakeCodepointBitmap, but you can specify a subpixel +// shift for the character + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint); +// same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering +// is performed (see stbtt_PackSetOversampling) + +STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); +// get the bbox of the bitmap centered around the glyph origin; so the +// bitmap width is ix1-ix0, height is iy1-iy0, and location to place +// the bitmap top left is (leftSideBearing*scale,iy0). +// (Note that the bitmap uses y-increases-down, but the shape uses +// y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) + +STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1); +// same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel +// shift for the character + +// the following functions are equivalent to the above functions, but operate +// on glyph indices instead of Unicode codepoints (for efficiency) +STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff); +STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff); +STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph); +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph); +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int glyph); +STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1); +STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1); + + +// @TODO: don't expose this structure +typedef struct +{ + int w,h,stride; + unsigned char *pixels; +} stbtt__bitmap; + +// rasterize a shape with quadratic beziers into a bitmap +STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, // 1-channel bitmap to draw into + float flatness_in_pixels, // allowable error of curve in pixels + stbtt_vertex *vertices, // array of vertices defining shape + int num_verts, // number of vertices in above array + float scale_x, float scale_y, // scale applied to input vertices + float shift_x, float shift_y, // translation applied to input vertices + int x_off, int y_off, // another translation applied to input + int invert, // if non-zero, vertically flip shape + void *userdata); // context for to STBTT_MALLOC + +////////////////////////////////////////////////////////////////////////////// +// +// Signed Distance Function (or Field) rendering + +STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata); +// frees the SDF bitmap allocated below + +STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff); +STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff); +// These functions compute a discretized SDF field for a single character, suitable for storing +// in a single-channel texture, sampling with bilinear filtering, and testing against +// larger than some threshold to produce scalable fonts. +// info -- the font +// scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap +// glyph/codepoint -- the character to generate the SDF for +// padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), +// which allows effects like bit outlines +// onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) +// pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) +// if positive, > onedge_value is inside; if negative, < onedge_value is inside +// width,height -- output height & width of the SDF bitmap (including padding) +// xoff,yoff -- output origin of the character +// return value -- a 2D array of bytes 0..255, width*height in size +// +// pixel_dist_scale & onedge_value are a scale & bias that allows you to make +// optimal use of the limited 0..255 for your application, trading off precision +// and special effects. SDF values outside the range 0..255 are clamped to 0..255. +// +// Example: +// scale = stbtt_ScaleForPixelHeight(22) +// padding = 5 +// onedge_value = 180 +// pixel_dist_scale = 180/5.0 = 36.0 +// +// This will create an SDF bitmap in which the character is about 22 pixels +// high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled +// shape, sample the SDF at each pixel and fill the pixel if the SDF value +// is greater than or equal to 180/255. (You'll actually want to antialias, +// which is beyond the scope of this example.) Additionally, you can compute +// offset outlines (e.g. to stroke the character border inside & outside, +// or only outside). For example, to fill outside the character up to 3 SDF +// pixels, you would compare against (180-36.0*3)/255 = 72/255. The above +// choice of variables maps a range from 5 pixels outside the shape to +// 2 pixels inside the shape to 0..255; this is intended primarily for apply +// outside effects only (the interior range is needed to allow proper +// antialiasing of the font at *smaller* sizes) +// +// The function computes the SDF analytically at each SDF pixel, not by e.g. +// building a higher-res bitmap and approximating it. In theory the quality +// should be as high as possible for an SDF of this size & representation, but +// unclear if this is true in practice (perhaps building a higher-res bitmap +// and computing from that can allow drop-out prevention). +// +// The algorithm has not been optimized at all, so expect it to be slow +// if computing lots of characters or very large sizes. + + + +////////////////////////////////////////////////////////////////////////////// +// +// Finding the right font... +// +// You should really just solve this offline, keep your own tables +// of what font is what, and don't try to get it out of the .ttf file. +// That's because getting it out of the .ttf file is really hard, because +// the names in the file can appear in many possible encodings, in many +// possible languages, and e.g. if you need a case-insensitive comparison, +// the details of that depend on the encoding & language in a complex way +// (actually underspecified in truetype, but also gigantic). +// +// But you can use the provided functions in two possible ways: +// stbtt_FindMatchingFont() will use *case-sensitive* comparisons on +// unicode-encoded names to try to find the font you want; +// you can run this before calling stbtt_InitFont() +// +// stbtt_GetFontNameString() lets you get any of the various strings +// from the file yourself and do your own comparisons on them. +// You have to have called stbtt_InitFont() first. + + +STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags); +// returns the offset (not index) of the font that matches, or -1 if none +// if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". +// if you use any other flag, use a font name like "Arial"; this checks +// the 'macStyle' header field; i don't know if fonts set this consistently +#define STBTT_MACSTYLE_DONTCARE 0 +#define STBTT_MACSTYLE_BOLD 1 +#define STBTT_MACSTYLE_ITALIC 2 +#define STBTT_MACSTYLE_UNDERSCORE 4 +#define STBTT_MACSTYLE_NONE 8 // <= not same as 0, this makes us check the bitfield is 0 + +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2); +// returns 1/0 whether the first string interpreted as utf8 is identical to +// the second string interpreted as big-endian utf16... useful for strings from next func + +STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID); +// returns the string (which may be big-endian double byte, e.g. for unicode) +// and puts the length in bytes in *length. +// +// some of the values for the IDs are below; for more see the truetype spec: +// http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html +// http://www.microsoft.com/typography/otspec/name.htm + +enum { // platformID + STBTT_PLATFORM_ID_UNICODE =0, + STBTT_PLATFORM_ID_MAC =1, + STBTT_PLATFORM_ID_ISO =2, + STBTT_PLATFORM_ID_MICROSOFT =3 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_UNICODE + STBTT_UNICODE_EID_UNICODE_1_0 =0, + STBTT_UNICODE_EID_UNICODE_1_1 =1, + STBTT_UNICODE_EID_ISO_10646 =2, + STBTT_UNICODE_EID_UNICODE_2_0_BMP=3, + STBTT_UNICODE_EID_UNICODE_2_0_FULL=4 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_MICROSOFT + STBTT_MS_EID_SYMBOL =0, + STBTT_MS_EID_UNICODE_BMP =1, + STBTT_MS_EID_SHIFTJIS =2, + STBTT_MS_EID_UNICODE_FULL =10 +}; + +enum { // encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes + STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4, + STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5, + STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6, + STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7 +}; + +enum { // languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... + // problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs + STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410, + STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411, + STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412, + STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419, + STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409, + STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D +}; + +enum { // languageID for STBTT_PLATFORM_ID_MAC + STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11, + STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23, + STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32, + STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 , + STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 , + STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33, + STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19 +}; + +#ifdef __cplusplus +} +#endif + +#endif // __STB_INCLUDE_STB_TRUETYPE_H__ + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// +//// +//// IMPLEMENTATION +//// +//// + +#ifdef STB_TRUETYPE_IMPLEMENTATION + +#ifndef STBTT_MAX_OVERSAMPLE +#define STBTT_MAX_OVERSAMPLE 8 +#endif + +#if STBTT_MAX_OVERSAMPLE > 255 +#error "STBTT_MAX_OVERSAMPLE cannot be > 255" +#endif + +typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1]; + +#ifndef STBTT_RASTERIZER_VERSION +#define STBTT_RASTERIZER_VERSION 2 +#endif + +#ifdef _MSC_VER +#define STBTT__NOTUSED(v) (void)(v) +#else +#define STBTT__NOTUSED(v) (void)sizeof(v) +#endif + +////////////////////////////////////////////////////////////////////////// +// +// stbtt__buf helpers to parse data from file +// + +static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b) +{ + if (b->cursor >= b->size) + return 0; + return b->data[b->cursor++]; +} + +static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b) +{ + if (b->cursor >= b->size) + return 0; + return b->data[b->cursor]; +} + +static void stbtt__buf_seek(stbtt__buf *b, int o) +{ + STBTT_assert(!(o > b->size || o < 0)); + b->cursor = (o > b->size || o < 0) ? b->size : o; +} + +static void stbtt__buf_skip(stbtt__buf *b, int o) +{ + stbtt__buf_seek(b, b->cursor + o); +} + +static stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n) +{ + stbtt_uint32 v = 0; + int i; + STBTT_assert(n >= 1 && n <= 4); + for (i = 0; i < n; i++) + v = (v << 8) | stbtt__buf_get8(b); + return v; +} + +static stbtt__buf stbtt__new_buf(const void *p, size_t size) +{ + stbtt__buf r; + STBTT_assert(size < 0x40000000); + r.data = (stbtt_uint8*) p; + r.size = (int) size; + r.cursor = 0; + return r; +} + +#define stbtt__buf_get16(b) stbtt__buf_get((b), 2) +#define stbtt__buf_get32(b) stbtt__buf_get((b), 4) + +static stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s) +{ + stbtt__buf r = stbtt__new_buf(NULL, 0); + if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r; + r.data = b->data + o; + r.size = s; + return r; +} + +static stbtt__buf stbtt__cff_get_index(stbtt__buf *b) +{ + int count, start, offsize; + start = b->cursor; + count = stbtt__buf_get16(b); + if (count) { + offsize = stbtt__buf_get8(b); + STBTT_assert(offsize >= 1 && offsize <= 4); + stbtt__buf_skip(b, offsize * count); + stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1); + } + return stbtt__buf_range(b, start, b->cursor - start); +} + +static stbtt_uint32 stbtt__cff_int(stbtt__buf *b) +{ + int b0 = stbtt__buf_get8(b); + if (b0 >= 32 && b0 <= 246) return b0 - 139; + else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108; + else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108; + else if (b0 == 28) return stbtt__buf_get16(b); + else if (b0 == 29) return stbtt__buf_get32(b); + STBTT_assert(0); + return 0; +} + +static void stbtt__cff_skip_operand(stbtt__buf *b) { + int v, b0 = stbtt__buf_peek8(b); + STBTT_assert(b0 >= 28); + if (b0 == 30) { + stbtt__buf_skip(b, 1); + while (b->cursor < b->size) { + v = stbtt__buf_get8(b); + if ((v & 0xF) == 0xF || (v >> 4) == 0xF) + break; + } + } else { + stbtt__cff_int(b); + } +} + +static stbtt__buf stbtt__dict_get(stbtt__buf *b, int key) +{ + stbtt__buf_seek(b, 0); + while (b->cursor < b->size) { + int start = b->cursor, end, op; + while (stbtt__buf_peek8(b) >= 28) + stbtt__cff_skip_operand(b); + end = b->cursor; + op = stbtt__buf_get8(b); + if (op == 12) op = stbtt__buf_get8(b) | 0x100; + if (op == key) return stbtt__buf_range(b, start, end-start); + } + return stbtt__buf_range(b, 0, 0); +} + +static void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out) +{ + int i; + stbtt__buf operands = stbtt__dict_get(b, key); + for (i = 0; i < outcount && operands.cursor < operands.size; i++) + out[i] = stbtt__cff_int(&operands); +} + +static int stbtt__cff_index_count(stbtt__buf *b) +{ + stbtt__buf_seek(b, 0); + return stbtt__buf_get16(b); +} + +static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i) +{ + int count, offsize, start, end; + stbtt__buf_seek(&b, 0); + count = stbtt__buf_get16(&b); + offsize = stbtt__buf_get8(&b); + STBTT_assert(i >= 0 && i < count); + STBTT_assert(offsize >= 1 && offsize <= 4); + stbtt__buf_skip(&b, i*offsize); + start = stbtt__buf_get(&b, offsize); + end = stbtt__buf_get(&b, offsize); + return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start); +} + +////////////////////////////////////////////////////////////////////////// +// +// accessors to parse data from file +// + +// on platforms that don't allow misaligned reads, if we want to allow +// truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE + +#define ttBYTE(p) (* (stbtt_uint8 *) (p)) +#define ttCHAR(p) (* (stbtt_int8 *) (p)) +#define ttFixed(p) ttLONG(p) + +static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; } +static stbtt_int16 ttSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; } +static stbtt_uint32 ttULONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } +static stbtt_int32 ttLONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; } + +#define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3)) +#define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3]) + +static int stbtt__isfont(stbtt_uint8 *font) +{ + // check the version number + if (stbtt_tag4(font, '1',0,0,0)) return 1; // TrueType 1 + if (stbtt_tag(font, "typ1")) return 1; // TrueType with type 1 font -- we don't support this! + if (stbtt_tag(font, "OTTO")) return 1; // OpenType with CFF + if (stbtt_tag4(font, 0,1,0,0)) return 1; // OpenType 1.0 + if (stbtt_tag(font, "true")) return 1; // Apple specification for TrueType fonts + return 0; +} + +// @OPTIMIZE: binary search +static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag) +{ + stbtt_int32 num_tables = ttUSHORT(data+fontstart+4); + stbtt_uint32 tabledir = fontstart + 12; + stbtt_int32 i; + for (i=0; i < num_tables; ++i) { + stbtt_uint32 loc = tabledir + 16*i; + if (stbtt_tag(data+loc+0, tag)) + return ttULONG(data+loc+8); + } + return 0; +} + +static int stbtt_GetFontOffsetForIndex_internal(unsigned char *font_collection, int index) +{ + // if it's just a font, there's only one valid index + if (stbtt__isfont(font_collection)) + return index == 0 ? 0 : -1; + + // check if it's a TTC + if (stbtt_tag(font_collection, "ttcf")) { + // version 1? + if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { + stbtt_int32 n = ttLONG(font_collection+8); + if (index >= n) + return -1; + return ttULONG(font_collection+12+index*4); + } + } + return -1; +} + +static int stbtt_GetNumberOfFonts_internal(unsigned char *font_collection) +{ + // if it's just a font, there's only one valid font + if (stbtt__isfont(font_collection)) + return 1; + + // check if it's a TTC + if (stbtt_tag(font_collection, "ttcf")) { + // version 1? + if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) { + return ttLONG(font_collection+8); + } + } + return 0; +} + +static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict) +{ + stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 }; + stbtt__buf pdict; + stbtt__dict_get_ints(&fontdict, 18, 2, private_loc); + if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0); + pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]); + stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff); + if (!subrsoff) return stbtt__new_buf(NULL, 0); + stbtt__buf_seek(&cff, private_loc[1]+subrsoff); + return stbtt__cff_get_index(&cff); +} + +static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart) +{ + stbtt_uint32 cmap, t; + stbtt_int32 i,numTables; + + info->data = data; + info->fontstart = fontstart; + info->cff = stbtt__new_buf(NULL, 0); + + cmap = stbtt__find_table(data, fontstart, "cmap"); // required + info->loca = stbtt__find_table(data, fontstart, "loca"); // required + info->head = stbtt__find_table(data, fontstart, "head"); // required + info->glyf = stbtt__find_table(data, fontstart, "glyf"); // required + info->hhea = stbtt__find_table(data, fontstart, "hhea"); // required + info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); // required + info->kern = stbtt__find_table(data, fontstart, "kern"); // not required + info->gpos = stbtt__find_table(data, fontstart, "GPOS"); // not required + + if (!cmap || !info->head || !info->hhea || !info->hmtx) + return 0; + if (info->glyf) { + // required for truetype + if (!info->loca) return 0; + } else { + // initialization for CFF / Type2 fonts (OTF) + stbtt__buf b, topdict, topdictidx; + stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0; + stbtt_uint32 cff; + + cff = stbtt__find_table(data, fontstart, "CFF "); + if (!cff) return 0; + + info->fontdicts = stbtt__new_buf(NULL, 0); + info->fdselect = stbtt__new_buf(NULL, 0); + + // @TODO this should use size from table (not 512MB) + info->cff = stbtt__new_buf(data+cff, 512*1024*1024); + b = info->cff; + + // read the header + stbtt__buf_skip(&b, 2); + stbtt__buf_seek(&b, stbtt__buf_get8(&b)); // hdrsize + + // @TODO the name INDEX could list multiple fonts, + // but we just use the first one. + stbtt__cff_get_index(&b); // name INDEX + topdictidx = stbtt__cff_get_index(&b); + topdict = stbtt__cff_index_get(topdictidx, 0); + stbtt__cff_get_index(&b); // string INDEX + info->gsubrs = stbtt__cff_get_index(&b); + + stbtt__dict_get_ints(&topdict, 17, 1, &charstrings); + stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype); + stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff); + stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff); + info->subrs = stbtt__get_subrs(b, topdict); + + // we only support Type 2 charstrings + if (cstype != 2) return 0; + if (charstrings == 0) return 0; + + if (fdarrayoff) { + // looks like a CID font + if (!fdselectoff) return 0; + stbtt__buf_seek(&b, fdarrayoff); + info->fontdicts = stbtt__cff_get_index(&b); + info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff); + } + + stbtt__buf_seek(&b, charstrings); + info->charstrings = stbtt__cff_get_index(&b); + } + + t = stbtt__find_table(data, fontstart, "maxp"); + if (t) + info->numGlyphs = ttUSHORT(data+t+4); + else + info->numGlyphs = 0xffff; + + // find a cmap encoding table we understand *now* to avoid searching + // later. (todo: could make this installable) + // the same regardless of glyph. + numTables = ttUSHORT(data + cmap + 2); + info->index_map = 0; + for (i=0; i < numTables; ++i) { + stbtt_uint32 encoding_record = cmap + 4 + 8 * i; + // find an encoding we understand: + switch(ttUSHORT(data+encoding_record)) { + case STBTT_PLATFORM_ID_MICROSOFT: + switch (ttUSHORT(data+encoding_record+2)) { + case STBTT_MS_EID_UNICODE_BMP: + case STBTT_MS_EID_UNICODE_FULL: + // MS/Unicode + info->index_map = cmap + ttULONG(data+encoding_record+4); + break; + } + break; + case STBTT_PLATFORM_ID_UNICODE: + // Mac/iOS has these + // all the encodingIDs are unicode, so we don't bother to check it + info->index_map = cmap + ttULONG(data+encoding_record+4); + break; + } + } + if (info->index_map == 0) + return 0; + + info->indexToLocFormat = ttUSHORT(data+info->head + 50); + return 1; +} + +STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint) +{ + stbtt_uint8 *data = info->data; + stbtt_uint32 index_map = info->index_map; + + stbtt_uint16 format = ttUSHORT(data + index_map + 0); + if (format == 0) { // apple byte encoding + stbtt_int32 bytes = ttUSHORT(data + index_map + 2); + if (unicode_codepoint < bytes-6) + return ttBYTE(data + index_map + 6 + unicode_codepoint); + return 0; + } else if (format == 6) { + stbtt_uint32 first = ttUSHORT(data + index_map + 6); + stbtt_uint32 count = ttUSHORT(data + index_map + 8); + if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count) + return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2); + return 0; + } else if (format == 2) { + STBTT_assert(0); // @TODO: high-byte mapping for japanese/chinese/korean + return 0; + } else if (format == 4) { // standard mapping for windows fonts: binary search collection of ranges + stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1; + stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1; + stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10); + stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1; + + // do a binary search of the segments + stbtt_uint32 endCount = index_map + 14; + stbtt_uint32 search = endCount; + + if (unicode_codepoint > 0xffff) + return 0; + + // they lie from endCount .. endCount + segCount + // but searchRange is the nearest power of two, so... + if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2)) + search += rangeShift*2; + + // now decrement to bias correctly to find smallest + search -= 2; + while (entrySelector) { + stbtt_uint16 end; + searchRange >>= 1; + end = ttUSHORT(data + search + searchRange*2); + if (unicode_codepoint > end) + search += searchRange*2; + --entrySelector; + } + search += 2; + + { + stbtt_uint16 offset, start; + stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1); + + STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item)); + start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item); + if (unicode_codepoint < start) + return 0; + + offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item); + if (offset == 0) + return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item)); + + return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item); + } + } else if (format == 12 || format == 13) { + stbtt_uint32 ngroups = ttULONG(data+index_map+12); + stbtt_int32 low,high; + low = 0; high = (stbtt_int32)ngroups; + // Binary search the right group. + while (low < high) { + stbtt_int32 mid = low + ((high-low) >> 1); // rounds down, so low <= mid < high + stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12); + stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4); + if ((stbtt_uint32) unicode_codepoint < start_char) + high = mid; + else if ((stbtt_uint32) unicode_codepoint > end_char) + low = mid+1; + else { + stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8); + if (format == 12) + return start_glyph + unicode_codepoint-start_char; + else // format == 13 + return start_glyph; + } + } + return 0; // not found + } + // @TODO + STBTT_assert(0); + return 0; +} + +STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices) +{ + return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices); +} + +static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy) +{ + v->type = type; + v->x = (stbtt_int16) x; + v->y = (stbtt_int16) y; + v->cx = (stbtt_int16) cx; + v->cy = (stbtt_int16) cy; +} + +static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index) +{ + int g1,g2; + + STBTT_assert(!info->cff.size); + + if (glyph_index >= info->numGlyphs) return -1; // glyph index out of range + if (info->indexToLocFormat >= 2) return -1; // unknown index->glyph map format + + if (info->indexToLocFormat == 0) { + g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2; + g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2; + } else { + g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4); + g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4); + } + + return g1==g2 ? -1 : g1; // if length is 0, return -1 +} + +static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); + +STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) +{ + if (info->cff.size) { + stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1); + } else { + int g = stbtt__GetGlyfOffset(info, glyph_index); + if (g < 0) return 0; + + if (x0) *x0 = ttSHORT(info->data + g + 2); + if (y0) *y0 = ttSHORT(info->data + g + 4); + if (x1) *x1 = ttSHORT(info->data + g + 6); + if (y1) *y1 = ttSHORT(info->data + g + 8); + } + return 1; +} + +STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1) +{ + return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1); +} + +STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index) +{ + stbtt_int16 numberOfContours; + int g; + if (info->cff.size) + return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0; + g = stbtt__GetGlyfOffset(info, glyph_index); + if (g < 0) return 1; + numberOfContours = ttSHORT(info->data + g); + return numberOfContours == 0; +} + +static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off, + stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy) +{ + if (start_off) { + if (was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy); + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy); + } else { + if (was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy); + else + stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0); + } + return num_vertices; +} + +static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) +{ + stbtt_int16 numberOfContours; + stbtt_uint8 *endPtsOfContours; + stbtt_uint8 *data = info->data; + stbtt_vertex *vertices=0; + int num_vertices=0; + int g = stbtt__GetGlyfOffset(info, glyph_index); + + *pvertices = NULL; + + if (g < 0) return 0; + + numberOfContours = ttSHORT(data + g); + + if (numberOfContours > 0) { + stbtt_uint8 flags=0,flagcount; + stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0; + stbtt_int32 x,y,cx,cy,sx,sy, scx,scy; + stbtt_uint8 *points; + endPtsOfContours = (data + g + 10); + ins = ttUSHORT(data + g + 10 + numberOfContours * 2); + points = data + g + 10 + numberOfContours * 2 + 2 + ins; + + n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2); + + m = n + 2*numberOfContours; // a loose bound on how many vertices we might need + vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata); + if (vertices == 0) + return 0; + + next_move = 0; + flagcount=0; + + // in first pass, we load uninterpreted data into the allocated array + // above, shifted to the end of the array so we won't overwrite it when + // we create our final data starting from the front + + off = m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated + + // first load flags + + for (i=0; i < n; ++i) { + if (flagcount == 0) { + flags = *points++; + if (flags & 8) + flagcount = *points++; + } else + --flagcount; + vertices[off+i].type = flags; + } + + // now load x coordinates + x=0; + for (i=0; i < n; ++i) { + flags = vertices[off+i].type; + if (flags & 2) { + stbtt_int16 dx = *points++; + x += (flags & 16) ? dx : -dx; // ??? + } else { + if (!(flags & 16)) { + x = x + (stbtt_int16) (points[0]*256 + points[1]); + points += 2; + } + } + vertices[off+i].x = (stbtt_int16) x; + } + + // now load y coordinates + y=0; + for (i=0; i < n; ++i) { + flags = vertices[off+i].type; + if (flags & 4) { + stbtt_int16 dy = *points++; + y += (flags & 32) ? dy : -dy; // ??? + } else { + if (!(flags & 32)) { + y = y + (stbtt_int16) (points[0]*256 + points[1]); + points += 2; + } + } + vertices[off+i].y = (stbtt_int16) y; + } + + // now convert them to our format + num_vertices=0; + sx = sy = cx = cy = scx = scy = 0; + for (i=0; i < n; ++i) { + flags = vertices[off+i].type; + x = (stbtt_int16) vertices[off+i].x; + y = (stbtt_int16) vertices[off+i].y; + + if (next_move == i) { + if (i != 0) + num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); + + // now start the new one + start_off = !(flags & 1); + if (start_off) { + // if we start off with an off-curve point, then when we need to find a point on the curve + // where we can start, and we need to save some state for when we wraparound. + scx = x; + scy = y; + if (!(vertices[off+i+1].type & 1)) { + // next point is also a curve point, so interpolate an on-point curve + sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1; + sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1; + } else { + // otherwise just use the next point as our start point + sx = (stbtt_int32) vertices[off+i+1].x; + sy = (stbtt_int32) vertices[off+i+1].y; + ++i; // we're using point i+1 as the starting point, so skip it + } + } else { + sx = x; + sy = y; + } + stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0); + was_off = 0; + next_move = 1 + ttUSHORT(endPtsOfContours+j*2); + ++j; + } else { + if (!(flags & 1)) { // if it's a curve + if (was_off) // two off-curve control points in a row means interpolate an on-curve midpoint + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy); + cx = x; + cy = y; + was_off = 1; + } else { + if (was_off) + stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy); + else + stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0); + was_off = 0; + } + } + } + num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); + } else if (numberOfContours == -1) { + // Compound shapes. + int more = 1; + stbtt_uint8 *comp = data + g + 10; + num_vertices = 0; + vertices = 0; + while (more) { + stbtt_uint16 flags, gidx; + int comp_num_verts = 0, i; + stbtt_vertex *comp_verts = 0, *tmp = 0; + float mtx[6] = {1,0,0,1,0,0}, m, n; + + flags = ttSHORT(comp); comp+=2; + gidx = ttSHORT(comp); comp+=2; + + if (flags & 2) { // XY values + if (flags & 1) { // shorts + mtx[4] = ttSHORT(comp); comp+=2; + mtx[5] = ttSHORT(comp); comp+=2; + } else { + mtx[4] = ttCHAR(comp); comp+=1; + mtx[5] = ttCHAR(comp); comp+=1; + } + } + else { + // @TODO handle matching point + STBTT_assert(0); + } + if (flags & (1<<3)) { // WE_HAVE_A_SCALE + mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; + mtx[1] = mtx[2] = 0; + } else if (flags & (1<<6)) { // WE_HAVE_AN_X_AND_YSCALE + mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; + mtx[1] = mtx[2] = 0; + mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; + } else if (flags & (1<<7)) { // WE_HAVE_A_TWO_BY_TWO + mtx[0] = ttSHORT(comp)/16384.0f; comp+=2; + mtx[1] = ttSHORT(comp)/16384.0f; comp+=2; + mtx[2] = ttSHORT(comp)/16384.0f; comp+=2; + mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; + } + + // Find transformation scales. + m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]); + n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]); + + // Get indexed glyph. + comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts); + if (comp_num_verts > 0) { + // Transform vertices. + for (i = 0; i < comp_num_verts; ++i) { + stbtt_vertex* v = &comp_verts[i]; + stbtt_vertex_type x,y; + x=v->x; y=v->y; + v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); + v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); + x=v->cx; y=v->cy; + v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4])); + v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5])); + } + // Append vertices. + tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata); + if (!tmp) { + if (vertices) STBTT_free(vertices, info->userdata); + if (comp_verts) STBTT_free(comp_verts, info->userdata); + return 0; + } + if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); //-V595 + STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex)); + if (vertices) STBTT_free(vertices, info->userdata); + vertices = tmp; + STBTT_free(comp_verts, info->userdata); + num_vertices += comp_num_verts; + } + // More components ? + more = flags & (1<<5); + } + } else if (numberOfContours < 0) { + // @TODO other compound variations? + STBTT_assert(0); + } else { + // numberOfCounters == 0, do nothing + } + + *pvertices = vertices; + return num_vertices; +} + +typedef struct +{ + int bounds; + int started; + float first_x, first_y; + float x, y; + stbtt_int32 min_x, max_x, min_y, max_y; + + stbtt_vertex *pvertices; + int num_vertices; +} stbtt__csctx; + +#define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0} + +static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y) +{ + if (x > c->max_x || !c->started) c->max_x = x; + if (y > c->max_y || !c->started) c->max_y = y; + if (x < c->min_x || !c->started) c->min_x = x; + if (y < c->min_y || !c->started) c->min_y = y; + c->started = 1; +} + +static void stbtt__csctx_v(stbtt__csctx *c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1) +{ + if (c->bounds) { + stbtt__track_vertex(c, x, y); + if (type == STBTT_vcubic) { + stbtt__track_vertex(c, cx, cy); + stbtt__track_vertex(c, cx1, cy1); + } + } else { + stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy); + c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1; + c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1; + } + c->num_vertices++; +} + +static void stbtt__csctx_close_shape(stbtt__csctx *ctx) +{ + if (ctx->first_x != ctx->x || ctx->first_y != ctx->y) + stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy) +{ + stbtt__csctx_close_shape(ctx); + ctx->first_x = ctx->x = ctx->x + dx; + ctx->first_y = ctx->y = ctx->y + dy; + stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy) +{ + ctx->x += dx; + ctx->y += dy; + stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0); +} + +static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3) +{ + float cx1 = ctx->x + dx1; + float cy1 = ctx->y + dy1; + float cx2 = cx1 + dx2; + float cy2 = cy1 + dy2; + ctx->x = cx2 + dx3; + ctx->y = cy2 + dy3; + stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2); +} + +static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n) +{ + int count = stbtt__cff_index_count(&idx); + int bias = 107; + if (count >= 33900) + bias = 32768; + else if (count >= 1240) + bias = 1131; + n += bias; + if (n < 0 || n >= count) + return stbtt__new_buf(NULL, 0); + return stbtt__cff_index_get(idx, n); +} + +static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index) +{ + stbtt__buf fdselect = info->fdselect; + int nranges, start, end, v, fmt, fdselector = -1, i; + + stbtt__buf_seek(&fdselect, 0); + fmt = stbtt__buf_get8(&fdselect); + if (fmt == 0) { + // untested + stbtt__buf_skip(&fdselect, glyph_index); + fdselector = stbtt__buf_get8(&fdselect); + } else if (fmt == 3) { + nranges = stbtt__buf_get16(&fdselect); + start = stbtt__buf_get16(&fdselect); + for (i = 0; i < nranges; i++) { + v = stbtt__buf_get8(&fdselect); + end = stbtt__buf_get16(&fdselect); + if (glyph_index >= start && glyph_index < end) { + fdselector = v; + break; + } + start = end; + } + } + if (fdselector == -1) stbtt__new_buf(NULL, 0); + return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector)); +} + +static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c) +{ + int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0; + int has_subrs = 0, clear_stack; + float s[48]; + stbtt__buf subr_stack[10], subrs = info->subrs, b; + float f; + +#define STBTT__CSERR(s) (0) + + // this currently ignores the initial width value, which isn't needed if we have hmtx + b = stbtt__cff_index_get(info->charstrings, glyph_index); + while (b.cursor < b.size) { + i = 0; + clear_stack = 1; + b0 = stbtt__buf_get8(&b); + switch (b0) { + // @TODO implement hinting + case 0x13: // hintmask + case 0x14: // cntrmask + if (in_header) + maskbits += (sp / 2); // implicit "vstem" + in_header = 0; + stbtt__buf_skip(&b, (maskbits + 7) / 8); + break; + + case 0x01: // hstem + case 0x03: // vstem + case 0x12: // hstemhm + case 0x17: // vstemhm + maskbits += (sp / 2); + break; + + case 0x15: // rmoveto + in_header = 0; + if (sp < 2) return STBTT__CSERR("rmoveto stack"); + stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]); + break; + case 0x04: // vmoveto + in_header = 0; + if (sp < 1) return STBTT__CSERR("vmoveto stack"); + stbtt__csctx_rmove_to(c, 0, s[sp-1]); + break; + case 0x16: // hmoveto + in_header = 0; + if (sp < 1) return STBTT__CSERR("hmoveto stack"); + stbtt__csctx_rmove_to(c, s[sp-1], 0); + break; + + case 0x05: // rlineto + if (sp < 2) return STBTT__CSERR("rlineto stack"); + for (; i + 1 < sp; i += 2) + stbtt__csctx_rline_to(c, s[i], s[i+1]); + break; + + // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical + // starting from a different place. + + case 0x07: // vlineto + if (sp < 1) return STBTT__CSERR("vlineto stack"); + goto vlineto; + case 0x06: // hlineto + if (sp < 1) return STBTT__CSERR("hlineto stack"); + for (;;) { + if (i >= sp) break; + stbtt__csctx_rline_to(c, s[i], 0); + i++; + vlineto: + if (i >= sp) break; + stbtt__csctx_rline_to(c, 0, s[i]); + i++; + } + break; + + case 0x1F: // hvcurveto + if (sp < 4) return STBTT__CSERR("hvcurveto stack"); + goto hvcurveto; + case 0x1E: // vhcurveto + if (sp < 4) return STBTT__CSERR("vhcurveto stack"); + for (;;) { + if (i + 3 >= sp) break; + stbtt__csctx_rccurve_to(c, 0, s[i], s[i+1], s[i+2], s[i+3], (sp - i == 5) ? s[i + 4] : 0.0f); + i += 4; + hvcurveto: + if (i + 3 >= sp) break; + stbtt__csctx_rccurve_to(c, s[i], 0, s[i+1], s[i+2], (sp - i == 5) ? s[i+4] : 0.0f, s[i+3]); + i += 4; + } + break; + + case 0x08: // rrcurveto + if (sp < 6) return STBTT__CSERR("rcurveline stack"); + for (; i + 5 < sp; i += 6) + stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); + break; + + case 0x18: // rcurveline + if (sp < 8) return STBTT__CSERR("rcurveline stack"); + for (; i + 5 < sp - 2; i += 6) + stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); + if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack"); + stbtt__csctx_rline_to(c, s[i], s[i+1]); + break; + + case 0x19: // rlinecurve + if (sp < 8) return STBTT__CSERR("rlinecurve stack"); + for (; i + 1 < sp - 6; i += 2) + stbtt__csctx_rline_to(c, s[i], s[i+1]); + if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack"); + stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]); + break; + + case 0x1A: // vvcurveto + case 0x1B: // hhcurveto + if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack"); + f = 0.0; + if (sp & 1) { f = s[i]; i++; } + for (; i + 3 < sp; i += 4) { + if (b0 == 0x1B) + stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0); + else + stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]); + f = 0.0; + } + break; + + case 0x0A: // callsubr + if (!has_subrs) { + if (info->fdselect.size) + subrs = stbtt__cid_get_glyph_subrs(info, glyph_index); + has_subrs = 1; + } + // fallthrough + case 0x1D: // callgsubr + if (sp < 1) return STBTT__CSERR("call(g|)subr stack"); + v = (int) s[--sp]; + if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit"); + subr_stack[subr_stack_height++] = b; + b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v); + if (b.size == 0) return STBTT__CSERR("subr not found"); + b.cursor = 0; + clear_stack = 0; + break; + + case 0x0B: // return + if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr"); + b = subr_stack[--subr_stack_height]; + clear_stack = 0; + break; + + case 0x0E: // endchar + stbtt__csctx_close_shape(c); + return 1; + + case 0x0C: { // two-byte escape + float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6; + float dx, dy; + int b1 = stbtt__buf_get8(&b); + switch (b1) { + // @TODO These "flex" implementations ignore the flex-depth and resolution, + // and always draw beziers. + case 0x22: // hflex + if (sp < 7) return STBTT__CSERR("hflex stack"); + dx1 = s[0]; + dx2 = s[1]; + dy2 = s[2]; + dx3 = s[3]; + dx4 = s[4]; + dx5 = s[5]; + dx6 = s[6]; + stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0); + stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0); + break; + + case 0x23: // flex + if (sp < 13) return STBTT__CSERR("flex stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dy3 = s[5]; + dx4 = s[6]; + dy4 = s[7]; + dx5 = s[8]; + dy5 = s[9]; + dx6 = s[10]; + dy6 = s[11]; + //fd is s[12] + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); + stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); + break; + + case 0x24: // hflex1 + if (sp < 9) return STBTT__CSERR("hflex1 stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dx4 = s[5]; + dx5 = s[6]; + dy5 = s[7]; + dx6 = s[8]; + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0); + stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5)); + break; + + case 0x25: // flex1 + if (sp < 11) return STBTT__CSERR("flex1 stack"); + dx1 = s[0]; + dy1 = s[1]; + dx2 = s[2]; + dy2 = s[3]; + dx3 = s[4]; + dy3 = s[5]; + dx4 = s[6]; + dy4 = s[7]; + dx5 = s[8]; + dy5 = s[9]; + dx6 = dy6 = s[10]; + dx = dx1+dx2+dx3+dx4+dx5; + dy = dy1+dy2+dy3+dy4+dy5; + if (STBTT_fabs(dx) > STBTT_fabs(dy)) + dy6 = -dy; + else + dx6 = -dx; + stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3); + stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6); + break; + + default: + return STBTT__CSERR("unimplemented"); + } + } break; + + default: + if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) //-V560 + return STBTT__CSERR("reserved operator"); + + // push immediate + if (b0 == 255) { + f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000; + } else { + stbtt__buf_skip(&b, -1); + f = (float)(stbtt_int16)stbtt__cff_int(&b); + } + if (sp >= 48) return STBTT__CSERR("push stack overflow"); + s[sp++] = f; + clear_stack = 0; + break; + } + if (clear_stack) sp = 0; + } + return STBTT__CSERR("no endchar"); + +#undef STBTT__CSERR +} + +static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) +{ + // runs the charstring twice, once to count and once to output (to avoid realloc) + stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1); + stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0); + if (stbtt__run_charstring(info, glyph_index, &count_ctx)) { + *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata); + output_ctx.pvertices = *pvertices; + if (stbtt__run_charstring(info, glyph_index, &output_ctx)) { + STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices); + return output_ctx.num_vertices; + } + } + *pvertices = NULL; + return 0; +} + +static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1) +{ + stbtt__csctx c = STBTT__CSCTX_INIT(1); + int r = stbtt__run_charstring(info, glyph_index, &c); + if (x0) *x0 = r ? c.min_x : 0; + if (y0) *y0 = r ? c.min_y : 0; + if (x1) *x1 = r ? c.max_x : 0; + if (y1) *y1 = r ? c.max_y : 0; + return r ? c.num_vertices : 0; +} + +STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices) +{ + if (!info->cff.size) + return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices); + else + return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices); +} + +STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing) +{ + stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34); + if (glyph_index < numOfLongHorMetrics) { + if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index); + if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2); + } else { + if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1)); + if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics)); + } +} + +static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) +{ + stbtt_uint8 *data = info->data + info->kern; + stbtt_uint32 needle, straw; + int l, r, m; + + // we only look at the first table. it must be 'horizontal' and format 0. + if (!info->kern) + return 0; + if (ttUSHORT(data+2) < 1) // number of tables, need at least 1 + return 0; + if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format + return 0; + + l = 0; + r = ttUSHORT(data+10) - 1; + needle = glyph1 << 16 | glyph2; + while (l <= r) { + m = (l + r) >> 1; + straw = ttULONG(data+18+(m*6)); // note: unaligned read + if (needle < straw) + r = m - 1; + else if (needle > straw) + l = m + 1; + else + return ttSHORT(data+22+(m*6)); + } + return 0; +} + +static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph) +{ + stbtt_uint16 coverageFormat = ttUSHORT(coverageTable); + switch(coverageFormat) { + case 1: { + stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2); + + // Binary search. + stbtt_int32 l=0, r=glyphCount-1, m; + int straw, needle=glyph; + while (l <= r) { + stbtt_uint8 *glyphArray = coverageTable + 4; + stbtt_uint16 glyphID; + m = (l + r) >> 1; + glyphID = ttUSHORT(glyphArray + 2 * m); + straw = glyphID; + if (needle < straw) + r = m - 1; + else if (needle > straw) + l = m + 1; + else { + return m; + } + } + } break; + + case 2: { + stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2); + stbtt_uint8 *rangeArray = coverageTable + 4; + + // Binary search. + stbtt_int32 l=0, r=rangeCount-1, m; + int strawStart, strawEnd, needle=glyph; + while (l <= r) { + stbtt_uint8 *rangeRecord; + m = (l + r) >> 1; + rangeRecord = rangeArray + 6 * m; + strawStart = ttUSHORT(rangeRecord); + strawEnd = ttUSHORT(rangeRecord + 2); + if (needle < strawStart) + r = m - 1; + else if (needle > strawEnd) + l = m + 1; + else { + stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4); + return startCoverageIndex + glyph - strawStart; + } + } + } break; + + default: { + // There are no other cases. + STBTT_assert(0); + } break; + } + + return -1; +} + +static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable, int glyph) +{ + stbtt_uint16 classDefFormat = ttUSHORT(classDefTable); + switch(classDefFormat) + { + case 1: { + stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2); + stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4); + stbtt_uint8 *classDef1ValueArray = classDefTable + 6; + + if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount) + return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID)); + + // [DEAR IMGUI] Commented to fix static analyzer warning + //classDefTable = classDef1ValueArray + 2 * glyphCount; + } break; + + case 2: { + stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2); + stbtt_uint8 *classRangeRecords = classDefTable + 4; + + // Binary search. + stbtt_int32 l=0, r=classRangeCount-1, m; + int strawStart, strawEnd, needle=glyph; + while (l <= r) { + stbtt_uint8 *classRangeRecord; + m = (l + r) >> 1; + classRangeRecord = classRangeRecords + 6 * m; + strawStart = ttUSHORT(classRangeRecord); + strawEnd = ttUSHORT(classRangeRecord + 2); + if (needle < strawStart) + r = m - 1; + else if (needle > strawEnd) + l = m + 1; + else + return (stbtt_int32)ttUSHORT(classRangeRecord + 4); + } + + // [DEAR IMGUI] Commented to fix static analyzer warning + //classDefTable = classRangeRecords + 6 * classRangeCount; + } break; + + default: { + // There are no other cases. + STBTT_assert(0); + } break; + } + + return -1; +} + +// Define to STBTT_assert(x) if you want to break on unimplemented formats. +#define STBTT_GPOS_TODO_assert(x) + +static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) +{ + stbtt_uint16 lookupListOffset; + stbtt_uint8 *lookupList; + stbtt_uint16 lookupCount; + stbtt_uint8 *data; + stbtt_int32 i; + + if (!info->gpos) return 0; + + data = info->data + info->gpos; + + if (ttUSHORT(data+0) != 1) return 0; // Major version 1 + if (ttUSHORT(data+2) != 0) return 0; // Minor version 0 + + lookupListOffset = ttUSHORT(data+8); + lookupList = data + lookupListOffset; + lookupCount = ttUSHORT(lookupList); + + for (i=0; i> 1; + pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m; + secondGlyph = ttUSHORT(pairValue); + straw = secondGlyph; + if (needle < straw) + r = m - 1; + else if (needle > straw) + l = m + 1; + else { + stbtt_int16 xAdvance = ttSHORT(pairValue + 2); + return xAdvance; + } + } + } break; + + case 2: { + stbtt_uint16 valueFormat1 = ttUSHORT(table + 4); + stbtt_uint16 valueFormat2 = ttUSHORT(table + 6); + + stbtt_uint16 classDef1Offset = ttUSHORT(table + 8); + stbtt_uint16 classDef2Offset = ttUSHORT(table + 10); + int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1); + int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2); + + stbtt_uint16 class1Count = ttUSHORT(table + 12); + stbtt_uint16 class2Count = ttUSHORT(table + 14); + STBTT_assert(glyph1class < class1Count); + STBTT_assert(glyph2class < class2Count); + + // TODO: Support more formats. + STBTT_GPOS_TODO_assert(valueFormat1 == 4); + if (valueFormat1 != 4) return 0; + STBTT_GPOS_TODO_assert(valueFormat2 == 0); + if (valueFormat2 != 0) return 0; + + if (glyph1class >= 0 && glyph1class < class1Count && glyph2class >= 0 && glyph2class < class2Count) { + stbtt_uint8 *class1Records = table + 16; + stbtt_uint8 *class2Records = class1Records + 2 * (glyph1class * class2Count); + stbtt_int16 xAdvance = ttSHORT(class2Records + 2 * glyph2class); + return xAdvance; + } + } break; + + default: { + // There are no other cases. + STBTT_assert(0); + break; + }; + } + } + break; + }; + + default: + // TODO: Implement other stuff. + break; + } + } + + return 0; +} + +STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int g2) +{ + int xAdvance = 0; + + if (info->gpos) + xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2); + + if (info->kern) + xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2); + + return xAdvance; +} + +STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2) +{ + if (!info->kern && !info->gpos) // if no kerning table, don't waste time looking up both codepoint->glyphs + return 0; + return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2)); +} + +STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing) +{ + stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing); +} + +STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap) +{ + if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4); + if (descent) *descent = ttSHORT(info->data+info->hhea + 6); + if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8); +} + +STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap) +{ + int tab = stbtt__find_table(info->data, info->fontstart, "OS/2"); + if (!tab) + return 0; + if (typoAscent ) *typoAscent = ttSHORT(info->data+tab + 68); + if (typoDescent) *typoDescent = ttSHORT(info->data+tab + 70); + if (typoLineGap) *typoLineGap = ttSHORT(info->data+tab + 72); + return 1; +} + +STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1) +{ + *x0 = ttSHORT(info->data + info->head + 36); + *y0 = ttSHORT(info->data + info->head + 38); + *x1 = ttSHORT(info->data + info->head + 40); + *y1 = ttSHORT(info->data + info->head + 42); +} + +STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height) +{ + int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6); + return (float) height / fheight; +} + +STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels) +{ + int unitsPerEm = ttUSHORT(info->data + info->head + 18); + return pixels / unitsPerEm; +} + +STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v) +{ + STBTT_free(v, info->userdata); +} + +////////////////////////////////////////////////////////////////////////////// +// +// antialiasing software rasterizer +// + +STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y,float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1) +{ + int x0=0,y0=0,x1,y1; // =0 suppresses compiler warning + if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) { + // e.g. space character + if (ix0) *ix0 = 0; + if (iy0) *iy0 = 0; + if (ix1) *ix1 = 0; + if (iy1) *iy1 = 0; + } else { + // move to integral bboxes (treating pixels as little squares, what pixels get touched)? + if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x); + if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y); + if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x); + if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y); + } +} + +STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo *font, int glyph, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) +{ + stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1); +} + +STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int *ix0, int *iy0, int *ix1, int *iy1) +{ + stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1); +} + +STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo *font, int codepoint, float scale_x, float scale_y, int *ix0, int *iy0, int *ix1, int *iy1) +{ + stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1); +} + +////////////////////////////////////////////////////////////////////////////// +// +// Rasterizer + +typedef struct stbtt__hheap_chunk +{ + struct stbtt__hheap_chunk *next; +} stbtt__hheap_chunk; + +typedef struct stbtt__hheap +{ + struct stbtt__hheap_chunk *head; + void *first_free; + int num_remaining_in_head_chunk; +} stbtt__hheap; + +static void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata) +{ + if (hh->first_free) { + void *p = hh->first_free; + hh->first_free = * (void **) p; + return p; + } else { + if (hh->num_remaining_in_head_chunk == 0) { + int count = (size < 32 ? 2000 : size < 128 ? 800 : 100); + stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata); + if (c == NULL) + return NULL; + c->next = hh->head; + hh->head = c; + hh->num_remaining_in_head_chunk = count; + } + --hh->num_remaining_in_head_chunk; + return (char *) (hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk; + } +} + +static void stbtt__hheap_free(stbtt__hheap *hh, void *p) +{ + *(void **) p = hh->first_free; + hh->first_free = p; +} + +static void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata) +{ + stbtt__hheap_chunk *c = hh->head; + while (c) { + stbtt__hheap_chunk *n = c->next; + STBTT_free(c, userdata); + c = n; + } +} + +typedef struct stbtt__edge { + float x0,y0, x1,y1; + int invert; +} stbtt__edge; + + +typedef struct stbtt__active_edge +{ + struct stbtt__active_edge *next; + #if STBTT_RASTERIZER_VERSION==1 + int x,dx; + float ey; + int direction; + #elif STBTT_RASTERIZER_VERSION==2 + float fx,fdx,fdy; + float direction; + float sy; + float ey; + #else + #error "Unrecognized value of STBTT_RASTERIZER_VERSION" + #endif +} stbtt__active_edge; + +#if STBTT_RASTERIZER_VERSION == 1 +#define STBTT_FIXSHIFT 10 +#define STBTT_FIX (1 << STBTT_FIXSHIFT) +#define STBTT_FIXMASK (STBTT_FIX-1) + +static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) +{ + stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); + float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); + STBTT_assert(z != NULL); + if (!z) return z; + + // round dx down to avoid overshooting + if (dxdy < 0) + z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy); + else + z->dx = STBTT_ifloor(STBTT_FIX * dxdy); + + z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0)); // use z->dx so when we offset later it's by the same amount + z->x -= off_x * STBTT_FIX; + + z->ey = e->y1; + z->next = 0; + z->direction = e->invert ? 1 : -1; + return z; +} +#elif STBTT_RASTERIZER_VERSION == 2 +static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata) +{ + stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata); + float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); + STBTT_assert(z != NULL); + //STBTT_assert(e->y0 <= start_point); + if (!z) return z; + z->fdx = dxdy; + z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f; + z->fx = e->x0 + dxdy * (start_point - e->y0); + z->fx -= off_x; + z->direction = e->invert ? 1.0f : -1.0f; + z->sy = e->y0; + z->ey = e->y1; + z->next = 0; + return z; +} +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + +#if STBTT_RASTERIZER_VERSION == 1 +// note: this routine clips fills that extend off the edges... ideally this +// wouldn't happen, but it could happen if the truetype glyph bounding boxes +// are wrong, or if the user supplies a too-small bitmap +static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight) +{ + // non-zero winding fill + int x0=0, w=0; + + while (e) { + if (w == 0) { + // if we're currently at zero, we need to record the edge start point + x0 = e->x; w += e->direction; + } else { + int x1 = e->x; w += e->direction; + // if we went to zero, we need to draw + if (w == 0) { + int i = x0 >> STBTT_FIXSHIFT; + int j = x1 >> STBTT_FIXSHIFT; + + if (i < len && j >= 0) { + if (i == j) { + // x0,x1 are the same pixel, so compute combined coverage + scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT); + } else { + if (i >= 0) // add antialiasing for x0 + scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT); + else + i = -1; // clip + + if (j < len) // add antialiasing for x1 + scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT); + else + j = len; // clip + + for (++i; i < j; ++i) // fill pixels between x0 and x1 + scanline[i] = scanline[i] + (stbtt_uint8) max_weight; + } + } + } + } + + e = e->next; + } +} + +static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata) +{ + stbtt__hheap hh = { 0, 0, 0 }; + stbtt__active_edge *active = NULL; + int y,j=0; + int max_weight = (255 / vsubsample); // weight per vertical scanline + int s; // vertical subsample index + unsigned char scanline_data[512], *scanline; + + if (result->w > 512) + scanline = (unsigned char *) STBTT_malloc(result->w, userdata); + else + scanline = scanline_data; + + y = off_y * vsubsample; + e[n].y0 = (off_y + result->h) * (float) vsubsample + 1; + + while (j < result->h) { + STBTT_memset(scanline, 0, result->w); + for (s=0; s < vsubsample; ++s) { + // find center of pixel for this scanline + float scan_y = y + 0.5f; + stbtt__active_edge **step = &active; + + // update all active edges; + // remove all active edges that terminate before the center of this scanline + while (*step) { + stbtt__active_edge * z = *step; + if (z->ey <= scan_y) { + *step = z->next; // delete from list + STBTT_assert(z->direction); + z->direction = 0; + stbtt__hheap_free(&hh, z); + } else { + z->x += z->dx; // advance to position for current scanline + step = &((*step)->next); // advance through list + } + } + + // resort the list if needed + for(;;) { + int changed=0; + step = &active; + while (*step && (*step)->next) { + if ((*step)->x > (*step)->next->x) { + stbtt__active_edge *t = *step; + stbtt__active_edge *q = t->next; + + t->next = q->next; + q->next = t; + *step = q; + changed = 1; + } + step = &(*step)->next; + } + if (!changed) break; + } + + // insert all edges that start before the center of this scanline -- omit ones that also end on this scanline + while (e->y0 <= scan_y) { + if (e->y1 > scan_y) { + stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata); + if (z != NULL) { + // find insertion point + if (active == NULL) + active = z; + else if (z->x < active->x) { + // insert at front + z->next = active; + active = z; + } else { + // find thing to insert AFTER + stbtt__active_edge *p = active; + while (p->next && p->next->x < z->x) + p = p->next; + // at this point, p->next->x is NOT < z->x + z->next = p->next; + p->next = z; + } + } + } + ++e; + } + + // now process all active edges in XOR fashion + if (active) + stbtt__fill_active_edges(scanline, result->w, active, max_weight); + + ++y; + } + STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w); + ++j; + } + + stbtt__hheap_cleanup(&hh, userdata); + + if (scanline != scanline_data) + STBTT_free(scanline, userdata); +} + +#elif STBTT_RASTERIZER_VERSION == 2 + +// the edge passed in here does not cross the vertical line at x or the vertical line at x+1 +// (i.e. it has already been clipped to those) +static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1) +{ + if (y0 == y1) return; + STBTT_assert(y0 < y1); + STBTT_assert(e->sy <= e->ey); + if (y0 > e->ey) return; + if (y1 < e->sy) return; + if (y0 < e->sy) { + x0 += (x1-x0) * (e->sy - y0) / (y1-y0); + y0 = e->sy; + } + if (y1 > e->ey) { + x1 += (x1-x0) * (e->ey - y1) / (y1-y0); + y1 = e->ey; + } + + if (x0 == x) + STBTT_assert(x1 <= x+1); + else if (x0 == x+1) + STBTT_assert(x1 >= x); + else if (x0 <= x) + STBTT_assert(x1 <= x); + else if (x0 >= x+1) + STBTT_assert(x1 >= x+1); + else + STBTT_assert(x1 >= x && x1 <= x+1); + + if (x0 <= x && x1 <= x) + scanline[x] += e->direction * (y1-y0); + else if (x0 >= x+1 && x1 >= x+1) + ; + else { + STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1); + scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); // coverage = 1 - average x position + } +} + +static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top) +{ + float y_bottom = y_top+1; + + while (e) { + // brute force every pixel + + // compute intersection points with top & bottom + STBTT_assert(e->ey >= y_top); + + if (e->fdx == 0) { + float x0 = e->fx; + if (x0 < len) { + if (x0 >= 0) { + stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom); + stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom); + } else { + stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom); + } + } + } else { + float x0 = e->fx; + float dx = e->fdx; + float xb = x0 + dx; + float x_top, x_bottom; + float sy0,sy1; + float dy = e->fdy; + STBTT_assert(e->sy <= y_bottom && e->ey >= y_top); + + // compute endpoints of line segment clipped to this scanline (if the + // line segment starts on this scanline. x0 is the intersection of the + // line with y_top, but that may be off the line segment. + if (e->sy > y_top) { + x_top = x0 + dx * (e->sy - y_top); + sy0 = e->sy; + } else { + x_top = x0; + sy0 = y_top; + } + if (e->ey < y_bottom) { + x_bottom = x0 + dx * (e->ey - y_top); + sy1 = e->ey; + } else { + x_bottom = xb; + sy1 = y_bottom; + } + + if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) { + // from here on, we don't have to range check x values + + if ((int) x_top == (int) x_bottom) { + float height; + // simple case, only spans one pixel + int x = (int) x_top; + height = sy1 - sy0; + STBTT_assert(x >= 0 && x < len); + scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height; + scanline_fill[x] += e->direction * height; // everything right of this pixel is filled + } else { + int x,x1,x2; + float y_crossing, step, sign, area; + // covers 2+ pixels + if (x_top > x_bottom) { + // flip scanline vertically; signed area is the same + float t; + sy0 = y_bottom - (sy0 - y_top); + sy1 = y_bottom - (sy1 - y_top); + t = sy0, sy0 = sy1, sy1 = t; + t = x_bottom, x_bottom = x_top, x_top = t; + dx = -dx; + dy = -dy; + t = x0, x0 = xb, xb = t; + // [DEAR IMGUI] Fix static analyzer warning + (void)dx; // [ImGui: fix static analyzer warning] + } + + x1 = (int) x_top; + x2 = (int) x_bottom; + // compute intersection with y axis at x1+1 + y_crossing = (x1+1 - x0) * dy + y_top; + + sign = e->direction; + // area of the rectangle covered from y0..y_crossing + area = sign * (y_crossing-sy0); + // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) + scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); + + step = sign * dy; + for (x = x1+1; x < x2; ++x) { + scanline[x] += area + step/2; + area += step; + } + y_crossing += dy * (x2 - (x1+1)); + + STBTT_assert(STBTT_fabs(area) <= 1.01f); + + scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); + + scanline_fill[x2] += sign * (sy1-sy0); + } + } else { + // if edge goes outside of box we're drawing, we require + // clipping logic. since this does not match the intended use + // of this library, we use a different, very slow brute + // force implementation + int x; + for (x=0; x < len; ++x) { + // cases: + // + // there can be up to two intersections with the pixel. any intersection + // with left or right edges can be handled by splitting into two (or three) + // regions. intersections with top & bottom do not necessitate case-wise logic. + // + // the old way of doing this found the intersections with the left & right edges, + // then used some simple logic to produce up to three segments in sorted order + // from top-to-bottom. however, this had a problem: if an x edge was epsilon + // across the x border, then the corresponding y position might not be distinct + // from the other y segment, and it might ignored as an empty segment. to avoid + // that, we need to explicitly produce segments based on x positions. + + // rename variables to clearly-defined pairs + float y0 = y_top; + float x1 = (float) (x); + float x2 = (float) (x+1); + float x3 = xb; + float y3 = y_bottom; + + // x = e->x + e->dx * (y-y_top) + // (y-y_top) = (x - e->x) / e->dx + // y = (x - e->x) / e->dx + y_top + float y1 = (x - x0) / dx + y_top; + float y2 = (x+1 - x0) / dx + y_top; + + if (x0 < x1 && x3 > x2) { // three segments descending down-right + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2); + stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else if (x3 < x1 && x0 > x2) { // three segments descending down-left + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1); + stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x0 < x1 && x3 > x1) { // two segments across x, down-right + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x3 < x1 && x0 > x1) { // two segments across x, down-left + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1); + stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3); + } else if (x0 < x2 && x3 > x2) { // two segments across x+1, down-right + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else if (x3 < x2 && x0 > x2) { // two segments across x+1, down-left + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2); + stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3); + } else { // one segment + stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3); + } + } + } + } + e = e->next; + } +} + +// directly AA rasterize edges w/o supersampling +static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata) +{ + stbtt__hheap hh = { 0, 0, 0 }; + stbtt__active_edge *active = NULL; + int y,j=0, i; + float scanline_data[129], *scanline, *scanline2; + + STBTT__NOTUSED(vsubsample); + + if (result->w > 64) + scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata); + else + scanline = scanline_data; + + scanline2 = scanline + result->w; + + y = off_y; + e[n].y0 = (float) (off_y + result->h) + 1; + + while (j < result->h) { + // find center of pixel for this scanline + float scan_y_top = y + 0.0f; + float scan_y_bottom = y + 1.0f; + stbtt__active_edge **step = &active; + + STBTT_memset(scanline , 0, result->w*sizeof(scanline[0])); + STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0])); + + // update all active edges; + // remove all active edges that terminate before the top of this scanline + while (*step) { + stbtt__active_edge * z = *step; + if (z->ey <= scan_y_top) { + *step = z->next; // delete from list + STBTT_assert(z->direction); + z->direction = 0; + stbtt__hheap_free(&hh, z); + } else { + step = &((*step)->next); // advance through list + } + } + + // insert all edges that start before the bottom of this scanline + while (e->y0 <= scan_y_bottom) { + if (e->y0 != e->y1) { + stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata); + if (z != NULL) { + if (j == 0 && off_y != 0) { + if (z->ey < scan_y_top) { + // this can happen due to subpixel positioning and some kind of fp rounding error i think + z->ey = scan_y_top; + } + } + STBTT_assert(z->ey >= scan_y_top); // if we get really unlucky a tiny bit of an edge can be out of bounds + // insert at front + z->next = active; + active = z; + } + } + ++e; + } + + // now process all active edges + if (active) + stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top); + + { + float sum = 0; + for (i=0; i < result->w; ++i) { + float k; + int m; + sum += scanline2[i]; + k = scanline[i] + sum; + k = (float) STBTT_fabs(k)*255 + 0.5f; + m = (int) k; + if (m > 255) m = 255; + result->pixels[j*result->stride + i] = (unsigned char) m; + } + } + // advance all the edges + step = &active; + while (*step) { + stbtt__active_edge *z = *step; + z->fx += z->fdx; // advance to position for current scanline + step = &((*step)->next); // advance through list + } + + ++y; + ++j; + } + + stbtt__hheap_cleanup(&hh, userdata); + + if (scanline != scanline_data) + STBTT_free(scanline, userdata); +} +#else +#error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + +#define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0) + +static void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n) +{ + int i,j; + for (i=1; i < n; ++i) { + stbtt__edge t = p[i], *a = &t; + j = i; + while (j > 0) { + stbtt__edge *b = &p[j-1]; + int c = STBTT__COMPARE(a,b); + if (!c) break; + p[j] = p[j-1]; + --j; + } + if (i != j) + p[j] = t; + } +} + +static void stbtt__sort_edges_quicksort(stbtt__edge *p, int n) +{ + /* threshold for transitioning to insertion sort */ + while (n > 12) { + stbtt__edge t; + int c01,c12,c,m,i,j; + + /* compute median of three */ + m = n >> 1; + c01 = STBTT__COMPARE(&p[0],&p[m]); + c12 = STBTT__COMPARE(&p[m],&p[n-1]); + /* if 0 >= mid >= end, or 0 < mid < end, then use mid */ + if (c01 != c12) { + /* otherwise, we'll need to swap something else to middle */ + int z; + c = STBTT__COMPARE(&p[0],&p[n-1]); + /* 0>mid && midn => n; 0 0 */ + /* 0n: 0>n => 0; 0 n */ + z = (c == c12) ? 0 : n-1; + t = p[z]; + p[z] = p[m]; + p[m] = t; + } + /* now p[m] is the median-of-three */ + /* swap it to the beginning so it won't move around */ + t = p[0]; + p[0] = p[m]; + p[m] = t; + + /* partition loop */ + i=1; + j=n-1; + for(;;) { + /* handling of equality is crucial here */ + /* for sentinels & efficiency with duplicates */ + for (;;++i) { + if (!STBTT__COMPARE(&p[i], &p[0])) break; + } + for (;;--j) { + if (!STBTT__COMPARE(&p[0], &p[j])) break; + } + /* make sure we haven't crossed */ + if (i >= j) break; + t = p[i]; + p[i] = p[j]; + p[j] = t; + + ++i; + --j; + } + /* recurse on smaller side, iterate on larger */ + if (j < (n-i)) { + stbtt__sort_edges_quicksort(p,j); + p = p+i; + n = n-i; + } else { + stbtt__sort_edges_quicksort(p+i, n-i); + n = j; + } + } +} + +static void stbtt__sort_edges(stbtt__edge *p, int n) +{ + stbtt__sort_edges_quicksort(p, n); + stbtt__sort_edges_ins_sort(p, n); +} + +typedef struct +{ + float x,y; +} stbtt__point; + +static void stbtt__rasterize(stbtt__bitmap *result, stbtt__point *pts, int *wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void *userdata) +{ + float y_scale_inv = invert ? -scale_y : scale_y; + stbtt__edge *e; + int n,i,j,k,m; +#if STBTT_RASTERIZER_VERSION == 1 + int vsubsample = result->h < 8 ? 15 : 5; +#elif STBTT_RASTERIZER_VERSION == 2 + int vsubsample = 1; +#else + #error "Unrecognized value of STBTT_RASTERIZER_VERSION" +#endif + // vsubsample should divide 255 evenly; otherwise we won't reach full opacity + + // now we have to blow out the windings into explicit edge lists + n = 0; + for (i=0; i < windings; ++i) + n += wcount[i]; + + e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); // add an extra one as a sentinel + if (e == 0) return; + n = 0; + + m=0; + for (i=0; i < windings; ++i) { + stbtt__point *p = pts + m; + m += wcount[i]; + j = wcount[i]-1; + for (k=0; k < wcount[i]; j=k++) { + int a=k,b=j; + // skip the edge if horizontal + if (p[j].y == p[k].y) + continue; + // add edge from j to k to the list + e[n].invert = 0; + if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) { + e[n].invert = 1; + a=j,b=k; + } + e[n].x0 = p[a].x * scale_x + shift_x; + e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample; + e[n].x1 = p[b].x * scale_x + shift_x; + e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample; + ++n; + } + } + + // now sort the edges by their highest point (should snap to integer, and then by x) + //STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); + stbtt__sort_edges(e, n); + + // now, traverse the scanlines and find the intersections on each scanline, use xor winding rule + stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata); + + STBTT_free(e, userdata); +} + +static void stbtt__add_point(stbtt__point *points, int n, float x, float y) +{ + if (!points) return; // during first pass, it's unallocated + points[n].x = x; + points[n].y = y; +} + +// tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching +static int stbtt__tesselate_curve(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n) +{ + // midpoint + float mx = (x0 + 2*x1 + x2)/4; + float my = (y0 + 2*y1 + y2)/4; + // versus directly drawn line + float dx = (x0+x2)/2 - mx; + float dy = (y0+y2)/2 - my; + if (n > 16) // 65536 segments on one curve better be enough! + return 1; + if (dx*dx+dy*dy > objspace_flatness_squared) { // half-pixel error allowed... need to be smaller if AA + stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1); + stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1); + } else { + stbtt__add_point(points, *num_points,x2,y2); + *num_points = *num_points+1; + } + return 1; +} + +static void stbtt__tesselate_cubic(stbtt__point *points, int *num_points, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n) +{ + // @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough + float dx0 = x1-x0; + float dy0 = y1-y0; + float dx1 = x2-x1; + float dy1 = y2-y1; + float dx2 = x3-x2; + float dy2 = y3-y2; + float dx = x3-x0; + float dy = y3-y0; + float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2)); + float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy); + float flatness_squared = longlen*longlen-shortlen*shortlen; + + if (n > 16) // 65536 segments on one curve better be enough! + return; + + if (flatness_squared > objspace_flatness_squared) { + float x01 = (x0+x1)/2; + float y01 = (y0+y1)/2; + float x12 = (x1+x2)/2; + float y12 = (y1+y2)/2; + float x23 = (x2+x3)/2; + float y23 = (y2+y3)/2; + + float xa = (x01+x12)/2; + float ya = (y01+y12)/2; + float xb = (x12+x23)/2; + float yb = (y12+y23)/2; + + float mx = (xa+xb)/2; + float my = (ya+yb)/2; + + stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1); + stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1); + } else { + stbtt__add_point(points, *num_points,x3,y3); + *num_points = *num_points+1; + } +} + +// returns number of contours +static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata) +{ + stbtt__point *points=0; + int num_points=0; + + float objspace_flatness_squared = objspace_flatness * objspace_flatness; + int i,n=0,start=0, pass; + + // count how many "moves" there are to get the contour count + for (i=0; i < num_verts; ++i) + if (vertices[i].type == STBTT_vmove) + ++n; + + *num_contours = n; + if (n == 0) return 0; + + *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata); + + if (*contour_lengths == 0) { + *num_contours = 0; + return 0; + } + + // make two passes through the points so we don't need to realloc + for (pass=0; pass < 2; ++pass) { + float x=0,y=0; + if (pass == 1) { + points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata); + if (points == NULL) goto error; + } + num_points = 0; + n= -1; + for (i=0; i < num_verts; ++i) { + switch (vertices[i].type) { + case STBTT_vmove: + // start the next contour + if (n >= 0) + (*contour_lengths)[n] = num_points - start; + ++n; + start = num_points; + + x = vertices[i].x, y = vertices[i].y; + stbtt__add_point(points, num_points++, x,y); + break; + case STBTT_vline: + x = vertices[i].x, y = vertices[i].y; + stbtt__add_point(points, num_points++, x, y); + break; + case STBTT_vcurve: + stbtt__tesselate_curve(points, &num_points, x,y, + vertices[i].cx, vertices[i].cy, + vertices[i].x, vertices[i].y, + objspace_flatness_squared, 0); + x = vertices[i].x, y = vertices[i].y; + break; + case STBTT_vcubic: + stbtt__tesselate_cubic(points, &num_points, x,y, + vertices[i].cx, vertices[i].cy, + vertices[i].cx1, vertices[i].cy1, + vertices[i].x, vertices[i].y, + objspace_flatness_squared, 0); + x = vertices[i].x, y = vertices[i].y; + break; + } + } + (*contour_lengths)[n] = num_points - start; + } + + return points; +error: + STBTT_free(points, userdata); + STBTT_free(*contour_lengths, userdata); + *contour_lengths = 0; + *num_contours = 0; + return NULL; +} + +STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, float flatness_in_pixels, stbtt_vertex *vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void *userdata) +{ + float scale = scale_x > scale_y ? scale_y : scale_x; + int winding_count = 0; + int *winding_lengths = NULL; + stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata); + if (windings) { + stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata); + STBTT_free(winding_lengths, userdata); + STBTT_free(windings, userdata); + } +} + +STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata) +{ + STBTT_free(bitmap, userdata); +} + +STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int *width, int *height, int *xoff, int *yoff) +{ + int ix0,iy0,ix1,iy1; + stbtt__bitmap gbm; + stbtt_vertex *vertices; + int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); + + if (scale_x == 0) scale_x = scale_y; + if (scale_y == 0) { + if (scale_x == 0) { + STBTT_free(vertices, info->userdata); + return NULL; + } + scale_y = scale_x; + } + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1); + + // now we get the size + gbm.w = (ix1 - ix0); + gbm.h = (iy1 - iy0); + gbm.pixels = NULL; // in case we error + + if (width ) *width = gbm.w; + if (height) *height = gbm.h; + if (xoff ) *xoff = ix0; + if (yoff ) *yoff = iy0; + + if (gbm.w && gbm.h) { + gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata); + if (gbm.pixels) { + gbm.stride = gbm.w; + + stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); + } + } + STBTT_free(vertices, info->userdata); + return gbm.pixels; +} + +STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff) +{ + return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph) +{ + int ix0,iy0; + stbtt_vertex *vertices; + int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); + stbtt__bitmap gbm; + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); + gbm.pixels = output; + gbm.w = out_w; + gbm.h = out_h; + gbm.stride = out_stride; + + if (gbm.w && gbm.h) + stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata); + + STBTT_free(vertices, info->userdata); +} + +STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph) +{ + stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph); +} + +STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff) +{ + return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff); +} + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint) +{ + stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, oversample_x, oversample_y, sub_x, sub_y, stbtt_FindGlyphIndex(info,codepoint)); +} + +STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint) +{ + stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint)); +} + +STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff) +{ + return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff); +} + +STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint) +{ + stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint); +} + +////////////////////////////////////////////////////////////////////////////// +// +// bitmap baking +// +// This is SUPER-CRAPPY packing to keep source code small + +static int stbtt_BakeFontBitmap_internal(unsigned char *data, int offset, // font location (use offset=0 for plain .ttf) + float pixel_height, // height of font in pixels + unsigned char *pixels, int pw, int ph, // bitmap to be filled in + int first_char, int num_chars, // characters to bake + stbtt_bakedchar *chardata) +{ + float scale; + int x,y,bottom_y, i; + stbtt_fontinfo f; + f.userdata = NULL; + if (!stbtt_InitFont(&f, data, offset)) + return -1; + STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels + x=y=1; + bottom_y = 1; + + scale = stbtt_ScaleForPixelHeight(&f, pixel_height); + + for (i=0; i < num_chars; ++i) { + int advance, lsb, x0,y0,x1,y1,gw,gh; + int g = stbtt_FindGlyphIndex(&f, first_char + i); + stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb); + stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1); + gw = x1-x0; + gh = y1-y0; + if (x + gw + 1 >= pw) + y = bottom_y, x = 1; // advance to next row + if (y + gh + 1 >= ph) // check if it fits vertically AFTER potentially moving to next row + return -i; + STBTT_assert(x+gw < pw); + STBTT_assert(y+gh < ph); + stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g); + chardata[i].x0 = (stbtt_int16) x; + chardata[i].y0 = (stbtt_int16) y; + chardata[i].x1 = (stbtt_int16) (x + gw); + chardata[i].y1 = (stbtt_int16) (y + gh); + chardata[i].xadvance = scale * advance; + chardata[i].xoff = (float) x0; + chardata[i].yoff = (float) y0; + x = x + gw + 1; + if (y+gh+1 > bottom_y) + bottom_y = y+gh+1; + } + return bottom_y; +} + +STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int opengl_fillrule) +{ + float d3d_bias = opengl_fillrule ? 0 : -0.5f; + float ipw = 1.0f / pw, iph = 1.0f / ph; + const stbtt_bakedchar *b = chardata + char_index; + int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f); + int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f); + + q->x0 = round_x + d3d_bias; + q->y0 = round_y + d3d_bias; + q->x1 = round_x + b->x1 - b->x0 + d3d_bias; + q->y1 = round_y + b->y1 - b->y0 + d3d_bias; + + q->s0 = b->x0 * ipw; + q->t0 = b->y0 * iph; + q->s1 = b->x1 * ipw; + q->t1 = b->y1 * iph; + + *xpos += b->xadvance; +} + +////////////////////////////////////////////////////////////////////////////// +// +// rectangle packing replacement routines if you don't have stb_rect_pack.h +// + +#ifndef STB_RECT_PACK_VERSION + +typedef int stbrp_coord; + +//////////////////////////////////////////////////////////////////////////////////// +// // +// // +// COMPILER WARNING ?!?!? // +// // +// // +// if you get a compile warning due to these symbols being defined more than // +// once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" // +// // +//////////////////////////////////////////////////////////////////////////////////// + +typedef struct +{ + int width,height; + int x,y,bottom_y; +} stbrp_context; + +typedef struct +{ + unsigned char x; +} stbrp_node; + +struct stbrp_rect +{ + stbrp_coord x,y; + int id,w,h,was_packed; +}; + +static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes) +{ + con->width = pw; + con->height = ph; + con->x = 0; + con->y = 0; + con->bottom_y = 0; + STBTT__NOTUSED(nodes); + STBTT__NOTUSED(num_nodes); +} + +static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects) +{ + int i; + for (i=0; i < num_rects; ++i) { + if (con->x + rects[i].w > con->width) { + con->x = 0; + con->y = con->bottom_y; + } + if (con->y + rects[i].h > con->height) + break; + rects[i].x = con->x; + rects[i].y = con->y; + rects[i].was_packed = 1; + con->x += rects[i].w; + if (con->y + rects[i].h > con->bottom_y) + con->bottom_y = con->y + rects[i].h; + } + for ( ; i < num_rects; ++i) + rects[i].was_packed = 0; +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// bitmap baking +// +// This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If +// stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy. + +STBTT_DEF int stbtt_PackBegin(stbtt_pack_context *spc, unsigned char *pixels, int pw, int ph, int stride_in_bytes, int padding, void *alloc_context) +{ + stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context); + int num_nodes = pw - padding; + stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context); + + if (context == NULL || nodes == NULL) { + if (context != NULL) STBTT_free(context, alloc_context); + if (nodes != NULL) STBTT_free(nodes , alloc_context); + return 0; + } + + spc->user_allocator_context = alloc_context; + spc->width = pw; + spc->height = ph; + spc->pixels = pixels; + spc->pack_info = context; + spc->nodes = nodes; + spc->padding = padding; + spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw; + spc->h_oversample = 1; + spc->v_oversample = 1; + spc->skip_missing = 0; + + stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes); + + if (pixels) + STBTT_memset(pixels, 0, pw*ph); // background of 0 around pixels + + return 1; +} + +STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc) +{ + STBTT_free(spc->nodes , spc->user_allocator_context); + STBTT_free(spc->pack_info, spc->user_allocator_context); +} + +STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample) +{ + STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE); + STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE); + if (h_oversample <= STBTT_MAX_OVERSAMPLE) + spc->h_oversample = h_oversample; + if (v_oversample <= STBTT_MAX_OVERSAMPLE) + spc->v_oversample = v_oversample; +} + +STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip) +{ + spc->skip_missing = skip; +} + +#define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1) + +static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +{ + unsigned char buffer[STBTT_MAX_OVERSAMPLE]; + int safe_w = w - kernel_width; + int j; + STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze + for (j=0; j < h; ++j) { + int i; + unsigned int total; + STBTT_memset(buffer, 0, kernel_width); + + total = 0; + + // make kernel_width a constant in common cases so compiler can optimize out the divide + switch (kernel_width) { + case 2: + for (i=0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char) (total / 2); + } + break; + case 3: + for (i=0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char) (total / 3); + } + break; + case 4: + for (i=0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char) (total / 4); + } + break; + case 5: + for (i=0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char) (total / 5); + } + break; + default: + for (i=0; i <= safe_w; ++i) { + total += pixels[i] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i]; + pixels[i] = (unsigned char) (total / kernel_width); + } + break; + } + + for (; i < w; ++i) { + STBTT_assert(pixels[i] == 0); + total -= buffer[i & STBTT__OVER_MASK]; + pixels[i] = (unsigned char) (total / kernel_width); + } + + pixels += stride_in_bytes; + } +} + +static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width) +{ + unsigned char buffer[STBTT_MAX_OVERSAMPLE]; + int safe_h = h - kernel_width; + int j; + STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); // suppress bogus warning from VS2013 -analyze + for (j=0; j < w; ++j) { + int i; + unsigned int total; + STBTT_memset(buffer, 0, kernel_width); + + total = 0; + + // make kernel_width a constant in common cases so compiler can optimize out the divide + switch (kernel_width) { + case 2: + for (i=0; i <= safe_h; ++i) { + total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (unsigned char) (total / 2); + } + break; + case 3: + for (i=0; i <= safe_h; ++i) { + total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (unsigned char) (total / 3); + } + break; + case 4: + for (i=0; i <= safe_h; ++i) { + total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (unsigned char) (total / 4); + } + break; + case 5: + for (i=0; i <= safe_h; ++i) { + total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (unsigned char) (total / 5); + } + break; + default: + for (i=0; i <= safe_h; ++i) { + total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK]; + buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes]; + pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); + } + break; + } + + for (; i < h; ++i) { + STBTT_assert(pixels[i*stride_in_bytes] == 0); + total -= buffer[i & STBTT__OVER_MASK]; + pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width); + } + + pixels += 1; + } +} + +static float stbtt__oversample_shift(int oversample) +{ + if (!oversample) + return 0.0f; + + // The prefilter is a box filter of width "oversample", + // which shifts phase by (oversample - 1)/2 pixels in + // oversampled space. We want to shift in the opposite + // direction to counter this. + return (float)-(oversample - 1) / (2.0f * (float)oversample); +} + +// rects array must be big enough to accommodate all characters in the given ranges +STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) +{ + int i,j,k; + + k=0; + for (i=0; i < num_ranges; ++i) { + float fh = ranges[i].font_size; + float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); + ranges[i].h_oversample = (unsigned char) spc->h_oversample; + ranges[i].v_oversample = (unsigned char) spc->v_oversample; + for (j=0; j < ranges[i].num_chars; ++j) { + int x0,y0,x1,y1; + int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; + int glyph = stbtt_FindGlyphIndex(info, codepoint); + if (glyph == 0 && spc->skip_missing) { + rects[k].w = rects[k].h = 0; + } else { + stbtt_GetGlyphBitmapBoxSubpixel(info,glyph, + scale * spc->h_oversample, + scale * spc->v_oversample, + 0,0, + &x0,&y0,&x1,&y1); + rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1); + rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1); + } + ++k; + } + } + + return k; +} + +STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int prefilter_x, int prefilter_y, float *sub_x, float *sub_y, int glyph) +{ + stbtt_MakeGlyphBitmapSubpixel(info, + output, + out_w - (prefilter_x - 1), + out_h - (prefilter_y - 1), + out_stride, + scale_x, + scale_y, + shift_x, + shift_y, + glyph); + + if (prefilter_x > 1) + stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x); + + if (prefilter_y > 1) + stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y); + + *sub_x = stbtt__oversample_shift(prefilter_x); + *sub_y = stbtt__oversample_shift(prefilter_y); +} + +// rects array must be big enough to accommodate all characters in the given ranges +STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) +{ + int i,j,k, return_value = 1; + + // save current values + int old_h_over = spc->h_oversample; + int old_v_over = spc->v_oversample; + + k = 0; + for (i=0; i < num_ranges; ++i) { + float fh = ranges[i].font_size; + float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh); + float recip_h,recip_v,sub_x,sub_y; + spc->h_oversample = ranges[i].h_oversample; + spc->v_oversample = ranges[i].v_oversample; + recip_h = 1.0f / spc->h_oversample; + recip_v = 1.0f / spc->v_oversample; + sub_x = stbtt__oversample_shift(spc->h_oversample); + sub_y = stbtt__oversample_shift(spc->v_oversample); + for (j=0; j < ranges[i].num_chars; ++j) { + stbrp_rect *r = &rects[k]; + if (r->was_packed && r->w != 0 && r->h != 0) { + stbtt_packedchar *bc = &ranges[i].chardata_for_range[j]; + int advance, lsb, x0,y0,x1,y1; + int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; + int glyph = stbtt_FindGlyphIndex(info, codepoint); + stbrp_coord pad = (stbrp_coord) spc->padding; + + // pad on left and top + r->x += pad; + r->y += pad; + r->w -= pad; + r->h -= pad; + stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb); + stbtt_GetGlyphBitmapBox(info, glyph, + scale * spc->h_oversample, + scale * spc->v_oversample, + &x0,&y0,&x1,&y1); + stbtt_MakeGlyphBitmapSubpixel(info, + spc->pixels + r->x + r->y*spc->stride_in_bytes, + r->w - spc->h_oversample+1, + r->h - spc->v_oversample+1, + spc->stride_in_bytes, + scale * spc->h_oversample, + scale * spc->v_oversample, + 0,0, + glyph); + + if (spc->h_oversample > 1) + stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, + r->w, r->h, spc->stride_in_bytes, + spc->h_oversample); + + if (spc->v_oversample > 1) + stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes, + r->w, r->h, spc->stride_in_bytes, + spc->v_oversample); + + bc->x0 = (stbtt_int16) r->x; + bc->y0 = (stbtt_int16) r->y; + bc->x1 = (stbtt_int16) (r->x + r->w); + bc->y1 = (stbtt_int16) (r->y + r->h); + bc->xadvance = scale * advance; + bc->xoff = (float) x0 * recip_h + sub_x; + bc->yoff = (float) y0 * recip_v + sub_y; + bc->xoff2 = (x0 + r->w) * recip_h + sub_x; + bc->yoff2 = (y0 + r->h) * recip_v + sub_y; + } else { + return_value = 0; // if any fail, report failure + } + + ++k; + } + } + + // restore original values + spc->h_oversample = old_h_over; + spc->v_oversample = old_v_over; + + return return_value; +} + +STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects) +{ + stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects); +} + +STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges) +{ + stbtt_fontinfo info; + int i,j,n, return_value = 1; + //stbrp_context *context = (stbrp_context *) spc->pack_info; + stbrp_rect *rects; + + // flag all characters as NOT packed + for (i=0; i < num_ranges; ++i) + for (j=0; j < ranges[i].num_chars; ++j) + ranges[i].chardata_for_range[j].x0 = + ranges[i].chardata_for_range[j].y0 = + ranges[i].chardata_for_range[j].x1 = + ranges[i].chardata_for_range[j].y1 = 0; + + n = 0; + for (i=0; i < num_ranges; ++i) + n += ranges[i].num_chars; + + rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context); + if (rects == NULL) + return 0; + + info.userdata = spc->user_allocator_context; + stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index)); + + n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects); + + stbtt_PackFontRangesPackRects(spc, rects, n); + + return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects); + + STBTT_free(rects, spc->user_allocator_context); + return return_value; +} + +STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size, + int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range) +{ + stbtt_pack_range range; + range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range; + range.array_of_unicode_codepoints = NULL; + range.num_chars = num_chars_in_range; + range.chardata_for_range = chardata_for_range; + range.font_size = font_size; + return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1); +} + +STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap) +{ + int i_ascent, i_descent, i_lineGap; + float scale; + stbtt_fontinfo info; + stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index)); + scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size); + stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap); + *ascent = (float) i_ascent * scale; + *descent = (float) i_descent * scale; + *lineGap = (float) i_lineGap * scale; +} + +STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, int char_index, float *xpos, float *ypos, stbtt_aligned_quad *q, int align_to_integer) +{ + float ipw = 1.0f / pw, iph = 1.0f / ph; + const stbtt_packedchar *b = chardata + char_index; + + if (align_to_integer) { + float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f); + float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f); + q->x0 = x; + q->y0 = y; + q->x1 = x + b->xoff2 - b->xoff; + q->y1 = y + b->yoff2 - b->yoff; + } else { + q->x0 = *xpos + b->xoff; + q->y0 = *ypos + b->yoff; + q->x1 = *xpos + b->xoff2; + q->y1 = *ypos + b->yoff2; + } + + q->s0 = b->x0 * ipw; + q->t0 = b->y0 * iph; + q->s1 = b->x1 * ipw; + q->t1 = b->y1 * iph; + + *xpos += b->xadvance; +} + +////////////////////////////////////////////////////////////////////////////// +// +// sdf computation +// + +#define STBTT_min(a,b) ((a) < (b) ? (a) : (b)) +#define STBTT_max(a,b) ((a) < (b) ? (b) : (a)) + +static int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2], float q1[2], float q2[2], float hits[2][2]) +{ + float q0perp = q0[1]*ray[0] - q0[0]*ray[1]; + float q1perp = q1[1]*ray[0] - q1[0]*ray[1]; + float q2perp = q2[1]*ray[0] - q2[0]*ray[1]; + float roperp = orig[1]*ray[0] - orig[0]*ray[1]; + + float a = q0perp - 2*q1perp + q2perp; + float b = q1perp - q0perp; + float c = q0perp - roperp; + + float s0 = 0., s1 = 0.; + int num_s = 0; + + if (a != 0.0) { + float discr = b*b - a*c; + if (discr > 0.0) { + float rcpna = -1 / a; + float d = (float) STBTT_sqrt(discr); + s0 = (b+d) * rcpna; + s1 = (b-d) * rcpna; + if (s0 >= 0.0 && s0 <= 1.0) + num_s = 1; + if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) { + if (num_s == 0) s0 = s1; + ++num_s; + } + } + } else { + // 2*b*s + c = 0 + // s = -c / (2*b) + s0 = c / (-2 * b); + if (s0 >= 0.0 && s0 <= 1.0) + num_s = 1; + } + + if (num_s == 0) + return 0; + else { + float rcp_len2 = 1 / (ray[0]*ray[0] + ray[1]*ray[1]); + float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2; + + float q0d = q0[0]*rayn_x + q0[1]*rayn_y; + float q1d = q1[0]*rayn_x + q1[1]*rayn_y; + float q2d = q2[0]*rayn_x + q2[1]*rayn_y; + float rod = orig[0]*rayn_x + orig[1]*rayn_y; + + float q10d = q1d - q0d; + float q20d = q2d - q0d; + float q0rd = q0d - rod; + + hits[0][0] = q0rd + s0*(2.0f - 2.0f*s0)*q10d + s0*s0*q20d; + hits[0][1] = a*s0+b; + + if (num_s > 1) { + hits[1][0] = q0rd + s1*(2.0f - 2.0f*s1)*q10d + s1*s1*q20d; + hits[1][1] = a*s1+b; + return 2; + } else { + return 1; + } + } +} + +static int equal(float *a, float *b) +{ + return (a[0] == b[0] && a[1] == b[1]); +} + +static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts) +{ + int i; + float orig[2], ray[2] = { 1, 0 }; + float y_frac; + int winding = 0; + + orig[0] = x; + //orig[1] = y; // [DEAR IMGUI] commmented double assignment + + // make sure y never passes through a vertex of the shape + y_frac = (float) STBTT_fmod(y, 1.0f); + if (y_frac < 0.01f) + y += 0.01f; + else if (y_frac > 0.99f) + y -= 0.01f; + orig[1] = y; + + // test a ray from (-infinity,y) to (x,y) + for (i=0; i < nverts; ++i) { + if (verts[i].type == STBTT_vline) { + int x0 = (int) verts[i-1].x, y0 = (int) verts[i-1].y; + int x1 = (int) verts[i ].x, y1 = (int) verts[i ].y; + if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { + float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; + if (x_inter < x) + winding += (y0 < y1) ? 1 : -1; + } + } + if (verts[i].type == STBTT_vcurve) { + int x0 = (int) verts[i-1].x , y0 = (int) verts[i-1].y ; + int x1 = (int) verts[i ].cx, y1 = (int) verts[i ].cy; + int x2 = (int) verts[i ].x , y2 = (int) verts[i ].y ; + int ax = STBTT_min(x0,STBTT_min(x1,x2)), ay = STBTT_min(y0,STBTT_min(y1,y2)); + int by = STBTT_max(y0,STBTT_max(y1,y2)); + if (y > ay && y < by && x > ax) { + float q0[2],q1[2],q2[2]; + float hits[2][2]; + q0[0] = (float)x0; + q0[1] = (float)y0; + q1[0] = (float)x1; + q1[1] = (float)y1; + q2[0] = (float)x2; + q2[1] = (float)y2; + if (equal(q0,q1) || equal(q1,q2)) { + x0 = (int)verts[i-1].x; + y0 = (int)verts[i-1].y; + x1 = (int)verts[i ].x; + y1 = (int)verts[i ].y; + if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { + float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; + if (x_inter < x) + winding += (y0 < y1) ? 1 : -1; + } + } else { + int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits); + if (num_hits >= 1) + if (hits[0][0] < 0) + winding += (hits[0][1] < 0 ? -1 : 1); + if (num_hits >= 2) + if (hits[1][0] < 0) + winding += (hits[1][1] < 0 ? -1 : 1); + } + } + } + } + return winding; +} + +static float stbtt__cuberoot( float x ) +{ + if (x<0) + return -(float) STBTT_pow(-x,1.0f/3.0f); + else + return (float) STBTT_pow( x,1.0f/3.0f); +} + +// x^3 + c*x^2 + b*x + a = 0 +static int stbtt__solve_cubic(float a, float b, float c, float* r) +{ + float s = -a / 3; + float p = b - a*a / 3; + float q = a * (2*a*a - 9*b) / 27 + c; + float p3 = p*p*p; + float d = q*q + 4*p3 / 27; + if (d >= 0) { + float z = (float) STBTT_sqrt(d); + float u = (-q + z) / 2; + float v = (-q - z) / 2; + u = stbtt__cuberoot(u); + v = stbtt__cuberoot(v); + r[0] = s + u + v; + return 1; + } else { + float u = (float) STBTT_sqrt(-p/3); + float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; // p3 must be negative, since d is negative + float m = (float) STBTT_cos(v); + float n = (float) STBTT_cos(v-3.141592/2)*1.732050808f; + r[0] = s + u * 2 * m; + r[1] = s - u * (m + n); + r[2] = s - u * (m - n); + + //STBTT_assert( STBTT_fabs(((r[0]+a)*r[0]+b)*r[0]+c) < 0.05f); // these asserts may not be safe at all scales, though they're in bezier t parameter units so maybe? + //STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); + //STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); + return 3; + } +} + +STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff) +{ + float scale_x = scale, scale_y = scale; + int ix0,iy0,ix1,iy1; + int w,h; + unsigned char *data; + + // if one scale is 0, use same scale for both + if (scale_x == 0) scale_x = scale_y; + if (scale_y == 0) { + if (scale_x == 0) return NULL; // if both scales are 0, return NULL + scale_y = scale_x; + } + + stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1); + + // if empty, return NULL + if (ix0 == ix1 || iy0 == iy1) + return NULL; + + ix0 -= padding; + iy0 -= padding; + ix1 += padding; + iy1 += padding; + + w = (ix1 - ix0); + h = (iy1 - iy0); + + if (width ) *width = w; + if (height) *height = h; + if (xoff ) *xoff = ix0; + if (yoff ) *yoff = iy0; + + // invert for y-downwards bitmaps + scale_y = -scale_y; + + { + int x,y,i,j; + float *precompute; + stbtt_vertex *verts; + int num_verts = stbtt_GetGlyphShape(info, glyph, &verts); + data = (unsigned char *) STBTT_malloc(w * h, info->userdata); + precompute = (float *) STBTT_malloc(num_verts * sizeof(float), info->userdata); + + for (i=0,j=num_verts-1; i < num_verts; j=i++) { + if (verts[i].type == STBTT_vline) { + float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; + float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y; + float dist = (float) STBTT_sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0)); + precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist; + } else if (verts[i].type == STBTT_vcurve) { + float x2 = verts[j].x *scale_x, y2 = verts[j].y *scale_y; + float x1 = verts[i].cx*scale_x, y1 = verts[i].cy*scale_y; + float x0 = verts[i].x *scale_x, y0 = verts[i].y *scale_y; + float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; + float len2 = bx*bx + by*by; + if (len2 != 0.0f) + precompute[i] = 1.0f / (bx*bx + by*by); + else + precompute[i] = 0.0f; + } else + precompute[i] = 0.0f; + } + + for (y=iy0; y < iy1; ++y) { + for (x=ix0; x < ix1; ++x) { + float val; + float min_dist = 999999.0f; + float sx = (float) x + 0.5f; + float sy = (float) y + 0.5f; + float x_gspace = (sx / scale_x); + float y_gspace = (sy / scale_y); + + int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts, verts); // @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path + + for (i=0; i < num_verts; ++i) { + float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; + + // check against every point here rather than inside line/curve primitives -- @TODO: wrong if multiple 'moves' in a row produce a garbage point, and given culling, probably more efficient to do within line/curve + float dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy); + if (dist2 < min_dist*min_dist) + min_dist = (float) STBTT_sqrt(dist2); + + if (verts[i].type == STBTT_vline) { + float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y; + + // coarse culling against bbox + //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && + // sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) + float dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i]; + STBTT_assert(i != 0); + if (dist < min_dist) { + // check position along line + // x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0) + // minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy) + float dx = x1-x0, dy = y1-y0; + float px = x0-sx, py = y0-sy; + // minimize (px+t*dx)^2 + (py+t*dy)^2 = px*px + 2*px*dx*t + t^2*dx*dx + py*py + 2*py*dy*t + t^2*dy*dy + // derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve + float t = -(px*dx + py*dy) / (dx*dx + dy*dy); + if (t >= 0.0f && t <= 1.0f) + min_dist = dist; + } + } else if (verts[i].type == STBTT_vcurve) { + float x2 = verts[i-1].x *scale_x, y2 = verts[i-1].y *scale_y; + float x1 = verts[i ].cx*scale_x, y1 = verts[i ].cy*scale_y; + float box_x0 = STBTT_min(STBTT_min(x0,x1),x2); + float box_y0 = STBTT_min(STBTT_min(y0,y1),y2); + float box_x1 = STBTT_max(STBTT_max(x0,x1),x2); + float box_y1 = STBTT_max(STBTT_max(y0,y1),y2); + // coarse culling against bbox to avoid computing cubic unnecessarily + if (sx > box_x0-min_dist && sx < box_x1+min_dist && sy > box_y0-min_dist && sy < box_y1+min_dist) { + int num=0; + float ax = x1-x0, ay = y1-y0; + float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; + float mx = x0 - sx, my = y0 - sy; + float res[3],px,py,t,it; + float a_inv = precompute[i]; + if (a_inv == 0.0) { // if a_inv is 0, it's 2nd degree so use quadratic formula + float a = 3*(ax*bx + ay*by); + float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by); + float c = mx*ax+my*ay; + if (a == 0.0) { // if a is 0, it's linear + if (b != 0.0) { + res[num++] = -c/b; + } + } else { + float discriminant = b*b - 4*a*c; + if (discriminant < 0) + num = 0; + else { + float root = (float) STBTT_sqrt(discriminant); + res[0] = (-b - root)/(2*a); + res[1] = (-b + root)/(2*a); + num = 2; // don't bother distinguishing 1-solution case, as code below will still work + } + } + } else { + float b = 3*(ax*bx + ay*by) * a_inv; // could precompute this as it doesn't depend on sample point + float c = (2*(ax*ax + ay*ay) + (mx*bx+my*by)) * a_inv; + float d = (mx*ax+my*ay) * a_inv; + num = stbtt__solve_cubic(b, c, d, res); + } + if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) { + t = res[0], it = 1.0f - t; + px = it*it*x0 + 2*t*it*x1 + t*t*x2; + py = it*it*y0 + 2*t*it*y1 + t*t*y2; + dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); + if (dist2 < min_dist * min_dist) + min_dist = (float) STBTT_sqrt(dist2); + } + if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) { + t = res[1], it = 1.0f - t; + px = it*it*x0 + 2*t*it*x1 + t*t*x2; + py = it*it*y0 + 2*t*it*y1 + t*t*y2; + dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); + if (dist2 < min_dist * min_dist) + min_dist = (float) STBTT_sqrt(dist2); + } + if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) { + t = res[2], it = 1.0f - t; + px = it*it*x0 + 2*t*it*x1 + t*t*x2; + py = it*it*y0 + 2*t*it*y1 + t*t*y2; + dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy); + if (dist2 < min_dist * min_dist) + min_dist = (float) STBTT_sqrt(dist2); + } + } + } + } + if (winding == 0) + min_dist = -min_dist; // if outside the shape, value is negative + val = onedge_value + pixel_dist_scale * min_dist; + if (val < 0) + val = 0; + else if (val > 255) + val = 255; + data[(y-iy0)*w+(x-ix0)] = (unsigned char) val; + } + } + STBTT_free(precompute, info->userdata); + STBTT_free(verts, info->userdata); + } + return data; +} + +STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff) +{ + return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, width, height, xoff, yoff); +} + +STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata) +{ + STBTT_free(bitmap, userdata); +} + +////////////////////////////////////////////////////////////////////////////// +// +// font name matching -- recommended not to use this +// + +// check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string +static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2) +{ + stbtt_int32 i=0; + + // convert utf16 to utf8 and compare the results while converting + while (len2) { + stbtt_uint16 ch = s2[0]*256 + s2[1]; + if (ch < 0x80) { + if (i >= len1) return -1; + if (s1[i++] != ch) return -1; + } else if (ch < 0x800) { + if (i+1 >= len1) return -1; + if (s1[i++] != 0xc0 + (ch >> 6)) return -1; + if (s1[i++] != 0x80 + (ch & 0x3f)) return -1; + } else if (ch >= 0xd800 && ch < 0xdc00) { + stbtt_uint32 c; + stbtt_uint16 ch2 = s2[2]*256 + s2[3]; + if (i+3 >= len1) return -1; + c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000; + if (s1[i++] != 0xf0 + (c >> 18)) return -1; + if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1; + if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1; + if (s1[i++] != 0x80 + ((c ) & 0x3f)) return -1; + s2 += 2; // plus another 2 below + len2 -= 2; + } else if (ch >= 0xdc00 && ch < 0xe000) { + return -1; + } else { + if (i+2 >= len1) return -1; + if (s1[i++] != 0xe0 + (ch >> 12)) return -1; + if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1; + if (s1[i++] != 0x80 + ((ch ) & 0x3f)) return -1; + } + s2 += 2; + len2 -= 2; + } + return i; +} + +static int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2) +{ + return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2); +} + +// returns results in whatever encoding you request... but note that 2-byte encodings +// will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare +STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID) +{ + stbtt_int32 i,count,stringOffset; + stbtt_uint8 *fc = font->data; + stbtt_uint32 offset = font->fontstart; + stbtt_uint32 nm = stbtt__find_table(fc, offset, "name"); + if (!nm) return NULL; + + count = ttUSHORT(fc+nm+2); + stringOffset = nm + ttUSHORT(fc+nm+4); + for (i=0; i < count; ++i) { + stbtt_uint32 loc = nm + 6 + 12 * i; + if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2) + && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) { + *length = ttUSHORT(fc+loc+8); + return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10)); + } + } + return NULL; +} + +static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id) +{ + stbtt_int32 i; + stbtt_int32 count = ttUSHORT(fc+nm+2); + stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4); + + for (i=0; i < count; ++i) { + stbtt_uint32 loc = nm + 6 + 12 * i; + stbtt_int32 id = ttUSHORT(fc+loc+6); + if (id == target_id) { + // find the encoding + stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4); + + // is this a Unicode encoding? + if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) { + stbtt_int32 slen = ttUSHORT(fc+loc+8); + stbtt_int32 off = ttUSHORT(fc+loc+10); + + // check if there's a prefix match + stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen); + if (matchlen >= 0) { + // check for target_id+1 immediately following, with same encoding & language + if (i+1 < count && ttUSHORT(fc+loc+12+6) == next_id && ttUSHORT(fc+loc+12) == platform && ttUSHORT(fc+loc+12+2) == encoding && ttUSHORT(fc+loc+12+4) == language) { + slen = ttUSHORT(fc+loc+12+8); + off = ttUSHORT(fc+loc+12+10); + if (slen == 0) { + if (matchlen == nlen) + return 1; + } else if (matchlen < nlen && name[matchlen] == ' ') { + ++matchlen; + if (stbtt_CompareUTF8toUTF16_bigendian_internal((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen)) + return 1; + } + } else { + // if nothing immediately following + if (matchlen == nlen) + return 1; + } + } + } + + // @TODO handle other encodings + } + } + return 0; +} + +static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags) +{ + stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name); + stbtt_uint32 nm,hd; + if (!stbtt__isfont(fc+offset)) return 0; + + // check italics/bold/underline flags in macStyle... + if (flags) { + hd = stbtt__find_table(fc, offset, "head"); + if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0; + } + + nm = stbtt__find_table(fc, offset, "name"); + if (!nm) return 0; + + if (flags) { + // if we checked the macStyle flags, then just check the family and ignore the subfamily + if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1; + if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1; + if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; + } else { + if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1; + if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1; + if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1; + } + + return 0; +} + +static int stbtt_FindMatchingFont_internal(unsigned char *font_collection, char *name_utf8, stbtt_int32 flags) +{ + stbtt_int32 i; + for (i=0;;++i) { + stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i); + if (off < 0) return off; + if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags)) + return off; + } +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif + +STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, + float pixel_height, unsigned char *pixels, int pw, int ph, + int first_char, int num_chars, stbtt_bakedchar *chardata) +{ + return stbtt_BakeFontBitmap_internal((unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata); +} + +STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index) +{ + return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index); +} + +STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data) +{ + return stbtt_GetNumberOfFonts_internal((unsigned char *) data); +} + +STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset) +{ + return stbtt_InitFont_internal(info, (unsigned char *) data, offset); +} + +STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags) +{ + return stbtt_FindMatchingFont_internal((unsigned char *) fontdata, (char *) name, flags); +} + +STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2) +{ + return stbtt_CompareUTF8toUTF16_bigendian_internal((char *) s1, len1, (char *) s2, len2); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif + +#endif // STB_TRUETYPE_IMPLEMENTATION + + +// FULL VERSION HISTORY +// +// 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod +// 1.18 (2018-01-29) add missing function +// 1.17 (2017-07-23) make more arguments const; doc fix +// 1.16 (2017-07-12) SDF support +// 1.15 (2017-03-03) make more arguments const +// 1.14 (2017-01-16) num-fonts-in-TTC function +// 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts +// 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual +// 1.11 (2016-04-02) fix unused-variable warning +// 1.10 (2016-04-02) allow user-defined fabs() replacement +// fix memory leak if fontsize=0.0 +// fix warning from duplicate typedef +// 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges +// 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges +// 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; +// allow PackFontRanges to pack and render in separate phases; +// fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); +// fixed an assert() bug in the new rasterizer +// replace assert() with STBTT_assert() in new rasterizer +// 1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine) +// also more precise AA rasterizer, except if shapes overlap +// remove need for STBTT_sort +// 1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC +// 1.04 (2015-04-15) typo in example +// 1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes +// 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++ +// 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match +// non-oversampled; STBTT_POINT_SIZE for packed case only +// 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling +// 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg) +// 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID +// 0.8b (2014-07-07) fix a warning +// 0.8 (2014-05-25) fix a few more warnings +// 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back +// 0.6c (2012-07-24) improve documentation +// 0.6b (2012-07-20) fix a few more warnings +// 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels, +// stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty +// 0.5 (2011-12-09) bugfixes: +// subpixel glyph renderer computed wrong bounding box +// first vertex of shape can be off-curve (FreeSans) +// 0.4b (2011-12-03) fixed an error in the font baking example +// 0.4 (2011-12-01) kerning, subpixel rendering (tor) +// bugfixes for: +// codepoint-to-glyph conversion using table fmt=12 +// codepoint-to-glyph conversion using table fmt=4 +// stbtt_GetBakedQuad with non-square texture (Zer) +// updated Hello World! sample to use kerning and subpixel +// fixed some warnings +// 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM) +// userdata, malloc-from-userdata, non-zero fill (stb) +// 0.2 (2009-03-11) Fix unsigned/signed char warnings +// 0.1 (2009-03-09) First public release +// + +/* +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2017 Sean Barrett +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +*/ diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 808f5a28..e4a9ef42 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -115,6 +115,7 @@ class viewer // init void info_gl(); void init_gl(); + void init_imgui(); void init_menus(); void init_glsl(); void update_vbo(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 9e6bd9a8..47a9e902 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -8,9 +8,13 @@ #include #include -#include -#include -#include +#include "che_off.h" +#include "che_obj.h" +#include "che_ply.h" + +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" using namespace std; @@ -41,6 +45,7 @@ viewer::viewer() bgc = 0; init_gl(); + init_imgui(); init_menus(); set_gl(); @@ -51,6 +56,10 @@ viewer::viewer() viewer::~viewer() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + glfwDestroyWindow(window); glfwTerminate(); } @@ -60,6 +69,32 @@ bool viewer::run() while(!glfwWindowShouldClose(window)) { display(); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + if(ImGui::BeginMainMenuBar()) + { + for(index_t i = 0; i < sub_menus.size(); i++) + { + if(ImGui::BeginMenu(sub_menus[i].c_str())) + { + for(auto & p: processes) + if(p.second.sub_menu == i && ImGui::MenuItem(p.second.name_function.c_str())) + p.second.function(this); + + ImGui::EndMenu(); + } + } + ImGui::EndMainMenuBar(); + } + + // Rendering + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + + glfwSwapBuffers(window); } return true; @@ -117,10 +152,22 @@ void viewer::init_gl() glewInit(); } +void viewer::init_imgui() +{ + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void) io; + + ImGui::StyleColorsDark(); + + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 460"); +} + void viewer::init_menus() { // init viewer menu - sub_menus.push_back("viewer"); + sub_menus.push_back("Viewer"); add_process(GLFW_KEY_F1, "[F1] Help", menu_help); add_process(GLFW_KEY_F2, "[F2] Invert Orintation", invert_orientation); add_process(GLFW_KEY_F3, "[F3] Render Wireframe", set_render_wireframe); @@ -440,7 +487,6 @@ void viewer::display() shader_program.disable(); - glfwSwapBuffers(window); glfwPollEvents(); } From a981feca321e4490abc0f66b3e9e21f669a26884 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 18 Dec 2019 16:53:28 +0100 Subject: [PATCH 0065/1018] normalise try outs --- src/mdict/denoising.cpp | 1 + src/mdict/dictionary.cpp | 6 +++--- src/mdict/inpainting.cpp | 1 + src/mdict/mdict.cpp | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index dfef4b5c..4ba00404 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -24,6 +24,7 @@ distance_t denoising::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); draw_patches(128); + draw_patches(12); phi_basis->plot_atoms(A); phi_basis->plot_basis(); TIC(d_time) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index f71742fc..1302a505 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -49,7 +49,7 @@ void dictionary::learning() { A.eye(phi_basis->dim, m); A = normalise(A); - gproshan_debug_var(phi_basis->dim); + gproshan_debug_var(phi_basis->radio); gproshan_debug_var(m); phi_basis->plot_atoms(A); KSVD(A, patches, L, K); @@ -59,7 +59,7 @@ void dictionary::learning() gproshan_debug_var(A); } else A.eye(phi_basis->dim, m); - + gproshan_debug_var(phi_basis->radio); assert(A.n_rows == phi_basis->dim); assert(A.n_cols == m); if(d_plot) @@ -160,7 +160,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->dim); phi_basis->discrete(p.phi, p.xyz); - //p.phi = normalise(p.phi); + p.phi = normalise(p.phi); } /* diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 934c8449..312b3046 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -195,6 +195,7 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); + phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index cbc1246a..e15435a6 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -190,7 +190,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { - return OMP(p.xyz.row(2).t(), normalise(p.phi * A), L); + return OMP(p.xyz.row(2).t(), p.phi * A, L); } a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) From f9cc066f9b8cd05d6d379b06c2223abacdd28b45 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 18 Dec 2019 18:08:21 +0100 Subject: [PATCH 0066/1018] move imgui include headers --- include/viewer/viewer.h | 5 +++++ src/viewer/viewer.cpp | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index e4a9ef42..d01e0f11 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -10,6 +10,11 @@ #include "include_opengl.h" +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" + + #define N_MESHES 12 diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 47a9e902..4195184e 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -12,10 +12,6 @@ #include "che_obj.h" #include "che_ply.h" -#include "imgui.h" -#include "imgui_impl_glfw.h" -#include "imgui_impl_opengl3.h" - using namespace std; From f7a4b8094b74c9e543612de2ef4a461b5cd5bee9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 18 Dec 2019 22:15:24 +0100 Subject: [PATCH 0067/1018] main menu select mesh --- include/viewer/che_viewer.h | 2 +- include/viewer/viewer.h | 11 +++--- shaders/fragment.glsl | 8 ++--- src/app_viewer.cpp | 68 ++++++++++++++++++------------------- src/viewer/viewer.cpp | 64 ++++++++++++++++++++-------------- 5 files changed, 82 insertions(+), 71 deletions(-) diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 96711add..b93cdb74 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -5,7 +5,7 @@ #include "include_opengl.h" -#define COLOR 0.5 +#define COLOR 0.4 #ifdef SINGLE_P #define glVertex3v(x) glVertex3fv(x) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index d01e0f11..d67b392e 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -65,9 +65,10 @@ class viewer struct process_t { - index_t sub_menu; - std::string name_function; + std::string key; + std::string name; function_t function; + index_t sub_menu; }; static const int m_window_size[N_MESHES][2]; @@ -84,15 +85,13 @@ class viewer size_t n_meshes; index_t current; // current mesh - char * share; - bool render_wireframe; bool render_gradient_field; bool render_normal_field; bool render_border; bool render_corr; bool render_lines; - bool is_flat; + bool render_flat; float bgc; std::map processes; @@ -112,7 +111,7 @@ class viewer bool run(); che_viewer & mesh(); - void add_process(const int & key, const std::string & name, function_t function); + void add_process(const int & key, const process_t & process); void add_mesh(const std::vector & _meshes); private: diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index be591d16..6ab0db52 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -2,8 +2,8 @@ uniform vec3 eye; uniform vec3 light; -uniform bool is_flat; -uniform bool lines; +uniform bool render_flat; +uniform bool render_lines; in vec3 position; in vec3 normal; @@ -77,7 +77,7 @@ void main() // lines - if(lines) + if(render_lines) { float h = color; h = h * 40.; @@ -89,7 +89,7 @@ void main() vec3 N; - if(is_flat) + if(render_flat) { vec3 X = dFdx(position); vec3 Y = dFdy(position); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index f701868a..366c192b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -60,58 +60,58 @@ int app_viewer::main(int nargs, const char ** args) add_mesh(meshes); sub_menus.push_back("Fairing"); - add_process(GLFW_KEY_T, "[T] Fairing Taubin", process_fairing_taubin); - add_process(GLFW_KEY_E, "[E] Fairing Spectral", process_fairing_spectral); + add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); + add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); sub_menus.push_back("Geodesics"); - add_process(GLFW_KEY_F, "[F] Fast Marching", process_geodesics_fm); - add_process(GLFW_KEY_U, "[C] Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu); + add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); + add_process(GLFW_KEY_U, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); #ifndef SINGLE_P - add_process(GLFW_KEY_L, "[L] Heat Method", process_geodesics_heat_flow); + add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_flow}); #endif #ifdef GPROSHAN_CUDA - add_process(GLFW_KEY_G, "[G] Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu); + add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); // add_process('L', "Geodesics (HEAT_FLOW_GPU)", process_geodesics_heat_flow_gpu); #endif // GPROSHAN_CUDA - add_process(GLFW_KEY_S, "[S] Geodesic Farthest Point Sampling", process_farthest_point_sampling); - add_process(GLFW_KEY_R, "[R] Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio); - add_process(GLFW_KEY_V, "[V] Geodesic Voronoi", process_voronoi); - add_process(GLFW_KEY_P, "[P] Toplesets", compute_toplesets); + add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); + add_process(GLFW_KEY_R, {"R", "Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio}); + add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); + add_process(GLFW_KEY_P, {"P", "Toplesets", compute_toplesets}); sub_menus.push_back("Dictionary Learning"); - add_process(GLFW_KEY_J, "[J] MDICT Patch", process_mdict_patch); - add_process(GLFW_KEY_D, "[D] MDICT Denoising", process_denoising); - add_process(GLFW_KEY_A, "[A] MDICT Super Resolution", process_super_resolution); - add_process(GLFW_KEY_I, "[I] MDICT Inpaiting", process_inpaiting); + add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); + add_process(GLFW_KEY_D, {"D", "MDICT Denoising", process_denoising}); + add_process(GLFW_KEY_A, {"A", "MDICT Super Resolution", process_super_resolution}); + add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); // add_process('A', "IT Inpainting", process_iterative_inpaiting); sub_menus.push_back("Signatures"); - add_process(GLFW_KEY_2, "[2] GPS", process_gps); - add_process(GLFW_KEY_3, "[3] HKS", process_hks); - add_process(GLFW_KEY_4, "[4] WKS", process_wks); - add_process(GLFW_KEY_5, "[5] Functional Maps", process_functional_maps); - add_process(GLFW_KEY_6, "[6] Key Points", process_key_points); - add_process(GLFW_KEY_7, "[7] Key Components", process_key_components); + add_process(GLFW_KEY_2, {"2", "GPS", process_gps}); + add_process(GLFW_KEY_3, {"3", "HKS", process_hks}); + add_process(GLFW_KEY_4, {"4", "WKS", process_wks}); + add_process(GLFW_KEY_5, {"5", "Functional Maps", process_functional_maps}); + add_process(GLFW_KEY_6, {"6", "Key Points", process_key_points}); + add_process(GLFW_KEY_7, {"7", "Key Components", process_key_components}); sub_menus.push_back("Repair Holes"); - add_process(GLFW_KEY_X, "[X] Poisson Membrane surface", process_poisson_laplacian_1); - add_process(GLFW_KEY_Y, "[Y] Poisson Thin-plate surface", process_poisson_laplacian_2); - add_process(GLFW_KEY_Z, "[Z] Poisson Minimum variation surface", process_poisson_laplacian_3); - add_process(GLFW_KEY_H, "[H] Fill hole - planar mesh", process_fill_holes); - add_process(GLFW_KEY_B, "[B] Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines); + add_process(GLFW_KEY_X, {"X", "Poisson Membrane surface", process_poisson_laplacian_1}); + add_process(GLFW_KEY_Y, {"Y", "Poisson Thin-plate surface", process_poisson_laplacian_2}); + add_process(GLFW_KEY_Z, {"Z", "Poisson Minimum variation surface", process_poisson_laplacian_3}); + add_process(GLFW_KEY_H, {"H", "Fill hole - planar mesh", process_fill_holes}); + add_process(GLFW_KEY_B, {"B", "Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines}); sub_menus.push_back("Others"); - add_process(GLFW_KEY_SLASH, "[SLASH] Threshold", process_threshold); - add_process(GLFW_KEY_N, "[N] Noise", process_noise); - add_process(GLFW_KEY_COMMA, "[COMMA] Black noise", process_black_noise); - add_process(GLFW_KEY_M, "[M] Multiplicate", process_multiplicate_vertices); - add_process(GLFW_KEY_PERIOD, "[PERIOD] Delete vertices", process_delete_vertices); - add_process(GLFW_KEY_MINUS, "[MINUS] Delete non-manifold vertices", process_delete_non_manifold_vertices); - add_process(GLFW_KEY_K, "[K] Gaussian curvature", process_gaussian_curvature); - add_process(GLFW_KEY_9, "[9] Edge Collapse", process_edge_collapse); - add_process(GLFW_KEY_SEMICOLON, "[SEMICOLON] Select multiple vertices", select_multiple); + add_process(GLFW_KEY_SLASH, {"SLASH", "Threshold", process_threshold}); + add_process(GLFW_KEY_N, {"N", "Noise", process_noise}); + add_process(GLFW_KEY_COMMA, {"COMMA", "Black noise", process_black_noise}); + add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); + add_process(GLFW_KEY_PERIOD, {"PERIOD", "Delete vertices", process_delete_vertices}); + add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); + add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); + add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); + add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", select_multiple}); run(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 4195184e..6d3940cb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -37,6 +37,7 @@ viewer::viewer() render_border = false; render_lines = false; render_corr = false; + render_flat = false; bgc = 0; @@ -72,12 +73,22 @@ bool viewer::run() if(ImGui::BeginMainMenuBar()) { + if(ImGui::BeginMenu("Select")) + { + for(index_t i = 0; i < n_meshes; i++) + if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str())) + current = i; + + ImGui::EndMenu(); + } + for(index_t i = 0; i < sub_menus.size(); i++) { if(ImGui::BeginMenu(sub_menus[i].c_str())) { for(auto & p: processes) - if(p.second.sub_menu == i && ImGui::MenuItem(p.second.name_function.c_str())) + if(p.second.function != nullptr && p.second.sub_menu == i && + ImGui::MenuItem(p.second.name.c_str(), ('[' + p.second.key + ']').c_str())) p.second.function(this); ImGui::EndMenu(); @@ -164,26 +175,26 @@ void viewer::init_menus() { // init viewer menu sub_menus.push_back("Viewer"); - add_process(GLFW_KEY_F1, "[F1] Help", menu_help); - add_process(GLFW_KEY_F2, "[F2] Invert Orintation", invert_orientation); - add_process(GLFW_KEY_F3, "[F3] Render Wireframe", set_render_wireframe); - add_process(GLFW_KEY_F4, "[F4] Gradient Field", set_render_gradient_field); - add_process(GLFW_KEY_F5, "[F5] Normal Field", set_render_normal_field); - add_process(GLFW_KEY_F6, "[F6] Show Borders", set_render_border); - add_process(GLFW_KEY_SPACE, "[SPACE] Level Curves", set_render_lines); - add_process(GLFW_KEY_TAB, "[TAB] Render Flat", set_is_flat); + add_process(GLFW_KEY_F1, {"F1", "Help", menu_help}); + add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); + add_process(GLFW_KEY_F3, {"F3", "Render Wireframe", set_render_wireframe}); + add_process(GLFW_KEY_F4, {"F4", "Gradient Field", set_render_gradient_field}); + add_process(GLFW_KEY_F5, {"F5", "Normal Field", set_render_normal_field}); + add_process(GLFW_KEY_F6, {"F6", "Show Borders", set_render_border}); + add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); + add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); // add_process('+', "Show Corr", set_render_corr); // init mesh menu sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, "[BACKSPACE] Reload/Reset", menu_reset_mesh); - add_process(GLFW_KEY_W, "[W] Save Mesh", menu_save_mesh); - add_process(GLFW_KEY_UP, "[UP] Zoom in", menu_zoom_in); - add_process(GLFW_KEY_DOWN, "[DOWN] Zoom out", menu_zoom_out); - add_process(GLFW_KEY_RIGHT, "[RIGHT] Background color inc", menu_bgc_inc); - add_process(GLFW_KEY_LEFT, "[LEFT] Background color dec", menu_bgc_dec); - add_process(GLFW_KEY_1, "[1] Background color white", menu_bgc_white); - add_process(GLFW_KEY_0, "[0] Background color black", menu_bgc_black); + add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); + add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); + add_process(GLFW_KEY_UP, {"UP", "Zoom in", menu_zoom_in}); + add_process(GLFW_KEY_DOWN, {"DOWN", "Zoom out", menu_zoom_out}); + add_process(GLFW_KEY_RIGHT, {"RIGHT", "Background color inc", menu_bgc_inc}); + add_process(GLFW_KEY_LEFT, {"LEFT", "Background color dec", menu_bgc_dec}); + add_process(GLFW_KEY_1, {"1", "Background color white", menu_bgc_white}); + add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); } void viewer::init_glsl() @@ -203,11 +214,12 @@ void viewer::menu_process(int value) menu_process(processes[value].function); } -void viewer::add_process(const int & key, const string & name, function_t function) +void viewer::add_process(const int & key, const process_t & process) { if(processes.find(key) == processes.end()) { - processes[key] = {(index_t) sub_menus.size() - 1, name, function}; + processes[key] = process; + processes[key].sub_menu = sub_menus.size() - 1; } else cerr << "Repeat key: " << key << endl; } @@ -304,8 +316,8 @@ void viewer::menu_process(function_t pro) void viewer::menu_help(viewer * view) { for(auto & p: view->processes) - if(p.second.name_function != "") - fprintf(stderr, "%s\n", p.second.name_function.c_str()); + if(p.second.function != nullptr) + fprintf(stderr, "%16s: %s\n", ('[' + p.second.key + ']').c_str(), p.second.name.c_str()); } void viewer::menu_reset_mesh(viewer * view) @@ -405,7 +417,7 @@ void viewer::set_render_corr(viewer * view) void viewer::set_is_flat(viewer * view) { - view->is_flat = !view->is_flat; + view->render_flat = !view->render_flat; } void viewer::display() @@ -449,11 +461,11 @@ void viewer::display() cam.setView(); - GLint uniformIsFlat = glGetUniformLocation(shader_program, "is_flat"); - glUniform1i(uniformIsFlat, is_flat); + GLint uniform_render_flat = glGetUniformLocation(shader_program, "render_flat"); + glUniform1i(uniform_render_flat, render_flat); - GLint uniformLines = glGetUniformLocation(shader_program, "lines"); - glUniform1i(uniformLines, render_lines); + GLint uniform_render_lines = glGetUniformLocation(shader_program, "render_lines"); + glUniform1i(uniform_render_lines, render_lines); GLfloat ModelViewMatrix[16]; GLfloat ProjectionMatrix[16]; From 243cc54bc262170a2b21b5dddd88eb2f262c58d9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 20 Dec 2019 12:04:31 +0100 Subject: [PATCH 0068/1018] fix draw patch --- src/mdict/basis.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index ea7f309e..68f66cd0 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -95,7 +95,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) os << "unset key;" << endl; os << "set pm3d at b;" << endl; os << "unset colorbox;" << endl; - os << "splot \"xyz.dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; + os << "splot \"xyz_" << to_string(i) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; for(index_t i = 0; i < m; i++) { From 55ab2742a3b3c36dd1f3553e863bb0d7d407dd7b Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 20 Dec 2019 15:50:54 +0100 Subject: [PATCH 0069/1018] adding normalise --- src/mdict/dictionary.cpp | 2 -- src/mdict/mdict.cpp | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 1302a505..9f241baf 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -55,8 +55,6 @@ void dictionary::learning() KSVD(A, patches, L, K); A.save(f_dict); } - gproshan_debug(Dicti); - gproshan_debug_var(A); } else A.eye(phi_basis->dim, m); gproshan_debug_var(phi_basis->radio); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index e15435a6..cbc1246a 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -190,7 +190,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { - return OMP(p.xyz.row(2).t(), p.phi * A, L); + return OMP(p.xyz.row(2).t(), normalise(p.phi * A), L); } a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) From 37aa4be09f3d0f34194192db21c6272bd8714ddf Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 3 Jan 2020 15:18:10 +0100 Subject: [PATCH 0070/1018] Without normalizing --- include/mdict/inpainting.h | 3 ++- src/mdict/inpainting.cpp | 10 +++++----- src/mdict/mdict.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 74bcf2c0..aeb8aec8 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -22,8 +22,9 @@ class inpainting : public dictionary virtual ~inpainting() = default; distance_t execute(); - void load_mask(std::vector vertices[], geodesics & ptp ); + void load_mask(const std::vector * vertices, const index_t * clusters); void init_patches_disjoint(); + void init_voronoi_sampling(const std::vector * vertices, const index_t * clusters); distance_t execute_tmp(); private: size_t avg_p; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 312b3046..af4f3a01 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -15,7 +15,7 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size mask = new bool[mesh->n_vertices()]; } -void inpainting::load_mask(std::vector vertices[], geodesics & ptp ) +void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) { string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); arma::uvec V; @@ -50,15 +50,15 @@ void inpainting::load_mask(std::vector vertices[], geodesics & ptp ) while( k < M ) { rn = distribution(generator); - if(!mask[rn] && percentages_size[ptp.clusters[rn] ] > 0) + if(!mask[rn] && percentages_size[clusters[rn] ] > 0) { mask[rn] = 1; V(rn) = 1; - percentages_size[ ptp.clusters[rn] ]--; + percentages_size[ clusters[rn] ]--; } - if(percentages_size[ptp.clusters[rn] ] == 0) + if(percentages_size[clusters[rn] ] == 0) k++; } V.save(f_mask); @@ -99,7 +99,7 @@ void inpainting::init_patches_disjoint() vertices[ ptp.clusters[i] ].push_back(i) ; } - load_mask(vertices, ptp); + load_mask(vertices, ptp.clusters); //Initializing patches diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index cbc1246a..e15435a6 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -190,7 +190,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { - return OMP(p.xyz.row(2).t(), normalise(p.phi * A), L); + return OMP(p.xyz.row(2).t(), p.phi * A, L); } a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) From 0307a1802ec082315722411b7ed8b40535a14ac9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 3 Jan 2020 15:35:52 +0100 Subject: [PATCH 0071/1018] ready for new radial sampling --- include/mdict/inpainting.h | 3 +- src/mdict/inpainting.cpp | 67 ++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index aeb8aec8..c359a08f 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -24,7 +24,8 @@ class inpainting : public dictionary distance_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); void init_patches_disjoint(); - void init_voronoi_sampling(const std::vector * vertices, const index_t * clusters); + index_t * init_voronoi_sampling(std::vector * vertices); + index_t * init_radial_sampling(const distance_t & radio, std::vector * vertices); distance_t execute_tmp(); private: size_t avg_p; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index af4f3a01..3be33271 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -65,41 +65,52 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } } +index_t * inpainting::init_radial_sampling(const distance_t & radio, std::vector * vertices) +{ -void inpainting::init_patches_disjoint() +} + +index_t * inpainting::init_voronoi_sampling( std::vector * vertices) { gproshan_log_var(M); - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - // creating disjoint clusters with geodesics aka voronoi -#ifdef GPROSHAN_CUDA - geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); -#else - geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); -#endif - TOC(d_time) - gproshan_log_var(d_time); - - std::vector vertices[M]; + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + // creating disjoint clusters with geodesics aka voronoi + #ifdef GPROSHAN_CUDA + geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); + #else + geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); + #endif + TOC(d_time) + gproshan_log_var(d_time); + + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(sample(s)); + } - //saving first vertex aka seed vertices - #pragma omp for - for(index_t s = 0; s < M; s++) - { - vertices[s].push_back(sample(s)); - } + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ ptp.clusters[i] ].push_back(i) ; + } + return ptp.clusters; + +} - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - ptp.clusters[i]--; - if(sample(ptp.clusters[i]) != i) - vertices[ ptp.clusters[i] ].push_back(i) ; - } +void inpainting::init_patches_disjoint() +{ - load_mask(vertices, ptp.clusters); + std::vector vertices[M]; + + load_mask(vertices, init_voronoi_sampling(vertices)); //Initializing patches From f2bca4b4b941a308b4d07039a0da742021efb979 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 6 Jan 2020 12:19:09 +0100 Subject: [PATCH 0072/1018] returning sampling function --- include/mdict/patch.h | 5 ++++ src/mdict/inpainting.cpp | 64 +++++++++++++++++++++++++++++++++++++--- src/mdict/patch.cpp | 12 ++++++++ 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 1cc6c3d3..94f2f04d 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -57,6 +57,11 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); + void init_radial_disjoint(che * mesh, + const index_t & v, + const size_t & n_toplevels, + vector & _vertices, + index_t * _toplevel = nullptr); void transform(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 3be33271..049bce74 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -30,6 +30,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } else { + V.zeros(mesh->n_vertices()); size_t * percentages_size = new size_t[M]; @@ -39,6 +40,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t for(index_t s = 0; s < M; s++) { percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; + } //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; @@ -48,7 +50,9 @@ void inpainting::load_mask(const std::vector * vertices, const index_t size_t rn=0; while( k < M ) - { + { + //gproshan_debug_var(clusters[rn]); + rn = distribution(generator); if(!mask[rn] && percentages_size[clusters[rn] ] > 0) { @@ -67,12 +71,27 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } index_t * inpainting::init_radial_sampling(const distance_t & radio, std::vector * vertices) { + gproshan_log_var(M); + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + //sampling + size_t count = 0; + + while(count < mesh->n_vertices() ) + { + //Choose a sample and get the points neighboring points + // Check the points are inside and add them + // while( ) + count++; + } } index_t * inpainting::init_voronoi_sampling( std::vector * vertices) { - gproshan_log_var(M); + gproshan_log_var(M); //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) @@ -107,10 +126,47 @@ index_t * inpainting::init_voronoi_sampling( std::vector * vertices) void inpainting::init_patches_disjoint() { - + ///// std::vector vertices[M]; + //index_t * clusters_tmp = init_voronoi_sampling(vertices); + + ////// + gproshan_log_var(M); - load_mask(vertices, init_voronoi_sampling(vertices)); + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + // creating disjoint clusters with geodesics aka voronoi + #ifdef GPROSHAN_CUDA + geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); + #else + geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); + #endif + TOC(d_time) + gproshan_log_var(d_time); + + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(sample(s)); + } + + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ ptp.clusters[i] ].push_back(i) ; + } + //return ptp.clusters; + + + load_mask(vertices, ptp.clusters); + ////// + + //load_mask(vertices, clusters_tmp); //Initializing patches diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 3862cc74..425e2085 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -53,6 +53,18 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } +void patch::init_radial_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +{ + index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; + + gather_vertices(mesh, v, n_toplevels, toplevel); + + vertices = std::move(_vertices); + + if(!_toplevel) delete [] toplevel; // If it is null +} + + // xyz = E.t * (xyz - avg) void patch::transform() { From 31be1368a9b44ac8f62d2f42869c39756e11a0fa Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 6 Jan 2020 16:25:38 +0100 Subject: [PATCH 0073/1018] ordering radial sampling function mask pending --- include/mdict/inpainting.h | 5 ++- include/mdict/patch.h | 1 + src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 67 +++++++++++++------------------------- src/mdict/patch.cpp | 17 +++++++++- 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index c359a08f..ec5b73df 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -23,9 +23,8 @@ class inpainting : public dictionary distance_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); - void init_patches_disjoint(); - index_t * init_voronoi_sampling(std::vector * vertices); - index_t * init_radial_sampling(const distance_t & radio, std::vector * vertices); + void init_voronoi_patches(); + void init_radial_patches(const distance_t & radio); distance_t execute_tmp(); private: size_t avg_p; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 94f2f04d..6811eabd 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -58,6 +58,7 @@ class patch vector & _vertices, index_t * _toplevel = nullptr); void init_radial_disjoint(che * mesh, + const distance_t & radio, const index_t & v, const size_t & n_toplevels, vector & _vertices, diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6a494a4b..04e129c9 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_patches_disjoint(); + dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 049bce74..6f248afe 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -69,43 +69,19 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } } -index_t * inpainting::init_radial_sampling(const distance_t & radio, std::vector * vertices) +void inpainting::init_radial_patches(const distance_t & radio) { + // ensure that M is large enough using the radio gproshan_log_var(M); + std::vector vertices[M]; //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); //sampling - size_t count = 0; - - while(count < mesh->n_vertices() ) - { - //Choose a sample and get the points neighboring points - // Check the points are inside and add them - // while( ) - count++; - } - -} - -index_t * inpainting::init_voronoi_sampling( std::vector * vertices) -{ - gproshan_log_var(M); - - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - // creating disjoint clusters with geodesics aka voronoi - #ifdef GPROSHAN_CUDA - geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); - #else - geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); - #endif - TOC(d_time) - gproshan_log_var(d_time); - + size_t count = 0, s = 0; + patches.resize(M); + patches_map.resize(n_vertices); //saving first vertex aka seed vertices #pragma omp for @@ -114,17 +90,25 @@ index_t * inpainting::init_voronoi_sampling( std::vector * vertices) vertices[s].push_back(sample(s)); } - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - ptp.clusters[i]--; - if(sample(ptp.clusters[i]) != i) - vertices[ ptp.clusters[i] ].push_back(i) ; - } - return ptp.clusters; + while(count < mesh->n_vertices() ) + { + //Choose a sample and get the points neighboring points + // Check the points are inside and add them + // while( ) + // mask at the end + index_t * toplevel = new index_t[mesh->n_vertices()]; + patches[s].init_radial_disjoint(mesh, radio, sample(s), dictionary::T, vertices[s], toplevel); + + count+= patches[s].vertices.size(); + count++; + } + //mask at the end no need to call the function + } -void inpainting::init_patches_disjoint() + +void inpainting::init_voronoi_patches() { ///// std::vector vertices[M]; @@ -160,14 +144,9 @@ void inpainting::init_patches_disjoint() if(sample(ptp.clusters[i]) != i) vertices[ ptp.clusters[i] ].push_back(i) ; } - //return ptp.clusters; load_mask(vertices, ptp.clusters); - ////// - - //load_mask(vertices, clusters_tmp); - //Initializing patches gproshan_log(initializing patches); @@ -228,7 +207,7 @@ void inpainting::init_patches_disjoint() distance_t inpainting::execute() { - TIC(d_time) init_patches_disjoint(); TOC(d_time) + TIC(d_time) init_voronoi_patches(); TOC(d_time) gproshan_debug_var(d_time); // L = 15; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 425e2085..e09d6c04 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -53,12 +53,27 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); + // Implementing radial constraint + //_vertices.push_back(v); + + for(index_t i = 0; i < vertices.size(); i++) + { + vertex n = mesh->normal(v);// normal at the center + vertex p = mesh->get_vertex(vertices[i]); + vertex c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + if(*p <= radio) _vertices.push_back(vertices[i]); + + } + vertices = std::move(_vertices); if(!_toplevel) delete [] toplevel; // If it is null From ffe4dc6931567f5a93cadedff05c23cd80693538 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 7 Jan 2020 10:33:01 +0100 Subject: [PATCH 0074/1018] saving mask radial sampling --- include/mdict/inpainting.h | 1 + src/app_viewer.cpp | 4 +- src/mdict/inpainting.cpp | 158 ++++++++++++++++++++++++++++++------- 3 files changed, 134 insertions(+), 29 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index ec5b73df..28b04767 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -23,6 +23,7 @@ class inpainting : public dictionary distance_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); + void load_mask(const distance_t & radio); void init_voronoi_patches(); void init_radial_patches(const distance_t & radio); distance_t execute_tmp(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 04e129c9..e3fe944e 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -528,7 +528,7 @@ void viewer_process_mask() size_t n=12; // dct size_t m = 144, M = 0; - distance_t f = 3; + distance_t f = 1; bool learn = 0; @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_voronoi_patches(); + dict.init_radial_patches(2.263e-02); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 6f248afe..29ac12b6 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -15,6 +15,53 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size mask = new bool[mesh->n_vertices()]; } + +void inpainting::load_mask(const distance_t & radio) +{ + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); + arma::uvec V; + + if(V.load(f_mask)) + { + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + mask[i] = V(i); + } + } + else + { + + V.zeros(mesh->n_vertices()); + size_t * percentages_size = new size_t[M]; + + int k = 0; + size_t rn = 0; + // create initial desired percentage sizes + #pragma omp for + for(index_t s = 0; s < M; s++) + { + percentages_size[s] = ceil(patches[s].vertices.size() * percent/ 100) ; + //Generate random mask according to a percentage of patches capacity + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, patches[s].vertices.size()); + while(k < percentages_size[s] ) + { + rn = distribution(generator); + if(!mask[rn]) + { + mask[rn] = 1; + V(rn) = 1; + k++; + } + } + } + + V.save(f_mask); + } + +} + void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) { string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); @@ -70,41 +117,98 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } void inpainting::init_radial_patches(const distance_t & radio) -{ + { // ensure that M is large enough using the radio - gproshan_log_var(M); - std::vector vertices[M]; - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); + gproshan_log_var(M); + std::vector vertices[M]; + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); - //sampling - size_t count = 0, s = 0; - patches.resize(M); - patches_map.resize(n_vertices); + //sampling + size_t count = 0, s = 0; + patches.resize(M); + patches_map.resize(n_vertices); + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(sample(s)); + } + + while(count < mesh->n_vertices() ) + { + //Choose a sample and get the points neighboring points + // Check the points are inside and add them + // while( ) + // mask at the end + index_t * toplevel = new index_t[mesh->n_vertices()]; + patches[s].init_radial_disjoint(mesh, radio, sample(s), dictionary::T, vertices[s], toplevel); + s++; + count+= patches[s].vertices.size(); + count++; + } + M = s-1; // updating number of vertices + //mask at the end no need to call the function + patches.resize(M); //??? + + load_mask(radio); + + //Initializing patches + gproshan_log(initializing patches); + + patches.resize(M); + patches_map.resize(n_vertices); + //initializing patch + #pragma omp parallel + { + index_t * toplevel = new index_t[mesh->n_vertices()]; - //saving first vertex aka seed vertices #pragma omp for for(index_t s = 0; s < M; s++) { - vertices[s].push_back(sample(s)); - } + patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); + + } + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; - while(count < mesh->n_vertices() ) - { - //Choose a sample and get the points neighboring points - // Check the points are inside and add them - // while( ) - // mask at the end - index_t * toplevel = new index_t[mesh->n_vertices()]; - patches[s].init_radial_disjoint(mesh, radio, sample(s), dictionary::T, vertices[s], toplevel); - - count+= patches[s].vertices.size(); - count++; - } - //mask at the end no need to call the function - + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < M; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < M; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < M; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= M; + //gproshan_debug_var(patch_avg_size); + //gproshan_debug_var(patch_min_size); + //gproshan_debug_var(patch_max_size); + #endif + } + + bool * pmask = mask; + for(index_t s = 0; s < M; s++) + patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + + #pragma omp parallel for + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + p.transform(); + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + + } + gproshan_log(radial patches are ready); + } From aaccc9c752084e46b295ddeb74a4e2b2d5c723f3 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 7 Jan 2020 12:24:46 +0100 Subject: [PATCH 0075/1018] fixing mask function still covering issues --- src/mdict/inpainting.cpp | 65 +++++++++++++++++++++++++++++++--------- src/mdict/patch.cpp | 4 --- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 29ac12b6..eb3168c1 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -13,6 +13,11 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size percent = _perc; // mask percentage M = mesh->n_vertices()/avg_p; mask = new bool[mesh->n_vertices()]; + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + mask[i] = 0; + } } @@ -20,6 +25,8 @@ void inpainting::load_mask(const distance_t & radio) { string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); arma::uvec V; + gproshan_log(loading radial mask); + if(V.load(f_mask)) { @@ -27,6 +34,7 @@ void inpainting::load_mask(const distance_t & radio) for(index_t i = 0; i < mesh->n_vertices(); i++) { mask[i] = V(i); + //gproshan_debug_var(mask[i]); } } else @@ -35,29 +43,39 @@ void inpainting::load_mask(const distance_t & radio) V.zeros(mesh->n_vertices()); size_t * percentages_size = new size_t[M]; - int k = 0; - size_t rn = 0; + int k; + size_t rn; // create initial desired percentage sizes #pragma omp for for(index_t s = 0; s < M; s++) { + + percentages_size[s] = ceil(patches[s].vertices.size() * percent/ 100) ; //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0, patches[s].vertices.size()); + //if some patch has been already masked then it already counts + for(auto i: patches[s].vertices) + if(mask[i] && percentages_size[s]-1) + { + percentages_size[s]--; + } + + + k = 0; while(k < percentages_size[s] ) { + // gproshan_debug_var(percentages_size[s]); + // gproshan_debug_var(k); rn = distribution(generator); - if(!mask[rn]) - { - mask[rn] = 1; - V(rn) = 1; - k++; - } + mask[rn] = 1; + V(rn) = 1; + k++; } } - V.save(f_mask); + // V.save(f_mask); } } @@ -119,17 +137,19 @@ void inpainting::load_mask(const std::vector * vertices, const index_t void inpainting::init_radial_patches(const distance_t & radio) { // ensure that M is large enough using the radio + gproshan_log(Init radial patches); gproshan_log_var(M); + gproshan_log_var(radio); std::vector vertices[M]; //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); //sampling - size_t count = 0, s = 0; + size_t count = 0, s; patches.resize(M); patches_map.resize(n_vertices); - + //saving first vertex aka seed vertices #pragma omp for for(index_t s = 0; s < M; s++) @@ -137,18 +157,33 @@ void inpainting::init_radial_patches(const distance_t & radio) vertices[s].push_back(sample(s)); } - while(count < mesh->n_vertices() ) + bool covered[mesh->n_vertices()]; + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + + s = 0; + while(count < mesh->n_vertices() && s < M) { + //Choose a sample and get the points neighboring points // Check the points are inside and add them // while( ) // mask at the end index_t * toplevel = new index_t[mesh->n_vertices()]; patches[s].init_radial_disjoint(mesh, radio, sample(s), dictionary::T, vertices[s], toplevel); + for(auto i:patches[s].vertices) + if(!covered[i]) + { + covered[i] = 1; + count++; + } s++; - count+= patches[s].vertices.size(); - count++; } + gproshan_debug(finished); + M = s-1; // updating number of vertices //mask at the end no need to call the function patches.resize(M); //??? @@ -161,6 +196,7 @@ void inpainting::init_radial_patches(const distance_t & radio) patches.resize(M); patches_map.resize(n_vertices); //initializing patch + gproshan_debug_var(M); #pragma omp parallel { index_t * toplevel = new index_t[mesh->n_vertices()]; @@ -168,6 +204,7 @@ void inpainting::init_radial_patches(const distance_t & radio) #pragma omp for for(index_t s = 0; s < M; s++) { + patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index e09d6c04..caa3e4d3 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -59,9 +59,6 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const ind gather_vertices(mesh, v, n_toplevels, toplevel); - // Implementing radial constraint - //_vertices.push_back(v); - for(index_t i = 0; i < vertices.size(); i++) { vertex n = mesh->normal(v);// normal at the center @@ -75,7 +72,6 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const ind } vertices = std::move(_vertices); - if(!_toplevel) delete [] toplevel; // If it is null } From 48937e73576edeee5961a39128bcda1c04e045c1 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 7 Jan 2020 16:30:03 +0100 Subject: [PATCH 0076/1018] fixing mask --- src/app_viewer.cpp | 4 ++-- src/mdict/inpainting.cpp | 39 ++++++++++++++++++++++----------------- src/mdict/patch.cpp | 3 ++- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index e3fe944e..7de477f4 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,8 +538,8 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_patches(2.263e-02); - + //dict.init_radial_patches(1.163e-02); + dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index eb3168c1..43b6f632 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -34,7 +34,6 @@ void inpainting::load_mask(const distance_t & radio) for(index_t i = 0; i < mesh->n_vertices(); i++) { mask[i] = V(i); - //gproshan_debug_var(mask[i]); } } else @@ -49,33 +48,36 @@ void inpainting::load_mask(const distance_t & radio) #pragma omp for for(index_t s = 0; s < M; s++) { - - - percentages_size[s] = ceil(patches[s].vertices.size() * percent/ 100) ; + percentages_size[s] = patches[s].vertices.size() - (patches[s].vertices.size() * (percent/ 100)) ; //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0, patches[s].vertices.size()); //if some patch has been already masked then it already counts for(auto i: patches[s].vertices) - if(mask[i] && percentages_size[s]-1) + if(mask[i] && percentages_size[s]) { percentages_size[s]--; } k = 0; + // gproshan_debug_var(percentages_size[s]); + // gproshan_debug_var(patches[s].vertices.size()); while(k < percentages_size[s] ) { - // gproshan_debug_var(percentages_size[s]); - // gproshan_debug_var(k); + //gproshan_debug_var(percentages_size[s]); + rn = distribution(generator); - mask[rn] = 1; - V(rn) = 1; + mask[patches[s].vertices[rn]] = 1; + V(patches[s].vertices[rn]) = 1; k++; + } } - - // V.save(f_mask); + /* for(index_t s = 0; s < n_vertices; s++) + gproshan_debug_var(mask[s]); +*/ + V.save(f_mask); } } @@ -104,7 +106,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t #pragma omp for for(index_t s = 0; s < M; s++) { - percentages_size[s] = ceil(vertices[s].size() * percent/ 100) ; + percentages_size[s] = vertices[s].size() * (percent/ 100) ; } //Generate random mask according to a percentage of patches capacity @@ -130,7 +132,10 @@ void inpainting::load_mask(const std::vector * vertices, const index_t if(percentages_size[clusters[rn] ] == 0) k++; } - V.save(f_mask); + for(index_t s = 0; s < n_vertices; s++) + gproshan_debug_var(V(s)); + //V.save(f_mask); + } } @@ -201,13 +206,13 @@ void inpainting::init_radial_patches(const distance_t & radio) { index_t * toplevel = new index_t[mesh->n_vertices()]; - #pragma omp for + /*#pragma omp for for(index_t s = 0; s < M; s++) { patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); - } + } */ #ifndef NDEBUG size_t patch_avg_size = 0; size_t patch_min_size = NIL; @@ -229,11 +234,11 @@ void inpainting::init_radial_patches(const distance_t & radio) //gproshan_debug_var(patch_max_size); #endif } - + bool * pmask = mask; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - + gproshan_debug(passed); #pragma omp parallel for for(index_t s = 0; s < M; s++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index caa3e4d3..c54c32d0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -123,13 +123,14 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); - +// gproshan_debug_var(m); if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; } +// gproshan_debug_var(m); xyz.set_size(3, m); for(index_t j = 0, i = 0; i < vertices.size(); i++) From 3a577582730d9b04117369219cfd42e170abed99 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 8 Jan 2020 12:18:08 +0100 Subject: [PATCH 0077/1018] load mask fixed (not radial) --- include/mdict/patch.h | 1 + src/mdict/inpainting.cpp | 31 ++++++++++++++++++++----------- src/mdict/patch.cpp | 13 +++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 6811eabd..3b8659e9 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -75,6 +75,7 @@ class patch ); void reset_xyz_disjoint( che * mesh, distance_t * dist, + size_t M, std::vector & vpatches, const index_t & p, const fmask_t & mask = nullptr diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 43b6f632..6dad0d70 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -48,7 +48,7 @@ void inpainting::load_mask(const distance_t & radio) #pragma omp for for(index_t s = 0; s < M; s++) { - percentages_size[s] = patches[s].vertices.size() - (patches[s].vertices.size() * (percent/ 100)) ; + percentages_size[s] = patches[s].vertices.size() - (patches[s].vertices.size() * (percent/ 100.0)) ; //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0, patches[s].vertices.size()); @@ -94,27 +94,32 @@ void inpainting::load_mask(const std::vector * vertices, const index_t { mask[i] = V(i); } + } else { V.zeros(mesh->n_vertices()); size_t * percentages_size = new size_t[M]; + bool cover_cluster[M]; // create initial desired percentage sizes #pragma omp for for(index_t s = 0; s < M; s++) { - percentages_size[s] = vertices[s].size() * (percent/ 100) ; + + percentages_size[s] = ceil(vertices[s].size() * (percent/ 100.0)) ; + cover_cluster[s] = 0; } //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; - std::uniform_int_distribution distribution(0,M); + std::uniform_int_distribution distribution(0,n_vertices-1); int k = 0; size_t rn=0; + while( k < M ) { @@ -126,15 +131,19 @@ void inpainting::load_mask(const std::vector * vertices, const index_t mask[rn] = 1; V(rn) = 1; - - percentages_size[ clusters[rn] ]--; + percentages_size[ clusters[rn] ]--; + } - if(percentages_size[clusters[rn] ] == 0) + if( !cover_cluster[ clusters[rn] ] && percentages_size[clusters[rn] ] == 0) + { + cover_cluster[ clusters[rn]] = 1; // It is finished k++; + } + } - for(index_t s = 0; s < n_vertices; s++) - gproshan_debug_var(V(s)); - //V.save(f_mask); + + + V.save(f_mask); } @@ -237,7 +246,7 @@ void inpainting::init_radial_patches(const distance_t & radio) bool * pmask = mask; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); gproshan_debug(passed); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -334,7 +343,7 @@ void inpainting::init_voronoi_patches() bool * pmask = mask; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); #pragma omp parallel for for(index_t s = 0; s < M; s++) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index c54c32d0..486a27d6 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -111,7 +111,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & xyz(0, j) = v.x; xyz(1, j) = v.y; xyz(2, j) = v.z; - //p idx patche where belongs to + //p idx patche where belongs to //j: local index //i: global index //if(vpatches[vertices[i]].size() == 0) @@ -120,15 +120,20 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } -void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); -// gproshan_debug_var(m); +// gproshan_debug_var(m); if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) - if(mask(i)) { dist[vertices[i] ] = float(p + 1) / 4804; m++; } else {dist[vertices[i] ] = INFINITY;}; + if(mask(i)) { dist[vertices[i] ] = float(p + 1) / M; m++; } else {dist[vertices[i] ] = INFINITY; }; + + gproshan_debug(number vertices considered); + gproshan_debug_var(m); + gproshan_debug(number vertices masked); + gproshan_debug_var(vertices.size() - m); } // gproshan_debug_var(m); From e00ba6b403f26504fdbb4657177460342f4ee7a9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 8 Jan 2020 12:35:48 +0100 Subject: [PATCH 0078/1018] fixed radial mask --- src/app_viewer.cpp | 4 ++-- src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 2 -- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7de477f4..57c72f1b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,8 +538,8 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - //dict.init_radial_patches(1.163e-02); - dict.init_voronoi_patches(); + dict.init_radial_patches(1.163e-02); + //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 6dad0d70..ca26d271 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -48,7 +48,7 @@ void inpainting::load_mask(const distance_t & radio) #pragma omp for for(index_t s = 0; s < M; s++) { - percentages_size[s] = patches[s].vertices.size() - (patches[s].vertices.size() * (percent/ 100.0)) ; + percentages_size[s] = patches[s].vertices.size() - ceil(patches[s].vertices.size() * (percent/ 100.0)) ; //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0, patches[s].vertices.size()); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 486a27d6..6c5ce122 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -123,7 +123,6 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); -// gproshan_debug_var(m); if(mask) { m = 0; @@ -135,7 +134,6 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Wed, 8 Jan 2020 14:57:04 +0100 Subject: [PATCH 0079/1018] radial sampling fixed --- src/mdict/inpainting.cpp | 8 +++++++- src/mdict/patch.cpp | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ca26d271..1f113ae4 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -148,6 +148,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } } + void inpainting::init_radial_patches(const distance_t & radio) { // ensure that M is large enough using the radio @@ -196,9 +197,12 @@ void inpainting::init_radial_patches(const distance_t & radio) } s++; } + gproshan_debug_var(M); + gproshan_debug_var(s); + gproshan_debug(finished); - M = s-1; // updating number of vertices + M = s; // updating number of vertices //mask at the end no need to call the function patches.resize(M); //??? @@ -247,7 +251,9 @@ void inpainting::init_radial_patches(const distance_t & radio) bool * pmask = mask; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + gproshan_debug(passed); + #pragma omp parallel for for(index_t s = 0; s < M; s++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 6c5ce122..8a108a95 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -70,7 +70,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const ind if(*p <= radio) _vertices.push_back(vertices[i]); } - + jet_fit_directions(mesh, v); vertices = std::move(_vertices); if(!_toplevel) delete [] toplevel; // If it is null } @@ -129,10 +129,10 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Wed, 8 Jan 2020 16:12:34 +0100 Subject: [PATCH 0080/1018] adding a random mask --- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 23 ++++++++++++++++++++++- src/mdict/patch.cpp | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 57c72f1b..5ae73d53 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_patches(1.163e-02); + dict.init_radial_patches(1.663e-02); //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 1f113ae4..48e91d6c 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -23,7 +23,8 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size void inpainting::load_mask(const distance_t & radio) { - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); + //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -38,7 +39,27 @@ void inpainting::load_mask(const distance_t & radio) } else { + V.zeros(mesh->n_vertices()); + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); + size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (percent/ 100.0)) ; + int k = 0; + size_t rn = 0; + while (k < percentage) + { + + rn = distribution(generator); + if(!mask[rn]) + { + mask[rn] = 1; + V(rn) = 1; + k++; + } + + } + /* +///////////////////////// V.zeros(mesh->n_vertices()); size_t * percentages_size = new size_t[M]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8a108a95..6657794e 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -129,7 +129,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Thu, 9 Jan 2020 12:38:37 +0100 Subject: [PATCH 0081/1018] radial sampling fixed --- include/mdict/basis.h | 1 + include/mdict/inpainting.h | 4 ++-- src/app_viewer.cpp | 2 +- src/mdict/basis.cpp | 5 +++++ src/mdict/inpainting.cpp | 11 +++++------ 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 7b605f9f..6ec3416e 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -29,6 +29,7 @@ class basis void plot_atoms(const a_mat & A); void plot_patch(const a_mat & A, const a_mat & xyz, index_t i); size_t get_dim(); + distance_t get_radio(); private: virtual void plot_basis(std::ostream & os) = 0; diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 28b04767..6638d706 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -23,9 +23,9 @@ class inpainting : public dictionary distance_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); - void load_mask(const distance_t & radio); + void load_mask(); void init_voronoi_patches(); - void init_radial_patches(const distance_t & radio); + void init_radial_patches(); distance_t execute_tmp(); private: size_t avg_p; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 5ae73d53..362d2202 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_patches(1.663e-02); + dict.init_radial_patches(); //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 68f66cd0..f9a1d427 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -72,6 +72,11 @@ size_t basis::get_dim() return dim; } +distance_t basis::get_radio() +{ + return radio; +} + void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) { string data = tmp_file_path("xyz_" + to_string(i) + ".dat"); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 48e91d6c..c5b3b76d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -21,7 +21,7 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size } -void inpainting::load_mask(const distance_t & radio) +void inpainting::load_mask() { //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); @@ -170,12 +170,11 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } -void inpainting::init_radial_patches(const distance_t & radio) +void inpainting::init_radial_patches() { // ensure that M is large enough using the radio gproshan_log(Init radial patches); gproshan_log_var(M); - gproshan_log_var(radio); std::vector vertices[M]; //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) @@ -209,7 +208,7 @@ void inpainting::init_radial_patches(const distance_t & radio) // while( ) // mask at the end index_t * toplevel = new index_t[mesh->n_vertices()]; - patches[s].init_radial_disjoint(mesh, radio, sample(s), dictionary::T, vertices[s], toplevel); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(s), dictionary::T, vertices[s], toplevel); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -227,7 +226,7 @@ void inpainting::init_radial_patches(const distance_t & radio) //mask at the end no need to call the function patches.resize(M); //??? - load_mask(radio); + load_mask(); //Initializing patches gproshan_log(initializing patches); @@ -389,7 +388,7 @@ void inpainting::init_voronoi_patches() distance_t inpainting::execute() { - TIC(d_time) init_voronoi_patches(); TOC(d_time) + TIC(d_time) init_radial_patches(); TOC(d_time) gproshan_debug_var(d_time); // L = 15; From d28dc926a0bfe64a324c690f3572092194f6ff68 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 9 Jan 2020 14:23:09 +0100 Subject: [PATCH 0082/1018] fixed init radial patches --- src/app_viewer.cpp | 4 ++-- src/mdict/inpainting.cpp | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 362d2202..4780abd0 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -532,8 +532,8 @@ void viewer_process_mask() bool learn = 0; - gproshan_input(avg_p percentage ); - cin >> avg_p >> percentage; + gproshan_input(avg_p percentage f ); + cin >> avg_p >> percentage >> f; basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index c5b3b76d..a1f37df6 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -200,6 +200,7 @@ void inpainting::init_radial_patches() } s = 0; + index_t * toplevel = new index_t[mesh->n_vertices()]; while(count < mesh->n_vertices() && s < M) { @@ -207,7 +208,7 @@ void inpainting::init_radial_patches() // Check the points are inside and add them // while( ) // mask at the end - index_t * toplevel = new index_t[mesh->n_vertices()]; + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(s), dictionary::T, vertices[s], toplevel); for(auto i:patches[s].vertices) if(!covered[i]) @@ -217,6 +218,7 @@ void inpainting::init_radial_patches() } s++; } + assert(count == mesh->n_vertices()); gproshan_debug_var(M); gproshan_debug_var(s); @@ -262,9 +264,9 @@ void inpainting::init_radial_patches() patch_max_size = max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= M; - //gproshan_debug_var(patch_avg_size); - //gproshan_debug_var(patch_min_size); - //gproshan_debug_var(patch_max_size); + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); #endif } From b40e4de675099d538556fb26bdc1aa437e7156d4 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 9 Jan 2020 14:55:18 +0100 Subject: [PATCH 0083/1018] computing average size for each patch --- include/mdict/patch.h | 2 ++ src/mdict/inpainting.cpp | 1 + src/mdict/patch.cpp | 9 +++++++++ 3 files changed, 12 insertions(+) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 3b8659e9..b33d5a92 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -38,6 +38,7 @@ class patch a_vec x; ///< Center point. a_mat xyz; ///< Matrix of points. a_mat phi; + double avg_dist; // Average distance betweenn points in a patch public: static size_t expected_nv; ///< Expected number of patch vertices. @@ -84,6 +85,7 @@ class patch void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); + void compute_avg_distance(); private: diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a1f37df6..a742a10c 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -284,6 +284,7 @@ void inpainting::init_radial_patches() p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); + p.compute_avg_distance(); } gproshan_log(radial patches are ready); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 6657794e..bde98a07 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -335,6 +335,15 @@ void patch::save_z(ostream & os) os< Date: Thu, 9 Jan 2020 16:15:09 +0100 Subject: [PATCH 0084/1018] frequency function ready --- include/mdict/basis.h | 2 ++ include/mdict/basis_dct.h | 3 ++- src/mdict/basis.cpp | 1 + src/mdict/basis_dct.cpp | 7 ++++++- 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 6ec3416e..d439153f 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -22,6 +22,7 @@ class basis distance_t radio; size_t dim; + public: virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_mat & xy) = 0; @@ -30,6 +31,7 @@ class basis void plot_patch(const a_mat & A, const a_mat & xyz, index_t i); size_t get_dim(); distance_t get_radio(); + virtual double get_frequency(size_t idx) = 0; private: virtual void plot_basis(std::ostream & os) = 0; diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index a56110ae..c09ce501 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -12,11 +12,12 @@ namespace gproshan::mdict { class basis_dct: public basis { private: - real_t n; // frequency + int n; // int frequency public: basis_dct(const size_t & _n, const distance_t & _radio = 0); void discrete(a_mat & phi, const a_mat & xy); + double get_frequency(size_t idx); private: void plot_basis(std::ostream & os); diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index f9a1d427..fe8b24e7 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -77,6 +77,7 @@ distance_t basis::get_radio() return radio; } + void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) { string data = tmp_file_path("xyz_" + to_string(i) + ".dat"); diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index 8bcd94d7..c66f140f 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -58,7 +58,12 @@ void basis_dct::dct(ostream & os, const index_t & nx, const index_t & ny) os << "cos( (pi * v * cos(u) * " << nx << " ) / " << radio << " ) *"; os << "cos( (pi * v * sin(u) * " << ny << " ) / " << radio << " )"; } - +double basis_dct::get_frequency(size_t idx) +{ + if(idx == 0 ) return INFINITY; + int tmp = (idx/n > idx%n)? idx/n:idx%n; + return (radio/tmp); +} } // namespace gproshan::mdict From 1d9c0d9a778c6f7154a939aa3236a6bfaf2352a7 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 10 Jan 2020 16:06:27 +0100 Subject: [PATCH 0085/1018] adding a new omp with mask --- include/mdict/basis.h | 2 +- include/mdict/mdict.h | 6 +++++ src/mdict/mdict.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index d439153f..fd365a5d 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -14,7 +14,7 @@ using namespace std; namespace gproshan::mdict { -class dictionary; +class dictionary; class basis { diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index 5ff943b3..99ace93b 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -4,6 +4,8 @@ #include "include.h" #include "patch.h" #include "d_mesh.h" +#include "basis.h" + #include "include_arma.h" @@ -35,6 +37,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L); a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L); +a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask); a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L); @@ -43,9 +46,12 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); // MESH DENSE +a_vec OMP(const patch & p, const a_mat & A, const size_t & L); a_vec OMP(const patch & p, const a_mat & A, const size_t & L); a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L); +a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, const size_t & L); + void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index e15435a6..2ea7bc43 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -135,6 +135,37 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L return {aa, selected_atoms.head(l)}; } +arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) +{ + + arma::uvec indices = arma::sort_index( V , "desscend"); + + for(int i=0; i< V.size(); i++) + if(mask[ indices [i]]) return indices[i]; + +} + +tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) +{ + arma::uvec selected_atoms(L); + real_t threshold = norm(x) * sigma; + + a_mat DD; + a_vec aa, r = x; + + index_t l = 0; + while(norm(r) > threshold && l < L) + { + selected_atoms(l) = max_index(abs(D.t() * r), mask); + + DD = D.cols(selected_atoms.head(l + 1)); + aa = pinv(DD) * x; + r = x - DD * aa; + l++; + } + return {aa, selected_atoms.head(l)}; +} + a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) { a_vec alpha(D.n_cols, arma::fill::zeros); @@ -147,6 +178,18 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) return alpha; } +a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) +{ + a_vec alpha(D.n_cols, arma::fill::zeros); + a_vec aa; + arma::uvec selected_atoms; + + tie(aa, selected_atoms) = _OMP(x, D, L, mask); + alpha.elem(selected_atoms) = aa; + + return alpha; +} + a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { a_mat alpha(D.n_cols, X.n_cols); @@ -192,6 +235,26 @@ a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { return OMP(p.xyz.row(2).t(), p.phi * A, L); } +a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) +{ + arma::uchar_vec mask; + mask.ones(A.n_cols); + for(int i = 0; i < A.n_cols; i++) + if( phi_basis->get_frequency(i) >= p.avg_dist) mask(i) = 0; + + return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); +} + +a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, const size_t & L) +{ + a_mat alpha(A.n_cols, patches.size()); + + #pragma omp parallel for + for(index_t i = 0; i < patches.size(); i++) + alpha.col(i) = OMP(patches[i],phi_basis, A, L); + + return alpha; +} a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) { From a5a79c8fe86d9fcdd12c0cc659707b50e10410d0 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 13 Jan 2020 15:57:05 +0100 Subject: [PATCH 0086/1018] fixing basis frequency threshold --- src/mdict/basis_dct.cpp | 2 +- src/mdict/inpainting.cpp | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index c66f140f..daf25868 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -62,7 +62,7 @@ double basis_dct::get_frequency(size_t idx) { if(idx == 0 ) return INFINITY; int tmp = (idx/n > idx%n)? idx/n:idx%n; - return (radio/tmp); + return (2*radio/tmp); } } // namespace gproshan::mdict diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a742a10c..6bd6a49c 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -425,7 +425,11 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); - phi_basis->plot_basis(); + draw_patches(200); + draw_patches(500); + draw_patches(56); + gproshan_debug_var(patches[56].vertices[0]); + //phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); From 0194cd8daecb1d42879867c933d0fa2aa473e095 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 13 Jan 2020 16:45:56 +0100 Subject: [PATCH 0087/1018] fixing frequency threshold --- src/mdict/dictionary.cpp | 2 +- src/mdict/mdict.cpp | 4 ++-- src/mdict/patch.cpp | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 9f241baf..cb45c407 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -72,7 +72,7 @@ void dictionary::sparse_coding() gproshan_log(MDICT); vector locval; - alpha = OMP_all(patches, A, L); + alpha = OMP_all(patches, phi_basis, A, L); } void dictionary::init_sampling() diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 2ea7bc43..a5c09c13 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -238,9 +238,9 @@ a_vec OMP(const patch & p, const a_mat & A, const size_t & L) a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) { arma::uchar_vec mask; - mask.ones(A.n_cols); + mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= p.avg_dist) mask(i) = 0; + if( phi_basis->get_frequency(i) >= 100*p.avg_dist) mask(i) = 1; return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index bde98a07..9dc8a38c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -340,9 +340,10 @@ void patch::compute_avg_distance() avg_dist = 0; for(int i = 0; i < vertices.size(); i++) for(int j = i+1; j < vertices.size(); j++) - avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); + if(avg_dist > norm(xyz.col(i)- xyz.col(j))) avg_dist = norm(xyz.col(i)- xyz.col(j)); + //avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); - avg_dist = avg_dist/vertices.size(); + //avg_dist = avg_dist/vertices.size(); } } // namespace gproshan::mdict From c9f8e1f42a096c65a8d27087d17c94f63b8a4e4f Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 14 Jan 2020 12:35:35 +0100 Subject: [PATCH 0088/1018] fixing frequency --- src/mdict/inpainting.cpp | 5 +++-- src/mdict/mdict.cpp | 7 ++++++- src/mdict/patch.cpp | 24 ++++++++++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 6bd6a49c..843ab7b0 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -226,7 +226,6 @@ void inpainting::init_radial_patches() M = s; // updating number of vertices //mask at the end no need to call the function - patches.resize(M); //??? load_mask(); @@ -282,9 +281,10 @@ void inpainting::init_radial_patches() patch & p = patches[s]; p.transform(); + p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - p.compute_avg_distance(); + } gproshan_log(radial patches are ready); @@ -382,6 +382,7 @@ void inpainting::init_voronoi_patches() p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); + p.compute_avg_distance(); } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index a5c09c13..7ecc3781 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -240,7 +240,12 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 100*p.avg_dist) mask(i) = 1; + if( phi_basis->get_frequency(i) >= 2.5*p.avg_dist) + { + // gproshan_debug_var(phi_basis->get_frequency(i)); + // gproshan_debug_var(p.avg_dist); + mask(i) = 1; + } return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 9dc8a38c..f94ef856 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -59,7 +59,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const ind gather_vertices(mesh, v, n_toplevels, toplevel); - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 1; i < vertices.size(); i++) { vertex n = mesh->normal(v);// normal at the center vertex p = mesh->get_vertex(vertices[i]); @@ -144,7 +144,6 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector norm(xyz.col(i)- xyz.col(j))) avg_dist = norm(xyz.col(i)- xyz.col(j)); + { + a_vec a = xyz.col(i); + a_vec b = xyz.col(j); + a(2) = 0; + b(2) = 0; + + if(avg_dist > norm(a - b)) + { + avg_dist = norm(a - b); + } + /*if(approx_equal(a,b,"absdiff", 0.0000001) ) + { + gproshan_debug_var(i); + gproshan_debug_var(j); + } */ + + } + //avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); //avg_dist = avg_dist/vertices.size(); From dfb43dd00fb25e88b6347bd447a381606e1acad1 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 15 Jan 2020 17:17:26 +0100 Subject: [PATCH 0089/1018] fixing non function patch init radial patches --- include/mdict/patch.h | 1 + src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 48 +++++++++++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index b33d5a92..b957fb80 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -60,6 +60,7 @@ class patch index_t * _toplevel = nullptr); void init_radial_disjoint(che * mesh, const distance_t & radio, + const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 843ab7b0..eab84d08 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -209,7 +209,7 @@ void inpainting::init_radial_patches() // while( ) // mask at the end - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(s), dictionary::T, vertices[s], toplevel); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); for(auto i:patches[s].vertices) if(!covered[i]) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f94ef856..cf879017 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -53,25 +53,38 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; - - gather_vertices(mesh, v, n_toplevels, toplevel); - - for(index_t i = 1; i < vertices.size(); i++) + distance_t *distances; + geodesics geo(mesh, {v}, geodesics::FM, distances, false, 2*avg_p); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + gproshan_debug_var(v); + gproshan_debug_var(geo.n_sorted_index()); + + //gather_vertices(mesh, v, n_toplevels, toplevel); + distance_t frontier = INFINITY; gproshan_debug_var(vertices.size()); + for(int i=1; inormal(v);// normal at the center - vertex p = mesh->get_vertex(vertices[i]); + vertex p = mesh->get_vertex(indexes[i]); vertex c = mesh->get_vertex(v); // central vertices p = p - c ; p = p - ((p,n)*n); - if(*p <= radio) _vertices.push_back(vertices[i]); - + gproshan_debug_var(frontier); + gproshan_debug_var(*p); + if(*p <= radio && *p <=frontier) + { + _vertices.push_back(indexes[i]); + frontier = *p; + } } + gproshan_debug_var(_vertices.size()); jet_fit_directions(mesh, v); vertices = std::move(_vertices); + if(!_toplevel) delete [] toplevel; // If it is null } @@ -337,6 +350,7 @@ void patch::save_z(ostream & os) void patch::compute_avg_distance() { avg_dist = INFINITY; + vector distances; for(int i = 0; i < vertices.size(); i++) for(int j = i+1; j < vertices.size(); j++) { @@ -344,8 +358,9 @@ void patch::compute_avg_distance() a_vec b = xyz.col(j); a(2) = 0; b(2) = 0; + distances.push_back(norm(a - b)); - if(avg_dist > norm(a - b)) + /* if(avg_dist > norm(a - b)) { avg_dist = norm(a - b); } @@ -356,11 +371,18 @@ void patch::compute_avg_distance() } */ } - + sort(distances.begin(), distances.end()); + size_t n_elem = distances.size(); + if(distances.size()%2 ==0) + { + avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1) ])/2; + } + else + { + avg_dist = distances[n_elem/2]; + } //avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); - - //avg_dist = avg_dist/vertices.size(); - } + //avg_dist = avg_dist/vertices.size(); } // namespace gproshan::mdict From 5d170c5b05029a74fd8d42a0c41e1dfecb73972c Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 16 Jan 2020 17:45:55 +0100 Subject: [PATCH 0090/1018] fixing radio v0.1 testing --- src/mdict/inpainting.cpp | 2 ++ src/mdict/patch.cpp | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index eab84d08..30e03852 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -218,6 +218,8 @@ void inpainting::init_radial_patches() } s++; } + gproshan_debug_var(count); + gproshan_debug_var(mesh->n_vertices()); assert(count == mesh->n_vertices()); gproshan_debug_var(M); gproshan_debug_var(s); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index cf879017..82e731a9 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -57,14 +57,12 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz { index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; distance_t *distances; - geodesics geo(mesh, {v}, geodesics::FM, distances, false, 2*avg_p); + geodesics geo(mesh, {v}, geodesics::FM, distances, false, 10*avg_p); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); - gproshan_debug_var(v); - gproshan_debug_var(geo.n_sorted_index()); //gather_vertices(mesh, v, n_toplevels, toplevel); - distance_t frontier = INFINITY; gproshan_debug_var(vertices.size()); + distance_t frontier = -1; for(int i=1; inormal(v);// normal at the center @@ -73,17 +71,18 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz p = p - c ; p = p - ((p,n)*n); - gproshan_debug_var(frontier); - gproshan_debug_var(*p); - if(*p <= radio && *p <=frontier) + if(*p <= radio) _vertices.push_back(indexes[i]); + /* + if(*p <= radio && *p >=frontier) { _vertices.push_back(indexes[i]); frontier = *p; - } + }*/ } - gproshan_debug_var(_vertices.size()); - jet_fit_directions(mesh, v); + vertices = std::move(_vertices); + //gproshan_debug_var(vertices.size()); + jet_fit_directions(mesh, v); if(!_toplevel) delete [] toplevel; // If it is null } @@ -141,7 +140,6 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector min_points); + vector in_points; in_points.reserve(vertices.size()); From 253c603092e95def904f8bdd1c75023c1f40a740 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jan 2020 20:34:33 +0100 Subject: [PATCH 0091/1018] update imgui cmake and log messages --- CMakeLists.txt | 8 ++++++-- src/viewer/viewer.cpp | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 74e5a19e..5e189c81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,10 +52,13 @@ include_directories(${gproshan_SOURCE_DIR}/include/mdict) include_directories(${gproshan_SOURCE_DIR}/include/cuda) include_directories(${gproshan_SOURCE_DIR}/imgui) -FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp ${gproshan_SOURCE_DIR}/imgui/*.cpp) +FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) +add_library(imgui STATIC ${imgui_sources}) + +FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) add_library(gproshan_cpp STATIC ${cpp_sources}) -target_link_libraries(gproshan_cpp ${OpenMP_CXX_LIBRARIES}) +target_link_libraries(gproshan_cpp ${OpenMP_CXX}) target_link_libraries(gproshan_cpp OpenGL::GL) target_link_libraries(gproshan_cpp OpenGL::GLU) target_link_libraries(gproshan_cpp GLEW::GLEW) @@ -64,6 +67,7 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan_cpp imgui) if(CUDA_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 6d3940cb..987fd166 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -230,6 +230,7 @@ void viewer::add_mesh(const vector & _meshes) { assert(n_meshes < N_MESHES); meshes[n_meshes++].init(_mesh); + meshes[n_meshes - 1].log_info(); } if(!n_meshes) return; @@ -765,7 +766,7 @@ void viewer::draw_gradient_field() void viewer::pick_vertex(int x, int y) { - gproshan_debug(VIEWER); + gproshan_log(VIEWER); int width, height; glfwGetFramebufferSize(window, &width, &height); @@ -819,7 +820,7 @@ void viewer::pick_vertex(int x, int y) { select_vertices.push_back(index); - gproshan_debug_var(index); + gproshan_log_var(index); gproshan_debug_var(mesh().color(index)); gproshan_debug_var(mesh()->evt(index)); From 8fbf1f33ff1ae5881fee3bf1fb810f5f01e3cb26 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 17 Jan 2020 15:02:20 +0100 Subject: [PATCH 0092/1018] fixing points out the radius --- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 7ecc3781..fcb4bd3e 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -240,7 +240,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 2.5*p.avg_dist) + if( phi_basis->get_frequency(i) >= p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 82e731a9..8323383a 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -56,8 +56,11 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; - distance_t *distances; - geodesics geo(mesh, {v}, geodesics::FM, distances, false, 10*avg_p); + + gather_vertices(mesh, v, n_toplevels, toplevel); + jet_fit_directions(mesh, v); + + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 5*avg_p); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -65,24 +68,25 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz distance_t frontier = -1; for(int i=1; inormal(v);// normal at the center + // gproshan_debug_var(geo[indexes[i]]); + a_vec vn = T.col(2);// normal at the center + vertex n; + n.x = vn(0); n.y = vn(1); n.z = vn(2); vertex p = mesh->get_vertex(indexes[i]); vertex c = mesh->get_vertex(v); // central vertices p = p - c ; p = p - ((p,n)*n); - if(*p <= radio) _vertices.push_back(indexes[i]); - /* + //if(*p <= radio) _vertices.push_back(indexes[i]); + if(*p <= radio && *p >=frontier) { _vertices.push_back(indexes[i]); frontier = *p; - }*/ + } } vertices = std::move(_vertices); - //gproshan_debug_var(vertices.size()); - jet_fit_directions(mesh, v); if(!_toplevel) delete [] toplevel; // If it is null } From 797d79310b1ef15b57517e80476a91fd580b2f3f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 20 Jan 2020 11:02:22 +0100 Subject: [PATCH 0093/1018] testing fill holes algorithms --- include/che_fill_hole.h | 13 +++++++------ include/viewer/che_viewer.h | 1 + src/che_fill_hole.cpp | 28 ++-------------------------- 3 files changed, 10 insertions(+), 32 deletions(-) diff --git a/include/che_fill_hole.h b/include/che_fill_hole.h index 937ef865..1bebbca2 100644 --- a/include/che_fill_hole.h +++ b/include/che_fill_hole.h @@ -39,16 +39,17 @@ struct border_t a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & lenght, const std::array & neighbors, const bool & o) { index_t p_v = neighbors[!o]; + index_t n_v = neighbors[o]; a_vec a = V[p_v] - V[v]; + a_vec b = V[n_v] - V[v]; + + a(2) = b(2) = 0; - a_vec r(3); - r[0] = cos(theta * div + atan2(a[1], a[0])); - r[1] = sin(theta * div + atan2(a[1], a[0])); + a_vec r = div * a + (1 - div) * b; - r = lenght * normalise(r); - r += V[v]; - r[2] = 0; + r = lenght * normalise(r) + V[v]; + r(2) = 0; return r; } diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index b93cdb74..05f89b4b 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -39,6 +39,7 @@ class che_viewer public: int vx, vy; ///< viewport positions. + std::vector selected; public: che_viewer(); diff --git a/src/che_fill_hole.cpp b/src/che_fill_hole.cpp index 3978647e..663e24b7 100644 --- a/src/che_fill_hole.cpp +++ b/src/che_fill_hole.cpp @@ -2,7 +2,6 @@ #include "che_off.h" #include "laplacian.h" -#include "viewer/viewer.h" #include @@ -56,7 +55,6 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, ve = E.t() * ve; vertices.push_back(*((vertex *) ve.memptr())); - //viewer::other_vertices.push_back(vertices.back()); } return fill_hole_front_angles(vertices, mesh->mean_edge(), normal, max_iter); @@ -254,7 +252,6 @@ void split_border(vector > & split_indices, che * mesh, c { cerr << b << " " << i << endl; a = b; -// viewer::select_vertices.push_back(border_vertices[i]); } } } @@ -300,7 +297,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t // holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter, { {77, 106}, {67, 106}, {38, 11} }); holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter); gproshan_debug(inpainting); - //holes[b]->write_file(tmp_file_path("fill_holes_" + to_string(b) + "_" + mesh->name() + ".off")); + if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + to_string(b) + "_" + mesh->name() + ".off")); gproshan_debug(inpainting); } @@ -376,9 +373,6 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, neighbors[v][o] = n_v; front.push(border_t(tmp_vertices, v, neighbors[v], o, tmp_normals[v])); - // viewer::vectors.push_back(vertex(tmp_vertices[v](0), tmp_vertices[v](1), tmp_vertices[v](2))); - // a_vec normal = tmp_vertices[v] + lenght * 3 * normalise(tmp_normals[v]); - // viewer::vectors.push_back(vertex(normal(0), normal(1), normal(2))); } border_t top; @@ -552,11 +546,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vertices.push_back(vertex(r[0], r[1], r[2])); for(index_t v = 0; false && v < tmp_vertices.size(); v++) - { - // viewer::vectors.push_back(vertex(tmp_vertices[v](0), tmp_vertices[v](1), tmp_vertices[v](2))); a_vec normal = tmp_vertices[v] + lenght * 3 * normalise(tmp_normals[v]); - // viewer::vectors.push_back(vertex(normal(0), normal(1), normal(2))); - } gproshan_debug_var(perimeter); // gproshan_debug(filling holes); @@ -812,21 +802,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & lenght, c return fill_hole_front_angles(vertices, lenght, -normal, max_iter, !is_grow); else return nullptr; } -/* - a_vec axis; - axis = avg + E.col(0) * lenght * 3; - viewer::vectors.push_back(vertex(avg(0), avg(1), avg(2))); - viewer::vectors.push_back(vertex(axis(0), axis(1), axis(2))); - axis = avg + E.col(1) * lenght * 3; - viewer::vectors.push_back(vertex(avg(0), avg(1), avg(2))); - viewer::vectors.push_back(vertex(axis(0), axis(1), axis(2))); - axis = avg + E.col(2) * lenght * 3; - viewer::vectors.push_back(vertex(avg(0), avg(1), avg(2))); - viewer::vectors.push_back(vertex(axis(0), axis(1), axis(2))); - axis = avg + normalise(orientation) * lenght * 3; - viewer::vectors.push_back(vertex(avg(0), avg(1), avg(2))); - viewer::vectors.push_back(vertex(axis(0), axis(1), axis(2))); -*/ + vertices.clear(); vertices.reserve(tmp_vertices.size()); From 4e538b3aaf6f19b8dcb03f23963067ea8d89561d Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 20 Jan 2020 17:44:42 +0100 Subject: [PATCH 0094/1018] changing mean paches threshold --- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index fcb4bd3e..2cf663fc 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -240,7 +240,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0.5 *p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8323383a..5144ff67 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -79,11 +79,18 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz p = p - ((p,n)*n); //if(*p <= radio) _vertices.push_back(indexes[i]); - if(*p <= radio && *p >=frontier) + if(*p <= radio && ( n, mesh->normal(indexes[i]) ) >= 0) { _vertices.push_back(indexes[i]); - frontier = *p; + //frontier = *p; } + + /* if(*p <= radio && *p >=frontier) + { + _vertices.push_back(indexes[i]); + frontier = *p; + }*/ + } vertices = std::move(_vertices); From b09f8db4dd1a44f8a87d9cd5ab503400fa1ab45e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Jan 2020 10:24:25 +0100 Subject: [PATCH 0095/1018] adding embree --- CMakeLists.txt | 3 +- include/embree.h | 88 ++++++++++++++++++++++++++++++ include/viewer/viewer.h | 4 ++ src/embree.cpp | 118 ++++++++++++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 35 ++++++++++++ 5 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 include/embree.h create mode 100644 src/embree.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e189c81..159a3390 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) find_package(Eigen3 REQUIRED) find_package(SuiteSparse REQUIRED) +find_package(Embree 3.6 REQUIRED) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) @@ -60,13 +61,13 @@ add_library(gproshan_cpp STATIC ${cpp_sources}) target_link_libraries(gproshan_cpp ${OpenMP_CXX}) target_link_libraries(gproshan_cpp OpenGL::GL) -target_link_libraries(gproshan_cpp OpenGL::GLU) target_link_libraries(gproshan_cpp GLEW::GLEW) target_link_libraries(gproshan_cpp glfw) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan_cpp ${EMBREE_LIBRARY}) target_link_libraries(gproshan_cpp imgui) if(CUDA_FOUND) diff --git a/include/embree.h b/include/embree.h new file mode 100644 index 00000000..4b9015cd --- /dev/null +++ b/include/embree.h @@ -0,0 +1,88 @@ +#ifndef EMBREE_H +#define EMBREE_H + +#include "che.h" + + +#include +#include + + +using namespace gproshan; + +struct ray_hit: public RTCRayHit +{ + ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), + const glm::vec3 & direction = glm::vec3(0.0f), + float near = 0.0f, + float far = FLT_MAX) + { + ray.org_x = origin.x; + ray.org_y = origin.y; + ray.org_z = origin.z; + ray.tnear = near; + + ray.dir_x = direction.x; + ray.dir_y = direction.y; + ray.dir_z = direction.z; + ray.time = 0.0f; + + ray.tfar = far; + ray.mask = 0; + ray.flags = 0; + + //hit.primID = RTC_INVALID_GEOMETRY_ID; + hit.geomID = RTC_INVALID_GEOMETRY_ID; + hit.instID[0] = RTC_INVALID_GEOMETRY_ID; + } + + void org(const glm::vec3 & origin) + { + ray.org_x = origin.x; + ray.org_y = origin.y; + ray.org_z = origin.z; + } + + const glm::vec3 org() const + { + return {ray.org_x, ray.org_y, ray.org_z}; + } + + void dir(const glm::vec3 & direction) + { + ray.dir_x = direction.x; + ray.dir_y = direction.y; + ray.dir_z = direction.z; + } + + const glm::vec3 dir() const + { + return {ray.dir_x, ray.dir_y, ray.dir_z}; + } + + const glm::vec3 normal() const + { + return {hit.Ng_x, hit.Ng_y, hit.Ng_z}; + } +}; + + +class embree +{ + RTCDevice device; + RTCScene scene; + + public: + embree(); + ~embree(); + + void build_bvh(); + unsigned add_sphere(const glm::vec4 & xyzr); + unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4()); + float * raycaster(const glm::uvec2 & windows_size, const glm::mat4 & projection_view, const glm::vec3 & cam_pos, const unsigned & samples = 4); + + bool intersect(ray_hit & r); +}; + +#endif // EMBREE_H + diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index d67b392e..0ce92eb0 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -14,6 +14,8 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" +#include "embree.h" + #define N_MESHES 12 @@ -157,6 +159,8 @@ class viewer static void set_render_lines(viewer * view); static void set_render_corr(viewer * view); static void set_is_flat(viewer * view); + + static void raycasting(viewer * view); // draw routines void set_gl(); diff --git a/src/embree.cpp b/src/embree.cpp new file mode 100644 index 00000000..4b39ea8b --- /dev/null +++ b/src/embree.cpp @@ -0,0 +1,118 @@ +#include "embree.h" + +#include +#include + +void embree_error(void * ptr, RTCError error, const char * str) +{ + fprintf(stderr, "EMBREE ERROR: %s\n", str); +} + +embree::embree() +{ + device = rtcNewDevice(NULL); + rtcSetDeviceErrorFunction(device, embree_error, NULL); + + scene = rtcNewScene(device); +} + +embree::~embree() +{ + rtcReleaseScene(scene); + rtcReleaseDevice(device); +} + +void embree::build_bvh() +{ + rtcCommitScene(scene); +} + +unsigned embree::add_sphere(const glm::vec4 & xyzr) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); + + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer(geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); + *pxyzr = xyzr; + + rtcCommitGeometry(geom); + + unsigned geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + +unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); + + glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer(geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT3, 3 * sizeof(float), + mesh->n_vertices()); + + glm::uvec3 * tri_idxs = (glm::uvec3 *) rtcSetNewGeometryBuffer(geom, + RTC_BUFFER_TYPE_INDEX, 0, + RTC_FORMAT_UINT3, 3 * sizeof(int), + mesh->n_faces()); + + const glm::vec3 * V = (glm::vec3 *) &mesh->gt(0); + + #pragma omp parallel for + for(unsigned i = 0; i < mesh->n_vertices(); i++) + vertices[i] = glm::vec3(model_matrix * glm::vec4(V[i], 1.f)); + + memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); + + rtcCommitGeometry(geom); + + unsigned geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + +float * embree::raycaster(const glm::uvec2 & windows_size, const glm::mat4 & projection_view, const glm::vec3 & cam_pos, const unsigned & samples) +{ + float * frame = new float[windows_size.x * windows_size.y]; + + std::default_random_engine gen; + std::uniform_real_distribution randf(0.0,1.0); + + #pragma omp parallel for + for(unsigned i = 0; i < windows_size.x; i++) + for(unsigned j = 0; j < windows_size.y; j++) + { + //row major + float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; + + for(unsigned s = 0; s < samples; s++) + { + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y ); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = glm::inverse(projection_view) * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + ray_hit r(cam_pos, glm::normalize(p - cam_pos)); + + if(intersect(r)) color += r.ray.tfar; + } + + color /= samples; + } + + return frame; +} + +bool embree::intersect(ray_hit & r) +{ + RTCIntersectContext context; + rtcInitIntersectContext(&context); + rtcIntersect1(scene, &context, &r); + + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 987fd166..bcd531bb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -7,11 +7,15 @@ #include #include #include +#include #include "che_off.h" #include "che_obj.h" #include "che_ply.h" +#include "CImg.h" + +using namespace cimg_library; using namespace std; @@ -181,6 +185,7 @@ void viewer::init_menus() add_process(GLFW_KEY_F4, {"F4", "Gradient Field", set_render_gradient_field}); add_process(GLFW_KEY_F5, {"F5", "Normal Field", set_render_normal_field}); add_process(GLFW_KEY_F6, {"F6", "Show Borders", set_render_border}); + add_process(GLFW_KEY_F7, {"F7", "Raycasting", raycasting}); add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); // add_process('+', "Show Corr", set_render_corr); @@ -421,6 +426,36 @@ void viewer::set_is_flat(viewer * view) view->render_flat = !view->render_flat; } +void viewer::raycasting(viewer * view) +{ + gproshan_log(VIEWER); + + embree rc; + + rc.add_mesh(view->mesh()); + + rc.build_bvh(); + + glm::uvec2 window_size; + glfwGetFramebufferSize(view->window, (int *) &window_size.x, (int *) &window_size.y); + + glm::mat4 model_view; + glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) &model_view); + + glm::mat4 projection; + glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *) &projection); + + float * frame = rc.raycaster(window_size, projection * model_view, {0., 0., -2.5 * view->cam.zoom}); + + std::thread([](CImg img) + { + img.display(); + }, + CImg(frame, window_size.x, window_size.y)).detach(); + + delete [] frame; +} + void viewer::display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); From 36f17e273ed110b073bd9cdcfee96255b1f16da4 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 21 Jan 2020 11:07:04 +0100 Subject: [PATCH 0096/1018] mean curvatures function --- include/che.h | 1 + src/che.cpp | 5 +++++ src/mdict/patch.cpp | 1 + 3 files changed, 7 insertions(+) diff --git a/include/che.h b/include/che.h index eb3a95de..32fc7ab2 100644 --- a/include/che.h +++ b/include/che.h @@ -75,6 +75,7 @@ class che real_t mean_edge() const; size_t memory() const; size_t genus() const; + real_t mean_curvature(const index_t & v); void normalize(); bool is_manifold() const; diff --git a/src/che.cpp b/src/che.cpp index 6c131097..eac15d80 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -358,6 +358,11 @@ size_t che::genus() const return (g - 2) / (-2); } +real_t che::mean_curvature(const index_t & v) +{ + //here it goes +} + void che::normalize() { vertex center; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 5144ff67..3996db1f 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -313,6 +313,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) T(0, 2) = monge_form.normal_direction()[0]; T(1, 2) = monge_form.normal_direction()[1]; T(2, 2) = monge_form.normal_direction()[2]; + //double min_curv = monge_form.principal_curvatures(0); } From 35ced77ee639a9f5c7468a8e0cd4e13a3655ef46 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Jan 2020 15:50:08 +0100 Subject: [PATCH 0097/1018] mean curvature --- src/app_viewer.cpp | 11 ++++++++--- src/che.cpp | 11 ++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 4780abd0..c1bd1ed1 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -840,16 +840,21 @@ void viewer_process_gaussian_curvature() b = viewer::mesh()->gt_vt(prev(he)) - viewer::mesh()->gt(v); g += acos((a,b) / (*a * *b)); } - gv(v) = (2 * M_PI - g) / viewer::mesh()->area_vertex(v); + //gv(v) = (2 * M_PI - g) / viewer::mesh()->area_vertex(v); + gv(v) = viewer::mesh()->mean_curvature(v); + g_max = max(g_max, gv(v)); g_min = min(g_min, gv(v)); } g = g_max - g_min; + gproshan_log_var(g); + gproshan_log_var(g_min); + gproshan_log_var(g_max); #pragma omp parallel for for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) - gv(v) = (gv(v) - g_min) / g; + gv(v) = (gv(v) + g_min) / g; real_t gm = mean(gv); real_t gs = var(gv); @@ -866,7 +871,7 @@ void viewer_process_gaussian_curvature() #pragma omp parallel for for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) - viewer::vcolor(v) = f(gv(v)); + viewer::vcolor(v) = 2 * atan(gv(v) * 10) / M_PI; } void viewer_process_edge_collapse() diff --git a/src/che.cpp b/src/che.cpp index eac15d80..d30a41e4 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -360,7 +360,16 @@ size_t che::genus() const real_t che::mean_curvature(const index_t & v) { - //here it goes + real_t h = 0; + real_t a = 0; + + for_star(he, this, v) + { + a += area_trig(trig(he)); + h += *(GT[next(he)] - GT[v]) * (normal(v), normal_he(he)); + } + + return 0.75 * h / a; } void che::normalize() From 5857d96425dd146369f28f2c6d34d1df21856d55 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Jan 2020 16:04:57 +0100 Subject: [PATCH 0098/1018] mean curvature fix --- src/che.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/che.cpp b/src/che.cpp index d30a41e4..c3168813 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -366,7 +366,7 @@ real_t che::mean_curvature(const index_t & v) for_star(he, this, v) { a += area_trig(trig(he)); - h += *(GT[next(he)] - GT[v]) * (normal(v), normal_he(he)); + h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); } return 0.75 * h / a; From 762e89c912557bd1d4f18809dd7e203f3d1f3ae5 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 22 Jan 2020 16:34:04 +0100 Subject: [PATCH 0099/1018] init radial curvature patches --- include/mdict/inpainting.h | 2 + src/mdict/inpainting.cpp | 214 ++++++++++++++++++++++++++++++++++++- src/mdict/patch.cpp | 1 - 3 files changed, 213 insertions(+), 4 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 6638d706..3e5a1339 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -26,6 +26,8 @@ class inpainting : public dictionary void load_mask(); void init_voronoi_patches(); void init_radial_patches(); + void init_radial_curvature_patches(); + distance_t execute_tmp(); private: size_t avg_p; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 30e03852..fd035739 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -2,10 +2,28 @@ #include #include +#ifndef CGAL_PATCH_DEFS + #define CGAL_PATCH_DEFS + #define CGAL_EIGEN3_ENABLED + #define CGAL_USE_BOOST_PROGRAM_OPTIONS + #define CGAL_USE_GMP + #define DCGAL_USE_MPFR +#endif + +#include +#include + // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { +typedef real_t DFT; +typedef CGAL::Simple_cartesian Data_Kernel; +typedef Data_Kernel::Point_3 DPoint; +typedef Data_Kernel::Vector_3 DVector; +typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; +typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; + inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p, size_t _perc, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { @@ -170,6 +188,8 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } + + void inpainting::init_radial_patches() { // ensure that M is large enough using the radio @@ -293,6 +313,194 @@ void inpainting::init_radial_patches() } +void inpainting::init_radial_curvature_patches() +{ + + //compute mean curvature + real_t *mean_curvature = new real_t[mesh->n_vertices()]; + vector points; + + map non_rep; + map::iterator it; + size_t d_fitting = 2; + size_t d_monge = 2; + size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + + real_t min = INFINITY; + real_t max = -INFINITY; + + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + link_t linkv; + mesh->link(linkv, v); + for(const index_t & he: linkv) + { + link_t linku; + const index_t & u = mesh->vt(he); + mesh->link(linku, u); + for(const index_t & he: linku) + { + it = non_rep.find(u); + if(it == non_rep.end()) + points.push_back(u); + + } + } + assert(points.size() > min_points); + vector in_points; + in_points.reserve(points.size()); + for(const index_t & u: points) + in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); + + My_Monge_form monge_form; + My_Monge_via_jet_fitting monge_fit; + monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); + + vertex normal = mesh->normal(v); + monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); + mean_curvature[v] = ( monge_form.principal_curvatures(0) + monge_form.principal_curvatures(0) ) / 2; + //gproshan_debug_var(mean_curvature[v]); + points.clear(); + non_rep.clear(); + + + } + real_t offset = 0.05; + real_t center = max - ( ( max + abs(min) ) / 2 ); + + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; + dist[v] = abs (mean_curvature[v]); + /*if( mean_curvature[v] > center - offset && mean_curvature[v] < center + offset ) dist[v] = 0.5; + else if( mean_curvature[v] <= center - offset) dist[v] = 0; + else if( mean_curvature[v] >= center + offset) dist[v] = 1;*/ + } + + + /* + + // ensure that M is large enough using the radio + gproshan_log(Init radial patches); + gproshan_log_var(M); + std::vector vertices[M]; + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + //sampling + size_t count = 0, s; + patches.resize(M); + patches_map.resize(n_vertices); + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(sample(s)); + } + + bool covered[mesh->n_vertices()]; + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + + s = 0; + index_t * toplevel = new index_t[mesh->n_vertices()]; + while(count < mesh->n_vertices() && s < M) + { + + //Choose a sample and get the points neighboring points + // Check the points are inside and add them + // while( ) + // mask at the end + + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); + for(auto i:patches[s].vertices) + if(!covered[i]) + { + covered[i] = 1; + count++; + } + s++; + } + gproshan_debug_var(count); + gproshan_debug_var(mesh->n_vertices()); + assert(count == mesh->n_vertices()); + gproshan_debug_var(M); + gproshan_debug_var(s); + + gproshan_debug(finished); + + M = s; // updating number of vertices + //mask at the end no need to call the function + + load_mask(); + + //Initializing patches + gproshan_log(initializing patches); + + patches.resize(M); + patches_map.resize(n_vertices); + //initializing patch + gproshan_debug_var(M); + #pragma omp parallel + { + index_t * toplevel = new index_t[mesh->n_vertices()]; + + /*#pragma omp for + for(index_t s = 0; s < M; s++) + { + + patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); + + } + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; + + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < M; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < M; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < M; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= M; + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); + #endif + } + + bool * pmask = mask; + for(index_t s = 0; s < M; s++) + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + + gproshan_debug(passed); + + #pragma omp parallel for + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + + p.transform(); + p.compute_avg_distance(); + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + + + } */ + gproshan_log(radial patches are ready); + +} + void inpainting::init_voronoi_patches() { @@ -394,11 +602,11 @@ void inpainting::init_voronoi_patches() distance_t inpainting::execute() { - TIC(d_time) init_radial_patches(); TOC(d_time) + TIC(d_time) init_radial_curvature_patches(); TOC(d_time) gproshan_debug_var(d_time); // L = 15; - +/* TIC(d_time) learning(); TOC(d_time) gproshan_debug_var(d_time); @@ -437,7 +645,7 @@ distance_t inpainting::execute() TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) - gproshan_debug_var(d_time); + gproshan_debug_var(d_time);*/ } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 3996db1f..5144ff67 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -313,7 +313,6 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) T(0, 2) = monge_form.normal_direction()[0]; T(1, 2) = monge_form.normal_direction()[1]; T(2, 2) = monge_form.normal_direction()[2]; - //double min_curv = monge_form.principal_curvatures(0); } From 00c6e18063959f122678f3efa0dd81c320766563 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 22 Jan 2020 17:42:16 +0100 Subject: [PATCH 0100/1018] curvature function ready --- include/mdict/dictionary.h | 1 + src/mdict/dictionary.cpp | 78 ++++++++++++++++++++++++++++++++++++ src/mdict/inpainting.cpp | 81 ++++++-------------------------------- 3 files changed, 90 insertions(+), 70 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index d633f45c..4322349d 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -65,6 +65,7 @@ class dictionary void learning(); void sparse_coding(); void init_sampling(); + void load_curvatures(a_vec & curvatures); void init_patches( const bool & reset = 1, const fmask_t & mask = nullptr ); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index cb45c407..e638c8d7 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -10,6 +10,17 @@ #include #include +#ifndef CGAL_PATCH_DEFS + #define CGAL_PATCH_DEFS + #define CGAL_EIGEN3_ENABLED + #define CGAL_USE_BOOST_PROGRAM_OPTIONS + #define CGAL_USE_GMP + #define DCGAL_USE_MPFR +#endif + +#include +#include + using namespace cimg_library; @@ -18,6 +29,14 @@ using namespace cimg_library; // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { +typedef real_t DFT; +typedef CGAL::Simple_cartesian Data_Kernel; +typedef Data_Kernel::Point_3 DPoint; +typedef Data_Kernel::Vector_3 DVector; +typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; +typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; + + size_t dictionary::L = 12; size_t dictionary::K = 10; @@ -101,6 +120,65 @@ void dictionary::init_sampling() gproshan_debug_var(phi_basis->radio); } +void dictionary::load_curvatures(a_vec & curvatures) +{ + string f_curv = tmp_file_path(mesh->name_size() + ".curv"); + if(! curvatures.load(f_curv)) + { + curvatures.zeros(mesh->n_vertices()); + //real_t *mean_curvature = new real_t[mesh->n_vertices()]; + vector points; + + map non_rep; + map::iterator it; + size_t d_fitting = 2; + size_t d_monge = 2; + size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + + real_t min = INFINITY; + real_t max = -INFINITY; + + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + link_t linkv; + mesh->link(linkv, v); + for(const index_t & he: linkv) + { + link_t linku; + const index_t & u = mesh->vt(he); + mesh->link(linku, u); + for(const index_t & he: linku) + { + it = non_rep.find(u); + if(it == non_rep.end()) + points.push_back(u); + + } + } + assert(points.size() > min_points); + vector in_points; + in_points.reserve(points.size()); + for(const index_t & u: points) + in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); + + My_Monge_form monge_form; + My_Monge_via_jet_fitting monge_fit; + monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); + + vertex normal = mesh->normal(v); + monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); + curvatures(v) = ( monge_form.principal_curvatures(0) + monge_form.principal_curvatures(0) ) / 2; + //gproshan_debug_var(mean_curvature[v]); + points.clear(); + non_rep.clear(); + + } + curvatures.save(f_curv); + } + gproshan_debug(curvatures ready); + +} + void dictionary::init_patches(const bool & reset, const fmask_t & mask) { gproshan_log(MDICT); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index fd035739..127c0228 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,29 +1,13 @@ #include "inpainting.h" #include #include +#include -#ifndef CGAL_PATCH_DEFS - #define CGAL_PATCH_DEFS - #define CGAL_EIGEN3_ENABLED - #define CGAL_USE_BOOST_PROGRAM_OPTIONS - #define CGAL_USE_GMP - #define DCGAL_USE_MPFR -#endif - -#include -#include // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -typedef real_t DFT; -typedef CGAL::Simple_cartesian Data_Kernel; -typedef Data_Kernel::Point_3 DPoint; -typedef Data_Kernel::Vector_3 DVector; -typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; -typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; - inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p, size_t _perc, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { @@ -317,64 +301,21 @@ void inpainting::init_radial_curvature_patches() { //compute mean curvature - real_t *mean_curvature = new real_t[mesh->n_vertices()]; - vector points; - - map non_rep; - map::iterator it; - size_t d_fitting = 2; - size_t d_monge = 2; - size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - - real_t min = INFINITY; - real_t max = -INFINITY; + a_vec curvatures; + load_curvatures(curvatures); + priority_queue< pair > Q; for(index_t v = 0; v < mesh->n_vertices(); v++) { - link_t linkv; - mesh->link(linkv, v); - for(const index_t & he: linkv) - { - link_t linku; - const index_t & u = mesh->vt(he); - mesh->link(linku, u); - for(const index_t & he: linku) - { - it = non_rep.find(u); - if(it == non_rep.end()) - points.push_back(u); - - } - } - assert(points.size() > min_points); - vector in_points; - in_points.reserve(points.size()); - for(const index_t & u: points) - in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); - - My_Monge_form monge_form; - My_Monge_via_jet_fitting monge_fit; - monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); - - vertex normal = mesh->normal(v); - monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); - mean_curvature[v] = ( monge_form.principal_curvatures(0) + monge_form.principal_curvatures(0) ) / 2; - //gproshan_debug_var(mean_curvature[v]); - points.clear(); - non_rep.clear(); - - + //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; + Q.push ( make_pair(abs (curvatures(v)), v ) ); } - real_t offset = 0.05; - real_t center = max - ( ( max + abs(min) ) / 2 ); - - for(index_t v = 0; v < mesh->n_vertices(); v++) + while(!Q.empty()) { - //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; - dist[v] = abs (mean_curvature[v]); - /*if( mean_curvature[v] > center - offset && mean_curvature[v] < center + offset ) dist[v] = 0.5; - else if( mean_curvature[v] <= center - offset) dist[v] = 0; - else if( mean_curvature[v] >= center + offset) dist[v] = 1;*/ + //gproshan_debug_var(Q.top().first); + //gproshan_debug_var(Q.top().second); + + Q.pop(); } From b60671cd544df0394c967ff65a65f3be164784e3 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 23 Jan 2020 12:40:55 +0100 Subject: [PATCH 0101/1018] new curvature sampling no test --- include/mdict/patch.h | 6 ++ src/mdict/dictionary.cpp | 10 +++ src/mdict/inpainting.cpp | 155 +++++++-------------------------------- src/mdict/patch.cpp | 42 ++++++++--- 4 files changed, 73 insertions(+), 140 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index b957fb80..879b5a74 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -39,6 +39,7 @@ class patch a_mat xyz; ///< Matrix of points. a_mat phi; double avg_dist; // Average distance betweenn points in a patch + distance_t radio; // radio of a patch public: static size_t expected_nv; ///< Expected number of patch vertices. @@ -65,6 +66,11 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); + void init_curvature_growing(che * mesh, + const index_t & v, + bool * covered, + a_mat & normals, + vector & _vertices); void transform(); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index e638c8d7..2ad3a935 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -123,6 +123,8 @@ void dictionary::init_sampling() void dictionary::load_curvatures(a_vec & curvatures) { string f_curv = tmp_file_path(mesh->name_size() + ".curv"); + string f_norm = tmp_file_path(mesh->name_size() + ".n"); + if(! curvatures.load(f_curv)) { curvatures.zeros(mesh->n_vertices()); @@ -137,6 +139,8 @@ void dictionary::load_curvatures(a_vec & curvatures) real_t min = INFINITY; real_t max = -INFINITY; + a_mat normals; + normals.zeros(3, mesh->n_vertices()); for(index_t v = 0; v < mesh->n_vertices(); v++) { @@ -168,12 +172,18 @@ void dictionary::load_curvatures(a_vec & curvatures) vertex normal = mesh->normal(v); monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); curvatures(v) = ( monge_form.principal_curvatures(0) + monge_form.principal_curvatures(0) ) / 2; + + + normals(0, v) = monge_form.normal_direction()[0]; + normals(1, v) = monge_form.normal_direction()[1]; + normals(2, v) = monge_form.normal_direction()[2]; //gproshan_debug_var(mean_curvature[v]); points.clear(); non_rep.clear(); } curvatures.save(f_curv); + normals.save(f_norm); } gproshan_debug(curvatures ready); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 127c0228..41b06211 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -244,15 +244,6 @@ void inpainting::init_radial_patches() gproshan_debug_var(M); #pragma omp parallel { - index_t * toplevel = new index_t[mesh->n_vertices()]; - - /*#pragma omp for - for(index_t s = 0; s < M; s++) - { - - patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); - - } */ #ifndef NDEBUG size_t patch_avg_size = 0; size_t patch_min_size = NIL; @@ -302,144 +293,50 @@ void inpainting::init_radial_curvature_patches() //compute mean curvature a_vec curvatures; + TIC(d_time) load_curvatures(curvatures); + TOC(d_time) + gproshan_debug_var(d_time); - priority_queue< pair > Q; + string f_norm = tmp_file_path(mesh->name_size() + ".n"); + + a_mat normals; + normals.load(f_norm); + + priority_queue< pair > Q; for(index_t v = 0; v < mesh->n_vertices(); v++) { //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; Q.push ( make_pair(abs (curvatures(v)), v ) ); } - while(!Q.empty()) - { - //gproshan_debug_var(Q.top().first); - //gproshan_debug_var(Q.top().second); - - Q.pop(); - } - - - /* - - // ensure that M is large enough using the radio - gproshan_log(Init radial patches); - gproshan_log_var(M); - std::vector vertices[M]; - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - //sampling - size_t count = 0, s; - patches.resize(M); - patches_map.resize(n_vertices); - - //saving first vertex aka seed vertices - #pragma omp for - for(index_t s = 0; s < M; s++) - { - vertices[s].push_back(sample(s)); - } + std::vector vertices; bool covered[mesh->n_vertices()]; + index_t * toplevel = new index_t[mesh->n_vertices()]; #pragma omp for for(index_t i = 0; i < mesh->n_vertices(); i++) { covered[i] = 0; } - - s = 0; - index_t * toplevel = new index_t[mesh->n_vertices()]; - while(count < mesh->n_vertices() && s < M) - { - - //Choose a sample and get the points neighboring points - // Check the points are inside and add them - // while( ) - // mask at the end - - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); - for(auto i:patches[s].vertices) - if(!covered[i]) - { - covered[i] = 1; - count++; - } - s++; - } - gproshan_debug_var(count); - gproshan_debug_var(mesh->n_vertices()); - assert(count == mesh->n_vertices()); - gproshan_debug_var(M); - gproshan_debug_var(s); - - gproshan_debug(finished); - - M = s; // updating number of vertices - //mask at the end no need to call the function - - load_mask(); - - //Initializing patches - gproshan_log(initializing patches); - - patches.resize(M); - patches_map.resize(n_vertices); - //initializing patch - gproshan_debug_var(M); - #pragma omp parallel + //dictionary::T = 3; + index_t ns; + size_t count = 0; + while(!Q.empty()) // You will give the seeds and the patch will save its points and kept the radio and give you the next fathest point { - index_t * toplevel = new index_t[mesh->n_vertices()]; - - /*#pragma omp for - for(index_t s = 0; s < M; s++) + index_t s = Q.top().second; + //gproshan_debug_var(Q.top().first); + //gproshan_debug_var(Q.top().second); + if(!covered[s]) { - - patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); - - } - #ifndef NDEBUG - size_t patch_avg_size = 0; - size_t patch_min_size = NIL; - size_t patch_max_size = 0; - - #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) - patch_avg_size += patches[s].vertices.size(); - #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); - #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); + count++; + vertices.push_back(s); + patches[s].init_curvature_growing(mesh, s, covered, normals, vertices); + } - patch_avg_size /= M; - gproshan_debug_var(patch_avg_size); - gproshan_debug_var(patch_min_size); - gproshan_debug_var(patch_max_size); - #endif + Q.pop(); } - - bool * pmask = mask; - for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - - gproshan_debug(passed); - - #pragma omp parallel for - for(index_t s = 0; s < M; s++) - { - patch & p = patches[s]; - - p.transform(); - p.compute_avg_distance(); - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); - - - } */ - gproshan_log(radial patches are ready); - + M = count; + gproshan_debug_var(M); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 5144ff67..8b56956c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -64,11 +64,8 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); - //gather_vertices(mesh, v, n_toplevels, toplevel); - distance_t frontier = -1; for(int i=1; inormal(indexes[i]) ) >= 0) { _vertices.push_back(indexes[i]); - //frontier = *p; } - /* if(*p <= radio && *p >=frontier) - { - _vertices.push_back(indexes[i]); - frontier = *p; - }*/ - } vertices = std::move(_vertices); @@ -99,6 +88,37 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz } +void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals, vector & _vertices) +{ + geodesics geo(mesh, {v}, geodesics::FM, NULL, false); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + + gproshan_debug_var(geo.n_sorted_index()); + for(int i=1; iget_vertex(indexes[i]); + vertex c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + //gather the good ones + gproshan_debug_var(i); + if( (n, mesh->normal(indexes[i]) ) >= 0) + { + radio = *p; + _vertices.push_back(indexes[i]); + covered[ indexes[i]] = 1; + } + + gproshan_debug_var(i); + } + + vertices = std::move(_vertices); +} // xyz = E.t * (xyz - avg) void patch::transform() { From f7b71e3386501a2d29d268ffda3c2e94c8054e51 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 23 Jan 2020 16:36:15 +0100 Subject: [PATCH 0102/1018] testing curvature sampling --- include/mdict/patch.h | 3 +- src/mdict/inpainting.cpp | 71 ++++++++++++++++++++++++++++++++++++---- src/mdict/patch.cpp | 31 +++++++++++------- 3 files changed, 85 insertions(+), 20 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 879b5a74..1beffa67 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -69,8 +69,7 @@ class patch void init_curvature_growing(che * mesh, const index_t & v, bool * covered, - a_mat & normals, - vector & _vertices); + a_mat & normals); void transform(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 41b06211..5a9318b6 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -310,7 +310,7 @@ void inpainting::init_radial_curvature_patches() Q.push ( make_pair(abs (curvatures(v)), v ) ); } - std::vector vertices; + bool covered[mesh->n_vertices()]; index_t * toplevel = new index_t[mesh->n_vertices()]; #pragma omp for @@ -318,25 +318,84 @@ void inpainting::init_radial_curvature_patches() { covered[i] = 0; } - //dictionary::T = 3; + + patches_map.resize(mesh->n_vertices()); + index_t ns; size_t count = 0; - while(!Q.empty()) // You will give the seeds and the patch will save its points and kept the radio and give you the next fathest point + while(!Q.empty()) // curvatures from higher to lower { index_t s = Q.top().second; //gproshan_debug_var(Q.top().first); //gproshan_debug_var(Q.top().second); + if(!covered[s]) { count++; - vertices.push_back(s); - patches[s].init_curvature_growing(mesh, s, covered, normals, vertices); - } + patch tmp; + tmp.init_curvature_growing(mesh, s, covered, normals); + patches.push_back(tmp); + } Q.pop(); } M = count; gproshan_debug_var(M); + + ////////////////////////////////////////////////////////////////////////////////// + load_mask(); + + //Initializing patches + gproshan_log(initializing patches); + n_vertices = mesh->n_vertices(); + patches.resize(M); + patches_map.resize(n_vertices); + //initializing patch + gproshan_debug_var(M); + #pragma omp parallel + { + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; + + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < M; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < M; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < M; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= M; + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); + #endif + } + + bool * pmask = mask; + for(index_t s = 0; s < M; s++) + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + + gproshan_debug(passed); + + #pragma omp parallel for + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + + p.transform(); + p.compute_avg_distance(); + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + + + } + gproshan_log(radial patches are ready); + } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8b56956c..a2e963a8 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -88,36 +88,43 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz } -void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals, vector & _vertices) +void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals) { geodesics geo(mesh, {v}, geodesics::FM, NULL, false); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); - + a_vec vn = normals.col(v); + vertex n; + n.x = vn(0); n.y = vn(1); n.z = vn(2); gproshan_debug_var(geo.n_sorted_index()); + + vertices.push_back(v); + for(int i=1; iget_vertex(indexes[i]); vertex c = mesh->get_vertex(v); // central vertices p = p - c ; p = p - ((p,n)*n); //gather the good ones - gproshan_debug_var(i); - if( (n, mesh->normal(indexes[i]) ) >= 0) + //gproshan_debug_var((n, mesh->normal(indexes[i]) ) ); + if( (n, mesh->normal(indexes[i]) ) >= 0 ) { radio = *p; - _vertices.push_back(indexes[i]); + vertices.push_back(indexes[i]); covered[ indexes[i]] = 1; } - - gproshan_debug_var(i); + else + break; + } - - vertices = std::move(_vertices); + gproshan_debug_var(vertices.size()); + jet_fit_directions(mesh, v); + } // xyz = E.t * (xyz - avg) void patch::transform() From 03a2e709cec60067b113ff16d2389565b93d6698 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 23 Jan 2020 17:34:07 +0100 Subject: [PATCH 0103/1018] curvature sampling almost done --- src/mdict/inpainting.cpp | 3 ++- src/mdict/patch.cpp | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5a9318b6..640218ca 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -334,7 +334,8 @@ void inpainting::init_radial_curvature_patches() count++; patch tmp; tmp.init_curvature_growing(mesh, s, covered, normals); - patches.push_back(tmp); + if(covered[s]) + patches.push_back(tmp); } Q.pop(); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a2e963a8..2182cfa2 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -99,10 +99,11 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered gproshan_debug_var(geo.n_sorted_index()); vertices.push_back(v); + covered[v] = 1; for(int i=1; iget_vertex(indexes[i]); @@ -111,7 +112,7 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered p = p - c ; p = p - ((p,n)*n); //gather the good ones - //gproshan_debug_var((n, mesh->normal(indexes[i]) ) ); + // gproshan_debug_var((n, mesh->normal(indexes[i]) ) ); if( (n, mesh->normal(indexes[i]) ) >= 0 ) { radio = *p; @@ -123,7 +124,20 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered } gproshan_debug_var(vertices.size()); - jet_fit_directions(mesh, v); + size_t d_fitting = 2; + size_t d_monge = 2; + size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + if(vertices.size() <= min_points ) + { + for(index_t i = 0; i < vertices.size(); i++) + { + covered[indexes[i]] = 0; + } + vertices.clear(); + + } + else + jet_fit_directions(mesh, v); } // xyz = E.t * (xyz - avg) From 42d298e01c3868e93662898e87a5ae632bf32766 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 24 Jan 2020 15:37:41 +0100 Subject: [PATCH 0104/1018] fixed init curvature sampling to solve yet sparse coding issues --- src/mdict/inpainting.cpp | 20 +++++++++++++++----- src/mdict/mdict.cpp | 2 ++ src/mdict/patch.cpp | 15 ++++++--------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 640218ca..9410c357 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -26,7 +26,7 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size void inpainting::load_mask() { //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -331,17 +331,26 @@ void inpainting::init_radial_curvature_patches() if(!covered[s]) { - count++; + patch tmp; tmp.init_curvature_growing(mesh, s, covered, normals); - if(covered[s]) + if(tmp.vertices.size() > 0) + { patches.push_back(tmp); + count++; + } + } Q.pop(); } M = count; gproshan_debug_var(M); + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + if(!covered[i] ) + gproshan_debug_var( covered[i] ); + } ////////////////////////////////////////////////////////////////////////////////// load_mask(); @@ -387,6 +396,7 @@ void inpainting::init_radial_curvature_patches() for(index_t s = 0; s < M; s++) { patch & p = patches[s]; + if(p.vertices.size() == 0) gproshan_debug_var(s); p.transform(); p.compute_avg_distance(); @@ -504,7 +514,7 @@ distance_t inpainting::execute() gproshan_debug_var(d_time); // L = 15; -/* + TIC(d_time) learning(); TOC(d_time) gproshan_debug_var(d_time); @@ -543,7 +553,7 @@ distance_t inpainting::execute() TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) - gproshan_debug_var(d_time);*/ + gproshan_debug_var(d_time); } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 2cf663fc..e0790570 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -147,6 +147,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) { + gproshan_debug_var(x); arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; @@ -156,6 +157,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L index_t l = 0; while(norm(r) > threshold && l < L) { + // gproshan_debug_var(D.t() * r); selected_atoms(l) = max_index(abs(D.t() * r), mask); DD = D.cols(selected_atoms.head(l + 1)); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2182cfa2..ccf5c807 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -90,13 +90,13 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals) { - geodesics geo(mesh, {v}, geodesics::FM, NULL, false); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/100); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); a_vec vn = normals.col(v); vertex n; n.x = vn(0); n.y = vn(1); n.z = vn(2); - gproshan_debug_var(geo.n_sorted_index()); +// gproshan_debug_var(geo.n_sorted_index()); vertices.push_back(v); covered[v] = 1; @@ -123,18 +123,13 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered break; } - gproshan_debug_var(vertices.size()); + //gproshan_debug_var(vertices.size()); size_t d_fitting = 2; size_t d_monge = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; if(vertices.size() <= min_points ) { - for(index_t i = 0; i < vertices.size(); i++) - { - covered[indexes[i]] = 0; - } vertices.clear(); - } else jet_fit_directions(mesh, v); @@ -197,12 +192,14 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vectorgt(vertices[i]); xyz(0, j) = v.x; xyz(1, j) = v.y; From 58d6f1f640a6cd19f23ce68cd2cf19c5ad971677 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 24 Jan 2020 16:47:47 +0100 Subject: [PATCH 0105/1018] fixing zoom factor --- include/mdict/d_mesh.h | 2 +- include/mdict/patch.h | 6 ++++-- src/mdict/d_mesh.cpp | 3 ++- src/mdict/dictionary.cpp | 2 +- src/mdict/inpainting.cpp | 4 ++-- src/mdict/patch.cpp | 27 +++++++++++++++++++++------ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index a408b4fe..810a24dd 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,7 +111,7 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -distance_t mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); +distance_t mesh_reconstruction(che * mesh, size_t M, const distance_t & radio, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 1beffa67..ae70e336 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -51,7 +51,7 @@ class patch void init( che * mesh, ///< input mesh. const index_t & v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. - const distance_t & radio, ///< euclidean radio in XY of the patch. + const distance_t & radio_, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); void init_disjoint(che * mesh, @@ -60,7 +60,7 @@ class patch vector & _vertices, index_t * _toplevel = nullptr); void init_radial_disjoint(che * mesh, - const distance_t & radio, + const distance_t & radio_, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, @@ -92,6 +92,8 @@ class patch void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); void compute_avg_distance(); + void scale_xyz(const real_t & radio_f); + void iscale_xyz(const real_t & radio_f); private: diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index f9259114..4378ce74 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -241,7 +241,7 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist,const fmask_t & mask, const index_t & v_i) +distance_t mesh_reconstruction(che * mesh, size_t M, const distance_t & radio, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, distance_t * dist,const fmask_t & mask, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); @@ -256,6 +256,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, vector & patches, ve a_vec x = rp.phi * A * alpha.col(p); rp.xyz.row(2) = x.t(); rp.itransform(); + rp.iscale_xyz(radio); } } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 2ad3a935..536ff7f3 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -283,7 +283,7 @@ distance_t dictionary::mesh_reconstruction(const fmask_t & mask) gproshan_log(MDICT); assert(n_vertices == mesh->n_vertices()); - return mdict::mesh_reconstruction(mesh, M, patches, patches_map, A, alpha, dist); + return mdict::mesh_reconstruction(mesh, M, phi_basis->get_radio(), patches, patches_map, A, alpha, dist); } void dictionary::update_alphas(a_mat & alpha, size_t threshold) { diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 9410c357..2a7fcf7b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -310,7 +310,6 @@ void inpainting::init_radial_curvature_patches() Q.push ( make_pair(abs (curvatures(v)), v ) ); } - bool covered[mesh->n_vertices()]; index_t * toplevel = new index_t[mesh->n_vertices()]; #pragma omp for @@ -402,7 +401,7 @@ void inpainting::init_radial_curvature_patches() p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - + p.scale_xyz(phi_basis->get_radio()); } gproshan_log(radial patches are ready); @@ -539,6 +538,7 @@ distance_t inpainting::execute() p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); + p.scale_xyz(phi_basis->get_radio()); } bool *pmask; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index ccf5c807..58f19b1b 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -30,19 +30,21 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * dictionary::T * (dictionary::T + 1); -void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const distance_t & radio, index_t * _toplevel) +void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const distance_t & radio_, index_t * _toplevel) { + radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); - gather_vertices(mesh, v, radio, toplevel); + gather_vertices(mesh, v, radio_, toplevel); if(!_toplevel) delete [] toplevel; } void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { + radio = 1; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); @@ -53,8 +55,9 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { + radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); @@ -90,6 +93,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio, const siz void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals) { + radio = 1; geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/100); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -178,6 +182,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } } } + void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { @@ -197,9 +202,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vectorgt(vertices[i]); xyz(0, j) = v.x; xyz(1, j) = v.y; @@ -209,6 +212,18 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Mon, 27 Jan 2020 12:18:16 +0100 Subject: [PATCH 0106/1018] fixed radial curvature sampling reconstruction issues --- include/mdict/basis_dct.h | 2 +- src/app_viewer.cpp | 8 ++++---- src/mdict/d_mesh.cpp | 2 +- src/mdict/inpainting.cpp | 12 +++++++----- src/mdict/mdict.cpp | 7 ++++--- src/mdict/patch.cpp | 1 + 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index c09ce501..26752394 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -15,7 +15,7 @@ class basis_dct: public basis int n; // int frequency public: - basis_dct(const size_t & _n, const distance_t & _radio = 0); + basis_dct(const size_t & _n, const distance_t & _radio = 1); void discrete(a_mat & phi, const a_mat & xy); double get_frequency(size_t idx); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c1bd1ed1..fc028101 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -523,7 +523,7 @@ void viewer_process_mask() { gproshan_log(APP_VIEWER); - size_t avg_p; + size_t avg_p = 20; size_t percentage; size_t n=12; // dct @@ -533,12 +533,12 @@ void viewer_process_mask() gproshan_input(avg_p percentage f ); - cin >> avg_p >> percentage >> f; - + //cin >> avg_p >> percentage >> f; + cin>> percentage; basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_patches(); + dict.init_radial_curvature_patches(); //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 4378ce74..9b67a30c 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -260,7 +260,7 @@ distance_t mesh_reconstruction(che * mesh, size_t M, const distance_t & radio, v } } - distance_t h = 0.2; +// distance_t h = 0.2; //Computes the weighted average of vertices #pragma omp parallel for for(index_t v = v_i; v < mesh->n_vertices(); v++) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 2a7fcf7b..875a7274 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -395,7 +395,6 @@ void inpainting::init_radial_curvature_patches() for(index_t s = 0; s < M; s++) { patch & p = patches[s]; - if(p.vertices.size() == 0) gproshan_debug_var(s); p.transform(); p.compute_avg_distance(); @@ -511,7 +510,6 @@ distance_t inpainting::execute() TIC(d_time) init_radial_curvature_patches(); TOC(d_time) gproshan_debug_var(d_time); - // L = 15; TIC(d_time) learning(); TOC(d_time) @@ -521,15 +519,18 @@ distance_t inpainting::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); + //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) { patches_map[i].clear(); } + for(index_t s = 0; s < M; s++) patches[s].reset_xyz(mesh, patches_map, s, 0); + #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -545,9 +546,10 @@ distance_t inpainting::execute() draw_patches(10); draw_patches(200); - draw_patches(500); - draw_patches(56); - gproshan_debug_var(patches[56].vertices[0]); + draw_patches(120); + draw_patches(50); + //draw_patches(500); + //draw_patches(56); //phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index e0790570..d776174c 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -147,7 +147,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) { - gproshan_debug_var(x); + arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; @@ -195,11 +195,12 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { a_mat alpha(D.n_cols, X.n_cols); - #pragma omp parallel for for(index_t i = 0; i < X.n_cols; i++) + { alpha.col(i) = OMP(X.col(i), D, L); - + } + return alpha; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 58f19b1b..8085f75f 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -222,6 +222,7 @@ void patch::iscale_xyz(const real_t & radio_f) { real_t factor = radio_f/radio; xyz = xyz / factor; + } const a_vec patch::normal() From 96421aa31907bacfb2c8db953519ea6b6a0de57d Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 27 Jan 2020 17:02:56 +0100 Subject: [PATCH 0107/1018] fixing radio patches --- src/mdict/d_mesh.cpp | 2 +- src/mdict/inpainting.cpp | 10 ++++++---- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 7 +++++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 9b67a30c..78da2c0b 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -255,8 +255,8 @@ distance_t mesh_reconstruction(che * mesh, size_t M, const distance_t & radio, v { a_vec x = rp.phi * A * alpha.col(p); rp.xyz.row(2) = x.t(); - rp.itransform(); rp.iscale_xyz(radio); + rp.itransform(); } } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 875a7274..bb9fd450 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -276,8 +276,9 @@ void inpainting::init_radial_patches() for(index_t s = 0; s < M; s++) { patch & p = patches[s]; - + p.transform(); + p.scale_xyz(phi_basis->get_radio()); p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); @@ -307,7 +308,7 @@ void inpainting::init_radial_curvature_patches() for(index_t v = 0; v < mesh->n_vertices(); v++) { //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; - Q.push ( make_pair(abs (curvatures(v)), v ) ); + Q.push ( make_pair(-1*curvatures(v), v ) ); } bool covered[mesh->n_vertices()]; @@ -397,10 +398,10 @@ void inpainting::init_radial_curvature_patches() patch & p = patches[s]; p.transform(); + p.scale_xyz(phi_basis->get_radio()); p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - p.scale_xyz(phi_basis->get_radio()); } gproshan_log(radial patches are ready); @@ -537,9 +538,10 @@ distance_t inpainting::execute() patch & p = patches[s]; p.transform(); + p.scale_xyz(phi_basis->get_radio()); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - p.scale_xyz(phi_basis->get_radio()); + } bool *pmask; diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index d776174c..86797991 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0.5 *p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8085f75f..7852c9c6 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -93,7 +93,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals) { - radio = 1; + radio = -INFINITY; geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/100); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -119,7 +119,8 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered // gproshan_debug_var((n, mesh->normal(indexes[i]) ) ); if( (n, mesh->normal(indexes[i]) ) >= 0 ) { - radio = *p; + if(*p > radio) + radio = *p; vertices.push_back(indexes[i]); covered[ indexes[i]] = 1; } @@ -216,12 +217,14 @@ void patch::scale_xyz(const real_t & radio_f) { real_t factor = radio_f/radio; xyz = factor * xyz; + //radio *= factor; } void patch::iscale_xyz(const real_t & radio_f) { real_t factor = radio_f/radio; xyz = xyz / factor; + //radio /= factor; } From 8d2b7ccff04ba6d3bedd953263decffdfe939a2e Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 28 Jan 2020 15:48:03 +0100 Subject: [PATCH 0108/1018] solving radio problem --- src/mdict/inpainting.cpp | 3 ++- src/mdict/patch.cpp | 45 ++++++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bb9fd450..a45f953b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -308,7 +308,7 @@ void inpainting::init_radial_curvature_patches() for(index_t v = 0; v < mesh->n_vertices(); v++) { //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; - Q.push ( make_pair(-1*curvatures(v), v ) ); + Q.push ( make_pair(-1* curvatures(v), v ) ); } bool covered[mesh->n_vertices()]; @@ -550,6 +550,7 @@ distance_t inpainting::execute() draw_patches(200); draw_patches(120); draw_patches(50); + //draw_patches(400); //draw_patches(500); //draw_patches(56); //phi_basis->plot_basis(); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 7852c9c6..58f75089 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -12,7 +12,7 @@ #include #include - +#define PI 3.14159265 #include // geometry processing and shape analysis framework @@ -113,14 +113,9 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered vertex p = mesh->get_vertex(indexes[i]); vertex c = mesh->get_vertex(v); // central vertices - p = p - c ; - p = p - ((p,n)*n); //gather the good ones - // gproshan_debug_var((n, mesh->normal(indexes[i]) ) ); if( (n, mesh->normal(indexes[i]) ) >= 0 ) { - if(*p > radio) - radio = *p; vertices.push_back(indexes[i]); covered[ indexes[i]] = 1; } @@ -128,6 +123,7 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered break; } + //gproshan_debug_var(vertices.size()); size_t d_fitting = 2; size_t d_monge = 2; @@ -137,7 +133,40 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered vertices.clear(); } else + { jet_fit_directions(mesh, v); + size_t n_vertices = vertices.size(); + vertices.clear(); + vertices.push_back(v); + covered[v] = 1; + + vn = T.col(2);// normal at the center + n.x = vn(0); n.y = vn(1); n.z = vn(2); + + for(int i=1; i < n_vertices; i++) + { + vertex p = mesh->get_vertex(indexes[i]); + vertex c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + //if( acos(n, mesh->normal(indexes[i]))*180 / PI >= 90 ) + if( (n, mesh->normal(indexes[i])) >= 0 ) + { + if(*p > radio) + radio = *p; + vertices.push_back(indexes[i]); + covered[ indexes[i]] = 1; + } + else + break; + } + + if(vertices.size() <= min_points ) + vertices.clear(); + + } + } // xyz = E.t * (xyz - avg) @@ -217,15 +246,13 @@ void patch::scale_xyz(const real_t & radio_f) { real_t factor = radio_f/radio; xyz = factor * xyz; - //radio *= factor; + } void patch::iscale_xyz(const real_t & radio_f) { real_t factor = radio_f/radio; xyz = xyz / factor; - //radio /= factor; - } const a_vec patch::normal() From f4ae9a0e2c21aa98ecaa5ba00a1e50aee5b7e0b7 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 28 Jan 2020 16:44:57 +0100 Subject: [PATCH 0109/1018] controlling angle between normals --- src/mdict/patch.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 58f75089..8be6dcf9 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -150,8 +150,9 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered p = p - c ; p = p - ((p,n)*n); - //if( acos(n, mesh->normal(indexes[i]))*180 / PI >= 90 ) - if( (n, mesh->normal(indexes[i])) >= 0 ) + //gproshan_debug_var((acos((n, mesh->normal(indexes[i])))*180 / PI)); + if( (acos((n, mesh->normal(indexes[i])))*180 / PI) <= 45 ) + //if( (n, mesh->normal(indexes[i])) >= 0 ) { if(*p > radio) radio = *p; From 2d9dae9c955b9106f3bfe21d29c70ef5f0727da8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Jan 2020 17:01:08 +0100 Subject: [PATCH 0110/1018] using glm for proj and view matrices --- include/viewer/viewer.h | 8 +-- src/viewer/viewer.cpp | 107 +++++++++++----------------------------- 2 files changed, 33 insertions(+), 82 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 0ce92eb0..68dd707d 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -77,11 +77,16 @@ class viewer GLFWwindow * window; + shader shader_program; camera cam; + int viewport_width; int viewport_height; + glm::mat4 view_mat; + glm::mat4 proj_mat; + che_viewer meshes[N_MESHES]; vcorr_t corr_mesh[N_MESHES]; size_t n_meshes; @@ -163,9 +168,6 @@ class viewer static void raycasting(viewer * view); // draw routines - void set_gl(); - void set_lighting(); - void set_mesh_material(); void draw_scene(); void draw_corr(); void draw_polygons(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index bcd531bb..7afd48ee 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -9,6 +9,9 @@ #include #include +#include +#include + #include "che_off.h" #include "che_obj.h" #include "che_ply.h" @@ -49,7 +52,6 @@ viewer::viewer() init_imgui(); init_menus(); - set_gl(); init_glsl(); info_gl(); @@ -69,6 +71,8 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { + glfwPollEvents(); + display(); ImGui_ImplOpenGL3_NewFrame(); @@ -309,7 +313,7 @@ void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset void viewer::idle() { //cam.idle(); - //glutPostRedisplay(); + ////glutPostRedisplay(); } void viewer::menu_process(function_t pro) @@ -439,13 +443,7 @@ void viewer::raycasting(viewer * view) glm::uvec2 window_size; glfwGetFramebufferSize(view->window, (int *) &window_size.x, (int *) &window_size.y); - glm::mat4 model_view; - glGetFloatv(GL_MODELVIEW_MATRIX, (GLfloat *) &model_view); - - glm::mat4 projection; - glGetFloatv(GL_PROJECTION_MATRIX, (GLfloat *) &projection); - - float * frame = rc.raycaster(window_size, projection * model_view, {0., 0., -2.5 * view->cam.zoom}); + float * frame = rc.raycaster(window_size, view->proj_mat * view->view_mat, {0., 0., -2. * view->cam.zoom}); std::thread([](CImg img) { @@ -458,32 +456,20 @@ void viewer::raycasting(viewer * view) void viewer::display() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - shader_program.enable(); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - GLint viewport[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - double aspect = (double) viewport[2] / (double) viewport[3]; - const double fovy = 50.; - const double clipNear = .01; - const double clipFar = 1000.; - gluPerspective(fovy, aspect, clipNear, clipFar); - //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); - //glOrtho(-1.0, 1.0, -1.0, 1.0, 100, 1000); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); + glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .1f, 100.f); - quaternion eye = vertex(0., 0., -2.5 * cam.zoom); + quaternion eye = vertex(0., 0., -2. * cam.zoom); quaternion center = vertex(0., 0., 0.); quaternion up = vertex(0., 1., 0.); - gluLookAt( eye[1], eye[2], eye[3], - center[1], center[2], center[3], - up[1], up[2], up[3]); + view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), + glm::vec3(center[1], center[2], center[3]), + glm::vec3(up[1], up[2], up[3]) + ); + + shader_program.enable(); quaternion r = cam.currentRotation(); eye = r.conj() * eye * r; @@ -495,7 +481,7 @@ void viewer::display() GLint uniformLight = glGetUniformLocation(shader_program, "light"); glUniform3f(uniformLight, light[1], light[2], light[3]); - cam.setView(); + //cam.setView(); GLint uniform_render_flat = glGetUniformLocation(shader_program, "render_flat"); glUniform1i(uniform_render_flat, render_flat); @@ -503,61 +489,24 @@ void viewer::display() GLint uniform_render_lines = glGetUniformLocation(shader_program, "render_lines"); glUniform1i(uniform_render_lines, render_lines); - GLfloat ModelViewMatrix[16]; - GLfloat ProjectionMatrix[16]; - - glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix); - glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMatrix); - GLint uniformModelViewMatrix = glGetUniformLocation(shader_program, "ModelViewMatrix"); + glUniformMatrix4fv(uniformModelViewMatrix, 1, 0,&view_mat[0][0]); + GLint uniformProjectionMatrix = glGetUniformLocation(shader_program, "ProjectionMatrix"); + glUniformMatrix4fv(uniformProjectionMatrix, 1, 0, &proj_mat[0][0]); - glUniformMatrix4fv(uniformModelViewMatrix, 1, 0, ModelViewMatrix); - glUniformMatrix4fv(uniformProjectionMatrix, 1, 0, ProjectionMatrix); - - //glPushAttrib(GL_ALL_ATTRIB_BITS); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); - set_mesh_material(); - - glfwGetFramebufferSize(window, &viewport_width, &viewport_height); viewport_width /= m_window_size[n_meshes - 1][1]; viewport_height /= m_window_size[n_meshes - 1][0]; + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + draw_scene(); - - //glPopAttrib(); - + shader_program.disable(); - glfwPollEvents(); -} - -void viewer::set_gl() -{ - glClearColor(bgc, bgc, bgc, 1.); - set_lighting(); -} - -void viewer::set_lighting() -{ - GLfloat position[4] = {20, 30, 40, 0}; - glLightfv(GL_LIGHT0, GL_POSITION, position); - glEnable(GL_LIGHT0); - glEnable(GL_NORMALIZE); -} - -void viewer::set_mesh_material() -{ - GLfloat diffuse[4] = { .8, .5, .3, 1. }; - GLfloat specular[4] = { .3, .3, .3, 1. }; - GLfloat ambient[4] = { .2, .2, .5, 1. }; - - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse); - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient); - glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 16.); } void viewer::draw_scene() @@ -658,7 +607,7 @@ void viewer::draw_isolated_vertices() { glPushMatrix(); glTranslated(v.x, v.y, v.z); - //glutSolidSphere(h, 10, 10); + ////glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -722,7 +671,7 @@ void viewer::draw_corr() glPushMatrix(); corr_v = meshes[corr_mesh[current].mesh_i]->corr_vertex(corr_mesh[current][v]); glTranslated(corr_v.x, corr_v.y, corr_v.z); -// glutSolidSphere(h, 10, 10); +// //glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -768,7 +717,7 @@ void viewer::draw_selected_vertices() glPushMatrix(); glTranslated(mesh()->gt(v).x, mesh()->gt(v).y, mesh()->gt(v).z); - // glutSolidSphere(h, 10, 10); + // //glutSolidSphere(h, 10, 10); glPopMatrix(); } @@ -824,7 +773,7 @@ void viewer::pick_vertex(int x, int y) glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - gluPickMatrix(x, viewport[3] - y, 10, 10, viewport); + //gluPickMatrix(x, viewport[3] - y, 10, 10, viewport); glMultMatrixd(projection); glMatrixMode(GL_MODELVIEW); @@ -873,7 +822,7 @@ void draw_str(const char * str, int x, int y, float color[4], void * font) glColor4fv(color); glRasterPos2i(x, y); - //viewport_heightile(*str) glutBitmapCharacter(font, *str++); + //viewport_heightile(*str) //glutBitmapCharacter(font, *str++); glEnable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); From 011c610af7afe964650d3af11a39630289dd28a0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Jan 2020 22:16:09 +0100 Subject: [PATCH 0111/1018] embree raycasting --- include/embree.h | 8 +++++--- shaders/vertex.glsl | 18 +++++++++--------- src/embree.cpp | 20 +++++++++++++------- src/viewer/viewer.cpp | 27 ++++++++++++--------------- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/include/embree.h b/include/embree.h index 4b9015cd..75b83f8d 100644 --- a/include/embree.h +++ b/include/embree.h @@ -3,7 +3,6 @@ #include "che.h" - #include #include @@ -78,8 +77,11 @@ class embree void build_bvh(); unsigned add_sphere(const glm::vec4 & xyzr); - unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4()); - float * raycaster(const glm::uvec2 & windows_size, const glm::mat4 & projection_view, const glm::vec3 & cam_pos, const unsigned & samples = 4); + unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + float * raycaster( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const unsigned & samples = 4 ); bool intersect(ray_hit & r); }; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 3307f05e..a6c328d3 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,21 +1,21 @@ #version 460 core -layout (location=0) in vec3 VertexPosition; -layout (location=1) in vec3 VertexNormal; -layout (location=2) in float VertexColor; +layout (location=0) in vec3 in_position; +layout (location=1) in vec3 in_normal; +layout (location=2) in float in_color; out vec3 position; out vec3 normal; out float color; -uniform mat4 ModelViewMatrix; -uniform mat4 ProjectionMatrix; +uniform mat4 model_view_mat; +uniform mat4 proj_mat; void main() { - position = VertexPosition; - normal = VertexNormal; - color = VertexColor; - gl_Position = ProjectionMatrix * ModelViewMatrix * vec4(VertexPosition, 1.); + position = in_position; + normal = in_normal; + color = in_color; + gl_Position = proj_mat * model_view_mat * vec4(position, 1.); } diff --git a/src/embree.cpp b/src/embree.cpp index 4b39ea8b..66657541 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -58,11 +58,12 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) RTC_FORMAT_UINT3, 3 * sizeof(int), mesh->n_faces()); - const glm::vec3 * V = (glm::vec3 *) &mesh->gt(0); - #pragma omp parallel for for(unsigned i = 0; i < mesh->n_vertices(); i++) - vertices[i] = glm::vec3(model_matrix * glm::vec4(V[i], 1.f)); + { + glm::vec4 v(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 1.f); + vertices[i] = glm::vec3(model_matrix * v); + } memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); @@ -74,13 +75,18 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) return geom_id; } -float * embree::raycaster(const glm::uvec2 & windows_size, const glm::mat4 & projection_view, const glm::vec3 & cam_pos, const unsigned & samples) +float * embree::raycaster( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const unsigned & samples ) { float * frame = new float[windows_size.x * windows_size.y]; std::default_random_engine gen; - std::uniform_real_distribution randf(0.0,1.0); - + std::uniform_real_distribution randf(0.f, 1.f); + + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); #pragma omp parallel for for(unsigned i = 0; i < windows_size.x; i++) for(unsigned j = 0; j < windows_size.y; j++) @@ -93,7 +99,7 @@ float * embree::raycaster(const glm::uvec2 & windows_size, const glm::mat4 & pro glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, (float(j) + randf(gen)) / windows_size.y ); glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = glm::inverse(projection_view) * view; + glm::vec4 q = inv_proj_view * view; glm::vec3 p = glm::vec3(q * (1.f / q.w)); ray_hit r(cam_pos, glm::normalize(p - cam_pos)); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 7afd48ee..8f241e6f 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -151,7 +151,7 @@ void viewer::init_gl() glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); #endif - + window = glfwCreateWindow(1600, 900, "gproshan", NULL, NULL); glfwSetWindowUserPointer(window, this); @@ -440,10 +440,10 @@ void viewer::raycasting(viewer * view) rc.build_bvh(); - glm::uvec2 window_size; - glfwGetFramebufferSize(view->window, (int *) &window_size.x, (int *) &window_size.y); - - float * frame = rc.raycaster(window_size, view->proj_mat * view->view_mat, {0., 0., -2. * view->cam.zoom}); + + float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), + view->view_mat, view->proj_mat + ); std::thread([](CImg img) { @@ -457,6 +457,8 @@ void viewer::raycasting(viewer * view) void viewer::display() { glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + viewport_width /= m_window_size[n_meshes - 1][1]; + viewport_height /= m_window_size[n_meshes - 1][0]; proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .1f, 100.f); @@ -489,24 +491,19 @@ void viewer::display() GLint uniform_render_lines = glGetUniformLocation(shader_program, "render_lines"); glUniform1i(uniform_render_lines, render_lines); - GLint uniformModelViewMatrix = glGetUniformLocation(shader_program, "ModelViewMatrix"); - glUniformMatrix4fv(uniformModelViewMatrix, 1, 0,&view_mat[0][0]); + GLint uniform_model_view_mat = glGetUniformLocation(shader_program, "model_view_mat"); + glUniformMatrix4fv(uniform_model_view_mat, 1, 0, &view_mat[0][0]); - GLint uniformProjectionMatrix = glGetUniformLocation(shader_program, "ProjectionMatrix"); - glUniformMatrix4fv(uniformProjectionMatrix, 1, 0, &proj_mat[0][0]); + GLint uniform_proj_mat = glGetUniformLocation(shader_program, "proj_mat"); + glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); - - viewport_width /= m_window_size[n_meshes - 1][1]; - viewport_height /= m_window_size[n_meshes - 1][0]; - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); shader_program.disable(); - } void viewer::draw_scene() From 6a9450237e5970af2149132a67ceb0f1aaae8fd3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Jan 2020 22:17:26 +0100 Subject: [PATCH 0112/1018] display ray casting img depth map --- src/viewer/viewer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8f241e6f..ba47ef98 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -449,7 +449,7 @@ void viewer::raycasting(viewer * view) { img.display(); }, - CImg(frame, window_size.x, window_size.y)).detach(); + CImg(frame, view->viewport_width, view->viewport_height)).detach(); delete [] frame; } From a4a0093044f9f095d5161658f8f8708397b63051 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Jan 2020 22:26:50 +0100 Subject: [PATCH 0113/1018] rotate camera --- src/viewer/viewer.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ba47ef98..5ae7ebf7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -460,31 +460,32 @@ void viewer::display() viewport_width /= m_window_size[n_meshes - 1][1]; viewport_height /= m_window_size[n_meshes - 1][0]; - proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .1f, 100.f); - quaternion eye = vertex(0., 0., -2. * cam.zoom); quaternion center = vertex(0., 0., 0.); quaternion up = vertex(0., 1., 0.); + + quaternion light = vertex(-1., 1., -2.); + quaternion r = cam.currentRotation(); + + eye = r.conj() * eye * r; + light = r.conj() * light * r; + + proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), glm::vec3(center[1], center[2], center[3]), glm::vec3(up[1], up[2], up[3]) ); + shader_program.enable(); - quaternion r = cam.currentRotation(); - eye = r.conj() * eye * r; GLint uniformEye = glGetUniformLocation(shader_program, "eye"); glUniform3f(uniformEye, eye[1], eye[2], eye[3]); - quaternion light = vertex(-1., 1., -2.); - light = r.conj() * light * r; GLint uniformLight = glGetUniformLocation(shader_program, "light"); glUniform3f(uniformLight, light[1], light[2], light[3]); - //cam.setView(); - GLint uniform_render_flat = glGetUniformLocation(shader_program, "render_flat"); glUniform1i(uniform_render_flat, render_flat); From 8337c4b75729d85cf0deff1d0fe0ba29cda6308e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Jan 2020 23:57:38 +0100 Subject: [PATCH 0114/1018] update camera and draw mesh --- include/viewer/camera.h | 1 - src/viewer/camera.cpp | 20 -------------------- src/viewer/viewer.cpp | 9 +-------- 3 files changed, 1 insertion(+), 29 deletions(-) diff --git a/include/viewer/camera.h b/include/viewer/camera.h index cb4be78d..663a295c 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -24,7 +24,6 @@ class camera public: camera(); quaternion clickToSphere(int x, int y); - void setView() const; void mouse(int button, int state, int x, int y); void motion(int x, int y); void idle(); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 2388e7af..bec9a7fb 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -53,26 +53,6 @@ quaternion camera::currentRotation() const return (pDrag * pClick.conj()) * rLast; } -void camera::setView() const -{ - quaternion r = (pDrag * pClick.conj()) * rLast; - - double w = r[0]; - double x = r[1]; - double y = r[2]; - double z = r[3]; - - GLdouble M[16] = { - 1.-2.*y*y-2.*z*z, 2.*x*y+2.*w*z, 2.*x*z-2.*w*y, 0., - 2.*x*y-2.*w*z, 1.-2.*x*x-2.*z*z, 2.*y*z+2.*w*x, 0., - 2.*x*z+2.*w*y, 2.*y*z-2.*w*x, 1.-2.*x*x-2.*y*y, 0., - 0., 0., 0., 1. - }; - - glMatrixMode(GL_MODELVIEW); - glMultMatrixd(M); -} - void camera::mouse(int , int state, int x, int y) { if(state == GLFW_PRESS) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 5ae7ebf7..eb66c977 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -71,8 +71,6 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { - glfwPollEvents(); - display(); ImGui_ImplOpenGL3_NewFrame(); @@ -110,6 +108,7 @@ bool viewer::run() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); + glfwPollEvents(); } return true; @@ -500,7 +499,6 @@ void viewer::display() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); - glEnable(GL_LIGHTING); draw_scene(); @@ -530,16 +528,11 @@ void viewer::draw_polygons() { shader_program.enable(); - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(1., 1.); - for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw(); } - - glDisable(GL_POLYGON_OFFSET_FILL); } void viewer::draw_wireframe() From ea41ffbe1b8414331b4654bb1f1250bac0ba6819 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Jan 2020 11:57:45 +0100 Subject: [PATCH 0115/1018] add render options --- include/viewer/viewer.h | 15 +++++++- src/viewer/viewer.cpp | 83 ++++++++++++++++++++++++++++------------- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 68dd707d..0bc76dbb 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -83,6 +83,12 @@ class viewer int viewport_width; int viewport_height; + + quaternion eye; + quaternion center; + quaternion up; + + quaternion light; glm::mat4 view_mat; glm::mat4 proj_mat; @@ -92,6 +98,8 @@ class viewer size_t n_meshes; index_t current; // current mesh + index_t render_opt; + bool render_wireframe; bool render_gradient_field; bool render_normal_field; @@ -131,7 +139,9 @@ class viewer void init_glsl(); void update_vbo(); - void display(); + void render_gl(); + void render_embree(); + void render_optix(); // callbacks static void idle(); @@ -157,6 +167,9 @@ class viewer // render options static void invert_orientation(viewer * view); + static void set_render_gl(viewer * view); + static void set_render_embree(viewer * view); + static void set_render_optix(viewer * view); static void set_render_wireframe(viewer * view); static void set_render_gradient_field(viewer * view); static void set_render_normal_field(viewer * view); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index eb66c977..2e39530b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -37,7 +37,9 @@ viewer::viewer() window = nullptr; n_meshes = current = 0; - + + render_opt = 0; + render_wireframe = false; render_gradient_field = false; render_normal_field = false; @@ -71,7 +73,34 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { - display(); + glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + viewport_width /= m_window_size[n_meshes - 1][1]; + viewport_height /= m_window_size[n_meshes - 1][0]; + + eye = vertex(0., 0., -2. * cam.zoom); + center = vertex(0., 0., 0.); + up = vertex(0., 1., 0.); + + light = vertex(-1., 1., -2.); + + quaternion r = cam.currentRotation(); + + eye = r.conj() * eye * r; + light = r.conj() * light * r; + + proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); + view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), + glm::vec3(center[1], center[2], center[3]), + glm::vec3(up[1], up[2], up[3]) + ); + + // RENDER + switch(render_opt) + { + case 1: render_embree(); break; + case 2: render_optix(); break; + default: render_gl(); + } ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); @@ -189,6 +218,9 @@ void viewer::init_menus() add_process(GLFW_KEY_F5, {"F5", "Normal Field", set_render_normal_field}); add_process(GLFW_KEY_F6, {"F6", "Show Borders", set_render_border}); add_process(GLFW_KEY_F7, {"F7", "Raycasting", raycasting}); + add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); + add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); + add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); // add_process('+', "Show Corr", set_render_corr); @@ -393,6 +425,21 @@ void viewer::invert_orientation(viewer * view) view->update_vbo(); } +void viewer::set_render_gl(viewer * view) +{ + view->render_opt = 0; +} + +void viewer::set_render_embree(viewer * view) +{ + view->render_opt = 1; +} + +void viewer::set_render_optix(viewer * view) +{ + view->render_opt = 2; +} + void viewer::set_render_wireframe(viewer * view) { view->render_wireframe = !view->render_wireframe; @@ -453,30 +500,8 @@ void viewer::raycasting(viewer * view) delete [] frame; } -void viewer::display() +void viewer::render_gl() { - glfwGetFramebufferSize(window, &viewport_width, &viewport_height); - viewport_width /= m_window_size[n_meshes - 1][1]; - viewport_height /= m_window_size[n_meshes - 1][0]; - - quaternion eye = vertex(0., 0., -2. * cam.zoom); - quaternion center = vertex(0., 0., 0.); - quaternion up = vertex(0., 1., 0.); - - quaternion light = vertex(-1., 1., -2.); - - quaternion r = cam.currentRotation(); - - eye = r.conj() * eye * r; - light = r.conj() * light * r; - - proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); - view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), - glm::vec3(center[1], center[2], center[3]), - glm::vec3(up[1], up[2], up[3]) - ); - - shader_program.enable(); GLint uniformEye = glGetUniformLocation(shader_program, "eye"); @@ -505,6 +530,14 @@ void viewer::display() shader_program.disable(); } +void viewer::render_embree() +{ +} + +void viewer::render_optix() +{ +} + void viewer::draw_scene() { draw_polygons(); From 4a691edae086ae083d63b0582f163cf0a7457024 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Jan 2020 15:36:00 +0100 Subject: [PATCH 0116/1018] ray tracing (raycasting) display with opengl embree --- include/embree.h | 5 +++++ include/viewer/viewer.h | 2 ++ src/embree.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 25 +++++++++++++++++++++-- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/include/embree.h b/include/embree.h index 75b83f8d..812d009e 100644 --- a/include/embree.h +++ b/include/embree.h @@ -78,6 +78,11 @@ class embree void build_bvh(); unsigned add_sphere(const glm::vec4 & xyzr); unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + void raytracing( float * frame, + const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const unsigned & samples = 4 ); float * raycaster( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 0bc76dbb..1169e27a 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -99,6 +99,8 @@ class viewer index_t current; // current mesh index_t render_opt; + embree * r_embree; + float * frame; bool render_wireframe; bool render_gradient_field; diff --git a/src/embree.cpp b/src/embree.cpp index 66657541..c7a66450 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -75,6 +75,49 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) return geom_id; } +void embree::raytracing( float * frame, + const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const unsigned & samples ) +{ + std::default_random_engine gen; + std::uniform_real_distribution randf(0.f, 1.f); + + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + + float max_color = 0; + + #pragma omp parallel for + for(unsigned i = 0; i < windows_size.x; i++) + for(unsigned j = 0; j < windows_size.y; j++) + { + //row major + float & color = frame[j * windows_size.x + i] = 0; + + for(unsigned s = 0; s < samples; s++) + { + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y ); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + ray_hit r(cam_pos, glm::normalize(p - cam_pos)); + + if(intersect(r)) color += r.ray.tfar; + } + + color /= samples; + max_color = std::max(max_color, color); + } + + #pragma omp parallel for + for(unsigned i = 0; i < windows_size.x * windows_size.y; i++) + frame[i] /= max_color; +} + float * embree::raycaster( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, @@ -87,6 +130,7 @@ float * embree::raycaster( const glm::uvec2 & windows_size, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + #pragma omp parallel for for(unsigned i = 0; i < windows_size.x; i++) for(unsigned j = 0; j < windows_size.y; j++) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2e39530b..372ef458 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -39,6 +39,8 @@ viewer::viewer() n_meshes = current = 0; render_opt = 0; + r_embree = nullptr; + frame = nullptr; render_wireframe = false; render_gradient_field = false; @@ -67,6 +69,9 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); + + if(r_embree) delete r_embree; + if(frame) delete [] frame; } bool viewer::run() @@ -483,10 +488,8 @@ void viewer::raycasting(viewer * view) embree rc; rc.add_mesh(view->mesh()); - rc.build_bvh(); - float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), view->view_mat, view->proj_mat ); @@ -532,6 +535,24 @@ void viewer::render_gl() void viewer::render_embree() { + if(r_embree == nullptr) + { + double time_build_embree; + TIC(time_build_embree); + + r_embree = new embree; + r_embree->add_mesh(mesh()); + r_embree->build_bvh(); + + frame = new float[viewport_width * viewport_height]; + + TOC(time_build_embree); + gproshan_log_var(time_build_embree); + } + + r_embree->raytracing(frame, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDrawPixels(viewport_width, viewport_height, GL_LUMINANCE, GL_FLOAT, frame); } void viewer::render_optix() From 313a6d903bbb1dbbb74ea2c400259194d0e955e7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Jan 2020 16:08:12 +0100 Subject: [PATCH 0117/1018] frame size control --- include/viewer/viewer.h | 1 + src/viewer/viewer.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 1169e27a..d1ffb737 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -101,6 +101,7 @@ class viewer index_t render_opt; embree * r_embree; float * frame; + size_t frame_size; bool render_wireframe; bool render_gradient_field; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 372ef458..965b5c09 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -544,11 +544,20 @@ void viewer::render_embree() r_embree->add_mesh(mesh()); r_embree->build_bvh(); - frame = new float[viewport_width * viewport_height]; + frame_size = viewport_width * viewport_height; + frame = new float[frame_size]; TOC(time_build_embree); gproshan_log_var(time_build_embree); } + + if(frame_size < viewport_width * viewport_height) + { + delete [] frame; + + frame_size = viewport_width * viewport_height; + frame = new float[frame_size]; + } r_embree->raytracing(frame, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); From 05915a957de524e569a7f14179d2fee7a7b627a1 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 29 Jan 2020 17:08:43 +0100 Subject: [PATCH 0118/1018] fixing curvature sampling restrictions and visualizing not taken points --- include/mdict/patch.h | 1 - src/app_viewer.cpp | 5 +++++ src/mdict/inpainting.cpp | 26 ++++++++++++++++++++------ src/mdict/patch.cpp | 31 ++++++++++++++++++++----------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index ae70e336..3e62a3e9 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -68,7 +68,6 @@ class patch index_t * _toplevel = nullptr); void init_curvature_growing(che * mesh, const index_t & v, - bool * covered, a_mat & normals); void transform(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index fc028101..edb3c490 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -542,6 +542,11 @@ void viewer_process_mask() //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); + string f_points = tmp_file_path(viewer::mesh()->name_size() + ".points"); + a_vec points_out; + points_out.load(f_points); + for(int i = 0; i< points_out.size(); i++) + viewer::select_vertices.push_back(points_out(i)); viewer::mesh().update_normals(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a45f953b..0f12183f 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -307,8 +307,10 @@ void inpainting::init_radial_curvature_patches() priority_queue< pair > Q; for(index_t v = 0; v < mesh->n_vertices(); v++) { - //if( abs (mean_curvature[v]) > 0.01 ) dist[v] = 1; - Q.push ( make_pair(-1* curvatures(v), v ) ); + + // -1 * curvatures(v) alta curvatura + // curvatures(v) baja curvatura + Q.push ( make_pair( abs(curvatures(v)), v ) ); } bool covered[mesh->n_vertices()]; @@ -333,24 +335,36 @@ void inpainting::init_radial_curvature_patches() { patch tmp; - tmp.init_curvature_growing(mesh, s, covered, normals); + tmp.init_curvature_growing(mesh, s, normals); if(tmp.vertices.size() > 0) { + gproshan_debug_var(tmp.vertices.size()); + for(index_t i = 0; i < tmp.vertices.size(); i++) + covered[ tmp.vertices[i] ] = 1; patches.push_back(tmp); count++; } - - } Q.pop(); } M = count; gproshan_debug_var(M); + vector outliers; + string f_points = tmp_file_path(mesh->name_size() + ".points"); for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) - gproshan_debug_var( covered[i] ); + { + outliers.push_back(i); + } + + } + a_vec outlv(outliers.size()); + for(index_t i = 0; i < outliers.size(); i++) + { + outlv(i) = outliers[i]; } + outlv.save(f_points); ////////////////////////////////////////////////////////////////////////////////// load_mask(); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8be6dcf9..aaa95dfc 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -91,10 +91,10 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si } -void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered, a_mat & normals) +void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normals) { radio = -INFINITY; - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/100); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/200); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); a_vec vn = normals.col(v); @@ -103,7 +103,6 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered // gproshan_debug_var(geo.n_sorted_index()); vertices.push_back(v); - covered[v] = 1; for(int i=1; iget_vertex(v); // central vertices //gather the good ones - if( (n, mesh->normal(indexes[i]) ) >= 0 ) + /*gproshan_debug_var(i); + gproshan_debug_var(acos((n, mesh->normal(indexes[i]))) ); + gproshan_debug_var(PI/2);*/ + if( acos((n, mesh->normal(indexes[i]) )) <= PI/4 ) { vertices.push_back(indexes[i]); - covered[ indexes[i]] = 1; } else + { break; + } + } @@ -138,7 +142,6 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered size_t n_vertices = vertices.size(); vertices.clear(); vertices.push_back(v); - covered[v] = 1; vn = T.col(2);// normal at the center n.x = vn(0); n.y = vn(1); n.z = vn(2); @@ -150,21 +153,27 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, bool * covered p = p - c ; p = p - ((p,n)*n); - //gproshan_debug_var((acos((n, mesh->normal(indexes[i])))*180 / PI)); - if( (acos((n, mesh->normal(indexes[i])))*180 / PI) <= 45 ) - //if( (n, mesh->normal(indexes[i])) >= 0 ) + + if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/4 ) + //if( (n, mesh->normal(indexes[i])) >= 0 ) // zerear los otroso { if(*p > radio) radio = *p; vertices.push_back(indexes[i]); - covered[ indexes[i]] = 1; } else + { break; + } + } - + // gproshan_debug(after); + // gproshan_debug_var(vertices.size()); if(vertices.size() <= min_points ) + { vertices.clear(); + } + } From adec7601e5495cac16d12b8414a8ff4e585113d6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 30 Jan 2020 11:30:51 +0100 Subject: [PATCH 0119/1018] embree raytracing frame rgba --- include/embree.h | 16 ++++++++++++---- include/viewer/viewer.h | 2 +- src/embree.cpp | 26 ++++++++++++++++---------- src/viewer/viewer.cpp | 8 ++++---- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/include/embree.h b/include/embree.h index 812d009e..713f2dc3 100644 --- a/include/embree.h +++ b/include/embree.h @@ -71,22 +71,30 @@ class embree RTCDevice device; RTCScene scene; + glm::vec3 light; + public: - embree(); + embree(const glm::vec3 & p_light = glm::vec3(1.f)); ~embree(); void build_bvh(); unsigned add_sphere(const glm::vec4 & xyzr); unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - void raytracing( float * frame, + + glm::vec4 Li(const ray_hit & r); + + void raytracing( glm::vec4 * frame, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const unsigned & samples = 4 ); + const unsigned & samples = 4 + ); + float * raycaster( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const unsigned & samples = 4 ); + const unsigned & samples = 4 + ); bool intersect(ray_hit & r); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index d1ffb737..92768663 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -100,7 +100,7 @@ class viewer index_t render_opt; embree * r_embree; - float * frame; + glm::vec4 * frame; size_t frame_size; bool render_wireframe; diff --git a/src/embree.cpp b/src/embree.cpp index c7a66450..bdeb9da4 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -8,7 +8,7 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -embree::embree() +embree::embree(const glm::vec3 & p_light): light(p_light) { device = rtcNewDevice(NULL); rtcSetDeviceErrorFunction(device, embree_error, NULL); @@ -51,12 +51,14 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices()); + mesh->n_vertices() + ); - glm::uvec3 * tri_idxs = (glm::uvec3 *) rtcSetNewGeometryBuffer(geom, + index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, - RTC_FORMAT_UINT3, 3 * sizeof(int), - mesh->n_faces()); + RTC_FORMAT_UINT3, 3 * sizeof(index_t), + mesh->n_faces() + ); #pragma omp parallel for for(unsigned i = 0; i < mesh->n_vertices(); i++) @@ -75,7 +77,12 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) return geom_id; } -void embree::raytracing( float * frame, +glm::vec4 embree::Li(const ray_hit & r) +{ + return glm::vec4(r.ray.tfar); +} + +void embree::raytracing( glm::vec4 * frame, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, @@ -87,14 +94,14 @@ void embree::raytracing( float * frame, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - float max_color = 0; + float max_color = 5; #pragma omp parallel for for(unsigned i = 0; i < windows_size.x; i++) for(unsigned j = 0; j < windows_size.y; j++) { //row major - float & color = frame[j * windows_size.x + i] = 0; + glm::vec4 & color = frame[j * windows_size.x + i] = glm::vec4(0); for(unsigned s = 0; s < samples; s++) { @@ -106,11 +113,10 @@ void embree::raytracing( float * frame, ray_hit r(cam_pos, glm::normalize(p - cam_pos)); - if(intersect(r)) color += r.ray.tfar; + if(intersect(r)) color += Li(r); } color /= samples; - max_color = std::max(max_color, color); } #pragma omp parallel for diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 965b5c09..2f397c55 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -540,12 +540,12 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - r_embree = new embree; + r_embree = new embree(glm::vec3(light[1], light[2], light[3])); r_embree->add_mesh(mesh()); r_embree->build_bvh(); frame_size = viewport_width * viewport_height; - frame = new float[frame_size]; + frame = new glm::vec4[frame_size]; TOC(time_build_embree); gproshan_log_var(time_build_embree); @@ -556,12 +556,12 @@ void viewer::render_embree() delete [] frame; frame_size = viewport_width * viewport_height; - frame = new float[frame_size]; + frame = new glm::vec4[frame_size]; } r_embree->raytracing(frame, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDrawPixels(viewport_width, viewport_height, GL_LUMINANCE, GL_FLOAT, frame); + glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, frame); } void viewer::render_optix() From 112b71175332d8c85515bedf181db1af94fbe174 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 30 Jan 2020 12:17:17 +0100 Subject: [PATCH 0120/1018] raytracing light and color --- include/embree.h | 19 +++++-------------- src/embree.cpp | 15 ++++++++------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/include/embree.h b/include/embree.h index 713f2dc3..b84417cc 100644 --- a/include/embree.h +++ b/include/embree.h @@ -35,25 +35,11 @@ struct ray_hit: public RTCRayHit hit.instID[0] = RTC_INVALID_GEOMETRY_ID; } - void org(const glm::vec3 & origin) - { - ray.org_x = origin.x; - ray.org_y = origin.y; - ray.org_z = origin.z; - } - const glm::vec3 org() const { return {ray.org_x, ray.org_y, ray.org_z}; } - void dir(const glm::vec3 & direction) - { - ray.dir_x = direction.x; - ray.dir_y = direction.y; - ray.dir_z = direction.z; - } - const glm::vec3 dir() const { return {ray.dir_x, ray.dir_y, ray.dir_z}; @@ -63,6 +49,11 @@ struct ray_hit: public RTCRayHit { return {hit.Ng_x, hit.Ng_y, hit.Ng_z}; } + + const glm::vec3 position() const + { + return org() + ray.tfar * dir(); + } }; diff --git a/src/embree.cpp b/src/embree.cpp index bdeb9da4..c6790d5d 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -79,7 +79,14 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) glm::vec4 embree::Li(const ray_hit & r) { - return glm::vec4(r.ray.tfar); + glm::vec3 color(.6f, .8f, 1.f); + + float dist_light = glm::length(light - r.position()); + float falloff = 2.f / (dist_light * dist_light); // intensity multiplier / falloff + + glm::vec3 wi = normalize(light - r.position()); + + return glm::vec4(color * falloff, 1.f);// * std::max(0.f, glm::dot(wi, r.normal())), 1.f); } void embree::raytracing( glm::vec4 * frame, @@ -94,8 +101,6 @@ void embree::raytracing( glm::vec4 * frame, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - float max_color = 5; - #pragma omp parallel for for(unsigned i = 0; i < windows_size.x; i++) for(unsigned j = 0; j < windows_size.y; j++) @@ -118,10 +123,6 @@ void embree::raytracing( glm::vec4 * frame, color /= samples; } - - #pragma omp parallel for - for(unsigned i = 0; i < windows_size.x * windows_size.y; i++) - frame[i] /= max_color; } float * embree::raycaster( const glm::uvec2 & windows_size, From b2284f040198e5284a9257d4828f01d393114087 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 30 Jan 2020 14:31:49 +0100 Subject: [PATCH 0121/1018] hard shadows --- include/embree.h | 14 ++++++++------ src/embree.cpp | 38 ++++++++++++++++++++++++++------------ src/viewer/viewer.cpp | 6 ++++-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/include/embree.h b/include/embree.h index b84417cc..56fe91e6 100644 --- a/include/embree.h +++ b/include/embree.h @@ -47,7 +47,7 @@ struct ray_hit: public RTCRayHit const glm::vec3 normal() const { - return {hit.Ng_x, hit.Ng_y, hit.Ng_z}; + return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); } const glm::vec3 position() const @@ -61,24 +61,25 @@ class embree { RTCDevice device; RTCScene scene; - - glm::vec3 light; + + RTCIntersectContext intersect_context; public: - embree(const glm::vec3 & p_light = glm::vec3(1.f)); + embree(); ~embree(); void build_bvh(); unsigned add_sphere(const glm::vec4 & xyzr); unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - glm::vec4 Li(const ray_hit & r); + glm::vec4 Li(const ray_hit & r, const glm::vec3 & light); void raytracing( glm::vec4 * frame, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const unsigned & samples = 4 + const glm::vec3 & light, + const unsigned & samples = 1 ); float * raycaster( const glm::uvec2 & windows_size, @@ -88,6 +89,7 @@ class embree ); bool intersect(ray_hit & r); + bool occluded(ray_hit & r); }; #endif // EMBREE_H diff --git a/src/embree.cpp b/src/embree.cpp index c6790d5d..d8e05bdc 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -8,12 +8,14 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -embree::embree(const glm::vec3 & p_light): light(p_light) +embree::embree() { device = rtcNewDevice(NULL); rtcSetDeviceErrorFunction(device, embree_error, NULL); scene = rtcNewScene(device); + + rtcInitIntersectContext(&intersect_context); } embree::~embree() @@ -77,22 +79,27 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) return geom_id; } -glm::vec4 embree::Li(const ray_hit & r) +glm::vec4 embree::Li(const ray_hit & r, const glm::vec3 & light) { glm::vec3 color(.6f, .8f, 1.f); float dist_light = glm::length(light - r.position()); - float falloff = 2.f / (dist_light * dist_light); // intensity multiplier / falloff + float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff glm::vec3 wi = normalize(light - r.position()); + + ray_hit ro(r.position() + 1e-5f * wi, wi); + if(occluded(ro)) + return glm::vec4(.2f * color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); - return glm::vec4(color * falloff, 1.f);// * std::max(0.f, glm::dot(wi, r.normal())), 1.f); + return glm::vec4(color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); } void embree::raytracing( glm::vec4 * frame, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, + const glm::vec3 & light, const unsigned & samples ) { std::default_random_engine gen; @@ -110,15 +117,17 @@ void embree::raytracing( glm::vec4 * frame, for(unsigned s = 0; s < samples; s++) { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y ); + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y + ); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); glm::vec4 q = inv_proj_view * view; glm::vec3 p = glm::vec3(q * (1.f / q.w)); ray_hit r(cam_pos, glm::normalize(p - cam_pos)); - if(intersect(r)) color += Li(r); + if(intersect(r)) color += Li(r, light); } color /= samples; @@ -147,8 +156,10 @@ float * embree::raycaster( const glm::uvec2 & windows_size, for(unsigned s = 0; s < samples; s++) { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y ); + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y + ); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); glm::vec4 q = inv_proj_view * view; glm::vec3 p = glm::vec3(q * (1.f / q.w)); @@ -166,10 +177,13 @@ float * embree::raycaster( const glm::uvec2 & windows_size, bool embree::intersect(ray_hit & r) { - RTCIntersectContext context; - rtcInitIntersectContext(&context); - rtcIntersect1(scene, &context, &r); + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} +bool embree::occluded(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2f397c55..50c13435 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -540,7 +540,7 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - r_embree = new embree(glm::vec3(light[1], light[2], light[3])); + r_embree = new embree; r_embree->add_mesh(mesh()); r_embree->build_bvh(); @@ -559,7 +559,9 @@ void viewer::render_embree() frame = new glm::vec4[frame_size]; } - r_embree->raytracing(frame, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, 1); + r_embree->raytracing( frame, glm::uvec2(viewport_width, viewport_height), + view_mat, proj_mat, glm::vec3(light[1], light[2], light[3])); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, frame); } From bf4c559238820dfdbbf8be5f026b7ef7b77f1205 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 30 Jan 2020 15:39:54 +0100 Subject: [PATCH 0122/1018] accumulate raytracing, hard shadows --- include/embree.h | 17 ++++++++---- include/viewer/viewer.h | 4 +-- src/embree.cpp | 57 +++++++++++++++++++++++++++++++---------- src/viewer/viewer.cpp | 35 +++++++++++++------------ 4 files changed, 76 insertions(+), 37 deletions(-) diff --git a/include/embree.h b/include/embree.h index 56fe91e6..7527c55c 100644 --- a/include/embree.h +++ b/include/embree.h @@ -60,10 +60,17 @@ struct ray_hit: public RTCRayHit class embree { RTCDevice device; - RTCScene scene; - + RTCScene scene; RTCIntersectContext intersect_context; + size_t width; + size_t height; + + size_t n_samples; + + public: + glm::vec4 * img; + public: embree(); ~embree(); @@ -74,12 +81,12 @@ class embree glm::vec4 Li(const ray_hit & r, const glm::vec3 & light); - void raytracing( glm::vec4 * frame, - const glm::uvec2 & windows_size, + bool rt_restart(const size_t & w, const size_t & h); + void raytracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const glm::vec3 & light, - const unsigned & samples = 1 + const bool & restart = false ); float * raycaster( const glm::uvec2 & windows_size, diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 92768663..ddba4f14 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -100,8 +100,8 @@ class viewer index_t render_opt; embree * r_embree; - glm::vec4 * frame; - size_t frame_size; + + bool action; bool render_wireframe; bool render_gradient_field; diff --git a/src/embree.cpp b/src/embree.cpp index d8e05bdc..85c01263 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -16,12 +16,17 @@ embree::embree() scene = rtcNewScene(device); rtcInitIntersectContext(&intersect_context); + + width = height = n_samples = 0; + img = nullptr; } embree::~embree() { rtcReleaseScene(scene); rtcReleaseDevice(device); + + if(img) delete [] img; } void embree::build_bvh() @@ -95,13 +100,38 @@ glm::vec4 embree::Li(const ray_hit & r, const glm::vec3 & light) return glm::vec4(color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); } -void embree::raytracing( glm::vec4 * frame, - const glm::uvec2 & windows_size, +bool embree::rt_restart(const size_t & w, const size_t & h) +{ + if(width * height < w * h) + { + if(img) delete [] img; + + width = w; + height = h; + img = new glm::vec4[width * height]; + + return true; + } + + if(width != w || height != h) + { + width = w; + height = h; + + return true; + } + + return false; +} + +void embree::raytracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const glm::vec3 & light, - const unsigned & samples ) + const glm::vec3 & light, const bool & restart ) { + if(rt_restart(windows_size.x, windows_size.y) || restart) + n_samples = 0; + std::default_random_engine gen; std::uniform_real_distribution randf(0.f, 1.f); @@ -109,16 +139,14 @@ void embree::raytracing( glm::vec4 * frame, glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); #pragma omp parallel for - for(unsigned i = 0; i < windows_size.x; i++) - for(unsigned j = 0; j < windows_size.y; j++) + for(unsigned i = 0; i < width; i++) + for(unsigned j = 0; j < height; j++) { //row major - glm::vec4 & color = frame[j * windows_size.x + i] = glm::vec4(0); + glm::vec4 & color = img[j * windows_size.x + i]; - for(unsigned s = 0; s < samples; s++) - { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, + (float(j) + randf(gen)) / height ); glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); @@ -127,11 +155,14 @@ void embree::raytracing( glm::vec4 * frame, ray_hit r(cam_pos, glm::normalize(p - cam_pos)); + color *= float(n_samples); + if(intersect(r)) color += Li(r, light); - } - color /= samples; + color /= float(n_samples + 1); } + + n_samples++; } float * embree::raycaster( const glm::uvec2 & windows_size, diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 50c13435..e84463b8 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -40,7 +40,6 @@ viewer::viewer() render_opt = 0; r_embree = nullptr; - frame = nullptr; render_wireframe = false; render_gradient_field = false; @@ -52,6 +51,8 @@ viewer::viewer() bgc = 0; + action = false; + init_gl(); init_imgui(); init_menus(); @@ -71,7 +72,6 @@ viewer::~viewer() glfwTerminate(); if(r_embree) delete r_embree; - if(frame) delete [] frame; } bool viewer::run() @@ -335,6 +335,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); view->cam.motion(x, y); + view->action = true; } } @@ -342,8 +343,17 @@ void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(yoffset > 0) view->cam.zoomIn(); - if(yoffset < 0) view->cam.zoomOut(); + if(yoffset > 0) + { + view->cam.zoomIn(); + view->action = true; + } + + if(yoffset < 0) + { + view->cam.zoomOut(); + view->action = true; + } } void viewer::idle() @@ -544,26 +554,17 @@ void viewer::render_embree() r_embree->add_mesh(mesh()); r_embree->build_bvh(); - frame_size = viewport_width * viewport_height; - frame = new glm::vec4[frame_size]; - TOC(time_build_embree); gproshan_log_var(time_build_embree); } - if(frame_size < viewport_width * viewport_height) - { - delete [] frame; - - frame_size = viewport_width * viewport_height; - frame = new glm::vec4[frame_size]; - } + r_embree->raytracing( glm::uvec2(viewport_width, viewport_height), + view_mat, proj_mat, glm::vec3(light[1], light[2], light[3]), action); - r_embree->raytracing( frame, glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, glm::vec3(light[1], light[2], light[3])); + action = false; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, frame); + glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, r_embree->img); } void viewer::render_optix() From 192a5d68b0add325c0861f257307aa6b34843cc2 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 30 Jan 2020 16:08:41 +0100 Subject: [PATCH 0123/1018] trying new sampling --- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 71 +++++++++++++++++++++++++++++----------- src/mdict/patch.cpp | 37 +++++++++++++-------- 3 files changed, 76 insertions(+), 34 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index edb3c490..e95d5e22 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_curvature_patches(); + dict.init_radial_patches(); //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 0f12183f..0518696a 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -204,33 +204,64 @@ void inpainting::init_radial_patches() } s = 0; + size_t it = 0; index_t * toplevel = new index_t[mesh->n_vertices()]; - while(count < mesh->n_vertices() && s < M) + while(it < M) { //Choose a sample and get the points neighboring points // Check the points are inside and add them // while( ) // mask at the end - - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); - for(auto i:patches[s].vertices) - if(!covered[i]) + if(!covered[sample(it)]) + { + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); + for(auto i:patches[s].vertices) + if(!covered[i]) + { + covered[i] = 1; + } + s++; + gproshan_debug(seed); + gproshan_debug_var(sample(it)); + /*for(index_t i = 0; i < mesh->n_vertices(); i++) { - covered[i] = 1; - count++; - } - s++; + if(!covered[i] ) + { + gproshan_debug_var(i); + } + + }*/ + + } + it++; } - gproshan_debug_var(count); - gproshan_debug_var(mesh->n_vertices()); - assert(count == mesh->n_vertices()); + + vector outliers; + string f_points = tmp_file_path(mesh->name_size() + ".points"); + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + if(!covered[i] ) + { + outliers.push_back(i); + } + + } + a_vec outlv(outliers.size()); + for(index_t i = 0; i < outliers.size(); i++) + { + outlv(i) = outliers[i]; + } + outlv.save(f_points); + + + //assert(outliers.size()>0); + M = s; // updating number of vertices + gproshan_debug_var(M); - gproshan_debug_var(s); gproshan_debug(finished); - M = s; // updating number of vertices //mask at the end no need to call the function load_mask(); @@ -310,7 +341,7 @@ void inpainting::init_radial_curvature_patches() // -1 * curvatures(v) alta curvatura // curvatures(v) baja curvatura - Q.push ( make_pair( abs(curvatures(v)), v ) ); + Q.push ( make_pair( -1 * curvatures(v), v ) ); } bool covered[mesh->n_vertices()]; @@ -523,7 +554,7 @@ void inpainting::init_voronoi_patches() distance_t inpainting::execute() { - TIC(d_time) init_radial_curvature_patches(); TOC(d_time) + TIC(d_time) init_radial_patches(); TOC(d_time) gproshan_debug_var(d_time); // L = 15; @@ -560,10 +591,10 @@ distance_t inpainting::execute() bool *pmask; - draw_patches(10); - draw_patches(200); - draw_patches(120); - draw_patches(50); + draw_patches(1); + draw_patches(3); + draw_patches(4); +// draw_patches(50); //draw_patches(400); //draw_patches(500); //draw_patches(56); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index aaa95dfc..4a24543c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -57,31 +57,41 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { - radio = radio_; + //radio = radio_; + radio = -INFINITY; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 5*avg_p); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); + a_vec vn = T.col(2);// normal at the center + vertex n; + n.x = vn(0); n.y = vn(1); n.z = vn(2); + vertex p, c; for(int i=1; iget_vertex(indexes[i]); - vertex c = mesh->get_vertex(v); // central vertices + + p = mesh->get_vertex(indexes[i]); + c = mesh->get_vertex(v); // central vertices p = p - c ; p = p - ((p,n)*n); - if(*p <= radio && ( n, mesh->normal(indexes[i]) ) >= 0) + //if(*p <= radio && ( n, mesh->normal(indexes[i]) ) >= 0) + if( *p <= 2*radio && acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) { + if(*p > radio) + radio = *p; _vertices.push_back(indexes[i]); } + else + { + break; + } } @@ -94,12 +104,12 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normals) { radio = -INFINITY; - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/200); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); a_vec vn = normals.col(v); - vertex n; - n.x = vn(0); n.y = vn(1); n.z = vn(2); + vertex n = mesh->normal( v ) ; + //n.x = vn(0); n.y = vn(1); n.z = vn(2); // gproshan_debug_var(geo.n_sorted_index()); vertices.push_back(v); @@ -116,7 +126,8 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normal /*gproshan_debug_var(i); gproshan_debug_var(acos((n, mesh->normal(indexes[i]))) ); gproshan_debug_var(PI/2);*/ - if( acos((n, mesh->normal(indexes[i]) )) <= PI/4 ) + // + if( acos((n, mesh->normal(indexes[i]) )) <= PI/2 ) { vertices.push_back(indexes[i]); } @@ -154,7 +165,7 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normal p = p - c ; p = p - ((p,n)*n); - if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/4 ) + if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) //if( (n, mesh->normal(indexes[i])) >= 0 ) // zerear los otroso { if(*p > radio) From ccc960cead283d803fff1fa35482bbf509836ba6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 30 Jan 2020 21:53:07 +0100 Subject: [PATCH 0124/1018] render rt point cloud --- include/che.h | 2 +- include/embree.h | 7 +++--- src/che.cpp | 2 +- src/embree.cpp | 56 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/include/che.h b/include/che.h index eb3a95de..16441b29 100644 --- a/include/che.h +++ b/include/che.h @@ -66,7 +66,7 @@ class che area_t area_vertex(const index_t & v); area_t area_surface() const; vertex normal_he(const index_t & he) const; - vertex normal(const index_t & v); + vertex normal(const index_t & v) const; vertex gradient_he(const index_t & he, const distance_t *const & f) const; vertex gradient(const index_t & v, const distance_t *const & f); vertex barycenter(const index_t & t) const; diff --git a/include/embree.h b/include/embree.h index 7527c55c..1adc6a63 100644 --- a/include/embree.h +++ b/include/embree.h @@ -76,8 +76,9 @@ class embree ~embree(); void build_bvh(); - unsigned add_sphere(const glm::vec4 & xyzr); - unsigned add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_sphere(const glm::vec4 & xyzr); + index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); glm::vec4 Li(const ray_hit & r, const glm::vec3 & light); @@ -92,7 +93,7 @@ class embree float * raycaster( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const unsigned & samples = 4 + const index_t & samples = 4 ); bool intersect(ray_hit & r); diff --git a/src/che.cpp b/src/che.cpp index 6c131097..bec3fd0f 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -249,7 +249,7 @@ vertex che::normal_he(const index_t & he) const return n / *n; } -vertex che::normal(const index_t & v) +vertex che::normal(const index_t & v) const { vertex n; area_t area, area_star = 0; diff --git a/src/embree.cpp b/src/embree.cpp index 85c01263..bd8e0623 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -34,7 +34,7 @@ void embree::build_bvh() rtcCommitScene(scene); } -unsigned embree::add_sphere(const glm::vec4 & xyzr) +index_t embree::add_sphere(const glm::vec4 & xyzr) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); @@ -45,13 +45,13 @@ unsigned embree::add_sphere(const glm::vec4 & xyzr) rtcCommitGeometry(geom); - unsigned geom_id = rtcAttachGeometry(scene, geom); + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); return geom_id; } -unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) +index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); @@ -68,7 +68,7 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) ); #pragma omp parallel for - for(unsigned i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices(); i++) { glm::vec4 v(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 1.f); vertices[i] = glm::vec3(model_matrix * v); @@ -78,12 +78,46 @@ unsigned embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) rtcCommitGeometry(geom); - unsigned geom_id = rtcAttachGeometry(scene, geom); + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); return geom_id; } +index_t embree::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, + 4 * sizeof(float), + mesh->n_vertices() + ); + + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_NORMAL, 0, + RTC_FORMAT_FLOAT3, + 3 * sizeof(float), + mesh->n_vertices() + ); + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.005f); + + vertex n = mesh->normal(i); + normal[i] = glm::vec3(n.x, n.y, n.z); + } + + rtcCommitGeometry(geom); + + index_t geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + glm::vec4 embree::Li(const ray_hit & r, const glm::vec3 & light) { glm::vec3 color(.6f, .8f, 1.f); @@ -139,8 +173,8 @@ void embree::raytracing( const glm::uvec2 & windows_size, glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); #pragma omp parallel for - for(unsigned i = 0; i < width; i++) - for(unsigned j = 0; j < height; j++) + for(index_t i = 0; i < width; i++) + for(index_t j = 0; j < height; j++) { //row major glm::vec4 & color = img[j * windows_size.x + i]; @@ -168,7 +202,7 @@ void embree::raytracing( const glm::uvec2 & windows_size, float * embree::raycaster( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const unsigned & samples ) + const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; @@ -179,13 +213,13 @@ float * embree::raycaster( const glm::uvec2 & windows_size, glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); #pragma omp parallel for - for(unsigned i = 0; i < windows_size.x; i++) - for(unsigned j = 0; j < windows_size.y; j++) + for(index_t i = 0; i < windows_size.x; i++) + for(index_t j = 0; j < windows_size.y; j++) { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; - for(unsigned s = 0; s < samples; s++) + for(index_t s = 0; s < samples; s++) { glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, (float(j) + randf(gen)) / windows_size.y From de21a545d8d0ac911a765e4788cd8322782f5310 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 10:38:02 +0100 Subject: [PATCH 0125/1018] organized menus imgui --- src/embree.cpp | 2 +- src/viewer/viewer.cpp | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/embree.cpp b/src/embree.cpp index bd8e0623..90658e84 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -104,7 +104,7 @@ index_t embree::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.005f); + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001f); vertex n = mesh->normal(i); normal[i] = glm::vec3(n.x, n.y, n.z); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e84463b8..d3cf98fc 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -18,6 +18,7 @@ #include "CImg.h" + using namespace cimg_library; using namespace std; @@ -214,32 +215,31 @@ void viewer::init_imgui() void viewer::init_menus() { - // init viewer menu sub_menus.push_back("Viewer"); add_process(GLFW_KEY_F1, {"F1", "Help", menu_help}); - add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); - add_process(GLFW_KEY_F3, {"F3", "Render Wireframe", set_render_wireframe}); - add_process(GLFW_KEY_F4, {"F4", "Gradient Field", set_render_gradient_field}); - add_process(GLFW_KEY_F5, {"F5", "Normal Field", set_render_normal_field}); - add_process(GLFW_KEY_F6, {"F6", "Show Borders", set_render_border}); - add_process(GLFW_KEY_F7, {"F7", "Raycasting", raycasting}); - add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); - add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); - add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); - add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); - add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); -// add_process('+', "Show Corr", set_render_corr); - - // init mesh menu - sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); - add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); add_process(GLFW_KEY_UP, {"UP", "Zoom in", menu_zoom_in}); add_process(GLFW_KEY_DOWN, {"DOWN", "Zoom out", menu_zoom_out}); add_process(GLFW_KEY_RIGHT, {"RIGHT", "Background color inc", menu_bgc_inc}); add_process(GLFW_KEY_LEFT, {"LEFT", "Background color dec", menu_bgc_dec}); add_process(GLFW_KEY_1, {"1", "Background color white", menu_bgc_white}); add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); + + sub_menus.push_back("Render"); + add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); + add_process(GLFW_KEY_F7, {"F7", "Render GL", set_render_gl}); + add_process(GLFW_KEY_F8, {"F8", "Render Embree", set_render_embree}); + add_process(GLFW_KEY_F9, {"F9", "Render OptiX", set_render_optix}); + add_process(GLFW_KEY_F10, {"F10", "Raycasting", raycasting}); + add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); + + sub_menus.push_back("Mesh"); + add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); + add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); + add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); + add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradient_field}); + add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normal_field}); + add_process(GLFW_KEY_F5, {"F5", "Show Borders", set_render_border}); + add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); } void viewer::init_glsl() From 3f598071555d58a17c77dd2e027c922f52c072ac Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 12:44:43 +0100 Subject: [PATCH 0126/1018] fix build embree and glfw --- .travis.yml | 3 +-- CMakeLists.txt | 24 +++++++++++++++--------- include/embree.h | 4 ++++ include/viewer/viewer.h | 7 ++++++- src/embree.cpp | 4 ++++ src/viewer/viewer.cpp | 9 ++++++++- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4871bab0..3fcafeb3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,8 +15,7 @@ addons: - libopenblas-dev - libsuitesparse-dev - libglew-dev - - freeglut3-dev - - libgles2-mesa-dev + - libglfw3-dev - cimg-dev - libcgal-dev script: diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d0c90d3..1bf4dd19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,11 +4,26 @@ project(gproshan VERSION 2.0 LANGUAGES CXX) list(APPEND CMAKE_MODULE_PATH "${gproshan_SOURCE_DIR}/cmake") +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + + find_package(CUDA 10.1) if(CUDA_FOUND) enable_language(CUDA) + add_definitions(-DGPROSHAN_CUDA) + include_directories(${CUDA_INCLUDE_DIRS}) endif(CUDA_FOUND) + +find_package(Embree 3.7) +if(EMBREE_LIBRARY) + add_definitions(-DGPROSHAN_EMBREE) + include_directories(${EMBREE_INCLUDE_DIRS}) +endif(EMBREE_LIBRARY) + + find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) @@ -19,11 +34,7 @@ find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) find_package(Eigen3 REQUIRED) find_package(SuiteSparse REQUIRED) -find_package(Embree 3.6 REQUIRED) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() set(CMAKE_CXX_FLAGS "-fopenmp -Wall -Wno-unused-result") set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") @@ -35,11 +46,6 @@ add_definitions(${EIGEN3_DEFINITIONS}) # ARMA warning ARMA_ALLOW_FAKE_GCC with g++-9 add_definitions(-DARMA_ALLOW_FAKE_GCC) -if(CUDA_FOUND) - add_definitions(-DGPROSHAN_CUDA) - include_directories(${CUDA_INCLUDE_DIRS}) -endif(CUDA_FOUND) - include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${GLEW_INCLUDE_DIRS}) include_directories(${GLFW3_INCLUDE_DIRS}) diff --git a/include/embree.h b/include/embree.h index 1adc6a63..f3dec464 100644 --- a/include/embree.h +++ b/include/embree.h @@ -1,3 +1,5 @@ +#ifdef GPROSHAN_EMBREE + #ifndef EMBREE_H #define EMBREE_H @@ -102,3 +104,5 @@ class embree #endif // EMBREE_H +#endif // GPROSHAN_EMBREE + diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index ddba4f14..e689c8fc 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -14,7 +14,9 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" -#include "embree.h" +#ifdef GPROSHAN_EMBREE + #include "embree.h" +#endif // GPROSHAN_EMBREE #define N_MESHES 12 @@ -99,7 +101,10 @@ class viewer index_t current; // current mesh index_t render_opt; + + #ifdef GPROSHAN_EMBREE embree * r_embree; + #endif // GPROSHAN_EMBREE bool action; diff --git a/src/embree.cpp b/src/embree.cpp index 90658e84..38a02361 100644 --- a/src/embree.cpp +++ b/src/embree.cpp @@ -1,5 +1,7 @@ #include "embree.h" +#ifdef GPROSHAN_EMBREE + #include #include @@ -252,3 +254,5 @@ bool embree::occluded(ray_hit & r) return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } +#endif // GPROSHAN_EMBREE + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d3cf98fc..fecbdea1 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -40,7 +40,10 @@ viewer::viewer() n_meshes = current = 0; render_opt = 0; - r_embree = nullptr; + + #ifdef GPROSHAN_EMBREE + r_embree = nullptr; + #endif // GPROSHAN_EMBREE render_wireframe = false; render_gradient_field = false; @@ -545,6 +548,8 @@ void viewer::render_gl() void viewer::render_embree() { +#ifdef GPROSHAN_EMBREE + if(r_embree == nullptr) { double time_build_embree; @@ -565,6 +570,8 @@ void viewer::render_embree() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, r_embree->img); + +#endif // GPROSHAN_EMBREE } void viewer::render_optix() From 5d7a9bf23f5d07674eaaa827f6066bf8fb8d3a4d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 12:50:40 +0100 Subject: [PATCH 0127/1018] glew glfw versions --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bf4dd19..62c38a37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,8 +27,8 @@ endif(EMBREE_LIBRARY) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) -find_package(GLEW 2.1 REQUIRED) -find_package(glfw3 3.3 REQUIRED) +find_package(GLEW 2 REQUIRED) +find_package(glfw3 3 REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) From 67bce46b47ced40a7f5154cf4c2c72f24b324299 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 14:57:14 +0100 Subject: [PATCH 0128/1018] glm include --- include/viewer/viewer.h | 2 ++ src/viewer/viewer.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index e689c8fc..303a3267 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -4,6 +4,8 @@ #include #include +#include + #include "camera.h" #include "shader.h" #include "che_viewer.h" diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index fecbdea1..5979c86f 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -75,7 +75,9 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); - if(r_embree) delete r_embree; + #ifdef GPROSHAN_EMBREE + if(r_embree) delete r_embree; + #endif // GPROSHAN_EMBREE } bool viewer::run() @@ -496,6 +498,8 @@ void viewer::set_is_flat(viewer * view) void viewer::raycasting(viewer * view) { +#ifdef GPROSHAN_EMBREE + gproshan_log(VIEWER); embree rc; @@ -514,6 +518,8 @@ void viewer::raycasting(viewer * view) CImg(frame, view->viewport_width, view->viewport_height)).detach(); delete [] frame; + +#endif // GPROSHAN_EMBREE } void viewer::render_gl() From d180f13010848d5f21ccec94c93787c33ec738e5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 15:03:18 +0100 Subject: [PATCH 0129/1018] update dependencies --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 3fcafeb3..efefe2ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ addons: - libsuitesparse-dev - libglew-dev - libglfw3-dev + - libglm-dev - cimg-dev - libcgal-dev script: From fccf6246435c42680d03ed97afc3314803c89656 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 16:09:20 +0100 Subject: [PATCH 0130/1018] drawing wireframe, pending draw triangles on surface --- include/viewer/viewer.h | 6 --- src/viewer/viewer.cpp | 108 +++------------------------------------- 2 files changed, 8 insertions(+), 106 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 303a3267..ed002c8a 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -114,7 +114,6 @@ class viewer bool render_gradient_field; bool render_normal_field; bool render_border; - bool render_corr; bool render_lines; bool render_flat; float bgc; @@ -185,19 +184,16 @@ class viewer static void set_render_normal_field(viewer * view); static void set_render_border(viewer * view); static void set_render_lines(viewer * view); - static void set_render_corr(viewer * view); static void set_is_flat(viewer * view); static void raycasting(viewer * view); // draw routines void draw_scene(); - void draw_corr(); void draw_polygons(); void draw_wireframe(); void draw_gradient_field(); void draw_normal_field(); - void draw_vertices(); void draw_border(); void draw_selected_vertices(); void draw_vectors(); @@ -206,8 +202,6 @@ class viewer void pick_vertex(int x, int y); }; -void draw_str(const char * str, int x, int y, float color[4], void * font); - } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 5979c86f..ddececfa 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -50,7 +50,6 @@ viewer::viewer() render_normal_field = false; render_border = false; render_lines = false; - render_corr = false; render_flat = false; bgc = 0; @@ -187,8 +186,6 @@ void viewer::init_gl() glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); #endif window = glfwCreateWindow(1600, 900, "gproshan", NULL, NULL); @@ -204,6 +201,11 @@ void viewer::init_gl() glfwSwapInterval(0); glewInit(); + + + glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } void viewer::init_imgui() @@ -486,11 +488,6 @@ void viewer::set_render_lines(viewer * view) view->render_lines = !view->render_lines; } -void viewer::set_render_corr(viewer * view) -{ - view->render_corr = !view->render_corr; -} - void viewer::set_is_flat(viewer * view) { view->render_flat = !view->render_flat; @@ -544,9 +541,7 @@ void viewer::render_gl() GLint uniform_proj_mat = glGetUniformLocation(shader_program, "proj_mat"); glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); - + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); shader_program.disable(); @@ -592,7 +587,6 @@ void viewer::draw_scene() if(render_gradient_field) draw_gradient_field(); if(render_normal_field) draw_normal_field(); if(render_border) draw_border(); - if(render_corr) draw_corr(); draw_isolated_vertices(); draw_vectors(); @@ -605,7 +599,7 @@ void viewer::draw_scene() void viewer::draw_polygons() { - shader_program.enable(); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); for(index_t i = 0; i < n_meshes; i++) { @@ -616,14 +610,6 @@ void viewer::draw_polygons() void viewer::draw_wireframe() { - shader_program.disable(); - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glDisable(GL_LIGHTING); - glColor4f(0., 0., 0., 0.4); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); for(index_t i = 0; i < n_meshes; i++) @@ -631,8 +617,6 @@ void viewer::draw_wireframe() glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); meshes[i].draw(); } - - glPopAttrib(); } void viewer::draw_vectors() @@ -702,66 +686,6 @@ void viewer::draw_isolated_vertices() glPopAttrib();*/ } -void viewer::draw_corr() -{ - if(n_meshes < 2) return; - if(!corr_mesh[current].is_loaded()) return; - - shader_program.disable(); - - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glDisable(GL_LIGHTING); - glColor3f(0.8, .0, .0); - glLineWidth(2.0); - - vertex corr_v; - - glBegin(GL_LINES); - for(index_t & v: select_vertices) - { - corr_v = meshes[corr_mesh[current].mesh_i]->corr_vertex(corr_mesh[current][v]); - glVertex3v(&meshes[current]->gt(v).x); - glVertex3v(&corr_v.x); - } - glEnd(); - - glPopAttrib(); - - // spheres corr - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glEnable(GL_COLOR_MATERIAL); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glColor3f(0.8, 0.0, 0.0); - - double h = 0.008 * cam.zoom; - for(index_t & v: select_vertices) - { - glPushMatrix(); - corr_v = meshes[corr_mesh[current].mesh_i]->corr_vertex(corr_mesh[current][v]); - glTranslated(corr_v.x, corr_v.y, corr_v.z); -// //glutSolidSphere(h, 10, 10); - glPopMatrix(); - } - - glEnd(); - - glPopAttrib(); - -} - -void viewer::draw_vertices() -{ - for(index_t v = 0; v < mesh()->n_vertices(); v++) - { - glLoadName(v); - glBegin(GL_POINTS); - glVertex3v(&mesh()->gt(v).x); - glEnd(); - } -} - void viewer::draw_border() { select_vertices.clear(); @@ -848,7 +772,7 @@ void viewer::pick_vertex(int x, int y) glMatrixMode(GL_MODELVIEW); glPushMatrix(); - draw_vertices(); +// draw_vertices(); glPopMatrix(); glMatrixMode(GL_PROJECTION); @@ -883,22 +807,6 @@ void viewer::pick_vertex(int x, int y) } } -void draw_str(const char * str, int x, int y, float color[4], void * font) -{ - glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - - glColor4fv(color); - glRasterPos2i(x, y); - - //viewport_heightile(*str) //glutBitmapCharacter(font, *str++); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_LIGHTING); - glPopAttrib(); -} - } // namespace gproshan From c904ada392fe8a92ca7aade8f70ca717652d27f3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 16:17:06 +0100 Subject: [PATCH 0131/1018] drawing polygonos fill/line --- include/viewer/viewer.h | 1 - src/viewer/viewer.cpp | 16 ++++------------ 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index ed002c8a..71d35666 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -191,7 +191,6 @@ class viewer // draw routines void draw_scene(); void draw_polygons(); - void draw_wireframe(); void draw_gradient_field(); void draw_normal_field(); void draw_border(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ddececfa..42581f50 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -583,7 +583,6 @@ void viewer::draw_scene() { draw_polygons(); - if(render_wireframe) draw_wireframe(); if(render_gradient_field) draw_gradient_field(); if(render_normal_field) draw_normal_field(); if(render_border) draw_border(); @@ -599,19 +598,12 @@ void viewer::draw_scene() void viewer::draw_polygons() { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + if(render_wireframe) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - for(index_t i = 0; i < n_meshes; i++) - { - glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - meshes[i].draw(); - } -} -void viewer::draw_wireframe() -{ - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); From 7e3ab14e3a4b692f98eb39b359e5f08aa09d3562 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 16:59:46 +0100 Subject: [PATCH 0132/1018] add raytracing src folder --- CMakeLists.txt | 1 + include/{ => raytracing}/embree.h | 0 src/{ => raytracing}/embree.cpp | 1 + 3 files changed, 2 insertions(+) rename include/{ => raytracing}/embree.h (100%) rename src/{ => raytracing}/embree.cpp (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62c38a37..a9592e2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/include/viewer) include_directories(${gproshan_SOURCE_DIR}/include/mdict) include_directories(${gproshan_SOURCE_DIR}/include/cuda) +include_directories(${gproshan_SOURCE_DIR}/include/raytracing) include_directories(${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) diff --git a/include/embree.h b/include/raytracing/embree.h similarity index 100% rename from include/embree.h rename to include/raytracing/embree.h diff --git a/src/embree.cpp b/src/raytracing/embree.cpp similarity index 99% rename from src/embree.cpp rename to src/raytracing/embree.cpp index 38a02361..3ecd876b 100644 --- a/src/embree.cpp +++ b/src/raytracing/embree.cpp @@ -4,6 +4,7 @@ #include #include +#include void embree_error(void * ptr, RTCError error, const char * str) { From 5b8af6311362236156b2a9f3876ba6f45bb5b842 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Jan 2020 22:55:41 +0100 Subject: [PATCH 0133/1018] add raytracing abstraction --- include/raytracing/embree.h | 41 ++++------ include/raytracing/raytracing.h | 56 ++++++++++++++ include/viewer/viewer.h | 2 +- src/raytracing/embree.cpp | 133 ++++++-------------------------- src/raytracing/raytracing.cpp | 126 ++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 18 ++--- 6 files changed, 229 insertions(+), 147 deletions(-) create mode 100644 include/raytracing/raytracing.h create mode 100644 src/raytracing/raytracing.cpp diff --git a/include/raytracing/embree.h b/include/raytracing/embree.h index f3dec464..91146df1 100644 --- a/include/raytracing/embree.h +++ b/include/raytracing/embree.h @@ -4,12 +4,16 @@ #define EMBREE_H #include "che.h" +#include "raytracing.h" #include #include -using namespace gproshan; +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + struct ray_hit: public RTCRayHit { @@ -59,20 +63,12 @@ struct ray_hit: public RTCRayHit }; -class embree +class embree : public raytracing { RTCDevice device; RTCScene scene; RTCIntersectContext intersect_context; - size_t width; - size_t height; - - size_t n_samples; - - public: - glm::vec4 * img; - public: embree(); ~embree(); @@ -82,26 +78,19 @@ class embree index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - glm::vec4 Li(const ray_hit & r, const glm::vec3 & light); - - bool rt_restart(const size_t & w, const size_t & h); - void raytracing( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const glm::vec3 & light, - const bool & restart = false - ); - - float * raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const index_t & samples = 4 - ); - + glm::vec4 li(const ray_hit & r, const glm::vec3 & light); + + private: bool intersect(ray_hit & r); bool occluded(ray_hit & r); + + const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); }; + +} // namespace gproshan + #endif // EMBREE_H #endif // GPROSHAN_EMBREE diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h new file mode 100644 index 00000000..c225e197 --- /dev/null +++ b/include/raytracing/raytracing.h @@ -0,0 +1,56 @@ +#ifndef RAYTRACING_H +#define RAYTRACING_H + +#include "che.h" + +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +class raytracing +{ + protected: + size_t width; + size_t height; + size_t n_samples; + + public: + glm::vec4 * img; + + public: + raytracing(); + virtual ~raytracing(); + + bool rt_restart(const size_t & w, const size_t & h); + void pathtracing( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const glm::vec3 & light, + const bool & restart = false + ); + + float * raycaster( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const index_t & samples = 4 + ); + + + protected: + virtual const glm::vec4 intersect_li( const glm::vec3 & org, + const glm::vec3 & dir, + const glm::vec3 & light ) = 0; + + virtual const float intersect_depth( const glm::vec3 & org, + const glm::vec3 & dir ) = 0; +}; + + +} // namespace gproshan + +#endif // RAYTRACING_H + diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 71d35666..9c5cfd7d 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -105,7 +105,7 @@ class viewer index_t render_opt; #ifdef GPROSHAN_EMBREE - embree * r_embree; + rt::embree * rt_embree; #endif // GPROSHAN_EMBREE bool action; diff --git a/src/raytracing/embree.cpp b/src/raytracing/embree.cpp index 3ecd876b..9591045f 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/embree.cpp @@ -6,6 +6,12 @@ #include #include + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + void embree_error(void * ptr, RTCError error, const char * str) { fprintf(stderr, "EMBREE ERROR: %s\n", str); @@ -19,17 +25,12 @@ embree::embree() scene = rtcNewScene(device); rtcInitIntersectContext(&intersect_context); - - width = height = n_samples = 0; - img = nullptr; } embree::~embree() { rtcReleaseScene(scene); rtcReleaseDevice(device); - - if(img) delete [] img; } void embree::build_bvh() @@ -121,7 +122,7 @@ index_t embree::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix return geom_id; } -glm::vec4 embree::Li(const ray_hit & r, const glm::vec3 & light) +glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) { glm::vec3 color(.6f, .8f, 1.f); @@ -137,123 +138,33 @@ glm::vec4 embree::Li(const ray_hit & r, const glm::vec3 & light) return glm::vec4(color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); } -bool embree::rt_restart(const size_t & w, const size_t & h) +bool embree::intersect(ray_hit & r) { - if(width * height < w * h) - { - if(img) delete [] img; - - width = w; - height = h; - img = new glm::vec4[width * height]; - - return true; - } - - if(width != w || height != h) - { - width = w; - height = h; - - return true; - } - - return false; + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } -void embree::raytracing( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const glm::vec3 & light, const bool & restart ) +bool embree::occluded(ray_hit & r) { - if(rt_restart(windows_size.x, windows_size.y) || restart) - n_samples = 0; - - std::default_random_engine gen; - std::uniform_real_distribution randf(0.f, 1.f); - - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - - #pragma omp parallel for - for(index_t i = 0; i < width; i++) - for(index_t j = 0; j < height; j++) - { - //row major - glm::vec4 & color = img[j * windows_size.x + i]; - - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, - (float(j) + randf(gen)) / height - ); - - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); - - ray_hit r(cam_pos, glm::normalize(p - cam_pos)); - - color *= float(n_samples); - - if(intersect(r)) color += Li(r, light); - - color /= float(n_samples + 1); - } - - n_samples++; + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } -float * embree::raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const index_t & samples ) -{ - float * frame = new float[windows_size.x * windows_size.y]; - - std::default_random_engine gen; - std::uniform_real_distribution randf(0.f, 1.f); - - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - #pragma omp parallel for - for(index_t i = 0; i < windows_size.x; i++) - for(index_t j = 0; j < windows_size.y; j++) - { - //row major - float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; - - for(index_t s = 0; s < samples; s++) - { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y - ); - - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); - - ray_hit r(cam_pos, glm::normalize(p - cam_pos)); - - if(intersect(r)) color += r.ray.tfar; - } - - color /= samples; - } - - return frame; -} - -bool embree::intersect(ray_hit & r) +const glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) { - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; + ray_hit r(org, dir); + return intersect(r) ? li(r, light) : glm::vec4(0.f); } -bool embree::occluded(ray_hit & r) +const float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) { - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; + ray_hit r(org, dir); + return intersect(r) ? r.ray.tfar : 0.f; } + +} // namespace gproshan + #endif // GPROSHAN_EMBREE diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp new file mode 100644 index 00000000..f7cce6d1 --- /dev/null +++ b/src/raytracing/raytracing.cpp @@ -0,0 +1,126 @@ +#include "raytracing.h" + +#include +#include +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +raytracing::raytracing() +{ + width = height = n_samples = 0; + img = nullptr; +} + +raytracing::~raytracing() +{ + if(img) delete [] img; +} + +bool raytracing::rt_restart(const size_t & w, const size_t & h) +{ + if(width * height < w * h) + { + if(img) delete [] img; + + width = w; + height = h; + img = new glm::vec4[width * height]; + + return true; + } + + if(width != w || height != h) + { + width = w; + height = h; + + return true; + } + + return false; +} + +void raytracing::pathtracing( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const glm::vec3 & light, const bool & restart ) +{ + if(rt_restart(windows_size.x, windows_size.y) || restart) + n_samples = 0; + + std::default_random_engine gen; + std::uniform_real_distribution randf(0.f, 1.f); + + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + + #pragma omp parallel for + for(index_t i = 0; i < width; i++) + for(index_t j = 0; j < height; j++) + { + //row major + glm::vec4 & color = img[j * windows_size.x + i]; + + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, + (float(j) + randf(gen)) / height + ); + + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + color *= float(n_samples); + color += intersect_li(cam_pos, glm::normalize(p - cam_pos), light); + color /= float(n_samples + 1); + } + + n_samples++; +} + +float * raytracing::raycaster( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const index_t & samples ) +{ + float * frame = new float[windows_size.x * windows_size.y]; + + std::default_random_engine gen; + std::uniform_real_distribution randf(0.f, 1.f); + + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + + #pragma omp parallel for + for(index_t i = 0; i < windows_size.x; i++) + for(index_t j = 0; j < windows_size.y; j++) + { + //row major + float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; + + for(index_t s = 0; s < samples; s++) + { + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y + ); + + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + color += intersect_depth(cam_pos, glm::normalize(p - cam_pos)); + } + + color /= samples; + } + + return frame; +} + + +} // namespace gproshan + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 42581f50..22f94c9c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -42,7 +42,7 @@ viewer::viewer() render_opt = 0; #ifdef GPROSHAN_EMBREE - r_embree = nullptr; + rt_embree = nullptr; #endif // GPROSHAN_EMBREE render_wireframe = false; @@ -75,7 +75,7 @@ viewer::~viewer() glfwTerminate(); #ifdef GPROSHAN_EMBREE - if(r_embree) delete r_embree; + if(rt_embree) delete rt_embree; #endif // GPROSHAN_EMBREE } @@ -499,7 +499,7 @@ void viewer::raycasting(viewer * view) gproshan_log(VIEWER); - embree rc; + rt::embree rc; rc.add_mesh(view->mesh()); rc.build_bvh(); @@ -551,26 +551,26 @@ void viewer::render_embree() { #ifdef GPROSHAN_EMBREE - if(r_embree == nullptr) + if(rt_embree == nullptr) { double time_build_embree; TIC(time_build_embree); - r_embree = new embree; - r_embree->add_mesh(mesh()); - r_embree->build_bvh(); + rt_embree = new rt::embree; + rt_embree->add_mesh(mesh()); + rt_embree->build_bvh(); TOC(time_build_embree); gproshan_log_var(time_build_embree); } - r_embree->raytracing( glm::uvec2(viewport_width, viewport_height), + rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, glm::vec3(light[1], light[2], light[3]), action); action = false; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, r_embree->img); + glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_embree->img); #endif // GPROSHAN_EMBREE } From 13f395260c1b48aa9566927a572d334187e0d889 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 3 Feb 2020 15:29:20 +0100 Subject: [PATCH 0134/1018] normal field draw shader --- include/viewer/che_viewer.h | 1 - include/viewer/viewer.h | 3 ++- shaders/fragment_normals.glsl | 9 +++++++++ shaders/geometry_normals.glsl | 20 ++++++++++++++++++++ shaders/vertex_normals.glsl | 16 ++++++++++++++++ src/viewer/che_viewer.cpp | 23 ----------------------- src/viewer/viewer.cpp | 33 +++++++++++++++++++-------------- 7 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 shaders/fragment_normals.glsl create mode 100644 shaders/geometry_normals.glsl create mode 100644 shaders/vertex_normals.glsl diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 05f89b4b..cb6e64d7 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -53,7 +53,6 @@ class che_viewer void update_normals(); void update_colors(const color_t *const c = nullptr); void draw(); - void draw_normal_field(); void draw_gradient_field(); void draw_mesh_info(); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 9c5cfd7d..283b6ae0 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -83,6 +83,8 @@ class viewer GLFWwindow * window; shader shader_program; + shader shader_normals; + shader shader_edges; camera cam; int viewport_width; @@ -192,7 +194,6 @@ class viewer void draw_scene(); void draw_polygons(); void draw_gradient_field(); - void draw_normal_field(); void draw_border(); void draw_selected_vertices(); void draw_vectors(); diff --git a/shaders/fragment_normals.glsl b/shaders/fragment_normals.glsl new file mode 100644 index 00000000..85ec7c42 --- /dev/null +++ b/shaders/fragment_normals.glsl @@ -0,0 +1,9 @@ +#version 460 core + +layout(location = 0) out vec4 FragColor; + +void main() +{ + FragColor = vec4(.8, .8, 1., 1.); +} + diff --git a/shaders/geometry_normals.glsl b/shaders/geometry_normals.glsl new file mode 100644 index 00000000..866ba46a --- /dev/null +++ b/shaders/geometry_normals.glsl @@ -0,0 +1,20 @@ +#version 460 core + +layout (triangles) in; +layout (line_strip, max_vertices = 2) out; + +in vec3 normal[]; + +const float lenght = 0.02; + +void main() +{ + gl_Position = gl_in[0].gl_Position; + EmitVertex(); + + gl_Position = gl_in[0].gl_Position + vec4(normal[0], 0.) * lenght; + EmitVertex(); + + EndPrimitive(); +} + diff --git a/shaders/vertex_normals.glsl b/shaders/vertex_normals.glsl new file mode 100644 index 00000000..dc959929 --- /dev/null +++ b/shaders/vertex_normals.glsl @@ -0,0 +1,16 @@ +#version 460 core + +layout (location=0) in vec3 in_position; +layout (location=1) in vec3 in_normal; + +out vec3 normal; + +uniform mat4 model_view_mat; +uniform mat4 proj_mat; + +void main() +{ + normal = normalize(vec3(proj_mat * model_view_mat * vec4(in_normal, 0.))); + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); +} + diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 6380c9be..1315ad53 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -182,29 +182,6 @@ void che_viewer::draw() glBindVertexArray(0); } -void che_viewer::draw_normal_field() -{ - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glDisable(GL_LIGHTING); - glColor3f(.8, .8, 1.0); - glLineWidth(2.0); - - glBegin(GL_LINES); - for(index_t v = 0; v < _n_vertices; v++) - { - vertex n = factor * normals[v]; - vertex a = mesh->get_vertex(v); - vertex b = a + n; - - glVertex3v(&a[0]); - glVertex3v(&b[0]); - } - glEnd(); - - glPopAttrib(); -} - void che_viewer::draw_gradient_field() { glPushAttrib(GL_ALL_ATTRIB_BITS); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 22f94c9c..8ddd6442 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -253,6 +253,10 @@ void viewer::init_glsl() { shader_program.load_vertex("../shaders/vertex.glsl"); shader_program.load_fragment("../shaders/fragment.glsl"); + + shader_normals.load_vertex("../shaders/vertex_normals.glsl"); + shader_normals.load_geometry("../shaders/geometry_normals.glsl"); + shader_normals.load_fragment("../shaders/fragment_normals.glsl"); } void viewer::update_vbo() @@ -521,6 +525,8 @@ void viewer::raycasting(viewer * view) void viewer::render_gl() { + glClearDepth(1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader_program.enable(); GLint uniformEye = glGetUniformLocation(shader_program, "eye"); @@ -541,10 +547,21 @@ void viewer::render_gl() GLint uniform_proj_mat = glGetUniformLocation(shader_program, "proj_mat"); glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); + + + if(render_normal_field) + { + shader_normals.enable(); - shader_program.disable(); + uniform_model_view_mat = glGetUniformLocation(shader_normals, "model_view_mat"); + glUniformMatrix4fv(uniform_model_view_mat, 1, 0, &view_mat[0][0]); + + uniform_proj_mat = glGetUniformLocation(shader_normals, "proj_mat"); + glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); + + draw_scene(); + } } void viewer::render_embree() @@ -584,7 +601,6 @@ void viewer::draw_scene() draw_polygons(); if(render_gradient_field) draw_gradient_field(); - if(render_normal_field) draw_normal_field(); if(render_border) draw_border(); draw_isolated_vertices(); @@ -712,17 +728,6 @@ void viewer::draw_selected_vertices() glPopAttrib(); } -void viewer::draw_normal_field() -{ - shader_program.disable(); - - for(index_t i = 0; i < n_meshes; i++) - { - glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - meshes[i].draw_normal_field(); - } -} - void viewer::draw_gradient_field() { shader_program.disable(); From bda0ef459b2a2dd84acbde3fb646321005161fc3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 3 Feb 2020 15:56:06 +0100 Subject: [PATCH 0135/1018] rt: adding more than one light source --- include/raytracing/raytracing.h | 4 +++- src/raytracing/raytracing.cpp | 29 +++++++++++++++++------------ src/viewer/viewer.cpp | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index c225e197..7d274753 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -3,6 +3,8 @@ #include "che.h" +#include + #include @@ -29,7 +31,7 @@ class raytracing void pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const glm::vec3 & light, + const std::vector & light, const bool & restart = false ); diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index f7cce6d1..62bf5efc 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -48,7 +48,8 @@ bool raytracing::rt_restart(const size_t & w, const size_t & h) void raytracing::pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, - const glm::vec3 & light, const bool & restart ) + const std::vector & light, + const bool & restart ) { if(rt_restart(windows_size.x, windows_size.y) || restart) n_samples = 0; @@ -58,25 +59,29 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - - #pragma omp parallel for + + glm::vec4 li; + + #pragma omp parallel for private(li) for(index_t i = 0; i < width; i++) for(index_t j = 0; j < height; j++) { //row major glm::vec4 & color = img[j * windows_size.x + i]; - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, - (float(j) + randf(gen)) / height - ); + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, + (float(j) + randf(gen)) / height + ); - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + li = glm::vec4(0.f); + for(auto & l: light) + li += intersect_li(cam_pos, glm::normalize(p - cam_pos), l); - color *= float(n_samples); - color += intersect_li(cam_pos, glm::normalize(p - cam_pos), light); - color /= float(n_samples + 1); + color = (color * float(n_samples) + li / float(light.size())) / float(n_samples + 1); } n_samples++; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8ddd6442..9f13aee9 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -582,7 +582,7 @@ void viewer::render_embree() } rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, glm::vec3(light[1], light[2], light[3]), action); + view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3]), glm::vec3(0.f, 0.f, 0.f)}, action); action = false; From 0c2c1b95e1356614eff85c257198ca9e97e0266d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 3 Feb 2020 22:44:14 +0100 Subject: [PATCH 0136/1018] gradient field and shader color --- include/che_fill_hole.h | 10 +++++----- shaders/fragment_normals.glsl | 2 +- shaders/geometry_normals.glsl | 17 ++++++++++++----- src/che_fill_hole.cpp | 32 ++++++++++++++++---------------- src/raytracing/embree.cpp | 2 +- src/viewer/viewer.cpp | 14 +++++++++----- 6 files changed, 44 insertions(+), 33 deletions(-) diff --git a/include/che_fill_hole.h b/include/che_fill_hole.h index 1bebbca2..f45af443 100644 --- a/include/che_fill_hole.h +++ b/include/che_fill_hole.h @@ -36,7 +36,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & lenght, const std::array & neighbors, const bool & o) + a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & length, const std::array & neighbors, const bool & o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -48,7 +48,7 @@ struct border_t a_vec r = div * a + (1 - div) * b; - r = lenght * normalise(r) + V[v]; + r = length * normalise(r) + V[v]; r(2) = 0; return r; @@ -79,7 +79,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & lenght, const std::array & neighbors, const bool & o, const a_vec & normal) + a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & length, const std::array & neighbors, const bool & o, const a_vec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -103,7 +103,7 @@ struct border_t r[1] = sin(theta * div); r[2] = 0; - r = lenght * normalise(r); + r = length * normalise(r); r = E * r; r += V[v]; @@ -123,7 +123,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti che * fill_hole_front_angles_without_projection(che * mesh, std::vector & front_vertices); -che * fill_hole_front_angles(std::vector & vertices, const real_t & lenght, const vertex & normal, const size_t & max_iter, bool is_grow = false); +che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow = false); che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index); diff --git a/shaders/fragment_normals.glsl b/shaders/fragment_normals.glsl index 85ec7c42..2323517d 100644 --- a/shaders/fragment_normals.glsl +++ b/shaders/fragment_normals.glsl @@ -4,6 +4,6 @@ layout(location = 0) out vec4 FragColor; void main() { - FragColor = vec4(.8, .8, 1., 1.); + FragColor = vec4(0.6, 1.0, 0.2, 1.); } diff --git a/shaders/geometry_normals.glsl b/shaders/geometry_normals.glsl index 866ba46a..bee4df8b 100644 --- a/shaders/geometry_normals.glsl +++ b/shaders/geometry_normals.glsl @@ -1,20 +1,27 @@ #version 460 core layout (triangles) in; -layout (line_strip, max_vertices = 2) out; +layout (line_strip, max_vertices = 6) out; in vec3 normal[]; -const float lenght = 0.02; +uniform float length; -void main() +void line_normal(int i) { - gl_Position = gl_in[0].gl_Position; + gl_Position = gl_in[i].gl_Position; EmitVertex(); - gl_Position = gl_in[0].gl_Position + vec4(normal[0], 0.) * lenght; + gl_Position = gl_in[i].gl_Position + vec4(normal[i], 0.) * length; EmitVertex(); EndPrimitive(); } +void main() +{ + line_normal(0); + line_normal(1); + line_normal(2); +} + diff --git a/src/che_fill_hole.cpp b/src/che_fill_hole.cpp index 663e24b7..3d00bdc2 100644 --- a/src/che_fill_hole.cpp +++ b/src/che_fill_hole.cpp @@ -320,7 +320,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, gproshan_debug(filling holes); distance_t perimeter = 0.0, init_perimeter = 0.0; - real_t lenght = mesh->mean_edge(); + real_t length = mesh->mean_edge(); priority_queue front; vector vertices; @@ -355,7 +355,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, init_perimeter += *(vertices.back() - vertices.front()); perimeter = init_perimeter; -// lenght = perimeter / vertices.size(); +// length = perimeter / vertices.size(); bool o = is_grow; @@ -407,9 +407,9 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, border_t b_n(tmp_vertices, n_v, neighbors[n_v], o, tmp_normals[n_v]); bool close_vertex = false; - if(b_p.theta < M_PI && norm(tmp_vertices[n_v] - tmp_vertices[neighbors[p_v][!o]]) < 1.5 * lenght) + if(b_p.theta < M_PI && norm(tmp_vertices[n_v] - tmp_vertices[neighbors[p_v][!o]]) < 1.5 * length) close_vertex = true; - if(b_n.theta < M_PI && norm(tmp_vertices[p_v] - tmp_vertices[neighbors[n_v][o]]) < 1.5 * lenght) + if(b_n.theta < M_PI && norm(tmp_vertices[p_v] - tmp_vertices[neighbors[n_v][o]]) < 1.5 * length) close_vertex = true; if( top.theta <= M_PI ) @@ -419,7 +419,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, perimeter -= norm(tmp_vertices[n_v] - tmp_vertices[v]); } - lenght = ( norm(tmp_vertices[v] - tmp_vertices[p_v]) + norm(tmp_vertices[n_v] - tmp_vertices[v]) ) / 2; + length = ( norm(tmp_vertices[v] - tmp_vertices[p_v]) + norm(tmp_vertices[n_v] - tmp_vertices[v]) ) / 2; m_normal = ( tmp_normals[v] + tmp_normals[p_v] + tmp_normals[n_v] ) / 3; //m_normal = normalise(m_normal); @@ -446,7 +446,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, { index_t m_v = tmp_vertices.size(); - m_vec = top.new_vertex(tmp_vertices, 0.5, lenght, neighbors[v], o, tmp_normals[v]); + m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); faces.push_back(m_v); @@ -484,9 +484,9 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, { index_t m_v = tmp_vertices.size(); - m_vec = top.new_vertex(tmp_vertices, 1./3, lenght, neighbors[v], o, tmp_normals[v]); + m_vec = top.new_vertex(tmp_vertices, 1./3, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); - m_vec = top.new_vertex(tmp_vertices, 2./3, lenght, neighbors[v], o, tmp_normals[v]); + m_vec = top.new_vertex(tmp_vertices, 2./3, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); faces.push_back(m_v); @@ -546,7 +546,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vertices.push_back(vertex(r[0], r[1], r[2])); for(index_t v = 0; false && v < tmp_vertices.size(); v++) - a_vec normal = tmp_vertices[v] + lenght * 3 * normalise(tmp_normals[v]); + a_vec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); gproshan_debug_var(perimeter); // gproshan_debug(filling holes); @@ -555,7 +555,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, return faces.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); } -che * fill_hole_front_angles(vector & vertices, const real_t & lenght, const vertex & normal, const size_t & max_iter, bool is_grow) +che * fill_hole_front_angles(vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) { size_t p_iter = max_iter; distance_t perimeter = 0.0; @@ -672,9 +672,9 @@ che * fill_hole_front_angles(vector & vertices, const real_t & lenght, c border_t b_n(tmp_vertices, n_v, neighbors[n_v], o); bool close_vertex = false; - if(b_p.theta <= M_PI && norm(tmp_vertices[n_v] - tmp_vertices[neighbors[p_v][!o]]) < 1.5 * lenght) + if(b_p.theta <= M_PI && norm(tmp_vertices[n_v] - tmp_vertices[neighbors[p_v][!o]]) < 1.5 * length) close_vertex = true; - if(b_n.theta <= M_PI && norm(tmp_vertices[p_v] - tmp_vertices[neighbors[n_v][o]]) < 1.5 * lenght) + if(b_n.theta <= M_PI && norm(tmp_vertices[p_v] - tmp_vertices[neighbors[n_v][o]]) < 1.5 * length) close_vertex = true; if( top.theta <= M_PI ) @@ -714,7 +714,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & lenght, c { index_t m_v = tmp_vertices.size(); - m_vec = top.new_vertex(tmp_vertices, 0.5, lenght, neighbors[v], o); + m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o); tmp_vertices.push_back(m_vec); faces.push_back(m_v); @@ -750,9 +750,9 @@ che * fill_hole_front_angles(vector & vertices, const real_t & lenght, c { index_t m_v = tmp_vertices.size(); - m_vec = top.new_vertex(tmp_vertices, 1./3, lenght, neighbors[v], o); + m_vec = top.new_vertex(tmp_vertices, 1./3, length, neighbors[v], o); tmp_vertices.push_back(m_vec); - m_vec = top.new_vertex(tmp_vertices, 2./3, lenght, neighbors[v], o); + m_vec = top.new_vertex(tmp_vertices, 2./3, length, neighbors[v], o); tmp_vertices.push_back(m_vec); faces.push_back(m_v); @@ -799,7 +799,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & lenght, c if(init_perimeter < perimeter) { if(!is_grow) - return fill_hole_front_angles(vertices, lenght, -normal, max_iter, !is_grow); + return fill_hole_front_angles(vertices, length, -normal, max_iter, !is_grow); else return nullptr; } diff --git a/src/raytracing/embree.cpp b/src/raytracing/embree.cpp index 9591045f..528ca175 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/embree.cpp @@ -127,7 +127,7 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) glm::vec3 color(.6f, .8f, 1.f); float dist_light = glm::length(light - r.position()); - float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff + float falloff = 0.5f / (dist_light * dist_light); // intensity multiplier / falloff glm::vec3 wi = normalize(light - r.position()); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 9f13aee9..ab6a2535 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -525,15 +525,16 @@ void viewer::raycasting(viewer * view) void viewer::render_gl() { - glClearDepth(1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + shader_program.enable(); - GLint uniformEye = glGetUniformLocation(shader_program, "eye"); - glUniform3f(uniformEye, eye[1], eye[2], eye[3]); + GLint uniform_eye = glGetUniformLocation(shader_program, "eye"); + glUniform3f(uniform_eye, eye[1], eye[2], eye[3]); - GLint uniformLight = glGetUniformLocation(shader_program, "light"); - glUniform3f(uniformLight, light[1], light[2], light[3]); + GLint uniform_light = glGetUniformLocation(shader_program, "light"); + glUniform3f(uniform_light, light[1], light[2], light[3]); GLint uniform_render_flat = glGetUniformLocation(shader_program, "render_flat"); glUniform1i(uniform_render_flat, render_flat); @@ -553,6 +554,9 @@ void viewer::render_gl() if(render_normal_field) { shader_normals.enable(); + + GLfloat uniform_length = glGetUniformLocation(shader_normals, "length"); + glUniform1f(uniform_length, mesh()->mean_edge()); uniform_model_view_mat = glGetUniformLocation(shader_normals, "model_view_mat"); glUniformMatrix4fv(uniform_model_view_mat, 1, 0, &view_mat[0][0]); From 8e3c4739070443e0c9b302f814bd7a4b8701a78a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 4 Feb 2020 12:21:41 +0100 Subject: [PATCH 0137/1018] display gradiend field shader --- include/viewer/che_viewer.h | 4 +- include/viewer/viewer.h | 2 +- shaders/fragment_gradient.glsl | 9 ++++ shaders/geometry_gradient.glsl | 35 ++++++++++++++ shaders/vertex_gradient.glsl | 16 +++++++ src/raytracing/embree.cpp | 2 +- src/viewer/che_viewer.cpp | 83 ---------------------------------- src/viewer/viewer.cpp | 62 ++++++++++--------------- 8 files changed, 88 insertions(+), 125 deletions(-) create mode 100644 shaders/fragment_gradient.glsl create mode 100644 shaders/geometry_gradient.glsl create mode 100644 shaders/vertex_gradient.glsl diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index cb6e64d7..fcceb620 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -28,7 +28,6 @@ class che_viewer che * mesh; size_t _n_vertices; // current number of vertices bool _invert_orientation; - real_t factor; vertex v_translate; vertex * normals; @@ -39,6 +38,7 @@ class che_viewer public: int vx, vy; ///< viewport positions. + real_t factor; std::vector selected; public: @@ -53,8 +53,6 @@ class che_viewer void update_normals(); void update_colors(const color_t *const c = nullptr); void draw(); - void draw_gradient_field(); - void draw_mesh_info(); const size_t & n_vertices() const; color_t & color(const index_t & v); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 283b6ae0..a8b718d1 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -84,6 +84,7 @@ class viewer shader shader_program; shader shader_normals; + shader shader_gradient; shader shader_edges; camera cam; @@ -193,7 +194,6 @@ class viewer // draw routines void draw_scene(); void draw_polygons(); - void draw_gradient_field(); void draw_border(); void draw_selected_vertices(); void draw_vectors(); diff --git a/shaders/fragment_gradient.glsl b/shaders/fragment_gradient.glsl new file mode 100644 index 00000000..2323517d --- /dev/null +++ b/shaders/fragment_gradient.glsl @@ -0,0 +1,9 @@ +#version 460 core + +layout(location = 0) out vec4 FragColor; + +void main() +{ + FragColor = vec4(0.6, 1.0, 0.2, 1.); +} + diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl new file mode 100644 index 00000000..8539f849 --- /dev/null +++ b/shaders/geometry_gradient.glsl @@ -0,0 +1,35 @@ +#version 460 core + +layout (triangles) in; +layout (line_strip, max_vertices = 2) out; + +in float color[]; + +uniform float length; + +void main() +{ + vec3 xi = vec3(gl_in[0].gl_Position); + vec3 xj = vec3(gl_in[1].gl_Position); + vec3 xk = vec3(gl_in[2].gl_Position); + + vec3 n = normalize(cross(xj - xi, xk - xi)); + + vec3 pij = cross(n, xj - xi); + vec3 pjk = cross(n, xk - xj); + vec3 pki = cross(n, xi - xk); + + vec3 g = normalize(color[0] * pjk + color[1] * pki + color[2] * pij); + + vec3 a = (xi + xj + xk) / 3; + vec3 b = a + g * length; + + gl_Position = vec4(a, 0.); + EmitVertex(); + + gl_Position = vec4(b, 0.); + EmitVertex(); + + EndPrimitive(); +} + diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl new file mode 100644 index 00000000..01ae4f1f --- /dev/null +++ b/shaders/vertex_gradient.glsl @@ -0,0 +1,16 @@ +#version 460 core + +layout (location=0) in vec3 in_position; +layout (location=2) in float in_color; + +out float color; + +uniform mat4 model_view_mat; +uniform mat4 proj_mat; + +void main() +{ + color = in_color; + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); +} + diff --git a/src/raytracing/embree.cpp b/src/raytracing/embree.cpp index 528ca175..9591045f 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/embree.cpp @@ -127,7 +127,7 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) glm::vec3 color(.6f, .8f, 1.f); float dist_light = glm::length(light - r.position()); - float falloff = 0.5f / (dist_light * dist_light); // intensity multiplier / falloff + float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff glm::vec3 wi = normalize(light - r.position()); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 1315ad53..53d2672d 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -182,89 +182,6 @@ void che_viewer::draw() glBindVertexArray(0); } -void che_viewer::draw_gradient_field() -{ - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glDisable(GL_LIGHTING); - glColor3f(.8, 1.0, .8); - glLineWidth(1.2); - - double h = 0.3 * factor; - - for(index_t f = 0; f < mesh->n_faces(); f++) - { - vertex g = h * mesh->gradient_he(f * che::P, colors); - vertex a = mesh->barycenter(f); - vertex b = a + g; - vertex n = mesh->normal_he(f * che::P); - - vertex v = b - a; - vertex v90 = n * v; - vertex p0 = b; - vertex p1 = p0 - 0.25 * v - 0.15 * v90; - vertex p2 = p0 - 0.25 * v + 0.15 * v90; - - glBegin(GL_LINES); - glVertex3v(&a[0]); - glVertex3v(&b[0]); - glEnd(); - - glBegin(GL_TRIANGLES); - glVertex3v(&p0[0]); - glVertex3v(&p1[0]); - glVertex3v(&p2[0]); - glEnd(); - } - - glPopAttrib(); -} - -void che_viewer::draw_mesh_info() -{ -/* - if(!mesh) return; - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - gluOrtho2D(0, viewer::window_width(), 0, viewer::window_height()); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - - char str[256]; - float color[4] = {1, .75, .25, 1}; - int h = 16; // dh = 16; - - sprintf(str, "%s", mesh->name().c_str()); - draw_str(str, 32, 32, color, GLUT_BITMAP_HELVETICA_18); - - sprintf(str, "%9lu n_vertices", mesh->n_vertices()); - draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_9_BY_15); - - sprintf(str, "%9lu n_faces", mesh->n_faces()); - draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_9_BY_15); - - sprintf(str, "%9lu n_edges", mesh->n_edges()); - draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_9_BY_15); - - sprintf(str, "%9lu n_half_edges", mesh->n_half_edges()); - draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_9_BY_15); - - sprintf(str, "%9lu n_borders", mesh->n_borders()); - draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_9_BY_15); - - //sprintf(str, "%9.3lf quality", mesh->quality()); - //draw_str(str, 10, viewer::window_height() - (h += 18), color, GLUT_BITMAP_8_BY_13); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); -*/ -} - const size_t & che_viewer::n_vertices() const { return _n_vertices; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ab6a2535..88b50aff 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -257,6 +257,10 @@ void viewer::init_glsl() shader_normals.load_vertex("../shaders/vertex_normals.glsl"); shader_normals.load_geometry("../shaders/geometry_normals.glsl"); shader_normals.load_fragment("../shaders/fragment_normals.glsl"); + + shader_gradient.load_vertex("../shaders/vertex_gradient.glsl"); + shader_gradient.load_geometry("../shaders/geometry_gradient.glsl"); + shader_gradient.load_fragment("../shaders/fragment_gradient.glsl"); } void viewer::update_vbo() @@ -530,23 +534,12 @@ void viewer::render_gl() shader_program.enable(); - GLint uniform_eye = glGetUniformLocation(shader_program, "eye"); - glUniform3f(uniform_eye, eye[1], eye[2], eye[3]); - - GLint uniform_light = glGetUniformLocation(shader_program, "light"); - glUniform3f(uniform_light, light[1], light[2], light[3]); - - GLint uniform_render_flat = glGetUniformLocation(shader_program, "render_flat"); - glUniform1i(uniform_render_flat, render_flat); - - GLint uniform_render_lines = glGetUniformLocation(shader_program, "render_lines"); - glUniform1i(uniform_render_lines, render_lines); - - GLint uniform_model_view_mat = glGetUniformLocation(shader_program, "model_view_mat"); - glUniformMatrix4fv(uniform_model_view_mat, 1, 0, &view_mat[0][0]); - - GLint uniform_proj_mat = glGetUniformLocation(shader_program, "proj_mat"); - glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); + glUniform3f(glGetUniformLocation(shader_program, "eye"), eye[1], eye[2], eye[3]); + glUniform3f(glGetUniformLocation(shader_program, "light"), light[1], light[2], light[3]); + glUniform1i(glGetUniformLocation(shader_program, "render_flat"), render_flat); + glUniform1i(glGetUniformLocation(shader_program, "render_lines"), render_lines); + glUniformMatrix4fv(glGetUniformLocation(shader_program, "model_view_mat"), 1, 0, &view_mat[0][0]); + glUniformMatrix4fv(glGetUniformLocation(shader_program, "proj_mat"), 1, 0, &proj_mat[0][0]); draw_scene(); @@ -555,15 +548,22 @@ void viewer::render_gl() { shader_normals.enable(); - GLfloat uniform_length = glGetUniformLocation(shader_normals, "length"); - glUniform1f(uniform_length, mesh()->mean_edge()); + glUniform1f(glGetUniformLocation(shader_normals, "length"), mesh().factor); + glUniformMatrix4fv(glGetUniformLocation(shader_normals, "model_view_mat"), 1, 0, &view_mat[0][0]); + glUniformMatrix4fv(glGetUniformLocation(shader_normals, "proj_mat"), 1, 0, &proj_mat[0][0]); + + draw_scene(); + } - uniform_model_view_mat = glGetUniformLocation(shader_normals, "model_view_mat"); - glUniformMatrix4fv(uniform_model_view_mat, 1, 0, &view_mat[0][0]); - uniform_proj_mat = glGetUniformLocation(shader_normals, "proj_mat"); - glUniformMatrix4fv(uniform_proj_mat, 1, 0, &proj_mat[0][0]); - + if(render_gradient_field) + { + shader_gradient.enable(); + + glUniform1f(glGetUniformLocation(shader_gradient, "length"), mesh().factor); + glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "model_view_mat"), 1, 0, &view_mat[0][0]); + glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "proj_mat"), 1, 0, &proj_mat[0][0]); + draw_scene(); } } @@ -586,7 +586,7 @@ void viewer::render_embree() } rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3]), glm::vec3(0.f, 0.f, 0.f)}, action); + view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); action = false; @@ -604,7 +604,6 @@ void viewer::draw_scene() { draw_polygons(); - if(render_gradient_field) draw_gradient_field(); if(render_border) draw_border(); draw_isolated_vertices(); @@ -732,17 +731,6 @@ void viewer::draw_selected_vertices() glPopAttrib(); } -void viewer::draw_gradient_field() -{ - shader_program.disable(); - - for(index_t i = 0; i < n_meshes; i++) - { - glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - meshes[i].draw_gradient_field(); - } -} - void viewer::pick_vertex(int x, int y) { gproshan_log(VIEWER); From d3bc07b90f1179e3bbf7aee1d346a91c65acf613 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 4 Feb 2020 12:55:52 +0100 Subject: [PATCH 0138/1018] compute gradient field shader --- shaders/geometry_gradient.glsl | 17 ++++++++++------- shaders/vertex_gradient.glsl | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index 8539f849..11f3348f 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -4,14 +4,17 @@ layout (triangles) in; layout (line_strip, max_vertices = 2) out; in float color[]; +in vec3 position[]; uniform float length; +uniform mat4 model_view_mat; +uniform mat4 proj_mat; void main() { - vec3 xi = vec3(gl_in[0].gl_Position); - vec3 xj = vec3(gl_in[1].gl_Position); - vec3 xk = vec3(gl_in[2].gl_Position); + vec3 xi = position[0]; + vec3 xj = position[1]; + vec3 xk = position[2]; vec3 n = normalize(cross(xj - xi, xk - xi)); @@ -21,13 +24,13 @@ void main() vec3 g = normalize(color[0] * pjk + color[1] * pki + color[2] * pij); - vec3 a = (xi + xj + xk) / 3; - vec3 b = a + g * length; + vec3 a = (xi + xj + xk) / 3.0; + vec3 b = a + g * 0.3 * length; - gl_Position = vec4(a, 0.); + gl_Position = proj_mat * model_view_mat * vec4(a, 1.); EmitVertex(); - gl_Position = vec4(b, 0.); + gl_Position = proj_mat * model_view_mat * vec4(b, 1.); EmitVertex(); EndPrimitive(); diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl index 01ae4f1f..798c9218 100644 --- a/shaders/vertex_gradient.glsl +++ b/shaders/vertex_gradient.glsl @@ -4,6 +4,7 @@ layout (location=0) in vec3 in_position; layout (location=2) in float in_color; out float color; +out vec3 position; uniform mat4 model_view_mat; uniform mat4 proj_mat; @@ -11,6 +12,7 @@ uniform mat4 proj_mat; void main() { color = in_color; + position = in_position; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); } From 90387a04868d17ce716c6eae7559ac40073cfb9a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 4 Feb 2020 17:52:45 +0100 Subject: [PATCH 0139/1018] render wireframe --- include/viewer/viewer.h | 4 +- shaders/fragment.glsl | 92 ++++++++++++++++++++--------------------- shaders/geometry.glsl | 53 ++++++++++++++++++++++++ shaders/vertex.glsl | 15 +++---- src/viewer/viewer.cpp | 16 +++++-- 5 files changed, 121 insertions(+), 59 deletions(-) create mode 100644 shaders/geometry.glsl diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index a8b718d1..ba902cde 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -114,6 +114,7 @@ class viewer bool action; bool render_wireframe; + bool render_wireframe_fill; bool render_gradient_field; bool render_normal_field; bool render_border; @@ -183,11 +184,12 @@ class viewer static void set_render_embree(viewer * view); static void set_render_optix(viewer * view); static void set_render_wireframe(viewer * view); + static void set_render_wireframe_fill(viewer * view); static void set_render_gradient_field(viewer * view); static void set_render_normal_field(viewer * view); static void set_render_border(viewer * view); static void set_render_lines(viewer * view); - static void set_is_flat(viewer * view); + static void set_render_flat(viewer * view); static void raycasting(viewer * view); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 6ab0db52..0209b555 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,33 +1,38 @@ #version 460 core +in vec3 gs_position; +in vec3 gs_normal; +in float gs_color; + +noperspective in vec3 edge_dist; + +layout(location = 0) out vec4 FragColor; + uniform vec3 eye; uniform vec3 light; uniform bool render_flat; uniform bool render_lines; +uniform bool render_wireframe; -in vec3 position; -in vec3 normal; -in float color; - -layout(location = 0) out vec4 FragColor; - -float diffuse( vec3 N, vec3 L ) +float diffuse(vec3 N, vec3 L) { - return max( 0., dot( N, L )); + return max(0, dot(N, L)); } -float specular( vec3 N, vec3 L, vec3 E ) +float specular(vec3 N, vec3 L, vec3 E) { - const float shininess = 4.; - vec3 R = 2.*dot(L,N)*N - L; - return pow( max( 0., dot( R, E )), shininess ); + const float shininess = 4; + vec3 R = 2 * dot(L, N) * N - L; + + return pow(max(0, dot(R, E)), shininess); } -float fresnel( vec3 N, vec3 E ) +float fresnel(vec3 N, vec3 E) { - const float sharpness = 10.; - float NE = max( 0., dot( N, E )); - return pow( sqrt( 1. - NE*NE ), sharpness ); + const float sharpness = 10; + float NE = max(0, dot(N, E)); + + return pow(sqrt( 1. - NE * NE ), sharpness); } //https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag @@ -65,48 +70,41 @@ vec3 colormap(float x) void main() { - // color - /* - float d = 1. - color; - float r = (1. - d*d) * .8; - float g = (1. - (2. * (d - .5)) * (2. * (d - .5))) * .7; - float b = (1. - (1. - d) * (1. - d)); - vec3 vcolor = vec3(r, g, b); - */ - vec3 vcolor = colormap(color); - + vec3 color = colormap(gs_color); // lines if(render_lines) { - float h = color; - h = h * 40.; - h = h - floor( h ); - h = (1. / (1. + exp(-100.*(h - .55)))) + (1. / (1. + exp(-100.*(-h + .45)))); - h = 1. - h; - vcolor.xyz = vec3(0, 0, 0) + (1. - h) * vcolor.xyz; + float h = gs_color; + h = h * 40; + h = h - floor(h); + h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); + h = 1 - h; + color = vec3(0) + (1. - h) * color; } vec3 N; if(render_flat) - { - vec3 X = dFdx(position); - vec3 Y = dFdy(position); - vec3 normal_flat = cross(X,Y); - N = normalize( normal_flat ); - } + N = normalize(cross(dFdx(gs_position), dFdy(gs_position))); else - { - N = normalize( normal ); - } + N = normalize(gs_normal); - vec3 L = normalize( light - position ); - vec3 E = normalize( eye - position ); - vec3 R = 2.*dot(L,N)*N - L; - vec3 one = vec3( 1., 1., 1. ); + vec3 L = normalize(light - gs_position); + vec3 E = normalize(eye - gs_position); + vec3 R = 2 * dot(L, N) * N - L; + vec3 one = vec3(1); + + FragColor = vec4(diffuse(N, L) * color + .1 * specular(N, L, E) * one + .5 * fresnel(N,E) * one, 1); - FragColor.rgb = diffuse(N,L) * vcolor + .1 * specular(N,L,E) * one + .5 * fresnel(N,E) * one; - FragColor.a = 1.; + if(render_wireframe) + { + vec3 delta = fwidth(edge_dist); + vec3 tmp = smoothstep(vec3(0), delta, edge_dist); + + float d = min(min(tmp.x, tmp.y), tmp.z); + + FragColor = mix(vec4(.1, .1, .1, 1), FragColor, d); + } } diff --git a/shaders/geometry.glsl b/shaders/geometry.glsl new file mode 100644 index 00000000..ea6ce905 --- /dev/null +++ b/shaders/geometry.glsl @@ -0,0 +1,53 @@ +#version 460 core + +layout(triangles) in; +layout(triangle_strip, max_vertices = 3) out; + +in vec3 vs_position[]; +in vec3 vs_normal[]; +in float vs_color[]; + +out vec3 gs_position; +out vec3 gs_normal; +out float gs_color; + +noperspective out vec3 edge_dist; + +void main() +{ + float a = length(vs_position[1] - vs_position[2]); + float b = length(vs_position[2] - vs_position[0]); + float c = length(vs_position[1] - vs_position[0]); + + float alpha = acos((b * b + c * c - a * a) / (2 * b * c)); + float beta = acos((a * a + c * c - b * b) / (2 * a * c)); + + float ha = abs(c * sin(beta)); + float hb = abs(c * sin(alpha)); + float hc = abs(b * sin(alpha)); + + + gs_position = vs_position[0]; + gs_normal = vs_normal[0]; + gs_color = vs_color[0]; + edge_dist = vec3(ha, 0, 0); + gl_Position = gl_in[0].gl_Position; + EmitVertex(); + + gs_position = vs_position[1]; + gs_normal = vs_normal[1]; + gs_color = vs_color[1]; + edge_dist = vec3(0, hb, 0); + gl_Position = gl_in[1].gl_Position; + EmitVertex(); + + gs_position = vs_position[2]; + gs_normal = vs_normal[2]; + gs_color = vs_color[2]; + edge_dist = vec3(0, 0, hc); + gl_Position = gl_in[2].gl_Position; + EmitVertex(); + + EndPrimitive(); +} + diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index a6c328d3..226f7546 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -4,18 +4,19 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; layout (location=2) in float in_color; -out vec3 position; -out vec3 normal; -out float color; +out vec3 vs_position; +out vec3 vs_normal; +out float vs_color; uniform mat4 model_view_mat; uniform mat4 proj_mat; void main() { - position = in_position; - normal = in_normal; - color = in_color; - gl_Position = proj_mat * model_view_mat * vec4(position, 1.); + vs_position = in_position; + vs_normal = in_normal; + vs_color = in_color; + + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 88b50aff..a6f4b586 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -46,6 +46,7 @@ viewer::viewer() #endif // GPROSHAN_EMBREE render_wireframe = false; + render_wireframe_fill = false; render_gradient_field = false; render_normal_field = false; render_border = false; @@ -232,7 +233,8 @@ void viewer::init_menus() add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); sub_menus.push_back("Render"); - add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); + add_process(GLFW_KEY_F5, {"F5", "Render Wireframe", set_render_wireframe}); + add_process(GLFW_KEY_F6, {"F6", "Render Wireframe Fill", set_render_wireframe_fill}); add_process(GLFW_KEY_F7, {"F7", "Render GL", set_render_gl}); add_process(GLFW_KEY_F8, {"F8", "Render Embree", set_render_embree}); add_process(GLFW_KEY_F9, {"F9", "Render OptiX", set_render_optix}); @@ -241,17 +243,17 @@ void viewer::init_menus() sub_menus.push_back("Mesh"); add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); - add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_is_flat}); + add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_render_flat}); add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradient_field}); add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normal_field}); - add_process(GLFW_KEY_F5, {"F5", "Show Borders", set_render_border}); add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); } void viewer::init_glsl() { shader_program.load_vertex("../shaders/vertex.glsl"); + shader_program.load_geometry("../shaders/geometry.glsl"); shader_program.load_fragment("../shaders/fragment.glsl"); shader_normals.load_vertex("../shaders/vertex_normals.glsl"); @@ -475,6 +477,11 @@ void viewer::set_render_wireframe(viewer * view) view->render_wireframe = !view->render_wireframe; } +void viewer::set_render_wireframe_fill(viewer * view) +{ + view->render_wireframe_fill = !view->render_wireframe_fill; +} + void viewer::set_render_gradient_field(viewer * view) { view->render_gradient_field = !view->render_gradient_field; @@ -496,7 +503,7 @@ void viewer::set_render_lines(viewer * view) view->render_lines = !view->render_lines; } -void viewer::set_is_flat(viewer * view) +void viewer::set_render_flat(viewer * view) { view->render_flat = !view->render_flat; } @@ -538,6 +545,7 @@ void viewer::render_gl() glUniform3f(glGetUniformLocation(shader_program, "light"), light[1], light[2], light[3]); glUniform1i(glGetUniformLocation(shader_program, "render_flat"), render_flat); glUniform1i(glGetUniformLocation(shader_program, "render_lines"), render_lines); + glUniform1i(glGetUniformLocation(shader_program, "render_wireframe"), render_wireframe_fill); glUniformMatrix4fv(glGetUniformLocation(shader_program, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_program, "proj_mat"), 1, 0, &proj_mat[0][0]); From 47158d4a2b6fea4813e1f5ac89f4d5e193d4cf95 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 4 Feb 2020 18:02:15 +0100 Subject: [PATCH 0140/1018] deleting old functions --- include/viewer/viewer.h | 2 -- src/viewer/viewer.cpp | 73 ----------------------------------------- 2 files changed, 75 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index ba902cde..ffec1c84 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -198,8 +198,6 @@ class viewer void draw_polygons(); void draw_border(); void draw_selected_vertices(); - void draw_vectors(); - void draw_isolated_vertices(); void pick_vertex(int x, int y); }; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index a6f4b586..8966307c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -614,13 +614,7 @@ void viewer::draw_scene() if(render_border) draw_border(); - draw_isolated_vertices(); - draw_vectors(); draw_selected_vertices(); - -// shader_program.disable(); -// mesh().draw_mesh_info(); -// shader_program.enable(); } void viewer::draw_polygons() @@ -638,73 +632,6 @@ void viewer::draw_polygons() } } -void viewer::draw_vectors() -{ - shader_program.disable(); - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glDisable(GL_LIGHTING); - glColor4f(1., 0., 0., 1); - glLineWidth(3.0); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - glBegin(GL_LINES); - - index_t i = 0; - for(vertex & v: vectors) - { - if(i % 8 == 0) glColor4f(1., 0., 0., 1); - if(i % 8 == 2) glColor4f(0., 1., 0., 1); - if(i % 8 == 4) glColor4f(0., 0., 1., 1); - if(i % 8 == 6) glColor4f(1., 1., 0., 1); - glVertex3v(&v.x); - i++; - } - - glEnd(); - glPopAttrib(); -} - -void viewer::draw_isolated_vertices() -{ - shader_program.disable(); - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glEnable(GL_COLOR_MATERIAL); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glColor3f(0.5, 0., 0.5); - - double h = 0.01 * cam.zoom; - for(const vertex & v: other_vertices) - { - glPushMatrix(); - glTranslated(v.x, v.y, v.z); - ////glutSolidSphere(h, 10, 10); - glPopMatrix(); - } - - glEnd(); - - glPopAttrib(); - /*shader_program.disable(); - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glPointSize(5); - glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); - glEnable(GL_POINT_SMOOTH); - glColor3f(1., 0., 0.); - - glBegin(GL_POINTS); - - for(const vertex & v: other_vertices) - glVertex3v(&v.x); - - glEnd(); - - glPopAttrib();*/ -} - void viewer::draw_border() { select_vertices.clear(); From cc073164de4fe9545ea2fdd6f7925bdf1e0b1051 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 5 Feb 2020 11:49:31 +0100 Subject: [PATCH 0141/1018] rt embree: non oriented surfaces --- src/raytracing/embree.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/raytracing/embree.cpp b/src/raytracing/embree.cpp index 9591045f..0420e503 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/embree.cpp @@ -131,11 +131,13 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) glm::vec3 wi = normalize(light - r.position()); + float dot_wi_normal = glm::dot(wi, r.normal()); + if(dot_wi_normal < 0) + dot_wi_normal = glm::dot(wi, -r.normal()); + ray_hit ro(r.position() + 1e-5f * wi, wi); - if(occluded(ro)) - return glm::vec4(.2f * color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); - return glm::vec4(color * falloff * std::max(0.f, glm::dot(wi, r.normal())), 1.f); + return (occluded(ro) ? .3f : 1.f) * glm::vec4(color * falloff * dot_wi_normal, 1.f); } bool embree::intersect(ray_hit & r) From 97ba10280e6a605ab7bdd644c227cdb18f50cfed Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 5 Feb 2020 13:03:10 +0100 Subject: [PATCH 0142/1018] read obj file: faces with negative indexes --- src/che_obj.cpp | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/che_obj.cpp b/src/che_obj.cpp index f92462ae..c2839601 100644 --- a/src/che_obj.cpp +++ b/src/che_obj.cpp @@ -28,7 +28,7 @@ void che_obj::read_file(const string & file) assert(is.good()); real_t x, y, z; - index_t face[8], i; + int face[8], i; vector vertices; vector faces; @@ -56,19 +56,41 @@ void che_obj::read_file(const string & file) if(i == che::P) // che::P = 3, triangular mesh { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[2] - 1); + if(face[0] < 0) + { + faces.push_back(vertices.size() + face[0]); + faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[2]); + } + else + { + faces.push_back(face[0] - 1); + faces.push_back(face[1] - 1); + faces.push_back(face[2] - 1); + } } else if(i == 4) // quadrangular mesh, split two triangles { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[3] - 1); - - faces.push_back(face[1] - 1); - faces.push_back(face[2] - 1); - faces.push_back(face[3] - 1); + if(face[0] < 0) + { + faces.push_back(vertices.size() + face[0]); + faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[3]); + + faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[2]); + faces.push_back(vertices.size() + face[3]); + } + else + { + faces.push_back(face[0] - 1); + faces.push_back(face[1] - 1); + faces.push_back(face[3] - 1); + + faces.push_back(face[1] - 1); + faces.push_back(face[2] - 1); + faces.push_back(face[3] - 1); + } } } } From 17d79dbfd1cd347dc5c54b154b0eb6dd95d8d9ca Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 5 Feb 2020 14:36:05 +0100 Subject: [PATCH 0143/1018] fix bug read obj and ply --- src/che_obj.cpp | 6 +++--- src/che_ply.cpp | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/che_obj.cpp b/src/che_obj.cpp index c2839601..8d4a8fbd 100644 --- a/src/che_obj.cpp +++ b/src/che_obj.cpp @@ -33,15 +33,15 @@ void che_obj::read_file(const string & file) vector vertices; vector faces; - char line[128]; + char line[256]; string key; while(is.getline(line, sizeof(line))) { stringstream ss(line); + key = ""; ss >> key; - if(key == "") continue; if(key == "v") { @@ -92,7 +92,7 @@ void che_obj::read_file(const string & file) faces.push_back(face[3] - 1); } } - } + } } is.close(); diff --git a/src/che_ply.cpp b/src/che_ply.cpp index 897bc6bb..2f916f31 100644 --- a/src/che_ply.cpp +++ b/src/che_ply.cpp @@ -50,8 +50,10 @@ void che_ply::read_file(const string & file) while(getline(is, str) && str != "end_header") { stringstream ss(str); - + + str = ""; ss >> str; + if(str == "element") { ss >> element; @@ -135,7 +137,6 @@ void che_ply::read_file(const string & file) index_t p, he = 0; while(n_f--) { - fn = 0; is.read(vbuffer, fn); if(fn == 1) p = *((char *) vbuffer); From 1f5333280b17cab7aa236cab6b78c1031bc6013a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 6 Feb 2020 12:14:15 +0100 Subject: [PATCH 0144/1018] inpaiting drawing patches --- src/mdict/inpainting.cpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 0518696a..e5f2c800 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -221,18 +221,10 @@ void inpainting::init_radial_patches() { covered[i] = 1; } - s++; - gproshan_debug(seed); - gproshan_debug_var(sample(it)); - /*for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) - { - gproshan_debug_var(i); - } - - }*/ + //gproshan_debug_var(patches[s].vertices.size()); + //gproshan_debug_var(sample(it)); + s++; } it++; } @@ -255,7 +247,7 @@ void inpainting::init_radial_patches() outlv.save(f_points); - //assert(outliers.size()>0); + assert(outliers.size()>0); M = s; // updating number of vertices gproshan_debug_var(M); @@ -591,10 +583,10 @@ distance_t inpainting::execute() bool *pmask; - draw_patches(1); - draw_patches(3); - draw_patches(4); -// draw_patches(50); + draw_patches(10); + draw_patches(200); + draw_patches(120); + draw_patches(50); //draw_patches(400); //draw_patches(500); //draw_patches(56); From 0fbe6da284e4546d23e490cb1bd9f818703b5d42 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 6 Feb 2020 12:38:04 +0100 Subject: [PATCH 0145/1018] rt: add optix dependence --- CMakeLists.txt | 7 ++++ cmake/FindOptiX.cmake | 78 +++++++++++++++++++++++++++++++++++++++ src/raytracing/embree.cpp | 7 +++- 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 cmake/FindOptiX.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a9592e2f..7d1b89a3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,13 @@ if(EMBREE_LIBRARY) endif(EMBREE_LIBRARY) +find_package(OptiX 7.0) +if(OptiX_INCLUDE) + add_definitions(-DGPROSHAN_OPTIX) + include_directories(${OptiX_INCLUDE}) +endif(OptiX_INCLUDE) + + find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) diff --git a/cmake/FindOptiX.cmake b/cmake/FindOptiX.cmake new file mode 100644 index 00000000..eec01ecf --- /dev/null +++ b/cmake/FindOptiX.cmake @@ -0,0 +1,78 @@ +# +# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of NVIDIA CORPORATION nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +# Locate the OptiX distribution. Search relative to the SDK first, then look in the system. + +# Our initial guess will be within the SDK. +#set(OptiX_INSTALL_DIR "${CMAKE_SOURCE_DIR}/../" CACHE PATH "Path to OptiX installed location.") + +# The distribution contains only 64 bit libraries. Error when we have been mis-configured. +if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) + if(WIN32) + message(SEND_ERROR "Make sure when selecting the generator, you select one with Win64 or x64.") + endif() + message(FATAL_ERROR "OptiX only supports builds configured for 64 bits.") +endif() + +# search path based on the bit-ness of the build. (i.e. 64: bin64, lib64; 32: +# bin, lib). Note that on Mac, the OptiX library is a universal binary, so we +# only need to look in lib and not lib64 for 64 bit builds. +if(NOT APPLE) + set(bit_dest "64") +else() + set(bit_dest "") +endif() + +# Include +find_path(OptiX_INCLUDE + NAMES optix.h + PATHS "${OptiX_INSTALL_DIR}/include" + NO_DEFAULT_PATH + ) +find_path(OptiX_INCLUDE + NAMES optix.h + ) + +# Check to make sure we found what we were looking for +function(OptiX_report_error error_message required component ) + if(DEFINED OptiX_FIND_REQUIRED_${component} AND NOT OptiX_FIND_REQUIRED_${component}) + set(required FALSE) + endif() + if(OptiX_FIND_REQUIRED AND required) + message(FATAL_ERROR "${error_message} Please locate before proceeding.") + else() + if(NOT OptiX_FIND_QUIETLY) + message(STATUS "${error_message}") + endif(NOT OptiX_FIND_QUIETLY) + endif() +endfunction() + +if(NOT OptiX_INCLUDE) + OptiX_report_error("OptiX headers (optix.h and friends) not found." TRUE headers ) +endif() + diff --git a/src/raytracing/embree.cpp b/src/raytracing/embree.cpp index 0420e503..88b77120 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/embree.cpp @@ -133,11 +133,14 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) float dot_wi_normal = glm::dot(wi, r.normal()); if(dot_wi_normal < 0) - dot_wi_normal = glm::dot(wi, -r.normal()); + dot_wi_normal = -dot_wi_normal; ray_hit ro(r.position() + 1e-5f * wi, wi); - return (occluded(ro) ? .3f : 1.f) * glm::vec4(color * falloff * dot_wi_normal, 1.f); + if(occluded(ro)) + return .5f * glm::vec4(color * falloff * dot_wi_normal, 1.f); + + return glm::vec4(color * falloff * dot_wi_normal, 1.f); } bool embree::intersect(ray_hit & r) From 7c2a5e45766acdc4edf8ef2e44c350d1878f3803 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 6 Feb 2020 17:38:24 +0100 Subject: [PATCH 0146/1018] radial patches updated --- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 17 +++++++---------- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 14 +++++++------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index e95d5e22..b72523f7 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -534,7 +534,7 @@ void viewer_process_mask() gproshan_input(avg_p percentage f ); //cin >> avg_p >> percentage >> f; - cin>> percentage; + cin>> avg_p >> percentage; basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e5f2c800..4542a4d5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -140,7 +140,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t std::default_random_engine generator; std::uniform_int_distribution distribution(0,n_vertices-1); - int k = 0; + size_t k = 0; size_t rn=0; @@ -224,6 +224,7 @@ void inpainting::init_radial_patches() //gproshan_debug_var(patches[s].vertices.size()); //gproshan_debug_var(sample(it)); + //gproshan_debug_var(it); s++; } it++; @@ -247,7 +248,7 @@ void inpainting::init_radial_patches() outlv.save(f_points); - assert(outliers.size()>0); + assert(outliers.size()==0); M = s; // updating number of vertices gproshan_debug_var(M); @@ -288,25 +289,22 @@ void inpainting::init_radial_patches() gproshan_debug_var(patch_max_size); #endif } - + gproshan_debug(resettt); bool * pmask = mask; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); gproshan_debug(passed); - #pragma omp parallel for + //#pragma omp parallel for for(index_t s = 0; s < M; s++) { patch & p = patches[s]; - p.transform(); p.scale_xyz(phi_basis->get_radio()); p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - - } gproshan_log(radial patches are ready); @@ -337,7 +335,6 @@ void inpainting::init_radial_curvature_patches() } bool covered[mesh->n_vertices()]; - index_t * toplevel = new index_t[mesh->n_vertices()]; #pragma omp for for(index_t i = 0; i < mesh->n_vertices(); i++) { @@ -346,7 +343,6 @@ void inpainting::init_radial_curvature_patches() patches_map.resize(mesh->n_vertices()); - index_t ns; size_t count = 0; while(!Q.empty()) // curvatures from higher to lower { @@ -430,7 +426,7 @@ void inpainting::init_radial_curvature_patches() gproshan_debug(passed); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < M-1; s++) { patch & p = patches[s]; @@ -439,6 +435,7 @@ void inpainting::init_radial_curvature_patches() p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); + gproshan_debug_var(s); } gproshan_log(radial patches are ready); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 86797991..8517f34d 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0 * p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 4a24543c..2de541d2 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -57,8 +57,8 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { - //radio = radio_; - radio = -INFINITY; + radio = radio_; + //radio = -INFINITY; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); @@ -80,12 +80,12 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si p = p - c ; p = p - ((p,n)*n); - + if(*p <= radio ) //if(*p <= radio && ( n, mesh->normal(indexes[i]) ) >= 0) - if( *p <= 2*radio && acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) + //if( *p <= 2*radio && acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) { - if(*p > radio) - radio = *p; + /*if(*p > radio) + radio = *p;*/ _vertices.push_back(indexes[i]); } else @@ -243,7 +243,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Fri, 7 Feb 2020 17:56:42 +0100 Subject: [PATCH 0147/1018] creating new normal --- include/mdict/patch.h | 3 +++ src/mdict/inpainting.cpp | 51 +++++----------------------------------- src/mdict/patch.cpp | 40 ++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 3e62a3e9..efbab528 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -114,6 +114,9 @@ class patch void jet_fit_directions(che * mesh, const index_t & v ); + void normal_fit_directions(che * mesh, + const index_t & v + ); real_t get_min_z(); real_t get_max_z(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 4542a4d5..9a3255ca 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -60,46 +60,6 @@ void inpainting::load_mask() } } - /* -///////////////////////// - V.zeros(mesh->n_vertices()); - size_t * percentages_size = new size_t[M]; - - int k; - size_t rn; - // create initial desired percentage sizes - #pragma omp for - for(index_t s = 0; s < M; s++) - { - percentages_size[s] = patches[s].vertices.size() - ceil(patches[s].vertices.size() * (percent/ 100.0)) ; - //Generate random mask according to a percentage of patches capacity - std::default_random_engine generator; - std::uniform_int_distribution distribution(0, patches[s].vertices.size()); - //if some patch has been already masked then it already counts - for(auto i: patches[s].vertices) - if(mask[i] && percentages_size[s]) - { - percentages_size[s]--; - } - - - k = 0; - // gproshan_debug_var(percentages_size[s]); - // gproshan_debug_var(patches[s].vertices.size()); - while(k < percentages_size[s] ) - { - //gproshan_debug_var(percentages_size[s]); - - rn = distribution(generator); - mask[patches[s].vertices[rn]] = 1; - V(patches[s].vertices[rn]) = 1; - k++; - - } - } - /* for(index_t s = 0; s < n_vertices; s++) - gproshan_debug_var(mask[s]); -*/ V.save(f_mask); } @@ -206,6 +166,7 @@ void inpainting::init_radial_patches() s = 0; size_t it = 0; index_t * toplevel = new index_t[mesh->n_vertices()]; + while(it < M) { @@ -213,9 +174,9 @@ void inpainting::init_radial_patches() // Check the points are inside and add them // while( ) // mask at the end - if(!covered[sample(it)]) + // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(s), dictionary::T, vertices[s], toplevel); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(it), dictionary::T, vertices[it], toplevel); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -248,7 +209,7 @@ void inpainting::init_radial_patches() outlv.save(f_points); - assert(outliers.size()==0); + //assert(outliers.size()==0); M = s; // updating number of vertices gproshan_debug_var(M); @@ -581,9 +542,9 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); - draw_patches(200); +/* draw_patches(200); draw_patches(120); - draw_patches(50); + draw_patches(50);*/ //draw_patches(400); //draw_patches(500); //draw_patches(56); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2de541d2..ac9d6e78 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -64,7 +64,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/20); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -421,6 +421,44 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) } +void patch::normal_fit_directions(che * mesh, const index_t & v) +{ + x.set_size(3); + x(0) = mesh->gt(v).x; + x(1) = mesh->gt(v).y; + x(2) = mesh->gt(v).z; + + + vertex nz = mesh->normal(v); + vertex nx = mesh->gt_vt_next_evt(v); +// GT[VT[next(EVT[v]]] + vertex c = mesh->get_vertex(v); + vertex ny; + nx = nx - c ; + nx = nx - ((nx,nz)*nz); + + ny = (nz * nx); + nx.unit(); + ny.unit(); + nz.unit(); + + + T.set_size(3, 3); + T(0, 0) = nx[0]; + T(1, 0) = nx[1]; + T(2, 0) = nx[2]; + T(0, 1) = ny[0]; + T(1, 1) = ny[1]; + T(2, 1) = ny[2]; + T(0, 2) = nz[0]; + T(1, 2) = nz[1]; + T(2, 2) = nz[2]; + +// T = normalise(T); + +} + + real_t patch::get_min_z() { return xyz.row(2).min(); From 5e8972782d2a4884f769cd0d18ed3ceed031c577 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 8 Feb 2020 18:23:37 +0100 Subject: [PATCH 0148/1018] renaming src files --- include/geodesics_ptp.h | 2 +- include/raytracing/embree.h | 97 ---------- include/raytracing/rt_embree.h | 97 ++++++++++ include/raytracing/rt_optix.h | 97 ++++++++++ include/viewer/viewer.h | 10 +- shaders/fragment.glsl | 6 +- src/raytracing/{embree.cpp => rt_embree.cpp} | 2 +- src/raytracing/rt_optix.cpp | 175 +++++++++++++++++++ src/viewer/viewer.cpp | 24 +++ 9 files changed, 407 insertions(+), 103 deletions(-) delete mode 100644 include/raytracing/embree.h create mode 100644 include/raytracing/rt_embree.h create mode 100644 include/raytracing/rt_optix.h rename src/raytracing/{embree.cpp => rt_embree.cpp} (99%) create mode 100644 src/raytracing/rt_optix.cpp diff --git a/include/geodesics_ptp.h b/include/geodesics_ptp.h index 16c19c31..092729b3 100644 --- a/include/geodesics_ptp.h +++ b/include/geodesics_ptp.h @@ -8,7 +8,7 @@ #include "che.h" -#define PTP_TOL 1e-3 +#define PTP_TOL 1e-4 // geometry processing and shape analysis framework diff --git a/include/raytracing/embree.h b/include/raytracing/embree.h deleted file mode 100644 index 91146df1..00000000 --- a/include/raytracing/embree.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifdef GPROSHAN_EMBREE - -#ifndef EMBREE_H -#define EMBREE_H - -#include "che.h" -#include "raytracing.h" - -#include -#include - - -// geometry processing and shape analysis framework -// raytracing approach -namespace gproshan::rt { - - -struct ray_hit: public RTCRayHit -{ - ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), - const glm::vec3 & direction = glm::vec3(0.0f), - float near = 0.0f, - float far = FLT_MAX) - { - ray.org_x = origin.x; - ray.org_y = origin.y; - ray.org_z = origin.z; - ray.tnear = near; - - ray.dir_x = direction.x; - ray.dir_y = direction.y; - ray.dir_z = direction.z; - ray.time = 0.0f; - - ray.tfar = far; - ray.mask = 0; - ray.flags = 0; - - //hit.primID = RTC_INVALID_GEOMETRY_ID; - hit.geomID = RTC_INVALID_GEOMETRY_ID; - hit.instID[0] = RTC_INVALID_GEOMETRY_ID; - } - - const glm::vec3 org() const - { - return {ray.org_x, ray.org_y, ray.org_z}; - } - - const glm::vec3 dir() const - { - return {ray.dir_x, ray.dir_y, ray.dir_z}; - } - - const glm::vec3 normal() const - { - return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - } - - const glm::vec3 position() const - { - return org() + ray.tfar * dir(); - } -}; - - -class embree : public raytracing -{ - RTCDevice device; - RTCScene scene; - RTCIntersectContext intersect_context; - - public: - embree(); - ~embree(); - - void build_bvh(); - index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - - glm::vec4 li(const ray_hit & r, const glm::vec3 & light); - - private: - bool intersect(ray_hit & r); - bool occluded(ray_hit & r); - - const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); - const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); -}; - - -} // namespace gproshan - -#endif // EMBREE_H - -#endif // GPROSHAN_EMBREE - diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h new file mode 100644 index 00000000..4cb40e9e --- /dev/null +++ b/include/raytracing/rt_embree.h @@ -0,0 +1,97 @@ +#ifdef GPROSHAN_EMBREE + +#ifndef RT_EMBREE_H +#define RT_EMBREE_H + +#include "che.h" +#include "raytracing.h" + +#include +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +class embree : public raytracing +{ + struct ray_hit: public RTCRayHit + { + ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), + const glm::vec3 & direction = glm::vec3(0.0f), + float near = 0.0f, + float far = FLT_MAX) + { + ray.org_x = origin.x; + ray.org_y = origin.y; + ray.org_z = origin.z; + ray.tnear = near; + + ray.dir_x = direction.x; + ray.dir_y = direction.y; + ray.dir_z = direction.z; + ray.time = 0.0f; + + ray.tfar = far; + ray.mask = 0; + ray.flags = 0; + + //hit.primID = RTC_INVALID_GEOMETRY_ID; + hit.geomID = RTC_INVALID_GEOMETRY_ID; + hit.instID[0] = RTC_INVALID_GEOMETRY_ID; + } + + const glm::vec3 org() const + { + return {ray.org_x, ray.org_y, ray.org_z}; + } + + const glm::vec3 dir() const + { + return {ray.dir_x, ray.dir_y, ray.dir_z}; + } + + const glm::vec3 normal() const + { + return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); + } + + const glm::vec3 position() const + { + return org() + ray.tfar * dir(); + } + }; + + + RTCDevice device; + RTCScene scene; + RTCIntersectContext intersect_context; + + public: + embree(); + ~embree(); + + void build_bvh(); + index_t add_sphere(const glm::vec4 & xyzr); + index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + + glm::vec4 li(const ray_hit & r, const glm::vec3 & light); + + private: + bool intersect(ray_hit & r); + bool occluded(ray_hit & r); + + const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); +}; + + +} // namespace gproshan + +#endif // RT_EMBREE_H + +#endif // GPROSHAN_EMBREE + diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h new file mode 100644 index 00000000..01b6b4c8 --- /dev/null +++ b/include/raytracing/rt_optix.h @@ -0,0 +1,97 @@ +#ifdef GPROSHAN_OPTIX + +#ifndef RT_OPTIX_H +#define RT_OPTIX_H + +#include "che.h" +#include "raytracing.h" + +#include +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +class optix : public raytracing +{ + struct ray_hit: public RTCRayHit + { + ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), + const glm::vec3 & direction = glm::vec3(0.0f), + float near = 0.0f, + float far = FLT_MAX) + { + ray.org_x = origin.x; + ray.org_y = origin.y; + ray.org_z = origin.z; + ray.tnear = near; + + ray.dir_x = direction.x; + ray.dir_y = direction.y; + ray.dir_z = direction.z; + ray.time = 0.0f; + + ray.tfar = far; + ray.mask = 0; + ray.flags = 0; + + //hit.primID = RTC_INVALID_GEOMETRY_ID; + hit.geomID = RTC_INVALID_GEOMETRY_ID; + hit.instID[0] = RTC_INVALID_GEOMETRY_ID; + } + + const glm::vec3 org() const + { + return {ray.org_x, ray.org_y, ray.org_z}; + } + + const glm::vec3 dir() const + { + return {ray.dir_x, ray.dir_y, ray.dir_z}; + } + + const glm::vec3 normal() const + { + return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); + } + + const glm::vec3 position() const + { + return org() + ray.tfar * dir(); + } + }; + + + RTCDevice device; + RTCScene scene; + RTCIntersectContext intersect_context; + + public: + optix(); + ~optix(); + + void build_bvh(); + index_t add_sphere(const glm::vec4 & xyzr); + index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + + glm::vec4 li(const ray_hit & r, const glm::vec3 & light); + + private: + bool intersect(ray_hit & r); + bool occluded(ray_hit & r); + + const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); +}; + + +} // namespace gproshan + +#endif // RT_OPTIX_H + +#endif // GPROSHAN_OPTIX + diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index ffec1c84..021c5a27 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -17,9 +17,13 @@ #include "imgui_impl_opengl3.h" #ifdef GPROSHAN_EMBREE - #include "embree.h" + #include "rt_embree.h" #endif // GPROSHAN_EMBREE +#ifdef GPROSHAN_OPTIX + #include "rt_optix.h" +#endif // GPROSHAN_OPTIX + #define N_MESHES 12 @@ -110,6 +114,10 @@ class viewer #ifdef GPROSHAN_EMBREE rt::embree * rt_embree; #endif // GPROSHAN_EMBREE + + #ifdef GPROSHAN_OPTIX + rt::optix * rt_optix; + #endif // GPROSHAN_OPTIX bool action; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 0209b555..d4f76296 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -95,8 +95,6 @@ void main() vec3 R = 2 * dot(L, N) * N - L; vec3 one = vec3(1); - FragColor = vec4(diffuse(N, L) * color + .1 * specular(N, L, E) * one + .5 * fresnel(N,E) * one, 1); - if(render_wireframe) { vec3 delta = fwidth(edge_dist); @@ -104,7 +102,9 @@ void main() float d = min(min(tmp.x, tmp.y), tmp.z); - FragColor = mix(vec4(.1, .1, .1, 1), FragColor, d); + color = mix(vec3(.2), color, d); } + + FragColor = vec4(diffuse(N, L) * color + .1 * specular(N, L, E) * one + .5 * fresnel(N,E) * one, 1); } diff --git a/src/raytracing/embree.cpp b/src/raytracing/rt_embree.cpp similarity index 99% rename from src/raytracing/embree.cpp rename to src/raytracing/rt_embree.cpp index 88b77120..76dfe9ee 100644 --- a/src/raytracing/embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -1,4 +1,4 @@ -#include "embree.h" +#include "rt_embree.h" #ifdef GPROSHAN_EMBREE diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp new file mode 100644 index 00000000..b85961bc --- /dev/null +++ b/src/raytracing/rt_optix.cpp @@ -0,0 +1,175 @@ +#include "rt_optix.h" + +#ifdef GPROSHAN_OPTIX + +#include +#include +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +void optix_error(void * ptr, RTCError error, const char * str) +{ + fprintf(stderr, "OPTIX ERROR: %s\n", str); +} + +optix::optix() +{ + device = rtcNewDevice(NULL); + rtcSetDeviceErrorFunction(device, optix_error, NULL); + + scene = rtcNewScene(device); + + rtcInitIntersectContext(&intersect_context); +} + +optix::~optix() +{ + rtcReleaseScene(scene); + rtcReleaseDevice(device); +} + +void optix::build_bvh() +{ + rtcCommitScene(scene); +} + +index_t optix::add_sphere(const glm::vec4 & xyzr) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); + + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer(geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); + *pxyzr = xyzr; + + rtcCommitGeometry(geom); + + index_t geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + +index_t optix::add_mesh(const che * mesh, const glm::mat4 & model_matrix) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); + + glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer(geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT3, 3 * sizeof(float), + mesh->n_vertices() + ); + + index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_INDEX, 0, + RTC_FORMAT_UINT3, 3 * sizeof(index_t), + mesh->n_faces() + ); + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + glm::vec4 v(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 1.f); + vertices[i] = glm::vec3(model_matrix * v); + } + + memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); + + rtcCommitGeometry(geom); + + index_t geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + +index_t optix::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, + 4 * sizeof(float), + mesh->n_vertices() + ); + + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_NORMAL, 0, + RTC_FORMAT_FLOAT3, + 3 * sizeof(float), + mesh->n_vertices() + ); + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001f); + + vertex n = mesh->normal(i); + normal[i] = glm::vec3(n.x, n.y, n.z); + } + + rtcCommitGeometry(geom); + + index_t geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + return geom_id; +} + +glm::vec4 optix::li(const ray_hit & r, const glm::vec3 & light) +{ + glm::vec3 color(.6f, .8f, 1.f); + + float dist_light = glm::length(light - r.position()); + float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff + + glm::vec3 wi = normalize(light - r.position()); + + float dot_wi_normal = glm::dot(wi, r.normal()); + if(dot_wi_normal < 0) + dot_wi_normal = -dot_wi_normal; + + ray_hit ro(r.position() + 1e-5f * wi, wi); + + if(occluded(ro)) + return .5f * glm::vec4(color * falloff * dot_wi_normal, 1.f); + + return glm::vec4(color * falloff * dot_wi_normal, 1.f); +} + +bool optix::intersect(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + +bool optix::occluded(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + + +const glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) +{ + ray_hit r(org, dir); + return intersect(r) ? li(r, light) : glm::vec4(0.f); +} + +const float optix::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) +{ + ray_hit r(org, dir); + return intersect(r) ? r.ray.tfar : 0.f; +} + + +} // namespace gproshan + +#endif // GPROSHAN_OPTIX + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8966307c..8a4a3f51 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -606,6 +606,30 @@ void viewer::render_embree() void viewer::render_optix() { +#ifdef GPROSHAN_OPTIX + + if(rt_optix == nullptr) + { + double time_build_embree; + TIC(time_build_embree); + + rt_optix = new rt::optix; + rt_optix->add_mesh(mesh()); + rt_optix->build_bvh(); + + TOC(time_build_embree); + gproshan_log_var(time_build_embree); + } + + rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), + view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); + + action = false; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_optix->img); + +#endif // GPROSHAN_OPTIX } void viewer::draw_scene() From a9585acbada4a2710308e6c0c890db37eec54573 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 11 Feb 2020 12:11:14 +0100 Subject: [PATCH 0149/1018] fixed normal orientation --- src/mdict/inpainting.cpp | 3 ++- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 9a3255ca..3988a8e0 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -542,9 +542,10 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); + draw_patches(50); /* draw_patches(200); draw_patches(120); - draw_patches(50);*/ + */ //draw_patches(400); //draw_patches(500); //draw_patches(56); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 8517f34d..86797991 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0 * p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index ac9d6e78..fedb6ad9 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -62,7 +62,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; gather_vertices(mesh, v, n_toplevels, toplevel); - jet_fit_directions(mesh, v); + normal_fit_directions(mesh, v); geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/20); index_t * indexes = new index_t[geo.n_sorted_index()]; @@ -454,7 +454,7 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) T(1, 2) = nz[1]; T(2, 2) = nz[2]; -// T = normalise(T); + T = normalise(T); } From 1629ce0a4835efe58fff21035007d7054d71f818 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 11 Feb 2020 12:19:20 +0100 Subject: [PATCH 0150/1018] rt: init optix --- CMakeLists.txt | 7 +- include/raytracing/rt_optix.h | 61 ++-------------- shaders/fragment.glsl | 4 +- src/che.cpp | 1 - src/raytracing/rt_optix.cpp | 131 ++-------------------------------- src/viewer/che_viewer.cpp | 1 - src/viewer/viewer.cpp | 4 ++ 7 files changed, 22 insertions(+), 187 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d1b89a3..6c217e2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,14 +20,12 @@ endif(CUDA_FOUND) find_package(Embree 3.7) if(EMBREE_LIBRARY) add_definitions(-DGPROSHAN_EMBREE) - include_directories(${EMBREE_INCLUDE_DIRS}) endif(EMBREE_LIBRARY) find_package(OptiX 7.0) if(OptiX_INCLUDE) add_definitions(-DGPROSHAN_OPTIX) - include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) @@ -61,6 +59,8 @@ include_directories(${AMADILLO_INCLUDE_DIR}) include_directories(${EIGEN3_INCLUDE_DIR}) include_directories(${SuiteSparse_INCLUDE_DIRS}) include_directories(${CGAL_INCLUDE_DIRS}) +include_directories(${EMBREE_INCLUDE_DIRS}) +include_directories(${OptiX_INCLUDE}) include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/include/viewer) @@ -81,9 +81,10 @@ target_link_libraries(gproshan_cpp GLEW::GLEW) target_link_libraries(gproshan_cpp glfw) target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan_cpp CGAL::CGAL) target_link_libraries(gproshan_cpp ${EMBREE_LIBRARY}) +target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) if(CUDA_FOUND) diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 01b6b4c8..12fd8bb7 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -6,7 +6,10 @@ #include "che.h" #include "raytracing.h" -#include +#include +#include +#include + #include @@ -17,58 +20,6 @@ namespace gproshan::rt { class optix : public raytracing { - struct ray_hit: public RTCRayHit - { - ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), - const glm::vec3 & direction = glm::vec3(0.0f), - float near = 0.0f, - float far = FLT_MAX) - { - ray.org_x = origin.x; - ray.org_y = origin.y; - ray.org_z = origin.z; - ray.tnear = near; - - ray.dir_x = direction.x; - ray.dir_y = direction.y; - ray.dir_z = direction.z; - ray.time = 0.0f; - - ray.tfar = far; - ray.mask = 0; - ray.flags = 0; - - //hit.primID = RTC_INVALID_GEOMETRY_ID; - hit.geomID = RTC_INVALID_GEOMETRY_ID; - hit.instID[0] = RTC_INVALID_GEOMETRY_ID; - } - - const glm::vec3 org() const - { - return {ray.org_x, ray.org_y, ray.org_z}; - } - - const glm::vec3 dir() const - { - return {ray.dir_x, ray.dir_y, ray.dir_z}; - } - - const glm::vec3 normal() const - { - return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - } - - const glm::vec3 position() const - { - return org() + ray.tfar * dir(); - } - }; - - - RTCDevice device; - RTCScene scene; - RTCIntersectContext intersect_context; - public: optix(); ~optix(); @@ -78,11 +29,7 @@ class optix : public raytracing index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - glm::vec4 li(const ray_hit & r, const glm::vec3 & light); - private: - bool intersect(ray_hit & r); - bool occluded(ray_hit & r); const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index d4f76296..5fcc9ddc 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -95,6 +95,7 @@ void main() vec3 R = 2 * dot(L, N) * N - L; vec3 one = vec3(1); + if(render_wireframe) { vec3 delta = fwidth(edge_dist); @@ -105,6 +106,7 @@ void main() color = mix(vec3(.2), color, d); } - FragColor = vec4(diffuse(N, L) * color + .1 * specular(N, L, E) * one + .5 * fresnel(N,E) * one, 1); + color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + FragColor = vec4(color, 1); } diff --git a/src/che.cpp b/src/che.cpp index 84707698..be6f0f91 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -1,7 +1,6 @@ #include "che.h" #include "include_arma.h" -#include "viewer/viewer.h" #include #include diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index b85961bc..771a0e26 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -6,166 +6,49 @@ #include #include +#include // geometry processing and shape analysis framework // raytracing approach namespace gproshan::rt { -void optix_error(void * ptr, RTCError error, const char * str) -{ - fprintf(stderr, "OPTIX ERROR: %s\n", str); -} - optix::optix() { - device = rtcNewDevice(NULL); - rtcSetDeviceErrorFunction(device, optix_error, NULL); - - scene = rtcNewScene(device); - - rtcInitIntersectContext(&intersect_context); + optixInit(); } optix::~optix() { - rtcReleaseScene(scene); - rtcReleaseDevice(device); } void optix::build_bvh() { - rtcCommitScene(scene); } index_t optix::add_sphere(const glm::vec4 & xyzr) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); - - glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer(geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); - *pxyzr = xyzr; - - rtcCommitGeometry(geom); - - index_t geom_id = rtcAttachGeometry(scene, geom); - rtcReleaseGeometry(geom); - - return geom_id; + return 0; } index_t optix::add_mesh(const che * mesh, const glm::mat4 & model_matrix) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); - - glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer(geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices() - ); - - index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_INDEX, 0, - RTC_FORMAT_UINT3, 3 * sizeof(index_t), - mesh->n_faces() - ); - - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - glm::vec4 v(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 1.f); - vertices[i] = glm::vec3(model_matrix * v); - } - - memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); - - rtcCommitGeometry(geom); - - index_t geom_id = rtcAttachGeometry(scene, geom); - rtcReleaseGeometry(geom); - - return geom_id; + return 0; } index_t optix::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); - - glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, - 4 * sizeof(float), - mesh->n_vertices() - ); - - glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_NORMAL, 0, - RTC_FORMAT_FLOAT3, - 3 * sizeof(float), - mesh->n_vertices() - ); - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001f); - - vertex n = mesh->normal(i); - normal[i] = glm::vec3(n.x, n.y, n.z); - } - - rtcCommitGeometry(geom); - - index_t geom_id = rtcAttachGeometry(scene, geom); - rtcReleaseGeometry(geom); - - return geom_id; -} - -glm::vec4 optix::li(const ray_hit & r, const glm::vec3 & light) -{ - glm::vec3 color(.6f, .8f, 1.f); - - float dist_light = glm::length(light - r.position()); - float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff - - glm::vec3 wi = normalize(light - r.position()); - - float dot_wi_normal = glm::dot(wi, r.normal()); - if(dot_wi_normal < 0) - dot_wi_normal = -dot_wi_normal; - - ray_hit ro(r.position() + 1e-5f * wi, wi); - - if(occluded(ro)) - return .5f * glm::vec4(color * falloff * dot_wi_normal, 1.f); - - return glm::vec4(color * falloff * dot_wi_normal, 1.f); + return 0; } -bool optix::intersect(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - -bool optix::occluded(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - - const glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) { - ray_hit r(org, dir); - return intersect(r) ? li(r, light) : glm::vec4(0.f); + return glm::vec4(0.f); } const float optix::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) { - ray_hit r(org, dir); - return intersect(r) ? r.ray.tfar : 0.f; + return 0; } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 53d2672d..60293c2e 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -1,5 +1,4 @@ #include "che_viewer.h" -#include "viewer.h" #include #include diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8a4a3f51..b8bc4784 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -45,6 +45,10 @@ viewer::viewer() rt_embree = nullptr; #endif // GPROSHAN_EMBREE + #ifdef GPROSHAN_OPTIX + rt_optix = nullptr; + #endif // GPROSHAN_OPTIX + render_wireframe = false; render_wireframe_fill = false; render_gradient_field = false; From a1198d19869b8ac795e12b5e773fdd984d0c2cd4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 11 Feb 2020 15:54:59 +0100 Subject: [PATCH 0151/1018] rt: embree constructor commit scene --- include/raytracing/raytracing.h | 14 +++++++------- include/raytracing/rt_embree.h | 16 ++++++++-------- src/raytracing/rt_embree.cpp | 7 ++++++- src/viewer/viewer.cpp | 11 +++-------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index 7d274753..addd3209 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -27,19 +27,19 @@ class raytracing raytracing(); virtual ~raytracing(); - bool rt_restart(const size_t & w, const size_t & h); - void pathtracing( const glm::uvec2 & windows_size, + virtual bool rt_restart(const size_t & w, const size_t & h); + virtual void pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, const bool & restart = false ); - float * raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const index_t & samples = 4 - ); + virtual float * raycaster( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const index_t & samples = 4 + ); protected: diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 4cb40e9e..5a19948f 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -70,15 +70,8 @@ class embree : public raytracing RTCIntersectContext intersect_context; public: - embree(); + embree(const std::vector & meshes); ~embree(); - - void build_bvh(); - index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - - glm::vec4 li(const ray_hit & r, const glm::vec3 & light); private: bool intersect(ray_hit & r); @@ -86,6 +79,13 @@ class embree : public raytracing const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + + void build_bvh(); + index_t add_sphere(const glm::vec4 & xyzr); + index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + + glm::vec4 li(const ray_hit & r, const glm::vec3 & light); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 76dfe9ee..72bb7f52 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -17,7 +17,7 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -embree::embree() +embree::embree(const std::vector & meshes) { device = rtcNewDevice(NULL); rtcSetDeviceErrorFunction(device, embree_error, NULL); @@ -25,6 +25,11 @@ embree::embree() scene = rtcNewScene(device); rtcInitIntersectContext(&intersect_context); + + for(auto & m: meshes) + add_mesh(m); + + build_bvh(); } embree::~embree() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index b8bc4784..639df6a7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -518,10 +518,7 @@ void viewer::raycasting(viewer * view) gproshan_log(VIEWER); - rt::embree rc; - - rc.add_mesh(view->mesh()); - rc.build_bvh(); + rt::embree rc({view->mesh()}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), view->view_mat, view->proj_mat @@ -589,9 +586,7 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - rt_embree = new rt::embree; - rt_embree->add_mesh(mesh()); - rt_embree->build_bvh(); + rt_embree = new rt::embree({mesh()}); TOC(time_build_embree); gproshan_log_var(time_build_embree); @@ -677,7 +672,7 @@ void viewer::draw_selected_vertices() glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glColor3f(0., 0.5, 0.5); - double h = 0.02 * cam.zoom; +// double h = 0.02 * cam.zoom; for(int v: select_vertices) { From 254f7712b523fa0449140893a499ddb523eef794 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 11 Feb 2020 17:49:51 +0100 Subject: [PATCH 0152/1018] incuding features aka harris 3d --- include/mdict/dictionary.h | 1 + src/app_viewer.cpp | 2 +- src/mdict/dictionary.cpp | 25 +++++++++++++++++++++++++ src/mdict/inpainting.cpp | 30 ++++++++++++++++++++++++++---- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 4322349d..fa182a6b 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -66,6 +66,7 @@ class dictionary void sparse_coding(); void init_sampling(); void load_curvatures(a_vec & curvatures); + void load_features(vector & v_feat); void init_patches( const bool & reset = 1, const fmask_t & mask = nullptr ); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b72523f7..0e0bba9e 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_patches(); + dict.init_radial_curvature_patches(); //dict.init_voronoi_patches(); delete phi; viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 536ff7f3..6e7fc72c 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -9,6 +9,7 @@ #include #include +#include #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS @@ -189,6 +190,30 @@ void dictionary::load_curvatures(a_vec & curvatures) } +void dictionary::load_features(vector & v_feat) +{ + string f_feat = tmp_file_path(mesh->name_size() + ".int"); + ifstream inp; + inp.open(f_feat.c_str(), ifstream::in); + inp.close(); + index_t tmp; + + if(inp.fail()){ + inp.clear(ios::failbit); + // call the function using system + //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d + //./harris3d fandisk.off example.prop + } + else // recover it + { + while(!inp.eof()) + { + inp>>tmp; + v_feat.push_back(tmp); + } + } +} + void dictionary::init_patches(const bool & reset, const fmask_t & mask) { gproshan_log(MDICT); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 3988a8e0..132dd526 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -280,19 +280,21 @@ void inpainting::init_radial_curvature_patches() load_curvatures(curvatures); TOC(d_time) gproshan_debug_var(d_time); - +/* string f_norm = tmp_file_path(mesh->name_size() + ".n"); a_mat normals; normals.load(f_norm); priority_queue< pair > Q; + real_t max = - INFINITY ; for(index_t v = 0; v < mesh->n_vertices(); v++) { // -1 * curvatures(v) alta curvatura // curvatures(v) baja curvatura - Q.push ( make_pair( -1 * curvatures(v), v ) ); + if(abs( curvatures(v)) > max) max = abs( curvatures(v)); + Q.push ( make_pair( abs( curvatures(v) ), v ) ); } bool covered[mesh->n_vertices()]; @@ -327,10 +329,29 @@ void inpainting::init_radial_curvature_patches() } Q.pop(); } + M = count; gproshan_debug_var(M); vector outliers; string f_points = tmp_file_path(mesh->name_size() + ".points"); + size_t i=0; + gproshan_debug_var(max); + while( i<= mesh->n_vertices()/2) + { + index_t s = Q.top().second; + real_t c = Q.top().first; + outliers.push_back(s); + + gproshan_debug_var(max/5); + if(c < max/5) + { + + break; + } + Q.pop(); + i++; + } + /* for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) @@ -344,8 +365,9 @@ void inpainting::init_radial_curvature_patches() { outlv(i) = outliers[i]; } - outlv.save(f_points); + outlv.save(f_points);*/ +/* ////////////////////////////////////////////////////////////////////////////////// load_mask(); @@ -398,7 +420,7 @@ void inpainting::init_radial_curvature_patches() phi_basis->discrete(p.phi, p.xyz); gproshan_debug_var(s); - } + } */ gproshan_log(radial patches are ready); } From ca1927a970b294ce2f5023b43241f8be94250c5f Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 12 Feb 2020 12:47:58 +0100 Subject: [PATCH 0153/1018] fixing feature sampling --- src/app_viewer.cpp | 2 +- src/mdict/dictionary.cpp | 26 +++++++++++++++++--------- src/mdict/inpainting.cpp | 25 ++++++++++++++++--------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 0e0bba9e..65b284c4 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -541,7 +541,7 @@ void viewer_process_mask() dict.init_radial_curvature_patches(); //dict.init_voronoi_patches(); delete phi; - viewer::mesh().update_colors(&dict[0]); + //viewer::mesh().update_colors(&dict[0]); string f_points = tmp_file_path(viewer::mesh()->name_size() + ".points"); a_vec points_out; points_out.load(f_points); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 6e7fc72c..c87f081c 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -192,26 +192,34 @@ void dictionary::load_curvatures(a_vec & curvatures) void dictionary::load_features(vector & v_feat) { - string f_feat = tmp_file_path(mesh->name_size() + ".int"); + string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; inp.open(f_feat.c_str(), ifstream::in); - inp.close(); + index_t tmp; + gproshan_debug_var(f_feat); if(inp.fail()){ inp.clear(ios::failbit); // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d - //./harris3d fandisk.off example.prop + + string command = "../../Harris3D-Cpp/harris3d " + tmp_file_path(mesh->name()) + ".off" + " ../tmp/example.prop"; + gproshan_debug_var(command); + system(command.c_str()); + gproshan_debug(created); + inp.close(); + inp.open(f_feat.c_str(), ifstream::in); } - else // recover it + + gproshan_debug(exists); + + while(!inp.eof()) { - while(!inp.eof()) - { - inp>>tmp; - v_feat.push_back(tmp); - } + inp>>tmp; + v_feat.push_back(tmp); } + inp.close(); } void dictionary::init_patches(const bool & reset, const fmask_t & mask) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 132dd526..5def47cf 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -273,14 +273,20 @@ void inpainting::init_radial_patches() void inpainting::init_radial_curvature_patches() { - + // compute features + vector features; + TIC(d_time) + load_features(features); + TOC(d_time) + gproshan_debug_var(d_time); + //gproshan_debug_var(features.size()); //compute mean curvature - a_vec curvatures; +/* a_vec curvatures; TIC(d_time) load_curvatures(curvatures); TOC(d_time) gproshan_debug_var(d_time); -/* + string f_norm = tmp_file_path(mesh->name_size() + ".n"); a_mat normals; @@ -333,7 +339,7 @@ void inpainting::init_radial_curvature_patches() M = count; gproshan_debug_var(M); vector outliers; - string f_points = tmp_file_path(mesh->name_size() + ".points"); + size_t i=0; gproshan_debug_var(max); while( i<= mesh->n_vertices()/2) @@ -359,13 +365,14 @@ void inpainting::init_radial_curvature_patches() outliers.push_back(i); } - } - a_vec outlv(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) + }*/ + string f_points = tmp_file_path(mesh->name_size() + ".points"); + a_vec outlv(features.size()); + for(index_t i = 0; i < features.size(); i++) { - outlv(i) = outliers[i]; + outlv(i) = features[i]; } - outlv.save(f_points);*/ + outlv.save(f_points); /* ////////////////////////////////////////////////////////////////////////////////// From 2ced1892c8532fbd0247f10d96a7fa63b2b8be4b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 12 Feb 2020 14:58:24 +0100 Subject: [PATCH 0154/1018] rt: load meshes --- include/raytracing/rt_embree.h | 6 +++--- include/raytracing/rt_optix.h | 13 ++++++------- src/raytracing/rt_embree.cpp | 24 ++++++++++++------------ src/raytracing/rt_optix.cpp | 5 +++-- src/viewer/viewer.cpp | 12 +++++------- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 5a19948f..538186fc 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -80,10 +80,10 @@ class embree : public raytracing const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - void build_bvh(); + void build_bvh(const std::vector & meshes); index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_mesh(const che * mesh); + index_t add_point_cloud(const che * mesh); glm::vec4 li(const ray_hit & r, const glm::vec3 & light); }; diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 12fd8bb7..1e955189 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -21,18 +21,17 @@ namespace gproshan::rt { class optix : public raytracing { public: - optix(); + optix(const std::vector & meshes); ~optix(); - void build_bvh(); - index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - private: - const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + + void build_as(const std::vector & meshes); + index_t add_sphere(const glm::vec4 & xyzr); + index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 72bb7f52..de2278e7 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -26,10 +26,7 @@ embree::embree(const std::vector & meshes) rtcInitIntersectContext(&intersect_context); - for(auto & m: meshes) - add_mesh(m); - - build_bvh(); + build_bvh(meshes); } embree::~embree() @@ -38,8 +35,11 @@ embree::~embree() rtcReleaseDevice(device); } -void embree::build_bvh() +void embree::build_bvh(const std::vector & meshes) { + for(auto & m: meshes) + add_mesh(m); + rtcCommitScene(scene); } @@ -60,7 +60,7 @@ index_t embree::add_sphere(const glm::vec4 & xyzr) return geom_id; } -index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) +index_t embree::add_mesh(const che * mesh) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); @@ -75,13 +75,13 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) RTC_FORMAT_UINT3, 3 * sizeof(index_t), mesh->n_faces() ); - +#ifdef SINGLE_P + memcpy(vertices, &mesh->gt(0), mesh->n_vertices() * sizeof(vertex)); +#else #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) - { - glm::vec4 v(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 1.f); - vertices[i] = glm::vec3(model_matrix * v); - } + vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); +#endif // SINGLE_P memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); @@ -93,7 +93,7 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_matrix) return geom_id; } -index_t embree::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix) +index_t embree::add_point_cloud(const che * mesh) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 771a0e26..bee279cd 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -13,7 +13,7 @@ namespace gproshan::rt { -optix::optix() +optix::optix(const std::vector & meshes) { optixInit(); } @@ -22,8 +22,9 @@ optix::~optix() { } -void optix::build_bvh() +void optix::build_as(const std::vector & meshes) { + } index_t optix::add_sphere(const glm::vec4 & xyzr) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 639df6a7..2f1a617b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -609,15 +609,13 @@ void viewer::render_optix() if(rt_optix == nullptr) { - double time_build_embree; - TIC(time_build_embree); + double time_build_optix; + TIC(time_build_optix); - rt_optix = new rt::optix; - rt_optix->add_mesh(mesh()); - rt_optix->build_bvh(); + rt_optix = new rt::optix({mesh()}); - TOC(time_build_embree); - gproshan_log_var(time_build_embree); + TOC(time_build_optix); + gproshan_log_var(time_build_optix); } rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), From 1a9256ca33fd32444e9ee1315891befb846fe1d3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 12 Feb 2020 16:17:46 +0100 Subject: [PATCH 0155/1018] rt: optix add mesh --- include/raytracing/rt_optix.h | 6 ++-- src/raytracing/rt_optix.cpp | 54 +++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 1e955189..c6ae14f5 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -28,10 +28,8 @@ class optix : public raytracing const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - void build_as(const std::vector & meshes); - index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); - index_t add_point_cloud(const che * mesh, const glm::mat4 & model_matrix = glm::mat4(1.f)); + OptixTraversableHandle build_as(const std::vector & meshes); + void add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index bee279cd..dd14c18a 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -22,24 +22,54 @@ optix::~optix() { } -void optix::build_as(const std::vector & meshes) +OptixTraversableHandle optix::build_as(const std::vector & meshes) { + std::vector optix_meshes(meshes.size()); + std::vector optix_trig_flags(meshes.size()); -} - -index_t optix::add_sphere(const glm::vec4 & xyzr) -{ - return 0; -} + for(index_t i = 0; i < meshes.size(); i++) + add_mesh(optix_meshes[i], optix_trig_flags[i], meshes[i]); -index_t optix::add_mesh(const che * mesh, const glm::mat4 & model_matrix) -{ - return 0; + OptixTraversableHandle optix_as_handle = {}; + + return optix_as_handle; } -index_t optix::add_point_cloud(const che * mesh, const glm::mat4 & model_matrix) +void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh) { - return 0; + void * d_vertex; + void * d_index; + + cudaMalloc(&d_vertex, mesh->n_vertices() * sizeof(vertex)); + cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices() * sizeof(vertex), cudaMemcpyHostToDevice); + + cudaMalloc(&d_index, mesh->n_half_edges() * sizeof(index_t)); + cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t), cudaMemcpyHostToDevice); + + optix_mesh = {}; + optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; + +#ifdef SINGLE_P + optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; +#else + optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_DOUBLE3; //ERROR! +#endif // SINGLE_P + optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); + optix_mesh.triangleArray.numVertices = (int) mesh->n_vertices(); + optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; + + optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; + optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); + optix_mesh.triangleArray.numIndexTriplets = (int) mesh->n_faces(); + optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; + + optix_trig_flags = 0 ; + + optix_mesh.triangleArray.flags = &optix_trig_flags; + optix_mesh.triangleArray.numSbtRecords = 1; + optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; + optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; + optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; } const glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) From c2eaadb5bf656d3514bff150bc5173e4c5754089 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 12 Feb 2020 17:37:45 +0100 Subject: [PATCH 0156/1018] rt: optix build acceleration structure --- CMakeLists.txt | 2 +- include/config.h | 2 +- include/raytracing/rt_optix.h | 7 +++ src/raytracing/rt_optix.cpp | 84 +++++++++++++++++++++++++++++++++-- src/viewer/viewer.cpp | 4 ++ 5 files changed, 94 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c217e2e..9ca2bac7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,7 +92,7 @@ if(CUDA_FOUND) add_library(gproshan_cu STATIC ${cu_sources}) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) target_link_libraries(gproshan_cu gproshan_cpp) - target_link_libraries(gproshan_cu ${CUDA_LIBRARIES} ${CUDA_cublas_LIBRARY} ${CUDA_cusolver_LIBRARY} ${CUDA_cusparse_LIBRARY}) + target_link_libraries(gproshan_cu ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_cublas_LIBRARY} ${CUDA_cusolver_LIBRARY} ${CUDA_cusparse_LIBRARY}) endif(CUDA_FOUND) add_executable(gproshan gproshan.cpp) diff --git a/include/config.h b/include/config.h index a0133ae5..fe8bc38e 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -//#define SINGLE_P +#define SINGLE_P // print log messages #define LOG diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index c6ae14f5..8b3f062c 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -20,6 +20,13 @@ namespace gproshan::rt { class optix : public raytracing { + CUcontext cuda_context; + CUstream stream; + cudaDeviceProp device_prop; + + OptixDeviceContext optix_context; + + public: optix(const std::vector & meshes); ~optix(); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index dd14c18a..138b2eff 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -13,9 +13,25 @@ namespace gproshan::rt { +void optix_log(unsigned int level, const char * tag, const char * message, void *) +{ + fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); +} + optix::optix(const std::vector & meshes) { optixInit(); + + cudaStreamCreate(&stream); + + cudaGetDeviceProperties(&device_prop, 0); // device id = 0 + + cuCtxGetCurrent(&cuda_context); + + optixDeviceContextCreate(cuda_context, 0, &optix_context); + optixDeviceContextSetLogCallback(optix_context, optix_log, nullptr, 4); + + build_as(meshes); } optix::~optix() @@ -24,13 +40,75 @@ optix::~optix() OptixTraversableHandle optix::build_as(const std::vector & meshes) { + OptixTraversableHandle optix_as_handle = {}; + std::vector optix_meshes(meshes.size()); std::vector optix_trig_flags(meshes.size()); for(index_t i = 0; i < meshes.size(); i++) add_mesh(optix_meshes[i], optix_trig_flags[i], meshes[i]); - OptixTraversableHandle optix_as_handle = {}; + OptixAccelBuildOptions optix_accel_opt = {}; + optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_NONE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION; + optix_accel_opt.motionOptions.numKeys = 1; + optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; + + OptixAccelBufferSizes optix_gas_buffer_size; + optixAccelComputeMemoryUsage( optix_context, + &optix_accel_opt, + optix_meshes.data(), + optix_meshes.size(), + &optix_gas_buffer_size + ); + + void * d_compacted_size; + cudaMalloc(&d_compacted_size, sizeof(uint64_t)); + + OptixAccelEmitDesc optix_emit_desc; + optix_emit_desc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE; + optix_emit_desc.result = (CUdeviceptr) d_compacted_size; + + void * d_temp_buffer; + cudaMalloc(&d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes); + + void * d_output_buffer; + cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); + + optixAccelBuild( optix_context, + 0, // stream + &optix_accel_opt, + optix_meshes.data(), + optix_meshes.size(), + (CUdeviceptr) d_temp_buffer, + optix_gas_buffer_size.tempSizeInBytes, + (CUdeviceptr) d_output_buffer, + optix_gas_buffer_size.outputSizeInBytes, + &optix_as_handle, + &optix_emit_desc, + 1 + ); + + cudaDeviceSynchronize(); + + uint64_t compacted_size; + cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); + + void * d_as; + cudaMalloc(&d_as, compacted_size); + + optixAccelCompact( optix_context, + 0, // stream + optix_as_handle, + (CUdeviceptr) d_as, + compacted_size, + &optix_as_handle + ); + + cudaDeviceSynchronize(); + + cudaFree(d_output_buffer); + cudaFree(d_temp_buffer); + cudaFree(d_compacted_size); return optix_as_handle; } @@ -55,12 +133,12 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_DOUBLE3; //ERROR! #endif // SINGLE_P optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); - optix_mesh.triangleArray.numVertices = (int) mesh->n_vertices(); + optix_mesh.triangleArray.numVertices = mesh->n_vertices(); optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = (int) mesh->n_faces(); + optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces(); optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; optix_trig_flags = 0 ; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2f1a617b..63b3c827 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -82,6 +82,10 @@ viewer::~viewer() #ifdef GPROSHAN_EMBREE if(rt_embree) delete rt_embree; #endif // GPROSHAN_EMBREE + + #ifdef GPROSHAN_OPTIX + if(rt_optix) delete rt_optix; + #endif // GPROSHAN_OPTIX } bool viewer::run() From d07a5ade798395e9da73e400fa46134a85a486af Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 12 Feb 2020 17:57:20 +0100 Subject: [PATCH 0157/1018] rt: OptiX_INCLUDE cmake --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ca2bac7..9de89944 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,12 +20,14 @@ endif(CUDA_FOUND) find_package(Embree 3.7) if(EMBREE_LIBRARY) add_definitions(-DGPROSHAN_EMBREE) + include_directories(${EMBREE_INCLUDE_DIRS}) endif(EMBREE_LIBRARY) find_package(OptiX 7.0) if(OptiX_INCLUDE) add_definitions(-DGPROSHAN_OPTIX) + include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) @@ -59,8 +61,6 @@ include_directories(${AMADILLO_INCLUDE_DIR}) include_directories(${EIGEN3_INCLUDE_DIR}) include_directories(${SuiteSparse_INCLUDE_DIRS}) include_directories(${CGAL_INCLUDE_DIRS}) -include_directories(${EMBREE_INCLUDE_DIRS}) -include_directories(${OptiX_INCLUDE}) include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/include/viewer) From 1e096fa395888ff016a4e8bb073fbc1f9152a546 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 13 Feb 2020 12:38:23 +0100 Subject: [PATCH 0158/1018] adding radial feature patches --- include/mdict/inpainting.h | 2 +- include/mdict/patch.h | 6 +- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 111 +++++++++++++++++++++++++------------ src/mdict/patch.cpp | 18 +++--- 5 files changed, 88 insertions(+), 51 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 3e5a1339..63813758 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -26,7 +26,7 @@ class inpainting : public dictionary void load_mask(); void init_voronoi_patches(); void init_radial_patches(); - void init_radial_curvature_patches(); + void init_radial_feature_patches(); distance_t execute_tmp(); private: diff --git a/include/mdict/patch.h b/include/mdict/patch.h index efbab528..7e261aa3 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -61,11 +61,7 @@ class patch index_t * _toplevel = nullptr); void init_radial_disjoint(che * mesh, const distance_t & radio_, - const size_t & avg_p, - const index_t & v, - const size_t & n_toplevels, - vector & _vertices, - index_t * _toplevel = nullptr); + const index_t & v); void init_curvature_growing(che * mesh, const index_t & v, a_mat & normals); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 65b284c4..55a4bf77 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -538,7 +538,7 @@ void viewer_process_mask() basis * phi = new basis_dct(n); inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); - dict.init_radial_curvature_patches(); + dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; //viewer::mesh().update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5def47cf..fc7b7259 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -149,12 +149,6 @@ void inpainting::init_radial_patches() patches.resize(M); patches_map.resize(n_vertices); - //saving first vertex aka seed vertices - #pragma omp for - for(index_t s = 0; s < M; s++) - { - vertices[s].push_back(sample(s)); - } bool covered[mesh->n_vertices()]; #pragma omp for @@ -176,7 +170,7 @@ void inpainting::init_radial_patches() // mask at the end // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), avg_p, sample(it), dictionary::T, vertices[it], toplevel); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it)); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -271,7 +265,7 @@ void inpainting::init_radial_patches() } -void inpainting::init_radial_curvature_patches() +void inpainting::init_radial_feature_patches() { // compute features vector features; @@ -279,6 +273,77 @@ void inpainting::init_radial_curvature_patches() load_features(features); TOC(d_time) gproshan_debug_var(d_time); + + geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + + string f_points = tmp_file_path(mesh->name_size() + ".points"); + + gproshan_debug_var(features.size()); + + size_t count = 0; + distance_t radio = geo[ indexes[mesh->n_vertices()-1] ] ; + //radio *= 1.1; + gproshan_debug_var(radio); + + patches_map.resize(mesh->n_vertices()); + + //Coverage of the points + bool covered[mesh->n_vertices()]; + + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + vector outliers; + + for(size_t i = mesh->n_vertices()-1; i >= features.size()+1 ; i--) + { + // gproshan_debug_var(indexes[i]); + // gproshan_debug_var(geo[ indexes[i]]); + // actual seed + index_t s = indexes[i]; + + if(!covered[s]) + { + patch p; + p.init_radial_disjoint(mesh, radio, s); + if(p.vertices.size() > 0) + { + gproshan_debug_var(p.vertices.size()); + for(index_t i = 0; i < p.vertices.size(); i++) + covered[ p.vertices[i] ] = 1; + patches.push_back(p); + count++; + } + } + // outlv(count++) = indexes[i]; + } + M = count; + gproshan_debug_var(M); + + + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + if(!covered[i] ) + { + outliers.push_back(indexes[i]); + gproshan_debug_var(geo[indexes[i]] ); + } + } + a_vec outlv(outliers.size()); + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; + +/* + for(index_t i = 0; i < features.size(); i++) + { + outlv(i) = features[i]; + } + */ + outlv.save(f_points); //gproshan_debug_var(features.size()); //compute mean curvature /* a_vec curvatures; @@ -339,24 +404,7 @@ void inpainting::init_radial_curvature_patches() M = count; gproshan_debug_var(M); vector outliers; - - size_t i=0; - gproshan_debug_var(max); - while( i<= mesh->n_vertices()/2) - { - index_t s = Q.top().second; - real_t c = Q.top().first; - outliers.push_back(s); - - gproshan_debug_var(max/5); - if(c < max/5) - { - - break; - } - Q.pop(); - i++; - } + /* for(index_t i = 0; i < mesh->n_vertices(); i++) { @@ -366,13 +414,8 @@ void inpainting::init_radial_curvature_patches() } }*/ - string f_points = tmp_file_path(mesh->name_size() + ".points"); - a_vec outlv(features.size()); - for(index_t i = 0; i < features.size(); i++) - { - outlv(i) = features[i]; - } - outlv.save(f_points); + + /* ////////////////////////////////////////////////////////////////////////////////// @@ -533,7 +576,7 @@ void inpainting::init_voronoi_patches() distance_t inpainting::execute() { - TIC(d_time) init_radial_patches(); TOC(d_time) + TIC(d_time) init_radial_feature_patches(); TOC(d_time) gproshan_debug_var(d_time); // L = 15; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index fedb6ad9..f8faa4f6 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -55,16 +55,14 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const size_t & avg_p, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v) { radio = radio_; //radio = -INFINITY; - index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; - - gather_vertices(mesh, v, n_toplevels, toplevel); + normal_fit_directions(mesh, v); - - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()/20); + + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -72,6 +70,8 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si vertex n; n.x = vn(0); n.y = vn(1); n.z = vn(2); vertex p, c; + vertices.push_back(v); + for(int i=1; i radio) radio = *p;*/ - _vertices.push_back(indexes[i]); + vertices.push_back(indexes[i]); } else { @@ -95,9 +95,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const si } - vertices = std::move(_vertices); - - if(!_toplevel) delete [] toplevel; // If it is null +// vertices = std::move(_vertices); } From 7a0f076269ef148004b1def7fbecfdb43c0ba97e Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 13 Feb 2020 18:25:01 +0100 Subject: [PATCH 0159/1018] new radio criteria --- include/mdict/patch.h | 3 +- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 102 ++++++++------------------------------- src/mdict/patch.cpp | 15 +++--- 4 files changed, 29 insertions(+), 93 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 7e261aa3..2a8d6370 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -61,7 +61,8 @@ class patch index_t * _toplevel = nullptr); void init_radial_disjoint(che * mesh, const distance_t & radio_, - const index_t & v); + const index_t & v, + bool * covered); void init_curvature_growing(che * mesh, const index_t & v, a_mat & normals); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 55a4bf77..7a9cd543 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -541,7 +541,7 @@ void viewer_process_mask() dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; - //viewer::mesh().update_colors(&dict[0]); + viewer::mesh().update_colors(&dict[0]); string f_points = tmp_file_path(viewer::mesh()->name_size() + ".points"); a_vec points_out; points_out.load(f_points); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index fc7b7259..377cf96a 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -170,7 +170,7 @@ void inpainting::init_radial_patches() // mask at the end // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it)); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), covered); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -267,25 +267,33 @@ void inpainting::init_radial_patches() void inpainting::init_radial_feature_patches() { - // compute features + // compute features will be seeds vector features; TIC(d_time) load_features(features); TOC(d_time) gproshan_debug_var(d_time); + string f_points = tmp_file_path(mesh->name_size() + ".points"); + geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); - string f_points = tmp_file_path(mesh->name_size() + ".points"); - gproshan_debug_var(features.size()); + size_t old_features_size = features.size(); + for(index_t i = mesh->n_vertices()-1; i >= old_features_size ; i--) + { + features.push_back(indexes[i]); + //gproshan_debug_var(geo[ indexes[i]]); + + } + // extending the set of seeds size_t count = 0; - distance_t radio = geo[ indexes[mesh->n_vertices()-1] ] ; + distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; //radio *= 1.1; - gproshan_debug_var(radio); + gproshan_debug_var(max_radio); patches_map.resize(mesh->n_vertices()); @@ -299,7 +307,8 @@ void inpainting::init_radial_feature_patches() } vector outliers; - for(size_t i = mesh->n_vertices()-1; i >= features.size()+1 ; i--) +//for(size_t i = mesh->n_vertices()-1; i >= mesh->n_vertices()-1 +3 ; i--) + for(size_t i = 0; i < features.size(); i++) { // gproshan_debug_var(indexes[i]); // gproshan_debug_var(geo[ indexes[i]]); @@ -309,8 +318,8 @@ void inpainting::init_radial_feature_patches() if(!covered[s]) { patch p; - p.init_radial_disjoint(mesh, radio, s); - if(p.vertices.size() > 0) + p.init_radial_disjoint(mesh, max_radio, s, covered); + if(p.vertices.size() > 1) { gproshan_debug_var(p.vertices.size()); for(index_t i = 0; i < p.vertices.size(); i++) @@ -337,7 +346,7 @@ void inpainting::init_radial_feature_patches() for(index_t i = 0; i < outliers.size(); i++) outlv(i) = outliers[i]; -/* +/* a_vec outlv(features.size()); for(index_t i = 0; i < features.size(); i++) { outlv(i) = features[i]; @@ -345,78 +354,6 @@ void inpainting::init_radial_feature_patches() */ outlv.save(f_points); //gproshan_debug_var(features.size()); - //compute mean curvature -/* a_vec curvatures; - TIC(d_time) - load_curvatures(curvatures); - TOC(d_time) - gproshan_debug_var(d_time); - - string f_norm = tmp_file_path(mesh->name_size() + ".n"); - - a_mat normals; - normals.load(f_norm); - - priority_queue< pair > Q; - real_t max = - INFINITY ; - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - - // -1 * curvatures(v) alta curvatura - // curvatures(v) baja curvatura - if(abs( curvatures(v)) > max) max = abs( curvatures(v)); - Q.push ( make_pair( abs( curvatures(v) ), v ) ); - } - - bool covered[mesh->n_vertices()]; - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; - } - - patches_map.resize(mesh->n_vertices()); - - size_t count = 0; - while(!Q.empty()) // curvatures from higher to lower - { - index_t s = Q.top().second; - //gproshan_debug_var(Q.top().first); - //gproshan_debug_var(Q.top().second); - - if(!covered[s]) - { - - patch tmp; - tmp.init_curvature_growing(mesh, s, normals); - if(tmp.vertices.size() > 0) - { - gproshan_debug_var(tmp.vertices.size()); - for(index_t i = 0; i < tmp.vertices.size(); i++) - covered[ tmp.vertices[i] ] = 1; - patches.push_back(tmp); - count++; - } - } - Q.pop(); - } - - M = count; - gproshan_debug_var(M); - vector outliers; - - /* - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) - { - outliers.push_back(i); - } - - }*/ - - - /* ////////////////////////////////////////////////////////////////////////////////// load_mask(); @@ -468,7 +405,6 @@ void inpainting::init_radial_feature_patches() p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - gproshan_debug_var(s); } */ gproshan_log(radial patches are ready); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f8faa4f6..0e4502a8 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -55,10 +55,10 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, bool * covered) { - radio = radio_; - //radio = -INFINITY; + //radio = radio_; + radio = -INFINITY; normal_fit_directions(mesh, v); @@ -80,12 +80,11 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in p = p - c ; p = p - ((p,n)*n); - if(*p <= radio ) - //if(*p <= radio && ( n, mesh->normal(indexes[i]) ) >= 0) - //if( *p <= 2*radio && acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) + //if(*p <= radio ) + if( !covered[indexes[i]] && acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) { - /*if(*p > radio) - radio = *p;*/ + if(*p > radio) + radio = *p; vertices.push_back(indexes[i]); } else From 68f6c84c1650e745cbf3f33442160b805b47060c Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 14 Feb 2020 14:28:24 +0100 Subject: [PATCH 0160/1018] new sampling variable radios debugging --- include/mdict/patch.h | 2 +- src/mdict/dictionary.cpp | 11 +++-- src/mdict/inpainting.cpp | 87 ++++++++++++++++++++++++++++++++++------ src/mdict/patch.cpp | 12 +++++- 4 files changed, 93 insertions(+), 19 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 2a8d6370..e67f90ff 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -62,7 +62,7 @@ class patch void init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, - bool * covered); + distance_t & euc_radio); void init_curvature_growing(che * mesh, const index_t & v, a_mat & normals); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index c87f081c..2f3b9fd9 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -196,7 +196,7 @@ void dictionary::load_features(vector & v_feat) ifstream inp; inp.open(f_feat.c_str(), ifstream::in); - index_t tmp; + index_t tam; gproshan_debug_var(f_feat); if(inp.fail()){ @@ -213,12 +213,15 @@ void dictionary::load_features(vector & v_feat) } gproshan_debug(exists); - - while(!inp.eof()) + inp>>tam; + v_feat.resize(tam); + for(int i=0; i>v_feat[i]; + /*while(!inp.eof()) { inp>>tmp; v_feat.push_back(tmp); - } + }*/ inp.close(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 377cf96a..88841e78 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -160,7 +160,7 @@ void inpainting::init_radial_patches() s = 0; size_t it = 0; index_t * toplevel = new index_t[mesh->n_vertices()]; - + distance_t radio; while(it < M) { @@ -170,7 +170,7 @@ void inpainting::init_radial_patches() // mask at the end // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), covered); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -269,13 +269,78 @@ void inpainting::init_radial_feature_patches() { // compute features will be seeds vector features; + vector seeds; TIC(d_time) load_features(features); TOC(d_time) gproshan_debug_var(d_time); - + + gproshan_debug_var(features.size()); string f_points = tmp_file_path(mesh->name_size() + ".points"); + + geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + size_t count = 0; + distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; + //radio *= 1.1; + gproshan_debug_var(max_radio); + + patches_map.resize(mesh->n_vertices()); + + //Coverage of the points + bool covered[mesh->n_vertices()]; + + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + distance_t euc_radio; + vector radios; + for(int i = 0; i < features.size(); i++) + { + bool found = false; + int j = 0; + + //select the next one + while(j < seeds.size() && !found){ + //calculate distance between the actual patch p seed i and other features + const vertex & v_patch = mesh->gt(features[i]); + const vertex & v_seed = mesh->gt(seeds[j]); + + /* double distX = interestPoints[j].getX() - candidatePoints[i].getX(); + double distY = interestPoints[j].getY() - candidatePoints[i].getY(); + double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ + + if( *(v_patch - v_seed) < radios[j] ) // radio of each patch + found = true; + j++; + } + if(!found) + { //it is a new patch + // get_radious + patch p; + p.init_radial_disjoint(mesh, 2*max_radio, indexes[i], euc_radio); + //gproshan_debug_var(p.vertices.size()); + for(index_t i = 0; i < p.vertices.size(); i++) + { + covered[ p.vertices[i] ] = 1; + //gproshan_debug_var(p.vertices[i] ); + } + patches.push_back(p); + seeds.push_back(features[i]); + radios.push_back( euc_radio ); + count++; + gproshan_debug_var(euc_radio); + gproshan_debug_var(indexes[i]); + gproshan_debug_var(p.vertices.size()); + } + + } + +/* geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -305,7 +370,7 @@ void inpainting::init_radial_feature_patches() { covered[i] = 0; } - vector outliers; + //for(size_t i = mesh->n_vertices()-1; i >= mesh->n_vertices()-1 +3 ; i--) for(size_t i = 0; i < features.size(); i++) @@ -329,28 +394,26 @@ void inpainting::init_radial_feature_patches() } } // outlv(count++) = indexes[i]; - } + }*/ M = count; gproshan_debug_var(M); - + vector outliers; + for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) { outliers.push_back(indexes[i]); - gproshan_debug_var(geo[indexes[i]] ); + //gproshan_debug_var(geo[indexes[i]] ); } } a_vec outlv(outliers.size()); for(index_t i = 0; i < outliers.size(); i++) outlv(i) = outliers[i]; -/* a_vec outlv(features.size()); - for(index_t i = 0; i < features.size(); i++) - { - outlv(i) = features[i]; - } + /*for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; */ outlv.save(f_points); //gproshan_debug_var(features.size()); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 0e4502a8..454a53ac 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -55,7 +55,7 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, bool * covered) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio) { //radio = radio_; radio = -INFINITY; @@ -71,6 +71,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in n.x = vn(0); n.y = vn(1); n.z = vn(2); vertex p, c; vertices.push_back(v); + euc_radio = -INFINITY; for(int i=1; inormal(indexes[i]) ) ) <= PI/2 ) + if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) { if(*p > radio) + { radio = *p; + } + //compute euclidean radio + p = mesh->get_vertex(indexes[i]); + if(*(p - c) > euc_radio) + euc_radio = *(p - c); + //gproshan_debug_var(euc_radio); vertices.push_back(indexes[i]); } else From 8151a5715a7ec772205159af1185fd42bbb81642 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 17 Feb 2020 12:09:31 +0100 Subject: [PATCH 0161/1018] fixed sampling still need more testing --- src/mdict/inpainting.cpp | 25 +++++++++++++------------ src/mdict/patch.cpp | 5 +++-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 88841e78..54c48f96 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -298,6 +298,7 @@ void inpainting::init_radial_feature_patches() } distance_t euc_radio; vector radios; + for(int i = 0; i < features.size(); i++) { bool found = false; @@ -321,21 +322,20 @@ void inpainting::init_radial_feature_patches() { //it is a new patch // get_radious patch p; - p.init_radial_disjoint(mesh, 2*max_radio, indexes[i], euc_radio); - - //gproshan_debug_var(p.vertices.size()); - for(index_t i = 0; i < p.vertices.size(); i++) + p.init_radial_disjoint(mesh, 2*max_radio, features[i], euc_radio); + + gproshan_debug_var(p.vertices.size()); + for(index_t k = 0; k < p.vertices.size(); k++) { - covered[ p.vertices[i] ] = 1; - //gproshan_debug_var(p.vertices[i] ); + covered[ p.vertices[k] ] = 1; } patches.push_back(p); seeds.push_back(features[i]); radios.push_back( euc_radio ); count++; - gproshan_debug_var(euc_radio); - gproshan_debug_var(indexes[i]); - gproshan_debug_var(p.vertices.size()); + // gproshan_debug_var(euc_radio); + // gproshan_debug_var(indexes[i]); + // gproshan_debug_var(p.vertices.size()); } } @@ -404,11 +404,12 @@ void inpainting::init_radial_feature_patches() { if(!covered[i] ) { - outliers.push_back(indexes[i]); + outliers.push_back(i); //gproshan_debug_var(geo[indexes[i]] ); } } a_vec outlv(outliers.size()); + gproshan_debug_var(outliers.size()); for(index_t i = 0; i < outliers.size(); i++) outlv(i) = outliers[i]; @@ -417,7 +418,7 @@ void inpainting::init_radial_feature_patches() */ outlv.save(f_points); //gproshan_debug_var(features.size()); -/* + ////////////////////////////////////////////////////////////////////////////////// load_mask(); @@ -469,7 +470,7 @@ void inpainting::init_radial_feature_patches() p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - } */ + } gproshan_log(radial patches are ready); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 454a53ac..376a73a1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -72,7 +72,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertex p, c; vertices.push_back(v); euc_radio = -INFINITY; - + //gproshan_debug_var(v); for(int i=1; inormal(indexes[i]) ) ) <= PI/2 ) { if(*p > radio) @@ -94,6 +95,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in euc_radio = *(p - c); //gproshan_debug_var(euc_radio); vertices.push_back(indexes[i]); + //gproshan_debug_var(geo[indexes[i]]); } else { @@ -146,7 +148,6 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normal //gproshan_debug_var(vertices.size()); size_t d_fitting = 2; - size_t d_monge = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; if(vertices.size() <= min_points ) { From 74816c664a2038180cedd123834e230092224ebc Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 17 Feb 2020 13:05:43 +0100 Subject: [PATCH 0162/1018] improving initial sampling --- shaders/new_fragment.glsl | 4 ++++ src/mdict/inpainting.cpp | 32 ++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/shaders/new_fragment.glsl b/shaders/new_fragment.glsl index be591d16..76fa9315 100644 --- a/shaders/new_fragment.glsl +++ b/shaders/new_fragment.glsl @@ -62,6 +62,10 @@ vec3 colormap(float x) float b = clamp(colormap_blue(x) / 255.0, .0, .9); return vec3(r, g, b); } +//winter +vec3 colormap2(float x) { + return vec3(0.0, clamp(x, 0.0, 1.0), clamp(-0.5 * x + 1.0, 0.0, 1.0)); +} void main() { diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 54c48f96..d2ef1d57 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -314,7 +314,7 @@ void inpainting::init_radial_feature_patches() double distY = interestPoints[j].getY() - candidatePoints[i].getY(); double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ - if( *(v_patch - v_seed) < radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 1.2*radios[j] ) // radio of each patch found = true; j++; } @@ -322,20 +322,24 @@ void inpainting::init_radial_feature_patches() { //it is a new patch // get_radious patch p; - p.init_radial_disjoint(mesh, 2*max_radio, features[i], euc_radio); + p.init_radial_disjoint(mesh, 1.5*max_radio, features[i], euc_radio); gproshan_debug_var(p.vertices.size()); - for(index_t k = 0; k < p.vertices.size(); k++) + if(p.vertices.size() >= 7) { - covered[ p.vertices[k] ] = 1; - } - patches.push_back(p); - seeds.push_back(features[i]); - radios.push_back( euc_radio ); - count++; - // gproshan_debug_var(euc_radio); - // gproshan_debug_var(indexes[i]); - // gproshan_debug_var(p.vertices.size()); + for(index_t k = 0; k < p.vertices.size(); k++) + { + covered[ p.vertices[k] ] = 1; + } + patches.push_back(p); + seeds.push_back(features[i]); + radios.push_back( euc_radio ); + count++; + // gproshan_debug_var(euc_radio); + // gproshan_debug_var(indexes[i]); + // gproshan_debug_var(p.vertices.size()); + } + } } @@ -410,8 +414,8 @@ void inpainting::init_radial_feature_patches() } a_vec outlv(outliers.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; + for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; From 96acb02559a898c73f3a6f12a5867fa52310eeaf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 17 Feb 2020 16:24:38 +0100 Subject: [PATCH 0163/1018] fix warnings --- CMakeLists.txt | 24 +++++++-------- include/quaternion.h | 3 +- include/raytracing/raytracing.h | 10 +++---- include/raytracing/rt_embree.h | 4 +-- include/raytracing/rt_optix.h | 4 +-- include/viewer/viewer.h | 5 +++- src/che.cpp | 2 +- src/che_ply.cpp | 4 ++- src/quaternion.cpp | 7 +---- src/raytracing/rt_embree.cpp | 4 +-- src/raytracing/rt_optix.cpp | 53 ++++++++++++++++++++++++++++++--- src/raytracing/rt_optix.cu | 3 ++ src/test_geodesics_ptp.cpp | 13 ++++---- src/viewer/viewer.cpp | 28 +++++++++-------- 14 files changed, 106 insertions(+), 58 deletions(-) create mode 100644 src/raytracing/rt_optix.cu diff --git a/CMakeLists.txt b/CMakeLists.txt index 9de89944..3c42d645 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,12 +8,20 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(CUDA 10.1) if(CUDA_FOUND) enable_language(CUDA) add_definitions(-DGPROSHAN_CUDA) include_directories(${CUDA_INCLUDE_DIRS}) + + find_package(OptiX 7.0) + if(OptiX_INCLUDE) + add_definitions(-DGPROSHAN_OPTIX) + include_directories(${OptiX_INCLUDE}) + endif(OptiX_INCLUDE) endif(CUDA_FOUND) @@ -24,13 +32,6 @@ if(EMBREE_LIBRARY) endif(EMBREE_LIBRARY) -find_package(OptiX 7.0) -if(OptiX_INCLUDE) - add_definitions(-DGPROSHAN_OPTIX) - include_directories(${OptiX_INCLUDE}) -endif(OptiX_INCLUDE) - - find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) @@ -43,9 +44,6 @@ find_package(Eigen3 REQUIRED) find_package(SuiteSparse REQUIRED) -set(CMAKE_CXX_FLAGS "-fopenmp -Wall -Wno-unused-result") -set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") - set(THREADS_PREFER_PTHREAD_FLAG ON) add_definitions(${EIGEN3_DEFINITIONS}) @@ -75,7 +73,9 @@ add_library(imgui STATIC ${imgui_sources}) FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) add_library(gproshan_cpp STATIC ${cpp_sources}) -target_link_libraries(gproshan_cpp ${OpenMP_CXX}) +target_compile_options(gproshan_cpp PRIVATE -Wall -Wno-unused-result) + +target_link_libraries(gproshan_cpp OpenMP::OpenMP_CXX) target_link_libraries(gproshan_cpp OpenGL::GL) target_link_libraries(gproshan_cpp GLEW::GLEW) target_link_libraries(gproshan_cpp glfw) @@ -88,7 +88,7 @@ target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) if(CUDA_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) + FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/cuda/*.cu) add_library(gproshan_cu STATIC ${cu_sources}) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) target_link_libraries(gproshan_cu gproshan_cpp) diff --git a/include/quaternion.h b/include/quaternion.h index 57b7870f..b5bed6aa 100644 --- a/include/quaternion.h +++ b/include/quaternion.h @@ -17,8 +17,7 @@ class quaternion vertex v; public: - quaternion(void); - quaternion(const quaternion & q); + quaternion(); quaternion(real_t s, real_t vi, real_t vj, real_t vk); quaternion(real_t s, const vertex & v); quaternion(real_t s); diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index addd3209..a2a0c353 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -43,12 +43,12 @@ class raytracing protected: - virtual const glm::vec4 intersect_li( const glm::vec3 & org, - const glm::vec3 & dir, - const glm::vec3 & light ) = 0; + virtual glm::vec4 intersect_li( const glm::vec3 & org, + const glm::vec3 & dir, + const glm::vec3 & light ) = 0; - virtual const float intersect_depth( const glm::vec3 & org, - const glm::vec3 & dir ) = 0; + virtual float intersect_depth( const glm::vec3 & org, + const glm::vec3 & dir ) = 0; }; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 538186fc..db7adf1d 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -77,8 +77,8 @@ class embree : public raytracing bool intersect(ray_hit & r); bool occluded(ray_hit & r); - const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); - const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); void build_bvh(const std::vector & meshes); index_t add_sphere(const glm::vec4 & xyzr); diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 8b3f062c..8cfe69b9 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -32,8 +32,8 @@ class optix : public raytracing ~optix(); private: - const glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); - const float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); OptixTraversableHandle build_as(const std::vector & meshes); void add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 021c5a27..91e3273d 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -71,7 +71,7 @@ class viewer { protected: - typedef void (*function_t) (viewer *); + using function_t = void (*) (viewer *); struct process_t { @@ -79,6 +79,9 @@ class viewer std::string name; function_t function; index_t sub_menu; + + process_t() = default; + process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; }; static const int m_window_size[N_MESHES][2]; diff --git a/src/che.cpp b/src/che.cpp index be6f0f91..cd42d331 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -602,7 +602,7 @@ void che::multiplicate_vertices() index_t * aVT = new index_t[nh + 6 * n_edges_]; index_t * aOT = new index_t[nh + 6 * n_edges_]; index_t * aEVT = new index_t[nv + n_edges_]; - index_t * aET = new index_t[ne + 3 * n_edges_]; + index_t * aET = new index_t[ne + 3 * n_edges_]; index_t * aEHT = new index_t[nh + 6 * n_edges_]; memcpy(aGT, GT, n_vertices_ * sizeof(vertex)); diff --git a/src/che_ply.cpp b/src/che_ply.cpp index 2f916f31..da4a82ea 100644 --- a/src/che_ply.cpp +++ b/src/che_ply.cpp @@ -109,7 +109,7 @@ void che_ply::read_file(const string & file) } else // binary_little_endian { - char vbuffer[vbytes]; + char * vbuffer = new char[vbytes]; for(index_t v = 0; v < n_vertices_; v++) { is.read(vbuffer, vbytes); @@ -151,6 +151,8 @@ void che_ply::read_file(const string & file) if(fbytes == 4) VT[he++] = *((int *) vbuffer); } } + + delete [] vbuffer; } is.close(); diff --git a/src/quaternion.cpp b/src/quaternion.cpp index aa4cc2c1..1b39368b 100644 --- a/src/quaternion.cpp +++ b/src/quaternion.cpp @@ -10,16 +10,11 @@ using namespace std; namespace gproshan { -quaternion :: quaternion(void) +quaternion :: quaternion() : s(0.), v(0., 0., 0.) { } -quaternion :: quaternion(const quaternion & q) -: s(q.s), v(q.v) -{ -} - quaternion :: quaternion(real_t s_, real_t vi, real_t vj, real_t vk) : s(s_), v(vi, vj, vk) { diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index de2278e7..ce04dade 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -161,13 +161,13 @@ bool embree::occluded(ray_hit & r) } -const glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) +glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) { ray_hit r(org, dir); return intersect(r) ? li(r, light) : glm::vec4(0.f); } -const float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) +float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) { ray_hit r(org, dir); return intersect(r) ? r.ray.tfar : 0.f; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 138b2eff..fbef9cfc 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -22,6 +22,8 @@ optix::optix(const std::vector & meshes) { optixInit(); + // create context + cudaStreamCreate(&stream); cudaGetDeviceProperties(&device_prop, 0); // device id = 0 @@ -31,7 +33,50 @@ optix::optix(const std::vector & meshes) optixDeviceContextCreate(cuda_context, 0, &optix_context); optixDeviceContextSetLogCallback(optix_context, optix_log, nullptr, 4); + // create module + + OptixModule optix_module; + OptixModuleCompileOptions optix_module_compile_opt; + + OptixPipeline optix_pipeline; + OptixPipelineCompileOptions optix_pipeline_compile_opt; + OptixPipelineLinkOptions optix_pipeline_link_opt; + + optix_module_compile_opt.maxRegisterCount = 50; + optix_module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; + optix_module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; + + optix_pipeline_compile_opt = {}; + optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; + optix_pipeline_compile_opt.usesMotionBlur = false; + optix_pipeline_compile_opt.numPayloadValues = 2; + optix_pipeline_compile_opt.numAttributeValues = 2; + optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optixLaunchParams"; + + optix_pipeline_link_opt.overrideUsesMotionBlur = false; + optix_pipeline_link_opt.maxTraceDepth = 2; + + const std::string ptx_code;; + + optixModuleCreateFromPTX( optix_context, + &optix_module_compile_opt, + &optix_pipeline_compile_opt, + ptx_code.c_str(), + ptx_code.size(), + nullptr, nullptr, // log message + &optix_module + ); + + // create programs + + // build as + build_as(meshes); + + // create pipeline + + // build sbt } optix::~optix() @@ -73,7 +118,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) void * d_output_buffer; cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); - + optixAccelBuild( optix_context, 0, // stream &optix_accel_opt, @@ -92,7 +137,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) uint64_t compacted_size; cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); - + gproshan_error_var(compacted_size); void * d_as; cudaMalloc(&d_as, compacted_size); @@ -150,12 +195,12 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; } -const glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) +glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) { return glm::vec4(0.f); } -const float optix::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) +float optix::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) { return 0; } diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu new file mode 100644 index 00000000..fb6d2a25 --- /dev/null +++ b/src/raytracing/rt_optix.cu @@ -0,0 +1,3 @@ +#include + + diff --git a/src/test_geodesics_ptp.cpp b/src/test_geodesics_ptp.cpp index 5c766dc0..5f1f2b3f 100644 --- a/src/test_geodesics_ptp.cpp +++ b/src/test_geodesics_ptp.cpp @@ -27,15 +27,14 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) int n_test = nargs == 5 ? atoi(args[4]) : 10; - FILE * ftable; - #ifdef SINGLE_P - ftable = fopen("ptp_results.tex", "w"); - #else - ftable = fopen("ptp_results_double.tex", "w"); - #endif +#ifdef SINGLE_P + FILE * ftable = fopen("ptp_results.tex", "w"); +#else + FILE *ftable = fopen("ptp_results_double.tex", "w"); + const char * ptime = "& %6.3lfs "; +#endif const char * str[2] = {"", "\\bf"}; - const char * ptime = "& %6.3lfs "; const char * pspeedup = "& \\bf (%.1lfx) "; const char * pbtime = "& %6s %6.3lfs "; const char * pberror = "& %6s %6.2lf\\%% "; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 63b3c827..4cbbd98e 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -126,35 +126,37 @@ bool viewer::run() ImGui::NewFrame(); if(ImGui::BeginMainMenuBar()) - { - if(ImGui::BeginMenu("Select")) + { + if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; i++) if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str())) current = i; - ImGui::EndMenu(); + ImGui::EndMenu(); } for(index_t i = 0; i < sub_menus.size(); i++) { - if(ImGui::BeginMenu(sub_menus[i].c_str())) - { + if(ImGui::BeginMenu(sub_menus[i].c_str())) + { for(auto & p: processes) - if(p.second.function != nullptr && p.second.sub_menu == i && - ImGui::MenuItem(p.second.name.c_str(), ('[' + p.second.key + ']').c_str())) + if( p.second.function != nullptr && + p.second.sub_menu == i && + ImGui::MenuItem(p.second.name.c_str(), ('[' + p.second.key + ']').c_str()) ) p.second.function(this); - ImGui::EndMenu(); + ImGui::EndMenu(); } - } - ImGui::EndMainMenuBar(); + } + + ImGui::EndMainMenuBar(); } // Rendering ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + glfwSwapBuffers(window); glfwPollEvents(); } @@ -226,7 +228,7 @@ void viewer::init_imgui() ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 460"); + ImGui_ImplOpenGL3_Init("#version 460"); } void viewer::init_menus() From 5bbefaaacc10a3394637daf3bac1fcb867c20978 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 17 Feb 2020 16:41:26 +0100 Subject: [PATCH 0164/1018] sampling ready and tested --- include/mdict/dictionary.h | 2 +- src/mdict/dictionary.cpp | 18 +++++--- src/mdict/inpainting.cpp | 85 +++++++------------------------------- src/mdict/patch.cpp | 2 +- 4 files changed, 30 insertions(+), 77 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index fa182a6b..45e996b1 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -66,7 +66,7 @@ class dictionary void sparse_coding(); void init_sampling(); void load_curvatures(a_vec & curvatures); - void load_features(vector & v_feat); + void load_features(vector & v_feat, size_t & featsize); void init_patches( const bool & reset = 1, const fmask_t & mask = nullptr ); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 2f3b9fd9..cf0ef13f 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -190,13 +190,14 @@ void dictionary::load_curvatures(a_vec & curvatures) } -void dictionary::load_features(vector & v_feat) +void dictionary::load_features(vector & v_feat, size_t & featsize) { string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; inp.open(f_feat.c_str(), ifstream::in); - index_t tam; + size_t tam; + index_t tmp; gproshan_debug_var(f_feat); if(inp.fail()){ @@ -213,15 +214,20 @@ void dictionary::load_features(vector & v_feat) } gproshan_debug(exists); + inp>>featsize; + //v_feat.resize(tam); + for(int i=0; i>tmp; + v_feat.push_back(tmp); + } inp>>tam; - v_feat.resize(tam); for(int i=0; i>v_feat[i]; - /*while(!inp.eof()) { inp>>tmp; v_feat.push_back(tmp); - }*/ + } + inp.close(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index d2ef1d57..9dda7dd5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -268,17 +268,20 @@ void inpainting::init_radial_patches() void inpainting::init_radial_feature_patches() { // compute features will be seeds - vector features; + vector all_sorted_features; vector seeds; + size_t featsize; TIC(d_time) - load_features(features); + load_features(all_sorted_features, featsize); TOC(d_time) gproshan_debug_var(d_time); - gproshan_debug_var(features.size()); + gproshan_debug_var(all_sorted_features.size()); string f_points = tmp_file_path(mesh->name_size() + ".points"); - - geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); + + vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); + gproshan_debug_var(features.size()); + geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; @@ -299,7 +302,7 @@ void inpainting::init_radial_feature_patches() distance_t euc_radio; vector radios; - for(int i = 0; i < features.size(); i++) + for(int i = 0; i < all_sorted_features.size(); i++) { bool found = false; int j = 0; @@ -307,14 +310,14 @@ void inpainting::init_radial_feature_patches() //select the next one while(j < seeds.size() && !found){ //calculate distance between the actual patch p seed i and other features - const vertex & v_patch = mesh->gt(features[i]); + const vertex & v_patch = mesh->gt(all_sorted_features[i]); const vertex & v_seed = mesh->gt(seeds[j]); /* double distX = interestPoints[j].getX() - candidatePoints[i].getX(); double distY = interestPoints[j].getY() - candidatePoints[i].getY(); double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ - if( *(v_patch - v_seed) < 1.2*radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.8 *radios[j] ) // radio of each patch found = true; j++; } @@ -322,9 +325,9 @@ void inpainting::init_radial_feature_patches() { //it is a new patch // get_radious patch p; - p.init_radial_disjoint(mesh, 1.5*max_radio, features[i], euc_radio); + p.init_radial_disjoint(mesh, 1.5*max_radio, all_sorted_features[i], euc_radio); - gproshan_debug_var(p.vertices.size()); + //gproshan_debug_var(p.vertices.size()); if(p.vertices.size() >= 7) { for(index_t k = 0; k < p.vertices.size(); k++) @@ -332,7 +335,7 @@ void inpainting::init_radial_feature_patches() covered[ p.vertices[k] ] = 1; } patches.push_back(p); - seeds.push_back(features[i]); + seeds.push_back(all_sorted_features[i]); radios.push_back( euc_radio ); count++; // gproshan_debug_var(euc_radio); @@ -340,65 +343,9 @@ void inpainting::init_radial_feature_patches() // gproshan_debug_var(p.vertices.size()); } - } - - } - -/* - geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); - index_t * indexes = new index_t[geo.n_sorted_index()]; - geo.copy_sorted_index(indexes, geo.n_sorted_index()); - - gproshan_debug_var(features.size()); - size_t old_features_size = features.size(); - for(index_t i = mesh->n_vertices()-1; i >= old_features_size ; i--) - { - features.push_back(indexes[i]); - //gproshan_debug_var(geo[ indexes[i]]); - - } - // extending the set of seeds - - size_t count = 0; - distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; - //radio *= 1.1; - gproshan_debug_var(max_radio); - - patches_map.resize(mesh->n_vertices()); - - //Coverage of the points - bool covered[mesh->n_vertices()]; - - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; + } } - -//for(size_t i = mesh->n_vertices()-1; i >= mesh->n_vertices()-1 +3 ; i--) - for(size_t i = 0; i < features.size(); i++) - { - // gproshan_debug_var(indexes[i]); - // gproshan_debug_var(geo[ indexes[i]]); - // actual seed - index_t s = indexes[i]; - - if(!covered[s]) - { - patch p; - p.init_radial_disjoint(mesh, max_radio, s, covered); - if(p.vertices.size() > 1) - { - gproshan_debug_var(p.vertices.size()); - for(index_t i = 0; i < p.vertices.size(); i++) - covered[ p.vertices[i] ] = 1; - patches.push_back(p); - count++; - } - } - // outlv(count++) = indexes[i]; - }*/ M = count; gproshan_debug_var(M); @@ -412,7 +359,7 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(outliers.size()); + a_vec outlv(seeds.size()); gproshan_debug_var(outliers.size()); for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 376a73a1..1bdf8ae0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -83,7 +83,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in p = p - ((p,n)*n); //if(*p <= radio ) //gproshan_debug_var(indexes[i]); - if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) + if( acos( (n, mesh->normal(indexes[i]) ) ) < PI/2 ) { if(*p > radio) { From b6d48cc0e3c689cb459465e4ec83e5b03ca38b64 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 17 Feb 2020 17:43:10 +0100 Subject: [PATCH 0165/1018] fixed sparse coding --- src/mdict/inpainting.cpp | 8 +++----- src/mdict/mdict.cpp | 4 ++-- src/mdict/patch.cpp | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 9dda7dd5..d44af9a6 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -411,7 +411,7 @@ void inpainting::init_radial_feature_patches() gproshan_debug(passed); #pragma omp parallel for - for(index_t s = 0; s < M-1; s++) + for(index_t s = 0; s < M; s++) { patch & p = patches[s]; @@ -545,11 +545,10 @@ distance_t inpainting::execute() patches_map[i].clear(); } - + for(index_t s = 0; s < M; s++) patches[s].reset_xyz(mesh, patches_map, s, 0); - #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -565,7 +564,7 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); - draw_patches(50); + draw_patches(4); /* draw_patches(200); draw_patches(120); */ @@ -575,7 +574,6 @@ distance_t inpainting::execute() //phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); - TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 86797991..bb87bcb9 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -140,7 +140,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) arma::uvec indices = arma::sort_index( V , "desscend"); - for(int i=0; i< V.size(); i++) + for(size_t i=0; i< V.size(); i++) if(mask[ indices [i]]) return indices[i]; } @@ -236,6 +236,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { + return OMP(p.xyz.row(2).t(), p.phi * A, L); } a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) @@ -249,7 +250,6 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) // gproshan_debug_var(p.avg_dist); mask(i) = 1; } - return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 1bdf8ae0..eb87022c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -73,7 +73,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertices.push_back(v); euc_radio = -INFINITY; //gproshan_debug_var(v); - for(int i=1; iget_vertex(indexes[i]); @@ -249,7 +249,7 @@ void patch::reset_xyz_disjoint(che * mesh, distance_t * dist, size_t M, vector Date: Tue, 18 Feb 2020 11:14:16 +0100 Subject: [PATCH 0166/1018] parameters guarantee full coverage --- src/mdict/inpainting.cpp | 69 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index d44af9a6..3c01c19a 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -317,7 +317,7 @@ void inpainting::init_radial_feature_patches() double distY = interestPoints[j].getY() - candidatePoints[i].getY(); double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ - if( *(v_patch - v_seed) < 0.8 *radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.7 *radios[j] ) // radio of each patch found = true; j++; } @@ -325,7 +325,7 @@ void inpainting::init_radial_feature_patches() { //it is a new patch // get_radious patch p; - p.init_radial_disjoint(mesh, 1.5*max_radio, all_sorted_features[i], euc_radio); + p.init_radial_disjoint(mesh, 1.1*max_radio, all_sorted_features[i], euc_radio); //gproshan_debug_var(p.vertices.size()); if(p.vertices.size() >= 7) @@ -346,6 +346,61 @@ void inpainting::init_radial_feature_patches() } } +/* + geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + + gproshan_debug_var(features.size()); + size_t old_features_size = features.size(); + for(index_t i = mesh->n_vertices()-1; i >= old_features_size ; i--) + { + features.push_back(indexes[i]); + //gproshan_debug_var(geo[ indexes[i]]); + + } + // extending the set of seeds + + size_t count = 0; + distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; + //radio *= 1.1; + gproshan_debug_var(max_radio); + + patches_map.resize(mesh->n_vertices()); + + //Coverage of the points + bool covered[mesh->n_vertices()]; + + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + + +//for(size_t i = mesh->n_vertices()-1; i >= mesh->n_vertices()-1 +3 ; i--) + for(size_t i = 0; i < features.size(); i++) + { + // gproshan_debug_var(indexes[i]); + // gproshan_debug_var(geo[ indexes[i]]); + // actual seed + index_t s = indexes[i]; + + if(!covered[s]) + { + patch p; + p.init_radial_disjoint(mesh, max_radio, s, covered); + if(p.vertices.size() > 1) + { + gproshan_debug_var(p.vertices.size()); + for(index_t i = 0; i < p.vertices.size(); i++) + covered[ p.vertices[i] ] = 1; + patches.push_back(p); + count++; + } + } + // outlv(count++) = indexes[i]; + }*/ M = count; gproshan_debug_var(M); @@ -360,7 +415,7 @@ void inpainting::init_radial_feature_patches() } } a_vec outlv(seeds.size()); - gproshan_debug_var(outliers.size()); + gproshan_debug_var(seeds.size()); for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; @@ -411,7 +466,7 @@ void inpainting::init_radial_feature_patches() gproshan_debug(passed); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < M-1; s++) { patch & p = patches[s]; @@ -545,10 +600,11 @@ distance_t inpainting::execute() patches_map[i].clear(); } - + for(index_t s = 0; s < M; s++) patches[s].reset_xyz(mesh, patches_map, s, 0); + #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -564,7 +620,7 @@ distance_t inpainting::execute() bool *pmask; draw_patches(10); - draw_patches(4); + draw_patches(50); /* draw_patches(200); draw_patches(120); */ @@ -574,6 +630,7 @@ distance_t inpainting::execute() //phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); + TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); } From 7a3d9eced9ce426630d2a8edb5400016934a6375 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 18 Feb 2020 11:23:13 +0100 Subject: [PATCH 0167/1018] fixed initial sampling coverage --- src/mdict/inpainting.cpp | 57 +--------------------------------------- 1 file changed, 1 insertion(+), 56 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 3c01c19a..e5c82c9b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -346,61 +346,6 @@ void inpainting::init_radial_feature_patches() } } -/* - geodesics geo(mesh, features, geodesics::FM, NULL, false, mesh->n_vertices()); - index_t * indexes = new index_t[geo.n_sorted_index()]; - geo.copy_sorted_index(indexes, geo.n_sorted_index()); - - gproshan_debug_var(features.size()); - size_t old_features_size = features.size(); - for(index_t i = mesh->n_vertices()-1; i >= old_features_size ; i--) - { - features.push_back(indexes[i]); - //gproshan_debug_var(geo[ indexes[i]]); - - } - // extending the set of seeds - - size_t count = 0; - distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; - //radio *= 1.1; - gproshan_debug_var(max_radio); - - patches_map.resize(mesh->n_vertices()); - - //Coverage of the points - bool covered[mesh->n_vertices()]; - - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; - } - - -//for(size_t i = mesh->n_vertices()-1; i >= mesh->n_vertices()-1 +3 ; i--) - for(size_t i = 0; i < features.size(); i++) - { - // gproshan_debug_var(indexes[i]); - // gproshan_debug_var(geo[ indexes[i]]); - // actual seed - index_t s = indexes[i]; - - if(!covered[s]) - { - patch p; - p.init_radial_disjoint(mesh, max_radio, s, covered); - if(p.vertices.size() > 1) - { - gproshan_debug_var(p.vertices.size()); - for(index_t i = 0; i < p.vertices.size(); i++) - covered[ p.vertices[i] ] = 1; - patches.push_back(p); - count++; - } - } - // outlv(count++) = indexes[i]; - }*/ M = count; gproshan_debug_var(M); @@ -466,7 +411,7 @@ void inpainting::init_radial_feature_patches() gproshan_debug(passed); #pragma omp parallel for - for(index_t s = 0; s < M-1; s++) + for(index_t s = 0; s < M; s++) { patch & p = patches[s]; From af81a0d330d35d01f9605839decd4fe97a3feca9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 18 Feb 2020 12:09:04 +0100 Subject: [PATCH 0168/1018] compiling ptx --- CMakeLists.txt | 14 ++++++++++++-- include/config.h | 2 +- src/raytracing/rt_optix.cpp | 30 ++++++++++++++++++++++-------- src/raytracing/rt_optix.cu | 4 ++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c42d645..b88dea79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.12) -project(gproshan VERSION 2.0 LANGUAGES CXX) +project(gproshan VERSION 2.0) list(APPEND CMAKE_MODULE_PATH "${gproshan_SOURCE_DIR}/cmake") @@ -70,8 +70,17 @@ include_directories(${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(imgui STATIC ${imgui_sources}) +set(ptx_code "") +if(CUDA_FOUND) + cuda_compile_ptx(ptx_files "${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu") + list(GET ptx_files 0 ptx_file) + set(ptx_code ptx_code.c) + add_custom_command( OUTPUT ${ptx_code} + COMMAND bin2c --const --padd 0 --type char --name ptx_code ${ptx_file} > ${ptx_code} DEPENDS ${ptx_file}) +endif(CUDA_FOUND) + FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) -add_library(gproshan_cpp STATIC ${cpp_sources}) +add_library(gproshan_cpp STATIC ${cpp_sources} ${ptx_code}) target_compile_options(gproshan_cpp PRIVATE -Wall -Wno-unused-result) @@ -90,6 +99,7 @@ target_link_libraries(gproshan_cpp imgui) if(CUDA_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/cuda/*.cu) add_library(gproshan_cu STATIC ${cu_sources}) + target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) target_link_libraries(gproshan_cu gproshan_cpp) target_link_libraries(gproshan_cu ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_cublas_LIBRARY} ${CUDA_cusolver_LIBRARY} ${CUDA_cusparse_LIBRARY}) diff --git a/include/config.h b/include/config.h index fe8bc38e..a0133ae5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -#define SINGLE_P +//#define SINGLE_P // print log messages #define LOG diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index fbef9cfc..a1ced6a7 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -8,6 +8,10 @@ #include + +extern "C" char ptx_code[]; + + // geometry processing and shape analysis framework // raytracing approach namespace gproshan::rt { @@ -57,13 +61,13 @@ optix::optix(const std::vector & meshes) optix_pipeline_link_opt.overrideUsesMotionBlur = false; optix_pipeline_link_opt.maxTraceDepth = 2; - const std::string ptx_code;; + const std::string str_ptx_code = ptx_code; optixModuleCreateFromPTX( optix_context, &optix_module_compile_opt, &optix_pipeline_compile_opt, - ptx_code.c_str(), - ptx_code.size(), + str_ptx_code.c_str(), + str_ptx_code.size(), nullptr, nullptr, // log message &optix_module ); @@ -162,9 +166,23 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, { void * d_vertex; void * d_index; - + + +#ifdef SINGLE_P cudaMalloc(&d_vertex, mesh->n_vertices() * sizeof(vertex)); cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices() * sizeof(vertex), cudaMemcpyHostToDevice); +#else + glm::vec3 * vertices = new glm::vec3[mesh->n_vertices()]; + cudaMalloc(&d_vertex, mesh->n_vertices() * sizeof(float) * 3); + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); + + cudaMemcpy(d_vertex, vertices, mesh->n_vertices() * sizeof(vertex), cudaMemcpyHostToDevice); + + delete [] vertices; +#endif // SINGLE_P cudaMalloc(&d_index, mesh->n_half_edges() * sizeof(index_t)); cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t), cudaMemcpyHostToDevice); @@ -172,11 +190,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; -#ifdef SINGLE_P optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; -#else - optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_DOUBLE3; //ERROR! -#endif // SINGLE_P optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); optix_mesh.triangleArray.numVertices = mesh->n_vertices(); optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index fb6d2a25..5fb1914a 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,3 +1,7 @@ #include +__global__ void hola() +{ + +} From 93623af56886fe29dac8e8c3867831be31930de3 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 18 Feb 2020 16:49:40 +0100 Subject: [PATCH 0169/1018] fixed sampling with features --- include/mdict/patch.h | 3 ++ src/mdict/inpainting.cpp | 75 +++++++++++++++++++++++++++++++++------- src/mdict/patch.cpp | 36 +++++++++++++++++++ 3 files changed, 101 insertions(+), 13 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index e67f90ff..48c6020b 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -63,6 +63,9 @@ class patch const distance_t & radio_, const index_t & v, distance_t & euc_radio); + void update_radial_disjoint(che * mesh, + const index_t & v, + vector & _vertices); void init_curvature_growing(che * mesh, const index_t & v, a_mat & normals); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e5c82c9b..bdb968ab 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -316,8 +316,9 @@ void inpainting::init_radial_feature_patches() /* double distX = interestPoints[j].getX() - candidatePoints[i].getX(); double distY = interestPoints[j].getY() - candidatePoints[i].getY(); double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ - - if( *(v_patch - v_seed) < 0.7 *radios[j] ) // radio of each patch + + // 0.7 coverage parameter + if( *(v_patch - v_seed) < 0.7*radios[j] ) // radio of each patch found = true; j++; } @@ -325,7 +326,8 @@ void inpainting::init_radial_feature_patches() { //it is a new patch // get_radious patch p; - p.init_radial_disjoint(mesh, 1.1*max_radio, all_sorted_features[i], euc_radio); + // increasing a bit the radio + p.init_radial_disjoint(mesh, 0.7*max_radio, all_sorted_features[i], euc_radio); //gproshan_debug_var(p.vertices.size()); if(p.vertices.size() >= 7) @@ -337,7 +339,7 @@ void inpainting::init_radial_feature_patches() patches.push_back(p); seeds.push_back(all_sorted_features[i]); radios.push_back( euc_radio ); - count++; + count+=p.vertices.size(); // gproshan_debug_var(euc_radio); // gproshan_debug_var(indexes[i]); // gproshan_debug_var(p.vertices.size()); @@ -345,11 +347,58 @@ void inpainting::init_radial_feature_patches() } } + vector outliers; + gproshan_debug_var(count); + M = seeds.size(); + ////////////////////////////// + //Remove extra overlapping using voronoi + //update vertices and radio +/* + geodesics voronoi(mesh, seeds , geodesics::FM, NULL, true, mesh->n_vertices()); + //index_t * indexes = new index_t[geo.n_sorted_index()]; + + vector vertices[M]; + + gproshan_debug_var(M); + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < M; s++) + { + vertices[s].push_back(seeds[s]); + } + + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + voronoi.clusters[i]--; // fixing the index betwween 0 and N + //gproshan_debug_var(i); + + if(seeds [i] != i && voronoi.clusters[i] < mesh->n_vertices()) //if not a seed vertex + { + vertices[ voronoi.clusters[i] ].push_back(i) ; + //gproshan_debug_var(voronoi.clusters[i]); + } + else + { + if(seeds [i] != i ) + outliers.push_back(i); + } + // + } - M = count; + for(index_t s = 0; s < M; s++) + { + patches[s].update_radial_disjoint(mesh,seeds[s],vertices[s]); + } +*/ + + + +/////////////////////////////////////// + gproshan_debug_var(M); - vector outliers; + for(index_t i = 0; i < mesh->n_vertices(); i++) { @@ -359,10 +408,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(seeds.size()); - gproshan_debug_var(seeds.size()); - for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; + a_vec outlv(outliers.size()); + gproshan_debug_var(outliers.size()); + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; @@ -566,9 +615,9 @@ distance_t inpainting::execute() draw_patches(10); draw_patches(50); -/* draw_patches(200); - draw_patches(120); - */ + draw_patches(90); + draw_patches(20); + //draw_patches(400); //draw_patches(500); //draw_patches(56); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index eb87022c..bbd3f1f2 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -108,6 +108,42 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } +void patch::update_radial_disjoint(che * mesh, const index_t & v, vector & _vertices) +{ + a_vec vn = T.col(2);// normal at the center + vertex n; + n.x = vn(0); n.y = vn(1); n.z = vn(2); + vertex p, c; + size_t d_fitting = 2; + size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + + if(vertices.size() >= 7) + { + radio = 0; + vertices = std::move(_vertices); + for(size_t i=1; i < vertices.size(); i++) + { + p = mesh->get_vertex(vertices[i]); + c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + if(*p > radio) + { + radio = *p; + } + } + jet_fit_directions(mesh, v); + + } + else + { + gproshan_debug_var(v); + gproshan_debug_var(_vertices.size()); + } + +} + void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normals) { radio = -INFINITY; From 9fc2fc2ef96c050c0756cc5788a5c16990f849c7 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 19 Feb 2020 15:35:24 +0100 Subject: [PATCH 0170/1018] finished sampling --- src/mdict/inpainting.cpp | 4 ++-- src/mdict/patch.cpp | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bdb968ab..3c83ffa7 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -318,7 +318,7 @@ void inpainting::init_radial_feature_patches() double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ // 0.7 coverage parameter - if( *(v_patch - v_seed) < 0.7*radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.5*radios[j] ) // radio of each patch found = true; j++; } @@ -327,7 +327,7 @@ void inpainting::init_radial_feature_patches() // get_radious patch p; // increasing a bit the radio - p.init_radial_disjoint(mesh, 0.7*max_radio, all_sorted_features[i], euc_radio); + p.init_radial_disjoint(mesh, 0.5*max_radio, all_sorted_features[i], euc_radio); //gproshan_debug_var(p.vertices.size()); if(p.vertices.size() >= 7) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index bbd3f1f2..e5cbbdf1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -83,7 +83,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in p = p - ((p,n)*n); //if(*p <= radio ) //gproshan_debug_var(indexes[i]); - if( acos( (n, mesh->normal(indexes[i]) ) ) < PI/2 ) + if( acos( (n, mesh->normal(indexes[i]) ) ) < PI/2.5 ) { if(*p > radio) { @@ -114,8 +114,6 @@ void patch::update_radial_disjoint(che * mesh, const index_t & v, vector= 7) { From 5963d65a3aa58d63232a30742d0f808093247a08 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 20 Feb 2020 17:04:15 +0100 Subject: [PATCH 0171/1018] fixed sampling reduced still needs debugging --- include/mdict/inpainting.h | 2 + include/mdict/patch.h | 1 + src/mdict/inpainting.cpp | 134 ++++++++++++++++++++++--------------- src/mdict/patch.cpp | 9 +++ 4 files changed, 92 insertions(+), 54 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 63813758..5bd0cf17 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -27,6 +27,8 @@ class inpainting : public dictionary void init_voronoi_patches(); void init_radial_patches(); void init_radial_feature_patches(); + vector sort_indexes(const vector &v); + distance_t execute_tmp(); private: diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 48c6020b..aaeed1d2 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -87,6 +87,7 @@ class patch const fmask_t & mask = nullptr ); const a_vec normal(); + bool is_covered( bool * covered); void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 3c83ffa7..22bcff99 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,6 +1,8 @@ #include "inpainting.h" #include #include +#include +#include #include @@ -46,7 +48,7 @@ void inpainting::load_mask() std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (percent/ 100.0)) ; - int k = 0; + size_t k = 0; size_t rn = 0; while (k < percentage) { @@ -145,7 +147,7 @@ void inpainting::init_radial_patches() gproshan_debug_var(d_time); //sampling - size_t count = 0, s; + size_t s; patches.resize(M); patches_map.resize(n_vertices); @@ -159,7 +161,6 @@ void inpainting::init_radial_patches() s = 0; size_t it = 0; - index_t * toplevel = new index_t[mesh->n_vertices()]; distance_t radio; while(it < M) { @@ -265,6 +266,22 @@ void inpainting::init_radial_patches() } +vector inpainting::sort_indexes(const vector &v) { + + // initialize original index locations + vector idx(v.size()); + iota(idx.begin(), idx.end(), 0); + + // sort indexes based on comparing values in v + // using std::stable_sort instead of std::sort + // to avoid unnecessary index re-orderings + // when v contains elements of equal values + stable_sort(idx.begin(), idx.end(), + [&v](index_t i1, index_t i2) {return v[i1] < v[i2];}); + + return idx; +} + void inpainting::init_radial_feature_patches() { // compute features will be seeds @@ -301,22 +318,20 @@ void inpainting::init_radial_feature_patches() } distance_t euc_radio; vector radios; + size_t count_cov = 0; - for(int i = 0; i < all_sorted_features.size(); i++) + for(size_t i = 0; i < all_sorted_features.size(); i++) { bool found = false; - int j = 0; - + size_t j = 0; + //select the next one - while(j < seeds.size() && !found){ + while(j < seeds.size() && !found ) + { //calculate distance between the actual patch p seed i and other features const vertex & v_patch = mesh->gt(all_sorted_features[i]); const vertex & v_seed = mesh->gt(seeds[j]); - /* double distX = interestPoints[j].getX() - candidatePoints[i].getX(); - double distY = interestPoints[j].getY() - candidatePoints[i].getY(); - double distZ = interestPoints[j].getZ() - candidatePoints[i].getZ();*/ - // 0.7 coverage parameter if( *(v_patch - v_seed) < 0.5*radios[j] ) // radio of each patch found = true; @@ -334,6 +349,7 @@ void inpainting::init_radial_feature_patches() { for(index_t k = 0; k < p.vertices.size(); k++) { + if(!covered[ p.vertices[k] ]) count_cov++; covered[ p.vertices[k] ] = 1; } patches.push_back(p); @@ -345,61 +361,71 @@ void inpainting::init_radial_feature_patches() // gproshan_debug_var(p.vertices.size()); } - } + } } + vector outliers; gproshan_debug_var(count); + gproshan_debug_var(count_cov); + gproshan_debug_var(seeds.size()); M = seeds.size(); - ////////////////////////////// - //Remove extra overlapping using voronoi - //update vertices and radio -/* - geodesics voronoi(mesh, seeds , geodesics::FM, NULL, true, mesh->n_vertices()); - //index_t * indexes = new index_t[geo.n_sorted_index()]; - - vector vertices[M]; - - gproshan_debug_var(M); - //saving first vertex aka seed vertices +////////////////////////////////////// + + //new order bigger to smaller + vector geo_radios; + geo_radios.resize(seeds.size()); + for(index_t i = 0; i < seeds.size(); i++) + geo_radios[i] = patches[i].radio; + vector sorted_patches_size = sort_indexes(geo_radios); + + //Coverage of the points + bool covered_p[mesh->n_vertices()]; + size_t ncount = 0; + seeds.clear(); + #pragma omp for - for(index_t s = 0; s < M; s++) - { - vertices[s].push_back(seeds[s]); - } - for(index_t i = 0; i < mesh->n_vertices(); i++) { - voronoi.clusters[i]--; // fixing the index betwween 0 and N - //gproshan_debug_var(i); - - if(seeds [i] != i && voronoi.clusters[i] < mesh->n_vertices()) //if not a seed vertex - { - vertices[ voronoi.clusters[i] ].push_back(i) ; - //gproshan_debug_var(voronoi.clusters[i]); - } - else - { - if(seeds [i] != i ) - outliers.push_back(i); - } - // + covered_p[i] = 0; } + size_t count_valid = 0; + + vector valid_patches; + + for(index_t i = 0; i < sorted_patches_size.size(); i++) + { + patch * p = &patches[sorted_patches_size[i]]; + if(!p->is_covered(covered_p)) // if not all vertices in the patch are covered + { + seeds.push_back(sorted_patches_size[i]); - for(index_t s = 0; s < M; s++) - { - patches[s].update_radial_disjoint(mesh,seeds[s],vertices[s]); - } -*/ - + valid_patches.push_back(patches[sorted_patches_size[i]]); + count_valid++; // we take this one + for(index_t k = 0; k < p->vertices.size(); k++) + { + if(!covered_p[ p->vertices[k] ]) ncount++; + covered_p[ p->vertices[k] ] = 1; + } + } + + //gproshan_debug_var(sorted_patches_size[i]); + //gproshan_debug_var(geo_radios[sorted_patches_size[i]]); + } + gproshan_debug_var(count_valid); + gproshan_debug_var(ncount); + + // discard patches which are not needed + patches.clear(); + patches = valid_patches; + gproshan_debug_var(patches.size()); + M = patches.size(); /////////////////////////////////////// gproshan_debug_var(M); - - for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) @@ -408,10 +434,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(outliers.size()); - gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; + a_vec outlv(seeds.size()); + gproshan_debug_var(seeds.size()); + for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index e5cbbdf1..db3b6bd9 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -577,6 +577,15 @@ void patch::compute_avg_distance() } //avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); } + +bool patch::is_covered( bool * covered) +{ + for(index_t i = 0; i < vertices.size(); i++) + if(!covered[i]) return false; + + return true; +} + //avg_dist = avg_dist/vertices.size(); } // namespace gproshan::mdict From 8659391fec80160d4cf9006b4c3373205a9c9e45 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Feb 2020 13:17:17 +0100 Subject: [PATCH 0172/1018] rt: create context and module optix --- CMakeLists.txt | 10 ++++++++-- include/raytracing/rt_optix.h | 1 - src/raytracing/rt_optix.cpp | 19 ++++++++++++------- src/raytracing/rt_optix.cu | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b88dea79..1750ebc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,9 @@ if(CUDA_FOUND) list(GET ptx_files 0 ptx_file) set(ptx_code ptx_code.c) add_custom_command( OUTPUT ${ptx_code} - COMMAND bin2c --const --padd 0 --type char --name ptx_code ${ptx_file} > ${ptx_code} DEPENDS ${ptx_file}) + COMMAND bin2c --const --padd 0 --type char --name ptx_code ${ptx_file} > ${ptx_code} + DEPENDS ${ptx_file} + ) endif(CUDA_FOUND) FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) @@ -102,7 +104,11 @@ if(CUDA_FOUND) target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) target_link_libraries(gproshan_cu gproshan_cpp) - target_link_libraries(gproshan_cu ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_cublas_LIBRARY} ${CUDA_cusolver_LIBRARY} ${CUDA_cusparse_LIBRARY}) + target_link_libraries(gproshan_cu ${CUDA_LIBRARIES}) + target_link_libraries(gproshan_cu ${CUDA_CUDA_LIBRARY}) + target_link_libraries(gproshan_cu ${CUDA_cublas_LIBRARY}) + target_link_libraries(gproshan_cu ${CUDA_cusolver_LIBRARY}) + target_link_libraries(gproshan_cu ${CUDA_cusparse_LIBRARY}) endif(CUDA_FOUND) add_executable(gproshan gproshan.cpp) diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 8cfe69b9..ce88f1d8 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -22,7 +22,6 @@ class optix : public raytracing { CUcontext cuda_context; CUstream stream; - cudaDeviceProp device_prop; OptixDeviceContext optix_context; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index a1ced6a7..8dd5167e 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -27,11 +27,10 @@ optix::optix(const std::vector & meshes) optixInit(); // create context + cudaSetDevice(0); cudaStreamCreate(&stream); - cudaGetDeviceProperties(&device_prop, 0); // device id = 0 - cuCtxGetCurrent(&cuda_context); optixDeviceContextCreate(cuda_context, 0, &optix_context); @@ -51,18 +50,18 @@ optix::optix(const std::vector & meshes) optix_module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; optix_pipeline_compile_opt = {}; - optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; + optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; optix_pipeline_compile_opt.usesMotionBlur = false; optix_pipeline_compile_opt.numPayloadValues = 2; optix_pipeline_compile_opt.numAttributeValues = 2; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optixLaunchParams"; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_launch_params"; optix_pipeline_link_opt.overrideUsesMotionBlur = false; optix_pipeline_link_opt.maxTraceDepth = 2; const std::string str_ptx_code = ptx_code; - + optixModuleCreateFromPTX( optix_context, &optix_module_compile_opt, &optix_pipeline_compile_opt, @@ -139,9 +138,14 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) cudaDeviceSynchronize(); +gproshan_error_var(optix_gas_buffer_size.tempSizeInBytes); +gproshan_error_var(optix_gas_buffer_size.outputSizeInBytes); + uint64_t compacted_size; cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); - gproshan_error_var(compacted_size); + +gproshan_error_var(compacted_size); + void * d_as; cudaMalloc(&d_as, compacted_size); @@ -154,6 +158,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) ); cudaDeviceSynchronize(); +gproshan_error_var(compacted_size); cudaFree(d_output_buffer); cudaFree(d_temp_buffer); @@ -191,7 +196,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; - optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); + optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); optix_mesh.triangleArray.numVertices = mesh->n_vertices(); optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 5fb1914a..1533d905 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,7 +1,21 @@ #include -__global__ void hola() +extern "C" __constant__ void * optix_launch_params; + +extern "C" __global__ void closest_hit() { + const int f = optixGetPrimitiveIndex(); +} + +extern "C" __global__ void any_hit() +{ +} +extern "C" __global__ void miss_hit() +{ +} + +extern "C" __global__ void __raygen__rt() +{ } From 743c9153984f64ac1f9a79af56021046b28f8b09 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 21 Feb 2020 16:08:27 +0100 Subject: [PATCH 0173/1018] improxving sampling --- src/mdict/inpainting.cpp | 39 +++++++++++++++++--------- src/mdict/patch.cpp | 60 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 22bcff99..e9c40f31 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -319,6 +319,8 @@ void inpainting::init_radial_feature_patches() distance_t euc_radio; vector radios; size_t count_cov = 0; + size_t count_cov_patch = 0; + distance_t over_factor = 2; for(size_t i = 0; i < all_sorted_features.size(); i++) { @@ -333,7 +335,7 @@ void inpainting::init_radial_feature_patches() const vertex & v_seed = mesh->gt(seeds[j]); // 0.7 coverage parameter - if( *(v_patch - v_seed) < 0.5*radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.5 * radios[j] ) // radio of each patch found = true; j++; } @@ -342,20 +344,31 @@ void inpainting::init_radial_feature_patches() // get_radious patch p; // increasing a bit the radio - p.init_radial_disjoint(mesh, 0.5*max_radio, all_sorted_features[i], euc_radio); + p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio); //gproshan_debug_var(p.vertices.size()); - if(p.vertices.size() >= 7) + count_cov_patch = 0; + if(p.vertices.size() >= 7 ) { for(index_t k = 0; k < p.vertices.size(); k++) { - if(!covered[ p.vertices[k] ]) count_cov++; - covered[ p.vertices[k] ] = 1; - } - patches.push_back(p); - seeds.push_back(all_sorted_features[i]); - radios.push_back( euc_radio ); - count+=p.vertices.size(); + if(!covered[ p.vertices[k] ]) count_cov_patch++; + //covered[ p.vertices[k] ] = 1; + } + + count_cov += count_cov_patch; + if(count_cov_patch > 0) + { + patches.push_back(p); + seeds.push_back(all_sorted_features[i]); + radios.push_back( euc_radio ); + count+=p.vertices.size(); + + for(index_t k = 0; k < p.vertices.size(); k++) + covered[ p.vertices[k] ] = 1; + + } + // gproshan_debug_var(euc_radio); // gproshan_debug_var(indexes[i]); // gproshan_debug_var(p.vertices.size()); @@ -371,7 +384,7 @@ void inpainting::init_radial_feature_patches() M = seeds.size(); ////////////////////////////////////// - +/* //new order bigger to smaller vector geo_radios; geo_radios.resize(seeds.size()); @@ -421,7 +434,7 @@ void inpainting::init_radial_feature_patches() patches = valid_patches; gproshan_debug_var(patches.size()); M = patches.size(); - +*/ /////////////////////////////////////// gproshan_debug_var(M); @@ -435,7 +448,7 @@ void inpainting::init_radial_feature_patches() } } a_vec outlv(seeds.size()); - gproshan_debug_var(seeds.size()); + gproshan_debug_var(outliers.size()); for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index db3b6bd9..4941d1bb 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -72,6 +72,8 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertex p, c; vertices.push_back(v); euc_radio = -INFINITY; + double angle; + double sum_angle = 0; //gproshan_debug_var(v); for(size_t i=1; inormal(indexes[i]) ) ) < PI/2.5 ) + angle = acos( (n, mesh->normal(indexes[i]) ) ) ; + if( angle < PI/2.5 && (sum_angle/vertices.size()) <= PI/( vertices.size() )) { + if(*p > radio) { radio = *p; @@ -96,13 +100,63 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //gproshan_debug_var(euc_radio); vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); + sum_angle += angle; + } else { break; } - + //gproshan_debug_var(sum_angle); } + delete indexes; + + /*if(vertices.size() == geo.n_sorted_index() ) + { + gproshan_debug_var(vertices.size()); + geodesics geo2(mesh, {v}, geodesics::FM, NULL, false, 0, 2*radio_); + indexes = new index_t[geo2.n_sorted_index()]; + geo2.copy_sorted_index(indexes, geo2.n_sorted_index()); + + for(size_t i=geo.n_sorted_index(); iget_vertex(indexes[i]); + c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + //if(*p <= radio ) + //gproshan_debug_var(indexes[i]); + angle = acos( (n, mesh->normal(indexes[i]) ) ) ; + if( angle < PI/2.5 && (sum_angle/vertices.size()) <= PI/( vertices.size() )) + { + + if(*p > radio) + { + radio = *p; + } + //compute euclidean radio + p = mesh->get_vertex(indexes[i]); + if(*(p - c) > euc_radio) + euc_radio = *(p - c); + //gproshan_debug_var(euc_radio); + vertices.push_back(indexes[i]); + //gproshan_debug_var(geo[indexes[i]]); + sum_angle += angle; + + } + else + { + break; + } + //gproshan_debug_var(sum_angle); + } + delete indexes; + }*/ + + //gproshan_debug_var(sum_angle/vertices.size()); + //gproshan_debug_var(PI/10); // vertices = std::move(_vertices); } @@ -155,7 +209,7 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normal vertices.push_back(v); - for(int i=1; i Date: Mon, 24 Feb 2020 12:50:17 +0100 Subject: [PATCH 0174/1018] sampling feature fixed --- src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e9c40f31..de01bde4 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -335,7 +335,7 @@ void inpainting::init_radial_feature_patches() const vertex & v_seed = mesh->gt(seeds[j]); // 0.7 coverage parameter - if( *(v_patch - v_seed) < 0.5 * radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.7 * radios[j] ) // radio of each patch found = true; j++; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 4941d1bb..30f40682 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -86,7 +86,8 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //if(*p <= radio ) //gproshan_debug_var(indexes[i]); angle = acos( (n, mesh->normal(indexes[i]) ) ) ; - if( angle < PI/2.5 && (sum_angle/vertices.size()) <= PI/( vertices.size() )) + //if( angle < PI/2.5 && (sum_angle/vertices.size()) + angle <= PI/2.2) + if( angle < PI/2.5 && (sum_angle) <= 1.2* PI) { if(*p > radio) @@ -110,11 +111,11 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //gproshan_debug_var(sum_angle); } delete indexes; - - /*if(vertices.size() == geo.n_sorted_index() ) +/* + if(vertices.size() == geo.n_sorted_index() ) { - gproshan_debug_var(vertices.size()); - geodesics geo2(mesh, {v}, geodesics::FM, NULL, false, 0, 2*radio_); + //gproshan_debug_var(vertices.size()); + geodesics geo2(mesh, {v}, geodesics::FM, NULL, false, 0, 3*radio_); indexes = new index_t[geo2.n_sorted_index()]; geo2.copy_sorted_index(indexes, geo2.n_sorted_index()); @@ -129,7 +130,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //if(*p <= radio ) //gproshan_debug_var(indexes[i]); angle = acos( (n, mesh->normal(indexes[i]) ) ) ; - if( angle < PI/2.5 && (sum_angle/vertices.size()) <= PI/( vertices.size() )) + if( angle < PI/2.5 && (sum_angle/vertices.size()) <= 1.5*PI/( vertices.size() )) { if(*p > radio) From 68340883cc3b0ad8b2ce6e2ac9ef9467fcdf2294 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 24 Feb 2020 18:05:34 +0100 Subject: [PATCH 0175/1018] compiling on macos clang opengl 4.1 --- include/che_fill_hole.h | 2 ++ include/include.h | 1 - include/mdict/mdict.h | 4 ++-- shaders/fragment.glsl | 2 +- shaders/fragment_gradient.glsl | 2 +- shaders/fragment_normals.glsl | 2 +- shaders/geometry.glsl | 2 +- shaders/geometry_gradient.glsl | 2 +- shaders/geometry_normals.glsl | 2 +- shaders/vertex.glsl | 2 +- shaders/vertex_gradient.glsl | 2 +- shaders/vertex_normals.glsl | 2 +- src/che_obj.cpp | 1 + src/che_ply.cpp | 1 + src/mdict/mdict.cpp | 5 ++--- src/viewer/viewer.cpp | 2 +- 16 files changed, 18 insertions(+), 16 deletions(-) diff --git a/include/che_fill_hole.h b/include/che_fill_hole.h index f45af443..a98f1b76 100644 --- a/include/che_fill_hole.h +++ b/include/che_fill_hole.h @@ -4,6 +4,8 @@ #include "che.h" #include "include_arma.h" +#include + // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/include.h b/include/include.h index c9dc2b98..2ce9952d 100644 --- a/include/include.h +++ b/include/include.h @@ -3,7 +3,6 @@ #include "config.h" -#include #include #include diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index d7e8b81f..9e24779e 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -19,10 +19,10 @@ struct locval_t { arma::uword i, j; real_t val; - - bool operator < (const locval_t & lc); }; +bool operator < (const locval_t & a, const locval_t & b); + void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 5fcc9ddc..0f1f38fd 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core in vec3 gs_position; in vec3 gs_normal; diff --git a/shaders/fragment_gradient.glsl b/shaders/fragment_gradient.glsl index 2323517d..b8d7b44f 100644 --- a/shaders/fragment_gradient.glsl +++ b/shaders/fragment_gradient.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout(location = 0) out vec4 FragColor; diff --git a/shaders/fragment_normals.glsl b/shaders/fragment_normals.glsl index 2323517d..b8d7b44f 100644 --- a/shaders/fragment_normals.glsl +++ b/shaders/fragment_normals.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout(location = 0) out vec4 FragColor; diff --git a/shaders/geometry.glsl b/shaders/geometry.glsl index ea6ce905..45bac232 100644 --- a/shaders/geometry.glsl +++ b/shaders/geometry.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout(triangles) in; layout(triangle_strip, max_vertices = 3) out; diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index 11f3348f..e5dc68e2 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout (triangles) in; layout (line_strip, max_vertices = 2) out; diff --git a/shaders/geometry_normals.glsl b/shaders/geometry_normals.glsl index bee4df8b..1627d464 100644 --- a/shaders/geometry_normals.glsl +++ b/shaders/geometry_normals.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout (triangles) in; layout (line_strip, max_vertices = 6) out; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 226f7546..b9ad9c44 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl index 798c9218..3b1cafc1 100644 --- a/shaders/vertex_gradient.glsl +++ b/shaders/vertex_gradient.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout (location=0) in vec3 in_position; layout (location=2) in float in_color; diff --git a/shaders/vertex_normals.glsl b/shaders/vertex_normals.glsl index dc959929..d4de84ed 100644 --- a/shaders/vertex_normals.glsl +++ b/shaders/vertex_normals.glsl @@ -1,4 +1,4 @@ -#version 460 core +#version 410 core layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; diff --git a/src/che_obj.cpp b/src/che_obj.cpp index 8d4a8fbd..d1e14629 100644 --- a/src/che_obj.cpp +++ b/src/che_obj.cpp @@ -1,6 +1,7 @@ #include "che_obj.h" #include +#include #include #include #include diff --git a/src/che_ply.cpp b/src/che_ply.cpp index da4a82ea..0b90efd5 100644 --- a/src/che_ply.cpp +++ b/src/che_ply.cpp @@ -1,6 +1,7 @@ #include "che_ply.h" #include +#include #include #include #include diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 14a14fda..00866e38 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -16,10 +16,9 @@ const real_t sigma = 0.001; // SPARSE -bool locval_t::operator < (const locval_t & lc) +bool operator < (const locval_t & a, const locval_t & b) { - if(i == lc.i) return j < lc.j; - return i < lc.i; + return (a.i == b.i) ? a.j < b.j : a.i < b.i; } std::ostream & operator << (std::ostream & os, const locval_t & lc) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 4cbbd98e..3ef9ad73 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -228,7 +228,7 @@ void viewer::init_imgui() ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); - ImGui_ImplOpenGL3_Init("#version 460"); + ImGui_ImplOpenGL3_Init("#version 410"); } void viewer::init_menus() From 8bff105774661866c0f3bb37b115802c1b65be80 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 24 Feb 2020 18:28:02 +0100 Subject: [PATCH 0176/1018] macos clang compilation --- src/app_viewer.cpp | 1 - src/che.cpp | 2 +- src/dijkstra.cpp | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 366c192b..29e6fa47 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -629,7 +629,6 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); gproshan_input(radio); distance_t radio; cin >> radio; diff --git a/src/che.cpp b/src/che.cpp index cd42d331..ff960527 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -368,7 +368,7 @@ void che::normalize() real_t max_norm = 0; - #pragma omp parallel for reduction(std::max : max_norm) + #pragma omp parallel for reduction(max : max_norm) for(index_t v = 0; v < n_vertices_; v++) { GT[v] -= center; diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index 6a6ddbf5..68e583c5 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -100,7 +100,7 @@ void dijkstra::run(che * shape) if(min_i != NIL) visited[min_i] = true; } - delete visited; + delete [] visited; } From 398cc7cb5a33ae83ecbb065fbe46c3ae08e2927f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 24 Feb 2020 21:13:19 +0100 Subject: [PATCH 0177/1018] review app viewer geodesics gpu calls --- src/app_viewer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 29e6fa47..366c192b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -629,6 +629,7 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); gproshan_input(radio); distance_t radio; cin >> radio; From 3b3d639a20247b3cce6e7de59d2a56ce584bf835 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 24 Feb 2020 23:03:27 +0100 Subject: [PATCH 0178/1018] updating mdict calls --- src/app_viewer.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c8642800..c869805a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -531,7 +531,7 @@ void app_viewer::process_super_resolution(viewer * p_view) cin >> n >> m >> M >> f >> learn; basis * phi = new basis_dct(n); - super_resolution dict(mesh, phi, m, M, f); + super_resolution dict(mesh, phi, m, M, f, learn); dict.execute(); delete phi; @@ -555,17 +555,19 @@ void app_viewer::process_inpaiting(viewer * p_view) cin >> n >> m >> M >> f >> learn >> avg_p >> percentage; basis * phi = new basis_dct(n); - inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); dict.execute(); delete phi; - viewer::mesh().update_colors(&dict[0]); + mesh.update_colors(&dict[0]); - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_mask() +void viewer_process_mask(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t avg_p = 20; size_t percentage; @@ -580,26 +582,28 @@ void viewer_process_mask() //cin >> avg_p >> percentage >> f; cin>> avg_p >> percentage; basis * phi = new basis_dct(n); - inpainting dict(viewer::mesh(), phi, m, M, f, learn, avg_p, percentage); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; - viewer::mesh().update_colors(&dict[0]); - string f_points = tmp_file_path(viewer::mesh()->name_size() + ".points"); + mesh.update_colors(&dict[0]); + string f_points = tmp_file_path(mesh->name_size() + ".points"); a_vec points_out; points_out.load(f_points); for(int i = 0; i< points_out.size(); i++) - viewer::select_vertices.push_back(points_out(i)); + view->select_vertices.push_back(points_out(i)); - viewer::mesh().update_normals(); + mesh.update_normals(); } -void viewer_process_synthesis() +void viewer_process_synthesis(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); size_t n; // dct size_t m, M; @@ -916,8 +920,8 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) b = mesh->gt_vt(prev(he)) - mesh->gt(v); g += acos((a,b) / (*a * *b)); } - //gv(v) = (2 * M_PI - g) / viewer::mesh()->area_vertex(v); - gv(v) = viewer::mesh()->mean_curvature(v); + //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); + gv(v) = mesh->mean_curvature(v); g_max = max(g_max, gv(v)); g_min = min(g_min, gv(v)); @@ -929,7 +933,7 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) gproshan_log_var(g_max); #pragma omp parallel for - for(index_t v = 0; v < viewer::mesh().n_vertices(); v++) + for(index_t v = 0; v < mesh.n_vertices(); v++) gv(v) = (gv(v) + g_min) / g; real_t gm = mean(gv); From 8d2aab6e6e3af390011b1a684b5a9c2b79f701ea Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Feb 2020 11:46:58 +0100 Subject: [PATCH 0179/1018] add missing mdict calls --- include/app_viewer.h | 2 ++ src/app_viewer.cpp | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 07c8055e..b6c262d0 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -68,6 +68,8 @@ class app_viewer : public viewer static void process_super_resolution(viewer * p_view); static void process_inpaiting(viewer * p_view); static void process_iterative_inpaiting(viewer * p_view); + static void process_mask(viewer * p_view); + static void process_synthesis(viewer * p_view); static void process_functional_maps(viewer * p_view); static void process_gps(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c869805a..494ead64 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -85,6 +85,8 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_D, {"D", "MDICT Denoising", process_denoising}); add_process(GLFW_KEY_A, {"A", "MDICT Super Resolution", process_super_resolution}); add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); + add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); + add_process(GLFW_KEY_F14, {"F14", "MDICT Synthesis", process_synthesis}); // add_process('A', "IT Inpainting", process_iterative_inpaiting); sub_menus.push_back("Signatures"); @@ -563,7 +565,7 @@ void app_viewer::process_inpaiting(viewer * p_view) mesh.update_normals(); } -void viewer_process_mask(viewer * p_view) +void app_viewer::process_mask(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -582,13 +584,13 @@ void viewer_process_mask(viewer * p_view) //cin >> avg_p >> percentage >> f; cin>> avg_p >> percentage; basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; mesh.update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + ".points"); + string f_points = tmp_file_path(mesh->name_size() + ".points"); a_vec points_out; points_out.load(f_points); for(int i = 0; i< points_out.size(); i++) @@ -597,9 +599,7 @@ void viewer_process_mask(viewer * p_view) mesh.update_normals(); } - - -void viewer_process_synthesis(viewer * p_view) +void app_viewer::process_synthesis(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -621,7 +621,6 @@ void viewer_process_synthesis(viewer * p_view) mesh.update_normals(); } - void app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); From 2bf6809e28bada89afbc950c64436cb94cb798c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Feb 2020 12:13:13 +0100 Subject: [PATCH 0180/1018] single precision config, opengl 4.1 (macos support) --- include/config.h | 2 +- src/viewer/viewer.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/config.h b/include/config.h index a0133ae5..fe8bc38e 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -//#define SINGLE_P +#define SINGLE_P // print log messages #define LOG diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 3ef9ad73..32e7059d 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -193,8 +193,8 @@ void viewer::init_gl() glfwInit(); #ifdef __APPLE__ - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif From 67539321dd55c0062f924bf7bfc6d6097420c9d5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Feb 2020 15:55:21 +0100 Subject: [PATCH 0181/1018] fix no optix require --- CMakeLists.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1750ebc1..54deb21e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) find_package(GLEW 2 REQUIRED) find_package(glfw3 3 REQUIRED) +find_package(glm REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) @@ -71,7 +72,7 @@ FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(imgui STATIC ${imgui_sources}) set(ptx_code "") -if(CUDA_FOUND) +if(OptiX_INCLUDE) cuda_compile_ptx(ptx_files "${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu") list(GET ptx_files 0 ptx_file) set(ptx_code ptx_code.c) @@ -79,7 +80,7 @@ if(CUDA_FOUND) COMMAND bin2c --const --padd 0 --type char --name ptx_code ${ptx_file} > ${ptx_code} DEPENDS ${ptx_file} ) -endif(CUDA_FOUND) +endif(OptiX_INCLUDE) FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) add_library(gproshan_cpp STATIC ${cpp_sources} ${ptx_code}) From 28ddf381087627e132e43153d2af2e804f19bb18 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 25 Feb 2020 16:09:26 +0100 Subject: [PATCH 0182/1018] controling angle variance --- src/mdict/inpainting.cpp | 3 +- src/mdict/patch.cpp | 74 ++++++---------------------------------- 2 files changed, 13 insertions(+), 64 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index de01bde4..c3182908 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -335,7 +335,7 @@ void inpainting::init_radial_feature_patches() const vertex & v_seed = mesh->gt(seeds[j]); // 0.7 coverage parameter - if( *(v_patch - v_seed) < 0.7 * radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.5 * radios[j] ) // radio of each patch found = true; j++; } @@ -508,6 +508,7 @@ void inpainting::init_radial_feature_patches() p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); + if(s == 90) gproshan_debug_var(p.vertices[0]); } gproshan_log(radial patches are ready); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 30f40682..7a52552f 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -74,6 +74,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in euc_radio = -INFINITY; double angle; double sum_angle = 0; + double delta = 1; //gproshan_debug_var(v); for(size_t i=1; inormal(indexes[i]) ) ) ; //if( angle < PI/2.5 && (sum_angle/vertices.size()) + angle <= PI/2.2) - if( angle < PI/2.5 && (sum_angle) <= 1.2* PI) + //if( angle < PI/2.5 && (sum_angle) <= delta * PI) + if( angle < PI/2.5 && (sum_angle) <= delta * PI) { if(*p > radio) @@ -102,7 +104,9 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); sum_angle += angle; + delta += 0.001; + //if(i%7 == 0) sum_angle = 0; } else { @@ -110,56 +114,11 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } //gproshan_debug_var(sum_angle); } +/* gproshan_debug_var(PI/2.5); + gproshan_debug_var(sum_angle); + gproshan_debug_var(PI/(delta+0.05)*(vertices.size()-1));*/ delete indexes; -/* - if(vertices.size() == geo.n_sorted_index() ) - { - //gproshan_debug_var(vertices.size()); - geodesics geo2(mesh, {v}, geodesics::FM, NULL, false, 0, 3*radio_); - indexes = new index_t[geo2.n_sorted_index()]; - geo2.copy_sorted_index(indexes, geo2.n_sorted_index()); - - for(size_t i=geo.n_sorted_index(); iget_vertex(indexes[i]); - c = mesh->get_vertex(v); // central vertices - - p = p - c ; - p = p - ((p,n)*n); - //if(*p <= radio ) - //gproshan_debug_var(indexes[i]); - angle = acos( (n, mesh->normal(indexes[i]) ) ) ; - if( angle < PI/2.5 && (sum_angle/vertices.size()) <= 1.5*PI/( vertices.size() )) - { - - if(*p > radio) - { - radio = *p; - } - //compute euclidean radio - p = mesh->get_vertex(indexes[i]); - if(*(p - c) > euc_radio) - euc_radio = *(p - c); - //gproshan_debug_var(euc_radio); - vertices.push_back(indexes[i]); - //gproshan_debug_var(geo[indexes[i]]); - sum_angle += angle; - - } - else - { - break; - } - //gproshan_debug_var(sum_angle); - } - delete indexes; - }*/ - //gproshan_debug_var(sum_angle/vertices.size()); - //gproshan_debug_var(PI/10); - -// vertices = std::move(_vertices); } @@ -252,7 +211,7 @@ void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normal vn = T.col(2);// normal at the center n.x = vn(0); n.y = vn(1); n.z = vn(2); - for(int i=1; i < n_vertices; i++) + for(size_t i=1; i < n_vertices; i++) { vertex p = mesh->get_vertex(indexes[i]); vertex c = mesh->get_vertex(v); // central vertices @@ -600,25 +559,14 @@ void patch::compute_avg_distance() { avg_dist = INFINITY; vector distances; - for(int i = 0; i < vertices.size(); i++) - for(int j = i+1; j < vertices.size(); j++) + for(size_t i = 0; i < vertices.size(); i++) + for(size_t j = i+1; j < vertices.size(); j++) { a_vec a = xyz.col(i); a_vec b = xyz.col(j); a(2) = 0; b(2) = 0; distances.push_back(norm(a - b)); - - /* if(avg_dist > norm(a - b)) - { - avg_dist = norm(a - b); - } - /*if(approx_equal(a,b,"absdiff", 0.0000001) ) - { - gproshan_debug_var(i); - gproshan_debug_var(j); - } */ - } sort(distances.begin(), distances.end()); size_t n_elem = distances.size(); From 9874c3ec6629bb360604e2d617886373de63e945 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Feb 2020 22:56:48 +0100 Subject: [PATCH 0183/1018] adding che_sphere class --- include/app_viewer.h | 1 + include/che_sphere.h | 26 ++++++++++++ src/app_viewer.cpp | 5 ++- src/che_sphere.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 3 +- 5 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 include/che_sphere.h create mode 100644 src/che_sphere.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index 9f27a292..24ca2615 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -7,6 +7,7 @@ #include "che_obj.h" #include "che_ply.h" #include "che_img.h" +#include "che_sphere.h" #include "laplacian.h" #include "che_off.h" #include "dijkstra.h" diff --git a/include/che_sphere.h b/include/che_sphere.h new file mode 100644 index 00000000..84930768 --- /dev/null +++ b/include/che_sphere.h @@ -0,0 +1,26 @@ +#ifndef CHE_SPHERE_H +#define CHE_SPHERE_H + +#include "che.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_sphere : public che +{ + private: + real_t radio; + + public: + che_sphere(const real_t & r = 1, const size_t & n = 10); + che_sphere(const che_sphere & mesh); + virtual ~che_sphere(); +}; + + +} // namespace gproshan + +#endif // CHE_SPHERE_H + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 366c192b..1fe5535f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -50,13 +50,14 @@ int app_viewer::main(int nargs, const char ** args) vector meshes; for(int i = 1; i < nargs; i++) meshes.push_back(load_mesh(args[i])); - + + meshes.push_back(new che_sphere); TOC(time) gproshan_log_var(sizeof(real_t)); gproshan_log_var(time); - //init mesher + //init meshes add_mesh(meshes); sub_menus.push_back("Fairing"); diff --git a/src/che_sphere.cpp b/src/che_sphere.cpp new file mode 100644 index 00000000..1be355d0 --- /dev/null +++ b/src/che_sphere.cpp @@ -0,0 +1,92 @@ +#include "che_sphere.h" + +#include +#include +#include +#include + +using namespace std; + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_sphere::che_sphere(const real_t & r, const size_t & n) +{ + std::vector vertices; + std::vector faces; + + const real_t delta = M_PI / n; + for(real_t phi = 0; phi < 2 * M_PI; phi += delta) + for(real_t theta = delta; theta < M_PI; theta += delta) + vertices.push_back({r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)}); + + vertices.push_back({0, 0, r}); + vertices.push_back({0, 0, -r}); + + size_t v, cols = n - 1; + + for(index_t i = 0; i < 2 * n - 1; i++) + { + for(index_t j = 0; j < cols - 1; j++) + { + v = i * cols + j; + + faces.push_back(v); + faces.push_back(v + 1); + faces.push_back(v + cols); + + faces.push_back(v + cols); + faces.push_back(v + 1); + faces.push_back(v + cols + 1); + } + + v = i * cols; + faces.push_back(vertices.size() - 2); + faces.push_back(v); + faces.push_back(v + cols); + + v = (i + 1) * cols - 1; + faces.push_back(vertices.size() - 1); + faces.push_back(v + cols); + faces.push_back(v); + } + + for(index_t j = 0; j < cols - 1; j++) + { + v = (2 * n - 1) * cols + j; + + faces.push_back(v + 1); + faces.push_back(j); + faces.push_back(v); + + faces.push_back(j + 1); + faces.push_back(j); + faces.push_back(v + 1); + } + + v = (2 * n - 1) * cols; + faces.push_back(vertices.size() - 2); + faces.push_back(v); + faces.push_back(0); + + v = (2 * n) * cols - 1; + faces.push_back(vertices.size() - 1); + faces.push_back(cols - 1); + faces.push_back(v); + + init(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); +} + +che_sphere::che_sphere(const che_sphere & mesh): che(mesh) +{ +} + +che_sphere::~che_sphere() +{ +} + + +} // namespace gproshan + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 32e7059d..85d95fc9 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -159,6 +159,7 @@ bool viewer::run() glfwSwapBuffers(window); glfwPollEvents(); + //glfwWaitEvents(); } return true; @@ -209,7 +210,7 @@ void viewer::init_gl() glfwSetScrollCallback(window, scroll_callback); glfwMakeContextCurrent(window); - glfwSwapInterval(0); + glfwSwapInterval(1); glewInit(); From 6fdb113ef6b0ff12ebe13f8e24f8943199578ea7 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 26 Feb 2020 11:01:36 +0100 Subject: [PATCH 0184/1018] fixing single precision --- include/config.h | 2 +- src/mdict/inpainting.cpp | 2 -- src/mdict/patch.cpp | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/include/config.h b/include/config.h index fe8bc38e..a0133ae5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -#define SINGLE_P +//#define SINGLE_P // print log messages #define LOG diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index c3182908..9d742b5e 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -508,8 +508,6 @@ void inpainting::init_radial_feature_patches() p.compute_avg_distance(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - if(s == 90) gproshan_debug_var(p.vertices[0]); - } gproshan_log(radial patches are ready); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 7a52552f..fb4dc2b5 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -104,8 +104,8 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); sum_angle += angle; - delta += 0.001; - + delta += 0.01; + // sharp meshes 0.001 //if(i%7 == 0) sum_angle = 0; } else From 8bddcc4383ab1147aa675f36a35c9841d059c052 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 26 Feb 2020 13:11:05 +0100 Subject: [PATCH 0185/1018] draw sphere on selected vertices (glDrawElementsInstanced) --- include/che_sphere.h | 3 -- include/viewer/che_viewer.h | 14 +++--- include/viewer/viewer.h | 4 ++ shaders/fragment.glsl | 4 +- shaders/fragment_gradient.glsl | 4 +- shaders/fragment_normals.glsl | 4 +- shaders/fragment_sphere.glsl | 45 ++++++++++++++++++ shaders/vertex.glsl | 2 +- shaders/vertex_sphere.glsl | 20 ++++++++ src/app_viewer.cpp | 15 +++--- src/che_sphere.cpp | 2 + src/viewer/che_viewer.cpp | 87 +++++++++++++++++++++------------- src/viewer/viewer.cpp | 41 ++++++++-------- 13 files changed, 166 insertions(+), 79 deletions(-) create mode 100644 shaders/fragment_sphere.glsl create mode 100644 shaders/vertex_sphere.glsl diff --git a/include/che_sphere.h b/include/che_sphere.h index 84930768..d45a90b2 100644 --- a/include/che_sphere.h +++ b/include/che_sphere.h @@ -10,9 +10,6 @@ namespace gproshan { class che_sphere : public che { - private: - real_t radio; - public: che_sphere(const real_t & r = 1, const size_t & n = 10); che_sphere(const che_sphere & mesh); diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index fcceb620..cf319e5f 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -26,15 +26,17 @@ class che_viewer { protected: che * mesh; - size_t _n_vertices; // current number of vertices - bool _invert_orientation; + + size_t n_instances; + size_t n_vertices; // current number of vertices + bool invert_normals; vertex v_translate; vertex * normals; color_t * colors; GLuint vao; - GLuint vbo[4]; + GLuint vbo[5]; public: int vx, vy; ///< viewport positions. @@ -42,19 +44,19 @@ class che_viewer std::vector selected; public: - che_viewer(); + che_viewer(const size_t & n = 0); virtual ~che_viewer(); che *& operator -> (); operator che *& (); - void init(che * _mesh); + void init(che * _mesh, const bool & normalize = true); void reload(); void update(); void update_vbo(); void update_normals(); void update_colors(const color_t *const c = nullptr); + void update_instances_translations(const std::vector & translations); void draw(); - const size_t & n_vertices() const; color_t & color(const index_t & v); vertex & normal(const index_t & v); vertex *& normals_ptr(); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 91e3273d..3d101531 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -134,6 +134,10 @@ class viewer float bgc; std::map processes; + + che_viewer sphere; + std::vector sphere_translations; + shader shader_sphere; public: diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 0f1f38fd..d0655873 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -6,7 +6,7 @@ in float gs_color; noperspective in vec3 edge_dist; -layout(location = 0) out vec4 FragColor; +layout(location = 0) out vec4 frag_color; uniform vec3 eye; uniform vec3 light; @@ -107,6 +107,6 @@ void main() } color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; - FragColor = vec4(color, 1); + frag_color = vec4(color, 1); } diff --git a/shaders/fragment_gradient.glsl b/shaders/fragment_gradient.glsl index b8d7b44f..888afba2 100644 --- a/shaders/fragment_gradient.glsl +++ b/shaders/fragment_gradient.glsl @@ -1,9 +1,9 @@ #version 410 core -layout(location = 0) out vec4 FragColor; +layout(location = 0) out vec4 frag_color; void main() { - FragColor = vec4(0.6, 1.0, 0.2, 1.); + frag_color = vec4(0.6, 1.0, 0.2, 1.); } diff --git a/shaders/fragment_normals.glsl b/shaders/fragment_normals.glsl index b8d7b44f..888afba2 100644 --- a/shaders/fragment_normals.glsl +++ b/shaders/fragment_normals.glsl @@ -1,9 +1,9 @@ #version 410 core -layout(location = 0) out vec4 FragColor; +layout(location = 0) out vec4 frag_color; void main() { - FragColor = vec4(0.6, 1.0, 0.2, 1.); + frag_color = vec4(0.6, 1.0, 0.2, 1.); } diff --git a/shaders/fragment_sphere.glsl b/shaders/fragment_sphere.glsl new file mode 100644 index 00000000..30f394a5 --- /dev/null +++ b/shaders/fragment_sphere.glsl @@ -0,0 +1,45 @@ +#version 410 core + +in vec3 vs_position; +in vec3 vs_normal; + +layout(location = 0) out vec4 frag_color; + +uniform vec3 eye; +uniform vec3 light; + +float diffuse(vec3 N, vec3 L) +{ + return max(0, dot(N, L)); +} + +float specular(vec3 N, vec3 L, vec3 E) +{ + const float shininess = 4; + vec3 R = 2 * dot(L, N) * N - L; + + return pow(max(0, dot(R, E)), shininess); +} + +float fresnel(vec3 N, vec3 E) +{ + const float sharpness = 10; + float NE = max(0, dot(N, E)); + + return pow(sqrt( 1. - NE * NE ), sharpness); +} + +void main() +{ + vec3 color = vec3(0, 1, 0); + + vec3 N = normalize(vs_normal); + vec3 L = normalize(light - vs_position); + vec3 E = normalize(eye - vs_position); + vec3 R = 2 * dot(L, N) * N - L; + vec3 one = vec3(1); + + color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + frag_color = vec4(color, 1); +} + diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index b9ad9c44..79551501 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -17,6 +17,6 @@ void main() vs_normal = in_normal; vs_color = in_color; - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); } diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl new file mode 100644 index 00000000..74b518a4 --- /dev/null +++ b/shaders/vertex_sphere.glsl @@ -0,0 +1,20 @@ +#version 410 core + +layout (location=0) in vec3 in_position; +layout (location=1) in vec3 in_normal; +layout (location=4) in vec3 in_translation; + +out vec3 vs_position; +out vec3 vs_normal; + +uniform mat4 model_view_mat; +uniform mat4 proj_mat; + +void main() +{ + vs_position = in_position + in_translation; + vs_normal = in_normal; + + gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1.); +} + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 1fe5535f..0dd89c9f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -51,7 +51,6 @@ int app_viewer::main(int nargs, const char ** args) for(int i = 1; i < nargs; i++) meshes.push_back(load_mesh(args[i])); - meshes.push_back(new che_sphere); TOC(time) gproshan_log_var(sizeof(real_t)); @@ -764,7 +763,7 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) if(!view->select_vertices.size()) view->select_vertices.push_back(0); - if(view->dist && view->n_dist != mesh.n_vertices()) + if(view->dist && view->n_dist != mesh->n_vertices()) { delete [] view->dist; view->n_dist = 0; @@ -773,7 +772,7 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) if(!view->dist) { - view->n_dist = mesh.n_vertices(); + view->n_dist = mesh->n_vertices(); view->dist = new distance_t[view->n_dist]; } @@ -811,7 +810,7 @@ void app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t old_n_vertices, n_vertices = mesh.n_vertices(); + size_t old_n_vertices, n_vertices = mesh->n_vertices(); size_t n_holes = mesh->n_borders(); vector * border_vertices; @@ -843,10 +842,10 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; - a_vec gv(mesh.n_vertices()); + a_vec gv(mesh->n_vertices()); #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) - for(index_t v = 0; v < mesh.n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) { g = 0; for_star(he, mesh, v) @@ -863,7 +862,7 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) g = g_max - g_min; #pragma omp parallel for - for(index_t v = 0; v < mesh.n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) gv(v) = (gv(v) - g_min) / g; real_t gm = mean(gv); @@ -880,7 +879,7 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) }; #pragma omp parallel for - for(index_t v = 0; v < mesh.n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) mesh.color(v) = f(gv(v)); } diff --git a/src/che_sphere.cpp b/src/che_sphere.cpp index 1be355d0..90782a76 100644 --- a/src/che_sphere.cpp +++ b/src/che_sphere.cpp @@ -14,6 +14,8 @@ namespace gproshan { che_sphere::che_sphere(const real_t & r, const size_t & n) { + filename_ = "sphere"; + std::vector vertices; std::vector faces; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 60293c2e..30022379 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -11,10 +11,10 @@ using namespace std; namespace gproshan { -che_viewer::che_viewer() +che_viewer::che_viewer(const size_t & n): n_instances(n) { mesh = nullptr; - _n_vertices = 0; + n_vertices = 0; normals = nullptr; colors = nullptr; @@ -24,9 +24,9 @@ che_viewer::che_viewer() che_viewer::~che_viewer() { - if(_n_vertices) return; + if(n_vertices) return; - glDeleteBuffers(4, vbo); + glDeleteBuffers(5, vbo); glDeleteVertexArrays(1, &vao); if(normals) delete [] normals; @@ -43,23 +43,25 @@ che_viewer::operator che *& () return mesh; } -void che_viewer::init(che * _mesh) +void che_viewer::init(che * _mesh, const bool & normalize) { - _n_vertices = 0; + n_vertices = 0; mesh = _mesh; - mesh->normalize(); + + if(normalize) + mesh->normalize(); - _invert_orientation = false; + invert_normals = false; glGenVertexArrays(1, &vao); - glGenBuffers(4, vbo); + glGenBuffers(5, vbo); update(); } void che_viewer::reload() { - _n_vertices = 0; + n_vertices = 0; mesh->reload(); mesh->normalize(); update(); @@ -72,14 +74,14 @@ void che_viewer::update() { assert(mesh != nullptr); - if(_n_vertices != mesh->n_vertices()) + if(n_vertices != mesh->n_vertices()) { if(normals) delete [] normals; if(colors) delete [] colors; - _n_vertices = mesh->n_vertices(); - normals = new vertex[_n_vertices]; - colors = new color_t[_n_vertices]; + n_vertices = mesh->n_vertices(); + normals = new vertex[n_vertices]; + colors = new color_t[n_vertices]; update_normals(); update_colors(); @@ -96,26 +98,26 @@ void che_viewer::update_vbo() // 0 VERTEX glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glBufferData(GL_ARRAY_BUFFER, _n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glBufferData(GL_ARRAY_BUFFER, _n_vertices * sizeof(vertex), normals, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(vertex), normals, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, _n_vertices * sizeof(real_t), colors, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(real_t), colors, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - // INDEXES + // 3 INDEXES if(mesh->n_faces()) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); @@ -124,21 +126,23 @@ void che_viewer::update_vbo() } else { - index_t * indices = new index_t[_n_vertices]; - iota(indices, indices + _n_vertices, 0); + index_t * indices = new index_t[n_vertices]; + iota(indices, indices + n_vertices, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, _n_vertices * sizeof(index_t), indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, n_vertices * sizeof(index_t), indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); delete [] indices; } + + glBindVertexArray(0); } void che_viewer::update_normals() { #pragma omp parallel for - for(index_t v = 0; v < _n_vertices; v++) - if(_invert_orientation) + for(index_t v = 0; v < n_vertices; v++) + if(invert_normals) normals[v] = -mesh->normal(v); else normals[v] = mesh->normal(v); @@ -149,7 +153,7 @@ void che_viewer::update_colors(const color_t *const c) if(!c) { #pragma omp parallel for - for(index_t v = 0; v < _n_vertices; v++) + for(index_t v = 0; v < n_vertices; v++) colors[v] = COLOR; return; @@ -158,20 +162,42 @@ void che_viewer::update_colors(const color_t *const c) distance_t max_c = 0; #pragma omp parallel for reduction(max: max_c) - for(index_t v = 0; v < _n_vertices; v++) + for(index_t v = 0; v < n_vertices; v++) if(c[v] < INFINITY) max_c = max(c[v], max_c); #pragma omp parallel for - for(index_t v = 0; v < _n_vertices; v++) + for(index_t v = 0; v < n_vertices; v++) colors[v] = c[v] / max_c; } +void che_viewer::update_instances_translations(const vector & translations) +{ + n_instances = translations.size(); + if(!n_instances) return; + + glBindVertexArray(vao); + + // 4 INSTANCES (translations) + glBindBuffer(GL_ARRAY_BUFFER, vbo[4]); + glBufferData(GL_ARRAY_BUFFER, n_instances * sizeof(vertex), translations.data(), GL_STATIC_DRAW); + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glVertexAttribDivisor(3, 1); + + glBindVertexArray(0); +} + void che_viewer::draw() { glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); - + + if(n_instances) + glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0, n_instances); + if(mesh->n_faces()) glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); else @@ -181,11 +207,6 @@ void che_viewer::draw() glBindVertexArray(0); } -const size_t & che_viewer::n_vertices() const -{ - return _n_vertices; -} - color_t & che_viewer::color(const index_t & v) { return colors[v]; @@ -212,7 +233,7 @@ void che_viewer::translate(const vertex & p) void che_viewer::invert_orientation() { - _invert_orientation = !_invert_orientation; + invert_normals = !invert_normals; } void che_viewer::log_info() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 85d95fc9..4c356c86 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -15,6 +15,7 @@ #include "che_off.h" #include "che_obj.h" #include "che_ply.h" +#include "che_sphere.h" #include "CImg.h" @@ -62,12 +63,13 @@ viewer::viewer() action = false; init_gl(); - init_imgui(); - init_menus(); - init_glsl(); + init_imgui(); + init_menus(); info_gl(); + + sphere.init(new che_sphere(0.1), false); } viewer::~viewer() @@ -263,6 +265,9 @@ void viewer::init_menus() void viewer::init_glsl() { + shader_sphere.load_vertex("../shaders/vertex_sphere.glsl"); + shader_sphere.load_fragment("../shaders/fragment_sphere.glsl"); + shader_program.load_vertex("../shaders/vertex.glsl"); shader_program.load_geometry("../shaders/geometry.glsl"); shader_program.load_fragment("../shaders/fragment.glsl"); @@ -670,28 +675,20 @@ void viewer::draw_border() void viewer::draw_selected_vertices() { - shader_program.disable(); - glPushAttrib(GL_ALL_ATTRIB_BITS); - - glEnable(GL_COLOR_MATERIAL); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - glColor3f(0., 0.5, 0.5); - -// double h = 0.02 * cam.zoom; - - for(int v: select_vertices) + if(sphere_translations.size() != select_vertices.size()) { - glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); - - glPushMatrix(); - glTranslated(mesh()->gt(v).x, mesh()->gt(v).y, mesh()->gt(v).z); - // //glutSolidSphere(h, 10, 10); - glPopMatrix(); - } + sphere_translations.resize(select_vertices.size()); - glEnd(); + for(index_t i = 0; i < select_vertices.size(); i++) + sphere_translations[i] = mesh()->gt(select_vertices[i]); - glPopAttrib(); + sphere.update_instances_translations(sphere_translations); + } + + shader_sphere.enable(); + if(sphere_translations.size()) + sphere.draw(); + shader_sphere.disable(); } void viewer::pick_vertex(int x, int y) From bbebcafafe7b2bf7bd5aa2a4b0c6cb3c0cd25f9b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 26 Feb 2020 13:46:05 +0100 Subject: [PATCH 0186/1018] mark selected vertices with spheres --- shaders/fragment_sphere.glsl | 2 +- shaders/vertex_sphere.glsl | 6 +++--- src/viewer/che_viewer.cpp | 3 +-- src/viewer/viewer.cpp | 20 ++++++++++++++------ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/shaders/fragment_sphere.glsl b/shaders/fragment_sphere.glsl index 30f394a5..e5cab4ff 100644 --- a/shaders/fragment_sphere.glsl +++ b/shaders/fragment_sphere.glsl @@ -31,7 +31,7 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color = vec3(0, 1, 0); + vec3 color = vec3(1, 0, 0); vec3 N = normalize(vs_normal); vec3 L = normalize(light - vs_position); diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index 74b518a4..715ff270 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -2,7 +2,7 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; -layout (location=4) in vec3 in_translation; +layout (location=3) in vec3 in_translation; out vec3 vs_position; out vec3 vs_normal; @@ -14,7 +14,7 @@ void main() { vs_position = in_position + in_translation; vs_normal = in_normal; - - gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1.); + + gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1); } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 30022379..b9d66bdb 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -197,8 +197,7 @@ void che_viewer::draw() if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0, n_instances); - - if(mesh->n_faces()) + else if(mesh->n_faces()) glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); else glDrawElements(GL_POINTS, mesh->n_vertices(), GL_UNSIGNED_INT, 0); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 4c356c86..d20169fb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -69,7 +69,7 @@ viewer::viewer() info_gl(); - sphere.init(new che_sphere(0.1), false); + sphere.init(new che_sphere(0.01), false); } viewer::~viewer() @@ -552,6 +552,14 @@ void viewer::render_gl() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + shader_sphere.enable(); + + glUniform3f(glGetUniformLocation(shader_sphere, "eye"), eye[1], eye[2], eye[3]); + glUniform3f(glGetUniformLocation(shader_sphere, "light"), light[1], light[2], light[3]); + glUniformMatrix4fv(glGetUniformLocation(shader_sphere, "model_view_mat"), 1, 0, &view_mat[0][0]); + glUniformMatrix4fv(glGetUniformLocation(shader_sphere, "proj_mat"), 1, 0, &proj_mat[0][0]); + + shader_program.enable(); glUniform3f(glGetUniformLocation(shader_program, "eye"), eye[1], eye[2], eye[3]); @@ -561,30 +569,29 @@ void viewer::render_gl() glUniform1i(glGetUniformLocation(shader_program, "render_wireframe"), render_wireframe_fill); glUniformMatrix4fv(glGetUniformLocation(shader_program, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_program, "proj_mat"), 1, 0, &proj_mat[0][0]); - + draw_scene(); if(render_normal_field) { - shader_normals.enable(); - glUniform1f(glGetUniformLocation(shader_normals, "length"), mesh().factor); glUniformMatrix4fv(glGetUniformLocation(shader_normals, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_normals, "proj_mat"), 1, 0, &proj_mat[0][0]); + shader_normals.enable(); draw_scene(); } if(render_gradient_field) { - shader_gradient.enable(); glUniform1f(glGetUniformLocation(shader_gradient, "length"), mesh().factor); glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "proj_mat"), 1, 0, &proj_mat[0][0]); + shader_gradient.enable(); draw_scene(); } } @@ -645,7 +652,8 @@ void viewer::draw_scene() { draw_polygons(); - if(render_border) draw_border(); + if(render_border) + draw_border(); draw_selected_vertices(); } From d1dc2ebf898ff66da8564b1e978109bb91c24dd2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 26 Feb 2020 14:11:32 +0100 Subject: [PATCH 0187/1018] select border vertices --- src/viewer/viewer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d20169fb..9f9dc7a5 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -246,20 +246,21 @@ void viewer::init_menus() add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); sub_menus.push_back("Render"); - add_process(GLFW_KEY_F5, {"F5", "Render Wireframe", set_render_wireframe}); - add_process(GLFW_KEY_F6, {"F6", "Render Wireframe Fill", set_render_wireframe_fill}); - add_process(GLFW_KEY_F7, {"F7", "Render GL", set_render_gl}); - add_process(GLFW_KEY_F8, {"F8", "Render Embree", set_render_embree}); - add_process(GLFW_KEY_F9, {"F9", "Render OptiX", set_render_optix}); - add_process(GLFW_KEY_F10, {"F10", "Raycasting", raycasting}); - add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); + add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); + add_process(GLFW_KEY_F7, {"F7", "Render Wireframe Fill", set_render_wireframe_fill}); + add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); + add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); + add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); + add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); sub_menus.push_back("Mesh"); add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_render_flat}); + add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradient_field}); add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normal_field}); + add_process(GLFW_KEY_F5, {"F5", "Select Border Vertices", set_render_border}); add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); } From 64401ddc5e4426ba20d7a68209f68bbcf26d7e0e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 26 Feb 2020 14:28:47 +0100 Subject: [PATCH 0188/1018] fix shader uniform params --- src/viewer/viewer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 9f9dc7a5..62e0d5fe 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -576,23 +576,24 @@ void viewer::render_gl() if(render_normal_field) { + shader_normals.enable(); + glUniform1f(glGetUniformLocation(shader_normals, "length"), mesh().factor); glUniformMatrix4fv(glGetUniformLocation(shader_normals, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_normals, "proj_mat"), 1, 0, &proj_mat[0][0]); - shader_normals.enable(); draw_scene(); } if(render_gradient_field) { + shader_gradient.enable(); glUniform1f(glGetUniformLocation(shader_gradient, "length"), mesh().factor); glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "model_view_mat"), 1, 0, &view_mat[0][0]); glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "proj_mat"), 1, 0, &proj_mat[0][0]); - shader_gradient.enable(); draw_scene(); } } From a713c7b71bfb0991b3ab01c8018610ea1b770fd9 Mon Sep 17 00:00:00 2001 From: lizeth Date: Thu, 27 Feb 2020 10:38:41 +0100 Subject: [PATCH 0189/1018] fixing error viewer debug command --- src/app_viewer.cpp | 2 +- src/mdict/dictionary.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 4e332dba..38fe3537 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -949,7 +949,7 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) }; #pragma omp parallel for - for(index_t v = 0; v < mesh.n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) mesh.color(v) = 2 * atan(gv(v) * 10) / M_PI; } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index cf0ef13f..a18aa1af 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -204,6 +204,7 @@ void dictionary::load_features(vector & v_feat, size_t & featsize) inp.clear(ios::failbit); // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d + //cmake -DCMAKE_BUILD_TYPE=Debug .. string command = "../../Harris3D-Cpp/harris3d " + tmp_file_path(mesh->name()) + ".off" + " ../tmp/example.prop"; gproshan_debug_var(command); From 0b142f3e2c7874a3f93e4d0ebe0985f15f85e627 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Feb 2020 12:51:32 +0100 Subject: [PATCH 0190/1018] add viewer frame --- include/viewer/che_viewer.h | 2 +- include/viewer/frame.h | 33 +++++++++++++++++++++ include/viewer/viewer.h | 12 +++++--- src/viewer/che_viewer.cpp | 3 +- src/viewer/frame.cpp | 57 +++++++++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 21 +++++++------- 6 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 include/viewer/frame.h create mode 100644 src/viewer/frame.cpp diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index cf319e5f..b600a7cd 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -44,7 +44,7 @@ class che_viewer std::vector selected; public: - che_viewer(const size_t & n = 0); + che_viewer(); virtual ~che_viewer(); che *& operator -> (); operator che *& (); diff --git a/include/viewer/frame.h b/include/viewer/frame.h new file mode 100644 index 00000000..c290b7b0 --- /dev/null +++ b/include/viewer/frame.h @@ -0,0 +1,33 @@ +#ifndef VIEWER_FRAME_H +#define VIEWER_FRAME_H + + +#include "shader.h" + +#include "include_opengl.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class frame +{ + private: + GLuint render_tex; + GLuint vao, vbo; + + shader program; + + public: + frame(); + ~frame(); + + void display(void * buffer); +}; + + +} // namespace gproshan + +#endif // VIEWER_FRAME_H + diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 3d101531..13b5921a 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -8,6 +8,7 @@ #include "camera.h" #include "shader.h" +#include "frame.h" #include "che_viewer.h" #include "include_opengl.h" @@ -16,6 +17,7 @@ #include "imgui_impl_glfw.h" #include "imgui_impl_opengl3.h" + #ifdef GPROSHAN_EMBREE #include "rt_embree.h" #endif // GPROSHAN_EMBREE @@ -88,15 +90,15 @@ class viewer GLFWwindow * window; + int viewport_width; + int viewport_height; shader shader_program; shader shader_normals; shader shader_gradient; - shader shader_edges; - camera cam; + shader shader_frame; - int viewport_width; - int viewport_height; + camera cam; quaternion eye; quaternion center; @@ -114,6 +116,8 @@ class viewer index_t render_opt; + frame * render_frame; + #ifdef GPROSHAN_EMBREE rt::embree * rt_embree; #endif // GPROSHAN_EMBREE diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index b9d66bdb..b9b7837e 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -11,10 +11,11 @@ using namespace std; namespace gproshan { -che_viewer::che_viewer(const size_t & n): n_instances(n) +che_viewer::che_viewer() { mesh = nullptr; n_vertices = 0; + n_instances = 0; normals = nullptr; colors = nullptr; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp new file mode 100644 index 00000000..dc831f9f --- /dev/null +++ b/src/viewer/frame.cpp @@ -0,0 +1,57 @@ +#include "frame.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +frame::frame() +{ + render_tex = 0; + vbo = 0; + + program.load_vertex("../shaders/vertex_frame.glsl"); + program.load_fragment("../shaders/fragment_frame.glsl"); + + + const GLfloat vertices[] = { + -1, -1, 0, + 1, -1, 0, + -1, 1, 0, + -1, 1, 0, + 1, -1, 0, + 1, 1, 0, + }; + + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +frame::~frame() +{ + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &vao); +} + +void frame::display(void * buffer) +{ + program.enable(); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + program.disable(); +} + + +} // namespace gproshan + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 62e0d5fe..c1654869 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -17,7 +17,7 @@ #include "che_ply.h" #include "che_sphere.h" -#include "CImg.h" +#include using namespace cimg_library; @@ -41,6 +41,7 @@ viewer::viewer() n_meshes = current = 0; render_opt = 0; + render_frame = nullptr; #ifdef GPROSHAN_EMBREE rt_embree = nullptr; @@ -116,6 +117,7 @@ bool viewer::run() ); // RENDER + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { case 1: render_embree(); break; @@ -550,9 +552,6 @@ void viewer::raycasting(viewer * view) void viewer::render_gl() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - shader_sphere.enable(); glUniform3f(glGetUniformLocation(shader_sphere, "eye"), eye[1], eye[2], eye[3]); @@ -602,7 +601,7 @@ void viewer::render_embree() { #ifdef GPROSHAN_EMBREE - if(rt_embree == nullptr) + if(!rt_embree) { double time_build_embree; TIC(time_build_embree); @@ -618,9 +617,12 @@ void viewer::render_embree() action = false; - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_embree->img); + if(!render_frame) render_frame = new frame; + + render_frame->display(rt_embree->img); + #endif // GPROSHAN_EMBREE } @@ -628,7 +630,7 @@ void viewer::render_optix() { #ifdef GPROSHAN_OPTIX - if(rt_optix == nullptr) + if(!rt_optix) { double time_build_optix; TIC(time_build_optix); @@ -643,15 +645,14 @@ void viewer::render_optix() view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); action = false; - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_optix->img); #endif // GPROSHAN_OPTIX } void viewer::draw_scene() -{ +{ draw_polygons(); if(render_border) From bf199d0bf133f155ce584fa4c2a9373b743e98c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Feb 2020 15:24:08 +0100 Subject: [PATCH 0191/1018] shader get uniform map --- include/viewer/shader.h | 5 ++++- src/viewer/shader.cpp | 18 ++++++++++++----- src/viewer/viewer.cpp | 44 +++++++++++++++++++---------------------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/include/viewer/shader.h b/include/viewer/shader.h index f7253be3..4c8e9622 100644 --- a/include/viewer/shader.h +++ b/include/viewer/shader.h @@ -2,6 +2,7 @@ #define VIEWER_SHADER_H #include +#include #include "include_opengl.h" @@ -13,17 +14,19 @@ class shader { protected: GLuint program {0}; + std::map uniform; bool linked; public: shader() = default; ~shader(); + const GLint & operator () (const std::string & name); + operator GLuint () const; void load_vertex(const char * filename); void load_fragment(const char * filename); void load_geometry(const char * filename); void enable(); void disable() const; - operator GLuint () const; protected: bool load(GLenum shader_type, const char * filename); diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index e51ced12..501740b7 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -16,6 +16,19 @@ shader::~shader() if(program) glDeleteProgram(program); } +const GLint & shader::operator () (const string & name) +{ + if(uniform.find(name) != uniform.end()) + uniform[name] = glGetUniformLocation(program, name.c_str()); + + return uniform[name]; +} + +shader::operator GLuint() const +{ + return program; +} + void shader::load_vertex(const char * filename) { load(GL_VERTEX_SHADER, filename); @@ -47,11 +60,6 @@ void shader::disable() const glUseProgram(0); } -shader::operator GLuint() const -{ - return program; -} - bool shader::load(GLenum shader_type, const char * filename) { string source; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c1654869..a5583e2c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -552,47 +552,43 @@ void viewer::raycasting(viewer * view) void viewer::render_gl() { - shader_sphere.enable(); + glProgramUniform3f(shader_sphere, shader_sphere("eye"), eye[1], eye[2], eye[3]); + glProgramUniform3f(shader_sphere, shader_sphere("light"), light[1], light[2], light[3]); + glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); - glUniform3f(glGetUniformLocation(shader_sphere, "eye"), eye[1], eye[2], eye[3]); - glUniform3f(glGetUniformLocation(shader_sphere, "light"), light[1], light[2], light[3]); - glUniformMatrix4fv(glGetUniformLocation(shader_sphere, "model_view_mat"), 1, 0, &view_mat[0][0]); - glUniformMatrix4fv(glGetUniformLocation(shader_sphere, "proj_mat"), 1, 0, &proj_mat[0][0]); - - shader_program.enable(); + glProgramUniform3f(shader_program, shader_program("eye"), eye[1], eye[2], eye[3]); + glProgramUniform3f(shader_program, shader_program("light"), light[1], light[2], light[3]); + glProgramUniform1i(shader_program, shader_program("render_flat"), render_flat); + glProgramUniform1i(shader_program, shader_program("render_lines"), render_lines); + glProgramUniform1i(shader_program, shader_program("render_wireframe"), render_wireframe_fill); + glProgramUniformMatrix4fv(shader_program, shader_program("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_program, shader_program("proj_mat"), 1, 0, &proj_mat[0][0]); - glUniform3f(glGetUniformLocation(shader_program, "eye"), eye[1], eye[2], eye[3]); - glUniform3f(glGetUniformLocation(shader_program, "light"), light[1], light[2], light[3]); - glUniform1i(glGetUniformLocation(shader_program, "render_flat"), render_flat); - glUniform1i(glGetUniformLocation(shader_program, "render_lines"), render_lines); - glUniform1i(glGetUniformLocation(shader_program, "render_wireframe"), render_wireframe_fill); - glUniformMatrix4fv(glGetUniformLocation(shader_program, "model_view_mat"), 1, 0, &view_mat[0][0]); - glUniformMatrix4fv(glGetUniformLocation(shader_program, "proj_mat"), 1, 0, &proj_mat[0][0]); + shader_program.enable(); draw_scene(); if(render_normal_field) { - shader_normals.enable(); + glProgramUniform1f(shader_normals, shader_normals("length"), mesh().factor); + glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - glUniform1f(glGetUniformLocation(shader_normals, "length"), mesh().factor); - glUniformMatrix4fv(glGetUniformLocation(shader_normals, "model_view_mat"), 1, 0, &view_mat[0][0]); - glUniformMatrix4fv(glGetUniformLocation(shader_normals, "proj_mat"), 1, 0, &proj_mat[0][0]); - + shader_normals.enable(); draw_scene(); } if(render_gradient_field) { - shader_gradient.enable(); + glProgramUniform1f(shader_gradient, shader_gradient("length"), mesh().factor); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); - glUniform1f(glGetUniformLocation(shader_gradient, "length"), mesh().factor); - glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "model_view_mat"), 1, 0, &view_mat[0][0]); - glUniformMatrix4fv(glGetUniformLocation(shader_gradient, "proj_mat"), 1, 0, &proj_mat[0][0]); - + shader_gradient.enable(); draw_scene(); } } From 461e639283c02f3576f80ae5311177eb56abfb29 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Feb 2020 16:31:26 +0100 Subject: [PATCH 0192/1018] render texture image --- include/viewer/frame.h | 4 +-- shaders/fragment_frame.glsl | 13 +++++++ shaders/vertex_frame.glsl | 11 ++++++ src/viewer/frame.cpp | 30 +++++++++++++++- src/viewer/viewer.cpp | 70 +++---------------------------------- 5 files changed, 59 insertions(+), 69 deletions(-) create mode 100644 shaders/fragment_frame.glsl create mode 100644 shaders/vertex_frame.glsl diff --git a/include/viewer/frame.h b/include/viewer/frame.h index c290b7b0..4f0a7a5e 100644 --- a/include/viewer/frame.h +++ b/include/viewer/frame.h @@ -15,7 +15,7 @@ class frame { private: GLuint render_tex; - GLuint vao, vbo; + GLuint vao, vbo, pbo; shader program; @@ -23,7 +23,7 @@ class frame frame(); ~frame(); - void display(void * buffer); + void display(const int & width, const int & height, void * buffer); }; diff --git a/shaders/fragment_frame.glsl b/shaders/fragment_frame.glsl new file mode 100644 index 00000000..75fa288f --- /dev/null +++ b/shaders/fragment_frame.glsl @@ -0,0 +1,13 @@ +#version 410 core + +in vec2 UV; +out vec3 color; + +uniform sampler2D render_tex; +uniform bool correct_gamma; + +void main() +{ + color = texture( render_tex, UV ).xyz; +} + diff --git a/shaders/vertex_frame.glsl b/shaders/vertex_frame.glsl new file mode 100644 index 00000000..220f3154 --- /dev/null +++ b/shaders/vertex_frame.glsl @@ -0,0 +1,11 @@ +#version 410 core + +layout(location = 0) in vec3 vertexPosition_modelspace; +out vec2 UV; + +void main() +{ + gl_Position = vec4(vertexPosition_modelspace,1); + UV = (vec2( vertexPosition_modelspace.x, vertexPosition_modelspace.y )+vec2(1,1))/2.0; +} + diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index dc831f9f..60b74d20 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -23,32 +23,60 @@ frame::frame() 1, 1, 0, }; + glGenTextures(1, &render_tex); glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); + glGenBuffers(1, &pbo); + glBindVertexArray(vao); + + glBindTexture(GL_TEXTURE_2D, render_tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glBindTexture(GL_TEXTURE_2D, 0); + glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } frame::~frame() { + glDeleteBuffers(1, &pbo); glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); + glDeleteTextures(1, &render_tex); } -void frame::display(void * buffer) +void frame::display(const int & width, const int & height, void * buffer) { + glBindBuffer(GL_ARRAY_BUFFER, pbo); + glBufferData(GL_ARRAY_BUFFER, width * height * sizeof(float) * 4, buffer, GL_STREAM_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + program.enable(); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, render_tex); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + glPixelStorei(GL_UNPACK_ALIGNMENT, 4); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + glUniform1i(program("render_tex"), 0); + glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, 6); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); + program.disable(); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index a5583e2c..bb06d9cf 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -613,11 +613,8 @@ void viewer::render_embree() action = false; - glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_embree->img); - - if(!render_frame) render_frame = new frame; - - render_frame->display(rt_embree->img); + if(!render_frame) render_frame = new frame; + render_frame->display(viewport_width, viewport_height, rt_embree->img); #endif // GPROSHAN_EMBREE } @@ -642,7 +639,8 @@ void viewer::render_optix() action = false; - glDrawPixels(viewport_width, viewport_height, GL_RGBA, GL_FLOAT, rt_optix->img); + if(!render_frame) render_frame = new frame; + render_frame->display(viewport_width, viewport_height, rt_embree->img); #endif // GPROSHAN_OPTIX } @@ -701,66 +699,6 @@ void viewer::draw_selected_vertices() void viewer::pick_vertex(int x, int y) { gproshan_log(VIEWER); - - int width, height; - glfwGetFramebufferSize(window, &width, &height); - - if(x < 0 || x >= width || y < 0 || y >= height) return; - - int bufSize = mesh()->n_vertices(); - GLuint * buf = new GLuint[bufSize]; - glSelectBuffer(bufSize, buf); - - GLint viewport[4]; - GLdouble projection[16]; - glGetIntegerv(GL_VIEWPORT, viewport); - glGetDoublev(GL_PROJECTION_MATRIX, projection); - - glRenderMode(GL_SELECT); - glInitNames(); - glPushName(0); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - //gluPickMatrix(x, viewport[3] - y, 10, 10, viewport); - glMultMatrixd(projection); - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); -// draw_vertices(); - glPopMatrix(); - - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - glMatrixMode(GL_MODELVIEW); - long hits = glRenderMode(GL_RENDER); - - index_t index = -1; - double min_z = 1.0e100; - for(long i = 0; i < hits; ++i) - { - double distance = buf[4*i + 1]; - if(distance < min_z) - { - index = buf[4*i + 3]; - min_z = distance; - } - } - delete[] buf; - - if(index != NIL && index < mesh()->n_vertices()) - { - select_vertices.push_back(index); - - gproshan_log_var(index); - gproshan_debug_var(mesh().color(index)); - gproshan_debug_var(mesh()->evt(index)); - - if(corr_mesh[current].is_loaded()) - gproshan_error_var(corr_mesh[current][index].alpha); - } } From 88f2b2d8c261c92c158ec9be3f2d6eb9a6384be8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Feb 2020 16:42:53 +0100 Subject: [PATCH 0193/1018] rt: embre display frame texture --- include/viewer/frame.h | 2 +- src/viewer/frame.cpp | 17 +++++------------ src/viewer/viewer.cpp | 2 ++ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/include/viewer/frame.h b/include/viewer/frame.h index 4f0a7a5e..2248792c 100644 --- a/include/viewer/frame.h +++ b/include/viewer/frame.h @@ -15,7 +15,7 @@ class frame { private: GLuint render_tex; - GLuint vao, vbo, pbo; + GLuint vao, vbo; shader program; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 60b74d20..adc66d43 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -26,7 +26,6 @@ frame::frame() glGenTextures(1, &render_tex); glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); - glGenBuffers(1, &pbo); glBindVertexArray(vao); @@ -49,7 +48,6 @@ frame::frame() frame::~frame() { - glDeleteBuffers(1, &pbo); glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); glDeleteTextures(1, &render_tex); @@ -57,24 +55,19 @@ frame::~frame() void frame::display(const int & width, const int & height, void * buffer) { - glBindBuffer(GL_ARRAY_BUFFER, pbo); - glBufferData(GL_ARRAY_BUFFER, width * height * sizeof(float) * 4, buffer, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - program.enable(); + + glBindVertexArray(vao); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, render_tex); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); - glPixelStorei(GL_UNPACK_ALIGNMENT, 4); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, 0); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - glUniform1i(program("render_tex"), 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, buffer); - glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, 6); glBindBuffer(GL_ARRAY_BUFFER, 0); + + glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); program.disable(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index bb06d9cf..987d3d4e 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -89,6 +89,8 @@ viewer::~viewer() #ifdef GPROSHAN_OPTIX if(rt_optix) delete rt_optix; #endif // GPROSHAN_OPTIX + + if(render_frame) delete render_frame; } bool viewer::run() From 5c1095ac2f754bbfbb2a6d9cc6231978844e4750 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Feb 2020 17:04:01 +0100 Subject: [PATCH 0194/1018] shader texture --- shaders/fragment_frame.glsl | 6 +++--- shaders/vertex_frame.glsl | 7 ++++--- src/viewer/frame.cpp | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/shaders/fragment_frame.glsl b/shaders/fragment_frame.glsl index 75fa288f..adf5df44 100644 --- a/shaders/fragment_frame.glsl +++ b/shaders/fragment_frame.glsl @@ -1,13 +1,13 @@ #version 410 core in vec2 UV; -out vec3 color; + +out vec4 color; uniform sampler2D render_tex; -uniform bool correct_gamma; void main() { - color = texture( render_tex, UV ).xyz; + color = texture(render_tex, UV).rgba; } diff --git a/shaders/vertex_frame.glsl b/shaders/vertex_frame.glsl index 220f3154..9666e273 100644 --- a/shaders/vertex_frame.glsl +++ b/shaders/vertex_frame.glsl @@ -1,11 +1,12 @@ #version 410 core -layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 0) in vec3 in_position; + out vec2 UV; void main() { - gl_Position = vec4(vertexPosition_modelspace,1); - UV = (vec2( vertexPosition_modelspace.x, vertexPosition_modelspace.y )+vec2(1,1))/2.0; + gl_Position = vec4(in_position, 1); + UV = ( vec2(in_position.x, in_position.y) + vec2(1) ) / 2; } diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index adc66d43..80914c3d 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -65,8 +65,8 @@ void frame::display(const int & width, const int & height, void * buffer) glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, 6); - glBindBuffer(GL_ARRAY_BUFFER, 0); - + + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); From e2bd16244eb91e2aba2a59e3254bad0de1a0ea22 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 28 Feb 2020 12:06:30 +0100 Subject: [PATCH 0195/1018] scale select vertices spheres on zoom in and zoom out --- CMakeLists.txt | 1 + shaders/vertex_sphere.glsl | 3 ++- src/viewer/viewer.cpp | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 54deb21e..24c97d1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ if(CUDA_FOUND) find_package(OptiX 7.0) if(OptiX_INCLUDE) + set(CUDA_HOST_COMPILER ${CMAKE_CUDA_HOST_COMPILER}) add_definitions(-DGPROSHAN_OPTIX) include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index 715ff270..d22d290e 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -9,10 +9,11 @@ out vec3 vs_normal; uniform mat4 model_view_mat; uniform mat4 proj_mat; +uniform float scale; void main() { - vs_position = in_position + in_translation; + vs_position = scale * in_position + in_translation; vs_normal = in_normal; gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 987d3d4e..39f00a08 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -558,6 +558,7 @@ void viewer::render_gl() glProgramUniform3f(shader_sphere, shader_sphere("light"), light[1], light[2], light[3]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); + glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); glProgramUniform3f(shader_program, shader_program("eye"), eye[1], eye[2], eye[3]); From b5b3779b0ae0ada915661ea4585962318b67474b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 28 Feb 2020 15:51:06 +0100 Subject: [PATCH 0196/1018] fix che_sphere --- include/che_sphere.h | 2 +- src/che.cpp | 2 +- src/che_sphere.cpp | 8 +++++--- src/viewer/viewer.cpp | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/che_sphere.h b/include/che_sphere.h index d45a90b2..89af9460 100644 --- a/include/che_sphere.h +++ b/include/che_sphere.h @@ -11,7 +11,7 @@ namespace gproshan { class che_sphere : public che { public: - che_sphere(const real_t & r = 1, const size_t & n = 10); + che_sphere(const real_t & r = 1, const size_t & n = 6); che_sphere(const che_sphere & mesh); virtual ~che_sphere(); }; diff --git a/src/che.cpp b/src/che.cpp index ff960527..e9b863be 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -625,7 +625,7 @@ void che::multiplicate_vertices() #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - aEVT[v] = EVT[v] * 3; + aEVT[v] = EVT[v] != NIL ? EVT[v] * 3 : NIL; #pragma omp parallel for for(index_t f = 0; f < n_faces_; f++) diff --git a/src/che_sphere.cpp b/src/che_sphere.cpp index 90782a76..4762674b 100644 --- a/src/che_sphere.cpp +++ b/src/che_sphere.cpp @@ -5,6 +5,7 @@ #include #include + using namespace std; @@ -20,10 +21,11 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) std::vector faces; const real_t delta = M_PI / n; - for(real_t phi = 0; phi < 2 * M_PI; phi += delta) - for(real_t theta = delta; theta < M_PI; theta += delta) - vertices.push_back({r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)}); + for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta; phi += delta) + for(real_t theta = delta; theta < M_PI - 0.5 * delta; theta += delta) + vertices.push_back({r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)}); + vertices.push_back({0, 0, r}); vertices.push_back({0, 0, -r}); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 39f00a08..b543cc56 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -693,6 +693,8 @@ void viewer::draw_selected_vertices() sphere.update_instances_translations(sphere_translations); } + glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); + shader_sphere.enable(); if(sphere_translations.size()) sphere.draw(); From 63545627f86f7710f943e3715bc04b98487a35b0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 2 Mar 2020 12:00:30 +0100 Subject: [PATCH 0197/1018] remove unneeded ifs --- include/che.h | 32 ++++++++++++++++---------------- include/geodesics.h | 3 ++- src/app_viewer.cpp | 6 +++--- src/che.cpp | 23 +++++++++-------------- src/geodesics.cpp | 7 ++++--- 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/include/che.h b/include/che.h index 16441b29..42ecb57b 100644 --- a/include/che.h +++ b/include/che.h @@ -27,26 +27,26 @@ struct corr_t; class che { public: - static const size_t P = 3; + static const size_t P = 3; ///< default polygon size 3, triangular meshes protected: std::string filename_; - size_t n_vertices_; - size_t n_faces_; - size_t n_half_edges_; - size_t n_edges_; - size_t n_borders_; - - vertex * GT; ///< geometry table : v -> vertex - index_t * VT; ///< vertex table (faces) : he -> v - index_t * OT; ///< opposite table : he -> he - index_t * EVT; ///< extra vertex table : v -> he - index_t * ET; ///< edge table : e -> he - index_t * EHT; ///< extra half edge table : he -> e - index_t * BT; ///< boundary table : b -> v - - bool manifold; + size_t n_vertices_ = 0; + size_t n_faces_ = 0; + size_t n_half_edges_ = 0; + size_t n_edges_ = 0; + size_t n_borders_ = 0; + + vertex * GT = nullptr; ///< geometry table : v -> vertex + index_t * VT = nullptr; ///< vertex table (faces) : he -> v + index_t * OT = nullptr; ///< opposite table : he -> he + index_t * EVT = nullptr; ///< extra vertex table : v -> he + index_t * ET = nullptr; ///< edge table : e -> he + index_t * EHT = nullptr; ///< extra half edge table : he -> e + index_t * BT = nullptr; ///< boundary table : b -> v + + bool manifold = true; public: che(const size_t & n_v = 0, const size_t & n_f = 0); diff --git a/include/geodesics.h b/include/geodesics.h index 32320826..8c0913ed 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -33,9 +33,10 @@ class geodesics private: distance_t * dist; ///< Results of computation geodesic distances. index_t * sorted_index; ///< Sort vertices by topological level or geodesic distance. - const size_t & n_vertices; ///< Number of vertices. size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; + + const size_t & n_vertices; ///< Number of vertices, constance reference public: geodesics(che * mesh, ///< input mesh must be a triangular mesh. diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 0dd89c9f..510a9c22 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -47,9 +47,9 @@ int app_viewer::main(int nargs, const char ** args) TIC(time) - vector meshes; + vector vm; for(int i = 1; i < nargs; i++) - meshes.push_back(load_mesh(args[i])); + vm.push_back(load_mesh(args[i])); TOC(time) @@ -57,7 +57,7 @@ int app_viewer::main(int nargs, const char ** args) gproshan_log_var(time); //init meshes - add_mesh(meshes); + add_mesh(vm); sub_menus.push_back("Fairing"); add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); diff --git a/src/che.cpp b/src/che.cpp index e9b863be..313a50b1 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -1275,14 +1275,9 @@ void che::init(const size_t & n_v, const size_t & n_f) { n_vertices_ = n_v; n_faces_ = n_f; - n_half_edges_ = n_edges_ = n_borders_ = 0; - - GT = nullptr; - VT = OT = EVT = ET = BT = nullptr; - manifold = true; - n_half_edges_ = che::P * n_faces_; - n_edges_ = 0; //n_half_edges_ / 2; /**/ + + n_edges_ = n_borders_ = 0; if(n_vertices_) GT = new vertex[n_vertices_]; if(n_half_edges_) VT = new index_t[n_half_edges_]; @@ -1402,13 +1397,13 @@ void che::update_bt() void che::delete_me() { - if(GT) delete [] GT; - if(VT) delete [] VT; - if(OT) delete [] OT; - if(EVT) delete [] EVT; - if(ET) delete [] ET; - if(EHT) delete [] EHT; - if(BT) delete [] BT; + delete [] GT; + delete [] VT; + delete [] OT; + delete [] EVT; + delete [] ET; + delete [] EHT; + delete [] BT; } void che::read_file(const string & ) diff --git a/src/geodesics.cpp b/src/geodesics.cpp index e3b3a892..b73d85ce 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -18,6 +18,7 @@ geodesics::geodesics(che * mesh, const vector & sources, const option_t assert(n_vertices > 0); free_dist = e_dist == nullptr; + dist = free_dist ? new distance_t[n_vertices] : e_dist; clusters = cluster ? new index_t[n_vertices] : nullptr; sorted_index = new index_t[n_vertices]; @@ -34,9 +35,9 @@ geodesics::geodesics(che * mesh, const vector & sources, const option_t geodesics::~geodesics() { - if(free_dist) delete [] dist; - if(sorted_index) delete [] sorted_index; - if(clusters) delete [] clusters; + delete [] dist; + delete [] sorted_index; + delete [] clusters; } const distance_t & geodesics::operator[](const index_t & i) const From ee6d433fa68a4d8a5a93c5926cf25527e5c21aa6 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 2 Mar 2020 17:42:52 +0100 Subject: [PATCH 0198/1018] new measurement of variation in patches --- include/mdict/patch.h | 1 + src/mdict/inpainting.cpp | 6 +++--- src/mdict/patch.cpp | 32 ++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index aaeed1d2..7acebd3e 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -94,6 +94,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); + vertex normal_trim(che * mesh, const index_t & v, index_t * indexes); private: diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 9d742b5e..ca094da4 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -447,10 +447,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(seeds.size()); + a_vec outlv(outliers.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index fb4dc2b5..cc1f86c9 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -55,6 +55,24 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } +vertex patch::normal_trim(che * mesh, const index_t & v, index_t * indexes) +{ + vertex n; + area_t area, area_star = 0; + + for_star(he, mesh, v) + { + //mesh->vt(next(he)) //index of the next vertex + area = mesh->area_trig(trig(he)); + area_star += area; + n += area * mesh->normal_he(he); + } + + n /= area_star; + return n / *n; +} + + void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio) { //radio = radio_; @@ -75,6 +93,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in double angle; double sum_angle = 0; double delta = 1; + //0.1 toomuch //gproshan_debug_var(v); for(size_t i=1; inormal(indexes[i]) ) ); angle = acos( (n, mesh->normal(indexes[i]) ) ) ; - //if( angle < PI/2.5 && (sum_angle/vertices.size()) + angle <= PI/2.2) //if( angle < PI/2.5 && (sum_angle) <= delta * PI) - if( angle < PI/2.5 && (sum_angle) <= delta * PI) + + if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) { if(*p > radio) @@ -103,16 +123,16 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //gproshan_debug_var(euc_radio); vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); - sum_angle += angle; - delta += 0.01; + //sum_angle += angle; + //delta += 0.03 ; // sharp meshes 0.001 - //if(i%7 == 0) sum_angle = 0; + // smooth meshes 0.035 at max } else { break; } - //gproshan_debug_var(sum_angle); + // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } /* gproshan_debug_var(PI/2.5); gproshan_debug_var(sum_angle); From e40ebba6f874da47e631436dfc9ba5d2a88ff820 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 3 Mar 2020 12:32:26 +0100 Subject: [PATCH 0199/1018] geodesics params udpate --- include/geodesics_ptp.h | 4 ++-- src/app_viewer.cpp | 13 ++++--------- src/cuda/geodesics_ptp_coalescence.cu | 8 ++++---- src/geodesics.cpp | 3 ++- src/geodesics_ptp.cpp | 5 +++-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/include/geodesics_ptp.h b/include/geodesics_ptp.h index 092729b3..30173953 100644 --- a/include/geodesics_ptp.h +++ b/include/geodesics_ptp.h @@ -29,9 +29,9 @@ struct toplesets_t const index_t *const & index; }; -che * ptp_coalescence(index_t * & inv, che * mesh, const toplesets_t & toplesets); +che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets); -double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf = 1); +double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf = 1); double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 510a9c22..89e6735d 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -31,7 +31,7 @@ app_viewer::app_viewer() app_viewer::~app_viewer() { - if(dist) delete [] dist; + delete [] dist; for(che * mesh: meshes) delete mesh; @@ -65,7 +65,7 @@ int app_viewer::main(int nargs, const char ** args) sub_menus.push_back("Geodesics"); add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); - add_process(GLFW_KEY_U, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); + add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); #ifndef SINGLE_P add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_flow}); #endif @@ -763,15 +763,10 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) if(!view->select_vertices.size()) view->select_vertices.push_back(0); - if(view->dist && view->n_dist != mesh->n_vertices()) + if(view->n_dist != mesh->n_vertices()) { delete [] view->dist; - view->n_dist = 0; - view->dist = nullptr; - } - - if(!view->dist) - { + view->n_dist = mesh->n_vertices(); view->dist = new distance_t[view->n_dist]; } diff --git a/src/cuda/geodesics_ptp_coalescence.cu b/src/cuda/geodesics_ptp_coalescence.cu index 33b7638a..f151e577 100644 --- a/src/cuda/geodesics_ptp_coalescence.cu +++ b/src/cuda/geodesics_ptp_coalescence.cu @@ -18,10 +18,10 @@ using namespace std; namespace gproshan { -double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets, const bool & set_inf) +double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const vector & sources, const toplesets_t & toplesets, const bool & set_inf) { index_t * inv = nullptr; - mesh = ptp_coalescence(inv, mesh, toplesets); + che * coalescence_mesh = ptp_coalescence(inv, mesh, toplesets); // ------------------------------------------------------ @@ -35,7 +35,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, // BEGIN PTP - CHE * h_mesh = new CHE(mesh); + CHE * h_mesh = new CHE(coalescence_mesh); CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); @@ -78,7 +78,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, cudaFree(d_dist[1]); cuda_free_CHE(dd_mesh, d_mesh); - delete mesh; + delete coalescence_mesh; delete [] inv; #pragma omp parallel for diff --git a/src/geodesics.cpp b/src/geodesics.cpp index b73d85ce..e3a8ae0c 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -35,7 +35,8 @@ geodesics::geodesics(che * mesh, const vector & sources, const option_t geodesics::~geodesics() { - delete [] dist; + if(free_dist) delete [] dist; + delete [] sorted_index; delete [] clusters; } diff --git a/src/geodesics_ptp.cpp b/src/geodesics_ptp.cpp index 3c46fad6..7c4ebb31 100644 --- a/src/geodesics_ptp.cpp +++ b/src/geodesics_ptp.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace std; @@ -12,11 +13,11 @@ namespace gproshan { ptp_out_t::ptp_out_t(distance_t *const & d, index_t *const & c): dist(d), clusters(c) {} -che * ptp_coalescence(index_t * & inv, che * mesh, const toplesets_t & toplesets) +che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets) { // sort data by levels, must be improve the coalescence - vector V(mesh->n_vertices()); + vector V(toplesets.limits.back()); vector F; F.reserve(mesh->n_half_edges()); From 36c1eed710ed5a131dea4fa4989d10ca7a0e4bb8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 4 Mar 2020 13:00:23 +0100 Subject: [PATCH 0200/1018] gl: rendering point clouds --- include/viewer/che_viewer.h | 4 +- include/viewer/viewer.h | 9 ++-- shaders/fragment_pointcloud.glsl | 93 ++++++++++++++++++++++++++++++++ shaders/vertex.glsl | 2 +- shaders/vertex_pointcloud.glsl | 22 ++++++++ src/viewer/che_viewer.cpp | 37 +++++++------ src/viewer/viewer.cpp | 67 ++++++++++++----------- 7 files changed, 181 insertions(+), 53 deletions(-) create mode 100644 shaders/fragment_pointcloud.glsl create mode 100644 shaders/vertex_pointcloud.glsl diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index b600a7cd..d9ee9b8b 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -2,6 +2,7 @@ #define CHE_VIEWER_H #include "che.h" +#include "shader.h" #include "include_opengl.h" @@ -55,7 +56,8 @@ class che_viewer void update_normals(); void update_colors(const color_t *const c = nullptr); void update_instances_translations(const std::vector & translations); - void draw(); + void draw(shader & program); + void draw_point_cloud(shader & program); color_t & color(const index_t & v); vertex & normal(const index_t & v); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 13b5921a..d2508872 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -96,7 +96,7 @@ class viewer shader shader_program; shader shader_normals; shader shader_gradient; - shader shader_frame; + shader shader_pointcloud; camera cam; @@ -213,11 +213,10 @@ class viewer static void raycasting(viewer * view); // draw routines - void draw_scene(); - void draw_polygons(); - void draw_border(); - void draw_selected_vertices(); + void draw_meshes(shader & program); + void draw_selected_vertices(shader & program); + void select_border_vertices(); void pick_vertex(int x, int y); }; diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl new file mode 100644 index 00000000..4e70e9c6 --- /dev/null +++ b/shaders/fragment_pointcloud.glsl @@ -0,0 +1,93 @@ +#version 410 core + +in vec3 vs_position; +in vec3 vs_normal; +in float vs_color; + +layout(location = 0) out vec4 frag_color; + +uniform vec3 eye; +uniform vec3 light; +uniform bool render_lines; + +float diffuse(vec3 N, vec3 L) +{ + return max(0, dot(N, L)); +} + +float specular(vec3 N, vec3 L, vec3 E) +{ + const float shininess = 4; + vec3 R = 2 * dot(L, N) * N - L; + + return pow(max(0, dot(R, E)), shininess); +} + +float fresnel(vec3 N, vec3 E) +{ + const float sharpness = 10; + float NE = max(0, dot(N, E)); + + return pow(sqrt( 1. - NE * NE ), sharpness); +} + +//https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag +float colormap_red(float x) +{ + if (x < 0.7520372909206926) + return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; + else + return -1.20830453148990E+01 * x + 1.44337397593436E+01; +} + +float colormap_green(float x) +{ + if (x < 0.7485333535031721) + return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; + else + return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; +} + +float colormap_blue(float x) +{ + if (x < 0.7628468501376879) + return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; + else + return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; +} + +vec3 colormap(float x) +{ + float r = clamp(colormap_red(x) / 255.0, .0, .9); + float g = clamp(colormap_green(x) / 255.0, .0, .9); + float b = clamp(colormap_blue(x) / 255.0, .0, .9); + return vec3(r, g, b); +} + +void main() +{ + vec3 color = colormap(vs_color); + + // lines + if(render_lines) + { + float h = vs_color; + h = h * 40; + h = h - floor(h); + h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); + h = 1 - h; + color = vec3(0) + (1. - h) * color; + } + + vec3 N = normalize(vs_normal); + + vec3 L = normalize(light - vs_position); + vec3 E = normalize(eye - vs_position); + vec3 R = 2 * dot(L, N) * N - L; + vec3 one = vec3(1); + + + color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + frag_color = vec4(color, 1); +} + diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 79551501..2abea13f 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -17,6 +17,6 @@ void main() vs_normal = in_normal; vs_color = in_color; - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); } diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl new file mode 100644 index 00000000..2abea13f --- /dev/null +++ b/shaders/vertex_pointcloud.glsl @@ -0,0 +1,22 @@ +#version 410 core + +layout (location=0) in vec3 in_position; +layout (location=1) in vec3 in_normal; +layout (location=2) in float in_color; + +out vec3 vs_position; +out vec3 vs_normal; +out float vs_color; + +uniform mat4 model_view_mat; +uniform mat4 proj_mat; + +void main() +{ + vs_position = in_position; + vs_normal = in_normal; + vs_color = in_color; + + gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); +} + diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index b9b7837e..d0a89f3a 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -125,16 +125,6 @@ void che_viewer::update_vbo() glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges() * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - else - { - index_t * indices = new index_t[n_vertices]; - iota(indices, indices + n_vertices, 0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, n_vertices * sizeof(index_t), indices, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - delete [] indices; - } glBindVertexArray(0); } @@ -191,20 +181,37 @@ void che_viewer::update_instances_translations(const vector & translatio glBindVertexArray(0); } -void che_viewer::draw() +void che_viewer::draw(shader & program) { + program.enable(); + glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0, n_instances); - else if(mesh->n_faces()) - glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); else - glDrawElements(GL_POINTS, mesh->n_vertices(), GL_UNSIGNED_INT, 0); - + glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); + + program.disable(); +} + +void che_viewer::draw_point_cloud(shader & program) +{ + program.enable(); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); + + glDrawArrays(GL_POINTS, 0, mesh->n_vertices()); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + program.disable(); } color_t & che_viewer::color(const index_t & v) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index b543cc56..91d3068e 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -284,6 +284,9 @@ void viewer::init_glsl() shader_gradient.load_vertex("../shaders/vertex_gradient.glsl"); shader_gradient.load_geometry("../shaders/geometry_gradient.glsl"); shader_gradient.load_fragment("../shaders/fragment_gradient.glsl"); + + shader_pointcloud.load_vertex("../shaders/vertex_pointcloud.glsl"); + shader_pointcloud.load_fragment("../shaders/fragment_pointcloud.glsl"); } void viewer::update_vbo() @@ -570,8 +573,13 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_program, shader_program("proj_mat"), 1, 0, &proj_mat[0][0]); - shader_program.enable(); - draw_scene(); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[1], eye[2], eye[3]); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); + glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); + + + draw_meshes(shader_program); if(render_normal_field) @@ -580,8 +588,7 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - shader_normals.enable(); - draw_scene(); + draw_meshes(shader_normals); } @@ -591,9 +598,13 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); - shader_gradient.enable(); - draw_scene(); + draw_meshes(shader_gradient); } + + + if(render_border) select_border_vertices(); + + draw_selected_vertices(shader_sphere); } void viewer::render_embree() @@ -648,17 +659,7 @@ void viewer::render_optix() #endif // GPROSHAN_OPTIX } -void viewer::draw_scene() -{ - draw_polygons(); - - if(render_border) - draw_border(); - - draw_selected_vertices(); -} - -void viewer::draw_polygons() +void viewer::draw_meshes(shader & program) { if(render_wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -669,19 +670,15 @@ void viewer::draw_polygons() for(index_t i = 0; i < n_meshes; i++) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - meshes[i].draw(); - } -} -void viewer::draw_border() -{ - select_vertices.clear(); - for(index_t b = 0; b < mesh()->n_borders(); b++) - for_border(he, mesh(), mesh()->bt(b)) - select_vertices.push_back(mesh()->vt(he)); + if(!meshes[i]->n_faces()) + meshes[i].draw_point_cloud(shader_pointcloud); + else + meshes[i].draw(program); + } } -void viewer::draw_selected_vertices() +void viewer::draw_selected_vertices(shader & program) { if(sphere_translations.size() != select_vertices.size()) { @@ -693,12 +690,20 @@ void viewer::draw_selected_vertices() sphere.update_instances_translations(sphere_translations); } - glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); - shader_sphere.enable(); if(sphere_translations.size()) - sphere.draw(); - shader_sphere.disable(); + { + glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); + sphere.draw(program); + } +} + +void viewer::select_border_vertices() +{ + select_vertices.clear(); + for(index_t b = 0; b < mesh()->n_borders(); b++) + for_border(he, mesh(), mesh()->bt(b)) + select_vertices.push_back(mesh()->vt(he)); } void viewer::pick_vertex(int x, int y) From 015da6a4797fb5f3dddaacbda7387905680525b1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 4 Mar 2020 13:47:38 +0100 Subject: [PATCH 0201/1018] rt: embree render point cloud as disc points --- src/raytracing/rt_embree.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index ce04dade..c3fb7d49 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -38,7 +38,8 @@ embree::~embree() void embree::build_bvh(const std::vector & meshes) { for(auto & m: meshes) - add_mesh(m); + if(m->n_faces()) add_mesh(m); + else add_point_cloud(m); rtcCommitScene(scene); } @@ -95,7 +96,7 @@ index_t embree::add_mesh(const che * mesh) index_t embree::add_point_cloud(const che * mesh) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_DISC_POINT); glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, @@ -103,20 +104,21 @@ index_t embree::add_point_cloud(const che * mesh) 4 * sizeof(float), mesh->n_vertices() ); - +/* glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), mesh->n_vertices() ); + */ #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001f); vertex n = mesh->normal(i); - normal[i] = glm::vec3(n.x, n.y, n.z); + // normal[i] = glm::vec3(n.x, n.y, n.z); } rtcCommitGeometry(geom); From a321b7a561bbe07ccca24f04f14bc335a76da39f Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 4 Mar 2020 16:18:30 +0100 Subject: [PATCH 0202/1018] new variation patch grwing --- include/mdict/patch.h | 4 +- src/mdict/inpainting.cpp | 8 ++-- src/mdict/patch.cpp | 86 ++++++++++++++++++++++++++++++---------- 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 7acebd3e..0865ada4 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -6,6 +6,7 @@ #include #include "include_arma.h" +#include "geodesics.h" #include @@ -94,7 +95,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - vertex normal_trim(che * mesh, const index_t & v, index_t * indexes); + bool add_vertex_by_faces(vertex * N, double thr_angle, index_t i, const geodesics & geo, che * mesh, const index_t & v, bool * taken); private: @@ -111,6 +112,7 @@ class patch const distance_t & radio, index_t * toplevel ); + bool exists(index_t idx); /// Initialize transformation matrix T and translation vector x, using CGAL jet_fitting. void jet_fit_directions(che * mesh, diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ca094da4..11715ae3 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -335,7 +335,7 @@ void inpainting::init_radial_feature_patches() const vertex & v_seed = mesh->gt(seeds[j]); // 0.7 coverage parameter - if( *(v_patch - v_seed) < 0.5 * radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.5* radios[j] ) // radio of each patch found = true; j++; } @@ -447,10 +447,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(outliers.size()); + a_vec outlv(seeds.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; + for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index cc1f86c9..2f78d2f1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -55,21 +55,55 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -vertex patch::normal_trim(che * mesh, const index_t & v, index_t * indexes) +bool patch::exists(index_t idx) { - vertex n; - area_t area, area_star = 0; + for(size_t i=1; i < vertices.size(); i++) + { + if(vertices[i] == idx) + return true; + } + return false; +} +bool patch::add_vertex_by_faces(vertex * N, double thr_angle, index_t i, const geodesics & geo, che * mesh, const index_t & v, bool * taken) +{ + + index_t a, b; + index_t min_he; + double angle = PI; + double tmp_angle; + bool added = false; for_star(he, mesh, v) { - //mesh->vt(next(he)) //index of the next vertex - area = mesh->area_trig(trig(he)); - area_star += area; - n += area * mesh->normal_he(he); - } + + a = mesh->vt(next(he)); //index of the next vertex index_t + b = mesh->vt(prev(he)); + /*gproshan_debug_var(geo[b]); + gproshan_debug_var(geo[a]);angle + gproshan_debug_var(geo[v ]);*/ + + // If is an adjacent face + if( geo[a] < geo[v] || geo[b] < geo[v] ) + { + tmp_angle = acos( (mesh->normal_he(he), N[i-1]) ); + //gproshan_debug_var(tmp_angle); + //gproshan_debug_var(N[i-1]); + //gproshan_debug_var(mesh->normal_he(he)); - n /= area_star; - return n / *n; + if ( angle > tmp_angle && tmp_angle < thr_angle ) // Fullfill conditions + { + angle = tmp_angle; + min_he = he; + //if( !exists(a) ) vertices.push_back(a); + //if( !exists(b) ) vertices.push_back(b); + if( !exists(v) ) vertices.push_back(v); + added = true; + } + } + + } + N[i] = mesh->normal_he(min_he); + return added; } @@ -90,12 +124,18 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertex p, c; vertices.push_back(v); euc_radio = -INFINITY; - double angle; - double sum_angle = 0; - double delta = 1; - //0.1 toomuch + + vertex N[geo.n_sorted_index()]; + N[0] = n; + //double angle; + //double sum_angle = 0; +// double delta = 1; + //vertex prev_n, curr_n; + bool taken[geo.n_sorted_index()] = {}; + //gproshan_debug_var(v); - for(size_t i=1; iget_vertex(indexes[i]); @@ -105,12 +145,18 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in p = p - ((p,n)*n); //if(*p <= radio ) //gproshan_debug_var(indexes[i]); - sum_angle = acos( (n, mesh->normal(indexes[i]) ) ); - angle = acos( (n, mesh->normal(indexes[i]) ) ) ; + // sum_angle = acos( (n, mesh->normal(indexes[i]) ) ); + //angle = acos( (n, mesh->normal(indexes[i]) ) ) ; + + //prev_n = curr_n; + //curr_n = normal_trim(geo, mesh, indexes[i]); + //gproshan_debug_var(acos( (prev_n, curr_n) )); //if( angle < PI/2.5 && (sum_angle) <= delta * PI) - - if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) + //if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders + if(add_vertex_by_faces(N, PI/8, i, geo, mesh, indexes[i], taken)) + //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders { + //gproshan_debug_var(vertices.size()); if(*p > radio) { @@ -121,7 +167,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in if(*(p - c) > euc_radio) euc_radio = *(p - c); //gproshan_debug_var(euc_radio); - vertices.push_back(indexes[i]); + //vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); //sum_angle += angle; //delta += 0.03 ; From fd5e8f84fe84d77b616e273f709361d048da7ac6 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 5 Mar 2020 10:35:02 +0100 Subject: [PATCH 0203/1018] restricting size of patches --- src/mdict/inpainting.cpp | 52 ---------------------------------------- src/mdict/patch.cpp | 7 +++--- 2 files changed, 4 insertions(+), 55 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 11715ae3..e990a0e1 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -383,58 +383,6 @@ void inpainting::init_radial_feature_patches() gproshan_debug_var(seeds.size()); M = seeds.size(); -////////////////////////////////////// -/* - //new order bigger to smaller - vector geo_radios; - geo_radios.resize(seeds.size()); - for(index_t i = 0; i < seeds.size(); i++) - geo_radios[i] = patches[i].radio; - vector sorted_patches_size = sort_indexes(geo_radios); - - //Coverage of the points - bool covered_p[mesh->n_vertices()]; - size_t ncount = 0; - seeds.clear(); - - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered_p[i] = 0; - } - size_t count_valid = 0; - - vector valid_patches; - - for(index_t i = 0; i < sorted_patches_size.size(); i++) - { - patch * p = &patches[sorted_patches_size[i]]; - if(!p->is_covered(covered_p)) // if not all vertices in the patch are covered - { - seeds.push_back(sorted_patches_size[i]); - - valid_patches.push_back(patches[sorted_patches_size[i]]); - count_valid++; // we take this one - for(index_t k = 0; k < p->vertices.size(); k++) - { - if(!covered_p[ p->vertices[k] ]) ncount++; - covered_p[ p->vertices[k] ] = 1; - } - - } - - //gproshan_debug_var(sorted_patches_size[i]); - //gproshan_debug_var(geo_radios[sorted_patches_size[i]]); - } - gproshan_debug_var(count_valid); - gproshan_debug_var(ncount); - - // discard patches which are not needed - patches.clear(); - patches = valid_patches; - gproshan_debug_var(patches.size()); - M = patches.size(); -*/ /////////////////////////////////////// gproshan_debug_var(M); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2f78d2f1..2a37dc91 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -129,7 +129,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in N[0] = n; //double angle; //double sum_angle = 0; -// double delta = 1; + double delta = 0; //vertex prev_n, curr_n; bool taken[geo.n_sorted_index()] = {}; @@ -152,8 +152,9 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //curr_n = normal_trim(geo, mesh, indexes[i]); //gproshan_debug_var(acos( (prev_n, curr_n) )); //if( angle < PI/2.5 && (sum_angle) <= delta * PI) + //penalize gowing, I want them to grow only if they do not vary so much //if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders - if(add_vertex_by_faces(N, PI/8, i, geo, mesh, indexes[i], taken)) + if(add_vertex_by_faces(N, PI/(8+delta), i, geo, mesh, indexes[i], taken)) //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders { //gproshan_debug_var(vertices.size()); @@ -170,7 +171,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); //sum_angle += angle; - //delta += 0.03 ; + delta += 0.001 ; // sharp meshes 0.001 // smooth meshes 0.035 at max } From 59aa79e01e45d0732d8d20703494fa61e60aeba2 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 5 Mar 2020 17:14:23 +0100 Subject: [PATCH 0204/1018] reviewing right angle comparison --- include/mdict/patch.h | 3 ++- src/mdict/inpainting.cpp | 6 ++--- src/mdict/patch.cpp | 54 +++++++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 0865ada4..4f5f6b13 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -95,7 +95,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(vertex * N, double thr_angle, index_t i, const geodesics & geo, che * mesh, const index_t & v, bool * taken); + bool add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v); private: @@ -125,6 +125,7 @@ class patch real_t get_max_z(); void save_z(ostream & os); + index_t find(index_t * indexes, size_t nc, index_t idx_global); friend class dictionary; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e990a0e1..d247ba37 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -395,10 +395,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(seeds.size()); + a_vec outlv(outliers.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2a37dc91..a037fb31 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -1,6 +1,8 @@ #include "patch.h" #include "dictionary.h" +#include "che_sphere.h" +#include "che_off.h" #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS @@ -64,10 +66,17 @@ bool patch::exists(index_t idx) } return false; } -bool patch::add_vertex_by_faces(vertex * N, double thr_angle, index_t i, const geodesics & geo, che * mesh, const index_t & v, bool * taken) + +index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) +{ + for(size_t i=0; i & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v) { - index_t a, b; + index_t a, b, i = 0; index_t min_he; double angle = PI; double tmp_angle; @@ -78,31 +87,40 @@ bool patch::add_vertex_by_faces(vertex * N, double thr_angle, index_t i, const g a = mesh->vt(next(he)); //index of the next vertex index_t b = mesh->vt(prev(he)); - /*gproshan_debug_var(geo[b]); - gproshan_debug_var(geo[a]);angle - gproshan_debug_var(geo[v ]);*/ + gproshan_debug_var(geo[b]); + gproshan_debug_var(geo[a]); + gproshan_debug_var(geo[v ]); // If is an adjacent face if( geo[a] < geo[v] || geo[b] < geo[v] ) { - tmp_angle = acos( (mesh->normal_he(he), N[i-1]) ); - //gproshan_debug_var(tmp_angle); - //gproshan_debug_var(N[i-1]); + if(geo[a] < geo[v]) + { + i = find(indexes, nc,a); + } + else + { + i = find(indexes, nc, b); + gproshan_debug_var(b); + gproshan_debug_var(i); + } + tmp_angle = acos( (mesh->normal_he(he), N[i]) ); + gproshan_debug_var(tmp_angle); + gproshan_debug_var(N[i]); //gproshan_debug_var(mesh->normal_he(he)); if ( angle > tmp_angle && tmp_angle < thr_angle ) // Fullfill conditions { angle = tmp_angle; min_he = he; - //if( !exists(a) ) vertices.push_back(a); - //if( !exists(b) ) vertices.push_back(b); if( !exists(v) ) vertices.push_back(v); added = true; } } } - N[i] = mesh->normal_he(min_he); + + N.push_back(mesh->normal_he(min_he)); return added; } @@ -111,6 +129,9 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in { //radio = radio_; radio = -INFINITY; + //che_sphere my_sphere(1,12); + //string sphere_file = tmp_file_path("sphere"); + //che_off::write_file(&my_sphere, sphere_file); normal_fit_directions(mesh, v); @@ -125,15 +146,13 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vertices.push_back(v); euc_radio = -INFINITY; - vertex N[geo.n_sorted_index()]; - N[0] = n; + vector N; + N.push_back(n); //double angle; //double sum_angle = 0; double delta = 0; //vertex prev_n, curr_n; - bool taken[geo.n_sorted_index()] = {}; - - //gproshan_debug_var(v); + //gproshan_debug_var(geo.n_sorted_index()); for(index_t i=1; inormal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders - if(add_vertex_by_faces(N, PI/(8+delta), i, geo, mesh, indexes[i], taken)) + if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/8, geo, mesh, indexes[i]) ) //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders { //gproshan_debug_var(vertices.size()); @@ -185,6 +204,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in gproshan_debug_var(sum_angle); gproshan_debug_var(PI/(delta+0.05)*(vertices.size()-1));*/ delete indexes; + //gproshan_debug_var(v); } From 8cca31ac9f48743594ed4c6a06f06011735ce72c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 6 Mar 2020 12:00:49 +0100 Subject: [PATCH 0205/1018] fix rotation --- src/viewer/viewer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 91d3068e..c6f4903c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -111,6 +111,7 @@ bool viewer::run() eye = r.conj() * eye * r; light = r.conj() * light * r; + up = r.conj() * up * r; proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), From 56c19b9c4a5875d2b530c911c7fb268ebbf942a6 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 6 Mar 2020 12:33:45 +0100 Subject: [PATCH 0206/1018] fixed new variation patch growing --- src/mdict/patch.cpp | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a037fb31..cf0b4c12 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -77,7 +77,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc { index_t a, b, i = 0; - index_t min_he; + vertex min_he; double angle = PI; double tmp_angle; bool added = false; @@ -87,40 +87,48 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc a = mesh->vt(next(he)); //index of the next vertex index_t b = mesh->vt(prev(he)); - gproshan_debug_var(geo[b]); + /*gproshan_debug_var(geo[b]); gproshan_debug_var(geo[a]); gproshan_debug_var(geo[v ]); - +*/ // If is an adjacent face if( geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) { - i = find(indexes, nc,a); + i = find(indexes, nc,a); + //gproshan_debug_var(a); + //gproshan_debug_var(i); } else { i = find(indexes, nc, b); - gproshan_debug_var(b); - gproshan_debug_var(i); + //gproshan_debug_var(b); + //gproshan_debug_var(i); } tmp_angle = acos( (mesh->normal_he(he), N[i]) ); - gproshan_debug_var(tmp_angle); - gproshan_debug_var(N[i]); + /* gproshan_debug_var(tmp_angle); + gproshan_debug_var(angle); + gproshan_debug_var(thr_angle); + gproshan_debug_var(N[i]);*/ //gproshan_debug_var(mesh->normal_he(he)); if ( angle > tmp_angle && tmp_angle < thr_angle ) // Fullfill conditions { angle = tmp_angle; - min_he = he; + //gproshan_debug_var(he); + min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); added = true; } + } + //gproshan_debug_var(N[i]); } + //gproshan_debug_var(min_he); - N.push_back(mesh->normal_he(min_he)); + N.push_back(min_he); return added; } @@ -173,7 +181,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //if( angle < PI/2.5 && (sum_angle) <= delta * PI) //penalize gowing, I want them to grow only if they do not vary so much //if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders - if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/8, geo, mesh, indexes[i]) ) + if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/12, geo, mesh, indexes[i]) ) //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders { //gproshan_debug_var(vertices.size()); From f953f730ebf644e446a9ac488a03320e15985a1c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 6 Mar 2020 14:45:44 +0100 Subject: [PATCH 0207/1018] rt: shading normals embree improve normal che --- include/che.h | 4 ++++ include/raytracing/raytracing.h | 3 +++ include/raytracing/rt_embree.h | 7 +++++++ src/che.cpp | 7 +++++++ src/raytracing/rt_embree.cpp | 7 ++++--- src/viewer/che_viewer.cpp | 2 ++ 6 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/che.h b/include/che.h index 42ecb57b..90147f53 100644 --- a/include/che.h +++ b/include/che.h @@ -48,6 +48,9 @@ class che bool manifold = true; + public: + vertex * VN = nullptr; ///< vertex normals : v -> normal(v) + public: che(const size_t & n_v = 0, const size_t & n_f = 0); che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); @@ -65,6 +68,7 @@ class che area_t area_trig(const index_t & t) const; area_t area_vertex(const index_t & v); area_t area_surface() const; + vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; vertex normal_he(const index_t & he) const; vertex normal(const index_t & v) const; vertex gradient_he(const index_t & he, const distance_t *const & f) const; diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index a2a0c353..7c7422bb 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -4,6 +4,7 @@ #include "che.h" #include +#include #include @@ -16,6 +17,8 @@ namespace gproshan::rt { class raytracing { protected: + std::map geomID_mesh; + size_t width; size_t height; size_t n_samples; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index db7adf1d..e882c8a9 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -58,6 +58,13 @@ class embree : public raytracing return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); } + const glm::vec3 shading_normal(const che * mesh) const + { + vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + + return glm::normalize(glm::vec3(n.x, n.y, n.z)); + } + const glm::vec3 position() const { return org() + ray.tfar * dir(); diff --git a/src/che.cpp b/src/che.cpp index 313a50b1..dec1b0c7 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -238,6 +238,13 @@ area_t che::area_surface() const return area; } +vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const +{ + index_t he = f * che::P; + + return {u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]}; +} + vertex che::normal_he(const index_t & he) const { vertex n = (GT[VT[next(he)]] - GT[VT[he]]) * (GT[VT[prev(he)]] - GT[VT[he]]); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index c3fb7d49..edeeef5e 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -38,8 +38,8 @@ embree::~embree() void embree::build_bvh(const std::vector & meshes) { for(auto & m: meshes) - if(m->n_faces()) add_mesh(m); - else add_point_cloud(m); + if(m->n_faces()) geomID_mesh[add_mesh(m)] = m; + else geomID_mesh[add_point_cloud(m)] = m; rtcCommitScene(scene); } @@ -138,7 +138,8 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) glm::vec3 wi = normalize(light - r.position()); - float dot_wi_normal = glm::dot(wi, r.normal()); + //float dot_wi_normal = glm::dot(wi, r.normal()); + float dot_wi_normal = glm::dot(wi, r.shading_normal(geomID_mesh[r.hit.geomID])); if(dot_wi_normal < 0) dot_wi_normal = -dot_wi_normal; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index d0a89f3a..c70cf541 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -131,6 +131,8 @@ void che_viewer::update_vbo() void che_viewer::update_normals() { + mesh->VN = normals; + #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) if(invert_normals) From cfb1ded5bd3f8d29a03575e09efee3ae34889347 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 9 Mar 2020 16:42:26 +0100 Subject: [PATCH 0208/1018] fixed feature patch alignment --- include/mdict/patch.h | 2 +- src/mdict/patch.cpp | 46 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 4f5f6b13..022dc427 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -95,7 +95,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v); + bool add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double &sum, double deviation); private: diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index cf0b4c12..be116301 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -73,7 +73,7 @@ index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) if(indexes[i] == idx_global) return i; return -1; } -bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v) +bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double &sum, double deviation) { index_t a, b, i = 0; @@ -113,10 +113,10 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc gproshan_debug_var(N[i]);*/ //gproshan_debug_var(mesh->normal_he(he)); - if ( angle > tmp_angle && tmp_angle < thr_angle ) // Fullfill conditions + if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { angle = tmp_angle; - //gproshan_debug_var(he); + min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); added = true; @@ -127,6 +127,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc } //gproshan_debug_var(min_he); + sum += acos( (min_he, N[i]) ); N.push_back(min_he); return added; @@ -135,6 +136,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio) { + //radio = radio_; radio = -INFINITY; //che_sphere my_sphere(1,12); @@ -157,11 +159,12 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in vector N; N.push_back(n); //double angle; - //double sum_angle = 0; + double sum_angle = 0; double delta = 0; + //vertex prev_n, curr_n; //gproshan_debug_var(geo.n_sorted_index()); - + for(index_t i=1; inormal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders - if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/12, geo, mesh, indexes[i]) ) + + // add one new candidate vertex //first regulates variation, // second regulates size of the patch + if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/6, geo, mesh, indexes[i], sum_angle, PI/2.5 ) && sum_angle < PI ) + // pi is too much //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders { //gproshan_debug_var(vertices.size()); @@ -208,6 +214,34 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } + // Refit the points and update the radius + size_t d_fitting = 2; + size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + if(vertices.size() > min_points) + { + jet_fit_directions(mesh, v); + n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + radio = -INFINITY; + + for(index_t i=1; i < vertices.size(); i++) + { + p = mesh->get_vertex(indexes[i]); + c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); + + if(*p > radio) + { + radio = *p; + } + + } + + } + + + //gproshan_debug_var(sum_angle); /* gproshan_debug_var(PI/2.5); gproshan_debug_var(sum_angle); gproshan_debug_var(PI/(delta+0.05)*(vertices.size()-1));*/ From 4fe19073e087e759e352205020970ef9fd1ef18c Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 10 Mar 2020 11:11:41 +0100 Subject: [PATCH 0209/1018] fixed variational patch creation --- src/mdict/inpainting.cpp | 6 +++--- src/mdict/patch.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index d247ba37..e990a0e1 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -395,10 +395,10 @@ void inpainting::init_radial_feature_patches() //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(outliers.size()); + a_vec outlv(seeds.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; + for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; /*for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index be116301..ac64c9b1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -204,7 +204,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in //vertices.push_back(indexes[i]); //gproshan_debug_var(geo[indexes[i]]); //sum_angle += angle; - delta += 0.001 ; + // delta += 0.001 ; // sharp meshes 0.001 // smooth meshes 0.035 at max } From a96ff4bf43503163cc3e1dcfe502c295d480f8c0 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 10 Mar 2020 12:05:37 +0100 Subject: [PATCH 0210/1018] creating new save sampling --- include/mdict/patch.h | 5 +++- src/mdict/inpainting.cpp | 29 ++++++++++--------- src/mdict/patch.cpp | 62 ++++++---------------------------------- 3 files changed, 27 insertions(+), 69 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 022dc427..65b2c3e0 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -63,7 +63,10 @@ class patch void init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, - distance_t & euc_radio); + distance_t & euc_radio, + double delta, + double sum_thres); + void update_radial_disjoint(che * mesh, const index_t & v, vector & _vertices); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e990a0e1..62ac4959 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -4,6 +4,7 @@ #include #include #include +#define PI 3.14159265 // geometry processing and shape analysis framework @@ -138,6 +139,8 @@ void inpainting::load_mask(const std::vector * vertices, const index_t void inpainting::init_radial_patches() { + double delta = PI/6; + double sum_thres = PI; // ensure that M is large enough using the radio gproshan_log(Init radial patches); gproshan_log_var(M); @@ -171,7 +174,7 @@ void inpainting::init_radial_patches() // mask at the end // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio, delta, sum_thres); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -284,6 +287,14 @@ vector inpainting::sort_indexes(const vector &v) { void inpainting::init_radial_feature_patches() { + // saving sampling + double delta = PI/6; + double sum_thres = PI; + string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".rsampl"); + + gproshan_debug_var(f_sampl); + arma::uvec S; + //if(S.load(f_sampl)) // compute features will be seeds vector all_sorted_features; vector seeds; @@ -334,17 +345,16 @@ void inpainting::init_radial_feature_patches() const vertex & v_patch = mesh->gt(all_sorted_features[i]); const vertex & v_seed = mesh->gt(seeds[j]); - // 0.7 coverage parameter + // 0.5 coverage parameter if( *(v_patch - v_seed) < 0.5* radios[j] ) // radio of each patch found = true; j++; } if(!found) - { //it is a new patch - // get_radious + { patch p; // increasing a bit the radio - p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio); + p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, delta, sum_thres); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; @@ -353,7 +363,6 @@ void inpainting::init_radial_feature_patches() for(index_t k = 0; k < p.vertices.size(); k++) { if(!covered[ p.vertices[k] ]) count_cov_patch++; - //covered[ p.vertices[k] ] = 1; } count_cov += count_cov_patch; @@ -368,10 +377,6 @@ void inpainting::init_radial_feature_patches() covered[ p.vertices[k] ] = 1; } - - // gproshan_debug_var(euc_radio); - // gproshan_debug_var(indexes[i]); - // gproshan_debug_var(p.vertices.size()); } } @@ -400,11 +405,7 @@ void inpainting::init_radial_feature_patches() for(index_t i = 0; i < seeds.size(); i++) outlv(i) = seeds[i]; - /*for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; - */ outlv.save(f_points); - //gproshan_debug_var(features.size()); ////////////////////////////////////////////////////////////////////////////////// load_mask(); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index ac64c9b1..d220d77c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -87,31 +87,19 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc a = mesh->vt(next(he)); //index of the next vertex index_t b = mesh->vt(prev(he)); - /*gproshan_debug_var(geo[b]); - gproshan_debug_var(geo[a]); - gproshan_debug_var(geo[v ]); -*/ // If is an adjacent face if( geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) { i = find(indexes, nc,a); - //gproshan_debug_var(a); - //gproshan_debug_var(i); } else { i = find(indexes, nc, b); - //gproshan_debug_var(b); - //gproshan_debug_var(i); + } tmp_angle = acos( (mesh->normal_he(he), N[i]) ); - /* gproshan_debug_var(tmp_angle); - gproshan_debug_var(angle); - gproshan_debug_var(thr_angle); - gproshan_debug_var(N[i]);*/ - //gproshan_debug_var(mesh->normal_he(he)); if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { @@ -123,10 +111,9 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc } } - //gproshan_debug_var(N[i]); } - //gproshan_debug_var(min_he); + sum += acos( (min_he, N[i]) ); N.push_back(min_he); @@ -134,14 +121,10 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc } -void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio) +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio, double delta, double sum_thres) { - //radio = radio_; radio = -INFINITY; - //che_sphere my_sphere(1,12); - //string sphere_file = tmp_file_path("sphere"); - //che_off::write_file(&my_sphere, sphere_file); normal_fit_directions(mesh, v); @@ -160,10 +143,6 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in N.push_back(n); //double angle; double sum_angle = 0; - double delta = 0; - - //vertex prev_n, curr_n; - //gproshan_debug_var(geo.n_sorted_index()); for(index_t i=1; inormal(indexes[i]) ) ); - //angle = acos( (n, mesh->normal(indexes[i]) ) ) ; - - //prev_n = curr_n; - //curr_n = normal_trim(geo, mesh, indexes[i]); - //gproshan_debug_var(acos( (prev_n, curr_n) )); + + //if( angle < PI/2.5 && (sum_angle) <= delta * PI) - //penalize gowing, I want them to grow only if they do not vary so much - //if( angle < PI/2.5 && acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) ) <= PI/8) // find borders - + // add one new candidate vertex //first regulates variation, // second regulates size of the patch - if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), PI/6, geo, mesh, indexes[i], sum_angle, PI/2.5 ) && sum_angle < PI ) - // pi is too much - //if( angle < PI/2.5 && acos( (prev_n, curr_n) ) <= PI/7) // find borders + if( add_vertex_by_faces(N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], sum_angle, PI/2.5 ) && sum_angle < sum_thres ) { - //gproshan_debug_var(vertices.size()); - + if(*p > radio) { radio = *p; @@ -200,13 +168,6 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in p = mesh->get_vertex(indexes[i]); if(*(p - c) > euc_radio) euc_radio = *(p - c); - //gproshan_debug_var(euc_radio); - //vertices.push_back(indexes[i]); - //gproshan_debug_var(geo[indexes[i]]); - //sum_angle += angle; - // delta += 0.001 ; - // sharp meshes 0.001 - // smooth meshes 0.035 at max } else { @@ -240,14 +201,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } - - //gproshan_debug_var(sum_angle); -/* gproshan_debug_var(PI/2.5); - gproshan_debug_var(sum_angle); - gproshan_debug_var(PI/(delta+0.05)*(vertices.size()-1));*/ delete indexes; - //gproshan_debug_var(v); - } From c09092f2a3dbbf9031b8f45cb0a15601363c87a0 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Tue, 10 Mar 2020 17:08:51 +0100 Subject: [PATCH 0211/1018] saving sampling --- src/mdict/inpainting.cpp | 184 ++++++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 79 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 62ac4959..9a9af39e 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -292,10 +292,8 @@ void inpainting::init_radial_feature_patches() double sum_thres = PI; string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".rsampl"); - gproshan_debug_var(f_sampl); - arma::uvec S; - //if(S.load(f_sampl)) - // compute features will be seeds + arma::mat S; + vector all_sorted_features; vector seeds; size_t featsize; @@ -317,95 +315,123 @@ void inpainting::init_radial_feature_patches() //radio *= 1.1; gproshan_debug_var(max_radio); - patches_map.resize(mesh->n_vertices()); + if(!S.load(f_sampl)) + { + + // compute features will be seeds - //Coverage of the points - bool covered[mesh->n_vertices()]; + patches_map.resize(mesh->n_vertices()); - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; - } - distance_t euc_radio; - vector radios; - size_t count_cov = 0; - size_t count_cov_patch = 0; - distance_t over_factor = 2; + //Coverage of the points + bool covered[mesh->n_vertices()]; - for(size_t i = 0; i < all_sorted_features.size(); i++) - { - bool found = false; - size_t j = 0; + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + covered[i] = 0; + } + distance_t euc_radio; + vector radios; + size_t count_cov = 0; + size_t count_cov_patch = 0; + distance_t over_factor = 2; - //select the next one - while(j < seeds.size() && !found ) - { - //calculate distance between the actual patch p seed i and other features - const vertex & v_patch = mesh->gt(all_sorted_features[i]); - const vertex & v_seed = mesh->gt(seeds[j]); - - // 0.5 coverage parameter - if( *(v_patch - v_seed) < 0.5* radios[j] ) // radio of each patch - found = true; - j++; - } - if(!found) - { - patch p; - // increasing a bit the radio - p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, delta, sum_thres); - - //gproshan_debug_var(p.vertices.size()); - count_cov_patch = 0; - if(p.vertices.size() >= 7 ) - { - for(index_t k = 0; k < p.vertices.size(); k++) - { - if(!covered[ p.vertices[k] ]) count_cov_patch++; - } + for(size_t i = 0; i < all_sorted_features.size(); i++) + { + bool found = false; + size_t j = 0; - count_cov += count_cov_patch; - if(count_cov_patch > 0) + //select the next one + while(j < seeds.size() && !found ) + { + //calculate distance between the actual patch p seed i and other features + const vertex & v_patch = mesh->gt(all_sorted_features[i]); + const vertex & v_seed = mesh->gt(seeds[j]); + + // 0.5 coverage parameter + if( *(v_patch - v_seed) < 0.5* radios[j] ) // radio of each patch + found = true; + j++; + } + if(!found) + { + patch p; + // increasing a bit the radio + p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, delta, sum_thres); + + //gproshan_debug_var(p.vertices.size()); + count_cov_patch = 0; + if(p.vertices.size() >= 7 ) { - patches.push_back(p); - seeds.push_back(all_sorted_features[i]); - radios.push_back( euc_radio ); - count+=p.vertices.size(); - for(index_t k = 0; k < p.vertices.size(); k++) - covered[ p.vertices[k] ] = 1; - + { + if(!covered[ p.vertices[k] ]) count_cov_patch++; + } + + count_cov += count_cov_patch; + if(count_cov_patch > 0) + { + patches.push_back(p); + seeds.push_back(all_sorted_features[i]); + radios.push_back( euc_radio ); + count+=p.vertices.size(); + + for(index_t k = 0; k < p.vertices.size(); k++) + covered[ p.vertices[k] ] = 1; + + } } - } + + } + } - } - } - - vector outliers; - gproshan_debug_var(count); - gproshan_debug_var(count_cov); - gproshan_debug_var(seeds.size()); - M = seeds.size(); + vector outliers; + gproshan_debug_var(count); + gproshan_debug_var(count_cov); + gproshan_debug_var(seeds.size()); + M = seeds.size(); -/////////////////////////////////////// - - gproshan_debug_var(M); - - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) + /////////////////////////////////////// + + gproshan_debug_var(M); + + for(index_t i = 0; i < mesh->n_vertices(); i++) { - outliers.push_back(i); - //gproshan_debug_var(geo[indexes[i]] ); + if(!covered[i] ) + { + outliers.push_back(i); + //gproshan_debug_var(geo[indexes[i]] ); + } } + a_vec outlv(seeds.size()); + gproshan_debug_var(outliers.size()); + for(index_t i = 0; i < seeds.size(); i++) + outlv(i) = seeds[i]; + + outlv.save(f_points); + S.resize(seeds.size(),2); + for(index_t i = 0; i < seeds.size(); i++) + { + S(i,0) = seeds[i]; + S(i,1) = radios[i]; + } + S.save(f_sampl); } - a_vec outlv(seeds.size()); - gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; + else + { + size_t n_seeds = S.n_rows; + for(index_t i = 0; i < n_seeds; i++) + { + patch p; + p.init_radial_disjoint(mesh, max_radio, S(i,0), S(i,1), delta, sum_thres); + patches.push_back(p); + } + M = n_seeds; + gproshan_debug_var(n_seeds); - outlv.save(f_points); + + } + ////////////////////////////////////////////////////////////////////////////////// load_mask(); From d8c1160262005975bf9b87f63ffff501b7daa8e7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 10 Mar 2020 17:15:05 +0100 Subject: [PATCH 0212/1018] select vertices per mesh --- include/raytracing/rt_embree.h | 2 + include/viewer/viewer.h | 2 - src/app_viewer.cpp | 68 +++++++++++++++++----------------- src/viewer/viewer.cpp | 30 ++++++--------- 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index e882c8a9..1ef1e976 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -60,6 +60,8 @@ class embree : public raytracing const glm::vec3 shading_normal(const che * mesh) const { +// if(hit.primID == RTC_INVALID_GEOMETRY_ID) return normal(); + vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::normalize(glm::vec3(n.x, n.y, n.z)); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index d2508872..b3cd0e1a 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -145,7 +145,6 @@ class viewer public: - std::vector select_vertices; std::vector other_vertices; std::vector vectors; std::vector sub_menus; @@ -169,7 +168,6 @@ class viewer void init_imgui(); void init_menus(); void init_glsl(); - void update_vbo(); void render_gl(); void render_embree(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 89e6735d..59a596cc 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -151,10 +151,10 @@ void app_viewer::process_delete_vertices(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) return; + if(!view->mesh().selected.size()) return; gproshan_debug(removing vertex); - mesh->remove_vertices(view->select_vertices); - view->select_vertices.clear(); + mesh->remove_vertices(view->mesh().selected); + view->mesh().selected.clear(); gproshan_debug(removing vertex); } @@ -415,11 +415,11 @@ void app_viewer::process_key_points(viewer * p_view) key_points kps(mesh); - view->select_vertices.clear(); - view->select_vertices.reserve(kps.size()); + view->mesh().selected.clear(); + view->mesh().selected.reserve(kps.size()); for(index_t i = 0; i < kps.size(); i++) - view->select_vertices.push_back(kps[i]); + view->mesh().selected.push_back(kps[i]); } void app_viewer::process_key_components(viewer * p_view) @@ -451,7 +451,7 @@ void app_viewer::process_mdict_patch(viewer * p_view) vertex vdir; patch p; distance_t mean_edge = mesh->mean_edge(); - for(auto & v: view->select_vertices) + for(auto & v: view->mesh().selected) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) @@ -481,7 +481,7 @@ void app_viewer::process_mdict_patch(viewer * p_view) avg_nvp += p.vertices.size(); } - avg_nvp /= view->select_vertices.size(); + avg_nvp /= view->mesh().selected.size(); gproshan_debug_var(avg_nvp); delete [] toplevel; @@ -560,7 +560,7 @@ void app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); -// mesh_iterative_inpaiting(mesh, view->select_vertices, freq, rt, m, M, f, learn); +// mesh_iterative_inpaiting(mesh, view->mesh().selected, freq, rt, m, M, f, learn); } void app_viewer::process_multiplicate_vertices(viewer * p_view) @@ -579,13 +579,13 @@ void app_viewer::compute_toplesets(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); index_t * toplesets = new index_t[mesh->n_vertices()]; index_t * sorted = new index_t[mesh->n_vertices()]; vector limites; - mesh->compute_toplesets(toplesets, sorted, limites, view->select_vertices); + mesh->compute_toplesets(toplesets, sorted, limites, view->mesh().selected); size_t n_toplesets = limites.size() - 1; @@ -610,9 +610,9 @@ void app_viewer::process_voronoi(viewer * p_view) TIC(view->time) #ifdef GPROSHAN_CUDA - geodesics ptp(mesh, view->select_vertices, geodesics::PTP_GPU, nullptr, 1); + geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_GPU, nullptr, 1); #else - geodesics ptp(mesh, view->select_vertices, geodesics::FM, nullptr, 1); + geodesics ptp(mesh, view->mesh().selected, geodesics::FM, nullptr, 1); #endif TOC(view->time) gproshan_log_var(view->time); @@ -621,7 +621,7 @@ void app_viewer::process_voronoi(viewer * p_view) for(index_t i = 0; i < mesh->n_vertices(); i++) { mesh.color(i) = ptp.clusters[i]; - mesh.color(i) /= view->select_vertices.size() + 1; + mesh.color(i) /= view->mesh().selected.size() + 1; } } @@ -638,13 +638,13 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) double time_fps; TIC(view->time) - radio = farthest_point_sampling_ptp_gpu(mesh, view->select_vertices, time_fps, NIL, radio); + radio = farthest_point_sampling_ptp_gpu(mesh, view->mesh().selected, time_fps, NIL, radio); TOC(view->time) gproshan_log_var(time_fps); #endif // GPROSHAN_CUDA gproshan_log_var(radio); - gproshan_log_var(view->select_vertices.size()); + gproshan_log_var(view->mesh().selected.size()); gproshan_log_var(view->time); } @@ -659,7 +659,7 @@ void app_viewer::process_farthest_point_sampling(viewer * p_view) distance_t radio; TIC(view->time) - load_sampling(view->select_vertices, radio, mesh, n); + load_sampling(view->mesh().selected, radio, mesh, n); TOC(view->time) gproshan_log_var(view->time); } @@ -706,11 +706,11 @@ void app_viewer::process_geodesics_fm(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); TIC(view->time) - geodesics fm(mesh, view->select_vertices); + geodesics fm(mesh, view->mesh().selected); TOC(view->time) gproshan_log_var(view->time); @@ -723,11 +723,11 @@ void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); TIC(view->time) - geodesics ptp(mesh, view->select_vertices, geodesics::PTP_CPU); + geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_CPU); TOC(view->time) gproshan_log_var(view->time); @@ -740,11 +740,11 @@ void app_viewer::process_geodesics_heat_flow(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->select_vertices, geodesics::HEAT_FLOW); + geodesics heat_flow(mesh, view->mesh().selected, geodesics::HEAT_FLOW); TOC(view->time) gproshan_log_var(view->time); @@ -760,8 +760,8 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); if(view->n_dist != mesh->n_vertices()) { @@ -772,7 +772,7 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) } TIC(view->time) - geodesics ptp(mesh, view->select_vertices, geodesics::PTP_GPU, view->dist); + geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_GPU, view->dist); TOC(view->time) gproshan_log_var(view->time); @@ -785,11 +785,11 @@ void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->select_vertices.size()) - view->select_vertices.push_back(0); + if(!view->mesh().selected.size()) + view->mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->select_vertices, geodesics::HEAT_FLOW_GPU); + geodesics heat_flow(mesh, view->mesh().selected, geodesics::HEAT_FLOW_GPU); TOC(view->time) gproshan_log_var(view->time); @@ -908,7 +908,7 @@ void app_viewer::select_multiple(viewer * p_view) stringstream ss(line); index_t v; while(ss >> v) - view->select_vertices.push_back(v); + view->mesh().selected.push_back(v); } } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c6f4903c..ca0faf15 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -290,12 +290,6 @@ void viewer::init_glsl() shader_pointcloud.load_fragment("../shaders/fragment_pointcloud.glsl"); } -void viewer::update_vbo() -{ - for(index_t i = 0; i < n_meshes; i++) - meshes[i].update(); -} - void viewer::menu_process(int value) { menu_process(processes[value].function); @@ -354,7 +348,6 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a void viewer::menu_meshes(int value) { current = value; - select_vertices.clear(); glfwSetWindowTitle(window, mesh()->filename().c_str()); } @@ -408,7 +401,7 @@ void viewer::menu_process(function_t pro) { if(pro) pro(this); - update_vbo(); + mesh().update_vbo(); } void viewer::menu_help(viewer * view) @@ -420,13 +413,12 @@ void viewer::menu_help(viewer * view) void viewer::menu_reset_mesh(viewer * view) { - view->select_vertices.clear(); + view->mesh().selected.clear(); view->other_vertices.clear(); view->vectors.clear(); view->mesh().reload(); - - view->update_vbo(); + view->mesh().update_vbo(); } void viewer::menu_save_mesh(viewer * view) @@ -479,7 +471,7 @@ void viewer::invert_orientation(viewer * view) { view->mesh().invert_orientation(); view->mesh().update_normals(); - view->update_vbo(); + view->mesh().update_vbo(); } void viewer::set_render_gl(viewer * view) @@ -520,7 +512,7 @@ void viewer::set_render_normal_field(viewer * view) void viewer::set_render_border(viewer * view) { view->render_border = !view->render_border; - if(!view->render_border) view->select_vertices.clear(); + if(!view->render_border) view->mesh().selected.clear(); } void viewer::set_render_lines(viewer * view) @@ -681,12 +673,12 @@ void viewer::draw_meshes(shader & program) void viewer::draw_selected_vertices(shader & program) { - if(sphere_translations.size() != select_vertices.size()) + if(sphere_translations.size() != mesh().selected.size()) { - sphere_translations.resize(select_vertices.size()); + sphere_translations.resize(mesh().selected.size()); - for(index_t i = 0; i < select_vertices.size(); i++) - sphere_translations[i] = mesh()->gt(select_vertices[i]); + for(index_t i = 0; i < mesh().selected.size(); i++) + sphere_translations[i] = mesh()->gt(mesh().selected[i]); sphere.update_instances_translations(sphere_translations); } @@ -701,10 +693,10 @@ void viewer::draw_selected_vertices(shader & program) void viewer::select_border_vertices() { - select_vertices.clear(); + mesh().selected.clear(); for(index_t b = 0; b < mesh()->n_borders(); b++) for_border(he, mesh(), mesh()->bt(b)) - select_vertices.push_back(mesh()->vt(he)); + mesh().selected.push_back(mesh()->vt(he)); } void viewer::pick_vertex(int x, int y) From 2bb60ab2ed5c7c2862b656ed94fb7307e0b02e4d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Mar 2020 11:14:41 +0100 Subject: [PATCH 0213/1018] fix select mesh menu --- include/viewer/viewer.h | 1 - src/viewer/viewer.cpp | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index b3cd0e1a..98d2d355 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -181,7 +181,6 @@ class viewer static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); // menu functions - void menu_meshes(int value); void menu_process(int value); void menu_process(function_t pro); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ca0faf15..3d618f95 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -138,7 +138,11 @@ bool viewer::run() { for(index_t i = 0; i < n_meshes; i++) if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str())) + { current = i; + sphere_translations.clear(); + glfwSetWindowTitle(window, mesh()->filename().c_str()); + } ImGui::EndMenu(); } @@ -310,8 +314,9 @@ void viewer::add_mesh(const vector & _meshes) for(che * _mesh: _meshes) { assert(n_meshes < N_MESHES); - meshes[n_meshes++].init(_mesh); - meshes[n_meshes - 1].log_info(); + meshes[n_meshes].init(_mesh); + meshes[n_meshes].log_info(); + n_meshes++; } if(!n_meshes) return; @@ -345,12 +350,6 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a glClearColor(view->bgc, view->bgc, view->bgc, 1.); } -void viewer::menu_meshes(int value) -{ - current = value; - glfwSetWindowTitle(window, mesh()->filename().c_str()); -} - void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); From 26aee2a7d547edc7fc5193da01240bb516f4c5c5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Mar 2020 12:11:17 +0100 Subject: [PATCH 0214/1018] update vbo --- src/app_viewer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 59a596cc..58ed019c 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -284,6 +284,8 @@ void app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) view->mesh().color(v) = eigvec(v, k); + + view->mesh().update_vbo(); } view->current = 0; From af416c78c242745efa05ed78faa3d8c9a527e871 Mon Sep 17 00:00:00 2001 From: lizeth Date: Wed, 11 Mar 2020 15:32:41 +0100 Subject: [PATCH 0215/1018] new load sampling function --- include/mdict/patch.h | 6 +++++- src/mdict/inpainting.cpp | 20 +++++++++-------- src/mdict/mdict.cpp | 5 +++-- src/mdict/patch.cpp | 46 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 65b2c3e0..6e05f7f6 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -64,8 +64,12 @@ class patch const distance_t & radio_, const index_t & v, distance_t & euc_radio, - double delta, + distance_t & geo_radio, + double delta, double sum_thres); + void recover_radial_disjoint(che * mesh, + const distance_t & radio_, + const index_t & v); void update_radial_disjoint(che * mesh, const index_t & v, diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 9a9af39e..eff5e27e 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -174,7 +174,7 @@ void inpainting::init_radial_patches() // mask at the end // if(!covered[sample(it)]) { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio, delta, sum_thres); + patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio, radio, delta, sum_thres); for(auto i:patches[s].vertices) if(!covered[i]) { @@ -248,13 +248,11 @@ void inpainting::init_radial_patches() gproshan_debug_var(patch_max_size); #endif } - gproshan_debug(resettt); + bool * pmask = mask; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - gproshan_debug(passed); - //#pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -312,6 +310,7 @@ void inpainting::init_radial_feature_patches() geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; distance_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; + //radio *= 1.1; gproshan_debug_var(max_radio); @@ -331,7 +330,9 @@ void inpainting::init_radial_feature_patches() covered[i] = 0; } distance_t euc_radio; + distance_t geo_radio; vector radios; + vector geo_radios; size_t count_cov = 0; size_t count_cov_patch = 0; distance_t over_factor = 2; @@ -357,7 +358,7 @@ void inpainting::init_radial_feature_patches() { patch p; // increasing a bit the radio - p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, delta, sum_thres); + p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; @@ -374,6 +375,7 @@ void inpainting::init_radial_feature_patches() patches.push_back(p); seeds.push_back(all_sorted_features[i]); radios.push_back( euc_radio ); + geo_radios.push_back( geo_radio); count+=p.vertices.size(); for(index_t k = 0; k < p.vertices.size(); k++) @@ -413,7 +415,7 @@ void inpainting::init_radial_feature_patches() for(index_t i = 0; i < seeds.size(); i++) { S(i,0) = seeds[i]; - S(i,1) = radios[i]; + S(i,1) = geo_radios[i]; } S.save(f_sampl); } @@ -423,8 +425,8 @@ void inpainting::init_radial_feature_patches() for(index_t i = 0; i < n_seeds; i++) { patch p; - p.init_radial_disjoint(mesh, max_radio, S(i,0), S(i,1), delta, sum_thres); - patches.push_back(p); + p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); + patches.push_back(p); } M = n_seeds; gproshan_debug_var(n_seeds); @@ -443,7 +445,7 @@ void inpainting::init_radial_feature_patches() patches_map.resize(n_vertices); //initializing patch gproshan_debug_var(M); - #pragma omp parallel + //#pragma omp parallel { #ifndef NDEBUG size_t patch_avg_size = 0; diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 3102dde6..9d60e4c9 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -293,6 +293,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) #pragma omp parallel for private(omega, a, aj, D, e, sum, sum_error) for(index_t j = 0; j < A.n_cols; j++) { + //Taking all alphas that uses atom j arma::uvec omega = find(abs(alpha.row(j)) > 0); sum.zeros(K, K); @@ -303,8 +304,8 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) a = alpha.col(i); a(j) = 0; - D = patches[i].phi * A; - e = patches[i].xyz.row(2).t() - D * a; + D = patches[i].phi * A; // fetch the discrete dictionary for the patch i + e = patches[i].xyz.row(2).t() - D * a; // getting the rec error for the patch i aj = as_scalar(e.t() * D.col(j) / (D.col(j).t() * D.col(j))); sum += aj * aj * patches[i].phi.t() * patches[i].phi; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d220d77c..180fa880 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -120,8 +120,50 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc return added; } +void patch::recover_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v) +{ + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ ); + index_t * indexes = new index_t[geo.n_sorted_index()]; + geo.copy_sorted_index(indexes, geo.n_sorted_index()); + + vertices.push_back(v); + + for(index_t i=1; i min_points) + { + jet_fit_directions(mesh, v); + n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + radio = -INFINITY; + + for(index_t i=1; i < vertices.size(); i++) + { + p = mesh->get_vertex(indexes[i]); + c = mesh->get_vertex(v); // central vertices + + p = p - c ; + p = p - ((p,n)*n); -void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio, double delta, double sum_thres) + if(*p > radio) + { + radio = *p; + } + } + } + else + { + gproshan_debug_var(vertices.size()); + } + +} + +void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v, distance_t & euc_radio, distance_t & geo_radio, double delta, double sum_thres) { radio = -INFINITY; @@ -175,6 +217,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } + // Refit the points and update the radius size_t d_fitting = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; @@ -200,6 +243,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in } } + geo_radio = geo[indexes [vertices.size()-1]]; delete indexes; } From c3cbc5d1266735a941f8d86cfb9e8b8924c18940 Mon Sep 17 00:00:00 2001 From: lizeth Date: Thu, 12 Mar 2020 12:59:03 +0100 Subject: [PATCH 0216/1018] fixing load sampling --- src/app_viewer.cpp | 6 +++--- src/mdict/patch.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 70474283..5aef4f08 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -571,8 +571,8 @@ void app_viewer::process_mask(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t avg_p = 20; - size_t percentage; + size_t avg_p = 36; + size_t percentage = 0; size_t n=12; // dct size_t m = 144, M = 0; @@ -582,7 +582,7 @@ void app_viewer::process_mask(viewer * p_view) gproshan_input(avg_p percentage f ); //cin >> avg_p >> percentage >> f; - cin>> avg_p >> percentage; + //cin>> avg_p >> percentage; basis * phi = new basis_dct(n); inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 180fa880..2d75afa0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -122,7 +122,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc void patch::recover_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v) { - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ ); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 2.e-5); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); From 51c9b262f2b386fd7b2391df118e26dc2f66ad6d Mon Sep 17 00:00:00 2001 From: lizeth Date: Thu, 12 Mar 2020 15:35:50 +0100 Subject: [PATCH 0217/1018] Cleaning code --- include/mdict/inpainting.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 5bd0cf17..02286b93 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -25,7 +25,6 @@ class inpainting : public dictionary void load_mask(const std::vector * vertices, const index_t * clusters); void load_mask(); void init_voronoi_patches(); - void init_radial_patches(); void init_radial_feature_patches(); vector sort_indexes(const vector &v); From 7b4abe94ff4812883f89a1d7e0cd445b98cf00d5 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 16 Mar 2020 11:15:23 +0100 Subject: [PATCH 0218/1018] adding area restriction --- src/mdict/inpainting.cpp | 150 +-------------------------------------- src/mdict/patch.cpp | 13 +++- 2 files changed, 12 insertions(+), 151 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index eff5e27e..92b48459 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -136,158 +136,12 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } - -void inpainting::init_radial_patches() - { - double delta = PI/6; - double sum_thres = PI; - // ensure that M is large enough using the radio - gproshan_log(Init radial patches); - gproshan_log_var(M); - std::vector vertices[M]; - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - //sampling - size_t s; - patches.resize(M); - patches_map.resize(n_vertices); - - - bool covered[mesh->n_vertices()]; - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; - } - - s = 0; - size_t it = 0; - distance_t radio; - while(it < M) - { - - //Choose a sample and get the points neighboring points - // Check the points are inside and add them - // while( ) - // mask at the end - // if(!covered[sample(it)]) - { - patches[s].init_radial_disjoint(mesh, phi_basis->get_radio(), sample(it), radio, radio, delta, sum_thres); - for(auto i:patches[s].vertices) - if(!covered[i]) - { - covered[i] = 1; - } - - //gproshan_debug_var(patches[s].vertices.size()); - //gproshan_debug_var(sample(it)); - //gproshan_debug_var(it); - s++; - } - it++; - } - - vector outliers; - string f_points = tmp_file_path(mesh->name_size() + ".points"); - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) - { - outliers.push_back(i); - } - - } - a_vec outlv(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - { - outlv(i) = outliers[i]; - } - outlv.save(f_points); - - - //assert(outliers.size()==0); - M = s; // updating number of vertices - - gproshan_debug_var(M); - - gproshan_debug(finished); - - //mask at the end no need to call the function - - load_mask(); - - //Initializing patches - gproshan_log(initializing patches); - - patches.resize(M); - patches_map.resize(n_vertices); - //initializing patch - gproshan_debug_var(M); - #pragma omp parallel - { - #ifndef NDEBUG - size_t patch_avg_size = 0; - size_t patch_min_size = NIL; - size_t patch_max_size = 0; - - #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) - patch_avg_size += patches[s].vertices.size(); - #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); - #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); - - patch_avg_size /= M; - gproshan_debug_var(patch_avg_size); - gproshan_debug_var(patch_min_size); - gproshan_debug_var(patch_max_size); - #endif - } - - bool * pmask = mask; - for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - - //#pragma omp parallel for - for(index_t s = 0; s < M; s++) - { - patch & p = patches[s]; - p.transform(); - p.scale_xyz(phi_basis->get_radio()); - p.compute_avg_distance(); - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); - } - gproshan_log(radial patches are ready); - -} - -vector inpainting::sort_indexes(const vector &v) { - - // initialize original index locations - vector idx(v.size()); - iota(idx.begin(), idx.end(), 0); - - // sort indexes based on comparing values in v - // using std::stable_sort instead of std::sort - // to avoid unnecessary index re-orderings - // when v contains elements of equal values - stable_sort(idx.begin(), idx.end(), - [&v](index_t i1, index_t i2) {return v[i1] < v[i2];}); - - return idx; -} - void inpainting::init_radial_feature_patches() { // saving sampling double delta = PI/6; - double sum_thres = PI; + double sum_thres = 0.0005; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow + //double sum_thres = PI; string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".rsampl"); arma::mat S; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2d75afa0..0d911adb 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -78,6 +78,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc index_t a, b, i = 0; vertex min_he; + double area_face = 0; double angle = PI; double tmp_angle; bool added = false; @@ -104,7 +105,8 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { angle = tmp_angle; - + //gproshan_debug_var(he); + area_face = mesh->area_trig(he/3); min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); added = true; @@ -114,7 +116,8 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc } - sum += acos( (min_he, N[i]) ); + sum += area_face; + //sum += acos( (min_he, N[i]) ); N.push_back(min_he); return added; @@ -185,6 +188,7 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in N.push_back(n); //double angle; double sum_angle = 0; + double area_mesh = mesh->area_surface(); for(index_t i=1; i radio) From 97010bf8247f1fc33ead41c307e5c3f87d108503 Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 16 Mar 2020 13:07:10 +0100 Subject: [PATCH 0219/1018] setout for point cloud reconstruction --- include/mdict/dictionary.h | 1 + include/mdict/inpainting.h | 8 ++- include/mdict/patch.h | 7 --- src/app_viewer.cpp | 32 +++++----- src/mdict/dictionary.cpp | 4 ++ src/mdict/inpainting.cpp | 19 ++++-- src/mdict/patch.cpp | 123 ------------------------------------- 7 files changed, 42 insertions(+), 152 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 45e996b1..109a702a 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -73,6 +73,7 @@ class dictionary distance_t mesh_reconstruction(const fmask_t & mask = nullptr); void update_alphas(a_mat & alpha, size_t threshold); + void save_alpha(string file); index_t sample(const index_t & s); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 02286b93..69b4f7e8 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -8,7 +8,7 @@ #include "geodesics.h" #include "geodesics_ptp.h" #include - +#define PI 3.14159265 // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -18,7 +18,8 @@ namespace gproshan::mdict { class inpainting : public dictionary { public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, const bool & _plot = false); + inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, + const distance_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , const bool & _plot = false); virtual ~inpainting() = default; distance_t execute(); @@ -26,6 +27,7 @@ class inpainting : public dictionary void load_mask(); void init_voronoi_patches(); void init_radial_feature_patches(); + void point_cloud_reconstruction(); vector sort_indexes(const vector &v); @@ -33,6 +35,8 @@ class inpainting : public dictionary private: size_t avg_p; size_t percent; + double delta; + double sum_thres; bool * mask; }; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 6e05f7f6..391d05df 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -71,13 +71,6 @@ class patch const distance_t & radio_, const index_t & v); - void update_radial_disjoint(che * mesh, - const index_t & v, - vector & _vertices); - void init_curvature_growing(che * mesh, - const index_t & v, - a_mat & normals); - void transform(); void itransform(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 5aef4f08..654e96a6 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -1,6 +1,7 @@ #include "app_viewer.h" #include +#define PI 3.14159265 using namespace std; using namespace gproshan::mdict; @@ -550,14 +551,16 @@ void app_viewer::process_inpaiting(viewer * p_view) size_t m, M; distance_t f; bool learn; - size_t avg_p; - size_t percentage; + size_t avg_p = 36; + size_t percentage = 0; + double delta = PI/6; + double sum_thres; - gproshan_input(n m M f learn avg_p percentage); - cin >> n >> m >> M >> f >> learn >> avg_p >> percentage; + gproshan_input(n m M f learn sum_thres); + cin >> n >> m >> M >> f >> learn >> sum_thres; basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); dict.execute(); delete phi; mesh.update_colors(&dict[0]); @@ -571,21 +574,21 @@ void app_viewer::process_mask(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t avg_p = 36; - size_t percentage = 0; - - size_t n=12; // dct + size_t n = 12; // dct size_t m = 144, M = 0; distance_t f = 1; bool learn = 0; + size_t avg_p = 36; + size_t percentage = 0; + double delta = PI/6; + double sum_thres; + gproshan_input(sum_thres ); + cin >> sum_thres; - gproshan_input(avg_p percentage f ); - //cin >> avg_p >> percentage >> f; - //cin>> avg_p >> percentage; basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage); - + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; @@ -694,7 +697,6 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); gproshan_input(radio); distance_t radio; cin >> radio; diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index a18aa1af..f8b36673 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -389,6 +389,10 @@ void dictionary::draw_patches(index_t i) phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz, i); } +void dictionary::save_alpha(string file) +{ + alpha.save(file); +} } // namespace gproshan::mdict diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 92b48459..ec8f8c56 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -12,8 +12,11 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _learn, size_t _avg_p, size_t _perc, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, + const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { + delta = _delta; + sum_thres = _sum_thres; avg_p = _avg_p; //size avg of number of vertices per patch percent = _perc; // mask percentage M = mesh->n_vertices()/avg_p; @@ -139,8 +142,8 @@ void inpainting::load_mask(const std::vector * vertices, const index_t void inpainting::init_radial_feature_patches() { // saving sampling - double delta = PI/6; - double sum_thres = 0.0005; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow + //double delta = PI/6; + //double sum_thres = 0.008; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow //double sum_thres = PI; string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".rsampl"); @@ -189,7 +192,6 @@ void inpainting::init_radial_feature_patches() vector geo_radios; size_t count_cov = 0; size_t count_cov_patch = 0; - distance_t over_factor = 2; for(size_t i = 0; i < all_sorted_features.size(); i++) { @@ -242,6 +244,7 @@ void inpainting::init_radial_feature_patches() } vector outliers; + gproshan_debug_var(sum_thres); gproshan_debug_var(count); gproshan_debug_var(count_cov); gproshan_debug_var(seeds.size()); @@ -455,7 +458,8 @@ distance_t inpainting::execute() // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".alpha"); + save_alpha(f_alpha); //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) @@ -498,6 +502,11 @@ distance_t inpainting::execute() gproshan_debug_var(d_time); } +void inpainting::point_cloud_reconstruction() +{ + +} + distance_t inpainting::execute_tmp() { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 0d911adb..9004dd45 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -255,129 +255,6 @@ void patch::init_radial_disjoint(che * mesh, const distance_t & radio_, const in delete indexes; } - -void patch::update_radial_disjoint(che * mesh, const index_t & v, vector & _vertices) -{ - a_vec vn = T.col(2);// normal at the center - vertex n; - n.x = vn(0); n.y = vn(1); n.z = vn(2); - vertex p, c; - - if(vertices.size() >= 7) - { - radio = 0; - vertices = std::move(_vertices); - for(size_t i=1; i < vertices.size(); i++) - { - p = mesh->get_vertex(vertices[i]); - c = mesh->get_vertex(v); // central vertices - - p = p - c ; - p = p - ((p,n)*n); - if(*p > radio) - { - radio = *p; - } - } - jet_fit_directions(mesh, v); - - } - else - { - gproshan_debug_var(v); - gproshan_debug_var(_vertices.size()); - } - -} - -void patch::init_curvature_growing(che * mesh, const index_t & v, a_mat & normals) -{ - radio = -INFINITY; - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, mesh->n_vertices()); - index_t * indexes = new index_t[geo.n_sorted_index()]; - geo.copy_sorted_index(indexes, geo.n_sorted_index()); - a_vec vn = normals.col(v); - vertex n = mesh->normal( v ) ; - //n.x = vn(0); n.y = vn(1); n.z = vn(2); -// gproshan_debug_var(geo.n_sorted_index()); - - vertices.push_back(v); - - for(size_t i=1; iget_vertex(indexes[i]); - vertex c = mesh->get_vertex(v); // central vertices - - //gather the good ones - /*gproshan_debug_var(i); - gproshan_debug_var(acos((n, mesh->normal(indexes[i]))) ); - gproshan_debug_var(PI/2);*/ - // - if( acos((n, mesh->normal(indexes[i]) )) <= PI/2 ) - { - vertices.push_back(indexes[i]); - } - else - { - break; - } - - - } - - //gproshan_debug_var(vertices.size()); - size_t d_fitting = 2; - size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - if(vertices.size() <= min_points ) - { - vertices.clear(); - } - else - { - jet_fit_directions(mesh, v); - size_t n_vertices = vertices.size(); - vertices.clear(); - vertices.push_back(v); - - vn = T.col(2);// normal at the center - n.x = vn(0); n.y = vn(1); n.z = vn(2); - - for(size_t i=1; i < n_vertices; i++) - { - vertex p = mesh->get_vertex(indexes[i]); - vertex c = mesh->get_vertex(v); // central vertices - - p = p - c ; - p = p - ((p,n)*n); - - if( acos( (n, mesh->normal(indexes[i]) ) ) <= PI/2 ) - //if( (n, mesh->normal(indexes[i])) >= 0 ) // zerear los otroso - { - if(*p > radio) - radio = *p; - vertices.push_back(indexes[i]); - } - else - { - break; - } - - } - // gproshan_debug(after); - // gproshan_debug_var(vertices.size()); - if(vertices.size() <= min_points ) - { - vertices.clear(); - } - - - } - - -} // xyz = E.t * (xyz - avg) void patch::transform() { From fef139c987e47beddcaa1b2f8dba7b577d74cde9 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 16 Mar 2020 13:35:25 +0100 Subject: [PATCH 0220/1018] fixing viewer --- src/app_viewer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 654e96a6..b3b6f9d6 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -695,9 +695,10 @@ void app_viewer::process_voronoi(viewer * p_view) void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - + che_viewer & mesh = view->mesh(); gproshan_input(radio); distance_t radio; cin >> radio; From 21aa59447ab031de1e0a6d92bcdda14926f7bfa7 Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 16 Mar 2020 13:48:22 +0100 Subject: [PATCH 0221/1018] separating sampling --- include/mdict/inpainting.h | 1 + src/mdict/inpainting.cpp | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 69b4f7e8..fe230408 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -27,6 +27,7 @@ class inpainting : public dictionary void load_mask(); void init_voronoi_patches(); void init_radial_feature_patches(); + void load_sampling(bool save_all); void point_cloud_reconstruction(); vector sort_indexes(const vector &v); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ec8f8c56..29fdb275 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -138,8 +138,7 @@ void inpainting::load_mask(const std::vector * vertices, const index_t } - -void inpainting::init_radial_feature_patches() +void inpainting::load_sampling(bool save_all) { // saving sampling //double delta = PI/6; @@ -290,9 +289,13 @@ void inpainting::init_radial_feature_patches() } +} + +void inpainting::init_radial_feature_patches() +{ + load_sampling(true); - ////////////////////////////////////////////////////////////////////////////////// load_mask(); //Initializing patches @@ -504,7 +507,7 @@ distance_t inpainting::execute() void inpainting::point_cloud_reconstruction() { - + } From 44e39050c7f008c99b96f3049b133ba7c14aa68f Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 16 Mar 2020 14:34:13 +0100 Subject: [PATCH 0222/1018] saving all sampling --- src/mdict/inpainting.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 29fdb275..ef03fc47 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -274,6 +274,8 @@ void inpainting::load_sampling(bool save_all) S(i,1) = geo_radios[i]; } S.save(f_sampl); + + } else { @@ -294,7 +296,7 @@ void inpainting::load_sampling(bool save_all) void inpainting::init_radial_feature_patches() { - load_sampling(true); + load_sampling(false); load_mask(); @@ -346,6 +348,31 @@ void inpainting::init_radial_feature_patches() p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); } + + bool save_all = true; + if(save_all) + { + arma::mat AS; + AS.resize(M,11); + for(index_t i = 0; i < M; i++) + { + patch & p = patches[i]; + AS(i,0) = p.vertices[0]; + AS(i,1) = p.radio; + AS(i,2) = p.T(0,0); + AS(i,3) = p.T(1,0); + AS(i,4) = p.T(2,0); + AS(i,5) = p.T(0,1); + AS(i,6) = p.T(1,1); + AS(i,7) = p.T(2,1); + AS(i,8) = p.T(0,2); + AS(i,9) = p.T(1,2); + AS(i,10) = p.T(2,2); + + } + string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".allsmp"); + AS.save(f_samplall); + } gproshan_log(radial patches are ready); } From 2deda2da10886bc9905fa8fa09b2108309f6d73f Mon Sep 17 00:00:00 2001 From: lizeth Date: Mon, 16 Mar 2020 15:08:01 +0100 Subject: [PATCH 0223/1018] point cloud rec --- src/mdict/inpainting.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index ef03fc47..1370a2d1 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -534,7 +534,33 @@ distance_t inpainting::execute() void inpainting::point_cloud_reconstruction() { - + arma::mat AS; + + string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".allsmp"); + AS.load(f_samplall); + //create patches + // recover points + // save points + // show and put seeds on it + /* + AS.resize(M,11); + for(index_t i = 0; i < M; i++) + { + patch & p = patches[i]; + AS(i,0) = p.vertices[0]; + AS(i,1) = p.radio; + AS(i,2) = p.T(0,0); + AS(i,3) = p.T(1,0); + AS(i,4) = p.T(2,0); + AS(i,5) = p.T(0,1); + AS(i,6) = p.T(1,1); + AS(i,7) = p.T(2,1); + AS(i,8) = p.T(0,2); + AS(i,9) = p.T(1,2); + AS(i,10) = p.T(2,2); + + }*/ + } From 98cd132aabf91fc48edb06d75a099ac785e3bcc7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 Mar 2020 08:53:28 +0100 Subject: [PATCH 0224/1018] flat render embree --- include/raytracing/raytracing.h | 4 +++- include/raytracing/rt_embree.h | 8 +++----- include/raytracing/rt_optix.h | 2 +- src/raytracing/raytracing.cpp | 3 ++- src/raytracing/rt_embree.cpp | 11 ++++++----- src/raytracing/rt_optix.cpp | 2 +- src/viewer/viewer.cpp | 8 +++++--- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index 7c7422bb..530566e3 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -35,6 +35,7 @@ class raytracing const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, + const bool & flat, const bool & restart = false ); @@ -48,7 +49,8 @@ class raytracing protected: virtual glm::vec4 intersect_li( const glm::vec3 & org, const glm::vec3 & dir, - const glm::vec3 & light ) = 0; + const glm::vec3 & light, + const bool & flat ) = 0; virtual float intersect_depth( const glm::vec3 & org, const glm::vec3 & dir ) = 0; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 1ef1e976..f061925f 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -53,15 +53,13 @@ class embree : public raytracing return {ray.dir_x, ray.dir_y, ray.dir_z}; } - const glm::vec3 normal() const + const glm::vec3 geometry_normal() const { return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); } const glm::vec3 shading_normal(const che * mesh) const { -// if(hit.primID == RTC_INVALID_GEOMETRY_ID) return normal(); - vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::normalize(glm::vec3(n.x, n.y, n.z)); @@ -86,7 +84,7 @@ class embree : public raytracing bool intersect(ray_hit & r); bool occluded(ray_hit & r); - glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); void build_bvh(const std::vector & meshes); @@ -94,7 +92,7 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); - glm::vec4 li(const ray_hit & r, const glm::vec3 & light); + glm::vec4 li(const ray_hit & r, const glm::vec3 & light, const bool & flat); }; diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index ce88f1d8..8a2bb4e1 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -31,7 +31,7 @@ class optix : public raytracing ~optix(); private: - glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light); + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); OptixTraversableHandle build_as(const std::vector & meshes); diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 62bf5efc..4b15d9ed 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -49,6 +49,7 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, + const bool & flat, const bool & restart ) { if(rt_restart(windows_size.x, windows_size.y) || restart) @@ -79,7 +80,7 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, li = glm::vec4(0.f); for(auto & l: light) - li += intersect_li(cam_pos, glm::normalize(p - cam_pos), l); + li += intersect_li(cam_pos, glm::normalize(p - cam_pos), l, flat); color = (color * float(n_samples) + li / float(light.size())) / float(n_samples + 1); } diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index edeeef5e..01d3b54d 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -129,7 +129,7 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) +glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light, const bool & flat) { glm::vec3 color(.6f, .8f, 1.f); @@ -138,8 +138,9 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light) glm::vec3 wi = normalize(light - r.position()); - //float dot_wi_normal = glm::dot(wi, r.normal()); - float dot_wi_normal = glm::dot(wi, r.shading_normal(geomID_mesh[r.hit.geomID])); + float dot_wi_normal = flat ? glm::dot(wi, r.geometry_normal()) + : glm::dot(wi, r.shading_normal(geomID_mesh[r.hit.geomID])); + if(dot_wi_normal < 0) dot_wi_normal = -dot_wi_normal; @@ -164,10 +165,10 @@ bool embree::occluded(ray_hit & r) } -glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) +glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light,const bool & flat) { ray_hit r(org, dir); - return intersect(r) ? li(r, light) : glm::vec4(0.f); + return intersect(r) ? li(r, light, flat) : glm::vec4(0.f); } float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 8dd5167e..bca6a2b2 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -214,7 +214,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; } -glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light) +glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat) { return glm::vec4(0.f); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 3d618f95..028036ae 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -169,8 +169,8 @@ bool viewer::run() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); - glfwPollEvents(); - //glfwWaitEvents(); + glfwWaitEvents(); + //glfwPollEvents(); } return true; @@ -522,6 +522,7 @@ void viewer::set_render_lines(viewer * view) void viewer::set_render_flat(viewer * view) { view->render_flat = !view->render_flat; + view->action = true; } void viewer::raycasting(viewer * view) @@ -615,7 +616,8 @@ void viewer::render_embree() } rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); + view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, + render_flat, action ); action = false; From 89069caa5d3eb518208aff741a4da15dc7acef74 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 Mar 2020 22:15:19 +0100 Subject: [PATCH 0225/1018] update imgui files --- imgui/LICENSE.txt | 2 +- imgui/README.md | 274 --- imgui/imconfig.h | 43 +- imgui/imgui.cpp | 3172 +++++++++++++++++++--------------- imgui/imgui.h | 343 ++-- imgui/imgui_demo.cpp | 317 ++-- imgui/imgui_draw.cpp | 412 +++-- imgui/imgui_impl_glfw.cpp | 47 +- imgui/imgui_impl_glfw.h | 5 +- imgui/imgui_impl_opengl3.cpp | 60 +- imgui/imgui_impl_opengl3.h | 7 +- imgui/imgui_internal.h | 651 ++++--- imgui/imgui_widgets.cpp | 663 ++++--- imgui/imstb_textedit.h | 2 +- imgui/imstb_truetype.h | 8 +- 15 files changed, 3438 insertions(+), 2568 deletions(-) delete mode 100644 imgui/README.md diff --git a/imgui/LICENSE.txt b/imgui/LICENSE.txt index 3b439aa4..d8763995 100644 --- a/imgui/LICENSE.txt +++ b/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2019 Omar Cornut +Copyright (c) 2014-2020 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/imgui/README.md b/imgui/README.md deleted file mode 100644 index 1991b954..00000000 --- a/imgui/README.md +++ /dev/null @@ -1,274 +0,0 @@ -dear imgui -===== -[![Build Status](https://api.travis-ci.com/ocornut/imgui.svg?branch=master)](https://travis-ci.com/ocornut/imgui) -[![Coverity Status](https://scan.coverity.com/projects/4720/badge.svg)](https://scan.coverity.com/projects/4720) - -(This library is available under a free and permissive license, but needs financial support to sustain its continued improvements. In addition to maintenance and stability there are many desirable features yet to be added. If your company is using dear imgui, please consider reaching out. If you are an individual using dear imgui, please consider supporting the project via Patreon or PayPal.) - -Businesses: support continued development via invoiced technical support, maintenance, sponsoring contracts: -
  _E-mail: contact @ dearimgui dot org_ - -Individuals/hobbyists: support continued maintenance and development via the monthly Patreon: -
  [![Patreon](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/patreon_02.png)](http://www.patreon.com/imgui) - -Individuals/hobbyists: support continued maintenance and development via PayPal: -
  [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WGHNC6MBFLZ2S) - ----- - -Dear ImGui is a **bloat-free graphical user interface library for C++**. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline enabled application. It is fast, portable, renderer agnostic and self-contained (no external dependencies). - -Dear ImGui is designed to **enable fast iterations** and to **empower programmers** to create **content creation tools and visualization / debug tools** (as opposed to UI for the average end-user). It favors simplicity and productivity toward this goal, and lacks certain features normally found in more high-level libraries. - -Dear ImGui is particularly suited to integration in games engine (for tooling), real-time 3D applications, fullscreen applications, embedded applications, or any applications on consoles platforms where operating system features are non-standard. - -| [Usage](#usage) - [How it works](#how-it-works) - [Demo](#demo) - [Integration](#integration) | -:----------------------------------------------------------: | -| [Upcoming changes](#upcoming-changes) - [Gallery](#gallery) - [Support, FAQ](#support-frequently-asked-questions-faq) - [How to help](#how-to-help) - [Sponsors](#sponsors) - [Credits](#credits) - [License](#license) | -| [Wiki](https://github.com/ocornut/imgui/wiki) - [Language & frameworks bindings](https://github.com/ocornut/imgui/wiki/Bindings) - [Software using Dear ImGui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) - [User quotes](https://github.com/ocornut/imgui/wiki/Quotes) | - -### Usage - -Dear ImGui is self-contained within a few files that you can easily copy and compile into your application/engine: -- imgui.cpp -- imgui.h -- imgui_demo.cpp -- imgui_draw.cpp -- imgui_widgets.cpp -- imgui_internal.h -- imconfig.h (empty by default, user-editable) -- imstb_rectpack.h -- imstb_textedit.h -- imstb_truetype.h - -No specific build process is required. You can add the .cpp files to your project or #include them from an existing file. - -Backends for a variety of graphics api and rendering platforms along with example applications are provided in the [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder. - -The backend passes mouse/keyboard/gamepad inputs and variety of settings to Dear ImGui, and is in charge of rendering the resulting vertices. After Dear ImGui is setup in your application, you can use it from \_anywhere\_ in your program loop: - -Code: -```cp -ImGui::Text("Hello, world %d", 123); -if (ImGui::Button("Save")) - MySaveFunction(); -ImGui::InputText("string", buf, IM_ARRAYSIZE(buf)); -ImGui::SliderFloat("float", &f, 0.0f, 1.0f); -``` -Result: -
![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_02.png) -
_(settings: Dark style (left), Light style (right) / Font: Roboto-Medium, 16px / Rounding: 5)_ - -Code: -```cpp -// Create a window called "My First Tool", with a menu bar. -ImGui::Begin("My First Tool", &my_tool_active, ImGuiWindowFlags_MenuBar); -if (ImGui::BeginMenuBar()) -{ - if (ImGui::BeginMenu("File")) - { - if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */ } - if (ImGui::MenuItem("Save", "Ctrl+S")) { /* Do stuff */ } - if (ImGui::MenuItem("Close", "Ctrl+W")) { my_tool_active = false; } - ImGui::EndMenu(); - } - ImGui::EndMenuBar(); -} - -// Edit a color (stored as ~4 floats) -ImGui::ColorEdit4("Color", my_color); - -// Plot some values -const float my_values[] = { 0.2f, 0.1f, 1.0f, 0.5f, 0.9f, 2.2f }; -ImGui::PlotLines("Frame Times", my_values, IM_ARRAYSIZE(my_values)); - -// Display contents in a scrolling region -ImGui::TextColored(ImVec4(1,1,0,1), "Important Stuff"); -ImGui::BeginChild("Scrolling"); -for (int n = 0; n < 50; n++) - ImGui::Text("%04d: Some text", n); -ImGui::EndChild(); -ImGui::End(); -``` -Result: -
![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_03_color.gif) - -Dear ImGui allows you **create elaborate tools** as well as very short-lived ones. On the extreme side of short-liveness: using the Edit&Continue (hot code reload) feature of modern compilers you can add a few widgets to tweaks variables while your application is running, and remove the code a minute later! Dear ImGui is not just for tweaking values. You can use it to trace a running algorithm by just emitting text commands. You can use it along with your own reflection data to browse your dataset live. You can use it to expose the internals of a subsystem in your engine, to create a logger, an inspection tool, a profiler, a debugger, an entire game making editor/framework, etc. - -### How it works - -Check out the Wiki's [About the IMGUI paradigm](https://github.com/ocornut/imgui/wiki#About-the-IMGUI-paradigm) section if you want to understand the core principles behind the IMGUI paradigm. An IMGUI tries to minimize superfluous state duplication, state synchronization and state retention from the user's point of view. It is less error prone (less code and less bugs) than traditional retained-mode interfaces, and lends itself to create dynamic user interfaces. - -Dear ImGui outputs vertex buffers and command lists that you can easily render in your application. The number of draw calls and state changes required to render them is fairly small. Because Dear ImGui doesn't know or touch graphics state directly, you can call its functions anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate dear imgui with your existing codebase. - -_A common misunderstanding is to mistake immediate mode gui for immediate mode rendering, which usually implies hammering your driver/GPU with a bunch of inefficient draw calls and state changes as the gui functions are called. This is NOT what Dear ImGui does. Dear ImGui outputs vertex buffers and a small list of draw calls batches. It never touches your GPU directly. The draw call batches are decently optimal and you can render them later, in your app or even remotely._ - -### Demo - -Calling the `ImGui::ShowDemoWindow()` function will create a demo window showcasing variety of features and examples. The code is always available for reference in `imgui_demo.cpp`. - -![screenshot demo](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/v167-misc.png) - -You should be able to build the examples from sources (tested on Windows/Mac/Linux). If you don't, let me know! If you want to have a quick look at some Dear ImGui features, you can download Windows binaries of the demo app here: -- [imgui-demo-binaries-20190715.zip](http://www.dearimgui.org/binaries/imgui-demo-binaries-20190715.zip) (Windows binaries, 1.72 WIP, built 2019/07/15, master branch, 5 executables) - -The demo applications are not DPI aware so expect some blurriness on a 4K screen. For DPI awareness in your application, you can load/reload your font at different scale, and scale your style with `style.ScaleAllSizes()`. - -### Integration - -On most platforms and when using C++, **you should be able to use a combination of the [imgui_impl_xxxx](https://github.com/ocornut/imgui/tree/master/examples) files without modification** (e.g. `imgui_impl_win32.cpp` + `imgui_impl_dx11.cpp`). If your engine supports multiple platforms, consider using more of the imgui_impl_xxxx files instead of rewriting them: this will be less work for you and you can get Dear ImGui running immediately. You can _later_ decide to rewrite a custom binding using your custom engine functions if you wish so. - -Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/keyboard/gamepad inputs 2) uploading one texture to your GPU/render engine 3) providing a render function that can bind textures and render textured triangles. The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications doing just that. If you are an experienced programmer at ease with those concepts, it should take you less than two hours to integrate Dear ImGui in your custom engine. **Make sure to spend time reading the [FAQ](https://github.com/ocornut/imgui/wiki/FAQ), comments, and one of the examples/ application!** - -Officially maintained bindings (in repository): -- Renderers: DirectX9, DirectX10, DirectX11, DirectX12, OpenGL (legacy), OpenGL3/ES/ES2 (modern), Vulkan, Metal. -- Platforms: GLFW, SDL2, Win32, Glut, OSX. -- Frameworks: Emscripten, Allegro5, Marmalade. - -Third-party bindings (see [Bindings](https://github.com/ocornut/imgui/wiki/Bindings/) page): -- Languages: C, C#/.Net, ChaiScript, D, Go, Haxe/hxcpp, Java, JavaScript, Julia, Lua, Odin, Pascal, PureBasic, Python, Ruby, Rust, Swift... -- Frameworks: Amethyst, bsf, Cinder, Cocos2d-x, Diligent Engine, Flexium, GML/GameMakerStudio2, Irrlicht, Ogre, OpenFrameworks, OpenSceneGraph/OSG, ORX, px_render, LÖVE+Lua, Magnum, NanoRT, Qt, QtDirect3D, SFML, Software Rasterizers, Unreal Engine 4... -- Note that C bindings ([cimgui](https://github.com/cimgui/cimgui)) are auto-generated, you can use its json/lua output to generate bindings for other languages. - -Also see [Wiki](https://github.com/ocornut/imgui/wiki) for more links and ideas. - -### Upcoming Changes - -Some of the goals for 2019 are: -- Finish work on docking, tabs. (see [#2109](https://github.com/ocornut/imgui/issues/2109), in public [docking](https://github.com/ocornut/imgui/tree/docking) branch looking for feedback) -- Finish work on multiple viewports / multiple OS windows. (see [#1542](https://github.com/ocornut/imgui/issues/1542), in public [docking](https://github.com/ocornut/imgui/tree/docking) branch looking for feedback) -- Finish work on gamepad/keyboard controls. (see [#787](https://github.com/ocornut/imgui/issues/787)) -- Add an automation and testing system, both to test the library and end-user apps. (see [#435](https://github.com/ocornut/imgui/issues/435)) -- Make Columns better. They are currently pretty terrible! New Tables API coming Q4 2019! -- Make the examples look better, improve styles, improve font support, make the examples hi-DPI and multi-DPI aware. - -### Gallery - -For more user-submitted screenshots of projects using Dear ImGui, check out the [Gallery Threads](https://github.com/ocornut/imgui/issues/2847)! - -Custom engine -[![screenshot game](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v149/gallery_TheDragonsTrap-01-thumb.jpg)](https://cloud.githubusercontent.com/assets/8225057/20628927/33e14cac-b329-11e6-80f6-9524e93b048a.png) - -Custom engine -[![screenshot tool](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white_preview.jpg)](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white.png) - -[Tracy Profiler](https://bitbucket.org/wolfpld/tracy) -![tracy profiler](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v167/tracy_profiler.png) - -### Support, Frequently Asked Questions (FAQ) - -Most common questions will be answered by the [Frequently Asked Questions (FAQ)](https://github.com/ocornut/imgui/wiki/FAQ) page, e.g.: - -**Basics** -- "Where is the documentation?" -- "Which version should I get?" -- "Why the names "Dear ImGui" vs "ImGui"?" - -**Community** -- "How can I help?" - -**Concerns** -- "Who uses Dear ImGui?" -- "Can you create elaborate/serious tools with Dear ImGui?" -- "Can you reskin the look of Dear ImGui?" -- "Why using C++ (as opposed to C)?" - -**Integration** -- "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application?" -- "How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display)" -- "I integrated Dear ImGui in my engine and the text or lines are blurry.." -- "I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.." - -**Usage** -- "Why are multiple widgets reacting when I interact with a single one? How can I have multiple widgets with the same label or with an empty label?" -- "How can I display an image? What is ImTextureID, how does it work?" -- "How can I use my own math types instead of ImVec2/ImVec4?" -- "How can I interact with standard C++ types (such as std::string and std::vector)?" -- "How can I use low-level drawing facilities? (using ImDrawList API)" - -**Fonts, Text** -- "How can I load a different font than the default?" -- "How can I easily use icons in my application?" -- "How can I load multiple fonts?" -- "How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic?" - -See: [Wiki](https://github.com/ocornut/imgui/wiki) for many links, references, articles. - -See: [Articles about the IMGUI paradigm](https://github.com/ocornut/imgui/wiki#Articles-about-the-IMGUI-paradigm) to read/learn about the Immediate Mode GUI paradigm. - -If you are new to Dear ImGui and have issues with: compiling, linking, adding fonts, wiring inputs, running or displaying Dear ImGui: you can use [Discord server](https://discord.gg/NgJ4SEP) or [Discourse forums](https://discourse.dearimgui.org). - -Otherwise, for any other questions, bug reports, requests, feedback, you may post on https://github.com/ocornut/imgui/issues. Please read and fill the New Issue template carefully. - -Paid private support is available for business customers (E-mail: _contact @ dearimgui dot org_). - -**Which version should I get?** - -I occasionally tag [Releases](https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported. - -You may also peak at the [Multi-Viewport](https://github.com/ocornut/imgui/issues/1542) and [Docking](https://github.com/ocornut/imgui/issues/2109) features in the `docking` branch. Many projects are using this branch and it is kept in sync with master regularly. - -**Who uses Dear ImGui?** - -See the [Quotes](https://github.com/ocornut/imgui/wiki/Quotes) and [Software using dear imgui](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) Wiki pages for a list of games/software which are publicly known to use dear imgui. Please add yours if you can! Also see the [Gallery Threads](https://github.com/ocornut/imgui/issues/2847)! - -How to help ------------ - -**How can I help?** - -- You may participate in the [Discord server](https://discord.gg/NgJ4SEP), [Discourse forums](https://discourse.dearimgui.org), GitHub [issues tracker](https://github.com/ocornut/imgui/issues). -- You may help with development and submit pull requests! Please understand that by submitting a PR you are also submitting a request for the maintainer to review your code and then take over its maintenance forever. PR should be crafted both in the interest in the end-users and also to ease the maintainer into understanding and accepting it. -- See [Help wanted](https://github.com/ocornut/imgui/wiki/Help-Wanted) on the [Wiki](https://github.com/ocornut/imgui/wiki/) for some more ideas. -- Have your company financially support this project. - -**How can I help financing further development of Dear ImGui?** - -Your contributions are keeping this project alive. The library is available under a free and permissive license, but continued maintenance and development are a full-time endeavor and I would like to grow the team. In addition to maintenance and stability there are many desirable features yet to be added. If your company is using dear imgui, please consider reaching out for invoiced technical support and maintenance contracts. If you are an individual using dear imgui, please consider supporting the project via Patreon or PayPal. Thank you! - -Businesses: support continued development via invoiced technical support, maintenance, sponsoring contracts: -
  _E-mail: contact @ dearimgui dot org_ - -Individuals/hobbyists: support continued maintenance and development via the monthly Patreon: -
  [![Patreon](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/patreon_02.png)](http://www.patreon.com/imgui) - -Individuals/hobbyists: support continued maintenance and development via PayPal: -
  [![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WGHNC6MBFLZ2S) - -### Sponsors - -Ongoing Dear ImGui development is financially supported by users and private sponsors, recently: - -*Platinum-chocolate sponsors* -- Blizzard Entertainment -- Google - -*Double-chocolate sponsors* -- Media Molecule, Mobigame, Aras Pranckevičius, Greggman, DotEmu, Nadeo, Supercell, Runner, Aiden Koss, Kylotonn. - -*Salty caramel supporters* -- Remedy Entertainment, Recognition Robotics, ikrima, Geoffrey Evans, Mercury Labs, Singularity Demo Group, Lionel Landwerlin, Ron Gilbert, Brandon Townsend, G3DVu, Cort Stratton, drudru, Harfang 3D, Jeff Roberts, Rainway inc, Ondra Voves, Mesh Consultants, Unit 2 Games, Neil Bickford, Bill Six, Graham Manders. - -*Caramel supporters* -- Jerome Lanquetot, Daniel Collin, Ctrl Alt Ninja, Neil Henning, Neil Blakey-Milner, Aleksei, NeiloGD, Eric, Game Atelier, Vincent Hamm, Morten Skaaning, Colin Riley, Sergio Gonzales, Andrew Berridge, Roy Eltham, Game Preservation Society, Josh Faust, Martin Donlon, Codecat, Doug McNabb, Emmanuel Julien, Guillaume Chereau, Jeffrey Slutter, Jeremiah Deckard, r-lyeh, Nekith, Joshua Fisher, Malte Hoffmann, Mustafa Karaalioglu, Merlyn Morgan-Graham, Per Vognsen, Fabian Giesen, Jan Staubach, Matt Hargett, John Shearer, Jesse Chounard, kingcoopa, Jonas Bernemann, Johan Andersson, Michael Labbe, Tomasz Golebiowski, Louis Schnellbach, Jimmy Andrews, Bojan Endrovski, Robin Berg Pettersen, Rachel Crawford, Andrew Johnson, Sean Hunter, Jordan Mellow, Nefarius Software Solutions, Laura Wieme, Robert Nix, Mick Honey, Steven Kah Hien Wong, Bartosz Bielecki, Oscar Penas, A M, Liam Moynihan, Artometa, Mark Lee, Dimitri Diakopoulos, Pete Goodwin, Johnathan Roatch, nyu lea, Oswald Hurlem, Semyon Smelyanskiy, Le Bach, Jeong MyeongSoo, Chris Matthews, Astrofra, Frederik De Bleser, Anticrisis, Matt Reyer. - -And all other past and present supporters; THANK YOU! -(Please contact me if you would like to be added or removed from this list) - -Credits -------- - -Developed by [Omar Cornut](http://www.miracleworld.net) and every direct or indirect contributors to the GitHub. The early version of this library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com) (Vita). - -I first discovered the IMGUI paradigm at [Q-Games](http://www.q-games.com) where Atman Binstock had dropped his own simple implementation in the codebase, which I spent quite some time improving and thinking about. It turned out that Atman was exposed to the concept directly by working with Casey. When I moved to Media Molecule I rewrote a new library trying to overcome the flaws and limitations of the first one I've worked with. It became this library and since then I have spent an unreasonable amount of time iterating and improving it. - -Embeds [ProggyClean.ttf](http://upperbounds.net) font by Tristan Grimmer (MIT license). - -Embeds [stb_textedit.h, stb_truetype.h, stb_rect_pack.h](https://github.com/nothings/stb/) by Sean Barrett (public domain). - -Inspiration, feedback, and testing for early versions: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. And everybody posting feedback, questions and patches on the GitHub. - -License -------- - -Dear ImGui is licensed under the MIT License, see [LICENSE.txt](https://github.com/ocornut/imgui/blob/master/LICENSE.txt) for more information. diff --git a/imgui/imconfig.h b/imgui/imconfig.h index 45e75ecf..4f01b778 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -14,6 +14,7 @@ #pragma once //---- Define assertion handler. Defaults to calling assert(). +// If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement. //#define IM_ASSERT(_EXPR) MyAssert(_EXPR) //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts @@ -22,21 +23,23 @@ //#define IMGUI_API __declspec( dllexport ) //#define IMGUI_API __declspec( dllimport ) -//---- Don't define obsolete functions/enums names. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. +//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS -//---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty) -// It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp. -//#define IMGUI_DISABLE_DEMO_WINDOWS -//#define IMGUI_DISABLE_METRICS_WINDOW +//---- Disable all of Dear ImGui or don't implement standard windows. +// It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. +//#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. +//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended. +//#define IMGUI_DISABLE_METRICS_WINDOW // Disable debug/metrics window: ShowMetricsWindow() will be empty. //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). -//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices'). -//#define IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself if you don't want to link with vsnprintf. -//#define IMGUI_DISABLE_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 wrapper so you can implement them yourself. Declare your prototypes in imconfig.h. +//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). +//#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) +//#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. +//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). //---- Include imgui_user.h at the end of imgui.h as a convenience @@ -52,6 +55,10 @@ //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION +//---- Unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined, use the much faster STB sprintf library implementation of vsnprintf instead of the one from the default C library. +// Note that stb_sprintf.h is meant to be provided by the user and available in the include path at compile time. Also, the compatibility checks of the arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf. +// #define IMGUI_USE_STB_SPRINTF + //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. // This will be inlined as part of ImVec2 and ImVec4 class declarations. /* @@ -64,26 +71,34 @@ operator MyVec4() const { return MyVec4(x,y,z,w); } */ -//---- Using 32-bits vertex indices (default is 16-bits) is one way to allow large meshes with more than 64K vertices. -// Your renderer back-end will need to support it (most example renderer back-ends support both 16/32-bits indices). -// Another way to allow large meshes while keeping 16-bits indices is to handle ImDrawCmd::VtxOffset in your renderer. +//---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices. +// Your renderer back-end will need to support it (most example renderer back-ends support both 16/32-bit indices). +// Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer. // Read about ImGuiBackendFlags_RendererHasVtxOffset for details. //#define ImDrawIdx unsigned int +//---- Use 32-bit for ImWchar (default is 16-bit) to support full unicode code points. +//#define ImWchar ImWchar32 + //---- Override ImDrawCallback signature (will need to modify renderer back-ends accordingly) //struct ImDrawList; //struct ImDrawCmd; //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); //#define ImDrawCallback MyImDrawCallback -//---- Debug Tools -// Use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging. +//---- Debug Tools: Macro to break in Debugger +// (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.) //#define IM_DEBUG_BREAK IM_ASSERT(0) //#define IM_DEBUG_BREAK __debugbreak() -// Have the Item Picker break in the ItemAdd() function instead of ItemHoverable() - which is earlier in the code, will catch a few extra items, allow picking items other than Hovered one. + +//---- Debug Tools: Have the Item Picker break in the ItemAdd() function instead of ItemHoverable(), +// (which comes earlier in the code, will catch a few extra items, allow picking items other than Hovered one.) // This adds a small runtime cost which is why it is not enabled by default. //#define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX +//---- Debug Tools: Enable slower asserts +//#define IMGUI_DEBUG_PARANOID + //---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files. /* namespace ImGui diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index fc155333..bb6dd25f 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,18 +1,25 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (main code and documentation) -// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. -// Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. -// Get latest version at https://github.com/ocornut/imgui -// Releases change-log at https://github.com/ocornut/imgui/releases -// Technical Support for Getting Started https://discourse.dearimgui.org/c/getting-started -// Gallery (please post your screenshots/video there!): https://github.com/ocornut/imgui/issues/2847 +// Help: +// - Read FAQ at http://dearimgui.org/faq +// - Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. + +// Resources: +// - FAQ http://dearimgui.org/faq +// - Homepage & latest https://github.com/ocornut/imgui +// - Releases & changelog https://github.com/ocornut/imgui/releases +// - Gallery https://github.com/ocornut/imgui/issues/2847 (please post your screenshots/video there!) +// - Glossary https://github.com/ocornut/imgui/wiki/Glossary +// - Wiki https://github.com/ocornut/imgui/wiki +// - Issues & support https://github.com/ocornut/imgui/issues // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). // This library is free but I need your support to sustain development and maintenance. -// Businesses: you can support continued maintenance and development via support contracts or sponsoring, see docs/README. -// Individuals: you can support continued maintenance and development via donations or Patreon https://www.patreon.com/imgui. +// Businesses: you can support continued development via invoiced technical support, maintenance and sponsoring contracts. Please reach out to "contact AT dearimgui.org". +// Individuals: you can support continued development via donations. See docs/README or web page. // It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. // Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without @@ -28,51 +35,38 @@ DOCUMENTATION - MISSION STATEMENT - END-USER GUIDE -- PROGRAMMER GUIDE (read me!) - - Read first. - - How to update to a newer version of Dear ImGui. - - Getting started with integrating Dear ImGui in your code/engine. - - This is how a simple application may look like (2 variations). - - This is how a simple rendering function may look like. - - Using gamepad/keyboard navigation controls. +- PROGRAMMER GUIDE + - READ FIRST + - HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI + - GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE + - HOW A SIMPLE APPLICATION MAY LOOK LIKE (2 variations) + - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE + - USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - API BREAKING CHANGES (read me when you update!) - FREQUENTLY ASKED QUESTIONS (FAQ) - - All answers in https://github.com/ocornut/imgui/wiki/FAQ - - Where is the documentation? - - Which version should I get? - - Who uses Dear ImGui? - - Why the odd dual naming, "Dear ImGui" vs "ImGui"? - - How can I tell whether to dispatch mouse/keyboard to imgui or to my application? - - How can I display an image? What is ImTextureID, how does it works? - - Why are multiple widgets reacting when I interact with a single one? How can I have - multiple widgets with the same label or with an empty label? A primer on labels and the ID Stack... - - How can I use my own math types instead of ImVec2/ImVec4? - - How can I load a different font than the default? - - How can I easily use icons in my application? - - How can I load multiple fonts? - - How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic? - - How can I interact with standard C++ types (such as std::string and std::vector)? - - How can I use the drawing facilities without a Dear ImGui window? (using ImDrawList API) - - How can I use Dear ImGui on a platform that doesn't have a mouse or a keyboard? (input share, remoting, gamepad) - - I integrated Dear ImGui in my engine and the text or lines are blurry.. - - I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. - - How can I help? + - Read all answers online: https://www.dearimgui.org/faq, or in docs/FAQ.md (with a Markdown viewer) CODE (search for "[SECTION]" in the code to find them) +// [SECTION] INCLUDES // [SECTION] FORWARD DECLARATIONS // [SECTION] CONTEXT AND MEMORY ALLOCATORS -// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) -// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) +// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +// [SECTION] MISC HELPERS/UTILITIES (Geometry functions) +// [SECTION] MISC HELPERS/UTILITIES (String, Format, Hash functions) +// [SECTION] MISC HELPERS/UTILITIES (File functions) // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) // [SECTION] MISC HELPERS/UTILITIES (Color functions) // [SECTION] ImGuiStorage // [SECTION] ImGuiTextFilter // [SECTION] ImGuiTextBuffer // [SECTION] ImGuiListClipper +// [SECTION] STYLING // [SECTION] RENDER HELPERS // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +// [SECTION] ERROR CHECKING +// [SECTION] LAYOUT // [SECTION] SCROLLING // [SECTION] TOOLTIPS // [SECTION] POPUPS @@ -136,9 +130,9 @@ CODE PROGRAMMER GUIDE ================ - READ FIRST: - - - Read the FAQ below this section! + READ FIRST + ---------- + - Remember to read the FAQ (https://www.dearimgui.org/faq) - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction or destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, less bugs. - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. @@ -158,8 +152,8 @@ CODE However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase. - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!). - HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI: - + HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI + ---------------------------------------------- - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) - Or maintain your own branch where you have imconfig.h modified. - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. @@ -168,11 +162,12 @@ CODE likely be a comment about it. Please report any issue to the GitHub page! - Try to keep your copy of dear imgui reasonably up to date. - GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE: - + GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE + --------------------------------------------------------------- - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. + - In the majority of cases you should be able to use unmodified back-ends files available in the examples/ folder. - Add the Dear ImGui source files to your projects or using your preferred build system. - It is recommended you build and statically link the .cpp files as part of your project and not as shared library (DLL). + It is recommended you build and statically link the .cpp files as part of your project and NOT as shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide. @@ -181,7 +176,8 @@ CODE - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code. - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder. - HOW A SIMPLE APPLICATION MAY LOOK LIKE: + HOW A SIMPLE APPLICATION MAY LOOK LIKE + -------------------------------------- EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder). // Application init: create a dear imgui context, setup some options, load fonts @@ -191,7 +187,7 @@ CODE // TODO: Fill optional fields of the io structure later. // TODO: Load TTF/OTF fonts if you don't want to use the default font. - // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32 and imgui_impl_dx11) + // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32.cpp and imgui_impl_dx11.cpp) ImGui_ImplWin32_Init(hwnd); ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); @@ -217,8 +213,7 @@ CODE ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext(); - HOW A SIMPLE APPLICATION MAY LOOK LIKE: - EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE. + EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); @@ -235,7 +230,7 @@ CODE // At this point you've got the texture data and you need to upload that your your graphic system: // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'. - // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ below for details about ImTextureID. + // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ for details about ImTextureID. MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32) io.Fonts->TexID = (void*)texture; @@ -272,8 +267,8 @@ CODE // Shutdown ImGui::DestroyContext(); - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE: - + HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE + --------------------------------------------- void void MyImGuiRenderFunction(ImDrawData* draw_data) { // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled @@ -310,7 +305,7 @@ CODE MyEngineScissor((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pos.x), (int)(pcmd->ClipRect.w - pos.y)); // Render 'pcmd->ElemCount/3' indexed triangles. - // By default the indices ImDrawIdx are 16-bits, you can change them to 32-bits in imconfig.h if your engine doesn't support 16-bits indices. + // By default the indices ImDrawIdx are 16-bit, you can change them to 32-bit in imconfig.h if your engine doesn't support 16-bit indices. MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer); } idx_buffer += pcmd->ElemCount; @@ -321,15 +316,24 @@ CODE - The examples/ folders contains many actual implementation of the pseudo-codes above. - When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated. They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs from the - rest of your application. In every cases you need to pass on the inputs to Dear ImGui. Refer to the FAQ for more information. - - Please read the FAQ below!. Amusingly, it is called a FAQ because people frequently run into the same issues! + rest of your application. In every cases you need to pass on the inputs to Dear ImGui. + - Refer to the FAQ for more information. Amusingly, it is called a FAQ because people frequently run into the same issues! USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - + ------------------------------------------ - The gamepad/keyboard navigation is fairly functional and keeps being improved. - - Gamepad support is particularly useful to use dear imgui on a console system (e.g. PS4, Switch, XB1) without a mouse! + - Gamepad support is particularly useful to use Dear ImGui on a console system (e.g. PS4, Switch, XB1) without a mouse! - You can ask questions and report issues at https://github.com/ocornut/imgui/issues/787 - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable. + - Keyboard: + - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. + NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. + - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag + will be set. For more advanced uses, you may want to read from: + - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. + - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). + - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. + Please reach out if you think the game vs navigation input sharing could be improved. - Gamepad: - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame(). @@ -341,15 +345,6 @@ CODE - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://goo.gl/9LgVZW. - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. - - Keyboard: - - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. - NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. - - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag - will be set. For more advanced uses, you may want to read from: - - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. - - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). - - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. - Please reach out if you think the game vs navigation input sharing could be improved. - Mouse: - PS4 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + uSynergy.c (on your console/tablet/phone app) to share your PC mouse/keyboard. @@ -370,13 +365,37 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2020/01/22 (1.75) - ImDrawList::AddCircle()/AddCircleFilled() functions don't accept negative radius any more. + - 2019/12/17 (1.75) - made Columns() limited to 64 columns by asserting above that limit. While the current code technically supports it, future code may not so we're putting the restriction ahead. + - 2019/12/13 (1.75) - [imgui_internal.h] changed ImRect() default constructor initializes all fields to 0.0f instead of (FLT_MAX,FLT_MAX,-FLT_MAX,-FLT_MAX). If you used ImRect::Add() to create bounding boxes by adding multiple points into it, you may need to fix your initial value. + - 2019/12/08 (1.75) - removed redirecting functions/enums that were marked obsolete in 1.53 (December 2017): + - ShowTestWindow() -> use ShowDemoWindow() + - IsRootWindowFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootWindow) + - IsRootWindowOrAnyChildFocused() -> use IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows) + - SetNextWindowContentWidth(w) -> use SetNextWindowContentSize(ImVec2(w, 0.0f) + - GetItemsLineHeightWithSpacing() -> use GetFrameHeightWithSpacing() + - ImGuiCol_ChildWindowBg -> use ImGuiCol_ChildBg + - ImGuiStyleVar_ChildWindowRounding -> use ImGuiStyleVar_ChildRounding + - ImGuiTreeNodeFlags_AllowOverlapMode -> use ImGuiTreeNodeFlags_AllowItemOverlap + - IMGUI_DISABLE_TEST_WINDOWS -> use IMGUI_DISABLE_DEMO_WINDOWS + - 2019/12/08 (1.75) - obsoleted calling ImDrawList::PrimReserve() with a negative count (which was the vaguely documented and rarely if ever used). Instead we added an explicit PrimUnreserve() API. + - 2019/12/06 (1.75) - removed implicit default parameter to IsMouseDragging(int button = 0) to be consistent with other mouse functions (none of the other functions have it). + - 2019/11/21 (1.74) - ImFontAtlas::AddCustomRectRegular() now requires an ID larger than 0x110000 (instead of 0x10000) to conform with supporting Unicode planes 1-16 in a future update. ID below 0x110000 will now assert. + - 2019/11/19 (1.74) - renamed IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS to IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS for consistency. + - 2019/11/19 (1.74) - renamed IMGUI_DISABLE_MATH_FUNCTIONS to IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS for consistency. + - 2019/10/22 (1.74) - removed redirecting functions/enums that were marked obsolete in 1.52 (October 2017): + - Begin() [old 5 args version] -> use Begin() [3 args], use SetNextWindowSize() SetNextWindowBgAlpha() if needed + - IsRootWindowOrAnyChildHovered() -> use IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows) + - AlignFirstTextHeightToWidgets() -> use AlignTextToFramePadding() + - SetNextWindowPosCenter() -> use SetNextWindowPos() with a pivot of (0.5f, 0.5f) + - ImFont::Glyph -> use ImFontGlyph - 2019/10/14 (1.74) - inputs: Fixed a miscalculation in the keyboard/mouse "typematic" repeat delay/rate calculation, used by keys and e.g. repeating mouse buttons as well as the GetKeyPressedAmount() function. if you were using a non-default value for io.KeyRepeatRate (previous default was 0.250), you can add +io.KeyRepeatDelay to it to compensate for the fix. The function was triggering on: 0.0 and (delay+rate*N) where (N>=1). Fixed formula responds to (N>=0). Effectively it made io.KeyRepeatRate behave like it was set to (io.KeyRepeatRate + io.KeyRepeatDelay). If you never altered io.KeyRepeatRate nor used GetKeyPressedAmount() this won't affect you. - 2019/07/15 (1.72) - removed TreeAdvanceToLabelPos() which is rarely used and only does SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()). Kept redirection function (will obsolete). - 2019/07/12 (1.72) - renamed ImFontAtlas::CustomRect to ImFontAtlasCustomRect. Kept redirection typedef (will obsolete). - - 2019/06/14 (1.72) - removed redirecting functions/enums names that were marked obsolete in 1.51 (June 2017): ImGuiCol_Column*, ImGuiSetCond_*, IsItemHoveredRect(), IsPosHoveringAnyWindow(), IsMouseHoveringAnyWindow(), IsMouseHoveringWindow(), IMGUI_ONCE_UPON_A_FRAME. Grep this log for details and new names. + - 2019/06/14 (1.72) - removed redirecting functions/enums names that were marked obsolete in 1.51 (June 2017): ImGuiCol_Column*, ImGuiSetCond_*, IsItemHoveredRect(), IsPosHoveringAnyWindow(), IsMouseHoveringAnyWindow(), IsMouseHoveringWindow(), IMGUI_ONCE_UPON_A_FRAME. Grep this log for details and new names, or see how they were implemented until 1.71. - 2019/06/07 (1.71) - rendering of child window outer decorations (bg color, border, scrollbars) is now performed as part of the parent window. If you have overlapping child windows in a same parent, and relied on their relative z-order to be mapped to their submission order, this will affect your rendering. This optimization is disabled if the parent window has no visual output, because it appears to be the most common situation leading to the creation of overlapping child windows. @@ -487,14 +506,9 @@ CODE - 2016/09/25 (1.50) - style.WindowTitleAlign is now a ImVec2 (ImGuiAlign enum was removed). set to (0.5f,0.5f) for horizontal+vertical centering, (0.0f,0.0f) for upper-left, etc. - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully breakage should be minimal. - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore. - If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you. - If your TitleBg/TitleBgActive alpha was <1.0f you need to tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. - This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color. - ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) - { - float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; - return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a); - } + If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you, otherwise if <1.0f you need tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. + This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color: + ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) { float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a); } If this is confusing, pick the RGB value from title bar from an old screenshot and apply this as TitleBg/TitleBgActive. Or you may just create TitleBgActive from a tweaked TitleBg color. - 2016/05/07 (1.49) - removed confusing set of GetInternalState(), GetInternalStateSize(), SetInternalState() functions. Now using CreateContext(), DestroyContext(), GetCurrentContext(), SetCurrentContext(). - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection. @@ -553,14 +567,13 @@ CODE - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused) - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions. - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. - (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. - font init: { const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); <..Upload texture to GPU..>; } - became: { unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); <..Upload texture to GPU>; io.Fonts->TexId = YourTextureIdentifier; } - you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. - it is now recommended that you sample the font texture with bilinear interpolation. - (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. - (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) - (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets + - 2015/01/11 (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. + - old: const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); [..Upload texture to GPU..]; + - new: unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); [..Upload texture to GPU..]; io.Fonts->TexId = YourTexIdentifier; + you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. It is now recommended that you sample the font texture with bilinear interpolation. + - 2015/01/11 (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. + - 2015/01/11 (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) + - 2015/01/11 (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) - 2014/11/28 (1.17) - moved IO.Font*** options to inside the IO.Font-> structure (FontYOffset, FontTexUvForWhite, FontBaseScale, FontFallbackGlyph) - 2014/11/26 (1.17) - reworked syntax of IMGUI_ONCE_UPON_A_FRAME helper macro to increase compiler compatibility @@ -574,12 +587,11 @@ CODE - 2014/08/28 (1.09) - changed the behavior of IO.PixelCenterOffset following various rendering fixes - FREQUENTLY ASKED QUESTIONS (FAQ), TIPS - ====================================== + FREQUENTLY ASKED QUESTIONS (FAQ) + ================================ - All answers in: https://github.com/ocornut/imgui/wiki/FAQ - Some answers are copied down here to facilitate searching in code or because they are most likely to - be varying depending on your version of the code. + Read all answers online: https://www.dearimgui.org/faq, or in docs/FAQ.md (with a Markdown viewer) + Some answers are copied down here to facilitate searching in code. Q&A: Basics =========== @@ -590,46 +602,30 @@ CODE - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. - The demo covers most features of Dear ImGui, so you can read the code and see its output. - See documentation and comments at the top of imgui.cpp + effectively imgui.h. - - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ - folder to explain how to integrate Dear ImGui with your own engine/application. + - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the + examples/ folder to explain how to integrate Dear ImGui with your own engine/application. + - The Wiki (https://github.com/ocornut/imgui/wiki) has many resources and links. + - The Glossary (https://github.com/ocornut/imgui/wiki/Glossary) page also may be useful. - Your programming IDE is your friend, find the type or function declaration to find comments associated to it. + Q: What is this library called? Q: Which version should I get? - Q: Why the names "Dear ImGui" vs "ImGui"? - A: See https://github.com/ocornut/imgui/wiki/FAQ - - Q&A: Concerns - ============= - - Q: Who uses Dear ImGui? - Q: Can you create elaborate/serious tools with Dear ImGui? - Q: Can you reskin the look of Dear ImGui? - Q: Why using C++ (as opposed to C)? - A: See https://github.com/ocornut/imgui/wiki/FAQ + >> This library is called "Dear ImGui", please don't call it "ImGui" :) + >> See https://www.dearimgui.org/faq Q&A: Integration ================ Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application? - A: You can read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags from the ImGuiIO structure (e.g. if (ImGui::GetIO().WantCaptureMouse) { ... } ) - - When 'io.WantCaptureMouse' is set, imgui wants to use your mouse state, and you may want to discard/hide the inputs from the rest of your application. - - When 'io.WantCaptureKeyboard' is set, imgui wants to use your keyboard state, and you may want to discard/hide the inputs from the rest of your application. - - When 'io.WantTextInput' is set to may want to notify your OS to popup an on-screen keyboard, if available (e.g. on a mobile phone, or console OS). - Note: you should always pass your mouse/keyboard inputs to imgui, even when the io.WantCaptureXXX flag are set false. - This is because imgui needs to detect that you clicked in the void to unfocus its own windows. - Note: The 'io.WantCaptureMouse' is more accurate that any attempt to "check if the mouse is hovering a window" (don't do that!). - It handle mouse dragging correctly (both dragging that started over your application or over an imgui window) and handle e.g. modal windows blocking inputs. - Those flags are updated by ImGui::NewFrame(). Preferably read the flags after calling NewFrame() if you can afford it, but reading them before is also - perfectly fine, as the bool toggle fairly rarely. If you have on a touch device, you might find use for an early call to UpdateHoveredWindowAndCaptureFlags(). - Note: Text input widget releases focus on "Return KeyDown", so the subsequent "Return KeyUp" event that your application receive will typically - have 'io.WantCaptureKeyboard=false'. Depending on your application logic it may or not be inconvenient. You might want to track which key-downs - were targeted for Dear ImGui, e.g. with an array of bool, and filter out the corresponding key-ups.) - + A: You should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! + >> See https://www.dearimgui.org/faq for fully detailed answer. You really want to read this. + + Q. How can I enable keyboard controls? Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) Q: I integrated Dear ImGui in my engine and the text or lines are blurry.. Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. - A: See https://github.com/ocornut/imgui/wiki/FAQ + >> See https://www.dearimgui.org/faq Q&A: Usage ---------- @@ -751,12 +747,12 @@ CODE node open/closed state differently. See what makes more sense in your situation! Q: How can I display an image? What is ImTextureID, how does it works? - A: See https://github.com/ocornut/imgui/wiki/FAQ and https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples + >> See https://www.dearimgui.org/faq and https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples Q: How can I use my own math types instead of ImVec2/ImVec4? Q: How can I interact with standard C++ types (such as std::string and std::vector)? - Q: How can I use low-level drawing facilities? (using ImDrawList API) - A: See https://github.com/ocornut/imgui/wiki/FAQ + Q: How can I display custom shapes? (using low-level ImDrawList API) + >> See https://www.dearimgui.org/faq Q&A: Fonts, Text ================ @@ -765,33 +761,51 @@ CODE Q: How can I easily use icons in my application? Q: How can I load multiple fonts? Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? - A: See https://github.com/ocornut/imgui/wiki/FAQ and misc/fonts/README.txt + >> See https://www.dearimgui.org/faq and docs/FONTS.txt + + Q&A: Concerns + ============= + + Q: Who uses Dear ImGui? + Q: Can you create elaborate/serious tools with Dear ImGui? + Q: Can you reskin the look of Dear ImGui? + Q: Why using C++ (as opposed to C)? + >> See https://www.dearimgui.org/faq Q&A: Community ============== Q: How can I help? - A: - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt + A: - Businesses: please reach out to "contact AT dearimgui.org" if you work in a place using Dear ImGui! + We can discuss ways for your company to fund development via invoiced technical support, maintenance or sponsoring contacts. + This is among the most useful thing you can do for Dear ImGui. With increased funding we can hire more people working on this project. + - Individuals: you can support continued development via PayPal donations. See README. + - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt and see how you want to help and can help! - - Businesses: convince your company to fund development via support contracts/sponsoring! This is among the most useful thing you can do for dear imgui. - - Individuals: you can also become a Patron (http://www.patreon.com/imgui) or donate on PayPal! See README. - - Disclose your usage of dear imgui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. + - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/2847). Visuals are ideal as they inspire other programmers. But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately). */ +//------------------------------------------------------------------------- +// [SECTION] INCLUDES +//------------------------------------------------------------------------- + #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS #endif #include "imgui.h" +#ifndef IMGUI_DISABLE + #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif #include "imgui_internal.h" +// System includes #include // toupper #include // vsnprintf, sscanf, printf #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier @@ -800,15 +814,40 @@ CODE #include // intptr_t #endif -// Debug options -#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL -#define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window -#define IMGUI_DEBUG_INI_SETTINGS 0 // Save additional comments in .ini file +// [Windows] OS specific includes (optional) +#if defined(_WIN32) && defined(IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) +#define IMGUI_DISABLE_WIN32_FUNCTIONS +#endif +#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#ifndef __MINGW32__ +#include // _wfopen, OpenClipboard +#else +#include +#endif +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) // UWP doesn't have all Win32 functions +#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS +#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS +#endif +#endif + +// [Apple] OS specific includes +#if defined(__APPLE__) +#include +#endif // Visual Studio warnings #ifdef _MSC_VER -#pragma warning (disable: 4127) // condition expression is constant -#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later +#pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types +#endif #endif // Clang/GCC warnings with -Weverything @@ -841,6 +880,11 @@ CODE #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif +// Debug options +#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL +#define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window +#define IMGUI_DEBUG_INI_SETTINGS 0 // Save additional comments in .ini file (particularly helps for Docking, but makes saving slower) + // When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch. static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear @@ -857,7 +901,6 @@ static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock static void SetCurrentWindow(ImGuiWindow* window); static void FindHoveredWindow(); static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags); -static void CheckStacksSize(ImGuiWindow* window, bool write); static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges); static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); @@ -866,9 +909,9 @@ static void AddWindowToSortBuffer(ImVector* out_sorted static ImRect GetViewportRect(); // Settings -static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); -static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); -static void SettingsHandlerWindow_WriteAll(ImGuiContext* imgui_ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf); +static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); +static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); +static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf); // Platform Dependents default implementation for IO functions static const char* GetClipboardTextFn_DefaultImpl(void* user_data); @@ -877,8 +920,6 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); namespace ImGui { -static bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); - // Navigation static void NavUpdate(); static void NavUpdateWindowing(); @@ -893,11 +934,17 @@ static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_win static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static int FindWindowFocusIndex(ImGuiWindow* window); +// Error Checking +static void ErrorCheckEndFrame(); +static void ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool write); + // Misc +static void UpdateSettings(); static void UpdateMouseInputs(); static void UpdateMouseWheel(); -static bool UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]); +static void UpdateTabFocus(); static void UpdateDebugToolItemPicker(); +static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]); static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); @@ -942,7 +989,7 @@ static void (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper; static void* GImAllocatorUserData = NULL; //----------------------------------------------------------------------------- -// [SECTION] MAIN USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) //----------------------------------------------------------------------------- ImGuiStyle::ImGuiStyle() @@ -975,12 +1022,13 @@ ImGuiStyle::ImGuiStyle() ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text. - DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows. + DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU. AntiAliasedFill = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. + CircleSegmentMaxError = 1.60f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. // Default theme ImGui::StyleColorsDark(this); @@ -1016,6 +1064,7 @@ ImGuiIO::ImGuiIO() { // Most fields are initialized with zero memset(this, 0, sizeof(*this)); + IM_ASSERT(IM_ARRAYSIZE(ImGuiIO::MouseDown) == ImGuiMouseButton_COUNT && IM_ARRAYSIZE(ImGuiIO::MouseClicked) == ImGuiMouseButton_COUNT); // Our pre-C++11 IM_STATIC_ASSERT() macros triggers warning on modern compilers so we don't use it here. // Settings ConfigFlags = ImGuiConfigFlags_None; @@ -1078,8 +1127,33 @@ ImGuiIO::ImGuiIO() // - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message void ImGuiIO::AddInputCharacter(unsigned int c) { - if (c > 0 && c < 0x10000) - InputQueueCharacters.push_back((ImWchar)c); + InputQueueCharacters.push_back(c > 0 && c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); +} + +// UTF16 strings use surrogate pairs to encode codepoints >= 0x10000, so +// we should save the high surrogate. +void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) +{ + if ((c & 0xFC00) == 0xD800) // High surrogate, must save + { + if (InputQueueSurrogate != 0) + InputQueueCharacters.push_back(0xFFFD); + InputQueueSurrogate = c; + return; + } + + ImWchar cp = c; + if (InputQueueSurrogate != 0) + { + if ((c & 0xFC00) != 0xDC00) // Invalid low surrogate + InputQueueCharacters.push_back(IM_UNICODE_CODEPOINT_INVALID); + else if (IM_UNICODE_CODEPOINT_MAX == (0xFFFF)) // Codepoint will not fit in ImWchar (extra parenthesis around 0xFFFF somehow fixes -Wunreachable-code with Clang) + cp = IM_UNICODE_CODEPOINT_INVALID; + else + cp = (ImWchar)(((InputQueueSurrogate - 0xD800) << 10) + (c - 0xDC00) + 0x10000); + InputQueueSurrogate = 0; + } + InputQueueCharacters.push_back(cp); } void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) @@ -1088,7 +1162,7 @@ void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { unsigned int c = 0; utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); - if (c > 0 && c < 0x10000) + if (c > 0) InputQueueCharacters.push_back((ImWchar)c); } } @@ -1099,9 +1173,77 @@ void ImGuiIO::ClearInputCharacters() } //----------------------------------------------------------------------------- -// [SECTION] MISC HELPERS/UTILITIES (Maths, String, Format, Hash, File functions) +// [SECTION] MISC HELPERS/UTILITIES (Geometry functions) //----------------------------------------------------------------------------- +ImVec2 ImBezierClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments) +{ + IM_ASSERT(num_segments > 0); // Use ImBezierClosestPointCasteljau() + ImVec2 p_last = p1; + ImVec2 p_closest; + float p_closest_dist2 = FLT_MAX; + float t_step = 1.0f / (float)num_segments; + for (int i_step = 1; i_step <= num_segments; i_step++) + { + ImVec2 p_current = ImBezierCalc(p1, p2, p3, p4, t_step * i_step); + ImVec2 p_line = ImLineClosestPoint(p_last, p_current, p); + float dist2 = ImLengthSqr(p - p_line); + if (dist2 < p_closest_dist2) + { + p_closest = p_line; + p_closest_dist2 = dist2; + } + p_last = p_current; + } + return p_closest; +} + +// Closely mimics PathBezierToCasteljau() in imgui_draw.cpp +static void BezierClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, ImVec2& p_last, float& p_closest_dist2, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) +{ + float dx = x4 - x1; + float dy = y4 - y1; + float d2 = ((x2 - x4) * dy - (y2 - y4) * dx); + float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); + d2 = (d2 >= 0) ? d2 : -d2; + d3 = (d3 >= 0) ? d3 : -d3; + if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy)) + { + ImVec2 p_current(x4, y4); + ImVec2 p_line = ImLineClosestPoint(p_last, p_current, p); + float dist2 = ImLengthSqr(p - p_line); + if (dist2 < p_closest_dist2) + { + p_closest = p_line; + p_closest_dist2 = dist2; + } + p_last = p_current; + } + else if (level < 10) + { + float x12 = (x1+x2)*0.5f, y12 = (y1+y2)*0.5f; + float x23 = (x2+x3)*0.5f, y23 = (y2+y3)*0.5f; + float x34 = (x3+x4)*0.5f, y34 = (y3+y4)*0.5f; + float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; + float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; + float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; + BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); + BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); + } +} + +// tess_tol is generally the same value you would find in ImGui::GetStyle().CurveTessellationTol +// Because those ImXXX functions are lower-level than ImGui:: we cannot access this value automatically. +ImVec2 ImBezierClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol) +{ + IM_ASSERT(tess_tol > 0.0f); + ImVec2 p_last = p1; + ImVec2 p_closest; + float p_closest_dist2 = FLT_MAX; + BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, tess_tol, 0); + return p_closest; +} + ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p) { ImVec2 ap = p - a; @@ -1150,6 +1292,10 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, return proj_ca; } +//----------------------------------------------------------------------------- +// [SECTION] MISC HELPERS/UTILITIES (String, Format, Hash functions) +//----------------------------------------------------------------------------- + // Consider using _stricmp/_strnicmp under Windows or strcasecmp/strncasecmp. We don't actually use either ImStricmp/ImStrnicmp in the codebase any more. int ImStricmp(const char* str1, const char* str2) { @@ -1203,7 +1349,7 @@ const char* ImStrchrRange(const char* str, const char* str_end, char c) int ImStrlenW(const ImWchar* str) { - //return (int)wcslen((const wchar_t*)str); // FIXME-OPT: Could use this when wchar_t are 16-bits + //return (int)wcslen((const wchar_t*)str); // FIXME-OPT: Could use this when wchar_t are 16-bit int n = 0; while (*str++) n++; return n; @@ -1261,15 +1407,25 @@ void ImStrTrimBlanks(char* buf) buf[p - p_start] = 0; // Zero terminate } +const char* ImStrSkipBlank(const char* str) +{ + while (str[0] == ' ' || str[0] == '\t') + str++; + return str; +} + // A) MSVC version appears to return -1 on overflow, whereas glibc appears to return total count (which may be >= buf_size). // Ideally we would test for only one of those limits at runtime depending on the behavior the vsnprintf(), but trying to deduct it at compile time sounds like a pandora can of worm. // B) When buf==NULL vsnprintf() will return the output size. -#ifndef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS +#ifndef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS -//#define IMGUI_USE_STB_SPRINTF +// We support stb_sprintf which is much faster (see: https://github.com/nothings/stb/blob/master/stb_sprintf.h) +// You may set IMGUI_USE_STB_SPRINTF to use our default wrapper, or set IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS +// and setup the wrapper yourself. (FIXME-OPT: Some of our high-level operations such as ImGuiTextBuffer::appendfv() are +// designed using two-passes worst case, which probably could be improved using the stbsp_vsprintfcb() function.) #ifdef IMGUI_USE_STB_SPRINTF #define STB_SPRINTF_IMPLEMENTATION -#include "imstb_sprintf.h" +#include "stb_sprintf.h" #endif #if defined(_MSC_VER) && !defined(vsnprintf) @@ -1308,7 +1464,7 @@ int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) buf[w] = 0; return w; } -#endif // #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS +#endif // #ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // CRC32 needs a 1KB lookup table (not cache friendly) // Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily: @@ -1380,58 +1536,73 @@ ImU32 ImHashStr(const char* data_p, size_t data_size, ImU32 seed) return ~crc; } -FILE* ImFileOpen(const char* filename, const char* mode) +//----------------------------------------------------------------------------- +// [SECTION] MISC HELPERS/UTILITIES (File functions) +//----------------------------------------------------------------------------- + +// Default file functions +#ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS + +ImFileHandle ImFileOpen(const char* filename, const char* mode) { -#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__GNUC__) - // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. Converting both strings from UTF-8 to wchar format (using a single allocation, because we can) - const int filename_wsize = ImTextCountCharsFromUtf8(filename, NULL) + 1; - const int mode_wsize = ImTextCountCharsFromUtf8(mode, NULL) + 1; +#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(__CYGWIN__) && !defined(__GNUC__) + // We need a fopen() wrapper because MSVC/Windows fopen doesn't handle UTF-8 filenames. + // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! + const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); + const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); ImVector buf; buf.resize(filename_wsize + mode_wsize); - ImTextStrFromUtf8(&buf[0], filename_wsize, filename, NULL); - ImTextStrFromUtf8(&buf[filename_wsize], mode_wsize, mode, NULL); - return _wfopen((wchar_t*)&buf[0], (wchar_t*)&buf[filename_wsize]); + ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize); + ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize); + return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]); #else return fopen(filename, mode); #endif } -// Load file content into memory +// We should in theory be using fseeko()/ftello() with off_t and _fseeki64()/_ftelli64() with __int64, waiting for the PR that does that in a very portable pre-C++11 zero-warnings way. +bool ImFileClose(ImFileHandle f) { return fclose(f) == 0; } +ImU64 ImFileGetSize(ImFileHandle f) { long off = 0, sz = 0; return ((off = ftell(f)) != -1 && !fseek(f, 0, SEEK_END) && (sz = ftell(f)) != -1 && !fseek(f, off, SEEK_SET)) ? (ImU64)sz : (ImU64)-1; } +ImU64 ImFileRead(void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fread(data, (size_t)sz, (size_t)count, f); } +ImU64 ImFileWrite(const void* data, ImU64 sz, ImU64 count, ImFileHandle f) { return fwrite(data, (size_t)sz, (size_t)count, f); } +#endif // #ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS + +// Helper: Load file content into memory // Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree() -void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes) +// This can't really be used with "rt" because fseek size won't match read size. +void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size, int padding_bytes) { - IM_ASSERT(filename && file_open_mode); + IM_ASSERT(filename && mode); if (out_file_size) *out_file_size = 0; - FILE* f; - if ((f = ImFileOpen(filename, file_open_mode)) == NULL) + ImFileHandle f; + if ((f = ImFileOpen(filename, mode)) == NULL) return NULL; - long file_size_signed; - if (fseek(f, 0, SEEK_END) || (file_size_signed = ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) + size_t file_size = (size_t)ImFileGetSize(f); + if (file_size == (size_t)-1) { - fclose(f); + ImFileClose(f); return NULL; } - size_t file_size = (size_t)file_size_signed; void* file_data = IM_ALLOC(file_size + padding_bytes); if (file_data == NULL) { - fclose(f); + ImFileClose(f); return NULL; } - if (fread(file_data, 1, file_size, f) != file_size) + if (ImFileRead(file_data, 1, file_size, f) != file_size) { - fclose(f); + ImFileClose(f); IM_FREE(file_data); return NULL; } if (padding_bytes > 0) memset((void*)(((char*)file_data) + file_size), 0, (size_t)padding_bytes); - fclose(f); + ImFileClose(f); if (out_file_size) *out_file_size = file_size; @@ -1442,7 +1613,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_ // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) //----------------------------------------------------------------------------- -// Convert UTF-8 to 32-bits character, process single character input. +// Convert UTF-8 to 32-bit character, process single character input. // Based on stb_from_utf8() from github.com/nothings/stb/ // We handle UTF-8 decoding error by skipping forward. int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) @@ -1457,7 +1628,7 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* } if ((*str & 0xe0) == 0xc0) { - *out_char = 0xFFFD; // will be invalid but not end of string + *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 2) return 1; if (*str < 0xc2) return 2; c = (unsigned int)((*str++ & 0x1f) << 6); @@ -1468,7 +1639,7 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* } if ((*str & 0xf0) == 0xe0) { - *out_char = 0xFFFD; // will be invalid but not end of string + *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 3) return 1; if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3; if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below @@ -1482,7 +1653,7 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* } if ((*str & 0xf8) == 0xf0) { - *out_char = 0xFFFD; // will be invalid but not end of string + *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string if (in_text_end && in_text_end - (const char*)str < 4) return 1; if (*str > 0xf4) return 4; if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4; @@ -1496,6 +1667,8 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* c += (*str++ & 0x3f); // utf-8 encodings of values used in surrogate pairs are invalid if ((c & 0xFFFFF800) == 0xD800) return 4; + // If codepoint does not fit in ImWchar, use replacement character U+FFFD instead + if (c > IM_UNICODE_CODEPOINT_MAX) c = IM_UNICODE_CODEPOINT_INVALID; *out_char = c; return 4; } @@ -1513,8 +1686,7 @@ int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const cha in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); if (c == 0) break; - if (c < 0x10000) // FIXME: Losing characters that don't fit in 2 bytes - *buf_out++ = (ImWchar)c; + *buf_out++ = (ImWchar)c; } *buf_out = 0; if (in_text_remaining) @@ -1531,8 +1703,7 @@ int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); if (c == 0) break; - if (c < 0x10000) - char_count++; + char_count++; } return char_count; } @@ -1552,11 +1723,15 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) buf[1] = (char)(0x80 + (c & 0x3f)); return 2; } - if (c >= 0xdc00 && c < 0xe000) + if (c < 0x10000) { - return 0; + if (buf_size < 3) return 0; + buf[0] = (char)(0xe0 + (c >> 12)); + buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); + buf[2] = (char)(0x80 + ((c ) & 0x3f)); + return 3; } - if (c >= 0xd800 && c < 0xdc00) + if (c <= 0x10FFFF) { if (buf_size < 4) return 0; buf[0] = (char)(0xf0 + (c >> 18)); @@ -1565,14 +1740,8 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) buf[3] = (char)(0x80 + ((c ) & 0x3f)); return 4; } - //else if (c < 0x10000) - { - if (buf_size < 3) return 0; - buf[0] = (char)(0xe0 + (c >> 12)); - buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); - buf[2] = (char)(0x80 + ((c ) & 0x3f)); - return 3; - } + // Invalid code point, the max unicode is 0x10FFFF + return 0; } // Not optimal but we very rarely use this function. @@ -1586,8 +1755,8 @@ static inline int ImTextCountUtf8BytesFromChar(unsigned int c) { if (c < 0x80) return 1; if (c < 0x800) return 2; - if (c >= 0xdc00 && c < 0xe000) return 0; - if (c >= 0xd800 && c < 0xdc00) return 4; + if (c < 0x10000) return 3; + if (c <= 0x10FFFF) return 4; return 3; } @@ -1697,38 +1866,6 @@ void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& } } -ImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul) -{ - ImGuiStyle& style = GImGui->Style; - ImVec4 c = style.Colors[idx]; - c.w *= style.Alpha * alpha_mul; - return ColorConvertFloat4ToU32(c); -} - -ImU32 ImGui::GetColorU32(const ImVec4& col) -{ - ImGuiStyle& style = GImGui->Style; - ImVec4 c = col; - c.w *= style.Alpha; - return ColorConvertFloat4ToU32(c); -} - -const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) -{ - ImGuiStyle& style = GImGui->Style; - return style.Colors[idx]; -} - -ImU32 ImGui::GetColorU32(ImU32 col) -{ - float style_alpha = GImGui->Style.Alpha; - if (style_alpha >= 1.0f) - return col; - ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; - a = (ImU32)(a * style_alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. - return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); -} - //----------------------------------------------------------------------------- // [SECTION] ImGuiStorage // Helper: Key->value storage @@ -2185,77 +2322,296 @@ bool ImGuiListClipper::Step() } //----------------------------------------------------------------------------- -// [SECTION] RENDER HELPERS -// Those (internal) functions are currently quite a legacy mess - their signature and behavior will change. -// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state. +// [SECTION] STYLING //----------------------------------------------------------------------------- -const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end) +ImGuiStyle& ImGui::GetStyle() { - const char* text_display_end = text; - if (!text_end) - text_end = (const char*)-1; + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); + return GImGui->Style; +} - while (text_display_end < text_end && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#')) - text_display_end++; - return text_display_end; +ImU32 ImGui::GetColorU32(ImGuiCol idx, float alpha_mul) +{ + ImGuiStyle& style = GImGui->Style; + ImVec4 c = style.Colors[idx]; + c.w *= style.Alpha * alpha_mul; + return ColorConvertFloat4ToU32(c); } -// Internal ImGui functions to render text -// RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText() -void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash) +ImU32 ImGui::GetColorU32(const ImVec4& col) { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; + ImGuiStyle& style = GImGui->Style; + ImVec4 c = col; + c.w *= style.Alpha; + return ColorConvertFloat4ToU32(c); +} - // Hide anything after a '##' string - const char* text_display_end; - if (hide_text_after_hash) - { - text_display_end = FindRenderedTextEnd(text, text_end); - } - else - { - if (!text_end) - text_end = text + strlen(text); // FIXME-OPT - text_display_end = text_end; - } +const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) +{ + ImGuiStyle& style = GImGui->Style; + return style.Colors[idx]; +} - if (text != text_display_end) - { - window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end); - if (g.LogEnabled) - LogRenderedText(&pos, text, text_display_end); - } +ImU32 ImGui::GetColorU32(ImU32 col) +{ + ImGuiStyle& style = GImGui->Style; + if (style.Alpha >= 1.0f) + return col; + ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; + a = (ImU32)(a * style.Alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. + return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); } -void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width) +// FIXME: This may incur a round-trip (if the end user got their data from a float4) but eventually we aim to store the in-flight colors as ImU32 +void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; + ImGuiColorMod backup; + backup.Col = idx; + backup.BackupValue = g.Style.Colors[idx]; + g.ColorModifiers.push_back(backup); + g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); +} - if (!text_end) - text_end = text + strlen(text); // FIXME-OPT +void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) +{ + ImGuiContext& g = *GImGui; + ImGuiColorMod backup; + backup.Col = idx; + backup.BackupValue = g.Style.Colors[idx]; + g.ColorModifiers.push_back(backup); + g.Style.Colors[idx] = col; +} - if (text != text_end) +void ImGui::PopStyleColor(int count) +{ + ImGuiContext& g = *GImGui; + while (count > 0) { - window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width); - if (g.LogEnabled) - LogRenderedText(&pos, text, text_end); + ImGuiColorMod& backup = g.ColorModifiers.back(); + g.Style.Colors[backup.Col] = backup.BackupValue; + g.ColorModifiers.pop_back(); + count--; } } -// Default clip_rect uses (pos_min,pos_max) -// Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges) -void ImGui::RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_display_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect) +struct ImGuiStyleVarInfo { - // Perform CPU side clipping for single clipped element to avoid using scissor state - ImVec2 pos = pos_min; - const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f); - - const ImVec2* clip_min = clip_rect ? &clip_rect->Min : &pos_min; - const ImVec2* clip_max = clip_rect ? &clip_rect->Max : &pos_max; - bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y); + ImGuiDataType Type; + ImU32 Count; + ImU32 Offset; + void* GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); } +}; + +static const ImGuiStyleVarInfo GStyleVarInfo[] = +{ + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign +}; + +static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx) +{ + IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT); + IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); + return &GStyleVarInfo[idx]; +} + +void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) +{ + const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) + { + ImGuiContext& g = *GImGui; + float* pvar = (float*)var_info->GetVarPtr(&g.Style); + g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; + return; + } + IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!"); +} + +void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) +{ + const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) + { + ImGuiContext& g = *GImGui; + ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); + g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; + return; + } + IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!"); +} + +void ImGui::PopStyleVar(int count) +{ + ImGuiContext& g = *GImGui; + while (count > 0) + { + // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. + ImGuiStyleMod& backup = g.StyleModifiers.back(); + const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); + void* data = info->GetVarPtr(&g.Style); + if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } + else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } + g.StyleModifiers.pop_back(); + count--; + } +} + +const char* ImGui::GetStyleColorName(ImGuiCol idx) +{ + // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1"; + switch (idx) + { + case ImGuiCol_Text: return "Text"; + case ImGuiCol_TextDisabled: return "TextDisabled"; + case ImGuiCol_WindowBg: return "WindowBg"; + case ImGuiCol_ChildBg: return "ChildBg"; + case ImGuiCol_PopupBg: return "PopupBg"; + case ImGuiCol_Border: return "Border"; + case ImGuiCol_BorderShadow: return "BorderShadow"; + case ImGuiCol_FrameBg: return "FrameBg"; + case ImGuiCol_FrameBgHovered: return "FrameBgHovered"; + case ImGuiCol_FrameBgActive: return "FrameBgActive"; + case ImGuiCol_TitleBg: return "TitleBg"; + case ImGuiCol_TitleBgActive: return "TitleBgActive"; + case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed"; + case ImGuiCol_MenuBarBg: return "MenuBarBg"; + case ImGuiCol_ScrollbarBg: return "ScrollbarBg"; + case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab"; + case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered"; + case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive"; + case ImGuiCol_CheckMark: return "CheckMark"; + case ImGuiCol_SliderGrab: return "SliderGrab"; + case ImGuiCol_SliderGrabActive: return "SliderGrabActive"; + case ImGuiCol_Button: return "Button"; + case ImGuiCol_ButtonHovered: return "ButtonHovered"; + case ImGuiCol_ButtonActive: return "ButtonActive"; + case ImGuiCol_Header: return "Header"; + case ImGuiCol_HeaderHovered: return "HeaderHovered"; + case ImGuiCol_HeaderActive: return "HeaderActive"; + case ImGuiCol_Separator: return "Separator"; + case ImGuiCol_SeparatorHovered: return "SeparatorHovered"; + case ImGuiCol_SeparatorActive: return "SeparatorActive"; + case ImGuiCol_ResizeGrip: return "ResizeGrip"; + case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; + case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; + case ImGuiCol_Tab: return "Tab"; + case ImGuiCol_TabHovered: return "TabHovered"; + case ImGuiCol_TabActive: return "TabActive"; + case ImGuiCol_TabUnfocused: return "TabUnfocused"; + case ImGuiCol_TabUnfocusedActive: return "TabUnfocusedActive"; + case ImGuiCol_PlotLines: return "PlotLines"; + case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; + case ImGuiCol_PlotHistogram: return "PlotHistogram"; + case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered"; + case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; + case ImGuiCol_DragDropTarget: return "DragDropTarget"; + case ImGuiCol_NavHighlight: return "NavHighlight"; + case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight"; + case ImGuiCol_NavWindowingDimBg: return "NavWindowingDimBg"; + case ImGuiCol_ModalWindowDimBg: return "ModalWindowDimBg"; + } + IM_ASSERT(0); + return "Unknown"; +} + +//----------------------------------------------------------------------------- +// [SECTION] RENDER HELPERS +// Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change. +// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state. +//----------------------------------------------------------------------------- + +const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end) +{ + const char* text_display_end = text; + if (!text_end) + text_end = (const char*)-1; + + while (text_display_end < text_end && *text_display_end != '\0' && (text_display_end[0] != '#' || text_display_end[1] != '#')) + text_display_end++; + return text_display_end; +} + +// Internal ImGui functions to render text +// RenderText***() functions calls ImDrawList::AddText() calls ImBitmapFont::RenderText() +void ImGui::RenderText(ImVec2 pos, const char* text, const char* text_end, bool hide_text_after_hash) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + // Hide anything after a '##' string + const char* text_display_end; + if (hide_text_after_hash) + { + text_display_end = FindRenderedTextEnd(text, text_end); + } + else + { + if (!text_end) + text_end = text + strlen(text); // FIXME-OPT + text_display_end = text_end; + } + + if (text != text_display_end) + { + window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end); + if (g.LogEnabled) + LogRenderedText(&pos, text, text_display_end); + } +} + +void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (!text_end) + text_end = text + strlen(text); // FIXME-OPT + + if (text != text_end) + { + window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_end, wrap_width); + if (g.LogEnabled) + LogRenderedText(&pos, text, text_end); + } +} + +// Default clip_rect uses (pos_min,pos_max) +// Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges) +void ImGui::RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_display_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect) +{ + // Perform CPU side clipping for single clipped element to avoid using scissor state + ImVec2 pos = pos_min; + const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f); + + const ImVec2* clip_min = clip_rect ? &clip_rect->Min : &pos_min; + const ImVec2* clip_max = clip_rect ? &clip_rect->Max : &pos_max; + bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y); if (clip_rect) // If we had no explicit clipping rectangle then pos==clip_min need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y); @@ -2517,6 +2873,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) SkipItems = false; Appearing = false; Hidden = false; + IsFallbackWindow = false; HasCloseButton = false; ResizeBorderHeld = -1; BeginCount = 0; @@ -2537,7 +2894,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) LastTimeActive = -1.0f; ItemWidthDefault = 0.0f; FontWindowScale = 1.0f; - SettingsIdx = -1; + SettingsOffset = -1; DrawList = &DrawListInst; DrawList->_OwnerName = Name; @@ -2652,25 +3009,6 @@ void ImGui::GcAwakeTransientWindowBuffers(ImGuiWindow* window) window->MemoryDrawListIdxCapacity = window->MemoryDrawListVtxCapacity = 0; } -void ImGui::SetNavID(ImGuiID id, int nav_layer) -{ - ImGuiContext& g = *GImGui; - IM_ASSERT(g.NavWindow); - IM_ASSERT(nav_layer == 0 || nav_layer == 1); - g.NavId = id; - g.NavWindow->NavLastIds[nav_layer] = id; -} - -void ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel) -{ - ImGuiContext& g = *GImGui; - SetNavID(id, nav_layer); - g.NavWindow->NavRectRel[nav_layer] = rect_rel; - g.NavMousePosDirty = true; - g.NavDisableHighlight = false; - g.NavDisableMouseHover = true; -} - void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) { ImGuiContext& g = *GImGui; @@ -2703,29 +3041,6 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) g.ActiveIdUsingKeyInputMask = 0x00; } -// FIXME-NAV: The existence of SetNavID/SetNavIDWithRectRel/SetFocusID is incredibly messy and confusing and needs some explanation or refactoring. -void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) -{ - ImGuiContext& g = *GImGui; - IM_ASSERT(id != 0); - - // Assume that SetFocusID() is called in the context where its NavLayer is the current layer, which is the case everywhere we call it. - const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; - if (g.NavWindow != window) - g.NavInitRequest = false; - g.NavId = id; - g.NavWindow = window; - g.NavLayer = nav_layer; - window->NavLastIds[nav_layer] = id; - if (window->DC.LastItemId == id) - window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos); - - if (g.ActiveIdSource == ImGuiInputSource_Nav) - g.NavDisableMouseHover = true; - else - g.NavDisableHighlight = true; -} - void ImGui::ClearActiveID() { SetActiveID(0, NULL); @@ -2784,98 +3099,6 @@ static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFla if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup)) return false; } - - return true; -} - -// Advance cursor given item size for layout. -void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (window->SkipItems) - return; - - // Always align ourselves on pixel boundaries - const float line_height = ImMax(window->DC.CurrLineSize.y, size.y); - //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] - window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; - window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y; - window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line - window->DC.CursorPos.y = (float)(int)(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y); // Next line - window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); - window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); - //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] - - window->DC.PrevLineSize.y = line_height; - window->DC.CurrLineSize.y = 0.0f; - window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); - window->DC.CurrLineTextBaseOffset = 0.0f; - - // Horizontal layout mode - if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) - SameLine(); -} - -void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) -{ - ItemSize(bb.GetSize(), text_baseline_y); -} - -// Declare item bounding box for clipping and interaction. -// Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface -// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). -bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - - if (id != 0) - { - // Navigation processing runs prior to clipping early-out - // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget - // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests - // unfortunately, but it is still limited to one window. It may not scale very well for windows with ten of - // thousands of item, but at least NavMoveRequest is only set on user interaction, aka maximum once a frame. - // We could early out with "if (is_clipped && !g.NavInitRequest) return false;" but when we wouldn't be able - // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). - // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. - // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. - window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask; - if (g.NavId == id || g.NavAnyRequest) - if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) - if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) - NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id); - - // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() -#ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX - if (id == g.DebugItemPickerBreakID) - { - IM_DEBUG_BREAK(); - g.DebugItemPickerBreakID = 0; - } -#endif - } - - window->DC.LastItemId = id; - window->DC.LastItemRect = bb; - window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; - g.NextItemData.Flags = ImGuiNextItemDataFlags_None; - -#ifdef IMGUI_ENABLE_TEST_ENGINE - if (id != 0) - IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id); -#endif - - // Clipping test - const bool is_clipped = IsClippedEx(bb, id, false); - if (is_clipped) - return false; - //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] - - // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) - if (IsMouseHoveringRect(bb.Min, bb.Max)) - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect; return true; } @@ -2951,7 +3174,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) // #define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX in imconfig.h if you want this check to also be performed in ItemAdd(). if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); - if (g.DebugItemPickerBreakID == id) + if (g.DebugItemPickerBreakId == id) IM_DEBUG_BREAK(); return true; @@ -2975,24 +3198,24 @@ bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) // Increment counters const bool is_tab_stop = (window->DC.ItemFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; - window->DC.FocusCounterAll++; + window->DC.FocusCounterRegular++; if (is_tab_stop) - window->DC.FocusCounterTab++; + window->DC.FocusCounterTabStop++; // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. // (Note that we can always TAB out of a widget that doesn't allow tabbing in) if (g.ActiveId == id && g.FocusTabPressed && !IsActiveIdUsingKey(ImGuiKey_Tab) && g.FocusRequestNextWindow == NULL) { g.FocusRequestNextWindow = window; - g.FocusRequestNextCounterTab = window->DC.FocusCounterTab + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. + g.FocusRequestNextCounterTabStop = window->DC.FocusCounterTabStop + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. } // Handle focus requests if (g.FocusRequestCurrWindow == window) { - if (window->DC.FocusCounterAll == g.FocusRequestCurrCounterAll) + if (window->DC.FocusCounterRegular == g.FocusRequestCurrCounterRegular) return true; - if (is_tab_stop && window->DC.FocusCounterTab == g.FocusRequestCurrCounterTab) + if (is_tab_stop && window->DC.FocusCounterTabStop == g.FocusRequestCurrCounterTabStop) { g.NavJustTabbedId = id; return true; @@ -3008,8 +3231,8 @@ bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) void ImGui::FocusableItemUnregister(ImGuiWindow* window) { - window->DC.FocusCounterAll--; - window->DC.FocusCounterTab--; + window->DC.FocusCounterRegular--; + window->DC.FocusCounterTabStop--; } float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) @@ -3077,24 +3300,6 @@ void ImGui::SetCurrentContext(ImGuiContext* ctx) #endif } -// Helper function to verify ABI compatibility between caller code and compiled version of Dear ImGui. -// Verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit -// If the user has inconsistent compilation settings, imgui configuration #define, packing pragma, etc. your user code -// may see different structures than what imgui.cpp sees, which is problematic. -// We usually require settings to be in imconfig.h to make sure that they are accessible to all compilation units involved with Dear ImGui. -bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx) -{ - bool error = false; - if (strcmp(version, IMGUI_VERSION)!=0) { error = true; IM_ASSERT(strcmp(version,IMGUI_VERSION)==0 && "Mismatched version string!"); } - if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } - if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); } - if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } - if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } - if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } - if (sz_idx != sizeof(ImDrawIdx)) { error = true; IM_ASSERT(sz_idx == sizeof(ImDrawIdx) && "Mismatched struct layout!"); } - return !error; -} - void ImGui::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data) { GImAllocatorAllocFunc = alloc_func; @@ -3127,12 +3332,6 @@ ImGuiIO& ImGui::GetIO() return GImGui->IO; } -ImGuiStyle& ImGui::GetStyle() -{ - IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); - return GImGui->Style; -} - // Same value as passed to the old io.RenderDrawListsFn function. Valid after Render() and until the next call to NewFrame() ImDrawData* ImGui::GetDrawData() { @@ -3185,6 +3384,9 @@ void ImGui::StartMouseMovingWindow(ImGuiWindow* window) // Handle mouse moving window // Note: moving window with the navigation keys (Square + d-pad / CTRL+TAB + Arrows) are processed in NavUpdateWindowing() +// FIXME: We don't have strong guarantee that g.MovingWindow stay synched with g.ActiveId == g.MovingWindow->MoveId. +// This is currently enforced by the fact that BeginDragDropSource() is setting all g.ActiveIdUsingXXXX flags to inhibit navigation inputs, +// but if we should more thoroughly test cases where g.ActiveId or g.MovingWindow gets changed and not the other. void ImGui::UpdateMouseMovingWindowNewFrame() { ImGuiContext& g = *GImGui; @@ -3223,10 +3425,10 @@ void ImGui::UpdateMouseMovingWindowNewFrame() } } -// Initiate moving window, handle left-click and right-click focus +// Initiate moving window when clicking on empty space or title bar. +// Handle left-click and right-click focus. void ImGui::UpdateMouseMovingWindowEndFrame() { - // Initiate moving window ImGuiContext& g = *GImGui; if (g.ActiveId != 0 || g.HoveredId != 0) return; @@ -3311,7 +3513,7 @@ static void ImGui::UpdateMouseInputs() ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) g.IO.MouseDoubleClicked[i] = true; - g.IO.MouseClickedTime[i] = -FLT_MAX; // so the third click isn't turned into a double-click + g.IO.MouseClickedTime[i] = -DBL_MAX; // so the third click isn't turned into a double-click } else { @@ -3423,6 +3625,42 @@ void ImGui::UpdateMouseWheel() } } +void ImGui::UpdateTabFocus() +{ + ImGuiContext& g = *GImGui; + + // Pressing TAB activate widget focus + g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); + if (g.ActiveId == 0 && g.FocusTabPressed) + { + // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also + // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. + g.FocusRequestNextWindow = g.NavWindow; + g.FocusRequestNextCounterRegular = INT_MAX; + if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) + g.FocusRequestNextCounterTabStop = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); + else + g.FocusRequestNextCounterTabStop = g.IO.KeyShift ? -1 : 0; + } + + // Turn queued focus request into current one + g.FocusRequestCurrWindow = NULL; + g.FocusRequestCurrCounterRegular = g.FocusRequestCurrCounterTabStop = INT_MAX; + if (g.FocusRequestNextWindow != NULL) + { + ImGuiWindow* window = g.FocusRequestNextWindow; + g.FocusRequestCurrWindow = window; + if (g.FocusRequestNextCounterRegular != INT_MAX && window->DC.FocusCounterRegular != -1) + g.FocusRequestCurrCounterRegular = ImModPositive(g.FocusRequestNextCounterRegular, window->DC.FocusCounterRegular + 1); + if (g.FocusRequestNextCounterTabStop != INT_MAX && window->DC.FocusCounterTabStop != -1) + g.FocusRequestCurrCounterTabStop = ImModPositive(g.FocusRequestNextCounterTabStop, window->DC.FocusCounterTabStop + 1); + g.FocusRequestNextWindow = NULL; + g.FocusRequestNextCounterRegular = g.FocusRequestNextCounterTabStop = INT_MAX; + } + + g.NavIdTabCounter = INT_MAX; +} + // The reason this is exposed in imgui_internal.h is: on touch-based system that don't have hovering, we want to dispatch inputs to the right target (imgui vs imgui+app) void ImGui::UpdateHoveredWindowAndCaptureFlags() { @@ -3495,6 +3733,7 @@ static void NewFrameSanityChecks() IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.CircleSegmentMaxError > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!"); IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); @@ -3522,34 +3761,21 @@ void ImGui::NewFrame() // Check and assert for various common IO and Configuration mistakes NewFrameSanityChecks(); - // Load settings on first frame (if not explicitly loaded manually before) - if (!g.SettingsLoaded) - { - IM_ASSERT(g.SettingsWindows.empty()); - if (g.IO.IniFilename) - LoadIniSettingsFromDisk(g.IO.IniFilename); - g.SettingsLoaded = true; - } - - // Save settings (with a delay after the last modification, so we don't spam disk too much) - if (g.SettingsDirtyTimer > 0.0f) - { - g.SettingsDirtyTimer -= g.IO.DeltaTime; - if (g.SettingsDirtyTimer <= 0.0f) - { - if (g.IO.IniFilename != NULL) - SaveIniSettingsToDisk(g.IO.IniFilename); - else - g.IO.WantSaveIniSettings = true; // Let user know they can call SaveIniSettingsToMemory(). user will need to clear io.WantSaveIniSettings themselves. - g.SettingsDirtyTimer = 0.0f; - } - } + // Load settings on first frame, save settings when modified (after a delay) + UpdateSettings(); g.Time += g.IO.DeltaTime; - g.FrameScopeActive = true; + g.WithinFrameScope = true; g.FrameCount += 1; g.TooltipOverrideCount = 0; g.WindowsActiveCount = 0; + g.MenusIdSubmittedThisFrame.resize(0); + + // Calculate frame-rate for the user, as a purely luxurious feature + g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; + g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; + g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); + g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; // Setup current font and draw list shared data g.IO.Fonts->Locked = true; @@ -3557,6 +3783,7 @@ void ImGui::NewFrame() IM_ASSERT(g.Font->IsLoaded()); g.DrawListSharedData.ClipRectFullscreen = ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; + g.DrawListSharedData.SetCircleSegmentMaxError(g.Style.CircleSegmentMaxError); g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; if (g.Style.AntiAliasedLines) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; @@ -3580,7 +3807,7 @@ void ImGui::NewFrame() if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) KeepAliveID(g.DragDropPayload.SourceId); - // Clear reference to active widget if the widget isn't alive anymore + // Update HoveredId data if (!g.HoveredIdPreviousFrame) g.HoveredIdTimer = 0.0f; if (!g.HoveredIdPreviousFrame || (g.HoveredId && g.ActiveId == g.HoveredId)) @@ -3592,6 +3819,8 @@ void ImGui::NewFrame() g.HoveredIdPreviousFrame = g.HoveredId; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; + + // Update ActiveId data (clear reference to active widget if the widget isn't alive anymore) if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) ClearActiveID(); if (g.ActiveId) @@ -3604,19 +3833,21 @@ void ImGui::NewFrame() g.ActiveIdHasBeenEditedThisFrame = false; g.ActiveIdPreviousFrameIsAlive = false; g.ActiveIdIsJustActivated = false; - if (g.TempInputTextId != 0 && g.ActiveId != g.TempInputTextId) - g.TempInputTextId = 0; + if (g.TempInputId != 0 && g.ActiveId != g.TempInputId) + g.TempInputId = 0; if (g.ActiveId == 0) { - g.ActiveIdUsingNavDirMask = g.ActiveIdUsingNavInputMask = 0; - g.ActiveIdUsingKeyInputMask = 0; + g.ActiveIdUsingNavDirMask = 0x00; + g.ActiveIdUsingNavInputMask = 0x00; + g.ActiveIdUsingKeyInputMask = 0x00; } // Drag and drop g.DragDropAcceptIdPrev = g.DragDropAcceptIdCurr; g.DragDropAcceptIdCurr = 0; g.DragDropAcceptIdCurrRectSurface = FLT_MAX; - g.DragDropWithinSourceOrTarget = false; + g.DragDropWithinSource = false; + g.DragDropWithinTarget = false; // Update keyboard input state memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); @@ -3629,12 +3860,6 @@ void ImGui::NewFrame() // Update mouse input state UpdateMouseInputs(); - // Calculate frame-rate for the user, as a purely luxurious feature - g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; - g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); - g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; - // Find hovered window // (needs to be before UpdateMouseMovingWindowNewFrame so we fill g.HoveredWindowUnderMovingWindow on the mouse release frame) UpdateHoveredWindowAndCaptureFlags(); @@ -3655,36 +3880,8 @@ void ImGui::NewFrame() // Mouse wheel scrolling, scale UpdateMouseWheel(); - // Pressing TAB activate widget focus - g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); - if (g.ActiveId == 0 && g.FocusTabPressed) - { - // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also - // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. - g.FocusRequestNextWindow = g.NavWindow; - g.FocusRequestNextCounterAll = INT_MAX; - if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) - g.FocusRequestNextCounterTab = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); - else - g.FocusRequestNextCounterTab = g.IO.KeyShift ? -1 : 0; - } - - // Turn queued focus request into current one - g.FocusRequestCurrWindow = NULL; - g.FocusRequestCurrCounterAll = g.FocusRequestCurrCounterTab = INT_MAX; - if (g.FocusRequestNextWindow != NULL) - { - ImGuiWindow* window = g.FocusRequestNextWindow; - g.FocusRequestCurrWindow = window; - if (g.FocusRequestNextCounterAll != INT_MAX && window->DC.FocusCounterAll != -1) - g.FocusRequestCurrCounterAll = ImModPositive(g.FocusRequestNextCounterAll, window->DC.FocusCounterAll + 1); - if (g.FocusRequestNextCounterTab != INT_MAX && window->DC.FocusCounterTab != -1) - g.FocusRequestCurrCounterTab = ImModPositive(g.FocusRequestNextCounterTab, window->DC.FocusCounterTab + 1); - g.FocusRequestNextWindow = NULL; - g.FocusRequestNextCounterAll = g.FocusRequestNextCounterTab = INT_MAX; - } - - g.NavIdTabCounter = INT_MAX; + // Update legacy TAB focus + UpdateTabFocus(); // Mark all windows as not visible and compact unused memory. IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size); @@ -3697,7 +3894,7 @@ void ImGui::NewFrame() window->Active = false; window->WriteAccessed = false; - // Garbage collect (this is totally functional but we may need decide if the side-effects are desirable) + // Garbage collect transient buffers of recently unused windows if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) GcCompactTransientWindowBuffers(window); } @@ -3718,9 +3915,10 @@ void ImGui::NewFrame() // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. // This fallback is particularly important as it avoid ImGui:: calls from crashing. + g.WithinFrameScopeWithImplicitWindow = true; SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver); Begin("Debug##Default"); - g.FrameScopePushedImplicitWindow = true; + IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); #ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiTestEngineHook_PostNewFrame(&g); @@ -3731,7 +3929,7 @@ void ImGui::NewFrame() void ImGui::UpdateDebugToolItemPicker() { ImGuiContext& g = *GImGui; - g.DebugItemPickerBreakID = 0; + g.DebugItemPickerBreakId = 0; if (g.DebugItemPickerActive) { const ImGuiID hovered_id = g.HoveredIdPreviousFrame; @@ -3740,7 +3938,7 @@ void ImGui::UpdateDebugToolItemPicker() g.DebugItemPickerActive = false; if (ImGui::IsMouseClicked(0) && hovered_id) { - g.DebugItemPickerBreakID = hovered_id; + g.DebugItemPickerBreakId = hovered_id; g.DebugItemPickerActive = false; } ImGui::SetNextWindowBgAlpha(0.60f); @@ -3758,13 +3956,31 @@ void ImGui::Initialize(ImGuiContext* context) IM_ASSERT(!g.Initialized && !g.SettingsLoaded); // Add .ini handle for ImGuiWindow type - ImGuiSettingsHandler ini_handler; - ini_handler.TypeName = "Window"; - ini_handler.TypeHash = ImHashStr("Window"); - ini_handler.ReadOpenFn = SettingsHandlerWindow_ReadOpen; - ini_handler.ReadLineFn = SettingsHandlerWindow_ReadLine; - ini_handler.WriteAllFn = SettingsHandlerWindow_WriteAll; - g.SettingsHandlers.push_back(ini_handler); + { + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "Window"; + ini_handler.TypeHash = ImHashStr("Window"); + ini_handler.ReadOpenFn = WindowSettingsHandler_ReadOpen; + ini_handler.ReadLineFn = WindowSettingsHandler_ReadLine; + ini_handler.WriteAllFn = WindowSettingsHandler_WriteAll; + g.SettingsHandlers.push_back(ini_handler); + } + +#ifdef IMGUI_HAS_TABLE + // Add .ini handle for ImGuiTable type + { + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "Table"; + ini_handler.TypeHash = ImHashStr("Table"); + ini_handler.ReadOpenFn = TableSettingsHandler_ReadOpen; + ini_handler.ReadLineFn = TableSettingsHandler_ReadLine; + ini_handler.WriteAllFn = TableSettingsHandler_WriteAll; + g.SettingsHandlers.push_back(ini_handler); + } +#endif // #ifdef IMGUI_HAS_TABLE + +#ifdef IMGUI_HAS_DOCK +#endif // #ifdef IMGUI_HAS_DOCK g.Initialized = true; } @@ -3799,7 +4015,7 @@ void ImGui::Shutdown(ImGuiContext* context) IM_DELETE(g.Windows[i]); g.Windows.clear(); g.WindowsFocusOrder.clear(); - g.WindowsSortBuffer.clear(); + g.WindowsTempSortBuffer.clear(); g.CurrentWindow = NULL; g.CurrentWindowStack.clear(); g.WindowsById.Clear(); @@ -3821,16 +4037,18 @@ void ImGui::Shutdown(ImGuiContext* context) g.ShrinkWidthBuffer.clear(); g.PrivateClipboard.clear(); + g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); - for (int i = 0; i < g.SettingsWindows.Size; i++) - IM_DELETE(g.SettingsWindows[i].Name); g.SettingsWindows.clear(); g.SettingsHandlers.clear(); - if (g.LogFile && g.LogFile != stdout) + if (g.LogFile) { - fclose(g.LogFile); +#ifndef IMGUI_DISABLE_TTY_FUNCTIONS + if (g.LogFile != stdout) +#endif + ImFileClose(g.LogFile); g.LogFile = NULL; } g.LogBuffer.clear(); @@ -3896,7 +4114,7 @@ static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* d // (A) Handle the ImDrawCmd::VtxOffset value in your renderer back-end, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. // Most example back-ends already support this from 1.71. Pre-1.71 back-ends won't. // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. - // (B) Or handle 32-bits indices in your renderer back-end, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. + // (B) Or handle 32-bit indices in your renderer back-end, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. // Most example back-ends already support this. For example, the OpenGL example code detect index size at compile-time: // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); // Your own engine or render API may use different parameters or function calls to specify index sizes. @@ -3926,10 +4144,8 @@ static void AddWindowToDrawData(ImVector* out_render_list, ImGuiWin static void AddRootWindowToDrawData(ImGuiWindow* window) { ImGuiContext& g = *GImGui; - if (window->Flags & ImGuiWindowFlags_Tooltip) - AddWindowToDrawData(&g.DrawDataBuilder.Layers[1], window); - else - AddWindowToDrawData(&g.DrawDataBuilder.Layers[0], window); + int layer = (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0; + AddWindowToDrawData(&g.DrawDataBuilder.Layers[layer], window); } void ImDrawDataBuilder::FlattenIntoSingleLayer() @@ -3989,7 +4205,7 @@ void ImGui::EndFrame() IM_ASSERT(g.Initialized); if (g.FrameCountEnded == g.FrameCount) // Don't process EndFrame() multiple times. return; - IM_ASSERT(g.FrameScopeActive && "Forgot to call ImGui::NewFrame()?"); + IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?"); // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) if (g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f)) @@ -3998,24 +4214,10 @@ void ImGui::EndFrame() g.PlatformImeLastPos = g.PlatformImePos; } - // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you - // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). - if (g.CurrentWindowStack.Size != 1) - { - if (g.CurrentWindowStack.Size > 1) - { - IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); - while (g.CurrentWindowStack.Size > 1) // FIXME-ERRORHANDLING - End(); - } - else - { - IM_ASSERT(g.CurrentWindowStack.Size == 1 && "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); - } - } + ErrorCheckEndFrame(); // Hide implicit/fallback "Debug" window if it hasn't been used - g.FrameScopePushedImplicitWindow = false; + g.WithinFrameScopeWithImplicitWindow = false; if (g.CurrentWindow && !g.CurrentWindow->WriteAccessed) g.CurrentWindow->Active = false; End(); @@ -4036,13 +4238,13 @@ void ImGui::EndFrame() // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing. if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount) { - g.DragDropWithinSourceOrTarget = true; + g.DragDropWithinSource = true; SetTooltip("..."); - g.DragDropWithinSourceOrTarget = false; + g.DragDropWithinSource = false; } // End frame - g.FrameScopeActive = false; + g.WithinFrameScope = false; g.FrameCountEnded = g.FrameCount; // Initiate moving window + handle left-click and right-click focus @@ -4050,19 +4252,19 @@ void ImGui::EndFrame() // Sort the window list so that all child windows are after their parent // We cannot do that on FocusWindow() because childs may not exist yet - g.WindowsSortBuffer.resize(0); - g.WindowsSortBuffer.reserve(g.Windows.Size); + g.WindowsTempSortBuffer.resize(0); + g.WindowsTempSortBuffer.reserve(g.Windows.Size); for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; if (window->Active && (window->Flags & ImGuiWindowFlags_ChildWindow)) // if a child is active its parent will add it continue; - AddWindowToSortBuffer(&g.WindowsSortBuffer, window); + AddWindowToSortBuffer(&g.WindowsTempSortBuffer, window); } // This usually assert if there is a mismatch between the ImGuiWindowFlags_ChildWindow / ParentWindow values and DC.ChildWindows[] in parents, aka we've done something wrong. - IM_ASSERT(g.Windows.Size == g.WindowsSortBuffer.Size); - g.Windows.swap(g.WindowsSortBuffer); + IM_ASSERT(g.Windows.Size == g.WindowsTempSortBuffer.Size); + g.Windows.swap(g.WindowsTempSortBuffer); g.IO.MetricsActiveWindows = g.WindowsActiveCount; // Unlock font atlas @@ -4082,16 +4284,17 @@ void ImGui::Render() if (g.FrameCountEnded != g.FrameCount) EndFrame(); g.FrameCountRendered = g.FrameCount; - - // Gather ImDrawList to render (for each active window) - g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = g.IO.MetricsRenderWindows = 0; + g.IO.MetricsRenderWindows = 0; g.DrawDataBuilder.Clear(); + + // Add background ImDrawList if (!g.BackgroundDrawList.VtxBuffer.empty()) AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList); + // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; - windows_to_render_top_most[1] = g.NavWindowingTarget ? g.NavWindowingList : NULL; + windows_to_render_top_most[1] = (g.NavWindowingTarget ? g.NavWindowingList : NULL); for (int n = 0; n != g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; @@ -4107,6 +4310,7 @@ void ImGui::Render() if (g.IO.MouseDrawCursor) RenderMouseCursor(&g.ForegroundDrawList, g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); + // Add foreground ImDrawList if (!g.ForegroundDrawList.VtxBuffer.empty()) AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.ForegroundDrawList); @@ -4141,7 +4345,7 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL); // Round - text_size.x = (float)(int)(text_size.x + 0.95f); + text_size.x = IM_FLOOR(text_size.x + 0.95f); return text_size; } @@ -4215,7 +4419,8 @@ int ImGui::GetKeyIndex(ImGuiKey imgui_key) return g.IO.KeyMap[imgui_key]; } -// Note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your back-end/engine stored them into io.KeysDown[]! +// Note that dear imgui doesn't know the semantic of each entry of io.KeysDown[]! +// Use your own indices/enums according to how your back-end/engine stored them into io.KeysDown[]! bool ImGui::IsKeyDown(int user_key_index) { if (user_key_index < 0) @@ -4275,23 +4480,14 @@ bool ImGui::IsKeyReleased(int user_key_index) return g.IO.KeysDownDurationPrev[user_key_index] >= 0.0f && !g.IO.KeysDown[user_key_index]; } -bool ImGui::IsMouseDown(int button) +bool ImGui::IsMouseDown(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseDown[button]; } -bool ImGui::IsAnyMouseDown() -{ - ImGuiContext& g = *GImGui; - for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) - if (g.IO.MouseDown[n]) - return true; - return false; -} - -bool ImGui::IsMouseClicked(int button, bool repeat) +bool ImGui::IsMouseClicked(ImGuiMouseButton button, bool repeat) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4306,18 +4502,17 @@ bool ImGui::IsMouseClicked(int button, bool repeat) if (amount > 0) return true; } - return false; } -bool ImGui::IsMouseReleased(int button) +bool ImGui::IsMouseReleased(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); return g.IO.MouseReleased[button]; } -bool ImGui::IsMouseDoubleClicked(int button) +bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4325,7 +4520,7 @@ bool ImGui::IsMouseDoubleClicked(int button) } // [Internal] This doesn't test if the button is pressed -bool ImGui::IsMouseDragPastThreshold(int button, float lock_threshold) +bool ImGui::IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4334,7 +4529,7 @@ bool ImGui::IsMouseDragPastThreshold(int button, float lock_threshold) return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; } -bool ImGui::IsMouseDragging(int button, float lock_threshold) +bool ImGui::IsMouseDragging(ImGuiMouseButton button, float lock_threshold) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4345,7 +4540,8 @@ bool ImGui::IsMouseDragging(int button, float lock_threshold) ImVec2 ImGui::GetMousePos() { - return GImGui->IO.MousePos; + ImGuiContext& g = *GImGui; + return g.IO.MousePos; } // NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! @@ -4368,10 +4564,19 @@ bool ImGui::IsMousePosValid(const ImVec2* mouse_pos) return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID; } +bool ImGui::IsAnyMouseDown() +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) + if (g.IO.MouseDown[n]) + return true; + return false; +} + // Return the delta from the initial clicking position while the mouse button is clicked or was just released. // This is locked and return 0.0f until the mouse moves past a distance threshold at least once. // NB: This is only valid if IsMousePosValid(). Back-ends in theory should always keep mouse position valid when dragging even outside the client window. -ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold) +ImVec2 ImGui::GetMouseDragDelta(ImGuiMouseButton button, float lock_threshold) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4384,7 +4589,7 @@ ImVec2 ImGui::GetMouseDragDelta(int button, float lock_threshold) return ImVec2(0.0f, 0.0f); } -void ImGui::ResetMouseDragDelta(int button) +void ImGui::ResetMouseDragDelta(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -4460,11 +4665,17 @@ bool ImGui::IsItemFocused() return true; } -bool ImGui::IsItemClicked(int mouse_button) +bool ImGui::IsItemClicked(ImGuiMouseButton mouse_button) { return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); } +bool ImGui::IsItemToggledOpen() +{ + ImGuiContext& g = *GImGui; + return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; +} + bool ImGui::IsItemToggledSelection() { ImGuiContext& g = *GImGui; @@ -4535,7 +4746,7 @@ static ImRect GetViewportRect() return ImRect(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); } -static bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) +bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; @@ -4603,7 +4814,10 @@ void ImGui::EndChild() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() callss + IM_ASSERT(g.WithinEndChild == false); + IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() calls + + g.WithinEndChild = true; if (window->BeginCount > 1) { End(); @@ -4635,6 +4849,7 @@ void ImGui::EndChild() ItemAdd(bb, 0); } } + g.WithinEndChild = false; } // Helper to create a child window / scrolling region that looks like a normal widget frame. @@ -4657,22 +4872,6 @@ void ImGui::EndChildFrame() EndChild(); } -// Save and compare stack sizes on Begin()/End() to detect usage errors -static void CheckStacksSize(ImGuiWindow* window, bool write) -{ - // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) - ImGuiContext& g = *GImGui; - short* p_backup = &window->DC.StackSizesBackup[0]; - { int current = window->IDStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "PushID/PopID or TreeNode/TreePop Mismatch!"); p_backup++; } // Too few or too many PopID()/TreePop() - { int current = window->DC.GroupStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "BeginGroup/EndGroup Mismatch!"); p_backup++; } // Too few or too many EndGroup() - { int current = g.BeginPopupStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup == current && "BeginMenu/EndMenu or BeginPopup/EndPopup Mismatch"); p_backup++;}// Too few or too many EndMenu()/EndPopup() - // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. - { int current = g.ColorModifiers.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushStyleColor/PopStyleColor Mismatch!"); p_backup++; } // Too few or too many PopStyleColor() - { int current = g.StyleModifiers.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushStyleVar/PopStyleVar Mismatch!"); p_backup++; } // Too few or too many PopStyleVar() - { int current = g.FontStack.Size; if (write) *p_backup = (short)current; else IM_ASSERT(*p_backup >= current && "PushFont/PopFont Mismatch!"); p_backup++; } // Too few or too many PopFont() - IM_ASSERT(p_backup == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup)); -} - static void SetWindowConditionAllowFlags(ImGuiWindow* window, ImGuiCond flags, bool enabled) { window->SetWindowPosAllowFlags = enabled ? (window->SetWindowPosAllowFlags | flags) : (window->SetWindowPosAllowFlags & ~flags); @@ -4710,7 +4909,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID)) { // Retrieve settings from .ini file - window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); + window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); window->Pos = ImVec2(settings->Pos.x, settings->Pos.y); window->Collapsed = settings->Collapsed; @@ -4761,15 +4960,16 @@ static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size g.NextWindowData.SizeCallback(&data); new_size = data.DesiredSize; } - new_size.x = ImFloor(new_size.x); - new_size.y = ImFloor(new_size.y); + new_size.x = IM_FLOOR(new_size.x); + new_size.y = IM_FLOOR(new_size.y); } // Minimum size if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) { + ImGuiWindow* window_for_height = window; new_size = ImMax(new_size, g.Style.WindowMinSize); - new_size.y = ImMax(new_size.y, window->TitleBarHeight() + window->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows + new_size.y = ImMax(new_size.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows } return new_size; } @@ -4783,8 +4983,8 @@ static ImVec2 CalcWindowContentSize(ImGuiWindow* window) return window->ContentSize; ImVec2 sz; - sz.x = (float)(int)((window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); - sz.y = (float)(int)((window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); + sz.x = IM_FLOOR((window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); + sz.y = IM_FLOOR((window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); return sz; } @@ -4863,10 +5063,10 @@ struct ImGuiResizeGripDef static const ImGuiResizeGripDef resize_grip_def[4] = { - { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower right - { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower left - { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper left - { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper right + { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower-right + { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower-left + { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper-left (Unused) + { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper-right (Unused) }; static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) @@ -4881,9 +5081,20 @@ static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_ return ImRect(); } +// 0..3: corners (Lower-right, Lower-left, Unused, Unused) +// 4..7: borders (Top, Right, Bottom, Left) +ImGuiID ImGui::GetWindowResizeID(ImGuiWindow* window, int n) +{ + IM_ASSERT(n >= 0 && n <= 7); + ImGuiID id = window->ID; + id = ImHashStr("#RESIZE", 0, id); + id = ImHashData(&n, sizeof(int), id); + return id; +} + // Handle resize for: Resize Grips, Borders, Gamepad // Return true when using auto-fit (double click on resize grip) -static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]) +static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]) { ImGuiContext& g = *GImGui; ImGuiWindowFlags flags = window->Flags; @@ -4895,8 +5106,8 @@ static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_au bool ret_auto_fit = false; const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0; - const float grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); - const float grip_hover_inner_size = (float)(int)(grip_draw_size * 0.75f); + const float grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); + const float grip_hover_inner_size = IM_FLOOR(grip_draw_size * 0.75f); const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS : 0.0f; ImVec2 pos_target(FLT_MAX, FLT_MAX); @@ -4918,7 +5129,7 @@ static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_au if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x); if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y); bool hovered, held; - ButtonBehavior(resize_rect, window->GetID((void*)(intptr_t)resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); + ButtonBehavior(resize_rect, window->GetID(resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); if (hovered || held) g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; @@ -4944,7 +5155,7 @@ static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_au { bool hovered, held; ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); - ButtonBehavior(border_rect, window->GetID((void*)(intptr_t)(border_n + 4)), &hovered, &held, ImGuiButtonFlags_FlattenChildren); + ButtonBehavior(border_rect, window->GetID(border_n + 4), &hovered, &held, ImGuiButtonFlags_FlattenChildren); //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) { @@ -4965,6 +5176,10 @@ static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_au } PopID(); + // Restore nav layer + window->DC.NavLayerCurrent = ImGuiNavLayer_Main; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + // Navigation resize (keyboard/gamepad) if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) { @@ -4997,10 +5212,6 @@ static bool ImGui::UpdateManualResize(ImGuiWindow* window, const ImVec2& size_au MarkIniSettingsDirty(window); } - // Resize nav layer - window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); - window->Size = window->SizeFull; return ret_auto_fit; } @@ -5049,12 +5260,17 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) } } +// Draw background and borders +// Draw and handle scrollbars void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size) { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; ImGuiWindowFlags flags = window->Flags; + // Ensure that ScrollBar doesn't read last frame's SkipItems + window->SkipItems = false; + // Draw window + handle manual resize // As we highlight the title bar when want_focus is set, multiple reappearing windows will have have their title bar highlighted on their reappearing frame. const float window_rounding = window->WindowRounding; @@ -5074,10 +5290,14 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar if (!(flags & ImGuiWindowFlags_NoBackground)) { ImU32 bg_col = GetColorU32(GetWindowBgColorIdxFromFlags(flags)); + bool override_alpha = false; float alpha = 1.0f; if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasBgAlpha) + { alpha = g.NextWindowData.BgAlphaVal; - if (alpha != 1.0f) + override_alpha = true; + } + if (override_alpha) bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT); window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Bot); } @@ -5204,7 +5424,7 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl if (flags & ImGuiWindowFlags_UnsavedDocument) { ImVec2 marker_pos = ImVec2(ImMax(layout_r.Min.x, layout_r.Min.x + (layout_r.GetWidth() - text_size.x) * style.WindowTitleAlign.x) + text_size.x, layout_r.Min.y) + ImVec2(2 - marker_size_x, 0.0f); - ImVec2 off = ImVec2(0.0f, (float)(int)(-g.FontSize * 0.25f)); + ImVec2 off = ImVec2(0.0f, IM_FLOOR(-g.FontSize * 0.25f)); RenderTextClipped(marker_pos + off, layout_r.Max + off, UNSAVED_DOCUMENT_MARKER, NULL, NULL, ImVec2(0, style.WindowTitleAlign.y), &clip_r); } } @@ -5236,7 +5456,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; IM_ASSERT(name != NULL && name[0] != '\0'); // Window name required - IM_ASSERT(g.FrameScopeActive); // Forgot to call ImGui::NewFrame() + IM_ASSERT(g.WithinFrameScope); // Forgot to call ImGui::NewFrame() IM_ASSERT(g.FrameCountEnded != g.FrameCount); // Called ImGui::Render() or ImGui::EndFrame() and haven't called ImGui::NewFrame() again yet // Find or create @@ -5257,6 +5477,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const int current_frame = g.FrameCount; const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame); + window->IsFallbackWindow = (g.CurrentWindowStack.Size == 0 && g.WithinFrameScopeWithImplicitWindow); // Update the Appearing flag bool window_just_activated_by_user = (window->LastFrameActive < current_frame - 1); // Not using !WasActive because the implicit "Debug" window would always toggle off->on @@ -5298,7 +5519,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() g.CurrentWindowStack.push_back(window); g.CurrentWindow = NULL; - CheckStacksSize(window, true); + ErrorCheckBeginEndCompareStacksSize(window, true); if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; @@ -5310,6 +5531,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window_just_appearing_after_hidden_for_resize && !(flags & ImGuiWindowFlags_ChildWindow)) window->NavLastIds[0] = 0; + // Update ->RootWindow and others pointers (before any possible call to FocusWindow) + if (first_begin_of_the_frame) + UpdateWindowParentAndRootLinks(window, flags, parent_window); + // Process SetNextWindow***() calls bool window_pos_set_by_api = false; bool window_size_x_set_by_api = false, window_size_y_set_by_api = false; @@ -5351,8 +5576,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { // Initialize const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) - UpdateWindowParentAndRootLinks(window, flags, parent_window); - window->Active = true; window->HasCloseButton = (p_open != NULL); window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX); @@ -5402,6 +5625,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } } + // SELECT VIEWPORT // FIXME-VIEWPORT: In the docking/viewport branch, this is the point where we select the current viewport (which may affect the style) SetCurrentWindow(window); @@ -5414,8 +5638,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->WindowPadding = style.WindowPadding; if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f) window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f); - window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); - window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; // Collapse window by double-clicking on title bar // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing @@ -5514,14 +5736,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->Pos = FindBestWindowPosForPopup(window); // Clamp position/size so window stays visible within its viewport or monitor - // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. ImRect viewport_rect(GetViewportRect()); if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) { - if (g.IO.DisplaySize.x > 0.0f && g.IO.DisplaySize.y > 0.0f) // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. + ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); + if (viewport_rect.GetWidth() > 0 && viewport_rect.GetHeight() > 0.0f) { - ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); ClampWindowRect(window, viewport_rect, clamp_padding); } } @@ -5542,11 +5763,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Handle manual resize: Resize Grips, Borders, Gamepad int border_held = -1; - ImU32 resize_grip_col[4] = { 0 }; - const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // 4 - const float resize_grip_draw_size = (float)(int)ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f); + ImU32 resize_grip_col[4] = {}; + const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. + const float resize_grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); if (!window->Collapsed) - if (UpdateManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0])) + if (UpdateWindowManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0])) use_current_size_for_scrollbar_x = use_current_size_for_scrollbar_y = true; window->ResizeBorderHeld = (signed char)border_held; @@ -5612,9 +5833,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Default item width. Make it proportional to window size if window manually resizes if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize)) - window->ItemWidthDefault = (float)(int)(window->Size.x * 0.65f); + window->ItemWidthDefault = ImFloor(window->Size.x * 0.65f); else - window->ItemWidthDefault = (float)(int)(g.FontSize * 16.0f); + window->ItemWidthDefault = ImFloor(g.FontSize * 16.0f); // SCROLLING @@ -5658,20 +5879,22 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We disable this when the parent window has zero vertices, which is a common pattern leading to laying out multiple overlapping child. // We also disabled this when we have dimming overlay behind this specific one child. // FIXME: More code may rely on explicit sorting of overlapping child window and would need to disable this somehow. Please get in contact if you are affected. - bool render_decorations_in_parent = false; - if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) - if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_window->DrawList->VtxBuffer.Size > 0) - render_decorations_in_parent = true; - if (render_decorations_in_parent) - window->DrawList = parent_window->DrawList; - - // Handle title bar, scrollbar, resize grips and resize borders - const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow; - const bool title_bar_is_highlight = want_focus || (window_to_highlight && window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight); - RenderWindowDecorations(window, title_bar_rect, title_bar_is_highlight, resize_grip_count, resize_grip_col, resize_grip_draw_size); - - if (render_decorations_in_parent) - window->DrawList = &window->DrawListInst; + { + bool render_decorations_in_parent = false; + if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) + if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_window->DrawList->VtxBuffer.Size > 0) + render_decorations_in_parent = true; + if (render_decorations_in_parent) + window->DrawList = parent_window->DrawList; + + // Handle title bar, scrollbar, resize grips and resize borders + const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow; + const bool title_bar_is_highlight = want_focus || (window_to_highlight && window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight); + RenderWindowDecorations(window, title_bar_rect, title_bar_is_highlight, resize_grip_count, resize_grip_col, resize_grip_draw_size); + + if (render_decorations_in_parent) + window->DrawList = &window->DrawListInst; + } // Draw navigation selection/windowing rectangle border if (g.NavWindowingTargetAnim == window) @@ -5703,14 +5926,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->WorkRect.Max.x = window->WorkRect.Min.x + work_rect_size_x; window->WorkRect.Max.y = window->WorkRect.Min.y + work_rect_size_y; - // [LEGACY] Contents Region - // FIXME-OBSOLETE: window->ContentsRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. + // [LEGACY] Content Region + // FIXME-OBSOLETE: window->ContentRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. // Used by: // - Mouse wheel scrolling + many other things - window->ContentsRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x; - window->ContentsRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + decoration_up_height; - window->ContentsRegionRect.Max.x = window->ContentsRegionRect.Min.x + (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : (window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); - window->ContentsRegionRect.Max.y = window->ContentsRegionRect.Min.y + (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : (window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); + window->ContentRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x; + window->ContentRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + decoration_up_height; + window->ContentRegionRect.Max.x = window->ContentRegionRect.Min.x + (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : (window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); + window->ContentRegionRect.Max.y = window->ContentRegionRect.Min.y + (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : (window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); // Setup drawing context // (NB: That term "drawing context / DC" lost its meaning a long time ago. Initially was meant to hold transient data only. Nowadays difference between window-> and window->DC-> is dubious.) @@ -5723,33 +5946,37 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CursorMaxPos = window->DC.CursorStartPos; window->DC.CurrLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; - window->DC.NavHideHighlightOneFrame = false; - window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); + + window->DC.NavLayerCurrent = ImGuiNavLayer_Main; + window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; window->DC.NavLayerActiveMaskNext = 0x00; + window->DC.NavFocusScopeIdCurrent = parent_window ? parent_window->DC.NavFocusScopeIdCurrent : 0; + window->DC.NavHideHighlightOneFrame = false; + window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); + window->DC.MenuBarAppending = false; + window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); + window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; + window->DC.MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); + window->DC.TreeDepth = 0; + window->DC.TreeJumpToParentOnPopMask = 0x00; window->DC.ChildWindows.resize(0); + window->DC.StateStorage = &window->StateStorage; + window->DC.CurrentColumns = NULL; window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical; - window->DC.FocusCounterAll = window->DC.FocusCounterTab = -1; - window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_; + window->DC.FocusCounterRegular = window->DC.FocusCounterTabStop = -1; + window->DC.ItemWidth = window->ItemWidthDefault; window->DC.TextWrapPos = -1.0f; // disabled window->DC.ItemFlagsStack.resize(0); window->DC.ItemWidthStack.resize(0); window->DC.TextWrapPosStack.resize(0); - window->DC.CurrentColumns = NULL; - window->DC.TreeDepth = 0; - window->DC.TreeMayJumpToParentOnPopMask = 0x00; - window->DC.StateStorage = &window->StateStorage; window->DC.GroupStack.resize(0); - window->MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); - - if ((flags & ImGuiWindowFlags_ChildWindow) && (window->DC.ItemFlags != parent_window->DC.ItemFlags)) - { - window->DC.ItemFlags = parent_window->DC.ItemFlags; + window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_; + if (parent_window) window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); - } if (window->AutoFitFramesX > 0) window->AutoFitFramesX--; @@ -5834,35 +6061,24 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) return !skip_items; } -// Old Begin() API with 5 parameters, avoid calling this version directly! Use SetNextWindowSize()/SetNextWindowBgAlpha() + Begin() instead. -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_first_use, float bg_alpha_override, ImGuiWindowFlags flags) -{ - // Old API feature: we could pass the initial window size as a parameter. This was misleading because it only had an effect if the window didn't have data in the .ini file. - if (size_first_use.x != 0.0f || size_first_use.y != 0.0f) - SetNextWindowSize(size_first_use, ImGuiCond_FirstUseEver); - - // Old API feature: override the window background alpha with a parameter. - if (bg_alpha_override >= 0.0f) - SetNextWindowBgAlpha(bg_alpha_override); - - return Begin(name, p_open, flags); -} -#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS - void ImGui::End() { ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; - if (g.CurrentWindowStack.Size <= 1 && g.FrameScopePushedImplicitWindow) + // Error checking: verify that user hasn't called End() too many times! + if (g.CurrentWindowStack.Size <= 1 && g.WithinFrameScopeWithImplicitWindow) { - IM_ASSERT(g.CurrentWindowStack.Size > 1 && "Calling End() too many times!"); - return; // FIXME-ERRORHANDLING + IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size > 1, "Calling End() too many times!"); + return; } IM_ASSERT(g.CurrentWindowStack.Size > 0); - ImGuiWindow* window = g.CurrentWindow; + // Error checking: verify that user doesn't directly call End() on a child window. + if (window->Flags & ImGuiWindowFlags_ChildWindow) + IM_ASSERT_USER_ERROR(g.WithinEndChild, "Must call EndChild() and not End()!"); + // Close anything that is open if (window->DC.CurrentColumns) EndColumns(); PopClipRect(); // Inner window clip rectangle @@ -5875,7 +6091,7 @@ void ImGui::End() g.CurrentWindowStack.pop_back(); if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - CheckStacksSize(window, false); + ErrorCheckBeginEndCompareStacksSize(window, false); SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back()); } @@ -5934,6 +6150,7 @@ void ImGui::FocusWindow(ImGuiWindow* window) g.NavMousePosDirty = true; g.NavInitRequest = false; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId + g.NavFocusScopeId = 0; g.NavIdIsAlive = false; g.NavLayer = ImGuiNavLayer_Main; //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); @@ -5947,18 +6164,19 @@ void ImGui::FocusWindow(ImGuiWindow* window) return; // Move the root window to the top of the pile - if (window->RootWindow) - window = window->RootWindow; + IM_ASSERT(window->RootWindow != NULL); + ImGuiWindow* focus_front_window = window->RootWindow; // NB: In docking branch this is window->RootWindowDockStop + ImGuiWindow* display_front_window = window->RootWindow; // Steal focus on active widgets - if (window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement should be unnecessary. Need further testing before removing it.. - if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != window) + if (focus_front_window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement may be unnecessary? Need further testing before removing it.. + if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != focus_front_window) ClearActiveID(); // Bring to front - BringWindowToFocusFront(window); - if (!(window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) - BringWindowToDisplayFront(window); + BringWindowToFocusFront(focus_front_window); + if (((window->Flags | display_front_window->Flags) & ImGuiWindowFlags_NoBringToFrontOnFocus) == 0) + BringWindowToDisplayFront(display_front_window); } void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window) @@ -5987,88 +6205,6 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind FocusWindow(NULL); } -void ImGui::SetNextItemWidth(float item_width) -{ - ImGuiContext& g = *GImGui; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasWidth; - g.NextItemData.Width = item_width; -} - -void ImGui::PushItemWidth(float item_width) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); - window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); - g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; -} - -void ImGui::PushMultiItemsWidths(int components, float w_full) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - const ImGuiStyle& style = g.Style; - const float w_item_one = ImMax(1.0f, (float)(int)((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); - const float w_item_last = ImMax(1.0f, (float)(int)(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); - window->DC.ItemWidthStack.push_back(w_item_last); - for (int i = 0; i < components-1; i++) - window->DC.ItemWidthStack.push_back(w_item_one); - window->DC.ItemWidth = window->DC.ItemWidthStack.back(); - g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; -} - -void ImGui::PopItemWidth() -{ - ImGuiWindow* window = GetCurrentWindow(); - window->DC.ItemWidthStack.pop_back(); - window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back(); -} - -// Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(). -// The SetNextItemWidth() data is generally cleared/consumed by ItemAdd() or NextItemData.ClearFlags() -float ImGui::CalcItemWidth() -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - float w; - if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) - w = g.NextItemData.Width; - else - w = window->DC.ItemWidth; - if (w < 0.0f) - { - float region_max_x = GetContentRegionMaxAbs().x; - w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); - } - w = (float)(int)w; - return w; -} - -// [Internal] Calculate full item size given user provided 'size' parameter and default width/height. Default width is often == CalcItemWidth(). -// Those two functions CalcItemWidth vs CalcItemSize are awkwardly named because they are not fully symmetrical. -// Note that only CalcItemWidth() is publicly exposed. -// The 4.0f here may be changed to match CalcItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) -ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - - ImVec2 region_max; - if (size.x < 0.0f || size.y < 0.0f) - region_max = GetContentRegionMaxAbs(); - - if (size.x == 0.0f) - size.x = default_w; - else if (size.x < 0.0f) - size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x); - - if (size.y == 0.0f) - size.y = default_h; - else if (size.y < 0.0f) - size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y); - - return size; -} - void ImGui::SetCurrentFont(ImFont* font) { ImGuiContext& g = *GImGui; @@ -6154,279 +6290,102 @@ void ImGui::PopTextWrapPos() window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back(); } -// FIXME: This may incur a round-trip (if the end user got their data from a float4) but eventually we aim to store the in-flight colors as ImU32 -void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) +bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) { - ImGuiContext& g = *GImGui; - ImGuiColorMod backup; - backup.Col = idx; - backup.BackupValue = g.Style.Colors[idx]; - g.ColorModifiers.push_back(backup); - g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); + if (window->RootWindow == potential_parent) + return true; + while (window != NULL) + { + if (window == potential_parent) + return true; + window = window->ParentWindow; + } + return false; } -void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) +bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { + IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function ImGuiContext& g = *GImGui; - ImGuiColorMod backup; - backup.Col = idx; - backup.BackupValue = g.Style.Colors[idx]; - g.ColorModifiers.push_back(backup); - g.Style.Colors[idx] = col; + + if (flags & ImGuiHoveredFlags_AnyWindow) + { + if (g.HoveredWindow == NULL) + return false; + } + else + { + switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) + { + case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: + if (g.HoveredRootWindow != g.CurrentWindow->RootWindow) + return false; + break; + case ImGuiHoveredFlags_RootWindow: + if (g.HoveredWindow != g.CurrentWindow->RootWindow) + return false; + break; + case ImGuiHoveredFlags_ChildWindows: + if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow)) + return false; + break; + default: + if (g.HoveredWindow != g.CurrentWindow) + return false; + break; + } + } + + if (!IsWindowContentHoverable(g.HoveredWindow, flags)) + return false; + if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) + if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId) + return false; + return true; } -void ImGui::PopStyleColor(int count) +bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) { ImGuiContext& g = *GImGui; - while (count > 0) + + if (flags & ImGuiFocusedFlags_AnyWindow) + return g.NavWindow != NULL; + + IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End() + switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows)) { - ImGuiColorMod& backup = g.ColorModifiers.back(); - g.Style.Colors[backup.Col] = backup.BackupValue; - g.ColorModifiers.pop_back(); - count--; + case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows: + return g.NavWindow && g.NavWindow->RootWindow == g.CurrentWindow->RootWindow; + case ImGuiFocusedFlags_RootWindow: + return g.NavWindow == g.CurrentWindow->RootWindow; + case ImGuiFocusedFlags_ChildWindows: + return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow); + default: + return g.NavWindow == g.CurrentWindow; } } -struct ImGuiStyleVarInfo +// Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) +// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly. +// If you want a window to never be focused, you may use the e.g. NoInputs flag. +bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) { - ImGuiDataType Type; - ImU32 Count; - ImU32 Offset; - void* GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); } -}; + return window->Active && window == window->RootWindow && !(window->Flags & ImGuiWindowFlags_NoNavFocus); +} -static const ImGuiStyleVarInfo GStyleVarInfo[] = +float ImGui::GetWindowWidth() { - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign -}; + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Size.x; +} -static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx) +float ImGui::GetWindowHeight() { - IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT); - IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); - return &GStyleVarInfo[idx]; + ImGuiWindow* window = GImGui->CurrentWindow; + return window->Size.y; } -void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) -{ - const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); - if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) - { - ImGuiContext& g = *GImGui; - float* pvar = (float*)var_info->GetVarPtr(&g.Style); - g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); - *pvar = val; - return; - } - IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!"); -} - -void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) -{ - const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); - if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) - { - ImGuiContext& g = *GImGui; - ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); - g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); - *pvar = val; - return; - } - IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!"); -} - -void ImGui::PopStyleVar(int count) -{ - ImGuiContext& g = *GImGui; - while (count > 0) - { - // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. - ImGuiStyleMod& backup = g.StyleModifiers.back(); - const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); - void* data = info->GetVarPtr(&g.Style); - if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } - else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } - g.StyleModifiers.pop_back(); - count--; - } -} - -const char* ImGui::GetStyleColorName(ImGuiCol idx) -{ - // Create switch-case from enum with regexp: ImGuiCol_{.*}, --> case ImGuiCol_\1: return "\1"; - switch (idx) - { - case ImGuiCol_Text: return "Text"; - case ImGuiCol_TextDisabled: return "TextDisabled"; - case ImGuiCol_WindowBg: return "WindowBg"; - case ImGuiCol_ChildBg: return "ChildBg"; - case ImGuiCol_PopupBg: return "PopupBg"; - case ImGuiCol_Border: return "Border"; - case ImGuiCol_BorderShadow: return "BorderShadow"; - case ImGuiCol_FrameBg: return "FrameBg"; - case ImGuiCol_FrameBgHovered: return "FrameBgHovered"; - case ImGuiCol_FrameBgActive: return "FrameBgActive"; - case ImGuiCol_TitleBg: return "TitleBg"; - case ImGuiCol_TitleBgActive: return "TitleBgActive"; - case ImGuiCol_TitleBgCollapsed: return "TitleBgCollapsed"; - case ImGuiCol_MenuBarBg: return "MenuBarBg"; - case ImGuiCol_ScrollbarBg: return "ScrollbarBg"; - case ImGuiCol_ScrollbarGrab: return "ScrollbarGrab"; - case ImGuiCol_ScrollbarGrabHovered: return "ScrollbarGrabHovered"; - case ImGuiCol_ScrollbarGrabActive: return "ScrollbarGrabActive"; - case ImGuiCol_CheckMark: return "CheckMark"; - case ImGuiCol_SliderGrab: return "SliderGrab"; - case ImGuiCol_SliderGrabActive: return "SliderGrabActive"; - case ImGuiCol_Button: return "Button"; - case ImGuiCol_ButtonHovered: return "ButtonHovered"; - case ImGuiCol_ButtonActive: return "ButtonActive"; - case ImGuiCol_Header: return "Header"; - case ImGuiCol_HeaderHovered: return "HeaderHovered"; - case ImGuiCol_HeaderActive: return "HeaderActive"; - case ImGuiCol_Separator: return "Separator"; - case ImGuiCol_SeparatorHovered: return "SeparatorHovered"; - case ImGuiCol_SeparatorActive: return "SeparatorActive"; - case ImGuiCol_ResizeGrip: return "ResizeGrip"; - case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; - case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; - case ImGuiCol_Tab: return "Tab"; - case ImGuiCol_TabHovered: return "TabHovered"; - case ImGuiCol_TabActive: return "TabActive"; - case ImGuiCol_TabUnfocused: return "TabUnfocused"; - case ImGuiCol_TabUnfocusedActive: return "TabUnfocusedActive"; - case ImGuiCol_PlotLines: return "PlotLines"; - case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; - case ImGuiCol_PlotHistogram: return "PlotHistogram"; - case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered"; - case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; - case ImGuiCol_DragDropTarget: return "DragDropTarget"; - case ImGuiCol_NavHighlight: return "NavHighlight"; - case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight"; - case ImGuiCol_NavWindowingDimBg: return "NavWindowingDimBg"; - case ImGuiCol_ModalWindowDimBg: return "ModalWindowDimBg"; - } - IM_ASSERT(0); - return "Unknown"; -} - -bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) -{ - if (window->RootWindow == potential_parent) - return true; - while (window != NULL) - { - if (window == potential_parent) - return true; - window = window->ParentWindow; - } - return false; -} - -bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) -{ - IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function - ImGuiContext& g = *GImGui; - - if (flags & ImGuiHoveredFlags_AnyWindow) - { - if (g.HoveredWindow == NULL) - return false; - } - else - { - switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) - { - case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: - if (g.HoveredRootWindow != g.CurrentWindow->RootWindow) - return false; - break; - case ImGuiHoveredFlags_RootWindow: - if (g.HoveredWindow != g.CurrentWindow->RootWindow) - return false; - break; - case ImGuiHoveredFlags_ChildWindows: - if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow)) - return false; - break; - default: - if (g.HoveredWindow != g.CurrentWindow) - return false; - break; - } - } - - if (!IsWindowContentHoverable(g.HoveredWindow, flags)) - return false; - if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) - if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId) - return false; - return true; -} - -bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) -{ - ImGuiContext& g = *GImGui; - - if (flags & ImGuiFocusedFlags_AnyWindow) - return g.NavWindow != NULL; - - IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End() - switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows)) - { - case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows: - return g.NavWindow && g.NavWindow->RootWindow == g.CurrentWindow->RootWindow; - case ImGuiFocusedFlags_RootWindow: - return g.NavWindow == g.CurrentWindow->RootWindow; - case ImGuiFocusedFlags_ChildWindows: - return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow); - default: - return g.NavWindow == g.CurrentWindow; - } -} - -// Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) -// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly. -// If you want a window to never be focused, you may use the e.g. NoInputs flag. -bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) -{ - return window->Active && window == window->RootWindow && !(window->Flags & ImGuiWindowFlags_NoNavFocus); -} - -float ImGui::GetWindowWidth() -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->Size.x; -} - -float ImGui::GetWindowHeight() -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->Size.y; -} - -ImVec2 ImGui::GetWindowPos() +ImVec2 ImGui::GetWindowPos() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -6483,7 +6442,7 @@ void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond con if (size.x > 0.0f) { window->AutoFitFramesX = 0; - window->SizeFull.x = ImFloor(size.x); + window->SizeFull.x = IM_FLOOR(size.x); } else { @@ -6493,7 +6452,7 @@ void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond con if (size.y > 0.0f) { window->AutoFitFramesY = 0; - window->SizeFull.y = ImFloor(size.y); + window->SizeFull.y = IM_FLOOR(size.y); } else { @@ -6624,104 +6583,397 @@ void ImGui::SetNextWindowBgAlpha(float alpha) g.NextWindowData.BgAlphaVal = alpha; } -// FIXME: This is in window space (not screen space!). We should try to obsolete all those functions. -ImVec2 ImGui::GetContentRegionMax() +ImDrawList* ImGui::GetWindowDrawList() { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImVec2 mx = window->ContentsRegionRect.Max - window->Pos; - if (window->DC.CurrentColumns) - mx.x = window->WorkRect.Max.x - window->Pos.x; - return mx; + ImGuiWindow* window = GetCurrentWindow(); + return window->DrawList; } -// [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. -ImVec2 ImGui::GetContentRegionMaxAbs() +ImFont* ImGui::GetFont() { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImVec2 mx = window->ContentsRegionRect.Max; - if (window->DC.CurrentColumns) - mx.x = window->WorkRect.Max.x; - return mx; + return GImGui->Font; } -ImVec2 ImGui::GetContentRegionAvail() +float ImGui::GetFontSize() { - ImGuiWindow* window = GImGui->CurrentWindow; - return GetContentRegionMaxAbs() - window->DC.CursorPos; + return GImGui->FontSize; } -// In window space (not screen space!) -ImVec2 ImGui::GetWindowContentRegionMin() +ImVec2 ImGui::GetFontTexUvWhitePixel() { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ContentsRegionRect.Min - window->Pos; + return GImGui->DrawListSharedData.TexUvWhitePixel; } -ImVec2 ImGui::GetWindowContentRegionMax() +void ImGui::SetWindowFontScale(float scale) { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ContentsRegionRect.Max - window->Pos; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->FontWindowScale = scale; + g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } -float ImGui::GetWindowContentRegionWidth() +void ImGui::ActivateItem(ImGuiID id) { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ContentsRegionRect.GetWidth(); + ImGuiContext& g = *GImGui; + g.NavNextActivateId = id; } -float ImGui::GetTextLineHeight() +void ImGui::PushFocusScope(ImGuiID id) { ImGuiContext& g = *GImGui; - return g.FontSize; + ImGuiWindow* window = g.CurrentWindow; + window->IDStack.push_back(window->DC.NavFocusScopeIdCurrent); + window->DC.NavFocusScopeIdCurrent = id; } -float ImGui::GetTextLineHeightWithSpacing() +void ImGui::PopFocusScope() { ImGuiContext& g = *GImGui; - return g.FontSize + g.Style.ItemSpacing.y; + ImGuiWindow* window = g.CurrentWindow; + window->DC.NavFocusScopeIdCurrent = window->IDStack.back(); + window->IDStack.pop_back(); } -float ImGui::GetFrameHeight() +void ImGui::SetKeyboardFocusHere(int offset) { + IM_ASSERT(offset >= -1); // -1 is allowed but not below ImGuiContext& g = *GImGui; - return g.FontSize + g.Style.FramePadding.y * 2.0f; + ImGuiWindow* window = g.CurrentWindow; + g.FocusRequestNextWindow = window; + g.FocusRequestNextCounterRegular = window->DC.FocusCounterRegular + 1 + offset; + g.FocusRequestNextCounterTabStop = INT_MAX; } -float ImGui::GetFrameHeightWithSpacing() +void ImGui::SetItemDefaultFocus() { ImGuiContext& g = *GImGui; - return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y; + ImGuiWindow* window = g.CurrentWindow; + if (!window->Appearing) + return; + if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent) + { + g.NavInitRequest = false; + g.NavInitResultId = g.NavWindow->DC.LastItemId; + g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos); + NavUpdateAnyRequestFlag(); + if (!IsItemVisible()) + SetScrollHereY(); + } } -ImDrawList* ImGui::GetWindowDrawList() +void ImGui::SetStateStorage(ImGuiStorage* tree) { - ImGuiWindow* window = GetCurrentWindow(); - return window->DrawList; + ImGuiWindow* window = GImGui->CurrentWindow; + window->DC.StateStorage = tree ? tree : &window->StateStorage; } -ImFont* ImGui::GetFont() +ImGuiStorage* ImGui::GetStateStorage() { - return GImGui->Font; + ImGuiWindow* window = GImGui->CurrentWindow; + return window->DC.StateStorage; } -float ImGui::GetFontSize() +void ImGui::PushID(const char* str_id) { - return GImGui->FontSize; + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(str_id)); } -ImVec2 ImGui::GetFontTexUvWhitePixel() +void ImGui::PushID(const char* str_id_begin, const char* str_id_end) { - return GImGui->DrawListSharedData.TexUvWhitePixel; + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end)); } -void ImGui::SetWindowFontScale(float scale) +void ImGui::PushID(const void* ptr_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); +} + +void ImGui::PushID(int int_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(window->GetIDNoKeepAlive(int_id)); +} + +// Push a given id value ignoring the ID stack as a seed. +void ImGui::PushOverrideID(ImGuiID id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.push_back(id); +} + +void ImGui::PopID() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->IDStack.pop_back(); +} + +ImGuiID ImGui::GetID(const char* str_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id); +} + +ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id_begin, str_id_end); +} + +ImGuiID ImGui::GetID(const void* ptr_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(ptr_id); +} + +bool ImGui::IsRectVisible(const ImVec2& size) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); +} + +bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); +} + + +//----------------------------------------------------------------------------- +// [SECTION] ERROR CHECKING +//----------------------------------------------------------------------------- + +// Helper function to verify ABI compatibility between caller code and compiled version of Dear ImGui. +// Verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit +// If the user has inconsistent compilation settings, imgui configuration #define, packing pragma, etc. your user code +// may see different structures than what imgui.cpp sees, which is problematic. +// We usually require settings to be in imconfig.h to make sure that they are accessible to all compilation units involved with Dear ImGui. +bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx) +{ + bool error = false; + if (strcmp(version, IMGUI_VERSION) != 0) { error = true; IM_ASSERT(strcmp(version, IMGUI_VERSION) == 0 && "Mismatched version string!"); } + if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } + if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); } + if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } + if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } + if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } + if (sz_idx != sizeof(ImDrawIdx)) { error = true; IM_ASSERT(sz_idx == sizeof(ImDrawIdx) && "Mismatched struct layout!"); } + return !error; +} + +static void ImGui::ErrorCheckEndFrame() +{ + // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you + // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). + ImGuiContext& g = *GImGui; + if (g.CurrentWindowStack.Size != 1) + { + if (g.CurrentWindowStack.Size > 1) + { + IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); + while (g.CurrentWindowStack.Size > 1) + End(); + } + else + { + IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); + } + } +} + +// Save and compare stack sizes on Begin()/End() to detect usage errors +// Begin() calls this with write=true +// End() calls this with write=false +static void ImGui::ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool write) +{ + ImGuiContext& g = *GImGui; + short* p = &window->DC.StackSizesBackup[0]; + + // Window stacks + // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) + { int n = window->IDStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "PushID/PopID or TreeNode/TreePop Mismatch!"); p++; } // Too few or too many PopID()/TreePop() + { int n = window->DC.GroupStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "BeginGroup/EndGroup Mismatch!"); p++; } // Too few or too many EndGroup() + + // Global stacks + // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. + { int n = g.BeginPopupStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "BeginMenu/EndMenu or BeginPopup/EndPopup Mismatch!"); p++; }// Too few or too many EndMenu()/EndPopup() + { int n = g.ColorModifiers.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushStyleColor/PopStyleColor Mismatch!"); p++; } // Too few or too many PopStyleColor() + { int n = g.StyleModifiers.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushStyleVar/PopStyleVar Mismatch!"); p++; } // Too few or too many PopStyleVar() + { int n = g.FontStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushFont/PopFont Mismatch!"); p++; } // Too few or too many PopFont() + IM_ASSERT(p == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup)); +} + + +//----------------------------------------------------------------------------- +// [SECTION] LAYOUT +//----------------------------------------------------------------------------- +// - ItemSize() +// - ItemAdd() +// - SameLine() +// - GetCursorScreenPos() +// - SetCursorScreenPos() +// - GetCursorPos(), GetCursorPosX(), GetCursorPosY() +// - SetCursorPos(), SetCursorPosX(), SetCursorPosY() +// - GetCursorStartPos() +// - Indent() +// - Unindent() +// - SetNextItemWidth() +// - PushItemWidth() +// - PushMultiItemsWidths() +// - PopItemWidth() +// - CalcItemWidth() +// - CalcItemSize() +// - GetTextLineHeight() +// - GetTextLineHeightWithSpacing() +// - GetFrameHeight() +// - GetFrameHeightWithSpacing() +// - GetContentRegionMax() +// - GetContentRegionMaxAbs() [Internal] +// - GetContentRegionAvail(), +// - GetWindowContentRegionMin(), GetWindowContentRegionMax() +// - GetWindowContentRegionWidth() +// - BeginGroup() +// - EndGroup() +// Also see in imgui_widgets: tab bars, columns. +//----------------------------------------------------------------------------- + +// Advance cursor given item size for layout. +void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + // We increase the height in this function to accommodate for baseline offset. + // In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor, + // but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect. + const float offset_to_match_baseline_y = (text_baseline_y >= 0) ? ImMax(0.0f, window->DC.CurrLineTextBaseOffset - text_baseline_y) : 0.0f; + const float line_height = ImMax(window->DC.CurrLineSize.y, size.y + offset_to_match_baseline_y); + + // Always align ourselves on pixel boundaries + //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] + window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; + window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y; + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line + window->DC.CursorPos.y = IM_FLOOR(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y); // Next line + window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); + //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] + + window->DC.PrevLineSize.y = line_height; + window->DC.CurrLineSize.y = 0.0f; + window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); + window->DC.CurrLineTextBaseOffset = 0.0f; + + // Horizontal layout mode + if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) + SameLine(); +} + +void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) +{ + ItemSize(bb.GetSize(), text_baseline_y); +} + +// Declare item bounding box for clipping and interaction. +// Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface +// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). +bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) { ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (id != 0) + { + // Navigation processing runs prior to clipping early-out + // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget + // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests + // unfortunately, but it is still limited to one window. It may not scale very well for windows with ten of + // thousands of item, but at least NavMoveRequest is only set on user interaction, aka maximum once a frame. + // We could early out with "if (is_clipped && !g.NavInitRequest) return false;" but when we wouldn't be able + // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). + // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. + // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. + window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask; + if (g.NavId == id || g.NavAnyRequest) + if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) + if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) + NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id); + + // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() +#ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX + if (id == g.DebugItemPickerBreakId) + { + IM_DEBUG_BREAK(); + g.DebugItemPickerBreakId = 0; + } +#endif + } + + window->DC.LastItemId = id; + window->DC.LastItemRect = bb; + window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; + g.NextItemData.Flags = ImGuiNextItemDataFlags_None; + +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (id != 0) + IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id); +#endif + + // Clipping test + const bool is_clipped = IsClippedEx(bb, id, false); + if (is_clipped) + return false; + //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] + + // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) + if (IsMouseHoveringRect(bb.Min, bb.Max)) + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect; + return true; +} + +// Gets back to previous line and continue with horizontal layout +// offset_from_start_x == 0 : follow right after previous item +// offset_from_start_x != 0 : align to specified x position (relative to window/group left) +// spacing_w < 0 : use default spacing if pos_x == 0, no spacing if pos_x != 0 +// spacing_w >= 0 : enforce spacing amount +void ImGui::SameLine(float offset_from_start_x, float spacing_w) +{ ImGuiWindow* window = GetCurrentWindow(); - window->FontWindowScale = scale; - g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); + if (window->SkipItems) + return; + + ImGuiContext& g = *GImGui; + if (offset_from_start_x != 0.0f) + { + if (spacing_w < 0.0f) spacing_w = 0.0f; + window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x; + window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; + } + else + { + if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x; + window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w; + window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; + } + window->DC.CurrLineSize = window->DC.PrevLineSize; + window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; +} + +ImVec2 ImGui::GetCursorScreenPos() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CursorPos; +} + +void ImGui::SetCursorScreenPos(const ImVec2& pos) +{ + ImGuiWindow* window = GetCurrentWindow(); + window->DC.CursorPos = pos; + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); } // User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient. @@ -6771,129 +7023,176 @@ ImVec2 ImGui::GetCursorStartPos() return window->DC.CursorStartPos - window->Pos; } -ImVec2 ImGui::GetCursorScreenPos() +void ImGui::Indent(float indent_w) { - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.CursorPos; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->DC.Indent.x += (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; + window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; } -void ImGui::SetCursorScreenPos(const ImVec2& pos) +void ImGui::Unindent(float indent_w) { + ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - window->DC.CursorPos = pos; - window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); + window->DC.Indent.x -= (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; + window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; } -void ImGui::ActivateItem(ImGuiID id) +// Affect large frame+labels widgets only. +void ImGui::SetNextItemWidth(float item_width) { ImGuiContext& g = *GImGui; - g.NavNextActivateId = id; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasWidth; + g.NextItemData.Width = item_width; } -void ImGui::SetKeyboardFocusHere(int offset) +void ImGui::PushItemWidth(float item_width) { - IM_ASSERT(offset >= -1); // -1 is allowed but not below ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - g.FocusRequestNextWindow = window; - g.FocusRequestNextCounterAll = window->DC.FocusCounterAll + 1 + offset; - g.FocusRequestNextCounterTab = INT_MAX; + window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); + window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); + g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; } -void ImGui::SetItemDefaultFocus() +void ImGui::PushMultiItemsWidths(int components, float w_full) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (!window->Appearing) - return; - if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent) - { - g.NavInitRequest = false; - g.NavInitResultId = g.NavWindow->DC.LastItemId; - g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos); - NavUpdateAnyRequestFlag(); - if (!IsItemVisible()) - SetScrollHereY(); - } + const ImGuiStyle& style = g.Style; + const float w_item_one = ImMax(1.0f, IM_FLOOR((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); + const float w_item_last = ImMax(1.0f, IM_FLOOR(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + window->DC.ItemWidthStack.push_back(w_item_last); + for (int i = 0; i < components-1; i++) + window->DC.ItemWidthStack.push_back(w_item_one); + window->DC.ItemWidth = window->DC.ItemWidthStack.back(); + g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; } -void ImGui::SetStateStorage(ImGuiStorage* tree) +void ImGui::PopItemWidth() { - ImGuiWindow* window = GImGui->CurrentWindow; - window->DC.StateStorage = tree ? tree : &window->StateStorage; + ImGuiWindow* window = GetCurrentWindow(); + window->DC.ItemWidthStack.pop_back(); + window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back(); } -ImGuiStorage* ImGui::GetStateStorage() +// Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(). +// The SetNextItemWidth() data is generally cleared/consumed by ItemAdd() or NextItemData.ClearFlags() +float ImGui::CalcItemWidth() { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->DC.StateStorage; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + float w; + if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) + w = g.NextItemData.Width; + else + w = window->DC.ItemWidth; + if (w < 0.0f) + { + float region_max_x = GetContentRegionMaxAbs().x; + w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); + } + w = IM_FLOOR(w); + return w; } -void ImGui::PushID(const char* str_id) +// [Internal] Calculate full item size given user provided 'size' parameter and default width/height. Default width is often == CalcItemWidth(). +// Those two functions CalcItemWidth vs CalcItemSize are awkwardly named because they are not fully symmetrical. +// Note that only CalcItemWidth() is publicly exposed. +// The 4.0f here may be changed to match CalcItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) +ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) { ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(str_id)); + + ImVec2 region_max; + if (size.x < 0.0f || size.y < 0.0f) + region_max = GetContentRegionMaxAbs(); + + if (size.x == 0.0f) + size.x = default_w; + else if (size.x < 0.0f) + size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x); + + if (size.y == 0.0f) + size.y = default_h; + else if (size.y < 0.0f) + size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y); + + return size; } -void ImGui::PushID(const char* str_id_begin, const char* str_id_end) +float ImGui::GetTextLineHeight() { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end)); + ImGuiContext& g = *GImGui; + return g.FontSize; } -void ImGui::PushID(const void* ptr_id) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); +float ImGui::GetTextLineHeightWithSpacing() +{ + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.ItemSpacing.y; } -void ImGui::PushID(int int_id) +float ImGui::GetFrameHeight() { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(int_id)); + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.FramePadding.y * 2.0f; } -// Push a given id value ignoring the ID stack as a seed. -void ImGui::PushOverrideID(ImGuiID id) +float ImGui::GetFrameHeightWithSpacing() { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(id); + ImGuiContext& g = *GImGui; + return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y; } -void ImGui::PopID() +// FIXME: All the Contents Region function are messy or misleading. WE WILL AIM TO OBSOLETE ALL OF THEM WITH A NEW "WORK RECT" API. Thanks for your patience! + +// FIXME: This is in window space (not screen space!). +ImVec2 ImGui::GetContentRegionMax() { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.pop_back(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 mx = window->ContentRegionRect.Max - window->Pos; + if (window->DC.CurrentColumns) + mx.x = window->WorkRect.Max.x - window->Pos.x; + return mx; } -ImGuiID ImGui::GetID(const char* str_id) +// [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. +ImVec2 ImGui::GetContentRegionMaxAbs() { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(str_id); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 mx = window->ContentRegionRect.Max; + if (window->DC.CurrentColumns) + mx.x = window->WorkRect.Max.x; + return mx; } -ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) +ImVec2 ImGui::GetContentRegionAvail() { ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(str_id_begin, str_id_end); + return GetContentRegionMaxAbs() - window->DC.CursorPos; } -ImGuiID ImGui::GetID(const void* ptr_id) +// In window space (not screen space!) +ImVec2 ImGui::GetWindowContentRegionMin() { ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(ptr_id); + return window->ContentRegionRect.Min - window->Pos; } -bool ImGui::IsRectVisible(const ImVec2& size) +ImVec2 ImGui::GetWindowContentRegionMax() { ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); + return window->ContentRegionRect.Max - window->Pos; } -bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) +float ImGui::GetWindowContentRegionWidth() { ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); + return window->ContentRegionRect.GetWidth(); } // Lock horizontal starting position + capture group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) @@ -6948,7 +7247,7 @@ void ImGui::EndGroup() } window->DC.CurrLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now. - ItemSize(group_bb.GetSize(), 0.0f); + ItemSize(group_bb.GetSize()); ItemAdd(group_bb, 0); // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group. @@ -6976,50 +7275,6 @@ void ImGui::EndGroup() //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] } -// Gets back to previous line and continue with horizontal layout -// offset_from_start_x == 0 : follow right after previous item -// offset_from_start_x != 0 : align to specified x position (relative to window/group left) -// spacing_w < 0 : use default spacing if pos_x == 0, no spacing if pos_x != 0 -// spacing_w >= 0 : enforce spacing amount -void ImGui::SameLine(float offset_from_start_x, float spacing_w) -{ - ImGuiWindow* window = GetCurrentWindow(); - if (window->SkipItems) - return; - - ImGuiContext& g = *GImGui; - if (offset_from_start_x != 0.0f) - { - if (spacing_w < 0.0f) spacing_w = 0.0f; - window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x; - window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; - } - else - { - if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x; - window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w; - window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; - } - window->DC.CurrLineSize = window->DC.PrevLineSize; - window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; -} - -void ImGui::Indent(float indent_w) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - window->DC.Indent.x += (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; - window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; -} - -void ImGui::Unindent(float indent_w) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - window->DC.Indent.x -= (indent_w != 0.0f) ? indent_w : g.Style.IndentSpacing; - window->DC.CursorPos.x = window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x; -} - //----------------------------------------------------------------------------- // [SECTION] SCROLLING @@ -7145,7 +7400,7 @@ void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x { // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size IM_ASSERT(center_x_ratio >= 0.0f && center_x_ratio <= 1.0f); - window->ScrollTarget.x = (float)(int)(local_x + window->Scroll.x); + window->ScrollTarget.x = IM_FLOOR(local_x + window->Scroll.x); window->ScrollTargetCenterRatio.x = center_x_ratio; } @@ -7155,7 +7410,7 @@ void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); local_y -= decoration_up_height; - window->ScrollTarget.y = (float)(int)(local_y + window->Scroll.y); + window->ScrollTarget.y = IM_FLOOR(local_y + window->Scroll.y); window->ScrollTargetCenterRatio.y = center_y_ratio; } @@ -7197,9 +7452,15 @@ void ImGui::SetScrollHereY(float center_y_ratio) //----------------------------------------------------------------------------- void ImGui::BeginTooltip() +{ + BeginTooltipEx(ImGuiWindowFlags_None, ImGuiTooltipFlags_None); +} + +void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags) { ImGuiContext& g = *GImGui; - if (g.DragDropWithinSourceOrTarget) + + if (g.DragDropWithinSource || g.DragDropWithinTarget) { // The default tooltip position is a little offset to give space to see the context menu (it's also clamped within the current viewport/monitor) // In the context of a dragging tooltip we try to reduce that offset and we enforce following the cursor. @@ -7209,21 +7470,12 @@ void ImGui::BeginTooltip() SetNextWindowPos(tooltip_pos); SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f); //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :( - BeginTooltipEx(0, true); - } - else - { - BeginTooltipEx(0, false); + tooltip_flags |= ImGuiTooltipFlags_OverridePreviousTooltip; } -} -// Not exposed publicly as BeginTooltip() because bool parameters are evil. Let's see if other needs arise first. -void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip) -{ - ImGuiContext& g = *GImGui; char window_name[16]; ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", g.TooltipOverrideCount); - if (override_previous_tooltip) + if (tooltip_flags & ImGuiTooltipFlags_OverridePreviousTooltip) if (ImGuiWindow* window = FindWindowByName(window_name)) if (window->Active) { @@ -7244,11 +7496,7 @@ void ImGui::EndTooltip() void ImGui::SetTooltipV(const char* fmt, va_list args) { - ImGuiContext& g = *GImGui; - if (g.DragDropWithinSourceOrTarget) - BeginTooltip(); - else - BeginTooltipEx(0, true); + BeginTooltipEx(0, ImGuiTooltipFlags_OverridePreviousTooltip); TextV(fmt, args); EndTooltip(); } @@ -7432,7 +7680,7 @@ void ImGui::CloseCurrentPopup() window->DC.NavHideHighlightOneFrame = true; } -bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags) +bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; if (!IsPopupOpen(id)) @@ -7442,12 +7690,13 @@ bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags) } char name[20]; - if (extra_flags & ImGuiWindowFlags_ChildMenu) + if (flags & ImGuiWindowFlags_ChildMenu) ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth else ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame - bool is_open = Begin(name, NULL, extra_flags | ImGuiWindowFlags_Popup); + flags |= ImGuiWindowFlags_Popup; + bool is_open = Begin(name, NULL, flags); if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display) EndPopup(); @@ -7499,16 +7748,22 @@ bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags fla void ImGui::EndPopup() { ImGuiContext& g = *GImGui; - IM_ASSERT(g.CurrentWindow->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginPopup()/EndPopup() calls + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(window->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginPopup()/EndPopup() calls IM_ASSERT(g.BeginPopupStack.Size > 0); // Make all menus and popups wrap around for now, may need to expose that policy. - NavMoveRequestTryWrapping(g.CurrentWindow, ImGuiNavMoveFlags_LoopY); + NavMoveRequestTryWrapping(window, ImGuiNavMoveFlags_LoopY); + // Child-popups don't need to be layed out + IM_ASSERT(g.WithinEndChild == false); + if (window->Flags & ImGuiWindowFlags_ChildWindow) + g.WithinEndChild = true; End(); + g.WithinEndChild = false; } -bool ImGui::OpenPopupOnItemClick(const char* str_id, int mouse_button) +bool ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiMouseButton mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) @@ -7524,7 +7779,7 @@ bool ImGui::OpenPopupOnItemClick(const char* str_id, int mouse_button) // This is a helper to handle the simplest case of associating one named popup to one given widget. // You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). // You can pass a NULL str_id to use the identifier of the last item. -bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button) +bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiMouseButton mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; if (window->SkipItems) @@ -7536,7 +7791,7 @@ bool ImGui::BeginPopupContextItem(const char* str_id, int mouse_button) return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } -bool ImGui::BeginPopupContextWindow(const char* str_id, int mouse_button, bool also_over_items) +bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mouse_button, bool also_over_items) { if (!str_id) str_id = "window_context"; @@ -7547,7 +7802,7 @@ bool ImGui::BeginPopupContextWindow(const char* str_id, int mouse_button, bool a return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } -bool ImGui::BeginPopupContextVoid(const char* str_id, int mouse_button) +bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiMouseButton mouse_button) { if (!str_id) str_id = "void_context"; @@ -7635,7 +7890,7 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) float horizontal_overlap = g.Style.ItemInnerSpacing.x; // We want some overlap to convey the relative depth of each menu (currently the amount of overlap is hard-coded to style.ItemSpacing.x). ImRect r_avoid; if (parent_window->DC.MenuBarAppending) - r_avoid = ImRect(-FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight(), FLT_MAX, parent_window->Pos.y + parent_window->TitleBarHeight() + parent_window->MenuBarHeight()); + r_avoid = ImRect(-FLT_MAX, parent_window->ClipRect.Min.y, FLT_MAX, parent_window->ClipRect.Max.y); // Avoid parent menu-bar. If we wanted multi-line menu-bar, we may instead want to have the calling window setup e.g. a NextWindowData.PosConstraintAvoidRect field else r_avoid = ImRect(parent_window->Pos.x + horizontal_overlap, -FLT_MAX, parent_window->Pos.x + parent_window->Size.x - horizontal_overlap - parent_window->ScrollbarSizes.x, FLT_MAX); return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); @@ -7664,11 +7919,56 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) return window->Pos; } - //----------------------------------------------------------------------------- // [SECTION] KEYBOARD/GAMEPAD NAVIGATION //----------------------------------------------------------------------------- +// FIXME-NAV: The existence of SetNavID vs SetNavIDWithRectRel vs SetFocusID is incredibly messy and confusing, +// and needs some explanation or serious refactoring. +void ImGui::SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavWindow); + IM_ASSERT(nav_layer == 0 || nav_layer == 1); + g.NavId = id; + g.NavFocusScopeId = focus_scope_id; + g.NavWindow->NavLastIds[nav_layer] = id; +} + +void ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) +{ + ImGuiContext& g = *GImGui; + SetNavID(id, nav_layer, focus_scope_id); + g.NavWindow->NavRectRel[nav_layer] = rect_rel; + g.NavMousePosDirty = true; + g.NavDisableHighlight = false; + g.NavDisableMouseHover = true; +} + +void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(id != 0); + + // Assume that SetFocusID() is called in the context where its window->DC.NavLayerCurrent and window->DC.NavFocusScopeIdCurrent are valid. + // Note that window may be != g.CurrentWindow (e.g. SetFocusID call in InputTextEx for multi-line text) + const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; + if (g.NavWindow != window) + g.NavInitRequest = false; + g.NavWindow = window; + g.NavId = id; + g.NavLayer = nav_layer; + g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; + window->NavLastIds[nav_layer] = id; + if (window->DC.LastItemId == id) + window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos); + + if (g.ActiveIdSource == ImGuiInputSource_Nav) + g.NavDisableMouseHover = true; + else + g.NavDisableHighlight = true; +} + ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy) { if (ImFabs(dx) > ImFabs(dy)) @@ -7872,21 +8172,22 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con #endif if (new_best) { - result->ID = id; - result->SelectScopeId = g.MultiSelectScopeId; result->Window = window; + result->ID = id; + result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; result->RectRel = nav_bb_rel; } + // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. const float VISIBLE_RATIO = 0.70f; if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb)) { result = &g.NavMoveResultLocalVisibleSet; - result->ID = id; - result->SelectScopeId = g.MultiSelectScopeId; result->Window = window; + result->ID = id; + result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; result->RectRel = nav_bb_rel; } } @@ -7896,8 +8197,9 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con { g.NavWindow = window; // Always refresh g.NavWindow, because some operations such as FocusItem() don't have a window. g.NavLayer = window->DC.NavLayerCurrent; + g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; g.NavIdIsAlive = true; - g.NavIdTabCounter = window->DC.FocusCounterTab; + g.NavIdTabCounter = window->DC.FocusCounterTabStop; window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel; // Store item bounding box (relative to window position) } } @@ -7986,10 +8288,11 @@ static void NavRestoreLayer(ImGuiNavLayer layer) g.NavLayer = layer; if (layer == 0) g.NavWindow = ImGui::NavRestoreLastChildNavWindow(g.NavWindow); - if (layer == 0 && g.NavWindow->NavLastIds[0] != 0) - ImGui::SetNavIDWithRectRel(g.NavWindow->NavLastIds[0], layer, g.NavWindow->NavRectRel[0]); + ImGuiWindow* window = g.NavWindow; + if (layer == 0 && window->NavLastIds[0] != 0) + ImGui::SetNavIDWithRectRel(window->NavLastIds[0], layer, 0, window->NavRectRel[0]); else - ImGui::NavInitWindow(g.NavWindow, true); + ImGui::NavInitWindow(window, true); } static inline void ImGui::NavUpdateAnyRequestFlag() @@ -8012,7 +8315,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) //IMGUI_DEBUG_LOG("[Nav] NavInitWindow() init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); if (init_for_nav) { - SetNavID(0, g.NavLayer); + SetNavID(0, g.NavLayer, 0); g.NavInitRequest = true; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; @@ -8022,6 +8325,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) else { g.NavId = window->NavLastIds[0]; + g.NavFocusScopeId = 0; } } @@ -8091,7 +8395,8 @@ static void ImGui::NavUpdate() if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG("NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.FrameCount, g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); #endif - // Set input source as Gamepad when buttons are pressed before we map Keyboard (some features differs when used with Gamepad vs Keyboard) + // Set input source as Gamepad when buttons are pressed (as some features differs when used with Gamepad vs Keyboard) + // (do it before we map Keyboard input!) bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; if (nav_gamepad_active) @@ -8128,9 +8433,9 @@ static void ImGui::NavUpdate() // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) //IMGUI_DEBUG_LOG("[Nav] Apply NavInitRequest result: 0x%08X Layer %d in \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); if (g.NavInitRequestFromMove) - SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, g.NavInitResultRectRel); + SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); else - SetNavID(g.NavInitResultId, g.NavLayer); + SetNavID(g.NavInitResultId, g.NavLayer, 0); g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; } g.NavInitRequest = false; @@ -8183,7 +8488,7 @@ static void ImGui::NavUpdate() g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); // Process NavCancel input (to close a popup, get back to parent, clear focus) - if (IsNavInputPressed(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) + if (IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) { if (g.ActiveId != 0) { @@ -8197,8 +8502,9 @@ static void ImGui::NavUpdate() ImGuiWindow* parent_window = g.NavWindow->ParentWindow; IM_ASSERT(child_window->ChildId != 0); FocusWindow(parent_window); - SetNavID(child_window->ChildId, 0); - g.NavIdIsAlive = false; + SetNavID(child_window->ChildId, 0, 0); + // Reassigning with same value, we're being explicit here. + g.NavIdIsAlive = false; // -V1048 if (g.NavDisableMouseHover) g.NavMousePosDirty = true; } @@ -8218,7 +8524,7 @@ static void ImGui::NavUpdate() // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) g.NavWindow->NavLastIds[0] = 0; - g.NavId = 0; + g.NavId = g.NavFocusScopeId = 0; } } @@ -8227,14 +8533,14 @@ static void ImGui::NavUpdate() if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); - bool activate_pressed = activate_down && IsNavInputPressed(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); + bool activate_pressed = activate_down && IsNavInputTest(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); if (g.ActiveId == 0 && activate_pressed) g.NavActivateId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) g.NavActivateDownId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) g.NavActivatePressedId = g.NavId; - if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputPressed(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) g.NavInputId = g.NavId; } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) @@ -8255,10 +8561,11 @@ static void ImGui::NavUpdate() g.NavMoveRequestFlags = ImGuiNavMoveFlags_None; if (g.NavWindow && !g.NavWindowingTarget && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { - if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadLeft, ImGuiNavInput_KeyLeft_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Left; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadRight,ImGuiNavInput_KeyRight_,ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Right; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadUp, ImGuiNavInput_KeyUp_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Up; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && IsNavInputPressedAnyOfTwo(ImGuiNavInput_DpadDown, ImGuiNavInput_KeyDown_, ImGuiInputReadMode_Repeat)) { g.NavMoveDir = ImGuiDir_Down; } + const ImGuiInputReadMode read_mode = ImGuiInputReadMode_Repeat; + if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && (IsNavInputTest(ImGuiNavInput_DpadLeft, read_mode) || IsNavInputTest(ImGuiNavInput_KeyLeft_, read_mode))) { g.NavMoveDir = ImGuiDir_Left; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && (IsNavInputTest(ImGuiNavInput_DpadRight, read_mode) || IsNavInputTest(ImGuiNavInput_KeyRight_, read_mode))) { g.NavMoveDir = ImGuiDir_Right; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && (IsNavInputTest(ImGuiNavInput_DpadUp, read_mode) || IsNavInputTest(ImGuiNavInput_KeyUp_, read_mode))) { g.NavMoveDir = ImGuiDir_Up; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && (IsNavInputTest(ImGuiNavInput_DpadDown, read_mode) || IsNavInputTest(ImGuiNavInput_KeyDown_, read_mode))) { g.NavMoveDir = ImGuiDir_Down; } } g.NavMoveClipDir = g.NavMoveDir; } @@ -8287,7 +8594,8 @@ static void ImGui::NavUpdate() { //IMGUI_DEBUG_LOG("[Nav] NavInitRequest from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); g.NavInitRequest = g.NavInitRequestFromMove = true; - g.NavInitResultId = 0; + // Reassigning with same value, we're being explicit here. + g.NavInitResultId = 0; // -V1048 g.NavDisableHighlight = false; } NavUpdateAnyRequestFlag(); @@ -8297,7 +8605,7 @@ static void ImGui::NavUpdate() { // *Fallback* manual-scroll with Nav directional keys when window has no navigable item ImGuiWindow* window = g.NavWindow; - const float scroll_speed = ImFloor(window->CalcFontSize() * 100 * g.IO.DeltaTime + 0.5f); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. + const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * g.IO.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) { if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) @@ -8336,7 +8644,7 @@ static void ImGui::NavUpdate() float pad = window->CalcFontSize() * 0.5f; window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item window->NavRectRel[g.NavLayer].ClipWith(window_rect_rel); - g.NavId = 0; + g.NavId = g.NavFocusScopeId = 0; } g.NavMoveFromClampedRefRect = false; } @@ -8416,9 +8724,10 @@ static void ImGui::NavUpdateMoveResult() { // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; - g.NavJustMovedToMultiSelectScopeId = result->SelectScopeId; + g.NavJustMovedToFocusScopeId = result->FocusScopeId; + } - SetNavIDWithRectRel(result->ID, g.NavLayer, result->RectRel); + SetNavIDWithRectRel(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); g.NavMoveFromClampedRefRect = false; } @@ -8553,12 +8862,12 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL-TAB or Square+L/R window selection - bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputPressed(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); + bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { - g.NavWindowingTarget = g.NavWindowingTargetAnim = window; + g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; // FIXME-DOCK: Will need to use RootWindowDockStop g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true; g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_NavKeyboard : ImGuiInputSource_NavGamepad; @@ -8572,7 +8881,7 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // Select window to focus - const int focus_change_dir = (int)IsNavInputPressed(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputPressed(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); + const int focus_change_dir = (int)IsNavInputTest(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputTest(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); if (focus_change_dir != 0) { NavUpdateWindowingHighlightWindow(focus_change_dir); @@ -8604,9 +8913,9 @@ static void ImGui::NavUpdateWindowing() // Keyboard: Press and Release ALT to toggle menu layer // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of back-end clearing releases all keys on ALT-TAB - if (IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) + if (IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) g.NavWindowingToggleLayer = true; - if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputPressed(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) + if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev)) apply_toggle_layer = true; @@ -8711,7 +9020,6 @@ void ImGui::NavUpdateWindowingOverlay() PopStyleVar(); } - //----------------------------------------------------------------------------- // [SECTION] DRAG AND DROP //----------------------------------------------------------------------------- @@ -8740,7 +9048,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) bool source_drag_active = false; ImGuiID source_id = 0; ImGuiID source_parent_id = 0; - int mouse_button = 0; + ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; if (!(flags & ImGuiDragDropFlags_SourceExtern)) { source_id = window->DC.LastItemId; @@ -8785,6 +9093,11 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) return false; source_parent_id = window->IDStack.back(); source_drag_active = IsMouseDragging(mouse_button); + + // Disable navigation and key inputs while dragging + g.ActiveIdUsingNavDirMask = ~(ImU32)0; + g.ActiveIdUsingNavInputMask = ~(ImU32)0; + g.ActiveIdUsingKeyInputMask = ~(ImU64)0; } else { @@ -8807,7 +9120,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) g.DragDropMouseButton = mouse_button; } g.DragDropSourceFrameCount = g.FrameCount; - g.DragDropWithinSourceOrTarget = true; + g.DragDropWithinSource = true; if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) { @@ -8834,7 +9147,7 @@ void ImGui::EndDragDropSource() { ImGuiContext& g = *GImGui; IM_ASSERT(g.DragDropActive); - IM_ASSERT(g.DragDropWithinSourceOrTarget && "Not after a BeginDragDropSource()?"); + IM_ASSERT(g.DragDropWithinSource && "Not after a BeginDragDropSource()?"); if (!(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) EndTooltip(); @@ -8842,7 +9155,7 @@ void ImGui::EndDragDropSource() // Discard the drag if have not called SetDragDropPayload() if (g.DragDropPayload.DataFrameCount == -1) ClearDragDrop(); - g.DragDropWithinSourceOrTarget = false; + g.DragDropWithinSource = false; } // Use 'cond' to choose to submit payload on drag start or every frame @@ -8904,10 +9217,10 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) if (window->SkipItems) return false; - IM_ASSERT(g.DragDropWithinSourceOrTarget == false); + IM_ASSERT(g.DragDropWithinTarget == false); g.DragDropTargetRect = bb; g.DragDropTargetId = id; - g.DragDropWithinSourceOrTarget = true; + g.DragDropWithinTarget = true; return true; } @@ -8934,10 +9247,10 @@ bool ImGui::BeginDragDropTarget() if (g.DragDropPayload.SourceId == id) return false; - IM_ASSERT(g.DragDropWithinSourceOrTarget == false); + IM_ASSERT(g.DragDropWithinTarget == false); g.DragDropTargetRect = display_rect; g.DragDropTargetId = id; - g.DragDropWithinSourceOrTarget = true; + g.DragDropWithinTarget = true; return true; } @@ -9001,11 +9314,10 @@ void ImGui::EndDragDropTarget() { ImGuiContext& g = *GImGui; IM_ASSERT(g.DragDropActive); - IM_ASSERT(g.DragDropWithinSourceOrTarget); - g.DragDropWithinSourceOrTarget = false; + IM_ASSERT(g.DragDropWithinTarget); + g.DragDropWithinTarget = false; } - //----------------------------------------------------------------------------- // [SECTION] LOGGING/CAPTURING //----------------------------------------------------------------------------- @@ -9023,9 +9335,15 @@ void ImGui::LogText(const char* fmt, ...) va_list args; va_start(args, fmt); if (g.LogFile) - vfprintf(g.LogFile, fmt, args); + { + g.LogBuffer.Buf.resize(0); + g.LogBuffer.appendfv(fmt, args); + ImFileWrite(g.LogBuffer.c_str(), sizeof(char), (ImU64)g.LogBuffer.size(), g.LogFile); + } else + { g.LogBuffer.appendfv(fmt, args); + } va_end(args); } @@ -9102,8 +9420,11 @@ void ImGui::LogToTTY(int auto_open_depth) ImGuiContext& g = *GImGui; if (g.LogEnabled) return; + IM_UNUSED(auto_open_depth); +#ifndef IMGUI_DISABLE_TTY_FUNCTIONS LogBegin(ImGuiLogType_TTY, auto_open_depth); g.LogFile = stdout; +#endif } // Start logging/capturing text output to given file @@ -9120,8 +9441,8 @@ void ImGui::LogToFile(int auto_open_depth, const char* filename) filename = g.IO.LogFilename; if (!filename || !filename[0]) return; - FILE* f = ImFileOpen(filename, "ab"); - if (f == NULL) + ImFileHandle f = ImFileOpen(filename, "ab"); + if (!f) { IM_ASSERT(0); return; @@ -9158,10 +9479,12 @@ void ImGui::LogFinish() switch (g.LogType) { case ImGuiLogType_TTY: +#ifndef IMGUI_DISABLE_TTY_FUNCTIONS fflush(g.LogFile); +#endif break; case ImGuiLogType_File: - fclose(g.LogFile); + ImFileClose(g.LogFile); break; case ImGuiLogType_Buffer: break; @@ -9187,7 +9510,11 @@ void ImGui::LogButtons() ImGuiContext& g = *GImGui; PushID("LogButtons"); +#ifndef IMGUI_DISABLE_TTY_FUNCTIONS const bool log_to_tty = Button("Log To TTY"); SameLine(); +#else + const bool log_to_tty = false; +#endif const bool log_to_file = Button("Log To File"); SameLine(); const bool log_to_clipboard = Button("Log To Clipboard"); SameLine(); PushAllowKeyboardFocus(false); @@ -9209,6 +9536,34 @@ void ImGui::LogButtons() // [SECTION] SETTINGS //----------------------------------------------------------------------------- +// Called by NewFrame() +void ImGui::UpdateSettings() +{ + // Load settings on first frame (if not explicitly loaded manually before) + ImGuiContext& g = *GImGui; + if (!g.SettingsLoaded) + { + IM_ASSERT(g.SettingsWindows.empty()); + if (g.IO.IniFilename) + LoadIniSettingsFromDisk(g.IO.IniFilename); + g.SettingsLoaded = true; + } + + // Save settings (with a delay after the last modification, so we don't spam disk too much) + if (g.SettingsDirtyTimer > 0.0f) + { + g.SettingsDirtyTimer -= g.IO.DeltaTime; + if (g.SettingsDirtyTimer <= 0.0f) + { + if (g.IO.IniFilename != NULL) + SaveIniSettingsToDisk(g.IO.IniFilename); + else + g.IO.WantSaveIniSettings = true; // Let user know they can call SaveIniSettingsToMemory(). user will need to clear io.WantSaveIniSettings themselves. + g.SettingsDirtyTimer = 0.0f; + } + } +} + void ImGui::MarkIniSettingsDirty() { ImGuiContext& g = *GImGui; @@ -9227,25 +9582,31 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window) ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) { ImGuiContext& g = *GImGui; - g.SettingsWindows.push_back(ImGuiWindowSettings()); - ImGuiWindowSettings* settings = &g.SettingsWindows.back(); + #if !IMGUI_DEBUG_INI_SETTINGS // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() // Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier. if (const char* p = strstr(name, "###")) name = p; #endif - settings->Name = ImStrdup(name); - settings->ID = ImHashStr(name); + const size_t name_len = strlen(name); + + // Allocate chunk + const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1; + ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size); + IM_PLACEMENT_NEW(settings) ImGuiWindowSettings(); + settings->ID = ImHashStr(name, name_len); + memcpy(settings->GetName(), name, name_len + 1); // Store with zero terminator + return settings; } ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id) { ImGuiContext& g = *GImGui; - for (int i = 0; i != g.SettingsWindows.Size; i++) - if (g.SettingsWindows[i].ID == id) - return &g.SettingsWindows[i]; + for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) + if (settings->ID == id) + return settings; return NULL; } @@ -9313,18 +9674,12 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) line_end[-1] = 0; const char* name_end = line_end - 1; const char* type_start = line + 1; - char* type_end = (char*)(intptr_t)ImStrchrRange(type_start, name_end, ']'); + char* type_end = (char*)(void*)ImStrchrRange(type_start, name_end, ']'); const char* name_start = type_end ? ImStrchrRange(type_end + 1, name_end, '[') : NULL; if (!type_end || !name_start) - { - name_start = type_start; // Import legacy entries that have no type - type_start = "Window"; - } - else - { - *type_end = 0; // Overwrite first ']' - name_start++; // Skip second '[' - } + continue; + *type_end = 0; // Overwrite first ']' + name_start++; // Skip second '[' entry_handler = FindSettingsHandler(type_start); entry_data = entry_handler ? entry_handler->ReadOpenFn(&g, entry_handler, name_start) : NULL; } @@ -9347,11 +9702,11 @@ void ImGui::SaveIniSettingsToDisk(const char* ini_filename) size_t ini_data_size = 0; const char* ini_data = SaveIniSettingsToMemory(&ini_data_size); - FILE* f = ImFileOpen(ini_filename, "wt"); + ImFileHandle f = ImFileOpen(ini_filename, "wt"); if (!f) return; - fwrite(ini_data, sizeof(char), ini_data_size, f); - fclose(f); + ImFileWrite(ini_data, sizeof(char), ini_data_size, f); + ImFileClose(f); } // Call registered handlers (e.g. SettingsHandlerWindow_WriteAll() + custom handlers) to write their stuff into a text buffer @@ -9371,7 +9726,7 @@ const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) return g.SettingsIniData.c_str(); } -static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) +static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name)); if (!settings) @@ -9379,7 +9734,7 @@ static void* SettingsHandlerWindow_ReadOpen(ImGuiContext*, ImGuiSettingsHandler* return (void*)settings; } -static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) +static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) { ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry; int x, y; @@ -9389,7 +9744,7 @@ static void SettingsHandlerWindow_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, else if (sscanf(line, "Collapsed=%d", &i) == 1) settings->Collapsed = (i != 0); } -static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) +static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) { // Gather data from windows that were active during this session // (if a window wasn't opened in this session we preserve its settings) @@ -9400,11 +9755,11 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl if (window->Flags & ImGuiWindowFlags_NoSavedSettings) continue; - ImGuiWindowSettings* settings = (window->SettingsIdx != -1) ? &g.SettingsWindows[window->SettingsIdx] : ImGui::FindWindowSettings(window->ID); + ImGuiWindowSettings* settings = (window->SettingsOffset != -1) ? g.SettingsWindows.ptr_from_offset(window->SettingsOffset) : ImGui::FindWindowSettings(window->ID); if (!settings) { settings = ImGui::CreateNewWindowSettings(window->Name); - window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings); + window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); } IM_ASSERT(settings->ID == window->ID); settings->Pos = ImVec2ih((short)window->Pos.x, (short)window->Pos.y); @@ -9413,15 +9768,15 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl } // Write to text buffer - buf->reserve(buf->size() + g.SettingsWindows.Size * 96); // ballpark reserve - for (int i = 0; i != g.SettingsWindows.Size; i++) + buf->reserve(buf->size() + g.SettingsWindows.size() * 6); // ballpark reserve + for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) { - const ImGuiWindowSettings* settings = &g.SettingsWindows[i]; - buf->appendf("[%s][%s]\n", handler->TypeName, settings->Name); + const char* settings_name = settings->GetName(); + buf->appendf("[%s][%s]\n", handler->TypeName, settings_name); buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y); buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y); buf->appendf("Collapsed=%d\n", settings->Collapsed); - buf->appendf("\n"); + buf->append("\n"); } } @@ -9444,23 +9799,11 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl // [SECTION] PLATFORM DEPENDENT HELPERS //----------------------------------------------------------------------------- -#if defined(_WIN32) && !defined(_WINDOWS_) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && (!defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) || !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)) -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#ifndef __MINGW32__ -#include -#else -#include -#endif -#elif defined(__APPLE__) -#include -#endif - #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) #ifdef _MSC_VER #pragma comment(lib, "user32") +#pragma comment(lib, "kernel32") #endif // Win32 clipboard implementation @@ -9476,11 +9819,11 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) ::CloseClipboard(); return NULL; } - if (ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle)) + if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle)) { - int buf_len = ImTextCountUtf8BytesFromStr(wbuf_global, NULL) + 1; + int buf_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, NULL, 0, NULL, NULL); buf_local.resize(buf_len); - ImTextStrToUtf8(buf_local.Data, buf_len, wbuf_global, NULL); + ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, buf_local.Data, buf_len, NULL, NULL); } ::GlobalUnlock(wbuf_handle); ::CloseClipboard(); @@ -9491,15 +9834,15 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text) { if (!::OpenClipboard(NULL)) return; - const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1; - HGLOBAL wbuf_handle = ::GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar)); + const int wbuf_length = ::MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + HGLOBAL wbuf_handle = ::GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(WCHAR)); if (wbuf_handle == NULL) { ::CloseClipboard(); return; } - ImWchar* wbuf_global = (ImWchar*)::GlobalLock(wbuf_handle); - ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL); + WCHAR* wbuf_global = (WCHAR*)::GlobalLock(wbuf_handle); + ::MultiByteToWideChar(CP_UTF8, 0, text, -1, wbuf_global, wbuf_length); ::GlobalUnlock(wbuf_handle); ::EmptyClipboard(); if (::SetClipboardData(CF_UNICODETEXT, wbuf_handle) == NULL) @@ -9615,6 +9958,20 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} //----------------------------------------------------------------------------- #ifndef IMGUI_DISABLE_METRICS_WINDOW +// Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds. +static void MetricsHelpMarker(const char* desc) +{ + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); + ImGui::TextUnformatted(desc); + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } +} + void ImGui::ShowMetricsWindow(bool* p_open) { if (!ImGui::Begin("Dear ImGui Metrics", p_open)) @@ -9623,13 +9980,19 @@ void ImGui::ShowMetricsWindow(bool* p_open) return; } + // Debugging enums + enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Content, WRT_ContentRegionRect, WRT_Count }; // Windows Rect Type + const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Content", "ContentRegionRect" }; + enum { TRT_OuterRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersDesired, TRT_ColumnsContentRowsFrozen, TRT_ColumnsContentRowsUnfrozen, TRT_Count }; // Tables Rect Type + const char* trt_rects_names[TRT_Count] = { "OuterRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersDesired", "ColumnsContentRowsFrozen", "ColumnsContentRowsUnfrozen" }; + // State - enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Contents, WRT_ContentsRegionRect, WRT_Count }; // Windows Rect Type - const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Contents", "ContentsRegionRect" }; static bool show_windows_rects = false; static int show_windows_rect_type = WRT_WorkRect; static bool show_windows_begin_order = false; - static bool show_drawcmd_clip_rects = true; + static bool show_tables_rects = false; + static int show_tables_rect_type = TRT_WorkRect; + static bool show_drawcmd_details = true; // Basic info ImGuiContext& g = *GImGui; @@ -9642,11 +10005,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::Separator(); // Helper functions to display common structures: - // - NodeDrawList - // - NodeColumns - // - NodeWindow - // - NodeWindows - // - NodeTabBar + // - NodeDrawList() + // - NodeColumns() + // - NodeWindow() + // - NodeWindows() + // - NodeTabBar() + // - NodeStorage() struct Funcs { static ImRect GetWindowRect(ImGuiWindow* window, int rect_type) @@ -9656,8 +10020,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) else if (rect_type == WRT_InnerRect) { return window->InnerRect; } else if (rect_type == WRT_InnerClipRect) { return window->InnerClipRect; } else if (rect_type == WRT_WorkRect) { return window->WorkRect; } - else if (rect_type == WRT_Contents) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } - else if (rect_type == WRT_ContentsRegionRect) { return window->ContentsRegionRect; } + else if (rect_type == WRT_Content) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } + else if (rect_type == WRT_ContentRegionRect) { return window->ContentRegionRect; } IM_ASSERT(0); return ImRect(); } @@ -9680,9 +10044,9 @@ void ImGui::ShowMetricsWindow(bool* p_open) return; if (window && !window->WasActive) - ImGui::Text("(Note: owning Window is inactive: DrawList is not being rendered!)"); + ImGui::TextDisabled("Warning: owning Window is inactive. This DrawList is not being rendered!"); - int elem_offset = 0; + unsigned int elem_offset = 0; for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++) { if (pcmd->UserCallback == NULL && pcmd->ElemCount == 0) @@ -9692,45 +10056,77 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); continue; } + ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; char buf[300]; - ImFormatString(buf, IM_ARRAYSIZE(buf), "Draw %4d triangles, tex 0x%p, clip_rect (%4.0f,%4.0f)-(%4.0f,%4.0f)", - pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); + ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd: %4d triangles, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", + pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, + pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); - if (show_drawcmd_clip_rects && fg_draw_list && ImGui::IsItemHovered()) + if (show_drawcmd_details && fg_draw_list && ImGui::IsItemHovered()) { ImRect clip_rect = pcmd->ClipRect; - ImRect vtxs_rect; - for (int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++) + ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + for (unsigned int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++) vtxs_rect.Add(draw_list->VtxBuffer[idx_buffer ? idx_buffer[i] : i].pos); - clip_rect.Floor(); fg_draw_list->AddRect(clip_rect.Min, clip_rect.Max, IM_COL32(255,0,255,255)); - vtxs_rect.Floor(); fg_draw_list->AddRect(vtxs_rect.Min, vtxs_rect.Max, IM_COL32(255,255,0,255)); + fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255,0,255,255)); + fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(255,255,0,255)); } if (!pcmd_node_open) continue; + // Calculate approximate coverage area (touched pixel count) + // This will be in pixels squared as long there's no post-scaling happening to the renderer output. + float total_area = 0.0f; + for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + pcmd->ElemCount); base_idx += 3) + { + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++) + triangle[n] = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; + total_area += ImTriangleArea(triangle[0], triangle[1], triangle[2]); + } + + // Display vertex information summary. Hover to get all triangles drawn in wire-frame + ImFormatString(buf, IM_ARRAYSIZE(buf), "Mesh: ElemCount: %d, VtxOffset: +%d, IdxOffset: +%d, Area: ~%0.f px", pcmd->ElemCount, pcmd->VtxOffset, pcmd->IdxOffset, total_area); + ImGui::Selectable(buf); + if (fg_draw_list && ImGui::IsItemHovered() && show_drawcmd_details) + { + // Draw wire-frame version of everything + ImDrawListFlags backup_flags = fg_draw_list->Flags; + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + ImRect clip_rect = pcmd->ClipRect; + fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); + for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + pcmd->ElemCount); base_idx += 3) + { + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++) + triangle[n] = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); + } + fg_draw_list->Flags = backup_flags; + } + // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. - ImGui::Text("ElemCount: %d, ElemCount/3: %d, VtxOffset: +%d, IdxOffset: +%d", pcmd->ElemCount, pcmd->ElemCount/3, pcmd->VtxOffset, pcmd->IdxOffset); ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. while (clipper.Step()) for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart*3; prim < clipper.DisplayEnd; prim++) { char *buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); - ImVec2 triangles_pos[3]; + ImVec2 triangle[3]; for (int n = 0; n < 3; n++, idx_i++) { - int vtx_i = idx_buffer ? idx_buffer[idx_i] : idx_i; - ImDrawVert& v = draw_list->VtxBuffer[vtx_i]; - triangles_pos[n] = v.pos; + ImDrawVert& v = draw_list->VtxBuffer[idx_buffer ? idx_buffer[idx_i] : idx_i]; + triangle[n] = v.pos; buf_p += ImFormatString(buf_p, buf_end - buf_p, "%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\n", - (n == 0) ? "elem" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); + (n == 0) ? "Vert:" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); } + ImGui::Selectable(buf, false); if (fg_draw_list && ImGui::IsItemHovered()) { ImDrawListFlags backup_flags = fg_draw_list->Flags; - fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines at is more readable for very large and thin triangles. - fg_draw_list->AddPolyline(triangles_pos, 3, IM_COL32(255,255,0,255), true, 1.0f); + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255,255,0,255), true, 1.0f); fg_draw_list->Flags = backup_flags; } } @@ -9754,7 +10150,11 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size)) return; for (int i = 0; i < windows.Size; i++) + { + ImGui::PushID(windows[i]); Funcs::NodeWindow(windows[i], "Window"); + ImGui::PopID(); + } ImGui::TreePop(); } @@ -9765,7 +10165,10 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::BulletText("%s: NULL", label); return; } - if (!ImGui::TreeNode(window, "%s '%s', %d @ 0x%p", label, window->Name, (window->Active || window->WasActive), window)) + bool open = ImGui::TreeNode(label, "%s '%s', %d @ 0x%p", label, window->Name, (window->Active || window->WasActive), window); + if (ImGui::IsItemHovered() && window->WasActive) + ImGui::GetForegroundDrawList()->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); + if (!open) return; ImGuiWindowFlags flags = window->Flags; NodeDrawList(window, window->DrawList, "DrawList"); @@ -9774,7 +10177,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); - ImGui::BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f)", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y); + ImGui::BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f) Scrollbar:%s%s", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y, window->ScrollbarX ? "X" : "", window->ScrollbarY ? "Y" : ""); ImGui::BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); ImGui::BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); ImGui::BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); @@ -9792,7 +10195,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) NodeColumns(&window->ColumnsStorage[n]); ImGui::TreePop(); } - ImGui::BulletText("Storage: %d bytes", window->StateStorage.Data.size_in_bytes()); + NodeStorage(&window->StateStorage, "Storage"); ImGui::TreePop(); } @@ -9802,7 +10205,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) char buf[256]; char* p = buf; const char* buf_end = buf + IM_ARRAYSIZE(buf); - ImFormatString(p, buf_end - p, "TabBar (%d tabs)%s", tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? " *Inactive*" : ""); + p += ImFormatString(p, buf_end - p, "TabBar (%d tabs)%s", tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? " *Inactive*" : ""); if (ImGui::TreeNode(tab_bar, "%s", buf)) { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) @@ -9811,22 +10214,36 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::PushID(tab); if (ImGui::SmallButton("<")) { TabBarQueueChangeTabOrder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); if (ImGui::SmallButton(">")) { TabBarQueueChangeTabOrder(tab_bar, tab, +1); } ImGui::SameLine(); - ImGui::Text("%02d%c Tab 0x%08X", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID); + ImGui::Text("%02d%c Tab 0x%08X '%s'", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : ""); ImGui::PopID(); } ImGui::TreePop(); } } + + static void NodeStorage(ImGuiStorage* storage, const char* label) + { + if (!ImGui::TreeNode(label, "%s: %d entries, %d bytes", label, storage->Data.Size, storage->Data.size_in_bytes())) + return; + for (int n = 0; n < storage->Data.Size; n++) + { + const ImGuiStorage::ImGuiStoragePair& p = storage->Data[n]; + ImGui::BulletText("Key 0x%08X Value { i: %d }", p.key, p.val_i); // Important: we currently don't store a type, real value may not be integer. + } + ImGui::TreePop(); + } }; Funcs::NodeWindows(g.Windows, "Windows"); - if (ImGui::TreeNode("DrawList", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) + //Funcs::NodeWindows(g.WindowsFocusOrder, "WindowsFocusOrder"); + if (ImGui::TreeNode("DrawLists", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) { for (int i = 0; i < g.DrawDataBuilder.Layers[0].Size; i++) Funcs::NodeDrawList(NULL, g.DrawDataBuilder.Layers[0][i], "DrawList"); ImGui::TreePop(); } + // Details for Popups if (ImGui::TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) { for (int i = 0; i < g.OpenPopupStack.Size; i++) @@ -9837,27 +10254,36 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::TreePop(); } - if (ImGui::TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.Data.Size)) + // Details for TabBars + if (ImGui::TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.GetSize())) { - for (int n = 0; n < g.TabBars.Data.Size; n++) + for (int n = 0; n < g.TabBars.GetSize(); n++) Funcs::NodeTabBar(g.TabBars.GetByIndex(n)); ImGui::TreePop(); } -#if 0 - if (ImGui::TreeNode("Docking")) + // Details for Tables + IM_UNUSED(trt_rects_names); + IM_UNUSED(show_tables_rects); + IM_UNUSED(show_tables_rect_type); +#ifdef IMGUI_HAS_TABLE + if (ImGui::TreeNode("Tables", "Tables (%d)", g.Tables.GetSize())) { + for (int n = 0; n < g.Tables.GetSize(); n++) + Funcs::NodeTable(g.Tables.GetByIndex(n)); ImGui::TreePop(); } -#endif +#endif // #define IMGUI_HAS_TABLE -#if 0 - if (ImGui::TreeNode("Tables", "Tables (%d)", g.Tables.Data.Size)) + // Details for Docking +#ifdef IMGUI_HAS_DOCK + if (ImGui::TreeNode("Docking")) { ImGui::TreePop(); } -#endif +#endif // #define IMGUI_HAS_DOCK + // Misc Details if (ImGui::TreeNode("Internal state")) { const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); @@ -9878,17 +10304,20 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::TreePop(); } + // Tools if (ImGui::TreeNode("Tools")) { // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. if (ImGui::Button("Item Picker..")) ImGui::DebugStartItemPicker(); + ImGui::SameLine(); + MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); ImGui::Checkbox("Show windows rectangles", &show_windows_rects); ImGui::SameLine(); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); - show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count); + show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count, WRT_Count); if (show_windows_rects && g.NavWindow) { ImGui::BulletText("'%s':", g.NavWindow->Name); @@ -9900,11 +10329,11 @@ void ImGui::ShowMetricsWindow(bool* p_open) } ImGui::Unindent(); } - ImGui::Checkbox("Show clipping rectangle when hovering ImDrawCmd node", &show_drawcmd_clip_rects); + ImGui::Checkbox("Show details when hovering ImDrawCmd node", &show_drawcmd_details); ImGui::TreePop(); } - // Tool: Display windows Rectangles and Begin Order + // Overlay: Display windows Rectangles and Begin Order if (show_windows_rects || show_windows_begin_order) { for (int n = 0; n < g.Windows.Size; n++) @@ -9928,6 +10357,25 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } } + +#ifdef IMGUI_HAS_TABLE + // Overlay: Display Tables Rectangles + if (show_tables_rects) + { + for (int table_n = 0; table_n < g.Tables.GetSize(); table_n++) + { + ImGuiTable* table = g.Tables.GetByIndex(table_n); + } + } +#endif // #define IMGUI_HAS_TABLE + +#ifdef IMGUI_HAS_DOCK + // Overlay: Display Docking info + if (show_docking_nodes && g.IO.KeyCtrl) + { + } +#endif // #define IMGUI_HAS_DOCK + ImGui::End(); } @@ -9946,3 +10394,5 @@ void ImGui::ShowMetricsWindow(bool*) { } #endif //----------------------------------------------------------------------------- + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui.h b/imgui/imgui.h index 1675fc6c..70798230 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,10 +1,20 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (headers) -// See imgui.cpp file for documentation. -// Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. -// Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. -// Get latest version at https://github.com/ocornut/imgui +// Help: +// - Read FAQ at http://dearimgui.org/faq +// - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. +// Read imgui.cpp for more details, documentation and comments. + +// Resources: +// - FAQ http://dearimgui.org/faq +// - Homepage & latest https://github.com/ocornut/imgui +// - Releases & changelog https://github.com/ocornut/imgui/releases +// - Gallery https://github.com/ocornut/imgui/issues/2847 (please post your screenshots/video there!) +// - Glossary https://github.com/ocornut/imgui/wiki/Glossary +// - Wiki https://github.com/ocornut/imgui/wiki +// - Issues & support https://github.com/ocornut/imgui/issues /* @@ -27,7 +37,7 @@ Index of this file: #pragma once -// Configuration file with compile-time options (edit imconfig.h or define IMGUI_USER_CONFIG to your own filename) +// Configuration file with compile-time options (edit imconfig.h or #define IMGUI_USER_CONFIG to your own filename) #ifdef IMGUI_USER_CONFIG #include IMGUI_USER_CONFIG #endif @@ -35,19 +45,22 @@ Index of this file: #include "imconfig.h" #endif +#ifndef IMGUI_DISABLE + //----------------------------------------------------------------------------- // Header mess //----------------------------------------------------------------------------- -#include // FLT_MAX -#include // va_list +// Includes +#include // FLT_MIN, FLT_MAX +#include // va_list, va_start, va_end #include // ptrdiff_t, NULL #include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.74 WIP" -#define IMGUI_VERSION_NUM 17301 +#define IMGUI_VERSION "1.76 WIP" +#define IMGUI_VERSION_NUM 17502 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Define attributes of all API symbols declarations (e.g. for DLL under Windows) @@ -65,20 +78,22 @@ Index of this file: #include #define IM_ASSERT(_EXPR) assert(_EXPR) // You can override the default assert handler by editing imconfig.h #endif -#if defined(__clang__) || defined(__GNUC__) -#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to user functions. +#if !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__)) +#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // To apply printf-style warnings to our functions. #define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) #else #define IM_FMTARGS(FMT) #define IM_FMTLIST(FMT) #endif -#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR))) // Size of a static C-style array. Don't use on pointers! +#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*_ARR))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)_VAR) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. #if (__cplusplus >= 201100) #define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 #else #define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. #endif +#define IM_UNICODE_CODEPOINT_MAX (sizeof(ImWchar) == 2 ? 0xFFFF : 0x10FFFF) // Last Unicode code point supported by this build. +#define IM_UNICODE_CODEPOINT_INVALID 0xFFFD // Standard invalid Unicode code point. // Warnings #if defined(__clang__) @@ -123,18 +138,23 @@ struct ImGuiTextBuffer; // Helper to hold and append into a text buf struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbb][,ccccc]") // Typedefs and Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) -// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. +// Use your programming IDE "Go to definition" facility on the names in the central column below to find the actual flags/enum lists. #ifndef ImTextureID typedef void* ImTextureID; // User data to identify a texture (this is whatever to you want it to be! read the FAQ about ImTextureID in imgui.cpp) #endif typedef unsigned int ImGuiID; // Unique ID used by widgets (typically hashed from a stack of string) -typedef unsigned short ImWchar; // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings. +#ifndef ImWchar +#define ImWchar ImWchar16 +#endif +typedef unsigned short ImWchar16; // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings. +typedef unsigned int ImWchar32; // A single U32 character for keyboard input/display. Define ImWchar to ImWchar32 to use it. See imconfig.h . typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type typedef int ImGuiDir; // -> enum ImGuiDir_ // Enum: A cardinal direction typedef int ImGuiKey; // -> enum ImGuiKey_ // Enum: A key identifier (ImGui-side enum) typedef int ImGuiNavInput; // -> enum ImGuiNavInput_ // Enum: An input identifier for navigation +typedef int ImGuiMouseButton; // -> enum ImGuiMouseButton_ // Enum: A mouse button identifier (0=left, 1=right, 2=middle) typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor identifier typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect(), AddRectFilled() etc. @@ -157,7 +177,7 @@ typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data); typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Scalar data types -typedef signed char ImS8; // 8-bit signed integer == char +typedef signed char ImS8; // 8-bit signed integer typedef unsigned char ImU8; // 8-bit unsigned integer typedef signed short ImS16; // 16-bit signed integer typedef unsigned short ImU16; // 16-bit unsigned integer @@ -178,11 +198,11 @@ typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) // 2D vector (often used to store positions, sizes, etc.) struct ImVec2 { - float x, y; - ImVec2() { x = y = 0.0f; } - ImVec2(float _x, float _y) { x = _x; y = _y; } - float operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. - float& operator[] (size_t idx) { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + float x, y; + ImVec2() { x = y = 0.0f; } + ImVec2(float _x, float _y) { x = _x; y = _y; } + float operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + float& operator[] (size_t idx) { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. #ifdef IM_VEC2_CLASS_EXTRA IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2. #endif @@ -191,9 +211,9 @@ struct ImVec2 // 4D vector (often used to store floating-point colors) struct ImVec4 { - float x, y, z, w; - ImVec4() { x = y = z = w = 0.0f; } - ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } + float x, y, z, w; + ImVec4() { x = y = z = w = 0.0f; } + ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } #ifdef IM_VEC4_CLASS_EXTRA IM_VEC4_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4. #endif @@ -201,14 +221,14 @@ struct ImVec4 //----------------------------------------------------------------------------- // ImGui: Dear ImGui end-user API -// (Inside a namespace so you can add extra functions in your own separate file. Please don't modify imgui.cpp/.h!) +// (This is a namespace. You can add extra ImGui:: functions in your own separate file. Please don't modify imgui source files!) //----------------------------------------------------------------------------- namespace ImGui { // Context creation and access // Each context create its own ImFontAtlas by default. You may instance one yourself and pass it to CreateContext() to share a font atlas between imgui contexts. - // All those functions are not reliant on the current context. + // None of those functions is reliant on the current context. IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = destroy current context IMGUI_API ImGuiContext* GetCurrentContext(); @@ -217,16 +237,16 @@ namespace ImGui // Main IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) - IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame. + IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame! IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). - IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(), you likely don't need to call that yourself directly. If you don't need to render data (skipping rendering) you may call EndFrame() but you'll have wasted CPU already! If you don't need to render, better to not create any imgui windows and not call NewFrame() at all! - IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can get call GetDrawData() to obtain it and run your rendering function. (Obsolete: this used to call io.RenderDrawListsFn(). Nowadays, we allow and prefer calling your render function yourself.) + IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(). If you don't need to render data (skipping rendering) you may call EndFrame() without Render()... but you'll have wasted CPU already! If you don't need to render, better to not create any windows and not call NewFrame() at all! + IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can get call GetDrawData() to obtain it and run your rendering function (up to v1.60, this used to call io.RenderDrawListsFn(). Nowadays, we allow and prefer calling your render function yourself.) IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render. // Demo, Debug, Information IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window (previously called ShowTestWindow). demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. - IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debug window. display Dear ImGui internals: draw commands (with individual draw calls and vertices), window list, basic internal state, etc. + IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Debug/Metrics window. display Dear ImGui internals: draw commands (with individual draw calls and vertices), window list, basic internal state, etc. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. @@ -245,8 +265,9 @@ namespace ImGui // which clicking will set the boolean to false when clicked. // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting // anything to the window. Always call a matching End() for each Begin() call, regardless of its return value! - // [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. - // where the EndXXX call should only be called if the corresponding BeginXXX function returned true.] + // [Important: due to legacy reason, this is inconsistent with most other functions such as BeginMenu/EndMenu, + // BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function + // returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] // - Note that the bottom of window stack always contains a window called "Debug". IMGUI_API bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); IMGUI_API void End(); @@ -255,13 +276,13 @@ namespace ImGui // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400). // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. - // Always call a matching EndChild() for each BeginChild() call, regardless of its return value [this is due to legacy reason and is inconsistent with most other functions such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function returned true.] + // Always call a matching EndChild() for each BeginChild() call, regardless of its return value [as with Begin: this is due to legacy reason and inconsistent with most BeginXXX functions apart from the regular Begin() which behaves like BeginChild().] IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API void EndChild(); // Windows Utilities - // - "current window" = the window we are appending into while inside a Begin()/End() block. "next window" = next window we will Begin() into. + // - 'current window' = the window we are appending into while inside a Begin()/End() block. 'next window' = next window we will Begin() into. IMGUI_API bool IsWindowAppearing(); IMGUI_API bool IsWindowCollapsed(); IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options. @@ -279,7 +300,7 @@ namespace ImGui IMGUI_API void SetNextWindowContentSize(const ImVec2& size); // set next window content size (~ scrollable client area, which enforce the range of scrollbars). Not including window decorations (title bar, menu bar, etc.) nor WindowPadding. set an axis to 0.0f to leave it automatic. call before Begin() IMGUI_API void SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // set next window collapsed state. call before Begin() IMGUI_API void SetNextWindowFocus(); // set next window to be focused / top-most. call before Begin() - IMGUI_API void SetNextWindowBgAlpha(float alpha); // set next window background color alpha. helper to easily modify ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground. + IMGUI_API void SetNextWindowBgAlpha(float alpha); // set next window background color alpha. helper to easily override the Alpha component of ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground. IMGUI_API void SetWindowPos(const ImVec2& pos, ImGuiCond cond = 0); // (not recommended) set current window position - call within Begin()/End(). prefer using SetNextWindowPos(), as this may incur tearing and side-effects. IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0,0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed(). @@ -328,11 +349,11 @@ namespace ImGui IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied // Parameters stacks (current window) - IMGUI_API void PushItemWidth(float item_width); // set width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side). 0.0f = default to ~2/3 of windows width, + IMGUI_API void PushItemWidth(float item_width); // push width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side). 0.0f = default to ~2/3 of windows width, IMGUI_API void PopItemWidth(); IMGUI_API void SetNextItemWidth(float item_width); // set width of the _next_ common large "item+label" widget. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side) IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position. NOT necessarily the width of last item unlike most 'Item' functions. - IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // word-wrapping for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space + IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // push word-wrapping position for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space IMGUI_API void PopTextWrapPos(); IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets IMGUI_API void PopAllowKeyboardFocus(); @@ -342,6 +363,10 @@ namespace ImGui // Cursor / Layout // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. + // - You can call SameLine() between widgets to undo the last carriage return and output at the right of the preceeding widget. + // - Attention! We currently have inconsistencies between window-local and absolute positions we will aim to fix with future API: + // Window-local coordinates: SameLine(), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), GetContentRegionMax(), GetWindowContentRegion*(), PushTextWrapPos() + // Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f); // call between widgets or groups to layout them horizontally. X position given in window coordinates. IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in an horizontal-layout context. @@ -383,8 +408,8 @@ namespace ImGui IMGUI_API ImGuiID GetID(const void* ptr_id); // Widgets: Text - IMGUI_API void TextUnformatted(const char* text, const char* text_end = NULL); // raw text without formatting. Roughly equivalent to Text("%s", text) but: A) doesn't require null terminated string if 'text_end' is specified, B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. - IMGUI_API void Text(const char* fmt, ...) IM_FMTARGS(1); // simple formatted text + IMGUI_API void TextUnformatted(const char* text, const char* text_end = NULL); // raw text without formatting. Roughly equivalent to Text("%s", text) but: A) doesn't require null terminated string if 'text_end' is specified, B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. + IMGUI_API void Text(const char* fmt, ...) IM_FMTARGS(1); // formatted text IMGUI_API void TextV(const char* fmt, va_list args) IM_FMTLIST(1); IMGUI_API void TextColored(const ImVec4& col, const char* fmt, ...) IM_FMTARGS(2); // shortcut for PushStyleColor(ImGuiCol_Text, col); Text(fmt, ...); PopStyleColor(); IMGUI_API void TextColoredV(const ImVec4& col, const char* fmt, va_list args) IM_FMTLIST(2); @@ -399,6 +424,7 @@ namespace ImGui // Widgets: Main // - Most widgets return true when the value has been changed or when pressed/selected + // - You may also use one of the many IsItemXXX functions (e.g. IsItemActive, IsItemHovered, etc.) to query widget state. IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0,0)); // button IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size); // button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) @@ -413,7 +439,7 @@ namespace ImGui IMGUI_API void Bullet(); // draw a small circle and keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses // Widgets: Combo Box - // - The new BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. + // - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. IMGUI_API bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0); IMGUI_API void EndCombo(); // only call EndCombo() if BeginCombo() returns true! @@ -427,6 +453,7 @@ namespace ImGui // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits. + // - Use v_max = FLT_MAX / INT_MAX etc to avoid clamping to a maximum, same with v_min = -FLT_MAX / INT_MIN to avoid clamping to a minimum. // - Use v_min > v_max to lock edits. IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); // If v_min >= v_max we have no bound IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); @@ -460,7 +487,7 @@ namespace ImGui IMGUI_API bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); // Widgets: Input with Keyboard - // - If you want to use InputText() with a dynamic string type such as std::string or your own, see misc/cpp/imgui_stdlib.h + // - If you want to use InputText() with std::string or any custom dynamic string type, see misc/cpp/imgui_stdlib.h and comments in imgui_demo.cpp. // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0,0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); @@ -509,7 +536,7 @@ namespace ImGui // Widgets: Selectables // - A selectable highlights when hovered, and can display another color when selected. - // - Neighbors selectable extend their highlight bounds in order to leave no gap between them. + // - Neighbors selectable extend their highlight bounds in order to leave no gap between them. This is so a series of selected Selectable appear contiguous. IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool selected" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. @@ -535,16 +562,20 @@ namespace ImGui IMGUI_API void Value(const char* prefix, float v, const char* float_format = NULL); // Widgets: Menus - IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. - IMGUI_API void EndMainMenuBar(); // only call EndMainMenuBar() if BeginMainMenuBar() returns true! + // - Use BeginMenuBar() on a window ImGuiWindowFlags_MenuBar to append to its menu bar. + // - Use BeginMainMenuBar() to create a menu bar at the top of the screen and append to it. + // - Use BeginMenu() to create a menu. You can call BeginMenu() multiple time with the same identifier to append more items to it. IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window). IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true! + IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. + IMGUI_API void EndMainMenuBar(); // only call EndMainMenuBar() if BeginMainMenuBar() returns true! IMGUI_API bool BeginMenu(const char* label, bool enabled = true); // create a sub-menu entry. only call EndMenu() if this returns true! IMGUI_API void EndMenu(); // only call EndMenu() if BeginMenu() returns true! IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true); // return true when activated. shortcuts are displayed for convenience but not processed by ImGui at the moment IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL // Tooltips + // - Tooltip are windows following the mouse which do not take focus away. IMGUI_API void BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). IMGUI_API void EndTooltip(); IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip(). @@ -556,22 +587,25 @@ namespace ImGui // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. // - Their visibility state (~bool) is held internally by imgui instead of being held by the programmer as we are used to with regular Begin() calls. // User can manipulate the visibility state by calling OpenPopup(). - // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. + // - We default to use the right mouse (ImGuiMouseButton_Right=1) for the Popup Context functions. + // (*) You can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). popups are closed when user click outside, or if CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. By default, Selectable()/MenuItem() are calling CloseCurrentPopup(). Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. only call EndPopup() if BeginPopup() returns true! - IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! - IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, int mouse_button = 1, bool also_over_items = true); // helper to open and begin popup when clicked on current window. - IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, int mouse_button = 1); // helper to open and begin popup when clicked in void (where there are no imgui windows). + IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! + IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1, bool also_over_items = true); // helper to open and begin popup when clicked on current window. + IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open and begin popup when clicked in void (where there are no imgui windows). IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // modal dialog (regular window with title bar, block interactions behind the modal window, can't close the modal window by clicking outside) IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! - IMGUI_API bool OpenPopupOnItemClick(const char* str_id = NULL, int mouse_button = 1); // helper to open popup when clicked on last item (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors). return true when just opened. + IMGUI_API bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open popup when clicked on last item (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors). return true when just opened. IMGUI_API bool IsPopupOpen(const char* str_id); // return true if the popup is open at the current begin-ed level of the popup stack. IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup. // Columns // - You can also use SameLine(pos_x) to mimic simplified columns. // - The columns API is work-in-progress and rather lacking (columns are arguably the worst part of dear imgui at the moment!) + // - There is a maximum of 64 columns. + // - Currently working on new 'Tables' api which will replace columns around Q2 2020 (see GitHub #2957). IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished IMGUI_API int GetColumnIndex(); // get current column index @@ -582,7 +616,6 @@ namespace ImGui IMGUI_API int GetColumnsCount(); // Tab Bars, Tabs - // [BETA API] API may evolve! IMGUI_API bool BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0); // create and append into a TabBar IMGUI_API void EndTabBar(); // only call EndTabBar() if BeginTabBar() returns true! IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0);// create a Tab. Returns true if the Tab is selected. @@ -623,12 +656,13 @@ namespace ImGui IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags = 0); // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options. IMGUI_API bool IsItemActive(); // is the last item active? (e.g. button being held, text field being edited. This will continuously return true while holding mouse button on an item. Items that don't interact will always return false) IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation? - IMGUI_API bool IsItemClicked(int mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered() + IMGUI_API bool IsItemClicked(ImGuiMouseButton mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered() IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling) IMGUI_API bool IsItemEdited(); // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the "bool" return value of many widgets. IMGUI_API bool IsItemActivated(); // was the last item just made active (item was previously inactive). IMGUI_API bool IsItemDeactivated(); // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that requires continuous editing. IMGUI_API bool IsItemDeactivatedAfterEdit(); // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that requires continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item). + IMGUI_API bool IsItemToggledOpen(); // was the last item open state toggled? set by TreeNode(). IMGUI_API bool IsAnyItemHovered(); // is any item hovered? IMGUI_API bool IsAnyItemActive(); // is any item active? IMGUI_API bool IsAnyItemFocused(); // is any item focused? @@ -648,38 +682,47 @@ namespace ImGui IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). IMGUI_API void SetStateStorage(ImGuiStorage* storage); // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it) IMGUI_API ImGuiStorage* GetStateStorage(); - IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // calculate coarse clipping for large list of evenly sized items. Prefer using the ImGuiListClipper higher-level helper if you can. IMGUI_API bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags flags = 0); // helper to create a child window / scrolling region that looks like a normal widget frame IMGUI_API void EndChildFrame(); // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window) + // Text Utilities + IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); + // Color Utilities IMGUI_API ImVec4 ColorConvertU32ToFloat4(ImU32 in); IMGUI_API ImU32 ColorConvertFloat4ToU32(const ImVec4& in); IMGUI_API void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v); IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); - // Inputs Utilities + // Inputs Utilities: Keyboard + // - For 'int user_key_index' you can use your own indices/enums according to how your backend/engine stored them in io.KeysDown[]. + // - We don't know the meaning of those value. You can use GetKeyIndex() to map a ImGuiKey_ value into the user index. IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] - IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. note that imgui doesn't know the semantic of each entry of io.KeysDown[]. Use your own indices/enums according to how your backend/engine stored them into io.KeysDown[]! - IMGUI_API bool IsKeyPressed(int user_key_index, bool repeat = true); // was key pressed (went from !Down to Down). if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate - IMGUI_API bool IsKeyReleased(int user_key_index); // was key released (went from Down to !Down).. + IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. + IMGUI_API bool IsKeyPressed(int user_key_index, bool repeat = true); // was key pressed (went from !Down to Down)? if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate + IMGUI_API bool IsKeyReleased(int user_key_index); // was key released (went from Down to !Down)? IMGUI_API int GetKeyPressedAmount(int key_index, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate - IMGUI_API bool IsMouseDown(int button); // is mouse button held (0=left, 1=right, 2=middle) - IMGUI_API bool IsAnyMouseDown(); // is any mouse button held - IMGUI_API bool IsMouseClicked(int button, bool repeat = false); // did mouse button clicked (went from !Down to Down) (0=left, 1=right, 2=middle) - IMGUI_API bool IsMouseDoubleClicked(int button); // did mouse button double-clicked. a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime. - IMGUI_API bool IsMouseReleased(int button); // did mouse button released (went from Down to !Down) - IMGUI_API bool IsMouseDragging(int button = 0, float lock_threshold = -1.0f); // is mouse dragging. if lock_threshold < -1.0f uses io.MouseDraggingThreshold - IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true); // is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. - IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse + IMGUI_API void CaptureKeyboardFromApp(bool want_capture_keyboard_value = true); // attention: misleading name! manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application to handle). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard_value"; after the next NewFrame() call. + + // Inputs Utilities: Mouse + // - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right. + // - You can also use regular integer: it is forever guaranteed that 0=Left, 1=Right, 2=Middle. + // - Dragging operations are only reported after mouse has moved a certain distance away from the initial clicking position (see 'lock_threshold' and 'io.MouseDraggingThreshold') + IMGUI_API bool IsMouseDown(ImGuiMouseButton button); // is mouse button held? + IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, bool repeat = false); // did mouse button clicked? (went from !Down to Down) + IMGUI_API bool IsMouseReleased(ImGuiMouseButton button); // did mouse button released? (went from Down to !Down) + IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button); // did mouse button double-clicked? a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime. + IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);// is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. + IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse available + IMGUI_API bool IsAnyMouseDown(); // is any mouse button held? IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls - IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve backup of mouse position at the time of opening popup we have BeginPopup() into - IMGUI_API ImVec2 GetMouseDragDelta(int button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once. If lock_threshold < -1.0f uses io.MouseDraggingThreshold. - IMGUI_API void ResetMouseDragDelta(int button = 0); // + IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve mouse position at the time of opening popup we have BeginPopup() into (helper to avoid user backing that value themselves) + IMGUI_API bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f); // is mouse dragging? (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) + IMGUI_API ImVec2 GetMouseDragDelta(ImGuiMouseButton button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) + IMGUI_API void ResetMouseDragDelta(ImGuiMouseButton button = 0); // IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired cursor type, reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you - IMGUI_API void SetMouseCursor(ImGuiMouseCursor type); // set desired cursor type - IMGUI_API void CaptureKeyboardFromApp(bool want_capture_keyboard_value = true); // attention: misleading name! manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application to handle). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard_value"; after the next NewFrame() call. + IMGUI_API void SetMouseCursor(ImGuiMouseCursor cursor_type); // set desired cursor type IMGUI_API void CaptureMouseFromApp(bool want_capture_mouse_value = true); // attention: misleading name! manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application to handle). This is equivalent to setting "io.WantCaptureMouse = want_capture_mouse_value;" after the next NewFrame() call. // Clipboard Utilities (also see the LogToClipboard() function to capture or output text data to the clipboard) @@ -744,8 +787,8 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_ChildMenu = 1 << 28 // Don't use! For internal use by BeginMenu() // [Obsolete] - //ImGuiWindowFlags_ShowBorders = 1 << 7, // --> Set style.FrameBorderSize=1.0f / style.WindowBorderSize=1.0f to enable borders around windows and items - //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) + //ImGuiWindowFlags_ShowBorders = 1 << 7, // --> Set style.FrameBorderSize=1.0f or style.WindowBorderSize=1.0f to enable borders around items or windows. + //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges=true and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) }; // Flags for ImGui::InputText() @@ -796,11 +839,6 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 14, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog - - // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiTreeNodeFlags_AllowOverlapMode = ImGuiTreeNodeFlags_AllowItemOverlap // [renamed in 1.53] -#endif }; // Flags for ImGui::Selectable() @@ -950,12 +988,12 @@ enum ImGuiKey_ ImGuiKey_Enter, ImGuiKey_Escape, ImGuiKey_KeyPadEnter, - ImGuiKey_A, // for text edit CTRL+A: select all - ImGuiKey_C, // for text edit CTRL+C: copy - ImGuiKey_V, // for text edit CTRL+V: paste - ImGuiKey_X, // for text edit CTRL+X: cut - ImGuiKey_Y, // for text edit CTRL+Y: redo - ImGuiKey_Z, // for text edit CTRL+Z: undo + ImGuiKey_A, // for text edit CTRL+A: select all + ImGuiKey_C, // for text edit CTRL+C: copy + ImGuiKey_V, // for text edit CTRL+V: paste + ImGuiKey_X, // for text edit CTRL+X: cut + ImGuiKey_Y, // for text edit CTRL+Y: redo + ImGuiKey_Z, // for text edit CTRL+Z: undo ImGuiKey_COUNT }; @@ -1017,7 +1055,7 @@ enum ImGuiBackendFlags_ ImGuiBackendFlags_HasGamepad = 1 << 0, // Back-end Platform supports gamepad and currently has one connected. ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Back-end Platform supports honoring GetMouseCursor() value to change the OS cursor shape. ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Back-end Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). - ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Back-end Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bits indices. + ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Back-end Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. }; // Enumeration for PushStyleColor() / PopStyleColor() @@ -1076,9 +1114,7 @@ enum ImGuiCol_ // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg // [renamed in 1.63] - , ImGuiCol_ChildWindowBg = ImGuiCol_ChildBg // [renamed in 1.53] - //ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered, // [unused since 1.60+] the close button now uses regular button colors. - //ImGuiCol_ComboBg, // [unused since 1.53+] ComboBg has been merged with PopupBg, so a redirect isn't accurate. + //, ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered// [unused since 1.60+] the close button now uses regular button colors. #endif }; @@ -1115,8 +1151,7 @@ enum ImGuiStyleVar_ // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT // [renamed in 1.60] - , ImGuiStyleVar_ChildWindowRounding = ImGuiStyleVar_ChildRounding // [renamed in 1.53] + , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT // [renamed in 1.60] #endif }; @@ -1133,6 +1168,7 @@ enum ImGuiColorEditFlags_ ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker). ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small colored square preview instead. ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source. + ImGuiColorEditFlags_NoBorder = 1 << 10, // // ColorButton: disable border (which is enforced by default) // User Options (right-click on widget to change some of them). ImGuiColorEditFlags_AlphaBar = 1 << 16, // // ColorEdit, ColorPicker: show vertical alpha bar/gradient in picker. @@ -1165,6 +1201,16 @@ enum ImGuiColorEditFlags_ #endif }; +// Identify a mouse button. +// Those values are guaranteed to be stable and we frequently use 0/1 directly. Named enums provided for convenience. +enum ImGuiMouseButton_ +{ + ImGuiMouseButton_Left = 0, + ImGuiMouseButton_Right = 1, + ImGuiMouseButton_Middle = 2, + ImGuiMouseButton_COUNT = 5 +}; + // Enumeration for GetMouseCursor() // User code may request binding to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here enum ImGuiMouseCursor_ @@ -1178,6 +1224,7 @@ enum ImGuiMouseCursor_ ImGuiMouseCursor_ResizeNESW, // When hovering over the bottom-left corner of a window ImGuiMouseCursor_ResizeNWSE, // When hovering over the bottom-right corner of a window ImGuiMouseCursor_Hand, // (Unused by Dear ImGui functions. Use for e.g. hyperlinks) + ImGuiMouseCursor_NotAllowed, // When hovering something with disallowed interaction. Usually a crossed circle. ImGuiMouseCursor_COUNT // Obsolete names (will be removed) @@ -1186,7 +1233,7 @@ enum ImGuiMouseCursor_ #endif }; -// Enumateration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions +// Enumeration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions // Represent a condition. // Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always. enum ImGuiCond_ @@ -1216,10 +1263,12 @@ template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p //----------------------------------------------------------------------------- // Helper: ImVector<> // Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug). -// You generally do NOT need to care or use this ever. But we need to make it available in imgui.h because some of our data structures are relying on it. -// Important: clear() frees memory, resize(0) keep the allocated buffer. We use resize(0) a lot to intentionally recycle allocated buffers across frames and amortize our costs. -// Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that, -// do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset. +//----------------------------------------------------------------------------- +// - You generally do NOT need to care or use this ever. But we need to make it available in imgui.h because some of our public structures are relying on it. +// - We use std-like naming convention here, which is a little unusual for this codebase. +// - Important: clear() frees memory, resize(0) keep the allocated buffer. We use resize(0) a lot to intentionally recycle allocated buffers across frames and amortize our costs. +// - Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that, +// Do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset. //----------------------------------------------------------------------------- template @@ -1261,6 +1310,7 @@ struct ImVector inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } + inline void shrink(int new_size) { IM_ASSERT(new_size <= Size); Size = new_size; } // Resize a vector to a smaller size, guaranteed not to cause a reallocation inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. @@ -1322,6 +1372,7 @@ struct ImGuiStyle bool AntiAliasedLines; // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU. bool AntiAliasedFill; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. + float CircleSegmentMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. ImVec4 Colors[ImGuiCol_COUNT]; IMGUI_API ImGuiStyle(); @@ -1377,9 +1428,9 @@ struct ImGuiIO // Optional: Platform/Renderer back-end name (informational only! will be displayed in About Window) + User data for back-end/wrappers to store their own stuff. const char* BackendPlatformName; // = NULL const char* BackendRendererName; // = NULL - void* BackendPlatformUserData; // = NULL - void* BackendRendererUserData; // = NULL - void* BackendLanguageUserData; // = NULL + void* BackendPlatformUserData; // = NULL // User data for platform back-end + void* BackendRendererUserData; // = NULL // User data for renderer back-end + void* BackendLanguageUserData; // = NULL // User data for non C++ programming language back-end // Optional: Access OS clipboard // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) @@ -1418,6 +1469,7 @@ struct ImGuiIO // Functions IMGUI_API void AddInputCharacter(unsigned int c); // Queue new character input + IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue new character input from an UTF-16 character, it can be a surrogate IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually @@ -1441,7 +1493,7 @@ struct ImGuiIO ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. //------------------------------------------------------------------ - // [Internal] ImGui will maintain those fields. Forward compatibility not guaranteed! + // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) @@ -1460,6 +1512,7 @@ struct ImGuiIO float KeysDownDurationPrev[512]; // Previous duration the key has been down float NavInputsDownDuration[ImGuiNavInput_COUNT]; float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; + ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16 ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper. IMGUI_API ImGuiIO(); @@ -1565,17 +1618,6 @@ namespace ImGui static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } static inline ImVec2 CalcItemRectClosestPoint(const ImVec2& pos, bool on_edge = false, float outward = 0.f) { IM_UNUSED(on_edge); IM_UNUSED(outward); IM_ASSERT(0); return pos; } - // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) - static inline void ShowTestWindow() { return ShowDemoWindow(); } - static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } - static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } - static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } - static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } - // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) - IMGUI_API bool Begin(const char* name, bool* p_open, const ImVec2& size_on_first_use, float bg_alpha_override = -1.0f, ImGuiWindowFlags flags = 0); // Use SetNextWindowSize(size, ImGuiCond_FirstUseEver) + SetNextWindowBgAlpha() instead. - static inline bool IsRootWindowOrAnyChildHovered() { return IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); } - static inline void AlignFirstTextHeightToWidgets() { AlignTextToFramePadding(); } - static inline void SetNextWindowPosCenter(ImGuiCond c=0) { ImGuiIO& io = GetIO(); SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f), c, ImVec2(0.5f, 0.5f)); } } typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETED in 1.63 (from Aug 2018): made the names consistent typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; @@ -1628,11 +1670,11 @@ struct ImGuiTextBuffer IMGUI_API static char EmptyString[1]; ImGuiTextBuffer() { } - inline char operator[](int i) { IM_ASSERT(Buf.Data != NULL); return Buf.Data[i]; } + inline char operator[](int i) const { IM_ASSERT(Buf.Data != NULL); return Buf.Data[i]; } const char* begin() const { return Buf.Data ? &Buf.front() : EmptyString; } const char* end() const { return Buf.Data ? &Buf.back() : EmptyString; } // Buf is zero-terminated, so end() will point on the zero-terminator int size() const { return Buf.Size ? Buf.Size - 1 : 0; } - bool empty() { return Buf.Size <= 1; } + bool empty() const { return Buf.Size <= 1; } void clear() { Buf.clear(); } void reserve(int capacity) { Buf.reserve(capacity); } const char* c_str() const { return Buf.Data ? Buf.Data : EmptyString; } @@ -1707,9 +1749,13 @@ struct ImGuiStorage // - Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. struct ImGuiListClipper { - float StartPosY; + int DisplayStart, DisplayEnd; + int ItemsCount; + + // [Internal] + int StepNo; float ItemsHeight; - int ItemsCount, StepNo, DisplayStart, DisplayEnd; + float StartPosY; // items_count: Use -1 to ignore (you can call Begin later). Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step). // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). @@ -1722,7 +1768,7 @@ struct ImGuiListClipper IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. }; -// Helpers macros to generate 32-bits encoded colors +// Helpers macros to generate 32-bit encoded colors #ifdef IMGUI_USE_BGRA_PACKED_COLOR #define IM_COL32_R_SHIFT 16 #define IM_COL32_G_SHIFT 8 @@ -1786,13 +1832,13 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c // Typically, 1 command = 1 GPU draw call (unless command is a callback) // Pre 1.71 back-ends will typically ignore the VtxOffset/IdxOffset fields. When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' -// is enabled, those fields allow us to render meshes larger than 64K vertices while keeping 16-bits indices. +// is enabled, those fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. struct ImDrawCmd { unsigned int ElemCount; // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. ImVec4 ClipRect; // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates ImTextureID TextureId; // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. - unsigned int VtxOffset; // Start offset in vertex buffer. Pre-1.71 or without ImGuiBackendFlags_RendererHasVtxOffset: always 0. With ImGuiBackendFlags_RendererHasVtxOffset: may be >0 to support meshes larger than 64K vertices with 16-bits indices. + unsigned int VtxOffset; // Start offset in vertex buffer. Pre-1.71 or without ImGuiBackendFlags_RendererHasVtxOffset: always 0. With ImGuiBackendFlags_RendererHasVtxOffset: may be >0 to support meshes larger than 64K vertices with 16-bit indices. unsigned int IdxOffset; // Start offset in index buffer. Always equal to sum of ElemCount drawn so far. ImDrawCallback UserCallback; // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. void* UserCallbackData; // The draw callback code can access this. @@ -1801,8 +1847,8 @@ struct ImDrawCmd }; // Vertex index -// (to allow large meshes with 16-bits indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end) -// (to use 32-bits indices: override with '#define ImDrawIdx unsigned int' in imconfig.h) +// (to allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end) +// (to use 32-bit indices: override with '#define ImDrawIdx unsigned int' in imconfig.h) #ifndef ImDrawIdx typedef unsigned short ImDrawIdx; #endif @@ -1910,8 +1956,11 @@ struct ImDrawList // Primitives // - For rectangular primitives, "p_min" and "p_max" represent the upper-left and lower-right corners. + // - For circle primitives, use "num_segments == 0" to automatically calculate tessellation (preferred). + // In future versions we will use textures to provide cheaper and higher-quality circles. + // Use AddNgon() and AddNgonFilled() functions if you need to guaranteed a specific number of sides. IMGUI_API void AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness = 1.0f); - IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size), rounding_corners_flags: 4-bits corresponding to which corner to round + IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size), rounding_corners_flags: 4 bits corresponding to which corner to round IMGUI_API void AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); // a: upper-left, b: lower-right (== upper-left + size) IMGUI_API void AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left); IMGUI_API void AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness = 1.0f); @@ -1920,11 +1969,13 @@ struct ImDrawList IMGUI_API void AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col); IMGUI_API void AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments = 12, float thickness = 1.0f); IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 12); + IMGUI_API void AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness = 1.0f); + IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness); IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. - IMGUI_API void AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments = 0); + IMGUI_API void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Image primitives // - Read FAQ to understand what ImTextureID is. @@ -1942,7 +1993,7 @@ struct ImDrawList inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 10); IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle - IMGUI_API void PathBezierCurveTo(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, int num_segments = 0); + IMGUI_API void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); // Advanced @@ -1951,8 +2002,11 @@ struct ImDrawList IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer. // Advanced: Channels - // - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit foreground primitives before background primitives) - // - Use to minimize draw calls (e.g. if going back-and-forth between multiple non-overlapping clipping rectangles, prefer to append into separate channels then merge at the end) + // - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit FG primitives before BG primitives) + // - Use to minimize draw calls (e.g. if going back-and-forth between multiple clipping rectangles, prefer to append into separate channels then merge at the end) + // - FIXME-OBSOLETE: This API shouldn't have been in ImDrawList in the first place! + // Prefer using your own persistent copy of ImDrawListSplitter as you can stack them. + // Using the ImDrawList::ChannelsXXXX you cannot stack a split over another. inline void ChannelsSplit(int count) { _Splitter.Split(this, count); } inline void ChannelsMerge() { _Splitter.Merge(this); } inline void ChannelsSetCurrent(int n) { _Splitter.SetCurrentChannel(this, n); } @@ -1962,6 +2016,7 @@ struct ImDrawList IMGUI_API void Clear(); IMGUI_API void ClearFreeMemory(); IMGUI_API void PrimReserve(int idx_count, int vtx_count); + IMGUI_API void PrimUnreserve(int idx_count, int vtx_count); IMGUI_API void PrimRect(const ImVec2& a, const ImVec2& b, ImU32 col); // Axis aligned rectangle (composed of two triangles) IMGUI_API void PrimRectUV(const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col); IMGUI_API void PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col); @@ -2025,9 +2080,12 @@ struct ImFontConfig IMGUI_API ImFontConfig(); }; +// Hold rendering data for one glyph. +// (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this) struct ImFontGlyph { - ImWchar Codepoint; // 0x0000..0xFFFF + unsigned int Codepoint : 31; // 0x0000..0xFFFF + unsigned int Visible : 1; // Flag to allow early out when rendering float AdvanceX; // Distance to next character (= data from font + ImFontConfig::GlyphExtraSpacing.x baked in) float X0, Y0, X1, Y1; // Glyph corners float U0, V0, U1, V1; // Texture coordinates @@ -2039,11 +2097,11 @@ struct ImFontGlyphRangesBuilder { ImVector UsedChars; // Store 1-bit per Unicode code point (0=unused, 1=used) - ImFontGlyphRangesBuilder() { Clear(); } - inline void Clear() { int size_in_bytes = 0x10000 / 8; UsedChars.resize(size_in_bytes / (int)sizeof(ImU32)); memset(UsedChars.Data, 0, (size_t)size_in_bytes); } - inline bool GetBit(int n) const { int off = (n >> 5); ImU32 mask = 1u << (n & 31); return (UsedChars[off] & mask) != 0; } // Get bit n in the array - inline void SetBit(int n) { int off = (n >> 5); ImU32 mask = 1u << (n & 31); UsedChars[off] |= mask; } // Set bit n in the array - inline void AddChar(ImWchar c) { SetBit(c); } // Add character + ImFontGlyphRangesBuilder() { Clear(); } + inline void Clear() { int size_in_bytes = (IM_UNICODE_CODEPOINT_MAX + 1) / 8; UsedChars.resize(size_in_bytes / (int)sizeof(ImU32)); memset(UsedChars.Data, 0, (size_t)size_in_bytes); } + inline bool GetBit(size_t n) const { int off = (int)(n >> 5); ImU32 mask = 1u << (n & 31); return (UsedChars[off] & mask) != 0; } // Get bit n in the array + inline void SetBit(size_t n) { int off = (int)(n >> 5); ImU32 mask = 1u << (n & 31); UsedChars[off] |= mask; } // Set bit n in the array + inline void AddChar(ImWchar c) { SetBit(c); } // Add character IMGUI_API void AddText(const char* text, const char* text_end = NULL); // Add string (each character of the UTF-8 string are added) IMGUI_API void AddRanges(const ImWchar* ranges); // Add ranges, e.g. builder.AddRanges(ImFontAtlas::GetGlyphRangesDefault()) to force add all of ASCII/Latin+Ext IMGUI_API void BuildRanges(ImVector* out_ranges); // Output new ranges @@ -2052,12 +2110,12 @@ struct ImFontGlyphRangesBuilder // See ImFontAtlas::AddCustomRectXXX functions. struct ImFontAtlasCustomRect { - unsigned int ID; // Input // User ID. Use <0x10000 to map into a font glyph, >=0x10000 for other/internal/custom texture data. + unsigned int ID; // Input // User ID. Use < 0x110000 to map into a font glyph, >= 0x110000 for other/internal/custom texture data. unsigned short Width, Height; // Input // Desired rectangle dimension unsigned short X, Y; // Output // Packed position in Atlas - float GlyphAdvanceX; // Input // For custom font glyphs only (ID<0x10000): glyph xadvance - ImVec2 GlyphOffset; // Input // For custom font glyphs only (ID<0x10000): glyph display offset - ImFont* Font; // Input // For custom font glyphs only (ID<0x10000): target font + float GlyphAdvanceX; // Input // For custom font glyphs only (ID < 0x110000): glyph xadvance + ImVec2 GlyphOffset; // Input // For custom font glyphs only (ID < 0x110000): glyph display offset + ImFont* Font; // Input // For custom font glyphs only (ID < 0x110000): target font ImFontAtlasCustomRect() { ID = 0xFFFFFFFF; Width = Height = 0; X = Y = 0xFFFF; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0,0); Font = NULL; } bool IsPacked() const { return X != 0xFFFF; } }; @@ -2109,7 +2167,7 @@ struct ImFontAtlas IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions. IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel - bool IsBuilt() { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); } + bool IsBuilt() const { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); } void SetTexID(ImTextureID id) { TexID = id; } //------------------------------------------- @@ -2136,13 +2194,13 @@ struct ImFontAtlas // After calling Build(), you can query the rectangle position and render your pixels. // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), // so you can render e.g. custom colorful icons and use them as regular glyphs. - // Read misc/fonts/README.txt for more details about using colorful icons. - IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x10000. Id >= 0x80000000 are reserved for ImGui and ImDrawList - IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x10000 to register a rectangle to map into a specific font. + // Read docs/FONTS.txt for more details about using colorful icons. + IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x110000. Id >= 0x80000000 are reserved for ImGui and ImDrawList + IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x110000 to register a rectangle to map into a specific font. const ImFontAtlasCustomRect*GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } // [Internal] - IMGUI_API void CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max); + IMGUI_API void CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const; IMGUI_API bool GetMouseCursorTexData(ImGuiMouseCursor cursor, ImVec2* out_offset, ImVec2* out_size, ImVec2 out_uv_border[2], ImVec2 out_uv_fill[2]); //------------------------------------------- @@ -2195,10 +2253,11 @@ struct ImFont short ConfigDataCount; // 2 // in // ~ 1 // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont. ImWchar FallbackChar; // 2 // in // = '?' // Replacement character if a glyph isn't found. Only set via SetFallbackChar() ImWchar EllipsisChar; // 2 // out // = -1 // Character used for ellipsis rendering. + bool DirtyLookupTables; // 1 // out // float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] int MetricsTotalSurface;// 4 // out // // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs) - bool DirtyLookupTables; // 1 // out // + ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations accross all used codepoints. // Methods IMGUI_API ImFont(); @@ -2222,11 +2281,9 @@ struct ImFont IMGUI_API void GrowIndex(int new_size); IMGUI_API void AddGlyph(ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x); IMGUI_API void AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built. + IMGUI_API void SetGlyphVisible(ImWchar c, bool visible); IMGUI_API void SetFallbackChar(ImWchar c); - -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - typedef ImFontGlyph Glyph; // OBSOLETED in 1.52+ -#endif + IMGUI_API bool IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last); }; #if defined(__clang__) @@ -2239,3 +2296,5 @@ struct ImFont #ifdef IMGUI_INCLUDE_IMGUI_USER_H #include "imgui_user.h" #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 0eeec980..2664cb14 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,17 +1,24 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (demo code) +// Help: +// - Read FAQ at http://dearimgui.org/faq +// - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. +// Read imgui.cpp for more details, documentation and comments. +// Get latest version at https://github.com/ocornut/imgui + // Message to the person tempted to delete this file when integrating Dear ImGui into their code base: // Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other coders // will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of // your game/app! Removing this file from your project is hindering access to documentation for everyone in your team, // likely leading you to poorer usage of the library. // Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow(). -// If you want to link core Dear ImGui in your shipped builds but want an easy guarantee that the demo will not be linked, +// If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be linked, // you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. // In other situation, whenever you have Dear ImGui available you probably want this to be available for reference. // Thank you, -// -Your beloved friend, imgui_demo.cpp (that you won't delete) +// -Your beloved friend, imgui_demo.cpp (which you won't delete) // Message to beginner C/C++ programmers about the meaning of the 'static' keyword: // In this demo code, we frequently we use 'static' variables inside functions. A static variable persist across calls, so it is @@ -21,13 +28,13 @@ // reentrant or used in multiple threads. This might be a pattern you will want to use in your code, but most of the real data // you would be editing is likely going to be stored outside your functions. -// The Demo code is this file is designed to be easy to copy-and-paste in into your application! +// The Demo code in this file is designed to be easy to copy-and-paste in into your application! // Because of this: // - We never omit the ImGui:: namespace when calling functions, even though most of our code is already in the same namespace. // - We try to declare static variables in the local scope, as close as possible to the code using them. -// - We never use any of the helpers/facilities used internally by dear imgui, unless it has been exposed in the public API (imgui.h). -// - We never use maths operators on ImVec2/ImVec4. For other imgui sources files, they are provided by imgui_internal.h w/ IMGUI_DEFINE_MATH_OPERATORS, -// for your own sources file they are optional and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. +// - We never use any of the helpers/facilities used internally by Dear ImGui, unless it has been exposed in the public API (imgui.h). +// - We never use maths operators on ImVec2/ImVec4. For other of our sources files, they are provided by imgui_internal.h w/ IMGUI_DEFINE_MATH_OPERATORS. +// For your own sources file they are optional and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. // Because we don't want to assume anything about your support of maths operators, we don't use them in imgui_demo.cpp. /* @@ -58,6 +65,8 @@ Index of this file: #endif #include "imgui.h" +#ifndef IMGUI_DISABLE + #include // toupper #include // INT_MIN, INT_MAX #include // sqrtf, powf, cosf, sinf, floorf, ceilf @@ -97,23 +106,24 @@ Index of this file: #pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. #endif -// Play it nice with Windows users. Notepad in 2017 still doesn't display text data with Unix-style \n. +// Play it nice with Windows users (Update: since 2018-05, Notepad finally appears to support Unix-style carriage returns!) #ifdef _WIN32 #define IM_NEWLINE "\r\n" -#define snprintf _snprintf -#define vsnprintf _vsnprintf #else #define IM_NEWLINE "\n" #endif +#if defined(_MSC_VER) && !defined(snprintf) +#define snprintf _snprintf +#endif +#if defined(_MSC_VER) && !defined(vsnprintf) +#define vsnprintf _vsnprintf +#endif + //----------------------------------------------------------------------------- // [SECTION] Forward Declarations, Helpers //----------------------------------------------------------------------------- -#if !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && defined(IMGUI_DISABLE_TEST_WINDOWS) && !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Obsolete name since 1.53, TEST->DEMO -#define IMGUI_DISABLE_DEMO_WINDOWS -#endif - #if !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Forward Declarations @@ -132,7 +142,7 @@ static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleMenuFile(); // Helper to display a little (?) mark which shows a tooltip when hovered. -// In your own code you may want to display an actual icon if you are using a merged icon fonts (see misc/fonts/README.txt) +// In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.txt) static void HelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); @@ -152,25 +162,28 @@ void ImGui::ShowUserGuide() ImGuiIO& io = ImGui::GetIO(); ImGui::BulletText("Double-click on title bar to collapse window."); ImGui::BulletText("Click and drag on lower corner to resize window\n(double-click to auto fit window to its contents)."); - if (io.ConfigWindowsMoveFromTitleBarOnly) - ImGui::BulletText("Click and drag on title bar to move window."); - else - ImGui::BulletText("Click and drag on any empty space to move window."); - ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); + ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); if (io.FontAllowUserScaling) ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents."); - ImGui::BulletText("Mouse Wheel to scroll."); - ImGui::BulletText("While editing text:\n"); + ImGui::BulletText("While inputing text:\n"); ImGui::Indent(); - ImGui::BulletText("Hold SHIFT or use mouse to select text."); ImGui::BulletText("CTRL+Left/Right to word jump."); ImGui::BulletText("CTRL+A or double-click to select all."); - ImGui::BulletText("CTRL+X,CTRL+C,CTRL+V to use clipboard."); + ImGui::BulletText("CTRL+X/C/V to use clipboard cut/copy/paste."); ImGui::BulletText("CTRL+Z,CTRL+Y to undo/redo."); ImGui::BulletText("ESCAPE to revert."); ImGui::BulletText("You can apply arithmetic operators +,*,/ on numerical values.\nUse +- to subtract."); ImGui::Unindent(); + ImGui::BulletText("With keyboard navigation enabled:"); + ImGui::Indent(); + ImGui::BulletText("Arrow keys to navigate."); + ImGui::BulletText("Space to activate a widget."); + ImGui::BulletText("Return to input text into a widget."); + ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window."); + ImGui::BulletText("Alt to jump to the menu layer of a window."); + ImGui::BulletText("CTRL+Tab to select a window."); + ImGui::Unindent(); } //----------------------------------------------------------------------------- @@ -311,12 +324,20 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::CollapsingHeader("Help")) { + ImGui::Text("ABOUT THIS DEMO:"); + ImGui::BulletText("Sections below are demonstrating many aspects of the library."); + ImGui::BulletText("The \"Examples\" menu above leads to more demo contents."); + ImGui::BulletText("The \"Tools\" menu above gives access to: About Box, Style Editor,\n" + "and Metrics (general purpose Dear ImGui debugging tool)."); + ImGui::Separator(); + ImGui::Text("PROGRAMMER GUIDE:"); - ImGui::BulletText("Please see the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); - ImGui::BulletText("Please see the comments in imgui.cpp."); - ImGui::BulletText("Please see the examples/ application."); - ImGui::BulletText("Enable 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); - ImGui::BulletText("Enable 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); + ImGui::BulletText("See the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); + ImGui::BulletText("See comments in imgui.cpp."); + ImGui::BulletText("See example applications in the examples/ folder."); + ImGui::BulletText("Read the FAQ at http://www.dearimgui.org/faq/"); + ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); + ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); ImGui::Separator(); ImGui::Text("USER GUIDE:"); @@ -360,7 +381,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Backend Flags")) { - HelpMarker("Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities."); + HelpMarker("Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities.\nHere we expose then as read-only fields to avoid breaking interactions with your back-end."); ImGuiBackendFlags backend_flags = io.BackendFlags; // Make a local copy to avoid modifying actual back-end flags. ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasGamepad); ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasMouseCursors); @@ -372,6 +393,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Style")) { + HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function."); ImGui::ShowStyleEditor(); ImGui::TreePop(); ImGui::Separator(); @@ -501,6 +523,8 @@ static void ShowDemoWindowWidgets() } { + // To wire InputText() with std::string or any other custom string type, + // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. static char str0[128] = "Hello, world!"; ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0)); ImGui::SameLine(); HelpMarker("USER:\nHold SHIFT or use mouse to select text.\n" "CTRL+Left/Right to word jump.\n" "CTRL+A or double-click to select all.\n" "CTRL+X,CTRL+C,CTRL+V clipboard.\n" "CTRL+Z,CTRL+Y undo/redo.\n" "ESCAPE to revert.\n\nPROGRAMMER:\nYou can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated in imgui_demo.cpp)."); @@ -605,7 +629,7 @@ static void ShowDemoWindowWidgets() { ImGui::Text("blah blah"); ImGui::SameLine(); - if (ImGui::SmallButton("button")) {}; + if (ImGui::SmallButton("button")) {} ImGui::TreePop(); } } @@ -753,14 +777,14 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("UTF-8 Text")) { // UTF-8 test with Japanese characters - // (Needs a suitable font, try Noto, or Arial Unicode, or M+ fonts. Read misc/fonts/README.txt for details.) + // (Needs a suitable font, try Noto, or Arial Unicode, or M+ fonts. Read docs/FONTS.txt for details.) // - From C++11 you can use the u8"my text" syntax to encode literal strings as UTF-8 // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. Visual Studio save your file as 'UTF-8 without signature') // - FOR THIS DEMO FILE ONLY, BECAUSE WE WANT TO SUPPORT OLD COMPILERS, WE ARE *NOT* INCLUDING RAW UTF-8 CHARACTERS IN THIS SOURCE FILE. // Instead we are encoding a few strings with hexadecimal constants. Don't do this in your application! // Please use u8"text in any language" in your application! // Note that characters values are preserved even by InputText() if the font cannot be displayed, so you can safely copy & paste garbled characters into another application. - ImGui::TextWrapped("CJK text will only appears if the font was loaded with the appropriate CJK character ranges. Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. Read misc/fonts/README.txt for details."); + ImGui::TextWrapped("CJK text will only appears if the font was loaded with the appropriate CJK character ranges. Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. Read docs/FONTS.txt for details."); ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; @@ -923,7 +947,7 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("In columns")) { ImGui::Columns(3, NULL, false); - static bool selected[16] = { 0 }; + static bool selected[16] = {}; for (int i = 0; i < 16; i++) { char label[32]; sprintf(label, "Item %d", i); @@ -976,6 +1000,8 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + // To wire InputText() with std::string or any other custom string type, + // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. if (ImGui::TreeNode("Text Input")) { if (ImGui::TreeNode("Multi-line Text Input")) @@ -995,7 +1021,7 @@ static void ShowDemoWindowWidgets() "\tlock cmpxchg8b eax\n"; static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput; - HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp)"); + HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include in here)"); ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", (unsigned int*)&flags, ImGuiInputTextFlags_ReadOnly); ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", (unsigned int*)&flags, ImGuiInputTextFlags_AllowTabInput); ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", (unsigned int*)&flags, ImGuiInputTextFlags_CtrlEnterForNewLine); @@ -1014,18 +1040,19 @@ static void ShowDemoWindowWidgets() static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); ImGui::Text("Password input"); - static char bufpass[64] = "password123"; - ImGui::InputText("password", bufpass, 64, ImGuiInputTextFlags_Password | ImGuiInputTextFlags_CharsNoBlank); + static char password[64] = "password123"; + ImGui::InputText("password", password, IM_ARRAYSIZE(password), ImGuiInputTextFlags_Password); ImGui::SameLine(); HelpMarker("Display all characters as '*'.\nDisable clipboard cut and copy.\nDisable logging.\n"); - ImGui::InputTextWithHint("password (w/ hint)", "", bufpass, 64, ImGuiInputTextFlags_Password | ImGuiInputTextFlags_CharsNoBlank); - ImGui::InputText("password (clear)", bufpass, 64, ImGuiInputTextFlags_CharsNoBlank); + ImGui::InputTextWithHint("password (w/ hint)", "", password, IM_ARRAYSIZE(password), ImGuiInputTextFlags_Password); + ImGui::InputText("password (clear)", password, IM_ARRAYSIZE(password)); ImGui::TreePop(); } if (ImGui::TreeNode("Resize Callback")) { - // If you have a custom string type you would typically create a ImGui::InputText() wrapper than takes your type as input. - // See misc/cpp/imgui_stdlib.h and .cpp for an implementation of this using std::string. + // To wire InputText() with std::string or any other custom string type, + // you can use the ImGuiInputTextFlags_CallbackResize flag + create a custom ImGui::InputText() wrapper using your prefered type. + // See misc/cpp/imgui_stdlib.h for an implementation of this using std::string. HelpMarker("Demonstrate using ImGuiInputTextFlags_CallbackResize to wire your resizable string type to InputText().\n\nSee misc/cpp/imgui_stdlib.h for an implementation of this for std::string."); struct Funcs { @@ -1075,7 +1102,7 @@ static void ShowDemoWindowWidgets() // Create a dummy array of contiguous float values to plot // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float and the sizeof() of your structure in the Stride parameter. - static float values[90] = { 0 }; + static float values[90] = {}; static int values_offset = 0; static double refresh_time = 0.0; if (!animate || refresh_time == 0.0) @@ -1176,7 +1203,7 @@ static void ShowDemoWindowWidgets() // Generate a dummy default palette. The palette will persist and can be edited. static bool saved_palette_init = true; - static ImVec4 saved_palette[32] = { }; + static ImVec4 saved_palette[32] = {}; if (saved_palette_init) { for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) @@ -1237,7 +1264,9 @@ static void ShowDemoWindowWidgets() } ImGui::Text("Color button only:"); - ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags, ImVec2(80,80)); + static bool no_border = false; + ImGui::Checkbox("ImGuiColorEditFlags_NoBorder", &no_border); + ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80,80)); ImGui::Text("Color picker:"); static bool alpha = true; @@ -1608,7 +1637,7 @@ static void ShowDemoWindowWidgets() { // Submit an item (various types available) so we can query their status in the following block. static int item_type = 1; - ImGui::Combo("Item Type", &item_type, "Text\0Button\0Button (w/ repeat)\0Checkbox\0SliderFloat\0InputText\0InputFloat\0InputFloat3\0ColorEdit4\0MenuItem\0TreeNode (w/ double-click)\0ListBox\0"); + ImGui::Combo("Item Type", &item_type, "Text\0Button\0Button (w/ repeat)\0Checkbox\0SliderFloat\0InputText\0InputFloat\0InputFloat3\0ColorEdit4\0MenuItem\0TreeNode\0TreeNode (w/ double-click)\0ListBox\0", 20); ImGui::SameLine(); HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions."); bool ret = false; @@ -1625,8 +1654,9 @@ static void ShowDemoWindowWidgets() if (item_type == 7) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) if (item_type == 8) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) if (item_type == 9) { ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) - if (item_type == 10){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. - if (item_type == 11){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + if (item_type == 10){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node + if (item_type == 11){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. + if (item_type == 12){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } // Display the value of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. @@ -1647,6 +1677,7 @@ static void ShowDemoWindowWidgets() "IsItemDeactivatedAfterEdit() = %d\n" "IsItemVisible() = %d\n" "IsItemClicked() = %d\n" + "IsItemToggledOpen() = %d\n" "GetItemRectMin() = (%.1f, %.1f)\n" "GetItemRectMax() = (%.1f, %.1f)\n" "GetItemRectSize() = (%.1f, %.1f)", @@ -1664,6 +1695,7 @@ static void ShowDemoWindowWidgets() ImGui::IsItemDeactivatedAfterEdit(), ImGui::IsItemVisible(), ImGui::IsItemClicked(), + ImGui::IsItemToggledOpen(), ImGui::GetItemRectMin().x, ImGui::GetItemRectMin().y, ImGui::GetItemRectMax().x, ImGui::GetItemRectMax().y, ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y @@ -1762,7 +1794,7 @@ static void ShowDemoWindowLayout() // Child 1: no border, enable horizontal scrollbar { ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar | (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0); - ImGui::BeginChild("Child1", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); + ImGui::BeginChild("ChildL", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); for (int i = 0; i < 100; i++) { ImGui::Text("%04d: scrollable region", i); @@ -1780,7 +1812,7 @@ static void ShowDemoWindowLayout() { ImGuiWindowFlags window_flags = (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0) | (disable_menu ? 0 : ImGuiWindowFlags_MenuBar); ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); - ImGui::BeginChild("Child2", ImVec2(0, 260), true, window_flags); + ImGui::BeginChild("ChildR", ImVec2(0, 260), true, window_flags); if (!disable_menu && ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("Menu")) @@ -1813,7 +1845,7 @@ static void ShowDemoWindowLayout() { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); - ImGui::BeginChild("blah", ImVec2(200, 100), true, ImGuiWindowFlags_None); + ImGui::BeginChild("Red", ImVec2(200, 100), true, ImGuiWindowFlags_None); for (int n = 0; n < 50; n++) ImGui::Text("Some test %d", n); ImGui::EndChild(); @@ -2007,7 +2039,7 @@ static void ShowDemoWindowLayout() if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) { for (int n = 0; n < IM_ARRAYSIZE(opened); n++) - if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n])) + if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n], ImGuiTabItemFlags_None)) { ImGui::Text("This is the %s tab!", names[n]); if (n & 1) @@ -2185,10 +2217,10 @@ static void ShowDemoWindowLayout() ImGui::SameLine(140); enable_track |= ImGui::DragInt("##item", &track_item, 0.25f, 0, 99, "Item = %d"); bool scroll_to_off = ImGui::Button("Scroll Offset"); - ImGui::SameLine(140); scroll_to_off |= ImGui::DragFloat("##off", &scroll_to_off_px, 1.00f, 0, 9999, "+%.0f px"); + ImGui::SameLine(140); scroll_to_off |= ImGui::DragFloat("##off", &scroll_to_off_px, 1.00f, 0, FLT_MAX, "+%.0f px"); bool scroll_to_pos = ImGui::Button("Scroll To Pos"); - ImGui::SameLine(140); scroll_to_pos |= ImGui::DragFloat("##pos", &scroll_to_pos_px, 1.00f, -10, 9999, "X/Y = %.0f px"); + ImGui::SameLine(140); scroll_to_pos |= ImGui::DragFloat("##pos", &scroll_to_pos_px, 1.00f, -10, FLT_MAX, "X/Y = %.0f px"); ImGui::PopItemWidth(); if (scroll_to_off || scroll_to_pos) @@ -2207,7 +2239,7 @@ static void ShowDemoWindowLayout() ImGui::TextUnformatted(names[i]); ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; - ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(child_w, 200.0f), true, child_flags); + bool window_visible = ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(child_w, 200.0f), true, child_flags); if (ImGui::BeginMenuBar()) { ImGui::TextUnformatted("abc"); @@ -2217,16 +2249,19 @@ static void ShowDemoWindowLayout() ImGui::SetScrollY(scroll_to_off_px); if (scroll_to_pos) ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scroll_to_pos_px, i * 0.25f); - for (int item = 0; item < 100; item++) + if (window_visible) // Avoid calling SetScrollHereY when running with culled items { - if (enable_track && item == track_item) - { - ImGui::TextColored(ImVec4(1,1,0,1), "Item %d", item); - ImGui::SetScrollHereY(i * 0.25f); // 0.0f:top, 0.5f:center, 1.0f:bottom - } - else + for (int item = 0; item < 100; item++) { - ImGui::Text("Item %d", item); + if (enable_track && item == track_item) + { + ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item); + ImGui::SetScrollHereY(i * 0.25f); // 0.0f:top, 0.5f:center, 1.0f:bottom + } + else + { + ImGui::Text("Item %d", item); + } } } float scroll_y = ImGui::GetScrollY(); @@ -2245,23 +2280,26 @@ static void ShowDemoWindowLayout() { float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f; ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0); - ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(-100, child_height), true, child_flags); + bool window_visible = ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(-100, child_height), true, child_flags); if (scroll_to_off) ImGui::SetScrollX(scroll_to_off_px); if (scroll_to_pos) ImGui::SetScrollFromPosX(ImGui::GetCursorStartPos().x + scroll_to_pos_px, i * 0.25f); - for (int item = 0; item < 100; item++) + if (window_visible) // Avoid calling SetScrollHereY when running with culled items { - if (enable_track && item == track_item) + for (int item = 0; item < 100; item++) { - ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item); - ImGui::SetScrollHereX(i * 0.25f); // 0.0f:left, 0.5f:center, 1.0f:right - } - else - { - ImGui::Text("Item %d", item); + if (enable_track && item == track_item) + { + ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item); + ImGui::SetScrollHereX(i * 0.25f); // 0.0f:left, 0.5f:center, 1.0f:right + } + else + { + ImGui::Text("Item %d", item); + } + ImGui::SameLine(); } - ImGui::SameLine(); } float scroll_x = ImGui::GetScrollX(); float scroll_max_x = ImGui::GetScrollMaxX(); @@ -2419,7 +2457,7 @@ static void ShowDemoWindowLayout() ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec4 clip_rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); ImGui::InvisibleButton("##dummy", size); - if (ImGui::IsItemActive() && ImGui::IsMouseDragging()) { offset.x += ImGui::GetIO().MouseDelta.x; offset.y += ImGui::GetIO().MouseDelta.y; } + if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) { offset.x += ImGui::GetIO().MouseDelta.x; offset.y += ImGui::GetIO().MouseDelta.y; } ImGui::GetWindowDrawList()->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(90, 90, 120, 255)); ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.0f, ImVec2(pos.x + offset.x, pos.y + offset.y), IM_COL32(255, 255, 255, 255), "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect); ImGui::TreePop(); @@ -3025,7 +3063,7 @@ static void ShowDemoWindowMisc() if (ImGui::TreeNode("Mouse cursors")) { - const char* mouse_cursors_names[] = { "Arrow", "TextInput", "Move", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand" }; + const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" }; IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); ImGui::Text("Current mouse cursor = %d: %s", ImGui::GetMouseCursor(), mouse_cursors_names[ImGui::GetMouseCursor()]); @@ -3092,11 +3130,17 @@ void ImGui::ShowAboutWindow(bool* p_open) #ifdef IMGUI_DISABLE_WIN32_FUNCTIONS ImGui::Text("define: IMGUI_DISABLE_WIN32_FUNCTIONS"); #endif -#ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS - ImGui::Text("define: IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS"); +#ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS"); #endif -#ifdef IMGUI_DISABLE_MATH_FUNCTIONS - ImGui::Text("define: IMGUI_DISABLE_MATH_FUNCTIONS"); +#ifdef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS"); +#endif +#ifdef IMGUI_DISABLE_FILE_FUNCTIONS + ImGui::Text("define: IMGUI_DISABLE_FILE_FUNCTIONS"); #endif #ifdef IMGUI_DISABLE_DEFAULT_ALLOCATORS ImGui::Text("define: IMGUI_DISABLE_DEFAULT_ALLOCATORS"); @@ -3223,7 +3267,7 @@ void ImGui::ShowFontSelector(const char* label) HelpMarker( "- Load additional fonts with io.Fonts->AddFontFromFileTTF().\n" "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n" - "- Read FAQ and documentation in misc/fonts/ for more details.\n" + "- Read FAQ and docs/FONTS.txt for more details.\n" "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame()."); } @@ -3263,7 +3307,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::Button("Revert Ref")) style = *ref; ImGui::SameLine(); - HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. Use \"Export Colors\" below to save them somewhere."); + HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. Use \"Export\" below to save them somewhere."); ImGui::Separator(); @@ -3311,7 +3355,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { static int output_dest = 0; static bool output_only_modified = true; - if (ImGui::Button("Export Unsaved")) + if (ImGui::Button("Export")) { if (output_dest == 0) ImGui::LogToClipboard(); @@ -3334,9 +3378,9 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) filter.Draw("Filter colors", ImGui::GetFontSize() * 16); static ImGuiColorEditFlags alpha_flags = 0; - ImGui::RadioButton("Opaque", &alpha_flags, 0); ImGui::SameLine(); - ImGui::RadioButton("Alpha", &alpha_flags, ImGuiColorEditFlags_AlphaPreview); ImGui::SameLine(); - ImGui::RadioButton("Both", &alpha_flags, ImGuiColorEditFlags_AlphaPreviewHalf); ImGui::SameLine(); + if (ImGui::RadioButton("Opaque", alpha_flags == 0)) { alpha_flags = 0; } ImGui::SameLine(); + if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_AlphaPreview)) { alpha_flags = ImGuiColorEditFlags_AlphaPreview; } ImGui::SameLine(); + if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine(); HelpMarker("In the color list:\nLeft-click on colored square to open color picker,\nRight-click to open edit options menu."); ImGui::BeginChild("##colors", ImVec2(0, 0), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); @@ -3351,7 +3395,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0) { // Tips: in a real user application, you may want to merge and use an icon font into the main font, so instead of "Save"/"Revert" you'd use icons. - // Read the FAQ and misc/fonts/README.txt about using icon fonts. It's really easy and super convenient! + // Read the FAQ and docs/FONTS.txt about using icon fonts. It's really easy and super convenient! ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) ref->Colors[i] = style.Colors[i]; ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) style.Colors[i] = ref->Colors[i]; } @@ -3369,7 +3413,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { ImGuiIO& io = ImGui::GetIO(); ImFontAtlas* atlas = io.Fonts; - HelpMarker("Read FAQ and misc/fonts/README.txt for details on font loading."); + HelpMarker("Read FAQ and docs/FONTS.txt for details on font loading."); ImGui::PushItemWidth(120); for (int i = 0; i < atlas->Fonts.Size; i++) { @@ -3389,17 +3433,27 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); const float surface_sqrt = sqrtf((float)font->MetricsTotalSurface); - ImGui::Text("Texture surface: %d pixels (approx) ~ %dx%d", font->MetricsTotalSurface, (int)surface_sqrt, (int)surface_sqrt); + ImGui::Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, (int)surface_sqrt, (int)surface_sqrt); for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) - if (const ImFontConfig* cfg = &font->ConfigData[config_i]) - ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); + if (font->ConfigData) + if (const ImFontConfig* cfg = &font->ConfigData[config_i]) + ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) { // Display all glyphs of the fonts in separate pages of 256 characters - for (int base = 0; base < 0x10000; base += 256) + for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256) { + // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k) + // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT is large. + // (if ImWchar==ImWchar32 we will do at least about 272 queries here) + if (!(base & 4095) && font->IsGlyphRangeUnused(base, base + 4095)) + { + base += 4096 - 256; + continue; + } + int count = 0; - for (int n = 0; n < 256; n++) + for (unsigned int n = 0; n < 256; n++) count += font->FindGlyphNoFallback((ImWchar)(base + n)) ? 1 : 0; if (count > 0 && ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) { @@ -3407,7 +3461,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) float cell_spacing = style.ItemSpacing.y; ImVec2 base_pos = ImGui::GetCursorScreenPos(); ImDrawList* draw_list = ImGui::GetWindowDrawList(); - for (int n = 0; n < 256; n++) + for (unsigned int n = 0; n < 256; n++) { ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); @@ -3420,6 +3474,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::BeginTooltip(); ImGui::Text("Codepoint: U+%04X", base + n); ImGui::Separator(); + ImGui::Text("Visible: %d", glyph->Visible); ImGui::Text("AdvanceX: %.1f", glyph->AdvanceX); ImGui::Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); ImGui::Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); @@ -3459,8 +3514,9 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines); ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); ImGui::PushItemWidth(100); - ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, FLT_MAX, "%.2f", 2.0f); + ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f"); if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f; + ImGui::DragFloat("Circle segment Max Error", &style.CircleSegmentMaxError, 0.01f, 0.10f, 10.0f, "%.2f"); ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero. ImGui::PopItemWidth(); @@ -3533,6 +3589,7 @@ static void ShowExampleMenuFile() } if (ImGui::MenuItem("Save", "Ctrl+S")) {} if (ImGui::MenuItem("Save As..")) {} + ImGui::Separator(); if (ImGui::BeginMenu("Options")) { @@ -3544,13 +3601,12 @@ static void ShowExampleMenuFile() ImGui::EndChild(); static float f = 0.5f; static int n = 0; - static bool b = true; ImGui::SliderFloat("Value", &f, 0.0f, 1.0f); ImGui::InputFloat("Input", &f, 0.1f); ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0"); - ImGui::Checkbox("Check", &b); ImGui::EndMenu(); } + if (ImGui::BeginMenu("Colors")) { float sz = ImGui::GetTextLineHeight(); @@ -3565,6 +3621,17 @@ static void ShowExampleMenuFile() } ImGui::EndMenu(); } + + // Here we demonstrate appending again to the "Options" menu (which we already created above) + // Of course in this demo it is a little bit silly that this function calls BeginMenu("Options") twice. + // In a real code-base using it would make senses to use this feature from very different code locations. + if (ImGui::BeginMenu("Options")) // <-- Append! + { + static bool b = true; + ImGui::Checkbox("SomeOption", &b); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Disabled", false)) // Disabled { IM_ASSERT(0); @@ -4407,28 +4474,59 @@ static void ShowExampleAppCustomRendering(bool* p_open) if (ImGui::BeginTabBar("##TabBar")) { - // Primitives if (ImGui::BeginTabItem("Primitives")) { + ImGui::PushItemWidth(-ImGui::GetFontSize() * 10); + + // Draw gradients + // (note that those are currently exacerbating our sRGB/Linear issues) + ImGui::Text("Gradients"); + ImVec2 gradient_size = ImVec2(ImGui::CalcItemWidth(), ImGui::GetFrameHeight()); + { + ImVec2 p = ImGui::GetCursorScreenPos(); + ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); + ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); + draw_list->AddRectFilledMultiColor(p, ImVec2(p.x + gradient_size.x, p.y + gradient_size.y), col_a, col_b, col_b, col_a); + ImGui::InvisibleButton("##gradient1", gradient_size); + } + { + ImVec2 p = ImGui::GetCursorScreenPos(); + ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); + ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); + draw_list->AddRectFilledMultiColor(p, ImVec2(p.x + gradient_size.x, p.y + gradient_size.y), col_a, col_b, col_b, col_a); + ImGui::InvisibleButton("##gradient2", gradient_size); + } + + // Draw a bunch of primitives + ImGui::Text("All primitives"); static float sz = 36.0f; static float thickness = 3.0f; + static int ngon_sides = 6; + static bool circle_segments_override = false; + static int circle_segments_override_v = 12; static ImVec4 colf = ImVec4(1.0f, 1.0f, 0.4f, 1.0f); ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 72.0f, "%.0f"); ImGui::DragFloat("Thickness", &thickness, 0.05f, 1.0f, 8.0f, "%.02f"); + ImGui::SliderInt("N-gon sides", &ngon_sides, 3, 12); + ImGui::Checkbox("##circlesegmentoverride", &circle_segments_override); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + if (ImGui::SliderInt("Circle segments", &circle_segments_override_v, 3, 40)) + circle_segments_override = true; ImGui::ColorEdit4("Color", &colf.x); const ImVec2 p = ImGui::GetCursorScreenPos(); const ImU32 col = ImColor(colf); + const float spacing = 10.0f; + const ImDrawCornerFlags corners_none = 0; + const ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; + const ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; + const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; float x = p.x + 4.0f, y = p.y + 4.0f; - float spacing = 10.0f; - ImDrawCornerFlags corners_none = 0; - ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; - ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; for (int n = 0; n < 2; n++) { // First line uses a thickness of 1.0f, second line uses the configurable thickness float th = (n == 0) ? 1.0f : thickness; - draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 6, th); x += sz + spacing; // Hexagon - draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 20, th); x += sz + spacing; // Circle + draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon + draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners @@ -4441,8 +4539,8 @@ static void ShowExampleAppCustomRendering(bool* p_open) x = p.x + 4; y += sz + spacing; } - draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 6); x += sz + spacing; // Hexagon - draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, 32); x += sz + spacing; // Circle + draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz*0.5f, col, ngon_sides); x += sz + spacing; // N-gon + draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments);x += sz + spacing; // Circle draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners @@ -4453,6 +4551,8 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); ImGui::Dummy(ImVec2((sz + spacing) * 9.8f, (sz + spacing) * 3)); + + ImGui::PopItemWidth(); ImGui::EndTabItem(); } @@ -4519,9 +4619,9 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImVec2 window_size = ImGui::GetWindowSize(); ImVec2 window_center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f); if (draw_bg) - ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 48, 10+4); + ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 0, 10+4); if (draw_fg) - ImGui::GetForegroundDrawList()->AddCircle(window_center, window_size.y * 0.6f, IM_COL32(0, 255, 0, 200), 48, 10); + ImGui::GetForegroundDrawList()->AddCircle(window_center, window_size.y * 0.6f, IM_COL32(0, 255, 0, 200), 0, 10); ImGui::EndTabItem(); } @@ -4632,7 +4732,8 @@ void ShowExampleAppDocuments(bool* p_open) static bool opt_reorderable = true; static ImGuiTabBarFlags opt_fitting_flags = ImGuiTabBarFlags_FittingPolicyDefault_; - if (!ImGui::Begin("Example: Documents", p_open, ImGuiWindowFlags_MenuBar)) + bool window_contents_visible = ImGui::Begin("Example: Documents", p_open, ImGuiWindowFlags_MenuBar); + if (!window_contents_visible) { ImGui::End(); return; @@ -4812,3 +4913,5 @@ void ImGui::ShowUserGuide() {} void ImGui::ShowStyleEditor(ImGuiStyle*) {} #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 4a6dce6d..d256c8d4 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (drawing and font code) /* @@ -27,6 +27,8 @@ Index of this file: #endif #include "imgui.h" +#ifndef IMGUI_DISABLE + #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif @@ -348,14 +350,29 @@ ImDrawListSharedData::ImDrawListSharedData() Font = NULL; FontSize = 0.0f; CurveTessellationTol = 0.0f; + CircleSegmentMaxError = 0.0f; ClipRectFullscreen = ImVec4(-8192.0f, -8192.0f, +8192.0f, +8192.0f); InitialFlags = ImDrawListFlags_None; - // Const data - for (int i = 0; i < IM_ARRAYSIZE(CircleVtx12); i++) + // Lookup tables + for (int i = 0; i < IM_ARRAYSIZE(ArcFastVtx); i++) { - const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(CircleVtx12); - CircleVtx12[i] = ImVec2(ImCos(a), ImSin(a)); + const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx); + ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); + } + memset(CircleSegmentCounts, 0, sizeof(CircleSegmentCounts)); // This will be set by SetCircleSegmentMaxError() +} + +void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) +{ + if (CircleSegmentMaxError == max_error) + return; + CircleSegmentMaxError = max_error; + for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) + { + const float radius = i + 1.0f; + const int segment_count = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError); + CircleSegmentCounts[i] = (ImU8)ImMin(segment_count, 255); } } @@ -516,10 +533,13 @@ void ImDrawList::PopTextureID() UpdateTextureID(); } -// NB: this can be called with negative count for removing primitives (as long as the result does not underflow) +// Reserve space for a number of vertices and indices. +// You must finish filling your reserved data before calling PrimReserve() again, as it may reallocate or +// submit the intermediate results. PrimUnreserve() can be used to release unused allocations. void ImDrawList::PrimReserve(int idx_count, int vtx_count) { // Large mesh support (when enabled) + IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) { _VtxCurrentOffset = VtxBuffer.Size; @@ -527,7 +547,7 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) AddDrawCmd(); } - ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size-1]; + ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size - 1]; draw_cmd.ElemCount += idx_count; int vtx_buffer_old_size = VtxBuffer.Size; @@ -539,6 +559,17 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; } +// Release the a number of reserved vertices/indices from the end of the last reservation made with PrimReserve(). +void ImDrawList::PrimUnreserve(int idx_count, int vtx_count) +{ + IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); + + ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size - 1]; + draw_cmd.ElemCount -= idx_count; + VtxBuffer.shrink(VtxBuffer.Size - vtx_count); + IdxBuffer.shrink(IdxBuffer.Size - idx_count); +} + // Fully unrolled with inline call to keep our debug builds decently fast. void ImDrawList::PrimRect(const ImVec2& a, const ImVec2& c, ImU32 col) { @@ -586,8 +617,8 @@ void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, c // On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superflous function calls to optimize debug/non-inlined builds. // Those macros expects l-values. -#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } } -#define IM_FIXNORMAL2F(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; } +#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } } while (0) +#define IM_FIXNORMAL2F(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; } while (0) // TODO: Thickness anti-aliased lines cap are missing their AA fringe. // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. @@ -649,7 +680,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 // Average normals float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; - IM_FIXNORMAL2F(dm_x, dm_y) + IM_FIXNORMAL2F(dm_x, dm_y); dm_x *= AA_SIZE; dm_y *= AA_SIZE; @@ -867,10 +898,18 @@ void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_ _Path.push_back(center); return; } + + // For legacy reason the PathArcToFast() always takes angles where 2*PI is represented by 12, + // but it is possible to set IM_DRAWLIST_ARCFAST_TESSELATION_MULTIPLIER to a higher value. This should compile to a no-op otherwise. +#if IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER != 1 + a_min_of_12 *= IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER; + a_max_of_12 *= IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER; +#endif + _Path.reserve(_Path.Size + (a_max_of_12 - a_min_of_12 + 1)); for (int a = a_min_of_12; a <= a_max_of_12; a++) { - const ImVec2& c = _Data->CircleVtx12[a % IM_ARRAYSIZE(_Data->CircleVtx12)]; + const ImVec2& c = _Data->ArcFastVtx[a % IM_ARRAYSIZE(_Data->ArcFastVtx)]; _Path.push_back(ImVec2(center.x + c.x * radius, center.y + c.y * radius)); } } @@ -893,6 +932,17 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa } } +ImVec2 ImBezierCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) +{ + float u = 1.0f - t; + float w1 = u*u*u; + float w2 = 3*u*u*t; + float w3 = 3*u*t*t; + float w4 = t*t*t; + return ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y); +} + +// Closely mimics BezierClosestPointCasteljauStep() in imgui.cpp static void PathBezierToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) { float dx = x4 - x1; @@ -913,7 +963,6 @@ static void PathBezierToCasteljau(ImVector* path, float x1, float y1, fl float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; - PathBezierToCasteljau(path, x1,y1, x12,y12, x123,y123, x1234,y1234, tess_tol, level+1); PathBezierToCasteljau(path, x1234,y1234, x234,y234, x34,y34, x4,y4, tess_tol, level+1); } @@ -924,22 +973,13 @@ void ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImV ImVec2 p1 = _Path.back(); if (num_segments == 0) { - // Auto-tessellated - PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); + PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); // Auto-tessellated } else { float t_step = 1.0f / (float)num_segments; for (int i_step = 1; i_step <= num_segments; i_step++) - { - float t = t_step * i_step; - float u = 1.0f - t; - float w1 = u*u*u; - float w2 = 3*u*u*t; - float w3 = 3*u*t*t; - float w4 = t*t*t; - _Path.push_back(ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y)); - } + _Path.push_back(ImBezierCalc(p1, p2, p3, p4, t_step * i_step)); } } @@ -1069,6 +1109,67 @@ void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImV } void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) + return; + + // Obtain segment count + if (num_segments <= 0) + { + // Automatic segment count + const int radius_idx = (int)radius - 1; + if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) + num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value + else + num_segments = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); + } + else + { + // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) + num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); + } + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; + if (num_segments == 12) + PathArcToFast(center, radius - 0.5f, 0, 12); + else + PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); + PathStroke(col, true, thickness); +} + +void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) +{ + if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) + return; + + // Obtain segment count + if (num_segments <= 0) + { + // Automatic segment count + const int radius_idx = (int)radius - 1; + if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) + num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value + else + num_segments = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); + } + else + { + // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) + num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); + } + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; + if (num_segments == 12) + PathArcToFast(center, radius, 0, 12); + else + PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); + PathFillConvex(col); +} + +// Guaranteed to honor 'num_segments' +void ImDrawList::AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) { if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) return; @@ -1079,7 +1180,8 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu PathStroke(col, true, thickness); } -void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) +// Guaranteed to honor 'num_segments' +void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) { if ((col & IM_COL32_A_MASK) == 0 || num_segments <= 2) return; @@ -1090,13 +1192,14 @@ void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, PathFillConvex(col); } -void ImDrawList::AddBezierCurve(const ImVec2& pos0, const ImVec2& cp0, const ImVec2& cp1, const ImVec2& pos1, ImU32 col, float thickness, int num_segments) +// Cubic Bezier takes 4 controls points +void ImDrawList::AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) { if ((col & IM_COL32_A_MASK) == 0) return; - PathLineTo(pos0); - PathBezierCurveTo(cp0, cp1, pos1, num_segments); + PathLineTo(p1); + PathBezierCurveTo(p2, p3, p4, num_segments); PathStroke(col, false, thickness); } @@ -1214,7 +1317,7 @@ void ImDrawListSplitter::ClearFreeMemory() void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) { - IM_ASSERT(_Current == 0 && _Count <= 1); + IM_ASSERT(_Current == 0 && _Count <= 1 && "Nested channel splitting is not supported. Please use separate instances of ImDrawListSplitter."); int old_channels_count = _Channels.Size; if (old_channels_count < channels_count) _Channels.resize(channels_count); @@ -1275,7 +1378,7 @@ void ImDrawListSplitter::Merge(ImDrawList* draw_list) // Merge previous channel last draw command with current channel first draw command if matching. last_cmd->ElemCount += ch._CmdBuffer[0].ElemCount; idx_offset += ch._CmdBuffer[0].ElemCount; - ch._CmdBuffer.erase(ch._CmdBuffer.Data); + ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges. } if (ch._CmdBuffer.Size > 0) last_cmd = &ch._CmdBuffer.back(); @@ -1673,7 +1776,7 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, void* data = ImFileLoadToMemory(filename, "rb", &data_size, 0); if (!data) { - IM_ASSERT(0); // Could not load file. + IM_ASSERT_USER_ERROR(0, "Could not load font file!"); return NULL; } ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); @@ -1725,7 +1828,8 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed int ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height) { - IM_ASSERT(id >= 0x10000); + // Breaking change on 2019/11/21 (1.74): ImFontAtlas::AddCustomRectRegular() now requires an ID >= 0x110000 (instead of >= 0x10000) + IM_ASSERT(id >= 0x110000); IM_ASSERT(width > 0 && width <= 0xFFFF); IM_ASSERT(height > 0 && height <= 0xFFFF); ImFontAtlasCustomRect r; @@ -1752,7 +1856,7 @@ int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int return CustomRects.Size - 1; // Return index } -void ImFontAtlas::CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) +void ImFontAtlas::CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const { IM_ASSERT(TexWidth > 0 && TexHeight > 0); // Font atlas needs to be built before we can calculate UV coordinates IM_ASSERT(rect->IsPacked()); // Make sure the rectangle has been packed @@ -1817,7 +1921,7 @@ struct ImFontBuildSrcData int DstIndex; // Index into atlas->Fonts[] and dst_tmp_array[] int GlyphsHighest; // Highest requested codepoint int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font) - ImBoolVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB) + ImBitVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB) ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsMap) }; @@ -1827,26 +1931,26 @@ struct ImFontBuildDstData int SrcCount; // Number of source fonts targeting this destination font. int GlyphsHighest; int GlyphsCount; - ImBoolVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font. + ImBitVector GlyphsSet; // This is used to resolve collision when multiple sources are merged into a same destination font. }; -static void UnpackBoolVectorToFlatIndexList(const ImBoolVector* in, ImVector* out) +static void UnpackBitVectorToFlatIndexList(const ImBitVector* in, ImVector* out) { IM_ASSERT(sizeof(in->Storage.Data[0]) == sizeof(int)); - const int* it_begin = in->Storage.begin(); - const int* it_end = in->Storage.end(); - for (const int* it = it_begin; it < it_end; it++) - if (int entries_32 = *it) - for (int bit_n = 0; bit_n < 32; bit_n++) - if (entries_32 & (1u << bit_n)) - out->push_back((int)((it - it_begin) << 5) + bit_n); + const ImU32* it_begin = in->Storage.begin(); + const ImU32* it_end = in->Storage.end(); + for (const ImU32* it = it_begin; it < it_end; it++) + if (ImU32 entries_32 = *it) + for (ImU32 bit_n = 0; bit_n < 32; bit_n++) + if (entries_32 & ((ImU32)1 << bit_n)) + out->push_back((int)(((it - it_begin) << 5) + bit_n)); } bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) { IM_ASSERT(atlas->ConfigData.Size > 0); - ImFontAtlasBuildRegisterDefaultCustomRects(atlas); + ImFontAtlasBuildInit(atlas); // Clear atlas atlas->TexID = (ImTextureID)NULL; @@ -1900,14 +2004,14 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; - src_tmp.GlyphsSet.Resize(src_tmp.GlyphsHighest + 1); + src_tmp.GlyphsSet.Create(src_tmp.GlyphsHighest + 1); if (dst_tmp.GlyphsSet.Storage.empty()) - dst_tmp.GlyphsSet.Resize(dst_tmp.GlyphsHighest + 1); + dst_tmp.GlyphsSet.Create(dst_tmp.GlyphsHighest + 1); for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) - for (int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++) + for (unsigned int codepoint = src_range[0]; codepoint <= src_range[1]; codepoint++) { - if (dst_tmp.GlyphsSet.GetBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true) + if (dst_tmp.GlyphsSet.TestBit(codepoint)) // Don't overwrite existing glyphs. We could make this an option for MergeMode (e.g. MergeOverwrite==true) continue; if (!stbtt_FindGlyphIndex(&src_tmp.FontInfo, codepoint)) // It is actually in the font? continue; @@ -1915,8 +2019,8 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) // Add to avail set/counters src_tmp.GlyphsCount++; dst_tmp.GlyphsCount++; - src_tmp.GlyphsSet.SetBit(codepoint, true); - dst_tmp.GlyphsSet.SetBit(codepoint, true); + src_tmp.GlyphsSet.SetBit(codepoint); + dst_tmp.GlyphsSet.SetBit(codepoint); total_glyphs_count++; } } @@ -1926,7 +2030,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) { ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; src_tmp.GlyphsList.reserve(src_tmp.GlyphsCount); - UnpackBoolVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList); + UnpackBitVectorToFlatIndexList(&src_tmp.GlyphsSet, &src_tmp.GlyphsList); src_tmp.GlyphsSet.Clear(); IM_ASSERT(src_tmp.GlyphsList.Size == src_tmp.GlyphsCount); } @@ -2069,7 +2173,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) const float descent = ImFloor(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent); const float font_off_x = cfg.GlyphOffset.x; - const float font_off_y = cfg.GlyphOffset.y + (float)(int)(dst_font->Ascent + 0.5f); + const float font_off_y = cfg.GlyphOffset.y + IM_ROUND(dst_font->Ascent); for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) { @@ -2080,7 +2184,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) const float char_advance_x_mod = ImClamp(char_advance_x_org, cfg.GlyphMinAdvanceX, cfg.GlyphMaxAdvanceX); float char_off_x = font_off_x; if (char_advance_x_org != char_advance_x_mod) - char_off_x += cfg.PixelSnapH ? (float)(int)((char_advance_x_mod - char_advance_x_org) * 0.5f) : (char_advance_x_mod - char_advance_x_org) * 0.5f; + char_off_x += cfg.PixelSnapH ? ImFloor((char_advance_x_mod - char_advance_x_org) * 0.5f) : (char_advance_x_mod - char_advance_x_org) * 0.5f; // Register glyph stbtt_aligned_quad q; @@ -2098,7 +2202,8 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) return true; } -void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas) +// Register default custom rectangles (this is called/shared by both the stb_truetype and the FreeType builder) +void ImFontAtlasBuildInit(ImFontAtlas* atlas) { if (atlas->CustomRectIds[0] >= 0) return; @@ -2189,7 +2294,7 @@ void ImFontAtlasBuildFinish(ImFontAtlas* atlas) for (int i = 0; i < atlas->CustomRects.Size; i++) { const ImFontAtlasCustomRect& r = atlas->CustomRects[i]; - if (r.Font == NULL || r.ID > 0x10000) + if (r.Font == NULL || r.ID >= 0x110000) continue; IM_ASSERT(r.Font->ContainerAtlas == atlas); @@ -2453,8 +2558,7 @@ void ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end) text += c_len; if (c_len == 0) break; - if (c < 0x10000) - AddChar((ImWchar)c); + AddChar((ImWchar)c); } } @@ -2467,12 +2571,12 @@ void ImFontGlyphRangesBuilder::AddRanges(const ImWchar* ranges) void ImFontGlyphRangesBuilder::BuildRanges(ImVector* out_ranges) { - int max_codepoint = 0x10000; - for (int n = 0; n < max_codepoint; n++) + const int max_codepoint = IM_UNICODE_CODEPOINT_MAX; + for (int n = 0; n <= max_codepoint; n++) if (GetBit(n)) { out_ranges->push_back((ImWchar)n); - while (n < max_codepoint - 1 && GetBit(n + 1)) + while (n < max_codepoint && GetBit(n + 1)) n++; out_ranges->push_back((ImWchar)n); } @@ -2498,6 +2602,7 @@ ImFont::ImFont() Scale = 1.0f; Ascent = Descent = 0.0f; MetricsTotalSurface = 0; + memset(Used4kPagesMap, 0, sizeof(Used4kPagesMap)); } ImFont::~ImFont() @@ -2525,23 +2630,29 @@ void ImFont::BuildLookupTable() for (int i = 0; i != Glyphs.Size; i++) max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint); + // Build lookup table IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved IndexAdvanceX.clear(); IndexLookup.clear(); DirtyLookupTables = false; + memset(Used4kPagesMap, 0, sizeof(Used4kPagesMap)); GrowIndex(max_codepoint + 1); for (int i = 0; i < Glyphs.Size; i++) { int codepoint = (int)Glyphs[i].Codepoint; IndexAdvanceX[codepoint] = Glyphs[i].AdvanceX; IndexLookup[codepoint] = (ImWchar)i; + + // Mark 4K page as used + const int page_n = codepoint / 4096; + Used4kPagesMap[page_n >> 3] |= 1 << (page_n & 7); } // Create a glyph to handle TAB // FIXME: Needs proper TAB handling but it needs to be contextualized (or we could arbitrary say that each string starts at "column 0" ?) if (FindGlyph((ImWchar)' ')) { - if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times + if (Glyphs.back().Codepoint != '\t') // So we can call this function multiple times (FIXME: Flaky) Glyphs.resize(Glyphs.Size + 1); ImFontGlyph& tab_glyph = Glyphs.back(); tab_glyph = *FindGlyph((ImWchar)' '); @@ -2551,6 +2662,11 @@ void ImFont::BuildLookupTable() IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size-1); } + // Mark special glyphs as not visible (note that AddGlyph already mark as non-visible glyphs with zero-size polygons) + SetGlyphVisible((ImWchar)' ', false); + SetGlyphVisible((ImWchar)'\t', false); + + // Setup fall-backs FallbackGlyph = FindGlyphNoFallback(FallbackChar); FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f; for (int i = 0; i < max_codepoint + 1; i++) @@ -2558,6 +2674,25 @@ void ImFont::BuildLookupTable() IndexAdvanceX[i] = FallbackAdvanceX; } +// API is designed this way to avoid exposing the 4K page size +// e.g. use with IsGlyphRangeUnused(0, 255) +bool ImFont::IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last) +{ + unsigned int page_begin = (c_begin / 4096); + unsigned int page_last = (c_last / 4096); + for (unsigned int page_n = page_begin; page_n <= page_last; page_n++) + if ((page_n >> 3) < sizeof(Used4kPagesMap)) + if (Used4kPagesMap[page_n >> 3] & (1 << (page_n & 7))) + return false; + return true; +} + +void ImFont::SetGlyphVisible(ImWchar c, bool visible) +{ + if (ImFontGlyph* glyph = (ImFontGlyph*)(void*)FindGlyph((ImWchar)c)) + glyph->Visible = visible ? 1 : 0; +} + void ImFont::SetFallbackChar(ImWchar c) { FallbackChar = c; @@ -2579,7 +2714,8 @@ void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, { Glyphs.resize(Glyphs.Size + 1); ImFontGlyph& glyph = Glyphs.back(); - glyph.Codepoint = (ImWchar)codepoint; + glyph.Codepoint = (unsigned int)codepoint; + glyph.Visible = (x0 != x1) && (y0 != y1); glyph.X0 = x0; glyph.Y0 = y0; glyph.X1 = x1; @@ -2591,7 +2727,7 @@ void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, glyph.AdvanceX = advance_x + ConfigData->GlyphExtraSpacing.x; // Bake spacing into AdvanceX if (ConfigData->PixelSnapH) - glyph.AdvanceX = (float)(int)(glyph.AdvanceX + 0.5f); + glyph.AdvanceX = IM_ROUND(glyph.AdvanceX); // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round) DirtyLookupTables = true; @@ -2601,7 +2737,7 @@ void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst) { IM_ASSERT(IndexLookup.Size > 0); // Currently this can only be called AFTER the font has been built, aka after calling ImFontAtlas::GetTexDataAs*() function. - int index_size = IndexLookup.Size; + unsigned int index_size = (unsigned int)IndexLookup.Size; if (dst < index_size && IndexLookup.Data[dst] == (ImWchar)-1 && !overwrite_dst) // 'dst' already exists return; @@ -2615,7 +2751,7 @@ void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst) const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const { - if (c >= IndexLookup.Size) + if (c >= (size_t)IndexLookup.Size) return FallbackGlyph; const ImWchar i = IndexLookup.Data[c]; if (i == (ImWchar)-1) @@ -2625,7 +2761,7 @@ const ImFontGlyph* ImFont::FindGlyph(ImWchar c) const const ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const { - if (c >= IndexLookup.Size) + if (c >= (size_t)IndexLookup.Size) return NULL; const ImWchar i = IndexLookup.Data[c]; if (i == (ImWchar)-1) @@ -2828,16 +2964,14 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const { - if (c == ' ' || c == '\t' || c == '\n' || c == '\r') // Match behavior of RenderText(), those 4 codepoints are hard-coded. + const ImFontGlyph* glyph = FindGlyph(c); + if (!glyph || !glyph->Visible) return; - if (const ImFontGlyph* glyph = FindGlyph(c)) - { - float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; - pos.x = (float)(int)pos.x + DisplayOffset.x; - pos.y = (float)(int)pos.y + DisplayOffset.y; - draw_list->PrimReserve(6, 4); - draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); - } + float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; + pos.x = IM_FLOOR(pos.x + DisplayOffset.x); + pos.y = IM_FLOOR(pos.y + DisplayOffset.y); + draw_list->PrimReserve(6, 4); + draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); } void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const @@ -2846,8 +2980,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. // Align to be pixel perfect - pos.x = (float)(int)pos.x + DisplayOffset.x; - pos.y = (float)(int)pos.y + DisplayOffset.y; + pos.x = IM_FLOOR(pos.x + DisplayOffset.x); + pos.y = IM_FLOOR(pos.y + DisplayOffset.y); float x = pos.x; float y = pos.y; if (y > clip_rect.w) @@ -2950,79 +3084,76 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col continue; } - float char_width = 0.0f; - if (const ImFontGlyph* glyph = FindGlyph((ImWchar)c)) - { - char_width = glyph->AdvanceX * scale; + const ImFontGlyph* glyph = FindGlyph((ImWchar)c); + if (glyph == NULL) + continue; - // Arbitrarily assume that both space and tabs are empty glyphs as an optimization - if (c != ' ' && c != '\t') + float char_width = glyph->AdvanceX * scale; + if (glyph->Visible) + { + // We don't do a second finer clipping test on the Y axis as we've already skipped anything before clip_rect.y and exit once we pass clip_rect.w + float x1 = x + glyph->X0 * scale; + float x2 = x + glyph->X1 * scale; + float y1 = y + glyph->Y0 * scale; + float y2 = y + glyph->Y1 * scale; + if (x1 <= clip_rect.z && x2 >= clip_rect.x) { - // We don't do a second finer clipping test on the Y axis as we've already skipped anything before clip_rect.y and exit once we pass clip_rect.w - float x1 = x + glyph->X0 * scale; - float x2 = x + glyph->X1 * scale; - float y1 = y + glyph->Y0 * scale; - float y2 = y + glyph->Y1 * scale; - if (x1 <= clip_rect.z && x2 >= clip_rect.x) + // Render a character + float u1 = glyph->U0; + float v1 = glyph->V0; + float u2 = glyph->U1; + float v2 = glyph->V1; + + // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads. + if (cpu_fine_clip) { - // Render a character - float u1 = glyph->U0; - float v1 = glyph->V0; - float u2 = glyph->U1; - float v2 = glyph->V1; - - // CPU side clipping used to fit text in their frame when the frame is too small. Only does clipping for axis aligned quads. - if (cpu_fine_clip) + if (x1 < clip_rect.x) { - if (x1 < clip_rect.x) - { - u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1); - x1 = clip_rect.x; - } - if (y1 < clip_rect.y) - { - v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1); - y1 = clip_rect.y; - } - if (x2 > clip_rect.z) - { - u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1); - x2 = clip_rect.z; - } - if (y2 > clip_rect.w) - { - v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1); - y2 = clip_rect.w; - } - if (y1 >= y2) - { - x += char_width; - continue; - } + u1 = u1 + (1.0f - (x2 - clip_rect.x) / (x2 - x1)) * (u2 - u1); + x1 = clip_rect.x; } - - // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: + if (y1 < clip_rect.y) + { + v1 = v1 + (1.0f - (y2 - clip_rect.y) / (y2 - y1)) * (v2 - v1); + y1 = clip_rect.y; + } + if (x2 > clip_rect.z) + { + u2 = u1 + ((clip_rect.z - x1) / (x2 - x1)) * (u2 - u1); + x2 = clip_rect.z; + } + if (y2 > clip_rect.w) + { + v2 = v1 + ((clip_rect.w - y1) / (y2 - y1)) * (v2 - v1); + y2 = clip_rect.w; + } + if (y1 >= y2) { - idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); - idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); - vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; - vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; - vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; - vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; - vtx_write += 4; - vtx_current_idx += 4; - idx_write += 6; + x += char_width; + continue; } } + + // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: + { + idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); + idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); + vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; + vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; + vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; + vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; + vtx_write += 4; + vtx_current_idx += 4; + idx_write += 6; + } } } - x += char_width; } - // Give back unused vertices - draw_list->VtxBuffer.resize((int)(vtx_write - draw_list->VtxBuffer.Data)); - draw_list->IdxBuffer.resize((int)(idx_write - draw_list->IdxBuffer.Data)); + // Give back unused vertices (clipped ones, blanks) ~ this is essentially a PrimUnreserve() action. + draw_list->VtxBuffer.Size = (int)(vtx_write - draw_list->VtxBuffer.Data); // Same as calling shrink() + draw_list->IdxBuffer.Size = (int)(idx_write - draw_list->IdxBuffer.Data); draw_list->CmdBuffer[draw_list->CmdBuffer.Size-1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); draw_list->_VtxWritePtr = vtx_write; draw_list->_IdxWritePtr = idx_write; @@ -3201,9 +3332,9 @@ static unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, uns { const unsigned long ADLER_MOD = 65521; unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; - unsigned long blocklen, i; + unsigned long blocklen = buflen % 5552; - blocklen = buflen % 5552; + unsigned long i; while (buflen) { for (i=0; i + 7 < blocklen; i += 8) { s1 += buffer[0], s2 += s1; @@ -3230,10 +3361,9 @@ static unsigned int stb_adler32(unsigned int adler32, unsigned char *buffer, uns static unsigned int stb_decompress(unsigned char *output, const unsigned char *i, unsigned int /*length*/) { - unsigned int olen; if (stb__in4(0) != 0x57bC0000) return 0; if (stb__in4(4) != 0) return 0; // error! stream is > 4GB - olen = stb_decompress_length(i); + const unsigned int olen = stb_decompress_length(i); stb__barrier_in_b = i; stb__barrier_out_e = output + olen; stb__barrier_out_b = output; @@ -3365,3 +3495,5 @@ static const char* GetDefaultCompressedFontDataTTFBase85() { return proggy_clean_ttf_compressed_data_base85; } + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index a9e72c6c..c496232c 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -6,7 +6,7 @@ // Implemented features: // [X] Platform: Clipboard support. // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. +// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. @@ -15,6 +15,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2020-01-17: Inputs: Disable error callback while assigning mouse cursors because some X11 setup don't have them and it generates errors. +// 2019-12-05: Inputs: Added support for new mouse cursors added in GLFW 3.4+ (resizing cursors, not allowed cursor). +// 2019-10-18: Misc: Previously installed user callbacks are now restored on shutdown. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. // 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter(). // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. @@ -49,6 +52,11 @@ #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface +#ifdef GLFW_RESIZE_NESW_CURSOR // let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? +#define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR +#else +#define GLFW_HAS_NEW_CURSORS (0) +#endif // Data enum GlfwClientApi @@ -61,7 +69,8 @@ static GLFWwindow* g_Window = NULL; // Main window static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown; static double g_Time = 0.0; static bool g_MouseJustPressed[5] = { false, false, false, false, false }; -static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 }; +static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; +static bool g_InstalledCallbacks = false; // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. static GLFWmousebuttonfun g_PrevUserCallbackMousebutton = NULL; @@ -113,7 +122,11 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int a io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL]; io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT]; io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT]; +#ifdef _WIN32 + io.KeySuper = false; +#else io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER]; +#endif } void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) @@ -167,14 +180,28 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window); #endif + // Create mouse cursors + // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist, + // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting. + // Missing cursors will return NULL and our _UpdateMouseCursor() function will use the Arrow cursor instead.) + GLFWerrorfun prev_error_callback = glfwSetErrorCallback(NULL); g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. - g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this. g_MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); +#if GLFW_HAS_NEW_CURSORS + g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR); + g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR); +#else + g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); +#endif + glfwSetErrorCallback(prev_error_callback); // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. g_PrevUserCallbackMousebutton = NULL; @@ -183,6 +210,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw g_PrevUserCallbackChar = NULL; if (install_callbacks) { + g_InstalledCallbacks = true; g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); @@ -205,6 +233,15 @@ bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks) void ImGui_ImplGlfw_Shutdown() { + if (g_InstalledCallbacks) + { + glfwSetMouseButtonCallback(g_Window, g_PrevUserCallbackMousebutton); + glfwSetScrollCallback(g_Window, g_PrevUserCallbackScroll); + glfwSetKeyCallback(g_Window, g_PrevUserCallbackKey); + glfwSetCharCallback(g_Window, g_PrevUserCallbackChar); + g_InstalledCallbacks = false; + } + for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) { glfwDestroyCursor(g_MouseCursors[cursor_n]); diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h index ccbe840d..a86790b7 100644 --- a/imgui/imgui_impl_glfw.h +++ b/imgui/imgui_impl_glfw.h @@ -25,8 +25,9 @@ IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool in IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); -// InitXXX function with 'install_callbacks=true': install GLFW callbacks. They will call user's previously installed callbacks, if any. -// InitXXX function with 'install_callbacks=false': do not install GLFW callbacks. You will need to call them yourself from your own GLFW callbacks. +// GLFW callbacks +// - When calling Init with 'install_callbacks=true': GLFW callbacks will be installed for you. They will call user's previously installed callbacks, if any. +// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call those function yourself from your own GLFW callbacks. IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index d7b108ef..9289913e 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -4,8 +4,8 @@ // This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) // Implemented features: -// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. -// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bits indices. +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! +// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. @@ -13,6 +13,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2020-01-07: OpenGL: Added support for glbindings OpenGL loader. +// 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders. // 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility. // 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call. // 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. @@ -77,19 +79,21 @@ #include "TargetConditionals.h" #endif -// Auto-detect GL version +// Auto-enable GLES on matching platforms #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" #undef IMGUI_IMPL_OPENGL_LOADER_GL3W #undef IMGUI_IMPL_OPENGL_LOADER_GLEW #undef IMGUI_IMPL_OPENGL_LOADER_GLAD +#undef IMGUI_IMPL_OPENGL_LOADER_GLBINDING #undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM #elif defined(__EMSCRIPTEN__) #define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" #undef IMGUI_IMPL_OPENGL_LOADER_GL3W #undef IMGUI_IMPL_OPENGL_LOADER_GLEW #undef IMGUI_IMPL_OPENGL_LOADER_GLAD +#undef IMGUI_IMPL_OPENGL_LOADER_GLBINDING #undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM #endif #endif @@ -99,9 +103,9 @@ #include #elif defined(IMGUI_IMPL_OPENGL_ES3) #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) -#include // Use GL ES 3 +#include // Use GL ES 3 #else -#include // Use GL ES 3 +#include // Use GL ES 3 #endif #else // About Desktop OpenGL function loaders: @@ -114,20 +118,25 @@ #include // Needs to be initialized with glewInit() in user's code #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) #include // Needs to be initialized with gladLoadGL() in user's code +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) +#include // Initialize with glbinding::initialize() +#include +using namespace gl; #else #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM #endif #endif -// Desktop GL has glDrawElementsBaseVertex() which GL ES and WebGL don't have. -#if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) -#define IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX 0 +// Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. +#if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) || !defined(GL_VERSION_3_2) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 0 #else -#define IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX 1 +#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 1 #endif // OpenGL Data -static char g_GlslVersionString[32] = ""; +static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries. +static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings. static GLuint g_FontTexture = 0; static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location @@ -137,14 +146,26 @@ static unsigned int g_VboHandle = 0, g_ElementsHandle = 0; // Functions bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { + // Query for GL version +#if !defined(IMGUI_IMPL_OPENGL_ES2) + GLint major, minor; + glGetIntegerv(GL_MAJOR_VERSION, &major); + glGetIntegerv(GL_MINOR_VERSION, &minor); + g_GlVersion = major * 1000 + minor; +#else + g_GlVersion = 2000; // GLES 2 +#endif + // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "imgui_impl_opengl3"; -#if IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX - io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. +#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET + if (g_GlVersion >= 3200) + io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif - // Store GLSL version string so we can refer to it later in case we recreate shaders. Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. + // Store GLSL version string so we can refer to it later in case we recreate shaders. + // Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. #if defined(IMGUI_IMPL_OPENGL_ES2) if (glsl_version == NULL) glsl_version = "#version 100"; @@ -172,6 +193,8 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) gl_loader = "GLEW"; #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) gl_loader = "GLAD"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) + gl_loader = "glbinding"; #else // IMGUI_IMPL_OPENGL_LOADER_CUSTOM gl_loader = "Custom"; #endif @@ -344,11 +367,12 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Bind texture, Draw glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); -#if IMGUI_IMPL_OPENGL_HAS_DRAW_WITH_BASE_VERTEX - glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); -#else - glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); +#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET + if (g_GlVersion >= 3200) + glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); + else #endif + glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); } } } @@ -389,7 +413,7 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() ImGuiIO& io = ImGui::GetIO(); unsigned char* pixels; int width, height; - io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. // Upload texture to graphics system GLint last_texture; diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index 75f8f8c8..98cefe7d 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -4,8 +4,8 @@ // This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) // Implemented features: -// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp. -// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bits indices. +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! +// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. @@ -46,6 +46,7 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #if !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) #if defined(__has_include) #if __has_include() @@ -54,6 +55,8 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #define IMGUI_IMPL_OPENGL_LOADER_GLAD #elif __has_include() #define IMGUI_IMPL_OPENGL_LOADER_GL3W + #elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING #else #error "Cannot detect OpenGL loader!" #endif diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index e7868613..eed5d82e 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -22,6 +22,7 @@ Index of this file: */ #pragma once +#ifndef IMGUI_DISABLE //----------------------------------------------------------------------------- // Header mess @@ -31,7 +32,7 @@ Index of this file: #error Must include imgui.h before imgui_internal.h #endif -#include // FILE* +#include // FILE*, sscanf #include // NULL, malloc, free, qsort, atoi, atof #include // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf #include // INT_MIN, INT_MAX @@ -60,10 +61,19 @@ Index of this file: #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif +// Legacy defines +#ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 +#error Use IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS +#endif +#ifdef IMGUI_DISABLE_MATH_FUNCTIONS // Renamed in 1.74 +#error Use IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS +#endif + //----------------------------------------------------------------------------- // Forward declarations //----------------------------------------------------------------------------- +struct ImBitVector; // Store 1-bit per value struct ImRect; // An axis-aligned rectangle (2 points) struct ImDrawDataBuilder; // Helper to build a ImDrawData instance struct ImDrawListSharedData; // Data shared between all ImDrawList instances @@ -86,7 +96,7 @@ struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) struct ImGuiWindow; // Storage for one window struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame) -struct ImGuiWindowSettings; // Storage for window settings stored in .ini file (we keep one of those even if the actual window wasn't instanced during this session) +struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session) // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical @@ -103,6 +113,7 @@ typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// F typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for SeparatorEx() typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for SliderBehavior() typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() +typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() //------------------------------------------------------------------------- // STB libraries includes @@ -131,24 +142,48 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer #endif //----------------------------------------------------------------------------- -// Generic helpers +// Macros //----------------------------------------------------------------------------- -#define IM_PI 3.14159265358979323846f +// Debug Logging +#ifndef IMGUI_DEBUG_LOG +#define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) +#endif + +// Static Asserts +#if (__cplusplus >= 201100) +#define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") +#else +#define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] +#endif + +// "Paranoid" Debug Asserts are meant to only be enabled during specific debugging/work, otherwise would slow down the code too much. +// We currently don't have many of those so the effect is currently negligible, but onward intent to add more aggressive ones in the code. +//#define IMGUI_DEBUG_PARANOID +#ifdef IMGUI_DEBUG_PARANOID +#define IM_ASSERT_PARANOID(_EXPR) IM_ASSERT(_EXPR) +#else +#define IM_ASSERT_PARANOID(_EXPR) +#endif + +// Error handling +// Down the line in some frameworks/languages we would like to have a way to redirect those to the programmer and recover from more faults. +#ifndef IM_ASSERT_USER_ERROR +#define IM_ASSERT_USER_ERROR(_EXP,_MSG) IM_ASSERT((_EXP) && _MSG) // Recoverable User Error +#endif + +// Misc Macros +#define IM_PI 3.14159265358979323846f #ifdef _WIN32 -#define IM_NEWLINE "\r\n" // Play it nice with Windows users (2018/05 news: Microsoft announced that Notepad will finally display Unix-style carriage returns!) +#define IM_NEWLINE "\r\n" // Play it nice with Windows users (Update: since 2018-05, Notepad finally appears to support Unix-style carriage returns!) #else -#define IM_NEWLINE "\n" +#define IM_NEWLINE "\n" #endif -#define IM_TABSIZE (4) -#define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] +#define IM_TABSIZE (4) #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 - -// Debug Logging -#ifndef IMGUI_DEBUG_LOG -#define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) -#endif +#define IM_FLOOR(_VAL) ((float)(int)(_VAL)) // ImFloor() is not inlined in MSVC debug builds +#define IM_ROUND(_VAL) ((float)(int)((_VAL) + 0.5f)) // // Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall #ifdef _MSC_VER @@ -157,36 +192,37 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer #define IMGUI_CDECL #endif -// Helpers: UTF-8 <> wchar -IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count -IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count -IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count -IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) -IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 -IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 +//----------------------------------------------------------------------------- +// Generic helpers +// Note that the ImXXX helpers functions are lower-level than ImGui functions. +// ImGui functions or the ImGui context are never called/used from other ImXXX functions. +//----------------------------------------------------------------------------- +// - Helpers: Misc +// - Helpers: Bit manipulation +// - Helpers: String, Formatting +// - Helpers: UTF-8 <> wchar conversions +// - Helpers: ImVec2/ImVec4 operators +// - Helpers: Maths +// - Helpers: Geometry +// - Helpers: Bit arrays +// - Helper: ImBitVector +// - Helper: ImPool<> +// - Helper: ImChunkStream<> +//----------------------------------------------------------------------------- // Helpers: Misc +#define ImQsort qsort IMGUI_API ImU32 ImHashData(const void* data, size_t data_size, ImU32 seed = 0); IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); -IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size = NULL, int padding_bytes = 0); -IMGUI_API FILE* ImFileOpen(const char* filename, const char* file_open_mode); -static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } -static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } -static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } -static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } -#define ImQsort qsort #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS static inline ImU32 ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] #endif -// Helpers: Geometry -IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p); -IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); -IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); -IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); -IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); +// Helpers: Bit manipulation +static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } +static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } -// Helpers: String +// Helpers: String, Formatting IMGUI_API int ImStricmp(const char* str1, const char* str2); IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); @@ -198,12 +234,23 @@ IMGUI_API const char* ImStreolRange(const char* str, const char* str_end); IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); IMGUI_API void ImStrTrimBlanks(char* str); +IMGUI_API const char* ImStrSkipBlank(const char* str); IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API const char* ImParseFormatFindStart(const char* format); IMGUI_API const char* ImParseFormatFindEnd(const char* format); IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size); IMGUI_API int ImParseFormatPrecision(const char* format, int default_value); +static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } +static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } + +// Helpers: UTF-8 <> wchar conversions +IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count +IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count +IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count +IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) +IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 +IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 // Helpers: ImVec2/ImVec4 operators // We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.) @@ -215,34 +262,58 @@ static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); } static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); } static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); } -static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } -static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } +static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } +static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } +static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; } +static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; } static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z, lhs.w+rhs.w); } static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); } static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x*rhs.x, lhs.y*rhs.y, lhs.z*rhs.z, lhs.w*rhs.w); } #endif +// Helpers: File System +#ifdef IMGUI_DISABLE_FILE_FUNCTIONS +#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS +typedef void* ImFileHandle; +static inline ImFileHandle ImFileOpen(const char*, const char*) { return NULL; } +static inline bool ImFileClose(ImFileHandle) { return false; } +static inline ImU64 ImFileGetSize(ImFileHandle) { return (ImU64)-1; } +static inline ImU64 ImFileRead(void*, ImU64, ImU64, ImFileHandle) { return 0; } +static inline ImU64 ImFileWrite(const void*, ImU64, ImU64, ImFileHandle) { return 0; } +#endif + +#ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS +typedef FILE* ImFileHandle; +IMGUI_API ImFileHandle ImFileOpen(const char* filename, const char* mode); +IMGUI_API bool ImFileClose(ImFileHandle file); +IMGUI_API ImU64 ImFileGetSize(ImFileHandle file); +IMGUI_API ImU64 ImFileRead(void* data, ImU64 size, ImU64 count, ImFileHandle file); +IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 count, ImFileHandle file); +#else +#define IMGUI_DISABLE_TTY_FUNCTIONS // Can't use stdout, fflush if we are not using default file functions +#endif +IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size = NULL, int padding_bytes = 0); + // Helpers: Maths // - Wrapper for standard libs functions. (Note that imgui_demo.cpp does _not_ use them to keep the code easy to copy) -#ifndef IMGUI_DISABLE_MATH_FUNCTIONS -static inline float ImFabs(float x) { return fabsf(x); } -static inline float ImSqrt(float x) { return sqrtf(x); } -static inline float ImPow(float x, float y) { return powf(x, y); } -static inline double ImPow(double x, double y) { return pow(x, y); } -static inline float ImFmod(float x, float y) { return fmodf(x, y); } -static inline double ImFmod(double x, double y) { return fmod(x, y); } -static inline float ImCos(float x) { return cosf(x); } -static inline float ImSin(float x) { return sinf(x); } -static inline float ImAcos(float x) { return acosf(x); } -static inline float ImAtan2(float y, float x) { return atan2f(y, x); } -static inline double ImAtof(const char* s) { return atof(s); } -static inline float ImFloorStd(float x) { return floorf(x); } // we already uses our own ImFloor() { return (float)(int)v } internally so the standard one wrapper is named differently (it's used by stb_truetype) -static inline float ImCeil(float x) { return ceilf(x); } +#ifndef IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS +#define ImFabs(X) fabsf(X) +#define ImSqrt(X) sqrtf(X) +#define ImFmod(X, Y) fmodf((X), (Y)) +#define ImCos(X) cosf(X) +#define ImSin(X) sinf(X) +#define ImAcos(X) acosf(X) +#define ImAtan2(Y, X) atan2f((Y), (X)) +#define ImAtof(STR) atof(STR) +#define ImFloorStd(X) floorf(X) // We already uses our own ImFloor() { return (float)(int)v } internally so the standard one wrapper is named differently (it's used by e.g. stb_truetype) +#define ImCeil(X) ceilf(X) +static inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision +static inline double ImPow(double x, double y) { return pow(x, y); } #endif -// - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support for variety of types: signed/unsigned int/long long float/double -// (Exceptionally using templates here but we could also redefine them for variety of types) +// - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support variety of types: signed/unsigned int/long long float/double +// (Exceptionally using templates here but we could also redefine them for those types) template static inline T ImMin(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; } template static inline T ImMax(T lhs, T rhs) { return lhs >= rhs ? lhs : rhs; } template static inline T ImClamp(T v, T mn, T mx) { return (v < mn) ? mn : (v > mx) ? mx : v; } @@ -261,49 +332,99 @@ static inline float ImSaturate(float f) static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; } static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; } static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; } -static inline float ImFloor(float f) { return (float)(int)f; } -static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)v.x, (float)(int)v.y); } +static inline float ImFloor(float f) { return (float)(int)(f); } +static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } -// Helper: ImBoolVector. Store 1-bit per value. -// Note that Resize() currently clears the whole vector. -struct ImBoolVector +// Helpers: Geometry +IMGUI_API ImVec2 ImBezierCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t); // Cubic Bezier +IMGUI_API ImVec2 ImBezierClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments); // For curves with explicit number of segments +IMGUI_API ImVec2 ImBezierClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol);// For auto-tessellated curves you can use tess_tol = style.CurveTessellationTol +IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p); +IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); +IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); +IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); +inline float ImTriangleArea(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ImFabs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y))) * 0.5f; } +IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); + +// Helpers: Bit arrays +inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } +inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } +inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } +inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) +{ + while (n <= n2) + { + int a_mod = (n & 31); + int b_mod = ((n2 >= n + 31) ? 31 : (n2 & 31)) + 1; + ImU32 mask = (ImU32)(((ImU64)1 << b_mod) - 1) & ~(ImU32)(((ImU64)1 << a_mod) - 1); + arr[n >> 5] |= mask; + n = (n + 32) & ~31; + } +} + +// Helper: ImBitVector +// Store 1-bit per value. +struct IMGUI_API ImBitVector { - ImVector Storage; - ImBoolVector() { } - void Resize(int sz) { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); } - void Clear() { Storage.clear(); } - bool GetBit(int n) const { int off = (n >> 5); int mask = 1 << (n & 31); return (Storage[off] & mask) != 0; } - void SetBit(int n, bool v) { int off = (n >> 5); int mask = 1 << (n & 31); if (v) Storage[off] |= mask; else Storage[off] &= ~mask; } + ImVector Storage; + void Create(int sz) { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); } + void Clear() { Storage.clear(); } + bool TestBit(int n) const { IM_ASSERT(n < (Storage.Size << 5)); return ImBitArrayTestBit(Storage.Data, n); } + void SetBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArraySetBit(Storage.Data, n); } + void ClearBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArrayClearBit(Storage.Data, n); } }; -// Helper: ImPool<>. Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer, +// Helper: ImPool<> +// Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer, // Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. typedef int ImPoolIdx; template struct IMGUI_API ImPool { - ImVector Data; // Contiguous data + ImVector Buf; // Contiguous data ImGuiStorage Map; // ID->Index ImPoolIdx FreeIdx; // Next free idx to use ImPool() { FreeIdx = 0; } ~ImPool() { Clear(); } - T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Data[idx] : NULL; } - T* GetByIndex(ImPoolIdx n) { return &Data[n]; } - ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Data.Data && p < Data.Data + Data.Size); return (ImPoolIdx)(p - Data.Data); } - T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Data[*p_idx]; *p_idx = FreeIdx; return Add(); } - bool Contains(const T* p) const { return (p >= Data.Data && p < Data.Data + Data.Size); } - void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Data[idx].~T(); } Map.Clear(); Data.clear(); FreeIdx = 0; } - T* Add() { int idx = FreeIdx; if (idx == Data.Size) { Data.resize(Data.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Data[idx]; } IM_PLACEMENT_NEW(&Data[idx]) T(); return &Data[idx]; } + T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Buf[idx] : NULL; } + T* GetByIndex(ImPoolIdx n) { return &Buf[n]; } + ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Buf.Data && p < Buf.Data + Buf.Size); return (ImPoolIdx)(p - Buf.Data); } + T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Buf[*p_idx]; *p_idx = FreeIdx; return Add(); } + bool Contains(const T* p) const { return (p >= Buf.Data && p < Buf.Data + Buf.Size); } + void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Buf[idx].~T(); } Map.Clear(); Buf.clear(); FreeIdx = 0; } + T* Add() { int idx = FreeIdx; if (idx == Buf.Size) { Buf.resize(Buf.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Buf[idx]; } IM_PLACEMENT_NEW(&Buf[idx]) T(); return &Buf[idx]; } void Remove(ImGuiID key, const T* p) { Remove(key, GetIndex(p)); } - void Remove(ImGuiID key, ImPoolIdx idx) { Data[idx].~T(); *(int*)&Data[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); } - void Reserve(int capacity) { Data.reserve(capacity); Map.Data.reserve(capacity); } - int GetSize() const { return Data.Size; } + void Remove(ImGuiID key, ImPoolIdx idx) { Buf[idx].~T(); *(int*)&Buf[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); } + void Reserve(int capacity) { Buf.reserve(capacity); Map.Data.reserve(capacity); } + int GetSize() const { return Buf.Size; } +}; + +// Helper: ImChunkStream<> +// Build and iterate a contiguous stream of variable-sized structures. +// This is used by Settings to store persistent data while reducing allocation count. +// We store the chunk size first, and align the final size on 4 bytes boundaries (this what the '(X + 3) & ~3' statement is for) +// The tedious/zealous amount of casting is to avoid -Wcast-align warnings. +template +struct IMGUI_API ImChunkStream +{ + ImVector Buf; + + void clear() { Buf.clear(); } + bool empty() const { return Buf.Size == 0; } + int size() const { return Buf.Size; } + T* alloc_chunk(size_t sz) { size_t HDR_SZ = 4; sz = ((HDR_SZ + sz) + 3u) & ~3u; int off = Buf.Size; Buf.resize(off + (int)sz); ((int*)(void*)(Buf.Data + off))[0] = (int)sz; return (T*)(void*)(Buf.Data + off + (int)HDR_SZ); } + T* begin() { size_t HDR_SZ = 4; if (!Buf.Data) return NULL; return (T*)(void*)(Buf.Data + HDR_SZ); } + T* next_chunk(T* p) { size_t HDR_SZ = 4; IM_ASSERT(p >= begin() && p < end()); p = (T*)(void*)((char*)(void*)p + chunk_size(p)); if (p == (T*)(void*)((char*)end() + HDR_SZ)) return (T*)0; IM_ASSERT(p < end()); return p; } + int chunk_size(const T* p) { return ((const int*)p)[-1]; } + T* end() { return (T*)(void*)(Buf.Data + Buf.Size); } + int offset_from_ptr(const T* p) { IM_ASSERT(p >= begin() && p < end()); const ptrdiff_t off = (const char*)p - Buf.Data; return (int)off; } + T* ptr_from_offset(int off) { IM_ASSERT(off >= 4 && off < Buf.Size); return (T*)(void*)(Buf.Data + off); } }; //----------------------------------------------------------------------------- @@ -314,20 +435,30 @@ enum ImGuiButtonFlags_ { ImGuiButtonFlags_None = 0, ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat - ImGuiButtonFlags_PressedOnClickRelease = 1 << 1, // [Default] return true on click + release on same item - ImGuiButtonFlags_PressedOnClick = 1 << 2, // return true on click (default requires click+release) - ImGuiButtonFlags_PressedOnRelease = 1 << 3, // return true on release (default requires click+release) - ImGuiButtonFlags_PressedOnDoubleClick = 1 << 4, // return true on double-click (default requires click+release) - ImGuiButtonFlags_FlattenChildren = 1 << 5, // allow interactions even if a child window is overlapping - ImGuiButtonFlags_AllowItemOverlap = 1 << 6, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() - ImGuiButtonFlags_DontClosePopups = 1 << 7, // disable automatically closing parent popup on press // [UNUSED] - ImGuiButtonFlags_Disabled = 1 << 8, // disable interactions - ImGuiButtonFlags_AlignTextBaseLine = 1 << 9, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine - ImGuiButtonFlags_NoKeyModifiers = 1 << 10, // disable interaction if a key modifier is held - ImGuiButtonFlags_NoHoldingActiveID = 1 << 11, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) - ImGuiButtonFlags_PressedOnDragDropHold = 1 << 12, // press when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) - ImGuiButtonFlags_NoNavFocus = 1 << 13, // don't override navigation focus when activated - ImGuiButtonFlags_NoHoveredOnNav = 1 << 14 // don't report as hovered when navigated on + ImGuiButtonFlags_PressedOnClick = 1 << 1, // return true on click (mouse down event) + ImGuiButtonFlags_PressedOnClickRelease = 1 << 2, // [Default] return true on click + release on same item <-- this is what the majority of Button are using + ImGuiButtonFlags_PressedOnClickReleaseAnywhere = 1 << 3, // return true on click + release even if the release event is not done while hovering the item + ImGuiButtonFlags_PressedOnRelease = 1 << 4, // return true on release (default requires click+release) + ImGuiButtonFlags_PressedOnDoubleClick = 1 << 5, // return true on double-click (default requires click+release) + ImGuiButtonFlags_PressedOnDragDropHold = 1 << 6, // return true when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) + ImGuiButtonFlags_FlattenChildren = 1 << 7, // allow interactions even if a child window is overlapping + ImGuiButtonFlags_AllowItemOverlap = 1 << 8, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() + ImGuiButtonFlags_DontClosePopups = 1 << 9, // disable automatically closing parent popup on press // [UNUSED] + ImGuiButtonFlags_Disabled = 1 << 10, // disable interactions + ImGuiButtonFlags_AlignTextBaseLine = 1 << 11, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine + ImGuiButtonFlags_NoKeyModifiers = 1 << 12, // disable mouse interaction if a key modifier is held + ImGuiButtonFlags_NoHoldingActiveId = 1 << 13, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) + ImGuiButtonFlags_NoNavFocus = 1 << 14, // don't override navigation focus when activated + ImGuiButtonFlags_NoHoveredOnFocus = 1 << 15, // don't report as hovered when nav focus is on this item + ImGuiButtonFlags_MouseButtonLeft = 1 << 16, // [Default] react on left mouse button + ImGuiButtonFlags_MouseButtonRight = 1 << 17, // react on right mouse button + ImGuiButtonFlags_MouseButtonMiddle = 1 << 18, // react on center mouse button + + ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, + ImGuiButtonFlags_MouseButtonShift_ = 16, + ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, + ImGuiButtonFlags_PressedOnMask_ = ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_PressedOnDragDropHold, + ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease }; enum ImGuiSliderFlags_ @@ -358,8 +489,8 @@ enum ImGuiSelectableFlagsPrivate_ { // NB: need to be in sync with last value of ImGuiSelectableFlags_ ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, - ImGuiSelectableFlags_PressedOnClick = 1 << 21, - ImGuiSelectableFlags_PressedOnRelease = 1 << 22, + ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release) + ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release) ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 23, // FIXME: We may be able to remove this (added in 6251d379 for menus) ImGuiSelectableFlags_DrawHoveredWhenHeld= 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25 @@ -402,8 +533,9 @@ enum ImGuiItemStatusFlags_ ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. - ImGuiItemStatusFlags_HasDeactivated = 1 << 4, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. - ImGuiItemStatusFlags_Deactivated = 1 << 5 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. + ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. + ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. + ImGuiItemStatusFlags_Deactivated = 1 << 6 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. #ifdef IMGUI_ENABLE_TEST_ENGINE , // [imgui_tests only] @@ -420,6 +552,12 @@ enum ImGuiTextFlags_ ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0 }; +enum ImGuiTooltipFlags_ +{ + ImGuiTooltipFlags_None = 0, + ImGuiTooltipFlags_OverridePreviousTooltip = 1 << 0 // Override will clear/ignore previously submitted tooltip (defaults to append) +}; + // FIXME: this is in development, not exposed/functional as a generic feature yet. // Horizontal/Vertical enums are fixed to 0/1 so they may be used to index ImVec2 enum ImGuiLayoutType_ @@ -533,8 +671,9 @@ struct ImVec1 struct ImVec2ih { short x, y; - ImVec2ih() { x = y = 0; } - ImVec2ih(short _x, short _y) { x = _x; y = _y; } + ImVec2ih() { x = y = 0; } + ImVec2ih(short _x, short _y) { x = _x; y = _y; } + explicit ImVec2ih(const ImVec2& rhs) { x = (short)rhs.x; y = (short)rhs.y; } }; // 2D axis aligned bounding-box @@ -544,7 +683,7 @@ struct IMGUI_API ImRect ImVec2 Min; // Upper-left ImVec2 Max; // Lower-right - ImRect() : Min(FLT_MAX,FLT_MAX), Max(-FLT_MAX,-FLT_MAX) {} + ImRect() : Min(0.0f, 0.0f), Max(0.0f, 0.0f) {} ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} @@ -569,7 +708,7 @@ struct IMGUI_API ImRect void TranslateY(float dy) { Min.y += dy; Max.y += dy; } void ClipWith(const ImRect& r) { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); } // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display. void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. - void Floor() { Min.x = (float)(int)Min.x; Min.y = (float)(int)Min.y; Max.x = (float)(int)Max.x; Max.y = (float)(int)Max.y; } + void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); } bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } }; @@ -622,14 +761,15 @@ struct IMGUI_API ImGuiMenuColumns ImGuiMenuColumns(); void Update(int count, float spacing, bool clear); float DeclColumns(float w0, float w1, float w2); - float CalcExtraSpace(float avail_w); + float CalcExtraSpace(float avail_w) const; }; // Internal state of the currently focused/edited text input box +// For a given item ID, access with ImGui::GetInputTextState() struct IMGUI_API ImGuiInputTextState { ImGuiID ID; // widget id owning the text state - int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 len is valid even if TextA is not. + int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not. ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. ImVector TextA; // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity. ImVector InitialTextA; // backup of end-user buffer at the time of focus (in UTF-8, unaltered) @@ -660,15 +800,17 @@ struct IMGUI_API ImGuiInputTextState }; // Windows data saved in imgui.ini file +// Because we never destroy or rename ImGuiWindowSettings, we can store the names in a separate buffer easily. +// (this is designed to be stored in a ImChunkStream buffer, with the variable-length Name following our structure) struct ImGuiWindowSettings { - char* Name; ImGuiID ID; ImVec2ih Pos; ImVec2ih Size; bool Collapsed; - ImGuiWindowSettings() { Name = NULL; ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; } + ImGuiWindowSettings() { ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; } + char* GetName() { return (char*)(this + 1); } }; struct ImGuiSettingsHandler @@ -722,6 +864,7 @@ struct ImGuiColumns ImRect HostClipRect; // Backup of ClipRect at the time of BeginColumns() ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() ImVector Columns; + ImDrawListSplitter Splitter; ImGuiColumns() { Clear(); } void Clear() @@ -740,21 +883,34 @@ struct ImGuiColumns } }; +// ImDrawList: Helper function to calculate a circle's segment count given its radius and a "maximum error" value. +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 12 +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp((int)((IM_PI * 2.0f) / ImAcos(((_RAD) - (_MAXERROR)) / (_RAD))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) + +// ImDrawList: You may set this to higher values (e.g. 2 or 3) to increase tessellation of fast rounded corners path. +#ifndef IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER +#define IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER 1 +#endif + // Data shared between all ImDrawList instances +// You may want to create your own instance of this if you want to use ImDrawList completely without ImGui. In that case, watch out for future changes to this structure. struct IMGUI_API ImDrawListSharedData { ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas ImFont* Font; // Current/default font (optional, for simplified AddText overload) float FontSize; // Current/default font size (optional, for simplified AddText overload) - float CurveTessellationTol; + float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() + float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) - // Const data - // FIXME: Bake rounded corners fill/borders in atlas - ImVec2 CircleVtx12[12]; + // [Internal] Lookup tables + ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas + ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead) ImDrawListSharedData(); + void SetCircleSegmentMaxError(float max_error); }; struct ImDrawDataBuilder @@ -768,16 +924,16 @@ struct ImDrawDataBuilder struct ImGuiNavMoveResult { - ImGuiID ID; // Best candidate - ImGuiID SelectScopeId;// Best candidate window current selectable group ID - ImGuiWindow* Window; // Best candidate window - float DistBox; // Best candidate box distance to current NavId - float DistCenter; // Best candidate center distance to current NavId - float DistAxial; - ImRect RectRel; // Best candidate bounding box in window relative space + ImGuiWindow* Window; // Best candidate window + ImGuiID ID; // Best candidate ID + ImGuiID FocusScopeId; // Best candidate focus scope ID + float DistBox; // Best candidate box distance to current NavId + float DistCenter; // Best candidate center distance to current NavId + float DistAxial; + ImRect RectRel; // Best candidate bounding box in window relative space ImGuiNavMoveResult() { Clear(); } - void Clear() { ID = SelectScopeId = 0; Window = NULL; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); } + void Clear() { Window = NULL; ID = FocusScopeId = 0; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); } }; enum ImGuiNextWindowDataFlags_ @@ -807,7 +963,7 @@ struct ImGuiNextWindowData ImRect SizeConstraintRect; ImGuiSizeCallback SizeCallback; void* SizeCallbackUserData; - float BgAlphaVal; + float BgAlphaVal; // Override background alpha ImVec2 MenuBarOffsetMinVal; // *Always on* This is not exposed publicly, so we don't clear it. ImGuiNextWindowData() { memset(this, 0, sizeof(*this)); } @@ -824,12 +980,13 @@ enum ImGuiNextItemDataFlags_ struct ImGuiNextItemData { ImGuiNextItemDataFlags Flags; - float Width; // Set by SetNextItemWidth(). - bool OpenVal; // Set by SetNextItemOpen() function. + float Width; // Set by SetNextItemWidth() + ImGuiID FocusScopeId; // Set by SetNextItemMultiSelectData() (!= 0 signify value has been set, so it's an alternate version of HasSelectionData, we don't use Flags for this because they are cleared too early. This is mostly used for debugging) ImGuiCond OpenCond; + bool OpenVal; // Set by SetNextItemOpen() ImGuiNextItemData() { memset(this, 0, sizeof(*this)); } - inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } + inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } // Also cleared manually by ItemAdd()! }; //----------------------------------------------------------------------------- @@ -852,15 +1009,13 @@ struct ImGuiPtrOrIndex }; //----------------------------------------------------------------------------- -// Main imgui context +// Main Dear ImGui context //----------------------------------------------------------------------------- struct ImGuiContext { bool Initialized; - bool FrameScopeActive; // Set by NewFrame(), cleared by EndFrame() - bool FrameScopePushedImplicitWindow; // Set by NewFrame(), cleared by EndFrame() - bool FontAtlasOwnedByContext; // Io.Fonts-> is owned by the ImGuiContext and will be destructed along with it. + bool FontAtlasOwnedByContext; // IO.Fonts-> is owned by the ImGuiContext and will be destructed along with it. ImGuiIO IO; ImGuiStyle Style; ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() @@ -871,19 +1026,22 @@ struct ImGuiContext int FrameCount; int FrameCountEnded; int FrameCountRendered; + bool WithinFrameScope; // Set by NewFrame(), cleared by EndFrame() + bool WithinFrameScopeWithImplicitWindow; // Set by NewFrame(), cleared by EndFrame() when the implicit debug window has been pushed + bool WithinEndChild; // Set within EndChild() // Windows state ImVector Windows; // Windows, sorted in display order, back to front - ImVector WindowsFocusOrder; // Windows, sorted in focus order, back to front - ImVector WindowsSortBuffer; + ImVector WindowsFocusOrder; // Windows, sorted in focus order, back to front. (FIXME: We could only store root windows here! Need to sort out the Docking equivalent which is RootWindowDockStop and is unfortunately a little more dynamic) + ImVector WindowsTempSortBuffer; // Temporary buffer used in EndFrame() to reorder windows so parents are kept before their child ImVector CurrentWindowStack; - ImGuiStorage WindowsById; - int WindowsActiveCount; - ImGuiWindow* CurrentWindow; // Being drawn into + ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow* + int WindowsActiveCount; // Number of unique windows submitted by frame + ImGuiWindow* CurrentWindow; // Window being drawn into ImGuiWindow* HoveredWindow; // Will catch mouse inputs ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only) ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow. - ImGuiWindow* WheelingWindow; + ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. ImVec2 WheelingWindowRefMousePos; float WheelingWindowTimer; @@ -907,11 +1065,11 @@ struct ImGuiContext ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) + int ActiveIdMouseButton; ImGuiID ActiveIdPreviousFrame; bool ActiveIdPreviousFrameIsAlive; bool ActiveIdPreviousFrameHasBeenEditedBefore; ImGuiWindow* ActiveIdPreviousFrameWindow; - ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. @@ -926,26 +1084,21 @@ struct ImGuiContext ImVectorOpenPopupStack; // Which popups are open (persistent) ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) - // Navigation data (for gamepad/keyboard) + // Gamepad/keyboard Navigation ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' ImGuiID NavId; // Focused item for navigation + ImGuiID NavFocusScopeId; ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0 ImGuiID NavJustTabbedId; // Just tabbed to this id. ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). - ImGuiID NavJustMovedToMultiSelectScopeId; // Just navigated to this select scope id (result of a successfully MoveRequest). + ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. ImRect NavScoringRectScreen; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. int NavScoringCount; // Metrics for debugging - ImGuiWindow* NavWindowingTarget; // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed top-most. - ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f - ImGuiWindow* NavWindowingList; - float NavWindowingTimer; - float NavWindowingHighlightAlpha; - bool NavWindowingToggleLayer; ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRefRectRel is valid @@ -967,13 +1120,21 @@ struct ImGuiContext ImGuiNavMoveResult NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) ImGuiNavMoveResult NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) - // Tabbing system (older than Nav, active even if Nav is disabled. FIXME-NAV: This needs a redesign!) + // Navigation: Windowing (CTRL+TAB, holding Menu button + directional pads to move/resize) + ImGuiWindow* NavWindowingTarget; // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed top-most. + ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f + ImGuiWindow* NavWindowingList; + float NavWindowingTimer; + float NavWindowingHighlightAlpha; + bool NavWindowingToggleLayer; + + // Legacy Focus/Tabbing system (older than Nav, active even if Nav is disabled, misnamed. FIXME-NAV: This needs a redesign!) ImGuiWindow* FocusRequestCurrWindow; // ImGuiWindow* FocusRequestNextWindow; // - int FocusRequestCurrCounterAll; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) - int FocusRequestCurrCounterTab; // Tab item being requested for focus, stored as an index - int FocusRequestNextCounterAll; // Stored for next frame - int FocusRequestNextCounterTab; // " + int FocusRequestCurrCounterRegular; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) + int FocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index + int FocusRequestNextCounterRegular; // Stored for next frame + int FocusRequestNextCounterTabStop; // " bool FocusTabPressed; // // Render @@ -986,20 +1147,21 @@ struct ImGuiContext // Drag and Drop bool DragDropActive; - bool DragDropWithinSourceOrTarget; + bool DragDropWithinSource; // Set when within a BeginDragDropXXX/EndDragDropXXX block for a drag source. + bool DragDropWithinTarget; // Set when within a BeginDragDropXXX/EndDragDropXXX block for a drag target. ImGuiDragDropFlags DragDropSourceFlags; int DragDropSourceFrameCount; int DragDropMouseButton; ImGuiPayload DragDropPayload; - ImRect DragDropTargetRect; + ImRect DragDropTargetRect; // Store rectangle of current target candidate (we favor small targets when overlapping) ImGuiID DragDropTargetId; ImGuiDragDropFlags DragDropAcceptFlags; float DragDropAcceptIdCurrRectSurface; // Target item surface (we resolve overlapping targets by prioritizing the smaller surface) ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload) ImGuiID DragDropAcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets) int DragDropAcceptFrameCount; // Last time a target expressed a desire to accept the source - ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly - unsigned char DragDropPayloadBufLocal[8]; // Local buffer for small payloads + ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly, ImGuiPayload only holds pointer+size + unsigned char DragDropPayloadBufLocal[16]; // Local buffer for small payloads // Tab bars ImGuiTabBar* CurrentTabBar; @@ -1011,37 +1173,35 @@ struct ImGuiContext ImVec2 LastValidMousePos; ImGuiInputTextState InputTextState; ImFont InputTextPasswordFont; - ImGuiID TempInputTextId; // Temporary text input when CTRL+clicking on a slider, etc. + ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets - float ColorEditLastHue; + float ColorEditLastHue; // Backup of last Hue associated to LastColor[3], so we can restore Hue in lossy RGB<>HSV round trips + float ColorEditLastSat; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips float ColorEditLastColor[3]; - ImVec4 ColorPickerRef; + ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; ImVector PrivateClipboard; // If no custom clipboard handler is defined - - // Range-Select/Multi-Select - // [This is unused in this branch, but left here to facilitate merging/syncing multiple branches] - ImGuiID MultiSelectScopeId; + ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once // Platform support ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor ImVec2 PlatformImeLastPos; // Settings - bool SettingsLoaded; - float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero - ImGuiTextBuffer SettingsIniData; // In memory .ini settings - ImVector SettingsHandlers; // List of .ini settings handlers - ImVector SettingsWindows; // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving) + bool SettingsLoaded; + float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero + ImGuiTextBuffer SettingsIniData; // In memory .ini settings + ImVector SettingsHandlers; // List of .ini settings handlers + ImChunkStream SettingsWindows; // ImGuiWindow .ini settings entries - // Logging + // Capture/Logging bool LogEnabled; ImGuiLogType LogType; - FILE* LogFile; // If != NULL log to stdout/ file + ImFileHandle LogFile; // If != NULL log to stdout/ file ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. float LogLinePosY; bool LogLineFirstItem; @@ -1051,7 +1211,7 @@ struct ImGuiContext // Debug Tools bool DebugItemPickerActive; - ImGuiID DebugItemPickerBreakID; // Will call IM_DEBUG_BREAK() when encountering this id + ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this id // Misc float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. @@ -1065,7 +1225,6 @@ struct ImGuiContext ImGuiContext(ImFontAtlas* shared_font_atlas) : BackgroundDrawList(&DrawListSharedData), ForegroundDrawList(&DrawListSharedData) { Initialized = false; - FrameScopeActive = FrameScopePushedImplicitWindow = false; Font = NULL; FontSize = FontBaseSize = 0.0f; FontAtlasOwnedByContext = shared_font_atlas ? false : true; @@ -1073,6 +1232,7 @@ struct ImGuiContext Time = 0.0f; FrameCount = 0; FrameCountEnded = FrameCountRendered = -1; + WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; WindowsActiveCount = 0; CurrentWindow = NULL; @@ -1100,24 +1260,20 @@ struct ImGuiContext ActiveIdClickOffset = ImVec2(-1,-1); ActiveIdWindow = NULL; ActiveIdSource = ImGuiInputSource_None; - + ActiveIdMouseButton = 0; ActiveIdPreviousFrame = 0; ActiveIdPreviousFrameIsAlive = false; ActiveIdPreviousFrameHasBeenEditedBefore = false; ActiveIdPreviousFrameWindow = NULL; - LastActiveId = 0; LastActiveIdTimer = 0.0f; NavWindow = NULL; - NavId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; - NavJustTabbedId = NavJustMovedToId = NavJustMovedToMultiSelectScopeId = NavNextActivateId = 0; + NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; + NavJustTabbedId = NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavInputSource = ImGuiInputSource_None; NavScoringRectScreen = ImRect(); NavScoringCount = 0; - NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL; - NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; - NavWindowingToggleLayer = false; NavLayer = ImGuiNavLayer_Main; NavIdTabCounter = INT_MAX; NavIdIsAlive = false; @@ -1130,13 +1286,17 @@ struct ImGuiContext NavInitResultId = 0; NavMoveFromClampedRefRect = false; NavMoveRequest = false; - NavMoveRequestFlags = 0; + NavMoveRequestFlags = ImGuiNavMoveFlags_None; NavMoveRequestForward = ImGuiNavForward_None; NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None; + NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL; + NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; + NavWindowingToggleLayer = false; + FocusRequestCurrWindow = FocusRequestNextWindow = NULL; - FocusRequestCurrCounterAll = FocusRequestCurrCounterTab = INT_MAX; - FocusRequestNextCounterAll = FocusRequestNextCounterTab = INT_MAX; + FocusRequestCurrCounterRegular = FocusRequestCurrCounterTabStop = INT_MAX; + FocusRequestNextCounterRegular = FocusRequestNextCounterTabStop = INT_MAX; FocusTabPressed = false; DimBgRatio = 0.0f; @@ -1144,12 +1304,12 @@ struct ImGuiContext ForegroundDrawList._OwnerName = "##Foreground"; // Give it a name for debugging MouseCursor = ImGuiMouseCursor_Arrow; - DragDropActive = DragDropWithinSourceOrTarget = false; - DragDropSourceFlags = 0; + DragDropActive = DragDropWithinSource = DragDropWithinTarget = false; + DragDropSourceFlags = ImGuiDragDropFlags_None; DragDropSourceFrameCount = -1; DragDropMouseButton = -1; DragDropTargetId = 0; - DragDropAcceptFlags = 0; + DragDropAcceptFlags = ImGuiDragDropFlags_None; DragDropAcceptIdCurrRectSurface = 0.0f; DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; DragDropAcceptFrameCount = -1; @@ -1158,9 +1318,9 @@ struct ImGuiContext CurrentTabBar = NULL; LastValidMousePos = ImVec2(0.0f, 0.0f); - TempInputTextId = 0; + TempInputId = 0; ColorEditOptions = ImGuiColorEditFlags__OptionsDefault; - ColorEditLastHue = 0.0f; + ColorEditLastHue = ColorEditLastSat = 0.0f; ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; @@ -1168,8 +1328,6 @@ struct ImGuiContext ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; - MultiSelectScopeId = 0; - PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); SettingsLoaded = false; @@ -1184,7 +1342,7 @@ struct ImGuiContext LogDepthToExpand = LogDepthToExpandDefault = 2; DebugItemPickerActive = false; - DebugItemPickerBreakID = 0; + DebugItemPickerBreakId = 0; memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); FramerateSecPerFrameIdx = 0; @@ -1202,6 +1360,7 @@ struct ImGuiContext // FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered. struct IMGUI_API ImGuiWindowTempData { + // Layout ImVec2 CursorPos; // Current emitting position, in absolute coordinates. ImVec2 CursorPosPrevLine; ImVec2 CursorStartPos; // Initial position after Begin(), generally ~ window position + WindowPadding. @@ -1210,27 +1369,40 @@ struct IMGUI_API ImGuiWindowTempData ImVec2 PrevLineSize; float CurrLineTextBaseOffset; // Baseline offset (0.0f by default on a new line, generally == style.FramePadding.y when a framed item has been added). float PrevLineTextBaseOffset; - int TreeDepth; // Current tree depth. - ImU32 TreeMayJumpToParentOnPopMask; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. + ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) + ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. + ImVec1 GroupOffset; + + // Last item status ImGuiID LastItemId; // ID for last item ImGuiItemStatusFlags LastItemStatusFlags; // Status flags for last item (see ImGuiItemStatusFlags_) ImRect LastItemRect; // Interaction rect for last item ImRect LastItemDisplayRect; // End-user display rect for last item (only valid if LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) + + // Keyboard/Gamepad navigation ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) int NavLayerCurrentMask; // = (1 << NavLayerCurrent) used by ItemAdd prior to clipping. int NavLayerActiveMask; // Which layer have been written to (result from previous frame) int NavLayerActiveMaskNext; // Which layer have been written to (buffer for current frame) + ImGuiID NavFocusScopeIdCurrent; // Current focus scope ID while appending bool NavHideHighlightOneFrame; bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) + + // Miscellaneous bool MenuBarAppending; // FIXME: Remove this ImVec2 MenuBarOffset; // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs. + ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items measurement + int TreeDepth; // Current tree depth. + ImU32 TreeJumpToParentOnPopMask; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. ImVector ChildWindows; ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state) + ImGuiColumns* CurrentColumns; // Current columns set ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() - int FocusCounterAll; // Counter for focus/tabbing system. Start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) - int FocusCounterTab; // (same, but only count widgets which you can Tab through) + int FocusCounterRegular; // (Legacy Focus/Tabbing system) Sequential counter, start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) + int FocusCounterTabStop; // (Legacy Focus/Tabbing system) Same, but only count widgets which you can Tab through. + // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. ImGuiItemFlags ItemFlags; // == ItemFlagsStack.back() [empty == ImGuiItemFlags_Default] float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window @@ -1241,49 +1413,47 @@ struct IMGUI_API ImGuiWindowTempData ImVectorGroupStack; short StackSizesBackup[6]; // Store size of various stacks for asserting - ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) - ImVec1 GroupOffset; - ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. - ImGuiColumns* CurrentColumns; // Current columns set - ImGuiWindowTempData() { CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f); CurrLineSize = PrevLineSize = ImVec2(0.0f, 0.0f); CurrLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f; - TreeDepth = 0; - TreeMayJumpToParentOnPopMask = 0x00; + Indent = ImVec1(0.0f); + ColumnsOffset = ImVec1(0.0f); + GroupOffset = ImVec1(0.0f); + LastItemId = 0; - LastItemStatusFlags = 0; + LastItemStatusFlags = ImGuiItemStatusFlags_None; LastItemRect = LastItemDisplayRect = ImRect(); + NavLayerActiveMask = NavLayerActiveMaskNext = 0x00; NavLayerCurrent = ImGuiNavLayer_Main; NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); + NavFocusScopeIdCurrent = 0; NavHideHighlightOneFrame = false; NavHasScroll = false; + MenuBarAppending = false; MenuBarOffset = ImVec2(0.0f, 0.0f); + TreeDepth = 0; + TreeJumpToParentOnPopMask = 0x00; StateStorage = NULL; + CurrentColumns = NULL; LayoutType = ParentLayoutType = ImGuiLayoutType_Vertical; - FocusCounterAll = FocusCounterTab = -1; + FocusCounterRegular = FocusCounterTabStop = -1; ItemFlags = ImGuiItemFlags_Default_; ItemWidth = 0.0f; TextWrapPos = -1.0f; memset(StackSizesBackup, 0, sizeof(StackSizesBackup)); - - Indent = ImVec1(0.0f); - GroupOffset = ImVec1(0.0f); - ColumnsOffset = ImVec1(0.0f); - CurrentColumns = NULL; } }; // Storage for one window struct IMGUI_API ImGuiWindow { - char* Name; - ImGuiID ID; // == ImHash(Name) + char* Name; // Window name, owned by the window. + ImGuiID ID; // == ImHashStr(Name) ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_ ImVec2 Pos; // Position (always rounded-up to nearest pixel) ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) @@ -1310,6 +1480,7 @@ struct IMGUI_API ImGuiWindow bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed) bool Appearing; // Set during the frame where the window is appearing (or re-appearing) bool Hidden; // Do not display (== (HiddenFrames*** > 0)) + bool IsFallbackWindow; // Set on the "Debug##Default" window. bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) @@ -1336,18 +1507,17 @@ struct IMGUI_API ImGuiWindow ImRect OuterRectClipped; // == Window->Rect() just after setup in Begin(). == window->Rect() for root window. ImRect InnerRect; // Inner rectangle (omit title bar, menu bar, scroll bar) ImRect InnerClipRect; // == InnerRect shrunk by WindowPadding*0.5f on each side, clipped within viewport or parent clip rect. - ImRect WorkRect; // Cover the whole scrolling region, shrunk by WindowPadding*1.0f on each side. This is meant to replace ContentsRegionRect over time (from 1.71+ onward). + ImRect WorkRect; // Cover the whole scrolling region, shrunk by WindowPadding*1.0f on each side. This is meant to replace ContentRegionRect over time (from 1.71+ onward). ImRect ClipRect; // Current clipping/scissoring rectangle, evolve as we are using PushClipRect(), etc. == DrawList->clip_rect_stack.back(). - ImRect ContentsRegionRect; // FIXME: This is currently confusing/misleading. It is essentially WorkRect but not handling of scrolling. We currently rely on it as right/bottom aligned sizing operation need some size to rely on. + ImRect ContentRegionRect; // FIXME: This is currently confusing/misleading. It is essentially WorkRect but not handling of scrolling. We currently rely on it as right/bottom aligned sizing operation need some size to rely on. int LastFrameActive; // Last frame number the window was Active. float LastTimeActive; // Last timestamp the window was Active (using float as we don't need high precision there) float ItemWidthDefault; - ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items ImGuiStorage StateStorage; ImVector ColumnsStorage; float FontWindowScale; // User scale multiplier per-window, via SetWindowFontScale() - int SettingsIdx; // Index into SettingsWindow[] (indices are always valid as we only grow the array from the back) + int SettingsOffset; // Offset into SettingsWindows[] (offsets are always valid as we only grow the array from the back) ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) ImDrawList DrawListInst; @@ -1413,7 +1583,7 @@ enum ImGuiTabBarFlagsPrivate_ // Extend ImGuiTabItemFlags_ enum ImGuiTabItemFlagsPrivate_ { - ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Store whether p_open is set or not, which we need to recompute WidthContents during layout. + ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) }; // Storage for one active tab item (sizeof() 26~32 bytes) @@ -1426,9 +1596,9 @@ struct ImGuiTabItem int NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames float Offset; // Position relative to beginning of tab float Width; // Width currently displayed - float WidthContents; // Width of actual contents, stored during BeginTabItem() call + float ContentWidth; // Width of actual contents, stored during BeginTabItem() call - ImGuiTabItem() { ID = Flags = 0; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = WidthContents = 0.0f; } + ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; } }; // Storage for a tab bar (sizeof() 92~96 bytes) @@ -1436,7 +1606,7 @@ struct ImGuiTabBar { ImVector Tabs; ImGuiID ID; // Zero for tab-bars used by docking - ImGuiID SelectedTabId; // Selected tab + ImGuiID SelectedTabId; // Selected tab/window ImGuiID NextSelectedTabId; ImGuiID VisibleTabId; // Can occasionally be != SelectedTabId (e.g. when previewing contents for CTRL+TAB preview) int CurrFrameVisible; @@ -1475,6 +1645,7 @@ struct ImGuiTabBar namespace ImGui { + // Windows // We should always have a CurrentWindow in the stack (there is an implicit "Debug" window) // If this ever crash because g.CurrentWindow is NULL it means that either // - ImGui::NewFrame() has never been called, which is illegal. @@ -1483,11 +1654,6 @@ namespace ImGui inline ImGuiWindow* GetCurrentWindow() { ImGuiContext& g = *GImGui; g.CurrentWindow->WriteAccessed = true; return g.CurrentWindow; } IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id); IMGUI_API ImGuiWindow* FindWindowByName(const char* name); - IMGUI_API void FocusWindow(ImGuiWindow* window); - IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window); - IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); - IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); - IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); IMGUI_API ImVec2 CalcWindowExpectedSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); @@ -1496,12 +1662,18 @@ namespace ImGui IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); - IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); - IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); + // Windows: Display Order and Focus Order + IMGUI_API void FocusWindow(ImGuiWindow* window); + IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window); + IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); + IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); + IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); + + // Fonts, drawing IMGUI_API void SetCurrentFont(ImFont* font); inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; } - inline ImDrawList* GetForegroundDrawList(ImGuiWindow*) { ImGuiContext& g = *GImGui; return &g.ForegroundDrawList; } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. + inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { IM_UNUSED(window); ImGuiContext& g = *GImGui; return &g.ForegroundDrawList; } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. // Init IMGUI_API void Initialize(ImGuiContext* context); @@ -1530,6 +1702,7 @@ namespace ImGui // Basic Accessors inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } + inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemStatusFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); @@ -1538,12 +1711,12 @@ namespace ImGui IMGUI_API ImGuiID GetHoveredID(); IMGUI_API void SetHoveredID(ImGuiID id); IMGUI_API void KeepAliveID(ImGuiID id); - IMGUI_API void MarkItemEdited(ImGuiID id); - IMGUI_API void PushOverrideID(ImGuiID id); + IMGUI_API void MarkItemEdited(ImGuiID id); // Mark data associated to given item as "edited", used by IsItemDeactivatedAfterEdit() function. + IMGUI_API void PushOverrideID(ImGuiID id); // Push given value at the top of the ID stack (whereas PushID combines old and new hashes) // Basic Helpers for widget code - IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = 0.0f); - IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = 0.0f); + IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); + IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f); IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); @@ -1563,12 +1736,13 @@ namespace ImGui IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer // Popups, Modals, Tooltips + IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); IMGUI_API void OpenPopupEx(ImGuiID id); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id within current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack! IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); - IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, bool override_previous_tooltip = true); + IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy = ImGuiPopupPositionPolicy_Default); @@ -1583,26 +1757,30 @@ namespace ImGui IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. - IMGUI_API void SetNavID(ImGuiID id, int nav_layer); - IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, const ImRect& rect_rel); + IMGUI_API void SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id); + IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); + + // Focus scope (WIP) + IMGUI_API void PushFocusScope(ImGuiID id); // Note: this is storing in same stack as IDStack, so Push/Pop mismatch will be reported there. + IMGUI_API void PopFocusScope(); + inline ImGuiID GetFocusScopeID() { ImGuiContext& g = *GImGui; return g.NavFocusScopeId; } // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; } inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; } - IMGUI_API bool IsMouseDragPastThreshold(int button, float lock_threshold = -1.0f); + IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f); inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { ImGuiContext& g = *GImGui; const int key_index = g.IO.KeyMap[key]; return (key_index >= 0) ? IsKeyPressed(key_index, repeat) : false; } inline bool IsNavInputDown(ImGuiNavInput n) { ImGuiContext& g = *GImGui; return g.IO.NavInputs[n] > 0.0f; } - inline bool IsNavInputPressed(ImGuiNavInput n, ImGuiInputReadMode mode) { return GetNavInputAmount(n, mode) > 0.0f; } - inline bool IsNavInputPressedAnyOfTwo(ImGuiNavInput n1, ImGuiNavInput n2, ImGuiInputReadMode mode) { return (GetNavInputAmount(n1, mode) + GetNavInputAmount(n2, mode)) > 0.0f; } + inline bool IsNavInputTest(ImGuiNavInput n, ImGuiInputReadMode rm) { return (GetNavInputAmount(n, rm) > 0.0f); } // Drag and Drop IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); IMGUI_API void ClearDragDrop(); IMGUI_API bool IsDragDropPayloadBeingAccepted(); - // New Columns API (FIXME-WIP) + // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables api) IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). IMGUI_API void EndColumns(); // close columns IMGUI_API void PushColumnClipRect(int column_index); @@ -1658,10 +1836,11 @@ namespace ImGui IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0); IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); - IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags); + IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawCornerFlags rounding_corners); - IMGUI_API ImGuiID GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis); + IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); + IMGUI_API ImGuiID GetWindowResizeID(ImGuiWindow* window, int n); // 0..3: corners, 4..7: borders IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); // Widgets low-level behaviors @@ -1689,8 +1868,10 @@ namespace ImGui // InputText IMGUI_API bool InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); - IMGUI_API bool TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format); - inline bool TempInputTextIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputTextId == id); } + IMGUI_API bool TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags); + IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format); + inline bool TempInputIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputId == id); } + inline ImGuiInputTextState* GetInputTextState(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.InputTextState.ID == id) ? &g.InputTextState : NULL; } // Get input text state if active // Color IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags); @@ -1704,6 +1885,10 @@ namespace ImGui IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1); IMGUI_API void ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp); + // Garbage collection + IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); + IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); + // Debug Tools inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max, col); } inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } @@ -1712,7 +1897,7 @@ namespace ImGui // ImFontAtlas internals IMGUI_API bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas); -IMGUI_API void ImFontAtlasBuildRegisterDefaultCustomRects(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); @@ -1757,3 +1942,5 @@ extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const cha #ifdef _MSC_VER #pragma warning (pop) #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index b2398a00..d22c1fa4 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.74 WIP +// dear imgui, v1.76 WIP // (widgets code) /* @@ -33,6 +33,8 @@ Index of this file: #endif #include "imgui.h" +#ifndef IMGUI_DISABLE + #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif @@ -47,8 +49,11 @@ Index of this file: // Visual Studio warnings #ifdef _MSC_VER -#pragma warning (disable: 4127) // condition expression is constant -#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later +#pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types +#endif #endif // Clang/GCC warnings with -Weverything @@ -63,6 +68,9 @@ Index of this file: #if __has_warning("-Wdouble-promotion") #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #endif +#if __has_warning("-Wdeprecated-enum-enum-conversion") +#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated +#endif #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked @@ -213,7 +221,7 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) text_size.y = (pos - text_pos).y; ImRect bb(text_pos, text_pos + text_size); - ItemSize(text_size); + ItemSize(text_size, 0.0f); ItemAdd(bb, 0); } else @@ -222,7 +230,7 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); ImRect bb(text_pos, text_pos + text_size); - ItemSize(text_size); + ItemSize(text_size, 0.0f); if (!ItemAdd(bb, 0)) return; @@ -359,17 +367,18 @@ void ImGui::BulletTextV(const char* fmt, va_list args) const char* text_begin = g.TempBuffer; const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); - const float text_base_offset_y = ImMax(0.0f, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it - const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); - const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x*2) : 0.0f), ImMax(line_height, label_size.y))); // Empty text doesn't add padding - ItemSize(bb); + const ImVec2 total_size = ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x * 2) : 0.0f), label_size.y); // Empty text doesn't add padding + ImVec2 pos = window->DC.CursorPos; + pos.y += window->DC.CurrLineTextBaseOffset; + ItemSize(total_size, 0.0f); + const ImRect bb(pos, pos + total_size); if (!ItemAdd(bb, 0)) return; // Render ImU32 text_col = GetColorU32(ImGuiCol_Text); - RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), text_col); - RenderText(bb.Min+ImVec2(g.FontSize + style.FramePadding.x*2, text_base_offset_y), text_begin, text_end, false); + RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, g.FontSize*0.5f), text_col); + RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0f), text_begin, text_end, false); } //------------------------------------------------------------------------- @@ -460,9 +469,13 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool return false; } - // Default behavior requires click+release on same spot - if ((flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick)) == 0) - flags |= ImGuiButtonFlags_PressedOnClickRelease; + // Default only reacts to left mouse button + if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0) + flags |= ImGuiButtonFlags_MouseButtonDefault_; + + // Default behavior requires click + release inside bounding box + if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0) + flags |= ImGuiButtonFlags_PressedOnDefault_; ImGuiWindow* backup_hovered_window = g.HoveredWindow; const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window; @@ -501,38 +514,55 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0)) hovered = false; - // Mouse + // Mouse handling if (hovered) { if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) { - if ((flags & ImGuiButtonFlags_PressedOnClickRelease) && g.IO.MouseClicked[0]) - { - SetActiveID(id, window); - if (!(flags & ImGuiButtonFlags_NoNavFocus)) - SetFocusID(id, window); - FocusWindow(window); - } - if (((flags & ImGuiButtonFlags_PressedOnClick) && g.IO.MouseClicked[0]) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[0])) + // Poll buttons + int mouse_button_clicked = -1; + int mouse_button_released = -1; + if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseClicked[0]) { mouse_button_clicked = 0; } + else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseClicked[1]) { mouse_button_clicked = 1; } + else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseClicked[2]) { mouse_button_clicked = 2; } + if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseReleased[0]) { mouse_button_released = 0; } + else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseReleased[1]) { mouse_button_released = 1; } + else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseReleased[2]) { mouse_button_released = 2; } + + if (mouse_button_clicked != -1 && g.ActiveId != id) { - pressed = true; - if (flags & ImGuiButtonFlags_NoHoldingActiveID) - ClearActiveID(); - else - SetActiveID(id, window); // Hold on ID - FocusWindow(window); + if (flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere)) + { + SetActiveID(id, window); + g.ActiveIdMouseButton = mouse_button_clicked; + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); + FocusWindow(window); + } + if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[mouse_button_clicked])) + { + pressed = true; + if (flags & ImGuiButtonFlags_NoHoldingActiveId) + ClearActiveID(); + else + SetActiveID(id, window); // Hold on ID + g.ActiveIdMouseButton = mouse_button_clicked; + FocusWindow(window); + } } - if ((flags & ImGuiButtonFlags_PressedOnRelease) && g.IO.MouseReleased[0]) + if ((flags & ImGuiButtonFlags_PressedOnRelease) && mouse_button_released != -1) { - if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay)) // Repeat mode trumps + // Repeat mode trumps on release behavior + if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay)) pressed = true; ClearActiveID(); } // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. - if ((flags & ImGuiButtonFlags_Repeat) && g.ActiveId == id && g.IO.MouseDownDuration[0] > 0.0f && IsMouseClicked(0, true)) - pressed = true; + if (g.ActiveId == id && (flags & ImGuiButtonFlags_Repeat)) + if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, true)) + pressed = true; } if (pressed) @@ -542,13 +572,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Gamepad/Keyboard navigation // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse. if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId)) - if (!(flags & ImGuiButtonFlags_NoHoveredOnNav)) + if (!(flags & ImGuiButtonFlags_NoHoveredOnFocus)) hovered = true; - if (g.NavActivateDownId == id) { bool nav_activated_by_code = (g.NavActivateId == id); - bool nav_activated_by_inputs = IsNavInputPressed(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); + bool nav_activated_by_inputs = IsNavInputTest(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); if (nav_activated_by_code || nav_activated_by_inputs) pressed = true; if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) @@ -564,22 +593,25 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool bool held = false; if (g.ActiveId == id) { - if (pressed) - g.ActiveIdHasBeenPressedBefore = true; if (g.ActiveIdSource == ImGuiInputSource_Mouse) { if (g.ActiveIdIsJustActivated) g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; - if (g.IO.MouseDown[0]) + + const int mouse_button = g.ActiveIdMouseButton; + IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); + if (g.IO.MouseDown[mouse_button]) { held = true; } else { - if (hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease) && !g.DragDropActive) + bool release_in = hovered && (flags & ImGuiButtonFlags_PressedOnClickRelease) != 0; + bool release_anywhere = (flags & ImGuiButtonFlags_PressedOnClickReleaseAnywhere) != 0; + if ((release_in || release_anywhere) && !g.DragDropActive) { - bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[0]; - bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[0] >= g.IO.KeyRepeatDelay; // Repeat mode trumps + bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[mouse_button]; + bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps if (!is_double_click_release && !is_repeating_already) pressed = true; } @@ -593,6 +625,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (g.NavActivateDownId != id) ClearActiveID(); } + if (pressed) + g.ActiveIdHasBeenPressedBefore = true; } if (out_hovered) *out_hovered = hovered; @@ -691,7 +725,7 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu const ImGuiID id = window->GetID(str_id); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); const float default_size = GetFrameHeight(); - ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); + ItemSize(size, (size.y >= default_size) ? g.Style.FramePadding.y : -1.0f); if (!ItemAdd(bb, id)) return false; @@ -714,7 +748,7 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) { float sz = GetFrameHeight(); - return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), 0); + return ArrowButtonEx(str_id, dir, ImVec2(sz, sz), ImGuiButtonFlags_None); } // Button to close a window @@ -767,13 +801,13 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold - if (IsItemActive() && IsMouseDragging()) + if (IsItemActive() && IsMouseDragging(0)) StartMouseMovingWindow(window); return pressed; } -ImGuiID ImGui::GetScrollbarID(ImGuiWindow* window, ImGuiAxis axis) +ImGuiID ImGui::GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis) { return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); } @@ -808,7 +842,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa const bool horizontal = (axis == ImGuiAxis_X); ImRect bb = bb_frame; - bb.Expand(ImVec2(-ImClamp((float)(int)((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp((float)(int)((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); + bb.Expand(ImVec2(-ImClamp(IM_FLOOR((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_FLOOR((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) const float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight(); @@ -851,7 +885,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa // Apply scroll // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); - *p_scroll_v = (float)(int)(0.5f + scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); + *p_scroll_v = IM_ROUND(scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); // Update values for rendering scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); @@ -880,7 +914,7 @@ void ImGui::Scrollbar(ImGuiAxis axis) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - const ImGuiID id = GetScrollbarID(window, axis); + const ImGuiID id = GetWindowScrollbarID(window, axis); KeepAliveID(id); // Calculate scrollbar bounding box @@ -1004,12 +1038,12 @@ bool ImGui::Checkbox(const char* label, bool* v) if (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) { // Undocumented tristate/mixed/indeterminate checkbox (#2644) - ImVec2 pad(ImMax(1.0f, (float)(int)(square_sz / 3.6f)), ImMax(1.0f, (float)(int)(square_sz / 3.6f))); + ImVec2 pad(ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)), ImMax(1.0f, IM_FLOOR(square_sz / 3.6f))); window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); } else if (*v) { - const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); + const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); RenderCheckMark(check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); } @@ -1057,8 +1091,8 @@ bool ImGui::RadioButton(const char* label, bool active) return false; ImVec2 center = check_bb.GetCenter(); - center.x = (float)(int)center.x + 0.5f; - center.y = (float)(int)center.y + 0.5f; + center.x = IM_ROUND(center.x); + center.y = IM_ROUND(center.y); const float radius = (square_sz - 1.0f) * 0.5f; bool hovered, held; @@ -1070,7 +1104,7 @@ bool ImGui::RadioButton(const char* label, bool active) window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16); if (active) { - const float pad = ImMax(1.0f, (float)(int)(square_sz / 6.0f)); + const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16); } @@ -1258,21 +1292,14 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) // We don't provide our width to the layout so that it doesn't get feed back into AutoFit const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness_draw)); ItemSize(ImVec2(0.0f, thickness_layout)); - if (!ItemAdd(bb, 0)) + const bool item_visible = ItemAdd(bb, 0); + if (item_visible) { - if (columns) - { - PopColumnsBackground(); - columns->LineMinY = window->DC.CursorPos.y; - } - return; + // Draw + window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); + if (g.LogEnabled) + LogRenderedText(&bb.Min, "--------------------------------"); } - - // Draw - window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); - if (g.LogEnabled) - LogRenderedText(&bb.Min, "--------------------------------"); - if (columns) { PopColumnsBackground(); @@ -1347,7 +1374,7 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float // Render const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); - window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, g.Style.FrameRounding); + window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, 0.0f); return held; } @@ -2085,7 +2112,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, // Tabbing or CTRL-clicking on Drag turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); - bool temp_input_is_active = TempInputTextIsActive(id); + bool temp_input_is_active = TempInputIsActive(id); bool temp_input_start = false; if (!temp_input_is_active) { @@ -2106,7 +2133,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, } } if (temp_input_is_active || temp_input_start) - return TempInputTextScalar(frame_bb, id, label, data_type, p_data, format); + return TempInputScalar(frame_bb, id, label, data_type, p_data, format); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2473,7 +2500,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ return value_changed; } -// For 32-bits and larger types, slider bounds are limited to half the natural type range. +// For 32-bit and larger types, slider bounds are limited to half the natural type range. // So e.g. an integer Slider between INT_MAX-10 and INT_MAX will fail, but an integer Slider between INT_MAX/2-10 and INT_MAX/2 will be ok. // It would be possible to lift that limitation with some work but it doesn't seem to be worth it for sliders. bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) @@ -2537,7 +2564,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Tabbing or CTRL-clicking on Slider turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); - bool temp_input_is_active = TempInputTextIsActive(id); + bool temp_input_is_active = TempInputIsActive(id); bool temp_input_start = false; if (!temp_input_is_active) { @@ -2557,7 +2584,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat } } if (temp_input_is_active || temp_input_start) - return TempInputTextScalar(frame_bb, id, label, data_type, p_data, format); + return TempInputScalar(frame_bb, id, label, data_type, p_data, format); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2836,32 +2863,39 @@ int ImParseFormatPrecision(const char* fmt, int default_precision) // Create text input in place of another active widget (e.g. used when doing a CTRL+Click on drag/slider widgets) // FIXME: Facilitate using this in variety of other situations. -bool ImGui::TempInputTextScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format) +bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags) { - ImGuiContext& g = *GImGui; - // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id. // We clear ActiveID on the first frame to allow the InputText() taking it back. - const bool init = (g.TempInputTextId != id); + ImGuiContext& g = *GImGui; + const bool init = (g.TempInputId != id); if (init) ClearActiveID(); + g.CurrentWindow->DC.CursorPos = bb.Min; + bool value_changed = InputTextEx(label, NULL, buf, buf_size, bb.GetSize(), flags); + if (init) + { + // First frame we started displaying the InputText widget, we expect it to take the active id. + IM_ASSERT(g.ActiveId == id); + g.TempInputId = g.ActiveId; + } + return value_changed; +} + +bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format) +{ + ImGuiContext& g = *GImGui; + char fmt_buf[32]; char data_buf[32]; format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); ImStrTrimBlanks(data_buf); - g.CurrentWindow->DC.CursorPos = bb.Min; ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; flags |= ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); - bool value_changed = InputTextEx(label, NULL, data_buf, IM_ARRAYSIZE(data_buf), bb.GetSize(), flags); - if (init) - { - // First frame we started displaying the InputText widget, we expect it to take the active id. - IM_ASSERT(g.ActiveId == id); - g.TempInputTextId = g.ActiveId; - } + bool value_changed = TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags); if (value_changed) { value_changed = DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); @@ -3156,7 +3190,7 @@ namespace ImStb static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return obj->CurLenW; } static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->TextW[idx]; } static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *GImGui; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } -static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x10000 ? 0 : key; } +static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx) { @@ -3249,8 +3283,25 @@ static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const Im #define STB_TEXTEDIT_IMPLEMENTATION #include "imstb_textedit.h" +// stb_textedit internally allows for a single undo record to do addition and deletion, but somehow, calling +// the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?) +static void stb_textedit_replace(STB_TEXTEDIT_STRING* str, STB_TexteditState* state, const STB_TEXTEDIT_CHARTYPE* text, int text_len) +{ + stb_text_makeundo_replace(str, state, 0, str->CurLenW, text_len); + ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenW); + if (text_len <= 0) + return; + if (ImStb::STB_TEXTEDIT_INSERTCHARS(str, 0, text, text_len)) + { + state->cursor = text_len; + state->has_preferred_x = 0; + return; + } + IM_ASSERT(0); // Failed to insert character, normally shouldn't happen because of how we currently use stb_textedit_replace() } +} // namespace ImStb + void ImGuiInputTextState::OnKeyPressed(int key) { stb_textedit_key(this, &Stb, key); @@ -3339,6 +3390,10 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f if (c >= 0xE000 && c <= 0xF8FF) return false; + // Filter Unicode ranges we are not handling in this build. + if (c > IM_UNICODE_CODEPOINT_MAX) + return false; + // Generic named filters if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) { @@ -3432,14 +3487,23 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ EndGroup(); return false; } - if (!BeginChildFrame(id, frame_bb.GetSize())) + + // We reproduce the contents of BeginChildFrame() in order to provide 'label' so our window internal data are easier to read/debug. + PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); + PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); + PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); + PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding); + bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding); + PopStyleVar(3); + PopStyleColor(); + if (!child_visible) { - EndChildFrame(); + EndChild(); EndGroup(); return false; } draw_window = g.CurrentWindow; // Child window - draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight + draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. inner_size.x -= draw_window->ScrollbarSizes.x; } else @@ -3452,19 +3516,17 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (hovered) g.MouseCursor = ImGuiMouseCursor_TextInput; - // NB: we are only allowed to access 'edit_state' if we are the active widget. - ImGuiInputTextState* state = NULL; - if (g.InputTextState.ID == id) - state = &g.InputTextState; + // We are only allowed to access the state if we are already the active widget. + ImGuiInputTextState* state = GetInputTextState(id); const bool focus_requested = FocusableItemRegister(window, id); - const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterAll == window->DC.FocusCounterAll); + const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterRegular == window->DC.FocusCounterRegular); const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code; const bool user_clicked = hovered && io.MouseClicked[0]; const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_NavKeyboard)); - const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetScrollbarID(draw_window, ImGuiAxis_Y); - const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetScrollbarID(draw_window, ImGuiAxis_Y); + const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); + const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); bool clear_active_id = false; bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline); @@ -3762,7 +3824,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ s += ImTextCharFromUtf8(&c, s, NULL); if (c == 0) break; - if (c >= 0x10000 || !InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; } @@ -3791,13 +3853,22 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. if (!is_readonly && strcmp(buf, state->InitialTextA.Data) != 0) { + // Push records into the undo stack so we can CTRL+Z the revert operation itself apply_new_text = state->InitialTextA.Data; apply_new_text_length = state->InitialTextA.Size - 1; + ImVector w_text; + if (apply_new_text_length > 0) + { + w_text.resize(ImTextCountCharsFromUtf8(apply_new_text, apply_new_text + apply_new_text_length) + 1); + ImTextStrFromUtf8(w_text.Data, w_text.Size, apply_new_text, apply_new_text + apply_new_text_length); + } + stb_textedit_replace(state, &state->Stb, w_text.Data, (apply_new_text_length > 0) ? (w_text.Size - 1) : 0); } } // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. - // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. Also this allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage. + // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. + // This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize). bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); if (apply_edit_back_to_user_buffer) { @@ -3891,8 +3962,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Copy result to user buffer if (apply_new_text) { + // We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size + // of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used + // without any storage on user's side. IM_ASSERT(apply_new_text_length >= 0); - if (backup_current_text_length != apply_new_text_length && is_resizable) + if (is_resizable) { ImGuiInputTextCallbackData callback_data; callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; @@ -3907,6 +3981,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); IM_ASSERT(apply_new_text_length <= buf_size); } + //IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); @@ -3986,7 +4061,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter. searches_remaining += is_multiline ? 1 : 0; int line_count = 0; - //for (const ImWchar* s = text_begin; (s = (const ImWchar*)wcschr((const wchar_t*)s, (wchar_t)'\n')) != NULL; s++) // FIXME-OPT: Could use this when wchar_t are 16-bits + //for (const ImWchar* s = text_begin; (s = (const ImWchar*)wcschr((const wchar_t*)s, (wchar_t)'\n')) != NULL; s++) // FIXME-OPT: Could use this when wchar_t are 16-bit for (const ImWchar* s = text_begin; *s != 0; s++) if (*s == '\n') { @@ -4022,9 +4097,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { const float scroll_increment_x = inner_size.x * 0.25f; if (cursor_offset.x < state->ScrollX) - state->ScrollX = (float)(int)ImMax(0.0f, cursor_offset.x - scroll_increment_x); + state->ScrollX = IM_FLOOR(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); else if (cursor_offset.x - inner_size.x >= state->ScrollX) - state->ScrollX = (float)(int)(cursor_offset.x - inner_size.x + scroll_increment_x); + state->ScrollX = IM_FLOOR(cursor_offset.x - inner_size.x + scroll_increment_x); } else { @@ -4063,7 +4138,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ break; if (rect_pos.y < clip_rect.y) { - //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\n', text_selected_end - p); // FIXME-OPT: Could use this when wchar_t are 16-bits + //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\n', text_selected_end - p); // FIXME-OPT: Could use this when wchar_t are 16-bit //p = p ? p + 1 : text_selected_end; while (p < text_selected_end) if (*p++ == '\n') @@ -4072,7 +4147,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else { ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true); - if (rect_size.x <= 0.0f) rect_size.x = (float)(int)(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines + if (rect_size.x <= 0.0f) rect_size.x = IM_FLOOR(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); if (rect.Overlaps(clip_rect)) @@ -4125,7 +4200,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_multiline) { Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line - EndChildFrame(); + EndChild(); EndGroup(); } @@ -4224,8 +4299,13 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag { // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); - if (f[1] == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) - f[0] = g.ColorEditLastHue; + if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) + { + if (f[1] == 0) + f[0] = g.ColorEditLastHue; + if (f[2] == 0) + f[1] = g.ColorEditLastSat; + } } int i[4] = { IM_F32_TO_INT8_UNBOUND(f[0]), IM_F32_TO_INT8_UNBOUND(f[1]), IM_F32_TO_INT8_UNBOUND(f[2]), IM_F32_TO_INT8_UNBOUND(f[3]) }; @@ -4239,8 +4319,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) { // RGB/HSV 0..255 Sliders - const float w_item_one = ImMax(1.0f, (float)(int)((w_inputs - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); - const float w_item_last = ImMax(1.0f, (float)(int)(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + const float w_item_one = ImMax(1.0f, IM_FLOOR((w_inputs - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); + const float w_item_last = ImMax(1.0f, IM_FLOOR(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); static const char* ids[4] = { "##X", "##Y", "##Z", "##W" }; @@ -4264,16 +4344,15 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag SameLine(0, style.ItemInnerSpacing.x); SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last); - // Disable Hue edit when Saturation is zero - const bool disable_hue_edit = (n == 0 && (flags & ImGuiColorEditFlags_DisplayHSV) && i[1] == 0); + // FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0. if (flags & ImGuiColorEditFlags_Float) { - value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, disable_hue_edit ? +FLT_MAX : 0.0f, disable_hue_edit ? -FLT_MAX : hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); + value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); value_changed_as_float |= value_changed; } else { - value_changed |= DragInt(ids[n], &i[n], 1.0f, disable_hue_edit ? INT_MAX : 0, disable_hue_edit ? INT_MIN : hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); + value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); @@ -4342,7 +4421,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if (label != label_display_end && !(flags & ImGuiColorEditFlags_NoLabel)) { - window->DC.CursorPos = ImVec2(pos.x + w_full + style.ItemInnerSpacing.x, pos.y + style.FramePadding.y); + const float text_offset_x = (flags & ImGuiColorEditFlags_NoInputs) ? w_button : w_full + style.ItemInnerSpacing.x; + window->DC.CursorPos = ImVec2(pos.x + text_offset_x, pos.y + style.FramePadding.y); TextEx(label, label_display_end); } @@ -4355,6 +4435,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB)) { g.ColorEditLastHue = f[0]; + g.ColorEditLastSat = f[1]; ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); memcpy(g.ColorEditLastColor, f, sizeof(float) * 3); } @@ -4515,7 +4596,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl float sv_picker_size = ImMax(bars_width * 1, width - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x; float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x; - float bars_triangles_half_sz = (float)(int)(bars_width * 0.20f); + float bars_triangles_half_sz = IM_FLOOR(bars_width * 0.20f); float backup_initial_col[4]; memcpy(backup_initial_col, col, components * sizeof(float)); @@ -4537,8 +4618,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(R, G, B, H, S, V); - if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) - H = g.ColorEditLastHue; + if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) + { + if (S == 0) + H = g.ColorEditLastHue; + if (V == 0) + S = g.ColorEditLastSat; + } } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -4666,6 +4752,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10*1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]); g.ColorEditLastHue = H; + g.ColorEditLastSat = S; memcpy(g.ColorEditLastColor, col, sizeof(float) * 3); } else if (flags & ImGuiColorEditFlags_InputHSV) @@ -4720,8 +4807,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl G = col[1]; B = col[2]; ColorConvertRGBtoHSV(R, G, B, H, S, V); - if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately. - H = g.ColorEditLastHue; + if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately. + { + if (S == 0) + H = g.ColorEditLastHue; + if (V == 0) + S = g.ColorEditLastSat; + } } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -4784,7 +4876,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl draw_list->PrimVtx(trb, uv_white, hue_color32); draw_list->PrimVtx(trc, uv_white, col_white); draw_list->PrimVtx(tra, uv_white, 0); - draw_list->PrimVtx(trb, uv_white, col_white); + draw_list->PrimVtx(trb, uv_white, col_black); draw_list->PrimVtx(trc, uv_white, 0); draw_list->AddTriangle(tra, trb, trc, col_midgrey, 1.5f); sv_cursor_pos = ImLerp(ImLerp(trc, tra, ImSaturate(S)), trb, ImSaturate(1 - V)); @@ -4795,13 +4887,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), col_white, hue_color32, hue_color32, col_white); draw_list->AddRectFilledMultiColor(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), 0, 0, col_black, col_black); RenderFrameBorder(picker_pos, picker_pos + ImVec2(sv_picker_size, sv_picker_size), 0.0f); - sv_cursor_pos.x = ImClamp((float)(int)(picker_pos.x + ImSaturate(S) * sv_picker_size + 0.5f), picker_pos.x + 2, picker_pos.x + sv_picker_size - 2); // Sneakily prevent the circle to stick out too much - sv_cursor_pos.y = ImClamp((float)(int)(picker_pos.y + ImSaturate(1 - V) * sv_picker_size + 0.5f), picker_pos.y + 2, picker_pos.y + sv_picker_size - 2); + sv_cursor_pos.x = ImClamp(IM_ROUND(picker_pos.x + ImSaturate(S) * sv_picker_size), picker_pos.x + 2, picker_pos.x + sv_picker_size - 2); // Sneakily prevent the circle to stick out too much + sv_cursor_pos.y = ImClamp(IM_ROUND(picker_pos.y + ImSaturate(1 - V) * sv_picker_size), picker_pos.y + 2, picker_pos.y + sv_picker_size - 2); // Render Hue Bar for (int i = 0; i < 6; ++i) draw_list->AddRectFilledMultiColor(ImVec2(bar0_pos_x, picker_pos.y + i * (sv_picker_size / 6)), ImVec2(bar0_pos_x + bars_width, picker_pos.y + (i + 1) * (sv_picker_size / 6)), col_hues[i], col_hues[i], col_hues[i + 1], col_hues[i + 1]); - float bar0_line_y = (float)(int)(picker_pos.y + H * sv_picker_size + 0.5f); + float bar0_line_y = IM_ROUND(picker_pos.y + H * sv_picker_size); RenderFrameBorder(ImVec2(bar0_pos_x, picker_pos.y), ImVec2(bar0_pos_x + bars_width, picker_pos.y + sv_picker_size), 0.0f); RenderArrowsForVerticalBar(draw_list, ImVec2(bar0_pos_x - 1, bar0_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f, style.Alpha); } @@ -4819,7 +4911,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size); RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f)); draw_list->AddRectFilledMultiColor(bar1_bb.Min, bar1_bb.Max, user_col32_striped_of_alpha, user_col32_striped_of_alpha, user_col32_striped_of_alpha & ~IM_COL32_A_MASK, user_col32_striped_of_alpha & ~IM_COL32_A_MASK); - float bar1_line_y = (float)(int)(picker_pos.y + (1.0f - alpha) * sv_picker_size + 0.5f); + float bar1_line_y = IM_ROUND(picker_pos.y + (1.0f - alpha) * sv_picker_size); RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f); RenderArrowsForVerticalBar(draw_list, ImVec2(bar1_pos_x - 1, bar1_line_y), ImVec2(bars_triangles_half_sz + 1, bars_triangles_half_sz), bars_width + 2.0f, style.Alpha); } @@ -4872,11 +4964,15 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl float grid_step = ImMin(size.x, size.y) / 2.99f; float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f); ImRect bb_inner = bb; - float off = -0.75f; // The border (using Col_FrameBg) tends to look off when color is near-opaque and rounding is enabled. This offset seemed like a good middle ground to reduce those artifacts. - bb_inner.Expand(off); + float off = 0.0f; + if ((flags & ImGuiColorEditFlags_NoBorder) == 0) + { + off = -0.75f; // The border (using Col_FrameBg) tends to look off when color is near-opaque and rounding is enabled. This offset seemed like a good middle ground to reduce those artifacts. + bb_inner.Expand(off); + } if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) { - float mid_x = (float)(int)((bb_inner.Min.x + bb_inner.Max.x) * 0.5f + 0.5f); + float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f); RenderColorRectWithAlphaCheckerboard(ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight); window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft); } @@ -4890,10 +4986,13 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All); } RenderNavHighlight(bb, id); - if (g.Style.FrameBorderSize > 0.0f) - RenderFrameBorder(bb.Min, bb.Max, rounding); - else - window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border + if ((flags & ImGuiColorEditFlags_NoBorder) == 0) + { + if (g.Style.FrameBorderSize > 0.0f) + RenderFrameBorder(bb.Min, bb.Max, rounding); + else + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border + } // Drag and Drop Source // NB: The ActiveId test is merely an optional micro-optimization, BeginDragDropSource() does the same test. @@ -4940,7 +5039,7 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags { ImGuiContext& g = *GImGui; - BeginTooltipEx(0, true); + BeginTooltipEx(0, ImGuiTooltipFlags_OverridePreviousTooltip); const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; if (text_end > text) { @@ -5005,12 +5104,15 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) ImFormatString(buf, IM_ARRAYSIZE(buf), "(%d,%d,%d,%d)", cr, cg, cb, ca); if (Selectable(buf)) SetClipboardText(buf); - if (flags & ImGuiColorEditFlags_NoAlpha) - ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X", cr, cg, cb); - else - ImFormatString(buf, IM_ARRAYSIZE(buf), "0x%02X%02X%02X%02X", cr, cg, cb, ca); + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", cr, cg, cb); if (Selectable(buf)) SetClipboardText(buf); + if (!(flags & ImGuiColorEditFlags_NoAlpha)) + { + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", cr, cg, cb, ca); + if (Selectable(buf)) + SetClipboardText(buf); + } EndPopup(); } @@ -5211,7 +5313,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const bool display_frame = (flags & ImGuiTreeNodeFlags_Framed) != 0; - const ImVec2 padding = (display_frame || (flags & ImGuiTreeNodeFlags_FramePadding)) ? style.FramePadding : ImVec2(style.FramePadding.x, 0.0f); + const ImVec2 padding = (display_frame || (flags & ImGuiTreeNodeFlags_FramePadding)) ? style.FramePadding : ImVec2(style.FramePadding.x, ImMin(window->DC.CurrLineTextBaseOffset, style.FramePadding.y)); if (!label_end) label_end = FindRenderedTextEnd(label); @@ -5228,28 +5330,28 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { // Framed header expand a little outside the default padding, to the edge of InnerClipRect // (FIXME: May remove this at some point and make InnerClipRect align with WindowPadding.x instead of WindowPadding.x*0.5f) - frame_bb.Min.x -= (float)(int)(window->WindowPadding.x * 0.5f - 1.0f); - frame_bb.Max.x += (float)(int)(window->WindowPadding.x * 0.5f); + frame_bb.Min.x -= IM_FLOOR(window->WindowPadding.x * 0.5f - 1.0f); + frame_bb.Max.x += IM_FLOOR(window->WindowPadding.x * 0.5f); } const float text_offset_x = g.FontSize + (display_frame ? padding.x*3 : padding.x*2); // Collapser arrow width + Spacing const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x*2 : 0.0f); // Include collapser ImVec2 text_pos(window->DC.CursorPos.x + text_offset_x, window->DC.CursorPos.y + text_offset_y); - ItemSize(ImVec2(text_width, frame_height), text_offset_y); + ItemSize(ImVec2(text_width, frame_height), padding.y); // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing ImRect interact_bb = frame_bb; if (!display_frame && (flags & (ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth)) == 0) interact_bb.Max.x = frame_bb.Min.x + text_width + style.ItemSpacing.x * 2.0f; - + // Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child. // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero. const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; bool is_open = TreeNodeBehaviorIsOpen(id, flags); if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) - window->DC.TreeMayJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); + window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); bool item_add = ItemAdd(interact_bb, id); window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; @@ -5268,14 +5370,25 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // - OpenOnDoubleClick .............. double-click anywhere to open // - OpenOnArrow .................... single-click on arrow to open // - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open - ImGuiButtonFlags button_flags = ImGuiButtonFlags_NoKeyModifiers; + ImGuiButtonFlags button_flags = 0; if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0); + else + button_flags |= ImGuiButtonFlags_PressedOnClickRelease; if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; + // We allow clicking on the arrow section with keyboard modifiers held, in order to easily + // allow browsing a tree while preserving selection with code implementing multi-selection patterns. + // When clicking on the rest of the tree node we always disallow keyboard modifiers. + const float hit_padding_x = style.TouchExtraPadding.x; + const float arrow_hit_x1 = (text_pos.x - text_offset_x) - hit_padding_x; + const float arrow_hit_x2 = (text_pos.x - text_offset_x) + (g.FontSize + padding.x * 2.0f) + hit_padding_x; + if (window != g.HoveredWindow || !(g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2)) + button_flags |= ImGuiButtonFlags_NoKeyModifiers; + bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0; const bool was_selected = selected; @@ -5286,13 +5399,12 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { if (pressed) { - const float arrow_x1 = text_pos.x - text_offset_x; - const float arrow_x2 = arrow_x1 + g.FontSize + padding.x * 2.0f; - toggled = !(flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) || (g.NavActivateId == id); + if ((flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) == 0 || (g.NavActivateId == id)) + toggled = true; if (flags & ImGuiTreeNodeFlags_OpenOnArrow) - toggled |= IsMouseHoveringRect(ImVec2(arrow_x1, interact_bb.Min.y), ImVec2(arrow_x2, interact_bb.Max.y)) && (!g.NavDisableMouseHover); - if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) - toggled |= g.IO.MouseDoubleClicked[0]; + toggled |= (g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2) && (!g.NavDisableMouseHover); // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job + if ((flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) && g.IO.MouseDoubleClicked[0]) + toggled = true; if (g.DragDropActive && is_open) // When using Drag and Drop "hold to open" we keep the node highlighted after opening, but never close it again. toggled = false; } @@ -5312,6 +5424,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { is_open = !is_open; window->DC.StateStorage->SetInt(id, is_open); + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledOpen; } } if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) @@ -5411,12 +5524,12 @@ void ImGui::TreePop() // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled) if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) - if (g.NavIdIsAlive && (window->DC.TreeMayJumpToParentOnPopMask & tree_depth_mask)) + if (g.NavIdIsAlive && (window->DC.TreeJumpToParentOnPopMask & tree_depth_mask)) { - SetNavID(window->IDStack.back(), g.NavLayer); + SetNavID(window->IDStack.back(), g.NavLayer, 0); NavMoveRequestCancel(); } - window->DC.TreeMayJumpToParentOnPopMask &= tree_depth_mask - 1; + window->DC.TreeJumpToParentOnPopMask &= tree_depth_mask - 1; IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much. PopID(); @@ -5461,7 +5574,9 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags return false; ImGuiID id = window->GetID(label); - flags |= ImGuiTreeNodeFlags_CollapsingHeader | (p_open ? ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton : 0); + flags |= ImGuiTreeNodeFlags_CollapsingHeader; + if (p_open) + flags |= ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; bool is_open = TreeNodeBehavior(id, flags, label); if (p_open) { @@ -5507,7 +5622,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ImVec2 pos = window->DC.CursorPos; pos.y += window->DC.CurrLineTextBaseOffset; ImRect bb_inner(pos, pos + size); - ItemSize(size); + ItemSize(size, 0.0f); // Fill horizontal space. ImVec2 window_padding = window->WindowPadding; @@ -5521,8 +5636,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // Selectables are tightly packed together so we extend the box to cover spacing between selectable. const float spacing_x = style.ItemSpacing.x; const float spacing_y = style.ItemSpacing.y; - const float spacing_L = (float)(int)(spacing_x * 0.50f); - const float spacing_U = (float)(int)(spacing_y * 0.50f); + const float spacing_L = IM_FLOOR(spacing_x * 0.50f); + const float spacing_U = IM_FLOOR(spacing_y * 0.50f); bb.Min.x -= spacing_L; bb.Min.y -= spacing_U; bb.Max.x += (spacing_x - spacing_L); @@ -5549,12 +5664,12 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; - if (flags & ImGuiSelectableFlags_NoHoldingActiveID) button_flags |= ImGuiButtonFlags_NoHoldingActiveID; - if (flags & ImGuiSelectableFlags_PressedOnClick) button_flags |= ImGuiButtonFlags_PressedOnClick; - if (flags & ImGuiSelectableFlags_PressedOnRelease) button_flags |= ImGuiButtonFlags_PressedOnRelease; - if (flags & ImGuiSelectableFlags_Disabled) button_flags |= ImGuiButtonFlags_Disabled; - if (flags & ImGuiSelectableFlags_AllowDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; - if (flags & ImGuiSelectableFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; + if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; } + if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } + if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } + if (flags & ImGuiSelectableFlags_Disabled) { button_flags |= ImGuiButtonFlags_Disabled; } + if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } + if (flags & ImGuiSelectableFlags_AllowItemOverlap) { button_flags |= ImGuiButtonFlags_AllowItemOverlap; } if (flags & ImGuiSelectableFlags_Disabled) selected = false; @@ -5569,7 +5684,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { g.NavDisableHighlight = true; - SetNavID(id, window->DC.NavLayerCurrent); + SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent); } } if (pressed) @@ -5943,10 +6058,10 @@ void ImGui::Value(const char* prefix, float v, const char* float_format) // [SECTION] MenuItem, BeginMenu, EndMenu, etc. //------------------------------------------------------------------------- // - ImGuiMenuColumns [Internal] -// - BeginMainMenuBar() -// - EndMainMenuBar() // - BeginMenuBar() // - EndMenuBar() +// - BeginMainMenuBar() +// - EndMainMenuBar() // - BeginMenu() // - EndMenu() // - MenuItem() @@ -5972,7 +6087,7 @@ void ImGuiMenuColumns::Update(int count, float spacing, bool clear) { if (i > 0 && NextWidths[i] > 0.0f) Width += Spacing; - Pos[i] = (float)(int)Width; + Pos[i] = IM_FLOOR(Width); Width += NextWidths[i]; NextWidths[i] = 0.0f; } @@ -5989,45 +6104,11 @@ float ImGuiMenuColumns::DeclColumns(float w0, float w1, float w2) // not using v return ImMax(Width, NextWidth); } -float ImGuiMenuColumns::CalcExtraSpace(float avail_w) +float ImGuiMenuColumns::CalcExtraSpace(float avail_w) const { return ImMax(0.0f, avail_w - Width); } -// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. -bool ImGui::BeginMainMenuBar() -{ - ImGuiContext& g = *GImGui; - g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); - SetNextWindowPos(ImVec2(0.0f, 0.0f)); - SetNextWindowSize(ImVec2(g.IO.DisplaySize.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y)); - PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0,0)); - ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; - bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); - PopStyleVar(2); - g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); - if (!is_open) - { - End(); - return false; - } - return true; //-V1020 -} - -void ImGui::EndMainMenuBar() -{ - EndMenuBar(); - - // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window - // FIXME: With this strategy we won't be able to restore a NULL focus. - ImGuiContext& g = *GImGui; - if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0 && !g.NavAnyRequest) - FocusTopMostWindowUnderOne(g.NavWindow, NULL); - - End(); -} - // FIXME: Provided a rectangle perhaps e.g. a BeginMenuBarEx() could be used anywhere.. // Currently the main responsibility of this function being to setup clip-rect + horizontal layout + menu navigation layer. // Ideally we also want this to be responsible for claiming space out of the main window scrolling rectangle, in which case ImGuiWindowFlags_MenuBar will become unnecessary. @@ -6047,7 +6128,7 @@ bool ImGui::BeginMenuBar() // We don't clip with current window clipping rectangle as it is already set to the area below. However we clip with window full rect. // We remove 1 worth of rounding to Max.x to that text in long menus and small windows don't tend to display over the lower-right rounded area, which looks particularly glitchy. ImRect bar_rect = window->MenuBarRect(); - ImRect clip_rect(ImFloor(bar_rect.Min.x + 0.5f), ImFloor(bar_rect.Min.y + window->WindowBorderSize + 0.5f), ImFloor(ImMax(bar_rect.Min.x, bar_rect.Max.x - window->WindowRounding) + 0.5f), ImFloor(bar_rect.Max.y + 0.5f)); + ImRect clip_rect(IM_ROUND(bar_rect.Min.x + window->WindowBorderSize), IM_ROUND(bar_rect.Min.y + window->WindowBorderSize), IM_ROUND(ImMax(bar_rect.Min.x, bar_rect.Max.x - ImMax(window->WindowRounding, window->WindowBorderSize))), IM_ROUND(bar_rect.Max.y)); clip_rect.ClipWith(window->OuterRectClipped); PushClipRect(clip_rect.Min, clip_rect.Max, false); @@ -6080,7 +6161,7 @@ void ImGui::EndMenuBar() const ImGuiNavLayer layer = ImGuiNavLayer_Menu; IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check FocusWindow(window); - SetNavIDWithRectRel(window->NavLastIds[layer], layer, window->NavRectRel[layer]); + SetNavIDWithRectRel(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); g.NavLayer = layer; g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; @@ -6101,6 +6182,40 @@ void ImGui::EndMenuBar() window->DC.MenuBarAppending = false; } +// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. +bool ImGui::BeginMainMenuBar() +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); + SetNextWindowPos(ImVec2(0.0f, 0.0f)); + SetNextWindowSize(ImVec2(g.IO.DisplaySize.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y)); + PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); + PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; + bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); + PopStyleVar(2); + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); + if (!is_open) + { + End(); + return false; + } + return true; //-V1020 +} + +void ImGui::EndMainMenuBar() +{ + EndMenuBar(); + + // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window + // FIXME: With this strategy we won't be able to restore a NULL focus. + ImGuiContext& g = *GImGui; + if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0 && !g.NavAnyRequest) + FocusTopMostWindowUnderOne(g.NavWindow, NULL); + + End(); +} + bool ImGui::BeginMenu(const char* label, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); @@ -6110,11 +6225,30 @@ bool ImGui::BeginMenu(const char* label, bool enabled) ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); + bool menu_is_open = IsPopupOpen(id); - ImVec2 label_size = CalcTextSize(label, NULL, true); + // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) + ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; + if (window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) + flags |= ImGuiWindowFlags_ChildWindow; + + // If a menu with same the ID was already submitted, we will append to it, matching the behavior of Begin(). + // We are relying on a O(N) search - so O(N log N) over the frame - which seems like the most efficient for the expected small amount of BeginMenu() calls per frame. + // If somehow this is ever becoming a problem we can switch to use e.g. a ImGuiStorager mapping key to last frame used. + if (g.MenusIdSubmittedThisFrame.contains(id)) + { + if (menu_is_open) + menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + else + g.NextWindowData.ClearFlags(); // we behave like Begin() and need to consume those values + return menu_is_open; + } + + // Tag menu as used. Next time BeginMenu() with same ID is called it will append to existing menu + g.MenusIdSubmittedThisFrame.push_back(id); + ImVec2 label_size = CalcTextSize(label, NULL, true); bool pressed; - bool menu_is_open = IsPopupOpen(id); bool menuset_is_open = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].OpenParentId == window->IDStack.back()); ImGuiWindow* backed_nav_window = g.NavWindow; if (menuset_is_open) @@ -6129,23 +6263,23 @@ bool ImGui::BeginMenu(const char* label, bool enabled) // Menu inside an horizontal menu bar // Selectable extend their highlight by half ItemSpacing in each direction. // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() - popup_pos = ImVec2(pos.x - 1.0f - (float)(int)(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); - window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); + popup_pos = ImVec2(pos.x - 1.0f - IM_FLOOR(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); + window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; - pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); PopStyleVar(); - window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else { // Menu inside a menu popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); - float w = window->MenuColumns.DeclColumns(label_size.x, 0.0f, (float)(int)(g.FontSize * 1.20f)); // Feedback to next frame + float w = window->DC.MenuColumns.DeclColumns(label_size.x, 0.0f, IM_FLOOR(g.FontSize * 1.20f)); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); - pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_PressedOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); ImU32 text_col = GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled); - RenderArrow(window->DrawList, pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right); + RenderArrow(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right); } const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id); @@ -6233,13 +6367,13 @@ bool ImGui::BeginMenu(const char* label, bool enabled) if (menu_is_open) { - // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) SetNextWindowPos(popup_pos, ImGuiCond_Always); - ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; - if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) - flags |= ImGuiWindowFlags_ChildWindow; menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) } + else + { + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values + } return menu_is_open; } @@ -6273,33 +6407,33 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73), // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only. - ImGuiSelectableFlags flags = ImGuiSelectableFlags_PressedOnRelease | ImGuiSelectableFlags_SetNavIdOnHover | (enabled ? 0 : ImGuiSelectableFlags_Disabled); + ImGuiSelectableFlags flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover | (enabled ? 0 : ImGuiSelectableFlags_Disabled); bool pressed; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful // Note that in this situation we render neither the shortcut neither the selected tick mark float w = label_size.x; - window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f); + window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); pressed = Selectable(label, false, flags, ImVec2(w, 0.0f)); PopStyleVar(); - window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else { ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f); - float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame + float w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_size.x, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f)); if (shortcut_size.x > 0.0f) { PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); - RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); + RenderText(pos + ImVec2(window->DC.MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); PopStyleColor(); } if (selected) - RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); + RenderCheckMark(pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); } IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); @@ -6320,9 +6454,6 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, //------------------------------------------------------------------------- // [SECTION] Widgets: BeginTabBar, EndTabBar, etc. //------------------------------------------------------------------------- -// [BETA API] API may evolve! This code has been extracted out of the Docking branch, -// and some of the construct which are not used in Master may be left here to facilitate merging. -//------------------------------------------------------------------------- // - BeginTabBar() // - BeginTabBarEx() [Internal] // - EndTabBar() @@ -6438,15 +6569,15 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG tab_bar->FramePadding = g.Style.FramePadding; // Layout - ItemSize(ImVec2(tab_bar->OffsetMaxIdeal, tab_bar->BarRect.GetHeight())); + ItemSize(ImVec2(tab_bar->OffsetMaxIdeal, tab_bar->BarRect.GetHeight()), tab_bar->FramePadding.y); window->DC.CursorPos.x = tab_bar->BarRect.Min.x; // Draw separator const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); const float y = tab_bar->BarRect.Max.y - 1.0f; { - const float separator_min_x = tab_bar->BarRect.Min.x - ImFloor(window->WindowPadding.x * 0.5f); - const float separator_max_x = tab_bar->BarRect.Max.x + ImFloor(window->WindowPadding.x * 0.5f); + const float separator_min_x = tab_bar->BarRect.Min.x - IM_FLOOR(window->WindowPadding.x * 0.5f); + const float separator_max_x = tab_bar->BarRect.Max.x + IM_FLOOR(window->WindowPadding.x * 0.5f); window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f); } return true; @@ -6462,8 +6593,8 @@ void ImGui::EndTabBar() ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { - IM_ASSERT(tab_bar != NULL && "Mismatched BeginTabBar()/EndTabBar()!"); - return; // FIXME-ERRORHANDLING + IM_ASSERT_USER_ERROR(tab_bar != NULL, "Mismatched BeginTabBar()/EndTabBar()!"); + return; } if (tab_bar->WantLayout) TabBarLayout(tab_bar); @@ -6565,13 +6696,13 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window. const char* tab_name = tab_bar->GetTabName(tab); const bool has_close_button = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) ? false : true; - tab->WidthContents = TabItemCalcSize(tab_name, has_close_button).x; + tab->ContentWidth = TabItemCalcSize(tab_name, has_close_button).x; - width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->WidthContents; + width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->ContentWidth; // Store data so we can build an array sorted by width if we need to shrink tabs down g.ShrinkWidthBuffer[tab_n].Index = tab_n; - g.ShrinkWidthBuffer[tab_n].Width = tab->WidthContents; + g.ShrinkWidthBuffer[tab_n].Width = tab->ContentWidth; } // Compute width @@ -6583,7 +6714,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // If we don't have enough room, resize down the largest tabs first ShrinkWidths(g.ShrinkWidthBuffer.Data, g.ShrinkWidthBuffer.Size, width_excess); for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) - tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index].Width = (float)(int)g.ShrinkWidthBuffer[tab_n].Width; + tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index].Width = IM_FLOOR(g.ShrinkWidthBuffer[tab_n].Width); } else { @@ -6591,7 +6722,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; - tab->Width = ImMin(tab->WidthContents, tab_max_width); + tab->Width = ImMin(tab->ContentWidth, tab_max_width); IM_ASSERT(tab->Width > 0.0f); } } @@ -6607,7 +6738,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) scroll_track_selected_tab_id = tab->ID; offset_x += tab->Width + g.Style.ItemInnerSpacing.x; - offset_x_ideal += tab->WidthContents + g.Style.ItemInnerSpacing.x; + offset_x_ideal += tab->ContentWidth + g.Style.ItemInnerSpacing.x; } tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); tab_bar->OffsetMaxIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); @@ -6837,9 +6968,6 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) //------------------------------------------------------------------------- // [SECTION] Widgets: BeginTabItem, EndTabItem, etc. //------------------------------------------------------------------------- -// [BETA API] API may evolve! This code has been extracted out of the Docking branch, -// and some of the construct which are not used in Master may be left here to facilitate merging. -//------------------------------------------------------------------------- // - BeginTabItem() // - EndTabItem() // - TabItemEx() [Internal] @@ -6859,8 +6987,8 @@ bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags f ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { - IM_ASSERT(tab_bar && "Needs to be called between BeginTabBar() and EndTabBar()!"); - return false; // FIXME-ERRORHANDLING + IM_ASSERT_USER_ERROR(tab_bar, "BeginTabItem() Needs to be called between BeginTabBar() and EndTabBar()!"); + return false; } bool ret = TabItemEx(tab_bar, label, p_open, flags); if (ret && !(flags & ImGuiTabItemFlags_NoPushId)) @@ -6913,6 +7041,12 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, return false; } + // Store into ImGuiTabItemFlags_NoCloseButton, also honor ImGuiTabItemFlags_NoCloseButton passed by user (although not documented) + if (flags & ImGuiTabItemFlags_NoCloseButton) + p_open = NULL; + else if (p_open == NULL) + flags |= ImGuiTabItemFlags_NoCloseButton; + // Calculate tab contents size ImVec2 size = TabItemCalcSize(label, p_open != NULL); @@ -6928,10 +7062,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab_is_new = true; } tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab); - tab->WidthContents = size.x; - - if (p_open == NULL) - flags |= ImGuiTabItemFlags_NoCloseButton; + tab->ContentWidth = size.x; const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; @@ -6984,7 +7115,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Layout size.x = tab->Width; - window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2((float)(int)tab->Offset - tab_bar->ScrollingAnim, 0.0f); + window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_FLOOR(tab->Offset - tab_bar->ScrollingAnim), 0.0f); ImVec2 pos = window->DC.CursorPos; ImRect bb(pos, pos + size); @@ -7039,10 +7170,10 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } #if 0 - if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->WidthContents) + if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->ContentWidth) { // Enlarge tab display when hovering - bb.Max.x = bb.Min.x + (float)(int)ImLerp(bb.GetWidth(), tab->WidthContents, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f)); + bb.Max.x = bb.Min.x + IM_FLOOR(ImLerp(bb.GetWidth(), tab->ContentWidth, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f))); display_draw_list = GetForegroundDrawList(window); TabItemBackground(display_draw_list, bb, flags, GetColorU32(ImGuiCol_TitleBgActive)); } @@ -7152,7 +7283,7 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, if (flags & ImGuiTabItemFlags_UnsavedDocument) { text_pixel_clip_bb.Max.x -= CalcTextSize(TAB_UNSAVED_MARKER, NULL, false).x; - ImVec2 unsaved_marker_pos(ImMin(bb.Min.x + frame_padding.x + label_size.x + 2, text_pixel_clip_bb.Max.x), bb.Min.y + frame_padding.y + (float)(int)(-g.FontSize * 0.25f)); + ImVec2 unsaved_marker_pos(ImMin(bb.Min.x + frame_padding.x + label_size.x + 2, text_pixel_clip_bb.Max.x), bb.Min.y + frame_padding.y + IM_FLOOR(-g.FontSize * 0.25f)); RenderTextClippedEx(draw_list, unsaved_marker_pos, bb.Max - frame_padding, TAB_UNSAVED_MARKER, NULL, NULL); } ImRect text_ellipsis_clip_bb = text_pixel_clip_bb; @@ -7346,7 +7477,7 @@ void ImGui::PushColumnsBackground() ImGuiColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; - window->DrawList->ChannelsSetCurrent(0); + columns->Splitter.SetCurrentChannel(window->DrawList, 0); int cmd_size = window->DrawList->CmdBuffer.Size; PushClipRect(columns->HostClipRect.Min, columns->HostClipRect.Max, false); IM_UNUSED(cmd_size); @@ -7359,7 +7490,7 @@ void ImGui::PopColumnsBackground() ImGuiColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; - window->DrawList->ChannelsSetCurrent(columns->Current + 1); + columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); PopClipRect(); } @@ -7394,8 +7525,8 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - IM_ASSERT(columns_count >= 1); - IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported + IM_ASSERT(columns_count >= 1 && columns_count <= 64); // Maximum 64 columns + IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported // Acquire storage for the columns set ImGuiID id = GetColumnsID(str_id, columns_count); @@ -7442,16 +7573,16 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag { // Compute clipping rectangle ImGuiColumnData* column = &columns->Columns[n]; - float clip_x1 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n)); - float clip_x2 = ImFloor(0.5f + window->Pos.x + GetColumnOffset(n + 1) - 1.0f); + float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n)); + float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f); column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); column->ClipRect.ClipWith(window->ClipRect); } if (columns->Count > 1) { - window->DrawList->ChannelsSplit(1 + columns->Count); - window->DrawList->ChannelsSetCurrent(1); + columns->Splitter.Split(window->DrawList, 1 + columns->Count); + columns->Splitter.SetCurrentChannel(window->DrawList, 1); PushColumnClipRect(0); } @@ -7461,7 +7592,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag float width = offset_1 - offset_0; PushItemWidth(width * 0.65f); window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; } @@ -7476,7 +7607,7 @@ void ImGui::NextColumn() if (columns->Count == 1) { - window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); IM_ASSERT(columns->Current == 0); return; } @@ -7490,18 +7621,18 @@ void ImGui::NextColumn() // Columns 1+ ignore IndentX (by canceling it out) // FIXME-COLUMNS: Unnecessary, could be locked? window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding; - window->DrawList->ChannelsSetCurrent(columns->Current + 1); + columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); } else { // New row/line // Column 0 honor IndentX window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - window->DrawList->ChannelsSetCurrent(1); + columns->Splitter.SetCurrentChannel(window->DrawList, 1); columns->Current = 0; columns->LineMinY = columns->LineMaxY; } - window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->DC.CursorPos.y = columns->LineMinY; window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = 0.0f; @@ -7527,7 +7658,7 @@ void ImGui::EndColumns() if (columns->Count > 1) { PopClipRect(); - window->DrawList->ChannelsMerge(); + columns->Splitter.Merge(window->DrawList); } const ImGuiColumnsFlags flags = columns->Flags; @@ -7568,7 +7699,7 @@ void ImGui::EndColumns() // Draw column const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); - const float xi = (float)(int)x; + const float xi = IM_FLOOR(x); window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); } @@ -7588,7 +7719,7 @@ void ImGui::EndColumns() window->WorkRect = columns->HostWorkRect; window->DC.CurrentColumns = NULL; window->DC.ColumnsOffset.x = 0.0f; - window->DC.CursorPos.x = (float)(int)(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); } // [2018-03: This is currently the only public API, while we are working on making BeginColumns/EndColumns user-facing] @@ -7611,3 +7742,5 @@ void ImGui::Columns(int columns_count, const char* id, bool border) } //------------------------------------------------------------------------- + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imstb_textedit.h b/imgui/imstb_textedit.h index d7fcbd62..2077d02a 100644 --- a/imgui/imstb_textedit.h +++ b/imgui/imstb_textedit.h @@ -1,4 +1,4 @@ -// [DEAR IMGUI] +// [DEAR IMGUI] // This is a slightly modified version of stb_textedit.h 1.13. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) diff --git a/imgui/imstb_truetype.h b/imgui/imstb_truetype.h index c1cdb180..b4bdbd86 100644 --- a/imgui/imstb_truetype.h +++ b/imgui/imstb_truetype.h @@ -1,4 +1,4 @@ -// [DEAR IMGUI] +// [DEAR IMGUI] // This is a slightly modified version of stb_truetype.h 1.20. // Mostly fixing for compiler and static analyzer warnings. // Grep for [DEAR IMGUI] to find the changes. @@ -2538,11 +2538,11 @@ static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, i // There are no other cases. STBTT_assert(0); break; - }; + } // [DEAR IMGUI] removed ; } } break; - }; + } // [DEAR IMGUI] removed ; default: // TODO: Implement other stuff. @@ -4132,7 +4132,7 @@ STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges) { stbtt_fontinfo info; - int i,j,n, return_value = 1; + int i,j,n, return_value; // [DEAR IMGUI] removed = 1 //stbrp_context *context = (stbrp_context *) spc->pack_info; stbrp_rect *rects; From 2bada58e19cacdad40f0b56efd7e1c95d998b8d1 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 18 Mar 2020 18:18:16 +0100 Subject: [PATCH 0226/1018] recovering mesh from sampling --- include/app_viewer.h | 1 + include/mdict/patch.h | 1 + src/app_viewer.cpp | 35 ++++++++++++++++ src/mdict/inpainting.cpp | 88 ++++++++++++++++++++++++---------------- src/mdict/patch.cpp | 29 ++++++++++++- 5 files changed, 119 insertions(+), 35 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 98d82c83..b168a6f2 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -70,6 +70,7 @@ class app_viewer : public viewer static void process_inpaiting(viewer * p_view); static void process_iterative_inpaiting(viewer * p_view); static void process_mask(viewer * p_view); + static void process_pc_reconstruction(viewer * p_view); static void process_synthesis(viewer * p_view); static void process_functional_maps(viewer * p_view); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 391d05df..09194de8 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -67,6 +67,7 @@ class patch distance_t & geo_radio, double delta, double sum_thres); + void init_random(vertex c, arma::mat T, distance_t radio); void recover_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b3b6f9d6..aceedc65 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -87,6 +87,7 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_A, {"A", "MDICT Super Resolution", process_super_resolution}); add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); + add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); add_process(GLFW_KEY_F14, {"F14", "MDICT Synthesis", process_synthesis}); // add_process('A', "IT Inpainting", process_iterative_inpaiting); @@ -602,6 +603,40 @@ void app_viewer::process_mask(viewer * p_view) mesh.update_normals(); } +void app_viewer::process_pc_reconstruction(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->mesh(); + + size_t n = 12; // dct + size_t m = 144, M = 0; + distance_t f = 1; + bool learn = 0; + size_t avg_p = 36; + size_t percentage = 0; + double delta = PI/6; + double sum_thres; + + gproshan_input(sum_thres ); + cin >> sum_thres; + + basis * phi = new basis_dct(n); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + + dict.point_cloud_reconstruction(); + //dict.init_voronoi_patches(); + delete phi; + mesh.update_colors(&dict[0]); + string f_points = tmp_file_path(mesh->name_size() + ".points"); + a_vec points_out; + points_out.load(f_points); + for(int i = 0; i< points_out.size(); i++) + view->select_vertices.push_back(points_out(i)); + + mesh.update_normals(); +} + void app_viewer::process_synthesis(viewer * p_view) { gproshan_log(APP_VIEWER); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 1370a2d1..a44c91d5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -353,24 +353,27 @@ void inpainting::init_radial_feature_patches() if(save_all) { arma::mat AS; - AS.resize(M,11); + AS.resize(M,13); for(index_t i = 0; i < M; i++) { patch & p = patches[i]; - AS(i,0) = p.vertices[0]; - AS(i,1) = p.radio; - AS(i,2) = p.T(0,0); - AS(i,3) = p.T(1,0); - AS(i,4) = p.T(2,0); - AS(i,5) = p.T(0,1); - AS(i,6) = p.T(1,1); - AS(i,7) = p.T(2,1); - AS(i,8) = p.T(0,2); - AS(i,9) = p.T(1,2); - AS(i,10) = p.T(2,2); + + AS(i,0) = p.x(0); + AS(i,1) = p.x(1); + AS(i,2) = p.x(2); + AS(i,3) = p.radio; + AS(i,4) = p.T(0,0); + AS(i,5) = p.T(1,0); + AS(i,6) = p.T(2,0); + AS(i,7) = p.T(0,1); + AS(i,8) = p.T(1,1); + AS(i,9) = p.T(2,1); + AS(i,10) = p.T(0,2); + AS(i,11) = p.T(1,2); + AS(i,12) = p.T(2,2); } - string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".allsmp"); + string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".smp"); AS.save(f_samplall); } gproshan_log(radial patches are ready); @@ -534,32 +537,49 @@ distance_t inpainting::execute() void inpainting::point_cloud_reconstruction() { - arma::mat AS; + arma::mat S; + arma::mat T(3,3); + arma::mat alpha; + - string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".allsmp"); - AS.load(f_samplall); + string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".smp"); + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".alpha"); + + S.load(f_smp); + alpha.load(f_alpha); + gproshan_debug_var(S.n_rows); + M = S.n_rows; + distance_t radio; + patches.resize(M); + vertex c; + for(index_t i = 0; i < M; i++) + { + c.x = S(i,0); + c.y = S(i,1); + c.z = S(i,2); + radio = S(i,3); + T(0,0) = S(i,4); + T(1,0) = S(i,5); + T(2,0) = S(i,6); + T(0,1) = S(i,7); + T(1,1) = S(i,8); + T(2,1) = S(i,9); + T(0,2) = S(i,10); + T(1,2) = S(i,11); + T(2,2) = S(i,12); + + patches[i].init_random(c, T, radio); + } +/* + a_vec x = rp.phi * A * alpha.col(p); + rp.xyz.row(2) = x.t(); + rp.iscale_xyz(radio); + rp.itransform(); +*/ //create patches // recover points // save points // show and put seeds on it - /* - AS.resize(M,11); - for(index_t i = 0; i < M; i++) - { - patch & p = patches[i]; - AS(i,0) = p.vertices[0]; - AS(i,1) = p.radio; - AS(i,2) = p.T(0,0); - AS(i,3) = p.T(1,0); - AS(i,4) = p.T(2,0); - AS(i,5) = p.T(0,1); - AS(i,6) = p.T(1,1); - AS(i,7) = p.T(2,1); - AS(i,8) = p.T(0,2); - AS(i,9) = p.T(1,2); - AS(i,10) = p.T(2,2); - - }*/ } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 9004dd45..ad71779a 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -16,6 +16,7 @@ #include #define PI 3.14159265 #include +#include // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -122,10 +123,36 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc N.push_back(min_he); return added; } +void patch::init_random(vertex c, arma::mat T, distance_t radio) +{ + this->T = T; + x.resize(3); + x(0) = c.x; + x(1) = c.y; + x(2) = c.z; + this->radio = radio; + + + std::random_device rd; //Will be used to obtain a seed for the random number engine + std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() + std::uniform_real_distribution<> dis(0, 1); + double a = dis(gen) * 2 * PI; + double r = radio * sqrt(dis(gen)); + + // If you need it in Cartesian coordinates + double x = r * cos(a); + double y = r * sin(a); + gproshan_debug_var(radio); + //gproshan_debug_var(x); + //gproshan_debug_var(y); + +} void patch::recover_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v) { - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 2.e-5); + // for small meshes 6000 0.e-5 + // for others 2.e-5 + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 1.e-5); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); From 861e43762b6e26abed5d45752c50ef97481bc3c5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Mar 2020 10:32:17 +0100 Subject: [PATCH 0227/1018] update normals --- include/che.h | 9 ++-- include/viewer/che_viewer.h | 17 +++---- include/viewer/viewer.h | 35 +++++++-------- src/app_viewer.cpp | 16 +++---- src/che.cpp | 57 ++++++++++++++--------- src/viewer/che_viewer.cpp | 90 +++++++++---------------------------- src/viewer/viewer.cpp | 51 ++++++--------------- 7 files changed, 105 insertions(+), 170 deletions(-) diff --git a/include/che.h b/include/che.h index 90147f53..cb645947 100644 --- a/include/che.h +++ b/include/che.h @@ -45,12 +45,11 @@ class che index_t * ET = nullptr; ///< edge table : e -> he index_t * EHT = nullptr; ///< extra half edge table : he -> e index_t * BT = nullptr; ///< boundary table : b -> v + + vertex * VN = nullptr; ///< vertex normals : v -> normal(v) bool manifold = true; - public: - vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - public: che(const size_t & n_v = 0, const size_t & n_f = 0); che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); @@ -68,9 +67,11 @@ class che area_t area_trig(const index_t & t) const; area_t area_vertex(const index_t & v); area_t area_surface() const; + void update_normals(); + const vertex & normal(const index_t & v) const; vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; + vertex normal_trig(const index_t & f) const; vertex normal_he(const index_t & he) const; - vertex normal(const index_t & v) const; vertex gradient_he(const index_t & he, const distance_t *const & f) const; vertex gradient(const index_t & v, const distance_t *const & f); vertex barycenter(const index_t & t) const; diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index d9ee9b8b..d03e1b1c 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -26,16 +26,14 @@ typedef real_t color_t; class che_viewer { protected: - che * mesh; + che * mesh = nullptr; + color_t * colors = nullptr; - size_t n_instances; - size_t n_vertices; // current number of vertices - bool invert_normals; + size_t n_instances = 0; + bool invert_normals = false; + bool normalize = false; vertex v_translate; - vertex * normals; - color_t * colors; - GLuint vao; GLuint vbo[5]; @@ -49,19 +47,16 @@ class che_viewer virtual ~che_viewer(); che *& operator -> (); operator che *& (); - void init(che * _mesh, const bool & normalize = true); + void init(che * mesh, const bool & normalize = true); void reload(); void update(); void update_vbo(); - void update_normals(); void update_colors(const color_t *const c = nullptr); void update_instances_translations(const std::vector & translations); void draw(shader & program); void draw_point_cloud(shader & program); color_t & color(const index_t & v); - vertex & normal(const index_t & v); - vertex *& normals_ptr(); void translate(const vertex & p); void invert_orientation(); diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 98d2d355..34a9c258 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -89,7 +89,7 @@ class viewer static const int m_window_size[N_MESHES][2]; - GLFWwindow * window; + GLFWwindow * window = nullptr; int viewport_width; int viewport_height; @@ -111,31 +111,31 @@ class viewer che_viewer meshes[N_MESHES]; vcorr_t corr_mesh[N_MESHES]; - size_t n_meshes; - index_t current; // current mesh + size_t n_meshes = 0; + index_t current = 0; // current mesh - index_t render_opt; - - frame * render_frame; + index_t render_opt = 0; + + frame * render_frame = nullptr; #ifdef GPROSHAN_EMBREE - rt::embree * rt_embree; + rt::embree * rt_embree = nullptr; #endif // GPROSHAN_EMBREE #ifdef GPROSHAN_OPTIX - rt::optix * rt_optix; + rt::optix * rt_optix = nullptr; #endif // GPROSHAN_OPTIX - bool action; + bool action = false; - bool render_wireframe; - bool render_wireframe_fill; - bool render_gradient_field; - bool render_normal_field; - bool render_border; - bool render_lines; - bool render_flat; - float bgc; + bool render_wireframe = false; + bool render_wireframe_fill = false; + bool render_gradient_field = false; + bool render_normal_field = false; + bool render_border = false; + bool render_lines = false; + bool render_flat = false; + float bgc = 0; std::map processes; @@ -181,7 +181,6 @@ class viewer static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); // menu functions - void menu_process(int value); void menu_process(function_t pro); static void menu_help(viewer * view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 58ed019c..0fd169f3 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -218,7 +218,7 @@ void app_viewer::process_noise(viewer * p_view) mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_black_noise(viewer * p_view) @@ -238,7 +238,7 @@ void app_viewer::process_black_noise(viewer * p_view) mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_threshold(viewer * p_view) @@ -510,7 +510,7 @@ void app_viewer::process_denoising(viewer * p_view) dict.execute(); delete phi; - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_super_resolution(viewer * p_view) @@ -532,7 +532,7 @@ void app_viewer::process_super_resolution(viewer * p_view) dict.execute(); delete phi; - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_inpaiting(viewer * p_view) @@ -554,7 +554,7 @@ void app_viewer::process_inpaiting(viewer * p_view) dict.execute(); delete phi; - mesh.update_normals(); + mesh->update_normals(); } @@ -681,7 +681,7 @@ void app_viewer::process_fairing_spectral(viewer * p_view) mesh->set_vertices(fair->get_postions()); delete fair; - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_fairing_taubin(viewer * p_view) @@ -699,7 +699,7 @@ void app_viewer::process_fairing_taubin(viewer * p_view) mesh->set_vertices(fair->get_postions()); delete fair; - mesh.update_normals(); + mesh->update_normals(); } void app_viewer::process_geodesics_fm(viewer * p_view) @@ -889,7 +889,7 @@ void app_viewer::process_edge_collapse(viewer * p_view) index_t levels; cin >> levels; - TIC(view->time) decimation sampling(mesh, mesh.normals_ptr(), levels); TOC(view->time) + TIC(view->time) decimation sampling(mesh, &mesh->normal(0), levels); TOC(view->time) gproshan_debug_var(view->time); if(view->n_meshes < 2) diff --git a/src/che.cpp b/src/che.cpp index dec1b0c7..ef3d8864 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -238,6 +238,29 @@ area_t che::area_surface() const return area; } +void che::update_normals() +{ + if(!VN) VN = new vertex[n_vertices_]; + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices_; v++) + { + vertex & n = VN[v]; + + n = 0; + for_star(he, this, v) + n += area_trig(trig(he)) * normal_he(he); + + n /= *n; + } +} + +const vertex & che::normal(const index_t & v) const +{ + assert(VN && v < n_vertices_); + return VN[v]; +} + vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const { index_t he = f * che::P; @@ -245,25 +268,14 @@ vertex che::shading_normal(const index_t & f, const float & u, const float & v, return {u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]}; } -vertex che::normal_he(const index_t & he) const +vertex che::normal_trig(const index_t & f) const { - vertex n = (GT[VT[next(he)]] - GT[VT[he]]) * (GT[VT[prev(he)]] - GT[VT[he]]); - return n / *n; + return normal_he(f * che::P); } -vertex che::normal(const index_t & v) const +vertex che::normal_he(const index_t & he) const { - vertex n; - area_t area, area_star = 0; - - for_star(he, this, v) - { - area = area_trig(trig(he)); - area_star += area; - n += area * normal_he(he); - } - - n /= area_star; + vertex n = (GT[VT[next(he)]] - GT[VT[he]]) * (GT[VT[prev(he)]] - GT[VT[he]]); return n / *n; } @@ -1404,13 +1416,14 @@ void che::update_bt() void che::delete_me() { - delete [] GT; - delete [] VT; - delete [] OT; - delete [] EVT; - delete [] ET; - delete [] EHT; - delete [] BT; + delete [] GT; GT = nullptr; + delete [] VT; VT = nullptr; + delete [] OT; OT = nullptr; + delete [] EVT; EVT = nullptr; + delete [] ET; ET = nullptr; + delete [] EHT; EHT = nullptr; + delete [] BT; BT = nullptr; + delete [] VN; VN = nullptr; } void che::read_file(const string & ) diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index c70cf541..1c28e5e4 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -11,26 +11,17 @@ using namespace std; namespace gproshan { -che_viewer::che_viewer() +che_viewer::~che_viewer() { - mesh = nullptr; - n_vertices = 0; - n_instances = 0; - - normals = nullptr; - colors = nullptr; - - vx = vy = 0; + glGenVertexArrays(1, &vao); + glGenBuffers(5, vbo); } che_viewer::~che_viewer() { - if(n_vertices) return; - glDeleteBuffers(5, vbo); glDeleteVertexArrays(1, &vao); - if(normals) delete [] normals; if(colors) delete [] colors; } @@ -44,51 +35,32 @@ che_viewer::operator che *& () return mesh; } -void che_viewer::init(che * _mesh, const bool & normalize) +void che_viewer::init(che * mesh, const bool & normalize) { - n_vertices = 0; - mesh = _mesh; - - if(normalize) - mesh->normalize(); - - invert_normals = false; - - glGenVertexArrays(1, &vao); - glGenBuffers(5, vbo); + this->mesh = m; + this->normalize = normalize; update(); } void che_viewer::reload() { - n_vertices = 0; mesh->reload(); - mesh->normalize(); - update(); - - translate(v_translate); update(); } void che_viewer::update() { - assert(mesh != nullptr); - - if(n_vertices != mesh->n_vertices()) - { - if(normals) delete [] normals; - if(colors) delete [] colors; - - n_vertices = mesh->n_vertices(); - normals = new vertex[n_vertices]; - colors = new color_t[n_vertices]; + if(normalize) mesh->normalize(); + + factor = mesh->mean_edge(); + + mesh->update_normals(); - update_normals(); - update_colors(); - } + delete [] colors; + colors = new color_t[mesh->n_vertices()]; + update_colors(); - factor = mesh->mean_edge(); update_vbo(); } @@ -99,21 +71,21 @@ void che_viewer::update_vbo() // 0 VERTEX glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(vertex), normals, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, n_vertices * sizeof(real_t), colors, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), colors, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -129,24 +101,12 @@ void che_viewer::update_vbo() glBindVertexArray(0); } -void che_viewer::update_normals() -{ - mesh->VN = normals; - - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) - if(invert_normals) - normals[v] = -mesh->normal(v); - else - normals[v] = mesh->normal(v); -} - void che_viewer::update_colors(const color_t *const c) { if(!c) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) colors[v] = COLOR; return; @@ -155,12 +115,12 @@ void che_viewer::update_colors(const color_t *const c) distance_t max_c = 0; #pragma omp parallel for reduction(max: max_c) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) if(c[v] < INFINITY) max_c = max(c[v], max_c); #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices(); v++) colors[v] = c[v] / max_c; } @@ -221,16 +181,6 @@ color_t & che_viewer::color(const index_t & v) return colors[v]; } -vertex & che_viewer::normal(const index_t & v) -{ - return normals[v]; -} - -vertex *& che_viewer::normals_ptr() -{ - return normals; -} - void che_viewer::translate(const vertex & p) { v_translate = p; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 028036ae..d7c6a778 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -36,33 +36,6 @@ const int viewer::m_window_size[N_MESHES][2] = {{1, 1}, {1, 2}, {1, 3}, viewer::viewer() { - window = nullptr; - - n_meshes = current = 0; - - render_opt = 0; - render_frame = nullptr; - - #ifdef GPROSHAN_EMBREE - rt_embree = nullptr; - #endif // GPROSHAN_EMBREE - - #ifdef GPROSHAN_OPTIX - rt_optix = nullptr; - #endif // GPROSHAN_OPTIX - - render_wireframe = false; - render_wireframe_fill = false; - render_gradient_field = false; - render_normal_field = false; - render_border = false; - render_lines = false; - render_flat = false; - - bgc = 0; - - action = false; - init_gl(); init_glsl(); init_imgui(); @@ -113,25 +86,33 @@ bool viewer::run() light = r.conj() * light * r; up = r.conj() * up * r; + proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), glm::vec3(center[1], center[2], center[3]), glm::vec3(up[1], up[2], up[3]) ); + + double render_time = glfwGetTime(); // RENDER glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { + case 0: render_gl(); break; case 1: render_embree(); break; case 2: render_optix(); break; - default: render_gl(); } + render_time = glfwGetTime() - render_time; + ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); + ImGui::Text("Frame Time: %.2lfms", render_time * 1000.0); + ImGui::Text("Frame Rate: %.2lffps", 1.0 / render_time); + if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) @@ -169,8 +150,7 @@ bool viewer::run() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); glfwSwapBuffers(window); - glfwWaitEvents(); - //glfwPollEvents(); + glfwPollEvents(); } return true; @@ -294,11 +274,6 @@ void viewer::init_glsl() shader_pointcloud.load_fragment("../shaders/fragment_pointcloud.glsl"); } -void viewer::menu_process(int value) -{ - menu_process(processes[value].function); -} - void viewer::add_process(const int & key, const process_t & process) { if(processes.find(key) == processes.end()) @@ -398,7 +373,9 @@ void viewer::idle() void viewer::menu_process(function_t pro) { - if(pro) pro(this); + if(!pro) return; + + pro(this); mesh().update_vbo(); } @@ -469,7 +446,7 @@ void viewer::menu_bgc_black(viewer * view) void viewer::invert_orientation(viewer * view) { view->mesh().invert_orientation(); - view->mesh().update_normals(); + view->mesh()->update_normals(); view->mesh().update_vbo(); } From d124274153e436f3cba3a00d3fb12878386a490e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Mar 2020 10:36:16 +0100 Subject: [PATCH 0228/1018] che viewer init buffers --- include/viewer/che_viewer.h | 2 +- src/viewer/che_viewer.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index d03e1b1c..06ea2f80 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -43,7 +43,7 @@ class che_viewer std::vector selected; public: - che_viewer(); + che_viewer() = default; virtual ~che_viewer(); che *& operator -> (); operator che *& (); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 1c28e5e4..2ba78295 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -11,12 +11,6 @@ using namespace std; namespace gproshan { -che_viewer::~che_viewer() -{ - glGenVertexArrays(1, &vao); - glGenBuffers(5, vbo); -} - che_viewer::~che_viewer() { glDeleteBuffers(5, vbo); @@ -37,7 +31,10 @@ che_viewer::operator che *& () void che_viewer::init(che * mesh, const bool & normalize) { - this->mesh = m; + glGenVertexArrays(1, &vao); + glGenBuffers(5, vbo); + + this->mesh = mesh; this->normalize = normalize; update(); @@ -61,7 +58,6 @@ void che_viewer::update() colors = new color_t[mesh->n_vertices()]; update_colors(); - update_vbo(); } From 5d3d17aa0f80eeb8ddd2ac00208da42ba9ebd964 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Mar 2020 10:43:16 +0100 Subject: [PATCH 0229/1018] cleaning code --- src/viewer/che_viewer.cpp | 5 +++-- src/viewer/viewer.cpp | 7 ------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 2ba78295..96afb5da 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -54,8 +54,6 @@ void che_viewer::update() mesh->update_normals(); - delete [] colors; - colors = new color_t[mesh->n_vertices()]; update_colors(); update_vbo(); @@ -99,6 +97,9 @@ void che_viewer::update_vbo() void che_viewer::update_colors(const color_t *const c) { + delete [] colors; + colors = new color_t[mesh->n_vertices()]; + if(!c) { #pragma omp parallel for diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d7c6a778..1e19c5b1 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -93,8 +93,6 @@ bool viewer::run() glm::vec3(up[1], up[2], up[3]) ); - double render_time = glfwGetTime(); - // RENDER glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) @@ -104,15 +102,10 @@ bool viewer::run() case 2: render_optix(); break; } - render_time = glfwGetTime() - render_time; - ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - ImGui::Text("Frame Time: %.2lfms", render_time * 1000.0); - ImGui::Text("Frame Rate: %.2lffps", 1.0 / render_time); - if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) From 7a7a9c91a47fcc4699eca1ca26d52071510f2da5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Mar 2020 11:24:07 +0100 Subject: [PATCH 0230/1018] invert face orientation --- include/che.h | 2 ++ include/viewer/che_viewer.h | 1 - src/che.cpp | 6 ++++++ src/viewer/che_viewer.cpp | 4 +++- src/viewer/viewer.cpp | 2 -- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/che.h b/include/che.h index cb645947..2737b822 100644 --- a/include/che.h +++ b/include/che.h @@ -47,6 +47,7 @@ class che index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) + vertex * VC = nullptr; ///< vertex color : v -> color(v) bool manifold = true; @@ -69,6 +70,7 @@ class che area_t area_surface() const; void update_normals(); const vertex & normal(const index_t & v) const; + vertex & normal(const index_t & v); vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; vertex normal_trig(const index_t & f) const; vertex normal_he(const index_t & he) const; diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 06ea2f80..c4a36b76 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -30,7 +30,6 @@ class che_viewer color_t * colors = nullptr; size_t n_instances = 0; - bool invert_normals = false; bool normalize = false; vertex v_translate; diff --git a/src/che.cpp b/src/che.cpp index ef3d8864..25e1950a 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -261,6 +261,12 @@ const vertex & che::normal(const index_t & v) const return VN[v]; } +vertex & che::normal(const index_t & v) +{ + assert(VN && v < n_vertices_); + return VN[v]; +} + vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const { index_t he = f * che::P; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 96afb5da..7693a447 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -189,7 +189,9 @@ void che_viewer::translate(const vertex & p) void che_viewer::invert_orientation() { - invert_normals = !invert_normals; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh->normal(v) = -mesh->normal(v); } void che_viewer::log_info() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1e19c5b1..1fe5ab81 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -439,8 +439,6 @@ void viewer::menu_bgc_black(viewer * view) void viewer::invert_orientation(viewer * view) { view->mesh().invert_orientation(); - view->mesh()->update_normals(); - view->mesh().update_vbo(); } void viewer::set_render_gl(viewer * view) From 4349ab51da607b8c20595b0bc1e3675c7e000a0c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Mar 2020 12:41:37 +0100 Subject: [PATCH 0231/1018] typedef real_t --- include/app_viewer.h | 2 +- include/che.h | 15 ++-- include/che_fill_hole.h | 6 +- include/cuda/geodesics_ptp.cuh | 12 +-- include/cuda/geodesics_ptp_coalescence.cuh | 6 +- include/cuda/test_geodesics_ptp.cuh | 2 +- .../cuda/test_geodesics_ptp_coalescence.cuh | 2 +- include/dijkstra.h | 4 +- include/geodesics.h | 22 +++--- include/geodesics_ptp.h | 10 +-- include/heat_flow.h | 4 +- include/include.h | 4 - include/mdict/basis.h | 2 +- include/mdict/basis_cosine.h | 2 +- include/mdict/basis_dct.h | 2 +- include/mdict/d_mesh.h | 8 +- include/mdict/denoising.h | 2 +- include/mdict/dictionary.h | 6 +- include/mdict/inpainting.h | 2 +- include/mdict/patch.h | 4 +- include/mdict/super_resolution.h | 2 +- include/sampling.h | 4 +- include/test_geodesics_ptp.h | 22 +++--- include/viewer/che_viewer.h | 6 -- src/app_viewer.cpp | 72 +++++++++--------- src/che.cpp | 68 +++++++++++++---- src/che_fill_hole.cpp | 26 +++---- src/cuda/geodesics_ptp.cu | 76 +++++++++---------- src/cuda/geodesics_ptp_coalescence.cu | 52 ++++++------- src/cuda/test_geodesics_ptp.cu | 44 +++++------ src/cuda/test_geodesics_ptp_coalescence.cu | 46 +++++------ src/dijkstra.cpp | 10 +-- src/geodesics.cpp | 36 ++++----- src/geodesics_ptp.cpp | 44 +++++------ src/heat_flow.cpp | 8 +- src/key_points.cpp | 2 +- src/mdict/basis_cosine.cpp | 2 +- src/mdict/basis_dct.cpp | 2 +- src/mdict/d_mesh.cpp | 30 ++++---- src/mdict/denoising.cpp | 2 +- src/mdict/dictionary.cpp | 2 +- src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 4 +- src/mdict/super_resolution.cpp | 2 +- src/sampling.cpp | 4 +- src/test_geodesics_ptp.cpp | 34 ++++----- src/viewer/che_viewer.cpp | 38 +--------- 47 files changed, 377 insertions(+), 380 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 24ca2615..57fda2ae 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -35,7 +35,7 @@ class app_viewer : public viewer { private: double time; - distance_t * dist; + real_t * dist; size_t n_dist; public: diff --git a/include/che.h b/include/che.h index 2737b822..30ae2adc 100644 --- a/include/che.h +++ b/include/che.h @@ -47,7 +47,7 @@ class che index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - vertex * VC = nullptr; ///< vertex color : v -> color(v) + real_t * VC = nullptr; ///< vertex color : v -> color(v) bool manifold = true; @@ -65,17 +65,20 @@ class che void flip(const index_t & e); real_t pdetriq(const index_t & t) const; real_t quality(); - area_t area_trig(const index_t & t) const; - area_t area_vertex(const index_t & v); - area_t area_surface() const; + real_t real_trig(const index_t & t) const; + real_t area_vertex(const index_t & v); + real_t area_surface() const; + void update_colors(const real_t * vcolor = nullptr); + const real_t & color(const index_t & v) const; + real_t & color(const index_t & v); void update_normals(); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; vertex normal_trig(const index_t & f) const; vertex normal_he(const index_t & he) const; - vertex gradient_he(const index_t & he, const distance_t *const & f) const; - vertex gradient(const index_t & v, const distance_t *const & f); + vertex gradient_he(const index_t & he, const real_t *const & f) const; + vertex gradient(const index_t & v, const real_t *const & f); vertex barycenter(const index_t & t) const; vertex corr_vertex(corr_t & corr) const; real_t cotan(const index_t & he) const; diff --git a/include/che_fill_hole.h b/include/che_fill_hole.h index a98f1b76..0f7a9232 100644 --- a/include/che_fill_hole.h +++ b/include/che_fill_hole.h @@ -13,7 +13,7 @@ namespace gproshan { struct border_t { - angle_t theta; + real_t theta; index_t v; border_t() = default; @@ -38,7 +38,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & length, const std::array & neighbors, const bool & o) + a_vec new_vertex(const std::vector & V, real_t div, const real_t & length, const std::array & neighbors, const bool & o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -81,7 +81,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, angle_t div, const distance_t & length, const std::array & neighbors, const bool & o, const a_vec & normal) + a_vec new_vertex(const std::vector & V, real_t div, const real_t & length, const std::array & neighbors, const bool & o, const a_vec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; diff --git a/include/cuda/geodesics_ptp.cuh b/include/cuda/geodesics_ptp.cuh index d9e5723a..17771c18 100644 --- a/include/cuda/geodesics_ptp.cuh +++ b/include/cuda/geodesics_ptp.cuh @@ -12,25 +12,25 @@ namespace gproshan { -index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, distance_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); +index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); __forceinline__ __device__ -distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & he); +real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he); __global__ -void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start = 0); +void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start = 0); __global__ -void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * sorted, index_t end, index_t start = 0); +void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * sorted, index_t end, index_t start = 0); __global__ -void relative_error(distance_t * error, const distance_t * new_dist, const distance_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); +void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); struct is_ok { __host__ __device__ - bool operator()(const distance_t & val) const; + bool operator()(const real_t & val) const; }; diff --git a/include/cuda/geodesics_ptp_coalescence.cuh b/include/cuda/geodesics_ptp_coalescence.cuh index 516098e4..90ee1bcc 100644 --- a/include/cuda/geodesics_ptp_coalescence.cuh +++ b/include/cuda/geodesics_ptp_coalescence.cuh @@ -10,13 +10,13 @@ namespace gproshan { -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const std::vector & sources, const toplesets_t & inv, distance_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); +index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); __global__ -void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start = 0); +void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start = 0); __global__ -void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t end, index_t start = 0); +void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t end, index_t start = 0); } // namespace gproshan diff --git a/include/cuda/test_geodesics_ptp.cuh b/include/cuda/test_geodesics_ptp.cuh index 3493887a..66ef6922 100644 --- a/include/cuda/test_geodesics_ptp.cuh +++ b/include/cuda/test_geodesics_ptp.cuh @@ -10,7 +10,7 @@ namespace gproshan { /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const distance_t * exact_dist, distance_t * d_error); +std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error); } // namespace gproshan diff --git a/include/cuda/test_geodesics_ptp_coalescence.cuh b/include/cuda/test_geodesics_ptp_coalescence.cuh index 8b52691e..dfd0ac9e 100644 --- a/include/cuda/test_geodesics_ptp_coalescence.cuh +++ b/include/cuda/test_geodesics_ptp_coalescence.cuh @@ -10,7 +10,7 @@ namespace gproshan { /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const distance_t * exact_dist, distance_t * d_error); +std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error); } // namespace gproshan diff --git a/include/dijkstra.h b/include/dijkstra.h index b27c86c8..ea376ebe 100644 --- a/include/dijkstra.h +++ b/include/dijkstra.h @@ -11,7 +11,7 @@ namespace gproshan { class dijkstra { private: - distance_t * weights; + real_t * weights; index_t * predecessors; size_t n_vertices; index_t source; @@ -19,7 +19,7 @@ class dijkstra public: dijkstra(che * shape, index_t src); ~dijkstra(); - distance_t & operator()(index_t i); + real_t & operator()(index_t i); index_t & operator[](index_t i); void print(std::ostream & os); diff --git a/include/geodesics.h b/include/geodesics.h index 8c0913ed..1a81c072 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -31,7 +31,7 @@ class geodesics index_t * clusters; ///< Clustering vertices to closest source. private: - distance_t * dist; ///< Results of computation geodesic distances. + real_t * dist; ///< Results of computation geodesic distances. index_t * sorted_index; ///< Sort vertices by topological level or geodesic distance. size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; @@ -42,34 +42,34 @@ class geodesics geodesics(che * mesh, ///< input mesh must be a triangular mesh. const std::vector & sources, ///< source vertices. const option_t & opt = FM, ///< specific the algorithm to execute. - distance_t *const & e_dist = nullptr, ///< external dist allocation + real_t *const & e_dist = nullptr, ///< external dist allocation const bool & cluster = false, ///< to cluster vertices to closest source. const size_t & n_iter = 0, ///< maximum number of iterations. - const distance_t & radio = INFINITY ///< execute until the specific radio. + const real_t & radio = INFINITY ///< execute until the specific radio. ); virtual ~geodesics(); - const distance_t & operator[](const index_t & i) const; + const real_t & operator[](const index_t & i) const; const index_t & operator()(const index_t & i) const; - const distance_t & radio() const; + const real_t & radio() const; const index_t & farthest() const; const size_t & n_sorted_index() const; void copy_sorted_index(index_t * indexes, const size_t & n) const; void normalize(); private: - void execute(che * mesh, const std::vector & sources, const size_t & n_iter, const distance_t & radio, const option_t & opt); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const distance_t & radio); - void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const distance_t & radio); + void execute(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const option_t & opt); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); + void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow(che * mesh, const std::vector & sources); #ifdef GPROSHAN_CUDA - void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const size_t & n_iter, const distance_t & radio); + void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow_gpu(che * mesh, const std::vector & sources); #endif // GPROSHAN_CUDA - distance_t update(index_t & d, che * mesh, const index_t & he, vertex & vx); - distance_t planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx); + real_t update(index_t & d, che * mesh, const index_t & he, vertex & vx); + real_t planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx); }; diff --git a/include/geodesics_ptp.h b/include/geodesics_ptp.h index 30173953..3abf5dd7 100644 --- a/include/geodesics_ptp.h +++ b/include/geodesics_ptp.h @@ -17,10 +17,10 @@ namespace gproshan { struct ptp_out_t { - distance_t * dist; + real_t * dist; index_t * clusters; - ptp_out_t(distance_t *const & d, index_t *const & c = nullptr); + ptp_out_t(real_t *const & d, index_t *const & c = nullptr); }; struct toplesets_t @@ -39,11 +39,11 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); -distance_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, distance_t radio = 0); +real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); -distance_t update_step(che * mesh, const distance_t * dist, const index_t & he); +real_t update_step(che * mesh, const real_t * dist, const index_t & he); -void normalize_ptp(distance_t * dist, const size_t & n); +void normalize_ptp(real_t * dist, const size_t & n); } // namespace gproshan diff --git a/include/heat_flow.h b/include/heat_flow.h index f4b722a2..66de7646 100644 --- a/include/heat_flow.h +++ b/include/heat_flow.h @@ -20,10 +20,10 @@ namespace gproshan { -distance_t * heat_flow(che * mesh, const std::vector & sources, double & solve_time); +real_t * heat_flow(che * mesh, const std::vector & sources, double & solve_time); #ifdef GPROSHAN_CUDA -distance_t * heat_flow_gpu(che * mesh, const std::vector & sources, double & solve_time); +real_t * heat_flow_gpu(che * mesh, const std::vector & sources, double & solve_time); #endif // GPROSHAN_CUDA void compute_divergence(che * mesh, const a_mat & u, a_mat & div); diff --git a/include/include.h b/include/include.h index 2ce9952d..2d40507e 100644 --- a/include/include.h +++ b/include/include.h @@ -23,10 +23,6 @@ typedef unsigned int index_t; typedef double real_t; #endif -typedef real_t distance_t; -typedef real_t area_t; -typedef real_t angle_t; - #define TMP_DIR "../tmp/" #define tmp_file_path(file) (std::string(TMP_DIR) + file) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 9d4e8d0e..c9474f57 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -19,7 +19,7 @@ class dictionary; class basis { protected: - distance_t radio; + real_t radio; size_t dim; public: diff --git a/include/mdict/basis_cosine.h b/include/mdict/basis_cosine.h index 2e429cd4..5628ee4e 100644 --- a/include/mdict/basis_cosine.h +++ b/include/mdict/basis_cosine.h @@ -16,7 +16,7 @@ class basis_cosine: public basis real_t n; // frequency public: - basis_cosine(const size_t & _r, const size_t & _n, const distance_t & _radio = 0); + basis_cosine(const size_t & _r, const size_t & _n, const real_t & _radio = 0); void discrete(a_mat & phi, const a_mat & xy); private: diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index a56110ae..d9337cf2 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -15,7 +15,7 @@ class basis_dct: public basis real_t n; // frequency public: - basis_dct(const size_t & _n, const distance_t & _radio = 0); + basis_dct(const size_t & _n, const real_t & _radio = 0); void discrete(a_mat & phi, const a_mat & xy); private: diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index 620d17b9..b44c21b8 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -99,7 +99,7 @@ struct patch_t a_vec gaussian(a_mat & xy, real_t sigma, real_t cx, real_t cy); -a_vec cossine(a_mat & xy, distance_t radio, size_t K); +a_vec cossine(a_mat & xy, real_t radio, size_t K); void phi_gaussian(a_mat & phi, a_mat & xy, void ** params); @@ -113,15 +113,15 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, st void mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); +a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); /// DEPRECATED void mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); /// DEPRECATED -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); +a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); -a_vec simple_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const distance_t & h); +a_vec simple_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); } // namespace gproshan::mdict diff --git a/include/mdict/denoising.h b/include/mdict/denoising.h index 505b0eb0..cfde58ac 100644 --- a/include/mdict/denoising.h +++ b/include/mdict/denoising.h @@ -12,7 +12,7 @@ namespace gproshan::mdict { class denoising : public dictionary { public: - denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot = true); virtual ~denoising() = default; void execute(); diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index a943a520..3dd26c20 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -28,8 +28,8 @@ class dictionary a_mat A; ///< dictionary continuous matrix. a_mat alpha; ///< sparse coding matrix. - distance_t f; - distance_t s_radio; ///< sampling geodesic radio. + real_t f; + real_t s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. @@ -46,7 +46,7 @@ class dictionary basis *const &_phi_basis, ///< pointer to continuous basis. const size_t & _m, ///< number of dictionary atoms. const size_t & _M, ///< number of patches. - const distance_t & _f, ///< deprecated + const real_t & _f, ///< deprecated const bool & _plot ///< flag to plot basis and atoms with gnuplot. ); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index d43608a8..fccf36b5 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -14,7 +14,7 @@ namespace gproshan::mdict { class inpainting : public dictionary { public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot = true); virtual ~inpainting() = default; void execute(); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 1483a7c3..b05ed9bc 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -40,7 +40,7 @@ class patch void init( che * mesh, ///< input mesh. const index_t & v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. - const distance_t & radio, ///< euclidean radio in XY of the patch. + const real_t & radio, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); @@ -65,7 +65,7 @@ class patch /// Gather the vertices filter by radio in the local coordinates require initialize T and x. void gather_vertices( che * mesh, const index_t & v, - const distance_t & radio, + const real_t & radio, index_t * toplevel ); diff --git a/include/mdict/super_resolution.h b/include/mdict/super_resolution.h index 228085ba..a49b69a3 100644 --- a/include/mdict/super_resolution.h +++ b/include/mdict/super_resolution.h @@ -12,7 +12,7 @@ namespace gproshan::mdict { class super_resolution : public dictionary { public: - super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot = true); + super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot = true); virtual ~super_resolution() = default; void execute(); diff --git a/include/sampling.h b/include/sampling.h index b27ecda0..ffa7e139 100644 --- a/include/sampling.h +++ b/include/sampling.h @@ -11,9 +11,9 @@ namespace gproshan { -index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, distance_t radio); +index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, real_t radio); -bool load_sampling(std::vector & points, distance_t & radio, che * mesh, size_t M); +bool load_sampling(std::vector & points, real_t & radio, che * mesh, size_t M); } // namespace gproshan diff --git a/include/test_geodesics_ptp.h b/include/test_geodesics_ptp.h index 8c6e192b..4ba9a050 100644 --- a/include/test_geodesics_ptp.h +++ b/include/test_geodesics_ptp.h @@ -12,41 +12,41 @@ namespace gproshan { /// Execute performance and accuracy test for ptp algorithm on cpu and gpu. void main_test_geodesics_ptp(const int & nargs, const char ** args); -double test_fast_marching(distance_t & error, const distance_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); -double test_ptp_cpu(distance_t & error, const distance_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); +double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); -double test_heat_method_cholmod(distance_t & error, double & stime, const distance_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); #ifdef GPROSHAN_CUDA -double test_ptp_gpu(distance_t & error, const distance_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); +double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); -double test_heat_method_gpu(distance_t & error, double & stime, const distance_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const distance_t * exact_dist, double & time_ptp); +std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp); /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const distance_t * exact_dist, double & time_ptp); +std::vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp); /// Return an array with the time per iteration. -double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, distance_t radio = 0); +double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, real_t radio = 0); /// Return an array with the time per iteration. -double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, distance_t radio = 0); +double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, real_t radio = 0); #endif // GPROSHAN_CUDA /// Exact geodesics computed using MeshLP https://github.com/areslp/matlab/tree/master/MeshLP/MeshLP, /// Geodesics code: http://code.google.com/p/geodesic/ -distance_t * load_exact_geodesics(const std::string & file, const size_t & n); +real_t * load_exact_geodesics(const std::string & file, const size_t & n); -distance_t compute_error(const distance_t * dist, const distance_t * exact, const size_t & n, const size_t & s); +real_t compute_error(const real_t * dist, const real_t * exact, const size_t & n, const size_t & s); } // namespace gproshan diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index c4a36b76..bee38b46 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -6,7 +6,6 @@ #include "include_opengl.h" -#define COLOR 0.4 #ifdef SINGLE_P #define glVertex3v(x) glVertex3fv(x) @@ -21,13 +20,10 @@ namespace gproshan { -typedef real_t color_t; - class che_viewer { protected: che * mesh = nullptr; - color_t * colors = nullptr; size_t n_instances = 0; bool normalize = false; @@ -50,12 +46,10 @@ class che_viewer void reload(); void update(); void update_vbo(); - void update_colors(const color_t *const c = nullptr); void update_instances_translations(const std::vector & translations); void draw(shader & program); void draw_point_cloud(shader & program); - color_t & color(const index_t & v); void translate(const vertex & p); void invert_orientation(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 0fd169f3..23aabb0d 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -131,7 +131,7 @@ void paint_holes_vertices(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - if(v >= nv) mesh.color(v) = .25; + if(v >= nv) mesh->color(v) = .25; } void app_viewer::process_delete_non_manifold_vertices(viewer * p_view) @@ -214,7 +214,7 @@ void app_viewer::process_noise(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) { - distance_t r = distance_t(d_mod_1000(generator)) / 200000; + real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } @@ -234,7 +234,7 @@ void app_viewer::process_black_noise(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) { - distance_t r = distance_t(d_mod_1000(generator)) / 200000; + real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } @@ -248,7 +248,7 @@ void app_viewer::process_threshold(viewer * p_view) che_viewer & mesh = view->mesh(); for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) = mesh.color(v) > 0.5 ? 1 : 0.5; + mesh->color(v) = mesh->color(v) > 0.5 ? 1 : 0.5; } void app_viewer::process_functional_maps(viewer * p_view) @@ -283,7 +283,7 @@ void app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - view->mesh().color(v) = eigvec(v, k); + view->mesh()->color(v) = eigvec(v, k); view->mesh().update_vbo(); } @@ -310,7 +310,7 @@ void app_viewer::process_wks(viewer * p_view) TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) gproshan_log_var(view->time); - distance_t max_s = 0; + real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) { @@ -319,13 +319,13 @@ void app_viewer::process_wks(viewer * p_view) for(index_t k = 1; k < K; k++) s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - mesh.color(v) = norm(s); - max_s = max(max_s, mesh.color(v)); + mesh->color(v) = norm(s); + max_s = max(max_s, mesh->color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) /= max_s; + mesh->color(v) /= max_s; } void app_viewer::process_hks(viewer * p_view) @@ -350,7 +350,7 @@ void app_viewer::process_hks(viewer * p_view) if(!K) return; - distance_t max_s = 0; + real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) { @@ -359,14 +359,14 @@ void app_viewer::process_hks(viewer * p_view) for(index_t k = 1; k < K; k++) s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); - mesh.color(v) = norm(abs(arma::fft(s, 128))); - //mesh.color(v) = norm(s); - max_s = max(max_s, mesh.color(v)); + mesh->color(v) = norm(abs(arma::fft(s, 128))); + //mesh->color(v) = norm(s); + max_s = max(max_s, mesh->color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) /= max_s; + mesh->color(v) /= max_s; } void app_viewer::process_gps(viewer * p_view) @@ -396,17 +396,17 @@ void app_viewer::process_gps(viewer * p_view) a_mat data = eigvec.t(); a_mat means; - distance_t max_s = 0; + real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) { - mesh.color(v) = norm(eigvec.row(v)); - max_s = max(max_s, mesh.color(v)); + mesh->color(v) = norm(eigvec.row(v)); + max_s = max(max_s, mesh->color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) /= max_s; + mesh->color(v) /= max_s; } void app_viewer::process_key_points(viewer * p_view) @@ -437,7 +437,7 @@ void app_viewer::process_key_components(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) = (real_t) kcs(v) / kcs; + mesh->color(v) = (real_t) kcs(v) / kcs; } void app_viewer::process_mdict_patch(viewer * p_view) @@ -452,12 +452,12 @@ void app_viewer::process_mdict_patch(viewer * p_view) vertex vdir; patch p; - distance_t mean_edge = mesh->mean_edge(); + real_t mean_edge = mesh->mean_edge(); for(auto & v: view->mesh().selected) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) - mesh.color(u) = 1; + mesh->color(u) = 1; vdir.x = p.T(0, 0); vdir.y = p.T(0, 1); @@ -499,7 +499,7 @@ void app_viewer::process_denoising(viewer * p_view) size_t n; // dct size_t m, M; - distance_t f; + real_t f; bool learn; gproshan_input(n m M f learn); @@ -521,7 +521,7 @@ void app_viewer::process_super_resolution(viewer * p_view) size_t n; // dct size_t m, M; - distance_t f; + real_t f; bool learn; gproshan_log(parameters: (n, m, M, f, learn)); @@ -543,7 +543,7 @@ void app_viewer::process_inpaiting(viewer * p_view) size_t n; // dct size_t m, M; - distance_t f; + real_t f; bool learn; gproshan_log(parameters: (n, m, M, f, learn)); @@ -595,7 +595,7 @@ void app_viewer::compute_toplesets(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices(); v++) { if(toplesets[v] < n_toplesets) - mesh.color(v) = distance_t(toplesets[v]) / (n_toplesets); + mesh->color(v) = real_t(toplesets[v]) / (n_toplesets); } gproshan_debug_var(n_toplesets); @@ -622,8 +622,8 @@ void app_viewer::process_voronoi(viewer * p_view) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - mesh.color(i) = ptp.clusters[i]; - mesh.color(i) /= view->mesh().selected.size() + 1; + mesh->color(i) = ptp.clusters[i]; + mesh->color(i) /= view->mesh().selected.size() + 1; } } @@ -634,7 +634,7 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) che_viewer & mesh = view->mesh(); gproshan_input(radio); - distance_t radio; cin >> radio; + real_t radio; cin >> radio; #ifdef GPROSHAN_CUDA // IMPLEMENT/REVIEW double time_fps; @@ -659,7 +659,7 @@ void app_viewer::process_farthest_point_sampling(viewer * p_view) gproshan_input(samples_number); index_t n; cin >> n; - distance_t radio; + real_t radio; TIC(view->time) load_sampling(view->mesh().selected, radio, mesh, n); TOC(view->time) @@ -716,7 +716,7 @@ void app_viewer::process_geodesics_fm(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh.update_colors(&fm[0]); + mesh->update_colors(&fm[0]); } void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) @@ -733,7 +733,7 @@ void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh.update_colors(&ptp[0]); + mesh->update_colors(&ptp[0]); } void app_viewer::process_geodesics_heat_flow(viewer * p_view) @@ -750,7 +750,7 @@ void app_viewer::process_geodesics_heat_flow(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh.update_colors(&heat_flow[0]); + mesh->update_colors(&heat_flow[0]); } @@ -770,7 +770,7 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) delete [] view->dist; view->n_dist = mesh->n_vertices(); - view->dist = new distance_t[view->n_dist]; + view->dist = new real_t[view->n_dist]; } TIC(view->time) @@ -778,7 +778,7 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh.update_colors(&ptp[0]); + mesh->update_colors(&ptp[0]); } void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) @@ -795,7 +795,7 @@ void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh.update_colors(&heat_flow[0]); + mesh->update_colors(&heat_flow[0]); } #endif // GPROSHAN_CUDA @@ -877,7 +877,7 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh.color(v) = f(gv(v)); + mesh->color(v) = f(gv(v)); } void app_viewer::process_edge_collapse(viewer * p_view) diff --git a/src/che.cpp b/src/che.cpp index 25e1950a..38a20c68 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -195,7 +195,7 @@ real_t che::pdetriq(const index_t & t) const *(GT[VT[prev(he)]] - GT[VT[next(he)]]), *(GT[VT[he]] - GT[VT[prev(he)]]) }; - return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); + return (4 * sqrt(3) * real_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } real_t che::quality() @@ -209,7 +209,7 @@ real_t che::quality() return q * 100 / n_faces_; } -area_t che::area_trig(const index_t & t) const +real_t che::real_trig(const index_t & t) const { index_t he = t * P; vertex a = GT[VT[next(he)]] - GT[VT[he]]; @@ -218,26 +218,63 @@ area_t che::area_trig(const index_t & t) const return *(a * b) / 2; } -area_t che::area_vertex(const index_t & v) +real_t che::area_vertex(const index_t & v) { - area_t area_star = 0; + real_t area_star = 0; for_star(he, this, v) - area_star += area_trig(trig(he)); + area_star += real_trig(trig(he)); return area_star / 3; } -area_t che::area_surface() const +real_t che::area_surface() const { - area_t area = 0; + real_t area = 0; #pragma omp parallel for reduction(+: area) for(index_t i = 0; i < n_faces_; i++) - area += area_trig(i); + area += real_trig(i); return area; } +void che::update_colors(const real_t * vcolor) +{ + if(!VC) VC = new real_t[n_vertices_]; + + if(!vcolor) + { + #pragma omp parallel for + for(index_t v = 0; v < n_vertices_; v++) + VC[v] = 0.5; + + return; + } + + real_t max_c = 0; + + #pragma omp parallel for reduction(max: max_c) + for(index_t v = 0; v < n_vertices_; v++) + if(vcolor[v] < INFINITY) + max_c = max(vcolor[v], max_c); + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices_; v++) + VC[v] = vcolor[v] / max_c; +} + +const real_t & che::color(const index_t & v) const +{ + assert(VC && v < n_vertices_); + return VC[v]; +} + +real_t & che::color(const index_t & v) +{ + assert(VC && v < n_vertices_); + return VC[v]; +} + void che::update_normals() { if(!VN) VN = new vertex[n_vertices_]; @@ -249,7 +286,7 @@ void che::update_normals() n = 0; for_star(he, this, v) - n += area_trig(trig(he)) * normal_he(he); + n += real_trig(trig(he)) * normal_he(he); n /= *n; } @@ -285,7 +322,7 @@ vertex che::normal_he(const index_t & he) const return n / *n; } -vertex che::gradient_he(const index_t & he, const distance_t *const & f) const +vertex che::gradient_he(const index_t & he, const real_t *const & f) const { index_t i = VT[he]; index_t j = VT[next(he)]; @@ -297,7 +334,7 @@ vertex che::gradient_he(const index_t & he, const distance_t *const & f) const vertex n = normal_he(he); - area_t A2 = area_trig(trig(he)) * 2; + real_t A2 = real_trig(trig(he)) * 2; vertex pij = n * (xj - xi); vertex pjk = n * (xk - xj); @@ -307,14 +344,14 @@ vertex che::gradient_he(const index_t & he, const distance_t *const & f) const return g / *g; } -vertex che::gradient(const index_t & v, const distance_t *const & f) +vertex che::gradient(const index_t & v, const real_t *const & f) { vertex g; - area_t area, area_star = 0; + real_t area, area_star = 0; for_star(he, this, v) { - area = area_trig(trig(he)); + area = real_trig(trig(he)); area_star += area; g += area * gradient_he(he, f); } @@ -1181,7 +1218,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con corr_t che::find_corr(const vertex & v, const vertex & n, const vector & he_trigs) { - distance_t d, dist = INFINITY; + real_t d, dist = INFINITY; corr_t corr, corr_d; a_mat A(4, 4, arma::fill::ones); @@ -1430,6 +1467,7 @@ void che::delete_me() delete [] EHT; EHT = nullptr; delete [] BT; BT = nullptr; delete [] VN; VN = nullptr; + delete [] VC; VC = nullptr; } void che::read_file(const string & ) diff --git a/src/che_fill_hole.cpp b/src/che_fill_hole.cpp index 3d00bdc2..423e4504 100644 --- a/src/che_fill_hole.cpp +++ b/src/che_fill_hole.cpp @@ -74,7 +74,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const index_t * vmap_border = new index_t[size]; index_t c = 1; - distance_t mean_edge = mesh->mean_edge(); + real_t mean_edge = mesh->mean_edge(); auto gen_vertices = [&mean_edge](vector & merge_vertices, vector & vertices, const vertex & va, const vertex & vb, const index_t & delta_v = 0) { @@ -237,7 +237,7 @@ void split_border(vector > & split_indices, che * mesh, c a = NIL; b = NIL; // review this for(index_t i = 0; i < n; i++) { - distance_t d, d_min = INFINITY; + real_t d, d_min = INFINITY; for(index_t c = 0; c < k; c++) { d = norm(data.col(i) - means.col(c)); @@ -318,7 +318,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, size_t p_iter, bool & is_grow) { gproshan_debug(filling holes); - distance_t perimeter = 0.0, init_perimeter = 0.0; + real_t perimeter = 0.0, init_perimeter = 0.0; real_t length = mesh->mean_edge(); priority_queue front; @@ -377,8 +377,8 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, border_t top; - angle_t a75 = 75.0 * M_PI / 180; - angle_t a135 = 135.0 * M_PI / 180; + real_t a75 = 75.0 * M_PI / 180; + real_t a135 = 135.0 * M_PI / 180; a_vec m_vec; a_vec m_normal; @@ -558,8 +558,8 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, che * fill_hole_front_angles(vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) { size_t p_iter = max_iter; - distance_t perimeter = 0.0; - distance_t init_perimeter = 0.0; + real_t perimeter = 0.0; + real_t init_perimeter = 0.0; priority_queue front; vector faces; @@ -644,8 +644,8 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c border_t top; - angle_t a75 = 75.0 * M_PI / 180; - angle_t a135 = 135.0 * M_PI / 180; + real_t a75 = 75.0 * M_PI / 180; + real_t a135 = 135.0 * M_PI / 180; a_vec m_vec; while(!front.empty() && p_iter-- && p_iter < 2000) @@ -851,11 +851,11 @@ void get_real_tri(che * mesh, vector & select_vertices, vector } } - distance_t weight = 1.8; + real_t weight = 1.8; - distance_t wp = weight / select_vertices.size(); - distance_t aux = wp * tri_sizes[0]; - distance_t wo = (1 - aux) / ( tri_sizes[1] + tri_sizes[2] ); + real_t wp = weight / select_vertices.size(); + real_t aux = wp * tri_sizes[0]; + real_t wo = (1 - aux) / ( tri_sizes[1] + tri_sizes[2] ); triangle.push_back( (wp * tri[0]) + wo * (tri[1] + tri[2]) ); diff --git a/src/cuda/geodesics_ptp.cu b/src/cuda/geodesics_ptp.cu index 87e397b2..7e87578a 100644 --- a/src/cuda/geodesics_ptp.cu +++ b/src/cuda/geodesics_ptp.cu @@ -33,17 +33,17 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = ptp_out.dist; + real_t * h_dist = ptp_out.dist; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); index_t d; if(ptp_out.clusters) @@ -64,7 +64,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, toplesets.limits, toplesets.index, d_sorted, d_error); } - cudaMemcpy(h_dist, d_dist[d], sizeof(distance_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); cudaFree(d_error); cudaFree(d_dist[0]); @@ -84,7 +84,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, return time / 1000; } -distance_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, double & time_fps, size_t n, distance_t radio) +real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, double & time_fps, size_t n, real_t radio) { cudaDeviceReset(); @@ -100,14 +100,14 @@ distance_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = new distance_t[h_mesh->n_vertices]; + real_t * h_dist = new real_t[h_mesh->n_vertices]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); @@ -126,7 +126,7 @@ distance_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples index_t d; int f; - distance_t max_dist = INFINITY; + real_t max_dist = INFINITY; while(n-- && max_dist > radio) { limits.clear(); @@ -142,7 +142,7 @@ distance_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples #endif if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(distance_t), cudaMemcpyDeviceToHost); + cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); samples.push_back(f - 1); } @@ -171,7 +171,7 @@ distance_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples return max_dist; } -index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, distance_t * d_error, index_t * h_clusters, index_t ** d_clusters) +index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) @@ -180,8 +180,8 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dis for(index_t i = 0; i < sources.size(); i++) h_dist[sources[i]] = 0; - cudaMemcpy(d_dist[0], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); if(h_clusters) @@ -232,7 +232,7 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dis } __global__ -void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * sorted, index_t end, index_t start) +void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * sorted, index_t end, index_t start) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -243,7 +243,7 @@ void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t { new_dist[v] = old_dist[v]; - distance_t d; + real_t d; cu_for_star(he, mesh, v) { d = cu_update_step(mesh, old_dist, he); @@ -255,7 +255,7 @@ void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t __global__ -void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start) +void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -267,7 +267,7 @@ void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t new_dist[v] = old_dist[v]; new_clusters[v] = old_clusters[v]; - distance_t d; + real_t d; cu_for_star(he, mesh, v) { d = cu_update_step(mesh, old_dist, he); @@ -282,7 +282,7 @@ void relax_ptp(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t } __global__ -void relative_error(distance_t * error, const distance_t * new_dist, const distance_t * old_dist, const index_t start, const index_t end, const index_t * sorted) +void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted) { index_t i = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -299,7 +299,7 @@ void relative_error(distance_t * error, const distance_t * new_dist, const dista } __forceinline__ __device__ -distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & he) +real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) { index_t x[3]; x[0] = mesh->VT[cu_next(he)]; @@ -310,37 +310,37 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; - distance_t t[2]; + real_t t[2]; t[0] = dist[x[0]]; t[1] = dist[x[1]]; - distance_t q[2][2]; + real_t q[2][2]; q[0][0] = (X[0], X[0]); q[0][1] = (X[0], X[1]); q[1][0] = (X[1], X[0]); q[1][1] = (X[1], X[1]); - distance_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - distance_t Q[2][2]; + real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; + real_t Q[2][2]; Q[0][0] = q[1][1] / det; Q[0][1] = -q[0][1] / det; Q[1][0] = -q[1][0] / det; Q[1][1] = q[0][0] / det; - distance_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - distance_t dis = delta * delta - + real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); + real_t dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); #ifdef SINGLE_P - distance_t p = delta + sqrtf(dis); + real_t p = delta + sqrtf(dis); #else - distance_t p = delta + sqrt(dis); + real_t p = delta + sqrt(dis); #endif p /= Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]; - distance_t tp[2]; + real_t tp[2]; tp[0] = t[0] - p; tp[1] = t[1] - p; @@ -348,17 +348,17 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); - distance_t cond[2]; + real_t cond[2]; cond[0] = (X[0] , n); cond[1] = (X[1] , n); - distance_t c[2]; + real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { - distance_t dp[2]; + real_t dp[2]; dp[0] = dist[x[0]] + *X[0]; dp[1] = dist[x[1]] + *X[1]; @@ -369,7 +369,7 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h } __host__ __device__ -bool is_ok::operator()(const distance_t & val) const +bool is_ok::operator()(const real_t & val) const { return val < PTP_TOL; } diff --git a/src/cuda/geodesics_ptp_coalescence.cu b/src/cuda/geodesics_ptp_coalescence.cu index f151e577..6e802375 100644 --- a/src/cuda/geodesics_ptp_coalescence.cu +++ b/src/cuda/geodesics_ptp_coalescence.cu @@ -39,14 +39,14 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = new distance_t[h_mesh->n_vertices]; + real_t * h_dist = new real_t[h_mesh->n_vertices]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); index_t d; if(ptp_out.clusters) @@ -71,7 +71,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, } else d = run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, {toplesets.limits, inv}, d_error); - cudaMemcpy(h_dist, d_dist[d], sizeof(distance_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); cudaFree(d_error); cudaFree(d_dist[0]); @@ -99,7 +99,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, return time / 1000; } -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const vector & sources, const toplesets_t & inv, distance_t * d_error, index_t * h_clusters, index_t ** d_clusters) +index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) @@ -108,8 +108,8 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distan for(index_t i = 0; i < sources.size(); i++) h_dist[inv.index[sources[i]]] = 0; - cudaMemcpy(d_dist[0], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); if(h_clusters) { @@ -160,7 +160,7 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distan } __global__ -void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t end, index_t start) +void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t end, index_t start) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -170,7 +170,7 @@ void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_d { new_dist[v] = old_dist[v]; - distance_t d; + real_t d; cu_for_star(he, mesh, v) { d = cu_update_step(mesh, old_dist, he); @@ -182,7 +182,7 @@ void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_d __global__ -void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start) +void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -193,7 +193,7 @@ void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_d new_dist[v] = old_dist[v]; new_clusters[v] = old_clusters[v]; - distance_t d; + real_t d; cu_for_star(he, mesh, v) { d = cu_update_step(mesh, old_dist, he); @@ -208,7 +208,7 @@ void relax_ptp_coalescence(CHE * mesh, distance_t * new_dist, distance_t * old_d } __forceinline__ __device__ -distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & he) +real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) { index_t x[3]; x[0] = mesh->VT[cu_next(he)]; @@ -219,27 +219,27 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; - distance_t t[2]; + real_t t[2]; t[0] = dist[x[0]]; t[1] = dist[x[1]]; - distance_t q[2][2]; + real_t q[2][2]; q[0][0] = (X[0], X[0]); q[0][1] = (X[0], X[1]); q[1][0] = (X[1], X[0]); q[1][1] = (X[1], X[1]); - distance_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - distance_t Q[2][2]; + real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; + real_t Q[2][2]; Q[0][0] = q[1][1] / det; Q[0][1] = -q[0][1] / det; Q[1][0] = -q[1][0] / det; Q[1][1] = q[0][0] / det; - distance_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - distance_t dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0]*t[0]*Q[0][0] + t[0]*t[1]*(Q[1][0] + Q[0][1]) + t[1]*t[1]*Q[1][1] - 1); + real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); + real_t dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0]*t[0]*Q[0][0] + t[0]*t[1]*(Q[1][0] + Q[0][1]) + t[1]*t[1]*Q[1][1] - 1); - distance_t p; + real_t p; if(dis >= 0) { @@ -251,7 +251,7 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h p /= Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]; } - distance_t tp[2]; + real_t tp[2]; tp[0] = t[0] - p; tp[1] = t[1] - p; @@ -259,17 +259,17 @@ distance_t cu_update_step(CHE * mesh, const distance_t * dist, const index_t & h tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); - distance_t cond[2]; + real_t cond[2]; cond[0] = (X[0] , n); cond[1] = (X[1] , n); - distance_t c[2]; + real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { - distance_t dp[2]; + real_t dp[2]; dp[0] = dist[x[0]] + *X[0]; dp[1] = dist[x[1]] + *X[1]; diff --git a/src/cuda/test_geodesics_ptp.cu b/src/cuda/test_geodesics_ptp.cu index 7527b682..dcba0245 100644 --- a/src/cuda/test_geodesics_ptp.cu +++ b/src/cuda/test_geodesics_ptp.cu @@ -18,7 +18,7 @@ using namespace std; namespace gproshan { -vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const distance_t * exact_dist, double & time_ptp) +vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) { cudaDeviceReset(); @@ -34,19 +34,19 @@ vector > iter_error_parallel_toplesets_propagation_gpu CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = new distance_t[h_mesh->n_vertices]; + real_t * h_dist = new real_t[h_mesh->n_vertices]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); + vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); delete [] h_dist; cudaFree(d_error); @@ -69,7 +69,7 @@ vector > iter_error_parallel_toplesets_propagation_gpu } /// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, size_t n, distance_t radio) +double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, size_t n, real_t radio) { cudaDeviceReset(); @@ -83,14 +83,14 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = new distance_t[h_mesh->n_vertices]; + real_t * h_dist = new real_t[h_mesh->n_vertices]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); @@ -112,7 +112,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam float time_fps; index_t d; int f; - distance_t max_dist = INFINITY; + real_t max_dist = INFINITY; while(n-- && max_dist > radio) { cudaEventRecord(start, 0); @@ -136,7 +136,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam times[samples.size()] = time_fps / 1000; if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(distance_t), cudaMemcpyDeviceToHost); + cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); samples.push_back(f - 1); } @@ -161,7 +161,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam return times; } -vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, const distance_t * exact_dist, distance_t * d_error) +vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) @@ -170,11 +170,11 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const in for(index_t i = 0; i < sources.size(); i++) h_dist[sources[i]] = 0; - cudaMemcpy(d_dist[0], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - vector > iter_error; + vector > iter_error; iter_error.reserve(limits.size()); index_t d = 0; @@ -192,7 +192,7 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const in relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_sorted, end, start); // begin calculating iteration error - cudaMemcpy(h_dist, d_dist[!d], sizeof(distance_t) * n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(j == limits.size() - 1) iter_error.push_back(make_pair(n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size()))); // end diff --git a/src/cuda/test_geodesics_ptp_coalescence.cu b/src/cuda/test_geodesics_ptp_coalescence.cu index e8c2a0f1..43bec37e 100644 --- a/src/cuda/test_geodesics_ptp_coalescence.cu +++ b/src/cuda/test_geodesics_ptp_coalescence.cu @@ -20,14 +20,14 @@ using namespace std; namespace gproshan { -vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const distance_t * exact_dist, double & time_ptp) +vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) { // sort data by levels, must be improve the coalescence vertex * V = new vertex[mesh->n_vertices()]; index_t * F = new index_t[mesh->n_faces() * che::P]; index_t * inv = new index_t[mesh->n_vertices()]; - distance_t * exact_dist_sorted = new distance_t[mesh->n_vertices()]; + real_t * exact_dist_sorted = new real_t[mesh->n_vertices()]; #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) @@ -62,16 +62,16 @@ vector > iter_error_parallel_toplesets_propagation_coa CHE * dd_mesh, * d_mesh; cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - distance_t * h_dist = new distance_t[h_mesh->n_vertices]; + real_t * h_dist = new real_t[h_mesh->n_vertices]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * h_mesh->n_vertices); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); + vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); delete [] h_dist; cudaFree(d_error); @@ -96,7 +96,7 @@ vector > iter_error_parallel_toplesets_propagation_coa } /// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector & samples, size_t n, distance_t radio) +double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector & samples, size_t n, real_t radio) { cudaDeviceReset(); @@ -111,14 +111,14 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices()]; - distance_t * h_dist = new distance_t[mesh->n_vertices()]; + real_t * h_dist = new real_t[mesh->n_vertices()]; - distance_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(distance_t) * mesh->n_vertices()); - cudaMalloc(&d_dist[1], sizeof(distance_t) * mesh->n_vertices()); + real_t * d_dist[2]; + cudaMalloc(&d_dist[0], sizeof(real_t) * mesh->n_vertices()); + cudaMalloc(&d_dist[1], sizeof(real_t) * mesh->n_vertices()); - distance_t * d_error; - cudaMalloc(&d_error, sizeof(distance_t) * mesh->n_vertices()); + real_t * d_error; + cudaMalloc(&d_error, sizeof(real_t) * mesh->n_vertices()); vector limits; index_t * toplesets = new index_t[mesh->n_vertices()]; @@ -137,7 +137,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector radio) { cudaEventRecord(start, 0); @@ -185,7 +185,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(distance_t), cudaMemcpyDeviceToHost); + cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); samples.push_back(sorted_index[f - 1]); } @@ -211,7 +211,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, distance_t * h_dist, distance_t ** d_dist, const vector & sources, const vector & limits, const index_t * inv, const distance_t * exact_dist, distance_t * d_error) +vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) @@ -220,10 +220,10 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_me for(index_t i = 0; i < sources.size(); i++) h_dist[inv[sources[i]]] = 0; - cudaMemcpy(d_dist[0], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(distance_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - vector > iter_error; + vector > iter_error; iter_error.reserve(limits.size()); ofstream os("band"); @@ -245,7 +245,7 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_me os << n_iter << " " << i << " " << j << " " << end - start << endl; // begin calculating iteration error - cudaMemcpy(h_dist, d_dist[!d], sizeof(distance_t) * n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(j == limits.size() - 1) iter_error.push_back(make_pair(n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size()))); // end diff --git a/src/dijkstra.cpp b/src/dijkstra.cpp index 68e583c5..1ac116f4 100644 --- a/src/dijkstra.cpp +++ b/src/dijkstra.cpp @@ -15,10 +15,10 @@ dijkstra::dijkstra(che * shape, index_t src) n_vertices = shape->n_vertices(); source = src; - weights = new distance_t[n_vertices]; + weights = new real_t[n_vertices]; predecessors = new index_t[n_vertices]; - memset(predecessors, 255, sizeof(distance_t)*n_vertices); + memset(predecessors, 255, sizeof(real_t)*n_vertices); for(index_t i = 0; i < n_vertices; i++) weights[i] = INFINITY; @@ -32,7 +32,7 @@ dijkstra::~dijkstra() if(predecessors) delete predecessors; } -distance_t & dijkstra::operator()(index_t i) +real_t & dijkstra::operator()(index_t i) { return weights[i]; } @@ -56,7 +56,7 @@ void dijkstra::run(che * shape) visited[source] = true; weights[source] = 0; - distance_t min; + real_t min; index_t min_i; for(index_t i = 0; i < n_vertices; i++) @@ -67,7 +67,7 @@ void dijkstra::run(che * shape) #pragma omp parallel for num_threads(8) for(index_t v = 0; v < n_vertices; v++) { - distance_t w; + real_t w; index_t nv; if(!visited[v]) diff --git a/src/geodesics.cpp b/src/geodesics.cpp index e3a8ae0c..614765d6 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -13,13 +13,13 @@ using namespace std; namespace gproshan { -geodesics::geodesics(che * mesh, const vector & sources, const option_t & opt, distance_t *const & e_dist, const bool & cluster, const size_t & n_iter, const distance_t & radio): n_vertices(mesh->n_vertices()) +geodesics::geodesics(che * mesh, const vector & sources, const option_t & opt, real_t *const & e_dist, const bool & cluster, const size_t & n_iter, const real_t & radio): n_vertices(mesh->n_vertices()) { assert(n_vertices > 0); free_dist = e_dist == nullptr; - dist = free_dist ? new distance_t[n_vertices] : e_dist; + dist = free_dist ? new real_t[n_vertices] : e_dist; clusters = cluster ? new index_t[n_vertices] : nullptr; sorted_index = new index_t[n_vertices]; @@ -41,7 +41,7 @@ geodesics::~geodesics() delete [] clusters; } -const distance_t & geodesics::operator[](const index_t & i) const +const real_t & geodesics::operator[](const index_t & i) const { assert(i < n_vertices); return dist[i]; @@ -53,7 +53,7 @@ const index_t & geodesics::operator()(const index_t & i) const return sorted_index[i]; } -const distance_t & geodesics::radio() const +const real_t & geodesics::radio() const { assert(n_sorted != 0); return dist[farthest()]; @@ -84,14 +84,14 @@ void geodesics::normalize() return; } - distance_t max = dist[farthest()]; + real_t max = dist[farthest()]; #pragma omp parallel for for(size_t i = 0; i < n_sorted; i++) dist[sorted_index[i]] /= max; } -void geodesics::execute(che * mesh, const vector & sources, const size_t & n_iter, const distance_t & radio, const option_t & opt) +void geodesics::execute(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const option_t & opt) { switch(opt) { @@ -111,7 +111,7 @@ void geodesics::execute(che * mesh, const vector & sources, const size_ } } -void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const distance_t & radio) +void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -122,11 +122,11 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co size_t green_count = n_iter ? n_iter : n_vertices; - priority_queue, - vector >, - greater > > Q; + priority_queue, + vector >, + greater > > Q; - distance_t dv, dp; + real_t dv, dp; index_t dir; // dir propagation vertex vx; @@ -191,7 +191,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co delete [] color; } -void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector & sources, const size_t & n_iter, const distance_t & radio) +void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) { index_t * toplesets = new index_t[n_vertices]; vector limits; @@ -224,7 +224,7 @@ void geodesics::run_heat_flow(che * mesh, const vector & sources) #ifdef GPROSHAN_CUDA -void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const size_t & n_iter, const distance_t & radio) +void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) { index_t * toplesets = new index_t[n_vertices]; vector limits; @@ -258,7 +258,7 @@ void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) //d = {NIL, 0, 1} cross edge, next, prev -distance_t geodesics::update(index_t & d, che * mesh, const index_t & he, vertex & vx) +real_t geodesics::update(index_t & d, che * mesh, const index_t & he, vertex & vx) { d = NIL; @@ -286,7 +286,7 @@ distance_t geodesics::update(index_t & d, che * mesh, const index_t & he, vertex return planar_update(d, X, x, vx); } -distance_t geodesics::planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx) +real_t geodesics::planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx) { a_mat ones(2,1); ones.ones(2,1); @@ -300,9 +300,9 @@ distance_t geodesics::planar_update(index_t & d, a_mat & X, index_t * x, vertex t(0) = dist[x[0]]; t(1) = dist[x[1]]; - distance_t p; + real_t p; a_mat delta = ones.t() * Q * t; - distance_t dis = as_scalar(delta * delta - (ones.t() * Q * ones) * (as_scalar(t.t() * Q * t) - 1)); + real_t dis = as_scalar(delta * delta - (ones.t() * Q * ones) * (as_scalar(t.t() * Q * t) - 1)); if(dis >= 0) { @@ -318,7 +318,7 @@ distance_t geodesics::planar_update(index_t & d, a_mat & X, index_t * x, vertex if(t(0) == INFINITY || t(1) == INFINITY || dis < 0 || (cond(0) >= 0 || cond(1) >= 0)) { - distance_t dp[2]; + real_t dp[2]; dp[0] = dist[x[0]] + norm(X.col(0)); dp[1] = dist[x[1]] + norm(X.col(1)); diff --git a/src/geodesics_ptp.cpp b/src/geodesics_ptp.cpp index 7c4ebb31..cb0fdfab 100644 --- a/src/geodesics_ptp.cpp +++ b/src/geodesics_ptp.cpp @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -ptp_out_t::ptp_out_t(distance_t *const & d, index_t *const & c): dist(d), clusters(c) {} +ptp_out_t::ptp_out_t(real_t *const & d, index_t *const & c): dist(d), clusters(c) {} che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets) { @@ -46,8 +46,8 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c mesh = ptp_coalescence(inv, mesh, toplesets); // ------------------------------------------------------ - distance_t * pdist[2] = {new distance_t[mesh->n_vertices()], new distance_t[mesh->n_vertices()]}; - distance_t * error = new distance_t[mesh->n_vertices()]; + real_t * pdist[2] = {new real_t[mesh->n_vertices()], new real_t[mesh->n_vertices()]}; + real_t * error = new real_t[mesh->n_vertices()]; #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) @@ -80,7 +80,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c { pdist[!d][v] = pdist[d][v]; - distance_t p; + real_t p; for_star(he, mesh, v) { p = update_step(mesh, pdist[d], he); @@ -122,8 +122,8 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) { - distance_t * pdist[2] = {ptp_out.dist, new distance_t[mesh->n_vertices()]}; - distance_t * error = new distance_t[mesh->n_vertices()]; + real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices()]}; + real_t * error = new real_t[mesh->n_vertices()]; #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) @@ -157,7 +157,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c const index_t & v = toplesets.index[vi]; pdist[!d][v] = pdist[d][v]; - distance_t p; + real_t p; for_star(he, mesh, v) { p = update_step(mesh, pdist[d], he); @@ -193,13 +193,13 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c if(ptp_out.dist != pdist[!d]) { - memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices() * sizeof(distance_t)); + memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices() * sizeof(real_t)); delete [] pdist[!d]; } else delete [] pdist[d]; } -distance_t update_step(che * mesh, const distance_t * dist, const index_t & he) +real_t update_step(che * mesh, const real_t * dist, const index_t & he) { index_t x[3]; x[0] = mesh->vt(next(he)); @@ -210,31 +210,31 @@ distance_t update_step(che * mesh, const distance_t * dist, const index_t & he) X[0] = mesh->gt(x[0]) - mesh->gt(x[2]); X[1] = mesh->gt(x[1]) - mesh->gt(x[2]); - distance_t t[2]; + real_t t[2]; t[0] = dist[x[0]]; t[1] = dist[x[1]]; - distance_t q[2][2]; + real_t q[2][2]; q[0][0] = (X[0], X[0]); q[0][1] = (X[0], X[1]); q[1][0] = (X[1], X[0]); q[1][1] = (X[1], X[1]); - distance_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - distance_t Q[2][2]; + real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; + real_t Q[2][2]; Q[0][0] = q[1][1] / det; Q[0][1] = -q[0][1] / det; Q[1][0] = -q[1][0] / det; Q[1][1] = q[0][0] / det; - distance_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - distance_t dis = delta * delta - + real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); + real_t dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); - distance_t p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); + real_t p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); - distance_t tp[2]; + real_t tp[2]; tp[0] = t[0] - p; tp[1] = t[1] - p; @@ -242,17 +242,17 @@ distance_t update_step(che * mesh, const distance_t * dist, const index_t & he) tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); - distance_t cond[2]; + real_t cond[2]; cond[0] = (X[0] , n); cond[1] = (X[1] , n); - distance_t c[2]; + real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { - distance_t dp[2]; + real_t dp[2]; dp[0] = dist[x[0]] + *X[0]; dp[1] = dist[x[1]] + *X[1]; @@ -262,9 +262,9 @@ distance_t update_step(che * mesh, const distance_t * dist, const index_t & he) return p; } -void normalize_ptp(distance_t * dist, const size_t & n) +void normalize_ptp(real_t * dist, const size_t & n) { - distance_t max_d = 0; + real_t max_d = 0; #pragma omp parallel for reduction(max: max_d) for(index_t v = 0; v < n; v++) diff --git a/src/heat_flow.cpp b/src/heat_flow.cpp index b543503d..ec1b6298 100644 --- a/src/heat_flow.cpp +++ b/src/heat_flow.cpp @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -distance_t * heat_flow(che * mesh, const vector & sources, double & solve_time) +real_t * heat_flow(che * mesh, const vector & sources, double & solve_time) { if(!sources.size()) return 0; @@ -42,7 +42,7 @@ distance_t * heat_flow(che * mesh, const vector & sources, double & sol //assert(spsolve(u, A, u0)); // arma // extract geodesics - distance_t * dist = new distance_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices()]; a_mat div(mesh->n_vertices(), 1); compute_divergence(mesh, u, div); @@ -63,7 +63,7 @@ distance_t * heat_flow(che * mesh, const vector & sources, double & sol #ifdef GPROSHAN_CUDA -distance_t * heat_flow_gpu(che * mesh, const vector & sources, double & solve_time) +real_t * heat_flow_gpu(che * mesh, const vector & sources, double & solve_time) { if(!sources.size()) return 0; @@ -90,7 +90,7 @@ distance_t * heat_flow_gpu(che * mesh, const vector & sources, double & solve_time += solve_positive_definite_gpu(u, A, u0); // cusorlver (cusparse) // extract geodesics - distance_t * dist = new distance_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices()]; a_mat div(mesh->n_vertices(), 1); compute_divergence(mesh, u, div); diff --git a/src/key_points.cpp b/src/key_points.cpp index 66d163ba..bb5b88a8 100644 --- a/src/key_points.cpp +++ b/src/key_points.cpp @@ -56,7 +56,7 @@ void key_points::compute_kps(che * mesh) #pragma omp parallel for for(index_t t = 0; t < n_faces; t++) { - face_areas[t].first = mesh->area_trig(t); + face_areas[t].first = mesh->real_trig(t); face_areas[t].second = t; } diff --git a/src/mdict/basis_cosine.cpp b/src/mdict/basis_cosine.cpp index 2ce8c965..082c0e7b 100644 --- a/src/mdict/basis_cosine.cpp +++ b/src/mdict/basis_cosine.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_cosine::basis_cosine(const size_t & _r, const size_t & _n, const distance_t & _radio) +basis_cosine::basis_cosine(const size_t & _r, const size_t & _n, const real_t & _radio) { radio = _radio; r = _r; diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index a9735e1f..dc41445d 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_dct::basis_dct(const size_t & _n, const distance_t & _radio) +basis_dct::basis_dct(const size_t & _n, const real_t & _radio) { radio = _radio; n = _n; diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 1f61197e..846d497d 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -39,7 +39,7 @@ a_vec gaussian(a_mat & xy, real_t sigma, real_t cx, real_t cy) return exp( - ( x + y ) / ( 2 * sigma * sigma ) ); } -a_vec cossine(a_mat & xy, distance_t radio, size_t K) +a_vec cossine(a_mat & xy, real_t radio, size_t K) { a_vec x = xy.row(0).t() + 0.5; a_vec y = xy.row(1).t() + 0.5; @@ -219,7 +219,7 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } } - distance_t h = 2; + real_t h = 2; a_vec V(3); @@ -258,7 +258,7 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorn_vertices(); v++) @@ -276,7 +276,7 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorn_vertices(); v++) error += *(new_vertices[v] - mesh->get_vertex(v)); @@ -289,15 +289,15 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vectorset_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); } -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const distance_t & h) +a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) { a_vec n_a_vec(3, arma::fill::zeros); - area_t sum = 0; + real_t sum = 0; - distance_t * w = new distance_t[patches_map[v].size()]; + real_t * w = new real_t[patches_map[v].size()]; index_t i = 0; - distance_t d = 0; + real_t d = 0; for(auto p: patches_map[v]) { @@ -343,7 +343,7 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector } } - distance_t h = 0.2; + real_t h = 0.2; #pragma omp parallel for for(index_t v = v_i; v < mesh->n_vertices(); v++) @@ -361,7 +361,7 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector vertex * new_vertices = (vertex *) V.memptr(); - distance_t error = 0; + real_t error = 0; #pragma omp parallel for reduction(+: error) for(index_t v = v_i; v < mesh->n_vertices(); v++) error += *(new_vertices[v] - mesh->get_vertex(v)); @@ -375,15 +375,15 @@ void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector } [[deprecated]] -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const distance_t & h) +a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) { a_vec n_a_vec(3, arma::fill::zeros); - area_t sum = 0; + real_t sum = 0; - distance_t * w = new distance_t[patches_map[v].size()]; + real_t * w = new real_t[patches_map[v].size()]; index_t i = 0; - distance_t d = 0; + real_t d = 0; for(auto p: patches_map[v]) { @@ -410,7 +410,7 @@ a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & return n_a_vec; } -a_vec simple_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const distance_t & h) +a_vec simple_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) { a_vec n_a_vec(3, arma::fill::zeros); diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index f0f41b22..5e7cf09f 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -6,7 +6,7 @@ namespace gproshan::mdict { -denoising::denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) +denoising::denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) { } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index b5b59517..e52ecb1c 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -16,7 +16,7 @@ namespace gproshan::mdict { size_t dictionary::L = 10; size_t dictionary::T = 5; -dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _d_plot): +dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _d_plot): mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), d_plot(_d_plot) { A.eye(phi_basis->dim, m); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 7b23fbde..297c45a5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -6,7 +6,7 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) { } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d8238dbf..8481c423 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -29,7 +29,7 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * dictionary::T * (dictionary::T + 1); -void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const distance_t & radio, index_t * _toplevel) +void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const real_t & radio, index_t * _toplevel) { index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; @@ -109,7 +109,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl } } -void patch::gather_vertices(che * mesh, const index_t & v, const distance_t & radio, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, index_t * toplevel) { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); if(vertices.size()) vertices.clear(); diff --git a/src/mdict/super_resolution.cpp b/src/mdict/super_resolution.cpp index 08a5097e..cb263522 100644 --- a/src/mdict/super_resolution.cpp +++ b/src/mdict/super_resolution.cpp @@ -6,7 +6,7 @@ namespace gproshan::mdict { -super_resolution::super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const distance_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) +super_resolution::super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _plot) { } diff --git a/src/sampling.cpp b/src/sampling.cpp index e6b07702..715fd0c8 100644 --- a/src/sampling.cpp +++ b/src/sampling.cpp @@ -12,7 +12,7 @@ using namespace std; namespace gproshan { -index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, distance_t radio) +index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, real_t radio) { normals = new vertex[n_points]; sizes = new size_t[n_points]; @@ -37,7 +37,7 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n return indexes; } -bool load_sampling(vector & points, distance_t & radio, che * mesh, size_t n) +bool load_sampling(vector & points, real_t & radio, che * mesh, size_t n) { const string & filename = mesh->filename(); diff --git a/src/test_geodesics_ptp.cpp b/src/test_geodesics_ptp.cpp index 5f1f2b3f..c2f2807f 100644 --- a/src/test_geodesics_ptp.cpp +++ b/src/test_geodesics_ptp.cpp @@ -58,9 +58,9 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // PERFORMANCE & ACCURACY ___________________________________________________________________ double Time[7]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse - distance_t Error[5]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse + real_t Error[5]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse - distance_t * exact = load_exact_geodesics(exact_dist_path + filename + ".exact", n_vertices); + real_t * exact = load_exact_geodesics(exact_dist_path + filename + ".exact", n_vertices); if(!exact) fprintf(stderr, "no exact geodesics for: %s.\n", filename.c_str()); Time[0] = test_fast_marching(Error[0], exact, mesh, source, n_test); @@ -98,7 +98,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) if(Time[t_min] > Time[i]) t_min = i; index_t e_min = 0; - for(index_t i = 1; i < sizeof(Error) / sizeof(distance_t); i++) + for(index_t i = 1; i < sizeof(Error) / sizeof(real_t); i++) if(Error[e_min] > Error[i]) e_min = i; fprintf(ftable, "%20s ", ("\\verb|" + filename + '|').c_str()); @@ -195,7 +195,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #ifdef GPROSHAN_CUDA // IMPLEMENT: iter_error_parallel_toplesets_propagation_coalescence_cpu double time; - vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); + vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); system(("mv band " + (test_path + filename + ".band")).c_str()); @@ -239,7 +239,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fclose(ftable); } -double test_fast_marching(distance_t & error, const distance_t * exact, che * mesh, const vector & source, const int & n_test) +double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const vector & source, const int & n_test) { double t, seconds = INFINITY; @@ -256,11 +256,11 @@ double test_fast_marching(distance_t & error, const distance_t * exact, che * me return seconds; } -double test_ptp_cpu(distance_t & error, const distance_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; - distance_t * dist = new distance_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices()]; for(int i = 0; i < n_test; i++) { TIC(t) parallel_toplesets_propagation_cpu(dist, mesh, source, toplesets); TOC(t) @@ -274,12 +274,12 @@ double test_ptp_cpu(distance_t & error, const distance_t * exact, che * mesh, co return seconds; } -double test_heat_method_cholmod(distance_t & error, double & stime, const distance_t * exact, che * mesh, const vector & source, const int & n_test) +double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const vector & source, const int & n_test) { double t, st, ptime; ptime = stime = INFINITY; - distance_t * dist = nullptr; + real_t * dist = nullptr; for(int i = 0; i < n_test; i++) { if(dist) delete [] dist; @@ -299,11 +299,11 @@ double test_heat_method_cholmod(distance_t & error, double & stime, const distan #ifdef GPROSHAN_CUDA -double test_ptp_gpu(distance_t & error, const distance_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; - distance_t * dist = new distance_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices()]; for(int i = 0; i < n_test; i++) { t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); @@ -317,12 +317,12 @@ double test_ptp_gpu(distance_t & error, const distance_t * exact, che * mesh, co return seconds; } -double test_heat_method_gpu(distance_t & error, double & stime, const distance_t * exact, che * mesh, const vector & source, const int & n_test) +double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const vector & source, const int & n_test) { double t, st, ptime; ptime = stime = INFINITY; - distance_t * dist = nullptr; + real_t * dist = nullptr; for(int i = 0; i < n_test; i++) { if(dist) delete [] dist; @@ -343,13 +343,13 @@ double test_heat_method_gpu(distance_t & error, double & stime, const distance_t #endif // GPROSHAN_CUDA -distance_t * load_exact_geodesics(const string & file, const size_t & n) +real_t * load_exact_geodesics(const string & file, const size_t & n) { ifstream is(file); if(!is.good()) return nullptr; - distance_t * exact = new distance_t[n]; + real_t * exact = new real_t[n]; for(index_t i = 0; i < n; i++) is >> exact[i]; @@ -358,9 +358,9 @@ distance_t * load_exact_geodesics(const string & file, const size_t & n) return exact; } -distance_t compute_error(const distance_t * dist, const distance_t * exact, const size_t & n, const size_t & s) +real_t compute_error(const real_t * dist, const real_t * exact, const size_t & n, const size_t & s) { - distance_t error = 0; + real_t error = 0; #pragma omp parallel for reduction(+: error) for(index_t v = 0; v < n; v++) diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 7693a447..e58b1134 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -15,8 +15,6 @@ che_viewer::~che_viewer() { glDeleteBuffers(5, vbo); glDeleteVertexArrays(1, &vao); - - if(colors) delete [] colors; } che *& che_viewer::operator -> () @@ -53,8 +51,7 @@ void che_viewer::update() factor = mesh->mean_edge(); mesh->update_normals(); - - update_colors(); + mesh->update_colors(); update_vbo(); } @@ -79,7 +76,7 @@ void che_viewer::update_vbo() // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), colors, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->color(0), GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -95,32 +92,6 @@ void che_viewer::update_vbo() glBindVertexArray(0); } -void che_viewer::update_colors(const color_t *const c) -{ - delete [] colors; - colors = new color_t[mesh->n_vertices()]; - - if(!c) - { - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - colors[v] = COLOR; - - return; - } - - distance_t max_c = 0; - - #pragma omp parallel for reduction(max: max_c) - for(index_t v = 0; v < mesh->n_vertices(); v++) - if(c[v] < INFINITY) - max_c = max(c[v], max_c); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - colors[v] = c[v] / max_c; -} - void che_viewer::update_instances_translations(const vector & translations) { n_instances = translations.size(); @@ -173,11 +144,6 @@ void che_viewer::draw_point_cloud(shader & program) program.disable(); } -color_t & che_viewer::color(const index_t & v) -{ - return colors[v]; -} - void che_viewer::translate(const vertex & p) { v_translate = p; From 3c52895879f63782c60103059cef009e57fc614d Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 19 Mar 2020 16:29:45 +0100 Subject: [PATCH 0232/1018] recovering point cloud ready --- include/mdict/patch.h | 2 +- src/mdict/inpainting.cpp | 58 +++++++++++++++++++++++++++++++--------- src/mdict/patch.cpp | 32 +++++++++++++++------- 3 files changed, 70 insertions(+), 22 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 09194de8..cc86411c 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -67,7 +67,7 @@ class patch distance_t & geo_radio, double delta, double sum_thres); - void init_random(vertex c, arma::mat T, distance_t radio); + void init_random(vertex c, arma::mat T, distance_t radio, distance_t max_radio); void recover_radial_disjoint(che * mesh, const distance_t & radio_, const index_t & v); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a44c91d5..98618812 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "che_off.h" #define PI 3.14159265 @@ -549,9 +550,15 @@ void inpainting::point_cloud_reconstruction() alpha.load(f_alpha); gproshan_debug_var(S.n_rows); M = S.n_rows; - distance_t radio; + distance_t radio, max_radio = -1; patches.resize(M); vertex c; + + for(index_t i = 0; i < M; i++) + if( S(i,3) > max_radio) max_radio = S(i,3); + + size_t total_points = 0; + for(index_t i = 0; i < M; i++) { c.x = S(i,0); @@ -568,18 +575,45 @@ void inpainting::point_cloud_reconstruction() T(1,2) = S(i,11); T(2,2) = S(i,12); - patches[i].init_random(c, T, radio); + patches[i].init_random(c, T, radio, max_radio); + total_points += patches[i].vertices.size(); + patches[i].phi.set_size(patches[i].vertices.size(), phi_basis->get_dim()); + phi_basis->discrete(patches[i].phi, patches[i].xyz); + + // a_vec x = patches[i].phi * A * alpha.col(i); + // patches[i].xyz.row(2) = x.t(); } -/* - a_vec x = rp.phi * A * alpha.col(p); - rp.xyz.row(2) = x.t(); - rp.iscale_xyz(radio); - rp.itransform(); -*/ - //create patches - // recover points - // save points - // show and put seeds on it + + + +// a_vec x = rp.phi * A * alpha.col(i); +// rp.xyz.row(2) = x.t(); + + + for(index_t i = 0; i < M; i++) + { + + patches[i].iscale_xyz(patches[i].radio); + patches[i].itransform(); + } + + vertex point_cloud[total_points]; + for(index_t i = 0, k=0; i < M; i++) + { + for(index_t j = 0; j < patches[i].vertices.size(); j++) + { + point_cloud[k].x = patches[i].xyz(0,j); + point_cloud[k].y = patches[i].xyz(1,j); + point_cloud[k].z = patches[i].xyz(2,j); + k++; + } + } + + che nmesh(point_cloud, total_points, nullptr, 0); + string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + "_pc"); + + che_off::write_file(&nmesh,f_pc); + gproshan_debug(Done!); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index ad71779a..3970ac0c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -123,7 +123,7 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc N.push_back(min_he); return added; } -void patch::init_random(vertex c, arma::mat T, distance_t radio) +void patch::init_random(vertex c, arma::mat T, distance_t radio, distance_t max_radio) { this->T = T; x.resize(3); @@ -137,15 +137,29 @@ void patch::init_random(vertex c, arma::mat T, distance_t radio) std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() std::uniform_real_distribution<> dis(0, 1); - double a = dis(gen) * 2 * PI; - double r = radio * sqrt(dis(gen)); - // If you need it in Cartesian coordinates - double x = r * cos(a); - double y = r * sin(a); - gproshan_debug_var(radio); - //gproshan_debug_var(x); - //gproshan_debug_var(y); + size_t n_points = (radio/max_radio) * 100; + gproshan_debug_var(n_points); + + vertices.resize(n_points); + xyz.resize(3,n_points); + + for(size_t i=0 ;i Date: Sun, 22 Mar 2020 17:54:21 +0100 Subject: [PATCH 0233/1018] imgui inputs menus selected --- include/viewer/viewer.h | 1 + src/app_viewer.cpp | 20 +++++++++--------- src/raytracing/rt_embree.cpp | 8 ++++---- src/raytracing/rt_optix.cpp | 2 -- src/viewer/viewer.cpp | 40 +++++++++++++++++++++--------------- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 34a9c258..71f84873 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -81,6 +81,7 @@ class viewer std::string name; function_t function; index_t sub_menu; + bool selected = false; process_t() = default; process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 23aabb0d..71749674 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -686,20 +686,20 @@ void app_viewer::process_fairing_spectral(viewer * p_view) void app_viewer::process_fairing_taubin(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - gproshan_input(step); - real_t step; cin >> step; + static real_t step = 0.001; //cin >> step; + ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); - fairing * fair = new fairing_taubin(step); - fair->run(mesh); - - mesh->set_vertices(fair->get_postions()); - delete fair; - - mesh->update_normals(); + if(ImGui::Button("Run")) + { + fairing_taubin fair(step); + fair.run(mesh); + + mesh->set_vertices(fair.get_postions()); + mesh->update_normals(); + } } void app_viewer::process_geodesics_fm(viewer * p_view) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 01d3b54d..54d851e8 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -115,7 +115,7 @@ index_t embree::add_point_cloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001f); + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001); vertex n = mesh->normal(i); // normal[i] = glm::vec3(n.x, n.y, n.z); @@ -131,7 +131,7 @@ index_t embree::add_point_cloud(const che * mesh) glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light, const bool & flat) { - glm::vec3 color(.6f, .8f, 1.f); + glm::vec3 color(0.6, 0.8, 1.0); float dist_light = glm::length(light - r.position()); float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff @@ -147,9 +147,9 @@ glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light, const bool & fl ray_hit ro(r.position() + 1e-5f * wi, wi); if(occluded(ro)) - return .5f * glm::vec4(color * falloff * dot_wi_normal, 1.f); + return .5f * glm::vec4(color * falloff * dot_wi_normal, 1); - return glm::vec4(color * falloff * dot_wi_normal, 1.f); + return glm::vec4(color * falloff * dot_wi_normal, 1); } bool embree::intersect(ray_hit & r) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index bca6a2b2..0a5e386a 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -27,8 +27,6 @@ optix::optix(const std::vector & meshes) optixInit(); // create context - cudaSetDevice(0); - cudaStreamCreate(&stream); cuCtxGetCurrent(&cuda_context); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1fe5ab81..1ce31ed0 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -126,10 +126,12 @@ bool viewer::run() if(ImGui::BeginMenu(sub_menus[i].c_str())) { for(auto & p: processes) - if( p.second.function != nullptr && - p.second.sub_menu == i && - ImGui::MenuItem(p.second.name.c_str(), ('[' + p.second.key + ']').c_str()) ) - p.second.function(this); + { + process_t & pro = p.second; + + if(pro.function != nullptr && pro.sub_menu == i) + ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected); + } ImGui::EndMenu(); } @@ -137,6 +139,19 @@ bool viewer::run() ImGui::EndMainMenuBar(); } + + for(auto & p: processes) + { + process_t & pro = p.second; + + if(pro.selected) + { + ImGui::Begin(pro.name.c_str()); + p.second.function(this); + mesh().update_vbo(); + ImGui::End(); + } + } // Rendering ImGui::Render(); @@ -198,7 +213,6 @@ void viewer::init_gl() glewInit(); - glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -314,8 +328,11 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a } viewer * view = (viewer *) glfwGetWindowUserPointer(window); - view->menu_process(view->processes[key].function); - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + + if(view->processes[key].function) + view->processes[key].selected = !view->processes[key].selected; + + //glClearColor(view->bgc, view->bgc, view->bgc, 1.); } void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods) @@ -364,15 +381,6 @@ void viewer::idle() ////glutPostRedisplay(); } -void viewer::menu_process(function_t pro) -{ - if(!pro) return; - - pro(this); - - mesh().update_vbo(); -} - void viewer::menu_help(viewer * view) { for(auto & p: view->processes) From e386a74d6d73d815161207277929cb2344965b36 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 22 Mar 2020 18:52:32 +0100 Subject: [PATCH 0234/1018] imgui no imgui menu processes --- include/app_viewer.h | 86 +++++++++++----------- include/viewer/viewer.h | 44 +++++------ src/app_viewer.cpp | 159 +++++++++++++++++++++++++++++----------- src/viewer/viewer.cpp | 101 ++++++++++++++++++------- 4 files changed, 255 insertions(+), 135 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 57fda2ae..02fa3b87 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -45,54 +45,54 @@ class app_viewer : public viewer int main(int nargs, const char ** args); private: - static void process_fairing_taubin(viewer * p_view); - static void process_fairing_spectral(viewer * p_view); + static bool process_fairing_taubin(viewer * p_view); + static bool process_fairing_spectral(viewer * p_view); - static void process_fastmarching(viewer * p_view); - static void process_geodesics_fm(viewer * p_view); - static void process_geodesics_ptp_cpu(viewer * p_view); - static void process_geodesics_heat_flow(viewer * p_view); + static bool process_fastmarching(viewer * p_view); + static bool process_geodesics_fm(viewer * p_view); + static bool process_geodesics_ptp_cpu(viewer * p_view); + static bool process_geodesics_heat_flow(viewer * p_view); #ifdef GPROSHAN_CUDA - static void process_geodesics_ptp_gpu(viewer * p_view); - static void process_geodesics_heat_flow_gpu(viewer * p_view); + static bool process_geodesics_ptp_gpu(viewer * p_view); + static bool process_geodesics_heat_flow_gpu(viewer * p_view); #endif // GPROSHAN_CUDA - static void process_farthest_point_sampling(viewer * p_view); - static void process_farthest_point_sampling_radio(viewer * p_view); - static void compute_toplesets(viewer * p_view); - static void process_voronoi(viewer * p_view); - - static void process_mdict_patch(viewer * p_view); - static void process_denoising(viewer * p_view); - static void process_super_resolution(viewer * p_view); - static void process_inpaiting(viewer * p_view); - static void process_iterative_inpaiting(viewer * p_view); - - static void process_functional_maps(viewer * p_view); - static void process_gps(viewer * p_view); - static void process_hks(viewer * p_view); - static void process_wks(viewer * p_view); - static void process_key_points(viewer * p_view); - static void process_key_components(viewer * p_view); - - static void process_poisson(viewer * p_view, const index_t & k); - static void process_poisson_laplacian_1(viewer * p_view); - static void process_poisson_laplacian_2(viewer * p_view); - static void process_poisson_laplacian_3(viewer * p_view); - - static void process_threshold(viewer * p_view); - static void process_noise(viewer * p_view); - static void process_black_noise(viewer * p_view); - static void process_multiplicate_vertices(viewer * p_view); - static void process_fill_holes(viewer * p_view); - static void process_delete_vertices(viewer * p_view); - static void process_fill_holes_test(viewer * p_view); - static void process_delete_non_manifold_vertices(viewer * p_view); - static void process_fill_holes_biharmonic_splines(viewer * p_view); - static void process_gaussian_curvature(viewer * p_view); - static void process_edge_collapse(viewer * p_view); - static void select_multiple(viewer * p_view); + static bool process_farthest_point_sampling(viewer * p_view); + static bool process_farthest_point_sampling_radio(viewer * p_view); + static bool compute_toplesets(viewer * p_view); + static bool process_voronoi(viewer * p_view); + + static bool process_mdict_patch(viewer * p_view); + static bool process_denoising(viewer * p_view); + static bool process_super_resolution(viewer * p_view); + static bool process_inpaiting(viewer * p_view); + static bool process_iterative_inpaiting(viewer * p_view); + + static bool process_functional_maps(viewer * p_view); + static bool process_gps(viewer * p_view); + static bool process_hks(viewer * p_view); + static bool process_wks(viewer * p_view); + static bool process_key_points(viewer * p_view); + static bool process_key_components(viewer * p_view); + + static bool process_poisson(viewer * p_view, const index_t & k); + static bool process_poisson_laplacian_1(viewer * p_view); + static bool process_poisson_laplacian_2(viewer * p_view); + static bool process_poisson_laplacian_3(viewer * p_view); + + static bool process_threshold(viewer * p_view); + static bool process_noise(viewer * p_view); + static bool process_black_noise(viewer * p_view); + static bool process_multiplicate_vertices(viewer * p_view); + static bool process_fill_holes(viewer * p_view); + static bool process_delete_vertices(viewer * p_view); + static bool process_fill_holes_test(viewer * p_view); + static bool process_delete_non_manifold_vertices(viewer * p_view); + static bool process_fill_holes_biharmonic_splines(viewer * p_view); + static bool process_gaussian_curvature(viewer * p_view); + static bool process_edge_collapse(viewer * p_view); + static bool select_multiple(viewer * p_view); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 71f84873..e1af5fb9 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -73,7 +73,7 @@ class viewer { protected: - using function_t = void (*) (viewer *); + using function_t = bool (*) (viewer *); struct process_t { @@ -184,30 +184,30 @@ class viewer // menu functions void menu_process(function_t pro); - static void menu_help(viewer * view); - static void menu_reset_mesh(viewer * view); - static void menu_save_mesh(viewer * view); - static void menu_zoom_in(viewer * view); - static void menu_zoom_out(viewer * view); - static void menu_bgc_inc(viewer * view); - static void menu_bgc_dec(viewer * view); - static void menu_bgc_white(viewer * view); - static void menu_bgc_black(viewer * view); + static bool menu_help(viewer * view); + static bool menu_reset_mesh(viewer * view); + static bool menu_save_mesh(viewer * view); + static bool menu_zoom_in(viewer * view); + static bool menu_zoom_out(viewer * view); + static bool menu_bgc_inc(viewer * view); + static bool menu_bgc_dec(viewer * view); + static bool menu_bgc_white(viewer * view); + static bool menu_bgc_black(viewer * view); // render options - static void invert_orientation(viewer * view); - static void set_render_gl(viewer * view); - static void set_render_embree(viewer * view); - static void set_render_optix(viewer * view); - static void set_render_wireframe(viewer * view); - static void set_render_wireframe_fill(viewer * view); - static void set_render_gradient_field(viewer * view); - static void set_render_normal_field(viewer * view); - static void set_render_border(viewer * view); - static void set_render_lines(viewer * view); - static void set_render_flat(viewer * view); + static bool invert_orientation(viewer * view); + static bool set_render_gl(viewer * view); + static bool set_render_embree(viewer * view); + static bool set_render_optix(viewer * view); + static bool set_render_wireframe(viewer * view); + static bool set_render_wireframe_fill(viewer * view); + static bool set_render_gradient_field(viewer * view); + static bool set_render_normal_field(viewer * view); + static bool set_render_border(viewer * view); + static bool set_render_lines(viewer * view); + static bool set_render_flat(viewer * view); - static void raycasting(viewer * view); + static bool raycasting(viewer * view); // draw routines void draw_meshes(shader & program); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 71749674..a1305bfd 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -120,7 +120,7 @@ int app_viewer::main(int nargs, const char ** args) return 0; } -void paint_holes_vertices(viewer * p_view) +bool paint_holes_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); @@ -132,9 +132,11 @@ void paint_holes_vertices(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) if(v >= nv) mesh->color(v) = .25; + + return false; } -void app_viewer::process_delete_non_manifold_vertices(viewer * p_view) +bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -143,22 +145,27 @@ void app_viewer::process_delete_non_manifold_vertices(viewer * p_view) gproshan_debug(removing vertex); mesh->remove_non_manifold_vertices(); gproshan_debug(removing vertex); + + return false; } -void app_viewer::process_delete_vertices(viewer * p_view) +bool app_viewer::process_delete_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - if(!view->mesh().selected.size()) return; + if(!view->mesh().selected.size()) return true; + gproshan_debug(removing vertex); mesh->remove_vertices(view->mesh().selected); view->mesh().selected.clear(); gproshan_debug(removing vertex); + + return false; } -void app_viewer::process_poisson(viewer * p_view, const index_t & k) +bool app_viewer::process_poisson(viewer * p_view, const index_t & k) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); @@ -170,27 +177,29 @@ void app_viewer::process_poisson(viewer * p_view, const index_t & k) gproshan_log_var(view->time); // paint_holes_vertices(); + + return false; } -void app_viewer::process_poisson_laplacian_1(viewer * p_view) +bool app_viewer::process_poisson_laplacian_1(viewer * p_view) { gproshan_log(APP_VIEWER); - process_poisson(p_view, 1); + return process_poisson(p_view, 1); } -void app_viewer::process_poisson_laplacian_2(viewer * p_view) +bool app_viewer::process_poisson_laplacian_2(viewer * p_view) { gproshan_log(APP_VIEWER); - process_poisson(p_view, 2); + return process_poisson(p_view, 2); } -void app_viewer::process_poisson_laplacian_3(viewer * p_view) +bool app_viewer::process_poisson_laplacian_3(viewer * p_view) { gproshan_log(APP_VIEWER); - process_poisson(p_view, 3); + return process_poisson(p_view, 3); } -void app_viewer::process_fill_holes(viewer * p_view) +bool app_viewer::process_fill_holes(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -199,9 +208,11 @@ void app_viewer::process_fill_holes(viewer * p_view) fill_all_holes(mesh); paint_holes_vertices(p_view); + + return false; } -void app_viewer::process_noise(viewer * p_view) +bool app_viewer::process_noise(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -219,9 +230,11 @@ void app_viewer::process_noise(viewer * p_view) } mesh->update_normals(); + + return false; } -void app_viewer::process_black_noise(viewer * p_view) +bool app_viewer::process_black_noise(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -239,9 +252,11 @@ void app_viewer::process_black_noise(viewer * p_view) } mesh->update_normals(); + + return false; } -void app_viewer::process_threshold(viewer * p_view) +bool app_viewer::process_threshold(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -249,9 +264,11 @@ void app_viewer::process_threshold(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) = mesh->color(v) > 0.5 ? 1 : 0.5; + + return false; } -void app_viewer::process_functional_maps(viewer * p_view) +bool app_viewer::process_functional_maps(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -289,9 +306,11 @@ void app_viewer::process_functional_maps(viewer * p_view) } view->current = 0; + + return false; } -void app_viewer::process_wks(viewer * p_view) +bool app_viewer::process_wks(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -326,9 +345,11 @@ void app_viewer::process_wks(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) /= max_s; + + return false; } -void app_viewer::process_hks(viewer * p_view) +bool app_viewer::process_hks(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -348,7 +369,7 @@ void app_viewer::process_hks(viewer * p_view) TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) gproshan_log_var(view->time); - if(!K) return; + if(!K) return true; real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) @@ -367,9 +388,11 @@ void app_viewer::process_hks(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) /= max_s; + + return false; } -void app_viewer::process_gps(viewer * p_view) +bool app_viewer::process_gps(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -407,9 +430,11 @@ void app_viewer::process_gps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) /= max_s; + + return false; } -void app_viewer::process_key_points(viewer * p_view) +bool app_viewer::process_key_points(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -422,9 +447,11 @@ void app_viewer::process_key_points(viewer * p_view) for(index_t i = 0; i < kps.size(); i++) view->mesh().selected.push_back(kps[i]); + + return false; } -void app_viewer::process_key_components(viewer * p_view) +bool app_viewer::process_key_components(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -438,9 +465,11 @@ void app_viewer::process_key_components(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) = (real_t) kcs(v) / kcs; + + return false; } -void app_viewer::process_mdict_patch(viewer * p_view) +bool app_viewer::process_mdict_patch(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -489,9 +518,11 @@ void app_viewer::process_mdict_patch(viewer * p_view) delete [] toplevel; TOC(view->time) gproshan_debug_var(view->time); + + return false; } -void app_viewer::process_denoising(viewer * p_view) +bool app_viewer::process_denoising(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -511,9 +542,11 @@ void app_viewer::process_denoising(viewer * p_view) delete phi; mesh->update_normals(); + + return false; } -void app_viewer::process_super_resolution(viewer * p_view) +bool app_viewer::process_super_resolution(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -533,9 +566,11 @@ void app_viewer::process_super_resolution(viewer * p_view) delete phi; mesh->update_normals(); + + return false; } -void app_viewer::process_inpaiting(viewer * p_view) +bool app_viewer::process_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -555,17 +590,21 @@ void app_viewer::process_inpaiting(viewer * p_view) delete phi; mesh->update_normals(); + + return false; } -void app_viewer::process_iterative_inpaiting(viewer * p_view) +bool app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); // mesh_iterative_inpaiting(mesh, view->mesh().selected, freq, rt, m, M, f, learn); + + return false; } -void app_viewer::process_multiplicate_vertices(viewer * p_view) +bool app_viewer::process_multiplicate_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -573,9 +612,11 @@ void app_viewer::process_multiplicate_vertices(viewer * p_view) mesh->multiplicate_vertices(); mesh.log_info(); + + return false; } -void app_viewer::compute_toplesets(viewer * p_view) +bool app_viewer::compute_toplesets(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -602,9 +643,11 @@ void app_viewer::compute_toplesets(viewer * p_view) delete [] toplesets; delete [] sorted; + + return false; } -void app_viewer::process_voronoi(viewer * p_view) +bool app_viewer::process_voronoi(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -625,9 +668,11 @@ void app_viewer::process_voronoi(viewer * p_view) mesh->color(i) = ptp.clusters[i]; mesh->color(i) /= view->mesh().selected.size() + 1; } + + return false; } -void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) +bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -648,9 +693,11 @@ void app_viewer::process_farthest_point_sampling_radio(viewer * p_view) gproshan_log_var(radio); gproshan_log_var(view->mesh().selected.size()); gproshan_log_var(view->time); + + return false; } -void app_viewer::process_farthest_point_sampling(viewer * p_view) +bool app_viewer::process_farthest_point_sampling(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -664,9 +711,11 @@ void app_viewer::process_farthest_point_sampling(viewer * p_view) load_sampling(view->mesh().selected, radio, mesh, n); TOC(view->time) gproshan_log_var(view->time); + + return false; } -void app_viewer::process_fairing_spectral(viewer * p_view) +bool app_viewer::process_fairing_spectral(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -682,9 +731,11 @@ void app_viewer::process_fairing_spectral(viewer * p_view) delete fair; mesh->update_normals(); + + return false; } -void app_viewer::process_fairing_taubin(viewer * p_view) +bool app_viewer::process_fairing_taubin(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); @@ -700,9 +751,11 @@ void app_viewer::process_fairing_taubin(viewer * p_view) mesh->set_vertices(fair.get_postions()); mesh->update_normals(); } + + return true; } -void app_viewer::process_geodesics_fm(viewer * p_view) +bool app_viewer::process_geodesics_fm(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -717,9 +770,11 @@ void app_viewer::process_geodesics_fm(viewer * p_view) gproshan_log_var(view->time); mesh->update_colors(&fm[0]); + + return false; } -void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) +bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -734,9 +789,11 @@ void app_viewer::process_geodesics_ptp_cpu(viewer * p_view) gproshan_log_var(view->time); mesh->update_colors(&ptp[0]); + + return false; } -void app_viewer::process_geodesics_heat_flow(viewer * p_view) +bool app_viewer::process_geodesics_heat_flow(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -751,12 +808,14 @@ void app_viewer::process_geodesics_heat_flow(viewer * p_view) gproshan_log_var(view->time); mesh->update_colors(&heat_flow[0]); + + return false; } #ifdef GPROSHAN_CUDA -void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) +bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -779,9 +838,11 @@ void app_viewer::process_geodesics_ptp_gpu(viewer * p_view) gproshan_log_var(view->time); mesh->update_colors(&ptp[0]); + + return false; } -void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) +bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -796,12 +857,14 @@ void app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) gproshan_log_var(view->time); mesh->update_colors(&heat_flow[0]); + + return false; } #endif // GPROSHAN_CUDA -void app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) +bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -813,7 +876,7 @@ void app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) vector * border_vertices; che ** holes; tie(border_vertices, holes) = fill_all_holes_meshes(mesh); - if(!holes) return; + if(!holes) return true; index_t k = 2; @@ -828,9 +891,11 @@ void app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) delete [] holes; delete [] border_vertices; paint_holes_vertices(p_view); + + return false; } -void app_viewer::process_gaussian_curvature(viewer * p_view) +bool app_viewer::process_gaussian_curvature(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -878,9 +943,11 @@ void app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) = f(gv(v)); + + return false; } -void app_viewer::process_edge_collapse(viewer * p_view) +bool app_viewer::process_edge_collapse(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -897,9 +964,11 @@ void app_viewer::process_edge_collapse(viewer * p_view) view->corr_mesh[1].init(view->meshes[1]->n_vertices(), view->current, sampling); view->current = 1; + + return false; } -void app_viewer::select_multiple(viewer * p_view) +bool app_viewer::select_multiple(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -912,6 +981,8 @@ void app_viewer::select_multiple(viewer * p_view) while(ss >> v) view->mesh().selected.push_back(v); } + + return false; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1ce31ed0..14ccc244 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -143,12 +143,13 @@ bool viewer::run() for(auto & p: processes) { process_t & pro = p.second; - if(pro.selected) { ImGui::Begin(pro.name.c_str()); - p.second.function(this); - mesh().update_vbo(); + + pro.selected = p.second.function(this); + mesh().update_vbo(); + ImGui::End(); } } @@ -331,8 +332,6 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a if(view->processes[key].function) view->processes[key].selected = !view->processes[key].selected; - - //glClearColor(view->bgc, view->bgc, view->bgc, 1.); } void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods) @@ -381,14 +380,16 @@ void viewer::idle() ////glutPostRedisplay(); } -void viewer::menu_help(viewer * view) +bool viewer::menu_help(viewer * view) { for(auto & p: view->processes) if(p.second.function != nullptr) fprintf(stderr, "%16s: %s\n", ('[' + p.second.key + ']').c_str(), p.second.name.c_str()); + + return false; } -void viewer::menu_reset_mesh(viewer * view) +bool viewer::menu_reset_mesh(viewer * view) { view->mesh().selected.clear(); view->other_vertices.clear(); @@ -396,9 +397,11 @@ void viewer::menu_reset_mesh(viewer * view) view->mesh().reload(); view->mesh().update_vbo(); + + return false; } -void viewer::menu_save_mesh(viewer * view) +bool viewer::menu_save_mesh(viewer * view) { gproshan_log(APP_VIEWER); @@ -412,96 +415,140 @@ void viewer::menu_save_mesh(viewer * view) if(format == "ply") che_ply::write_file(view->mesh(), file); cerr << "saved: " << file + "." + format << endl; + + return false; } -void viewer::menu_zoom_in(viewer * view) +bool viewer::menu_zoom_in(viewer * view) { view->cam.zoomIn(); + + return false; } -void viewer::menu_zoom_out(viewer * view) +bool viewer::menu_zoom_out(viewer * view) { view->cam.zoomOut(); + + return false; } -void viewer::menu_bgc_inc(viewer * view) +bool viewer::menu_bgc_inc(viewer * view) { if(view->bgc < 1) view->bgc += 0.05; + else view->bgc = 1; + + glClearColor(view->bgc, view->bgc, view->bgc, 1.); + + return false; } -void viewer::menu_bgc_dec(viewer * view) +bool viewer::menu_bgc_dec(viewer * view) { if(view->bgc > 0) view->bgc -= 0.05; + else view->bgc = 0; + + glClearColor(view->bgc, view->bgc, view->bgc, 1.); + + return false; } -void viewer::menu_bgc_white(viewer * view) +bool viewer::menu_bgc_white(viewer * view) { view->bgc = 1; + glClearColor(view->bgc, view->bgc, view->bgc, 1.); + + return false; } -void viewer::menu_bgc_black(viewer * view) +bool viewer::menu_bgc_black(viewer * view) { view->bgc = 0; + glClearColor(view->bgc, view->bgc, view->bgc, 1.); + + return false; } -void viewer::invert_orientation(viewer * view) +bool viewer::invert_orientation(viewer * view) { view->mesh().invert_orientation(); + + return false; } -void viewer::set_render_gl(viewer * view) +bool viewer::set_render_gl(viewer * view) { view->render_opt = 0; + + return false; } -void viewer::set_render_embree(viewer * view) +bool viewer::set_render_embree(viewer * view) { view->render_opt = 1; + + return false; } -void viewer::set_render_optix(viewer * view) +bool viewer::set_render_optix(viewer * view) { view->render_opt = 2; + + return false; } -void viewer::set_render_wireframe(viewer * view) +bool viewer::set_render_wireframe(viewer * view) { view->render_wireframe = !view->render_wireframe; + + return false; } -void viewer::set_render_wireframe_fill(viewer * view) +bool viewer::set_render_wireframe_fill(viewer * view) { view->render_wireframe_fill = !view->render_wireframe_fill; + + return false; } -void viewer::set_render_gradient_field(viewer * view) +bool viewer::set_render_gradient_field(viewer * view) { view->render_gradient_field = !view->render_gradient_field; + + return false; } -void viewer::set_render_normal_field(viewer * view) +bool viewer::set_render_normal_field(viewer * view) { view->render_normal_field = !view->render_normal_field; + + return false; } -void viewer::set_render_border(viewer * view) +bool viewer::set_render_border(viewer * view) { view->render_border = !view->render_border; if(!view->render_border) view->mesh().selected.clear(); + + return false; } -void viewer::set_render_lines(viewer * view) +bool viewer::set_render_lines(viewer * view) { view->render_lines = !view->render_lines; + + return false; } -void viewer::set_render_flat(viewer * view) +bool viewer::set_render_flat(viewer * view) { view->render_flat = !view->render_flat; view->action = true; + + return false; } -void viewer::raycasting(viewer * view) +bool viewer::raycasting(viewer * view) { #ifdef GPROSHAN_EMBREE @@ -522,6 +569,8 @@ void viewer::raycasting(viewer * view) delete [] frame; #endif // GPROSHAN_EMBREE + + return false; } void viewer::render_gl() From acf8fed589fa9ff86df802414d3f06d907064481 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 22 Mar 2020 21:57:07 +0100 Subject: [PATCH 0235/1018] imgui windows focus --- CMakeLists.txt | 2 +- include/viewer/viewer.h | 3 +- src/app_viewer.cpp | 248 ++++++++++++++++++++++------------------ src/viewer/viewer.cpp | 10 +- 4 files changed, 145 insertions(+), 118 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24c97d1c..e5cffe01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(CUDA 10.1) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index e1af5fb9..93a2abb6 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -128,6 +128,7 @@ class viewer #endif // GPROSHAN_OPTIX bool action = false; + bool imgui_focus = false; bool render_wireframe = false; bool render_wireframe_fill = false; @@ -182,8 +183,6 @@ class viewer static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); // menu functions - void menu_process(function_t pro); - static bool menu_help(viewer * view); static bool menu_reset_mesh(viewer * view); static bool menu_save_mesh(viewer * view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a1305bfd..a0f2e6c5 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -270,168 +270,186 @@ bool app_viewer::process_threshold(viewer * p_view) bool app_viewer::process_functional_maps(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t K = 100; + static int K = 20; + + ImGui::InputInt("eigenvectors", &K); - a_sp_mat L, A; + if(ImGui::Button("Run")) + { + a_sp_mat L, A; - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); - a_vec eigval; - a_mat eigvec; + a_vec eigval; + a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); - gproshan_log_var(K); + gproshan_log_var(K); - K = K < N_MESHES ? K : N_MESHES; - for(index_t k = 0; k < N_MESHES; k++) - { - if(k) view->add_mesh({new che(*mesh)}); - view->current = k; + K = K < N_MESHES ? K : N_MESHES; + for(index_t k = 0; k < N_MESHES; k++) + { + if(k) view->add_mesh({new che(*mesh)}); + view->current = k; - eigvec.col(k) -= eigvec.col(k).min(); - eigvec.col(k) /= eigvec.col(k).max(); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - view->mesh()->color(v) = eigvec(v, k); + eigvec.col(k) -= eigvec.col(k).min(); + eigvec.col(k) /= eigvec.col(k).max(); + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + view->mesh()->color(v) = eigvec(v, k); - view->mesh().update_vbo(); + view->mesh().update_vbo(); + } + + view->current = 0; } - - view->current = 0; - - return false; + + return true; } bool app_viewer::process_wks(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t K = 50, T = 100; + static int K = 100; + static int T = 100; - a_sp_mat L, A; + ImGui::InputInt("eigenvectors", &K); + ImGui::InputInt("times", &T); - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); + if(ImGui::Button("Run")) + { + a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + a_vec eigval; + a_mat eigvec; - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - a_vec s(T, arma::fill::zeros); - for(index_t t = 0; t < T; t++) - for(index_t k = 1; k < K; k++) - s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); + + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + a_vec s(T, arma::fill::zeros); + for(int t = 0; t < T; t++) + for(int k = 1; k < K; k++) + s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - mesh->color(v) = norm(s); - max_s = max(max_s, mesh->color(v)); + mesh->color(v) = norm(s); + max_s = max(max_s, mesh->color(v)); + } + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh->color(v) /= max_s; } - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; - - return false; + return true; } bool app_viewer::process_hks(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t K = 100; - size_t T = 100; + static int K = 100; + static int T = 100; - a_sp_mat L, A; + ImGui::InputInt("eigenvectors", &K); + ImGui::InputInt("times", &T); - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); + if(ImGui::Button("Run")) + { + a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + a_vec eigval; + a_mat eigvec; - if(!K) return true; + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - a_vec s(T, arma::fill::zeros); - for(index_t t = 0; t < T; t++) - for(index_t k = 1; k < K; k++) - s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); - - mesh->color(v) = norm(abs(arma::fft(s, 128))); - //mesh->color(v) = norm(s); - max_s = max(max_s, mesh->color(v)); + if(!K) return true; + + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + a_vec s(T, arma::fill::zeros); + for(int t = 0; t < T; t++) + for(int k = 1; k < K; k++) + s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); + + mesh->color(v) = norm(abs(arma::fft(s, 128))); + //mesh->color(v) = norm(s); + max_s = max(max_s, mesh->color(v)); + } + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh->color(v) /= max_s; } - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; - - return false; + return true; } bool app_viewer::process_gps(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t K = 50; + static int K = 50; + ImGui::InputInt("eigenvectors", &K); - a_sp_mat L, A; + if(ImGui::Button("Run")) + { + a_sp_mat L, A; - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); + TIC(view->time) laplacian(mesh, L, A); TOC(view->time) + gproshan_log_var(view->time); - a_vec eigval; - a_mat eigvec; + a_vec eigval; + a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + gproshan_log_var(view->time); - eigvec = abs(eigvec); - eigvec.col(0).zeros(); - for(index_t i = 1; i < K; i++) - eigvec.col(i) /= sqrt(abs(eigval(i))); + eigvec = abs(eigvec); + eigvec.col(0).zeros(); + for(int i = 1; i < K; i++) + eigvec.col(i) /= sqrt(abs(eigval(i))); - a_mat data = eigvec.t(); - a_mat means; + a_mat data = eigvec.t(); + a_mat means; - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - mesh->color(v) = norm(eigvec.row(v)); - max_s = max(max_s, mesh->color(v)); + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + mesh->color(v) = norm(eigvec.row(v)); + max_s = max(max_s, mesh->color(v)); + } + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh->color(v) /= max_s; } - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; - - return false; + return true; } bool app_viewer::process_key_points(viewer * p_view) @@ -699,20 +717,24 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) bool app_viewer::process_farthest_point_sampling(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - gproshan_input(samples_number); - index_t n; cin >> n; + static int n = 10; + static real_t radio; - real_t radio; - TIC(view->time) - load_sampling(view->mesh().selected, radio, mesh, n); - TOC(view->time) - gproshan_log_var(view->time); + ImGui::InputInt("samples", &n, 1, mesh->n_vertices() / 6); + ImGui::Text("radio: %.3f", radio); - return false; + if(ImGui::Button("Run")) + { + TIC(view->time) + load_sampling(view->mesh().selected, radio, mesh, n); + TOC(view->time) + gproshan_log_var(view->time); + } + + return true; } bool app_viewer::process_fairing_spectral(viewer * p_view) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 14ccc244..68029c4a 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -140,14 +140,16 @@ bool viewer::run() ImGui::EndMainMenuBar(); } + imgui_focus = false; for(auto & p: processes) { process_t & pro = p.second; if(pro.selected) { - ImGui::Begin(pro.name.c_str()); + ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); - pro.selected = p.second.function(this); + imgui_focus = imgui_focus || ImGui::IsWindowFocused(); + pro.selected = pro.selected && p.second.function(this); mesh().update_vbo(); ImGui::End(); @@ -329,6 +331,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a } viewer * view = (viewer *) glfwGetWindowUserPointer(window); + if(view->imgui_focus) return; if(view->processes[key].function) view->processes[key].selected = !view->processes[key].selected; @@ -352,6 +355,8 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(state == GLFW_PRESS) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); + if(view->imgui_focus) return; + view->cam.motion(x, y); view->action = true; } @@ -360,6 +365,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); + if(view->imgui_focus) return; if(yoffset > 0) { From 70a177654c28b3cfe4396da70cf274300a453ec1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 23 Mar 2020 11:29:23 +0100 Subject: [PATCH 0236/1018] fix mouse/keyboard imgui glfw --- include/viewer/viewer.h | 1 - src/app_viewer.cpp | 24 ++++++++++++------------ src/viewer/viewer.cpp | 11 +++++------ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 93a2abb6..0cd7708e 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -128,7 +128,6 @@ class viewer #endif // GPROSHAN_OPTIX bool action = false; - bool imgui_focus = false; bool render_wireframe = false; bool render_wireframe_fill = false; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a0f2e6c5..11853344 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -723,7 +723,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) static int n = 10; static real_t radio; - ImGui::InputInt("samples", &n, 1, mesh->n_vertices() / 6); + ImGui::SliderInt("samples", &n, 1, mesh->n_vertices() / 6); ImGui::Text("radio: %.3f", radio); if(ImGui::Button("Run")) @@ -739,22 +739,22 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) bool app_viewer::process_fairing_spectral(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - gproshan_input(k (eigenvectors number)); - size_t k; cin >> k; + static int k = 100; + ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices() / 6); - fairing * fair = new fairing_spectral(k); - fair->run(mesh); + if(ImGui::Button("Run")) + { + fairing_spectral fair(k); + fair.run(mesh); - mesh->set_vertices(fair->get_postions()); - delete fair; + mesh->set_vertices(fair.get_postions()); + mesh->update_normals(); + } - mesh->update_normals(); - - return false; + return true; } bool app_viewer::process_fairing_taubin(viewer * p_view) @@ -762,7 +762,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - static real_t step = 0.001; //cin >> step; + static float step = 0.001; //cin >> step; ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); if(ImGui::Button("Run")) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 68029c4a..e833e1d2 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -111,7 +111,7 @@ bool viewer::run() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; i++) - if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str())) + if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == current, i != current)) { current = i; sphere_translations.clear(); @@ -140,15 +140,14 @@ bool viewer::run() ImGui::EndMainMenuBar(); } - imgui_focus = false; for(auto & p: processes) { process_t & pro = p.second; if(pro.selected) { + ImGui::SetNextWindowSize(ImVec2(256, -1)); ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); - imgui_focus = imgui_focus || ImGui::IsWindowFocused(); pro.selected = pro.selected && p.second.function(this); mesh().update_vbo(); @@ -331,7 +330,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a } viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(view->imgui_focus) return; + if(ImGui::GetIO().WantCaptureKeyboard) return; if(view->processes[key].function) view->processes[key].selected = !view->processes[key].selected; @@ -355,7 +354,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(state == GLFW_PRESS) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(view->imgui_focus) return; + if(ImGui::GetIO().WantCaptureMouse) return; view->cam.motion(x, y); view->action = true; @@ -365,7 +364,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(view->imgui_focus) return; + if(ImGui::GetIO().WantCaptureMouse) return; if(yoffset > 0) { From a1612ddb54791c4eb7b62fb25845b24a0b58ef97 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 23 Mar 2020 20:34:44 +0100 Subject: [PATCH 0237/1018] updating new interface --- src/app_viewer.cpp | 165 +++++++++++++++++++++------------------ src/mdict/inpainting.cpp | 13 ++- src/mdict/patch.cpp | 2 - 3 files changed, 97 insertions(+), 83 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a737d642..a10f6cf5 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -600,103 +600,120 @@ bool app_viewer::process_super_resolution(viewer * p_view) bool app_viewer::process_inpaiting(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t n; // dct - size_t m, M; - real_t f; - bool learn; - size_t avg_p = 36; - size_t percentage = 0; - double delta = PI/6; - double sum_thres; + static int n = 12; // dct + static int m = 144; + static int M = 0; + static float f = 1; + static bool learn = 0; + static int avg_p = 36; + static int percentage = 0; + static float delta = PI/6; + static float sum_thres = 0.001; - gproshan_input(n m M f learn sum_thres); - cin >> n >> m >> M >> f >> learn >> sum_thres; - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); - dict.execute(); - delete phi; - mesh->update_colors(&dict[0]); - - mesh->update_normals(); + ImGui::InputInt("basis", &n); + ImGui::InputInt("atoms", &m); + ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::Checkbox("learn", &learn); - return false; + if(ImGui::Button("Run")) + { + basis * phi = new basis_dct(n); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + dict.execute(); + delete phi; + mesh->update_colors(&dict[0]); + + mesh->update_normals(); + } + return true; } bool app_viewer::process_mask(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t n = 12; // dct - size_t m = 144, M = 0; - real_t f = 1; - bool learn = 0; - size_t avg_p = 36; - size_t percentage = 0; - double delta = PI/6; - double sum_thres; - - gproshan_input(sum_thres ); - cin >> sum_thres; + static int n = 12; // dct + static int m = 144; + static int M = 0; + static float f = 1; + static bool learn = 0; + static int avg_p = 36; + static int percentage = 0; + static float delta = PI/6; + static float sum_thres = 0.001; + + ImGui::InputInt("basis", &n); + ImGui::InputInt("atoms", &m); + ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::Checkbox("learn", &learn); - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); - - dict.init_radial_feature_patches(); - //dict.init_voronoi_patches(); - delete phi; - mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + ".points"); - a_vec points_out; - points_out.load(f_points); - for(int i = 0; i< points_out.size(); i++) - mesh.selected.push_back(points_out(i)); - - mesh->update_normals(); - - return false; + if(ImGui::Button("Run")) + { + basis * phi = new basis_dct(n); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + + dict.init_radial_feature_patches(); + //dict.init_voronoi_patches(); + delete phi; + mesh->update_colors(&dict[0]); + string f_points = tmp_file_path(mesh->name_size() + ".points"); + a_vec points_out; + points_out.load(f_points); + for(int i = 0; i< points_out.size(); i++) + mesh.selected.push_back(points_out(i)); + + mesh->update_normals(); + } + return true; } bool app_viewer::process_pc_reconstruction(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->mesh(); - size_t n = 12; // dct - size_t m = 144, M = 0; - real_t f = 1; - bool learn = 0; - size_t avg_p = 36; - size_t percentage = 0; - double delta = PI/6; - double sum_thres; + static int n = 12; // dct + static int m = 144; + static int M = 0; + static float f = 1; + static bool learn = 0; + static int avg_p = 36; + static int percentage = 0; + static float delta = PI/6; + static float sum_thres = 0.001; + + ImGui::InputInt("basis", &n); + ImGui::InputInt("atoms", &m); + ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::Checkbox("learn", &learn); - gproshan_input(sum_thres ); - cin >> sum_thres; - - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); - - dict.point_cloud_reconstruction(); - //dict.init_voronoi_patches(); - delete phi; - mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + ".points"); - a_vec points_out; - points_out.load(f_points); - for(int i = 0; i< points_out.size(); i++) - mesh.selected.push_back(points_out(i)); + if(ImGui::Button("Run")) + { + basis * phi = new basis_dct(n); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + + dict.point_cloud_reconstruction(); + //dict.init_voronoi_patches(); + delete phi; + mesh->update_colors(&dict[0]); + string f_points = tmp_file_path(mesh->name_size() + ".points"); + a_vec points_out; + points_out.load(f_points); + for(int i = 0; i< points_out.size(); i++) + mesh.selected.push_back(points_out(i)); + + mesh->update_normals(); + } - mesh->update_normals(); - - return false; + return true; } bool app_viewer::process_synthesis(viewer * p_view) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 404148fc..e8a031fb 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -558,6 +558,8 @@ void inpainting::point_cloud_reconstruction() if( S(i,3) > max_radio) max_radio = S(i,3); size_t total_points = 0; + A.eye(phi_basis->get_dim(), m); + for(index_t i = 0; i < M; i++) { @@ -579,16 +581,13 @@ void inpainting::point_cloud_reconstruction() total_points += patches[i].vertices.size(); patches[i].phi.set_size(patches[i].vertices.size(), phi_basis->get_dim()); phi_basis->discrete(patches[i].phi, patches[i].xyz); + - // a_vec x = patches[i].phi * A * alpha.col(i); - // patches[i].xyz.row(2) = x.t(); + a_vec x = patches[i].phi * A * alpha.col(i); + patches[i].xyz.row(2) = x.t(); } - - -// a_vec x = rp.phi * A * alpha.col(i); -// rp.xyz.row(2) = x.t(); - + gproshan_debug_var(total_points); for(index_t i = 0; i < M; i++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 09cc5068..f009a4d0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -139,8 +139,6 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio) size_t n_points = (radio/max_radio) * 100; - gproshan_debug_var(n_points); - vertices.resize(n_points); xyz.resize(3,n_points); From 1f00c5eb59eabbd90b9ae753b142ddcc5f64fe80 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Sat, 28 Mar 2020 13:16:01 +0100 Subject: [PATCH 0238/1018] area ratio proj area added --- include/che.h | 2 +- include/mdict/patch.h | 2 +- shaders/vertex_pointcloud.glsl | 1 + src/app_viewer.cpp | 6 ++--- src/che.cpp | 16 ++++++------ src/key_points.cpp | 2 +- src/mdict/inpainting.cpp | 1 + src/mdict/patch.cpp | 47 ++++++++++++++++++++++++---------- src/viewer/viewer.cpp | 1 + 9 files changed, 51 insertions(+), 27 deletions(-) diff --git a/include/che.h b/include/che.h index e372ca8c..eed8239b 100644 --- a/include/che.h +++ b/include/che.h @@ -65,7 +65,7 @@ class che void flip(const index_t & e); real_t pdetriq(const index_t & t) const; real_t quality(); - real_t real_trig(const index_t & t) const; + real_t area_trig(const index_t & t) const; real_t area_vertex(const index_t & v); real_t area_surface() const; void update_colors(const real_t * vcolor = nullptr); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index e7e1e8dc..3408bf1f 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -96,7 +96,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double &sum, double deviation); + bool add_vertex_by_faces(vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation); private: diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index 2abea13f..ea4dc026 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -18,5 +18,6 @@ void main() vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); + gl_PointSize = 2; } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a10f6cf5..647fc12d 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -617,7 +617,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) @@ -651,7 +651,7 @@ bool app_viewer::process_mask(viewer * p_view) ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) @@ -692,7 +692,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 3); + ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) diff --git a/src/che.cpp b/src/che.cpp index 37d36398..ab81d8dd 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -195,7 +195,7 @@ real_t che::pdetriq(const index_t & t) const *(GT[VT[prev(he)]] - GT[VT[next(he)]]), *(GT[VT[he]] - GT[VT[prev(he)]]) }; - return (4 * sqrt(3) * real_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); + return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } real_t che::quality() @@ -209,7 +209,7 @@ real_t che::quality() return q * 100 / n_faces_; } -real_t che::real_trig(const index_t & t) const +real_t che::area_trig(const index_t & t) const { index_t he = t * P; vertex a = GT[VT[next(he)]] - GT[VT[he]]; @@ -222,7 +222,7 @@ real_t che::area_vertex(const index_t & v) { real_t area_star = 0; for_star(he, this, v) - area_star += real_trig(trig(he)); + area_star += area_trig(trig(he)); return area_star / 3; } @@ -233,7 +233,7 @@ real_t che::area_surface() const #pragma omp parallel for reduction(+: area) for(index_t i = 0; i < n_faces_; i++) - area += real_trig(i); + area += area_trig(i); return area; } @@ -286,7 +286,7 @@ void che::update_normals() n = 0; for_star(he, this, v) - n += real_trig(trig(he)) * normal_he(he); + n += area_trig(trig(he)) * normal_he(he); n /= *n; } @@ -334,7 +334,7 @@ vertex che::gradient_he(const index_t & he, const real_t *const & f) const vertex n = normal_he(he); - real_t A2 = real_trig(trig(he)) * 2; + real_t A2 = area_trig(trig(he)) * 2; vertex pij = n * (xj - xi); vertex pjk = n * (xk - xj); @@ -351,7 +351,7 @@ vertex che::gradient(const index_t & v, const real_t *const & f) for_star(he, this, v) { - area = real_trig(trig(he)); + area = area_trig(trig(he)); area_star += area; g += area * gradient_he(he, f); } @@ -422,7 +422,7 @@ real_t che::mean_curvature(const index_t & v) for_star(he, this, v) { - a += real_trig(trig(he)); + a += area_trig(trig(he)); h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); } diff --git a/src/key_points.cpp b/src/key_points.cpp index bb5b88a8..66d163ba 100644 --- a/src/key_points.cpp +++ b/src/key_points.cpp @@ -56,7 +56,7 @@ void key_points::compute_kps(che * mesh) #pragma omp parallel for for(index_t t = 0; t < n_faces; t++) { - face_areas[t].first = mesh->real_trig(t); + face_areas[t].first = mesh->area_trig(t); face_areas[t].second = t; } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e8a031fb..3aebfe5c 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -609,6 +609,7 @@ void inpainting::point_cloud_reconstruction() } che nmesh(point_cloud, total_points, nullptr, 0); + gproshan_debug_var(sum_thres); string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + "_pc"); che_off::write_file(&nmesh,f_pc); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f009a4d0..d4767017 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -74,21 +74,25 @@ index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) if(indexes[i] == idx_global) return i; return -1; } -bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double &sum, double deviation) +bool patch::add_vertex_by_faces(vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { index_t a, b, i = 0; vertex min_he; - double area_face = 0; + double area_face = 0, proj_area_face = 0; double angle = PI; double tmp_angle; bool added = false; + vertex pav, pbv, va, vb,vv; for_star(he, mesh, v) { a = mesh->vt(next(he)); //index of the next vertex index_t b = mesh->vt(prev(he)); + va = mesh->gt(a); + vb = mesh->gt(b); + vv = mesh->gt(v); // If is an adjacent face if( geo[a] < geo[v] || geo[b] < geo[v] ) { @@ -107,7 +111,12 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc { angle = tmp_angle; //gproshan_debug_var(he); - area_face = mesh->real_trig(he/3); + area_face = mesh->area_trig(he/3); + // compute projected area + pav = va - vv + ( (n,vv) - (n,va) ) * n; + pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; + proj_area_face = *(pav * pbv) / 2; + min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); added = true; @@ -116,8 +125,10 @@ bool patch::add_vertex_by_faces(vector & N, index_t * indexes, size_t nc } } + area += area_face; + proj_area += proj_area_face; - sum += area_face; +// sum += (proj_area_face/area_face); //sum += acos( (min_he, N[i]) ); N.push_back(min_he); @@ -135,17 +146,19 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio) std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() - std::uniform_real_distribution<> dis(0, 1); - + // std::uniform_real_distribution<> dis(0, 1); + std::normal_distribution dis(0,1); - size_t n_points = (radio/max_radio) * 100; + // free the parameters to the interface + // fix the point cloud viewer + size_t n_points = (radio/max_radio) * 50; // change this using a sigmoid function vertices.resize(n_points); xyz.resize(3,n_points); for(size_t i=0 ;i N; N.push_back(n); //double angle; - double sum_angle = 0; + double area = 0; + double proj_area = 0; double area_mesh = mesh->area_surface(); + double ratio; for(index_t i=1; i radio) { @@ -263,6 +283,7 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ } // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } + gproshan_debug_var(vertices.size()); // Refit the points and update the radius size_t d_fitting = 2; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e833e1d2..35db54f7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -218,6 +218,7 @@ void viewer::init_gl() glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_PROGRAM_POINT_SIZE); } void viewer::init_imgui() From 300041e6bebc875a490dd7c9a09c5c7b010fd4bc Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 1 Apr 2020 13:36:22 +0200 Subject: [PATCH 0239/1018] adding by triangles --- include/mdict/patch.h | 3 +- src/mdict/inpainting.cpp | 4 +- src/mdict/patch.cpp | 82 +++++++++++++++++++++++++++++++--------- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 3408bf1f..45955e19 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -96,7 +96,8 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation); + bool add_vertex_by_faces(vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, + const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation); private: diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 3aebfe5c..f9514412 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -264,8 +264,8 @@ void inpainting::load_sampling(bool save_all) } a_vec outlv(seeds.size()); gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < seeds.size(); i++) - outlv(i) = seeds[i]; + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; outlv.save(f_points); S.resize(seeds.size(),2); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d4767017..3cdfd521 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -74,10 +74,14 @@ index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) if(indexes[i] == idx_global) return i; return -1; } -bool patch::add_vertex_by_faces(vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) +bool patch::add_vertex_by_faces(vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, + che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { - - index_t a, b, i = 0; + // it needs to return both vertices + // it needs to filter repeated indexes. + // p should be the maximun + + index_t a, b, i = 0, ma, mb; vertex min_he; double area_face = 0, proj_area_face = 0; double angle = PI; @@ -119,12 +123,47 @@ bool patch::add_vertex_by_faces(vertex & n, vector & N, index_t * indexe min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); + ma = a; + mb = b; added = true; } } } + //p = mesh->get_vertex(indexes[i]); + //p = p - c ; + //p = p - ((p,n)*n); + + if(added) + { + //gproshan_debug_var(ma); + //gproshan_debug_var(mb); + + if( !exists(ma) ) vertices.push_back(ma); + if( !exists(mb) ) vertices.push_back(mb); + + vertex pa, pb, mab; + pa = mesh->get_vertex(ma); + pb = mesh->get_vertex(mb); + + pa = pa - c; + pb = pb - c; + + pa = pa - ((pa,n)*n); + pb = pb - ((pb,n)*n); + if( *pa > *pb) mab = mesh->get_vertex(ma); + else + mab = mesh->get_vertex(mb); + + p = mesh->get_vertex(v); + p = p - c ; + p = p - ((p,n)*n); + + if(*mab > *p) p = mab; + + // get the one who has the greatest distance + } area += area_face; proj_area += proj_area_face; @@ -225,7 +264,7 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ normal_fit_directions(mesh, v); - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, 1.2 * radio_); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -246,13 +285,15 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ for(index_t i=1; i radio_) break; + - p = mesh->get_vertex(indexes[i]); c = mesh->get_vertex(v); // central vertices + /*p = mesh->get_vertex(indexes[i]); p = p - c ; p = p - ((p,n)*n); - + */ //if( angle < PI/2.5 && (sum_angle) <= delta * PI) @@ -260,22 +301,24 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ //if( add_vertex_by_faces(n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], sum_angle, PI/2.5 ) && sum_angle/area_mesh < sum_thres ) ratio = (i==1)? 0:(area/proj_area); - gproshan_debug_var(proj_area); + /*gproshan_debug_var(proj_area); gproshan_debug_var(area); - gproshan_debug_var(ratio); + gproshan_debug_var(ratio);*/ + // p es el vertice de la malla el maximo - if( add_vertex_by_faces(n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && ratio < sum_thres ) - { + if( add_vertex_by_faces(p,c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && ratio < sum_thres ) + { + //compute euclidean radio + //p = mesh->get_vertex(indexes[i]); + if(*(p - c) > euc_radio) + euc_radio = *(p - c); - + /* p = p - c ; + p = p - ((p,n)*n); if(*p > radio) { radio = *p; - } - //compute euclidean radio - p = mesh->get_vertex(indexes[i]); - if(*(p - c) > euc_radio) - euc_radio = *(p - c); + }*/ } else { @@ -283,7 +326,7 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ } // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } - gproshan_debug_var(vertices.size()); + //gproshan_debug_var(vertices.size()); // Refit the points and update the radius size_t d_fitting = 2; @@ -296,7 +339,7 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ for(index_t i=1; i < vertices.size(); i++) { - p = mesh->get_vertex(indexes[i]); + p = mesh->get_vertex(vertices[i]); c = mesh->get_vertex(v); // central vertices p = p - c ; @@ -310,6 +353,9 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ } } + //gproshan_debug_var(vertices.size()); + //gproshan_debug_var(geo.n_sorted_index()); + geo_radio = geo[indexes [vertices.size()-1]]; delete indexes; From 724c60ea532979396fa4eac31fb1a09431e29e99 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 8 Apr 2020 16:02:54 +0200 Subject: [PATCH 0240/1018] fixing outliers showing points --- src/app_viewer.cpp | 4 +++- src/mdict/inpainting.cpp | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 647fc12d..c1e44887 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -663,9 +663,11 @@ bool app_viewer::process_mask(viewer * p_view) //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) + ".points"); a_vec points_out; + gproshan_debug_var(f_points); points_out.load(f_points); + gproshan_debug_var(points_out.size()); for(int i = 0; i< points_out.size(); i++) mesh.selected.push_back(points_out(i)); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index f9514412..378da9f6 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -158,7 +158,7 @@ void inpainting::load_sampling(bool save_all) gproshan_debug_var(d_time); gproshan_debug_var(all_sorted_features.size()); - string f_points = tmp_file_path(mesh->name_size() + ".points"); + string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + ".points"); vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); gproshan_debug_var(features.size()); @@ -262,7 +262,7 @@ void inpainting::load_sampling(bool save_all) //gproshan_debug_var(geo[indexes[i]] ); } } - a_vec outlv(seeds.size()); + a_vec outlv(outliers.size()); gproshan_debug_var(outliers.size()); for(index_t i = 0; i < outliers.size(); i++) outlv(i) = outliers[i]; From 5e5d92a7f0184015c57d0b6053232cf83b321203 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Apr 2020 12:07:22 +0200 Subject: [PATCH 0241/1018] mesh multiplicate vertices --- src/app_viewer.cpp | 3 +++ src/raytracing/rt_optix.cpp | 2 ++ src/viewer/viewer.cpp | 5 +++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 11853344..7b2b7980 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -5,6 +5,7 @@ using namespace std; using namespace gproshan::mdict; + // geometry processing and shape analysis framework namespace gproshan { @@ -629,6 +630,8 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) che_viewer & mesh = view->mesh(); mesh->multiplicate_vertices(); + mesh.update(); + mesh.log_info(); return false; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 0a5e386a..51472ffb 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -71,6 +71,8 @@ optix::optix(const std::vector & meshes) // create programs + + // build as build_as(meshes); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e833e1d2..faffe346 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -145,7 +145,8 @@ bool viewer::run() process_t & pro = p.second; if(pro.selected) { - ImGui::SetNextWindowSize(ImVec2(256, -1)); + ImGui::SetNextWindowSize(ImVec2(300, -1)); + ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); pro.selected = pro.selected && p.second.function(this); @@ -154,7 +155,7 @@ bool viewer::run() ImGui::End(); } } - + // Rendering ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); From 7961dd64a1c4c78690951c6e0afe9a8158e02d9c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Apr 2020 09:48:16 +0200 Subject: [PATCH 0242/1018] imgui: select vertices --- src/app_viewer.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7b2b7980..01232d9f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -995,11 +995,13 @@ bool app_viewer::process_edge_collapse(viewer * p_view) bool app_viewer::select_multiple(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - char line[128]; - if(fgets(line, 128, stdin)) + static char line[128] = ""; + + ImGui::InputText("select", line, sizeof(line)); + + if(ImGui::Button("Add")) { stringstream ss(line); index_t v; @@ -1007,7 +1009,7 @@ bool app_viewer::select_multiple(viewer * p_view) view->mesh().selected.push_back(v); } - return false; + return true; } From 1b090f5a25377db47384d60884fa37a8abd625ba Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 22 Apr 2020 16:22:00 +0200 Subject: [PATCH 0243/1018] Fixing recovering sampling from seed points --- include/mdict/patch.h | 6 +-- src/mdict/dictionary.cpp | 3 +- src/mdict/inpainting.cpp | 84 +++++++++++++++++++++++++++++++++++----- src/mdict/patch.cpp | 45 +++++++++++++-------- 4 files changed, 108 insertions(+), 30 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 45955e19..811d1af6 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -60,7 +60,7 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); - void init_radial_disjoint(che * mesh, + void init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, @@ -81,7 +81,7 @@ class patch const index_t & p, const fmask_t & mask = nullptr ); - void reset_xyz_disjoint( che * mesh, + void reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, @@ -96,7 +96,7 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, + bool add_vertex_by_faces(index_t & idx_he, vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index de9982eb..9329f077 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -386,7 +386,8 @@ const real_t & dictionary::operator[](const index_t & i) const void dictionary::draw_patches(index_t i) { - phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz, i); + gproshan_debug_var(patches[i].vertices[0]); + phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz, patches[i].vertices[0]); } void dictionary::save_alpha(string file) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 378da9f6..d09511c7 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -193,6 +193,9 @@ void inpainting::load_sampling(bool save_all) size_t count_cov = 0; size_t count_cov_patch = 0; + bool faces[mesh->n_faces()] = {}; + vector idxs_he; + for(size_t i = 0; i < all_sorted_features.size(); i++) { bool found = false; @@ -206,15 +209,22 @@ void inpainting::load_sampling(bool save_all) const vertex & v_seed = mesh->gt(seeds[j]); // 0.5 coverage parameter - if( *(v_patch - v_seed) < 0.5* radios[j] ) // radio of each patch + if( *(v_patch - v_seed) < 0.2* radios[j] ) + { + // radio of each patch found = true; + } j++; + } + if(!found) { + patch p; // increasing a bit the radio - p.init_radial_disjoint(mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres); + idxs_he.clear(); + p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; @@ -228,6 +238,7 @@ void inpainting::load_sampling(bool save_all) count_cov += count_cov_patch; if(count_cov_patch > 0) { + //gproshan_debug_var(p.vertices.size()); patches.push_back(p); seeds.push_back(all_sorted_features[i]); radios.push_back( euc_radio ); @@ -236,9 +247,26 @@ void inpainting::load_sampling(bool save_all) for(index_t k = 0; k < p.vertices.size(); k++) covered[ p.vertices[k] ] = 1; + + for(auto i:idxs_he) + faces[i] = true; + } } + /*if(all_sorted_features[i] == 10594 || all_sorted_features[i] == 3923 || all_sorted_features[i] == 9149 || all_sorted_features[i] == 5678 || + all_sorted_features[i] == 3067 || all_sorted_features[i] == 6203 || all_sorted_features[i] == 3238 || all_sorted_features[i] == 2870 || + all_sorted_features[i] == 699 || all_sorted_features[i] == 11532 || all_sorted_features[i] == 10749 || all_sorted_features[i] == 3989 || + all_sorted_features[i] == 1563 || all_sorted_features[i] == 7471 || all_sorted_features[i] == 2874 || all_sorted_features[i] == 8981 || + all_sorted_features[i] == 8678 || all_sorted_features[i] == 3008 || all_sorted_features[i] == 2689 ||all_sorted_features[i] == 5118 || + all_sorted_features[i] == 6368 || all_sorted_features[i] == 11880 || all_sorted_features[i] == 9617 || all_sorted_features[i] == 11882 || + all_sorted_features[i] == 1479 || all_sorted_features[i] == 8848 || all_sorted_features[i] == 8592 || all_sorted_features[i] == 2688 ) + { + gproshan_debug_var(all_sorted_features[i]); + gproshan_debug_var(count_cov_patch); + gproshan_debug_var(p.vertices.size()); + }*/ + } } @@ -253,19 +281,54 @@ void inpainting::load_sampling(bool save_all) /////////////////////////////////////// gproshan_debug_var(M); + index_t tmp; + bool remark[mesh->n_vertices()] = {}; - for(index_t i = 0; i < mesh->n_vertices(); i++) + //outliers by triangles. + for(index_t i = 0; i < mesh->n_faces(); i++) + { + if(!faces[i]) + { + tmp = mesh->vt(next(i*3)); + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + + tmp = mesh->vt(prev(i*3)); + + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + + tmp = mesh->vt(i*3); + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + } + + } + gproshan_debug_var(outliers.size()); + /*for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) { outliers.push_back(i); - //gproshan_debug_var(geo[indexes[i]] ); } + }*/ + a_vec outlv(seeds.size() ); + gproshan_debug_var(seeds.size()); + for(index_t i = 0; i < seeds.size(); i++) + { + outlv(i) = seeds[i]; + //gproshan_debug_var(seeds[i]); } - a_vec outlv(outliers.size()); - gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; + outlv.save(f_points); S.resize(seeds.size(),2); @@ -281,10 +344,13 @@ void inpainting::load_sampling(bool save_all) else { size_t n_seeds = S.n_rows; + vector idxs_he; + real_t euc_radio, geo_radio; for(index_t i = 0; i < n_seeds; i++) { patch p; - p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); + //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); + p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres); patches.push_back(p); } M = n_seeds; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 3cdfd521..96a8c20b 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -74,7 +74,7 @@ index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) if(indexes[i] == idx_global) return i; return -1; } -bool patch::add_vertex_by_faces(vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, +bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { // it needs to return both vertices @@ -113,9 +113,16 @@ bool patch::add_vertex_by_faces(vertex & p, const vertex & c, vertex & n, vector if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { + /*if(vertices[0] == 6016 || vertices[0] == 3925 || vertices[0] == 5108 || vertices[0] == 5428 ) + { + gproshan_debug_var(vertices[0]); + gproshan_debug_var(tmp_angle); + }*/ + angle = tmp_angle; //gproshan_debug_var(he); - area_face = mesh->area_trig(he/3); + area_face = mesh->area_trig(he/3); + idx_he = he/3; // compute projected area pav = va - vv + ( (n,vv) - (n,va) ) * n; pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; @@ -257,7 +264,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind } -void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres) +void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres) { radio = -INFINITY; @@ -282,6 +289,7 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ double proj_area = 0; double area_mesh = mesh->area_surface(); double ratio; + index_t idx_he; for(index_t i=1; iget_vertex(indexes[i]); if(*(p - c) > euc_radio) euc_radio = *(p - c); + idxs_he.push_back(idx_he); /* p = p - c ; p = p - ((p,n)*n); @@ -334,22 +343,24 @@ void patch::init_radial_disjoint(che * mesh, const real_t & radio_, const index_ if(vertices.size() > min_points) { jet_fit_directions(mesh, v); - n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); - radio = -INFINITY; + } + else + normal_fit_directions(mesh,v); - for(index_t i=1; i < vertices.size(); i++) - { - p = mesh->get_vertex(vertices[i]); - c = mesh->get_vertex(v); // central vertices + n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + radio = -INFINITY; - p = p - c ; - p = p - ((p,n)*n); + for(index_t i=1; i < vertices.size(); i++) + { + p = mesh->get_vertex(vertices[i]); + c = mesh->get_vertex(v); // central vertices - if(*p > radio) - { - radio = *p; - } + p = p - c ; + p = p - ((p,n)*n); + if(*p > radio) + { + radio = *p; } } From 3c8f58efc33884183a7a244b5a0a7b2722a3ce42 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 23 Apr 2020 15:39:07 +0200 Subject: [PATCH 0244/1018] adding area rest to the sampling --- include/mdict/inpainting.h | 3 ++- src/app_viewer.cpp | 18 +++++++++++------- src/mdict/inpainting.cpp | 21 +++++++++++---------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 54f4bedd..3f8b8523 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -19,7 +19,7 @@ class inpainting : public dictionary { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, - const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , const bool & _plot = false); + const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); virtual ~inpainting() = default; real_t execute(); @@ -39,6 +39,7 @@ class inpainting : public dictionary double delta; double sum_thres; bool * mask; + double area_thres; }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 41fd0a65..a9a02c19 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -612,19 +612,21 @@ bool app_viewer::process_inpaiting(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 0.001; + static float sum_thres = 1.001; + static float area_thres = 0.001; ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); + ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); + ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); dict.execute(); delete phi; mesh->update_colors(&dict[0]); @@ -647,24 +649,26 @@ bool app_viewer::process_mask(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 0.001; + static float sum_thres = 1.001; + static float area_thres = 0.001; ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); + ImGui::InputFloat("sum_thres", &sum_thres, 1.001, 0.1, 6); + ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); a_vec points_out; gproshan_debug_var(f_points); points_out.load(f_points); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index d09511c7..77ba9fd3 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -14,12 +14,13 @@ namespace gproshan::mdict { inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, - const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) + const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, double _area_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { delta = _delta; sum_thres = _sum_thres; avg_p = _avg_p; //size avg of number of vertices per patch percent = _perc; // mask percentage + area_thres = _area_thres; M = mesh->n_vertices()/avg_p; mask = new bool[mesh->n_vertices()]; #pragma omp for @@ -145,7 +146,7 @@ void inpainting::load_sampling(bool save_all) //double delta = PI/6; //double sum_thres = 0.008; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow //double sum_thres = PI; - string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".rsampl"); + string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".rsampl"); arma::mat S; @@ -158,7 +159,7 @@ void inpainting::load_sampling(bool save_all) gproshan_debug_var(d_time); gproshan_debug_var(all_sorted_features.size()); - string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); gproshan_debug_var(features.size()); @@ -209,7 +210,7 @@ void inpainting::load_sampling(bool save_all) const vertex & v_seed = mesh->gt(seeds[j]); // 0.5 coverage parameter - if( *(v_patch - v_seed) < 0.2* radios[j] ) + if( *(v_patch - v_seed) < 0.4* radios[j] ) { // radio of each patch found = true; @@ -329,7 +330,7 @@ void inpainting::load_sampling(bool save_all) //gproshan_debug_var(seeds[i]); } - + gproshan_debug_var(f_points); outlv.save(f_points); S.resize(seeds.size(),2); for(index_t i = 0; i < seeds.size(); i++) @@ -440,7 +441,7 @@ void inpainting::init_radial_feature_patches() AS(i,12) = p.T(2,2); } - string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".smp"); + string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); AS.save(f_samplall); } gproshan_log(radial patches are ready); @@ -558,7 +559,7 @@ real_t inpainting::execute() // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".alpha"); + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); save_alpha(f_alpha); //patches_map.resize(n_vertices); @@ -609,8 +610,8 @@ void inpainting::point_cloud_reconstruction() arma::mat alpha; - string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".smp"); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + ".alpha"); + string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) +'_' + to_string(area_thres) + ".alpha"); S.load(f_smp); alpha.load(f_alpha); @@ -676,7 +677,7 @@ void inpainting::point_cloud_reconstruction() che nmesh(point_cloud, total_points, nullptr, 0); gproshan_debug_var(sum_thres); - string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + "_pc"); + string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + "_pc"); che_off::write_file(&nmesh,f_pc); gproshan_debug(Done!); From 0ecb2710c6753e85132502791abfb6002a5749c5 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 23 Apr 2020 16:38:19 +0200 Subject: [PATCH 0245/1018] fixed adding area thres to the sampling restrictions --- include/mdict/patch.h | 3 ++- src/app_viewer.cpp | 2 +- src/mdict/inpainting.cpp | 17 +++++++++-------- src/mdict/patch.cpp | 4 ++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 811d1af6..2c190490 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -66,7 +66,8 @@ class patch real_t & euc_radio, real_t & geo_radio, double delta, - double sum_thres); + double sum_thres, + double area_thres); void init_random(vertex c, arma::mat T, real_t radio, real_t max_radio); void recover_radial_disjoint(che * mesh, const real_t & radio_, diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a9a02c19..685560d3 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -655,7 +655,7 @@ bool app_viewer::process_mask(viewer * p_view) ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 1.001, 0.1, 6); + ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 77ba9fd3..63063d57 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -225,7 +225,7 @@ void inpainting::load_sampling(bool save_all) patch p; // increasing a bit the radio idxs_he.clear(); - p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres); + p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres, area_thres); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; @@ -315,18 +315,19 @@ void inpainting::load_sampling(bool save_all) } gproshan_debug_var(outliers.size()); - /*for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices(); i++) { if(!covered[i] ) { outliers.push_back(i); } - }*/ - a_vec outlv(seeds.size() ); - gproshan_debug_var(seeds.size()); - for(index_t i = 0; i < seeds.size(); i++) + } + a_vec outlv(outliers.size() ); + //gproshan_debug_var(seeds.size()); + for(index_t i = 0; i < outliers.size(); i++) { - outlv(i) = seeds[i]; + //outlv(i) = seeds[i]; + outlv(i) = outliers[i]; //gproshan_debug_var(seeds[i]); } @@ -351,7 +352,7 @@ void inpainting::load_sampling(bool save_all) { patch p; //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); - p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres); + p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); patches.push_back(p); } M = n_seeds; diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 96a8c20b..2fa47c15 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -264,7 +264,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind } -void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres) +void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres, double area_thres) { radio = -INFINITY; @@ -314,7 +314,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re gproshan_debug_var(ratio);*/ // p es el vertice de la malla el maximo //(area/area_mesh) < 0.001 - if( add_vertex_by_faces(idx_he, p, c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < 0.000 ) ) + if( add_vertex_by_faces(idx_he, p, c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) { //compute euclidean radio //p = mesh->get_vertex(indexes[i]); From cf15da72b96bb6989087ecf34e830ce480dced32 Mon Sep 17 00:00:00 2001 From: Lish Date: Fri, 1 May 2020 12:21:36 +0200 Subject: [PATCH 0246/1018] fixed recover sampling --- src/app_viewer.cpp | 10 ++++++---- src/mdict/inpainting.cpp | 13 ++++++++++++- src/mdict/patch.cpp | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 685560d3..926a4aad 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -694,24 +694,26 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 0.001; + static float sum_thres = 1.001; + static float area_thres = 0.001; ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("sum_thres", &sum_thres, 0.001, 0.1, 6); + ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); + ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); dict.point_cloud_reconstruction(); //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); a_vec points_out; points_out.load(f_points); for(int i = 0; i< points_out.size(); i++) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 63063d57..eece2562 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -345,19 +345,30 @@ void inpainting::load_sampling(bool save_all) } else { + gproshan_debug(already done); size_t n_seeds = S.n_rows; vector idxs_he; real_t euc_radio, geo_radio; for(index_t i = 0; i < n_seeds; i++) { patch p; + //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); patches.push_back(p); } + M = n_seeds; + string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); gproshan_debug_var(n_seeds); - + a_vec outlv(n_seeds); + gproshan_debug(restored); + for(index_t i = 0; i < n_seeds; i++) + { + outlv(i) = S(i,0); + } + outlv.save(f_points); + gproshan_debug(restored); } } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2fa47c15..0c778c9a 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -197,14 +197,14 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio) // free the parameters to the interface // fix the point cloud viewer - size_t n_points = (radio/max_radio) * 50; // change this using a sigmoid function + size_t n_points = (radio/max_radio) * 100; // change this using a sigmoid function vertices.resize(n_points); xyz.resize(3,n_points); for(size_t i=0 ;i Date: Sat, 2 May 2020 16:36:42 +0200 Subject: [PATCH 0247/1018] fixing point cloud sampling --- include/mdict/inpainting.h | 2 +- include/mdict/patch.h | 2 +- src/app_viewer.cpp | 8 ++++++-- src/mdict/inpainting.cpp | 7 ++++--- src/mdict/patch.cpp | 16 ++++++++++------ 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 3f8b8523..a7431c14 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -28,7 +28,7 @@ class inpainting : public dictionary void init_voronoi_patches(); void init_radial_feature_patches(); void load_sampling(bool save_all); - void point_cloud_reconstruction(); + void point_cloud_reconstruction(real_t per, real_t fr); vector sort_indexes(const vector &v); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 2c190490..99b27694 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -68,7 +68,7 @@ class patch double delta, double sum_thres, double area_thres); - void init_random(vertex c, arma::mat T, real_t radio, real_t max_radio); + void init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr); void recover_radial_disjoint(che * mesh, const real_t & radio_, const index_t & v); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 926a4aad..48cec8d4 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -695,13 +695,17 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static int percentage = 0; static float delta = PI/6; static float sum_thres = 1.001; - static float area_thres = 0.001; + static float area_thres = 0.001; + static float percentage_size = 100; + static float radio_factor = 1; ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); + ImGui::InputFloat("percentage_size", &percentage_size, 100, 10, 6); + ImGui::InputFloat("radio_factor", &radio_factor, 1, 0.1, 6); ImGui::Checkbox("learn", &learn); if(ImGui::Button("Run")) @@ -709,7 +713,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) basis * phi = new basis_dct(n); inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); - dict.point_cloud_reconstruction(); + dict.point_cloud_reconstruction(percentage_size,radio_factor); //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index eece2562..f24a55d1 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -615,7 +615,7 @@ real_t inpainting::execute() gproshan_debug_var(d_time); } -void inpainting::point_cloud_reconstruction() +void inpainting::point_cloud_reconstruction(real_t per, real_t fr) { arma::mat S; arma::mat T(3,3); @@ -656,7 +656,7 @@ void inpainting::point_cloud_reconstruction() T(1,2) = S(i,11); T(2,2) = S(i,12); - patches[i].init_random(c, T, radio, max_radio); + patches[i].init_random(c, T, radio, max_radio, per, fr); total_points += patches[i].vertices.size(); patches[i].phi.set_size(patches[i].vertices.size(), phi_basis->get_dim()); phi_basis->discrete(patches[i].phi, patches[i].xyz); @@ -689,7 +689,8 @@ void inpainting::point_cloud_reconstruction() che nmesh(point_cloud, total_points, nullptr, 0); gproshan_debug_var(sum_thres); - string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + "_pc"); + string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); che_off::write_file(&nmesh,f_pc); gproshan_debug(Done!); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 0c778c9a..f921fce4 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -180,7 +180,7 @@ bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, N.push_back(min_he); return added; } -void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio) +void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr) { this->T = T; x.resize(3); @@ -192,19 +192,23 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio) std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() - // std::uniform_real_distribution<> dis(0, 1); - std::normal_distribution dis(0,1); + std::uniform_real_distribution<> dis(0, 1); + //std::normal_distribution dis(0,0.5); // free the parameters to the interface // fix the point cloud viewer - size_t n_points = (radio/max_radio) * 100; // change this using a sigmoid function + size_t n_points = (radio/max_radio) * per; // change this using a sigmoid function vertices.resize(n_points); xyz.resize(3,n_points); - for(size_t i=0 ;i Date: Mon, 11 May 2020 21:29:55 +0200 Subject: [PATCH 0248/1018] cmake embree 3.10, embree_FOUND --- CMakeLists.txt | 9 ++++----- src/viewer/viewer.cpp | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5cffe01..3cecf99d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,11 +26,10 @@ if(CUDA_FOUND) endif(CUDA_FOUND) -find_package(Embree 3.7) -if(EMBREE_LIBRARY) +find_package(embree 3) +if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) - include_directories(${EMBREE_INCLUDE_DIRS}) -endif(EMBREE_LIBRARY) +endif(embree_FOUND) find_package(Threads REQUIRED) @@ -96,7 +95,7 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp ${EMBREE_LIBRARY}) +target_link_libraries(gproshan_cpp embree) target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index faffe346..8340b743 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -679,7 +679,7 @@ void viewer::render_optix() action = false; if(!render_frame) render_frame = new frame; - render_frame->display(viewport_width, viewport_height, rt_embree->img); + render_frame->display(viewport_width, viewport_height, rt_optix->img); #endif // GPROSHAN_OPTIX } From cd0b165df7180b6c25b7262cb02ad11e704879a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 12 May 2020 11:41:59 +0200 Subject: [PATCH 0249/1018] cmake found embree, embree_LIBRARY --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cecf99d..c23668f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ endif(CUDA_FOUND) find_package(embree 3) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) + set(embree_LIBRARY embree) endif(embree_FOUND) @@ -95,7 +96,7 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp embree) +target_link_libraries(gproshan_cpp ${embree_LIBRARY}) target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) From ffce489ad1ea8febca5fbc733ce764ce68130dc8 Mon Sep 17 00:00:00 2001 From: Lish Date: Fri, 22 May 2020 17:21:44 +0200 Subject: [PATCH 0250/1018] cleaning --- src/geodesics.cpp | 1 + src/mdict/dictionary.cpp | 15 ++++++++++++++- src/mdict/inpainting.cpp | 14 ++++++++------ src/mdict/mdict.cpp | 3 +++ src/mdict/patch.cpp | 7 ++++++- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/geodesics.cpp b/src/geodesics.cpp index 614765d6..5f4eb009 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -43,6 +43,7 @@ geodesics::~geodesics() const real_t & geodesics::operator[](const index_t & i) const { + assert(i < n_vertices); return dist[i]; } diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 9329f077..58af3033 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -67,12 +67,25 @@ void dictionary::learning() if(!A.load(f_dict)) { - A.eye(phi_basis->dim, m); + //A.eye(phi_basis->dim, m); + //initialize with some alpha + //random + //arma::uvec r_ind = arma::randi(m, arma::distr_param(0, M)); + //A = alpha.cols(r_ind); + a_mat R, E, U, V; + a_vec s; + svd(U, s, V, alpha); + gproshan_debug(svd done!); + A = U.cols(0,m); + gproshan_debug(svd done!); A = normalise(A); gproshan_debug_var(phi_basis->radio); gproshan_debug_var(m); + // + phi_basis->plot_atoms(A); KSVD(A, patches, L, K); + phi_basis->plot_atoms(A); A.save(f_dict); } } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index f24a55d1..49655b29 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -23,11 +23,7 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size area_thres = _area_thres; M = mesh->n_vertices()/avg_p; mask = new bool[mesh->n_vertices()]; - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - mask[i] = 0; - } + memset(mask,0,sizeof(bool)*mesh->n_vertices()); } @@ -281,6 +277,7 @@ void inpainting::load_sampling(bool save_all) /////////////////////////////////////// + #ifndef NDEBUG gproshan_debug_var(M); index_t tmp; bool remark[mesh->n_vertices()] = {}; @@ -330,6 +327,7 @@ void inpainting::load_sampling(bool save_all) outlv(i) = outliers[i]; //gproshan_debug_var(seeds[i]); } + #endif // NDEBUG gproshan_debug_var(f_points); outlv.save(f_points); @@ -352,7 +350,7 @@ void inpainting::load_sampling(bool save_all) for(index_t i = 0; i < n_seeds; i++) { patch p; - + // gproshan_debug_var(i); //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); patches.push_back(p); @@ -565,6 +563,10 @@ real_t inpainting::execute() gproshan_debug_var(d_time); // L = 15; + // sparse coding and reconstruction with all patches + TIC(d_time) sparse_coding(); TOC(d_time) + gproshan_debug_var(d_time); + TIC(d_time) learning(); TOC(d_time) gproshan_debug_var(d_time); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 9d60e4c9..12a10406 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -310,6 +310,9 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) sum += aj * aj * patches[i].phi.t() * patches[i].phi; sum_error += aj * patches[i].phi.t() * e; + //concat e patches[i].phi.t() * e; + //apply svd to update the atom + } if(omega.size()) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f921fce4..68f05cd0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -98,6 +98,10 @@ bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, vb = mesh->gt(b); vv = mesh->gt(v); // If is an adjacent face + assert(a < mesh->n_vertices()); + assert(b < mesh->n_vertices()); + assert(v < mesh->n_vertices()); + if( geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) @@ -294,10 +298,11 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re double area_mesh = mesh->area_surface(); double ratio; index_t idx_he; + for(index_t i=1; i radio_) break; + if (indexes[i] >= mesh->n_vertices() || geo[indexes [i]] > radio_) break; c = mesh->get_vertex(v); // central vertices From 4add0fb3dac3fdf3304ce6f47fcf620a4c224c7f Mon Sep 17 00:00:00 2001 From: Lish Date: Fri, 22 May 2020 18:28:42 +0200 Subject: [PATCH 0251/1018] adding vertex by vertex --- include/mdict/patch.h | 17 ++++++++-- src/mdict/inpainting.cpp | 15 +-------- src/mdict/patch.cpp | 69 +++------------------------------------- 3 files changed, 19 insertions(+), 82 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 99b27694..920a758c 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -60,7 +60,8 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); - void init_radial_disjoint(vector & idxs_he, che * mesh, + void init_radial_disjoint(vector & idxs_he, + che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, @@ -97,8 +98,18 @@ class patch void compute_avg_distance(); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(index_t & idx_he, vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, - const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation); + bool add_vertex_by_faces(const vertex & c, + vertex & n, + vector & N, + index_t * indexes, + size_t nc, + double thr_angle, + const geodesics & geo, + che * mesh, + const index_t & v, + double & area, + double & proj_area, + double deviation); private: diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 49655b29..75b99af4 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -251,20 +251,7 @@ void inpainting::load_sampling(bool save_all) } } - /*if(all_sorted_features[i] == 10594 || all_sorted_features[i] == 3923 || all_sorted_features[i] == 9149 || all_sorted_features[i] == 5678 || - all_sorted_features[i] == 3067 || all_sorted_features[i] == 6203 || all_sorted_features[i] == 3238 || all_sorted_features[i] == 2870 || - all_sorted_features[i] == 699 || all_sorted_features[i] == 11532 || all_sorted_features[i] == 10749 || all_sorted_features[i] == 3989 || - all_sorted_features[i] == 1563 || all_sorted_features[i] == 7471 || all_sorted_features[i] == 2874 || all_sorted_features[i] == 8981 || - all_sorted_features[i] == 8678 || all_sorted_features[i] == 3008 || all_sorted_features[i] == 2689 ||all_sorted_features[i] == 5118 || - all_sorted_features[i] == 6368 || all_sorted_features[i] == 11880 || all_sorted_features[i] == 9617 || all_sorted_features[i] == 11882 || - all_sorted_features[i] == 1479 || all_sorted_features[i] == 8848 || all_sorted_features[i] == 8592 || all_sorted_features[i] == 2688 ) - { - gproshan_debug_var(all_sorted_features[i]); - gproshan_debug_var(count_cov_patch); - gproshan_debug_var(p.vertices.size()); - }*/ - - + } } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 68f05cd0..845a2217 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -74,14 +74,14 @@ index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) if(indexes[i] == idx_global) return i; return -1; } -bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, +bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { // it needs to return both vertices // it needs to filter repeated indexes. // p should be the maximun - index_t a, b, i = 0, ma, mb; + index_t a, b, i = 0; vertex min_he; double area_face = 0, proj_area_face = 0; double angle = PI; @@ -117,16 +117,10 @@ bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { - /*if(vertices[0] == 6016 || vertices[0] == 3925 || vertices[0] == 5108 || vertices[0] == 5428 ) - { - gproshan_debug_var(vertices[0]); - gproshan_debug_var(tmp_angle); - }*/ angle = tmp_angle; //gproshan_debug_var(he); area_face = mesh->area_trig(he/3); - idx_he = he/3; // compute projected area pav = va - vv + ( (n,vv) - (n,va) ) * n; pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; @@ -134,8 +128,6 @@ bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, min_he = mesh->normal_he(he); if( !exists(v) ) vertices.push_back(v); - ma = a; - mb = b; added = true; } @@ -146,40 +138,9 @@ bool patch::add_vertex_by_faces( index_t & idx_he, vertex & p, const vertex & c, //p = p - c ; //p = p - ((p,n)*n); - if(added) - { - //gproshan_debug_var(ma); - //gproshan_debug_var(mb); - - if( !exists(ma) ) vertices.push_back(ma); - if( !exists(mb) ) vertices.push_back(mb); - - vertex pa, pb, mab; - pa = mesh->get_vertex(ma); - pb = mesh->get_vertex(mb); - - pa = pa - c; - pb = pb - c; - - pa = pa - ((pa,n)*n); - pb = pb - ((pb,n)*n); - if( *pa > *pb) mab = mesh->get_vertex(ma); - else - mab = mesh->get_vertex(mb); - - p = mesh->get_vertex(v); - p = p - c ; - p = p - ((p,n)*n); - - if(*mab > *p) p = mab; - - // get the one who has the greatest distance - } area += area_face; proj_area += proj_area_face; -// sum += (proj_area_face/area_face); - //sum += acos( (min_he, N[i]) ); N.push_back(min_he); return added; @@ -306,37 +267,15 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re c = mesh->get_vertex(v); // central vertices - - /*p = mesh->get_vertex(indexes[i]); - p = p - c ; - p = p - ((p,n)*n); - */ - - //if( angle < PI/2.5 && (sum_angle) <= delta * PI) - - // add one new candidate vertex //first regulates variation, // second regulates size of the patch - - //if( add_vertex_by_faces(n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], sum_angle, PI/2.5 ) && sum_angle/area_mesh < sum_thres ) ratio = (i==1)? 0:(area/proj_area); - /*gproshan_debug_var(proj_area); - gproshan_debug_var(area); - gproshan_debug_var(ratio);*/ - // p es el vertice de la malla el maximo - //(area/area_mesh) < 0.001 - if( add_vertex_by_faces(idx_he, p, c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) + if( add_vertex_by_faces(c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) { //compute euclidean radio - //p = mesh->get_vertex(indexes[i]); + p = mesh->get_vertex(indexes[i]); if(*(p - c) > euc_radio) euc_radio = *(p - c); idxs_he.push_back(idx_he); - /* p = p - c ; - p = p - ((p,n)*n); - if(*p > radio) - { - radio = *p; - }*/ } else { From 33eb20e110e8a0dcc7b8db6fa16333c93254d7e2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 28 May 2020 11:32:34 +0200 Subject: [PATCH 0252/1018] adding kdtree --- include/kdtree.h | 28 ++++++++++++++++++++++++++++ src/che.cpp | 9 +++++++++ src/kdtree.cpp | 34 ++++++++++++++++++++++++++++++++++ src/raytracing/rt_embree.cpp | 5 +++++ 4 files changed, 76 insertions(+) create mode 100644 include/kdtree.h create mode 100644 src/kdtree.cpp diff --git a/include/kdtree.h b/include/kdtree.h new file mode 100644 index 00000000..9a169f7b --- /dev/null +++ b/include/kdtree.h @@ -0,0 +1,28 @@ +#ifndef KDTREE_H +#define KDTREE_H + +#include "vertex.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class kdtree +{ + private: + index_t * nodes = nullptr; + + public: + kdtree(const vertex * pointcloud, const size_t & n_points); + virtual ~kdtree(); + + private: + void build(const index_t & n, const vertex * pc, const index_t & i, const index_t & j, const index_t & d); +}; + + +} // namespace gproshan + +#endif // KDTREE_H + diff --git a/src/che.cpp b/src/che.cpp index 38a20c68..b320b6b2 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -1,5 +1,6 @@ #include "che.h" +#include "kdtree.h" #include "include_arma.h" #include @@ -279,6 +280,14 @@ void che::update_normals() { if(!VN) VN = new vertex[n_vertices_]; + // point cloud normals + if(!n_faces_) + { + kdtree rnn(GT, n_vertices_); + + return; + } + #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) { diff --git a/src/kdtree.cpp b/src/kdtree.cpp new file mode 100644 index 00000000..d08e45d3 --- /dev/null +++ b/src/kdtree.cpp @@ -0,0 +1,34 @@ +#include "kdtree.h" + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +kdtree::kdtree(const vertex * pointcloud, const size_t & n_points) +{ + nodes = new index_t[n_points >> 1]; + + build(0, pointcloud, 0, n_points, 0); +} + +kdtree::~kdtree() +{ + delete [] nodes; +} + +void kdtree::build(const index_t & n, const vertex * pc, const index_t & i, const index_t & j, const index_t & d) +{ + + if(i == j) + { + nodes[i] = i; + return; + } +} + + +} // namespace gproshan + diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 54d851e8..c6b5ed7d 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -12,6 +12,11 @@ namespace gproshan::rt { +void compute_normals(glm::vec3 * normals, const glm::vec3 * vertices, const size_t & n_vertices) +{ + +} + void embree_error(void * ptr, RTCError error, const char * str) { fprintf(stderr, "EMBREE ERROR: %s\n", str); From 9f5031a4d9bdccadc66397bb2d3d0538f67d65ec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 2 Jun 2020 09:48:05 +0200 Subject: [PATCH 0253/1018] fix path harris3d --- src/mdict/dictionary.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 58af3033..d212c07b 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -219,7 +219,7 @@ void dictionary::load_features(vector & v_feat, size_t & featsize) //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - string command = "../../Harris3D-Cpp/harris3d " + tmp_file_path(mesh->name()) + ".off" + " ../tmp/example.prop"; + string command = "../../Harris3D-Cpp/harris3d " + mesh->filename() + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); system(command.c_str()); gproshan_debug(created); From 8f7b1a57dd334baf37d29a3568e567d5c048e77b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 2 Jun 2020 09:56:23 +0200 Subject: [PATCH 0254/1018] cleaning up spaces --- include/mdict/d_mesh.h | 2 +- include/mdict/inpainting.h | 2 +- include/mdict/patch.h | 2 +- src/app_viewer.cpp | 10 ++++----- src/mdict/d_hybrid_denoising.cpp | 2 +- src/mdict/d_mesh.cpp | 2 +- src/mdict/dictionary.cpp | 14 ++++++------- src/mdict/inpainting.cpp | 36 ++++++++++++++++---------------- src/mdict/patch.cpp | 30 +++++++++++++------------- src/viewer/frame.cpp | 4 ++-- 10 files changed, 52 insertions(+), 52 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index 8ed0d99b..fecdedd9 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,7 +111,7 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, real_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); +real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, real_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index a7431c14..4a5f47f5 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -19,7 +19,7 @@ class inpainting : public dictionary { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, - const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); + const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); virtual ~inpainting() = default; real_t execute(); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 920a758c..769d5411 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -11,7 +11,7 @@ #include #ifdef Success - #undef Success + #undef Success #endif using namespace cimg_library; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 48cec8d4..4d161e08 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -626,7 +626,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); dict.execute(); delete phi; mesh->update_colors(&dict[0]); @@ -662,13 +662,13 @@ bool app_viewer::process_mask(viewer * p_view) if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); a_vec points_out; gproshan_debug_var(f_points); points_out.load(f_points); @@ -711,13 +711,13 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) if(ImGui::Button("Run")) { basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); + inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); dict.point_cloud_reconstruction(percentage_size,radio_factor); //dict.init_voronoi_patches(); delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); a_vec points_out; points_out.load(f_points); for(int i = 0; i< points_out.size(); i++) diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp index d28a4e5a..5498b536 100644 --- a/src/mdict/d_hybrid_denoising.cpp +++ b/src/mdict/d_hybrid_denoising.cpp @@ -82,7 +82,7 @@ void test_hybrid_denoising(const string & file) if(rp.phi.n_rows) { a_vec x = rp.phi * A * alpha.col(p); - gproshan_debug_var(alpha.col(p)); + gproshan_debug_var(alpha.col(p)); rp.xyz.row(2) = x.t(); // rp.itransform(); } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index cd223488..4d852409 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -1,5 +1,5 @@ #include "d_mesh.h" - #include "che_off.h" + #include "che_off.h" #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index d212c07b..cc8879c5 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -76,7 +76,7 @@ void dictionary::learning() a_vec s; svd(U, s, V, alpha); gproshan_debug(svd done!); - A = U.cols(0,m); + A = U.cols(0,m); gproshan_debug(svd done!); A = normalise(A); gproshan_debug_var(phi_basis->radio); @@ -136,8 +136,8 @@ void dictionary::init_sampling() void dictionary::load_curvatures(a_vec & curvatures) { - string f_curv = tmp_file_path(mesh->name_size() + ".curv"); - string f_norm = tmp_file_path(mesh->name_size() + ".n"); + string f_curv = tmp_file_path(mesh->name_size() + ".curv"); + string f_norm = tmp_file_path(mesh->name_size() + ".n"); if(! curvatures.load(f_curv)) { @@ -205,15 +205,15 @@ void dictionary::load_curvatures(a_vec & curvatures) void dictionary::load_features(vector & v_feat, size_t & featsize) { - string f_feat = tmp_file_path(mesh->name() + ".int"); + string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; - inp.open(f_feat.c_str(), ifstream::in); - + inp.open(f_feat.c_str(), ifstream::in); + size_t tam; index_t tmp; gproshan_debug_var(f_feat); - if(inp.fail()){ + if(inp.fail()){ inp.clear(ios::failbit); // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 75b99af4..4dc3d64d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -14,7 +14,7 @@ namespace gproshan::mdict { inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, - const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, double _area_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) + const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, double _area_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) { delta = _delta; sum_thres = _sum_thres; @@ -29,8 +29,8 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size void inpainting::load_mask() { - //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(percent) + ".msk"); + //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -71,7 +71,7 @@ void inpainting::load_mask() void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) { - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); arma::uvec V; if(V.load(f_mask)) @@ -142,7 +142,7 @@ void inpainting::load_sampling(bool save_all) //double delta = PI/6; //double sum_thres = 0.008; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow //double sum_thres = PI; - string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".rsampl"); + string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".rsampl"); arma::mat S; @@ -155,11 +155,11 @@ void inpainting::load_sampling(bool save_all) gproshan_debug_var(d_time); gproshan_debug_var(all_sorted_features.size()); - string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); gproshan_debug_var(features.size()); - geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); + geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; @@ -191,7 +191,7 @@ void inpainting::load_sampling(bool save_all) size_t count_cov_patch = 0; bool faces[mesh->n_faces()] = {}; - vector idxs_he; + vector idxs_he; for(size_t i = 0; i < all_sorted_features.size(); i++) { @@ -344,7 +344,7 @@ void inpainting::load_sampling(bool save_all) } M = n_seeds; - string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); gproshan_debug_var(n_seeds); a_vec outlv(n_seeds); gproshan_debug(restored); @@ -358,7 +358,7 @@ void inpainting::load_sampling(bool save_all) } } -void inpainting::init_radial_feature_patches() +void inpainting::init_radial_feature_patches() { load_sampling(false); @@ -398,7 +398,7 @@ void inpainting::init_radial_feature_patches() bool * pmask = mask; for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); gproshan_debug(passed); @@ -438,7 +438,7 @@ void inpainting::init_radial_feature_patches() AS(i,12) = p.T(2,2); } - string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); + string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); AS.save(f_samplall); } gproshan_log(radial patches are ready); @@ -560,11 +560,11 @@ real_t inpainting::execute() // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); save_alpha(f_alpha); //patches_map.resize(n_vertices); - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; i++) { patches_map[i].clear(); } @@ -611,8 +611,8 @@ void inpainting::point_cloud_reconstruction(real_t per, real_t fr) arma::mat alpha; - string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) +'_' + to_string(area_thres) + ".alpha"); + string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); + string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) +'_' + to_string(area_thres) + ".alpha"); S.load(f_smp); alpha.load(f_alpha); @@ -678,8 +678,8 @@ void inpainting::point_cloud_reconstruction(real_t per, real_t fr) che nmesh(point_cloud, total_points, nullptr, 0); gproshan_debug_var(sum_thres); - string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) - + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); + string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); che_off::write_file(&nmesh,f_pc); gproshan_debug(Done!); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 845a2217..841bcf09 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -58,7 +58,7 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev if(!_toplevel) delete [] toplevel; // If it is null } -bool patch::exists(index_t idx) +bool patch::exists(index_t idx) { for(size_t i=1; i < vertices.size(); i++) { @@ -115,15 +115,15 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N } tmp_angle = acos( (mesh->normal_he(he), N[i]) ); - if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions + if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions { angle = tmp_angle; //gproshan_debug_var(he); area_face = mesh->area_trig(he/3); // compute projected area - pav = va - vv + ( (n,vv) - (n,va) ) * n; - pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; + pav = va - vv + ( (n,vv) - (n,va) ) * n; + pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; proj_area_face = *(pav * pbv) / 2; min_he = mesh->normal_he(he); @@ -155,9 +155,9 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, r this->radio = radio; - std::random_device rd; //Will be used to obtain a seed for the random number engine - std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() - std::uniform_real_distribution<> dis(0, 1); + std::random_device rd; //Will be used to obtain a seed for the random number engine + std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() + std::uniform_real_distribution<> dis(0, 1); //std::normal_distribution dis(0,0.5); // free the parameters to the interface @@ -192,7 +192,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind { // for small meshes 6000 0.e-5 // for others 2.e-5 - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 1.e-5); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 1.e-5); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -209,7 +209,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind if(vertices.size() > min_points) { jet_fit_directions(mesh, v); - n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); radio = -INFINITY; for(index_t i=1; i < vertices.size(); i++) @@ -240,7 +240,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re normal_fit_directions(mesh, v); - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, 1.2 * radio_); + geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, 1.2 * radio_); index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -295,7 +295,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re else normal_fit_directions(mesh,v); - n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); radio = -INFINITY; for(index_t i=1; i < vertices.size(); i++) @@ -365,14 +365,14 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } -void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); if(mask) { m = 0; for(index_t i = 0; i < vertices.size(); i++) - if(mask(i)) { dist[vertices[i] ] = float(p + 1) / M; m++; } else {dist[vertices[i] ] = INFINITY; }; + if(mask(i)) { dist[vertices[i] ] = float(p + 1) / M; m++; } else {dist[vertices[i] ] = INFINITY; }; /*gproshan_debug(number vertices considered); gproshan_debug_var(m); gproshan_debug(number vertices masked); @@ -380,7 +380,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector Date: Wed, 3 Jun 2020 11:10:42 +0200 Subject: [PATCH 0255/1018] cleaning up pc reconstruction --- include/mdict/inpainting.h | 2 +- shaders/vertex_pointcloud.glsl | 2 +- src/app_viewer.cpp | 16 +++------------- src/mdict/dictionary.cpp | 5 +++-- src/mdict/inpainting.cpp | 9 +++++---- 5 files changed, 13 insertions(+), 21 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 4a5f47f5..0741f17f 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -28,7 +28,7 @@ class inpainting : public dictionary void init_voronoi_patches(); void init_radial_feature_patches(); void load_sampling(bool save_all); - void point_cloud_reconstruction(real_t per, real_t fr); + che * point_cloud_reconstruction(real_t per, real_t fr); vector sort_indexes(const vector &v); diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index ea4dc026..0d104841 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -18,6 +18,6 @@ void main() vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); - gl_PointSize = 2; + gl_PointSize = 1; } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 4d161e08..3a730507 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -710,20 +710,10 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) if(ImGui::Button("Run")) { - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); + basis_dct phi(n); + inpainting dict(mesh, &phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); - dict.point_cloud_reconstruction(percentage_size,radio_factor); - //dict.init_voronoi_patches(); - delete phi; - mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); - a_vec points_out; - points_out.load(f_points); - for(int i = 0; i< points_out.size(); i++) - mesh.selected.push_back(points_out(i)); - - mesh->update_normals(); + view->add_mesh({dict.point_cloud_reconstruction(percentage_size,radio_factor)}); } return true; diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index cc8879c5..5bfe77b3 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -213,13 +213,14 @@ void dictionary::load_features(vector & v_feat, size_t & featsize) index_t tmp; gproshan_debug_var(f_feat); - if(inp.fail()){ + if(inp.fail()) + { inp.clear(ios::failbit); // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - string command = "../../Harris3D-Cpp/harris3d " + mesh->filename() + " " + tmp_file_path("example.prop"); + string command = "../../harris3d/harris3d " + mesh->filename() + " " + f_feat + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); system(command.c_str()); gproshan_debug(created); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 4dc3d64d..d5e816a2 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -604,7 +604,7 @@ real_t inpainting::execute() gproshan_debug_var(d_time); } -void inpainting::point_cloud_reconstruction(real_t per, real_t fr) +che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) { arma::mat S; arma::mat T(3,3); @@ -676,14 +676,15 @@ void inpainting::point_cloud_reconstruction(real_t per, real_t fr) } } - che nmesh(point_cloud, total_points, nullptr, 0); + che * nmesh = new che(point_cloud, total_points, nullptr, 0); gproshan_debug_var(sum_thres); string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); - che_off::write_file(&nmesh,f_pc); + che_off::write_file(nmesh, f_pc); gproshan_debug(Done!); - + + return nmesh; } From 5dd4f52d2dbb41a2475e682a67b0a43b87aa8883 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 3 Jun 2020 11:54:34 +0200 Subject: [PATCH 0256/1018] pc reconstruction --- src/app_viewer.cpp | 8 ++++---- src/mdict/inpainting.cpp | 23 +++++++++++------------ src/mdict/patch.cpp | 20 +++++++------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 3a730507..928e1f23 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -612,8 +612,8 @@ bool app_viewer::process_inpaiting(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 1.001; - static float area_thres = 0.001; + static float sum_thres = 1.01; + static float area_thres = 0.005; ImGui::InputInt("basis", &n); @@ -694,8 +694,8 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 1.001; - static float area_thres = 0.001; + static float sum_thres = 1.01; + static float area_thres = 0.005; static float percentage_size = 100; static float radio_factor = 1; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index d5e816a2..4a3a56d7 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -664,19 +664,18 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) patches[i].itransform(); } - vertex point_cloud[total_points]; - for(index_t i = 0, k=0; i < M; i++) - { - for(index_t j = 0; j < patches[i].vertices.size(); j++) - { - point_cloud[k].x = patches[i].xyz(0,j); - point_cloud[k].y = patches[i].xyz(1,j); - point_cloud[k].z = patches[i].xyz(2,j); - k++; - } - } + vector point_cloud; + point_cloud.reserve(total_points); + + for(index_t i = 0; i < M; i++) + for(index_t j = 0; j < patches[i].vertices.size(); j++) + point_cloud.push_back({ patches[i].xyz(0, j), + patches[i].xyz(1, j), + patches[i].xyz(2, j) + }); + + che * nmesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); - che * nmesh = new che(point_cloud, total_points, nullptr, 0); gproshan_debug_var(sum_thres); string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 841bcf09..6d32ef16 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -145,6 +145,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N N.push_back(min_he); return added; } + void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr) { this->T = T; @@ -170,24 +171,17 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, r xyz(1, 0) = 0; xyz(2, 0) = 0; - for(size_t i=1 ;i Date: Wed, 3 Jun 2020 16:15:56 +0200 Subject: [PATCH 0257/1018] fixing radio local scaling --- src/mdict/inpainting.cpp | 12 +++++++++--- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 4a3a56d7..bafa632d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -588,8 +588,8 @@ real_t inpainting::execute() bool *pmask; - draw_patches(10); - draw_patches(50); + draw_patches(295); + draw_patches(384); draw_patches(90); draw_patches(20); @@ -653,6 +653,12 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) a_vec x = patches[i].phi * A * alpha.col(i); patches[i].xyz.row(2) = x.t(); + + for(index_t j = 0; j < patches[i].vertices.size(); j++) + if (patches[i].xyz(2, j) > 2) + gproshan_debug_var( i); + + } gproshan_debug_var(total_points); @@ -660,7 +666,7 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) for(index_t i = 0; i < M; i++) { - patches[i].iscale_xyz(patches[i].radio); + patches[i].iscale_xyz(phi_basis->get_radio()); patches[i].itransform(); } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 12a10406..37f84943 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0.8 * p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 6d32ef16..2d3e4ef2 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -174,7 +174,7 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, r for(size_t i = 1; i < n_points; i++) { double a = abs(dis(gen)) * 2 * PI; - double r = fr * radio * abs(dis(gen)); + double r = fr * abs(dis(gen)); xyz(0, i) = r * cos(a); xyz(1, i) = r * sin(a); From 51ea8c905e5c647e52fb75c5cc7b7aa07bcefdfa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 4 Jun 2020 12:02:44 +0200 Subject: [PATCH 0258/1018] update cmake embree library --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c23668f6..cff398ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,11 +26,11 @@ if(CUDA_FOUND) endif(CUDA_FOUND) -find_package(embree 3) -if(embree_FOUND) +find_package(EMBREE 3) +if(EMBREE_FOUND) add_definitions(-DGPROSHAN_EMBREE) - set(embree_LIBRARY embree) -endif(embree_FOUND) + include_directories(${EMBREE_INCLUDE_DIRS}) +endif(EMBREE_FOUND) find_package(Threads REQUIRED) @@ -96,7 +96,7 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp ${embree_LIBRARY}) +target_link_libraries(gproshan_cpp ${EMBREE_LIBRARY}) target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) From 3fa264fcacdd06401fe3fd9880f24d503ccca837 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 15:43:12 +0200 Subject: [PATCH 0259/1018] cleaning up viewer --- include/viewer/viewer.h | 33 --------------------------------- src/app_viewer.cpp | 7 ++----- 2 files changed, 2 insertions(+), 38 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 0cd7708e..4596aa15 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -37,38 +37,6 @@ namespace gproshan { class viewer; -struct vcorr_t -{ - index_t mesh_i; - corr_t * corr; - - vcorr_t() - { - mesh_i = NIL; - corr = nullptr; - } - - void init(const size_t & n, const index_t & _mesh_i, const corr_t * _corr) - { - if(corr) delete [] corr; - corr = new corr_t[n]; - - mesh_i = _mesh_i; - memcpy(corr, _corr, n * sizeof(corr_t)); - } - - operator corr_t *& () - { - return corr; - } - - bool is_loaded() - { - return mesh_i != NIL && corr != nullptr; - } -}; - - class viewer { protected: @@ -111,7 +79,6 @@ class viewer glm::mat4 proj_mat; che_viewer meshes[N_MESHES]; - vcorr_t corr_mesh[N_MESHES]; size_t n_meshes = 0; index_t current = 0; // current mesh diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 01232d9f..bdbba161 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -984,12 +984,9 @@ bool app_viewer::process_edge_collapse(viewer * p_view) TIC(view->time) decimation sampling(mesh, &mesh->normal(0), levels); TOC(view->time) gproshan_debug_var(view->time); - if(view->n_meshes < 2) - view->add_mesh({new che(*mesh)}); + //if(view->n_meshes < 2) + // view->add_mesh({new che(*mesh)}); - view->corr_mesh[1].init(view->meshes[1]->n_vertices(), view->current, sampling); - view->current = 1; - return false; } From d4b0c44d7595bd1bf3b655afc1fcb987c134a56f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 16:14:30 +0200 Subject: [PATCH 0260/1018] viewer add mesh --- include/viewer/viewer.h | 2 +- src/app_viewer.cpp | 7 ++----- src/viewer/che_viewer.cpp | 1 + src/viewer/viewer.cpp | 20 +++++++++++--------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 4596aa15..cff8fd9e 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -126,7 +126,7 @@ class viewer che_viewer & mesh(); void add_process(const int & key, const process_t & process); - void add_mesh(const std::vector & _meshes); + void add_mesh(che * p_mesh); private: diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index bdbba161..88fb9112 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -48,18 +48,14 @@ int app_viewer::main(int nargs, const char ** args) TIC(time) - vector vm; for(int i = 1; i < nargs; i++) - vm.push_back(load_mesh(args[i])); + add_mesh(load_mesh(args[i])); TOC(time) gproshan_log_var(sizeof(real_t)); gproshan_log_var(time); - //init meshes - add_mesh(vm); - sub_menus.push_back("Fairing"); add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); @@ -178,6 +174,7 @@ bool app_viewer::process_poisson(viewer * p_view, const index_t & k) gproshan_log_var(view->time); // paint_holes_vertices(); + mesh.update(); return false; } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index e58b1134..de14be19 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -164,6 +164,7 @@ void che_viewer::log_info() { if(!mesh) return; + gproshan_log_var(mesh->filename()); gproshan_log_var(mesh->n_vertices()); gproshan_log_var(mesh->n_faces()); gproshan_log_var(mesh->n_half_edges()); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8340b743..de7183fe 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -294,20 +294,22 @@ void viewer::add_process(const int & key, const process_t & process) else cerr << "Repeat key: " << key << endl; } -void viewer::add_mesh(const vector & _meshes) +void viewer::add_mesh(che * p_mesh) { - for(che * _mesh: _meshes) + if(n_meshes == N_MESHES) { - assert(n_meshes < N_MESHES); - meshes[n_meshes].init(_mesh); - meshes[n_meshes].log_info(); - n_meshes++; + gproshan_log_var(n_meshes == N_MESHES); + gproshan_log_var(n_meshes); + return; } - - if(!n_meshes) return; + meshes[n_meshes].init(p_mesh); + meshes[n_meshes].log_info(); + n_meshes++; + + current = n_meshes - 1; glfwSetWindowTitle(window, mesh()->filename().c_str()); - + const int * mw = m_window_size[n_meshes - 1]; index_t m = n_meshes - 1; From 24531c34e7ae16b7b41eb73816fd710d9ae962a8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 16:19:57 +0200 Subject: [PATCH 0261/1018] update imgui --- imgui/imconfig.h | 6 +- imgui/imgui.cpp | 885 +++++++++++++-------- imgui/imgui.h | 196 +++-- imgui/imgui_demo.cpp | 1444 +++++++++++++++++++++------------- imgui/imgui_draw.cpp | 202 +++-- imgui/imgui_impl_glfw.cpp | 2 +- imgui/imgui_impl_glfw.h | 1 + imgui/imgui_impl_opengl3.cpp | 120 +-- imgui/imgui_impl_opengl3.h | 59 +- imgui/imgui_internal.h | 752 ++++++++++-------- imgui/imgui_widgets.cpp | 446 ++++++----- 11 files changed, 2536 insertions(+), 1577 deletions(-) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index 4f01b778..c6817de7 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -48,6 +48,9 @@ //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) //#define IMGUI_USE_BGRA_PACKED_COLOR +//---- Use 32-bit for ImWchar (default is 16-bit) to support full unicode code points. +//#define IMGUI_USE_WCHAR32 + //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version // By default the embedded implementations are declared static and not available outside of imgui cpp files. //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" @@ -77,9 +80,6 @@ // Read about ImGuiBackendFlags_RendererHasVtxOffset for details. //#define ImDrawIdx unsigned int -//---- Use 32-bit for ImWchar (default is 16-bit) to support full unicode code points. -//#define ImWchar ImWchar32 - //---- Override ImDrawCallback signature (will need to modify renderer back-ends accordingly) //struct ImDrawList; //struct ImDrawCmd; diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index bb6dd25f..b74f44cf 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,16 +1,17 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (main code and documentation) // Help: // - Read FAQ at http://dearimgui.org/faq // - Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. -// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. +// Read imgui.cpp for details, links and comments. // Resources: // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/2847 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/3075 (please post your screenshots/video there!) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues @@ -39,7 +40,7 @@ DOCUMENTATION - READ FIRST - HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI - GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE - - HOW A SIMPLE APPLICATION MAY LOOK LIKE (2 variations) + - HOW A SIMPLE APPLICATION MAY LOOK LIKE - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE - USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - API BREAKING CHANGES (read me when you update!) @@ -138,7 +139,7 @@ CODE - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build. - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori). - You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links docs/README.md. + You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links in the FAQ. - Dear ImGui is a "single pass" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances. For every application frame your UI code will be called only once. This is in contrast to e.g. Unity's own implementation of an IMGUI, where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches. @@ -152,6 +153,7 @@ CODE However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase. - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!). + HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI ---------------------------------------------- - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) @@ -162,6 +164,7 @@ CODE likely be a comment about it. Please report any issue to the GitHub page! - Try to keep your copy of dear imgui reasonably up to date. + GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE --------------------------------------------------------------- - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. @@ -176,9 +179,11 @@ CODE - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code. - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder. + HOW A SIMPLE APPLICATION MAY LOOK LIKE -------------------------------------- - EXHIBIT 1: USING THE EXAMPLE BINDINGS (imgui_impl_XXX.cpp files from the examples/ folder). + EXHIBIT 1: USING THE EXAMPLE BINDINGS (= imgui_impl_XXX.cpp files from the examples/ folder). + The sub-folders in examples/ contains examples applications following this structure. // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); @@ -267,8 +272,15 @@ CODE // Shutdown ImGui::DestroyContext(); + To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest your application, + you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! + Please read the FAQ and example applications for details about this! + + HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE --------------------------------------------- + The bindings in impl_impl_XXX.cpp files contains many working implementations of a rendering function. + void void MyImGuiRenderFunction(ImDrawData* draw_data) { // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled @@ -313,11 +325,6 @@ CODE } } - - The examples/ folders contains many actual implementation of the pseudo-codes above. - - When calling NewFrame(), the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags are updated. - They tell you if Dear ImGui intends to use your inputs. When a flag is set you want to hide the corresponding inputs from the - rest of your application. In every cases you need to pass on the inputs to Dear ImGui. - - Refer to the FAQ for more information. Amusingly, it is called a FAQ because people frequently run into the same issues! USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS ------------------------------------------ @@ -365,8 +372,9 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2020/04/23 (1.77) - Removed unnecessary ID (first arg) of ImFontAtlas::AddCustomRectRegular(). - 2020/01/22 (1.75) - ImDrawList::AddCircle()/AddCircleFilled() functions don't accept negative radius any more. - - 2019/12/17 (1.75) - made Columns() limited to 64 columns by asserting above that limit. While the current code technically supports it, future code may not so we're putting the restriction ahead. + - 2019/12/17 (1.75) - [undid this change in 1.76] made Columns() limited to 64 columns by asserting above that limit. While the current code technically supports it, future code may not so we're putting the restriction ahead. - 2019/12/13 (1.75) - [imgui_internal.h] changed ImRect() default constructor initializes all fields to 0.0f instead of (FLT_MAX,FLT_MAX,-FLT_MAX,-FLT_MAX). If you used ImRect::Add() to create bounding boxes by adding multiple points into it, you may need to fix your initial value. - 2019/12/08 (1.75) - removed redirecting functions/enums that were marked obsolete in 1.53 (December 2017): - ShowTestWindow() -> use ShowDemoWindow() @@ -497,7 +505,7 @@ CODE - 2017/08/09 (1.51) - removed ValueColor() helpers, they are equivalent to calling Text(label) + SameLine() + ColorButton(). - 2017/08/08 (1.51) - removed ColorEditMode() and ImGuiColorEditMode in favor of ImGuiColorEditFlags and parameters to the various Color*() functions. The SetColorEditOptions() allows to initialize default but the user can still change them with right-click context menu. - changed prototype of 'ColorEdit4(const char* label, float col[4], bool show_alpha = true)' to 'ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0)', where passing flags = 0x01 is a safe no-op (hello dodgy backward compatibility!). - check and run the demo window, under "Color/Picker Widgets", to understand the various new options. - - changed prototype of rarely used 'ColorButton(ImVec4 col, bool small_height = false, bool outline_border = true)' to 'ColorButton(const char* desc_id, ImVec4 col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0))' + - changed prototype of rarely used 'ColorButton(ImVec4 col, bool small_height = false, bool outline_border = true)' to 'ColorButton(const char* desc_id, ImVec4 col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0, 0))' - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset. - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity. @@ -783,7 +791,7 @@ CODE - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt and see how you want to help and can help! - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. - You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/2847). Visuals are ideal as they inspire other programmers. + You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/3075). Visuals are ideal as they inspire other programmers. But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately). @@ -892,7 +900,7 @@ static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time // Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by back-end) static const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f; // Extend outside and inside windows. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. -static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certaint time, unless mouse moved. +static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. //------------------------------------------------------------------------- // [SECTION] FORWARD DECLARATIONS @@ -900,7 +908,7 @@ static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock static void SetCurrentWindow(ImGuiWindow* window); static void FindHoveredWindow(); -static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags); +static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags); static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges); static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); @@ -909,8 +917,10 @@ static void AddWindowToSortBuffer(ImVector* out_sorted static ImRect GetViewportRect(); // Settings +static void WindowSettingsHandler_ClearAll(ImGuiContext*, ImGuiSettingsHandler*); static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line); +static void WindowSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSettingsHandler*); static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf); // Platform Dependents default implementation for IO functions @@ -927,6 +937,7 @@ static void NavUpdateWindowingOverlay(); static void NavUpdateMoveResult(); static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); +static void NavEndFrame(); static bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand); static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); static ImVec2 NavCalcPreferredRefPos(); @@ -935,7 +946,8 @@ static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static int FindWindowFocusIndex(ImGuiWindow* window); // Error Checking -static void ErrorCheckEndFrame(); +static void ErrorCheckNewFrameSanityChecks(); +static void ErrorCheckEndFrameSanityChecks(); static void ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool write); // Misc @@ -1019,9 +1031,10 @@ ImGuiStyle::ImGuiStyle() GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. + TabMinWidthForUnselectedCloseButton = 0.0f; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. - SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text when button is larger than text. + SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. @@ -1055,6 +1068,8 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) GrabMinSize = ImFloor(GrabMinSize * scale_factor); GrabRounding = ImFloor(GrabRounding * scale_factor); TabRounding = ImFloor(TabRounding * scale_factor); + if (TabMinWidthForUnselectedCloseButton != FLT_MAX) + TabMinWidthForUnselectedCloseButton = ImFloor(TabMinWidthForUnselectedCloseButton * scale_factor); DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); @@ -1127,17 +1142,21 @@ ImGuiIO::ImGuiIO() // - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message void ImGuiIO::AddInputCharacter(unsigned int c) { - InputQueueCharacters.push_back(c > 0 && c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); + if (c != 0) + InputQueueCharacters.push_back(c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); } // UTF16 strings use surrogate pairs to encode codepoints >= 0x10000, so // we should save the high surrogate. void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) { + if (c == 0 && InputQueueSurrogate == 0) + return; + if ((c & 0xFC00) == 0xD800) // High surrogate, must save { if (InputQueueSurrogate != 0) - InputQueueCharacters.push_back(0xFFFD); + InputQueueCharacters.push_back(IM_UNICODE_CODEPOINT_INVALID); InputQueueSurrogate = c; return; } @@ -1162,7 +1181,7 @@ void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { unsigned int c = 0; utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); - if (c > 0) + if (c != 0) InputQueueCharacters.push_back((ImWchar)c); } } @@ -1791,10 +1810,19 @@ int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_e } //----------------------------------------------------------------------------- -// [SECTION] MISC HELPERS/UTILTIES (Color functions) +// [SECTION] MISC HELPERS/UTILITIES (Color functions) // Note: The Convert functions are early design which are not consistent with other API. //----------------------------------------------------------------------------- +IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b) +{ + float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f; + int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t); + int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t); + int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t); + return IM_COL32(r, g, b, 0xFF); +} + ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in) { float s = 1.0f/255.0f; @@ -2215,7 +2243,9 @@ void ImGui::CalcListClipping(int items_count, float items_height, int* out_items // We create the union of the ClipRect and the NavScoringRect which at worst should be 1 page away from ClipRect ImRect unclipped_rect = window->ClipRect; if (g.NavMoveRequest) - unclipped_rect.Add(g.NavScoringRectScreen); + unclipped_rect.Add(g.NavScoringRect); + if (g.NavJustMovedToId && window->NavLastIds[0] == g.NavJustMovedToId) + unclipped_rect.Add(ImRect(window->Pos + window->NavRectRel[0].Min, window->Pos + window->NavRectRel[0].Max)); const ImVec2 pos = window->DC.CursorPos; int start = (int)((unclipped_rect.Min.y - pos.y) / items_height); @@ -2542,8 +2572,9 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) //----------------------------------------------------------------------------- // [SECTION] RENDER HELPERS -// Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change. -// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: state. +// Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change, +// we need a nicer separation between low-level functions and high-level functions relying on the ImGui context. +// Also see imgui_draw.cpp for some more which have been reworked to not rely on ImGui:: context. //----------------------------------------------------------------------------- const char* ImGui::FindRenderedTextEnd(const char* text, const char* text_end) @@ -2753,61 +2784,6 @@ void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) } } -// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state -void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale) -{ - const float h = draw_list->_Data->FontSize * 1.00f; - float r = h * 0.40f * scale; - ImVec2 center = pos + ImVec2(h * 0.50f, h * 0.50f * scale); - - ImVec2 a, b, c; - switch (dir) - { - case ImGuiDir_Up: - case ImGuiDir_Down: - if (dir == ImGuiDir_Up) r = -r; - a = ImVec2(+0.000f,+0.750f) * r; - b = ImVec2(-0.866f,-0.750f) * r; - c = ImVec2(+0.866f,-0.750f) * r; - break; - case ImGuiDir_Left: - case ImGuiDir_Right: - if (dir == ImGuiDir_Left) r = -r; - a = ImVec2(+0.750f,+0.000f) * r; - b = ImVec2(-0.750f,+0.866f) * r; - c = ImVec2(-0.750f,-0.866f) * r; - break; - case ImGuiDir_None: - case ImGuiDir_COUNT: - IM_ASSERT(0); - break; - } - draw_list->AddTriangleFilled(center + a, center + b, center + c, col); -} - -void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col) -{ - draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8); -} - -void ImGui::RenderCheckMark(ImVec2 pos, ImU32 col, float sz) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - - float thickness = ImMax(sz / 5.0f, 1.0f); - sz -= thickness*0.5f; - pos += ImVec2(thickness*0.25f, thickness*0.25f); - - float third = sz / 3.0f; - float bx = pos.x + third; - float by = pos.y + sz - third*0.5f; - window->DrawList->PathLineTo(ImVec2(bx - third, by - third)); - window->DrawList->PathLineTo(ImVec2(bx, by)); - window->DrawList->PathLineTo(ImVec2(bx + third*2, by - third*2)); - window->DrawList->PathStroke(col, false, thickness); -} - void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags) { ImGuiContext& g = *GImGui; @@ -2924,6 +2900,10 @@ ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); ImGui::KeepAliveID(id); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); +#endif return id; } @@ -2932,6 +2912,10 @@ ImGuiID ImGuiWindow::GetID(const void* ptr) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); ImGui::KeepAliveID(id); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_Pointer, ptr); +#endif return id; } @@ -2940,25 +2924,44 @@ ImGuiID ImGuiWindow::GetID(int n) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&n, sizeof(n), seed); ImGui::KeepAliveID(id); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_S32, (intptr_t)n); +#endif return id; } ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) { ImGuiID seed = IDStack.back(); - return ImHashStr(str, str_end ? (str_end - str) : 0, seed); + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); +#endif + return id; } ImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr) { ImGuiID seed = IDStack.back(); - return ImHashData(&ptr, sizeof(void*), seed); + ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_Pointer, ptr); +#endif + return id; } ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n) { ImGuiID seed = IDStack.back(); - return ImHashData(&n, sizeof(n), seed); + ImGuiID id = ImHashData(&n, sizeof(n), seed); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_S32, (intptr_t)n); +#endif + return id; } // This is only used in rare/specific situations to manufacture an ID out of nowhere. @@ -3185,7 +3188,7 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!bb.Overlaps(window->ClipRect)) - if (id == 0 || id != g.ActiveId) + if (id == 0 || (id != g.ActiveId && id != g.NavId)) if (clip_even_when_logged || !g.LogEnabled) return true; return false; @@ -3513,7 +3516,7 @@ static void ImGui::UpdateMouseInputs() ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) g.IO.MouseDoubleClicked[i] = true; - g.IO.MouseClickedTime[i] = -DBL_MAX; // so the third click isn't turned into a double-click + g.IO.MouseClickedTime[i] = -g.IO.MouseDoubleClickTime * 2.0f; // Mark as "old enough" so the third click isn't turned into a double-click } else { @@ -3720,33 +3723,15 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() g.IO.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; } -static void NewFrameSanityChecks() +ImGuiKeyModFlags ImGui::GetMergedKeyModFlags() { ImGuiContext& g = *GImGui; - - // Check user data - // (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument) - IM_ASSERT(g.Initialized); - IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); - IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); - IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); - IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); - IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); - IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); - IM_ASSERT(g.Style.CircleSegmentMaxError > 0.0f && "Invalid style setting!"); - IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!"); - IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); - IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); - for (int n = 0; n < ImGuiKey_COUNT; n++) - IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && "io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)"); - - // Perform simple check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only recently added in 1.60 WIP) - if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) - IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); - - // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. - if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) - g.IO.ConfigWindowsResizeFromEdges = false; + ImGuiKeyModFlags key_mod_flags = ImGuiKeyModFlags_None; + if (g.IO.KeyCtrl) { key_mod_flags |= ImGuiKeyModFlags_Ctrl; } + if (g.IO.KeyShift) { key_mod_flags |= ImGuiKeyModFlags_Shift; } + if (g.IO.KeyAlt) { key_mod_flags |= ImGuiKeyModFlags_Alt; } + if (g.IO.KeySuper) { key_mod_flags |= ImGuiKeyModFlags_Super; } + return key_mod_flags; } void ImGui::NewFrame() @@ -3759,7 +3744,7 @@ void ImGui::NewFrame() #endif // Check and assert for various common IO and Configuration mistakes - NewFrameSanityChecks(); + ErrorCheckNewFrameSanityChecks(); // Load settings on first frame, save settings when modified (after a delay) UpdateSettings(); @@ -3848,13 +3833,16 @@ void ImGui::NewFrame() g.DragDropAcceptIdCurrRectSurface = FLT_MAX; g.DragDropWithinSource = false; g.DragDropWithinTarget = false; + g.DragDropHoldJustPressedId = 0; // Update keyboard input state + // Synchronize io.KeyMods with individual modifiers io.KeyXXX bools + g.IO.KeyMods = GetMergedKeyModFlags(); memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f; - // Update gamepad/keyboard directional navigation + // Update gamepad/keyboard navigation NavUpdate(); // Update mouse input state @@ -3960,8 +3948,10 @@ void ImGui::Initialize(ImGuiContext* context) ImGuiSettingsHandler ini_handler; ini_handler.TypeName = "Window"; ini_handler.TypeHash = ImHashStr("Window"); + ini_handler.ClearAllFn = WindowSettingsHandler_ClearAll; ini_handler.ReadOpenFn = WindowSettingsHandler_ReadOpen; ini_handler.ReadLineFn = WindowSettingsHandler_ReadLine; + ini_handler.ApplyAllFn = WindowSettingsHandler_ApplyAll; ini_handler.WriteAllFn = WindowSettingsHandler_WriteAll; g.SettingsHandlers.push_back(ini_handler); } @@ -4036,7 +4026,7 @@ void ImGui::Shutdown(ImGuiContext* context) g.CurrentTabBarStack.clear(); g.ShrinkWidthBuffer.clear(); - g.PrivateClipboard.clear(); + g.ClipboardHandlerData.clear(); g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); @@ -4203,10 +4193,14 @@ void ImGui::EndFrame() { ImGuiContext& g = *GImGui; IM_ASSERT(g.Initialized); - if (g.FrameCountEnded == g.FrameCount) // Don't process EndFrame() multiple times. + + // Don't process EndFrame() multiple times. + if (g.FrameCountEnded == g.FrameCount) return; IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?"); + ErrorCheckEndFrameSanityChecks(); + // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) if (g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f)) { @@ -4214,17 +4208,14 @@ void ImGui::EndFrame() g.PlatformImeLastPos = g.PlatformImePos; } - ErrorCheckEndFrame(); - // Hide implicit/fallback "Debug" window if it hasn't been used g.WithinFrameScopeWithImplicitWindow = false; if (g.CurrentWindow && !g.CurrentWindow->WriteAccessed) g.CurrentWindow->Active = false; End(); - // Show CTRL+TAB list window - if (g.NavWindowingTarget != NULL) - NavUpdateWindowingOverlay(); + // Update navigation: CTRL+Tab, wrap-around requests + NavEndFrame(); // Drag and Drop: Elapse payload (if delivered, or if source stops being submitted) if (g.DragDropActive) @@ -4236,7 +4227,7 @@ void ImGui::EndFrame() } // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing. - if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount) + if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) { g.DragDropWithinSource = true; SetTooltip("..."); @@ -4251,7 +4242,7 @@ void ImGui::EndFrame() UpdateMouseMovingWindowEndFrame(); // Sort the window list so that all child windows are after their parent - // We cannot do that on FocusWindow() because childs may not exist yet + // We cannot do that on FocusWindow() because children may not exist yet g.WindowsTempSortBuffer.resize(0); g.WindowsTempSortBuffer.reserve(g.Windows.Size); for (int i = 0; i != g.Windows.Size; i++) @@ -4294,7 +4285,7 @@ void ImGui::Render() // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; - windows_to_render_top_most[1] = (g.NavWindowingTarget ? g.NavWindowingList : NULL); + windows_to_render_top_most[1] = (g.NavWindowingTarget ? g.NavWindowingListWindow : NULL); for (int n = 0; n != g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; @@ -4351,7 +4342,7 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex } // Find window given position, search front-to-back -// FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programatically +// FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programmatically // with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is // called, aka before the next Begin(). Moving window isn't affected. static void FindHoveredWindow() @@ -4891,7 +4882,15 @@ ImGuiWindow* ImGui::FindWindowByName(const char* name) return FindWindowByID(id); } -static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFlags flags) +static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settings) +{ + window->Pos = ImFloor(ImVec2(settings->Pos.x, settings->Pos.y)); + if (settings->Size.x > 0 && settings->Size.y > 0) + window->Size = window->SizeFull = ImFloor(ImVec2(settings->Size.x, settings->Size.y)); + window->Collapsed = settings->Collapsed; +} + +static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; //IMGUI_DEBUG_LOG("CreateNewWindow '%s', flags = 0x%08X\n", name, flags); @@ -4911,12 +4910,8 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl // Retrieve settings from .ini file window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); - window->Pos = ImVec2(settings->Pos.x, settings->Pos.y); - window->Collapsed = settings->Collapsed; - if (settings->Size.x > 0 && settings->Size.y > 0) - size = ImVec2(settings->Size.x, settings->Size.y); + ApplyWindowSettings(window, settings); } - window->Size = window->SizeFull = ImFloor(size); window->DC.CursorStartPos = window->DC.CursorMaxPos = window->Pos; // So first call to CalcContentSize() doesn't return crazy values if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) @@ -5069,14 +5064,29 @@ static const ImGuiResizeGripDef resize_grip_def[4] = { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper-right (Unused) }; +struct ImGuiResizeBorderDef +{ + ImVec2 InnerDir; + ImVec2 CornerPosN1, CornerPosN2; + float OuterAngle; +}; + +static const ImGuiResizeBorderDef resize_border_def[4] = +{ + { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top + { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right + { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom + { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f } // Left +}; + static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) { ImRect rect = window->Rect(); if (thickness == 0.0f) rect.Max -= ImVec2(1,1); - if (border_n == 0) return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); // Top - if (border_n == 1) return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); // Right - if (border_n == 2) return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); // Bottom - if (border_n == 3) return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding); // Left + if (border_n == 0) { return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); } // Top + if (border_n == 1) { return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); } // Right + if (border_n == 2) { return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); } // Bottom + if (border_n == 3) { return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding); } // Left IM_ASSERT(0); return ImRect(); } @@ -5234,19 +5244,6 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) int border_held = window->ResizeBorderHeld; if (border_held != -1) { - struct ImGuiResizeBorderDef - { - ImVec2 InnerDir; - ImVec2 CornerPosN1, CornerPosN2; - float OuterAngle; - }; - static const ImGuiResizeBorderDef resize_border_def[4] = - { - { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top - { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right - { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom - { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f } // Left - }; const ImGuiResizeBorderDef& def = resize_border_def[border_held]; ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI*0.25f, def.OuterAngle); @@ -5463,10 +5460,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) ImGuiWindow* window = FindWindowByName(name); const bool window_just_created = (window == NULL); if (window_just_created) - { - ImVec2 size_on_first_use = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) ? g.NextWindowData.SizeVal : ImVec2(0.0f, 0.0f); // Any condition flag will do since we are creating a new window here. - window = CreateNewWindow(name, size_on_first_use, flags); - } + window = CreateNewWindow(name, flags); // Automatically disable manual moving/resizing when NoInputs is set if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) @@ -5536,6 +5530,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) UpdateWindowParentAndRootLinks(window, flags, parent_window); // Process SetNextWindow***() calls + // (FIXME: Consider splitting the HasXXX flags into X/Y components bool window_pos_set_by_api = false; bool window_size_x_set_by_api = false, window_size_y_set_by_api = false; if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) @@ -5560,6 +5555,19 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window_size_y_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.y > 0.0f); SetWindowSize(window, g.NextWindowData.SizeVal, g.NextWindowData.SizeCond); } + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasScroll) + { + if (g.NextWindowData.ScrollVal.x >= 0.0f) + { + window->ScrollTarget.x = g.NextWindowData.ScrollVal.x; + window->ScrollTargetCenterRatio.x = 0.0f; + } + if (g.NextWindowData.ScrollVal.y >= 0.0f) + { + window->ScrollTarget.y = g.NextWindowData.ScrollVal.y; + window->ScrollTargetCenterRatio.y = 0.0f; + } + } if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasContentSize) window->ContentSizeExplicit = g.NextWindowData.ContentSizeVal; else if (first_begin_of_the_frame) @@ -5588,7 +5596,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update stored window name when it changes (which can _only_ happen with the "###" operator, so the ID would stay unchanged). // The title bar always display the 'name' parameter, so we only update the string storage if it needs to be visible to the end-user elsewhere. bool window_title_visible_elsewhere = false; - if (g.NavWindowingList != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0) // Window titles visible when using CTRL+TAB + if (g.NavWindowingListWindow != NULL && (window->Flags & ImGuiWindowFlags_NoNavFocus) == 0) // Window titles visible when using CTRL+TAB window_title_visible_elsewhere = true; if (window_title_visible_elsewhere && !window_just_created && strcmp(name, window->Name) != 0) { @@ -5951,7 +5959,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; window->DC.NavLayerActiveMaskNext = 0x00; - window->DC.NavFocusScopeIdCurrent = parent_window ? parent_window->DC.NavFocusScopeIdCurrent : 0; + window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : 0; // -V595 window->DC.NavHideHighlightOneFrame = false; window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); @@ -6159,19 +6167,18 @@ void ImGui::FocusWindow(ImGuiWindow* window) // Close popups if any ClosePopupsOverWindow(window, false); - // Passing NULL allow to disable keyboard focus - if (!window) - return; - // Move the root window to the top of the pile - IM_ASSERT(window->RootWindow != NULL); - ImGuiWindow* focus_front_window = window->RootWindow; // NB: In docking branch this is window->RootWindowDockStop - ImGuiWindow* display_front_window = window->RootWindow; + IM_ASSERT(window == NULL || window->RootWindow != NULL); + ImGuiWindow* focus_front_window = window ? window->RootWindow : NULL; // NB: In docking branch this is window->RootWindowDockStop + ImGuiWindow* display_front_window = window ? window->RootWindow : NULL; // Steal focus on active widgets - if (focus_front_window->Flags & ImGuiWindowFlags_Popup) // FIXME: This statement may be unnecessary? Need further testing before removing it.. - if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != focus_front_window) - ClearActiveID(); + if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != focus_front_window) + ClearActiveID(); + + // Passing NULL allow to disable keyboard focus + if (!window) + return; // Bring to front BringWindowToFocusFront(focus_front_window); @@ -6366,7 +6373,7 @@ bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) } // Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) -// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmaticaly. +// Note that NoNavFocus makes the window not reachable with CTRL+TAB but it can still be focused with mouse or programmatically. // If you want a window to never be focused, you may use the e.g. NoInputs flag. bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) { @@ -6561,6 +6568,13 @@ void ImGui::SetNextWindowContentSize(const ImVec2& size) g.NextWindowData.ContentSizeVal = size; } +void ImGui::SetNextWindowScroll(const ImVec2& scroll) +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasScroll; + g.NextWindowData.ScrollVal = scroll; +} + void ImGui::SetNextWindowCollapsed(bool collapsed, ImGuiCond cond) { ImGuiContext& g = *GImGui; @@ -6606,6 +6620,7 @@ ImVec2 ImGui::GetFontTexUvWhitePixel() void ImGui::SetWindowFontScale(float scale) { + IM_ASSERT(scale > 0.0f); ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); window->FontWindowScale = scale; @@ -6618,6 +6633,7 @@ void ImGui::ActivateItem(ImGuiID id) g.NavNextActivateId = id; } +// Note: this is storing in same stack as IDStack, so Push/Pop mismatch will be reported there. void ImGui::PushFocusScope(ImGuiID id) { ImGuiContext& g = *GImGui; @@ -6675,32 +6691,41 @@ ImGuiStorage* ImGui::GetStateStorage() void ImGui::PushID(const char* str_id) { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(str_id)); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetIDNoKeepAlive(str_id); + window->IDStack.push_back(id); } void ImGui::PushID(const char* str_id_begin, const char* str_id_end) { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(str_id_begin, str_id_end)); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetIDNoKeepAlive(str_id_begin, str_id_end); + window->IDStack.push_back(id); } void ImGui::PushID(const void* ptr_id) { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(ptr_id)); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetIDNoKeepAlive(ptr_id); + window->IDStack.push_back(id); } void ImGui::PushID(int int_id) { - ImGuiWindow* window = GImGui->CurrentWindow; - window->IDStack.push_back(window->GetIDNoKeepAlive(int_id)); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetIDNoKeepAlive(int_id); + window->IDStack.push_back(id); } // Push a given id value ignoring the ID stack as a seed. void ImGui::PushOverrideID(ImGuiID id) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; window->IDStack.push_back(id); } @@ -6763,11 +6788,55 @@ bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, si return !error; } -static void ImGui::ErrorCheckEndFrame() +static void ImGui::ErrorCheckNewFrameSanityChecks() +{ + ImGuiContext& g = *GImGui; + + // Check user IM_ASSERT macro + // (IF YOU GET A WARNING OR COMPILE ERROR HERE: it means you assert macro is incorrectly defined! + // If your macro uses multiple statements, it NEEDS to be surrounded by a 'do { ... } while (0)' block. + // This is a common C/C++ idiom to allow multiple statements macros to be used in control flow blocks.) + // #define IM_ASSERT(EXPR) SomeCode(EXPR); SomeMoreCode(); // Wrong! + // #define IM_ASSERT(EXPR) do { SomeCode(EXPR); SomeMoreCode(); } while (0) // Correct! + if (true) IM_ASSERT(1); else IM_ASSERT(0); + + // Check user data + // (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument) + IM_ASSERT(g.Initialized); + IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); + IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); + IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); + IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); + IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); + IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.CircleSegmentMaxError > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!"); + IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); + IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); + for (int n = 0; n < ImGuiKey_COUNT; n++) + IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && "io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)"); + + // Perform simple check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only recently added in 1.60 WIP) + if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) + IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); + + // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. + if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) + g.IO.ConfigWindowsResizeFromEdges = false; +} + +static void ImGui::ErrorCheckEndFrameSanityChecks() { + ImGuiContext& g = *GImGui; + + // Verify that io.KeyXXX fields haven't been tampered with. Key mods should not be modified between NewFrame() and EndFrame() + // One possible reason leading to this assert is that your back-ends update inputs _AFTER_ NewFrame(). + const ImGuiKeyModFlags expected_key_mod_flags = GetMergedKeyModFlags(); + IM_ASSERT(g.IO.KeyMods == expected_key_mod_flags && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); + IM_UNUSED(expected_key_mod_flags); + // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). - ImGuiContext& g = *GImGui; if (g.CurrentWindowStack.Size != 1) { if (g.CurrentWindowStack.Size > 1) @@ -6840,6 +6909,8 @@ static void ImGui::ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool //----------------------------------------------------------------------------- // Advance cursor given item size for layout. +// Register minimum needed size so it can extend the bounding box used for auto-fit calculation. +// See comments in ItemAdd() about how/why the size provided to ItemSize() vs ItemAdd() may often different. void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) { ImGuiContext& g = *GImGui; @@ -6880,7 +6951,7 @@ void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface -// declare their minimum size requirement to ItemSize() and then use a larger region for drawing/interaction, which is passed to ItemAdd(). +// declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) { ImGuiContext& g = *GImGui; @@ -7306,7 +7377,8 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool s target_y = window->ContentSize.y + window->WindowPadding.y * 2.0f; scroll.y = target_y - cr_y * (window->SizeFull.y - window->ScrollbarSizes.y - decoration_up_height); } - scroll = ImMax(scroll, ImVec2(0.0f, 0.0f)); + scroll.x = IM_FLOOR(ImMax(scroll.x, 0.0f)); + scroll.y = IM_FLOOR(ImMax(scroll.y, 0.0f)); if (!window->Collapsed && !window->SkipItems) { scroll.x = ImMin(scroll.x, window->ScrollMax.x); @@ -7642,7 +7714,7 @@ void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_ } else { - if (g.NavLayer == 0 && focus_window) + if (g.NavLayer == ImGuiNavLayer_Main && focus_window) focus_window = NavRestoreLastChildNavWindow(focus_window); FocusWindow(focus_window); } @@ -7753,9 +7825,10 @@ void ImGui::EndPopup() IM_ASSERT(g.BeginPopupStack.Size > 0); // Make all menus and popups wrap around for now, may need to expose that policy. - NavMoveRequestTryWrapping(window, ImGuiNavMoveFlags_LoopY); + if (g.NavWindow == window) + NavMoveRequestTryWrapping(window, ImGuiNavMoveFlags_LoopY); - // Child-popups don't need to be layed out + // Child-popups don't need to be laid out IM_ASSERT(g.WithinEndChild == false); if (window->Flags & ImGuiWindowFlags_ChildWindow) g.WithinEndChild = true; @@ -7999,7 +8072,7 @@ static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect } } -// Scoring function for directional navigation. Based on https://gist.github.com/rygorous/6981057 +// Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057 static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) { ImGuiContext& g = *GImGui; @@ -8007,7 +8080,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) if (g.NavLayer != window->DC.NavLayerCurrent) return false; - const ImRect& curr = g.NavScoringRectScreen; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width) + const ImRect& curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width) g.NavScoringCount++; // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring @@ -8121,7 +8194,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) // 2017/09/29: FIXME: This now currently only enabled inside menu bars, ideally we'd disable it everywhere. Menus in particular need to catch failure. For general navigation it feels awkward. // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option? if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial) // Check axial match - if (g.NavLayer == 1 && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) + if (g.NavLayer == ImGuiNavLayer_Menu && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f)) { result->DistAxial = dist_axial; @@ -8232,36 +8305,11 @@ void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const Im void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags) { ImGuiContext& g = *GImGui; - if (g.NavWindow != window || !NavMoveRequestButNoResultYet() || g.NavMoveRequestForward != ImGuiNavForward_None || g.NavLayer != 0) - return; - IM_ASSERT(move_flags != 0); // No points calling this with no wrapping - ImRect bb_rel = window->NavRectRel[0]; - ImGuiDir clip_dir = g.NavMoveDir; - if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) - { - bb_rel.Min.x = bb_rel.Max.x = ImMax(window->SizeFull.x, window->ContentSize.x + window->WindowPadding.x * 2.0f) - window->Scroll.x; - if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(-bb_rel.GetHeight()); clip_dir = ImGuiDir_Up; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); - } - if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) - { - bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x; - if (move_flags & ImGuiNavMoveFlags_WrapX) { bb_rel.TranslateY(+bb_rel.GetHeight()); clip_dir = ImGuiDir_Down; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); - } - if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) - { - bb_rel.Min.y = bb_rel.Max.y = ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y; - if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(-bb_rel.GetWidth()); clip_dir = ImGuiDir_Left; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); - } - if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) - { - bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y; - if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(+bb_rel.GetWidth()); clip_dir = ImGuiDir_Right; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); - } + // Navigation wrap-around logic is delayed to the end of the frame because this operation is only valid after entire + // popup is assembled and in case of appended popups it is not clear which EndPopup() call is final. + g.NavWrapRequestWindow = window; + g.NavWrapRequestFlags = move_flags; } // FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0). @@ -8391,6 +8439,8 @@ static void ImGui::NavUpdate() { ImGuiContext& g = *GImGui; g.IO.WantSetMousePos = false; + g.NavWrapRequestWindow = NULL; + g.NavWrapRequestFlags = ImGuiNavMoveFlags_None; #if 0 if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG("NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.FrameCount, g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); #endif @@ -8477,7 +8527,7 @@ static void ImGui::NavUpdate() // Store our return window (for returning from Layer 1 to Layer 0) and clear it as soon as we step back in our own Layer 0 if (g.NavWindow) NavSaveLastChildNavWindowIntoParent(g.NavWindow); - if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == 0) + if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == ImGuiNavLayer_Main) g.NavWindow->NavLastChildNavWindow = NULL; // Update CTRL+TAB and Windowing features (hold Square to move/resize/etc.) @@ -8514,7 +8564,7 @@ static void ImGui::NavUpdate() if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); } - else if (g.NavLayer != 0) + else if (g.NavLayer != ImGuiNavLayer_Main) { // Leave the "menu" layer NavRestoreLayer(ImGuiNavLayer_Main); @@ -8588,6 +8638,7 @@ static void ImGui::NavUpdate() if (g.NavMoveDir != ImGuiDir_None) { g.NavMoveRequest = true; + g.NavMoveRequestKeyMods = g.IO.KeyMods; g.NavMoveDirLast = g.NavMoveDir; } if (g.NavMoveRequest && g.NavId == 0) @@ -8635,7 +8686,7 @@ static void ImGui::NavUpdate() g.NavMoveResultOther.Clear(); // When we have manually scrolled (without using navigation) and NavId becomes out of bounds, we project its bounding box to the visible area to restart navigation within visible items - if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == 0) + if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == ImGuiNavLayer_Main) { ImGuiWindow* window = g.NavWindow; ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1,1), window->InnerRect.Max - window->Pos + ImVec2(1,1)); @@ -8651,11 +8702,11 @@ static void ImGui::NavUpdate() // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) ImRect nav_rect_rel = (g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted()) ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0,0,0,0); - g.NavScoringRectScreen = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : GetViewportRect(); - g.NavScoringRectScreen.TranslateY(nav_scoring_rect_offset_y); - g.NavScoringRectScreen.Min.x = ImMin(g.NavScoringRectScreen.Min.x + 1.0f, g.NavScoringRectScreen.Max.x); - g.NavScoringRectScreen.Max.x = g.NavScoringRectScreen.Min.x; - IM_ASSERT(!g.NavScoringRectScreen.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). + g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : GetViewportRect(); + g.NavScoringRect.TranslateY(nav_scoring_rect_offset_y); + g.NavScoringRect.Min.x = ImMin(g.NavScoringRect.Min.x + 1.0f, g.NavScoringRect.Max.x); + g.NavScoringRect.Max.x = g.NavScoringRect.Min.x; + IM_ASSERT(!g.NavScoringRect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). //GetForegroundDrawList()->AddRect(g.NavScoringRectScreen.Min, g.NavScoringRectScreen.Max, IM_COL32(255,200,0,255)); // [DEBUG] g.NavScoringCount = 0; #if IMGUI_DEBUG_NAV_RECTS @@ -8698,7 +8749,7 @@ static void ImGui::NavUpdateMoveResult() IM_ASSERT(g.NavWindow && result->Window); // Scroll to keep newly navigated item fully into view. - if (g.NavLayer == 0) + if (g.NavLayer == ImGuiNavLayer_Main) { ImVec2 delta_scroll; if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_ScrollToEdge) @@ -8725,7 +8776,7 @@ static void ImGui::NavUpdateMoveResult() // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; g.NavJustMovedToFocusScopeId = result->FocusScopeId; - + g.NavJustMovedToKeyMods = g.NavMoveRequestKeyMods; } SetNavIDWithRectRel(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); g.NavMoveFromClampedRefRect = false; @@ -8737,7 +8788,7 @@ static float ImGui::NavUpdatePageUpPageDown() ImGuiContext& g = *GImGui; if (g.NavMoveDir != ImGuiDir_None || g.NavWindow == NULL) return 0.0f; - if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != 0) + if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main) return 0.0f; ImGuiWindow* window = g.NavWindow; @@ -8803,6 +8854,68 @@ static float ImGui::NavUpdatePageUpPageDown() return 0.0f; } +static void ImGui::NavEndFrame() +{ + ImGuiContext& g = *GImGui; + + // Show CTRL+TAB list window + if (g.NavWindowingTarget != NULL) + NavUpdateWindowingOverlay(); + + // Perform wrap-around in menus + ImGuiWindow* window = g.NavWrapRequestWindow; + ImGuiNavMoveFlags move_flags = g.NavWrapRequestFlags; + if (window != NULL && g.NavWindow == window && NavMoveRequestButNoResultYet() && g.NavMoveRequestForward == ImGuiNavForward_None && g.NavLayer == ImGuiNavLayer_Main) + { + IM_ASSERT(move_flags != 0); // No points calling this with no wrapping + ImRect bb_rel = window->NavRectRel[0]; + + ImGuiDir clip_dir = g.NavMoveDir; + if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + { + bb_rel.Min.x = bb_rel.Max.x = + ImMax(window->SizeFull.x, window->ContentSize.x + window->WindowPadding.x * 2.0f) - window->Scroll.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) + { + bb_rel.TranslateY(-bb_rel.GetHeight()); + clip_dir = ImGuiDir_Up; + } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + { + bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) + { + bb_rel.TranslateY(+bb_rel.GetHeight()); + clip_dir = ImGuiDir_Down; + } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = + ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) + { + bb_rel.TranslateX(-bb_rel.GetWidth()); + clip_dir = ImGuiDir_Left; + } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) + { + bb_rel.TranslateX(+bb_rel.GetWidth()); + clip_dir = ImGuiDir_Right; + } + NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + } + } +} + static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N) { ImGuiContext& g = *GImGui; @@ -9000,8 +9113,8 @@ void ImGui::NavUpdateWindowingOverlay() if (g.NavWindowingTimer < NAV_WINDOWING_LIST_APPEAR_DELAY) return; - if (g.NavWindowingList == NULL) - g.NavWindowingList = FindWindowByName("###NavWindowingList"); + if (g.NavWindowingListWindow == NULL) + g.NavWindowingListWindow = FindWindowByName("###NavWindowingList"); SetNextWindowSizeConstraints(ImVec2(g.IO.DisplaySize.x * 0.20f, g.IO.DisplaySize.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX)); SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f); @@ -9535,6 +9648,19 @@ void ImGui::LogButtons() //----------------------------------------------------------------------------- // [SECTION] SETTINGS //----------------------------------------------------------------------------- +// - UpdateSettings() [Internal] +// - MarkIniSettingsDirty() [Internal] +// - CreateNewWindowSettings() [Internal] +// - FindWindowSettings() [Internal] +// - FindOrCreateWindowSettings() [Internal] +// - FindSettingsHandler() [Internal] +// - ClearIniSettings() [Internal] +// - LoadIniSettingsFromDisk() +// - LoadIniSettingsFromMemory() +// - SaveIniSettingsToDisk() +// - SaveIniSettingsToMemory() +// - WindowSettingsHandler_***() [Internal] +//----------------------------------------------------------------------------- // Called by NewFrame() void ImGui::UpdateSettings() @@ -9617,16 +9743,6 @@ ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name) return CreateNewWindowSettings(name); } -void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) -{ - size_t file_data_size = 0; - char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); - if (!file_data) - return; - LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); - IM_FREE(file_data); -} - ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) { ImGuiContext& g = *GImGui; @@ -9637,21 +9753,48 @@ ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) return NULL; } +void ImGui::ClearIniSettings() +{ + ImGuiContext& g = *GImGui; + g.SettingsIniData.clear(); + for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) + if (g.SettingsHandlers[handler_n].ClearAllFn) + g.SettingsHandlers[handler_n].ClearAllFn(&g, &g.SettingsHandlers[handler_n]); +} + +void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) +{ + size_t file_data_size = 0; + char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); + if (!file_data) + return; + LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); + IM_FREE(file_data); +} + // Zero-tolerance, no error reporting, cheap .ini parsing void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) { ImGuiContext& g = *GImGui; IM_ASSERT(g.Initialized); - IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); + //IM_ASSERT(!g.WithinFrameScope && "Cannot be called between NewFrame() and EndFrame()"); + //IM_ASSERT(g.SettingsLoaded == false && g.FrameCount == 0); // For user convenience, we allow passing a non zero-terminated string (hence the ini_size parameter). // For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy.. if (ini_size == 0) ini_size = strlen(ini_data); - char* buf = (char*)IM_ALLOC(ini_size + 1); - char* buf_end = buf + ini_size; + g.SettingsIniData.Buf.resize((int)ini_size + 1); + char* const buf = g.SettingsIniData.Buf.Data; + char* const buf_end = buf + ini_size; memcpy(buf, ini_data, ini_size); - buf[ini_size] = 0; + buf_end[0] = 0; + + // Call pre-read handlers + // Some types will clear their data (e.g. dock information) some types will allow merge/override (window) + for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) + if (g.SettingsHandlers[handler_n].ReadInitFn) + g.SettingsHandlers[handler_n].ReadInitFn(&g, &g.SettingsHandlers[handler_n]); void* entry_data = NULL; ImGuiSettingsHandler* entry_handler = NULL; @@ -9689,8 +9832,15 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) entry_handler->ReadLineFn(&g, entry_handler, entry_data, line); } } - IM_FREE(buf); g.SettingsLoaded = true; + + // [DEBUG] Restore untouched copy so it can be browsed in Metrics (not strictly necessary) + memcpy(buf, ini_data, ini_size); + + // Call post-read handlers + for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) + if (g.SettingsHandlers[handler_n].ApplyAllFn) + g.SettingsHandlers[handler_n].ApplyAllFn(&g, &g.SettingsHandlers[handler_n]); } void ImGui::SaveIniSettingsToDisk(const char* ini_filename) @@ -9726,11 +9876,21 @@ const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) return g.SettingsIniData.c_str(); } +static void WindowSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) +{ + ImGuiContext& g = *ctx; + for (int i = 0; i != g.Windows.Size; i++) + g.Windows[i]->SettingsOffset = -1; + g.SettingsWindows.clear(); +} + static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { - ImGuiWindowSettings* settings = ImGui::FindWindowSettings(ImHashStr(name)); - if (!settings) - settings = ImGui::CreateNewWindowSettings(name); + ImGuiWindowSettings* settings = ImGui::FindOrCreateWindowSettings(name); + ImGuiID id = settings->ID; + *settings = ImGuiWindowSettings(); // Clear existing if recycling previous entry + settings->ID = id; + settings->WantApply = true; return (void*)settings; } @@ -9744,6 +9904,19 @@ static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, else if (sscanf(line, "Collapsed=%d", &i) == 1) settings->Collapsed = (i != 0); } +// Apply to existing windows (if any) +static void WindowSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*) +{ + ImGuiContext& g = *ctx; + for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) + if (settings->WantApply) + { + if (ImGuiWindow* window = ImGui::FindWindowByID(settings->ID)) + ApplyWindowSettings(window, settings); + settings->WantApply = false; + } +} + static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) { // Gather data from windows that were active during this session @@ -9807,10 +9980,11 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl #endif // Win32 clipboard implementation +// We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown() static const char* GetClipboardTextFn_DefaultImpl(void*) { - static ImVector buf_local; - buf_local.clear(); + ImGuiContext& g = *GImGui; + g.ClipboardHandlerData.clear(); if (!::OpenClipboard(NULL)) return NULL; HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); @@ -9822,12 +9996,12 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle)) { int buf_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, NULL, 0, NULL, NULL); - buf_local.resize(buf_len); - ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, buf_local.Data, buf_len, NULL, NULL); + g.ClipboardHandlerData.resize(buf_len); + ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, g.ClipboardHandlerData.Data, buf_len, NULL, NULL); } ::GlobalUnlock(wbuf_handle); ::CloseClipboard(); - return buf_local.Data; + return g.ClipboardHandlerData.Data; } static void SetClipboardTextFn_DefaultImpl(void*, const char* text) @@ -9889,13 +10063,14 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) CFDataRef cf_data; if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr) { - static ImVector clipboard_text; + ImGuiContext& g = *GImGui; + g.ClipboardHandlerData.clear(); int length = (int)CFDataGetLength(cf_data); - clipboard_text.resize(length + 1); - CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)clipboard_text.Data); - clipboard_text[length] = 0; + g.ClipboardHandlerData.resize(length + 1); + CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)g.ClipboardHandlerData.Data); + g.ClipboardHandlerData[length] = 0; CFRelease(cf_data); - return clipboard_text.Data; + return g.ClipboardHandlerData.Data; } } } @@ -9908,17 +10083,17 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void*) { ImGuiContext& g = *GImGui; - return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin(); + return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin(); } static void SetClipboardTextFn_DefaultImpl(void*, const char* text) { ImGuiContext& g = *GImGui; - g.PrivateClipboard.clear(); + g.ClipboardHandlerData.clear(); const char* text_end = text + strlen(text); - g.PrivateClipboard.resize((int)(text_end - text) + 1); - memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text)); - g.PrivateClipboard[(int)(text_end - text)] = 0; + g.ClipboardHandlerData.resize((int)(text_end - text) + 1); + memcpy(&g.ClipboardHandlerData[0], text, (size_t)(text_end - text)); + g.ClipboardHandlerData[(int)(text_end - text)] = 0; } #endif @@ -9983,8 +10158,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Debugging enums enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Content, WRT_ContentRegionRect, WRT_Count }; // Windows Rect Type const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Content", "ContentRegionRect" }; - enum { TRT_OuterRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersDesired, TRT_ColumnsContentRowsFrozen, TRT_ColumnsContentRowsUnfrozen, TRT_Count }; // Tables Rect Type - const char* trt_rects_names[TRT_Count] = { "OuterRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersDesired", "ColumnsContentRowsFrozen", "ColumnsContentRowsUnfrozen" }; + enum { TRT_OuterRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersIdeal, TRT_ColumnsContentRowsFrozen, TRT_ColumnsContentRowsUnfrozen, TRT_Count }; // Tables Rect Type + const char* trt_rects_names[TRT_Count] = { "OuterRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersIdeal", "ColumnsContentRowsFrozen", "ColumnsContentRowsUnfrozen" }; // State static bool show_windows_rects = false; @@ -9992,7 +10167,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) static bool show_windows_begin_order = false; static bool show_tables_rects = false; static int show_tables_rect_type = TRT_WorkRect; - static bool show_drawcmd_details = true; + static bool show_drawcmd_mesh = true; + static bool show_drawcmd_aabb = true; // Basic info ImGuiContext& g = *GImGui; @@ -10026,6 +10202,38 @@ void ImGui::ShowMetricsWindow(bool* p_open) return ImRect(); } + static void NodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, int elem_offset, bool show_mesh, bool show_aabb) + { + IM_ASSERT(show_mesh || show_aabb); + ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list + ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; + + // Draw wire-frame version of all triangles + ImRect clip_rect = draw_cmd->ClipRect; + ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + ImDrawListFlags backup_flags = fg_draw_list->Flags; + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + draw_cmd->ElemCount); base_idx += 3) + { + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++) + { + ImVec2 p = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; + triangle[n] = p; + vtxs_rect.Add(p); + } + if (show_mesh) + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles + } + // Draw bounding boxes + if (show_aabb) + { + fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU + fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles + } + fg_draw_list->Flags = backup_flags; + } + static void NodeDrawList(ImGuiWindow* window, ImDrawList* draw_list, const char* label) { bool node_open = ImGui::TreeNode(draw_list, "%s: '%s' %d vtx, %d indices, %d cmds", label, draw_list->_OwnerName ? draw_list->_OwnerName : "", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, draw_list->CmdBuffer.Size); @@ -10063,15 +10271,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); - if (show_drawcmd_details && fg_draw_list && ImGui::IsItemHovered()) - { - ImRect clip_rect = pcmd->ClipRect; - ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - for (unsigned int i = elem_offset; i < elem_offset + (int)pcmd->ElemCount; i++) - vtxs_rect.Add(draw_list->VtxBuffer[idx_buffer ? idx_buffer[i] : i].pos); - fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255,0,255,255)); - fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(255,255,0,255)); - } + if (ImGui::IsItemHovered() && (show_drawcmd_mesh || show_drawcmd_aabb) && fg_draw_list) + NodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, elem_offset, show_drawcmd_mesh, show_drawcmd_aabb); if (!pcmd_node_open) continue; @@ -10089,22 +10290,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Display vertex information summary. Hover to get all triangles drawn in wire-frame ImFormatString(buf, IM_ARRAYSIZE(buf), "Mesh: ElemCount: %d, VtxOffset: +%d, IdxOffset: +%d, Area: ~%0.f px", pcmd->ElemCount, pcmd->VtxOffset, pcmd->IdxOffset, total_area); ImGui::Selectable(buf); - if (fg_draw_list && ImGui::IsItemHovered() && show_drawcmd_details) - { - // Draw wire-frame version of everything - ImDrawListFlags backup_flags = fg_draw_list->Flags; - fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. - ImRect clip_rect = pcmd->ClipRect; - fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); - for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + pcmd->ElemCount); base_idx += 3) - { - ImVec2 triangle[3]; - for (int n = 0; n < 3; n++) - triangle[n] = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; - fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); - } - fg_draw_list->Flags = backup_flags; - } + if (ImGui::IsItemHovered() && fg_draw_list) + NodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, elem_offset, true, false); // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. @@ -10170,6 +10357,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::GetForegroundDrawList()->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); if (!open) return; + + if (!window->WasActive) + ImGui::TextDisabled("Note: window is not currently visible."); + if (window->MemoryCompacted) + ImGui::TextDisabled("Note: some memory buffers have been compacted/freed."); + ImGuiWindowFlags flags = window->Flags; NodeDrawList(window, window->DrawList, "DrawList"); ImGui::BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y); @@ -10199,6 +10392,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::TreePop(); } + static void NodeWindowSettings(ImGuiWindowSettings* settings) + { + ImGui::Text("0x%08X \"%s\" Pos (%d,%d) Size (%d,%d) Collapsed=%d", + settings->ID, settings->GetName(), settings->Pos.x, settings->Pos.y, settings->Size.x, settings->Size.y, settings->Collapsed); + } + static void NodeTabBar(ImGuiTabBar* tab_bar) { // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings. @@ -10234,6 +10433,37 @@ void ImGui::ShowMetricsWindow(bool* p_open) } }; + // Tools + if (ImGui::TreeNode("Tools")) + { + // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. + if (ImGui::Button("Item Picker..")) + ImGui::DebugStartItemPicker(); + ImGui::SameLine(); + MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); + + ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); + ImGui::Checkbox("Show windows rectangles", &show_windows_rects); + ImGui::SameLine(); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); + show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count, WRT_Count); + if (show_windows_rects && g.NavWindow) + { + ImGui::BulletText("'%s':", g.NavWindow->Name); + ImGui::Indent(); + for (int rect_n = 0; rect_n < WRT_Count; rect_n++) + { + ImRect r = Funcs::GetWindowRect(g.NavWindow, rect_n); + ImGui::Text("(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), wrt_rects_names[rect_n]); + } + ImGui::Unindent(); + } + ImGui::Checkbox("Show mesh when hovering ImDrawCmd", &show_drawcmd_mesh); + ImGui::Checkbox("Show bounding boxes when hovering ImDrawCmd", &show_drawcmd_aabb); + ImGui::TreePop(); + } + + // Contents Funcs::NodeWindows(g.Windows, "Windows"); //Funcs::NodeWindows(g.WindowsFocusOrder, "WindowsFocusOrder"); if (ImGui::TreeNode("DrawLists", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) @@ -10273,7 +10503,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Funcs::NodeTable(g.Tables.GetByIndex(n)); ImGui::TreePop(); } -#endif // #define IMGUI_HAS_TABLE +#endif // #ifdef IMGUI_HAS_TABLE // Details for Docking #ifdef IMGUI_HAS_DOCK @@ -10281,7 +10511,55 @@ void ImGui::ShowMetricsWindow(bool* p_open) { ImGui::TreePop(); } -#endif // #define IMGUI_HAS_DOCK +#endif // #ifdef IMGUI_HAS_DOCK + + // Settings + if (ImGui::TreeNode("Settings")) + { + if (ImGui::SmallButton("Clear")) + ImGui::ClearIniSettings(); + ImGui::SameLine(); + if (ImGui::SmallButton("Save to disk")) + ImGui::SaveIniSettingsToDisk(g.IO.IniFilename); + ImGui::SameLine(); + if (g.IO.IniFilename) + ImGui::Text("\"%s\"", g.IO.IniFilename); + else + ImGui::TextUnformatted(""); + ImGui::Text("SettingsDirtyTimer %.2f", g.SettingsDirtyTimer); + if (ImGui::TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) + { + for (int n = 0; n < g.SettingsHandlers.Size; n++) + ImGui::TextUnformatted(g.SettingsHandlers[n].TypeName); + ImGui::TreePop(); + } + if (ImGui::TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) + { + for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) + Funcs::NodeWindowSettings(settings); + ImGui::TreePop(); + } + +#ifdef IMGUI_HAS_TABLE + if (ImGui::TreeNode("SettingsTables", "Settings packed data: Tables: %d bytes", g.SettingsTables.size())) + { + for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) + Funcs::NodeTableSettings(settings); + ImGui::TreePop(); + } +#endif // #ifdef IMGUI_HAS_TABLE + +#ifdef IMGUI_HAS_DOCK +#endif // #ifdef IMGUI_HAS_DOCK + + if (ImGui::TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size())) + { + char* buf = (char*)(void*)(g.SettingsIniData.Buf.Data ? g.SettingsIniData.Buf.Data : ""); + ImGui::InputTextMultiline("##Ini", buf, g.SettingsIniData.Buf.Size, ImVec2(-FLT_MIN, 0.0f), ImGuiInputTextFlags_ReadOnly); + ImGui::TreePop(); + } + ImGui::TreePop(); + } // Misc Details if (ImGui::TreeNode("Internal state")) @@ -10304,35 +10582,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::TreePop(); } - // Tools - if (ImGui::TreeNode("Tools")) - { - // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. - if (ImGui::Button("Item Picker..")) - ImGui::DebugStartItemPicker(); - ImGui::SameLine(); - MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); - - ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); - ImGui::Checkbox("Show windows rectangles", &show_windows_rects); - ImGui::SameLine(); - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); - show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count, WRT_Count); - if (show_windows_rects && g.NavWindow) - { - ImGui::BulletText("'%s':", g.NavWindow->Name); - ImGui::Indent(); - for (int rect_n = 0; rect_n < WRT_Count; rect_n++) - { - ImRect r = Funcs::GetWindowRect(g.NavWindow, rect_n); - ImGui::Text("(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), wrt_rects_names[rect_n]); - } - ImGui::Unindent(); - } - ImGui::Checkbox("Show details when hovering ImDrawCmd node", &show_drawcmd_details); - ImGui::TreePop(); - } - // Overlay: Display windows Rectangles and Begin Order if (show_windows_rects || show_windows_begin_order) { @@ -10367,14 +10616,14 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGuiTable* table = g.Tables.GetByIndex(table_n); } } -#endif // #define IMGUI_HAS_TABLE +#endif // #ifdef IMGUI_HAS_TABLE #ifdef IMGUI_HAS_DOCK // Overlay: Display Docking info if (show_docking_nodes && g.IO.KeyCtrl) { } -#endif // #define IMGUI_HAS_DOCK +#endif // #ifdef IMGUI_HAS_DOCK ImGui::End(); } diff --git a/imgui/imgui.h b/imgui/imgui.h index 70798230..824c98de 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,17 +1,17 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (headers) // Help: // - Read FAQ at http://dearimgui.org/faq // - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. -// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. -// Read imgui.cpp for more details, documentation and comments. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. +// Read imgui.cpp for details, links and comments. // Resources: // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/2847 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/3075 (please post your screenshots/video there!) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues @@ -59,13 +59,13 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.76 WIP" -#define IMGUI_VERSION_NUM 17502 +#define IMGUI_VERSION "1.77 WIP" +#define IMGUI_VERSION_NUM 17601 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Define attributes of all API symbols declarations (e.g. for DLL under Windows) // IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default bindings files (imgui_impl_xxx.h) -// Using dear imgui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +// Using dear imgui via a shared library is not recommended, because we don't guarantee backward nor forward ABI compatibility (also function call overhead, as dear imgui is a call-heavy API) #ifndef IMGUI_API #define IMGUI_API #endif @@ -92,8 +92,6 @@ Index of this file: #else #define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. #endif -#define IM_UNICODE_CODEPOINT_MAX (sizeof(ImWchar) == 2 ? 0xFFFF : 0x10FFFF) // Last Unicode code point supported by this build. -#define IM_UNICODE_CODEPOINT_INVALID 0xFFFD // Standard invalid Unicode code point. // Warnings #if defined(__clang__) @@ -112,6 +110,7 @@ Index of this file: // Forward declarations and basic types //----------------------------------------------------------------------------- +// Forward declarations struct ImDrawChannel; // Temporary storage to output draw commands out of order, used by ImDrawListSplitter and ImDrawList::ChannelsSplit() struct ImDrawCmd; // A single draw command within a parent ImDrawList (generally maps to 1 GPU draw call, unless it is a callback) struct ImDrawData; // All draw command lists required to render the frame + pos/size coordinates to use for the projection matrix. @@ -135,19 +134,12 @@ struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSiz struct ImGuiStorage; // Helper for key->value storage struct ImGuiStyle; // Runtime data for styling/colors struct ImGuiTextBuffer; // Helper to hold and append into a text buffer (~string builder) -struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbb][,ccccc]") +struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbbb][,ccccc]") -// Typedefs and Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) -// Use your programming IDE "Go to definition" facility on the names in the central column below to find the actual flags/enum lists. -#ifndef ImTextureID -typedef void* ImTextureID; // User data to identify a texture (this is whatever to you want it to be! read the FAQ about ImTextureID in imgui.cpp) -#endif -typedef unsigned int ImGuiID; // Unique ID used by widgets (typically hashed from a stack of string) -#ifndef ImWchar -#define ImWchar ImWchar16 -#endif -typedef unsigned short ImWchar16; // A single U16 character for keyboard input/display. We encode them as multi bytes UTF-8 when used in strings. -typedef unsigned int ImWchar32; // A single U32 character for keyboard input/display. Define ImWchar to ImWchar32 to use it. See imconfig.h . +// Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) +// - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! +// In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type @@ -168,15 +160,32 @@ typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: f typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() +typedef int ImGuiKeyModFlags; // -> enum ImGuiKeyModFlags_ // Flags: for io.KeyMods (Ctrl/Shift/Alt/Super) typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem() typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: for TreeNode(), TreeNodeEx(), CollapsingHeader() typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: for Begin(), BeginChild() + +// Other types +#ifndef ImTextureID // ImTextureID [configurable type: override in imconfig.h with '#define ImTextureID xxx'] +typedef void* ImTextureID; // User data for rendering back-end to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. +#endif +typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data); typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); -// Scalar data types +// Decoded character types +// (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) +typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. +typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. +#ifdef IMGUI_USE_WCHAR32 // ImWchar [configurable type: override in imconfig.h with '#define IMGUI_USE_WCHAR32' to support Unicode planes 1-16] +typedef ImWchar32 ImWchar; +#else +typedef ImWchar16 ImWchar; +#endif + +// Basic scalar data types typedef signed char ImS8; // 8-bit signed integer typedef unsigned char ImU8; // 8-bit unsigned integer typedef signed short ImS16; // 16-bit signed integer @@ -195,7 +204,7 @@ typedef signed long long ImS64; // 64-bit signed integer (post C++11) typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) #endif -// 2D vector (often used to store positions, sizes, etc.) +// 2D vector (often used to store positions or sizes) struct ImVec2 { float x, y; @@ -233,7 +242,6 @@ namespace ImGui IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = destroy current context IMGUI_API ImGuiContext* GetCurrentContext(); IMGUI_API void SetCurrentContext(ImGuiContext* ctx); - IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // Main IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) @@ -260,9 +268,10 @@ namespace ImGui // Windows // - Begin() = push window to the stack and start appending to it. End() = pop window from the stack. - // - You may append multiple times to the same window during the same frame. // - Passing 'bool* p_open != NULL' shows a window-closing widget in the upper-right corner of the window, // which clicking will set the boolean to false when clicked. + // - You may append multiple times to the same window during the same frame by calling Begin()/End() pairs multiple times. + // Some information such as 'flags' or 'p_open' will only be considered by the first call to Begin(). // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting // anything to the window. Always call a matching End() for each Begin() call, regardless of its return value! // [Important: due to legacy reason, this is inconsistent with most other functions such as BeginMenu/EndMenu, @@ -277,8 +286,8 @@ namespace ImGui // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400). // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. // Always call a matching EndChild() for each BeginChild() call, regardless of its return value [as with Begin: this is due to legacy reason and inconsistent with most BeginXXX functions apart from the regular Begin() which behaves like BeginChild().] - IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); - IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0,0), bool border = false, ImGuiWindowFlags flags = 0); + IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); + IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API void EndChild(); // Windows Utilities @@ -294,7 +303,7 @@ namespace ImGui IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) // Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). - IMGUI_API void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0,0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc. + IMGUI_API void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0, 0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc. IMGUI_API void SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0); // set next window size. set axis to 0.0f to force an auto-fit on this axis. call before Begin() IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use -1,-1 on either X/Y axis to preserve the current size. Sizes will be rounded down. Use callback to apply non-trivial programmatic constraints. IMGUI_API void SetNextWindowContentSize(const ImVec2& size); // set next window content size (~ scrollable client area, which enforce the range of scrollbars). Not including window decorations (title bar, menu bar, etc.) nor WindowPadding. set an axis to 0.0f to leave it automatic. call before Begin() @@ -302,7 +311,7 @@ namespace ImGui IMGUI_API void SetNextWindowFocus(); // set next window to be focused / top-most. call before Begin() IMGUI_API void SetNextWindowBgAlpha(float alpha); // set next window background color alpha. helper to easily override the Alpha component of ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground. IMGUI_API void SetWindowPos(const ImVec2& pos, ImGuiCond cond = 0); // (not recommended) set current window position - call within Begin()/End(). prefer using SetNextWindowPos(), as this may incur tearing and side-effects. - IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0,0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. + IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0, 0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed(). IMGUI_API void SetWindowFocus(); // (not recommended) set current window to be focused / top-most. prefer using SetNextWindowFocus(). IMGUI_API void SetWindowFontScale(float scale); // set font scale. Adjust IO.FontGlobalScale if you want to scale all windows. This is an old API! For correct scaling, prefer to reload font + rebuild ImFontAtlas + call style.ScaleAllSizes(). @@ -363,7 +372,7 @@ namespace ImGui // Cursor / Layout // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. - // - You can call SameLine() between widgets to undo the last carriage return and output at the right of the preceeding widget. + // - You can call SameLine() between widgets to undo the last carriage return and output at the right of the preceding widget. // - Attention! We currently have inconsistencies between window-local and absolute positions we will aim to fix with future API: // Window-local coordinates: SameLine(), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), GetContentRegionMax(), GetWindowContentRegion*(), PushTextWrapPos() // Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. @@ -425,17 +434,17 @@ namespace ImGui // Widgets: Main // - Most widgets return true when the value has been changed or when pressed/selected // - You may also use one of the many IsItemXXX functions (e.g. IsItemActive, IsItemHovered, etc.) to query widget state. - IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0,0)); // button + IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0, 0)); // button IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size); // button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape - IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); - IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding + IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); + IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding IMGUI_API bool Checkbox(const char* label, bool* v); IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer - IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1,0), const char* overlay = NULL); + IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1, 0), const char* overlay = NULL); IMGUI_API void Bullet(); // draw a small circle and keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses // Widgets: Combo Box @@ -490,7 +499,7 @@ namespace ImGui // - If you want to use InputText() with std::string or any custom dynamic string type, see misc/cpp/imgui_stdlib.h and comments in imgui_demo.cpp. // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); - IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0,0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); IMGUI_API bool InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); IMGUI_API bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, const char* format = "%.3f", ImGuiInputTextFlags flags = 0); IMGUI_API bool InputFloat2(const char* label, float v[2], const char* format = "%.3f", ImGuiInputTextFlags flags = 0); @@ -511,7 +520,7 @@ namespace ImGui IMGUI_API bool ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL); - IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0,0)); // display a colored square/button, hover for details, return true when pressed. + IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0, 0)); // display a colored square/button, hover for details, return true when pressed. IMGUI_API void SetColorEditOptions(ImGuiColorEditFlags flags); // initialize current options (generally on application startup) if you want to select a default format, picker type, etc. User will be able to change many settings, unless you pass the _NoOptions flag to your calls. // Widgets: Trees @@ -537,14 +546,14 @@ namespace ImGui // Widgets: Selectables // - A selectable highlights when hovered, and can display another color when selected. // - Neighbors selectable extend their highlight bounds in order to leave no gap between them. This is so a series of selected Selectable appear contiguous. - IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool selected" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height - IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0,0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. + IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool selected" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height + IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. // Widgets: List Boxes // - FIXME: To be consistent with all the newer API, ListBoxHeader/ListBoxFooter should in reality be called BeginListBox/EndListBox. Will rename them. IMGUI_API bool ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1); IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); - IMGUI_API bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0,0)); // use if you want to reimplement ListBox() will custom data or interactions. if the function return true, you can output elements then call ListBoxFooter() afterwards. + IMGUI_API bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)); // use if you want to reimplement ListBox() will custom data or interactions. if the function return true, you can output elements then call ListBoxFooter() afterwards. IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // " IMGUI_API void ListBoxFooter(); // terminate the scrolling region. only call ListBoxFooter() if ListBoxHeader() returned true! @@ -583,13 +592,16 @@ namespace ImGui // Popups, Modals // The properties of popups windows are: - // - They block normal mouse hovering detection outside them. (*) + // - They block normal mouse hovering detection outside them. (*1) // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. - // - Their visibility state (~bool) is held internally by imgui instead of being held by the programmer as we are used to with regular Begin() calls. - // User can manipulate the visibility state by calling OpenPopup(). + // Because hovering detection is disabled outside the popup, when clicking outside the click will not be seen by underlying widgets! (*1) + // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as we are used to with regular Begin() calls. + // User can manipulate the visibility state by calling OpenPopup(), CloseCurrentPopup() etc. // - We default to use the right mouse (ImGuiMouseButton_Right=1) for the Popup Context functions. - // (*) You can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. - // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. + // Those three properties are connected: we need to retain popup visibility state in the library because popups may be closed as any time. + // (*1) You can bypass that restriction and detect hovering even when normally blocked by a popup. + // To do this use the ImGuiHoveredFlags_AllowWhenBlockedByPopup when calling IsItemHovered() or IsWindowHovered(). + // This is what BeginPopupContextItem() and BeginPopupContextWindow() are doing already, allowing a right-click to reopen another popups without losing the click. IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). popups are closed when user click outside, or if CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. By default, Selectable()/MenuItem() are calling CloseCurrentPopup(). Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. only call EndPopup() if BeginPopup() returns true! IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! @@ -632,7 +644,8 @@ namespace ImGui IMGUI_API void LogText(const char* fmt, ...) IM_FMTARGS(1); // pass text data straight to log (without being displayed) // Drag and Drop - // [BETA API] API may evolve! + // - [BETA API] API may evolve! + // - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip as replacement) IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource() IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. IMGUI_API void EndDragDropSource(); // only call EndDragDropSource() if BeginDragDropSource() returns true! @@ -725,7 +738,8 @@ namespace ImGui IMGUI_API void SetMouseCursor(ImGuiMouseCursor cursor_type); // set desired cursor type IMGUI_API void CaptureMouseFromApp(bool want_capture_mouse_value = true); // attention: misleading name! manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application to handle). This is equivalent to setting "io.WantCaptureMouse = want_capture_mouse_value;" after the next NewFrame() call. - // Clipboard Utilities (also see the LogToClipboard() function to capture or output text data to the clipboard) + // Clipboard Utilities + // - Also see the LogToClipboard() function to capture GUI into clipboard, or easily output text data to the clipboard. IMGUI_API const char* GetClipboardText(); IMGUI_API void SetClipboardText(const char* text); @@ -737,6 +751,9 @@ namespace ImGui IMGUI_API void SaveIniSettingsToDisk(const char* ini_filename); // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext). IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. + // Debug Utilities + IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. + // Memory Allocators // - All those functions are not reliant on the current context. // - If you reload the contents of imgui.cpp at runtime, you may need to call SetCurrentContext() + SetAllocatorFunctions() again because we use global storage for those. @@ -898,12 +915,12 @@ enum ImGuiFocusedFlags_ ImGuiFocusedFlags_None = 0, ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy) - ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use ImGui::GetIO().WantCaptureMouse instead. + ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use 'io.WantCaptureMouse' instead! Please read the FAQ! ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows }; // Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered() -// Note: if you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that. Please read the FAQ! +// Note: if you are trying to check whether your mouse should be dispatched to Dear ImGui or to your app, you should use 'io.WantCaptureMouse' instead! Please read the FAQ! // Note: windows with the ImGuiWindowFlags_NoInputs flag are ignored by IsWindowHovered() calls. enum ImGuiHoveredFlags_ { @@ -997,7 +1014,17 @@ enum ImGuiKey_ ImGuiKey_COUNT }; -// Gamepad/Keyboard directional navigation +// To test io.KeyMods (which is a combination of individual fields io.KeyCtrl, io.KeyShift, io.KeyAlt set by user/back-end) +enum ImGuiKeyModFlags_ +{ + ImGuiKeyModFlags_None = 0, + ImGuiKeyModFlags_Ctrl = 1 << 0, + ImGuiKeyModFlags_Shift = 1 << 1, + ImGuiKeyModFlags_Alt = 1 << 2, + ImGuiKeyModFlags_Super = 1 << 3 +}; + +// Gamepad/Keyboard navigation // Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. // Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Back-end: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). // Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW. @@ -1119,8 +1146,12 @@ enum ImGuiCol_ }; // Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure. -// NB: the enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. During initialization, feel free to just poke into ImGuiStyle directly. -// NB: if changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type. +// - The enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. +// During initialization or between frames, feel free to just poke into ImGuiStyle directly. +// - Tip: Use your programming IDE navigation facilities on the names in the _second column_ below to find the actual members and their description. +// In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - When changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type. enum ImGuiStyleVar_ { // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) @@ -1239,7 +1270,7 @@ enum ImGuiMouseCursor_ enum ImGuiCond_ { ImGuiCond_Always = 1 << 0, // Set the variable - ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call with succeed) + ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call will succeed) ImGuiCond_FirstUseEver = 1 << 2, // Set the variable if the object/window has no persistently saved data (no entry in .ini file) ImGuiCond_Appearing = 1 << 3 // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) }; @@ -1363,9 +1394,10 @@ struct ImGuiStyle float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. + float TabMinWidthForUnselectedCloseButton; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). - ImVec2 SelectableTextAlign; // Alignment of selectable text when selectable is larger than text. Defaults to (0.0f, 0.0f) (top-left aligned). + ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows. ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. @@ -1456,8 +1488,8 @@ struct ImGuiIO // Input - Fill before calling NewFrame() //------------------------------------------------------------------ - ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX,-FLT_MAX) if mouse is unavailable (on another screen, etc.) - bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras. ImGui itself mostly only uses left button (BeginPopupContext** are using right button). Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. + ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.) + bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends. bool KeyCtrl; // Keyboard modifier pressed: Control @@ -1474,17 +1506,19 @@ struct ImGuiIO IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually //------------------------------------------------------------------ - // Output - Retrieve after calling NewFrame() + // Output - Updated by NewFrame() or EndFrame()/Render() + // (when reading from the io.WantCaptureMouse, io.WantCaptureKeyboard flags to dispatch your inputs, it is + // generally easier and more correct to use their state BEFORE calling NewFrame(). See FAQ for details!) //------------------------------------------------------------------ - bool WantCaptureMouse; // When io.WantCaptureMouse is true, imgui will use the mouse inputs, do not dispatch them to your main game/application (in both cases, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). - bool WantCaptureKeyboard; // When io.WantCaptureKeyboard is true, imgui will use the keyboard inputs, do not dispatch them to your main game/application (in both cases, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). - bool WantTextInput; // Mobile/console: when io.WantTextInput is true, you may display an on-screen keyboard. This is set by ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). - bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. - bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. IMPORTANT: You need to clear io.WantSaveIniSettings yourself. - bool NavActive; // Directional navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. - bool NavVisible; // Directional navigation is visible and allowed (will handle ImGuiKey_NavXXX events). - float Framerate; // Application framerate estimation, in frame per second. Solely for convenience. Rolling average estimation based on IO.DeltaTime over 120 frames + bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). + bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). + bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). + bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. + bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! + bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. + bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). + float Framerate; // Application framerate estimate, in frame per second. Solely for convenience. Rolling average estimation based on io.DeltaTime over 120 frames. int MetricsRenderVertices; // Vertices output during last call to Render() int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 int MetricsRenderWindows; // Number of visible windows @@ -1496,6 +1530,7 @@ struct ImGuiIO // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ + ImGuiKeyModFlags KeyMods; // Key mods flags (same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags), updated by NewFrame() ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) ImVec2 MouseClickedPos[5]; // Position at time of clicking double MouseClickedTime[5]; // Time of last click (used to figure out double-click) @@ -1627,6 +1662,14 @@ typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; // Helpers //----------------------------------------------------------------------------- +// Helper: Unicode defines +#define IM_UNICODE_CODEPOINT_INVALID 0xFFFD // Invalid Unicode code point (standard value). +#ifdef IMGUI_USE_WCHAR32 +#define IM_UNICODE_CODEPOINT_MAX 0x10FFFF // Maximum Unicode code point supported by this build. +#else +#define IM_UNICODE_CODEPOINT_MAX 0xFFFF // Maximum Unicode code point supported by this build. +#endif + // Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. // Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); struct ImGuiOnceUponAFrame @@ -1813,7 +1856,7 @@ struct ImColor // Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. //----------------------------------------------------------------------------- -// Draw callbacks for advanced uses. +// ImDrawCallback: Draw callbacks for advanced uses [configurable type: override in imconfig.h] // NB: You most likely do NOT need to use draw callbacks just to create your own widget or customized UI rendering, // you can poke into the draw list for that! Draw callback may be useful for example to: // A) Change your GPU render state, @@ -1846,9 +1889,9 @@ struct ImDrawCmd ImDrawCmd() { ElemCount = 0; TextureId = (ImTextureID)NULL; VtxOffset = IdxOffset = 0; UserCallback = NULL; UserCallbackData = NULL; } }; -// Vertex index -// (to allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end) -// (to use 32-bit indices: override with '#define ImDrawIdx unsigned int' in imconfig.h) +// Vertex index, default to 16-bit +// To allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end (recommended). +// To use 32-bit indices: override with '#define ImDrawIdx unsigned int' in imconfig.h. #ifndef ImDrawIdx typedef unsigned short ImDrawIdx; #endif @@ -2110,13 +2153,13 @@ struct ImFontGlyphRangesBuilder // See ImFontAtlas::AddCustomRectXXX functions. struct ImFontAtlasCustomRect { - unsigned int ID; // Input // User ID. Use < 0x110000 to map into a font glyph, >= 0x110000 for other/internal/custom texture data. unsigned short Width, Height; // Input // Desired rectangle dimension unsigned short X, Y; // Output // Packed position in Atlas - float GlyphAdvanceX; // Input // For custom font glyphs only (ID < 0x110000): glyph xadvance - ImVec2 GlyphOffset; // Input // For custom font glyphs only (ID < 0x110000): glyph display offset - ImFont* Font; // Input // For custom font glyphs only (ID < 0x110000): target font - ImFontAtlasCustomRect() { ID = 0xFFFFFFFF; Width = Height = 0; X = Y = 0xFFFF; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0,0); Font = NULL; } + unsigned int GlyphID; // Input // For custom font glyphs only (ID < 0x110000) + float GlyphAdvanceX; // Input // For custom font glyphs only: glyph xadvance + ImVec2 GlyphOffset; // Input // For custom font glyphs only: glyph display offset + ImFont* Font; // Input // For custom font glyphs only: target font + ImFontAtlasCustomRect() { Width = Height = 0; X = Y = 0xFFFF; GlyphID = 0; GlyphAdvanceX = 0.0f; GlyphOffset = ImVec2(0, 0); Font = NULL; } bool IsPacked() const { return X != 0xFFFF; } }; @@ -2195,8 +2238,9 @@ struct ImFontAtlas // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), // so you can render e.g. custom colorful icons and use them as regular glyphs. // Read docs/FONTS.txt for more details about using colorful icons. - IMGUI_API int AddCustomRectRegular(unsigned int id, int width, int height); // Id needs to be >= 0x110000. Id >= 0x80000000 are reserved for ImGui and ImDrawList - IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0,0)); // Id needs to be < 0x110000 to register a rectangle to map into a specific font. + // Note: this API may be redesigned later in order to support multi-monitor varying DPI settings. + IMGUI_API int AddCustomRectRegular(int width, int height); + IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0)); const ImFontAtlasCustomRect*GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } // [Internal] @@ -2257,7 +2301,7 @@ struct ImFont float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] int MetricsTotalSurface;// 4 // out // // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs) - ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations accross all used codepoints. + ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints. // Methods IMGUI_API ImFont(); diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 2664cb14..8cece7be 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,41 +1,42 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (demo code) // Help: // - Read FAQ at http://dearimgui.org/faq // - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. -// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp for demo code. All applications in examples/ are doing that. +// - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. // Read imgui.cpp for more details, documentation and comments. // Get latest version at https://github.com/ocornut/imgui // Message to the person tempted to delete this file when integrating Dear ImGui into their code base: -// Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other coders -// will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of -// your game/app! Removing this file from your project is hindering access to documentation for everyone in your team, -// likely leading you to poorer usage of the library. +// Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other +// coders will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available +// debug menu of your game/app! Removing this file from your project is hindering access to documentation for everyone +// in your team, likely leading you to poorer usage of the library. // Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow(). -// If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be linked, -// you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. +// If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be +// linked, you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. // In other situation, whenever you have Dear ImGui available you probably want this to be available for reference. // Thank you, // -Your beloved friend, imgui_demo.cpp (which you won't delete) // Message to beginner C/C++ programmers about the meaning of the 'static' keyword: -// In this demo code, we frequently we use 'static' variables inside functions. A static variable persist across calls, so it is -// essentially like a global variable but declared inside the scope of the function. We do this as a way to gather code and data -// in the same place, to make the demo source code faster to read, faster to write, and smaller in size. -// It also happens to be a convenient way of storing simple UI related information as long as your function doesn't need to be -// reentrant or used in multiple threads. This might be a pattern you will want to use in your code, but most of the real data -// you would be editing is likely going to be stored outside your functions. +// In this demo code, we frequently we use 'static' variables inside functions. A static variable persist across calls, +// so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to +// gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller +// in size. It also happens to be a convenient way of storing simple UI related information as long as your function +// doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code, +// but most of the real data you would be editing is likely going to be stored outside your functions. // The Demo code in this file is designed to be easy to copy-and-paste in into your application! // Because of this: -// - We never omit the ImGui:: namespace when calling functions, even though most of our code is already in the same namespace. +// - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace. // - We try to declare static variables in the local scope, as close as possible to the code using them. -// - We never use any of the helpers/facilities used internally by Dear ImGui, unless it has been exposed in the public API (imgui.h). -// - We never use maths operators on ImVec2/ImVec4. For other of our sources files, they are provided by imgui_internal.h w/ IMGUI_DEFINE_MATH_OPERATORS. -// For your own sources file they are optional and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. -// Because we don't want to assume anything about your support of maths operators, we don't use them in imgui_demo.cpp. +// - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API. +// - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided +// by imgui_internal.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional +// and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. +// Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. /* @@ -82,37 +83,38 @@ Index of this file: #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #endif #if defined(__clang__) -#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. -#pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning : 'xx' is deprecated: The POSIX name for this item.. // for strdup used in demo code (so user can copy & paste the code) -#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' -#pragma clang diagnostic ignored "-Wformat-security" // warning : warning: format string is not a string literal -#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning : declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. -#pragma clang diagnostic ignored "-Wunused-macros" // warning : warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning: 'xx' is deprecated: The POSIX name for this.. // for strdup used in demo code (so user can copy & paste the code) +#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning: cast to 'void *' from smaller integer type +#pragma clang diagnostic ignored "-Wformat-security" // warning: format string is not a string literal +#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning: declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. +#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. #if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 #endif #if __has_warning("-Wdouble-promotion") -#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #endif #if __has_warning("-Wreserved-id-macro") -#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning : macro name is a reserved identifier // +#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier #endif #elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind -#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size -#pragma GCC diagnostic ignored "-Wformat-security" // warning : format string is not a string literal (potentially insecure) -#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function -#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value -#pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size +#pragma GCC diagnostic ignored "-Wformat-security" // warning: format string is not a string literal (potentially insecure) +#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function +#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value +#pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. #endif -// Play it nice with Windows users (Update: since 2018-05, Notepad finally appears to support Unix-style carriage returns!) +// Play it nice with Windows users (Update: May 2018, Notepad now supports Unix-style carriage returns!) #ifdef _WIN32 #define IM_NEWLINE "\r\n" #else #define IM_NEWLINE "\n" #endif +// Helpers #if defined(_MSC_VER) && !defined(snprintf) #define snprintf _snprintf #endif @@ -120,6 +122,14 @@ Index of this file: #define vsnprintf _vsnprintf #endif +// Helpers macros +// We normally try to not use many helpers in imgui_demo.cpp in order to make code easier to copy and paste, +// but making an exception here as those are largely simplifying code... +// In other imgui sources we can use nicer internal functions from imgui_internal.h (ImMin/ImMax) but not in the demo. +#define IM_MIN(A, B) (((A) < (B)) ? (A) : (B)) +#define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B)) +#define IM_CLAMP(V, MN, MX) ((V) < (MN) ? (MN) : (V) > (MX) ? (MX) : (V)) + //----------------------------------------------------------------------------- // [SECTION] Forward Declarations, Helpers //----------------------------------------------------------------------------- @@ -161,7 +171,9 @@ void ImGui::ShowUserGuide() { ImGuiIO& io = ImGui::GetIO(); ImGui::BulletText("Double-click on title bar to collapse window."); - ImGui::BulletText("Click and drag on lower corner to resize window\n(double-click to auto fit window to its contents)."); + ImGui::BulletText( + "Click and drag on lower corner to resize window\n" + "(double-click to auto fit window to its contents)."); ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); if (io.FontAllowUserScaling) @@ -196,7 +208,8 @@ void ImGui::ShowUserGuide() // - ShowDemoWindowMisc() //----------------------------------------------------------------------------- -// We split the contents of the big ShowDemoWindow() function into smaller functions (because the link time of very large functions grow non-linearly) +// We split the contents of the big ShowDemoWindow() function into smaller functions +// (because the link time of very large functions grow non-linearly) static void ShowDemoWindowWidgets(); static void ShowDemoWindowLayout(); static void ShowDemoWindowPopups(); @@ -204,10 +217,13 @@ static void ShowDemoWindowColumns(); static void ShowDemoWindowMisc(); // Demonstrate most Dear ImGui features (this is big function!) -// You may execute this function to experiment with the UI and understand what it does. You may then search for keywords in the code when you are interested by a specific feature. +// You may execute this function to experiment with the UI and understand what it does. +// You may then search for keywords in the code when you are interested by a specific feature. void ImGui::ShowDemoWindow(bool* p_open) { - IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); // Exceptionally add an extra assert here for people confused with initial dear imgui setup + // Exceptionally add an extra assert here for people confused about initial Dear ImGui setup + // Most ImGui functions would normally just crash if the context is missing. + IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); // Examples Apps (accessible from the "Examples" menu) static bool show_app_documents = false; @@ -241,9 +257,14 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool show_app_style_editor = false; static bool show_app_about = false; - if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } - if (show_app_style_editor) { ImGui::Begin("Style Editor", &show_app_style_editor); ImGui::ShowStyleEditor(); ImGui::End(); } - if (show_app_about) { ImGui::ShowAboutWindow(&show_app_about); } + if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } + if (show_app_about) { ImGui::ShowAboutWindow(&show_app_about); } + if (show_app_style_editor) + { + ImGui::Begin("Dear ImGui Style Editor", &show_app_style_editor); + ImGui::ShowStyleEditor(); + ImGui::End(); + } // Demonstrate the various window flags. Typically you would just use the default! static bool no_titlebar = false; @@ -269,7 +290,8 @@ void ImGui::ShowDemoWindow(bool* p_open) if (no_bring_to_front) window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus; if (no_close) p_open = NULL; // Don't pass our bool* to Begin - // We specify a default position/size in case there's no data in the .ini file. Typically this isn't required! We only do it to make the Demo applications a little more welcoming. + // We specify a default position/size in case there's no data in the .ini file. + // We only do it to make the demo applications a little more welcoming, but typically this isn't required. ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver); @@ -281,9 +303,13 @@ void ImGui::ShowDemoWindow(bool* p_open) return; } - // Most "big" widgets share a common width settings by default. - //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); // Use 2/3 of the space for widgets and 1/3 for labels (default) - ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // Use fixed width for labels (by passing a negative value), the rest goes to widgets. We choose a width proportional to our font size. + // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details. + + // e.g. Use 2/3 of the space for widgets and 1/3 for labels (default) + //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); + + // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets. + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // Menu Bar if (ImGui::BeginMenuBar()) @@ -350,13 +376,15 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Configuration##2")) { - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); ImGui::SameLine(); HelpMarker("Required back-end to feed in gamepad inputs in io.NavInputs[] and set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); - ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); - if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) // Create a way to restore this flag otherwise we could be stuck completely! + ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); + + // The "NoMouse" option above can get us stuck with a disable mouse! Provide an alternative way to fix it: + if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) { if (fmodf((float)ImGui::GetTime(), 0.40f) < 0.20f) { @@ -374,18 +402,22 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); - ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor for you. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); ImGui::TreePop(); ImGui::Separator(); } if (ImGui::TreeNode("Backend Flags")) { - HelpMarker("Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities.\nHere we expose then as read-only fields to avoid breaking interactions with your back-end."); - ImGuiBackendFlags backend_flags = io.BackendFlags; // Make a local copy to avoid modifying actual back-end flags. - ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasGamepad); - ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasMouseCursors); - ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasSetMousePos); + HelpMarker( + "Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities.\n" + "Here we expose then as read-only fields to avoid breaking interactions with your back-end."); + + // Make a local copy to avoid modifying actual back-end flags. + ImGuiBackendFlags backend_flags = io.BackendFlags; + ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasGamepad); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasMouseCursors); + ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasSetMousePos); ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", (unsigned int *)&backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); ImGui::TreePop(); ImGui::Separator(); @@ -401,10 +433,13 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Capture/Logging")) { - ImGui::TextWrapped("The logging API redirects all text output so you can easily capture the content of a window or a block. Tree nodes can be automatically expanded."); - HelpMarker("Try opening any of the contents below in this window and then click one of the \"Log To\" button."); + HelpMarker( + "The logging API redirects all text output so you can easily capture the content of " + "a window or a block. Tree nodes can be automatically expanded.\n" + "Try opening any of the contents below in this window and then click one of the \"Log To\" button."); ImGui::LogButtons(); - ImGui::TextWrapped("You can also call ImGui::LogText() to output directly to the log without a visual output."); + + HelpMarker("You can also call ImGui::LogText() to output directly to the log without a visual output."); if (ImGui::Button("Copy \"Hello, world!\" to clipboard")) { ImGui::LogToClipboard(); @@ -478,7 +513,9 @@ static void ShowDemoWindowWidgets() ImGui::PopID(); } - // Use AlignTextToFramePadding() to align text baseline to the baseline of framed elements (otherwise a Text+SameLine+Button sequence will have the text a little too high by default) + // Use AlignTextToFramePadding() to align text baseline to the baseline of framed widgets elements + // (otherwise a Text+SameLine+Button sequence will have the text a little too high by default!) + // See 'Demo->Layout->Text Baseline Alignment' for details. ImGui::AlignTextToFramePadding(); ImGui::Text("Hold to repeat:"); ImGui::SameLine(); @@ -516,7 +553,7 @@ static void ShowDemoWindowWidgets() { // Using the _simplified_ one-liner Combo() api here // See "Combo" section for examples of how to use the more complete BeginCombo()/EndCombo() api. - const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; static int item_current = 0; ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); ImGui::SameLine(); HelpMarker("Refer to the \"Combo\" section below for an explanation of the full BeginCombo/EndCombo API, and demonstration of various flags.\n"); @@ -527,14 +564,28 @@ static void ShowDemoWindowWidgets() // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. static char str0[128] = "Hello, world!"; ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0)); - ImGui::SameLine(); HelpMarker("USER:\nHold SHIFT or use mouse to select text.\n" "CTRL+Left/Right to word jump.\n" "CTRL+A or double-click to select all.\n" "CTRL+X,CTRL+C,CTRL+V clipboard.\n" "CTRL+Z,CTRL+Y undo/redo.\n" "ESCAPE to revert.\n\nPROGRAMMER:\nYou can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated in imgui_demo.cpp)."); + ImGui::SameLine(); HelpMarker( + "USER:\n" + "Hold SHIFT or use mouse to select text.\n" + "CTRL+Left/Right to word jump.\n" + "CTRL+A or double-click to select all.\n" + "CTRL+X,CTRL+C,CTRL+V clipboard.\n" + "CTRL+Z,CTRL+Y undo/redo.\n" + "ESCAPE to revert.\n\n" + "PROGRAMMER:\n" + "You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputText() " + "to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example (this is not demonstrated " + "in imgui_demo.cpp)."); static char str1[128] = ""; ImGui::InputTextWithHint("input text (w/ hint)", "enter text here", str1, IM_ARRAYSIZE(str1)); static int i0 = 123; ImGui::InputInt("input int", &i0); - ImGui::SameLine(); HelpMarker("You can apply arithmetic operators +,*,/ on numerical values.\n e.g. [ 100 ], input \'*2\', result becomes [ 200 ]\nUse +- to subtract.\n"); + ImGui::SameLine(); HelpMarker( + "You can apply arithmetic operators +,*,/ on numerical values.\n" + " e.g. [ 100 ], input \'*2\', result becomes [ 200 ]\n" + "Use +- to subtract."); static float f0 = 0.001f; ImGui::InputFloat("input float", &f0, 0.01f, 1.0f, "%.3f"); @@ -544,7 +595,9 @@ static void ShowDemoWindowWidgets() static float f1 = 1.e10f; ImGui::InputFloat("input scientific", &f1, 0.0f, 0.0f, "%e"); - ImGui::SameLine(); HelpMarker("You can input value using the scientific notation,\n e.g. \"1e+8\" becomes \"100000000\".\n"); + ImGui::SameLine(); HelpMarker( + "You can input value using the scientific notation,\n" + " e.g. \"1e+8\" becomes \"100000000\"."); static float vec4a[4] = { 0.10f, 0.20f, 0.30f, 0.44f }; ImGui::InputFloat3("input float3", vec4a); @@ -553,7 +606,10 @@ static void ShowDemoWindowWidgets() { static int i1 = 50, i2 = 42; ImGui::DragInt("drag int", &i1, 1); - ImGui::SameLine(); HelpMarker("Click and drag to edit value.\nHold SHIFT/ALT for faster/slower edit.\nDouble-click or CTRL+click to input value."); + ImGui::SameLine(); HelpMarker( + "Click and drag to edit value.\n" + "Hold SHIFT/ALT for faster/slower edit.\n" + "Double-click or CTRL+click to input value."); ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%"); @@ -578,10 +634,10 @@ static void ShowDemoWindowWidgets() // Here we completely omit '%d' from the format string, so it'll only display a name. // This technique can also be used with DragInt(). enum Element { Element_Fire, Element_Earth, Element_Air, Element_Water, Element_COUNT }; - const char* element_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" }; - static int current_element = Element_Fire; - const char* current_element_name = (current_element >= 0 && current_element < Element_COUNT) ? element_names[current_element] : "Unknown"; - ImGui::SliderInt("slider enum", ¤t_element, 0, Element_COUNT - 1, current_element_name); + static int elem = Element_Fire; + const char* elems_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" }; + const char* elem_name = (elem >= 0 && elem < Element_COUNT) ? elems_names[elem] : "Unknown"; + ImGui::SliderInt("slider enum", &elem, 0, Element_COUNT - 1, elem_name); ImGui::SameLine(); HelpMarker("Using the format string parameter to display a name instead of the underlying integer."); } @@ -589,16 +645,20 @@ static void ShowDemoWindowWidgets() static float col1[3] = { 1.0f,0.0f,0.2f }; static float col2[4] = { 0.4f,0.7f,0.0f,0.5f }; ImGui::ColorEdit3("color 1", col1); - ImGui::SameLine(); HelpMarker("Click on the colored square to open a color picker.\nClick and hold to use drag and drop.\nRight-click on the colored square to show options.\nCTRL+click on individual component to input value.\n"); + ImGui::SameLine(); HelpMarker( + "Click on the colored square to open a color picker.\n" + "Click and hold to use drag and drop.\n" + "Right-click on the colored square to show options.\n" + "CTRL+click on individual component to input value.\n"); ImGui::ColorEdit4("color 2", col2); } { // List box - const char* listbox_items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; - static int listbox_item_current = 1; - ImGui::ListBox("listbox\n(single select)", &listbox_item_current, listbox_items, IM_ARRAYSIZE(listbox_items), 4); + const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; + static int item_current = 1; + ImGui::ListBox("listbox\n(single select)", &item_current, items, IM_ARRAYSIZE(items), 4); //static int listbox_item_current2 = 2; //ImGui::SetNextItemWidth(-1); @@ -620,8 +680,8 @@ static void ShowDemoWindowWidgets() { for (int i = 0; i < 5; i++) { - // Use SetNextItemOpen() so set the default state of a node to be open. - // We could also use TreeNodeEx() with the ImGuiTreeNodeFlags_DefaultOpen flag to achieve the same thing! + // Use SetNextItemOpen() so set the default state of a node to be open. We could + // also use TreeNodeEx() with the ImGuiTreeNodeFlags_DefaultOpen flag to achieve the same thing! if (i == 0) ImGui::SetNextItemOpen(true, ImGuiCond_Once); @@ -638,23 +698,31 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Advanced, with Selectable nodes")) { - HelpMarker("This is a more typical looking tree with selectable nodes.\nClick to select, CTRL+Click to toggle, click on arrows or double-click to open."); + HelpMarker( + "This is a more typical looking tree with selectable nodes.\n" + "Click to select, CTRL+Click to toggle, click on arrows or double-click to open."); static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth; static bool align_label_with_current_x_position = false; - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnArrow); + static bool test_drag_and_drop = false; + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnArrow); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanFullWidth); - ImGui::Checkbox("Align label with current X position)", &align_label_with_current_x_position); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node."); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position); + ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop); ImGui::Text("Hello!"); if (align_label_with_current_x_position) ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing()); - static int selection_mask = (1 << 2); // Dumb representation of what may be user-side selection state. You may carry selection state inside or outside your objects in whatever format you see fit. - int node_clicked = -1; // Temporary storage of what node we have clicked to process selection at the end of the loop. May be a pointer to your own node type, etc. + // 'selection_mask' is dumb representation of what may be user-side selection state. + // You may retain selection state inside or outside your objects in whatever format you see fit. + // 'node_clicked' is temporary storage of what node we have clicked to process selection at the end + /// of the loop. May be a pointer to your own node type, etc. + static int selection_mask = (1 << 2); + int node_clicked = -1; for (int i = 0; i < 6; i++) { - // Disable the default open on single-click behavior and pass in Selected flag according to our selection state. + // Disable the default "open on single-click behavior" + set Selected flag according to our selection. ImGuiTreeNodeFlags node_flags = base_flags; const bool is_selected = (selection_mask & (1 << i)) != 0; if (is_selected) @@ -665,6 +733,12 @@ static void ShowDemoWindowWidgets() bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i); if (ImGui::IsItemClicked()) node_clicked = i; + if (test_drag_and_drop && ImGui::BeginDragDropSource()) + { + ImGui::SetDragDropPayload("_TREENODE", NULL, 0); + ImGui::Text("This is a drag and drop source"); + ImGui::EndDragDropSource(); + } if (node_open) { ImGui::BulletText("Blah blah\nBlah Blah"); @@ -674,20 +748,27 @@ static void ShowDemoWindowWidgets() else { // Items 3..5 are Tree Leaves - // The only reason we use TreeNode at all is to allow selection of the leaf. - // Otherwise we can use BulletText() or advance the cursor by GetTreeNodeToLabelSpacing() and call Text(). + // The only reason we use TreeNode at all is to allow selection of the leaf. Otherwise we can + // use BulletText() or advance the cursor by GetTreeNodeToLabelSpacing() and call Text(). node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Leaf %d", i); if (ImGui::IsItemClicked()) node_clicked = i; + if (test_drag_and_drop && ImGui::BeginDragDropSource()) + { + ImGui::SetDragDropPayload("_TREENODE", NULL, 0); + ImGui::Text("This is a drag and drop source"); + ImGui::EndDragDropSource(); + } } } if (node_clicked != -1) { - // Update selection state. Process outside of tree loop to avoid visual inconsistencies during the clicking-frame. + // Update selection state + // (process outside of tree loop to avoid visual inconsistencies during the clicking frame) if (ImGui::GetIO().KeyCtrl) selection_mask ^= (1 << node_clicked); // CTRL+click to toggle - else //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, this commented bit preserve selection when clicking on item that is part of the selection + else //if (!(selection_mask & (1 << node_clicked))) // Depending on selection behavior you want, may want to preserve selection when clicking on item that is part of the selection selection_mask = (1 << node_clicked); // Click to single-select } if (align_label_with_current_x_position) @@ -755,21 +836,24 @@ static void ShowDemoWindowWidgets() static float wrap_width = 200.0f; ImGui::SliderFloat("Wrap width", &wrap_width, -20, 600, "%.0f"); - ImGui::Text("Test paragraph 1:"); - ImVec2 pos = ImGui::GetCursorScreenPos(); - ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), IM_COL32(255,0,255,255)); - ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); - ImGui::Text("The lazy dog is a good dog. This paragraph is made to fit within %.0f pixels. Testing a 1 character word. The quick brown fox jumps over the lazy dog.", wrap_width); - ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255,255,0,255)); - ImGui::PopTextWrapPos(); - - ImGui::Text("Test paragraph 2:"); - pos = ImGui::GetCursorScreenPos(); - ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(pos.x + wrap_width, pos.y), ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()), IM_COL32(255,0,255,255)); - ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); - ImGui::Text("aaaaaaaa bbbbbbbb, c cccccccc,dddddddd. d eeeeeeee ffffffff. gggggggg!hhhhhhhh"); - ImGui::GetWindowDrawList()->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255,255,0,255)); - ImGui::PopTextWrapPos(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + for (int n = 0; n < 2; n++) + { + ImGui::Text("Test paragraph %d:", n); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImVec2 marker_min = ImVec2(pos.x + wrap_width, pos.y); + ImVec2 marker_max = ImVec2(pos.x + wrap_width + 10, pos.y + ImGui::GetTextLineHeight()); + ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); + if (n == 0) + ImGui::Text("The lazy dog is a good dog. This paragraph should fit within %.0f pixels. Testing a 1 character word. The quick brown fox jumps over the lazy dog.", wrap_width); + if (n == 1) + ImGui::Text("aaaaaaaa bbbbbbbb, c cccccccc,dddddddd. d eeeeeeee ffffffff. gggggggg!hhhhhhhh"); + + // Draw actual text bounding box, following by marker of our expected limit (should not overlap!) + draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), IM_COL32(255, 255, 0, 255)); + draw_list->AddRectFilled(marker_min, marker_max, IM_COL32(255, 0, 255, 255)); + ImGui::PopTextWrapPos(); + } ImGui::TreePop(); } @@ -777,14 +861,19 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("UTF-8 Text")) { // UTF-8 test with Japanese characters - // (Needs a suitable font, try Noto, or Arial Unicode, or M+ fonts. Read docs/FONTS.txt for details.) + // (Needs a suitable font? Try "Google Noto" or "Arial Unicode". See docs/FONTS.txt for details.) // - From C++11 you can use the u8"my text" syntax to encode literal strings as UTF-8 - // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. Visual Studio save your file as 'UTF-8 without signature') - // - FOR THIS DEMO FILE ONLY, BECAUSE WE WANT TO SUPPORT OLD COMPILERS, WE ARE *NOT* INCLUDING RAW UTF-8 CHARACTERS IN THIS SOURCE FILE. - // Instead we are encoding a few strings with hexadecimal constants. Don't do this in your application! - // Please use u8"text in any language" in your application! - // Note that characters values are preserved even by InputText() if the font cannot be displayed, so you can safely copy & paste garbled characters into another application. - ImGui::TextWrapped("CJK text will only appears if the font was loaded with the appropriate CJK character ranges. Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. Read docs/FONTS.txt for details."); + // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. in Visual Studio, you + // can save your source files as 'UTF-8 without signature'). + // - FOR THIS DEMO FILE ONLY, BECAUSE WE WANT TO SUPPORT OLD COMPILERS, WE ARE *NOT* INCLUDING RAW UTF-8 + // CHARACTERS IN THIS SOURCE FILE. Instead we are encoding a few strings with hexadecimal constants. + // Don't do this in your application! Please use u8"text in any language" in your application! + // Note that characters values are preserved even by InputText() if the font cannot be displayed, + // so you can safely copy & paste garbled characters into another application. + ImGui::TextWrapped( + "CJK text will only appears if the font was loaded with the appropriate CJK character ranges. " + "Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. " + "Read docs/FONTS.txt for details."); ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; @@ -800,41 +889,63 @@ static void ShowDemoWindowWidgets() ImGuiIO& io = ImGui::GetIO(); ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!"); - // Here we are grabbing the font texture because that's the only one we have access to inside the demo code. - // Remember that ImTextureID is just storage for whatever you want it to be, it is essentially a value that will be passed to the render function inside the ImDrawCmd structure. - // If you use one of the default imgui_impl_XXXX.cpp renderer, they all have comments at the top of their file to specify what they expect to be stored in ImTextureID. - // (for example, the imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer. The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier etc.) - // If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers to ImGui::Image(), and gather width/height through your own functions, etc. - // Using ShowMetricsWindow() as a "debugger" to inspect the draw data that are being passed to your render will help you debug issues if you are confused about this. - // Consider using the lower-level ImDrawList::AddImage() API, via ImGui::GetWindowDrawList()->AddImage(). + // Below we are displaying the font texture because it is the only texture we have access to inside the demo! + // Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that + // will be passed to the rendering back-end via the ImDrawCmd structure. + // If you use one of the default imgui_impl_XXXX.cpp rendering back-end, they all have comments at the top + // of their respective source file to specify what they expect to be stored in ImTextureID, for example: + // - The imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer + // - The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier, etc. + // More: + // - If you decided that ImTextureID = MyEngineTexture*, then you can pass your MyEngineTexture* pointers + // to ImGui::Image(), and gather width/height through your own functions, etc. + // - You can use ShowMetricsWindow() to inspect the draw data that are being passed to your renderer, + // it will help you debug issues if you are confused about it. + // - Consider using the lower-level ImDrawList::AddImage() API, via ImGui::GetWindowDrawList()->AddImage(). + // - Read https://github.com/ocornut/imgui/blob/master/docs/FAQ.md + // - Read https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples ImTextureID my_tex_id = io.Fonts->TexID; float my_tex_w = (float)io.Fonts->TexWidth; float my_tex_h = (float)io.Fonts->TexHeight; - - ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h); - ImVec2 pos = ImGui::GetCursorScreenPos(); - ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), ImVec2(0,0), ImVec2(1,1), ImVec4(1.0f,1.0f,1.0f,1.0f), ImVec4(1.0f,1.0f,1.0f,0.5f)); - if (ImGui::IsItemHovered()) { - ImGui::BeginTooltip(); - float region_sz = 32.0f; - float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; if (region_x < 0.0f) region_x = 0.0f; else if (region_x > my_tex_w - region_sz) region_x = my_tex_w - region_sz; - float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; if (region_y < 0.0f) region_y = 0.0f; else if (region_y > my_tex_h - region_sz) region_y = my_tex_h - region_sz; - float zoom = 4.0f; - ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y); - ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz); - ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h); - ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h); - ImGui::Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, ImVec4(1.0f, 1.0f, 1.0f, 1.0f), ImVec4(1.0f, 1.0f, 1.0f, 0.5f)); - ImGui::EndTooltip(); + ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left + ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint + ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); // 50% opaque white + ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, tint_col, border_col); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + float region_sz = 32.0f; + float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; + float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; + float zoom = 4.0f; + if (region_x < 0.0f) { region_x = 0.0f; } + else if (region_x > my_tex_w - region_sz) { region_x = my_tex_w - region_sz; } + if (region_y < 0.0f) { region_y = 0.0f; } + else if (region_y > my_tex_h - region_sz) { region_y = my_tex_h - region_sz; } + ImGui::Text("Min: (%.2f, %.2f)", region_x, region_y); + ImGui::Text("Max: (%.2f, %.2f)", region_x + region_sz, region_y + region_sz); + ImVec2 uv0 = ImVec2((region_x) / my_tex_w, (region_y) / my_tex_h); + ImVec2 uv1 = ImVec2((region_x + region_sz) / my_tex_w, (region_y + region_sz) / my_tex_h); + ImGui::Image(my_tex_id, ImVec2(region_sz * zoom, region_sz * zoom), uv0, uv1, tint_col, border_col); + ImGui::EndTooltip(); + } } ImGui::TextWrapped("And now some textured buttons.."); static int pressed_count = 0; for (int i = 0; i < 8; i++) { ImGui::PushID(i); - int frame_padding = -1 + i; // -1 = uses default padding - if (ImGui::ImageButton(my_tex_id, ImVec2(32,32), ImVec2(0,0), ImVec2(32.0f/my_tex_w,32/my_tex_h), frame_padding, ImVec4(0.0f,0.0f,0.0f,1.0f))) + int frame_padding = -1 + i; // -1 == uses default padding (style.FramePadding) + ImVec2 size = ImVec2(32.0f, 32.0f); // Size of the image we want to make visible + ImVec2 uv0 = ImVec2(0.0f, 0.0f); // UV coordinates for lower-left + ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32 / my_tex_h); // UV coordinates for (32,32) in our texture + ImVec4 bg_col = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); // Black background + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint + if (ImGui::ImageButton(my_tex_id, size, uv0, uv1, frame_padding, bg_col, tint_col)) pressed_count += 1; ImGui::PopID(); ImGui::SameLine(); @@ -855,19 +966,23 @@ static void ShowDemoWindowWidgets() if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", (unsigned int*)&flags, ImGuiComboFlags_NoPreview)) flags &= ~ImGuiComboFlags_NoArrowButton; // Clear the other flag, as we cannot combine both - // General BeginCombo() API, you have full control over your selection data and display type. - // (your selection data could be an index, a pointer to the object, an id for the object, a flag stored in the object itself, etc.) + // Using the generic BeginCombo() API, you have full control over how to display the combo contents. + // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively + // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; - static const char* item_current = items[0]; // Here our selection is a single pointer stored outside the object. - if (ImGui::BeginCombo("combo 1", item_current, flags)) // The second parameter is the label previewed before opening the combo. + static int item_current_idx = 0; // Here our selection data is an index. + const char* combo_label = items[item_current_idx]; // Label to preview before opening the combo (technically could be anything)( + if (ImGui::BeginCombo("combo 1", combo_label, flags)) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { - bool is_selected = (item_current == items[n]); + const bool is_selected = (item_current_idx == n); if (ImGui::Selectable(items[n], is_selected)) - item_current = items[n]; + item_current_idx = n; + + // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) if (is_selected) - ImGui::SetItemDefaultFocus(); // Set the initial focus when opening the combo (scrolling + for keyboard navigation support in the upcoming navigation branch) + ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } @@ -891,9 +1006,11 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Selectables")) { // Selectable() has 2 overloads: - // - The one taking "bool selected" as a read-only selection information. When Selectable() has been clicked is returns true and you can alter selection state accordingly. + // - The one taking "bool selected" as a read-only selection information. + // When Selectable() has been clicked it returns true and you can alter selection state accordingly. // - The one taking "bool* p_selected" as a read-write selection information (convenient in some cases) - // The earlier is more flexible, as in real application your selection may be stored in a different manner (in flags within objects, as an external list, etc). + // The earlier is more flexible, as in real application your selection may be stored in many different ways + // and not necessarily inside a bool value (e.g. in flags within objects, as an external list, etc). if (ImGui::TreeNode("Basic")) { static bool selection[5] = { false, true, false, false, false }; @@ -937,7 +1054,8 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("Rendering more text into the same line")) { - // Using the Selectable() override that takes "bool* p_selected" parameter and toggle your booleans automatically. + // Using the Selectable() override that takes "bool* p_selected" parameter, + // this function toggle your bool value automatically. static bool selected[3] = { false, false, false }; ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(300); ImGui::Text("12,345 bytes"); @@ -959,13 +1077,17 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("Grid")) { - static bool selected[4*4] = { true, false, false, false, false, true, false, false, false, false, true, false, false, false, false, true }; + static int selected[4*4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; for (int i = 0; i < 4*4; i++) { ImGui::PushID(i); - if (ImGui::Selectable("Sailor", &selected[i], 0, ImVec2(50,50))) + if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50,50))) { - // Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer. The second part of each test is unnecessary. + // Toggle + selected[i] = !selected[i]; + + // Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer. + // The second part of each test is unnecessary. int x = i % 4; int y = i / 4; if (x > 0) { selected[i - 1] ^= 1; } @@ -980,8 +1102,11 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("Alignment")) { - HelpMarker("Alignment applies when a selectable is larger than its text content.\nBy default, Selectables uses style.SelectableTextAlign but it can be overriden on a per-item basis using PushStyleVar()."); - static bool selected[3*3] = { true, false, true, false, true, false, true, false, true }; + HelpMarker( + "By default, Selectables uses style.SelectableTextAlign but it can be overridden on a per-item " + "basis using PushStyleVar(). You'll probably want to always keep your default situation to " + "left-align otherwise it becomes difficult to layout multiple items on a same line"); + static bool selected[3 * 3] = { true, false, true, false, true, false, true, false, true }; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) @@ -1031,12 +1156,22 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Filtered Text Input")) { - static char buf1[64] = ""; ImGui::InputText("default", buf1, 64); - static char buf2[64] = ""; ImGui::InputText("decimal", buf2, 64, ImGuiInputTextFlags_CharsDecimal); + struct TextFilters + { + // Return 0 (pass) if the character is 'i' or 'm' or 'g' or 'u' or 'i' + static int FilterImGuiLetters(ImGuiInputTextCallbackData* data) + { + if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar)) + return 0; + return 1; + } + }; + + static char buf1[64] = ""; ImGui::InputText("default", buf1, 64); + static char buf2[64] = ""; ImGui::InputText("decimal", buf2, 64, ImGuiInputTextFlags_CharsDecimal); static char buf3[64] = ""; ImGui::InputText("hexadecimal", buf3, 64, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase); - static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase); - static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank); - struct TextFilters { static int FilterImGuiLetters(ImGuiInputTextCallbackData* data) { if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar)) return 0; return 1; } }; + static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase); + static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank); static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); ImGui::Text("Password input"); @@ -1051,9 +1186,11 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Resize Callback")) { // To wire InputText() with std::string or any other custom string type, - // you can use the ImGuiInputTextFlags_CallbackResize flag + create a custom ImGui::InputText() wrapper using your prefered type. - // See misc/cpp/imgui_stdlib.h for an implementation of this using std::string. - HelpMarker("Demonstrate using ImGuiInputTextFlags_CallbackResize to wire your resizable string type to InputText().\n\nSee misc/cpp/imgui_stdlib.h for an implementation of this for std::string."); + // you can use the ImGuiInputTextFlags_CallbackResize flag + create a custom ImGui::InputText() wrapper + // using your preferred type. See misc/cpp/imgui_stdlib.h for an implementation of this using std::string. + HelpMarker( + "Using ImGuiInputTextFlags_CallbackResize to wire your custom string type to InputText().\n\n" + "See misc/cpp/imgui_stdlib.h for an implementation of this for std::string."); struct Funcs { static int MyResizeCallback(ImGuiInputTextCallbackData* data) @@ -1062,14 +1199,14 @@ static void ShowDemoWindowWidgets() { ImVector* my_str = (ImVector*)data->UserData; IM_ASSERT(my_str->begin() == data->Buf); - my_str->resize(data->BufSize); // NB: On resizing calls, generally data->BufSize == data->BufTextLen + 1 + my_str->resize(data->BufSize); // NB: On resizing calls, generally data->BufSize == data->BufTextLen + 1 data->Buf = my_str->begin(); } return 0; } - // Tip: Because ImGui:: is a namespace you would typicall add your own function into the namespace in your own source files. - // For example, you may add a function called ImGui::InputText(const char* label, MyString* my_str). + // Note: Because ImGui:: is a namespace you would typically add your own function into the namespace. + // For example, you code may declare a function 'ImGui::InputText(const char* label, MyString* my_str)' static bool MyInputTextMultiline(const char* label, ImVector* my_str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0) { IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); @@ -1078,7 +1215,8 @@ static void ShowDemoWindowWidgets() }; // For this demo we are using ImVector as a string container. - // Note that because we need to store a terminating zero character, our size/capacity are 1 more than usually reported by a typical string class. + // Note that because we need to store a terminating zero character, our size/capacity are 1 more + // than usually reported by a typical string class. static ImVector my_str; if (my_str.empty()) my_str.push_back(0); @@ -1091,7 +1229,8 @@ static void ShowDemoWindowWidgets() } // Plot/Graph widgets are currently fairly limited. - // Consider writing your own plotting widget, or using a third-party one (see "Wiki->Useful Widgets", or github.com/ocornut/imgui/issues/2747) + // Consider writing your own plotting widget, or using a third-party one + // (for third-party Plot widgets, see 'Wiki->Useful Widgets' or https://github.com/ocornut/imgui/labels/plot%2Fgraph) if (ImGui::TreeNode("Plots Widgets")) { static bool animate = true; @@ -1101,13 +1240,14 @@ static void ShowDemoWindowWidgets() ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); // Create a dummy array of contiguous float values to plot - // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float and the sizeof() of your structure in the Stride parameter. + // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float + // and the sizeof() of your structure in the "stride" parameter. static float values[90] = {}; static int values_offset = 0; static double refresh_time = 0.0; if (!animate || refresh_time == 0.0) refresh_time = ImGui::GetTime(); - while (refresh_time < ImGui::GetTime()) // Create dummy data at fixed 60 hz rate for the demo + while (refresh_time < ImGui::GetTime()) // Create dummy data at fixed 60 Hz rate for the demo { static float phase = 0.0f; values[values_offset] = cosf(phase); @@ -1125,12 +1265,13 @@ static void ShowDemoWindowWidgets() average /= (float)IM_ARRAYSIZE(values); char overlay[32]; sprintf(overlay, "avg %f", average); - ImGui::PlotLines("Lines", values, IM_ARRAYSIZE(values), values_offset, overlay, -1.0f, 1.0f, ImVec2(0,80)); + ImGui::PlotLines("Lines", values, IM_ARRAYSIZE(values), values_offset, overlay, -1.0f, 1.0f, ImVec2(0, 80.0f)); } - ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0,80)); + ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f)); // Use functions to generate output - // FIXME: This is rather awkward because current plot API only pass in indices. We probably want an API passing floats and user provide sample rate/count. + // FIXME: This is rather awkward because current plot API only pass in indices. + // We probably want an API passing floats and user provide sample rate/count. struct Funcs { static float Sin(void*, int i) { return sinf(i * 0.1f); } @@ -1162,7 +1303,7 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); ImGui::Text("Progress Bar"); - float progress_saturated = (progress < 0.0f) ? 0.0f : (progress > 1.0f) ? 1.0f : progress; + float progress_saturated = IM_CLAMP(progress, 0.0f, 1.0f); char buf[32]; sprintf(buf, "%d/%d", (int)(progress_saturated*1753), 1753); ImGui::ProgressBar(progress, ImVec2(0.f,0.f), buf); @@ -1186,7 +1327,9 @@ static void ShowDemoWindowWidgets() ImGuiColorEditFlags misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions); ImGui::Text("Color widget:"); - ImGui::SameLine(); HelpMarker("Click on the colored square to open a color picker.\nCTRL+click on individual component to input value.\n"); + ImGui::SameLine(); HelpMarker( + "Click on the colored square to open a color picker.\n" + "CTRL+click on individual component to input value.\n"); ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags); ImGui::Text("Color widget HSV with Alpha:"); @@ -1196,7 +1339,10 @@ static void ShowDemoWindowWidgets() ImGui::ColorEdit4("MyColor##2f", (float*)&color, ImGuiColorEditFlags_Float | misc_flags); ImGui::Text("Color button with Picker:"); - ImGui::SameLine(); HelpMarker("With the ImGuiColorEditFlags_NoInputs flag you can hide all the slider/text inputs.\nWith the ImGuiColorEditFlags_NoLabel flag you can pass a non-empty label which will only be used for the tooltip and picker popup."); + ImGui::SameLine(); HelpMarker( + "With the ImGuiColorEditFlags_NoInputs flag you can hide all the slider/text inputs.\n" + "With the ImGuiColorEditFlags_NoLabel flag you can pass a non-empty label which will only " + "be used for the tooltip and picker popup."); ImGui::ColorEdit4("MyColor##3", (float*)&color, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | misc_flags); ImGui::Text("Color button with Custom Picker Popup:"); @@ -1208,7 +1354,8 @@ static void ShowDemoWindowWidgets() { for (int n = 0; n < IM_ARRAYSIZE(saved_palette); n++) { - ImGui::ColorConvertHSVtoRGB(n / 31.0f, 0.8f, 0.8f, saved_palette[n].x, saved_palette[n].y, saved_palette[n].z); + ImGui::ColorConvertHSVtoRGB(n / 31.0f, 0.8f, 0.8f, + saved_palette[n].x, saved_palette[n].y, saved_palette[n].z); saved_palette[n].w = 1.0f; // Alpha } saved_palette_init = false; @@ -1243,11 +1390,13 @@ static void ShowDemoWindowWidgets() ImGui::PushID(n); if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); - if (ImGui::ColorButton("##palette", saved_palette[n], ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip, ImVec2(20,20))) + + ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip; + if (ImGui::ColorButton("##palette", saved_palette[n], palette_button_flags, ImVec2(20,20))) color = ImVec4(saved_palette[n].x, saved_palette[n].y, saved_palette[n].z, color.w); // Preserve alpha! - // Allow user to drop colors into each palette entry - // (Note that ColorButton is already a drag source by default, unless using ImGuiColorEditFlags_NoDragDrop) + // Allow user to drop colors into each palette entry. Note that ColorButton() is already a + // drag source by default, unless specifying the ImGuiColorEditFlags_NoDragDrop flag. if (ImGui::BeginDragDropTarget()) { if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) @@ -1290,7 +1439,10 @@ static void ShowDemoWindowWidgets() } } ImGui::Combo("Display Mode", &display_mode, "Auto/Current\0None\0RGB Only\0HSV Only\0Hex Only\0"); - ImGui::SameLine(); HelpMarker("ColorEdit defaults to displaying RGB inputs if you don't specify a display mode, but the user can change it with a right-click.\n\nColorPicker defaults to displaying RGB+HSV+Hex if you don't specify a display mode.\n\nYou can change the defaults using SetColorEditOptions()."); + ImGui::SameLine(); HelpMarker( + "ColorEdit defaults to displaying RGB inputs if you don't specify a display mode, " + "but the user can change it with a right-click.\n\nColorPicker defaults to displaying RGB+HSV+Hex " + "if you don't specify a display mode.\n\nYou can change the defaults using SetColorEditOptions()."); ImGui::Combo("Picker Mode", &picker_mode, "Auto/Current\0Hue bar + SV rect\0Hue wheel + SV triangle\0"); ImGui::SameLine(); HelpMarker("User can right-click the picker to change mode."); ImGuiColorEditFlags flags = misc_flags; @@ -1305,22 +1457,29 @@ static void ShowDemoWindowWidgets() if (display_mode == 4) flags |= ImGuiColorEditFlags_DisplayHex; ImGui::ColorPicker4("MyColor##4", (float*)&color, flags, ref_color ? &ref_color_v.x : NULL); - ImGui::Text("Programmatically set defaults:"); - ImGui::SameLine(); HelpMarker("SetColorEditOptions() is designed to allow you to set boot-time default.\nWe don't have Push/Pop functions because you can force options on a per-widget basis if needed, and the user can change non-forced ones with the options menu.\nWe don't have a getter to avoid encouraging you to persistently save values that aren't forward-compatible."); + ImGui::Text("Set defaults in code:"); + ImGui::SameLine(); HelpMarker( + "SetColorEditOptions() is designed to allow you to set boot-time default.\n" + "We don't have Push/Pop functions because you can force options on a per-widget basis if needed," + "and the user can change non-forced ones with the options menu.\nWe don't have a getter to avoid" + "encouraging you to persistently save values that aren't forward-compatible."); if (ImGui::Button("Default: Uint8 + HSV + Hue Bar")) ImGui::SetColorEditOptions(ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_PickerHueBar); if (ImGui::Button("Default: Float + HDR + Hue Wheel")) ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel); // HSV encoded support (to avoid RGB<>HSV round trips and singularities when S==0 or V==0) - static ImVec4 color_stored_as_hsv(0.23f, 1.0f, 1.0f, 1.0f); + static ImVec4 color_hsv(0.23f, 1.0f, 1.0f, 1.0f); // Stored as HSV! ImGui::Spacing(); ImGui::Text("HSV encoded colors"); - ImGui::SameLine(); HelpMarker("By default, colors are given to ColorEdit and ColorPicker in RGB, but ImGuiColorEditFlags_InputHSV allows you to store colors as HSV and pass them to ColorEdit and ColorPicker as HSV. This comes with the added benefit that you can manipulate hue values with the picker even when saturation or value are zero."); + ImGui::SameLine(); HelpMarker( + "By default, colors are given to ColorEdit and ColorPicker in RGB, but ImGuiColorEditFlags_InputHSV" + "allows you to store colors as HSV and pass them to ColorEdit and ColorPicker as HSV. This comes with the" + "added benefit that you can manipulate hue values with the picker even when saturation or value are zero."); ImGui::Text("Color widget with InputHSV:"); - ImGui::ColorEdit4("HSV shown as RGB##1", (float*)&color_stored_as_hsv, ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); - ImGui::ColorEdit4("HSV shown as HSV##1", (float*)&color_stored_as_hsv, ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); - ImGui::DragFloat4("Raw HSV values", (float*)&color_stored_as_hsv, 0.01f, 0.0f, 1.0f); + ImGui::ColorEdit4("HSV shown as RGB##1", (float*)&color_hsv, ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); + ImGui::ColorEdit4("HSV shown as HSV##1", (float*)&color_hsv, ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_InputHSV | ImGuiColorEditFlags_Float); + ImGui::DragFloat4("Raw HSV values", (float*)&color_hsv, 0.01f, 0.0f, 1.0f); ImGui::TreePop(); } @@ -1336,20 +1495,23 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Data Types")) { - // The DragScalar/InputScalar/SliderScalar functions allow various data types: signed/unsigned int/long long and float/double - // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum to pass the type, - // and passing all arguments by address. + // DragScalar/InputScalar/SliderScalar functions allow various data types + // - signed/unsigned + // - 8/16/32/64-bits + // - integer/float/double + // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum + // to pass the type, and passing all arguments by pointer. // This is the reason the test code below creates local variables to hold "zero" "one" etc. for each types. - // In practice, if you frequently use a given type that is not covered by the normal API entry points, you can wrap it - // yourself inside a 1 line function which can take typed argument as value instead of void*, and then pass their address - // to the generic function. For example: + // In practice, if you frequently use a given type that is not covered by the normal API entry points, + // you can wrap it yourself inside a 1 line function which can take typed argument as value instead of void*, + // and then pass their address to the generic function. For example: // bool MySliderU64(const char *label, u64* value, u64 min = 0, u64 max = 0, const char* format = "%lld") // { // return SliderScalar(label, ImGuiDataType_U64, value, &min, &max, format); // } - // Limits (as helper variables that we can take the address of) - // Note that the SliderScalar function has a maximum usable range of half the natural type maximum, hence the /2 below. + // Setup limits (as helper variables so we can take their address, as explained above) + // Note: SliderScalar() functions have a maximum usable range of half the natural type maximum, hence the /2. #ifndef LLONG_MIN ImS64 LLONG_MIN = -9223372036854775807LL - 1; ImS64 LLONG_MAX = 9223372036854775807LL; @@ -1538,8 +1700,9 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Drag and drop in standard widgets")) { // ColorEdit widgets automatically act as drag source and drag target. - // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F to allow your own widgets - // to use colors in their drag and drop interaction. Also see the demo in Color Picker -> Palette demo. + // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F + // to allow your own widgets to use colors in their drag and drop interaction. + // Also see 'Demo->Widgets->Color/Picker Widgets->Palette' demo. HelpMarker("You can drag from the colored squares."); static float col1[3] = { 1.0f, 0.0f, 0.2f }; static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; @@ -1560,7 +1723,12 @@ static void ShowDemoWindowWidgets() if (ImGui::RadioButton("Copy", mode == Mode_Copy)) { mode = Mode_Copy; } ImGui::SameLine(); if (ImGui::RadioButton("Move", mode == Mode_Move)) { mode = Mode_Move; } ImGui::SameLine(); if (ImGui::RadioButton("Swap", mode == Mode_Swap)) { mode = Mode_Swap; } - static const char* names[9] = { "Bobby", "Beatrice", "Betty", "Brianna", "Barry", "Bernard", "Bibi", "Blaine", "Bryn" }; + const char* names[9] = + { + "Bobby", "Beatrice", "Betty", + "Brianna", "Barry", "Bernard", + "Bibi", "Blaine", "Bryn" + }; for (int n = 0; n < IM_ARRAYSIZE(names); n++) { ImGui::PushID(n); @@ -1571,8 +1739,12 @@ static void ShowDemoWindowWidgets() // Our buttons are both drag sources and drag targets here! if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) { - ImGui::SetDragDropPayload("DND_DEMO_CELL", &n, sizeof(int)); // Set payload to carry the index of our item (could be anything) - if (mode == Mode_Copy) { ImGui::Text("Copy %s", names[n]); } // Display preview (could be anything, e.g. when dragging an image we could decide to display the filename and a small preview of the image, etc.) + // Set payload to carry the index of our item (could be anything) + ImGui::SetDragDropPayload("DND_DEMO_CELL", &n, sizeof(int)); + + // Display preview (could be anything, e.g. when dragging an image we could decide to display + // the filename and a small preview of the image, etc.) + if (mode == Mode_Copy) { ImGui::Text("Copy %s", names[n]); } if (mode == Mode_Move) { ImGui::Text("Move %s", names[n]); } if (mode == Mode_Swap) { ImGui::Text("Swap %s", names[n]); } ImGui::EndDragDropSource(); @@ -1609,7 +1781,9 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Drag to reorder items (simple)")) { // Simple reordering - HelpMarker("We don't use the drag and drop api at all here! Instead we query when the item is held but not hovered, and order items accordingly."); + HelpMarker( + "We don't use the drag and drop api at all here! " + "Instead we query when the item is held but not hovered, and order items accordingly."); static const char* item_names[] = { "Item One", "Item Two", "Item Three", "Item Four", "Item Five" }; for (int n = 0; n < IM_ARRAYSIZE(item_names); n++) { @@ -1635,11 +1809,18 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Querying Status (Active/Focused/Hovered etc.)")) { - // Submit an item (various types available) so we can query their status in the following block. + // Select an item type + const char* item_names[] = + { + "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputFloat", + "InputFloat3", "ColorEdit4", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "ListBox" + }; static int item_type = 1; - ImGui::Combo("Item Type", &item_type, "Text\0Button\0Button (w/ repeat)\0Checkbox\0SliderFloat\0InputText\0InputFloat\0InputFloat3\0ColorEdit4\0MenuItem\0TreeNode\0TreeNode (w/ double-click)\0ListBox\0", 20); + ImGui::Combo("Item Type", &item_type, item_names, IM_ARRAYSIZE(item_names), IM_ARRAYSIZE(item_names)); ImGui::SameLine(); HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions."); + + // Submit selected item item so we can query their status in the code following it. bool ret = false; static bool b = false; static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f }; @@ -1658,10 +1839,10 @@ static void ShowDemoWindowWidgets() if (item_type == 11){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. if (item_type == 12){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } - // Display the value of IsItemHovered() and other common item state functions. + // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. // Because BulletText is an item itself and that would affect the output of IsItemXXX functions, - // we query every state in a single call to avoid storing them and to simplify the code + // we query every state in a single call to avoid storing them and to simplify the code. ImGui::BulletText( "Return value = %d\n" "IsItemFocused() = %d\n" @@ -1704,7 +1885,7 @@ static void ShowDemoWindowWidgets() static bool embed_all_inside_a_child_window = false; ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) - ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20), true); + ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), true); // Testing IsWindowFocused() function with its various flags. // Note that the ImGuiFocusedFlags_XXX flags can be combined. @@ -1750,7 +1931,7 @@ static void ShowDemoWindowWidgets() ImGui::InputText("dummy", dummy_str, IM_ARRAYSIZE(dummy_str), ImGuiInputTextFlags_ReadOnly); // Calling IsItemHovered() after begin returns the hovered status of the title bar. - // This is useful in particular if you want to create a context menu (with BeginPopupContextItem) associated to the title bar of a window. + // This is useful in particular if you want to create a context menu associated to the title bar of a window. static bool test_window = false; ImGui::Checkbox("Hovered/Active tests after Begin() for title bar testing", &test_window); if (test_window) @@ -1793,7 +1974,9 @@ static void ShowDemoWindowLayout() // Child 1: no border, enable horizontal scrollbar { - ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar | (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + if (disable_mouse_wheel) + window_flags |= ImGuiWindowFlags_NoScrollWithMouse; ImGui::BeginChild("ChildL", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); for (int i = 0; i < 100; i++) { @@ -1810,7 +1993,11 @@ static void ShowDemoWindowLayout() // Child 2: rounded border { - ImGuiWindowFlags window_flags = (disable_mouse_wheel ? ImGuiWindowFlags_NoScrollWithMouse : 0) | (disable_menu ? 0 : ImGuiWindowFlags_MenuBar); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_None; + if (disable_mouse_wheel) + window_flags |= ImGuiWindowFlags_NoScrollWithMouse; + if (!disable_menu) + window_flags |= ImGuiWindowFlags_MenuBar; ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); ImGui::BeginChild("ChildR", ImVec2(0, 260), true, window_flags); if (!disable_menu && ImGui::BeginMenuBar()) @@ -1838,10 +2025,11 @@ static void ShowDemoWindowLayout() // Demonstrate a few extra things // - Changing ImGuiCol_ChildBg (which is transparent black in default styles) - // - Using SetCursorPos() to position the child window (because the child window is an item from the POV of the parent window) - // You can also call SetNextWindowPos() to position the child window. The parent window will effectively layout from this position. - // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from the POV of the parent window) - // See "Widgets" -> "Querying Status (Active/Focused/Hovered etc.)" section for more details about this. + // - Using SetCursorPos() to position child window (the child window is an item from the POV of parent window) + // You can also call SetNextWindowPos() to position the child window. The parent window will effectively + // layout from this position. + // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from + // the POV of the parent window). See 'Demo->Querying Status (Active/Focused/Hovered etc.)' for details. { ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); @@ -1862,6 +2050,9 @@ static void ShowDemoWindowLayout() { // Use SetNextItemWidth() to set the width of a single upcoming item. // Use PushItemWidth()/PopItemWidth() to set the width of a group of items. + // In real code use you'll probably want to choose width values that are proportional to your font size + // e.g. Using '20.0f * GetFontSize()' as width instead of '200.0f', etc. + static float f = 0.0f; ImGui::Text("SetNextItemWidth/PushItemWidth(100)"); ImGui::SameLine(); HelpMarker("Fixed width."); @@ -1883,7 +2074,8 @@ static void ShowDemoWindowLayout() ImGui::SetNextItemWidth(-100); ImGui::DragFloat("float##4", &f); - // Demonstrate using PushItemWidth to surround three items. Calling SetNextItemWidth() before each of them would have the same effect. + // Demonstrate using PushItemWidth to surround three items. + // Calling SetNextItemWidth() before each of them would have the same effect. ImGui::Text("SetNextItemWidth/PushItemWidth(-1)"); ImGui::SameLine(); HelpMarker("Align to right edge"); ImGui::PushItemWidth(-1); @@ -1964,7 +2156,8 @@ static void ShowDemoWindowLayout() ImGui::Dummy(button_sz); ImGui::SameLine(); ImGui::Button("B", button_sz); - // Manually wrapping (we should eventually provide this as an automatic layout feature, but for now you can do it manually) + // Manually wrapping + // (we should eventually provide this as an automatic layout feature, but for now you can do it manually) ImGui::Text("Manually wrapping:"); ImGuiStyle& style = ImGui::GetStyle(); int buttons_count = 20; @@ -2035,7 +2228,8 @@ static void ShowDemoWindowLayout() ImGui::Checkbox(names[n], &opened[n]); } - // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): the underlying bool will be set to false when the tab is closed. + // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): + // the underlying bool will be set to false when the tab is closed. if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) { for (int n = 0; n < IM_ARRAYSIZE(opened); n++) @@ -2056,7 +2250,10 @@ static void ShowDemoWindowLayout() if (ImGui::TreeNode("Groups")) { - HelpMarker("BeginGroup() basically locks the horizontal position for new line. EndGroup() bundles the whole group so that you can use \"item\" functions such as IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group."); + HelpMarker( + "BeginGroup() basically locks the horizontal position for new line. " + "EndGroup() bundles the whole group so that you can use \"item\" functions such as " + "IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group."); ImGui::BeginGroup(); { ImGui::BeginGroup(); @@ -2079,9 +2276,9 @@ static void ShowDemoWindowLayout() const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f }; ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size); - ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f, size.y)); + ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); ImGui::SameLine(); - ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x)*0.5f, size.y)); + ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); ImGui::EndGroup(); ImGui::SameLine(); @@ -2102,8 +2299,9 @@ static void ShowDemoWindowLayout() { { ImGui::BulletText("Text baseline:"); - ImGui::SameLine(); - HelpMarker("This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. Lines only composed of text or \"small\" widgets fit in less vertical spaces than lines with normal widgets."); + ImGui::SameLine(); HelpMarker( + "This is testing the vertical alignment that gets applied on text to keep it aligned with widgets. " + "Lines only composed of text or \"small\" widgets use less vertical space than lines with framed widgets."); ImGui::Indent(); ImGui::Text("KO Blahblah"); ImGui::SameLine(); @@ -2111,7 +2309,8 @@ static void ShowDemoWindowLayout() HelpMarker("Baseline of button will look misaligned with text.."); // If your line starts with text, call AlignTextToFramePadding() to align text to upcoming widgets. - // Because we don't know what's coming after the Text() statement, we need to move the text baseline down by FramePadding.y + // (because we don't know what's coming after the Text() statement, we need to move the text baseline + // down by FramePadding.y ahead of time) ImGui::AlignTextToFramePadding(); ImGui::Text("OK Blahblah"); ImGui::SameLine(); ImGui::Button("Some framed item"); ImGui::SameLine(); @@ -2163,7 +2362,7 @@ static void ShowDemoWindowLayout() ImGui::BulletText("Misc items:"); ImGui::Indent(); - // SmallButton() sets FramePadding to zero. Text baseline is aligned to match baseline of previous Button + // SmallButton() sets FramePadding to zero. Text baseline is aligned to match baseline of previous Button. ImGui::Button("80x80", ImVec2(80, 80)); ImGui::SameLine(); ImGui::Button("50x50", ImVec2(50, 50)); @@ -2176,12 +2375,29 @@ static void ShowDemoWindowLayout() const float spacing = ImGui::GetStyle().ItemInnerSpacing.x; ImGui::Button("Button##1"); ImGui::SameLine(0.0f, spacing); - if (ImGui::TreeNode("Node##1")) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data + if (ImGui::TreeNode("Node##1")) + { + // Dummy tree data + for (int i = 0; i < 6; i++) + ImGui::BulletText("Item %d..", i); + ImGui::TreePop(); + } - ImGui::AlignTextToFramePadding(); // Vertically align text node a bit lower so it'll be vertically centered with upcoming widget. Otherwise you can use SmallButton (smaller fit). - bool node_open = ImGui::TreeNode("Node##2");// Common mistake to avoid: if we want to SameLine after TreeNode we need to do it before we add child content. + // Vertically align text node a bit lower so it'll be vertically centered with upcoming widget. + // Otherwise you can use SmallButton() (smaller fit). + ImGui::AlignTextToFramePadding(); + + // Common mistake to avoid: if we want to SameLine after TreeNode we need to do it before we add + // other contents below the node. + bool node_open = ImGui::TreeNode("Node##2"); ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2"); - if (node_open) { for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); } // Dummy tree data + if (node_open) + { + // Dummy tree data + for (int i = 0; i < 6; i++) + ImGui::BulletText("Item %d..", i); + ImGui::TreePop(); + } // Bullet ImGui::Button("Button##3"); @@ -2238,8 +2454,9 @@ static void ShowDemoWindowLayout() const char* names[] = { "Top", "25%", "Center", "75%", "Bottom" }; ImGui::TextUnformatted(names[i]); - ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; - bool window_visible = ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(child_w, 200.0f), true, child_flags); + const ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; + const ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); + const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), true, child_flags); if (ImGui::BeginMenuBar()) { ImGui::TextUnformatted("abc"); @@ -2249,7 +2466,7 @@ static void ShowDemoWindowLayout() ImGui::SetScrollY(scroll_to_off_px); if (scroll_to_pos) ImGui::SetScrollFromPosY(ImGui::GetCursorStartPos().y + scroll_to_pos_px, i * 0.25f); - if (window_visible) // Avoid calling SetScrollHereY when running with culled items + if (child_is_visible) // Avoid calling SetScrollHereY when running with culled items { for (int item = 0; item < 100; item++) { @@ -2274,18 +2491,25 @@ static void ShowDemoWindowLayout() // Horizontal scroll functions ImGui::Spacing(); - HelpMarker("Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\nUsing the \"Scroll To Pos\" button above will make the discontinuity at edges visible: scrolling to the top/bottom/left/right-most item will add an additional WindowPadding to reflect on reaching the edge of the list.\n\nBecause the clipping rectangle of most window hides half worth of WindowPadding on the left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the equivalent SetScrollFromPosY(+1) wouldn't."); + HelpMarker( + "Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n" + "Using the \"Scroll To Pos\" button above will make the discontinuity at edges visible: " + "scrolling to the top/bottom/left/right-most item will add an additional WindowPadding to reflect " + "on reaching the edge of the list.\n\nBecause the clipping rectangle of most window hides half " + "worth of WindowPadding on the left/right, using SetScrollFromPosX(+1) will usually result in " + "clipped text whereas the equivalent SetScrollFromPosY(+1) wouldn't."); ImGui::PushID("##HorizontalScrolling"); for (int i = 0; i < 5; i++) { float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f; ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0); - bool window_visible = ImGui::BeginChild(ImGui::GetID((void*)(intptr_t)i), ImVec2(-100, child_height), true, child_flags); + ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); + bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), true, child_flags); if (scroll_to_off) ImGui::SetScrollX(scroll_to_off_px); if (scroll_to_pos) ImGui::SetScrollFromPosX(ImGui::GetCursorStartPos().x + scroll_to_pos_px, i * 0.25f); - if (window_visible) // Avoid calling SetScrollHereY when running with culled items + if (child_is_visible) // Avoid calling SetScrollHereY when running with culled items { for (int item = 0; item < 100; item++) { @@ -2312,16 +2536,21 @@ static void ShowDemoWindowLayout() ImGui::PopID(); // Miscellaneous Horizontal Scrolling Demo - HelpMarker("Horizontal scrolling for a window has to be enabled explicitly via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\nYou may want to explicitly specify content width by calling SetNextWindowContentWidth() before Begin()."); + HelpMarker( + "Horizontal scrolling for a window is enabled via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\n" + "You may want to also explicitly specify content width by using SetNextWindowContentWidth() before Begin()."); static int lines = 7; ImGui::SliderInt("Lines", &lines, 1, 15); ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2.0f, 1.0f)); - ImGui::BeginChild("scrolling", ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30), true, ImGuiWindowFlags_HorizontalScrollbar); + ImVec2 scrolling_child_size = ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30); + ImGui::BeginChild("scrolling", scrolling_child_size, true, ImGuiWindowFlags_HorizontalScrollbar); for (int line = 0; line < lines; line++) { - // Display random stuff (for the sake of this trivial demo we are using basic Button+SameLine. If you want to create your own time line for a real application you may be better off - // manipulating the cursor position yourself, aka using SetCursorPos/SetCursorScreenPos to position the widgets yourself. You may also want to use the lower-level ImDrawList API) + // Display random stuff. For the sake of this trivial demo we are using basic Button() + SameLine() + // If you want to create your own time line for a real application you may be better off manipulating + // the cursor position yourself, aka using SetCursorPos/SetCursorScreenPos to position the widgets + // yourself. You may also want to use the lower-level ImDrawList API. int num_buttons = 10 + ((line & 1) ? line * 9 : line * 3); for (int n = 0; n < num_buttons; n++) { @@ -2344,13 +2573,21 @@ static void ShowDemoWindowLayout() ImGui::EndChild(); ImGui::PopStyleVar(2); float scroll_x_delta = 0.0f; - ImGui::SmallButton("<<"); if (ImGui::IsItemActive()) { scroll_x_delta = -ImGui::GetIO().DeltaTime * 1000.0f; } ImGui::SameLine(); + ImGui::SmallButton("<<"); + if (ImGui::IsItemActive()) + scroll_x_delta = -ImGui::GetIO().DeltaTime * 1000.0f; + ImGui::SameLine(); ImGui::Text("Scroll from code"); ImGui::SameLine(); - ImGui::SmallButton(">>"); if (ImGui::IsItemActive()) { scroll_x_delta = +ImGui::GetIO().DeltaTime * 1000.0f; } ImGui::SameLine(); + ImGui::SmallButton(">>"); + if (ImGui::IsItemActive()) + scroll_x_delta = +ImGui::GetIO().DeltaTime * 1000.0f; + ImGui::SameLine(); ImGui::Text("%.0f/%.0f", scroll_x, scroll_max_x); if (scroll_x_delta != 0.0f) { - ImGui::BeginChild("scrolling"); // Demonstrate a trick: you can use Begin to set yourself in the context of another window (here we are already out of your child window) + // Demonstrate a trick: you can use Begin to set yourself in the context of another window + // (here we are already out of your child window) + ImGui::BeginChild("scrolling"); ImGui::SetScrollX(ImGui::GetScrollX() + scroll_x_delta); ImGui::EndChild(); } @@ -2439,7 +2676,7 @@ static void ShowDemoWindowLayout() } if (show_child) { - ImGui::BeginChild("child", ImVec2(0,0), true); + ImGui::BeginChild("child", ImVec2(0, 0), true); ImGui::EndChild(); } ImGui::End(); @@ -2451,15 +2688,22 @@ static void ShowDemoWindowLayout() if (ImGui::TreeNode("Clipping")) { static ImVec2 size(100, 100), offset(50, 20); - ImGui::TextWrapped("On a per-widget basis we are occasionally clipping text CPU-side if it won't fit in its frame. Otherwise we are doing coarser clipping + passing a scissor rectangle to the renderer. The system is designed to try minimizing both execution and CPU/GPU rendering cost."); + ImGui::TextWrapped( + "On a per-widget basis we are occasionally clipping text CPU-side if it won't fit in its frame. " + "Otherwise we are doing coarser clipping + passing a scissor rectangle to the renderer. " + "The system is designed to try minimizing both execution and CPU/GPU rendering cost."); ImGui::DragFloat2("size", (float*)&size, 0.5f, 1.0f, 200.0f, "%.0f"); ImGui::TextWrapped("(Click and drag)"); ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec4 clip_rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); ImGui::InvisibleButton("##dummy", size); - if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) { offset.x += ImGui::GetIO().MouseDelta.x; offset.y += ImGui::GetIO().MouseDelta.y; } + if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) + { + offset.x += ImGui::GetIO().MouseDelta.x; + offset.y += ImGui::GetIO().MouseDelta.y; + } ImGui::GetWindowDrawList()->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(90, 90, 120, 255)); - ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.0f, ImVec2(pos.x + offset.x, pos.y + offset.y), IM_COL32(255, 255, 255, 255), "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect); + ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.0f, ImVec2(pos.x + offset.x, pos.y + offset.y), IM_COL32_WHITE, "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect); ImGui::TreePop(); } } @@ -2472,10 +2716,12 @@ static void ShowDemoWindowPopups() // The properties of popups windows are: // - They block normal mouse hovering detection outside them. (*) // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. - // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as we are used to with regular Begin() calls. - // User can manipulate the visibility state by calling OpenPopup(). - // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even when normally blocked by a popup. - // Those three properties are connected. The library needs to hold their visibility state because it can close popups at any time. + // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as + // we are used to with regular Begin() calls. User can manipulate the visibility state by calling OpenPopup(). + // (*) One can use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup) to bypass it and detect hovering even + // when normally blocked by a popup. + // Those three properties are connected. The library needs to hold their visibility state BECAUSE it can close + // popups at any time. // Typical use for regular windows: // bool my_tool_is_active = false; if (ImGui::Button("Open")) my_tool_is_active = true; [...] if (my_tool_is_active) Begin("My Tool", &my_tool_is_active) { [...] } End(); @@ -2487,14 +2733,16 @@ static void ShowDemoWindowPopups() if (ImGui::TreeNode("Popups")) { - ImGui::TextWrapped("When a popup is active, it inhibits interacting with windows that are behind the popup. Clicking outside the popup closes it."); + ImGui::TextWrapped( + "When a popup is active, it inhibits interacting with windows that are behind the popup. " + "Clicking outside the popup closes it."); static int selected_fish = -1; const char* names[] = { "Bream", "Haddock", "Mackerel", "Pollock", "Tilefish" }; static bool toggles[] = { true, false, false, false, false }; - // Simple selection popup - // (If you want to show the current selection inside the Button itself, you may want to build a string using the "###" operator to preserve a constant ID with a variable label) + // Simple selection popup (if you want to show the current selection inside the Button itself, + // you may want to build a string using the "###" operator to preserve a constant ID with a variable label) if (ImGui::Button("Select..")) ImGui::OpenPopup("my_select_popup"); ImGui::SameLine(); @@ -2568,7 +2816,8 @@ static void ShowDemoWindowPopups() // if (IsItemHovered() && IsMouseReleased(0)) // OpenPopup(id); // return BeginPopup(id); - // For more advanced uses you may want to replicate and cuztomize this code. This the comments inside BeginPopupContextItem() implementation. + // For more advanced uses you may want to replicate and customize this code. + // See details in BeginPopupContextItem(). static float value = 0.5f; ImGui::Text("Value = %.3f (<-- right-click here)", value); if (ImGui::BeginPopupContextItem("item context menu")) @@ -2580,16 +2829,19 @@ static void ShowDemoWindowPopups() ImGui::EndPopup(); } - // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the Begin call. - // So here we will make it that clicking on the text field with the right mouse button (1) will toggle the visibility of the popup above. + // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the + // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) + // will toggle the visibility of the popup above. ImGui::Text("(You can also right-click me to open the same popup as above.)"); ImGui::OpenPopupOnItemClick("item context menu", 1); - // When used after an item that has an ID (here the Button), we can skip providing an ID to BeginPopupContextItem(). + // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). // BeginPopupContextItem() will use the last item ID as the popup ID. - // In addition here, we want to include your editable label inside the button label. We use the ### operator to override the ID (read FAQ about ID for details) + // In addition here, we want to include your editable label inside the button label. + // We use the ### operator to override the ID (read FAQ about ID for details) static char name[32] = "Label1"; - char buf[64]; sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label + char buf[64]; + sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label ImGui::Button(buf); if (ImGui::BeginPopupContextItem()) { @@ -2606,7 +2858,7 @@ static void ShowDemoWindowPopups() if (ImGui::TreeNode("Modals")) { - ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside the window."); + ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside."); if (ImGui::Button("Delete..")) ImGui::OpenPopup("Delete?"); @@ -2655,8 +2907,9 @@ static void ShowDemoWindowPopups() if (ImGui::Button("Add another modal..")) ImGui::OpenPopup("Stacked 2"); - // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which will close the popup. - // Note that the visibility state of popups is owned by imgui, so the input value of the bool actually doesn't matter here. + // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which + // will close the popup. Note that the visibility state of popups is owned by imgui, so the input value + // of the bool actually doesn't matter here. bool dummy_open = true; if (ImGui::BeginPopupModal("Stacked 2", &dummy_open)) { @@ -2678,9 +2931,12 @@ static void ShowDemoWindowPopups() { ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); ImGui::Separator(); - // NB: As a quirk in this very specific example, we want to differentiate the parent of this menu from the parent of the various popup menus above. - // To do so we are encloding the items in a PushID()/PopID() block to make them two different menusets. If we don't, opening any popup above and hovering our menu here - // would open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, which is the desired behavior for regular menus. + + // Note: As a quirk in this very specific example, we want to differentiate the parent of this menu from the + // parent of the various popup menus above. To do so we are encloding the items in a PushID()/PopID() block + // to make them two different menusets. If we don't, opening any popup above and hovering our menu here would + // open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, + // which is the desired behavior for regular menus. ImGui::PushID("foo"); ImGui::MenuItem("Menu item", "CTRL+M"); if (ImGui::BeginMenu("Menu inside a regular window")) @@ -2862,7 +3118,8 @@ static void ShowDemoWindowColumns() if (ImGui::TreeNode("Horizontal Scrolling")) { ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f)); - ImGui::BeginChild("##ScrollingRegion", ImVec2(0, ImGui::GetFontSize() * 20), false, ImGuiWindowFlags_HorizontalScrollbar); + ImVec2 child_size = ImVec2(0, ImGui::GetFontSize() * 20.0f); + ImGui::BeginChild("##ScrollingRegion", child_size, false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::Columns(10); int ITEMS_COUNT = 2000; ImGuiListClipper clipper(ITEMS_COUNT); // Also demonstrate using the clipper for large list @@ -2961,7 +3218,7 @@ static void ShowDemoWindowMisc() ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (io.MouseDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse dbl-clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); @@ -2971,9 +3228,9 @@ static void ShowDemoWindowMisc() ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. - ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputs[i]); } - ImGui::Text("NavInputs pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] == 0.0f) { ImGui::SameLine(); ImGui::Text("[%d]", i); } - ImGui::Text("NavInputs duration:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputsDownDuration[i]); } + ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputs[i]); } + ImGui::Text("NavInputs pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] == 0.0f) { ImGui::SameLine(); ImGui::Text("[%d]", i); } + ImGui::Text("NavInputs duration:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputsDownDuration[i]); } ImGui::Button("Hovering me sets the\nkeyboard capture flag"); if (ImGui::IsItemHovered()) @@ -2995,7 +3252,7 @@ static void ShowDemoWindowMisc() ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); ImGui::PushAllowKeyboardFocus(false); ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf)); - //ImGui::SameLine(); HelpMarker("Use ImGui::PushAllowKeyboardFocus(bool)\nto disable tabbing through certain widgets."); + //ImGui::SameLine(); HelpMarker("Use ImGui::PushAllowKeyboardFocus(bool) to disable tabbing through certain widgets."); ImGui::PopAllowKeyboardFocus(); ImGui::InputText("5", buf, IM_ARRAYSIZE(buf)); ImGui::TreePop(); @@ -3045,19 +3302,27 @@ static void ShowDemoWindowMisc() { ImGui::TextWrapped("You can use ImGui::GetMouseDragDelta(0) to query for the dragged amount on any widget."); for (int button = 0; button < 3; button++) - ImGui::Text("IsMouseDragging(%d):\n w/ default threshold: %d,\n w/ zero threshold: %d\n w/ large threshold: %d", - button, ImGui::IsMouseDragging(button), ImGui::IsMouseDragging(button, 0.0f), ImGui::IsMouseDragging(button, 20.0f)); + { + ImGui::Text("IsMouseDragging(%d):", button); + ImGui::Text(" w/ default threshold: %d,", ImGui::IsMouseDragging(button)); + ImGui::Text(" w/ zero threshold: %d,", ImGui::IsMouseDragging(button, 0.0f)); + ImGui::Text(" w/ large threshold: %d,", ImGui::IsMouseDragging(button, 20.0f)); + } ImGui::Button("Drag Me"); if (ImGui::IsItemActive()) ImGui::GetForegroundDrawList()->AddLine(io.MouseClickedPos[0], io.MousePos, ImGui::GetColorU32(ImGuiCol_Button), 4.0f); // Draw a line between the button and the mouse cursor - // Drag operations gets "unlocked" when the mouse has moved past a certain threshold (the default threshold is stored in io.MouseDragThreshold) - // You can request a lower or higher threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta() + // Drag operations gets "unlocked" when the mouse has moved past a certain threshold + // (the default threshold is stored in io.MouseDragThreshold). You can request a lower or higher + // threshold using the second parameter of IsMouseDragging() and GetMouseDragDelta(). ImVec2 value_raw = ImGui::GetMouseDragDelta(0, 0.0f); ImVec2 value_with_lock_threshold = ImGui::GetMouseDragDelta(0); ImVec2 mouse_delta = io.MouseDelta; - ImGui::Text("GetMouseDragDelta(0):\n w/ default threshold: (%.1f, %.1f),\n w/ zero threshold: (%.1f, %.1f)\nMouseDelta: (%.1f, %.1f)", value_with_lock_threshold.x, value_with_lock_threshold.y, value_raw.x, value_raw.y, mouse_delta.x, mouse_delta.y); + ImGui::Text("GetMouseDragDelta(0):"); + ImGui::Text(" w/ default threshold: (%.1f, %.1f)", value_with_lock_threshold.x, value_with_lock_threshold.y); + ImGui::Text(" w/ zero threshold: (%.1f, %.1f)", value_raw.x, value_raw.y); + ImGui::Text("io.MouseDelta: (%.1f, %.1f)", mouse_delta.x, mouse_delta.y); ImGui::TreePop(); } @@ -3066,9 +3331,13 @@ static void ShowDemoWindowMisc() const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" }; IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); - ImGui::Text("Current mouse cursor = %d: %s", ImGui::GetMouseCursor(), mouse_cursors_names[ImGui::GetMouseCursor()]); + ImGuiMouseCursor current = ImGui::GetMouseCursor(); + ImGui::Text("Current mouse cursor = %d: %s", current, mouse_cursors_names[current]); ImGui::Text("Hover to see mouse cursors:"); - ImGui::SameLine(); HelpMarker("Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, otherwise your backend needs to handle it."); + ImGui::SameLine(); HelpMarker( + "Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. " + "If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, " + "otherwise your backend needs to handle it."); for (int i = 0; i < ImGuiMouseCursor_COUNT; i++) { char label[32]; @@ -3107,11 +3376,12 @@ void ImGui::ShowAboutWindow(bool* p_open) ImGuiStyle& style = ImGui::GetStyle(); bool copy_to_clipboard = ImGui::Button("Copy to clipboard"); - ImGui::BeginChildFrame(ImGui::GetID("cfginfos"), ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 18), ImGuiWindowFlags_NoMove); + ImVec2 child_size = ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 18); + ImGui::BeginChildFrame(ImGui::GetID("cfg_infos"), child_size, ImGuiWindowFlags_NoMove); if (copy_to_clipboard) { ImGui::LogToClipboard(); - ImGui::LogText("```\n"); // Back quotes will make the text appears without formatting when pasting to GitHub + ImGui::LogText("```\n"); // Back quotes will make text appears without formatting when pasting on GitHub } ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); @@ -3228,7 +3498,8 @@ void ImGui::ShowAboutWindow(bool* p_open) //----------------------------------------------------------------------------- // Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options. -// Here we use the simplified Combo() api that packs items into a single literal string. Useful for quick combo boxes where the choices are known locally. +// Here we use the simplified Combo() api that packs items into a single literal string. +// Useful for quick combo boxes where the choices are known locally. bool ImGui::ShowStyleSelector(const char* label) { static int style_idx = -1; @@ -3271,9 +3542,99 @@ void ImGui::ShowFontSelector(const char* label) "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame()."); } +// [Internal] Display details for a single font, called by ShowStyleEditor(). +static void NodeFont(ImFont* font) +{ + ImGuiIO& io = ImGui::GetIO(); + ImGuiStyle& style = ImGui::GetStyle(); + bool font_details_opened = ImGui::TreeNode(font, "Font: \"%s\"\n%.2f px, %d glyphs, %d file(s)", + font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size, font->ConfigDataCount); + ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { io.FontDefault = font; } + if (!font_details_opened) + return; + + ImGui::PushFont(font); + ImGui::Text("The quick brown fox jumps over the lazy dog"); + ImGui::PopFont(); + ImGui::DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); // Scale only this font + ImGui::SameLine(); HelpMarker( + "Note than the default embedded font is NOT meant to be scaled.\n\n" + "Font are currently rendered into bitmaps at a given size at the time of building the atlas. " + "You may oversample them to get some flexibility with scaling. " + "You can also render at multiple sizes and select which one to use at runtime.\n\n" + "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)"); + ImGui::InputFloat("Font offset", &font->DisplayOffset.y, 1, 1, "%.0f"); + ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); + ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); + ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); + const int surface_sqrt = (int)sqrtf((float)font->MetricsTotalSurface); + ImGui::Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, surface_sqrt, surface_sqrt); + for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) + if (font->ConfigData) + if (const ImFontConfig* cfg = &font->ConfigData[config_i]) + ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", + config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); + if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) + { + // Display all glyphs of the fonts in separate pages of 256 characters + const ImU32 glyph_col = ImGui::GetColorU32(ImGuiCol_Text); + for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256) + { + // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k) + // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT + // is large // (if ImWchar==ImWchar32 we will do at least about 272 queries here) + if (!(base & 4095) && font->IsGlyphRangeUnused(base, base + 4095)) + { + base += 4096 - 256; + continue; + } + + int count = 0; + for (unsigned int n = 0; n < 256; n++) + if (font->FindGlyphNoFallback((ImWchar)(base + n))) + count++; + if (count <= 0) + continue; + if (!ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) + continue; + float cell_size = font->FontSize * 1; + float cell_spacing = style.ItemSpacing.y; + ImVec2 base_pos = ImGui::GetCursorScreenPos(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + for (unsigned int n = 0; n < 256; n++) + { + // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions + // available here and thus cannot easily generate a zero-terminated UTF-8 encoded string. + ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); + ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); + const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); + draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); + if (glyph) + font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); + if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2)) + { + ImGui::BeginTooltip(); + ImGui::Text("Codepoint: U+%04X", base + n); + ImGui::Separator(); + ImGui::Text("Visible: %d", glyph->Visible); + ImGui::Text("AdvanceX: %.1f", glyph->AdvanceX); + ImGui::Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); + ImGui::Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); + ImGui::EndTooltip(); + } + } + ImGui::Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16)); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + ImGui::TreePop(); +} + void ImGui::ShowStyleEditor(ImGuiStyle* ref) { - // You can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it compares to an internally stored reference) + // You can pass in a reference ImGuiStyle structure to compare to, revert to and save to + // (without a reference style pointer, we will use one compared locally as a reference) ImGuiStyle& style = ImGui::GetStyle(); static ImGuiStyle ref_saved_style; @@ -3291,14 +3652,14 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ref_saved_style = style; ImGui::ShowFontSelector("Fonts##Selector"); - // Simplified Settings + // Simplified Settings (expose floating-pointer border sizes as boolean representing 0.0f or 1.0f) if (ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f")) style.GrabRounding = style.FrameRounding; // Make GrabRounding always the same value as FrameRounding - { bool window_border = (style.WindowBorderSize > 0.0f); if (ImGui::Checkbox("WindowBorder", &window_border)) style.WindowBorderSize = window_border ? 1.0f : 0.0f; } + { bool border = (style.WindowBorderSize > 0.0f); if (ImGui::Checkbox("WindowBorder", &border)) { style.WindowBorderSize = border ? 1.0f : 0.0f; } } ImGui::SameLine(); - { bool frame_border = (style.FrameBorderSize > 0.0f); if (ImGui::Checkbox("FrameBorder", &frame_border)) style.FrameBorderSize = frame_border ? 1.0f : 0.0f; } + { bool border = (style.FrameBorderSize > 0.0f); if (ImGui::Checkbox("FrameBorder", &border)) { style.FrameBorderSize = border ? 1.0f : 0.0f; } } ImGui::SameLine(); - { bool popup_border = (style.PopupBorderSize > 0.0f); if (ImGui::Checkbox("PopupBorder", &popup_border)) style.PopupBorderSize = popup_border ? 1.0f : 0.0f; } + { bool border = (style.PopupBorderSize > 0.0f); if (ImGui::Checkbox("PopupBorder", &border)) { style.PopupBorderSize = border ? 1.0f : 0.0f; } } // Save/Revert button if (ImGui::Button("Save Ref")) @@ -3307,7 +3668,9 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::Button("Revert Ref")) style = *ref; ImGui::SameLine(); - HelpMarker("Save/Revert in local non-persistent storage. Default Colors definition are not affected. Use \"Export\" below to save them somewhere."); + HelpMarker( + "Save/Revert in local non-persistent storage. Default Colors definition are not affected. " + "Use \"Export\" below to save them somewhere."); ImGui::Separator(); @@ -3344,9 +3707,12 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0")) style.WindowMenuButtonPosition = window_menu_button_position - 1; ImGui::Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0"); - ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content."); - ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content."); - ImGui::Text("Safe Area Padding"); ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); + ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); + ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content."); + ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f"); + ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content."); + ImGui::Text("Safe Area Padding"); + ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::EndTabItem(); } @@ -3367,7 +3733,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) const ImVec4& col = style.Colors[i]; const char* name = ImGui::GetStyleColorName(i); if (!output_only_modified || memcmp(&col, &ref->Colors[i], sizeof(ImVec4)) != 0) - ImGui::LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE, name, 23 - (int)strlen(name), "", col.x, col.y, col.z, col.w); + ImGui::LogText("colors[ImGuiCol_%s]%*s= ImVec4(%.2ff, %.2ff, %.2ff, %.2ff);" IM_NEWLINE, + name, 23 - (int)strlen(name), "", col.x, col.y, col.z, col.w); } ImGui::LogFinish(); } @@ -3378,10 +3745,13 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) filter.Draw("Filter colors", ImGui::GetFontSize() * 16); static ImGuiColorEditFlags alpha_flags = 0; - if (ImGui::RadioButton("Opaque", alpha_flags == 0)) { alpha_flags = 0; } ImGui::SameLine(); - if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_AlphaPreview)) { alpha_flags = ImGuiColorEditFlags_AlphaPreview; } ImGui::SameLine(); - if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine(); - HelpMarker("In the color list:\nLeft-click on colored square to open color picker,\nRight-click to open edit options menu."); + if (ImGui::RadioButton("Opaque", alpha_flags == ImGuiColorEditFlags_None)) { alpha_flags = ImGuiColorEditFlags_None; } ImGui::SameLine(); + if (ImGui::RadioButton("Alpha", alpha_flags == ImGuiColorEditFlags_AlphaPreview)) { alpha_flags = ImGuiColorEditFlags_AlphaPreview; } ImGui::SameLine(); + if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine(); + HelpMarker( + "In the color list:\n" + "Left-click on colored square to open color picker,\n" + "Right-click to open edit options menu."); ImGui::BeginChild("##colors", ImVec2(0, 0), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); ImGui::PushItemWidth(-160); @@ -3394,10 +3764,11 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags); if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0) { - // Tips: in a real user application, you may want to merge and use an icon font into the main font, so instead of "Save"/"Revert" you'd use icons. + // Tips: in a real user application, you may want to merge and use an icon font into the main font, + // so instead of "Save"/"Revert" you'd use icons! // Read the FAQ and docs/FONTS.txt about using icon fonts. It's really easy and super convenient! - ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) ref->Colors[i] = style.Colors[i]; - ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) style.Colors[i] = ref->Colors[i]; + ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) { ref->Colors[i] = style.Colors[i]; } + ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) { style.Colors[i] = ref->Colors[i]; } } ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); ImGui::TextUnformatted(name); @@ -3419,76 +3790,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { ImFont* font = atlas->Fonts[i]; ImGui::PushID(font); - bool font_details_opened = ImGui::TreeNode(font, "Font %d: \"%s\"\n%.2f px, %d glyphs, %d file(s)", i, font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size, font->ConfigDataCount); - ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { io.FontDefault = font; } - if (font_details_opened) - { - ImGui::PushFont(font); - ImGui::Text("The quick brown fox jumps over the lazy dog"); - ImGui::PopFont(); - ImGui::DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); // Scale only this font - ImGui::SameLine(); HelpMarker("Note than the default embedded font is NOT meant to be scaled.\n\nFont are currently rendered into bitmaps at a given size at the time of building the atlas. You may oversample them to get some flexibility with scaling. You can also render at multiple sizes and select which one to use at runtime.\n\n(Glimmer of hope: the atlas system should hopefully be rewritten in the future to make scaling more natural and automatic.)"); - ImGui::InputFloat("Font offset", &font->DisplayOffset.y, 1, 1, "%.0f"); - ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); - ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); - ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); - const float surface_sqrt = sqrtf((float)font->MetricsTotalSurface); - ImGui::Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, (int)surface_sqrt, (int)surface_sqrt); - for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) - if (font->ConfigData) - if (const ImFontConfig* cfg = &font->ConfigData[config_i]) - ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); - if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) - { - // Display all glyphs of the fonts in separate pages of 256 characters - for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256) - { - // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k) - // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT is large. - // (if ImWchar==ImWchar32 we will do at least about 272 queries here) - if (!(base & 4095) && font->IsGlyphRangeUnused(base, base + 4095)) - { - base += 4096 - 256; - continue; - } - - int count = 0; - for (unsigned int n = 0; n < 256; n++) - count += font->FindGlyphNoFallback((ImWchar)(base + n)) ? 1 : 0; - if (count > 0 && ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) - { - float cell_size = font->FontSize * 1; - float cell_spacing = style.ItemSpacing.y; - ImVec2 base_pos = ImGui::GetCursorScreenPos(); - ImDrawList* draw_list = ImGui::GetWindowDrawList(); - for (unsigned int n = 0; n < 256; n++) - { - ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); - ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); - const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); - draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); - if (glyph) - font->RenderChar(draw_list, cell_size, cell_p1, ImGui::GetColorU32(ImGuiCol_Text), (ImWchar)(base + n)); // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions available to generate a string. - if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2)) - { - ImGui::BeginTooltip(); - ImGui::Text("Codepoint: U+%04X", base + n); - ImGui::Separator(); - ImGui::Text("Visible: %d", glyph->Visible); - ImGui::Text("AdvanceX: %.1f", glyph->AdvanceX); - ImGui::Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); - ImGui::Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); - ImGui::EndTooltip(); - } - } - ImGui::Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16)); - ImGui::TreePop(); - } - } - ImGui::TreePop(); - } - ImGui::TreePop(); - } + NodeFont(font); ImGui::PopID(); } if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) @@ -3499,11 +3801,20 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::TreePop(); } - HelpMarker("Those are old settings provided for convenience.\nHowever, the _correct_ way of scaling your UI is currently to reload your font at the designed size, rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure."); + // Post-baking font scaling. Note that this is NOT the nice way of scaling fonts, read below. + // (we enforce hard clamping manually as by default DragFloat/SliderFloat allows CTRL+Click text to get out of bounds). + const float MIN_SCALE = 0.3f; + const float MAX_SCALE = 2.0f; + HelpMarker( + "Those are old settings provided for convenience.\n" + "However, the _correct_ way of scaling your UI is currently to reload your font at the designed size, " + "rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n" + "Using those settings here will give you poor quality results."); static float window_scale = 1.0f; - if (ImGui::DragFloat("window scale", &window_scale, 0.005f, 0.3f, 2.0f, "%.2f")) // scale only this window - ImGui::SetWindowFontScale(window_scale); - ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, 0.3f, 2.0f, "%.2f"); // scale everything + if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f")) // Scale only this window + ImGui::SetWindowFontScale(IM_MAX(window_scale, MIN_SCALE)); + if (ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f")) // Scale everything + io.FontGlobalScale = IM_MAX(io.FontGlobalScale, MIN_SCALE); ImGui::PopItemWidth(); ImGui::EndTabItem(); @@ -3511,7 +3822,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::BeginTabItem("Rendering")) { - ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines); ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); + ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines); + ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); ImGui::PushItemWidth(100); ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f"); @@ -3538,7 +3850,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) // Demonstrate creating a "main" fullscreen menu bar and populating it. // Note the difference between BeginMainMenuBar() and BeginMenuBar(): -// - BeginMenuBar() = menu-bar inside current window we Begin()-ed into (the window needs the ImGuiWindowFlags_MenuBar flag) +// - BeginMenuBar() = menu-bar inside current window (which needs the ImGuiWindowFlags_MenuBar flag!) // - BeginMainMenuBar() = helper to create menu-bar-sized window at the top of the main viewport + call BeginMenuBar() into it. static void ShowExampleAppMainMenuBar() { @@ -3563,7 +3875,8 @@ static void ShowExampleAppMainMenuBar() } } -// Note that shortcuts are currently provided for display only (future version will add flags to BeginMenu to process shortcuts) +// Note that shortcuts are currently provided for display only +// (future version will add explicit flags to BeginMenu() to request processing shortcuts) static void ShowExampleMenuFile() { ImGui::MenuItem("(dummy menu)", NULL, false, false); @@ -3645,7 +3958,7 @@ static void ShowExampleMenuFile() //----------------------------------------------------------------------------- // Demonstrate creating a simple console window, with scrolling, filtering, completion and history. -// For the console example, here we are using a more C++ like approach of declaring a class to hold the data and the functions. +// For the console example, we are using a more C++ like approach of declaring a class to hold both data and functions. struct ExampleAppConsole { char InputBuf[256]; @@ -3662,10 +3975,12 @@ struct ExampleAppConsole ClearLog(); memset(InputBuf, 0, sizeof(InputBuf)); HistoryPos = -1; + + // "CLASSIFY" is here to provide the test case where "C"+[tab] completes to "CL" and display multiple matches. Commands.push_back("HELP"); Commands.push_back("HISTORY"); Commands.push_back("CLEAR"); - Commands.push_back("CLASSIFY"); // "classify" is only here to provide an example of "C"+[tab] completing to "CL" and displaying matches. + Commands.push_back("CLASSIFY"); AutoScroll = true; ScrollToBottom = false; AddLog("Welcome to Dear ImGui!"); @@ -3678,10 +3993,10 @@ struct ExampleAppConsole } // Portable helpers - static int Stricmp(const char* str1, const char* str2) { int d; while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } return d; } - static int Strnicmp(const char* str1, const char* str2, int n) { int d = 0; while (n > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; n--; } return d; } - static char* Strdup(const char *str) { size_t len = strlen(str) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)str, len); } - static void Strtrim(char* str) { char* str_end = str + strlen(str); while (str_end > str && str_end[-1] == ' ') str_end--; *str_end = 0; } + static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; } + static int Strnicmp(const char* s1, const char* s2, int n) { int d = 0; while (n > 0 && (d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; n--; } return d; } + static char* Strdup(const char* s) { size_t len = strlen(s) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } + static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; } void ClearLog() { @@ -3711,7 +4026,8 @@ struct ExampleAppConsole return; } - // As a specific feature guaranteed by the library, after calling Begin() the last Item represent the title bar. So e.g. IsItemHovered() will return true when hovering the title bar. + // As a specific feature guaranteed by the library, after calling Begin() the last Item represent the title bar. + // So e.g. IsItemHovered() will return true when hovering the title bar. // Here we create a context menu only available from the title bar. if (ImGui::BeginPopupContextItem()) { @@ -3720,14 +4036,16 @@ struct ExampleAppConsole ImGui::EndPopup(); } - ImGui::TextWrapped("This example implements a console with basic coloring, completion and history. A more elaborate implementation may want to store entries along with extra data such as timestamp, emitter, etc."); + ImGui::TextWrapped( + "This example implements a console with basic coloring, completion and history. A more elaborate " + "implementation may want to store entries along with extra data such as timestamp, emitter, etc."); ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion."); // TODO: display items starting from the bottom if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine(); if (ImGui::SmallButton("Add Dummy Error")) { AddLog("[error] something went wrong"); } ImGui::SameLine(); - if (ImGui::SmallButton("Clear")) { ClearLog(); } ImGui::SameLine(); + if (ImGui::SmallButton("Clear")) { ClearLog(); } ImGui::SameLine(); bool copy_to_clipboard = ImGui::SmallButton("Copy"); //static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); } @@ -3747,25 +4065,38 @@ struct ExampleAppConsole Filter.Draw("Filter (\"incl,-excl\") (\"error\")", 180); ImGui::Separator(); - const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); // 1 separator, 1 input text - ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); // Leave room for 1 separator + 1 InputText + // Reserve enough left-over height for 1 separator + 1 input text + const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); + ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); if (ImGui::BeginPopupContextWindow()) { if (ImGui::Selectable("Clear")) ClearLog(); ImGui::EndPopup(); } - // Display every line as a separate entry so we can change their color or add custom widgets. If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end()); - // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping to only process visible items. - // You can seek and display only the lines that are visible using the ImGuiListClipper helper, if your elements are evenly spaced and you have cheap random access to the elements. - // To use the clipper we could replace the 'for (int i = 0; i < Items.Size; i++)' loop with: - // ImGuiListClipper clipper(Items.Size); - // while (clipper.Step()) + // Display every line as a separate entry so we can change their color or add custom widgets. + // If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end()); + // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping + // to only process visible items. The clipper will automatically measure the height of your first item and then + // "seek" to display only items in the visible area. + // To use the clipper we can replace your standard loop: + // for (int i = 0; i < Items.Size; i++) + // With: + // ImGuiListClipper clipper(Items.Size); + // while (clipper.Step()) // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) - // However, note that you can not use this code as is if a filter is active because it breaks the 'cheap random-access' property. We would need random-access on the post-filtered list. - // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices that passed the filtering test, recomputing this array when user changes the filter, - // and appending newly elements as they are inserted. This is left as a task to the user until we can manage to improve this example code! - // If your items are of variable size you may want to implement code similar to what ImGuiListClipper does. Or split your data into fixed height items to allow random-seeking into your list. + // - That your items are evenly spaced (same height) + // - That you have cheap random access to your elements (you can access them given their index, + // without processing all the ones before) + // You cannot this code as-is if a filter is active because it breaks the 'cheap random-access' property. + // We would need random-access on the post-filtered list. + // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices + // or offsets of items that passed the filtering test, recomputing this array when user changes the filter, + // and appending newly elements as they are inserted. This is left as a task to the user until we can manage + // to improve this example code! + // If your items are of variable height: + // - Split them into same height items would be simpler and facilitate random-seeking into your list. + // - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // Tighten spacing if (copy_to_clipboard) ImGui::LogToClipboard(); @@ -3775,12 +4106,16 @@ struct ExampleAppConsole if (!Filter.PassFilter(item)) continue; - // Normally you would store more information in your item (e.g. make Items[] an array of structure, store color/type etc.) - bool pop_color = false; - if (strstr(item, "[error]")) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.4f, 0.4f, 1.0f)); pop_color = true; } - else if (strncmp(item, "# ", 2) == 0) { ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 0.8f, 0.6f, 1.0f)); pop_color = true; } + // Normally you would store more information in your item than just a string. + // (e.g. make Items[] an array of structure, store color/type etc.) + ImVec4 color; + bool has_color = false; + if (strstr(item, "[error]")) { color = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); has_color = true; } + else if (strncmp(item, "# ", 2) == 0) { color = ImVec4(1.0f, 0.8f, 0.6f, 1.0f); has_color = true; } + if (has_color) + ImGui::PushStyleColor(ImGuiCol_Text, color); ImGui::TextUnformatted(item); - if (pop_color) + if (has_color) ImGui::PopStyleColor(); } if (copy_to_clipboard) @@ -3796,7 +4131,8 @@ struct ExampleAppConsole // Command-line bool reclaim_focus = false; - if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), ImGuiInputTextFlags_EnterReturnsTrue|ImGuiInputTextFlags_CallbackCompletion|ImGuiInputTextFlags_CallbackHistory, &TextEditCallbackStub, (void*)this)) + ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory; + if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), input_text_flags, &TextEditCallbackStub, (void*)this)) { char* s = InputBuf; Strtrim(s); @@ -3818,7 +4154,8 @@ struct ExampleAppConsole { AddLog("# %s\n", command_line); - // Insert into history. First find match and delete it so it can be pushed to the back. This isn't trying to be smart or optimal. + // Insert into history. First find match and delete it so it can be pushed to the back. + // This isn't trying to be smart or optimal. HistoryPos = -1; for (int i = History.Size-1; i >= 0; i--) if (Stricmp(History[i], command_line) == 0) @@ -3851,11 +4188,12 @@ struct ExampleAppConsole AddLog("Unknown command: '%s'\n", command_line); } - // On commad input, we scroll to bottom even if AutoScroll==false + // On command input, we scroll to bottom even if AutoScroll==false ScrollToBottom = true; } - static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) // In C++11 you are better off using lambdas for this sort of forwarding callbacks + // In C++11 you'd be better off using lambdas for this sort of forwarding callbacks + static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) { ExampleAppConsole* console = (ExampleAppConsole*)data->UserData; return console->TextEditCallback(data); @@ -3894,14 +4232,15 @@ struct ExampleAppConsole } else if (candidates.Size == 1) { - // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing + // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing. data->DeleteChars((int)(word_start-data->Buf), (int)(word_end-word_start)); data->InsertChars(data->CursorPos, candidates[0]); data->InsertChars(data->CursorPos, " "); } else { - // Multiple matches. Complete as much as we can, so inputing "C" will complete to "CL" and display "CLEAR" and "CLASSIFY" + // Multiple matches. Complete as much as we can.. + // So inputing "C"+Tab will complete to "CL" then display "CLEAR" and "CLASSIFY" as matches. int match_len = (int)(word_end - word_start); for (;;) { @@ -3980,8 +4319,8 @@ struct ExampleAppLog { ImGuiTextBuffer Buf; ImGuiTextFilter Filter; - ImVector LineOffsets; // Index to lines offset. We maintain this with AddLog() calls, allowing us to have a random access on lines - bool AutoScroll; // Keep scrolling if already at the bottom + ImVector LineOffsets; // Index to lines offset. We maintain this with AddLog() calls. + bool AutoScroll; // Keep scrolling if already at the bottom. ExampleAppLog() { @@ -4034,7 +4373,7 @@ struct ExampleAppLog Filter.Draw("Filter", -100.0f); ImGui::Separator(); - ImGui::BeginChild("scrolling", ImVec2(0,0), false, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); if (clear) Clear(); @@ -4048,8 +4387,8 @@ struct ExampleAppLog { // In this example we don't use the clipper when Filter is enabled. // This is because we don't have a random access on the result on our filter. - // A real application processing logs with ten of thousands of entries may want to store the result of search/filter. - // especially if the filtering function is not trivial (e.g. reg-exp). + // A real application processing logs with ten of thousands of entries may want to store the result of + // search/filter.. especially if the filtering function is not trivial (e.g. reg-exp). for (int line_no = 0; line_no < LineOffsets.Size; line_no++) { const char* line_start = buf + LineOffsets[line_no]; @@ -4062,13 +4401,17 @@ struct ExampleAppLog { // The simplest and easy way to display the entire buffer: // ImGui::TextUnformatted(buf_begin, buf_end); - // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward to skip non-visible lines. - // Here we instead demonstrate using the clipper to only process lines that are within the visible area. - // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them on your side is recommended. - // Using ImGuiListClipper requires A) random access into your data, and B) items all being the same height, + // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward + // to skip non-visible lines. Here we instead demonstrate using the clipper to only process lines that are + // within the visible area. + // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them + // on your side is recommended. Using ImGuiListClipper requires + // - A) random access into your data + // - B) items all being the same height, // both of which we can handle since we an array pointing to the beginning of each line of text. - // When using the filter (in the block of code above) we don't have random access into the data to display anymore, which is why we don't use the clipper. - // Storing or skimming through the search result would make it possible (and would be recommended if you want to search through tens of thousands of entries) + // When using the filter (in the block of code above) we don't have random access into the data to display + // anymore, which is why we don't use the clipper. Storing or skimming through the search result would make + // it possible (and would be recommended if you want to search through tens of thousands of entries). ImGuiListClipper clipper; clipper.Begin(LineOffsets.Size); while (clipper.Step()) @@ -4105,12 +4448,14 @@ static void ShowExampleAppLog(bool* p_open) if (ImGui::SmallButton("[Debug] Add 5 entries")) { static int counter = 0; + const char* categories[3] = { "info", "warn", "error" }; + const char* words[] = { "Bumfuzzled", "Cattywampus", "Snickersnee", "Abibliophobia", "Absquatulate", "Nincompoop", "Pauciloquent" }; for (int n = 0; n < 5; n++) { - const char* categories[3] = { "info", "warn", "error" }; - const char* words[] = { "Bumfuzzled", "Cattywampus", "Snickersnee", "Abibliophobia", "Absquatulate", "Nincompoop", "Pauciloquent" }; + const char* category = categories[counter % IM_ARRAYSIZE(categories)]; + const char* word = words[counter % IM_ARRAYSIZE(words)]; log.AddLog("[%05d] [%s] Hello, current time is %.1f, here's a word: '%s'\n", - ImGui::GetFrameCount(), categories[counter % IM_ARRAYSIZE(categories)], ImGui::GetTime(), words[counter % IM_ARRAYSIZE(words)]); + ImGui::GetFrameCount(), category, ImGui::GetTime(), word); counter++; } } @@ -4140,43 +4485,47 @@ static void ShowExampleAppLayout(bool* p_open) ImGui::EndMenuBar(); } - // left + // Left static int selected = 0; - ImGui::BeginChild("left pane", ImVec2(150, 0), true); - for (int i = 0; i < 100; i++) { - char label[128]; - sprintf(label, "MyObject %d", i); - if (ImGui::Selectable(label, selected == i)) - selected = i; + ImGui::BeginChild("left pane", ImVec2(150, 0), true); + for (int i = 0; i < 100; i++) + { + char label[128]; + sprintf(label, "MyObject %d", i); + if (ImGui::Selectable(label, selected == i)) + selected = i; + } + ImGui::EndChild(); } - ImGui::EndChild(); ImGui::SameLine(); - // right - ImGui::BeginGroup(); + // Right + { + ImGui::BeginGroup(); ImGui::BeginChild("item view", ImVec2(0, -ImGui::GetFrameHeightWithSpacing())); // Leave room for 1 line below us - ImGui::Text("MyObject: %d", selected); - ImGui::Separator(); - if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None)) + ImGui::Text("MyObject: %d", selected); + ImGui::Separator(); + if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None)) + { + if (ImGui::BeginTabItem("Description")) { - if (ImGui::BeginTabItem("Description")) - { - ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem("Details")) - { - ImGui::Text("ID: 0123456789"); - ImGui::EndTabItem(); - } - ImGui::EndTabBar(); + ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "); + ImGui::EndTabItem(); } + if (ImGui::BeginTabItem("Details")) + { + ImGui::Text("ID: 0123456789"); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } ImGui::EndChild(); if (ImGui::Button("Revert")) {} ImGui::SameLine(); if (ImGui::Button("Save")) {} - ImGui::EndGroup(); + ImGui::EndGroup(); + } } ImGui::End(); } @@ -4185,6 +4534,47 @@ static void ShowExampleAppLayout(bool* p_open) // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() //----------------------------------------------------------------------------- +static void ShowDummyObject(const char* prefix, int uid) +{ + // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID. + ImGui::PushID(uid); + ImGui::AlignTextToFramePadding(); // Text and Tree nodes are less high than framed widgets, here we add vertical spacing to make the tree lines equal high. + bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); + ImGui::NextColumn(); + ImGui::AlignTextToFramePadding(); + ImGui::Text("my sailor is rich"); + ImGui::NextColumn(); + if (node_open) + { + static float dummy_members[8] = { 0.0f,0.0f,1.0f,3.1416f,100.0f,999.0f }; + for (int i = 0; i < 8; i++) + { + ImGui::PushID(i); // Use field index as identifier. + if (i < 2) + { + ShowDummyObject("Child", 424242); + } + else + { + // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well) + ImGui::AlignTextToFramePadding(); + ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet; + ImGui::TreeNodeEx("Field", flags, "Field_%d", i); + ImGui::NextColumn(); + ImGui::SetNextItemWidth(-1); + if (i >= 5) + ImGui::InputFloat("##value", &dummy_members[i], 1.0f); + else + ImGui::DragFloat("##value", &dummy_members[i], 0.01f); + ImGui::NextColumn(); + } + ImGui::PopID(); + } + ImGui::TreePop(); + } + ImGui::PopID(); +} + // Demonstrate create a simple property editor. static void ShowExampleAppPropertyEditor(bool* p_open) { @@ -4195,57 +4585,19 @@ static void ShowExampleAppPropertyEditor(bool* p_open) return; } - HelpMarker("This example shows how you may implement a property editor using two columns.\nAll objects/fields data are dummies here.\nRemember that in many simple cases, you can use ImGui::SameLine(xxx) to position\nyour cursor horizontally instead of using the Columns() API."); + HelpMarker( + "This example shows how you may implement a property editor using two columns.\n" + "All objects/fields data are dummies here.\n" + "Remember that in many simple cases, you can use ImGui::SameLine(xxx) to position\n" + "your cursor horizontally instead of using the Columns() API."); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2,2)); ImGui::Columns(2); ImGui::Separator(); - struct funcs - { - static void ShowDummyObject(const char* prefix, int uid) - { - ImGui::PushID(uid); // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID. - ImGui::AlignTextToFramePadding(); // Text and Tree nodes are less high than regular widgets, here we add vertical spacing to make the tree lines equal high. - bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); - ImGui::NextColumn(); - ImGui::AlignTextToFramePadding(); - ImGui::Text("my sailor is rich"); - ImGui::NextColumn(); - if (node_open) - { - static float dummy_members[8] = { 0.0f,0.0f,1.0f,3.1416f,100.0f,999.0f }; - for (int i = 0; i < 8; i++) - { - ImGui::PushID(i); // Use field index as identifier. - if (i < 2) - { - ShowDummyObject("Child", 424242); - } - else - { - // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well) - ImGui::AlignTextToFramePadding(); - ImGui::TreeNodeEx("Field", ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet, "Field_%d", i); - ImGui::NextColumn(); - ImGui::SetNextItemWidth(-1); - if (i >= 5) - ImGui::InputFloat("##value", &dummy_members[i], 1.0f); - else - ImGui::DragFloat("##value", &dummy_members[i], 0.01f); - ImGui::NextColumn(); - } - ImGui::PopID(); - } - ImGui::TreePop(); - } - ImGui::PopID(); - } - }; - // Iterate dummy objects with dummy members (all the same data) for (int obj_i = 0; obj_i < 3; obj_i++) - funcs::ShowDummyObject("Object", obj_i); + ShowDummyObject("Object", obj_i); ImGui::Columns(1); ImGui::Separator(); @@ -4271,7 +4623,10 @@ static void ShowExampleAppLongText(bool* p_open) static ImGuiTextBuffer log; static int lines = 0; ImGui::Text("Printing unusually long amount of text."); - ImGui::Combo("Test type", &test_type, "Single call to TextUnformatted()\0Multiple calls to Text(), clipped\0Multiple calls to Text(), not clipped (slow)\0"); + ImGui::Combo("Test type", &test_type, + "Single call to TextUnformatted()\0" + "Multiple calls to Text(), clipped\0" + "Multiple calls to Text(), not clipped (slow)\0"); ImGui::Text("Buffer contents: %d lines, %d bytes", lines, log.size()); if (ImGui::Button("Clear")) { log.clear(); lines = 0; } ImGui::SameLine(); @@ -4291,7 +4646,7 @@ static void ShowExampleAppLongText(bool* p_open) case 1: { // Multiple calls to Text(), manually coarsely clipped - demonstrate how to use the ImGuiListClipper helper. - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0)); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); ImGuiListClipper clipper(lines); while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) @@ -4301,7 +4656,7 @@ static void ShowExampleAppLongText(bool* p_open) } case 2: // Multiple calls to Text(), not clipped (slow) - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0,0)); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); for (int i = 0; i < lines; i++) ImGui::Text("%i The quick brown fox jumps over the lazy dog", i); ImGui::PopStyleVar(); @@ -4325,7 +4680,10 @@ static void ShowExampleAppAutoResize(bool* p_open) } static int lines = 10; - ImGui::Text("Window will resize every-frame to the size of its content.\nNote that you probably don't want to query the window size to\noutput your content because that would create a feedback loop."); + ImGui::TextUnformatted( + "Window will resize every-frame to the size of its content.\n" + "Note that you probably don't want to query the window size to\n" + "output your content because that would create a feedback loop."); ImGui::SliderInt("Number of lines", &lines, 1, 20); for (int i = 0; i < lines; i++) ImGui::Text("%*sThis is line %d", i * 4, "", i); // Pad with space to extend size horizontally @@ -4339,12 +4697,24 @@ static void ShowExampleAppAutoResize(bool* p_open) // Demonstrate creating a window with custom resize constraints. static void ShowExampleAppConstrainedResize(bool* p_open) { - struct CustomConstraints // Helper functions to demonstrate programmatic constraints + struct CustomConstraints { - static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = (data->DesiredSize.x > data->DesiredSize.y ? data->DesiredSize.x : data->DesiredSize.y); } + // Helper functions to demonstrate programmatic constraints + static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y); } static void Step(ImGuiSizeCallbackData* data) { float step = (float)(int)(intptr_t)data->UserData; data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step); } }; + const char* test_desc[] = + { + "Resize vertical only", + "Resize horizontal only", + "Width > 100, Height > 100", + "Width 400-500", + "Height 400-500", + "Custom: Always Square", + "Custom: Fixed Steps (100)", + }; + static bool auto_resize = false; static int type = 0; static int display_lines = 10; @@ -4359,21 +4729,11 @@ static void ShowExampleAppConstrainedResize(bool* p_open) ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0; if (ImGui::Begin("Example: Constrained Resize", p_open, flags)) { - const char* desc[] = - { - "Resize vertical only", - "Resize horizontal only", - "Width > 100, Height > 100", - "Width 400-500", - "Height 400-500", - "Custom: Always Square", - "Custom: Fixed Steps (100)", - }; if (ImGui::Button("200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine(); if (ImGui::Button("500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine(); if (ImGui::Button("800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); } ImGui::SetNextItemWidth(200); - ImGui::Combo("Constraint", &type, desc, IM_ARRAYSIZE(desc)); + ImGui::Combo("Constraint", &type, test_desc, IM_ARRAYSIZE(test_desc)); ImGui::SetNextItemWidth(200); ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100); ImGui::Checkbox("Auto-resize", &auto_resize); @@ -4387,7 +4747,8 @@ static void ShowExampleAppConstrainedResize(bool* p_open) // [SECTION] Example App: Simple Overlay / ShowExampleAppSimpleOverlay() //----------------------------------------------------------------------------- -// Demonstrate creating a simple static window with no decoration + a context-menu to choose which corner of the screen to use. +// Demonstrate creating a simple static window with no decoration +// + a context-menu to choose which corner of the screen to use. static void ShowExampleAppSimpleOverlay(bool* p_open) { const float DISTANCE = 10.0f; @@ -4400,7 +4761,10 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); } ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background - if (ImGui::Begin("Example: Simple overlay", p_open, (corner != -1 ? ImGuiWindowFlags_NoMove : 0) | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav)) + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; + if (corner != -1) + window_flags |= ImGuiWindowFlags_NoMove; + if (ImGui::Begin("Example: Simple overlay", p_open, window_flags)) { ImGui::Text("Simple overlay\n" "in the corner of the screen.\n" "(right-click to change position)"); ImGui::Separator(); @@ -4427,7 +4791,8 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) //----------------------------------------------------------------------------- // Demonstrate using "##" and "###" in identifiers to manipulate ID generation. -// This apply to all regular items as well. Read FAQ section "How can I have multiple widgets with the same label? Can I have widget without a label? (Yes). A primer on the purpose of labels/IDs." for details. +// This apply to all regular items as well. +// Read FAQ section "How can I have multiple widgets with the same label?" for details. static void ShowExampleAppWindowTitles(bool*) { // By default, Windows are uniquely identified by their title. @@ -4466,10 +4831,10 @@ static void ShowExampleAppCustomRendering(bool* p_open) return; } - // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of overloaded operators, etc. - // Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your types and ImVec2/ImVec4. - // ImGui defines overloaded operators but they are internal to imgui.cpp and not exposed outside (to avoid messing with your types) - // In this example we are not using the maths operators! + // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of + // overloaded operators, etc. Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your + // types and ImVec2/ImVec4. Dear ImGui defines overloaded operators but they are internal to imgui.cpp and not + // exposed outside (to avoid messing with your types) In this example we are not using the maths operators! ImDrawList* draw_list = ImGui::GetWindowDrawList(); if (ImGui::BeginTabBar("##TabBar")) @@ -4483,17 +4848,19 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImGui::Text("Gradients"); ImVec2 gradient_size = ImVec2(ImGui::CalcItemWidth(), ImGui::GetFrameHeight()); { - ImVec2 p = ImGui::GetCursorScreenPos(); + ImVec2 p0 = ImGui::GetCursorScreenPos(); + ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y); ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); - draw_list->AddRectFilledMultiColor(p, ImVec2(p.x + gradient_size.x, p.y + gradient_size.y), col_a, col_b, col_b, col_a); + draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a); ImGui::InvisibleButton("##gradient1", gradient_size); } { - ImVec2 p = ImGui::GetCursorScreenPos(); + ImVec2 p0 = ImGui::GetCursorScreenPos(); + ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y); ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); - draw_list->AddRectFilledMultiColor(p, ImVec2(p.x + gradient_size.x, p.y + gradient_size.y), col_a, col_b, col_b, col_a); + draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a); ImGui::InvisibleButton("##gradient2", gradient_size); } @@ -4564,23 +4931,25 @@ static void ShowExampleAppCustomRendering(bool* p_open) if (points.Size >= 2) { ImGui::SameLine(); if (ImGui::Button("Undo")) { points.pop_back(); points.pop_back(); } } ImGui::Text("Left-click and drag to add lines,\nRight-click to undo"); - // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use IsItemHovered() - // But you can also draw directly and poll mouse/keyboard by yourself. You can manipulate the cursor using GetCursorPos() and SetCursorPos(). - // If you only use the ImDrawList API, you can notify the owner window of its extends by using SetCursorPos(max). - ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! - ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available - if (canvas_size.x < 50.0f) canvas_size.x = 50.0f; - if (canvas_size.y < 50.0f) canvas_size.y = 50.0f; - draw_list->AddRectFilledMultiColor(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), IM_COL32(50, 50, 50, 255), IM_COL32(50, 50, 60, 255), IM_COL32(60, 60, 70, 255), IM_COL32(50, 50, 60, 255)); - draw_list->AddRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), IM_COL32(255, 255, 255, 255)); + // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use + // IsItemHovered(). But you can also draw directly and poll mouse/keyboard by yourself. + // You can manipulate the cursor using GetCursorPos() and SetCursorPos(). + // If you only use the ImDrawList API, you can notify the owner window of its extends with SetCursorPos(max). + ImVec2 canvas_p = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! + ImVec2 canvas_sz = ImGui::GetContentRegionAvail(); // Resize canvas to what's available + if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f; + if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f; + draw_list->AddRectFilledMultiColor(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), IM_COL32(50, 50, 50, 255), IM_COL32(50, 50, 60, 255), IM_COL32(60, 60, 70, 255), IM_COL32(50, 50, 60, 255)); + draw_list->AddRect(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), IM_COL32(255, 255, 255, 255)); bool adding_preview = false; - ImGui::InvisibleButton("canvas", canvas_size); - ImVec2 mouse_pos_in_canvas = ImVec2(ImGui::GetIO().MousePos.x - canvas_pos.x, ImGui::GetIO().MousePos.y - canvas_pos.y); + ImGui::InvisibleButton("canvas", canvas_sz); + ImVec2 mouse_pos_global = ImGui::GetIO().MousePos; + ImVec2 mouse_pos_canvas = ImVec2(mouse_pos_global.x - canvas_p.x, mouse_pos_global.y - canvas_p.y); if (adding_line) { adding_preview = true; - points.push_back(mouse_pos_in_canvas); + points.push_back(mouse_pos_canvas); if (!ImGui::IsMouseDown(0)) adding_line = adding_preview = false; } @@ -4588,7 +4957,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) { if (!adding_line && ImGui::IsMouseClicked(0)) { - points.push_back(mouse_pos_in_canvas); + points.push_back(mouse_pos_canvas); adding_line = true; } if (ImGui::IsMouseClicked(1) && !points.empty()) @@ -4598,9 +4967,11 @@ static void ShowExampleAppCustomRendering(bool* p_open) points.pop_back(); } } - draw_list->PushClipRect(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), true); // clip lines within the canvas (if we resize it, etc.) + + // Draw all lines in the canvas (with a clipping rectangle so they don't stray out of it). + draw_list->PushClipRect(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), true); for (int i = 0; i < points.Size - 1; i += 2) - draw_list->AddLine(ImVec2(canvas_pos.x + points[i].x, canvas_pos.y + points[i].y), ImVec2(canvas_pos.x + points[i + 1].x, canvas_pos.y + points[i + 1].y), IM_COL32(255, 255, 0, 255), 2.0f); + draw_list->AddLine(ImVec2(canvas_p.x + points[i].x, canvas_p.y + points[i].y), ImVec2(canvas_p.x + points[i + 1].x, canvas_p.y + points[i + 1].y), IM_COL32(255, 255, 0, 255), 2.0f); draw_list->PopClipRect(); if (adding_preview) points.pop_back(); @@ -4638,12 +5009,12 @@ static void ShowExampleAppCustomRendering(bool* p_open) // Simplified structure to mimic a Document model struct MyDocument { - const char* Name; // Document title - bool Open; // Set when the document is open (in this demo, we keep an array of all available documents to simplify the demo) - bool OpenPrev; // Copy of Open from last update. - bool Dirty; // Set when the document has been modified - bool WantClose; // Set when the document - ImVec4 Color; // An arbitrary variable associated to the document + const char* Name; // Document title + bool Open; // Set when open (we keep an array of all available documents to simplify demo code!) + bool OpenPrev; // Copy of Open from last update. + bool Dirty; // Set when the document has been modified + bool WantClose; // Set when the document + ImVec4 Color; // An arbitrary variable associated to the document MyDocument(const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f,1.0f,1.0f,1.0f)) { @@ -4707,10 +5078,11 @@ struct ExampleAppDocuments }; // [Optional] Notify the system of Tabs/Windows closure that happened outside the regular tab interface. -// If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo, as opposed -// to clicking on the regular tab closing button) and stops being submitted, it will take a frame for the tab bar to notice its absence. -// During this frame there will be a gap in the tab bar, and if the tab that has disappeared was the selected one, the tab bar -// will report no selected tab during the frame. This will effectively give the impression of a flicker for one frame. +// If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo, +// as opposed to clicking on the regular tab closing button) and stops being submitted, it will take a frame for +// the tab bar to notice its absence. During this frame there will be a gap in the tab bar, and if the tab that has +// disappeared was the selected one, the tab bar will report no selected tab during the frame. This will effectively +// give the impression of a flicker for one frame. // We call SetTabItemClosed() to manually notify the Tab Bar or Docking system of removed tabs to avoid this glitch. // Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag. static void NotifyOfDocumentsClosedElsewhere(ExampleAppDocuments& app) diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index d256c8d4..057ddb65 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (drawing and font code) /* @@ -16,7 +16,7 @@ Index of this file: // [SECTION] ImFontAtlas glyph ranges helpers // [SECTION] ImFontGlyphRangesBuilder // [SECTION] ImFont -// [SECTION] Internal Render Helpers +// [SECTION] ImGui Internal Render Helpers // [SECTION] Decompression code // [SECTION] Default font data (ProggyClean.ttf) @@ -36,7 +36,7 @@ Index of this file: #include // vsnprintf, sscanf, printf #if !defined(alloca) -#if defined(__GLIBC__) || defined(__sun) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__SWITCH__) +#if defined(__GLIBC__) || defined(__sun) || defined(__APPLE__) || defined(__NEWLIB__) #include // alloca (glibc uses . Note that Cygwin may have _WIN32 defined, so the order matters here) #elif defined(_WIN32) #include // alloca @@ -120,7 +120,7 @@ namespace IMGUI_STB_NAMESPACE #ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) #ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION #define STBRP_STATIC -#define STBRP_ASSERT(x) IM_ASSERT(x) +#define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0) #define STBRP_SORT ImQsort #define STB_RECT_PACK_IMPLEMENTATION #endif @@ -135,7 +135,7 @@ namespace IMGUI_STB_NAMESPACE #ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION #define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) #define STBTT_free(x,u) ((void)(u), IM_FREE(x)) -#define STBTT_assert(x) IM_ASSERT(x) +#define STBTT_assert(x) do { IM_ASSERT(x); } while(0) #define STBTT_fmod(x,y) ImFmod(x,y) #define STBTT_sqrt(x) ImSqrt(x) #define STBTT_pow(x,y) ImPow(x,y) @@ -615,7 +615,7 @@ void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, c _IdxWritePtr += 6; } -// On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superflous function calls to optimize debug/non-inlined builds. +// On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superfluous function calls to optimize debug/non-inlined builds. // Those macros expects l-values. #define IM_NORMALIZE2F_OVER_ZERO(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } } while (0) #define IM_FIXNORMAL2F(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; } while (0) @@ -627,13 +627,12 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 if (points_count < 2) return; - const ImVec2 uv = _Data->TexUvWhitePixel; - + const ImVec2 opaque_uv = _Data->TexUvWhitePixel; int count = points_count; if (!closed) count = points_count-1; - const bool thick_line = thickness > 1.0f; + const bool thick_line = (thickness > 1.0f); if (Flags & ImDrawListFlags_AntiAliasedLines) { // Anti-aliased stroke @@ -684,7 +683,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 dm_x *= AA_SIZE; dm_y *= AA_SIZE; - // Add temporary vertexes + // Add temporary vertices ImVec2* out_vtx = &temp_points[i2*2]; out_vtx[0].x = points[i2].x + dm_x; out_vtx[0].y = points[i2].y + dm_y; @@ -701,12 +700,12 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 idx1 = idx2; } - // Add vertexes + // Add vertices for (int i = 0; i < points_count; i++) { - _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; - _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; - _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col_trans; + _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; + _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; _VtxWritePtr += 3; } } @@ -715,22 +714,23 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; if (!closed) { + const int points_last = points_count - 1; temp_points[0] = points[0] + temp_normals[0] * (half_inner_thickness + AA_SIZE); temp_points[1] = points[0] + temp_normals[0] * (half_inner_thickness); temp_points[2] = points[0] - temp_normals[0] * (half_inner_thickness); temp_points[3] = points[0] - temp_normals[0] * (half_inner_thickness + AA_SIZE); - temp_points[(points_count-1)*4+0] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); - temp_points[(points_count-1)*4+1] = points[points_count-1] + temp_normals[points_count-1] * (half_inner_thickness); - temp_points[(points_count-1)*4+2] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness); - temp_points[(points_count-1)*4+3] = points[points_count-1] - temp_normals[points_count-1] * (half_inner_thickness + AA_SIZE); + temp_points[points_last * 4 + 0] = points[points_last] + temp_normals[points_last] * (half_inner_thickness + AA_SIZE); + temp_points[points_last * 4 + 1] = points[points_last] + temp_normals[points_last] * (half_inner_thickness); + temp_points[points_last * 4 + 2] = points[points_last] - temp_normals[points_last] * (half_inner_thickness); + temp_points[points_last * 4 + 3] = points[points_last] - temp_normals[points_last] * (half_inner_thickness + AA_SIZE); } // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. unsigned int idx1 = _VtxCurrentIdx; for (int i1 = 0; i1 < count; i1++) { - const int i2 = (i1+1) == points_count ? 0 : i1+1; - unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+4; + const int i2 = (i1 + 1) == points_count ? 0 : (i1 + 1); // i2 is the second point of the line segment + const unsigned int idx2 = (i1 + 1) == points_count ? _VtxCurrentIdx : (idx1 + 4); // Vertex index for end of segment // Average normals float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; @@ -741,7 +741,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 float dm_in_x = dm_x * half_inner_thickness; float dm_in_y = dm_y * half_inner_thickness; - // Add temporary vertexes + // Add temporary vertices ImVec2* out_vtx = &temp_points[i2*4]; out_vtx[0].x = points[i2].x + dm_out_x; out_vtx[0].y = points[i2].y + dm_out_y; @@ -753,24 +753,24 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 out_vtx[3].y = points[i2].y - dm_out_y; // Add indexes - _IdxWritePtr[0] = (ImDrawIdx)(idx2+1); _IdxWritePtr[1] = (ImDrawIdx)(idx1+1); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); - _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+1); - _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); - _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10] = (ImDrawIdx)(idx2+0); _IdxWritePtr[11] = (ImDrawIdx)(idx2+1); - _IdxWritePtr[12] = (ImDrawIdx)(idx2+2); _IdxWritePtr[13] = (ImDrawIdx)(idx1+2); _IdxWritePtr[14] = (ImDrawIdx)(idx1+3); - _IdxWritePtr[15] = (ImDrawIdx)(idx1+3); _IdxWritePtr[16] = (ImDrawIdx)(idx2+3); _IdxWritePtr[17] = (ImDrawIdx)(idx2+2); + _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); + _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 1); + _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); + _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); + _IdxWritePtr[12] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[13] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[14] = (ImDrawIdx)(idx1 + 3); + _IdxWritePtr[15] = (ImDrawIdx)(idx1 + 3); _IdxWritePtr[16] = (ImDrawIdx)(idx2 + 3); _IdxWritePtr[17] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr += 18; idx1 = idx2; } - // Add vertexes + // Add vertices for (int i = 0; i < points_count; i++) { - _VtxWritePtr[0].pos = temp_points[i*4+0]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col_trans; - _VtxWritePtr[1].pos = temp_points[i*4+1]; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; - _VtxWritePtr[2].pos = temp_points[i*4+2]; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; - _VtxWritePtr[3].pos = temp_points[i*4+3]; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col_trans; + _VtxWritePtr[0].pos = temp_points[i * 4 + 0]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col_trans; + _VtxWritePtr[1].pos = temp_points[i * 4 + 1]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos = temp_points[i * 4 + 2]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos = temp_points[i * 4 + 3]; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col_trans; _VtxWritePtr += 4; } } @@ -795,14 +795,14 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 dx *= (thickness * 0.5f); dy *= (thickness * 0.5f); - _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; - _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col; - _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = uv; _VtxWritePtr[2].col = col; - _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = uv; _VtxWritePtr[3].col = col; + _VtxWritePtr[0].pos.x = p1.x + dy; _VtxWritePtr[0].pos.y = p1.y - dx; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; + _VtxWritePtr[1].pos.x = p2.x + dy; _VtxWritePtr[1].pos.y = p2.y - dx; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col; + _VtxWritePtr[2].pos.x = p2.x - dy; _VtxWritePtr[2].pos.y = p2.y + dx; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col; + _VtxWritePtr[3].pos.x = p1.x - dy; _VtxWritePtr[3].pos.y = p1.y + dx; _VtxWritePtr[3].uv = opaque_uv; _VtxWritePtr[3].col = col; _VtxWritePtr += 4; - _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+2); - _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx+2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx+3); + _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + 2); + _IdxWritePtr[3] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[4] = (ImDrawIdx)(_VtxCurrentIdx + 2); _IdxWritePtr[5] = (ImDrawIdx)(_VtxCurrentIdx + 3); _IdxWritePtr += 6; _VtxCurrentIdx += 4; } @@ -1543,7 +1543,6 @@ ImFontConfig::ImFontConfig() // The white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. const int FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF = 108; const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; -const unsigned int FONT_ATLAS_DEFAULT_TEX_DATA_ID = 0x80000000; static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = { "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX " @@ -1826,14 +1825,11 @@ ImFont* ImFontAtlas::AddFontFromMemoryCompressedBase85TTF(const char* compressed return font; } -int ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height) +int ImFontAtlas::AddCustomRectRegular(int width, int height) { - // Breaking change on 2019/11/21 (1.74): ImFontAtlas::AddCustomRectRegular() now requires an ID >= 0x110000 (instead of >= 0x10000) - IM_ASSERT(id >= 0x110000); IM_ASSERT(width > 0 && width <= 0xFFFF); IM_ASSERT(height > 0 && height <= 0xFFFF); ImFontAtlasCustomRect r; - r.ID = id; r.Width = (unsigned short)width; r.Height = (unsigned short)height; CustomRects.push_back(r); @@ -1842,13 +1838,16 @@ int ImFontAtlas::AddCustomRectRegular(unsigned int id, int width, int height) int ImFontAtlas::AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset) { +#ifdef IMGUI_USE_WCHAR32 + IM_ASSERT(id <= IM_UNICODE_CODEPOINT_MAX); +#endif IM_ASSERT(font != NULL); IM_ASSERT(width > 0 && width <= 0xFFFF); IM_ASSERT(height > 0 && height <= 0xFFFF); ImFontAtlasCustomRect r; - r.ID = id; r.Width = (unsigned short)width; r.Height = (unsigned short)height; + r.GlyphID = id; r.GlyphAdvanceX = advance_x; r.GlyphOffset = offset; r.Font = font; @@ -1873,7 +1872,6 @@ bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* ou IM_ASSERT(CustomRectIds[0] != -1); ImFontAtlasCustomRect& r = CustomRects[CustomRectIds[0]]; - IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y); ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1]; *out_size = size; @@ -2208,9 +2206,9 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas) if (atlas->CustomRectIds[0] >= 0) return; if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) - atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF*2+1, FONT_ATLAS_DEFAULT_TEX_DATA_H); + atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF*2+1, FONT_ATLAS_DEFAULT_TEX_DATA_H); else - atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_ID, 2, 2); + atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(2, 2); } void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) @@ -2259,7 +2257,6 @@ static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) IM_ASSERT(atlas->CustomRectIds[0] >= 0); IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); ImFontAtlasCustomRect& r = atlas->CustomRects[atlas->CustomRectIds[0]]; - IM_ASSERT(r.ID == FONT_ATLAS_DEFAULT_TEX_DATA_ID); IM_ASSERT(r.IsPacked()); const int w = atlas->TexWidth; @@ -2294,13 +2291,13 @@ void ImFontAtlasBuildFinish(ImFontAtlas* atlas) for (int i = 0; i < atlas->CustomRects.Size; i++) { const ImFontAtlasCustomRect& r = atlas->CustomRects[i]; - if (r.Font == NULL || r.ID >= 0x110000) + if (r.Font == NULL || r.GlyphID == 0) continue; IM_ASSERT(r.Font->ContainerAtlas == atlas); ImVec2 uv0, uv1; atlas->CalcCustomRectUV(&r, &uv0, &uv1); - r.Font->AddGlyph((ImWchar)r.ID, r.GlyphOffset.x, r.GlyphOffset.y, r.GlyphOffset.x + r.Width, r.GlyphOffset.y + r.Height, uv0.x, uv0.y, uv1.x, uv1.y, r.GlyphAdvanceX); + r.Font->AddGlyph((ImWchar)r.GlyphID, r.GlyphOffset.x, r.GlyphOffset.y, r.GlyphOffset.x + r.Width, r.GlyphOffset.y + r.Height, uv0.x, uv0.y, uv1.x, uv1.y, r.GlyphAdvanceX); } // Build all fonts lookup tables @@ -2310,7 +2307,7 @@ void ImFontAtlasBuildFinish(ImFontAtlas* atlas) // Ellipsis character is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character. - // FIXME: Also note that 0x2026 is currently seldomly included in our font ranges. Because of this we are more likely to use three individual dots. + // FIXME: Also note that 0x2026 is currently seldom included in our font ranges. Because of this we are more likely to use three individual dots. for (int i = 0; i < atlas->Fonts.size(); i++) { ImFont* font = atlas->Fonts[i]; @@ -2343,7 +2340,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesKorean() { 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x3131, 0x3163, // Korean alphabets - 0xAC00, 0xD79D, // Korean characters + 0xAC00, 0xD7A3, // Korean characters 0, }; return &ranges[0]; @@ -2876,7 +2873,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons const float line_height = size; const float scale = size / FontSize; - ImVec2 text_size = ImVec2(0,0); + ImVec2 text_size = ImVec2(0, 0); float line_width = 0.0f; const bool word_wrap_enabled = (wrap_width > 0.0f); @@ -3161,13 +3158,71 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col } //----------------------------------------------------------------------------- -// [SECTION] Internal Render Helpers -// (progressively moved from imgui.cpp to here when they are redesigned to stop accessing ImGui global state) +// [SECTION] ImGui Internal Render Helpers //----------------------------------------------------------------------------- +// Vaguely redesigned to stop accessing ImGui global state: +// - RenderArrow() +// - RenderBullet() +// - RenderCheckMark() // - RenderMouseCursor() // - RenderArrowPointingAt() // - RenderRectFilledRangeH() //----------------------------------------------------------------------------- +// Function in need of a redesign (legacy mess) +// - RenderColorRectWithAlphaCheckerboard() +//----------------------------------------------------------------------------- + +// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state +void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale) +{ + const float h = draw_list->_Data->FontSize * 1.00f; + float r = h * 0.40f * scale; + ImVec2 center = pos + ImVec2(h * 0.50f, h * 0.50f * scale); + + ImVec2 a, b, c; + switch (dir) + { + case ImGuiDir_Up: + case ImGuiDir_Down: + if (dir == ImGuiDir_Up) r = -r; + a = ImVec2(+0.000f, +0.750f) * r; + b = ImVec2(-0.866f, -0.750f) * r; + c = ImVec2(+0.866f, -0.750f) * r; + break; + case ImGuiDir_Left: + case ImGuiDir_Right: + if (dir == ImGuiDir_Left) r = -r; + a = ImVec2(+0.750f, +0.000f) * r; + b = ImVec2(-0.750f, +0.866f) * r; + c = ImVec2(-0.750f, -0.866f) * r; + break; + case ImGuiDir_None: + case ImGuiDir_COUNT: + IM_ASSERT(0); + break; + } + draw_list->AddTriangleFilled(center + a, center + b, center + c, col); +} + +void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col) +{ + draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8); +} + +void ImGui::RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz) +{ + float thickness = ImMax(sz / 5.0f, 1.0f); + sz -= thickness * 0.5f; + pos += ImVec2(thickness * 0.25f, thickness * 0.25f); + + float third = sz / 3.0f; + float bx = pos.x + third; + float by = pos.y + sz - third * 0.5f; + draw_list->PathLineTo(ImVec2(bx - third, by - third)); + draw_list->PathLineTo(ImVec2(bx, by)); + draw_list->PathLineTo(ImVec2(bx + third * 2.0f, by - third * 2.0f)); + draw_list->PathStroke(col, false, thickness); +} void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) { @@ -3272,6 +3327,43 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im draw_list->PathFillConvex(col); } +// Helper for ColorPicker4() +// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. +// Spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding altogether. +// FIXME: uses ImGui::GetColorU32 +void ImGui::RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags) +{ + if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF) + { + ImU32 col_bg1 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(204, 204, 204, 255), col)); + ImU32 col_bg2 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(128, 128, 128, 255), col)); + draw_list->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags); + + int yi = 0; + for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++) + { + float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y); + if (y2 <= y1) + continue; + for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f) + { + float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x); + if (x2 <= x1) + continue; + int rounding_corners_flags_cell = 0; + if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; } + if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; } + rounding_corners_flags_cell &= rounding_corners_flags; + draw_list->AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell); + } + } + } + else + { + draw_list->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags); + } +} + //----------------------------------------------------------------------------- // [SECTION] Decompression code //----------------------------------------------------------------------------- diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index c496232c..ef63c81f 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -68,7 +68,7 @@ enum GlfwClientApi static GLFWwindow* g_Window = NULL; // Main window static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown; static double g_Time = 0.0; -static bool g_MouseJustPressed[5] = { false, false, false, false, false }; +static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {}; static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; static bool g_InstalledCallbacks = false; diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h index a86790b7..f62f44f3 100644 --- a/imgui/imgui_impl_glfw.h +++ b/imgui/imgui_impl_glfw.h @@ -17,6 +17,7 @@ // Only override if your GL version doesn't handle this GLSL version. Keep NULL if unsure! #pragma once +#include "imgui.h" // IMGUI_IMPL_API struct GLFWwindow; diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index 9289913e..028a704b 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -13,7 +13,11 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2020-01-07: OpenGL: Added support for glbindings OpenGL loader. +// 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. +// 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix. +// 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset. +// 2020-03-24: OpenGL: Added support for glbinding 2.x OpenGL loader. +// 2020-01-07: OpenGL: Added support for glbinding 3.x OpenGL loader. // 2019-10-25: OpenGL: Using a combination of GL define and runtime GL version to decide whether to use glDrawElementsBaseVertex(). Fix building with pre-3.2 GL loaders. // 2019-09-22: OpenGL: Detect default GL loader using __has_include compiler facility. // 2019-09-16: OpenGL: Tweak initialization code to allow application calling ImGui_ImplOpenGL3_CreateFontsTexture() before the first NewFrame() call. @@ -75,28 +79,7 @@ #else #include // intptr_t #endif -#if defined(__APPLE__) -#include "TargetConditionals.h" -#endif -// Auto-enable GLES on matching platforms -#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) -#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) -#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" -#undef IMGUI_IMPL_OPENGL_LOADER_GL3W -#undef IMGUI_IMPL_OPENGL_LOADER_GLEW -#undef IMGUI_IMPL_OPENGL_LOADER_GLAD -#undef IMGUI_IMPL_OPENGL_LOADER_GLBINDING -#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM -#elif defined(__EMSCRIPTEN__) -#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" -#undef IMGUI_IMPL_OPENGL_LOADER_GL3W -#undef IMGUI_IMPL_OPENGL_LOADER_GLEW -#undef IMGUI_IMPL_OPENGL_LOADER_GLAD -#undef IMGUI_IMPL_OPENGL_LOADER_GLBINDING -#undef IMGUI_IMPL_OPENGL_LOADER_CUSTOM -#endif -#endif // GL includes #if defined(IMGUI_IMPL_OPENGL_ES2) @@ -113,14 +96,24 @@ // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. #if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) -#include // Needs to be initialized with gl3wInit() in user's code +#include // Needs to be initialized with gl3wInit() in user's code #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) -#include // Needs to be initialized with glewInit() in user's code +#include // Needs to be initialized with glewInit() in user's code. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) -#include // Needs to be initialized with gladLoadGL() in user's code -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) -#include // Initialize with glbinding::initialize() -#include +#include // Needs to be initialized with gladLoadGL() in user's code. +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) +#ifndef GLFW_INCLUDE_NONE +#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. +#endif +#include // Needs to be initialized with glbinding::Binding::initialize() in user's code. +#include +using namespace gl; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) +#ifndef GLFW_INCLUDE_NONE +#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. +#endif +#include // Needs to be initialized with glbinding::initialize() in user's code. +#include using namespace gl; #else #include IMGUI_IMPL_OPENGL_LOADER_CUSTOM @@ -135,32 +128,32 @@ using namespace gl; #endif // OpenGL Data -static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries. +static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings. static GLuint g_FontTexture = 0; static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; -static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location -static int g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location +static GLint g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location +static GLuint g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location static unsigned int g_VboHandle = 0, g_ElementsHandle = 0; // Functions bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { - // Query for GL version + // Query for GL version (e.g. 320 for GL 3.2) #if !defined(IMGUI_IMPL_OPENGL_ES2) GLint major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); - g_GlVersion = major * 1000 + minor; + g_GlVersion = (GLuint)(major * 100 + minor * 10); #else - g_GlVersion = 2000; // GLES 2 + g_GlVersion = 200; // GLES 2 #endif // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "imgui_impl_opengl3"; #if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET - if (g_GlVersion >= 3200) + if (g_GlVersion >= 320) io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif @@ -172,6 +165,9 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) #elif defined(IMGUI_IMPL_OPENGL_ES3) if (glsl_version == NULL) glsl_version = "#version 300 es"; +#elif defined(__APPLE__) + if (glsl_version == NULL) + glsl_version = "#version 150"; #else if (glsl_version == NULL) glsl_version = "#version 130"; @@ -193,10 +189,14 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) gl_loader = "GLEW"; #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) gl_loader = "GLAD"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) - gl_loader = "glbinding"; -#else // IMGUI_IMPL_OPENGL_LOADER_CUSTOM - gl_loader = "Custom"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) + gl_loader = "glbinding2"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) + gl_loader = "glbinding3"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) + gl_loader = "custom"; +#else + gl_loader = "none"; #endif // Make a dummy GL call (we don't actually need the result) @@ -232,6 +232,14 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif + // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) + bool clip_origin_lower_left = true; +#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) + GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); + if (current_clip_origin == GL_UPPER_LEFT) + clip_origin_lower_left = false; +#endif + // Setup viewport, orthographic projection matrix // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height); @@ -239,6 +247,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; + if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left const float ortho_projection[4][4] = { { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, @@ -283,14 +292,14 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Backup GL state GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); glActiveTexture(GL_TEXTURE0); - GLint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, &last_program); - GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); + GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); + GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); #ifdef GL_SAMPLER_BINDING - GLint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, &last_sampler); + GLuint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); #endif - GLint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); + GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_ES2 - GLint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array_object); + GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif #ifdef GL_POLYGON_MODE GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); @@ -307,12 +316,6 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); - bool clip_origin_lower_left = true; -#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) - GLenum last_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)&last_clip_origin); // Support for GL 4.5's glClipControl(GL_UPPER_LEFT) - if (last_clip_origin == GL_UPPER_LEFT) - clip_origin_lower_left = false; -#endif // Setup desired GL state // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) @@ -333,8 +336,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) const ImDrawList* cmd_list = draw_data->CmdLists[n]; // Upload vertex/index buffers - glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW); + glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW); for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { @@ -360,15 +363,12 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f) { // Apply scissor/clipping rectangle - if (clip_origin_lower_left) - glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); - else - glScissor((int)clip_rect.x, (int)clip_rect.y, (int)clip_rect.z, (int)clip_rect.w); // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) + glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); // Bind texture, Draw glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); #if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET - if (g_GlVersion >= 3200) + if (g_GlVersion >= 320) glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); else #endif @@ -643,9 +643,9 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); - g_AttribLocationVtxPos = glGetAttribLocation(g_ShaderHandle, "Position"); - g_AttribLocationVtxUV = glGetAttribLocation(g_ShaderHandle, "UV"); - g_AttribLocationVtxColor = glGetAttribLocation(g_ShaderHandle, "Color"); + g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(g_ShaderHandle, "Position"); + g_AttribLocationVtxUV = (GLuint)glGetAttribLocation(g_ShaderHandle, "UV"); + g_AttribLocationVtxColor = (GLuint)glGetAttribLocation(g_ShaderHandle, "Color"); // Create buffers glGenBuffers(1, &g_VboHandle); diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index 98cefe7d..07d35219 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -12,7 +12,7 @@ // https://github.com/ocornut/imgui // About Desktop OpenGL function loaders: -// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. +// Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. // Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). // You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. @@ -22,6 +22,7 @@ // Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp. #pragma once +#include "imgui.h" // IMGUI_IMPL_API // Backend API IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL); @@ -35,33 +36,49 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); -// Specific OpenGL versions +// Specific OpenGL ES versions //#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten //#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android -// Desktop OpenGL: attempt to detect default GL loader based on available header files. +// Attempt to auto-detect the default Desktop GL loader based on available header files. // If auto-detection fails or doesn't select the same GL loader file as used by your application, // you are likely to get a crash in ImGui_ImplOpenGL3_Init(). -// You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. -#if !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ +// You can explicitly select a loader by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. +#if !defined(IMGUI_IMPL_OPENGL_ES2) \ + && !defined(IMGUI_IMPL_OPENGL_ES3) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) - #if defined(__has_include) - #if __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLEW - #elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLAD - #elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GL3W - #elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING - #else - #error "Cannot detect OpenGL loader!" - #endif - #else - #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W - #endif + +// Try to detect GLES on matching platforms +#if defined(__APPLE__) +#include "TargetConditionals.h" #endif +#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) +#define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" +#elif defined(__EMSCRIPTEN__) +#define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" +// Otherwise try to detect supported Desktop OpenGL loaders.. +#elif defined(__has_include) +#if __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLEW +#elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLAD +#elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GL3W +#elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING3 +#elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING2 +#else + #error "Cannot detect OpenGL loader!" +#endif +#else + #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W embedded in our repository +#endif + +#endif diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index eed5d82e..96e7b19e 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -9,15 +9,26 @@ /* Index of this file: -// Header mess -// Forward declarations -// STB libraries includes -// Context pointer -// Generic helpers -// Misc data structures -// Main imgui context -// Tab bar, tab item -// Internal API + +// [SECTION] Header mess +// [SECTION] Forward declarations +// [SECTION] Context pointer +// [SECTION] STB libraries includes +// [SECTION] Macros +// [SECTION] Generic helpers +// [SECTION] ImDrawList support +// [SECTION] Widgets support: flags, enums, data structures +// [SECTION] Columns support +// [SECTION] Settings support +// [SECTION] Multi-select support +// [SECTION] Docking support +// [SECTION] Viewport support +// [SECTION] ImGuiContext (main imgui context) +// [SECTION] ImGuiWindowTempData, ImGuiWindow +// [SECTION] Tab bar, Tab item support +// [SECTION] Table support +// [SECTION] Internal API +// [SECTION] Test Engine Hooks (imgui_test_engine) */ @@ -25,7 +36,7 @@ Index of this file: #ifndef IMGUI_DISABLE //----------------------------------------------------------------------------- -// Header mess +// [SECTION] Header mess //----------------------------------------------------------------------------- #ifndef IMGUI_VERSION @@ -46,8 +57,8 @@ Index of this file: // Clang/GCC warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h -#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h +#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h +#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h #pragma clang diagnostic ignored "-Wold-style-cast" #if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" @@ -57,20 +68,20 @@ Index of this file: #endif #elif defined(__GNUC__) #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind -#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif // Legacy defines -#ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 +#ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 #error Use IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS #endif -#ifdef IMGUI_DISABLE_MATH_FUNCTIONS // Renamed in 1.74 +#ifdef IMGUI_DISABLE_MATH_FUNCTIONS // Renamed in 1.74 #error Use IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS #endif //----------------------------------------------------------------------------- -// Forward declarations +// [SECTION] Forward declarations //----------------------------------------------------------------------------- struct ImBitVector; // Store 1-bit per value @@ -86,7 +97,7 @@ struct ImGuiGroupData; // Stacked storage data for BeginGroup()/End struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiItemHoveredDataBackup; // Backup and restore IsItemHovered() internal data struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only -struct ImGuiNavMoveResult; // Result of a directional navigation move query result +struct ImGuiNavMoveResult; // Result of a gamepad/keyboard directional navigation move query result struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions struct ImGuiPopupData; // Storage for current popup stack @@ -115,8 +126,17 @@ typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // F typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() +//----------------------------------------------------------------------------- +// [SECTION] Context pointer +// See implementation of this variable in imgui.cpp for comments and details. +//----------------------------------------------------------------------------- + +#ifndef GImGui +extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer +#endif + //------------------------------------------------------------------------- -// STB libraries includes +// [SECTION] STB libraries includes //------------------------------------------------------------------------- namespace ImStb @@ -134,15 +154,7 @@ namespace ImStb } // namespace ImStb //----------------------------------------------------------------------------- -// Context pointer -//----------------------------------------------------------------------------- - -#ifndef GImGui -extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer -#endif - -//----------------------------------------------------------------------------- -// Macros +// [SECTION] Macros //----------------------------------------------------------------------------- // Debug Logging @@ -192,32 +204,53 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer #define IMGUI_CDECL #endif +// Debug Tools +// Use 'Metrics->Tools->Item Picker' to break into the call-stack of a specific item. +#ifndef IM_DEBUG_BREAK +#if defined(__clang__) +#define IM_DEBUG_BREAK() __builtin_debugtrap() +#elif defined (_MSC_VER) +#define IM_DEBUG_BREAK() __debugbreak() +#else +#define IM_DEBUG_BREAK() IM_ASSERT(0) // It is expected that you define IM_DEBUG_BREAK() into something that will break nicely in a debugger! +#endif +#endif // #ifndef IM_DEBUG_BREAK + //----------------------------------------------------------------------------- -// Generic helpers +// [SECTION] Generic helpers // Note that the ImXXX helpers functions are lower-level than ImGui functions. // ImGui functions or the ImGui context are never called/used from other ImXXX functions. //----------------------------------------------------------------------------- -// - Helpers: Misc +// - Helpers: Hashing +// - Helpers: Sorting // - Helpers: Bit manipulation // - Helpers: String, Formatting // - Helpers: UTF-8 <> wchar conversions // - Helpers: ImVec2/ImVec4 operators // - Helpers: Maths // - Helpers: Geometry -// - Helpers: Bit arrays +// - Helper: ImVec1 +// - Helper: ImVec2ih +// - Helper: ImRect +// - Helper: ImBitArray // - Helper: ImBitVector // - Helper: ImPool<> // - Helper: ImChunkStream<> //----------------------------------------------------------------------------- -// Helpers: Misc -#define ImQsort qsort +// Helpers: Hashing IMGUI_API ImU32 ImHashData(const void* data, size_t data_size, ImU32 seed = 0); IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS static inline ImU32 ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] #endif +// Helpers: Sorting +#define ImQsort qsort + +// Helpers: Color Blending +IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b); + // Helpers: Bit manipulation static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } @@ -283,7 +316,6 @@ static inline ImU64 ImFileGetSize(ImFileHandle) static inline ImU64 ImFileRead(void*, ImU64, ImU64, ImFileHandle) { return 0; } static inline ImU64 ImFileWrite(const void*, ImU64, ImU64, ImFileHandle) { return 0; } #endif - #ifndef IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS typedef FILE* ImFileHandle; IMGUI_API ImFileHandle ImFileOpen(const char* filename, const char* mode); @@ -351,7 +383,61 @@ IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& inline float ImTriangleArea(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ImFabs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y))) * 0.5f; } IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); -// Helpers: Bit arrays +// Helper: ImVec1 (1D vector) +// (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) +struct ImVec1 +{ + float x; + ImVec1() { x = 0.0f; } + ImVec1(float _x) { x = _x; } +}; + +// Helper: ImVec2ih (2D vector, half-size integer, for long-term packed storage) +struct ImVec2ih +{ + short x, y; + ImVec2ih() { x = y = 0; } + ImVec2ih(short _x, short _y) { x = _x; y = _y; } + explicit ImVec2ih(const ImVec2& rhs) { x = (short)rhs.x; y = (short)rhs.y; } +}; + +// Helper: ImRect (2D axis aligned bounding-box) +// NB: we can't rely on ImVec2 math operators being available here! +struct IMGUI_API ImRect +{ + ImVec2 Min; // Upper-left + ImVec2 Max; // Lower-right + + ImRect() : Min(0.0f, 0.0f), Max(0.0f, 0.0f) {} + ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} + ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} + ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} + + ImVec2 GetCenter() const { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); } + ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } + float GetWidth() const { return Max.x - Min.x; } + float GetHeight() const { return Max.y - Min.y; } + ImVec2 GetTL() const { return Min; } // Top-left + ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right + ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left + ImVec2 GetBR() const { return Max; } // Bottom-right + bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; } + bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; } + bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; } + void Add(const ImVec2& p) { if (Min.x > p.x) Min.x = p.x; if (Min.y > p.y) Min.y = p.y; if (Max.x < p.x) Max.x = p.x; if (Max.y < p.y) Max.y = p.y; } + void Add(const ImRect& r) { if (Min.x > r.Min.x) Min.x = r.Min.x; if (Min.y > r.Min.y) Min.y = r.Min.y; if (Max.x < r.Max.x) Max.x = r.Max.x; if (Max.y < r.Max.y) Max.y = r.Max.y; } + void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; } + void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; } + void Translate(const ImVec2& d) { Min.x += d.x; Min.y += d.y; Max.x += d.x; Max.y += d.y; } + void TranslateX(float dx) { Min.x += dx; Max.x += dx; } + void TranslateY(float dy) { Min.y += dy; Max.y += dy; } + void ClipWith(const ImRect& r) { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); } // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display. + void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. + void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); } + bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } +}; + +// Helper: ImBitArray inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } @@ -428,9 +514,88 @@ struct IMGUI_API ImChunkStream }; //----------------------------------------------------------------------------- -// Misc data structures +// [SECTION] ImDrawList support +//----------------------------------------------------------------------------- + +// ImDrawList: Helper function to calculate a circle's segment count given its radius and a "maximum error" value. +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 12 +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp((int)((IM_PI * 2.0f) / ImAcos(((_RAD) - (_MAXERROR)) / (_RAD))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) + +// ImDrawList: You may set this to higher values (e.g. 2 or 3) to increase tessellation of fast rounded corners path. +#ifndef IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER +#define IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER 1 +#endif + +// Data shared between all ImDrawList instances +// You may want to create your own instance of this if you want to use ImDrawList completely without ImGui. In that case, watch out for future changes to this structure. +struct IMGUI_API ImDrawListSharedData +{ + ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas + ImFont* Font; // Current/default font (optional, for simplified AddText overload) + float FontSize; // Current/default font size (optional, for simplified AddText overload) + float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() + float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc + ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() + ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) + + // [Internal] Lookup tables + ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas + ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead) + + ImDrawListSharedData(); + void SetCircleSegmentMaxError(float max_error); +}; + +struct ImDrawDataBuilder +{ + ImVector Layers[2]; // Global layers for: regular, tooltip + + void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } + void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } + IMGUI_API void FlattenIntoSingleLayer(); +}; + +//----------------------------------------------------------------------------- +// [SECTION] Widgets support: flags, enums, data structures //----------------------------------------------------------------------------- +// Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin(). +// This is going to be exposed in imgui.h when stabilized enough. +enum ImGuiItemFlags_ +{ + ImGuiItemFlags_None = 0, + ImGuiItemFlags_NoTabStop = 1 << 0, // false + ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. + ImGuiItemFlags_Disabled = 1 << 2, // false // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211 + ImGuiItemFlags_NoNav = 1 << 3, // false + ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false + ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window + ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) + ImGuiItemFlags_Default_ = 0 +}; + +// Storage for LastItem data +enum ImGuiItemStatusFlags_ +{ + ImGuiItemStatusFlags_None = 0, + ImGuiItemStatusFlags_HoveredRect = 1 << 0, + ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, + ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) + ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. + ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. + ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. + ImGuiItemStatusFlags_Deactivated = 1 << 6 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. + +#ifdef IMGUI_ENABLE_TEST_ENGINE + , // [imgui_tests only] + ImGuiItemStatusFlags_Openable = 1 << 10, // + ImGuiItemStatusFlags_Opened = 1 << 11, // + ImGuiItemStatusFlags_Checkable = 1 << 12, // + ImGuiItemStatusFlags_Checked = 1 << 13 // +#endif +}; + enum ImGuiButtonFlags_ { ImGuiButtonFlags_None = 0, @@ -473,17 +638,6 @@ enum ImGuiDragFlags_ ImGuiDragFlags_Vertical = 1 << 0 }; -enum ImGuiColumnsFlags_ -{ - // Default: 0 - ImGuiColumnsFlags_None = 0, - ImGuiColumnsFlags_NoBorder = 1 << 0, // Disable column dividers - ImGuiColumnsFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers - ImGuiColumnsFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns - ImGuiColumnsFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window - ImGuiColumnsFlags_GrowParentContentsSize= 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. -}; - // Extend ImGuiSelectableFlags_ enum ImGuiSelectableFlagsPrivate_ { @@ -491,7 +645,7 @@ enum ImGuiSelectableFlagsPrivate_ ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release) ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release) - ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 23, // FIXME: We may be able to remove this (added in 6251d379 for menus) + ImGuiSelectableFlags_SpanAvailWidth = 1 << 23, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) ImGuiSelectableFlags_DrawHoveredWhenHeld= 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25 }; @@ -510,42 +664,6 @@ enum ImGuiSeparatorFlags_ ImGuiSeparatorFlags_SpanAllColumns = 1 << 2 }; -// Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin(). -// This is going to be exposed in imgui.h when stabilized enough. -enum ImGuiItemFlags_ -{ - ImGuiItemFlags_None = 0, - ImGuiItemFlags_NoTabStop = 1 << 0, // false - ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. - ImGuiItemFlags_Disabled = 1 << 2, // false // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211 - ImGuiItemFlags_NoNav = 1 << 3, // false - ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false - ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window - ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) - ImGuiItemFlags_Default_ = 0 -}; - -// Storage for LastItem data -enum ImGuiItemStatusFlags_ -{ - ImGuiItemStatusFlags_None = 0, - ImGuiItemStatusFlags_HoveredRect = 1 << 0, - ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, - ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) - ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. - ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. - ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. - ImGuiItemStatusFlags_Deactivated = 1 << 6 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. - -#ifdef IMGUI_ENABLE_TEST_ENGINE - , // [imgui_tests only] - ImGuiItemStatusFlags_Openable = 1 << 10, // - ImGuiItemStatusFlags_Opened = 1 << 11, // - ImGuiItemStatusFlags_Checkable = 1 << 12, // - ImGuiItemStatusFlags_Checked = 1 << 13 // -#endif -}; - enum ImGuiTextFlags_ { ImGuiTextFlags_None = 0, @@ -659,57 +777,9 @@ enum ImGuiPopupPositionPolicy ImGuiPopupPositionPolicy_ComboBox }; -// 1D vector (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) -struct ImVec1 +struct ImGuiDataTypeTempStorage { - float x; - ImVec1() { x = 0.0f; } - ImVec1(float _x) { x = _x; } -}; - -// 2D vector (half-size integer) -struct ImVec2ih -{ - short x, y; - ImVec2ih() { x = y = 0; } - ImVec2ih(short _x, short _y) { x = _x; y = _y; } - explicit ImVec2ih(const ImVec2& rhs) { x = (short)rhs.x; y = (short)rhs.y; } -}; - -// 2D axis aligned bounding-box -// NB: we can't rely on ImVec2 math operators being available here -struct IMGUI_API ImRect -{ - ImVec2 Min; // Upper-left - ImVec2 Max; // Lower-right - - ImRect() : Min(0.0f, 0.0f), Max(0.0f, 0.0f) {} - ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} - ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} - ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} - - ImVec2 GetCenter() const { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); } - ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } - float GetWidth() const { return Max.x - Min.x; } - float GetHeight() const { return Max.y - Min.y; } - ImVec2 GetTL() const { return Min; } // Top-left - ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right - ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left - ImVec2 GetBR() const { return Max; } // Bottom-right - bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; } - bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; } - bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; } - void Add(const ImVec2& p) { if (Min.x > p.x) Min.x = p.x; if (Min.y > p.y) Min.y = p.y; if (Max.x < p.x) Max.x = p.x; if (Max.y < p.y) Max.y = p.y; } - void Add(const ImRect& r) { if (Min.x > r.Min.x) Min.x = r.Min.x; if (Min.y > r.Min.y) Min.y = r.Min.y; if (Max.x < r.Max.x) Max.x = r.Max.x; if (Max.y < r.Max.y) Max.y = r.Max.y; } - void Expand(const float amount) { Min.x -= amount; Min.y -= amount; Max.x += amount; Max.y += amount; } - void Expand(const ImVec2& amount) { Min.x -= amount.x; Min.y -= amount.y; Max.x += amount.x; Max.y += amount.y; } - void Translate(const ImVec2& d) { Min.x += d.x; Min.y += d.y; Max.x += d.x; Max.y += d.y; } - void TranslateX(float dx) { Min.x += dx; Max.x += dx; } - void TranslateY(float dy) { Min.y += dy; Max.y += dy; } - void ClipWith(const ImRect& r) { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); } // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display. - void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. - void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); } - bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } + ImU8 Data[8]; // Can fit any data up to ImGuiDataType_COUNT }; // Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). @@ -720,6 +790,14 @@ struct ImGuiDataTypeInfo const char* ScanFmt; // Default scanf format for the type }; +// Extend ImGuiDataType_ +enum ImGuiDataTypePrivate_ +{ + ImGuiDataType_String = ImGuiDataType_COUNT + 1, + ImGuiDataType_Pointer, + ImGuiDataType_ID +}; + // Stacked color modifier, backup of modified data so we can restore it struct ImGuiColorMod { @@ -799,32 +877,6 @@ struct IMGUI_API ImGuiInputTextState void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } }; -// Windows data saved in imgui.ini file -// Because we never destroy or rename ImGuiWindowSettings, we can store the names in a separate buffer easily. -// (this is designed to be stored in a ImChunkStream buffer, with the variable-length Name following our structure) -struct ImGuiWindowSettings -{ - ImGuiID ID; - ImVec2ih Pos; - ImVec2ih Size; - bool Collapsed; - - ImGuiWindowSettings() { ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; } - char* GetName() { return (char*)(this + 1); } -}; - -struct ImGuiSettingsHandler -{ - const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']' - ImGuiID TypeHash; // == ImHashStr(TypeName) - void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); // Read: Called when entering into a new ini entry e.g. "[Window][Name]" - void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry - void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); // Write: Output every entries into 'out_buf' - void* UserData; - - ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } -}; - // Storage for current popup stack struct ImGuiPopupData { @@ -839,89 +891,6 @@ struct ImGuiPopupData ImGuiPopupData() { PopupId = 0; Window = SourceWindow = NULL; OpenFrameCount = -1; OpenParentId = 0; } }; -struct ImGuiColumnData -{ - float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) - float OffsetNormBeforeResize; - ImGuiColumnsFlags Flags; // Not exposed - ImRect ClipRect; - - ImGuiColumnData() { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; } -}; - -struct ImGuiColumns -{ - ImGuiID ID; - ImGuiColumnsFlags Flags; - bool IsFirstFrame; - bool IsBeingResized; - int Current; - int Count; - float OffMinX, OffMaxX; // Offsets from HostWorkRect.Min.x - float LineMinY, LineMaxY; - float HostCursorPosY; // Backup of CursorPos at the time of BeginColumns() - float HostCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() - ImRect HostClipRect; // Backup of ClipRect at the time of BeginColumns() - ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() - ImVector Columns; - ImDrawListSplitter Splitter; - - ImGuiColumns() { Clear(); } - void Clear() - { - ID = 0; - Flags = ImGuiColumnsFlags_None; - IsFirstFrame = false; - IsBeingResized = false; - Current = 0; - Count = 1; - OffMinX = OffMaxX = 0.0f; - LineMinY = LineMaxY = 0.0f; - HostCursorPosY = 0.0f; - HostCursorMaxPosX = 0.0f; - Columns.clear(); - } -}; - -// ImDrawList: Helper function to calculate a circle's segment count given its radius and a "maximum error" value. -#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 12 -#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 -#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp((int)((IM_PI * 2.0f) / ImAcos(((_RAD) - (_MAXERROR)) / (_RAD))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) - -// ImDrawList: You may set this to higher values (e.g. 2 or 3) to increase tessellation of fast rounded corners path. -#ifndef IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER -#define IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER 1 -#endif - -// Data shared between all ImDrawList instances -// You may want to create your own instance of this if you want to use ImDrawList completely without ImGui. In that case, watch out for future changes to this structure. -struct IMGUI_API ImDrawListSharedData -{ - ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas - ImFont* Font; // Current/default font (optional, for simplified AddText overload) - float FontSize; // Current/default font size (optional, for simplified AddText overload) - float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() - float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc - ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() - ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) - - // [Internal] Lookup tables - ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas - ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead) - - ImDrawListSharedData(); - void SetCircleSegmentMaxError(float max_error); -}; - -struct ImDrawDataBuilder -{ - ImVector Layers[2]; // Global layers for: regular, tooltip - - void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } - void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } - IMGUI_API void FlattenIntoSingleLayer(); -}; - struct ImGuiNavMoveResult { ImGuiWindow* Window; // Best candidate window @@ -945,7 +914,8 @@ enum ImGuiNextWindowDataFlags_ ImGuiNextWindowDataFlags_HasCollapsed = 1 << 3, ImGuiNextWindowDataFlags_HasSizeConstraint = 1 << 4, ImGuiNextWindowDataFlags_HasFocus = 1 << 5, - ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6 + ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6, + ImGuiNextWindowDataFlags_HasScroll = 1 << 7 }; // Storage for SetNexWindow** functions @@ -959,6 +929,7 @@ struct ImGuiNextWindowData ImVec2 PosPivotVal; ImVec2 SizeVal; ImVec2 ContentSizeVal; + ImVec2 ScrollVal; bool CollapsedVal; ImRect SizeConstraintRect; ImGuiSizeCallback SizeCallback; @@ -989,27 +960,140 @@ struct ImGuiNextItemData inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } // Also cleared manually by ItemAdd()! }; +struct ImGuiShrinkWidthItem +{ + int Index; + float Width; +}; + +struct ImGuiPtrOrIndex +{ + void* Ptr; // Either field can be set, not both. e.g. Dock node tab bars are loose while BeginTabBar() ones are in a pool. + int Index; // Usually index in a main pool. + + ImGuiPtrOrIndex(void* ptr) { Ptr = ptr; Index = -1; } + ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } +}; + //----------------------------------------------------------------------------- -// Tabs +// [SECTION] Columns support //----------------------------------------------------------------------------- -struct ImGuiShrinkWidthItem +enum ImGuiColumnsFlags_ +{ + // Default: 0 + ImGuiColumnsFlags_None = 0, + ImGuiColumnsFlags_NoBorder = 1 << 0, // Disable column dividers + ImGuiColumnsFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers + ImGuiColumnsFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns + ImGuiColumnsFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window + ImGuiColumnsFlags_GrowParentContentsSize= 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. +}; + +struct ImGuiColumnData { - int Index; - float Width; + float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) + float OffsetNormBeforeResize; + ImGuiColumnsFlags Flags; // Not exposed + ImRect ClipRect; + + ImGuiColumnData() { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; } }; -struct ImGuiPtrOrIndex +struct ImGuiColumns +{ + ImGuiID ID; + ImGuiColumnsFlags Flags; + bool IsFirstFrame; + bool IsBeingResized; + int Current; + int Count; + float OffMinX, OffMaxX; // Offsets from HostWorkRect.Min.x + float LineMinY, LineMaxY; + float HostCursorPosY; // Backup of CursorPos at the time of BeginColumns() + float HostCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() + ImRect HostClipRect; // Backup of ClipRect at the time of BeginColumns() + ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() + ImVector Columns; + ImDrawListSplitter Splitter; + + ImGuiColumns() { Clear(); } + void Clear() + { + ID = 0; + Flags = ImGuiColumnsFlags_None; + IsFirstFrame = false; + IsBeingResized = false; + Current = 0; + Count = 1; + OffMinX = OffMaxX = 0.0f; + LineMinY = LineMaxY = 0.0f; + HostCursorPosY = 0.0f; + HostCursorMaxPosX = 0.0f; + Columns.clear(); + } +}; + +//----------------------------------------------------------------------------- +// [SECTION] Multi-select support +//----------------------------------------------------------------------------- + +#ifdef IMGUI_HAS_MULTI_SELECT +// +#endif // #ifdef IMGUI_HAS_MULTI_SELECT + +//----------------------------------------------------------------------------- +// [SECTION] Docking support +//----------------------------------------------------------------------------- + +#ifdef IMGUI_HAS_DOCK +// +#endif // #ifdef IMGUI_HAS_DOCK + +//----------------------------------------------------------------------------- +// [SECTION] Viewport support +//----------------------------------------------------------------------------- + +#ifdef IMGUI_HAS_VIEWPORT +// +#endif // #ifdef IMGUI_HAS_VIEWPORT + +//----------------------------------------------------------------------------- +// [SECTION] Settings support +//----------------------------------------------------------------------------- + +// Windows data saved in imgui.ini file +// Because we never destroy or rename ImGuiWindowSettings, we can store the names in a separate buffer easily. +// (this is designed to be stored in a ImChunkStream buffer, with the variable-length Name following our structure) +struct ImGuiWindowSettings { - void* Ptr; // Either field can be set, not both. e.g. Dock node tab bars are loose while BeginTabBar() ones are in a pool. - int Index; // Usually index in a main pool. + ImGuiID ID; + ImVec2ih Pos; + ImVec2ih Size; + bool Collapsed; + bool WantApply; // Set when loaded from .ini data (to enable merging/loading .ini data into an already running context) - ImGuiPtrOrIndex(void* ptr) { Ptr = ptr; Index = -1; } - ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } + ImGuiWindowSettings() { ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = WantApply = false; } + char* GetName() { return (char*)(this + 1); } +}; + +struct ImGuiSettingsHandler +{ + const char* TypeName; // Short description stored in .ini file. Disallowed characters: '[' ']' + ImGuiID TypeHash; // == ImHashStr(TypeName) + void (*ClearAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler); // Clear all settings data + void (*ReadInitFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler); // Read: Called before reading (in registration order) + void* (*ReadOpenFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, const char* name); // Read: Called when entering into a new ini entry e.g. "[Window][Name]" + void (*ReadLineFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, void* entry, const char* line); // Read: Called for every line of text within an ini entry + void (*ApplyAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler); // Read: Called after reading (in registration order) + void (*WriteAllFn)(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf); // Write: Output every entries into 'out_buf' + void* UserData; + + ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } }; //----------------------------------------------------------------------------- -// Main Dear ImGui context +// [SECTION] ImGuiContext (main imgui context) //----------------------------------------------------------------------------- struct ImGuiContext @@ -1029,6 +1113,9 @@ struct ImGuiContext bool WithinFrameScope; // Set by NewFrame(), cleared by EndFrame() bool WithinFrameScopeWithImplicitWindow; // Set by NewFrame(), cleared by EndFrame() when the implicit debug window has been pushed bool WithinEndChild; // Set within EndChild() + bool TestEngineHookItems; // Will call test engine hooks: ImGuiTestEngineHook_ItemAdd(), ImGuiTestEngineHook_ItemInfo(), ImGuiTestEngineHook_Log() + ImGuiID TestEngineHookIdInfo; // Will call test engine hooks: ImGuiTestEngineHook_IdInfo() from GetID() + void* TestEngine; // Test engine user data // Windows state ImVector Windows; // Windows, sorted in display order, back to front @@ -1059,7 +1146,7 @@ struct ImGuiContext bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdHasBeenEditedThisFrame; - ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those directional navigation requests (e.g. can activate a button and move away from it) + ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs. ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array. ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) @@ -1087,7 +1174,7 @@ struct ImGuiContext // Gamepad/keyboard Navigation ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' ImGuiID NavId; // Focused item for navigation - ImGuiID NavFocusScopeId; + ImGuiID NavFocusScopeId; // Identify a selection scope (selection code often wants to "clear other items" when landing on an item of the selection set) ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 @@ -1095,9 +1182,10 @@ struct ImGuiContext ImGuiID NavJustTabbedId; // Just tabbed to this id. ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). + ImGuiKeyModFlags NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. - ImRect NavScoringRectScreen; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. + ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. int NavScoringCount; // Metrics for debugging ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing @@ -1108,22 +1196,25 @@ struct ImGuiContext bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest bool NavInitRequest; // Init request for appearing window to select first item bool NavInitRequestFromMove; - ImGuiID NavInitResultId; - ImRect NavInitResultRectRel; + ImGuiID NavInitResultId; // Init request result (first item of the window, or one for which SetItemDefaultFocus() was called) + ImRect NavInitResultRectRel; // Init request result rectangle (relative to parent window) bool NavMoveFromClampedRefRect; // Set by manual scrolling, if we scroll to a point where NavId isn't visible we reset navigation from visible items bool NavMoveRequest; // Move request for this frame ImGuiNavMoveFlags NavMoveRequestFlags; ImGuiNavForward NavMoveRequestForward; // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu) + ImGuiKeyModFlags NavMoveRequestKeyMods; ImGuiDir NavMoveDir, NavMoveDirLast; // Direction of the move request (left/right/up/down), direction of the previous move request ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? ImGuiNavMoveResult NavMoveResultLocal; // Best move request candidate within NavWindow ImGuiNavMoveResult NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) ImGuiNavMoveResult NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) + ImGuiWindow* NavWrapRequestWindow; // Window which requested trying nav wrap-around. + ImGuiNavMoveFlags NavWrapRequestFlags; // Wrap-around operation flags. - // Navigation: Windowing (CTRL+TAB, holding Menu button + directional pads to move/resize) - ImGuiWindow* NavWindowingTarget; // When selecting a window (holding Menu+FocusPrev/Next, or equivalent of CTRL-TAB) this window is temporarily displayed top-most. - ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f - ImGuiWindow* NavWindowingList; + // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) + ImGuiWindow* NavWindowingTarget; // Target window when doing CTRL+Tab (or Pad Menu + FocusPrev/Next), this window is temporarily displayed top-most! + ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f, so the fade-out can stay on it. + ImGuiWindow* NavWindowingListWindow; // Internal window actually listing the CTRL+Tab contents float NavWindowingTimer; float NavWindowingHighlightAlpha; bool NavWindowingToggleLayer; @@ -1160,6 +1251,7 @@ struct ImGuiContext ImGuiID DragDropAcceptIdCurr; // Target item id (set at the time of accepting the payload) ImGuiID DragDropAcceptIdPrev; // Target item id from previous frame (we need to store this to allow for overlapping drag and drop targets) int DragDropAcceptFrameCount; // Last time a target expressed a desire to accept the source + ImGuiID DragDropHoldJustPressedId; // Set when holding a payload just made ButtonBehavior() return a press. ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly, ImGuiPayload only holds pointer+size unsigned char DragDropPayloadBufLocal[16]; // Local buffer for small payloads @@ -1184,7 +1276,7 @@ struct ImGuiContext float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; - ImVector PrivateClipboard; // If no custom clipboard handler is defined + ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once // Platform support @@ -1199,8 +1291,8 @@ struct ImGuiContext ImChunkStream SettingsWindows; // ImGuiWindow .ini settings entries // Capture/Logging - bool LogEnabled; - ImGuiLogType LogType; + bool LogEnabled; // Currently capturing + ImGuiLogType LogType; // Capture target ImFileHandle LogFile; // If != NULL log to stdout/ file ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. float LogLinePosY; @@ -1210,7 +1302,7 @@ struct ImGuiContext int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. // Debug Tools - bool DebugItemPickerActive; + bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this id // Misc @@ -1225,14 +1317,17 @@ struct ImGuiContext ImGuiContext(ImFontAtlas* shared_font_atlas) : BackgroundDrawList(&DrawListSharedData), ForegroundDrawList(&DrawListSharedData) { Initialized = false; + FontAtlasOwnedByContext = shared_font_atlas ? false : true; Font = NULL; FontSize = FontBaseSize = 0.0f; - FontAtlasOwnedByContext = shared_font_atlas ? false : true; IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); Time = 0.0f; FrameCount = 0; FrameCountEnded = FrameCountRendered = -1; WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; + TestEngineHookItems = false; + TestEngineHookIdInfo = 0; + TestEngine = NULL; WindowsActiveCount = 0; CurrentWindow = NULL; @@ -1271,8 +1366,9 @@ struct ImGuiContext NavWindow = NULL; NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; NavJustTabbedId = NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; + NavJustMovedToKeyMods = ImGuiKeyModFlags_None; NavInputSource = ImGuiInputSource_None; - NavScoringRectScreen = ImRect(); + NavScoringRect = ImRect(); NavScoringCount = 0; NavLayer = ImGuiNavLayer_Main; NavIdTabCounter = INT_MAX; @@ -1288,9 +1384,12 @@ struct ImGuiContext NavMoveRequest = false; NavMoveRequestFlags = ImGuiNavMoveFlags_None; NavMoveRequestForward = ImGuiNavForward_None; + NavMoveRequestKeyMods = ImGuiKeyModFlags_None; NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None; + NavWrapRequestWindow = NULL; + NavWrapRequestFlags = ImGuiNavMoveFlags_None; - NavWindowingTarget = NavWindowingTargetAnim = NavWindowingList = NULL; + NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; @@ -1313,6 +1412,7 @@ struct ImGuiContext DragDropAcceptIdCurrRectSurface = 0.0f; DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; DragDropAcceptFrameCount = -1; + DragDropHoldJustPressedId = 0; memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); CurrentTabBar = NULL; @@ -1353,7 +1453,7 @@ struct ImGuiContext }; //----------------------------------------------------------------------------- -// ImGuiWindow +// [SECTION] ImGuiWindowTempData, ImGuiWindow //----------------------------------------------------------------------------- // Transient per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the DC variable name in ImGuiWindow. @@ -1470,7 +1570,7 @@ struct IMGUI_API ImGuiWindow ImVec2 ScrollMax; ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change) ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered - ImVec2 ScrollbarSizes; // Size taken by scrollbars on each axis + ImVec2 ScrollbarSizes; // Size taken by each scrollbars on their smaller axis. Pay attention! ScrollbarSizes.x == width of the vertical scrollbar, ScrollbarSizes.y = height of the horizontal scrollbar. bool ScrollbarX, ScrollbarY; // Are scrollbars visible? bool Active; // Set to true on Begin(), unless Collapsed bool WasActive; @@ -1497,7 +1597,7 @@ struct IMGUI_API ImGuiWindow ImGuiCond SetWindowSizeAllowFlags; // store acceptable condition flags for SetNextWindowSize() use. ImGuiCond SetWindowCollapsedAllowFlags; // store acceptable condition flags for SetNextWindowCollapsed() use. ImVec2 SetWindowPosVal; // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size) - ImVec2 SetWindowPosPivot; // store window pivot for positioning. ImVec2(0,0) when positioning from top-left corner; ImVec2(0.5f,0.5f) for centering; ImVec2(1,1) for bottom right. + ImVec2 SetWindowPosPivot; // store window pivot for positioning. ImVec2(0, 0) when positioning from top-left corner; ImVec2(0.5f, 0.5f) for centering; ImVec2(1, 1) for bottom right. ImVector IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack. (In theory this should be in the TempData structure) ImGuiWindowTempData DC; // Temporary per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the "DC" variable name. @@ -1530,8 +1630,8 @@ struct IMGUI_API ImGuiWindow ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space - bool MemoryCompacted; - int MemoryDrawListIdxCapacity; + bool MemoryCompacted; // Set when window extraneous data have been garbage collected + int MemoryDrawListIdxCapacity; // Backup of last idx/vtx count, so when waking up the window we can preallocate and avoid iterative alloc/copy int MemoryDrawListVtxCapacity; public: @@ -1547,12 +1647,12 @@ struct IMGUI_API ImGuiWindow ImGuiID GetIDFromRectangle(const ImRect& r_abs); // We don't use g.FontSize because the window may be != g.CurrentWidow. - ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } - float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } - float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } - ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } - float MenuBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } - ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } + ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } + float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } + float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } + ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } + float MenuBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } + ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } }; // Backup and restore just enough data to be able to use IsItemHovered() on item A after another B in the same window has overwritten the data. @@ -1569,7 +1669,7 @@ struct ImGuiItemHoveredDataBackup }; //----------------------------------------------------------------------------- -// Tab bar, tab item +// [SECTION] Tab bar, Tab item support //----------------------------------------------------------------------------- // Extend ImGuiTabBarFlags_ @@ -1639,8 +1739,16 @@ struct ImGuiTabBar }; //----------------------------------------------------------------------------- -// Internal API -// No guarantee of forward compatibility here. +// [SECTION] Table support +//----------------------------------------------------------------------------- + +#ifdef IMGUI_HAS_TABLE +// +#endif // #ifdef IMGUI_HAS_TABLE + +//----------------------------------------------------------------------------- +// [SECTION] Internal API +// No guarantee of forward compatibility here! //----------------------------------------------------------------------------- namespace ImGui @@ -1688,12 +1796,14 @@ namespace ImGui // Settings IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); + IMGUI_API void ClearIniSettings(); IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name); IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); // Scrolling + IMGUI_API void SetNextWindowScroll(const ImVec2& scroll); // Use -1.0f on one axis to leave as-is IMGUI_API void SetScrollX(ImGuiWindow* window, float new_scroll_x); IMGUI_API void SetScrollY(ImGuiWindow* window, float new_scroll_y); IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio = 0.5f); @@ -1701,7 +1811,7 @@ namespace ImGui IMGUI_API ImVec2 ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect); // Basic Accessors - inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } + inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } // Get ID of last item (~~ often same ImGui::GetID(label) beforehand) inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemStatusFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } @@ -1712,7 +1822,7 @@ namespace ImGui IMGUI_API void SetHoveredID(ImGuiID id); IMGUI_API void KeepAliveID(ImGuiID id); IMGUI_API void MarkItemEdited(ImGuiID id); // Mark data associated to given item as "edited", used by IsItemDeactivatedAfterEdit() function. - IMGUI_API void PushOverrideID(ImGuiID id); // Push given value at the top of the ID stack (whereas PushID combines old and new hashes) + IMGUI_API void PushOverrideID(ImGuiID id); // Push given value as-is at the top of the ID stack (whereas PushID combines old and new hashes) // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); @@ -1727,27 +1837,27 @@ namespace ImGui IMGUI_API void PushMultiItemsWidths(int components, float width_full); IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PopItemFlag(); - IMGUI_API bool IsItemToggledSelection(); // Was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly) + IMGUI_API bool IsItemToggledSelection(); // Was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly) IMGUI_API ImVec2 GetContentRegionMaxAbs(); IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess); // Logging/Capture - IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. - IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer + IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. + IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer // Popups, Modals, Tooltips IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); IMGUI_API void OpenPopupEx(ImGuiID id); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); - IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id within current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack! + IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id at current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack! IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy = ImGuiPopupPositionPolicy_Default); - // Navigation + // Gamepad/Keyboard Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); IMGUI_API bool NavMoveRequestButNoResultYet(); IMGUI_API void NavMoveRequestCancel(); @@ -1760,8 +1870,10 @@ namespace ImGui IMGUI_API void SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id); IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); - // Focus scope (WIP) - IMGUI_API void PushFocusScope(ImGuiID id); // Note: this is storing in same stack as IDStack, so Push/Pop mismatch will be reported there. + // Focus Scope (WIP) + // This is generally used to identify a selection set (multiple of which may be in the same window), as selection + // patterns generally need to react (e.g. clear selection) when landing on an item of the set. + IMGUI_API void PushFocusScope(ImGuiID id); IMGUI_API void PopFocusScope(); inline ImGuiID GetFocusScopeID() { ImGuiContext& g = *GImGui; return g.NavFocusScopeId; } @@ -1774,13 +1886,14 @@ namespace ImGui inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { ImGuiContext& g = *GImGui; const int key_index = g.IO.KeyMap[key]; return (key_index >= 0) ? IsKeyPressed(key_index, repeat) : false; } inline bool IsNavInputDown(ImGuiNavInput n) { ImGuiContext& g = *GImGui; return g.IO.NavInputs[n] > 0.0f; } inline bool IsNavInputTest(ImGuiNavInput n, ImGuiInputReadMode rm) { return (GetNavInputAmount(n, rm) > 0.0f); } + IMGUI_API ImGuiKeyModFlags GetMergedKeyModFlags(); // Drag and Drop IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); IMGUI_API void ClearDragDrop(); IMGUI_API bool IsDragDropPayloadBeingAccepted(); - // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables api) + // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). IMGUI_API void EndColumns(); // close columns IMGUI_API void PushColumnClipRect(int column_index); @@ -1800,20 +1913,19 @@ namespace ImGui IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); - IMGUI_API bool TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id); + IMGUI_API bool TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible); // Render helpers // AVOID USING OUTSIDE OF IMGUI.CPP! NOT FOR PUBLIC CONSUMPTION. THOSE FUNCTIONS ARE A MESS. THEIR SIGNATURE AND BEHAVIOR WILL CHANGE, THEY NEED TO BE REFACTORED INTO SOMETHING DECENT. // NB: All position are in absolute pixels coordinates (we are never using window coordinates internally) IMGUI_API void RenderText(ImVec2 pos, const char* text, const char* text_end = NULL, bool hide_text_after_hash = true); IMGUI_API void RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end, float wrap_width); - IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0,0), const ImRect* clip_rect = NULL); + IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL); IMGUI_API void RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL); IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known); IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); - IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0); - IMGUI_API void RenderCheckMark(ImVec2 pos, ImU32 col, float sz); + IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0); IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); @@ -1821,6 +1933,7 @@ namespace ImGui // Render helpers (those functions don't access any ImGui state!) IMGUI_API void RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale = 1.0f); IMGUI_API void RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col); + IMGUI_API void RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz); IMGUI_API void RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col); IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding); @@ -1833,12 +1946,13 @@ namespace ImGui // Widgets IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); - IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0,0), ImGuiButtonFlags flags = 0); + IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawCornerFlags rounding_corners); + IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowResizeID(ImGuiWindow* window, int n); // 0..3: corners, 4..7: borders IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); @@ -1865,11 +1979,12 @@ namespace ImGui IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format); IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg_1, const void* arg_2); IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format); + IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max); // InputText IMGUI_API bool InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); IMGUI_API bool TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags); - IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format); + IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min = NULL, const void* p_clamp_max = NULL); inline bool TempInputIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputId == id); } inline ImGuiInputTextState* GetInputTextState(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.InputTextState.ID == id) ? &g.InputTextState : NULL; } // Get input text state if active @@ -1879,7 +1994,7 @@ namespace ImGui IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags); // Plot - IMGUI_API void PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size); + IMGUI_API int PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size); // Shade functions (write over already created vertices) IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1); @@ -1904,33 +2019,30 @@ IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); -// Debug Tools -// Use 'Metrics->Tools->Item Picker' to break into the call-stack of a specific item. -#ifndef IM_DEBUG_BREAK -#if defined(__clang__) -#define IM_DEBUG_BREAK() __builtin_debugtrap() -#elif defined (_MSC_VER) -#define IM_DEBUG_BREAK() __debugbreak() -#else -#define IM_DEBUG_BREAK() IM_ASSERT(0) // It is expected that you define IM_DEBUG_BREAK() into something that will break nicely in a debugger! -#endif -#endif // #ifndef IM_DEBUG_BREAK +//----------------------------------------------------------------------------- +// [SECTION] Test Engine Hooks (imgui_test_engine) +//----------------------------------------------------------------------------- -// Test Engine Hooks (imgui_tests) //#define IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); +extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); +extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id, const void* data_id_end); extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); -#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box -#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) -#define IMGUI_TEST_ENGINE_LOG(_FMT, ...) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log +#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) +#define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log +#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) if (g.TestEngineHookIdInfo == id) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA)); +#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) if (g.TestEngineHookIdInfo == id) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA), (const void*)(_DATA2)); #else -#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB, _ID) do { } while (0) -#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID, _LABEL, _FLAGS) do { } while (0) -#define IMGUI_TEST_ENGINE_LOG(_FMT, ...) do { } while (0) +#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) do { } while (0) +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) do { } while (0) +#define IMGUI_TEST_ENGINE_LOG(_FMT,...) do { } while (0) +#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) do { } while (0) +#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) do { } while (0) #endif #if defined(__clang__) diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index d22c1fa4..07be3b36 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.76 WIP +// dear imgui, v1.77 WIP // (widgets code) /* @@ -391,8 +391,10 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // - ArrowButton() // - CloseButton() [Internal] // - CollapseButton() [Internal] -// - ScrollbarEx() [Internal] +// - GetWindowScrollbarID() [Internal] +// - GetWindowScrollbarRect() [Internal] // - Scrollbar() [Internal] +// - ScrollbarEx() [Internal] // - Image() // - ImageButton() // - Checkbox() @@ -484,7 +486,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool #ifdef IMGUI_ENABLE_TEST_ENGINE if (id != 0 && window->DC.LastItemId != id) - ImGuiTestEngineHook_ItemAdd(&g, bb, id); + IMGUI_TEST_ENGINE_ITEM_ADD(bb, id); #endif bool pressed = false; @@ -498,11 +500,13 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) { + const float DRAG_DROP_HOLD_TIMER = 0.70f; hovered = true; SetHoveredID(id); - if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, 0.70f, 0.00f)) + if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, DRAG_DROP_HOLD_TIMER, 0.00f)) { pressed = true; + g.DragDropHoldJustPressedId = id; FocusWindow(window); } } @@ -812,6 +816,49 @@ ImGuiID ImGui::GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis) return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); } +// Return scrollbar rectangle, must only be called for corresponding axis if window->ScrollbarX/Y is set. +ImRect ImGui::GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis) +{ + const ImRect outer_rect = window->Rect(); + const ImRect inner_rect = window->InnerRect; + const float border_size = window->WindowBorderSize; + const float scrollbar_size = window->ScrollbarSizes[axis ^ 1]; // (ScrollbarSizes.x = width of Y scrollbar; ScrollbarSizes.y = height of X scrollbar) + IM_ASSERT(scrollbar_size > 0.0f); + if (axis == ImGuiAxis_X) + return ImRect(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size), inner_rect.Max.x, outer_rect.Max.y); + else + return ImRect(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y, outer_rect.Max.x, inner_rect.Max.y); +} + +void ImGui::Scrollbar(ImGuiAxis axis) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + const ImGuiID id = GetWindowScrollbarID(window, axis); + KeepAliveID(id); + + // Calculate scrollbar bounding box + ImRect bb = GetWindowScrollbarRect(window, axis); + ImDrawCornerFlags rounding_corners = 0; + if (axis == ImGuiAxis_X) + { + rounding_corners |= ImDrawCornerFlags_BotLeft; + if (!window->ScrollbarY) + rounding_corners |= ImDrawCornerFlags_BotRight; + } + else + { + if ((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) + rounding_corners |= ImDrawCornerFlags_TopRight; + if (!window->ScrollbarX) + rounding_corners |= ImDrawCornerFlags_BotRight; + } + float size_avail = window->InnerRect.Max[axis] - window->InnerRect.Min[axis]; + float size_contents = window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f; + ScrollbarEx(bb, id, axis, &window->Scroll[axis], size_avail, size_contents, rounding_corners); +} + // Vertical/Horizontal scrollbar // The entire piece of code below is rather confusing because: // - We handle absolute seeking (when first clicking outside the grab) and relative manipulation (afterward or when clicking inside the grab) @@ -830,7 +877,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa if (bb_frame_width <= 0.0f || bb_frame_height <= 0.0f) return false; - // When we are too small, start hiding and disabling the grab (this reduce visual noise on very small window and facilitate using the resize grab) + // When we are too small, start hiding and disabling the grab (this reduce visual noise on very small window and facilitate using the window resize grab) float alpha = 1.0f; if ((axis == ImGuiAxis_Y) && bb_frame_height < g.FontSize + g.Style.FramePadding.y * 2.0f) alpha = ImSaturate((bb_frame_height - g.FontSize) / (g.Style.FramePadding.y * 2.0f)); @@ -839,13 +886,12 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa const ImGuiStyle& style = g.Style; const bool allow_interaction = (alpha >= 1.0f); - const bool horizontal = (axis == ImGuiAxis_X); ImRect bb = bb_frame; bb.Expand(ImVec2(-ImClamp(IM_FLOOR((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_FLOOR((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) - const float scrollbar_size_v = horizontal ? bb.GetWidth() : bb.GetHeight(); + const float scrollbar_size_v = (axis == ImGuiAxis_X) ? bb.GetWidth() : bb.GetHeight(); // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) // But we maintain a minimum size in pixel to allow for the user to still aim inside. @@ -861,11 +907,11 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa float scroll_max = ImMax(1.0f, size_contents_v - size_avail_v); float scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); - float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; + float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; // Grab position in normalized space if (held && allow_interaction && grab_h_norm < 1.0f) { - float scrollbar_pos_v = horizontal ? bb.Min.x : bb.Min.y; - float mouse_pos_v = horizontal ? g.IO.MousePos.x : g.IO.MousePos.y; + float scrollbar_pos_v = bb.Min[axis]; + float mouse_pos_v = g.IO.MousePos[axis]; // Click position in scrollbar normalized space (0.0f->1.0f) const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); @@ -882,7 +928,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; } - // Apply scroll + // Apply scroll (p_scroll_v will generally point on one member of window->Scroll) // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); *p_scroll_v = IM_ROUND(scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); @@ -897,10 +943,11 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa } // Render - window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, GetColorU32(ImGuiCol_ScrollbarBg), window->WindowRounding, rounding_corners); + const ImU32 bg_col = GetColorU32(ImGuiCol_ScrollbarBg); const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha); + window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, bg_col, window->WindowRounding, rounding_corners); ImRect grab_rect; - if (horizontal) + if (axis == ImGuiAxis_X) grab_rect = ImRect(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm), bb.Min.y, ImLerp(bb.Min.x, bb.Max.x, grab_v_norm) + grab_h_pixels, bb.Max.y); else grab_rect = ImRect(bb.Min.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm), bb.Max.x, ImLerp(bb.Min.y, bb.Max.y, grab_v_norm) + grab_h_pixels); @@ -909,38 +956,6 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa return held; } -void ImGui::Scrollbar(ImGuiAxis axis) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - - const ImGuiID id = GetWindowScrollbarID(window, axis); - KeepAliveID(id); - - // Calculate scrollbar bounding box - const ImRect outer_rect = window->Rect(); - const ImRect inner_rect = window->InnerRect; - const float border_size = window->WindowBorderSize; - const float scrollbar_size = window->ScrollbarSizes[axis ^ 1]; - IM_ASSERT(scrollbar_size > 0.0f); - const float other_scrollbar_size = window->ScrollbarSizes[axis]; - ImDrawCornerFlags rounding_corners = (other_scrollbar_size <= 0.0f) ? ImDrawCornerFlags_BotRight : 0; - ImRect bb; - if (axis == ImGuiAxis_X) - { - bb.Min = ImVec2(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size)); - bb.Max = ImVec2(inner_rect.Max.x, outer_rect.Max.y); - rounding_corners |= ImDrawCornerFlags_BotLeft; - } - else - { - bb.Min = ImVec2(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y); - bb.Max = ImVec2(outer_rect.Max.x, window->InnerRect.Max.y); - rounding_corners |= ((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) ? ImDrawCornerFlags_TopRight : 0; - } - ScrollbarEx(bb, id, axis, &window->Scroll[axis], inner_rect.Max[axis] - inner_rect.Min[axis], window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f, rounding_corners); -} - void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) { ImGuiWindow* window = GetCurrentWindow(); @@ -1044,7 +1059,7 @@ bool ImGui::Checkbox(const char* label, bool* v) else if (*v) { const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); - RenderCheckMark(check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); + RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); } if (g.LogEnabled) @@ -1119,6 +1134,7 @@ bool ImGui::RadioButton(const char* label, bool active) if (label_size.x > 0.0f) RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); return pressed; } @@ -1209,7 +1225,7 @@ void ImGui::Spacing() ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; - ItemSize(ImVec2(0,0)); + ItemSize(ImVec2(0, 0)); } void ImGui::Dummy(const ImVec2& size) @@ -1233,7 +1249,7 @@ void ImGui::NewLine() const ImGuiLayoutType backup_layout_type = window->DC.LayoutType; window->DC.LayoutType = ImGuiLayoutType_Vertical; if (window->DC.CurrLineSize.y > 0.0f) // In the event that we are on a line with items that is smaller that FontSize high, we will preserve its height. - ItemSize(ImVec2(0,0)); + ItemSize(ImVec2(0, 0)); else ItemSize(ImVec2(0.0f, g.FontSize)); window->DC.LayoutType = backup_layout_type; @@ -1595,7 +1611,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi // The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here. if (popup_max_height_in_items != -1 && !(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint)) - SetNextWindowSizeConstraints(ImVec2(0,0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); + SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); if (!BeginCombo(label, preview_value, ImGuiComboFlags_None)) return false; @@ -1653,6 +1669,7 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa // - DataTypeFormatString() // - DataTypeApplyOp() // - DataTypeApplyOpFromText() +// - DataTypeClamp() // - GetMinimumStepAtDecimalPrecision // - RoundScalarWithFormat<>() //------------------------------------------------------------------------- @@ -1804,11 +1821,9 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b return false; // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. - IM_ASSERT(data_type < ImGuiDataType_COUNT); - int data_backup[2]; - const ImGuiDataTypeInfo* type_info = ImGui::DataTypeGetInfo(data_type); - IM_ASSERT(type_info->Size <= sizeof(data_backup)); - memcpy(data_backup, p_data, type_info->Size); + const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type); + ImGuiDataTypeTempStorage data_backup; + memcpy(&data_backup, p_data, type_info->Size); if (format == NULL) format = type_info->ScanFmt; @@ -1880,7 +1895,35 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b IM_ASSERT(0); } - return memcmp(data_backup, p_data, type_info->Size) != 0; + return memcmp(&data_backup, p_data, type_info->Size) != 0; +} + +template +static bool ClampBehaviorT(T* v, T v_min, T v_max) +{ + if (*v < v_min) { *v = v_min; return true; } + if (*v > v_max) { *v = v_max; return true; } + return false; +} + +bool ImGui::DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max) +{ + switch (data_type) + { + case ImGuiDataType_S8: return ClampBehaviorT((ImS8* )p_data, *(const ImS8* )p_min, *(const ImS8* )p_max); + case ImGuiDataType_U8: return ClampBehaviorT((ImU8* )p_data, *(const ImU8* )p_min, *(const ImU8* )p_max); + case ImGuiDataType_S16: return ClampBehaviorT((ImS16* )p_data, *(const ImS16* )p_min, *(const ImS16* )p_max); + case ImGuiDataType_U16: return ClampBehaviorT((ImU16* )p_data, *(const ImU16* )p_min, *(const ImU16* )p_max); + case ImGuiDataType_S32: return ClampBehaviorT((ImS32* )p_data, *(const ImS32* )p_min, *(const ImS32* )p_max); + case ImGuiDataType_U32: return ClampBehaviorT((ImU32* )p_data, *(const ImU32* )p_min, *(const ImU32* )p_max); + case ImGuiDataType_S64: return ClampBehaviorT((ImS64* )p_data, *(const ImS64* )p_min, *(const ImS64* )p_max); + case ImGuiDataType_U64: return ClampBehaviorT((ImU64* )p_data, *(const ImU64* )p_min, *(const ImU64* )p_max); + case ImGuiDataType_Float: return ClampBehaviorT((float* )p_data, *(const float* )p_min, *(const float* )p_max); + case ImGuiDataType_Double: return ClampBehaviorT((double*)p_data, *(const double*)p_min, *(const double*)p_max); + case ImGuiDataType_COUNT: break; + } + IM_ASSERT(0); + return false; } static float GetMinimumStepAtDecimalPrecision(int decimal_precision) @@ -2132,8 +2175,10 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, } } } + + // Our current specs do NOT clamp when using CTRL+Click manual input, but we should eventually add a flag for that.. if (temp_input_is_active || temp_input_start) - return TempInputScalar(frame_bb, id, label, data_type, p_data, format); + return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2329,7 +2374,7 @@ float ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_m return (float)((FLOATTYPE)(v_clamped - v_min) / (FLOATTYPE)(v_max - v_min)); } -// FIXME: Move some of the code into SliderBehavior(). Current responsability is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc. +// FIXME: Move some of the code into SliderBehavior(). Current responsibility is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc. template bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) { @@ -2583,8 +2628,10 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat } } } + + // Our current specs do NOT clamp when using CTRL+Click manual input, but we should eventually add a flag for that.. if (temp_input_is_active || temp_input_start) - return TempInputScalar(frame_bb, id, label, data_type, p_data, format); + return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max); // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2883,7 +2930,21 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* return value_changed; } -bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format) +// Note that Drag/Slider functions are currently NOT forwarding the min/max values clamping values! +// This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility. +// However this may not be ideal for all uses, as some user code may break on out of bound values. +// In the future we should add flags to Slider/Drag to specify how to enforce min/max values with CTRL+Click. +// See GitHub issues #1829 and #3209 +// In the meanwhile, you can easily "wrap" those functions to enforce clamping, using wrapper functions, e.g. +// bool SliderFloatClamp(const char* label, float* v, float v_min, float v_max) +// { +// float v_backup = *v; +// if (!SliderFloat(label, v, v_min, v_max)) +// return false; +// *v = ImClamp(*v, v_min, v_max); +// return v_backup != *v; +// } +bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max) { ImGuiContext& g = *GImGui; @@ -2895,10 +2956,21 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; flags |= ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); - bool value_changed = TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags); - if (value_changed) + bool value_changed = false; + if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) { - value_changed = DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); + // Backup old value + size_t data_type_size = DataTypeGetInfo(data_type)->Size; + ImGuiDataTypeTempStorage data_backup; + memcpy(&data_backup, p_data, data_type_size); + + // Apply new value (or operations) then clamp + DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); + if (p_clamp_min && p_clamp_max) + DataTypeClamp(data_type, p_data, p_clamp_min, p_clamp_max); + + // Only mark as edited if new value is different + value_changed = memcmp(&data_backup, p_data, data_type_size) != 0; if (value_changed) MarkItemEdited(id); } @@ -2926,7 +2998,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) flags |= ImGuiInputTextFlags_CharsDecimal; flags |= ImGuiInputTextFlags_AutoSelectAll; - flags |= ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselve by comparing the actual data rather than the string. + flags |= ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. if (p_step != NULL) { @@ -3110,7 +3182,7 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() - return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0,0), flags, callback, user_data); + return InputTextEx(label, NULL, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data); } bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) @@ -3145,7 +3217,7 @@ static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* t const float line_height = g.FontSize; const float scale = line_height / font->FontSize; - ImVec2 text_size = ImVec2(0,0); + ImVec2 text_size = ImVec2(0, 0); float line_width = 0.0f; const ImWchar* s = text_begin; @@ -3344,7 +3416,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons if (!is_resizable) return; - // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the midly similar code (until we remove the U16 buffer alltogether!) + // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the mildly similar code (until we remove the U16 buffer altogether!) ImGuiContext& g = *GImGui; ImGuiInputTextState* edit_state = &g.InputTextState; IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID); @@ -3451,6 +3523,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (window->SkipItems) return false; + IM_ASSERT(buf != NULL && buf_size >= 0); IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackHistory) && (flags & ImGuiInputTextFlags_Multiline))); // Can't use both together (they both use up/down keys) IM_ASSERT(!((flags & ImGuiInputTextFlags_CallbackCompletion) && (flags & ImGuiInputTextFlags_AllowTabInput))); // Can't use both together (they both use tab key) @@ -3728,14 +3801,16 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (g.ActiveId == id && !g.ActiveIdIsJustActivated && !clear_active_id) { IM_ASSERT(state != NULL); + IM_ASSERT(io.KeyMods == GetMergedKeyModFlags() && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); // We rarely do this check, but if anything let's do it here. + const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_osx = io.ConfigMacOSXBehaviors; - const bool is_shortcut_key = (is_osx ? (io.KeySuper && !io.KeyCtrl) : (io.KeyCtrl && !io.KeySuper)) && !io.KeyAlt && !io.KeyShift; // OS X style: Shortcuts using Cmd/Super instead of Ctrl - const bool is_osx_shift_shortcut = is_osx && io.KeySuper && io.KeyShift && !io.KeyCtrl && !io.KeyAlt; + const bool is_osx_shift_shortcut = is_osx && (io.KeyMods == (ImGuiKeyModFlags_Super | ImGuiKeyModFlags_Shift)); const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End - const bool is_ctrl_key_only = io.KeyCtrl && !io.KeyShift && !io.KeyAlt && !io.KeySuper; - const bool is_shift_key_only = io.KeyShift && !io.KeyCtrl && !io.KeyAlt && !io.KeySuper; + const bool is_ctrl_key_only = (io.KeyMods == ImGuiKeyModFlags_Ctrl); + const bool is_shift_key_only = (io.KeyMods == ImGuiKeyModFlags_Shift); + const bool is_shortcut_key = g.IO.ConfigMacOSXBehaviors ? (io.KeyMods == ImGuiKeyModFlags_Super) : (io.KeyMods == ImGuiKeyModFlags_Ctrl); const bool is_cut = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_X)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Delete))) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); const bool is_copy = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_C)) || (is_ctrl_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_password && (!is_multiline || state->HasSelection()); @@ -4493,52 +4568,6 @@ bool ImGui::ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags fl return true; } -static inline ImU32 ImAlphaBlendColor(ImU32 col_a, ImU32 col_b) -{ - float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f; - int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t); - int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t); - int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t); - return IM_COL32(r, g, b, 0xFF); -} - -// Helper for ColorPicker4() -// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. -// I spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding alltogether. -void ImGui::RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags) -{ - ImGuiWindow* window = GetCurrentWindow(); - if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF) - { - ImU32 col_bg1 = GetColorU32(ImAlphaBlendColor(IM_COL32(204,204,204,255), col)); - ImU32 col_bg2 = GetColorU32(ImAlphaBlendColor(IM_COL32(128,128,128,255), col)); - window->DrawList->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags); - - int yi = 0; - for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++) - { - float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y); - if (y2 <= y1) - continue; - for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f) - { - float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x); - if (x2 <= x1) - continue; - int rounding_corners_flags_cell = 0; - if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; } - if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; } - rounding_corners_flags_cell &= rounding_corners_flags; - window->DrawList->AddRectFilled(ImVec2(x1,y1), ImVec2(x2,y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell); - } - } - } - else - { - window->DrawList->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags); - } -} - // Helper for ColorPicker4() static void RenderArrowsForVerticalBar(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, float bar_w, float alpha) { @@ -4773,7 +4802,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags__DisplayMask) == 0) if (ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_DisplayRGB)) { - // FIXME: Hackily differenciating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget. + // FIXME: Hackily differentiating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget. // For the later we don't want to run the hue-wrap canceling code. If you are well versed in HSV picker please provide your input! (See #2050) value_changed_fix_hue_wrap = (g.ActiveId != 0 && !g.ActiveIdAllowOverlap); value_changed = true; @@ -4909,7 +4938,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { float alpha = ImSaturate(col[3]); ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size); - RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f)); + RenderColorRectWithAlphaCheckerboard(draw_list, bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f)); draw_list->AddRectFilledMultiColor(bar1_bb.Min, bar1_bb.Max, user_col32_striped_of_alpha, user_col32_striped_of_alpha, user_col32_striped_of_alpha & ~IM_COL32_A_MASK, user_col32_striped_of_alpha & ~IM_COL32_A_MASK); float bar1_line_y = IM_ROUND(picker_pos.y + (1.0f - alpha) * sv_picker_size); RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f); @@ -4973,7 +5002,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) { float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f); - RenderColorRectWithAlphaCheckerboard(ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight); + RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight); window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft); } else @@ -4981,7 +5010,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha; if (col_source.w < 1.0f) - RenderColorRectWithAlphaCheckerboard(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); + RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); else window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All); } @@ -5365,30 +5394,40 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l return is_open; } - // Flags that affects opening behavior: - // - 0 (default) .................... single-click anywhere to open - // - OpenOnDoubleClick .............. double-click anywhere to open - // - OpenOnArrow .................... single-click on arrow to open - // - OpenOnDoubleClick|OpenOnArrow .. single-click on arrow or double-click anywhere to open - ImGuiButtonFlags button_flags = 0; + ImGuiButtonFlags button_flags = ImGuiTreeNodeFlags_None; if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) button_flags |= ImGuiButtonFlags_AllowItemOverlap; - if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) - button_flags |= ImGuiButtonFlags_PressedOnDoubleClick | ((flags & ImGuiTreeNodeFlags_OpenOnArrow) ? ImGuiButtonFlags_PressedOnClickRelease : 0); - else - button_flags |= ImGuiButtonFlags_PressedOnClickRelease; if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; // We allow clicking on the arrow section with keyboard modifiers held, in order to easily // allow browsing a tree while preserving selection with code implementing multi-selection patterns. // When clicking on the rest of the tree node we always disallow keyboard modifiers. - const float hit_padding_x = style.TouchExtraPadding.x; - const float arrow_hit_x1 = (text_pos.x - text_offset_x) - hit_padding_x; - const float arrow_hit_x2 = (text_pos.x - text_offset_x) + (g.FontSize + padding.x * 2.0f) + hit_padding_x; - if (window != g.HoveredWindow || !(g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2)) + const float arrow_hit_x1 = (text_pos.x - text_offset_x) - style.TouchExtraPadding.x; + const float arrow_hit_x2 = (text_pos.x - text_offset_x) + (g.FontSize + padding.x * 2.0f) + style.TouchExtraPadding.x; + const bool is_mouse_x_over_arrow = (g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2); + if (window != g.HoveredWindow || !is_mouse_x_over_arrow) button_flags |= ImGuiButtonFlags_NoKeyModifiers; + // Open behaviors can be altered with the _OpenOnArrow and _OnOnDoubleClick flags. + // Some alteration have subtle effects (e.g. toggle on MouseUp vs MouseDown events) due to requirements for multi-selection and drag and drop support. + // - Single-click on label = Toggle on MouseUp (default) + // - Single-click on arrow = Toggle on MouseUp (when _OpenOnArrow=0) + // - Single-click on arrow = Toggle on MouseDown (when _OpenOnArrow=1) + // - Double-click on label = Toggle on MouseDoubleClick (when _OpenOnDoubleClick=1) + // - Double-click on arrow = Toggle on MouseDoubleClick (when _OpenOnDoubleClick=1 and _OpenOnArrow=0) + // This makes _OpenOnArrow have a subtle effect on _OpenOnDoubleClick: arrow click reacts on Down rather than Up. + // It is rather standard that arrow click react on Down rather than Up and we'd be tempted to make it the default + // (by removing the _OpenOnArrow test below), however this would have a perhaps surprising effect on CollapsingHeader()? + // So right now we are making this optional. May evolve later. + // We set ImGuiButtonFlags_PressedOnClickRelease on OpenOnDoubleClick because we want the item to be active on the initial MouseDown in order for drag and drop to work. + if (is_mouse_x_over_arrow && (flags & ImGuiTreeNodeFlags_OpenOnArrow)) + button_flags |= ImGuiButtonFlags_PressedOnClick; + else if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) + button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; + else + button_flags |= ImGuiButtonFlags_PressedOnClickRelease; + bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0; const bool was_selected = selected; @@ -5397,16 +5436,20 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l bool toggled = false; if (!is_leaf) { - if (pressed) + if (pressed && g.DragDropHoldJustPressedId != id) { if ((flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) == 0 || (g.NavActivateId == id)) toggled = true; if (flags & ImGuiTreeNodeFlags_OpenOnArrow) - toggled |= (g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2) && (!g.NavDisableMouseHover); // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job + toggled |= is_mouse_x_over_arrow && !g.NavDisableMouseHover; // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job if ((flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) && g.IO.MouseDoubleClicked[0]) toggled = true; - if (g.DragDropActive && is_open) // When using Drag and Drop "hold to open" we keep the node highlighted after opening, but never close it again. - toggled = false; + } + else if (pressed && g.DragDropHoldJustPressedId == id) + { + IM_ASSERT(button_flags & ImGuiButtonFlags_PressedOnDragDropHold); + if (!is_open) // When using Drag and Drop "hold to open" we keep the node highlighted after opening, but never close it again. + toggled = true; } if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Left && is_open) @@ -5507,7 +5550,8 @@ void ImGui::TreePush(const void* ptr_id) void ImGui::TreePushOverrideID(ImGuiID id) { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; Indent(); window->DC.TreeDepth++; window->IDStack.push_back(id); @@ -5604,6 +5648,8 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags // Tip: pass a non-visible label (e.g. "##dummy") then you can use the space to draw other text or image. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id. +// With this scheme, ImGuiSelectableFlags_SpanAllColumns and ImGuiSelectableFlags_AllowItemOverlap are also frequently used flags. +// FIXME: Selectable() with (size.x == 0.0f) and (SelectableTextAlign.x > 0.0f) followed by SameLine() is currently not supported. bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) { ImGuiWindow* window = GetCurrentWindow(); @@ -5616,44 +5662,48 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. PushColumnsBackground(); + // Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle. ImGuiID id = window->GetID(label); ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y); ImVec2 pos = window->DC.CursorPos; pos.y += window->DC.CurrLineTextBaseOffset; - ImRect bb_inner(pos, pos + size); ItemSize(size, 0.0f); - // Fill horizontal space. - ImVec2 window_padding = window->WindowPadding; - float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x : GetContentRegionMax().x; - float w_draw = ImMax(label_size.x, window->Pos.x + max_x - window_padding.x - pos.x); - ImVec2 size_draw((size_arg.x != 0 && !(flags & ImGuiSelectableFlags_DrawFillAvailWidth)) ? size_arg.x : w_draw, size_arg.y != 0.0f ? size_arg.y : size.y); - ImRect bb(pos, pos + size_draw); - if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth)) - bb.Max.x += window_padding.x; + // Fill horizontal space + const float min_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? window->ContentRegionRect.Min.x : pos.x; + const float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? window->ContentRegionRect.Max.x : GetContentRegionMaxAbs().x; + if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_SpanAvailWidth)) + size.x = ImMax(label_size.x, max_x - min_x); + + // Text stays at the submission position, but bounding box may be extended on both sides + const ImVec2 text_min = pos; + const ImVec2 text_max(min_x + size.x, pos.y + size.y); - // Selectables are tightly packed together so we extend the box to cover spacing between selectable. + // Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable. + ImRect bb_enlarged(min_x, pos.y, text_max.x, text_max.y); const float spacing_x = style.ItemSpacing.x; const float spacing_y = style.ItemSpacing.y; const float spacing_L = IM_FLOOR(spacing_x * 0.50f); const float spacing_U = IM_FLOOR(spacing_y * 0.50f); - bb.Min.x -= spacing_L; - bb.Min.y -= spacing_U; - bb.Max.x += (spacing_x - spacing_L); - bb.Max.y += (spacing_y - spacing_U); + bb_enlarged.Min.x -= spacing_L; + bb_enlarged.Min.y -= spacing_U; + bb_enlarged.Max.x += (spacing_x - spacing_L); + bb_enlarged.Max.y += (spacing_y - spacing_U); + //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_align.Min, bb_align.Max, IM_COL32(255, 0, 0, 255)); } + //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_enlarged.Min, bb_enlarged.Max, IM_COL32(0, 255, 0, 255)); } bool item_add; if (flags & ImGuiSelectableFlags_Disabled) { ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus; - item_add = ItemAdd(bb, id); + item_add = ItemAdd(bb_enlarged, id); window->DC.ItemFlags = backup_item_flags; } else { - item_add = ItemAdd(bb, id); + item_add = ItemAdd(bb_enlarged, id); } if (!item_add) { @@ -5676,7 +5726,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const bool was_selected = selected; bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); + bool pressed = ButtonBehavior(bb_enlarged, id, &hovered, &held, button_flags); // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) @@ -5703,18 +5753,15 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (hovered || selected) { const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); - RenderFrame(bb.Min, bb.Max, col, false, 0.0f); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + RenderFrame(bb_enlarged.Min, bb_enlarged.Max, col, false, 0.0f); + RenderNavHighlight(bb_enlarged, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); } if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) - { PopColumnsBackground(); - bb.Max.x -= (GetContentRegionMax().x - max_x); - } if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); - RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb); + RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb_enlarged); if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor(); // Automatically close popups @@ -5867,13 +5914,13 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v // - PlotHistogram() //------------------------------------------------------------------------- -void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) +int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) { + ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) - return; + return -1; - ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); @@ -5888,7 +5935,7 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0, &frame_bb)) - return; + return -1; const bool hovered = ItemHoverable(frame_bb, id); // Determine scale from values if not specified @@ -5913,13 +5960,13 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); const int values_count_min = (plot_type == ImGuiPlotType_Lines) ? 2 : 1; + int idx_hovered = -1; if (values_count >= values_count_min) { int res_w = ImMin((int)frame_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); int item_count = values_count + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0); // Tooltip on hover - int v_hovered = -1; if (hovered && inner_bb.Contains(g.IO.MousePos)) { const float t = ImClamp((g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f); @@ -5932,7 +5979,7 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1); else if (plot_type == ImGuiPlotType_Histogram) SetTooltip("%d: %8.4g", v_idx, v0); - v_hovered = v_idx; + idx_hovered = v_idx; } const float t_step = 1.0f / (float)res_w; @@ -5959,13 +6006,13 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge ImVec2 pos1 = ImLerp(inner_bb.Min, inner_bb.Max, (plot_type == ImGuiPlotType_Lines) ? tp1 : ImVec2(tp1.x, histogram_zero_line_t)); if (plot_type == ImGuiPlotType_Lines) { - window->DrawList->AddLine(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + window->DrawList->AddLine(pos0, pos1, idx_hovered == v1_idx ? col_hovered : col_base); } else if (plot_type == ImGuiPlotType_Histogram) { if (pos1.x >= pos0.x + 2.0f) pos1.x -= 1.0f; - window->DrawList->AddRectFilled(pos0, pos1, v_hovered == v1_idx ? col_hovered : col_base); + window->DrawList->AddRectFilled(pos0, pos1, idx_hovered == v1_idx ? col_hovered : col_base); } t0 = t1; @@ -5979,6 +6026,10 @@ void ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_ge if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); + + // Return hovered index or -1 if none are hovered. + // This is currently not exposed in the public API because we need a larger redesign of the whole thing, but in the short-term we are making it available in PlotEx(). + return idx_hovered; } struct ImGuiPlotArrayGetterData @@ -6210,7 +6261,7 @@ void ImGui::EndMainMenuBar() // When the user has left the menu layer (typically: closed menus through activation of an item), we restore focus to the previous window // FIXME: With this strategy we won't be able to restore a NULL focus. ImGuiContext& g = *GImGui; - if (g.CurrentWindow == g.NavWindow && g.NavLayer == 0 && !g.NavAnyRequest) + if (g.CurrentWindow == g.NavWindow && g.NavLayer == ImGuiNavLayer_Main && !g.NavAnyRequest) FocusTopMostWindowUnderOne(g.NavWindow, NULL); End(); @@ -6255,7 +6306,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) g.NavWindow = window; // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent) // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu, - // However the final position is going to be different! It is choosen by FindBestWindowPosForPopup(). + // However the final position is going to be different! It is chosen by FindBestWindowPosForPopup(). // e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering. ImVec2 popup_pos, pos = window->DC.CursorPos; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) @@ -6274,10 +6325,12 @@ bool ImGui::BeginMenu(const char* label, bool enabled) else { // Menu inside a menu + // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f. + // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); - float w = window->DC.MenuColumns.DeclColumns(label_size.x, 0.0f, IM_FLOOR(g.FontSize * 1.20f)); // Feedback to next frame - float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); - pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, 0.0f, IM_FLOOR(g.FontSize * 1.20f)); // Feedback to next frame + float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); + pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(min_w, 0.0f)); ImU32 text_col = GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled); RenderArrow(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right); } @@ -6422,18 +6475,21 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo } else { - ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f); - float w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_size.x, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame - float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w); - pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f)); - if (shortcut_size.x > 0.0f) + // Menu item inside a vertical menu + // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f. + // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. + float shortcut_w = shortcut ? CalcTextSize(shortcut, NULL).x : 0.0f; + float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_w, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame + float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); + pressed = Selectable(label, false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + if (shortcut_w > 0.0f) { PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); RenderText(pos + ImVec2(window->DC.MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); PopStyleColor(); } if (selected) - RenderCheckMark(pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); + RenderCheckMark(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); } IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); @@ -7033,6 +7089,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const ImGuiID id = TabBarCalcTabID(tab_bar, label); // If the user called us with *p_open == false, we early out and don't render. We make a dummy call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); if (p_open && !*p_open) { PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); @@ -7090,6 +7147,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab_bar->NextSelectedTabId = id; // Lock visibility + // (Note: tab_contents_visible != tab_selected... because CTRL+TAB operations may preview some tabs without selecting them!) bool tab_contents_visible = (tab_bar->VisibleTabId == id); if (tab_contents_visible) tab_bar->VisibleTabWasSubmitted = true; @@ -7195,7 +7253,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Render tab label, process close button const ImGuiID close_button_id = p_open ? window->GetID((void*)((intptr_t)id + 1)) : 0; - bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id); + bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id, tab_contents_visible); if (just_closed && p_open != NULL) { *p_open = false; @@ -7270,13 +7328,21 @@ void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabI // Render text label (with custom clipping) + Unsaved Document marker + Close Button logic // We tend to lock style.FramePadding for a given tab-bar, hence the 'frame_padding' parameter. -bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id) +bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible) { ImGuiContext& g = *GImGui; ImVec2 label_size = CalcTextSize(label, NULL, true); if (bb.GetWidth() <= 1.0f) return false; + // In Style V2 we'll have full override of all colors per state (e.g. focused, selected) + // But right now if you want to alter text color of tabs this is what you need to do. +#if 0 + const float backup_alpha = g.Style.Alpha; + if (!is_contents_visible) + g.Style.Alpha *= 0.7f; +#endif + // Render text label (with clipping + alpha gradient) + unsaved marker const char* TAB_UNSAVED_MARKER = "*"; ImRect text_pixel_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y); @@ -7296,8 +7362,9 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, bool close_button_pressed = false; bool close_button_visible = false; if (close_button_id != 0) - if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id) - close_button_visible = true; + if (is_contents_visible || bb.GetWidth() >= g.Style.TabMinWidthForUnselectedCloseButton) + if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id) + close_button_visible = true; if (close_button_visible) { ImGuiItemHoveredDataBackup last_item_backup; @@ -7318,6 +7385,11 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, float ellipsis_max_x = close_button_visible ? text_pixel_clip_bb.Max.x : bb.Max.x - 1.0f; RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, text_pixel_clip_bb.Max.x, ellipsis_max_x, label, NULL, &label_size); +#if 0 + if (!is_contents_visible) + g.Style.Alpha = backup_alpha; +#endif + return close_button_pressed; } @@ -7525,8 +7597,8 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - IM_ASSERT(columns_count >= 1 && columns_count <= 64); // Maximum 64 columns - IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported + IM_ASSERT(columns_count >= 1); + IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported // Acquire storage for the columns set ImGuiID id = GetColumnsID(str_id, columns_count); From 1a985bb4555468dd23776c08b5c81d69920c1d32 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 16:31:16 +0200 Subject: [PATCH 0262/1018] update README --- .travis.yml | 4 ++-- README.md | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index efefe2ac..c134976d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,13 +12,13 @@ addons: - cmake - libarmadillo-dev - libeigen3-dev - - libopenblas-dev + - libcgal-dev - libsuitesparse-dev + - libopenblas-dev - libglew-dev - libglfw3-dev - libglm-dev - cimg-dev - - libcgal-dev script: - export CC=/usr/bin/gcc-8 - export CXX=/usr/bin/g++-8 diff --git a/README.md b/README.md index d093719b..574f7cd1 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 8.3, cuda >= 10.1, libarmadillo, libeigen, libsuitesparse, libopenblas, opengl, glew, gnuplot, libcgal, libgles2-mesa, cimg +g++ >= 8.3, cuda >= 10.1, cmake >= 3.12, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot In Ubuntu (>= 18.04) you can install them with: - sudo apt install libarmadillo-dev libeigen3-dev libopenblas-dev libsuitesparse-dev libglew-dev freeglut3-dev libgles2-mesa-dev cimg-dev libcgal-dev + sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot #### Build Status @@ -140,7 +140,7 @@ The viewer was initially based in the viewer of [https://github.com/dgpdec/cours MIT License -## Authors -- [Lizeth Joseline Fuentes Pérez](https://github.com/lishh) +## Authors/Contributors - [Luciano Arnaldo Romero Calla](https://github.com/larc) +- [Lizeth Joseline Fuentes Pérez](https://github.com/lishh) From d96c328f816020f0c836e0b68e73fae9262dc0d2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 21:26:08 +0200 Subject: [PATCH 0263/1018] refactoring active mesh id, camera methods, grid view meshes --- include/viewer/camera.h | 6 +- include/viewer/viewer.h | 6 +- src/app_viewer.cpp | 142 ++++++++++++++++++++-------------------- src/viewer/camera.cpp | 8 +-- src/viewer/viewer.cpp | 99 ++++++++++++++-------------- 5 files changed, 130 insertions(+), 131 deletions(-) diff --git a/include/viewer/camera.h b/include/viewer/camera.h index 663a295c..f73e00c1 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -27,9 +27,9 @@ class camera void mouse(int button, int state, int x, int y); void motion(int x, int y); void idle(); - void zoomIn(); - void zoomOut(); - quaternion currentRotation() const; + void zoom_in(); + void zoom_out(); + quaternion current_rotation() const; }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index cff8fd9e..eda8279f 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -55,7 +55,7 @@ class viewer process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; }; - static const int m_window_size[N_MESHES][2]; + static const int m_window_size[N_MESHES + 1][2]; GLFWwindow * window = nullptr; @@ -80,7 +80,7 @@ class viewer che_viewer meshes[N_MESHES]; size_t n_meshes = 0; - index_t current = 0; // current mesh + index_t idx_active_mesh = 0; // idx_active_mesh mesh index_t render_opt = 0; @@ -124,7 +124,7 @@ class viewer bool run(); - che_viewer & mesh(); + che_viewer & active_mesh(); void add_process(const int & key, const process_t & process); void add_mesh(che * p_mesh); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 88fb9112..9b72b91a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -120,7 +120,7 @@ int app_viewer::main(int nargs, const char ** args) bool paint_holes_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t nv = mesh->n_vertices(); @@ -137,7 +137,7 @@ bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); gproshan_debug(removing vertex); mesh->remove_non_manifold_vertices(); @@ -150,13 +150,13 @@ bool app_viewer::process_delete_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) return true; + if(!view->active_mesh().selected.size()) return true; gproshan_debug(removing vertex); - mesh->remove_vertices(view->mesh().selected); - view->mesh().selected.clear(); + mesh->remove_vertices(view->active_mesh().selected); + view->active_mesh().selected.clear(); gproshan_debug(removing vertex); return false; @@ -165,7 +165,7 @@ bool app_viewer::process_delete_vertices(viewer * p_view) bool app_viewer::process_poisson(viewer * p_view, const index_t & k) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t old_n_vertices = mesh->n_vertices(); delete [] fill_all_holes(mesh); @@ -201,7 +201,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); fill_all_holes(mesh); @@ -214,7 +214,7 @@ bool app_viewer::process_noise(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); std::default_random_engine generator; std::uniform_int_distribution d_mod_5(0, 4); @@ -236,7 +236,7 @@ bool app_viewer::process_black_noise(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); std::default_random_engine generator; std::uniform_int_distribution d_mod_5(0, 4); @@ -258,7 +258,7 @@ bool app_viewer::process_threshold(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); for(index_t v = 0; v < mesh->n_vertices(); v++) mesh->color(v) = mesh->color(v) > 0.5 ? 1 : 0.5; @@ -269,7 +269,7 @@ bool app_viewer::process_threshold(viewer * p_view) bool app_viewer::process_functional_maps(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int K = 20; @@ -294,19 +294,19 @@ bool app_viewer::process_functional_maps(viewer * p_view) for(index_t k = 0; k < N_MESHES; k++) { if(k) view->add_mesh({new che(*mesh)}); - view->current = k; + view->idx_active_mesh = k; eigvec.col(k) -= eigvec.col(k).min(); eigvec.col(k) /= eigvec.col(k).max(); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - view->mesh()->color(v) = eigvec(v, k); + view->active_mesh()->color(v) = eigvec(v, k); - view->mesh().update_vbo(); + view->active_mesh().update_vbo(); } - view->current = 0; + view->idx_active_mesh = 0; } return true; @@ -315,7 +315,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) bool app_viewer::process_wks(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int K = 100; static int T = 100; @@ -360,7 +360,7 @@ bool app_viewer::process_wks(viewer * p_view) bool app_viewer::process_hks(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int K = 100; static int T = 100; @@ -408,7 +408,7 @@ bool app_viewer::process_hks(viewer * p_view) bool app_viewer::process_gps(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int K = 50; ImGui::InputInt("eigenvectors", &K); @@ -454,15 +454,15 @@ bool app_viewer::process_key_points(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); key_points kps(mesh); - view->mesh().selected.clear(); - view->mesh().selected.reserve(kps.size()); + view->active_mesh().selected.clear(); + view->active_mesh().selected.reserve(kps.size()); for(index_t i = 0; i < kps.size(); i++) - view->mesh().selected.push_back(kps[i]); + view->active_mesh().selected.push_back(kps[i]); return false; } @@ -471,7 +471,7 @@ bool app_viewer::process_key_components(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); key_points kps(mesh); key_components kcs(mesh, kps, .25); @@ -489,7 +489,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); TIC(view->time) index_t * toplevel = new index_t[mesh->n_vertices()]; @@ -498,7 +498,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) vertex vdir; patch p; real_t mean_edge = mesh->mean_edge(); - for(auto & v: view->mesh().selected) + for(auto & v: view->active_mesh().selected) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) @@ -528,7 +528,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) avg_nvp += p.vertices.size(); } - avg_nvp /= view->mesh().selected.size(); + avg_nvp /= view->active_mesh().selected.size(); gproshan_debug_var(avg_nvp); delete [] toplevel; @@ -542,7 +542,7 @@ bool app_viewer::process_denoising(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t n; // dct size_t m, M; @@ -566,7 +566,7 @@ bool app_viewer::process_super_resolution(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t n; // dct size_t m, M; @@ -590,7 +590,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t n; // dct size_t m, M; @@ -615,7 +615,7 @@ bool app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); -// mesh_iterative_inpaiting(mesh, view->mesh().selected, freq, rt, m, M, f, learn); +// mesh_iterative_inpaiting(mesh, view->active_mesh().selected, freq, rt, m, M, f, learn); return false; } @@ -624,7 +624,7 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); mesh->multiplicate_vertices(); mesh.update(); @@ -638,15 +638,15 @@ bool app_viewer::compute_toplesets(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); index_t * toplesets = new index_t[mesh->n_vertices()]; index_t * sorted = new index_t[mesh->n_vertices()]; vector limites; - mesh->compute_toplesets(toplesets, sorted, limites, view->mesh().selected); + mesh->compute_toplesets(toplesets, sorted, limites, view->active_mesh().selected); size_t n_toplesets = limites.size() - 1; @@ -669,13 +669,13 @@ bool app_viewer::process_voronoi(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); TIC(view->time) #ifdef GPROSHAN_CUDA - geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_GPU, nullptr, 1); + geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_GPU, nullptr, 1); #else - geodesics ptp(mesh, view->mesh().selected, geodesics::FM, nullptr, 1); + geodesics ptp(mesh, view->active_mesh().selected, geodesics::FM, nullptr, 1); #endif TOC(view->time) gproshan_log_var(view->time); @@ -684,7 +684,7 @@ bool app_viewer::process_voronoi(viewer * p_view) for(index_t i = 0; i < mesh->n_vertices(); i++) { mesh->color(i) = ptp.clusters[i]; - mesh->color(i) /= view->mesh().selected.size() + 1; + mesh->color(i) /= view->active_mesh().selected.size() + 1; } return false; @@ -694,7 +694,7 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); gproshan_input(radio); real_t radio; cin >> radio; @@ -703,13 +703,13 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) double time_fps; TIC(view->time) - radio = farthest_point_sampling_ptp_gpu(mesh, view->mesh().selected, time_fps, NIL, radio); + radio = farthest_point_sampling_ptp_gpu(mesh, view->active_mesh().selected, time_fps, NIL, radio); TOC(view->time) gproshan_log_var(time_fps); #endif // GPROSHAN_CUDA gproshan_log_var(radio); - gproshan_log_var(view->mesh().selected.size()); + gproshan_log_var(view->active_mesh().selected.size()); gproshan_log_var(view->time); return false; @@ -718,7 +718,7 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) bool app_viewer::process_farthest_point_sampling(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int n = 10; static real_t radio; @@ -729,7 +729,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) if(ImGui::Button("Run")) { TIC(view->time) - load_sampling(view->mesh().selected, radio, mesh, n); + load_sampling(view->active_mesh().selected, radio, mesh, n); TOC(view->time) gproshan_log_var(view->time); } @@ -740,7 +740,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) bool app_viewer::process_fairing_spectral(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int k = 100; ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices() / 6); @@ -760,7 +760,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) bool app_viewer::process_fairing_taubin(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static float step = 0.001; //cin >> step; ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); @@ -781,13 +781,13 @@ bool app_viewer::process_geodesics_fm(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics fm(mesh, view->mesh().selected); + geodesics fm(mesh, view->active_mesh().selected); TOC(view->time) gproshan_log_var(view->time); @@ -800,13 +800,13 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_CPU); + geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_CPU); TOC(view->time) gproshan_log_var(view->time); @@ -819,13 +819,13 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->mesh().selected, geodesics::HEAT_FLOW); + geodesics heat_flow(mesh, view->active_mesh().selected, geodesics::HEAT_FLOW); TOC(view->time) gproshan_log_var(view->time); @@ -841,10 +841,10 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); if(view->n_dist != mesh->n_vertices()) { @@ -855,7 +855,7 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) } TIC(view->time) - geodesics ptp(mesh, view->mesh().selected, geodesics::PTP_GPU, view->dist); + geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_GPU, view->dist); TOC(view->time) gproshan_log_var(view->time); @@ -868,13 +868,13 @@ bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); - if(!view->mesh().selected.size()) - view->mesh().selected.push_back(0); + if(!view->active_mesh().selected.size()) + view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->mesh().selected, geodesics::HEAT_FLOW_GPU); + geodesics heat_flow(mesh, view->active_mesh().selected, geodesics::HEAT_FLOW_GPU); TOC(view->time) gproshan_log_var(view->time); @@ -890,7 +890,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); size_t old_n_vertices, n_vertices = mesh->n_vertices(); size_t n_holes = mesh->n_borders(); @@ -921,7 +921,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; @@ -973,7 +973,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); index_t levels; cin >> levels; @@ -1000,7 +1000,7 @@ bool app_viewer::select_multiple(viewer * p_view) stringstream ss(line); index_t v; while(ss >> v) - view->mesh().selected.push_back(v); + view->active_mesh().selected.push_back(v); } return true; diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index bec9a7fb..7df410fd 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -30,7 +30,7 @@ quaternion camera::clickToSphere(int x, int y) x %= w; y %= h; - quaternion p(0., + quaternion p( 0., 2. * (double) x / (double) w - 1., 2. * (double) y / (double) h - 1., 0.); @@ -48,7 +48,7 @@ quaternion camera::clickToSphere(int x, int y) return p; } -quaternion camera::currentRotation() const +quaternion camera::current_rotation() const { return (pDrag * pClick.conj()) * rLast; } @@ -102,12 +102,12 @@ void camera::idle() t0 = t1; } -void camera::zoomIn() +void camera::zoom_in() { zoom -= 0.01; } -void camera::zoomOut() +void camera::zoom_out() { zoom += 0.01; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index de7183fe..ceb8bb55 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -28,10 +28,12 @@ using namespace std; namespace gproshan { -const int viewer::m_window_size[N_MESHES][2] = {{1, 1}, {1, 2}, {1, 3}, - {2, 2}, {2, 3}, {2, 3}, - {2, 4}, {2, 4}, {2, 5}, - {2, 5}, {3, 4}, {3, 4}}; +const int viewer::m_window_size[N_MESHES + 1][2] = {{1, 1}, + {1, 1}, {1, 2}, {1, 3}, + {2, 2}, {2, 3}, {2, 3}, + {2, 4}, {2, 4}, {2, 5}, + {2, 5}, {3, 4}, {3, 4} + }; viewer::viewer() @@ -71,8 +73,8 @@ bool viewer::run() while(!glfwWindowShouldClose(window)) { glfwGetFramebufferSize(window, &viewport_width, &viewport_height); - viewport_width /= m_window_size[n_meshes - 1][1]; - viewport_height /= m_window_size[n_meshes - 1][0]; + viewport_width /= m_window_size[n_meshes][1]; + viewport_height /= m_window_size[n_meshes][0]; eye = vertex(0., 0., -2. * cam.zoom); center = vertex(0., 0., 0.); @@ -80,7 +82,7 @@ bool viewer::run() light = vertex(-1., 1., -2.); - quaternion r = cam.currentRotation(); + quaternion r = cam.current_rotation(); eye = r.conj() * eye * r; light = r.conj() * light * r; @@ -111,11 +113,11 @@ bool viewer::run() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; i++) - if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == current, i != current)) + if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { - current = i; + idx_active_mesh = i; sphere_translations.clear(); - glfwSetWindowTitle(window, mesh()->filename().c_str()); + glfwSetWindowTitle(window, active_mesh()->filename().c_str()); } ImGui::EndMenu(); @@ -150,7 +152,7 @@ bool viewer::run() ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); pro.selected = pro.selected && p.second.function(this); - mesh().update_vbo(); + active_mesh().update_vbo(); ImGui::End(); } @@ -167,9 +169,9 @@ bool viewer::run() return true; } -che_viewer & viewer::mesh() +che_viewer & viewer::active_mesh() { - return meshes[current]; + return meshes[idx_active_mesh]; } void viewer::info_gl() @@ -307,18 +309,15 @@ void viewer::add_mesh(che * p_mesh) meshes[n_meshes].log_info(); n_meshes++; - current = n_meshes - 1; - glfwSetWindowTitle(window, mesh()->filename().c_str()); + idx_active_mesh = n_meshes - 1; + glfwSetWindowTitle(window, active_mesh()->filename().c_str()); - const int * mw = m_window_size[n_meshes - 1]; - - index_t m = n_meshes - 1; - for(int i = mw[1] - 1; i >= 0; i--) - for(int j = 0; j < mw[0]; j++) + const int & rows = m_window_size[n_meshes][0]; + const int & cols = m_window_size[n_meshes][1]; + for(int m = 0; m < n_meshes; m++) { - meshes[m].vx = i; - meshes[m].vy = j; - if(!m--) return; + meshes[m].vx = m % cols; + meshes[m].vy = rows - (m / cols) - 1; } } @@ -371,13 +370,13 @@ void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset if(yoffset > 0) { - view->cam.zoomIn(); + view->cam.zoom_in(); view->action = true; } if(yoffset < 0) { - view->cam.zoomOut(); + view->cam.zoom_out(); view->action = true; } } @@ -399,12 +398,12 @@ bool viewer::menu_help(viewer * view) bool viewer::menu_reset_mesh(viewer * view) { - view->mesh().selected.clear(); + view->active_mesh().selected.clear(); view->other_vertices.clear(); view->vectors.clear(); - view->mesh().reload(); - view->mesh().update_vbo(); + view->active_mesh().reload(); + view->active_mesh().update_vbo(); return false; } @@ -416,11 +415,11 @@ bool viewer::menu_save_mesh(viewer * view) gproshan_log(format: [off obj ply]); string format; cin >> format; - string file = view->mesh()->filename() + "_new"; + string file = view->active_mesh()->filename() + "_new"; - if(format == "off") che_off::write_file(view->mesh(), file); - if(format == "obj") che_obj::write_file(view->mesh(), file); - if(format == "ply") che_ply::write_file(view->mesh(), file); + if(format == "off") che_off::write_file(view->active_mesh(), file); + if(format == "obj") che_obj::write_file(view->active_mesh(), file); + if(format == "ply") che_ply::write_file(view->active_mesh(), file); cerr << "saved: " << file + "." + format << endl; @@ -429,14 +428,14 @@ bool viewer::menu_save_mesh(viewer * view) bool viewer::menu_zoom_in(viewer * view) { - view->cam.zoomIn(); + view->cam.zoom_in(); return false; } bool viewer::menu_zoom_out(viewer * view) { - view->cam.zoomOut(); + view->cam.zoom_out(); return false; } @@ -479,7 +478,7 @@ bool viewer::menu_bgc_black(viewer * view) bool viewer::invert_orientation(viewer * view) { - view->mesh().invert_orientation(); + view->active_mesh().invert_orientation(); return false; } @@ -536,7 +535,7 @@ bool viewer::set_render_normal_field(viewer * view) bool viewer::set_render_border(viewer * view) { view->render_border = !view->render_border; - if(!view->render_border) view->mesh().selected.clear(); + if(!view->render_border) view->active_mesh().selected.clear(); return false; } @@ -562,7 +561,7 @@ bool viewer::raycasting(viewer * view) gproshan_log(VIEWER); - rt::embree rc({view->mesh()}); + rt::embree rc({view->active_mesh()}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), view->view_mat, view->proj_mat @@ -610,7 +609,7 @@ void viewer::render_gl() if(render_normal_field) { - glProgramUniform1f(shader_normals, shader_normals("length"), mesh().factor); + glProgramUniform1f(shader_normals, shader_normals("length"), active_mesh().factor); glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); @@ -620,7 +619,7 @@ void viewer::render_gl() if(render_gradient_field) { - glProgramUniform1f(shader_gradient, shader_gradient("length"), mesh().factor); + glProgramUniform1f(shader_gradient, shader_gradient("length"), active_mesh().factor); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); @@ -642,7 +641,7 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - rt_embree = new rt::embree({mesh()}); + rt_embree = new rt::embree({active_mesh()}); TOC(time_build_embree); gproshan_log_var(time_build_embree); @@ -669,7 +668,7 @@ void viewer::render_optix() double time_build_optix; TIC(time_build_optix); - rt_optix = new rt::optix({mesh()}); + rt_optix = new rt::optix({active_mesh()}); TOC(time_build_optix); gproshan_log_var(time_build_optix); @@ -707,12 +706,12 @@ void viewer::draw_meshes(shader & program) void viewer::draw_selected_vertices(shader & program) { - if(sphere_translations.size() != mesh().selected.size()) + if(sphere_translations.size() != active_mesh().selected.size()) { - sphere_translations.resize(mesh().selected.size()); + sphere_translations.resize(active_mesh().selected.size()); - for(index_t i = 0; i < mesh().selected.size(); i++) - sphere_translations[i] = mesh()->gt(mesh().selected[i]); + for(index_t i = 0; i < active_mesh().selected.size(); i++) + sphere_translations[i] = active_mesh()->gt(active_mesh().selected[i]); sphere.update_instances_translations(sphere_translations); } @@ -720,17 +719,17 @@ void viewer::draw_selected_vertices(shader & program) if(sphere_translations.size()) { - glViewport(mesh().vx * viewport_width, mesh().vy * viewport_height, viewport_width, viewport_height); + glViewport(active_mesh().vx * viewport_width, active_mesh().vy * viewport_height, viewport_width, viewport_height); sphere.draw(program); } } void viewer::select_border_vertices() { - mesh().selected.clear(); - for(index_t b = 0; b < mesh()->n_borders(); b++) - for_border(he, mesh(), mesh()->bt(b)) - mesh().selected.push_back(mesh()->vt(he)); + active_mesh().selected.clear(); + for(index_t b = 0; b < active_mesh()->n_borders(); b++) + for_border(he, active_mesh(), active_mesh()->bt(b)) + active_mesh().selected.push_back(active_mesh()->vt(he)); } void viewer::pick_vertex(int x, int y) From 6fefbaaeb5bfcbd8bad5402923a0fb866e3c8348 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 21:38:32 +0200 Subject: [PATCH 0264/1018] update travis build status branch raytracing --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 574f7cd1..2cd6b254 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,9 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run + +| `Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | + Install all dependencies and run: mkdir build @@ -25,10 +28,6 @@ In Ubuntu (>= 18.04) you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot -#### Build Status - -##### Ubuntu 18.04 (Bionic) -[![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=master)](https://travis-ci.com/larc/gproshan) ## Contributions From 1e3abb39fb293759563afeae58bde382d057b87a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 5 Jun 2020 21:43:32 +0200 Subject: [PATCH 0265/1018] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cd6b254..7794f8ac 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -| `Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | +| Build Type | Status | +| --- | --- | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | Install all dependencies and run: From dfcabd0fad29bf1c331e0c944a4a742ddf36440f Mon Sep 17 00:00:00 2001 From: Lish Date: Sat, 6 Jun 2020 18:40:32 +0200 Subject: [PATCH 0266/1018] adding new vertices comment --- src/mdict/inpainting.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bafa632d..5b868882 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -413,6 +413,29 @@ void inpainting::init_radial_feature_patches() p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); } +/* + // Adding border points at zero z level + size_t n_extra_vert = 8; + double angle = 360/n_extra_vert; + for(index_t s = 0; s < M; s++) + { + patch & p = patches[s]; + p.xyz.resize(3, p.vertices.size() + n_extra_vert); + + for(index_t j = p.vertices.size(), i = 0; i < n_extra_vert; i++, j++) + { + p.xyz(0, j) = cos(angle); + p.xyz(1, j) = sin(angle); + p.xyz(2, j) = 0; + angle += angle; + } + + p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); + phi_basis->discrete(p.phi, p.xyz); + } +*/ + + bool save_all = true; if(save_all) @@ -590,8 +613,8 @@ real_t inpainting::execute() draw_patches(295); draw_patches(384); - draw_patches(90); - draw_patches(20); + draw_patches(1379); + draw_patches(1486); //draw_patches(400); //draw_patches(500); From c07925b424934d5d339fd6cf44200c5b8b907daf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 6 Jun 2020 22:33:18 +0200 Subject: [PATCH 0267/1018] viewer update viewports --- src/viewer/viewer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ceb8bb55..0a74ad19 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -314,7 +314,7 @@ void viewer::add_mesh(che * p_mesh) const int & rows = m_window_size[n_meshes][0]; const int & cols = m_window_size[n_meshes][1]; - for(int m = 0; m < n_meshes; m++) + for(index_t m = 0; m < n_meshes; m++) { meshes[m].vx = m % cols; meshes[m].vy = rows - (m / cols) - 1; From 94c49b97bbfb07a609ebb8ff5f58e7360b5d1747 Mon Sep 17 00:00:00 2001 From: Lish Date: Mon, 8 Jun 2020 18:01:10 +0200 Subject: [PATCH 0268/1018] computing k nearest median distance --- include/mdict/patch.h | 2 +- src/mdict/inpainting.cpp | 8 ++++---- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 30 ++++++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 769d5411..c84152f4 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -95,7 +95,7 @@ class patch void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); - void compute_avg_distance(); + void compute_avg_distance(che * mesh, vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); bool add_vertex_by_faces(const vertex & c, diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5b868882..c7545b2d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -409,7 +409,7 @@ void inpainting::init_radial_feature_patches() p.transform(); p.scale_xyz(phi_basis->get_radio()); - p.compute_avg_distance(); + p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); } @@ -559,7 +559,7 @@ void inpainting::init_voronoi_patches() p.transform(); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - p.compute_avg_distance(); + p.compute_avg_distance(mesh, patches_map, s); } @@ -613,8 +613,8 @@ real_t inpainting::execute() draw_patches(295); draw_patches(384); - draw_patches(1379); - draw_patches(1486); + draw_patches(100); + //draw_patches(1486); //draw_patches(400); //draw_patches(500); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 37f84943..12a10406 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0.8 * p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 2d3e4ef2..cbf26ad8 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -625,12 +625,37 @@ void patch::save_z(ostream & os) os< & vpatches, const index_t & p) { avg_dist = INFINITY; vector distances; + link_t link; + for(size_t i = 0; i < vertices.size(); i++) - for(size_t j = i+1; j < vertices.size(); j++) + { + const index_t & v = vertices[i]; + mesh->link(link, v); + for(const index_t & he: link) + { + const index_t & u = mesh->vt(he); + + for (auto itp:vpatches[u]) + { + if( itp.first == p) + { + a_vec a = xyz.col(i); + a_vec b = xyz.col(itp.second); + a(2) = 0; + b(2) = 0; + distances.push_back(norm(a - b)); + break; + } + } + } + + } + /* + for(size_t j = i+1; j < vertices.size(); j++) // replace for 1 ring { a_vec a = xyz.col(i); a_vec b = xyz.col(j); @@ -638,6 +663,7 @@ void patch::compute_avg_distance() b(2) = 0; distances.push_back(norm(a - b)); } + */ sort(distances.begin(), distances.end()); size_t n_elem = distances.size(); if(distances.size()%2 ==0) From ae1466a4bb720bdc1196cc696f1b160be8f0f802 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 9 Jun 2020 10:15:10 +0200 Subject: [PATCH 0269/1018] update CMakeLists: c++17 standard, cuda 11 (support for c++17) --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cff398ff..0ac52a1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.12) -project(gproshan VERSION 2.0) +project(gproshan VERSION 3.0) list(APPEND CMAKE_MODULE_PATH "${gproshan_SOURCE_DIR}/cmake") @@ -8,10 +8,10 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(CUDA 10.1) +find_package(CUDA 11) if(CUDA_FOUND) enable_language(CUDA) add_definitions(-DGPROSHAN_CUDA) From e8d41c7133fc46c0cbae660e111547e60d9b0bd0 Mon Sep 17 00:00:00 2001 From: Lish Date: Tue, 9 Jun 2020 10:20:45 +0200 Subject: [PATCH 0270/1018] fixing nquist theorem --- src/mdict/d_mesh.cpp | 5 +++++ src/mdict/inpainting.cpp | 7 ++++++- src/mdict/mdict.cpp | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 4d852409..883c7140 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -255,6 +255,11 @@ real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, vector 2); + if(outlier.n_elem) + gproshan_debug_var(p); + rp.iscale_xyz(radio); rp.itransform(); } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index c7545b2d..5f5a6045 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -597,6 +597,7 @@ real_t inpainting::execute() patches[s].reset_xyz(mesh, patches_map, s, 0); + #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -611,9 +612,12 @@ real_t inpainting::execute() bool *pmask; + draw_patches(295); draw_patches(384); - draw_patches(100); + draw_patches(319); + draw_patches(312); + draw_patches(50); //draw_patches(1486); //draw_patches(400); @@ -625,6 +629,7 @@ real_t inpainting::execute() TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); + } che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 12a10406..b1ed710f 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0.5 * p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); From 5bee61caa32e84f5160a7a496a366ac661a7bfa0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 9 Jun 2020 12:32:42 +0200 Subject: [PATCH 0271/1018] travis add build cuda --- .travis.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.travis.yml b/.travis.yml index c134976d..7072ba35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,11 @@ dist: bionic language: cpp compiler: gcc os: linux + +env: + - CUDA_BIN= + - CUDA_BIN=/usr/local/cuda/bin + addons: apt: sources: @@ -19,9 +24,19 @@ addons: - libglfw3-dev - libglm-dev - cimg-dev + +before_install: + - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin + - sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 + - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub + - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" + - sudo apt update + - sudo apt -y install cuda + script: - export CC=/usr/bin/gcc-8 - export CXX=/usr/bin/g++-8 + - export PATH=${CUDA_BIN}:${PATH} - $CC -v && $CXX -v && cmake --version - mkdir build && cd build - cmake .. && make From 9dda532ec4b72b2e348576bb13804b71830847a3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 9 Jun 2020 13:02:47 +0200 Subject: [PATCH 0272/1018] travis jobs names gproshan cuda --- .travis.yml | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7072ba35..631c4edf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,19 @@ language: cpp compiler: gcc os: linux -env: - - CUDA_BIN= - - CUDA_BIN=/usr/local/cuda/bin +jobs: + include: + - name: gproshan + env: CUDA_BIN= + - name: gproshan_cuda + env: CUDA_BIN=/usr/local/cuda/bin + before_install: + - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin + - sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 + - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub + - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" + - sudo apt update + - sudo apt -y install cuda addons: apt: @@ -25,19 +35,11 @@ addons: - libglm-dev - cimg-dev -before_install: - - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin - - sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 - - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub - - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" - - sudo apt update - - sudo apt -y install cuda - script: - export CC=/usr/bin/gcc-8 - export CXX=/usr/bin/g++-8 - export PATH=${CUDA_BIN}:${PATH} - $CC -v && $CXX -v && cmake --version - mkdir build && cd build - - cmake .. && make + - cmake .. && make -j 4 From 08d4b121fb1ef964e5aca799805241cc6c54e1bc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 9 Jun 2020 13:17:42 +0200 Subject: [PATCH 0273/1018] update readme, fix travis syntax --- .travis.yml | 4 ++-- README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 631c4edf..ccb4758d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,9 @@ os: linux jobs: include: - name: gproshan - env: CUDA_BIN= + env: CUDA_BIN= - name: gproshan_cuda - env: CUDA_BIN=/usr/local/cuda/bin + env: CUDA_BIN=/usr/local/cuda/bin before_install: - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin - sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 diff --git a/README.md b/README.md index 7794f8ac..fe79e7c4 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 8.3, cuda >= 10.1, cmake >= 3.12, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot +g++ >= 8.4, cuda >= 11.0, cmake >= 3.12, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot In Ubuntu (>= 18.04) you can install them with: From edd1cb3c47c97c160d1fd04927e3bdadbcf97710 Mon Sep 17 00:00:00 2001 From: Lish Date: Tue, 9 Jun 2020 19:26:18 +0200 Subject: [PATCH 0274/1018] adding vertices to small patches --- include/mdict/patch.h | 6 +-- src/mdict/inpainting.cpp | 23 ---------- src/mdict/patch.cpp | 90 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 32 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index c84152f4..56505223 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -3,11 +3,11 @@ #include "include.h" #include "che.h" - -#include #include "include_arma.h" #include "geodesics.h" +#include +#include #include #ifdef Success @@ -28,7 +28,7 @@ class dictionary; typedef function fmask_t; typedef function fmask_local_t; -typedef std::vector > vpatches_t; +typedef std::map vpatches_t; /// class patch diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5f5a6045..1a62f69a 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -413,29 +413,6 @@ void inpainting::init_radial_feature_patches() p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); } -/* - // Adding border points at zero z level - size_t n_extra_vert = 8; - double angle = 360/n_extra_vert; - for(index_t s = 0; s < M; s++) - { - patch & p = patches[s]; - p.xyz.resize(3, p.vertices.size() + n_extra_vert); - - for(index_t j = p.vertices.size(), i = 0; i < n_extra_vert; i++, j++) - { - p.xyz(0, j) = cos(angle); - p.xyz(1, j) = sin(angle); - p.xyz(2, j) = 0; - angle += angle; - } - - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); - } -*/ - - bool save_all = true; if(save_all) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index cbf26ad8..02a01bd1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -140,8 +140,6 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N area += area_face; proj_area += proj_area_face; - - N.push_back(min_he); return added; } @@ -353,15 +351,20 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & //j: local index //i: global index //if(vpatches[vertices[i]].size() == 0) - vpatches[vertices[i]].push_back({p, j++}); + //vpatches[vertices[i]].push_back({p, j++}); + vpatches[vertices[i]][p] = j++; } } + + + } void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { - size_t m = vertices.size(); + size_t extra = 36 > vertices.size() ? 36 - vertices.size() : 0; + size_t m = vertices.size() + extra; if(mask) { m = 0; @@ -374,7 +377,8 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector dis(0, 1); + while(extra) + { + gproshan_debug_var(extra); + // add new vertices + // create a random point + double a = abs(dis(gen)) * 2 * PI; + double r = abs(dis(gen)); + a_vec np; + np(0)= r * cos(a); + np(1) = r * sin(a); + np(2) = 0; + // find the closest point + index_t min_v; + double min_d = INFINITY; + for(index_t v: vertices) + { + a_vec aux = xyz.col(vpatches[v][p]); + //vertex aux(v.x, v.y, 0); + if( norm(np-aux) < min_d) + { + min_d =norm(np-aux); + min_v = v; + } + } + + // forstar to find closest trinagle + index_t ia,ib; + a_vec pnorm; + a_mat abc(3,3); + for_star(he, mesh, min_v) + { + //discard triangles outside the patch + ia = vpatches[mesh->vt(next(he))][p]; + ib = vpatches[mesh->vt(prev(he))][p]; + if(vpatches[ia].find(p)!= vpatches[ia].end() || vpatches[ib].find(p)!= vpatches[ib].end() ) + { + arma::uvec xi = { vpatches[min_v][p], + vpatches[ia][p], + vpatches[ib][p] + }; + + abc = xyz.cols(xi); + + // find the normal n = (A,B,C) with x product + pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); + // z = (-Ax -By + < n, po> ) / C + double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); + double al, be, ga; + // project the vectors + al = arma::dot(abc.col(1) - abc.col(0), np - abc.col(0) ); + be = arma::dot(abc.col(2) - abc.col(0), np - abc.col(0) ); + ga = 1 - al - ga; + + // verify if lies inside this trinagle + if((al + be + ga) == 1 && al < 1 && al >= 0 && be < 1 && be >= 0 && ga < 1 && ga >= 0 ) + { + np(2) = z; + xyz(0, j) = np(0); + xyz(1, j) = np(1); + xyz(2, j) = np(2); + extra --; + j++; + break; + } + + } + } } } From f010cd985e2c45a6c8a76127f42a0a1648674e2f Mon Sep 17 00:00:00 2001 From: Lish Date: Tue, 9 Jun 2020 21:56:42 +0200 Subject: [PATCH 0275/1018] fixing errors density patch --- src/mdict/patch.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 02a01bd1..a32486ea 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -394,14 +394,14 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector dis(0, 1); - while(extra) + while(extra--) { gproshan_debug_var(extra); // add new vertices // create a random point double a = abs(dis(gen)) * 2 * PI; double r = abs(dis(gen)); - a_vec np; + a_vec np(3); np(0)= r * cos(a); np(1) = r * sin(a); np(2) = 0; @@ -418,23 +418,21 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorvt(next(he))][p]; - ib = vpatches[mesh->vt(prev(he))][p]; - if(vpatches[ia].find(p)!= vpatches[ia].end() || vpatches[ib].find(p)!= vpatches[ib].end() ) + vpatches_t & ma = vpatches[mesh->vt(next(he))]; + vpatches_t & mb = vpatches[mesh->vt(prev(he))]; + + if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { - arma::uvec xi = { vpatches[min_v][p], - vpatches[ia][p], - vpatches[ib][p] - }; - + arma::uvec xi = {vpatches[min_v][p], ma[p], mb[p]}; abc = xyz.cols(xi); // find the normal n = (A,B,C) with x product @@ -450,6 +448,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector= 0 && be < 1 && be >= 0 && ga < 1 && ga >= 0 ) { + gproshan_debug_var(z); np(2) = z; xyz(0, j) = np(0); xyz(1, j) = np(1); @@ -461,7 +460,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector Date: Thu, 11 Jun 2020 21:26:57 +0200 Subject: [PATCH 0276/1018] adding extra vertices function --- include/mdict/patch.h | 2 + src/mdict/inpainting.cpp | 2 + src/mdict/patch.cpp | 132 +++++++++++++++++++++++---------------- 3 files changed, 81 insertions(+), 55 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 56505223..69a4ee9f 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef Success #undef Success @@ -90,6 +91,7 @@ class patch const index_t & p, const fmask_t & mask = nullptr ); + void add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p); const a_vec normal(); bool is_covered( bool * covered); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 1a62f69a..a833f420 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -409,6 +409,8 @@ void inpainting::init_radial_feature_patches() p.transform(); p.scale_xyz(phi_basis->get_radio()); + // adding add extra function + p.add_extra_xyz_disjoint(mesh,patches_map, s); p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a32486ea..0b855b86 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -329,7 +329,6 @@ void patch::itransform() void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); - if(mask) { @@ -359,44 +358,24 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } - -void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) +double area_tri(double x1, double y1, double x2, double y2, double x3, double y3) +{ + return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); +} +void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) { - size_t extra = 36 > vertices.size() ? 36 - vertices.size() : 0; - size_t m = vertices.size() + extra; - if(mask) - { - m = 0; - for(index_t i = 0; i < vertices.size(); i++) - if(mask(i)) { dist[vertices[i] ] = float(p + 1) / M; m++; } else {dist[vertices[i] ] = INFINITY; }; - /*gproshan_debug(number vertices considered); - gproshan_debug_var(m); - gproshan_debug(number vertices masked); - gproshan_debug_var(vertices.size() - m);*/ - } - - xyz.set_size(3, m); - index_t j = 0; - for(index_t i = 0; i < vertices.size(); i++) - { - if(!mask || mask(i)) - { - const vertex & v = mesh->gt(vertices[i]); - xyz(0, j) = v.x; - xyz(1, j) = v.y; - xyz(2, j) = v.z; - //vpatches[vertices[i]].push_back({p, j++}); - vpatches[vertices[i]][p] = j++; - } - } + size_t n_min_vert = 23; + size_t m = std::max (vertices.size(), n_min_vert); + size_t j = vertices.size(); std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() std::uniform_real_distribution<> dis(0, 1); - while(extra--) + + while(j < m && vertices.size() < 13) { - gproshan_debug_var(extra); + // add new vertices // create a random point double a = abs(dis(gen)) * 2 * PI; @@ -405,22 +384,24 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorvt(next(he))]; vpatches_t & mb = vpatches[mesh->vt(prev(he))]; + + //gproshan_debug_var(mesh->vt(prev(he))); + //gproshan_debug_var(mesh->vt(next(he))); + //gproshan_debug_var(mesh->vt(he)); + if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { arma::uvec xi = {vpatches[min_v][p], ma[p], mb[p]}; abc = xyz.cols(xi); - - // find the normal n = (A,B,C) with x product - pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); - // z = (-Ax -By + < n, po> ) / C - double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); - double al, be, ga; - // project the vectors - al = arma::dot(abc.col(1) - abc.col(0), np - abc.col(0) ); - be = arma::dot(abc.col(2) - abc.col(0), np - abc.col(0) ); - ga = 1 - al - ga; - - // verify if lies inside this trinagle - if((al + be + ga) == 1 && al < 1 && al >= 0 && be < 1 && be >= 0 && ga < 1 && ga >= 0 ) + + //gproshan_debug_var(abc); + //gproshan_debug_var(np); + // verify if this is inside a triangle + + double A = area_tri(abc(0,0), abc(0,1), abc(1,0), abc(1,1), abc(2,0), abc(2,1) ); + double A1 = area_tri(np(0), np(1), abc(1,0), abc(1,1), abc(2,0), abc(2,1) ); + double A2 = area_tri(abc(0,0), abc(0,1), np(0), np(1), abc(2,0), abc(2,1) ); + double A3 = area_tri(abc(0,0), abc(0,1), abc(1,0), abc(1,1), np(0), np(1) ); + + if( abs(A - (A1 + A2 + A3)) < 0.00001) { - gproshan_debug_var(z); - np(2) = z; - xyz(0, j) = np(0); - xyz(1, j) = np(1); - xyz(2, j) = np(2); - extra --; - j++; - break; - } + pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); + // z = (-Ax -By + < n, po> ) / C + + double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); + //z = 0; + if(!isnan(z)) //z = 0; + { + xyz(0, j) = np(0); + xyz(1, j) = np(1); + xyz(2, j) = z; + j++; + break; + } + + } } } } } +void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) +{ + size_t m = vertices.size(); + if(mask) + { + m = 0; + for(index_t i = 0; i < vertices.size(); i++) + if(mask(i)) { dist[vertices[i] ] = float(p + 1) / M; m++; } else {dist[vertices[i] ] = INFINITY; }; + /*gproshan_debug(number vertices considered); + gproshan_debug_var(m); + gproshan_debug(number vertices masked); + gproshan_debug_var(vertices.size() - m);*/ + } + size_t n_min_vert = 23; + m = std::max (vertices.size(), n_min_vert); + + xyz.set_size(3, m); + index_t j = 0; + for(index_t i = 0; i < vertices.size(); i++) + { + if(!mask || mask(i)) + { + const vertex & v = mesh->gt(vertices[i]); + xyz(0, j) = v.x; + xyz(1, j) = v.y; + xyz(2, j) = v.z; + //vpatches[vertices[i]].push_back({p, j++}); + vpatches[vertices[i]][p] = j++; + } + } + +} void patch::scale_xyz(const real_t & radio_f) { From 52c30c38b79b8451d419361897bd6faa8bd21c65 Mon Sep 17 00:00:00 2001 From: Lish Date: Fri, 12 Jun 2020 09:54:07 +0200 Subject: [PATCH 0277/1018] density fxiing projection function --- src/mdict/patch.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 0b855b86..a2a8c001 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -420,6 +420,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { arma::uvec xi = {vpatches[min_v][p], ma[p], mb[p]}; abc = xyz.cols(xi); + abc.row(2).zeros(); //gproshan_debug_var(abc); //gproshan_debug_var(np); @@ -433,19 +434,24 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co if( abs(A - (A1 + A2 + A3)) < 0.00001) { - pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); + //pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); // z = (-Ax -By + < n, po> ) / C - double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); + //double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); //z = 0; - if(!isnan(z)) //z = 0; + // alpha, beta = inv (ab) x + // x = a,b * (alpha, beta) + a_mat proj_ab = abc.submat(0,1,1,2) - abc.submat(0,0,1,0); + a_vec coeff = arma::inv( proj_ab ) * np.head(2); + /*if(!isnan(z)) //z = 0; { + xyz.col(j) = xyz(0, j) = np(0); xyz(1, j) = np(1); xyz(2, j) = z; j++; break; - } + }*/ } } From 30a8fb1232f4ecbf6c3a6bf392b1fe4e2c95d030 Mon Sep 17 00:00:00 2001 From: Lish Date: Sat, 13 Jun 2020 13:31:31 +0200 Subject: [PATCH 0278/1018] getting z height of new random point --- include/mdict/patch.h | 1 + src/mdict/patch.cpp | 41 ++++++++++++++++++----------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 69a4ee9f..bd0a1f68 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -42,6 +42,7 @@ class patch a_mat phi; double avg_dist; // Average distance betweenn points in a patch real_t radio; // radio of a patch + size_t min_nv; public: static size_t expected_nv; ///< Expected number of patch vertices. diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a2a8c001..7e0dd72d 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -229,6 +229,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { radio = -INFINITY; + min_nv = 23; normal_fit_directions(mesh, v); @@ -365,15 +366,15 @@ double area_tri(double x1, double y1, double x2, double y2, double x3, double y3 } void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) { - size_t n_min_vert = 23; - size_t m = std::max (vertices.size(), n_min_vert); + + size_t m = std::max (vertices.size(), min_nv); size_t j = vertices.size(); std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() std::uniform_real_distribution<> dis(0, 1); - while(j < m && vertices.size() < 13) + while(j < m ) { // add new vertices @@ -433,25 +434,19 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co if( abs(A - (A1 + A2 + A3)) < 0.00001) { + a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); + np -= abc.col(0); + + a_vec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); + np = proj_abc * coef + abc.col(0); + + xyz(0, j) = np(0); + xyz(1, j) = np(1); + xyz(2, j) = np(2); + j++; + break; - //pnorm = arma::cross(abc.col(1) - abc.col(0), abc.col(2) - abc.col(0) ); - // z = (-Ax -By + < n, po> ) / C - - //double z = (-pnorm(0)*np(0) -pnorm(1)*np(1) + arma::dot(pnorm,np) )/ pnorm(2); - //z = 0; - // alpha, beta = inv (ab) x - // x = a,b * (alpha, beta) - a_mat proj_ab = abc.submat(0,1,1,2) - abc.submat(0,0,1,0); - a_vec coeff = arma::inv( proj_ab ) * np.head(2); - /*if(!isnan(z)) //z = 0; - { - xyz.col(j) = - xyz(0, j) = np(0); - xyz(1, j) = np(1); - xyz(2, j) = z; - j++; - break; - }*/ + /*if(!isnan(z)) //z = 0;*/ } } @@ -472,8 +467,8 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector Date: Sat, 13 Jun 2020 20:47:45 +0200 Subject: [PATCH 0279/1018] adding points to increase density --- include/mdict/inpainting.h | 2 +- src/mdict/inpainting.cpp | 8 ++++---- src/mdict/patch.cpp | 30 ++++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 0741f17f..0a78249e 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -19,7 +19,7 @@ class inpainting : public dictionary { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, - const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 50, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); + const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 0, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); virtual ~inpainting() = default; real_t execute(); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a833f420..a701437d 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -564,7 +564,7 @@ real_t inpainting::execute() gproshan_debug_var(d_time); string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); save_alpha(f_alpha); - + draw_patches(76); //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) { @@ -592,11 +592,11 @@ real_t inpainting::execute() bool *pmask; - draw_patches(295); + /*draw_patches(295); draw_patches(384); draw_patches(319); - draw_patches(312); - draw_patches(50); + draw_patches(312);*/ + //draw_patches(76); //draw_patches(1486); //draw_patches(400); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 7e0dd72d..c03032d0 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -229,7 +229,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { radio = -INFINITY; - min_nv = 23; + min_nv = 128; normal_fit_directions(mesh, v); @@ -368,12 +368,17 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { size_t m = std::max (vertices.size(), min_nv); + gproshan_debug_var(min_nv); + gproshan_debug_var(vertices.size()); + gproshan_debug_var(m); size_t j = vertices.size(); std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() std::uniform_real_distribution<> dis(0, 1); + //if(p == 76) gproshan_debug_var(xyz); + while(j < m ) { @@ -421,7 +426,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { arma::uvec xi = {vpatches[min_v][p], ma[p], mb[p]}; abc = xyz.cols(xi); - abc.row(2).zeros(); + //abc.row(2).zeros(); //gproshan_debug_var(abc); //gproshan_debug_var(np); @@ -434,18 +439,27 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co if( abs(A - (A1 + A2 + A3)) < 0.00001) { + a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); a_vec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); np = proj_abc * coef + abc.col(0); - xyz(0, j) = np(0); - xyz(1, j) = np(1); - xyz(2, j) = np(2); - j++; - break; - + if(!isnan(np(2))) + { + xyz(0, j) = np(0); + xyz(1, j) = np(1); + xyz(2, j) = np(2); + j++; + /*if(p == 76) + { + gproshan_debug_var(np); + gproshan_debug_var(abc); + gproshan_debug_var(coef); + }*/ + break; + } /*if(!isnan(z)) //z = 0;*/ } From ca6ce473064ed8be9afbc72e6e9c9b63a1a9f535 Mon Sep 17 00:00:00 2001 From: Lish Date: Sun, 14 Jun 2020 10:57:58 +0200 Subject: [PATCH 0280/1018] fixing adding points --- src/mdict/inpainting.cpp | 3 +++ src/mdict/patch.cpp | 58 +++++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a701437d..bcb947aa 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -564,7 +564,10 @@ real_t inpainting::execute() gproshan_debug_var(d_time); string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); save_alpha(f_alpha); + + draw_patches(76); + draw_patches(384); //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index c03032d0..61dd31ca 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -229,7 +229,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { radio = -INFINITY; - min_nv = 128; + min_nv = 256; normal_fit_directions(mesh, v); @@ -368,9 +368,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { size_t m = std::max (vertices.size(), min_nv); - gproshan_debug_var(min_nv); - gproshan_debug_var(vertices.size()); - gproshan_debug_var(m); + size_t j = vertices.size(); std::random_device rd; //Will be used to obtain a seed for the random number engine @@ -384,12 +382,10 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co // add new vertices // create a random point - double a = abs(dis(gen)) * 2 * PI; - double r = abs(dis(gen)); - a_vec np(3); - np(0)= r * cos(a); - np(1) = r * sin(a); - np(2) = 0; + real_t a = abs(dis(gen)) * 2 * PI; + real_t r = abs(dis(gen)); + a_vec np = { r * cos(a), r * sin(a), 0 }; + //gproshan_debug_var(np); // find the closest point index_t min_v; @@ -398,48 +394,48 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { a_vec aux = xyz.col(vpatches[v][p]); aux(2) = 0; - //vertex aux(v.x, v.y, 0); - if( norm(np-aux) < min_d) + + if(norm(np - aux) < min_d) { - min_d =norm(np-aux); - //gproshan_debug_var(min_d); + min_d = norm(np - aux); min_v = v; } } // forstar to find closest trinagle - a_vec pnorm; a_mat abc(3,3); - //gproshan_debug_var(min_v); for_star(he, mesh, min_v) { //discard triangles outside the patch vpatches_t & ma = vpatches[mesh->vt(next(he))]; vpatches_t & mb = vpatches[mesh->vt(prev(he))]; - //gproshan_debug_var(mesh->vt(prev(he))); - //gproshan_debug_var(mesh->vt(next(he))); - //gproshan_debug_var(mesh->vt(he)); - - if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { - arma::uvec xi = {vpatches[min_v][p], ma[p], mb[p]}; + arma::uvec xi = { vpatches[min_v][p], ma[p], mb[p] }; abc = xyz.cols(xi); - //abc.row(2).zeros(); - //gproshan_debug_var(abc); + if(p == 76) gproshan_debug_var(xi); //gproshan_debug_var(np); // verify if this is inside a triangle - double A = area_tri(abc(0,0), abc(0,1), abc(1,0), abc(1,1), abc(2,0), abc(2,1) ); - double A1 = area_tri(np(0), np(1), abc(1,0), abc(1,1), abc(2,0), abc(2,1) ); - double A2 = area_tri(abc(0,0), abc(0,1), np(0), np(1), abc(2,0), abc(2,1) ); - double A3 = area_tri(abc(0,0), abc(0,1), abc(1,0), abc(1,1), np(0), np(1) ); - - if( abs(A - (A1 + A2 + A3)) < 0.00001) + double A = area_tri(abc(0,0), abc(1,0), abc(0,1), abc(1,1), abc(0,2), abc(1,2) ); + double A1 = area_tri(np(0), np(1), abc(0,1), abc(1,1), abc(0,2), abc(1,2) ); + double A2 = area_tri(abc(0,0), abc(1,0), np(0), np(1), abc(0,2), abc(1,2) ); + double A3 = area_tri(abc(0,0), abc(1,0), abc(0,1), abc(1,1), np(0), np(1) ); + + if(p == 76) { - + gproshan_debug_var(abc); + gproshan_debug_var(A); + gproshan_debug_var(A1); + gproshan_debug_var(A2); + gproshan_debug_var(A3); + } + + if( abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) + { + if(p == 76) gproshan_debug(FIND TRIANG); a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); From e21a8de98313581e63718746399f2e21d03777c5 Mon Sep 17 00:00:00 2001 From: Lish Date: Sun, 14 Jun 2020 11:06:24 +0200 Subject: [PATCH 0281/1018] fixing and not using nquist --- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index b1ed710f..d0614af4 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= p.avg_dist) //2.5* if it ismin + if( phi_basis->get_frequency(i) >= 0*p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 61dd31ca..c445a08d 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -415,7 +415,6 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co arma::uvec xi = { vpatches[min_v][p], ma[p], mb[p] }; abc = xyz.cols(xi); - if(p == 76) gproshan_debug_var(xi); //gproshan_debug_var(np); // verify if this is inside a triangle @@ -424,18 +423,10 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co double A2 = area_tri(abc(0,0), abc(1,0), np(0), np(1), abc(0,2), abc(1,2) ); double A3 = area_tri(abc(0,0), abc(1,0), abc(0,1), abc(1,1), np(0), np(1) ); - if(p == 76) - { - gproshan_debug_var(abc); - gproshan_debug_var(A); - gproshan_debug_var(A1); - gproshan_debug_var(A2); - gproshan_debug_var(A3); - } + if( abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) { - if(p == 76) gproshan_debug(FIND TRIANG); a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); From 6169e40b838d1a5c8a7c546d37e20ef09f50eefc Mon Sep 17 00:00:00 2001 From: Lish Date: Tue, 16 Jun 2020 12:33:17 +0200 Subject: [PATCH 0282/1018] adding compression ratio --- src/app_viewer.cpp | 4 ++-- src/mdict/inpainting.cpp | 8 +++++++- src/mdict/patch.cpp | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 928e1f23..54a9070d 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -649,8 +649,8 @@ bool app_viewer::process_mask(viewer * p_view) static int avg_p = 36; static int percentage = 0; static float delta = PI/6; - static float sum_thres = 1.001; - static float area_thres = 0.001; + static float sum_thres = 1.01; + static float area_thres = 0.005; ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bcb947aa..bc005511 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -566,8 +566,10 @@ real_t inpainting::execute() save_alpha(f_alpha); - draw_patches(76); + draw_patches(295); draw_patches(384); + draw_patches(319); + draw_patches(312); //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) { @@ -611,6 +613,10 @@ real_t inpainting::execute() TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); + arma::uvec non_zero = find( abs(alpha) > 0.00001); + gproshan_debug_var(non_zero.size()); + real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); + gproshan_debug_var(ratio); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index c445a08d..b5be1120 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -229,7 +229,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { radio = -INFINITY; - min_nv = 256; + min_nv = 128; normal_fit_directions(mesh, v); From 4776be632c5e21b223829ae4bf8e799dc2f4c995 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 10:16:26 +0200 Subject: [PATCH 0283/1018] save mesh imgui --- src/app_viewer.cpp | 2 +- src/viewer/viewer.cpp | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 9b72b91a..727e16be 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -104,7 +104,7 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_N, {"N", "Noise", process_noise}); add_process(GLFW_KEY_COMMA, {"COMMA", "Black noise", process_black_noise}); add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); - add_process(GLFW_KEY_PERIOD, {"PERIOD", "Delete vertices", process_delete_vertices}); + add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0a74ad19..c015b456 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -410,20 +410,25 @@ bool viewer::menu_reset_mesh(viewer * view) bool viewer::menu_save_mesh(viewer * view) { - gproshan_log(APP_VIEWER); - - gproshan_log(format: [off obj ply]); - - string format; cin >> format; - string file = view->active_mesh()->filename() + "_new"; - - if(format == "off") che_off::write_file(view->active_mesh(), file); - if(format == "obj") che_obj::write_file(view->active_mesh(), file); - if(format == "ply") che_ply::write_file(view->active_mesh(), file); + static char file[128] = "copy"; + static int format = 0; + + ImGui::InputText("file", file, sizeof(file)); + ImGui::Combo("format", &format, ".off\0.obj\0.ply"); - cerr << "saved: " << file + "." + format << endl; + if(ImGui::Button("Save")) + { + if(format == 0) che_off::write_file(view->active_mesh(), file); + if(format == 1) che_obj::write_file(view->active_mesh(), file); + if(format == 2) che_ply::write_file(view->active_mesh(), file); + } - return false; + return true; +} + +bool viewer::menu_save_view(viewer * view) +{ + return true; } bool viewer::menu_zoom_in(viewer * view) From b75340f10bd4f6b7d485362f37663d880afffb8e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 12:11:57 +0200 Subject: [PATCH 0284/1018] save load view (camera state) --- include/viewer/camera.h | 11 ++++++++--- include/viewer/viewer.h | 1 + src/viewer/camera.cpp | 6 +++--- src/viewer/viewer.cpp | 32 ++++++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/viewer/camera.h b/include/viewer/camera.h index f73e00c1..0484eb77 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -12,24 +12,29 @@ namespace gproshan { class camera { - public: + private: quaternion pClick; quaternion pDrag; quaternion pLast; quaternion rLast; quaternion momentum; int tLast; - double zoom, vZoom; + double vZoom; + + public: + double zoom; public: camera(); - quaternion clickToSphere(int x, int y); void mouse(int button, int state, int x, int y); void motion(int x, int y); void idle(); void zoom_in(); void zoom_out(); quaternion current_rotation() const; + + private: + quaternion click_to_sphere(int x, int y); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index eda8279f..b48d3f68 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -150,6 +150,7 @@ class viewer // menu functions static bool menu_help(viewer * view); + static bool menu_save_load_view(viewer * view); static bool menu_reset_mesh(viewer * view); static bool menu_save_mesh(viewer * view); static bool menu_zoom_in(viewer * view); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 7df410fd..229d114d 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -20,7 +20,7 @@ camera::camera() zoom(1.) {} -quaternion camera::clickToSphere(int x, int y) +quaternion camera::click_to_sphere(int x, int y) { GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); @@ -57,7 +57,7 @@ void camera::mouse(int , int state, int x, int y) { if(state == GLFW_PRESS) { - pClick = pDrag = pLast = clickToSphere(x, y); + pClick = pDrag = pLast = click_to_sphere(x, y); momentum = 1.; } if(state == GLFW_RELEASE) @@ -83,7 +83,7 @@ void camera::motion(int x, int y) { tLast = clock(); pLast = pDrag; - pDrag = clickToSphere(x, y); + pDrag = click_to_sphere(x, y); } void camera::idle() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c015b456..d94af8cc 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -239,6 +240,7 @@ void viewer::init_menus() { sub_menus.push_back("Viewer"); add_process(GLFW_KEY_F1, {"F1", "Help", menu_help}); + add_process(GLFW_KEY_PERIOD, {"PERIOD", "Save/Load view", menu_save_load_view}); add_process(GLFW_KEY_UP, {"UP", "Zoom in", menu_zoom_in}); add_process(GLFW_KEY_DOWN, {"DOWN", "Zoom out", menu_zoom_out}); add_process(GLFW_KEY_RIGHT, {"RIGHT", "Background color inc", menu_bgc_inc}); @@ -396,6 +398,29 @@ bool viewer::menu_help(viewer * view) return false; } +bool viewer::menu_save_load_view(viewer * view) +{ + static char file[128] = "view"; + + ImGui::InputText("file", file, sizeof(file)); + + if(ImGui::Button("Save")) + { + ofstream os(tmp_file_path("views/" + file)); + os << view->cam.current_rotation() << endl; + os.close(); + } + + ImGui::SameLine(); + + if(ImGui::Button("Load")) + { + } + + return true; +} + + bool viewer::menu_reset_mesh(viewer * view) { view->active_mesh().selected.clear(); @@ -414,7 +439,7 @@ bool viewer::menu_save_mesh(viewer * view) static int format = 0; ImGui::InputText("file", file, sizeof(file)); - ImGui::Combo("format", &format, ".off\0.obj\0.ply"); + ImGui::Combo("format", &format, ".off\0.obj\0.ply\0\0"); if(ImGui::Button("Save")) { @@ -426,11 +451,6 @@ bool viewer::menu_save_mesh(viewer * view) return true; } -bool viewer::menu_save_view(viewer * view) -{ - return true; -} - bool viewer::menu_zoom_in(viewer * view) { view->cam.zoom_in(); From 8c7b48bf48f793809a1520db17624f7bc880d313 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 14:19:58 +0200 Subject: [PATCH 0285/1018] load normals noff --- include/che.h | 2 +- src/che_off.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/che.h b/include/che.h index 30ae2adc..ba6ca0d4 100644 --- a/include/che.h +++ b/include/che.h @@ -47,7 +47,7 @@ class che index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - real_t * VC = nullptr; ///< vertex color : v -> color(v) + real_t * VC = nullptr; ///< vertex color : v -> color(v) bool manifold = true; diff --git a/src/che_off.cpp b/src/che_off.cpp index 9e7bfa88..a50f70a0 100644 --- a/src/che_off.cpp +++ b/src/che_off.cpp @@ -37,17 +37,17 @@ void che_off::read_file(const string & file) is >> soff; is >> n_v >> n_f >> v; init(n_v, n_f); + + if(soff[0] == 'N') VN = new vertex[n_vertices_]; int r, g, b, a; for(index_t i = 0; i < n_vertices_; i++) { is >> GT[i]; - if(soff[0] == 'C') // COFF file, ignore RGBA - is >> r >> g >> b >> a; - if(soff[0] == 'N') // NOFF file, ignore normals - is >> r >> g >> b; + if(soff[0] == 'C') is >> r >> g >> b >> a; // ignore RGB for now + if(soff[0] == 'N') is >> VN[i]; } - + index_t he = 0; for(index_t i = 0; i < n_faces_; i++) { From d4b6919ce5384677d81a5392971bd85636c2d9cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 14:51:14 +0200 Subject: [PATCH 0286/1018] rt: embree render pointcloud required normals --- src/raytracing/rt_embree.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index c6b5ed7d..ec062bbc 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -101,7 +101,7 @@ index_t embree::add_mesh(const che * mesh) index_t embree::add_point_cloud(const che * mesh) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_DISC_POINT); + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, @@ -109,21 +109,19 @@ index_t embree::add_point_cloud(const che * mesh) 4 * sizeof(float), mesh->n_vertices() ); -/* + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), mesh->n_vertices() ); - */ + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001); - - vertex n = mesh->normal(i); - // normal[i] = glm::vec3(n.x, n.y, n.z); + normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); } rtcCommitGeometry(geom); From 228ab62e6cf3b541f1a67a67fc6bd70b40f5bf56 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 15:53:15 +0200 Subject: [PATCH 0287/1018] shading normal = geometry normal with for point clouds --- include/raytracing/rt_embree.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index f061925f..2d6ad95f 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -60,7 +60,11 @@ class embree : public raytracing const glm::vec3 shading_normal(const che * mesh) const { - vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + vertex n; + if(mesh->n_faces()) + n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + else + n = { hit.Ng_x, hit.Ng_y, hit.Ng_z }; return glm::normalize(glm::vec3(n.x, n.y, n.z)); } From 2d7c5d14ccd20cbff60436ff21104d7789e415c5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 16:38:16 +0200 Subject: [PATCH 0288/1018] transparency effect --- include/raytracing/rt_embree.h | 4 +-- src/raytracing/rt_embree.cpp | 45 +++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 2d6ad95f..fa2ba401 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -21,7 +21,7 @@ class embree : public raytracing { ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), const glm::vec3 & direction = glm::vec3(0.0f), - float near = 0.0f, + float near = 1e-5f, float far = FLT_MAX) { ray.org_x = origin.x; @@ -96,7 +96,7 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); - glm::vec4 li(const ray_hit & r, const glm::vec3 & light, const bool & flat); + glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index ec062bbc..29569979 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -132,27 +132,44 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -glm::vec4 embree::li(const ray_hit & r, const glm::vec3 & light, const bool & flat) +glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) { - glm::vec3 color(0.6, 0.8, 1.0); + const glm::vec3 color(0.6, 0.8, 1.0); + const float max_tfar = 8; + + float total_tfar = 0; + float tfar = r.ray.tfar; + glm::vec4 out_li = glm::vec4(0); - float dist_light = glm::length(light - r.position()); - float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff + float dist_light, falloff, dot_wi_normal; + glm::vec3 wi; + + while(tfar < max_tfar) + { + total_tfar += tfar; + + dist_light = glm::length(light - r.position()); + falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff - glm::vec3 wi = normalize(light - r.position()); + wi = normalize(light - r.position()); - float dot_wi_normal = flat ? glm::dot(wi, r.geometry_normal()) + dot_wi_normal = flat ? glm::dot(wi, r.geometry_normal()) : glm::dot(wi, r.shading_normal(geomID_mesh[r.hit.geomID])); - if(dot_wi_normal < 0) - dot_wi_normal = -dot_wi_normal; - - ray_hit ro(r.position() + 1e-5f * wi, wi); + if(dot_wi_normal < 0) dot_wi_normal = -dot_wi_normal; + + out_li += tfar * glm::vec4(color * falloff * dot_wi_normal, 1); + + ray_hit ro(r.position(), wi); + if(occluded(ro)) out_li *= .5f; + + r = ray_hit(r.position(), r.dir()); + if(!intersect(r)) break; + + tfar = r.ray.tfar + total_tfar; + } - if(occluded(ro)) - return .5f * glm::vec4(color * falloff * dot_wi_normal, 1); - - return glm::vec4(color * falloff * dot_wi_normal, 1); + return out_li / total_tfar; } bool embree::intersect(ray_hit & r) From 6da8434a57e919993fba45071fc3947f8b5aa910 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 18:46:07 +0200 Subject: [PATCH 0289/1018] rt: embree pointcloud_hit evaluation --- include/raytracing/rt_embree.h | 19 ++++------ src/raytracing/rt_embree.cpp | 65 ++++++++++++++++++++++++---------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index fa2ba401..21636eb9 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -53,19 +53,12 @@ class embree : public raytracing return {ray.dir_x, ray.dir_y, ray.dir_z}; } - const glm::vec3 geometry_normal() const + const glm::vec3 normal(const che * mesh, const bool & flat = false) const { - return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - } - - const glm::vec3 shading_normal(const che * mesh) const - { - vertex n; - if(mesh->n_faces()) - n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - else - n = { hit.Ng_x, hit.Ng_y, hit.Ng_z }; - + if(flat || !mesh->n_faces()) + return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); + + vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::normalize(glm::vec3(n.x, n.y, n.z)); } @@ -96,6 +89,8 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); + void pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r); + glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 29569979..1b52bbf6 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -132,44 +132,71 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) +void embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) +{ + int n = 0; + + r = ray_hit(r.position(), r.dir()); + while(intersect(r) && r.ray.tfar < 0.001) + { + n++; + position += r.position(); + normal += r.normal(geomID_mesh[r.hit.geomID], true); + r = ray_hit(r.position(), r.dir()); + } + + position /= n; + normal /= n; +} + +glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal) { const glm::vec3 color(0.6, 0.8, 1.0); + + const glm::vec3 wi = normalize(light - position); + const float dist_light = glm::length(light - position); + const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff + const float dot_wi_normal = glm::dot(wi, normal); + + const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); + + ray_hit r(position, wi); + + return occluded(r) ? 0.5f * L : L; +} + +glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) +{ const float max_tfar = 8; float total_tfar = 0; float tfar = r.ray.tfar; - glm::vec4 out_li = glm::vec4(0); + glm::vec4 L(0); - float dist_light, falloff, dot_wi_normal; - glm::vec3 wi; - + glm::vec3 position, normal; while(tfar < max_tfar) { total_tfar += tfar; - - dist_light = glm::length(light - r.position()); - falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff - - wi = normalize(light - r.position()); - - dot_wi_normal = flat ? glm::dot(wi, r.geometry_normal()) - : glm::dot(wi, r.shading_normal(geomID_mesh[r.hit.geomID])); - - if(dot_wi_normal < 0) dot_wi_normal = -dot_wi_normal; - out_li += tfar * glm::vec4(color * falloff * dot_wi_normal, 1); + position = r.position(); + normal = r.normal(geomID_mesh[r.hit.geomID], flat); - ray_hit ro(r.position(), wi); - if(occluded(ro)) out_li *= .5f; + if(!geomID_mesh[r.hit.geomID]->n_faces()) + pointcloud_hit(position, normal, r); + L += li(light, position, normal); + + /* r = ray_hit(r.position(), r.dir()); if(!intersect(r)) break; tfar = r.ray.tfar + total_tfar; + */ + + tfar = 10; } - return out_li / total_tfar; + return L / total_tfar; } bool embree::intersect(ray_hit & r) From 7318367c24d5710956c2bd9e1f5e7ca5318f8a46 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 21:01:59 +0200 Subject: [PATCH 0290/1018] rt: embree pointcloud hit weighted evaluation --- include/che.h | 1 + include/raytracing/rt_embree.h | 2 +- src/che.cpp | 5 +++++ src/raytracing/rt_embree.cpp | 23 ++++++++++++++--------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/che.h b/include/che.h index ba6ca0d4..89361386 100644 --- a/include/che.h +++ b/include/che.h @@ -88,6 +88,7 @@ class che void normalize(); bool is_manifold() const; + bool is_pointcloud() const; const index_t & vt(const index_t & he) const; const vertex & gt(const index_t & v) const; const vertex & gt_vt(const index_t & he) const; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 21636eb9..ac5e878c 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -55,7 +55,7 @@ class embree : public raytracing const glm::vec3 normal(const che * mesh, const bool & flat = false) const { - if(flat || !mesh->n_faces()) + if(flat || mesh->is_pointcloud()) return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); diff --git a/src/che.cpp b/src/che.cpp index b320b6b2..1fd35c3f 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -456,6 +456,11 @@ bool che::is_manifold() const return manifold; } +bool che::is_pointcloud() const +{ + return n_faces_ == 0; +} + const index_t & che::vt(const index_t & he) const { assert(he < n_half_edges_); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 1b52bbf6..6892aa98 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -134,19 +134,24 @@ index_t embree::add_point_cloud(const che * mesh) void embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) { - int n = 0; + float dist, sum_dist = 0; + position = glm::vec3(0); + normal = glm::vec3(0); - r = ray_hit(r.position(), r.dir()); - while(intersect(r) && r.ray.tfar < 0.001) + do { - n++; - position += r.position(); - normal += r.normal(geomID_mesh[r.hit.geomID], true); + glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); + + sum_dist += dist = glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); + position += dist * r.position(); + normal += dist * r.normal(geomID_mesh[r.hit.geomID], true); + r = ray_hit(r.position(), r.dir()); } + while(intersect(r) && r.ray.tfar < 0.001); - position /= n; - normal /= n; + position /= sum_dist; + normal /= sum_dist; } glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal) @@ -181,7 +186,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); - if(!geomID_mesh[r.hit.geomID]->n_faces()) + if(geomID_mesh[r.hit.geomID]->is_pointcloud()) pointcloud_hit(position, normal, r); L += li(light, position, normal); From 97283dd9ea3a04d935b7068ecfed486206ca91b4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Jun 2020 22:36:12 +0200 Subject: [PATCH 0291/1018] rt: embree pc_radius --- include/raytracing/rt_embree.h | 6 ++++-- src/raytracing/rt_embree.cpp | 34 +++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index ac5e878c..2951154e 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -17,6 +17,8 @@ namespace gproshan::rt { class embree : public raytracing { + static const float pc_radius; + struct ray_hit: public RTCRayHit { ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), @@ -89,8 +91,8 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); - void pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r); - glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal); + float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r); + glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 6892aa98..ed0c87cc 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -22,6 +22,8 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } +const float embree::pc_radius = 0.001; + embree::embree(const std::vector & meshes) { device = rtcNewDevice(NULL); @@ -120,7 +122,7 @@ index_t embree::add_point_cloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, 0.001); + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); } @@ -132,9 +134,11 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -void embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) +float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) { - float dist, sum_dist = 0; + const glm::vec3 hit = position; + + float w, sum_w = 0; position = glm::vec3(0); normal = glm::vec3(0); @@ -142,19 +146,21 @@ void embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & { glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); - sum_dist += dist = glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); - position += dist * r.position(); - normal += dist * r.normal(geomID_mesh[r.hit.geomID], true); + sum_w += w = pc_radius - glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); + position += w * r.position(); + normal += w * r.normal(geomID_mesh[r.hit.geomID], true); r = ray_hit(r.position(), r.dir()); } - while(intersect(r) && r.ray.tfar < 0.001); + while(intersect(r) && r.ray.tfar < pc_radius); + + position /= sum_w; + normal /= sum_w; - position /= sum_dist; - normal /= sum_dist; + return std::max(glm::length(hit - position), pc_radius); } -glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal) +glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near) { const glm::vec3 color(0.6, 0.8, 1.0); @@ -165,7 +171,7 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); - ray_hit r(position, wi); + ray_hit r(position, wi, near); return occluded(r) ? 0.5f * L : L; } @@ -178,6 +184,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) float tfar = r.ray.tfar; glm::vec4 L(0); + float near; glm::vec3 position, normal; while(tfar < max_tfar) { @@ -186,10 +193,11 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); + near = 1e-5f; if(geomID_mesh[r.hit.geomID]->is_pointcloud()) - pointcloud_hit(position, normal, r); + near += 20 * pointcloud_hit(position, normal, r); - L += li(light, position, normal); + L += li(light, position, normal, near); /* r = ray_hit(r.position(), r.dir()); From 47e24e7fa7d6b0626b39cb17e85447573d0cf947 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Jun 2020 08:09:05 +0200 Subject: [PATCH 0292/1018] rt: embree color radiance --- src/raytracing/rt_embree.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index ed0c87cc..723a8e69 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -55,9 +55,9 @@ index_t embree::add_sphere(const glm::vec4 & xyzr) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); - glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer(geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); *pxyzr = xyzr; rtcCommitGeometry(geom); @@ -72,11 +72,11 @@ index_t embree::add_mesh(const che * mesh) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); - glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer(geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices() - ); + glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT3, 3 * sizeof(float), + mesh->n_vertices() + ); index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, @@ -166,14 +166,13 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const const glm::vec3 wi = normalize(light - position); const float dist_light = glm::length(light - position); - const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff + const float falloff = 12.f / (dist_light * dist_light); // intensity multiplier / falloff const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); + const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * glm::vec3(1) * (float(1.f/M_PI) * color), 1); ray_hit r(position, wi, near); - - return occluded(r) ? 0.5f * L : L; + return (occluded(r) ? 0.5f : 1.f) * L; } glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) From 7869eac8337adde1d7024b46fd672e23889d6c5a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Jun 2020 09:51:51 +0200 Subject: [PATCH 0293/1018] rt: embree pc non const radius --- include/raytracing/rt_embree.h | 2 +- src/raytracing/rt_embree.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 2951154e..fcbad1c8 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -17,7 +17,7 @@ namespace gproshan::rt { class embree : public raytracing { - static const float pc_radius; + static float pc_radius; struct ray_hit: public RTCRayHit { diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 723a8e69..002f18ce 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -22,7 +22,7 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -const float embree::pc_radius = 0.001; +float embree::pc_radius = 0.0125; embree::embree(const std::vector & meshes) { From 9c90327f7069adbbdfd17fa2fbc311399a008b58 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Jun 2020 11:26:35 +0200 Subject: [PATCH 0294/1018] rt: add embree imgui input pc_radius --- include/raytracing/rt_embree.h | 5 +++-- src/raytracing/rt_embree.cpp | 12 ++++++------ src/viewer/viewer.cpp | 9 +++++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index fcbad1c8..cbc91a49 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -17,8 +17,6 @@ namespace gproshan::rt { class embree : public raytracing { - static float pc_radius; - struct ray_hit: public RTCRayHit { ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), @@ -74,6 +72,9 @@ class embree : public raytracing RTCDevice device; RTCScene scene; RTCIntersectContext intersect_context; + + public: + static float pc_radius; public: embree(const std::vector & meshes); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 002f18ce..94194faf 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -22,7 +22,7 @@ void embree_error(void * ptr, RTCError error, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -float embree::pc_radius = 0.0125; +float embree::pc_radius = 0.001; embree::embree(const std::vector & meshes) { @@ -162,17 +162,17 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near) { - const glm::vec3 color(0.6, 0.8, 1.0); + const glm::vec3 color(0.5, 0.7, 1.0); const glm::vec3 wi = normalize(light - position); const float dist_light = glm::length(light - position); - const float falloff = 12.f / (dist_light * dist_light); // intensity multiplier / falloff + const float falloff = 8.f / (dist_light * dist_light); // intensity multiplier / falloff const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * glm::vec3(1) * (float(1.f/M_PI) * color), 1); + const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (float(1.f/M_PI) * color) + glm::vec3(0.1), 1); ray_hit r(position, wi, near); - return (occluded(r) ? 0.5f : 1.f) * L; + return (occluded(r) ? 0.6f : 1.f) * L; } glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) @@ -197,7 +197,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) near += 20 * pointcloud_hit(position, normal, r); L += li(light, position, normal, near); - + /* r = ray_hit(r.position(), r.dir()); if(!intersect(r)) break; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d94af8cc..70d6a4c4 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -109,6 +109,15 @@ bool viewer::run() ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); + if(!rt_embree) + { + ImGui::SetNextWindowSize(ImVec2(300, -1)); + ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); + ImGui::Begin("rt_embree_pointcloud"); + ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.4f"); + ImGui::End(); + } + if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) From 46f52ed5edea1b584cf137fb241ea3bff516b878 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Jun 2020 11:30:14 +0200 Subject: [PATCH 0295/1018] rt: shadow near ray parameter --- src/raytracing/rt_embree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 94194faf..43831e26 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -194,7 +194,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) near = 1e-5f; if(geomID_mesh[r.hit.geomID]->is_pointcloud()) - near += 20 * pointcloud_hit(position, normal, r); + near += 10 * pointcloud_hit(position, normal, r); L += li(light, position, normal, near); From 6cd92518130952e929669a613a93cfd88d473981 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Jun 2020 12:55:11 +0200 Subject: [PATCH 0296/1018] rt: embree hit pc, return sum_w --- src/raytracing/rt_embree.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 43831e26..00c29d4a 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -136,8 +136,6 @@ index_t embree::add_point_cloud(const che * mesh) float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) { - const glm::vec3 hit = position; - float w, sum_w = 0; position = glm::vec3(0); normal = glm::vec3(0); @@ -157,7 +155,7 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & position /= sum_w; normal /= sum_w; - return std::max(glm::length(hit - position), pc_radius); + return sum_w; } glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near) @@ -194,7 +192,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) near = 1e-5f; if(geomID_mesh[r.hit.geomID]->is_pointcloud()) - near += 10 * pointcloud_hit(position, normal, r); + near += pointcloud_hit(position, normal, r); L += li(light, position, normal, near); From 5d5e6f456d24319881bc965890e8b405c0e02f0d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Jun 2020 12:55:54 +0200 Subject: [PATCH 0297/1018] save view: camera state --- include/quaternion.h | 16 +++---- include/viewer/camera.h | 15 ++++--- include/viewer/viewer.h | 1 - src/quaternion.cpp | 97 +++++++++++++++++------------------------ src/viewer/camera.cpp | 69 ++++++++++++++--------------- src/viewer/viewer.cpp | 11 ++--- 6 files changed, 94 insertions(+), 115 deletions(-) diff --git a/include/quaternion.h b/include/quaternion.h index b5bed6aa..557f1f67 100644 --- a/include/quaternion.h +++ b/include/quaternion.h @@ -17,16 +17,14 @@ class quaternion vertex v; public: - quaternion(); - quaternion(real_t s, real_t vi, real_t vj, real_t vk); + quaternion(real_t s = 0, real_t vi = 0, real_t vj = 0, real_t vk = 0); quaternion(real_t s, const vertex & v); - quaternion(real_t s); quaternion(const vertex & v); - const quaternion & operator=(real_t s); - const quaternion & operator=(const vertex & v); - real_t & operator[](int index); - const real_t & operator[](int index) const; + const quaternion & operator = (real_t s); + const quaternion & operator = (const vertex & v); + real_t & operator [] (int index); + const real_t & operator [] (int index) const; void toMatrix(real_t Q[4][4]) const; real_t & re(void); const real_t & re(void) const; @@ -53,10 +51,12 @@ class quaternion real_t norm2() const; quaternion unit() const; void normalize(); + + friend std::ostream & operator << (std::ostream & os, const quaternion & q); + friend std::istream & operator >> (std::istream & is, quaternion & q); }; quaternion operator * (real_t c, const quaternion & q); -std::ostream & operator << (std::ostream & os, const quaternion & q); } // namespace gproshan diff --git a/include/viewer/camera.h b/include/viewer/camera.h index 0484eb77..9f44ae95 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -13,13 +13,11 @@ namespace gproshan { class camera { private: - quaternion pClick; - quaternion pDrag; - quaternion pLast; - quaternion rLast; - quaternion momentum; - int tLast; - double vZoom; + quaternion p_click; + quaternion p_drag; + quaternion p_last; + quaternion r_last; + int t_last; public: double zoom; @@ -35,6 +33,9 @@ class camera private: quaternion click_to_sphere(int x, int y); + + friend std::ostream & operator << (std::ostream & os, const camera & cam); + friend std::istream & operator >> (std::istream & is, camera & cam); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index b48d3f68..92983deb 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -142,7 +142,6 @@ class viewer void render_optix(); // callbacks - static void idle(); static void keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods); static void mouse_callback(GLFWwindow * window, int button, int action, int mods); static void cursor_callback(GLFWwindow * window, double x, double y); diff --git a/src/quaternion.cpp b/src/quaternion.cpp index 1b39368b..04b9529e 100644 --- a/src/quaternion.cpp +++ b/src/quaternion.cpp @@ -10,56 +10,37 @@ using namespace std; namespace gproshan { -quaternion :: quaternion() -: s(0.), v(0., 0., 0.) -{ -} - -quaternion :: quaternion(real_t s_, real_t vi, real_t vj, real_t vk) -: s(s_), v(vi, vj, vk) -{ -} - -quaternion :: quaternion(real_t s_, const vertex & v_) -: s(s_), v(v_) -{ -} +quaternion::quaternion(real_t s_, real_t vi, real_t vj, real_t vk): s(s_), v(vi, vj, vk) {} -quaternion :: quaternion(real_t s_) -: s(s_),v(0., 0., 0.) -{ -} +quaternion::quaternion(real_t s_, const vertex & v_): s(s_), v(v_) {} -quaternion :: quaternion(const vertex & v_) -: s(0.), v(v_) -{ -} +quaternion::quaternion(const vertex & v_): s(0), v(v_) {} -const quaternion & quaternion :: operator=(real_t _s) +const quaternion & quaternion::operator = (real_t _s) { s = _s; - v = vertex(0., 0., 0.); + v = vertex(0, 0, 0); return *this; } -const quaternion & quaternion :: operator=(const vertex & _v) +const quaternion & quaternion::operator = (const vertex & _v) { - s = 0.; + s = 0; v = _v; return *this; } -real_t & quaternion::operator[](int index) +real_t & quaternion::operator [] (int index) { - return ( &s)[ index ]; + return (&s)[index]; } -const real_t & quaternion::operator[](int index) const +const real_t & quaternion::operator [] (int index) const { - return ( &s)[ index ]; + return (&s)[index]; } void quaternion::toMatrix(real_t Q[4][4]) const @@ -70,92 +51,92 @@ void quaternion::toMatrix(real_t Q[4][4]) const Q[3][0] = v.z; Q[3][1] = -v.y; Q[3][2] = v.x; Q[3][3] = s; } -real_t & quaternion::re(void) +real_t & quaternion::re() { return s; } -const real_t & quaternion::re(void) const +const real_t & quaternion::re() const { return s; } -vertex & quaternion::im(void) +vertex & quaternion::im() { return v; } -const vertex & quaternion::im(void) const +const vertex & quaternion::im() const { return v; } -quaternion quaternion::operator+(const quaternion & q) const +quaternion quaternion::operator + (const quaternion & q) const { return quaternion(s + q.s, v + q.v); } -quaternion quaternion::operator-(const quaternion & q) const +quaternion quaternion::operator - (const quaternion & q) const { return quaternion(s - q.s, v - q.v); } -quaternion quaternion::operator-(void) const +quaternion quaternion::operator - () const { return quaternion(-s, -v); } -quaternion quaternion::operator*(real_t c) const +quaternion quaternion::operator * (real_t c) const { return quaternion(c * s, c * v); } -quaternion operator*(real_t c, const quaternion & q) +quaternion operator * (real_t c, const quaternion & q) { return q * c; } -quaternion quaternion::operator/(real_t c) const +quaternion quaternion::operator / (real_t c) const { return quaternion(s / c, v / c); } -void quaternion::operator+=(const quaternion & q) +void quaternion::operator += (const quaternion & q) { s += q.s; v += q.v; } -void quaternion::operator+=(real_t c) +void quaternion::operator += (real_t c) { s += c; } -void quaternion::operator-=(const quaternion & q) +void quaternion::operator -= (const quaternion & q) { s -= q.s; v -= q.v; } -void quaternion::operator-=(real_t c) +void quaternion::operator -= (real_t c) { s -= c; } -void quaternion::operator*=(real_t c) +void quaternion::operator *= (real_t c) { s *= c; v *= c; } -void quaternion::operator/=(real_t c) +void quaternion::operator /= (real_t c) { s /= c; v /= c; } // Hamilton product -quaternion quaternion::operator*(const quaternion & q) const +quaternion quaternion::operator * (const quaternion & q) const { const real_t & s1(s); const real_t & s2(q.s); @@ -165,37 +146,37 @@ quaternion quaternion::operator*(const quaternion & q) const return quaternion(s1*s2 - (v1,v2), s1*v2 + s2*v1 + (v1*v2)); } -void quaternion::operator*=(const quaternion & q) +void quaternion::operator *= (const quaternion & q) { *this = (*this * q); } -quaternion quaternion::conj(void) const +quaternion quaternion::conj() const { return quaternion(s, -v); } -quaternion quaternion::inv(void) const +quaternion quaternion::inv() const { return (this->conj()) / this->norm2(); } -real_t quaternion::norm(void) const +real_t quaternion::norm() const { return sqrt(norm2()); } -real_t quaternion::norm2(void) const +real_t quaternion::norm2() const { return s * s + (v , v); } -quaternion quaternion::unit(void) const +quaternion quaternion::unit() const { return *this / norm(); } -void quaternion::normalize(void) +void quaternion::normalize() { *this /= norm(); } @@ -217,11 +198,15 @@ quaternion slerp(const quaternion & q0, const quaternion & q1, real_t t) return m * p; } -ostream & operator<<(ostream & os, const quaternion & q) +ostream & operator << (ostream & os, const quaternion & q) { - return os << "(" << q.re() << ", " << q.im() << ")"; + return os << q.s << " " << q.v; } +istream & operator >> (istream & is, quaternion & q) +{ + return is >> q.s >> q.v; +} } // namespace gproshan diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 229d114d..f6b398c3 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -11,14 +11,7 @@ using namespace std; namespace gproshan { -camera::camera() -: pClick(1.), - pDrag(1.), - pLast(1.), - rLast(1.), - momentum(1.), - zoom(1.) -{} +camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(1) {} quaternion camera::click_to_sphere(int x, int y) { @@ -50,23 +43,23 @@ quaternion camera::click_to_sphere(int x, int y) quaternion camera::current_rotation() const { - return (pDrag * pClick.conj()) * rLast; + return (p_drag * p_click.conj()) * r_last; } void camera::mouse(int , int state, int x, int y) { + quaternion momentum = 1; + if(state == GLFW_PRESS) - { - pClick = pDrag = pLast = click_to_sphere(x, y); - momentum = 1.; - } + p_click = p_drag = p_last = click_to_sphere(x, y); + if(state == GLFW_RELEASE) { - double timeSinceDrag = (clock() - tLast) / (double) CLOCKS_PER_SEC; + double timeSinceDrag = (clock() - t_last) / (double) CLOCKS_PER_SEC; if(timeSinceDrag < .1) { - momentum = pDrag * pLast.conj(); + momentum = p_drag * p_last.conj(); momentum = (.03 * momentum + .97).unit(); } else @@ -74,32 +67,16 @@ void camera::mouse(int , int state, int x, int y) momentum = 1.; } - rLast = pDrag * pClick.conj() * rLast; - pClick = pDrag = 1.; + r_last = p_drag * p_click.conj() * r_last; + p_click = p_drag = 1.; } } void camera::motion(int x, int y) { - tLast = clock(); - pLast = pDrag; - pDrag = click_to_sphere(x, y); -} - -void camera::idle() -{ - // get time since last idle event - static int t0 = clock(); - int t1 = clock(); - double dt = (t1-t0) / (double) CLOCKS_PER_SEC; - - rLast = momentum * rLast; - momentum = ((1.-.5*dt) * momentum + .5*dt).unit(); - - zoom += vZoom*dt; - vZoom *= max(0., 1.-5.*dt); - - t0 = t1; + t_last = clock(); + p_last = p_drag; + p_drag = click_to_sphere(x, y); } void camera::zoom_in() @@ -112,6 +89,26 @@ void camera::zoom_out() zoom += 0.01; } +ostream & operator << (ostream & os, const camera & cam) +{ + return os << cam.p_click << "\n" + << cam.p_drag << "\n" + << cam.p_last << "\n" + << cam.r_last << "\n" + << cam.t_last << "\n" + << cam.zoom << "\n"; +} + +istream & operator >> (istream & is, camera & cam) +{ + return is >> cam.p_click + >> cam.p_drag + >> cam.p_last + >> cam.r_last + >> cam.t_last + >> cam.zoom; +} + } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d94af8cc..eeeb3a89 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -383,12 +383,6 @@ void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset } } -void viewer::idle() -{ - //cam.idle(); - ////glutPostRedisplay(); -} - bool viewer::menu_help(viewer * view) { for(auto & p: view->processes) @@ -407,7 +401,7 @@ bool viewer::menu_save_load_view(viewer * view) if(ImGui::Button("Save")) { ofstream os(tmp_file_path("views/" + file)); - os << view->cam.current_rotation() << endl; + os << view->cam; os.close(); } @@ -415,6 +409,9 @@ bool viewer::menu_save_load_view(viewer * view) if(ImGui::Button("Load")) { + ifstream is(tmp_file_path("views/" + file)); + is >> view->cam; + is.close(); } return true; From 653f0fe1adff1ad797bbdcf6489709f6d8397b8a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Jun 2020 16:23:53 +0200 Subject: [PATCH 0298/1018] fix patch methods --- src/app_viewer.cpp | 6 +- src/mdict/d_mesh.cpp | 2 +- src/mdict/inpainting.cpp | 366 +++++++++++++++++++-------------------- 3 files changed, 178 insertions(+), 196 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7c47ae6e..2ba61e77 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -599,7 +599,7 @@ bool app_viewer::process_super_resolution(viewer * p_view) bool app_viewer::process_inpaiting(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int n = 12; // dct static int m = 144; @@ -636,7 +636,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) bool app_viewer::process_mask(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int n = 12; // dct static int m = 144; @@ -681,7 +681,7 @@ bool app_viewer::process_mask(viewer * p_view) bool app_viewer::process_pc_reconstruction(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->mesh(); + che_viewer & mesh = view->active_mesh(); static int n = 12; // dct static int m = 144; diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 883c7140..a1f7d7ce 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -356,7 +356,7 @@ a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & p return n_a_vec; } -/// DEPRECATED +[[deprecated]] void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i) { a_mat V(3, mesh->n_vertices(), arma::fill::zeros); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index bc005511..95dbebe5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -168,167 +168,7 @@ void inpainting::load_sampling(bool save_all) //radio *= 1.1; gproshan_debug_var(max_radio); - if(!S.load(f_sampl)) - { - - // compute features will be seeds - - patches_map.resize(mesh->n_vertices()); - - //Coverage of the points - bool covered[mesh->n_vertices()]; - - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - covered[i] = 0; - } - real_t euc_radio; - real_t geo_radio; - vector radios; - vector geo_radios; - size_t count_cov = 0; - size_t count_cov_patch = 0; - - bool faces[mesh->n_faces()] = {}; - vector idxs_he; - - for(size_t i = 0; i < all_sorted_features.size(); i++) - { - bool found = false; - size_t j = 0; - - //select the next one - while(j < seeds.size() && !found ) - { - //calculate distance between the actual patch p seed i and other features - const vertex & v_patch = mesh->gt(all_sorted_features[i]); - const vertex & v_seed = mesh->gt(seeds[j]); - - // 0.5 coverage parameter - if( *(v_patch - v_seed) < 0.4* radios[j] ) - { - // radio of each patch - found = true; - } - j++; - - } - - if(!found) - { - - patch p; - // increasing a bit the radio - idxs_he.clear(); - p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, all_sorted_features[i], euc_radio, geo_radio, delta, sum_thres, area_thres); - - //gproshan_debug_var(p.vertices.size()); - count_cov_patch = 0; - if(p.vertices.size() >= 7 ) - { - for(index_t k = 0; k < p.vertices.size(); k++) - { - if(!covered[ p.vertices[k] ]) count_cov_patch++; - } - - count_cov += count_cov_patch; - if(count_cov_patch > 0) - { - //gproshan_debug_var(p.vertices.size()); - patches.push_back(p); - seeds.push_back(all_sorted_features[i]); - radios.push_back( euc_radio ); - geo_radios.push_back( geo_radio); - count+=p.vertices.size(); - - for(index_t k = 0; k < p.vertices.size(); k++) - covered[ p.vertices[k] ] = 1; - - for(auto i:idxs_he) - faces[i] = true; - - - } - } - - } - } - - vector outliers; - gproshan_debug_var(sum_thres); - gproshan_debug_var(count); - gproshan_debug_var(count_cov); - gproshan_debug_var(seeds.size()); - M = seeds.size(); - - /////////////////////////////////////// - - #ifndef NDEBUG - gproshan_debug_var(M); - index_t tmp; - bool remark[mesh->n_vertices()] = {}; - - //outliers by triangles. - for(index_t i = 0; i < mesh->n_faces(); i++) - { - if(!faces[i]) - { - tmp = mesh->vt(next(i*3)); - if(!covered[ tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - - tmp = mesh->vt(prev(i*3)); - - if(!covered[ tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - - tmp = mesh->vt(i*3); - if(!covered[ tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - } - - } - gproshan_debug_var(outliers.size()); - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) - { - outliers.push_back(i); - } - } - a_vec outlv(outliers.size() ); - //gproshan_debug_var(seeds.size()); - for(index_t i = 0; i < outliers.size(); i++) - { - //outlv(i) = seeds[i]; - outlv(i) = outliers[i]; - //gproshan_debug_var(seeds[i]); - } - #endif // NDEBUG - - gproshan_debug_var(f_points); - outlv.save(f_points); - S.resize(seeds.size(),2); - for(index_t i = 0; i < seeds.size(); i++) - { - S(i,0) = seeds[i]; - S(i,1) = geo_radios[i]; - } - S.save(f_sampl); - - - } - else + if(S.load(f_sampl)) { gproshan_debug(already done); size_t n_seeds = S.n_rows; @@ -355,12 +195,159 @@ void inpainting::load_sampling(bool save_all) outlv.save(f_points); gproshan_debug(restored); + return; + } + + //ELSE IF !S.load(f_sample) + + // compute features will be seeds + patches_map.resize(mesh->n_vertices()); + + //Coverage of the points + bool covered[mesh->n_vertices()]; + + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + covered[i] = 0; + + real_t euc_radio; + real_t geo_radio; + vector radios; + vector geo_radios; + size_t count_cov = 0; + size_t count_cov_patch = 0; + + bool faces[mesh->n_faces()] = {}; + vector idxs_he; + + for(const index_t & vsf: all_sorted_features) + { + bool found = false; + + //select the next one + for(index_t j = 0; j < seeds.size(); j++) + { + //calculate distance between the actual patch p seed i and other features + const vertex & v_patch = mesh->gt(vsf); + const vertex & v_seed = mesh->gt(seeds[j]); + + // 0.5 coverage parameter + if( *(v_patch - v_seed) < 0.4* radios[j] ) + { + // radio of each patch + found = true; + break; + } + } + + if(found) continue; + + patch p; + // increasing a bit the radio + idxs_he.clear(); + p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, vsf, euc_radio, geo_radio, delta, sum_thres, area_thres); + + //gproshan_debug_var(p.vertices.size()); + count_cov_patch = 0; + if(p.vertices.size() >= 7 ) + { + for(index_t k = 0; k < p.vertices.size(); k++) + if(!covered[ p.vertices[k] ]) count_cov_patch++; + + count_cov += count_cov_patch; + if(count_cov_patch > 0) + { + //gproshan_debug_var(p.vertices.size()); + patches.push_back(p); + seeds.push_back(vsf); + radios.push_back( euc_radio ); + geo_radios.push_back( geo_radio); + count+=p.vertices.size(); + + for(index_t k = 0; k < p.vertices.size(); k++) + covered[ p.vertices[k] ] = 1; + + for(auto i:idxs_he) + faces[i] = true; + } + } + } + + vector outliers; + gproshan_debug_var(sum_thres); + gproshan_debug_var(count); + gproshan_debug_var(count_cov); + gproshan_debug_var(seeds.size()); + M = seeds.size(); + + /////////////////////////////////////// + +#ifndef NDEBUG + gproshan_debug_var(M); + index_t tmp; + bool remark[mesh->n_vertices()] = {}; + + //outliers by triangles. + for(index_t i = 0; i < mesh->n_faces(); i++) + { + if(!faces[i]) + { + tmp = mesh->vt(next(i*3)); + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + + tmp = mesh->vt(prev(i*3)); + + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + + tmp = mesh->vt(i*3); + if(!covered[ tmp] && !remark[tmp]) + { + outliers.push_back(tmp); + remark[tmp] = true; + } + } + + } + + gproshan_debug_var(outliers.size()); + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + if(!covered[i] ) + { + outliers.push_back(i); + } + } + a_vec outlv(outliers.size() ); + //gproshan_debug_var(seeds.size()); + for(index_t i = 0; i < outliers.size(); i++) + { + //outlv(i) = seeds[i]; + outlv(i) = outliers[i]; + //gproshan_debug_var(seeds[i]); + } + + gproshan_debug_var(f_points); + outlv.save(f_points); + S.resize(seeds.size(),2); + for(index_t i = 0; i < seeds.size(); i++) + { + S(i,0) = seeds[i]; + S(i,1) = geo_radios[i]; } + S.save(f_sampl); +#endif // NDEBUG } void inpainting::init_radial_feature_patches() { - load_sampling(false); load_mask(); @@ -372,36 +359,33 @@ void inpainting::init_radial_feature_patches() patches_map.resize(n_vertices); //initializing patch gproshan_debug_var(M); - //#pragma omp parallel - { - #ifndef NDEBUG - size_t patch_avg_size = 0; - size_t patch_min_size = NIL; - size_t patch_max_size = 0; + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; - #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) - patch_avg_size += patches[s].vertices.size(); - #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); - #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < M; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < M; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < M; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= M; + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); + #endif - patch_avg_size /= M; - gproshan_debug_var(patch_avg_size); - gproshan_debug_var(patch_min_size); - gproshan_debug_var(patch_max_size); - #endif - } - bool * pmask = mask; for(index_t s = 0; s < M; s++) patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - + gproshan_debug(passed); - + #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -414,7 +398,7 @@ void inpainting::init_radial_feature_patches() p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); phi_basis->discrete(p.phi, p.xyz); - } + } bool save_all = true; if(save_all) @@ -436,24 +420,22 @@ void inpainting::init_radial_feature_patches() AS(i,8) = p.T(1,1); AS(i,9) = p.T(2,1); AS(i,10) = p.T(0,2); - AS(i,11) = p.T(1,2); - AS(i,12) = p.T(2,2); + AS(i,11) = p.T(1,2); + AS(i,12) = p.T(2,2); } string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); AS.save(f_samplall); } gproshan_log(radial patches are ready); - } - void inpainting::init_voronoi_patches() { ///// std::vector vertices[M]; //index_t * clusters_tmp = init_voronoi_sampling(vertices); - + ////// gproshan_log_var(M); From 107120adffa309acd224d9ff31eb8d901eb804f1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Jun 2020 21:06:58 +0200 Subject: [PATCH 0299/1018] mdict: optimizing sampling patches --- src/che.cpp | 6 ++-- src/che_fill_hole.cpp | 8 ++--- src/mdict/inpainting.cpp | 75 +++++++++++++++++++--------------------- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 10 +++--- src/viewer/shader.cpp | 2 +- 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/che.cpp b/src/che.cpp index cde1b2b1..e841f081 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -821,11 +821,11 @@ void che::remove_non_manifold_vertices() { for( index_t he = 0; he < n_half_edges_; he+=3) { - if(EVT[ VT[he] ] == NIL || EVT[ VT[he+1]] == NIL || EVT[ VT[he+2] ] == NIL) + if(EVT[VT[he]] == NIL || EVT[VT[he+1]] == NIL || EVT[VT[he+2]] == NIL) { VT[he] = NIL; - VT[he + 1 ] = NIL; - VT[he + 2 ] = NIL; + VT[he + 1] = NIL; + VT[he + 2] = NIL; } } diff --git a/src/che_fill_hole.cpp b/src/che_fill_hole.cpp index 423e4504..acc3e372 100644 --- a/src/che_fill_hole.cpp +++ b/src/che_fill_hole.cpp @@ -529,8 +529,8 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, front.push(border_t(tmp_vertices, n_v, neighbors[n_v], o, tmp_normals[n_v])); perimeter += norm(tmp_vertices[p_v] - tmp_vertices[m_v]); - perimeter += norm(tmp_vertices[m_v + 1 ] - tmp_vertices[m_v]); - perimeter += norm(tmp_vertices[m_v + 1 ] - tmp_vertices[n_v]); + perimeter += norm(tmp_vertices[m_v + 1] - tmp_vertices[m_v]); + perimeter += norm(tmp_vertices[m_v + 1] - tmp_vertices[n_v]); } } @@ -791,8 +791,8 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c perimeter += norm(tmp_vertices[p_v] - tmp_vertices[m_v]); - perimeter += norm(tmp_vertices[m_v + 1 ] - tmp_vertices[m_v]); - perimeter += norm(tmp_vertices[m_v + 1 ] - tmp_vertices[n_v]); + perimeter += norm(tmp_vertices[m_v + 1] - tmp_vertices[m_v]); + perimeter += norm(tmp_vertices[m_v + 1] - tmp_vertices[n_v]); } } diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 95dbebe5..c5b74a06 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -113,17 +113,17 @@ void inpainting::load_mask(const std::vector * vertices, const index_t //gproshan_debug_var(clusters[rn]); rn = distribution(generator); - if(!mask[rn] && percentages_size[clusters[rn] ] > 0) + if(!mask[rn] && percentages_size[clusters[rn]] > 0) { mask[rn] = 1; V(rn) = 1; - percentages_size[ clusters[rn] ]--; + percentages_size[clusters[rn]]--; } - if( !cover_cluster[ clusters[rn] ] && percentages_size[clusters[rn] ] == 0) + if( !cover_cluster[clusters[rn]] && percentages_size[clusters[rn]] == 0) { - cover_cluster[ clusters[rn]] = 1; // It is finished + cover_cluster[clusters[rn]] = 1; // It is finished k++; } @@ -163,7 +163,7 @@ void inpainting::load_sampling(bool save_all) index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; - real_t max_radio = geo[ indexes[mesh->n_vertices()-1] ] ; + real_t max_radio = geo[indexes[mesh->n_vertices()-1]] ; //radio *= 1.1; gproshan_debug_var(max_radio); @@ -220,39 +220,24 @@ void inpainting::load_sampling(bool save_all) bool faces[mesh->n_faces()] = {}; vector idxs_he; + bool * invalid_seed = new bool[mesh->n_vertices()]; + memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); + for(const index_t & vsf: all_sorted_features) { - bool found = false; - - //select the next one - for(index_t j = 0; j < seeds.size(); j++) - { - //calculate distance between the actual patch p seed i and other features - const vertex & v_patch = mesh->gt(vsf); - const vertex & v_seed = mesh->gt(seeds[j]); - - // 0.5 coverage parameter - if( *(v_patch - v_seed) < 0.4* radios[j] ) - { - // radio of each patch - found = true; - break; - } - } - - if(found) continue; + if(invalid_seed[vsf]) continue; patch p; // increasing a bit the radio idxs_he.clear(); - p.init_radial_disjoint(idxs_he, mesh, 1*max_radio, vsf, euc_radio, geo_radio, delta, sum_thres, area_thres); + p.init_radial_disjoint(idxs_he, mesh, max_radio, vsf, euc_radio, geo_radio, delta, sum_thres, area_thres); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; if(p.vertices.size() >= 7 ) { - for(index_t k = 0; k < p.vertices.size(); k++) - if(!covered[ p.vertices[k] ]) count_cov_patch++; + for(const index_t & v: p.vertices) + if(!covered[v]) count_cov_patch++; count_cov += count_cov_patch; if(count_cov_patch > 0) @@ -260,19 +245,31 @@ void inpainting::load_sampling(bool save_all) //gproshan_debug_var(p.vertices.size()); patches.push_back(p); seeds.push_back(vsf); - radios.push_back( euc_radio ); - geo_radios.push_back( geo_radio); - count+=p.vertices.size(); + radios.push_back(euc_radio); + geo_radios.push_back(geo_radio); + count += p.vertices.size(); + + for(const index_t & v: p.vertices) + { + covered[v] = 1; - for(index_t k = 0; k < p.vertices.size(); k++) - covered[ p.vertices[k] ] = 1; + if(!invalid_seed[v]) + { + const vertex & va = mesh->get_vertex(vsf); + const vertex & vb = mesh->get_vertex(v); - for(auto i:idxs_he) + invalid_seed[v] = *(va - vb) < 0.4 * euc_radio; + } + } + + for(auto i: idxs_he) faces[i] = true; } } } + delete [] invalid_seed; + vector outliers; gproshan_debug_var(sum_thres); gproshan_debug_var(count); @@ -293,7 +290,7 @@ void inpainting::load_sampling(bool save_all) if(!faces[i]) { tmp = mesh->vt(next(i*3)); - if(!covered[ tmp] && !remark[tmp]) + if(!covered[tmp] && !remark[tmp]) { outliers.push_back(tmp); remark[tmp] = true; @@ -301,14 +298,14 @@ void inpainting::load_sampling(bool save_all) tmp = mesh->vt(prev(i*3)); - if(!covered[ tmp] && !remark[tmp]) + if(!covered[tmp] && !remark[tmp]) { outliers.push_back(tmp); remark[tmp] = true; } tmp = mesh->vt(i*3); - if(!covered[ tmp] && !remark[tmp]) + if(!covered[tmp] && !remark[tmp]) { outliers.push_back(tmp); remark[tmp] = true; @@ -420,8 +417,8 @@ void inpainting::init_radial_feature_patches() AS(i,8) = p.T(1,1); AS(i,9) = p.T(2,1); AS(i,10) = p.T(0,2); - AS(i,11) = p.T(1,2); - AS(i,12) = p.T(2,2); + AS(i,11) = p.T(1,2); + AS(i,12) = p.T(2,2); } string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); @@ -464,7 +461,7 @@ void inpainting::init_voronoi_patches() { ptp.clusters[i]--; if(sample(ptp.clusters[i]) != i) - vertices[ ptp.clusters[i] ].push_back(i) ; + vertices[ptp.clusters[i]].push_back(i) ; } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index d0614af4..347afc23 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -140,7 +140,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) arma::uvec indices = arma::sort_index( V , "desscend"); for(size_t i=0; i< V.size(); i++) - if(mask[ indices [i]]) return indices[i]; + if(mask[indices [i]]) return indices[i]; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index b5be1120..c7ed1dcc 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -362,7 +362,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & double area_tri(double x1, double y1, double x2, double y2, double x3, double y3) { - return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); + return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); } void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) { @@ -401,7 +401,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co min_v = v; } } - + // forstar to find closest trinagle a_mat abc(3,3); for_star(he, mesh, min_v) @@ -462,7 +462,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, cons size_t n_elem = distances.size(); if(distances.size()%2 ==0) { - avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1) ])/2; + avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; } else { diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 501740b7..2c2f09d7 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -99,7 +99,7 @@ bool shader::load(GLenum shader_type, const char * filename) if(maxLength > 0) { - GLchar* infoLog = new char[ maxLength ]; + GLchar* infoLog = new char[maxLength]; GLsizei length; glGetShaderInfoLog(shader, maxLength, &length, infoLog); From 42b4ddf1dba3bd493f1219b1d00329ee993ce8c3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 09:41:49 +0200 Subject: [PATCH 0300/1018] update imgui --- imgui/imgui.cpp | 188 ++++++++++++++++++++------------ imgui/imgui.h | 119 ++++++++++++--------- imgui/imgui_demo.cpp | 60 ++++++----- imgui/imgui_draw.cpp | 230 +++++++++++++++++++++++++--------------- imgui/imgui_internal.h | 26 +++-- imgui/imgui_widgets.cpp | 102 +++++++++++------- 6 files changed, 442 insertions(+), 283 deletions(-) diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index b74f44cf..c9146e13 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -18,7 +18,7 @@ // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). -// This library is free but I need your support to sustain development and maintenance. +// This library is free but needs your support to sustain development and maintenance. // Businesses: you can support continued development via invoiced technical support, maintenance and sponsoring contracts. Please reach out to "contact AT dearimgui.org". // Individuals: you can support continued development via donations. See docs/README or web page. @@ -169,7 +169,7 @@ CODE --------------------------------------------------------------- - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - In the majority of cases you should be able to use unmodified back-ends files available in the examples/ folder. - - Add the Dear ImGui source files to your projects or using your preferred build system. + - Add the Dear ImGui source files + selected back-end source files to your projects or using your preferred build system. It is recommended you build and statically link the .cpp files as part of your project and NOT as shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. @@ -372,7 +372,9 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. - - 2020/04/23 (1.77) - Removed unnecessary ID (first arg) of ImFontAtlas::AddCustomRectRegular(). + - 2020/06/15 (1.77) - renamed OpenPopupOnItemClick() to OpenPopupContextItem(). Kept inline redirection function (will obsolete). + - 2020/06/15 (1.77) - removed CalcItemRectClosestPoint() entry point which was made obsolete and asserting in December 2017. + - 2020/04/23 (1.77) - removed unnecessary ID (first arg) of ImFontAtlas::AddCustomRectRegular(). - 2020/01/22 (1.75) - ImDrawList::AddCircle()/AddCircleFilled() functions don't accept negative radius any more. - 2019/12/17 (1.75) - [undid this change in 1.76] made Columns() limited to 64 columns by asserting above that limit. While the current code technically supports it, future code may not so we're putting the restriction ahead. - 2019/12/13 (1.75) - [imgui_internal.h] changed ImRect() default constructor initializes all fields to 0.0f instead of (FLT_MAX,FLT_MAX,-FLT_MAX,-FLT_MAX). If you used ImRect::Add() to create bounding boxes by adding multiple points into it, you may need to fix your initial value. @@ -620,20 +622,24 @@ CODE Q: What is this library called? Q: Which version should I get? >> This library is called "Dear ImGui", please don't call it "ImGui" :) - >> See https://www.dearimgui.org/faq + >> See https://www.dearimgui.org/faq for details. Q&A: Integration ================ + Q: How to get started? + A: Read 'PROGRAMMER GUIDE' above. Read examples/README.txt. + Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application? A: You should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! >> See https://www.dearimgui.org/faq for fully detailed answer. You really want to read this. Q. How can I enable keyboard controls? Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) - Q: I integrated Dear ImGui in my engine and the text or lines are blurry.. + Q: I integrated Dear ImGui in my engine and little squares are showing instead of text.. Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. - >> See https://www.dearimgui.org/faq + Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries.. +>> See https://www.dearimgui.org/faq Q&A: Usage ---------- @@ -765,11 +771,12 @@ CODE Q&A: Fonts, Text ================ + Q: How should I handle DPI in my application? Q: How can I load a different font than the default? Q: How can I easily use icons in my application? Q: How can I load multiple fonts? Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? - >> See https://www.dearimgui.org/faq and docs/FONTS.txt + >> See https://www.dearimgui.org/faq and https://github.com/ocornut/imgui/edit/master/docs/FONTS.md Q&A: Concerns ============= @@ -860,21 +867,21 @@ CODE // Clang/GCC warnings with -Weverything #if defined(__clang__) -#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning : unknown warning group '-Wformat-pedantic *' // not all warnings are known by all clang versions.. so ignoring warnings triggers new warnings on some configuration. great! -#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. -#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. -#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. -#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning : declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. -#pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. -#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // -#pragma clang diagnostic ignored "-Wformat-pedantic" // warning : format specifies type 'void *' but the argument has type 'xxxx *' // unreasonable, would lead to casting every %p arg to void*. probably enabled by -pedantic. -#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning : cast to 'void *' from smaller integer type 'int' -#if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 -#endif -#if __has_warning("-Wdouble-promotion") -#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. +#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. +#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning: declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. +#pragma clang diagnostic ignored "-Wglobal-constructors" // warning: declaration requires a global destructor // similar to above, not sure what the exact difference is. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wformat-pedantic" // warning: format specifies type 'void *' but the argument has type 'xxxx *' // unreasonable, would lead to casting every %p arg to void*. probably enabled by -pedantic. +#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning: cast to 'void *' from smaller integer type 'int' +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) // We disable -Wpragmas because GCC doesn't provide an has_warning equivalent and some forks/patches may not following the warning/version association. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -1008,7 +1015,7 @@ ImGuiStyle::ImGuiStyle() { Alpha = 1.0f; // Global alpha applies to everything in ImGui WindowPadding = ImVec2(8,8); // Padding within a window - WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows + WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. WindowMinSize = ImVec2(32,32); // Minimum window size WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text @@ -2994,7 +3001,7 @@ void ImGui::GcCompactTransientWindowBuffers(ImGuiWindow* window) window->MemoryDrawListIdxCapacity = window->DrawList->IdxBuffer.Capacity; window->MemoryDrawListVtxCapacity = window->DrawList->VtxBuffer.Capacity; window->IDStack.clear(); - window->DrawList->ClearFreeMemory(); + window->DrawList->_ClearFreeMemory(); window->DC.ChildWindows.clear(); window->DC.ItemFlagsStack.clear(); window->DC.ItemWidthStack.clear(); @@ -3168,17 +3175,22 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) if (window->DC.ItemFlags & ImGuiItemFlags_Disabled) return false; - SetHoveredID(id); + // We exceptionally allow this function to be called with id==0 to allow using it for easy high-level + // hover test in widgets code. We could also decide to split this function is two. + if (id != 0) + { + SetHoveredID(id); - // [DEBUG] Item Picker tool! - // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making - // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered - // items if we perform the test in ItemAdd(), but that would incur a small runtime cost. - // #define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX in imconfig.h if you want this check to also be performed in ItemAdd(). - if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) - GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); - if (g.DebugItemPickerBreakId == id) - IM_DEBUG_BREAK(); + // [DEBUG] Item Picker tool! + // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making + // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered + // items if we perform the test in ItemAdd(), but that would incur a small runtime cost. + // #define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX in imconfig.h if you want this check to also be performed in ItemAdd(). + if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) + GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); + if (g.DebugItemPickerBreakId == id) + IM_DEBUG_BREAK(); + } return true; } @@ -3440,17 +3452,22 @@ void ImGui::UpdateMouseMovingWindowEndFrame() if (g.NavWindow && g.NavWindow->Appearing) return; - // Click to focus window and start moving (after we're done with all our widgets) + // Click on empty space to focus window and start moving (after we're done with all our widgets) if (g.IO.MouseClicked[0]) { - if (g.HoveredRootWindow != NULL) + // Handle the edge case of a popup being closed while clicking in its empty space. + // If we try to focus it, FocusWindow() > ClosePopupsOverWindow() will accidentally close any parent popups because they are not linked together any more. + ImGuiWindow* root_window = g.HoveredRootWindow; + const bool is_closed_popup = root_window && (root_window->Flags & ImGuiWindowFlags_Popup) && !IsPopupOpenAtAnyLevel(root_window->PopupId); + + if (root_window != NULL && !is_closed_popup) { StartMouseMovingWindow(g.HoveredWindow); - if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(g.HoveredRootWindow->Flags & ImGuiWindowFlags_NoTitleBar)) - if (!g.HoveredRootWindow->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) + if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(root_window->Flags & ImGuiWindowFlags_NoTitleBar)) + if (!root_window->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) g.MovingWindow = NULL; } - else if (g.NavWindow != NULL && GetTopMostPopupModal() == NULL) + else if (root_window != NULL && g.NavWindow != NULL && GetTopMostPopupModal() == NULL) { // Clicking on void disable focus FocusWindow(NULL); @@ -3691,7 +3708,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) { if (g.IO.MouseClicked[i]) - g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (!g.OpenPopupStack.empty()); + g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (g.OpenPopupStack.Size > 0); mouse_any_down |= g.IO.MouseDown[i]; if (g.IO.MouseDown[i]) if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[i] < g.IO.MouseClickedTime[mouse_earliest_button_down]) @@ -3709,7 +3726,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() if (g.WantCaptureMouseNextFrame != -1) g.IO.WantCaptureMouse = (g.WantCaptureMouseNextFrame != 0); else - g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (!g.OpenPopupStack.empty()); + g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (g.OpenPopupStack.Size > 0); // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to imgui, false = dispatch keyboard info to Dear ImGui + app) if (g.WantCaptureKeyboardNextFrame != -1) @@ -3777,11 +3794,11 @@ void ImGui::NewFrame() if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; - g.BackgroundDrawList.Clear(); + g.BackgroundDrawList._ResetForNewFrame(); g.BackgroundDrawList.PushTextureID(g.IO.Fonts->TexID); g.BackgroundDrawList.PushClipRectFullScreen(); - g.ForegroundDrawList.Clear(); + g.ForegroundDrawList._ResetForNewFrame(); g.ForegroundDrawList.PushTextureID(g.IO.Fonts->TexID); g.ForegroundDrawList.PushClipRectFullScreen(); @@ -4019,8 +4036,8 @@ void ImGui::Shutdown(ImGuiContext* context) g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); g.DrawDataBuilder.ClearFreeMemory(); - g.BackgroundDrawList.ClearFreeMemory(); - g.ForegroundDrawList.ClearFreeMemory(); + g.BackgroundDrawList._ClearFreeMemory(); + g.ForegroundDrawList._ClearFreeMemory(); g.TabBars.Clear(); g.CurrentTabBarStack.clear(); @@ -4077,18 +4094,12 @@ static void AddWindowToSortBuffer(ImVector* out_sorted_windows, Im static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) { - if (draw_list->CmdBuffer.empty()) + // Remove trailing command if unused. + // Technically we could return directly instead of popping, but this make things looks neat in Metrics window as well. + draw_list->_PopUnusedDrawCmd(); + if (draw_list->CmdBuffer.Size == 0) return; - // Remove trailing command if unused - ImDrawCmd& last_cmd = draw_list->CmdBuffer.back(); - if (last_cmd.ElemCount == 0 && last_cmd.UserCallback == NULL) - { - draw_list->CmdBuffer.pop_back(); - if (draw_list->CmdBuffer.empty()) - return; - } - // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. // May trigger for you if you are using PrimXXX functions incorrectly. IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); @@ -4173,7 +4184,12 @@ static void SetupDrawData(ImVector* draw_lists, ImDrawData* draw_da } } -// When using this function it is sane to ensure that float are perfectly rounded to integer values, to that e.g. (int)(max.x-min.x) in user's render produce correct result. +// Push a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering. +// - When using this function it is sane to ensure that float are perfectly rounded to integer values, +// so that e.g. (int)(max.x-min.x) in user's render produce correct result. +// - If the code here changes, may need to update code of functions like NextColumn() and PushColumnClipRect(): +// some frequently called functions which to modify both channels and clipping simultaneously tend to use the +// more specialized SetWindowClipRectBeforeSetChannel() to avoid extraneous updates of underlying ImDrawCmds. void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect) { ImGuiWindow* window = GetCurrentWindow(); @@ -5226,11 +5242,13 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s return ret_auto_fit; } -static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& rect, const ImVec2& padding) +static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& viewport_rect, const ImVec2& padding) { ImGuiContext& g = *GImGui; - ImVec2 size_for_clamping = (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) ? ImVec2(window->Size.x, window->TitleBarHeight()) : window->Size; - window->Pos = ImMin(rect.Max - padding, ImMax(window->Pos + size_for_clamping, rect.Min + padding) - size_for_clamping); + ImVec2 size_for_clamping = window->Size; + if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) + size_for_clamping.y = window->TitleBarHeight(); + window->Pos = ImClamp(window->Pos, viewport_rect.Min + padding - size_for_clamping, viewport_rect.Max - padding); } static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) @@ -5588,6 +5606,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->HasCloseButton = (p_open != NULL); window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX); window->IDStack.resize(1); + window->DrawList->_ResetForNewFrame(); // Restore buffer capacity when woken from a compacted state, to avoid if (window->MemoryCompacted) @@ -5757,8 +5776,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->Pos = ImFloor(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) + // Large values tend to lead to variety of artifacts and are not recommended. window->WindowRounding = (flags & ImGuiWindowFlags_ChildWindow) ? style.ChildRounding : ((flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupRounding : style.WindowRounding; + // For windows with title bar or menu bar, we clamp to FrameHeight(FontSize + FramePadding.y * 2.0f) to completely hide artifacts. + //if ((window->Flags & ImGuiWindowFlags_MenuBar) || !(window->Flags & ImGuiWindowFlags_NoTitleBar)) + // window->WindowRounding = ImMin(window->WindowRounding, g.FontSize + style.FramePadding.y * 2.0f); + // Apply window focus (new and reactivated windows are moved to front) bool want_focus = false; if (window_just_activated_by_user && !(flags & ImGuiWindowFlags_NoFocusOnAppearing)) @@ -5860,7 +5884,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // DRAWING // Setup draw list and outer clipping rectangle - window->DrawList->Clear(); + IM_ASSERT(window->DrawList->CmdBuffer.Size == 1 && window->DrawList->CmdBuffer[0].ElemCount == 0); window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID); PushClipRect(host_rect.Min, host_rect.Max, false); @@ -7585,18 +7609,37 @@ void ImGui::SetTooltip(const char* fmt, ...) // [SECTION] POPUPS //----------------------------------------------------------------------------- +// Return true if the popup is open at the current BeginPopup() level of the popup stack bool ImGui::IsPopupOpen(ImGuiID id) { ImGuiContext& g = *GImGui; return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id; } +// Return true if the popup is open at the current BeginPopup() level of the popup stack bool ImGui::IsPopupOpen(const char* str_id) { ImGuiContext& g = *GImGui; return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == g.CurrentWindow->GetID(str_id); } +bool ImGui::IsPopupOpenAtAnyLevel(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < g.OpenPopupStack.Size; n++) + if (g.OpenPopupStack[n].PopupId == id) + return true; + return false; +} + +// Return true if any popup is open at the current BeginPopup() level of the popup stack +// This may be used to e.g. test for another popups already opened in the same frame to handle popups priorities at the same level. +bool ImGui::IsAnyPopupOpen() +{ + ImGuiContext& g = *GImGui; + return g.OpenPopupStack.Size > g.BeginPopupStack.Size; +} + ImGuiWindow* ImGui::GetTopMostPopupModal() { ImGuiContext& g = *GImGui; @@ -7648,8 +7691,8 @@ void ImGui::OpenPopupEx(ImGuiID id) else { // Close child popups if any, then flag popup for open/reopen - g.OpenPopupStack.resize(current_stack_size + 1); - g.OpenPopupStack[current_stack_size] = popup_ref; + ClosePopupToLevel(current_stack_size, false); + g.OpenPopupStack.push_back(popup_ref); } // When reopening a popup we first refocus its parent, otherwise if its parent is itself a popup it would get closed by ClosePopupsOverWindow(). @@ -7662,7 +7705,7 @@ void ImGui::OpenPopupEx(ImGuiID id) void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; - if (g.OpenPopupStack.empty()) + if (g.OpenPopupStack.Size == 0) return; // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it. @@ -7701,6 +7744,8 @@ void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_ { ImGuiContext& g = *GImGui; IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); + + // Trim open popup stack ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow; ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window; g.OpenPopupStack.resize(remaining); @@ -7836,7 +7881,7 @@ void ImGui::EndPopup() g.WithinEndChild = false; } -bool ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiMouseButton mouse_button) +bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiMouseButton mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) @@ -7850,8 +7895,10 @@ bool ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiMouseButton mouse_butt } // This is a helper to handle the simplest case of associating one named popup to one given widget. -// You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). -// You can pass a NULL str_id to use the identifier of the last item. +// - You can pass a NULL str_id to use the identifier of the last item. +// - You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). +// - This is essentially the same as calling OpenPopupContextItem() + BeginPopup() but written to avoid +// computing the ID twice because BeginPopupContextXXX functions are called very frequently. bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiMouseButton mouse_button) { ImGuiWindow* window = GImGui->CurrentWindow; @@ -7866,9 +7913,10 @@ bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiMouseButton mouse_but bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mouse_button, bool also_over_items) { + ImGuiWindow* window = GImGui->CurrentWindow; if (!str_id) str_id = "window_context"; - ImGuiID id = GImGui->CurrentWindow->GetID(str_id); + ImGuiID id = window->GetID(str_id); if (IsMouseReleased(mouse_button) && IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) if (also_over_items || !IsAnyItemHovered()) OpenPopupEx(id); @@ -7877,11 +7925,13 @@ bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mouse_b bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiMouseButton mouse_button) { + ImGuiWindow* window = GImGui->CurrentWindow; if (!str_id) str_id = "void_context"; - ImGuiID id = GImGui->CurrentWindow->GetID(str_id); + ImGuiID id = window->GetID(str_id); if (IsMouseReleased(mouse_button) && !IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) - OpenPopupEx(id); + if (GetTopMostPopupModal() == NULL) + OpenPopupEx(id); return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); } @@ -10267,7 +10317,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; char buf[300]; - ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd: %4d triangles, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", + ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d triangles, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); diff --git a/imgui/imgui.h b/imgui/imgui.h index 824c98de..eb2a611b 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -60,7 +60,7 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) #define IMGUI_VERSION "1.77 WIP" -#define IMGUI_VERSION_NUM 17601 +#define IMGUI_VERSION_NUM 17602 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Define attributes of all API symbols declarations (e.g. for DLL under Windows) @@ -591,27 +591,34 @@ namespace ImGui IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); // Popups, Modals - // The properties of popups windows are: - // - They block normal mouse hovering detection outside them. (*1) - // - Unless modal, they can be closed by clicking anywhere outside them, or by pressing ESCAPE. - // Because hovering detection is disabled outside the popup, when clicking outside the click will not be seen by underlying widgets! (*1) - // - Their visibility state (~bool) is held internally by Dear ImGui instead of being held by the programmer as we are used to with regular Begin() calls. - // User can manipulate the visibility state by calling OpenPopup(), CloseCurrentPopup() etc. - // - We default to use the right mouse (ImGuiMouseButton_Right=1) for the Popup Context functions. - // Those three properties are connected: we need to retain popup visibility state in the library because popups may be closed as any time. - // (*1) You can bypass that restriction and detect hovering even when normally blocked by a popup. - // To do this use the ImGuiHoveredFlags_AllowWhenBlockedByPopup when calling IsItemHovered() or IsWindowHovered(). - // This is what BeginPopupContextItem() and BeginPopupContextWindow() are doing already, allowing a right-click to reopen another popups without losing the click. - IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). popups are closed when user click outside, or if CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. By default, Selectable()/MenuItem() are calling CloseCurrentPopup(). Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). - IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. only call EndPopup() if BeginPopup() returns true! - IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open and begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! - IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1, bool also_over_items = true); // helper to open and begin popup when clicked on current window. - IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open and begin popup when clicked in void (where there are no imgui windows). - IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // modal dialog (regular window with title bar, block interactions behind the modal window, can't close the modal window by clicking outside) - IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! - IMGUI_API bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1); // helper to open popup when clicked on last item (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors). return true when just opened. - IMGUI_API bool IsPopupOpen(const char* str_id); // return true if the popup is open at the current begin-ed level of the popup stack. - IMGUI_API void CloseCurrentPopup(); // close the popup we have begin-ed into. clicking on a MenuItem or Selectable automatically close the current popup. + // - They block normal mouse hovering detection (and therefore most mouse interactions) behind them. + // - If not modal: they can be closed by clicking anywhere outside them, or by pressing ESCAPE. + // - Their visibility state (~bool) is held internally instead of being held by the programmer as we are used to with regular Begin*() calls. + // - The 3 properties above are related: we need to retain popup visibility state in the library because popups may be closed as any time. + // - You can bypass the hovering restriction by using ImGuiHoveredFlags_AllowWhenBlockedByPopup when calling IsItemHovered() or IsWindowHovered(). + // - IMPORTANT: Popup identifiers are relative to the current ID stack, so OpenPopup and BeginPopup generally needs to be at the same level of the stack. + // This is sometimes leading to confusing mistakes. May rework this in the future. + // Popups: begin/end functions + // - BeginPopup(): query popup state, if open start appending into the window. Call EndPopup() afterwards. ImGuiWindowFlags are forwarded to the window. + // - BeginPopupModal(): block every interactions behind the window, cannot be closed by user, add a dimming background, has a title bar. + IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. + IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // return true if the modal is open, and you can start outputting to it. + IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! + // Popups: open/close functions + // - OpenPopup(): set popup state to open. + // - If not modal: they can be closed by clicking anywhere outside them, or by pressing ESCAPE. + // - CloseCurrentPopup(): use inside the BeginPopup()/EndPopup() scope to close manually. + // - CloseCurrentPopup() is called by default by Selectable()/MenuItem() when activated (FIXME: need some options). + IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). + IMGUI_API bool OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) + IMGUI_API void CloseCurrentPopup(); // manually close the popup we have begin-ed into. + IMGUI_API bool IsPopupOpen(const char* str_id); // return true if the popup is open at the current BeginPopup() level of the popup stack + // Popups: open+begin combined functions helpers + // - Helpers to do OpenPopup+BeginPopup where the Open action is triggered by e.g. hovering an item and right-clicking. + // - They are convenient to easily create context menus, hence the name. + IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! + IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1, bool also_over_items = true); // open+begin popup when clicked on current window. + IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // open+begin popup when clicked in void (where there are no windows). // Columns // - You can also use SameLine(pos_x) to mimic simplified columns. @@ -906,7 +913,8 @@ enum ImGuiTabItemFlags_ ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. Also: tab is selected on closure and closure is deferred by one frame to allow code to undo it without flicker. ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. - ImGuiTabItemFlags_NoPushId = 1 << 3 // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() + ImGuiTabItemFlags_NoPushId = 1 << 3, // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() + ImGuiTabItemFlags_NoTooltip = 1 << 4 // Disable tooltip for the given tab }; // Flags for ImGui::IsWindowFocused() @@ -1371,7 +1379,7 @@ struct ImGuiStyle { float Alpha; // Global alpha applies to everything in Dear ImGui. ImVec2 WindowPadding; // Padding within a window. - float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. + float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. float WindowBorderSize; // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). ImVec2 WindowMinSize; // Minimum window size. This is a global setting. If you want to constraint individual windows, use SetNextWindowSizeConstraints(). ImVec2 WindowTitleAlign; // Alignment for title bar text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered. @@ -1632,6 +1640,8 @@ struct ImGuiPayload #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.77 (from June 2020) + static inline bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1) { return OpenPopupContextItem(str_id, mouse_button); } // OBSOLETED in 1.72 (from July 2019) static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } // OBSOLETED in 1.71 (from June 2019) @@ -1652,7 +1662,6 @@ namespace ImGui // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } - static inline ImVec2 CalcItemRectClosestPoint(const ImVec2& pos, bool on_edge = false, float outward = 0.f) { IM_UNUSED(on_edge); IM_UNUSED(outward); IM_ASSERT(0); return pos; } } typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETED in 1.63 (from Aug 2018): made the names consistent typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; @@ -1874,19 +1883,21 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c #define ImDrawCallback_ResetRenderState (ImDrawCallback)(-1) // Typically, 1 command = 1 GPU draw call (unless command is a callback) -// Pre 1.71 back-ends will typically ignore the VtxOffset/IdxOffset fields. When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' -// is enabled, those fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. +// - VtxOffset/IdxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled, +// those fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. +// Pre-1.71 back-ends will typically ignore the VtxOffset/IdxOffset fields. +// - The ClipRect/TextureId/VtxOffset fields must be contiguous as we memcmp() them together (this is asserted for). struct ImDrawCmd { - unsigned int ElemCount; // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. - ImVec4 ClipRect; // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates - ImTextureID TextureId; // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. - unsigned int VtxOffset; // Start offset in vertex buffer. Pre-1.71 or without ImGuiBackendFlags_RendererHasVtxOffset: always 0. With ImGuiBackendFlags_RendererHasVtxOffset: may be >0 to support meshes larger than 64K vertices with 16-bit indices. - unsigned int IdxOffset; // Start offset in index buffer. Always equal to sum of ElemCount drawn so far. - ImDrawCallback UserCallback; // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. - void* UserCallbackData; // The draw callback code can access this. - - ImDrawCmd() { ElemCount = 0; TextureId = (ImTextureID)NULL; VtxOffset = IdxOffset = 0; UserCallback = NULL; UserCallbackData = NULL; } + ImVec4 ClipRect; // 4*4 // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates + ImTextureID TextureId; // 4-8 // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. + unsigned int VtxOffset; // 4 // Start offset in vertex buffer. ImGuiBackendFlags_RendererHasVtxOffset: always 0, otherwise may be >0 to support meshes larger than 64K vertices with 16-bit indices. + unsigned int IdxOffset; // 4 // Start offset in index buffer. Always equal to sum of ElemCount drawn so far. + unsigned int ElemCount; // 4 // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. + ImDrawCallback UserCallback; // 4-8 // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. + void* UserCallbackData; // 4-8 // The draw callback code can access this. + + ImDrawCmd() { memset(this, 0, sizeof(*this)); } // Also ensure our padding fields are zeroed }; // Vertex index, default to 16-bit @@ -1977,18 +1988,19 @@ struct ImDrawList // [Internal, used while building lists] const ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) const char* _OwnerName; // Pointer to owner window's name for debugging - unsigned int _VtxCurrentOffset; // [Internal] Always 0 unless 'Flags & ImDrawListFlags_AllowVtxOffset'. unsigned int _VtxCurrentIdx; // [Internal] Generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImVector _ClipRectStack; // [Internal] ImVector _TextureIdStack; // [Internal] ImVector _Path; // [Internal] current path building - ImDrawListSplitter _Splitter; // [Internal] for channels api + ImDrawCmd _CmdHeader; // [Internal] Template of active commands. Fields should match those of CmdBuffer.back(). + ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!) // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) - ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; _OwnerName = NULL; Clear(); } - ~ImDrawList() { ClearFreeMemory(); } + ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; Flags = ImDrawListFlags_None; _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; _OwnerName = NULL; } + + ~ImDrawList() { _ClearFreeMemory(); } IMGUI_API void PushClipRect(ImVec2 clip_rect_min, ImVec2 clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) IMGUI_API void PushClipRectFullScreen(); IMGUI_API void PopClipRect(); @@ -2048,26 +2060,31 @@ struct ImDrawList // - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit FG primitives before BG primitives) // - Use to minimize draw calls (e.g. if going back-and-forth between multiple clipping rectangles, prefer to append into separate channels then merge at the end) // - FIXME-OBSOLETE: This API shouldn't have been in ImDrawList in the first place! - // Prefer using your own persistent copy of ImDrawListSplitter as you can stack them. + // Prefer using your own persistent instance of ImDrawListSplitter as you can stack them. // Using the ImDrawList::ChannelsXXXX you cannot stack a split over another. inline void ChannelsSplit(int count) { _Splitter.Split(this, count); } inline void ChannelsMerge() { _Splitter.Merge(this); } inline void ChannelsSetCurrent(int n) { _Splitter.SetCurrentChannel(this, n); } - // Internal helpers - // NB: all primitives needs to be reserved via PrimReserve() beforehand! - IMGUI_API void Clear(); - IMGUI_API void ClearFreeMemory(); + // Advanced: Primitives allocations + // - We render triangles (three vertices) + // - All primitives needs to be reserved via PrimReserve() beforehand. IMGUI_API void PrimReserve(int idx_count, int vtx_count); IMGUI_API void PrimUnreserve(int idx_count, int vtx_count); IMGUI_API void PrimRect(const ImVec2& a, const ImVec2& b, ImU32 col); // Axis aligned rectangle (composed of two triangles) IMGUI_API void PrimRectUV(const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, ImU32 col); IMGUI_API void PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& d, const ImVec2& uv_a, const ImVec2& uv_b, const ImVec2& uv_c, const ImVec2& uv_d, ImU32 col); - inline void PrimWriteVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col){ _VtxWritePtr->pos = pos; _VtxWritePtr->uv = uv; _VtxWritePtr->col = col; _VtxWritePtr++; _VtxCurrentIdx++; } - inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } - inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } - IMGUI_API void UpdateClipRect(); - IMGUI_API void UpdateTextureID(); + inline void PrimWriteVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { _VtxWritePtr->pos = pos; _VtxWritePtr->uv = uv; _VtxWritePtr->col = col; _VtxWritePtr++; _VtxCurrentIdx++; } + inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } + inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index + + // [Internal helpers] + IMGUI_API void _ResetForNewFrame(); + IMGUI_API void _ClearFreeMemory(); + IMGUI_API void _PopUnusedDrawCmd(); + IMGUI_API void _OnChangedClipRect(); + IMGUI_API void _OnChangedTextureID(); + IMGUI_API void _OnChangedVtxOffset(); }; // All draw data to render a Dear ImGui frame @@ -2237,7 +2254,7 @@ struct ImFontAtlas // After calling Build(), you can query the rectangle position and render your pixels. // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), // so you can render e.g. custom colorful icons and use them as regular glyphs. - // Read docs/FONTS.txt for more details about using colorful icons. + // Read docs/FONTS.md for more details about using colorful icons. // Note: this API may be redesigned later in order to support multi-monitor varying DPI settings. IMGUI_API int AddCustomRectRegular(int width, int height); IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0)); diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 8cece7be..33d069dc 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -79,32 +79,34 @@ Index of this file: #include // intptr_t #endif +// Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen #endif + +// Clang/GCC warnings with -Weverything #if defined(__clang__) -#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. -#pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning: 'xx' is deprecated: The POSIX name for this.. // for strdup used in demo code (so user can copy & paste the code) -#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning: cast to 'void *' from smaller integer type -#pragma clang diagnostic ignored "-Wformat-security" // warning: format string is not a string literal -#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning: declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. -#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. -#if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 -#endif -#if __has_warning("-Wdouble-promotion") -#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. -#endif -#if __has_warning("-Wreserved-id-macro") -#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wdeprecated-declarations" // warning: 'xx' is deprecated: The POSIX name for this.. // for strdup used in demo code (so user can copy & paste the code) +#pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" // warning: cast to 'void *' from smaller integer type +#pragma clang diagnostic ignored "-Wformat-security" // warning: format string is not a string literal +#pragma clang diagnostic ignored "-Wexit-time-destructors" // warning: declaration requires an exit-time destructor // exit-time destruction order is undefined. if MemFree() leads to users code that has been disabled before exit it might cause problems. ImGui coding style welcomes static/globals. +#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind -#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size -#pragma GCC diagnostic ignored "-Wformat-security" // warning: format string is not a string literal (potentially insecure) -#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function -#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value -#pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size +#pragma GCC diagnostic ignored "-Wformat-security" // warning: format string is not a string literal (potentially insecure) +#pragma GCC diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function +#pragma GCC diagnostic ignored "-Wconversion" // warning: conversion to 'xxxx' from 'xxxx' may alter its value +#pragma GCC diagnostic ignored "-Wmisleading-indentation" // [__GNUC__ >= 6] warning: this 'if' clause does not guard this statement // GCC 6.0+ only. See #883 on GitHub. #endif // Play it nice with Windows users (Update: May 2018, Notepad now supports Unix-style carriage returns!) @@ -152,7 +154,7 @@ static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleMenuFile(); // Helper to display a little (?) mark which shows a tooltip when hovered. -// In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.txt) +// In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.md) static void HelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); @@ -861,7 +863,7 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("UTF-8 Text")) { // UTF-8 test with Japanese characters - // (Needs a suitable font? Try "Google Noto" or "Arial Unicode". See docs/FONTS.txt for details.) + // (Needs a suitable font? Try "Google Noto" or "Arial Unicode". See docs/FONTS.md for details.) // - From C++11 you can use the u8"my text" syntax to encode literal strings as UTF-8 // - For earlier compiler, you may be able to encode your sources as UTF-8 (e.g. in Visual Studio, you // can save your source files as 'UTF-8 without signature'). @@ -873,7 +875,7 @@ static void ShowDemoWindowWidgets() ImGui::TextWrapped( "CJK text will only appears if the font was loaded with the appropriate CJK character ranges. " "Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. " - "Read docs/FONTS.txt for details."); + "Read docs/FONTS.md for details."); ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; @@ -2813,7 +2815,7 @@ static void ShowDemoWindowPopups() if (ImGui::TreeNode("Context menus")) { // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing: - // if (IsItemHovered() && IsMouseReleased(0)) + // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) // OpenPopup(id); // return BeginPopup(id); // For more advanced uses you may want to replicate and customize this code. @@ -2829,11 +2831,11 @@ static void ShowDemoWindowPopups() ImGui::EndPopup(); } - // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the + // We can also use OpenPopupContextItem() which is the same as BeginPopupContextItem() but without the // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) // will toggle the visibility of the popup above. ImGui::Text("(You can also right-click me to open the same popup as above.)"); - ImGui::OpenPopupOnItemClick("item context menu", 1); + ImGui::OpenPopupContextItem("item context menu", 1); // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). // BeginPopupContextItem() will use the last item ID as the popup ID. @@ -3538,7 +3540,7 @@ void ImGui::ShowFontSelector(const char* label) HelpMarker( "- Load additional fonts with io.Fonts->AddFontFromFileTTF().\n" "- The font atlas is built when calling io.Fonts->GetTexDataAsXXXX() or io.Fonts->Build().\n" - "- Read FAQ and docs/FONTS.txt for more details.\n" + "- Read FAQ and docs/FONTS.md for more details.\n" "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame()."); } @@ -3766,7 +3768,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { // Tips: in a real user application, you may want to merge and use an icon font into the main font, // so instead of "Save"/"Revert" you'd use icons! - // Read the FAQ and docs/FONTS.txt about using icon fonts. It's really easy and super convenient! + // Read the FAQ and docs/FONTS.md about using icon fonts. It's really easy and super convenient! ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Save")) { ref->Colors[i] = style.Colors[i]; } ImGui::SameLine(0.0f, style.ItemInnerSpacing.x); if (ImGui::Button("Revert")) { style.Colors[i] = ref->Colors[i]; } } @@ -3784,7 +3786,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { ImGuiIO& io = ImGui::GetIO(); ImFontAtlas* atlas = io.Fonts; - HelpMarker("Read FAQ and docs/FONTS.txt for details on font loading."); + HelpMarker("Read FAQ and docs/FONTS.md for details on font loading."); ImGui::PushItemWidth(120); for (int i = 0; i < atlas->Fonts.Size; i++) { diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 057ddb65..0e7c294e 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -57,22 +57,19 @@ Index of this file: // Clang/GCC warnings with -Weverything #if defined(__clang__) -#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. -#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants ok. -#pragma clang diagnostic ignored "-Wglobal-constructors" // warning : declaration requires a global destructor // similar to above, not sure what the exact difference is. -#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // -#if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 -#endif -#if __has_warning("-Wcomma") -#pragma clang diagnostic ignored "-Wcomma" // warning : possible misuse of comma operator here // -#endif -#if __has_warning("-Wreserved-id-macro") -#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning : macro name is a reserved identifier // -#endif -#if __has_warning("-Wdouble-promotion") -#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok. +#pragma clang diagnostic ignored "-Wglobal-constructors" // warning: declaration requires a global destructor // similar to above, not sure what the exact difference is. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wcomma" // warning: possible misuse of comma operator here +#pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used @@ -108,7 +105,7 @@ namespace IMGUI_STB_NAMESPACE #pragma clang diagnostic ignored "-Wunused-function" #pragma clang diagnostic ignored "-Wmissing-prototypes" #pragma clang diagnostic ignored "-Wimplicit-fallthrough" -#pragma clang diagnostic ignored "-Wcast-qual" // warning : cast from 'const xxxx *' to 'xxx *' drops const qualifier // +#pragma clang diagnostic ignored "-Wcast-qual" // warning: cast from 'const xxxx *' to 'xxx *' drops const qualifier #endif #if defined(__GNUC__) @@ -376,13 +373,20 @@ void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) } } -void ImDrawList::Clear() +// Initialize before use in a new frame. We always have a command ready in the buffer. +void ImDrawList::_ResetForNewFrame() { + // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. + // (those should be IM_STATIC_ASSERT() in theory but with our pre C++11 setup the whole check doesn't compile with GCC) + IM_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); + IM_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); + IM_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); + CmdBuffer.resize(0); IdxBuffer.resize(0); VtxBuffer.resize(0); - Flags = _Data ? _Data->InitialFlags : ImDrawListFlags_None; - _VtxCurrentOffset = 0; + Flags = _Data->InitialFlags; + memset(&_CmdHeader, 0, sizeof(_CmdHeader)); _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; @@ -390,13 +394,15 @@ void ImDrawList::Clear() _TextureIdStack.resize(0); _Path.resize(0); _Splitter.Clear(); + CmdBuffer.push_back(ImDrawCmd()); } -void ImDrawList::ClearFreeMemory() +void ImDrawList::_ClearFreeMemory() { CmdBuffer.clear(); IdxBuffer.clear(); VtxBuffer.clear(); + Flags = ImDrawListFlags_None; _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; @@ -416,86 +422,117 @@ ImDrawList* ImDrawList::CloneOutput() const return dst; } -// Using macros because C++ is a terrible language, we want guaranteed inline, no code in header, and no overhead in Debug builds -#define GetCurrentClipRect() (_ClipRectStack.Size ? _ClipRectStack.Data[_ClipRectStack.Size-1] : _Data->ClipRectFullscreen) -#define GetCurrentTextureId() (_TextureIdStack.Size ? _TextureIdStack.Data[_TextureIdStack.Size-1] : (ImTextureID)NULL) - void ImDrawList::AddDrawCmd() { ImDrawCmd draw_cmd; - draw_cmd.ClipRect = GetCurrentClipRect(); - draw_cmd.TextureId = GetCurrentTextureId(); - draw_cmd.VtxOffset = _VtxCurrentOffset; + draw_cmd.ClipRect = _CmdHeader.ClipRect; // Same as calling ImDrawCmd_HeaderCopy() + draw_cmd.TextureId = _CmdHeader.TextureId; + draw_cmd.VtxOffset = _CmdHeader.VtxOffset; draw_cmd.IdxOffset = IdxBuffer.Size; IM_ASSERT(draw_cmd.ClipRect.x <= draw_cmd.ClipRect.z && draw_cmd.ClipRect.y <= draw_cmd.ClipRect.w); CmdBuffer.push_back(draw_cmd); } +// Pop trailing draw command (used before merging or presenting to user) +// Note that this leaves the ImDrawList in a state unfit for further commands, as most code assume that CmdBuffer.Size > 0 && CmdBuffer.back().UserCallback == NULL +void ImDrawList::_PopUnusedDrawCmd() +{ + if (CmdBuffer.Size == 0) + return; + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount == 0 && curr_cmd->UserCallback == NULL) + CmdBuffer.pop_back(); +} + void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) { - ImDrawCmd* current_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; - if (!current_cmd || current_cmd->ElemCount != 0 || current_cmd->UserCallback != NULL) + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + IM_ASSERT(curr_cmd->UserCallback == NULL); + if (curr_cmd->ElemCount != 0) { AddDrawCmd(); - current_cmd = &CmdBuffer.back(); + curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; } - current_cmd->UserCallback = callback; - current_cmd->UserCallbackData = callback_data; + curr_cmd->UserCallback = callback; + curr_cmd->UserCallbackData = callback_data; AddDrawCmd(); // Force a new command after us (see comment below) } +// Compare ClipRect, TextureId and VtxOffset with a single memcmp() +#define ImDrawCmd_HeaderSize (IM_OFFSETOF(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) +#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset +#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset + // Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack. // The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only. -void ImDrawList::UpdateClipRect() +void ImDrawList::_OnChangedClipRect() { // If current command is used with different settings we need to add a new command - const ImVec4 curr_clip_rect = GetCurrentClipRect(); - ImDrawCmd* curr_cmd = CmdBuffer.Size > 0 ? &CmdBuffer.Data[CmdBuffer.Size-1] : NULL; - if (!curr_cmd || (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) != 0) || curr_cmd->UserCallback != NULL) + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &_CmdHeader.ClipRect, sizeof(ImVec4)) != 0) { AddDrawCmd(); return; } + IM_ASSERT(curr_cmd->UserCallback == NULL); // Try to merge with previous command if it matches, else use current command - ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; - if (curr_cmd->ElemCount == 0 && prev_cmd && memcmp(&prev_cmd->ClipRect, &curr_clip_rect, sizeof(ImVec4)) == 0 && prev_cmd->TextureId == GetCurrentTextureId() && prev_cmd->UserCallback == NULL) + ImDrawCmd* prev_cmd = curr_cmd - 1; + if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && prev_cmd->UserCallback == NULL) + { CmdBuffer.pop_back(); - else - curr_cmd->ClipRect = curr_clip_rect; + return; + } + + curr_cmd->ClipRect = _CmdHeader.ClipRect; } -void ImDrawList::UpdateTextureID() +void ImDrawList::_OnChangedTextureID() { // If current command is used with different settings we need to add a new command - const ImTextureID curr_texture_id = GetCurrentTextureId(); - ImDrawCmd* curr_cmd = CmdBuffer.Size ? &CmdBuffer.back() : NULL; - if (!curr_cmd || (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != curr_texture_id) || curr_cmd->UserCallback != NULL) + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId) { AddDrawCmd(); return; } + IM_ASSERT(curr_cmd->UserCallback == NULL); // Try to merge with previous command if it matches, else use current command - ImDrawCmd* prev_cmd = CmdBuffer.Size > 1 ? curr_cmd - 1 : NULL; - if (curr_cmd->ElemCount == 0 && prev_cmd && prev_cmd->TextureId == curr_texture_id && memcmp(&prev_cmd->ClipRect, &GetCurrentClipRect(), sizeof(ImVec4)) == 0 && prev_cmd->UserCallback == NULL) + ImDrawCmd* prev_cmd = curr_cmd - 1; + if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && prev_cmd->UserCallback == NULL) + { CmdBuffer.pop_back(); - else - curr_cmd->TextureId = curr_texture_id; + return; + } + + curr_cmd->TextureId = _CmdHeader.TextureId; } -#undef GetCurrentClipRect -#undef GetCurrentTextureId +void ImDrawList::_OnChangedVtxOffset() +{ + // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. + _VtxCurrentIdx = 0; + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); + if (curr_cmd->ElemCount != 0) + { + AddDrawCmd(); + return; + } + IM_ASSERT(curr_cmd->UserCallback == NULL); + curr_cmd->VtxOffset = _CmdHeader.VtxOffset; +} // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) { ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); - if (intersect_with_current_clip_rect && _ClipRectStack.Size) + if (intersect_with_current_clip_rect) { - ImVec4 current = _ClipRectStack.Data[_ClipRectStack.Size-1]; + ImVec4 current = _CmdHeader.ClipRect; if (cr.x < current.x) cr.x = current.x; if (cr.y < current.y) cr.y = current.y; if (cr.z > current.z) cr.z = current.z; @@ -505,7 +542,8 @@ void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_ cr.w = ImMax(cr.y, cr.w); _ClipRectStack.push_back(cr); - UpdateClipRect(); + _CmdHeader.ClipRect = cr; + _OnChangedClipRect(); } void ImDrawList::PushClipRectFullScreen() @@ -515,22 +553,23 @@ void ImDrawList::PushClipRectFullScreen() void ImDrawList::PopClipRect() { - IM_ASSERT(_ClipRectStack.Size > 0); _ClipRectStack.pop_back(); - UpdateClipRect(); + _CmdHeader.ClipRect = (_ClipRectStack.Size == 0) ? _Data->ClipRectFullscreen : _ClipRectStack.Data[_ClipRectStack.Size - 1]; + _OnChangedClipRect(); } void ImDrawList::PushTextureID(ImTextureID texture_id) { _TextureIdStack.push_back(texture_id); - UpdateTextureID(); + _CmdHeader.TextureId = texture_id; + _OnChangedTextureID(); } void ImDrawList::PopTextureID() { - IM_ASSERT(_TextureIdStack.Size > 0); _TextureIdStack.pop_back(); - UpdateTextureID(); + _CmdHeader.TextureId = (_TextureIdStack.Size == 0) ? (ImTextureID)NULL : _TextureIdStack.Data[_TextureIdStack.Size - 1]; + _OnChangedTextureID(); } // Reserve space for a number of vertices and indices. @@ -542,13 +581,12 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) { - _VtxCurrentOffset = VtxBuffer.Size; - _VtxCurrentIdx = 0; - AddDrawCmd(); + _CmdHeader.VtxOffset = VtxBuffer.Size; + _OnChangedVtxOffset(); } - ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size - 1]; - draw_cmd.ElemCount += idx_count; + ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + draw_cmd->ElemCount += idx_count; int vtx_buffer_old_size = VtxBuffer.Size; VtxBuffer.resize(vtx_buffer_old_size + vtx_count); @@ -564,8 +602,8 @@ void ImDrawList::PrimUnreserve(int idx_count, int vtx_count) { IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); - ImDrawCmd& draw_cmd = CmdBuffer.Data[CmdBuffer.Size - 1]; - draw_cmd.ElemCount -= idx_count; + ImDrawCmd* draw_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + draw_cmd->ElemCount -= idx_count; VtxBuffer.shrink(VtxBuffer.Size - vtx_count); IdxBuffer.shrink(IdxBuffer.Size - idx_count); } @@ -1219,9 +1257,9 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, if (font_size == 0.0f) font_size = _Data->FontSize; - IM_ASSERT(font->ContainerAtlas->TexID == _TextureIdStack.back()); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. + IM_ASSERT(font->ContainerAtlas->TexID == _CmdHeader.TextureId); // Use high-level ImGui::PushFont() or low-level ImDrawList::PushTextureId() to change font. - ImVec4 clip_rect = _ClipRectStack.back(); + ImVec4 clip_rect = _CmdHeader.ClipRect; if (cpu_fine_clip_rect) { clip_rect.x = ImMax(clip_rect.x, cpu_fine_clip_rect->x); @@ -1242,7 +1280,7 @@ void ImDrawList::AddImage(ImTextureID user_texture_id, const ImVec2& p_min, cons if ((col & IM_COL32_A_MASK) == 0) return; - const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; if (push_texture_id) PushTextureID(user_texture_id); @@ -1258,7 +1296,7 @@ void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, con if ((col & IM_COL32_A_MASK) == 0) return; - const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; if (push_texture_id) PushTextureID(user_texture_id); @@ -1341,27 +1379,20 @@ void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) if (_Channels[i]._CmdBuffer.Size == 0) { ImDrawCmd draw_cmd; - draw_cmd.ClipRect = draw_list->_ClipRectStack.back(); - draw_cmd.TextureId = draw_list->_TextureIdStack.back(); + ImDrawCmd_HeaderCopy(&draw_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset _Channels[i]._CmdBuffer.push_back(draw_cmd); } } } -static inline bool CanMergeDrawCommands(ImDrawCmd* a, ImDrawCmd* b) -{ - return memcmp(&a->ClipRect, &b->ClipRect, sizeof(a->ClipRect)) == 0 && a->TextureId == b->TextureId && a->VtxOffset == b->VtxOffset && !a->UserCallback && !b->UserCallback; -} - void ImDrawListSplitter::Merge(ImDrawList* draw_list) { - // Note that we never use or rely on channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use. + // Note that we never use or rely on _Channels.Size because it is merely a buffer that we never shrink back to 0 to keep all sub-buffers ready for use. if (_Count <= 1) return; SetCurrentChannel(draw_list, 0); - if (draw_list->CmdBuffer.Size != 0 && draw_list->CmdBuffer.back().ElemCount == 0) - draw_list->CmdBuffer.pop_back(); + draw_list->_PopUnusedDrawCmd(); // Calculate our final buffer sizes. Also fix the incorrect IdxOffset values in each command. int new_cmd_buffer_count = 0; @@ -1371,14 +1402,21 @@ void ImDrawListSplitter::Merge(ImDrawList* draw_list) for (int i = 1; i < _Count; i++) { ImDrawChannel& ch = _Channels[i]; + + // Equivalent of PopUnusedDrawCmd() for this channel's cmdbuffer and except we don't need to test for UserCallback. if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0) ch._CmdBuffer.pop_back(); - if (ch._CmdBuffer.Size > 0 && last_cmd != NULL && CanMergeDrawCommands(last_cmd, &ch._CmdBuffer[0])) + + if (ch._CmdBuffer.Size > 0 && last_cmd != NULL) { - // Merge previous channel last draw command with current channel first draw command if matching. - last_cmd->ElemCount += ch._CmdBuffer[0].ElemCount; - idx_offset += ch._CmdBuffer[0].ElemCount; - ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges. + ImDrawCmd* next_cmd = &ch._CmdBuffer[0]; + if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL) + { + // Merge previous channel last draw command with current channel first draw command if matching. + last_cmd->ElemCount += next_cmd->ElemCount; + idx_offset += next_cmd->ElemCount; + ch._CmdBuffer.erase(ch._CmdBuffer.Data); // FIXME-OPT: Improve for multiple merges. + } } if (ch._CmdBuffer.Size > 0) last_cmd = &ch._CmdBuffer.back(); @@ -1403,8 +1441,18 @@ void ImDrawListSplitter::Merge(ImDrawList* draw_list) if (int sz = ch._IdxBuffer.Size) { memcpy(idx_write, ch._IdxBuffer.Data, sz * sizeof(ImDrawIdx)); idx_write += sz; } } draw_list->_IdxWritePtr = idx_write; - draw_list->UpdateClipRect(); // We call this instead of AddDrawCmd(), so that empty channels won't produce an extra draw call. - draw_list->UpdateTextureID(); + + // Ensure there's always a non-callback draw command trailing the command-buffer + if (draw_list->CmdBuffer.Size == 0 || draw_list->CmdBuffer.back().UserCallback != NULL) + draw_list->AddDrawCmd(); + + // If current command is used with different settings we need to add a new command + ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount == 0) + ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset + else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) + draw_list->AddDrawCmd(); + _Count = 1; } @@ -1413,6 +1461,7 @@ void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) IM_ASSERT(idx >= 0 && idx < _Count); if (_Current == idx) return; + // Overwrite ImVector (12/16 bytes), four times. This is merely a silly optimization instead of doing .swap() memcpy(&_Channels.Data[_Current]._CmdBuffer, &draw_list->CmdBuffer, sizeof(draw_list->CmdBuffer)); memcpy(&_Channels.Data[_Current]._IdxBuffer, &draw_list->IdxBuffer, sizeof(draw_list->IdxBuffer)); @@ -1420,6 +1469,13 @@ void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) memcpy(&draw_list->CmdBuffer, &_Channels.Data[idx]._CmdBuffer, sizeof(draw_list->CmdBuffer)); memcpy(&draw_list->IdxBuffer, &_Channels.Data[idx]._IdxBuffer, sizeof(draw_list->IdxBuffer)); draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size; + + // If current command is used with different settings we need to add a new command + ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount == 0) + ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset + else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) + draw_list->AddDrawCmd(); } //----------------------------------------------------------------------------- @@ -2847,7 +2903,7 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c } // Allow wrapping after punctuation. - inside_word = !(c == '.' || c == ',' || c == ';' || c == '!' || c == '?' || c == '\"'); + inside_word = (c != '.' && c != ',' && c != ';' && c != '!' && c != '?' && c != '\"'); } // We ignore blank width at the end of the line (they can be skipped) diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index 96e7b19e..9e0fe99e 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -57,15 +57,16 @@ Index of this file: // Clang/GCC warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h -#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' +#endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h +#pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h #pragma clang diagnostic ignored "-Wold-style-cast" -#if __has_warning("-Wzero-as-null-pointer-constant") #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" -#endif -#if __has_warning("-Wdouble-promotion") #pragma clang diagnostic ignored "-Wdouble-promotion" -#endif +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -435,6 +436,7 @@ struct IMGUI_API ImRect void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); } bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } + ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); } }; // Helper: ImBitArray @@ -1012,7 +1014,8 @@ struct ImGuiColumns float LineMinY, LineMaxY; float HostCursorPosY; // Backup of CursorPos at the time of BeginColumns() float HostCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() - ImRect HostClipRect; // Backup of ClipRect at the time of BeginColumns() + ImRect HostInitialClipRect; // Backup of ClipRect at the time of BeginColumns() + ImRect HostBackupClipRect; // Backup of ClipRect during PushColumnsBackground()/PopColumnsBackground() ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() ImVector Columns; ImDrawListSplitter Splitter; @@ -1561,7 +1564,7 @@ struct IMGUI_API ImGuiWindow ImVec2 ContentSize; // Size of contents/scrollable client area (calculated from the extents reach of the cursor) from previous frame. Does not include window decoration or window padding. ImVec2 ContentSizeExplicit; // Size of contents/scrollable client area explicitly request by the user via SetNextWindowContentSize(). ImVec2 WindowPadding; // Window padding at the time of Begin(). - float WindowRounding; // Window rounding at the time of Begin(). + float WindowRounding; // Window rounding at the time of Begin(). May be clamped lower to avoid rendering artifacts with title bar, menu bar etc. float WindowBorderSize; // Window border size at the time of Begin(). int NameBufLen; // Size of buffer storing Name. May be larger than strlen(Name)! ImGuiID MoveId; // == window->GetID("#MOVE") @@ -1579,7 +1582,7 @@ struct IMGUI_API ImGuiWindow bool WantCollapseToggle; bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed) bool Appearing; // Set during the frame where the window is appearing (or re-appearing) - bool Hidden; // Do not display (== (HiddenFrames*** > 0)) + bool Hidden; // Do not display (== HiddenFrames*** > 0) bool IsFallbackWindow; // Set on the "Debug##Default" window. bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) @@ -1850,7 +1853,9 @@ namespace ImGui IMGUI_API void OpenPopupEx(ImGuiID id); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); - IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id at current popup stack level (currently begin-ed into); this doesn't scan the whole popup stack! + IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id at the current BeginPopup() level of the popup stack (this doesn't scan the whole popup stack!) + IMGUI_API bool IsPopupOpenAtAnyLevel(ImGuiID id); + IMGUI_API bool IsAnyPopupOpen(); // Return true if any popup is open at the current BeginPopup() level of the popup stack IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); @@ -1894,6 +1899,7 @@ namespace ImGui IMGUI_API bool IsDragDropPayloadBeingAccepted(); // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) + IMGUI_API void SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect); IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). IMGUI_API void EndColumns(); // close columns IMGUI_API void PushColumnClipRect(int column_index); diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index 07be3b36..70ab9e3d 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -58,19 +58,19 @@ Index of this file: // Clang/GCC warnings with -Weverything #if defined(__clang__) -#pragma clang diagnostic ignored "-Wold-style-cast" // warning : use of old-style cast // yes, they are more terse. -#pragma clang diagnostic ignored "-Wfloat-equal" // warning : comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. -#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning : format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. -#pragma clang diagnostic ignored "-Wsign-conversion" // warning : implicit conversion changes signedness // -#if __has_warning("-Wzero-as-null-pointer-constant") -#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning : zero as null pointer constant // some standard header variations use #define NULL 0 -#endif -#if __has_warning("-Wdouble-promotion") -#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. -#endif -#if __has_warning("-Wdeprecated-enum-enum-conversion") -#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. +#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') +#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked @@ -557,7 +557,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if ((flags & ImGuiButtonFlags_PressedOnRelease) && mouse_button_released != -1) { // Repeat mode trumps on release behavior - if (!((flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay)) + const bool has_repeated_at_least_once = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; + if (!has_repeated_at_least_once) pressed = true; ClearActiveID(); } @@ -3469,18 +3470,22 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Generic named filters if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) { + // Allow 0-9 . - + * / if (flags & ImGuiInputTextFlags_CharsDecimal) if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/')) return false; + // Allow 0-9 . - + * / e E if (flags & ImGuiInputTextFlags_CharsScientific) if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E')) return false; + // Allow 0-9 a-F A-F if (flags & ImGuiInputTextFlags_CharsHexadecimal) if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return false; + // Turn a-z into A-Z if (flags & ImGuiInputTextFlags_CharsUppercase) if (c >= 'a' && c <= 'z') *p_char = (c += (unsigned int)('A'-'a')); @@ -4283,7 +4288,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ PopFont(); // Log as text - if (g.LogEnabled && !(is_password && !is_displaying_hint)) + if (g.LogEnabled && (!is_password || is_displaying_hint)) LogRenderedText(&draw_pos, buf_display, buf_display_end); if (label_size.x > 0) @@ -4430,7 +4435,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupContextItem("context"); } } else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) @@ -4455,7 +4460,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupContextItem("context"); } ImGuiWindow* picker_active_window = NULL; @@ -4476,7 +4481,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupContextItem("context"); if (BeginPopup("picker")) { @@ -4696,7 +4701,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupContextItem("context"); } else if (flags & ImGuiColorEditFlags_PickerHueBar) { @@ -4709,7 +4714,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupContextItem("context"); // Hue bar logic SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); @@ -7157,7 +7162,9 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, if (tab_bar->Tabs.Size == 1 && !(tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs)) tab_contents_visible = true; - if (tab_appearing && !(tab_bar_appearing && !tab_is_new)) + // Note that tab_is_new is not necessarily the same as tab_appearing! When a tab bar stops being submitted + // and then gets submitted again, the tabs will have 'tab_appearing=true' but 'tab_is_new=false'. + if (tab_appearing && (!tab_bar_appearing || tab_is_new)) { PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); ItemAdd(ImRect(), id); @@ -7268,14 +7275,16 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) - if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip)) + if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); return tab_contents_visible; } // [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed. -// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem() +// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem(). +// Tabs closed by the close button will automatically be flagged to avoid this issue. +// FIXME: We should aim to support calling SetTabItemClosed() after the tab submission (for next frame) void ImGui::SetTabItemClosed(const char* label) { ImGuiContext& g = *GImGui; @@ -7398,6 +7407,7 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, // [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc. // In the current version, Columns are very weak. Needs to be replaced with a more full-featured system. //------------------------------------------------------------------------- +// - SetWindowClipRectBeforeSetChannel() [Internal] // - GetColumnIndex() // - GetColumnCount() // - GetColumnOffset() @@ -7415,6 +7425,18 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, // - Columns() //------------------------------------------------------------------------- +// [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences, +// they would meddle many times with the underlying ImDrawCmd. +// Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let +// the subsequent single call to SetCurrentChannel() does it things once. +void ImGui::SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect) +{ + ImVec4 clip_rect_vec4 = clip_rect.ToVec4(); + window->ClipRect = clip_rect; + window->DrawList->_CmdHeader.ClipRect = clip_rect_vec4; + window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 1] = clip_rect_vec4; +} + int ImGui::GetColumnIndex() { ImGuiWindow* window = GetCurrentWindowRead(); @@ -7549,11 +7571,11 @@ void ImGui::PushColumnsBackground() ImGuiColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; + + // Optimization: avoid SetCurrentChannel() + PushClipRect() + columns->HostBackupClipRect = window->ClipRect; + SetWindowClipRectBeforeSetChannel(window, columns->HostInitialClipRect); columns->Splitter.SetCurrentChannel(window->DrawList, 0); - int cmd_size = window->DrawList->CmdBuffer.Size; - PushClipRect(columns->HostClipRect.Min, columns->HostClipRect.Max, false); - IM_UNUSED(cmd_size); - IM_ASSERT(cmd_size == window->DrawList->CmdBuffer.Size); // Being in channel 0 this should not have created an ImDrawCmd } void ImGui::PopColumnsBackground() @@ -7562,8 +7584,10 @@ void ImGui::PopColumnsBackground() ImGuiColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; + + // Optimization: avoid PopClipRect() + SetCurrentChannel() + SetWindowClipRectBeforeSetChannel(window, columns->HostBackupClipRect); columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); - PopClipRect(); } ImGuiColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) @@ -7611,7 +7635,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag columns->HostCursorPosY = window->DC.CursorPos.y; columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; - columns->HostClipRect = window->ClipRect; + columns->HostInitialClipRect = window->ClipRect; columns->HostWorkRect = window->WorkRect; // Set state for first column @@ -7683,25 +7707,31 @@ void ImGui::NextColumn() IM_ASSERT(columns->Current == 0); return; } + + // Next column + if (++columns->Current == columns->Count) + columns->Current = 0; + PopItemWidth(); - PopClipRect(); + + // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect() + // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them), + ImGuiColumnData* column = &columns->Columns[columns->Current]; + SetWindowClipRectBeforeSetChannel(window, column->ClipRect); + columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); const float column_padding = g.Style.ItemSpacing.x; columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); - if (++columns->Current < columns->Count) + if (columns->Current > 0) { // Columns 1+ ignore IndentX (by canceling it out) // FIXME-COLUMNS: Unnecessary, could be locked? window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding; - columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); } else { - // New row/line - // Column 0 honor IndentX + // New row/line: column 0 honor IndentX. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - columns->Splitter.SetCurrentChannel(window->DrawList, 1); - columns->Current = 0; columns->LineMinY = columns->LineMaxY; } window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); @@ -7709,8 +7739,6 @@ void ImGui::NextColumn() window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = 0.0f; - PushColumnClipRect(columns->Current); // FIXME-COLUMNS: Could it be an overwrite? - // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup. float offset_0 = GetColumnOffset(columns->Current); float offset_1 = GetColumnOffset(columns->Current + 1); From 3ccf5f052e866b16795c342bb06c8207260510c0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 11:19:00 +0200 Subject: [PATCH 0301/1018] create directory save/load views --- src/viewer/viewer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index eeeb3a89..17537700 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -397,9 +397,11 @@ bool viewer::menu_save_load_view(viewer * view) static char file[128] = "view"; ImGui::InputText("file", file, sizeof(file)); - + if(ImGui::Button("Save")) { + filesystem::create_directory(tmp_file_path("views/")); + ofstream os(tmp_file_path("views/" + file)); os << view->cam; os.close(); From 235a68743899eaf3a286ae9fdeabeb6c29a40073 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 15:13:15 +0200 Subject: [PATCH 0302/1018] add load view camera status --- src/viewer/viewer.cpp | 44 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 17537700..3f2f5447 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -148,7 +148,7 @@ bool viewer::run() process_t & pro = p.second; if(pro.selected) { - ImGui::SetNextWindowSize(ImVec2(300, -1)); + ImGui::SetNextWindowSize(ImVec2(320, -1)); ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); @@ -394,28 +394,56 @@ bool viewer::menu_help(viewer * view) bool viewer::menu_save_load_view(viewer * view) { - static char file[128] = "view"; - - ImGui::InputText("file", file, sizeof(file)); + filesystem::create_directory(tmp_file_path("views/")); + + static char file[128] = "new_view"; + + ImGui::InputText("##savefile", file, sizeof(file)); + + ImGui::SameLine(); + if(ImGui::Button("Save")) { - filesystem::create_directory(tmp_file_path("views/")); - ofstream os(tmp_file_path("views/" + file)); os << view->cam; os.close(); } - ImGui::SameLine(); + static index_t select = 0; + static vector vfiles; + + vfiles.clear(); + for(auto & p: filesystem::directory_iterator(tmp_file_path("views/"))) + vfiles.push_back(p.path().string()); + + if(!vfiles.size()) return true; + + if(ImGui::BeginCombo("##loadfile", vfiles[select].c_str())) + { + for(index_t i = 0; i < vfiles.size(); i++) + { + if(ImGui::Selectable(vfiles[i].c_str(), select == i)) + select = i; + + if(select == i) + ImGui::SetItemDefaultFocus(); + } + + ImGui::EndCombo(); + } + + ImGui::SameLine(); + if(ImGui::Button("Load")) { - ifstream is(tmp_file_path("views/" + file)); + ifstream is(vfiles[select]); is >> view->cam; is.close(); } + return true; } From ff96dcf945c205a77c82c6c84c853ee39f5dfb01 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 16:23:13 +0200 Subject: [PATCH 0303/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe79e7c4..57c4e9d8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | Install all dependencies and run: From 401e1ca88f1b9ad306ffced0d6a5d1c5ce47e10e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 16:25:19 +0200 Subject: [PATCH 0304/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe79e7c4..fbedd3f6 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=alpha)](https://travis-ci.com/larc/gproshan) | Install all dependencies and run: From ef1fb57d8cc991f1ec0d910284f056c4aaa2ec8f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 16:26:07 +0200 Subject: [PATCH 0305/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe79e7c4..0e319bac 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=mdict)](https://travis-ci.com/larc/gproshan) | Install all dependencies and run: From 26ab8a1f3fd3cace871e3f9d2bc8f07cdd293750 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 16:52:17 +0200 Subject: [PATCH 0306/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e319bac..da0b7dac 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=mdict)](https://travis-ci.com/larc/gproshan) | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=mdict)](https://travis-ci.com/larc/gproshan_dev) | Install all dependencies and run: From f03ddc65f8cb6fc7559c73866d2aa31f68dfcf2c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jun 2020 16:52:45 +0200 Subject: [PATCH 0307/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe79e7c4..f6239e69 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan.svg?branch=raytracing)](https://travis-ci.com/larc/gproshan) | +| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=raytracing)](https://travis-ci.com/larc/gproshan_dev) | Install all dependencies and run: From f9a48093f5ac0f93a189527972398de0134915df Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jun 2020 16:45:07 +0200 Subject: [PATCH 0308/1018] che_off: read and write noff files --- include/che_off.h | 7 +++++-- src/che.cpp | 7 +++++-- src/che_off.cpp | 40 ++++++++++++++++++++++++++++++---------- src/viewer/viewer.cpp | 10 +++++++--- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/include/che_off.h b/include/che_off.h index 5f819517..300930e0 100644 --- a/include/che_off.h +++ b/include/che_off.h @@ -14,13 +14,16 @@ class che_off : public che che_off(const std::string & file); che_off(const che_off & mesh); virtual ~che_off(); - - static void write_file(const che * mesh, const std::string & file); + + enum type { OFF, NOFF, COFF, CNOFF }; + static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool & pointcloud = false); private: void read_file(const std::string & file); }; +std::ostream & operator << (std::ostream & os, const che_off::type & off); + } // namespace gproshan diff --git a/src/che.cpp b/src/che.cpp index b320b6b2..9ceeb12c 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -278,15 +278,18 @@ real_t & che::color(const index_t & v) void che::update_normals() { - if(!VN) VN = new vertex[n_vertices_]; + if(VN) return; // normals was already loaded/computed + + VN = new vertex[n_vertices_]; - // point cloud normals + /* point cloud normals if(!n_faces_) { kdtree rnn(GT, n_vertices_); return; } + */ #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) diff --git a/src/che_off.cpp b/src/che_off.cpp index a50f70a0..645d507b 100644 --- a/src/che_off.cpp +++ b/src/che_off.cpp @@ -78,27 +78,47 @@ void che_off::read_file(const string & file) is.close(); } -void che_off::write_file(const che * mesh, const string & file) +void che_off::write_file(const che * mesh, const string & file, const che_off::type & off, const bool & pointcloud) { ofstream os(file + ".off"); - os << "OFF" << endl; - os << mesh->n_vertices() << " " << mesh->n_faces() << " 0" << endl; - + os << off << endl; + os << mesh->n_vertices() << " " << (pointcloud ? 0 : mesh->n_faces()) << " 0" << endl; + for(size_t v = 0; v < mesh->n_vertices(); v++) - os << mesh->gt(v) << endl; - - for(index_t he = 0; he < mesh->n_half_edges(); ) { - os << che::P; - for(index_t i = 0; i < che::P; i++) - os << " " << mesh->vt(he++); + os << mesh->gt(v); + + if(off == NOFF) os << " " << mesh->normal(v); // NOFF file + os << endl; } + + if(!pointcloud) + for(index_t he = 0; he < mesh->n_half_edges(); ) + { + os << che::P; + for(index_t i = 0; i < che::P; i++) + os << " " << mesh->vt(he++); + os << endl; + } os.close(); } +ostream & operator << (ostream & os, const che_off::type & off) +{ + switch(off) + { + case che_off::OFF : os << "OFF"; break; + case che_off::NOFF : os << "NOFF"; break; + case che_off::COFF : os << "COFF"; break; + case che_off::CNOFF : os << "CNOFF"; break; + } + + return os; +} + } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 3f2f5447..8cb3f5c9 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -462,17 +462,21 @@ bool viewer::menu_reset_mesh(viewer * view) bool viewer::menu_save_mesh(viewer * view) { + const che * mesh = view->active_mesh(); + static char file[128] = "copy"; static int format = 0; + static bool pc = false; ImGui::InputText("file", file, sizeof(file)); ImGui::Combo("format", &format, ".off\0.obj\0.ply\0\0"); + ImGui::Checkbox("point cloud", &pc); if(ImGui::Button("Save")) { - if(format == 0) che_off::write_file(view->active_mesh(), file); - if(format == 1) che_obj::write_file(view->active_mesh(), file); - if(format == 2) che_ply::write_file(view->active_mesh(), file); + if(format == 0) che_off::write_file(mesh, file, che_off::NOFF, pc); + if(format == 1) che_obj::write_file(mesh, file); + if(format == 2) che_ply::write_file(mesh, file); } return true; From ee6ccc6fb345d2c1b57fdb747fde7efc3e793530 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jun 2020 17:03:30 +0200 Subject: [PATCH 0309/1018] add render pointcloud menu --- include/che.h | 1 + include/viewer/viewer.h | 2 ++ src/che.cpp | 5 +++++ src/viewer/viewer.cpp | 12 ++++++++++-- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/che.h b/include/che.h index ba6ca0d4..b785a7e8 100644 --- a/include/che.h +++ b/include/che.h @@ -87,6 +87,7 @@ class che size_t genus() const; void normalize(); + bool is_pointcloud() const; bool is_manifold() const; const index_t & vt(const index_t & he) const; const vertex & gt(const index_t & v) const; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 92983deb..c508c438 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -96,6 +96,7 @@ class viewer bool action = false; + bool render_pointcloud = false; bool render_wireframe = false; bool render_wireframe_fill = false; bool render_gradient_field = false; @@ -164,6 +165,7 @@ class viewer static bool set_render_gl(viewer * view); static bool set_render_embree(viewer * view); static bool set_render_optix(viewer * view); + static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); static bool set_render_wireframe_fill(viewer * view); static bool set_render_gradient_field(viewer * view); diff --git a/src/che.cpp b/src/che.cpp index 9ceeb12c..aec32c12 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -454,6 +454,11 @@ void che::normalize() GT[v] /= max_norm; } +bool che::is_pointcloud() const +{ + return n_faces_ == 0; +} + bool che::is_manifold() const { return manifold; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 8cb3f5c9..255e6549 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -249,6 +249,7 @@ void viewer::init_menus() add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); sub_menus.push_back("Render"); + add_process(GLFW_KEY_F5, {"F5", "Render Point Cloud", set_render_pointcloud}); add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); add_process(GLFW_KEY_F7, {"F7", "Render Wireframe Fill", set_render_wireframe_fill}); add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); @@ -263,7 +264,7 @@ void viewer::init_menus() add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradient_field}); add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normal_field}); - add_process(GLFW_KEY_F5, {"F5", "Select Border Vertices", set_render_border}); + add_process(GLFW_KEY_APOSTROPHE, {"APOSTROPHE", "Select Border Vertices", set_render_border}); add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); } @@ -560,6 +561,13 @@ bool viewer::set_render_optix(viewer * view) return false; } +bool viewer::set_render_pointcloud(viewer * view) +{ + view->render_pointcloud = !view->render_pointcloud; + + return false; +} + bool viewer::set_render_wireframe(viewer * view) { view->render_wireframe = !view->render_wireframe; @@ -753,7 +761,7 @@ void viewer::draw_meshes(shader & program) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - if(!meshes[i]->n_faces()) + if(meshes[i]->is_pointcloud() || render_pointcloud) meshes[i].draw_point_cloud(shader_pointcloud); else meshes[i].draw(program); From c0ce9e167191f870c5523679474468c15cbfcfbe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jun 2020 18:34:18 +0200 Subject: [PATCH 0310/1018] render normals per point, bug for point cloud files --- include/viewer/viewer.h | 2 +- shaders/geometry_normals.glsl | 17 +++++------------ src/che.cpp | 2 +- src/viewer/che_viewer.cpp | 2 +- src/viewer/viewer.cpp | 8 +++++--- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index c508c438..4e0dc5cf 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -177,7 +177,7 @@ class viewer static bool raycasting(viewer * view); // draw routines - void draw_meshes(shader & program); + void draw_meshes(shader & program, const bool & normals = false); void draw_selected_vertices(shader & program); void select_border_vertices(); diff --git a/shaders/geometry_normals.glsl b/shaders/geometry_normals.glsl index 1627d464..ebf9e636 100644 --- a/shaders/geometry_normals.glsl +++ b/shaders/geometry_normals.glsl @@ -1,27 +1,20 @@ #version 410 core -layout (triangles) in; -layout (line_strip, max_vertices = 6) out; +layout (points) in; +layout (line_strip, max_vertices = 2) out; in vec3 normal[]; uniform float length; -void line_normal(int i) +void main() { - gl_Position = gl_in[i].gl_Position; + gl_Position = gl_in[0].gl_Position; EmitVertex(); - gl_Position = gl_in[i].gl_Position + vec4(normal[i], 0.) * length; + gl_Position = gl_in[0].gl_Position + vec4(normal[0], 0.) * length; EmitVertex(); EndPrimitive(); } -void main() -{ - line_normal(0); - line_normal(1); - line_normal(2); -} - diff --git a/src/che.cpp b/src/che.cpp index aec32c12..491c0374 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -279,7 +279,7 @@ real_t & che::color(const index_t & v) void che::update_normals() { if(VN) return; // normals was already loaded/computed - + VN = new vertex[n_vertices_]; /* point cloud normals diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index de14be19..987ba95a 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -82,7 +82,7 @@ void che_viewer::update_vbo() glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 INDEXES - if(mesh->n_faces()) + if(!mesh->is_pointcloud()) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges() * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 255e6549..3009ff43 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -677,7 +677,7 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - draw_meshes(shader_normals); + draw_meshes(shader_normals, true); } @@ -749,7 +749,7 @@ void viewer::render_optix() #endif // GPROSHAN_OPTIX } -void viewer::draw_meshes(shader & program) +void viewer::draw_meshes(shader & program, const bool & normals) { if(render_wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); @@ -761,7 +761,9 @@ void viewer::draw_meshes(shader & program) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - if(meshes[i]->is_pointcloud() || render_pointcloud) + if(normals) + meshes[i].draw_point_cloud(program); + else if(meshes[i]->is_pointcloud() || render_pointcloud) meshes[i].draw_point_cloud(shader_pointcloud); else meshes[i].draw(program); From 44ae4204b8945c4cfce5ba9b821527f1fe542985 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jun 2020 18:47:28 +0200 Subject: [PATCH 0311/1018] length normal and gradient field vectors --- src/viewer/viewer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 3009ff43..3a0e0155 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -673,7 +673,7 @@ void viewer::render_gl() if(render_normal_field) { - glProgramUniform1f(shader_normals, shader_normals("length"), active_mesh().factor); + glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom * 0.02); glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); @@ -683,7 +683,7 @@ void viewer::render_gl() if(render_gradient_field) { - glProgramUniform1f(shader_gradient, shader_gradient("length"), active_mesh().factor); + glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom * 0.02); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); From 9b3f1c618f5bd63eb86e874a20694087e9318a3e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jun 2020 20:50:48 +0200 Subject: [PATCH 0312/1018] fix rt_embree reference no embree found, fix merge is_pointcloud --- include/che.h | 1 - src/che.cpp | 5 ----- src/viewer/viewer.cpp | 2 ++ 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/include/che.h b/include/che.h index 42ba0a23..b785a7e8 100644 --- a/include/che.h +++ b/include/che.h @@ -89,7 +89,6 @@ class che void normalize(); bool is_pointcloud() const; bool is_manifold() const; - bool is_pointcloud() const; const index_t & vt(const index_t & he) const; const vertex & gt(const index_t & v) const; const vertex & gt_vt(const index_t & he) const; diff --git a/src/che.cpp b/src/che.cpp index b9d09757..491c0374 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -464,11 +464,6 @@ bool che::is_manifold() const return manifold; } -bool che::is_pointcloud() const -{ - return n_faces_ == 0; -} - const index_t & che::vt(const index_t & he) const { assert(he < n_half_edges_); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index bf276898..6a75cae6 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -109,6 +109,7 @@ bool viewer::run() ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); + #ifdef GPROSHAN_EMBREE if(!rt_embree) { ImGui::SetNextWindowSize(ImVec2(300, -1)); @@ -117,6 +118,7 @@ bool viewer::run() ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.4f"); ImGui::End(); } + #endif // GPROSHAN_EMBREE if(ImGui::BeginMainMenuBar()) { From beeebc62be262b1ae7c2f5cc6076675f0de30776 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 24 Jun 2020 11:46:04 +0200 Subject: [PATCH 0313/1018] remove PI, use M_PI --- include/mdict/inpainting.h | 8 ++++---- src/app_viewer.cpp | 7 +++---- src/mdict/inpainting.cpp | 5 +++-- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 14 +++++++------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 0a78249e..2086fdef 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -2,13 +2,13 @@ #define INPAINTING_H #include "dictionary.h" -#include "../che_poisson.h" -#include "../che_fill_hole.h" +#include "che_poisson.h" +#include "che_fill_hole.h" #include "sampling.h" #include "geodesics.h" #include "geodesics_ptp.h" + #include -#define PI 3.14159265 // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -19,7 +19,7 @@ class inpainting : public dictionary { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, - const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 0, double _delta=PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); + const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 0, double _delta = M_PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); virtual ~inpainting() = default; real_t execute(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2ba61e77..169c9738 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -1,7 +1,6 @@ #include "app_viewer.h" #include -#define PI 3.14159265 using namespace std; using namespace gproshan::mdict; @@ -608,7 +607,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) static bool learn = 0; static int avg_p = 36; static int percentage = 0; - static float delta = PI/6; + static float delta = M_PI/6; static float sum_thres = 1.01; static float area_thres = 0.005; @@ -645,7 +644,7 @@ bool app_viewer::process_mask(viewer * p_view) static bool learn = 0; static int avg_p = 36; static int percentage = 0; - static float delta = PI/6; + static float delta = M_PI/6; static float sum_thres = 1.01; static float area_thres = 0.005; @@ -690,7 +689,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static bool learn = 0; static int avg_p = 36; static int percentage = 0; - static float delta = PI/6; + static float delta = M_PI/6; static float sum_thres = 1.01; static float area_thres = 0.005; static float percentage_size = 100; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index c5b74a06..950caf6b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,11 +1,12 @@ #include "inpainting.h" + +#include "che_off.h" + #include #include #include #include #include -#include "che_off.h" -#define PI 3.14159265 // geometry processing and shape analysis framework diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 347afc23..fab6300a 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,7 +243,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(int i = 0; i < A.n_cols; i++) - if( phi_basis->get_frequency(i) >= 0*p.avg_dist) //2.5* if it ismin + if(phi_basis->get_frequency(i) >= 0*p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index c7ed1dcc..40d077b2 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -4,6 +4,9 @@ #include "che_sphere.h" #include "che_off.h" +#include +#include + #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS #define CGAL_EIGEN3_ENABLED @@ -14,9 +17,6 @@ #include #include -#define PI 3.14159265 -#include -#include // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -84,7 +84,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N index_t a, b, i = 0; vertex min_he; double area_face = 0, proj_area_face = 0; - double angle = PI; + double angle = M_PI; double tmp_angle; bool added = false; vertex pav, pbv, va, vb,vv; @@ -171,7 +171,7 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, r for(size_t i = 1; i < n_points; i++) { - double a = abs(dis(gen)) * 2 * PI; + double a = abs(dis(gen)) * 2 * M_PI; double r = fr * abs(dis(gen)); xyz(0, i) = r * cos(a); @@ -261,7 +261,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re c = mesh->get_vertex(v); // central vertices ratio = (i==1)? 0:(area/proj_area); - if( add_vertex_by_faces(c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) + if( add_vertex_by_faces(c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) { //compute euclidean radio p = mesh->get_vertex(indexes[i]); @@ -382,7 +382,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co // add new vertices // create a random point - real_t a = abs(dis(gen)) * 2 * PI; + real_t a = abs(dis(gen)) * 2 * M_PI; real_t r = abs(dis(gen)); a_vec np = { r * cos(a), r * sin(a), 0 }; From 10ee287266a88442e3d98fae26f1e02b3bde64ae Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 24 Jun 2020 19:27:21 +0200 Subject: [PATCH 0314/1018] che: update_color input max_color --- include/che.h | 2 +- src/che.cpp | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/che.h b/include/che.h index b785a7e8..5be94b4c 100644 --- a/include/che.h +++ b/include/che.h @@ -68,7 +68,7 @@ class che real_t real_trig(const index_t & t) const; real_t area_vertex(const index_t & v); real_t area_surface() const; - void update_colors(const real_t * vcolor = nullptr); + void update_colors(const real_t * vcolor = nullptr, real_t max_color = 0); const real_t & color(const index_t & v) const; real_t & color(const index_t & v); void update_normals(); diff --git a/src/che.cpp b/src/che.cpp index 491c0374..b2a9a94b 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -239,7 +239,7 @@ real_t che::area_surface() const return area; } -void che::update_colors(const real_t * vcolor) +void che::update_colors(const real_t * vcolor, real_t max_color) { if(!VC) VC = new real_t[n_vertices_]; @@ -251,17 +251,18 @@ void che::update_colors(const real_t * vcolor) return; } - - real_t max_c = 0; - #pragma omp parallel for reduction(max: max_c) - for(index_t v = 0; v < n_vertices_; v++) - if(vcolor[v] < INFINITY) - max_c = max(vcolor[v], max_c); + if(max_color > numeric_limits::epsilon()) + { + #pragma omp parallel for reduction(max: max_color) + for(index_t v = 0; v < n_vertices_; v++) + if(vcolor[v] < INFINITY) + max_color = max(vcolor[v], max_color); + } #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VC[v] = vcolor[v] / max_c; + VC[v] = vcolor[v] / max_color; } const real_t & che::color(const index_t & v) const From 897f2a1c2a291dbe8c47fd472352be0a372fa5a5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 24 Jun 2020 19:34:29 +0200 Subject: [PATCH 0315/1018] inpainting max error colored --- src/app_viewer.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 169c9738..ef37c63f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -611,7 +611,6 @@ bool app_viewer::process_inpaiting(viewer * p_view) static float sum_thres = 1.01; static float area_thres = 0.005; - ImGui::InputInt("basis", &n); ImGui::InputInt("atoms", &m); ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); @@ -621,14 +620,14 @@ bool app_viewer::process_inpaiting(viewer * p_view) if(ImGui::Button("Run")) { - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); + basis_dct phi(n); + inpainting dict(mesh, &phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); dict.execute(); - delete phi; - mesh->update_colors(&dict[0]); + mesh->update_colors(&dict[0], 0.003); mesh->update_normals(); } + return true; } From ff66e0a1f393ecf431f4103d3afaa1819bf59c47 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 25 Jun 2020 19:01:36 +0200 Subject: [PATCH 0316/1018] add dictionary::params struct, cleaning up inpainting constructor imgui inputs --- include/mdict/dictionary.h | 17 +++++- include/mdict/inpainting.h | 3 +- src/app_viewer.cpp | 120 +++++++++++++++++-------------------- src/mdict/inpainting.cpp | 18 +++--- 4 files changed, 80 insertions(+), 78 deletions(-) diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 9845ef90..792364af 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -17,6 +17,21 @@ namespace gproshan::mdict { class dictionary { + public: + struct params + { + size_t m = 144; ///< number of dictionary atoms + size_t M = 0; ///< number of patches + size_t avg_p = 36; ///< avg number of vertices per patch + size_t percentage = 0; ///< mask percentage + real_t f = 1; ///< + real_t delta = M_PI / 6; ///< + real_t sum_thres = 1.01; ///< + real_t area_thres = 0.005; ///< + bool learn = false; ///< + bool plot = false; + }; + protected: che * mesh; ///< input mesh. size_t n_vertices; ///< number of vertices. @@ -29,7 +44,7 @@ class dictionary a_mat alpha; ///< sparse coding matrix. real_t f; - real_t s_radio; ///< sampling geodesic radio. + real_t s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 2086fdef..1e0746ab 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -18,8 +18,7 @@ namespace gproshan::mdict { class inpainting : public dictionary { public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, - const real_t & _f, const bool & _learn, size_t _avg_p = 36, size_t _perc = 0, double _delta = M_PI/6, double _sum_thres = 0.001 , double _area_thres = 0.001, const bool & _plot = false); + inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p); virtual ~inpainting() = default; real_t execute(); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index ef37c63f..92dec188 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -600,28 +600,22 @@ bool app_viewer::process_inpaiting(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int n = 12; // dct - static int m = 144; - static int M = 0; - static float f = 1; - static bool learn = 0; - static int avg_p = 36; - static int percentage = 0; - static float delta = M_PI/6; - static float sum_thres = 1.01; - static float area_thres = 0.005; - - ImGui::InputInt("basis", &n); - ImGui::InputInt("atoms", &m); - ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); - ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); - ImGui::Checkbox("learn", &learn); + static dictionary::params params; + static size_t n = 12; + + assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); + + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) { basis_dct phi(n); - inpainting dict(mesh, &phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); + inpainting dict(mesh, &phi, params); dict.execute(); mesh->update_colors(&dict[0], 0.003); @@ -636,43 +630,41 @@ bool app_viewer::process_mask(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int n = 12; // dct - static int m = 144; - static int M = 0; - static float f = 1; - static bool learn = 0; - static int avg_p = 36; - static int percentage = 0; - static float delta = M_PI/6; - static float sum_thres = 1.01; - static float area_thres = 0.005; - - ImGui::InputInt("basis", &n); - ImGui::InputInt("atoms", &m); - ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); - ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); - ImGui::Checkbox("learn", &learn); + static dictionary::params params; + static size_t n = 12; + + assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); + + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) { - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn, avg_p, percentage, delta, sum_thres, area_thres); + basis_dct phi(n); + inpainting dict(mesh, &phi, params); dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); - delete phi; mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + to_string(sum_thres) +"_" + to_string(area_thres) + ".points"); + string f_points = tmp_file_path(mesh->name_size() + "_" + + to_string(params.sum_thres) + "_" + + to_string(params.area_thres) + ".points"); + a_vec points_out; gproshan_debug_var(f_points); points_out.load(f_points); gproshan_debug_var(points_out.size()); - for(int i = 0; i< points_out.size(); i++) + + for(index_t i = 0; i < points_out.size(); i++) mesh.selected.push_back(points_out(i)); mesh->update_normals(); } + return true; } @@ -681,34 +673,29 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int n = 12; // dct - static int m = 144; - static int M = 0; - static float f = 1; - static bool learn = 0; - static int avg_p = 36; - static int percentage = 0; - static float delta = M_PI/6; - static float sum_thres = 1.01; - static float area_thres = 0.005; - static float percentage_size = 100; - static float radio_factor = 1; - - ImGui::InputInt("basis", &n); - ImGui::InputInt("atoms", &m); - ImGui::InputFloat("delta", &delta, 0.001, 0.1, 3); - ImGui::InputFloat("proj_thres", &sum_thres, 1.001, 0.1, 6); - ImGui::InputFloat("area_thres", &area_thres, 0.001, 0.1, 6); - ImGui::InputFloat("percentage_size", &percentage_size, 100, 10, 6); - ImGui::InputFloat("radio_factor", &radio_factor, 1, 0.1, 6); - ImGui::Checkbox("learn", &learn); + static dictionary::params params; + static size_t n = 12; + static real_t percentage_size = 100; + static real_t radio_factor = 1; + + assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); + + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::Checkbox("learn", ¶ms.learn); + + ImGui::InputDouble("percentage_size", &percentage_size, 100, 10, "%.3f"); + ImGui::InputDouble("radio_factor", &radio_factor, 1, 0.1, "%.3f"); if(ImGui::Button("Run")) { basis_dct phi(n); - inpainting dict(mesh, &phi, m, M, f, learn, avg_p, percentage, delta, sum_thres,area_thres); + inpainting dict(mesh, &phi, params); - view->add_mesh({dict.point_cloud_reconstruction(percentage_size,radio_factor)}); + view->add_mesh({dict.point_cloud_reconstruction(percentage_size, radio_factor)}); } return true; @@ -720,6 +707,8 @@ bool app_viewer::process_synthesis(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); + static dictionary::params params; + size_t n; // dct size_t m, M; real_t f; @@ -728,11 +717,10 @@ bool app_viewer::process_synthesis(viewer * p_view) gproshan_log(parameters: (n, m, M, f, learn)); cin >> n >> m >> M >> f >> learn; - basis * phi = new basis_dct(n); - inpainting dict(mesh, phi, m, M, f, learn); + basis_dct phi(n); + inpainting dict(mesh, &phi, params); dict.execute(); - delete phi; mesh->update_normals(); return false; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 950caf6b..18dd3659 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -14,17 +14,17 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, - const bool & _learn, size_t _avg_p, size_t _perc, double _delta, double _sum_thres, double _area_thres, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): dictionary(_mesh, _phi_basis, p.m, p.M, p.f, p.learn, p.plot) { - delta = _delta; - sum_thres = _sum_thres; - avg_p = _avg_p; //size avg of number of vertices per patch - percent = _perc; // mask percentage - area_thres = _area_thres; - M = mesh->n_vertices()/avg_p; + delta = p.delta; + sum_thres = p.sum_thres; + avg_p = p.avg_p; + percent = p.percentage; + area_thres = p.area_thres; + + M = mesh->n_vertices() / avg_p; mask = new bool[mesh->n_vertices()]; - memset(mask,0,sizeof(bool)*mesh->n_vertices()); + memset(mask, 0, sizeof(bool) * mesh->n_vertices()); } From b5bfc6eb9e968ec77fd160ae1c1e1fc405d58749 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 25 Jun 2020 21:51:07 +0200 Subject: [PATCH 0317/1018] create struct params for geodesics --- include/geodesics.h | 42 ++++++++++++++++++++++---------------- src/app_viewer.cpp | 26 +++++++++++++++-------- src/che.cpp | 2 +- src/geodesics.cpp | 14 ++++++------- src/sampling.cpp | 13 ++++++------ src/test_geodesics_ptp.cpp | 4 ++-- 6 files changed, 58 insertions(+), 43 deletions(-) diff --git a/include/geodesics.h b/include/geodesics.h index 1a81c072..463d6eb8 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -18,35 +18,41 @@ namespace gproshan { class geodesics { public: - enum option_t { FM, ///< Execute Fast Marching algorithm - #ifdef GPROSHAN_CUDA - PTP_GPU, ///< Execute Parallel Toplesets Propagation algorithm on GPU - HEAT_FLOW_GPU, ///< Execute Heat Flow - cusparse (GPU) - #endif // GPROSHAN_CUDA - PTP_CPU, ///< Execute Parallel Toplesets Propagation algorithm on CPU - HEAT_FLOW ///< Execute Heat Flow - cholmod (CPU) + enum algorithm { FM, ///< Execute Fast Marching algorithm + #ifdef GPROSHAN_CUDA + PTP_GPU, ///< Execute Parallel Toplesets Propagation GPU algorithm + HEAT_FLOW_GPU, ///< Execute Heat Flow - cusparse (GPU) + #endif // GPROSHAN_CUDA + PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm + HEAT_FLOW ///< Execute Heat Flow - cholmod (CPU) }; + + struct params + { + algorithm alg = FM; ///< specific the algorithm to execute. + size_t n_iter = 0; ///< maximum number of iterations. + real_t radio = INFINITY; ///< execute until the specific radio. + real_t * dist_alloc = nullptr; ///< external dist allocation + bool cluster = false; ///< to cluster vertices to closest source. + bool (*fun) (const index_t & u) = nullptr; ///< fun is executed inside FM loop + }; public: index_t * clusters; ///< Clustering vertices to closest source. private: - real_t * dist; ///< Results of computation geodesic distances. + real_t * dist; ///< Results of computation geodesic distances. index_t * sorted_index; ///< Sort vertices by topological level or geodesic distance. size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; - const size_t & n_vertices; ///< Number of vertices, constance reference + const size_t & n_vertices; ///< Number of vertices, const reference public: - geodesics(che * mesh, ///< input mesh must be a triangular mesh. - const std::vector & sources, ///< source vertices. - const option_t & opt = FM, ///< specific the algorithm to execute. - real_t *const & e_dist = nullptr, ///< external dist allocation - const bool & cluster = false, ///< to cluster vertices to closest source. - const size_t & n_iter = 0, ///< maximum number of iterations. - const real_t & radio = INFINITY ///< execute until the specific radio. - ); + geodesics( che * mesh, ///< input triangular mesh. + const std::vector & sources, ///< source vertices. + const params & p = {FM, 0, INFINITY, nullptr, false, nullptr} + ); virtual ~geodesics(); const real_t & operator[](const index_t & i) const; @@ -58,7 +64,7 @@ class geodesics void normalize(); private: - void execute(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const option_t & opt); + void execute(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const algorithm & alg); void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow(che * mesh, const std::vector & sources); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 727e16be..2d3f3f50 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -670,14 +670,18 @@ bool app_viewer::process_voronoi(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - - TIC(view->time) + + geodesics::params params; + params.cluster = true; + #ifdef GPROSHAN_CUDA - geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_GPU, nullptr, 1); -#else - geodesics ptp(mesh, view->active_mesh().selected, geodesics::FM, nullptr, 1); + params.alg = geodesics::PTP_GPU; #endif + + TIC(view->time) + geodesics ptp(mesh, mesh.selected, params); TOC(view->time) + gproshan_log_var(view->time); #pragma omp parallel for @@ -806,7 +810,7 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_CPU); + geodesics ptp(mesh, view->active_mesh().selected, { geodesics::PTP_CPU }); TOC(view->time) gproshan_log_var(view->time); @@ -825,7 +829,7 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->active_mesh().selected, geodesics::HEAT_FLOW); + geodesics heat_flow(mesh, view->active_mesh().selected, { geodesics::HEAT_FLOW }); TOC(view->time) gproshan_log_var(view->time); @@ -854,8 +858,12 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) view->dist = new real_t[view->n_dist]; } + geodesics::params params; + params.alg = geodesics::PTP_GPU; + params.dist_alloc = view->dist; + TIC(view->time) - geodesics ptp(mesh, view->active_mesh().selected, geodesics::PTP_GPU, view->dist); + geodesics ptp(mesh, view->active_mesh().selected, params); TOC(view->time) gproshan_log_var(view->time); @@ -874,7 +882,7 @@ bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) view->active_mesh().selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->active_mesh().selected, geodesics::HEAT_FLOW_GPU); + geodesics heat_flow(mesh, view->active_mesh().selected, { geodesics::HEAT_FLOW_GPU }); TOC(view->time) gproshan_log_var(view->time); diff --git a/src/che.cpp b/src/che.cpp index b2a9a94b..6cbeaf4a 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -252,7 +252,7 @@ void che::update_colors(const real_t * vcolor, real_t max_color) return; } - if(max_color > numeric_limits::epsilon()) + if(max_color < numeric_limits::epsilon()) { #pragma omp parallel for reduction(max: max_color) for(index_t v = 0; v < n_vertices_; v++) diff --git a/src/geodesics.cpp b/src/geodesics.cpp index 614765d6..44b9d90d 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -13,14 +13,14 @@ using namespace std; namespace gproshan { -geodesics::geodesics(che * mesh, const vector & sources, const option_t & opt, real_t *const & e_dist, const bool & cluster, const size_t & n_iter, const real_t & radio): n_vertices(mesh->n_vertices()) +geodesics::geodesics(che * mesh, const vector & sources, const params & p): n_vertices(mesh->n_vertices()) { assert(n_vertices > 0); - free_dist = e_dist == nullptr; + free_dist = p.dist_alloc == nullptr; - dist = free_dist ? new real_t[n_vertices] : e_dist; - clusters = cluster ? new index_t[n_vertices] : nullptr; + dist = free_dist ? new real_t[n_vertices] : p.dist_alloc; + clusters = p.cluster ? new index_t[n_vertices] : nullptr; sorted_index = new index_t[n_vertices]; n_sorted = 0; @@ -30,7 +30,7 @@ geodesics::geodesics(che * mesh, const vector & sources, const option_t dist[v] = INFINITY; assert(sources.size() > 0); - execute(mesh, sources, n_iter, radio, opt); + execute(mesh, sources, p.n_iter, p.radio, p.alg); } geodesics::~geodesics() @@ -91,9 +91,9 @@ void geodesics::normalize() dist[sorted_index[i]] /= max; } -void geodesics::execute(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const option_t & opt) +void geodesics::execute(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const algorithm & alg) { - switch(opt) + switch(alg) { case FM: run_fastmarching(mesh, sources, n_iter, radio); break; diff --git a/src/sampling.cpp b/src/sampling.cpp index 715fd0c8..2ccfac98 100644 --- a/src/sampling.cpp +++ b/src/sampling.cpp @@ -17,16 +17,17 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n normals = new vertex[n_points]; sizes = new size_t[n_points]; index_t ** indexes = new index_t * [n_points]; - - index_t v; - - #pragma omp parallel for private(v) + + geodesics::params params; + params.radio = radio; + + #pragma omp parallel for for(index_t i = 0; i < n_points; i++) { - v = points[i]; + const index_t & v = points[i]; normals[i] = shape->normal(v); - geodesics fm(shape, {v}, geodesics::FM, nullptr, NIL, radio); + geodesics fm(shape, { v }, params); indexes[i] = new index_t[fm.n_sorted_index()]; diff --git a/src/test_geodesics_ptp.cpp b/src/test_geodesics_ptp.cpp index c2f2807f..681c1474 100644 --- a/src/test_geodesics_ptp.cpp +++ b/src/test_geodesics_ptp.cpp @@ -245,11 +245,11 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons for(int i = 0; i < n_test; i++) { - TIC(t) geodesics fm(mesh, source, geodesics::FM); TOC(t); + TIC(t) geodesics fm(mesh, source); TOC(t); seconds = min(seconds, t); } - geodesics fm(mesh, source, geodesics::FM); + geodesics fm(mesh, source); error = compute_error(&fm[0], exact, mesh->n_vertices(), source.size()); From 9906a57e4180dfa8fe47672e94952488fcc589d2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 25 Jun 2020 22:04:38 +0200 Subject: [PATCH 0318/1018] geodesics: execute fun in FM loop, break if return false --- include/geodesics.h | 4 ++-- src/geodesics.cpp | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/geodesics.h b/include/geodesics.h index 463d6eb8..56a1a3bc 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -64,8 +64,8 @@ class geodesics void normalize(); private: - void execute(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const algorithm & alg); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); + void execute(che * mesh, const std::vector & sources, const params & p); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow(che * mesh, const std::vector & sources); diff --git a/src/geodesics.cpp b/src/geodesics.cpp index 44b9d90d..371f9a83 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -30,7 +30,7 @@ geodesics::geodesics(che * mesh, const vector & sources, const params & dist[v] = INFINITY; assert(sources.size() > 0); - execute(mesh, sources, p.n_iter, p.radio, p.alg); + execute(mesh, sources, p); } geodesics::~geodesics() @@ -91,19 +91,19 @@ void geodesics::normalize() dist[sorted_index[i]] /= max; } -void geodesics::execute(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const algorithm & alg) +void geodesics::execute(che * mesh, const vector & sources, const params & p) { - switch(alg) + switch(p.alg) { - case FM: run_fastmarching(mesh, sources, n_iter, radio); + case FM: run_fastmarching(mesh, sources, p.n_iter, p.radio, p.fun); break; - case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources, n_iter, radio); + case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources, p.n_iter, p.radio); break; case HEAT_FLOW: run_heat_flow(mesh, sources); break; #ifdef GPROSHAN_CUDA - case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources, n_iter, radio); + case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources, p.n_iter, p.radio); break; case HEAT_FLOW_GPU: run_heat_flow_gpu(mesh, sources); break; @@ -111,7 +111,7 @@ void geodesics::execute(che * mesh, const vector & sources, const size_ } } -void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) +void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -157,6 +157,8 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co sorted_index[n_sorted++] = black_i; + if(fun && !fun(black_i)) break; + link_t black_link; mesh->link(black_link, black_i); for(const index_t & he: black_link) From ab146d59ed4a65ef443160078c1544147ec98fdf Mon Sep 17 00:00:00 2001 From: Lish Date: Thu, 25 Jun 2020 22:12:59 +0200 Subject: [PATCH 0319/1018] inpainting: remove extra vertices, update shader error map --- include/mdict/inpainting.h | 1 + include/mdict/patch.h | 1 + shaders/fragment.glsl | 45 +++++++++++++++++++------------------- src/app_viewer.cpp | 2 +- src/mdict/d_mesh.cpp | 3 +++ src/mdict/mdict.cpp | 8 +++---- src/mdict/patch.cpp | 17 ++++++++++++-- 7 files changed, 47 insertions(+), 30 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 1e0746ab..3fa964b8 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -39,6 +39,7 @@ class inpainting : public dictionary double sum_thres; bool * mask; double area_thres; + size_t max_points; }; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index bd0a1f68..6a6eee43 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -92,6 +92,7 @@ class patch const index_t & p, const fmask_t & mask = nullptr ); + void remove_extra_xyz_disjoint(size_t & max_points); void add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p); const a_vec normal(); bool is_covered( bool * covered); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index d0655873..9dcade76 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -36,35 +36,34 @@ float fresnel(vec3 N, vec3 E) } //https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag -float colormap_red(float x) -{ - if (x < 0.7520372909206926) - return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; - else - return -1.20830453148990E+01 * x + 1.44337397593436E+01; +float colormap_red(float x) { + if (x < 0.75) { + return 1012.0 * x - 389.0; + } else { + return -1.11322769567548E+03 * x + 1.24461193212872E+03; + } } -float colormap_green(float x) -{ - if (x < 0.7485333535031721) - return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; - else - return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; +float colormap_green(float x) { + if (x < 0.5) { + return 1012.0 * x - 129.0; + } else { + return -1012.0 * x + 899.0; + } } -float colormap_blue(float x) -{ - if (x < 0.7628468501376879) - return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; - else - return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; +float colormap_blue(float x) { + if (x < 0.25) { + return 1012.0 * x + 131.0; + } else { + return -1012.0 * x + 643.0; + } } -vec3 colormap(float x) -{ - float r = clamp(colormap_red(x) / 255.0, .0, .9); - float g = clamp(colormap_green(x) / 255.0, .0, .9); - float b = clamp(colormap_blue(x) / 255.0, .0, .9); +vec3 colormap(float x) { + float r = clamp(colormap_red(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue(x) / 255.0, 0.0, 1.0); return vec3(r, g, b); } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 92dec188..87186f61 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -618,7 +618,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) inpainting dict(mesh, &phi, params); dict.execute(); - mesh->update_colors(&dict[0], 0.003); + mesh->update_colors(&dict[0], 0.03); mesh->update_normals(); } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index a1f7d7ce..9b40096a 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -286,10 +286,12 @@ real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, vectorn_vertices(); v++) { dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); + if(dist[v] > max_error) max_error = dist[v]; error += *(new_vertices[v] - mesh->get_vertex(v)); } @@ -297,6 +299,7 @@ real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, vectorn_vertices()); error /= mesh->n_vertices(); gproshan_debug_var(error); + gproshan_debug_var(max_error); gproshan_debug_var(v_i); mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index fab6300a..5de56811 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -160,7 +160,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L selected_atoms(l) = max_index(abs(D.t() * r), mask); DD = D.cols(selected_atoms.head(l + 1)); - aa = pinv(DD) * x; + aa = solve(DD, x); r = x - DD * aa; l++; } @@ -242,8 +242,8 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) { arma::uchar_vec mask; mask.zeros(A.n_cols); - for(int i = 0; i < A.n_cols; i++) - if(phi_basis->get_frequency(i) >= 0*p.avg_dist) //2.5* if it ismin + for(index_t i = 0; i < A.n_cols; i++) + if(phi_basis->get_frequency(i) >= 0.5*p.avg_dist) //2.5* if it ismin { // gproshan_debug_var(phi_basis->get_frequency(i)); // gproshan_debug_var(p.avg_dist); @@ -259,7 +259,7 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, #pragma omp parallel for for(index_t i = 0; i < patches.size(); i++) alpha.col(i) = OMP(patches[i],phi_basis, A, L); - + return alpha; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 40d077b2..c39c794b 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -229,7 +229,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { radio = -INFINITY; - min_nv = 128; + min_nv = 0; normal_fit_directions(mesh, v); @@ -309,7 +309,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re //gproshan_debug_var(geo.n_sorted_index()); geo_radio = geo[indexes [vertices.size()-1]]; - + //gproshan_debug_var(vertices.size()); delete indexes; } @@ -364,6 +364,19 @@ double area_tri(double x1, double y1, double x2, double y2, double x3, double y3 { return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); } + +void patch::remove_extra_xyz_disjoint(size_t & max_points) +{ + if(vertices.size() > max_points) + { + arma::uvec xi; + xi.zeros(max_points); + for (size_t i=1; i< max_points; i++) xi[i] = i; + xi = arma::shuffle(xi); + xyz = xyz.cols(xi); + } + +} void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) { From 399ce21660b59badefe042271e7cba3f0d437464 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 25 Jun 2020 22:22:30 +0200 Subject: [PATCH 0320/1018] update active_mesh references --- include/geodesics.h | 2 +- src/app_viewer.cpp | 69 +++++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/include/geodesics.h b/include/geodesics.h index 56a1a3bc..4c57fc54 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -34,7 +34,7 @@ class geodesics real_t radio = INFINITY; ///< execute until the specific radio. real_t * dist_alloc = nullptr; ///< external dist allocation bool cluster = false; ///< to cluster vertices to closest source. - bool (*fun) (const index_t & u) = nullptr; ///< fun is executed inside FM loop + bool (*fun) (const index_t &) = nullptr; ///< fun is executed inside FM loop }; public: diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2d3f3f50..0a799c72 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -152,11 +152,11 @@ bool app_viewer::process_delete_vertices(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) return true; + if(!mesh.selected.size()) return true; gproshan_debug(removing vertex); - mesh->remove_vertices(view->active_mesh().selected); - view->active_mesh().selected.clear(); + mesh->remove_vertices(mesh.selected); + mesh.selected.clear(); gproshan_debug(removing vertex); return false; @@ -301,9 +301,9 @@ bool app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - view->active_mesh()->color(v) = eigvec(v, k); + mesh->color(v) = eigvec(v, k); - view->active_mesh().update_vbo(); + mesh.update_vbo(); } view->idx_active_mesh = 0; @@ -458,11 +458,11 @@ bool app_viewer::process_key_points(viewer * p_view) key_points kps(mesh); - view->active_mesh().selected.clear(); - view->active_mesh().selected.reserve(kps.size()); + mesh.selected.clear(); + mesh.selected.reserve(kps.size()); for(index_t i = 0; i < kps.size(); i++) - view->active_mesh().selected.push_back(kps[i]); + mesh.selected.push_back(kps[i]); return false; } @@ -498,7 +498,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) vertex vdir; patch p; real_t mean_edge = mesh->mean_edge(); - for(auto & v: view->active_mesh().selected) + for(auto & v: mesh.selected) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) @@ -528,7 +528,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) avg_nvp += p.vertices.size(); } - avg_nvp /= view->active_mesh().selected.size(); + avg_nvp /= mesh.selected.size(); gproshan_debug_var(avg_nvp); delete [] toplevel; @@ -615,7 +615,7 @@ bool app_viewer::process_iterative_inpaiting(viewer * p_view) { gproshan_log(APP_VIEWER); -// mesh_iterative_inpaiting(mesh, view->active_mesh().selected, freq, rt, m, M, f, learn); +// mesh_iterative_inpaiting(mesh, mesh.selected, freq, rt, m, M, f, learn); return false; } @@ -640,13 +640,13 @@ bool app_viewer::compute_toplesets(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); index_t * toplesets = new index_t[mesh->n_vertices()]; index_t * sorted = new index_t[mesh->n_vertices()]; vector limites; - mesh->compute_toplesets(toplesets, sorted, limites, view->active_mesh().selected); + mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); size_t n_toplesets = limites.size() - 1; @@ -688,7 +688,7 @@ bool app_viewer::process_voronoi(viewer * p_view) for(index_t i = 0; i < mesh->n_vertices(); i++) { mesh->color(i) = ptp.clusters[i]; - mesh->color(i) /= view->active_mesh().selected.size() + 1; + mesh->color(i) /= mesh.selected.size() + 1; } return false; @@ -707,13 +707,13 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) double time_fps; TIC(view->time) - radio = farthest_point_sampling_ptp_gpu(mesh, view->active_mesh().selected, time_fps, NIL, radio); + radio = farthest_point_sampling_ptp_gpu(mesh, mesh.selected, time_fps, NIL, radio); TOC(view->time) gproshan_log_var(time_fps); #endif // GPROSHAN_CUDA gproshan_log_var(radio); - gproshan_log_var(view->active_mesh().selected.size()); + gproshan_log_var(mesh.selected.size()); gproshan_log_var(view->time); return false; @@ -733,7 +733,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) if(ImGui::Button("Run")) { TIC(view->time) - load_sampling(view->active_mesh().selected, radio, mesh, n); + load_sampling(mesh.selected, radio, mesh, n); TOC(view->time) gproshan_log_var(view->time); } @@ -787,11 +787,11 @@ bool app_viewer::process_geodesics_fm(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); TIC(view->time) - geodesics fm(mesh, view->active_mesh().selected); + geodesics fm(mesh, mesh.selected); TOC(view->time) gproshan_log_var(view->time); @@ -806,11 +806,11 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); TIC(view->time) - geodesics ptp(mesh, view->active_mesh().selected, { geodesics::PTP_CPU }); + geodesics ptp(mesh, mesh.selected, { geodesics::PTP_CPU }); TOC(view->time) gproshan_log_var(view->time); @@ -825,11 +825,11 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->active_mesh().selected, { geodesics::HEAT_FLOW }); + geodesics heat_flow(mesh, mesh.selected, { geodesics::HEAT_FLOW }); TOC(view->time) gproshan_log_var(view->time); @@ -847,8 +847,8 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); if(view->n_dist != mesh->n_vertices()) { @@ -863,7 +863,7 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) params.dist_alloc = view->dist; TIC(view->time) - geodesics ptp(mesh, view->active_mesh().selected, params); + geodesics ptp(mesh, mesh.selected, params); TOC(view->time) gproshan_log_var(view->time); @@ -878,11 +878,11 @@ bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!view->active_mesh().selected.size()) - view->active_mesh().selected.push_back(0); + if(!mesh.selected.size()) + mesh.selected.push_back(0); TIC(view->time) - geodesics heat_flow(mesh, view->active_mesh().selected, { geodesics::HEAT_FLOW_GPU }); + geodesics heat_flow(mesh, mesh.selected, { geodesics::HEAT_FLOW_GPU }); TOC(view->time) gproshan_log_var(view->time); @@ -998,6 +998,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) bool app_viewer::select_multiple(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); static char line[128] = ""; @@ -1008,7 +1009,7 @@ bool app_viewer::select_multiple(viewer * p_view) stringstream ss(line); index_t v; while(ss >> v) - view->active_mesh().selected.push_back(v); + mesh.selected.push_back(v); } return true; From a25c5e8664e81ac58dae915f757cbcafafa5e63b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Jun 2020 09:53:12 +0200 Subject: [PATCH 0321/1018] mdict: fix geodesics params, max_radio area mesh --- include/mdict/patch.h | 4 ++-- src/mdict/inpainting.cpp | 25 +++++++++++-------- src/mdict/patch.cpp | 52 +++++++++++++++++++++++----------------- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 6a6eee43..71636e9d 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -105,7 +105,7 @@ class patch bool add_vertex_by_faces(const vertex & c, vertex & n, vector & N, - index_t * indexes, + const index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, @@ -143,7 +143,7 @@ class patch real_t get_max_z(); void save_z(ostream & os); - index_t find(index_t * indexes, size_t nc, index_t idx_global); + index_t find(const index_t * indexes, size_t nc, index_t idx_global); friend class dictionary; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 18dd3659..5986b388 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -158,15 +158,16 @@ void inpainting::load_sampling(bool save_all) gproshan_debug_var(all_sorted_features.size()); string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); - vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); - gproshan_debug_var(features.size()); - geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); - index_t * indexes = new index_t[geo.n_sorted_index()]; - geo.copy_sorted_index(indexes, geo.n_sorted_index()); +// vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); +// gproshan_debug_var(features.size()); +// geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); +// index_t * indexes = new index_t[geo.n_sorted_index()]; +// geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; - real_t max_radio = geo[indexes[mesh->n_vertices()-1]] ; +// real_t max_radio = geo[indexes[mesh->n_vertices()-1]] ; //radio *= 1.1; + real_t max_radio = 0.05 * mesh->area_surface(); gproshan_debug_var(max_radio); if(S.load(f_sampl)) @@ -440,13 +441,17 @@ void inpainting::init_voronoi_patches() //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); - + + geodesics::params params; + params.cluster = 1; + + // creating disjoint clusters with geodesics aka voronoi #ifdef GPROSHAN_CUDA - geodesics ptp( mesh, sampling, geodesics::PTP_GPU, nullptr, 1); - #else - geodesics ptp( mesh, sampling, geodesics::FM, nullptr, 1); + params.alg = geodesics::PTP_GPU; #endif + + geodesics ptp(mesh, sampling, params); TOC(d_time) gproshan_log_var(d_time); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index c39c794b..41d745ef 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -68,14 +68,15 @@ bool patch::exists(index_t idx) return false; } -index_t patch::find(index_t * indexes, size_t nc, index_t idx_global) +index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) { for(size_t i=0; i & N, index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, - che * mesh, const index_t & v, double & area, double & proj_area, double deviation) + +bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, const index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -91,7 +92,6 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N for_star(he, mesh, v) { - a = mesh->vt(next(he)); //index of the next vertex index_t b = mesh->vt(prev(he)); va = mesh->gt(a); @@ -102,16 +102,15 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N assert(b < mesh->n_vertices()); assert(v < mesh->n_vertices()); - if( geo[a] < geo[v] || geo[b] < geo[v] ) + if(geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) { - i = find(indexes, nc,a); + i = find(indexes, nc, a); } else { i = find(indexes, nc, b); - } tmp_angle = acos( (mesh->normal_he(he), N[i]) ); @@ -184,7 +183,11 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind { // for small meshes 6000 0.e-5 // for others 2.e-5 - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, radio_ + 1.e-5); + geodesics::params params; + params.radio = radio_ + 1.3-5; + + geodesics geo(mesh, {v}, params); + index_t * indexes = new index_t[geo.n_sorted_index()]; geo.copy_sorted_index(indexes, geo.n_sorted_index()); @@ -227,15 +230,18 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres, double area_thres) { - radio = -INFINITY; min_nv = 0; normal_fit_directions(mesh, v); + + geodesics::params params; + params.radio = 1.2 * radio_; - geodesics geo(mesh, {v}, geodesics::FM, NULL, false, 0, 1.2 * radio_); - index_t * indexes = new index_t[geo.n_sorted_index()]; - geo.copy_sorted_index(indexes, geo.n_sorted_index()); + geodesics geo(mesh, {v}, params); + +// index_t * indexes = new index_t[geo.n_sorted_index()]; +// geo.copy_sorted_index(indexes, geo.n_sorted_index()); a_vec vn = T.col(2);// normal at the center vertex n; @@ -254,17 +260,20 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re index_t idx_he; - for(index_t i=1; i= mesh->n_vertices() || geo[indexes [i]] > radio_) break; - + const index_t & v = geo(i); + + if(v >= mesh->n_vertices() || geo[v] > radio_) break; - c = mesh->get_vertex(v); // central vertices - ratio = (i==1)? 0:(area/proj_area); - if( add_vertex_by_faces(c, n, N, indexes, geo.n_sorted_index(), delta, geo, mesh, indexes[i], area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres ) ) + c = mesh->get_vertex(v); // central vertice + + ratio = (i == 1) ? 0 : area / proj_area; + + if(add_vertex_by_faces(c, n, N, &geo(0), geo.n_sorted_index(), delta, geo, mesh, v, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) { //compute euclidean radio - p = mesh->get_vertex(indexes[i]); + p = mesh->get_vertex(v); if(*(p - c) > euc_radio) euc_radio = *(p - c); idxs_he.push_back(idx_he); @@ -308,9 +317,8 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re //gproshan_debug_var(vertices.size()); //gproshan_debug_var(geo.n_sorted_index()); - geo_radio = geo[indexes [vertices.size()-1]]; + geo_radio = geo.radio(); //gproshan_debug_var(vertices.size()); - delete indexes; } // xyz = E.t * (xyz - avg) @@ -799,6 +807,6 @@ bool patch::is_covered( bool * covered) return true; } - //avg_dist = avg_dist/vertices.size(); + } // namespace gproshan::mdict From 4698ca2e8e4c9b96d5635ae8e800a2b0c6f89892 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Jun 2020 15:13:14 +0200 Subject: [PATCH 0322/1018] fix merge inpainting patch --- src/mdict/inpainting.cpp | 15 ++++++++++++--- src/mdict/patch.cpp | 21 ++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5986b388..919f2eb8 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -201,7 +201,9 @@ void inpainting::load_sampling(bool save_all) } //ELSE IF !S.load(f_sample) - + + gproshan_debug(SAMPLING); + // compute features will be seeds patches_map.resize(mesh->n_vertices()); @@ -212,6 +214,7 @@ void inpainting::load_sampling(bool save_all) for(index_t i = 0; i < mesh->n_vertices(); i++) covered[i] = 0; + gproshan_debug(SAMPLING); real_t euc_radio; real_t geo_radio; vector radios; @@ -219,11 +222,15 @@ void inpainting::load_sampling(bool save_all) size_t count_cov = 0; size_t count_cov_patch = 0; - bool faces[mesh->n_faces()] = {}; + gproshan_debug(SAMPLING); + bool * faces = new bool[mesh->n_faces()]; + memset(faces, 0, sizeof(bool) * mesh->n_faces()); vector idxs_he; + gproshan_debug(SAMPLING); bool * invalid_seed = new bool[mesh->n_vertices()]; memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); + gproshan_debug(SAMPLING); for(const index_t & vsf: all_sorted_features) { @@ -265,10 +272,11 @@ void inpainting::load_sampling(bool save_all) } for(auto i: idxs_he) - faces[i] = true; + faces[i/3] = true; } } } + gproshan_debug(SAMPLING); delete [] invalid_seed; @@ -279,6 +287,7 @@ void inpainting::load_sampling(bool save_all) gproshan_debug_var(seeds.size()); M = seeds.size(); + gproshan_debug(SAMPLING); /////////////////////////////////////// #ifndef NDEBUG diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 41d745ef..b26a5ba1 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -184,7 +184,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind // for small meshes 6000 0.e-5 // for others 2.e-5 geodesics::params params; - params.radio = radio_ + 1.3-5; + params.radio = radio_ + 1e-5;//numeric_limits::epsilon(); geodesics geo(mesh, {v}, params); @@ -246,7 +246,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re a_vec vn = T.col(2);// normal at the center vertex n; n.x = vn(0); n.y = vn(1); n.z = vn(2); - vertex p, c; + vertex p; vertices.push_back(v); euc_radio = -INFINITY; @@ -257,26 +257,25 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re double proj_area = 0; double area_mesh = mesh->area_surface(); double ratio; - index_t idx_he; +// index_t idx_he; + vertex c = mesh->get_vertex(v); for(index_t i = 1; i < geo.n_sorted_index(); i++) { - const index_t & v = geo(i); + const index_t & u = geo(i); - if(v >= mesh->n_vertices() || geo[v] > radio_) break; + if(u >= mesh->n_vertices() || geo[u] > radio_) break; - c = mesh->get_vertex(v); // central vertice - ratio = (i == 1) ? 0 : area / proj_area; - if(add_vertex_by_faces(c, n, N, &geo(0), geo.n_sorted_index(), delta, geo, mesh, v, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) + if(add_vertex_by_faces(c, n, N, &geo(0), geo.n_sorted_index(), delta, geo, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) { //compute euclidean radio - p = mesh->get_vertex(v); + p = mesh->get_vertex(u); if(*(p - c) > euc_radio) euc_radio = *(p - c); - idxs_he.push_back(idx_he); +// idxs_he.push_back(idx_he); } else @@ -317,7 +316,7 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re //gproshan_debug_var(vertices.size()); //gproshan_debug_var(geo.n_sorted_index()); - geo_radio = geo.radio(); + geo_radio = geo[geo(vertices.size() - 1)]; //gproshan_debug_var(vertices.size()); } From 64c7c991b66b1cbef8ec94184562b78004223380 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Jun 2020 15:42:29 +0200 Subject: [PATCH 0323/1018] patch fix params, area_mesh --- include/mdict/patch.h | 21 +++++++++------- src/mdict/inpainting.cpp | 51 +++----------------------------------- src/mdict/patch.cpp | 53 +++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 86 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 71636e9d..2f4a1ac9 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -62,15 +62,18 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); - void init_radial_disjoint(vector & idxs_he, - che * mesh, - const real_t & radio_, - const index_t & v, - real_t & euc_radio, - real_t & geo_radio, - double delta, - double sum_thres, - double area_thres); + + void init_radial_disjoint( real_t & euc_radio, + real_t & geo_radio, + che * mesh, + const index_t & v, + const real_t & radio, + const real_t & delta, + const real_t & sum_thres, + const real_t & area_thres, + const real_t & area_mesh + ); + void init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr); void recover_radial_disjoint(che * mesh, const real_t & radio_, diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 919f2eb8..6174422b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -167,21 +167,21 @@ void inpainting::load_sampling(bool save_all) // real_t max_radio = geo[indexes[mesh->n_vertices()-1]] ; //radio *= 1.1; - real_t max_radio = 0.05 * mesh->area_surface(); + real_t area_mesh = mesh->area_surface(); + real_t max_radio = 0.05 * area_mesh; gproshan_debug_var(max_radio); if(S.load(f_sampl)) { gproshan_debug(already done); size_t n_seeds = S.n_rows; - vector idxs_he; real_t euc_radio, geo_radio; for(index_t i = 0; i < n_seeds; i++) { patch p; // gproshan_debug_var(i); //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); - p.init_radial_disjoint(idxs_he, mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); + // p.init_radial_disjoint(mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); patches.push_back(p); } @@ -214,7 +214,6 @@ void inpainting::load_sampling(bool save_all) for(index_t i = 0; i < mesh->n_vertices(); i++) covered[i] = 0; - gproshan_debug(SAMPLING); real_t euc_radio; real_t geo_radio; vector radios; @@ -222,15 +221,8 @@ void inpainting::load_sampling(bool save_all) size_t count_cov = 0; size_t count_cov_patch = 0; - gproshan_debug(SAMPLING); - bool * faces = new bool[mesh->n_faces()]; - memset(faces, 0, sizeof(bool) * mesh->n_faces()); - vector idxs_he; - gproshan_debug(SAMPLING); - bool * invalid_seed = new bool[mesh->n_vertices()]; memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); - gproshan_debug(SAMPLING); for(const index_t & vsf: all_sorted_features) { @@ -238,8 +230,7 @@ void inpainting::load_sampling(bool save_all) patch p; // increasing a bit the radio - idxs_he.clear(); - p.init_radial_disjoint(idxs_he, mesh, max_radio, vsf, euc_radio, geo_radio, delta, sum_thres, area_thres); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, max_radio, delta, sum_thres, area_thres, area_mesh); //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; @@ -270,13 +261,9 @@ void inpainting::load_sampling(bool save_all) invalid_seed[v] = *(va - vb) < 0.4 * euc_radio; } } - - for(auto i: idxs_he) - faces[i/3] = true; } } } - gproshan_debug(SAMPLING); delete [] invalid_seed; @@ -295,36 +282,6 @@ void inpainting::load_sampling(bool save_all) index_t tmp; bool remark[mesh->n_vertices()] = {}; - //outliers by triangles. - for(index_t i = 0; i < mesh->n_faces(); i++) - { - if(!faces[i]) - { - tmp = mesh->vt(next(i*3)); - if(!covered[tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - - tmp = mesh->vt(prev(i*3)); - - if(!covered[tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - - tmp = mesh->vt(i*3); - if(!covered[tmp] && !remark[tmp]) - { - outliers.push_back(tmp); - remark[tmp] = true; - } - } - - } - gproshan_debug_var(outliers.size()); for(index_t i = 0; i < mesh->n_vertices(); i++) { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index b26a5ba1..304eccb8 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -228,36 +228,41 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind } -void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const real_t & radio_, const index_t & v, real_t & euc_radio, real_t & geo_radio, double delta, double sum_thres, double area_thres) +void patch::init_radial_disjoint( real_t & euc_radio, + real_t & geo_radio, + che * mesh, + const index_t & v, + const real_t & max_radio, + const real_t & delta, + const real_t & sum_thres, + const real_t & area_thres, + const real_t & area_mesh + ) { radio = -INFINITY; min_nv = 0; + euc_radio = -INFINITY; + normal_fit_directions(mesh, v); geodesics::params params; - params.radio = 1.2 * radio_; + params.radio = 1.2 * max_radio; geodesics geo(mesh, {v}, params); -// index_t * indexes = new index_t[geo.n_sorted_index()]; -// geo.copy_sorted_index(indexes, geo.n_sorted_index()); - a_vec vn = T.col(2);// normal at the center - vertex n; - n.x = vn(0); n.y = vn(1); n.z = vn(2); - vertex p; + a_vec vn = T.col(2); + vertex n = { vn(0), vn(1), vn(2) }; + vertices.push_back(v); - euc_radio = -INFINITY; vector N; N.push_back(n); - //double angle; - double area = 0; - double proj_area = 0; - double area_mesh = mesh->area_surface(); - double ratio; -// index_t idx_he; + + real_t area = 0; + real_t proj_area = 0; + real_t ratio; vertex c = mesh->get_vertex(v); @@ -265,26 +270,15 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re { const index_t & u = geo(i); - if(u >= mesh->n_vertices() || geo[u] > radio_) break; + if(u >= mesh->n_vertices() || geo[u] > max_radio) break; ratio = (i == 1) ? 0 : area / proj_area; if(add_vertex_by_faces(c, n, N, &geo(0), geo.n_sorted_index(), delta, geo, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) - { - //compute euclidean radio - p = mesh->get_vertex(u); - if(*(p - c) > euc_radio) - euc_radio = *(p - c); -// idxs_he.push_back(idx_he); - - } + euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); else - { break; - } - // gproshan_debug_var(acos( (mesh->normal(indexes[i-1]), mesh->normal(indexes[i]) ) )); } - //gproshan_debug_var(vertices.size()); // Refit the points and update the radius size_t d_fitting = 2; @@ -299,11 +293,10 @@ void patch::init_radial_disjoint(vector & idxs_he, che * mesh, const re n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); radio = -INFINITY; + vertex p; for(index_t i=1; i < vertices.size(); i++) { p = mesh->get_vertex(vertices[i]); - c = mesh->get_vertex(v); // central vertices - p = p - c ; p = p - ((p,n)*n); From 8a155621909cb971ca159c255e124ed63e5a49bf Mon Sep 17 00:00:00 2001 From: Lish Date: Fri, 26 Jun 2020 15:47:40 +0200 Subject: [PATCH 0324/1018] removing tic toc --- src/mdict/inpainting.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 6174422b..5c65e264 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -150,10 +150,7 @@ void inpainting::load_sampling(bool save_all) vector all_sorted_features; vector seeds; size_t featsize; - TIC(d_time) load_features(all_sorted_features, featsize); - TOC(d_time) - gproshan_debug_var(d_time); gproshan_debug_var(all_sorted_features.size()); string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); From 38e071807db100f06451e205f057432f881f2204 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Jun 2020 16:20:51 +0200 Subject: [PATCH 0325/1018] fix patch get vertices faces --- include/mdict/patch.h | 23 +++++++++++------------ src/mdict/inpainting.cpp | 2 +- src/mdict/patch.cpp | 33 ++++++++++++++++----------------- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 2f4a1ac9..45dc6a32 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -105,18 +105,17 @@ class patch void compute_avg_distance(che * mesh, vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces(const vertex & c, - vertex & n, - vector & N, - const index_t * indexes, - size_t nc, - double thr_angle, - const geodesics & geo, - che * mesh, - const index_t & v, - double & area, - double & proj_area, - double deviation); + bool add_vertex_by_faces( const vertex & c, + vertex & n, + vector & N, + double thr_angle, + const geodesics & geo, + che * mesh, + const index_t & v, + double & area, + double & proj_area, + double deviation + ); private: diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5c65e264..5b020728 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -178,7 +178,7 @@ void inpainting::load_sampling(bool save_all) patch p; // gproshan_debug_var(i); //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); - // p.init_radial_disjoint(mesh, S(i,1), S(i,0), euc_radio, geo_radio, delta, sum_thres, area_thres); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i, 0), S(i, 1), delta, sum_thres, area_thres, area_mesh); patches.push_back(p); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 304eccb8..30a36295 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -76,7 +76,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, const index_t * indexes, size_t nc, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) +bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -105,33 +105,26 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N if(geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) - { - i = find(indexes, nc, a); - } + i = find(vertices.data(), vertices.size(), a); else - { - i = find(indexes, nc, b); - } + i = find(vertices.data(), vertices.size(), b); + tmp_angle = acos( (mesh->normal_he(he), N[i]) ); - if ( angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation ) // Fullfill conditions + if(angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation) // Fullfill conditions { - angle = tmp_angle; - //gproshan_debug_var(he); - area_face = mesh->area_trig(he/3); + area_face = mesh->area_trig(he / 3); + // compute projected area pav = va - vv + ( (n,vv) - (n,va) ) * n; pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; proj_area_face = *(pav * pbv) / 2; min_he = mesh->normal_he(he); - if( !exists(v) ) vertices.push_back(v); added = true; } - } - } //p = mesh->get_vertex(indexes[i]); //p = p - c ; @@ -139,7 +132,13 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N area += area_face; proj_area += proj_area_face; - N.push_back(min_he); + + if(added) + { + vertices.push_back(v); + N.push_back(min_he); + } + return added; } @@ -274,7 +273,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, ratio = (i == 1) ? 0 : area / proj_area; - if(add_vertex_by_faces(c, n, N, &geo(0), geo.n_sorted_index(), delta, geo, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) + if(add_vertex_by_faces(c, n, N, delta, geo, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); else break; @@ -309,7 +308,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, //gproshan_debug_var(vertices.size()); //gproshan_debug_var(geo.n_sorted_index()); - geo_radio = geo[geo(vertices.size() - 1)]; + geo_radio = geo[vertices.back()]; //gproshan_debug_var(vertices.size()); } From 4d108a49df39ae7b627066e33c1f02221e183047 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Jun 2020 17:16:27 +0200 Subject: [PATCH 0326/1018] no geo max radio patch creation --- include/geodesics.h | 9 +++++++-- include/mdict/patch.h | 2 +- src/geodesics.cpp | 4 ++-- src/mdict/mdict.cpp | 2 +- src/mdict/patch.cpp | 38 +++++++++++++++++++------------------- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/include/geodesics.h b/include/geodesics.h index 4c57fc54..902d3bff 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -5,6 +5,9 @@ #include "include_arma.h" +#include + + // geometry processing and shape analysis framework namespace gproshan { @@ -17,6 +20,8 @@ namespace gproshan { */ class geodesics { + using fun_t = std::function; + public: enum algorithm { FM, ///< Execute Fast Marching algorithm #ifdef GPROSHAN_CUDA @@ -34,7 +39,7 @@ class geodesics real_t radio = INFINITY; ///< execute until the specific radio. real_t * dist_alloc = nullptr; ///< external dist allocation bool cluster = false; ///< to cluster vertices to closest source. - bool (*fun) (const index_t &) = nullptr; ///< fun is executed inside FM loop + fun_t fun = [](const index_t &) -> bool { return true; }; ///< fun is executed inside FM loop }; public: @@ -65,7 +70,7 @@ class geodesics private: void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, fun_t fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow(che * mesh, const std::vector & sources); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 45dc6a32..3726c532 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -109,7 +109,7 @@ class patch vertex & n, vector & N, double thr_angle, - const geodesics & geo, + const real_t * geo, che * mesh, const index_t & v, double & area, diff --git a/src/geodesics.cpp b/src/geodesics.cpp index 79146636..15049585 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -112,7 +112,7 @@ void geodesics::execute(che * mesh, const vector & sources, const param } } -void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)) +void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, fun_t fun) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -158,7 +158,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co sorted_index[n_sorted++] = black_i; - if(fun && !fun(black_i)) break; + if(!fun(black_i)) break; link_t black_link; mesh->link(black_link, black_i); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 5de56811..5e060158 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -160,7 +160,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L selected_atoms(l) = max_index(abs(D.t() * r), mask); DD = D.cols(selected_atoms.head(l + 1)); - aa = solve(DD, x); + aa = pinv(DD) * x; r = x - DD * aa; l++; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 30a36295..eb48fe88 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -76,7 +76,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const geodesics & geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) +bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -245,12 +245,6 @@ void patch::init_radial_disjoint( real_t & euc_radio, normal_fit_directions(mesh, v); - geodesics::params params; - params.radio = 1.2 * max_radio; - - geodesics geo(mesh, {v}, params); - - a_vec vn = T.col(2); vertex n = { vn(0), vn(1), vn(2) }; @@ -260,25 +254,31 @@ void patch::init_radial_disjoint( real_t & euc_radio, N.push_back(n); real_t area = 0; - real_t proj_area = 0; + real_t proj_area = std::numeric_limits::epsilon(); real_t ratio; vertex c = mesh->get_vertex(v); - - for(index_t i = 1; i < geo.n_sorted_index(); i++) + + geodesics::params params; + params.dist_alloc = new real_t[mesh->n_vertices()]; + params.fun = [&](const index_t & u) -> bool { - const index_t & u = geo(i); + if(u == v) return true; - if(u >= mesh->n_vertices() || geo[u] > max_radio) break; - - ratio = (i == 1) ? 0 : area / proj_area; + ratio = area / proj_area; - if(add_vertex_by_faces(c, n, N, delta, geo, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) + if(add_vertex_by_faces(c, n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) + { euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); - else - break; - } - + return true; + } + + return false; + }; + + geodesics geo(mesh, {v}, params); + delete [] params.dist_alloc; + // Refit the points and update the radius size_t d_fitting = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; From 1fb378cbcb2fc8e23e48dba65e654edf188c6e6d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 27 Jun 2020 09:45:36 +0200 Subject: [PATCH 0327/1018] geodesics: update fm function lambda parameter --- include/geodesics.h | 11 ++++++++--- src/geodesics.cpp | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/include/geodesics.h b/include/geodesics.h index 4c57fc54..85b1e807 100644 --- a/include/geodesics.h +++ b/include/geodesics.h @@ -5,6 +5,9 @@ #include "include_arma.h" +#include + + // geometry processing and shape analysis framework namespace gproshan { @@ -17,6 +20,8 @@ namespace gproshan { */ class geodesics { + using fm_function_t = std::function; + public: enum algorithm { FM, ///< Execute Fast Marching algorithm #ifdef GPROSHAN_CUDA @@ -34,7 +39,7 @@ class geodesics real_t radio = INFINITY; ///< execute until the specific radio. real_t * dist_alloc = nullptr; ///< external dist allocation bool cluster = false; ///< to cluster vertices to closest source. - bool (*fun) (const index_t &) = nullptr; ///< fun is executed inside FM loop + fm_function_t fun; ///< fun is executed inside FM loop }; public: @@ -51,7 +56,7 @@ class geodesics public: geodesics( che * mesh, ///< input triangular mesh. const std::vector & sources, ///< source vertices. - const params & p = {FM, 0, INFINITY, nullptr, false, nullptr} + const params & p = {FM, 0, INFINITY, nullptr, false} ); virtual ~geodesics(); @@ -65,7 +70,7 @@ class geodesics private: void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_flow(che * mesh, const std::vector & sources); diff --git a/src/geodesics.cpp b/src/geodesics.cpp index 371f9a83..3957edca 100644 --- a/src/geodesics.cpp +++ b/src/geodesics.cpp @@ -43,6 +43,7 @@ geodesics::~geodesics() const real_t & geodesics::operator[](const index_t & i) const { + assert(i < n_vertices); return dist[i]; } @@ -111,7 +112,7 @@ void geodesics::execute(che * mesh, const vector & sources, const param } } -void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, bool (*fun) (const index_t &)) +void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; From 49123ba7a57abb77d10e8c6e497a17fa713c0301 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 28 Jun 2020 01:17:37 +0200 Subject: [PATCH 0328/1018] add colormap.glsl, process shader #include --- shaders/colormap.glsl | 108 ++++++++++++++++++++++++++++++++++++++++++ shaders/fragment.glsl | 37 ++------------- src/viewer/shader.cpp | 31 +++++++++--- 3 files changed, 135 insertions(+), 41 deletions(-) create mode 100644 shaders/colormap.glsl diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl new file mode 100644 index 00000000..6bc2b39c --- /dev/null +++ b/shaders/colormap.glsl @@ -0,0 +1,108 @@ +// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag + +float colormap_red_0(float x) +{ + if (x < 0.7520372909206926) + return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; + else + return -1.20830453148990E+01 * x + 1.44337397593436E+01; +} + +float colormap_green_0(float x) +{ + if (x < 0.7485333535031721) + return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; + else + return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; +} + +float colormap_blue_0(float x) +{ + if (x < 0.7628468501376879) + return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; + else + return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; +} + +vec3 colormap_0(float x) +{ + float r = clamp(colormap_red_0(x) / 255.0, .0, .9); + float g = clamp(colormap_green_0(x) / 255.0, .0, .9); + float b = clamp(colormap_blue_0(x) / 255.0, .0, .9); + return vec3(r, g, b); +} + + +// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-YIOrBr.frag + +float colormap_red_1(float x) +{ + return ((((1.30858855846896E+03 * x - 2.84649723684787E+03) * x + 1.76048857883363E+03) * x - 3.99775093706324E+02) * x + 2.69759225316811E+01) * x + 2.54587325383574E+02; +} + +float colormap_green_1(float x) +{ + return ((((-8.85605750526301E+02 * x + 2.20590941129997E+03) * x - 1.50123293069936E+03) * x + 2.38490009587258E+01) * x - 6.03460495073813E+01) * x + 2.54768707485247E+02; +} + +float colormap_blue_1(float x) +{ + if (x < 0.2363454401493073) + return (-3.68734834041388E+01 * x - 3.28163398692792E+02) * x + 2.27342862588147E+02; + else if (x < 0.7571054399013519) + return ((((1.60988309475108E+04 * x - 4.18782706486673E+04) * x + 4.14508040221340E+04) * x - 1.88926043556059E+04) * x + 3.50108270140290E+03) * x - 5.28541997751406E+01; + else + return 1.68513761929930E+01 * x - 1.06424668227935E+01; +} + +vec3 colormap_1(float x) +{ + float r = clamp(colormap_red_1(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green_1(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue_1(x) / 255.0, 0.0, 1.0); + return vec3(r, g, b); +} + + +// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_Blue-Red_2.frag + +float colormap_red_2(float x) +{ + if (x < 0.75) + return 1012.0 * x - 389.0; + else + return -1.11322769567548E+03 * x + 1.24461193212872E+03; +} + +float colormap_green_2(float x) +{ + if (x < 0.5) + return 1012.0 * x - 129.0; + else + return -1012.0 * x + 899.0; +} + +float colormap_blue_2(float x) +{ + if (x < 0.25) + return 1012.0 * x + 131.0; + else + return -1012.0 * x + 643.0; +} + +vec3 colormap_2(float x) +{ + float r = clamp(colormap_red_2(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green_2(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue_2(x) / 255.0, 0.0, 1.0); + return vec3(r, g, b); +} + +vec3 colormap(int i, float x) +{ + if(i == 0) return colormap_0(x); + if(i == 1) return colormap_1(x); + if(i == 2) return colormap_2(x); + +} + diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index d0655873..bd7c6870 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,5 +1,7 @@ #version 410 core +#include ../shaders/colormap.glsl + in vec3 gs_position; in vec3 gs_normal; in float gs_color; @@ -35,42 +37,9 @@ float fresnel(vec3 N, vec3 E) return pow(sqrt( 1. - NE * NE ), sharpness); } -//https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag -float colormap_red(float x) -{ - if (x < 0.7520372909206926) - return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; - else - return -1.20830453148990E+01 * x + 1.44337397593436E+01; -} - -float colormap_green(float x) -{ - if (x < 0.7485333535031721) - return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; - else - return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; -} - -float colormap_blue(float x) -{ - if (x < 0.7628468501376879) - return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; - else - return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; -} - -vec3 colormap(float x) -{ - float r = clamp(colormap_red(x) / 255.0, .0, .9); - float g = clamp(colormap_green(x) / 255.0, .0, .9); - float b = clamp(colormap_blue(x) / 255.0, .0, .9); - return vec3(r, g, b); -} - void main() { - vec3 color = colormap(gs_color); + vec3 color = colormap(2, gs_color); // lines if(render_lines) diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 501740b7..6ace6304 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -3,6 +3,8 @@ #include #include #include +#include + using namespace std; @@ -119,15 +121,30 @@ bool shader::load(GLenum shader_type, const char * filename) bool shader::read_source(const char * filename, std::string & source) { - source = ""; - - ifstream in(filename); + ifstream is(filename); - assert(in.is_open()); + if(!is.is_open()) + return false; - string line; - while(getline(in, line)) - source += line + '\n'; + source = ""; + + string line, include; + while(getline(is, line)) + { + stringstream ss(line); + + ss >> include; + if(include == "#include") + { + ss >> include; + if(read_source(include.c_str(), include)) + source += include + '\n'; + } + else + source += line + '\n'; + } + + is.close(); return true; } From 2dff4369186e1e8d20dc23af5fbcf2a6a86c45f9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 28 Jun 2020 01:57:09 +0200 Subject: [PATCH 0329/1018] add imgui select colormap --- include/viewer/viewer.h | 7 +++--- shaders/colormap.glsl | 2 +- shaders/fragment.glsl | 3 ++- shaders/fragment_pointcloud.glsl | 38 ++++---------------------------- src/viewer/viewer.cpp | 11 +++++++++ 5 files changed, 21 insertions(+), 40 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 4e0dc5cf..c5280064 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -34,9 +34,6 @@ namespace gproshan { -class viewer; - - class viewer { protected: @@ -62,6 +59,8 @@ class viewer int viewport_width; int viewport_height; + unsigned int idx_colormap = 0; // colormap index defined in shaders/colormap.glsl + const std::vector colormap = {"blue", "red", "blue/read"}; shader shader_program; shader shader_normals; shader shader_gradient; @@ -95,7 +94,7 @@ class viewer #endif // GPROSHAN_OPTIX bool action = false; - + bool render_pointcloud = false; bool render_wireframe = false; bool render_wireframe_fill = false; diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl index 6bc2b39c..f3fad9f6 100644 --- a/shaders/colormap.glsl +++ b/shaders/colormap.glsl @@ -98,7 +98,7 @@ vec3 colormap_2(float x) return vec3(r, g, b); } -vec3 colormap(int i, float x) +vec3 colormap(uint i, float x) { if(i == 0) return colormap_0(x); if(i == 1) return colormap_1(x); diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index bd7c6870..aa432054 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -10,6 +10,7 @@ noperspective in vec3 edge_dist; layout(location = 0) out vec4 frag_color; +uniform uint idx_colormap; uniform vec3 eye; uniform vec3 light; uniform bool render_flat; @@ -39,7 +40,7 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color = colormap(2, gs_color); + vec3 color = colormap(idx_colormap, gs_color); // lines if(render_lines) diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 4e70e9c6..e988c39e 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -1,11 +1,14 @@ #version 410 core +#include ../shaders/colormap.glsl + in vec3 vs_position; in vec3 vs_normal; in float vs_color; layout(location = 0) out vec4 frag_color; +uniform uint idx_colormap; uniform vec3 eye; uniform vec3 light; uniform bool render_lines; @@ -31,42 +34,9 @@ float fresnel(vec3 N, vec3 E) return pow(sqrt( 1. - NE * NE ), sharpness); } -//https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag -float colormap_red(float x) -{ - if (x < 0.7520372909206926) - return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; - else - return -1.20830453148990E+01 * x + 1.44337397593436E+01; -} - -float colormap_green(float x) -{ - if (x < 0.7485333535031721) - return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; - else - return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; -} - -float colormap_blue(float x) -{ - if (x < 0.7628468501376879) - return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; - else - return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; -} - -vec3 colormap(float x) -{ - float r = clamp(colormap_red(x) / 255.0, .0, .9); - float g = clamp(colormap_green(x) / 255.0, .0, .9); - float b = clamp(colormap_blue(x) / 255.0, .0, .9); - return vec3(r, g, b); -} - void main() { - vec3 color = colormap(vs_color); + vec3 color = colormap(idx_colormap, vs_color); // lines if(render_lines) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 3a0e0155..17c9b538 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -124,6 +124,15 @@ bool viewer::run() ImGui::EndMenu(); } + if(ImGui::BeginMenu("Colormap")) + { + for(index_t i = 0; i < colormap.size(); i++) + if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == idx_colormap, i != idx_colormap)) + idx_colormap = i; + + ImGui::EndMenu(); + } + for(index_t i = 0; i < sub_menus.size(); i++) { if(ImGui::BeginMenu(sub_menus[i].c_str())) @@ -653,6 +662,7 @@ void viewer::render_gl() glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); + glProgramUniform1ui(shader_program, shader_program("idx_colormap"), idx_colormap); glProgramUniform3f(shader_program, shader_program("eye"), eye[1], eye[2], eye[3]); glProgramUniform3f(shader_program, shader_program("light"), light[1], light[2], light[3]); glProgramUniform1i(shader_program, shader_program("render_flat"), render_flat); @@ -662,6 +672,7 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_program, shader_program("proj_mat"), 1, 0, &proj_mat[0][0]); + glProgramUniform1ui(shader_pointcloud, shader_program("idx_colormap"), idx_colormap); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[1], eye[2], eye[3]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); From b4fd5cfb433f5f36ad593ba141bee02282fe09a3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 28 Jun 2020 18:48:04 +0200 Subject: [PATCH 0330/1018] basis adding d_discrete and refactoring code --- include/mdict/basis.h | 23 +++++++-------- include/mdict/basis_cosine.h | 8 +++--- include/mdict/basis_dct.h | 10 ++++--- src/mdict/basis.cpp | 44 ++++++++++++++++------------- src/mdict/basis_cosine.cpp | 35 +++++++++-------------- src/mdict/basis_dct.cpp | 54 ++++++++++++++++++++---------------- src/mdict/dictionary.cpp | 35 +++++++++++------------ src/mdict/inpainting.cpp | 31 +++++++++++---------- src/mdict/mdict.cpp | 4 +-- 9 files changed, 124 insertions(+), 120 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 5bdf73e5..3dfbd87e 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -14,30 +14,27 @@ using namespace std; namespace gproshan::mdict { -class dictionary; - class basis { protected: - real_t radio; - size_t dim; - + real_t _radio; + size_t _dim; public: + basis(const real_t & r, const size_t & d); virtual ~basis() = default; - virtual void discrete(a_mat & phi, const a_mat & xy) = 0; + virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; + virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; + virtual real_t freq(const index_t & idx) = 0; + real_t & radio(); + const size_t & dim() const; void plot_basis(); void plot_atoms(const a_mat & A); - void plot_patch(const a_mat & A, const a_mat & xyz, index_t i); - size_t get_dim(); - real_t get_radio(); - virtual double get_frequency(size_t idx) = 0; - + void plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p); + private: virtual void plot_basis(std::ostream & os) = 0; virtual void plot_atoms(std::ostream & os, const a_vec & A) = 0; - - friend class dictionary; }; diff --git a/include/mdict/basis_cosine.h b/include/mdict/basis_cosine.h index 5628ee4e..1cbc5b70 100644 --- a/include/mdict/basis_cosine.h +++ b/include/mdict/basis_cosine.h @@ -12,12 +12,12 @@ namespace gproshan::mdict { class basis_cosine: public basis { private: - real_t r; // rotations - real_t n; // frequency + size_t n_rot; ///< rotations + size_t n_freq; ///< frequency public: - basis_cosine(const size_t & _r, const size_t & _n, const real_t & _radio = 0); - void discrete(a_mat & phi, const a_mat & xy); + basis_cosine(const size_t & nr, const size_t & nf, const real_t & r = 0); + void discrete(a_mat & phi, const a_vec & x, const a_vec & y); private: void plot_basis(std::ostream & os); diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index 0aaa06c2..e391a18b 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -12,17 +12,19 @@ namespace gproshan::mdict { class basis_dct: public basis { private: - int n; // int frequency + size_t n_freq; ///< frequency public: - basis_dct(const size_t & _n, const real_t & _radio = 1); - void discrete(a_mat & phi, const a_mat & xy); - double get_frequency(size_t idx); + basis_dct(const size_t & n, const real_t & r = 1); + void discrete(a_mat & phi, const a_vec & x, const a_vec & y); + void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y); + real_t freq(const index_t & idx); private: void plot_basis(std::ostream & os); void plot_atoms(std::ostream & os, const a_vec & A); a_vec dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny); + a_vec d_dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny); void dct(std::ostream & os, const index_t & nx, const index_t & ny); }; diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 8fc7acfb..74ab8ddf 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -6,14 +6,27 @@ namespace gproshan::mdict { +basis::basis(const real_t & r, const size_t & d): _radio(r), _dim(d) {} + +real_t & basis::radio() +{ + return _radio; +} + +const size_t & basis::dim() const +{ + return _dim; +} + void basis::plot_basis() { string file = tmp_file_path("basis.gpi"); ofstream os(file); + os << "set term qt size 1000,1000;" << endl; os << "set isosamples 50,50;" << endl; os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << radio <<"];" << endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; os << "set urange [-pi:pi];" << endl; os << "unset key;" << endl; os << "set pm3d at b;" << endl; @@ -45,7 +58,7 @@ void basis::plot_atoms(const a_mat & A) os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << endl; os << "set isosamples 25,25;" << endl; os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << radio <<"];" << endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; os << "set urange [-pi:pi];" << endl; os << "unset key;" << endl; os << "set pm3d at b;" << endl; @@ -57,6 +70,7 @@ void basis::plot_atoms(const a_mat & A) plot_atoms(os, A.col(i)); os << ";" << endl; } + os << "unset multiplot;" << endl; os << "pause -1;" << endl; @@ -67,41 +81,30 @@ void basis::plot_atoms(const a_mat & A) system(file.c_str()); } -size_t basis::get_dim() +void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) { - return dim; -} - -real_t basis::get_radio() -{ - return radio; -} - - -void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) -{ - string data = tmp_file_path("xyz_" + to_string(i) + ".dat"); a_mat tmp = xyz.t(); - tmp.save(data.c_str(),arma::arma_ascii); + string data = tmp_file_path("xyz_" + to_string(p) + ".dat"); + tmp.save(data.c_str(), arma::arma_ascii); size_t K = A.n_rows; size_t m = A.n_cols; size_t s = sqrt(m); s += !(s * s == K); - string file = tmp_file_path("atoms_patch_"+ to_string(i) + ".gpi"); + string file = tmp_file_path("atoms_patch_"+ to_string(p) + ".gpi"); ofstream os(file); os << "set term qt size 1000,1000;" << endl; os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << endl; os << "set isosamples 25,25;" << endl; os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << radio <<"];" << endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; os << "set urange [-pi:pi];" << endl; os << "unset key;" << endl; os << "set pm3d at b;" << endl; os << "unset colorbox;" << endl; - os << "splot \"xyz_" << to_string(i) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; + os << "splot \"xyz_" << to_string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; for(index_t i = 0; i < m; i++) { @@ -109,6 +112,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) plot_atoms(os, A.col(i)); os << ";" << endl; } + os << "unset multiplot;" << endl; os << "pause -1;" << endl; @@ -117,8 +121,8 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, index_t i) //file = "gnuplot -persist " + file + " &"; //system(file.c_str()); - } + } // namespace gproshan::mdict diff --git a/src/mdict/basis_cosine.cpp b/src/mdict/basis_cosine.cpp index 082c0e7b..01d0e8ce 100644 --- a/src/mdict/basis_cosine.cpp +++ b/src/mdict/basis_cosine.cpp @@ -8,56 +8,47 @@ namespace gproshan::mdict { -basis_cosine::basis_cosine(const size_t & _r, const size_t & _n, const real_t & _radio) -{ - radio = _radio; - r = _r; - n = _n; - dim = r * n; -} +basis_cosine::basis_cosine(const size_t & nr, const size_t & nf, const real_t & r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} -void basis_cosine::discrete(a_mat & phi, const a_mat & xy) +void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { - assert(phi.n_cols == dim); - - a_vec x = xy.row(0).t(); - a_vec y = xy.row(1).t(); + assert(phi.n_cols == _dim); - real_t d = 1.0 / (r - 1); + real_t d = 1.0 / (n_rot - 1); real_t c; - for(size_t k = 0, ni = 1; ni <= n; ni++ ) + for(size_t k = 0, ni = 1; ni <= n_freq; ni++ ) for(real_t alpha = 0; alpha <= 1; alpha += d, k++) { - c = ni * M_PI / radio; + c = ni * M_PI / _radio; phi.col(k) = cosine(x, y, c, alpha); } } void basis_cosine::plot_basis(ostream & os) { - real_t d = 1.0 / (r - 1); + real_t d = 1.0 / (n_rot - 1); real_t c; - os << "set multiplot layout " << n << "," << r << " rowsfirst scale 1.2;" << endl; + os << "set multiplot layout " << n_freq << "," << n_rot << " rowsfirst scale 1.2;" << endl; - for(size_t ni = 1; ni <= n; ni++ ) + for(size_t ni = 1; ni <= n_freq; ni++ ) for(real_t alpha = 0; alpha <= 1; alpha += d) { - c = ni * M_PI / radio; + c = ni * M_PI / _radio; os << "splot v * cos(u), v * sin(u), "; cosine(os, c, alpha); os << ";" << endl; } } void basis_cosine::plot_atoms(ostream & os, const a_vec & A) { - real_t d = 1.0 / (r - 1); + real_t d = 1.0 / (n_rot - 1); real_t c; - for(size_t k = 0, ni = 1; ni <= n; ni++ ) + for(size_t k = 0, ni = 1; ni <= n_freq; ni++ ) for(real_t alpha = 0; alpha <= 1; alpha += d, k++) { - c = ni * M_PI / radio; + c = ni * M_PI / _radio; os << " + " << A(k) << " * "; cosine(os, c, alpha); } } diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index 51a26e7c..b87b28fa 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -8,32 +8,32 @@ namespace gproshan::mdict { -basis_dct::basis_dct(const size_t & _n, const real_t & _radio) +basis_dct::basis_dct(const size_t & n, const real_t & r): basis(r, n * n), n_freq(n) {} + +void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { - radio = _radio; - n = _n; - dim = n * n; + assert(phi.n_cols == _dim); + + for(index_t k = 0, nx = 0; nx < n_freq; nx++) + for(index_t ny = 0; ny < n_freq; ny++, k++) + phi.col(k) = dct(x, y, nx, ny); } -void basis_dct::discrete(a_mat & phi, const a_mat & xy) +void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y) { - assert(phi.n_cols == dim); + assert(phi.n_cols == _dim); - a_vec x = xy.row(0).t(); - a_vec y = xy.row(1).t(); - for(index_t k = 0, nx = 0; nx < n; nx++) - for(index_t ny = 0; ny < n; ny++, k++) - { + for(index_t k = 0, nx = 0; nx < n_freq; nx++) + for(index_t ny = 0; ny < n_freq; ny++, k++) phi.col(k) = dct(x, y, nx, ny); - } } void basis_dct::plot_basis(ostream & os) { - os << "set multiplot layout " << n << "," << n << " rowsfirst scale 1.2;" << endl; + os << "set multiplot layout " << n_freq << "," << n_freq << " rowsfirst scale 1.2;" << endl; - for(index_t nx = 0; nx < n; nx++) - for(index_t ny = 0; ny < n; ny++) + for(index_t nx = 0; nx < n_freq; nx++) + for(index_t ny = 0; ny < n_freq; ny++) { os << "splot v * cos(u), v * sin(u), "; dct(os, nx, ny); os << ";" << endl; } @@ -41,8 +41,8 @@ void basis_dct::plot_basis(ostream & os) void basis_dct::plot_atoms(ostream & os, const a_vec & A) { - for(index_t k = 0, nx = 0; nx < n; nx++) - for(index_t ny = 0; ny < n; ny++, k++) + for(index_t k = 0, nx = 0; nx < n_freq; nx++) + for(index_t ny = 0; ny < n_freq; ny++, k++) { os << " + " << A(k) << " * "; dct(os, nx, ny); } @@ -50,20 +50,26 @@ void basis_dct::plot_atoms(ostream & os, const a_vec & A) a_vec basis_dct::dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny) { - return cos( (M_PI * x * nx) / radio ) % cos( (M_PI * y * ny) / radio ); + return cos(M_PI * nx * x / _radio ) % cos(M_PI * ny * y / _radio); +} + + +a_vec basis_dct::d_dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny) +{ + return - (M_PI * nx / _radio) * (sin(M_PI * nx * x / _radio) % cos(M_PI * ny * y / _radio)); } void basis_dct::dct(ostream & os, const index_t & nx, const index_t & ny) { - os << "cos( (pi * v * cos(u) * " << nx << " ) / " << radio << " ) *"; - os << "cos( (pi * v * sin(u) * " << ny << " ) / " << radio << " )"; + os << "cos( (pi * v * cos(u) * " << nx << " ) / " << _radio << " ) *"; + os << "cos( (pi * v * sin(u) * " << ny << " ) / " << _radio << " )"; } -double basis_dct::get_frequency(size_t idx) + +real_t basis_dct::freq(const index_t & idx) { - if(idx == 0 ) return INFINITY; - int tmp = (idx/n > idx%n)? idx/n:idx%n; - return (2*radio/tmp); + return !idx ? INFINITY : 2 * _radio / max(idx / n_freq, idx % n_freq); } + } // namespace gproshan::mdict diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 5bfe77b3..9feba199 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -46,7 +46,7 @@ size_t dictionary::T = 5; dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _d_plot): mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), learn(_learn), d_plot(_d_plot) { - A.eye(phi_basis->dim, m); + A.eye(phi_basis->dim(), m); dist = new real_t[mesh->n_vertices()]; } @@ -59,7 +59,7 @@ void dictionary::learning() { gproshan_log(MDICT); - string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim) + '_' + to_string(m) + '_' + to_string(f) + '_' + to_string(L) + ".dict"); + string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim()) + '_' + to_string(m) + '_' + to_string(f) + '_' + to_string(L) + ".dict"); if(learn) { @@ -67,7 +67,7 @@ void dictionary::learning() if(!A.load(f_dict)) { - //A.eye(phi_basis->dim, m); + //A.eye(phi_basis->dim(), m); //initialize with some alpha //random //arma::uvec r_ind = arma::randi(m, arma::distr_param(0, M)); @@ -79,7 +79,7 @@ void dictionary::learning() A = U.cols(0,m); gproshan_debug(svd done!); A = normalise(A); - gproshan_debug_var(phi_basis->radio); + gproshan_debug_var(phi_basis->radio()); gproshan_debug_var(m); // @@ -89,9 +89,9 @@ void dictionary::learning() A.save(f_dict); } } - else A.eye(phi_basis->dim, m); - gproshan_debug_var(phi_basis->radio); - assert(A.n_rows == phi_basis->dim); + else A.eye(phi_basis->dim(), m); + gproshan_debug_var(phi_basis->radio()); + assert(A.n_rows == phi_basis->dim()); assert(A.n_cols == m); if(d_plot) { @@ -118,20 +118,20 @@ void dictionary::init_sampling() if(M == 0) { M = mesh->n_vertices(); - phi_basis->radio = mesh->mean_edge(); + phi_basis->radio() = mesh->mean_edge(); } else { sampling.reserve(M); - if(!load_sampling(sampling, phi_basis->radio, mesh, M)) + if(!load_sampling(sampling, phi_basis->radio(), mesh, M)) cerr << "Failed to load sampling" << endl; } - s_radio = phi_basis->radio; - phi_basis->radio *= f; + s_radio = phi_basis->radio(); + phi_basis->radio() *= f; gproshan_debug_var(s_radio); - gproshan_debug_var(phi_basis->radio); + gproshan_debug_var(phi_basis->radio()); } void dictionary::load_curvatures(a_vec & curvatures) @@ -263,7 +263,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) for(index_t s = 0; s < M; s++) { index_t v = sample(s); - patches[s].init(mesh, v, dictionary::T, phi_basis->radio, toplevel); + patches[s].init(mesh, v, dictionary::T, phi_basis->radio(), toplevel); } @@ -301,8 +301,8 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) patch & p = patches[s]; p.transform(); - p.phi.set_size(p.xyz.n_cols, phi_basis->dim); - phi_basis->discrete(p.phi, p.xyz); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); p.phi = normalise(p.phi); } @@ -310,7 +310,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) #ifndef NDEBUG CImgList imlist; for(index_t s = 0; s < M; s++) - patches[s].save(phi_basis->radio, 16, imlist); + patches[s].save(phi_basis->radio(), 16, imlist); imlist.save_ffmpeg_external("tmp/patches.mpg", 5); #endif @@ -340,8 +340,9 @@ real_t dictionary::mesh_reconstruction(const fmask_t & mask) gproshan_log(MDICT); assert(n_vertices == mesh->n_vertices()); - return mdict::mesh_reconstruction(mesh, M, phi_basis->get_radio(), patches, patches_map, A, alpha, dist); + return mdict::mesh_reconstruction(mesh, M, phi_basis->radio(), patches, patches_map, A, alpha, dist); } + void dictionary::update_alphas(a_mat & alpha, size_t threshold) { size_t np_new = M - threshold; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5b020728..44aa3c85 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -354,12 +354,12 @@ void inpainting::init_radial_feature_patches() patch & p = patches[s]; p.transform(); - p.scale_xyz(phi_basis->get_radio()); + p.scale_xyz(phi_basis->radio()); // adding add extra function p.add_extra_xyz_disjoint(mesh,patches_map, s); p.compute_avg_distance(mesh, patches_map, s); - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); } bool save_all = true; @@ -484,8 +484,8 @@ void inpainting::init_voronoi_patches() patch & p = patches[s]; p.transform(); - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); p.compute_avg_distance(mesh, patches_map, s); } @@ -536,9 +536,9 @@ real_t inpainting::execute() patch & p = patches[s]; p.transform(); - p.scale_xyz(phi_basis->get_radio()); - p.phi.set_size(p.xyz.n_cols, phi_basis->get_dim()); - phi_basis->discrete(p.phi, p.xyz); + p.scale_xyz(phi_basis->radio()); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); } @@ -590,7 +590,7 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) if( S(i,3) > max_radio) max_radio = S(i,3); size_t total_points = 0; - A.eye(phi_basis->get_dim(), m); + A.eye(phi_basis->dim(), m); for(index_t i = 0; i < M; i++) @@ -608,16 +608,19 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) T(0,2) = S(i,10); T(1,2) = S(i,11); T(2,2) = S(i,12); + + patch & p = patches[i]; - patches[i].init_random(c, T, radio, max_radio, per, fr); - total_points += patches[i].vertices.size(); - patches[i].phi.set_size(patches[i].vertices.size(), phi_basis->get_dim()); - phi_basis->discrete(patches[i].phi, patches[i].xyz); + p.init_random(c, T, radio, max_radio, per, fr); + p.phi.set_size(p.vertices.size(), phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); a_vec x = patches[i].phi * A * alpha.col(i); patches[i].xyz.row(2) = x.t(); + total_points += patches[i].vertices.size(); + for(index_t j = 0; j < patches[i].vertices.size(); j++) if (patches[i].xyz(2, j) > 2) gproshan_debug_var( i); @@ -630,7 +633,7 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) for(index_t i = 0; i < M; i++) { - patches[i].iscale_xyz(phi_basis->get_radio()); + patches[i].iscale_xyz(phi_basis->radio()); patches[i].itransform(); } diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 5e060158..98021be2 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -243,9 +243,9 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) arma::uchar_vec mask; mask.zeros(A.n_cols); for(index_t i = 0; i < A.n_cols; i++) - if(phi_basis->get_frequency(i) >= 0.5*p.avg_dist) //2.5* if it ismin + if(phi_basis->freq(i) >= 0.5*p.avg_dist) //2.5* if it ismin { - // gproshan_debug_var(phi_basis->get_frequency(i)); + // gproshan_debug_var(phi_basis->freq(i)); // gproshan_debug_var(p.avg_dist); mask(i) = 1; } From 612a21ec456124d042f9c757f83fd809d4a65a9f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 29 Jun 2020 20:07:31 +0200 Subject: [PATCH 0331/1018] cleaning up code inpainting --- include/mdict/inpainting.h | 7 +- include/mdict/patch.h | 4 +- src/mdict/inpainting.cpp | 217 +++++++++++++++---------------------- src/mdict/patch.cpp | 52 ++++----- 4 files changed, 113 insertions(+), 167 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 3fa964b8..d3fff1da 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -19,7 +19,7 @@ class inpainting : public dictionary { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p); - virtual ~inpainting() = default; + virtual ~inpainting(); real_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); @@ -30,8 +30,8 @@ class inpainting : public dictionary che * point_cloud_reconstruction(real_t per, real_t fr); vector sort_indexes(const vector &v); - real_t execute_tmp(); + private: size_t avg_p; size_t percent; @@ -40,7 +40,8 @@ class inpainting : public dictionary bool * mask; double area_thres; size_t max_points; - + + std::string key_name; }; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 3726c532..9ddc20f3 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -67,14 +67,14 @@ class patch real_t & geo_radio, che * mesh, const index_t & v, - const real_t & radio, const real_t & delta, const real_t & sum_thres, const real_t & area_thres, const real_t & area_mesh ); - void init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr); + void init_random(const vertex & c, const arma::mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr); + void recover_radial_disjoint(che * mesh, const real_t & radio_, const index_t & v); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 44aa3c85..5b796401 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -22,11 +22,18 @@ inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const para percent = p.percentage; area_thres = p.area_thres; + key_name = mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres); + M = mesh->n_vertices() / avg_p; + mask = new bool[mesh->n_vertices()]; memset(mask, 0, sizeof(bool) * mesh->n_vertices()); } +inpainting::~inpainting() +{ + delete [] mask; +} void inpainting::load_mask() { @@ -55,7 +62,6 @@ void inpainting::load_mask() size_t rn = 0; while (k < percentage) { - rn = distribution(generator); if(!mask[rn]) { @@ -63,11 +69,9 @@ void inpainting::load_mask() V(rn) = 1; k++; } - } V.save(f_mask); } - } void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) @@ -139,67 +143,38 @@ void inpainting::load_mask(const std::vector * vertices, const index_t void inpainting::load_sampling(bool save_all) { - // saving sampling - //double delta = PI/6; - //double sum_thres = 0.008; // best arma 0.0001 the worst with 0.001, now with 0.0005 lets see tomorrow - //double sum_thres = PI; - string f_sampl = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".rsampl"); - - arma::mat S; - + size_t featsize; vector all_sorted_features; vector seeds; - size_t featsize; load_features(all_sorted_features, featsize); gproshan_debug_var(all_sorted_features.size()); - string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); -// vector features(all_sorted_features.begin(), all_sorted_features.begin() + featsize ); -// gproshan_debug_var(features.size()); -// geodesics geo(mesh, features , geodesics::FM, NULL, false, mesh->n_vertices()); -// index_t * indexes = new index_t[geo.n_sorted_index()]; -// geo.copy_sorted_index(indexes, geo.n_sorted_index()); size_t count = 0; -// real_t max_radio = geo[indexes[mesh->n_vertices()-1]] ; - - //radio *= 1.1; real_t area_mesh = mesh->area_surface(); - real_t max_radio = 0.05 * area_mesh; - gproshan_debug_var(max_radio); - if(S.load(f_sampl)) + a_vec S; + if(S.load(tmp_file_path(key_name + ".rsampl"))) { - gproshan_debug(already done); + gproshan_debug(loading sampling); + size_t n_seeds = S.n_rows; real_t euc_radio, geo_radio; for(index_t i = 0; i < n_seeds; i++) { patch p; - // gproshan_debug_var(i); - //p.recover_radial_disjoint( mesh, S(i,1), S(i,0) ); - p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i, 0), S(i, 1), delta, sum_thres, area_thres, area_mesh); - patches.push_back(p); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), delta, sum_thres, area_thres, area_mesh); + patches.push_back(move(p)); } M = n_seeds; - string f_points = tmp_file_path(mesh->name_size() + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".points"); - gproshan_debug_var(n_seeds); - a_vec outlv(n_seeds); - gproshan_debug(restored); - for(index_t i = 0; i < n_seeds; i++) - { - outlv(i) = S(i,0); - } - outlv.save(f_points); - gproshan_debug(restored); - + return; } //ELSE IF !S.load(f_sample) - gproshan_debug(SAMPLING); + gproshan_debug(computing sampling); // compute features will be seeds patches_map.resize(mesh->n_vertices()); @@ -226,10 +201,8 @@ void inpainting::load_sampling(bool save_all) if(invalid_seed[vsf]) continue; patch p; - // increasing a bit the radio - p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, max_radio, delta, sum_thres, area_thres, area_mesh); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, delta, sum_thres, area_thres, area_mesh); - //gproshan_debug_var(p.vertices.size()); count_cov_patch = 0; if(p.vertices.size() >= 7 ) { @@ -239,8 +212,7 @@ void inpainting::load_sampling(bool save_all) count_cov += count_cov_patch; if(count_cov_patch > 0) { - //gproshan_debug_var(p.vertices.size()); - patches.push_back(p); + patches.push_back(move(p)); seeds.push_back(vsf); radios.push_back(euc_radio); geo_radios.push_back(geo_radio); @@ -264,63 +236,46 @@ void inpainting::load_sampling(bool save_all) delete [] invalid_seed; - vector outliers; + M = seeds.size(); + + gproshan_log(saving sampling); + + S.resize(seeds.size()); + + #pragma omp parallel for + for(index_t i = 0; i < seeds.size(); i++) + S(i) = seeds[i]; + + S.save(tmp_file_path(key_name + ".rsampl")); + gproshan_debug_var(sum_thres); gproshan_debug_var(count); gproshan_debug_var(count_cov); gproshan_debug_var(seeds.size()); - M = seeds.size(); + gproshan_debug_var(M); - gproshan_debug(SAMPLING); - /////////////////////////////////////// #ifndef NDEBUG - gproshan_debug_var(M); - index_t tmp; - bool remark[mesh->n_vertices()] = {}; + vector outliers; gproshan_debug_var(outliers.size()); for(index_t i = 0; i < mesh->n_vertices(); i++) - { - if(!covered[i] ) - { + if(!covered[i]) outliers.push_back(i); - } - } - a_vec outlv(outliers.size() ); - //gproshan_debug_var(seeds.size()); + + a_vec outlv(outliers.size()); for(index_t i = 0; i < outliers.size(); i++) - { - //outlv(i) = seeds[i]; outlv(i) = outliers[i]; - //gproshan_debug_var(seeds[i]); - } - gproshan_debug_var(f_points); - outlv.save(f_points); - S.resize(seeds.size(),2); - for(index_t i = 0; i < seeds.size(); i++) - { - S(i,0) = seeds[i]; - S(i,1) = geo_radios[i]; - } - S.save(f_sampl); + outlv.save(tmp_file_path(key_name + ".outlv")); #endif // NDEBUG } void inpainting::init_radial_feature_patches() { load_sampling(false); - load_mask(); - //Initializing patches - gproshan_log(initializing patches); - n_vertices = mesh->n_vertices(); - patches.resize(M); - patches_map.resize(n_vertices); - //initializing patch - gproshan_debug_var(M); #ifndef NDEBUG size_t patch_avg_size = 0; size_t patch_min_size = NIL; @@ -342,11 +297,18 @@ void inpainting::init_radial_feature_patches() gproshan_debug_var(patch_max_size); #endif + //Initializing patches + gproshan_log(initializing patches); + gproshan_debug_var(M); + + n_vertices = mesh->n_vertices(); + patches.resize(M); + patches_map.resize(n_vertices); + bool * pmask = mask; + for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - - gproshan_debug(passed); + patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); #pragma omp parallel for for(index_t s = 0; s < M; s++) @@ -355,7 +317,6 @@ void inpainting::init_radial_feature_patches() p.transform(); p.scale_xyz(phi_basis->radio()); - // adding add extra function p.add_extra_xyz_disjoint(mesh,patches_map, s); p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); @@ -384,11 +345,11 @@ void inpainting::init_radial_feature_patches() AS(i,10) = p.T(0,2); AS(i,11) = p.T(1,2); AS(i,12) = p.T(2,2); - } - string f_samplall = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); - AS.save(f_samplall); + + AS.save(tmp_file_path(key_name + ".smp")); } + gproshan_log(radial patches are ready); } @@ -510,10 +471,9 @@ real_t inpainting::execute() // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".alpha"); - save_alpha(f_alpha); - + save_alpha(tmp_file_path(key_name + ".alpha")); + draw_patches(295); draw_patches(384); draw_patches(319); @@ -565,40 +525,37 @@ real_t inpainting::execute() gproshan_debug_var(non_zero.size()); real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); gproshan_debug_var(ratio); - + + return 0; } che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) { arma::mat S; - arma::mat T(3,3); arma::mat alpha; - - string f_smp = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres) + ".smp"); - string f_alpha = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) +'_' + to_string(area_thres) + ".alpha"); - - S.load(f_smp); - alpha.load(f_alpha); - gproshan_debug_var(S.n_rows); + S.load(tmp_file_path(key_name + ".smp")); + alpha.load(tmp_file_path(key_name + ".alpha")); + M = S.n_rows; - real_t radio, max_radio = -1; patches.resize(M); - vertex c; + gproshan_debug_var(M); + + real_t max_radio = -1; + + #pragma omp parallel for reduction(max: max_radio) for(index_t i = 0; i < M; i++) - if( S(i,3) > max_radio) max_radio = S(i,3); + max_radio = max(max_radio, S(i, 3)); - size_t total_points = 0; A.eye(phi_basis->dim(), m); - + size_t total_points = 0; + + #pragma omp parallel for for(index_t i = 0; i < M; i++) { - c.x = S(i,0); - c.y = S(i,1); - c.z = S(i,2); - radio = S(i,3); + arma::mat T(3,3); T(0,0) = S(i,4); T(1,0) = S(i,5); T(2,0) = S(i,6); @@ -611,32 +568,29 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) patch & p = patches[i]; - p.init_random(c, T, radio, max_radio, per, fr); + p.init_random({S(i, 0), S(i, 1), S(i, 2)}, T, S(i, 3), max_radio, per, fr); p.phi.set_size(p.vertices.size(), phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - a_vec x = patches[i].phi * A * alpha.col(i); - patches[i].xyz.row(2) = x.t(); + p.xyz.row(2) = x.t(); + + p.iscale_xyz(phi_basis->radio()); + p.itransform(); - total_points += patches[i].vertices.size(); + #pragma omp atomic + total_points += p.vertices.size(); for(index_t j = 0; j < patches[i].vertices.size(); j++) - if (patches[i].xyz(2, j) > 2) - gproshan_debug_var( i); - - + if(patches[i].xyz(2, j) > 2) + { + gproshan_debug_var(i); + break; + } } gproshan_debug_var(total_points); - for(index_t i = 0; i < M; i++) - { - - patches[i].iscale_xyz(phi_basis->radio()); - patches[i].itransform(); - } - vector point_cloud; point_cloud.reserve(total_points); @@ -647,16 +601,13 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) patches[i].xyz(2, j) }); - che * nmesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); - - gproshan_debug_var(sum_thres); - string f_pc = tmp_file_path(mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres)+ '_' + to_string(area_thres) - + '_' + to_string(per) + '_' + to_string(fr) + "_pc"); + che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); - che_off::write_file(nmesh, f_pc); - gproshan_debug(Done!); + che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc")); - return nmesh; + gproshan_debug(saved new point cloud); + + return new_mesh; } @@ -697,6 +648,8 @@ real_t inpainting::execute_tmp() TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); + + return 0; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index eb48fe88..94e7825f 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -142,14 +142,15 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N return added; } -void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, real_t per, real_t fr) +void patch::init_random(const vertex & c, const arma::mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr) { + this->radio = radio; this->T = T; + x.resize(3); x(0) = c.x; x(1) = c.y; x(2) = c.z; - this->radio = radio; std::random_device rd; //Will be used to obtain a seed for the random number engine @@ -159,7 +160,7 @@ void patch::init_random(vertex c, arma::mat T, real_t radio, real_t max_radio, r // free the parameters to the interface // fix the point cloud viewer - size_t n_points = (radio/max_radio) * per; // change this using a sigmoid function + size_t n_points = (radio / max_radio) * percent; // change this using a sigmoid function vertices.resize(n_points); xyz.resize(3,n_points); @@ -231,7 +232,6 @@ void patch::init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, const index_t & v, - const real_t & max_radio, const real_t & delta, const real_t & sum_thres, const real_t & area_thres, @@ -267,7 +267,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, ratio = area / proj_area; - if(add_vertex_by_faces(c, n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI/2.5 ) && (ratio < sum_thres || (area/area_mesh) < area_thres) ) + if(add_vertex_by_faces(c, n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); return true; @@ -283,9 +283,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, size_t d_fitting = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; if(vertices.size() > min_points) - { jet_fit_directions(mesh, v); - } else normal_fit_directions(mesh,v); @@ -293,23 +291,17 @@ void patch::init_radial_disjoint( real_t & euc_radio, radio = -INFINITY; vertex p; - for(index_t i=1; i < vertices.size(); i++) + for(auto & vi: vertices) { - p = mesh->get_vertex(vertices[i]); - p = p - c ; - p = p - ((p,n)*n); + p = mesh->get_vertex(vi); - if(*p > radio) - { - radio = *p; - } + p = p - c ; + p = p - ((p, n) * n); + radio = max(radio, *p); } - //gproshan_debug_var(vertices.size()); - //gproshan_debug_var(geo.n_sorted_index()); geo_radio = geo[vertices.back()]; - //gproshan_debug_var(vertices.size()); } // xyz = E.t * (xyz - avg) @@ -467,6 +459,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co } } } + void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); @@ -481,23 +474,22 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorgt(vertices[i]); - xyz(0, j) = v.x; - xyz(1, j) = v.y; - xyz(2, j) = v.z; - //vpatches[vertices[i]].push_back({p, j++}); - vpatches[vertices[i]][p] = j++; + const vertex & v = mesh->gt(vi); + xyz(0, i) = v.x; + xyz(1, i) = v.y; + xyz(2, i) = v.z; + + vpatches[vi][p] = i++; } } - } void patch::scale_xyz(const real_t & radio_f) From f53ca217b5692030bb4106dfe799b41e16fc0f44 Mon Sep 17 00:00:00 2001 From: Lish Date: Mon, 29 Jun 2020 22:38:43 +0200 Subject: [PATCH 0332/1018] fixing parameters --- include/mdict/inpainting.h | 4 +++- src/app_viewer.cpp | 8 +++----- src/che.cpp | 2 +- src/mdict/d_mesh.cpp | 2 +- src/mdict/inpainting.cpp | 33 ++++++++++++++++----------------- src/mdict/mdict.cpp | 1 + src/mdict/patch.cpp | 2 +- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index d3fff1da..682448ff 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -21,6 +21,8 @@ class inpainting : public dictionary inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p); virtual ~inpainting(); + operator const std::string & () const; + real_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); void load_mask(); @@ -40,7 +42,7 @@ class inpainting : public dictionary bool * mask; double area_thres; size_t max_points; - + std::string key_name; }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7c7bf79d..8b0c84d2 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -616,9 +616,9 @@ bool app_viewer::process_inpaiting(viewer * p_view) { basis_dct phi(n); inpainting dict(mesh, &phi, params); - dict.execute(); + real_t max_error = dict.execute(); - mesh->update_colors(&dict[0], 0.03); + mesh->update_colors(&dict[0], 0.0068); mesh->update_normals(); } @@ -650,9 +650,7 @@ bool app_viewer::process_mask(viewer * p_view) dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); mesh->update_colors(&dict[0]); - string f_points = tmp_file_path(mesh->name_size() + "_" + - to_string(params.sum_thres) + "_" + - to_string(params.area_thres) + ".points"); + string f_points = tmp_file_path(string(dict) + ".rsampl"); a_vec points_out; gproshan_debug_var(f_points); diff --git a/src/che.cpp b/src/che.cpp index 0239e3ad..96c9a346 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -247,7 +247,7 @@ void che::update_colors(const real_t * vcolor, real_t max_color) { #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VC[v] = 0.5; + VC[v] = 0.3; return; } diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 9b40096a..7e9e036f 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -321,7 +321,7 @@ real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, vector & patches, vector & patches_map, const real_t & h) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 5b796401..2da926a2 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -35,6 +35,11 @@ inpainting::~inpainting() delete [] mask; } +inpainting::operator const std::string & () const +{ + return key_name; +} + void inpainting::load_mask() { //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); @@ -227,7 +232,7 @@ void inpainting::load_sampling(bool save_all) const vertex & va = mesh->get_vertex(vsf); const vertex & vb = mesh->get_vertex(v); - invalid_seed[v] = *(va - vb) < 0.4 * euc_radio; + invalid_seed[v] = *(va - vb) < 0.8 * euc_radio; } } } @@ -258,7 +263,7 @@ void inpainting::load_sampling(bool save_all) #ifndef NDEBUG vector outliers; - gproshan_debug_var(outliers.size()); + for(index_t i = 0; i < mesh->n_vertices(); i++) if(!covered[i]) outliers.push_back(i); @@ -459,11 +464,11 @@ real_t inpainting::execute() TIC(d_time) init_radial_feature_patches(); TOC(d_time) gproshan_debug_var(d_time); -// L = 15; + //L = 10; // sparse coding and reconstruction with all patches - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); + //TIC(d_time) sparse_coding(); TOC(d_time) + //gproshan_debug_var(d_time); TIC(d_time) learning(); TOC(d_time) gproshan_debug_var(d_time); @@ -472,11 +477,9 @@ real_t inpainting::execute() TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - save_alpha(tmp_file_path(key_name + ".alpha")); - draw_patches(295); - draw_patches(384); - draw_patches(319); + //draw_patches(384); + //draw_patches(319); draw_patches(312); //patches_map.resize(n_vertices); for(index_t i = 0; i < n_vertices; i++) @@ -505,10 +508,7 @@ real_t inpainting::execute() bool *pmask; - /*draw_patches(295); - draw_patches(384); - draw_patches(319); - draw_patches(312);*/ + //draw_patches(76); //draw_patches(1486); @@ -518,15 +518,14 @@ real_t inpainting::execute() //phi_basis->plot_basis(); //gproshan_debug_var(alpha.col(463)); - - TIC(d_time) mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) + real_t max_error; + TIC(d_time) max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); gproshan_debug_var(ratio); - - return 0; + } che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 98021be2..a24e15ac 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -259,6 +259,7 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, #pragma omp parallel for for(index_t i = 0; i < patches.size(); i++) alpha.col(i) = OMP(patches[i],phi_basis, A, L); + return alpha; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 94e7825f..5e2fef81 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -239,7 +239,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, ) { radio = -INFINITY; - min_nv = 0; + min_nv = 128; euc_radio = -INFINITY; From c4690a45fc5e9c53ace99d18ccd7884921fca259 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 10:31:05 +0200 Subject: [PATCH 0333/1018] cleaning mesh recosntruction, add patches_error --- include/mdict/d_mesh.h | 2 - include/mdict/dictionary.h | 3 +- src/mdict/d_mesh.cpp | 83 --------------------------------- src/mdict/dictionary.cpp | 94 ++++++++++++++++++++++++++++++++++---- src/mdict/inpainting.cpp | 31 +++---------- 5 files changed, 93 insertions(+), 120 deletions(-) diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index fecdedd9..a6e8fca9 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -111,8 +111,6 @@ void save_patches(std::vector patches, size_t M); void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); -real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, real_t * dist, const fmask_t & mask = nullptr, const index_t & v_i = 0); - a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); [[deprecated]] diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 792364af..499acdd1 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -48,6 +48,7 @@ class dictionary std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. + std::vector > patches_error; double d_time; ///< time of operations. bool learn; @@ -61,7 +62,7 @@ class dictionary public: const real_t & operator[](const index_t & i) const; - void draw_patches(index_t i); + const index_t & draw_patches(const index_t & p); protected: dictionary( che *const & _mesh, ///< pointer to input mesh. diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 7e9e036f..14d46206 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -241,89 +241,6 @@ void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, ve } -real_t mesh_reconstruction(che * mesh, size_t M, const real_t & radio, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, real_t * dist,const fmask_t & mask, const index_t & v_i) -{ - a_mat V(3, mesh->n_vertices(), arma::fill::zeros); - - //Returning to the original coordinates. - #pragma omp parallel for - for(index_t p = 0; p < M; p++) - { - patch & rp = patches[p]; - - if(rp.phi.n_rows) - { - a_vec x = rp.phi * A * alpha.col(p); - rp.xyz.row(2) = x.t(); - - arma::uvec outlier = find(abs(x) > 2); - if(outlier.n_elem) - gproshan_debug_var(p); - - rp.iscale_xyz(radio); - rp.itransform(); - } - } - -// real_t h = 0.2; - //Computes the weighted average of vertices - #pragma omp parallel for - for(index_t v = v_i; v < mesh->n_vertices(); v++) - { - if(patches_map[v].size() && (!mask || mask(v)) ) - V.col(v) = simple_means_vertex(v, patches, patches_map); - //V.col(v) = non_local_means_vertex(alpha, v, patches, patches_map, h); - else - { - V(0, v) = mesh->gt(v).x; - V(1, v) = mesh->gt(v).y; - V(2, v) = mesh->gt(v).z; - } - } - - // ------------------------------------------------------------------------ - - vertex * new_vertices = (vertex *) V.memptr(); - - real_t error = 0; - real_t max_error = -1; - #pragma omp parallel for reduction(+: error) - for(index_t v = v_i; v < mesh->n_vertices(); v++) - { - dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); - if(dist[v] > max_error) max_error = dist[v]; - error += *(new_vertices[v] - mesh->get_vertex(v)); - } - - - gproshan_debug_var(mesh->n_vertices()); - error /= mesh->n_vertices(); - gproshan_debug_var(error); - gproshan_debug_var(max_error); - - gproshan_debug_var(v_i); - mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); - che_off::write_file(mesh,"../tmp/recon_mesh"); -/* -////// Images test - CImg image("../tmp/barbara_input.jpg"); - - CImg image_out(image.width(), image.height()); - //image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; - - index_t v = 0; - for(int i = 0; i < image.width(); i++) - for(int j = 0; j < image.height(); j++) - { - image_out(i,j) = mesh->gt(v++).z; - } - - image_out.save("../tmp/barbara_output.jpg"); - (image,image_out).display();*/ - //real_t error = 0; - return max_error; -} - a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) { a_vec n_a_vec(3, arma::fill::zeros); diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 9feba199..d077eaf1 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -4,6 +4,7 @@ #include "mdict.h" #include "che_poisson.h" #include "che_fill_hole.h" +#include "che_off.h" #include "viewer/viewer.h" @@ -229,17 +230,17 @@ void dictionary::load_features(vector & v_feat, size_t & featsize) } gproshan_debug(exists); - inp>>featsize; - //v_feat.resize(tam); - for(int i=0; i> featsize; + for(index_t i = 0; i < featsize; i++) { inp>>tmp; v_feat.push_back(tmp); } - inp>>tam; - for(int i=0; i> tam; + for(index_t i = 0; i < tam; i++) { - inp>>tmp; + inp >> tmp; v_feat.push_back(tmp); } @@ -340,7 +341,80 @@ real_t dictionary::mesh_reconstruction(const fmask_t & mask) gproshan_log(MDICT); assert(n_vertices == mesh->n_vertices()); - return mdict::mesh_reconstruction(mesh, M, phi_basis->radio(), patches, patches_map, A, alpha, dist); + + a_mat V(3, mesh->n_vertices(), arma::fill::zeros); + + patches_error.resize(M); + + #pragma omp parallel for + for(index_t p = 0; p < M; p++) + { + patch & rp = patches[p]; + + a_vec x = rp.phi * A * alpha.col(p); + + patches_error[p] = { norm(x - rp.xyz.row(2).t()), p }; + + rp.xyz.row(2) = x.t(); + } + + sort(patches_error.begin(), patches_error.end()); + + fprintf(stderr, "error %16s%16s\n", "best", "worst"); + for(index_t i = 0; i < 10; i++) + { + const index_t & best = patches_error[i].second; + const index_t & worst = patches_error[M - i - 1].second; + + fprintf(stderr, "%5d:%8u>%8u%8u>%8u\n", i, best, draw_patches(best), worst, draw_patches(worst)); + } + + #pragma omp parallel for + for(index_t p = 0; p < M; p++) + { + patch & rp = patches[p]; + rp.iscale_xyz(phi_basis->radio()); + rp.itransform(); + } + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + if(patches_map[v].size() && (!mask || mask(v)) ) + V.col(v) = simple_means_vertex(v, patches, patches_map); + else + { + V(0, v) = mesh->gt(v).x; + V(1, v) = mesh->gt(v).y; + V(2, v) = mesh->gt(v).z; + } + } + + // ------------------------------------------------------------------------ + + vertex * new_vertices = (vertex *) V.memptr(); + + real_t error = 0; + real_t max_error = -1; + + #pragma omp parallel for reduction(+: error) reduction(max: max_error) + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); + error += dist[v]; + max_error = max(max_error, dist[v]); + } + + error /= mesh->n_vertices(); + + gproshan_debug_var(mesh->n_vertices()); + gproshan_debug_var(error); + gproshan_debug_var(max_error); + + mesh->set_vertices(new_vertices, mesh->n_vertices()); + che_off::write_file(mesh, "../tmp/recon_mesh"); + + return max_error; } void dictionary::update_alphas(a_mat & alpha, size_t threshold) @@ -399,10 +473,10 @@ const real_t & dictionary::operator[](const index_t & i) const return dist[i]; } -void dictionary::draw_patches(index_t i) +const index_t & dictionary::draw_patches(const index_t & p) { - gproshan_debug_var(patches[i].vertices[0]); - phi_basis->plot_patch(A*alpha.col(i),patches[i].xyz, patches[i].vertices[0]); + phi_basis->plot_patch(A * alpha.col(p), patches[p].xyz, patches[p].vertices[0]); + return patches[p].vertices[0]; } void dictionary::save_alpha(string file) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 2da926a2..dd994e33 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -478,21 +478,15 @@ real_t inpainting::execute() gproshan_debug_var(d_time); draw_patches(295); - //draw_patches(384); - //draw_patches(319); draw_patches(312); - //patches_map.resize(n_vertices); + + #pragma omp parallel for for(index_t i = 0; i < n_vertices; i++) - { patches_map[i].clear(); - } - for(index_t s = 0; s < M; s++) patches[s].reset_xyz(mesh, patches_map, s, 0); - - #pragma omp parallel for for(index_t s = 0; s < M; s++) { @@ -502,30 +496,19 @@ real_t inpainting::execute() p.scale_xyz(phi_basis->radio()); p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - } - bool *pmask; - - + bool * pmask = mask; - //draw_patches(76); - //draw_patches(1486); - - //draw_patches(400); - //draw_patches(500); - //draw_patches(56); - //phi_basis->plot_basis(); - //gproshan_debug_var(alpha.col(463)); - - real_t max_error; - TIC(d_time) max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) + TIC(d_time) + real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); + TOC(d_time) gproshan_debug_var(d_time); + arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); gproshan_debug_var(ratio); - } che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) From f141825f23d072ff323252b0ffa19fd0dc870af6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 10:51:25 +0200 Subject: [PATCH 0334/1018] error per patch --- src/mdict/dictionary.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index d077eaf1..295cd6a0 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -353,7 +353,7 @@ real_t dictionary::mesh_reconstruction(const fmask_t & mask) a_vec x = rp.phi * A * alpha.col(p); - patches_error[p] = { norm(x - rp.xyz.row(2).t()), p }; + patches_error[p] = { accu(abs(x - rp.xyz.row(2).t())) / rp.vertices.size(), p }; rp.xyz.row(2) = x.t(); } From e92e593f46f89d4b8dac601fcfea0af525c634ee Mon Sep 17 00:00:00 2001 From: Lish Date: Tue, 30 Jun 2020 10:55:14 +0200 Subject: [PATCH 0335/1018] update colors max value --- src/app_viewer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 8b0c84d2..8e90eaf0 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -618,7 +618,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) inpainting dict(mesh, &phi, params); real_t max_error = dict.execute(); - mesh->update_colors(&dict[0], 0.0068); + mesh->update_colors(&dict[0]); mesh->update_normals(); } From b2006d6e8a22498d66128dacd864601100e2d163 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 14:05:39 +0200 Subject: [PATCH 0336/1018] computing normals pc reconstruction --- src/mdict/inpainting.cpp | 41 +++++++++++++++++++++++++++++++++------- src/mdict/patch.cpp | 3 +-- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index dd994e33..a7f33dd8 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -476,7 +476,9 @@ real_t inpainting::execute() // sparse coding and reconstruction with all patches TIC(d_time) sparse_coding(); TOC(d_time) gproshan_debug_var(d_time); - + + save_alpha(tmp_file_path(key_name + ".alpha")); + draw_patches(295); draw_patches(312); @@ -551,19 +553,19 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) patch & p = patches[i]; p.init_random({S(i, 0), S(i, 1), S(i, 2)}, T, S(i, 3), max_radio, per, fr); - p.phi.set_size(p.vertices.size(), phi_basis->dim()); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - a_vec x = patches[i].phi * A * alpha.col(i); + a_vec x = p.phi * A * alpha.col(i); p.xyz.row(2) = x.t(); p.iscale_xyz(phi_basis->radio()); p.itransform(); #pragma omp atomic - total_points += p.vertices.size(); + total_points += p.xyz.n_cols; - for(index_t j = 0; j < patches[i].vertices.size(); j++) + for(index_t j = 0; j < patches[i].xyz.n_cols; j++) if(patches[i].xyz(2, j) > 2) { gproshan_debug_var(i); @@ -577,15 +579,40 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) point_cloud.reserve(total_points); for(index_t i = 0; i < M; i++) - for(index_t j = 0; j < patches[i].vertices.size(); j++) + for(index_t j = 0; j < patches[i].xyz.n_cols; j++) point_cloud.push_back({ patches[i].xyz(0, j), patches[i].xyz(1, j), patches[i].xyz(2, j) }); che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); + new_mesh->update_normals(); + + vertex vdx, vdy; + for(index_t v = 0, i = 0; i < M; i++) + { + patch & p = patches[i]; + + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + a_vec dx = p.phi * A * alpha.col(i); + + phi_basis->d_discrete(p.phi, p.xyz.row(1).t(), p.xyz.row(0).t()); + a_vec dy = p.phi * A * alpha.col(i); + + for(index_t j = 0; j < patches[i].xyz.n_cols; j++, v++) + { + vertex & n = new_mesh->normal(v); + + vdx = {1, 0, dx(j)}; + vdy = {0, 1, dy(j)}; + + n = vdx * vdy; + n /= *n; + // n = {p.T(0, 2), p.T(1, 2), p.T(2, 2)}; + } + } - che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc")); + che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc"), che_off::NOFF); gproshan_debug(saved new point cloud); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 5e2fef81..97d88c7b 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -161,8 +161,7 @@ void patch::init_random(const vertex & c, const arma::mat & T, const real_t & ra // free the parameters to the interface // fix the point cloud viewer size_t n_points = (radio / max_radio) * percent; // change this using a sigmoid function - vertices.resize(n_points); - xyz.resize(3,n_points); + xyz.resize(3, n_points); xyz(0, 0) = 0; xyz(1, 0) = 0; From 3db45ebc6fe4ec2c4f43201ffdb481f328e6fed6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 15:21:12 +0200 Subject: [PATCH 0337/1018] computing normas per patch pc reconstruction --- include/mdict/basis.h | 2 +- include/mdict/basis_dct.h | 2 +- src/mdict/basis_dct.cpp | 4 ++-- src/mdict/inpainting.cpp | 17 ++++++----------- 4 files changed, 10 insertions(+), 15 deletions(-) diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 3dfbd87e..5969cf43 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -24,7 +24,7 @@ class basis basis(const real_t & r, const size_t & d); virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; - virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; + virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) = 0; virtual real_t freq(const index_t & idx) = 0; real_t & radio(); const size_t & dim() const; diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index e391a18b..ab965764 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -17,7 +17,7 @@ class basis_dct: public basis public: basis_dct(const size_t & n, const real_t & r = 1); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); - void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y); + void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b); real_t freq(const index_t & idx); private: diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index b87b28fa..9c9a0bc8 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -19,13 +19,13 @@ void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) phi.col(k) = dct(x, y, nx, ny); } -void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y) +void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) { assert(phi.n_cols == _dim); for(index_t k = 0, nx = 0; nx < n_freq; nx++) for(index_t ny = 0; ny < n_freq; ny++, k++) - phi.col(k) = dct(x, y, nx, ny); + phi.col(k) = !b ? dct(x, y, nx, ny) : dct(y, x, ny, nx); } void basis_dct::plot_basis(ostream & os) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index a7f33dd8..6b8df962 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -588,27 +588,22 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); new_mesh->update_normals(); - vertex vdx, vdy; + a_vec n; for(index_t v = 0, i = 0; i < M; i++) { patch & p = patches[i]; - phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 0); a_vec dx = p.phi * A * alpha.col(i); - phi_basis->d_discrete(p.phi, p.xyz.row(1).t(), p.xyz.row(0).t()); + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); a_vec dy = p.phi * A * alpha.col(i); for(index_t j = 0; j < patches[i].xyz.n_cols; j++, v++) { - vertex & n = new_mesh->normal(v); - - vdx = {1, 0, dx(j)}; - vdy = {0, 1, dy(j)}; - - n = vdx * vdy; - n /= *n; - // n = {p.T(0, 2), p.T(1, 2), p.T(2, 2)}; + n = {-dx(j), -dy(j), 1}; + n = normalise(p.T * n); + new_mesh->normal(v) = {n(0), n(1), n(2)}; } } From af9c5e8a4944e87fb68d8c91df0a6132cbdd4912 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 16:36:26 +0200 Subject: [PATCH 0338/1018] rename shader_program to shader_triangles --- include/viewer/viewer.h | 2 +- src/viewer/viewer.cpp | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index c5280064..eeea34ea 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -61,7 +61,7 @@ class viewer unsigned int idx_colormap = 0; // colormap index defined in shaders/colormap.glsl const std::vector colormap = {"blue", "red", "blue/read"}; - shader shader_program; + shader shader_triangles; shader shader_normals; shader shader_gradient; shader shader_pointcloud; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 17c9b538..d98bd51f 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -282,9 +282,9 @@ void viewer::init_glsl() shader_sphere.load_vertex("../shaders/vertex_sphere.glsl"); shader_sphere.load_fragment("../shaders/fragment_sphere.glsl"); - shader_program.load_vertex("../shaders/vertex.glsl"); - shader_program.load_geometry("../shaders/geometry.glsl"); - shader_program.load_fragment("../shaders/fragment.glsl"); + shader_triangles.load_vertex("../shaders/vertex.glsl"); + shader_triangles.load_geometry("../shaders/geometry.glsl"); + shader_triangles.load_fragment("../shaders/fragment.glsl"); shader_normals.load_vertex("../shaders/vertex_normals.glsl"); shader_normals.load_geometry("../shaders/geometry_normals.glsl"); @@ -661,25 +661,23 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); + glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), idx_colormap); + glProgramUniform3f(shader_triangles, shader_triangles("eye"), eye[1], eye[2], eye[3]); + glProgramUniform3f(shader_triangles, shader_triangles("light"), light[1], light[2], light[3]); + glProgramUniform1i(shader_triangles, shader_triangles("render_flat"), render_flat); + glProgramUniform1i(shader_triangles, shader_triangles("render_lines"), render_lines); + glProgramUniform1i(shader_triangles, shader_triangles("render_wireframe"), render_wireframe_fill); + glProgramUniformMatrix4fv(shader_triangles, shader_triangles("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1ui(shader_program, shader_program("idx_colormap"), idx_colormap); - glProgramUniform3f(shader_program, shader_program("eye"), eye[1], eye[2], eye[3]); - glProgramUniform3f(shader_program, shader_program("light"), light[1], light[2], light[3]); - glProgramUniform1i(shader_program, shader_program("render_flat"), render_flat); - glProgramUniform1i(shader_program, shader_program("render_lines"), render_lines); - glProgramUniform1i(shader_program, shader_program("render_wireframe"), render_wireframe_fill); - glProgramUniformMatrix4fv(shader_program, shader_program("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_program, shader_program("proj_mat"), 1, 0, &proj_mat[0][0]); - - - glProgramUniform1ui(shader_pointcloud, shader_program("idx_colormap"), idx_colormap); + glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), idx_colormap); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[1], eye[2], eye[3]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); - draw_meshes(shader_program); + draw_meshes(shader_triangles); if(render_normal_field) From 6c2c4651cad48e699062e2328fb9b51738b4617a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jun 2020 17:02:29 +0200 Subject: [PATCH 0339/1018] adding nyquist_factor static patch member --- include/mdict/patch.h | 1 + src/app_viewer.cpp | 1 + src/mdict/mdict.cpp | 15 +++++---------- src/mdict/patch.cpp | 1 + 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 9ddc20f3..3c8f9947 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -46,6 +46,7 @@ class patch public: static size_t expected_nv; ///< Expected number of patch vertices. + static real_t nyquist_factor; ///< nyquist factor public: patch() = default; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 8e90eaf0..bc7f4abb 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -605,6 +605,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); + ImGui::InputDouble("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index a24e15ac..0bf8290c 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -235,20 +235,16 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { - return OMP(p.xyz.row(2).t(), p.phi * A, L); } + a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) { - arma::uchar_vec mask; - mask.zeros(A.n_cols); + arma::uchar_vec mask(A.n_cols); + for(index_t i = 0; i < A.n_cols; i++) - if(phi_basis->freq(i) >= 0.5*p.avg_dist) //2.5* if it ismin - { - // gproshan_debug_var(phi_basis->freq(i)); - // gproshan_debug_var(p.avg_dist); - mask(i) = 1; - } + mask(i) = phi_basis->freq(i) >= patch::nyquist_factor * p.avg_dist; // 2.5* if it ismin + return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } @@ -260,7 +256,6 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, for(index_t i = 0; i < patches.size(); i++) alpha.col(i) = OMP(patches[i],phi_basis, A, L); - return alpha; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 97d88c7b..d0e6da82 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -32,6 +32,7 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * dictionary::T * (dictionary::T + 1); +real_t patch::nyquist_factor = 0.5; void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) { From 288e7feb35e49d344cc5015fe0d9bce5cf585ece Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Sep 2020 10:45:56 +0200 Subject: [PATCH 0340/1018] update imgui --- imgui/imconfig.h | 9 +- imgui/imgui.cpp | 1038 ++++++++++++++++-------------- imgui/imgui.h | 287 ++++++--- imgui/imgui_demo.cpp | 718 +++++++++++++-------- imgui/imgui_draw.cpp | 465 +++++++++----- imgui/imgui_impl_glfw.cpp | 2 +- imgui/imgui_impl_opengl3.cpp | 45 +- imgui/imgui_impl_opengl3.h | 3 + imgui/imgui_internal.h | 228 ++++--- imgui/imgui_widgets.cpp | 1152 +++++++++++++++++++++------------- imgui/imstb_textedit.h | 10 +- 11 files changed, 2436 insertions(+), 1521 deletions(-) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index c6817de7..6b87dd6c 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -3,10 +3,11 @@ // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. //----------------------------------------------------------------------------- -// A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/branch with your modifications to imconfig.h) -// B) or add configuration directives in your own file and compile with #define IMGUI_USER_CONFIG "myfilename.h" -// If you do so you need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include -// the imgui*.cpp files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. +// A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it) +// B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template. +//----------------------------------------------------------------------------- +// You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp +// files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. // Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using. //----------------------------------------------------------------------------- diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index c9146e13..a3c7e72a 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (main code and documentation) // Help: @@ -372,6 +372,18 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2020/09/17 (1.79) - removed ImFont::DisplayOffset in favor of ImFontConfig::GlyphOffset. DisplayOffset was applied after scaling and not very meaningful/useful outside of being needed by the default ProggyClean font. It was also getting in the way of better font scaling, so let's get rid of it now! + - 2020/08/17 (1.78) - obsoleted use of the trailing 'float power=1.0f' parameter for DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(), DragFloatRange2(), DragScalar(), DragScalarN(), SliderFloat(), SliderFloat2(), SliderFloat3(), SliderFloat4(), SliderScalar(), SliderScalarN(), VSliderFloat() and VSliderScalar(). + replaced the 'float power=1.0f' argument with integer-based flags defaulting to 0 (as with all our flags). + worked out a backward-compatibility scheme so hopefully most C++ codebase should not be affected. in short, when calling those functions: + - if you omitted the 'power' parameter (likely!), you are not affected. + - if you set the 'power' parameter to 1.0f (same as previous default value): 1/ your compiler may warn on float>int conversion, 2/ everything else will work. 3/ you can replace the 1.0f value with 0 to fix the warning, and be technically correct. + - if you set the 'power' parameter to >1.0f (to enable non-linear editing): 1/ your compiler may warn on float>int conversion, 2/ code will assert at runtime, 3/ in case asserts are disabled, the code will not crash and enable the _Logarithmic flag. 4/ you can replace the >1.0f value with ImGuiSliderFlags_Logarithmic to fix the warning/assert and get a _similar_ effect as previous uses of power >1.0f. + see https://github.com/ocornut/imgui/issues/3361 for all details. + kept inline redirection functions (will obsolete) apart for: DragFloatRange2(), VSliderFloat(), VSliderScalar(). For those three the 'float power=1.0f' version were removed directly as they were most unlikely ever used. + for shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. + - obsoleted use of v_min > v_max in DragInt, DragFloat, DragScalar to lock edits (introduced in 1.73, was not demoed nor documented very), will be replaced by a more generic ReadOnly feature. You may use the ImGuiSliderFlags_ReadOnly internal flag in the meantime. + - 2020/06/23 (1.77) - removed BeginPopupContextWindow(const char*, int mouse_button, bool also_over_items) in favor of BeginPopupContextWindow(const char*, ImGuiPopupFlags flags) with ImGuiPopupFlags_NoOverItems. - 2020/06/15 (1.77) - renamed OpenPopupOnItemClick() to OpenPopupContextItem(). Kept inline redirection function (will obsolete). - 2020/06/15 (1.77) - removed CalcItemRectClosestPoint() entry point which was made obsolete and asserting in December 2017. - 2020/04/23 (1.77) - removed unnecessary ID (first arg) of ImFontAtlas::AddCustomRectRegular(). @@ -416,7 +428,7 @@ CODE - 2019/04/29 (1.70) - removed GetContentRegionAvailWidth(), use GetContentRegionAvail().x instead. Kept inline redirection function (will obsolete). - 2019/03/04 (1.69) - renamed GetOverlayDrawList() to GetForegroundDrawList(). Kept redirection function (will obsolete). - 2019/02/26 (1.69) - renamed ImGuiColorEditFlags_RGB/ImGuiColorEditFlags_HSV/ImGuiColorEditFlags_HEX to ImGuiColorEditFlags_DisplayRGB/ImGuiColorEditFlags_DisplayHSV/ImGuiColorEditFlags_DisplayHex. Kept redirection enums (will obsolete). - - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with a dummy small value! + - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with an arbitrary small value! - 2019/02/01 (1.68) - removed io.DisplayVisibleMin/DisplayVisibleMax (which were marked obsolete and removed from viewport/docking branch already). - 2019/01/06 (1.67) - renamed io.InputCharacters[], marked internal as was always intended. Please don't access directly, and use AddInputCharacter() instead! - 2019/01/06 (1.67) - renamed ImFontAtlas::GlyphRangesBuilder to ImFontGlyphRangesBuilder. Kept redirection typedef (will obsolete). @@ -600,7 +612,10 @@ CODE FREQUENTLY ASKED QUESTIONS (FAQ) ================================ - Read all answers online: https://www.dearimgui.org/faq, or in docs/FAQ.md (with a Markdown viewer) + Read all answers online: + https://www.dearimgui.org/faq or https://github.com/ocornut/imgui/blob/master/docs/FAQ.md (same url) + Read all answers locally (with a text editor or ideally a Markdown viewer): + docs/FAQ.md Some answers are copied down here to facilitate searching in code. Q&A: Basics @@ -639,130 +654,15 @@ CODE Q: I integrated Dear ImGui in my engine and little squares are showing instead of text.. Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries.. ->> See https://www.dearimgui.org/faq + >> See https://www.dearimgui.org/faq Q&A: Usage ---------- - Q: Why are multiple widgets reacting when I interact with a single one? - Q: How can I have multiple widgets with the same label or with an empty label? - A: A primer on labels and the ID Stack... - - Dear ImGui internally need to uniquely identify UI elements. - Elements that are typically not clickable (such as calls to the Text functions) don't need an ID. - Interactive widgets (such as calls to Button buttons) need a unique ID. - Unique ID are used internally to track active widgets and occasionally associate state to widgets. - Unique ID are implicitly built from the hash of multiple elements that identify the "path" to the UI element. - - - Unique ID are often derived from a string label: - - Button("OK"); // Label = "OK", ID = hash of (..., "OK") - Button("Cancel"); // Label = "Cancel", ID = hash of (..., "Cancel") - - - ID are uniquely scoped within windows, tree nodes, etc. which all pushes to the ID stack. Having - two buttons labeled "OK" in different windows or different tree locations is fine. - We used "..." above to signify whatever was already pushed to the ID stack previously: - - Begin("MyWindow"); - Button("OK"); // Label = "OK", ID = hash of ("MyWindow", "OK") - End(); - Begin("MyOtherWindow"); - Button("OK"); // Label = "OK", ID = hash of ("MyOtherWindow", "OK") - End(); - - - If you have a same ID twice in the same location, you'll have a conflict: - - Button("OK"); - Button("OK"); // ID collision! Interacting with either button will trigger the first one. - - Fear not! this is easy to solve and there are many ways to solve it! - - - Solving ID conflict in a simple/local context: - When passing a label you can optionally specify extra ID information within string itself. - Use "##" to pass a complement to the ID that won't be visible to the end-user. - This helps solving the simple collision cases when you know e.g. at compilation time which items - are going to be created: - - Begin("MyWindow"); - Button("Play"); // Label = "Play", ID = hash of ("MyWindow", "Play") - Button("Play##foo1"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo1") // Different from above - Button("Play##foo2"); // Label = "Play", ID = hash of ("MyWindow", "Play##foo2") // Different from above - End(); - - - If you want to completely hide the label, but still need an ID: - - Checkbox("##On", &b); // Label = "", ID = hash of (..., "##On") // No visible label, just a checkbox! - - - Occasionally/rarely you might want change a label while preserving a constant ID. This allows - you to animate labels. For example you may want to include varying information in a window title bar, - but windows are uniquely identified by their ID. Use "###" to pass a label that isn't part of ID: - - Button("Hello###ID"); // Label = "Hello", ID = hash of (..., "###ID") - Button("World###ID"); // Label = "World", ID = hash of (..., "###ID") // Same as above, even though the label looks different - - sprintf(buf, "My game (%f FPS)###MyGame", fps); - Begin(buf); // Variable title, ID = hash of "MyGame" - - - Solving ID conflict in a more general manner: - Use PushID() / PopID() to create scopes and manipulate the ID stack, as to avoid ID conflicts - within the same window. This is the most convenient way of distinguishing ID when iterating and - creating many UI elements programmatically. - You can push a pointer, a string or an integer value into the ID stack. - Remember that ID are formed from the concatenation of _everything_ pushed into the ID stack. - At each level of the stack we store the seed used for items at this level of the ID stack. - - Begin("Window"); - for (int i = 0; i < 100; i++) - { - PushID(i); // Push i to the id tack - Button("Click"); // Label = "Click", ID = hash of ("Window", i, "Click") - PopID(); - } - for (int i = 0; i < 100; i++) - { - MyObject* obj = Objects[i]; - PushID(obj); - Button("Click"); // Label = "Click", ID = hash of ("Window", obj pointer, "Click") - PopID(); - } - for (int i = 0; i < 100; i++) - { - MyObject* obj = Objects[i]; - PushID(obj->Name); - Button("Click"); // Label = "Click", ID = hash of ("Window", obj->Name, "Click") - PopID(); - } - End(); - - - You can stack multiple prefixes into the ID stack: - - Button("Click"); // Label = "Click", ID = hash of (..., "Click") - PushID("node"); - Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") - PushID(my_ptr); - Button("Click"); // Label = "Click", ID = hash of (..., "node", my_ptr, "Click") - PopID(); - PopID(); - - - Tree nodes implicitly creates a scope for you by calling PushID(). - - Button("Click"); // Label = "Click", ID = hash of (..., "Click") - if (TreeNode("node")) // <-- this function call will do a PushID() for you (unless instructed not to, with a special flag) - { - Button("Click"); // Label = "Click", ID = hash of (..., "node", "Click") - TreePop(); - } - - - When working with trees, ID are used to preserve the open/close state of each tree node. - Depending on your use cases you may want to use strings, indices or pointers as ID. - e.g. when following a single pointer that may change over time, using a static string as ID - will preserve your node open/closed state when the targeted object change. - e.g. when displaying a list of objects, using indices or pointers as ID will preserve the - node open/closed state differently. See what makes more sense in your situation! - + Q: Why is my widget not reacting when I click on it? + Q: How can I have widgets with an empty label? + Q: How can I have multiple widgets with the same label? Q: How can I display an image? What is ImTextureID, how does it works? - >> See https://www.dearimgui.org/faq and https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples - Q: How can I use my own math types instead of ImVec2/ImVec4? Q: How can I interact with standard C++ types (such as std::string and std::vector)? Q: How can I display custom shapes? (using low-level ImDrawList API) @@ -916,7 +816,7 @@ static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock static void SetCurrentWindow(ImGuiWindow* window); static void FindHoveredWindow(); static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags); -static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges); +static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window); static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window); @@ -942,6 +842,7 @@ static void NavUpdate(); static void NavUpdateWindowing(); static void NavUpdateWindowingOverlay(); static void NavUpdateMoveResult(); +static void NavUpdateInitResult(); static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); static void NavEndFrame(); @@ -963,7 +864,7 @@ static void UpdateMouseInputs(); static void UpdateMouseWheel(); static void UpdateTabFocus(); static void UpdateDebugToolItemPicker(); -static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]); +static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); @@ -1036,6 +937,7 @@ ImGuiStyle::ImGuiStyle() ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. + LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. TabMinWidthForUnselectedCloseButton = 0.0f; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. @@ -1045,8 +947,9 @@ ImGuiStyle::ImGuiStyle() DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. - AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU. - AntiAliasedFill = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) + AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. + AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering. + AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. CircleSegmentMaxError = 1.60f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. @@ -1074,6 +977,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) ScrollbarRounding = ImFloor(ScrollbarRounding * scale_factor); GrabMinSize = ImFloor(GrabMinSize * scale_factor); GrabRounding = ImFloor(GrabRounding * scale_factor); + LogSliderDeadzone = ImFloor(LogSliderDeadzone * scale_factor); TabRounding = ImFloor(TabRounding * scale_factor); if (TabMinWidthForUnselectedCloseButton != FLT_MAX) TabMinWidthForUnselectedCloseButton = ImFloor(TabMinWidthForUnselectedCloseButton * scale_factor); @@ -1092,7 +996,7 @@ ImGuiIO::ImGuiIO() ConfigFlags = ImGuiConfigFlags_None; BackendFlags = ImGuiBackendFlags_None; DisplaySize = ImVec2(-1.0f, -1.0f); - DeltaTime = 1.0f/60.0f; + DeltaTime = 1.0f / 60.0f; IniSavingRate = 5.0f; IniFilename = "imgui.ini"; LogFilename = "imgui_log.txt"; @@ -1233,7 +1137,7 @@ static void BezierClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); d2 = (d2 >= 0) ? d2 : -d2; d3 = (d3 >= 0) ? d3 : -d3; - if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy)) + if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) { ImVec2 p_current(x4, y4); ImVec2 p_line = ImLineClosestPoint(p_last, p_current, p); @@ -1247,12 +1151,12 @@ static void BezierClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, } else if (level < 10) { - float x12 = (x1+x2)*0.5f, y12 = (y1+y2)*0.5f; - float x23 = (x2+x3)*0.5f, y23 = (y2+y3)*0.5f; - float x34 = (x3+x4)*0.5f, y34 = (y3+y4)*0.5f; - float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; - float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; - float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; + float x12 = (x1 + x2)*0.5f, y12 = (y1 + y2)*0.5f; + float x23 = (x2 + x3)*0.5f, y23 = (y2 + y3)*0.5f; + float x34 = (x3 + x4)*0.5f, y34 = (y3 + y4)*0.5f; + float x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; + float x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; + float x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); } @@ -1706,7 +1610,7 @@ int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const cha { ImWchar* buf_out = buf; ImWchar* buf_end = buf + buf_size; - while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) + while (buf_out < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c; in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); @@ -1753,7 +1657,7 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) { if (buf_size < 3) return 0; buf[0] = (char)(0xe0 + (c >> 12)); - buf[1] = (char)(0x80 + ((c>> 6) & 0x3f)); + buf[1] = (char)(0x80 + ((c >> 6) & 0x3f)); buf[2] = (char)(0x80 + ((c ) & 0x3f)); return 3; } @@ -1773,8 +1677,8 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) // Not optimal but we very rarely use this function. int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end) { - unsigned int dummy = 0; - return ImTextCharFromUtf8(&dummy, in_text, in_text_end); + unsigned int unused = 0; + return ImTextCharFromUtf8(&unused, in_text, in_text_end); } static inline int ImTextCountUtf8BytesFromChar(unsigned int c) @@ -1790,13 +1694,13 @@ int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWch { char* buf_out = buf; const char* buf_end = buf + buf_size; - while (buf_out < buf_end-1 && (!in_text_end || in_text < in_text_end) && *in_text) + while (buf_out < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c = (unsigned int)(*in_text++); if (c < 0x80) *buf_out++ = (char)c; else - buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end-buf_out-1), c); + buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end - buf_out - 1), c); } *buf_out = 0; return (int)(buf_out - buf); @@ -1832,7 +1736,7 @@ IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b) ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in) { - float s = 1.0f/255.0f; + float s = 1.0f / 255.0f; return ImVec4( ((in >> IM_COL32_R_SHIFT) & 0xFF) * s, ((in >> IM_COL32_G_SHIFT) & 0xFF) * s, @@ -1883,7 +1787,7 @@ void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& return; } - h = ImFmod(h, 1.0f) / (60.0f/360.0f); + h = ImFmod(h, 1.0f) / (60.0f / 360.0f); int i = (int)h; float f = h - (float)i; float p = v * (1.0f - s); @@ -2100,7 +2004,7 @@ void ImGuiTextFilter::ImGuiTextRange::split(char separator, ImVector 0) - SetCursorPosYAndSetupDummyPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor + SetCursorPosYAndSetupForPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor StepNo = 2; } } @@ -2313,7 +2217,7 @@ void ImGuiListClipper::End() return; // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. if (ItemsCount < INT_MAX) - SetCursorPosYAndSetupDummyPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor + SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor ItemsCount = -1; StepNo = 3; } @@ -2347,7 +2251,7 @@ bool ImGuiListClipper::Step() StepNo = 3; return true; } - if (StepNo == 2) // Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3. + if (StepNo == 2) // Step 2: empty step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3. { IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0); StepNo = 3; @@ -2577,6 +2481,7 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) return "Unknown"; } + //----------------------------------------------------------------------------- // [SECTION] RENDER HELPERS // Some of those (internal) functions are currently quite a legacy mess - their signature and behavior will change, @@ -2774,7 +2679,7 @@ void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, const float border_size = g.Style.FrameBorderSize; if (border && border_size > 0.0f) { - window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); } } @@ -2786,7 +2691,7 @@ void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) const float border_size = g.Style.FrameBorderSize; if (border_size > 0.0f) { - window->DrawList->AddRect(p_min+ImVec2(1,1), p_max+ImVec2(1,1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); } } @@ -2809,11 +2714,11 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl { const float THICKNESS = 2.0f; const float DISTANCE = 3.0f + THICKNESS * 0.5f; - display_rect.Expand(ImVec2(DISTANCE,DISTANCE)); + display_rect.Expand(ImVec2(DISTANCE, DISTANCE)); bool fully_visible = window->ClipRect.Contains(display_rect); if (!fully_visible) window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); - window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), display_rect.Max - ImVec2(THICKNESS*0.5f,THICKNESS*0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, ImDrawCornerFlags_All, THICKNESS); + window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), display_rect.Max - ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, ImDrawCornerFlags_All, THICKNESS); if (!fully_visible) window->DrawList->PopClipRect(); } @@ -3036,6 +2941,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) } g.ActiveId = id; g.ActiveIdAllowOverlap = false; + g.ActiveIdNoClearOnFocusLoss = false; g.ActiveIdWindow = window; g.ActiveIdHasBeenEditedThisFrame = false; if (id) @@ -3053,7 +2959,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) void ImGui::ClearActiveID() { - SetActiveID(0, NULL); + SetActiveID(0, NULL); // g.ActiveId = 0; } void ImGui::SetHoveredID(ImGuiID id) @@ -3149,7 +3055,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) if ((window->DC.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; - // Special handling for the dummy item after Begin() which represent the title bar or tab. + // Special handling for calling after Begin() which represent the title bar or tab. // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. if (window->DC.LastItemId == window->MoveId && window->WriteAccessed) return false; @@ -3170,10 +3076,13 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) return false; if (!IsMouseHoveringRect(bb.Min, bb.Max)) return false; - if (g.NavDisableMouseHover || !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) + if (g.NavDisableMouseHover) return false; - if (window->DC.ItemFlags & ImGuiItemFlags_Disabled) + if (!IsWindowContentHoverable(window, ImGuiHoveredFlags_None) || (window->DC.ItemFlags & ImGuiItemFlags_Disabled)) + { + g.HoveredIdDisabled = true; return false; + } // We exceptionally allow this function to be called with id==0 to allow using it for easy high-level // hover test in widgets code. We could also decide to split this function is two. @@ -3206,6 +3115,15 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged return false; } +// This is also inlined in ItemAdd() +// Note: if ImGuiItemStatusFlags_HasDisplayRect is set, user needs to set window->DC.LastItemDisplayRect! +void ImGui::SetLastItemData(ImGuiWindow* window, ImGuiID item_id, ImGuiItemStatusFlags item_flags, const ImRect& item_rect) +{ + window->DC.LastItemId = item_id; + window->DC.LastItemStatusFlags = item_flags; + window->DC.LastItemRect = item_rect; +} + // Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { @@ -3255,11 +3173,21 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) if (wrap_pos_x < 0.0f) return 0.0f; - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; if (wrap_pos_x == 0.0f) + { + // We could decide to setup a default wrapping max point for auto-resizing windows, + // or have auto-wrap (with unspecified wrapping pos) behave as a ContentSize extending function? + //if (window->Hidden && (window->Flags & ImGuiWindowFlags_AlwaysAutoResize)) + // wrap_pos_x = ImMax(window->WorkRect.Min.x + g.FontSize * 10.0f, window->WorkRect.Max.x); + //else wrap_pos_x = window->WorkRect.Max.x; + } else if (wrap_pos_x > 0.0f) + { wrap_pos_x += window->Pos.x - window->Scroll.x; // wrap_pos_x is provided is window local space + } return ImMax(wrap_pos_x - pos.x, 1.0f); } @@ -3388,6 +3316,7 @@ void ImGui::StartMouseMovingWindow(ImGuiWindow* window) FocusWindow(window); SetActiveID(window->MoveId, window); g.NavDisableHighlight = true; + g.ActiveIdNoClearOnFocusLoss = true; g.ActiveIdClickOffset = g.IO.MousePos - window->RootWindow->Pos; bool can_move_window = true; @@ -3458,16 +3387,22 @@ void ImGui::UpdateMouseMovingWindowEndFrame() // Handle the edge case of a popup being closed while clicking in its empty space. // If we try to focus it, FocusWindow() > ClosePopupsOverWindow() will accidentally close any parent popups because they are not linked together any more. ImGuiWindow* root_window = g.HoveredRootWindow; - const bool is_closed_popup = root_window && (root_window->Flags & ImGuiWindowFlags_Popup) && !IsPopupOpenAtAnyLevel(root_window->PopupId); + const bool is_closed_popup = root_window && (root_window->Flags & ImGuiWindowFlags_Popup) && !IsPopupOpen(root_window->PopupId, ImGuiPopupFlags_AnyPopupLevel); if (root_window != NULL && !is_closed_popup) { StartMouseMovingWindow(g.HoveredWindow); + + // Cancel moving if clicked outside of title bar if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(root_window->Flags & ImGuiWindowFlags_NoTitleBar)) if (!root_window->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) g.MovingWindow = NULL; + + // Cancel moving if clicked over an item which was disabled or inhibited by popups (note that we know HoveredId == 0 already) + if (g.HoveredIdDisabled) + g.MovingWindow = NULL; } - else if (root_window != NULL && g.NavWindow != NULL && GetTopMostPopupModal() == NULL) + else if (root_window == NULL && g.NavWindow != NULL && GetTopMostPopupModal() == NULL) { // Clicking on void disable focus FocusWindow(NULL); @@ -3690,17 +3625,17 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // - Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow. // - When moving a window we can skip the search, which also conveniently bypasses the fact that window->WindowRectClipped is lagging as this point of the frame. // - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms. + bool clear_hovered_windows = false; FindHoveredWindow(); - // Modal windows prevents cursor from hovering behind them. + // Modal windows prevents mouse from hovering behind them. ImGuiWindow* modal_window = GetTopMostPopupModal(); - if (modal_window) - if (g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window)) - g.HoveredRootWindow = g.HoveredWindow = NULL; + if (modal_window && g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window)) + clear_hovered_windows = true; // Disabled mouse? if (g.IO.ConfigFlags & ImGuiConfigFlags_NoMouse) - g.HoveredWindow = g.HoveredRootWindow = NULL; + clear_hovered_windows = true; // We track click ownership. When clicked outside of a window the click is owned by the application and won't report hovering nor request capture even while dragging over our windows afterward. int mouse_earliest_button_down = -1; @@ -3720,7 +3655,10 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // FIXME: For patterns of drag and drop across OS windows, we may need to rework/remove this test (first committed 311c0ca9 on 2015/02) const bool mouse_dragging_extern_payload = g.DragDropActive && (g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) != 0; if (!mouse_avail_to_imgui && !mouse_dragging_extern_payload) - g.HoveredWindow = g.HoveredRootWindow = NULL; + clear_hovered_windows = true; + + if (clear_hovered_windows) + g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to Dear ImGui + app) if (g.WantCaptureMouseNextFrame != -1) @@ -3789,6 +3727,8 @@ void ImGui::NewFrame() g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; if (g.Style.AntiAliasedLines) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; + if (g.Style.AntiAliasedLinesUseTex && !(g.Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLinesUseTex; if (g.Style.AntiAliasedFill) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedFill; if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) @@ -3821,6 +3761,7 @@ void ImGui::NewFrame() g.HoveredIdPreviousFrame = g.HoveredId; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; + g.HoveredIdDisabled = false; // Update ActiveId data (clear reference to active widget if the widget isn't alive anymore) if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) @@ -3921,7 +3862,7 @@ void ImGui::NewFrame() // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. // This fallback is particularly important as it avoid ImGui:: calls from crashing. g.WithinFrameScopeWithImplicitWindow = true; - SetNextWindowSize(ImVec2(400,400), ImGuiCond_FirstUseEver); + SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); Begin("Debug##Default"); IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); @@ -4017,6 +3958,11 @@ void ImGui::Shutdown(ImGuiContext* context) SetCurrentContext(backup_context); } + // Notify hooked test engine, if any +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiTestEngineHook_Shutdown(context); +#endif + // Clear everything else for (int i = 0; i < g.Windows.Size; i++) IM_DELETE(g.Windows[i]); @@ -4027,7 +3973,7 @@ void ImGui::Shutdown(ImGuiContext* context) g.CurrentWindowStack.clear(); g.WindowsById.Clear(); g.NavWindow = NULL; - g.HoveredWindow = g.HoveredRootWindow = NULL; + g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; g.MovingWindow = NULL; g.ColorModifiers.clear(); @@ -4366,6 +4312,7 @@ static void FindHoveredWindow() ImGuiContext& g = *GImGui; ImGuiWindow* hovered_window = NULL; + ImGuiWindow* hovered_window_ignoring_moving_window = NULL; if (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs)) hovered_window = g.MovingWindow; @@ -4388,16 +4335,27 @@ static void FindHoveredWindow() if (!bb.Contains(g.IO.MousePos)) continue; - // Those seemingly unnecessary extra tests are because the code here is a little different in viewport/docking branches. + // Support for one rectangular hole in any given window + // FIXME: Consider generalizing hit-testing override (with more generic data, callback, etc.) (#1512) + if (window->HitTestHoleSize.x != 0) + { + ImVec2 hole_pos(window->Pos.x + (float)window->HitTestHoleOffset.x, window->Pos.y + (float)window->HitTestHoleOffset.y); + ImVec2 hole_size((float)window->HitTestHoleSize.x, (float)window->HitTestHoleSize.y); + if (ImRect(hole_pos, hole_pos + hole_size).Contains(g.IO.MousePos)) + continue; + } + if (hovered_window == NULL) hovered_window = window; - if (hovered_window) + if (hovered_window_ignoring_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow)) + hovered_window_ignoring_moving_window = window; + if (hovered_window && hovered_window_ignoring_moving_window) break; } g.HoveredWindow = hovered_window; g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; - + g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window; } // Test if mouse cursor is hovering given rectangle @@ -4526,6 +4484,7 @@ bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) return g.IO.MouseDoubleClicked[button]; } +// Return if a mouse click/drag went past the given threshold. Valid to call during the MouseReleased frame. // [Internal] This doesn't test if the button is pressed bool ImGui::IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold) { @@ -4556,7 +4515,7 @@ ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() { ImGuiContext& g = *GImGui; if (g.BeginPopupStack.Size > 0) - return g.OpenPopupStack[g.BeginPopupStack.Size-1].OpenMousePos; + return g.OpenPopupStack[g.BeginPopupStack.Size - 1].OpenMousePos; return g.IO.MousePos; } @@ -4758,7 +4717,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; - flags |= ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_ChildWindow; + flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_ChildWindow; flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag // Size @@ -4798,7 +4757,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b { FocusWindow(child_window); NavInitWindow(child_window, false); - SetActiveID(id+1, child_window); // Steal ActiveId with a dummy id so that key-press won't activate child item + SetActiveID(id + 1, child_window); // Steal ActiveId with another arbitrary id so that key-press won't activate child item g.ActiveIdSource = ImGuiInputSource_Nav; } return ret; @@ -4848,7 +4807,7 @@ void ImGui::EndChild() // When browsing a window that has no activable items (scroll only) we keep a highlight on the child if (window->DC.NavLayerActiveMask == 0 && window == g.NavWindow) - RenderNavHighlight(ImRect(bb.Min - ImVec2(2,2), bb.Max + ImVec2(2,2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); + RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); } else { @@ -5074,10 +5033,10 @@ struct ImGuiResizeGripDef static const ImGuiResizeGripDef resize_grip_def[4] = { - { ImVec2(1,1), ImVec2(-1,-1), 0, 3 }, // Lower-right - { ImVec2(0,1), ImVec2(+1,-1), 3, 6 }, // Lower-left - { ImVec2(0,0), ImVec2(+1,+1), 6, 9 }, // Upper-left (Unused) - { ImVec2(1,0), ImVec2(-1,+1), 9,12 }, // Upper-right (Unused) + { ImVec2(1, 1), ImVec2(-1, -1), 0, 3 }, // Lower-right + { ImVec2(0, 1), ImVec2(+1, -1), 3, 6 }, // Lower-left + { ImVec2(0, 0), ImVec2(+1, +1), 6, 9 }, // Upper-left (Unused) + { ImVec2(1, 0), ImVec2(-1, +1), 9, 12 }, // Upper-right (Unused) }; struct ImGuiResizeBorderDef @@ -5089,16 +5048,16 @@ struct ImGuiResizeBorderDef static const ImGuiResizeBorderDef resize_border_def[4] = { - { ImVec2(0,+1), ImVec2(0,0), ImVec2(1,0), IM_PI*1.50f }, // Top - { ImVec2(-1,0), ImVec2(1,0), ImVec2(1,1), IM_PI*0.00f }, // Right - { ImVec2(0,-1), ImVec2(1,1), ImVec2(0,1), IM_PI*0.50f }, // Bottom - { ImVec2(+1,0), ImVec2(0,1), ImVec2(0,0), IM_PI*1.00f } // Left + { ImVec2(0, +1), ImVec2(0, 0), ImVec2(1, 0), IM_PI * 1.50f }, // Top + { ImVec2(-1, 0), ImVec2(1, 0), ImVec2(1, 1), IM_PI * 0.00f }, // Right + { ImVec2(0, -1), ImVec2(1, 1), ImVec2(0, 1), IM_PI * 0.50f }, // Bottom + { ImVec2(+1, 0), ImVec2(0, 1), ImVec2(0, 0), IM_PI * 1.00f } // Left }; static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) { ImRect rect = window->Rect(); - if (thickness == 0.0f) rect.Max -= ImVec2(1,1); + if (thickness == 0.0f) rect.Max -= ImVec2(1, 1); if (border_n == 0) { return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); } // Top if (border_n == 1) { return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); } // Right if (border_n == 2) { return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); } // Bottom @@ -5120,7 +5079,7 @@ ImGuiID ImGui::GetWindowResizeID(ImGuiWindow* window, int n) // Handle resize for: Resize Grips, Borders, Gamepad // Return true when using auto-fit (double click on resize grip) -static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4]) +static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect) { ImGuiContext& g = *GImGui; ImGuiWindowFlags flags = window->Flags; @@ -5172,6 +5131,9 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s // Resize from any of the four corners // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(grip.InnerDir * grip_hover_outer_size, grip.InnerDir * -grip_hover_inner_size, grip.CornerPosN); // Corner of the window corresponding to our corner grip + ImVec2 clamp_min = ImVec2(grip.CornerPosN.x == 1.0f ? visibility_rect.Min.x : -FLT_MAX, grip.CornerPosN.y == 1.0f ? visibility_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max = ImVec2(grip.CornerPosN.x == 0.0f ? visibility_rect.Max.x : +FLT_MAX, grip.CornerPosN.y == 0.0f ? visibility_rect.Max.y : +FLT_MAX); + corner_target = ImClamp(corner_target, clamp_min, clamp_max); CalcResizePosSizeFromAnyCorner(window, corner_target, grip.CornerPosN, &pos_target, &size_target); } if (resize_grip_n == 0 || held || hovered) @@ -5197,6 +5159,9 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (border_n == 1) { border_posn = ImVec2(1, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Right if (border_n == 2) { border_posn = ImVec2(0, 1); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Bottom if (border_n == 3) { border_posn = ImVec2(0, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Left + ImVec2 clamp_min = ImVec2(border_n == 1 ? visibility_rect.Min.x : -FLT_MAX, border_n == 2 ? visibility_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max = ImVec2(border_n == 3 ? visibility_rect.Max.x : +FLT_MAX, border_n == 0 ? visibility_rect.Max.y : +FLT_MAX); + border_target = ImClamp(border_target, clamp_min, clamp_max); CalcResizePosSizeFromAnyCorner(window, border_target, border_posn, &pos_target, &size_target); } } @@ -5218,6 +5183,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s { const float NAV_RESIZE_SPEED = 600.0f; nav_resize_delta *= ImFloor(NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); + nav_resize_delta = ImMax(nav_resize_delta, visibility_rect.Min - window->Pos - window->Size); g.NavWindowingToggleLayer = false; g.NavDisableMouseHover = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); @@ -5242,13 +5208,13 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s return ret_auto_fit; } -static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& viewport_rect, const ImVec2& padding) +static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& visibility_rect) { ImGuiContext& g = *GImGui; ImVec2 size_for_clamping = window->Size; if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) size_for_clamping.y = window->TitleBarHeight(); - window->Pos = ImClamp(window->Pos, viewport_rect.Min + padding - size_for_clamping, viewport_rect.Max - padding); + window->Pos = ImClamp(window->Pos, visibility_rect.Min - size_for_clamping, visibility_rect.Max); } static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) @@ -5264,8 +5230,8 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) { const ImGuiResizeBorderDef& def = resize_border_def[border_held]; ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI*0.25f, def.OuterAngle); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI*0.25f); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), false, ImMax(2.0f, border_size)); // Thicker than usual } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) @@ -5284,6 +5250,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar ImGuiWindowFlags flags = window->Flags; // Ensure that ScrollBar doesn't read last frame's SkipItems + IM_ASSERT(window->BeginCount == 0); window->SkipItems = false; // Draw window + handle manual resize @@ -5604,7 +5571,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) window->Active = true; window->HasCloseButton = (p_open != NULL); - window->ClipRect = ImVec4(-FLT_MAX,-FLT_MAX,+FLT_MAX,+FLT_MAX); + window->ClipRect = ImVec4(-FLT_MAX, -FLT_MAX, +FLT_MAX, +FLT_MAX); window->IDStack.resize(1); window->DrawList->_ResetForNewFrame(); @@ -5666,6 +5633,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f) window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f); + // Lock menu offset so size calculation can use it as menu-bar windows need a minimum size. + window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); + window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; + // Collapse window by double-clicking on title bar // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse)) @@ -5738,7 +5709,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window_just_activated_by_user) { window->AutoPosLastDirection = ImGuiDir_None; - if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api) + if ((flags & ImGuiWindowFlags_Popup) != 0 && !(flags & ImGuiWindowFlags_Modal) && !window_pos_set_by_api) // FIXME: BeginPopup() could use SetNextWindowPos() window->Pos = g.BeginPopupStack.back().OpenPopupPos; } @@ -5754,7 +5725,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const bool window_pos_with_pivot = (window->SetWindowPosVal.x != FLT_MAX && window->HiddenFramesCannotSkipItems == 0); if (window_pos_with_pivot) - SetWindowPos(window, window->SetWindowPosVal - window->SizeFull * window->SetWindowPosPivot, 0); // Position given a pivot (e.g. for centering) + SetWindowPos(window, window->SetWindowPosVal - window->Size * window->SetWindowPosPivot, 0); // Position given a pivot (e.g. for centering) else if ((flags & ImGuiWindowFlags_ChildMenu) != 0) window->Pos = FindBestWindowPosForPopup(window); else if ((flags & ImGuiWindowFlags_Popup) != 0 && !window_pos_set_by_api && window_just_appearing_after_hidden_for_resize) @@ -5762,17 +5733,17 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) else if ((flags & ImGuiWindowFlags_Tooltip) != 0 && !window_pos_set_by_api && !window_is_child_tooltip) window->Pos = FindBestWindowPosForPopup(window); + // Calculate the range of allowed position for that window (to be movable and visible past safe area padding) + // When clamping to stay visible, we will enforce that window->Pos stays inside of visibility_rect. + ImRect viewport_rect(GetViewportRect()); + ImVec2 visibility_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); + ImRect visibility_rect(viewport_rect.Min + visibility_padding, viewport_rect.Max - visibility_padding); + // Clamp position/size so window stays visible within its viewport or monitor // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. - ImRect viewport_rect(GetViewportRect()); if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) - { - ImVec2 clamp_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); - if (viewport_rect.GetWidth() > 0 && viewport_rect.GetHeight() > 0.0f) - { - ClampWindowRect(window, viewport_rect, clamp_padding); - } - } + if (viewport_rect.GetWidth() > 0.0f && viewport_rect.GetHeight() > 0.0f) + ClampWindowRect(window, visibility_rect); window->Pos = ImFloor(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) @@ -5799,7 +5770,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. const float resize_grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); if (!window->Collapsed) - if (UpdateWindowManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0])) + if (UpdateWindowManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect)) use_current_size_for_scrollbar_x = use_current_size_for_scrollbar_y = true; window->ResizeBorderHeld = (signed char)border_held; @@ -5878,7 +5849,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->ScrollMax.y = ImMax(0.0f, window->ContentSize.y + window->WindowPadding.y * 2.0f - window->InnerRect.GetHeight()); // Apply scrolling - window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window, true); + window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window); window->ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); // DRAWING @@ -5957,6 +5928,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->WorkRect.Min.y = ImFloor(window->InnerRect.Min.y - window->Scroll.y + ImMax(window->WindowPadding.y, window->WindowBorderSize)); window->WorkRect.Max.x = window->WorkRect.Min.x + work_rect_size_x; window->WorkRect.Max.y = window->WorkRect.Min.y + work_rect_size_y; + window->ParentWorkRect = window->WorkRect; // [LEGACY] Content Region // FIXME-OBSOLETE: window->ContentRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. @@ -5988,8 +5960,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); window->DC.MenuBarAppending = false; - window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); - window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; window->DC.MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); window->DC.TreeDepth = 0; window->DC.TreeJumpToParentOnPopMask = 0x00; @@ -6026,6 +5996,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (!(flags & ImGuiWindowFlags_NoTitleBar)) RenderWindowTitleBarContents(window, title_bar_rect, name, p_open); + // Clear hit test shape every frame + window->HitTestHoleSize.x = window->HitTestHoleSize.y = 0; + // Pressing CTRL+C while holding on a window copy its content to the clipboard // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope. // Maybe we can support CTRL+C on every element? @@ -6037,9 +6010,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). // This is useful to allow creating context menus on title bar only, etc. - window->DC.LastItemId = window->MoveId; - window->DC.LastItemStatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0; - window->DC.LastItemRect = title_bar_rect; + SetLastItemData(window, window->MoveId, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect); + #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) IMGUI_TEST_ENGINE_ITEM_ADD(window->DC.LastItemRect, window->DC.LastItemId); @@ -6060,37 +6032,41 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->BeginCount++; g.NextWindowData.ClearFlags(); - if (flags & ImGuiWindowFlags_ChildWindow) + // Update visibility + if (first_begin_of_the_frame) { - // Child window can be out of sight and have "negative" clip windows. - // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). - IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); - if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) - if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + if (flags & ImGuiWindowFlags_ChildWindow) + { + // Child window can be out of sight and have "negative" clip windows. + // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). + IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); + if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + window->HiddenFramesCanSkipItems = 1; + + // Hide along with parent or if parent is collapsed + if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) window->HiddenFramesCanSkipItems = 1; + if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCannotSkipItems > 0)) + window->HiddenFramesCannotSkipItems = 1; + } - // Hide along with parent or if parent is collapsed - if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) + // Don't render if style alpha is 0.0 at the time of Begin(). This is arbitrary and inconsistent but has been there for a long while (may remove at some point) + if (style.Alpha <= 0.0f) window->HiddenFramesCanSkipItems = 1; - if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCannotSkipItems > 0)) - window->HiddenFramesCannotSkipItems = 1; - } - // Don't render if style alpha is 0.0 at the time of Begin(). This is arbitrary and inconsistent but has been there for a long while (may remove at some point) - if (style.Alpha <= 0.0f) - window->HiddenFramesCanSkipItems = 1; + // Update the Hidden flag + window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); - // Update the Hidden flag - window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); - - // Update the SkipItems flag, used to early out of all items functions (no layout required) - bool skip_items = false; - if (window->Collapsed || !window->Active || window->Hidden) - if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0) - skip_items = true; - window->SkipItems = skip_items; + // Update the SkipItems flag, used to early out of all items functions (no layout required) + bool skip_items = false; + if (window->Collapsed || !window->Active || window->Hidden) + if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0) + skip_items = true; + window->SkipItems = skip_items; + } - return !skip_items; + return !window->SkipItems; } void ImGui::End() @@ -6145,7 +6121,7 @@ void ImGui::BringWindowToDisplayFront(ImGuiWindow* window) { ImGuiContext& g = *GImGui; ImGuiWindow* current_front_window = g.Windows.back(); - if (current_front_window == window || current_front_window->RootWindow == window) + if (current_front_window == window || current_front_window->RootWindow == window) // Cheap early out (could be better) return; for (int i = g.Windows.Size - 2; i >= 0; i--) // We can ignore the top-most window if (g.Windows[i] == window) @@ -6196,9 +6172,12 @@ void ImGui::FocusWindow(ImGuiWindow* window) ImGuiWindow* focus_front_window = window ? window->RootWindow : NULL; // NB: In docking branch this is window->RootWindowDockStop ImGuiWindow* display_front_window = window ? window->RootWindow : NULL; - // Steal focus on active widgets + // Steal active widgets. Some of the cases it triggers includes: + // - Focus a window while an InputText in another window is active, if focus happens before the old InputText can run. + // - When using Nav to activate menu items (due to timing of activating on press->new window appears->losing ActiveId) if (g.ActiveId != 0 && g.ActiveIdWindow && g.ActiveIdWindow->RootWindow != focus_front_window) - ClearActiveID(); + if (!g.ActiveIdNoClearOnFocusLoss) + ClearActiveID(); // Passing NULL allow to disable keyboard focus if (!window) @@ -6247,6 +6226,7 @@ void ImGui::SetCurrentFont(ImFont* font) ImFontAtlas* atlas = g.Font->ContainerAtlas; g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel; + g.DrawListSharedData.TexUvLines = atlas->TexUvLines; g.DrawListSharedData.Font = g.Font; g.DrawListSharedData.FontSize = g.FontSize; } @@ -6514,6 +6494,13 @@ void ImGui::SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond co window->Collapsed = collapsed; } +void ImGui::SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size) +{ + IM_ASSERT(window->HitTestHoleSize.x == 0); // We don't support multiple holes/hit test filters + window->HitTestHoleSize = ImVec2ih(size); + window->HitTestHoleOffset = ImVec2ih(pos - window->Pos); +} + void ImGui::SetWindowCollapsed(bool collapsed, ImGuiCond cond) { SetWindowCollapsed(GImGui->CurrentWindow, collapsed, cond); @@ -7008,6 +6995,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) #endif } + // Equivalent to calling SetLastItemData() window->DC.LastItemId = id; window->DC.LastItemRect = bb; window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; @@ -7156,10 +7144,10 @@ void ImGui::PushMultiItemsWidths(int components, float w_full) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiStyle& style = g.Style; - const float w_item_one = ImMax(1.0f, IM_FLOOR((w_full - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); - const float w_item_last = ImMax(1.0f, IM_FLOOR(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + const float w_item_one = ImMax(1.0f, IM_FLOOR((w_full - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components)); + const float w_item_last = ImMax(1.0f, IM_FLOOR(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components - 1))); window->DC.ItemWidthStack.push_back(w_item_last); - for (int i = 0; i < components-1; i++) + for (int i = 0; i < components - 1; i++) window->DC.ItemWidthStack.push_back(w_item_one); window->DC.ItemWidth = window->DC.ItemWidthStack.back(); g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; @@ -7291,10 +7279,11 @@ float ImGui::GetWindowContentRegionWidth() } // Lock horizontal starting position + capture group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) +// Groups are currently a mishmash of functionalities which should perhaps be clarified and separated. void ImGui::BeginGroup() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); + ImGuiWindow* window = g.CurrentWindow; window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1); ImGuiGroupData& group_data = window->DC.GroupStack.back(); @@ -7319,8 +7308,8 @@ void ImGui::BeginGroup() void ImGui::EndGroup() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - IM_ASSERT(!window->DC.GroupStack.empty()); // Mismatched BeginGroup()/EndGroup() calls + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(window->DC.GroupStack.Size > 0); // Mismatched BeginGroup()/EndGroup() calls ImGuiGroupData& group_data = window->DC.GroupStack.back(); @@ -7348,9 +7337,9 @@ void ImGui::EndGroup() // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group. // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets. // Also if you grep for LastItemId you'll notice it is only used in that context. - // (The tests not symmetrical because ActiveIdIsAlive is an ID itself, in order to be able to handle ActiveId being overwritten during the frame.) + // (The two tests not the same because ActiveIdIsAlive is an ID itself, in order to be able to handle ActiveId being overwritten during the frame.) const bool group_contains_curr_active_id = (group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId; - const bool group_contains_prev_active_id = !group_data.BackupActiveIdPreviousFrameIsAlive && g.ActiveIdPreviousFrameIsAlive; + const bool group_contains_prev_active_id = (group_data.BackupActiveIdPreviousFrameIsAlive == false) && (g.ActiveIdPreviousFrameIsAlive == true); if (group_contains_curr_active_id) window->DC.LastItemId = g.ActiveId; else if (group_contains_prev_active_id) @@ -7375,31 +7364,42 @@ void ImGui::EndGroup() // [SECTION] SCROLLING //----------------------------------------------------------------------------- -static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window, bool snap_on_edges) +// Helper to snap on edges when aiming at an item very close to the edge, +// So the difference between WindowPadding and ItemSpacing will be in the visible area after scrolling. +// When we refactor the scrolling API this may be configurable with a flag? +// Note that the effect for this won't be visible on X axis with default Style settings as WindowPadding.x == ItemSpacing.x by default. +static float CalcScrollEdgeSnap(float target, float snap_min, float snap_max, float snap_threshold, float center_ratio) +{ + if (target <= snap_min + snap_threshold) + return ImLerp(snap_min, target, center_ratio); + if (target >= snap_max - snap_threshold) + return ImLerp(target, snap_max, center_ratio); + return target; +} + +static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window) { - ImGuiContext& g = *GImGui; ImVec2 scroll = window->Scroll; if (window->ScrollTarget.x < FLT_MAX) { - float cr_x = window->ScrollTargetCenterRatio.x; - float target_x = window->ScrollTarget.x; - if (snap_on_edges && cr_x <= 0.0f && target_x <= window->WindowPadding.x) - target_x = 0.0f; - else if (snap_on_edges && cr_x >= 1.0f && target_x >= window->ContentSize.x + window->WindowPadding.x + g.Style.ItemSpacing.x) - target_x = window->ContentSize.x + window->WindowPadding.x * 2.0f; - scroll.x = target_x - cr_x * (window->SizeFull.x - window->ScrollbarSizes.x); + float center_x_ratio = window->ScrollTargetCenterRatio.x; + float scroll_target_x = window->ScrollTarget.x; + float snap_x_min = 0.0f; + float snap_x_max = window->ScrollMax.x + window->Size.x; + if (window->ScrollTargetEdgeSnapDist.x > 0.0f) + scroll_target_x = CalcScrollEdgeSnap(scroll_target_x, snap_x_min, snap_x_max, window->ScrollTargetEdgeSnapDist.x, center_x_ratio); + scroll.x = scroll_target_x - center_x_ratio * (window->SizeFull.x - window->ScrollbarSizes.x); } if (window->ScrollTarget.y < FLT_MAX) { - // 'snap_on_edges' allows for a discontinuity at the edge of scrolling limits to take account of WindowPadding so that scrolling to make the last item visible scroll far enough to see the padding. float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); - float cr_y = window->ScrollTargetCenterRatio.y; - float target_y = window->ScrollTarget.y; - if (snap_on_edges && cr_y <= 0.0f && target_y <= window->WindowPadding.y) - target_y = 0.0f; - if (snap_on_edges && cr_y >= 1.0f && target_y >= window->ContentSize.y + window->WindowPadding.y + g.Style.ItemSpacing.y) - target_y = window->ContentSize.y + window->WindowPadding.y * 2.0f; - scroll.y = target_y - cr_y * (window->SizeFull.y - window->ScrollbarSizes.y - decoration_up_height); + float center_y_ratio = window->ScrollTargetCenterRatio.y; + float scroll_target_y = window->ScrollTarget.y; + float snap_y_min = 0.0f; + float snap_y_max = window->ScrollMax.y + window->Size.y - decoration_up_height; + if (window->ScrollTargetEdgeSnapDist.y > 0.0f) + scroll_target_y = CalcScrollEdgeSnap(scroll_target_y, snap_y_min, snap_y_max, window->ScrollTargetEdgeSnapDist.y, center_y_ratio); + scroll.y = scroll_target_y - center_y_ratio * (window->SizeFull.y - window->ScrollbarSizes.y - decoration_up_height); } scroll.x = IM_FLOOR(ImMax(scroll.x, 0.0f)); scroll.y = IM_FLOOR(ImMax(scroll.y, 0.0f)); @@ -7422,7 +7422,7 @@ ImVec2 ImGui::ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_ if (!window_rect.Contains(item_rect)) { if (window->ScrollbarX && item_rect.Min.x < window_rect.Min.x) - SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x + g.Style.ItemSpacing.x, 0.0f); + SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x - g.Style.ItemSpacing.x, 0.0f); else if (window->ScrollbarX && item_rect.Max.x >= window_rect.Max.x) SetScrollFromPosX(window, item_rect.Max.x - window->Pos.x + g.Style.ItemSpacing.x, 1.0f); if (item_rect.Min.y < window_rect.Min.y) @@ -7430,7 +7430,7 @@ ImVec2 ImGui::ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_ else if (item_rect.Max.y >= window_rect.Max.y) SetScrollFromPosY(window, item_rect.Max.y - window->Pos.y + g.Style.ItemSpacing.y, 1.0f); - ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window, false); + ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); delta_scroll = next_scroll - window->Scroll; } @@ -7465,49 +7465,57 @@ float ImGui::GetScrollMaxY() return window->ScrollMax.y; } -void ImGui::SetScrollX(float scroll_x) +void ImGui::SetScrollX(ImGuiWindow* window, float scroll_x) { - ImGuiWindow* window = GetCurrentWindow(); window->ScrollTarget.x = scroll_x; window->ScrollTargetCenterRatio.x = 0.0f; + window->ScrollTargetEdgeSnapDist.x = 0.0f; } -void ImGui::SetScrollY(float scroll_y) +void ImGui::SetScrollY(ImGuiWindow* window, float scroll_y) { - ImGuiWindow* window = GetCurrentWindow(); window->ScrollTarget.y = scroll_y; window->ScrollTargetCenterRatio.y = 0.0f; + window->ScrollTargetEdgeSnapDist.y = 0.0f; } -void ImGui::SetScrollX(ImGuiWindow* window, float new_scroll_x) +void ImGui::SetScrollX(float scroll_x) { - window->ScrollTarget.x = new_scroll_x; - window->ScrollTargetCenterRatio.x = 0.0f; + ImGuiContext& g = *GImGui; + SetScrollX(g.CurrentWindow, scroll_x); } -void ImGui::SetScrollY(ImGuiWindow* window, float new_scroll_y) +void ImGui::SetScrollY(float scroll_y) { - window->ScrollTarget.y = new_scroll_y; - window->ScrollTargetCenterRatio.y = 0.0f; + ImGuiContext& g = *GImGui; + SetScrollY(g.CurrentWindow, scroll_y); } - +// Note that a local position will vary depending on initial scroll value, +// This is a little bit confusing so bear with us: +// - local_pos = (absolution_pos - window->Pos) +// - So local_x/local_y are 0.0f for a position at the upper-left corner of a window, +// and generally local_x/local_y are >(padding+decoration) && <(size-padding-decoration) when in the visible area. +// - They mostly exists because of legacy API. +// Following the rules above, when trying to work with scrolling code, consider that: +// - SetScrollFromPosY(0.0f) == SetScrollY(0.0f + scroll.y) == has no effect! +// - SetScrollFromPosY(-scroll.y) == SetScrollY(-scroll.y + scroll.y) == SetScrollY(0.0f) == reset scroll. Of course writing SetScrollY(0.0f) directly then makes more sense +// We store a target position so centering and clamping can occur on the next frame when we are guaranteed to have a known window size void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio) { - // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size IM_ASSERT(center_x_ratio >= 0.0f && center_x_ratio <= 1.0f); - window->ScrollTarget.x = IM_FLOOR(local_x + window->Scroll.x); + window->ScrollTarget.x = IM_FLOOR(local_x + window->Scroll.x); // Convert local position to scroll offset window->ScrollTargetCenterRatio.x = center_x_ratio; + window->ScrollTargetEdgeSnapDist.x = 0.0f; } void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio) { - // We store a target position so centering can occur on the next frame when we are guaranteed to have a known window size IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); - const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); - local_y -= decoration_up_height; - window->ScrollTarget.y = IM_FLOOR(local_y + window->Scroll.y); + local_y -= window->TitleBarHeight() + window->MenuBarHeight(); // FIXME: Would be nice to have a more standardized access to our scrollable/client rect + window->ScrollTarget.y = IM_FLOOR(local_y + window->Scroll.y); // Convert local position to scroll offset window->ScrollTargetCenterRatio.y = center_y_ratio; + window->ScrollTargetEdgeSnapDist.y = 0.0f; } void ImGui::SetScrollFromPosX(float local_x, float center_x_ratio) @@ -7527,10 +7535,12 @@ void ImGui::SetScrollHereX(float center_x_ratio) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - float target_x = window->DC.LastItemRect.Min.x - window->Pos.x; // Left of last item, in window space - float last_item_width = window->DC.LastItemRect.GetWidth(); - target_x += (last_item_width * center_x_ratio) + (g.Style.ItemSpacing.x * (center_x_ratio - 0.5f) * 2.0f); // Precisely aim before, in the middle or after the last item. - SetScrollFromPosX(target_x, center_x_ratio); + float spacing_x = g.Style.ItemSpacing.x; + float target_pos_x = ImLerp(window->DC.LastItemRect.Min.x - spacing_x, window->DC.LastItemRect.Max.x + spacing_x, center_x_ratio); + SetScrollFromPosX(window, target_pos_x - window->Pos.x, center_x_ratio); // Convert from absolute to local pos + + // Tweak: snap on edges when aiming at an item very close to the edge + window->ScrollTargetEdgeSnapDist.x = ImMax(0.0f, window->WindowPadding.x - spacing_x); } // center_y_ratio: 0.0f top of last item, 0.5f vertical center of last item, 1.0f bottom of last item. @@ -7538,9 +7548,12 @@ void ImGui::SetScrollHereY(float center_y_ratio) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - float target_y = window->DC.CursorPosPrevLine.y - window->Pos.y; // Top of last item, in window space - target_y += (window->DC.PrevLineSize.y * center_y_ratio) + (g.Style.ItemSpacing.y * (center_y_ratio - 0.5f) * 2.0f); // Precisely aim above, in the middle or below the last line. - SetScrollFromPosY(target_y, center_y_ratio); + float spacing_y = g.Style.ItemSpacing.y; + float target_pos_y = ImLerp(window->DC.CursorPosPrevLine.y - spacing_y, window->DC.CursorPosPrevLine.y + window->DC.PrevLineSize.y + spacing_y, center_y_ratio); + SetScrollFromPosY(window, target_pos_y - window->Pos.y, center_y_ratio); // Convert from absolute to local pos + + // Tweak: snap on edges when aiming at an item very close to the edge + window->ScrollTargetEdgeSnapDist.y = ImMax(0.0f, window->WindowPadding.y - spacing_y); } //----------------------------------------------------------------------------- @@ -7580,7 +7593,7 @@ void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags toolt window->HiddenFramesCanSkipItems = 1; ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } - ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip|ImGuiWindowFlags_NoInputs|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize; + ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; Begin(window_name, NULL, flags | extra_flags); } @@ -7609,62 +7622,77 @@ void ImGui::SetTooltip(const char* fmt, ...) // [SECTION] POPUPS //----------------------------------------------------------------------------- -// Return true if the popup is open at the current BeginPopup() level of the popup stack -bool ImGui::IsPopupOpen(ImGuiID id) -{ - ImGuiContext& g = *GImGui; - return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id; -} - -// Return true if the popup is open at the current BeginPopup() level of the popup stack -bool ImGui::IsPopupOpen(const char* str_id) -{ - ImGuiContext& g = *GImGui; - return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == g.CurrentWindow->GetID(str_id); -} - -bool ImGui::IsPopupOpenAtAnyLevel(ImGuiID id) +// Supported flags: ImGuiPopupFlags_AnyPopupId, ImGuiPopupFlags_AnyPopupLevel +bool ImGui::IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; - for (int n = 0; n < g.OpenPopupStack.Size; n++) - if (g.OpenPopupStack[n].PopupId == id) - return true; - return false; + if (popup_flags & ImGuiPopupFlags_AnyPopupId) + { + // Return true if any popup is open at the current BeginPopup() level of the popup stack + // This may be used to e.g. test for another popups already opened to handle popups priorities at the same level. + IM_ASSERT(id == 0); + if (popup_flags & ImGuiPopupFlags_AnyPopupLevel) + return g.OpenPopupStack.Size > 0; + else + return g.OpenPopupStack.Size > g.BeginPopupStack.Size; + } + else + { + if (popup_flags & ImGuiPopupFlags_AnyPopupLevel) + { + // Return true if the popup is open anywhere in the popup stack + for (int n = 0; n < g.OpenPopupStack.Size; n++) + if (g.OpenPopupStack[n].PopupId == id) + return true; + return false; + } + else + { + // Return true if the popup is open at the current BeginPopup() level of the popup stack (this is the most-common query) + return g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].PopupId == id; + } + } } -// Return true if any popup is open at the current BeginPopup() level of the popup stack -// This may be used to e.g. test for another popups already opened in the same frame to handle popups priorities at the same level. -bool ImGui::IsAnyPopupOpen() +bool ImGui::IsPopupOpen(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; - return g.OpenPopupStack.Size > g.BeginPopupStack.Size; + ImGuiID id = (popup_flags & ImGuiPopupFlags_AnyPopupId) ? 0 : g.CurrentWindow->GetID(str_id); + if ((popup_flags & ImGuiPopupFlags_AnyPopupLevel) && id != 0) + IM_ASSERT(0 && "Cannot use IsPopupOpen() with a string id and ImGuiPopupFlags_AnyPopupLevel."); // But non-string version is legal and used internally + return IsPopupOpen(id, popup_flags); } ImGuiWindow* ImGui::GetTopMostPopupModal() { ImGuiContext& g = *GImGui; - for (int n = g.OpenPopupStack.Size-1; n >= 0; n--) + for (int n = g.OpenPopupStack.Size - 1; n >= 0; n--) if (ImGuiWindow* popup = g.OpenPopupStack.Data[n].Window) if (popup->Flags & ImGuiWindowFlags_Modal) return popup; return NULL; } -void ImGui::OpenPopup(const char* str_id) +void ImGui::OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; - OpenPopupEx(g.CurrentWindow->GetID(str_id)); + OpenPopupEx(g.CurrentWindow->GetID(str_id), popup_flags); } // Mark popup as open (toggle toward open state). // Popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. // Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). // One open popup per level of the popup hierarchy (NB: when assigning we reset the Window member of ImGuiPopupRef to NULL) -void ImGui::OpenPopupEx(ImGuiID id) +void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; - int current_stack_size = g.BeginPopupStack.Size; + const int current_stack_size = g.BeginPopupStack.Size; + + if (popup_flags & ImGuiPopupFlags_NoOpenOverExistingPopup) + if (IsPopupOpen(0u, ImGuiPopupFlags_AnyPopupId)) + return; + ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. popup_ref.PopupId = id; popup_ref.Window = NULL; @@ -7674,7 +7702,7 @@ void ImGui::OpenPopupEx(ImGuiID id) popup_ref.OpenPopupPos = NavCalcPreferredRefPos(); popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos; - //IMGUI_DEBUG_LOG("OpenPopupEx(0x%08X)\n", g.FrameCount, id); + IMGUI_DEBUG_LOG_POPUP("OpenPopupEx(0x%08X)\n", id); if (g.OpenPopupStack.Size < current_stack_size + 1) { g.OpenPopupStack.push_back(popup_ref); @@ -7702,13 +7730,14 @@ void ImGui::OpenPopupEx(ImGuiID id) } } +// When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it. +// This function closes any popups that are over 'ref_window'. void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; if (g.OpenPopupStack.Size == 0) return; - // When popups are stacked, clicking on a lower level popups puts focus back to it and close popups above it. // Don't close our own child popup windows. int popup_count_to_keep = 0; if (ref_window) @@ -7723,19 +7752,26 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow) continue; - // Trim the stack when popups are not direct descendant of the reference window (the reference window is often the NavWindow) - bool popup_or_descendent_is_ref_window = false; - for (int m = popup_count_to_keep; m < g.OpenPopupStack.Size && !popup_or_descendent_is_ref_window; m++) - if (ImGuiWindow* popup_window = g.OpenPopupStack[m].Window) + // Trim the stack unless the popup is a direct parent of the reference window (the reference window is often the NavWindow) + // - With this stack of window, clicking/focusing Popup1 will close Popup2 and Popup3: + // Window -> Popup1 -> Popup2 -> Popup3 + // - Each popups may contain child windows, which is why we compare ->RootWindow! + // Window -> Popup1 -> Popup1_Child -> Popup2 -> Popup2_Child + bool ref_window_is_descendent_of_popup = false; + for (int n = popup_count_to_keep; n < g.OpenPopupStack.Size; n++) + if (ImGuiWindow* popup_window = g.OpenPopupStack[n].Window) if (popup_window->RootWindow == ref_window->RootWindow) - popup_or_descendent_is_ref_window = true; - if (!popup_or_descendent_is_ref_window) + { + ref_window_is_descendent_of_popup = true; + break; + } + if (!ref_window_is_descendent_of_popup) break; } } if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below { - //IMGUI_DEBUG_LOG("ClosePopupsOverWindow(%s) -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep); + IMGUI_DEBUG_LOG_POPUP("ClosePopupsOverWindow(\"%s\") -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep); ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup); } } @@ -7743,6 +7779,7 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; + IMGUI_DEBUG_LOG_POPUP("ClosePopupToLevel(%d), restore_focus_to_window_under_popup=%d\n", remaining, restore_focus_to_window_under_popup); IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); // Trim open popup stack @@ -7787,7 +7824,7 @@ void ImGui::CloseCurrentPopup() break; popup_idx--; } - //IMGUI_DEBUG_LOG("CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); + IMGUI_DEBUG_LOG_POPUP("CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); ClosePopupToLevel(popup_idx, true); // A common pattern is to close a popup when selecting a menu item/selectable that will open another window. @@ -7797,10 +7834,11 @@ void ImGui::CloseCurrentPopup() window->DC.NavHideHighlightOneFrame = true; } +// Attention! BeginPopup() adds default flags which BeginPopupEx()! bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; - if (!IsPopupOpen(id)) + if (!IsPopupOpen(id, ImGuiPopupFlags_None)) { g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values return false; @@ -7839,18 +7877,19 @@ bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags fla ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiID id = window->GetID(name); - if (!IsPopupOpen(id)) + if (!IsPopupOpen(id, ImGuiPopupFlags_None)) { g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values return false; } - // Center modal windows by default + // Center modal windows by default for increased visibility + // (this won't really last as settings will kick in, and is mostly for backward compatibility. user may do the same themselves) // FIXME: Should test for (PosCond & window->SetWindowPosAllowFlags) with the upcoming window. if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) == 0) - SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); - flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings; + flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse; const bool is_open = Begin(name, p_open, flags); if (!is_open || (p_open && !*p_open)) // NB: is_open can be 'false' when the popup is completely clipped (e.g. zero size display) { @@ -7881,14 +7920,16 @@ void ImGui::EndPopup() g.WithinEndChild = false; } -bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiMouseButton mouse_button) +// Open a popup if mouse button is released over the item +bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; + int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) { ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) - OpenPopupEx(id); + OpenPopupEx(id, popup_flags); return true; } return false; @@ -7899,40 +7940,43 @@ bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiMouseButton mouse_butt // - You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). // - This is essentially the same as calling OpenPopupContextItem() + BeginPopup() but written to avoid // computing the ID twice because BeginPopupContextXXX functions are called very frequently. -bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiMouseButton mouse_button) +bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; if (window->SkipItems) return false; ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) + int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) - OpenPopupEx(id); - return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); + OpenPopupEx(id, popup_flags); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); } -bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mouse_button, bool also_over_items) +bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; if (!str_id) str_id = "window_context"; ImGuiID id = window->GetID(str_id); + int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) - if (also_over_items || !IsAnyItemHovered()) - OpenPopupEx(id); - return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); + if (!(popup_flags & ImGuiPopupFlags_NoOpenOverItems) || !IsAnyItemHovered()) + OpenPopupEx(id, popup_flags); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); } -bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiMouseButton mouse_button) +bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; if (!str_id) str_id = "void_context"; ImGuiID id = window->GetID(str_id); + int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && !IsWindowHovered(ImGuiHoveredFlags_AnyWindow)) if (GetTopMostPopupModal() == NULL) - OpenPopupEx(id); - return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoSavedSettings); + OpenPopupEx(id, popup_flags); + return BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); } // r_avoid = the rectangle to avoid (e.g. for tooltip it is a rectangle around the mouse cursor which we want to avoid. for popups it's a small point around the cursor.) @@ -7964,26 +8008,47 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s } } - // Default popup policy - const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left }; - for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) + // Tooltip and Default popup policy + // (Always first try the direction we used on the last frame, if any) + if (policy == ImGuiPopupPositionPolicy_Tooltip || policy == ImGuiPopupPositionPolicy_Default) { - const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; - if (n != -1 && dir == *last_dir) // Already tried this direction? - continue; - float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x); - float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y); - if (avail_w < size.x || avail_h < size.y) - continue; - ImVec2 pos; - pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x; - pos.y = (dir == ImGuiDir_Up) ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down) ? r_avoid.Max.y : base_pos_clamped.y; - *last_dir = dir; - return pos; + const ImGuiDir dir_prefered_order[ImGuiDir_COUNT] = { ImGuiDir_Right, ImGuiDir_Down, ImGuiDir_Up, ImGuiDir_Left }; + for (int n = (*last_dir != ImGuiDir_None) ? -1 : 0; n < ImGuiDir_COUNT; n++) + { + const ImGuiDir dir = (n == -1) ? *last_dir : dir_prefered_order[n]; + if (n != -1 && dir == *last_dir) // Already tried this direction? + continue; + + const float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x); + const float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y); + + // If there not enough room on one axis, there's no point in positioning on a side on this axis (e.g. when not enough width, use a top/bottom position to maximize available width) + if (avail_w < size.x && (dir == ImGuiDir_Left || dir == ImGuiDir_Right)) + continue; + if (avail_h < size.y && (dir == ImGuiDir_Up || dir == ImGuiDir_Down)) + continue; + + ImVec2 pos; + pos.x = (dir == ImGuiDir_Left) ? r_avoid.Min.x - size.x : (dir == ImGuiDir_Right) ? r_avoid.Max.x : base_pos_clamped.x; + pos.y = (dir == ImGuiDir_Up) ? r_avoid.Min.y - size.y : (dir == ImGuiDir_Down) ? r_avoid.Max.y : base_pos_clamped.y; + + // Clamp top-left corner of popup + pos.x = ImMax(pos.x, r_outer.Min.x); + pos.y = ImMax(pos.y, r_outer.Min.y); + + *last_dir = dir; + return pos; + } } - // Fallback, try to keep within display + // Fallback when not enough room: *last_dir = ImGuiDir_None; + + // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible. + if (policy == ImGuiPopupPositionPolicy_Tooltip) + return ref_pos + ImVec2(2, 2); + + // Otherwise try to keep within display ImVec2 pos = ref_pos; pos.x = ImMax(ImMin(pos.x + size.x, r_outer.Max.x) - size.x, r_outer.Min.x); pos.y = ImMax(ImMin(pos.y + size.y, r_outer.Max.y) - size.y, r_outer.Min.y); @@ -8016,12 +8081,12 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) r_avoid = ImRect(-FLT_MAX, parent_window->ClipRect.Min.y, FLT_MAX, parent_window->ClipRect.Max.y); // Avoid parent menu-bar. If we wanted multi-line menu-bar, we may instead want to have the calling window setup e.g. a NextWindowData.PosConstraintAvoidRect field else r_avoid = ImRect(parent_window->Pos.x + horizontal_overlap, -FLT_MAX, parent_window->Pos.x + parent_window->Size.x - horizontal_overlap - parent_window->ScrollbarSizes.x, FLT_MAX); - return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); + return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Default); } if (window->Flags & ImGuiWindowFlags_Popup) { ImRect r_avoid = ImRect(window->Pos.x - 1, window->Pos.y - 1, window->Pos.x + 1, window->Pos.y + 1); - return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); + return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Default); } if (window->Flags & ImGuiWindowFlags_Tooltip) { @@ -8033,10 +8098,7 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8); else r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * sc, ref_pos.y + 24 * sc); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important. - ImVec2 pos = FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid); - if (window->AutoPosLastDirection == ImGuiDir_None) - pos = ref_pos + ImVec2(2, 2); // If there's not enough room, for tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible. - return pos; + return FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Tooltip); } IM_ASSERT(0); return window->Pos; @@ -8151,7 +8213,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x); float dby = NavScoreItemDistInterval(ImLerp(cand.Min.y, cand.Max.y, 0.2f), ImLerp(cand.Min.y, cand.Max.y, 0.8f), ImLerp(curr.Min.y, curr.Max.y, 0.2f), ImLerp(curr.Min.y, curr.Max.y, 0.8f)); // Scale down on Y to keep using box-distance for vertically touching items if (dby != 0.0f && dbx != 0.0f) - dbx = (dbx/1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f); + dbx = (dbx / 1000.0f) + ((dbx > 0.0f) ? +1.0f : -1.0f); float dist_box = ImFabs(dbx) + ImFabs(dby); // Compute distance between centers (this is off by a factor of 2, but we only compare center distances with each other so it doesn't matter) @@ -8192,7 +8254,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) ImDrawList* draw_list = GetForegroundDrawList(window); draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100)); draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200)); - draw_list->AddRectFilled(cand.Max - ImVec2(4,4), cand.Max + CalcTextSize(buf) + ImVec2(4,4), IM_COL32(40,0,0,150)); + draw_list->AddRectFilled(cand.Max - ImVec2(4, 4), cand.Max + CalcTextSize(buf) + ImVec2(4, 4), IM_COL32(40,0,0,150)); draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf); } else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate. @@ -8206,7 +8268,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf); } } - #endif +#endif // Is it in the quadrant we're interesting in moving to? bool new_best = false; @@ -8282,7 +8344,7 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con // Process Move Request (scoring for navigation) // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy) - if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled|ImGuiItemFlags_NoNav))) + if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) { ImGuiNavMoveResult* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; #if IMGUI_DEBUG_NAV_SCORING @@ -8366,18 +8428,20 @@ void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags mov // This way we could find the last focused window among our children. It would be much less confusing this way? static void ImGui::NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window) { - ImGuiWindow* parent_window = nav_window; - while (parent_window && (parent_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) - parent_window = parent_window->ParentWindow; - if (parent_window && parent_window != nav_window) - parent_window->NavLastChildNavWindow = nav_window; + ImGuiWindow* parent = nav_window; + while (parent && (parent->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) + parent = parent->ParentWindow; + if (parent && parent != nav_window) + parent->NavLastChildNavWindow = nav_window; } // Restore the last focused child. // Call when we are expected to land on the Main Layer (0) after FocusWindow() static ImGuiWindow* ImGui::NavRestoreLastChildNavWindow(ImGuiWindow* window) { - return window->NavLastChildNavWindow ? window->NavLastChildNavWindow : window; + if (window->NavLastChildNavWindow && window->NavLastChildNavWindow->WasActive) + return window->NavLastChildNavWindow; + return window; } static void NavRestoreLayer(ImGuiNavLayer layer) @@ -8410,7 +8474,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) if (!(window->Flags & ImGuiWindowFlags_NoNavInputs)) if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) init_for_nav = true; - //IMGUI_DEBUG_LOG("[Nav] NavInitWindow() init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from NavInitWindow(), init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); if (init_for_nav) { SetNavID(0, g.NavLayer, 0); @@ -8488,7 +8552,9 @@ ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInput static void ImGui::NavUpdate() { ImGuiContext& g = *GImGui; - g.IO.WantSetMousePos = false; + ImGuiIO& io = g.IO; + + io.WantSetMousePos = false; g.NavWrapRequestWindow = NULL; g.NavWrapRequestFlags = ImGuiNavMoveFlags_None; #if 0 @@ -8497,16 +8563,19 @@ static void ImGui::NavUpdate() // Set input source as Gamepad when buttons are pressed (as some features differs when used with Gamepad vs Keyboard) // (do it before we map Keyboard input!) - bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; - if (nav_gamepad_active) - if (g.IO.NavInputs[ImGuiNavInput_Activate] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Input] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Cancel] > 0.0f || g.IO.NavInputs[ImGuiNavInput_Menu] > 0.0f) + bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + if (nav_gamepad_active && g.NavInputSource != ImGuiInputSource_NavGamepad) + { + if (io.NavInputs[ImGuiNavInput_Activate] > 0.0f || io.NavInputs[ImGuiNavInput_Input] > 0.0f || io.NavInputs[ImGuiNavInput_Cancel] > 0.0f || io.NavInputs[ImGuiNavInput_Menu] > 0.0f + || io.NavInputs[ImGuiNavInput_DpadLeft] > 0.0f || io.NavInputs[ImGuiNavInput_DpadRight] > 0.0f || io.NavInputs[ImGuiNavInput_DpadUp] > 0.0f || io.NavInputs[ImGuiNavInput_DpadDown] > 0.0f) g.NavInputSource = ImGuiInputSource_NavGamepad; + } // Update Keyboard->Nav inputs mapping if (nav_keyboard_active) { - #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(g.IO.KeyMap[_KEY])) { g.IO.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; } } while (0) + #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(io.KeyMap[_KEY])) { io.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; } } while (0) NAV_MAP_KEY(ImGuiKey_Space, ImGuiNavInput_Activate ); NAV_MAP_KEY(ImGuiKey_Enter, ImGuiNavInput_Input ); NAV_MAP_KEY(ImGuiKey_Escape, ImGuiNavInput_Cancel ); @@ -8514,30 +8583,21 @@ static void ImGui::NavUpdate() NAV_MAP_KEY(ImGuiKey_RightArrow,ImGuiNavInput_KeyRight_); NAV_MAP_KEY(ImGuiKey_UpArrow, ImGuiNavInput_KeyUp_ ); NAV_MAP_KEY(ImGuiKey_DownArrow, ImGuiNavInput_KeyDown_ ); - if (g.IO.KeyCtrl) - g.IO.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; - if (g.IO.KeyShift) - g.IO.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; - if (g.IO.KeyAlt && !g.IO.KeyCtrl) // AltGR is Alt+Ctrl, also even on keyboards without AltGR we don't want Alt+Ctrl to open menu. - g.IO.NavInputs[ImGuiNavInput_KeyMenu_] = 1.0f; + if (io.KeyCtrl) + io.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; + if (io.KeyShift) + io.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; + if (io.KeyAlt && !io.KeyCtrl) // AltGR is Alt+Ctrl, also even on keyboards without AltGR we don't want Alt+Ctrl to open menu. + io.NavInputs[ImGuiNavInput_KeyMenu_] = 1.0f; #undef NAV_MAP_KEY } - memcpy(g.IO.NavInputsDownDurationPrev, g.IO.NavInputsDownDuration, sizeof(g.IO.NavInputsDownDuration)); - for (int i = 0; i < IM_ARRAYSIZE(g.IO.NavInputs); i++) - g.IO.NavInputsDownDuration[i] = (g.IO.NavInputs[i] > 0.0f) ? (g.IO.NavInputsDownDuration[i] < 0.0f ? 0.0f : g.IO.NavInputsDownDuration[i] + g.IO.DeltaTime) : -1.0f; + memcpy(io.NavInputsDownDurationPrev, io.NavInputsDownDuration, sizeof(io.NavInputsDownDuration)); + for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) + io.NavInputsDownDuration[i] = (io.NavInputs[i] > 0.0f) ? (io.NavInputsDownDuration[i] < 0.0f ? 0.0f : io.NavInputsDownDuration[i] + io.DeltaTime) : -1.0f; // Process navigation init request (select first/default focus) - // In very rare cases g.NavWindow may be null (e.g. clearing focus after requesting an init request, which does happen when releasing Alt while clicking on void) - if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove) && g.NavWindow) - { - // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) - //IMGUI_DEBUG_LOG("[Nav] Apply NavInitRequest result: 0x%08X Layer %d in \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); - if (g.NavInitRequestFromMove) - SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); - else - SetNavID(g.NavInitResultId, g.NavLayer, 0); - g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; - } + if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove)) + NavUpdateInitResult(); g.NavInitRequest = false; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; @@ -8560,12 +8620,12 @@ static void ImGui::NavUpdate() if (g.NavMousePosDirty && g.NavIdIsAlive) { // Set mouse position given our knowledge of the navigated item position from last frame - if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (g.IO.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) + if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) { if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) { - g.IO.MousePos = g.IO.MousePosPrev = NavCalcPreferredRefPos(); - g.IO.WantSetMousePos = true; + io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); + io.WantSetMousePos = true; } } g.NavMousePosDirty = false; @@ -8584,12 +8644,13 @@ static void ImGui::NavUpdate() NavUpdateWindowing(); // Set output flags for user application - g.IO.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs); - g.IO.NavVisible = (g.IO.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); + io.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs); + io.NavVisible = (io.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); // Process NavCancel input (to close a popup, get back to parent, clear focus) if (IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) { + IMGUI_DEBUG_LOG_NAV("[nav] ImGuiNavInput_Cancel\n"); if (g.ActiveId != 0) { if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) @@ -8675,6 +8736,7 @@ static void ImGui::NavUpdate() // (Preserve g.NavMoveRequestFlags, g.NavMoveClipDir which were set by the NavMoveRequestForward() function) IM_ASSERT(g.NavMoveDir != ImGuiDir_None && g.NavMoveClipDir != ImGuiDir_None); IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_ForwardQueued); + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequestForward %d\n", g.NavMoveDir); g.NavMoveRequestForward = ImGuiNavForward_ForwardActive; } @@ -8688,12 +8750,12 @@ static void ImGui::NavUpdate() if (g.NavMoveDir != ImGuiDir_None) { g.NavMoveRequest = true; - g.NavMoveRequestKeyMods = g.IO.KeyMods; + g.NavMoveRequestKeyMods = io.KeyMods; g.NavMoveDirLast = g.NavMoveDir; } if (g.NavMoveRequest && g.NavId == 0) { - //IMGUI_DEBUG_LOG("[Nav] NavInitRequest from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); g.NavInitRequest = g.NavInitRequestFromMove = true; // Reassigning with same value, we're being explicit here. g.NavInitResultId = 0; // -V1048 @@ -8706,7 +8768,7 @@ static void ImGui::NavUpdate() { // *Fallback* manual-scroll with Nav directional keys when window has no navigable item ImGuiWindow* window = g.NavWindow; - const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * g.IO.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. + const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * io.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) { if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) @@ -8717,17 +8779,11 @@ static void ImGui::NavUpdate() // *Normal* Manual scroll with NavScrollXXX keys // Next movement request will clamp the NavId reference rectangle to the visible area, so navigation will resume within those bounds. - ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f/10.0f, 10.0f); + ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f / 10.0f, 10.0f); if (scroll_dir.x != 0.0f && window->ScrollbarX) - { SetScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed)); - g.NavMoveFromClampedRefRect = true; - } if (scroll_dir.y != 0.0f) - { SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed)); - g.NavMoveFromClampedRefRect = true; - } } // Reset search results @@ -8735,23 +8791,25 @@ static void ImGui::NavUpdate() g.NavMoveResultLocalVisibleSet.Clear(); g.NavMoveResultOther.Clear(); - // When we have manually scrolled (without using navigation) and NavId becomes out of bounds, we project its bounding box to the visible area to restart navigation within visible items - if (g.NavMoveRequest && g.NavMoveFromClampedRefRect && g.NavLayer == ImGuiNavLayer_Main) + // When using gamepad, we project the reference nav bounding box into window visible area. + // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad every movements are relative + // (can't focus a visible object like we can with the mouse). + if (g.NavMoveRequest && g.NavInputSource == ImGuiInputSource_NavGamepad && g.NavLayer == ImGuiNavLayer_Main) { ImGuiWindow* window = g.NavWindow; - ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1,1), window->InnerRect.Max - window->Pos + ImVec2(1,1)); + ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1, 1), window->InnerRect.Max - window->Pos + ImVec2(1, 1)); if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) { + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel\n"); float pad = window->CalcFontSize() * 0.5f; window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item - window->NavRectRel[g.NavLayer].ClipWith(window_rect_rel); + window->NavRectRel[g.NavLayer].ClipWithFull(window_rect_rel); g.NavId = g.NavFocusScopeId = 0; } - g.NavMoveFromClampedRefRect = false; } // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) - ImRect nav_rect_rel = (g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted()) ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0,0,0,0); + ImRect nav_rect_rel = g.NavWindow ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : GetViewportRect(); g.NavScoringRect.TranslateY(nav_scoring_rect_offset_y); g.NavScoringRect.Min.x = ImMin(g.NavScoringRect.Min.x + 1.0f, g.NavScoringRect.Max.x); @@ -8769,6 +8827,22 @@ static void ImGui::NavUpdate() #endif } +static void ImGui::NavUpdateInitResult() +{ + // In very rare cases g.NavWindow may be null (e.g. clearing focus after requesting an init request, which does happen when releasing Alt while clicking on void) + ImGuiContext& g = *GImGui; + if (!g.NavWindow) + return; + + // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); + if (g.NavInitRequestFromMove) + SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); + else + SetNavID(g.NavInitResultId, g.NavLayer, 0); + g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; +} + // Apply result from previous frame navigation directional move request static void ImGui::NavUpdateMoveResult() { @@ -8828,32 +8902,34 @@ static void ImGui::NavUpdateMoveResult() g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = g.NavMoveRequestKeyMods; } + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); SetNavIDWithRectRel(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); - g.NavMoveFromClampedRefRect = false; } // Handle PageUp/PageDown/Home/End keys static float ImGui::NavUpdatePageUpPageDown() { ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + if (g.NavMoveDir != ImGuiDir_None || g.NavWindow == NULL) return 0.0f; if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main) return 0.0f; ImGuiWindow* window = g.NavWindow; - const bool page_up_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); - const bool page_down_held = IsKeyDown(g.IO.KeyMap[ImGuiKey_PageDown]) && !IsActiveIdUsingKey(ImGuiKey_PageDown); - const bool home_pressed = IsKeyPressed(g.IO.KeyMap[ImGuiKey_Home]) && !IsActiveIdUsingKey(ImGuiKey_Home); - const bool end_pressed = IsKeyPressed(g.IO.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); + const bool page_up_held = IsKeyDown(io.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); + const bool page_down_held = IsKeyDown(io.KeyMap[ImGuiKey_PageDown]) && !IsActiveIdUsingKey(ImGuiKey_PageDown); + const bool home_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_Home]) && !IsActiveIdUsingKey(ImGuiKey_Home); + const bool end_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); if (page_up_held != page_down_held || home_pressed != end_pressed) // If either (not both) are pressed { if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll) { // Fallback manual-scroll when window has no navigable item - if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) + if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); - else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) + else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); else if (home_pressed) SetScrollY(window, 0.0f); @@ -8865,14 +8941,14 @@ static float ImGui::NavUpdatePageUpPageDown() ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; const float page_offset_y = ImMax(0.0f, window->InnerRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); float nav_scoring_rect_offset_y = 0.0f; - if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageUp], true)) + if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) { nav_scoring_rect_offset_y = -page_offset_y; g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) g.NavMoveClipDir = ImGuiDir_Up; g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; } - else if (IsKeyPressed(g.IO.KeyMap[ImGuiKey_PageDown], true)) + else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) { nav_scoring_rect_offset_y = +page_offset_y; g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) @@ -8969,7 +9045,7 @@ static void ImGui::NavEndFrame() static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N) { ImGuiContext& g = *GImGui; - for (int i = g.WindowsFocusOrder.Size-1; i >= 0; i--) + for (int i = g.WindowsFocusOrder.Size - 1; i >= 0; i--) if (g.WindowsFocusOrder[i] == window) return i; return -1; @@ -9010,11 +9086,9 @@ static void ImGui::NavUpdateWindowing() bool apply_toggle_layer = false; ImGuiWindow* modal_window = GetTopMostPopupModal(); - if (modal_window != NULL) - { + bool allow_windowing = (modal_window == NULL); + if (!allow_windowing) g.NavWindowingTarget = NULL; - return; - } // Fade out if (g.NavWindowingTargetAnim && g.NavWindowingTarget == NULL) @@ -9025,8 +9099,8 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL-TAB or Square+L/R window selection - bool start_windowing_with_gamepad = !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); - bool start_windowing_with_keyboard = !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); + bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); + bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { @@ -9093,10 +9167,11 @@ static void ImGui::NavUpdateWindowing() if (move_delta.x != 0.0f || move_delta.y != 0.0f) { const float NAV_MOVE_SPEED = 800.0f; - const float move_speed = ImFloor(NAV_MOVE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); // FIXME: Doesn't code variable framerate very well - SetWindowPos(g.NavWindowingTarget->RootWindow, g.NavWindowingTarget->RootWindow->Pos + move_delta * move_speed, ImGuiCond_Always); + const float move_speed = ImFloor(NAV_MOVE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); // FIXME: Doesn't handle variable framerate very well + ImGuiWindow* moving_window = g.NavWindowingTarget->RootWindow; + SetWindowPos(moving_window, moving_window->Pos + move_delta * move_speed, ImGuiCond_Always); + MarkIniSettingsDirty(moving_window); g.NavDisableMouseHover = true; - MarkIniSettingsDirty(g.NavWindowingTarget); } } @@ -9183,6 +9258,7 @@ void ImGui::NavUpdateWindowingOverlay() PopStyleVar(); } + //----------------------------------------------------------------------------- // [SECTION] DRAG AND DROP //----------------------------------------------------------------------------- @@ -9372,7 +9448,8 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) return false; ImGuiWindow* window = g.CurrentWindow; - if (g.HoveredWindow == NULL || window->RootWindow != g.HoveredWindow->RootWindow) + ImGuiWindow* hovered_window = g.HoveredWindowUnderMovingWindow; + if (hovered_window == NULL || window->RootWindow != hovered_window->RootWindow) return false; IM_ASSERT(id != 0); if (!IsMouseHoveringRect(bb.Min, bb.Max) || (id == g.DragDropPayload.SourceId)) @@ -9400,7 +9477,8 @@ bool ImGui::BeginDragDropTarget() ImGuiWindow* window = g.CurrentWindow; if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) return false; - if (g.HoveredWindow == NULL || window->RootWindow != g.HoveredWindow->RootWindow) + ImGuiWindow* hovered_window = g.HoveredWindowUnderMovingWindow; + if (hovered_window == NULL || window->RootWindow != hovered_window->RootWindow) return false; const ImRect& display_rect = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? window->DC.LastItemDisplayRect : window->DC.LastItemRect; @@ -9453,7 +9531,7 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop // FIXME-DRAG: Settle on a proper default visuals for drop target. r.Expand(3.5f); bool push_clip_rect = !window->ClipRect.Contains(r); - if (push_clip_rect) window->DrawList->PushClipRect(r.Min-ImVec2(1,1), r.Max+ImVec2(1,1)); + if (push_clip_rect) window->DrawList->PushClipRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1)); window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f); if (push_clip_rect) window->DrawList->PopClipRect(); } @@ -9695,6 +9773,7 @@ void ImGui::LogButtons() LogToClipboard(); } + //----------------------------------------------------------------------------- // [SECTION] SETTINGS //----------------------------------------------------------------------------- @@ -9949,9 +10028,9 @@ static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, ImGuiWindowSettings* settings = (ImGuiWindowSettings*)entry; int x, y; int i; - if (sscanf(line, "Pos=%i,%i", &x, &y) == 2) settings->Pos = ImVec2ih((short)x, (short)y); - else if (sscanf(line, "Size=%i,%i", &x, &y) == 2) settings->Size = ImVec2ih((short)x, (short)y); - else if (sscanf(line, "Collapsed=%d", &i) == 1) settings->Collapsed = (i != 0); + if (sscanf(line, "Pos=%i,%i", &x, &y) == 2) { settings->Pos = ImVec2ih((short)x, (short)y); } + else if (sscanf(line, "Size=%i,%i", &x, &y) == 2) { settings->Size = ImVec2ih((short)x, (short)y); } + else if (sscanf(line, "Collapsed=%d", &i) == 1) { settings->Collapsed = (i != 0); } } // Apply to existing windows (if any) @@ -10290,7 +10369,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (draw_list == ImGui::GetWindowDrawList()) { ImGui::SameLine(); - ImGui::TextColored(ImVec4(1.0f,0.4f,0.4f,1.0f), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered) + ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered) if (node_open) ImGui::TreePop(); return; } @@ -10318,7 +10397,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; char buf[300]; ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d triangles, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", - pcmd->ElemCount/3, (void*)(intptr_t)pcmd->TextureId, + pcmd->ElemCount / 3, (void*)(intptr_t)pcmd->TextureId, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); if (ImGui::IsItemHovered() && (show_drawcmd_mesh || show_drawcmd_aabb) && fg_draw_list) @@ -10344,11 +10423,11 @@ void ImGui::ShowMetricsWindow(bool* p_open) NodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, elem_offset, true, false); // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. - ImGuiListClipper clipper(pcmd->ElemCount/3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. + ImGuiListClipper clipper(pcmd->ElemCount / 3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. while (clipper.Step()) - for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart*3; prim < clipper.DisplayEnd; prim++) + for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart * 3; prim < clipper.DisplayEnd; prim++) { - char *buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); + char* buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); ImVec2 triangle[3]; for (int n = 0; n < 3; n++, idx_i++) { @@ -10386,7 +10465,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) { if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size)) return; - for (int i = 0; i < windows.Size; i++) + ImGui::Text("(In front-to-back order:)"); + for (int i = windows.Size - 1; i >= 0; i--) // Iterate front to back { ImGui::PushID(windows[i]); Funcs::NodeWindow(windows[i], "Window"); @@ -10402,14 +10482,18 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::BulletText("%s: NULL", label); return; } - bool open = ImGui::TreeNode(label, "%s '%s', %d @ 0x%p", label, window->Name, (window->Active || window->WasActive), window); - if (ImGui::IsItemHovered() && window->WasActive) - ImGui::GetForegroundDrawList()->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); + + ImGuiContext& g = *GImGui; + const bool is_active = window->WasActive; + ImGuiTreeNodeFlags tree_node_flags = (window == g.NavWindow) ? ImGuiTreeNodeFlags_Selected : ImGuiTreeNodeFlags_None; + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + const bool open = ImGui::TreeNodeEx(label, tree_node_flags, "%s '%s'%s", label, window->Name, is_active ? "" : " *Inactive*"); + if (!is_active) { PopStyleColor(); } + if (ImGui::IsItemHovered() && is_active) + ImGui::GetForegroundDrawList(window)->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); if (!open) return; - if (!window->WasActive) - ImGui::TextDisabled("Note: window is not currently visible."); if (window->MemoryCompacted) ImGui::TextDisabled("Note: some memory buffers have been compacted/freed."); @@ -10454,15 +10538,20 @@ void ImGui::ShowMetricsWindow(bool* p_open) char buf[256]; char* p = buf; const char* buf_end = buf + IM_ARRAYSIZE(buf); - p += ImFormatString(p, buf_end - p, "TabBar (%d tabs)%s", tab_bar->Tabs.Size, (tab_bar->PrevFrameVisible < ImGui::GetFrameCount() - 2) ? " *Inactive*" : ""); - if (ImGui::TreeNode(tab_bar, "%s", buf)) + const bool is_active = (tab_bar->PrevFrameVisible >= ImGui::GetFrameCount() - 2); + p += ImFormatString(p, buf_end - p, "Tab Bar 0x%08X (%d tabs)%s", tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); + IM_UNUSED(p); + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + bool open = ImGui::TreeNode(tab_bar, "%s", buf); + if (!is_active) { PopStyleColor(); } + if (open) { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; ImGui::PushID(tab); - if (ImGui::SmallButton("<")) { TabBarQueueChangeTabOrder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); - if (ImGui::SmallButton(">")) { TabBarQueueChangeTabOrder(tab_bar, tab, +1); } ImGui::SameLine(); + if (ImGui::SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); + if (ImGui::SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } ImGui::SameLine(); ImGui::Text("%02d%c Tab 0x%08X '%s'", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : ""); ImGui::PopID(); } @@ -10557,7 +10646,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Details for Docking #ifdef IMGUI_HAS_DOCK - if (ImGui::TreeNode("Docking")) + if (ImGui::TreeNode("Dock nodes")) { ImGui::TreePop(); } @@ -10569,6 +10658,9 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (ImGui::SmallButton("Clear")) ImGui::ClearIniSettings(); ImGui::SameLine(); + if (ImGui::SmallButton("Save to memory")) + ImGui::SaveIniSettingsToMemory(); + ImGui::SameLine(); if (ImGui::SmallButton("Save to disk")) ImGui::SaveIniSettingsToDisk(g.IO.IniFilename); ImGui::SameLine(); @@ -10580,7 +10672,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (ImGui::TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) { for (int n = 0; n < g.SettingsHandlers.Size; n++) - ImGui::TextUnformatted(g.SettingsHandlers[n].TypeName); + ImGui::BulletText("%s", g.SettingsHandlers[n].TypeName); ImGui::TreePop(); } if (ImGui::TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) @@ -10604,8 +10696,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (ImGui::TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size())) { - char* buf = (char*)(void*)(g.SettingsIniData.Buf.Data ? g.SettingsIniData.Buf.Data : ""); - ImGui::InputTextMultiline("##Ini", buf, g.SettingsIniData.Buf.Size, ImVec2(-FLT_MIN, 0.0f), ImGuiInputTextFlags_ReadOnly); + ImGui::InputTextMultiline("##Ini", (char*)(void*)g.SettingsIniData.c_str(), g.SettingsIniData.Buf.Size, ImVec2(-FLT_MIN, 0.0f), ImGuiInputTextFlags_ReadOnly); ImGui::TreePop(); } ImGui::TreePop(); @@ -10615,20 +10706,35 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (ImGui::TreeNode("Internal state")) { const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); + + ImGui::Text("WINDOWING"); + ImGui::Indent(); ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); - ImGui::Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + ImGui::Text("HoveredWindowUnderMovingWindow: '%s'", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : "NULL"); + ImGui::Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); + ImGui::Unindent(); + + ImGui::Text("ITEMS"); + ImGui::Indent(); ImGui::Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); ImGui::Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); - ImGui::Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); + ImGui::Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + ImGui::Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); + ImGui::Unindent(); + + ImGui::Text("NAV,FOCUS"); + ImGui::Indent(); ImGui::Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); ImGui::Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); ImGui::Text("NavInputSource: %s", input_source_names[g.NavInputSource]); ImGui::Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); ImGui::Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); ImGui::Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); + ImGui::Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); ImGui::Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); - ImGui::Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); + ImGui::Unindent(); + ImGui::TreePop(); } diff --git a/imgui/imgui.h b/imgui/imgui.h index eb2a611b..48889186 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (headers) // Help: @@ -59,8 +59,8 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.77 WIP" -#define IMGUI_VERSION_NUM 17602 +#define IMGUI_VERSION "1.79 WIP" +#define IMGUI_VERSION_NUM 17803 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Define attributes of all API symbols declarations (e.g. for DLL under Windows) @@ -85,8 +85,8 @@ Index of this file: #define IM_FMTARGS(FMT) #define IM_FMTLIST(FMT) #endif -#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*_ARR))) // Size of a static C-style array. Don't use on pointers! -#define IM_UNUSED(_VAR) ((void)_VAR) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. +#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! +#define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. #if (__cplusplus >= 201100) #define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 #else @@ -151,8 +151,9 @@ typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect(), AddRectFilled() etc. typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList -typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas +typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags +typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for InvisibleButton() typedef int ImGuiColorEditFlags; // -> enum ImGuiColorEditFlags_ // Flags: for ColorEdit4(), ColorPicker4() etc. typedef int ImGuiConfigFlags; // -> enum ImGuiConfigFlags_ // Flags: for io.ConfigFlags typedef int ImGuiComboFlags; // -> enum ImGuiComboFlags_ // Flags: for BeginCombo() @@ -161,7 +162,9 @@ typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: f typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() typedef int ImGuiKeyModFlags; // -> enum ImGuiKeyModFlags_ // Flags: for io.KeyMods (Ctrl/Shift/Alt/Super) +typedef int ImGuiPopupFlags; // -> enum ImGuiPopupFlags_ // Flags: for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() +typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem() typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: for TreeNode(), TreeNodeEx(), CollapsingHeader() @@ -172,7 +175,7 @@ typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: f typedef void* ImTextureID; // User data for rendering back-end to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. #endif typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. -typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData *data); +typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Decoded character types @@ -331,8 +334,8 @@ namespace ImGui // Windows Scrolling IMGUI_API float GetScrollX(); // get scrolling amount [0..GetScrollMaxX()] IMGUI_API float GetScrollY(); // get scrolling amount [0..GetScrollMaxY()] - IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.X - WindowSize.X - IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.Y - WindowSize.Y + IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.x - WindowSize.x + IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.y - WindowSize.y IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0..GetScrollMaxX()] IMGUI_API void SetScrollY(float scroll_y); // set scrolling amount [0..GetScrollMaxY()] IMGUI_API void SetScrollHereX(float center_x_ratio = 0.5f); // adjust scrolling amount to make current cursor position visible. center_x_ratio=0.0: left, 0.5: center, 1.0: right. When using to make a "default/current item" visible, consider using SetItemDefaultFocus() instead. @@ -436,7 +439,7 @@ namespace ImGui // - You may also use one of the many IsItemXXX functions (e.g. IsItemActive, IsItemHovered, etc.) to query widget state. IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0, 0)); // button IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text - IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size); // button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) + IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size, ImGuiButtonFlags flags = 0); // flexible button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding @@ -445,7 +448,7 @@ namespace ImGui IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1, 0), const char* overlay = NULL); - IMGUI_API void Bullet(); // draw a small circle and keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses + IMGUI_API void Bullet(); // draw a small circle + keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses // Widgets: Combo Box // - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. @@ -456,44 +459,50 @@ namespace ImGui IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_max_height_in_items = -1); // Separate items with \0 within a string, end item-list with \0\0. e.g. "One\0Two\0Three\0" IMGUI_API bool Combo(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_max_height_in_items = -1); - // Widgets: Drags + // Widgets: Drag Sliders // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped and can go off-bounds. // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every functions, note that a 'float v[X]' function argument is the same as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. + // - Format string may also be set to NULL or use the default format ("%f" or "%d"). // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits. // - Use v_max = FLT_MAX / INT_MAX etc to avoid clamping to a maximum, same with v_min = -FLT_MAX / INT_MIN to avoid clamping to a minimum. - // - Use v_min > v_max to lock edits. - IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); // If v_min >= v_max we have no bound - IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", const char* format_max = NULL, float power = 1.0f); - IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); // If v_min >= v_max we have no bound - IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); - IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); - IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); - IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL); - IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, float power = 1.0f); - IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, float power = 1.0f); - - // Widgets: Sliders + // - We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. + // - Legacy: Pre-1.78 there are DragXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. + // If you get a warning converting a float to ImGuiSliderFlags, read https://github.com/ocornut/imgui/issues/3361 + IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); // If v_min >= v_max we have no bound + IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", const char* format_max = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); // If v_min >= v_max we have no bound + IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); + + // Widgets: Regular Sliders // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. - IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. Use power!=1.0 for power curve sliders - IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool SliderAngle(const char* label, float* v_rad, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f, const char* format = "%.0f deg"); - IMGUI_API bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = "%d"); - IMGUI_API bool SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format = "%d"); - IMGUI_API bool SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format = "%d"); - IMGUI_API bool SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format = "%d"); - IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); - IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); - IMGUI_API bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format = "%.3f", float power = 1.0f); - IMGUI_API bool VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format = "%d"); - IMGUI_API bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, float power = 1.0f); + // - Format string may also be set to NULL or use the default format ("%f" or "%d"). + // - Legacy: Pre-1.78 there are SliderXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. + // If you get a warning converting a float to ImGuiSliderFlags, read https://github.com/ocornut/imgui/issues/3361 + IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. + IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderAngle(const char* label, float* v_rad, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f, const char* format = "%.0f deg", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); + IMGUI_API bool VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format = "%d", ImGuiSliderFlags flags = 0); + IMGUI_API bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, ImGuiSliderFlags flags = 0); // Widgets: Input with Keyboard // - If you want to use InputText() with std::string or any custom dynamic string type, see misc/cpp/imgui_stdlib.h and comments in imgui_demo.cpp. @@ -605,20 +614,27 @@ namespace ImGui IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // return true if the modal is open, and you can start outputting to it. IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! // Popups: open/close functions - // - OpenPopup(): set popup state to open. + // - OpenPopup(): set popup state to open. ImGuiPopupFlags are available for opening options. // - If not modal: they can be closed by clicking anywhere outside them, or by pressing ESCAPE. // - CloseCurrentPopup(): use inside the BeginPopup()/EndPopup() scope to close manually. // - CloseCurrentPopup() is called by default by Selectable()/MenuItem() when activated (FIXME: need some options). - IMGUI_API void OpenPopup(const char* str_id); // call to mark popup as open (don't call every frame!). - IMGUI_API bool OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) + // - Use ImGuiPopupFlags_NoOpenOverExistingPopup to avoid opening a popup if there's already one at the same level. This is equivalent to e.g. testing for !IsAnyPopupOpen() prior to OpenPopup(). + IMGUI_API void OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags = 0); // call to mark popup as open (don't call every frame!). + IMGUI_API bool OpenPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) IMGUI_API void CloseCurrentPopup(); // manually close the popup we have begin-ed into. - IMGUI_API bool IsPopupOpen(const char* str_id); // return true if the popup is open at the current BeginPopup() level of the popup stack // Popups: open+begin combined functions helpers // - Helpers to do OpenPopup+BeginPopup where the Open action is triggered by e.g. hovering an item and right-clicking. // - They are convenient to easily create context menus, hence the name. - IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! - IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1, bool also_over_items = true); // open+begin popup when clicked on current window. - IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiMouseButton mouse_b = 1); // open+begin popup when clicked in void (where there are no windows). + // - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future. + // - We exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter. Passing a mouse button to ImGuiPopupFlags is guaranteed to be legal. + IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! + IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window. + IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows). + // Popups: test function + // - IsPopupOpen(): return true if the popup is open at the current BeginPopup() level of the popup stack. + // - IsPopupOpen() with ImGuiPopupFlags_AnyPopupId: return true if any popup is open at the current BeginPopup() level of the popup stack. + // - IsPopupOpen() with ImGuiPopupFlags_AnyPopupId + ImGuiPopupFlags_AnyPopupLevel: return true if any popup is open. + IMGUI_API bool IsPopupOpen(const char* str_id, ImGuiPopupFlags flags = 0); // return true if the popup is open. // Columns // - You can also use SameLine(pos_x) to mimic simplified columns. @@ -716,7 +732,7 @@ namespace ImGui IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); // Inputs Utilities: Keyboard - // - For 'int user_key_index' you can use your own indices/enums according to how your backend/engine stored them in io.KeysDown[]. + // - For 'int user_key_index' you can use your own indices/enums according to how your back-end/engine stored them in io.KeysDown[]. // - We don't know the meaning of those value. You can use GetKeyIndex() to map a ImGuiKey_ value into the user index. IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. @@ -732,7 +748,7 @@ namespace ImGui IMGUI_API bool IsMouseDown(ImGuiMouseButton button); // is mouse button held? IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, bool repeat = false); // did mouse button clicked? (went from !Down to Down) IMGUI_API bool IsMouseReleased(ImGuiMouseButton button); // did mouse button released? (went from Down to !Down) - IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button); // did mouse button double-clicked? a double-click returns false in IsMouseClicked(). uses io.MouseDoubleClickTime. + IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button); // did mouse button double-clicked? (note that a double-click will also report IsMouseClicked() == true) IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);// is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse available IMGUI_API bool IsAnyMouseDown(); // is any mouse button held? @@ -838,6 +854,7 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) + ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) // [Internal] ImGuiInputTextFlags_Multiline = 1 << 20, // For internal use by InputTextMultiline() ImGuiInputTextFlags_NoMarkEdited = 1 << 21 // For internal use by functions using InputText() before reformatting data @@ -865,6 +882,29 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog }; +// Flags for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() functions. +// - To be backward compatible with older API which took an 'int mouse_button = 1' argument, we need to treat +// small flags values as a mouse button index, so we encode the mouse button in the first few bits of the flags. +// It is therefore guaranteed to be legal to pass a mouse button index in ImGuiPopupFlags. +// - For the same reason, we exceptionally default the ImGuiPopupFlags argument of BeginPopupContextXXX functions to 1 instead of 0. +// IMPORTANT: because the default parameter is 1 (==ImGuiPopupFlags_MouseButtonRight), if you rely on the default parameter +// and want to another another flag, you need to pass in the ImGuiPopupFlags_MouseButtonRight flag. +// - Multiple buttons currently cannot be combined/or-ed in those functions (we could allow it later). +enum ImGuiPopupFlags_ +{ + ImGuiPopupFlags_None = 0, + ImGuiPopupFlags_MouseButtonLeft = 0, // For BeginPopupContext*(): open on Left Mouse release. Guaranteed to always be == 0 (same as ImGuiMouseButton_Left) + ImGuiPopupFlags_MouseButtonRight = 1, // For BeginPopupContext*(): open on Right Mouse release. Guaranteed to always be == 1 (same as ImGuiMouseButton_Right) + ImGuiPopupFlags_MouseButtonMiddle = 2, // For BeginPopupContext*(): open on Middle Mouse release. Guaranteed to always be == 2 (same as ImGuiMouseButton_Middle) + ImGuiPopupFlags_MouseButtonMask_ = 0x1F, + ImGuiPopupFlags_MouseButtonDefault_ = 1, + ImGuiPopupFlags_NoOpenOverExistingPopup = 1 << 5, // For OpenPopup*(), BeginPopupContext*(): don't open if there's already a popup at the same level of the popup stack + ImGuiPopupFlags_NoOpenOverItems = 1 << 6, // For BeginPopupContextWindow(): don't return true when hovering items, only when hovering empty space + ImGuiPopupFlags_AnyPopupId = 1 << 7, // For IsPopupOpen(): ignore the ImGuiID parameter and test for any popup. + ImGuiPopupFlags_AnyPopupLevel = 1 << 8, // For IsPopupOpen(): search/test at any level of the popup stack (default test in the current level) + ImGuiPopupFlags_AnyPopup = ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel +}; + // Flags for ImGui::Selectable() enum ImGuiSelectableFlags_ { @@ -1194,6 +1234,19 @@ enum ImGuiStyleVar_ #endif }; +// Flags for InvisibleButton() [extended in imgui_internal.h] +enum ImGuiButtonFlags_ +{ + ImGuiButtonFlags_None = 0, + ImGuiButtonFlags_MouseButtonLeft = 1 << 0, // React on left mouse button (default) + ImGuiButtonFlags_MouseButtonRight = 1 << 1, // React on right mouse button + ImGuiButtonFlags_MouseButtonMiddle = 1 << 2, // React on center mouse button + + // [Internal] + ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, + ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft +}; + // Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() enum ImGuiColorEditFlags_ { @@ -1226,13 +1279,13 @@ enum ImGuiColorEditFlags_ // Defaults Options. You can set application defaults using SetColorEditOptions(). The intent is that you probably don't want to // override them in most of your calls. Let the user choose via the option menu and/or call SetColorEditOptions() once during startup. - ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_PickerHueBar, + ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_PickerHueBar, // [Internal] Masks - ImGuiColorEditFlags__DisplayMask = ImGuiColorEditFlags_DisplayRGB|ImGuiColorEditFlags_DisplayHSV|ImGuiColorEditFlags_DisplayHex, - ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_Float, - ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel|ImGuiColorEditFlags_PickerHueBar, - ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_InputHSV + ImGuiColorEditFlags__DisplayMask = ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_DisplayHex, + ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_Float, + ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_PickerHueBar, + ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1240,6 +1293,18 @@ enum ImGuiColorEditFlags_ #endif }; +// Flags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. +// We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. +enum ImGuiSliderFlags_ +{ + ImGuiSliderFlags_None = 0, + ImGuiSliderFlags_ClampOnInput = 1 << 4, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. + ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. + ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits) + ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget + ImGuiSliderFlags_InvalidMask_ = 0x7000000F // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. +}; + // Identify a mouse button. // Those values are guaranteed to be stable and we frequently use 0/1 directly. Named enums provided for convenience. enum ImGuiMouseButton_ @@ -1277,7 +1342,8 @@ enum ImGuiMouseCursor_ // Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always. enum ImGuiCond_ { - ImGuiCond_Always = 1 << 0, // Set the variable + ImGuiCond_None = 0, // No condition (always set the variable), same as _Always + ImGuiCond_Always = 1 << 0, // No condition (always set the variable) ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call will succeed) ImGuiCond_FirstUseEver = 1 << 2, // Set the variable if the object/window has no persistently saved data (no entry in .ini file) ImGuiCond_Appearing = 1 << 3 // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) @@ -1287,16 +1353,16 @@ enum ImGuiCond_ // Helpers: Memory allocations macros // IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() // We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. -// Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. +// Defining a custom placement new() with a custom parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. //----------------------------------------------------------------------------- -struct ImNewDummy {}; -inline void* operator new(size_t, ImNewDummy, void* ptr) { return ptr; } -inline void operator delete(void*, ImNewDummy, void*) {} // This is only required so we can use the symmetrical new() +struct ImNewWrapper {}; +inline void* operator new(size_t, ImNewWrapper, void* ptr) { return ptr; } +inline void operator delete(void*, ImNewWrapper, void*) {} // This is only required so we can use the symmetrical new() #define IM_ALLOC(_SIZE) ImGui::MemAlloc(_SIZE) #define IM_FREE(_PTR) ImGui::MemFree(_PTR) -#define IM_PLACEMENT_NEW(_PTR) new(ImNewDummy(), _PTR) -#define IM_NEW(_TYPE) new(ImNewDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE +#define IM_PLACEMENT_NEW(_PTR) new(ImNewWrapper(), _PTR) +#define IM_NEW(_TYPE) new(ImNewWrapper(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } //----------------------------------------------------------------------------- @@ -1331,6 +1397,7 @@ struct ImVector inline bool empty() const { return Size == 0; } inline int size() const { return Size; } inline int size_in_bytes() const { return Size * (int)sizeof(T); } + inline int max_size() const { return 0x7FFFFFFF / (int)sizeof(T); } inline int capacity() const { return Capacity; } inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } @@ -1346,7 +1413,7 @@ struct ImVector inline const T& back() const { IM_ASSERT(Size > 0); return Data[Size - 1]; } inline void swap(ImVector& rhs) { int rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; int rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; T* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; } - inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity/2) : 8; return new_capacity > sz ? new_capacity : sz; } + inline int _grow_capacity(int sz) const { int new_capacity = Capacity ? (Capacity + Capacity / 2) : 8; return new_capacity > sz ? new_capacity : sz; } inline void resize(int new_size) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); Size = new_size; } inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } inline void shrink(int new_size) { IM_ASSERT(new_size <= Size); Size = new_size; } // Resize a vector to a smaller size, guaranteed not to cause a reallocation @@ -1356,10 +1423,10 @@ struct ImVector inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } inline void pop_back() { IM_ASSERT(Size > 0); Size--; } inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } - inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data+Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } - inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data+Size && it_last > it && it_last <= Data+Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - count) * sizeof(T)); Size -= (int)count; return Data + off; } - inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data+Size); const ptrdiff_t off = it - Data; if (it < Data+Size-1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } - inline T* insert(const T* it, const T& v) { IM_ASSERT(it >= Data && it <= Data+Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; } + inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } + inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last > it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - count) * sizeof(T)); Size -= (int)count; return Data + off; } + inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; if (it < Data + Size - 1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } + inline T* insert(const T* it, const T& v) { IM_ASSERT(it >= Data && it <= Data + Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; } inline bool contains(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; } inline T* find(const T& v) { T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } inline const T* find(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } @@ -1400,17 +1467,19 @@ struct ImGuiStyle float ScrollbarRounding; // Radius of grab corners for scrollbar. float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar. float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. + float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. float TabMinWidthForUnselectedCloseButton; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. - ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area by at least this amount. Only applies to regular windows. + ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. - bool AntiAliasedLines; // Enable anti-aliasing on lines/borders. Disable if you are really tight on CPU/GPU. - bool AntiAliasedFill; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.) + bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). + bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering. Latched at the beginning of the frame (copied to ImDrawList). + bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. float CircleSegmentMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. ImVec4 Colors[ImGuiCol_COUNT]; @@ -1555,6 +1624,7 @@ struct ImGuiIO float KeysDownDurationPrev[512]; // Previous duration the key has been down float NavInputsDownDuration[ImGuiNavInput_COUNT]; float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; + float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16 ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper. @@ -1568,9 +1638,10 @@ struct ImGuiIO // Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used. // The callback function should return 0 by default. // Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details) +// - ImGuiInputTextFlags_CallbackEdit: Callback on buffer edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) +// - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration // - ImGuiInputTextFlags_CallbackCompletion: Callback on pressing TAB // - ImGuiInputTextFlags_CallbackHistory: Callback on pressing Up/Down arrows -// - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration // - ImGuiInputTextFlags_CallbackCharFilter: Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. // - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. struct ImGuiInputTextCallbackData @@ -1597,7 +1668,9 @@ struct ImGuiInputTextCallbackData IMGUI_API ImGuiInputTextCallbackData(); IMGUI_API void DeleteChars(int pos, int bytes_count); IMGUI_API void InsertChars(int pos, const char* text, const char* text_end = NULL); - bool HasSelection() const { return SelectionStart != SelectionEnd; } + void SelectAll() { SelectionStart = 0; SelectionEnd = BufTextLen; } + void ClearSelection() { SelectionStart = SelectionEnd = BufTextLen; } + bool HasSelection() const { return SelectionStart != SelectionEnd; } }; // Resizing callback data to apply custom constraint. As enabled by SetNextWindowSizeConstraints(). Callback is called during the next Begin(). @@ -1621,7 +1694,7 @@ struct ImGuiPayload ImGuiID SourceId; // Source item id ImGuiID SourceParentId; // Source parent id (if available) int DataFrameCount; // Data timestamp - char DataType[32+1]; // Data type tag (short user-supplied string, 32 characters max) + char DataType[32 + 1]; // Data type tag (short user-supplied string, 32 characters max) bool Preview; // Set when AcceptDragDropPayload() was called and mouse has been hovering the target item (nb: handle overlapping drag targets) bool Delivery; // Set when AcceptDragDropPayload() was called and mouse button is released over the target item. @@ -1640,8 +1713,24 @@ struct ImGuiPayload #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.78 (from August 2020) + // Old drag/sliders functions that took a 'float power = 1.0' argument instead of flags. + // For shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. + IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power); + IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power); + static inline bool DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } + IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power); + IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format, float power); + static inline bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } + static inline bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } + static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } + static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } // OBSOLETED in 1.77 (from June 2020) - static inline bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mouse_button = 1) { return OpenPopupContextItem(str_id, mouse_button); } + static inline bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mb = 1) { return OpenPopupContextItem(str_id, mb); } // Passing a mouse button to ImGuiPopupFlags is legal + static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } // OBSOLETED in 1.72 (from July 2019) static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } // OBSOLETED in 1.71 (from June 2019) @@ -1797,7 +1886,7 @@ struct ImGuiStorage // ImGui::Text("line number %d", i); // - Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height (step skipped if we passed a known height as second arg to constructor). // - Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. -// - (Step 2: dummy step only required if an explicit items_height was passed to constructor or Begin() and user call Step(). Does nothing and switch to Step 3.) +// - (Step 2: empty step only required if an explicit items_height was passed to constructor or Begin() and user call Step(). Does nothing and switch to Step 3.) // - Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. struct ImGuiListClipper { @@ -1848,8 +1937,8 @@ struct ImColor ImVec4 Value; ImColor() { Value.x = Value.y = Value.z = Value.w = 0.0f; } - ImColor(int r, int g, int b, int a = 255) { float sc = 1.0f/255.0f; Value.x = (float)r * sc; Value.y = (float)g * sc; Value.z = (float)b * sc; Value.w = (float)a * sc; } - ImColor(ImU32 rgba) { float sc = 1.0f/255.0f; Value.x = (float)((rgba>>IM_COL32_R_SHIFT)&0xFF) * sc; Value.y = (float)((rgba>>IM_COL32_G_SHIFT)&0xFF) * sc; Value.z = (float)((rgba>>IM_COL32_B_SHIFT)&0xFF) * sc; Value.w = (float)((rgba>>IM_COL32_A_SHIFT)&0xFF) * sc; } + ImColor(int r, int g, int b, int a = 255) { float sc = 1.0f / 255.0f; Value.x = (float)r * sc; Value.y = (float)g * sc; Value.z = (float)b * sc; Value.w = (float)a * sc; } + ImColor(ImU32 rgba) { float sc = 1.0f / 255.0f; Value.x = (float)((rgba >> IM_COL32_R_SHIFT) & 0xFF) * sc; Value.y = (float)((rgba >> IM_COL32_G_SHIFT) & 0xFF) * sc; Value.z = (float)((rgba >> IM_COL32_B_SHIFT) & 0xFF) * sc; Value.w = (float)((rgba >> IM_COL32_A_SHIFT) & 0xFF) * sc; } ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } ImColor(const ImVec4& col) { Value = col; } inline operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } @@ -1857,7 +1946,7 @@ struct ImColor // FIXME-OBSOLETE: May need to obsolete/cleanup those helpers. inline void SetHSV(float h, float s, float v, float a = 1.0f){ ImGui::ColorConvertHSVtoRGB(h, s, v, Value.x, Value.y, Value.z); Value.w = a; } - static ImColor HSV(float h, float s, float v, float a = 1.0f) { float r,g,b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r,g,b,a); } + static ImColor HSV(float h, float s, float v, float a = 1.0f) { float r, g, b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r, g, b, a); } }; //----------------------------------------------------------------------------- @@ -1865,6 +1954,11 @@ struct ImColor // Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. //----------------------------------------------------------------------------- +// The maximum line width to bake anti-aliased textures for. Build atlas with ImFontAtlasFlags_NoBakedLines to disable baking. +#ifndef IM_DRAWLIST_TEX_LINES_WIDTH_MAX +#define IM_DRAWLIST_TEX_LINES_WIDTH_MAX (63) +#endif + // ImDrawCallback: Draw callbacks for advanced uses [configurable type: override in imconfig.h] // NB: You most likely do NOT need to use draw callbacks just to create your own widget or customized UI rendering, // you can poke into the draw list for that! Draw callback may be useful for example to: @@ -1961,12 +2055,15 @@ enum ImDrawCornerFlags_ ImDrawCornerFlags_All = 0xF // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience }; +// Flags for ImDrawList. Those are set automatically by ImGui:: functions from ImGuiIO settings, and generally not manipulated directly. +// It is however possible to temporarily alter flags between calls to ImDrawList:: functions. enum ImDrawListFlags_ { - ImDrawListFlags_None = 0, - ImDrawListFlags_AntiAliasedLines = 1 << 0, // Lines are anti-aliased (*2 the number of triangles for 1.0f wide line, otherwise *3 the number of triangles) - ImDrawListFlags_AntiAliasedFill = 1 << 1, // Filled shapes have anti-aliased edges (*2 the number of vertices) - ImDrawListFlags_AllowVtxOffset = 1 << 2 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. + ImDrawListFlags_None = 0, + ImDrawListFlags_AntiAliasedLines = 1 << 0, // Enable anti-aliased lines/borders (*2 the number of triangles for 1.0f wide line or lines thin enough to be drawn using textures, otherwise *3 the number of triangles) + ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require back-end to render with bilinear filtering. + ImDrawListFlags_AntiAliasedFill = 1 << 2, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles). + ImDrawListFlags_AllowVtxOffset = 1 << 3 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. }; // Draw command list @@ -2012,6 +2109,7 @@ struct ImDrawList // Primitives // - For rectangular primitives, "p_min" and "p_max" represent the upper-left and lower-right corners. // - For circle primitives, use "num_segments == 0" to automatically calculate tessellation (preferred). + // In older versions (until Dear ImGui 1.77) the AddCircle functions defaulted to num_segments == 12. // In future versions we will use textures to provide cheaper and higher-quality circles. // Use AddNgon() and AddNgonFilled() functions if you need to guaranteed a specific number of sides. IMGUI_API void AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness = 1.0f); @@ -2022,8 +2120,8 @@ struct ImDrawList IMGUI_API void AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col); IMGUI_API void AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness = 1.0f); IMGUI_API void AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col); - IMGUI_API void AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments = 12, float thickness = 1.0f); - IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 12); + IMGUI_API void AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments = 0, float thickness = 1.0f); + IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 0); IMGUI_API void AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness = 1.0f); IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); @@ -2043,7 +2141,7 @@ struct ImDrawList // Stateful path API, add points then finish with PathFillConvex() or PathStroke() inline void PathClear() { _Path.Size = 0; } inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } - inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size-1], &pos, 8) != 0) _Path.push_back(pos); } + inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size - 1], &pos, 8) != 0) _Path.push_back(pos); } inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } // Note: Anti-aliased filling requires points to be in clockwise order. inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 10); @@ -2180,11 +2278,13 @@ struct ImFontAtlasCustomRect bool IsPacked() const { return X != 0xFFFF; } }; +// Flags for ImFontAtlas build enum ImFontAtlasFlags_ { ImFontAtlasFlags_None = 0, ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0, // Don't round the height to next power of two - ImFontAtlasFlags_NoMouseCursors = 1 << 1 // Don't build software mouse cursors into the atlas + ImFontAtlasFlags_NoMouseCursors = 1 << 1, // Don't build software mouse cursors into the atlas (save a little texture memory) + ImFontAtlasFlags_NoBakedLines = 1 << 2 // Don't build thick line textures into the atlas (save a little texture memory). The AntiAliasedLinesUseTex features uses them, otherwise they will be rendered using polygons (more expensive for CPU/GPU). }; // Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding: @@ -2258,7 +2358,7 @@ struct ImFontAtlas // Note: this API may be redesigned later in order to support multi-monitor varying DPI settings. IMGUI_API int AddCustomRectRegular(int width, int height); IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0)); - const ImFontAtlasCustomRect*GetCustomRectByIndex(int index) const { if (index < 0) return NULL; return &CustomRects[index]; } + ImFontAtlasCustomRect* GetCustomRectByIndex(int index) { IM_ASSERT(index >= 0); return &CustomRects[index]; } // [Internal] IMGUI_API void CalcCustomRectUV(const ImFontAtlasCustomRect* rect, ImVec2* out_uv_min, ImVec2* out_uv_max) const; @@ -2284,8 +2384,12 @@ struct ImFontAtlas ImVec2 TexUvWhitePixel; // Texture coordinates to a white pixel ImVector Fonts; // Hold all the fonts returned by AddFont*. Fonts[0] is the default font upon calling ImGui::NewFrame(), use ImGui::PushFont()/PopFont() to change the current font. ImVector CustomRects; // Rectangles for packing custom texture data into the atlas. - ImVector ConfigData; // Internal data - int CustomRectIds[1]; // Identifiers of custom texture rectangle used by ImFontAtlas/ImDrawList + ImVector ConfigData; // Configuration data + ImVec4 TexUvLines[IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1]; // UVs for baked anti-aliased lines + + // [Internal] Packing data + int PackIdMouseCursors; // Custom texture rectangle ID for white pixel and mouse cursors + int PackIdLines; // Custom texture rectangle ID for baked anti-aliased lines #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+ @@ -2302,11 +2406,10 @@ struct ImFont float FallbackAdvanceX; // 4 // out // = FallbackGlyph->AdvanceX float FontSize; // 4 // in // // Height of characters/line, set during loading (don't change after loading) - // Members: Hot ~36/48 bytes (for CalcTextSize + render loop) + // Members: Hot ~28/40 bytes (for CalcTextSize + render loop) ImVector IndexLookup; // 12-16 // out // // Sparse. Index glyphs by Unicode code-point. ImVector Glyphs; // 12-16 // out // // All glyphs. const ImFontGlyph* FallbackGlyph; // 4-8 // out // = FindGlyph(FontFallbackChar) - ImVec2 DisplayOffset; // 8 // in // = (0,0) // Offset font rendering by xx pixels // Members: Cold ~32/40 bytes ImFontAtlas* ContainerAtlas; // 4-8 // out // // What we has been loaded into @@ -2340,7 +2443,7 @@ struct ImFont IMGUI_API void BuildLookupTable(); IMGUI_API void ClearOutputData(); IMGUI_API void GrowIndex(int new_size); - IMGUI_API void AddGlyph(ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x); + IMGUI_API void AddGlyph(const ImFontConfig* src_cfg, ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x); IMGUI_API void AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built. IMGUI_API void SetGlyphVisible(ImWchar c, bool visible); IMGUI_API void SetFallbackChar(ImWchar c); diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 33d069dc..9099116c 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (demo code) // Help: @@ -228,8 +228,8 @@ void ImGui::ShowDemoWindow(bool* p_open) IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); // Examples Apps (accessible from the "Examples" menu) - static bool show_app_documents = false; static bool show_app_main_menu_bar = false; + static bool show_app_documents = false; static bool show_app_console = false; static bool show_app_log = false; static bool show_app_layout = false; @@ -241,8 +241,9 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool show_app_window_titles = false; static bool show_app_custom_rendering = false; - if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); + if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); + if (show_app_console) ShowExampleAppConsole(&show_app_console); if (show_app_log) ShowExampleAppLog(&show_app_log); if (show_app_layout) ShowExampleAppLayout(&show_app_layout); @@ -378,12 +379,12 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Configuration##2")) { - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); ImGui::SameLine(); HelpMarker("Required back-end to feed in gamepad inputs in io.NavInputs[] and set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); - ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); + ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); // The "NoMouse" option above can get us stuck with a disable mouse! Provide an alternative way to fix it: if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) @@ -396,7 +397,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse; } - ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", (unsigned int *)&io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); + ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); ImGui::SameLine(); HelpMarker("Instruct back-end to not alter mouse cursor shape and visibility."); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); ImGui::SameLine(); HelpMarker("Set to false to disable blinking cursor, for users who consider it distracting"); @@ -405,6 +406,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + ImGui::Text("Also see Style->Rendering for rendering options."); ImGui::TreePop(); ImGui::Separator(); } @@ -417,10 +419,10 @@ void ImGui::ShowDemoWindow(bool* p_open) // Make a local copy to avoid modifying actual back-end flags. ImGuiBackendFlags backend_flags = io.BackendFlags; - ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasGamepad); - ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasMouseCursors); - ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int *)&backend_flags, ImGuiBackendFlags_HasSetMousePos); - ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", (unsigned int *)&backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); + ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasGamepad); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasMouseCursors); + ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasSetMousePos); + ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", (unsigned int*)&backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); ImGui::TreePop(); ImGui::Separator(); } @@ -507,9 +509,9 @@ static void ShowDemoWindowWidgets() if (i > 0) ImGui::SameLine(); ImGui::PushID(i); - ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(i/7.0f, 0.6f, 0.6f)); - ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(i/7.0f, 0.7f, 0.7f)); - ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i/7.0f, 0.8f, 0.8f)); + ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(i / 7.0f, 0.6f, 0.6f)); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(i / 7.0f, 0.7f, 0.7f)); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.8f, 0.8f)); ImGui::Button("Click"); ImGui::PopStyleColor(3); ImGui::PopID(); @@ -558,7 +560,9 @@ static void ShowDemoWindowWidgets() const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; static int item_current = 0; ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); - ImGui::SameLine(); HelpMarker("Refer to the \"Combo\" section below for an explanation of the full BeginCombo/EndCombo API, and demonstration of various flags.\n"); + ImGui::SameLine(); HelpMarker( + "Refer to the \"Combo\" section below for an explanation of the full BeginCombo/EndCombo API, " + "and demonstration of various flags.\n"); } { @@ -613,21 +617,21 @@ static void ShowDemoWindowWidgets() "Hold SHIFT/ALT for faster/slower edit.\n" "Double-click or CTRL+click to input value."); - ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%"); + ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%", ImGuiSliderFlags_ClampOnInput); - static float f1=1.00f, f2=0.0067f; + static float f1 = 1.00f, f2 = 0.0067f; ImGui::DragFloat("drag float", &f1, 0.005f); ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns"); } { - static int i1=0; + static int i1 = 0; ImGui::SliderInt("slider int", &i1, -1, 3); ImGui::SameLine(); HelpMarker("CTRL+click to input value."); - static float f1=0.123f, f2=0.0f; + static float f1 = 0.123f, f2 = 0.0f; ImGui::SliderFloat("slider float", &f1, 0.0f, 1.0f, "ratio = %.3f"); - ImGui::SliderFloat("slider float (curve)", &f2, -10.0f, 10.0f, "%.4f", 2.0f); + ImGui::SliderFloat("slider float (log)", &f2, -10.0f, 10.0f, "%.4f", ImGuiSliderFlags_Logarithmic); static float angle = 0.0f; ImGui::SliderAngle("slider angle", &angle); @@ -644,8 +648,8 @@ static void ShowDemoWindowWidgets() } { - static float col1[3] = { 1.0f,0.0f,0.2f }; - static float col2[4] = { 0.4f,0.7f,0.0f,0.5f }; + static float col1[3] = { 1.0f, 0.0f, 0.2f }; + static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::ColorEdit3("color 1", col1); ImGui::SameLine(); HelpMarker( "Click on the colored square to open a color picker.\n" @@ -822,8 +826,8 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Colored Text")) { // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility. - ImGui::TextColored(ImVec4(1.0f,0.0f,1.0f,1.0f), "Pink"); - ImGui::TextColored(ImVec4(1.0f,1.0f,0.0f,1.0f), "Yellow"); + ImGui::TextColored(ImVec4(1.0f, 0.0f, 1.0f, 1.0f), "Pink"); + ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "Yellow"); ImGui::TextDisabled("Disabled"); ImGui::SameLine(); HelpMarker("The TextDisabled color is stored in ImGuiStyle."); ImGui::TreePop(); @@ -832,7 +836,9 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Word Wrapping")) { // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility. - ImGui::TextWrapped("This text should automatically wrap on the edge of the window. The current implementation for text wrapping follows simple rules suitable for English and possibly other languages."); + ImGui::TextWrapped( + "This text should automatically wrap on the edge of the window. The current implementation " + "for text wrapping follows simple rules suitable for English and possibly other languages."); ImGui::Spacing(); static float wrap_width = 200.0f; @@ -848,7 +854,7 @@ static void ShowDemoWindowWidgets() ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + wrap_width); if (n == 0) ImGui::Text("The lazy dog is a good dog. This paragraph should fit within %.0f pixels. Testing a 1 character word. The quick brown fox jumps over the lazy dog.", wrap_width); - if (n == 1) + else ImGui::Text("aaaaaaaa bbbbbbbb, c cccccccc,dddddddd. d eeeeeeee ffffffff. gggggggg!hhhhhhhh"); // Draw actual text bounding box, following by marker of our expected limit (should not overlap!) @@ -889,7 +895,10 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Images")) { ImGuiIO& io = ImGui::GetIO(); - ImGui::TextWrapped("Below we are displaying the font texture (which is the only texture we have access to in this demo). Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. Hover the texture for a zoomed view!"); + ImGui::TextWrapped( + "Below we are displaying the font texture (which is the only texture we have access to in this demo). " + "Use the 'ImTextureID' type as storage to pass pointers or identifier to your own texture data. " + "Hover the texture for a zoomed view!"); // Below we are displaying the font texture because it is the only texture we have access to inside the demo! // Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that @@ -944,7 +953,7 @@ static void ShowDemoWindowWidgets() int frame_padding = -1 + i; // -1 == uses default padding (style.FramePadding) ImVec2 size = ImVec2(32.0f, 32.0f); // Size of the image we want to make visible ImVec2 uv0 = ImVec2(0.0f, 0.0f); // UV coordinates for lower-left - ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32 / my_tex_h); // UV coordinates for (32,32) in our texture + ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32.0f / my_tex_h);// UV coordinates for (32,32) in our texture ImVec4 bg_col = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); // Black background ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint if (ImGui::ImageButton(my_tex_id, size, uv0, uv1, frame_padding, bg_col, tint_col)) @@ -973,7 +982,7 @@ static void ShowDemoWindowWidgets() // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; static int item_current_idx = 0; // Here our selection data is an index. - const char* combo_label = items[item_current_idx]; // Label to preview before opening the combo (technically could be anything)( + const char* combo_label = items[item_current_idx]; // Label to preview before opening the combo (technically it could be anything) if (ImGui::BeginCombo("combo 1", combo_label, flags)) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) @@ -998,9 +1007,9 @@ static void ShowDemoWindowWidgets() ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items)); // Simplified one-liner Combo() using an accessor function - struct FuncHolder { static bool ItemGetter(void* data, int idx, const char** out_str) { *out_str = ((const char**)data)[idx]; return true; } }; + struct Funcs { static bool ItemGetter(void* data, int n, const char** out_str) { *out_str = ((const char**)data)[n]; return true; } }; static int item_current_4 = 0; - ImGui::Combo("combo 4 (function)", &item_current_4, &FuncHolder::ItemGetter, items, IM_ARRAYSIZE(items)); + ImGui::Combo("combo 4 (function)", &item_current_4, &Funcs::ItemGetter, items, IM_ARRAYSIZE(items)); ImGui::TreePop(); } @@ -1079,11 +1088,11 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("Grid")) { - static int selected[4*4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; - for (int i = 0; i < 4*4; i++) + static int selected[4 * 4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; + for (int i = 0; i < 4 * 4; i++) { ImGui::PushID(i); - if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50,50))) + if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50, 50))) { // Toggle selected[i] = !selected[i]; @@ -1118,7 +1127,7 @@ static void ShowDemoWindowWidgets() sprintf(name, "(%.1f,%.1f)", alignment.x, alignment.y); if (x > 0) ImGui::SameLine(); ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, alignment); - ImGui::Selectable(name, &selected[3*y+x], ImGuiSelectableFlags_None, ImVec2(80,80)); + ImGui::Selectable(name, &selected[3 * y + x], ImGuiSelectableFlags_None, ImVec2(80, 80)); ImGui::PopStyleVar(); } } @@ -1175,8 +1184,11 @@ static void ShowDemoWindowWidgets() static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase); static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank); static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); + ImGui::TreePop(); + } - ImGui::Text("Password input"); + if (ImGui::TreeNode("Password Input")) + { static char password[64] = "password123"; ImGui::InputText("password", password, IM_ARRAYSIZE(password), ImGuiInputTextFlags_Password); ImGui::SameLine(); HelpMarker("Display all characters as '*'.\nDisable clipboard cut and copy.\nDisable logging.\n"); @@ -1185,6 +1197,62 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + if (ImGui::TreeNode("Completion, History, Edit Callbacks")) + { + struct Funcs + { + static int MyCallback(ImGuiInputTextCallbackData* data) + { + if (data->EventFlag == ImGuiInputTextFlags_CallbackCompletion) + { + data->InsertChars(data->CursorPos, ".."); + } + else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory) + { + if (data->EventKey == ImGuiKey_UpArrow) + { + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, "Pressed Up!"); + data->SelectAll(); + } + else if (data->EventKey == ImGuiKey_DownArrow) + { + data->DeleteChars(0, data->BufTextLen); + data->InsertChars(0, "Pressed Down!"); + data->SelectAll(); + } + } + else if (data->EventFlag == ImGuiInputTextFlags_CallbackEdit) + { + // Toggle casing of first character + char c = data->Buf[0]; + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) data->Buf[0] ^= 32; + data->BufDirty = true; + + // Increment a counter + int* p_int = (int*)data->UserData; + *p_int = *p_int + 1; + } + return 0; + } + }; + static char buf1[64]; + ImGui::InputText("Completion", buf1, 64, ImGuiInputTextFlags_CallbackCompletion, Funcs::MyCallback); + ImGui::SameLine(); HelpMarker("Here we append \"..\" each time Tab is pressed. See 'Examples>Console' for a more meaningful demonstration of using this callback."); + + static char buf2[64]; + ImGui::InputText("History", buf2, 64, ImGuiInputTextFlags_CallbackHistory, Funcs::MyCallback); + ImGui::SameLine(); HelpMarker("Here we replace and select text each time Up/Down are pressed. See 'Examples>Console' for a more meaningful demonstration of using this callback."); + + static char buf3[64]; + static int edit_count = 0; + ImGui::InputText("Edit", buf3, 64, ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count); + ImGui::SameLine(); HelpMarker("Here we toggle the casing of the first character on every edits + count edits."); + ImGui::SameLine(); ImGui::Text("(%d)", edit_count); + + ImGui::TreePop(); + } + if (ImGui::TreeNode("Resize Callback")) { // To wire InputText() with std::string or any other custom string type, @@ -1241,7 +1309,7 @@ static void ShowDemoWindowWidgets() static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); - // Create a dummy array of contiguous float values to plot + // Fill an array of contiguous float values to plot // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float // and the sizeof() of your structure in the "stride" parameter. static float values[90] = {}; @@ -1249,13 +1317,13 @@ static void ShowDemoWindowWidgets() static double refresh_time = 0.0; if (!animate || refresh_time == 0.0) refresh_time = ImGui::GetTime(); - while (refresh_time < ImGui::GetTime()) // Create dummy data at fixed 60 Hz rate for the demo + while (refresh_time < ImGui::GetTime()) // Create data at fixed 60 Hz rate for the demo { static float phase = 0.0f; values[values_offset] = cosf(phase); - values_offset = (values_offset+1) % IM_ARRAYSIZE(values); - phase += 0.10f*values_offset; - refresh_time += 1.0f/60.0f; + values_offset = (values_offset + 1) % IM_ARRAYSIZE(values); + phase += 0.10f * values_offset; + refresh_time += 1.0f / 60.0f; } // Plots can display overlay texts @@ -1286,8 +1354,8 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); ImGui::SliderInt("Sample count", &display_count, 1, 400); float (*func)(void*, int) = (func_type == 0) ? Funcs::Sin : Funcs::Saw; - ImGui::PlotLines("Lines", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0,80)); - ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0,80)); + ImGui::PlotLines("Lines", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); + ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); ImGui::Separator(); // Animate a simple progress bar @@ -1301,20 +1369,20 @@ static void ShowDemoWindowWidgets() // Typically we would use ImVec2(-1.0f,0.0f) or ImVec2(-FLT_MIN,0.0f) to use all available width, // or ImVec2(width,0.0f) for a specified width. ImVec2(0.0f,0.0f) uses ItemWidth. - ImGui::ProgressBar(progress, ImVec2(0.0f,0.0f)); + ImGui::ProgressBar(progress, ImVec2(0.0f, 0.0f)); ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); ImGui::Text("Progress Bar"); float progress_saturated = IM_CLAMP(progress, 0.0f, 1.0f); char buf[32]; - sprintf(buf, "%d/%d", (int)(progress_saturated*1753), 1753); - ImGui::ProgressBar(progress, ImVec2(0.f,0.f), buf); + sprintf(buf, "%d/%d", (int)(progress_saturated * 1753), 1753); + ImGui::ProgressBar(progress, ImVec2(0.f, 0.f), buf); ImGui::TreePop(); } if (ImGui::TreeNode("Color/Picker Widgets")) { - static ImVec4 color = ImVec4(114.0f/255.0f, 144.0f/255.0f, 154.0f/255.0f, 200.0f/255.0f); + static ImVec4 color = ImVec4(114.0f / 255.0f, 144.0f / 255.0f, 154.0f / 255.0f, 200.0f / 255.0f); static bool alpha_preview = true; static bool alpha_half_preview = false; @@ -1349,7 +1417,7 @@ static void ShowDemoWindowWidgets() ImGui::Text("Color button with Custom Picker Popup:"); - // Generate a dummy default palette. The palette will persist and can be edited. + // Generate a default palette. The palette will persist and can be edited. static bool saved_palette_init = true; static ImVec4 saved_palette[32] = {}; if (saved_palette_init) @@ -1381,9 +1449,9 @@ static void ShowDemoWindowWidgets() ImGui::BeginGroup(); // Lock X position ImGui::Text("Current"); - ImGui::ColorButton("##current", color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60,40)); + ImGui::ColorButton("##current", color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60, 40)); ImGui::Text("Previous"); - if (ImGui::ColorButton("##previous", backup_color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60,40))) + if (ImGui::ColorButton("##previous", backup_color, ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_AlphaPreviewHalf, ImVec2(60, 40))) color = backup_color; ImGui::Separator(); ImGui::Text("Palette"); @@ -1394,7 +1462,7 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y); ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoTooltip; - if (ImGui::ColorButton("##palette", saved_palette[n], palette_button_flags, ImVec2(20,20))) + if (ImGui::ColorButton("##palette", saved_palette[n], palette_button_flags, ImVec2(20, 20))) color = ImVec4(saved_palette[n].x, saved_palette[n].y, saved_palette[n].z, color.w); // Preserve alpha! // Allow user to drop colors into each palette entry. Note that ColorButton() is already a @@ -1417,14 +1485,14 @@ static void ShowDemoWindowWidgets() ImGui::Text("Color button only:"); static bool no_border = false; ImGui::Checkbox("ImGuiColorEditFlags_NoBorder", &no_border); - ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80,80)); + ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80, 80)); ImGui::Text("Color picker:"); static bool alpha = true; static bool alpha_bar = true; static bool side_preview = true; static bool ref_color = false; - static ImVec4 ref_color_v(1.0f,0.0f,1.0f,0.5f); + static ImVec4 ref_color_v(1.0f, 0.0f, 1.0f, 0.5f); static int display_mode = 0; static int picker_mode = 0; ImGui::Checkbox("With Alpha", &alpha); @@ -1486,11 +1554,45 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + if (ImGui::TreeNode("Drag/Slider Flags")) + { + // Demonstrate using advanced flags for DragXXX and SliderXXX functions. Note that the flags are the same! + static ImGuiSliderFlags flags = ImGuiSliderFlags_None; + ImGui::CheckboxFlags("ImGuiSliderFlags_ClampOnInput", (unsigned int*)&flags, ImGuiSliderFlags_ClampOnInput); + ImGui::SameLine(); HelpMarker("Always clamp value to min/max bounds (if any) when input manually with CTRL+Click."); + ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic", (unsigned int*)&flags, ImGuiSliderFlags_Logarithmic); + ImGui::SameLine(); HelpMarker("Enable logarithmic editing (more precision for small values)."); + ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", (unsigned int*)&flags, ImGuiSliderFlags_NoRoundToFormat); + ImGui::SameLine(); HelpMarker("Disable rounding underlying value to match precision of the format string (e.g. %.3f values are rounded to those 3 digits)."); + ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput", (unsigned int*)&flags, ImGuiSliderFlags_NoInput); + ImGui::SameLine(); HelpMarker("Disable CTRL+Click or Enter key allowing to input text directly into the widget."); + + // Drags + static float drag_f = 0.5f; + static int drag_i = 50; + ImGui::Text("Underlying float value: %f", drag_f); + ImGui::DragFloat("DragFloat (0 -> 1)", &drag_f, 0.005f, 0.0f, 1.0f, "%.3f", flags); + ImGui::DragFloat("DragFloat (0 -> +inf)", &drag_f, 0.005f, 0.0f, FLT_MAX, "%.3f", flags); + ImGui::DragFloat("DragFloat (-inf -> 1)", &drag_f, 0.005f, -FLT_MAX, 1.0f, "%.3f", flags); + ImGui::DragFloat("DragFloat (-inf -> +inf)", &drag_f, 0.005f, -FLT_MAX, +FLT_MAX, "%.3f", flags); + ImGui::DragInt("DragInt (0 -> 100)", &drag_i, 0.5f, 0, 100, "%d", flags); + + // Sliders + static float slider_f = 0.5f; + static int slider_i = 50; + ImGui::Text("Underlying float value: %f", slider_f); + ImGui::SliderFloat("SliderFloat (0 -> 1)", &slider_f, 0.0f, 1.0f, "%.3f", flags); + ImGui::SliderInt("SliderInt (0 -> 100)", &slider_i, 0, 100, "%d", flags); + + ImGui::TreePop(); + } + if (ImGui::TreeNode("Range Widgets")) { static float begin = 10, end = 90; static int begin_i = 100, end_i = 1000; - ImGui::DragFloatRange2("range", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%"); + ImGui::DragFloatRange2("range float", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%", ImGuiSliderFlags_ClampOnInput); + ImGui::DragIntRange2("range int", &begin_i, &end_i, 5, 0, 1000, "Min: %d units", "Max: %d units"); ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %d units", "Max: %d units"); ImGui::TreePop(); } @@ -1545,7 +1647,10 @@ static void ShowDemoWindowWidgets() const float drag_speed = 0.2f; static bool drag_clamp = false; ImGui::Text("Drags:"); - ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); HelpMarker("As with every widgets in dear imgui, we never modify values unless there is a user interaction.\nYou can override the clamping limits by using CTRL+Click to input a value."); + ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); + ImGui::SameLine(); HelpMarker( + "As with every widgets in dear imgui, we never modify values unless there is a user interaction.\n" + "You can override the clamping limits by using CTRL+Click to input a value."); ImGui::DragScalar("drag s8", ImGuiDataType_S8, &s8_v, drag_speed, drag_clamp ? &s8_zero : NULL, drag_clamp ? &s8_fifty : NULL); ImGui::DragScalar("drag u8", ImGuiDataType_U8, &u8_v, drag_speed, drag_clamp ? &u8_zero : NULL, drag_clamp ? &u8_fifty : NULL, "%u ms"); ImGui::DragScalar("drag s16", ImGuiDataType_S16, &s16_v, drag_speed, drag_clamp ? &s16_zero : NULL, drag_clamp ? &s16_fifty : NULL); @@ -1554,34 +1659,42 @@ static void ShowDemoWindowWidgets() ImGui::DragScalar("drag u32", ImGuiDataType_U32, &u32_v, drag_speed, drag_clamp ? &u32_zero : NULL, drag_clamp ? &u32_fifty : NULL, "%u ms"); ImGui::DragScalar("drag s64", ImGuiDataType_S64, &s64_v, drag_speed, drag_clamp ? &s64_zero : NULL, drag_clamp ? &s64_fifty : NULL); ImGui::DragScalar("drag u64", ImGuiDataType_U64, &u64_v, drag_speed, drag_clamp ? &u64_zero : NULL, drag_clamp ? &u64_fifty : NULL); - ImGui::DragScalar("drag float", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", 1.0f); - ImGui::DragScalar("drag float ^2", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", 2.0f); ImGui::SameLine(); HelpMarker("You can use the 'power' parameter to increase tweaking precision on one side of the range."); - ImGui::DragScalar("drag double", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, NULL, "%.10f grams", 1.0f); - ImGui::DragScalar("drag double ^2", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", 2.0f); + ImGui::DragScalar("drag float", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f"); + ImGui::DragScalar("drag float log", ImGuiDataType_Float, &f32_v, 0.005f, &f32_zero, &f32_one, "%f", ImGuiSliderFlags_Logarithmic); + ImGui::DragScalar("drag double", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, NULL, "%.10f grams"); + ImGui::DragScalar("drag double log",ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", ImGuiSliderFlags_Logarithmic); ImGui::Text("Sliders"); - ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d"); - ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u"); - ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d"); - ImGui::SliderScalar("slider u16 full", ImGuiDataType_U16, &u16_v, &u16_min, &u16_max, "%u"); - ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d"); - ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d"); - ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d"); - ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); - ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); - ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); - ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%I64d"); - ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%I64d"); - ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%I64d"); - ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%I64u ms"); - ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%I64u ms"); - ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%I64u ms"); - ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one); - ImGui::SliderScalar("slider float low^2", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", 2.0f); - ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e"); - ImGui::SliderScalar("slider double low", ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f grams", 1.0f); - ImGui::SliderScalar("slider double low^2",ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f", 2.0f); - ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams", 1.0f); + ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d"); + ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u"); + ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d"); + ImGui::SliderScalar("slider u16 full", ImGuiDataType_U16, &u16_v, &u16_min, &u16_max, "%u"); + ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d"); + ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d"); + ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d"); + ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); + ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); + ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); + ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%I64d"); + ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%I64d"); + ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%I64d"); + ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%I64u ms"); + ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%I64u ms"); + ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%I64u ms"); + ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one); + ImGui::SliderScalar("slider float low log", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", ImGuiSliderFlags_Logarithmic); + ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e"); + ImGui::SliderScalar("slider double low", ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f grams"); + ImGui::SliderScalar("slider double low log",ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f", ImGuiSliderFlags_Logarithmic); + ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams"); + + ImGui::Text("Sliders (reverse)"); + ImGui::SliderScalar("slider s8 reverse", ImGuiDataType_S8, &s8_v, &s8_max, &s8_min, "%d"); + ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u"); + ImGui::SliderScalar("slider s32 reverse", ImGuiDataType_S32, &s32_v, &s32_fifty, &s32_zero, "%d"); + ImGui::SliderScalar("slider u32 reverse", ImGuiDataType_U32, &u32_v, &u32_fifty, &u32_zero, "%u"); + ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%I64d"); + ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%I64u ms"); static bool inputs_step = true; ImGui::Text("Inputs"); @@ -1639,7 +1752,7 @@ static void ShowDemoWindowWidgets() ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(spacing, spacing)); static int int_value = 0; - ImGui::VSliderInt("##int", ImVec2(18,160), &int_value, 0, 5); + ImGui::VSliderInt("##int", ImVec2(18, 160), &int_value, 0, 5); ImGui::SameLine(); static float values[7] = { 0.0f, 0.60f, 0.35f, 0.9f, 0.70f, 0.20f, 0.0f }; @@ -1648,11 +1761,11 @@ static void ShowDemoWindowWidgets() { if (i > 0) ImGui::SameLine(); ImGui::PushID(i); - ImGui::PushStyleColor(ImGuiCol_FrameBg, (ImVec4)ImColor::HSV(i/7.0f, 0.5f, 0.5f)); - ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, (ImVec4)ImColor::HSV(i/7.0f, 0.6f, 0.5f)); - ImGui::PushStyleColor(ImGuiCol_FrameBgActive, (ImVec4)ImColor::HSV(i/7.0f, 0.7f, 0.5f)); - ImGui::PushStyleColor(ImGuiCol_SliderGrab, (ImVec4)ImColor::HSV(i/7.0f, 0.9f, 0.9f)); - ImGui::VSliderFloat("##v", ImVec2(18,160), &values[i], 0.0f, 1.0f, ""); + ImGui::PushStyleColor(ImGuiCol_FrameBg, (ImVec4)ImColor::HSV(i / 7.0f, 0.5f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, (ImVec4)ImColor::HSV(i / 7.0f, 0.6f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_FrameBgActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.7f, 0.5f)); + ImGui::PushStyleColor(ImGuiCol_SliderGrab, (ImVec4)ImColor::HSV(i / 7.0f, 0.9f, 0.9f)); + ImGui::VSliderFloat("##v", ImVec2(18, 160), &values[i], 0.0f, 1.0f, ""); if (ImGui::IsItemActive() || ImGui::IsItemHovered()) ImGui::SetTooltip("%.3f", values[i]); ImGui::PopStyleColor(4); @@ -1671,7 +1784,7 @@ static void ShowDemoWindowWidgets() ImGui::BeginGroup(); for (int ny = 0; ny < rows; ny++) { - ImGui::PushID(nx*rows+ny); + ImGui::PushID(nx * rows + ny); ImGui::VSliderFloat("##v", small_slider_size, &values2[nx], 0.0f, 1.0f, ""); if (ImGui::IsItemActive() || ImGui::IsItemHovered()) ImGui::SetTooltip("%.3f", values2[nx]); @@ -1688,7 +1801,7 @@ static void ShowDemoWindowWidgets() if (i > 0) ImGui::SameLine(); ImGui::PushID(i); ImGui::PushStyleVar(ImGuiStyleVar_GrabMinSize, 40); - ImGui::VSliderFloat("##v", ImVec2(40,160), &values[i], 0.0f, 1.0f, "%.2f\nsec"); + ImGui::VSliderFloat("##v", ImVec2(40, 160), &values[i], 0.0f, 1.0f, "%.2f\nsec"); ImGui::PopStyleVar(); ImGui::PopID(); } @@ -1725,7 +1838,7 @@ static void ShowDemoWindowWidgets() if (ImGui::RadioButton("Copy", mode == Mode_Copy)) { mode = Mode_Copy; } ImGui::SameLine(); if (ImGui::RadioButton("Move", mode == Mode_Move)) { mode = Mode_Move; } ImGui::SameLine(); if (ImGui::RadioButton("Swap", mode == Mode_Swap)) { mode = Mode_Swap; } - const char* names[9] = + static const char* names[9] = { "Bobby", "Beatrice", "Betty", "Brianna", "Barry", "Bernard", @@ -1736,7 +1849,7 @@ static void ShowDemoWindowWidgets() ImGui::PushID(n); if ((n % 3) != 0) ImGui::SameLine(); - ImGui::Button(names[n], ImVec2(60,60)); + ImGui::Button(names[n], ImVec2(60, 60)); // Our buttons are both drag sources and drag targets here! if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) @@ -1929,8 +2042,8 @@ static void ShowDemoWindowWidgets() if (embed_all_inside_a_child_window) ImGui::EndChild(); - static char dummy_str[] = "This is a dummy field to be able to tab-out of the widgets above."; - ImGui::InputText("dummy", dummy_str, IM_ARRAYSIZE(dummy_str), ImGuiInputTextFlags_ReadOnly); + static char unused_str[] = "This widget is only here to be able to tab-out of the widgets above."; + ImGui::InputText("unused", unused_str, IM_ARRAYSIZE(unused_str), ImGuiInputTextFlags_ReadOnly); // Calling IsItemHovered() after begin returns the hovered status of the title bar. // This is useful in particular if you want to create a context menu associated to the title bar of a window. @@ -1957,7 +2070,7 @@ static void ShowDemoWindowWidgets() static void ShowDemoWindowLayout() { - if (!ImGui::CollapsingHeader("Layout")) + if (!ImGui::CollapsingHeader("Layout & Scrolling")) return; if (ImGui::TreeNode("Child windows")) @@ -1968,12 +2081,6 @@ static void ShowDemoWindowLayout() ImGui::Checkbox("Disable Mouse Wheel", &disable_mouse_wheel); ImGui::Checkbox("Disable Menu", &disable_menu); - static int line = 50; - bool goto_line = ImGui::Button("Goto"); - ImGui::SameLine(); - ImGui::SetNextItemWidth(100); - goto_line |= ImGui::InputInt("##Line", &line, 0, 0, ImGuiInputTextFlags_EnterReturnsTrue); - // Child 1: no border, enable horizontal scrollbar { ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; @@ -1981,13 +2088,7 @@ static void ShowDemoWindowLayout() window_flags |= ImGuiWindowFlags_NoScrollWithMouse; ImGui::BeginChild("ChildL", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); for (int i = 0; i < 100; i++) - { ImGui::Text("%04d: scrollable region", i); - if (goto_line && line == i) - ImGui::SetScrollHereY(); - } - if (goto_line && line >= 100) - ImGui::SetScrollHereY(); ImGui::EndChild(); } @@ -2033,15 +2134,21 @@ static void ShowDemoWindowLayout() // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from // the POV of the parent window). See 'Demo->Querying Status (Active/Focused/Hovered etc.)' for details. { - ImGui::SetCursorPosX(ImGui::GetCursorPosX() + 10); + static int offset_x = 0; + ImGui::SetNextItemWidth(100); + ImGui::DragInt("Offset X", &offset_x, 1.0f, -1000, 1000); + + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (float)offset_x); ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); ImGui::BeginChild("Red", ImVec2(200, 100), true, ImGuiWindowFlags_None); for (int n = 0; n < 50; n++) ImGui::Text("Some test %d", n); ImGui::EndChild(); + bool child_is_hovered = ImGui::IsItemHovered(); ImVec2 child_rect_min = ImGui::GetItemRectMin(); ImVec2 child_rect_max = ImGui::GetItemRectMax(); ImGui::PopStyleColor(); + ImGui::Text("Hovered: %d", child_is_hovered); ImGui::Text("Rect of child window is: (%.0f,%.0f) (%.0f,%.0f)", child_rect_min.x, child_rect_min.y, child_rect_max.x, child_rect_max.y); } @@ -2379,7 +2486,7 @@ static void ShowDemoWindowLayout() ImGui::SameLine(0.0f, spacing); if (ImGui::TreeNode("Node##1")) { - // Dummy tree data + // Placeholder tree data for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); @@ -2395,7 +2502,7 @@ static void ShowDemoWindowLayout() ImGui::SameLine(0.0f, spacing); ImGui::Button("Button##2"); if (node_open) { - // Dummy tree data + // Placeholder tree data for (int i = 0; i < 6; i++) ImGui::BulletText("Item %d..", i); ImGui::TreePop(); @@ -2427,8 +2534,6 @@ static void ShowDemoWindowLayout() static float scroll_to_pos_px = 200.0f; ImGui::Checkbox("Decoration", &enable_extra_decorations); - ImGui::SameLine(); - HelpMarker("We expose this for testing because scrolling sometimes had issues with window decoration such as menu-bars."); ImGui::Checkbox("Track", &enable_track); ImGui::PushItemWidth(100); @@ -2495,11 +2600,9 @@ static void ShowDemoWindowLayout() ImGui::Spacing(); HelpMarker( "Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n" - "Using the \"Scroll To Pos\" button above will make the discontinuity at edges visible: " - "scrolling to the top/bottom/left/right-most item will add an additional WindowPadding to reflect " - "on reaching the edge of the list.\n\nBecause the clipping rectangle of most window hides half " - "worth of WindowPadding on the left/right, using SetScrollFromPosX(+1) will usually result in " - "clipped text whereas the equivalent SetScrollFromPosY(+1) wouldn't."); + "Because the clipping rectangle of most window hides half worth of WindowPadding on the " + "left/right, using SetScrollFromPosX(+1) will usually result in clipped text whereas the " + "equivalent SetScrollFromPosY(+1) wouldn't."); ImGui::PushID("##HorizontalScrolling"); for (int i = 0; i < 5; i++) { @@ -2560,8 +2663,8 @@ static void ShowDemoWindowLayout() ImGui::PushID(n + line * 1000); char num_buf[16]; sprintf(num_buf, "%d", n); - const char* label = (!(n%15)) ? "FizzBuzz" : (!(n%3)) ? "Fizz" : (!(n%5)) ? "Buzz" : num_buf; - float hue = n*0.05f; + const char* label = (!(n % 15)) ? "FizzBuzz" : (!(n % 3)) ? "Fizz" : (!(n % 5)) ? "Buzz" : num_buf; + float hue = n * 0.05f; ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(hue, 0.6f, 0.6f)); ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(hue, 0.7f, 0.7f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(hue, 0.8f, 0.8f)); @@ -2689,23 +2792,66 @@ static void ShowDemoWindowLayout() if (ImGui::TreeNode("Clipping")) { - static ImVec2 size(100, 100), offset(50, 20); - ImGui::TextWrapped( - "On a per-widget basis we are occasionally clipping text CPU-side if it won't fit in its frame. " - "Otherwise we are doing coarser clipping + passing a scissor rectangle to the renderer. " - "The system is designed to try minimizing both execution and CPU/GPU rendering cost."); + static ImVec2 size(100.0f, 100.0f); + static ImVec2 offset(30.0f, 30.0f); ImGui::DragFloat2("size", (float*)&size, 0.5f, 1.0f, 200.0f, "%.0f"); - ImGui::TextWrapped("(Click and drag)"); - ImVec2 pos = ImGui::GetCursorScreenPos(); - ImVec4 clip_rect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); - ImGui::InvisibleButton("##dummy", size); - if (ImGui::IsItemActive() && ImGui::IsMouseDragging(0)) + ImGui::TextWrapped("(Click and drag to scroll)"); + + for (int n = 0; n < 3; n++) { - offset.x += ImGui::GetIO().MouseDelta.x; - offset.y += ImGui::GetIO().MouseDelta.y; + if (n > 0) + ImGui::SameLine(); + ImGui::PushID(n); + ImGui::BeginGroup(); // Lock X position + + ImGui::InvisibleButton("##empty", size); + if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left)) + { + offset.x += ImGui::GetIO().MouseDelta.x; + offset.y += ImGui::GetIO().MouseDelta.y; + } + const ImVec2 p0 = ImGui::GetItemRectMin(); + const ImVec2 p1 = ImGui::GetItemRectMax(); + const char* text_str = "Line 1 hello\nLine 2 clip me!"; + const ImVec2 text_pos = ImVec2(p0.x + offset.x, p0.y + offset.y); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + + switch (n) + { + case 0: + HelpMarker( + "Using ImGui::PushClipRect():\n" + "Will alter ImGui hit-testing logic + ImDrawList rendering.\n" + "(use this if you want your clipping rectangle to affect interactions)"); + ImGui::PushClipRect(p0, p1, true); + draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); + draw_list->AddText(text_pos, IM_COL32_WHITE, text_str); + ImGui::PopClipRect(); + break; + case 1: + HelpMarker( + "Using ImDrawList::PushClipRect():\n" + "Will alter ImDrawList rendering only.\n" + "(use this as a shortcut if you are only using ImDrawList calls)"); + draw_list->PushClipRect(p0, p1, true); + draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); + draw_list->AddText(text_pos, IM_COL32_WHITE, text_str); + draw_list->PopClipRect(); + break; + case 2: + HelpMarker( + "Using ImDrawList::AddText() with a fine ClipRect:\n" + "Will alter only this specific ImDrawList::AddText() rendering.\n" + "(this is often used internally to avoid altering the clipping rectangle and minimize draw calls)"); + ImVec4 clip_rect(p0.x, p0.y, p1.x, p1.y); // AddText() takes a ImVec4* here so let's convert. + draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); + draw_list->AddText(ImGui::GetFont(), ImGui::GetFontSize(), text_pos, IM_COL32_WHITE, text_str, NULL, 0.0f, &clip_rect); + break; + } + ImGui::EndGroup(); + ImGui::PopID(); } - ImGui::GetWindowDrawList()->AddRectFilled(pos, ImVec2(pos.x + size.x, pos.y + size.y), IM_COL32(90, 90, 120, 255)); - ImGui::GetWindowDrawList()->AddText(ImGui::GetFont(), ImGui::GetFontSize()*2.0f, ImVec2(pos.x + offset.x, pos.y + offset.y), IM_COL32_WHITE, "Line 1 hello\nLine 2 clip me!", NULL, 0.0f, &clip_rect); + ImGui::TreePop(); } } @@ -2865,13 +3011,17 @@ static void ShowDemoWindowPopups() if (ImGui::Button("Delete..")) ImGui::OpenPopup("Delete?"); + // Always center this window when appearing + ImVec2 center(ImGui::GetIO().DisplaySize.x * 0.5f, ImGui::GetIO().DisplaySize.y * 0.5f); + ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); ImGui::Separator(); - //static int dummy_i = 0; - //ImGui::Combo("Combo", &dummy_i, "Delete\0Delete harder\0"); + //static int unused_i = 0; + //ImGui::Combo("Combo", &unused_i, "Delete\0Delete harder\0"); static bool dont_ask_me_next_time = false; ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); @@ -2893,7 +3043,7 @@ static void ShowDemoWindowPopups() { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("Dummy menu item")) {} + if (ImGui::MenuItem("Some menu item")) {} ImGui::EndMenu(); } ImGui::EndMenuBar(); @@ -2902,7 +3052,7 @@ static void ShowDemoWindowPopups() // Testing behavior of widgets stacking their own regular popups over the modal. static int item = 1; - static float color[4] = { 0.4f,0.7f,0.0f,0.5f }; + static float color[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); ImGui::ColorEdit4("color", color); @@ -2912,8 +3062,8 @@ static void ShowDemoWindowPopups() // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which // will close the popup. Note that the visibility state of popups is owned by imgui, so the input value // of the bool actually doesn't matter here. - bool dummy_open = true; - if (ImGui::BeginPopupModal("Stacked 2", &dummy_open)) + bool unused_open = true; + if (ImGui::BeginPopupModal("Stacked 2", &unused_open)) { ImGui::Text("Hello from Stacked The Second!"); if (ImGui::Button("Close")) @@ -3220,7 +3370,7 @@ static void ShowDemoWindowMisc() ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (io.MouseDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); @@ -3248,7 +3398,7 @@ static void ShowDemoWindowMisc() if (ImGui::TreeNode("Tabbing")) { ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields."); - static char buf[32] = "dummy"; + static char buf[32] = "hello"; ImGui::InputText("1", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); @@ -3565,7 +3715,6 @@ static void NodeFont(ImFont* font) "You may oversample them to get some flexibility with scaling. " "You can also render at multiple sizes and select which one to use at runtime.\n\n" "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)"); - ImGui::InputFloat("Font offset", &font->DisplayOffset.y, 1, 1, "%.0f"); ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); @@ -3574,8 +3723,8 @@ static void NodeFont(ImFont* font) for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) if (font->ConfigData) if (const ImFontConfig* cfg = &font->ConfigData[config_i]) - ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d", - config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH); + ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d, Offset: (%.1f,%.1f)", + config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH, cfg->GlyphOffset.x, cfg->GlyphOffset.y); if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) { // Display all glyphs of the fonts in separate pages of 256 characters @@ -3702,6 +3851,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f"); ImGui::Text("Alignment"); ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); @@ -3813,10 +3963,9 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n" "Using those settings here will give you poor quality results."); static float window_scale = 1.0f; - if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f")) // Scale only this window - ImGui::SetWindowFontScale(IM_MAX(window_scale, MIN_SCALE)); - if (ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f")) // Scale everything - io.FontGlobalScale = IM_MAX(io.FontGlobalScale, MIN_SCALE); + if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_ClampOnInput)) // Scale only this window + ImGui::SetWindowFontScale(window_scale); + ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_ClampOnInput); // Scale everything ImGui::PopItemWidth(); ImGui::EndTabItem(); @@ -3825,12 +3974,40 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::BeginTabItem("Rendering")) { ImGui::Checkbox("Anti-aliased lines", &style.AntiAliasedLines); - ImGui::SameLine(); HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); + ImGui::SameLine(); + HelpMarker("When disabling anti-aliasing lines, you'll probably want to disable borders in your style as well."); + + ImGui::Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex); + ImGui::SameLine(); + HelpMarker("Faster lines using texture data. Require back-end to render with bilinear filtering (not point/nearest filtering)."); + ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); ImGui::PushItemWidth(100); ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f"); if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f; - ImGui::DragFloat("Circle segment Max Error", &style.CircleSegmentMaxError, 0.01f, 0.10f, 10.0f, "%.2f"); + + // When editing the "Circle Segment Max Error" value, draw a preview of its effect on auto-tessellated circles. + ImGui::DragFloat("Circle Segment Max Error", &style.CircleSegmentMaxError, 0.01f, 0.10f, 10.0f, "%.2f"); + if (ImGui::IsItemActive()) + { + ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos()); + ImGui::BeginTooltip(); + ImVec2 p = ImGui::GetCursorScreenPos(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + float RAD_MIN = 10.0f, RAD_MAX = 80.0f; + float off_x = 10.0f; + for (int n = 0; n < 7; n++) + { + const float rad = RAD_MIN + (RAD_MAX - RAD_MIN) * (float)n / (7.0f - 1.0f); + draw_list->AddCircle(ImVec2(p.x + off_x + rad, p.y + RAD_MAX), rad, ImGui::GetColorU32(ImGuiCol_Text), 0); + off_x += 10.0f + rad * 2.0f; + } + ImGui::Dummy(ImVec2(off_x, RAD_MAX * 2.0f)); + ImGui::EndTooltip(); + } + ImGui::SameLine(); + HelpMarker("When drawing circle primitives with \"num_segments == 0\" tesselation will be calculated automatically."); + ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero. ImGui::PopItemWidth(); @@ -3881,7 +4058,7 @@ static void ShowExampleAppMainMenuBar() // (future version will add explicit flags to BeginMenu() to request processing shortcuts) static void ShowExampleMenuFile() { - ImGui::MenuItem("(dummy menu)", NULL, false, false); + ImGui::MenuItem("(demo menu)", NULL, false, false); if (ImGui::MenuItem("New")) {} if (ImGui::MenuItem("Open", "Ctrl+O")) {} if (ImGui::BeginMenu("Open Recent")) @@ -3929,7 +4106,7 @@ static void ShowExampleMenuFile() { const char* name = ImGui::GetStyleColorName((ImGuiCol)i); ImVec2 p = ImGui::GetCursorScreenPos(); - ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x+sz, p.y+sz), ImGui::GetColorU32((ImGuiCol)i)); + ImGui::GetWindowDrawList()->AddRectFilled(p, ImVec2(p.x + sz, p.y + sz), ImGui::GetColorU32((ImGuiCol)i)); ImGui::Dummy(ImVec2(sz, sz)); ImGui::SameLine(); ImGui::MenuItem(name); @@ -4021,7 +4198,7 @@ struct ExampleAppConsole void Draw(const char* title, bool* p_open) { - ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); if (!ImGui::Begin(title, p_open)) { ImGui::End(); @@ -4039,15 +4216,18 @@ struct ExampleAppConsole } ImGui::TextWrapped( - "This example implements a console with basic coloring, completion and history. A more elaborate " + "This example implements a console with basic coloring, completion (TAB key) and history (Up/Down keys). A more elaborate " "implementation may want to store entries along with extra data such as timestamp, emitter, etc."); - ImGui::TextWrapped("Enter 'HELP' for help, press TAB to use text completion."); + ImGui::TextWrapped("Enter 'HELP' for help."); // TODO: display items starting from the bottom - if (ImGui::SmallButton("Add Dummy Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } ImGui::SameLine(); - if (ImGui::SmallButton("Add Dummy Error")) { AddLog("[error] something went wrong"); } ImGui::SameLine(); - if (ImGui::SmallButton("Clear")) { ClearLog(); } ImGui::SameLine(); + if (ImGui::SmallButton("Add Debug Text")) { AddLog("%d some text", Items.Size); AddLog("some more text"); AddLog("display very important message here!"); } + ImGui::SameLine(); + if (ImGui::SmallButton("Add Debug Error")) { AddLog("[error] something went wrong"); } + ImGui::SameLine(); + if (ImGui::SmallButton("Clear")) { ClearLog(); } + ImGui::SameLine(); bool copy_to_clipboard = ImGui::SmallButton("Copy"); //static float t = 0.0f; if (ImGui::GetTime() - t > 0.02f) { t = ImGui::GetTime(); AddLog("Spam %f", t); } @@ -4099,7 +4279,7 @@ struct ExampleAppConsole // If your items are of variable height: // - Split them into same height items would be simpler and facilitate random-seeking into your list. // - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items. - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4,1)); // Tighten spacing + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing if (copy_to_clipboard) ImGui::LogToClipboard(); for (int i = 0; i < Items.Size; i++) @@ -4159,7 +4339,7 @@ struct ExampleAppConsole // Insert into history. First find match and delete it so it can be pushed to the back. // This isn't trying to be smart or optimal. HistoryPos = -1; - for (int i = History.Size-1; i >= 0; i--) + for (int i = History.Size - 1; i >= 0; i--) if (Stricmp(History[i], command_line) == 0) { free(History[i]); @@ -4224,18 +4404,18 @@ struct ExampleAppConsole // Build a list of candidates ImVector candidates; for (int i = 0; i < Commands.Size; i++) - if (Strnicmp(Commands[i], word_start, (int)(word_end-word_start)) == 0) + if (Strnicmp(Commands[i], word_start, (int)(word_end - word_start)) == 0) candidates.push_back(Commands[i]); if (candidates.Size == 0) { // No match - AddLog("No match for \"%.*s\"!\n", (int)(word_end-word_start), word_start); + AddLog("No match for \"%.*s\"!\n", (int)(word_end - word_start), word_start); } else if (candidates.Size == 1) { // Single match. Delete the beginning of the word and replace it entirely so we've got nice casing. - data->DeleteChars((int)(word_start-data->Buf), (int)(word_end-word_start)); + data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start)); data->InsertChars(data->CursorPos, candidates[0]); data->InsertChars(data->CursorPos, " "); } @@ -4260,7 +4440,7 @@ struct ExampleAppConsole if (match_len > 0) { - data->DeleteChars((int)(word_start - data->Buf), (int)(word_end-word_start)); + data->DeleteChars((int)(word_start - data->Buf), (int)(word_end - word_start)); data->InsertChars(data->CursorPos, candidates[0], candidates[0] + match_len); } @@ -4536,11 +4716,13 @@ static void ShowExampleAppLayout(bool* p_open) // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() //----------------------------------------------------------------------------- -static void ShowDummyObject(const char* prefix, int uid) +static void ShowPlaceholderObject(const char* prefix, int uid) { // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID. ImGui::PushID(uid); - ImGui::AlignTextToFramePadding(); // Text and Tree nodes are less high than framed widgets, here we add vertical spacing to make the tree lines equal high. + + // Text and Tree nodes are less high than framed widgets, using AlignTextToFramePadding() we add vertical spacing to make the tree lines equal high. + ImGui::AlignTextToFramePadding(); bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); ImGui::NextColumn(); ImGui::AlignTextToFramePadding(); @@ -4548,13 +4730,13 @@ static void ShowDummyObject(const char* prefix, int uid) ImGui::NextColumn(); if (node_open) { - static float dummy_members[8] = { 0.0f,0.0f,1.0f,3.1416f,100.0f,999.0f }; + static float placeholder_members[8] = { 0.0f, 0.0f, 1.0f, 3.1416f, 100.0f, 999.0f }; for (int i = 0; i < 8; i++) { ImGui::PushID(i); // Use field index as identifier. if (i < 2) { - ShowDummyObject("Child", 424242); + ShowPlaceholderObject("Child", 424242); } else { @@ -4565,9 +4747,9 @@ static void ShowDummyObject(const char* prefix, int uid) ImGui::NextColumn(); ImGui::SetNextItemWidth(-1); if (i >= 5) - ImGui::InputFloat("##value", &dummy_members[i], 1.0f); + ImGui::InputFloat("##value", &placeholder_members[i], 1.0f); else - ImGui::DragFloat("##value", &dummy_members[i], 0.01f); + ImGui::DragFloat("##value", &placeholder_members[i], 0.01f); ImGui::NextColumn(); } ImGui::PopID(); @@ -4580,7 +4762,7 @@ static void ShowDummyObject(const char* prefix, int uid) // Demonstrate create a simple property editor. static void ShowExampleAppPropertyEditor(bool* p_open) { - ImGui::SetNextWindowSize(ImVec2(430,450), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver); if (!ImGui::Begin("Example: Property editor", p_open)) { ImGui::End(); @@ -4593,13 +4775,13 @@ static void ShowExampleAppPropertyEditor(bool* p_open) "Remember that in many simple cases, you can use ImGui::SameLine(xxx) to position\n" "your cursor horizontally instead of using the Columns() API."); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2,2)); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); ImGui::Columns(2); ImGui::Separator(); - // Iterate dummy objects with dummy members (all the same data) + // Iterate placeholder objects (all the same data) for (int obj_i = 0; obj_i < 3; obj_i++) - ShowDummyObject("Object", obj_i); + ShowPlaceholderObject("Object", obj_i); ImGui::Columns(1); ImGui::Separator(); @@ -4614,7 +4796,7 @@ static void ShowExampleAppPropertyEditor(bool* p_open) // Demonstrate/test rendering huge amount of text, and the incidence of clipping. static void ShowExampleAppLongText(bool* p_open) { - ImGui::SetNextWindowSize(ImVec2(520,600), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(520, 600), ImGuiCond_FirstUseEver); if (!ImGui::Begin("Example: Long text display", p_open)) { ImGui::End(); @@ -4635,7 +4817,7 @@ static void ShowExampleAppLongText(bool* p_open) if (ImGui::Button("Add 1000 lines")) { for (int i = 0; i < 1000; i++) - log.appendf("%i The quick brown fox jumps over the lazy dog\n", lines+i); + log.appendf("%i The quick brown fox jumps over the lazy dog\n", lines + i); lines += 1000; } ImGui::BeginChild("Log"); @@ -4756,16 +4938,15 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) const float DISTANCE = 10.0f; static int corner = 0; ImGuiIO& io = ImGui::GetIO(); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; if (corner != -1) { + window_flags |= ImGuiWindowFlags_NoMove; ImVec2 window_pos = ImVec2((corner & 1) ? io.DisplaySize.x - DISTANCE : DISTANCE, (corner & 2) ? io.DisplaySize.y - DISTANCE : DISTANCE); ImVec2 window_pos_pivot = ImVec2((corner & 1) ? 1.0f : 0.0f, (corner & 2) ? 1.0f : 0.0f); ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); } ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background - ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; - if (corner != -1) - window_flags |= ImGuiWindowFlags_NoMove; if (ImGui::Begin("Example: Simple overlay", p_open, window_flags)) { ImGui::Text("Simple overlay\n" "in the corner of the screen.\n" "(right-click to change position)"); @@ -4837,31 +5018,32 @@ static void ShowExampleAppCustomRendering(bool* p_open) // overloaded operators, etc. Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your // types and ImVec2/ImVec4. Dear ImGui defines overloaded operators but they are internal to imgui.cpp and not // exposed outside (to avoid messing with your types) In this example we are not using the maths operators! - ImDrawList* draw_list = ImGui::GetWindowDrawList(); if (ImGui::BeginTabBar("##TabBar")) { if (ImGui::BeginTabItem("Primitives")) { ImGui::PushItemWidth(-ImGui::GetFontSize() * 10); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); // Draw gradients // (note that those are currently exacerbating our sRGB/Linear issues) + // Calling ImGui::GetColorU32() multiplies the given colors by the current Style Alpha, but you may pass the IM_COL32() directly as well.. ImGui::Text("Gradients"); ImVec2 gradient_size = ImVec2(ImGui::CalcItemWidth(), ImGui::GetFrameHeight()); { ImVec2 p0 = ImGui::GetCursorScreenPos(); ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y); - ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 0.0f, 0.0f, 1.0f)); - ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 1.0f, 1.0f, 1.0f)); + ImU32 col_a = ImGui::GetColorU32(IM_COL32(0, 0, 0, 255)); + ImU32 col_b = ImGui::GetColorU32(IM_COL32(255, 255, 255, 255)); draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a); ImGui::InvisibleButton("##gradient1", gradient_size); } { ImVec2 p0 = ImGui::GetCursorScreenPos(); ImVec2 p1 = ImVec2(p0.x + gradient_size.x, p0.y + gradient_size.y); - ImU32 col_a = ImGui::GetColorU32(ImVec4(0.0f, 1.0f, 0.0f, 1.0f)); - ImU32 col_b = ImGui::GetColorU32(ImVec4(1.0f, 0.0f, 0.0f, 1.0f)); + ImU32 col_a = ImGui::GetColorU32(IM_COL32(0, 255, 0, 255)); + ImU32 col_b = ImGui::GetColorU32(IM_COL32(255, 0, 0, 255)); draw_list->AddRectFilledMultiColor(p0, p1, col_a, col_b, col_b, col_a); ImGui::InvisibleButton("##gradient2", gradient_size); } @@ -4882,6 +5064,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) if (ImGui::SliderInt("Circle segments", &circle_segments_override_v, 3, 40)) circle_segments_override = true; ImGui::ColorEdit4("Color", &colf.x); + const ImVec2 p = ImGui::GetCursorScreenPos(); const ImU32 col = ImColor(colf); const float spacing = 10.0f; @@ -4889,38 +5072,39 @@ static void ShowExampleAppCustomRendering(bool* p_open) const ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; const ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; - float x = p.x + 4.0f, y = p.y + 4.0f; + float x = p.x + 4.0f; + float y = p.y + 4.0f; for (int n = 0; n < 2; n++) { // First line uses a thickness of 1.0f, second line uses the configurable thickness float th = (n == 0) ? 1.0f : thickness; - draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon - draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners - draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th); x += sz + spacing; // Triangle - draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th); x += sz*0.4f + spacing; // Thin triangle - draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) - draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) - draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line + draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon + draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners + draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th);x += sz + spacing; // Triangle + //draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th);x+= sz*0.4f + spacing; // Thin triangle + draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) + draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) + draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line draw_list->AddBezierCurve(ImVec2(x, y), ImVec2(x + sz*1.3f, y + sz*0.3f), ImVec2(x + sz - sz*1.3f, y + sz - sz*0.3f), ImVec2(x + sz, y + sz), col, th); x = p.x + 4; y += sz + spacing; } - draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz*0.5f, col, ngon_sides); x += sz + spacing; // N-gon - draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments);x += sz + spacing; // Circle - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners - draw_list->AddTriangleFilled(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col); x += sz + spacing; // Triangle - draw_list->AddTriangleFilled(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col); x += sz*0.4f + spacing; // Thin triangle - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness) - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing*2.0f; // Vertical line (faster than AddLine, but only handle integer thickness) - draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) + draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz*0.5f, col, ngon_sides); x += sz + spacing; // N-gon + draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments); x += sz + spacing; // Circle + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners + draw_list->AddTriangleFilled(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col); x += sz + spacing; // Triangle + //draw_list->AddTriangleFilled(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col); x += sz*0.4f + spacing; // Thin triangle + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness) + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing * 2.0f;// Vertical line (faster than AddLine, but only handle integer thickness) + draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); - ImGui::Dummy(ImVec2((sz + spacing) * 9.8f, (sz + spacing) * 3)); + ImGui::Dummy(ImVec2((sz + spacing) * 8.8f, (sz + spacing) * 3.0f)); ImGui::PopItemWidth(); ImGui::EndTabItem(); } @@ -4928,55 +5112,97 @@ static void ShowExampleAppCustomRendering(bool* p_open) if (ImGui::BeginTabItem("Canvas")) { static ImVector points; + static ImVec2 scrolling(0.0f, 0.0f); + static bool opt_enable_grid = true; + static bool opt_enable_context_menu = true; static bool adding_line = false; - if (ImGui::Button("Clear")) points.clear(); - if (points.Size >= 2) { ImGui::SameLine(); if (ImGui::Button("Undo")) { points.pop_back(); points.pop_back(); } } - ImGui::Text("Left-click and drag to add lines,\nRight-click to undo"); - - // Here we are using InvisibleButton() as a convenience to 1) advance the cursor and 2) allows us to use - // IsItemHovered(). But you can also draw directly and poll mouse/keyboard by yourself. - // You can manipulate the cursor using GetCursorPos() and SetCursorPos(). - // If you only use the ImDrawList API, you can notify the owner window of its extends with SetCursorPos(max). - ImVec2 canvas_p = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! + + ImGui::Checkbox("Enable grid", &opt_enable_grid); + ImGui::Checkbox("Enable context menu", &opt_enable_context_menu); + ImGui::Text("Mouse Left: drag to add lines,\nMouse Right: drag to scroll, click for context menu."); + + // Typically you would use a BeginChild()/EndChild() pair to benefit from a clipping region + own scrolling. + // Here we demonstrate that this can be replaced by simple offsetting + custom drawing + PushClipRect/PopClipRect() calls. + // To use a child window instead we could use, e.g: + // ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Disable padding + // ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(50, 50, 50, 255)); // Set a background color + // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags_NoMove); + // ImGui::PopStyleColor(); + // ImGui::PopStyleVar(); + // [...] + // ImGui::EndChild(); + + // Using InvisibleButton() as a convenience 1) it will advance the layout cursor and 2) allows us to use IsItemHovered()/IsItemActive() + ImVec2 canvas_p0 = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! ImVec2 canvas_sz = ImGui::GetContentRegionAvail(); // Resize canvas to what's available if (canvas_sz.x < 50.0f) canvas_sz.x = 50.0f; if (canvas_sz.y < 50.0f) canvas_sz.y = 50.0f; - draw_list->AddRectFilledMultiColor(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), IM_COL32(50, 50, 50, 255), IM_COL32(50, 50, 60, 255), IM_COL32(60, 60, 70, 255), IM_COL32(50, 50, 60, 255)); - draw_list->AddRect(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), IM_COL32(255, 255, 255, 255)); + ImVec2 canvas_p1 = ImVec2(canvas_p0.x + canvas_sz.x, canvas_p0.y + canvas_sz.y); - bool adding_preview = false; - ImGui::InvisibleButton("canvas", canvas_sz); - ImVec2 mouse_pos_global = ImGui::GetIO().MousePos; - ImVec2 mouse_pos_canvas = ImVec2(mouse_pos_global.x - canvas_p.x, mouse_pos_global.y - canvas_p.y); + // Draw border and background color + ImGuiIO& io = ImGui::GetIO(); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + draw_list->AddRectFilled(canvas_p0, canvas_p1, IM_COL32(50, 50, 50, 255)); + draw_list->AddRect(canvas_p0, canvas_p1, IM_COL32(255, 255, 255, 255)); + + // This will catch our interactions + ImGui::InvisibleButton("canvas", canvas_sz, ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight); + const bool is_hovered = ImGui::IsItemHovered(); // Hovered + const bool is_active = ImGui::IsItemActive(); // Held + const ImVec2 origin(canvas_p0.x + scrolling.x, canvas_p0.y + scrolling.y); // Lock scrolled origin + const ImVec2 mouse_pos_in_canvas(io.MousePos.x - origin.x, io.MousePos.y - origin.y); + + // Add first and second point + if (is_hovered && !adding_line && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) + { + points.push_back(mouse_pos_in_canvas); + points.push_back(mouse_pos_in_canvas); + adding_line = true; + } if (adding_line) { - adding_preview = true; - points.push_back(mouse_pos_canvas); - if (!ImGui::IsMouseDown(0)) - adding_line = adding_preview = false; + points.back() = mouse_pos_in_canvas; + if (!ImGui::IsMouseDown(ImGuiMouseButton_Left)) + adding_line = false; } - if (ImGui::IsItemHovered()) + + // Pan (we use a zero mouse threshold when there's no context menu) + // You may decide to make that threshold dynamic based on whether the mouse is hovering something etc. + const float mouse_threshold_for_pan = opt_enable_context_menu ? -1.0f : 0.0f; + if (is_active && ImGui::IsMouseDragging(ImGuiMouseButton_Right, mouse_threshold_for_pan)) { - if (!adding_line && ImGui::IsMouseClicked(0)) - { - points.push_back(mouse_pos_canvas); - adding_line = true; - } - if (ImGui::IsMouseClicked(1) && !points.empty()) - { - adding_line = adding_preview = false; - points.pop_back(); - points.pop_back(); - } + scrolling.x += io.MouseDelta.x; + scrolling.y += io.MouseDelta.y; } - // Draw all lines in the canvas (with a clipping rectangle so they don't stray out of it). - draw_list->PushClipRect(canvas_p, ImVec2(canvas_p.x + canvas_sz.x, canvas_p.y + canvas_sz.y), true); - for (int i = 0; i < points.Size - 1; i += 2) - draw_list->AddLine(ImVec2(canvas_p.x + points[i].x, canvas_p.y + points[i].y), ImVec2(canvas_p.x + points[i + 1].x, canvas_p.y + points[i + 1].y), IM_COL32(255, 255, 0, 255), 2.0f); + // Context menu (under default mouse threshold) + ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right); + if (opt_enable_context_menu && ImGui::IsMouseReleased(ImGuiMouseButton_Right) && drag_delta.x == 0.0f && drag_delta.y == 0.0f) + ImGui::OpenPopupContextItem("context"); + if (ImGui::BeginPopup("context")) + { + if (adding_line) + points.resize(points.size() - 2); + adding_line = false; + if (ImGui::MenuItem("Remove one", NULL, false, points.Size > 0)) { points.resize(points.size() - 2); } + if (ImGui::MenuItem("Remove all", NULL, false, points.Size > 0)) { points.clear(); } + ImGui::EndPopup(); + } + + // Draw grid + all lines in the canvas + draw_list->PushClipRect(canvas_p0, canvas_p1, true); + if (opt_enable_grid) + { + const float GRID_STEP = 64.0f; + for (float x = fmodf(scrolling.x, GRID_STEP); x < canvas_sz.x; x += GRID_STEP) + draw_list->AddLine(ImVec2(canvas_p0.x + x, canvas_p0.y), ImVec2(canvas_p0.x + x, canvas_p1.y), IM_COL32(200, 200, 200, 40)); + for (float y = fmodf(scrolling.y, GRID_STEP); y < canvas_sz.y; y += GRID_STEP) + draw_list->AddLine(ImVec2(canvas_p0.x, canvas_p0.y + y), ImVec2(canvas_p1.x, canvas_p0.y + y), IM_COL32(200, 200, 200, 40)); + } + for (int n = 0; n < points.Size; n += 2) + draw_list->AddLine(ImVec2(origin.x + points[n].x, origin.y + points[n].y), ImVec2(origin.x + points[n + 1].x, origin.y + points[n + 1].y), IM_COL32(255, 255, 0, 255), 2.0f); draw_list->PopClipRect(); - if (adding_preview) - points.pop_back(); + ImGui::EndTabItem(); } @@ -4992,7 +5218,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImVec2 window_size = ImGui::GetWindowSize(); ImVec2 window_center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f); if (draw_bg) - ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 0, 10+4); + ImGui::GetBackgroundDrawList()->AddCircle(window_center, window_size.x * 0.6f, IM_COL32(255, 0, 0, 200), 0, 10 + 4); if (draw_fg) ImGui::GetForegroundDrawList()->AddCircle(window_center, window_size.y * 0.6f, IM_COL32(0, 255, 0, 200), 0, 10); ImGui::EndTabItem(); @@ -5018,7 +5244,7 @@ struct MyDocument bool WantClose; // Set when the document ImVec4 Color; // An arbitrary variable associated to the document - MyDocument(const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f,1.0f,1.0f,1.0f)) + MyDocument(const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)) { Name = name; Open = OpenPrev = open; @@ -5031,7 +5257,7 @@ struct MyDocument void DoForceClose() { Open = false; Dirty = false; } void DoSave() { Dirty = false; } - // Display dummy contents for the Document + // Display placeholder contents for the Document static void DisplayContents(MyDocument* doc) { ImGui::PushID(doc); diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 0e7c294e..3f666698 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (drawing and font code) /* @@ -339,7 +339,7 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) } //----------------------------------------------------------------------------- -// ImDrawList +// [SECTION] ImDrawList //----------------------------------------------------------------------------- ImDrawListSharedData::ImDrawListSharedData() @@ -358,6 +358,7 @@ ImDrawListSharedData::ImDrawListSharedData() ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); } memset(CircleSegmentCounts, 0, sizeof(CircleSegmentCounts)); // This will be set by SetCircleSegmentMaxError() + TexUvLines = NULL; } void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) @@ -516,7 +517,7 @@ void ImDrawList::_OnChangedVtxOffset() // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. _VtxCurrentIdx = 0; ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; - IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); + //IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); // See #3349 if (curr_cmd->ElemCount != 0) { AddDrawCmd(); @@ -581,6 +582,9 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); if (sizeof(ImDrawIdx) == 2 && (_VtxCurrentIdx + vtx_count >= (1 << 16)) && (Flags & ImDrawListFlags_AllowVtxOffset)) { + // FIXME: In theory we should be testing that vtx_count <64k here. + // In practice, RenderText() relies on reserving ahead for a worst case scenario so it is currently useful for us + // to not make that check until we rework the text functions to handle clipping and large horizontal lines better. _CmdHeader.VtxOffset = VtxBuffer.Size; _OnChangedVtxOffset(); } @@ -666,28 +670,41 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 return; const ImVec2 opaque_uv = _Data->TexUvWhitePixel; - int count = points_count; - if (!closed) - count = points_count-1; - + const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw const bool thick_line = (thickness > 1.0f); + if (Flags & ImDrawListFlags_AntiAliasedLines) { // Anti-aliased stroke const float AA_SIZE = 1.0f; const ImU32 col_trans = col & ~IM_COL32_A_MASK; - const int idx_count = thick_line ? count*18 : count*12; - const int vtx_count = thick_line ? points_count*4 : points_count*3; + // Thicknesses <1.0 should behave like thickness 1.0 + thickness = ImMax(thickness, 1.0f); + const int integer_thickness = (int)thickness; + const float fractional_thickness = thickness - integer_thickness; + + // Do we want to draw this line using a texture? + // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved. + // - If AA_SIZE is not 1.0f we cannot use the texture path. + const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f); + + // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off + IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)); + + const int idx_count = use_texture ? (count * 6) : (thick_line ? count * 18 : count * 12); + const int vtx_count = use_texture ? (points_count * 2) : (thick_line ? points_count * 4 : points_count * 3); PrimReserve(idx_count, vtx_count); // Temporary buffer - ImVec2* temp_normals = (ImVec2*)alloca(points_count * (thick_line ? 5 : 3) * sizeof(ImVec2)); //-V630 + // The first items are normals at each line point, then after that there are either 2 or 4 temp points for each line point + ImVec2* temp_normals = (ImVec2*)alloca(points_count * ((use_texture || !thick_line) ? 3 : 5) * sizeof(ImVec2)); //-V630 ImVec2* temp_points = temp_normals + points_count; + // Calculate normals (tangents) for each line segment for (int i1 = 0; i1 < count; i1++) { - const int i2 = (i1+1) == points_count ? 0 : i1+1; + const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; float dx = points[i2].x - points[i1].x; float dy = points[i2].y - points[i1].y; IM_NORMALIZE2F_OVER_ZERO(dx, dy); @@ -695,61 +712,113 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 temp_normals[i1].y = -dx; } if (!closed) - temp_normals[points_count-1] = temp_normals[points_count-2]; + temp_normals[points_count - 1] = temp_normals[points_count - 2]; - if (!thick_line) + // If we are drawing a one-pixel-wide line without a texture, or a textured line of any width, we only need 2 or 3 vertices per point + if (use_texture || !thick_line) { + // [PATH 1] Texture-based lines (thick or non-thick) + // [PATH 2] Non texture-based lines (non-thick) + + // The width of the geometry we need to draw - this is essentially pixels for the line itself, plus "one pixel" for AA. + // - In the texture-based path, we don't use AA_SIZE here because the +1 is tied to the generated texture + // (see ImFontAtlasBuildRenderLinesTexData() function), and so alternate values won't work without changes to that code. + // - In the non texture-based paths, we would allow AA_SIZE to potentially be != 1.0f with a patch (e.g. fringe_scale patch to + // allow scaling geometry while preserving one-screen-pixel AA fringe). + const float half_draw_size = use_texture ? ((thickness * 0.5f) + 1) : AA_SIZE; + + // If line is not closed, the first and last points need to be generated differently as there are no normals to blend if (!closed) { - temp_points[0] = points[0] + temp_normals[0] * AA_SIZE; - temp_points[1] = points[0] - temp_normals[0] * AA_SIZE; - temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * AA_SIZE; - temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * AA_SIZE; + temp_points[0] = points[0] + temp_normals[0] * half_draw_size; + temp_points[1] = points[0] - temp_normals[0] * half_draw_size; + temp_points[(points_count-1)*2+0] = points[points_count-1] + temp_normals[points_count-1] * half_draw_size; + temp_points[(points_count-1)*2+1] = points[points_count-1] - temp_normals[points_count-1] * half_draw_size; } + // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges + // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps) // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. - unsigned int idx1 = _VtxCurrentIdx; - for (int i1 = 0; i1 < count; i1++) + unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment + for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment { - const int i2 = (i1+1) == points_count ? 0 : i1+1; - unsigned int idx2 = (i1+1) == points_count ? _VtxCurrentIdx : idx1+3; + const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; // i2 is the second point of the line segment + const unsigned int idx2 = ((i1 + 1) == points_count) ? _VtxCurrentIdx : (idx1 + (use_texture ? 2 : 3)); // Vertex index for end of segment // Average normals float dm_x = (temp_normals[i1].x + temp_normals[i2].x) * 0.5f; float dm_y = (temp_normals[i1].y + temp_normals[i2].y) * 0.5f; IM_FIXNORMAL2F(dm_x, dm_y); - dm_x *= AA_SIZE; - dm_y *= AA_SIZE; + dm_x *= half_draw_size; // dm_x, dm_y are offset to the outer edge of the AA area + dm_y *= half_draw_size; - // Add temporary vertices - ImVec2* out_vtx = &temp_points[i2*2]; + // Add temporary vertexes for the outer edges + ImVec2* out_vtx = &temp_points[i2 * 2]; out_vtx[0].x = points[i2].x + dm_x; out_vtx[0].y = points[i2].y + dm_y; out_vtx[1].x = points[i2].x - dm_x; out_vtx[1].y = points[i2].y - dm_y; - // Add indexes - _IdxWritePtr[0] = (ImDrawIdx)(idx2+0); _IdxWritePtr[1] = (ImDrawIdx)(idx1+0); _IdxWritePtr[2] = (ImDrawIdx)(idx1+2); - _IdxWritePtr[3] = (ImDrawIdx)(idx1+2); _IdxWritePtr[4] = (ImDrawIdx)(idx2+2); _IdxWritePtr[5] = (ImDrawIdx)(idx2+0); - _IdxWritePtr[6] = (ImDrawIdx)(idx2+1); _IdxWritePtr[7] = (ImDrawIdx)(idx1+1); _IdxWritePtr[8] = (ImDrawIdx)(idx1+0); - _IdxWritePtr[9] = (ImDrawIdx)(idx1+0); _IdxWritePtr[10]= (ImDrawIdx)(idx2+0); _IdxWritePtr[11]= (ImDrawIdx)(idx2+1); - _IdxWritePtr += 12; + if (use_texture) + { + // Add indices for two triangles + _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 1); // Right tri + _IdxWritePtr[3] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[4] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Left tri + _IdxWritePtr += 6; + } + else + { + // Add indexes for four triangles + _IdxWritePtr[0] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[1] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[2] = (ImDrawIdx)(idx1 + 2); // Right tri 1 + _IdxWritePtr[3] = (ImDrawIdx)(idx1 + 2); _IdxWritePtr[4] = (ImDrawIdx)(idx2 + 2); _IdxWritePtr[5] = (ImDrawIdx)(idx2 + 0); // Right tri 2 + _IdxWritePtr[6] = (ImDrawIdx)(idx2 + 1); _IdxWritePtr[7] = (ImDrawIdx)(idx1 + 1); _IdxWritePtr[8] = (ImDrawIdx)(idx1 + 0); // Left tri 1 + _IdxWritePtr[9] = (ImDrawIdx)(idx1 + 0); _IdxWritePtr[10] = (ImDrawIdx)(idx2 + 0); _IdxWritePtr[11] = (ImDrawIdx)(idx2 + 1); // Left tri 2 + _IdxWritePtr += 12; + } idx1 = idx2; } - // Add vertices - for (int i = 0; i < points_count; i++) + // Add vertexes for each point on the line + if (use_texture) + { + // If we're using textures we only need to emit the left/right edge vertices + ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness]; + if (fractional_thickness != 0.0f) + { + const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1]; + tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp() + tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness; + tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness; + tex_uvs.w = tex_uvs.w + (tex_uvs_1.w - tex_uvs.w) * fractional_thickness; + } + ImVec2 tex_uv0(tex_uvs.x, tex_uvs.y); + ImVec2 tex_uv1(tex_uvs.z, tex_uvs.w); + for (int i = 0; i < points_count; i++) + { + _VtxWritePtr[0].pos = temp_points[i * 2 + 0]; _VtxWritePtr[0].uv = tex_uv0; _VtxWritePtr[0].col = col; // Left-side outer edge + _VtxWritePtr[1].pos = temp_points[i * 2 + 1]; _VtxWritePtr[1].uv = tex_uv1; _VtxWritePtr[1].col = col; // Right-side outer edge + _VtxWritePtr += 2; + } + } + else { - _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; - _VtxWritePtr[1].pos = temp_points[i*2+0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; - _VtxWritePtr[2].pos = temp_points[i*2+1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; - _VtxWritePtr += 3; + // If we're not using a texture, we need the center vertex as well + for (int i = 0; i < points_count; i++) + { + _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = opaque_uv; _VtxWritePtr[0].col = col; // Center of line + _VtxWritePtr[1].pos = temp_points[i * 2 + 0]; _VtxWritePtr[1].uv = opaque_uv; _VtxWritePtr[1].col = col_trans; // Left-side outer edge + _VtxWritePtr[2].pos = temp_points[i * 2 + 1]; _VtxWritePtr[2].uv = opaque_uv; _VtxWritePtr[2].col = col_trans; // Right-side outer edge + _VtxWritePtr += 3; + } } } else { + // [PATH 2] Non texture-based lines (thick): we need to draw the solid line core and thus require four vertices per point const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f; + + // If line is not closed, the first and last points need to be generated differently as there are no normals to blend if (!closed) { const int points_last = points_count - 1; @@ -763,9 +832,11 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 temp_points[points_last * 4 + 3] = points[points_last] - temp_normals[points_last] * (half_inner_thickness + AA_SIZE); } + // Generate the indices to form a number of triangles for each line segment, and the vertices for the line edges + // This takes points n and n+1 and writes into n+1, with the first point in a closed line being generated from the final one (as n+1 wraps) // FIXME-OPT: Merge the different loops, possibly remove the temporary buffer. - unsigned int idx1 = _VtxCurrentIdx; - for (int i1 = 0; i1 < count; i1++) + unsigned int idx1 = _VtxCurrentIdx; // Vertex index for start of line segment + for (int i1 = 0; i1 < count; i1++) // i1 is the first point of the line segment { const int i2 = (i1 + 1) == points_count ? 0 : (i1 + 1); // i2 is the second point of the line segment const unsigned int idx2 = (i1 + 1) == points_count ? _VtxCurrentIdx : (idx1 + 4); // Vertex index for end of segment @@ -780,7 +851,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 float dm_in_y = dm_y * half_inner_thickness; // Add temporary vertices - ImVec2* out_vtx = &temp_points[i2*4]; + ImVec2* out_vtx = &temp_points[i2 * 4]; out_vtx[0].x = points[i2].x + dm_out_x; out_vtx[0].y = points[i2].y + dm_out_y; out_vtx[1].x = points[i2].x + dm_in_x; @@ -816,14 +887,14 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 } else { - // Non Anti-aliased Stroke - const int idx_count = count*6; - const int vtx_count = count*4; // FIXME-OPT: Not sharing edges + // [PATH 4] Non texture-based, Non anti-aliased lines + const int idx_count = count * 6; + const int vtx_count = count * 4; // FIXME-OPT: Not sharing edges PrimReserve(idx_count, vtx_count); for (int i1 = 0; i1 < count; i1++) { - const int i2 = (i1+1) == points_count ? 0 : i1+1; + const int i2 = (i1 + 1) == points_count ? 0 : i1 + 1; const ImVec2& p1 = points[i1]; const ImVec2& p2 = points[i2]; @@ -860,22 +931,22 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun // Anti-aliased Fill const float AA_SIZE = 1.0f; const ImU32 col_trans = col & ~IM_COL32_A_MASK; - const int idx_count = (points_count-2)*3 + points_count*6; - const int vtx_count = (points_count*2); + const int idx_count = (points_count - 2)*3 + points_count * 6; + const int vtx_count = (points_count * 2); PrimReserve(idx_count, vtx_count); // Add indexes for fill unsigned int vtx_inner_idx = _VtxCurrentIdx; - unsigned int vtx_outer_idx = _VtxCurrentIdx+1; + unsigned int vtx_outer_idx = _VtxCurrentIdx + 1; for (int i = 2; i < points_count; i++) { - _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+((i-1)<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx+(i<<1)); + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + ((i - 1) << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (i << 1)); _IdxWritePtr += 3; } // Compute normals ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2)); //-V630 - for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) + for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) { const ImVec2& p0 = points[i0]; const ImVec2& p1 = points[i1]; @@ -886,7 +957,7 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun temp_normals[i0].y = -dx; } - for (int i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) + for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) { // Average normals const ImVec2& n0 = temp_normals[i0]; @@ -903,8 +974,8 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun _VtxWritePtr += 2; // Add indexes for fringes - _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx+(i0<<1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); - _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx+(i0<<1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx+(i1<<1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx+(i1<<1)); + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); + _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr += 6; } _VtxCurrentIdx += (ImDrawIdx)vtx_count; @@ -912,7 +983,7 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun else { // Non Anti-aliased Fill - const int idx_count = (points_count-2)*3; + const int idx_count = (points_count - 2)*3; const int vtx_count = points_count; PrimReserve(idx_count, vtx_count); for (int i = 0; i < vtx_count; i++) @@ -922,7 +993,7 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun } for (int i = 2; i < points_count; i++) { - _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx+i-1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx+i); + _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + i - 1); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + i); _IdxWritePtr += 3; } _VtxCurrentIdx += (ImDrawIdx)vtx_count; @@ -989,20 +1060,20 @@ static void PathBezierToCasteljau(ImVector* path, float x1, float y1, fl float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); d2 = (d2 >= 0) ? d2 : -d2; d3 = (d3 >= 0) ? d3 : -d3; - if ((d2+d3) * (d2+d3) < tess_tol * (dx*dx + dy*dy)) + if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) { path->push_back(ImVec2(x4, y4)); } else if (level < 10) { - float x12 = (x1+x2)*0.5f, y12 = (y1+y2)*0.5f; - float x23 = (x2+x3)*0.5f, y23 = (y2+y3)*0.5f; - float x34 = (x3+x4)*0.5f, y34 = (y3+y4)*0.5f; - float x123 = (x12+x23)*0.5f, y123 = (y12+y23)*0.5f; - float x234 = (x23+x34)*0.5f, y234 = (y23+y34)*0.5f; - float x1234 = (x123+x234)*0.5f, y1234 = (y123+y234)*0.5f; - PathBezierToCasteljau(path, x1,y1, x12,y12, x123,y123, x1234,y1234, tess_tol, level+1); - PathBezierToCasteljau(path, x1234,y1234, x234,y234, x34,y34, x4,y4, tess_tol, level+1); + float x12 = (x1 + x2)*0.5f, y12 = (y1 + y2)*0.5f; + float x23 = (x2 + x3)*0.5f, y23 = (y2 + y3)*0.5f; + float x34 = (x3 + x4)*0.5f, y34 = (y3 + y4)*0.5f; + float x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; + float x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; + float x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; + PathBezierToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); + PathBezierToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); } } @@ -1062,9 +1133,9 @@ void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, fl if ((col & IM_COL32_A_MASK) == 0) return; if (Flags & ImDrawListFlags_AntiAliasedLines) - PathRect(p_min + ImVec2(0.50f,0.50f), p_max - ImVec2(0.50f,0.50f), rounding, rounding_corners); + PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.50f, 0.50f), rounding, rounding_corners); else - PathRect(p_min + ImVec2(0.50f,0.50f), p_max - ImVec2(0.49f,0.49f), rounding, rounding_corners); // Better looking lower-right corner and rounded non-AA shapes. + PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.49f, 0.49f), rounding, rounding_corners); // Better looking lower-right corner and rounded non-AA shapes. PathStroke(col, true, thickness); } @@ -1092,8 +1163,8 @@ void ImDrawList::AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_ma const ImVec2 uv = _Data->TexUvWhitePixel; PrimReserve(6, 4); - PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); - PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx+3)); + PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 1)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 2)); + PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 2)); PrimWriteIdx((ImDrawIdx)(_VtxCurrentIdx + 3)); PrimWriteVtx(p_min, uv, col_upr_left); PrimWriteVtx(ImVec2(p_max.x, p_min.y), uv, col_upr_right); PrimWriteVtx(p_max, uv, col_bot_right); @@ -1170,7 +1241,7 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; if (num_segments == 12) - PathArcToFast(center, radius - 0.5f, 0, 12); + PathArcToFast(center, radius - 0.5f, 0, 12 - 1); else PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); PathStroke(col, true, thickness); @@ -1200,7 +1271,7 @@ void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; if (num_segments == 12) - PathArcToFast(center, radius, 0, 12); + PathArcToFast(center, radius, 0, 12 - 1); else PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); PathFillConvex(col); @@ -1334,7 +1405,7 @@ void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_mi //----------------------------------------------------------------------------- -// ImDrawListSplitter +// [SECTION] ImDrawListSplitter //----------------------------------------------------------------------------- // FIXME: This may be a little confusing, trying to be a little too low-level/optimal instead of just doing vector swap.. //----------------------------------------------------------------------------- @@ -1528,13 +1599,19 @@ void ImGui::ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int ve float gradient_inv_length2 = 1.0f / ImLengthSqr(gradient_extent); ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; + const int col0_r = (int)(col0 >> IM_COL32_R_SHIFT) & 0xFF; + const int col0_g = (int)(col0 >> IM_COL32_G_SHIFT) & 0xFF; + const int col0_b = (int)(col0 >> IM_COL32_B_SHIFT) & 0xFF; + const int col_delta_r = ((int)(col1 >> IM_COL32_R_SHIFT) & 0xFF) - col0_r; + const int col_delta_g = ((int)(col1 >> IM_COL32_G_SHIFT) & 0xFF) - col0_g; + const int col_delta_b = ((int)(col1 >> IM_COL32_B_SHIFT) & 0xFF) - col0_b; for (ImDrawVert* vert = vert_start; vert < vert_end; vert++) { float d = ImDot(vert->pos - gradient_p0, gradient_extent); float t = ImClamp(d * gradient_inv_length2, 0.0f, 1.0f); - int r = ImLerp((int)(col0 >> IM_COL32_R_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_R_SHIFT) & 0xFF, t); - int g = ImLerp((int)(col0 >> IM_COL32_G_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_G_SHIFT) & 0xFF, t); - int b = ImLerp((int)(col0 >> IM_COL32_B_SHIFT) & 0xFF, (int)(col1 >> IM_COL32_B_SHIFT) & 0xFF, t); + int r = (int)(col0_r + col_delta_r * t); + int g = (int)(col0_g + col_delta_g * t); + int b = (int)(col0_b + col_delta_b * t); vert->col = (r << IM_COL32_R_SHIFT) | (g << IM_COL32_G_SHIFT) | (b << IM_COL32_B_SHIFT) | (vert->col & IM_COL32_A_MASK); } } @@ -1596,10 +1673,10 @@ ImFontConfig::ImFontConfig() //----------------------------------------------------------------------------- // A work of art lies ahead! (. = white layer, X = black layer, others are blank) -// The white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. -const int FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF = 108; -const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; -static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = +// The 2x2 white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. +const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 108; // Actual texture will be 2 times that + 1 spacing. +const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; +static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = { "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX " "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X " @@ -1656,8 +1733,7 @@ ImFontAtlas::ImFontAtlas() TexWidth = TexHeight = 0; TexUvScale = ImVec2(0.0f, 0.0f); TexUvWhitePixel = ImVec2(0.0f, 0.0f); - for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) - CustomRectIds[n] = -1; + PackIdMouseCursors = PackIdLines = -1; } ImFontAtlas::~ImFontAtlas() @@ -1685,8 +1761,7 @@ void ImFontAtlas::ClearInputData() } ConfigData.clear(); CustomRects.clear(); - for (int n = 0; n < IM_ARRAYSIZE(CustomRectIds); n++) - CustomRectIds[n] = -1; + PackIdMouseCursors = PackIdLines = -1; } void ImFontAtlas::ClearTexData() @@ -1787,15 +1862,15 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) } // Default font TTF is compressed with stb_compress then base85 encoded (see misc/fonts/binary_to_compressed_c.cpp for encoder) -static unsigned int stb_decompress_length(const unsigned char *input); -static unsigned int stb_decompress(unsigned char *output, const unsigned char *input, unsigned int length); +static unsigned int stb_decompress_length(const unsigned char* input); +static unsigned int stb_decompress(unsigned char* output, const unsigned char* input, unsigned int length); static const char* GetDefaultCompressedFontDataTTFBase85(); static unsigned int Decode85Byte(char c) { return c >= '\\' ? c-36 : c-35; } static void Decode85(const unsigned char* src, unsigned char* dst) { while (*src) { - unsigned int tmp = Decode85Byte(src[0]) + 85*(Decode85Byte(src[1]) + 85*(Decode85Byte(src[2]) + 85*(Decode85Byte(src[3]) + 85*Decode85Byte(src[4])))); + unsigned int tmp = Decode85Byte(src[0]) + 85 * (Decode85Byte(src[1]) + 85 * (Decode85Byte(src[2]) + 85 * (Decode85Byte(src[3]) + 85 * Decode85Byte(src[4])))); dst[0] = ((tmp >> 0) & 0xFF); dst[1] = ((tmp >> 8) & 0xFF); dst[2] = ((tmp >> 16) & 0xFF); dst[3] = ((tmp >> 24) & 0xFF); // We can't assume little-endianness. src += 5; dst += 4; @@ -1816,11 +1891,11 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) if (font_cfg.Name[0] == '\0') ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels); font_cfg.EllipsisChar = (ImWchar)0x0085; + font_cfg.GlyphOffset.y = 1.0f * IM_FLOOR(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85(); const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); ImFont* font = AddFontFromMemoryCompressedBase85TTF(ttf_compressed_base85, font_cfg.SizePixels, &font_cfg, glyph_ranges); - font->DisplayOffset.y = 1.0f; return font; } @@ -1862,7 +1937,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float si ImFont* ImFontAtlas::AddFontFromMemoryCompressedTTF(const void* compressed_ttf_data, int compressed_ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { const unsigned int buf_decompressed_size = stb_decompress_length((const unsigned char*)compressed_ttf_data); - unsigned char* buf_decompressed_data = (unsigned char *)IM_ALLOC(buf_decompressed_size); + unsigned char* buf_decompressed_data = (unsigned char*)IM_ALLOC(buf_decompressed_size); stb_decompress(buf_decompressed_data, (const unsigned char*)compressed_ttf_data, (unsigned int)compressed_ttf_size); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); @@ -1926,15 +2001,15 @@ bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* ou if (Flags & ImFontAtlasFlags_NoMouseCursors) return false; - IM_ASSERT(CustomRectIds[0] != -1); - ImFontAtlasCustomRect& r = CustomRects[CustomRectIds[0]]; - ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r.X, (float)r.Y); + IM_ASSERT(PackIdMouseCursors != -1); + ImFontAtlasCustomRect* r = GetCustomRectByIndex(PackIdMouseCursors); + ImVec2 pos = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][0] + ImVec2((float)r->X, (float)r->Y); ImVec2 size = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][1]; *out_size = size; *out_offset = FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[cursor_type][2]; out_uv_border[0] = (pos) * TexUvScale; out_uv_border[1] = (pos + size) * TexUvScale; - pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; + pos.x += FONT_ATLAS_DEFAULT_TEX_DATA_W + 1; out_uv_fill[0] = (pos) * TexUvScale; out_uv_fill[1] = (pos + size) * TexUvScale; return true; @@ -2149,7 +2224,7 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) if (atlas->TexDesiredWidth > 0) atlas->TexWidth = atlas->TexDesiredWidth; else - atlas->TexWidth = (surface_sqrt >= 4096*0.7f) ? 4096 : (surface_sqrt >= 2048*0.7f) ? 2048 : (surface_sqrt >= 1024*0.7f) ? 1024 : 512; + atlas->TexWidth = (surface_sqrt >= 4096 * 0.7f) ? 4096 : (surface_sqrt >= 2048 * 0.7f) ? 2048 : (surface_sqrt >= 1024 * 0.7f) ? 1024 : 512; // 5. Start packing // Pack our extra data rectangles first, so it will be on the upper-left corner of our texture (UV will have small values). @@ -2216,8 +2291,11 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) if (src_tmp.GlyphsCount == 0) continue; + // When merging fonts with MergeMode=true: + // - We can have multiple input fonts writing into a same destination font. + // - dst_font->ConfigData is != from cfg which is our source configuration. ImFontConfig& cfg = atlas->ConfigData[src_i]; - ImFont* dst_font = cfg.DstFont; // We can have multiple input fonts writing into a same destination font (when using MergeMode=true) + ImFont* dst_font = cfg.DstFont; const float font_scale = stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels); int unscaled_ascent, unscaled_descent, unscaled_line_gap; @@ -2231,20 +2309,13 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) { + // Register glyph const int codepoint = src_tmp.GlyphsList[glyph_i]; const stbtt_packedchar& pc = src_tmp.PackedChars[glyph_i]; - - const float char_advance_x_org = pc.xadvance; - const float char_advance_x_mod = ImClamp(char_advance_x_org, cfg.GlyphMinAdvanceX, cfg.GlyphMaxAdvanceX); - float char_off_x = font_off_x; - if (char_advance_x_org != char_advance_x_mod) - char_off_x += cfg.PixelSnapH ? ImFloor((char_advance_x_mod - char_advance_x_org) * 0.5f) : (char_advance_x_mod - char_advance_x_org) * 0.5f; - - // Register glyph stbtt_aligned_quad q; - float dummy_x = 0.0f, dummy_y = 0.0f; - stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &dummy_x, &dummy_y, &q, 0); - dst_font->AddGlyph((ImWchar)codepoint, q.x0 + char_off_x, q.y0 + font_off_y, q.x1 + char_off_x, q.y1 + font_off_y, q.s0, q.t0, q.s1, q.t1, char_advance_x_mod); + float unused_x = 0.0f, unused_y = 0.0f; + stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &unused_x, &unused_y, &q, 0); + dst_font->AddGlyph(&cfg, (ImWchar)codepoint, q.x0 + font_off_x, q.y0 + font_off_y, q.x1 + font_off_x, q.y1 + font_off_y, q.s0, q.t0, q.s1, q.t1, pc.xadvance); } } @@ -2256,17 +2327,6 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) return true; } -// Register default custom rectangles (this is called/shared by both the stb_truetype and the FreeType builder) -void ImFontAtlasBuildInit(ImFontAtlas* atlas) -{ - if (atlas->CustomRectIds[0] >= 0) - return; - if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) - atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF*2+1, FONT_ATLAS_DEFAULT_TEX_DATA_H); - else - atlas->CustomRectIds[0] = atlas->AddCustomRectRegular(2, 2); -} - void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) { if (!font_config->MergeMode) @@ -2274,6 +2334,7 @@ void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* f font->ClearOutputData(); font->FontSize = font_config->SizePixels; font->ConfigData = font_config; + font->ConfigDataCount = 0; font->ContainerAtlas = atlas; font->Ascent = ascent; font->Descent = descent; @@ -2308,52 +2369,113 @@ void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opa } } +void ImFontAtlasBuildRender1bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value) +{ + IM_ASSERT(x >= 0 && x + w <= atlas->TexWidth); + IM_ASSERT(y >= 0 && y + h <= atlas->TexHeight); + unsigned char* out_pixel = atlas->TexPixelsAlpha8 + x + (y * atlas->TexWidth); + for (int off_y = 0; off_y < h; off_y++, out_pixel += atlas->TexWidth, in_str += w) + for (int off_x = 0; off_x < w; off_x++) + out_pixel[off_x] = (in_str[off_x] == in_marker_char) ? in_marker_pixel_value : 0x00; +} + static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) { - IM_ASSERT(atlas->CustomRectIds[0] >= 0); - IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); - ImFontAtlasCustomRect& r = atlas->CustomRects[atlas->CustomRectIds[0]]; - IM_ASSERT(r.IsPacked()); + ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdMouseCursors); + IM_ASSERT(r->IsPacked()); const int w = atlas->TexWidth; if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) { // Render/copy pixels - IM_ASSERT(r.Width == FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF * 2 + 1 && r.Height == FONT_ATLAS_DEFAULT_TEX_DATA_H); - for (int y = 0, n = 0; y < FONT_ATLAS_DEFAULT_TEX_DATA_H; y++) - for (int x = 0; x < FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF; x++, n++) - { - const int offset0 = (int)(r.X + x) + (int)(r.Y + y) * w; - const int offset1 = offset0 + FONT_ATLAS_DEFAULT_TEX_DATA_W_HALF + 1; - atlas->TexPixelsAlpha8[offset0] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == '.' ? 0xFF : 0x00; - atlas->TexPixelsAlpha8[offset1] = FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[n] == 'X' ? 0xFF : 0x00; - } + IM_ASSERT(r->Width == FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1 && r->Height == FONT_ATLAS_DEFAULT_TEX_DATA_H); + const int x_for_white = r->X; + const int x_for_black = r->X + FONT_ATLAS_DEFAULT_TEX_DATA_W + 1; + ImFontAtlasBuildRender1bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', 0xFF); + ImFontAtlasBuildRender1bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', 0xFF); } else { - IM_ASSERT(r.Width == 2 && r.Height == 2); - const int offset = (int)(r.X) + (int)(r.Y) * w; + // Render 4 white pixels + IM_ASSERT(r->Width == 2 && r->Height == 2); + const int offset = (int)r->X + (int)r->Y * w; atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF; } - atlas->TexUvWhitePixel = ImVec2((r.X + 0.5f) * atlas->TexUvScale.x, (r.Y + 0.5f) * atlas->TexUvScale.y); + atlas->TexUvWhitePixel = ImVec2((r->X + 0.5f) * atlas->TexUvScale.x, (r->Y + 0.5f) * atlas->TexUvScale.y); } +static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) +{ + if (atlas->Flags & ImFontAtlasFlags_NoBakedLines) + return; + + // This generates a triangular shape in the texture, with the various line widths stacked on top of each other to allow interpolation between them + ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdLines); + IM_ASSERT(r->IsPacked()); + for (unsigned int n = 0; n < IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1; n++) // +1 because of the zero-width row + { + // Each line consists of at least two empty pixels at the ends, with a line of solid pixels in the middle + unsigned int y = n; + unsigned int line_width = n; + unsigned int pad_left = (r->Width - line_width) / 2; + unsigned int pad_right = r->Width - (pad_left + line_width); + + // Write each slice + IM_ASSERT(pad_left + line_width + pad_right == r->Width && y < r->Height); // Make sure we're inside the texture bounds before we start writing pixels + unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)]; + memset(write_ptr, 0x00, pad_left); + memset(write_ptr + pad_left, 0xFF, line_width); + memset(write_ptr + pad_left + line_width, 0x00, pad_right); + + // Calculate UVs for this line + ImVec2 uv0 = ImVec2((float)(r->X + pad_left - 1), (float)(r->Y + y)) * atlas->TexUvScale; + ImVec2 uv1 = ImVec2((float)(r->X + pad_left + line_width + 1), (float)(r->Y + y + 1)) * atlas->TexUvScale; + float half_v = (uv0.y + uv1.y) * 0.5f; // Calculate a constant V in the middle of the row to avoid sampling artifacts + atlas->TexUvLines[n] = ImVec4(uv0.x, half_v, uv1.x, half_v); + } +} + +// Note: this is called / shared by both the stb_truetype and the FreeType builder +void ImFontAtlasBuildInit(ImFontAtlas* atlas) +{ + // Register texture region for mouse cursors or standard white pixels + if (atlas->PackIdMouseCursors < 0) + { + if (!(atlas->Flags & ImFontAtlasFlags_NoMouseCursors)) + atlas->PackIdMouseCursors = atlas->AddCustomRectRegular(FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1, FONT_ATLAS_DEFAULT_TEX_DATA_H); + else + atlas->PackIdMouseCursors = atlas->AddCustomRectRegular(2, 2); + } + + // Register texture region for thick lines + // The +2 here is to give space for the end caps, whilst height +1 is to accommodate the fact we have a zero-width row + if (atlas->PackIdLines < 0) + { + if (!(atlas->Flags & ImFontAtlasFlags_NoBakedLines)) + atlas->PackIdLines = atlas->AddCustomRectRegular(IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 2, IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1); + } +} + +// This is called/shared by both the stb_truetype and the FreeType builder. void ImFontAtlasBuildFinish(ImFontAtlas* atlas) { - // Render into our custom data block + // Render into our custom data blocks + IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); ImFontAtlasBuildRenderDefaultTexData(atlas); + ImFontAtlasBuildRenderLinesTexData(atlas); // Register custom rectangle glyphs for (int i = 0; i < atlas->CustomRects.Size; i++) { - const ImFontAtlasCustomRect& r = atlas->CustomRects[i]; - if (r.Font == NULL || r.GlyphID == 0) + const ImFontAtlasCustomRect* r = &atlas->CustomRects[i]; + if (r->Font == NULL || r->GlyphID == 0) continue; - IM_ASSERT(r.Font->ContainerAtlas == atlas); + // Will ignore ImFontConfig settings: GlyphMinAdvanceX, GlyphMinAdvanceY, GlyphExtraSpacing, PixelSnapH + IM_ASSERT(r->Font->ContainerAtlas == atlas); ImVec2 uv0, uv1; - atlas->CalcCustomRectUV(&r, &uv0, &uv1); - r.Font->AddGlyph((ImWchar)r.GlyphID, r.GlyphOffset.x, r.GlyphOffset.y, r.GlyphOffset.x + r.Width, r.GlyphOffset.y + r.Height, uv0.x, uv0.y, uv1.x, uv1.y, r.GlyphAdvanceX); + atlas->CalcCustomRectUV(r, &uv0, &uv1); + r->Font->AddGlyph(NULL, (ImWchar)r->GlyphID, r->GlyphOffset.x, r->GlyphOffset.y, r->GlyphOffset.x + r->Width, r->GlyphOffset.y + r->Height, uv0.x, uv0.y, uv1.x, uv1.y, r->GlyphAdvanceX); } // Build all fonts lookup tables @@ -2646,7 +2768,6 @@ ImFont::ImFont() FallbackAdvanceX = 0.0f; FallbackChar = (ImWchar)'?'; EllipsisChar = (ImWchar)-1; - DisplayOffset = ImVec2(0.0f, 0.0f); FallbackGlyph = NULL; ContainerAtlas = NULL; ConfigData = NULL; @@ -2712,7 +2833,7 @@ void ImFont::BuildLookupTable() tab_glyph.Codepoint = '\t'; tab_glyph.AdvanceX *= IM_TABSIZE; IndexAdvanceX[(int)tab_glyph.Codepoint] = (float)tab_glyph.AdvanceX; - IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size-1); + IndexLookup[(int)tab_glyph.Codepoint] = (ImWchar)(Glyphs.Size - 1); } // Mark special glyphs as not visible (note that AddGlyph already mark as non-visible glyphs with zero-size polygons) @@ -2763,8 +2884,29 @@ void ImFont::GrowIndex(int new_size) // x0/y0/x1/y1 are offset from the character upper-left layout position, in pixels. Therefore x0/y0 are often fairly close to zero. // Not to be mistaken with texture coordinates, which are held by u0/v0/u1/v1 in normalized format (0.0..1.0 on each texture axis). -void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x) +// 'cfg' is not necessarily == 'this->ConfigData' because multiple source fonts+configs can be used to build one target font. +void ImFont::AddGlyph(const ImFontConfig* cfg, ImWchar codepoint, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x) { + if (cfg != NULL) + { + // Clamp & recenter if needed + const float advance_x_original = advance_x; + advance_x = ImClamp(advance_x, cfg->GlyphMinAdvanceX, cfg->GlyphMaxAdvanceX); + if (advance_x != advance_x_original) + { + float char_off_x = cfg->PixelSnapH ? ImFloor((advance_x - advance_x_original) * 0.5f) : (advance_x - advance_x_original) * 0.5f; + x0 += char_off_x; + x1 += char_off_x; + } + + // Snap to pixel + if (cfg->PixelSnapH) + advance_x = IM_ROUND(advance_x); + + // Bake spacing + advance_x += cfg->GlyphExtraSpacing.x; + } + Glyphs.resize(Glyphs.Size + 1); ImFontGlyph& glyph = Glyphs.back(); glyph.Codepoint = (unsigned int)codepoint; @@ -2777,14 +2919,13 @@ void ImFont::AddGlyph(ImWchar codepoint, float x0, float y0, float x1, float y1, glyph.V0 = v0; glyph.U1 = u1; glyph.V1 = v1; - glyph.AdvanceX = advance_x + ConfigData->GlyphExtraSpacing.x; // Bake spacing into AdvanceX - - if (ConfigData->PixelSnapH) - glyph.AdvanceX = IM_ROUND(glyph.AdvanceX); + glyph.AdvanceX = advance_x; // Compute rough surface usage metrics (+1 to account for average padding, +0.99 to round) + // We use (U1-U0)*TexWidth instead of X1-X0 to account for oversampling. + float pad = ContainerAtlas->TexGlyphPadding + 0.99f; DirtyLookupTables = true; - MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + 1.99f) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + 1.99f); + MetricsTotalSurface += (int)((glyph.U1 - glyph.U0) * ContainerAtlas->TexWidth + pad) * (int)((glyph.V1 - glyph.V0) * ContainerAtlas->TexHeight + pad); } void ImFont::AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst) @@ -3021,8 +3162,8 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col if (!glyph || !glyph->Visible) return; float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; - pos.x = IM_FLOOR(pos.x + DisplayOffset.x); - pos.y = IM_FLOOR(pos.y + DisplayOffset.y); + pos.x = IM_FLOOR(pos.x); + pos.y = IM_FLOOR(pos.y); draw_list->PrimReserve(6, 4); draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); } @@ -3033,8 +3174,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. // Align to be pixel perfect - pos.x = IM_FLOOR(pos.x + DisplayOffset.x); - pos.y = IM_FLOOR(pos.y + DisplayOffset.y); + pos.x = IM_FLOOR(pos.x); + pos.y = IM_FLOOR(pos.y); float x = pos.x; float y = pos.y; if (y > clip_rect.w) @@ -3207,7 +3348,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col // Give back unused vertices (clipped ones, blanks) ~ this is essentially a PrimUnreserve() action. draw_list->VtxBuffer.Size = (int)(vtx_write - draw_list->VtxBuffer.Data); // Same as calling shrink() draw_list->IdxBuffer.Size = (int)(idx_write - draw_list->IdxBuffer.Data); - draw_list->CmdBuffer[draw_list->CmdBuffer.Size-1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); + draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); draw_list->_VtxWritePtr = vtx_write; draw_list->_IdxWritePtr = idx_write; draw_list->_VtxCurrentIdx = vtx_current_idx; @@ -3293,10 +3434,10 @@ void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, Im pos -= offset; const ImTextureID tex_id = font_atlas->TexID; draw_list->PushTextureID(tex_id); - draw_list->AddImage(tex_id, pos + ImVec2(1,0)*scale, pos + ImVec2(1,0)*scale + size*scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos + ImVec2(2,0)*scale, pos + ImVec2(2,0)*scale + size*scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos, pos + size*scale, uv[2], uv[3], col_border); - draw_list->AddImage(tex_id, pos, pos + size*scale, uv[0], uv[1], col_fill); + draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill); draw_list->PopTextureID(); } } @@ -3383,6 +3524,22 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im draw_list->PathFillConvex(col); } +void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding) +{ + const bool fill_L = (inner.Min.x > outer.Min.x); + const bool fill_R = (inner.Max.x < outer.Max.x); + const bool fill_U = (inner.Min.y > outer.Min.y); + const bool fill_D = (inner.Max.y < outer.Max.y); + if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopLeft) | (fill_D ? 0 : ImDrawCornerFlags_BotLeft)); + if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopRight) | (fill_D ? 0 : ImDrawCornerFlags_BotRight)); + if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_TopLeft) | (fill_R ? 0 : ImDrawCornerFlags_TopRight)); + if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_BotLeft) | (fill_R ? 0 : ImDrawCornerFlags_BotRight)); + if (fill_L && fill_U) draw_list->AddRectFilled(ImVec2(outer.Min.x, outer.Min.y), ImVec2(inner.Min.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopLeft); + if (fill_R && fill_U) draw_list->AddRectFilled(ImVec2(inner.Max.x, outer.Min.y), ImVec2(outer.Max.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopRight); + if (fill_L && fill_D) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Max.y), ImVec2(inner.Min.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotLeft); + if (fill_R && fill_D) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Max.y), ImVec2(outer.Max.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotRight); +} + // Helper for ColorPicker4() // NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. // Spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding altogether. @@ -3551,7 +3708,7 @@ static unsigned int stb_decompress(unsigned char *output, const unsigned char *i // Exported using misc/fonts/binary_to_compressed_c.cpp (with compression + base85 string encoding). // The purpose of encoding as base85 instead of "0x00,0x01,..." style is only save on _source code_ size. //----------------------------------------------------------------------------- -static const char proggy_clean_ttf_compressed_data_base85[11980+1] = +static const char proggy_clean_ttf_compressed_data_base85[11980 + 1] = "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/" "2*>]b(MC;$jPfY.;h^`IWM9Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1=Ke$$'5F%)]0^#0X@U.a 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); + io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); g_Time = current_time; ImGui_ImplGlfw_UpdateMousePosAndButtons(); diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index 028a704b..8a1f5873 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -13,6 +13,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. +// 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. // 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. // 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix. // 2020-04-12: OpenGL: Fixed context version check mistakenly testing for 4.0+ instead of 3.2+ to enable ImGuiBackendFlags_RendererHasVtxOffset. @@ -24,7 +26,7 @@ // 2019-05-29: OpenGL: Desktop GL only: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. // 2019-04-30: OpenGL: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. // 2019-03-29: OpenGL: Not calling glBindBuffer more than necessary in the render loop. -// 2019-03-15: OpenGL: Added a dummy GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early. +// 2019-03-15: OpenGL: Added a GL call + comments in ImGui_ImplOpenGL3_Init() to detect uninitialized GL function loaders early. // 2019-03-03: OpenGL: Fix support for ES 2.0 (WebGL 1.0). // 2019-02-20: OpenGL: Fix for OSX not supporting OpenGL 4.5, we don't try to read GL_CLIP_ORIGIN even if defined by the headers/loader. // 2019-02-11: OpenGL: Projecting clipping rectangles correctly using draw_data->FramebufferScale to allow multi-viewports for retina display. @@ -80,7 +82,6 @@ #include // intptr_t #endif - // GL includes #if defined(IMGUI_IMPL_OPENGL_ES2) #include @@ -101,6 +102,8 @@ #include // Needs to be initialized with glewInit() in user's code. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) #include // Needs to be initialized with gladLoadGL() in user's code. +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) +#include // Needs to be initialized with gladLoadGL(...) or gladLoaderLoadGL() in user's code. #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) #ifndef GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. @@ -121,10 +124,13 @@ using namespace gl; #endif // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. -#if defined(IMGUI_IMPL_OPENGL_ES2) || defined(IMGUI_IMPL_OPENGL_ES3) || !defined(GL_VERSION_3_2) -#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 0 -#else -#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET 1 +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_2) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET +#endif + +// Desktop GL 3.3+ has glBindSampler() +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_3) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER #endif // OpenGL Data @@ -152,7 +158,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) // Setup back-end capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "imgui_impl_opengl3"; -#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET if (g_GlVersion >= 320) io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif @@ -176,7 +182,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) strcpy(g_GlslVersionString, glsl_version); strcat(g_GlslVersionString, "\n"); - // Dummy construct to make it easily visible in the IDE and debugger which GL loader has been selected. + // Debugging construct to make it easily visible in the IDE and debugger which GL loader has been selected. // The code actually never uses the 'gl_loader' variable! It is only here so you can read it! // If auto-detection fails or doesn't select the same GL loader file as used by your application, // you are likely to get a crash below. @@ -189,6 +195,8 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) gl_loader = "GLEW"; #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) gl_loader = "GLAD"; +#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) + gl_loader = "GLAD2"; #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) gl_loader = "glbinding2"; #elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) @@ -199,7 +207,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) gl_loader = "none"; #endif - // Make a dummy GL call (we don't actually need the result) + // Make an arbitrary GL call (we don't actually need the result) // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. GLint current_texture; @@ -258,10 +266,12 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glUseProgram(g_ShaderHandle); glUniform1i(g_AttribLocationTex, 0); glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); -#ifdef GL_SAMPLER_BINDING - glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. + +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER + if (g_GlVersion >= 330) + glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. #endif - + (void)vertex_array_object; #ifndef IMGUI_IMPL_OPENGL_ES2 glBindVertexArray(vertex_array_object); @@ -294,8 +304,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) glActiveTexture(GL_TEXTURE0); GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); -#ifdef GL_SAMPLER_BINDING - GLuint last_sampler; glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER + GLuint last_sampler; if (g_GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } #endif GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_ES2 @@ -367,7 +377,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Bind texture, Draw glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); -#if IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET if (g_GlVersion >= 320) glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); else @@ -386,8 +396,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Restore modified GL state glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); -#ifdef GL_SAMPLER_BINDING - glBindSampler(0, last_sampler); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER + if (g_GlVersion >= 330) + glBindSampler(0, last_sampler); #endif glActiveTexture(last_active_texture); #ifndef IMGUI_IMPL_OPENGL_ES2 diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index 07d35219..14eb2842 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -49,6 +49,7 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); && !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ + && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \ && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) @@ -68,6 +69,8 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #define IMGUI_IMPL_OPENGL_LOADER_GLEW #elif __has_include() #define IMGUI_IMPL_OPENGL_LOADER_GLAD +#elif __has_include() + #define IMGUI_IMPL_OPENGL_LOADER_GLAD2 #elif __has_include() #define IMGUI_IMPL_OPENGL_LOADER_GL3W #elif __has_include() diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index 9e0fe99e..ec37b38b 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -96,7 +96,7 @@ struct ImGuiContext; // Main Dear ImGui context struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box -struct ImGuiItemHoveredDataBackup; // Backup and restore IsItemHovered() internal data +struct ImGuiLastItemDataBackup; // Backup and restore IsItemHovered() internal data struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiNavMoveResult; // Result of a gamepad/keyboard directional navigation move query result struct ImGuiNextWindowData; // Storage for SetNextWindow** functions @@ -114,7 +114,6 @@ struct ImGuiWindowSettings; // Storage for a window .ini settings (we ke typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for ButtonEx(), ButtonBehavior() typedef int ImGuiColumnsFlags; // -> enum ImGuiColumnsFlags_ // Flags: BeginColumns() -typedef int ImGuiDragFlags; // -> enum ImGuiDragFlags_ // Flags: for DragBehavior() typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() @@ -123,7 +122,6 @@ typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // F typedef int ImGuiNextItemDataFlags; // -> enum ImGuiNextItemDataFlags_ // Flags: for SetNextItemXXX() functions typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// Flags: for SetNextWindowXXX() functions typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for SeparatorEx() -typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for SliderBehavior() typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() @@ -147,7 +145,7 @@ namespace ImStb #undef STB_TEXTEDIT_CHARTYPE #define STB_TEXTEDIT_STRING ImGuiInputTextState #define STB_TEXTEDIT_CHARTYPE ImWchar -#define STB_TEXTEDIT_GETWIDTH_NEWLINE -1.0f +#define STB_TEXTEDIT_GETWIDTH_NEWLINE (-1.0f) #define STB_TEXTEDIT_UNDOSTATECOUNT 99 #define STB_TEXTEDIT_UNDOCHARCOUNT 999 #include "imstb_textedit.h" @@ -163,6 +161,12 @@ namespace ImStb #define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) #endif +// Debug Logging for selected systems. Remove the '((void)0) //' to enable. +//#define IMGUI_DEBUG_LOG_POPUP IMGUI_DEBUG_LOG // Enable log +//#define IMGUI_DEBUG_LOG_NAV IMGUI_DEBUG_LOG // Enable log +#define IMGUI_DEBUG_LOG_POPUP(...) ((void)0) // Disable log +#define IMGUI_DEBUG_LOG_NAV(...) ((void)0) // Disable log + // Static Asserts #if (__cplusplus >= 201100) #define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") @@ -290,21 +294,21 @@ IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, cons // We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.) // We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. #ifdef IMGUI_DEFINE_MATH_OPERATORS -static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x*rhs, lhs.y*rhs); } -static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x/rhs, lhs.y/rhs); } -static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x+rhs.x, lhs.y+rhs.y); } -static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x-rhs.x, lhs.y-rhs.y); } -static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x*rhs.x, lhs.y*rhs.y); } -static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x/rhs.x, lhs.y/rhs.y); } +static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); } +static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); } +static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } +static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } +static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } +static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); } static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; } -static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z, lhs.w+rhs.w); } -static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z, lhs.w-rhs.w); } -static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x*rhs.x, lhs.y*rhs.y, lhs.z*rhs.z, lhs.w*rhs.w); } +static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); } +static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); } +static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); } #endif // Helpers: File System @@ -344,6 +348,12 @@ IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* #define ImCeil(X) ceilf(X) static inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision static inline double ImPow(double x, double y) { return pow(x, y); } +static inline float ImLog(float x) { return logf(x); } // DragBehaviorT/SliderBehaviorT uses ImLog with either float/double and need the precision +static inline double ImLog(double x) { return log(x); } +static inline float ImAbs(float x) { return fabsf(x); } +static inline double ImAbs(double x) { return fabs(x); } +static inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : ((x > 0.0f) ? 1.0f : 0.0f); } // Sign operator - returns -1, 0 or 1 based on sign of argument +static inline double ImSign(double x) { return (x < 0.0) ? -1.0 : ((x > 0.0) ? 1.0 : 0.0); } #endif // - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support variety of types: signed/unsigned int/long long float/double // (Exceptionally using templates here but we could also redefine them for those types) @@ -362,9 +372,9 @@ static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); } static inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); } static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } -static inline float ImLengthSqr(const ImVec2& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y; } -static inline float ImLengthSqr(const ImVec4& lhs) { return lhs.x*lhs.x + lhs.y*lhs.y + lhs.z*lhs.z + lhs.w*lhs.w; } -static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = lhs.x*lhs.x + lhs.y*lhs.y; if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; } +static inline float ImLengthSqr(const ImVec2& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y); } +static inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); } +static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; } static inline float ImFloor(float f) { return (float)(int)(f); } static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } @@ -544,6 +554,7 @@ struct IMGUI_API ImDrawListSharedData // [Internal] Lookup tables ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead) + const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas ImDrawListSharedData(); void SetCircleSegmentMaxError(float max_error); @@ -574,6 +585,7 @@ enum ImGuiItemFlags_ ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) + ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. ImGuiItemFlags_Default_ = 0 }; @@ -598,58 +610,47 @@ enum ImGuiItemStatusFlags_ #endif }; -enum ImGuiButtonFlags_ +// Extend ImGuiButtonFlags_ +enum ImGuiButtonFlagsPrivate_ { - ImGuiButtonFlags_None = 0, - ImGuiButtonFlags_Repeat = 1 << 0, // hold to repeat - ImGuiButtonFlags_PressedOnClick = 1 << 1, // return true on click (mouse down event) - ImGuiButtonFlags_PressedOnClickRelease = 1 << 2, // [Default] return true on click + release on same item <-- this is what the majority of Button are using - ImGuiButtonFlags_PressedOnClickReleaseAnywhere = 1 << 3, // return true on click + release even if the release event is not done while hovering the item - ImGuiButtonFlags_PressedOnRelease = 1 << 4, // return true on release (default requires click+release) - ImGuiButtonFlags_PressedOnDoubleClick = 1 << 5, // return true on double-click (default requires click+release) - ImGuiButtonFlags_PressedOnDragDropHold = 1 << 6, // return true when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) - ImGuiButtonFlags_FlattenChildren = 1 << 7, // allow interactions even if a child window is overlapping - ImGuiButtonFlags_AllowItemOverlap = 1 << 8, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() - ImGuiButtonFlags_DontClosePopups = 1 << 9, // disable automatically closing parent popup on press // [UNUSED] - ImGuiButtonFlags_Disabled = 1 << 10, // disable interactions - ImGuiButtonFlags_AlignTextBaseLine = 1 << 11, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine - ImGuiButtonFlags_NoKeyModifiers = 1 << 12, // disable mouse interaction if a key modifier is held - ImGuiButtonFlags_NoHoldingActiveId = 1 << 13, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) - ImGuiButtonFlags_NoNavFocus = 1 << 14, // don't override navigation focus when activated - ImGuiButtonFlags_NoHoveredOnFocus = 1 << 15, // don't report as hovered when nav focus is on this item - ImGuiButtonFlags_MouseButtonLeft = 1 << 16, // [Default] react on left mouse button - ImGuiButtonFlags_MouseButtonRight = 1 << 17, // react on right mouse button - ImGuiButtonFlags_MouseButtonMiddle = 1 << 18, // react on center mouse button - - ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, - ImGuiButtonFlags_MouseButtonShift_ = 16, - ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, + ImGuiButtonFlags_PressedOnClick = 1 << 4, // return true on click (mouse down event) + ImGuiButtonFlags_PressedOnClickRelease = 1 << 5, // [Default] return true on click + release on same item <-- this is what the majority of Button are using + ImGuiButtonFlags_PressedOnClickReleaseAnywhere = 1 << 6, // return true on click + release even if the release event is not done while hovering the item + ImGuiButtonFlags_PressedOnRelease = 1 << 7, // return true on release (default requires click+release) + ImGuiButtonFlags_PressedOnDoubleClick = 1 << 8, // return true on double-click (default requires click+release) + ImGuiButtonFlags_PressedOnDragDropHold = 1 << 9, // return true when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) + ImGuiButtonFlags_Repeat = 1 << 10, // hold to repeat + ImGuiButtonFlags_FlattenChildren = 1 << 11, // allow interactions even if a child window is overlapping + ImGuiButtonFlags_AllowItemOverlap = 1 << 12, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() + ImGuiButtonFlags_DontClosePopups = 1 << 13, // disable automatically closing parent popup on press // [UNUSED] + ImGuiButtonFlags_Disabled = 1 << 14, // disable interactions + ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine + ImGuiButtonFlags_NoKeyModifiers = 1 << 16, // disable mouse interaction if a key modifier is held + ImGuiButtonFlags_NoHoldingActiveId = 1 << 17, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) + ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated + ImGuiButtonFlags_NoHoveredOnFocus = 1 << 19, // don't report as hovered when nav focus is on this item ImGuiButtonFlags_PressedOnMask_ = ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_PressedOnDragDropHold, ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease }; -enum ImGuiSliderFlags_ -{ - ImGuiSliderFlags_None = 0, - ImGuiSliderFlags_Vertical = 1 << 0 -}; - -enum ImGuiDragFlags_ +// Extend ImGuiSliderFlags_ +enum ImGuiSliderFlagsPrivate_ { - ImGuiDragFlags_None = 0, - ImGuiDragFlags_Vertical = 1 << 0 + ImGuiSliderFlags_Vertical = 1 << 20, // Should this slider be orientated vertically? + ImGuiSliderFlags_ReadOnly = 1 << 21 }; // Extend ImGuiSelectableFlags_ enum ImGuiSelectableFlagsPrivate_ { // NB: need to be in sync with last value of ImGuiSelectableFlags_ - ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, - ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release) - ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release) - ImGuiSelectableFlags_SpanAvailWidth = 1 << 23, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) - ImGuiSelectableFlags_DrawHoveredWhenHeld= 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. - ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25 + ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, + ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release) + ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release) + ImGuiSelectableFlags_SpanAvailWidth = 1 << 23, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) + ImGuiSelectableFlags_DrawHoveredWhenHeld = 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. + ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25, // Set Nav/Focus ID on mouse hover (used by MenuItem) + ImGuiSelectableFlags_NoPadWithHalfSpacing = 1 << 26 // Disable padding each side with ItemSpacing * 0.5f }; // Extend ImGuiTreeNodeFlags_ @@ -776,7 +777,8 @@ enum ImGuiNavLayer enum ImGuiPopupPositionPolicy { ImGuiPopupPositionPolicy_Default, - ImGuiPopupPositionPolicy_ComboBox + ImGuiPopupPositionPolicy_ComboBox, + ImGuiPopupPositionPolicy_Tooltip }; struct ImGuiDataTypeTempStorage @@ -787,7 +789,8 @@ struct ImGuiDataTypeTempStorage // Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). struct ImGuiDataTypeInfo { - size_t Size; // Size in byte + size_t Size; // Size in bytes + const char* Name; // Short descriptive name for the type, for debugging const char* PrintFmt; // Default printf format for the type const char* ScanFmt; // Default scanf format for the type }; @@ -860,6 +863,7 @@ struct IMGUI_API ImGuiInputTextState float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection + bool Edited; // edited this frame ImGuiInputTextFlags UserFlags; // Temporarily set while we call user's callback ImGuiInputTextCallback UserCallback; // " void* UserCallbackData; // " @@ -1016,7 +1020,7 @@ struct ImGuiColumns float HostCursorMaxPosX; // Backup of CursorMaxPos at the time of BeginColumns() ImRect HostInitialClipRect; // Backup of ClipRect at the time of BeginColumns() ImRect HostBackupClipRect; // Backup of ClipRect during PushColumnsBackground()/PopColumnsBackground() - ImRect HostWorkRect; // Backup of WorkRect at the time of BeginColumns() + ImRect HostBackupParentWorkRect;//Backup of WorkRect at the time of BeginColumns() ImVector Columns; ImDrawListSplitter Splitter; @@ -1128,17 +1132,19 @@ struct ImGuiContext ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow* int WindowsActiveCount; // Number of unique windows submitted by frame ImGuiWindow* CurrentWindow; // Window being drawn into - ImGuiWindow* HoveredWindow; // Will catch mouse inputs - ImGuiWindow* HoveredRootWindow; // Will catch mouse inputs (for focus/move only) - ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actually window that is moved is generally MovingWindow->RootWindow. + ImGuiWindow* HoveredWindow; // Window the mouse is hovering. Will typically catch mouse inputs. + ImGuiWindow* HoveredRootWindow; // == HoveredWindow ? HoveredWindow->RootWindow : NULL, merely a shortcut to avoid null test in some situation. + ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. + ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actual window that is moved is generally MovingWindow->RootWindow. ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. ImVec2 WheelingWindowRefMousePos; float WheelingWindowTimer; // Item/widgets state and tracking information ImGuiID HoveredId; // Hovered widget - bool HoveredIdAllowOverlap; ImGuiID HoveredIdPreviousFrame; + bool HoveredIdAllowOverlap; + bool HoveredIdDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0. float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active ImGuiID ActiveId; // Active widget @@ -1146,6 +1152,7 @@ struct ImGuiContext float ActiveIdTimer; bool ActiveIdIsJustActivated; // Set at the time of activation for one frame bool ActiveIdAllowOverlap; // Active widget allows another widget to steal active id (generally for overlapping widgets, but not always) + bool ActiveIdNoClearOnFocusLoss; // Disable losing active id if the active id window gets unfocused. bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdHasBeenEditedThisFrame; @@ -1188,11 +1195,11 @@ struct ImGuiContext ImGuiKeyModFlags NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. - ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->DC.NavRefRectRel[], modified for directional navigation scoring. + ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->NavRectRel[], modified for directional navigation scoring. int NavScoringCount; // Metrics for debugging ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing - bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRefRectRel is valid + bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) bool NavDisableMouseHover; // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again. @@ -1201,7 +1208,6 @@ struct ImGuiContext bool NavInitRequestFromMove; ImGuiID NavInitResultId; // Init request result (first item of the window, or one for which SetItemDefaultFocus() was called) ImRect NavInitResultRectRel; // Init request result rectangle (relative to parent window) - bool NavMoveFromClampedRefRect; // Set by manual scrolling, if we scroll to a point where NavId isn't visible we reset navigation from visible items bool NavMoveRequest; // Move request for this frame ImGuiNavMoveFlags NavMoveRequestFlags; ImGuiNavForward NavMoveRequestForward; // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu) @@ -1274,6 +1280,8 @@ struct ImGuiContext float ColorEditLastSat; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips float ColorEditLastColor[3]; ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. + float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. + bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio @@ -1285,6 +1293,7 @@ struct ImGuiContext // Platform support ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor ImVec2 PlatformImeLastPos; + char PlatformLocaleDecimalPoint; // '.' or *localeconv()->decimal_point // Settings bool SettingsLoaded; @@ -1315,7 +1324,7 @@ struct ImGuiContext int WantCaptureMouseNextFrame; // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags int WantCaptureKeyboardNextFrame; int WantTextInputNextFrame; - char TempBuffer[1024*3+1]; // Temporary text buffer + char TempBuffer[1024 * 3 + 1]; // Temporary text buffer ImGuiContext(ImFontAtlas* shared_font_atlas) : BackgroundDrawList(&DrawListSharedData), ForegroundDrawList(&DrawListSharedData) { @@ -1336,26 +1345,28 @@ struct ImGuiContext CurrentWindow = NULL; HoveredWindow = NULL; HoveredRootWindow = NULL; + HoveredWindowUnderMovingWindow = NULL; MovingWindow = NULL; WheelingWindow = NULL; WheelingWindowTimer = 0.0f; - HoveredId = 0; + HoveredId = HoveredIdPreviousFrame = 0; HoveredIdAllowOverlap = false; - HoveredIdPreviousFrame = 0; + HoveredIdDisabled = false; HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; ActiveId = 0; ActiveIdIsAlive = 0; ActiveIdTimer = 0.0f; ActiveIdIsJustActivated = false; ActiveIdAllowOverlap = false; + ActiveIdNoClearOnFocusLoss = false; ActiveIdHasBeenPressedBefore = false; ActiveIdHasBeenEditedBefore = false; ActiveIdHasBeenEditedThisFrame = false; ActiveIdUsingNavDirMask = 0x00; ActiveIdUsingNavInputMask = 0x00; ActiveIdUsingKeyInputMask = 0x00; - ActiveIdClickOffset = ImVec2(-1,-1); + ActiveIdClickOffset = ImVec2(-1, -1); ActiveIdWindow = NULL; ActiveIdSource = ImGuiInputSource_None; ActiveIdMouseButton = 0; @@ -1383,7 +1394,6 @@ struct ImGuiContext NavInitRequest = false; NavInitRequestFromMove = false; NavInitResultId = 0; - NavMoveFromClampedRefRect = false; NavMoveRequest = false; NavMoveRequestFlags = ImGuiNavMoveFlags_None; NavMoveRequestForward = ImGuiNavForward_None; @@ -1425,6 +1435,8 @@ struct ImGuiContext ColorEditOptions = ImGuiColorEditFlags__OptionsDefault; ColorEditLastHue = ColorEditLastSat = 0.0f; ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX; + SliderCurrentAccum = 0.0f; + SliderCurrentAccumDirty = false; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; @@ -1432,6 +1444,7 @@ struct ImGuiContext TooltipOverrideCount = 0; PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); + PlatformLocaleDecimalPoint = '.'; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; @@ -1573,6 +1586,7 @@ struct IMGUI_API ImGuiWindow ImVec2 ScrollMax; ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change) ImVec2 ScrollTargetCenterRatio; // 0.0f = scroll so that target position is at top, 0.5f = scroll so that target position is centered + ImVec2 ScrollTargetEdgeSnapDist; // 0.0f = no snapping, >0.0f snapping threshold ImVec2 ScrollbarSizes; // Size taken by each scrollbars on their smaller axis. Pay attention! ScrollbarSizes.x == width of the vertical scrollbar, ScrollbarSizes.y = height of the horizontal scrollbar. bool ScrollbarX, ScrollbarY; // Are scrollbars visible? bool Active; // Set to true on Begin(), unless Collapsed @@ -1610,9 +1624,12 @@ struct IMGUI_API ImGuiWindow ImRect OuterRectClipped; // == Window->Rect() just after setup in Begin(). == window->Rect() for root window. ImRect InnerRect; // Inner rectangle (omit title bar, menu bar, scroll bar) ImRect InnerClipRect; // == InnerRect shrunk by WindowPadding*0.5f on each side, clipped within viewport or parent clip rect. - ImRect WorkRect; // Cover the whole scrolling region, shrunk by WindowPadding*1.0f on each side. This is meant to replace ContentRegionRect over time (from 1.71+ onward). + ImRect WorkRect; // Initially covers the whole scrolling region. Reduced by containers e.g columns/tables when active. Shrunk by WindowPadding*1.0f on each side. This is meant to replace ContentRegionRect over time (from 1.71+ onward). + ImRect ParentWorkRect; // Backup of WorkRect before entering a container such as columns/tables. Used by e.g. SpanAllColumns functions to easily access. Stacked containers are responsible for maintaining this. // FIXME-WORKRECT: Could be a stack? ImRect ClipRect; // Current clipping/scissoring rectangle, evolve as we are using PushClipRect(), etc. == DrawList->clip_rect_stack.back(). ImRect ContentRegionRect; // FIXME: This is currently confusing/misleading. It is essentially WorkRect but not handling of scrolling. We currently rely on it as right/bottom aligned sizing operation need some size to rely on. + ImVec2ih HitTestHoleSize; // Define an optional rectangular hole where mouse will pass-through the window. + ImVec2ih HitTestHoleOffset; int LastFrameActive; // Last frame number the window was Active. float LastTimeActive; // Last timestamp the window was Active (using float as we don't need high precision there) @@ -1625,7 +1642,7 @@ struct IMGUI_API ImGuiWindow ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) ImDrawList DrawListInst; ImGuiWindow* ParentWindow; // If we are a child _or_ popup window, this is pointing to our parent. Otherwise NULL. - ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window. + ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window == Top-level window. ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag. @@ -1650,7 +1667,7 @@ struct IMGUI_API ImGuiWindow ImGuiID GetIDFromRectangle(const ImRect& r_abs); // We don't use g.FontSize because the window may be != g.CurrentWidow. - ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } + ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } @@ -1659,14 +1676,14 @@ struct IMGUI_API ImGuiWindow }; // Backup and restore just enough data to be able to use IsItemHovered() on item A after another B in the same window has overwritten the data. -struct ImGuiItemHoveredDataBackup +struct ImGuiLastItemDataBackup { ImGuiID LastItemId; ImGuiItemStatusFlags LastItemStatusFlags; ImRect LastItemRect; ImRect LastItemDisplayRect; - ImGuiItemHoveredDataBackup() { Backup(); } + ImGuiLastItemDataBackup() { Backup(); } void Backup() { ImGuiWindow* window = GImGui->CurrentWindow; LastItemId = window->DC.LastItemId; LastItemStatusFlags = window->DC.LastItemStatusFlags; LastItemRect = window->DC.LastItemRect; LastItemDisplayRect = window->DC.LastItemDisplayRect; } void Restore() const { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.LastItemId = LastItemId; window->DC.LastItemStatusFlags = LastItemStatusFlags; window->DC.LastItemRect = LastItemRect; window->DC.LastItemDisplayRect = LastItemDisplayRect; } }; @@ -1689,19 +1706,21 @@ enum ImGuiTabItemFlagsPrivate_ ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) }; -// Storage for one active tab item (sizeof() 26~32 bytes) +// Storage for one active tab item (sizeof() 28~32 bytes) struct ImGuiTabItem { ImGuiID ID; ImGuiTabItemFlags Flags; int LastFrameVisible; int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance - int NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames float Offset; // Position relative to beginning of tab float Width; // Width currently displayed float ContentWidth; // Width of actual contents, stored during BeginTabItem() call + ImS16 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames + ImS8 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable + bool WantClose; // Marked as closed by SetTabItemClosed() - ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; } + ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; BeginOrder = -1; WantClose = false; } }; // Storage for a tab bar (sizeof() 92~96 bytes) @@ -1716,8 +1735,8 @@ struct ImGuiTabBar int PrevFrameVisible; ImRect BarRect; float LastTabContentHeight; // Record the height of contents submitted below the tab bar - float OffsetMax; // Distance from BarRect.Min.x, locked during layout - float OffsetMaxIdeal; // Ideal offset if all tabs were visible and not clipped + float WidthAllTabs; // Actual width of all tabs (locked during layout) + float WidthAllTabsIdeal; // Ideal width if all tabs were visible and not clipped float OffsetNextTab; // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set. float ScrollingAnim; float ScrollingTarget; @@ -1726,9 +1745,10 @@ struct ImGuiTabBar ImGuiTabBarFlags Flags; ImGuiID ReorderRequestTabId; ImS8 ReorderRequestDir; + ImS8 TabsActiveCount; // Number of tabs submitted this frame. bool WantLayout; bool VisibleTabWasSubmitted; - short LastTabItemIdx; // For BeginTabItem()/EndTabItem() + short LastTabItemIdx; // Index of last BeginTabItem() tab for use by EndTabItem() ImVec2 FramePadding; // style.FramePadding locked at the time of BeginTabBar() ImGuiTextBuffer TabsNames; // For non-docking tab bar we re-append names in a contiguous buffer. @@ -1736,7 +1756,7 @@ struct ImGuiTabBar int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); } const char* GetTabName(const ImGuiTabItem* tab) const { - IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size); + IM_ASSERT(tab->NameOffset != -1 && (int)tab->NameOffset < TabsNames.Buf.Size); return TabsNames.Buf.Data + tab->NameOffset; } }; @@ -1773,6 +1793,7 @@ namespace ImGui IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); + IMGUI_API void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); // Windows: Display Order and Focus Order IMGUI_API void FocusWindow(ImGuiWindow* window); @@ -1807,10 +1828,10 @@ namespace ImGui // Scrolling IMGUI_API void SetNextWindowScroll(const ImVec2& scroll); // Use -1.0f on one axis to leave as-is - IMGUI_API void SetScrollX(ImGuiWindow* window, float new_scroll_x); - IMGUI_API void SetScrollY(ImGuiWindow* window, float new_scroll_y); - IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio = 0.5f); - IMGUI_API void SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio = 0.5f); + IMGUI_API void SetScrollX(ImGuiWindow* window, float scroll_x); + IMGUI_API void SetScrollY(ImGuiWindow* window, float scroll_y); + IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio); + IMGUI_API void SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio); IMGUI_API ImVec2 ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect); // Basic Accessors @@ -1833,6 +1854,7 @@ namespace ImGui IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); + IMGUI_API void SetLastItemData(ImGuiWindow* window, ImGuiID item_id, ImGuiItemStatusFlags status_flags, const ImRect& item_rect); IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id); // Return true if focus is requested IMGUI_API void FocusableItemUnregister(ImGuiWindow* window); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); @@ -1850,17 +1872,15 @@ namespace ImGui // Popups, Modals, Tooltips IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); - IMGUI_API void OpenPopupEx(ImGuiID id); + IMGUI_API void OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags = ImGuiPopupFlags_None); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); - IMGUI_API bool IsPopupOpen(ImGuiID id); // Test for id at the current BeginPopup() level of the popup stack (this doesn't scan the whole popup stack!) - IMGUI_API bool IsPopupOpenAtAnyLevel(ImGuiID id); - IMGUI_API bool IsAnyPopupOpen(); // Return true if any popup is open at the current BeginPopup() level of the popup stack + IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); - IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy = ImGuiPopupPositionPolicy_Default); + IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); // Gamepad/Keyboard Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); @@ -1915,7 +1935,8 @@ namespace ImGui IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); - IMGUI_API void TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir); + IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir); + IMGUI_API bool TabBarProcessReorder(ImGuiTabBar* tab_bar); IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); @@ -1943,6 +1964,7 @@ namespace ImGui IMGUI_API void RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col); IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding); + IMGUI_API void RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // [1.71: 2019/06/07: Updating prototypes of some of the internal functions. Leaving those for reference for a short while] @@ -1958,6 +1980,7 @@ namespace ImGui IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawCornerFlags rounding_corners); + IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col); IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowResizeID(ImGuiWindow* window, int n); // 0..3: corners, 4..7: borders @@ -1965,8 +1988,8 @@ namespace ImGui // Widgets low-level behaviors IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); - IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, float power, ImGuiDragFlags flags); - IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb); + IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags); + IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); IMGUI_API bool SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f); IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL); IMGUI_API bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextItemOpen() data, if any. May return true when logging @@ -1975,16 +1998,18 @@ namespace ImGui // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. // To use them externally (for custom widget) you may need an "extern template" statement in your code in order to link to existing instances and silence Clang warnings (see #2036). // e.g. " extern template IMGUI_API float RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, float v); " - template IMGUI_API bool DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, float power, ImGuiDragFlags flags); - template IMGUI_API bool SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb); - template IMGUI_API float SliderCalcRatioFromValueT(ImGuiDataType data_type, T v, T v_min, T v_max, float power, float linear_zero_pos); + template IMGUI_API float ScaleRatioFromValueT(ImGuiDataType data_type, T v, T v_min, T v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_size); + template IMGUI_API T ScaleValueFromRatioT(ImGuiDataType data_type, float t, T v_min, T v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_size); + template IMGUI_API bool DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, ImGuiSliderFlags flags); + template IMGUI_API bool SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); // Data type helpers IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format); - IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg_1, const void* arg_2); + IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const void* arg_1, const void* arg_2); IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format); + IMGUI_API int DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2); IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max); // InputText @@ -2022,6 +2047,7 @@ IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildRender1bppRectFromString(ImFontAtlas* atlas, int atlas_x, int atlas_y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value); IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); @@ -2029,8 +2055,8 @@ IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned ch // [SECTION] Test Engine Hooks (imgui_test_engine) //----------------------------------------------------------------------------- -//#define IMGUI_ENABLE_TEST_ENGINE #ifdef IMGUI_ENABLE_TEST_ENGINE +extern void ImGuiTestEngineHook_Shutdown(ImGuiContext* ctx); extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index 70ab9e3d..466bc803 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.77 WIP +// dear imgui, v1.79 WIP // (widgets code) /* @@ -160,7 +160,7 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) // - We use memchr(), pay attention that well optimized versions of those str/mem functions are much faster than a casually written loop. const char* line = text; const float line_height = GetTextLineHeight(); - ImVec2 text_size(0,0); + ImVec2 text_size(0, 0); // Lines to skip (can't skip when logging text) ImVec2 pos = text_pos; @@ -274,7 +274,10 @@ void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args) { PushStyleColor(ImGuiCol_Text, col); - TextV(fmt, args); + if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) + TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting + else + TextV(fmt, args); PopStyleColor(); } @@ -288,8 +291,12 @@ void ImGui::TextDisabled(const char* fmt, ...) void ImGui::TextDisabledV(const char* fmt, va_list args) { - PushStyleColor(ImGuiCol_Text, GImGui->Style.Colors[ImGuiCol_TextDisabled]); - TextV(fmt, args); + ImGuiContext& g = *GImGui; + PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); + if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) + TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting + else + TextV(fmt, args); PopStyleColor(); } @@ -303,11 +310,14 @@ void ImGui::TextWrapped(const char* fmt, ...) void ImGui::TextWrappedV(const char* fmt, va_list args) { - ImGuiWindow* window = GetCurrentWindow(); - bool need_backup = (window->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set + ImGuiContext& g = *GImGui; + bool need_backup = (g.CurrentWindow->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set if (need_backup) PushTextWrapPos(0.0f); - TextV(fmt, args); + if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) + TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting + else + TextV(fmt, args); if (need_backup) PopTextWrapPos(); } @@ -332,8 +342,8 @@ void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) const float w = CalcItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2)); - const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y*2) + label_size); + const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2)); + const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y * 2) + label_size); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0)) return; @@ -341,7 +351,7 @@ void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) // Render const char* value_text_begin = &g.TempBuffer[0]; const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); - RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f,0.5f)); + RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f, 0.5f)); if (label_size.x > 0.0f) RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label); } @@ -377,7 +387,7 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // Render ImU32 text_col = GetColorU32(ImGuiCol_Text); - RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, g.FontSize*0.5f), text_col); + RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize * 0.5f, g.FontSize * 0.5f), text_col); RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0f), text_begin, text_end, false); } @@ -595,6 +605,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool } } + // Process while held bool held = false; if (g.ActiveId == id) { @@ -615,6 +626,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool bool release_anywhere = (flags & ImGuiButtonFlags_PressedOnClickReleaseAnywhere) != 0; if ((release_in || release_anywhere) && !g.DragDropActive) { + // Report as pressed when releasing the mouse (this is the most common path) bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[mouse_button]; bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps if (!is_double_click_release && !is_repeating_already) @@ -627,6 +639,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { + // When activated using Nav, we hold on the ActiveID until activation button is released if (g.NavActivateDownId != id) ClearActiveID(); } @@ -682,7 +695,7 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags bool ImGui::Button(const char* label, const ImVec2& size_arg) { - return ButtonEx(label, size_arg, 0); + return ButtonEx(label, size_arg, ImGuiButtonFlags_None); } // Small buttons fits within text without additional vertical spacing. @@ -698,7 +711,7 @@ bool ImGui::SmallButton(const char* label) // Tip: use ImGui::PushID()/PopID() to push indices or pointers in the ID stack. // Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id) -bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) +bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg, ImGuiButtonFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -715,7 +728,7 @@ bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg) return false; bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held); + bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); return pressed; } @@ -781,8 +794,8 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)//, float size) float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; ImU32 cross_col = GetColorU32(ImGuiCol_Text); center -= ImVec2(0.5f, 0.5f); - window->DrawList->AddLine(center + ImVec2(+cross_extent,+cross_extent), center + ImVec2(-cross_extent,-cross_extent), cross_col, 1.0f); - window->DrawList->AddLine(center + ImVec2(+cross_extent,-cross_extent), center + ImVec2(-cross_extent,+cross_extent), cross_col, 1.0f); + window->DrawList->AddLine(center + ImVec2(+cross_extent, +cross_extent), center + ImVec2(-cross_extent, -cross_extent), cross_col, 1.0f); + window->DrawList->AddLine(center + ImVec2(+cross_extent, -cross_extent), center + ImVec2(-cross_extent, +cross_extent), cross_col, 1.0f); return pressed; } @@ -981,28 +994,16 @@ void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& } } -// frame_padding < 0: uses FramePadding from style (default) -// frame_padding = 0: no framing -// frame_padding > 0: set framing size -// The color used are the button colors. -bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) +// ImageButton() is flawed as 'id' is always derived from 'texture_id' (see #2464 #1390) +// We provide this internal helper to write your own variant while we figure out how to redesign the public ImageButton() API. +bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col) { + ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - ImGuiContext& g = *GImGui; - const ImGuiStyle& style = g.Style; - - // Default to using texture ID as ID. User can still push string/integer prefixes. - // We could hash the size/uv to create a unique ID but that would prevent the user from animating UV. - PushID((void*)(intptr_t)user_texture_id); - const ImGuiID id = window->GetID("#image"); - PopID(); - - const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : style.FramePadding; const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2); - const ImRect image_bb(window->DC.CursorPos + padding, window->DC.CursorPos + padding + size); ItemSize(bb); if (!ItemAdd(bb, id)) return false; @@ -1013,14 +1014,33 @@ bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const I // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); - RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, style.FrameRounding)); + RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, g.Style.FrameRounding)); if (bg_col.w > 0.0f) - window->DrawList->AddRectFilled(image_bb.Min, image_bb.Max, GetColorU32(bg_col)); - window->DrawList->AddImage(user_texture_id, image_bb.Min, image_bb.Max, uv0, uv1, GetColorU32(tint_col)); + window->DrawList->AddRectFilled(bb.Min + padding, bb.Max - padding, GetColorU32(bg_col)); + window->DrawList->AddImage(texture_id, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col)); return pressed; } +// frame_padding < 0: uses FramePadding from style (default) +// frame_padding = 0: no framing +// frame_padding > 0: set framing size +bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + // Default to using texture ID as ID. User can still push string/integer prefixes. + PushID((void*)(intptr_t)user_texture_id); + const ImGuiID id = window->GetID("#image"); + PopID(); + + const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : g.Style.FramePadding; + return ImageButtonEx(id, user_texture_id, size, uv0, uv1, padding, bg_col, tint_col); +} + bool ImGui::Checkbox(const char* label, bool* v) { ImGuiWindow* window = GetCurrentWindow(); @@ -1060,7 +1080,7 @@ bool ImGui::Checkbox(const char* label, bool* v) else if (*v) { const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); - RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad*2.0f); + RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad * 2.0f); } if (g.LogEnabled) @@ -1126,7 +1146,7 @@ bool ImGui::RadioButton(const char* label, bool active) if (style.FrameBorderSize > 0.0f) { - window->DrawList->AddCircle(center + ImVec2(1,1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); + window->DrawList->AddCircle(center + ImVec2(1, 1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); } @@ -1159,7 +1179,7 @@ void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* over const ImGuiStyle& style = g.Style; ImVec2 pos = window->DC.CursorPos; - ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), g.FontSize + style.FramePadding.y*2.0f); + ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), g.FontSize + style.FramePadding.y * 2.0f); ImRect bb(pos, pos + size); ItemSize(size, style.FramePadding.y); if (!ItemAdd(bb, 0)) @@ -1176,13 +1196,13 @@ void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* over char overlay_buf[32]; if (!overlay) { - ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction*100+0.01f); + ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction * 100 + 0.01f); overlay = overlay_buf; } ImVec2 overlay_size = CalcTextSize(overlay, NULL); if (overlay_size.x > 0.0f) - RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f,0.5f), &bb); + RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f, 0.5f), &bb); } void ImGui::Bullet() @@ -1193,18 +1213,18 @@ void ImGui::Bullet() ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; - const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y*2), g.FontSize); + const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y * 2), g.FontSize); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height)); ItemSize(bb); if (!ItemAdd(bb, 0)) { - SameLine(0, style.FramePadding.x*2); + SameLine(0, style.FramePadding.x * 2); return; } // Render and stay on same line ImU32 text_col = GetColorU32(ImGuiCol_Text); - RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize*0.5f, line_height*0.5f), text_col); + RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize * 0.5f, line_height * 0.5f), text_col); SameLine(0, style.FramePadding.x * 2.0f); } @@ -1477,7 +1497,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF const ImVec2 label_size = CalcTextSize(label, NULL, true); const float expected_w = CalcItemWidth(); const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w; - const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id, &frame_bb)) @@ -1485,7 +1505,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF bool hovered, held; bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held); - bool popup_open = IsPopupOpen(id); + bool popup_open = IsPopupOpen(id, ImGuiPopupFlags_None); const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); @@ -1502,7 +1522,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF } RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) - RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f,0.0f)); + RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f, 0.0f)); if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); @@ -1510,7 +1530,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF { if (window->DC.NavLayerCurrent == 0) window->NavLastIds[0] = id; - OpenPopupEx(id); + OpenPopupEx(id, ImGuiPopupFlags_None); popup_open = true; } @@ -1677,21 +1697,21 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa static const ImGuiDataTypeInfo GDataTypeInfo[] = { - { sizeof(char), "%d", "%d" }, // ImGuiDataType_S8 - { sizeof(unsigned char), "%u", "%u" }, - { sizeof(short), "%d", "%d" }, // ImGuiDataType_S16 - { sizeof(unsigned short), "%u", "%u" }, - { sizeof(int), "%d", "%d" }, // ImGuiDataType_S32 - { sizeof(unsigned int), "%u", "%u" }, + { sizeof(char), "S8", "%d", "%d" }, // ImGuiDataType_S8 + { sizeof(unsigned char), "U8", "%u", "%u" }, + { sizeof(short), "S16", "%d", "%d" }, // ImGuiDataType_S16 + { sizeof(unsigned short), "U16", "%u", "%u" }, + { sizeof(int), "S32", "%d", "%d" }, // ImGuiDataType_S32 + { sizeof(unsigned int), "U32", "%u", "%u" }, #ifdef _MSC_VER - { sizeof(ImS64), "%I64d","%I64d" }, // ImGuiDataType_S64 - { sizeof(ImU64), "%I64u","%I64u" }, + { sizeof(ImS64), "S64", "%I64d","%I64d" }, // ImGuiDataType_S64 + { sizeof(ImU64), "U64", "%I64u","%I64u" }, #else - { sizeof(ImS64), "%lld", "%lld" }, // ImGuiDataType_S64 - { sizeof(ImU64), "%llu", "%llu" }, + { sizeof(ImS64), "S64", "%lld", "%lld" }, // ImGuiDataType_S64 + { sizeof(ImU64), "U64", "%llu", "%llu" }, #endif - { sizeof(float), "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) - { sizeof(double), "%f", "%lf" }, // ImGuiDataType_Double + { sizeof(float), "float", "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) + { sizeof(double), "double","%f", "%lf" }, // ImGuiDataType_Double }; IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); @@ -1748,7 +1768,7 @@ int ImGui::DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type return 0; } -void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, void* arg1, const void* arg2) +void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const void* arg1, const void* arg2) { IM_ASSERT(op == '+' || op == '-'); switch (data_type) @@ -1900,10 +1920,39 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b } template -static bool ClampBehaviorT(T* v, T v_min, T v_max) +static int DataTypeCompareT(const T* lhs, const T* rhs) { - if (*v < v_min) { *v = v_min; return true; } - if (*v > v_max) { *v = v_max; return true; } + if (*lhs < *rhs) return -1; + if (*lhs > *rhs) return +1; + return 0; +} + +int ImGui::DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2) +{ + switch (data_type) + { + case ImGuiDataType_S8: return DataTypeCompareT((const ImS8* )arg_1, (const ImS8* )arg_2); + case ImGuiDataType_U8: return DataTypeCompareT((const ImU8* )arg_1, (const ImU8* )arg_2); + case ImGuiDataType_S16: return DataTypeCompareT((const ImS16* )arg_1, (const ImS16* )arg_2); + case ImGuiDataType_U16: return DataTypeCompareT((const ImU16* )arg_1, (const ImU16* )arg_2); + case ImGuiDataType_S32: return DataTypeCompareT((const ImS32* )arg_1, (const ImS32* )arg_2); + case ImGuiDataType_U32: return DataTypeCompareT((const ImU32* )arg_1, (const ImU32* )arg_2); + case ImGuiDataType_S64: return DataTypeCompareT((const ImS64* )arg_1, (const ImS64* )arg_2); + case ImGuiDataType_U64: return DataTypeCompareT((const ImU64* )arg_1, (const ImU64* )arg_2); + case ImGuiDataType_Float: return DataTypeCompareT((const float* )arg_1, (const float* )arg_2); + case ImGuiDataType_Double: return DataTypeCompareT((const double*)arg_1, (const double*)arg_2); + case ImGuiDataType_COUNT: break; + } + IM_ASSERT(0); + return 0; +} + +template +static bool DataTypeClampT(T* v, const T* v_min, const T* v_max) +{ + // Clamp, both sides are optional, return true if modified + if (v_min && *v < *v_min) { *v = *v_min; return true; } + if (v_max && *v > *v_max) { *v = *v_max; return true; } return false; } @@ -1911,16 +1960,16 @@ bool ImGui::DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_m { switch (data_type) { - case ImGuiDataType_S8: return ClampBehaviorT((ImS8* )p_data, *(const ImS8* )p_min, *(const ImS8* )p_max); - case ImGuiDataType_U8: return ClampBehaviorT((ImU8* )p_data, *(const ImU8* )p_min, *(const ImU8* )p_max); - case ImGuiDataType_S16: return ClampBehaviorT((ImS16* )p_data, *(const ImS16* )p_min, *(const ImS16* )p_max); - case ImGuiDataType_U16: return ClampBehaviorT((ImU16* )p_data, *(const ImU16* )p_min, *(const ImU16* )p_max); - case ImGuiDataType_S32: return ClampBehaviorT((ImS32* )p_data, *(const ImS32* )p_min, *(const ImS32* )p_max); - case ImGuiDataType_U32: return ClampBehaviorT((ImU32* )p_data, *(const ImU32* )p_min, *(const ImU32* )p_max); - case ImGuiDataType_S64: return ClampBehaviorT((ImS64* )p_data, *(const ImS64* )p_min, *(const ImS64* )p_max); - case ImGuiDataType_U64: return ClampBehaviorT((ImU64* )p_data, *(const ImU64* )p_min, *(const ImU64* )p_max); - case ImGuiDataType_Float: return ClampBehaviorT((float* )p_data, *(const float* )p_min, *(const float* )p_max); - case ImGuiDataType_Double: return ClampBehaviorT((double*)p_data, *(const double*)p_min, *(const double*)p_max); + case ImGuiDataType_S8: return DataTypeClampT((ImS8* )p_data, (const ImS8* )p_min, (const ImS8* )p_max); + case ImGuiDataType_U8: return DataTypeClampT((ImU8* )p_data, (const ImU8* )p_min, (const ImU8* )p_max); + case ImGuiDataType_S16: return DataTypeClampT((ImS16* )p_data, (const ImS16* )p_min, (const ImS16* )p_max); + case ImGuiDataType_U16: return DataTypeClampT((ImU16* )p_data, (const ImU16* )p_min, (const ImU16* )p_max); + case ImGuiDataType_S32: return DataTypeClampT((ImS32* )p_data, (const ImS32* )p_min, (const ImS32* )p_max); + case ImGuiDataType_U32: return DataTypeClampT((ImU32* )p_data, (const ImU32* )p_min, (const ImU32* )p_max); + case ImGuiDataType_S64: return DataTypeClampT((ImS64* )p_data, (const ImS64* )p_min, (const ImS64* )p_max); + case ImGuiDataType_U64: return DataTypeClampT((ImU64* )p_data, (const ImU64* )p_min, (const ImU64* )p_max); + case ImGuiDataType_Float: return DataTypeClampT((float* )p_data, (const float* )p_min, (const float* )p_max); + case ImGuiDataType_Double: return DataTypeClampT((double*)p_data, (const double*)p_min, (const double*)p_max); case ImGuiDataType_COUNT: break; } IM_ASSERT(0); @@ -1987,16 +2036,13 @@ TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, // This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls) template -bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiDragFlags flags) +bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, ImGuiSliderFlags flags) { ImGuiContext& g = *GImGui; - const ImGuiAxis axis = (flags & ImGuiDragFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; + const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool is_clamped = (v_min < v_max); - const bool is_power = (power != 1.0f && is_decimal && is_clamped && (v_max - v_min < FLT_MAX)); - const bool is_locked = (v_min > v_max); - if (is_locked) - return false; + const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; // Default tweak speed if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) @@ -2004,7 +2050,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings float adjust_delta = 0.0f; - if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f*1.0f) + if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f * 1.0f) { adjust_delta = g.IO.MouseDelta[axis]; if (g.IO.KeyAlt) @@ -2024,12 +2070,15 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const if (axis == ImGuiAxis_Y) adjust_delta = -adjust_delta; + // For logarithmic use our range is effectively 0..1 so scale the delta into that range + if (is_logarithmic && (v_max - v_min < FLT_MAX) && ((v_max - v_min) > 0.000001f)) // Epsilon to avoid /0 + adjust_delta /= (float)(v_max - v_min); + // Clear current value on activation // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300. bool is_just_activated = g.ActiveIdIsJustActivated; bool is_already_past_limits_and_pushing_outward = is_clamped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f)); - bool is_drag_direction_change_with_power = is_power && ((adjust_delta < 0 && g.DragCurrentAccum > 0) || (adjust_delta > 0 && g.DragCurrentAccum < 0)); - if (is_just_activated || is_already_past_limits_and_pushing_outward || is_drag_direction_change_with_power) + if (is_just_activated || is_already_past_limits_and_pushing_outward) { g.DragCurrentAccum = 0.0f; g.DragCurrentAccumDirty = false; @@ -2046,13 +2095,19 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_cur = *v; FLOATTYPE v_old_ref_for_accum_remainder = (FLOATTYPE)0.0f; - if (is_power) + float logarithmic_zero_epsilon = 0.0f; // Only valid when is_logarithmic is true + const float zero_deadzone_halfsize = 0.0f; // Drag widgets have no deadzone (as it doesn't make sense) + if (is_logarithmic) { - // Offset + round to user desired precision, with a curve on the v_min..v_max range to get more precision on one side of the range - FLOATTYPE v_old_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); - FLOATTYPE v_new_norm_curved = v_old_norm_curved + (g.DragCurrentAccum / (v_max - v_min)); - v_cur = v_min + (SIGNEDTYPE)ImPow(ImSaturate((float)v_new_norm_curved), power) * (v_max - v_min); - v_old_ref_for_accum_remainder = v_old_norm_curved; + // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. + const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; + logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); + + // Convert to parametric space, apply delta, convert back + float v_old_parametric = ScaleRatioFromValueT(data_type, v_cur, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + float v_new_parametric = v_old_parametric + g.DragCurrentAccum; + v_cur = ScaleValueFromRatioT(data_type, v_new_parametric, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + v_old_ref_for_accum_remainder = v_old_parametric; } else { @@ -2060,14 +2115,16 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const } // Round to user desired precision based on format string - v_cur = RoundScalarWithFormatT(format, data_type, v_cur); + if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_cur = RoundScalarWithFormatT(format, data_type, v_cur); // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. g.DragCurrentAccumDirty = false; - if (is_power) + if (is_logarithmic) { - FLOATTYPE v_cur_norm_curved = ImPow((FLOATTYPE)(v_cur - v_min) / (FLOATTYPE)(v_max - v_min), (FLOATTYPE)1.0f / power); - g.DragCurrentAccum -= (float)(v_cur_norm_curved - v_old_ref_for_accum_remainder); + // Convert to parametric space, apply delta, convert back + float v_new_parametric = ScaleRatioFromValueT(data_type, v_cur, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + g.DragCurrentAccum -= (float)(v_new_parametric - v_old_ref_for_accum_remainder); } else { @@ -2094,8 +2151,11 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const return true; } -bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, float power, ImGuiDragFlags flags) +bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { + // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. + IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flags! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + ImGuiContext& g = *GImGui; if (g.ActiveId == id) { @@ -2106,19 +2166,21 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v } if (g.ActiveId != id) return false; + if ((g.CurrentWindow->DC.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + return false; switch (data_type) { - case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS8*) p_min : IM_S8_MIN, p_max ? *(const ImS8*)p_max : IM_S8_MAX, format, power, flags); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } - case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU8*) p_min : IM_U8_MIN, p_max ? *(const ImU8*)p_max : IM_U8_MAX, format, power, flags); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } - case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS16*)p_min : IM_S16_MIN, p_max ? *(const ImS16*)p_max : IM_S16_MAX, format, power, flags); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } - case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU16*)p_min : IM_U16_MIN, p_max ? *(const ImU16*)p_max : IM_U16_MAX, format, power, flags); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } - case ImGuiDataType_S32: return DragBehaviorT(data_type, (ImS32*)p_v, v_speed, p_min ? *(const ImS32* )p_min : IM_S32_MIN, p_max ? *(const ImS32* )p_max : IM_S32_MAX, format, power, flags); - case ImGuiDataType_U32: return DragBehaviorT(data_type, (ImU32*)p_v, v_speed, p_min ? *(const ImU32* )p_min : IM_U32_MIN, p_max ? *(const ImU32* )p_max : IM_U32_MAX, format, power, flags); - case ImGuiDataType_S64: return DragBehaviorT(data_type, (ImS64*)p_v, v_speed, p_min ? *(const ImS64* )p_min : IM_S64_MIN, p_max ? *(const ImS64* )p_max : IM_S64_MAX, format, power, flags); - case ImGuiDataType_U64: return DragBehaviorT(data_type, (ImU64*)p_v, v_speed, p_min ? *(const ImU64* )p_min : IM_U64_MIN, p_max ? *(const ImU64* )p_max : IM_U64_MAX, format, power, flags); - case ImGuiDataType_Float: return DragBehaviorT(data_type, (float*)p_v, v_speed, p_min ? *(const float* )p_min : -FLT_MAX, p_max ? *(const float* )p_max : FLT_MAX, format, power, flags); - case ImGuiDataType_Double: return DragBehaviorT(data_type, (double*)p_v, v_speed, p_min ? *(const double*)p_min : -DBL_MAX, p_max ? *(const double*)p_max : DBL_MAX, format, power, flags); + case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS8*) p_min : IM_S8_MIN, p_max ? *(const ImS8*)p_max : IM_S8_MAX, format, flags); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } + case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU8*) p_min : IM_U8_MIN, p_max ? *(const ImU8*)p_max : IM_U8_MAX, format, flags); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } + case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = DragBehaviorT(ImGuiDataType_S32, &v32, v_speed, p_min ? *(const ImS16*)p_min : IM_S16_MIN, p_max ? *(const ImS16*)p_max : IM_S16_MAX, format, flags); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } + case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = DragBehaviorT(ImGuiDataType_U32, &v32, v_speed, p_min ? *(const ImU16*)p_min : IM_U16_MIN, p_max ? *(const ImU16*)p_max : IM_U16_MAX, format, flags); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } + case ImGuiDataType_S32: return DragBehaviorT(data_type, (ImS32*)p_v, v_speed, p_min ? *(const ImS32* )p_min : IM_S32_MIN, p_max ? *(const ImS32* )p_max : IM_S32_MAX, format, flags); + case ImGuiDataType_U32: return DragBehaviorT(data_type, (ImU32*)p_v, v_speed, p_min ? *(const ImU32* )p_min : IM_U32_MIN, p_max ? *(const ImU32* )p_max : IM_U32_MAX, format, flags); + case ImGuiDataType_S64: return DragBehaviorT(data_type, (ImS64*)p_v, v_speed, p_min ? *(const ImS64* )p_min : IM_S64_MIN, p_max ? *(const ImS64* )p_max : IM_S64_MAX, format, flags); + case ImGuiDataType_U64: return DragBehaviorT(data_type, (ImU64*)p_v, v_speed, p_min ? *(const ImU64* )p_min : IM_U64_MIN, p_max ? *(const ImU64* )p_max : IM_U64_MAX, format, flags); + case ImGuiDataType_Float: return DragBehaviorT(data_type, (float*)p_v, v_speed, p_min ? *(const float* )p_min : -FLT_MAX, p_max ? *(const float* )p_max : FLT_MAX, format, flags); + case ImGuiDataType_Double: return DragBehaviorT(data_type, (double*)p_v, v_speed, p_min ? *(const double*)p_min : -DBL_MAX, p_max ? *(const double*)p_max : DBL_MAX, format, flags); case ImGuiDataType_COUNT: break; } IM_ASSERT(0); @@ -2126,22 +2188,19 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v } // Note: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a Drag widget, p_min and p_max are optional. -// Read code of e.g. SliderFloat(), SliderInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. -bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +// Read code of e.g. DragFloat(), DragInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. +bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - if (power != 1.0f) - IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds - ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const float w = CalcItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); ItemSize(total_bb, style.FramePadding.y); @@ -2156,11 +2215,11 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, // Tabbing or CTRL-clicking on Drag turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); - bool temp_input_is_active = TempInputIsActive(id); - bool temp_input_start = false; + const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; + bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = FocusableItemRegister(window, id); + const bool focus_requested = temp_input_allowed && FocusableItemRegister(window, id); const bool clicked = (hovered && g.IO.MouseClicked[0]); const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) @@ -2169,17 +2228,20 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, SetFocusID(id, window); FocusWindow(window); g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id) + if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id)) { - temp_input_start = true; + temp_input_is_active = true; FocusableItemUnregister(window); } } } - // Our current specs do NOT clamp when using CTRL+Click manual input, but we should eventually add a flag for that.. - if (temp_input_is_active || temp_input_start) - return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max); + if (temp_input_is_active) + { + // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampInput is set + const bool is_clamp_input = (flags & ImGuiSliderFlags_ClampOnInput) != 0 && (p_min == NULL || p_max == NULL || DataTypeCompare(data_type, p_min, p_max) < 0); + return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); + } // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2187,7 +2249,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); // Drag behavior - const bool value_changed = DragBehavior(id, data_type, p_data, v_speed, p_min, p_max, format, power, ImGuiDragFlags_None); + const bool value_changed = DragBehavior(id, data_type, p_data, v_speed, p_min, p_max, format, flags); if (value_changed) MarkItemEdited(id); @@ -2203,7 +2265,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, return value_changed; } -bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2220,7 +2282,7 @@ bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data PushID(i); if (i > 0) SameLine(0, g.Style.ItemInnerSpacing.x); - value_changed |= DragScalar("", data_type, p_data, v_speed, p_min, p_max, format, power); + value_changed |= DragScalar("", data_type, p_data, v_speed, p_min, p_max, format, flags); PopID(); PopItemWidth(); p_data = (void*)((char*)p_data + type_size); @@ -2238,27 +2300,28 @@ bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data return value_changed; } -bool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) +bool ImGui::DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); + return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) +bool ImGui::DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); + return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) +bool ImGui::DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); + return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) +bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); + return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, float power) +// NB: You likely want to specify the ImGuiSliderFlags_ClampOnInput when using this. +bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2269,10 +2332,17 @@ bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_cu BeginGroup(); PushMultiItemsWidths(2, CalcItemWidth()); - bool value_changed = DragFloat("##min", v_current_min, v_speed, (v_min >= v_max) ? -FLT_MAX : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format, power); + float min_min = (v_min >= v_max) ? -FLT_MAX : v_min; + float min_max = (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max); + ImGuiSliderFlags min_flags = flags | ((min_min == min_max) ? ImGuiSliderFlags_ReadOnly : 0); + bool value_changed = DragScalar("##min", ImGuiDataType_Float, v_current_min, v_speed, &min_min, &min_max, format, min_flags); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); - value_changed |= DragFloat("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? FLT_MAX : v_max, format_max ? format_max : format, power); + + float max_min = (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min); + float max_max = (v_min >= v_max) ? FLT_MAX : v_max; + ImGuiSliderFlags max_flags = flags | ((max_min == max_max) ? ImGuiSliderFlags_ReadOnly : 0); + value_changed |= DragScalar("##max", ImGuiDataType_Float, v_current_max, v_speed, &max_min, &max_max, format_max ? format_max : format, max_flags); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); @@ -2283,27 +2353,28 @@ bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_cu } // NB: v_speed is float to allow adjusting the drag speed with more precision -bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format) +bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format); + return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format) +bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format); + return DragScalarN(label, ImGuiDataType_S32, v, 2, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format) +bool ImGui::DragInt3(const char* label, int v[3], float v_speed, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format); + return DragScalarN(label, ImGuiDataType_S32, v, 3, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format) +bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format); + return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format, flags); } -bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max) +// NB: You likely want to specify the ImGuiSliderFlags_ClampOnInput when using this. +bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2314,10 +2385,17 @@ bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_ BeginGroup(); PushMultiItemsWidths(2, CalcItemWidth()); - bool value_changed = DragInt("##min", v_current_min, v_speed, (v_min >= v_max) ? INT_MIN : v_min, (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max), format); + int min_min = (v_min >= v_max) ? INT_MIN : v_min; + int min_max = (v_min >= v_max) ? *v_current_max : ImMin(v_max, *v_current_max); + ImGuiSliderFlags min_flags = flags | ((min_min == min_max) ? ImGuiSliderFlags_ReadOnly : 0); + bool value_changed = DragInt("##min", v_current_min, v_speed, min_min, min_max, format, min_flags); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); - value_changed |= DragInt("##max", v_current_max, v_speed, (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min), (v_min >= v_max) ? INT_MAX : v_max, format_max ? format_max : format); + + int max_min = (v_min >= v_max) ? *v_current_min : ImMax(v_min, *v_current_min); + int max_max = (v_min >= v_max) ? INT_MAX : v_max; + ImGuiSliderFlags max_flags = flags | ((max_min == max_max) ? ImGuiSliderFlags_ReadOnly : 0); + value_changed |= DragInt("##max", v_current_max, v_speed, max_min, max_max, format_max ? format_max : format, max_flags); PopItemWidth(); SameLine(0, g.Style.ItemInnerSpacing.x); @@ -2328,9 +2406,40 @@ bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_ return value_changed; } +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +// Obsolete versions with power parameter. See https://github.com/ocornut/imgui/issues/3361 for details. +bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiSliderFlags drag_flags = ImGuiSliderFlags_None; + if (power != 1.0f) + { + IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); + IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds + drag_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths + } + return DragScalar(label, data_type, p_data, v_speed, p_min, p_max, format, drag_flags); +} + +bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiSliderFlags drag_flags = ImGuiSliderFlags_None; + if (power != 1.0f) + { + IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); + IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds + drag_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths + } + return DragScalarN(label, data_type, p_data, components, v_speed, p_min, p_max, format, drag_flags); +} + +#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //------------------------------------------------------------------------- // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. //------------------------------------------------------------------------- +// - ScaleRatioFromValueT<> [Internal] +// - ScaleValueFromRatioT<> [Internal] // - SliderBehaviorT<>() [Internal] // - SliderBehavior() [Internal] // - SliderScalar() @@ -2349,42 +2458,151 @@ bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_ // - VSliderInt() //------------------------------------------------------------------------- -template -float ImGui::SliderCalcRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos) +// Convert a value v in the output space of a slider into a parametric position on the slider itself (the logical opposite of ScaleValueFromRatioT) +template +float ImGui::ScaleRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize) { if (v_min == v_max) return 0.0f; + IM_UNUSED(data_type); - const bool is_power = (power != 1.0f) && (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double); const TYPE v_clamped = (v_min < v_max) ? ImClamp(v, v_min, v_max) : ImClamp(v, v_max, v_min); - if (is_power) + if (is_logarithmic) { - if (v_clamped < 0.0f) + bool flipped = v_max < v_min; + + if (flipped) // Handle the case where the range is backwards + ImSwap(v_min, v_max); + + // Fudge min/max to avoid getting close to log(0) + FLOATTYPE v_min_fudged = (ImAbs((FLOATTYPE)v_min) < logarithmic_zero_epsilon) ? ((v_min < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_min; + FLOATTYPE v_max_fudged = (ImAbs((FLOATTYPE)v_max) < logarithmic_zero_epsilon) ? ((v_max < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_max; + + // Awkward special cases - we need ranges of the form (-100 .. 0) to convert to (-100 .. -epsilon), not (-100 .. epsilon) + if ((v_min == 0.0f) && (v_max < 0.0f)) + v_min_fudged = -logarithmic_zero_epsilon; + else if ((v_max == 0.0f) && (v_min < 0.0f)) + v_max_fudged = -logarithmic_zero_epsilon; + + float result; + + if (v_clamped <= v_min_fudged) + result = 0.0f; // Workaround for values that are in-range but below our fudge + else if (v_clamped >= v_max_fudged) + result = 1.0f; // Workaround for values that are in-range but above our fudge + else if ((v_min * v_max) < 0.0f) // Range crosses zero, so split into two portions { - const float f = 1.0f - (float)((v_clamped - v_min) / (ImMin((TYPE)0, v_max) - v_min)); - return (1.0f - ImPow(f, 1.0f/power)) * linear_zero_pos; + float zero_point_center = (-(float)v_min) / ((float)v_max - (float)v_min); // The zero point in parametric space. There's an argument we should take the logarithmic nature into account when calculating this, but for now this should do (and the most common case of a symmetrical range works fine) + float zero_point_snap_L = zero_point_center - zero_deadzone_halfsize; + float zero_point_snap_R = zero_point_center + zero_deadzone_halfsize; + if (v == 0.0f) + result = zero_point_center; // Special case for exactly zero + else if (v < 0.0f) + result = (1.0f - (float)(ImLog(-(FLOATTYPE)v_clamped / logarithmic_zero_epsilon) / ImLog(-v_min_fudged / logarithmic_zero_epsilon))) * zero_point_snap_L; + else + result = zero_point_snap_R + ((float)(ImLog((FLOATTYPE)v_clamped / logarithmic_zero_epsilon) / ImLog(v_max_fudged / logarithmic_zero_epsilon)) * (1.0f - zero_point_snap_R)); } + else if ((v_min < 0.0f) || (v_max < 0.0f)) // Entirely negative slider + result = 1.0f - (float)(ImLog(-(FLOATTYPE)v_clamped / -v_max_fudged) / ImLog(-v_min_fudged / -v_max_fudged)); + else + result = (float)(ImLog((FLOATTYPE)v_clamped / v_min_fudged) / ImLog(v_max_fudged / v_min_fudged)); + + return flipped ? (1.0f - result) : result; + } + + // Linear slider + return (float)((FLOATTYPE)(SIGNEDTYPE)(v_clamped - v_min) / (FLOATTYPE)(SIGNEDTYPE)(v_max - v_min)); +} + +// Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT) +template +TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize) +{ + if (v_min == v_max) + return (TYPE)0.0f; + const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + + TYPE result; + if (is_logarithmic) + { + // We special-case the extents because otherwise our fudging can lead to "mathematically correct" but non-intuitive behaviors like a fully-left slider not actually reaching the minimum value + if (t <= 0.0f) + result = v_min; + else if (t >= 1.0f) + result = v_max; else { - const float f = (float)((v_clamped - ImMax((TYPE)0, v_min)) / (v_max - ImMax((TYPE)0, v_min))); - return linear_zero_pos + ImPow(f, 1.0f/power) * (1.0f - linear_zero_pos); + bool flipped = v_max < v_min; // Check if range is "backwards" + + // Fudge min/max to avoid getting silly results close to zero + FLOATTYPE v_min_fudged = (ImAbs((FLOATTYPE)v_min) < logarithmic_zero_epsilon) ? ((v_min < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_min; + FLOATTYPE v_max_fudged = (ImAbs((FLOATTYPE)v_max) < logarithmic_zero_epsilon) ? ((v_max < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_max; + + if (flipped) + ImSwap(v_min_fudged, v_max_fudged); + + // Awkward special case - we need ranges of the form (-100 .. 0) to convert to (-100 .. -epsilon), not (-100 .. epsilon) + if ((v_max == 0.0f) && (v_min < 0.0f)) + v_max_fudged = -logarithmic_zero_epsilon; + + float t_with_flip = flipped ? (1.0f - t) : t; // t, but flipped if necessary to account for us flipping the range + + if ((v_min * v_max) < 0.0f) // Range crosses zero, so we have to do this in two parts + { + float zero_point_center = (-(float)ImMin(v_min, v_max)) / ImAbs((float)v_max - (float)v_min); // The zero point in parametric space + float zero_point_snap_L = zero_point_center - zero_deadzone_halfsize; + float zero_point_snap_R = zero_point_center + zero_deadzone_halfsize; + if (t_with_flip >= zero_point_snap_L && t_with_flip <= zero_point_snap_R) + result = (TYPE)0.0f; // Special case to make getting exactly zero possible (the epsilon prevents it otherwise) + else if (t_with_flip < zero_point_center) + result = (TYPE)-(logarithmic_zero_epsilon * ImPow(-v_min_fudged / logarithmic_zero_epsilon, (FLOATTYPE)(1.0f - (t_with_flip / zero_point_snap_L)))); + else + result = (TYPE)(logarithmic_zero_epsilon * ImPow(v_max_fudged / logarithmic_zero_epsilon, (FLOATTYPE)((t_with_flip - zero_point_snap_R) / (1.0f - zero_point_snap_R)))); + } + else if ((v_min < 0.0f) || (v_max < 0.0f)) // Entirely negative slider + result = (TYPE)-(-v_max_fudged * ImPow(-v_min_fudged / -v_max_fudged, (FLOATTYPE)(1.0f - t_with_flip))); + else + result = (TYPE)(v_min_fudged * ImPow(v_max_fudged / v_min_fudged, (FLOATTYPE)t_with_flip)); + } + } + else + { + // Linear slider + if (is_decimal) + { + result = ImLerp(v_min, v_max, t); + } + else + { + // - For integer values we want the clicking position to match the grab box so we round above + // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property.. + // - Not doing a *1.0 multiply at the end of a range as it tends to be lossy. While absolute aiming at a large s64/u64 + // range is going to be imprecise anyway, with this check we at least make the edge values matches expected limits. + if (t < 1.0) + { + FLOATTYPE v_new_off_f = (SIGNEDTYPE)(v_max - v_min) * t; + result = (TYPE)((SIGNEDTYPE)v_min + (SIGNEDTYPE)(v_new_off_f + (FLOATTYPE)(v_min > v_max ? -0.5 : 0.5))); + } + else + { + result = v_max; + } } } - // Linear slider - return (float)((FLOATTYPE)(v_clamped - v_min) / (FLOATTYPE)(v_max - v_min)); + return result; } -// FIXME: Move some of the code into SliderBehavior(). Current responsibility is larger than what the equivalent DragBehaviorT<> does, we also do some rendering, etc. +// FIXME: Move more of the code into SliderBehavior() template -bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) +bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb) { ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); - const bool is_power = (power != 1.0f) && is_decimal; + const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; const float grab_padding = 2.0f; const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; @@ -2397,19 +2615,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f; const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f; - // For power curve sliders that cross over sign boundary we want the curve to be symmetric around 0.0f - float linear_zero_pos; // 0.0->1.0f - if (is_power && v_min * v_max < 0.0f) - { - // Different sign - const FLOATTYPE linear_dist_min_to_0 = ImPow(v_min >= 0 ? (FLOATTYPE)v_min : -(FLOATTYPE)v_min, (FLOATTYPE)1.0f / power); - const FLOATTYPE linear_dist_max_to_0 = ImPow(v_max >= 0 ? (FLOATTYPE)v_max : -(FLOATTYPE)v_max, (FLOATTYPE)1.0f / power); - linear_zero_pos = (float)(linear_dist_min_to_0 / (linear_dist_min_to_0 + linear_dist_max_to_0)); - } - else + float logarithmic_zero_epsilon = 0.0f; // Only valid when is_logarithmic is true + float zero_deadzone_halfsize = 0.0f; // Only valid when is_logarithmic is true + if (is_logarithmic) { - // Same sign - linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f; + // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. + const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; + logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); + zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f); } // Process interacting with the slider @@ -2435,87 +2648,80 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { - const ImVec2 delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f); - float delta = (axis == ImGuiAxis_X) ? delta2.x : -delta2.y; - if (g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) + if (g.ActiveIdIsJustActivated) { - ClearActiveID(); + g.SliderCurrentAccum = 0.0f; // Reset any stored nav delta upon activation + g.SliderCurrentAccumDirty = false; } - else if (delta != 0.0f) + + const ImVec2 input_delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f); + float input_delta = (axis == ImGuiAxis_X) ? input_delta2.x : -input_delta2.y; + if (input_delta != 0.0f) { - clicked_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; - if ((decimal_precision > 0) || is_power) + if (decimal_precision > 0) { - delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds + input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds if (IsNavInputDown(ImGuiNavInput_TweakSlow)) - delta /= 10.0f; + input_delta /= 10.0f; } else { if ((v_range >= -100.0f && v_range <= 100.0f) || IsNavInputDown(ImGuiNavInput_TweakSlow)) - delta = ((delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps + input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps else - delta /= 100.0f; + input_delta /= 100.0f; } if (IsNavInputDown(ImGuiNavInput_TweakFast)) - delta *= 10.0f; - set_new_value = true; - if ((clicked_t >= 1.0f && delta > 0.0f) || (clicked_t <= 0.0f && delta < 0.0f)) // This is to avoid applying the saturation when already past the limits - set_new_value = false; - else - clicked_t = ImSaturate(clicked_t + delta); + input_delta *= 10.0f; + + g.SliderCurrentAccum += input_delta; + g.SliderCurrentAccumDirty = true; } - } - if (set_new_value) - { - TYPE v_new; - if (is_power) + float delta = g.SliderCurrentAccum; + if (g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) { - // Account for power curve scale on both sides of the zero - if (clicked_t < linear_zero_pos) - { - // Negative: rescale to the negative range before powering - float a = 1.0f - (clicked_t / linear_zero_pos); - a = ImPow(a, power); - v_new = ImLerp(ImMin(v_max, (TYPE)0), v_min, a); - } - else - { - // Positive: rescale to the positive range before powering - float a; - if (ImFabs(linear_zero_pos - 1.0f) > 1.e-6f) - a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos); - else - a = clicked_t; - a = ImPow(a, power); - v_new = ImLerp(ImMax(v_min, (TYPE)0), v_max, a); - } + ClearActiveID(); } - else + else if (g.SliderCurrentAccumDirty) { - // Linear slider - if (is_decimal) + clicked_t = ScaleRatioFromValueT(data_type, *v, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + + if ((clicked_t >= 1.0f && delta > 0.0f) || (clicked_t <= 0.0f && delta < 0.0f)) // This is to avoid applying the saturation when already past the limits { - v_new = ImLerp(v_min, v_max, clicked_t); + set_new_value = false; + g.SliderCurrentAccum = 0.0f; // If pushing up against the limits, don't continue to accumulate } else { - // For integer values we want the clicking position to match the grab box so we round above - // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property.. - FLOATTYPE v_new_off_f = (v_max - v_min) * clicked_t; - TYPE v_new_off_floor = (TYPE)(v_new_off_f); - TYPE v_new_off_round = (TYPE)(v_new_off_f + (FLOATTYPE)0.5); - if (v_new_off_floor < v_new_off_round) - v_new = v_min + v_new_off_round; + set_new_value = true; + float old_clicked_t = clicked_t; + clicked_t = ImSaturate(clicked_t + delta); + + // Calculate what our "new" clicked_t will be, and thus how far we actually moved the slider, and subtract this from the accumulator + TYPE v_new = ScaleValueFromRatioT(data_type, clicked_t, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_new = RoundScalarWithFormatT(format, data_type, v_new); + float new_clicked_t = ScaleRatioFromValueT(data_type, v_new, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + + if (delta > 0) + g.SliderCurrentAccum -= ImMin(new_clicked_t - old_clicked_t, delta); else - v_new = v_min + v_new_off_floor; + g.SliderCurrentAccum -= ImMax(new_clicked_t - old_clicked_t, delta); } + + g.SliderCurrentAccumDirty = false; } + } + + if (set_new_value) + { + TYPE v_new = ScaleValueFromRatioT(data_type, clicked_t, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); // Round to user desired precision based on format string - v_new = RoundScalarWithFormatT(format, data_type, v_new); + if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_new = RoundScalarWithFormatT(format, data_type, v_new); // Apply result if (*v != v_new) @@ -2533,7 +2739,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ else { // Output grab position so it can be displayed by the caller - float grab_t = SliderCalcRatioFromValueT(data_type, *v, v_min, v_max, power, linear_zero_pos); + float grab_t = ScaleRatioFromValueT(data_type, *v, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); if (axis == ImGuiAxis_Y) grab_t = 1.0f - grab_t; const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t); @@ -2549,32 +2755,39 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ // For 32-bit and larger types, slider bounds are limited to half the natural type range. // So e.g. an integer Slider between INT_MAX-10 and INT_MAX will fail, but an integer Slider between INT_MAX/2-10 and INT_MAX/2 will be ok. // It would be possible to lift that limitation with some work but it doesn't seem to be worth it for sliders. -bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, float power, ImGuiSliderFlags flags, ImRect* out_grab_bb) +bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb) { + // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. + IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flag! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + + ImGuiContext& g = *GImGui; + if ((g.CurrentWindow->DC.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + return false; + switch (data_type) { - case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS8*)p_min, *(const ImS8*)p_max, format, power, flags, out_grab_bb); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } - case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU8*)p_min, *(const ImU8*)p_max, format, power, flags, out_grab_bb); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } - case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS16*)p_min, *(const ImS16*)p_max, format, power, flags, out_grab_bb); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } - case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU16*)p_min, *(const ImU16*)p_max, format, power, flags, out_grab_bb); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } + case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS8*)p_min, *(const ImS8*)p_max, format, flags, out_grab_bb); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } + case ImGuiDataType_U8: { ImU32 v32 = (ImU32)*(ImU8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU8*)p_min, *(const ImU8*)p_max, format, flags, out_grab_bb); if (r) *(ImU8*)p_v = (ImU8)v32; return r; } + case ImGuiDataType_S16: { ImS32 v32 = (ImS32)*(ImS16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS16*)p_min, *(const ImS16*)p_max, format, flags, out_grab_bb); if (r) *(ImS16*)p_v = (ImS16)v32; return r; } + case ImGuiDataType_U16: { ImU32 v32 = (ImU32)*(ImU16*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_U32, &v32, *(const ImU16*)p_min, *(const ImU16*)p_max, format, flags, out_grab_bb); if (r) *(ImU16*)p_v = (ImU16)v32; return r; } case ImGuiDataType_S32: - IM_ASSERT(*(const ImS32*)p_min >= IM_S32_MIN/2 && *(const ImS32*)p_max <= IM_S32_MAX/2); - return SliderBehaviorT(bb, id, data_type, (ImS32*)p_v, *(const ImS32*)p_min, *(const ImS32*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const ImS32*)p_min >= IM_S32_MIN / 2 && *(const ImS32*)p_max <= IM_S32_MAX / 2); + return SliderBehaviorT(bb, id, data_type, (ImS32*)p_v, *(const ImS32*)p_min, *(const ImS32*)p_max, format, flags, out_grab_bb); case ImGuiDataType_U32: - IM_ASSERT(*(const ImU32*)p_max <= IM_U32_MAX/2); - return SliderBehaviorT(bb, id, data_type, (ImU32*)p_v, *(const ImU32*)p_min, *(const ImU32*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const ImU32*)p_max <= IM_U32_MAX / 2); + return SliderBehaviorT(bb, id, data_type, (ImU32*)p_v, *(const ImU32*)p_min, *(const ImU32*)p_max, format, flags, out_grab_bb); case ImGuiDataType_S64: - IM_ASSERT(*(const ImS64*)p_min >= IM_S64_MIN/2 && *(const ImS64*)p_max <= IM_S64_MAX/2); - return SliderBehaviorT(bb, id, data_type, (ImS64*)p_v, *(const ImS64*)p_min, *(const ImS64*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const ImS64*)p_min >= IM_S64_MIN / 2 && *(const ImS64*)p_max <= IM_S64_MAX / 2); + return SliderBehaviorT(bb, id, data_type, (ImS64*)p_v, *(const ImS64*)p_min, *(const ImS64*)p_max, format, flags, out_grab_bb); case ImGuiDataType_U64: - IM_ASSERT(*(const ImU64*)p_max <= IM_U64_MAX/2); - return SliderBehaviorT(bb, id, data_type, (ImU64*)p_v, *(const ImU64*)p_min, *(const ImU64*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const ImU64*)p_max <= IM_U64_MAX / 2); + return SliderBehaviorT(bb, id, data_type, (ImU64*)p_v, *(const ImU64*)p_min, *(const ImU64*)p_max, format, flags, out_grab_bb); case ImGuiDataType_Float: - IM_ASSERT(*(const float*)p_min >= -FLT_MAX/2.0f && *(const float*)p_max <= FLT_MAX/2.0f); - return SliderBehaviorT(bb, id, data_type, (float*)p_v, *(const float*)p_min, *(const float*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const float*)p_min >= -FLT_MAX / 2.0f && *(const float*)p_max <= FLT_MAX / 2.0f); + return SliderBehaviorT(bb, id, data_type, (float*)p_v, *(const float*)p_min, *(const float*)p_max, format, flags, out_grab_bb); case ImGuiDataType_Double: - IM_ASSERT(*(const double*)p_min >= -DBL_MAX/2.0f && *(const double*)p_max <= DBL_MAX/2.0f); - return SliderBehaviorT(bb, id, data_type, (double*)p_v, *(const double*)p_min, *(const double*)p_max, format, power, flags, out_grab_bb); + IM_ASSERT(*(const double*)p_min >= -DBL_MAX / 2.0f && *(const double*)p_max <= DBL_MAX / 2.0f); + return SliderBehaviorT(bb, id, data_type, (double*)p_v, *(const double*)p_min, *(const double*)p_max, format, flags, out_grab_bb); case ImGuiDataType_COUNT: break; } IM_ASSERT(0); @@ -2583,7 +2796,7 @@ bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type // Note: p_data, p_min and p_max are _pointers_ to a memory address holding the data. For a slider, they are all required. // Read code of e.g. SliderFloat(), SliderInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. -bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) +bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2595,7 +2808,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat const float w = CalcItemWidth(); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*2.0f)); + const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); ItemSize(total_bb, style.FramePadding.y); @@ -2610,11 +2823,11 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Tabbing or CTRL-clicking on Slider turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); - bool temp_input_is_active = TempInputIsActive(id); - bool temp_input_start = false; + const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; + bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = FocusableItemRegister(window, id); + const bool focus_requested = temp_input_allowed && FocusableItemRegister(window, id); const bool clicked = (hovered && g.IO.MouseClicked[0]); if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id) { @@ -2622,17 +2835,20 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat SetFocusID(id, window); FocusWindow(window); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id) + if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id)) { - temp_input_start = true; + temp_input_is_active = true; FocusableItemUnregister(window); } } } - // Our current specs do NOT clamp when using CTRL+Click manual input, but we should eventually add a flag for that.. - if (temp_input_is_active || temp_input_start) - return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max); + if (temp_input_is_active) + { + // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampInput is set + const bool is_clamp_input = (flags & ImGuiSliderFlags_ClampOnInput) != 0; + return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); + } // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); @@ -2641,7 +2857,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Slider behavior ImRect grab_bb; - const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, power, ImGuiSliderFlags_None, &grab_bb); + const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, flags, &grab_bb); if (value_changed) MarkItemEdited(id); @@ -2652,7 +2868,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); - RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.5f)); + RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); @@ -2662,7 +2878,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat } // Add multiple sliders on 1 line for compact edition of multiple components -bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power) +bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2679,7 +2895,7 @@ bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, i PushID(i); if (i > 0) SameLine(0, g.Style.ItemInnerSpacing.x); - value_changed |= SliderScalar("", data_type, v, v_min, v_max, format, power); + value_changed |= SliderScalar("", data_type, v, v_min, v_max, format, flags); PopID(); PopItemWidth(); v = (void*)((char*)v + type_size); @@ -2697,57 +2913,57 @@ bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, i return value_changed; } -bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) +bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); + return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, flags); } -bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) +bool ImGui::SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); + return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, flags); } -bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) +bool ImGui::SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); + return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, flags); } -bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) +bool ImGui::SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); + return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, flags); } -bool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format) +bool ImGui::SliderAngle(const char* label, float* v_rad, float v_degrees_min, float v_degrees_max, const char* format, ImGuiSliderFlags flags) { if (format == NULL) format = "%.0f deg"; - float v_deg = (*v_rad) * 360.0f / (2*IM_PI); - bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, 1.0f); - *v_rad = v_deg * (2*IM_PI) / 360.0f; + float v_deg = (*v_rad) * 360.0f / (2 * IM_PI); + bool value_changed = SliderFloat(label, &v_deg, v_degrees_min, v_degrees_max, format, flags); + *v_rad = v_deg * (2 * IM_PI) / 360.0f; return value_changed; } -bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format) +bool ImGui::SliderInt(const char* label, int* v, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalar(label, ImGuiDataType_S32, v, &v_min, &v_max, format); + return SliderScalar(label, ImGuiDataType_S32, v, &v_min, &v_max, format, flags); } -bool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format) +bool ImGui::SliderInt2(const char* label, int v[2], int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_S32, v, 2, &v_min, &v_max, format); + return SliderScalarN(label, ImGuiDataType_S32, v, 2, &v_min, &v_max, format, flags); } -bool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format) +bool ImGui::SliderInt3(const char* label, int v[3], int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_S32, v, 3, &v_min, &v_max, format); + return SliderScalarN(label, ImGuiDataType_S32, v, 3, &v_min, &v_max, format, flags); } -bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format) +bool ImGui::SliderInt4(const char* label, int v[4], int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return SliderScalarN(label, ImGuiDataType_S32, v, 4, &v_min, &v_max, format); + return SliderScalarN(label, ImGuiDataType_S32, v, 4, &v_min, &v_max, format, flags); } -bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) +bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -2787,7 +3003,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d // Slider behavior ImRect grab_bb; - const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, power, ImGuiSliderFlags_Vertical, &grab_bb); + const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, flags | ImGuiSliderFlags_Vertical, &grab_bb); if (value_changed) MarkItemEdited(id); @@ -2799,23 +3015,50 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d // For the vertical slider we allow centered text to overlap the frame padding char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); - RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f,0.0f)); + RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.0f)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); return value_changed; } -bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format, float power) +bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* format, ImGuiSliderFlags flags) { - return VSliderScalar(label, size, ImGuiDataType_Float, v, &v_min, &v_max, format, power); + return VSliderScalar(label, size, ImGuiDataType_Float, v, &v_min, &v_max, format, flags); } -bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format) +bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, int v_max, const char* format, ImGuiSliderFlags flags) { - return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format); + return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format, flags); } +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +// Obsolete versions with power parameter. See https://github.com/ocornut/imgui/issues/3361 for details. +bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) +{ + ImGuiSliderFlags slider_flags = ImGuiSliderFlags_None; + if (power != 1.0f) + { + IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); + slider_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths + } + return SliderScalar(label, data_type, p_data, p_min, p_max, format, slider_flags); +} + +bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power) +{ + ImGuiSliderFlags slider_flags = ImGuiSliderFlags_None; + if (power != 1.0f) + { + IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); + slider_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths + } + return SliderScalarN(label, data_type, v, components, v_min, v_max, format, slider_flags); +} + +#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //------------------------------------------------------------------------- // [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. //------------------------------------------------------------------------- @@ -2931,20 +3174,10 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* return value_changed; } -// Note that Drag/Slider functions are currently NOT forwarding the min/max values clamping values! +// Note that Drag/Slider functions are only forwarding the min/max values clamping values if the +// ImGuiSliderFlags_ClampOnInput / ImGuiSliderFlags_ClampOnInput flag is set! // This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility. // However this may not be ideal for all uses, as some user code may break on out of bound values. -// In the future we should add flags to Slider/Drag to specify how to enforce min/max values with CTRL+Click. -// See GitHub issues #1829 and #3209 -// In the meanwhile, you can easily "wrap" those functions to enforce clamping, using wrapper functions, e.g. -// bool SliderFloatClamp(const char* label, float* v, float v_min, float v_max) -// { -// float v_backup = *v; -// if (!SliderFloat(label, v, v_min, v_max)) -// return false; -// *v = ImClamp(*v, v_min, v_max); -// return v_backup != *v; -// } bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max) { ImGuiContext& g = *GImGui; @@ -2967,8 +3200,12 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG // Apply new value (or operations) then clamp DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); - if (p_clamp_min && p_clamp_max) + if (p_clamp_min || p_clamp_max) + { + if (DataTypeCompare(data_type, p_clamp_min, p_clamp_max) > 0) + ImSwap(p_clamp_min, p_clamp_max); DataTypeClamp(data_type, p_data, p_clamp_min, p_clamp_max); + } // Only mark as edited if new value is different value_changed = memcmp(&data_backup, p_data, data_type_size) != 0; @@ -3090,7 +3327,7 @@ bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* p_dat bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags) { flags |= ImGuiInputTextFlags_CharsScientific; - return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step>0.0f ? &step : NULL), (void*)(step_fast>0.0f ? &step_fast : NULL), format, flags); + return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step > 0.0f ? &step : NULL), (void*)(step_fast > 0.0f ? &step_fast : NULL), format, flags); } bool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags flags) @@ -3147,7 +3384,7 @@ bool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiIn { // Hexadecimal input provided as a convenience but the flag name is awkward. Typically you'd use InputText() to parse your own data, if you want to handle prefixes. const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? "%08X" : "%d"; - return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step>0 ? &step : NULL), (void*)(step_fast>0 ? &step_fast : NULL), format, flags); + return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step > 0 ? &step : NULL), (void*)(step_fast > 0 ? &step_fast : NULL), format, flags); } bool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags flags) @@ -3168,7 +3405,7 @@ bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags) bool ImGui::InputDouble(const char* label, double* v, double step, double step_fast, const char* format, ImGuiInputTextFlags flags) { flags |= ImGuiInputTextFlags_CharsScientific; - return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step>0.0 ? &step : NULL), (void*)(step_fast>0.0 ? &step_fast : NULL), format, flags); + return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step > 0.0 ? &step : NULL), (void*)(step_fast > 0.0 ? &step_fast : NULL), format, flags); } //------------------------------------------------------------------------- @@ -3279,10 +3516,10 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* ob } static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } -static int is_word_boundary_from_right(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (is_separator( obj->TextW[idx-1] ) && !is_separator( obj->TextW[idx] ) ) : 1; } +static int is_word_boundary_from_right(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (is_separator(obj->TextW[idx - 1]) && !is_separator(obj->TextW[idx]) ) : 1; } static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } #ifdef __APPLE__ // FIXME: Move setting to IO structure -static int is_word_boundary_from_left(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (!is_separator( obj->TextW[idx-1] ) && is_separator( obj->TextW[idx] ) ) : 1; } +static int is_word_boundary_from_left(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx]) ) : 1; } static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } #else static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } @@ -3295,6 +3532,7 @@ static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) ImWchar* dst = obj->TextW.Data + pos; // We maintain our buffer length in both UTF-8 and wchar formats + obj->Edited = true; obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n); obj->CurLenW -= n; @@ -3329,6 +3567,7 @@ static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const Im memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar)); memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar)); + obj->Edited = true; obj->CurLenW += new_text_len; obj->CurLenA += new_text_len_utf8; obj->TextW[obj->CurLenW] = '\0'; @@ -3399,7 +3638,7 @@ void ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count) *dst++ = c; *dst = '\0'; - if (CursorPos + bytes_count >= pos) + if (CursorPos >= pos + bytes_count) CursorPos -= bytes_count; else if (CursorPos >= pos) CursorPos = pos; @@ -3470,14 +3709,22 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Generic named filters if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) { + // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf. + // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. + // We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point. + // Change the default decimal_point with: + // ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; + ImGuiContext& g = *GImGui; + const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint; + // Allow 0-9 . - + * / if (flags & ImGuiInputTextFlags_CharsDecimal) - if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/')) + if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/')) return false; // Allow 0-9 . - + * / e E if (flags & ImGuiInputTextFlags_CharsScientific) - if (!(c >= '0' && c <= '9') && (c != '.') && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E')) + if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E')) return false; // Allow 0-9 a-F A-F @@ -3488,7 +3735,7 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Turn a-z into A-Z if (flags & ImGuiInputTextFlags_CharsUppercase) if (c >= 'a' && c <= 'z') - *p_char = (c += (unsigned int)('A'-'a')); + *p_char = (c += (unsigned int)('A' - 'a')); if (flags & ImGuiInputTextFlags_CharsNoBlank) if (ImCharIsBlankW(c)) @@ -3549,7 +3796,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ BeginGroup(); const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const ImVec2 frame_size = CalcItemSize(size_arg, CalcItemWidth(), (is_multiline ? g.FontSize * 8.0f : label_size.y) + style.FramePadding.y*2.0f); // Arbitrary default of 8 lines high for multi-line + const ImVec2 frame_size = CalcItemSize(size_arg, CalcItemWidth(), (is_multiline ? g.FontSize * 8.0f : label_size.y) + style.FramePadding.y * 2.0f); // Arbitrary default of 8 lines high for multi-line const ImVec2 total_size = ImVec2(frame_size.x + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), frame_size.y); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); @@ -3711,7 +3958,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImFont* password_font = &g.InputTextPasswordFont; password_font->FontSize = g.Font->FontSize; password_font->Scale = g.Font->Scale; - password_font->DisplayOffset = g.Font->DisplayOffset; password_font->Ascent = g.Font->Ascent; password_font->Descent = g.Font->Descent; password_font->ContainerAtlas = g.Font->ContainerAtlas; @@ -3727,6 +3973,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { IM_ASSERT(state != NULL); backup_current_text_length = state->CurLenA; + state->Edited = false; state->BufCapacityA = buf_size; state->UserFlags = flags; state->UserCallback = callback; @@ -3739,7 +3986,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Edit in progress const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; - const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize*0.5f)); + const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize * 0.5f)); const bool is_osx = io.ConfigMacOSXBehaviors; if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0])) @@ -3835,9 +4082,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!state->HasSelection()) { if (is_wordmove_key_down) - state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT|STB_TEXTEDIT_K_SHIFT); + state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT); else if (is_osx && io.KeySuper && !io.KeyAlt && !io.KeyCtrl) - state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART|STB_TEXTEDIT_K_SHIFT); + state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT); } state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); } @@ -3896,7 +4143,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Filter pasted buffer const int clipboard_len = (int)strlen(clipboard); - ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len+1) * sizeof(ImWchar)); + ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len + 1) * sizeof(ImWchar)); int clipboard_filtered_len = 0; for (const char* s = clipboard; *s; ) { @@ -3964,7 +4211,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // User callback - if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackAlways)) != 0) + if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackEdit | ImGuiInputTextFlags_CallbackAlways)) != 0) { IM_ASSERT(callback != NULL); @@ -3986,8 +4233,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ event_flag = ImGuiInputTextFlags_CallbackHistory; event_key = ImGuiKey_DownArrow; } + else if ((flags & ImGuiInputTextFlags_CallbackEdit) && state->Edited) + { + event_flag = ImGuiInputTextFlags_CallbackEdit; + } else if (flags & ImGuiInputTextFlags_CallbackAlways) + { event_flag = ImGuiInputTextFlags_CallbackAlways; + } if (event_flag) { @@ -4189,11 +4442,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Vertical scroll if (is_multiline) { + // Test if cursor is vertically visible float scroll_y = draw_window->Scroll.y; + const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f); if (cursor_offset.y - g.FontSize < scroll_y) scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize); else if (cursor_offset.y - inner_size.y >= scroll_y) - scroll_y = cursor_offset.y - inner_size.y; + scroll_y = cursor_offset.y - inner_size.y + style.FramePadding.y * 2.0f; + scroll_y = ImClamp(scroll_y, 0.0f, scroll_max_y); draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag draw_window->Scroll.y = scroll_y; } @@ -4228,7 +4484,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true); if (rect_size.x <= 0.0f) rect_size.x = IM_FLOOR(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines - ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos +ImVec2(rect_size.x, bg_offy_dn)); + ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos + ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); if (rect.Overlaps(clip_rect)) draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color); @@ -4277,16 +4533,16 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } } + if (is_password && !is_displaying_hint) + PopFont(); + if (is_multiline) { - Dummy(text_size + ImVec2(0.0f, g.FontSize)); // Always add room to scroll an extra line + Dummy(text_size); EndChild(); EndGroup(); } - if (is_password && !is_displaying_hint) - PopFont(); - // Log as text if (g.LogEnabled && (!is_password || is_displaying_hint)) LogRenderedText(&draw_pos, buf_display, buf_display_end); @@ -4399,8 +4655,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) { // RGB/HSV 0..255 Sliders - const float w_item_one = ImMax(1.0f, IM_FLOOR((w_inputs - (style.ItemInnerSpacing.x) * (components-1)) / (float)components)); - const float w_item_last = ImMax(1.0f, IM_FLOOR(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components-1))); + const float w_item_one = ImMax(1.0f, IM_FLOOR((w_inputs - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components)); + const float w_item_last = ImMax(1.0f, IM_FLOOR(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components - 1))); const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); static const char* ids[4] = { "##X", "##Y", "##Z", "##W" }; @@ -4427,7 +4683,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0. if (flags & ImGuiColorEditFlags_Float) { - value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); + value_changed |= DragFloat(ids[n], &f[n], 1.0f / 255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]); value_changed_as_float |= value_changed; } else @@ -4443,9 +4699,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // RGB Hexadecimal Input char buf[64]; if (alpha) - ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255), ImClamp(i[3],0,255)); + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X%02X", ImClamp(i[0], 0, 255), ImClamp(i[1], 0, 255), ImClamp(i[2], 0, 255), ImClamp(i[3], 0, 255)); else - ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", ImClamp(i[0],0,255), ImClamp(i[1],0,255), ImClamp(i[2],0,255)); + ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", ImClamp(i[0], 0, 255), ImClamp(i[1], 0, 255), ImClamp(i[2], 0, 255)); SetNextItemWidth(w_inputs); if (InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) { @@ -4477,7 +4733,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Store current color and open a picker g.ColorPickerRef = col_v4; OpenPopup("picker"); - SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1,style.ItemSpacing.y)); + SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1, style.ItemSpacing.y)); } } if (!(flags & ImGuiColorEditFlags_NoOptions)) @@ -4638,7 +4894,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl float wheel_thickness = sv_picker_size * 0.08f; float wheel_r_outer = sv_picker_size * 0.50f; float wheel_r_inner = wheel_r_outer - wheel_thickness; - ImVec2 wheel_center(picker_pos.x + (sv_picker_size + bars_width)*0.5f, picker_pos.y + sv_picker_size*0.5f); + ImVec2 wheel_center(picker_pos.x + (sv_picker_size + bars_width)*0.5f, picker_pos.y + sv_picker_size * 0.5f); // Note: the triangle is displayed rotated with triangle_pa pointing to Hue, but most coordinates stays unrotated for logic. float triangle_r = wheel_r_inner - (int)(sv_picker_size * 0.027f); @@ -4677,10 +4933,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl ImVec2 initial_off = g.IO.MouseClickedPos[0] - wheel_center; ImVec2 current_off = g.IO.MousePos - wheel_center; float initial_dist2 = ImLengthSqr(initial_off); - if (initial_dist2 >= (wheel_r_inner-1)*(wheel_r_inner-1) && initial_dist2 <= (wheel_r_outer+1)*(wheel_r_outer+1)) + if (initial_dist2 >= (wheel_r_inner - 1) * (wheel_r_inner - 1) && initial_dist2 <= (wheel_r_outer + 1) * (wheel_r_outer + 1)) { // Interactive with Hue wheel - H = ImAtan2(current_off.y, current_off.x) / IM_PI*0.5f; + H = ImAtan2(current_off.y, current_off.x) / IM_PI * 0.5f; if (H < 0.0f) H += 1.0f; value_changed = value_changed_h = true; @@ -4709,8 +4965,8 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl InvisibleButton("sv", ImVec2(sv_picker_size, sv_picker_size)); if (IsItemActive()) { - S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size-1)); - V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size - 1)); + V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) @@ -4721,7 +4977,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl InvisibleButton("hue", ImVec2(bars_width, sv_picker_size)); if (IsItemActive()) { - H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); value_changed = value_changed_h = true; } } @@ -4733,7 +4989,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl InvisibleButton("alpha", ImVec2(bars_width, sv_picker_size)); if (IsItemActive()) { - col[3] = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size-1)); + col[3] = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); value_changed = true; } } @@ -4784,7 +5040,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { if (flags & ImGuiColorEditFlags_InputRGB) { - ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10*1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]); + ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10 * 1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]); g.ColorEditLastHue = H; g.ColorEditLastSat = S; memcpy(g.ColorEditLastColor, col, sizeof(float) * 3); @@ -4887,17 +5143,17 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl // Paint colors over existing vertices ImVec2 gradient_p0(wheel_center.x + ImCos(a0) * wheel_r_inner, wheel_center.y + ImSin(a0) * wheel_r_inner); ImVec2 gradient_p1(wheel_center.x + ImCos(a1) * wheel_r_inner, wheel_center.y + ImSin(a1) * wheel_r_inner); - ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col_hues[n], col_hues[n+1]); + ShadeVertsLinearColorGradientKeepAlpha(draw_list, vert_start_idx, vert_end_idx, gradient_p0, gradient_p1, col_hues[n], col_hues[n + 1]); } // Render Cursor + preview on Hue Wheel float cos_hue_angle = ImCos(H * 2.0f * IM_PI); float sin_hue_angle = ImSin(H * 2.0f * IM_PI); - ImVec2 hue_cursor_pos(wheel_center.x + cos_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f, wheel_center.y + sin_hue_angle * (wheel_r_inner+wheel_r_outer)*0.5f); + ImVec2 hue_cursor_pos(wheel_center.x + cos_hue_angle * (wheel_r_inner + wheel_r_outer) * 0.5f, wheel_center.y + sin_hue_angle * (wheel_r_inner + wheel_r_outer) * 0.5f); float hue_cursor_rad = value_changed_h ? wheel_thickness * 0.65f : wheel_thickness * 0.55f; int hue_cursor_segments = ImClamp((int)(hue_cursor_rad / 1.4f), 9, 32); draw_list->AddCircleFilled(hue_cursor_pos, hue_cursor_rad, hue_color32, hue_cursor_segments); - draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad+1, col_midgrey, hue_cursor_segments); + draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad + 1, col_midgrey, hue_cursor_segments); draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad, col_white, hue_cursor_segments); // Render SV triangle (rotated according to hue) @@ -4935,7 +5191,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range) float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f; draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, user_col32_striped_of_alpha, 12); - draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad+1, col_midgrey, 12); + draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad + 1, col_midgrey, 12); draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, col_white, 12); // Render alpha bar @@ -5007,8 +5263,8 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) { float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f); - RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight); - window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft); + RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight); + window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft); } else { @@ -5126,7 +5382,7 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) if (allow_opt_inputs || allow_opt_datatype) Separator(); - if (Button("Copy as..", ImVec2(-1,0))) + if (Button("Copy as..", ImVec2(-1, 0))) OpenPopup("Copy"); if (BeginPopup("Copy")) { @@ -5170,16 +5426,16 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl // Draw small/thumbnail version of each picker type (over an invisible button for selection) if (picker_type > 0) Separator(); PushID(picker_type); - ImGuiColorEditFlags picker_flags = ImGuiColorEditFlags_NoInputs|ImGuiColorEditFlags_NoOptions|ImGuiColorEditFlags_NoLabel|ImGuiColorEditFlags_NoSidePreview|(flags & ImGuiColorEditFlags_NoAlpha); + ImGuiColorEditFlags picker_flags = ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoSidePreview | (flags & ImGuiColorEditFlags_NoAlpha); if (picker_type == 0) picker_flags |= ImGuiColorEditFlags_PickerHueBar; if (picker_type == 1) picker_flags |= ImGuiColorEditFlags_PickerHueWheel; ImVec2 backup_pos = GetCursorScreenPos(); if (Selectable("##selectable", false, 0, picker_size)) // By default, Selectable() is closing popup g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags__PickerMask) | (picker_flags & ImGuiColorEditFlags__PickerMask); SetCursorScreenPos(backup_pos); - ImVec4 dummy_ref_col; - memcpy(&dummy_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4)); - ColorPicker4("##dummypicker", &dummy_ref_col.x, picker_flags); + ImVec4 previewing_ref_col; + memcpy(&previewing_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4)); + ColorPicker4("##previewing_picker", &previewing_ref_col.x, picker_flags); PopID(); } PopItemWidth(); @@ -5354,7 +5610,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l const ImVec2 label_size = CalcTextSize(label, label_end, false); // We vertically grow up to current line height up the typical widget height. - const float frame_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y*2), label_size.y + padding.y*2); + const float frame_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y * 2), label_size.y + padding.y * 2); ImRect frame_bb; frame_bb.Min.x = (flags & ImGuiTreeNodeFlags_SpanFullWidth) ? window->WorkRect.Min.x : window->DC.CursorPos.x; frame_bb.Min.y = window->DC.CursorPos.y; @@ -5368,9 +5624,9 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l frame_bb.Max.x += IM_FLOOR(window->WindowPadding.x * 0.5f); } - const float text_offset_x = g.FontSize + (display_frame ? padding.x*3 : padding.x*2); // Collapser arrow width + Spacing + const float text_offset_x = g.FontSize + (display_frame ? padding.x * 3 : padding.x * 2); // Collapser arrow width + Spacing const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it - const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x*2 : 0.0f); // Include collapser + const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x * 2 : 0.0f); // Include collapser ImVec2 text_pos(window->DC.CursorPos.x + text_offset_x, window->DC.CursorPos.y + text_offset_y); ItemSize(ImVec2(text_width, frame_height), padding.y); @@ -5416,17 +5672,14 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // Open behaviors can be altered with the _OpenOnArrow and _OnOnDoubleClick flags. // Some alteration have subtle effects (e.g. toggle on MouseUp vs MouseDown events) due to requirements for multi-selection and drag and drop support. - // - Single-click on label = Toggle on MouseUp (default) - // - Single-click on arrow = Toggle on MouseUp (when _OpenOnArrow=0) + // - Single-click on label = Toggle on MouseUp (default, when _OpenOnArrow=0) + // - Single-click on arrow = Toggle on MouseDown (when _OpenOnArrow=0) // - Single-click on arrow = Toggle on MouseDown (when _OpenOnArrow=1) // - Double-click on label = Toggle on MouseDoubleClick (when _OpenOnDoubleClick=1) // - Double-click on arrow = Toggle on MouseDoubleClick (when _OpenOnDoubleClick=1 and _OpenOnArrow=0) - // This makes _OpenOnArrow have a subtle effect on _OpenOnDoubleClick: arrow click reacts on Down rather than Up. - // It is rather standard that arrow click react on Down rather than Up and we'd be tempted to make it the default - // (by removing the _OpenOnArrow test below), however this would have a perhaps surprising effect on CollapsingHeader()? - // So right now we are making this optional. May evolve later. + // It is rather standard that arrow click react on Down rather than Up. // We set ImGuiButtonFlags_PressedOnClickRelease on OpenOnDoubleClick because we want the item to be active on the initial MouseDown in order for drag and drop to work. - if (is_mouse_x_over_arrow && (flags & ImGuiTreeNodeFlags_OpenOnArrow)) + if (is_mouse_x_over_arrow) button_flags |= ImGuiButtonFlags_PressedOnClick; else if (flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; @@ -5504,9 +5757,9 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here. const char log_prefix[] = "\n##"; const char log_suffix[] = "##"; - LogRenderedText(&text_pos, log_prefix, log_prefix+3); + LogRenderedText(&text_pos, log_prefix, log_prefix + 3); RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); - LogRenderedText(&text_pos, log_suffix, log_suffix+2); + LogRenderedText(&text_pos, log_suffix, log_suffix + 2); } else { @@ -5627,13 +5880,13 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags if (p_open) flags |= ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; bool is_open = TreeNodeBehavior(id, flags, label); - if (p_open) + if (p_open != NULL) { // Create a small overlapping close button // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc. // FIXME: CloseButton can overlap into text, need find a way to clip the text somehow. ImGuiContext& g = *GImGui; - ImGuiItemHoveredDataBackup last_item_backup; + ImGuiLastItemDataBackup last_item_backup; float button_size = g.FontSize; float button_x = ImMax(window->DC.LastItemRect.Min.x, window->DC.LastItemRect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); float button_y = window->DC.LastItemRect.Min.y; @@ -5651,7 +5904,7 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags // - Selectable() //------------------------------------------------------------------------- -// Tip: pass a non-visible label (e.g. "##dummy") then you can use the space to draw other text or image. +// Tip: pass a non-visible label (e.g. "##hello") then you can use the space to draw other text or image. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id. // With this scheme, ImGuiSelectableFlags_SpanAllColumns and ImGuiSelectableFlags_AllowItemOverlap are also frequently used flags. // FIXME: Selectable() with (size.x == 0.0f) and (SelectableTextAlign.x > 0.0f) followed by SameLine() is currently not supported. @@ -5664,7 +5917,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; - if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. + const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0; + if (span_all_columns && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. PushColumnsBackground(); // Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle. @@ -5676,8 +5930,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ItemSize(size, 0.0f); // Fill horizontal space - const float min_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? window->ContentRegionRect.Min.x : pos.x; - const float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? window->ContentRegionRect.Max.x : GetContentRegionMaxAbs().x; + const float min_x = span_all_columns ? window->ParentWorkRect.Min.x : pos.x; + const float max_x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x; if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_SpanAvailWidth)) size.x = ImMax(label_size.x, max_x - min_x); @@ -5686,33 +5940,35 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const ImVec2 text_max(min_x + size.x, pos.y + size.y); // Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable. - ImRect bb_enlarged(min_x, pos.y, text_max.x, text_max.y); - const float spacing_x = style.ItemSpacing.x; - const float spacing_y = style.ItemSpacing.y; - const float spacing_L = IM_FLOOR(spacing_x * 0.50f); - const float spacing_U = IM_FLOOR(spacing_y * 0.50f); - bb_enlarged.Min.x -= spacing_L; - bb_enlarged.Min.y -= spacing_U; - bb_enlarged.Max.x += (spacing_x - spacing_L); - bb_enlarged.Max.y += (spacing_y - spacing_U); - //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_align.Min, bb_align.Max, IM_COL32(255, 0, 0, 255)); } - //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_enlarged.Min, bb_enlarged.Max, IM_COL32(0, 255, 0, 255)); } + ImRect bb(min_x, pos.y, text_max.x, text_max.y); + if ((flags & ImGuiSelectableFlags_NoPadWithHalfSpacing) == 0) + { + const float spacing_x = style.ItemSpacing.x; + const float spacing_y = style.ItemSpacing.y; + const float spacing_L = IM_FLOOR(spacing_x * 0.50f); + const float spacing_U = IM_FLOOR(spacing_y * 0.50f); + bb.Min.x -= spacing_L; + bb.Min.y -= spacing_U; + bb.Max.x += (spacing_x - spacing_L); + bb.Max.y += (spacing_y - spacing_U); + } + //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); } bool item_add; if (flags & ImGuiSelectableFlags_Disabled) { ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus; - item_add = ItemAdd(bb_enlarged, id); + item_add = ItemAdd(bb, id); window->DC.ItemFlags = backup_item_flags; } else { - item_add = ItemAdd(bb_enlarged, id); + item_add = ItemAdd(bb, id); } if (!item_add) { - if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) + if (span_all_columns && window->DC.CurrentColumns) PopColumnsBackground(); return false; } @@ -5731,7 +5987,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const bool was_selected = selected; bool hovered, held; - bool pressed = ButtonBehavior(bb_enlarged, id, &hovered, &held, button_flags); + bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) @@ -5758,15 +6014,15 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (hovered || selected) { const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); - RenderFrame(bb_enlarged.Min, bb_enlarged.Max, col, false, 0.0f); - RenderNavHighlight(bb_enlarged, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + RenderFrame(bb.Min, bb.Max, col, false, 0.0f); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); } - if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) + if (span_all_columns && window->DC.CurrentColumns) PopColumnsBackground(); if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); - RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb_enlarged); + RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor(); // Automatically close popups @@ -5981,7 +6237,7 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get const float v0 = values_getter(data, (v_idx + values_offset) % values_count); const float v1 = values_getter(data, (v_idx + 1 + values_offset) % values_count); if (plot_type == ImGuiPlotType_Lines) - SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx+1, v1); + SetTooltip("%d: %8.4g\n%d: %8.4g", v_idx, v0, v_idx + 1, v1); else if (plot_type == ImGuiPlotType_Histogram) SetTooltip("%d: %8.4g", v_idx, v0); idx_hovered = v_idx; @@ -6027,7 +6283,7 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get // Text overlay if (overlay_text) - RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f,0.0f)); + RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y), frame_bb.Max, overlay_text, NULL, NULL, ImVec2(0.5f, 0.0f)); if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label); @@ -6188,7 +6444,8 @@ bool ImGui::BeginMenuBar() clip_rect.ClipWith(window->OuterRectClipped); PushClipRect(clip_rect.Min, clip_rect.Max, false); - window->DC.CursorPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); + // We overwrite CursorMaxPos because BeginGroup sets it to CursorPos (essentially the .EmitItem hack in EndMenuBar() would need something analoguous here, maybe a BeginGroupEx() with flags). + window->DC.CursorPos = window->DC.CursorMaxPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); window->DC.LayoutType = ImGuiLayoutType_Horizontal; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); @@ -6281,7 +6538,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); - bool menu_is_open = IsPopupOpen(id); + bool menu_is_open = IsPopupOpen(id, ImGuiPopupFlags_None); // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; @@ -6407,7 +6664,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) if (!enabled) // explicitly close if an open menu becomes disabled, facilitate users code a lot in pattern such as 'if (BeginMenu("options", has_object)) { ..use object.. }' want_close = true; - if (want_close && IsPopupOpen(id)) + if (want_close && IsPopupOpen(id, ImGuiPopupFlags_None)) ClosePopupToLevel(g.BeginPopupStack.Size, true); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); @@ -6524,7 +6781,7 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, // - TabBarFindTabById() [Internal] // - TabBarRemoveTab() [Internal] // - TabBarCloseTab() [Internal] -// - TabBarScrollClamp()v +// - TabBarScrollClamp() [Internal] // - TabBarScrollToTab() [Internal] // - TabBarQueueChangeTabOrder() [Internal] // - TabBarScrollingButtons() [Internal] @@ -6548,20 +6805,21 @@ ImGuiTabBar::ImGuiTabBar() SelectedTabId = NextSelectedTabId = VisibleTabId = 0; CurrFrameVisible = PrevFrameVisible = -1; LastTabContentHeight = 0.0f; - OffsetMax = OffsetMaxIdeal = OffsetNextTab = 0.0f; + WidthAllTabs = WidthAllTabsIdeal = OffsetNextTab = 0.0f; ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f; Flags = ImGuiTabBarFlags_None; ReorderRequestTabId = 0; ReorderRequestDir = 0; + TabsActiveCount = 0; WantLayout = VisibleTabWasSubmitted = false; LastTabItemIdx = -1; } -static int IMGUI_CDECL TabItemComparerByVisibleOffset(const void* lhs, const void* rhs) +static int IMGUI_CDECL TabItemComparerByBeginOrder(const void* lhs, const void* rhs) { const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; const ImGuiTabItem* b = (const ImGuiTabItem*)rhs; - return (int)(a->Offset - b->Offset); + return (int)(a->BeginOrder - b->BeginOrder); } static ImGuiTabBar* GetTabBarFromTabBarRef(const ImGuiPtrOrIndex& ref) @@ -6613,10 +6871,9 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG return true; } - // When toggling back from ordered to manually-reorderable, shuffle tabs to enforce the last visible order. - // Otherwise, the most recently inserted tabs would move at the end of visible list which can be a little too confusing or magic for the user. - if ((flags & ImGuiTabBarFlags_Reorderable) && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1 && tab_bar->PrevFrameVisible != -1) - ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByVisibleOffset); + // When toggling ImGuiTabBarFlags_Reorderable flag, ensure tabs are ordered based on their submission order. + if ((flags & ImGuiTabBarFlags_Reorderable) != (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1) + ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByBeginOrder); // Flags if ((flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) @@ -6628,10 +6885,11 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG tab_bar->PrevFrameVisible = tab_bar->CurrFrameVisible; tab_bar->CurrFrameVisible = g.FrameCount; tab_bar->FramePadding = g.Style.FramePadding; + tab_bar->TabsActiveCount = 0; - // Layout - ItemSize(ImVec2(tab_bar->OffsetMaxIdeal, tab_bar->BarRect.GetHeight()), tab_bar->FramePadding.y); + // Set cursor pos in a way which only be used in the off-chance the user erroneously submits item before BeginTabItem(): items will overlap window->DC.CursorPos.x = tab_bar->BarRect.Min.x; + window->DC.CursorPos.y = tab_bar->BarRect.Max.y + g.Style.ItemSpacing.y; // Draw separator const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); @@ -6657,7 +6915,7 @@ void ImGui::EndTabBar() IM_ASSERT_USER_ERROR(tab_bar != NULL, "Mismatched BeginTabBar()/EndTabBar()!"); return; } - if (tab_bar->WantLayout) + if (tab_bar->WantLayout) // Fallback in case no TabItem have been submitted TabBarLayout(tab_bar); // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed(). @@ -6681,15 +6939,17 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) ImGuiContext& g = *GImGui; tab_bar->WantLayout = false; - // Garbage collect + // Garbage collect by compacting list int tab_dst_n = 0; for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n]; - if (tab->LastFrameVisible < tab_bar->PrevFrameVisible) + if (tab->LastFrameVisible < tab_bar->PrevFrameVisible || tab->WantClose) { - if (tab->ID == tab_bar->SelectedTabId) - tab_bar->SelectedTabId = 0; + // Remove tab + if (tab_bar->VisibleTabId == tab->ID) { tab_bar->VisibleTabId = 0; } + if (tab_bar->SelectedTabId == tab->ID) { tab_bar->SelectedTabId = 0; } + if (tab_bar->NextSelectedTabId == tab->ID) { tab_bar->NextSelectedTabId = 0; } continue; } if (tab_dst_n != tab_src_n) @@ -6711,23 +6971,9 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Process order change request (we could probably process it when requested but it's just saner to do it in a single spot). if (tab_bar->ReorderRequestTabId != 0) { - if (ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId)) - { - //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools - int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir; - if (tab2_order >= 0 && tab2_order < tab_bar->Tabs.Size) - { - ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; - ImGuiTabItem item_tmp = *tab1; - *tab1 = *tab2; - *tab2 = item_tmp; - if (tab2->ID == tab_bar->SelectedTabId) - scroll_track_selected_tab_id = tab2->ID; - tab1 = tab2 = NULL; - } - if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) - MarkIniSettingsDirty(); - } + if (TabBarProcessReorder(tab_bar)) + if (tab_bar->ReorderRequestTabId == tab_bar->SelectedTabId) + scroll_track_selected_tab_id = tab_bar->ReorderRequestTabId; tab_bar->ReorderRequestTabId = 0; } @@ -6801,11 +7047,11 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) offset_x += tab->Width + g.Style.ItemInnerSpacing.x; offset_x_ideal += tab->ContentWidth + g.Style.ItemInnerSpacing.x; } - tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); - tab_bar->OffsetMaxIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); + tab_bar->WidthAllTabs = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); + tab_bar->WidthAllTabsIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); // Horizontal scrolling buttons - const bool scrolling_buttons = (tab_bar->OffsetMax > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll); + const bool scrolling_buttons = (tab_bar->WidthAllTabs > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll); if (scrolling_buttons) if (ImGuiTabItem* tab_to_select = TabBarScrollingButtons(tab_bar)) // NB: Will alter BarRect.Max.x! scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; @@ -6843,6 +7089,11 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Clear name buffers if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) tab_bar->TabsNames.Buf.resize(0); + + // Actual layout in host window (we don't do it in BeginTabBar() so as not to waste an extra frame) + ImGuiWindow* window = g.CurrentWindow; + window->DC.CursorPos = tab_bar->BarRect.Min; + ItemSize(ImVec2(tab_bar->WidthAllTabsIdeal, tab_bar->BarRect.GetHeight()), tab_bar->FramePadding.y); } // Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack. @@ -6905,7 +7156,7 @@ void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) static float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling) { - scrolling = ImMin(scrolling, tab_bar->OffsetMax - tab_bar->BarRect.GetWidth()); + scrolling = ImMin(scrolling, tab_bar->WidthAllTabs - tab_bar->BarRect.GetWidth()); return ImMax(scrolling, 0.0f); } @@ -6929,7 +7180,7 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) } } -void ImGui::TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir) +void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir) { IM_ASSERT(dir == -1 || dir == +1); IM_ASSERT(tab_bar->ReorderRequestTabId == 0); @@ -6937,6 +7188,28 @@ void ImGui::TabBarQueueChangeTabOrder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab_bar->ReorderRequestDir = (ImS8)dir; } +bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) +{ + ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId); + if (!tab1) + return false; + + //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools + int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir; + if (tab2_order < 0 || tab2_order >= tab_bar->Tabs.Size) + return false; + + ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; + ImGuiTabItem item_tmp = *tab1; + *tab1 = *tab2; + *tab2 = item_tmp; + tab1 = tab2 = NULL; + + if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) + MarkIniSettingsDirty(); + return true; +} + static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) { ImGuiContext& g = *GImGui; @@ -7006,7 +7279,7 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) arrow_col.w *= 0.5f; PushStyleColor(ImGuiCol_Text, arrow_col); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); - bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview); + bool open = BeginCombo("##v", NULL, ImGuiComboFlags_NoPreview | ImGuiComboFlags_HeightLargest); PopStyleColor(2); ImGuiTabItem* tab_to_select = NULL; @@ -7048,7 +7321,7 @@ bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags f ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { - IM_ASSERT_USER_ERROR(tab_bar, "BeginTabItem() Needs to be called between BeginTabBar() and EndTabBar()!"); + IM_ASSERT_USER_ERROR(tab_bar, "Needs to be called between BeginTabBar() and EndTabBar()!"); return false; } bool ret = TabItemEx(tab_bar, label, p_open, flags); @@ -7070,7 +7343,7 @@ void ImGui::EndTabItem() ImGuiTabBar* tab_bar = g.CurrentTabBar; if (tab_bar == NULL) { - IM_ASSERT(tab_bar != NULL && "Needs to be called between BeginTabBar() and EndTabBar()!"); + IM_ASSERT_USER_ERROR(tab_bar != NULL, "Needs to be called between BeginTabBar() and EndTabBar()!"); return; } IM_ASSERT(tab_bar->LastTabItemIdx >= 0); @@ -7093,7 +7366,8 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const ImGuiStyle& style = g.Style; const ImGuiID id = TabBarCalcTabID(tab_bar, label); - // If the user called us with *p_open == false, we early out and don't render. We make a dummy call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. + // If the user called us with *p_open == false, we early out and don't render. + // We make a call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); if (p_open && !*p_open) { @@ -7125,6 +7399,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab); tab->ContentWidth = size.x; + tab->BeginOrder = tab_bar->TabsActiveCount++; const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; @@ -7133,7 +7408,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab->Flags = flags; // Append name with zero-terminator - tab->NameOffset = tab_bar->TabsNames.size(); + tab->NameOffset = (ImS16)tab_bar->TabsNames.size(); tab_bar->TabsNames.append(label, label + strlen(label) + 1); // If we are not reorderable, always reset offset based on submission order. @@ -7224,12 +7499,12 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < bb.Min.x) { if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) - TabBarQueueChangeTabOrder(tab_bar, tab, -1); + TabBarQueueReorder(tab_bar, tab, -1); } else if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > bb.Max.x) { if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) - TabBarQueueChangeTabOrder(tab_bar, tab, +1); + TabBarQueueReorder(tab_bar, tab, +1); } } } @@ -7282,9 +7557,8 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } // [Public] This is call is 100% optional but it allows to remove some one-frame glitches when a tab has been unexpectedly removed. -// To use it to need to call the function SetTabItemClosed() after BeginTabBar() and before any call to BeginTabItem(). +// To use it to need to call the function SetTabItemClosed() between BeginTabBar() and EndTabBar(). // Tabs closed by the close button will automatically be flagged to avoid this issue. -// FIXME: We should aim to support calling SetTabItemClosed() after the tab submission (for next frame) void ImGui::SetTabItemClosed(const char* label) { ImGuiContext& g = *GImGui; @@ -7292,9 +7566,9 @@ void ImGui::SetTabItemClosed(const char* label) if (is_within_manual_tab_bar) { ImGuiTabBar* tab_bar = g.CurrentTabBar; - IM_ASSERT(tab_bar->WantLayout); // Needs to be called AFTER BeginTabBar() and BEFORE the first call to BeginTabItem() ImGuiID tab_id = TabBarCalcTabID(tab_bar, label); - TabBarRemoveTab(tab_bar, tab_id); + if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id)) + tab->WantClose = true; // Will be processed by next call to TabBarLayout() } } @@ -7372,11 +7646,11 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, bool close_button_visible = false; if (close_button_id != 0) if (is_contents_visible || bb.GetWidth() >= g.Style.TabMinWidthForUnselectedCloseButton) - if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == close_button_id) + if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == tab_id || g.ActiveId == close_button_id) close_button_visible = true; if (close_button_visible) { - ImGuiItemHoveredDataBackup last_item_backup; + ImGuiLastItemDataBackup last_item_backup; const float close_button_sz = g.FontSize; PushStyleVar(ImGuiStyleVar_FramePadding, frame_padding); if (CloseButton(close_button_id, ImVec2(bb.Max.x - frame_padding.x * 2.0f - close_button_sz, bb.Min.y))) @@ -7531,7 +7805,7 @@ void ImGui::SetColumnOffset(int column_index, float offset) column_index = columns->Current; IM_ASSERT(column_index < columns->Columns.Size); - const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count-1); + const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count - 1); const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; if (!(columns->Flags & ImGuiColumnsFlags_NoForceWithinWindow)) @@ -7636,7 +7910,8 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag columns->HostCursorPosY = window->DC.CursorPos.y; columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; columns->HostInitialClipRect = window->ClipRect; - columns->HostWorkRect = window->WorkRect; + columns->HostBackupParentWorkRect = window->ParentWorkRect; + window->ParentWorkRect = window->WorkRect; // Set state for first column // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect @@ -7672,7 +7947,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n)); float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f); column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); - column->ClipRect.ClipWith(window->ClipRect); + column->ClipRect.ClipWithFull(window->ClipRect); } if (columns->Count > 1) @@ -7816,7 +8091,8 @@ void ImGui::EndColumns() } columns->IsBeingResized = is_being_resized; - window->WorkRect = columns->HostWorkRect; + window->WorkRect = window->ParentWorkRect; + window->ParentWorkRect = columns->HostBackupParentWorkRect; window->DC.CurrentColumns = NULL; window->DC.ColumnsOffset.x = 0.0f; window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); diff --git a/imgui/imstb_textedit.h b/imgui/imstb_textedit.h index 2077d02a..e7205e77 100644 --- a/imgui/imstb_textedit.h +++ b/imgui/imstb_textedit.h @@ -177,7 +177,7 @@ // Keyboard input must be encoded as a single integer value; e.g. a character code // and some bitflags that represent shift states. to simplify the interface, SHIFT must // be a bitflag, so we can test the shifted state of cursor movements to allow selection, -// i.e. (STB_TEXTED_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow. +// i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow. // // You can encode other things, such as CONTROL or ALT, in additional bits, and // then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example, @@ -877,6 +877,12 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, // now find character position down a row if (find.length) { + + // [DEAR IMGUI] + // going down while being on the last line shouldn't bring us to that line end + if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE) + break; + float goal_x = state->has_preferred_x ? state->preferred_x : find.x; float x; int start = find.first_char + find.length; @@ -1134,7 +1140,7 @@ static void stb_textedit_discard_redo(StbUndoState *state) state->undo_rec[i].char_storage += n; } // now move all the redo records towards the end of the buffer; the first one is at 'redo_point' - // {DEAR IMGUI] + // [DEAR IMGUI] size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; From 2e3b72f9a00322424df2b3b741165062b2ff48aa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Sep 2020 21:24:35 +0200 Subject: [PATCH 0341/1018] che_ptx: loading .ptx point cloud file --- include/app_viewer.h | 1 + include/che_ptx.h | 28 +++++++++++++++++ include/config.h | 2 +- src/app_viewer.cpp | 3 +- src/che_ptx.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 include/che_ptx.h create mode 100644 src/che_ptx.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index 02fa3b87..920c7f88 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -6,6 +6,7 @@ #include "che_off.h" #include "che_obj.h" #include "che_ply.h" +#include "che_ptx.h" #include "che_img.h" #include "che_sphere.h" #include "laplacian.h" diff --git a/include/che_ptx.h b/include/che_ptx.h new file mode 100644 index 00000000..01fa9737 --- /dev/null +++ b/include/che_ptx.h @@ -0,0 +1,28 @@ +#ifndef CHE_PTX_H +#define CHE_PTX_H + +#include "che.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_ptx : public che +{ + public: + che_ptx(const std::string & file); + che_ptx(const che_ptx & mesh); + virtual ~che_ptx(); + + static void write_file(const che * mesh, const std::string & file); + + private: + void read_file(const std::string & file); +}; + + +} // namespace gproshan + +#endif // CHE_PTX_H + diff --git a/include/config.h b/include/config.h index fe8bc38e..a0133ae5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -#define SINGLE_P +//#define SINGLE_P // print log messages #define LOG diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 0a799c72..41d809dc 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -20,6 +20,7 @@ che * load_mesh(const string & file_path) if(extension == "off") return new che_off(file_path); if(extension == "obj") return new che_obj(file_path); if(extension == "ply") return new che_ply(file_path); + if(extension == "ptx") return new che_ptx(file_path); return new che_img(file_path); } @@ -69,7 +70,7 @@ int app_viewer::main(int nargs, const char ** args) #ifdef GPROSHAN_CUDA add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); -// add_process('L', "Geodesics (HEAT_FLOW_GPU)", process_geodesics_heat_flow_gpu); + add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_flow_gpu}); #endif // GPROSHAN_CUDA add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); diff --git a/src/che_ptx.cpp b/src/che_ptx.cpp new file mode 100644 index 00000000..bd186b48 --- /dev/null +++ b/src/che_ptx.cpp @@ -0,0 +1,71 @@ +#include "che_ptx.h" + +#include +#include +#include +#include + +using namespace std; + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_ptx::che_ptx(const string & file) +{ + init(file); +} + +che_ptx::che_ptx(const che_ptx & mesh): che(mesh) +{ +} + +che_ptx::~che_ptx() +{ +} + +void che_ptx::read_file(const string & file) +{ + size_t n_rows, n_cols; + vertex T[4], R[3], tr; + real_t s; + + ifstream is(file); + + assert(is.good()); + + is >> n_rows >> n_cols; + init(n_rows * n_cols, 0); + + is >> T[0] >> T[1] >> T[2] >> T[3]; + is >> R[0] >> s; + is >> R[1] >> s; + is >> R[2] >> s; + is >> tr >> s; + + char line[256]; + is.getline(line, sizeof(line)); + + for(index_t i = 0; i < n_vertices_; i++) + { + is.getline(line, sizeof(line)); + stringstream ss(line); + + ss >> GT[i]; + } + + is.close(); +} + +void che_ptx::write_file(const che * mesh, const string & file) +{ + ofstream os(file + ".off"); + + + os.close(); +} + + +} // namespace gproshan + From 487b78c12dfdf13d022830f8935f00a01f39c14f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Sep 2020 06:23:56 +0200 Subject: [PATCH 0342/1018] loading mesh vertex color --- include/che.h | 9 +++++--- shaders/colormap.glsl | 1 - shaders/vertex.glsl | 2 +- shaders/vertex_pointcloud.glsl | 2 +- src/app_viewer.cpp | 38 +++++++++++++++++----------------- src/che.cpp | 23 +++++++++++++++----- src/che_off.cpp | 5 +++-- src/che_ptx.cpp | 3 +++ src/viewer/che_viewer.cpp | 13 +++++++++--- 9 files changed, 61 insertions(+), 35 deletions(-) diff --git a/include/che.h b/include/che.h index 5be94b4c..669ab084 100644 --- a/include/che.h +++ b/include/che.h @@ -47,7 +47,8 @@ class che index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - real_t * VC = nullptr; ///< vertex color : v -> color(v) + vertex * VC = nullptr; ///< vertex color : v -> + real_t * VHC = nullptr; ///< vertex color heat map : v -> hm_color(v) bool manifold = true; @@ -69,8 +70,10 @@ class che real_t area_vertex(const index_t & v); real_t area_surface() const; void update_colors(const real_t * vcolor = nullptr, real_t max_color = 0); - const real_t & color(const index_t & v) const; - real_t & color(const index_t & v); + const vertex & color(const index_t & v) const; + vertex & color(const index_t & v); + const real_t & hm_color(const index_t & v) const; + real_t & hm_color(const index_t & v); void update_normals(); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl index f3fad9f6..2c270791 100644 --- a/shaders/colormap.glsl +++ b/shaders/colormap.glsl @@ -103,6 +103,5 @@ vec3 colormap(uint i, float x) if(i == 0) return colormap_0(x); if(i == 1) return colormap_1(x); if(i == 2) return colormap_2(x); - } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 2abea13f..d7f4aefa 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -2,7 +2,7 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; -layout (location=2) in float in_color; +layout (location=3) in float in_color; out vec3 vs_position; out vec3 vs_normal; diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index 2abea13f..d7f4aefa 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -2,7 +2,7 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; -layout (location=2) in float in_color; +layout (location=3) in float in_color; out vec3 vs_position; out vec3 vs_normal; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 41d809dc..a377ce71 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -129,7 +129,7 @@ bool paint_holes_vertices(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - if(v >= nv) mesh->color(v) = .25; + if(v >= nv) mesh->hm_color(v) = .25; return false; } @@ -262,7 +262,7 @@ bool app_viewer::process_threshold(viewer * p_view) che_viewer & mesh = view->active_mesh(); for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = mesh->color(v) > 0.5 ? 1 : 0.5; + mesh->hm_color(v) = mesh->hm_color(v) > 0.5 ? 1 : 0.5; return false; } @@ -302,7 +302,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = eigvec(v, k); + mesh->hm_color(v) = eigvec(v, k); mesh.update_vbo(); } @@ -346,13 +346,13 @@ bool app_viewer::process_wks(viewer * p_view) for(int k = 1; k < K; k++) s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - mesh->color(v) = norm(s); - max_s = max(max_s, mesh->color(v)); + mesh->hm_color(v) = norm(s); + max_s = max(max_s, mesh->hm_color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; + mesh->hm_color(v) /= max_s; } return true; @@ -393,14 +393,14 @@ bool app_viewer::process_hks(viewer * p_view) for(int k = 1; k < K; k++) s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); - mesh->color(v) = norm(abs(arma::fft(s, 128))); - //mesh->color(v) = norm(s); - max_s = max(max_s, mesh->color(v)); + mesh->hm_color(v) = norm(abs(arma::fft(s, 128))); + //mesh->hm_color(v) = norm(s); + max_s = max(max_s, mesh->hm_color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; + mesh->hm_color(v) /= max_s; } return true; @@ -439,13 +439,13 @@ bool app_viewer::process_gps(viewer * p_view) #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) { - mesh->color(v) = norm(eigvec.row(v)); - max_s = max(max_s, mesh->color(v)); + mesh->hm_color(v) = norm(eigvec.row(v)); + max_s = max(max_s, mesh->hm_color(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) /= max_s; + mesh->hm_color(v) /= max_s; } return true; @@ -481,7 +481,7 @@ bool app_viewer::process_key_components(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = (real_t) kcs(v) / kcs; + mesh->hm_color(v) = (real_t) kcs(v) / kcs; return false; } @@ -503,7 +503,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) - mesh->color(u) = 1; + mesh->hm_color(u) = 1; vdir.x = p.T(0, 0); vdir.y = p.T(0, 1); @@ -655,7 +655,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices(); v++) { if(toplesets[v] < n_toplesets) - mesh->color(v) = real_t(toplesets[v]) / (n_toplesets); + mesh->hm_color(v) = real_t(toplesets[v]) / (n_toplesets); } gproshan_debug_var(n_toplesets); @@ -688,8 +688,8 @@ bool app_viewer::process_voronoi(viewer * p_view) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - mesh->color(i) = ptp.clusters[i]; - mesh->color(i) /= mesh.selected.size() + 1; + mesh->hm_color(i) = ptp.clusters[i]; + mesh->hm_color(i) /= mesh.selected.size() + 1; } return false; @@ -973,7 +973,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = f(gv(v)); + mesh->hm_color(v) = f(gv(v)); return false; } diff --git a/src/che.cpp b/src/che.cpp index 6cbeaf4a..25f546e6 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -241,13 +241,13 @@ real_t che::area_surface() const void che::update_colors(const real_t * vcolor, real_t max_color) { - if(!VC) VC = new real_t[n_vertices_]; + if(!VHC) VHC = new real_t[n_vertices_]; if(!vcolor) { #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VC[v] = 0.5; + VHC[v] = 0.5; return; } @@ -262,21 +262,33 @@ void che::update_colors(const real_t * vcolor, real_t max_color) #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VC[v] = vcolor[v] / max_color; + VHC[v] = vcolor[v] / max_color; } -const real_t & che::color(const index_t & v) const +const vertex & che::color(const index_t & v) const { assert(VC && v < n_vertices_); return VC[v]; } -real_t & che::color(const index_t & v) +vertex & che::color(const index_t & v) { assert(VC && v < n_vertices_); return VC[v]; } +const real_t & che::hm_color(const index_t & v) const +{ + assert(VHC && v < n_vertices_); + return VHC[v]; +} + +real_t & che::hm_color(const index_t & v) +{ + assert(VHC && v < n_vertices_); + return VHC[v]; +} + void che::update_normals() { if(VN) return; // normals was already loaded/computed @@ -1486,6 +1498,7 @@ void che::delete_me() delete [] BT; BT = nullptr; delete [] VN; VN = nullptr; delete [] VC; VC = nullptr; + delete [] VHC; VHC = nullptr; } void che::read_file(const string & ) diff --git a/src/che_off.cpp b/src/che_off.cpp index 645d507b..5d21ced4 100644 --- a/src/che_off.cpp +++ b/src/che_off.cpp @@ -39,12 +39,13 @@ void che_off::read_file(const string & file) init(n_v, n_f); if(soff[0] == 'N') VN = new vertex[n_vertices_]; + if(soff[0] == 'C' || soff[1] == 'C') VC = new vertex[n_vertices_]; - int r, g, b, a; + real_t alpha; // color for(index_t i = 0; i < n_vertices_; i++) { is >> GT[i]; - if(soff[0] == 'C') is >> r >> g >> b >> a; // ignore RGB for now + if(soff[0] == 'C' || soff[1] == 'C') is >> VC[i] >> alpha; if(soff[0] == 'N') is >> VN[i]; } diff --git a/src/che_ptx.cpp b/src/che_ptx.cpp index bd186b48..dc41add6 100644 --- a/src/che_ptx.cpp +++ b/src/che_ptx.cpp @@ -53,6 +53,9 @@ void che_ptx::read_file(const string & file) stringstream ss(line); ss >> GT[i]; + + VN[i] = T[0] - GT[i]; + VN[i].unit(); } is.close(); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 987ba95a..1bc34f0f 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -74,11 +74,18 @@ void che_viewer::update_vbo() glVertexAttribPointer(1, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - // 2 COLOR + // 2 MESH COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->color(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->color(0), GL_STATIC_DRAW); glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 1, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(2, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // 3 HEAT MAP COLOR + glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->hm_color(0), GL_STATIC_DRAW); + glEnableVertexAttribArray(3); + glVertexAttribPointer(3, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 INDEXES From bc0e53cf9d9164a479e792d2fac6b08fbc3e42da Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Sep 2020 06:48:33 +0200 Subject: [PATCH 0343/1018] render opengl with mesh color --- include/viewer/che_viewer.h | 2 +- include/viewer/viewer.h | 2 +- shaders/fragment.glsl | 7 ++++++- shaders/fragment_pointcloud.glsl | 7 ++++++- shaders/geometry.glsl | 5 +++++ shaders/vertex.glsl | 3 +++ shaders/vertex_pointcloud.glsl | 3 +++ src/che_off.cpp | 17 +++++++++++++---- src/viewer/che_viewer.cpp | 10 +++++----- 9 files changed, 43 insertions(+), 13 deletions(-) diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index bee38b46..8aebdc90 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -30,7 +30,7 @@ class che_viewer vertex v_translate; GLuint vao; - GLuint vbo[5]; + GLuint vbo[6]; public: int vx, vy; ///< viewport positions. diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index eeea34ea..6f6aa6ec 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -60,7 +60,7 @@ class viewer int viewport_height; unsigned int idx_colormap = 0; // colormap index defined in shaders/colormap.glsl - const std::vector colormap = {"blue", "red", "blue/read"}; + const std::vector colormap = {"blue", "red", "blue/read", "mesh"}; shader shader_triangles; shader shader_normals; shader shader_gradient; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index aa432054..fe450c95 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -4,6 +4,7 @@ in vec3 gs_position; in vec3 gs_normal; +in vec3 gs_mesh_color; in float gs_color; noperspective in vec3 edge_dist; @@ -40,7 +41,11 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color = colormap(idx_colormap, gs_color); + vec3 color; + if(idx_colormap == 3) + color = gs_mesh_color; + else + color = colormap(idx_colormap, gs_color); // lines if(render_lines) diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index e988c39e..8a81002f 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -4,6 +4,7 @@ in vec3 vs_position; in vec3 vs_normal; +in vec3 vs_mesh_color; in float vs_color; layout(location = 0) out vec4 frag_color; @@ -36,7 +37,11 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color = colormap(idx_colormap, vs_color); + vec3 color; + if(idx_colormap == 3) + color = vs_mesh_color; + else + color = colormap(idx_colormap, vs_color); // lines if(render_lines) diff --git a/shaders/geometry.glsl b/shaders/geometry.glsl index 45bac232..2998d0b7 100644 --- a/shaders/geometry.glsl +++ b/shaders/geometry.glsl @@ -5,10 +5,12 @@ layout(triangle_strip, max_vertices = 3) out; in vec3 vs_position[]; in vec3 vs_normal[]; +in vec3 vs_mesh_color[]; in float vs_color[]; out vec3 gs_position; out vec3 gs_normal; +out vec3 gs_mesh_color; out float gs_color; noperspective out vec3 edge_dist; @@ -29,6 +31,7 @@ void main() gs_position = vs_position[0]; gs_normal = vs_normal[0]; + gs_mesh_color = vs_mesh_color[0]; gs_color = vs_color[0]; edge_dist = vec3(ha, 0, 0); gl_Position = gl_in[0].gl_Position; @@ -36,6 +39,7 @@ void main() gs_position = vs_position[1]; gs_normal = vs_normal[1]; + gs_mesh_color = vs_mesh_color[1]; gs_color = vs_color[1]; edge_dist = vec3(0, hb, 0); gl_Position = gl_in[1].gl_Position; @@ -43,6 +47,7 @@ void main() gs_position = vs_position[2]; gs_normal = vs_normal[2]; + gs_mesh_color = vs_mesh_color[2]; gs_color = vs_color[2]; edge_dist = vec3(0, 0, hc); gl_Position = gl_in[2].gl_Position; diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index d7f4aefa..a74a31be 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -2,10 +2,12 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; +layout (location=2) in vec3 in_mesh_color; layout (location=3) in float in_color; out vec3 vs_position; out vec3 vs_normal; +out vec3 vs_mesh_color; out float vs_color; uniform mat4 model_view_mat; @@ -15,6 +17,7 @@ void main() { vs_position = in_position; vs_normal = in_normal; + vs_mesh_color = in_mesh_color; vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index d7f4aefa..a74a31be 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -2,10 +2,12 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; +layout (location=2) in vec3 in_mesh_color; layout (location=3) in float in_color; out vec3 vs_position; out vec3 vs_normal; +out vec3 vs_mesh_color; out float vs_color; uniform mat4 model_view_mat; @@ -15,6 +17,7 @@ void main() { vs_position = in_position; vs_normal = in_normal; + vs_mesh_color = in_mesh_color; vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); diff --git a/src/che_off.cpp b/src/che_off.cpp index 5d21ced4..3af58e67 100644 --- a/src/che_off.cpp +++ b/src/che_off.cpp @@ -38,17 +38,26 @@ void che_off::read_file(const string & file) is >> n_v >> n_f >> v; init(n_v, n_f); - if(soff[0] == 'N') VN = new vertex[n_vertices_]; - if(soff[0] == 'C' || soff[1] == 'C') VC = new vertex[n_vertices_]; + if(soff[0] == 'N') + VN = new vertex[n_vertices_]; + if(soff[0] == 'C' || soff[1] == 'C') + VC = new vertex[n_vertices_]; real_t alpha; // color for(index_t i = 0; i < n_vertices_; i++) { is >> GT[i]; - if(soff[0] == 'C' || soff[1] == 'C') is >> VC[i] >> alpha; - if(soff[0] == 'N') is >> VN[i]; + if(VC) is >> VC[i] >> alpha; + if(VN) is >> VN[i]; } + if(VC) + { + #pragma omp parallel for + for(index_t i = 0; i < n_vertices_; i++) + VC[i] /= 255; + } + index_t he = 0; for(index_t i = 0; i < n_faces_; i++) { diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 1bc34f0f..4f0d58ec 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -30,7 +30,7 @@ che_viewer::operator che *& () void che_viewer::init(che * mesh, const bool & normalize) { glGenVertexArrays(1, &vao); - glGenBuffers(5, vbo); + glGenBuffers(6, vbo); this->mesh = mesh; this->normalize = normalize; @@ -82,7 +82,7 @@ void che_viewer::update_vbo() glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 HEAT MAP COLOR - glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->hm_color(0), GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 1, GL_VERTEX_T, GL_FALSE, 0, 0); @@ -91,7 +91,7 @@ void che_viewer::update_vbo() // 3 INDEXES if(!mesh->is_pointcloud()) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges() * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -107,7 +107,7 @@ void che_viewer::update_instances_translations(const vector & translatio glBindVertexArray(vao); // 4 INSTANCES (translations) - glBindBuffer(GL_ARRAY_BUFFER, vbo[4]); + glBindBuffer(GL_ARRAY_BUFFER, vbo[5]); glBufferData(GL_ARRAY_BUFFER, n_instances * sizeof(vertex), translations.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 3, GL_VERTEX_T, GL_FALSE, 0, 0); @@ -123,7 +123,7 @@ void che_viewer::draw(shader & program) program.enable(); glBindVertexArray(vao); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[3]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0, n_instances); From 5b26b5b252861ba3aaec093823c979356b53912d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 15:04:30 +0200 Subject: [PATCH 0344/1018] rt_embree: render with color --- include/raytracing/rt_embree.h | 10 ++++++++-- src/raytracing/rt_embree.cpp | 16 +++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index cbc91a49..322bcecf 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -53,6 +53,12 @@ class embree : public raytracing return {ray.dir_x, ray.dir_y, ray.dir_z}; } + const glm::vec3 color(const che * mesh) + { + const vertex & c = mesh->color(hit.primID); + return glm::vec3(c.x, c.y, c.z); + } + const glm::vec3 normal(const che * mesh, const bool & flat = false) const { if(flat || mesh->is_pointcloud()) @@ -92,8 +98,8 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); - float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r); - glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near = 1e-5f); + float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit & r); + glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 00c29d4a..63aa4990 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -134,11 +134,12 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & r) +float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit & r) { float w, sum_w = 0; position = glm::vec3(0); normal = glm::vec3(0); + color = glm::vec3(0); do { @@ -147,6 +148,7 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & sum_w += w = pc_radius - glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); position += w * r.position(); normal += w * r.normal(geomID_mesh[r.hit.geomID], true); + color += w * r.color(geomID_mesh[r.hit.geomID]); r = ray_hit(r.position(), r.dir()); } @@ -154,14 +156,13 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, ray_hit & position /= sum_w; normal /= sum_w; + color /= sum_w; return sum_w; } -glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const float & near) +glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near) { - const glm::vec3 color(0.5, 0.7, 1.0); - const glm::vec3 wi = normalize(light - position); const float dist_light = glm::length(light - position); const float falloff = 8.f / (dist_light * dist_light); // intensity multiplier / falloff @@ -182,19 +183,20 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) glm::vec4 L(0); float near; - glm::vec3 position, normal; + glm::vec3 position, normal, color; while(tfar < max_tfar) { total_tfar += tfar; position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); + color = r.color(geomID_mesh[r.hit.geomID]); near = 1e-5f; if(geomID_mesh[r.hit.geomID]->is_pointcloud()) - near += pointcloud_hit(position, normal, r); + near += pointcloud_hit(position, normal, color, r); - L += li(light, position, normal, near); + L += li(light, position, normal, color, near); /* r = ray_hit(r.position(), r.dir()); From 2ef917b2408ad2876557a4c6ab7b28d6f321f11a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 15:15:07 +0200 Subject: [PATCH 0345/1018] update .travis.yml focal --- .travis.yml | 21 +++++++++------------ CMakeLists.txt | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index ccb4758d..d3299643 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -dist: bionic +dist: focal language: cpp compiler: gcc os: linux @@ -10,21 +10,20 @@ jobs: - name: gproshan_cuda env: CUDA_BIN=/usr/local/cuda/bin before_install: - - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin - - sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 - - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub - - sudo add-apt-repository "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" - - sudo apt update - - sudo apt -y install cuda + - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin + - sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 + - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub + - sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" + - sudo apt-get update + - sudo apt-get -y install cuda addons: apt: sources: - ubuntu-toolchain-r-test packages: - - gcc-8 - - g++-8 - - cmake + - build-essential + - cmake - libarmadillo-dev - libeigen3-dev - libcgal-dev @@ -36,8 +35,6 @@ addons: - cimg-dev script: - - export CC=/usr/bin/gcc-8 - - export CXX=/usr/bin/g++-8 - export PATH=${CUDA_BIN}:${PATH} - $CC -v && $CXX -v && cmake --version - mkdir build && cd build diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ac52a1f..cbaf8af2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.12) +cmake_minimum_required(VERSION 3.16) project(gproshan VERSION 3.0) @@ -17,7 +17,7 @@ if(CUDA_FOUND) add_definitions(-DGPROSHAN_CUDA) include_directories(${CUDA_INCLUDE_DIRS}) - find_package(OptiX 7.0) + find_package(OptiX 7.1) if(OptiX_INCLUDE) set(CUDA_HOST_COMPILER ${CMAKE_CUDA_HOST_COMPILER}) add_definitions(-DGPROSHAN_OPTIX) From 0b07a5b6f8b88dc504b52582863c303b9088b25b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 15:39:14 +0200 Subject: [PATCH 0346/1018] fix .travis.yml tabs and update color map menu --- .travis.yml | 8 +++--- include/viewer/viewer.h | 4 +-- shaders/colormap.glsl | 47 ++++++++++++++++---------------- shaders/fragment.glsl | 6 +--- shaders/fragment_pointcloud.glsl | 6 +--- 5 files changed, 32 insertions(+), 39 deletions(-) diff --git a/.travis.yml b/.travis.yml index d3299643..7c8a2450 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,9 +13,9 @@ jobs: - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - - sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - - sudo apt-get update - - sudo apt-get -y install cuda + - sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" + - sudo apt update + - sudo apt -y install cuda addons: apt: @@ -23,7 +23,7 @@ addons: - ubuntu-toolchain-r-test packages: - build-essential - - cmake + - cmake - libarmadillo-dev - libeigen3-dev - libcgal-dev diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 6f6aa6ec..98615aec 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -59,8 +59,8 @@ class viewer int viewport_width; int viewport_height; - unsigned int idx_colormap = 0; // colormap index defined in shaders/colormap.glsl - const std::vector colormap = {"blue", "red", "blue/read", "mesh"}; + unsigned int idx_colormap = 1; // colormap index defined in shaders/colormap.glsl + const std::vector colormap = {"vertex color", "blue", "red", "blue/read"}; shader shader_triangles; shader shader_normals; shader shader_gradient; diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl index 2c270791..f5c25e17 100644 --- a/shaders/colormap.glsl +++ b/shaders/colormap.glsl @@ -1,6 +1,6 @@ // https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-PuBu.frag -float colormap_red_0(float x) +float colormap_red_1(float x) { if (x < 0.7520372909206926) return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; @@ -8,7 +8,7 @@ float colormap_red_0(float x) return -1.20830453148990E+01 * x + 1.44337397593436E+01; } -float colormap_green_0(float x) +float colormap_green_1(float x) { if (x < 0.7485333535031721) return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; @@ -16,7 +16,7 @@ float colormap_green_0(float x) return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; } -float colormap_blue_0(float x) +float colormap_blue_1(float x) { if (x < 0.7628468501376879) return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; @@ -24,28 +24,28 @@ float colormap_blue_0(float x) return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; } -vec3 colormap_0(float x) +vec3 colormap_1(float x) { - float r = clamp(colormap_red_0(x) / 255.0, .0, .9); - float g = clamp(colormap_green_0(x) / 255.0, .0, .9); - float b = clamp(colormap_blue_0(x) / 255.0, .0, .9); + float r = clamp(colormap_red_1(x) / 255.0, .0, .9); + float g = clamp(colormap_green_1(x) / 255.0, .0, .9); + float b = clamp(colormap_blue_1(x) / 255.0, .0, .9); return vec3(r, g, b); } // https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-YIOrBr.frag -float colormap_red_1(float x) +float colormap_red_2(float x) { return ((((1.30858855846896E+03 * x - 2.84649723684787E+03) * x + 1.76048857883363E+03) * x - 3.99775093706324E+02) * x + 2.69759225316811E+01) * x + 2.54587325383574E+02; } -float colormap_green_1(float x) +float colormap_green_2(float x) { return ((((-8.85605750526301E+02 * x + 2.20590941129997E+03) * x - 1.50123293069936E+03) * x + 2.38490009587258E+01) * x - 6.03460495073813E+01) * x + 2.54768707485247E+02; } -float colormap_blue_1(float x) +float colormap_blue_2(float x) { if (x < 0.2363454401493073) return (-3.68734834041388E+01 * x - 3.28163398692792E+02) * x + 2.27342862588147E+02; @@ -55,18 +55,18 @@ float colormap_blue_1(float x) return 1.68513761929930E+01 * x - 1.06424668227935E+01; } -vec3 colormap_1(float x) +vec3 colormap_2(float x) { - float r = clamp(colormap_red_1(x) / 255.0, 0.0, 1.0); - float g = clamp(colormap_green_1(x) / 255.0, 0.0, 1.0); - float b = clamp(colormap_blue_1(x) / 255.0, 0.0, 1.0); + float r = clamp(colormap_red_2(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green_2(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue_2(x) / 255.0, 0.0, 1.0); return vec3(r, g, b); } -// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_Blue-Red_2.frag +// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_Blue-Red_3.frag -float colormap_red_2(float x) +float colormap_red_3(float x) { if (x < 0.75) return 1012.0 * x - 389.0; @@ -74,7 +74,7 @@ float colormap_red_2(float x) return -1.11322769567548E+03 * x + 1.24461193212872E+03; } -float colormap_green_2(float x) +float colormap_green_3(float x) { if (x < 0.5) return 1012.0 * x - 129.0; @@ -82,7 +82,7 @@ float colormap_green_2(float x) return -1012.0 * x + 899.0; } -float colormap_blue_2(float x) +float colormap_blue_3(float x) { if (x < 0.25) return 1012.0 * x + 131.0; @@ -90,18 +90,19 @@ float colormap_blue_2(float x) return -1012.0 * x + 643.0; } -vec3 colormap_2(float x) +vec3 colormap_3(float x) { - float r = clamp(colormap_red_2(x) / 255.0, 0.0, 1.0); - float g = clamp(colormap_green_2(x) / 255.0, 0.0, 1.0); - float b = clamp(colormap_blue_2(x) / 255.0, 0.0, 1.0); + float r = clamp(colormap_red_3(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green_3(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue_3(x) / 255.0, 0.0, 1.0); return vec3(r, g, b); } vec3 colormap(uint i, float x) { - if(i == 0) return colormap_0(x); if(i == 1) return colormap_1(x); if(i == 2) return colormap_2(x); + if(i == 3) return colormap_3(x); + return vec3(0, 0, 0); } diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index fe450c95..cfed274c 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -41,11 +41,7 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color; - if(idx_colormap == 3) - color = gs_mesh_color; - else - color = colormap(idx_colormap, gs_color); + vec3 color = idx_colormap > 0 ? colormap(idx_colormap, gs_color) : gs_mesh_color; // lines if(render_lines) diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 8a81002f..72824ae1 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -37,11 +37,7 @@ float fresnel(vec3 N, vec3 E) void main() { - vec3 color; - if(idx_colormap == 3) - color = vs_mesh_color; - else - color = colormap(idx_colormap, vs_color); + vec3 color = idx_colormap > 0 ? colormap(idx_colormap, vs_color) : vs_mesh_color; // lines if(render_lines) From 8b1bc2c7d2e6597da9f9a852f62c3c1c10301845 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 16:02:10 +0200 Subject: [PATCH 0347/1018] rename hm_color to heatmap --- .travis.yml | 2 -- include/che.h | 8 ++++---- src/app_viewer.cpp | 38 +++++++++++++++++++------------------- src/che.cpp | 6 +++--- src/viewer/che_viewer.cpp | 2 +- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c8a2450..0f3a9aab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,6 @@ jobs: addons: apt: - sources: - - ubuntu-toolchain-r-test packages: - build-essential - cmake diff --git a/include/che.h b/include/che.h index 669ab084..6b879047 100644 --- a/include/che.h +++ b/include/che.h @@ -47,8 +47,8 @@ class che index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - vertex * VC = nullptr; ///< vertex color : v -> - real_t * VHC = nullptr; ///< vertex color heat map : v -> hm_color(v) + vertex * VC = nullptr; ///< vertex color : v -> color(v) + real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) bool manifold = true; @@ -72,8 +72,8 @@ class che void update_colors(const real_t * vcolor = nullptr, real_t max_color = 0); const vertex & color(const index_t & v) const; vertex & color(const index_t & v); - const real_t & hm_color(const index_t & v) const; - real_t & hm_color(const index_t & v); + const real_t & heatmap(const index_t & v) const; + real_t & heatmap(const index_t & v); void update_normals(); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a377ce71..2c0f32bd 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -129,7 +129,7 @@ bool paint_holes_vertices(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - if(v >= nv) mesh->hm_color(v) = .25; + if(v >= nv) mesh->heatmap(v) = .25; return false; } @@ -262,7 +262,7 @@ bool app_viewer::process_threshold(viewer * p_view) che_viewer & mesh = view->active_mesh(); for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) = mesh->hm_color(v) > 0.5 ? 1 : 0.5; + mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; return false; } @@ -302,7 +302,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) = eigvec(v, k); + mesh->heatmap(v) = eigvec(v, k); mesh.update_vbo(); } @@ -346,13 +346,13 @@ bool app_viewer::process_wks(viewer * p_view) for(int k = 1; k < K; k++) s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - mesh->hm_color(v) = norm(s); - max_s = max(max_s, mesh->hm_color(v)); + mesh->heatmap(v) = norm(s); + max_s = max(max_s, mesh->heatmap(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) /= max_s; + mesh->heatmap(v) /= max_s; } return true; @@ -393,14 +393,14 @@ bool app_viewer::process_hks(viewer * p_view) for(int k = 1; k < K; k++) s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); - mesh->hm_color(v) = norm(abs(arma::fft(s, 128))); - //mesh->hm_color(v) = norm(s); - max_s = max(max_s, mesh->hm_color(v)); + mesh->heatmap(v) = norm(abs(arma::fft(s, 128))); + //mesh->heatmap(v) = norm(s); + max_s = max(max_s, mesh->heatmap(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) /= max_s; + mesh->heatmap(v) /= max_s; } return true; @@ -439,13 +439,13 @@ bool app_viewer::process_gps(viewer * p_view) #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) { - mesh->hm_color(v) = norm(eigvec.row(v)); - max_s = max(max_s, mesh->hm_color(v)); + mesh->heatmap(v) = norm(eigvec.row(v)); + max_s = max(max_s, mesh->heatmap(v)); } #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) /= max_s; + mesh->heatmap(v) /= max_s; } return true; @@ -481,7 +481,7 @@ bool app_viewer::process_key_components(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) = (real_t) kcs(v) / kcs; + mesh->heatmap(v) = (real_t) kcs(v) / kcs; return false; } @@ -503,7 +503,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) { p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); for(auto & u: p.vertices) - mesh->hm_color(u) = 1; + mesh->heatmap(u) = 1; vdir.x = p.T(0, 0); vdir.y = p.T(0, 1); @@ -655,7 +655,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices(); v++) { if(toplesets[v] < n_toplesets) - mesh->hm_color(v) = real_t(toplesets[v]) / (n_toplesets); + mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); } gproshan_debug_var(n_toplesets); @@ -688,8 +688,8 @@ bool app_viewer::process_voronoi(viewer * p_view) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices(); i++) { - mesh->hm_color(i) = ptp.clusters[i]; - mesh->hm_color(i) /= mesh.selected.size() + 1; + mesh->heatmap(i) = ptp.clusters[i]; + mesh->heatmap(i) /= mesh.selected.size() + 1; } return false; @@ -973,7 +973,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->hm_color(v) = f(gv(v)); + mesh->heatmap(v) = f(gv(v)); return false; } diff --git a/src/che.cpp b/src/che.cpp index 25f546e6..ae05fea8 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -247,7 +247,7 @@ void che::update_colors(const real_t * vcolor, real_t max_color) { #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VHC[v] = 0.5; + VHC[v] = 0.45; return; } @@ -277,13 +277,13 @@ vertex & che::color(const index_t & v) return VC[v]; } -const real_t & che::hm_color(const index_t & v) const +const real_t & che::heatmap(const index_t & v) const { assert(VHC && v < n_vertices_); return VHC[v]; } -real_t & che::hm_color(const index_t & v) +real_t & che::heatmap(const index_t & v) { assert(VHC && v < n_vertices_); return VHC[v]; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 4f0d58ec..0a892f74 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -83,7 +83,7 @@ void che_viewer::update_vbo() // 3 HEAT MAP COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->hm_color(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); From 56507f99c017d938c417c476bd2f5ccc261b9a78 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 16:51:33 +0200 Subject: [PATCH 0348/1018] update README.md --- README.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 57c4e9d8..67be7000 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 18.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | +| `Linux Ubuntu 20.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | Install all dependencies and run: @@ -24,12 +24,13 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 8.4, cuda >= 11.0, cmake >= 3.12, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot +g++ >= 9.3, cuda >= 11.0, cmake >= 3.16, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot In Ubuntu (>= 18.04) you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot +You can build and run with g++ >= 8.4 and cmake >= 3.12, but for the std::filesystem iterator need to add some flags to compile. ## Contributions @@ -44,7 +45,7 @@ approach is competitive with the current methods and is simple to implement. Ple [A minimalistic approach for fast computation of geodesic distances on triangular meshes](https://doi.org/10.1016/j.cag.2019.08.014) ```bibtex -@Article{ROMEROCALLA2019, +@Article{RFM19, author = { {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. and Montenegro, Anselmo A. }, title = { A minimalistic approach for fast computation of geodesic distances on triangular meshes }, journal = { Computers \& Graphics }, @@ -68,13 +69,13 @@ our work: ```bibtex @ARTICLE{2018arXiv181008266F, - author = { {Fuentes Perez}, L.~J. and {Romero Calla}, L.~A. and {Montenegro}, A.~A. }, + author = { {Fuentes Perez}, Lizeth J. and {Romero Calla}, Luciano A. and {Montenegro}, Anselmo A. }, title = { Dictionary Learning-based Inpainting on Triangular Meshes }, journal = { ArXiv e-prints }, eprint = { 1810.08266 }, year = 2018, month = oct, - url = { https://arxiv.org/abs/1810.08266 } + url = { https://arxiv.org/abs/1810.08266 } } ``` @@ -97,14 +98,14 @@ Please cite our paper (in Spanish): [Efficient approach for interest points detection in non-rigid shapes](https://doi.org/10.1109/CLEI.2015.7359459) ```bibtex -@INPROCEEDINGS{7359459, - author = { C. J. Lopez Del Alamo and L. A. Romero Calla and L. J. Fuentes Perez }, - booktitle = { 2015 Latin American Computing Conference (CLEI) }, +@INPROCEEDINGS{LRF2015, + author = { {Lopez Del Alamo} Cristian J. and {Romero Calla} Luciano A. and {Fuentes Perez} Lizeth J. }, + booktitle = { Latin American Computing Conference (CLEI) }, title = { Efficient approach for interest points detection in non-rigid shapes }, year = { 2015 }, month = { Oct }, pages = { 1-8 }, - doi = { 10.1109/CLEI.2015.7359459 } + doi = { 10.1109/CLEI.2015.7359459 } } ``` @@ -135,7 +136,9 @@ Execute: to generate the documentation in *html* and *LaTeX*. ## Viewer -The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). The current viewer uses VAO and VBO to render, and the shaders have been modified and upgraded. +- The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). +- The current viewer is done in modern OpengGL: uses VAO and VBO to render and the shaders have been modified and upgraded. +- The viewer now presents an GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). ## License From 5f5cf3d7eeb53c667e8e7d7c1915c31034a634a6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 17:01:06 +0200 Subject: [PATCH 0349/1018] Update README.md --- README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 67be7000..34da0789 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ In Ubuntu (>= 18.04) you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot -You can build and run with g++ >= 8.4 and cmake >= 3.12, but for the std::filesystem iterator need to add some flags to compile. +You can build and run with g++ >= 8.4 and cmake >= 3.12, but the std::filesystem iterator could present some problems then you need to add some flags to compile. ## Contributions @@ -59,6 +59,9 @@ approach is competitive with the current methods and is simple to implement. Ple Also, we have implemented the [Fast Marching algorithm](), and the [Heat method](https://www.cs.cmu.edu/~kmcrane/Projects/HeatMethod/index.html). +### Ray Tracing +We have implemented a ray tracing viewer option for mesh and pointcloud visualization using [Intel Embree](https://www.embree.org/) v3.1x and [Nvidia Optix](https://developer.nvidia.com/optix) v7.1. + ### Dictionary Learning We proposed a Dictionary Learning and Sparse Coding framework, to solve the problems of Denoising, Inpainting, and Multiresolution on triangular meshes. This work is still in process. Please cite @@ -75,7 +78,7 @@ our work: eprint = { 1810.08266 }, year = 2018, month = oct, - url = { https://arxiv.org/abs/1810.08266 } + url = { https://arxiv.org/abs/1810.08266 } } ``` @@ -105,7 +108,7 @@ Please cite our paper (in Spanish): year = { 2015 }, month = { Oct }, pages = { 1-8 }, - doi = { 10.1109/CLEI.2015.7359459 } + doi = { 10.1109/CLEI.2015.7359459 } } ``` @@ -116,7 +119,6 @@ We proposed a method based on geodesics to determine the key components. Please see and cite our final undergraduate project: [key-components report](http://repositorio.unsa.edu.pe/handle/UNSA/2575) (in Spanish). - ### Decimation We are implementing the algorithm described by the paper [Stellar Mesh Simplification Using Probabilistic Optimization](https://doi.org/10.1111/j.1467-8659.2004.00811.x), to compute a mesh simplification. @@ -128,6 +130,7 @@ See the Chapter 4 of the book [Polygon Mesh Processing](http://www.pmp-book.org/ ### Laplacian and signatures Laplace-Beltrami operator and its eigen decomposition, WKS, HKS, GPS signatures. + ## Documentation Execute: @@ -135,16 +138,18 @@ Execute: to generate the documentation in *html* and *LaTeX*. + ## Viewer - The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). - The current viewer is done in modern OpengGL: uses VAO and VBO to render and the shaders have been modified and upgraded. - The viewer now presents an GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). + ## License MIT License + ## Authors/Contributors - [Luciano Arnaldo Romero Calla](https://github.com/larc) - [Lizeth Joseline Fuentes Pérez](https://github.com/lishh) - From 9df6522b5874b6e9fb7dc06a6308688b5f1b1ee8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 17:19:41 +0200 Subject: [PATCH 0350/1018] Update README.md --- README.md | 50 +++++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 34da0789..fd4a99a1 100644 --- a/README.md +++ b/README.md @@ -30,30 +30,28 @@ In Ubuntu (>= 18.04) you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot -You can build and run with g++ >= 8.4 and cmake >= 3.12, but the std::filesystem iterator could present some problems then you need to add some flags to compile. +You can build and run with g++ >= 8.4 and cmake >= 3.12, but the std::filesystem iterator could present some errors, then you need to add some flags to compile. ## Contributions ### CHE implementation -We have implemented a [Compact Half-Edge (CHE)](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.523.7580) data structure to manipulated triangular meshes, also can be extended for other polygonal meshes. -See the paper: [CHE: A scalable topological data structure for triangular meshes](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.523.7580) for more details. +We have implemented a [Compact Half-Edge (CHE)](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.523.7580) data structure to manipulated triangular meshes, also can be extended for other polygonal meshes. See the paper: [CHE: A scalable topological data structure for triangular meshes](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.523.7580) for more details. ### Geodesics -We proposed a CPU/GPU parallel algorithm to compute geodesics distances on triangular meshes. Our -approach is competitive with the current methods and is simple to implement. Please cite our paper: +We proposed a CPU/GPU parallel algorithm to compute geodesics distances on triangular meshes. Our approach is competitive with the current methods and is simple to implement. Please cite our paper: [A minimalistic approach for fast computation of geodesic distances on triangular meshes](https://doi.org/10.1016/j.cag.2019.08.014) ```bibtex @Article{RFM19, - author = { {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. and Montenegro, Anselmo A. }, - title = { A minimalistic approach for fast computation of geodesic distances on triangular meshes }, - journal = { Computers \& Graphics }, - year = { 2019 }, - issn = { 0097-8493 }, - doi = { https://doi.org/10.1016/j.cag.2019.08.014 }, - keywords = { Geodesic distance, Fast marching, Triangular meshes, Parallel programming, Breadth-first search }, - url = { http://www.sciencedirect.com/science/article/pii/S0097849319301426 } + author = { {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. and Montenegro, Anselmo A. }, + title = { A minimalistic approach for fast computation of geodesic distances on triangular meshes }, + journal = { Computers \& Graphics }, + year = { 2019 }, + issn = { 0097-8493 }, + doi = { https://doi.org/10.1016/j.cag.2019.08.014 }, + keywords = { Geodesic distance, Fast marching, Triangular meshes, Parallel programming, Breadth-first search }, + url = { http://www.sciencedirect.com/science/article/pii/S0097849319301426 } } ``` @@ -63,9 +61,7 @@ Also, we have implemented the [Fast Marching algorithm](), and the [Heat method] We have implemented a ray tracing viewer option for mesh and pointcloud visualization using [Intel Embree](https://www.embree.org/) v3.1x and [Nvidia Optix](https://developer.nvidia.com/optix) v7.1. ### Dictionary Learning -We proposed a Dictionary Learning and Sparse Coding framework, to solve the problems of Denoising, -Inpainting, and Multiresolution on triangular meshes. This work is still in process. Please cite -our work: +We proposed a Dictionary Learning and Sparse Coding framework, to solve the problems of Denoising, Inpainting, and Multiresolution on triangular meshes. This work is still in process. Please cite our work: [A Dictionary Learning-based framework on Triangular Meshes](https://arxiv.org/abs/1810.08266) @@ -87,8 +83,7 @@ We implemented repairing mesh holes in two steps: 1. Generate a mesh to cover the hole. We modified the algorithm presented in the paper: [A robust hole-filling algorithm for triangular mesh](https://doi.org/10.1007/s00371-007-0167-y), in order to generate a planar triangular mesh using a priority queue. -2. Fit the surface described by the new points in order to minimize the variation of the surface, -solving the Poisson equation (see the Chapter 4 of the book [Polygon Mesh Processing](http://www.pmp-book.org/)) or using Biharmonic splines. +2. Fit the surface described by the new points in order to minimize the variation of the surface, solving the Poisson equation (see the Chapter 4 of the book [Polygon Mesh Processing](http://www.pmp-book.org/)) or using Biharmonic splines. Please see and cite our final undergraduate project: [mesh hole repairing report](http://repositorio.unsa.edu.pe/handle/UNSA/2576) (in Spanish). @@ -112,20 +107,15 @@ Please cite our paper (in Spanish): } ``` -Computing key-components depends on the accuracy and definition of the key points. We were inspired -by the [work of Ivan Sipiran](https://www.researchgate.net/publication/262350194_Key-component_detection_on_3D_meshes_using_local_features), -he defined for the first time the notion of a key-component in meshes. -We proposed a method based on geodesics to determine the key components. +Computing key-components depends on the accuracy and definition of the key points. We were inspired by the [work of Ivan Sipiran](https://www.researchgate.net/publication/262350194_Key-component_detection_on_3D_meshes_using_local_features), he defined for the first time the notion of a key-component in meshes. We proposed a method based on geodesics to determine the key components. Please see and cite our final undergraduate project: [key-components report](http://repositorio.unsa.edu.pe/handle/UNSA/2575) (in Spanish). ### Decimation -We are implementing the algorithm described by the paper [Stellar Mesh Simplification Using Probabilistic Optimization](https://doi.org/10.1111/j.1467-8659.2004.00811.x), -to compute a mesh simplification. +We are implementing the algorithm described by the paper [Stellar Mesh Simplification Using Probabilistic Optimization](https://doi.org/10.1111/j.1467-8659.2004.00811.x), to compute a mesh simplification. ### Fairing -We implemented Spectral and Taubin fairing algorithms to smooth a mesh surface. -See the Chapter 4 of the book [Polygon Mesh Processing](http://www.pmp-book.org/). +We implemented Spectral and Taubin fairing algorithms to smooth a mesh surface. See the Chapter 4 of the book [Polygon Mesh Processing](http://www.pmp-book.org/). ### Laplacian and signatures Laplace-Beltrami operator and its eigen decomposition, WKS, HKS, GPS signatures. @@ -140,9 +130,7 @@ to generate the documentation in *html* and *LaTeX*. ## Viewer -- The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). -- The current viewer is done in modern OpengGL: uses VAO and VBO to render and the shaders have been modified and upgraded. -- The viewer now presents an GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). +The viewer is done with modern OpenGL and a GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). ## License @@ -151,5 +139,5 @@ MIT License ## Authors/Contributors -- [Luciano Arnaldo Romero Calla](https://github.com/larc) -- [Lizeth Joseline Fuentes Pérez](https://github.com/lishh) +- [Luciano A. Romero Calla](https://github.com/larc) +- [Lizeth J. Fuentes Pérez](https://github.com/lishh) From c06a5c03f7ec45fc02d3da37e771360c3cc2c2e9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Sep 2020 20:28:06 +0200 Subject: [PATCH 0351/1018] rt_embree: shading_color --- README.md | 2 +- include/che.h | 5 ++++- include/raytracing/rt_embree.h | 5 +++-- src/app_viewer.cpp | 10 +++++----- src/che.cpp | 17 ++++++++++++----- src/viewer/che_viewer.cpp | 2 +- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 11b2e4ac..fd4a99a1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This framework integrates some algorithms and contributions focus on the areas o | Build Type | Status | | --- | --- | -| `Linux Ubuntu 20.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=raytracing)](https://travis-ci.com/larc/gproshan_dev) | +| `Linux Ubuntu 20.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | Install all dependencies and run: diff --git a/include/che.h b/include/che.h index 6b879047..c41b56b5 100644 --- a/include/che.h +++ b/include/che.h @@ -26,6 +26,8 @@ struct corr_t; class che { + vertex vcolor = { 0.75, 0.85, 1.0 }; + public: static const size_t P = 3; ///< default polygon size 3, triangular meshes @@ -69,9 +71,10 @@ class che real_t real_trig(const index_t & t) const; real_t area_vertex(const index_t & v); real_t area_surface() const; - void update_colors(const real_t * vcolor = nullptr, real_t max_color = 0); + void update_heatmaps(const real_t * hm = nullptr, real_t max_color = 0); const vertex & color(const index_t & v) const; vertex & color(const index_t & v); + vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; const real_t & heatmap(const index_t & v) const; real_t & heatmap(const index_t & v); void update_normals(); diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 322bcecf..125cf600 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -55,7 +55,8 @@ class embree : public raytracing const glm::vec3 color(const che * mesh) { - const vertex & c = mesh->color(hit.primID); + const vertex & c = mesh->is_pointcloud() ? mesh->color(hit.primID) : + mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::vec3(c.x, c.y, c.z); } @@ -64,7 +65,7 @@ class embree : public raytracing if(flat || mesh->is_pointcloud()) return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - vertex n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::normalize(glm::vec3(n.x, n.y, n.z)); } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2c0f32bd..b1a2ec41 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -796,7 +796,7 @@ bool app_viewer::process_geodesics_fm(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_colors(&fm[0]); + mesh->update_heatmaps(&fm[0]); return false; } @@ -815,7 +815,7 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_colors(&ptp[0]); + mesh->update_heatmaps(&ptp[0]); return false; } @@ -834,7 +834,7 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_colors(&heat_flow[0]); + mesh->update_heatmaps(&heat_flow[0]); return false; } @@ -868,7 +868,7 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_colors(&ptp[0]); + mesh->update_heatmaps(&ptp[0]); return false; } @@ -887,7 +887,7 @@ bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_colors(&heat_flow[0]); + mesh->update_heatmaps(&heat_flow[0]); return false; } diff --git a/src/che.cpp b/src/che.cpp index ae05fea8..84e31ceb 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -239,11 +239,11 @@ real_t che::area_surface() const return area; } -void che::update_colors(const real_t * vcolor, real_t max_color) +void che::update_heatmaps(const real_t * hm, real_t max_color) { if(!VHC) VHC = new real_t[n_vertices_]; - if(!vcolor) + if(!hm) { #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) @@ -256,13 +256,13 @@ void che::update_colors(const real_t * vcolor, real_t max_color) { #pragma omp parallel for reduction(max: max_color) for(index_t v = 0; v < n_vertices_; v++) - if(vcolor[v] < INFINITY) - max_color = max(vcolor[v], max_color); + if(hm[v] < INFINITY) + max_color = max(hm[v], max_color); } #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) - VHC[v] = vcolor[v] / max_color; + VHC[v] = hm[v] / max_color; } const vertex & che::color(const index_t & v) const @@ -277,6 +277,13 @@ vertex & che::color(const index_t & v) return VC[v]; } +vertex che::shading_color(const index_t & f, const float & u, const float & v, const float & w) const +{ + index_t he = f * che::P; + + return VC ? vertex(u * VC[VT[he]] + v * VC[VT[he + 1]] + w * VC[VT[he + 2]]) : vcolor; +} + const real_t & che::heatmap(const index_t & v) const { assert(VHC && v < n_vertices_); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 0a892f74..20c8176c 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -51,7 +51,7 @@ void che_viewer::update() factor = mesh->mean_edge(); mesh->update_normals(); - mesh->update_colors(); + mesh->update_heatmaps(); update_vbo(); } From 00a841ef02acbdab54f1bd73a16c124488f546d1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 11:37:47 +0200 Subject: [PATCH 0352/1018] Create build.yml --- .github/workflows/build.yml | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/build.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..473d5e31 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,46 @@ +name: build + +on: [push] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally + # well on Windows or Mac. You can convert this to a matrix build if you need + # cross-platform coverage. + # See: https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#configuring-a-build-matrix + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + # Some projects don't allow in-source building, so create a separate build directory + # We'll use this as our working directory for all subsequent commands + run: cmake -E make_directory ${{runner.workspace}}/build + + - name: Configure CMake + # Use a bash shell so we can use the same syntax for environment variable + # access regardless of the host operating system + shell: bash + working-directory: ${{runner.workspace}}/build + # Note the current convention is to use the -S and -B options here to specify source + # and build directories, but this is only available with CMake 3.13 and higher. + # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute the build. You can specify a specific target with "--target " + run: cmake --build . --config $BUILD_TYPE + + - name: Test + working-directory: ${{runner.workspace}}/build + shell: bash + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C $BUILD_TYPE From 4b901aee2ae3f14332c5cd2ca8e7ca028cab7692 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 11:46:24 +0200 Subject: [PATCH 0353/1018] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fd4a99a1..2fde65b4 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run +![build](https://github.com/larc/gproshan_dev/workflows/build/badge.svg?branch=dev) + | Build Type | Status | | --- | --- | | `Linux Ubuntu 20.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | From 3bb4f9fe6ea92012d9628defeb5bb4e63063ad72 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 11:58:05 +0200 Subject: [PATCH 0354/1018] Update build.yml: install dependencies --- .github/workflows/build.yml | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 473d5e31..81e165e0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: build +name: Build on: [push] @@ -12,11 +12,23 @@ jobs: # well on Windows or Mac. You can convert this to a matrix build if you need # cross-platform coverage. # See: https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#configuring-a-build-matrix - runs-on: ubuntu-latest + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - + + - name: Install Dependencies + run: sudo apt install \ + libarmadillo-dev \ + libeigen3-dev \ + libcgal-dev \ + libsuitesparse-dev \ + libopenblas-dev \ + libglew-dev \ + libglfw3-dev \ + libglm-dev \ + cimg-dev + - name: Create Build Environment # Some projects don't allow in-source building, so create a separate build directory # We'll use this as our working directory for all subsequent commands From 3423fc7395adc3a648980320b41c0f482b585978 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 12:01:49 +0200 Subject: [PATCH 0355/1018] Update build.yml --- .github/workflows/build.yml | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 81e165e0..8d96a8d7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,16 +18,18 @@ jobs: - uses: actions/checkout@v2 - name: Install Dependencies - run: sudo apt install \ - libarmadillo-dev \ - libeigen3-dev \ - libcgal-dev \ - libsuitesparse-dev \ - libopenblas-dev \ - libglew-dev \ - libglfw3-dev \ - libglm-dev \ - cimg-dev + run: | + sudo apt update + sudo apt install \ + libarmadillo-dev \ + libeigen3-dev \ + libcgal-dev \ + libsuitesparse-dev \ + libopenblas-dev \ + libglew-dev \ + libglfw3-dev \ + libglm-dev \ + cimg-dev - name: Create Build Environment # Some projects don't allow in-source building, so create a separate build directory From 2f91ed9b54f03ed10ee3506c5bdb03816136ceb7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 12:25:49 +0200 Subject: [PATCH 0356/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2fde65b4..8c01ffec 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![build](https://github.com/larc/gproshan_dev/workflows/build/badge.svg?branch=dev) +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg) | Build Type | Status | | --- | --- | From 035ae4e1953fff551845d352acbad3568b901906 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Oct 2020 14:39:00 +0200 Subject: [PATCH 0357/1018] Update build.yml --- .github/workflows/build.yml | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8d96a8d7..29488f24 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,12 +2,15 @@ name: Build on: [push] -env: - # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) - BUILD_TYPE: Release - jobs: build: + name: ${{ matrix.os }} (${{ matrix.config }}) + strategy: + fail-fast: false + matrix: + os: [ubuntu-20.04, ubuntu-18.04] + config: [Release, Debug] + # The CMake configure and build commands are platform agnostic and should work equally # well on Windows or Mac. You can convert this to a matrix build if you need # cross-platform coverage. @@ -44,17 +47,11 @@ jobs: # Note the current convention is to use the -S and -B options here to specify source # and build directories, but this is only available with CMake 3.13 and higher. # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} - name: Build working-directory: ${{runner.workspace}}/build shell: bash # Execute the build. You can specify a specific target with "--target " - run: cmake --build . --config $BUILD_TYPE + run: cmake --build . --config ${{ matrix.config }} - - name: Test - working-directory: ${{runner.workspace}}/build - shell: bash - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C $BUILD_TYPE From 1cf92a8262b89cad8cdd03689c4b77d400ae52fa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 12:26:39 +0200 Subject: [PATCH 0358/1018] update build.yml: cuda --- .github/workflows/build.yml | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 29488f24..aea7affd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,20 +2,20 @@ name: Build on: [push] +env: + CUDA_BIN: /usr/local/cuda/bin + jobs: build: name: ${{ matrix.os }} (${{ matrix.config }}) + runs-on: ${{ matrix.os }} + strategy: fail-fast: false matrix: os: [ubuntu-20.04, ubuntu-18.04] config: [Release, Debug] - - # The CMake configure and build commands are platform agnostic and should work equally - # well on Windows or Mac. You can convert this to a matrix build if you need - # cross-platform coverage. - # See: https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#configuring-a-build-matrix - runs-on: ubuntu-20.04 + cuda: [on, off] steps: - uses: actions/checkout@v2 @@ -33,7 +33,17 @@ jobs: libglfw3-dev \ libglm-dev \ cimg-dev - + + - name: Install Cuda + if: ${{ matrix.cuda }} == 'on' + run: | + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin + sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 + sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub + sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" + sudo apt update + sudo apt -y install cuda + - name: Create Build Environment # Some projects don't allow in-source building, so create a separate build directory # We'll use this as our working directory for all subsequent commands @@ -47,7 +57,9 @@ jobs: # Note the current convention is to use the -S and -B options here to specify source # and build directories, but this is only available with CMake 3.13 and higher. # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} + run: | + export PATH=${CUDA_BIN}:${PATH} + cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} - name: Build working-directory: ${{runner.workspace}}/build From d5c8dd2452f72804e55183130edfdf7405b51bb5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 12:54:47 +0200 Subject: [PATCH 0359/1018] Update build.yml --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aea7affd..a60d6cab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,6 +22,7 @@ jobs: - name: Install Dependencies run: | + $CC -v && $CXX -v && cmake --version sudo apt update sudo apt install \ libarmadillo-dev \ @@ -35,7 +36,7 @@ jobs: cimg-dev - name: Install Cuda - if: ${{ matrix.cuda }} == 'on' + if: runner.cuda == 'on' run: | wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 From 1d2da62f5855f6b428c92c02d19f834e9adb691b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 13:09:23 +0200 Subject: [PATCH 0360/1018] Update build.yml --- .github/workflows/build.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a60d6cab..f6bb5dc4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -7,22 +7,21 @@ env: jobs: build: - name: ${{ matrix.os }} (${{ matrix.config }}) + name: ${{ matrix.os }} (${{ matrix.config }}) ${{ matrix.cuda }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [ubuntu-20.04, ubuntu-18.04] + os: [ubuntu-20.04] config: [Release, Debug] - cuda: [on, off] + cuda: [cuda, ] steps: - uses: actions/checkout@v2 - name: Install Dependencies run: | - $CC -v && $CXX -v && cmake --version sudo apt update sudo apt install \ libarmadillo-dev \ @@ -36,7 +35,7 @@ jobs: cimg-dev - name: Install Cuda - if: runner.cuda == 'on' + if: matrix.cuda == 'cuda' run: | wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 From 062a4d7a7e70a551855eb92455b088e9274420ec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 13:16:37 +0200 Subject: [PATCH 0361/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f6bb5dc4..89c84b59 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,7 +15,7 @@ jobs: matrix: os: [ubuntu-20.04] config: [Release, Debug] - cuda: [cuda, ] + cuda: [cuda, ''] steps: - uses: actions/checkout@v2 From 89a334b239fd9a7b6e798f0782e0a69f1fe9eb19 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 13:31:35 +0200 Subject: [PATCH 0362/1018] cmake version check --- .github/workflows/build.yml | 3 +++ .travis.yml | 40 ------------------------------------- src/che_ptx.cpp | 12 +++++++++-- src/mdict/patch.cpp | 2 +- 4 files changed, 14 insertions(+), 43 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 89c84b59..fc914c39 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,6 +20,9 @@ jobs: steps: - uses: actions/checkout@v2 + - name: CMake version + run: cmake --version + - name: Install Dependencies run: | sudo apt update diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0f3a9aab..00000000 --- a/.travis.yml +++ /dev/null @@ -1,40 +0,0 @@ -dist: focal -language: cpp -compiler: gcc -os: linux - -jobs: - include: - - name: gproshan - env: CUDA_BIN= - - name: gproshan_cuda - env: CUDA_BIN=/usr/local/cuda/bin - before_install: - - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - - sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 - - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub - - sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - - sudo apt update - - sudo apt -y install cuda - -addons: - apt: - packages: - - build-essential - - cmake - - libarmadillo-dev - - libeigen3-dev - - libcgal-dev - - libsuitesparse-dev - - libopenblas-dev - - libglew-dev - - libglfw3-dev - - libglm-dev - - cimg-dev - -script: - - export PATH=${CUDA_BIN}:${PATH} - - $CC -v && $CXX -v && cmake --version - - mkdir build && cd build - - cmake .. && make -j 4 - diff --git a/src/che_ptx.cpp b/src/che_ptx.cpp index dc41add6..85aa4faa 100644 --- a/src/che_ptx.cpp +++ b/src/che_ptx.cpp @@ -44,20 +44,28 @@ void che_ptx::read_file(const string & file) is >> R[2] >> s; is >> tr >> s; + VN = new vertex[n_vertices_]; + VC = new vertex[n_vertices_]; + char line[256]; is.getline(line, sizeof(line)); + real_t alpha; // color for(index_t i = 0; i < n_vertices_; i++) { is.getline(line, sizeof(line)); stringstream ss(line); - ss >> GT[i]; + ss >> GT[i] >> alpha >> VC[i]; - VN[i] = T[0] - GT[i]; + VN[i] = tr - GT[i]; VN[i].unit(); } + #pragma omp parallel for + for(index_t i = 0; i < n_vertices_; i++) + VC[i] /= 255; + is.close(); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 8481c423..440c1994 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -170,7 +170,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) { size_t d_fitting = 2; size_t d_monge = 2; - size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; + //size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; assert(vertices.size() > min_points); vector in_points; From 98b33b50d66f31cbd59befc051e057ee4a14ce39 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Oct 2020 14:09:26 +0200 Subject: [PATCH 0363/1018] update cuda architectures cmake 3.17 --- CMakeLists.txt | 8 ++++++++ src/mdict/patch.cpp | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cbaf8af2..8eb9f9d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(CUDA 11) if(CUDA_FOUND) enable_language(CUDA) + set(CMAKE_CUDA_STANDARD 17) + set(CMAKE_CUDA_STANDARD_REQUIRED ON) + add_definitions(-DGPROSHAN_CUDA) include_directories(${CUDA_INCLUDE_DIRS}) @@ -122,6 +125,11 @@ target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) if(CUDA_FOUND) + set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 60 61 62) + set_property(TARGET gproshan PROPERTY CUDA_ARCHITECTURES 60 61 62) + set_property(TARGET test_geodesics PROPERTY CUDA_ARCHITECTURES 60 61 62) + set_property(TARGET test_image_denoising PROPERTY CUDA_ARCHITECTURES 60 61 62) + target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 440c1994..76bb4294 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -171,7 +171,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) size_t d_fitting = 2; size_t d_monge = 2; //size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - assert(vertices.size() > min_points); + //assert(vertices.size() > min_points); vector in_points; in_points.reserve(vertices.size()); From 12e8473fa2b269bb7b6ac13054681acd687893e6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Oct 2020 11:01:55 +0200 Subject: [PATCH 0364/1018] Update build.yml --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fc914c39..188ce217 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,6 +25,8 @@ jobs: - name: Install Dependencies run: | + sudo apt remove --purge cmake + sudo snap install cmake --classic sudo apt update sudo apt install \ libarmadillo-dev \ From 95816ce3fe4c4190495f85020412f97776c4f1d4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Oct 2020 11:58:27 +0200 Subject: [PATCH 0365/1018] update README --- CMakeLists.txt | 2 +- README.md | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8eb9f9d3..5e2588f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.18) project(gproshan VERSION 3.0) diff --git a/README.md b/README.md index 8c01ffec..00b30c04 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,6 @@ This framework integrates some algorithms and contributions focus on the areas o ![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg) -| Build Type | Status | -| --- | --- | -| `Linux Ubuntu 20.04` | [![Build Status](https://travis-ci.com/larc/gproshan_dev.svg?token=t9aCnytCvpecrZv3sMxq&branch=dev)](https://travis-ci.com/larc/gproshan_dev) | - Install all dependencies and run: mkdir build @@ -26,13 +22,12 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.3, cuda >= 11.0, cmake >= 3.16, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot +g++ >= 9.3, cuda >= 11.0, cmake >= 3.18, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot -In Ubuntu (>= 18.04) you can install them with: +In Ubuntu you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot -You can build and run with g++ >= 8.4 and cmake >= 3.12, but the std::filesystem iterator could present some errors, then you need to add some flags to compile. ## Contributions From 5234c49b82830cea4fb493270289dd8e19e7d09c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Oct 2020 11:59:13 +0200 Subject: [PATCH 0366/1018] Update build.yml --- .github/workflows/build.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 188ce217..19dcb0d1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,9 +20,6 @@ jobs: steps: - uses: actions/checkout@v2 - - name: CMake version - run: cmake --version - - name: Install Dependencies run: | sudo apt remove --purge cmake From 5fa3b1111a12594aeebb987be19e93bf8b4cca5b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Oct 2020 12:03:13 +0200 Subject: [PATCH 0367/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 00b30c04..c1d05038 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg) +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) Install all dependencies and run: From d1ac053337d888d1a86866e007c366414055a0cc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Oct 2020 12:04:32 +0200 Subject: [PATCH 0368/1018] update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1d05038..83220dbf 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=raytracing) Install all dependencies and run: From a7a4d7749584841559378b06822d67283ee6301a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 15 Oct 2020 12:57:32 +0200 Subject: [PATCH 0369/1018] fix merge dev --- src/app_viewer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index cf22041b..4e1a43d7 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -561,11 +561,11 @@ bool app_viewer::process_denoising(viewer * p_view) dict.execute(); delete phi; - mesh->update_colors(&dict[0]); + mesh->update_heatmaps(&dict[0]); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = 2 * atan(mesh->color(v) * 10) / M_PI; + mesh->color(v) = 2 * atan(mesh->heatmap(v) * 10) / M_PI; mesh->update_normals(); @@ -620,7 +620,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) inpainting dict(mesh, &phi, params); real_t max_error = dict.execute(); - mesh->update_colors(&dict[0]); + mesh->update_heatmaps(&dict[0]); mesh->update_normals(); } @@ -651,7 +651,7 @@ bool app_viewer::process_mask(viewer * p_view) dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); - mesh->update_colors(&dict[0]); + mesh->update_heatmaps(&dict[0]); string f_points = tmp_file_path(string(dict) + ".rsampl"); a_vec points_out; From 4490565218753e21c8c8039578c0bf301354bb7a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 15 Oct 2020 14:19:44 +0200 Subject: [PATCH 0370/1018] rename heatmaps to heatmap --- include/che.h | 2 +- src/app_viewer.cpp | 10 +++++----- src/che.cpp | 2 +- src/viewer/che_viewer.cpp | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/che.h b/include/che.h index c41b56b5..44325e17 100644 --- a/include/che.h +++ b/include/che.h @@ -71,7 +71,7 @@ class che real_t real_trig(const index_t & t) const; real_t area_vertex(const index_t & v); real_t area_surface() const; - void update_heatmaps(const real_t * hm = nullptr, real_t max_color = 0); + void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); const vertex & color(const index_t & v) const; vertex & color(const index_t & v); vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b1a2ec41..7e26e1a9 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -796,7 +796,7 @@ bool app_viewer::process_geodesics_fm(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmaps(&fm[0]); + mesh->update_heatmap(&fm[0]); return false; } @@ -815,7 +815,7 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmaps(&ptp[0]); + mesh->update_heatmap(&ptp[0]); return false; } @@ -834,7 +834,7 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmaps(&heat_flow[0]); + mesh->update_heatmap(&heat_flow[0]); return false; } @@ -868,7 +868,7 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmaps(&ptp[0]); + mesh->update_heatmap(&ptp[0]); return false; } @@ -887,7 +887,7 @@ bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmaps(&heat_flow[0]); + mesh->update_heatmap(&heat_flow[0]); return false; } diff --git a/src/che.cpp b/src/che.cpp index 84e31ceb..103ac3f8 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -239,7 +239,7 @@ real_t che::area_surface() const return area; } -void che::update_heatmaps(const real_t * hm, real_t max_color) +void che::update_heatmap(const real_t * hm, real_t max_color) { if(!VHC) VHC = new real_t[n_vertices_]; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 20c8176c..d1add5ce 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -51,7 +51,7 @@ void che_viewer::update() factor = mesh->mean_edge(); mesh->update_normals(); - mesh->update_heatmaps(); + mesh->update_heatmap(); update_vbo(); } From 5931456c270661e9a4040c10feac17087911a2ef Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 15 Oct 2020 19:29:06 +0200 Subject: [PATCH 0371/1018] fix merge dev --- src/app_viewer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 21076a49..32b7fa99 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -561,7 +561,7 @@ bool app_viewer::process_denoising(viewer * p_view) dict.execute(); delete phi; - mesh->update_heatmaps(&dict[0]); + mesh->update_heatmap(&dict[0]); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) @@ -620,7 +620,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) inpainting dict(mesh, &phi, params); real_t max_error = dict.execute(); - mesh->update_heatmaps(&dict[0]); + mesh->update_heatmap(&dict[0]); mesh->update_normals(); } @@ -651,7 +651,7 @@ bool app_viewer::process_mask(viewer * p_view) dict.init_radial_feature_patches(); //dict.init_voronoi_patches(); - mesh->update_heatmaps(&dict[0]); + mesh->update_heatmap(&dict[0]); string f_points = tmp_file_path(string(dict) + ".rsampl"); a_vec points_out; From 7c8222a62b89580184e101d43039bd05c0de88fc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 21 Nov 2020 15:38:25 +0100 Subject: [PATCH 0372/1018] fix vertex color gl, embree --- include/che.h | 2 +- src/che.cpp | 8 +++++++- src/che_off.cpp | 10 +++++----- src/raytracing/rt_embree.cpp | 9 +++++---- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/che.h b/include/che.h index 44325e17..f4d0957d 100644 --- a/include/che.h +++ b/include/che.h @@ -26,7 +26,7 @@ struct corr_t; class che { - vertex vcolor = { 0.75, 0.85, 1.0 }; + vertex vcolor{ 0.75, 0.85, 1.0 }; public: static const size_t P = 3; ///< default polygon size 3, triangular meshes diff --git a/src/che.cpp b/src/che.cpp index 103ac3f8..556c0b74 100644 --- a/src/che.cpp +++ b/src/che.cpp @@ -281,7 +281,7 @@ vertex che::shading_color(const index_t & f, const float & u, const float & v, c { index_t he = f * che::P; - return VC ? vertex(u * VC[VT[he]] + v * VC[VT[he + 1]] + w * VC[VT[he + 2]]) : vcolor; + return VC ? u * VC[VT[he]] + v * VC[VT[he + 1]] + w * VC[VT[he + 2]] : vcolor; } const real_t & che::heatmap(const index_t & v) const @@ -1383,6 +1383,12 @@ void che::init(const size_t & n_v, const size_t & n_f) if(n_half_edges_) OT = new index_t[n_half_edges_]; if(n_vertices_) EVT = new index_t[n_vertices_]; if(n_vertices_) EHT = new index_t[n_half_edges_]; + + if(n_vertices_) VC = new vertex[n_vertices_]; + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices_; v++) + VC[v] = vcolor; } void che::update_evt_ot_et() diff --git a/src/che_off.cpp b/src/che_off.cpp index 3af58e67..fb489bc6 100644 --- a/src/che_off.cpp +++ b/src/che_off.cpp @@ -40,18 +40,18 @@ void che_off::read_file(const string & file) if(soff[0] == 'N') VN = new vertex[n_vertices_]; - if(soff[0] == 'C' || soff[1] == 'C') - VC = new vertex[n_vertices_]; real_t alpha; // color for(index_t i = 0; i < n_vertices_; i++) { is >> GT[i]; - if(VC) is >> VC[i] >> alpha; - if(VN) is >> VN[i]; + if(soff[0] == 'C' || soff[1] == 'C') + is >> VC[i] >> alpha; + if(soff[0] == 'N') + is >> VN[i]; } - if(VC) + if(soff[0] == 'C' || soff[1] == 'C') { #pragma omp parallel for for(index_t i = 0; i < n_vertices_; i++) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 63aa4990..4897ff4a 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -165,10 +165,11 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const { const glm::vec3 wi = normalize(light - position); const float dist_light = glm::length(light - position); - const float falloff = 8.f / (dist_light * dist_light); // intensity multiplier / falloff + const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (float(1.f/M_PI) * color) + glm::vec3(0.1), 1); + const glm::vec4 L = glm::vec4((dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); + //const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (float(1.f/M_PI) * color) + glm::vec3(0.3), 1); ray_hit r(position, wi, near); return (occluded(r) ? 0.6f : 1.f) * L; @@ -191,7 +192,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); color = r.color(geomID_mesh[r.hit.geomID]); - + near = 1e-5f; if(geomID_mesh[r.hit.geomID]->is_pointcloud()) near += pointcloud_hit(position, normal, color, r); @@ -208,7 +209,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) tfar = 10; } - return L / total_tfar; + return L;// / total_tfar; } bool embree::intersect(ray_hit & r) From b0923bf0d18f27707860b9ca51bebe630ac662e2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 22 Nov 2020 00:13:13 +0100 Subject: [PATCH 0373/1018] rt embree: transparency --- src/raytracing/rt_embree.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 4897ff4a..e078dbb7 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -177,17 +177,15 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) { - const float max_tfar = 8; - float total_tfar = 0; - float tfar = r.ray.tfar; - glm::vec4 L(0); float near; glm::vec3 position, normal, color; - while(tfar < max_tfar) + + glm::vec4 L(0); + while(true) { - total_tfar += tfar; + total_tfar += r.ray.tfar; position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); @@ -197,19 +195,14 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) if(geomID_mesh[r.hit.geomID]->is_pointcloud()) near += pointcloud_hit(position, normal, color, r); - L += li(light, position, normal, color, near); + L += r.ray.tfar * li(light, position, normal, color, near); - /* r = ray_hit(r.position(), r.dir()); - if(!intersect(r)) break; - - tfar = r.ray.tfar + total_tfar; - */ - - tfar = 10; + if(!intersect(r)) + break; } - return L;// / total_tfar; + return L / total_tfar; } bool embree::intersect(ray_hit & r) From 9dc356aa4de2f41d49acee1fca65d3b95a42c4d8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 22 Nov 2020 00:29:37 +0100 Subject: [PATCH 0374/1018] rt embree fix point cloud ray tfar --- include/raytracing/rt_embree.h | 2 +- src/raytracing/rt_embree.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 125cf600..0e345569 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -99,7 +99,7 @@ class embree : public raytracing index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); - float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit & r); + float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index e078dbb7..e49c97ad 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -134,7 +134,7 @@ index_t embree::add_point_cloud(const che * mesh) return geom_id; } -float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit & r) +float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) { float w, sum_w = 0; position = glm::vec3(0); From b6c0fed4eb5e2333897e498d9d4e9aeb35ae7682 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 22 Nov 2020 17:16:16 +0100 Subject: [PATCH 0375/1018] Update README.md --- README.md | 58 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c1d05038..98312ffb 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,12 @@ We proposed a CPU/GPU parallel algorithm to compute geodesics distances on trian ```bibtex @Article{RFM19, - author = { {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. and Montenegro, Anselmo A. }, - title = { A minimalistic approach for fast computation of geodesic distances on triangular meshes }, - journal = { Computers \& Graphics }, - year = { 2019 }, - issn = { 0097-8493 }, - doi = { https://doi.org/10.1016/j.cag.2019.08.014 }, - keywords = { Geodesic distance, Fast marching, Triangular meshes, Parallel programming, Breadth-first search }, - url = { http://www.sciencedirect.com/science/article/pii/S0097849319301426 } + author = { {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. and Montenegro, Anselmo A. }, + title = { A minimalistic approach for fast computation of geodesic distances on triangular meshes }, + issn = { 0097-8493 }, + year = { 2019 }, + doi = { 10.1016/j.cag.2019.08.014 }, + journaltitle = { Computers \& Graphics } } ``` @@ -60,18 +58,31 @@ We have implemented a ray tracing viewer option for mesh and pointcloud visualiz ### Dictionary Learning We proposed a Dictionary Learning and Sparse Coding framework, to solve the problems of Denoising, Inpainting, and Multiresolution on triangular meshes. This work is still in process. Please cite our work: -[A Dictionary Learning-based framework on Triangular Meshes](https://arxiv.org/abs/1810.08266) +[A Robust Feature-aware Sparse Mesh Representation](https://diglib.eg.org/handle/10.2312/pg20201226) ```bibtex +@InProceedings{FRMMP20, + booktitle = { Pacific Graphics Short Papers, Posters, and Work-in-Progress Papers}, + title = { {A Robust Feature-aware Sparse Mesh Representation} }, + author = { {Fuentes Perez}, Lizeth J. and {Romero Calla}, Luciano A. and Montenegro, Anselmo A. and Mura, Claudio and Pajarola, Renato }, + year = { 2020 }, + publisher = { The Eurographics Association }, + ISBN = { 978-3-03868-120-5 }, + DOI = { 10.2312/pg.20201226 } +} +``` +[A Dictionary Learning-based framework on Triangular Meshes](https://arxiv.org/abs/1810.08266) + +```bibtex @ARTICLE{2018arXiv181008266F, - author = { {Fuentes Perez}, Lizeth J. and {Romero Calla}, Luciano A. and {Montenegro}, Anselmo A. }, - title = { Dictionary Learning-based Inpainting on Triangular Meshes }, - journal = { ArXiv e-prints }, - eprint = { 1810.08266 }, - year = 2018, - month = oct, - url = { https://arxiv.org/abs/1810.08266 } + author = { {Fuentes Perez}, L.~J. and {Romero Calla}, L.~A. and {Montenegro}, A.~A. }, + title = { Dictionary Learning-based Inpainting on Triangular Meshes }, + journal = { ArXiv e-prints }, + eprint = { 1810.08266 }, + year = 2018, + month = oct, + url = { https://arxiv.org/abs/1810.08266 } } ``` @@ -93,14 +104,13 @@ Please cite our paper (in Spanish): [Efficient approach for interest points detection in non-rigid shapes](https://doi.org/10.1109/CLEI.2015.7359459) ```bibtex -@INPROCEEDINGS{LRF2015, - author = { {Lopez Del Alamo} Cristian J. and {Romero Calla} Luciano A. and {Fuentes Perez} Lizeth J. }, - booktitle = { Latin American Computing Conference (CLEI) }, - title = { Efficient approach for interest points detection in non-rigid shapes }, - year = { 2015 }, - month = { Oct }, - pages = { 1-8 }, - doi = { 10.1109/CLEI.2015.7359459 } +@InProceedings{LRF15, + author = { {Lopez Del Alamo}, Cristian J. and {Romero Calla}, Luciano A. and {Fuentes Perez}, Lizeth J. }, + title = { Efficient approach for interest points detection in non-rigid shapes }, + booktitle = { Latin American Computing Conference (CLEI) }, + pages = { 1-8 }, + year = { 2015 }, + doi = { 10.1109/CLEI.2015.7359459 } } ``` From 6c26e01a0849edf384122e5b2158da5b95891d8b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 23 Nov 2020 10:23:28 +0100 Subject: [PATCH 0376/1018] update imgui --- imgui/imconfig.h | 8 +- imgui/imgui.cpp | 1518 +++++++++++++++++++--------------- imgui/imgui.h | 254 +++--- imgui/imgui_demo.cpp | 306 ++++--- imgui/imgui_draw.cpp | 32 +- imgui/imgui_impl_glfw.cpp | 12 +- imgui/imgui_impl_glfw.h | 8 +- imgui/imgui_impl_opengl3.cpp | 43 +- imgui/imgui_impl_opengl3.h | 10 +- imgui/imgui_internal.h | 288 ++++--- imgui/imgui_widgets.cpp | 680 +++++++++------ imgui/imstb_textedit.h | 72 +- src/raytracing/rt_embree.cpp | 2 +- 13 files changed, 1896 insertions(+), 1337 deletions(-) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index 6b87dd6c..736958b3 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -31,7 +31,7 @@ // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended. -//#define IMGUI_DISABLE_METRICS_WINDOW // Disable debug/metrics window: ShowMetricsWindow() will be empty. +//#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger window: ShowMetricsWindow() will be empty. //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. @@ -49,7 +49,7 @@ //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) //#define IMGUI_USE_BGRA_PACKED_COLOR -//---- Use 32-bit for ImWchar (default is 16-bit) to support full unicode code points. +//---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...) //#define IMGUI_USE_WCHAR32 //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version @@ -76,12 +76,12 @@ */ //---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices. -// Your renderer back-end will need to support it (most example renderer back-ends support both 16/32-bit indices). +// Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices). // Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer. // Read about ImGuiBackendFlags_RendererHasVtxOffset for details. //#define ImDrawIdx unsigned int -//---- Override ImDrawCallback signature (will need to modify renderer back-ends accordingly) +//---- Override ImDrawCallback signature (will need to modify renderer backends accordingly) //struct ImDrawList; //struct ImDrawCmd; //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index a3c7e72a..34c0da3c 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (main code and documentation) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/3075 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/3488 (please post your screenshots/video there!) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues @@ -76,7 +76,7 @@ CODE // [SECTION] LOGGING/CAPTURING // [SECTION] SETTINGS // [SECTION] PLATFORM DEPENDENT HELPERS -// [SECTION] METRICS/DEBUG WINDOW +// [SECTION] METRICS/DEBUGGER WINDOW */ @@ -92,14 +92,13 @@ CODE - Easy to use to create code-driven and data-driven tools. - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools. - Easy to hack and improve. - - Minimize screen real-estate usage. - Minimize setup and maintenance. - Minimize state storage on user side. - Portable, minimize dependencies, run on target (consoles, phones, etc.). - - Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window,. - opening a tree node for the first time, etc. but a typical frame should not allocate anything). + - Efficient runtime and memory consumption. + + Designed for developers and content-creators, not the typical end-user! Some of the current weaknesses includes: - Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes: - Doesn't look fancy, doesn't animate. - Limited layout features, intricate layouts are typically crafted in code. @@ -168,21 +167,21 @@ CODE GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE --------------------------------------------------------------- - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - - In the majority of cases you should be able to use unmodified back-ends files available in the examples/ folder. - - Add the Dear ImGui source files + selected back-end source files to your projects or using your preferred build system. + - In the majority of cases you should be able to use unmodified backends files available in the examples/ folder. + - Add the Dear ImGui source files + selected backend source files to your projects or using your preferred build system. It is recommended you build and statically link the .cpp files as part of your project and NOT as shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide. Effectively it means you can create widgets at any time in your code, regardless of considerations of being in "update" vs "render" phases of your own application. All rendering information are stored into command-lists that you will retrieve after calling ImGui::Render(). - - Refer to the bindings and demo applications in the examples/ folder for instruction on how to setup your code. + - Refer to the backends and demo applications in the examples/ folder for instruction on how to setup your code. - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder. HOW A SIMPLE APPLICATION MAY LOOK LIKE -------------------------------------- - EXHIBIT 1: USING THE EXAMPLE BINDINGS (= imgui_impl_XXX.cpp files from the examples/ folder). + EXHIBIT 1: USING THE EXAMPLE BACKENDS (= imgui_impl_XXX.cpp files from the backends/ folder). The sub-folders in examples/ contains examples applications following this structure. // Application init: create a dear imgui context, setup some options, load fonts @@ -192,7 +191,7 @@ CODE // TODO: Fill optional fields of the io structure later. // TODO: Load TTF/OTF fonts if you don't want to use the default font. - // Initialize helper Platform and Renderer bindings (here we are using imgui_impl_win32.cpp and imgui_impl_dx11.cpp) + // Initialize helper Platform and Renderer backends (here we are using imgui_impl_win32.cpp and imgui_impl_dx11.cpp) ImGui_ImplWin32_Init(hwnd); ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); @@ -218,7 +217,7 @@ CODE ImGui_ImplWin32_Shutdown(); ImGui::DestroyContext(); - EXHIBIT 2: IMPLEMENTING CUSTOM BINDING / CUSTOM ENGINE + EXHIBIT 2: IMPLEMENTING CUSTOM BACKEND / CUSTOM ENGINE // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); @@ -243,7 +242,7 @@ CODE while (true) { // Setup low-level inputs, e.g. on Win32: calling GetKeyboardState(), or write to those fields from your Windows message handlers, etc. - // (In the examples/ app this is usually done within the ImGui_ImplXXX_NewFrame() function from one of the demo Platform bindings) + // (In the examples/ app this is usually done within the ImGui_ImplXXX_NewFrame() function from one of the demo Platform Backends) io.DeltaTime = 1.0f/60.0f; // set the time elapsed since the previous frame (in seconds) io.DisplaySize.x = 1920.0f; // set the current display width io.DisplaySize.y = 1280.0f; // set the current display height here @@ -279,7 +278,7 @@ CODE HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE --------------------------------------------- - The bindings in impl_impl_XXX.cpp files contains many working implementations of a rendering function. + The backends in impl_impl_XXX.cpp files contains many working implementations of a rendering function. void void MyImGuiRenderFunction(ImDrawData* draw_data) { @@ -358,7 +357,7 @@ CODE - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. - When that happens your back-end NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the binding in examples/ do that. + When that happens your backend NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the backends in examples/ do that. (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse as moving back and forth!) (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want to set a boolean to ignore your other external mouse positions until the external source is moved again.) @@ -372,7 +371,24 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. - - 2020/09/17 (1.79) - removed ImFont::DisplayOffset in favor of ImFontConfig::GlyphOffset. DisplayOffset was applied after scaling and not very meaningful/useful outside of being needed by the default ProggyClean font. It was also getting in the way of better font scaling, so let's get rid of it now! + - 2020/11/18 (1.80) - renamed undocumented/internals ImGuiColumnsFlags_* to ImGuiOldColumnFlags_* in prevision of incoming Tables API. + - 2020/11/03 (1.80) - renamed io.ConfigWindowsMemoryCompactTimer to io.ConfigMemoryCompactTimer as the feature will apply to other data structures + - 2020/10/14 (1.80) - backends: moved all backends files (imgui_impl_XXXX.cpp, imgui_impl_XXXX.h) from examples/ to backends/. + - 2020/10/12 (1.80) - removed redirecting functions/enums that were marked obsolete in 1.60 (April 2018): + - io.RenderDrawListsFn pointer -> use ImGui::GetDrawData() value and call the render function of your backend + - ImGui::IsAnyWindowFocused() -> use ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow) + - ImGui::IsAnyWindowHovered() -> use ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) + - ImGuiStyleVar_Count_ -> use ImGuiStyleVar_COUNT + - ImGuiMouseCursor_Count_ -> use ImGuiMouseCursor_COUNT + - removed redirecting functions names that were marked obsolete in 1.61 (May 2018): + - InputFloat (... int decimal_precision ...) -> use InputFloat (... const char* format ...) with format = "%.Xf" where X is your value for decimal_precision. + - same for InputFloat2()/InputFloat3()/InputFloat4() variants taking a `int decimal_precision` parameter. + - 2020/10/05 (1.79) - removed ImGuiListClipper: Renamed constructor parameters which created an ambiguous alternative to using the ImGuiListClipper::Begin() function, with misleading edge cases (note: imgui_memory_editor <0.40 from imgui_club/ used this old clipper API. Update your copy if needed). + - 2020/09/25 (1.79) - renamed ImGuiSliderFlags_ClampOnInput to ImGuiSliderFlags_AlwaysClamp. Kept redirection enum (will obsolete sooner because previous name was added recently). + - 2020/09/25 (1.79) - renamed style.TabMinWidthForUnselectedCloseButton to style.TabMinWidthForCloseButton. + - 2020/09/21 (1.79) - renamed OpenPopupContextItem() back to OpenPopupOnItemClick(), reverting the change from 1.77. For varieties of reason this is more self-explanatory. + - 2020/09/21 (1.79) - removed return value from OpenPopupOnItemClick() - returned true on mouse release on item - because it is inconsistent with other popup APIs and makes others misleading. It's also and unnecessary: you can use IsWindowAppearing() after BeginPopup() for a similar result. + - 2020/09/17 (1.79) - removed ImFont::DisplayOffset in favor of ImFontConfig::GlyphOffset. DisplayOffset was applied after scaling and not very meaningful/useful outside of being needed by the default ProggyClean font. If you scaled this value after calling AddFontDefault(), this is now done automatically. It was also getting in the way of better font scaling, so let's get rid of it now! - 2020/08/17 (1.78) - obsoleted use of the trailing 'float power=1.0f' parameter for DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(), DragFloatRange2(), DragScalar(), DragScalarN(), SliderFloat(), SliderFloat2(), SliderFloat3(), SliderFloat4(), SliderScalar(), SliderScalarN(), VSliderFloat() and VSliderScalar(). replaced the 'float power=1.0f' argument with integer-based flags defaulting to 0 (as with all our flags). worked out a backward-compatibility scheme so hopefully most C++ codebase should not be affected. in short, when calling those functions: @@ -384,7 +400,7 @@ CODE for shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. - obsoleted use of v_min > v_max in DragInt, DragFloat, DragScalar to lock edits (introduced in 1.73, was not demoed nor documented very), will be replaced by a more generic ReadOnly feature. You may use the ImGuiSliderFlags_ReadOnly internal flag in the meantime. - 2020/06/23 (1.77) - removed BeginPopupContextWindow(const char*, int mouse_button, bool also_over_items) in favor of BeginPopupContextWindow(const char*, ImGuiPopupFlags flags) with ImGuiPopupFlags_NoOverItems. - - 2020/06/15 (1.77) - renamed OpenPopupOnItemClick() to OpenPopupContextItem(). Kept inline redirection function (will obsolete). + - 2020/06/15 (1.77) - renamed OpenPopupOnItemClick() to OpenPopupContextItem(). Kept inline redirection function (will obsolete). [NOTE: THIS WAS REVERTED IN 1.79] - 2020/06/15 (1.77) - removed CalcItemRectClosestPoint() entry point which was made obsolete and asserting in December 2017. - 2020/04/23 (1.77) - removed unnecessary ID (first arg) of ImFontAtlas::AddCustomRectRegular(). - 2020/01/22 (1.75) - ImDrawList::AddCircle()/AddCircleFilled() functions don't accept negative radius any more. @@ -449,10 +465,10 @@ CODE - 2018/08/01 (1.63) - renamed io.OptCursorBlink to io.ConfigCursorBlink [-> io.ConfigInputTextCursorBlink in 1.65], io.OptMacOSXBehaviors to ConfigMacOSXBehaviors for consistency. - 2018/07/22 (1.63) - changed ImGui::GetTime() return value from float to double to avoid accumulating floating point imprecisions over time. - 2018/07/08 (1.63) - style: renamed ImGuiCol_ModalWindowDarkening to ImGuiCol_ModalWindowDimBg for consistency with other features. Kept redirection enum (will obsolete). - - 2018/06/08 (1.62) - examples: the imgui_impl_xxx files have been split to separate platform (Win32, Glfw, SDL2, etc.) from renderer (DX11, OpenGL, Vulkan, etc.). - old bindings will still work as is, however prefer using the separated bindings as they will be updated to support multi-viewports. - when adopting new bindings follow the main.cpp code of your preferred examples/ folder to know which functions to call. - in particular, note that old bindings called ImGui::NewFrame() at the end of their ImGui_ImplXXXX_NewFrame() function. + - 2018/06/08 (1.62) - examples: the imgui_impl_XXX files have been split to separate platform (Win32, GLFW, SDL2, etc.) from renderer (DX11, OpenGL, Vulkan, etc.). + old backends will still work as is, however prefer using the separated backends as they will be updated to support multi-viewports. + when adopting new backends follow the main.cpp code of your preferred examples/ folder to know which functions to call. + in particular, note that old backends called ImGui::NewFrame() at the end of their ImGui_ImplXXXX_NewFrame() function. - 2018/06/06 (1.62) - renamed GetGlyphRangesChinese() to GetGlyphRangesChineseFull() to distinguish other variants and discourage using the full set. - 2018/06/06 (1.62) - TreeNodeEx()/TreeNodeBehavior(): the ImGuiTreeNodeFlags_CollapsingHeader helper now include the ImGuiTreeNodeFlags_NoTreePushOnOpen flag. See Changelog for details. - 2018/05/03 (1.61) - DragInt(): the default compile-time format string has been changed from "%.0f" to "%d", as we are not using integers internally any more. @@ -462,7 +478,7 @@ CODE - 2018/04/28 (1.61) - obsoleted InputFloat() functions taking an optional "int decimal_precision" in favor of an equivalent and more flexible "const char* format", consistent with other functions. Kept redirection functions (will obsolete). - 2018/04/09 (1.61) - IM_DELETE() helper function added in 1.60 doesn't clear the input _pointer_ reference, more consistent with expectation and allows passing r-value. - - 2018/03/20 (1.60) - renamed io.WantMoveMouse to io.WantSetMousePos for consistency and ease of understanding (was added in 1.52, _not_ used by core and only honored by some binding ahead of merging the Nav branch). + - 2018/03/20 (1.60) - renamed io.WantMoveMouse to io.WantSetMousePos for consistency and ease of understanding (was added in 1.52, _not_ used by core and only honored by some backend ahead of merging the Nav branch). - 2018/03/12 (1.60) - removed ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered as the closing cross uses regular button colors now. - 2018/03/08 (1.60) - changed ImFont::DisplayOffset.y to default to 0 instead of +1. Fixed rounding of Ascent/Descent to match TrueType renderer. If you were adding or subtracting to ImFont::DisplayOffset check if your fonts are correctly aligned vertically. - 2018/03/03 (1.60) - renamed ImGuiStyleVar_Count_ to ImGuiStyleVar_COUNT and ImGuiMouseCursor_Count_ to ImGuiMouseCursor_COUNT for consistency with other public enums. @@ -506,7 +522,7 @@ CODE - 2017/10/11 (1.52) - renamed AlignFirstTextHeightToWidgets() to AlignTextToFramePadding(). Kept inline redirection function (will obsolete). - 2017/09/26 (1.52) - renamed ImFont::Glyph to ImFontGlyph. Kept redirection typedef (will obsolete). - 2017/09/25 (1.52) - removed SetNextWindowPosCenter() because SetNextWindowPos() now has the optional pivot information to do the same and more. Kept redirection function (will obsolete). - - 2017/08/25 (1.52) - io.MousePos needs to be set to ImVec2(-FLT_MAX,-FLT_MAX) when mouse is unavailable/missing. Previously ImVec2(-1,-1) was enough but we now accept negative mouse coordinates. In your binding if you need to support unavailable mouse, make sure to replace "io.MousePos = ImVec2(-1,-1)" with "io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX)". + - 2017/08/25 (1.52) - io.MousePos needs to be set to ImVec2(-FLT_MAX,-FLT_MAX) when mouse is unavailable/missing. Previously ImVec2(-1,-1) was enough but we now accept negative mouse coordinates. In your backend if you need to support unavailable mouse, make sure to replace "io.MousePos = ImVec2(-1,-1)" with "io.MousePos = ImVec2(-FLT_MAX,-FLT_MAX)". - 2017/08/22 (1.51) - renamed IsItemHoveredRect() to IsItemRectHovered(). Kept inline redirection function (will obsolete). -> (1.52) use IsItemHovered(ImGuiHoveredFlags_RectOnly)! - renamed IsMouseHoveringAnyWindow() to IsAnyWindowHovered() for consistency. Kept inline redirection function (will obsolete). - renamed IsMouseHoveringWindow() to IsWindowRectHovered() for consistency. Kept inline redirection function (will obsolete). @@ -553,7 +569,7 @@ CODE you need to render your textured triangles with bilinear filtering to benefit from sub-pixel positioning of text. - 2015/07/08 (1.43) - switched rendering data to use indexed rendering. this is saving a fair amount of CPU/GPU and enables us to get anti-aliasing for a marginal cost. this necessary change will break your rendering function! the fix should be very easy. sorry for that :( - - if you are using a vanilla copy of one of the imgui_impl_XXXX.cpp provided in the example, you just need to update your copy and you can ignore the rest. + - if you are using a vanilla copy of one of the imgui_impl_XXX.cpp provided in the example, you just need to update your copy and you can ignore the rest. - the signature of the io.RenderDrawListsFn handler has changed! old: ImGui_XXXX_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) new: ImGui_XXXX_RenderDrawLists(ImDrawData* draw_data). @@ -698,7 +714,7 @@ CODE - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt and see how you want to help and can help! - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. - You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/3075). Visuals are ideal as they inspire other programmers. + You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/3488). Visuals are ideal as they inspire other programmers. But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately). @@ -804,7 +820,7 @@ CODE static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear -// Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by back-end) +// Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by backend) static const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f; // Extend outside and inside windows. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. @@ -847,6 +863,7 @@ static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); static void NavEndFrame(); static bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand); +static void NavApplyItemToResult(ImGuiNavMoveResult* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel); static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); @@ -856,7 +873,6 @@ static int FindWindowFocusIndex(ImGuiWindow* window); // Error Checking static void ErrorCheckNewFrameSanityChecks(); static void ErrorCheckEndFrameSanityChecks(); -static void ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool write); // Misc static void UpdateSettings(); @@ -940,7 +956,7 @@ ImGuiStyle::ImGuiStyle() LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. - TabMinWidthForUnselectedCloseButton = 0.0f; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -948,7 +964,7 @@ ImGuiStyle::ImGuiStyle() DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. - AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering. + AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. CircleSegmentMaxError = 1.60f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. @@ -979,8 +995,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) GrabRounding = ImFloor(GrabRounding * scale_factor); LogSliderDeadzone = ImFloor(LogSliderDeadzone * scale_factor); TabRounding = ImFloor(TabRounding * scale_factor); - if (TabMinWidthForUnselectedCloseButton != FLT_MAX) - TabMinWidthForUnselectedCloseButton = ImFloor(TabMinWidthForUnselectedCloseButton * scale_factor); + TabMinWidthForCloseButton = (TabMinWidthForCloseButton != FLT_MAX) ? ImFloor(TabMinWidthForCloseButton * scale_factor) : FLT_MAX; DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); @@ -1024,7 +1039,7 @@ ImGuiIO::ImGuiIO() ConfigInputTextCursorBlink = true; ConfigWindowsResizeFromEdges = true; ConfigWindowsMoveFromTitleBarOnly = false; - ConfigWindowsMemoryCompactTimer = 60.0f; + ConfigMemoryCompactTimer = 60.0f; // Platform Functions BackendPlatformName = BackendRendererName = NULL; @@ -1035,10 +1050,6 @@ ImGuiIO::ImGuiIO() ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl; ImeWindowHandle = NULL; -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - RenderDrawListsFn = NULL; -#endif - // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX); @@ -1544,66 +1555,58 @@ void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_f //----------------------------------------------------------------------------- // Convert UTF-8 to 32-bit character, process single character input. -// Based on stb_from_utf8() from github.com/nothings/stb/ +// A nearly-branchless UTF-8 decoder, based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8). // We handle UTF-8 decoding error by skipping forward. int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end) { - unsigned int c = (unsigned int)-1; - const unsigned char* str = (const unsigned char*)in_text; - if (!(*str & 0x80)) - { - c = (unsigned int)(*str++); - *out_char = c; - return 1; - } - if ((*str & 0xe0) == 0xc0) - { - *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string - if (in_text_end && in_text_end - (const char*)str < 2) return 1; - if (*str < 0xc2) return 2; - c = (unsigned int)((*str++ & 0x1f) << 6); - if ((*str & 0xc0) != 0x80) return 2; - c += (*str++ & 0x3f); - *out_char = c; - return 2; - } - if ((*str & 0xf0) == 0xe0) - { - *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string - if (in_text_end && in_text_end - (const char*)str < 3) return 1; - if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return 3; - if (*str == 0xed && str[1] > 0x9f) return 3; // str[1] < 0x80 is checked below - c = (unsigned int)((*str++ & 0x0f) << 12); - if ((*str & 0xc0) != 0x80) return 3; - c += (unsigned int)((*str++ & 0x3f) << 6); - if ((*str & 0xc0) != 0x80) return 3; - c += (*str++ & 0x3f); - *out_char = c; - return 3; - } - if ((*str & 0xf8) == 0xf0) - { - *out_char = IM_UNICODE_CODEPOINT_INVALID; // will be invalid but not end of string - if (in_text_end && in_text_end - (const char*)str < 4) return 1; - if (*str > 0xf4) return 4; - if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return 4; - if (*str == 0xf4 && str[1] > 0x8f) return 4; // str[1] < 0x80 is checked below - c = (unsigned int)((*str++ & 0x07) << 18); - if ((*str & 0xc0) != 0x80) return 4; - c += (unsigned int)((*str++ & 0x3f) << 12); - if ((*str & 0xc0) != 0x80) return 4; - c += (unsigned int)((*str++ & 0x3f) << 6); - if ((*str & 0xc0) != 0x80) return 4; - c += (*str++ & 0x3f); - // utf-8 encodings of values used in surrogate pairs are invalid - if ((c & 0xFFFFF800) == 0xD800) return 4; - // If codepoint does not fit in ImWchar, use replacement character U+FFFD instead - if (c > IM_UNICODE_CODEPOINT_MAX) c = IM_UNICODE_CODEPOINT_INVALID; - *out_char = c; - return 4; - } - *out_char = 0; - return 0; + static const char lengths[32] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 }; + static const int masks[] = { 0x00, 0x7f, 0x1f, 0x0f, 0x07 }; + static const uint32_t mins[] = { 0x400000, 0, 0x80, 0x800, 0x10000 }; + static const int shiftc[] = { 0, 18, 12, 6, 0 }; + static const int shifte[] = { 0, 6, 4, 2, 0 }; + int len = lengths[*(const unsigned char*)in_text >> 3]; + int wanted = len + !len; + + if (in_text_end == NULL) + in_text_end = in_text + wanted; // Max length, nulls will be taken into account. + + // Copy at most 'len' bytes, stop copying at 0 or past in_text_end. Branch predictor does a good job here, + // so it is fast even with excessive branching. + unsigned char s[4]; + s[0] = in_text + 0 < in_text_end ? in_text[0] : 0; + s[1] = in_text + 1 < in_text_end ? in_text[1] : 0; + s[2] = in_text + 2 < in_text_end ? in_text[2] : 0; + s[3] = in_text + 3 < in_text_end ? in_text[3] : 0; + + // Assume a four-byte character and load four bytes. Unused bits are shifted out. + *out_char = (uint32_t)(s[0] & masks[len]) << 18; + *out_char |= (uint32_t)(s[1] & 0x3f) << 12; + *out_char |= (uint32_t)(s[2] & 0x3f) << 6; + *out_char |= (uint32_t)(s[3] & 0x3f) << 0; + *out_char >>= shiftc[len]; + + // Accumulate the various error conditions. + int e = 0; + e = (*out_char < mins[len]) << 6; // non-canonical encoding + e |= ((*out_char >> 11) == 0x1b) << 7; // surrogate half? + e |= (*out_char > IM_UNICODE_CODEPOINT_MAX) << 8; // out of range? + e |= (s[1] & 0xc0) >> 2; + e |= (s[2] & 0xc0) >> 4; + e |= (s[3] ) >> 6; + e ^= 0x2a; // top two bits of each tail byte correct? + e >>= shifte[len]; + + if (e) + { + // No bytes are consumed when *in_text == 0 || in_text == in_text_end. + // One byte is consumed in case of invalid first byte of in_text. + // All available bytes (at most `len` bytes) are consumed on incomplete/invalid second to last bytes. + // Invalid or incomplete input may consume less bytes than wanted, therefore every byte has to be inspected in s. + wanted = ImMin(wanted, !!s[0] + !!s[1] + !!s[2] + !!s[3]); + *out_char = IM_UNICODE_CODEPOINT_INVALID; + } + + return wanted; } int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_text_remaining) @@ -2185,39 +2188,45 @@ static void SetCursorPosYAndSetupForPrevLine(float pos_y, float line_height) window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, pos_y); window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height; // Setting those fields so that SetScrollHereY() can properly function after the end of our clipper usage. window->DC.PrevLineSize.y = (line_height - g.Style.ItemSpacing.y); // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list. - if (ImGuiColumns* columns = window->DC.CurrentColumns) + if (ImGuiOldColumns* columns = window->DC.CurrentColumns) columns->LineMinY = window->DC.CursorPos.y; // Setting this so that cell Y position are set properly } -// Use case A: Begin() called from constructor with items_height<0, then called again from Sync() in StepNo 1 +ImGuiListClipper::ImGuiListClipper() +{ + memset(this, 0, sizeof(*this)); + ItemsCount = -1; +} + +ImGuiListClipper::~ImGuiListClipper() +{ + IM_ASSERT(ItemsCount == -1 && "Forgot to call End(), or to Step() until false?"); +} + +// Use case A: Begin() called from constructor with items_height<0, then called again from Step() in StepNo 1 // Use case B: Begin() called from constructor with items_height>0 // FIXME-LEGACY: Ideally we should remove the Begin/End functions but they are part of the legacy API we still support. This is why some of the code in Step() calling Begin() and reassign some fields, spaghetti style. -void ImGuiListClipper::Begin(int count, float items_height) +void ImGuiListClipper::Begin(int items_count, float items_height) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; StartPosY = window->DC.CursorPos.y; ItemsHeight = items_height; - ItemsCount = count; + ItemsCount = items_count; StepNo = 0; - DisplayEnd = DisplayStart = -1; - if (ItemsHeight > 0.0f) - { - ImGui::CalcListClipping(ItemsCount, ItemsHeight, &DisplayStart, &DisplayEnd); // calculate how many to clip/display - if (DisplayStart > 0) - SetCursorPosYAndSetupForPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); // advance cursor - StepNo = 2; - } + DisplayStart = -1; + DisplayEnd = 0; } void ImGuiListClipper::End() { - if (ItemsCount < 0) + if (ItemsCount < 0) // Already ended return; + // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. - if (ItemsCount < INT_MAX) - SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor + if (ItemsCount < INT_MAX && DisplayStart >= 0) + SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); ItemsCount = -1; StepNo = 3; } @@ -2227,38 +2236,70 @@ bool ImGuiListClipper::Step() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (ItemsCount == 0 || window->SkipItems) + // Reached end of list + if (DisplayEnd >= ItemsCount || window->SkipItems) { - ItemsCount = -1; + End(); return false; } - if (StepNo == 0) // Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height. + + // Step 0: Let you process the first element (regardless of it being visible or not, so we can measure the element height) + if (StepNo == 0) { - DisplayStart = 0; - DisplayEnd = 1; StartPosY = window->DC.CursorPos.y; - StepNo = 1; - return true; + if (ItemsHeight <= 0.0f) + { + // Submit the first item so we can measure its height (generally it is 0..1) + DisplayStart = 0; + DisplayEnd = 1; + StepNo = 1; + return true; + } + + // Already has item height (given by user in Begin): skip to calculating step + DisplayStart = DisplayEnd; + StepNo = 2; } - if (StepNo == 1) // Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. + + // Step 1: the clipper infer height from first element + if (StepNo == 1) { - if (ItemsCount == 1) { ItemsCount = -1; return false; } - float items_height = window->DC.CursorPos.y - StartPosY; - IM_ASSERT(items_height > 0.0f); // If this triggers, it means Item 0 hasn't moved the cursor vertically - Begin(ItemsCount - 1, items_height); - DisplayStart++; - DisplayEnd++; - StepNo = 3; - return true; + IM_ASSERT(ItemsHeight <= 0.0f); + ItemsHeight = window->DC.CursorPos.y - StartPosY; + IM_ASSERT(ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); + StepNo = 2; } - if (StepNo == 2) // Step 2: empty step only required if an explicit items_height was passed to constructor or Begin() and user still call Step(). Does nothing and switch to Step 3. + + // Step 2: calculate the actual range of elements to display, and position the cursor before the first element + if (StepNo == 2) { - IM_ASSERT(DisplayStart >= 0 && DisplayEnd >= 0); + IM_ASSERT(ItemsHeight > 0.0f); + + int already_submitted = DisplayEnd; + ImGui::CalcListClipping(ItemsCount - already_submitted, ItemsHeight, &DisplayStart, &DisplayEnd); + DisplayStart += already_submitted; + DisplayEnd += already_submitted; + + // Seek cursor + if (DisplayStart > already_submitted) + SetCursorPosYAndSetupForPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); + StepNo = 3; return true; } - if (StepNo == 3) // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. - End(); + + // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), + // Advance the cursor to the end of the list and then returns 'false' to end the loop. + if (StepNo == 3) + { + // Seek cursor + if (ItemsCount < INT_MAX) + SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor + ItemsCount = -1; + return false; + } + + IM_ASSERT(0); return false; } @@ -2311,7 +2352,7 @@ void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) ImGuiColorMod backup; backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; - g.ColorModifiers.push_back(backup); + g.ColorStack.push_back(backup); g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); } @@ -2321,7 +2362,7 @@ void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) ImGuiColorMod backup; backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; - g.ColorModifiers.push_back(backup); + g.ColorStack.push_back(backup); g.Style.Colors[idx] = col; } @@ -2330,9 +2371,9 @@ void ImGui::PopStyleColor(int count) ImGuiContext& g = *GImGui; while (count > 0) { - ImGuiColorMod& backup = g.ColorModifiers.back(); + ImGuiColorMod& backup = g.ColorStack.back(); g.Style.Colors[backup.Col] = backup.BackupValue; - g.ColorModifiers.pop_back(); + g.ColorStack.pop_back(); count--; } } @@ -2386,7 +2427,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) { ImGuiContext& g = *GImGui; float* pvar = (float*)var_info->GetVarPtr(&g.Style); - g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } @@ -2400,7 +2441,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) { ImGuiContext& g = *GImGui; ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); - g.StyleModifiers.push_back(ImGuiStyleMod(idx, *pvar)); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } @@ -2413,12 +2454,12 @@ void ImGui::PopStyleVar(int count) while (count > 0) { // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. - ImGuiStyleMod& backup = g.StyleModifiers.back(); + ImGuiStyleMod& backup = g.StyleVarStack.back(); const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); void* data = info->GetVarPtr(&g.Style); if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } - g.StyleModifiers.pop_back(); + g.StyleVarStack.pop_back(); count--; } } @@ -2733,70 +2774,27 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl //----------------------------------------------------------------------------- // ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods -ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) - : DrawListInst(&context->DrawListSharedData) +ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst(NULL) { + memset(this, 0, sizeof(*this)); Name = ImStrdup(name); + NameBufLen = (int)strlen(name) + 1; ID = ImHashStr(name); IDStack.push_back(ID); - Flags = ImGuiWindowFlags_None; - Pos = ImVec2(0.0f, 0.0f); - Size = SizeFull = ImVec2(0.0f, 0.0f); - ContentSize = ContentSizeExplicit = ImVec2(0.0f, 0.0f); - WindowPadding = ImVec2(0.0f, 0.0f); - WindowRounding = 0.0f; - WindowBorderSize = 0.0f; - NameBufLen = (int)strlen(name) + 1; MoveId = GetID("#MOVE"); - ChildId = 0; - Scroll = ImVec2(0.0f, 0.0f); ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); - ScrollbarSizes = ImVec2(0.0f, 0.0f); - ScrollbarX = ScrollbarY = false; - Active = WasActive = false; - WriteAccessed = false; - Collapsed = false; - WantCollapseToggle = false; - SkipItems = false; - Appearing = false; - Hidden = false; - IsFallbackWindow = false; - HasCloseButton = false; - ResizeBorderHeld = -1; - BeginCount = 0; - BeginOrderWithinParent = -1; - BeginOrderWithinContext = -1; - PopupId = 0; AutoFitFramesX = AutoFitFramesY = -1; - AutoFitChildAxises = 0x00; - AutoFitOnlyGrows = false; AutoPosLastDirection = ImGuiDir_None; - HiddenFramesCanSkipItems = HiddenFramesCannotSkipItems = 0; SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); - - InnerRect = ImRect(0.0f, 0.0f, 0.0f, 0.0f); // Clear so the InnerRect.GetSize() code in Begin() doesn't lead to overflow even if the result isn't used. - LastFrameActive = -1; LastTimeActive = -1.0f; - ItemWidthDefault = 0.0f; FontWindowScale = 1.0f; SettingsOffset = -1; - DrawList = &DrawListInst; + DrawList->_Data = &context->DrawListSharedData; DrawList->_OwnerName = Name; - ParentWindow = NULL; - RootWindow = NULL; - RootWindowForTitleBarHighlight = NULL; - RootWindowForNav = NULL; - - NavLastIds[0] = NavLastIds[1] = 0; - NavRectRel[0] = NavRectRel[1] = ImRect(); - NavLastChildNavWindow = NULL; - - MemoryCompacted = false; - MemoryDrawListIdxCapacity = MemoryDrawListVtxCapacity = 0; } ImGuiWindow::~ImGuiWindow() @@ -2804,7 +2802,7 @@ ImGuiWindow::~ImGuiWindow() IM_ASSERT(DrawList == &DrawListInst); IM_DELETE(Name); for (int i = 0; i != ColumnsStorage.Size; i++) - ColumnsStorage[i].~ImGuiColumns(); + ColumnsStorage[i].~ImGuiOldColumns(); } ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) @@ -2894,11 +2892,16 @@ static void SetCurrentWindow(ImGuiWindow* window) g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } +void ImGui::GcCompactTransientMiscBuffers() +{ + ImGuiContext& g = *GImGui; + g.ItemFlagsStack.clear(); + g.GroupStack.clear(); +} + // Free up/compact internal window buffers, we can use this when a window becomes unused. -// This is currently unused by the library, but you may call this yourself for easy GC. // Not freed: -// - ImGuiWindow, ImGuiWindowSettings, Name -// - StateStorage, ColumnsStorage (may hold useful data) +// - ImGuiWindow, ImGuiWindowSettings, Name, StateStorage, ColumnsStorage (may hold useful data) // This should have no noticeable visual effect. When the window reappear however, expect new allocation/buffer growth/copy cost. void ImGui::GcCompactTransientWindowBuffers(ImGuiWindow* window) { @@ -2908,10 +2911,8 @@ void ImGui::GcCompactTransientWindowBuffers(ImGuiWindow* window) window->IDStack.clear(); window->DrawList->_ClearFreeMemory(); window->DC.ChildWindows.clear(); - window->DC.ItemFlagsStack.clear(); window->DC.ItemWidthStack.clear(); window->DC.TextWrapPosStack.clear(); - window->DC.GroupStack.clear(); } void ImGui::GcAwakeTransientWindowBuffers(ImGuiWindow* window) @@ -3269,13 +3270,31 @@ void ImGui::DestroyContext(ImGuiContext* ctx) IM_DELETE(ctx); } +// No specific ordering/dependency support, will see as needed +void ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) +{ + ImGuiContext& g = *ctx; + IM_ASSERT(hook->Callback != NULL); + g.Hooks.push_back(*hook); +} + +// Call context hooks (used by e.g. test engine) +// We assume a small number of hooks so all stored in same array +void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type) +{ + ImGuiContext& g = *ctx; + for (int n = 0; n < g.Hooks.Size; n++) + if (g.Hooks[n].Type == hook_type) + g.Hooks[n].Callback(&g, &g.Hooks[n]); +} + ImGuiIO& ImGui::GetIO() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); return GImGui->IO; } -// Same value as passed to the old io.RenderDrawListsFn function. Valid after Render() and until the next call to NewFrame() +// Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() ImDrawData* ImGui::GetDrawData() { ImGuiContext& g = *GImGui; @@ -3391,7 +3410,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() if (root_window != NULL && !is_closed_popup) { - StartMouseMovingWindow(g.HoveredWindow); + StartMouseMovingWindow(g.HoveredWindow); //-V595 // Cancel moving if clicked outside of title bar if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(root_window->Flags & ImGuiWindowFlags_NoTitleBar)) @@ -3417,17 +3436,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() // Find the top-most window between HoveredWindow and the top-most Modal Window. // This is where we can trim the popup stack. ImGuiWindow* modal = GetTopMostPopupModal(); - bool hovered_window_above_modal = false; - if (modal == NULL) - hovered_window_above_modal = true; - for (int i = g.Windows.Size - 1; i >= 0 && hovered_window_above_modal == false; i--) - { - ImGuiWindow* window = g.Windows[i]; - if (window == modal) - break; - if (window == g.HoveredWindow) - hovered_window_above_modal = true; - } + bool hovered_window_above_modal = g.HoveredWindow && IsWindowAbove(g.HoveredWindow, modal); ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true); } } @@ -3694,9 +3703,7 @@ void ImGui::NewFrame() IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); ImGuiContext& g = *GImGui; -#ifdef IMGUI_ENABLE_TEST_ENGINE - ImGuiTestEngineHook_PreNewFrame(&g); -#endif + CallContextHooks(&g, ImGuiContextHookType_NewFramePre); // Check and assert for various common IO and Configuration mistakes ErrorCheckNewFrameSanityChecks(); @@ -3831,7 +3838,7 @@ void ImGui::NewFrame() // Mark all windows as not visible and compact unused memory. IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size); - const float memory_compact_start_time = (g.IO.ConfigWindowsMemoryCompactTimer >= 0.0f) ? (float)g.Time - g.IO.ConfigWindowsMemoryCompactTimer : FLT_MAX; + const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; for (int i = 0; i != g.Windows.Size; i++) { ImGuiWindow* window = g.Windows[i]; @@ -3844,6 +3851,9 @@ void ImGui::NewFrame() if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) GcCompactTransientWindowBuffers(window); } + if (g.GcCompactAll) + GcCompactTransientMiscBuffers(); + g.GcCompactAll = false; // Closing the focused window restore focus to the first active root window in descending z-order if (g.NavWindow && !g.NavWindow->WasActive) @@ -3853,6 +3863,9 @@ void ImGui::NewFrame() // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. g.CurrentWindowStack.resize(0); g.BeginPopupStack.resize(0); + g.ItemFlagsStack.resize(0); + g.ItemFlagsStack.push_back(ImGuiItemFlags_Default_); + g.GroupStack.resize(0); ClosePopupsOverWindow(g.NavWindow, false); // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. @@ -3866,9 +3879,7 @@ void ImGui::NewFrame() Begin("Debug##Default"); IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); -#ifdef IMGUI_ENABLE_TEST_ENGINE - ImGuiTestEngineHook_PostNewFrame(&g); -#endif + CallContextHooks(&g, ImGuiContextHookType_NewFramePost); } // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. @@ -3953,15 +3964,12 @@ void ImGui::Shutdown(ImGuiContext* context) if (g.SettingsLoaded && g.IO.IniFilename != NULL) { ImGuiContext* backup_context = GImGui; - SetCurrentContext(context); + SetCurrentContext(&g); SaveIniSettingsToDisk(g.IO.IniFilename); SetCurrentContext(backup_context); } - // Notify hooked test engine, if any -#ifdef IMGUI_ENABLE_TEST_ENGINE - ImGuiTestEngineHook_Shutdown(context); -#endif + CallContextHooks(&g, ImGuiContextHookType_Shutdown); // Clear everything else for (int i = 0; i < g.Windows.Size; i++) @@ -3976,8 +3984,8 @@ void ImGui::Shutdown(ImGuiContext* context) g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; g.MovingWindow = NULL; - g.ColorModifiers.clear(); - g.StyleModifiers.clear(); + g.ColorStack.clear(); + g.StyleVarStack.clear(); g.FontStack.clear(); g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); @@ -4041,7 +4049,7 @@ static void AddWindowToSortBuffer(ImVector* out_sorted_windows, Im static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) { // Remove trailing command if unused. - // Technically we could return directly instead of popping, but this make things looks neat in Metrics window as well. + // Technically we could return directly instead of popping, but this make things looks neat in Metrics/Debugger window as well. draw_list->_PopUnusedDrawCmd(); if (draw_list->CmdBuffer.Size == 0) return; @@ -4056,13 +4064,13 @@ static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* d // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) // If this assert triggers because you are drawing lots of stuff manually: // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds. - // Be mindful that the ImDrawList API doesn't filter vertices. Use the Metrics window to inspect draw list contents. + // Be mindful that the ImDrawList API doesn't filter vertices. Use the Metrics/Debugger window to inspect draw list contents. // - If you want large meshes with more than 64K vertices, you can either: - // (A) Handle the ImDrawCmd::VtxOffset value in your renderer back-end, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. - // Most example back-ends already support this from 1.71. Pre-1.71 back-ends won't. + // (A) Handle the ImDrawCmd::VtxOffset value in your renderer backend, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. + // Most example backends already support this from 1.71. Pre-1.71 backends won't. // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. - // (B) Or handle 32-bit indices in your renderer back-end, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. - // Most example back-ends already support this. For example, the OpenGL example code detect index size at compile-time: + // (B) Or handle 32-bit indices in your renderer backend, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. + // Most example backends already support this. For example, the OpenGL example code detect index size at compile-time: // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); // Your own engine or render API may use different parameters or function calls to specify index sizes. // 2 and 4 bytes indices are generally supported by most graphics API. @@ -4161,6 +4169,8 @@ void ImGui::EndFrame() return; IM_ASSERT(g.WithinFrameScope && "Forgot to call ImGui::NewFrame()?"); + CallContextHooks(&g, ImGuiContextHookType_EndFramePre); + ErrorCheckEndFrameSanityChecks(); // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) @@ -4227,6 +4237,8 @@ void ImGui::EndFrame() g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.InputQueueCharacters.resize(0); memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); + + CallContextHooks(&g, ImGuiContextHookType_EndFramePost); } void ImGui::Render() @@ -4240,6 +4252,8 @@ void ImGui::Render() g.IO.MetricsRenderWindows = 0; g.DrawDataBuilder.Clear(); + CallContextHooks(&g, ImGuiContextHookType_RenderPre); + // Add background ImDrawList if (!g.BackgroundDrawList.VtxBuffer.empty()) AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList); @@ -4272,11 +4286,7 @@ void ImGui::Render() g.IO.MetricsRenderVertices = g.DrawData.TotalVtxCount; g.IO.MetricsRenderIndices = g.DrawData.TotalIdxCount; - // (Legacy) Call the Render callback function. The current prefer way is to let the user retrieve GetDrawData() and call the render function themselves. -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - if (g.DrawData.CmdListsCount > 0 && g.IO.RenderDrawListsFn != NULL) - g.IO.RenderDrawListsFn(&g.DrawData); -#endif + CallContextHooks(&g, ImGuiContextHookType_RenderPost); } // Calculate text size. Text can be multi-line. Optionally ignore text after a ## marker. @@ -4385,7 +4395,7 @@ int ImGui::GetKeyIndex(ImGuiKey imgui_key) } // Note that dear imgui doesn't know the semantic of each entry of io.KeysDown[]! -// Use your own indices/enums according to how your back-end/engine stored them into io.KeysDown[]! +// Use your own indices/enums according to how your backend/engine stored them into io.KeysDown[]! bool ImGui::IsKeyDown(int user_key_index) { if (user_key_index < 0) @@ -4541,7 +4551,7 @@ bool ImGui::IsAnyMouseDown() // Return the delta from the initial clicking position while the mouse button is clicked or was just released. // This is locked and return 0.0f until the mouse moves past a distance threshold at least once. -// NB: This is only valid if IsMousePosValid(). Back-ends in theory should always keep mouse position valid when dragging even outside the client window. +// NB: This is only valid if IsMousePosValid(). backends in theory should always keep mouse position valid when dragging even outside the client window. ImVec2 ImGui::GetMouseDragDelta(ImGuiMouseButton button, float lock_threshold) { ImGuiContext& g = *GImGui; @@ -4621,12 +4631,13 @@ bool ImGui::IsItemDeactivatedAfterEdit() return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEditedBefore || (g.ActiveId == 0 && g.ActiveIdHasBeenEditedBefore)); } +// == GetItemID() == GetFocusID() bool ImGui::IsItemFocused() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (g.NavId == 0 || g.NavDisableHighlight || g.NavId != window->DC.LastItemId) + if (g.NavId != window->DC.LastItemId || g.NavId == 0) return false; return true; } @@ -5100,7 +5111,6 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s // Resize grips and borders are on layer 1 window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); // Manual resize grips PushID("#RESIZE"); @@ -5169,7 +5179,6 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s // Restore nav layer window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); // Navigation resize (keyboard/gamepad) if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) @@ -5340,7 +5349,6 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; window->DC.ItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); // Layout buttons // FIXME: Would be nice to generalize the subtleties expressed here into reusable code. @@ -5376,7 +5384,6 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl *p_open = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.ItemFlags = item_flags_backup; // Title bar text (with: horizontal alignment, avoiding collapse/close button, optional "unsaved document" marker) @@ -5497,8 +5504,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Add to stack // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() g.CurrentWindowStack.push_back(window); + g.CurrentWindow = window; + window->DC.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindow = NULL; - ErrorCheckBeginEndCompareStacksSize(window, true); + if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; @@ -5952,10 +5961,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; window->DC.NavLayerActiveMaskNext = 0x00; - window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : 0; // -V595 window->DC.NavHideHighlightOneFrame = false; window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); @@ -5972,13 +5979,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.ItemWidth = window->ItemWidthDefault; window->DC.TextWrapPos = -1.0f; // disabled - window->DC.ItemFlagsStack.resize(0); window->DC.ItemWidthStack.resize(0); window->DC.TextWrapPosStack.resize(0); - window->DC.GroupStack.resize(0); - window->DC.ItemFlags = parent_window ? parent_window->DC.ItemFlags : ImGuiItemFlags_Default_; - if (parent_window) - window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); if (window->AutoFitFramesX > 0) window->AutoFitFramesX--; @@ -6023,12 +6025,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) SetCurrentWindow(window); } + // Pull/inherit current state + window->DC.ItemFlags = g.ItemFlagsStack.back(); // Inherit from shared stack + window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : 0; // Inherit from parent only // -V595 + PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); // Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default "Debug" window is unused) - if (first_begin_of_the_frame) - window->WriteAccessed = false; - + window->WriteAccessed = false; window->BeginCount++; g.NextWindowData.ClearFlags(); @@ -6099,7 +6103,7 @@ void ImGui::End() g.CurrentWindowStack.pop_back(); if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - ErrorCheckBeginEndCompareStacksSize(window, false); + window->DC.StackSizesOnBegin.CompareWithCurrentState(); SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back()); } @@ -6251,19 +6255,25 @@ void ImGui::PopFont() void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiItemFlags item_flags = window->DC.ItemFlags; + IM_ASSERT(item_flags == g.ItemFlagsStack.back()); if (enabled) - window->DC.ItemFlags |= option; + item_flags |= option; else - window->DC.ItemFlags &= ~option; - window->DC.ItemFlagsStack.push_back(window->DC.ItemFlags); + item_flags &= ~option; + window->DC.ItemFlags = item_flags; + g.ItemFlagsStack.push_back(item_flags); } void ImGui::PopItemFlag() { - ImGuiWindow* window = GetCurrentWindow(); - window->DC.ItemFlagsStack.pop_back(); - window->DC.ItemFlags = window->DC.ItemFlagsStack.empty() ? ImGuiItemFlags_Default_ : window->DC.ItemFlagsStack.back(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(g.ItemFlagsStack.Size > 1); // Too many calls to PopItemFlag() - we always leave a 0 at the bottom of the stack. + g.ItemFlagsStack.pop_back(); + window->DC.ItemFlags = g.ItemFlagsStack.back(); } // FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. @@ -6314,6 +6324,20 @@ bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) return false; } +bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below) +{ + ImGuiContext& g = *GImGui; + for (int i = g.Windows.Size - 1; i >= 0; i--) + { + ImGuiWindow* candidate_window = g.Windows[i]; + if (candidate_window == potential_above) + return true; + if (candidate_window == potential_below) + return false; + } + return false; +} + bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function @@ -6644,12 +6668,11 @@ void ImGui::ActivateItem(ImGuiID id) g.NavNextActivateId = id; } -// Note: this is storing in same stack as IDStack, so Push/Pop mismatch will be reported there. void ImGui::PushFocusScope(ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - window->IDStack.push_back(window->DC.NavFocusScopeIdCurrent); + g.FocusScopeStack.push_back(window->DC.NavFocusScopeIdCurrent); window->DC.NavFocusScopeIdCurrent = id; } @@ -6657,8 +6680,9 @@ void ImGui::PopFocusScope() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - window->DC.NavFocusScopeIdCurrent = window->IDStack.back(); - window->IDStack.pop_back(); + IM_ASSERT(g.FocusScopeStack.Size > 0); // Too many PopFocusScope() ? + window->DC.NavFocusScopeIdCurrent = g.FocusScopeStack.back(); + g.FocusScopeStack.pop_back(); } void ImGui::SetKeyboardFocusHere(int offset) @@ -6740,9 +6764,24 @@ void ImGui::PushOverrideID(ImGuiID id) window->IDStack.push_back(id); } +// Helper to avoid a common series of PushOverrideID -> GetID() -> PopID() call +// (note that when using this pattern, TestEngine's "Stack Tool" will tend to not display the intermediate stack level. +// for that to work we would need to do PushOverrideID() -> ItemAdd() -> PopID() which would alter widget code a little more) +ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) +{ + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); + ImGui::KeepAliveID(id); +#ifdef IMGUI_ENABLE_TEST_ENGINE + ImGuiContext& g = *GImGui; + IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); +#endif + return id; +} + void ImGui::PopID() { ImGuiWindow* window = GImGui->CurrentWindow; + IM_ASSERT(window->IDStack.Size > 1); // Too many PopID(), or could be popping in a wrong/different window? window->IDStack.pop_back(); } @@ -6807,8 +6846,8 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() // (IF YOU GET A WARNING OR COMPILE ERROR HERE: it means you assert macro is incorrectly defined! // If your macro uses multiple statements, it NEEDS to be surrounded by a 'do { ... } while (0)' block. // This is a common C/C++ idiom to allow multiple statements macros to be used in control flow blocks.) - // #define IM_ASSERT(EXPR) SomeCode(EXPR); SomeMoreCode(); // Wrong! - // #define IM_ASSERT(EXPR) do { SomeCode(EXPR); SomeMoreCode(); } while (0) // Correct! + // #define IM_ASSERT(EXPR) if (SomeCode(EXPR)) SomeMoreCode(); // Wrong! + // #define IM_ASSERT(EXPR) do { if (SomeCode(EXPR)) SomeMoreCode(); } while (0) // Correct! if (true) IM_ASSERT(1); else IM_ASSERT(0); // Check user data @@ -6817,21 +6856,21 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); - IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); - IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8() ?"); + IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); + IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.CircleSegmentMaxError > 0.0f && "Invalid style setting!"); - IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting. Alpha cannot be negative (allows us to avoid a few clamps in color computations)!"); + IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting!"); // Allows us to avoid a few clamps in color computations IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); for (int n = 0; n < ImGuiKey_COUNT; n++) IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && "io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)"); - // Perform simple check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only recently added in 1.60 WIP) + // Check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only added in 1.60 WIP) if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); - // Perform simple check: the beta io.ConfigWindowsResizeFromEdges option requires back-end to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. + // Check: the io.ConfigWindowsResizeFromEdges option requires backend to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) g.IO.ConfigWindowsResizeFromEdges = false; } @@ -6841,10 +6880,17 @@ static void ImGui::ErrorCheckEndFrameSanityChecks() ImGuiContext& g = *GImGui; // Verify that io.KeyXXX fields haven't been tampered with. Key mods should not be modified between NewFrame() and EndFrame() - // One possible reason leading to this assert is that your back-ends update inputs _AFTER_ NewFrame(). - const ImGuiKeyModFlags expected_key_mod_flags = GetMergedKeyModFlags(); - IM_ASSERT(g.IO.KeyMods == expected_key_mod_flags && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); - IM_UNUSED(expected_key_mod_flags); + // One possible reason leading to this assert is that your backends update inputs _AFTER_ NewFrame(). + // It is known that when some modal native windows called mid-frame takes focus away, some backends such as GLFW will + // send key release events mid-frame. This would normally trigger this assertion and lead to sheared inputs. + // We silently accommodate for this case by ignoring/ the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0), + // while still correctly asserting on mid-frame key press events. + const ImGuiKeyModFlags key_mod_flags = GetMergedKeyModFlags(); + IM_ASSERT((key_mod_flags == 0 || g.IO.KeyMods == key_mod_flags) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); + IM_UNUSED(key_mod_flags); + + // Recover from errors + //ErrorCheckEndFrameRecover(); // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). @@ -6861,28 +6907,114 @@ static void ImGui::ErrorCheckEndFrameSanityChecks() IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); } } + + IM_ASSERT_USER_ERROR(g.GroupStack.Size == 0, "Missing EndGroup call!"); +} + +// Experimental recovery from incorrect usage of BeginXXX/EndXXX/PushXXX/PopXXX calls. +// Must be called during or before EndFrame(). +// This is generally flawed as we are not necessarily End/Popping things in the right order. +// FIXME: Can't recover from inside BeginTabItem/EndTabItem yet. +// FIXME: Can't recover from interleaved BeginTabBar/Begin +void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data) +{ + // PVS-Studio V1044 is "Loop break conditions do not depend on the number of iterations" + ImGuiContext& g = *GImGui; + while (g.CurrentWindowStack.Size > 0) + { +#ifdef IMGUI_HAS_TABLE + while (g.CurrentTable && (g.CurrentTable->OuterWindow == g.CurrentWindow || g.CurrentTable->InnerWindow == g.CurrentWindow)) + { + if (log_callback) log_callback(user_data, "Recovered from missing EndTable() in '%s'", g.CurrentTable->OuterWindow->Name); + EndTable(); + } +#endif + ImGuiWindow* window = g.CurrentWindow; + while (g.CurrentTabBar != NULL) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); + EndTabBar(); + } + while (g.CurrentWindow->DC.TreeDepth > 0) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); + TreePop(); + } + while (g.GroupStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfGroupStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing EndGroup() in '%s'", window->Name); + EndGroup(); + } + while (g.CurrentWindow->IDStack.Size > 1) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopID() in '%s'", window->Name); + PopID(); + } + while (g.ColorStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfColorStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopStyleColor() in '%s' for ImGuiCol_%s", window->Name, GetStyleColorName(g.ColorStack.back().Col)); + PopStyleColor(); + } + while (g.StyleVarStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfStyleVarStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); + PopStyleVar(); + } + while (g.FocusScopeStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfFocusScopeStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); + PopFocusScope(); + } + if (g.CurrentWindowStack.Size == 1) + { + IM_ASSERT(g.CurrentWindow->IsFallbackWindow); + break; + } + if (g.CurrentWindow->Flags & ImGuiWindowFlags_ChildWindow) + { + if (log_callback) log_callback(user_data, "Recovered from missing EndChild() for '%s'", window->Name); + EndChild(); + } + else + { + if (log_callback) log_callback(user_data, "Recovered from missing End() for '%s'", window->Name); + End(); + } + } } -// Save and compare stack sizes on Begin()/End() to detect usage errors -// Begin() calls this with write=true -// End() calls this with write=false -static void ImGui::ErrorCheckBeginEndCompareStacksSize(ImGuiWindow* window, bool write) +// Save current stack sizes for later compare +void ImGuiStackSizes::SetToCurrentState() { ImGuiContext& g = *GImGui; - short* p = &window->DC.StackSizesBackup[0]; + ImGuiWindow* window = g.CurrentWindow; + SizeOfIDStack = (short)window->IDStack.Size; + SizeOfColorStack = (short)g.ColorStack.Size; + SizeOfStyleVarStack = (short)g.StyleVarStack.Size; + SizeOfFontStack = (short)g.FontStack.Size; + SizeOfFocusScopeStack = (short)g.FocusScopeStack.Size; + SizeOfGroupStack = (short)g.GroupStack.Size; + SizeOfBeginPopupStack = (short)g.BeginPopupStack.Size; +} + +// Compare to detect usage errors +void ImGuiStackSizes::CompareWithCurrentState() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; // Window stacks - // NOT checking: DC.ItemWidth, DC.AllowKeyboardFocus, DC.ButtonRepeat, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) - { int n = window->IDStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "PushID/PopID or TreeNode/TreePop Mismatch!"); p++; } // Too few or too many PopID()/TreePop() - { int n = window->DC.GroupStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "BeginGroup/EndGroup Mismatch!"); p++; } // Too few or too many EndGroup() + // NOT checking: DC.ItemWidth, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) + IM_ASSERT(SizeOfIDStack == window->IDStack.Size && "PushID/PopID or TreeNode/TreePop Mismatch!"); // Global stacks // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. - { int n = g.BeginPopupStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p == n && "BeginMenu/EndMenu or BeginPopup/EndPopup Mismatch!"); p++; }// Too few or too many EndMenu()/EndPopup() - { int n = g.ColorModifiers.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushStyleColor/PopStyleColor Mismatch!"); p++; } // Too few or too many PopStyleColor() - { int n = g.StyleModifiers.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushStyleVar/PopStyleVar Mismatch!"); p++; } // Too few or too many PopStyleVar() - { int n = g.FontStack.Size; if (write) *p = (short)n; else IM_ASSERT(*p >= n && "PushFont/PopFont Mismatch!"); p++; } // Too few or too many PopFont() - IM_ASSERT(p == window->DC.StackSizesBackup + IM_ARRAYSIZE(window->DC.StackSizesBackup)); + IM_ASSERT(SizeOfGroupStack == g.GroupStack.Size && "BeginGroup/EndGroup Mismatch!"); + IM_ASSERT(SizeOfBeginPopupStack == g.BeginPopupStack.Size && "BeginPopup/EndPopup or BeginMenu/EndMenu Mismatch!"); + IM_ASSERT(SizeOfColorStack >= g.ColorStack.Size && "PushStyleColor/PopStyleColor Mismatch!"); + IM_ASSERT(SizeOfStyleVarStack >= g.StyleVarStack.Size && "PushStyleVar/PopStyleVar Mismatch!"); + IM_ASSERT(SizeOfFontStack >= g.FontStack.Size && "PushFont/PopFont Mismatch!"); + IM_ASSERT(SizeOfFocusScopeStack == g.FocusScopeStack.Size && "PushFocusScope/PopFocusScope Mismatch!"); } @@ -6979,7 +7111,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. - window->DC.NavLayerActiveMaskNext |= window->DC.NavLayerCurrentMask; + window->DC.NavLayerActiveMaskNext |= (1 << window->DC.NavLayerCurrent); if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) @@ -7285,8 +7417,9 @@ void ImGui::BeginGroup() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - window->DC.GroupStack.resize(window->DC.GroupStack.Size + 1); - ImGuiGroupData& group_data = window->DC.GroupStack.back(); + g.GroupStack.resize(g.GroupStack.Size + 1); + ImGuiGroupData& group_data = g.GroupStack.back(); + group_data.WindowID = window->ID; group_data.BackupCursorPos = window->DC.CursorPos; group_data.BackupCursorMaxPos = window->DC.CursorMaxPos; group_data.BackupIndent = window->DC.Indent; @@ -7309,9 +7442,10 @@ void ImGui::EndGroup() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(window->DC.GroupStack.Size > 0); // Mismatched BeginGroup()/EndGroup() calls + IM_ASSERT(g.GroupStack.Size > 0); // Mismatched BeginGroup()/EndGroup() calls - ImGuiGroupData& group_data = window->DC.GroupStack.back(); + ImGuiGroupData& group_data = g.GroupStack.back(); + IM_ASSERT(group_data.WindowID == window->ID); // EndGroup() in wrong window? ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos)); @@ -7326,7 +7460,7 @@ void ImGui::EndGroup() if (!group_data.EmitItem) { - window->DC.GroupStack.pop_back(); + g.GroupStack.pop_back(); return; } @@ -7355,7 +7489,7 @@ void ImGui::EndGroup() if (group_contains_prev_active_id && g.ActiveId != g.ActiveIdPreviousFrame) window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Deactivated; - window->DC.GroupStack.pop_back(); + g.GroupStack.pop_back(); //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] } @@ -7920,8 +8054,9 @@ void ImGui::EndPopup() g.WithinEndChild = false; } -// Open a popup if mouse button is released over the item -bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) +// Helper to open a popup if mouse button is released over the item +// - This is essentially the same as BeginPopupContextItem() but without the trailing BeginPopup() +void ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); @@ -7930,16 +8065,14 @@ bool ImGui::OpenPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) OpenPopupEx(id, popup_flags); - return true; } - return false; } // This is a helper to handle the simplest case of associating one named popup to one given widget. // - You can pass a NULL str_id to use the identifier of the last item. // - You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). -// - This is essentially the same as calling OpenPopupContextItem() + BeginPopup() but written to avoid -// computing the ID twice because BeginPopupContextXXX functions are called very frequently. +// - This is essentially the same as calling OpenPopupOnItemClick() + BeginPopup() but written to avoid +// computing the ID twice because BeginPopupContextXXX functions may be called very frequently. bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiWindow* window = GImGui->CurrentWindow; @@ -8046,7 +8179,7 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s // For tooltip we prefer avoiding the cursor at all cost even if it means that part of the tooltip won't be visible. if (policy == ImGuiPopupPositionPolicy_Tooltip) - return ref_pos + ImVec2(2, 2); + return ref_pos + ImVec2(2, 2); // Otherwise try to keep within display ImVec2 pos = ref_pos; @@ -8316,6 +8449,14 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) return new_best; } +static void ImGui::NavApplyItemToResult(ImGuiNavMoveResult* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel) +{ + result->Window = window; + result->ID = id; + result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; + result->RectRel = nav_bb_rel; +} + // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, const ImGuiID id) { @@ -8356,25 +8497,14 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con bool new_best = g.NavMoveRequest && NavScoreItem(result, nav_bb); #endif if (new_best) - { - result->Window = window; - result->ID = id; - result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; - result->RectRel = nav_bb_rel; - } + NavApplyItemToResult(result, window, id, nav_bb_rel); // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. const float VISIBLE_RATIO = 0.70f; if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb)) - { - result = &g.NavMoveResultLocalVisibleSet; - result->Window = window; - result->ID = id; - result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; - result->RectRel = nav_bb_rel; - } + NavApplyItemToResult(&g.NavMoveResultLocalVisibleSet, window, id, nav_bb_rel); } // Update window-relative bounding box of navigated item @@ -8507,7 +8637,7 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer]; ImVec2 pos = g.NavWindow->Pos + ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); ImRect visible_rect = GetViewportRect(); - return ImFloor(ImClamp(pos, visible_rect.Min, visible_rect.Max)); // ImFloor() is important because non-integer mouse position application in back-end might be lossy and result in undesirable non-zero delta. + return ImFloor(ImClamp(pos, visible_rect.Min, visible_rect.Max)); // ImFloor() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. } } @@ -9149,7 +9279,7 @@ static void ImGui::NavUpdateWindowing() } // Keyboard: Press and Release ALT to toggle menu layer - // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of back-end clearing releases all keys on ALT-TAB + // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of backend clearing releases all keys on ALT-TAB if (IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) g.NavWindowingToggleLayer = true; if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) @@ -9357,6 +9487,8 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) g.DragDropActive = true; g.DragDropSourceFlags = flags; g.DragDropMouseButton = mouse_button; + if (payload.SourceId == g.ActiveId) + g.ActiveIdNoClearOnFocusLoss = true; } g.DragDropSourceFrameCount = g.FrameCount; g.DragDropWithinSource = true; @@ -9516,7 +9648,7 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop const bool was_accepted_previously = (g.DragDropAcceptIdPrev == g.DragDropTargetId); ImRect r = g.DragDropTargetRect; float r_surface = r.GetWidth() * r.GetHeight(); - if (r_surface < g.DragDropAcceptIdCurrRectSurface) + if (r_surface <= g.DragDropAcceptIdCurrRectSurface) { g.DragDropAcceptFlags = flags; g.DragDropAcceptIdCurr = g.DragDropTargetId; @@ -10258,10 +10390,22 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} #endif //----------------------------------------------------------------------------- -// [SECTION] METRICS/DEBUG WINDOW +// [SECTION] METRICS/DEBUGGER WINDOW +//----------------------------------------------------------------------------- +// - MetricsHelpMarker() [Internal] +// - ShowMetricsWindow() +// - DebugNodeColumns() [Internal] +// - DebugNodeDrawList() [Internal] +// - DebugNodeDrawCmdShowMeshAndBoundingBox() [Internal] +// - DebugNodeStorage() [Internal] +// - DebugNodeTabBar() [Internal] +// - DebugNodeWindow() [Internal] +// - DebugNodeWindowSettings() [Internal] +// - DebugNodeWindowsList() [Internal] //----------------------------------------------------------------------------- #ifndef IMGUI_DISABLE_METRICS_WINDOW + // Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds. static void MetricsHelpMarker(const char* desc) { @@ -10278,44 +10422,36 @@ static void MetricsHelpMarker(const char* desc) void ImGui::ShowMetricsWindow(bool* p_open) { - if (!ImGui::Begin("Dear ImGui Metrics", p_open)) + if (!Begin("Dear ImGui Metrics/Debugger", p_open)) { - ImGui::End(); + End(); return; } + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + + // Basic info + Text("Dear ImGui %s", ImGui::GetVersion()); + Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); + Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); + Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); + Text("%d active allocations", io.MetricsActiveAllocations); + //SameLine(); if (SmallButton("GC")) { g.GcCompactAll = true; } + + Separator(); + // Debugging enums enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Content, WRT_ContentRegionRect, WRT_Count }; // Windows Rect Type const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Content", "ContentRegionRect" }; enum { TRT_OuterRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersIdeal, TRT_ColumnsContentRowsFrozen, TRT_ColumnsContentRowsUnfrozen, TRT_Count }; // Tables Rect Type const char* trt_rects_names[TRT_Count] = { "OuterRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersIdeal", "ColumnsContentRowsFrozen", "ColumnsContentRowsUnfrozen" }; + if (cfg->ShowWindowsRectsType < 0) + cfg->ShowWindowsRectsType = WRT_WorkRect; + if (cfg->ShowTablesRectsType < 0) + cfg->ShowTablesRectsType = TRT_WorkRect; - // State - static bool show_windows_rects = false; - static int show_windows_rect_type = WRT_WorkRect; - static bool show_windows_begin_order = false; - static bool show_tables_rects = false; - static int show_tables_rect_type = TRT_WorkRect; - static bool show_drawcmd_mesh = true; - static bool show_drawcmd_aabb = true; - - // Basic info - ImGuiContext& g = *GImGui; - ImGuiIO& io = ImGui::GetIO(); - ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); - ImGui::Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); - ImGui::Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); - ImGui::Text("%d active allocations", io.MetricsActiveAllocations); - ImGui::Separator(); - - // Helper functions to display common structures: - // - NodeDrawList() - // - NodeColumns() - // - NodeWindow() - // - NodeWindows() - // - NodeTabBar() - // - NodeStorage() struct Funcs { static ImRect GetWindowRect(ImGuiWindow* window, int rect_type) @@ -10330,416 +10466,174 @@ void ImGui::ShowMetricsWindow(bool* p_open) IM_ASSERT(0); return ImRect(); } - - static void NodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, int elem_offset, bool show_mesh, bool show_aabb) - { - IM_ASSERT(show_mesh || show_aabb); - ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list - ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; - - // Draw wire-frame version of all triangles - ImRect clip_rect = draw_cmd->ClipRect; - ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - ImDrawListFlags backup_flags = fg_draw_list->Flags; - fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. - for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + draw_cmd->ElemCount); base_idx += 3) - { - ImVec2 triangle[3]; - for (int n = 0; n < 3; n++) - { - ImVec2 p = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; - triangle[n] = p; - vtxs_rect.Add(p); - } - if (show_mesh) - fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles - } - // Draw bounding boxes - if (show_aabb) - { - fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU - fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles - } - fg_draw_list->Flags = backup_flags; - } - - static void NodeDrawList(ImGuiWindow* window, ImDrawList* draw_list, const char* label) - { - bool node_open = ImGui::TreeNode(draw_list, "%s: '%s' %d vtx, %d indices, %d cmds", label, draw_list->_OwnerName ? draw_list->_OwnerName : "", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, draw_list->CmdBuffer.Size); - if (draw_list == ImGui::GetWindowDrawList()) - { - ImGui::SameLine(); - ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered) - if (node_open) ImGui::TreePop(); - return; - } - - ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list - if (window && IsItemHovered()) - fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); - if (!node_open) - return; - - if (window && !window->WasActive) - ImGui::TextDisabled("Warning: owning Window is inactive. This DrawList is not being rendered!"); - - unsigned int elem_offset = 0; - for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.begin(); pcmd < draw_list->CmdBuffer.end(); elem_offset += pcmd->ElemCount, pcmd++) - { - if (pcmd->UserCallback == NULL && pcmd->ElemCount == 0) - continue; - if (pcmd->UserCallback) - { - ImGui::BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); - continue; - } - - ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; - char buf[300]; - ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d triangles, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", - pcmd->ElemCount / 3, (void*)(intptr_t)pcmd->TextureId, - pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); - bool pcmd_node_open = ImGui::TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); - if (ImGui::IsItemHovered() && (show_drawcmd_mesh || show_drawcmd_aabb) && fg_draw_list) - NodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, elem_offset, show_drawcmd_mesh, show_drawcmd_aabb); - if (!pcmd_node_open) - continue; - - // Calculate approximate coverage area (touched pixel count) - // This will be in pixels squared as long there's no post-scaling happening to the renderer output. - float total_area = 0.0f; - for (unsigned int base_idx = elem_offset; base_idx < (elem_offset + pcmd->ElemCount); base_idx += 3) - { - ImVec2 triangle[3]; - for (int n = 0; n < 3; n++) - triangle[n] = draw_list->VtxBuffer[idx_buffer ? idx_buffer[base_idx + n] : (base_idx + n)].pos; - total_area += ImTriangleArea(triangle[0], triangle[1], triangle[2]); - } - - // Display vertex information summary. Hover to get all triangles drawn in wire-frame - ImFormatString(buf, IM_ARRAYSIZE(buf), "Mesh: ElemCount: %d, VtxOffset: +%d, IdxOffset: +%d, Area: ~%0.f px", pcmd->ElemCount, pcmd->VtxOffset, pcmd->IdxOffset, total_area); - ImGui::Selectable(buf); - if (ImGui::IsItemHovered() && fg_draw_list) - NodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, elem_offset, true, false); - - // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. - ImGuiListClipper clipper(pcmd->ElemCount / 3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. - while (clipper.Step()) - for (int prim = clipper.DisplayStart, idx_i = elem_offset + clipper.DisplayStart * 3; prim < clipper.DisplayEnd; prim++) - { - char* buf_p = buf, *buf_end = buf + IM_ARRAYSIZE(buf); - ImVec2 triangle[3]; - for (int n = 0; n < 3; n++, idx_i++) - { - ImDrawVert& v = draw_list->VtxBuffer[idx_buffer ? idx_buffer[idx_i] : idx_i]; - triangle[n] = v.pos; - buf_p += ImFormatString(buf_p, buf_end - buf_p, "%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\n", - (n == 0) ? "Vert:" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); - } - - ImGui::Selectable(buf, false); - if (fg_draw_list && ImGui::IsItemHovered()) - { - ImDrawListFlags backup_flags = fg_draw_list->Flags; - fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. - fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255,255,0,255), true, 1.0f); - fg_draw_list->Flags = backup_flags; - } - } - ImGui::TreePop(); - } - ImGui::TreePop(); - } - - static void NodeColumns(const ImGuiColumns* columns) - { - if (!ImGui::TreeNode((void*)(uintptr_t)columns->ID, "Columns Id: 0x%08X, Count: %d, Flags: 0x%04X", columns->ID, columns->Count, columns->Flags)) - return; - ImGui::BulletText("Width: %.1f (MinX: %.1f, MaxX: %.1f)", columns->OffMaxX - columns->OffMinX, columns->OffMinX, columns->OffMaxX); - for (int column_n = 0; column_n < columns->Columns.Size; column_n++) - ImGui::BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", column_n, columns->Columns[column_n].OffsetNorm, GetColumnOffsetFromNorm(columns, columns->Columns[column_n].OffsetNorm)); - ImGui::TreePop(); - } - - static void NodeWindows(ImVector& windows, const char* label) - { - if (!ImGui::TreeNode(label, "%s (%d)", label, windows.Size)) - return; - ImGui::Text("(In front-to-back order:)"); - for (int i = windows.Size - 1; i >= 0; i--) // Iterate front to back - { - ImGui::PushID(windows[i]); - Funcs::NodeWindow(windows[i], "Window"); - ImGui::PopID(); - } - ImGui::TreePop(); - } - - static void NodeWindow(ImGuiWindow* window, const char* label) - { - if (window == NULL) - { - ImGui::BulletText("%s: NULL", label); - return; - } - - ImGuiContext& g = *GImGui; - const bool is_active = window->WasActive; - ImGuiTreeNodeFlags tree_node_flags = (window == g.NavWindow) ? ImGuiTreeNodeFlags_Selected : ImGuiTreeNodeFlags_None; - if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } - const bool open = ImGui::TreeNodeEx(label, tree_node_flags, "%s '%s'%s", label, window->Name, is_active ? "" : " *Inactive*"); - if (!is_active) { PopStyleColor(); } - if (ImGui::IsItemHovered() && is_active) - ImGui::GetForegroundDrawList(window)->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); - if (!open) - return; - - if (window->MemoryCompacted) - ImGui::TextDisabled("Note: some memory buffers have been compacted/freed."); - - ImGuiWindowFlags flags = window->Flags; - NodeDrawList(window, window->DrawList, "DrawList"); - ImGui::BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y); - ImGui::BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, - (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", - (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", - (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); - ImGui::BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f) Scrollbar:%s%s", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y, window->ScrollbarX ? "X" : "", window->ScrollbarY ? "Y" : ""); - ImGui::BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); - ImGui::BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); - ImGui::BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); - ImGui::BulletText("NavLastChildNavWindow: %s", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); - if (!window->NavRectRel[0].IsInverted()) - ImGui::BulletText("NavRectRel[0]: (%.1f,%.1f)(%.1f,%.1f)", window->NavRectRel[0].Min.x, window->NavRectRel[0].Min.y, window->NavRectRel[0].Max.x, window->NavRectRel[0].Max.y); - else - ImGui::BulletText("NavRectRel[0]: "); - if (window->RootWindow != window) NodeWindow(window->RootWindow, "RootWindow"); - if (window->ParentWindow != NULL) NodeWindow(window->ParentWindow, "ParentWindow"); - if (window->DC.ChildWindows.Size > 0) NodeWindows(window->DC.ChildWindows, "ChildWindows"); - if (window->ColumnsStorage.Size > 0 && ImGui::TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) - { - for (int n = 0; n < window->ColumnsStorage.Size; n++) - NodeColumns(&window->ColumnsStorage[n]); - ImGui::TreePop(); - } - NodeStorage(&window->StateStorage, "Storage"); - ImGui::TreePop(); - } - - static void NodeWindowSettings(ImGuiWindowSettings* settings) - { - ImGui::Text("0x%08X \"%s\" Pos (%d,%d) Size (%d,%d) Collapsed=%d", - settings->ID, settings->GetName(), settings->Pos.x, settings->Pos.y, settings->Size.x, settings->Size.y, settings->Collapsed); - } - - static void NodeTabBar(ImGuiTabBar* tab_bar) - { - // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings. - char buf[256]; - char* p = buf; - const char* buf_end = buf + IM_ARRAYSIZE(buf); - const bool is_active = (tab_bar->PrevFrameVisible >= ImGui::GetFrameCount() - 2); - p += ImFormatString(p, buf_end - p, "Tab Bar 0x%08X (%d tabs)%s", tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); - IM_UNUSED(p); - if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } - bool open = ImGui::TreeNode(tab_bar, "%s", buf); - if (!is_active) { PopStyleColor(); } - if (open) - { - for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) - { - const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; - ImGui::PushID(tab); - if (ImGui::SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } ImGui::SameLine(0, 2); - if (ImGui::SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } ImGui::SameLine(); - ImGui::Text("%02d%c Tab 0x%08X '%s'", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : ""); - ImGui::PopID(); - } - ImGui::TreePop(); - } - } - - static void NodeStorage(ImGuiStorage* storage, const char* label) - { - if (!ImGui::TreeNode(label, "%s: %d entries, %d bytes", label, storage->Data.Size, storage->Data.size_in_bytes())) - return; - for (int n = 0; n < storage->Data.Size; n++) - { - const ImGuiStorage::ImGuiStoragePair& p = storage->Data[n]; - ImGui::BulletText("Key 0x%08X Value { i: %d }", p.key, p.val_i); // Important: we currently don't store a type, real value may not be integer. - } - ImGui::TreePop(); - } }; // Tools - if (ImGui::TreeNode("Tools")) + if (TreeNode("Tools")) { // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. - if (ImGui::Button("Item Picker..")) - ImGui::DebugStartItemPicker(); - ImGui::SameLine(); + if (Button("Item Picker..")) + DebugStartItemPicker(); + SameLine(); MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); - ImGui::Checkbox("Show windows begin order", &show_windows_begin_order); - ImGui::Checkbox("Show windows rectangles", &show_windows_rects); - ImGui::SameLine(); - ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); - show_windows_rects |= ImGui::Combo("##show_windows_rect_type", &show_windows_rect_type, wrt_rects_names, WRT_Count, WRT_Count); - if (show_windows_rects && g.NavWindow) + Checkbox("Show windows begin order", &cfg->ShowWindowsBeginOrder); + Checkbox("Show windows rectangles", &cfg->ShowWindowsRects); + SameLine(); + SetNextItemWidth(GetFontSize() * 12); + cfg->ShowWindowsRects |= Combo("##show_windows_rect_type", &cfg->ShowWindowsRectsType, wrt_rects_names, WRT_Count, WRT_Count); + if (cfg->ShowWindowsRects && g.NavWindow != NULL) { - ImGui::BulletText("'%s':", g.NavWindow->Name); - ImGui::Indent(); + BulletText("'%s':", g.NavWindow->Name); + Indent(); for (int rect_n = 0; rect_n < WRT_Count; rect_n++) { ImRect r = Funcs::GetWindowRect(g.NavWindow, rect_n); - ImGui::Text("(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), wrt_rects_names[rect_n]); + Text("(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), wrt_rects_names[rect_n]); } - ImGui::Unindent(); + Unindent(); } - ImGui::Checkbox("Show mesh when hovering ImDrawCmd", &show_drawcmd_mesh); - ImGui::Checkbox("Show bounding boxes when hovering ImDrawCmd", &show_drawcmd_aabb); - ImGui::TreePop(); + Checkbox("Show ImDrawCmd mesh when hovering", &cfg->ShowDrawCmdMesh); + Checkbox("Show ImDrawCmd bounding boxes when hovering", &cfg->ShowDrawCmdBoundingBoxes); + TreePop(); } // Contents - Funcs::NodeWindows(g.Windows, "Windows"); - //Funcs::NodeWindows(g.WindowsFocusOrder, "WindowsFocusOrder"); - if (ImGui::TreeNode("DrawLists", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) + DebugNodeWindowsList(&g.Windows, "Windows"); + //DebugNodeWindowList(&g.WindowsFocusOrder, "WindowsFocusOrder"); + if (TreeNode("DrawLists", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) { for (int i = 0; i < g.DrawDataBuilder.Layers[0].Size; i++) - Funcs::NodeDrawList(NULL, g.DrawDataBuilder.Layers[0][i], "DrawList"); - ImGui::TreePop(); + DebugNodeDrawList(NULL, g.DrawDataBuilder.Layers[0][i], "DrawList"); + TreePop(); } // Details for Popups - if (ImGui::TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) + if (TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) { for (int i = 0; i < g.OpenPopupStack.Size; i++) { ImGuiWindow* window = g.OpenPopupStack[i].Window; - ImGui::BulletText("PopupID: %08x, Window: '%s'%s%s", g.OpenPopupStack[i].PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? " ChildWindow" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? " ChildMenu" : ""); + BulletText("PopupID: %08x, Window: '%s'%s%s", g.OpenPopupStack[i].PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? " ChildWindow" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? " ChildMenu" : ""); } - ImGui::TreePop(); + TreePop(); } // Details for TabBars - if (ImGui::TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.GetSize())) + if (TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.GetSize())) { for (int n = 0; n < g.TabBars.GetSize(); n++) - Funcs::NodeTabBar(g.TabBars.GetByIndex(n)); - ImGui::TreePop(); + DebugNodeTabBar(g.TabBars.GetByIndex(n), "TabBar"); + TreePop(); } // Details for Tables IM_UNUSED(trt_rects_names); - IM_UNUSED(show_tables_rects); - IM_UNUSED(show_tables_rect_type); #ifdef IMGUI_HAS_TABLE - if (ImGui::TreeNode("Tables", "Tables (%d)", g.Tables.GetSize())) + if (TreeNode("Tables", "Tables (%d)", g.Tables.GetSize())) { for (int n = 0; n < g.Tables.GetSize(); n++) - Funcs::NodeTable(g.Tables.GetByIndex(n)); - ImGui::TreePop(); + DebugNodeTable(g.Tables.GetByIndex(n)); + TreePop(); } #endif // #ifdef IMGUI_HAS_TABLE // Details for Docking #ifdef IMGUI_HAS_DOCK - if (ImGui::TreeNode("Dock nodes")) + if (TreeNode("Docking")) { - ImGui::TreePop(); + TreePop(); } #endif // #ifdef IMGUI_HAS_DOCK // Settings - if (ImGui::TreeNode("Settings")) - { - if (ImGui::SmallButton("Clear")) - ImGui::ClearIniSettings(); - ImGui::SameLine(); - if (ImGui::SmallButton("Save to memory")) - ImGui::SaveIniSettingsToMemory(); - ImGui::SameLine(); - if (ImGui::SmallButton("Save to disk")) - ImGui::SaveIniSettingsToDisk(g.IO.IniFilename); - ImGui::SameLine(); + if (TreeNode("Settings")) + { + if (SmallButton("Clear")) + ClearIniSettings(); + SameLine(); + if (SmallButton("Save to memory")) + SaveIniSettingsToMemory(); + SameLine(); + if (SmallButton("Save to disk")) + SaveIniSettingsToDisk(g.IO.IniFilename); + SameLine(); if (g.IO.IniFilename) - ImGui::Text("\"%s\"", g.IO.IniFilename); + Text("\"%s\"", g.IO.IniFilename); else - ImGui::TextUnformatted(""); - ImGui::Text("SettingsDirtyTimer %.2f", g.SettingsDirtyTimer); - if (ImGui::TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) + TextUnformatted(""); + Text("SettingsDirtyTimer %.2f", g.SettingsDirtyTimer); + if (TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) { for (int n = 0; n < g.SettingsHandlers.Size; n++) - ImGui::BulletText("%s", g.SettingsHandlers[n].TypeName); - ImGui::TreePop(); + BulletText("%s", g.SettingsHandlers[n].TypeName); + TreePop(); } - if (ImGui::TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) + if (TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) { for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) - Funcs::NodeWindowSettings(settings); - ImGui::TreePop(); + DebugNodeWindowSettings(settings); + TreePop(); } #ifdef IMGUI_HAS_TABLE - if (ImGui::TreeNode("SettingsTables", "Settings packed data: Tables: %d bytes", g.SettingsTables.size())) + if (TreeNode("SettingsTables", "Settings packed data: Tables: %d bytes", g.SettingsTables.size())) { for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) - Funcs::NodeTableSettings(settings); - ImGui::TreePop(); + DebugNodeTableSettings(settings); + TreePop(); } #endif // #ifdef IMGUI_HAS_TABLE #ifdef IMGUI_HAS_DOCK #endif // #ifdef IMGUI_HAS_DOCK - if (ImGui::TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size())) + if (TreeNode("SettingsIniData", "Settings unpacked data (.ini): %d bytes", g.SettingsIniData.size())) { - ImGui::InputTextMultiline("##Ini", (char*)(void*)g.SettingsIniData.c_str(), g.SettingsIniData.Buf.Size, ImVec2(-FLT_MIN, 0.0f), ImGuiInputTextFlags_ReadOnly); - ImGui::TreePop(); + InputTextMultiline("##Ini", (char*)(void*)g.SettingsIniData.c_str(), g.SettingsIniData.Buf.Size, ImVec2(-FLT_MIN, GetTextLineHeight() * 20), ImGuiInputTextFlags_ReadOnly); + TreePop(); } - ImGui::TreePop(); + TreePop(); } // Misc Details - if (ImGui::TreeNode("Internal state")) + if (TreeNode("Internal state")) { const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); - ImGui::Text("WINDOWING"); - ImGui::Indent(); - ImGui::Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); - ImGui::Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); - ImGui::Text("HoveredWindowUnderMovingWindow: '%s'", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : "NULL"); - ImGui::Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); - ImGui::Unindent(); - - ImGui::Text("ITEMS"); - ImGui::Indent(); - ImGui::Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); - ImGui::Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); - ImGui::Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not - ImGui::Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); - ImGui::Unindent(); - - ImGui::Text("NAV,FOCUS"); - ImGui::Indent(); - ImGui::Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); - ImGui::Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); - ImGui::Text("NavInputSource: %s", input_source_names[g.NavInputSource]); - ImGui::Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); - ImGui::Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); - ImGui::Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); - ImGui::Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); - ImGui::Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); - ImGui::Unindent(); - - ImGui::TreePop(); + Text("WINDOWING"); + Indent(); + Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); + Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); + Text("HoveredWindowUnderMovingWindow: '%s'", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : "NULL"); + Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); + Unindent(); + + Text("ITEMS"); + Indent(); + Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); + Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); + Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); + Unindent(); + + Text("NAV,FOCUS"); + Indent(); + Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); + Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); + Text("NavInputSource: %s", input_source_names[g.NavInputSource]); + Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); + Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); + Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); + Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); + Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); + Unindent(); + + TreePop(); } // Overlay: Display windows Rectangles and Begin Order - if (show_windows_rects || show_windows_begin_order) + if (cfg->ShowWindowsRects || cfg->ShowWindowsBeginOrder) { for (int n = 0; n < g.Windows.Size; n++) { @@ -10747,16 +10641,16 @@ void ImGui::ShowMetricsWindow(bool* p_open) if (!window->WasActive) continue; ImDrawList* draw_list = GetForegroundDrawList(window); - if (show_windows_rects) + if (cfg->ShowWindowsRects) { - ImRect r = Funcs::GetWindowRect(window, show_windows_rect_type); + ImRect r = Funcs::GetWindowRect(window, cfg->ShowWindowsRectsType); draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 0, 128, 255)); } - if (show_windows_begin_order && !(window->Flags & ImGuiWindowFlags_ChildWindow)) + if (cfg->ShowWindowsBeginOrder && !(window->Flags & ImGuiWindowFlags_ChildWindow)) { char buf[32]; ImFormatString(buf, IM_ARRAYSIZE(buf), "%d", window->BeginOrderWithinContext); - float font_size = ImGui::GetFontSize(); + float font_size = GetFontSize(); draw_list->AddRectFilled(window->Pos, window->Pos + ImVec2(font_size, font_size), IM_COL32(200, 100, 100, 255)); draw_list->AddText(window->Pos, IM_COL32(255, 255, 255, 255), buf); } @@ -10781,12 +10675,276 @@ void ImGui::ShowMetricsWindow(bool* p_open) } #endif // #ifdef IMGUI_HAS_DOCK - ImGui::End(); + End(); +} + +// [DEBUG] Display contents of Columns +void ImGui::DebugNodeColumns(ImGuiOldColumns* columns) +{ + if (!TreeNode((void*)(uintptr_t)columns->ID, "Columns Id: 0x%08X, Count: %d, Flags: 0x%04X", columns->ID, columns->Count, columns->Flags)) + return; + BulletText("Width: %.1f (MinX: %.1f, MaxX: %.1f)", columns->OffMaxX - columns->OffMinX, columns->OffMinX, columns->OffMaxX); + for (int column_n = 0; column_n < columns->Columns.Size; column_n++) + BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", column_n, columns->Columns[column_n].OffsetNorm, GetColumnOffsetFromNorm(columns, columns->Columns[column_n].OffsetNorm)); + TreePop(); +} + +// [DEBUG] Display contents of ImDrawList +void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label) +{ + ImGuiContext& g = *GImGui; + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + int cmd_count = draw_list->CmdBuffer.Size; + if (cmd_count > 0 && draw_list->CmdBuffer.back().ElemCount == 0 && draw_list->CmdBuffer.back().UserCallback == NULL) + cmd_count--; + bool node_open = TreeNode(draw_list, "%s: '%s' %d vtx, %d indices, %d cmds", label, draw_list->_OwnerName ? draw_list->_OwnerName : "", draw_list->VtxBuffer.Size, draw_list->IdxBuffer.Size, cmd_count); + if (draw_list == GetWindowDrawList()) + { + SameLine(); + TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "CURRENTLY APPENDING"); // Can't display stats for active draw list! (we don't have the data double-buffered) + if (node_open) + TreePop(); + return; + } + + ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list + if (window && IsItemHovered()) + fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); + if (!node_open) + return; + + if (window && !window->WasActive) + TextDisabled("Warning: owning Window is inactive. This DrawList is not being rendered!"); + + for (const ImDrawCmd* pcmd = draw_list->CmdBuffer.Data; pcmd < draw_list->CmdBuffer.Data + cmd_count; pcmd++) + { + if (pcmd->UserCallback) + { + BulletText("Callback %p, user_data %p", pcmd->UserCallback, pcmd->UserCallbackData); + continue; + } + + char buf[300]; + ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d tris, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", + pcmd->ElemCount / 3, (void*)(intptr_t)pcmd->TextureId, + pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); + bool pcmd_node_open = TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); + if (IsItemHovered() && (cfg->ShowDrawCmdMesh || cfg->ShowDrawCmdBoundingBoxes) && fg_draw_list) + DebugNodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, cfg->ShowDrawCmdMesh, cfg->ShowDrawCmdBoundingBoxes); + if (!pcmd_node_open) + continue; + + // Calculate approximate coverage area (touched pixel count) + // This will be in pixels squared as long there's no post-scaling happening to the renderer output. + const ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; + const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + pcmd->VtxOffset; + float total_area = 0.0f; + for (unsigned int idx_n = pcmd->IdxOffset; idx_n < pcmd->IdxOffset + pcmd->ElemCount; ) + { + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++, idx_n++) + triangle[n] = vtx_buffer[idx_buffer ? idx_buffer[idx_n] : idx_n].pos; + total_area += ImTriangleArea(triangle[0], triangle[1], triangle[2]); + } + + // Display vertex information summary. Hover to get all triangles drawn in wire-frame + ImFormatString(buf, IM_ARRAYSIZE(buf), "Mesh: ElemCount: %d, VtxOffset: +%d, IdxOffset: +%d, Area: ~%0.f px", pcmd->ElemCount, pcmd->VtxOffset, pcmd->IdxOffset, total_area); + Selectable(buf); + if (IsItemHovered() && fg_draw_list) + DebugNodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, true, false); + + // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. + ImGuiListClipper clipper; + clipper.Begin(pcmd->ElemCount / 3); // Manually coarse clip our print out of individual vertices to save CPU, only items that may be visible. + while (clipper.Step()) + for (int prim = clipper.DisplayStart, idx_i = pcmd->IdxOffset + clipper.DisplayStart * 3; prim < clipper.DisplayEnd; prim++) + { + char* buf_p = buf, * buf_end = buf + IM_ARRAYSIZE(buf); + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++, idx_i++) + { + const ImDrawVert& v = vtx_buffer[idx_buffer ? idx_buffer[idx_i] : idx_i]; + triangle[n] = v.pos; + buf_p += ImFormatString(buf_p, buf_end - buf_p, "%s %04d: pos (%8.2f,%8.2f), uv (%.6f,%.6f), col %08X\n", + (n == 0) ? "Vert:" : " ", idx_i, v.pos.x, v.pos.y, v.uv.x, v.uv.y, v.col); + } + + Selectable(buf, false); + if (fg_draw_list && IsItemHovered()) + { + ImDrawListFlags backup_flags = fg_draw_list->Flags; + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); + fg_draw_list->Flags = backup_flags; + } + } + TreePop(); + } + TreePop(); +} + +// [DEBUG] Display mesh/aabb of a ImDrawCmd +void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb) +{ + IM_ASSERT(show_mesh || show_aabb); + ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list + ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; + ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + draw_cmd->VtxOffset; + + // Draw wire-frame version of all triangles + ImRect clip_rect = draw_cmd->ClipRect; + ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + ImDrawListFlags backup_flags = fg_draw_list->Flags; + fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + for (unsigned int idx_n = draw_cmd->IdxOffset; idx_n < draw_cmd->IdxOffset + draw_cmd->ElemCount; ) + { + ImVec2 triangle[3]; + for (int n = 0; n < 3; n++, idx_n++) + vtxs_rect.Add((triangle[n] = vtx_buffer[idx_buffer ? idx_buffer[idx_n] : idx_n].pos)); + if (show_mesh) + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles + } + // Draw bounding boxes + if (show_aabb) + { + fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU + fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles + } + fg_draw_list->Flags = backup_flags; +} + +// [DEBUG] Display contents of ImGuiStorage +void ImGui::DebugNodeStorage(ImGuiStorage* storage, const char* label) +{ + if (!TreeNode(label, "%s: %d entries, %d bytes", label, storage->Data.Size, storage->Data.size_in_bytes())) + return; + for (int n = 0; n < storage->Data.Size; n++) + { + const ImGuiStorage::ImGuiStoragePair& p = storage->Data[n]; + BulletText("Key 0x%08X Value { i: %d }", p.key, p.val_i); // Important: we currently don't store a type, real value may not be integer. + } + TreePop(); +} + +// [DEBUG] Display contents of ImGuiTabBar +void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) +{ + // Standalone tab bars (not associated to docking/windows functionality) currently hold no discernible strings. + char buf[256]; + char* p = buf; + const char* buf_end = buf + IM_ARRAYSIZE(buf); + const bool is_active = (tab_bar->PrevFrameVisible >= GetFrameCount() - 2); + p += ImFormatString(p, buf_end - p, "%s 0x%08X (%d tabs)%s", label, tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); + IM_UNUSED(p); + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + bool open = TreeNode(tab_bar, "%s", buf); + if (!is_active) { PopStyleColor(); } + if (is_active && IsItemHovered()) + { + ImDrawList* draw_list = GetForegroundDrawList(); + draw_list->AddRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max, IM_COL32(255, 255, 0, 255)); + draw_list->AddLine(ImVec2(tab_bar->ScrollingRectMinX, tab_bar->BarRect.Min.y), ImVec2(tab_bar->ScrollingRectMinX, tab_bar->BarRect.Max.y), IM_COL32(0, 255, 0, 255)); + draw_list->AddLine(ImVec2(tab_bar->ScrollingRectMaxX, tab_bar->BarRect.Min.y), ImVec2(tab_bar->ScrollingRectMaxX, tab_bar->BarRect.Max.y), IM_COL32(0, 255, 0, 255)); + } + if (open) + { + for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + { + const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + PushID(tab); + if (SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } SameLine(0, 2); + if (SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } SameLine(); + Text("%02d%c Tab 0x%08X '%s' Offset: %.1f, Width: %.1f/%.1f", + tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "", tab->Offset, tab->Width, tab->ContentWidth); + PopID(); + } + TreePop(); + } +} + +void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) +{ + if (window == NULL) + { + BulletText("%s: NULL", label); + return; + } + + ImGuiContext& g = *GImGui; + const bool is_active = window->WasActive; + ImGuiTreeNodeFlags tree_node_flags = (window == g.NavWindow) ? ImGuiTreeNodeFlags_Selected : ImGuiTreeNodeFlags_None; + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + const bool open = TreeNodeEx(label, tree_node_flags, "%s '%s'%s", label, window->Name, is_active ? "" : " *Inactive*"); + if (!is_active) { PopStyleColor(); } + if (IsItemHovered() && is_active) + GetForegroundDrawList(window)->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); + if (!open) + return; + + if (window->MemoryCompacted) + TextDisabled("Note: some memory buffers have been compacted/freed."); + + ImGuiWindowFlags flags = window->Flags; + DebugNodeDrawList(window, window->DrawList, "DrawList"); + BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y); + BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, + (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", + (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", + (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); + BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f) Scrollbar:%s%s", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y, window->ScrollbarX ? "X" : "", window->ScrollbarY ? "Y" : ""); + BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); + BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); + BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); + BulletText("NavLastChildNavWindow: %s", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); + if (!window->NavRectRel[0].IsInverted()) + BulletText("NavRectRel[0]: (%.1f,%.1f)(%.1f,%.1f)", window->NavRectRel[0].Min.x, window->NavRectRel[0].Min.y, window->NavRectRel[0].Max.x, window->NavRectRel[0].Max.y); + else + BulletText("NavRectRel[0]: "); + if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } + if (window->ParentWindow != NULL) { DebugNodeWindow(window->ParentWindow, "ParentWindow"); } + if (window->DC.ChildWindows.Size > 0) { DebugNodeWindowsList(&window->DC.ChildWindows, "ChildWindows"); } + if (window->ColumnsStorage.Size > 0 && TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) + { + for (int n = 0; n < window->ColumnsStorage.Size; n++) + DebugNodeColumns(&window->ColumnsStorage[n]); + TreePop(); + } + DebugNodeStorage(&window->StateStorage, "Storage"); + TreePop(); +} + +void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings* settings) +{ + Text("0x%08X \"%s\" Pos (%d,%d) Size (%d,%d) Collapsed=%d", + settings->ID, settings->GetName(), settings->Pos.x, settings->Pos.y, settings->Size.x, settings->Size.y, settings->Collapsed); +} + + +void ImGui::DebugNodeWindowsList(ImVector* windows, const char* label) +{ + if (!TreeNode(label, "%s (%d)", label, windows->Size)) + return; + Text("(In front-to-back order:)"); + for (int i = windows->Size - 1; i >= 0; i--) // Iterate front to back + { + PushID((*windows)[i]); + DebugNodeWindow((*windows)[i], "Window"); + PopID(); + } + TreePop(); } #else -void ImGui::ShowMetricsWindow(bool*) { } +void ImGui::ShowMetricsWindow(bool*) {} +void ImGui::DebugNodeColumns(ImGuiOldColumns*) {} +void ImGui::DebugNodeDrawList(ImGuiWindow*, const ImDrawList*, const char*) {} +void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow*, const ImDrawList*, const ImDrawCmd*, bool, bool) {} +void ImGui::DebugNodeStorage(ImGuiStorage*, const char*) {} +void ImGui::DebugNodeTabBar(ImGuiTabBar*, const char*) {} +void ImGui::DebugNodeWindow(ImGuiWindow*, const char*) {} +void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings*) {} +void ImGui::DebugNodeWindowsList(ImVector*, const char*) {} #endif diff --git a/imgui/imgui.h b/imgui/imgui.h index 48889186..6d2b54cd 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (headers) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/3075 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/3488 (please post your screenshots/video there!) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues @@ -59,12 +59,12 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.79 WIP" -#define IMGUI_VERSION_NUM 17803 +#define IMGUI_VERSION "1.80 WIP" +#define IMGUI_VERSION_NUM 17906 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Define attributes of all API symbols declarations (e.g. for DLL under Windows) -// IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default bindings files (imgui_impl_xxx.h) +// IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default backends files (imgui_impl_xxx.h) // Using dear imgui via a shared library is not recommended, because we don't guarantee backward nor forward ABI compatibility (also function call overhead, as dear imgui is a call-heavy API) #ifndef IMGUI_API #define IMGUI_API @@ -78,13 +78,6 @@ Index of this file: #include #define IM_ASSERT(_EXPR) assert(_EXPR) // You can override the default assert handler by editing imconfig.h #endif -#if !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__)) -#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // To apply printf-style warnings to our functions. -#define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) -#else -#define IM_FMTARGS(FMT) -#define IM_FMTLIST(FMT) -#endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. #if (__cplusplus >= 201100) @@ -92,6 +85,16 @@ Index of this file: #else #define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. #endif +#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__clang__) +#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to our formatting functions. +#define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) +#elif !defined(IMGUI_USE_STB_SPRINTF) && defined(__GNUC__) && defined(__MINGW32__) +#define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) // Apply printf-style warnings to our formatting functions. +#define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0))) +#else +#define IM_FMTARGS(FMT) +#define IM_FMTLIST(FMT) +#endif // Warnings #if defined(__clang__) @@ -172,7 +175,7 @@ typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: f // Other types #ifndef ImTextureID // ImTextureID [configurable type: override in imconfig.h with '#define ImTextureID xxx'] -typedef void* ImTextureID; // User data for rendering back-end to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. +typedef void* ImTextureID; // User data for rendering backend to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. #endif typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); @@ -251,13 +254,13 @@ namespace ImGui IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame! IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(). If you don't need to render data (skipping rendering) you may call EndFrame() without Render()... but you'll have wasted CPU already! If you don't need to render, better to not create any windows and not call NewFrame() at all! - IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can get call GetDrawData() to obtain it and run your rendering function (up to v1.60, this used to call io.RenderDrawListsFn(). Nowadays, we allow and prefer calling your render function yourself.) + IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can then get call GetDrawData(). IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render. // Demo, Debug, Information IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window (previously called ShowTestWindow). demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. - IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Debug/Metrics window. display Dear ImGui internals: draw commands (with individual draw calls and vertices), window list, basic internal state, etc. + IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debugger window. display Dear ImGui internals: windows, draw commands, various internal state, etc. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. @@ -288,7 +291,10 @@ namespace ImGui // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400). // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. - // Always call a matching EndChild() for each BeginChild() call, regardless of its return value [as with Begin: this is due to legacy reason and inconsistent with most BeginXXX functions apart from the regular Begin() which behaves like BeginChild().] + // Always call a matching EndChild() for each BeginChild() call, regardless of its return value. + // [Important: due to legacy reason, this is inconsistent with most other functions such as BeginMenu/EndMenu, + // BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function + // returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); IMGUI_API void EndChild(); @@ -352,6 +358,10 @@ namespace ImGui IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); IMGUI_API void PopStyleVar(int count = 1); + IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets + IMGUI_API void PopAllowKeyboardFocus(); + IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. + IMGUI_API void PopButtonRepeat(); IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. IMGUI_API ImFont* GetFont(); // get current font IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied @@ -367,10 +377,6 @@ namespace ImGui IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position. NOT necessarily the width of last item unlike most 'Item' functions. IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // push word-wrapping position for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space IMGUI_API void PopTextWrapPos(); - IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets - IMGUI_API void PopAllowKeyboardFocus(); - IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. - IMGUI_API void PopButtonRepeat(); // Cursor / Layout // - By "cursor" we mean the current output position. @@ -384,8 +390,8 @@ namespace ImGui IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in an horizontal-layout context. IMGUI_API void Spacing(); // add vertical spacing. IMGUI_API void Dummy(const ImVec2& size); // add a dummy item of given size. unlike InvisibleButton(), Dummy() won't take the mouse click or be navigable into. - IMGUI_API void Indent(float indent_w = 0.0f); // move content position toward the right, by style.IndentSpacing or indent_w if != 0 - IMGUI_API void Unindent(float indent_w = 0.0f); // move content position back to the left, by style.IndentSpacing or indent_w if != 0 + IMGUI_API void Indent(float indent_w = 0.0f); // move content position toward the right, by indent_w, or style.IndentSpacing if indent_w <= 0 + IMGUI_API void Unindent(float indent_w = 0.0f); // move content position back to the left, by indent_w, or style.IndentSpacing if indent_w <= 0 IMGUI_API void BeginGroup(); // lock horizontal starting position IMGUI_API void EndGroup(); // unlock horizontal starting position + capture the whole group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) IMGUI_API ImVec2 GetCursorPos(); // cursor position in window coordinates (relative to window position) @@ -444,6 +450,7 @@ namespace ImGui IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding IMGUI_API bool Checkbox(const char* label, bool* v); + IMGUI_API bool CheckboxFlags(const char* label, int* flags, int flags_value); IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer @@ -522,14 +529,14 @@ namespace ImGui IMGUI_API bool InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0); IMGUI_API bool InputScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0); - // Widgets: Color Editor/Picker (tip: the ColorEdit* functions have a little colored preview square that can be left-clicked to open a picker, and right-clicked to open an option menu.) + // Widgets: Color Editor/Picker (tip: the ColorEdit* functions have a little color square that can be left-clicked to open a picker, and right-clicked to open an option menu.) // - Note that in C++ a 'float v[X]' function argument is the _same_ as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. // - You can pass the address of a first float element out of a contiguous structure, e.g. &myvector.x IMGUI_API bool ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL); - IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0, 0)); // display a colored square/button, hover for details, return true when pressed. + IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0, 0)); // display a color square/button, hover for details, return true when pressed. IMGUI_API void SetColorEditOptions(ImGuiColorEditFlags flags); // initialize current options (generally on application startup) if you want to select a default format, picker type, etc. User will be able to change many settings, unless you pass the _NoOptions flag to your calls. // Widgets: Trees @@ -620,13 +627,13 @@ namespace ImGui // - CloseCurrentPopup() is called by default by Selectable()/MenuItem() when activated (FIXME: need some options). // - Use ImGuiPopupFlags_NoOpenOverExistingPopup to avoid opening a popup if there's already one at the same level. This is equivalent to e.g. testing for !IsAnyPopupOpen() prior to OpenPopup(). IMGUI_API void OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags = 0); // call to mark popup as open (don't call every frame!). - IMGUI_API bool OpenPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) + IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) IMGUI_API void CloseCurrentPopup(); // manually close the popup we have begin-ed into. // Popups: open+begin combined functions helpers // - Helpers to do OpenPopup+BeginPopup where the Open action is triggered by e.g. hovering an item and right-clicking. // - They are convenient to easily create context menus, hence the name. // - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future. - // - We exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter. Passing a mouse button to ImGuiPopupFlags is guaranteed to be legal. + // - IMPORTANT: we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight. IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window. IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows). @@ -653,8 +660,9 @@ namespace ImGui // Tab Bars, Tabs IMGUI_API bool BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0); // create and append into a TabBar IMGUI_API void EndTabBar(); // only call EndTabBar() if BeginTabBar() returns true! - IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0);// create a Tab. Returns true if the Tab is selected. + IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0); // create a Tab. Returns true if the Tab is selected. IMGUI_API void EndTabItem(); // only call EndTabItem() if BeginTabItem() returns true! + IMGUI_API bool TabItemButton(const char* label, ImGuiTabItemFlags flags = 0); // create a Tab behaving like a button. return true when clicked. cannot be selected in the tab bar. IMGUI_API void SetTabItemClosed(const char* tab_or_docked_window_label); // notify TabBar or Docking system of a closed tab/window ahead (useful to reduce visual flicker on reorderable tab bars). For tab-bar: call after BeginTabBar() and before Tab submissions. Otherwise call with a window name. // Logging/Capture @@ -732,7 +740,7 @@ namespace ImGui IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); // Inputs Utilities: Keyboard - // - For 'int user_key_index' you can use your own indices/enums according to how your back-end/engine stored them in io.KeysDown[]. + // - For 'int user_key_index' you can use your own indices/enums according to how your backend/engine stored them in io.KeysDown[]. // - We don't know the meaning of those value. You can use GetKeyIndex() to map a ImGuiKey_ value into the user index. IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. @@ -827,8 +835,7 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_ChildMenu = 1 << 28 // Don't use! For internal use by BeginMenu() // [Obsolete] - //ImGuiWindowFlags_ShowBorders = 1 << 7, // --> Set style.FrameBorderSize=1.0f or style.WindowBorderSize=1.0f to enable borders around items or windows. - //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges=true and make sure mouse cursors are supported by back-end (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) + //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges=true and make sure mouse cursors are supported by backend (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) }; // Flags for ImGui::InputText() @@ -865,7 +872,7 @@ enum ImGuiTreeNodeFlags_ { ImGuiTreeNodeFlags_None = 0, ImGuiTreeNodeFlags_Selected = 1 << 0, // Draw as selected - ImGuiTreeNodeFlags_Framed = 1 << 1, // Full colored frame (e.g. for CollapsingHeader) + ImGuiTreeNodeFlags_Framed = 1 << 1, // Draw frame with background (e.g. for CollapsingHeader) ImGuiTreeNodeFlags_AllowItemOverlap = 1 << 2, // Hit testing to allow subsequent widgets to overlap this one ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes) @@ -954,7 +961,10 @@ enum ImGuiTabItemFlags_ ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. ImGuiTabItemFlags_NoPushId = 1 << 3, // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() - ImGuiTabItemFlags_NoTooltip = 1 << 4 // Disable tooltip for the given tab + ImGuiTabItemFlags_NoTooltip = 1 << 4, // Disable tooltip for the given tab + ImGuiTabItemFlags_NoReorder = 1 << 5, // Disable reordering this tab or having another tab cross over this tab + ImGuiTabItemFlags_Leading = 1 << 6, // Enforce the tab position to the left of the tab bar (after the tab list popup button) + ImGuiTabItemFlags_Trailing = 1 << 7 // Enforce the tab position to the right of the tab bar (before the scrolling buttons) }; // Flags for ImGui::IsWindowFocused() @@ -1062,7 +1072,7 @@ enum ImGuiKey_ ImGuiKey_COUNT }; -// To test io.KeyMods (which is a combination of individual fields io.KeyCtrl, io.KeyShift, io.KeyAlt set by user/back-end) +// To test io.KeyMods (which is a combination of individual fields io.KeyCtrl, io.KeyShift, io.KeyAlt set by user/backend) enum ImGuiKeyModFlags_ { ImGuiKeyModFlags_None = 0, @@ -1074,7 +1084,7 @@ enum ImGuiKeyModFlags_ // Gamepad/Keyboard navigation // Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. -// Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Back-end: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). +// Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Backend: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). // Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW. enum ImGuiNavInput_ { @@ -1112,25 +1122,25 @@ enum ImGuiConfigFlags_ { ImGuiConfigFlags_None = 0, ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. NewFrame() will automatically fill io.NavInputs[] based on io.KeysDown[]. - ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. This is mostly to instruct your imgui back-end to fill io.NavInputs[]. Back-end also needs to set ImGuiBackendFlags_HasGamepad. - ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your binding, otherwise ImGui will react as if the mouse is jumping around back and forth. + ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. This is mostly to instruct your imgui backend to fill io.NavInputs[]. Backend also needs to set ImGuiBackendFlags_HasGamepad. + ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your backend, otherwise ImGui will react as if the mouse is jumping around back and forth. ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. - ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the back-end. - ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct back-end to not alter mouse cursor shape and visibility. Use if the back-end cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. + ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the backend. + ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct backend to not alter mouse cursor shape and visibility. Use if the backend cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. - // User storage (to allow your back-end/engine to communicate to code that may be shared between multiple projects. Those flags are not used by core Dear ImGui) + // User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are not used by core Dear ImGui) ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. ImGuiConfigFlags_IsTouchScreen = 1 << 21 // Application is using a touch screen instead of a mouse. }; -// Back-end capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom back-end. +// Backend capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom backend. enum ImGuiBackendFlags_ { ImGuiBackendFlags_None = 0, - ImGuiBackendFlags_HasGamepad = 1 << 0, // Back-end Platform supports gamepad and currently has one connected. - ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Back-end Platform supports honoring GetMouseCursor() value to change the OS cursor shape. - ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Back-end Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). - ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Back-end Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. + ImGuiBackendFlags_HasGamepad = 1 << 0, // Backend Platform supports gamepad and currently has one connected. + ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Backend Platform supports honoring GetMouseCursor() value to change the OS cursor shape. + ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). + ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. }; // Enumeration for PushStyleColor() / PopStyleColor() @@ -1189,7 +1199,6 @@ enum ImGuiCol_ // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg // [renamed in 1.63] - //, ImGuiCol_CloseButton, ImGuiCol_CloseButtonActive, ImGuiCol_CloseButtonHovered// [unused since 1.60+] the close button now uses regular button colors. #endif }; @@ -1227,11 +1236,6 @@ enum ImGuiStyleVar_ ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign ImGuiStyleVar_COUNT - - // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiStyleVar_Count_ = ImGuiStyleVar_COUNT // [renamed in 1.60] -#endif }; // Flags for InvisibleButton() [extended in imgui_internal.h] @@ -1252,13 +1256,13 @@ enum ImGuiColorEditFlags_ { ImGuiColorEditFlags_None = 0, ImGuiColorEditFlags_NoAlpha = 1 << 1, // // ColorEdit, ColorPicker, ColorButton: ignore Alpha component (will only read 3 components from the input pointer). - ImGuiColorEditFlags_NoPicker = 1 << 2, // // ColorEdit: disable picker when clicking on colored square. + ImGuiColorEditFlags_NoPicker = 1 << 2, // // ColorEdit: disable picker when clicking on color square. ImGuiColorEditFlags_NoOptions = 1 << 3, // // ColorEdit: disable toggling options menu when right-clicking on inputs/small preview. - ImGuiColorEditFlags_NoSmallPreview = 1 << 4, // // ColorEdit, ColorPicker: disable colored square preview next to the inputs. (e.g. to show only the inputs) - ImGuiColorEditFlags_NoInputs = 1 << 5, // // ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the small preview colored square). + ImGuiColorEditFlags_NoSmallPreview = 1 << 4, // // ColorEdit, ColorPicker: disable color square preview next to the inputs. (e.g. to show only the inputs) + ImGuiColorEditFlags_NoInputs = 1 << 5, // // ColorEdit, ColorPicker: disable inputs sliders/text widgets (e.g. to show only the small preview color square). ImGuiColorEditFlags_NoTooltip = 1 << 6, // // ColorEdit, ColorPicker, ColorButton: disable tooltip when hovering the preview. ImGuiColorEditFlags_NoLabel = 1 << 7, // // ColorEdit, ColorPicker: disable display of inline text label (the label is still forwarded to the tooltip and picker). - ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small colored square preview instead. + ImGuiColorEditFlags_NoSidePreview = 1 << 8, // // ColorPicker: disable bigger color preview on right side of the picker, use small color square preview instead. ImGuiColorEditFlags_NoDragDrop = 1 << 9, // // ColorEdit: disable drag and drop target. ColorButton: disable drag and drop source. ImGuiColorEditFlags_NoBorder = 1 << 10, // // ColorButton: disable border (which is enforced by default) @@ -1298,11 +1302,16 @@ enum ImGuiColorEditFlags_ enum ImGuiSliderFlags_ { ImGuiSliderFlags_None = 0, - ImGuiSliderFlags_ClampOnInput = 1 << 4, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. + ImGuiSliderFlags_AlwaysClamp = 1 << 4, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits) ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget ImGuiSliderFlags_InvalidMask_ = 0x7000000F // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp // [renamed in 1.79] +#endif }; // Identify a mouse button. @@ -1316,7 +1325,7 @@ enum ImGuiMouseButton_ }; // Enumeration for GetMouseCursor() -// User code may request binding to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here +// User code may request backend to display given cursor by calling SetMouseCursor(), which is why we have some cursors that are marked unused here enum ImGuiMouseCursor_ { ImGuiMouseCursor_None = -1, @@ -1330,11 +1339,6 @@ enum ImGuiMouseCursor_ ImGuiMouseCursor_Hand, // (Unused by Dear ImGui functions. Use for e.g. hyperlinks) ImGuiMouseCursor_NotAllowed, // When hovering something with disallowed interaction. Usually a crossed circle. ImGuiMouseCursor_COUNT - - // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiMouseCursor_Count_ = ImGuiMouseCursor_COUNT // [renamed in 1.60] -#endif }; // Enumeration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions @@ -1399,8 +1403,8 @@ struct ImVector inline int size_in_bytes() const { return Size * (int)sizeof(T); } inline int max_size() const { return 0x7FFFFFFF / (int)sizeof(T); } inline int capacity() const { return Capacity; } - inline T& operator[](int i) { IM_ASSERT(i < Size); return Data[i]; } - inline const T& operator[](int i) const { IM_ASSERT(i < Size); return Data[i]; } + inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } + inline const T& operator[](int i) const { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } inline T* begin() { return Data; } @@ -1470,7 +1474,7 @@ struct ImGuiStyle float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. - float TabMinWidthForUnselectedCloseButton; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + float TabMinWidthForCloseButton; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -1478,7 +1482,7 @@ struct ImGuiStyle ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). - bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require back-end to render with bilinear filtering. Latched at the beginning of the frame (copied to ImDrawList). + bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. Latched at the beginning of the frame (copied to ImDrawList). bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. float CircleSegmentMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. @@ -1501,7 +1505,7 @@ struct ImGuiIO //------------------------------------------------------------------ ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. - ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by back-end (imgui_impl_xxx files or custom back-end) to communicate features supported by the back-end. + ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by backend (imgui_impl_xxx files or custom backend) to communicate features supported by the backend. ImVec2 DisplaySize; // // Main display size, in pixels. float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. @@ -1522,24 +1526,24 @@ struct ImGuiIO ImVec2 DisplayFramebufferScale; // = (1, 1) // For retina display or other situations where window coordinates are different from framebuffer coordinates. This generally ends up in ImDrawData::FramebufferScale. // Miscellaneous options - bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by back-end implementations. + bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by backend implementations. bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl (was called io.OptMacOSXBehaviors prior to 1.63) bool ConfigInputTextCursorBlink; // = true // Set to false to disable blinking cursor, for users who consider it distracting. (was called: io.OptCursorBlink prior to 1.63) bool ConfigWindowsResizeFromEdges; // = true // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag) bool ConfigWindowsMoveFromTitleBarOnly; // = false // [BETA] Set to true to only allow moving windows when clicked+dragged from the title bar. Windows without a title bar are not affected. - float ConfigWindowsMemoryCompactTimer;// = 60.0f // [BETA] Compact window memory usage when unused. Set to -1.0f to disable. + float ConfigMemoryCompactTimer; // = 60.0f // [BETA] Free transient windows/tables memory buffers when unused for given amount of time. Set to -1.0f to disable. //------------------------------------------------------------------ // Platform Functions - // (the imgui_impl_xxxx back-end files are setting those up for you) + // (the imgui_impl_xxxx backend files are setting those up for you) //------------------------------------------------------------------ - // Optional: Platform/Renderer back-end name (informational only! will be displayed in About Window) + User data for back-end/wrappers to store their own stuff. + // Optional: Platform/Renderer backend name (informational only! will be displayed in About Window) + User data for backend/wrappers to store their own stuff. const char* BackendPlatformName; // = NULL const char* BackendRendererName; // = NULL - void* BackendPlatformUserData; // = NULL // User data for platform back-end - void* BackendRendererUserData; // = NULL // User data for renderer back-end - void* BackendLanguageUserData; // = NULL // User data for non C++ programming language back-end + void* BackendPlatformUserData; // = NULL // User data for platform backend + void* BackendRendererUserData; // = NULL // User data for renderer backend + void* BackendLanguageUserData; // = NULL // User data for non C++ programming language backend // Optional: Access OS clipboard // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) @@ -1552,15 +1556,6 @@ struct ImGuiIO void (*ImeSetInputScreenPosFn)(int x, int y); void* ImeWindowHandle; // = NULL // (Windows) Set this to your HWND to get automatic IME cursor positioning. -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - // [OBSOLETE since 1.60+] Rendering function, will be automatically called in Render(). Please call your rendering function yourself now! - // You can obtain the ImDrawData* by calling ImGui::GetDrawData() after Render(). See example applications if you are unsure of how to implement this. - void (*RenderDrawListsFn)(ImDrawData* data); -#else - // This is only here to keep ImGuiIO the same size/layout, so that IMGUI_DISABLE_OBSOLETE_FUNCTIONS can exceptionally be used outside of imconfig.h. - void* RenderDrawListsFnUnused; -#endif - //------------------------------------------------------------------ // Input - Fill before calling NewFrame() //------------------------------------------------------------------ @@ -1568,7 +1563,7 @@ struct ImGuiIO ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.) bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. - float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all back-ends. + float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all backends. bool KeyCtrl; // Keyboard modifier pressed: Control bool KeyShift; // Keyboard modifier pressed: Shift bool KeyAlt; // Keyboard modifier pressed: Alt @@ -1591,7 +1586,7 @@ struct ImGuiIO bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). - bool WantSetMousePos; // MousePos has been altered, back-end should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. + bool WantSetMousePos; // MousePos has been altered, backend should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). @@ -1626,7 +1621,7 @@ struct ImGuiIO float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16 - ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform back-end). Fill using AddInputCharacter() helper. + ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. IMGUI_API ImGuiIO(); }; @@ -1713,7 +1708,9 @@ struct ImGuiPayload #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { - // OBSOLETED in 1.78 (from August 2020) + // OBSOLETED in 1.79 (from August 2020) + static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! + // OBSOLETED in 1.78 (from June 2020) // Old drag/sliders functions that took a 'float power = 1.0' argument instead of flags. // For shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power); @@ -1729,9 +1726,8 @@ namespace ImGui static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } // OBSOLETED in 1.77 (from June 2020) - static inline bool OpenPopupOnItemClick(const char* str_id = NULL, ImGuiMouseButton mb = 1) { return OpenPopupContextItem(str_id, mb); } // Passing a mouse button to ImGuiPopupFlags is legal static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } - // OBSOLETED in 1.72 (from July 2019) + // OBSOLETED in 1.72 (from April 2019) static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } // OBSOLETED in 1.71 (from June 2019) static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } @@ -1743,14 +1739,6 @@ namespace ImGui static inline void SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); } // OBSOLETED in 1.63 (between Aug 2018 and Sept 2018) static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } - // OBSOLETED in 1.61 (between Apr 2018 and Aug 2018) - IMGUI_API bool InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags = 0); // Use the 'const char* format' version instead of 'decimal_precision'! - IMGUI_API bool InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags = 0); - IMGUI_API bool InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags = 0); - IMGUI_API bool InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags = 0); - // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) - static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } - static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } } typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETED in 1.63 (from Aug 2018): made the names consistent typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; @@ -1876,37 +1864,45 @@ struct ImGuiStorage }; // Helper: Manually clip large list of items. -// If you are submitting lots of evenly spaced items and you have a random access to the list, you can perform coarse clipping based on visibility to save yourself from processing those items at all. +// If you are submitting lots of evenly spaced items and you have a random access to the list, you can perform coarse +// clipping based on visibility to save yourself from processing those items at all. // The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped. -// ImGui already clip items based on their bounds but it needs to measure text size to do so. Coarse clipping before submission makes this cost and your own data fetching/submission cost null. +// (Dear ImGui already clip items based on their bounds but it needs to measure text size to do so, whereas manual coarse clipping before submission makes this cost and your own data fetching/submission cost almost null) // Usage: -// ImGuiListClipper clipper(1000); // we have 1000 elements, evenly spaced. -// while (clipper.Step()) -// for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) -// ImGui::Text("line number %d", i); -// - Step 0: the clipper let you process the first element, regardless of it being visible or not, so we can measure the element height (step skipped if we passed a known height as second arg to constructor). -// - Step 1: the clipper infer height from first element, calculate the actual range of elements to display, and position the cursor before the first element. -// - (Step 2: empty step only required if an explicit items_height was passed to constructor or Begin() and user call Step(). Does nothing and switch to Step 3.) -// - Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), advance the cursor to the end of the list and then returns 'false' to end the loop. +// ImGuiListClipper clipper; +// clipper.Begin(1000); // We have 1000 elements, evenly spaced. +// while (clipper.Step()) +// for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) +// ImGui::Text("line number %d", i); +// Generally what happens is: +// - Clipper lets you process the first element (DisplayStart = 0, DisplayEnd = 1) regardless of it being visible or not. +// - User code submit one element. +// - Clipper can measure the height of the first element +// - Clipper calculate the actual range of elements to display based on the current clipping rectangle, position the cursor before the first visible element. +// - User code submit visible elements. struct ImGuiListClipper { - int DisplayStart, DisplayEnd; - int ItemsCount; + int DisplayStart; + int DisplayEnd; // [Internal] + int ItemsCount; int StepNo; float ItemsHeight; float StartPosY; - // items_count: Use -1 to ignore (you can call Begin later). Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step). - // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). - // If you don't specify an items_height, you NEED to call Step(). If you specify items_height you may call the old Begin()/End() api directly, but prefer calling Step(). - ImGuiListClipper(int items_count = -1, float items_height = -1.0f) { Begin(items_count, items_height); } // NB: Begin() initialize every fields (as we allow user to call Begin/End multiple times on a same instance if they want). - ~ImGuiListClipper() { IM_ASSERT(ItemsCount == -1); } // Assert if user forgot to call End() or Step() until false. + IMGUI_API ImGuiListClipper(); + IMGUI_API ~ImGuiListClipper(); - IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. + // items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step) + // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). IMGUI_API void Begin(int items_count, float items_height = -1.0f); // Automatically called by constructor if you passed 'items_count' or by Step() in Step 1. IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. + IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79] +#endif }; // Helpers macros to generate 32-bit encoded colors @@ -1965,13 +1961,13 @@ struct ImColor // A) Change your GPU render state, // B) render a complex 3D scene inside a UI element without an intermediate texture/render target, etc. // The expected behavior from your rendering function is 'if (cmd.UserCallback != NULL) { cmd.UserCallback(parent_list, cmd); } else { RenderTriangles() }' -// If you want to override the signature of ImDrawCallback, you can simply use e.g. '#define ImDrawCallback MyDrawCallback' (in imconfig.h) + update rendering back-end accordingly. +// If you want to override the signature of ImDrawCallback, you can simply use e.g. '#define ImDrawCallback MyDrawCallback' (in imconfig.h) + update rendering backend accordingly. #ifndef ImDrawCallback typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* cmd); #endif -// Special Draw callback value to request renderer back-end to reset the graphics/render state. -// The renderer back-end needs to handle this special value, otherwise it will crash trying to call a function at this address. +// Special Draw callback value to request renderer backend to reset the graphics/render state. +// The renderer backend needs to handle this special value, otherwise it will crash trying to call a function at this address. // This is useful for example if you submitted callbacks which you know have altered the render state and you want it to be restored. // It is not done by default because they are many perfectly useful way of altering render state for imgui contents (e.g. changing shader/blending settings before an Image call). #define ImDrawCallback_ResetRenderState (ImDrawCallback)(-1) @@ -1979,7 +1975,7 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c // Typically, 1 command = 1 GPU draw call (unless command is a callback) // - VtxOffset/IdxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled, // those fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. -// Pre-1.71 back-ends will typically ignore the VtxOffset/IdxOffset fields. +// Pre-1.71 backends will typically ignore the VtxOffset/IdxOffset fields. // - The ClipRect/TextureId/VtxOffset fields must be contiguous as we memcmp() them together (this is asserted for). struct ImDrawCmd { @@ -1995,7 +1991,7 @@ struct ImDrawCmd }; // Vertex index, default to 16-bit -// To allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer back-end (recommended). +// To allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer backend (recommended). // To use 32-bit indices: override with '#define ImDrawIdx unsigned int' in imconfig.h. #ifndef ImDrawIdx typedef unsigned short ImDrawIdx; @@ -2017,7 +2013,15 @@ struct ImDrawVert IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT; #endif -// For use by ImDrawListSplitter. +// [Internal] For use by ImDrawList +struct ImDrawCmdHeader +{ + ImVec4 ClipRect; + ImTextureID TextureId; + unsigned int VtxOffset; +}; + +// [Internal] For use by ImDrawListSplitter struct ImDrawChannel { ImVector _CmdBuffer; @@ -2032,7 +2036,7 @@ struct ImDrawListSplitter int _Count; // Number of active channels (1+) ImVector _Channels; // Draw channels (not resized down so _Count might be < Channels.Size) - inline ImDrawListSplitter() { Clear(); } + inline ImDrawListSplitter() { memset(this, 0, sizeof(*this)); } inline ~ImDrawListSplitter() { ClearFreeMemory(); } inline void Clear() { _Current = 0; _Count = 1; } // Do not clear Channels[] so our allocations are reused next frame IMGUI_API void ClearFreeMemory(); @@ -2061,7 +2065,7 @@ enum ImDrawListFlags_ { ImDrawListFlags_None = 0, ImDrawListFlags_AntiAliasedLines = 1 << 0, // Enable anti-aliased lines/borders (*2 the number of triangles for 1.0f wide line or lines thin enough to be drawn using textures, otherwise *3 the number of triangles) - ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require back-end to render with bilinear filtering. + ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require backend to render with bilinear filtering. ImDrawListFlags_AntiAliasedFill = 1 << 2, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles). ImDrawListFlags_AllowVtxOffset = 1 << 3 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. }; @@ -2083,19 +2087,19 @@ struct ImDrawList ImDrawListFlags Flags; // Flags, you may poke into these to adjust anti-aliasing settings per-primitive. // [Internal, used while building lists] + unsigned int _VtxCurrentIdx; // [Internal] generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. const ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) const char* _OwnerName; // Pointer to owner window's name for debugging - unsigned int _VtxCurrentIdx; // [Internal] Generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImVector _ClipRectStack; // [Internal] ImVector _TextureIdStack; // [Internal] ImVector _Path; // [Internal] current path building - ImDrawCmd _CmdHeader; // [Internal] Template of active commands. Fields should match those of CmdBuffer.back(). + ImDrawCmdHeader _CmdHeader; // [Internal] template of active commands. Fields should match those of CmdBuffer.back(). ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!) // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) - ImDrawList(const ImDrawListSharedData* shared_data) { _Data = shared_data; Flags = ImDrawListFlags_None; _VtxCurrentIdx = 0; _VtxWritePtr = NULL; _IdxWritePtr = NULL; _OwnerName = NULL; } + ImDrawList(const ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; } ~ImDrawList() { _ClearFreeMemory(); } IMGUI_API void PushClipRect(ImVec2 clip_rect_min, ImVec2 clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 9099116c..71fe735f 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (demo code) // Help: @@ -308,8 +308,8 @@ void ImGui::ShowDemoWindow(bool* p_open) // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details. - // e.g. Use 2/3 of the space for widgets and 1/3 for labels (default) - //ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.65f); + // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align) + //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets. ImGui::PushItemWidth(ImGui::GetFontSize() * -12); @@ -340,7 +340,7 @@ void ImGui::ShowDemoWindow(bool* p_open) } if (ImGui::BeginMenu("Tools")) { - ImGui::MenuItem("Metrics", NULL, &show_app_metrics); + ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics); ImGui::MenuItem("Style Editor", NULL, &show_app_style_editor); ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about); ImGui::EndMenu(); @@ -357,7 +357,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::BulletText("Sections below are demonstrating many aspects of the library."); ImGui::BulletText("The \"Examples\" menu above leads to more demo contents."); ImGui::BulletText("The \"Tools\" menu above gives access to: About Box, Style Editor,\n" - "and Metrics (general purpose Dear ImGui debugging tool)."); + "and Metrics/Debugger (general purpose Dear ImGui debugging tool)."); ImGui::Separator(); ImGui::Text("PROGRAMMER GUIDE:"); @@ -379,16 +379,16 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Configuration##2")) { - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); - ImGui::SameLine(); HelpMarker("Required back-end to feed in gamepad inputs in io.NavInputs[] and set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", &io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); + ImGui::SameLine(); HelpMarker("Enable keyboard controls."); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", &io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); + ImGui::SameLine(); HelpMarker("Enable gamepad controls. Require backend to set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); + ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", &io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); - ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NoMouse); - - // The "NoMouse" option above can get us stuck with a disable mouse! Provide an alternative way to fix it: + ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", &io.ConfigFlags, ImGuiConfigFlags_NoMouse); if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) { + // The "NoMouse" option can get us stuck with a disabled mouse! Let's provide an alternative way to fix it: if (fmodf((float)ImGui::GetTime(), 0.40f) < 0.20f) { ImGui::SameLine(); @@ -397,8 +397,8 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse; } - ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", (unsigned int*)&io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); - ImGui::SameLine(); HelpMarker("Instruct back-end to not alter mouse cursor shape and visibility."); + ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); + ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility."); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); ImGui::SameLine(); HelpMarker("Set to false to disable blinking cursor, for users who consider it distracting"); ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); @@ -414,15 +414,15 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Backend Flags")) { HelpMarker( - "Those flags are set by the back-ends (imgui_impl_xxx files) to specify their capabilities.\n" - "Here we expose then as read-only fields to avoid breaking interactions with your back-end."); + "Those flags are set by the backends (imgui_impl_xxx files) to specify their capabilities.\n" + "Here we expose then as read-only fields to avoid breaking interactions with your backend."); - // Make a local copy to avoid modifying actual back-end flags. + // Make a local copy to avoid modifying actual backend flags. ImGuiBackendFlags backend_flags = io.BackendFlags; - ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasGamepad); - ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasMouseCursors); - ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", (unsigned int*)&backend_flags, ImGuiBackendFlags_HasSetMousePos); - ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", (unsigned int*)&backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); + ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", &backend_flags, ImGuiBackendFlags_HasGamepad); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &backend_flags, ImGuiBackendFlags_HasMouseCursors); + ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &backend_flags, ImGuiBackendFlags_HasSetMousePos); + ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); ImGui::TreePop(); ImGui::Separator(); } @@ -476,6 +476,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ShowDemoWindowMisc(); // End of ShowDemoWindow() + ImGui::PopItemWidth(); ImGui::End(); } @@ -617,7 +618,7 @@ static void ShowDemoWindowWidgets() "Hold SHIFT/ALT for faster/slower edit.\n" "Double-click or CTRL+click to input value."); - ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%", ImGuiSliderFlags_ClampOnInput); + ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp); static float f1 = 1.00f, f2 = 0.0067f; ImGui::DragFloat("drag float", &f1, 0.005f); @@ -652,9 +653,9 @@ static void ShowDemoWindowWidgets() static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::ColorEdit3("color 1", col1); ImGui::SameLine(); HelpMarker( - "Click on the colored square to open a color picker.\n" + "Click on the color square to open a color picker.\n" "Click and hold to use drag and drop.\n" - "Right-click on the colored square to show options.\n" + "Right-click on the color square to show options.\n" "CTRL+click on individual component to input value.\n"); ImGui::ColorEdit4("color 2", col2); @@ -710,10 +711,10 @@ static void ShowDemoWindowWidgets() static ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth; static bool align_label_with_current_x_position = false; static bool test_drag_and_drop = false; - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnArrow); - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node."); - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", (unsigned int*)&base_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnArrow", &base_flags, ImGuiTreeNodeFlags_OpenOnArrow); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", &base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", &base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node."); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &base_flags, ImGuiTreeNodeFlags_SpanFullWidth); ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position); ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop); ImGui::Text("Hello!"); @@ -823,7 +824,7 @@ static void ShowDemoWindowWidgets() if (ImGui::TreeNode("Text")) { - if (ImGui::TreeNode("Colored Text")) + if (ImGui::TreeNode("Colorful Text")) { // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility. ImGui::TextColored(ImVec4(1.0f, 0.0f, 1.0f, 1.0f), "Pink"); @@ -902,8 +903,8 @@ static void ShowDemoWindowWidgets() // Below we are displaying the font texture because it is the only texture we have access to inside the demo! // Remember that ImTextureID is just storage for whatever you want it to be. It is essentially a value that - // will be passed to the rendering back-end via the ImDrawCmd structure. - // If you use one of the default imgui_impl_XXXX.cpp rendering back-end, they all have comments at the top + // will be passed to the rendering backend via the ImDrawCmd structure. + // If you use one of the default imgui_impl_XXXX.cpp rendering backend, they all have comments at the top // of their respective source file to specify what they expect to be stored in ImTextureID, for example: // - The imgui_impl_dx11.cpp renderer expect a 'ID3D11ShaderResourceView*' pointer // - The imgui_impl_opengl3.cpp renderer expect a GLuint OpenGL texture identifier, etc. @@ -970,11 +971,11 @@ static void ShowDemoWindowWidgets() { // Expose flags as checkbox for the demo static ImGuiComboFlags flags = 0; - ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", (unsigned int*)&flags, ImGuiComboFlags_PopupAlignLeft); + ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", &flags, ImGuiComboFlags_PopupAlignLeft); ImGui::SameLine(); HelpMarker("Only makes a difference if the popup is larger than the combo"); - if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", (unsigned int*)&flags, ImGuiComboFlags_NoArrowButton)) + if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", &flags, ImGuiComboFlags_NoArrowButton)) flags &= ~ImGuiComboFlags_NoPreview; // Clear the other flag, as we cannot combine both - if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", (unsigned int*)&flags, ImGuiComboFlags_NoPreview)) + if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", &flags, ImGuiComboFlags_NoPreview)) flags &= ~ImGuiComboFlags_NoArrowButton; // Clear the other flag, as we cannot combine both // Using the generic BeginCombo() API, you have full control over how to display the combo contents. @@ -1088,27 +1089,34 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("Grid")) { - static int selected[4 * 4] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; - for (int i = 0; i < 4 * 4; i++) - { - ImGui::PushID(i); - if (ImGui::Selectable("Sailor", selected[i] != 0, 0, ImVec2(50, 50))) + static char selected[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; + + // Add in a bit of silly fun... + const float time = (float)ImGui::GetTime(); + const bool winning_state = memchr(selected, 0, sizeof(selected)) == NULL; // If all cells are selected... + if (winning_state) + ImGui::PushStyleVar(ImGuiStyleVar_SelectableTextAlign, ImVec2(0.5f + 0.5f * cosf(time * 2.0f), 0.5f + 0.5f * sinf(time * 3.0f))); + + for (int y = 0; y < 4; y++) + for (int x = 0; x < 4; x++) { - // Toggle - selected[i] = !selected[i]; - - // Note: We _unnecessarily_ test for both x/y and i here only to silence some static analyzer. - // The second part of each test is unnecessary. - int x = i % 4; - int y = i / 4; - if (x > 0) { selected[i - 1] ^= 1; } - if (x < 3 && i < 15) { selected[i + 1] ^= 1; } - if (y > 0 && i > 3) { selected[i - 4] ^= 1; } - if (y < 3 && i < 12) { selected[i + 4] ^= 1; } + if (x > 0) + ImGui::SameLine(); + ImGui::PushID(y * 4 + x); + if (ImGui::Selectable("Sailor", selected[y][x] != 0, 0, ImVec2(50, 50))) + { + // Toggle clicked cell + toggle neighbors + selected[y][x] ^= 1; + if (x > 0) { selected[y][x - 1] ^= 1; } + if (x < 3) { selected[y][x + 1] ^= 1; } + if (y > 0) { selected[y - 1][x] ^= 1; } + if (y < 3) { selected[y + 1][x] ^= 1; } + } + ImGui::PopID(); } - if ((i % 4) < 3) ImGui::SameLine(); - ImGui::PopID(); - } + + if (winning_state) + ImGui::PopStyleVar(); ImGui::TreePop(); } if (ImGui::TreeNode("Alignment")) @@ -1158,9 +1166,9 @@ static void ShowDemoWindowWidgets() static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput; HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include in here)"); - ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", (unsigned int*)&flags, ImGuiInputTextFlags_ReadOnly); - ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", (unsigned int*)&flags, ImGuiInputTextFlags_AllowTabInput); - ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", (unsigned int*)&flags, ImGuiInputTextFlags_CtrlEnterForNewLine); + ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly); + ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", &flags, ImGuiInputTextFlags_AllowTabInput); + ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", &flags, ImGuiInputTextFlags_CtrlEnterForNewLine); ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags); ImGui::TreePop(); } @@ -1398,7 +1406,7 @@ static void ShowDemoWindowWidgets() ImGui::Text("Color widget:"); ImGui::SameLine(); HelpMarker( - "Click on the colored square to open a color picker.\n" + "Click on the color square to open a color picker.\n" "CTRL+click on individual component to input value.\n"); ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags); @@ -1558,13 +1566,13 @@ static void ShowDemoWindowWidgets() { // Demonstrate using advanced flags for DragXXX and SliderXXX functions. Note that the flags are the same! static ImGuiSliderFlags flags = ImGuiSliderFlags_None; - ImGui::CheckboxFlags("ImGuiSliderFlags_ClampOnInput", (unsigned int*)&flags, ImGuiSliderFlags_ClampOnInput); + ImGui::CheckboxFlags("ImGuiSliderFlags_AlwaysClamp", &flags, ImGuiSliderFlags_AlwaysClamp); ImGui::SameLine(); HelpMarker("Always clamp value to min/max bounds (if any) when input manually with CTRL+Click."); - ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic", (unsigned int*)&flags, ImGuiSliderFlags_Logarithmic); + ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic", &flags, ImGuiSliderFlags_Logarithmic); ImGui::SameLine(); HelpMarker("Enable logarithmic editing (more precision for small values)."); - ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", (unsigned int*)&flags, ImGuiSliderFlags_NoRoundToFormat); + ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", &flags, ImGuiSliderFlags_NoRoundToFormat); ImGui::SameLine(); HelpMarker("Disable rounding underlying value to match precision of the format string (e.g. %.3f values are rounded to those 3 digits)."); - ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput", (unsigned int*)&flags, ImGuiSliderFlags_NoInput); + ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput", &flags, ImGuiSliderFlags_NoInput); ImGui::SameLine(); HelpMarker("Disable CTRL+Click or Enter key allowing to input text directly into the widget."); // Drags @@ -1591,7 +1599,7 @@ static void ShowDemoWindowWidgets() { static float begin = 10, end = 90; static int begin_i = 100, end_i = 1000; - ImGui::DragFloatRange2("range float", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%", ImGuiSliderFlags_ClampOnInput); + ImGui::DragFloatRange2("range float", &begin, &end, 0.25f, 0.0f, 100.0f, "Min: %.1f %%", "Max: %.1f %%", ImGuiSliderFlags_AlwaysClamp); ImGui::DragIntRange2("range int", &begin_i, &end_i, 5, 0, 1000, "Min: %d units", "Max: %d units"); ImGui::DragIntRange2("range int (no bounds)", &begin_i, &end_i, 5, 0, 0, "Min: %d units", "Max: %d units"); ImGui::TreePop(); @@ -1818,7 +1826,7 @@ static void ShowDemoWindowWidgets() // They are using standardized payload strings IMGUI_PAYLOAD_TYPE_COLOR_3F and IMGUI_PAYLOAD_TYPE_COLOR_4F // to allow your own widgets to use colors in their drag and drop interaction. // Also see 'Demo->Widgets->Color/Picker Widgets->Palette' demo. - HelpMarker("You can drag from the colored squares."); + HelpMarker("You can drag from the color squares."); static float col1[3] = { 1.0f, 0.0f, 0.2f }; static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::ColorEdit3("color 1", col1); @@ -2163,34 +2171,69 @@ static void ShowDemoWindowLayout() // e.g. Using '20.0f * GetFontSize()' as width instead of '200.0f', etc. static float f = 0.0f; + static bool show_indented_items = true; + ImGui::Checkbox("Show indented items", &show_indented_items); + ImGui::Text("SetNextItemWidth/PushItemWidth(100)"); ImGui::SameLine(); HelpMarker("Fixed width."); - ImGui::SetNextItemWidth(100); - ImGui::DragFloat("float##1", &f); + ImGui::PushItemWidth(100); + ImGui::DragFloat("float##1b", &f); + if (show_indented_items) + { + ImGui::Indent(); + ImGui::DragFloat("float (indented)##1b", &f); + ImGui::Unindent(); + } + ImGui::PopItemWidth(); - ImGui::Text("SetNextItemWidth/PushItemWidth(GetWindowWidth() * 0.5f)"); - ImGui::SameLine(); HelpMarker("Half of window width."); - ImGui::SetNextItemWidth(ImGui::GetWindowWidth() * 0.5f); - ImGui::DragFloat("float##2", &f); + ImGui::Text("SetNextItemWidth/PushItemWidth(-100)"); + ImGui::SameLine(); HelpMarker("Align to right edge minus 100"); + ImGui::PushItemWidth(-100); + ImGui::DragFloat("float##2a", &f); + if (show_indented_items) + { + ImGui::Indent(); + ImGui::DragFloat("float (indented)##2b", &f); + ImGui::Unindent(); + } + ImGui::PopItemWidth(); ImGui::Text("SetNextItemWidth/PushItemWidth(GetContentRegionAvail().x * 0.5f)"); ImGui::SameLine(); HelpMarker("Half of available width.\n(~ right-cursor_pos)\n(works within a column set)"); - ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x * 0.5f); - ImGui::DragFloat("float##3", &f); + ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x * 0.5f); + ImGui::DragFloat("float##3a", &f); + if (show_indented_items) + { + ImGui::Indent(); + ImGui::DragFloat("float (indented)##3b", &f); + ImGui::Unindent(); + } + ImGui::PopItemWidth(); - ImGui::Text("SetNextItemWidth/PushItemWidth(-100)"); - ImGui::SameLine(); HelpMarker("Align to right edge minus 100"); - ImGui::SetNextItemWidth(-100); - ImGui::DragFloat("float##4", &f); + ImGui::Text("SetNextItemWidth/PushItemWidth(-GetContentRegionAvail().x * 0.5f)"); + ImGui::SameLine(); HelpMarker("Align to right edge minus half"); + ImGui::PushItemWidth(-ImGui::GetContentRegionAvail().x * 0.5f); + ImGui::DragFloat("float##4a", &f); + if (show_indented_items) + { + ImGui::Indent(); + ImGui::DragFloat("float (indented)##4b", &f); + ImGui::Unindent(); + } + ImGui::PopItemWidth(); // Demonstrate using PushItemWidth to surround three items. // Calling SetNextItemWidth() before each of them would have the same effect. - ImGui::Text("SetNextItemWidth/PushItemWidth(-1)"); + ImGui::Text("SetNextItemWidth/PushItemWidth(-FLT_MIN)"); ImGui::SameLine(); HelpMarker("Align to right edge"); - ImGui::PushItemWidth(-1); + ImGui::PushItemWidth(-FLT_MIN); ImGui::DragFloat("##float5a", &f); - ImGui::DragFloat("##float5b", &f); - ImGui::DragFloat("##float5c", &f); + if (show_indented_items) + { + ImGui::Indent(); + ImGui::DragFloat("float (indented)##5b", &f); + ImGui::Unindent(); + } ImGui::PopItemWidth(); ImGui::TreePop(); @@ -2317,15 +2360,15 @@ static void ShowDemoWindowLayout() { // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable; - ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_Reorderable); - ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); - ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); - ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); + ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", &tab_bar_flags, ImGuiTabBarFlags_Reorderable); + ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", &tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); + ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); + ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_; - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", (unsigned int*)&tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); // Tab Bar @@ -2354,6 +2397,72 @@ static void ShowDemoWindowLayout() ImGui::Separator(); ImGui::TreePop(); } + + if (ImGui::TreeNode("TabItemButton & Leading/Trailing flags")) + { + static ImVector active_tabs; + static int next_tab_id = 0; + if (next_tab_id == 0) // Initialize with some default tabs + for (int i = 0; i < 3; i++) + active_tabs.push_back(next_tab_id++); + + // TabItemButton() and Leading/Trailing flags are distinct features which we will demo together. + // (It is possible to submit regular tabs with Leading/Trailing flags, or TabItemButton tabs without Leading/Trailing flags... + // but they tend to make more sense together) + static bool show_leading_button = true; + static bool show_trailing_button = true; + ImGui::Checkbox("Show Leading TabItemButton()", &show_leading_button); + ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button); + + // Expose some other flags which are useful to showcase how they interact with Leading/Trailing tabs + static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown; + ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); + + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + // Demo a Leading TabItemButton(): click the "?" button to open a menu + if (show_leading_button) + if (ImGui::TabItemButton("?", ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_NoTooltip)) + ImGui::OpenPopup("MyHelpMenu"); + if (ImGui::BeginPopup("MyHelpMenu")) + { + ImGui::Selectable("Hello!"); + ImGui::EndPopup(); + } + + // Demo Trailing Tabs: click the "+" button to add a new tab (in your app you may want to use a font icon instead of the "+") + // Note that we submit it before the regular tabs, but because of the ImGuiTabItemFlags_Trailing flag it will always appear at the end. + if (show_trailing_button) + if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) + active_tabs.push_back(next_tab_id++); // Add new tab + + // Submit our regular tabs + for (int n = 0; n < active_tabs.Size; ) + { + bool open = true; + char name[16]; + snprintf(name, IM_ARRAYSIZE(name), "%04d", active_tabs[n]); + if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None)) + { + ImGui::Text("This is the %s tab!", name); + ImGui::EndTabItem(); + } + + if (!open) + active_tabs.erase(active_tabs.Data + n); + else + n++; + } + + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } ImGui::TreePop(); } @@ -2977,11 +3086,11 @@ static void ShowDemoWindowPopups() ImGui::EndPopup(); } - // We can also use OpenPopupContextItem() which is the same as BeginPopupContextItem() but without the + // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) // will toggle the visibility of the popup above. ImGui::Text("(You can also right-click me to open the same popup as above.)"); - ImGui::OpenPopupContextItem("item context menu", 1); + ImGui::OpenPopupOnItemClick("item context menu", 1); // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). // BeginPopupContextItem() will use the last item ID as the popup ID. @@ -3274,7 +3383,8 @@ static void ShowDemoWindowColumns() ImGui::BeginChild("##ScrollingRegion", child_size, false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::Columns(10); int ITEMS_COUNT = 2000; - ImGuiListClipper clipper(ITEMS_COUNT); // Also demonstrate using the clipper for large list + ImGuiListClipper clipper; // Also demonstrate using the clipper for large list + clipper.Begin(ITEMS_COUNT); while (clipper.Step()) { for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) @@ -3495,7 +3605,7 @@ static void ShowDemoWindowMisc() char label[32]; sprintf(label, "Mouse cursor %d: %s", i, mouse_cursors_names[i]); ImGui::Bullet(); ImGui::Selectable(label, false); - if (ImGui::IsItemHovered() || ImGui::IsItemFocused()) + if (ImGui::IsItemHovered()) ImGui::SetMouseCursor(i); } ImGui::TreePop(); @@ -3612,7 +3722,7 @@ void ImGui::ShowAboutWindow(bool* p_open) if (io.ConfigInputTextCursorBlink) ImGui::Text("io.ConfigInputTextCursorBlink"); if (io.ConfigWindowsResizeFromEdges) ImGui::Text("io.ConfigWindowsResizeFromEdges"); if (io.ConfigWindowsMoveFromTitleBarOnly) ImGui::Text("io.ConfigWindowsMoveFromTitleBarOnly"); - if (io.ConfigWindowsMemoryCompactTimer >= 0.0f) ImGui::Text("io.ConfigWindowsMemoryCompactTimer = %.1ff", io.ConfigWindowsMemoryCompactTimer); + if (io.ConfigMemoryCompactTimer >= 0.0f) ImGui::Text("io.ConfigMemoryCompactTimer = %.1f", io.ConfigMemoryCompactTimer); ImGui::Text("io.BackendFlags: 0x%08X", io.BackendFlags); if (io.BackendFlags & ImGuiBackendFlags_HasGamepad) ImGui::Text(" HasGamepad"); if (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) ImGui::Text(" HasMouseCursors"); @@ -3902,7 +4012,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (ImGui::RadioButton("Both", alpha_flags == ImGuiColorEditFlags_AlphaPreviewHalf)) { alpha_flags = ImGuiColorEditFlags_AlphaPreviewHalf; } ImGui::SameLine(); HelpMarker( "In the color list:\n" - "Left-click on colored square to open color picker,\n" + "Left-click on color square to open color picker,\n" "Right-click to open edit options menu."); ImGui::BeginChild("##colors", ImVec2(0, 0), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); @@ -3963,9 +4073,9 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n" "Using those settings here will give you poor quality results."); static float window_scale = 1.0f; - if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_ClampOnInput)) // Scale only this window + if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp)) // Scale only this window ImGui::SetWindowFontScale(window_scale); - ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_ClampOnInput); // Scale everything + ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp); // Scale everything ImGui::PopItemWidth(); ImGui::EndTabItem(); @@ -3979,7 +4089,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::Checkbox("Anti-aliased lines use texture", &style.AntiAliasedLinesUseTex); ImGui::SameLine(); - HelpMarker("Faster lines using texture data. Require back-end to render with bilinear filtering (not point/nearest filtering)."); + HelpMarker("Faster lines using texture data. Require backend to render with bilinear filtering (not point/nearest filtering)."); ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); ImGui::PushItemWidth(100); @@ -4264,7 +4374,8 @@ struct ExampleAppConsole // To use the clipper we can replace your standard loop: // for (int i = 0; i < Items.Size; i++) // With: - // ImGuiListClipper clipper(Items.Size); + // ImGuiListClipper clipper; + // clipper.Begin(Items.Size); // while (clipper.Step()) // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) // - That your items are evenly spaced (same height) @@ -4831,7 +4942,8 @@ static void ShowExampleAppLongText(bool* p_open) { // Multiple calls to Text(), manually coarsely clipped - demonstrate how to use the ImGuiListClipper helper. ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); - ImGuiListClipper clipper(lines); + ImGuiListClipper clipper; + clipper.Begin(lines); while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) ImGui::Text("%i The quick brown fox jumps over the lazy dog", i); @@ -5178,7 +5290,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) // Context menu (under default mouse threshold) ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right); if (opt_enable_context_menu && ImGui::IsMouseReleased(ImGuiMouseButton_Right) && drag_delta.x == 0.0f && drag_delta.y == 0.0f) - ImGui::OpenPopupContextItem("context"); + ImGui::OpenPopupOnItemClick("context"); if (ImGui::BeginPopup("context")) { if (adding_line) diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 3f666698..306c296e 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (drawing and font code) /* @@ -60,6 +60,9 @@ Index of this file: #if __has_warning("-Wunknown-warning-option") #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif +#if __has_warning("-Walloca") +#pragma clang diagnostic ignored "-Walloca" // warning: use of function '__builtin_alloca' is discouraged +#endif #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok. @@ -344,21 +347,12 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) ImDrawListSharedData::ImDrawListSharedData() { - Font = NULL; - FontSize = 0.0f; - CurveTessellationTol = 0.0f; - CircleSegmentMaxError = 0.0f; - ClipRectFullscreen = ImVec4(-8192.0f, -8192.0f, +8192.0f, +8192.0f); - InitialFlags = ImDrawListFlags_None; - - // Lookup tables + memset(this, 0, sizeof(*this)); for (int i = 0; i < IM_ARRAYSIZE(ArcFastVtx); i++) { const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx); ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); } - memset(CircleSegmentCounts, 0, sizeof(CircleSegmentCounts)); // This will be set by SetCircleSegmentMaxError() - TexUvLines = NULL; } void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) @@ -1426,10 +1420,14 @@ void ImDrawListSplitter::ClearFreeMemory() void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) { + IM_UNUSED(draw_list); IM_ASSERT(_Current == 0 && _Count <= 1 && "Nested channel splitting is not supported. Please use separate instances of ImDrawListSplitter."); int old_channels_count = _Channels.Size; if (old_channels_count < channels_count) + { + _Channels.reserve(channels_count); // Avoid over reserving since this is likely to stay stable _Channels.resize(channels_count); + } _Count = channels_count; // Channels[] (24/32 bytes each) hold storage that we'll swap with draw_list->_CmdBuffer/_IdxBuffer @@ -1447,12 +1445,6 @@ void ImDrawListSplitter::Split(ImDrawList* draw_list, int channels_count) _Channels[i]._CmdBuffer.resize(0); _Channels[i]._IdxBuffer.resize(0); } - if (_Channels[i]._CmdBuffer.Size == 0) - { - ImDrawCmd draw_cmd; - ImDrawCmd_HeaderCopy(&draw_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset - _Channels[i]._CmdBuffer.push_back(draw_cmd); - } } } @@ -1542,8 +1534,10 @@ void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) draw_list->_IdxWritePtr = draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size; // If current command is used with different settings we need to add a new command - ImDrawCmd* curr_cmd = &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; - if (curr_cmd->ElemCount == 0) + ImDrawCmd* curr_cmd = (draw_list->CmdBuffer.Size == 0) ? NULL : &draw_list->CmdBuffer.Data[draw_list->CmdBuffer.Size - 1]; + if (curr_cmd == NULL) + draw_list->AddDrawCmd(); + else if (curr_cmd->ElemCount == 0) ImDrawCmd_HeaderCopy(curr_cmd, &draw_list->_CmdHeader); // Copy ClipRect, TextureId, VtxOffset else if (ImDrawCmd_HeaderCompare(curr_cmd, &draw_list->_CmdHeader) != 0) draw_list->AddDrawCmd(); diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index 5ea7be08..74a266e1 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -1,4 +1,4 @@ -// dear imgui: Platform Binding for GLFW +// dear imgui: Platform Backend for GLFW // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) // (Requires: GLFW 3.1+) @@ -9,9 +9,9 @@ // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). -// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. -// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. -// https://github.com/ocornut/imgui +// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. +// Read online: https://github.com/ocornut/imgui/tree/master/docs // CHANGELOG // (minor and older changes stripped away, please see git history for details) @@ -143,7 +143,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw g_Window = window; g_Time = 0.0; - // Setup back-end capabilities flags + // Setup backend capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) @@ -345,7 +345,7 @@ static void ImGui_ImplGlfw_UpdateGamepads() void ImGui_ImplGlfw_NewFrame() { ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer back-end. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."); + IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."); // Setup display size (every frame to accommodate for window resizing) int w, h; diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h index f62f44f3..6abb4056 100644 --- a/imgui/imgui_impl_glfw.h +++ b/imgui/imgui_impl_glfw.h @@ -1,4 +1,4 @@ -// dear imgui: Platform Binding for GLFW +// dear imgui: Platform Backend for GLFW // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) @@ -8,9 +8,9 @@ // [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). -// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. -// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. -// https://github.com/ocornut/imgui +// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. +// Read online: https://github.com/ocornut/imgui/tree/master/docs // About GLSL version: // The 'glsl_version' initialization parameter defaults to "#version 150" if NULL. diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index 8a1f5873..c70cfd6c 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -1,18 +1,20 @@ -// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline +// dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline // - Desktop GL: 2.x 3.x 4.x // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) -// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) +// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) // Implemented features: // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. -// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. -// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. -// https://github.com/ocornut/imgui +// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. +// Read online: https://github.com/ocornut/imgui/tree/master/docs // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2020-10-23: OpenGL: Save and restore current GL_PRIMITIVE_RESTART state. +// 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) // 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. // 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. // 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. @@ -133,6 +135,11 @@ using namespace gl; #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER #endif +// Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART +#endif + // OpenGL Data static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings. @@ -147,15 +154,22 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { // Query for GL version (e.g. 320 for GL 3.2) #if !defined(IMGUI_IMPL_OPENGL_ES2) - GLint major, minor; + GLint major = 0; + GLint minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); + if (major == 0 && minor == 0) + { + // Query GL_VERSION in desktop GL 2.x, the string will start with "." + const char* gl_version = (const char*)glGetString(GL_VERSION); + sscanf(gl_version, "%d.%d", &major, &minor); + } g_GlVersion = (GLuint)(major * 100 + minor * 10); #else g_GlVersion = 200; // GLES 2 #endif - // Setup back-end capabilities flags + // Setup backend capabilities flags ImGuiIO& io = ImGui::GetIO(); io.BackendRendererName = "imgui_impl_opengl3"; #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET @@ -236,6 +250,10 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART + if (g_GlVersion >= 310) + glDisable(GL_PRIMITIVE_RESTART); +#endif #ifdef GL_POLYGON_MODE glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif @@ -289,8 +307,8 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid } // OpenGL3 Render function. -// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) -// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly, in order to be able to run within any OpenGL engine that doesn't do so. +// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL state explicitly. +// This is in order to be able to run within an OpenGL engine that doesn't do so. void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) { // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates) @@ -326,6 +344,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART + GLboolean last_enable_primitive_restart = (g_GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; +#endif // Setup desired GL state // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) @@ -411,6 +432,10 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART + if (g_GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } +#endif + #ifdef GL_POLYGON_MODE glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); #endif diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index 14eb2842..8c0126d8 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -1,15 +1,15 @@ -// dear imgui: Renderer for modern OpenGL with shaders / programmatic pipeline +// dear imgui: Renderer Backend for modern OpenGL with shaders / programmatic pipeline // - Desktop GL: 2.x 3.x 4.x // - Embedded GL: ES 2.0 (WebGL 1.0), ES 3.0 (WebGL 2.0) -// This needs to be used along with a Platform Binding (e.g. GLFW, SDL, Win32, custom..) +// This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) // Implemented features: // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. -// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this. -// If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp. -// https://github.com/ocornut/imgui +// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. +// Read online: https://github.com/ocornut/imgui/tree/master/docs // About Desktop OpenGL function loaders: // Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index ec37b38b..f3ab30e7 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -19,16 +19,18 @@ Index of this file: // [SECTION] ImDrawList support // [SECTION] Widgets support: flags, enums, data structures // [SECTION] Columns support -// [SECTION] Settings support // [SECTION] Multi-select support // [SECTION] Docking support // [SECTION] Viewport support +// [SECTION] Settings support +// [SECTION] Metrics, Debug +// [SECTION] Generic context hooks // [SECTION] ImGuiContext (main imgui context) // [SECTION] ImGuiWindowTempData, ImGuiWindow // [SECTION] Tab bar, Tab item support // [SECTION] Table support // [SECTION] Internal API -// [SECTION] Test Engine Hooks (imgui_test_engine) +// [SECTION] Test Engine specific hooks (imgui_test_engine) */ @@ -90,19 +92,22 @@ struct ImRect; // An axis-aligned rectangle (2 points) struct ImDrawDataBuilder; // Helper to build a ImDrawData instance struct ImDrawListSharedData; // Data shared between all ImDrawList instances struct ImGuiColorMod; // Stacked color modifier, backup of modified data so we can restore it -struct ImGuiColumnData; // Storage data for a single column -struct ImGuiColumns; // Storage data for a columns set struct ImGuiContext; // Main Dear ImGui context +struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiLastItemDataBackup; // Backup and restore IsItemHovered() internal data struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiNavMoveResult; // Result of a gamepad/keyboard directional navigation move query result +struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions +struct ImGuiOldColumnData; // Storage data for a single column for legacy Columns() api +struct ImGuiOldColumns; // Storage data for a columns set for legacy Columns() api struct ImGuiPopupData; // Storage for current popup stack struct ImGuiSettingsHandler; // Storage for one type registered in the .ini file +struct ImGuiStackSizes; // Storage of stack sizes for debugging/asserting struct ImGuiStyleMod; // Stacked style modifier, backup of modified data so we can restore it struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) @@ -112,10 +117,9 @@ struct ImGuiWindowSettings; // Storage for a window .ini settings (we ke // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical -typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for ButtonEx(), ButtonBehavior() -typedef int ImGuiColumnsFlags; // -> enum ImGuiColumnsFlags_ // Flags: BeginColumns() typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags +typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() typedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // Flags: for GetNavInputAmount2d() typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests @@ -125,6 +129,8 @@ typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // F typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() +typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...); + //----------------------------------------------------------------------------- // [SECTION] Context pointer // See implementation of this variable in imgui.cpp for comments and details. @@ -450,15 +456,15 @@ struct IMGUI_API ImRect }; // Helper: ImBitArray -inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } -inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } -inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } -inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) +inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } +inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } +inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } +inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) { while (n <= n2) { int a_mod = (n & 31); - int b_mod = ((n2 >= n + 31) ? 31 : (n2 & 31)) + 1; + int b_mod = (n2 > (n | 31) ? 31 : (n2 & 31)) + 1; ImU32 mask = (ImU32)(((ImU64)1 << b_mod) - 1) & ~(ImU32)(((ImU64)1 << a_mod) - 1); arr[n >> 5] |= mask; n = (n + 32) & ~31; @@ -823,6 +829,7 @@ struct ImGuiStyleMod // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiGroupData { + ImGuiID WindowID; ImVec2 BackupCursorPos; ImVec2 BackupCursorMaxPos; ImVec1 BackupIndent; @@ -841,7 +848,7 @@ struct IMGUI_API ImGuiMenuColumns float Width, NextWidth; float Pos[3], NextWidths[3]; - ImGuiMenuColumns(); + ImGuiMenuColumns() { memset(this, 0, sizeof(*this)); } void Update(int count, float spacing, bool clear); float DeclColumns(float w0, float w1, float w2); float CalcExtraSpace(float avail_w) const; @@ -894,7 +901,7 @@ struct ImGuiPopupData ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup - ImGuiPopupData() { PopupId = 0; Window = SourceWindow = NULL; OpenFrameCount = -1; OpenParentId = 0; } + ImGuiPopupData() { memset(this, 0, sizeof(*this)); OpenFrameCount = -1; } }; struct ImGuiNavMoveResult @@ -985,31 +992,41 @@ struct ImGuiPtrOrIndex // [SECTION] Columns support //----------------------------------------------------------------------------- -enum ImGuiColumnsFlags_ +// Flags for internal's BeginColumns(). Prefix using BeginTable() nowadays! +enum ImGuiOldColumnFlags_ { - // Default: 0 - ImGuiColumnsFlags_None = 0, - ImGuiColumnsFlags_NoBorder = 1 << 0, // Disable column dividers - ImGuiColumnsFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers - ImGuiColumnsFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns - ImGuiColumnsFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window - ImGuiColumnsFlags_GrowParentContentsSize= 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. + ImGuiOldColumnFlags_None = 0, + ImGuiOldColumnFlags_NoBorder = 1 << 0, // Disable column dividers + ImGuiOldColumnFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers + ImGuiOldColumnFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns + ImGuiOldColumnFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window + ImGuiOldColumnFlags_GrowParentContentsSize = 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. + + // Obsolete names (will be removed) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiColumnsFlags_None = ImGuiOldColumnFlags_None, + ImGuiColumnsFlags_NoBorder = ImGuiOldColumnFlags_NoBorder, + ImGuiColumnsFlags_NoResize = ImGuiOldColumnFlags_NoResize, + ImGuiColumnsFlags_NoPreserveWidths = ImGuiOldColumnFlags_NoPreserveWidths, + ImGuiColumnsFlags_NoForceWithinWindow = ImGuiOldColumnFlags_NoForceWithinWindow, + ImGuiColumnsFlags_GrowParentContentsSize = ImGuiOldColumnFlags_GrowParentContentsSize +#endif }; -struct ImGuiColumnData +struct ImGuiOldColumnData { float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) float OffsetNormBeforeResize; - ImGuiColumnsFlags Flags; // Not exposed + ImGuiOldColumnFlags Flags; // Not exposed ImRect ClipRect; - ImGuiColumnData() { OffsetNorm = OffsetNormBeforeResize = 0.0f; Flags = ImGuiColumnsFlags_None; } + ImGuiOldColumnData() { memset(this, 0, sizeof(*this)); } }; -struct ImGuiColumns +struct ImGuiOldColumns { ImGuiID ID; - ImGuiColumnsFlags Flags; + ImGuiOldColumnFlags Flags; bool IsFirstFrame; bool IsBeingResized; int Current; @@ -1021,24 +1038,10 @@ struct ImGuiColumns ImRect HostInitialClipRect; // Backup of ClipRect at the time of BeginColumns() ImRect HostBackupClipRect; // Backup of ClipRect during PushColumnsBackground()/PopColumnsBackground() ImRect HostBackupParentWorkRect;//Backup of WorkRect at the time of BeginColumns() - ImVector Columns; + ImVector Columns; ImDrawListSplitter Splitter; - ImGuiColumns() { Clear(); } - void Clear() - { - ID = 0; - Flags = ImGuiColumnsFlags_None; - IsFirstFrame = false; - IsBeingResized = false; - Current = 0; - Count = 1; - OffMinX = OffMaxX = 0.0f; - LineMinY = LineMaxY = 0.0f; - HostCursorPosY = 0.0f; - HostCursorMaxPosX = 0.0f; - Columns.clear(); - } + ImGuiOldColumns() { memset(this, 0, sizeof(*this)); } }; //----------------------------------------------------------------------------- @@ -1080,7 +1083,7 @@ struct ImGuiWindowSettings bool Collapsed; bool WantApply; // Set when loaded from .ini data (to enable merging/loading .ini data into an already running context) - ImGuiWindowSettings() { ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = WantApply = false; } + ImGuiWindowSettings() { memset(this, 0, sizeof(*this)); } char* GetName() { return (char*)(this + 1); } }; @@ -1099,6 +1102,64 @@ struct ImGuiSettingsHandler ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } }; +//----------------------------------------------------------------------------- +// [SECTION] Metrics, Debug +//----------------------------------------------------------------------------- + +struct ImGuiMetricsConfig +{ + bool ShowWindowsRects; + bool ShowWindowsBeginOrder; + bool ShowTablesRects; + bool ShowDrawCmdMesh; + bool ShowDrawCmdBoundingBoxes; + int ShowWindowsRectsType; + int ShowTablesRectsType; + + ImGuiMetricsConfig() + { + ShowWindowsRects = false; + ShowWindowsBeginOrder = false; + ShowTablesRects = false; + ShowDrawCmdMesh = true; + ShowDrawCmdBoundingBoxes = true; + ShowWindowsRectsType = -1; + ShowTablesRectsType = -1; + } +}; + +struct IMGUI_API ImGuiStackSizes +{ + short SizeOfIDStack; + short SizeOfColorStack; + short SizeOfStyleVarStack; + short SizeOfFontStack; + short SizeOfFocusScopeStack; + short SizeOfGroupStack; + short SizeOfBeginPopupStack; + + ImGuiStackSizes() { memset(this, 0, sizeof(*this)); } + void SetToCurrentState(); + void CompareWithCurrentState(); +}; + +//----------------------------------------------------------------------------- +// [SECTION] Generic context hooks +//----------------------------------------------------------------------------- + +typedef void (*ImGuiContextHookCallback)(ImGuiContext* ctx, ImGuiContextHook* hook); +enum ImGuiContextHookType { ImGuiContextHookType_NewFramePre, ImGuiContextHookType_NewFramePost, ImGuiContextHookType_EndFramePre, ImGuiContextHookType_EndFramePost, ImGuiContextHookType_RenderPre, ImGuiContextHookType_RenderPost, ImGuiContextHookType_Shutdown }; + +struct ImGuiContextHook +{ + ImGuiContextHookType Type; + ImGuiID Owner; + ImGuiContextHookCallback Callback; + void* UserData; + + ImGuiContextHook() { memset(this, 0, sizeof(*this)); } +}; + //----------------------------------------------------------------------------- // [SECTION] ImGuiContext (main imgui context) //----------------------------------------------------------------------------- @@ -1120,6 +1181,7 @@ struct ImGuiContext bool WithinFrameScope; // Set by NewFrame(), cleared by EndFrame() bool WithinFrameScopeWithImplicitWindow; // Set by NewFrame(), cleared by EndFrame() when the implicit debug window has been pushed bool WithinEndChild; // Set within EndChild() + bool GcCompactAll; // Request full GC bool TestEngineHookItems; // Will call test engine hooks: ImGuiTestEngineHook_ItemAdd(), ImGuiTestEngineHook_ItemInfo(), ImGuiTestEngineHook_Log() ImGuiID TestEngineHookIdInfo; // Will call test engine hooks: ImGuiTestEngineHook_IdInfo() from GetID() void* TestEngine; // Test engine user data @@ -1175,9 +1237,12 @@ struct ImGuiContext ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions // Shared stacks - ImVector ColorModifiers; // Stack for PushStyleColor()/PopStyleColor() - ImVector StyleModifiers; // Stack for PushStyleVar()/PopStyleVar() - ImVector FontStack; // Stack for PushFont()/PopFont() + ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() + ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() + ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() + ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - not inherited by Begin(), unless child window + ImVectorItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() + ImVectorGroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() ImVectorOpenPopupStack; // Which popups are open (persistent) ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) @@ -1301,6 +1366,7 @@ struct ImGuiContext ImGuiTextBuffer SettingsIniData; // In memory .ini settings ImVector SettingsHandlers; // List of .ini settings handlers ImChunkStream SettingsWindows; // ImGuiWindow .ini settings entries + ImVector Hooks; // Hooks for extensions (e.g. test engine) // Capture/Logging bool LogEnabled; // Currently capturing @@ -1316,6 +1382,7 @@ struct ImGuiContext // Debug Tools bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this id + ImGuiMetricsConfig DebugMetricsConfig; // Misc float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. @@ -1337,6 +1404,7 @@ struct ImGuiContext FrameCount = 0; FrameCountEnded = FrameCountRendered = -1; WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; + GcCompactAll = false; TestEngineHookItems = false; TestEngineHookIdInfo = 0; TestEngine = NULL; @@ -1473,7 +1541,8 @@ struct ImGuiContext //----------------------------------------------------------------------------- // Transient per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the DC variable name in ImGuiWindow. -// FIXME: That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered. +// (That's theory, in practice the delimitation between ImGuiWindow and ImGuiWindowTempData is quite tenuous and could be reconsidered..) +// (This doesn't need a constructor because we zero-clear it as part of ImGuiWindow and all frame-temporary data are setup on Begin) struct IMGUI_API ImGuiWindowTempData { // Layout @@ -1497,9 +1566,8 @@ struct IMGUI_API ImGuiWindowTempData // Keyboard/Gamepad navigation ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) - int NavLayerCurrentMask; // = (1 << NavLayerCurrent) used by ItemAdd prior to clipping. - int NavLayerActiveMask; // Which layer have been written to (result from previous frame) - int NavLayerActiveMaskNext; // Which layer have been written to (buffer for current frame) + int NavLayerActiveMask; // Which layers have been written to (result from previous frame) + int NavLayerActiveMaskNext; // Which layers have been written to (accumulator for current frame) ImGuiID NavFocusScopeIdCurrent; // Current focus scope ID while appending bool NavHideHighlightOneFrame; bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) @@ -1512,7 +1580,7 @@ struct IMGUI_API ImGuiWindowTempData ImU32 TreeJumpToParentOnPopMask; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. ImVector ChildWindows; ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state) - ImGuiColumns* CurrentColumns; // Current columns set + ImGuiOldColumns* CurrentColumns; // Current columns set ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() int FocusCounterRegular; // (Legacy Focus/Tabbing system) Sequential counter, start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) @@ -1520,49 +1588,12 @@ struct IMGUI_API ImGuiWindowTempData // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. - ImGuiItemFlags ItemFlags; // == ItemFlagsStack.back() [empty == ImGuiItemFlags_Default] + ImGuiItemFlags ItemFlags; // == g.ItemFlagsStack.back() float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f] - ImVectorItemFlagsStack; ImVector ItemWidthStack; ImVector TextWrapPosStack; - ImVectorGroupStack; - short StackSizesBackup[6]; // Store size of various stacks for asserting - - ImGuiWindowTempData() - { - CursorPos = CursorPosPrevLine = CursorStartPos = CursorMaxPos = ImVec2(0.0f, 0.0f); - CurrLineSize = PrevLineSize = ImVec2(0.0f, 0.0f); - CurrLineTextBaseOffset = PrevLineTextBaseOffset = 0.0f; - Indent = ImVec1(0.0f); - ColumnsOffset = ImVec1(0.0f); - GroupOffset = ImVec1(0.0f); - - LastItemId = 0; - LastItemStatusFlags = ImGuiItemStatusFlags_None; - LastItemRect = LastItemDisplayRect = ImRect(); - - NavLayerActiveMask = NavLayerActiveMaskNext = 0x00; - NavLayerCurrent = ImGuiNavLayer_Main; - NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); - NavFocusScopeIdCurrent = 0; - NavHideHighlightOneFrame = false; - NavHasScroll = false; - - MenuBarAppending = false; - MenuBarOffset = ImVec2(0.0f, 0.0f); - TreeDepth = 0; - TreeJumpToParentOnPopMask = 0x00; - StateStorage = NULL; - CurrentColumns = NULL; - LayoutType = ParentLayoutType = ImGuiLayoutType_Vertical; - FocusCounterRegular = FocusCounterTabStop = -1; - - ItemFlags = ImGuiItemFlags_Default_; - ItemWidth = 0.0f; - TextWrapPos = -1.0f; - memset(StackSizesBackup, 0, sizeof(StackSizesBackup)); - } + ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting }; // Storage for one window @@ -1619,7 +1650,7 @@ struct IMGUI_API ImGuiWindow ImVector IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack. (In theory this should be in the TempData structure) ImGuiWindowTempData DC; // Temporary per-window data, reset at the beginning of the frame. This used to be called ImGuiDrawContext, hence the "DC" variable name. - // The best way to understand what those rectangles are is to use the 'Metrics -> Tools -> Show windows rectangles' viewer. + // The best way to understand what those rectangles are is to use the 'Metrics->Tools->Show Windows Rectangles' viewer. // The main 'OuterRect', omitted as a field, is window->Rect(). ImRect OuterRectClipped; // == Window->Rect() just after setup in Begin(). == window->Rect() for root window. ImRect InnerRect; // Inner rectangle (omit title bar, menu bar, scroll bar) @@ -1635,7 +1666,7 @@ struct IMGUI_API ImGuiWindow float LastTimeActive; // Last timestamp the window was Active (using float as we don't need high precision there) float ItemWidthDefault; ImGuiStorage StateStorage; - ImVector ColumnsStorage; + ImVector ColumnsStorage; float FontWindowScale; // User scale multiplier per-window, via SetWindowFontScale() int SettingsOffset; // Offset into SettingsWindows[] (offsets are always valid as we only grow the array from the back) @@ -1650,9 +1681,9 @@ struct IMGUI_API ImGuiWindow ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space - bool MemoryCompacted; // Set when window extraneous data have been garbage collected int MemoryDrawListIdxCapacity; // Backup of last idx/vtx count, so when waking up the window we can preallocate and avoid iterative alloc/copy int MemoryDrawListVtxCapacity; + bool MemoryCompacted; // Set when window extraneous data have been garbage collected public: ImGuiWindow(ImGuiContext* context, const char* name); @@ -1703,7 +1734,8 @@ enum ImGuiTabBarFlagsPrivate_ // Extend ImGuiTabItemFlags_ enum ImGuiTabItemFlagsPrivate_ { - ImGuiTabItemFlags_NoCloseButton = 1 << 20 // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) + ImGuiTabItemFlags_NoCloseButton = 1 << 20, // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) + ImGuiTabItemFlags_Button = 1 << 21 // Used by TabItemButton, change the tab item behavior to mimic a button }; // Storage for one active tab item (sizeof() 28~32 bytes) @@ -1715,18 +1747,20 @@ struct ImGuiTabItem int LastFrameSelected; // This allows us to infer an ordered list of the last activated tabs with little maintenance float Offset; // Position relative to beginning of tab float Width; // Width currently displayed - float ContentWidth; // Width of actual contents, stored during BeginTabItem() call + float ContentWidth; // Width of label, stored during BeginTabItem() call ImS16 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames - ImS8 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable + ImS16 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable + ImS16 IndexDuringLayout; // Index only used during TabBarLayout() bool WantClose; // Marked as closed by SetTabItemClosed() - ImGuiTabItem() { ID = 0; Flags = ImGuiTabItemFlags_None; LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; Offset = Width = ContentWidth = 0.0f; BeginOrder = -1; WantClose = false; } + ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; NameOffset = BeginOrder = IndexDuringLayout = -1; } }; -// Storage for a tab bar (sizeof() 92~96 bytes) +// Storage for a tab bar (sizeof() 152 bytes) struct ImGuiTabBar { ImVector Tabs; + ImGuiTabBarFlags Flags; ImGuiID ID; // Zero for tab-bars used by docking ImGuiID SelectedTabId; // Selected tab/window ImGuiID NextSelectedTabId; @@ -1734,22 +1768,27 @@ struct ImGuiTabBar int CurrFrameVisible; int PrevFrameVisible; ImRect BarRect; - float LastTabContentHeight; // Record the height of contents submitted below the tab bar + float CurrTabsContentsHeight; + float PrevTabsContentsHeight; // Record the height of contents submitted below the tab bar float WidthAllTabs; // Actual width of all tabs (locked during layout) float WidthAllTabsIdeal; // Ideal width if all tabs were visible and not clipped - float OffsetNextTab; // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set. float ScrollingAnim; float ScrollingTarget; float ScrollingTargetDistToVisibility; float ScrollingSpeed; - ImGuiTabBarFlags Flags; + float ScrollingRectMinX; + float ScrollingRectMaxX; ImGuiID ReorderRequestTabId; ImS8 ReorderRequestDir; - ImS8 TabsActiveCount; // Number of tabs submitted this frame. + ImS8 BeginCount; bool WantLayout; bool VisibleTabWasSubmitted; - short LastTabItemIdx; // Index of last BeginTabItem() tab for use by EndTabItem() + bool TabsAddedNew; // Set to true when a new tab item or button has been added to the tab bar during last frame + ImS16 TabsActiveCount; // Number of tabs submitted this frame. + ImS16 LastTabItemIdx; // Index of last BeginTabItem() tab for use by EndTabItem() + float ItemSpacingY; ImVec2 FramePadding; // style.FramePadding locked at the time of BeginTabBar() + ImVec2 BackupCursorPos; ImGuiTextBuffer TabsNames; // For non-docking tab bar we re-append names in a contiguous buffer. ImGuiTabBar(); @@ -1788,6 +1827,7 @@ namespace ImGui IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); IMGUI_API ImVec2 CalcWindowExpectedSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); + IMGUI_API bool IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); IMGUI_API ImRect GetWindowAllowedExtentRect(ImGuiWindow* window); IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); @@ -1817,6 +1857,10 @@ namespace ImGui IMGUI_API void UpdateMouseMovingWindowNewFrame(); IMGUI_API void UpdateMouseMovingWindowEndFrame(); + // Generic context hooks + IMGUI_API void AddContextHook(ImGuiContext* context, const ImGuiContextHook* hook); + IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type); + // Settings IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); @@ -1839,6 +1883,7 @@ namespace ImGui inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemStatusFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } + inline ImGuiItemFlags GetItemsFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.ItemFlags; } IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); IMGUI_API void SetFocusID(ImGuiID id, ImGuiWindow* window); IMGUI_API void ClearActiveID(); @@ -1847,6 +1892,7 @@ namespace ImGui IMGUI_API void KeepAliveID(ImGuiID id); IMGUI_API void MarkItemEdited(ImGuiID id); // Mark data associated to given item as "edited", used by IsItemDeactivatedAfterEdit() function. IMGUI_API void PushOverrideID(ImGuiID id); // Push given value as-is at the top of the ID stack (whereas PushID combines old and new hashes) + IMGUI_API ImGuiID GetIDWithSeed(const char* str_id_begin, const char* str_id_end, ImGuiID seed); // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); @@ -1900,7 +1946,8 @@ namespace ImGui // patterns generally need to react (e.g. clear selection) when landing on an item of the set. IMGUI_API void PushFocusScope(ImGuiID id); IMGUI_API void PopFocusScope(); - inline ImGuiID GetFocusScopeID() { ImGuiContext& g = *GImGui; return g.NavFocusScopeId; } + inline ImGuiID GetFocusedFocusScope() { ImGuiContext& g = *GImGui; return g.NavFocusScopeId; } // Focus scope which is actually active + inline ImGuiID GetFocusScope() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.NavFocusScopeIdCurrent; } // Focus scope we are outputting into, set by PushFocusScope() // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. @@ -1920,15 +1967,15 @@ namespace ImGui // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) IMGUI_API void SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect); - IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiColumnsFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). - IMGUI_API void EndColumns(); // close columns + IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiOldColumnFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). + IMGUI_API void EndColumns(); // close columns IMGUI_API void PushColumnClipRect(int column_index); IMGUI_API void PushColumnsBackground(); IMGUI_API void PopColumnsBackground(); IMGUI_API ImGuiID GetColumnsID(const char* str_id, int count); - IMGUI_API ImGuiColumns* FindOrCreateColumns(ImGuiWindow* window, ImGuiID id); - IMGUI_API float GetColumnOffsetFromNorm(const ImGuiColumns* columns, float offset_norm); - IMGUI_API float GetColumnNormFromOffset(const ImGuiColumns* columns, float offset); + IMGUI_API ImGuiOldColumns* FindOrCreateColumns(ImGuiWindow* window, ImGuiID id); + IMGUI_API float GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm); + IMGUI_API float GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset); // Tab Bars IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags); @@ -1940,7 +1987,7 @@ namespace ImGui IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); - IMGUI_API bool TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible); + IMGUI_API void TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible, bool* out_just_closed, bool* out_text_clipped); // Render helpers // AVOID USING OUTSIDE OF IMGUI.CPP! NOT FOR PUBLIC CONSUMPTION. THOSE FUNCTIONS ARE A MESS. THEIR SIGNATURE AND BEHAVIOR WILL CHANGE, THEY NEED TO BE REFACTORED INTO SOMETHING DECENT. @@ -2003,6 +2050,7 @@ namespace ImGui template IMGUI_API bool DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, ImGuiSliderFlags flags); template IMGUI_API bool SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); + template IMGUI_API bool CheckboxFlagsT(const char* label, T* flags, T flags_value); // Data type helpers IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); @@ -2032,13 +2080,24 @@ namespace ImGui IMGUI_API void ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp); // Garbage collection + IMGUI_API void GcCompactTransientMiscBuffers(); IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); // Debug Tools + IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max, col); } inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } + IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); + IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); + IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); + IMGUI_API void DebugNodeStorage(ImGuiStorage* storage, const char* label); + IMGUI_API void DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label); + IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); + IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); + IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); + } // namespace ImGui // ImFontAtlas internals @@ -2052,13 +2111,10 @@ IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned cha IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); //----------------------------------------------------------------------------- -// [SECTION] Test Engine Hooks (imgui_test_engine) +// [SECTION] Test Engine specific hooks (imgui_test_engine) //----------------------------------------------------------------------------- #ifdef IMGUI_ENABLE_TEST_ENGINE -extern void ImGuiTestEngineHook_Shutdown(ImGuiContext* ctx); -extern void ImGuiTestEngineHook_PreNewFrame(ImGuiContext* ctx); -extern void ImGuiTestEngineHook_PostNewFrame(ImGuiContext* ctx); extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); @@ -2077,6 +2133,8 @@ extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const cha #define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) do { } while (0) #endif +//----------------------------------------------------------------------------- + #if defined(__clang__) #pragma clang diagnostic pop #elif defined(__GNUC__) diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index 466bc803..08a741fb 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.79 WIP +// dear imgui, v1.80 WIP // (widgets code) /* @@ -408,6 +408,7 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // - Image() // - ImageButton() // - Checkbox() +// - CheckboxFlagsT() [Internal] // - CheckboxFlags() // - RadioButton() // - ProgressBar() @@ -1071,9 +1072,11 @@ bool ImGui::Checkbox(const char* label, bool* v) RenderNavHighlight(total_bb, id); RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); - if (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) + bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0; + if (mixed_value) { // Undocumented tristate/mixed/indeterminate checkbox (#2644) + // This may seem awkwardly designed because the aim is to make ImGuiItemFlags_MixedValue supported by all widgets (not just checkbox) ImVec2 pad(ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)), ImMax(1.0f, IM_FLOOR(square_sz / 3.6f))); window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); } @@ -1084,7 +1087,7 @@ bool ImGui::Checkbox(const char* label, bool* v) } if (g.LogEnabled) - LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]"); + LogRenderedText(&total_bb.Min, mixed_value ? "[~]" : *v ? "[x]" : "[ ]"); if (label_size.x > 0.0f) RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); @@ -1092,21 +1095,45 @@ bool ImGui::Checkbox(const char* label, bool* v) return pressed; } -bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value) +template +bool ImGui::CheckboxFlagsT(const char* label, T* flags, T flags_value) { - bool v = ((*flags & flags_value) == flags_value); - bool pressed = Checkbox(label, &v); + bool all_on = (*flags & flags_value) == flags_value; + bool any_on = (*flags & flags_value) != 0; + bool pressed; + if (!all_on && any_on) + { + ImGuiWindow* window = GetCurrentWindow(); + ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; + window->DC.ItemFlags |= ImGuiItemFlags_MixedValue; + pressed = Checkbox(label, &all_on); + window->DC.ItemFlags = backup_item_flags; + } + else + { + pressed = Checkbox(label, &all_on); + + } if (pressed) { - if (v) + if (all_on) *flags |= flags_value; else *flags &= ~flags_value; } - return pressed; } +bool ImGui::CheckboxFlags(const char* label, int* flags, int flags_value) +{ + return CheckboxFlagsT(label, flags, flags_value); +} + +bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value) +{ + return CheckboxFlagsT(label, flags, flags_value); +} + bool ImGui::RadioButton(const char* label, bool active) { ImGuiWindow* window = GetCurrentWindow(); @@ -1319,10 +1346,12 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) // Horizontal Separator float x1 = window->Pos.x; float x2 = window->Pos.x + window->Size.x; - if (!window->DC.GroupStack.empty()) + + // FIXME-WORKRECT: old hack (#205) until we decide of consistent behavior with WorkRect/Indent and Separator + if (g.GroupStack.Size > 0 && g.GroupStack.back().WindowID == window->ID) x1 += window->DC.Indent.x; - ImGuiColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; + ImGuiOldColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; if (columns) PushColumnsBackground(); @@ -1426,11 +1455,13 @@ static int IMGUI_CDECL ShrinkWidthItemComparer(const void* lhs, const void* rhs) } // Shrink excess width from a set of item, by removing width from the larger items first. +// Set items Width to -1.0f to disable shrinking this item. void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess) { if (count == 1) { - items[0].Width = ImMax(items[0].Width - width_excess, 1.0f); + if (items[0].Width >= 0.0f) + items[0].Width = ImMax(items[0].Width - width_excess, 1.0f); return; } ImQsort(items, (size_t)count, sizeof(ImGuiShrinkWidthItem), ShrinkWidthItemComparer); @@ -1439,7 +1470,9 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc { while (count_same_width < count && items[0].Width <= items[count_same_width].Width) count_same_width++; - float max_width_to_remove_per_item = (count_same_width < count) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f); + float max_width_to_remove_per_item = (count_same_width < count && items[count_same_width].Width >= 0.0f) ? (items[0].Width - items[count_same_width].Width) : (items[0].Width - 1.0f); + if (max_width_to_remove_per_item <= 0.0f) + break; float width_to_remove_per_item = ImMin(width_excess / count_same_width, max_width_to_remove_per_item); for (int item_n = 0; item_n < count_same_width; item_n++) items[item_n].Width -= width_to_remove_per_item; @@ -2238,8 +2271,8 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (temp_input_is_active) { - // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampInput is set - const bool is_clamp_input = (flags & ImGuiSliderFlags_ClampOnInput) != 0 && (p_min == NULL || p_max == NULL || DataTypeCompare(data_type, p_min, p_max) < 0); + // Only clamp CTRL+Click input when ImGuiSliderFlags_AlwaysClamp is set + const bool is_clamp_input = (flags & ImGuiSliderFlags_AlwaysClamp) != 0 && (p_min == NULL || p_max == NULL || DataTypeCompare(data_type, p_min, p_max) < 0); return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); } @@ -2320,7 +2353,7 @@ bool ImGui::DragFloat4(const char* label, float v[4], float v_speed, float v_min return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, flags); } -// NB: You likely want to specify the ImGuiSliderFlags_ClampOnInput when using this. +// NB: You likely want to specify the ImGuiSliderFlags_AlwaysClamp when using this. bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed, float v_min, float v_max, const char* format, const char* format_max, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); @@ -2373,7 +2406,7 @@ bool ImGui::DragInt4(const char* label, int v[4], float v_speed, int v_min, int return DragScalarN(label, ImGuiDataType_S32, v, 4, v_speed, &v_min, &v_max, format, flags); } -// NB: You likely want to specify the ImGuiSliderFlags_ClampOnInput when using this. +// NB: You likely want to specify the ImGuiSliderFlags_AlwaysClamp when using this. bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max, ImGuiSliderFlags flags) { ImGuiWindow* window = GetCurrentWindow(); @@ -2845,8 +2878,8 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (temp_input_is_active) { - // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampInput is set - const bool is_clamp_input = (flags & ImGuiSliderFlags_ClampOnInput) != 0; + // Only clamp CTRL+Click input when ImGuiSliderFlags_AlwaysClamp is set + const bool is_clamp_input = (flags & ImGuiSliderFlags_AlwaysClamp) != 0; return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); } @@ -3174,8 +3207,7 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* return value_changed; } -// Note that Drag/Slider functions are only forwarding the min/max values clamping values if the -// ImGuiSliderFlags_ClampOnInput / ImGuiSliderFlags_ClampOnInput flag is set! +// Note that Drag/Slider functions are only forwarding the min/max values clamping values if the ImGuiSliderFlags_AlwaysClamp flag is set! // This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility. // However this may not be ideal for all uses, as some user code may break on out of bound values. bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max) @@ -3345,41 +3377,6 @@ bool ImGui::InputFloat4(const char* label, float v[4], const char* format, ImGui return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); } -// Prefer using "const char* format" directly, which is more flexible and consistent with other API. -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, int decimal_precision, ImGuiInputTextFlags flags) -{ - char format[16] = "%f"; - if (decimal_precision >= 0) - ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); - return InputFloat(label, v, step, step_fast, format, flags); -} - -bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, ImGuiInputTextFlags flags) -{ - char format[16] = "%f"; - if (decimal_precision >= 0) - ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); - return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, flags); -} - -bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags flags) -{ - char format[16] = "%f"; - if (decimal_precision >= 0) - ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); - return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, flags); -} - -bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags flags) -{ - char format[16] = "%f"; - if (decimal_precision >= 0) - ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision); - return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, flags); -} -#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS - bool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiInputTextFlags flags) { // Hexadecimal input provided as a convenience but the flag name is awkward. Typically you'd use InputText() to parse your own data, if you want to handle prefixes. @@ -3590,6 +3587,8 @@ static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const Im #define STB_TEXTEDIT_K_REDO 0x20000B // keyboard input to perform redo #define STB_TEXTEDIT_K_WORDLEFT 0x20000C // keyboard input to move cursor left one word #define STB_TEXTEDIT_K_WORDRIGHT 0x20000D // keyboard input to move cursor right one word +#define STB_TEXTEDIT_K_PGUP 0x20000E // keyboard input to move cursor up a page +#define STB_TEXTEDIT_K_PGDOWN 0x20000F // keyboard input to move cursor down a page #define STB_TEXTEDIT_K_SHIFT 0x400000 #define STB_TEXTEDIT_IMPLEMENTATION @@ -3828,7 +3827,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ return false; } draw_window = g.CurrentWindow; // Child window - draw_window->DC.NavLayerActiveMaskNext |= draw_window->DC.NavLayerCurrentMask; // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. + draw_window->DC.NavLayerActiveMaskNext |= (1 << draw_window->DC.NavLayerCurrent); // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. inner_size.x -= draw_window->ScrollbarSizes.x; } else @@ -3856,9 +3855,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ bool clear_active_id = false; bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline); + float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; + + const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); const bool init_make_active = (focus_requested || user_clicked || user_scroll_finish || user_nav_input_start); const bool init_state = (init_make_active || user_scroll_active); - if (init_state && g.ActiveId != id) + if ((init_state && g.ActiveId != id) || init_changed_specs) { // Access state even if we don't own it yet. state = &g.InputTextState; @@ -3880,7 +3882,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Preserve cursor position and undo/redo stack if we come back to same widget // FIXME: For non-readonly widgets we might be able to require that TextAIsValid && TextA == buf ? (untested) and discard undo stack if user buffer has changed. - const bool recycle_state = (state->ID == id); + const bool recycle_state = (state->ID == id && !init_changed_specs); if (recycle_state) { // Recycle existing cursor/selection/undo stack but clamp position @@ -3916,7 +3918,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Home) | ((ImU64)1 << ImGuiKey_End); if (is_multiline) - g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_PageUp) | ((ImU64)1 << ImGuiKey_PageDown); // FIXME-NAV: Page up/down actually not supported yet by widget, but claim them ahead. + g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_PageUp) | ((ImU64)1 << ImGuiKey_PageDown); if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Tab); } @@ -4017,7 +4019,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (state->SelectedAllMouseLock && !io.MouseDown[0]) state->SelectedAllMouseLock = false; - // It is ill-defined whether the back-end needs to send a \t character when pressing the TAB keys. + // It is ill-defined whether the backend needs to send a \t character when pressing the TAB keys. // Win32 and GLFW naturally do it but not SDL. const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) @@ -4055,6 +4057,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IM_ASSERT(state != NULL); IM_ASSERT(io.KeyMods == GetMergedKeyModFlags() && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); // We rarely do this check, but if anything let's do it here. + const int row_count_per_page = ImMax((int)((inner_size.y - style.FramePadding.y) / g.FontSize), 1); + state->Stb.row_count_per_page = row_count_per_page; + const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_osx = io.ConfigMacOSXBehaviors; const bool is_osx_shift_shortcut = is_osx && (io.KeyMods == (ImGuiKeyModFlags_Super | ImGuiKeyModFlags_Shift)); @@ -4074,6 +4079,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } else if (IsKeyPressedMap(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } else if (IsKeyPressedMap(ImGuiKey_DownArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMin(draw_window->Scroll.y + g.FontSize, GetScrollMaxY())); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTEND : STB_TEXTEDIT_K_DOWN) | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_PageUp) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGUP | k_mask); scroll_y -= row_count_per_page * g.FontSize; } + else if (IsKeyPressedMap(ImGuiKey_PageDown) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGDOWN | k_mask); scroll_y += row_count_per_page * g.FontSize; } else if (IsKeyPressedMap(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } else if (IsKeyPressedMap(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } @@ -4269,10 +4276,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IM_ASSERT(callback_data.Buf == state->TextA.Data); // Invalid to modify those fields IM_ASSERT(callback_data.BufSize == state->BufCapacityA); IM_ASSERT(callback_data.Flags == flags); - if (callback_data.CursorPos != utf8_cursor_pos) { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; } - if (callback_data.SelectionStart != utf8_selection_start) { state->Stb.select_start = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); } - if (callback_data.SelectionEnd != utf8_selection_end) { state->Stb.select_end = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } - if (callback_data.BufDirty) + const bool buf_dirty = callback_data.BufDirty; + if (callback_data.CursorPos != utf8_cursor_pos || buf_dirty) { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; } + if (callback_data.SelectionStart != utf8_selection_start || buf_dirty) { state->Stb.select_start = (callback_data.SelectionStart == callback_data.CursorPos) ? state->Stb.cursor : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); } + if (callback_data.SelectionEnd != utf8_selection_end || buf_dirty) { state->Stb.select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb.select_start : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } + if (buf_dirty) { IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! if (callback_data.BufTextLen > backup_current_text_length && is_resizable) @@ -4443,12 +4451,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_multiline) { // Test if cursor is vertically visible - float scroll_y = draw_window->Scroll.y; - const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f); if (cursor_offset.y - g.FontSize < scroll_y) scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize); else if (cursor_offset.y - inner_size.y >= scroll_y) scroll_y = cursor_offset.y - inner_size.y + style.FramePadding.y * 2.0f; + const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f); scroll_y = ImClamp(scroll_y, 0.0f, scroll_max_y); draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag draw_window->Scroll.y = scroll_y; @@ -4582,7 +4589,7 @@ bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flag // Edit colors components (each component in 0.0f..1.0f range). // See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. -// With typical options: Left-click on colored square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item. +// With typical options: Left-click on color square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item. bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags) { ImGuiWindow* window = GetCurrentWindow(); @@ -4691,7 +4698,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupContextItem("context"); + OpenPopupOnItemClick("context"); } } else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) @@ -4716,7 +4723,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupContextItem("context"); + OpenPopupOnItemClick("context"); } ImGuiWindow* picker_active_window = NULL; @@ -4737,7 +4744,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupContextItem("context"); + OpenPopupOnItemClick("context"); if (BeginPopup("picker")) { @@ -4957,7 +4964,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupContextItem("context"); + OpenPopupOnItemClick("context"); } else if (flags & ImGuiColorEditFlags_PickerHueBar) { @@ -4970,7 +4977,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupContextItem("context"); + OpenPopupOnItemClick("context"); // Hue bar logic SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); @@ -5218,7 +5225,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl return value_changed; } -// A little colored square. Return true when clicked. +// A little color square. Return true when clicked. // FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip. // 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. // Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set. @@ -5443,7 +5450,7 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl if (allow_opt_alpha_bar) { if (allow_opt_picker) Separator(); - CheckboxFlags("Alpha Bar", (unsigned int*)&g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); + CheckboxFlags("Alpha Bar", &g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); } EndPopup(); } @@ -5890,7 +5897,8 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags float button_size = g.FontSize; float button_x = ImMax(window->DC.LastItemRect.Min.x, window->DC.LastItemRect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); float button_y = window->DC.LastItemRect.Min.y; - if (CloseButton(window->GetID((void*)((intptr_t)id + 1)), ImVec2(button_x, button_y))) + ImGuiID close_button_id = GetIDWithSeed("#CLOSE", NULL, id); + if (CloseButton(close_button_id, ImVec2(button_x, button_y))) *p_open = false; last_item_backup.Restore(); } @@ -5917,10 +5925,6 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; - const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0; - if (span_all_columns && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped. - PushColumnsBackground(); - // Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle. ImGuiID id = window->GetID(label); ImVec2 label_size = CalcTextSize(label, NULL, true); @@ -5930,6 +5934,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ItemSize(size, 0.0f); // Fill horizontal space + // We don't support (size < 0.0f) in Selectable() because the ItemSpacing extension would make explicitely right-aligned sizes not visibly match other widgets. + const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0; const float min_x = span_all_columns ? window->ParentWorkRect.Min.x : pos.x; const float max_x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x; if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_SpanAvailWidth)) @@ -5954,6 +5960,15 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl } //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); } + // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackground for every Selectable.. + const float backup_clip_rect_min_x = window->ClipRect.Min.x; + const float backup_clip_rect_max_x = window->ClipRect.Max.x; + if (span_all_columns) + { + window->ClipRect.Min.x = window->ParentWorkRect.Min.x; + window->ClipRect.Max.x = window->ParentWorkRect.Max.x; + } + bool item_add; if (flags & ImGuiSelectableFlags_Disabled) { @@ -5966,13 +5981,21 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { item_add = ItemAdd(bb, id); } - if (!item_add) + + if (span_all_columns) { - if (span_all_columns && window->DC.CurrentColumns) - PopColumnsBackground(); - return false; + window->ClipRect.Min.x = backup_clip_rect_min_x; + window->ClipRect.Max.x = backup_clip_rect_max_x; } + if (!item_add) + return false; + + // FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only, + // which would be advantageous since most selectable are not selected. + if (span_all_columns && window->DC.CurrentColumns) + PushColumnsBackground(); + // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; } @@ -6141,7 +6164,8 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v // Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper. ImGuiContext& g = *GImGui; bool value_changed = false; - ImGuiListClipper clipper(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to. + ImGuiListClipper clipper; + clipper.Begin(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to. while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { @@ -6380,13 +6404,6 @@ void ImGui::Value(const char* prefix, float v, const char* float_format) //------------------------------------------------------------------------- // Helpers for internal use -ImGuiMenuColumns::ImGuiMenuColumns() -{ - Spacing = Width = NextWidth = 0.0f; - memset(Pos, 0, sizeof(Pos)); - memset(NextWidths, 0, sizeof(NextWidths)); -} - void ImGuiMenuColumns::Update(int count, float spacing, bool clear) { IM_ASSERT(count == IM_ARRAYSIZE(Pos)); @@ -6448,7 +6465,6 @@ bool ImGui::BeginMenuBar() window->DC.CursorPos = window->DC.CursorMaxPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); window->DC.LayoutType = ImGuiLayoutType_Horizontal; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Menu); window->DC.MenuBarAppending = true; AlignTextToFramePadding(); return true; @@ -6487,11 +6503,10 @@ void ImGui::EndMenuBar() PopClipRect(); PopID(); window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->MenuBarRect().Min.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos. - window->DC.GroupStack.back().EmitItem = false; + g.GroupStack.back().EmitItem = false; EndGroup(); // Restore position on layer 0 window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerCurrentMask = (1 << ImGuiNavLayer_Main); window->DC.MenuBarAppending = false; } @@ -6547,7 +6562,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) // If a menu with same the ID was already submitted, we will append to it, matching the behavior of Begin(). // We are relying on a O(N) search - so O(N log N) over the frame - which seems like the most efficient for the expected small amount of BeginMenu() calls per frame. - // If somehow this is ever becoming a problem we can switch to use e.g. a ImGuiStorager mapping key to last frame used. + // If somehow this is ever becoming a problem we can switch to use e.g. ImGuiStorage mapping key to last frame used. if (g.MenusIdSubmittedThisFrame.contains(id)) { if (menu_is_open) @@ -6788,33 +6803,44 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, // - TabBarTabListPopupButton() [Internal] //------------------------------------------------------------------------- +struct ImGuiTabBarSection +{ + int TabCount; // Number of tabs in this section. + float Width; // Sum of width of tabs in this section (after shrinking down) + float Spacing; // Horizontal spacing at the end of the section. + + ImGuiTabBarSection() { memset(this, 0, sizeof(*this)); } +}; + namespace ImGui { static void TabBarLayout(ImGuiTabBar* tab_bar); static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label); static float TabBarCalcMaxTabWidth(); static float TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling); - static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); + static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImGuiTabBarSection* sections); static ImGuiTabItem* TabBarScrollingButtons(ImGuiTabBar* tab_bar); static ImGuiTabItem* TabBarTabListPopupButton(ImGuiTabBar* tab_bar); } ImGuiTabBar::ImGuiTabBar() { - ID = 0; - SelectedTabId = NextSelectedTabId = VisibleTabId = 0; + memset(this, 0, sizeof(*this)); CurrFrameVisible = PrevFrameVisible = -1; - LastTabContentHeight = 0.0f; - WidthAllTabs = WidthAllTabsIdeal = OffsetNextTab = 0.0f; - ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f; - Flags = ImGuiTabBarFlags_None; - ReorderRequestTabId = 0; - ReorderRequestDir = 0; - TabsActiveCount = 0; - WantLayout = VisibleTabWasSubmitted = false; LastTabItemIdx = -1; } +static int IMGUI_CDECL TabItemComparerBySection(const void* lhs, const void* rhs) +{ + const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; + const ImGuiTabItem* b = (const ImGuiTabItem*)rhs; + const int a_section = (a->Flags & ImGuiTabItemFlags_Leading) ? 0 : (a->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + const int b_section = (b->Flags & ImGuiTabItemFlags_Leading) ? 0 : (b->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + if (a_section != b_section) + return a_section - b_section; + return (int)(a->IndexDuringLayout - b->IndexDuringLayout); +} + static int IMGUI_CDECL TabItemComparerByBeginOrder(const void* lhs, const void* rhs) { const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; @@ -6864,16 +6890,20 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG g.CurrentTabBarStack.push_back(GetTabBarRefFromTabBar(tab_bar)); g.CurrentTabBar = tab_bar; + // Append with multiple BeginTabBar()/EndTabBar() pairs. + tab_bar->BackupCursorPos = window->DC.CursorPos; if (tab_bar->CurrFrameVisible == g.FrameCount) { - //IMGUI_DEBUG_LOG("BeginTabBarEx already called this frame\n", g.FrameCount); - IM_ASSERT(0); + window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x, tab_bar->BarRect.Max.y + tab_bar->ItemSpacingY); + tab_bar->BeginCount++; return true; } - // When toggling ImGuiTabBarFlags_Reorderable flag, ensure tabs are ordered based on their submission order. - if ((flags & ImGuiTabBarFlags_Reorderable) != (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) && tab_bar->Tabs.Size > 1) - ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByBeginOrder); + // Ensure correct ordering when toggling ImGuiTabBarFlags_Reorderable flag, or when a new tab was added while being not reorderable + if ((flags & ImGuiTabBarFlags_Reorderable) != (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) || (tab_bar->TabsAddedNew && !(flags & ImGuiTabBarFlags_Reorderable))) + if (tab_bar->Tabs.Size > 1) + ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByBeginOrder); + tab_bar->TabsAddedNew = false; // Flags if ((flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) @@ -6884,12 +6914,15 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG tab_bar->WantLayout = true; // Layout will be done on the first call to ItemTab() tab_bar->PrevFrameVisible = tab_bar->CurrFrameVisible; tab_bar->CurrFrameVisible = g.FrameCount; + tab_bar->PrevTabsContentsHeight = tab_bar->CurrTabsContentsHeight; + tab_bar->CurrTabsContentsHeight = 0.0f; + tab_bar->ItemSpacingY = g.Style.ItemSpacing.y; tab_bar->FramePadding = g.Style.FramePadding; tab_bar->TabsActiveCount = 0; + tab_bar->BeginCount = 1; // Set cursor pos in a way which only be used in the off-chance the user erroneously submits item before BeginTabItem(): items will overlap - window->DC.CursorPos.x = tab_bar->BarRect.Min.x; - window->DC.CursorPos.y = tab_bar->BarRect.Max.y + g.Style.ItemSpacing.y; + window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x, tab_bar->BarRect.Max.y + tab_bar->ItemSpacingY); // Draw separator const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); @@ -6915,15 +6948,24 @@ void ImGui::EndTabBar() IM_ASSERT_USER_ERROR(tab_bar != NULL, "Mismatched BeginTabBar()/EndTabBar()!"); return; } - if (tab_bar->WantLayout) // Fallback in case no TabItem have been submitted + + // Fallback in case no TabItem have been submitted + if (tab_bar->WantLayout) TabBarLayout(tab_bar); // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed(). const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); if (tab_bar->VisibleTabWasSubmitted || tab_bar->VisibleTabId == 0 || tab_bar_appearing) - tab_bar->LastTabContentHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f); + { + tab_bar->CurrTabsContentsHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, tab_bar->CurrTabsContentsHeight); + window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->CurrTabsContentsHeight; + } else - window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->LastTabContentHeight; + { + window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->PrevTabsContentsHeight; + } + if (tab_bar->BeginCount > 1) + window->DC.CursorPos = tab_bar->BackupCursorPos; if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) PopID(); @@ -6940,7 +6982,10 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) tab_bar->WantLayout = false; // Garbage collect by compacting list + // Detect if we need to sort out tab list (e.g. in rare case where a tab changed section) int tab_dst_n = 0; + bool need_sort_by_section = false; + ImGuiTabBarSection sections[3]; // Layout sections: Leading, Central, Trailing for (int tab_src_n = 0; tab_src_n < tab_bar->Tabs.Size; tab_src_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_src_n]; @@ -6954,11 +6999,35 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) } if (tab_dst_n != tab_src_n) tab_bar->Tabs[tab_dst_n] = tab_bar->Tabs[tab_src_n]; + + tab = &tab_bar->Tabs[tab_dst_n]; + tab->IndexDuringLayout = (ImS16)tab_dst_n; + + // We will need sorting if tabs have changed section (e.g. moved from one of Leading/Central/Trailing to another) + int curr_tab_section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + if (tab_dst_n > 0) + { + ImGuiTabItem* prev_tab = &tab_bar->Tabs[tab_dst_n - 1]; + int prev_tab_section_n = (prev_tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (prev_tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + if (curr_tab_section_n == 0 && prev_tab_section_n != 0) + need_sort_by_section = true; + if (prev_tab_section_n == 2 && curr_tab_section_n != 2) + need_sort_by_section = true; + } + + sections[curr_tab_section_n].TabCount++; tab_dst_n++; } if (tab_bar->Tabs.Size != tab_dst_n) tab_bar->Tabs.resize(tab_dst_n); + if (need_sort_by_section) + ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerBySection); + + // Calculate spacing between sections + sections[0].Spacing = sections[0].TabCount > 0 && (sections[1].TabCount + sections[2].TabCount) > 0 ? g.Style.ItemInnerSpacing.x : 0.0f; + sections[1].Spacing = sections[1].TabCount > 0 && sections[2].TabCount > 0 ? g.Style.ItemInnerSpacing.x : 0.0f; + // Setup next selected tab ImGuiID scroll_track_selected_tab_id = 0; if (tab_bar->NextSelectedTabId) @@ -6980,23 +7049,29 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Tab List Popup (will alter tab_bar->BarRect and therefore the available width!) const bool tab_list_popup_button = (tab_bar->Flags & ImGuiTabBarFlags_TabListPopupButton) != 0; if (tab_list_popup_button) - if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Max.x! + if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Min.x! scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; - // Compute ideal widths + // Leading/Trailing tabs will be shrink only if central one aren't visible anymore, so layout the shrink data as: leading, trailing, central + // (whereas our tabs are stored as: leading, central, trailing) + int shrink_buffer_indexes[3] = { 0, sections[0].TabCount + sections[2].TabCount, sections[0].TabCount }; g.ShrinkWidthBuffer.resize(tab_bar->Tabs.Size); - float width_total_contents = 0.0f; + + // Compute ideal tabs widths + store them into shrink buffer ImGuiTabItem* most_recently_selected_tab = NULL; + int curr_section_n = -1; bool found_selected_tab_id = false; for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; IM_ASSERT(tab->LastFrameVisible >= tab_bar->PrevFrameVisible); - if (most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected) + if ((most_recently_selected_tab == NULL || most_recently_selected_tab->LastFrameSelected < tab->LastFrameSelected) && !(tab->Flags & ImGuiTabItemFlags_Button)) most_recently_selected_tab = tab; if (tab->ID == tab_bar->SelectedTabId) found_selected_tab_id = true; + if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) + scroll_track_selected_tab_id = tab->ID; // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar. // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, @@ -7005,56 +7080,87 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) const bool has_close_button = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) ? false : true; tab->ContentWidth = TabItemCalcSize(tab_name, has_close_button).x; - width_total_contents += (tab_n > 0 ? g.Style.ItemInnerSpacing.x : 0.0f) + tab->ContentWidth; + int section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + ImGuiTabBarSection* section = §ions[section_n]; + section->Width += tab->ContentWidth + (section_n == curr_section_n ? g.Style.ItemInnerSpacing.x : 0.0f); + curr_section_n = section_n; // Store data so we can build an array sorted by width if we need to shrink tabs down - g.ShrinkWidthBuffer[tab_n].Index = tab_n; - g.ShrinkWidthBuffer[tab_n].Width = tab->ContentWidth; - } + int shrink_buffer_index = shrink_buffer_indexes[section_n]++; + g.ShrinkWidthBuffer[shrink_buffer_index].Index = tab_n; + g.ShrinkWidthBuffer[shrink_buffer_index].Width = tab->ContentWidth; - // Compute width - const float initial_offset_x = 0.0f; // g.Style.ItemInnerSpacing.x; - const float width_avail = ImMax(tab_bar->BarRect.GetWidth() - initial_offset_x, 0.0f); - float width_excess = (width_avail < width_total_contents) ? (width_total_contents - width_avail) : 0.0f; - if (width_excess > 0.0f && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown)) - { - // If we don't have enough room, resize down the largest tabs first - ShrinkWidths(g.ShrinkWidthBuffer.Data, g.ShrinkWidthBuffer.Size, width_excess); - for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) - tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index].Width = IM_FLOOR(g.ShrinkWidthBuffer[tab_n].Width); + IM_ASSERT(tab->ContentWidth > 0.0f); + tab->Width = tab->ContentWidth; } + + // Compute total ideal width (used for e.g. auto-resizing a window) + tab_bar->WidthAllTabsIdeal = 0.0f; + for (int section_n = 0; section_n < 3; section_n++) + tab_bar->WidthAllTabsIdeal += sections[section_n].Width + sections[section_n].Spacing; + + // Horizontal scrolling buttons + // (note that TabBarScrollButtons() will alter BarRect.Max.x) + if ((tab_bar->WidthAllTabsIdeal > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll)) + if (ImGuiTabItem* scroll_track_selected_tab = TabBarScrollingButtons(tab_bar)) + { + scroll_track_selected_tab_id = scroll_track_selected_tab->ID; + if (!(scroll_track_selected_tab->Flags & ImGuiTabItemFlags_Button)) + tab_bar->SelectedTabId = scroll_track_selected_tab_id; + } + + // Shrink widths if full tabs don't fit in their allocated space + float section_0_w = sections[0].Width + sections[0].Spacing; + float section_1_w = sections[1].Width + sections[1].Spacing; + float section_2_w = sections[2].Width + sections[2].Spacing; + bool central_section_is_visible = (section_0_w + section_2_w) < tab_bar->BarRect.GetWidth(); + float width_excess; + if (central_section_is_visible) + width_excess = ImMax(section_1_w - (tab_bar->BarRect.GetWidth() - section_0_w - section_2_w), 0.0f); // Excess used to shrink central section else + width_excess = (section_0_w + section_2_w) - tab_bar->BarRect.GetWidth(); // Excess used to shrink leading/trailing section + + // With ImGuiTabBarFlags_FittingPolicyScroll policy, we will only shrink leading/trailing if the central section is not visible anymore + if (width_excess > 0.0f && ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown) || !central_section_is_visible)) { - const float tab_max_width = TabBarCalcMaxTabWidth(); - for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + int shrink_data_count = (central_section_is_visible ? sections[1].TabCount : sections[0].TabCount + sections[2].TabCount); + int shrink_data_offset = (central_section_is_visible ? sections[0].TabCount + sections[2].TabCount : 0); + ShrinkWidths(g.ShrinkWidthBuffer.Data + shrink_data_offset, shrink_data_count, width_excess); + + // Apply shrunk values into tabs and sections + for (int tab_n = shrink_data_offset; tab_n < shrink_data_offset + shrink_data_count; tab_n++) { - ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; - tab->Width = ImMin(tab->ContentWidth, tab_max_width); - IM_ASSERT(tab->Width > 0.0f); + ImGuiTabItem* tab = &tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index]; + float shrinked_width = IM_FLOOR(g.ShrinkWidthBuffer[tab_n].Width); + if (shrinked_width < 0.0f) + continue; + + int section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + sections[section_n].Width -= (tab->Width - shrinked_width); + tab->Width = shrinked_width; } } // Layout all active tabs - float offset_x = initial_offset_x; - float offset_x_ideal = offset_x; - tab_bar->OffsetNextTab = offset_x; // This is used by non-reorderable tab bar where the submission order is always honored. - for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) + int section_tab_index = 0; + float tab_offset = 0.0f; + tab_bar->WidthAllTabs = 0.0f; + for (int section_n = 0; section_n < 3; section_n++) { - ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; - tab->Offset = offset_x; - if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) - scroll_track_selected_tab_id = tab->ID; - offset_x += tab->Width + g.Style.ItemInnerSpacing.x; - offset_x_ideal += tab->ContentWidth + g.Style.ItemInnerSpacing.x; - } - tab_bar->WidthAllTabs = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); - tab_bar->WidthAllTabsIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); + ImGuiTabBarSection* section = §ions[section_n]; + if (section_n == 2) + tab_offset = ImMin(ImMax(0.0f, tab_bar->BarRect.GetWidth() - section->Width), tab_offset); - // Horizontal scrolling buttons - const bool scrolling_buttons = (tab_bar->WidthAllTabs > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll); - if (scrolling_buttons) - if (ImGuiTabItem* tab_to_select = TabBarScrollingButtons(tab_bar)) // NB: Will alter BarRect.Max.x! - scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; + for (int tab_n = 0; tab_n < section->TabCount; tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[section_tab_index + tab_n]; + tab->Offset = tab_offset; + tab_offset += tab->Width + (tab_n < section->TabCount - 1 ? g.Style.ItemInnerSpacing.x : 0.0f); + } + tab_bar->WidthAllTabs += ImMax(section->Width + section->Spacing, 0.0f); + tab_offset += section->Spacing; + section_tab_index += section->TabCount; + } // If we have lost the selected tab, select the next most recently active one if (found_selected_tab_id == false) @@ -7069,7 +7175,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Update scrolling if (scroll_track_selected_tab_id) if (ImGuiTabItem* scroll_track_selected_tab = TabBarFindTabByID(tab_bar, scroll_track_selected_tab_id)) - TabBarScrollToTab(tab_bar, scroll_track_selected_tab); + TabBarScrollToTab(tab_bar, scroll_track_selected_tab, sections); tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim); tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget); if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget) @@ -7085,6 +7191,8 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) { tab_bar->ScrollingSpeed = 0.0f; } + tab_bar->ScrollingRectMinX = tab_bar->BarRect.Min.x + sections[0].Width + sections[0].Spacing; + tab_bar->ScrollingRectMaxX = tab_bar->BarRect.Max.x - sections[2].Width - sections[1].Spacing; // Clear name buffers if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) @@ -7140,17 +7248,23 @@ void ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id) // Called on manual closure attempt void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { - if ((tab_bar->VisibleTabId == tab->ID) && !(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) + IM_ASSERT(!(tab->Flags & ImGuiTabItemFlags_Button)); + if (!(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) { // This will remove a frame of lag for selecting another tab on closure. // However we don't run it in the case where the 'Unsaved' flag is set, so user gets a chance to fully undo the closure - tab->LastFrameVisible = -1; - tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = 0; + tab->WantClose = true; + if (tab_bar->VisibleTabId == tab->ID) + { + tab->LastFrameVisible = -1; + tab_bar->SelectedTabId = tab_bar->NextSelectedTabId = 0; + } } - else if ((tab_bar->VisibleTabId != tab->ID) && (tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) + else { - // Actually select before expecting closure - tab_bar->NextSelectedTabId = tab->ID; + // Actually select before expecting closure attempt (on an UnsavedDocument tab user is expect to e.g. show a popup) + if (tab_bar->VisibleTabId != tab->ID) + tab_bar->NextSelectedTabId = tab->ID; } } @@ -7160,23 +7274,34 @@ static float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling) return ImMax(scrolling, 0.0f); } -static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) +static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImGuiTabBarSection* sections) { + if (tab->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) + return; + ImGuiContext& g = *GImGui; float margin = g.FontSize * 1.0f; // When to scroll to make Tab N+1 visible always make a bit of N visible to suggest more scrolling area (since we don't have a scrollbar) int order = tab_bar->GetTabOrder(tab); - float tab_x1 = tab->Offset + (order > 0 ? -margin : 0.0f); - float tab_x2 = tab->Offset + tab->Width + (order + 1 < tab_bar->Tabs.Size ? margin : 1.0f); + + // Scrolling happens only in the central section (leading/trailing sections are not scrolling) + // FIXME: This is all confusing. + float scrollable_width = tab_bar->BarRect.GetWidth() - sections[0].Width - sections[2].Width - sections[1].Spacing; + + // We make all tabs positions all relative Sections[0].Width to make code simpler + float tab_x1 = tab->Offset - sections[0].Width + (order > sections[0].TabCount - 1 ? -margin : 0.0f); + float tab_x2 = tab->Offset - sections[0].Width + tab->Width + (order + 1 < tab_bar->Tabs.Size - sections[2].TabCount ? margin : 1.0f); tab_bar->ScrollingTargetDistToVisibility = 0.0f; - if (tab_bar->ScrollingTarget > tab_x1 || (tab_x2 - tab_x1 >= tab_bar->BarRect.GetWidth())) + if (tab_bar->ScrollingTarget > tab_x1 || (tab_x2 - tab_x1 >= scrollable_width)) { + // Scroll to the left tab_bar->ScrollingTargetDistToVisibility = ImMax(tab_bar->ScrollingAnim - tab_x2, 0.0f); tab_bar->ScrollingTarget = tab_x1; } - else if (tab_bar->ScrollingTarget < tab_x2 - tab_bar->BarRect.GetWidth()) + else if (tab_bar->ScrollingTarget < tab_x2 - scrollable_width) { - tab_bar->ScrollingTargetDistToVisibility = ImMax((tab_x1 - tab_bar->BarRect.GetWidth()) - tab_bar->ScrollingAnim, 0.0f); - tab_bar->ScrollingTarget = tab_x2 - tab_bar->BarRect.GetWidth(); + // Scroll to the right + tab_bar->ScrollingTargetDistToVisibility = ImMax((tab_x1 - scrollable_width) - tab_bar->ScrollingAnim, 0.0f); + tab_bar->ScrollingTarget = tab_x2 - scrollable_width; } } @@ -7191,7 +7316,7 @@ void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, in bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) { ImGuiTabItem* tab1 = TabBarFindTabByID(tab_bar, tab_bar->ReorderRequestTabId); - if (!tab1) + if (tab1 == NULL || (tab1->Flags & ImGuiTabItemFlags_NoReorder)) return false; //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools @@ -7199,11 +7324,16 @@ bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) if (tab2_order < 0 || tab2_order >= tab_bar->Tabs.Size) return false; + // Reordered TabItem must share the same position flags than target ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; + if (tab2->Flags & ImGuiTabItemFlags_NoReorder) + return false; + if ((tab1->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) != (tab2->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing))) + return false; + ImGuiTabItem item_tmp = *tab1; *tab1 = *tab2; *tab2 = item_tmp; - tab1 = tab2 = NULL; if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) MarkIniSettingsDirty(); @@ -7221,13 +7351,6 @@ static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) const ImVec2 backup_cursor_pos = window->DC.CursorPos; //window->DrawList->AddRect(ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y), ImVec2(tab_bar->BarRect.Max.x, tab_bar->BarRect.Max.y), IM_COL32(255,0,0,255)); - const ImRect avail_bar_rect = tab_bar->BarRect; - bool want_clip_rect = !avail_bar_rect.Contains(ImRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(scrolling_buttons_width, 0.0f))); - if (want_clip_rect) - PushClipRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max + ImVec2(g.Style.ItemInnerSpacing.x, 0.0f), true); - - ImGuiTabItem* tab_to_select = NULL; - int select_dir = 0; ImVec4 arrow_col = g.Style.Colors[ImGuiCol_Text]; arrow_col.w *= 0.5f; @@ -7238,30 +7361,44 @@ static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) const float backup_repeat_rate = g.IO.KeyRepeatRate; g.IO.KeyRepeatDelay = 0.250f; g.IO.KeyRepeatRate = 0.200f; - window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width, tab_bar->BarRect.Min.y); + float x = ImMax(tab_bar->BarRect.Min.x, tab_bar->BarRect.Max.x - scrolling_buttons_width); + window->DC.CursorPos = ImVec2(x, tab_bar->BarRect.Min.y); if (ArrowButtonEx("##<", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) select_dir = -1; - window->DC.CursorPos = ImVec2(tab_bar->BarRect.Max.x - scrolling_buttons_width + arrow_button_size.x, tab_bar->BarRect.Min.y); + window->DC.CursorPos = ImVec2(x + arrow_button_size.x, tab_bar->BarRect.Min.y); if (ArrowButtonEx("##>", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) select_dir = +1; PopStyleColor(2); g.IO.KeyRepeatRate = backup_repeat_rate; g.IO.KeyRepeatDelay = backup_repeat_delay; - if (want_clip_rect) - PopClipRect(); - + ImGuiTabItem* tab_to_scroll_to = NULL; if (select_dir != 0) if (ImGuiTabItem* tab_item = TabBarFindTabByID(tab_bar, tab_bar->SelectedTabId)) { int selected_order = tab_bar->GetTabOrder(tab_item); int target_order = selected_order + select_dir; - tab_to_select = &tab_bar->Tabs[(target_order >= 0 && target_order < tab_bar->Tabs.Size) ? target_order : selected_order]; // If we are at the end of the list, still scroll to make our tab visible + + // Skip tab item buttons until another tab item is found or end is reached + while (tab_to_scroll_to == NULL) + { + // If we are at the end of the list, still scroll to make our tab visible + tab_to_scroll_to = &tab_bar->Tabs[(target_order >= 0 && target_order < tab_bar->Tabs.Size) ? target_order : selected_order]; + + // Cross through buttons + // (even if first/last item is a button, return it so we can update the scroll) + if (tab_to_scroll_to->Flags & ImGuiTabItemFlags_Button) + { + target_order += select_dir; + selected_order += select_dir; + tab_to_scroll_to = (target_order < 0 || target_order >= tab_bar->Tabs.Size) ? tab_to_scroll_to : NULL; + } + } } window->DC.CursorPos = backup_cursor_pos; tab_bar->BarRect.Max.x -= scrolling_buttons_width + 1.0f; - return tab_to_select; + return tab_to_scroll_to; } static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) @@ -7288,6 +7425,9 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + if (tab->Flags & ImGuiTabItemFlags_Button) + continue; + const char* tab_name = tab_bar->GetTabName(tab); if (Selectable(tab_name, tab_bar->SelectedTabId == tab->ID)) tab_to_select = tab; @@ -7304,6 +7444,7 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) //------------------------------------------------------------------------- // - BeginTabItem() // - EndTabItem() +// - TabItemButton() // - TabItemEx() [Internal] // - SetTabItemClosed() // - TabItemCalcSize() [Internal] @@ -7324,6 +7465,8 @@ bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags f IM_ASSERT_USER_ERROR(tab_bar, "Needs to be called between BeginTabBar() and EndTabBar()!"); return false; } + IM_ASSERT(!(flags & ImGuiTabItemFlags_Button)); // BeginTabItem() Can't be used with button flags, use TabItemButton() instead! + bool ret = TabItemEx(tab_bar, label, p_open, flags); if (ret && !(flags & ImGuiTabItemFlags_NoPushId)) { @@ -7349,7 +7492,23 @@ void ImGui::EndTabItem() IM_ASSERT(tab_bar->LastTabItemIdx >= 0); ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; if (!(tab->Flags & ImGuiTabItemFlags_NoPushId)) - window->IDStack.pop_back(); + PopID(); +} + +bool ImGui::TabItemButton(const char* label, ImGuiTabItemFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + ImGuiTabBar* tab_bar = g.CurrentTabBar; + if (tab_bar == NULL) + { + IM_ASSERT_USER_ERROR(tab_bar != NULL, "Needs to be called between BeginTabBar() and EndTabBar()!"); + return false; + } + return TabItemEx(tab_bar, label, NULL, flags | ImGuiTabItemFlags_Button | ImGuiTabItemFlags_NoReorder); } bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags) @@ -7377,6 +7536,9 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, return false; } + IM_ASSERT(!p_open || !(flags & ImGuiTabItemFlags_Button)); + IM_ASSERT((flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) != (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)); // Can't use both Leading and Trailing + // Store into ImGuiTabItemFlags_NoCloseButton, also honor ImGuiTabItemFlags_NoCloseButton passed by user (although not documented) if (flags & ImGuiTabItemFlags_NoCloseButton) p_open = NULL; @@ -7395,15 +7557,17 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab = &tab_bar->Tabs.back(); tab->ID = id; tab->Width = size.x; + tab_bar->TabsAddedNew = true; tab_is_new = true; } - tab_bar->LastTabItemIdx = (short)tab_bar->Tabs.index_from_ptr(tab); + tab_bar->LastTabItemIdx = (ImS16)tab_bar->Tabs.index_from_ptr(tab); tab->ContentWidth = size.x; tab->BeginOrder = tab_bar->TabsActiveCount++; const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; const bool tab_appearing = (tab->LastFrameVisible + 1 < g.FrameCount); + const bool is_tab_button = (flags & ImGuiTabItemFlags_Button) != 0; tab->LastFrameVisible = g.FrameCount; tab->Flags = flags; @@ -7411,20 +7575,14 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab->NameOffset = (ImS16)tab_bar->TabsNames.size(); tab_bar->TabsNames.append(label, label + strlen(label) + 1); - // If we are not reorderable, always reset offset based on submission order. - // (We already handled layout and sizing using the previous known order, but sizing is not affected by order!) - if (!tab_appearing && !(tab_bar->Flags & ImGuiTabBarFlags_Reorderable)) - { - tab->Offset = tab_bar->OffsetNextTab; - tab_bar->OffsetNextTab += tab->Width + g.Style.ItemInnerSpacing.x; - } - // Update selected tab if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) - tab_bar->NextSelectedTabId = id; // New tabs gets activated + if (!is_tab_button) + tab_bar->NextSelectedTabId = id; // New tabs gets activated if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // SetSelected can only be passed on explicit tab bar - tab_bar->NextSelectedTabId = id; + if (!is_tab_button) + tab_bar->NextSelectedTabId = id; // Lock visibility // (Note: tab_contents_visible != tab_selected... because CTRL+TAB operations may preview some tabs without selecting them!) @@ -7444,6 +7602,8 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); ItemAdd(ImRect(), id); PopItemFlag(); + if (is_tab_button) + return false; return tab_contents_visible; } @@ -7454,15 +7614,19 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const ImVec2 backup_main_cursor_pos = window->DC.CursorPos; // Layout + const bool is_central_section = (tab->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) == 0; size.x = tab->Width; - window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_FLOOR(tab->Offset - tab_bar->ScrollingAnim), 0.0f); + if (is_central_section) + window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_FLOOR(tab->Offset - tab_bar->ScrollingAnim), 0.0f); + else + window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(tab->Offset, 0.0f); ImVec2 pos = window->DC.CursorPos; ImRect bb(pos, pos + size); // We don't have CPU clipping primitives to clip the CloseButton (until it becomes a texture), so need to add an extra draw call (temporary in the case of vertical animation) - bool want_clip_rect = (bb.Min.x < tab_bar->BarRect.Min.x) || (bb.Max.x > tab_bar->BarRect.Max.x); + const bool want_clip_rect = is_central_section && (bb.Min.x < tab_bar->ScrollingRectMinX || bb.Max.x > tab_bar->ScrollingRectMaxX); if (want_clip_rect) - PushClipRect(ImVec2(ImMax(bb.Min.x, tab_bar->BarRect.Min.x), bb.Min.y - 1), ImVec2(tab_bar->BarRect.Max.x, bb.Max.y), true); + PushClipRect(ImVec2(ImMax(bb.Min.x, tab_bar->ScrollingRectMinX), bb.Min.y - 1), ImVec2(tab_bar->ScrollingRectMaxX, bb.Max.y), true); ImVec2 backup_cursor_max_pos = window->DC.CursorMaxPos; ItemSize(bb.GetSize(), style.FramePadding.y); @@ -7477,12 +7641,12 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } // Click to Select a tab - ImGuiButtonFlags button_flags = (ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_AllowItemOverlap); + ImGuiButtonFlags button_flags = ((is_tab_button ? ImGuiButtonFlags_PressedOnClickRelease : ImGuiButtonFlags_PressedOnClick) | ImGuiButtonFlags_AllowItemOverlap); if (g.DragDropActive) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); - if (pressed) + if (pressed && !is_tab_button) tab_bar->NextSelectedTabId = id; hovered |= (g.HoveredId == id); @@ -7528,14 +7692,17 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1))) - tab_bar->NextSelectedTabId = id; + if (!is_tab_button) + tab_bar->NextSelectedTabId = id; if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton; // Render tab label, process close button - const ImGuiID close_button_id = p_open ? window->GetID((void*)((intptr_t)id + 1)) : 0; - bool just_closed = TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id, tab_contents_visible); + const ImGuiID close_button_id = p_open ? GetIDWithSeed("#CLOSE", NULL, id) : 0; + bool just_closed; + bool text_clipped; + TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id, tab_contents_visible, &just_closed, &text_clipped); if (just_closed && p_open != NULL) { *p_open = false; @@ -7549,10 +7716,13 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) - if (g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) + if (text_clipped && g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); + IM_ASSERT(!is_tab_button || !(tab_bar->SelectedTabId == tab->ID && is_tab_button)); // TabItemButton should not be selected + if (is_tab_button) + return pressed; return tab_contents_visible; } @@ -7591,7 +7761,7 @@ void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabI const float width = bb.GetWidth(); IM_UNUSED(flags); IM_ASSERT(width > 0.0f); - const float rounding = ImMax(0.0f, ImMin(g.Style.TabRounding, width * 0.5f - 1.0f)); + const float rounding = ImMax(0.0f, ImMin((flags & ImGuiTabItemFlags_Button) ? g.Style.FrameRounding : g.Style.TabRounding, width * 0.5f - 1.0f)); const float y1 = bb.Min.y + 1.0f; const float y2 = bb.Max.y - 1.0f; draw_list->PathLineTo(ImVec2(bb.Min.x, y2)); @@ -7611,12 +7781,18 @@ void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabI // Render text label (with custom clipping) + Unsaved Document marker + Close Button logic // We tend to lock style.FramePadding for a given tab-bar, hence the 'frame_padding' parameter. -bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible) +void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible, bool* out_just_closed, bool* out_text_clipped) { ImGuiContext& g = *GImGui; ImVec2 label_size = CalcTextSize(label, NULL, true); + + if (out_just_closed) + *out_just_closed = false; + if (out_text_clipped) + *out_text_clipped = false; + if (bb.GetWidth() <= 1.0f) - return false; + return; // In Style V2 we'll have full override of all colors per state (e.g. focused, selected) // But right now if you want to alter text color of tabs this is what you need to do. @@ -7637,6 +7813,13 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, } ImRect text_ellipsis_clip_bb = text_pixel_clip_bb; + // Return clipped state ignoring the close button + if (out_text_clipped) + { + *out_text_clipped = (text_ellipsis_clip_bb.Min.x + label_size.x) > text_pixel_clip_bb.Max.x; + //draw_list->AddCircle(text_ellipsis_clip_bb.Min, 3.0f, *out_text_clipped ? IM_COL32(255, 0, 0, 255) : IM_COL32(0, 255, 0, 255)); + } + // Close Button // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap() // 'hovered' will be true when hovering the Tab but NOT when hovering the close button @@ -7645,7 +7828,7 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, bool close_button_pressed = false; bool close_button_visible = false; if (close_button_id != 0) - if (is_contents_visible || bb.GetWidth() >= g.Style.TabMinWidthForUnselectedCloseButton) + if (is_contents_visible || bb.GetWidth() >= g.Style.TabMinWidthForCloseButton) if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == tab_id || g.ActiveId == close_button_id) close_button_visible = true; if (close_button_visible) @@ -7665,6 +7848,7 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, text_pixel_clip_bb.Max.x -= close_button_sz; } + // FIXME: if FramePadding is noticeably large, ellipsis_max_x will be wrong here (e.g. #3497), maybe for consistency that parameter of RenderTextEllipsis() shouldn't exist.. float ellipsis_max_x = close_button_visible ? text_pixel_clip_bb.Max.x : bb.Max.x - 1.0f; RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, text_pixel_clip_bb.Max.x, ellipsis_max_x, label, NULL, &label_size); @@ -7673,7 +7857,8 @@ bool ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, g.Style.Alpha = backup_alpha; #endif - return close_button_pressed; + if (out_just_closed) + *out_just_closed = close_button_pressed; } @@ -7723,19 +7908,19 @@ int ImGui::GetColumnsCount() return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1; } -float ImGui::GetColumnOffsetFromNorm(const ImGuiColumns* columns, float offset_norm) +float ImGui::GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm) { return offset_norm * (columns->OffMaxX - columns->OffMinX); } -float ImGui::GetColumnNormFromOffset(const ImGuiColumns* columns, float offset) +float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset) { return offset / (columns->OffMaxX - columns->OffMinX); } static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; -static float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index) +static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index) { // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning. @@ -7746,7 +7931,7 @@ static float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index) float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); - if ((columns->Flags & ImGuiColumnsFlags_NoPreserveWidths)) + if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths)) x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); return x; @@ -7755,7 +7940,7 @@ static float GetDraggedColumnOffset(ImGuiColumns* columns, int column_index) float ImGui::GetColumnOffset(int column_index) { ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns == NULL) return 0.0f; @@ -7768,7 +7953,7 @@ float ImGui::GetColumnOffset(int column_index) return x_offset; } -static float GetColumnWidthEx(ImGuiColumns* columns, int column_index, bool before_resize = false) +static float GetColumnWidthEx(ImGuiOldColumns* columns, int column_index, bool before_resize = false) { if (column_index < 0) column_index = columns->Current; @@ -7785,7 +7970,7 @@ float ImGui::GetColumnWidth(int column_index) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns == NULL) return GetContentRegionAvail().x; @@ -7798,17 +7983,17 @@ void ImGui::SetColumnOffset(int column_index, float offset) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) column_index = columns->Current; IM_ASSERT(column_index < columns->Columns.Size); - const bool preserve_width = !(columns->Flags & ImGuiColumnsFlags_NoPreserveWidths) && (column_index < columns->Count - 1); + const bool preserve_width = !(columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths) && (column_index < columns->Count - 1); const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; - if (!(columns->Flags & ImGuiColumnsFlags_NoForceWithinWindow)) + if (!(columns->Flags & ImGuiOldColumnFlags_NoForceWithinWindow)) offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index)); columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX); @@ -7819,7 +8004,7 @@ void ImGui::SetColumnOffset(int column_index, float offset) void ImGui::SetColumnWidth(int column_index, float width) { ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); if (column_index < 0) @@ -7830,11 +8015,11 @@ void ImGui::SetColumnWidth(int column_index, float width) void ImGui::PushColumnClipRect(int column_index) { ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (column_index < 0) column_index = columns->Current; - ImGuiColumnData* column = &columns->Columns[column_index]; + ImGuiOldColumnData* column = &columns->Columns[column_index]; PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false); } @@ -7842,7 +8027,7 @@ void ImGui::PushColumnClipRect(int column_index) void ImGui::PushColumnsBackground() { ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; @@ -7855,7 +8040,7 @@ void ImGui::PushColumnsBackground() void ImGui::PopColumnsBackground() { ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) return; @@ -7864,15 +8049,15 @@ void ImGui::PopColumnsBackground() columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); } -ImGuiColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) +ImGuiOldColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) { // We have few columns per window so for now we don't need bother much with turning this into a faster lookup. for (int n = 0; n < window->ColumnsStorage.Size; n++) if (window->ColumnsStorage[n].ID == id) return &window->ColumnsStorage[n]; - window->ColumnsStorage.push_back(ImGuiColumns()); - ImGuiColumns* columns = &window->ColumnsStorage.back(); + window->ColumnsStorage.push_back(ImGuiOldColumns()); + ImGuiOldColumns* columns = &window->ColumnsStorage.back(); columns->ID = id; return columns; } @@ -7890,7 +8075,7 @@ ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count) return id; } -void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlags flags) +void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -7900,7 +8085,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag // Acquire storage for the columns set ImGuiID id = GetColumnsID(str_id, columns_count); - ImGuiColumns* columns = FindOrCreateColumns(window, id); + ImGuiOldColumns* columns = FindOrCreateColumns(window, id); IM_ASSERT(columns->ID == id); columns->Current = 0; columns->Count = columns_count; @@ -7934,7 +8119,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag columns->Columns.reserve(columns_count + 1); for (int n = 0; n < columns_count + 1; n++) { - ImGuiColumnData column; + ImGuiOldColumnData column; column.OffsetNorm = n / (float)columns_count; columns->Columns.push_back(column); } @@ -7943,7 +8128,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiColumnsFlag for (int n = 0; n < columns_count; n++) { // Compute clipping rectangle - ImGuiColumnData* column = &columns->Columns[n]; + ImGuiOldColumnData* column = &columns->Columns[n]; float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n)); float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f); column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); @@ -7974,7 +8159,7 @@ void ImGui::NextColumn() return; ImGuiContext& g = *GImGui; - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns->Count == 1) { @@ -7991,7 +8176,7 @@ void ImGui::NextColumn() // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect() // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them), - ImGuiColumnData* column = &columns->Columns[columns->Current]; + ImGuiOldColumnData* column = &columns->Columns[columns->Current]; SetWindowClipRectBeforeSetChannel(window, column->ClipRect); columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); @@ -8026,7 +8211,7 @@ void ImGui::EndColumns() { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumns* columns = window->DC.CurrentColumns; IM_ASSERT(columns != NULL); PopItemWidth(); @@ -8036,16 +8221,16 @@ void ImGui::EndColumns() columns->Splitter.Merge(window->DrawList); } - const ImGuiColumnsFlags flags = columns->Flags; + const ImGuiOldColumnFlags flags = columns->Flags; columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); window->DC.CursorPos.y = columns->LineMaxY; - if (!(flags & ImGuiColumnsFlags_GrowParentContentsSize)) + if (!(flags & ImGuiOldColumnFlags_GrowParentContentsSize)) window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent // Draw columns borders and handle resize // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy bool is_being_resized = false; - if (!(flags & ImGuiColumnsFlags_NoBorder) && !window->SkipItems) + if (!(flags & ImGuiOldColumnFlags_NoBorder) && !window->SkipItems) { // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers. const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y); @@ -8053,7 +8238,7 @@ void ImGui::EndColumns() int dragging_column = -1; for (int n = 1; n < columns->Count; n++) { - ImGuiColumnData* column = &columns->Columns[n]; + ImGuiOldColumnData* column = &columns->Columns[n]; float x = window->Pos.x + GetColumnOffset(n); const ImGuiID column_id = columns->ID + ImGuiID(n); const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; @@ -8063,12 +8248,12 @@ void ImGui::EndColumns() continue; bool hovered = false, held = false; - if (!(flags & ImGuiColumnsFlags_NoResize)) + if (!(flags & ImGuiOldColumnFlags_NoResize)) { ButtonBehavior(column_hit_rect, column_id, &hovered, &held); if (hovered || held) g.MouseCursor = ImGuiMouseCursor_ResizeEW; - if (held && !(column->Flags & ImGuiColumnsFlags_NoResize)) + if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize)) dragging_column = n; } @@ -8098,15 +8283,14 @@ void ImGui::EndColumns() window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); } -// [2018-03: This is currently the only public API, while we are working on making BeginColumns/EndColumns user-facing] void ImGui::Columns(int columns_count, const char* id, bool border) { ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(columns_count >= 1); - ImGuiColumnsFlags flags = (border ? 0 : ImGuiColumnsFlags_NoBorder); - //flags |= ImGuiColumnsFlags_NoPreserveWidths; // NB: Legacy behavior - ImGuiColumns* columns = window->DC.CurrentColumns; + ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder); + //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior + ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) return; diff --git a/imgui/imstb_textedit.h b/imgui/imstb_textedit.h index e7205e77..76446709 100644 --- a/imgui/imstb_textedit.h +++ b/imgui/imstb_textedit.h @@ -148,6 +148,8 @@ // STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right // STB_TEXTEDIT_K_UP keyboard input to move cursor up // STB_TEXTEDIT_K_DOWN keyboard input to move cursor down +// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page +// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page // STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME // STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END // STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME @@ -170,10 +172,6 @@ // STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text // STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text // -// Todo: -// STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page -// STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page -// // Keyboard input must be encoded as a single integer value; e.g. a character code // and some bitflags that represent shift states. to simplify the interface, SHIFT must // be a bitflag, so we can test the shifted state of cursor movements to allow selection, @@ -337,6 +335,10 @@ typedef struct // each textfield keeps its own insert mode state. to keep an app-wide // insert mode, copy this value in/out of the app state + int row_count_per_page; + // page size in number of row. + // this value MUST be set to >0 for pageup or pagedown in multilines documents. + ///////////////////// // // private data @@ -855,12 +857,16 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, break; case STB_TEXTEDIT_K_DOWN: - case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: { + case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT: + case STB_TEXTEDIT_K_PGDOWN: + case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: { StbFindState find; StbTexteditRow row; - int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN; + int row_count = is_page ? state->row_count_per_page : 1; - if (state->single_line) { + if (!is_page && state->single_line) { // on windows, up&down in single-line behave like left&right key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT); goto retry; @@ -869,23 +875,25 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, if (sel) stb_textedit_prep_selection_at_cursor(state); else if (STB_TEXT_HAS_SELECTION(state)) - stb_textedit_move_to_last(str,state); + stb_textedit_move_to_last(str, state); // compute current position of cursor point stb_textedit_clamp(str, state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); - // now find character position down a row - if (find.length) { + for (j = 0; j < row_count; ++j) { + float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; + int start = find.first_char + find.length; + + if (find.length == 0) + break; // [DEAR IMGUI] // going down while being on the last line shouldn't bring us to that line end if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE) break; - float goal_x = state->has_preferred_x ? state->preferred_x : find.x; - float x; - int start = find.first_char + find.length; + // now find character position down a row state->cursor = start; STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); x = row.x0; @@ -907,17 +915,25 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, if (sel) state->select_end = state->cursor; + + // go to next line + find.first_char = find.first_char + find.length; + find.length = row.num_chars; } break; } case STB_TEXTEDIT_K_UP: - case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: { + case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: + case STB_TEXTEDIT_K_PGUP: + case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: { StbFindState find; StbTexteditRow row; - int i, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0; + int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP; + int row_count = is_page ? state->row_count_per_page : 1; - if (state->single_line) { + if (!is_page && state->single_line) { // on windows, up&down become left&right key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT); goto retry; @@ -932,11 +948,14 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, stb_textedit_clamp(str, state); stb_textedit_find_charpos(&find, str, state->cursor, state->single_line); - // can only go up if there's a previous row - if (find.prev_first != find.first_char) { + for (j = 0; j < row_count; ++j) { + float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x; + + // can only go up if there's a previous row + if (find.prev_first == find.first_char) + break; + // now find character position up a row - float goal_x = state->has_preferred_x ? state->preferred_x : find.x; - float x; state->cursor = find.prev_first; STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor); x = row.x0; @@ -958,6 +977,14 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, if (sel) state->select_end = state->cursor; + + // go to previous line + // (we need to scan previous line the hard way. maybe we could expose this as a new API function?) + prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0; + while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE) + --prev_scan; + find.first_char = find.prev_first; + find.prev_first = prev_scan; } break; } @@ -1081,10 +1108,6 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, state->has_preferred_x = 0; break; } - -// @TODO: -// STB_TEXTEDIT_K_PGUP - move cursor up a page -// STB_TEXTEDIT_K_PGDOWN - move cursor down a page } } @@ -1356,6 +1379,7 @@ static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_lin state->initialized = 1; state->single_line = (unsigned char) is_single_line; state->insert_mode = 0; + state->row_count_per_page = 0; } // API initialize diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index e49c97ad..c93d61fd 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -183,7 +183,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) glm::vec3 position, normal, color; glm::vec4 L(0); - while(true) + while(total_tfar < 0.1) { total_tfar += r.ray.tfar; From 535c6a3af04dce6d2359e30807cac10bffb949bc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 24 Nov 2020 12:27:40 +0100 Subject: [PATCH 0377/1018] center model --- src/viewer/che_viewer.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index d1add5ce..80237d1c 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -46,7 +46,25 @@ void che_viewer::reload() void che_viewer::update() { - if(normalize) mesh->normalize(); + if(normalize) mesh->normalize(); + + vertex pmin(INFINITY, INFINITY, INFINITY); + vertex pmax(0, 0, 0); + + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + const vertex & p = mesh->gt(v); + + pmin.x = min(pmin.x, p.x); + pmin.y = min(pmin.y, p.y); + pmin.z = min(pmin.z, p.z); + + pmax.x = max(pmax.x, p.x); + pmax.y = max(pmax.y, p.y); + pmax.z = max(pmax.z, p.z); + } + + translate(-(pmax + pmin) / 2); factor = mesh->mean_edge(); From 7be560aeae092ae41970e758b53b16a7fea72e58 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 25 Nov 2020 20:04:15 +0100 Subject: [PATCH 0378/1018] update cmake cuda, embree, armadillo --- CMakeLists.txt | 39 +++++++++++++++++---------------------- include/include_arma.h | 1 + 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e2588f5..e61a4f07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,14 +11,14 @@ endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -find_package(CUDA 11) -if(CUDA_FOUND) +find_package(CUDAToolkit 11) +if(CUDAToolkit_FOUND) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) add_definitions(-DGPROSHAN_CUDA) - include_directories(${CUDA_INCLUDE_DIRS}) + include_directories(${CUDAToolkit_INCLUDE_DIRS}) find_package(OptiX 7.1) if(OptiX_INCLUDE) @@ -26,14 +26,14 @@ if(CUDA_FOUND) add_definitions(-DGPROSHAN_OPTIX) include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) -endif(CUDA_FOUND) +endif(CUDAToolkit_FOUND) -find_package(EMBREE 3) -if(EMBREE_FOUND) +find_package(embree 3.0) +if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) - include_directories(${EMBREE_INCLUDE_DIRS}) -endif(EMBREE_FOUND) + include_directories(${embree_INCLUDE_DIRS}) +endif(embree_FOUND) find_package(Threads REQUIRED) @@ -51,10 +51,6 @@ find_package(SuiteSparse REQUIRED) set(THREADS_PREFER_PTHREAD_FLAG ON) -add_definitions(${EIGEN3_DEFINITIONS}) - -# ARMA warning ARMA_ALLOW_FAKE_GCC with g++-9 -add_definitions(-DARMA_ALLOW_FAKE_GCC) include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${GLEW_INCLUDE_DIRS}) @@ -99,22 +95,21 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp ${EMBREE_LIBRARY}) +target_link_libraries(gproshan_cpp embree) target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) -if(CUDA_FOUND) +if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/cuda/*.cu) add_library(gproshan_cu STATIC ${cu_sources}) target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) target_link_libraries(gproshan_cu gproshan_cpp) - target_link_libraries(gproshan_cu ${CUDA_LIBRARIES}) - target_link_libraries(gproshan_cu ${CUDA_CUDA_LIBRARY}) - target_link_libraries(gproshan_cu ${CUDA_cublas_LIBRARY}) - target_link_libraries(gproshan_cu ${CUDA_cusolver_LIBRARY}) - target_link_libraries(gproshan_cu ${CUDA_cusparse_LIBRARY}) -endif(CUDA_FOUND) + target_link_libraries(gproshan_cu CUDA::cudart) + target_link_libraries(gproshan_cu CUDA::cublas) + target_link_libraries(gproshan_cu CUDA::cusolver) + target_link_libraries(gproshan_cu CUDA::cusparse) +endif(CUDAToolkit_FOUND) add_executable(gproshan gproshan.cpp) add_executable(test_geodesics test_geodesics.cpp) @@ -124,7 +119,7 @@ target_link_libraries(gproshan gproshan_cpp) target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) -if(CUDA_FOUND) +if(CUDAToolkit_FOUND) set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 60 61 62) set_property(TARGET gproshan PROPERTY CUDA_ARCHITECTURES 60 61 62) set_property(TARGET test_geodesics PROPERTY CUDA_ARCHITECTURES 60 61 62) @@ -133,7 +128,7 @@ if(CUDA_FOUND) target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) -endif(CUDA_FOUND) +endif(CUDAToolkit_FOUND) file(MAKE_DIRECTORY tmp) diff --git a/include/include_arma.h b/include/include_arma.h index 0c64ade1..968af97d 100644 --- a/include/include_arma.h +++ b/include/include_arma.h @@ -3,6 +3,7 @@ #include "include.h" +#define ARMA_ALLOW_FAKE_GCC #include #ifdef NDEBUG From 04cc2384352432fa4eda9220fbfd64e76f2a8957 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 25 Nov 2020 20:12:25 +0100 Subject: [PATCH 0379/1018] Update README.md --- README.md | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/README.md b/README.md index 98312ffb..83a8a42e 100644 --- a/README.md +++ b/README.md @@ -72,20 +72,6 @@ We proposed a Dictionary Learning and Sparse Coding framework, to solve the prob } ``` -[A Dictionary Learning-based framework on Triangular Meshes](https://arxiv.org/abs/1810.08266) - -```bibtex -@ARTICLE{2018arXiv181008266F, - author = { {Fuentes Perez}, L.~J. and {Romero Calla}, L.~A. and {Montenegro}, A.~A. }, - title = { Dictionary Learning-based Inpainting on Triangular Meshes }, - journal = { ArXiv e-prints }, - eprint = { 1810.08266 }, - year = 2018, - month = oct, - url = { https://arxiv.org/abs/1810.08266 } -} -``` - ### Hole repairing We implemented repairing mesh holes in two steps: From deafb17857367b32ac81d83292c94214c40ea73f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 25 Nov 2020 20:24:50 +0100 Subject: [PATCH 0380/1018] fix link embree lib --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e61a4f07..a518d2bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,10 +95,13 @@ target_link_libraries(gproshan_cpp ${X11_X11_LIB}) target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp embree) target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) target_link_libraries(gproshan_cpp imgui) +if(embree_FOUND) + target_link_libraries(gproshan_cpp embree) +endif(embree_FOUND) + if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/cuda/*.cu) add_library(gproshan_cu STATIC ${cu_sources}) From 4ffe5fd9adb26d423d8e695a071c4a573110bc33 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 26 Nov 2020 14:57:13 +0100 Subject: [PATCH 0381/1018] organizing codes in modules/folders --- CMakeLists.txt | 6 +-- include/app_viewer.h | 42 ++++++++++--------- include/{ => features}/key_components.h | 4 +- include/{ => features}/key_points.h | 2 +- include/{ => features}/shot.h | 0 include/{cuda => geodesics}/che.cuh | 4 +- include/{ => geodesics}/dijkstra.h | 2 +- include/{ => geodesics}/geodesics.h | 2 +- include/{cuda => geodesics}/geodesics_ptp.cuh | 2 +- include/{ => geodesics}/geodesics_ptp.h | 2 +- .../geodesics_ptp_coalescence.cuh | 6 +-- include/{ => geodesics}/heat_flow.h | 2 +- include/{ => geodesics}/sampling.h | 4 +- .../test_geodesics_ptp.cuh | 2 +- include/{ => geodesics}/test_geodesics_ptp.h | 4 +- .../test_geodesics_ptp_coalescence.cuh | 2 +- include/{cuda => geodesics}/vertex.cuh | 0 include/{ => laplacian}/fairing.h | 2 +- include/{ => laplacian}/fairing_spectral.h | 2 +- include/{ => laplacian}/fairing_taubin.h | 2 +- include/{ => laplacian}/laplacian.h | 2 +- include/mdict/basis_cosine.h | 2 +- include/mdict/basis_dct.h | 2 +- include/mdict/d_mesh.h | 6 +-- include/mdict/denoising.h | 2 +- include/mdict/dictionary.h | 10 ++--- include/mdict/image_denoising.h | 2 +- include/mdict/inpainting.h | 6 +-- include/mdict/mdict.h | 4 +- include/mdict/patch.h | 2 +- include/mdict/super_resolution.h | 2 +- include/{ => mesh}/che.h | 2 +- include/{ => mesh}/che_fill_hole.h | 2 +- include/{ => mesh}/che_img.h | 2 +- include/{ => mesh}/che_obj.h | 2 +- include/{ => mesh}/che_off.h | 2 +- include/{ => mesh}/che_ply.h | 2 +- include/{ => mesh}/che_poisson.h | 2 +- include/{ => mesh}/che_ptx.h | 2 +- include/{ => mesh}/che_sphere.h | 2 +- include/{ => mesh}/decimation.h | 2 +- include/{ => mesh}/kdtree.h | 2 +- include/{ => mesh}/quaternion.h | 2 +- include/{ => mesh}/vertex.h | 0 include/raytracing/raytracing.h | 2 +- include/raytracing/rt_embree.h | 4 +- include/raytracing/rt_optix.h | 4 +- include/viewer/camera.h | 4 +- include/viewer/che_viewer.h | 6 +-- include/viewer/frame.h | 4 +- include/viewer/shader.h | 2 +- include/viewer/viewer.h | 14 +++---- src/{ => features}/key_components.cpp | 4 +- src/{ => features}/key_points.cpp | 2 +- src/features/shot.cpp | 3 ++ src/{cuda => geodesics}/che.cu | 2 +- src/{ => geodesics}/dijkstra.cpp | 2 +- src/{ => geodesics}/geodesics.cpp | 6 +-- src/{ => geodesics}/geodesics_ptp.cpp | 2 +- src/{cuda => geodesics}/geodesics_ptp.cu | 4 +- .../geodesics_ptp_coalescence.cu | 4 +- src/{ => geodesics}/heat_flow.cpp | 4 +- src/{cuda => geodesics}/heat_flow.cu | 0 src/{ => geodesics}/sampling.cpp | 6 +-- src/{ => geodesics}/test_geodesics_ptp.cpp | 8 ++-- src/{cuda => geodesics}/test_geodesics_ptp.cu | 8 ++-- .../test_geodesics_ptp_coalescence.cu | 10 ++--- src/{cuda => geodesics}/vertex.cu | 2 +- src/{ => laplacian}/fairing.cpp | 2 +- src/{ => laplacian}/fairing_spectral.cpp | 4 +- src/{ => laplacian}/fairing_taubin.cpp | 4 +- src/{ => laplacian}/laplacian.cpp | 2 +- src/mdict/basis.cpp | 2 +- src/mdict/basis_cosine.cpp | 2 +- src/mdict/basis_dct.cpp | 2 +- src/mdict/d_mesh.cpp | 2 +- src/mdict/denoising.cpp | 2 +- src/mdict/dictionary.cpp | 10 ++--- src/mdict/image_denoising.cpp | 2 +- src/mdict/inpainting.cpp | 2 +- src/mdict/mdict.cpp | 4 +- src/mdict/patch.cpp | 4 +- src/mdict/super_resolution.cpp | 2 +- src/{ => mesh}/che.cpp | 4 +- src/{ => mesh}/che_fill_hole.cpp | 6 +-- src/{ => mesh}/che_img.cpp | 2 +- src/{ => mesh}/che_obj.cpp | 2 +- src/{ => mesh}/che_off.cpp | 2 +- src/{ => mesh}/che_ply.cpp | 2 +- src/{ => mesh}/che_poisson.cpp | 4 +- src/{ => mesh}/che_ptx.cpp | 2 +- src/{ => mesh}/che_sphere.cpp | 2 +- src/{ => mesh}/decimation.cpp | 2 +- src/{ => mesh}/kdtree.cpp | 2 +- src/{ => mesh}/quaternion.cpp | 2 +- src/{ => mesh}/vertex.cpp | 2 +- src/raytracing/raytracing.cpp | 2 +- src/raytracing/rt_embree.cpp | 2 +- src/raytracing/rt_optix.cpp | 2 +- src/raytracing/rt_optix.cu | 4 ++ src/shot.cpp | 3 -- src/viewer/camera.cpp | 2 +- src/viewer/che_viewer.cpp | 2 +- src/viewer/frame.cpp | 2 +- src/viewer/shader.cpp | 2 +- src/viewer/viewer.cpp | 10 ++--- test_geodesics.cpp | 2 +- 107 files changed, 190 insertions(+), 186 deletions(-) rename include/{ => features}/key_components.h (92%) rename include/{ => features}/key_points.h (96%) rename include/{ => features}/shot.h (100%) rename include/{cuda => geodesics}/che.cuh (90%) rename include/{ => geodesics}/dijkstra.h (95%) rename include/{ => geodesics}/geodesics.h (99%) rename include/{cuda => geodesics}/geodesics_ptp.cuh (97%) rename include/{ => geodesics}/geodesics_ptp.h (98%) rename include/{cuda => geodesics}/geodesics_ptp_coalescence.cuh (88%) rename include/{ => geodesics}/heat_flow.h (99%) rename include/{ => geodesics}/sampling.h (85%) rename include/{cuda => geodesics}/test_geodesics_ptp.cuh (95%) rename include/{ => geodesics}/test_geodesics_ptp.h (97%) rename include/{cuda => geodesics}/test_geodesics_ptp_coalescence.cuh (95%) rename include/{cuda => geodesics}/vertex.cuh (100%) rename include/{ => laplacian}/fairing.h (94%) rename include/{ => laplacian}/fairing_spectral.h (92%) rename include/{ => laplacian}/fairing_taubin.h (92%) rename include/{ => laplacian}/laplacian.h (95%) rename include/{ => mesh}/che.h (99%) rename include/{ => mesh}/che_fill_hole.h (99%) rename include/{ => mesh}/che_img.h (94%) rename include/{ => mesh}/che_obj.h (95%) rename include/{ => mesh}/che_off.h (96%) rename include/{ => mesh}/che_ply.h (95%) rename include/{ => mesh}/che_poisson.h (95%) rename include/{ => mesh}/che_ptx.h (95%) rename include/{ => mesh}/che_sphere.h (93%) rename include/{ => mesh}/decimation.h (96%) rename include/{ => mesh}/kdtree.h (94%) rename include/{ => mesh}/quaternion.h (98%) rename include/{ => mesh}/vertex.h (100%) rename src/{ => features}/key_components.cpp (95%) rename src/{ => features}/key_points.cpp (97%) create mode 100644 src/features/shot.cpp rename src/{cuda => geodesics}/che.cu (98%) rename src/{ => geodesics}/dijkstra.cpp (98%) rename src/{ => geodesics}/geodesics.cpp (98%) rename src/{ => geodesics}/geodesics_ptp.cpp (99%) rename src/{cuda => geodesics}/geodesics_ptp.cu (99%) rename src/{cuda => geodesics}/geodesics_ptp_coalescence.cu (98%) rename src/{ => geodesics}/heat_flow.cpp (98%) rename src/{cuda => geodesics}/heat_flow.cu (100%) rename src/{ => geodesics}/sampling.cpp (94%) rename src/{ => geodesics}/test_geodesics_ptp.cpp (98%) rename src/{cuda => geodesics}/test_geodesics_ptp.cu (97%) rename src/{cuda => geodesics}/test_geodesics_ptp_coalescence.cu (96%) rename src/{cuda => geodesics}/vertex.cu (87%) rename src/{ => laplacian}/fairing.cpp (91%) rename src/{ => laplacian}/fairing_spectral.cpp (91%) rename src/{ => laplacian}/fairing_taubin.cpp (92%) rename src/{ => laplacian}/laplacian.cpp (98%) rename src/{ => mesh}/che.cpp (99%) rename src/{ => mesh}/che_fill_hole.cpp (99%) rename src/{ => mesh}/che_img.cpp (98%) rename src/{ => mesh}/che_obj.cpp (98%) rename src/{ => mesh}/che_off.cpp (98%) rename src/{ => mesh}/che_ply.cpp (99%) rename src/{ => mesh}/che_poisson.cpp (98%) rename src/{ => mesh}/che_ptx.cpp (97%) rename src/{ => mesh}/che_sphere.cpp (98%) rename src/{ => mesh}/decimation.cpp (99%) rename src/{ => mesh}/kdtree.cpp (95%) rename src/{ => mesh}/quaternion.cpp (99%) rename src/{ => mesh}/vertex.cpp (98%) delete mode 100644 src/shot.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a518d2bd..e5445cb2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,10 +62,6 @@ include_directories(${SuiteSparse_INCLUDE_DIRS}) include_directories(${CGAL_INCLUDE_DIRS}) include_directories(${gproshan_SOURCE_DIR}/include) -include_directories(${gproshan_SOURCE_DIR}/include/viewer) -include_directories(${gproshan_SOURCE_DIR}/include/mdict) -include_directories(${gproshan_SOURCE_DIR}/include/cuda) -include_directories(${gproshan_SOURCE_DIR}/include/raytracing) include_directories(${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) @@ -103,7 +99,7 @@ if(embree_FOUND) endif(embree_FOUND) if(CUDAToolkit_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/cuda/*.cu) + FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) add_library(gproshan_cu STATIC ${cu_sources}) target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) diff --git a/include/app_viewer.h b/include/app_viewer.h index 920c7f88..64b5cce8 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -1,31 +1,35 @@ #ifndef APP_VIEWER_H #define APP_VIEWER_H -#include "viewer/viewer.h" #include "include.h" -#include "che_off.h" -#include "che_obj.h" -#include "che_ply.h" -#include "che_ptx.h" -#include "che_img.h" -#include "che_sphere.h" -#include "laplacian.h" -#include "che_off.h" -#include "dijkstra.h" -#include "geodesics.h" -#include "fairing_taubin.h" -#include "fairing_spectral.h" -#include "sampling.h" -#include "che_fill_hole.h" -#include "che_poisson.h" -#include "decimation.h" +#include "viewer/viewer.h" + +#include "mesh/che_off.h" +#include "mesh/che_obj.h" +#include "mesh/che_ply.h" +#include "mesh/che_ptx.h" +#include "mesh/che_img.h" +#include "mesh/che_sphere.h" +#include "mesh/che_fill_hole.h" +#include "mesh/che_poisson.h" +#include "mesh/decimation.h" + +#include "laplacian/laplacian.h" +#include "laplacian/fairing_taubin.h" +#include "laplacian/fairing_spectral.h" + +#include "geodesics/dijkstra.h" +#include "geodesics/geodesics.h" +#include "geodesics/sampling.h" + #include "mdict/denoising.h" #include "mdict/super_resolution.h" #include "mdict/inpainting.h" #include "mdict/basis_dct.h" #include "mdict/patch.h" -#include "key_points.h" -#include "key_components.h" + +#include "features/key_points.h" +#include "features/key_components.h" // geometry processing and shape analysis framework diff --git a/include/key_components.h b/include/features/key_components.h similarity index 92% rename from include/key_components.h rename to include/features/key_components.h index 424f36b3..6a88057a 100644 --- a/include/key_components.h +++ b/include/features/key_components.h @@ -1,8 +1,8 @@ #ifndef KEY_COMPONENTS_H #define KEY_COMPONENTS_H -#include "che.h" -#include "key_points.h" +#include "mesh/che.h" +#include "features/key_points.h" #include diff --git a/include/key_points.h b/include/features/key_points.h similarity index 96% rename from include/key_points.h rename to include/features/key_points.h index 89652c72..dac54375 100644 --- a/include/key_points.h +++ b/include/features/key_points.h @@ -1,7 +1,7 @@ #ifndef KEY_POINTS_H #define KEY_POINTS_H -#include "che.h" +#include "mesh/che.h" #include diff --git a/include/shot.h b/include/features/shot.h similarity index 100% rename from include/shot.h rename to include/features/shot.h diff --git a/include/cuda/che.cuh b/include/geodesics/che.cuh similarity index 90% rename from include/cuda/che.cuh rename to include/geodesics/che.cuh index cf35c1c1..f898fd68 100644 --- a/include/cuda/che.cuh +++ b/include/geodesics/che.cuh @@ -1,8 +1,8 @@ #ifndef CHE_CUH #define CHE_CUH -#include "vertex.cuh" -#include "../che.h" +#include "geodesics/vertex.cuh" +#include "../mesh/che.h" #define cu_for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[cu_prev(he)]) != stop ? he : NIL) diff --git a/include/dijkstra.h b/include/geodesics/dijkstra.h similarity index 95% rename from include/dijkstra.h rename to include/geodesics/dijkstra.h index ea376ebe..c3117a50 100644 --- a/include/dijkstra.h +++ b/include/geodesics/dijkstra.h @@ -1,7 +1,7 @@ #ifndef DIJKSTRA_H #define DIJKSTRA_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/geodesics.h b/include/geodesics/geodesics.h similarity index 99% rename from include/geodesics.h rename to include/geodesics/geodesics.h index 85b1e807..773b7979 100644 --- a/include/geodesics.h +++ b/include/geodesics/geodesics.h @@ -1,7 +1,7 @@ #ifndef GEODESICS_H #define GEODESICS_H -#include "che.h" +#include "mesh/che.h" #include "include_arma.h" diff --git a/include/cuda/geodesics_ptp.cuh b/include/geodesics/geodesics_ptp.cuh similarity index 97% rename from include/cuda/geodesics_ptp.cuh rename to include/geodesics/geodesics_ptp.cuh index 17771c18..b1f519f7 100644 --- a/include/cuda/geodesics_ptp.cuh +++ b/include/geodesics/geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef GEODESICS_PTP_CUH #define GEODESICS_PTP_CUH -#include "che.cuh" +#include "geodesics/che.cuh" #define NT 64 diff --git a/include/geodesics_ptp.h b/include/geodesics/geodesics_ptp.h similarity index 98% rename from include/geodesics_ptp.h rename to include/geodesics/geodesics_ptp.h index 3abf5dd7..0015067a 100644 --- a/include/geodesics_ptp.h +++ b/include/geodesics/geodesics_ptp.h @@ -6,7 +6,7 @@ topleset: (Topological Level Set) */ -#include "che.h" +#include "mesh/che.h" #define PTP_TOL 1e-4 diff --git a/include/cuda/geodesics_ptp_coalescence.cuh b/include/geodesics/geodesics_ptp_coalescence.cuh similarity index 88% rename from include/cuda/geodesics_ptp_coalescence.cuh rename to include/geodesics/geodesics_ptp_coalescence.cuh index 90ee1bcc..ae627f42 100644 --- a/include/cuda/geodesics_ptp_coalescence.cuh +++ b/include/geodesics/geodesics_ptp_coalescence.cuh @@ -1,9 +1,9 @@ #ifndef GEODESICS_PTP_COALESCENCE_CUH #define GEODESICS_PTP_COALESCENCE_CUH -#include "che.cuh" -#include "geodesics_ptp.cuh" -#include "geodesics_ptp.h" +#include "geodesics/che.cuh" +#include "geodesics/geodesics_ptp.cuh" +#include "geodesics/geodesics_ptp.h" // geometry processing and shape analysis framework diff --git a/include/heat_flow.h b/include/geodesics/heat_flow.h similarity index 99% rename from include/heat_flow.h rename to include/geodesics/heat_flow.h index 66de7646..06f3af66 100644 --- a/include/heat_flow.h +++ b/include/geodesics/heat_flow.h @@ -10,7 +10,7 @@ #ifndef HEAT_FLOW_H #define HEAT_FLOW_H -#include "che.h" +#include "mesh/che.h" #include "include_arma.h" #include // suitesparse/cholmod.h diff --git a/include/sampling.h b/include/geodesics/sampling.h similarity index 85% rename from include/sampling.h rename to include/geodesics/sampling.h index ffa7e139..7cd41389 100644 --- a/include/sampling.h +++ b/include/geodesics/sampling.h @@ -1,8 +1,8 @@ #ifndef SAMPLING_H #define SAMPLING_H -#include "geodesics.h" -#include "geodesics_ptp.h" +#include "geodesics/geodesics.h" +#include "geodesics/geodesics_ptp.h" #include diff --git a/include/cuda/test_geodesics_ptp.cuh b/include/geodesics/test_geodesics_ptp.cuh similarity index 95% rename from include/cuda/test_geodesics_ptp.cuh rename to include/geodesics/test_geodesics_ptp.cuh index 66ef6922..9e64444e 100644 --- a/include/cuda/test_geodesics_ptp.cuh +++ b/include/geodesics/test_geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_CUH #define TEST_GEODESICS_PTP_CUH -#include "che.cuh" +#include "geodesics/che.cuh" // geometry processing and shape analysis framework diff --git a/include/test_geodesics_ptp.h b/include/geodesics/test_geodesics_ptp.h similarity index 97% rename from include/test_geodesics_ptp.h rename to include/geodesics/test_geodesics_ptp.h index 4ba9a050..b4f629c6 100644 --- a/include/test_geodesics_ptp.h +++ b/include/geodesics/test_geodesics_ptp.h @@ -1,8 +1,8 @@ #ifndef TEST_GEODESICS_PTP_H #define TEST_GEODESICS_PTP_H -#include "geodesics.h" -#include "geodesics_ptp.h" +#include "geodesics/geodesics.h" +#include "geodesics/geodesics_ptp.h" // geometry processing and shape analysis framework diff --git a/include/cuda/test_geodesics_ptp_coalescence.cuh b/include/geodesics/test_geodesics_ptp_coalescence.cuh similarity index 95% rename from include/cuda/test_geodesics_ptp_coalescence.cuh rename to include/geodesics/test_geodesics_ptp_coalescence.cuh index dfd0ac9e..a3fa75a6 100644 --- a/include/cuda/test_geodesics_ptp_coalescence.cuh +++ b/include/geodesics/test_geodesics_ptp_coalescence.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_COALESCENCE_CUH #define TEST_GEODESICS_PTP_COALESCENCE_CUH -#include "che.cuh" +#include "geodesics/che.cuh" // geometry processing and shape analysis framework diff --git a/include/cuda/vertex.cuh b/include/geodesics/vertex.cuh similarity index 100% rename from include/cuda/vertex.cuh rename to include/geodesics/vertex.cuh diff --git a/include/fairing.h b/include/laplacian/fairing.h similarity index 94% rename from include/fairing.h rename to include/laplacian/fairing.h index ff32e868..46d31946 100644 --- a/include/fairing.h +++ b/include/laplacian/fairing.h @@ -1,7 +1,7 @@ #ifndef FAIRING_H #define FAIRING_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/fairing_spectral.h b/include/laplacian/fairing_spectral.h similarity index 92% rename from include/fairing_spectral.h rename to include/laplacian/fairing_spectral.h index 128be5b8..83ef034f 100644 --- a/include/fairing_spectral.h +++ b/include/laplacian/fairing_spectral.h @@ -1,7 +1,7 @@ #ifndef FAIRING_SPECTRAL_H #define FAIRING_SPECTRAL_H -#include "fairing.h" +#include "laplacian/fairing.h" // geometry processing and shape analysis framework diff --git a/include/fairing_taubin.h b/include/laplacian/fairing_taubin.h similarity index 92% rename from include/fairing_taubin.h rename to include/laplacian/fairing_taubin.h index 58a1ddc8..74d540f6 100644 --- a/include/fairing_taubin.h +++ b/include/laplacian/fairing_taubin.h @@ -1,7 +1,7 @@ #ifndef FAIRING_TAUBIN_H #define FAIRING_TAUBIN_H -#include "fairing.h" +#include "laplacian/fairing.h" // geometry processing and shape analysis framework diff --git a/include/laplacian.h b/include/laplacian/laplacian.h similarity index 95% rename from include/laplacian.h rename to include/laplacian/laplacian.h index a5143dc9..47abb2ea 100644 --- a/include/laplacian.h +++ b/include/laplacian/laplacian.h @@ -1,7 +1,7 @@ #ifndef LAPLACIAN_H #define LAPLACIAN_H -#include "che.h" +#include "mesh/che.h" #include "include_arma.h" #include diff --git a/include/mdict/basis_cosine.h b/include/mdict/basis_cosine.h index 5628ee4e..604804da 100644 --- a/include/mdict/basis_cosine.h +++ b/include/mdict/basis_cosine.h @@ -1,7 +1,7 @@ #ifndef BASIS_COSSINE_H #define BASIS_COSSINE_H -#include "basis.h" +#include "mdict/basis.h" // geometry processing and shape analysis framework diff --git a/include/mdict/basis_dct.h b/include/mdict/basis_dct.h index d9337cf2..1fdefd94 100644 --- a/include/mdict/basis_dct.h +++ b/include/mdict/basis_dct.h @@ -1,7 +1,7 @@ #ifndef BASIS_DCT_H #define BASIS_DCT_H -#include "basis.h" +#include "mdict/basis.h" // geometry processing and shape analysis framework diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h index b44c21b8..87ade74a 100644 --- a/include/mdict/d_mesh.h +++ b/include/mdict/d_mesh.h @@ -2,9 +2,9 @@ #define D_MESH_H #include "include.h" -#include "che.h" -#include "patch.h" -#include "geodesics.h" +#include "mesh/che.h" +#include "mdict/patch.h" +#include "geodesics/geodesics.h" #include "include_arma.h" diff --git a/include/mdict/denoising.h b/include/mdict/denoising.h index cfde58ac..10e3daa2 100644 --- a/include/mdict/denoising.h +++ b/include/mdict/denoising.h @@ -1,7 +1,7 @@ #ifndef DENOISING_H #define DENOISING_H -#include "dictionary.h" +#include "mdict/dictionary.h" // geometry processing and shape analysis framework diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 3dd26c20..84a897c0 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -1,11 +1,11 @@ #ifndef DICTIONARY_H #define DICTIONARY_H -#include "che.h" -#include "patch.h" -#include "mdict.h" -#include "basis.h" -#include "d_mesh.h" +#include "mesh/che.h" +#include "mdict/patch.h" +#include "mdict/mdict.h" +#include "mdict/basis.h" +#include "mdict/d_mesh.h" #include "include_arma.h" diff --git a/include/mdict/image_denoising.h b/include/mdict/image_denoising.h index d6b18075..8550a7d0 100644 --- a/include/mdict/image_denoising.h +++ b/include/mdict/image_denoising.h @@ -1,7 +1,7 @@ #ifndef IMAGE_DENOISING_H #define IMAGE_DENOISING_H -#include "mdict.h" +#include "mdict/mdict.h" // geometry processing and shape analysis framework diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index fccf36b5..03b65b46 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -1,9 +1,9 @@ #ifndef INPAINTING_H #define INPAINTING_H -#include "dictionary.h" -#include "../che_poisson.h" -#include "../che_fill_hole.h" +#include "mdict/dictionary.h" +#include "../mesh/che_poisson.h" +#include "../mesh/che_fill_hole.h" // geometry processing and shape analysis framework diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index 9e24779e..374f568b 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -2,8 +2,8 @@ #define MDICT_H #include "include.h" -#include "patch.h" -#include "d_mesh.h" +#include "mdict/patch.h" +#include "mdict/d_mesh.h" #include "include_arma.h" diff --git a/include/mdict/patch.h b/include/mdict/patch.h index b05ed9bc..d0400879 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -2,7 +2,7 @@ #define PATCH_H #include "include.h" -#include "che.h" +#include "mesh/che.h" #include #include "include_arma.h" diff --git a/include/mdict/super_resolution.h b/include/mdict/super_resolution.h index a49b69a3..23eca6e5 100644 --- a/include/mdict/super_resolution.h +++ b/include/mdict/super_resolution.h @@ -1,7 +1,7 @@ #ifndef SUPER_RESOLUTION_H #define SUPER_RESOLUTION_H -#include "dictionary.h" +#include "mdict/dictionary.h" // geometry processing and shape analysis framework diff --git a/include/che.h b/include/mesh/che.h similarity index 99% rename from include/che.h rename to include/mesh/che.h index f4d0957d..11af2560 100644 --- a/include/che.h +++ b/include/mesh/che.h @@ -2,7 +2,7 @@ #define CHE_H #include "include.h" -#include "vertex.h" +#include "mesh/vertex.h" #include #include diff --git a/include/che_fill_hole.h b/include/mesh/che_fill_hole.h similarity index 99% rename from include/che_fill_hole.h rename to include/mesh/che_fill_hole.h index 0f7a9232..49112c28 100644 --- a/include/che_fill_hole.h +++ b/include/mesh/che_fill_hole.h @@ -1,7 +1,7 @@ #ifndef CHE_FILL_HOLE_H #define CHE_FILL_HOLE_H -#include "che.h" +#include "mesh/che.h" #include "include_arma.h" #include diff --git a/include/che_img.h b/include/mesh/che_img.h similarity index 94% rename from include/che_img.h rename to include/mesh/che_img.h index a18af037..ba99409e 100644 --- a/include/che_img.h +++ b/include/mesh/che_img.h @@ -1,7 +1,7 @@ #ifndef CHE_IMG_H #define CHE_IMG_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_obj.h b/include/mesh/che_obj.h similarity index 95% rename from include/che_obj.h rename to include/mesh/che_obj.h index b7fe0d7c..22d4c1d6 100644 --- a/include/che_obj.h +++ b/include/mesh/che_obj.h @@ -1,7 +1,7 @@ #ifndef CHE_OBJ_H #define CHE_OBJ_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_off.h b/include/mesh/che_off.h similarity index 96% rename from include/che_off.h rename to include/mesh/che_off.h index 300930e0..924b2572 100644 --- a/include/che_off.h +++ b/include/mesh/che_off.h @@ -1,7 +1,7 @@ #ifndef CHE_OFF_H #define CHE_OFF_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_ply.h b/include/mesh/che_ply.h similarity index 95% rename from include/che_ply.h rename to include/mesh/che_ply.h index 705e56af..6a2400d6 100644 --- a/include/che_ply.h +++ b/include/mesh/che_ply.h @@ -1,7 +1,7 @@ #ifndef CHE_PLY_H #define CHE_PLY_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_poisson.h b/include/mesh/che_poisson.h similarity index 95% rename from include/che_poisson.h rename to include/mesh/che_poisson.h index ca97a928..c72ce321 100644 --- a/include/che_poisson.h +++ b/include/mesh/che_poisson.h @@ -1,7 +1,7 @@ #ifndef CHE_POISSON_H #define CHE_POISSON_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_ptx.h b/include/mesh/che_ptx.h similarity index 95% rename from include/che_ptx.h rename to include/mesh/che_ptx.h index 01fa9737..9c49827e 100644 --- a/include/che_ptx.h +++ b/include/mesh/che_ptx.h @@ -1,7 +1,7 @@ #ifndef CHE_PTX_H #define CHE_PTX_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/che_sphere.h b/include/mesh/che_sphere.h similarity index 93% rename from include/che_sphere.h rename to include/mesh/che_sphere.h index 89af9460..3a1247e1 100644 --- a/include/che_sphere.h +++ b/include/mesh/che_sphere.h @@ -1,7 +1,7 @@ #ifndef CHE_SPHERE_H #define CHE_SPHERE_H -#include "che.h" +#include "mesh/che.h" // geometry processing and shape analysis framework diff --git a/include/decimation.h b/include/mesh/decimation.h similarity index 96% rename from include/decimation.h rename to include/mesh/decimation.h index 1f78bd46..6c9a2b2d 100644 --- a/include/decimation.h +++ b/include/mesh/decimation.h @@ -1,7 +1,7 @@ #ifndef DECIMATION_H #define DECIMATION_H -#include "che.h" +#include "mesh/che.h" #include "include_arma.h" #include diff --git a/include/kdtree.h b/include/mesh/kdtree.h similarity index 94% rename from include/kdtree.h rename to include/mesh/kdtree.h index 9a169f7b..8e9ba6fc 100644 --- a/include/kdtree.h +++ b/include/mesh/kdtree.h @@ -1,7 +1,7 @@ #ifndef KDTREE_H #define KDTREE_H -#include "vertex.h" +#include "mesh/vertex.h" // geometry processing and shape analysis framework diff --git a/include/quaternion.h b/include/mesh/quaternion.h similarity index 98% rename from include/quaternion.h rename to include/mesh/quaternion.h index 557f1f67..e9ac3b67 100644 --- a/include/quaternion.h +++ b/include/mesh/quaternion.h @@ -1,7 +1,7 @@ #ifndef QUATERNION_H #define QUATERNION_H -#include "vertex.h" +#include "mesh/vertex.h" #include diff --git a/include/vertex.h b/include/mesh/vertex.h similarity index 100% rename from include/vertex.h rename to include/mesh/vertex.h diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index 530566e3..378eed89 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -1,7 +1,7 @@ #ifndef RAYTRACING_H #define RAYTRACING_H -#include "che.h" +#include "mesh/che.h" #include #include diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 0e345569..079673c2 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -3,8 +3,8 @@ #ifndef RT_EMBREE_H #define RT_EMBREE_H -#include "che.h" -#include "raytracing.h" +#include "mesh/che.h" +#include "raytracing/raytracing.h" #include #include diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 8a2bb4e1..bb3a43b3 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -3,8 +3,8 @@ #ifndef RT_OPTIX_H #define RT_OPTIX_H -#include "che.h" -#include "raytracing.h" +#include "mesh/che.h" +#include "raytracing/raytracing.h" #include #include diff --git a/include/viewer/camera.h b/include/viewer/camera.h index 9f44ae95..5e6bbd7c 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -1,9 +1,9 @@ #ifndef VIEWER_CAMERA_H #define VIEWER_CAMERA_H -#include "quaternion.h" +#include "mesh/quaternion.h" -#include "include_opengl.h" +#include "viewer/include_opengl.h" // geometry processing and shape analysis framework diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 8aebdc90..90154ef3 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -1,10 +1,10 @@ #ifndef CHE_VIEWER_H #define CHE_VIEWER_H -#include "che.h" -#include "shader.h" +#include "mesh/che.h" +#include "viewer/shader.h" -#include "include_opengl.h" +#include "viewer/include_opengl.h" #ifdef SINGLE_P diff --git a/include/viewer/frame.h b/include/viewer/frame.h index 2248792c..452104bb 100644 --- a/include/viewer/frame.h +++ b/include/viewer/frame.h @@ -2,9 +2,9 @@ #define VIEWER_FRAME_H -#include "shader.h" +#include "viewer/shader.h" -#include "include_opengl.h" +#include "viewer/include_opengl.h" // geometry processing and shape analysis framework diff --git a/include/viewer/shader.h b/include/viewer/shader.h index 4c8e9622..c23ba6c1 100644 --- a/include/viewer/shader.h +++ b/include/viewer/shader.h @@ -4,7 +4,7 @@ #include #include -#include "include_opengl.h" +#include "viewer/include_opengl.h" // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 98615aec..4af76da9 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -6,12 +6,12 @@ #include -#include "camera.h" -#include "shader.h" -#include "frame.h" -#include "che_viewer.h" +#include "viewer/camera.h" +#include "viewer/shader.h" +#include "viewer/frame.h" +#include "viewer/che_viewer.h" -#include "include_opengl.h" +#include "viewer/include_opengl.h" #include "imgui.h" #include "imgui_impl_glfw.h" @@ -19,11 +19,11 @@ #ifdef GPROSHAN_EMBREE - #include "rt_embree.h" + #include "raytracing/rt_embree.h" #endif // GPROSHAN_EMBREE #ifdef GPROSHAN_OPTIX - #include "rt_optix.h" + #include "raytracing/rt_optix.h" #endif // GPROSHAN_OPTIX diff --git a/src/key_components.cpp b/src/features/key_components.cpp similarity index 95% rename from src/key_components.cpp rename to src/features/key_components.cpp index 0d73773a..f458546b 100644 --- a/src/key_components.cpp +++ b/src/features/key_components.cpp @@ -1,6 +1,6 @@ -#include "key_components.h" +#include "features/key_components.h" -#include "geodesics.h" +#include "geodesics/geodesics.h" #include diff --git a/src/key_points.cpp b/src/features/key_points.cpp similarity index 97% rename from src/key_points.cpp rename to src/features/key_points.cpp index bb5b88a8..5a8bdfc9 100644 --- a/src/key_points.cpp +++ b/src/features/key_points.cpp @@ -1,4 +1,4 @@ -#include "key_points.h" +#include "features/key_points.h" #include #include diff --git a/src/features/shot.cpp b/src/features/shot.cpp new file mode 100644 index 00000000..02c05061 --- /dev/null +++ b/src/features/shot.cpp @@ -0,0 +1,3 @@ +#include "features/shot.h" + + diff --git a/src/cuda/che.cu b/src/geodesics/che.cu similarity index 98% rename from src/cuda/che.cu rename to src/geodesics/che.cu index 15919a89..be520aca 100644 --- a/src/cuda/che.cu +++ b/src/geodesics/che.cu @@ -1,4 +1,4 @@ -#include "che.cuh" +#include "geodesics/che.cuh" // geometry processing and shape analysis framework diff --git a/src/dijkstra.cpp b/src/geodesics/dijkstra.cpp similarity index 98% rename from src/dijkstra.cpp rename to src/geodesics/dijkstra.cpp index 1ac116f4..3039a0d5 100644 --- a/src/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -1,4 +1,4 @@ -#include "dijkstra.h" +#include "geodesics/dijkstra.h" #include #include diff --git a/src/geodesics.cpp b/src/geodesics/geodesics.cpp similarity index 98% rename from src/geodesics.cpp rename to src/geodesics/geodesics.cpp index 3957edca..77945070 100644 --- a/src/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -1,7 +1,7 @@ -#include "geodesics.h" -#include "geodesics_ptp.h" +#include "geodesics/geodesics.h" +#include "geodesics/geodesics_ptp.h" -#include "heat_flow.h" +#include "geodesics/heat_flow.h" #include #include diff --git a/src/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp similarity index 99% rename from src/geodesics_ptp.cpp rename to src/geodesics/geodesics_ptp.cpp index cb0fdfab..32bbbb0c 100644 --- a/src/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -1,4 +1,4 @@ -#include "geodesics_ptp.h" +#include "geodesics/geodesics_ptp.h" #include #include diff --git a/src/cuda/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu similarity index 99% rename from src/cuda/geodesics_ptp.cu rename to src/geodesics/geodesics_ptp.cu index 7e87578a..d7c49cce 100644 --- a/src/cuda/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -1,5 +1,5 @@ -#include "geodesics_ptp.cuh" -#include "geodesics_ptp.h" +#include "geodesics/geodesics_ptp.cuh" +#include "geodesics/geodesics_ptp.h" #include #include diff --git a/src/cuda/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu similarity index 98% rename from src/cuda/geodesics_ptp_coalescence.cu rename to src/geodesics/geodesics_ptp_coalescence.cu index 6e802375..19c97ad6 100644 --- a/src/cuda/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -1,6 +1,6 @@ -#include "geodesics_ptp_coalescence.cuh" +#include "geodesics/geodesics_ptp_coalescence.cuh" -#include "che_off.h" +#include "mesh/che_off.h" #include #include diff --git a/src/heat_flow.cpp b/src/geodesics/heat_flow.cpp similarity index 98% rename from src/heat_flow.cpp rename to src/geodesics/heat_flow.cpp index ec1b6298..7ddbca8e 100644 --- a/src/heat_flow.cpp +++ b/src/geodesics/heat_flow.cpp @@ -1,6 +1,6 @@ -#include "heat_flow.h" +#include "geodesics/heat_flow.h" -#include "laplacian.h" +#include "laplacian/laplacian.h" #include diff --git a/src/cuda/heat_flow.cu b/src/geodesics/heat_flow.cu similarity index 100% rename from src/cuda/heat_flow.cu rename to src/geodesics/heat_flow.cu diff --git a/src/sampling.cpp b/src/geodesics/sampling.cpp similarity index 94% rename from src/sampling.cpp rename to src/geodesics/sampling.cpp index 2ccfac98..a3d18a66 100644 --- a/src/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -1,7 +1,7 @@ -#include "sampling.h" +#include "geodesics/sampling.h" -#include "geodesics_ptp.h" -#include "che_off.h" +#include "geodesics/geodesics_ptp.h" +#include "mesh/che_off.h" #include diff --git a/src/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp similarity index 98% rename from src/test_geodesics_ptp.cpp rename to src/geodesics/test_geodesics_ptp.cpp index 681c1474..c662f052 100644 --- a/src/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -1,8 +1,8 @@ -#include "test_geodesics_ptp.h" +#include "geodesics/test_geodesics_ptp.h" -#include "che_off.h" -#include "geodesics_ptp.h" -#include "heat_flow.h" +#include "mesh/che_off.h" +#include "geodesics/geodesics_ptp.h" +#include "geodesics/heat_flow.h" #include diff --git a/src/cuda/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu similarity index 97% rename from src/cuda/test_geodesics_ptp.cu rename to src/geodesics/test_geodesics_ptp.cu index dcba0245..62aaa31f 100644 --- a/src/cuda/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -1,8 +1,8 @@ -#include "test_geodesics_ptp.cuh" -#include "test_geodesics_ptp.h" +#include "geodesics/test_geodesics_ptp.cuh" +#include "geodesics/test_geodesics_ptp.h" -#include "geodesics_ptp.cuh" -#include "geodesics_ptp.h" +#include "geodesics/geodesics_ptp.cuh" +#include "geodesics/geodesics_ptp.h" #include #include diff --git a/src/cuda/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu similarity index 96% rename from src/cuda/test_geodesics_ptp_coalescence.cu rename to src/geodesics/test_geodesics_ptp_coalescence.cu index 43bec37e..06cc92ba 100644 --- a/src/cuda/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -1,10 +1,10 @@ -#include "test_geodesics_ptp_coalescence.cuh" +#include "geodesics/test_geodesics_ptp_coalescence.cuh" -#include "geodesics_ptp_coalescence.cuh" -#include "geodesics_ptp.h" -#include "test_geodesics_ptp.h" +#include "geodesics/geodesics_ptp_coalescence.cuh" +#include "geodesics/geodesics_ptp.h" +#include "geodesics/test_geodesics_ptp.h" -#include "che_off.h" +#include "mesh/che_off.h" #include #include diff --git a/src/cuda/vertex.cu b/src/geodesics/vertex.cu similarity index 87% rename from src/cuda/vertex.cu rename to src/geodesics/vertex.cu index 5a9e339e..af7ef320 100644 --- a/src/cuda/vertex.cu +++ b/src/geodesics/vertex.cu @@ -1,4 +1,4 @@ -#include "vertex.cuh" +#include "geodesics/vertex.cuh" // geometry processing and shape analysis framework diff --git a/src/fairing.cpp b/src/laplacian/fairing.cpp similarity index 91% rename from src/fairing.cpp rename to src/laplacian/fairing.cpp index a230e643..68761a21 100644 --- a/src/fairing.cpp +++ b/src/laplacian/fairing.cpp @@ -1,4 +1,4 @@ -#include "fairing.h" +#include "laplacian/fairing.h" // geometry processing and shape analysis framework diff --git a/src/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp similarity index 91% rename from src/fairing_spectral.cpp rename to src/laplacian/fairing_spectral.cpp index de9d1694..d6f0539f 100644 --- a/src/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -1,6 +1,6 @@ -#include "fairing_spectral.h" +#include "laplacian/fairing_spectral.h" -#include "laplacian.h" +#include "laplacian/laplacian.h" // geometry processing and shape analysis framework diff --git a/src/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp similarity index 92% rename from src/fairing_taubin.cpp rename to src/laplacian/fairing_taubin.cpp index 324bb644..d19e81b2 100644 --- a/src/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -1,6 +1,6 @@ -#include "fairing_taubin.h" +#include "laplacian/fairing_taubin.h" -#include "laplacian.h" +#include "laplacian/laplacian.h" // geometry processing and shape analysis framework diff --git a/src/laplacian.cpp b/src/laplacian/laplacian.cpp similarity index 98% rename from src/laplacian.cpp rename to src/laplacian/laplacian.cpp index 12c33e0f..017c01ee 100644 --- a/src/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -1,4 +1,4 @@ -#include "laplacian.h" +#include "laplacian/laplacian.h" using namespace std; using namespace Eigen; diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index dd90ccf7..e0233336 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -1,4 +1,4 @@ -#include "basis.h" +#include "mdict/basis.h" // geometry processing and shape analysis framework diff --git a/src/mdict/basis_cosine.cpp b/src/mdict/basis_cosine.cpp index 082c0e7b..d6d1f48e 100644 --- a/src/mdict/basis_cosine.cpp +++ b/src/mdict/basis_cosine.cpp @@ -1,4 +1,4 @@ -#include "basis_cosine.h" +#include "mdict/basis_cosine.h" #include diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index dc41445d..c8f38072 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -1,4 +1,4 @@ -#include "basis_dct.h" +#include "mdict/basis_dct.h" #include diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp index 846d497d..1a89ce48 100644 --- a/src/mdict/d_mesh.cpp +++ b/src/mdict/d_mesh.cpp @@ -1,4 +1,4 @@ -#include "d_mesh.h" +#include "mdict/d_mesh.h" #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp index 5e7cf09f..47ac547f 100644 --- a/src/mdict/denoising.cpp +++ b/src/mdict/denoising.cpp @@ -1,4 +1,4 @@ -#include "denoising.h" +#include "mdict/denoising.h" // geometry processing and shape analysis framework diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index e52ecb1c..6b364788 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -1,9 +1,9 @@ -#include "dictionary.h" +#include "mdict/dictionary.h" -#include "sampling.h" -#include "mdict.h" -#include "che_poisson.h" -#include "che_fill_hole.h" +#include "geodesics/sampling.h" +#include "mdict/mdict.h" +#include "mesh/che_poisson.h" +#include "mesh/che_fill_hole.h" #include diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 1a76c4d2..10a3006a 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -1,4 +1,4 @@ -#include "image_denoising.h" +#include "mdict/image_denoising.h" #include diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 297c45a5..83979863 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,4 +1,4 @@ -#include "inpainting.h" +#include "mdict/inpainting.h" // geometry processing and shape analysis framework diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 00866e38..b5bbef51 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -1,6 +1,6 @@ -#include "mdict.h" +#include "mdict/mdict.h" -#include "sampling.h" +#include "geodesics/sampling.h" #include #include diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 76bb4294..862d3875 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -1,6 +1,6 @@ -#include "patch.h" +#include "mdict/patch.h" -#include "dictionary.h" +#include "mdict/dictionary.h" #ifndef CGAL_PATCH_DEFS #define CGAL_PATCH_DEFS diff --git a/src/mdict/super_resolution.cpp b/src/mdict/super_resolution.cpp index cb263522..2b881fa1 100644 --- a/src/mdict/super_resolution.cpp +++ b/src/mdict/super_resolution.cpp @@ -1,4 +1,4 @@ -#include "super_resolution.h" +#include "mdict/super_resolution.h" // geometry processing and shape analysis framework diff --git a/src/che.cpp b/src/mesh/che.cpp similarity index 99% rename from src/che.cpp rename to src/mesh/che.cpp index 556c0b74..3505e014 100644 --- a/src/che.cpp +++ b/src/mesh/che.cpp @@ -1,6 +1,6 @@ -#include "che.h" +#include "mesh/che.h" -#include "kdtree.h" +#include "mesh/kdtree.h" #include "include_arma.h" #include diff --git a/src/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp similarity index 99% rename from src/che_fill_hole.cpp rename to src/mesh/che_fill_hole.cpp index 423e4504..1a3516ac 100644 --- a/src/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -1,7 +1,7 @@ -#include "che_fill_hole.h" +#include "mesh/che_fill_hole.h" -#include "che_off.h" -#include "laplacian.h" +#include "mesh/che_off.h" +#include "laplacian/laplacian.h" #include diff --git a/src/che_img.cpp b/src/mesh/che_img.cpp similarity index 98% rename from src/che_img.cpp rename to src/mesh/che_img.cpp index b5978011..b5735ddd 100644 --- a/src/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -1,4 +1,4 @@ -#include "che_img.h" +#include "mesh/che_img.h" #include #include diff --git a/src/che_obj.cpp b/src/mesh/che_obj.cpp similarity index 98% rename from src/che_obj.cpp rename to src/mesh/che_obj.cpp index d1e14629..b35ca17d 100644 --- a/src/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -1,4 +1,4 @@ -#include "che_obj.h" +#include "mesh/che_obj.h" #include #include diff --git a/src/che_off.cpp b/src/mesh/che_off.cpp similarity index 98% rename from src/che_off.cpp rename to src/mesh/che_off.cpp index fb489bc6..43aada31 100644 --- a/src/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -1,4 +1,4 @@ -#include "che_off.h" +#include "mesh/che_off.h" #include #include diff --git a/src/che_ply.cpp b/src/mesh/che_ply.cpp similarity index 99% rename from src/che_ply.cpp rename to src/mesh/che_ply.cpp index 0b90efd5..ecc8e085 100644 --- a/src/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -1,4 +1,4 @@ -#include "che_ply.h" +#include "mesh/che_ply.h" #include #include diff --git a/src/che_poisson.cpp b/src/mesh/che_poisson.cpp similarity index 98% rename from src/che_poisson.cpp rename to src/mesh/che_poisson.cpp index 33d05f9b..e9b6850d 100644 --- a/src/che_poisson.cpp +++ b/src/mesh/che_poisson.cpp @@ -1,6 +1,6 @@ -#include "che_poisson.h" +#include "mesh/che_poisson.h" -#include "laplacian.h" +#include "laplacian/laplacian.h" #include "include_arma.h" using namespace std; diff --git a/src/che_ptx.cpp b/src/mesh/che_ptx.cpp similarity index 97% rename from src/che_ptx.cpp rename to src/mesh/che_ptx.cpp index 85aa4faa..a558df9f 100644 --- a/src/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -1,4 +1,4 @@ -#include "che_ptx.h" +#include "mesh/che_ptx.h" #include #include diff --git a/src/che_sphere.cpp b/src/mesh/che_sphere.cpp similarity index 98% rename from src/che_sphere.cpp rename to src/mesh/che_sphere.cpp index 4762674b..59b7e6ee 100644 --- a/src/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -1,4 +1,4 @@ -#include "che_sphere.h" +#include "mesh/che_sphere.h" #include #include diff --git a/src/decimation.cpp b/src/mesh/decimation.cpp similarity index 99% rename from src/decimation.cpp rename to src/mesh/decimation.cpp index 9c6b8ba7..501960be 100644 --- a/src/decimation.cpp +++ b/src/mesh/decimation.cpp @@ -1,4 +1,4 @@ -#include "decimation.h" +#include "mesh/decimation.h" using namespace std; diff --git a/src/kdtree.cpp b/src/mesh/kdtree.cpp similarity index 95% rename from src/kdtree.cpp rename to src/mesh/kdtree.cpp index d08e45d3..7838a8f8 100644 --- a/src/kdtree.cpp +++ b/src/mesh/kdtree.cpp @@ -1,4 +1,4 @@ -#include "kdtree.h" +#include "mesh/kdtree.h" #include diff --git a/src/quaternion.cpp b/src/mesh/quaternion.cpp similarity index 99% rename from src/quaternion.cpp rename to src/mesh/quaternion.cpp index 04b9529e..30c08594 100644 --- a/src/quaternion.cpp +++ b/src/mesh/quaternion.cpp @@ -1,4 +1,4 @@ -#include "quaternion.h" +#include "mesh/quaternion.h" #include #include diff --git a/src/vertex.cpp b/src/mesh/vertex.cpp similarity index 98% rename from src/vertex.cpp rename to src/mesh/vertex.cpp index 184e2583..4b0d5a8d 100644 --- a/src/vertex.cpp +++ b/src/mesh/vertex.cpp @@ -1,4 +1,4 @@ -#include "vertex.h" +#include "mesh/vertex.h" #include diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 4b15d9ed..fbaad7c3 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -1,4 +1,4 @@ -#include "raytracing.h" +#include "raytracing/raytracing.h" #include #include diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index c93d61fd..56e46b1e 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -1,4 +1,4 @@ -#include "rt_embree.h" +#include "raytracing/rt_embree.h" #ifdef GPROSHAN_EMBREE diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 51472ffb..f1fa09a3 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -1,4 +1,4 @@ -#include "rt_optix.h" +#include "raytracing/rt_optix.h" #ifdef GPROSHAN_OPTIX diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 1533d905..31f8ec44 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,3 +1,5 @@ +#ifdef GPROSHAN_OPTIX + #include extern "C" __constant__ void * optix_launch_params; @@ -19,3 +21,5 @@ extern "C" __global__ void __raygen__rt() { } +#endif // GPROSHAN_OPTIX + diff --git a/src/shot.cpp b/src/shot.cpp deleted file mode 100644 index 0ac979b9..00000000 --- a/src/shot.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "shot.h" - - diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index f6b398c3..d89661d8 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -1,4 +1,4 @@ -#include "camera.h" +#include "viewer/camera.h" #include #include diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 80237d1c..22555c55 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -1,4 +1,4 @@ -#include "che_viewer.h" +#include "viewer/che_viewer.h" #include #include diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 80914c3d..ed11da39 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -1,4 +1,4 @@ -#include "frame.h" +#include "viewer/frame.h" // geometry processing and shape analysis framework diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 6ace6304..57187828 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -1,4 +1,4 @@ -#include "shader.h" +#include "viewer/shader.h" #include #include diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1ae7fd35..165faaa5 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -1,4 +1,4 @@ -#include "viewer.h" +#include "viewer/viewer.h" #include #include @@ -13,10 +13,10 @@ #include #include -#include "che_off.h" -#include "che_obj.h" -#include "che_ply.h" -#include "che_sphere.h" +#include "mesh/che_off.h" +#include "mesh/che_obj.h" +#include "mesh/che_ply.h" +#include "mesh/che_sphere.h" #include diff --git a/test_geodesics.cpp b/test_geodesics.cpp index 0301b5d2..d874d4be 100644 --- a/test_geodesics.cpp +++ b/test_geodesics.cpp @@ -1,4 +1,4 @@ -#include "test_geodesics_ptp.h" +#include "geodesics/test_geodesics_ptp.h" int main(int nargs, const char ** args) { From 1d60b471740a5d1616db4ed754730c31900c785c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 26 Nov 2020 15:50:47 +0100 Subject: [PATCH 0382/1018] fix area_trig method name --- include/mesh/che.h | 2 +- src/features/key_points.cpp | 2 +- src/mesh/che.cpp | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index 11af2560..e7809710 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -68,7 +68,7 @@ class che void flip(const index_t & e); real_t pdetriq(const index_t & t) const; real_t quality(); - real_t real_trig(const index_t & t) const; + real_t area_trig(const index_t & t) const; real_t area_vertex(const index_t & v); real_t area_surface() const; void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 5a8bdfc9..bf6b2cb7 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -56,7 +56,7 @@ void key_points::compute_kps(che * mesh) #pragma omp parallel for for(index_t t = 0; t < n_faces; t++) { - face_areas[t].first = mesh->real_trig(t); + face_areas[t].first = mesh->area_trig(t); face_areas[t].second = t; } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 3505e014..d8996f74 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -196,7 +196,7 @@ real_t che::pdetriq(const index_t & t) const *(GT[VT[prev(he)]] - GT[VT[next(he)]]), *(GT[VT[he]] - GT[VT[prev(he)]]) }; - return (4 * sqrt(3) * real_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); + return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } real_t che::quality() @@ -210,7 +210,7 @@ real_t che::quality() return q * 100 / n_faces_; } -real_t che::real_trig(const index_t & t) const +real_t che::area_trig(const index_t & t) const { index_t he = t * P; vertex a = GT[VT[next(he)]] - GT[VT[he]]; @@ -223,7 +223,7 @@ real_t che::area_vertex(const index_t & v) { real_t area_star = 0; for_star(he, this, v) - area_star += real_trig(trig(he)); + area_star += area_trig(trig(he)); return area_star / 3; } @@ -234,7 +234,7 @@ real_t che::area_surface() const #pragma omp parallel for reduction(+: area) for(index_t i = 0; i < n_faces_; i++) - area += real_trig(i); + area += area_trig(i); return area; } @@ -318,7 +318,7 @@ void che::update_normals() n = 0; for_star(he, this, v) - n += real_trig(trig(he)) * normal_he(he); + n += area_trig(trig(he)) * normal_he(he); n /= *n; } @@ -366,7 +366,7 @@ vertex che::gradient_he(const index_t & he, const real_t *const & f) const vertex n = normal_he(he); - real_t A2 = real_trig(trig(he)) * 2; + real_t A2 = area_trig(trig(he)) * 2; vertex pij = n * (xj - xi); vertex pjk = n * (xk - xj); @@ -383,7 +383,7 @@ vertex che::gradient(const index_t & v, const real_t *const & f) for_star(he, this, v) { - area = real_trig(trig(he)); + area = area_trig(trig(he)); area_star += area; g += area * gradient_he(he, f); } From 07f0be2a76a0fae2a2a53fe1dbbd49529555f300 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 27 Nov 2020 22:05:59 +0100 Subject: [PATCH 0383/1018] che_ply: read binary_big_endian format --- src/mesh/che_ply.cpp | 11 +++++++++++ src/raytracing/rt_embree.cpp | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index ecc8e085..8a78e176 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -110,10 +110,18 @@ void che_ply::read_file(const string & file) } else // binary_little_endian { + bool big_endian = format == "binary_big_endian"; + auto big_to_little = [](char * buffer, const index_t & n) + { + for(index_t i = 0, j = n - 1; i < j; i++, j--) + swap(buffer[i], buffer[j]); + }; + char * vbuffer = new char[vbytes]; for(index_t v = 0; v < n_vertices_; v++) { is.read(vbuffer, vbytes); + if(big_endian) big_to_little(vbuffer, vbytes); if(xyz == sizeof(real_t)) memcpy(>[v], vbuffer, 3 * sizeof(real_t)); @@ -139,6 +147,7 @@ void che_ply::read_file(const string & file) while(n_f--) { is.read(vbuffer, fn); + if(big_endian) big_to_little(vbuffer, fn); if(fn == 1) p = *((char *) vbuffer); if(fn == 2) p = *((short *) vbuffer); @@ -147,6 +156,8 @@ void che_ply::read_file(const string & file) while(p--) { is.read(vbuffer, fbytes); + if(big_endian) big_to_little(vbuffer, fbytes); + if(fbytes == 1) VT[he++] = *((char *) vbuffer); if(fbytes == 2) VT[he++] = *((short *) vbuffer); if(fbytes == 4) VT[he++] = *((int *) vbuffer); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 56e46b1e..1c1b7453 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -164,11 +164,11 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near) { const glm::vec3 wi = normalize(light - position); - const float dist_light = glm::length(light - position); - const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec4 L = glm::vec4((dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); + + // const float dist_light = glm::length(light - position); + // const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff //const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (float(1.f/M_PI) * color) + glm::vec3(0.3), 1); ray_hit r(position, wi, near); From 57fc5f3dd94ba978e8f5ac2c25fa2cf5ea2705c2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 Nov 2020 15:02:59 +0100 Subject: [PATCH 0384/1018] che: add mesh types --- include/mesh/che.h | 6 +-- src/features/key_points.cpp | 4 +- src/geodesics/che.cu | 6 +-- src/geodesics/geodesics_ptp.cpp | 2 +- .../test_geodesics_ptp_coalescence.cu | 4 +- src/mesh/che.cpp | 42 +++++++++---------- src/mesh/che_fill_hole.cpp | 2 +- src/mesh/che_img.cpp | 4 +- src/mesh/che_obj.cpp | 6 +-- src/mesh/che_off.cpp | 10 ++--- src/mesh/che_ply.cpp | 6 +-- src/mesh/decimation.cpp | 10 ++--- 12 files changed, 51 insertions(+), 51 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index e7809710..d364e89f 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -29,7 +29,7 @@ class che vertex vcolor{ 0.75, 0.85, 1.0 }; public: - static const size_t P = 3; ///< default polygon size 3, triangular meshes + enum mesh_type { mtrig = 3, mquad = 4 }; ///< meshes_types protected: std::string filename_; @@ -174,11 +174,11 @@ struct corr_t void init(const index_t & he) { - t = trig(he) * che::P; + t = trig(he) * che::mtrig; alpha[0] = he == t; alpha[1] = he == next(t); alpha[2] = he == prev(t); - t /= che::P; + t /= che::mtrig; } }; diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index bf6b2cb7..775d7d76 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -68,8 +68,8 @@ void key_points::compute_kps(che * mesh) index_t he, k = 0; for(index_t t = 0; t < n_faces; t++) { - he = che::P * face_areas[t].second; - for(index_t i = 0; i < che::P; i++) + he = che::mtrig * face_areas[t].second; + for(index_t i = 0; i < che::mtrig; i++) { const index_t & v = mesh->vt(he); if(!is_kp[v]) diff --git a/src/geodesics/che.cu b/src/geodesics/che.cu index be520aca..ee17fd7c 100644 --- a/src/geodesics/che.cu +++ b/src/geodesics/che.cu @@ -9,21 +9,21 @@ __host__ __device__ index_t cu_trig(index_t he) { if(he == NIL) return NIL; - return he / che::P; + return he / che::mtrig; } __host__ __device__ index_t cu_next(index_t he) { if(he == NIL) return NIL; - return che::P * cu_trig(he) + (he + 1) % che::P; + return che::mtrig * cu_trig(he) + (he + 1) % che::mtrig; } __host__ __device__ index_t cu_prev(index_t he) { if(he == NIL) return NIL; - return che::P * cu_trig(he) + (he + che::P - 1) % che::P; + return che::mtrig * cu_trig(he) + (he + che::mtrig - 1) % che::mtrig; } void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che) diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index 32bbbb0c..dbf131e6 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -35,7 +35,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top if(inv[mesh->vt(he)] != NIL && inv[mesh->vt(prev(he))] != NIL && inv[mesh->vt(next(he))] != NIL) F.push_back(inv[mesh->vt(he)]); - return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::P); + return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); } void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 06cc92ba..6bd4d6e2 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -25,7 +25,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc // sort data by levels, must be improve the coalescence vertex * V = new vertex[mesh->n_vertices()]; - index_t * F = new index_t[mesh->n_faces() * che::P]; + index_t * F = new index_t[mesh->n_faces() * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices()]; real_t * exact_dist_sorted = new real_t[mesh->n_vertices()]; @@ -107,7 +107,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices()]; - index_t * F = new index_t[mesh->n_faces() * che::P]; + index_t * F = new index_t[mesh->n_faces() * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices()]; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index d8996f74..e948009b 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -18,19 +18,19 @@ namespace gproshan { index_t trig(const index_t & he) { if(he == NIL) return NIL; - return he / che::P; + return he / che::mtrig; } index_t next(const index_t & he) { if(he == NIL) return NIL; - return che::P * trig(he) + (he + 1) % che::P; + return che::mtrig * trig(he) + (he + 1) % che::mtrig; } index_t prev(const index_t & he) { if(he == NIL) return NIL; - return che::P * trig(he) + (he + che::P - 1) % che::P; + return che::mtrig * trig(he) + (he + che::mtrig - 1) % che::mtrig; } CHE::CHE(che * mesh) @@ -190,7 +190,7 @@ void che::flip(const index_t & e) // h1^2+h2^2+h3^2 real_t che::pdetriq(const index_t & t) const { - index_t he = t * P; + index_t he = t * che::mtrig; real_t h[3] = { *(GT[VT[next(he)]] - GT[VT[he]]), *(GT[VT[prev(he)]] - GT[VT[next(he)]]), @@ -212,7 +212,7 @@ real_t che::quality() real_t che::area_trig(const index_t & t) const { - index_t he = t * P; + index_t he = t * che::mtrig; vertex a = GT[VT[next(he)]] - GT[VT[he]]; vertex b = GT[VT[prev(he)]] - GT[VT[he]]; @@ -279,7 +279,7 @@ vertex & che::color(const index_t & v) vertex che::shading_color(const index_t & f, const float & u, const float & v, const float & w) const { - index_t he = f * che::P; + index_t he = f * che::mtrig; return VC ? u * VC[VT[he]] + v * VC[VT[he + 1]] + w * VC[VT[he + 2]] : vcolor; } @@ -338,14 +338,14 @@ vertex & che::normal(const index_t & v) vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const { - index_t he = f * che::P; + index_t he = f * che::mtrig; return {u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]}; } vertex che::normal_trig(const index_t & f) const { - return normal_he(f * che::P); + return normal_he(f * che::mtrig); } vertex che::normal_he(const index_t & he) const @@ -394,7 +394,7 @@ vertex che::gradient(const index_t & v, const real_t *const & f) vertex che::barycenter(const index_t & t) const { vertex bc; - index_t tmp, he = t * P; + index_t tmp, he = t * che::mtrig; tmp = he; do @@ -404,12 +404,12 @@ vertex che::barycenter(const index_t & t) const } while(he != tmp); - return bc / P; + return bc / che::mtrig; } vertex che::corr_vertex(corr_t & corr) const { - index_t he = corr.t * P; + index_t he = corr.t * che::mtrig; assert(he < n_half_edges_); return corr.alpha[0] * gt_vt(he) + corr.alpha[1] * gt_vt(next(he)) + corr.alpha[2] * gt_vt(prev(he)); } @@ -730,8 +730,8 @@ void che::multiplicate_vertices() for(index_t f = 0; f < n_faces_; f++) { index_t v = n_vertices_ + f; - index_t he = f * P; - index_t ahe = f * P * 3; + index_t he = f * che::mtrig; + index_t ahe = f * che::mtrig * 3; aGT[v] = (GT[VT[prev(he)]] + GT[VT[he]] + GT[VT[next(he)]]) / 3; @@ -826,7 +826,7 @@ void che::multiplicate_vertices() } else flip(e); - n_faces_ = n_half_edges_ / che::P; + n_faces_ = n_half_edges_ / che::mtrig; update_bt(); } @@ -886,7 +886,7 @@ void che::remove_non_manifold_vertices() gproshan_debug(removing vertex); delete_me(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / P); + init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); gproshan_debug(removing vertex); } @@ -954,7 +954,7 @@ void che::remove_vertices(const vector & vertices) gproshan_debug(removing vertex); delete_me(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / P); + init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); gproshan_debug(removing vertex); } @@ -1199,10 +1199,10 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con vector he_trigs; for_star(he, this, va) if(faces_fixed[trig(he)] > -1) - he_trigs.push_back(trig(he) * P); + he_trigs.push_back(trig(he) * che::mtrig); for_star(he, this, vb) if(faces_fixed[trig(he)] > -1) - he_trigs.push_back(trig(he) * P); + he_trigs.push_back(trig(he) * che::mtrig); gproshan_debug_var(va); corr[va] = find_corr(aux_va, normals[va], he_trigs); @@ -1241,10 +1241,10 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con } for(index_t v = 0; v < n_vertices_; v++) - corr[v].t = trig(map_he[corr[v].t * P]); + corr[v].t = trig(map_he[corr[v].t * che::mtrig]); delete_me(); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / P); + init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); delete [] faces_fixed; delete [] deleted_vertices; @@ -1374,7 +1374,7 @@ void che::init(const size_t & n_v, const size_t & n_f) { n_vertices_ = n_v; n_faces_ = n_f; - n_half_edges_ = che::P * n_faces_; + n_half_edges_ = che::mtrig * n_faces_; n_edges_ = n_borders_ = 0; diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 1a3516ac..ca091e7a 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -876,7 +876,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i size_t n_faces = select_vertices.size() + 4; vertex * vertices = new vertex[n_vertices]; - index_t * faces = new index_t[n_faces * che::P]; + index_t * faces = new index_t[n_faces * che::mtrig]; vector triangle; vector tri_sizes(3,0); diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index b5735ddd..afd5a37b 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -68,9 +68,9 @@ void che_img::write_file(const string & file) const for(index_t he = 0; he < n_half_edges_; he++) { - if(!(he % che::P)) os << che::P; + if(!(he % che::mtrig)) os << che::mtrig; os << " " << VT[he]; - if(he % che::P == che::P - 1) os << endl; + if(he % che::mtrig == che::mtrig - 1) os << endl; } os.close(); diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index b35ca17d..1c458865 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -55,7 +55,7 @@ void che_obj::read_file(const string & file) for(i = 0; ss >> face[i]; i++) ss.ignore(256, ' '); - if(i == che::P) // che::P = 3, triangular mesh + if(i == che::mtrig) // che::mtrig = 3, triangular mesh { if(face[0] < 0) { @@ -98,7 +98,7 @@ void che_obj::read_file(const string & file) is.close(); - init(vertices.data(), vertices.size(), faces.data(), faces.size() / che::P); + init(vertices.data(), vertices.size(), faces.data(), faces.size() / che::mtrig); } void che_obj::write_file(const che * mesh, const string & file) @@ -117,7 +117,7 @@ void che_obj::write_file(const che * mesh, const string & file) for(index_t he = 0; he < mesh->n_half_edges(); ) { os << "f"; - for(index_t i = 0; i < che::P; i++) + for(index_t i = 0; i < che::mtrig; i++) os << " " << mesh->vt(he++) + 1; os << endl; } diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 43aada31..4ccedded 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -62,12 +62,12 @@ void che_off::read_file(const string & file) for(index_t i = 0; i < n_faces_; i++) { is >> v; - if(!i && v > che::P) + if(!i && v > che::mtrig) { vertex * tGT = GT; GT = nullptr; delete_me(); - init(n_v, n_f * (v - che::P + 1)); + init(n_v, n_f * (v - che::mtrig + 1)); GT = tGT; } @@ -79,7 +79,7 @@ void che_off::read_file(const string & file) if(v == 4) { VT[he] = VT[he - v]; he++; - VT[he] = VT[he - che::P]; he++; + VT[he] = VT[he - che::mtrig]; he++; i++; } @@ -107,8 +107,8 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t if(!pointcloud) for(index_t he = 0; he < mesh->n_half_edges(); ) { - os << che::P; - for(index_t i = 0; i < che::P; i++) + os << che::mtrig; + for(index_t i = 0; i < che::mtrig; i++) os << " " << mesh->vt(he++); os << endl; } diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 8a78e176..11607db4 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -176,7 +176,7 @@ void che_ply::write_file(const che * mesh, const string & file) os << "ply" << endl; os << "format ascii 1.0" << endl; - os << "comment generated by gproshan 2019" << endl; + os << "comment generated by gproshan 2020" << endl; os << "element vertex " << mesh->n_vertices() << endl; os << "property float x" << endl; os << "property float y" << endl; @@ -190,8 +190,8 @@ void che_ply::write_file(const che * mesh, const string & file) for(index_t he = 0; he < mesh->n_half_edges(); ) { - os << che::P; - for(index_t i = 0; i < che::P; i++) + os << che::mtrig; + for(index_t i = 0; i < che::mtrig; i++) os << " " << mesh->vt(he++); os << endl; } diff --git a/src/mesh/decimation.cpp b/src/mesh/decimation.cpp index 501960be..44f6dd41 100644 --- a/src/mesh/decimation.cpp +++ b/src/mesh/decimation.cpp @@ -35,7 +35,7 @@ void decimation::execute(const vertex *const & normals) index_t * sort_edges = new index_t[mesh->n_edges()]; real_t * error_edges = new real_t[mesh->n_edges()]; vertex * corr_v = new vertex[n_vertices]; - index_t * corr_i = new index_t[n_vertices * che::P]; + index_t * corr_i = new index_t[n_vertices * che::mtrig]; corr_t * corr_aux; index_t he, vi; @@ -43,7 +43,7 @@ void decimation::execute(const vertex *const & normals) auto add_he_trigs = [this](vector & he_trigs, const corr_t & c) { - const index_t he = c.t * che::P; + const index_t he = c.t * che::mtrig; for_star(t_he, mesh, mesh->vt(he)) he_trigs.push_back(t_he); for_star(t_he, mesh, mesh->vt(next(he))) @@ -61,8 +61,8 @@ void decimation::execute(const vertex *const & normals) for(index_t v = 0; v < n_vertices; v++) { corr_v[v] = mesh->corr_vertex(corr[v]); - he = corr[v].t * che::P; - vi = v * che::P; + he = corr[v].t * che::mtrig; + vi = v * che::mtrig; corr_i[vi] = mesh->vt(he); corr_i[vi + 1] = mesh->vt(next(he)); corr_i[vi + 2] = mesh->vt(prev(he)); @@ -74,7 +74,7 @@ void decimation::execute(const vertex *const & normals) #pragma omp parallel for private(vi, a, b, c) for(index_t v = 0; v < n_vertices; v++) { - vi = v * che::P; + vi = v * che::mtrig; a = mesh->corr_vertex(corr_aux[corr_i[vi]]); b = mesh->corr_vertex(corr_aux[corr_i[vi + 1]]); c = mesh->corr_vertex(corr_aux[corr_i[vi + 2]]); From ced39e2b0512b1bbd810c71c54dca6b99c7540eb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 Nov 2020 15:23:22 +0100 Subject: [PATCH 0385/1018] che: read and load quad meshes as trig meshes --- src/mesh/che_obj.cpp | 2 +- src/mesh/che_off.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 1c458865..f72cfa1d 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -70,7 +70,7 @@ void che_obj::read_file(const string & file) faces.push_back(face[2] - 1); } } - else if(i == 4) // quadrangular mesh, split two triangles + else if(i == che::mquad) // quadrangular mesh, split two triangles { if(face[0] < 0) { diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 4ccedded..8bafeaa5 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -76,7 +76,7 @@ void che_off::read_file(const string & file) is >> VT[he++]; // divide face - if(v == 4) + if(v == che::mquad) { VT[he] = VT[he - v]; he++; VT[he] = VT[he - che::mtrig]; he++; From 9fc70ce9514b0f5fb147b0842f31d6800f32a3d3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 Nov 2020 18:36:03 +0100 Subject: [PATCH 0386/1018] cleaning comments --- src/mesh/che_obj.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index f72cfa1d..27578fbc 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -55,7 +55,7 @@ void che_obj::read_file(const string & file) for(i = 0; ss >> face[i]; i++) ss.ignore(256, ' '); - if(i == che::mtrig) // che::mtrig = 3, triangular mesh + if(i == che::mtrig) { if(face[0] < 0) { @@ -70,7 +70,7 @@ void che_obj::read_file(const string & file) faces.push_back(face[2] - 1); } } - else if(i == che::mquad) // quadrangular mesh, split two triangles + else if(i == che::mquad) { if(face[0] < 0) { From a08d311c51e9c09ff3352df014316c1c648e3ff1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 30 Nov 2020 23:41:18 +0100 Subject: [PATCH 0387/1018] che: read ptx file, load and cull faces. --- include/mesh/vertex.h | 2 ++ src/mesh/che_ptx.cpp | 38 +++++++++++++++++++++++++++++++------- src/mesh/vertex.cpp | 7 +++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/include/mesh/vertex.h b/include/mesh/vertex.h index 080f873a..fcb97a30 100644 --- a/include/mesh/vertex.h +++ b/include/mesh/vertex.h @@ -44,6 +44,8 @@ class vertex bool operator < (const vertex & v); bool operator == (const vertex & v); + + bool is_zero(); }; vertex operator * (const real_t & a, const vertex & v); diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index a558df9f..41eb11c6 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -1,6 +1,7 @@ #include "mesh/che_ptx.h" #include +#include #include #include #include @@ -36,7 +37,7 @@ void che_ptx::read_file(const string & file) assert(is.good()); is >> n_rows >> n_cols; - init(n_rows * n_cols, 0); + init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); is >> T[0] >> T[1] >> T[2] >> T[3]; is >> R[0] >> s; @@ -44,9 +45,6 @@ void che_ptx::read_file(const string & file) is >> R[2] >> s; is >> tr >> s; - VN = new vertex[n_vertices_]; - VC = new vertex[n_vertices_]; - char line[256]; is.getline(line, sizeof(line)); @@ -57,11 +55,37 @@ void che_ptx::read_file(const string & file) stringstream ss(line); ss >> GT[i] >> alpha >> VC[i]; + } + + index_t he = 0; + auto add_trig = [&](const index_t & i, const index_t & j, const index_t & k) + { + if(GT[i].is_zero() || GT[j].is_zero() || GT[k].is_zero()) + return; + + VT[he++] = i; + VT[he++] = j; + VT[he++] = k; + + if(pdetriq(trig(he - 1)) < 0.08) + he -= 3; + }; - VN[i] = tr - GT[i]; - VN[i].unit(); + for(index_t r = 0; r < n_rows - 1; r++) + for(index_t c = 0; c < n_cols - 1; c++) + { + add_trig( c + r * n_cols, + c + (r + 1) * n_cols, + (c + 1) + r * n_cols ); + + add_trig( (c + 1) + (r + 1) * n_cols, + (c + 1) + r * n_cols, + c + (r + 1) * n_cols ); } - + + n_half_edges_ = he; + n_faces_ = he / che::mtrig; + #pragma omp parallel for for(index_t i = 0; i < n_vertices_; i++) VC[i] /= 255; diff --git a/src/mesh/vertex.cpp b/src/mesh/vertex.cpp index 4b0d5a8d..0ca0a3dc 100644 --- a/src/mesh/vertex.cpp +++ b/src/mesh/vertex.cpp @@ -104,6 +104,13 @@ bool vertex::operator == (const vertex & v) return x == v.x && y == v.y && z == v.z; } +bool vertex::is_zero() +{ + real_t eps = std::numeric_limits::epsilon(); + + return abs(x) < eps && abs(y) < eps && abs(z) < eps; +} + vertex operator * (const real_t & a, const vertex & v) { return vertex(a * v.x, a * v.y, a * v.z); From cc74fe6dd6c4a92c09e88a69c1367b0fddc0a7a0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 30 Nov 2020 23:55:33 +0100 Subject: [PATCH 0388/1018] che_ptx: face orientation scanner --- src/mesh/che_ptx.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 41eb11c6..7cdec5d6 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -67,20 +67,20 @@ void che_ptx::read_file(const string & file) VT[he++] = j; VT[he++] = k; - if(pdetriq(trig(he - 1)) < 0.08) + if(pdetriq(trig(he - 1)) < 0.1) he -= 3; }; for(index_t r = 0; r < n_rows - 1; r++) for(index_t c = 0; c < n_cols - 1; c++) { - add_trig( c + r * n_cols, - c + (r + 1) * n_cols, - (c + 1) + r * n_cols ); - - add_trig( (c + 1) + (r + 1) * n_cols, - (c + 1) + r * n_cols, - c + (r + 1) * n_cols ); + add_trig((c ) + (r ) * n_cols, + (c ) + (r + 1) * n_cols, + (c + 1) + (r ) * n_cols); + + add_trig((c + 1) + (r + 1) * n_cols, + (c + 1) + (r ) * n_cols, + (c ) + (r + 1) * n_cols); } n_half_edges_ = he; From b935ffaee7ee4476cb21bd4c09b285a8ef31bdba Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 1 Dec 2020 00:54:04 +0100 Subject: [PATCH 0389/1018] che_ptx: optimized loading --- src/mesh/che_ptx.cpp | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 7cdec5d6..4b52cc70 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -28,16 +28,15 @@ che_ptx::~che_ptx() void che_ptx::read_file(const string & file) { - size_t n_rows, n_cols; - vertex T[4], R[3], tr; - real_t s; - ifstream is(file); assert(is.good()); + size_t n_rows, n_cols; + vertex T[4], R[3], tr; + real_t s; + is >> n_rows >> n_cols; - init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); is >> T[0] >> T[1] >> T[2] >> T[3]; is >> R[0] >> s; @@ -45,18 +44,31 @@ void che_ptx::read_file(const string & file) is >> R[2] >> s; is >> tr >> s; - char line[256]; - is.getline(line, sizeof(line)); + real_t * points = new real_t[7 * n_rows * n_cols]; - real_t alpha; // color - for(index_t i = 0; i < n_vertices_; i++) + index_t p = 0; + while(is >> points[p++]); + + // p is offset for [x y z a] or [x y z a r g b] point data + p = n_rows * n_cols * 4 == p ? 4 : 7; + + init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices_; v++) { - is.getline(line, sizeof(line)); - stringstream ss(line); - - ss >> GT[i] >> alpha >> VC[i]; + const index_t & i = v * p; + + GT[v] = { points[i], points[i + 1], points[i + 2] }; + + if(p == 4) + VC[v] = { points[i + 3], points[i + 3], points[i + 3] }; + else + VC[v] = { points[i + 4], points[i + 5], points[i + 6] }; } + delete [] points; + index_t he = 0; auto add_trig = [&](const index_t & i, const index_t & j, const index_t & k) { From e470b3d4068244fb36bb827fb66f3cb3eecc0d5f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Dec 2020 16:36:22 +0100 Subject: [PATCH 0390/1018] add cuda atchitectures max compatibility and update simplification --- CMakeLists.txt | 7 +-- include/app_viewer.h | 2 +- .../mesh/{decimation.h => simplification.h} | 12 ++--- include/viewer/viewer.h | 3 +- src/app_viewer.cpp | 2 +- .../{decimation.cpp => simplification.cpp} | 18 +++---- src/viewer/viewer.cpp | 47 ++++++++++++------- 7 files changed, 51 insertions(+), 40 deletions(-) rename include/mesh/{decimation.h => simplification.h} (72%) rename src/mesh/{decimation.cpp => simplification.cpp} (85%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5445cb2..34277e9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,8 @@ if(CUDAToolkit_FOUND) add_library(gproshan_cu STATIC ${cu_sources}) target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) + set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80 86) + target_link_libraries(gproshan_cu gproshan_cpp) target_link_libraries(gproshan_cu CUDA::cudart) target_link_libraries(gproshan_cu CUDA::cublas) @@ -119,11 +121,6 @@ target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) if(CUDAToolkit_FOUND) - set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 60 61 62) - set_property(TARGET gproshan PROPERTY CUDA_ARCHITECTURES 60 61 62) - set_property(TARGET test_geodesics PROPERTY CUDA_ARCHITECTURES 60 61 62) - set_property(TARGET test_image_denoising PROPERTY CUDA_ARCHITECTURES 60 61 62) - target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) diff --git a/include/app_viewer.h b/include/app_viewer.h index 64b5cce8..8d87ff6f 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -12,7 +12,7 @@ #include "mesh/che_sphere.h" #include "mesh/che_fill_hole.h" #include "mesh/che_poisson.h" -#include "mesh/decimation.h" +#include "mesh/simplification.h" #include "laplacian/laplacian.h" #include "laplacian/fairing_taubin.h" diff --git a/include/mesh/decimation.h b/include/mesh/simplification.h similarity index 72% rename from include/mesh/decimation.h rename to include/mesh/simplification.h index 6c9a2b2d..4c1fb8e0 100644 --- a/include/mesh/decimation.h +++ b/include/mesh/simplification.h @@ -1,5 +1,5 @@ -#ifndef DECIMATION_H -#define DECIMATION_H +#ifndef SIMPLIFICATION_H +#define SIMPLIFICATION_H #include "mesh/che.h" #include "include_arma.h" @@ -11,7 +11,7 @@ namespace gproshan { -class decimation +class simplification { private: a_mat * Q; @@ -20,8 +20,8 @@ class decimation index_t levels; public: - decimation(che * mesh, const vertex *const & normals, const index_t & levels_ = 1); - ~decimation(); + simplification(che * mesh, const vertex *const & normals, const index_t & levels_ = 1); + ~simplification(); operator const corr_t * (); private: @@ -35,5 +35,5 @@ class decimation } // namespace gproshan -#endif // DECIMATION_H +#endif // SIMPLIFICATION_H diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 4af76da9..30f9bb9e 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -81,7 +81,8 @@ class viewer size_t n_meshes = 0; index_t idx_active_mesh = 0; // idx_active_mesh mesh - index_t render_opt = 0; + enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; + index_t render_opt = R_GL; frame * render_frame = nullptr; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7e26e1a9..3e706f16 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -987,7 +987,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) index_t levels; cin >> levels; - TIC(view->time) decimation sampling(mesh, &mesh->normal(0), levels); TOC(view->time) + TIC(view->time) simplification sampling(mesh, &mesh->normal(0), levels); TOC(view->time) gproshan_debug_var(view->time); //if(view->n_meshes < 2) diff --git a/src/mesh/decimation.cpp b/src/mesh/simplification.cpp similarity index 85% rename from src/mesh/decimation.cpp rename to src/mesh/simplification.cpp index 44f6dd41..a6f68ccc 100644 --- a/src/mesh/decimation.cpp +++ b/src/mesh/simplification.cpp @@ -1,4 +1,4 @@ -#include "mesh/decimation.h" +#include "mesh/simplification.h" using namespace std; @@ -7,7 +7,7 @@ using namespace std; namespace gproshan { -decimation::decimation(che * mesh_, const vertex *const & normals, const index_t & levels_) +simplification::simplification(che * mesh_, const vertex *const & normals, const index_t & levels_) { mesh = mesh_; levels = levels_; @@ -16,18 +16,18 @@ decimation::decimation(che * mesh_, const vertex *const & normals, const index_t execute(normals); } -decimation::~decimation() +simplification::~simplification() { if(Q) delete [] Q; if(corr) delete [] corr; } -decimation::operator const corr_t * () +simplification::operator const corr_t * () { return corr; } -void decimation::execute(const vertex *const & normals) +void simplification::execute(const vertex *const & normals) { compute_quadrics(); @@ -97,7 +97,7 @@ void decimation::execute(const vertex *const & normals) delete [] corr_i; } -void decimation::compute_quadrics() +void simplification::compute_quadrics() { vertex n; @@ -121,7 +121,7 @@ void decimation::compute_quadrics() } } -void decimation::order_edges(index_t * const & sort_edges, real_t * const & error_edges) +void simplification::order_edges(index_t * const & sort_edges, real_t * const & error_edges) { #pragma omp parallel for for(index_t e = 0; e < mesh->n_edges(); e++) @@ -138,7 +138,7 @@ void decimation::order_edges(index_t * const & sort_edges, real_t * const & erro ); } -real_t decimation::compute_error(const index_t & e) +real_t simplification::compute_error(const index_t & e) { vertex ve = create_vertex(e); a_vec v(4); @@ -151,7 +151,7 @@ real_t decimation::compute_error(const index_t & e) return as_scalar(v.t() * (Q[mesh->vt_e(e)] + Q[mesh->vt_e(e, true)]) * v); } -vertex decimation::create_vertex(const index_t & e) +vertex simplification::create_vertex(const index_t & e) { vertex va, vb; va = mesh->gt_e(e); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 165faaa5..e96ac10a 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -100,26 +100,15 @@ bool viewer::run() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { - case 0: render_gl(); break; - case 1: render_embree(); break; - case 2: render_optix(); break; + case R_GL: render_gl(); break; + case R_EMBREE: render_embree(); break; + case R_OPTIX: render_optix(); break; } ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - #ifdef GPROSHAN_EMBREE - if(!rt_embree) - { - ImGui::SetNextWindowSize(ImVec2(300, -1)); - ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); - ImGui::Begin("rt_embree_pointcloud"); - ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.4f"); - ImGui::End(); - } - #endif // GPROSHAN_EMBREE - if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) @@ -562,15 +551,39 @@ bool viewer::invert_orientation(viewer * view) bool viewer::set_render_gl(viewer * view) { - view->render_opt = 0; + view->render_opt = R_GL; return false; } bool viewer::set_render_embree(viewer * view) { - view->render_opt = 1; - +#ifdef GPROSHAN_EMBREE + + if(!view->rt_embree) + { + ImGui::InputFloat("disk radius", &rt::embree::pc_radius, 0, 0, "%.4f"); + + if(ImGui::Button("Start")) + view->render_opt = R_EMBREE; + } + else + { + ImGui::LabelText("disk radius", "%.4f", rt::embree::pc_radius); + + if(ImGui::Button("Reset")) + { + delete view->rt_embree; + view->rt_embree = nullptr; + + view->render_opt = R_GL; + } + } + + return true; + +#endif // GPROSHAN_EMBREE + return false; } From cff36ff441831696d235bcf4b96e59ad5e651b80 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Dec 2020 17:56:21 +0100 Subject: [PATCH 0391/1018] factoring app_viewer geodesics code --- include/app_viewer.h | 4 +- src/app_viewer.cpp | 103 +++++++++++------------------------- src/geodesics/geodesics.cpp | 4 +- 3 files changed, 33 insertions(+), 78 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 8d87ff6f..a2d86370 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -40,8 +40,6 @@ class app_viewer : public viewer { private: double time; - real_t * dist; - size_t n_dist; public: app_viewer(); @@ -53,7 +51,7 @@ class app_viewer : public viewer static bool process_fairing_taubin(viewer * p_view); static bool process_fairing_spectral(viewer * p_view); - static bool process_fastmarching(viewer * p_view); + static bool process_geodesics(viewer * p_view, const geodesics::algorithm & alg); static bool process_geodesics_fm(viewer * p_view); static bool process_geodesics_ptp_cpu(viewer * p_view); static bool process_geodesics_heat_flow(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 3e706f16..7f52161b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -27,14 +27,10 @@ che * load_mesh(const string & file_path) app_viewer::app_viewer() { - dist = nullptr; - n_dist = 0; } app_viewer::~app_viewer() { - delete [] dist; - for(che * mesh: meshes) delete mesh; } @@ -782,61 +778,58 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) return true; } -bool app_viewer::process_geodesics_fm(viewer * p_view) +bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & alg) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); if(!mesh.selected.size()) mesh.selected.push_back(0); + static size_t n_dist = 0; + static real_t * dist = nullptr; + + if(n_dist != mesh->n_vertices()) + { + delete [] dist; + + n_dist = mesh->n_vertices(); + dist = new real_t[n_dist]; + } + + geodesics::params params; + params.alg = alg; + params.dist_alloc = dist; + TIC(view->time) - geodesics fm(mesh, mesh.selected); + geodesics G(mesh, mesh.selected, params); TOC(view->time) gproshan_log_var(view->time); - - mesh->update_heatmap(&fm[0]); + + mesh->update_heatmap(&G[0]); return false; } -bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) +bool app_viewer::process_geodesics_fm(viewer * p_view) { gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - TIC(view->time) - geodesics ptp(mesh, mesh.selected, { geodesics::PTP_CPU }); - TOC(view->time) - gproshan_log_var(view->time); + return process_geodesics(p_view, geodesics::FM); +} - mesh->update_heatmap(&ptp[0]); - - return false; +bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + + return process_geodesics(p_view, geodesics::PTP_CPU); } bool app_viewer::process_geodesics_heat_flow(viewer * p_view) { gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - TIC(view->time) - geodesics heat_flow(mesh, mesh.selected, { geodesics::HEAT_FLOW }); - TOC(view->time) - gproshan_log_var(view->time); - mesh->update_heatmap(&heat_flow[0]); - - return false; + return process_geodesics(p_view, geodesics::HEAT_FLOW); } @@ -845,51 +838,15 @@ bool app_viewer::process_geodesics_heat_flow(viewer * p_view) bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - if(view->n_dist != mesh->n_vertices()) - { - delete [] view->dist; - - view->n_dist = mesh->n_vertices(); - view->dist = new real_t[view->n_dist]; - } - geodesics::params params; - params.alg = geodesics::PTP_GPU; - params.dist_alloc = view->dist; - - TIC(view->time) - geodesics ptp(mesh, mesh.selected, params); - TOC(view->time) - gproshan_log_var(view->time); - - mesh->update_heatmap(&ptp[0]); - - return false; + return process_geodesics(p_view, geodesics::PTP_GPU); } bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - TIC(view->time) - geodesics heat_flow(mesh, mesh.selected, { geodesics::HEAT_FLOW_GPU }); - TOC(view->time) - gproshan_log_var(view->time); - - mesh->update_heatmap(&heat_flow[0]); - - return false; + return process_geodesics(p_view, geodesics::HEAT_FLOW_GPU); } #endif // GPROSHAN_CUDA diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 77945070..02f8d9fd 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -213,7 +213,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< void geodesics::run_heat_flow(che * mesh, const vector & sources) { - if(dist) delete [] dist; + delete [] dist; double time_total, solve_time; TIC(time_total) @@ -246,7 +246,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector< void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) { - if(dist) delete [] dist; + delete [] dist; double time_total, solve_time; TIC(time_total) From c07968762b73e166521e937a6adc8ba861787e36 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Dec 2020 17:59:05 +0100 Subject: [PATCH 0392/1018] update cuda architectures for cuda 11.0 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34277e9a..e41c8b6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,7 +103,7 @@ if(CUDAToolkit_FOUND) add_library(gproshan_cu STATIC ${cu_sources}) target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) - set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80 86) + set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80) target_link_libraries(gproshan_cu gproshan_cpp) target_link_libraries(gproshan_cu CUDA::cudart) From a8f30021356ef39683021c059389f5a4989dbe06 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Dec 2020 19:15:00 +0100 Subject: [PATCH 0393/1018] geodesics: external memory allocation for heat method --- include/geodesics/heat_flow.h | 2 +- src/app_viewer.cpp | 15 +++++---------- src/geodesics/geodesics.cpp | 6 +----- src/geodesics/heat_flow.cpp | 7 +++---- src/geodesics/test_geodesics_ptp.cpp | 6 ++---- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/include/geodesics/heat_flow.h b/include/geodesics/heat_flow.h index 06f3af66..dbe9d503 100644 --- a/include/geodesics/heat_flow.h +++ b/include/geodesics/heat_flow.h @@ -20,7 +20,7 @@ namespace gproshan { -real_t * heat_flow(che * mesh, const std::vector & sources, double & solve_time); +double heat_flow(real_t * dist, che * mesh, const std::vector & sources); #ifdef GPROSHAN_CUDA real_t * heat_flow_gpu(che * mesh, const std::vector & sources, double & solve_time); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 7f52161b..6be43d42 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -785,21 +785,16 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & if(!mesh.selected.size()) mesh.selected.push_back(0); + - static size_t n_dist = 0; - static real_t * dist = nullptr; + static vector dist; - if(n_dist != mesh->n_vertices()) - { - delete [] dist; - - n_dist = mesh->n_vertices(); - dist = new real_t[n_dist]; - } + if(dist.size() != mesh->n_vertices()) + dist.resize(mesh->n_vertices()); geodesics::params params; params.alg = alg; - params.dist_alloc = dist; + params.dist_alloc = dist.data(); TIC(view->time) geodesics G(mesh, mesh.selected, params); diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 02f8d9fd..f014241f 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -213,11 +213,9 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< void geodesics::run_heat_flow(che * mesh, const vector & sources) { - delete [] dist; - double time_total, solve_time; TIC(time_total) - dist = heat_flow(mesh, sources, solve_time); + solve_time = heat_flow(dist, mesh, sources); TOC(time_total) gproshan_log_var(time_total - solve_time); @@ -246,8 +244,6 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector< void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) { - delete [] dist; - double time_total, solve_time; TIC(time_total) dist = heat_flow_gpu(mesh, sources, solve_time); diff --git a/src/geodesics/heat_flow.cpp b/src/geodesics/heat_flow.cpp index 7ddbca8e..3a3a3766 100644 --- a/src/geodesics/heat_flow.cpp +++ b/src/geodesics/heat_flow.cpp @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -real_t * heat_flow(che * mesh, const vector & sources, double & solve_time) +double heat_flow(real_t * dist, che * mesh, const vector & sources) { if(!sources.size()) return 0; @@ -36,13 +36,12 @@ real_t * heat_flow(che * mesh, const vector & sources, double & solve_t cholmod_common context; cholmod_l_start(&context); - solve_time = 0; + double solve_time = 0; solve_time += solve_positive_definite(u, A, u0, &context); // cholmod (suitesparse) //assert(spsolve(u, A, u0)); // arma // extract geodesics - real_t * dist = new real_t[mesh->n_vertices()]; a_mat div(mesh->n_vertices(), 1); compute_divergence(mesh, u, div); @@ -58,7 +57,7 @@ real_t * heat_flow(che * mesh, const vector & sources, double & solve_t //cholmod_l_gpu_stats(&context); cholmod_l_finish(&context); - return dist; + return solve_time; } #ifdef GPROSHAN_CUDA diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index c662f052..9e34e6b4 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -279,12 +279,10 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e double t, st, ptime; ptime = stime = INFINITY; - real_t * dist = nullptr; + real_t * dist = new real_t[mesh->n_vertices()]; for(int i = 0; i < n_test; i++) { - if(dist) delete [] dist; - - TIC(t) dist = heat_flow(mesh, source, st); TOC(t) + TIC(t) st = heat_flow(dist, mesh, source); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); } From fd88af7950beb6028144b281ff89d54b48dcff54 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Dec 2020 20:27:50 +0100 Subject: [PATCH 0394/1018] geodesics: rename heat method --- include/app_viewer.h | 4 ++-- include/geodesics/geodesics.h | 8 ++++---- include/geodesics/{heat_flow.h => heat_method.h} | 12 ++++++------ src/app_viewer.cpp | 12 ++++++------ src/geodesics/geodesics.cpp | 14 +++++++------- src/geodesics/{heat_flow.cpp => heat_method.cpp} | 6 +++--- src/geodesics/{heat_flow.cu => heat_method.cu} | 0 src/geodesics/test_geodesics_ptp.cpp | 6 +++--- 8 files changed, 31 insertions(+), 31 deletions(-) rename include/geodesics/{heat_flow.h => heat_method.h} (88%) rename src/geodesics/{heat_flow.cpp => heat_method.cpp} (95%) rename src/geodesics/{heat_flow.cu => heat_method.cu} (100%) diff --git a/include/app_viewer.h b/include/app_viewer.h index a2d86370..95bdb772 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -54,11 +54,11 @@ class app_viewer : public viewer static bool process_geodesics(viewer * p_view, const geodesics::algorithm & alg); static bool process_geodesics_fm(viewer * p_view); static bool process_geodesics_ptp_cpu(viewer * p_view); - static bool process_geodesics_heat_flow(viewer * p_view); + static bool process_geodesics_heat_method(viewer * p_view); #ifdef GPROSHAN_CUDA static bool process_geodesics_ptp_gpu(viewer * p_view); - static bool process_geodesics_heat_flow_gpu(viewer * p_view); + static bool process_geodesics_heat_method_gpu(viewer * p_view); #endif // GPROSHAN_CUDA static bool process_farthest_point_sampling(viewer * p_view); diff --git a/include/geodesics/geodesics.h b/include/geodesics/geodesics.h index 773b7979..2c63ad47 100644 --- a/include/geodesics/geodesics.h +++ b/include/geodesics/geodesics.h @@ -26,10 +26,10 @@ class geodesics enum algorithm { FM, ///< Execute Fast Marching algorithm #ifdef GPROSHAN_CUDA PTP_GPU, ///< Execute Parallel Toplesets Propagation GPU algorithm - HEAT_FLOW_GPU, ///< Execute Heat Flow - cusparse (GPU) + HEAT_METHOD_GPU, ///< Execute Heat Method - cusparse (GPU) #endif // GPROSHAN_CUDA PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm - HEAT_FLOW ///< Execute Heat Flow - cholmod (CPU) + HEAT_METHOD ///< Execute Heat Method - cholmod (CPU) }; struct params @@ -72,11 +72,11 @@ class geodesics void execute(che * mesh, const std::vector & sources, const params & p); void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); - void run_heat_flow(che * mesh, const std::vector & sources); + void run_heat_method(che * mesh, const std::vector & sources); #ifdef GPROSHAN_CUDA void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); - void run_heat_flow_gpu(che * mesh, const std::vector & sources); + void run_heat_method_gpu(che * mesh, const std::vector & sources); #endif // GPROSHAN_CUDA real_t update(index_t & d, che * mesh, const index_t & he, vertex & vx); diff --git a/include/geodesics/heat_flow.h b/include/geodesics/heat_method.h similarity index 88% rename from include/geodesics/heat_flow.h rename to include/geodesics/heat_method.h index dbe9d503..97ddbb72 100644 --- a/include/geodesics/heat_flow.h +++ b/include/geodesics/heat_method.h @@ -2,13 +2,13 @@ * Base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics * forked from https://github.com/dgpdec/course * - * Geodesics in Heat: A New Approach to Computing Distance Based on Heat Flow + * Geodesics in Heat: A New Approach to Computing Distance Based on Heat Method * Keenan Crane, Clarisse Weischedel, Max Wardetzky * To appear at ACM Transactions on Graphics */ -#ifndef HEAT_FLOW_H -#define HEAT_FLOW_H +#ifndef HEAT_METHOD_H +#define HEAT_METHOD_H #include "mesh/che.h" #include "include_arma.h" @@ -20,10 +20,10 @@ namespace gproshan { -double heat_flow(real_t * dist, che * mesh, const std::vector & sources); +double heat_method(real_t * dist, che * mesh, const std::vector & sources); #ifdef GPROSHAN_CUDA -real_t * heat_flow_gpu(che * mesh, const std::vector & sources, double & solve_time); +real_t * heat_method_gpu(che * mesh, const std::vector & sources, double & solve_time); #endif // GPROSHAN_CUDA void compute_divergence(che * mesh, const a_mat & u, a_mat & div); @@ -57,5 +57,5 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons } // namespace gproshan -#endif // HEAT_FLOW_H +#endif // HEAT_METHOD_H diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6be43d42..86832a45 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -61,12 +61,12 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); #ifndef SINGLE_P - add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_flow}); + add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_method}); #endif #ifdef GPROSHAN_CUDA add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); - add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_flow_gpu}); + add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_method_gpu}); #endif // GPROSHAN_CUDA add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); @@ -820,11 +820,11 @@ bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) return process_geodesics(p_view, geodesics::PTP_CPU); } -bool app_viewer::process_geodesics_heat_flow(viewer * p_view) +bool app_viewer::process_geodesics_heat_method(viewer * p_view) { gproshan_log(APP_VIEWER); - return process_geodesics(p_view, geodesics::HEAT_FLOW); + return process_geodesics(p_view, geodesics::HEAT_METHOD); } @@ -837,11 +837,11 @@ bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) return process_geodesics(p_view, geodesics::PTP_GPU); } -bool app_viewer::process_geodesics_heat_flow_gpu(viewer * p_view) +bool app_viewer::process_geodesics_heat_method_gpu(viewer * p_view) { gproshan_log(APP_VIEWER); - return process_geodesics(p_view, geodesics::HEAT_FLOW_GPU); + return process_geodesics(p_view, geodesics::HEAT_METHOD_GPU); } #endif // GPROSHAN_CUDA diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index f014241f..486de3a2 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -1,7 +1,7 @@ #include "geodesics/geodesics.h" #include "geodesics/geodesics_ptp.h" -#include "geodesics/heat_flow.h" +#include "geodesics/heat_method.h" #include #include @@ -100,13 +100,13 @@ void geodesics::execute(che * mesh, const vector & sources, const param break; case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources, p.n_iter, p.radio); break; - case HEAT_FLOW: run_heat_flow(mesh, sources); + case HEAT_METHOD: run_heat_method(mesh, sources); break; #ifdef GPROSHAN_CUDA case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources, p.n_iter, p.radio); break; - case HEAT_FLOW_GPU: run_heat_flow_gpu(mesh, sources); + case HEAT_METHOD_GPU: run_heat_method_gpu(mesh, sources); break; #endif // GPROSHAN_CUDA } @@ -211,11 +211,11 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< delete [] toplesets; } -void geodesics::run_heat_flow(che * mesh, const vector & sources) +void geodesics::run_heat_method(che * mesh, const vector & sources) { double time_total, solve_time; TIC(time_total) - solve_time = heat_flow(dist, mesh, sources); + solve_time = heat_method(dist, mesh, sources); TOC(time_total) gproshan_log_var(time_total - solve_time); @@ -242,11 +242,11 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector< delete [] toplesets; } -void geodesics::run_heat_flow_gpu(che * mesh, const vector & sources) +void geodesics::run_heat_method_gpu(che * mesh, const vector & sources) { double time_total, solve_time; TIC(time_total) - dist = heat_flow_gpu(mesh, sources, solve_time); + dist = heat_method_gpu(mesh, sources, solve_time); TOC(time_total) gproshan_debug_var(time_total - solve_time); diff --git a/src/geodesics/heat_flow.cpp b/src/geodesics/heat_method.cpp similarity index 95% rename from src/geodesics/heat_flow.cpp rename to src/geodesics/heat_method.cpp index 3a3a3766..9c7870c0 100644 --- a/src/geodesics/heat_flow.cpp +++ b/src/geodesics/heat_method.cpp @@ -1,4 +1,4 @@ -#include "geodesics/heat_flow.h" +#include "geodesics/heat_method.h" #include "laplacian/laplacian.h" @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -double heat_flow(real_t * dist, che * mesh, const vector & sources) +double heat_method(real_t * dist, che * mesh, const vector & sources) { if(!sources.size()) return 0; @@ -62,7 +62,7 @@ double heat_flow(real_t * dist, che * mesh, const vector & sources) #ifdef GPROSHAN_CUDA -real_t * heat_flow_gpu(che * mesh, const vector & sources, double & solve_time) +real_t * heat_method_gpu(che * mesh, const vector & sources, double & solve_time) { if(!sources.size()) return 0; diff --git a/src/geodesics/heat_flow.cu b/src/geodesics/heat_method.cu similarity index 100% rename from src/geodesics/heat_flow.cu rename to src/geodesics/heat_method.cu diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 9e34e6b4..f23e3a43 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -2,7 +2,7 @@ #include "mesh/che_off.h" #include "geodesics/geodesics_ptp.h" -#include "geodesics/heat_flow.h" +#include "geodesics/heat_method.h" #include @@ -282,7 +282,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e real_t * dist = new real_t[mesh->n_vertices()]; for(int i = 0; i < n_test; i++) { - TIC(t) st = heat_flow(dist, mesh, source); TOC(t) + TIC(t) st = heat_method(dist, mesh, source); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); } @@ -325,7 +325,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact { if(dist) delete [] dist; - TIC(t) dist = heat_flow_gpu(mesh, source, st); TOC(t) + TIC(t) dist = heat_method_gpu(mesh, source, st); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); From 6e46715c43f0112ddf3386a6a00531c642030ea6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Dec 2020 13:34:37 +0100 Subject: [PATCH 0395/1018] fix viewer functional maps --- src/app_viewer.cpp | 4 ++-- src/mesh/che.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 86832a45..f466c204 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -298,9 +298,9 @@ bool app_viewer::process_functional_maps(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->heatmap(v) = eigvec(v, k); + view->active_mesh()->heatmap(v) = eigvec(v, k); - mesh.update_vbo(); + view->active_mesh().update_vbo(); } view->idx_active_mesh = 0; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index e948009b..3ab6cc25 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -74,6 +74,15 @@ che::che(const che & mesh) BT = new index_t[n_borders_]; memcpy(BT, mesh.BT, n_borders_ * sizeof(index_t)); + + VN = new vertex[n_vertices_]; + memcpy(VN, mesh.VN, n_vertices_ * sizeof(vertex)); + + VC = new vertex[n_vertices_]; + memcpy(VC, mesh.VC, n_vertices_ * sizeof(vertex)); + + VHC = new real_t[n_vertices_]; + memcpy(VHC, mesh.VHC, n_vertices_ * sizeof(real_t)); } che::che(const size_t & n_v, const size_t & n_f) From aca136416cc018b7335b12c381b5851ff5f798f1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Jan 2021 11:59:33 +0100 Subject: [PATCH 0396/1018] raytracing: set img buffer to zeros --- src/raytracing/raytracing.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index fbaad7c3..0f5b1c64 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -25,7 +25,7 @@ bool raytracing::rt_restart(const size_t & w, const size_t & h) { if(width * height < w * h) { - if(img) delete [] img; + delete [] img; width = w; height = h; @@ -63,12 +63,20 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, glm::vec4 li; + if(!n_samples) + { + #pragma omp parallel for + for(index_t i = 0; i < width; i++) + for(index_t j = 0; j < height; j++) + img[j * width + i] = glm::vec4(0); + } + #pragma omp parallel for private(li) for(index_t i = 0; i < width; i++) for(index_t j = 0; j < height; j++) { //row major - glm::vec4 & color = img[j * windows_size.x + i]; + glm::vec4 & color = img[j * width + i]; glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, (float(j) + randf(gen)) / height From 760dcf112d89625f718c35ad80e6944ced90d696 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Jan 2021 13:22:51 +0100 Subject: [PATCH 0397/1018] rt_embre: render pointcloud --- include/raytracing/raytracing.h | 13 ++++++++++++- include/raytracing/rt_embree.h | 14 +++++++------- src/raytracing/rt_embree.cpp | 20 +++++++++----------- src/viewer/viewer.cpp | 2 +- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index 378eed89..0e015b7b 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -17,7 +17,18 @@ namespace gproshan::rt { class raytracing { protected: - std::map geomID_mesh; + struct rt_mesh + { + che * mesh; + bool pointcloud; + + che * operator -> () const + { + return mesh; + } + }; + + std::map geomID_mesh; size_t width; size_t height; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 079673c2..18e4b70e 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -53,16 +53,16 @@ class embree : public raytracing return {ray.dir_x, ray.dir_y, ray.dir_z}; } - const glm::vec3 color(const che * mesh) + const glm::vec3 color(const rt_mesh & mesh) { - const vertex & c = mesh->is_pointcloud() ? mesh->color(hit.primID) : - mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : + mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::vec3(c.x, c.y, c.z); } - const glm::vec3 normal(const che * mesh, const bool & flat = false) const + const glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const { - if(flat || mesh->is_pointcloud()) + if(flat || mesh.pointcloud) return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); @@ -84,7 +84,7 @@ class embree : public raytracing static float pc_radius; public: - embree(const std::vector & meshes); + embree(const std::vector & meshes, const bool & pointcloud = false); ~embree(); private: @@ -94,7 +94,7 @@ class embree : public raytracing glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - void build_bvh(const std::vector & meshes); + void build_bvh(const std::vector & meshes, const bool & pointcloud = false); index_t add_sphere(const glm::vec4 & xyzr); index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 1c1b7453..0d1ff82f 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -24,7 +24,7 @@ void embree_error(void * ptr, RTCError error, const char * str) float embree::pc_radius = 0.001; -embree::embree(const std::vector & meshes) +embree::embree(const std::vector & meshes, const bool & pointcloud) { device = rtcNewDevice(NULL); rtcSetDeviceErrorFunction(device, embree_error, NULL); @@ -33,7 +33,7 @@ embree::embree(const std::vector & meshes) rtcInitIntersectContext(&intersect_context); - build_bvh(meshes); + build_bvh(meshes, pointcloud); } embree::~embree() @@ -42,11 +42,13 @@ embree::~embree() rtcReleaseDevice(device); } -void embree::build_bvh(const std::vector & meshes) +void embree::build_bvh(const std::vector & meshes, const bool & pointcloud) { for(auto & m: meshes) - if(m->n_faces()) geomID_mesh[add_mesh(m)] = m; - else geomID_mesh[add_point_cloud(m)] = m; + if(!m->n_faces() || pointcloud) + geomID_mesh[add_point_cloud(m)] = {m, true}; + else + geomID_mesh[add_mesh(m)] = {m, false}; rtcCommitScene(scene); } @@ -147,7 +149,7 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 sum_w += w = pc_radius - glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); position += w * r.position(); - normal += w * r.normal(geomID_mesh[r.hit.geomID], true); + normal += w * r.normal(geomID_mesh[r.hit.geomID]); color += w * r.color(geomID_mesh[r.hit.geomID]); r = ray_hit(r.position(), r.dir()); @@ -167,10 +169,6 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const const float dot_wi_normal = glm::dot(wi, normal); const glm::vec4 L = glm::vec4((dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); - // const float dist_light = glm::length(light - position); - // const float falloff = 4.f / (dist_light * dist_light); // intensity multiplier / falloff - //const glm::vec4 L = glm::vec4(falloff * (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (float(1.f/M_PI) * color) + glm::vec3(0.3), 1); - ray_hit r(position, wi, near); return (occluded(r) ? 0.6f : 1.f) * L; } @@ -192,7 +190,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) color = r.color(geomID_mesh[r.hit.geomID]); near = 1e-5f; - if(geomID_mesh[r.hit.geomID]->is_pointcloud()) + if(geomID_mesh[r.hit.geomID].pointcloud) near += pointcloud_hit(position, normal, color, r); L += r.ray.tfar * li(light, position, normal, color, near); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e96ac10a..ed4b70ce 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -738,7 +738,7 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - rt_embree = new rt::embree({active_mesh()}); + rt_embree = new rt::embree({active_mesh()}, render_pointcloud); TOC(time_build_embree); gproshan_log_var(time_build_embree); From cc4186c91c854b39cd217cc1e097b7d4eb84c48d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Jan 2021 15:03:15 +0100 Subject: [PATCH 0398/1018] viewer: set render option embree --- src/viewer/viewer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ed4b70ce..30339327 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -569,6 +569,8 @@ bool viewer::set_render_embree(viewer * view) } else { + view->render_opt = R_EMBREE; + ImGui::LabelText("disk radius", "%.4f", rt::embree::pc_radius); if(ImGui::Button("Reset")) From ca9688f66d06d0a4cd64052e3ccfb1f8b95794c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Jan 2021 12:41:39 +0100 Subject: [PATCH 0399/1018] mdict: cleaning up test_mesh_denoising --- CMakeLists.txt | 3 -- include/mdict/d_mesh_denoising.h | 21 ------------ src/app_viewer.cpp | 2 ++ src/mdict/d_mesh_denoising.cpp | 55 -------------------------------- test_mesh_denoising.cpp | 12 ------- 5 files changed, 2 insertions(+), 91 deletions(-) delete mode 100644 include/mdict/d_mesh_denoising.h delete mode 100644 src/mdict/d_mesh_denoising.cpp delete mode 100644 test_mesh_denoising.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a898b7bd..73dd8c56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,20 +116,17 @@ endif(CUDAToolkit_FOUND) add_executable(gproshan gproshan.cpp) add_executable(test_geodesics test_geodesics.cpp) add_executable(test_image_denoising test_image_denoising.cpp) -add_executable(test_mesh_denoising test_mesh_denoising.cpp) add_executable(test_hybrid_denoising test_hybrid_denoising.cpp) target_link_libraries(gproshan gproshan_cpp) target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) -target_link_libraries(test_mesh_denoising gproshan_cpp) target_link_libraries(test_hybrid_denoising gproshan_cpp) if(CUDAToolkit_FOUND) target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) - target_link_libraries(test_mesh_denoising gproshan_cu) target_link_libraries(test_hybrid_denoising gproshan_cu) endif(CUDAToolkit_FOUND) diff --git a/include/mdict/d_mesh_denoising.h b/include/mdict/d_mesh_denoising.h deleted file mode 100644 index e020ae27..00000000 --- a/include/mdict/d_mesh_denoising.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef D_MESH_DENOISING_H -#define D_MESH_DENOISING_H - -#include "mdict/denoising.h" - -#include "mesh/che_off.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -void test_mesh_denoising(const string & file); - - -} // namespace gproshan::mdict - - -#endif // D_MESH_DENOISING_H - diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6b861312..df0f5223 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -614,7 +614,9 @@ bool app_viewer::process_inpaiting(viewer * p_view) { basis_dct phi(n); inpainting dict(mesh, &phi, params); + real_t max_error = dict.execute(); + gproshan_log_var(max_error); mesh->update_heatmap(&dict[0]); mesh->update_normals(); diff --git a/src/mdict/d_mesh_denoising.cpp b/src/mdict/d_mesh_denoising.cpp deleted file mode 100644 index 9f50c05b..00000000 --- a/src/mdict/d_mesh_denoising.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "mdict/d_mesh_denoising.h" - -#include "mdict/basis_dct.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -void test_mesh_denoising(const string & file) -{ - che * mesh = new che_off(file.c_str()); - - size_t n = 16; - size_t m = 256; - size_t M = 0; - real_t f = 1.3; - bool learn = false; - real_t error; -// dictionary::L = 10; - basis * phi = new basis_dct(n); - denoising dict(mesh, phi, m, M, f, learn,0); - error = dict.execute(); - //dictionary::T = 3; - //denoising dict(mesh, phi, m, M, f, learn,0); - //dict.execute(); -/* - ofstream os("../tmp/test.txt"); - - for(; f<1.4; f+=0.1) - { - os<< f ; - for(size_t i = 10; i<26; i+=5) - { - dictionary::L = i; - mesh = new che_off(file.c_str()); - denoising dict(mesh, phi, m, M, f, learn,0); - error = dict.execute(); - os<< "\t"< Date: Tue, 5 Jan 2021 12:48:29 +0100 Subject: [PATCH 0400/1018] mdict: clean up d_hybrid_denoising --- CMakeLists.txt | 3 - MDICT.md | 1 - include/mdict/d_hybrid_denoising.h | 20 ---- src/mdict/d_hybrid_denoising.cpp | 144 ----------------------------- test_hybrid_denoising.cpp | 9 -- 5 files changed, 177 deletions(-) delete mode 100644 MDICT.md delete mode 100644 include/mdict/d_hybrid_denoising.h delete mode 100644 src/mdict/d_hybrid_denoising.cpp delete mode 100644 test_hybrid_denoising.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 73dd8c56..4d53abec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,18 +116,15 @@ endif(CUDAToolkit_FOUND) add_executable(gproshan gproshan.cpp) add_executable(test_geodesics test_geodesics.cpp) add_executable(test_image_denoising test_image_denoising.cpp) -add_executable(test_hybrid_denoising test_hybrid_denoising.cpp) target_link_libraries(gproshan gproshan_cpp) target_link_libraries(test_geodesics gproshan_cpp) target_link_libraries(test_image_denoising gproshan_cpp) -target_link_libraries(test_hybrid_denoising gproshan_cpp) if(CUDAToolkit_FOUND) target_link_libraries(gproshan gproshan_cu) target_link_libraries(test_geodesics gproshan_cu) target_link_libraries(test_image_denoising gproshan_cu) - target_link_libraries(test_hybrid_denoising gproshan_cu) endif(CUDAToolkit_FOUND) file(MAKE_DIRECTORY tmp) diff --git a/MDICT.md b/MDICT.md deleted file mode 100644 index 0fbea8fc..00000000 --- a/MDICT.md +++ /dev/null @@ -1 +0,0 @@ -# Private development remote for Mesh Dictionary Learning framework. diff --git a/include/mdict/d_hybrid_denoising.h b/include/mdict/d_hybrid_denoising.h deleted file mode 100644 index ce756eaf..00000000 --- a/include/mdict/d_hybrid_denoising.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef D_HYBRID_DENOISING_H -#define D_HYBRID_DENOISING_H - - -#include - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -void test_hybrid_denoising(const std::string & file); - - -} // namespace gproshan::mdict - - -#endif // D_HYBRID_DENOISING_H - diff --git a/src/mdict/d_hybrid_denoising.cpp b/src/mdict/d_hybrid_denoising.cpp deleted file mode 100644 index e764096b..00000000 --- a/src/mdict/d_hybrid_denoising.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "mdict/d_hybrid_denoising.h" - -#include "mesh/che_off.h" -#include "mesh/che_img.h" - -#include "mdict/mdict.h" -#include "mdict/patch.h" -#include "mdict/basis.h" -#include "mdict/basis_dct.h" -#include "mdict/d_mesh.h" - - -#include "include_arma.h" - -#include - -using namespace cimg_library; - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -void test_hybrid_denoising(const string & file) -{ - size_t N = 128; - - CImg image(file.c_str()); - image.resize(N, N); - image.save("../tmp/image_128.jpg"); - (image).display(); - image = image.get_normalize(0, 1); - (image).display(); - size_t p = 8; // square side of each patche - size_t rows = image.width(); - size_t cols = image.height(); - size_t n = p * p; // size of each patche - size_t n_basis = 4; - size_t m = n_basis * n_basis; // number of atoms - size_t M = rows * cols; // number of patches - size_t L = 10; // sparsity OMP norm L_0 - size_t K = 10; // KSVD iterations - - - a_mat X(n, M); - che * mesh = new che_img("../tmp/image_128.jpg"); - //mesh->write_file("../tmp/image_128"); - che_off::write_file(mesh,"../tmp/barbara_input_img"); - - std::vector patches(M); ///< vector of patches. - std::vector patches_map(M); ///< invert index vertex to patches. -/* - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) - { - index_t i = x + y * rows; - - for(index_t b = y; b < cols && b < y + p; b++) - for(index_t a = x; a < rows && a < x + p; a++) - patches[i].vertices.push_back(a + b * rows); - } - - a_mat A; - a_mat alpha; - basis * phi_basis = new basis_dct(n_basis); - A.eye(m, m); - alpha.zeros(m, M); - - for(index_t s = 0; s < M; s++) - { - patches[s].reset_xyz(mesh, patches_map, s, nullptr); - } - - //#pragma omp parallel for - for(index_t s = 0; s < M; s++) - { - patch & p = patches[s]; - //p.T.eye(3, 3); - //p.transform(); - p.phi.set_size(p.xyz.n_cols, n_basis*n_basis); - // gproshan_debug_var(p.xyz); - phi_basis->discrete(p.phi, p.xyz); - } - - OMP_all_patches_ksvt(alpha, A, patches, M, L); - gproshan_debug_var(size(alpha)); - - //Mesh reconstruction - for(index_t p = 0; p < M; p++) - { - patch & rp = patches[p]; - - if(rp.phi.n_rows) - { - a_vec x = rp.phi * A * alpha.col(p); - gproshan_debug_var(alpha.col(p)); - rp.xyz.row(2) = x.t(); - // rp.itransform(); - } - } - - a_mat V(3, mesh->n_vertices(), arma::fill::zeros); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - if(patches_map[v].size()) - V.col(v) = mdict::simple_means_vertex(v, patches, patches_map); - } - vertex * new_vertices = (vertex *) V.memptr(); - mesh->set_vertices(new_vertices, mesh->n_vertices(), 0); -*/ - char * input_str = "barbara_input.jpg"; - CImg image_out=image; - - for(size_t v = 0; v < mesh->n_vertices(); v++) - image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; - - image_out.save("../tmp/barbara_input.jpg"); - //che_img::save_img(mesh, "../tmp/barbara_input.jpg", rows); -/* - CImg image_out = image; - image_out.fill(0); - - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) - { - index_t i = x + y * rows; - - image_out(x, y) = mesh->gt(i).z; - // gproshan_debug_var(mesh->gt(i).z); - } - image_out.save(tmp_file_path("barbara_input.jpg").c_str()); -// CImg diff = abs(image - image_out); -// (image, image_out, diff).display();*/ -// (image_out).display(); -// image_out = image_out.get_normalize(0, 255); -// CImg image_out("../tmp/barbara_input.jpg"); - (image, image_out).display(); -} - - -} // namespace gproshan::mdict - diff --git a/test_hybrid_denoising.cpp b/test_hybrid_denoising.cpp deleted file mode 100644 index 427dd6bd..00000000 --- a/test_hybrid_denoising.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "mdict/d_hybrid_denoising.h" - -int main(int nargs, const char ** args) -{ - gproshan::mdict::test_hybrid_denoising(args[1]); - - return 0; -} - From 63c63c15427cb4e8d664cf46847d2beee8a5488c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Jan 2021 15:06:49 +0100 Subject: [PATCH 0401/1018] mdict: clening up d_mesh deprecated functions --- include/mdict/d_mesh.h | 128 ------------- include/mdict/dictionary.h | 4 +- include/mdict/mdict.h | 1 - src/mdict/d_mesh.cpp | 376 ------------------------------------- src/mdict/dictionary.cpp | 82 +------- src/mdict/inpainting.cpp | 1 + src/mdict/patch.cpp | 1 + 7 files changed, 13 insertions(+), 580 deletions(-) delete mode 100644 include/mdict/d_mesh.h delete mode 100644 src/mdict/d_mesh.cpp diff --git a/include/mdict/d_mesh.h b/include/mdict/d_mesh.h deleted file mode 100644 index 22dff8c7..00000000 --- a/include/mdict/d_mesh.h +++ /dev/null @@ -1,128 +0,0 @@ -#ifndef D_MESH_H -#define D_MESH_H - -#include "include.h" -#include "mesh/che.h" -#include "mdict/patch.h" -#include "geodesics/geodesics.h" - -#include "include_arma.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -typedef void * params_t[]; -typedef void (* phi_function_t) (a_mat &, a_mat &, params_t); - -typedef map< index_t, index_t > patches_map_t; - -struct patch_t; - -void jet_fit_directions(patch_t & rp); -void PCA(patch_t & rp); -void principal_curvatures(patch_t & rp, che * mesh); - -struct patch_t -{ - static bool del_index; - static size_t min_nvp; - - size_t n; - index_t * indexes; - a_mat xyz; - a_vec avg; - a_mat E; - a_mat phi; - - patch_t() - { - indexes = nullptr; - } - - ~patch_t() - { - if(del_index) - if(indexes) delete [] indexes; - } - - index_t operator[](index_t i) - { - return indexes[i]; - } - - // xyz = E.t * (xyz - avg) - void transform() - { - xyz.each_col() -= avg; - xyz = E.t() * xyz; - } - - void itransform() - { - xyz = E * xyz; - xyz.each_col() += avg; - } - - bool valid_xyz() - { - return xyz.n_cols > min_nvp; - } - - void reset_xyz(che * mesh, std::vector & patches_map, const index_t & p, const index_t & threshold = NIL) - { - size_t m = n; - if(threshold != NIL) - { - m = 0; - for(index_t i = 0; i < n; i++) - if(indexes[i] < threshold) m++; - } - - xyz.set_size(3, m); - for(index_t j = 0, i = 0; i < n; i++) - { - if(indexes[i] < threshold) - { - const vertex & v = mesh->gt(indexes[i]); - xyz(0, j) = v.x; - xyz(1, j) = v.y; - xyz(2, j) = v.z; - - patches_map[indexes[i]][p] = j++; - } - } - } -}; - -a_vec gaussian(a_mat & xy, real_t sigma, real_t cx, real_t cy); - -a_vec cossine(a_mat & xy, real_t radio, size_t K); - -void phi_gaussian(a_mat & phi, a_mat & xy, void ** params); - -void get_centers_gaussian(a_vec & cx, a_vec & cy, real_t radio, size_t K); - -void save_patches_coordinates(std::vector & patches, std::vector > * lpatches, size_t NV); - -void save_patches(std::vector patches, size_t M); - -void partial_mesh_reconstruction(size_t old_n_vertices, che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha); - -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); - -[[deprecated]] -void mesh_reconstruction(che * mesh, size_t M, std::vector & patches, std::vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i = 0); - -[[deprecated]] -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, std::vector & patches, std::vector & patches_map, const real_t & h); - -a_vec simple_means_vertex( const index_t & v, std::vector & patches, std::vector & patches_map); - - -} // namespace gproshan::mdict - -#endif // D_MESH_H - diff --git a/include/mdict/dictionary.h b/include/mdict/dictionary.h index 45a53394..6891b53d 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/dictionary.h @@ -5,7 +5,6 @@ #include "mdict/patch.h" #include "mdict/mdict.h" #include "mdict/basis.h" -#include "mdict/d_mesh.h" #include "include_arma.h" @@ -69,7 +68,7 @@ class dictionary basis *const &_phi_basis, ///< pointer to continuous basis. const size_t & _m, ///< number of dictionary atoms. const size_t & _M, ///< number of patches. - const real_t & _f, ///< deprecated + const real_t & _f, ///< deprecated const bool & _learn, const bool & _plot ///< flag to plot basis and atoms with gnuplot. ); @@ -81,7 +80,6 @@ class dictionary void learning(); void sparse_coding(); void init_sampling(); - void load_curvatures(a_vec & curvatures); void load_features(vector & v_feat, size_t & featsize); void init_patches( const bool & reset = 1, const fmask_t & mask = nullptr diff --git a/include/mdict/mdict.h b/include/mdict/mdict.h index 01245520..8289df5b 100644 --- a/include/mdict/mdict.h +++ b/include/mdict/mdict.h @@ -3,7 +3,6 @@ #include "include.h" -#include "mdict/d_mesh.h" #include "mdict/patch.h" #include "mdict/basis.h" diff --git a/src/mdict/d_mesh.cpp b/src/mdict/d_mesh.cpp deleted file mode 100644 index 04bdbaa5..00000000 --- a/src/mdict/d_mesh.cpp +++ /dev/null @@ -1,376 +0,0 @@ -#include "mdict/d_mesh.h" - -#ifndef CGAL_PATCH_DEFS - #define CGAL_PATCH_DEFS - #define CGAL_EIGEN3_ENABLED - #define CGAL_USE_BOOST_PROGRAM_OPTIONS - #define CGAL_USE_GMP - #define DCGAL_USE_MPFR -#endif - -#include -#include - - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -typedef real_t DFT; -typedef CGAL::Simple_cartesian Data_Kernel; -typedef Data_Kernel::Point_3 DPoint; -typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; -typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; - - -size_t patch_t::min_nvp = 36; -bool patch_t::del_index = false; - -a_vec gaussian(a_mat & xy, real_t sigma, real_t cx, real_t cy) -{ - a_vec x = xy.row(0).t() - cx; - a_vec y = xy.row(1).t() - cy; - - x.for_each( [] (a_mat::elem_type & val) { val *= val; } ); - y.for_each( [] (a_mat::elem_type & val) { val *= val; } ); - - return exp( - ( x + y ) / ( 2 * sigma * sigma ) ); -} - -a_vec cossine(a_mat & xy, real_t radio, size_t K) -{ - a_vec x = xy.row(0).t() + 0.5; - a_vec y = xy.row(1).t() + 0.5; - - - size_t k = sqrt(K); - a_vec sum(x.n_elem, arma::fill::zeros); - a_vec tmp; - - for(index_t nx = 0; nx < k; nx++) - for(index_t ny = 0; ny < k; ny++) - { - tmp = cos( (M_PI*x*(nx-1)+nx)/radio ) % cos((M_PI*y*(ny-1)+ny)/radio ); - } sum += tmp; - - return sum; -} - -void phi_gaussian(a_mat & phi, a_mat & xy, params_t params) -{ - a_vec & cx = *( (a_vec * ) params[0] ); - a_vec & cy = *( (a_vec * ) params[1] ); - real_t sigma = *( (real_t * ) params[2] ); - - size_t K = phi.n_cols; - - for(index_t k = 0 ; k < K; k++) - phi.col(k) = gaussian(xy, sigma, cx(k), cy(k)); -} - -void get_centers_gaussian(a_vec & cx, a_vec & cy, real_t radio, size_t K) -{ - if(K == 1) - { - cx(0) = cy(0) = 0; - return; - } - - size_t k = sqrt(K); - real_t d = 2 * radio / (k - 1); - - for(index_t c = 0, i = 0; i < k; i++) - for(index_t j = 0; j < k; j++, c++) - { - cx(c) = -radio + d * i; - cy(c) = -radio + d * j; - } -} - -void jet_fit_directions(patch_t & rp) -{ - vector in_points; - in_points.reserve(rp.n); - for(index_t i = 0; i < rp.xyz.n_cols; i++) - in_points.push_back(DPoint(rp.xyz(0, i), rp.xyz(1, i), rp.xyz(2, i))); - - size_t d_fitting = 4; - size_t d_monge = 4; - - My_Monge_form monge_form; - My_Monge_via_jet_fitting monge_fit; - monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); - - rp.avg.set_size(3); - rp.avg(0) = monge_form.origin()[0]; - rp.avg(1) = monge_form.origin()[1]; - rp.avg(2) = monge_form.origin()[2]; - rp.E.set_size(3,3); - rp.E(0, 0) = monge_form.maximal_principal_direction()[0]; - rp.E(1, 0) = monge_form.maximal_principal_direction()[1]; - rp.E(2, 0) = monge_form.maximal_principal_direction()[2]; - rp.E(0, 1) = monge_form.minimal_principal_direction()[0]; - rp.E(1, 1) = monge_form.minimal_principal_direction()[1]; - rp.E(2, 1) = monge_form.minimal_principal_direction()[2]; - rp.E(0, 2) = monge_form.normal_direction()[0]; - rp.E(1, 2) = monge_form.normal_direction()[1]; - rp.E(2, 2) = monge_form.normal_direction()[2]; -} - -void PCA(patch_t & rp) -{ - rp.avg = mean(rp.xyz, 1); - rp.xyz.each_col() -= rp.avg; - - a_mat C = rp.xyz * rp.xyz.t(); - a_vec eigval; - eig_sym(eigval, rp.E, C); - - rp.E.swap_cols(0, 2); -} - -void principal_curvatures( patch_t & rp, che * mesh) -{ - rp.avg = rp.xyz.col(0); - - vertex N = mesh->normal(rp[0]); - - vertex max; - real_t K = -INFINITY; - real_t k; - - for_star(he, mesh, rp[0]) - { - vertex d = mesh->gt_vt(next(he)) - mesh->gt_vt(he); - d /= *d; - k = (N,d); - - if(K < k) - { - max = d; - K = k; - } - } - - rp.E.set_size(3, 3); - rp.E(0, 2) = N.x; - rp.E(1, 2) = N.y; - rp.E(2, 2) = N.z; - - max -= K * N; - rp.E(0, 0) = max.x; - rp.E(1, 0) = max.y; - rp.E(2, 0) = max.z; - - rp.E.col(1) = cross(rp.E.col(0), rp.E.col(2)); - rp.E.col(1) /= norm(rp.E.col(1)); -} - -void save_patches_coordinates(vector & patches, vector< pair > * lpatches, size_t NV) -{ - ofstream os(tmp_file_path("test-patches_coordinates")); - - for(index_t v = 0; v < NV; v++) - { - for(auto pi: lpatches[v]) - { - patch_t & rp = patches[pi.first]; - os< & patches, size_t M) -{ - ofstream os(tmp_file_path("test-patch_wise_coordinates")); - - for(index_t p = 0; p < M; p++) - { - patch_t & rp = patches[p]; - for(index_t i = 0; i < rp.n; i++) - { - os< & patches, vector & patches_map, a_mat & A, a_mat & alpha) -{ - #pragma omp parallel for - for(index_t p = M; p < patches.size(); p++) - { - patch_t & rp = patches[p]; - - if(rp.indexes) - { - a_vec x = rp.phi * A * alpha.col(p); - - rp.xyz.row(2) = x.t(); - - rp.itransform(); - } - } - - real_t h = 2; - - a_vec V(3); - - #pragma omp parallel for private(V) - for(index_t v = old_n_vertices; v < mesh->n_vertices(); v++) - { - if(patches_map[v].size()) - V = non_local_means_vertex(alpha, v, patches, patches_map, h); - else - { - V(0) = mesh->gt(v).x; - V(1) = mesh->gt(v).y; - V(2) = mesh->gt(v).z; - } - - mesh->get_vertex(v) = *((vertex *) V.memptr()); - } - -} - -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) -{ - a_vec n_a_vec(3, arma::fill::zeros); - real_t sum = 0; - - real_t * w = new real_t[patches_map[v].size()]; - - index_t i = 0; - real_t d = 0; - - for(auto p: patches_map[v]) - { - d = 0; - for(auto q: patches_map[v]) - d += norm(alpha.col(p.first) - alpha.col(q.first)); - d /= patches_map[v].size(); - - w[i] = exp(- d * d / h); - sum += w[i]; - i++; - } - - i = 0; - for(auto p: patches_map[v]) - { - w[i] /= sum; - n_a_vec += w[i] * patches[p.first].xyz.col(p.second); - i++; - } - - delete [] w; - - return n_a_vec; -} - -[[deprecated]] -void mesh_reconstruction(che * mesh, size_t M, vector & patches, vector & patches_map, a_mat & A, a_mat & alpha, const index_t & v_i) -{ - a_mat V(3, mesh->n_vertices(), arma::fill::zeros); - - #pragma omp parallel for - for(index_t p = 0; p < M; p++) - { - patch_t & rp = patches[p]; - - if(rp.phi.n_rows) - { - a_vec x = rp.phi * A * alpha.col(p); - - rp.xyz.row(2) = x.t(); - rp.itransform(); - } - } - - real_t h = 0.2; - - #pragma omp parallel for - for(index_t v = v_i; v < mesh->n_vertices(); v++) - { - if(patches_map[v].size()) - V.col(v) = non_local_means_vertex(alpha, v, patches, patches_map, h); - else - { - V(0, v) = mesh->gt(v).x; - V(1, v) = mesh->gt(v).y; - V(2, v) = mesh->gt(v).z; - } - } - - - // ------------------------------------------------------------------------ - - vertex * new_vertices = (vertex *) V.memptr(); - - real_t error = 0; - #pragma omp parallel for reduction(+: error) - for(index_t v = v_i; v < mesh->n_vertices(); v++) - error += *(new_vertices[v] - mesh->get_vertex(v)); - - gproshan_debug_var(mesh->n_vertices()); - error /= mesh->n_vertices(); - gproshan_debug_var(error); - - gproshan_debug_var(v_i); - mesh->set_vertices(new_vertices + v_i, mesh->n_vertices() - v_i, v_i); -} - -[[deprecated]] -a_vec non_local_means_vertex(a_mat & alpha, const index_t & v, vector & patches, vector & patches_map, const real_t & h) -{ - a_vec n_a_vec(3, arma::fill::zeros); - real_t sum = 0; - - real_t * w = new real_t[patches_map[v].size()]; - - index_t i = 0; - real_t d = 0; - - for(auto p: patches_map[v]) - { - d = 0; - for(auto q: patches_map[v]) - d += norm(alpha.col(p.first) - alpha.col(q.first)); - d /= patches_map[v].size(); - - w[i] = exp(- d * d / h); - sum += w[i]; - i++; - } - - for(auto p: patches_map[v]) - { - w[i] /= sum; - n_a_vec += w[i] * patches[p.first].xyz.col(p.second); - i++; - } - - delete [] w; - - return n_a_vec/i; -} - -a_vec simple_means_vertex( const index_t & v, vector & patches, vector & patches_map) -{ - a_vec n_a_vec(3, arma::fill::zeros); - - for(auto p: patches_map[v]) - n_a_vec += patches[p.first].xyz.col(p.second); - return n_a_vec/ patches_map[v].size(); -} - - -} // namespace gproshan::mdict - diff --git a/src/mdict/dictionary.cpp b/src/mdict/dictionary.cpp index 8f5d1bab..fbb43245 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/dictionary.cpp @@ -53,7 +53,6 @@ dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size dictionary::~dictionary() { - patch_t::del_index = true; } void dictionary::learning() @@ -135,75 +134,6 @@ void dictionary::init_sampling() gproshan_debug_var(phi_basis->radio()); } -void dictionary::load_curvatures(a_vec & curvatures) -{ - string f_curv = tmp_file_path(mesh->name_size() + ".curv"); - string f_norm = tmp_file_path(mesh->name_size() + ".n"); - - if(! curvatures.load(f_curv)) - { - curvatures.zeros(mesh->n_vertices()); - //real_t *mean_curvature = new real_t[mesh->n_vertices()]; - vector points; - - map non_rep; - map::iterator it; - size_t d_fitting = 2; - size_t d_monge = 2; - size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - - real_t min = INFINITY; - real_t max = -INFINITY; - a_mat normals; - normals.zeros(3, mesh->n_vertices()); - - for(index_t v = 0; v < mesh->n_vertices(); v++) - { - link_t linkv; - mesh->link(linkv, v); - for(const index_t & he: linkv) - { - link_t linku; - const index_t & u = mesh->vt(he); - mesh->link(linku, u); - for(const index_t & he: linku) - { - it = non_rep.find(u); - if(it == non_rep.end()) - points.push_back(u); - - } - } - assert(points.size() > min_points); - vector in_points; - in_points.reserve(points.size()); - for(const index_t & u: points) - in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); - - My_Monge_form monge_form; - My_Monge_via_jet_fitting monge_fit; - monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); - - vertex normal = mesh->normal(v); - monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); - curvatures(v) = ( monge_form.principal_curvatures(0) + monge_form.principal_curvatures(0) ) / 2; - - - normals(0, v) = monge_form.normal_direction()[0]; - normals(1, v) = monge_form.normal_direction()[1]; - normals(2, v) = monge_form.normal_direction()[2]; - //gproshan_debug_var(mean_curvature[v]); - points.clear(); - non_rep.clear(); - - } - curvatures.save(f_curv); - normals.save(f_norm); - } - gproshan_debug(curvatures ready); - -} - void dictionary::load_features(vector & v_feat, size_t & featsize) { string f_feat = tmp_file_path(mesh->name() + ".int"); @@ -380,8 +310,15 @@ real_t dictionary::mesh_reconstruction(const fmask_t & mask) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) { - if(patches_map[v].size() && (!mask || mask(v)) ) - V.col(v) = simple_means_vertex(v, patches, patches_map); + // simple means vertex + if(patches_map[v].size() && (!mask || mask(v))) + { + a_vec mv = arma::zeros(3); + for(auto p: patches_map[v]) + mv += patches[p.first].xyz.col(p.second); + + V.col(v) = mv / patches_map[v].size(); + } else { V(0, v) = mesh->gt(v).x; @@ -484,5 +421,6 @@ void dictionary::save_alpha(string file) alpha.save(file); } + } // namespace gproshan::mdict diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 7200f21f..e87af52b 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -1,6 +1,7 @@ #include "mdict/inpainting.h" #include "mesh/che_off.h" +#include "geodesics/geodesics.h" #include #include diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 498cefea..768f2e05 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -1,6 +1,7 @@ #include "mdict/patch.h" #include "mdict/dictionary.h" +#include "geodesics/geodesics.h" #include #include From d1fb7b871b00344b4fcd203722233af32a9c8bb3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Jan 2021 15:16:49 +0100 Subject: [PATCH 0402/1018] mdict: bug using arma sort index "descend", review, test again --- src/mdict/inpainting.cpp | 4 +++- src/mdict/mdict.cpp | 6 +++--- src/mdict/super_resolution.cpp | 2 ++ src/mdict/synthesis.cpp | 2 ++ 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index e87af52b..0564ce88 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -511,7 +511,9 @@ real_t inpainting::execute() arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); - gproshan_debug_var(ratio); + gproshan_log_var(ratio); + + return max_error; } che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index b751e871..cd03b28c 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -136,12 +136,12 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) { - - arma::uvec indices = arma::sort_index( V , "desscend"); + arma::uvec indices = arma::sort_index(V, "descend"); for(size_t i=0; i< V.size(); i++) - if(mask[indices [i]]) return indices[i]; + if(mask[indices[i]]) return indices[i]; + return NIL; } tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) diff --git a/src/mdict/super_resolution.cpp b/src/mdict/super_resolution.cpp index 2aa78a0f..32e768c9 100644 --- a/src/mdict/super_resolution.cpp +++ b/src/mdict/super_resolution.cpp @@ -26,6 +26,8 @@ real_t super_resolution::execute() TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); + + return 0; } diff --git a/src/mdict/synthesis.cpp b/src/mdict/synthesis.cpp index 4d0be6b3..b125e3f7 100644 --- a/src/mdict/synthesis.cpp +++ b/src/mdict/synthesis.cpp @@ -36,6 +36,8 @@ real_t synthesis::execute() TIC(d_time) mesh_reconstruction(); TOC(d_time) gproshan_debug_var(d_time); + + return 0; } From 3cc45b57d1cb7e1e6013cf77a22cad3872a54417 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Jan 2021 11:39:17 +0100 Subject: [PATCH 0403/1018] mdict: remove extra codes --- include/app_viewer.h | 5 --- include/mdict/super_resolution.h | 25 ------------- include/mdict/synthesis.h | 25 ------------- src/app_viewer.cpp | 61 -------------------------------- src/mdict/super_resolution.cpp | 35 ------------------ src/mdict/synthesis.cpp | 45 ----------------------- 6 files changed, 196 deletions(-) delete mode 100644 include/mdict/super_resolution.h delete mode 100644 include/mdict/synthesis.h delete mode 100644 src/mdict/super_resolution.cpp delete mode 100644 src/mdict/synthesis.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index b6244cb6..9d5726c1 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -23,10 +23,8 @@ #include "geodesics/sampling.h" #include "mdict/denoising.h" -#include "mdict/super_resolution.h" #include "mdict/inpainting.h" #include "mdict/basis_dct.h" -#include "mdict/synthesis.h" #include "mdict/patch.h" #include "features/key_points.h" @@ -69,12 +67,9 @@ class app_viewer : public viewer static bool process_mdict_patch(viewer * p_view); static bool process_denoising(viewer * p_view); - static bool process_super_resolution(viewer * p_view); static bool process_inpaiting(viewer * p_view); - static bool process_iterative_inpaiting(viewer * p_view); static bool process_mask(viewer * p_view); static bool process_pc_reconstruction(viewer * p_view); - static bool process_synthesis(viewer * p_view); static bool process_functional_maps(viewer * p_view); static bool process_gps(viewer * p_view); diff --git a/include/mdict/super_resolution.h b/include/mdict/super_resolution.h deleted file mode 100644 index b4d60856..00000000 --- a/include/mdict/super_resolution.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef SUPER_RESOLUTION_H -#define SUPER_RESOLUTION_H - -#include "mdict/dictionary.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -class super_resolution : public dictionary -{ - public: - super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool &_learn, const bool & _plot = true); - virtual ~super_resolution() = default; - - real_t execute(); -}; - - -} // namespace gproshan::mdict - -#endif // SUPER_RESOLUTION_H - diff --git a/include/mdict/synthesis.h b/include/mdict/synthesis.h deleted file mode 100644 index f55e5199..00000000 --- a/include/mdict/synthesis.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef SYNTHESIS_H -#define SYNTHESIS_H - -#include "dictionary.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -class synthesis : public dictionary -{ - public: - synthesis(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _plot = true); - virtual ~synthesis() = default; - - real_t execute(); -}; - - -} // namespace gproshan::mdict - -#endif // SYNTHESIS_H - diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index df0f5223..74e6e5b4 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -77,12 +77,9 @@ int app_viewer::main(int nargs, const char ** args) sub_menus.push_back("Dictionary Learning"); add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); add_process(GLFW_KEY_D, {"D", "MDICT Denoising", process_denoising}); - add_process(GLFW_KEY_A, {"A", "MDICT Super Resolution", process_super_resolution}); add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); - add_process(GLFW_KEY_F14, {"F14", "MDICT Synthesis", process_synthesis}); -// add_process('A', "IT Inpainting", process_iterative_inpaiting); sub_menus.push_back("Signatures"); add_process(GLFW_KEY_2, {"2", "GPS", process_gps}); @@ -568,30 +565,6 @@ bool app_viewer::process_denoising(viewer * p_view) return false; } -bool app_viewer::process_super_resolution(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - size_t n; // dct - size_t m, M; - real_t f; - bool learn; - - gproshan_log(parameters: (n, m, M, f, learn)); - cin >> n >> m >> M >> f >> learn; - - basis * phi = new basis_dct(n); - super_resolution dict(mesh, phi, m, M, f, learn); - dict.execute(); - - delete phi; - mesh->update_normals(); - - return false; -} - bool app_viewer::process_inpaiting(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; @@ -699,40 +672,6 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) return true; } -bool app_viewer::process_synthesis(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - static dictionary::params params; - - size_t n; // dct - size_t m, M; - real_t f; - bool learn; - - gproshan_log(parameters: (n, m, M, f, learn)); - cin >> n >> m >> M >> f >> learn; - - basis_dct phi(n); - inpainting dict(mesh, &phi, params); - dict.execute(); - - mesh->update_normals(); - - return false; -} - -bool app_viewer::process_iterative_inpaiting(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - -// mesh_iterative_inpaiting(mesh, mesh.selected, freq, rt, m, M, f, learn); - - return false; -} - bool app_viewer::process_multiplicate_vertices(viewer * p_view) { gproshan_log(APP_VIEWER); diff --git a/src/mdict/super_resolution.cpp b/src/mdict/super_resolution.cpp deleted file mode 100644 index 32e768c9..00000000 --- a/src/mdict/super_resolution.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "mdict/super_resolution.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -super_resolution::super_resolution(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) -{ -} - -real_t super_resolution::execute() -{ - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) init_patches(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) learning(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) mesh_reconstruction(); TOC(d_time) - gproshan_debug_var(d_time); - - return 0; -} - - -} // namespace gproshan::mdict - diff --git a/src/mdict/synthesis.cpp b/src/mdict/synthesis.cpp deleted file mode 100644 index b125e3f7..00000000 --- a/src/mdict/synthesis.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "mdict/synthesis.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -synthesis::synthesis(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) -{ -} - -real_t synthesis::execute() -{ - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) init_patches(); TOC(d_time) - gproshan_debug_var(d_time); - - //TIC(d_time) learning(); TOC(d_time) - /* - string name; - d_message(Dictionary name:) - cin>>name; - string f_dict = "tmp/" + name + ".dict"; - debug(f_dict) - d_message(loading dictionary) - if(!A.load(f_dict)) - { - d_message(This dictionary does not exist Bye) return; - } - gproshan_debug_var(d_time);*/ - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) mesh_reconstruction(); TOC(d_time) - gproshan_debug_var(d_time); - - return 0; -} - - -} // namespace gproshan::mdict - From 690b4e230571251531620fc8e058d14c00ccb39e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Jan 2021 21:14:29 +0100 Subject: [PATCH 0404/1018] mdict: cleaning denoising class --- include/app_viewer.h | 2 -- include/mdict/denoising.h | 25 ------------------------- src/app_viewer.cpp | 31 ------------------------------- src/mdict/denoising.cpp | 38 -------------------------------------- 4 files changed, 96 deletions(-) delete mode 100644 include/mdict/denoising.h delete mode 100644 src/mdict/denoising.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index 9d5726c1..bd0200f3 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -22,7 +22,6 @@ #include "geodesics/geodesics.h" #include "geodesics/sampling.h" -#include "mdict/denoising.h" #include "mdict/inpainting.h" #include "mdict/basis_dct.h" #include "mdict/patch.h" @@ -66,7 +65,6 @@ class app_viewer : public viewer static bool process_voronoi(viewer * p_view); static bool process_mdict_patch(viewer * p_view); - static bool process_denoising(viewer * p_view); static bool process_inpaiting(viewer * p_view); static bool process_mask(viewer * p_view); static bool process_pc_reconstruction(viewer * p_view); diff --git a/include/mdict/denoising.h b/include/mdict/denoising.h deleted file mode 100644 index 5649cab0..00000000 --- a/include/mdict/denoising.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef DENOISING_H -#define DENOISING_H - -#include "mdict/dictionary.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -class denoising : public dictionary -{ - public: - denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _plot = false); - virtual ~denoising() = default; - - real_t execute(); -}; - - -} // namespace gproshan::mdict - -#endif // DENOISING_H - diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 74e6e5b4..d7b3649b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -76,7 +76,6 @@ int app_viewer::main(int nargs, const char ** args) sub_menus.push_back("Dictionary Learning"); add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); - add_process(GLFW_KEY_D, {"D", "MDICT Denoising", process_denoising}); add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); @@ -535,36 +534,6 @@ bool app_viewer::process_mdict_patch(viewer * p_view) return false; } -bool app_viewer::process_denoising(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - size_t n; // dct - size_t m, M; - real_t f; - bool learn; - - gproshan_input(n m M f learn); - cin >> n >> m >> M >> f >> learn; - - basis * phi = new basis_dct(n); - denoising dict(mesh, phi, m, M, f, learn); - dict.execute(); - - delete phi; - mesh->update_heatmap(&dict[0]); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->color(v) = 2 * atan(mesh->heatmap(v) * 10) / M_PI; - - mesh->update_normals(); - - return false; -} - bool app_viewer::process_inpaiting(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; diff --git a/src/mdict/denoising.cpp b/src/mdict/denoising.cpp deleted file mode 100644 index 5bbb5900..00000000 --- a/src/mdict/denoising.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "mdict/denoising.h" - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -denoising::denoising(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _plot): dictionary(_mesh, _phi_basis, _m, _M, _f, _learn, _plot) -{ -} - -real_t denoising::execute() -{ - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) init_patches(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) learning(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); - draw_patches(128); - draw_patches(12); - phi_basis->plot_atoms(A); - phi_basis->plot_basis(); - TIC(d_time) - real_t error = mesh_reconstruction(); TOC(d_time) - gproshan_debug_var(d_time); - return error; -} - - -} // namespace gproshan::mdict - From 4dcb453270eecb27942225d3fcc5a5d9ee1ae0a7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Jan 2021 21:54:39 +0100 Subject: [PATCH 0405/1018] mdict: rename class dictionary to msparse_coding --- include/mdict/inpainting.h | 4 +-- .../mdict/{dictionary.h => msparse_coding.h} | 6 ++-- include/mdict/patch.h | 4 +-- src/app_viewer.cpp | 8 ++--- src/mdict/inpainting.cpp | 4 +-- .../{dictionary.cpp => msparse_coding.cpp} | 36 +++++++++---------- src/mdict/patch.cpp | 4 +-- 7 files changed, 33 insertions(+), 33 deletions(-) rename include/mdict/{dictionary.h => msparse_coding.h} (95%) rename src/mdict/{dictionary.cpp => msparse_coding.cpp} (89%) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 87b1a526..75a05dcd 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -1,7 +1,7 @@ #ifndef INPAINTING_H #define INPAINTING_H -#include "mdict/dictionary.h" +#include "mdict/msparse_coding.h" #include "mesh/che_poisson.h" #include "mesh/che_fill_hole.h" @@ -12,7 +12,7 @@ namespace gproshan::mdict { -class inpainting : public dictionary +class inpainting : public msparse_coding { public: inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p); diff --git a/include/mdict/dictionary.h b/include/mdict/msparse_coding.h similarity index 95% rename from include/mdict/dictionary.h rename to include/mdict/msparse_coding.h index 6891b53d..aa71d789 100644 --- a/include/mdict/dictionary.h +++ b/include/mdict/msparse_coding.h @@ -14,7 +14,7 @@ namespace gproshan::mdict { -class dictionary +class msparse_coding { public: struct params @@ -64,7 +64,7 @@ class dictionary const index_t & draw_patches(const index_t & p); protected: - dictionary( che *const & _mesh, ///< pointer to input mesh. + msparse_coding( che *const & _mesh, ///< pointer to input mesh. basis *const &_phi_basis, ///< pointer to continuous basis. const size_t & _m, ///< number of dictionary atoms. const size_t & _M, ///< number of patches. @@ -73,7 +73,7 @@ class dictionary const bool & _plot ///< flag to plot basis and atoms with gnuplot. ); - virtual ~dictionary(); + virtual ~msparse_coding(); virtual real_t execute() = 0; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 2e4770b6..a17fbc99 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -28,7 +28,7 @@ using namespace std; namespace gproshan::mdict { -class dictionary; +class msparse_coding; typedef function fmask_t; typedef function fmask_local_t; @@ -152,7 +152,7 @@ class patch index_t find(const index_t * indexes, size_t nc, index_t idx_global); - friend class dictionary; + friend class msparse_coding; }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index d7b3649b..e803d55d 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -496,7 +496,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) real_t mean_edge = mesh->mean_edge(); for(auto & v: mesh.selected) { - p.init(mesh, v, dictionary::T, dictionary::T * mean_edge, toplevel); + p.init(mesh, v, msparse_coding::T, msparse_coding::T * mean_edge, toplevel); for(auto & u: p.vertices) mesh->heatmap(u) = 1; @@ -539,7 +539,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static dictionary::params params; + static msparse_coding::params params; static size_t n = 12; assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); @@ -572,7 +572,7 @@ bool app_viewer::process_mask(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static dictionary::params params; + static msparse_coding::params params; static size_t n = 12; assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); @@ -613,7 +613,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static dictionary::params params; + static msparse_coding::params params; static size_t n = 12; static real_t percentage_size = 100; static real_t radio_factor = 1; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 0564ce88..428f18a5 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -15,7 +15,7 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): dictionary(_mesh, _phi_basis, p.m, p.M, p.f, p.learn, p.plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): msparse_coding(_mesh, _phi_basis, p.m, p.M, p.f, p.learn, p.plot) { delta = p.delta; sum_thres = p.sum_thres; @@ -416,7 +416,7 @@ void inpainting::init_voronoi_patches() #pragma omp for for(index_t s = 0; s < M; s++) { - patches[s].init_disjoint(mesh, sample(s), dictionary::T, vertices[s], toplevel); + patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); } #ifndef NDEBUG diff --git a/src/mdict/dictionary.cpp b/src/mdict/msparse_coding.cpp similarity index 89% rename from src/mdict/dictionary.cpp rename to src/mdict/msparse_coding.cpp index fbb43245..6720a050 100644 --- a/src/mdict/dictionary.cpp +++ b/src/mdict/msparse_coding.cpp @@ -1,4 +1,4 @@ -#include "mdict/dictionary.h" +#include "mdict/msparse_coding.h" #include "geodesics/sampling.h" #include "mdict/mdict.h" @@ -40,22 +40,22 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; -size_t dictionary::L = 12; -size_t dictionary::K = 10; -size_t dictionary::T = 5; +size_t msparse_coding::L = 12; +size_t msparse_coding::K = 10; +size_t msparse_coding::T = 5; -dictionary::dictionary(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _d_plot): +msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _d_plot): mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), learn(_learn), d_plot(_d_plot) { A.eye(phi_basis->dim(), m); dist = new real_t[mesh->n_vertices()]; } -dictionary::~dictionary() +msparse_coding::~msparse_coding() { } -void dictionary::learning() +void msparse_coding::learning() { gproshan_log(MDICT); @@ -100,7 +100,7 @@ void dictionary::learning() } } -void dictionary::sparse_coding() +void msparse_coding::sparse_coding() { gproshan_log(MDICT); @@ -108,7 +108,7 @@ void dictionary::sparse_coding() alpha = OMP_all(patches, phi_basis, A, L); } -void dictionary::init_sampling() +void msparse_coding::init_sampling() { gproshan_log(MDICT); @@ -134,7 +134,7 @@ void dictionary::init_sampling() gproshan_debug_var(phi_basis->radio()); } -void dictionary::load_features(vector & v_feat, size_t & featsize) +void msparse_coding::load_features(vector & v_feat, size_t & featsize) { string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; @@ -177,7 +177,7 @@ void dictionary::load_features(vector & v_feat, size_t & featsize) inp.close(); } -void dictionary::init_patches(const bool & reset, const fmask_t & mask) +void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) { gproshan_log(MDICT); @@ -194,7 +194,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) for(index_t s = 0; s < M; s++) { index_t v = sample(s); - patches[s].init(mesh, v, dictionary::T, phi_basis->radio(), toplevel); + patches[s].init(mesh, v, msparse_coding::T, phi_basis->radio(), toplevel); } @@ -266,7 +266,7 @@ void dictionary::init_patches(const bool & reset, const fmask_t & mask) */ } -real_t dictionary::mesh_reconstruction(const fmask_t & mask) +real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) { gproshan_log(MDICT); @@ -354,7 +354,7 @@ real_t dictionary::mesh_reconstruction(const fmask_t & mask) return max_error; } -void dictionary::update_alphas(a_mat & alpha, size_t threshold) +void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) { size_t np_new = M - threshold; bool patches_covered[np_new]; @@ -397,26 +397,26 @@ void dictionary::update_alphas(a_mat & alpha, size_t threshold) // repeat until threshold reachs all patches } -index_t dictionary::sample(const index_t & s) +index_t msparse_coding::sample(const index_t & s) { assert(s < M); if(sampling.size()) return sampling[s]; return s; } -const real_t & dictionary::operator[](const index_t & i) const +const real_t & msparse_coding::operator[](const index_t & i) const { assert(i < mesh->n_vertices()); return dist[i]; } -const index_t & dictionary::draw_patches(const index_t & p) +const index_t & msparse_coding::draw_patches(const index_t & p) { phi_basis->plot_patch(A * alpha.col(p), patches[p].xyz, patches[p].vertices[0]); return patches[p].vertices[0]; } -void dictionary::save_alpha(string file) +void msparse_coding::save_alpha(string file) { alpha.save(file); } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 768f2e05..b5e0215c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -1,6 +1,6 @@ #include "mdict/patch.h" -#include "mdict/dictionary.h" +#include "mdict/msparse_coding.h" #include "geodesics/geodesics.h" #include @@ -30,7 +30,7 @@ typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; -size_t patch::expected_nv = 3 * dictionary::T * (dictionary::T + 1); +size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); real_t patch::nyquist_factor = 0.5; void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) From 0d802e6a5c4b13e959c37702b2adc3590fa85b28 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Jan 2021 23:45:39 +0100 Subject: [PATCH 0406/1018] mdict: update params, n_atoms = m, n_patches = M --- include/mdict/inpainting.h | 8 +-- include/mdict/msparse_coding.h | 39 +++++------ src/app_viewer.cpp | 8 +-- src/mdict/inpainting.cpp | 116 +++++++++++++++------------------ src/mdict/msparse_coding.cpp | 65 +++++++++--------- 5 files changed, 106 insertions(+), 130 deletions(-) diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h index 75a05dcd..daf66bd7 100644 --- a/include/mdict/inpainting.h +++ b/include/mdict/inpainting.h @@ -32,14 +32,7 @@ class inpainting : public msparse_coding real_t execute_tmp(); private: - size_t avg_p; - size_t percent; - double delta; - double sum_thres; bool * mask; - double area_thres; - size_t max_points; - std::string key_name; }; @@ -47,3 +40,4 @@ class inpainting : public msparse_coding } // namespace gproshan::mdict #endif // INPAINTING_H + diff --git a/include/mdict/msparse_coding.h b/include/mdict/msparse_coding.h index aa71d789..9e6bbb07 100644 --- a/include/mdict/msparse_coding.h +++ b/include/mdict/msparse_coding.h @@ -19,16 +19,16 @@ class msparse_coding public: struct params { - size_t m = 144; ///< number of dictionary atoms - size_t M = 0; ///< number of patches - size_t avg_p = 36; ///< avg number of vertices per patch - size_t percentage = 0; ///< mask percentage - real_t f = 1; ///< - real_t delta = M_PI / 6; ///< - real_t sum_thres = 1.01; ///< - real_t area_thres = 0.005; ///< - bool learn = false; ///< - bool plot = false; + size_t n_atoms = 144; ///< number of dictionary atoms + size_t n_patches = 0; ///< number of patches + size_t avg_p = 36; ///< avg number of vertices per patch + size_t percent = 0; ///< mask percentage + real_t f = 1; ///< + real_t delta = M_PI / 6; ///< + real_t sum_thres = 1.01; ///< + real_t area_thres = 0.005; ///< + bool learn = false; ///< + bool plot = false; ///< }; protected: @@ -36,13 +36,11 @@ class msparse_coding size_t n_vertices; ///< number of vertices. basis * phi_basis; ///< continuous basis. + params m_params; ///< - size_t m; ///< number of dictionary atoms. - size_t M; ///< number of patches. a_mat A; ///< dictionary continuous matrix. a_mat alpha; ///< sparse coding matrix. - real_t f; real_t s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. @@ -50,8 +48,6 @@ class msparse_coding std::vector > patches_error; double d_time; ///< time of operations. - bool learn; - bool d_plot; ///< plot atoms and basis with gnuplot. real_t * dist; public: @@ -65,13 +61,9 @@ class msparse_coding protected: msparse_coding( che *const & _mesh, ///< pointer to input mesh. - basis *const &_phi_basis, ///< pointer to continuous basis. - const size_t & _m, ///< number of dictionary atoms. - const size_t & _M, ///< number of patches. - const real_t & _f, ///< deprecated - const bool & _learn, - const bool & _plot ///< flag to plot basis and atoms with gnuplot. - ); + basis *const &_phi_basis, ///< pointer to continuous basis. + const params & p ///< + ); virtual ~msparse_coding(); @@ -89,8 +81,7 @@ class msparse_coding void update_alphas(a_mat & alpha, size_t threshold); void save_alpha(string file); - index_t sample(const index_t & s); - + index_t sample(const index_t & s); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index e803d55d..969f7ef2 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -74,7 +74,7 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); add_process(GLFW_KEY_P, {"P", "Toplesets", compute_toplesets}); - sub_menus.push_back("Dictionary Learning"); + sub_menus.push_back("Sparse Coding"); add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); @@ -546,7 +546,7 @@ bool app_viewer::process_inpaiting(viewer * p_view) ImGui::InputDouble("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); - ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); @@ -578,7 +578,7 @@ bool app_viewer::process_mask(viewer * p_view) assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); - ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); @@ -621,7 +621,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); - ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.m); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp index 428f18a5..5f5d31ab 100644 --- a/src/mdict/inpainting.cpp +++ b/src/mdict/inpainting.cpp @@ -15,17 +15,11 @@ namespace gproshan::mdict { -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): msparse_coding(_mesh, _phi_basis, p.m, p.M, p.f, p.learn, p.plot) +inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): msparse_coding(_mesh, _phi_basis, p) { - delta = p.delta; - sum_thres = p.sum_thres; - avg_p = p.avg_p; - percent = p.percentage; - area_thres = p.area_thres; - - key_name = mesh->name_size() + '_' + to_string(delta) + '_' + to_string(sum_thres) + '_' + to_string(area_thres); - - M = mesh->n_vertices() / avg_p; + m_params.n_patches = mesh->n_vertices() / m_params.avg_p; + + key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); mask = new bool[mesh->n_vertices()]; memset(mask, 0, sizeof(bool) * mesh->n_vertices()); @@ -44,7 +38,7 @@ inpainting::operator const std::string & () const void inpainting::load_mask() { //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(percent) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -62,7 +56,7 @@ void inpainting::load_mask() V.zeros(mesh->n_vertices()); std::default_random_engine generator; std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); - size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (percent/ 100.0)) ; + size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (m_params.percent / 100.0)) ; size_t k = 0; size_t rn = 0; @@ -82,7 +76,7 @@ void inpainting::load_mask() void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) { - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.avg_p) + '_' + to_string(m_params.percent) + ".msk"); arma::uvec V; if(V.load(f_mask)) @@ -98,28 +92,26 @@ void inpainting::load_mask(const std::vector * vertices, const index_t { V.zeros(mesh->n_vertices()); - size_t * percentages_size = new size_t[M]; - bool cover_cluster[M]; + size_t * percentages_size = new size_t[m_params.n_patches]; + bool cover_cluster[m_params.n_patches]; // create initial desired percentage sizes #pragma omp for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { - - percentages_size[s] = ceil(vertices[s].size() * (percent/ 100.0)) ; + percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; cover_cluster[s] = 0; - } + //Generate random mask according to a percentage of patches capacity std::default_random_engine generator; std::uniform_int_distribution distribution(0,n_vertices-1); size_t k = 0; - size_t rn=0; + size_t rn = 0; - - while( k < M ) + while(k < m_params.n_patches) { //gproshan_debug_var(clusters[rn]); @@ -169,11 +161,11 @@ void inpainting::load_sampling(bool save_all) for(index_t i = 0; i < n_seeds; i++) { patch p; - p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), delta, sum_thres, area_thres, area_mesh); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); patches.push_back(move(p)); } - M = n_seeds; + m_params.n_patches = n_seeds; return; } @@ -207,7 +199,7 @@ void inpainting::load_sampling(bool save_all) if(invalid_seed[vsf]) continue; patch p; - p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, delta, sum_thres, area_thres, area_mesh); + p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); count_cov_patch = 0; if(p.vertices.size() >= 7 ) @@ -242,7 +234,7 @@ void inpainting::load_sampling(bool save_all) delete [] invalid_seed; - M = seeds.size(); + m_params.n_patches = seeds.size(); gproshan_log(saving sampling); @@ -254,11 +246,11 @@ void inpainting::load_sampling(bool save_all) S.save(tmp_file_path(key_name + ".rsampl")); - gproshan_debug_var(sum_thres); + gproshan_debug_var(m_params.sum_thres); gproshan_debug_var(count); gproshan_debug_var(count_cov); gproshan_debug_var(seeds.size()); - gproshan_debug_var(M); + gproshan_debug_var(m_params.n_patches); #ifndef NDEBUG @@ -288,16 +280,16 @@ void inpainting::init_radial_feature_patches() size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_max_size = max(patches[s].vertices.size(), patch_max_size); - patch_avg_size /= M; + patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); gproshan_debug_var(patch_min_size); gproshan_debug_var(patch_max_size); @@ -305,19 +297,19 @@ void inpainting::init_radial_feature_patches() //Initializing patches gproshan_log(initializing patches); - gproshan_debug_var(M); + gproshan_debug_var(m_params.n_patches); n_vertices = mesh->n_vertices(); - patches.resize(M); + patches.resize(m_params.n_patches); patches_map.resize(n_vertices); bool * pmask = mask; - for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); + for(index_t s = 0; s < m_params.n_patches; s++) + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patch & p = patches[s]; @@ -333,8 +325,8 @@ void inpainting::init_radial_feature_patches() if(save_all) { arma::mat AS; - AS.resize(M,13); - for(index_t i = 0; i < M; i++) + AS.resize(m_params.n_patches, 13); + for(index_t i = 0; i < m_params.n_patches; i++) { patch & p = patches[i]; @@ -362,11 +354,11 @@ void inpainting::init_radial_feature_patches() void inpainting::init_voronoi_patches() { ///// - std::vector vertices[M]; + std::vector vertices[m_params.n_patches]; //index_t * clusters_tmp = init_voronoi_sampling(vertices); ////// - gproshan_log_var(M); + gproshan_log_var(m_params.n_patches); //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) @@ -388,7 +380,7 @@ void inpainting::init_voronoi_patches() //saving first vertex aka seed vertices #pragma omp for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { vertices[s].push_back(sample(s)); } @@ -406,7 +398,7 @@ void inpainting::init_voronoi_patches() //Initializing patches gproshan_log(initializing patches); - patches.resize(M); + patches.resize(m_params.n_patches); patches_map.resize(n_vertices); //initializing patch #pragma omp parallel @@ -414,7 +406,7 @@ void inpainting::init_voronoi_patches() index_t * toplevel = new index_t[mesh->n_vertices()]; #pragma omp for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); @@ -425,16 +417,16 @@ void inpainting::init_voronoi_patches() size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_max_size = max(patches[s].vertices.size(), patch_max_size); - patch_avg_size /= M; + patch_avg_size /= m_params.n_patches; //gproshan_debug_var(patch_avg_size); //gproshan_debug_var(patch_min_size); //gproshan_debug_var(patch_max_size); @@ -442,11 +434,11 @@ void inpainting::init_voronoi_patches() } bool * pmask = mask; - for(index_t s = 0; s < M; s++) - patches[s].reset_xyz_disjoint(mesh, dist, M, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + for(index_t s = 0; s < m_params.n_patches; s++) + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patch & p = patches[s]; @@ -487,11 +479,11 @@ real_t inpainting::execute() for(index_t i = 0; i < n_vertices; i++) patches_map[i].clear(); - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patches[s].reset_xyz(mesh, patches_map, s, 0); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patch & p = patches[s]; @@ -510,7 +502,7 @@ real_t inpainting::execute() arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); - real_t ratio = (M * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); + real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); gproshan_log_var(ratio); return max_error; @@ -524,23 +516,23 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) S.load(tmp_file_path(key_name + ".smp")); alpha.load(tmp_file_path(key_name + ".alpha")); - M = S.n_rows; - patches.resize(M); + m_params.n_patches = S.n_rows; + patches.resize(m_params.n_patches); - gproshan_debug_var(M); + gproshan_debug_var(m_params.n_patches); real_t max_radio = -1; #pragma omp parallel for reduction(max: max_radio) - for(index_t i = 0; i < M; i++) + for(index_t i = 0; i < m_params.n_patches; i++) max_radio = max(max_radio, S(i, 3)); - A.eye(phi_basis->dim(), m); + A.eye(phi_basis->dim(), m_params.n_atoms); size_t total_points = 0; #pragma omp parallel for - for(index_t i = 0; i < M; i++) + for(index_t i = 0; i < m_params.n_patches; i++) { arma::mat T(3,3); T(0,0) = S(i,4); @@ -581,7 +573,7 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) vector point_cloud; point_cloud.reserve(total_points); - for(index_t i = 0; i < M; i++) + for(index_t i = 0; i < m_params.n_patches; i++) for(index_t j = 0; j < patches[i].xyz.n_cols; j++) point_cloud.push_back({ patches[i].xyz(0, j), patches[i].xyz(1, j), @@ -592,7 +584,7 @@ che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) new_mesh->update_normals(); a_vec n; - for(index_t v = 0, i = 0; i < M; i++) + for(index_t v = 0, i = 0; i < m_params.n_patches; i++) { patch & p = patches[i]; diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 6720a050..2931ec47 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -44,10 +44,9 @@ size_t msparse_coding::L = 12; size_t msparse_coding::K = 10; size_t msparse_coding::T = 5; -msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const size_t & _m, const size_t & _M, const real_t & _f, const bool & _learn, const bool & _d_plot): - mesh(_mesh), phi_basis(_phi_basis), m(_m), M(_M), f(_f), learn(_learn), d_plot(_d_plot) +msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) { - A.eye(phi_basis->dim(), m); + A.eye(phi_basis->dim(), m_params.n_atoms); dist = new real_t[mesh->n_vertices()]; } @@ -59,9 +58,9 @@ void msparse_coding::learning() { gproshan_log(MDICT); - string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim()) + '_' + to_string(m) + '_' + to_string(f) + '_' + to_string(L) + ".dict"); + string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim()) + '_' + to_string(m_params.n_atoms) + '_' + to_string(m_params.f) + '_' + to_string(L) + ".dict"); - if(learn) + if(m_params.learn) { gproshan_log_var(f_dict); @@ -76,11 +75,11 @@ void msparse_coding::learning() a_vec s; svd(U, s, V, alpha); gproshan_debug(svd done!); - A = U.cols(0,m); + A = U.cols(0, m_params.n_atoms); gproshan_debug(svd done!); A = normalise(A); gproshan_debug_var(phi_basis->radio()); - gproshan_debug_var(m); + gproshan_debug_var(m_params.n_atoms); // phi_basis->plot_atoms(A); @@ -89,11 +88,11 @@ void msparse_coding::learning() A.save(f_dict); } } - else A.eye(phi_basis->dim(), m); + else A.eye(phi_basis->dim(), m_params.n_atoms); gproshan_debug_var(phi_basis->radio()); assert(A.n_rows == phi_basis->dim()); - assert(A.n_cols == m); - if(d_plot) + assert(A.n_cols == m_params.n_atoms); + if(m_params.plot) { phi_basis->plot_basis(); phi_basis->plot_atoms(A); @@ -115,20 +114,20 @@ void msparse_coding::init_sampling() n_vertices = mesh->n_vertices(); // load sampling - if(M == 0) + if(m_params.n_patches == 0) { - M = mesh->n_vertices(); + m_params.n_patches = mesh->n_vertices(); phi_basis->radio() = mesh->mean_edge(); } else { - sampling.reserve(M); - if(!load_sampling(sampling, phi_basis->radio(), mesh, M)) + sampling.reserve(m_params.n_patches); + if(!load_sampling(sampling, phi_basis->radio(), mesh, m_params.n_patches)) cerr << "Failed to load sampling" << endl; } s_radio = phi_basis->radio(); - phi_basis->radio() *= f; + phi_basis->radio() *= m_params.f; gproshan_debug_var(s_radio); gproshan_debug_var(phi_basis->radio()); @@ -183,7 +182,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) if(reset) { - patches.resize(M); + patches.resize(m_params.n_patches); patches_map.resize(n_vertices); #pragma omp parallel @@ -191,7 +190,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) index_t * toplevel = new index_t[n_vertices]; #pragma omp for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { index_t v = sample(s); patches[s].init(mesh, v, msparse_coding::T, phi_basis->radio(), toplevel); @@ -207,27 +206,27 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patch_max_size = max(patches[s].vertices.size(), patch_max_size); - patch_avg_size /= M; + patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); gproshan_debug_var(patch_min_size); gproshan_debug_var(patch_max_size); #endif } - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patches[s].reset_xyz(mesh, patches_map, s, mask); #pragma omp parallel for - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patch & p = patches[s]; @@ -240,7 +239,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) /* #ifndef NDEBUG CImgList imlist; - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) patches[s].save(phi_basis->radio(), 16, imlist); imlist.save_ffmpeg_external("tmp/patches.mpg", 5); #endif @@ -250,14 +249,14 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) /*Saving Patches*/ /* ofstream os(tmp_file_path("patch-mat")); - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { patch & p = patches[s]; p.save_z(os); } os.close(); // DRAW NORMALS DEBUG - for(index_t s = 0; s < M; s++) + for(index_t s = 0; s < m_params.n_patches; s++) { viewer::vectors.push_back({patches[s].x(0), patches[s].x(1), patches[s].x(2)}); a_vec r = patches[s].x + 0.02 * patches[s].normal(); @@ -274,10 +273,10 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) a_mat V(3, mesh->n_vertices(), arma::fill::zeros); - patches_error.resize(M); + patches_error.resize(m_params.n_patches); #pragma omp parallel for - for(index_t p = 0; p < M; p++) + for(index_t p = 0; p < m_params.n_patches; p++) { patch & rp = patches[p]; @@ -294,13 +293,13 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) for(index_t i = 0; i < 10; i++) { const index_t & best = patches_error[i].second; - const index_t & worst = patches_error[M - i - 1].second; + const index_t & worst = patches_error[m_params.n_patches - i - 1].second; fprintf(stderr, "%5d:%8u>%8u%8u>%8u\n", i, best, draw_patches(best), worst, draw_patches(worst)); } #pragma omp parallel for - for(index_t p = 0; p < M; p++) + for(index_t p = 0; p < m_params.n_patches; p++) { patch & rp = patches[p]; rp.iscale_xyz(phi_basis->radio()); @@ -356,7 +355,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) { - size_t np_new = M - threshold; + size_t np_new = m_params.n_patches - threshold; bool patches_covered[np_new]; memset(patches_covered, 0, sizeof(patches_covered)); size_t count = 0; @@ -365,7 +364,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) while(count < threshold) { #pragma omp parallel for - for(index_t s = threshold; s < M; s++) + for(index_t s = threshold; s < m_params.n_patches; s++) { if(!patches_covered[s-threshold]) @@ -399,7 +398,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) index_t msparse_coding::sample(const index_t & s) { - assert(s < M); + assert(s < m_params.n_patches); if(sampling.size()) return sampling[s]; return s; } From cbb93280516df895373e37ac61dc9d30457c697e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 12:55:43 +0100 Subject: [PATCH 0407/1018] mdict: merge inpainting into msparse_coding --- include/app_viewer.h | 4 +- include/mdict/inpainting.h | 43 --- include/mdict/msparse_coding.h | 32 +- src/app_viewer.cpp | 22 +- src/mdict/inpainting.cpp | 656 --------------------------------- src/mdict/msparse_coding.cpp | 635 ++++++++++++++++++++++++++++++- 6 files changed, 668 insertions(+), 724 deletions(-) delete mode 100644 include/mdict/inpainting.h delete mode 100644 src/mdict/inpainting.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index bd0200f3..b4104a61 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -22,7 +22,7 @@ #include "geodesics/geodesics.h" #include "geodesics/sampling.h" -#include "mdict/inpainting.h" +#include "mdict/msparse_coding.h" #include "mdict/basis_dct.h" #include "mdict/patch.h" @@ -64,8 +64,8 @@ class app_viewer : public viewer static bool compute_toplesets(viewer * p_view); static bool process_voronoi(viewer * p_view); + static bool process_msparse_coding(viewer * p_view); static bool process_mdict_patch(viewer * p_view); - static bool process_inpaiting(viewer * p_view); static bool process_mask(viewer * p_view); static bool process_pc_reconstruction(viewer * p_view); diff --git a/include/mdict/inpainting.h b/include/mdict/inpainting.h deleted file mode 100644 index daf66bd7..00000000 --- a/include/mdict/inpainting.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef INPAINTING_H -#define INPAINTING_H - -#include "mdict/msparse_coding.h" -#include "mesh/che_poisson.h" -#include "mesh/che_fill_hole.h" - -#include - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -class inpainting : public msparse_coding -{ - public: - inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p); - virtual ~inpainting(); - - operator const std::string & () const; - - real_t execute(); - void load_mask(const std::vector * vertices, const index_t * clusters); - void load_mask(); - void init_voronoi_patches(); - void init_radial_feature_patches(); - void load_sampling(bool save_all); - che * point_cloud_reconstruction(real_t per, real_t fr); - vector sort_indexes(const vector &v); - - real_t execute_tmp(); - - private: - bool * mask; - std::string key_name; -}; - - -} // namespace gproshan::mdict - -#endif // INPAINTING_H - diff --git a/include/mdict/msparse_coding.h b/include/mdict/msparse_coding.h index 9e6bbb07..98a335cc 100644 --- a/include/mdict/msparse_coding.h +++ b/include/mdict/msparse_coding.h @@ -1,5 +1,5 @@ -#ifndef DICTIONARY_H -#define DICTIONARY_H +#ifndef MSPARSE_CODING_H +#define MSPARSE_CODING_H #include "mesh/che.h" #include "mdict/patch.h" @@ -31,7 +31,7 @@ class msparse_coding bool plot = false; ///< }; - protected: + private: che * mesh; ///< input mesh. size_t n_vertices; ///< number of vertices. @@ -49,6 +49,9 @@ class msparse_coding double d_time; ///< time of operations. real_t * dist; + + bool * mask = nullptr; + std::string key_name; public: static size_t K; ///< number of iterations KSVD. @@ -56,19 +59,30 @@ class msparse_coding static size_t T; ///< factor of patches' size, default 5 toplesets. public: - const real_t & operator[](const index_t & i) const; - const index_t & draw_patches(const index_t & p); - - protected: msparse_coding( che *const & _mesh, ///< pointer to input mesh. basis *const &_phi_basis, ///< pointer to continuous basis. const params & p ///< ); virtual ~msparse_coding(); + + const real_t & operator[](const index_t & i) const; + const index_t & draw_patches(const index_t & p); + + operator const std::string & () const; + + real_t execute(); + void load_mask(const std::vector * vertices, const index_t * clusters); + void load_mask(); + void init_voronoi_patches(); + void init_radial_feature_patches(); + void load_sampling(bool save_all); + che * point_cloud_reconstruction(real_t per, real_t fr); + vector sort_indexes(const vector &v); - virtual real_t execute() = 0; + real_t execute_tmp(); + private: void learning(); void sparse_coding(); void init_sampling(); @@ -87,5 +101,5 @@ class msparse_coding } // namespace gproshan::mdict -#endif // DICTIONARY_H +#endif // MSPARSE_CODING_H diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 969f7ef2..75f421b9 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -75,8 +75,8 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_P, {"P", "Toplesets", compute_toplesets}); sub_menus.push_back("Sparse Coding"); + add_process(GLFW_KEY_I, {"I", "Mesh Sparse Coding", process_msparse_coding}); add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); - add_process(GLFW_KEY_I, {"I", "MDICT Inpaiting", process_inpaiting}); add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); @@ -534,7 +534,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) return false; } -bool app_viewer::process_inpaiting(viewer * p_view) +bool app_viewer::process_msparse_coding(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -555,12 +555,12 @@ bool app_viewer::process_inpaiting(viewer * p_view) if(ImGui::Button("Run")) { basis_dct phi(n); - inpainting dict(mesh, &phi, params); + msparse_coding msc(mesh, &phi, params); - real_t max_error = dict.execute(); + real_t max_error = msc.execute(); gproshan_log_var(max_error); - mesh->update_heatmap(&dict[0]); + mesh->update_heatmap(&msc[0]); mesh->update_normals(); } @@ -587,12 +587,12 @@ bool app_viewer::process_mask(viewer * p_view) if(ImGui::Button("Run")) { basis_dct phi(n); - inpainting dict(mesh, &phi, params); + msparse_coding msc(mesh, &phi, params); - dict.init_radial_feature_patches(); + msc.init_radial_feature_patches(); //dict.init_voronoi_patches(); - mesh->update_heatmap(&dict[0]); - string f_points = tmp_file_path(string(dict) + ".rsampl"); + mesh->update_heatmap(&msc[0]); + string f_points = tmp_file_path(string(msc) + ".rsampl"); a_vec points_out; gproshan_debug_var(f_points); @@ -633,9 +633,9 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) if(ImGui::Button("Run")) { basis_dct phi(n); - inpainting dict(mesh, &phi, params); + msparse_coding msc(mesh, &phi, params); - view->add_mesh({dict.point_cloud_reconstruction(percentage_size, radio_factor)}); + view->add_mesh({msc.point_cloud_reconstruction(percentage_size, radio_factor)}); } return true; diff --git a/src/mdict/inpainting.cpp b/src/mdict/inpainting.cpp deleted file mode 100644 index 5f5d31ab..00000000 --- a/src/mdict/inpainting.cpp +++ /dev/null @@ -1,656 +0,0 @@ -#include "mdict/inpainting.h" - -#include "mesh/che_off.h" -#include "geodesics/geodesics.h" - -#include -#include -#include -#include -#include - - -// geometry processing and shape analysis framework -// mesh dictionary learning and sparse coding namespace -namespace gproshan::mdict { - - -inpainting::inpainting(che *const & _mesh, basis *const & _phi_basis, const params & p): msparse_coding(_mesh, _phi_basis, p) -{ - m_params.n_patches = mesh->n_vertices() / m_params.avg_p; - - key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); - - mask = new bool[mesh->n_vertices()]; - memset(mask, 0, sizeof(bool) * mesh->n_vertices()); -} - -inpainting::~inpainting() -{ - delete [] mask; -} - -inpainting::operator const std::string & () const -{ - return key_name; -} - -void inpainting::load_mask() -{ - //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.percent) + ".msk"); - arma::uvec V; - gproshan_log(loading radial mask); - - - if(V.load(f_mask)) - { - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - mask[i] = V(i); - } - } - else - { - V.zeros(mesh->n_vertices()); - std::default_random_engine generator; - std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); - size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (m_params.percent / 100.0)) ; - - size_t k = 0; - size_t rn = 0; - while (k < percentage) - { - rn = distribution(generator); - if(!mask[rn]) - { - mask[rn] = 1; - V(rn) = 1; - k++; - } - } - V.save(f_mask); - } -} - -void inpainting::load_mask(const std::vector * vertices, const index_t * clusters) -{ - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.avg_p) + '_' + to_string(m_params.percent) + ".msk"); - arma::uvec V; - - if(V.load(f_mask)) - { - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - mask[i] = V(i); - } - - } - else - { - - V.zeros(mesh->n_vertices()); - size_t * percentages_size = new size_t[m_params.n_patches]; - bool cover_cluster[m_params.n_patches]; - - - // create initial desired percentage sizes - #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) - { - percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; - cover_cluster[s] = 0; - } - - //Generate random mask according to a percentage of patches capacity - std::default_random_engine generator; - std::uniform_int_distribution distribution(0,n_vertices-1); - - size_t k = 0; - size_t rn = 0; - - while(k < m_params.n_patches) - { - //gproshan_debug_var(clusters[rn]); - - rn = distribution(generator); - if(!mask[rn] && percentages_size[clusters[rn]] > 0) - { - - mask[rn] = 1; - V(rn) = 1; - percentages_size[clusters[rn]]--; - - } - if( !cover_cluster[clusters[rn]] && percentages_size[clusters[rn]] == 0) - { - cover_cluster[clusters[rn]] = 1; // It is finished - k++; - } - - } - - - V.save(f_mask); - - } - -} - -void inpainting::load_sampling(bool save_all) -{ - size_t featsize; - vector all_sorted_features; - vector seeds; - load_features(all_sorted_features, featsize); - - gproshan_debug_var(all_sorted_features.size()); - - size_t count = 0; - real_t area_mesh = mesh->area_surface(); - - a_vec S; - if(S.load(tmp_file_path(key_name + ".rsampl"))) - { - gproshan_debug(loading sampling); - - size_t n_seeds = S.n_rows; - real_t euc_radio, geo_radio; - for(index_t i = 0; i < n_seeds; i++) - { - patch p; - p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); - patches.push_back(move(p)); - } - - m_params.n_patches = n_seeds; - - return; - } - - //ELSE IF !S.load(f_sample) - - gproshan_debug(computing sampling); - - // compute features will be seeds - patches_map.resize(mesh->n_vertices()); - - //Coverage of the points - bool covered[mesh->n_vertices()]; - - #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) - covered[i] = 0; - - real_t euc_radio; - real_t geo_radio; - vector radios; - vector geo_radios; - size_t count_cov = 0; - size_t count_cov_patch = 0; - - bool * invalid_seed = new bool[mesh->n_vertices()]; - memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); - - for(const index_t & vsf: all_sorted_features) - { - if(invalid_seed[vsf]) continue; - - patch p; - p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); - - count_cov_patch = 0; - if(p.vertices.size() >= 7 ) - { - for(const index_t & v: p.vertices) - if(!covered[v]) count_cov_patch++; - - count_cov += count_cov_patch; - if(count_cov_patch > 0) - { - patches.push_back(move(p)); - seeds.push_back(vsf); - radios.push_back(euc_radio); - geo_radios.push_back(geo_radio); - count += p.vertices.size(); - - for(const index_t & v: p.vertices) - { - covered[v] = 1; - - if(!invalid_seed[v]) - { - const vertex & va = mesh->get_vertex(vsf); - const vertex & vb = mesh->get_vertex(v); - - invalid_seed[v] = *(va - vb) < 0.8 * euc_radio; - } - } - } - } - } - - delete [] invalid_seed; - - m_params.n_patches = seeds.size(); - - gproshan_log(saving sampling); - - S.resize(seeds.size()); - - #pragma omp parallel for - for(index_t i = 0; i < seeds.size(); i++) - S(i) = seeds[i]; - - S.save(tmp_file_path(key_name + ".rsampl")); - - gproshan_debug_var(m_params.sum_thres); - gproshan_debug_var(count); - gproshan_debug_var(count_cov); - gproshan_debug_var(seeds.size()); - gproshan_debug_var(m_params.n_patches); - - -#ifndef NDEBUG - vector outliers; - - - for(index_t i = 0; i < mesh->n_vertices(); i++) - if(!covered[i]) - outliers.push_back(i); - - a_vec outlv(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) - outlv(i) = outliers[i]; - - outlv.save(tmp_file_path(key_name + ".outlv")); -#endif // NDEBUG -} - -void inpainting::init_radial_feature_patches() -{ - load_sampling(false); - load_mask(); - - #ifndef NDEBUG - size_t patch_avg_size = 0; - size_t patch_min_size = NIL; - size_t patch_max_size = 0; - - #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_avg_size += patches[s].vertices.size(); - #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); - #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); - - patch_avg_size /= m_params.n_patches; - gproshan_debug_var(patch_avg_size); - gproshan_debug_var(patch_min_size); - gproshan_debug_var(patch_max_size); - #endif - - //Initializing patches - gproshan_log(initializing patches); - gproshan_debug_var(m_params.n_patches); - - n_vertices = mesh->n_vertices(); - patches.resize(m_params.n_patches); - patches_map.resize(n_vertices); - - bool * pmask = mask; - - for(index_t s = 0; s < m_params.n_patches; s++) - patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); - - #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) - { - patch & p = patches[s]; - - p.transform(); - p.scale_xyz(phi_basis->radio()); - p.add_extra_xyz_disjoint(mesh,patches_map, s); - p.compute_avg_distance(mesh, patches_map, s); - p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); - phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - } - - bool save_all = true; - if(save_all) - { - arma::mat AS; - AS.resize(m_params.n_patches, 13); - for(index_t i = 0; i < m_params.n_patches; i++) - { - patch & p = patches[i]; - - AS(i,0) = p.x(0); - AS(i,1) = p.x(1); - AS(i,2) = p.x(2); - AS(i,3) = p.radio; - AS(i,4) = p.T(0,0); - AS(i,5) = p.T(1,0); - AS(i,6) = p.T(2,0); - AS(i,7) = p.T(0,1); - AS(i,8) = p.T(1,1); - AS(i,9) = p.T(2,1); - AS(i,10) = p.T(0,2); - AS(i,11) = p.T(1,2); - AS(i,12) = p.T(2,2); - } - - AS.save(tmp_file_path(key_name + ".smp")); - } - - gproshan_log(radial patches are ready); -} - -void inpainting::init_voronoi_patches() -{ - ///// - std::vector vertices[m_params.n_patches]; - //index_t * clusters_tmp = init_voronoi_sampling(vertices); - - ////// - gproshan_log_var(m_params.n_patches); - - //FPS samplif_dictng with desired number of sources - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - geodesics::params params; - params.cluster = 1; - - - // creating disjoint clusters with geodesics aka voronoi - #ifdef GPROSHAN_CUDA - params.alg = geodesics::PTP_GPU; - #endif - - geodesics ptp(mesh, sampling, params); - TOC(d_time) - gproshan_log_var(d_time); - - - //saving first vertex aka seed vertices - #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) - { - vertices[s].push_back(sample(s)); - } - - for(index_t i = 0; i < mesh->n_vertices(); i++) - { - ptp.clusters[i]--; - if(sample(ptp.clusters[i]) != i) - vertices[ptp.clusters[i]].push_back(i) ; - } - - - load_mask(vertices, ptp.clusters); - - //Initializing patches - gproshan_log(initializing patches); - - patches.resize(m_params.n_patches); - patches_map.resize(n_vertices); - //initializing patch - #pragma omp parallel - { - index_t * toplevel = new index_t[mesh->n_vertices()]; - - #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) - { - patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); - - } - #ifndef NDEBUG - size_t patch_avg_size = 0; - size_t patch_min_size = NIL; - size_t patch_max_size = 0; - - #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_avg_size += patches[s].vertices.size(); - #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); - #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < m_params.n_patches; s++) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); - - patch_avg_size /= m_params.n_patches; - //gproshan_debug_var(patch_avg_size); - //gproshan_debug_var(patch_min_size); - //gproshan_debug_var(patch_max_size); - #endif - } - - bool * pmask = mask; - for(index_t s = 0; s < m_params.n_patches; s++) - patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); - - #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) - { - patch & p = patches[s]; - - p.transform(); - p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); - phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - p.compute_avg_distance(mesh, patches_map, s); - - } - - gproshan_log(our patches are ready); -} - -real_t inpainting::execute() -{ - - TIC(d_time) init_radial_feature_patches(); TOC(d_time) - gproshan_debug_var(d_time); - //L = 10; - - // sparse coding and reconstruction with all patches - //TIC(d_time) sparse_coding(); TOC(d_time) - //gproshan_debug_var(d_time); - - TIC(d_time) learning(); TOC(d_time) - gproshan_debug_var(d_time); - - // sparse coding and reconstruction with all patches - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); - - save_alpha(tmp_file_path(key_name + ".alpha")); - - draw_patches(295); - draw_patches(312); - - #pragma omp parallel for - for(index_t i = 0; i < n_vertices; i++) - patches_map[i].clear(); - - for(index_t s = 0; s < m_params.n_patches; s++) - patches[s].reset_xyz(mesh, patches_map, s, 0); - - #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) - { - patch & p = patches[s]; - - p.transform(); - p.scale_xyz(phi_basis->radio()); - p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); - phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - } - - bool * pmask = mask; - - TIC(d_time) - real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); - TOC(d_time) - gproshan_debug_var(d_time); - - arma::uvec non_zero = find( abs(alpha) > 0.00001); - gproshan_debug_var(non_zero.size()); - real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); - gproshan_log_var(ratio); - - return max_error; -} - -che * inpainting::point_cloud_reconstruction(real_t per, real_t fr) -{ - arma::mat S; - arma::mat alpha; - - S.load(tmp_file_path(key_name + ".smp")); - alpha.load(tmp_file_path(key_name + ".alpha")); - - m_params.n_patches = S.n_rows; - patches.resize(m_params.n_patches); - - gproshan_debug_var(m_params.n_patches); - - real_t max_radio = -1; - - #pragma omp parallel for reduction(max: max_radio) - for(index_t i = 0; i < m_params.n_patches; i++) - max_radio = max(max_radio, S(i, 3)); - - A.eye(phi_basis->dim(), m_params.n_atoms); - - size_t total_points = 0; - - #pragma omp parallel for - for(index_t i = 0; i < m_params.n_patches; i++) - { - arma::mat T(3,3); - T(0,0) = S(i,4); - T(1,0) = S(i,5); - T(2,0) = S(i,6); - T(0,1) = S(i,7); - T(1,1) = S(i,8); - T(2,1) = S(i,9); - T(0,2) = S(i,10); - T(1,2) = S(i,11); - T(2,2) = S(i,12); - - patch & p = patches[i]; - - p.init_random({S(i, 0), S(i, 1), S(i, 2)}, T, S(i, 3), max_radio, per, fr); - p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); - phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - - a_vec x = p.phi * A * alpha.col(i); - p.xyz.row(2) = x.t(); - - p.iscale_xyz(phi_basis->radio()); - p.itransform(); - - #pragma omp atomic - total_points += p.xyz.n_cols; - - for(index_t j = 0; j < patches[i].xyz.n_cols; j++) - if(patches[i].xyz(2, j) > 2) - { - gproshan_debug_var(i); - break; - } - } - - gproshan_debug_var(total_points); - - vector point_cloud; - point_cloud.reserve(total_points); - - for(index_t i = 0; i < m_params.n_patches; i++) - for(index_t j = 0; j < patches[i].xyz.n_cols; j++) - point_cloud.push_back({ patches[i].xyz(0, j), - patches[i].xyz(1, j), - patches[i].xyz(2, j) - }); - - che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); - new_mesh->update_normals(); - - a_vec n; - for(index_t v = 0, i = 0; i < m_params.n_patches; i++) - { - patch & p = patches[i]; - - phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 0); - a_vec dx = p.phi * A * alpha.col(i); - - phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); - a_vec dy = p.phi * A * alpha.col(i); - - for(index_t j = 0; j < patches[i].xyz.n_cols; j++, v++) - { - n = {-dx(j), -dy(j), 1}; - n = normalise(p.T * n); - new_mesh->normal(v) = {n(0), n(1), n(2)}; - } - } - - che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc"), che_off::NOFF); - - gproshan_debug(saved new point cloud); - - return new_mesh; -} - - -real_t inpainting::execute_tmp() -{ - // fill holes - size_t threshold = mesh->n_vertices(); - delete [] fill_all_holes(mesh); - TIC(d_time) poisson(mesh, threshold, 2); TOC(d_time) - gproshan_debug_var(d_time); - - // remove possible non manifold vertices - mesh->remove_non_manifold_vertices(); - - // sampling including new vertices - TIC(d_time) init_sampling(); TOC(d_time) - gproshan_debug_var(d_time); - - // initializing patches with threshold - TIC(d_time) init_patches(1, [&threshold](const index_t & i) -> bool { return i < threshold; }); TOC(d_time) - gproshan_debug_var(d_time); - - // learning only from valid patches - TIC(d_time) learning(); TOC(d_time) - gproshan_debug_var(d_time); - - // including vertices out of threshold - TIC(d_time) init_patches(0); TOC(d_time) - gproshan_debug_var(d_time); - - // Update new alphas, propagating the info towards the center - TIC(d_time) update_alphas(alpha, threshold); TOC(d_time) - gproshan_debug_var(d_time); - - // sparse coding and reconstruction with all patches - TIC(d_time) sparse_coding(); TOC(d_time) - gproshan_debug_var(d_time); - - TIC(d_time) mesh_reconstruction(); TOC(d_time) - gproshan_debug_var(d_time); - - return 0; -} - - -} // namespace gproshan::mdict - diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 2931ec47..099d96b4 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -1,10 +1,10 @@ #include "mdict/msparse_coding.h" -#include "geodesics/sampling.h" #include "mdict/mdict.h" #include "mesh/che_off.h" #include "mesh/che_poisson.h" #include "mesh/che_fill_hole.h" +#include "geodesics/sampling.h" #include "viewer/viewer.h" @@ -31,6 +31,7 @@ using namespace cimg_library; // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { + typedef real_t DFT; typedef CGAL::Simple_cartesian Data_Kernel; typedef Data_Kernel::Point_3 DPoint; @@ -39,7 +40,6 @@ typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; - size_t msparse_coding::L = 12; size_t msparse_coding::K = 10; size_t msparse_coding::T = 5; @@ -48,10 +48,639 @@ msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, co { A.eye(phi_basis->dim(), m_params.n_atoms); dist = new real_t[mesh->n_vertices()]; + + m_params.n_patches = mesh->n_vertices() / m_params.avg_p; + + key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); + + mask = new bool[mesh->n_vertices()]; + memset(mask, 0, sizeof(bool) * mesh->n_vertices()); } msparse_coding::~msparse_coding() { + delete [] mask; +} + +msparse_coding::operator const std::string & () const +{ + return key_name; +} + +void msparse_coding::load_mask() +{ + //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.percent) + ".msk"); + arma::uvec V; + gproshan_log(loading radial mask); + + + if(V.load(f_mask)) + { + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + mask[i] = V(i); + } + } + else + { + V.zeros(mesh->n_vertices()); + std::default_random_engine generator; + std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); + size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (m_params.percent / 100.0)) ; + + size_t k = 0; + size_t rn = 0; + while (k < percentage) + { + rn = distribution(generator); + if(!mask[rn]) + { + mask[rn] = 1; + V(rn) = 1; + k++; + } + } + V.save(f_mask); + } +} + +void msparse_coding::load_mask(const std::vector * vertices, const index_t * clusters) +{ + string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.avg_p) + '_' + to_string(m_params.percent) + ".msk"); + arma::uvec V; + + if(V.load(f_mask)) + { + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + mask[i] = V(i); + } + + } + else + { + + V.zeros(mesh->n_vertices()); + size_t * percentages_size = new size_t[m_params.n_patches]; + bool cover_cluster[m_params.n_patches]; + + + // create initial desired percentage sizes + #pragma omp for + for(index_t s = 0; s < m_params.n_patches; s++) + { + percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; + cover_cluster[s] = 0; + } + + //Generate random mask according to a percentage of patches capacity + std::default_random_engine generator; + std::uniform_int_distribution distribution(0,n_vertices-1); + + size_t k = 0; + size_t rn = 0; + + while(k < m_params.n_patches) + { + //gproshan_debug_var(clusters[rn]); + + rn = distribution(generator); + if(!mask[rn] && percentages_size[clusters[rn]] > 0) + { + + mask[rn] = 1; + V(rn) = 1; + percentages_size[clusters[rn]]--; + + } + if( !cover_cluster[clusters[rn]] && percentages_size[clusters[rn]] == 0) + { + cover_cluster[clusters[rn]] = 1; // It is finished + k++; + } + + } + + + V.save(f_mask); + + } + +} + +void msparse_coding::load_sampling(bool save_all) +{ + size_t featsize; + vector all_sorted_features; + vector seeds; + load_features(all_sorted_features, featsize); + + gproshan_debug_var(all_sorted_features.size()); + + size_t count = 0; + real_t area_mesh = mesh->area_surface(); + + a_vec S; + if(S.load(tmp_file_path(key_name + ".rsampl"))) + { + gproshan_debug(loading sampling); + + size_t n_seeds = S.n_rows; + real_t euc_radio, geo_radio; + for(index_t i = 0; i < n_seeds; i++) + { + patch p; + p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); + patches.push_back(move(p)); + } + + m_params.n_patches = n_seeds; + + return; + } + + //ELSE IF !S.load(f_sample) + + gproshan_debug(computing sampling); + + // compute features will be seeds + patches_map.resize(mesh->n_vertices()); + + //Coverage of the points + bool covered[mesh->n_vertices()]; + + #pragma omp for + for(index_t i = 0; i < mesh->n_vertices(); i++) + covered[i] = 0; + + real_t euc_radio; + real_t geo_radio; + vector radios; + vector geo_radios; + size_t count_cov = 0; + size_t count_cov_patch = 0; + + bool * invalid_seed = new bool[mesh->n_vertices()]; + memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); + + for(const index_t & vsf: all_sorted_features) + { + if(invalid_seed[vsf]) continue; + + patch p; + p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); + + count_cov_patch = 0; + if(p.vertices.size() >= 7 ) + { + for(const index_t & v: p.vertices) + if(!covered[v]) count_cov_patch++; + + count_cov += count_cov_patch; + if(count_cov_patch > 0) + { + patches.push_back(move(p)); + seeds.push_back(vsf); + radios.push_back(euc_radio); + geo_radios.push_back(geo_radio); + count += p.vertices.size(); + + for(const index_t & v: p.vertices) + { + covered[v] = 1; + + if(!invalid_seed[v]) + { + const vertex & va = mesh->get_vertex(vsf); + const vertex & vb = mesh->get_vertex(v); + + invalid_seed[v] = *(va - vb) < 0.8 * euc_radio; + } + } + } + } + } + + delete [] invalid_seed; + + m_params.n_patches = seeds.size(); + + gproshan_log(saving sampling); + + S.resize(seeds.size()); + + #pragma omp parallel for + for(index_t i = 0; i < seeds.size(); i++) + S(i) = seeds[i]; + + S.save(tmp_file_path(key_name + ".rsampl")); + + gproshan_debug_var(m_params.sum_thres); + gproshan_debug_var(count); + gproshan_debug_var(count_cov); + gproshan_debug_var(seeds.size()); + gproshan_debug_var(m_params.n_patches); + + +#ifndef NDEBUG + vector outliers; + + + for(index_t i = 0; i < mesh->n_vertices(); i++) + if(!covered[i]) + outliers.push_back(i); + + a_vec outlv(outliers.size()); + for(index_t i = 0; i < outliers.size(); i++) + outlv(i) = outliers[i]; + + outlv.save(tmp_file_path(key_name + ".outlv")); +#endif // NDEBUG +} + +void msparse_coding::init_radial_feature_patches() +{ + load_sampling(false); + load_mask(); + + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; + + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= m_params.n_patches; + gproshan_debug_var(patch_avg_size); + gproshan_debug_var(patch_min_size); + gproshan_debug_var(patch_max_size); + #endif + + //Initializing patches + gproshan_log(initializing patches); + gproshan_debug_var(m_params.n_patches); + + n_vertices = mesh->n_vertices(); + patches.resize(m_params.n_patches); + patches_map.resize(n_vertices); + + bool * pmask = mask; + + for(index_t s = 0; s < m_params.n_patches; s++) + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); + + #pragma omp parallel for + for(index_t s = 0; s < m_params.n_patches; s++) + { + patch & p = patches[s]; + + p.transform(); + p.scale_xyz(phi_basis->radio()); + p.add_extra_xyz_disjoint(mesh,patches_map, s); + p.compute_avg_distance(mesh, patches_map, s); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + } + + bool save_all = true; + if(save_all) + { + arma::mat AS; + AS.resize(m_params.n_patches, 13); + for(index_t i = 0; i < m_params.n_patches; i++) + { + patch & p = patches[i]; + + AS(i,0) = p.x(0); + AS(i,1) = p.x(1); + AS(i,2) = p.x(2); + AS(i,3) = p.radio; + AS(i,4) = p.T(0,0); + AS(i,5) = p.T(1,0); + AS(i,6) = p.T(2,0); + AS(i,7) = p.T(0,1); + AS(i,8) = p.T(1,1); + AS(i,9) = p.T(2,1); + AS(i,10) = p.T(0,2); + AS(i,11) = p.T(1,2); + AS(i,12) = p.T(2,2); + } + + AS.save(tmp_file_path(key_name + ".smp")); + } + + gproshan_log(radial patches are ready); +} + +void msparse_coding::init_voronoi_patches() +{ + ///// + std::vector vertices[m_params.n_patches]; + //index_t * clusters_tmp = init_voronoi_sampling(vertices); + + ////// + gproshan_log_var(m_params.n_patches); + + //FPS samplif_dictng with desired number of sources + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + geodesics::params params; + params.cluster = 1; + + + // creating disjoint clusters with geodesics aka voronoi + #ifdef GPROSHAN_CUDA + params.alg = geodesics::PTP_GPU; + #endif + + geodesics ptp(mesh, sampling, params); + TOC(d_time) + gproshan_log_var(d_time); + + + //saving first vertex aka seed vertices + #pragma omp for + for(index_t s = 0; s < m_params.n_patches; s++) + { + vertices[s].push_back(sample(s)); + } + + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + ptp.clusters[i]--; + if(sample(ptp.clusters[i]) != i) + vertices[ptp.clusters[i]].push_back(i) ; + } + + + load_mask(vertices, ptp.clusters); + + //Initializing patches + gproshan_log(initializing patches); + + patches.resize(m_params.n_patches); + patches_map.resize(n_vertices); + //initializing patch + #pragma omp parallel + { + index_t * toplevel = new index_t[mesh->n_vertices()]; + + #pragma omp for + for(index_t s = 0; s < m_params.n_patches; s++) + { + patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); + + } + #ifndef NDEBUG + size_t patch_avg_size = 0; + size_t patch_min_size = NIL; + size_t patch_max_size = 0; + + #pragma omp parallel for reduction(+: patch_avg_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_avg_size += patches[s].vertices.size(); + #pragma omp parallel for reduction(min: patch_min_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_min_size = min(patches[s].vertices.size(), patch_min_size); + #pragma omp parallel for reduction(max: patch_max_size) + for(index_t s = 0; s < m_params.n_patches; s++) + patch_max_size = max(patches[s].vertices.size(), patch_max_size); + + patch_avg_size /= m_params.n_patches; + //gproshan_debug_var(patch_avg_size); + //gproshan_debug_var(patch_min_size); + //gproshan_debug_var(patch_max_size); + #endif + } + + bool * pmask = mask; + for(index_t s = 0; s < m_params.n_patches; s++) + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + + #pragma omp parallel for + for(index_t s = 0; s < m_params.n_patches; s++) + { + patch & p = patches[s]; + + p.transform(); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + p.compute_avg_distance(mesh, patches_map, s); + + } + + gproshan_log(our patches are ready); +} + +real_t msparse_coding::execute() +{ + + TIC(d_time) init_radial_feature_patches(); TOC(d_time) + gproshan_debug_var(d_time); + //L = 10; + + // sparse coding and reconstruction with all patches + //TIC(d_time) sparse_coding(); TOC(d_time) + //gproshan_debug_var(d_time); + + TIC(d_time) learning(); TOC(d_time) + gproshan_debug_var(d_time); + + // sparse coding and reconstruction with all patches + TIC(d_time) sparse_coding(); TOC(d_time) + gproshan_debug_var(d_time); + + save_alpha(tmp_file_path(key_name + ".alpha")); + + draw_patches(295); + draw_patches(312); + + #pragma omp parallel for + for(index_t i = 0; i < n_vertices; i++) + patches_map[i].clear(); + + for(index_t s = 0; s < m_params.n_patches; s++) + patches[s].reset_xyz(mesh, patches_map, s, 0); + + #pragma omp parallel for + for(index_t s = 0; s < m_params.n_patches; s++) + { + patch & p = patches[s]; + + p.transform(); + p.scale_xyz(phi_basis->radio()); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + } + + bool * pmask = mask; + + TIC(d_time) + real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); + TOC(d_time) + gproshan_debug_var(d_time); + + arma::uvec non_zero = find( abs(alpha) > 0.00001); + gproshan_debug_var(non_zero.size()); + real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); + gproshan_log_var(ratio); + + return max_error; +} + +che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) +{ + arma::mat S; + arma::mat alpha; + + S.load(tmp_file_path(key_name + ".smp")); + alpha.load(tmp_file_path(key_name + ".alpha")); + + m_params.n_patches = S.n_rows; + patches.resize(m_params.n_patches); + + gproshan_debug_var(m_params.n_patches); + + real_t max_radio = -1; + + #pragma omp parallel for reduction(max: max_radio) + for(index_t i = 0; i < m_params.n_patches; i++) + max_radio = max(max_radio, S(i, 3)); + + A.eye(phi_basis->dim(), m_params.n_atoms); + + size_t total_points = 0; + + #pragma omp parallel for + for(index_t i = 0; i < m_params.n_patches; i++) + { + arma::mat T(3,3); + T(0,0) = S(i,4); + T(1,0) = S(i,5); + T(2,0) = S(i,6); + T(0,1) = S(i,7); + T(1,1) = S(i,8); + T(2,1) = S(i,9); + T(0,2) = S(i,10); + T(1,2) = S(i,11); + T(2,2) = S(i,12); + + patch & p = patches[i]; + + p.init_random({S(i, 0), S(i, 1), S(i, 2)}, T, S(i, 3), max_radio, per, fr); + p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); + phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); + + a_vec x = p.phi * A * alpha.col(i); + p.xyz.row(2) = x.t(); + + p.iscale_xyz(phi_basis->radio()); + p.itransform(); + + #pragma omp atomic + total_points += p.xyz.n_cols; + + for(index_t j = 0; j < patches[i].xyz.n_cols; j++) + if(patches[i].xyz(2, j) > 2) + { + gproshan_debug_var(i); + break; + } + } + + gproshan_debug_var(total_points); + + vector point_cloud; + point_cloud.reserve(total_points); + + for(index_t i = 0; i < m_params.n_patches; i++) + for(index_t j = 0; j < patches[i].xyz.n_cols; j++) + point_cloud.push_back({ patches[i].xyz(0, j), + patches[i].xyz(1, j), + patches[i].xyz(2, j) + }); + + che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); + new_mesh->update_normals(); + + a_vec n; + for(index_t v = 0, i = 0; i < m_params.n_patches; i++) + { + patch & p = patches[i]; + + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 0); + a_vec dx = p.phi * A * alpha.col(i); + + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); + a_vec dy = p.phi * A * alpha.col(i); + + for(index_t j = 0; j < patches[i].xyz.n_cols; j++, v++) + { + n = {-dx(j), -dy(j), 1}; + n = normalise(p.T * n); + new_mesh->normal(v) = {n(0), n(1), n(2)}; + } + } + + che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc"), che_off::NOFF); + + gproshan_debug(saved new point cloud); + + return new_mesh; +} + + +real_t msparse_coding::execute_tmp() +{ + // fill holes + size_t threshold = mesh->n_vertices(); + delete [] fill_all_holes(mesh); + TIC(d_time) poisson(mesh, threshold, 2); TOC(d_time) + gproshan_debug_var(d_time); + + // remove possible non manifold vertices + mesh->remove_non_manifold_vertices(); + + // sampling including new vertices + TIC(d_time) init_sampling(); TOC(d_time) + gproshan_debug_var(d_time); + + // initializing patches with threshold + TIC(d_time) init_patches(1, [&threshold](const index_t & i) -> bool { return i < threshold; }); TOC(d_time) + gproshan_debug_var(d_time); + + // learning only from valid patches + TIC(d_time) learning(); TOC(d_time) + gproshan_debug_var(d_time); + + // including vertices out of threshold + TIC(d_time) init_patches(0); TOC(d_time) + gproshan_debug_var(d_time); + + // Update new alphas, propagating the info towards the center + TIC(d_time) update_alphas(alpha, threshold); TOC(d_time) + gproshan_debug_var(d_time); + + // sparse coding and reconstruction with all patches + TIC(d_time) sparse_coding(); TOC(d_time) + gproshan_debug_var(d_time); + + TIC(d_time) mesh_reconstruction(); TOC(d_time) + gproshan_debug_var(d_time); + + return 0; } void msparse_coding::learning() @@ -122,7 +751,7 @@ void msparse_coding::init_sampling() else { sampling.reserve(m_params.n_patches); - if(!load_sampling(sampling, phi_basis->radio(), mesh, m_params.n_patches)) + if(!gproshan::load_sampling(sampling, phi_basis->radio(), mesh, m_params.n_patches)) cerr << "Failed to load sampling" << endl; } From f64867c3e40d0053b4dc39eaf302fa2cc8cc0989 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 13:21:59 +0100 Subject: [PATCH 0408/1018] mdict: minor changes --- include/mesh/che_img.h | 3 --- shaders/vertex_pointcloud.glsl | 1 - src/mesh/che.cpp | 1 + src/mesh/che_img.cpp | 28 ---------------------------- src/viewer/viewer.cpp | 1 - 5 files changed, 1 insertion(+), 33 deletions(-) diff --git a/include/mesh/che_img.h b/include/mesh/che_img.h index 319d2b3d..a7c56a63 100644 --- a/include/mesh/che_img.h +++ b/include/mesh/che_img.h @@ -14,9 +14,6 @@ class che_img : public che che_img(const std::string & file); che_img(const che_img & mesh); virtual ~che_img(); - void write_file(const std::string & file) const; - //void save_img(const std::string & file, size_t tam) const; - static void save_img(const che * mesh, const std::string & file, size_t tam); private: void read_file(const std::string & file); diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index 57d2b8c5..a74a31be 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -21,6 +21,5 @@ void main() vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); - gl_PointSize = 1; } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 13222eb7..663b53d1 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -456,6 +456,7 @@ size_t che::genus() const return (g - 2) / (-2); } +// The Gauss-Bonnet Scheme real_t che::mean_curvature(const index_t & v) { real_t h = 0; diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index 61249bcd..ddaa7d11 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -56,34 +56,6 @@ void che_img::read_file(const string & file) thread([](CImg img) { img.display(); }, img).detach(); } -void che_img::write_file(const string & file) const -{ - ofstream os(file); - - os << "OFF" << endl; - os << n_vertices_ << " " << n_faces_ << " 0" << endl; - - for(size_t v = 0; v < n_vertices_; v++) - os << GT[v] << endl; - - for(index_t he = 0; he < n_half_edges_; he++) - { - if(!(he % che::mtrig)) os << che::mtrig; - os << " " << VT[he]; - if(he % che::mtrig == che::mtrig - 1) os << endl; - } - - os.close(); -} -void save_img(const che * mesh, const std::string & file, size_t tam) -{ - CImg image_out(tam,tam); - - for(size_t v = 0; v < mesh->n_vertices(); v++) - image_out(mesh->gt(v).x, mesh->gt(v).y) = mesh->gt(v).z; - - image_out.save(file.c_str()); -} } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0f10875d..30339327 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -231,7 +231,6 @@ void viewer::init_gl() glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_PROGRAM_POINT_SIZE); } void viewer::init_imgui() From 232a129a172666cf222cbc5b40bd610295f57fe8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 13:55:05 +0100 Subject: [PATCH 0409/1018] update build badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d58251c..83a8a42e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=mdict) +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) Install all dependencies and run: From 2aac2adebf531fe914ce08747e9ff3c1391cb402 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 20:10:32 +0100 Subject: [PATCH 0410/1018] mdict: cleaning patch code --- include/mdict/patch.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/mdict/patch.h b/include/mdict/patch.h index a17fbc99..f951b2e4 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -31,7 +31,6 @@ namespace gproshan::mdict { class msparse_coding; typedef function fmask_t; -typedef function fmask_local_t; typedef std::map vpatches_t; /// @@ -41,15 +40,15 @@ class patch std::vector vertices; ///< Vertices of the patch. a_mat T; ///< Transformation matrix. a_vec x; ///< Center point. - a_mat xyz; ///< Matrix of points. - a_mat phi; - double avg_dist; // Average distance betweenn points in a patch - real_t radio; // radio of a patch - size_t min_nv; + a_mat xyz; ///< Matrix of points. + a_mat phi; ///< Projected basis. + double avg_dist; ///< Average distance between points. + real_t radio; ///< Radio. + size_t min_nv; ///< public: - static size_t expected_nv; ///< Expected number of patch vertices. - static real_t nyquist_factor; ///< nyquist factor + static size_t expected_nv; ///< Expected number of patch vertices. + static real_t nyquist_factor; ///< nyquist factor public: patch() = default; @@ -58,9 +57,10 @@ class patch void init( che * mesh, ///< input mesh. const index_t & v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. - const real_t & radio_, ///< euclidean radio in XY of the patch. - index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. + const real_t & radio_, ///< euclidean radio in XY of the patch. + index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); + void init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, From 798b5c4dd0a3839a3e3c0283c16c33b61691b070 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 20:27:16 +0100 Subject: [PATCH 0411/1018] rt: add new rt embree splat class --- include/raytracing/rt_embree_splat.h | 26 ++++++++++++++++++++++++++ src/raytracing/rt_embree_splat.cpp | 1 + 2 files changed, 27 insertions(+) create mode 100644 include/raytracing/rt_embree_splat.h create mode 100644 src/raytracing/rt_embree_splat.cpp diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h new file mode 100644 index 00000000..ff475580 --- /dev/null +++ b/include/raytracing/rt_embree_splat.h @@ -0,0 +1,26 @@ +#ifdef GPROSHAN_EMBREE + +#ifndef RT_EMBREE_SPLAT_H +#define RT_EMBREE_SPLAT_H + +#include "raytracing/rt_embree.h" + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +class rt_embree_splat +{ + private: + ray_hit rh; +}; + + +} // namespace gproshan + +#endif // RT_EMBREE_SPLAT_H + +#endif // GPROSHAN_EMBREE + diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp new file mode 100644 index 00000000..d42ca6f4 --- /dev/null +++ b/src/raytracing/rt_embree_splat.cpp @@ -0,0 +1 @@ +#include "raytracing/rt_embree_splat.h" From bbfdb6af4cfc6fed348addf6e2a740970803c835 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 20:49:18 +0100 Subject: [PATCH 0412/1018] rt: organizing embree code --- include/raytracing/rt_embree.h | 90 +++++++++------------------------- src/raytracing/rt_embree.cpp | 51 ++++++++++++++++++- 2 files changed, 74 insertions(+), 67 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 18e4b70e..7e4963a8 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -17,91 +17,49 @@ namespace gproshan::rt { class embree : public raytracing { - struct ray_hit: public RTCRayHit - { - ray_hit(const glm::vec3 & origin = glm::vec3(0.0f), - const glm::vec3 & direction = glm::vec3(0.0f), - float near = 1e-5f, - float far = FLT_MAX) + protected: + struct ray_hit : public RTCRayHit { - ray.org_x = origin.x; - ray.org_y = origin.y; - ray.org_z = origin.z; - ray.tnear = near; - - ray.dir_x = direction.x; - ray.dir_y = direction.y; - ray.dir_z = direction.z; - ray.time = 0.0f; - - ray.tfar = far; - ray.mask = 0; - ray.flags = 0; - - //hit.primID = RTC_INVALID_GEOMETRY_ID; - hit.geomID = RTC_INVALID_GEOMETRY_ID; - hit.instID[0] = RTC_INVALID_GEOMETRY_ID; - } - - const glm::vec3 org() const - { - return {ray.org_x, ray.org_y, ray.org_z}; - } - - const glm::vec3 dir() const - { - return {ray.dir_x, ray.dir_y, ray.dir_z}; - } + ray_hit(const glm::vec3 & p_org = glm::vec3(0.0f), + const glm::vec3 & v_dir = glm::vec3(0.0f), + float near = 1e-5f, + float far = FLT_MAX); + + glm::vec3 org() const; + glm::vec3 dir() const; + glm::vec3 color(const rt_mesh & mesh) const; + glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const; + glm::vec3 position() const; + }; + + + RTCDevice device; + RTCScene scene; + RTCIntersectContext intersect_context; - const glm::vec3 color(const rt_mesh & mesh) - { - const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : - mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm::vec3(c.x, c.y, c.z); - } - - const glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const - { - if(flat || mesh.pointcloud) - return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - - const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm::normalize(glm::vec3(n.x, n.y, n.z)); - } - - const glm::vec3 position() const - { - return org() + ray.tfar * dir(); - } - }; - - - RTCDevice device; - RTCScene scene; - RTCIntersectContext intersect_context; - public: static float pc_radius; public: embree(const std::vector & meshes, const bool & pointcloud = false); - ~embree(); + virtual ~embree(); - private: + protected: bool intersect(ray_hit & r); bool occluded(ray_hit & r); - glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); - float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - void build_bvh(const std::vector & meshes, const bool & pointcloud = false); index_t add_sphere(const glm::vec4 & xyzr); index_t add_mesh(const che * mesh); index_t add_point_cloud(const che * mesh); float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); + glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); + + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); + float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 0d1ff82f..88b31245 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -12,11 +12,60 @@ namespace gproshan::rt { -void compute_normals(glm::vec3 * normals, const glm::vec3 * vertices, const size_t & n_vertices) +embree::ray_hit::ray_hit(const glm::vec3 & p_org, const glm::vec3 & v_dir, float near, float far) { + ray.org_x = p_org.x; + ray.org_y = p_org.y; + ray.org_z = p_org.z; + ray.tnear = near; + ray.dir_x = v_dir.x; + ray.dir_y = v_dir.y; + ray.dir_z = v_dir.z; + + ray.time = 0.0f; + + ray.tfar = far; + ray.mask = 0; + ray.flags = 0; + + //hit.primID = RTC_INVALID_GEOMETRY_ID; + hit.geomID = RTC_INVALID_GEOMETRY_ID; + hit.instID[0] = RTC_INVALID_GEOMETRY_ID; +} + +glm::vec3 embree::ray_hit::org() const +{ + return {ray.org_x, ray.org_y, ray.org_z}; +} + +glm::vec3 embree::ray_hit::dir() const +{ + return {ray.dir_x, ray.dir_y, ray.dir_z}; +} + +glm::vec3 embree::ray_hit::color(const rt_mesh & mesh) const +{ + const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : + mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + return glm::vec3(c.x, c.y, c.z); +} + +glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const +{ + if(flat || mesh.pointcloud) + return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); + + const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + return glm::normalize(glm::vec3(n.x, n.y, n.z)); } +glm::vec3 embree::ray_hit::position() const +{ + return org() + ray.tfar * dir(); +} + + void embree_error(void * ptr, RTCError error, const char * str) { fprintf(stderr, "EMBREE ERROR: %s\n", str); From dcbc0c0fe63c83767922b3fb91b437fcbf0a43a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 21:40:37 +0100 Subject: [PATCH 0413/1018] rt: adding new class embree_splat --- include/raytracing/rt_embree.h | 3 +- include/raytracing/rt_embree_splat.h | 7 ++-- include/viewer/viewer.h | 20 +++-------- src/raytracing/rt_embree.cpp | 39 ++++++++++---------- src/raytracing/rt_embree_splat.cpp | 54 ++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 12 ++++++- 6 files changed, 97 insertions(+), 38 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 7e4963a8..a2eabf2e 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -41,6 +41,7 @@ class embree : public raytracing static float pc_radius; public: + embree(); embree(const std::vector & meshes, const bool & pointcloud = false); virtual ~embree(); @@ -51,7 +52,7 @@ class embree : public raytracing void build_bvh(const std::vector & meshes, const bool & pointcloud = false); index_t add_sphere(const glm::vec4 & xyzr); index_t add_mesh(const che * mesh); - index_t add_point_cloud(const che * mesh); + virtual index_t add_pointcloud(const che * mesh); float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index ff475580..25946506 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -11,10 +11,13 @@ namespace gproshan::rt { -class rt_embree_splat +class embree_splat : public embree { + public: + embree_splat(const std::vector & meshes, const bool & pointcloud); + private: - ray_hit rh; + index_t add_pointcloud(const che * mesh); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 30f9bb9e..e25f984b 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -11,6 +11,8 @@ #include "viewer/frame.h" #include "viewer/che_viewer.h" +#include "raytracing/raytracing.h" + #include "viewer/include_opengl.h" #include "imgui.h" @@ -18,15 +20,6 @@ #include "imgui_impl_opengl3.h" -#ifdef GPROSHAN_EMBREE - #include "raytracing/rt_embree.h" -#endif // GPROSHAN_EMBREE - -#ifdef GPROSHAN_OPTIX - #include "raytracing/rt_optix.h" -#endif // GPROSHAN_OPTIX - - #define N_MESHES 12 @@ -86,13 +79,8 @@ class viewer frame * render_frame = nullptr; - #ifdef GPROSHAN_EMBREE - rt::embree * rt_embree = nullptr; - #endif // GPROSHAN_EMBREE - - #ifdef GPROSHAN_OPTIX - rt::optix * rt_optix = nullptr; - #endif // GPROSHAN_OPTIX + rt::raytracing * rt_embree = nullptr; + rt::raytracing * rt_optix = nullptr; bool action = false; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 88b31245..6e8f40d7 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -73,15 +73,17 @@ void embree_error(void * ptr, RTCError error, const char * str) float embree::pc_radius = 0.001; -embree::embree(const std::vector & meshes, const bool & pointcloud) +embree::embree() { device = rtcNewDevice(NULL); - rtcSetDeviceErrorFunction(device, embree_error, NULL); - scene = rtcNewScene(device); rtcInitIntersectContext(&intersect_context); + rtcSetDeviceErrorFunction(device, embree_error, NULL); +} +embree::embree(const std::vector & meshes, const bool & pointcloud): embree() +{ build_bvh(meshes, pointcloud); } @@ -91,11 +93,23 @@ embree::~embree() rtcReleaseDevice(device); } +bool embree::intersect(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + +bool embree::occluded(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + void embree::build_bvh(const std::vector & meshes, const bool & pointcloud) { for(auto & m: meshes) if(!m->n_faces() || pointcloud) - geomID_mesh[add_point_cloud(m)] = {m, true}; + geomID_mesh[add_pointcloud(m)] = {m, true}; else geomID_mesh[add_mesh(m)] = {m, false}; @@ -152,7 +166,7 @@ index_t embree::add_mesh(const che * mesh) return geom_id; } -index_t embree::add_point_cloud(const che * mesh) +index_t embree::add_pointcloud(const che * mesh) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); @@ -181,6 +195,8 @@ index_t embree::add_point_cloud(const che * mesh) index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); + + gproshan_log(EMBREE); return geom_id; } @@ -252,19 +268,6 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) return L / total_tfar; } -bool embree::intersect(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - -bool embree::occluded(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - - glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light,const bool & flat) { ray_hit r(org, dir); diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index d42ca6f4..486f20ff 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -1 +1,55 @@ #include "raytracing/rt_embree_splat.h" + +#ifdef GPROSHAN_EMBREE + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +embree_splat::embree_splat(const std::vector & meshes, const bool & pointcloud) +{ + build_bvh(meshes, pointcloud); +} + +index_t embree_splat::add_pointcloud(const che * mesh) +{ + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + + glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, + 4 * sizeof(float), + mesh->n_vertices() + ); + + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_NORMAL, 0, + RTC_FORMAT_FLOAT3, + 3 * sizeof(float), + mesh->n_vertices() + ); + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices(); i++) + { + pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); + normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); + } + + rtcCommitGeometry(geom); + + index_t geom_id = rtcAttachGeometry(scene, geom); + rtcReleaseGeometry(geom); + + gproshan_log(EMBREE SPLAT); + + return geom_id; +} + + +} // namespace gproshan + +#endif // GPROSHAN_EMBREE + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 30339327..5c88ba1a 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -18,6 +18,16 @@ #include "mesh/che_ply.h" #include "mesh/che_sphere.h" +#ifdef GPROSHAN_EMBREE + #include "raytracing/rt_embree.h" + #include "raytracing/rt_embree_splat.h" +#endif // GPROSHAN_EMBREE + +#ifdef GPROSHAN_OPTIX + #include "raytracing/rt_optix.h" +#endif // GPROSHAN_OPTIX + + #include @@ -740,7 +750,7 @@ void viewer::render_embree() double time_build_embree; TIC(time_build_embree); - rt_embree = new rt::embree({active_mesh()}, render_pointcloud); + rt_embree = new rt::embree_splat({active_mesh()}, render_pointcloud); TOC(time_build_embree); gproshan_log_var(time_build_embree); From 5c4a1e7e44d3a8d28edcccc6532a68d0007dd8ed Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 22:33:50 +0100 Subject: [PATCH 0414/1018] rt: embree splat rendering uniform radios, flat --- include/raytracing/rt_embree.h | 4 ++-- include/raytracing/rt_embree_splat.h | 12 ++++++++++ src/raytracing/rt_embree.cpp | 2 -- src/raytracing/rt_embree_splat.cpp | 36 ++++++++++++++++++++++------ src/viewer/viewer.cpp | 9 ++----- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index a2eabf2e..f40c3a2e 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -52,9 +52,9 @@ class embree : public raytracing void build_bvh(const std::vector & meshes, const bool & pointcloud = false); index_t add_sphere(const glm::vec4 & xyzr); index_t add_mesh(const che * mesh); - virtual index_t add_pointcloud(const che * mesh); - float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); + virtual index_t add_pointcloud(const che * mesh); + virtual float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index 25946506..b98efefc 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -13,11 +13,23 @@ namespace gproshan::rt { class embree_splat : public embree { + struct splat + { + glm::vec4 xyzr; + glm::vec3 normal; + glm::vec3 color; + }; + + std::vector vsplat; + public: embree_splat(const std::vector & meshes, const bool & pointcloud); private: index_t add_pointcloud(const che * mesh); + float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); + + void init_splats(const che * mesh); }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 6e8f40d7..0c8ce126 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -196,8 +196,6 @@ index_t embree::add_pointcloud(const che * mesh) index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - gproshan_log(EMBREE); - return geom_id; } diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 486f20ff..69961a8a 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -15,27 +15,29 @@ embree_splat::embree_splat(const std::vector & meshes, const bool & point index_t embree_splat::add_pointcloud(const che * mesh) { + init_splats(mesh); + RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, 4 * sizeof(float), - mesh->n_vertices() + vsplat.size() ); glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices() + vsplat.size() ); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < vsplat.size(); i++) { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); - normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); + pxyzr[i] = vsplat[i].xyzr; + normal[i] = vsplat[i].normal; } rtcCommitGeometry(geom); @@ -43,11 +45,31 @@ index_t embree_splat::add_pointcloud(const che * mesh) index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - gproshan_log(EMBREE SPLAT); - return geom_id; } +float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) +{ + position = r.position(); + normal = vsplat[r.hit.primID].normal; + color = vsplat[r.hit.primID].color; + + return 1e-5f; +} + +void embree_splat::init_splats(const che * mesh) +{ + const size_t s = 10; + vsplat.reserve(vsplat.size() + (mesh->n_vertices() + s - 1) / s); + + for(index_t i = 0; i < mesh->n_vertices(); i += s) + vsplat.push_back({ glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius), + glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z), + glm::vec3(mesh->color(i).x, mesh->color(i).y, mesh->color(i).z) + }); + +} + } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 5c88ba1a..e25b97d8 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -68,13 +68,8 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); - #ifdef GPROSHAN_EMBREE - if(rt_embree) delete rt_embree; - #endif // GPROSHAN_EMBREE - - #ifdef GPROSHAN_OPTIX - if(rt_optix) delete rt_optix; - #endif // GPROSHAN_OPTIX + delete rt_embree; + delete rt_optix; if(render_frame) delete render_frame; } From 162352319150fc3aa510046477b9b0f5a482526a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jan 2021 23:48:06 +0100 Subject: [PATCH 0415/1018] rt: embree splats non uniform radio --- include/raytracing/rt_embree_splat.h | 24 +++++++-- src/raytracing/rt_embree_splat.cpp | 80 +++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 16 deletions(-) diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index b98efefc..db810965 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -13,11 +13,29 @@ namespace gproshan::rt { class embree_splat : public embree { + static const size_t K = 16; + struct splat { - glm::vec4 xyzr; - glm::vec3 normal; - glm::vec3 color; + glm::vec3 P[K]; + glm::vec3 N[K]; + glm::vec3 C[K]; + float radio; + + const glm::vec4 xyzr() + { + return glm::vec4(P[0], radio); + } + + const glm::vec3 & normal() + { + return N[0]; + } + + const glm::vec3 & color() + { + return C[0]; + } }; std::vector vsplat; diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 69961a8a..21875a30 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -3,6 +3,10 @@ #ifdef GPROSHAN_EMBREE +#include +#include + + // geometry processing and shape analysis framework // raytracing approach namespace gproshan::rt { @@ -36,8 +40,8 @@ index_t embree_splat::add_pointcloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < vsplat.size(); i++) { - pxyzr[i] = vsplat[i].xyzr; - normal[i] = vsplat[i].normal; + pxyzr[i] = vsplat[i].xyzr(); + normal[i] = vsplat[i].normal(); } rtcCommitGeometry(geom); @@ -51,23 +55,73 @@ index_t embree_splat::add_pointcloud(const che * mesh) float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) { position = r.position(); - normal = vsplat[r.hit.primID].normal; - color = vsplat[r.hit.primID].color; + normal = vsplat[r.hit.primID].normal(); + color = vsplat[r.hit.primID].color(); return 1e-5f; } void embree_splat::init_splats(const che * mesh) { - const size_t s = 10; - vsplat.reserve(vsplat.size() + (mesh->n_vertices() + s - 1) / s); - - for(index_t i = 0; i < mesh->n_vertices(); i += s) - vsplat.push_back({ glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius), - glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z), - glm::vec3(mesh->color(i).x, mesh->color(i).y, mesh->color(i).z) - }); - + const size_t n = 10; + vsplat.resize((mesh->n_vertices() + n - 1) / n); + + gproshan_log_var(vsplat.size()); + + #pragma omp parallel for + for(index_t i = 0; i < vsplat.size(); i++) + { + const index_t v = n * i; // random, feature aware index + + std::set points; + std::queue q; + + q.push(v); + points.insert(v); + + index_t u; + while(!q.empty() && points.size() < 2 * K) + { + for_star(he, mesh, q.front()) + { + u = mesh->vt(prev(he)); + if(points.find(u) == points.end()) + { + points.insert(u); + q.push(u); + } + } + + q.pop(); + } + + real_t dist, d; + const vertex & c = mesh->gt(v); + + splat & s = vsplat[i]; + for(index_t j = 0; j < K; j++) + { + dist = INFINITY; + + for(const index_t & p: points) + { + d = *(mesh->gt(p) - c); + if(d < dist) + { + dist = d; + u = p; + } + } + + points.erase(u); + + s.P[j] = glm::vec3(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z); + s.N[j] = glm::vec3(mesh->normal(u).x, mesh->normal(u).y, mesh->normal(u).z); + s.C[j] = glm::vec3(mesh->color(u).x, mesh->color(u).y, mesh->color(u).z); + } + + s.radio = glm::length(s.P[K - 1] - s.P[0]); + } } From 9f9a03855741e1a138f7a2ee22fa09ad4e32ac12 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 8 Jan 2021 00:52:54 +0100 Subject: [PATCH 0416/1018] rt: embree splat gaussian shading --- include/raytracing/rt_embree_splat.h | 20 ++++++++++++++++++++ src/raytracing/rt_embree.cpp | 6 +++--- src/raytracing/rt_embree_splat.cpp | 9 +++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index db810965..55637ee4 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -36,6 +36,26 @@ class embree_splat : public embree { return C[0]; } + + void shading(const glm::vec3 & p, glm::vec3 & normal, glm::vec3 & color) + { + normal = glm::vec3(0); + color = glm::vec3(0); + + float w, sum_w = 0, sigma = radio * 0.1; + + for(index_t i = 0; i < K; i++) + { + w = glm::length(p - P[i]); + w = exp(-0.5 * w * w / (sigma * sigma)) / (sigma * sqrt(2 * M_PI)); + normal += w * N[i]; + color += w * C[i]; + sum_w += w; + } + + normal /= sum_w; + color /= sum_w; + } }; std::vector vsplat; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 0c8ce126..a8d9d271 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -244,7 +244,7 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) glm::vec3 position, normal, color; glm::vec4 L(0); - while(total_tfar < 0.1) +// while(total_tfar < 0.1) { total_tfar += r.ray.tfar; @@ -259,8 +259,8 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) L += r.ray.tfar * li(light, position, normal, color, near); r = ray_hit(r.position(), r.dir()); - if(!intersect(r)) - break; +// if(!intersect(r)) +// break; } return L / total_tfar; diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 21875a30..4ca679be 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -55,15 +55,16 @@ index_t embree_splat::add_pointcloud(const che * mesh) float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) { position = r.position(); - normal = vsplat[r.hit.primID].normal(); - color = vsplat[r.hit.primID].color(); + vsplat[r.hit.primID].shading(position, normal, color); + // normal = vsplat[r.hit.primID].normal(); + // color = vsplat[r.hit.primID].color(); - return 1e-5f; + return 1e-2f; } void embree_splat::init_splats(const che * mesh) { - const size_t n = 10; + const size_t n = 5; vsplat.resize((mesh->n_vertices() + n - 1) / n); gproshan_log_var(vsplat.size()); From 4b32fa1555d9b052697a6be89cef87a9b9f2e12e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 12 Jan 2021 22:37:59 +0100 Subject: [PATCH 0417/1018] rt: ray discard hit --- include/raytracing/rt_embree_splat.h | 10 ++++++---- src/raytracing/rt_embree_splat.cpp | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index 55637ee4..16d1e4ce 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -13,7 +13,7 @@ namespace gproshan::rt { class embree_splat : public embree { - static const size_t K = 16; + static const size_t K = 32; struct splat { @@ -37,17 +37,17 @@ class embree_splat : public embree return C[0]; } - void shading(const glm::vec3 & p, glm::vec3 & normal, glm::vec3 & color) + float shading(const glm::vec3 & p, glm::vec3 & normal, glm::vec3 & color) { normal = glm::vec3(0); color = glm::vec3(0); - float w, sum_w = 0, sigma = radio * 0.1; + float w, sum_w = 0, sigma = radio * pc_radius; for(index_t i = 0; i < K; i++) { w = glm::length(p - P[i]); - w = exp(-0.5 * w * w / (sigma * sigma)) / (sigma * sqrt(2 * M_PI)); + w = exp(-0.5 * w * w / (sigma * sigma)); normal += w * N[i]; color += w * C[i]; sum_w += w; @@ -55,6 +55,8 @@ class embree_splat : public embree normal /= sum_w; color /= sum_w; + + return sum_w; } }; diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 4ca679be..6b0efcd4 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -55,16 +55,28 @@ index_t embree_splat::add_pointcloud(const che * mesh) float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) { position = r.position(); - vsplat[r.hit.primID].shading(position, normal, color); + float w = vsplat[r.hit.primID].shading(position, normal, color); // normal = vsplat[r.hit.primID].normal(); // color = vsplat[r.hit.primID].color(); - +/* if(w < 1e-2f) + { + normal = glm::vec3(0); + color = glm::vec3(0); + } +*/ + if(w < 1e-5f) + { + r = ray_hit(r.position(), r.dir()); + if(intersect(r)) + return pointcloud_hit(position, normal, color, r); + } + return 1e-2f; } void embree_splat::init_splats(const che * mesh) { - const size_t n = 5; + const size_t n = 10; vsplat.resize((mesh->n_vertices() + n - 1) / n); gproshan_log_var(vsplat.size()); From c1bd38e0219a71ce547ee998d2fc533a7547c78d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 3 Feb 2021 19:39:57 +0100 Subject: [PATCH 0418/1018] embree minimum version updated --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d53abec..00123c5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -find_package(embree 3.0) +find_package(embree 3.10) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) include_directories(${embree_INCLUDE_DIRS}) From 94fcb997b6c5c0ed64a908ecd677cdc8c7eb77db Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 8 Feb 2021 21:27:04 +0100 Subject: [PATCH 0419/1018] Create CHANGELOG.md --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..0af757ee --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +Version History +--------------- + +### gproshan 3.0.0 +- Upgraded version of the sparse mesh coding with feature aware sampling module and published paper reference. +- Updated save mesh with options for file types, normals, and point-cloud. +- Added render option using [Embree](https://www.embree.org/) ray tracing. +- Implemented the loading and rendering of point-clouds. +- Added heatmap viewer options and loading vertex color from a file. +- New user interface implemented with [ImGui](https://github.com/ocornut/imgui). +- Viewer upgraded using GLEW, GLFW, and GLM. From d892a3fe315851900b8e40f77554de4d7c722c6d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 8 Feb 2021 22:11:49 +0100 Subject: [PATCH 0420/1018] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a7fad905..83a8a42e 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=raytracing) +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) Install all dependencies and run: From 3eb72faf55f6845b6c0df582d407ee8f27fc5ac0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 11 Feb 2021 14:06:49 +0100 Subject: [PATCH 0421/1018] fix mouse in windows coordinates --- include/viewer/camera.h | 12 ++++++------ include/viewer/viewer.h | 10 +++++----- src/viewer/camera.cpp | 20 ++++++-------------- src/viewer/viewer.cpp | 10 +++++++--- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/include/viewer/camera.h b/include/viewer/camera.h index 5e6bbd7c..1fa4c323 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -1,5 +1,5 @@ -#ifndef VIEWER_CAMERA_H -#define VIEWER_CAMERA_H +#ifndef CAMERA_H +#define CAMERA_H #include "mesh/quaternion.h" @@ -24,15 +24,15 @@ class camera public: camera(); - void mouse(int button, int state, int x, int y); - void motion(int x, int y); + void mouse(int button, int state, int x, int y, int w, int h); + void motion(int x, int y, int w, int h); void idle(); void zoom_in(); void zoom_out(); quaternion current_rotation() const; private: - quaternion click_to_sphere(int x, int y); + quaternion click_to_sphere(int x, int y, int w, int h); friend std::ostream & operator << (std::ostream & os, const camera & cam); friend std::istream & operator >> (std::istream & is, camera & cam); @@ -41,5 +41,5 @@ class camera } // namespace gproshan -#endif +#endif // CAMERA_H diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index e25f984b..204b603a 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -1,5 +1,5 @@ -#ifndef VIEWER_VIEWER_H -#define VIEWER_VIEWER_H +#ifndef VIEWER_H +#define VIEWER_H #include #include @@ -49,8 +49,8 @@ class viewer GLFWwindow * window = nullptr; - int viewport_width; - int viewport_height; + int window_width, window_height; + int viewport_width, viewport_height; unsigned int idx_colormap = 1; // colormap index defined in shaders/colormap.glsl const std::vector colormap = {"vertex color", "blue", "red", "blue/read"}; @@ -175,5 +175,5 @@ class viewer } // namespace gproshan -#endif // VIEWER_VIEWER_H +#endif // VIEWER_H diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index d89661d8..8640e99d 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -13,16 +13,8 @@ namespace gproshan { camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(1) {} -quaternion camera::click_to_sphere(int x, int y) +quaternion camera::click_to_sphere(int x, int y, int w, int h) { - GLint viewport[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - int w = viewport[2]; - int h = viewport[3]; - - x %= w; - y %= h; - quaternion p( 0., 2. * (double) x / (double) w - 1., 2. * (double) y / (double) h - 1., @@ -46,12 +38,12 @@ quaternion camera::current_rotation() const return (p_drag * p_click.conj()) * r_last; } -void camera::mouse(int , int state, int x, int y) +void camera::mouse(int , int state, int x, int y, int w, int h) { quaternion momentum = 1; if(state == GLFW_PRESS) - p_click = p_drag = p_last = click_to_sphere(x, y); + p_click = p_drag = p_last = click_to_sphere(x, y, w, h); if(state == GLFW_RELEASE) { @@ -64,7 +56,7 @@ void camera::mouse(int , int state, int x, int y) } else { - momentum = 1.; + momentum = 1.; } r_last = p_drag * p_click.conj() * r_last; @@ -72,11 +64,11 @@ void camera::mouse(int , int state, int x, int y) } } -void camera::motion(int x, int y) +void camera::motion(int x, int y, int w, int h) { t_last = clock(); p_last = p_drag; - p_drag = click_to_sphere(x, y); + p_drag = click_to_sphere(x, y, w, h); } void camera::zoom_in() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e25b97d8..1cbcbe59 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -78,7 +78,9 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { + glfwGetWindowSize(window, &window_width, &window_height); glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + viewport_width /= m_window_size[n_meshes][1]; viewport_height /= m_window_size[n_meshes][0]; @@ -355,16 +357,17 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a view->processes[key].selected = !view->processes[key].selected; } -void viewer::mouse_callback(GLFWwindow* window, int button, int action, int mods) +void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); + printf("mouse %f, %f, %d, %d\n", xpos, ypos, view->window_width, view->window_height); if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); - else view->cam.mouse(button, action, xpos, ypos); + else view->cam.mouse(button, action, xpos, ypos, view->window_width, view->window_height); } void viewer::cursor_callback(GLFWwindow * window, double x, double y) @@ -375,7 +378,8 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; - view->cam.motion(x, y); + printf("%lf, %lf, %d, %d\n", x, y, view->window_width, view->window_height); + view->cam.motion(x, y, view->window_width, view->window_height); view->action = true; } } From 92eaa5f70d1a6c6f939fa657a5762fbf7af841e8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 11 Feb 2021 14:38:16 +0100 Subject: [PATCH 0422/1018] add framebuffer and window size callback --- include/viewer/viewer.h | 10 +++------- src/app_viewer.cpp | 6 +++--- src/viewer/viewer.cpp | 34 ++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 204b603a..f7cff1fe 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -101,14 +101,12 @@ class viewer shader shader_sphere; public: - std::vector other_vertices; std::vector vectors; std::vector sub_menus; public: - - viewer(); + viewer(int width = 1600, int height = 900); virtual ~viewer(); bool run(); @@ -118,8 +116,6 @@ class viewer void add_mesh(che * p_mesh); private: - - // init void info_gl(); void init_gl(); void init_imgui(); @@ -130,13 +126,13 @@ class viewer void render_embree(); void render_optix(); - // callbacks + static void framebuffer_size_callback(GLFWwindow * window, int width, int height); + static void window_size_callback(GLFWwindow * window, int width, int height); static void keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods); static void mouse_callback(GLFWwindow * window, int button, int action, int mods); static void cursor_callback(GLFWwindow * window, double x, double y); static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); - // menu functions static bool menu_help(viewer * view); static bool menu_save_load_view(viewer * view); static bool menu_reset_mesh(viewer * view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 75f421b9..065bae7f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -289,7 +289,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) K = K < N_MESHES ? K : N_MESHES; for(index_t k = 0; k < N_MESHES; k++) { - if(k) view->add_mesh({new che(*mesh)}); + if(k) view->add_mesh(new che(*mesh)); view->idx_active_mesh = k; eigvec.col(k) -= eigvec.col(k).min(); @@ -635,7 +635,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) basis_dct phi(n); msparse_coding msc(mesh, &phi, params); - view->add_mesh({msc.point_cloud_reconstruction(percentage_size, radio_factor)}); + view->add_mesh(msc.point_cloud_reconstruction(percentage_size, radio_factor)); } return true; @@ -973,7 +973,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) gproshan_debug_var(view->time); //if(view->n_meshes < 2) - // view->add_mesh({new che(*mesh)}); + // view->add_mesh(new che(*mesh)); return false; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1cbcbe59..fc590d41 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -47,7 +47,7 @@ const int viewer::m_window_size[N_MESHES + 1][2] = {{1, 1}, }; -viewer::viewer() +viewer::viewer(int width, int height): window_width(width), window_height(height) { init_gl(); init_glsl(); @@ -56,7 +56,7 @@ viewer::viewer() info_gl(); - sphere.init(new che_sphere(0.01), false); + sphere.init(new che_sphere(0.01), false); } viewer::~viewer() @@ -71,19 +71,13 @@ viewer::~viewer() delete rt_embree; delete rt_optix; - if(render_frame) delete render_frame; + delete render_frame; } bool viewer::run() { while(!glfwWindowShouldClose(window)) { - glfwGetWindowSize(window, &window_width, &window_height); - glfwGetFramebufferSize(window, &viewport_width, &viewport_height); - - viewport_width /= m_window_size[n_meshes][1]; - viewport_height /= m_window_size[n_meshes][0]; - eye = vertex(0., 0., -2. * cam.zoom); center = vertex(0., 0., 0.); up = vertex(0., 1., 0.); @@ -221,10 +215,12 @@ void viewer::init_gl() glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif - window = glfwCreateWindow(1600, 900, "gproshan", NULL, NULL); + window = glfwCreateWindow(window_width, window_height, "gproshan", NULL, NULL); glfwSetWindowUserPointer(window, this); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetWindowSizeCallback(window, window_size_callback); glfwSetKeyCallback(window, keyboard_callback); glfwSetMouseButtonCallback(window, mouse_callback); glfwSetCursorPosCallback(window, cursor_callback); @@ -338,6 +334,24 @@ void viewer::add_mesh(che * p_mesh) meshes[m].vx = m % cols; meshes[m].vy = rows - (m / cols) - 1; } + + glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + viewport_width /= m_window_size[n_meshes][1]; + viewport_height /= m_window_size[n_meshes][0]; +} + +void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) +{ + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + view->viewport_width = width / m_window_size[view->n_meshes][1]; + view->viewport_height = height / m_window_size[view->n_meshes][0]; +} + +void viewer::window_size_callback(GLFWwindow * window, int width, int height) +{ + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + view->window_width = width; + view->window_height = height; } void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods) From fcb7d442ed55beb0e5baad836c63d7978442d9b6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 11 Feb 2021 17:31:16 +0100 Subject: [PATCH 0423/1018] renaming defines, removing extra printf --- include/viewer/frame.h | 6 +++--- include/viewer/shader.h | 6 +++--- src/viewer/viewer.cpp | 2 -- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/include/viewer/frame.h b/include/viewer/frame.h index 452104bb..849840c6 100644 --- a/include/viewer/frame.h +++ b/include/viewer/frame.h @@ -1,5 +1,5 @@ -#ifndef VIEWER_FRAME_H -#define VIEWER_FRAME_H +#ifndef FRAME_H +#define FRAME_H #include "viewer/shader.h" @@ -29,5 +29,5 @@ class frame } // namespace gproshan -#endif // VIEWER_FRAME_H +#endif // FRAME_H diff --git a/include/viewer/shader.h b/include/viewer/shader.h index c23ba6c1..9f77d671 100644 --- a/include/viewer/shader.h +++ b/include/viewer/shader.h @@ -1,5 +1,5 @@ -#ifndef VIEWER_SHADER_H -#define VIEWER_SHADER_H +#ifndef SHADER_H +#define SHADER_H #include #include @@ -36,5 +36,5 @@ class shader } // namespace gproshan -#endif // VIEWER_SHADER_H +#endif // SHADER_H diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index fc590d41..be22b3a7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -378,7 +378,6 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - printf("mouse %f, %f, %d, %d\n", xpos, ypos, view->window_width, view->window_height); if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); else view->cam.mouse(button, action, xpos, ypos, view->window_width, view->window_height); @@ -392,7 +391,6 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; - printf("%lf, %lf, %d, %d\n", x, y, view->window_width, view->window_height); view->cam.motion(x, y, view->window_width, view->window_height); view->action = true; } From b36aff9cbdf7babef9248866315498f65b32f03f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 11 Feb 2021 21:23:19 +0100 Subject: [PATCH 0424/1018] fix update normals --- src/mesh/che.cpp | 17 +++-------------- src/mesh/che_off.cpp | 3 --- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 663b53d1..6e3520bf 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -250,8 +250,6 @@ real_t che::area_surface() const void che::update_heatmap(const real_t * hm, real_t max_color) { - if(!VHC) VHC = new real_t[n_vertices_]; - if(!hm) { #pragma omp parallel for @@ -307,18 +305,7 @@ real_t & che::heatmap(const index_t & v) void che::update_normals() { - if(VN) return; // normals was already loaded/computed - - VN = new vertex[n_vertices_]; - - /* point cloud normals - if(!n_faces_) - { - kdtree rnn(GT, n_vertices_); - - return; - } - */ + if(!n_faces_) return; #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) @@ -1408,7 +1395,9 @@ void che::init(const size_t & n_v, const size_t & n_f) if(n_vertices_) EVT = new index_t[n_vertices_]; if(n_vertices_) EHT = new index_t[n_half_edges_]; + if(n_vertices_) VN = new vertex[n_vertices_]; if(n_vertices_) VC = new vertex[n_vertices_]; + if(n_vertices_) VHC = new real_t[n_vertices_]; #pragma omp parallel for for(index_t v = 0; v < n_vertices_; v++) diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 8bafeaa5..11a006b5 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -38,9 +38,6 @@ void che_off::read_file(const string & file) is >> n_v >> n_f >> v; init(n_v, n_f); - if(soff[0] == 'N') - VN = new vertex[n_vertices_]; - real_t alpha; // color for(index_t i = 0; i < n_vertices_; i++) { From 4ee528868b3ecdd9c3b770c5644b93bc5f4cfc8e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 13 Feb 2021 20:00:35 +0100 Subject: [PATCH 0425/1018] refactoring eigs_laplacian --- include/app_viewer.h | 1 + include/features/descriptor.h | 33 ++++++++++++++++++++++++++++++ include/laplacian/laplacian.h | 6 +++--- include/mesh/che.h | 2 +- src/app_viewer.cpp | 25 +++++----------------- src/features/descriptor.cpp | 25 ++++++++++++++++++++++ src/laplacian/fairing_spectral.cpp | 8 ++------ src/laplacian/laplacian.cpp | 14 +++++++------ src/mesh/che.cpp | 2 +- 9 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 include/features/descriptor.h create mode 100644 src/features/descriptor.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index b4104a61..ce8f1e84 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -26,6 +26,7 @@ #include "mdict/basis_dct.h" #include "mdict/patch.h" +#include "features/descriptor.h" #include "features/key_points.h" #include "features/key_components.h" diff --git a/include/features/descriptor.h b/include/features/descriptor.h new file mode 100644 index 00000000..b725d0a2 --- /dev/null +++ b/include/features/descriptor.h @@ -0,0 +1,33 @@ +#ifndef DESCRIPTOR_H +#define DESCRIPTOR_H + +#include "mesh/che.h" +#include "include_arma.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class descriptor +{ + public: + enum signature { GPS, HKS, WKS }; + + private: + a_sp_mat L, A; + a_vec eigval; + a_mat eigvec; + + public: + descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); + + private: + bool compute_eigs(const che * mesh, const size_t & n_eigs); +}; + + +} // namespace gproshan + +#endif // DESCRIPTOR_H + diff --git a/include/laplacian/laplacian.h b/include/laplacian/laplacian.h index 47abb2ea..fcad4da4 100644 --- a/include/laplacian/laplacian.h +++ b/include/laplacian/laplacian.h @@ -13,11 +13,11 @@ namespace gproshan { typedef Eigen::SparseMatrix sp_mat_e; -void laplacian(che * mesh, a_sp_mat & L, a_sp_mat & A); +void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A); -void laplacian(che * mesh, sp_mat_e & L, sp_mat_e & A); +void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A); -size_t eigs_laplacian(a_vec & eigval, a_mat & eigvec, che * mesh, const a_sp_mat & L, const a_sp_mat & A, const size_t & K); +size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k); } // namespace gproshan diff --git a/include/mesh/che.h b/include/mesh/che.h index 0ae15710..12b04133 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -69,7 +69,7 @@ class che real_t pdetriq(const index_t & t) const; real_t quality(); real_t area_trig(const index_t & t) const; - real_t area_vertex(const index_t & v); + real_t area_vertex(const index_t & v) const; real_t area_surface() const; void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); const vertex & color(const index_t & v) const; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 065bae7f..90e6d5dd 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -2,6 +2,7 @@ #include + using namespace std; using namespace gproshan::mdict; @@ -274,14 +275,10 @@ bool app_viewer::process_functional_maps(viewer * p_view) if(ImGui::Button("Run")) { a_sp_mat L, A; - - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); - a_vec eigval; a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) gproshan_log_var(view->time); gproshan_log_var(K); @@ -322,14 +319,10 @@ bool app_viewer::process_wks(viewer * p_view) if(ImGui::Button("Run")) { a_sp_mat L, A; - - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); - a_vec eigval; a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) gproshan_log_var(view->time); real_t max_s = 0; @@ -367,14 +360,10 @@ bool app_viewer::process_hks(viewer * p_view) if(ImGui::Button("Run")) { a_sp_mat L, A; - - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); - a_vec eigval; a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) gproshan_log_var(view->time); if(!K) return true; @@ -412,14 +401,10 @@ bool app_viewer::process_gps(viewer * p_view) if(ImGui::Button("Run")) { a_sp_mat L, A; - - TIC(view->time) laplacian(mesh, L, A); TOC(view->time) - gproshan_log_var(view->time); - a_vec eigval; a_mat eigvec; - TIC(view->time) K = eigs_laplacian(eigval, eigvec, mesh, L, A, K); TOC(view->time) + TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) gproshan_log_var(view->time); eigvec = abs(eigvec); diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp new file mode 100644 index 00000000..98068751 --- /dev/null +++ b/src/features/descriptor.cpp @@ -0,0 +1,25 @@ +#include "features/descriptor.h" + +#include "laplacian/laplacian.h" + +// geometry processing and shape analysis framework +namespace gproshan { + + +descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n_eigs) +{ + if(!compute_eigs(mesh, n_eigs)) + return; +} + +bool descriptor::compute_eigs(const che * mesh, const size_t & k) +{ + if(eigs_laplacian(mesh, eigval, eigvec, L, A, k) == 0) + return false; + + +} + + +} // namespace gproshan + diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index d6f0539f..d6d5b657 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -20,11 +20,6 @@ void fairing_spectral::compute(che * shape) { double time; - a_sp_mat L, A; - - TIC(time) laplacian(shape, L, A); TOC(time) - gproshan_debug_var(time); - positions = new vertex[shape->n_vertices()]; a_mat X((real_t *) positions, 3, shape->n_vertices(), false, true); @@ -33,10 +28,11 @@ void fairing_spectral::compute(che * shape) for(index_t v = 0; v < shape->n_vertices(); v++) positions[v] = shape->gt(v); + a_sp_mat L, A; a_vec eigval; a_mat eigvec; - TIC(time) k = eigs_laplacian(eigval, eigvec, shape, L, A, k); TOC(time) + TIC(time) k = eigs_laplacian(shape, eigval, eigvec, L, A, k); TOC(time) gproshan_debug_var(time); X = X * eigvec * eigvec.t(); diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 017c01ee..6660fd75 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -8,7 +8,7 @@ using namespace Eigen; namespace gproshan { -void laplacian(che * mesh, a_sp_mat & L, a_sp_mat & A) +void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) { size_t n_edges = mesh->n_edges(); size_t n_vertices = mesh->n_vertices(); @@ -51,7 +51,7 @@ void laplacian(che * mesh, a_sp_mat & L, a_sp_mat & A) A(v, v) = mesh->area_vertex(v); } -void laplacian(che * mesh, sp_mat_e & L, sp_mat_e & A) +void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) { gproshan_debug(LAPLACIAN); @@ -80,12 +80,14 @@ void laplacian(che * mesh, sp_mat_e & L, sp_mat_e & A) A.insert(v, v) = mesh->area_vertex(v); } -size_t eigs_laplacian(a_vec & eigval, a_mat & eigvec, che * mesh, const a_sp_mat & L, const a_sp_mat & A, const size_t & K) +size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) { gproshan_debug(LAPLACIAN); + + laplacian(mesh, L, A); - string feigval = tmp_file_path(mesh->name_size() + '_' + to_string(K) + ".L_eigval"); - string feigvec = tmp_file_path(mesh->name_size() + '_' + to_string(K) + ".L_eigvec"); + string feigval = tmp_file_path(mesh->name_size() + '_' + to_string(k) + ".L_eigval"); + string feigvec = tmp_file_path(mesh->name_size() + '_' + to_string(k) + ".L_eigvec"); gproshan_debug_var(feigval); gproshan_debug_var(feigvec); @@ -95,7 +97,7 @@ size_t eigs_laplacian(a_vec & eigval, a_mat & eigvec, che * mesh, const a_sp_mat // a_sp_mat D = sqrt(A); // D.for_each([](a_sp_mat::elem_type & val) { val = 1. / val; }); - if(!eigs_sym(eigval, eigvec, L, K, "sa")) + if(!eigs_sym(eigval, eigvec, L, k, "sa")) return 0; eigval.save(feigval); diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 6e3520bf..1fb93003 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -228,7 +228,7 @@ real_t che::area_trig(const index_t & t) const return *(a * b) / 2; } -real_t che::area_vertex(const index_t & v) +real_t che::area_vertex(const index_t & v) const { real_t area_star = 0; for_star(he, this, v) From 7a65975545535c871c80e6c24ca694d332957117 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 13 Feb 2021 20:07:09 +0100 Subject: [PATCH 0426/1018] rename shape to mesh --- include/features/descriptor.h | 1 - include/geodesics/dijkstra.h | 4 ++-- include/geodesics/sampling.h | 2 +- include/laplacian/fairing.h | 4 ++-- include/laplacian/fairing_spectral.h | 2 +- include/laplacian/fairing_taubin.h | 2 +- src/features/descriptor.cpp | 10 +--------- src/geodesics/dijkstra.cpp | 14 +++++++------- src/geodesics/sampling.cpp | 6 +++--- src/laplacian/fairing.cpp | 4 ++-- src/laplacian/fairing_spectral.cpp | 12 ++++++------ src/laplacian/fairing_taubin.cpp | 14 +++++++------- 12 files changed, 33 insertions(+), 42 deletions(-) diff --git a/include/features/descriptor.h b/include/features/descriptor.h index b725d0a2..4273ea48 100644 --- a/include/features/descriptor.h +++ b/include/features/descriptor.h @@ -23,7 +23,6 @@ class descriptor descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); private: - bool compute_eigs(const che * mesh, const size_t & n_eigs); }; diff --git a/include/geodesics/dijkstra.h b/include/geodesics/dijkstra.h index c3117a50..a1c1484b 100644 --- a/include/geodesics/dijkstra.h +++ b/include/geodesics/dijkstra.h @@ -17,14 +17,14 @@ class dijkstra index_t source; public: - dijkstra(che * shape, index_t src); + dijkstra(che * mesh, index_t src); ~dijkstra(); real_t & operator()(index_t i); index_t & operator[](index_t i); void print(std::ostream & os); private: - void run(che * shape); + void run(che * mesh); }; diff --git a/include/geodesics/sampling.h b/include/geodesics/sampling.h index 7cd41389..b3dddcf7 100644 --- a/include/geodesics/sampling.h +++ b/include/geodesics/sampling.h @@ -11,7 +11,7 @@ namespace gproshan { -index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, real_t radio); +index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio); bool load_sampling(std::vector & points, real_t & radio, che * mesh, size_t M); diff --git a/include/laplacian/fairing.h b/include/laplacian/fairing.h index 46d31946..5b86cc92 100644 --- a/include/laplacian/fairing.h +++ b/include/laplacian/fairing.h @@ -16,11 +16,11 @@ class fairing public: fairing(); virtual ~fairing(); - void run(che * shape); + void run(che * mesh); vertex * get_postions(); protected: - virtual void compute(che * shape) = 0; + virtual void compute(che * mesh) = 0; }; diff --git a/include/laplacian/fairing_spectral.h b/include/laplacian/fairing_spectral.h index 83ef034f..414c2747 100644 --- a/include/laplacian/fairing_spectral.h +++ b/include/laplacian/fairing_spectral.h @@ -18,7 +18,7 @@ class fairing_spectral : public fairing virtual ~fairing_spectral(); private: - void compute(che * shape); + void compute(che * mesh); }; diff --git a/include/laplacian/fairing_taubin.h b/include/laplacian/fairing_taubin.h index 74d540f6..26beb49c 100644 --- a/include/laplacian/fairing_taubin.h +++ b/include/laplacian/fairing_taubin.h @@ -18,7 +18,7 @@ class fairing_taubin : public fairing virtual ~fairing_taubin(); private: - void compute(che * shape); + void compute(che * mesh); }; diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 98068751..fedef998 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -8,18 +8,10 @@ namespace gproshan { descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n_eigs) { - if(!compute_eigs(mesh, n_eigs)) + if(eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs) == 0) return; } -bool descriptor::compute_eigs(const che * mesh, const size_t & k) -{ - if(eigs_laplacian(mesh, eigval, eigvec, L, A, k) == 0) - return false; - - -} - } // namespace gproshan diff --git a/src/geodesics/dijkstra.cpp b/src/geodesics/dijkstra.cpp index 3039a0d5..b173b70b 100644 --- a/src/geodesics/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -10,9 +10,9 @@ using namespace std; namespace gproshan { -dijkstra::dijkstra(che * shape, index_t src) +dijkstra::dijkstra(che * mesh, index_t src) { - n_vertices = shape->n_vertices(); + n_vertices = mesh->n_vertices(); source = src; weights = new real_t[n_vertices]; @@ -23,7 +23,7 @@ dijkstra::dijkstra(che * shape, index_t src) for(index_t i = 0; i < n_vertices; i++) weights[i] = INFINITY; - run(shape); + run(mesh); } dijkstra::~dijkstra() @@ -48,7 +48,7 @@ void dijkstra::print(ostream & os) os<link(link_he, v); + mesh->link(link_he, v); for(index_t he: link_he) { - nv = shape->vt(next(he)); + nv = mesh->vt(next(he)); if(visited[nv]) { - w = weights[nv] + *(shape->get_vertex(nv) - shape->get_vertex(v)); + w = weights[nv] + *(mesh->get_vertex(nv) - mesh->get_vertex(v)); if(w < weights[v]) weights[v] = w; diff --git a/src/geodesics/sampling.cpp b/src/geodesics/sampling.cpp index a3d18a66..4205cf64 100644 --- a/src/geodesics/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -12,7 +12,7 @@ using namespace std; namespace gproshan { -index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& normals, che * shape, size_t n_points, real_t radio) +index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio) { normals = new vertex[n_points]; sizes = new size_t[n_points]; @@ -25,9 +25,9 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n for(index_t i = 0; i < n_points; i++) { const index_t & v = points[i]; - normals[i] = shape->normal(v); + normals[i] = mesh->normal(v); - geodesics fm(shape, { v }, params); + geodesics fm(mesh, { v }, params); indexes[i] = new index_t[fm.n_sorted_index()]; diff --git a/src/laplacian/fairing.cpp b/src/laplacian/fairing.cpp index 68761a21..cd96a9e6 100644 --- a/src/laplacian/fairing.cpp +++ b/src/laplacian/fairing.cpp @@ -15,9 +15,9 @@ fairing::~fairing() if(positions) delete [] positions; } -void fairing::run(che * shape) +void fairing::run(che * mesh) { - compute(shape); + compute(mesh); } vertex * fairing::get_postions() diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index d6d5b657..78206f9d 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -16,23 +16,23 @@ fairing_spectral::~fairing_spectral() } -void fairing_spectral::compute(che * shape) +void fairing_spectral::compute(che * mesh) { double time; - positions = new vertex[shape->n_vertices()]; + positions = new vertex[mesh->n_vertices()]; - a_mat X((real_t *) positions, 3, shape->n_vertices(), false, true); + a_mat X((real_t *) positions, 3, mesh->n_vertices(), false, true); #pragma omp parallel for - for(index_t v = 0; v < shape->n_vertices(); v++) - positions[v] = shape->gt(v); + for(index_t v = 0; v < mesh->n_vertices(); v++) + positions[v] = mesh->gt(v); a_sp_mat L, A; a_vec eigval; a_mat eigvec; - TIC(time) k = eigs_laplacian(shape, eigval, eigvec, L, A, k); TOC(time) + TIC(time) k = eigs_laplacian(mesh, eigval, eigvec, L, A, k); TOC(time) gproshan_debug_var(time); X = X * eigvec * eigvec.t(); diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index d19e81b2..745a6244 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -16,13 +16,13 @@ fairing_taubin::~fairing_taubin() } -void fairing_taubin::compute(che * shape) +void fairing_taubin::compute(che * mesh) { double time; /* a_sp_mat_e Le, Ae; TIC(time) - laplacian(shape, Le, Ae); + laplacian(mesh, Le, Ae); TOC(time) cout<<"time laplacian: "<n_vertices()]; + positions = new vertex[mesh->n_vertices()]; - a_mat X((real_t *) positions, 3, shape->n_vertices(), false, true); + a_mat X((real_t *) positions, 3, mesh->n_vertices(), false, true); #pragma omp parallel for - for(index_t v = 0; v < shape->n_vertices(); v++) - positions[v] = shape->gt(v); + for(index_t v = 0; v < mesh->n_vertices(); v++) + positions[v] = mesh->gt(v); a_mat R; a_mat AX = A * X.t(); From 5094de1b500fdad214735e265fb5101f8a33b170 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 13 Feb 2021 23:50:57 +0100 Subject: [PATCH 0427/1018] add compute_gps (global point signature) --- include/features/descriptor.h | 6 ++++ src/app_viewer.cpp | 61 ++++++++++++++++++----------------- src/features/descriptor.cpp | 36 ++++++++++++++++++++- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/include/features/descriptor.h b/include/features/descriptor.h index 4273ea48..285e4f93 100644 --- a/include/features/descriptor.h +++ b/include/features/descriptor.h @@ -18,11 +18,17 @@ class descriptor a_sp_mat L, A; a_vec eigval; a_mat eigvec; + a_mat features; public: descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); + operator bool () const; ///< return true if the features were computed + real_t operator () (const index_t & v) const; private: + void compute_gps(const size_t & t); + void compute_hks(const size_t & t); + void compute_wks(const size_t & t); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 90e6d5dd..4f52ac63 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -312,19 +312,25 @@ bool app_viewer::process_wks(viewer * p_view) static int K = 100; static int T = 100; + static bool status = true; ImGui::InputInt("eigenvectors", &K); ImGui::InputInt("times", &T); + if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing WKS."); if(ImGui::Button("Run")) { - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; - - TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + descriptor wks(descriptor::WKS, mesh, K); + + if(wks) + { + status = true; + + } + else status = false; + +/* real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices(); v++) @@ -340,7 +346,8 @@ bool app_viewer::process_wks(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->heatmap(v) /= max_s; + mesh->heatmap(v) /= max_s;i + */ } return true; @@ -396,36 +403,30 @@ bool app_viewer::process_gps(viewer * p_view) che_viewer & mesh = view->active_mesh(); static int K = 50; + static bool status = true; ImGui::InputInt("eigenvectors", &K); if(ImGui::Button("Run")) { - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; - - TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) - gproshan_log_var(view->time); - - eigvec = abs(eigvec); - eigvec.col(0).zeros(); - for(int i = 1; i < K; i++) - eigvec.col(i) /= sqrt(abs(eigval(i))); - - a_mat data = eigvec.t(); - a_mat means; - - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) + descriptor GPS(descriptor::GPS, mesh, K); + + if(GPS) { - mesh->heatmap(v) = norm(eigvec.row(v)); + status = true; + + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices(); v++) + { + mesh->heatmap(v) = GPS(v); max_s = max(max_s, mesh->heatmap(v)); - } + } - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) - mesh->heatmap(v) /= max_s; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices(); v++) + mesh->heatmap(v) /= max_s; + } + else status = false; } return true; diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index fedef998..df6dceb9 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -8,8 +8,42 @@ namespace gproshan { descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n_eigs) { - if(eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs) == 0) + if(!eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs)) return; + + switch(sig) + { + case GPS: compute_gps(1); break; + case HKS: break; + case WKS: break; + } +} + +descriptor::operator bool () const +{ + return features.size() > 0; +} + +real_t descriptor::operator () (const index_t & v) const +{ + return norm(features.row(v)); +} + +void descriptor::compute_gps(const size_t & T) +{ + features = eigvec.tail_cols(eigvec.n_cols - 1); + for(index_t i = 1; i < eigval.size(); i++) + features.col(i - 1) /= sqrt(eigval(i)); +} + +void descriptor::compute_hks(const size_t & T) +{ + features.zeros(); +} + +void descriptor::compute_wks(const size_t & T) +{ + features.zeros(); } From a835035eb130e544e86032acbc8c4c0ccc916ef6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Feb 2021 00:35:07 +0100 Subject: [PATCH 0428/1018] refactoring n_vertices, n_faces member che class --- include/mesh/che.h | 18 +- src/app_viewer.cpp | 60 +-- src/features/key_components.cpp | 2 +- src/features/key_points.cpp | 4 +- src/geodesics/dijkstra.cpp | 2 +- src/geodesics/geodesics.cpp | 2 +- src/geodesics/geodesics_ptp.cpp | 24 +- src/geodesics/geodesics_ptp.cu | 4 +- src/geodesics/heat_method.cpp | 20 +- src/geodesics/test_geodesics_ptp.cpp | 18 +- src/geodesics/test_geodesics_ptp.cu | 4 +- .../test_geodesics_ptp_coalescence.cu | 44 +- src/laplacian/fairing_spectral.cpp | 6 +- src/laplacian/fairing_taubin.cpp | 6 +- src/laplacian/laplacian.cpp | 8 +- src/mdict/msparse_coding.cpp | 62 +-- src/mdict/patch.cpp | 16 +- src/mesh/che.cpp | 502 +++++++++--------- src/mesh/che_fill_hole.cpp | 18 +- src/mesh/che_obj.cpp | 8 +- src/mesh/che_off.cpp | 12 +- src/mesh/che_ply.cpp | 12 +- src/mesh/che_poisson.cpp | 12 +- src/mesh/che_ptx.cpp | 8 +- src/mesh/simplification.cpp | 14 +- src/raytracing/rt_embree.cpp | 18 +- src/raytracing/rt_embree_splat.cpp | 2 +- src/raytracing/rt_optix.cpp | 20 +- src/viewer/che_viewer.cpp | 32 +- src/viewer/viewer.cpp | 2 +- 30 files changed, 468 insertions(+), 492 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index 12b04133..3a5c25d4 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -18,6 +18,7 @@ namespace gproshan { typedef std::vector star_t; // star (vector of he) typedef std::vector link_t; // link (vector of he) +size_t & rw(const size_t & n); index_t trig(const index_t & he); index_t next(const index_t & he); index_t prev(const index_t & he); @@ -31,15 +32,15 @@ class che public: enum mesh_type { mtrig = 3, mquad = 4 }; ///< meshes_types + const size_t n_vertices = 0; + const size_t n_faces = 0; + const size_t n_half_edges = 0; + const size_t n_edges = 0; + const size_t n_borders = 0; + protected: std::string filename_; - size_t n_vertices_ = 0; - size_t n_faces_ = 0; - size_t n_half_edges_ = 0; - size_t n_edges_ = 0; - size_t n_borders_ = 0; - vertex * GT = nullptr; ///< geometry table : v -> vertex index_t * VT = nullptr; ///< vertex table (faces) : he -> v index_t * OT = nullptr; ///< opposite table : he -> he @@ -108,11 +109,6 @@ class che const index_t & ot_evt(const index_t & v) const; const index_t & evt(const index_t & v) const; const index_t & bt(const index_t & b) const; - const size_t & n_vertices() const; - const size_t & n_faces() const; - const size_t & n_half_edges() const; - const size_t & n_edges() const; - const size_t & n_borders() const; size_t max_degree() const; vertex & get_vertex(index_t v); void set_vertices(const vertex *const& positions, size_t n = 0, const index_t & v_i = 0); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 4f52ac63..6a0d1447 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -119,12 +119,12 @@ bool paint_holes_vertices(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - size_t nv = mesh->n_vertices(); + size_t nv = mesh->n_vertices; mesh.update(); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) if(v >= nv) mesh->heatmap(v) = .25; return false; @@ -164,7 +164,7 @@ bool app_viewer::process_poisson(viewer * p_view, const index_t & k) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - size_t old_n_vertices = mesh->n_vertices(); + size_t old_n_vertices = mesh->n_vertices; delete [] fill_all_holes(mesh); TIC(view->time) poisson(mesh, old_n_vertices, k); TOC(view->time) @@ -218,7 +218,7 @@ bool app_viewer::process_noise(viewer * p_view) std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); @@ -240,7 +240,7 @@ bool app_viewer::process_black_noise(viewer * p_view) std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); @@ -257,7 +257,7 @@ bool app_viewer::process_threshold(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; return false; @@ -293,7 +293,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) eigvec.col(k) /= eigvec.col(k).max(); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) view->active_mesh()->heatmap(v) = eigvec(v, k); view->active_mesh().update_vbo(); @@ -333,7 +333,7 @@ bool app_viewer::process_wks(viewer * p_view) /* real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { a_vec s(T, arma::fill::zeros); for(int t = 0; t < T; t++) @@ -345,7 +345,7 @@ bool app_viewer::process_wks(viewer * p_view) } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) /= max_s;i */ } @@ -377,7 +377,7 @@ bool app_viewer::process_hks(viewer * p_view) real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { a_vec s(T, arma::fill::zeros); for(int t = 0; t < T; t++) @@ -390,7 +390,7 @@ bool app_viewer::process_hks(viewer * p_view) } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) /= max_s; } @@ -416,14 +416,14 @@ bool app_viewer::process_gps(viewer * p_view) real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { mesh->heatmap(v) = GPS(v); max_s = max(max_s, mesh->heatmap(v)); } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) /= max_s; } else status = false; @@ -461,7 +461,7 @@ bool app_viewer::process_key_components(viewer * p_view) gproshan_debug_var(kcs); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) = (real_t) kcs(v) / kcs; return false; @@ -474,7 +474,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) che_viewer & mesh = view->active_mesh(); TIC(view->time) - index_t * toplevel = new index_t[mesh->n_vertices()]; + index_t * toplevel = new index_t[mesh->n_vertices]; size_t avg_nvp = 0; vertex vdir; @@ -650,15 +650,15 @@ bool app_viewer::compute_toplesets(viewer * p_view) if(!mesh.selected.size()) mesh.selected.push_back(0); - index_t * toplesets = new index_t[mesh->n_vertices()]; - index_t * sorted = new index_t[mesh->n_vertices()]; + index_t * toplesets = new index_t[mesh->n_vertices]; + index_t * sorted = new index_t[mesh->n_vertices]; vector limites; mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); size_t n_toplesets = limites.size() - 1; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { if(toplesets[v] < n_toplesets) mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); @@ -692,7 +692,7 @@ bool app_viewer::process_voronoi(viewer * p_view) gproshan_log_var(view->time); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { mesh->heatmap(i) = ptp.clusters[i]; mesh->heatmap(i) /= mesh.selected.size() + 1; @@ -735,7 +735,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) static int n = 10; static real_t radio; - ImGui::SliderInt("samples", &n, 1, mesh->n_vertices() / 6); + ImGui::SliderInt("samples", &n, 1, mesh->n_vertices / 6); ImGui::Text("radio: %.3f", radio); if(ImGui::Button("Run")) @@ -755,7 +755,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) che_viewer & mesh = view->active_mesh(); static int k = 100; - ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices() / 6); + ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices / 6); if(ImGui::Button("Run")) { @@ -800,8 +800,8 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & static vector dist; - if(dist.size() != mesh->n_vertices()) - dist.resize(mesh->n_vertices()); + if(dist.size() != mesh->n_vertices) + dist.resize(mesh->n_vertices); geodesics::params params; params.alg = alg; @@ -864,8 +864,8 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - size_t old_n_vertices, n_vertices = mesh->n_vertices(); - size_t n_holes = mesh->n_borders(); + size_t old_n_vertices, n_vertices = mesh->n_vertices; + size_t n_holes = mesh->n_borders; vector * border_vertices; che ** holes; @@ -878,7 +878,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) if(holes[h]) { old_n_vertices = n_vertices; - biharmonic_interp_2(mesh, old_n_vertices, n_vertices += holes[h]->n_vertices() - border_vertices[h].size(), border_vertices[h], k); + biharmonic_interp_2(mesh, old_n_vertices, n_vertices += holes[h]->n_vertices - border_vertices[h].size(), border_vertices[h], k); delete holes[h]; } @@ -898,10 +898,10 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; - a_vec gv(mesh->n_vertices()); + a_vec gv(mesh->n_vertices); #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { g = 0; for_star(he, mesh, v) @@ -923,7 +923,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) gproshan_log_var(g_max); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) gv(v) = (gv(v) + g_min) / g; real_t gm = mean(gv); @@ -940,7 +940,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) }; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->heatmap(v) = f(gv(v)); return false; diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index f458546b..e64327c4 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -13,7 +13,7 @@ namespace gproshan { key_components::key_components(che * mesh, const key_points & kps, const real_t & r): radio(r) { - n_vertices = mesh->n_vertices(); + n_vertices = mesh->n_vertices; comp = new index_t[n_vertices]; comp_size = new size_t[n_vertices]; diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 775d7d76..5312260e 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -13,8 +13,8 @@ namespace gproshan { key_points::key_points(che * mesh, const real_t & percent) { - n_faces = mesh->n_faces(); - n_vertices = mesh->n_vertices(); + n_faces = mesh->n_faces; + n_vertices = mesh->n_vertices; face_areas = new real_idx_t[n_faces]; diff --git a/src/geodesics/dijkstra.cpp b/src/geodesics/dijkstra.cpp index b173b70b..57ad57e9 100644 --- a/src/geodesics/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -12,7 +12,7 @@ namespace gproshan { dijkstra::dijkstra(che * mesh, index_t src) { - n_vertices = mesh->n_vertices(); + n_vertices = mesh->n_vertices; source = src; weights = new real_t[n_vertices]; diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 486de3a2..270f6c2c 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -13,7 +13,7 @@ using namespace std; namespace gproshan { -geodesics::geodesics(che * mesh, const vector & sources, const params & p): n_vertices(mesh->n_vertices()) +geodesics::geodesics(che * mesh, const vector & sources, const params & p): n_vertices(mesh->n_vertices) { assert(n_vertices > 0); diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index dbf131e6..df451965 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -19,10 +19,10 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top vector V(toplesets.limits.back()); vector F; - F.reserve(mesh->n_half_edges()); + F.reserve(mesh->n_half_edges); - inv = !inv ? new index_t[mesh->n_vertices()] : inv; - memset(inv, -1, sizeof(index_t) * mesh->n_vertices()); + inv = !inv ? new index_t[mesh->n_vertices] : inv; + memset(inv, -1, sizeof(index_t) * mesh->n_vertices); #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); i++) @@ -31,7 +31,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top inv[toplesets.index[i]] = i; } - for(index_t he = 0; he < mesh->n_half_edges(); he++) + for(index_t he = 0; he < mesh->n_half_edges; he++) if(inv[mesh->vt(he)] != NIL && inv[mesh->vt(prev(he))] != NIL && inv[mesh->vt(next(he))] != NIL) F.push_back(inv[mesh->vt(he)]); @@ -40,17 +40,17 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) { - const size_t n_vertices = mesh->n_vertices(); + const size_t n_vertices = mesh->n_vertices; index_t * inv = nullptr; mesh = ptp_coalescence(inv, mesh, toplesets); // ------------------------------------------------------ - real_t * pdist[2] = {new real_t[mesh->n_vertices()], new real_t[mesh->n_vertices()]}; - real_t * error = new real_t[mesh->n_vertices()]; + real_t * pdist[2] = {new real_t[mesh->n_vertices], new real_t[mesh->n_vertices]}; + real_t * error = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) pdist[0][v] = pdist[1][v] = INFINITY; for(index_t i = 0; i < sources.size(); i++) @@ -122,11 +122,11 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) { - real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices()]}; - real_t * error = new real_t[mesh->n_vertices()]; + real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices]}; + real_t * error = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) pdist[0][v] = pdist[1][v] = INFINITY; for(index_t i = 0; i < sources.size(); i++) @@ -193,7 +193,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c if(ptp_out.dist != pdist[!d]) { - memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices() * sizeof(real_t)); + memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices * sizeof(real_t)); delete [] pdist[!d]; } else delete [] pdist[d]; diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index d7c49cce..3a320d46 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -136,9 +136,9 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do // 1 indexing #ifdef SINGLE_P - cublasIsamax(handle, mesh->n_vertices(), d_dist[d], 1, &f); + cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #else - cublasIdamax(handle, mesh->n_vertices(), d_dist[d], 1, &f); + cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #endif if(radio > 0 || !n) diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 9c7870c0..1a6d0b3b 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -16,7 +16,7 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) if(!sources.size()) return 0; // build impulse signal - a_mat u0(mesh->n_vertices(), 1, arma::fill::zeros); + a_mat u0(mesh->n_vertices, 1, arma::fill::zeros); for(auto & v: sources) u0(v) = 1; // step @@ -31,7 +31,7 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) // heat flow for short interval A += dt * L; - a_mat u(mesh->n_vertices(), 1); + a_mat u(mesh->n_vertices, 1); cholmod_common context; cholmod_l_start(&context); @@ -43,10 +43,10 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) // extract geodesics - a_mat div(mesh->n_vertices(), 1); + a_mat div(mesh->n_vertices, 1); compute_divergence(mesh, u, div); - a_mat phi(dist, mesh->n_vertices(), 1, false); + a_mat phi(dist, mesh->n_vertices, 1, false); solve_time += solve_positive_definite(phi, L, div, &context); // cholmod (suitesparse) //assert(spsolve(phi, L, div)); // arma @@ -67,7 +67,7 @@ real_t * heat_method_gpu(che * mesh, const vector & sources, double & s if(!sources.size()) return 0; // build impulse signal - a_mat u0(mesh->n_vertices(), 1, arma::fill::zeros); + a_mat u0(mesh->n_vertices, 1, arma::fill::zeros); for(auto & v: sources) u0(v) = 1; // step @@ -82,19 +82,19 @@ real_t * heat_method_gpu(che * mesh, const vector & sources, double & s // heat flow for short interval A += dt * L; - a_mat u(mesh->n_vertices(), 1); + a_mat u(mesh->n_vertices, 1); solve_time = 0; solve_time += solve_positive_definite_gpu(u, A, u0); // cusorlver (cusparse) // extract geodesics - real_t * dist = new real_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices]; - a_mat div(mesh->n_vertices(), 1); + a_mat div(mesh->n_vertices, 1); compute_divergence(mesh, u, div); - a_mat phi(dist, mesh->n_vertices(), 1, false); + a_mat phi(dist, mesh->n_vertices, 1, false); solve_time += solve_positive_definite_gpu(phi, L, div); // cusolver (cusparse) @@ -109,7 +109,7 @@ real_t * heat_method_gpu(che * mesh, const vector & sources, double & s void compute_divergence(che * mesh, const a_mat & u, a_mat & div) { - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { real_t & sum = div(v); diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index f23e3a43..1f31dd4e 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -47,7 +47,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) vector source = { 0 }; che * mesh = new che_off(data_path + filename + ".off"); - size_t n_vertices = mesh->n_vertices(); + size_t n_vertices = mesh->n_vertices; index_t * toplesets = new index_t[n_vertices]; index_t * sorted_index = new index_t[n_vertices]; @@ -251,7 +251,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons geodesics fm(mesh, source); - error = compute_error(&fm[0], exact, mesh->n_vertices(), source.size()); + error = compute_error(&fm[0], exact, mesh->n_vertices, source.size()); return seconds; } @@ -260,14 +260,14 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vect { double t, seconds = INFINITY; - real_t * dist = new real_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; i++) { TIC(t) parallel_toplesets_propagation_cpu(dist, mesh, source, toplesets); TOC(t) seconds = min(seconds, t); } - error = compute_error(dist, exact, mesh->n_vertices(), source.size()); + error = compute_error(dist, exact, mesh->n_vertices, source.size()); delete [] dist; @@ -279,7 +279,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e double t, st, ptime; ptime = stime = INFINITY; - real_t * dist = new real_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; i++) { TIC(t) st = heat_method(dist, mesh, source); TOC(t) @@ -287,7 +287,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e stime = min(st, stime); } - error = compute_error(dist, exact, mesh->n_vertices(), source.size()); + error = compute_error(dist, exact, mesh->n_vertices, source.size()); delete [] dist; @@ -301,14 +301,14 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vect { double t, seconds = INFINITY; - real_t * dist = new real_t[mesh->n_vertices()]; + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; i++) { t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); seconds = min(seconds, t); } - error = compute_error(dist, exact, mesh->n_vertices(), source.size()); + error = compute_error(dist, exact, mesh->n_vertices, source.size()); delete [] dist; @@ -331,7 +331,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact stime = min(st, stime); } - error = compute_error(dist, exact, mesh->n_vertices(), source.size()); + error = compute_error(dist, exact, mesh->n_vertices, source.size()); delete [] dist; diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index 62aaa31f..2dcb29af 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -124,9 +124,9 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam // 1 indexing #ifdef SINGLE_P - cublasIsamax(handle, mesh->n_vertices(), d_dist[d], 1, &f); + cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #else - cublasIdamax(handle, mesh->n_vertices(), d_dist[d], 1, &f); + cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #endif cudaEventRecord(stop, 0); diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 6bd4d6e2..1d4c8357 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -24,13 +24,13 @@ vector > iter_error_parallel_toplesets_propagation_coalesc { // sort data by levels, must be improve the coalescence - vertex * V = new vertex[mesh->n_vertices()]; - index_t * F = new index_t[mesh->n_faces() * che::mtrig]; - index_t * inv = new index_t[mesh->n_vertices()]; - real_t * exact_dist_sorted = new real_t[mesh->n_vertices()]; + vertex * V = new vertex[mesh->n_vertices]; + index_t * F = new index_t[mesh->n_faces * che::mtrig]; + index_t * inv = new index_t[mesh->n_vertices]; + real_t * exact_dist_sorted = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { V[i] = mesh->gt(sorted_index[i]); inv[sorted_index[i]] = i; @@ -38,10 +38,10 @@ vector > iter_error_parallel_toplesets_propagation_coalesc } #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges(); he++) + for(index_t he = 0; he < mesh->n_half_edges; he++) F[he] = inv[mesh->vt(he)]; - mesh = new che(V, mesh->n_vertices(), F, mesh->n_faces()); + mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); delete [] V; delete [] F; @@ -106,28 +106,28 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices()]; - index_t * F = new index_t[mesh->n_faces() * che::mtrig]; - index_t * inv = new index_t[mesh->n_vertices()]; + vertex * V = new vertex[mesh->n_vertices]; + index_t * F = new index_t[mesh->n_faces * che::mtrig]; + index_t * inv = new index_t[mesh->n_vertices]; - real_t * h_dist = new real_t[mesh->n_vertices()]; + real_t * h_dist = new real_t[mesh->n_vertices]; real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * mesh->n_vertices()); - cudaMalloc(&d_dist[1], sizeof(real_t) * mesh->n_vertices()); + cudaMalloc(&d_dist[0], sizeof(real_t) * mesh->n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * mesh->n_vertices); real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * mesh->n_vertices()); + cudaMalloc(&d_error, sizeof(real_t) * mesh->n_vertices); vector limits; - index_t * toplesets = new index_t[mesh->n_vertices()]; - index_t * sorted_index = new index_t[mesh->n_vertices()]; + index_t * toplesets = new index_t[mesh->n_vertices]; + index_t * sorted_index = new index_t[mesh->n_vertices]; cublasHandle_t handle; cublasCreate(&handle); - if(n >= mesh->n_vertices()) n = mesh->n_vertices() >> 1; + if(n >= mesh->n_vertices) n = mesh->n_vertices >> 1; double * times = new double[n + 1]; @@ -148,17 +148,17 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { V[i] = mesh->gt(sorted_index[i]); inv[sorted_index[i]] = i; } #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges(); he++) + for(index_t he = 0; he < mesh->n_half_edges; he++) F[he] = inv[mesh->vt(he)]; - che * tmp_mesh = new che(V, mesh->n_vertices(), F, mesh->n_faces()); + che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); CHE * h_mesh = new CHE(tmp_mesh); CHE * dd_mesh, * d_mesh; @@ -173,9 +173,9 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices(), d_dist[d], 1, &f); + cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #else - cublasIdamax(handle, mesh->n_vertices(), d_dist[d], 1, &f); + cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #endif cudaEventRecord(stop, 0); diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index 78206f9d..ec96969a 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -20,12 +20,12 @@ void fairing_spectral::compute(che * mesh) { double time; - positions = new vertex[mesh->n_vertices()]; + positions = new vertex[mesh->n_vertices]; - a_mat X((real_t *) positions, 3, mesh->n_vertices(), false, true); + a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) positions[v] = mesh->gt(v); a_sp_mat L, A; diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index 745a6244..62eb096b 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -33,12 +33,12 @@ void fairing_taubin::compute(che * mesh) TIC(time) laplacian(mesh, L, A); TOC(time) gproshan_debug_var(time); - positions = new vertex[mesh->n_vertices()]; + positions = new vertex[mesh->n_vertices]; - a_mat X((real_t *) positions, 3, mesh->n_vertices(), false, true); + a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) positions[v] = mesh->gt(v); a_mat R; diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 6660fd75..2ba0d508 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -10,8 +10,8 @@ namespace gproshan { void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) { - size_t n_edges = mesh->n_edges(); - size_t n_vertices = mesh->n_vertices(); + size_t n_edges = mesh->n_edges; + size_t n_vertices = mesh->n_vertices; arma::umat DI(2, 2 * n_edges); a_vec DV(2 * n_edges); @@ -55,8 +55,8 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) { gproshan_debug(LAPLACIAN); - size_t n_edges = mesh->n_edges(); - size_t n_vertices = mesh->n_vertices(); + size_t n_edges = mesh->n_edges; + size_t n_vertices = mesh->n_vertices; sp_mat_e D(n_edges, n_vertices); sp_mat_e S(n_edges, n_edges); diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 099d96b4..df59abdb 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -47,14 +47,14 @@ size_t msparse_coding::T = 5; msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) { A.eye(phi_basis->dim(), m_params.n_atoms); - dist = new real_t[mesh->n_vertices()]; + dist = new real_t[mesh->n_vertices]; - m_params.n_patches = mesh->n_vertices() / m_params.avg_p; + m_params.n_patches = mesh->n_vertices / m_params.avg_p; key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); - mask = new bool[mesh->n_vertices()]; - memset(mask, 0, sizeof(bool) * mesh->n_vertices()); + mask = new bool[mesh->n_vertices]; + memset(mask, 0, sizeof(bool) * mesh->n_vertices); } msparse_coding::~msparse_coding() @@ -78,17 +78,17 @@ void msparse_coding::load_mask() if(V.load(f_mask)) { #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { mask[i] = V(i); } } else { - V.zeros(mesh->n_vertices()); + V.zeros(mesh->n_vertices); std::default_random_engine generator; - std::uniform_int_distribution distribution(0, mesh->n_vertices()-1); - size_t percentage = mesh->n_vertices() - ceil(mesh->n_vertices() * (m_params.percent / 100.0)) ; + std::uniform_int_distribution distribution(0, mesh->n_vertices-1); + size_t percentage = mesh->n_vertices - ceil(mesh->n_vertices * (m_params.percent / 100.0)) ; size_t k = 0; size_t rn = 0; @@ -114,7 +114,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde if(V.load(f_mask)) { #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { mask[i] = V(i); } @@ -123,7 +123,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde else { - V.zeros(mesh->n_vertices()); + V.zeros(mesh->n_vertices); size_t * percentages_size = new size_t[m_params.n_patches]; bool cover_cluster[m_params.n_patches]; @@ -207,13 +207,13 @@ void msparse_coding::load_sampling(bool save_all) gproshan_debug(computing sampling); // compute features will be seeds - patches_map.resize(mesh->n_vertices()); + patches_map.resize(mesh->n_vertices); //Coverage of the points - bool covered[mesh->n_vertices()]; + bool covered[mesh->n_vertices]; #pragma omp for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) covered[i] = 0; real_t euc_radio; @@ -223,8 +223,8 @@ void msparse_coding::load_sampling(bool save_all) size_t count_cov = 0; size_t count_cov_patch = 0; - bool * invalid_seed = new bool[mesh->n_vertices()]; - memset(invalid_seed, 0, mesh->n_vertices() * sizeof(bool)); + bool * invalid_seed = new bool[mesh->n_vertices]; + memset(invalid_seed, 0, mesh->n_vertices * sizeof(bool)); for(const index_t & vsf: all_sorted_features) { @@ -289,7 +289,7 @@ void msparse_coding::load_sampling(bool save_all) vector outliers; - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) if(!covered[i]) outliers.push_back(i); @@ -331,7 +331,7 @@ void msparse_coding::init_radial_feature_patches() gproshan_log(initializing patches); gproshan_debug_var(m_params.n_patches); - n_vertices = mesh->n_vertices(); + n_vertices = mesh->n_vertices; patches.resize(m_params.n_patches); patches_map.resize(n_vertices); @@ -417,7 +417,7 @@ void msparse_coding::init_voronoi_patches() vertices[s].push_back(sample(s)); } - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { ptp.clusters[i]--; if(sample(ptp.clusters[i]) != i) @@ -435,7 +435,7 @@ void msparse_coding::init_voronoi_patches() //initializing patch #pragma omp parallel { - index_t * toplevel = new index_t[mesh->n_vertices()]; + index_t * toplevel = new index_t[mesh->n_vertices]; #pragma omp for for(index_t s = 0; s < m_params.n_patches; s++) @@ -534,7 +534,7 @@ real_t msparse_coding::execute() arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); - real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices()); + real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices); gproshan_log_var(ratio); return max_error; @@ -645,7 +645,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) real_t msparse_coding::execute_tmp() { // fill holes - size_t threshold = mesh->n_vertices(); + size_t threshold = mesh->n_vertices; delete [] fill_all_holes(mesh); TIC(d_time) poisson(mesh, threshold, 2); TOC(d_time) gproshan_debug_var(d_time); @@ -740,12 +740,12 @@ void msparse_coding::init_sampling() { gproshan_log(MDICT); - n_vertices = mesh->n_vertices(); + n_vertices = mesh->n_vertices; // load sampling if(m_params.n_patches == 0) { - m_params.n_patches = mesh->n_vertices(); + m_params.n_patches = mesh->n_vertices; phi_basis->radio() = mesh->mean_edge(); } else @@ -898,9 +898,9 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) { gproshan_log(MDICT); - assert(n_vertices == mesh->n_vertices()); + assert(n_vertices == mesh->n_vertices); - a_mat V(3, mesh->n_vertices(), arma::fill::zeros); + a_mat V(3, mesh->n_vertices, arma::fill::zeros); patches_error.resize(m_params.n_patches); @@ -936,7 +936,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { // simple means vertex if(patches_map[v].size() && (!mask || mask(v))) @@ -963,20 +963,20 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) real_t max_error = -1; #pragma omp parallel for reduction(+: error) reduction(max: max_error) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); error += dist[v]; max_error = max(max_error, dist[v]); } - error /= mesh->n_vertices(); + error /= mesh->n_vertices; - gproshan_debug_var(mesh->n_vertices()); + gproshan_debug_var(mesh->n_vertices); gproshan_debug_var(error); gproshan_debug_var(max_error); - mesh->set_vertices(new_vertices, mesh->n_vertices()); + mesh->set_vertices(new_vertices, mesh->n_vertices); che_off::write_file(mesh, "../tmp/recon_mesh"); return max_error; @@ -1034,7 +1034,7 @@ index_t msparse_coding::sample(const index_t & s) const real_t & msparse_coding::operator[](const index_t & i) const { - assert(i < mesh->n_vertices()); + assert(i < mesh->n_vertices); return dist[i]; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index b5e0215c..53e23f1a 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -36,7 +36,7 @@ real_t patch::nyquist_factor = 0.5; void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) { radio = radio_; - index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; + index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); @@ -48,7 +48,7 @@ void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, cons void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { radio = 1; - index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices()]; + index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); @@ -98,9 +98,9 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N vb = mesh->gt(b); vv = mesh->gt(v); // If is an adjacent face - assert(a < mesh->n_vertices()); - assert(b < mesh->n_vertices()); - assert(v < mesh->n_vertices()); + assert(a < mesh->n_vertices); + assert(b < mesh->n_vertices); + assert(v < mesh->n_vertices); if(geo[a] < geo[v] || geo[b] < geo[v] ) { @@ -259,7 +259,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, vertex c = mesh->get_vertex(v); geodesics::params params; - params.dist_alloc = new real_t[mesh->n_vertices()]; + params.dist_alloc = new real_t[mesh->n_vertices]; params.fun = [&](const index_t & u) -> bool { if(u == v) return true; @@ -536,7 +536,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl if(vertices.size()) vertices.clear(); vertices.reserve(expected_nv); - memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices()); + memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); link_t link; toplevel[v] = 0; @@ -572,7 +572,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, priority_queue > qvertices; - memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices()); + memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); a_vec p(3); link_t link; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 1fb93003..7a212a52 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -15,6 +15,11 @@ using namespace std; namespace gproshan { +size_t & rw(const size_t & n) +{ + return const_cast(n); +} + index_t trig(const index_t & he) { if(he == NIL) return NIL; @@ -35,9 +40,9 @@ index_t prev(const index_t & he) CHE::CHE(che * mesh) { - n_vertices = mesh->n_vertices_; - n_faces = mesh->n_faces_; - n_half_edges = mesh->n_half_edges_; + n_vertices = mesh->n_vertices; + n_faces = mesh->n_faces; + n_half_edges = mesh->n_half_edges; GT = (vertex_cu *) mesh->GT; VT = mesh->VT; @@ -47,42 +52,42 @@ CHE::CHE(che * mesh) che::che(const che & mesh) { - filename_ = mesh.filename_; - n_vertices_ = mesh.n_vertices_; - n_faces_ = mesh.n_faces_; - n_half_edges_ = mesh.n_half_edges_; - n_edges_ = mesh.n_edges_; - n_borders_ = mesh.n_borders_; + filename_ = mesh.filename_; + rw(n_vertices) = mesh.n_vertices; + rw(n_faces) = mesh.n_faces; + rw(n_half_edges) = mesh.n_half_edges; + rw(n_edges) = mesh.n_edges; + rw(n_borders) = mesh.n_borders; - GT = new vertex[n_vertices_]; - memcpy(GT, mesh.GT, n_vertices_ * sizeof(vertex)); + GT = new vertex[n_vertices]; + memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); - VT = new index_t[n_half_edges_]; - memcpy(VT, mesh.VT, n_half_edges_ * sizeof(index_t)); + VT = new index_t[n_half_edges]; + memcpy(VT, mesh.VT, n_half_edges * sizeof(index_t)); - OT = new index_t[n_half_edges_]; - memcpy(OT, mesh.OT, n_half_edges_ * sizeof(index_t)); + OT = new index_t[n_half_edges]; + memcpy(OT, mesh.OT, n_half_edges * sizeof(index_t)); - EVT = new index_t[n_vertices_]; - memcpy(EVT, mesh.EVT, n_vertices_ * sizeof(index_t)); + EVT = new index_t[n_vertices]; + memcpy(EVT, mesh.EVT, n_vertices * sizeof(index_t)); - ET = new index_t[n_edges_]; - memcpy(ET, mesh.ET, n_edges_ * sizeof(index_t)); + ET = new index_t[n_edges]; + memcpy(ET, mesh.ET, n_edges * sizeof(index_t)); - EHT = new index_t[n_half_edges_]; - memcpy(EHT, mesh.EHT, n_half_edges_ * sizeof(index_t)); + EHT = new index_t[n_half_edges]; + memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); - BT = new index_t[n_borders_]; - memcpy(BT, mesh.BT, n_borders_ * sizeof(index_t)); + BT = new index_t[n_borders]; + memcpy(BT, mesh.BT, n_borders * sizeof(index_t)); - VN = new vertex[n_vertices_]; - memcpy(VN, mesh.VN, n_vertices_ * sizeof(vertex)); + VN = new vertex[n_vertices]; + memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); - VC = new vertex[n_vertices_]; - memcpy(VC, mesh.VC, n_vertices_ * sizeof(vertex)); + VC = new vertex[n_vertices]; + memcpy(VC, mesh.VC, n_vertices * sizeof(vertex)); - VHC = new real_t[n_vertices_]; - memcpy(VHC, mesh.VHC, n_vertices_ * sizeof(real_t)); + VHC = new real_t[n_vertices]; + memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); } che::che(const size_t & n_v, const size_t & n_f) @@ -102,7 +107,7 @@ che::~che() void che::star(star_t & s, const index_t & v) { - if(v >= n_vertices_) return; + if(v >= n_vertices) return; for_star(he, this, v) s.push_back(he); @@ -110,7 +115,7 @@ void che::star(star_t & s, const index_t & v) void che::link(link_t & l, const index_t & v) { - if(v >= n_vertices_) return; + if(v >= n_vertices) return; for_star(he, this, v) { @@ -128,7 +133,7 @@ void che::border(vector & border, const index_t & b) bool che::is_border_v(const index_t & v) const { - assert(EVT[v] < n_half_edges_); + assert(EVT[v] < n_half_edges); return OT[EVT[v]] == NIL; } @@ -213,10 +218,10 @@ real_t che::quality() real_t q = 0; #pragma omp parallel for reduction(+: q) - for(index_t t = 0; t < n_faces_; t++) + for(index_t t = 0; t < n_faces; t++) q += pdetriq(t) > 0.6; // is confederating good triangle - return q * 100 / n_faces_; + return q * 100 / n_faces; } real_t che::area_trig(const index_t & t) const @@ -242,7 +247,7 @@ real_t che::area_surface() const real_t area = 0; #pragma omp parallel for reduction(+: area) - for(index_t i = 0; i < n_faces_; i++) + for(index_t i = 0; i < n_faces; i++) area += area_trig(i); return area; @@ -253,7 +258,7 @@ void che::update_heatmap(const real_t * hm, real_t max_color) if(!hm) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) VHC[v] = 0.45; return; @@ -262,25 +267,25 @@ void che::update_heatmap(const real_t * hm, real_t max_color) if(max_color < numeric_limits::epsilon()) { #pragma omp parallel for reduction(max: max_color) - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) if(hm[v] < INFINITY) max_color = max(hm[v], max_color); } #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) VHC[v] = hm[v] / max_color; } const vertex & che::color(const index_t & v) const { - assert(VC && v < n_vertices_); + assert(VC && v < n_vertices); return VC[v]; } vertex & che::color(const index_t & v) { - assert(VC && v < n_vertices_); + assert(VC && v < n_vertices); return VC[v]; } @@ -293,22 +298,22 @@ vertex che::shading_color(const index_t & f, const float & u, const float & v, c const real_t & che::heatmap(const index_t & v) const { - assert(VHC && v < n_vertices_); + assert(VHC && v < n_vertices); return VHC[v]; } real_t & che::heatmap(const index_t & v) { - assert(VHC && v < n_vertices_); + assert(VHC && v < n_vertices); return VHC[v]; } void che::update_normals() { - if(!n_faces_) return; + if(!n_faces) return; #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { vertex & n = VN[v]; @@ -322,13 +327,13 @@ void che::update_normals() const vertex & che::normal(const index_t & v) const { - assert(VN && v < n_vertices_); + assert(VN && v < n_vertices); return VN[v]; } vertex & che::normal(const index_t & v) { - assert(VN && v < n_vertices_); + assert(VN && v < n_vertices); return VN[v]; } @@ -406,7 +411,7 @@ vertex che::barycenter(const index_t & t) const vertex che::corr_vertex(corr_t & corr) const { index_t he = corr.t * che::mtrig; - assert(he < n_half_edges_); + assert(he < n_half_edges); return corr.alpha[0] * gt_vt(he) + corr.alpha[1] * gt_vt(next(he)) + corr.alpha[2] * gt_vt(prev(he)); } @@ -425,21 +430,21 @@ real_t che::mean_edge() const real_t m = 0; #pragma omp parallel for reduction(+: m) - for(index_t e = 0; e < n_edges_; e++) + for(index_t e = 0; e < n_edges; e++) m += *(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); - return m / n_edges_; + return m / n_edges; } size_t che::memory() const { - return sizeof(*this) + n_vertices_ * (sizeof(vertex) + sizeof(index_t)) + filename_.size() - + sizeof(index_t) * (3 * n_half_edges_ + n_edges_ + n_borders_); + return sizeof(*this) + n_vertices * (sizeof(vertex) + sizeof(index_t)) + filename_.size() + + sizeof(index_t) * (3 * n_half_edges + n_edges + n_borders); } size_t che::genus() const { - size_t g = n_vertices_ - n_edges_ + n_faces_; + size_t g = n_vertices - n_edges + n_faces; return (g - 2) / (-2); } @@ -463,31 +468,31 @@ void che::normalize() vertex center; #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { #pragma omp critical center += GT[v]; } - center /= n_vertices_; + center /= n_vertices; real_t max_norm = 0; #pragma omp parallel for reduction(max : max_norm) - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { GT[v] -= center; max_norm = std::max(max_norm, *GT[v]); } #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) GT[v] /= max_norm; } bool che::is_pointcloud() const { - return n_faces_ == 0; + return n_faces == 0; } bool che::is_manifold() const @@ -497,67 +502,67 @@ bool che::is_manifold() const const index_t & che::vt(const index_t & he) const { - assert(he < n_half_edges_); + assert(he < n_half_edges); return VT[he]; } const vertex & che::gt(const index_t & v) const { - assert(v < n_vertices_); + assert(v < n_vertices); return GT[v]; } const vertex & che::gt_vt(const index_t & he) const { - assert(he < n_half_edges_); + assert(he < n_half_edges); return GT[VT[he]]; } const vertex & che::gt_vt_next_evt(const index_t & v) const { - assert(v < n_vertices_); + assert(v < n_vertices); return GT[VT[next(EVT[v])]]; } const vertex & che::gt_e(const index_t & e, const bool & op) { - assert(e < n_edges_); + assert(e < n_edges); return op ? GT[VT[next(ET[e])]] : GT[VT[ET[e]]]; } const index_t & che::vt_e(const index_t & e, const bool & op) { - assert(e < n_edges_); + assert(e < n_edges); return op ? VT[next(ET[e])] : VT[ET[e]]; } const index_t & che::et(const index_t & e) const { - assert(e < n_edges_); + assert(e < n_edges); return ET[e]; } const index_t & che::ot_et(const index_t & e) const { - assert(e < n_edges_); + assert(e < n_edges); return OT[ET[e]]; } const index_t & che::ot(const index_t & he) const { - assert(he < n_half_edges_); + assert(he < n_half_edges); return OT[he]; } const index_t & che::ot_evt(const index_t & v) const { - assert(v < n_vertices_); + assert(v < n_vertices); return OT[EVT[v]]; } const index_t & che::evt(const index_t & v) const { - assert(v < n_vertices_); + assert(v < n_vertices); return EVT[v]; } @@ -566,37 +571,12 @@ const index_t & che::bt(const index_t & b) const return BT[b]; } -const size_t & che::n_vertices() const -{ - return n_vertices_; -} - -const size_t & che::n_faces() const -{ - return n_faces_; -} - -const size_t & che::n_half_edges() const -{ - return n_half_edges_; -} - -const size_t & che::n_edges() const -{ - return n_edges_; -} - -const size_t & che::n_borders() const -{ - return n_borders_; -} - size_t che::max_degree() const { size_t d, md = 0; #pragma omp parallel for private(d) reduction(max: md) - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { d = 0; for_star(he, this, v) d++; @@ -615,7 +595,7 @@ vertex & che::get_vertex(index_t v) void che::set_vertices(const vertex *const& positions, size_t n, const index_t & v_i) { if(!positions) return; - if(!n) n = n_vertices_; + if(!n) n = n_vertices; memcpy(GT + v_i, positions, sizeof(vertex) * n); } @@ -626,7 +606,7 @@ const string & che::filename() const const string che::filename_size() const { - return filename_ + "_" + to_string(n_vertices_); + return filename_ + "_" + to_string(n_vertices); } void che::set_filename(const string & f) @@ -643,7 +623,7 @@ const string che::name() const const string che::name_size() const { - return name() + "_" + to_string(n_vertices_); + return name() + "_" + to_string(n_vertices); } void che::reload() @@ -656,7 +636,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector new_faces; // each 3 gproshan_debug(removing vertex); - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { if(EVT[v] != NIL) new_vertices.push_back(GT[v]); else removed.push_back(v); } - removed.push_back(n_vertices_); + removed.push_back(n_vertices); gproshan_debug_var(removed.size()); gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; - for(index_t v = removed[0] + 1; v < n_vertices_; v++) + for(index_t v = removed[0] + 1; v < n_vertices; v++) { if(v < removed[r]) { @@ -887,7 +867,7 @@ void che::remove_non_manifold_vertices() } } - for(index_t he = 0; he < n_half_edges_; he++) + for(index_t he = 0; he < n_half_edges; he++) if(VT[he] != NIL) new_faces.push_back(VT[he]); else gproshan_error_var(he); @@ -928,20 +908,20 @@ void che::remove_vertices(const vector & vertices) vector new_faces; // each 3 gproshan_debug(removing vertex); - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { if(EVT[v] != NIL) new_vertices.push_back(GT[v]); else removed.push_back(v); } - removed.push_back(n_vertices_); + removed.push_back(n_vertices); gproshan_debug_var(removed.size()); gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; - for(index_t v = removed[0] + 1; v < n_vertices_; v++) + for(index_t v = removed[0] + 1; v < n_vertices; v++) { if(v < removed[r]) { @@ -955,7 +935,7 @@ void che::remove_vertices(const vector & vertices) } } - for(index_t he = 0; he < n_half_edges_; he++) + for(index_t he = 0; he < n_half_edges; he++) if(VT[he] != NIL) new_faces.push_back(VT[he]); else gproshan_error_var(he); @@ -978,10 +958,10 @@ gproshan_debug(fill_holes); bool is_open = mesh->VT[next(mesh->EVT[0])] >= ncv; gproshan_debug_var(is_open); - size_t nv = n_vertices_ + mesh->n_vertices_ - ncv; - size_t nf = n_faces_ + mesh->n_faces_; - size_t nh = n_half_edges_ + mesh->n_half_edges_; - size_t ne = n_edges_ + mesh->n_edges_ - (ncv - is_open); + size_t nv = n_vertices + mesh->n_vertices - ncv; + size_t nf = n_faces + mesh->n_faces; + size_t nh = n_half_edges + mesh->n_half_edges; + size_t ne = n_edges + mesh->n_edges - (ncv - is_open); vertex * aGT = new vertex[nv]; index_t * aVT = new index_t[nh]; @@ -991,22 +971,22 @@ gproshan_debug(fill_holes); index_t * aEHT = new index_t[nh]; gproshan_debug(fill_holes); - memcpy(aGT, GT, sizeof(vertex) * n_vertices_); - memcpy(aGT + n_vertices_, mesh->GT + ncv, sizeof(vertex) * (nv - n_vertices_)); + memcpy(aGT, GT, sizeof(vertex) * n_vertices); + memcpy(aGT + n_vertices, mesh->GT + ncv, sizeof(vertex) * (nv - n_vertices)); - memcpy(aVT, VT, sizeof(index_t) * n_half_edges_); + memcpy(aVT, VT, sizeof(index_t) * n_half_edges); - index_t * t_aVT = aVT + n_half_edges_; - for(index_t he = 0; he < mesh->n_half_edges_; he++) - t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices_ - ncv; + index_t * t_aVT = aVT + n_half_edges; + for(index_t he = 0; he < mesh->n_half_edges; he++) + t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices - ncv; gproshan_debug(fill_holes); - memcpy(aOT, OT, sizeof(index_t) * n_half_edges_); + memcpy(aOT, OT, sizeof(index_t) * n_half_edges); gproshan_debug(fill_holes); - index_t * t_aOT = aOT + n_half_edges_; - for(index_t he = 0; he < mesh->n_half_edges_; he++) - t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges_ : NIL; + index_t * t_aOT = aOT + n_half_edges; + for(index_t he = 0; he < mesh->n_half_edges; he++) + t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges : NIL; gproshan_debug(fill_holes); for(index_t v, he_v, he_i, i = 0; i < ncv; i++) @@ -1016,28 +996,28 @@ gproshan_debug(fill_holes); { v = com_vertices[mesh->VT[next(he_i)]]; he_v = EVT[v]; - aOT[he_v] = he_i + n_half_edges_; + aOT[he_v] = he_i + n_half_edges; aOT[aOT[he_v]] = he_v; } } gproshan_debug(fill_holes); - memcpy(aEVT, EVT, sizeof(index_t) * n_vertices_); + memcpy(aEVT, EVT, sizeof(index_t) * n_vertices); gproshan_debug(fill_holes); if(is_open) - aEVT[com_vertices[0]] = mesh->EVT[0] != NIL ? mesh->EVT[0] + n_half_edges_ : NIL; + aEVT[com_vertices[0]] = mesh->EVT[0] != NIL ? mesh->EVT[0] + n_half_edges : NIL; gproshan_debug(fill_holes); - index_t * t_aEVT = aEVT + n_vertices_; - for(index_t v = ncv; v < mesh->n_vertices_; v++) - t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges_ : NIL; + index_t * t_aEVT = aEVT + n_vertices; + for(index_t v = ncv; v < mesh->n_vertices; v++) + t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges : NIL; gproshan_debug(fill_holes); - memcpy(aET, ET, sizeof(index_t) * n_edges_); + memcpy(aET, ET, sizeof(index_t) * n_edges); gproshan_debug(fill_holes); - bool * common_edge = new bool[mesh->n_edges_]; - memset(common_edge, 0, sizeof(bool) * mesh->n_edges_); + bool * common_edge = new bool[mesh->n_edges]; + memset(common_edge, 0, sizeof(bool) * mesh->n_edges); for(index_t he_i, i = 0; i < ncv; i++) { he_i = mesh->EVT[i]; @@ -1045,10 +1025,10 @@ gproshan_debug(fill_holes); common_edge[mesh->EHT[he_i]] = true; } - index_t ae = n_edges_; - for(index_t e = 0; e < mesh->n_edges_; e++) + index_t ae = n_edges; + for(index_t e = 0; e < mesh->n_edges; e++) if(!common_edge[e]) - aET[ae++] = mesh->ET[e] + n_half_edges_; + aET[ae++] = mesh->ET[e] + n_half_edges; gproshan_debug_var(ae == ne); gproshan_debug_var(ae); @@ -1059,17 +1039,17 @@ gproshan_debug(fill_holes); delete_me(); gproshan_debug(fill_holes); - GT = aGT; - VT = aVT; - OT = aOT; - EVT = aEVT; - ET = aET; - EHT = aEHT; - - n_vertices_ = nv; - n_faces_ = nf; - n_half_edges_ = nh; - n_edges_ = ne; + GT = aGT; + VT = aVT; + OT = aOT; + EVT = aEVT; + ET = aET; + EHT = aEHT; + + rw(n_vertices) = nv; + rw(n_faces) = nf; + rw(n_half_edges) = nh; + rw(n_edges) = ne; update_eht(); update_bt(); @@ -1096,7 +1076,7 @@ void che::set_head_vertices(index_t * head, const size_t & n) VT[he] = v; swap(EVT[v], EVT[i]); - for(index_t b = 0; b < n_borders_; b++) + for(index_t b = 0; b < n_borders; b++) { if(BT[b] == i) BT[b] = v; else if(BT[b] == v) BT[b] = i; @@ -1122,18 +1102,18 @@ index_t che::link_intersect(const index_t & v_a, const index_t & v_b) corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *const & normals) { - if(n_faces_ < 2) return nullptr; + if(n_faces < 2) return nullptr; // init default corr - corr_t * corr = new corr_t[n_vertices_]; + corr_t * corr = new corr_t[n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) corr[v].init(EVT[v]); - short * faces_fixed = new short[n_faces_]; - memset(faces_fixed, 0, sizeof(short) * n_faces_); - index_t * deleted_vertices = new index_t[n_vertices_]; - memset(deleted_vertices, 0, sizeof(index_t) * n_vertices_); + short * faces_fixed = new short[n_faces]; + memset(faces_fixed, 0, sizeof(short) * n_faces); + index_t * deleted_vertices = new index_t[n_vertices]; + memset(deleted_vertices, 0, sizeof(index_t) * n_vertices); index_t e_d, he_d, ohe_d, va, vb; vertex aux_va, aux_vb; @@ -1151,11 +1131,11 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con bool is_collapse; - for(index_t e = 0; e < n_edges_; e++) + for(index_t e = 0; e < n_edges; e++) { //e_d = ne; - e_d = sort_edges ? sort_edges[e] : rand() % n_edges_; - assert(e_d < n_edges_); + e_d = sort_edges ? sort_edges[e] : rand() % n_edges; + assert(e_d < n_edges); he_d = ET[e_d]; ohe_d = OT[he_d]; @@ -1227,11 +1207,11 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con vector new_vertices; vector new_faces; - new_vertices.reserve(n_vertices_); - new_faces.reserve(n_faces_); + new_vertices.reserve(n_vertices); + new_faces.reserve(n_faces); index_t dv = 0; - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { if(deleted_vertices[v]) dv++; else @@ -1241,17 +1221,17 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con } } - index_t * map_he = new index_t[n_half_edges_]; - memset(map_he, 255, sizeof(index_t) * n_half_edges_); + index_t * map_he = new index_t[n_half_edges]; + memset(map_he, 255, sizeof(index_t) * n_half_edges); - for(index_t n_he = 0, he = 0; he < n_half_edges_; he++) + for(index_t n_he = 0, he = 0; he < n_half_edges; he++) if(faces_fixed[trig(he)] > -1) { new_faces.push_back(VT[he] - deleted_vertices[VT[he]]); map_he[he] = n_he++; } - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) corr[v].t = trig(map_he[corr[v].t * che::mtrig]); delete_me(); @@ -1364,8 +1344,8 @@ void che::init(const vertex * vertices, const index_t & n_v, const index_t * fac { init(n_v, n_f); - memcpy(GT, vertices, n_vertices_ * sizeof(vertex)); - memcpy(VT, faces, n_half_edges_ * sizeof(index_t)); + memcpy(GT, vertices, n_vertices * sizeof(vertex)); + memcpy(VT, faces, n_half_edges * sizeof(index_t)); update_evt_ot_et(); update_eht(); @@ -1383,47 +1363,47 @@ void che::init(const string & file) void che::init(const size_t & n_v, const size_t & n_f) { - n_vertices_ = n_v; - n_faces_ = n_f; - n_half_edges_ = che::mtrig * n_faces_; - - n_edges_ = n_borders_ = 0; + rw(n_vertices) = n_v; + rw(n_faces) = n_f; + rw(n_half_edges) = che::mtrig * n_faces; + rw(n_edges) = 0; + rw(n_borders) = 0; - if(n_vertices_) GT = new vertex[n_vertices_]; - if(n_half_edges_) VT = new index_t[n_half_edges_]; - if(n_half_edges_) OT = new index_t[n_half_edges_]; - if(n_vertices_) EVT = new index_t[n_vertices_]; - if(n_vertices_) EHT = new index_t[n_half_edges_]; + if(n_vertices) GT = new vertex[n_vertices]; + if(n_half_edges) VT = new index_t[n_half_edges]; + if(n_half_edges) OT = new index_t[n_half_edges]; + if(n_vertices) EVT = new index_t[n_vertices]; + if(n_vertices) EHT = new index_t[n_half_edges]; - if(n_vertices_) VN = new vertex[n_vertices_]; - if(n_vertices_) VC = new vertex[n_vertices_]; - if(n_vertices_) VHC = new real_t[n_vertices_]; + if(n_vertices) VN = new vertex[n_vertices]; + if(n_vertices) VC = new vertex[n_vertices]; + if(n_vertices) VHC = new real_t[n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) VC[v] = vcolor; } void che::update_evt_ot_et() { - memset(EVT, -1, sizeof(index_t) * n_vertices_); + memset(EVT, -1, sizeof(index_t) * n_vertices); - if(!n_faces_) return; + if(!n_faces) return; - vector * he_p_vertex = new vector[n_vertices_]; + vector * he_p_vertex = new vector[n_vertices]; //vertex table - for(index_t he = 0; he < n_half_edges_; he++) + for(index_t he = 0; he < n_half_edges; he++) { EVT[VT[he]] = he; he_p_vertex[VT[he]].push_back(he); } //opposite table - edge table - memset(OT, -1, sizeof(index_t) * n_half_edges_); + memset(OT, -1, sizeof(index_t) * n_half_edges); vector et; - for(index_t he = 0; he < n_half_edges_; he++) + for(index_t he = 0; he < n_half_edges; he++) { if(OT[he] == NIL) { @@ -1443,16 +1423,16 @@ void che::update_evt_ot_et() } // non manifold two disk - //for(index_t he = 0; he < n_half_edges_; he++) + //for(index_t he = 0; he < n_half_edges; he++) // if(OT[he] != NIL) assert(he == OT[OT[he]]); //edge table - n_edges_ = et.size(); - ET = new index_t[n_edges_]; - memcpy(ET, et.data(), sizeof(index_t) * n_edges_); + rw(n_edges) = et.size(); + ET = new index_t[n_edges]; + memcpy(ET, et.data(), sizeof(index_t) * n_edges); - for(index_t he = 0; he < n_half_edges_; he++) + for(index_t he = 0; he < n_half_edges; he++) if(OT[he] == NIL && EVT[VT[he]] != NIL) { if(OT[EVT[VT[he]]] == NIL && EVT[VT[he]] != he) @@ -1463,11 +1443,11 @@ void che::update_evt_ot_et() else EVT[VT[he]] = he; } -// for(index_t v = 0; v < n_vertices_; v++) -// if(EVT[v] >= n_half_edges_) +// for(index_t v = 0; v < n_vertices; v++) +// if(EVT[v] >= n_half_edges) // { // gproshan_debug_var(EVT[v]); -// assert(EVT[v] < n_half_edges_); +// assert(EVT[v] < n_half_edges); // } delete [] he_p_vertex; @@ -1476,7 +1456,7 @@ void che::update_evt_ot_et() void che::update_eht() { #pragma omp parallel for - for(index_t e = 0; e < n_edges_; e++) + for(index_t e = 0; e < n_edges; e++) { EHT[ET[e]] = e; if(OT[ET[e]] != NIL) @@ -1486,15 +1466,15 @@ void che::update_eht() void che::update_bt() { - if(!n_faces_) return; + if(!n_faces) return; if(!manifold) return; - bool * border = new bool[n_vertices_]; - memset(border, 0, sizeof(bool) * n_vertices_); + bool * border = new bool[n_vertices]; + memset(border, 0, sizeof(bool) * n_vertices); vector borders; - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) if(!border[v] && EVT[v] != NIL && OT[EVT[v]] == NIL) { borders.push_back(v); @@ -1502,11 +1482,11 @@ void che::update_bt() border[VT[he]] = true; } - n_borders_ = borders.size(); - if(n_borders_) + rw(n_borders) = borders.size(); + if(n_borders) { - BT = new index_t[n_borders_]; - memcpy(BT, borders.data(), sizeof(index_t) * n_borders_); + BT = new index_t[n_borders]; + memcpy(BT, borders.data(), sizeof(index_t) * n_borders); } else BT = nullptr; diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 6bd5985c..3b104759 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -129,17 +129,17 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const if(j != p.second) { - n_v = add_border_vertices((j + 1) % size, p.second, hole->n_vertices() - merge_vertices[!c].size()); - merge_vertices[c].push_back(hole->n_vertices() + n_v - 1); + n_v = add_border_vertices((j + 1) % size, p.second, hole->n_vertices - merge_vertices[!c].size()); + merge_vertices[c].push_back(hole->n_vertices + n_v - 1); } else merge_vertices[c].push_back(merge_vertices[!c].back()); - gen_vertices(merge_vertices[c], vertices[c], mesh->gt(border_vertices[p.second]), mesh->gt(border_vertices[p.first]), hole->n_vertices() - merge_vertices[!c].size()); + gen_vertices(merge_vertices[c], vertices[c], mesh->gt(border_vertices[p.second]), mesh->gt(border_vertices[p.first]), hole->n_vertices - merge_vertices[!c].size()); if(i != p.first) { - merge_vertices[c].push_back(vertices[c].size() + hole->n_vertices() - merge_vertices[!c].size()); - n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1 , hole->n_vertices() - merge_vertices[!c].size()); + merge_vertices[c].push_back(vertices[c].size() + hole->n_vertices - merge_vertices[!c].size()); + n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1 , hole->n_vertices - merge_vertices[!c].size()); } else merge_vertices[c].push_back(merge_vertices[!c].front()); @@ -183,7 +183,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const j = (j + 1) % size; normal = 0; - n_v = add_border_vertices(j, i, hole->n_vertices() - merge_vertices[!c].size()); + n_v = add_border_vertices(j, i, hole->n_vertices - merge_vertices[!c].size()); normal /= n_v; aux_hole = nullptr; @@ -265,7 +265,7 @@ vector * fill_all_holes(che * mesh, const size_t & max_iter) tie(border_vertices, holes) = fill_all_holes_meshes(mesh, max_iter); if(holes) { - for(index_t b = 0; b < mesh->n_borders(); b++) + for(index_t b = 0; b < mesh->n_borders; b++) if(holes[b]) delete holes[b]; } delete [] holes; @@ -277,7 +277,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t vector * border_vertices = nullptr; che ** holes = nullptr; - const size_t n_borders = mesh->n_borders(); + const size_t n_borders = mesh->n_borders; if(!n_borders) return make_tuple(border_vertices, holes); border_vertices = new vector[n_borders]; @@ -311,7 +311,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t } - gproshan_debug_var(mesh->n_borders()); + gproshan_debug_var(mesh->n_borders); return make_tuple(border_vertices, holes); } diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 27578fbc..b3152260 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -107,14 +107,14 @@ void che_obj::write_file(const che * mesh, const string & file) os << "####\n#\n"; os << "# OBJ generated by gproshan 2019" << endl; - os << "# vertices: " << mesh->n_vertices() << endl; - os << "# faces: " << mesh->n_faces() << endl; + os << "# vertices: " << mesh->n_vertices << endl; + os << "# faces: " << mesh->n_faces << endl; os << "#\n####\n"; - for(size_t v = 0; v < mesh->n_vertices(); v++) + for(size_t v = 0; v < mesh->n_vertices; v++) os << "v " << mesh->gt(v) << endl; - for(index_t he = 0; he < mesh->n_half_edges(); ) + for(index_t he = 0; he < mesh->n_half_edges; ) { os << "f"; for(index_t i = 0; i < che::mtrig; i++) diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 11a006b5..ae2bf44b 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -39,7 +39,7 @@ void che_off::read_file(const string & file) init(n_v, n_f); real_t alpha; // color - for(index_t i = 0; i < n_vertices_; i++) + for(index_t i = 0; i < n_vertices; i++) { is >> GT[i]; if(soff[0] == 'C' || soff[1] == 'C') @@ -51,12 +51,12 @@ void che_off::read_file(const string & file) if(soff[0] == 'C' || soff[1] == 'C') { #pragma omp parallel for - for(index_t i = 0; i < n_vertices_; i++) + for(index_t i = 0; i < n_vertices; i++) VC[i] /= 255; } index_t he = 0; - for(index_t i = 0; i < n_faces_; i++) + for(index_t i = 0; i < n_faces; i++) { is >> v; if(!i && v > che::mtrig) @@ -90,9 +90,9 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t ofstream os(file + ".off"); os << off << endl; - os << mesh->n_vertices() << " " << (pointcloud ? 0 : mesh->n_faces()) << " 0" << endl; + os << mesh->n_vertices << " " << (pointcloud ? 0 : mesh->n_faces) << " 0" << endl; - for(size_t v = 0; v < mesh->n_vertices(); v++) + for(size_t v = 0; v < mesh->n_vertices; v++) { os << mesh->gt(v); @@ -102,7 +102,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t } if(!pointcloud) - for(index_t he = 0; he < mesh->n_half_edges(); ) + for(index_t he = 0; he < mesh->n_half_edges; ) { os << che::mtrig; for(index_t i = 0; i < che::mtrig; i++) diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 11607db4..8849cbcc 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -89,7 +89,7 @@ void che_ply::read_file(const string & file) if(format == "ascii") { - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { getline(is, str); stringstream ss(str); @@ -118,7 +118,7 @@ void che_ply::read_file(const string & file) }; char * vbuffer = new char[vbytes]; - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { is.read(vbuffer, vbytes); if(big_endian) big_to_little(vbuffer, vbytes); @@ -177,18 +177,18 @@ void che_ply::write_file(const che * mesh, const string & file) os << "ply" << endl; os << "format ascii 1.0" << endl; os << "comment generated by gproshan 2020" << endl; - os << "element vertex " << mesh->n_vertices() << endl; + os << "element vertex " << mesh->n_vertices << endl; os << "property float x" << endl; os << "property float y" << endl; os << "property float z" << endl; - os << "element face " << mesh->n_faces() << endl; + os << "element face " << mesh->n_faces << endl; os << "property list uchar int vertex_index" << endl; os << "end_header" << endl; - for(size_t v = 0; v < mesh->n_vertices(); v++) + for(size_t v = 0; v < mesh->n_vertices; v++) os << mesh->gt(v) << endl; - for(index_t he = 0; he < mesh->n_half_edges(); ) + for(index_t he = 0; he < mesh->n_half_edges; ) { os << che::mtrig; for(index_t i = 0; i < che::mtrig; i++) diff --git a/src/mesh/che_poisson.cpp b/src/mesh/che_poisson.cpp index e9b6850d..a5d8c34b 100644 --- a/src/mesh/che_poisson.cpp +++ b/src/mesh/che_poisson.cpp @@ -14,8 +14,8 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) { if(!k) return; - a_mat B(mesh->n_vertices(), 3); - for(index_t v = 0; v < mesh->n_vertices(); v++) + a_mat B(mesh->n_vertices, 3); + for(index_t v = 0; v < mesh->n_vertices; v++) { if(v < old_n_vertices) { @@ -29,7 +29,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) a_sp_mat L, A; laplacian(mesh, L, A); - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) B.row(i) *= -1 / A(i,i); a_sp_mat M; @@ -53,7 +53,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) a_mat X; if(spsolve(X, s * L, s * B)) - for(index_t v = old_n_vertices; v < mesh->n_vertices(); v++) + for(index_t v = old_n_vertices; v < mesh->n_vertices; v++) { mesh->get_vertex(v).x = X(v - old_n_vertices, 0); mesh->get_vertex(v).y = X(v - old_n_vertices, 1); @@ -101,8 +101,8 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t { if(old_n_vertices == n_vertices) return; - index_t * rings = new index_t[mesh->n_vertices()]; - index_t * sorted = new index_t[mesh->n_vertices()]; + index_t * rings = new index_t[mesh->n_vertices]; + index_t * sorted = new index_t[mesh->n_vertices]; vector limites; mesh->compute_toplesets(rings, sorted, limites, border_vertices, k); diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 4b52cc70..df39f8da 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -55,7 +55,7 @@ void che_ptx::read_file(const string & file) init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); #pragma omp parallel for - for(index_t v = 0; v < n_vertices_; v++) + for(index_t v = 0; v < n_vertices; v++) { const index_t & i = v * p; @@ -95,11 +95,11 @@ void che_ptx::read_file(const string & file) (c ) + (r + 1) * n_cols); } - n_half_edges_ = he; - n_faces_ = he / che::mtrig; + rw(n_half_edges) = he; + rw(n_faces) = he / che::mtrig; #pragma omp parallel for - for(index_t i = 0; i < n_vertices_; i++) + for(index_t i = 0; i < n_vertices; i++) VC[i] /= 255; is.close(); diff --git a/src/mesh/simplification.cpp b/src/mesh/simplification.cpp index a6f68ccc..11d2cc24 100644 --- a/src/mesh/simplification.cpp +++ b/src/mesh/simplification.cpp @@ -11,7 +11,7 @@ simplification::simplification(che * mesh_, const vertex *const & normals, const { mesh = mesh_; levels = levels_; - Q = new a_mat[mesh->n_vertices()]; + Q = new a_mat[mesh->n_vertices]; execute(normals); } @@ -31,9 +31,9 @@ void simplification::execute(const vertex *const & normals) { compute_quadrics(); - const size_t n_vertices = mesh->n_vertices(); - index_t * sort_edges = new index_t[mesh->n_edges()]; - real_t * error_edges = new real_t[mesh->n_edges()]; + const size_t n_vertices = mesh->n_vertices; + index_t * sort_edges = new index_t[mesh->n_edges]; + real_t * error_edges = new real_t[mesh->n_edges]; vertex * corr_v = new vertex[n_vertices]; index_t * corr_i = new index_t[n_vertices * che::mtrig]; @@ -102,7 +102,7 @@ void simplification::compute_quadrics() vertex n; #pragma omp parallel for private(n) - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { Q[v].resize(4,4); Q[v].zeros(); @@ -124,13 +124,13 @@ void simplification::compute_quadrics() void simplification::order_edges(index_t * const & sort_edges, real_t * const & error_edges) { #pragma omp parallel for - for(index_t e = 0; e < mesh->n_edges(); e++) + for(index_t e = 0; e < mesh->n_edges; e++) { sort_edges[e] = e; error_edges[e] = compute_error(e); } - sort(sort_edges, sort_edges + mesh->n_edges(), + sort(sort_edges, sort_edges + mesh->n_edges, [&error_edges](const index_t & a, const index_t & b) { return error_edges[a] < error_edges[b]; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index a8d9d271..636362c7 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -108,7 +108,7 @@ bool embree::occluded(ray_hit & r) void embree::build_bvh(const std::vector & meshes, const bool & pointcloud) { for(auto & m: meshes) - if(!m->n_faces() || pointcloud) + if(!m->n_faces || pointcloud) geomID_mesh[add_pointcloud(m)] = {m, true}; else geomID_mesh[add_mesh(m)] = {m, false}; @@ -140,23 +140,23 @@ index_t embree::add_mesh(const che * mesh) glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices() + mesh->n_vertices ); index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(index_t), - mesh->n_faces() + mesh->n_faces ); #ifdef SINGLE_P - memcpy(vertices, &mesh->gt(0), mesh->n_vertices() * sizeof(vertex)); + memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); #else #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); #endif // SINGLE_P - memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t)); + memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); @@ -174,18 +174,18 @@ index_t embree::add_pointcloud(const che * mesh) RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, 4 * sizeof(float), - mesh->n_vertices() + mesh->n_vertices ); glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices() + mesh->n_vertices ); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) { pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 6b0efcd4..3b647b83 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -77,7 +77,7 @@ float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm void embree_splat::init_splats(const che * mesh) { const size_t n = 10; - vsplat.resize((mesh->n_vertices() + n - 1) / n); + vsplat.resize((mesh->n_vertices + n - 1) / n); gproshan_log_var(vsplat.size()); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index f1fa09a3..377f0f53 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -174,35 +174,35 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, #ifdef SINGLE_P - cudaMalloc(&d_vertex, mesh->n_vertices() * sizeof(vertex)); - cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices() * sizeof(vertex), cudaMemcpyHostToDevice); + cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(vertex)); + cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); #else - glm::vec3 * vertices = new glm::vec3[mesh->n_vertices()]; - cudaMalloc(&d_vertex, mesh->n_vertices() * sizeof(float) * 3); + glm::vec3 * vertices = new glm::vec3[mesh->n_vertices]; + cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(float) * 3); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices(); i++) + for(index_t i = 0; i < mesh->n_vertices; i++) vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); - cudaMemcpy(d_vertex, vertices, mesh->n_vertices() * sizeof(vertex), cudaMemcpyHostToDevice); + cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); delete [] vertices; #endif // SINGLE_P - cudaMalloc(&d_index, mesh->n_half_edges() * sizeof(index_t)); - cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges() * sizeof(index_t), cudaMemcpyHostToDevice); + cudaMalloc(&d_index, mesh->n_half_edges * sizeof(index_t)); + cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t), cudaMemcpyHostToDevice); optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); - optix_mesh.triangleArray.numVertices = mesh->n_vertices(); + optix_mesh.triangleArray.numVertices = mesh->n_vertices; optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces(); + optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces; optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; optix_trig_flags = 0 ; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 22555c55..0ad013b1 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -51,7 +51,7 @@ void che_viewer::update() vertex pmin(INFINITY, INFINITY, INFINITY); vertex pmax(0, 0, 0); - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) { const vertex & p = mesh->gt(v); @@ -80,28 +80,28 @@ void che_viewer::update_vbo() // 0 VERTEX glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 2 MESH COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(vertex), &mesh->color(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->color(0), GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 HEAT MAP COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices() * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 1, GL_VERTEX_T, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -110,7 +110,7 @@ void che_viewer::update_vbo() if(!mesh->is_pointcloud()) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges() * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -144,9 +144,9 @@ void che_viewer::draw(shader & program) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); if(n_instances) - glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0, n_instances); + glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); else - glDrawElements(GL_TRIANGLES, mesh->n_half_edges(), GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -161,7 +161,7 @@ void che_viewer::draw_point_cloud(shader & program) glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glDrawArrays(GL_POINTS, 0, mesh->n_vertices()); + glDrawArrays(GL_POINTS, 0, mesh->n_vertices); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -174,14 +174,14 @@ void che_viewer::translate(const vertex & p) v_translate = p; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->get_vertex(v) += v_translate; } void che_viewer::invert_orientation() { #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices(); v++) + for(index_t v = 0; v < mesh->n_vertices; v++) mesh->normal(v) = -mesh->normal(v); } @@ -190,13 +190,13 @@ void che_viewer::log_info() if(!mesh) return; gproshan_log_var(mesh->filename()); - gproshan_log_var(mesh->n_vertices()); - gproshan_log_var(mesh->n_faces()); - gproshan_log_var(mesh->n_half_edges()); - gproshan_log_var(mesh->n_edges()); + gproshan_log_var(mesh->n_vertices); + gproshan_log_var(mesh->n_faces); + gproshan_log_var(mesh->n_half_edges); + gproshan_log_var(mesh->n_edges); gproshan_log_var(mesh->area_surface()); gproshan_log_var(mesh->is_manifold()); - gproshan_log_var(mesh->n_borders()); + gproshan_log_var(mesh->n_borders); gproshan_log_var(mesh->memory() / 1E6); gproshan_log_var(mesh->quality()); gproshan_log_var(mesh->genus()); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index be22b3a7..939e6422 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -849,7 +849,7 @@ void viewer::draw_selected_vertices(shader & program) void viewer::select_border_vertices() { active_mesh().selected.clear(); - for(index_t b = 0; b < active_mesh()->n_borders(); b++) + for(index_t b = 0; b < active_mesh()->n_borders; b++) for_border(he, active_mesh(), active_mesh()->bt(b)) active_mesh().selected.push_back(active_mesh()->vt(he)); } From e8936e653ea1d959f588183816f257a79eb00c5c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Feb 2021 22:49:15 +0100 Subject: [PATCH 0429/1018] add compute_hks (heat kernel signature) --- include/features/descriptor.h | 8 +++--- src/app_viewer.cpp | 50 +++++++++++++++-------------------- src/features/descriptor.cpp | 20 +++++++++----- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/include/features/descriptor.h b/include/features/descriptor.h index 285e4f93..245316b4 100644 --- a/include/features/descriptor.h +++ b/include/features/descriptor.h @@ -19,16 +19,16 @@ class descriptor a_vec eigval; a_mat eigvec; a_mat features; - + public: descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); operator bool () const; ///< return true if the features were computed real_t operator () (const index_t & v) const; private: - void compute_gps(const size_t & t); - void compute_hks(const size_t & t); - void compute_wks(const size_t & t); + void compute_gps(); + void compute_hks(); + void compute_wks(); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6a0d1447..2d9bec60 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -315,7 +315,6 @@ bool app_viewer::process_wks(viewer * p_view) static bool status = true; ImGui::InputInt("eigenvectors", &K); - ImGui::InputInt("times", &T); if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing WKS."); if(ImGui::Button("Run")) @@ -358,40 +357,32 @@ bool app_viewer::process_hks(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int K = 100; - static int T = 100; - + static int K = 50; + static bool status = true; ImGui::InputInt("eigenvectors", &K); - ImGui::InputInt("times", &T); + if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing HKS."); if(ImGui::Button("Run")) { - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; - - TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) - gproshan_log_var(view->time); - - if(!K) return true; - - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices; v++) + descriptor HKS(descriptor::HKS, mesh, K); + + if(HKS) { - a_vec s(T, arma::fill::zeros); - for(int t = 0; t < T; t++) - for(int k = 1; k < K; k++) - s(t) += exp(-abs(eigval(k)) * t) * eigvec(v, k) * eigvec(v, k); + status = true; - mesh->heatmap(v) = norm(abs(arma::fft(s, 128))); - //mesh->heatmap(v) = norm(s); - max_s = max(max_s, mesh->heatmap(v)); - } + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices; v++) + { + mesh->heatmap(v) = HKS(v); + max_s = max(max_s, mesh->heatmap(v)); + } - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) - mesh->heatmap(v) /= max_s; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; v++) + mesh->heatmap(v) /= max_s; + } + else status = false; } return true; @@ -405,10 +396,11 @@ bool app_viewer::process_gps(viewer * p_view) static int K = 50; static bool status = true; ImGui::InputInt("eigenvectors", &K); + if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing GPS."); if(ImGui::Button("Run")) { - descriptor GPS(descriptor::GPS, mesh, K); + descriptor GPS(descriptor::GPS, mesh, K); if(GPS) { diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index df6dceb9..b3940a6d 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -13,9 +13,9 @@ descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n switch(sig) { - case GPS: compute_gps(1); break; - case HKS: break; - case WKS: break; + case GPS: compute_gps(); break; + case HKS: compute_hks(); break; + case WKS: compute_wks(); break; } } @@ -29,19 +29,25 @@ real_t descriptor::operator () (const index_t & v) const return norm(features.row(v)); } -void descriptor::compute_gps(const size_t & T) +void descriptor::compute_gps() { features = eigvec.tail_cols(eigvec.n_cols - 1); for(index_t i = 1; i < eigval.size(); i++) features.col(i - 1) /= sqrt(eigval(i)); } -void descriptor::compute_hks(const size_t & T) +void descriptor::compute_hks() { - features.zeros(); + features.zeros(eigvec.n_rows, eigvec.n_cols); + + eigvec %= eigvec; // element wise product + + #pragma omp parallel for + for(index_t t = 0; t < features.n_cols; t++) + features.col(t) = eigvec * exp(-eigval * t); } -void descriptor::compute_wks(const size_t & T) +void descriptor::compute_wks() { features.zeros(); } From 0daeee2e99f0492d48da9958b384145df58515c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Feb 2021 23:35:19 +0100 Subject: [PATCH 0430/1018] add compute_wks (wave kernel signature) --- include/app_viewer.h | 1 + src/app_viewer.cpp | 97 ++++++------------------------------- src/features/descriptor.cpp | 21 ++++++-- 3 files changed, 32 insertions(+), 87 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index ce8f1e84..bc7fd2d4 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -71,6 +71,7 @@ class app_viewer : public viewer static bool process_pc_reconstruction(viewer * p_view); static bool process_functional_maps(viewer * p_view); + static bool descriptor_heatmap(viewer * p_view, const descriptor::signature & sig); static bool process_gps(viewer * p_view); static bool process_hks(viewer * p_view); static bool process_wks(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2d9bec60..92a10e5e 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -305,54 +305,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) return true; } -bool app_viewer::process_wks(viewer * p_view) -{ - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - static int K = 100; - static int T = 100; - static bool status = true; - - ImGui::InputInt("eigenvectors", &K); - if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing WKS."); - - if(ImGui::Button("Run")) - { - descriptor wks(descriptor::WKS, mesh, K); - - if(wks) - { - status = true; - - - } - else status = false; - -/* - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices; v++) - { - a_vec s(T, arma::fill::zeros); - for(int t = 0; t < T; t++) - for(int k = 1; k < K; k++) - s(t) += exp(-eigval(k) * t) * eigvec(v, k) * eigvec(v, k); - - mesh->heatmap(v) = norm(s); - max_s = max(max_s, mesh->heatmap(v)); - } - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) - mesh->heatmap(v) /= max_s;i - */ - } - - return true; -} - -bool app_viewer::process_hks(viewer * p_view) +bool app_viewer::descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -360,13 +313,13 @@ bool app_viewer::process_hks(viewer * p_view) static int K = 50; static bool status = true; ImGui::InputInt("eigenvectors", &K); - if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing HKS."); + if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing features."); if(ImGui::Button("Run")) { - descriptor HKS(descriptor::HKS, mesh, K); + descriptor features(sig, mesh, K); - if(HKS) + if(features) { status = true; @@ -374,7 +327,7 @@ bool app_viewer::process_hks(viewer * p_view) #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices; v++) { - mesh->heatmap(v) = HKS(v); + mesh->heatmap(v) = features(v); max_s = max(max_s, mesh->heatmap(v)); } @@ -390,38 +343,17 @@ bool app_viewer::process_hks(viewer * p_view) bool app_viewer::process_gps(viewer * p_view) { - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - static int K = 50; - static bool status = true; - ImGui::InputInt("eigenvectors", &K); - if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing GPS."); - - if(ImGui::Button("Run")) - { - descriptor GPS(descriptor::GPS, mesh, K); - - if(GPS) - { - status = true; - - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices; v++) - { - mesh->heatmap(v) = GPS(v); - max_s = max(max_s, mesh->heatmap(v)); - } + return descriptor_heatmap(p_view, descriptor::GPS); +} - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) - mesh->heatmap(v) /= max_s; - } - else status = false; - } +bool app_viewer::process_hks(viewer * p_view) +{ + return descriptor_heatmap(p_view, descriptor::HKS); +} - return true; +bool app_viewer::process_wks(viewer * p_view) +{ + return descriptor_heatmap(p_view, descriptor::WKS); } bool app_viewer::process_key_points(viewer * p_view) @@ -695,7 +627,6 @@ bool app_viewer::process_voronoi(viewer * p_view) bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index b3940a6d..83775d4c 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -38,18 +38,31 @@ void descriptor::compute_gps() void descriptor::compute_hks() { - features.zeros(eigvec.n_rows, eigvec.n_cols); + eigvec %= eigvec; // element wise product - eigvec %= eigvec; // element wise product + features.zeros(eigvec.n_rows, eigvec.n_cols); #pragma omp parallel for for(index_t t = 0; t < features.n_cols; t++) features.col(t) = eigvec * exp(-eigval * t); } +///< http://imagine.enpc.fr/~aubrym/projects/wks/index.html void descriptor::compute_wks() -{ - features.zeros(); +{ + eigvec %= eigvec; // element wise product + eigval = log(eigval); + + a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); + real_t sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference + real_t sigma_2 = 2 * sigma * sigma; + + features.zeros(eigvec.n_rows, e.n_elem); + + #pragma omp parallel for + for(index_t t = 0; t < features.n_cols; t++) + features.col(t) = eigvec * exp(-pow(e(t) - eigval, 2) / sigma_2) / + sum(exp(-pow(e(t) - eigval, 2) / sigma_2)); } From 385dad3131c0091f72392e7dbf5250fcff8c24d8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Feb 2021 16:38:09 +0100 Subject: [PATCH 0431/1018] update CMakeLists gproshan library --- CMakeLists.txt | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00123c5c..60ce0627 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,12 +17,16 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) + set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") + set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) + set(CMAKE_CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80) + add_definitions(-DGPROSHAN_CUDA) include_directories(${CUDAToolkit_INCLUDE_DIRS}) - find_package(OptiX 7.1) + find_package(OptiX 7.2) if(OptiX_INCLUDE) - set(CUDA_HOST_COMPILER ${CMAKE_CUDA_HOST_COMPILER}) +# set(CUDA_HOST_COMPILER ${CMAKE_CUDA_HOST_COMPILER}) add_definitions(-DGPROSHAN_OPTIX) include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) @@ -65,9 +69,11 @@ include_directories(${CGAL_INCLUDE_DIRS}) include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/imgui) + FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(imgui STATIC ${imgui_sources}) + set(ptx_code "") if(OptiX_INCLUDE) cuda_compile_ptx(ptx_files "${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu") @@ -79,53 +85,45 @@ if(OptiX_INCLUDE) ) endif(OptiX_INCLUDE) + FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) -add_library(gproshan_cpp STATIC ${cpp_sources} ${ptx_code}) +if(CUDAToolkit_FOUND) + FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) +endif(CUDAToolkit_FOUND) + +add_library(gproshan STATIC ${cpp_sources} ${cu_sources} ${ptx_code}) -target_compile_options(gproshan_cpp PRIVATE -Wall -Wno-unused-result) +target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) -target_link_libraries(gproshan_cpp OpenMP::OpenMP_CXX) -target_link_libraries(gproshan_cpp OpenGL::GL) -target_link_libraries(gproshan_cpp GLEW::GLEW) -target_link_libraries(gproshan_cpp glfw) -target_link_libraries(gproshan_cpp ${X11_X11_LIB}) -target_link_libraries(gproshan_cpp ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan_cpp ${SuiteSparse_LIBRARIES}) -target_link_libraries(gproshan_cpp CGAL::CGAL) -target_link_libraries(gproshan_cpp ${OptiX_LIBRARY}) -target_link_libraries(gproshan_cpp imgui) +target_link_libraries(gproshan OpenMP::OpenMP_CXX) +target_link_libraries(gproshan OpenGL::GL) +target_link_libraries(gproshan GLEW::GLEW) +target_link_libraries(gproshan glfw) +target_link_libraries(gproshan ${X11_X11_LIB}) +target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) +target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan CGAL::CGAL) +target_link_libraries(gproshan ${OptiX_LIBRARY}) +target_link_libraries(gproshan imgui) if(embree_FOUND) - target_link_libraries(gproshan_cpp embree) + target_link_libraries(gproshan embree) endif(embree_FOUND) if(CUDAToolkit_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) - add_library(gproshan_cu STATIC ${cu_sources}) - target_compile_options(gproshan_cu PRIVATE -Xcompiler -fopenmp) - set_target_properties(gproshan_cu PROPERTIES CUDA_SEPARABLE_COMPILATION ON) - set_property(TARGET gproshan_cu PROPERTY CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80) - - target_link_libraries(gproshan_cu gproshan_cpp) - target_link_libraries(gproshan_cu CUDA::cudart) - target_link_libraries(gproshan_cu CUDA::cublas) - target_link_libraries(gproshan_cu CUDA::cusolver) - target_link_libraries(gproshan_cu CUDA::cusparse) + target_link_libraries(gproshan CUDA::cudart) + target_link_libraries(gproshan CUDA::cublas) + target_link_libraries(gproshan CUDA::cusolver) + target_link_libraries(gproshan CUDA::cusparse) endif(CUDAToolkit_FOUND) -add_executable(gproshan gproshan.cpp) +add_executable(gproshan_viewer gproshan.cpp) add_executable(test_geodesics test_geodesics.cpp) add_executable(test_image_denoising test_image_denoising.cpp) -target_link_libraries(gproshan gproshan_cpp) -target_link_libraries(test_geodesics gproshan_cpp) -target_link_libraries(test_image_denoising gproshan_cpp) - -if(CUDAToolkit_FOUND) - target_link_libraries(gproshan gproshan_cu) - target_link_libraries(test_geodesics gproshan_cu) - target_link_libraries(test_image_denoising gproshan_cu) -endif(CUDAToolkit_FOUND) +target_link_libraries(gproshan_viewer gproshan) +target_link_libraries(test_geodesics gproshan) +target_link_libraries(test_image_denoising gproshan) file(MAKE_DIRECTORY tmp) From cb83f43b8c8ba78c7a796e0804815a7586bfa6c8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Feb 2021 17:55:11 +0100 Subject: [PATCH 0432/1018] gproshan shared lib and executable --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60ce0627..d3bf5e9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,7 +71,7 @@ include_directories(${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) -add_library(imgui STATIC ${imgui_sources}) +add_library(imgui SHARED ${imgui_sources}) set(ptx_code "") @@ -91,7 +91,7 @@ if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) endif(CUDAToolkit_FOUND) -add_library(gproshan STATIC ${cpp_sources} ${cu_sources} ${ptx_code}) +add_library(gproshan SHARED ${cpp_sources} ${cu_sources} ${ptx_code}) target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) @@ -117,11 +117,13 @@ if(CUDAToolkit_FOUND) target_link_libraries(gproshan CUDA::cusparse) endif(CUDAToolkit_FOUND) -add_executable(gproshan_viewer gproshan.cpp) +add_executable(gproshan_exe gproshan.cpp) +set_target_properties(gproshan_exe PROPERTIES OUTPUT_NAME gproshan) + add_executable(test_geodesics test_geodesics.cpp) add_executable(test_image_denoising test_image_denoising.cpp) -target_link_libraries(gproshan_viewer gproshan) +target_link_libraries(gproshan_exe gproshan) target_link_libraries(test_geodesics gproshan) target_link_libraries(test_image_denoising gproshan) From eb7f430b7722ca9650bd17dd207d4384205c1222 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Feb 2021 15:10:43 +0100 Subject: [PATCH 0433/1018] setting cmake for optix 7.2 --- CMakeLists.txt | 26 ++++++++++++-------------- cmake/FindOptiX.cmake | 2 +- src/raytracing/rt_optix.cpp | 6 +----- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d3bf5e9e..29550aa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,6 @@ if(CUDAToolkit_FOUND) find_package(OptiX 7.2) if(OptiX_INCLUDE) -# set(CUDA_HOST_COMPILER ${CMAKE_CUDA_HOST_COMPILER}) add_definitions(-DGPROSHAN_OPTIX) include_directories(${OptiX_INCLUDE}) endif(OptiX_INCLUDE) @@ -74,24 +73,22 @@ FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(imgui SHARED ${imgui_sources}) -set(ptx_code "") -if(OptiX_INCLUDE) - cuda_compile_ptx(ptx_files "${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu") - list(GET ptx_files 0 ptx_file) - set(ptx_code ptx_code.c) - add_custom_command( OUTPUT ${ptx_code} - COMMAND bin2c --const --padd 0 --type char --name ptx_code ${ptx_file} > ${ptx_code} - DEPENDS ${ptx_file} - ) -endif(OptiX_INCLUDE) - - FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) + list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) endif(CUDAToolkit_FOUND) -add_library(gproshan SHARED ${cpp_sources} ${cu_sources} ${ptx_code}) + +add_library(gproshan SHARED ${cpp_sources} ${cu_sources}) + + +if(OptiX_INCLUDE) + add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) + add_dependencies(gproshan ptx_sources) +endif(OptiX_INCLUDE) + target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) @@ -112,6 +109,7 @@ endif(embree_FOUND) if(CUDAToolkit_FOUND) target_link_libraries(gproshan CUDA::cudart) + target_link_libraries(gproshan CUDA::cuda_driver) target_link_libraries(gproshan CUDA::cublas) target_link_libraries(gproshan CUDA::cusolver) target_link_libraries(gproshan CUDA::cusparse) diff --git a/cmake/FindOptiX.cmake b/cmake/FindOptiX.cmake index eec01ecf..aeabce84 100644 --- a/cmake/FindOptiX.cmake +++ b/cmake/FindOptiX.cmake @@ -29,7 +29,7 @@ # Locate the OptiX distribution. Search relative to the SDK first, then look in the system. # Our initial guess will be within the SDK. -#set(OptiX_INSTALL_DIR "${CMAKE_SOURCE_DIR}/../" CACHE PATH "Path to OptiX installed location.") +set(OptiX_INSTALL_DIR $ENV{OptiX_INSTALL_DIR} CACHE PATH "Path to OptiX installed location.") # The distribution contains only 64 bit libraries. Error when we have been mis-configured. if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 377f0f53..f67e238b 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -9,9 +9,6 @@ #include -extern "C" char ptx_code[]; - - // geometry processing and shape analysis framework // raytracing approach namespace gproshan::rt { @@ -55,10 +52,9 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_launch_params"; - optix_pipeline_link_opt.overrideUsesMotionBlur = false; optix_pipeline_link_opt.maxTraceDepth = 2; - const std::string str_ptx_code = ptx_code; + const std::string str_ptx_code = ""; optixModuleCreateFromPTX( optix_context, &optix_module_compile_opt, From 7ea6bb2376c36c2cb1c074b55658921ef1437c90 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 12:18:57 +0100 Subject: [PATCH 0434/1018] rt_optix: load ptx code --- CMakeLists.txt | 29 +++++++++++++++++------------ src/raytracing/rt_optix.cpp | 13 ++++++++----- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 29550aa0..ec660898 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ include_directories(${EIGEN3_INCLUDE_DIR}) include_directories(${SuiteSparse_INCLUDE_DIRS}) include_directories(${CGAL_INCLUDE_DIRS}) + include_directories(${gproshan_SOURCE_DIR}/include) include_directories(${gproshan_SOURCE_DIR}/imgui) @@ -83,13 +84,6 @@ endif(CUDAToolkit_FOUND) add_library(gproshan SHARED ${cpp_sources} ${cu_sources}) -if(OptiX_INCLUDE) - add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) - set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - add_dependencies(gproshan ptx_sources) -endif(OptiX_INCLUDE) - - target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) target_link_libraries(gproshan OpenMP::OpenMP_CXX) @@ -115,15 +109,26 @@ if(CUDAToolkit_FOUND) target_link_libraries(gproshan CUDA::cusparse) endif(CUDAToolkit_FOUND) -add_executable(gproshan_exe gproshan.cpp) -set_target_properties(gproshan_exe PROPERTIES OUTPUT_NAME gproshan) +add_executable(test_gproshan gproshan.cpp) +set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) +target_link_libraries(test_gproshan gproshan) -add_executable(test_geodesics test_geodesics.cpp) -add_executable(test_image_denoising test_image_denoising.cpp) +if(OptiX_INCLUDE) + add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) + add_dependencies(test_gproshan ptx_sources) + add_custom_target(ptx_copy COMMAND ${CMAKE_COMMAND} -E copy $ $) + add_dependencies(test_gproshan ptx_copy) +endif(OptiX_INCLUDE) -target_link_libraries(gproshan_exe gproshan) + +add_executable(test_geodesics test_geodesics.cpp) target_link_libraries(test_geodesics gproshan) + + +add_executable(test_image_denoising test_image_denoising.cpp) target_link_libraries(test_image_denoising gproshan) + file(MAKE_DIRECTORY tmp) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index f67e238b..c03d5ebe 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -2,9 +2,10 @@ #ifdef GPROSHAN_OPTIX +#include #include +#include #include -#include #include @@ -52,9 +53,11 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_launch_params"; - optix_pipeline_link_opt.maxTraceDepth = 2; - - const std::string str_ptx_code = ""; + optix_pipeline_link_opt.maxTraceDepth = 2; + + std::ifstream ptx_is("rt_optix.ptx"); + const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); + ptx_is.close(); optixModuleCreateFromPTX( optix_context, &optix_module_compile_opt, @@ -201,7 +204,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces; optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; - optix_trig_flags = 0 ; + optix_trig_flags = 0; optix_mesh.triangleArray.flags = &optix_trig_flags; optix_mesh.triangleArray.numSbtRecords = 1; From 25f547e5629830d64d452d3e342bf17b431a2f64 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 13:06:11 +0100 Subject: [PATCH 0435/1018] app_viewer: virtual init method added --- CMakeLists.txt | 5 ++--- include/app_viewer.h | 10 +++++++--- src/app_viewer.cpp | 32 +++++++++++++++----------------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec660898..5ecbead3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,9 +116,8 @@ target_link_libraries(test_gproshan gproshan) if(OptiX_INCLUDE) add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - add_dependencies(test_gproshan ptx_sources) - add_custom_target(ptx_copy COMMAND ${CMAKE_COMMAND} -E copy $ $) - add_dependencies(test_gproshan ptx_copy) + add_custom_target(ptx_cp_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) + add_dependencies(test_gproshan ptx_cp_sources) endif(OptiX_INCLUDE) diff --git a/include/app_viewer.h b/include/app_viewer.h index bc7fd2d4..def5c845 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -37,16 +37,20 @@ namespace gproshan { class app_viewer : public viewer { - private: + protected: double time; public: - app_viewer(); + app_viewer() = default; ~app_viewer(); int main(int nargs, const char ** args); - private: + protected: + virtual void init(); + + che * load_mesh(const string & file_path); + static bool process_fairing_taubin(viewer * p_view); static bool process_fairing_spectral(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 92a10e5e..b19f41b9 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -11,7 +11,13 @@ using namespace gproshan::mdict; namespace gproshan { -che * load_mesh(const string & file_path) +app_viewer::~app_viewer() +{ + for(che * mesh: meshes) + delete mesh; +} + +che * app_viewer::load_mesh(const string & file_path) { size_t pos = file_path.rfind('.'); assert(pos != string::npos); @@ -26,16 +32,6 @@ che * load_mesh(const string & file_path) return new che_img(file_path); } -app_viewer::app_viewer() -{ -} - -app_viewer::~app_viewer() -{ - for(che * mesh: meshes) - delete mesh; -} - int app_viewer::main(int nargs, const char ** args) { if(nargs < 2) @@ -54,6 +50,14 @@ int app_viewer::main(int nargs, const char ** args) gproshan_log_var(sizeof(real_t)); gproshan_log_var(time); + init(); + run(); + + return 0; +} + +void app_viewer::init() +{ sub_menus.push_back("Fairing"); add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); @@ -106,12 +110,6 @@ int app_viewer::main(int nargs, const char ** args) add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", select_multiple}); - - - run(); - - - return 0; } bool paint_holes_vertices(viewer * p_view) From 6d630c705d8d50effc0042a2907b5d2cca835018 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 15:21:35 +0100 Subject: [PATCH 0436/1018] viewer: enable point size, using ImGui::CollapsingHeader instead of windows --- include/viewer/viewer.h | 1 + shaders/vertex_pointcloud.glsl | 2 ++ src/viewer/viewer.cpp | 23 +++++++++++++++-------- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index f7cff1fe..59c79428 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -84,6 +84,7 @@ class viewer bool action = false; + unsigned int point_size = 1; bool render_pointcloud = false; bool render_wireframe = false; bool render_wireframe_fill = false; diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index a74a31be..52f41cfe 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -12,6 +12,7 @@ out float vs_color; uniform mat4 model_view_mat; uniform mat4 proj_mat; +uniform uint point_size; void main() { @@ -21,5 +22,6 @@ void main() vs_color = in_color; gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); + gl_PointSize = point_size; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 939e6422..809f93b3 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -153,22 +153,27 @@ bool viewer::run() ImGui::EndMainMenuBar(); } + ImGui::SetNextWindowSize(ImVec2(320, -1)); + ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); + ImGui::Begin("gproshan"); + + if(render_pointcloud) + ImGui::SliderInt("point_size", (int *) &point_size, 1, 32); + for(auto & p: processes) { process_t & pro = p.second; - if(pro.selected) + if(ImGui::CollapsingHeader(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::SetNextWindowSize(ImVec2(320, -1)); - ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); - ImGui::Begin(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected); - + ImGui::Indent(); pro.selected = pro.selected && p.second.function(this); active_mesh().update_vbo(); - - ImGui::End(); + ImGui::Unindent(); } } - + + ImGui::End(); + // Rendering ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); @@ -233,6 +238,7 @@ void viewer::init_gl() glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); + glEnable(GL_PROGRAM_POINT_SIZE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } @@ -722,6 +728,7 @@ void viewer::render_gl() glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); + glProgramUniform1ui(shader_pointcloud, shader_pointcloud("point_size"), point_size); draw_meshes(shader_triangles); From c1e8ba927de153af082b85e4378f1b63a5d80960 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 19:41:37 +0100 Subject: [PATCH 0437/1018] viewer: fix imgui id on collapsingheader --- src/viewer/viewer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 809f93b3..db4189fe 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -165,10 +165,12 @@ bool viewer::run() process_t & pro = p.second; if(ImGui::CollapsingHeader(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) { + ImGui::PushID(pro.name.c_str()); ImGui::Indent(); pro.selected = pro.selected && p.second.function(this); active_mesh().update_vbo(); ImGui::Unindent(); + ImGui::PopID(); } } From da6f5ad9235c9d2e959e2c75a73a903c516293de Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 22:03:07 +0100 Subject: [PATCH 0438/1018] compiling with single precision --- include/config.h | 2 +- include/mdict/patch.h | 8 ++++---- include/viewer/che_viewer.h | 4 ++-- include/viewer/viewer.h | 6 ++++++ src/app_viewer.cpp | 24 ++++++++++++------------ src/features/descriptor.cpp | 2 +- src/mdict/msparse_coding.cpp | 10 +++++----- src/mdict/patch.cpp | 4 ++-- src/viewer/che_viewer.cpp | 10 +++++----- 9 files changed, 38 insertions(+), 32 deletions(-) diff --git a/include/config.h b/include/config.h index a0133ae5..fe8bc38e 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -//#define SINGLE_P +#define SINGLE_P // print log messages #define LOG diff --git a/include/mdict/patch.h b/include/mdict/patch.h index f951b2e4..e21fb4e1 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -77,7 +77,7 @@ class patch const real_t & area_mesh ); - void init_random(const vertex & c, const arma::mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr); + void init_random(const vertex & c, const a_mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr); void recover_radial_disjoint(che * mesh, const real_t & radio_, @@ -116,9 +116,9 @@ class patch const real_t * geo, che * mesh, const index_t & v, - double & area, - double & proj_area, - double deviation + real_t & area, + real_t & proj_area, + real_t deviation ); diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 90154ef3..a7744157 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -9,10 +9,10 @@ #ifdef SINGLE_P #define glVertex3v(x) glVertex3fv(x) - #define GL_VERTEX_T GL_FLOAT + #define GL_REAL GL_FLOAT #else #define glVertex3v(x) glVertex3dv(x) - #define GL_VERTEX_T GL_DOUBLE + #define GL_REAL GL_DOUBLE #endif diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 59c79428..a3ffb9e0 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -20,6 +20,12 @@ #include "imgui_impl_opengl3.h" +#ifdef SINGLE_P + #define ImGui_InputReal ImGui::InputFloat +#else + #define ImGui_InputReal ImGui::InputDouble +#endif // SINGLE_P + #define N_MESHES 12 diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b19f41b9..fdb831ba 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -452,12 +452,12 @@ bool app_viewer::process_msparse_coding(viewer * p_view) assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - ImGui::InputDouble("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); + ImGui_InputReal("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) @@ -487,9 +487,9 @@ bool app_viewer::process_mask(viewer * p_view) ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) @@ -530,13 +530,13 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui::InputDouble("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui::InputDouble("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui::InputDouble("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); - ImGui::InputDouble("percentage_size", &percentage_size, 100, 10, "%.3f"); - ImGui::InputDouble("radio_factor", &radio_factor, 1, 0.1, "%.3f"); + ImGui_InputReal("percentage_size", &percentage_size, 100, 10, "%.3f"); + ImGui_InputReal("radio_factor", &radio_factor, 1, 0.1, "%.3f"); if(ImGui::Button("Run")) { diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 83775d4c..5b981eb8 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -53,7 +53,7 @@ void descriptor::compute_wks() eigvec %= eigvec; // element wise product eigval = log(eigval); - a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); + a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); real_t sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference real_t sigma_2 = 2 * sigma * sigma; diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index df59abdb..ab406d68 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -356,7 +356,7 @@ void msparse_coding::init_radial_feature_patches() bool save_all = true; if(save_all) { - arma::mat AS; + a_mat AS; AS.resize(m_params.n_patches, 13); for(index_t i = 0; i < m_params.n_patches; i++) { @@ -542,8 +542,8 @@ real_t msparse_coding::execute() che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) { - arma::mat S; - arma::mat alpha; + a_mat S; + a_mat alpha; S.load(tmp_file_path(key_name + ".smp")); alpha.load(tmp_file_path(key_name + ".alpha")); @@ -566,7 +566,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) #pragma omp parallel for for(index_t i = 0; i < m_params.n_patches; i++) { - arma::mat T(3,3); + a_mat T(3,3); T(0,0) = S(i,4); T(1,0) = S(i,5); T(2,0) = S(i,6); @@ -941,7 +941,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) // simple means vertex if(patches_map[v].size() && (!mask || mask(v))) { - a_vec mv = arma::zeros(3); + a_vec mv = arma::zeros(3); for(auto p: patches_map[v]) mv += patches[p.first].xyz.col(p.second); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 53e23f1a..f465e738 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -76,7 +76,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, double & area, double & proj_area, double deviation) +bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -142,7 +142,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N return added; } -void patch::init_random(const vertex & c, const arma::mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr) +void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr) { this->radio = radio; this->T = T; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 0ad013b1..86271ea2 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -82,28 +82,28 @@ void che_viewer::update_vbo() glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(0, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(1, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 2 MESH COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->color(0), GL_STATIC_DRAW); glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(2, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 HEAT MAP COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 1, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(3, 1, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 3 INDEXES @@ -128,7 +128,7 @@ void che_viewer::update_instances_translations(const vector & translatio glBindBuffer(GL_ARRAY_BUFFER, vbo[5]); glBufferData(GL_ARRAY_BUFFER, n_instances * sizeof(vertex), translations.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 3, GL_VERTEX_T, GL_FALSE, 0, 0); + glVertexAttribPointer(3, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribDivisor(3, 1); From 50f047c4349873657aaabc04e7dcf852f8aada3c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 22:50:29 +0100 Subject: [PATCH 0439/1018] using cstdio to read ptx file --- src/mesh/che_ptx.cpp | 64 +++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index df39f8da..db705904 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -1,10 +1,9 @@ #include "mesh/che_ptx.h" -#include -#include -#include #include #include +#include +#include using namespace std; @@ -28,46 +27,43 @@ che_ptx::~che_ptx() void che_ptx::read_file(const string & file) { - ifstream is(file); - - assert(is.good()); + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); size_t n_rows, n_cols; - vertex T[4], R[3], tr; - real_t s; - - is >> n_rows >> n_cols; + float T[12], R[12], tr[4]; - is >> T[0] >> T[1] >> T[2] >> T[3]; - is >> R[0] >> s; - is >> R[1] >> s; - is >> R[2] >> s; - is >> tr >> s; - - real_t * points = new real_t[7 * n_rows * n_cols]; - - index_t p = 0; - while(is >> points[p++]); + fscanf(fp, "%lu %lu", &n_rows, &n_cols); + + for(index_t i = 0; i < 12; i++) + fscanf(fp, "%f", T + i); - // p is offset for [x y z a] or [x y z a r g b] point data - p = n_rows * n_cols * 4 == p ? 4 : 7; + for(index_t i = 0; i < 12; i++) + fscanf(fp, "%f", R + i); + + for(index_t i = 0; i < 4; i++) + fscanf(fp, "%f", tr + i); + init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); + + float values[7]; + char line[128]; - #pragma omp parallel for for(index_t v = 0; v < n_vertices; v++) { - const index_t & i = v * p; + fgets(line, sizeof(line), fp); - GT[v] = { points[i], points[i + 1], points[i + 2] }; + if(sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6) == 7) + VC[v] = { values[3], values[3], values[3] }; + else + VC[v] = { values[4], values[5], values[6] }; - if(p == 4) - VC[v] = { points[i + 3], points[i + 3], points[i + 3] }; - else - VC[v] = { points[i + 4], points[i + 5], points[i + 6] }; + GT[v] = { values[0], values[1], values[2] }; } - delete [] points; + fclose(fp); + index_t he = 0; auto add_trig = [&](const index_t & i, const index_t & j, const index_t & k) @@ -100,17 +96,11 @@ void che_ptx::read_file(const string & file) #pragma omp parallel for for(index_t i = 0; i < n_vertices; i++) - VC[i] /= 255; - - is.close(); + VC[i] /= 255; } void che_ptx::write_file(const che * mesh, const string & file) { - ofstream os(file + ".off"); - - - os.close(); } From 2f7ce2a70ec3935156782763195a21c76457397e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 19 Feb 2021 23:03:27 +0100 Subject: [PATCH 0440/1018] read ptx: reading rgb vertex color --- src/mesh/che_ptx.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index db705904..198b82aa 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -50,16 +50,37 @@ void che_ptx::read_file(const string & file) float values[7]; char line[128]; - for(index_t v = 0; v < n_vertices; v++) - { - fgets(line, sizeof(line), fp); + bool rgb = false; - if(sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6) == 7) - VC[v] = { values[3], values[3], values[3] }; - else - VC[v] = { values[4], values[5], values[6] }; + // vertex 0: x y z a or x y z r g b a + fgets(line, sizeof(line), fp); + rgb = sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6) == 7; - GT[v] = { values[0], values[1], values[2] }; + if(rgb) + { + GT[0] = { values[0], values[1], values[2] }; + VC[0] = { values[4], values[5], values[6] }; + + for(index_t v = 1; v < n_vertices; v++) + { + fgets(line, sizeof(line), fp); + sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6); + GT[v] = { values[0], values[1], values[2] }; + VC[v] = { values[4], values[5], values[6] }; + } + } + else + { + GT[0] = { values[0], values[1], values[2] }; + VC[0] = { values[4], values[5], values[6] }; + + for(index_t v = 1; v < n_vertices; v++) + { + fgets(line, sizeof(line), fp); + sscanf(line, "%f %f %f %f", values, values + 1, values + 2, values + 3); + GT[v] = { values[0], values[1], values[2] }; + VC[v] = { values[4], values[3], values[3] }; + } } fclose(fp); From 19d939cc0ee5cd7414cf82f70757ebb13adccc2e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 20 Feb 2021 13:38:49 +0100 Subject: [PATCH 0441/1018] macos: imgui linking libraries --- CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ecbead3..4d61141e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,6 @@ endif(embree_FOUND) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) -find_package(OpenGL REQUIRED) find_package(GLEW 2 REQUIRED) find_package(glfw3 3 REQUIRED) find_package(glm REQUIRED) @@ -56,7 +55,6 @@ find_package(Boost COMPONENTS thread system) set(THREADS_PREFER_PTHREAD_FLAG ON) -include_directories(${OPENGL_INCLUDE_DIR}) include_directories(${GLEW_INCLUDE_DIRS}) include_directories(${GLFW3_INCLUDE_DIRS}) include_directories(${X11_INCLUDE_DIR}) @@ -72,6 +70,8 @@ include_directories(${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) add_library(imgui SHARED ${imgui_sources}) +target_link_libraries(imgui GLEW::GLEW) +target_link_libraries(imgui glfw) FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) @@ -87,7 +87,6 @@ add_library(gproshan SHARED ${cpp_sources} ${cu_sources}) target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) target_link_libraries(gproshan OpenMP::OpenMP_CXX) -target_link_libraries(gproshan OpenGL::GL) target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) target_link_libraries(gproshan ${X11_X11_LIB}) From 1606a9751763ade03575320334ac5897533cb4c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 20 Feb 2021 13:41:19 +0100 Subject: [PATCH 0442/1018] solving clang warnings --- src/features/descriptor.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 5b981eb8..ba5f3aa5 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -38,7 +38,7 @@ void descriptor::compute_gps() void descriptor::compute_hks() { - eigvec %= eigvec; // element wise product + eigvec = eigvec % eigvec; // element wise product features.zeros(eigvec.n_rows, eigvec.n_cols); @@ -50,7 +50,7 @@ void descriptor::compute_hks() ///< http://imagine.enpc.fr/~aubrym/projects/wks/index.html void descriptor::compute_wks() { - eigvec %= eigvec; // element wise product + eigvec = eigvec % eigvec; // element wise product eigval = log(eigval); a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); From 29122048e3fe5930814e796ff40a87eafe3dd75c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 20 Feb 2021 14:56:33 +0100 Subject: [PATCH 0443/1018] enable heat method for single precision --- src/app_viewer.cpp | 4 +--- src/geodesics/heat_method.cpp | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index fdb831ba..a2bff5ed 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -36,7 +36,7 @@ int app_viewer::main(int nargs, const char ** args) { if(nargs < 2) { - printf("./gproshan [mesh_paths.(off,obj,ply)]\n"); + printf("./%s [mesh_paths.(off,obj,ply)]\n", args[0]); return 0; } @@ -65,9 +65,7 @@ void app_viewer::init() sub_menus.push_back("Geodesics"); add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); -#ifndef SINGLE_P add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_method}); -#endif #ifdef GPROSHAN_CUDA add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 1a6d0b3b..c9568ebf 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -34,12 +34,12 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) a_mat u(mesh->n_vertices, 1); cholmod_common context; - cholmod_l_start(&context); + cholmod_start(&context); double solve_time = 0; - solve_time += solve_positive_definite(u, A, u0, &context); // cholmod (suitesparse) - //assert(spsolve(u, A, u0)); // arma + //solve_time += solve_positive_definite(u, A, u0, &context); // cholmod (suitesparse) + assert(spsolve(u, A, u0)); // arma // extract geodesics @@ -48,14 +48,14 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) a_mat phi(dist, mesh->n_vertices, 1, false); - solve_time += solve_positive_definite(phi, L, div, &context); // cholmod (suitesparse) - //assert(spsolve(phi, L, div)); // arma + //solve_time += solve_positive_definite(phi, L, div, &context); // cholmod (suitesparse) + assert(spsolve(phi, L, div)); // arma real_t min_val = phi.min(); phi.for_each([&min_val](a_mat::elem_type & val) { val -= min_val; val *= 0.5; }); - //cholmod_l_gpu_stats(&context); - cholmod_l_finish(&context); + //cholmod_gpu_stats(&context); + cholmod_finish(&context); return solve_time; } @@ -129,8 +129,8 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c cholmod_dense * cb = arma_2_cholmod(b, context); - cholmod_factor * L = cholmod_l_analyze(cA, context); - cholmod_l_factorize(cA, L, context); + cholmod_factor * L = cholmod_analyze(cA, context); + cholmod_factorize(cA, L, context); /* fill ratio gproshan_debug_var(L->xsize); @@ -140,22 +140,22 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c double solve_time; TIC(solve_time) - cholmod_dense * cx = cholmod_l_solve(CHOLMOD_A, L, cb, context); + cholmod_dense * cx = cholmod_solve(CHOLMOD_A, L, cb, context); TOC(solve_time) assert(x.n_rows == b.n_rows); memcpy(x.memptr(), cx->x, x.n_rows * sizeof(real_t)); - cholmod_l_free_factor(&L, context); - cholmod_l_free_sparse(&cA, context); - cholmod_l_free_dense(&cb, context); + cholmod_free_factor(&L, context); + cholmod_free_sparse(&cA, context); + cholmod_free_dense(&cb, context); return solve_time; } cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) { - cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); + cholmod_dense * cD = cholmod_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); memcpy(cD->x, D.memptr(), D.n_elem * sizeof(real_t)); return cD; @@ -165,7 +165,7 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) { assert(sizeof(arma::uword) == sizeof(SuiteSparse_long)); - cholmod_sparse * cS = cholmod_l_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); + cholmod_sparse * cS = cholmod_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); memcpy(cS->p, S.col_ptrs, (S.n_cols + 1) * sizeof(arma::uword)); memcpy(cS->i, S.row_indices, S.n_nonzero * sizeof(arma::uword)); From 36b46bbd3eeb0f6bb1ea9a952e1d25964986c827 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 13:32:32 +0100 Subject: [PATCH 0444/1018] improving geodesics heat method code: arma, cholmod, cuda --- include/config.h | 2 +- include/geodesics/heat_method.h | 12 ++- src/geodesics/geodesics.cpp | 8 +- src/geodesics/heat_method.cpp | 108 +++++++++++---------------- src/geodesics/test_geodesics_ptp.cpp | 5 +- 5 files changed, 54 insertions(+), 81 deletions(-) diff --git a/include/config.h b/include/config.h index fe8bc38e..a0133ae5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -#define SINGLE_P +//#define SINGLE_P // print log messages #define LOG diff --git a/include/geodesics/heat_method.h b/include/geodesics/heat_method.h index 97ddbb72..20c86c27 100644 --- a/include/geodesics/heat_method.h +++ b/include/geodesics/heat_method.h @@ -13,20 +13,17 @@ #include "mesh/che.h" #include "include_arma.h" -#include // suitesparse/cholmod.h +#include // geometry processing and shape analysis framework namespace gproshan { +enum heat_method_opt { HEAT_ARMA, HEAT_CHOLMOD, HEAT_CUDA }; -double heat_method(real_t * dist, che * mesh, const std::vector & sources); +double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); -#ifdef GPROSHAN_CUDA -real_t * heat_method_gpu(che * mesh, const std::vector & sources, double & solve_time); -#endif // GPROSHAN_CUDA - -void compute_divergence(che * mesh, const a_mat & u, a_mat & div); +void compute_divergence(const che * mesh, const a_mat & u, a_mat & div); /// cholmod Keenan implementation /// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics @@ -37,6 +34,7 @@ cholmod_dense * arma_2_cholmod(const a_mat & m, cholmod_common * context); cholmod_sparse * arma_2_cholmod(const a_sp_mat & m, cholmod_common * context); #ifdef GPROSHAN_CUDA + /// double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b); #endif // GPROSHAN_CUDA diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 270f6c2c..2265e075 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -215,7 +215,7 @@ void geodesics::run_heat_method(che * mesh, const vector & sources) { double time_total, solve_time; TIC(time_total) - solve_time = heat_method(dist, mesh, sources); + solve_time = heat_method(dist, mesh, sources, HEAT_CHOLMOD); TOC(time_total) gproshan_log_var(time_total - solve_time); @@ -246,11 +246,11 @@ void geodesics::run_heat_method_gpu(che * mesh, const vector & sources) { double time_total, solve_time; TIC(time_total) - dist = heat_method_gpu(mesh, sources, solve_time); + solve_time = heat_method(dist, mesh, sources, HEAT_ARMA); TOC(time_total) - gproshan_debug_var(time_total - solve_time); - gproshan_debug_var(solve_time); + gproshan_log_var(time_total - solve_time); + gproshan_log_var(solve_time); } #endif // GPROSHAN_CUDA diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index c9568ebf..7be84854 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -double heat_method(real_t * dist, che * mesh, const vector & sources) +double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) { if(!sources.size()) return 0; @@ -34,12 +34,24 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) a_mat u(mesh->n_vertices, 1); cholmod_common context; - cholmod_start(&context); + cholmod_l_start(&context); double solve_time = 0; - - //solve_time += solve_positive_definite(u, A, u0, &context); // cholmod (suitesparse) - assert(spsolve(u, A, u0)); // arma + + switch(opt) + { + case HEAT_ARMA: + if(spsolve(u, A, u0)) gproshan_error(arma: no solution); + break; + case HEAT_CHOLMOD: + solve_time += solve_positive_definite(u, A, u0, &context); + break; + #ifdef GPROSHAN_CUDA + case HEAT_CUDA: + solve_time += solve_positive_definite_gpu(u, A, u0); + break; + #endif // GPROSHAN_CUDA + } // extract geodesics @@ -48,66 +60,30 @@ double heat_method(real_t * dist, che * mesh, const vector & sources) a_mat phi(dist, mesh->n_vertices, 1, false); - //solve_time += solve_positive_definite(phi, L, div, &context); // cholmod (suitesparse) - assert(spsolve(phi, L, div)); // arma - + switch(opt) + { + case HEAT_ARMA: + if(spsolve(phi, L, div)) gproshan_error(arma: no solution); + break; + case HEAT_CHOLMOD: + solve_time += solve_positive_definite(phi, L, div, &context); + break; + #ifdef GPROSHAN_CUDA + case HEAT_CUDA: + solve_time += solve_positive_definite_gpu(phi, L, div); + break; + #endif // GPROSHAN_CUDA + } + real_t min_val = phi.min(); phi.for_each([&min_val](a_mat::elem_type & val) { val -= min_val; val *= 0.5; }); - //cholmod_gpu_stats(&context); - cholmod_finish(&context); + cholmod_l_finish(&context); return solve_time; } -#ifdef GPROSHAN_CUDA - -real_t * heat_method_gpu(che * mesh, const vector & sources, double & solve_time) -{ - if(!sources.size()) return 0; - - // build impulse signal - a_mat u0(mesh->n_vertices, 1, arma::fill::zeros); - for(auto & v: sources) u0(v) = 1; - - // step - real_t dt = mesh->mean_edge(); - dt *= dt; - - a_sp_mat L, A; - laplacian(mesh, L, A); - - // make L positive-definite - L += 1.0e-8 * A; - - // heat flow for short interval - A += dt * L; - a_mat u(mesh->n_vertices, 1); - - solve_time = 0; - - solve_time += solve_positive_definite_gpu(u, A, u0); // cusorlver (cusparse) - - // extract geodesics - real_t * dist = new real_t[mesh->n_vertices]; - - a_mat div(mesh->n_vertices, 1); - compute_divergence(mesh, u, div); - - a_mat phi(dist, mesh->n_vertices, 1, false); - - solve_time += solve_positive_definite_gpu(phi, L, div); // cusolver (cusparse) - - real_t min_val = phi.min(); - phi.for_each([&min_val](a_mat::elem_type & val) { val -= min_val; val *= 0.5; }); - - return dist; -} - -#endif // GPROSHAN_CUDA - - -void compute_divergence(che * mesh, const a_mat & u, a_mat & div) +void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) { for(index_t v = 0; v < mesh->n_vertices; v++) { @@ -129,8 +105,8 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c cholmod_dense * cb = arma_2_cholmod(b, context); - cholmod_factor * L = cholmod_analyze(cA, context); - cholmod_factorize(cA, L, context); + cholmod_factor * L = cholmod_l_analyze(cA, context); + cholmod_l_factorize(cA, L, context); /* fill ratio gproshan_debug_var(L->xsize); @@ -140,22 +116,22 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c double solve_time; TIC(solve_time) - cholmod_dense * cx = cholmod_solve(CHOLMOD_A, L, cb, context); + cholmod_dense * cx = cholmod_l_solve(CHOLMOD_A, L, cb, context); TOC(solve_time) assert(x.n_rows == b.n_rows); memcpy(x.memptr(), cx->x, x.n_rows * sizeof(real_t)); - cholmod_free_factor(&L, context); - cholmod_free_sparse(&cA, context); - cholmod_free_dense(&cb, context); + cholmod_l_free_factor(&L, context); + cholmod_l_free_sparse(&cA, context); + cholmod_l_free_dense(&cb, context); return solve_time; } cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) { - cholmod_dense * cD = cholmod_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); + cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); memcpy(cD->x, D.memptr(), D.n_elem * sizeof(real_t)); return cD; @@ -165,7 +141,7 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) { assert(sizeof(arma::uword) == sizeof(SuiteSparse_long)); - cholmod_sparse * cS = cholmod_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); + cholmod_sparse * cS = cholmod_l_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); memcpy(cS->p, S.col_ptrs, (S.n_cols + 1) * sizeof(arma::uword)); memcpy(cS->i, S.row_indices, S.n_nonzero * sizeof(arma::uword)); diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 1f31dd4e..5e62cade 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -282,7 +282,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; i++) { - TIC(t) st = heat_method(dist, mesh, source); TOC(t) + TIC(t) st = heat_method(dist, mesh, source, HEAT_CHOLMOD); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); } @@ -325,8 +325,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact { if(dist) delete [] dist; - TIC(t) dist = heat_method_gpu(mesh, source, st); TOC(t) - + TIC(t) st = heat_method(dist, mesh, source, HEAT_CUDA); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); } From cef3b8fddc248797d383420b0382bde93f8b3f83 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 17:06:58 +0100 Subject: [PATCH 0445/1018] added util.h copy_real_t_array --- include/config.h | 2 +- include/util.h | 23 ++++++++++++++++++++++ src/geodesics/heat_method.cpp | 11 ++++++----- src/util.cpp | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 include/util.h create mode 100644 src/util.cpp diff --git a/include/config.h b/include/config.h index a0133ae5..fe8bc38e 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -//#define SINGLE_P +#define SINGLE_P // print log messages #define LOG diff --git a/include/util.h b/include/util.h new file mode 100644 index 00000000..528ea636 --- /dev/null +++ b/include/util.h @@ -0,0 +1,23 @@ +#ifndef UTIL_H +#define UTIL_H + +#include "include.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +void copy_real_t_array(float * destination, const float * source, const size_t & n_elem); + +void copy_real_t_array(float * destination, const double * source, const size_t & n_elem); + +void copy_real_t_array(double * destination, const float * source, const size_t & n_elem); + +void copy_real_t_array(double * destination, const double * source, const size_t & n_elem); + + +} // namespace gproshan + +#endif // UTIL_H + diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 7be84854..0bbc89ab 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -1,5 +1,6 @@ #include "geodesics/heat_method.h" +#include "util.h" #include "laplacian/laplacian.h" #include @@ -41,7 +42,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & switch(opt) { case HEAT_ARMA: - if(spsolve(u, A, u0)) gproshan_error(arma: no solution); + if(!spsolve(u, A, u0)) gproshan_error(arma: no solution); break; case HEAT_CHOLMOD: solve_time += solve_positive_definite(u, A, u0, &context); @@ -63,7 +64,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & switch(opt) { case HEAT_ARMA: - if(spsolve(phi, L, div)) gproshan_error(arma: no solution); + if(!spsolve(phi, L, div)) gproshan_error(arma: no solution); break; case HEAT_CHOLMOD: solve_time += solve_positive_definite(phi, L, div, &context); @@ -120,7 +121,7 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c TOC(solve_time) assert(x.n_rows == b.n_rows); - memcpy(x.memptr(), cx->x, x.n_rows * sizeof(real_t)); + copy_real_t_array(x.memptr(), (double *) cx->x, x.n_rows); cholmod_l_free_factor(&L, context); cholmod_l_free_sparse(&cA, context); @@ -132,7 +133,7 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) { cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); - memcpy(cD->x, D.memptr(), D.n_elem * sizeof(real_t)); + copy_real_t_array((double *) cD->x, D.memptr(), D.n_elem); return cD; } @@ -143,9 +144,9 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) cholmod_sparse * cS = cholmod_l_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); + copy_real_t_array((double *) cS->x, S.values, S.n_nonzero); memcpy(cS->p, S.col_ptrs, (S.n_cols + 1) * sizeof(arma::uword)); memcpy(cS->i, S.row_indices, S.n_nonzero * sizeof(arma::uword)); - memcpy(cS->x, S.values, S.n_nonzero * sizeof(real_t)); return cS; } diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 00000000..60409775 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,36 @@ +#include "util.h" + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +void cp_real_t_array(float * destination, const float * source, const size_t & n_elem) +{ + memcpy(destination, source, n_elem * sizeof(float)); +} + +void cp_real_t_array(float * destination, const double * source, const size_t & n_elem) +{ + #pragma omp parallel for + for(index_t i = 0; i < n_elem; i++) + destination[i] = source[i]; +} + +void cp_real_t_array(double * destination, const float * source, const size_t & n_elem) +{ + #pragma omp parallel for + for(index_t i = 0; i < n_elem; i++) + destination[i] = source[i]; +} + +void cp_real_t_array(double * destination, const double * source, const size_t & n_elem) +{ + memcpy(destination, source, n_elem * sizeof(double)); +} + + +} // namespace gproshan + From f2cf50cd750ff9c8839693c4b26cc87b00ffe827 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 20:02:18 +0100 Subject: [PATCH 0446/1018] heat method: float precision only works for small meshes --- src/geodesics/heat_method.cpp | 11 ++++++----- src/util.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 0bbc89ab..370ec0b9 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -17,7 +17,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & if(!sources.size()) return 0; // build impulse signal - a_mat u0(mesh->n_vertices, 1, arma::fill::zeros); + a_vec u0(mesh->n_vertices, arma::fill::zeros); for(auto & v: sources) u0(v) = 1; // step @@ -32,7 +32,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & // heat flow for short interval A += dt * L; - a_mat u(mesh->n_vertices, 1); + a_vec u(mesh->n_vertices); cholmod_common context; cholmod_l_start(&context); @@ -56,10 +56,10 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & // extract geodesics - a_mat div(mesh->n_vertices, 1); + a_vec div(mesh->n_vertices); compute_divergence(mesh, u, div); - a_mat phi(dist, mesh->n_vertices, 1, false); + a_vec phi(dist, mesh->n_vertices, false); switch(opt) { @@ -77,7 +77,8 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & } real_t min_val = phi.min(); - phi.for_each([&min_val](a_mat::elem_type & val) { val -= min_val; val *= 0.5; }); + phi -= min_val; + phi *= 0.5; cholmod_l_finish(&context); diff --git a/src/util.cpp b/src/util.cpp index 60409775..b533db37 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -7,26 +7,26 @@ namespace gproshan { -void cp_real_t_array(float * destination, const float * source, const size_t & n_elem) +void copy_real_t_array(float * destination, const float * source, const size_t & n_elem) { memcpy(destination, source, n_elem * sizeof(float)); } -void cp_real_t_array(float * destination, const double * source, const size_t & n_elem) +void copy_real_t_array(float * destination, const double * source, const size_t & n_elem) { #pragma omp parallel for for(index_t i = 0; i < n_elem; i++) destination[i] = source[i]; } -void cp_real_t_array(double * destination, const float * source, const size_t & n_elem) +void copy_real_t_array(double * destination, const float * source, const size_t & n_elem) { #pragma omp parallel for for(index_t i = 0; i < n_elem; i++) destination[i] = source[i]; } -void cp_real_t_array(double * destination, const double * source, const size_t & n_elem) +void copy_real_t_array(double * destination, const double * source, const size_t & n_elem) { memcpy(destination, source, n_elem * sizeof(double)); } From 1a10158d93033db3adcd7ca653ac357928441592 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 20:19:40 +0100 Subject: [PATCH 0447/1018] using copy_real_t_array --- src/geodesics/geodesics.cpp | 2 +- src/raytracing/rt_embree.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 2265e075..f4129a46 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -246,7 +246,7 @@ void geodesics::run_heat_method_gpu(che * mesh, const vector & sources) { double time_total, solve_time; TIC(time_total) - solve_time = heat_method(dist, mesh, sources, HEAT_ARMA); + solve_time = heat_method(dist, mesh, sources, HEAT_CUDA); TOC(time_total) gproshan_log_var(time_total - solve_time); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 636362c7..fcf304da 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -2,6 +2,8 @@ #ifdef GPROSHAN_EMBREE +#include "util.h" + #include #include #include @@ -148,14 +150,8 @@ index_t embree::add_mesh(const che * mesh) RTC_FORMAT_UINT3, 3 * sizeof(index_t), mesh->n_faces ); -#ifdef SINGLE_P - memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); -#else - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; i++) - vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); -#endif // SINGLE_P - + + copy_real_t_array((float *) vertices, (real_t *) &mesh->gt(0), 3 * mesh->n_vertices); memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); From c548ba0bb85b6ccddf361d7135d84bfb602aca20 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 22:25:55 +0100 Subject: [PATCH 0448/1018] setting definition GPROSHAN_DIR, used to find the shaders dir and tmp dir --- CMakeLists.txt | 2 ++ include/include.h | 4 ++-- include/viewer/shader.h | 10 +++++----- include/viewer/viewer.h | 1 + shaders/fragment_pointcloud.glsl | 18 +++++++++++------- src/app_viewer.cpp | 2 +- src/mesh/che_ptx.cpp | 29 +++++++++++++++-------------- src/viewer/frame.cpp | 5 +++-- src/viewer/shader.cpp | 10 +++++----- src/viewer/viewer.cpp | 30 +++++++++++++++++------------- 10 files changed, 62 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d61141e..3e6bfcdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,6 +74,8 @@ target_link_libraries(imgui GLEW::GLEW) target_link_libraries(imgui glfw) +add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") + FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) diff --git a/include/include.h b/include/include.h index 2d40507e..73817b2f 100644 --- a/include/include.h +++ b/include/include.h @@ -24,8 +24,8 @@ typedef unsigned int index_t; #endif -#define TMP_DIR "../tmp/" -#define tmp_file_path(file) (std::string(TMP_DIR) + file) +#define tmp_file_path(file) (std::string(GPROSHAN_DIR) + "/tmp/" + file) +#define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) #ifdef LOG #define gproshan_log_var(vari) std::cerr << "\033[0;33m[LOG] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << std::endl diff --git a/include/viewer/shader.h b/include/viewer/shader.h index 9f77d671..5a1b7647 100644 --- a/include/viewer/shader.h +++ b/include/viewer/shader.h @@ -22,15 +22,15 @@ class shader ~shader(); const GLint & operator () (const std::string & name); operator GLuint () const; - void load_vertex(const char * filename); - void load_fragment(const char * filename); - void load_geometry(const char * filename); + void load_vertex(const std::string & filename); + void load_fragment(const std::string & filename); + void load_geometry(const std::string & filename); void enable(); void disable() const; protected: - bool load(GLenum shader_type, const char * filename); - bool read_source(const char * filename, std::string & source); + bool load(GLenum shader_type, const std::string & filename); + bool read_source(const std::string & filename, std::string & source); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index a3ffb9e0..4ef67426 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -90,6 +90,7 @@ class viewer bool action = false; + bool point_normals = true; unsigned int point_size = 1; bool render_pointcloud = false; bool render_wireframe = false; diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 72824ae1..91114dfa 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -13,6 +13,7 @@ uniform uint idx_colormap; uniform vec3 eye; uniform vec3 light; uniform bool render_lines; +uniform bool point_normals; float diffuse(vec3 N, vec3 L) { @@ -49,16 +50,19 @@ void main() h = 1 - h; color = vec3(0) + (1. - h) * color; } - - vec3 N = normalize(vs_normal); - vec3 L = normalize(light - vs_position); - vec3 E = normalize(eye - vs_position); - vec3 R = 2 * dot(L, N) * N - L; - vec3 one = vec3(1); + if(point_normals) + { + vec3 N = normalize(vs_normal); + + vec3 L = normalize(light - vs_position); + vec3 E = normalize(eye - vs_position); + vec3 R = 2 * dot(L, N) * N - L; + vec3 one = vec3(1); + color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + } - color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; frag_color = vec4(color, 1); } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a2bff5ed..c27688c1 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -36,7 +36,7 @@ int app_viewer::main(int nargs, const char ** args) { if(nargs < 2) { - printf("./%s [mesh_paths.(off,obj,ply)]\n", args[0]); + printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); return 0; } diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 198b82aa..73a917db 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -47,39 +47,40 @@ void che_ptx::read_file(const string & file) init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); - float values[7]; + float x, y, z, a, r, g, b; char line[128]; bool rgb = false; - // vertex 0: x y z a or x y z r g b a + // vertex 0: x y z a or x y z a r g b fgets(line, sizeof(line), fp); - rgb = sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6) == 7; - + fgets(line, sizeof(line), fp); + rgb = sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b) == 7; + if(rgb) { - GT[0] = { values[0], values[1], values[2] }; - VC[0] = { values[4], values[5], values[6] }; + GT[0] = { x, y, z }; + VC[0] = { r, g, b }; for(index_t v = 1; v < n_vertices; v++) { fgets(line, sizeof(line), fp); - sscanf(line, "%f %f %f %f %f %f %f", values, values + 1, values + 2, values + 3, values + 4, values + 5, values + 6); - GT[v] = { values[0], values[1], values[2] }; - VC[v] = { values[4], values[5], values[6] }; + sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b); + GT[v] = { x, y, z }; + VC[v] = { r, g, b }; } } else { - GT[0] = { values[0], values[1], values[2] }; - VC[0] = { values[4], values[5], values[6] }; + GT[0] = { x, y, z }; + VC[0] = { a, a, a }; for(index_t v = 1; v < n_vertices; v++) { fgets(line, sizeof(line), fp); - sscanf(line, "%f %f %f %f", values, values + 1, values + 2, values + 3); - GT[v] = { values[0], values[1], values[2] }; - VC[v] = { values[4], values[3], values[3] }; + sscanf(line, "%f %f %f %f", &x, &y, &z, &a); + GT[v] = { x, y, z }; + VC[v] = { a, a, a }; } } diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index e3d67dd3..f75fbaa2 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -1,5 +1,6 @@ #include "viewer/frame.h" +#include "include.h" // geometry processing and shape analysis framework namespace gproshan { @@ -10,8 +11,8 @@ frame::frame() render_tex = 0; vbo = 0; - program.load_vertex("../shaders/vertex_frame.glsl"); - program.load_fragment("../shaders/fragment_frame.glsl"); + program.load_vertex(shaders_path("vertex_frame.glsl")); + program.load_fragment(shaders_path("fragment_frame.glsl")); const GLfloat vertices[] = { diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index dd558660..1f7f381e 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -31,17 +31,17 @@ shader::operator GLuint() const return program; } -void shader::load_vertex(const char * filename) +void shader::load_vertex(const std::string & filename) { load(GL_VERTEX_SHADER, filename); } -void shader::load_fragment(const char * filename) +void shader::load_fragment(const std::string & filename) { load(GL_FRAGMENT_SHADER, filename); } -void shader::load_geometry(const char * filename) +void shader::load_geometry(const std::string & filename) { load(GL_GEOMETRY_SHADER_EXT, filename); } @@ -62,7 +62,7 @@ void shader::disable() const glUseProgram(0); } -bool shader::load(GLenum shader_type, const char * filename) +bool shader::load(GLenum shader_type, const std::string & filename) { string source; @@ -119,7 +119,7 @@ bool shader::load(GLenum shader_type, const char * filename) return true; } -bool shader::read_source(const char * filename, std::string & source) +bool shader::read_source(const std::string & filename, std::string & source) { ifstream is(filename); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index db4189fe..f3c5287c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -158,7 +158,10 @@ bool viewer::run() ImGui::Begin("gproshan"); if(render_pointcloud) + { + ImGui::Checkbox("point_normals", &point_normals); ImGui::SliderInt("point_size", (int *) &point_size, 1, 32); + } for(auto & p: processes) { @@ -290,23 +293,23 @@ void viewer::init_menus() void viewer::init_glsl() { - shader_sphere.load_vertex("../shaders/vertex_sphere.glsl"); - shader_sphere.load_fragment("../shaders/fragment_sphere.glsl"); + shader_sphere.load_vertex(shaders_path("vertex_sphere.glsl")); + shader_sphere.load_fragment(shaders_path("fragment_sphere.glsl")); - shader_triangles.load_vertex("../shaders/vertex.glsl"); - shader_triangles.load_geometry("../shaders/geometry.glsl"); - shader_triangles.load_fragment("../shaders/fragment.glsl"); + shader_triangles.load_vertex(shaders_path("vertex.glsl")); + shader_triangles.load_geometry(shaders_path("geometry.glsl")); + shader_triangles.load_fragment(shaders_path("fragment.glsl")); - shader_normals.load_vertex("../shaders/vertex_normals.glsl"); - shader_normals.load_geometry("../shaders/geometry_normals.glsl"); - shader_normals.load_fragment("../shaders/fragment_normals.glsl"); + shader_normals.load_vertex(shaders_path("vertex_normals.glsl")); + shader_normals.load_geometry(shaders_path("geometry_normals.glsl")); + shader_normals.load_fragment(shaders_path("fragment_normals.glsl")); - shader_gradient.load_vertex("../shaders/vertex_gradient.glsl"); - shader_gradient.load_geometry("../shaders/geometry_gradient.glsl"); - shader_gradient.load_fragment("../shaders/fragment_gradient.glsl"); + shader_gradient.load_vertex(shaders_path("vertex_gradient.glsl")); + shader_gradient.load_geometry(shaders_path("geometry_gradient.glsl")); + shader_gradient.load_fragment(shaders_path("fragment_gradient.glsl")); - shader_pointcloud.load_vertex("../shaders/vertex_pointcloud.glsl"); - shader_pointcloud.load_fragment("../shaders/fragment_pointcloud.glsl"); + shader_pointcloud.load_vertex(shaders_path("vertex_pointcloud.glsl")); + shader_pointcloud.load_fragment(shaders_path("fragment_pointcloud.glsl")); } void viewer::add_process(const int & key, const process_t & process) @@ -730,6 +733,7 @@ void viewer::render_gl() glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); + glProgramUniform1i(shader_pointcloud, shader_pointcloud("point_normals"), point_normals); glProgramUniform1ui(shader_pointcloud, shader_pointcloud("point_size"), point_size); From 71ab81a8276ed86f65db574a81f1ac54b8a45cda Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Feb 2021 23:59:29 +0100 Subject: [PATCH 0449/1018] fast loading che off file --- include/config.h | 2 +- src/mesh/che_off.cpp | 57 +++++++++++++++++++++++++------------------- src/mesh/che_ptx.cpp | 1 - 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/include/config.h b/include/config.h index fe8bc38e..a0133ae5 100644 --- a/include/config.h +++ b/include/config.h @@ -5,7 +5,7 @@ //#define NDEBUG // uncomment this line to compile gproshan with single precision (float) -#define SINGLE_P +//#define SINGLE_P // print log messages #define LOG diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index ae2bf44b..fa86c627 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -1,9 +1,9 @@ #include "mesh/che_off.h" -#include -#include #include #include +#include +#include using namespace std; @@ -27,25 +27,34 @@ che_off::~che_off() void che_off::read_file(const string & file) { - string soff; - size_t n_v, n_f, v; - - ifstream is(file); - - assert(is.good()); + char soff[32]; + size_t nv, nf, ne; + + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); - is >> soff; - is >> n_v >> n_f >> v; - init(n_v, n_f); + fgets(soff, sizeof(soff), fp); + fscanf(fp, "%lu %lu %lu", &nv, &nf, &ne); + + init(nv, nf); - real_t alpha; // color - for(index_t i = 0; i < n_vertices; i++) + float x, y, z, r, g, b, a; + for(index_t v = 0; v < n_vertices; v++) { - is >> GT[i]; + fscanf(fp, "%f %f %f", &x, &y, &z); + GT[v] = { x, y, z }; + if(soff[0] == 'C' || soff[1] == 'C') - is >> VC[i] >> alpha; + { + fscanf(fp, "%f %f %f %f", &r, &g, &b, &a); + VC[v] = { r, g, b }; + } + if(soff[0] == 'N') - is >> VN[i]; + { + fscanf(fp, "%f %f %f", &x, &y, &z); + VN[v] = { x, y, z }; + } } if(soff[0] == 'C' || soff[1] == 'C') @@ -58,31 +67,31 @@ void che_off::read_file(const string & file) index_t he = 0; for(index_t i = 0; i < n_faces; i++) { - is >> v; - if(!i && v > che::mtrig) + fscanf(fp, "%lu", &ne); + if(!i && ne > che::mtrig) { vertex * tGT = GT; GT = nullptr; delete_me(); - init(n_v, n_f * (v - che::mtrig + 1)); + init(nv, nf * (ne - che::mtrig + 1)); GT = tGT; } - for(index_t j = 0; j < v; j++) - is >> VT[he++]; + for(index_t j = 0; j < ne; j++) + fscanf(fp, "%u", VT + he++); // divide face - if(v == che::mquad) + if(ne == che::mquad) { - VT[he] = VT[he - v]; he++; + VT[he] = VT[he - ne]; he++; VT[he] = VT[he - che::mtrig]; he++; i++; } } - is.close(); + fclose(fp); } void che_off::write_file(const che * mesh, const string & file, const che_off::type & off, const bool & pointcloud) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 73a917db..83c647f1 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; From 2e4e841cf48221ec02a0e7356b104d975c6b1cbc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Feb 2021 00:28:06 +0100 Subject: [PATCH 0450/1018] adding install cmake --- .gitignore | 9 ++++----- CMakeLists.txt | 7 +++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index c5b6cd07..1af9b0f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,11 @@ -build*/* -obj/* +build/* +bin/* lib/* doc/* tmp/* *.swp *.swo *.user -.vscode/* -.DS_Store +*.vscode/* +*.DS_Store -src/.vscode/settings.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e6bfcdd..5a31a3f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ if(CUDAToolkit_FOUND) target_link_libraries(gproshan CUDA::cusparse) endif(CUDAToolkit_FOUND) + add_executable(test_gproshan gproshan.cpp) set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) target_link_libraries(test_gproshan gproshan) @@ -130,5 +131,11 @@ add_executable(test_image_denoising test_image_denoising.cpp) target_link_libraries(test_image_denoising gproshan) +install(TARGETS gproshan imgui test_gproshan test_geodesics test_image_denoising + ARCHIVE DESTINATION ${gproshan_SOURCE_DIR}/lib + LIBRARY DESTINATION ${gproshan_SOURCE_DIR}/lib + RUNTIME DESTINATION ${gproshan_SOURCE_DIR}/bin) + + file(MAKE_DIRECTORY tmp) From 7be166a1d147cf15cff9af02ff37db9c956b0f93 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Feb 2021 01:00:24 +0100 Subject: [PATCH 0451/1018] full path shaders --- shaders/fragment.glsl | 2 +- shaders/fragment_pointcloud.glsl | 2 +- src/viewer/shader.cpp | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index cfed274c..ef0561f9 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,6 +1,6 @@ #version 410 core -#include ../shaders/colormap.glsl +#include colormap.glsl in vec3 gs_position; in vec3 gs_normal; diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 91114dfa..ef995f46 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -1,6 +1,6 @@ #version 410 core -#include ../shaders/colormap.glsl +#include colormap.glsl in vec3 vs_position; in vec3 vs_normal; diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 1f7f381e..9628dd1b 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -1,5 +1,7 @@ #include "viewer/shader.h" +#include "include.h" + #include #include #include @@ -137,7 +139,7 @@ bool shader::read_source(const std::string & filename, std::string & source) if(include == "#include") { ss >> include; - if(read_source(include.c_str(), include)) + if(read_source(shaders_path(include), include)) source += include + '\n'; } else From 041285d6e9baf7c08cf78e9c852b1519b43b0dbe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Feb 2021 12:45:52 +0100 Subject: [PATCH 0452/1018] update imgui to latest version 1.81 --- imgui/LICENSE.txt | 2 +- imgui/imconfig.h | 15 +- imgui/imgui.cpp | 849 ++++++-- imgui/imgui.h | 541 ++++- imgui/imgui_demo.cpp | 2836 ++++++++++++++++++++---- imgui/imgui_draw.cpp | 402 +++- imgui/imgui_impl_glfw.cpp | 7 +- imgui/imgui_impl_glfw.h | 3 +- imgui/imgui_impl_opengl3.cpp | 18 +- imgui/imgui_internal.h | 530 ++++- imgui/imgui_tables.cpp | 3950 ++++++++++++++++++++++++++++++++++ imgui/imgui_widgets.cpp | 720 ++----- imgui/imstb_truetype.h | 2 +- 13 files changed, 8418 insertions(+), 1457 deletions(-) create mode 100644 imgui/imgui_tables.cpp diff --git a/imgui/LICENSE.txt b/imgui/LICENSE.txt index d8763995..780533dc 100644 --- a/imgui/LICENSE.txt +++ b/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2020 Omar Cornut +Copyright (c) 2014-2021 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/imgui/imconfig.h b/imgui/imconfig.h index 736958b3..c5e1650a 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -53,16 +53,25 @@ //#define IMGUI_USE_WCHAR32 //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version -// By default the embedded implementations are declared static and not available outside of imgui cpp files. +// By default the embedded implementations are declared static and not available outside of Dear ImGui sources files. //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION -//---- Unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined, use the much faster STB sprintf library implementation of vsnprintf instead of the one from the default C library. -// Note that stb_sprintf.h is meant to be provided by the user and available in the include path at compile time. Also, the compatibility checks of the arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf. +//---- Use stb_printf's faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) +// Requires 'stb_sprintf.h' to be available in the include path. Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf. // #define IMGUI_USE_STB_SPRINTF +//---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui) +// Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided). +// On Windows you may use vcpkg with 'vcpkg install freetype' + 'vcpkg integrate install'. +//#define IMGUI_ENABLE_FREETYPE + +//---- Use stb_truetype to build and rasterize the font atlas (default) +// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend. +//#define IMGUI_ENABLE_STB_TRUETYPE + //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. // This will be inlined as part of ImVec2 and ImVec4 class declarations. /* diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index 34c0da3c..0e401332 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (main code and documentation) // Help: @@ -75,6 +75,7 @@ CODE // [SECTION] DRAG AND DROP // [SECTION] LOGGING/CAPTURING // [SECTION] SETTINGS +// [SECTION] VIEWPORTS // [SECTION] PLATFORM DEPENDENT HELPERS // [SECTION] METRICS/DEBUGGER WINDOW @@ -124,7 +125,7 @@ CODE - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!) - Controls are automatically adjusted for OSX to match standard OSX text editing operations. - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard. - - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://goo.gl/9LgVZW + - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://dearimgui.org/controls_sheets PROGRAMMER GUIDE @@ -156,18 +157,20 @@ CODE HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI ---------------------------------------------- - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) - - Or maintain your own branch where you have imconfig.h modified. + - Or maintain your own branch where you have imconfig.h modified as a top-most commit which you can regularly rebase over master. + - You can also use '#define IMGUI_USER_CONFIG "my_config_file.h" to redirect configuration to your own file. - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will likely be a comment about it. Please report any issue to the GitHub page! - - Try to keep your copy of dear imgui reasonably up to date. + - To find out usage of old API, you can add '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in your configuration file. + - Try to keep your copy of Dear ImGui reasonably up to date. GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE --------------------------------------------------------------- - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - - In the majority of cases you should be able to use unmodified backends files available in the examples/ folder. + - In the majority of cases you should be able to use unmodified backends files available in the backends/ folder. - Add the Dear ImGui source files + selected backend source files to your projects or using your preferred build system. It is recommended you build and statically link the .cpp files as part of your project and NOT as shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. @@ -236,7 +239,7 @@ CODE // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'. // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ for details about ImTextureID. MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32) - io.Fonts->TexID = (void*)texture; + io.Fonts->SetTexID((void*)texture); // Application main loop while (true) @@ -307,10 +310,11 @@ CODE // We are using scissoring to clip some objects. All low-level graphics API should supports it. // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches // (some elements visible outside their bounds) but you can fix that once everything else works! - // - Clipping coordinates are provided in imgui coordinates space (from draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize) - // In a single viewport application, draw_data->DisplayPos will always be (0,0) and draw_data->DisplaySize will always be == io.DisplaySize. - // However, in the interest of supporting multi-viewport applications in the future (see 'viewport' branch on github), - // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space. + // - Clipping coordinates are provided in imgui coordinates space: + // - For a given viewport, draw_data->DisplayPos == viewport->Pos and draw_data->DisplaySize == viewport->Size + // - In a single viewport application, draw_data->DisplayPos == (0,0) and draw_data->DisplaySize == io.DisplaySize, but always use GetMainViewport()->Pos/Size instead of hardcoding those values. + // - In the interest of supporting multi-viewport applications (see 'docking' branch on github), + // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space. // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min) ImVec2 pos = draw_data->DisplayPos; MyEngineScissor((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pos.x), (int)(pcmd->ClipRect.w - pos.y)); @@ -348,7 +352,7 @@ CODE 0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks. - We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. Your code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). - - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://goo.gl/9LgVZW. + - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://dearimgui.org/controls_sheets - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. - Mouse: @@ -371,6 +375,19 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2021/02/03 (1.81) - renamed ListBoxHeader(const char* label, ImVec2 size) to BeginListBox(). Kept inline redirection function (will obsolete). + - removed ListBoxHeader(const char* label, int items_count, int height_in_items = -1) in favor of specifying size. Kept inline redirection function (will obsolete). + - renamed ListBoxFooter() to EndListBox(). Kept inline redirection function (will obsolete). + - 2021/01/26 (1.81) - removed ImGuiFreeType::BuildFontAtlas(). Kept inline redirection function. Prefer using '#define IMGUI_ENABLE_FREETYPE', but there's a runtime selection path available too. The shared extra flags parameters (very rarely used) are now stored in ImFontAtlas::FontBuilderFlags. + - renamed ImFontConfig::RasterizerFlags (used by FreeType) to ImFontConfig::FontBuilderFlags. + - renamed ImGuiFreeType::XXX flags to ImGuiFreeTypeBuilderFlags_XXX for consistency with other API. + - 2020/10/12 (1.80) - removed redirecting functions/enums that were marked obsolete in 1.63 (August 2018): + - ImGui::IsItemDeactivatedAfterChange() -> use ImGui::IsItemDeactivatedAfterEdit(). + - ImGuiCol_ModalWindowDarkening -> use ImGuiCol_ModalWindowDimBg + - ImGuiInputTextCallback -> use ImGuiTextEditCallback + - ImGuiInputTextCallbackData -> use ImGuiTextEditCallbackData + - 2020/12/21 (1.80) - renamed ImDrawList::AddBezierCurve() to AddBezierCubic(), and PathBezierCurveTo() to PathBezierCubicCurveTo(). Kept inline redirection function (will obsolete). + - 2020/12/04 (1.80) - added imgui_tables.cpp file! Manually constructed project files will need the new file added! - 2020/11/18 (1.80) - renamed undocumented/internals ImGuiColumnsFlags_* to ImGuiOldColumnFlags_* in prevision of incoming Tables API. - 2020/11/03 (1.80) - renamed io.ConfigWindowsMemoryCompactTimer to io.ConfigMemoryCompactTimer as the feature will apply to other data structures - 2020/10/14 (1.80) - backends: moved all backends files (imgui_impl_XXXX.cpp, imgui_impl_XXXX.h) from examples/ to backends/. @@ -607,9 +624,9 @@ CODE - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. - 2015/01/11 (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. - old: const void* png_data; unsigned int png_size; ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size); [..Upload texture to GPU..]; - - new: unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); [..Upload texture to GPU..]; io.Fonts->TexId = YourTexIdentifier; + - new: unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); [..Upload texture to GPU..]; io.Fonts->SetTexID(YourTexIdentifier); you now have more flexibility to load multiple TTF fonts and manage the texture buffer for internal needs. It is now recommended that you sample the font texture with bilinear interpolation. - - 2015/01/11 (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to set io.Fonts->TexID. + - 2015/01/11 (1.30) - added texture identifier in ImDrawCmd passed to your render function (we can now render images). make sure to call io.Fonts->SetTexID() - 2015/01/11 (1.30) - removed IO.PixelCenterOffset (unnecessary, can be handled in user projection matrix) - 2015/01/11 (1.30) - removed ImGui::IsItemFocused() in favor of ImGui::IsItemActive() which handles all widgets - 2014/12/10 (1.18) - removed SetNewWindowDefaultPos() in favor of new generic API SetNextWindowPos(pos, ImGuiSetCondition_FirstUseEver) @@ -837,8 +854,6 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* wind static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window); -static ImRect GetViewportRect(); - // Settings static void WindowSettingsHandler_ClearAll(ImGuiContext*, ImGuiSettingsHandler*); static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name); @@ -885,6 +900,9 @@ static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); +// Viewports +static void UpdateViewportsNewFrame(); + } //----------------------------------------------------------------------------- @@ -932,7 +950,7 @@ ImGuiStyle::ImGuiStyle() { Alpha = 1.0f; // Global alpha applies to everything in ImGui WindowPadding = ImVec2(8,8); // Padding within a window - WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. + WindowRounding = 0.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. WindowMinSize = ImVec2(32,32); // Minimum window size WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text @@ -946,6 +964,7 @@ ImGuiStyle::ImGuiStyle() FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) + CellPadding = ImVec2(4,2); // Padding within a table cell TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). @@ -986,6 +1005,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) FrameRounding = ImFloor(FrameRounding * scale_factor); ItemSpacing = ImFloor(ItemSpacing * scale_factor); ItemInnerSpacing = ImFloor(ItemInnerSpacing * scale_factor); + CellPadding = ImFloor(CellPadding * scale_factor); TouchExtraPadding = ImFloor(TouchExtraPadding * scale_factor); IndentSpacing = ImFloor(IndentSpacing * scale_factor); ColumnsMinSpacing = ImFloor(ColumnsMinSpacing * scale_factor); @@ -1117,16 +1137,16 @@ void ImGuiIO::ClearInputCharacters() // [SECTION] MISC HELPERS/UTILITIES (Geometry functions) //----------------------------------------------------------------------------- -ImVec2 ImBezierClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments) +ImVec2 ImBezierCubicClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments) { - IM_ASSERT(num_segments > 0); // Use ImBezierClosestPointCasteljau() + IM_ASSERT(num_segments > 0); // Use ImBezierCubicClosestPointCasteljau() ImVec2 p_last = p1; ImVec2 p_closest; float p_closest_dist2 = FLT_MAX; float t_step = 1.0f / (float)num_segments; for (int i_step = 1; i_step <= num_segments; i_step++) { - ImVec2 p_current = ImBezierCalc(p1, p2, p3, p4, t_step * i_step); + ImVec2 p_current = ImBezierCubicCalc(p1, p2, p3, p4, t_step * i_step); ImVec2 p_line = ImLineClosestPoint(p_last, p_current, p); float dist2 = ImLengthSqr(p - p_line); if (dist2 < p_closest_dist2) @@ -1140,7 +1160,7 @@ ImVec2 ImBezierClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3 } // Closely mimics PathBezierToCasteljau() in imgui_draw.cpp -static void BezierClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, ImVec2& p_last, float& p_closest_dist2, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) +static void ImBezierCubicClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, ImVec2& p_last, float& p_closest_dist2, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) { float dx = x4 - x1; float dy = y4 - y1; @@ -1168,20 +1188,20 @@ static void BezierClosestPointCasteljauStep(const ImVec2& p, ImVec2& p_closest, float x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; float x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; float x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; - BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); - BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); + ImBezierCubicClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); + ImBezierCubicClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); } } // tess_tol is generally the same value you would find in ImGui::GetStyle().CurveTessellationTol // Because those ImXXX functions are lower-level than ImGui:: we cannot access this value automatically. -ImVec2 ImBezierClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol) +ImVec2 ImBezierCubicClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol) { IM_ASSERT(tess_tol > 0.0f); ImVec2 p_last = p1; ImVec2 p_closest; float p_closest_dist2 = FLT_MAX; - BezierClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, tess_tol, 0); + ImBezierCubicClosestPointCasteljauStep(p, p_closest, p_last, p_closest_dist2, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, tess_tol, 0); return p_closest; } @@ -1433,7 +1453,7 @@ static const ImU32 GCrc32LookupTable[256] = // Known size hash // It is ok to call ImHashData on a string with known length but the ### operator won't be supported. // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. -ImU32 ImHashData(const void* data_p, size_t data_size, ImU32 seed) +ImGuiID ImHashData(const void* data_p, size_t data_size, ImU32 seed) { ImU32 crc = ~seed; const unsigned char* data = (const unsigned char*)data_p; @@ -1449,7 +1469,7 @@ ImU32 ImHashData(const void* data_p, size_t data_size, ImU32 seed) // - If we reach ### in the string we discard the hash so far and reset to the seed. // - We don't do 'current += 2; continue;' after handling ### to keep the code smaller/faster (measured ~10% diff in Debug build) // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. -ImU32 ImHashStr(const char* data_p, size_t data_size, ImU32 seed) +ImGuiID ImHashStr(const char* data_p, size_t data_size, ImU32 seed) { seed = ~seed; ImU32 crc = seed; @@ -2134,6 +2154,14 @@ void ImGuiTextBuffer::appendfv(const char* fmt, va_list args) // the API mid-way through development and support two ways to using the clipper, needs some rework (see TODO) //----------------------------------------------------------------------------- +// FIXME-TABLE: This prevents us from using ImGuiListClipper _inside_ a table cell. +// The problem we have is that without a Begin/End scheme for rows using the clipper is ambiguous. +static bool GetSkipItemForListClipping() +{ + ImGuiContext& g = *GImGui; + return (g.CurrentTable ? g.CurrentTable->HostSkipItems : g.CurrentWindow->SkipItems); +} + // Helper to calculate coarse clipping of large list of evenly sized items. // NB: Prefer using the ImGuiListClipper higher-level helper if you can! Read comments and instructions there on how those use this sort of pattern. // NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX @@ -2148,7 +2176,7 @@ void ImGui::CalcListClipping(int items_count, float items_height, int* out_items *out_items_display_end = items_count; return; } - if (window->SkipItems) + if (GetSkipItemForListClipping()) { *out_items_display_start = *out_items_display_end = 0; return; @@ -2184,12 +2212,22 @@ static void SetCursorPosYAndSetupForPrevLine(float pos_y, float line_height) // The clipper should probably have a 4th step to display the last item in a regular manner. ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + float off_y = pos_y - window->DC.CursorPos.y; window->DC.CursorPos.y = pos_y; window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, pos_y); window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height; // Setting those fields so that SetScrollHereY() can properly function after the end of our clipper usage. window->DC.PrevLineSize.y = (line_height - g.Style.ItemSpacing.y); // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list. if (ImGuiOldColumns* columns = window->DC.CurrentColumns) columns->LineMinY = window->DC.CursorPos.y; // Setting this so that cell Y position are set properly + if (ImGuiTable* table = g.CurrentTable) + { + if (table->IsInsideRow) + ImGui::TableEndRow(table); + table->RowPosY2 = window->DC.CursorPos.y; + const int row_increase = (int)((off_y / line_height) + 0.5f); + //table->CurrentRow += row_increase; // Can't do without fixing TableEndRow() + table->RowBgColorCounter += row_increase; + } } ImGuiListClipper::ImGuiListClipper() @@ -2211,9 +2249,14 @@ void ImGuiListClipper::Begin(int items_count, float items_height) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + if (ImGuiTable* table = g.CurrentTable) + if (table->IsInsideRow) + ImGui::TableEndRow(table); + StartPosY = window->DC.CursorPos.y; ItemsHeight = items_height; ItemsCount = items_count; + ItemsFrozen = 0; StepNo = 0; DisplayStart = -1; DisplayEnd = 0; @@ -2226,7 +2269,7 @@ void ImGuiListClipper::End() // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. if (ItemsCount < INT_MAX && DisplayStart >= 0) - SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); + SetCursorPosYAndSetupForPrevLine(StartPosY + (ItemsCount - ItemsFrozen) * ItemsHeight, ItemsHeight); ItemsCount = -1; StepNo = 3; } @@ -2236,8 +2279,12 @@ bool ImGuiListClipper::Step() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - // Reached end of list - if (DisplayEnd >= ItemsCount || window->SkipItems) + ImGuiTable* table = g.CurrentTable; + if (table && table->IsInsideRow) + ImGui::TableEndRow(table); + + // No items + if (ItemsCount == 0 || GetSkipItemForListClipping()) { End(); return false; @@ -2246,12 +2293,22 @@ bool ImGuiListClipper::Step() // Step 0: Let you process the first element (regardless of it being visible or not, so we can measure the element height) if (StepNo == 0) { + // While we are in frozen row state, keep displaying items one by one, unclipped + // FIXME: Could be stored as a table-agnostic state. + if (table != NULL && !table->IsUnfrozenRows) + { + DisplayStart = ItemsFrozen; + DisplayEnd = ItemsFrozen + 1; + ItemsFrozen++; + return true; + } + StartPosY = window->DC.CursorPos.y; if (ItemsHeight <= 0.0f) { // Submit the first item so we can measure its height (generally it is 0..1) - DisplayStart = 0; - DisplayEnd = 1; + DisplayStart = ItemsFrozen; + DisplayEnd = ItemsFrozen + 1; StepNo = 1; return true; } @@ -2265,11 +2322,28 @@ bool ImGuiListClipper::Step() if (StepNo == 1) { IM_ASSERT(ItemsHeight <= 0.0f); - ItemsHeight = window->DC.CursorPos.y - StartPosY; + if (table) + { + const float pos_y1 = table->RowPosY1; // Using this instead of StartPosY to handle clipper straddling the frozen row + const float pos_y2 = table->RowPosY2; // Using this instead of CursorPos.y to take account of tallest cell. + ItemsHeight = pos_y2 - pos_y1; + window->DC.CursorPos.y = pos_y2; + } + else + { + ItemsHeight = window->DC.CursorPos.y - StartPosY; + } IM_ASSERT(ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); StepNo = 2; } + // Reached end of list + if (DisplayEnd >= ItemsCount) + { + End(); + return false; + } + // Step 2: calculate the actual range of elements to display, and position the cursor before the first element if (StepNo == 2) { @@ -2282,7 +2356,7 @@ bool ImGuiListClipper::Step() // Seek cursor if (DisplayStart > already_submitted) - SetCursorPosYAndSetupForPrevLine(StartPosY + DisplayStart * ItemsHeight, ItemsHeight); + SetCursorPosYAndSetupForPrevLine(StartPosY + (DisplayStart - ItemsFrozen) * ItemsHeight, ItemsHeight); StepNo = 3; return true; @@ -2294,7 +2368,7 @@ bool ImGuiListClipper::Step() { // Seek cursor if (ItemsCount < INT_MAX) - SetCursorPosYAndSetupForPrevLine(StartPosY + ItemsCount * ItemsHeight, ItemsHeight); // advance cursor + SetCursorPosYAndSetupForPrevLine(StartPosY + (ItemsCount - ItemsFrozen) * ItemsHeight, ItemsHeight); // advance cursor ItemsCount = -1; return false; } @@ -2404,6 +2478,7 @@ static const ImGuiStyleVarInfo GStyleVarInfo[] = { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize @@ -2511,6 +2586,11 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; case ImGuiCol_PlotHistogram: return "PlotHistogram"; case ImGuiCol_PlotHistogramHovered: return "PlotHistogramHovered"; + case ImGuiCol_TableHeaderBg: return "TableHeaderBg"; + case ImGuiCol_TableBorderStrong: return "TableBorderStrong"; + case ImGuiCol_TableBorderLight: return "TableBorderLight"; + case ImGuiCol_TableRowBg: return "TableRowBg"; + case ImGuiCol_TableRowBgAlt: return "TableRowBgAlt"; case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; case ImGuiCol_DragDropTarget: return "DragDropTarget"; case ImGuiCol_NavHighlight: return "NavHighlight"; @@ -2888,6 +2968,7 @@ static void SetCurrentWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; g.CurrentWindow = window; + g.CurrentTable = window && window->DC.CurrentTableIdx != -1 ? g.Tables.GetByIndex(window->DC.CurrentTableIdx) : NULL; if (window) g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } @@ -2897,6 +2978,7 @@ void ImGui::GcCompactTransientMiscBuffers() ImGuiContext& g = *GImGui; g.ItemFlagsStack.clear(); g.GroupStack.clear(); + TableGcCompactSettings(); } // Free up/compact internal window buffers, we can use this when a window becomes unused. @@ -2953,6 +3035,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) // Clear declaration of inputs claimed by the widget // (Please note that this is WIP and not all keys/inputs are thoroughly declared by all widgets yet) + g.ActiveIdUsingMouseWheel = false; g.ActiveIdUsingNavDirMask = 0x00; g.ActiveIdUsingNavInputMask = 0x00; g.ActiveIdUsingKeyInputMask = 0x00; @@ -2968,6 +3051,7 @@ void ImGui::SetHoveredID(ImGuiID id) ImGuiContext& g = *GImGui; g.HoveredId = id; g.HoveredIdAllowOverlap = false; + g.HoveredIdUsingMouseWheel = false; if (id != 0 && g.HoveredIdPreviousFrame != id) g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f; } @@ -3271,11 +3355,23 @@ void ImGui::DestroyContext(ImGuiContext* ctx) } // No specific ordering/dependency support, will see as needed -void ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) +ImGuiID ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) { ImGuiContext& g = *ctx; - IM_ASSERT(hook->Callback != NULL); + IM_ASSERT(hook->Callback != NULL && hook->HookId == 0 && hook->Type != ImGuiContextHookType_PendingRemoval_); g.Hooks.push_back(*hook); + g.Hooks.back().HookId = ++g.HookIdNext; + return g.HookIdNext; +} + +// Deferred removal, avoiding issue with changing vector while iterating it +void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) +{ + ImGuiContext& g = *ctx; + IM_ASSERT(hook_id != 0); + for (int n = 0; n < g.Hooks.Size; n++) + if (g.Hooks[n].HookId == hook_id) + g.Hooks[n].Type = ImGuiContextHookType_PendingRemoval_; } // Call context hooks (used by e.g. test engine) @@ -3298,7 +3394,8 @@ ImGuiIO& ImGui::GetIO() ImDrawData* ImGui::GetDrawData() { ImGuiContext& g = *GImGui; - return g.DrawData.Valid ? &g.DrawData : NULL; + ImGuiViewportP* viewport = g.Viewports[0]; + return viewport->DrawDataP.Valid ? &viewport->DrawDataP : NULL; } double ImGui::GetTime() @@ -3311,14 +3408,50 @@ int ImGui::GetFrameCount() return GImGui->FrameCount; } +static ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) +{ + // Create the draw list on demand, because they are not frequently used for all viewports + ImGuiContext& g = *GImGui; + IM_ASSERT(drawlist_no < IM_ARRAYSIZE(viewport->DrawLists)); + ImDrawList* draw_list = viewport->DrawLists[drawlist_no]; + if (draw_list == NULL) + { + draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData); + draw_list->_OwnerName = drawlist_name; + viewport->DrawLists[drawlist_no] = draw_list; + } + + // Our ImDrawList system requires that there is always a command + if (viewport->DrawListsLastFrame[drawlist_no] != g.FrameCount) + { + draw_list->_ResetForNewFrame(); + draw_list->PushTextureID(g.IO.Fonts->TexID); + draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false); + viewport->DrawListsLastFrame[drawlist_no] = g.FrameCount; + } + return draw_list; +} + +ImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport) +{ + return GetViewportDrawList((ImGuiViewportP*)viewport, 0, "##Background"); +} + ImDrawList* ImGui::GetBackgroundDrawList() { - return &GImGui->BackgroundDrawList; + ImGuiContext& g = *GImGui; + return GetBackgroundDrawList(g.Viewports[0]); +} + +ImDrawList* ImGui::GetForegroundDrawList(ImGuiViewport* viewport) +{ + return GetViewportDrawList((ImGuiViewportP*)viewport, 1, "##Foreground"); } ImDrawList* ImGui::GetForegroundDrawList() { - return &GImGui->ForegroundDrawList; + ImGuiContext& g = *GImGui; + return GetForegroundDrawList(g.Viewports[0]); } ImDrawListSharedData* ImGui::GetDrawListSharedData() @@ -3400,7 +3533,8 @@ void ImGui::UpdateMouseMovingWindowEndFrame() if (g.NavWindow && g.NavWindow->Appearing) return; - // Click on empty space to focus window and start moving (after we're done with all our widgets) + // Click on empty space to focus window and start moving + // (after we're done with all our widgets) if (g.IO.MouseClicked[0]) { // Handle the edge case of a popup being closed while clicking in its empty space. @@ -3533,6 +3667,9 @@ void ImGui::UpdateMouseWheel() if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f) return; + if ((g.ActiveId != 0 && g.ActiveIdUsingMouseWheel) || (g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrameUsingMouseWheel)) + return; + ImGuiWindow* window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow; if (!window || window->Collapsed) return; @@ -3702,6 +3839,12 @@ void ImGui::NewFrame() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); ImGuiContext& g = *GImGui; + + // Remove pending delete hooks before frame start. + // This deferred removal avoid issues of removal while iterating the hook vector + for (int n = g.Hooks.Size - 1; n >= 0; n--) + if (g.Hooks[n].Type == ImGuiContextHookType_PendingRemoval_) + g.Hooks.erase(&g.Hooks[n]); CallContextHooks(&g, ImGuiContextHookType_NewFramePre); @@ -3724,11 +3867,16 @@ void ImGui::NewFrame() g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; + UpdateViewportsNewFrame(); + // Setup current font and draw list shared data g.IO.Fonts->Locked = true; SetCurrentFont(GetDefaultFont()); IM_ASSERT(g.Font->IsLoaded()); - g.DrawListSharedData.ClipRectFullscreen = ImVec4(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); + ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + for (int n = 0; n < g.Viewports.Size; n++) + virtual_space.Add(g.Viewports[n]->GetMainRect()); + g.DrawListSharedData.ClipRectFullscreen = virtual_space.ToVec4(); g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; g.DrawListSharedData.SetCircleSegmentMaxError(g.Style.CircleSegmentMaxError); g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; @@ -3741,16 +3889,12 @@ void ImGui::NewFrame() if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; - g.BackgroundDrawList._ResetForNewFrame(); - g.BackgroundDrawList.PushTextureID(g.IO.Fonts->TexID); - g.BackgroundDrawList.PushClipRectFullScreen(); - - g.ForegroundDrawList._ResetForNewFrame(); - g.ForegroundDrawList.PushTextureID(g.IO.Fonts->TexID); - g.ForegroundDrawList.PushClipRectFullScreen(); - // Mark rendering data as invalid to prevent user who may have a handle on it to use it. - g.DrawData.Clear(); + for (int n = 0; n < g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + viewport->DrawDataP.Clear(); + } // Drag and drop keep the source ID alive so even if the source disappear our state is consistent if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) @@ -3766,8 +3910,10 @@ void ImGui::NewFrame() if (g.HoveredId && g.ActiveId != g.HoveredId) g.HoveredIdNotActiveTimer += g.IO.DeltaTime; g.HoveredIdPreviousFrame = g.HoveredId; + g.HoveredIdPreviousFrameUsingMouseWheel = g.HoveredIdUsingMouseWheel; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; + g.HoveredIdUsingMouseWheel = false; g.HoveredIdDisabled = false; // Update ActiveId data (clear reference to active widget if the widget isn't alive anymore) @@ -3851,6 +3997,11 @@ void ImGui::NewFrame() if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) GcCompactTransientWindowBuffers(window); } + + // Garbage collect transient buffers of recently unused tables + for (int i = 0; i < g.TablesLastTimeActive.Size; i++) + if (g.TablesLastTimeActive[i] >= 0.0f && g.TablesLastTimeActive[i] < memory_compact_start_time) + TableGcCompactTransientBuffers(g.Tables.GetByIndex(i)); if (g.GcCompactAll) GcCompactTransientMiscBuffers(); g.GcCompactAll = false; @@ -3927,17 +4078,13 @@ void ImGui::Initialize(ImGuiContext* context) #ifdef IMGUI_HAS_TABLE // Add .ini handle for ImGuiTable type - { - ImGuiSettingsHandler ini_handler; - ini_handler.TypeName = "Table"; - ini_handler.TypeHash = ImHashStr("Table"); - ini_handler.ReadOpenFn = TableSettingsHandler_ReadOpen; - ini_handler.ReadLineFn = TableSettingsHandler_ReadLine; - ini_handler.WriteAllFn = TableSettingsHandler_WriteAll; - g.SettingsHandlers.push_back(ini_handler); - } + TableSettingsInstallHandler(context); #endif // #ifdef IMGUI_HAS_TABLE + // Create default viewport + ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); + g.Viewports.push_back(viewport); + #ifdef IMGUI_HAS_DOCK #endif // #ifdef IMGUI_HAS_DOCK @@ -3989,14 +4136,19 @@ void ImGui::Shutdown(ImGuiContext* context) g.FontStack.clear(); g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); - g.DrawDataBuilder.ClearFreeMemory(); - g.BackgroundDrawList._ClearFreeMemory(); - g.ForegroundDrawList._ClearFreeMemory(); + + for (int i = 0; i < g.Viewports.Size; i++) + IM_DELETE(g.Viewports[i]); + g.Viewports.clear(); g.TabBars.Clear(); g.CurrentTabBarStack.clear(); g.ShrinkWidthBuffer.clear(); + g.Tables.Clear(); + g.CurrentTableStack.clear(); + g.DrawChannelsTempMergeBuffer.clear(); + g.ClipboardHandlerData.clear(); g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); @@ -4082,25 +4234,25 @@ static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* d out_list->push_back(draw_list); } -static void AddWindowToDrawData(ImVector* out_render_list, ImGuiWindow* window) +static void AddWindowToDrawData(ImGuiWindow* window, int layer) { ImGuiContext& g = *GImGui; + ImGuiViewportP* viewport = g.Viewports[0]; g.IO.MetricsRenderWindows++; - AddDrawListToDrawData(out_render_list, window->DrawList); + AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[layer], window->DrawList); for (int i = 0; i < window->DC.ChildWindows.Size; i++) { ImGuiWindow* child = window->DC.ChildWindows[i]; - if (IsWindowActiveAndVisible(child)) // clipped children may have been marked not active - AddWindowToDrawData(out_render_list, child); + if (IsWindowActiveAndVisible(child)) // Clipped children may have been marked not active + AddWindowToDrawData(child, layer); } } // Layer is locked for the root window, however child windows may use a different viewport (e.g. extruding menu) static void AddRootWindowToDrawData(ImGuiWindow* window) { - ImGuiContext& g = *GImGui; int layer = (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0; - AddWindowToDrawData(&g.DrawDataBuilder.Layers[layer], window); + AddWindowToDrawData(window, layer); } void ImDrawDataBuilder::FlattenIntoSingleLayer() @@ -4121,15 +4273,16 @@ void ImDrawDataBuilder::FlattenIntoSingleLayer() } } -static void SetupDrawData(ImVector* draw_lists, ImDrawData* draw_data) +static void SetupViewportDrawData(ImGuiViewportP* viewport, ImVector* draw_lists) { ImGuiIO& io = ImGui::GetIO(); + ImDrawData* draw_data = &viewport->DrawDataP; draw_data->Valid = true; draw_data->CmdLists = (draw_lists->Size > 0) ? draw_lists->Data : NULL; draw_data->CmdListsCount = draw_lists->Size; draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0; - draw_data->DisplayPos = ImVec2(0.0f, 0.0f); - draw_data->DisplaySize = io.DisplaySize; + draw_data->DisplayPos = viewport->Pos; + draw_data->DisplaySize = viewport->Size; draw_data->FramebufferScale = io.DisplayFramebufferScale; for (int n = 0; n < draw_lists->Size; n++) { @@ -4250,13 +4403,17 @@ void ImGui::Render() EndFrame(); g.FrameCountRendered = g.FrameCount; g.IO.MetricsRenderWindows = 0; - g.DrawDataBuilder.Clear(); CallContextHooks(&g, ImGuiContextHookType_RenderPre); - // Add background ImDrawList - if (!g.BackgroundDrawList.VtxBuffer.empty()) - AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.BackgroundDrawList); + // Add background ImDrawList (for each active viewport) + for (int n = 0; n != g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + viewport->DrawDataBuilder.Clear(); + if (viewport->DrawLists[0] != NULL) + AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); + } // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; @@ -4271,20 +4428,27 @@ void ImGui::Render() for (int n = 0; n < IM_ARRAYSIZE(windows_to_render_top_most); n++) if (windows_to_render_top_most[n] && IsWindowActiveAndVisible(windows_to_render_top_most[n])) // NavWindowingTarget is always temporarily displayed as the top-most window AddRootWindowToDrawData(windows_to_render_top_most[n]); - g.DrawDataBuilder.FlattenIntoSingleLayer(); - // Draw software mouse cursor if requested - if (g.IO.MouseDrawCursor) - RenderMouseCursor(&g.ForegroundDrawList, g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); + // Setup ImDrawData structures for end-user + g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = 0; + for (int n = 0; n < g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + viewport->DrawDataBuilder.FlattenIntoSingleLayer(); + + // Draw software mouse cursor if requested by io.MouseDrawCursor flag + if (g.IO.MouseDrawCursor) + RenderMouseCursor(GetForegroundDrawList(viewport), g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); - // Add foreground ImDrawList - if (!g.ForegroundDrawList.VtxBuffer.empty()) - AddDrawListToDrawData(&g.DrawDataBuilder.Layers[0], &g.ForegroundDrawList); + // Add foreground ImDrawList (for each active viewport) + if (viewport->DrawLists[1] != NULL) + AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport)); - // Setup ImDrawData structure for end-user - SetupDrawData(&g.DrawDataBuilder.Layers[0], &g.DrawData); - g.IO.MetricsRenderVertices = g.DrawData.TotalVtxCount; - g.IO.MetricsRenderIndices = g.DrawData.TotalIdxCount; + SetupViewportDrawData(viewport, &viewport->DrawDataBuilder.Layers[0]); + ImDrawData* draw_data = &viewport->DrawDataP; + g.IO.MetricsRenderVertices += draw_data->TotalVtxCount; + g.IO.MetricsRenderIndices += draw_data->TotalIdxCount; + } CallContextHooks(&g, ImGuiContextHookType_RenderPost); } @@ -4308,7 +4472,11 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex ImVec2 text_size = font->CalcTextSizeA(font_size, FLT_MAX, wrap_width, text, text_display_end, NULL); // Round - text_size.x = IM_FLOOR(text_size.x + 0.95f); + // FIXME: This has been here since Dec 2015 (7b0bf230) but down the line we want this out. + // FIXME: Investigate using ceilf or e.g. + // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c + // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html + text_size.x = IM_FLOOR(text_size.x + 0.99999f); return text_size; } @@ -4690,15 +4858,27 @@ bool ImGui::IsItemEdited() } // Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. +// FIXME: Although this is exposed, its interaction and ideal idiom with using ImGuiButtonFlags_AllowItemOverlap flag are extremely confusing, need rework. void ImGui::SetItemAllowOverlap() { ImGuiContext& g = *GImGui; - if (g.HoveredId == g.CurrentWindow->DC.LastItemId) + ImGuiID id = g.CurrentWindow->DC.LastItemId; + if (g.HoveredId == id) g.HoveredIdAllowOverlap = true; - if (g.ActiveId == g.CurrentWindow->DC.LastItemId) + if (g.ActiveId == id) g.ActiveIdAllowOverlap = true; } +void ImGui::SetItemUsingMouseWheel() +{ + ImGuiContext& g = *GImGui; + ImGuiID id = g.CurrentWindow->DC.LastItemId; + if (g.HoveredId == id) + g.HoveredIdUsingMouseWheel = true; + if (g.ActiveId == id) + g.ActiveIdUsingMouseWheel = true; +} + ImVec2 ImGui::GetItemRectMin() { ImGuiWindow* window = GetCurrentWindowRead(); @@ -4717,12 +4897,6 @@ ImVec2 ImGui::GetItemRectSize() return window->DC.LastItemRect.GetSize(); } -static ImRect GetViewportRect() -{ - ImGuiContext& g = *GImGui; - return ImRect(0.0f, 0.0f, g.IO.DisplaySize.x, g.IO.DisplaySize.y); -} - bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; @@ -4742,16 +4916,15 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b SetNextWindowSize(size); // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. - char title[256]; if (name) - ImFormatString(title, IM_ARRAYSIZE(title), "%s/%s_%08X", parent_window->Name, name, id); + ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%s_%08X", parent_window->Name, name, id); else - ImFormatString(title, IM_ARRAYSIZE(title), "%s/%08X", parent_window->Name, id); + ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%08X", parent_window->Name, id); const float backup_border_size = g.Style.ChildBorderSize; if (!border) g.Style.ChildBorderSize = 0.0f; - bool ret = Begin(title, NULL, flags); + bool ret = Begin(g.TempBuffer, NULL, flags); g.Style.ChildBorderSize = backup_border_size; ImGuiWindow* child_window = g.CurrentWindow; @@ -4827,6 +5000,7 @@ void ImGui::EndChild() } } g.WithinEndChild = false; + g.LogLinePosY = -FLT_MAX; // To enforce a carriage return } // Helper to create a child window / scrolling region that looks like a normal widget frame. @@ -4887,7 +5061,8 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) g.WindowsById.SetVoidPtr(window->ID, window); // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. - window->Pos = ImVec2(60, 60); + const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + window->Pos = main_viewport->Pos + ImVec2(60, 60); // User can disable loading and saving of settings. Tooltip and child windows also don't store settings. if (!(flags & ImGuiWindowFlags_NoSavedSettings)) @@ -4955,18 +5130,24 @@ static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size return new_size; } -static ImVec2 CalcWindowContentSize(ImGuiWindow* window) +static void CalcWindowContentSizes(ImGuiWindow* window, ImVec2* content_size_current, ImVec2* content_size_ideal) { - if (window->Collapsed) - if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) - return window->ContentSize; - if (window->Hidden && window->HiddenFramesCannotSkipItems == 0 && window->HiddenFramesCanSkipItems > 0) - return window->ContentSize; + bool preserve_old_content_sizes = false; + if (window->Collapsed && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + preserve_old_content_sizes = true; + else if (window->Hidden && window->HiddenFramesCannotSkipItems == 0 && window->HiddenFramesCanSkipItems > 0) + preserve_old_content_sizes = true; + if (preserve_old_content_sizes) + { + *content_size_current = window->ContentSize; + *content_size_ideal = window->ContentSizeIdeal; + return; + } - ImVec2 sz; - sz.x = IM_FLOOR((window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); - sz.y = IM_FLOOR((window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); - return sz; + content_size_current->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_FLOOR(window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); + content_size_current->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_FLOOR(window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); + content_size_ideal->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_FLOOR(ImMax(window->DC.CursorMaxPos.x, window->DC.IdealMaxPos.x) - window->DC.CursorStartPos.x); + content_size_ideal->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_FLOOR(ImMax(window->DC.CursorMaxPos.y, window->DC.IdealMaxPos.y) - window->DC.CursorStartPos.y); } static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_contents) @@ -4989,7 +5170,10 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont ImVec2 size_min = style.WindowMinSize; if (is_popup || is_menu) // Popups and menus bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) size_min = ImMin(size_min, ImVec2(4.0f, 4.0f)); - ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, g.IO.DisplaySize - style.DisplaySafeAreaPadding * 2.0f)); + + // FIXME-VIEWPORT-WORKAREA: May want to use GetWorkSize() instead of Size depending on the type of windows? + ImVec2 avail_size = ImGui::GetMainViewport()->Size; + ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, avail_size - style.DisplaySafeAreaPadding * 2.0f)); // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. @@ -5004,10 +5188,12 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont } } -ImVec2 ImGui::CalcWindowExpectedSize(ImGuiWindow* window) +ImVec2 ImGui::CalcWindowNextAutoFitSize(ImGuiWindow* window) { - ImVec2 size_contents = CalcWindowContentSize(window); - ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, size_contents); + ImVec2 size_contents_current; + ImVec2 size_contents_ideal; + CalcWindowContentSizes(window, &size_contents_current, &size_contents_ideal); + ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, size_contents_ideal); ImVec2 size_final = CalcWindowSizeAfterConstraint(window, size_auto_fit); return size_final; } @@ -5393,7 +5579,7 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl const ImVec2 text_size = CalcTextSize(name, NULL, true) + ImVec2(marker_size_x, 0.0f); // As a nice touch we try to ensure that centered title text doesn't get affected by visibility of Close/Collapse button, - // while uncentered title text will still reach edges correct. + // while uncentered title text will still reach edges correctly. if (pad_l > style.FramePadding.x) pad_l += g.Style.ItemInnerSpacing.x; if (pad_r > style.FramePadding.x) @@ -5407,8 +5593,9 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl } ImRect layout_r(title_bar_rect.Min.x + pad_l, title_bar_rect.Min.y, title_bar_rect.Max.x - pad_r, title_bar_rect.Max.y); - ImRect clip_r(layout_r.Min.x, layout_r.Min.y, layout_r.Max.x + g.Style.ItemInnerSpacing.x, layout_r.Max.y); - //if (g.IO.KeyCtrl) window->DrawList->AddRect(layout_r.Min, layout_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] + ImRect clip_r(layout_r.Min.x, layout_r.Min.y, ImMin(layout_r.Max.x + g.Style.ItemInnerSpacing.x, title_bar_rect.Max.x), layout_r.Max.y); + //if (g.IO.KeyShift) window->DrawList->AddRect(layout_r.Min, layout_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] + //if (g.IO.KeyCtrl) window->DrawList->AddRect(clip_r.Min, clip_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] RenderTextClipped(layout_r.Min, layout_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_r); if (flags & ImGuiWindowFlags_UnsavedDocument) { @@ -5583,6 +5770,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->ClipRect = ImVec4(-FLT_MAX, -FLT_MAX, +FLT_MAX, +FLT_MAX); window->IDStack.resize(1); window->DrawList->_ResetForNewFrame(); + window->DC.CurrentTableIdx = -1; // Restore buffer capacity when woken from a compacted state, to avoid if (window->MemoryCompacted) @@ -5603,11 +5791,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS // Update contents size from last frame for auto-fitting (or use explicit size) - window->ContentSize = CalcWindowContentSize(window); + CalcWindowContentSizes(window, &window->ContentSize, &window->ContentSizeIdeal); if (window->HiddenFramesCanSkipItems > 0) window->HiddenFramesCanSkipItems--; if (window->HiddenFramesCannotSkipItems > 0) window->HiddenFramesCannotSkipItems--; + if (window->HiddenFramesForRenderOnly > 0) + window->HiddenFramesForRenderOnly--; // Hide new windows for one frame until they calculate their size if (window_just_created && (!window_size_x_set_by_api || !window_size_y_set_by_api)) @@ -5624,7 +5814,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->Size.x = window->SizeFull.x = 0.f; if (!window_size_y_set_by_api) window->Size.y = window->SizeFull.y = 0.f; - window->ContentSize = ImVec2(0.f, 0.f); + window->ContentSize = window->ContentSizeIdeal = ImVec2(0.f, 0.f); } } @@ -5670,7 +5860,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // SIZE // Calculate auto-fit size, handle automatic resize - const ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, window->ContentSize); + const ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, window->ContentSizeIdeal); bool use_current_size_for_scrollbar_x = window_just_created; bool use_current_size_for_scrollbar_y = window_just_created; if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window->Collapsed) @@ -5744,9 +5934,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Calculate the range of allowed position for that window (to be movable and visible past safe area padding) // When clamping to stay visible, we will enforce that window->Pos stays inside of visibility_rect. - ImRect viewport_rect(GetViewportRect()); + ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); + ImRect viewport_rect(viewport->GetMainRect()); + ImRect viewport_work_rect(viewport->GetWorkRect()); ImVec2 visibility_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); - ImRect visibility_rect(viewport_rect.Min + visibility_padding, viewport_rect.Max - visibility_padding); + ImRect visibility_rect(viewport_work_rect.Min + visibility_padding, viewport_work_rect.Max - visibility_padding); // Clamp position/size so window stays visible within its viewport or monitor // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. @@ -5777,7 +5969,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) int border_held = -1; ImU32 resize_grip_col[4] = {}; const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. - const float resize_grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); + const float resize_grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.10f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); if (!window->Collapsed) if (UpdateWindowManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect)) use_current_size_for_scrollbar_x = use_current_size_for_scrollbar_y = true; @@ -5957,6 +6149,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CursorPos = window->DC.CursorStartPos; window->DC.CursorPosPrevLine = window->DC.CursorPos; window->DC.CursorMaxPos = window->DC.CursorStartPos; + window->DC.IdealMaxPos = window->DC.CursorStartPos; window->DC.CurrLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; @@ -5996,7 +6189,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Title bar if (!(flags & ImGuiWindowFlags_NoTitleBar)) - RenderWindowTitleBarContents(window, title_bar_rect, name, p_open); + RenderWindowTitleBarContents(window, ImRect(title_bar_rect.Min.x + window->WindowBorderSize, title_bar_rect.Min.y, title_bar_rect.Max.x - window->WindowBorderSize, title_bar_rect.Max.y), name, p_open); // Clear hit test shape every frame window->HitTestHoleSize.x = window->HitTestHoleSize.y = 0; @@ -6005,7 +6198,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope. // Maybe we can support CTRL+C on every element? /* - if (g.ActiveId == move_id) + //if (g.NavWindow == window && g.ActiveId == 0) + if (g.ActiveId == window->MoveId) if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) LogToClipboard(); */ @@ -6044,9 +6238,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Child window can be out of sight and have "negative" clip windows. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); - if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) - if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) - window->HiddenFramesCanSkipItems = 1; + if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) // FIXME: Doesn't make sense for ChildWindow?? + if (!g.LogEnabled) + if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + window->HiddenFramesCanSkipItems = 1; // Hide along with parent or if parent is collapsed if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) @@ -6060,7 +6255,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->HiddenFramesCanSkipItems = 1; // Update the Hidden flag - window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); + window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0) || (window->HiddenFramesForRenderOnly > 0); // Update the SkipItems flag, used to early out of all items functions (no layout required) bool skip_items = false; @@ -6300,15 +6495,15 @@ void ImGui::PopButtonRepeat() void ImGui::PushTextWrapPos(float wrap_pos_x) { ImGuiWindow* window = GetCurrentWindow(); + window->DC.TextWrapPosStack.push_back(window->DC.TextWrapPos); window->DC.TextWrapPos = wrap_pos_x; - window->DC.TextWrapPosStack.push_back(wrap_pos_x); } void ImGui::PopTextWrapPos() { ImGuiWindow* window = GetCurrentWindow(); + window->DC.TextWrapPos = window->DC.TextWrapPosStack.back(); window->DC.TextWrapPosStack.pop_back(); - window->DC.TextWrapPos = window->DC.TextWrapPosStack.empty() ? -1.0f : window->DC.TextWrapPosStack.back(); } bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) @@ -6405,7 +6600,7 @@ bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) // If you want a window to never be focused, you may use the e.g. NoInputs flag. bool ImGui::IsWindowNavFocusable(ImGuiWindow* window) { - return window->Active && window == window->RootWindow && !(window->Flags & ImGuiWindowFlags_NoNavFocus); + return window->WasActive && window == window->RootWindow && !(window->Flags & ImGuiWindowFlags_NoNavFocus); } float ImGui::GetWindowWidth() @@ -6443,6 +6638,7 @@ void ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond) ImVec2 offset = window->Pos - old_pos; window->DC.CursorPos += offset; // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor window->DC.CursorMaxPos += offset; // And more importantly we need to offset CursorMaxPos/CursorStartPos this so ContentSize calculation doesn't get affected. + window->DC.IdealMaxPos += offset; window->DC.CursorStartPos += offset; } @@ -6600,7 +6796,7 @@ void ImGui::SetNextWindowContentSize(const ImVec2& size) { ImGuiContext& g = *GImGui; g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasContentSize; - g.NextWindowData.ContentSizeVal = size; + g.NextWindowData.ContentSizeVal = ImFloor(size); } void ImGui::SetNextWindowScroll(const ImVec2& scroll) @@ -6930,37 +7126,38 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi } #endif ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(window != NULL); while (g.CurrentTabBar != NULL) //-V1044 { if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); EndTabBar(); } - while (g.CurrentWindow->DC.TreeDepth > 0) //-V1044 + while (window->DC.TreeDepth > 0) { if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); TreePop(); } - while (g.GroupStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfGroupStack) //-V1044 + while (g.GroupStack.Size > window->DC.StackSizesOnBegin.SizeOfGroupStack) { if (log_callback) log_callback(user_data, "Recovered from missing EndGroup() in '%s'", window->Name); EndGroup(); } - while (g.CurrentWindow->IDStack.Size > 1) //-V1044 + while (window->IDStack.Size > 1) { if (log_callback) log_callback(user_data, "Recovered from missing PopID() in '%s'", window->Name); PopID(); } - while (g.ColorStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfColorStack) //-V1044 + while (g.ColorStack.Size > window->DC.StackSizesOnBegin.SizeOfColorStack) { if (log_callback) log_callback(user_data, "Recovered from missing PopStyleColor() in '%s' for ImGuiCol_%s", window->Name, GetStyleColorName(g.ColorStack.back().Col)); PopStyleColor(); } - while (g.StyleVarStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfStyleVarStack) //-V1044 + while (g.StyleVarStack.Size > window->DC.StackSizesOnBegin.SizeOfStyleVarStack) { if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); PopStyleVar(); } - while (g.FocusScopeStack.Size > g.CurrentWindow->DC.StackSizesOnBegin.SizeOfFocusScopeStack) //-V1044 + while (g.FocusScopeStack.Size > window->DC.StackSizesOnBegin.SizeOfFocusScopeStack) { if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); PopFocusScope(); @@ -6970,7 +7167,8 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi IM_ASSERT(g.CurrentWindow->IsFallbackWindow); break; } - if (g.CurrentWindow->Flags & ImGuiWindowFlags_ChildWindow) + IM_ASSERT(window == g.CurrentWindow); + if (window->Flags & ImGuiWindowFlags_ChildWindow) { if (log_callback) log_callback(user_data, "Recovered from missing EndChild() for '%s'", window->Name); EndChild(); @@ -7002,6 +7200,7 @@ void ImGuiStackSizes::CompareWithCurrentState() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + IM_UNUSED(window); // Window stacks // NOT checking: DC.ItemWidth, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) @@ -7262,12 +7461,13 @@ void ImGui::SetNextItemWidth(float item_width) g.NextItemData.Width = item_width; } +// FIXME: Remove the == 0.0f behavior? void ImGui::PushItemWidth(float item_width) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); // Backup current width window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); - window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; } @@ -7278,18 +7478,19 @@ void ImGui::PushMultiItemsWidths(int components, float w_full) const ImGuiStyle& style = g.Style; const float w_item_one = ImMax(1.0f, IM_FLOOR((w_full - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components)); const float w_item_last = ImMax(1.0f, IM_FLOOR(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components - 1))); + window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); // Backup current width window->DC.ItemWidthStack.push_back(w_item_last); - for (int i = 0; i < components - 1; i++) + for (int i = 0; i < components - 2; i++) window->DC.ItemWidthStack.push_back(w_item_one); - window->DC.ItemWidth = window->DC.ItemWidthStack.back(); + window->DC.ItemWidth = (components == 1) ? w_item_last : w_item_one; g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; } void ImGui::PopItemWidth() { ImGuiWindow* window = GetCurrentWindow(); + window->DC.ItemWidth = window->DC.ItemWidthStack.back(); window->DC.ItemWidthStack.pop_back(); - window->DC.ItemWidth = window->DC.ItemWidthStack.empty() ? window->ItemWidthDefault : window->DC.ItemWidthStack.back(); } // Calculate default item width given value passed to PushItemWidth() or SetNextItemWidth(). @@ -7369,7 +7570,7 @@ ImVec2 ImGui::GetContentRegionMax() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImVec2 mx = window->ContentRegionRect.Max - window->Pos; - if (window->DC.CurrentColumns) + if (window->DC.CurrentColumns || g.CurrentTable) mx.x = window->WorkRect.Max.x - window->Pos.x; return mx; } @@ -7380,7 +7581,7 @@ ImVec2 ImGui::GetContentRegionMaxAbs() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImVec2 mx = window->ContentRegionRect.Max; - if (window->DC.CurrentColumns) + if (window->DC.CurrentColumns || g.CurrentTable) mx.x = window->WorkRect.Max.x; return mx; } @@ -7435,7 +7636,7 @@ void ImGui::BeginGroup() window->DC.CursorMaxPos = window->DC.CursorPos; window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); if (g.LogEnabled) - g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return + g.LogLinePosY = -FLT_MAX; // To enforce a carriage return } void ImGui::EndGroup() @@ -7456,7 +7657,7 @@ void ImGui::EndGroup() window->DC.CurrLineSize = group_data.BackupCurrLineSize; window->DC.CurrLineTextBaseOffset = group_data.BackupCurrLineTextBaseOffset; if (g.LogEnabled) - g.LogLinePosY = -FLT_MAX; // To enforce Log carriage return + g.LogLinePosY = -FLT_MAX; // To enforce a carriage return if (!group_data.EmitItem) { @@ -7724,7 +7925,7 @@ void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags toolt { // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. window->Hidden = true; - window->HiddenFramesCanSkipItems = 1; + window->HiddenFramesCanSkipItems = 1; // FIXME: This may not be necessary? ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; @@ -8021,7 +8222,10 @@ bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags fla // (this won't really last as settings will kick in, and is mostly for backward compatibility. user may do the same themselves) // FIXME: Should test for (PosCond & window->SetWindowPosAllowFlags) with the upcoming window. if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) == 0) - SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); + { + const ImGuiViewport* viewport = GetMainViewport(); + SetNextWindowPos(viewport->GetCenter(), ImGuiCond_FirstUseEver, ImVec2(0.5f, 0.5f)); + } flags |= ImGuiWindowFlags_Popup | ImGuiWindowFlags_Modal | ImGuiWindowFlags_NoCollapse; const bool is_open = Begin(name, p_open, flags); @@ -8114,6 +8318,9 @@ bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiPopupFlags popup_flag // r_avoid = the rectangle to avoid (e.g. for tooltip it is a rectangle around the mouse cursor which we want to avoid. for popups it's a small point around the cursor.) // r_outer = the visible area rectangle, minus safe area padding. If our popup size won't fit because of safe area padding we ignore it. +// (r_outer is usually equivalent to the viewport rectangle minus padding, but when multi-viewports are enabled and monitor +// information are available, it may represent the entire platform monitor from the frame of reference of the current viewport. +// this allows us to have tooltips/popups displayed out of the parent viewport.) ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy) { ImVec2 base_pos_clamped = ImClamp(ref_pos, r_outer.Min, r_outer.Max - size); @@ -8188,11 +8395,13 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s return pos; } +// Note that this is used for popups, which can overlap the non work-area of individual viewports. ImRect ImGui::GetWindowAllowedExtentRect(ImGuiWindow* window) { + ImGuiContext& g = *GImGui; IM_UNUSED(window); - ImVec2 padding = GImGui->Style.DisplaySafeAreaPadding; - ImRect r_screen = GetViewportRect(); + ImRect r_screen = ((ImGuiViewportP*)(void*)GetMainViewport())->GetMainRect(); + ImVec2 padding = g.Style.DisplaySafeAreaPadding; r_screen.Expand(ImVec2((r_screen.GetWidth() > padding.x * 2) ? -padding.x : 0.0f, (r_screen.GetHeight() > padding.y * 2) ? -padding.y : 0.0f)); return r_screen; } @@ -8636,8 +8845,8 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() // When navigation is active and mouse is disabled, decide on an arbitrary position around the bottom left of the currently navigated item. const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer]; ImVec2 pos = g.NavWindow->Pos + ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); - ImRect visible_rect = GetViewportRect(); - return ImFloor(ImClamp(pos, visible_rect.Min, visible_rect.Max)); // ImFloor() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. + ImGuiViewport* viewport = GetMainViewport(); + return ImFloor(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImFloor() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. } } @@ -8940,7 +9149,7 @@ static void ImGui::NavUpdate() // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) ImRect nav_rect_rel = g.NavWindow ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); - g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : GetViewportRect(); + g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : ImRect(0, 0, 0, 0); g.NavScoringRect.TranslateY(nav_scoring_rect_offset_y); g.NavScoringRect.Min.x = ImMin(g.NavScoringRect.Min.x + 1.0f, g.NavScoringRect.Max.x); g.NavScoringRect.Max.x = g.NavScoringRect.Min.x; @@ -9370,8 +9579,9 @@ void ImGui::NavUpdateWindowingOverlay() if (g.NavWindowingListWindow == NULL) g.NavWindowingListWindow = FindWindowByName("###NavWindowingList"); - SetNextWindowSizeConstraints(ImVec2(g.IO.DisplaySize.x * 0.20f, g.IO.DisplaySize.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX)); - SetNextWindowPos(g.IO.DisplaySize * 0.5f, ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + const ImGuiViewport* viewport = GetMainViewport(); + SetNextWindowSizeConstraints(ImVec2(viewport->Size.x * 0.20f, viewport->Size.y * 0.20f), ImVec2(FLT_MAX, FLT_MAX)); + SetNextWindowPos(viewport->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f); Begin("###NavWindowingList", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings); for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--) @@ -9722,54 +9932,63 @@ void ImGui::LogText(const char* fmt, ...) // Internal version that takes a position to decide on newline placement and pad items according to their depth. // We split text into individual lines to add current tree level padding +// FIXME: This code is a little complicated perhaps, considering simplifying the whole system. void ImGui::LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + const char* prefix = g.LogNextPrefix; + const char* suffix = g.LogNextSuffix; + g.LogNextPrefix = g.LogNextSuffix = NULL; + if (!text_end) text_end = FindRenderedTextEnd(text, text_end); - const bool log_new_line = ref_pos && (ref_pos->y > g.LogLinePosY + 1); + const bool log_new_line = ref_pos && (ref_pos->y > g.LogLinePosY + g.Style.FramePadding.y + 1); if (ref_pos) g.LogLinePosY = ref_pos->y; if (log_new_line) + { + LogText(IM_NEWLINE); g.LogLineFirstItem = true; + } - const char* text_remaining = text; - if (g.LogDepthRef > window->DC.TreeDepth) // Re-adjust padding if we have popped out of our starting depth + if (prefix) + LogRenderedText(ref_pos, prefix, prefix + strlen(prefix)); // Calculate end ourself to ensure "##" are included here. + + // Re-adjust padding if we have popped out of our starting depth + if (g.LogDepthRef > window->DC.TreeDepth) g.LogDepthRef = window->DC.TreeDepth; const int tree_depth = (window->DC.TreeDepth - g.LogDepthRef); + + const char* text_remaining = text; for (;;) { - // Split the string. Each new line (after a '\n') is followed by spacing corresponding to the current depth of our log entry. - // We don't add a trailing \n to allow a subsequent item on the same line to be captured. + // Split the string. Each new line (after a '\n') is followed by indentation corresponding to the current depth of our log entry. + // We don't add a trailing \n yet to allow a subsequent item on the same line to be captured. const char* line_start = text_remaining; const char* line_end = ImStreolRange(line_start, text_end); - const bool is_first_line = (line_start == text); const bool is_last_line = (line_end == text_end); - if (!is_last_line || (line_start != line_end)) + if (line_start != line_end || !is_last_line) { - const int char_count = (int)(line_end - line_start); - if (log_new_line || !is_first_line) - LogText(IM_NEWLINE "%*s%.*s", tree_depth * 4, "", char_count, line_start); - else if (g.LogLineFirstItem) - LogText("%*s%.*s", tree_depth * 4, "", char_count, line_start); - else - LogText(" %.*s", char_count, line_start); + const int line_length = (int)(line_end - line_start); + const int indentation = g.LogLineFirstItem ? tree_depth * 4 : 1; + LogText("%*s%.*s", indentation, "", line_length, line_start); g.LogLineFirstItem = false; + if (*line_end == '\n') + { + LogText(IM_NEWLINE); + g.LogLineFirstItem = true; + } } - else if (log_new_line) - { - // An empty "" string at a different Y position should output a carriage return. - LogText(IM_NEWLINE); - break; - } - if (is_last_line) break; text_remaining = line_end + 1; } + + if (suffix) + LogRenderedText(ref_pos, suffix, suffix + strlen(suffix)); } // Start logging/capturing text output @@ -9782,12 +10001,21 @@ void ImGui::LogBegin(ImGuiLogType type, int auto_open_depth) IM_ASSERT(g.LogBuffer.empty()); g.LogEnabled = true; g.LogType = type; + g.LogNextPrefix = g.LogNextSuffix = NULL; g.LogDepthRef = window->DC.TreeDepth; g.LogDepthToExpand = ((auto_open_depth >= 0) ? auto_open_depth : g.LogDepthToExpandDefault); g.LogLinePosY = FLT_MAX; g.LogLineFirstItem = true; } +// Important: doesn't copy underlying data, use carefully (prefix/suffix must be in scope at the time of the next LogRenderedText) +void ImGui::LogSetNextTextDecoration(const char* prefix, const char* suffix) +{ + ImGuiContext& g = *GImGui; + g.LogNextPrefix = prefix; + g.LogNextSuffix = suffix; +} + void ImGui::LogToTTY(int auto_open_depth) { ImGuiContext& g = *GImGui; @@ -10218,9 +10446,41 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl //----------------------------------------------------------------------------- // [SECTION] VIEWPORTS, PLATFORM WINDOWS //----------------------------------------------------------------------------- +// - GetMainViewport() +// - UpdateViewportsNewFrame() [Internal] +// (this section is more complete in the 'docking' branch) +//----------------------------------------------------------------------------- -// (this section is filled in the 'docking' branch) +ImGuiViewport* ImGui::GetMainViewport() +{ + ImGuiContext& g = *GImGui; + return g.Viewports[0]; +} +// Update viewports and monitor infos +static void ImGui::UpdateViewportsNewFrame() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.Viewports.Size == 1); + + // Update main viewport with current platform position. + // FIXME-VIEWPORT: Size is driven by backend/user code for backward-compatibility but we should aim to make this more consistent. + ImGuiViewportP* main_viewport = g.Viewports[0]; + main_viewport->Flags = ImGuiViewportFlags_IsPlatformWindow | ImGuiViewportFlags_OwnedByApp; + main_viewport->Pos = ImVec2(0.0f, 0.0f); + main_viewport->Size = g.IO.DisplaySize; + + for (int n = 0; n < g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + + // Lock down space taken by menu bars and status bars, reset the offset for fucntions like BeginMainMenuBar() to alter them again. + viewport->WorkOffsetMin = viewport->CurrWorkOffsetMin; + viewport->WorkOffsetMax = viewport->CurrWorkOffsetMax; + viewport->CurrWorkOffsetMin = viewport->CurrWorkOffsetMax = ImVec2(0.0f, 0.0f); + viewport->UpdateWorkRect(); + } +} //----------------------------------------------------------------------------- // [SECTION] DOCKING @@ -10392,6 +10652,8 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} //----------------------------------------------------------------------------- // [SECTION] METRICS/DEBUGGER WINDOW //----------------------------------------------------------------------------- +// - RenderViewportThumbnail() [Internal] +// - RenderViewportsThumbnails() [Internal] // - MetricsHelpMarker() [Internal] // - ShowMetricsWindow() // - DebugNodeColumns() [Internal] @@ -10399,6 +10661,7 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} // - DebugNodeDrawCmdShowMeshAndBoundingBox() [Internal] // - DebugNodeStorage() [Internal] // - DebugNodeTabBar() [Internal] +// - DebugNodeViewport() [Internal] // - DebugNodeWindow() [Internal] // - DebugNodeWindowSettings() [Internal] // - DebugNodeWindowsList() [Internal] @@ -10406,6 +10669,57 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} #ifndef IMGUI_DISABLE_METRICS_WINDOW +void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + ImVec2 scale = bb.GetSize() / viewport->Size; + ImVec2 off = bb.Min - viewport->Pos * scale; + float alpha_mul = 1.0f; + window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul * 0.40f)); + for (int i = 0; i != g.Windows.Size; i++) + { + ImGuiWindow* thumb_window = g.Windows[i]; + if (!thumb_window->WasActive || (thumb_window->Flags & ImGuiWindowFlags_ChildWindow)) + continue; + + ImRect thumb_r = thumb_window->Rect(); + ImRect title_r = thumb_window->TitleBarRect(); + thumb_r = ImRect(ImFloor(off + thumb_r.Min * scale), ImFloor(off + thumb_r.Max * scale)); + title_r = ImRect(ImFloor(off + title_r.Min * scale), ImFloor(off + ImVec2(title_r.Max.x, title_r.Min.y) * scale) + ImVec2(0,5)); // Exaggerate title bar height + thumb_r.ClipWithFull(bb); + title_r.ClipWithFull(bb); + const bool window_is_focused = (g.NavWindow && thumb_window->RootWindowForTitleBarHighlight == g.NavWindow->RootWindowForTitleBarHighlight); + window->DrawList->AddRectFilled(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_WindowBg, alpha_mul)); + window->DrawList->AddRectFilled(title_r.Min, title_r.Max, GetColorU32(window_is_focused ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg, alpha_mul)); + window->DrawList->AddRect(thumb_r.Min, thumb_r.Max, GetColorU32(ImGuiCol_Border, alpha_mul)); + window->DrawList->AddText(g.Font, g.FontSize * 1.0f, title_r.Min, GetColorU32(ImGuiCol_Text, alpha_mul), thumb_window->Name, FindRenderedTextEnd(thumb_window->Name)); + } + draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul)); +} + +static void RenderViewportsThumbnails() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + // We don't display full monitor bounds (we could, but it often looks awkward), instead we display just enough to cover all of our viewports. + float SCALE = 1.0f / 8.0f; + ImRect bb_full(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + for (int n = 0; n < g.Viewports.Size; n++) + bb_full.Add(g.Viewports[n]->GetMainRect()); + ImVec2 p = window->DC.CursorPos; + ImVec2 off = p - bb_full.Min * SCALE; + for (int n = 0; n < g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + ImRect viewport_draw_bb(off + (viewport->Pos) * SCALE, off + (viewport->Pos + viewport->Size) * SCALE); + ImGui::DebugRenderViewportThumbnail(window->DrawList, viewport, viewport_draw_bb); + } + ImGui::Dummy(bb_full.GetSize() * SCALE); +} + // Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds. static void MetricsHelpMarker(const char* desc) { @@ -10433,7 +10747,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; // Basic info - Text("Dear ImGui %s", ImGui::GetVersion()); + Text("Dear ImGui %s", GetVersion()); Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); @@ -10443,10 +10757,10 @@ void ImGui::ShowMetricsWindow(bool* p_open) Separator(); // Debugging enums - enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Content, WRT_ContentRegionRect, WRT_Count }; // Windows Rect Type - const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Content", "ContentRegionRect" }; - enum { TRT_OuterRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersIdeal, TRT_ColumnsContentRowsFrozen, TRT_ColumnsContentRowsUnfrozen, TRT_Count }; // Tables Rect Type - const char* trt_rects_names[TRT_Count] = { "OuterRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersIdeal", "ColumnsContentRowsFrozen", "ColumnsContentRowsUnfrozen" }; + enum { WRT_OuterRect, WRT_OuterRectClipped, WRT_InnerRect, WRT_InnerClipRect, WRT_WorkRect, WRT_Content, WRT_ContentIdeal, WRT_ContentRegionRect, WRT_Count }; // Windows Rect Type + const char* wrt_rects_names[WRT_Count] = { "OuterRect", "OuterRectClipped", "InnerRect", "InnerClipRect", "WorkRect", "Content", "ContentIdeal", "ContentRegionRect" }; + enum { TRT_OuterRect, TRT_InnerRect, TRT_WorkRect, TRT_HostClipRect, TRT_InnerClipRect, TRT_BackgroundClipRect, TRT_ColumnsRect, TRT_ColumnsWorkRect, TRT_ColumnsClipRect, TRT_ColumnsContentHeadersUsed, TRT_ColumnsContentHeadersIdeal, TRT_ColumnsContentFrozen, TRT_ColumnsContentUnfrozen, TRT_Count }; // Tables Rect Type + const char* trt_rects_names[TRT_Count] = { "OuterRect", "InnerRect", "WorkRect", "HostClipRect", "InnerClipRect", "BackgroundClipRect", "ColumnsRect", "ColumnsWorkRect", "ColumnsClipRect", "ColumnsContentHeadersUsed", "ColumnsContentHeadersIdeal", "ColumnsContentFrozen", "ColumnsContentUnfrozen" }; if (cfg->ShowWindowsRectsType < 0) cfg->ShowWindowsRectsType = WRT_WorkRect; if (cfg->ShowTablesRectsType < 0) @@ -10454,6 +10768,25 @@ void ImGui::ShowMetricsWindow(bool* p_open) struct Funcs { + static ImRect GetTableRect(ImGuiTable* table, int rect_type, int n) + { + if (rect_type == TRT_OuterRect) { return table->OuterRect; } + else if (rect_type == TRT_InnerRect) { return table->InnerRect; } + else if (rect_type == TRT_WorkRect) { return table->WorkRect; } + else if (rect_type == TRT_HostClipRect) { return table->HostClipRect; } + else if (rect_type == TRT_InnerClipRect) { return table->InnerClipRect; } + else if (rect_type == TRT_BackgroundClipRect) { return table->BgClipRect; } + else if (rect_type == TRT_ColumnsRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->MinX, table->InnerClipRect.Min.y, c->MaxX, table->InnerClipRect.Min.y + table->LastOuterHeight); } + else if (rect_type == TRT_ColumnsWorkRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->WorkRect.Min.y, c->WorkMaxX, table->WorkRect.Max.y); } + else if (rect_type == TRT_ColumnsClipRect) { ImGuiTableColumn* c = &table->Columns[n]; return c->ClipRect; } + else if (rect_type == TRT_ColumnsContentHeadersUsed){ ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersUsed, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } // Note: y1/y2 not always accurate + else if (rect_type == TRT_ColumnsContentHeadersIdeal){ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersIdeal, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } + else if (rect_type == TRT_ColumnsContentFrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXFrozen, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } + else if (rect_type == TRT_ColumnsContentUnfrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y + table->LastFirstRowHeight, c->ContentMaxXUnfrozen, table->InnerClipRect.Max.y); } + IM_ASSERT(0); + return ImRect(); + } + static ImRect GetWindowRect(ImGuiWindow* window, int rect_type) { if (rect_type == WRT_OuterRect) { return window->Rect(); } @@ -10461,7 +10794,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) else if (rect_type == WRT_InnerRect) { return window->InnerRect; } else if (rect_type == WRT_InnerClipRect) { return window->InnerClipRect; } else if (rect_type == WRT_WorkRect) { return window->WorkRect; } - else if (rect_type == WRT_Content) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } + else if (rect_type == WRT_Content) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } + else if (rect_type == WRT_ContentIdeal) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSizeIdeal); } else if (rect_type == WRT_ContentRegionRect) { return window->ContentRegionRect; } IM_ASSERT(0); return ImRect(); @@ -10495,16 +10829,83 @@ void ImGui::ShowMetricsWindow(bool* p_open) } Checkbox("Show ImDrawCmd mesh when hovering", &cfg->ShowDrawCmdMesh); Checkbox("Show ImDrawCmd bounding boxes when hovering", &cfg->ShowDrawCmdBoundingBoxes); + + Checkbox("Show tables rectangles", &cfg->ShowTablesRects); + SameLine(); + SetNextItemWidth(GetFontSize() * 12); + cfg->ShowTablesRects |= Combo("##show_table_rects_type", &cfg->ShowTablesRectsType, trt_rects_names, TRT_Count, TRT_Count); + if (cfg->ShowTablesRects && g.NavWindow != NULL) + { + for (int table_n = 0; table_n < g.Tables.GetSize(); table_n++) + { + ImGuiTable* table = g.Tables.GetByIndex(table_n); + if (table->LastFrameActive < g.FrameCount - 1 || (table->OuterWindow != g.NavWindow && table->InnerWindow != g.NavWindow)) + continue; + + BulletText("Table 0x%08X (%d columns, in '%s')", table->ID, table->ColumnsCount, table->OuterWindow->Name); + if (IsItemHovered()) + GetForegroundDrawList()->AddRect(table->OuterRect.Min - ImVec2(1, 1), table->OuterRect.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + Indent(); + char buf[128]; + for (int rect_n = 0; rect_n < TRT_Count; rect_n++) + { + if (rect_n >= TRT_ColumnsRect) + { + if (rect_n != TRT_ColumnsRect && rect_n != TRT_ColumnsClipRect) + continue; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImRect r = Funcs::GetTableRect(table, rect_n, column_n); + ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) Col %d %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), column_n, trt_rects_names[rect_n]); + Selectable(buf); + if (IsItemHovered()) + GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + } + } + else + { + ImRect r = Funcs::GetTableRect(table, rect_n, -1); + ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), trt_rects_names[rect_n]); + Selectable(buf); + if (IsItemHovered()) + GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + } + } + Unindent(); + } + } + TreePop(); } - // Contents + // Windows DebugNodeWindowsList(&g.Windows, "Windows"); - //DebugNodeWindowList(&g.WindowsFocusOrder, "WindowsFocusOrder"); - if (TreeNode("DrawLists", "Active DrawLists (%d)", g.DrawDataBuilder.Layers[0].Size)) + //DebugNodeWindowsList(&g.WindowsFocusOrder, "WindowsFocusOrder"); + + // DrawLists + int drawlist_count = 0; + for (int viewport_i = 0; viewport_i < g.Viewports.Size; viewport_i++) + drawlist_count += g.Viewports[viewport_i]->DrawDataBuilder.GetDrawListCount(); + if (TreeNode("DrawLists", "DrawLists (%d)", drawlist_count)) { - for (int i = 0; i < g.DrawDataBuilder.Layers[0].Size; i++) - DebugNodeDrawList(NULL, g.DrawDataBuilder.Layers[0][i], "DrawList"); + for (int viewport_i = 0; viewport_i < g.Viewports.Size; viewport_i++) + { + ImGuiViewportP* viewport = g.Viewports[viewport_i]; + for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) + for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++) + DebugNodeDrawList(NULL, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList"); + } + TreePop(); + } + + // Viewports + if (TreeNode("Viewports", "Viewports (%d)", g.Viewports.Size)) + { + Indent(GetTreeNodeToLabelSpacing()); + RenderViewportsThumbnails(); + Unindent(GetTreeNodeToLabelSpacing()); + for (int i = 0; i < g.Viewports.Size; i++) + DebugNodeViewport(g.Viewports[i]); TreePop(); } @@ -10528,7 +10929,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) } // Details for Tables - IM_UNUSED(trt_rects_names); #ifdef IMGUI_HAS_TABLE if (TreeNode("Tables", "Tables (%d)", g.Tables.GetSize())) { @@ -10659,11 +11059,29 @@ void ImGui::ShowMetricsWindow(bool* p_open) #ifdef IMGUI_HAS_TABLE // Overlay: Display Tables Rectangles - if (show_tables_rects) + if (cfg->ShowTablesRects) { for (int table_n = 0; table_n < g.Tables.GetSize(); table_n++) { ImGuiTable* table = g.Tables.GetByIndex(table_n); + if (table->LastFrameActive < g.FrameCount - 1) + continue; + ImDrawList* draw_list = GetForegroundDrawList(table->OuterWindow); + if (cfg->ShowTablesRectsType >= TRT_ColumnsRect) + { + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImRect r = Funcs::GetTableRect(table, cfg->ShowTablesRectsType, column_n); + ImU32 col = (table->HoveredColumnBody == column_n) ? IM_COL32(255, 255, 128, 255) : IM_COL32(255, 0, 128, 255); + float thickness = (table->HoveredColumnBody == column_n) ? 3.0f : 1.0f; + draw_list->AddRect(r.Min, r.Max, col, 0.0f, ~0, thickness); + } + } + else + { + ImRect r = Funcs::GetTableRect(table, cfg->ShowTablesRectsType, -1); + draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 0, 128, 255)); + } } } #endif // #ifdef IMGUI_HAS_TABLE @@ -10730,7 +11148,7 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); if (IsItemHovered() && (cfg->ShowDrawCmdMesh || cfg->ShowDrawCmdBoundingBoxes) && fg_draw_list) - DebugNodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, cfg->ShowDrawCmdMesh, cfg->ShowDrawCmdBoundingBoxes); + DebugNodeDrawCmdShowMeshAndBoundingBox(fg_draw_list, draw_list, pcmd, cfg->ShowDrawCmdMesh, cfg->ShowDrawCmdBoundingBoxes); if (!pcmd_node_open) continue; @@ -10751,7 +11169,7 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, ImFormatString(buf, IM_ARRAYSIZE(buf), "Mesh: ElemCount: %d, VtxOffset: +%d, IdxOffset: +%d, Area: ~%0.f px", pcmd->ElemCount, pcmd->VtxOffset, pcmd->IdxOffset, total_area); Selectable(buf); if (IsItemHovered() && fg_draw_list) - DebugNodeDrawCmdShowMeshAndBoundingBox(window, draw_list, pcmd, true, false); + DebugNodeDrawCmdShowMeshAndBoundingBox(fg_draw_list, draw_list, pcmd, true, false); // Display individual triangles/vertices. Hover on to get the corresponding triangle highlighted. ImGuiListClipper clipper; @@ -10784,33 +11202,32 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, } // [DEBUG] Display mesh/aabb of a ImDrawCmd -void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb) +void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb) { IM_ASSERT(show_mesh || show_aabb); - ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + draw_cmd->VtxOffset; // Draw wire-frame version of all triangles ImRect clip_rect = draw_cmd->ClipRect; ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - ImDrawListFlags backup_flags = fg_draw_list->Flags; - fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. + ImDrawListFlags backup_flags = out_draw_list->Flags; + out_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. for (unsigned int idx_n = draw_cmd->IdxOffset; idx_n < draw_cmd->IdxOffset + draw_cmd->ElemCount; ) { ImVec2 triangle[3]; for (int n = 0; n < 3; n++, idx_n++) vtxs_rect.Add((triangle[n] = vtx_buffer[idx_buffer ? idx_buffer[idx_n] : idx_n].pos)); if (show_mesh) - fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles + out_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles } // Draw bounding boxes if (show_aabb) { - fg_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU - fg_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles + out_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU + out_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles } - fg_draw_list->Flags = backup_flags; + out_draw_list->Flags = backup_flags; } // [DEBUG] Display contents of ImGuiStorage @@ -10862,6 +11279,26 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) } } +void ImGui::DebugNodeViewport(ImGuiViewportP* viewport) +{ + SetNextItemOpen(true, ImGuiCond_Once); + if (TreeNode("viewport0", "Viewport #%d", 0)) + { + ImGuiWindowFlags flags = viewport->Flags; + BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nWorkArea Offset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f", + viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y, + viewport->WorkOffsetMin.x, viewport->WorkOffsetMin.y, viewport->WorkOffsetMax.x, viewport->WorkOffsetMax.y); + BulletText("Flags: 0x%04X =%s%s%s", viewport->Flags, + (flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", + (flags & ImGuiViewportFlags_IsPlatformMonitor) ? " IsPlatformMonitor" : "", + (flags & ImGuiViewportFlags_OwnedByApp) ? " OwnedByApp" : ""); + for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) + for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++) + DebugNodeDrawList(NULL, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList"); + TreePop(); + } +} + void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) { if (window == NULL) @@ -10886,7 +11323,7 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) ImGuiWindowFlags flags = window->Flags; DebugNodeDrawList(window, window->DrawList, "DrawList"); - BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y); + BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f) Ideal (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y, window->ContentSizeIdeal.x, window->ContentSizeIdeal.y); BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", @@ -10919,7 +11356,6 @@ void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings* settings) settings->ID, settings->GetName(), settings->Pos.x, settings->Pos.y, settings->Size.x, settings->Size.y, settings->Collapsed); } - void ImGui::DebugNodeWindowsList(ImVector* windows, const char* label) { if (!TreeNode(label, "%s (%d)", label, windows->Size)) @@ -10939,12 +11375,13 @@ void ImGui::DebugNodeWindowsList(ImVector* windows, const char* la void ImGui::ShowMetricsWindow(bool*) {} void ImGui::DebugNodeColumns(ImGuiOldColumns*) {} void ImGui::DebugNodeDrawList(ImGuiWindow*, const ImDrawList*, const char*) {} -void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow*, const ImDrawList*, const ImDrawCmd*, bool, bool) {} +void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList*, const ImDrawList*, const ImDrawCmd*, bool, bool) {} void ImGui::DebugNodeStorage(ImGuiStorage*, const char*) {} void ImGui::DebugNodeTabBar(ImGuiTabBar*, const char*) {} void ImGui::DebugNodeWindow(ImGuiWindow*, const char*) {} void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings*) {} void ImGui::DebugNodeWindowsList(ImVector*, const char*) {} +void ImGui::DebugNodeViewport(ImGuiViewportP*) {} #endif diff --git a/imgui/imgui.h b/imgui/imgui.h index 6d2b54cd..408969ad 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (headers) // Help: @@ -19,25 +19,25 @@ /* Index of this file: -// Header mess -// Forward declarations and basic types -// ImGui API (Dear ImGui end-user API) -// Flags & Enumerations -// Memory allocations macros -// ImVector<> -// ImGuiStyle -// ImGuiIO -// Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload) -// Obsolete functions -// Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) -// Draw List API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) -// Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) +// [SECTION] Header mess +// [SECTION] Forward declarations and basic types +// [SECTION] Dear ImGui end-user API functions +// [SECTION] Flags & Enumerations +// [SECTION] Helpers: Memory allocations macros, ImVector<> +// [SECTION] ImGuiStyle +// [SECTION] ImGuiIO +// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) +// [SECTION] Obsolete functions +// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) +// [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) +// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) +// [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) */ #pragma once -// Configuration file with compile-time options (edit imconfig.h or #define IMGUI_USER_CONFIG to your own filename) +// Configuration file with compile-time options (edit imconfig.h or '#define IMGUI_USER_CONFIG "myfilename.h" from your build system') #ifdef IMGUI_USER_CONFIG #include IMGUI_USER_CONFIG #endif @@ -48,7 +48,7 @@ Index of this file: #ifndef IMGUI_DISABLE //----------------------------------------------------------------------------- -// Header mess +// [SECTION] Header mess //----------------------------------------------------------------------------- // Includes @@ -59,9 +59,10 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.80 WIP" -#define IMGUI_VERSION_NUM 17906 +#define IMGUI_VERSION "1.81" +#define IMGUI_VERSION_NUM 18100 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) +#define IMGUI_HAS_TABLE // Define attributes of all API symbols declarations (e.g. for DLL under Windows) // IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default backends files (imgui_impl_xxx.h) @@ -80,16 +81,18 @@ Index of this file: #endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. -#if (__cplusplus >= 201100) +#if (__cplusplus >= 201100) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201100) #define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 #else #define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. #endif + +// Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. #if !defined(IMGUI_USE_STB_SPRINTF) && defined(__clang__) -#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) // Apply printf-style warnings to our formatting functions. +#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) #define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) #elif !defined(IMGUI_USE_STB_SPRINTF) && defined(__GNUC__) && defined(__MINGW32__) -#define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) // Apply printf-style warnings to our formatting functions. +#define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) #define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0))) #else #define IM_FMTARGS(FMT) @@ -105,12 +108,12 @@ Index of this file: #endif #elif defined(__GNUC__) #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind -#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif //----------------------------------------------------------------------------- -// Forward declarations and basic types +// [SECTION] Forward declarations and basic types //----------------------------------------------------------------------------- // Forward declarations @@ -123,6 +126,7 @@ struct ImDrawListSplitter; // Helper to split a draw list into differen struct ImDrawVert; // A single vertex (pos + uv + col = 20 bytes by default. Override layout with IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT) struct ImFont; // Runtime data for a single font within a parent ImFontAtlas struct ImFontAtlas; // Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader +struct ImFontBuilderIO; // Opaque interface to a font builder (stb_truetype or FreeType). struct ImFontConfig; // Configuration data when adding a font or merging fonts struct ImFontGlyph; // A single font glyph (code point + coordinates within in ImFontAtlas + offset) struct ImFontGlyphRangesBuilder; // Helper to build glyph ranges from text/string data @@ -136,8 +140,11 @@ struct ImGuiPayload; // User data payload for drag and drop opera struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) struct ImGuiStorage; // Helper for key->value storage struct ImGuiStyle; // Runtime data for styling/colors +struct ImGuiTableSortSpecs; // Sorting specifications for a table (often handling sort specs for a single column, occasionally more) +struct ImGuiTableColumnSortSpecs; // Sorting specification for one column of a table struct ImGuiTextBuffer; // Helper to hold and append into a text buffer (~string builder) struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbbb][,ccccc]") +struct ImGuiViewport; // A Platform Window (always only one in 'master' branch), in the future may represent Platform Monitor // Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! @@ -151,7 +158,9 @@ typedef int ImGuiKey; // -> enum ImGuiKey_ // Enum: A typedef int ImGuiNavInput; // -> enum ImGuiNavInput_ // Enum: An input identifier for navigation typedef int ImGuiMouseButton; // -> enum ImGuiMouseButton_ // Enum: A mouse button identifier (0=left, 1=right, 2=middle) typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor identifier +typedef int ImGuiSortDirection; // -> enum ImGuiSortDirection_ // Enum: A sorting direction (ascending or descending) typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling +typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A color target for TableSetBgColor() typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect(), AddRectFilled() etc. typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build @@ -170,7 +179,11 @@ typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: f typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() typedef int ImGuiTabItemFlags; // -> enum ImGuiTabItemFlags_ // Flags: for BeginTabItem() +typedef int ImGuiTableFlags; // -> enum ImGuiTableFlags_ // Flags: For BeginTable() +typedef int ImGuiTableColumnFlags; // -> enum ImGuiTableColumnFlags_// Flags: For TableSetupColumn() +typedef int ImGuiTableRowFlags; // -> enum ImGuiTableRowFlags_ // Flags: For TableNextRow() typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: for TreeNode(), TreeNodeEx(), CollapsingHeader() +typedef int ImGuiViewportFlags; // -> enum ImGuiViewportFlags_ // Flags: for ImGuiViewport typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: for Begin(), BeginChild() // Other types @@ -178,10 +191,10 @@ typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: f typedef void* ImTextureID; // User data for rendering backend to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. #endif typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. -typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); -typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); +typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); // Callback function for ImGui::InputText() +typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints() -// Decoded character types +// Character types // (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. @@ -226,8 +239,8 @@ struct ImVec2 // 4D vector (often used to store floating-point colors) struct ImVec4 { - float x, y, z, w; - ImVec4() { x = y = z = w = 0.0f; } + float x, y, z, w; + ImVec4() { x = y = z = w = 0.0f; } ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } #ifdef IM_VEC4_CLASS_EXTRA IM_VEC4_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4. @@ -235,8 +248,8 @@ struct ImVec4 }; //----------------------------------------------------------------------------- -// ImGui: Dear ImGui end-user API -// (This is a namespace. You can add extra ImGui:: functions in your own separate file. Please don't modify imgui source files!) +// [SECTION] Dear ImGui end-user API functions +// (Note that ImGui:: being a namespace, you can add extra ImGui:: functions in your own separate file. Please don't modify imgui source files!) //----------------------------------------------------------------------------- namespace ImGui @@ -258,19 +271,19 @@ namespace ImGui IMGUI_API ImDrawData* GetDrawData(); // valid after Render() and until the next call to NewFrame(). this is what you have to render. // Demo, Debug, Information - IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window (previously called ShowTestWindow). demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! - IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. + IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window. demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debugger window. display Dear ImGui internals: windows, draw commands, various internal state, etc. + IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls). - IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.23" (essentially the compiled value for IMGUI_VERSION) + IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.80 WIP" (essentially the value for IMGUI_VERSION from the compiled version of imgui.cpp) // Styles IMGUI_API void StyleColorsDark(ImGuiStyle* dst = NULL); // new, recommended style (default) - IMGUI_API void StyleColorsClassic(ImGuiStyle* dst = NULL); // classic imgui style IMGUI_API void StyleColorsLight(ImGuiStyle* dst = NULL); // best used with borders and a custom, thicker font + IMGUI_API void StyleColorsClassic(ImGuiStyle* dst = NULL); // classic imgui style // Windows // - Begin() = push window to the stack and start appending to it. End() = pop window from the stack. @@ -330,20 +343,21 @@ namespace ImGui IMGUI_API void SetWindowFocus(const char* name); // set named window to be focused / top-most. use NULL to remove focus. // Content region - // - Those functions are bound to be redesigned soon (they are confusing, incomplete and return values in local window coordinates which increases confusion) - IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates + // - Retrieve available space from a given point. GetContentRegionAvail() is frequently useful. + // - Those functions are bound to be redesigned (they are confusing, incomplete and the Min/Max return values are in local window coordinates which increases confusion) IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() + IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min (roughly (0,0)-Scroll), in window coordinates IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates IMGUI_API float GetWindowContentRegionWidth(); // // Windows Scrolling - IMGUI_API float GetScrollX(); // get scrolling amount [0..GetScrollMaxX()] - IMGUI_API float GetScrollY(); // get scrolling amount [0..GetScrollMaxY()] - IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.x - WindowSize.x - IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.y - WindowSize.y - IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0..GetScrollMaxX()] - IMGUI_API void SetScrollY(float scroll_y); // set scrolling amount [0..GetScrollMaxY()] + IMGUI_API float GetScrollX(); // get scrolling amount [0 .. GetScrollMaxX()] + IMGUI_API float GetScrollY(); // get scrolling amount [0 .. GetScrollMaxY()] + IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0 .. GetScrollMaxX()] + IMGUI_API void SetScrollY(float scroll_y); // set scrolling amount [0 .. GetScrollMaxY()] + IMGUI_API float GetScrollMaxX(); // get maximum scrolling amount ~~ ContentSize.x - WindowSize.x - DecorationsSize.x + IMGUI_API float GetScrollMaxY(); // get maximum scrolling amount ~~ ContentSize.y - WindowSize.y - DecorationsSize.y IMGUI_API void SetScrollHereX(float center_x_ratio = 0.5f); // adjust scrolling amount to make current cursor position visible. center_x_ratio=0.0: left, 0.5: center, 1.0: right. When using to make a "default/current item" visible, consider using SetItemDefaultFocus() instead. IMGUI_API void SetScrollHereY(float center_y_ratio = 0.5f); // adjust scrolling amount to make current cursor position visible. center_y_ratio=0.0: top, 0.5: center, 1.0: bottom. When using to make a "default/current item" visible, consider using SetItemDefaultFocus() instead. IMGUI_API void SetScrollFromPosX(float local_x, float center_x_ratio = 0.5f); // adjust scrolling amount to make given position visible. Generally GetCursorStartPos() + offset to compute a valid position. @@ -352,32 +366,34 @@ namespace ImGui // Parameters stacks (shared) IMGUI_API void PushFont(ImFont* font); // use NULL as a shortcut to push default font IMGUI_API void PopFont(); - IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); + IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); // modify a style color. always use this if you modify the style after NewFrame(). IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4& col); IMGUI_API void PopStyleColor(int count = 1); - IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); - IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame(). + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. always use this if you modify the style after NewFrame(). IMGUI_API void PopStyleVar(int count = 1); IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets IMGUI_API void PopAllowKeyboardFocus(); IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. IMGUI_API void PopButtonRepeat(); - IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. - IMGUI_API ImFont* GetFont(); // get current font - IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied - IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API - IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier - IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied - IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied // Parameters stacks (current window) - IMGUI_API void PushItemWidth(float item_width); // push width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side). 0.0f = default to ~2/3 of windows width, + IMGUI_API void PushItemWidth(float item_width); // push width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -FLT_MIN always align width to the right side). IMGUI_API void PopItemWidth(); - IMGUI_API void SetNextItemWidth(float item_width); // set width of the _next_ common large "item+label" widget. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -1.0f always align width to the right side) + IMGUI_API void SetNextItemWidth(float item_width); // set width of the _next_ common large "item+label" widget. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -FLT_MIN always align width to the right side) IMGUI_API float CalcItemWidth(); // width of item given pushed settings and current cursor position. NOT necessarily the width of last item unlike most 'Item' functions. IMGUI_API void PushTextWrapPos(float wrap_local_pos_x = 0.0f); // push word-wrapping position for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space IMGUI_API void PopTextWrapPos(); + // Style read access + IMGUI_API ImFont* GetFont(); // get current font + IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied + IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API + IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier, packed as a 32-bit value suitable for ImDrawList + IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList + IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList + IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. + // Cursor / Layout // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. @@ -401,8 +417,8 @@ namespace ImGui IMGUI_API void SetCursorPosX(float local_x); // GetWindowPos() + GetCursorPos() == GetCursorScreenPos() etc.) IMGUI_API void SetCursorPosY(float local_y); // IMGUI_API ImVec2 GetCursorStartPos(); // initial cursor position in window coordinates - IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute screen coordinates [0..io.DisplaySize] (useful to work with ImDrawList API) - IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute screen coordinates [0..io.DisplaySize] + IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute coordinates (useful to work with ImDrawList API). generally top-left == GetMainViewport()->Pos == (0,0) in single viewport mode, and bottom-right == GetMainViewport()->Pos+Size == io.DisplaySize in single-viewport mode. + IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute coordinates IMGUI_API void AlignTextToFramePadding(); // vertically align upcoming text baseline to FramePadding.y so that it will align properly to regularly framed items (call if you have text on a line before a framed item) IMGUI_API float GetTextLineHeight(); // ~ FontSize IMGUI_API float GetTextLineHeightWithSpacing(); // ~ FontSize + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of text) @@ -454,12 +470,12 @@ namespace ImGui IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); IMGUI_API bool RadioButton(const char* label, bool active); // use with e.g. if (RadioButton("one", my_value==1)) { my_value = 1; } IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer - IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-1, 0), const char* overlay = NULL); + IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-FLT_MIN, 0), const char* overlay = NULL); IMGUI_API void Bullet(); // draw a small circle + keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses // Widgets: Combo Box // - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. - // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. + // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. This is analogous to how ListBox are created. IMGUI_API bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0); IMGUI_API void EndCombo(); // only call EndCombo() if BeginCombo() returns true! IMGUI_API bool Combo(const char* label, int* current_item, const char* const items[], int items_count, int popup_max_height_in_items = -1); @@ -556,7 +572,7 @@ namespace ImGui IMGUI_API void TreePop(); // ~ Unindent()+PopId() IMGUI_API float GetTreeNodeToLabelSpacing(); // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode IMGUI_API bool CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0); // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop(). - IMGUI_API bool CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags = 0); // when 'p_open' isn't NULL, display an additional small close button on upper right of the header + IMGUI_API bool CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFlags flags = 0); // when 'p_visible != NULL': if '*p_visible==true' display an additional small close button on upper right of the header which will set the bool to false when clicked, if '*p_visible==false' don't display the header. IMGUI_API void SetNextItemOpen(bool is_open, ImGuiCond cond = 0); // set next TreeNode/CollapsingHeader open state. // Widgets: Selectables @@ -566,14 +582,18 @@ namespace ImGui IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. // Widgets: List Boxes - // - FIXME: To be consistent with all the newer API, ListBoxHeader/ListBoxFooter should in reality be called BeginListBox/EndListBox. Will rename them. + // - This is essentially a thin wrapper to using BeginChild/EndChild with some stylistic changes. + // - The BeginListBox()/EndListBox() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() or any items. + // - The simplified/old ListBox() api are helpers over BeginListBox()/EndListBox() which are kept available for convenience purpose. This is analoguous to how Combos are created. + // - Choose frame width: size.x > 0.0f: custom / size.x < 0.0f or -FLT_MIN: right-align / size.x = 0.0f (default): use current ItemWidth + // - Choose frame height: size.y > 0.0f: custom / size.y < 0.0f or -FLT_MIN: bottom-align / size.y = 0.0f (default): arbitrary default height which can fit ~7 items + IMGUI_API bool BeginListBox(const char* label, const ImVec2& size = ImVec2(0, 0)); // open a framed scrolling region + IMGUI_API void EndListBox(); // only call EndListBox() if BeginListBox() returned true! IMGUI_API bool ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1); IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); - IMGUI_API bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)); // use if you want to reimplement ListBox() will custom data or interactions. if the function return true, you can output elements then call ListBoxFooter() afterwards. - IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // " - IMGUI_API void ListBoxFooter(); // terminate the scrolling region. only call ListBoxFooter() if ListBoxHeader() returned true! // Widgets: Data Plotting + // - Consider using ImPlot (https://github.com/epezent/implot) IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); IMGUI_API void PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); @@ -600,7 +620,7 @@ namespace ImGui IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL // Tooltips - // - Tooltip are windows following the mouse which do not take focus away. + // - Tooltip are windows following the mouse. They do not take focus away. IMGUI_API void BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). IMGUI_API void EndTooltip(); IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip(). @@ -643,11 +663,66 @@ namespace ImGui // - IsPopupOpen() with ImGuiPopupFlags_AnyPopupId + ImGuiPopupFlags_AnyPopupLevel: return true if any popup is open. IMGUI_API bool IsPopupOpen(const char* str_id, ImGuiPopupFlags flags = 0); // return true if the popup is open. - // Columns + // Tables + // [BETA API] API may evolve slightly! If you use this, please update to the next version when it comes out! + // - Full-featured replacement for old Columns API. + // - See Demo->Tables for demo code. + // - See top of imgui_tables.cpp for general commentary. + // - See ImGuiTableFlags_ and ImGuiTableColumnFlags_ enums for a description of available flags. + // The typical call flow is: + // - 1. Call BeginTable(). + // - 2. Optionally call TableSetupColumn() to submit column name/flags/defaults. + // - 3. Optionally call TableSetupScrollFreeze() to request scroll freezing of columns/rows. + // - 4. Optionally call TableHeadersRow() to submit a header row. Names are pulled from TableSetupColumn() data. + // - 5. Populate contents: + // - In most situations you can use TableNextRow() + TableSetColumnIndex(N) to start appending into a column. + // - If you are using tables as a sort of grid, where every columns is holding the same type of contents, + // you may prefer using TableNextColumn() instead of TableNextRow() + TableSetColumnIndex(). + // TableNextColumn() will automatically wrap-around into the next row if needed. + // - IMPORTANT: Comparatively to the old Columns() API, we need to call TableNextColumn() for the first column! + // - Summary of possible call flow: + // -------------------------------------------------------------------------------------------------------- + // TableNextRow() -> TableSetColumnIndex(0) -> Text("Hello 0") -> TableSetColumnIndex(1) -> Text("Hello 1") // OK + // TableNextRow() -> TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK + // TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK: TableNextColumn() automatically gets to next row! + // TableNextRow() -> Text("Hello 0") // Not OK! Missing TableSetColumnIndex() or TableNextColumn()! Text will not appear! + // -------------------------------------------------------------------------------------------------------- + // - 5. Call EndTable() + IMGUI_API bool BeginTable(const char* str_id, int column, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0.0f, 0.0f), float inner_width = 0.0f); + IMGUI_API void EndTable(); // only call EndTable() if BeginTable() returns true! + IMGUI_API void TableNextRow(ImGuiTableRowFlags row_flags = 0, float min_row_height = 0.0f); // append into the first cell of a new row. + IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return true when column is visible. + IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return true when column is visible. + // Tables: Headers & Columns declaration + // - Use TableSetupColumn() to specify label, resizing policy, default width/weight, id, various other flags etc. + // - Use TableHeadersRow() to create a header row and automatically submit a TableHeader() for each column. + // Headers are required to perform: reordering, sorting, and opening the context menu. + // The context menu can also be made available in columns body using ImGuiTableFlags_ContextMenuInBody. + // - You may manually submit headers using TableNextRow() + TableHeader() calls, but this is only useful in + // some advanced use cases (e.g. adding custom widgets in header row). + // - Use TableSetupScrollFreeze() to lock columns/rows so they stay visible when scrolled. + IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = 0.0f, ImU32 user_id = 0); + IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. + IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu + IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) + // Tables: Sorting + // - Call TableGetSortSpecs() to retrieve latest sort specs for the table. NULL when not sorting. + // - When 'SpecsDirty == true' you should sort your data. It will be true when sorting specs have changed + // since last call, or the first time. Make sure to set 'SpecsDirty = false' after sorting, else you may + // wastefully sort your data every frame! + // - Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). + IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). + // Tables: Miscellaneous functions + // - Functions args 'int column_n' treat the default value of -1 as the same as passing the current column index. + IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable) + IMGUI_API int TableGetColumnIndex(); // return current column index. + IMGUI_API int TableGetRowIndex(); // return current row index. + IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column. + IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags. Pass -1 to use current column. + IMGUI_API void TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n = -1); // change the color of a cell, row, or column. See ImGuiTableBgTarget_ flags for details. + + // Legacy Columns API (2020: prefer using Tables!) // - You can also use SameLine(pos_x) to mimic simplified columns. - // - The columns API is work-in-progress and rather lacking (columns are arguably the worst part of dear imgui at the moment!) - // - There is a maximum of 64 columns. - // - Currently working on new 'Tables' api which will replace columns around Q2 2020 (see GitHub #2957). IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished IMGUI_API int GetColumnIndex(); // get current column index @@ -675,7 +750,6 @@ namespace ImGui IMGUI_API void LogText(const char* fmt, ...) IM_FMTARGS(1); // pass text data straight to log (without being displayed) // Drag and Drop - // - [BETA API] API may evolve! // - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip as replacement) IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource() IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. @@ -686,6 +760,7 @@ namespace ImGui IMGUI_API const ImGuiPayload* GetDragDropPayload(); // peek directly into the current payload from anywhere. may return NULL. use ImGuiPayload::IsDataType() to test for the payload type. // Clipping + // - Mouse hovering is affected by ImGui::PushClipRect() calls, unlike direct calls to ImDrawList::PushClipRect() which are render only. IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect); IMGUI_API void PopClipRect(); @@ -715,6 +790,12 @@ namespace ImGui IMGUI_API ImVec2 GetItemRectSize(); // get size of last item IMGUI_API void SetItemAllowOverlap(); // allow last item to be overlapped by a subsequent item. sometimes useful with invisible buttons, selectables, etc. to catch unused area. + // Viewports + // - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. + // - In 'docking' branch with multi-viewport enabled, we extend this concept to have multiple active viewports. + // - In the future we will extend this concept further to also represent Platform Monitor and support a "no main platform window" operation mode. + IMGUI_API ImGuiViewport* GetMainViewport(); // return primary/default viewport. + // Miscellaneous Utilities IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped. IMGUI_API bool IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max); // test if rectangle (in screen space) is visible / not clipped. to perform coarse clipping on user's side. @@ -795,7 +876,7 @@ namespace ImGui } // namespace ImGui //----------------------------------------------------------------------------- -// Flags & Enumerations +// [SECTION] Flags & Enumerations //----------------------------------------------------------------------------- // Flags for ImGui::Begin() @@ -967,6 +1048,149 @@ enum ImGuiTabItemFlags_ ImGuiTabItemFlags_Trailing = 1 << 7 // Enforce the tab position to the right of the tab bar (before the scrolling buttons) }; +// Flags for ImGui::BeginTable() +// [BETA API] API may evolve slightly! If you use this, please update to the next version when it comes out! +// - Important! Sizing policies have complex and subtle side effects, more so than you would expect. +// Read comments/demos carefully + experiment with live demos to get acquainted with them. +// - The DEFAULT sizing policies are: +// - Default to ImGuiTableFlags_SizingFixedFit if ScrollX is on, or if host window has ImGuiWindowFlags_AlwaysAutoResize. +// - Default to ImGuiTableFlags_SizingStretchSame if ScrollX is off. +// - When ScrollX is off: +// - Table defaults to ImGuiTableFlags_SizingStretchSame -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch with same weight. +// - Columns sizing policy allowed: Stretch (default), Fixed/Auto. +// - Fixed Columns will generally obtain their requested width (unless the table cannot fit them all). +// - Stretch Columns will share the remaining width. +// - Mixed Fixed/Stretch columns is possible but has various side-effects on resizing behaviors. +// The typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. +// (this is because the visible order of columns have subtle but necessary effects on how they react to manual resizing). +// - When ScrollX is on: +// - Table defaults to ImGuiTableFlags_SizingFixedFit -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed +// - Columns sizing policy allowed: Fixed/Auto mostly. +// - Fixed Columns can be enlarged as needed. Table will show an horizontal scrollbar if needed. +// - When using auto-resizing (non-resizable) fixed columns, querying the content width to use item right-alignment e.g. SetNextItemWidth(-FLT_MIN) doesn't make sense, would create a feedback loop. +// - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable(). +// If you specify a value for 'inner_width' then effectively the scrolling space is known and Stretch or mixed Fixed/Stretch columns become meaningful again. +// - Read on documentation at the top of imgui_tables.cpp for details. +enum ImGuiTableFlags_ +{ + // Features + ImGuiTableFlags_None = 0, + ImGuiTableFlags_Resizable = 1 << 0, // Enable resizing columns. + ImGuiTableFlags_Reorderable = 1 << 1, // Enable reordering columns in header row (need calling TableSetupColumn() + TableHeadersRow() to display headers) + ImGuiTableFlags_Hideable = 1 << 2, // Enable hiding/disabling columns in context menu. + ImGuiTableFlags_Sortable = 1 << 3, // Enable sorting. Call TableGetSortSpecs() to obtain sort specs. Also see ImGuiTableFlags_SortMulti and ImGuiTableFlags_SortTristate. + ImGuiTableFlags_NoSavedSettings = 1 << 4, // Disable persisting columns order, width and sort settings in the .ini file. + ImGuiTableFlags_ContextMenuInBody = 1 << 5, // Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow(). + // Decorations + ImGuiTableFlags_RowBg = 1 << 6, // Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent of calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually) + ImGuiTableFlags_BordersInnerH = 1 << 7, // Draw horizontal borders between rows. + ImGuiTableFlags_BordersOuterH = 1 << 8, // Draw horizontal borders at the top and bottom. + ImGuiTableFlags_BordersInnerV = 1 << 9, // Draw vertical borders between columns. + ImGuiTableFlags_BordersOuterV = 1 << 10, // Draw vertical borders on the left and right sides. + ImGuiTableFlags_BordersH = ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_BordersOuterH, // Draw horizontal borders. + ImGuiTableFlags_BordersV = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersOuterV, // Draw vertical borders. + ImGuiTableFlags_BordersInner = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersInnerH, // Draw inner borders. + ImGuiTableFlags_BordersOuter = ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_BordersOuterH, // Draw outer borders. + ImGuiTableFlags_Borders = ImGuiTableFlags_BordersInner | ImGuiTableFlags_BordersOuter, // Draw all borders. + ImGuiTableFlags_NoBordersInBody = 1 << 11, // [ALPHA] Disable vertical borders in columns Body (borders will always appears in Headers). -> May move to style + ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 12, // [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers). -> May move to style + // Sizing Policy (read above for defaults) + ImGuiTableFlags_SizingFixedFit = 1 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching contents width. + ImGuiTableFlags_SizingFixedSame = 2 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching the maximum contents width of all columns. Implicitly enable ImGuiTableFlags_NoKeepColumnsVisible. + ImGuiTableFlags_SizingStretchProp = 3 << 13, // Columns default to _WidthStretch with default weights proportional to each columns contents widths. + ImGuiTableFlags_SizingStretchSame = 4 << 13, // Columns default to _WidthStretch with default weights all equal, unless overridden by TableSetupColumn(). + // Sizing Extra Options + ImGuiTableFlags_NoHostExtendX = 1 << 16, // Make outer width auto-fit to columns, overriding outer_size.x value. Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. + ImGuiTableFlags_NoHostExtendY = 1 << 17, // Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible. + ImGuiTableFlags_NoKeepColumnsVisible = 1 << 18, // Disable keeping column always minimally visible when ScrollX is off and table gets too small. Not recommended if columns are resizable. + ImGuiTableFlags_PreciseWidths = 1 << 19, // Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth. + // Clipping + ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze(). + // Padding + ImGuiTableFlags_PadOuterX = 1 << 21, // Default if BordersOuterV is on. Enable outer-most padding. Generally desirable if you have headers. + ImGuiTableFlags_NoPadOuterX = 1 << 22, // Default if BordersOuterV is off. Disable outer-most padding. + ImGuiTableFlags_NoPadInnerX = 1 << 23, // Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off). + // Scrolling + ImGuiTableFlags_ScrollX = 1 << 24, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this create a child window, ScrollY is currently generally recommended when using ScrollX. + ImGuiTableFlags_ScrollY = 1 << 25, // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. + // Sorting + ImGuiTableFlags_SortMulti = 1 << 26, // Hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1). + ImGuiTableFlags_SortTristate = 1 << 27, // Allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0). + + // [Internal] Combinations and masks + ImGuiTableFlags_SizingMask_ = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_SizingStretchSame + + // Obsolete names (will be removed soon) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //, ImGuiTableFlags_ColumnsWidthFixed = ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_ColumnsWidthStretch = ImGuiTableFlags_SizingStretchSame // WIP Tables 2020/12 + //, ImGuiTableFlags_SizingPolicyFixed = ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_SizingPolicyStretch = ImGuiTableFlags_SizingStretchSame // WIP Tables 2021/01 +#endif +}; + +// Flags for ImGui::TableSetupColumn() +enum ImGuiTableColumnFlags_ +{ + // Input configuration flags + ImGuiTableColumnFlags_None = 0, + ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden/disabled column. + ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column. + ImGuiTableColumnFlags_WidthStretch = 1 << 2, // Column will stretch. Preferable with horizontal scrolling disabled (default if table sizing policy is _SizingStretchSame or _SizingStretchProp). + ImGuiTableColumnFlags_WidthFixed = 1 << 3, // Column will not stretch. Preferable with horizontal scrolling enabled (default if table sizing policy is _SizingFixedFit and table is resizable). + ImGuiTableColumnFlags_NoResize = 1 << 4, // Disable manual resizing. + ImGuiTableColumnFlags_NoReorder = 1 << 5, // Disable manual reordering this column, this will also prevent other columns from crossing over this column. + ImGuiTableColumnFlags_NoHide = 1 << 6, // Disable ability to hide/disable this column. + ImGuiTableColumnFlags_NoClip = 1 << 7, // Disable clipping for this column (all NoClip columns will render in a same draw command). + ImGuiTableColumnFlags_NoSort = 1 << 8, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). + ImGuiTableColumnFlags_NoSortAscending = 1 << 9, // Disable ability to sort in the ascending direction. + ImGuiTableColumnFlags_NoSortDescending = 1 << 10, // Disable ability to sort in the descending direction. + ImGuiTableColumnFlags_NoHeaderWidth = 1 << 11, // Disable header text width contribution to automatic column width. + ImGuiTableColumnFlags_PreferSortAscending = 1 << 12, // Make the initial sort direction Ascending when first sorting on this column (default). + ImGuiTableColumnFlags_PreferSortDescending = 1 << 13, // Make the initial sort direction Descending when first sorting on this column. + ImGuiTableColumnFlags_IndentEnable = 1 << 14, // Use current Indent value when entering cell (default for column 0). + ImGuiTableColumnFlags_IndentDisable = 1 << 15, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored. + + // Output status flags, read-only via TableGetColumnFlags() + ImGuiTableColumnFlags_IsEnabled = 1 << 20, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags. + ImGuiTableColumnFlags_IsVisible = 1 << 21, // Status: is visible == is enabled AND not clipped by scrolling. + ImGuiTableColumnFlags_IsSorted = 1 << 22, // Status: is currently part of the sort specs + ImGuiTableColumnFlags_IsHovered = 1 << 23, // Status: is hovered by mouse + + // [Internal] Combinations and masks + ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed, + ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable, + ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered, + ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) + + // Obsolete names (will be removed soon) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //ImGuiTableColumnFlags_WidthAuto = ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize, // Column will not stretch and keep resizing based on submitted contents. +#endif +}; + +// Flags for ImGui::TableNextRow() +enum ImGuiTableRowFlags_ +{ + ImGuiTableRowFlags_None = 0, + ImGuiTableRowFlags_Headers = 1 << 0 // Identify header row (set default background color + width of its contents accounted different for auto column width) +}; + +// Enum for ImGui::TableSetBgColor() +// Background colors are rendering in 3 layers: +// - Layer 0: draw with RowBg0 color if set, otherwise draw with ColumnBg0 if set. +// - Layer 1: draw with RowBg1 color if set, otherwise draw with ColumnBg1 if set. +// - Layer 2: draw with CellBg color if set. +// The purpose of the two row/columns layers is to let you decide if a background color changes should override or blend with the existing color. +// When using ImGuiTableFlags_RowBg on the table, each row has the RowBg0 color automatically set for odd/even rows. +// If you set the color of RowBg0 target, your color will override the existing RowBg0 color. +// If you set the color of RowBg1 or ColumnBg1 target, your color will blend over the RowBg0 color. +enum ImGuiTableBgTarget_ +{ + ImGuiTableBgTarget_None = 0, + ImGuiTableBgTarget_RowBg0 = 1, // Set row background color 0 (generally used for background, automatically set when ImGuiTableFlags_RowBg is used) + ImGuiTableBgTarget_RowBg1 = 2, // Set row background color 1 (generally used for selection marking) + ImGuiTableBgTarget_CellBg = 3 // Set cell background color (top-most color) +}; + // Flags for ImGui::IsWindowFocused() enum ImGuiFocusedFlags_ { @@ -1044,6 +1268,14 @@ enum ImGuiDir_ ImGuiDir_COUNT }; +// A sorting direction +enum ImGuiSortDirection_ +{ + ImGuiSortDirection_None = 0, + ImGuiSortDirection_Ascending = 1, // Ascending = 0->9, A->Z etc. + ImGuiSortDirection_Descending = 2 // Descending = 9->0, Z->A etc. +}; + // User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array enum ImGuiKey_ { @@ -1085,7 +1317,7 @@ enum ImGuiKeyModFlags_ // Gamepad/Keyboard navigation // Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. // Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Backend: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). -// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://goo.gl/9LgVZW. +// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://dearimgui.org/controls_sheets. enum ImGuiNavInput_ { // Gamepad Mapping @@ -1188,6 +1420,11 @@ enum ImGuiCol_ ImGuiCol_PlotLinesHovered, ImGuiCol_PlotHistogram, ImGuiCol_PlotHistogramHovered, + ImGuiCol_TableHeaderBg, // Table header background + ImGuiCol_TableBorderStrong, // Table outer and header borders (prefer using Alpha=1.0 here) + ImGuiCol_TableBorderLight, // Table inner borders (prefer using Alpha=1.0 here) + ImGuiCol_TableRowBg, // Table row background (even rows) + ImGuiCol_TableRowBgAlt, // Table row background (odd rows) ImGuiCol_TextSelectedBg, ImGuiCol_DragDropTarget, ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item @@ -1195,11 +1432,6 @@ enum ImGuiCol_ ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active ImGuiCol_ModalWindowDimBg, // Darken/colorize entire screen behind a modal window, when one is active ImGuiCol_COUNT - - // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiCol_ModalWindowDarkening = ImGuiCol_ModalWindowDimBg // [renamed in 1.63] -#endif }; // Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure. @@ -1228,6 +1460,7 @@ enum ImGuiStyleVar_ ImGuiStyleVar_ItemSpacing, // ImVec2 ItemSpacing ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ItemInnerSpacing ImGuiStyleVar_IndentSpacing, // float IndentSpacing + ImGuiStyleVar_CellPadding, // ImVec2 CellPadding ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding ImGuiStyleVar_GrabMinSize, // float GrabMinSize @@ -1354,7 +1587,10 @@ enum ImGuiCond_ }; //----------------------------------------------------------------------------- -// Helpers: Memory allocations macros +// [SECTION] Helpers: Memory allocations macros, ImVector<> +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- // IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() // We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. // Defining a custom placement new() with a custom parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. @@ -1370,7 +1606,7 @@ inline void operator delete(void*, ImNewWrapper, void*) {} // This is only re template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p); } } //----------------------------------------------------------------------------- -// Helper: ImVector<> +// ImVector<> // Lightweight std::vector<>-like class to avoid dragging dependencies (also, some implementations of STL with debug enabled are absurdly slow, we bypass it so our code runs fast in debug). //----------------------------------------------------------------------------- // - You generally do NOT need to care or use this ever. But we need to make it available in imgui.h because some of our public structures are relying on it. @@ -1440,7 +1676,8 @@ struct ImVector }; //----------------------------------------------------------------------------- -// ImGuiStyle +// [SECTION] ImGuiStyle +//----------------------------------------------------------------------------- // You may modify the ImGui::GetStyle() main instance during initialization and before NewFrame(). // During the frame, use ImGui::PushStyleVar(ImGuiStyleVar_XXXX)/PopStyleVar() to alter the main style values, // and ImGui::PushStyleColor(ImGuiCol_XXX)/PopStyleColor() for colors. @@ -1464,6 +1701,7 @@ struct ImGuiStyle float FrameBorderSize; // Thickness of border around frames. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines. ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label). + ImVec2 CellPadding; // Padding within a table cell ImVec2 TouchExtraPadding; // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! float IndentSpacing; // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). @@ -1493,7 +1731,8 @@ struct ImGuiStyle }; //----------------------------------------------------------------------------- -// ImGuiIO +// [SECTION] ImGuiIO +//----------------------------------------------------------------------------- // Communicate most settings and inputs/outputs to Dear ImGui using this structure. // Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. //----------------------------------------------------------------------------- @@ -1506,7 +1745,7 @@ struct ImGuiIO ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by backend (imgui_impl_xxx files or custom backend) to communicate features supported by the backend. - ImVec2 DisplaySize; // // Main display size, in pixels. + ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size) float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. const char* IniFilename; // = "imgui.ini" // Path to .ini file. Set NULL to disable automatic .ini loading/saving, if e.g. you want to manually load/save from memory. @@ -1527,11 +1766,12 @@ struct ImGuiIO // Miscellaneous options bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by backend implementations. - bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl (was called io.OptMacOSXBehaviors prior to 1.63) - bool ConfigInputTextCursorBlink; // = true // Set to false to disable blinking cursor, for users who consider it distracting. (was called: io.OptCursorBlink prior to 1.63) + bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl. + bool ConfigInputTextCursorBlink; // = true // Enable blinking cursor (optional as some users consider it to be distracting). + bool ConfigDragClickToInputText; // = false // [BETA] Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving). Not desirable on devices without a keyboard. bool ConfigWindowsResizeFromEdges; // = true // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag) - bool ConfigWindowsMoveFromTitleBarOnly; // = false // [BETA] Set to true to only allow moving windows when clicked+dragged from the title bar. Windows without a title bar are not affected. - float ConfigMemoryCompactTimer; // = 60.0f // [BETA] Free transient windows/tables memory buffers when unused for given amount of time. Set to -1.0f to disable. + bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. + float ConfigMemoryCompactTimer; // = 60.0f // Timer (in seconds) to free transient windows/tables memory buffers when unused. Set to -1.0f to disable. //------------------------------------------------------------------ // Platform Functions @@ -1627,7 +1867,7 @@ struct ImGuiIO }; //----------------------------------------------------------------------------- -// Misc data structures +// [SECTION] Misc data structures //----------------------------------------------------------------------------- // Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used. @@ -1700,14 +1940,43 @@ struct ImGuiPayload bool IsDelivery() const { return Delivery; } }; +// Sorting specification for one column of a table (sizeof == 12 bytes) +struct ImGuiTableColumnSortSpecs +{ + ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) + ImS16 ColumnIndex; // Index of the column + ImS16 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) + ImGuiSortDirection SortDirection : 8; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending (you can use this or SortSign, whichever is more convenient for your sort function) + + ImGuiTableColumnSortSpecs() { memset(this, 0, sizeof(*this)); } +}; + +// Sorting specifications for a table (often handling sort specs for a single column, occasionally more) +// Obtained by calling TableGetSortSpecs(). +// When 'SpecsDirty == true' you can sort your data. It will be true with sorting specs have changed since last call, or the first time. +// Make sure to set 'SpecsDirty = false' after sorting, else you may wastefully sort your data every frame! +struct ImGuiTableSortSpecs +{ + const ImGuiTableColumnSortSpecs* Specs; // Pointer to sort spec array. + int SpecsCount; // Sort spec count. Most often 1. May be > 1 when ImGuiTableFlags_SortMulti is enabled. May be == 0 when ImGuiTableFlags_SortTristate is enabled. + bool SpecsDirty; // Set to true when specs have changed since last time! Use this to sort again, then clear the flag. + + ImGuiTableSortSpecs() { memset(this, 0, sizeof(*this)); } +}; + //----------------------------------------------------------------------------- -// Obsolete functions (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) +// [SECTION] Obsolete functions +// (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) // Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. //----------------------------------------------------------------------------- #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.81 (from February 2021) + IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items + static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } + static inline void ListBoxFooter() { EndListBox(); } // OBSOLETED in 1.79 (from August 2020) static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! // OBSOLETED in 1.78 (from June 2020) @@ -1737,15 +2006,11 @@ namespace ImGui static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } // OBSOLETED in 1.66 (from Sep 2018) static inline void SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); } - // OBSOLETED in 1.63 (between Aug 2018 and Sept 2018) - static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } } -typedef ImGuiInputTextCallback ImGuiTextEditCallback; // OBSOLETED in 1.63 (from Aug 2018): made the names consistent -typedef ImGuiInputTextCallbackData ImGuiTextEditCallbackData; #endif //----------------------------------------------------------------------------- -// Helpers +// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) //----------------------------------------------------------------------------- // Helper: Unicode defines @@ -1888,6 +2153,7 @@ struct ImGuiListClipper // [Internal] int ItemsCount; int StepNo; + int ItemsFrozen; float ItemsHeight; float StartPosY; @@ -1946,7 +2212,7 @@ struct ImColor }; //----------------------------------------------------------------------------- -// Draw List API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) +// [SECTION] Drawing API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) // Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. //----------------------------------------------------------------------------- @@ -2028,8 +2294,9 @@ struct ImDrawChannel ImVector _IdxBuffer; }; + // Split/Merge functions are used to split the draw list into different layers which can be drawn into out of order. -// This is used by the Columns api, so items of each column can be batched together in a same draw call. +// This is used by the Columns/Tables API, so items of each column can be batched together in a same draw call. struct ImDrawListSplitter { int _Current; // Current channel number (0) @@ -2076,7 +2343,8 @@ enum ImDrawListFlags_ // Each dear imgui window contains its own ImDrawList. You can use ImGui::GetWindowDrawList() to // access the current window draw list and draw custom primitives. // You can interleave normal ImGui:: calls and adding primitives to the current draw list. -// All positions are generally in pixel coordinates (top-left at (0,0), bottom-right at io.DisplaySize), but you are totally free to apply whatever transformation matrix to want to the data (if you apply such transformation you'll want to apply it to ClipRect as well) +// In single viewport mode, top-left is == GetMainViewport()->Pos (generally 0,0), bottom-right is == GetMainViewport()->Pos+Size (generally io.DisplaySize). +// You are totally free to apply whatever transformation matrix to want to the data (depending on the use of the transformation you may want to apply it to ClipRect as well!) // Important: Primitives are always added to the list and not culled (culling is done at higher-level by ImGui:: functions), if you use this API a lot consider coarse culling your drawn objects. struct ImDrawList { @@ -2097,6 +2365,7 @@ struct ImDrawList ImVector _Path; // [Internal] current path building ImDrawCmdHeader _CmdHeader; // [Internal] template of active commands. Fields should match those of CmdBuffer.back(). ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!) + float _FringeScale; // [Internal] anti-alias fringe is scaled by this value, this helps to keep things sharp while zooming at vertex buffer content // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) ImDrawList(const ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; } @@ -2132,7 +2401,8 @@ struct ImDrawList IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness); IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. - IMGUI_API void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); + IMGUI_API void AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Cubic Bezier (4 control points) + IMGUI_API void AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments = 0); // Quadratic Bezier (3 control points) // Image primitives // - Read FAQ to understand what ImTextureID is. @@ -2149,8 +2419,9 @@ struct ImDrawList inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } // Note: Anti-aliased filling requires points to be in clockwise order. inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 10); - IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle - IMGUI_API void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); + IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle + IMGUI_API void PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); // Cubic Bezier (4 control points) + IMGUI_API void PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments = 0); // Quadratic Bezier (3 control points) IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); // Advanced @@ -2180,6 +2451,11 @@ struct ImDrawList inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } + inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } +#endif + // [Internal helpers] IMGUI_API void _ResetForNewFrame(); IMGUI_API void _ClearFreeMemory(); @@ -2195,24 +2471,23 @@ struct ImDrawList struct ImDrawData { bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. - ImDrawList** CmdLists; // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here. int CmdListsCount; // Number of ImDrawList* to render int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size - ImVec2 DisplayPos; // Upper-left position of the viewport to render (== upper-left of the orthogonal projection matrix to use) - ImVec2 DisplaySize; // Size of the viewport to render (== io.DisplaySize for the main viewport) (DisplayPos + DisplaySize == lower-right of the orthogonal projection matrix to use) + ImDrawList** CmdLists; // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here. + ImVec2 DisplayPos; // Top-left position of the viewport to render (== top-left of the orthogonal projection matrix to use) (== GetMainViewport()->Pos for the main viewport, == (0.0) in most single-viewport applications) + ImVec2 DisplaySize; // Size of the viewport to render (== GetMainViewport()->Size for the main viewport, == io.DisplaySize in most single-viewport applications) ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display. // Functions - ImDrawData() { Valid = false; Clear(); } - ~ImDrawData() { Clear(); } - void Clear() { Valid = false; CmdLists = NULL; CmdListsCount = TotalVtxCount = TotalIdxCount = 0; DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.f, 0.f); } // The ImDrawList are owned by ImGuiContext! + ImDrawData() { Clear(); } + void Clear() { memset(this, 0, sizeof(*this)); } // The ImDrawList are owned by ImGuiContext! IMGUI_API void DeIndexAllBuffers(); // Helper to convert all buffers from indexed to non-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! IMGUI_API void ScaleClipRects(const ImVec2& fb_scale); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than Dear ImGui expects, or if there is a difference between your window resolution and framebuffer resolution. }; //----------------------------------------------------------------------------- -// Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont) +// [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontAtlasFlags, ImFontAtlas, ImFontGlyphRangesBuilder, ImFont) //----------------------------------------------------------------------------- struct ImFontConfig @@ -2222,8 +2497,8 @@ struct ImFontConfig bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself). int FontNo; // 0 // Index of font within TTF/OTF file float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height). - int OversampleH; // 3 // Rasterize at higher quality for sub-pixel positioning. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. - int OversampleV; // 1 // Rasterize at higher quality for sub-pixel positioning. We don't use sub-pixel positions on the Y axis. + int OversampleH; // 3 // Rasterize at higher quality for sub-pixel positioning. Note the difference between 2 and 3 is minimal so you can reduce this to 2 to save memory. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. + int OversampleV; // 1 // Rasterize at higher quality for sub-pixel positioning. This is not really useful as we don't use sub-pixel positions on the Y axis. bool PixelSnapH; // false // Align every glyph to pixel boundary. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1. ImVec2 GlyphExtraSpacing; // 0, 0 // Extra spacing (in pixels) between glyphs. Only X axis is supported for now. ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input. @@ -2231,7 +2506,7 @@ struct ImFontConfig float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights. - unsigned int RasterizerFlags; // 0x00 // Settings for custom font rasterizer (e.g. ImGuiFreeType). Leave as zero if you aren't using one. + unsigned int FontBuilderFlags; // 0 // Settings for custom font builder. THIS IS BUILDER IMPLEMENTATION DEPENDENT. Leave as zero if unsure. float RasterizerMultiply; // 1.0f // Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. ImWchar EllipsisChar; // -1 // Explicitly specify unicode codepoint of ellipsis character. When fonts are being merged first specified ellipsis will be used. @@ -2246,8 +2521,9 @@ struct ImFontConfig // (Note: some language parsers may fail to convert the 31+1 bitfield members, in this case maybe drop store a single u32 or we can rework this) struct ImFontGlyph { - unsigned int Codepoint : 31; // 0x0000..0xFFFF - unsigned int Visible : 1; // Flag to allow early out when rendering + unsigned int Colored : 1; // Flag to indicate glyph is colored and should generally ignore tinting (make it usable with no shift on little-endian as this is used in loops) + unsigned int Visible : 1; // Flag to indicate glyph has no visible pixels (e.g. space). Allow early out when rendering. + unsigned int Codepoint : 30; // 0x0000..0x10FFFF float AdvanceX; // Distance to next character (= data from font + ImFontConfig::GlyphExtraSpacing.x baked in) float X0, Y0, X1, Y1; // Glyph corners float U0, V0, U1, V1; // Texture coordinates @@ -2343,7 +2619,7 @@ struct ImFontAtlas // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data. IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin IMGUI_API const ImWchar* GetGlyphRangesKorean(); // Default + Korean characters - IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs + IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 2999 Ideographs IMGUI_API const ImWchar* GetGlyphRangesChineseFull(); // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs IMGUI_API const ImWchar* GetGlyphRangesChineseSimplifiedCommon();// Default + Half-Width + Japanese Hiragana/Katakana + set of 2500 CJK Unified Ideographs for common simplified Chinese IMGUI_API const ImWchar* GetGlyphRangesCyrillic(); // Default + about 400 Cyrillic characters @@ -2391,6 +2667,10 @@ struct ImFontAtlas ImVector ConfigData; // Configuration data ImVec4 TexUvLines[IM_DRAWLIST_TEX_LINES_WIDTH_MAX + 1]; // UVs for baked anti-aliased lines + // [Internal] Font builder + const ImFontBuilderIO* FontBuilderIO; // Opaque interface to a font builder (default to stb_truetype, can be changed to use FreeType by defining IMGUI_ENABLE_FREETYPE). + unsigned int FontBuilderFlags; // Shared flags (for all fonts) for custom font builder. THIS IS BUILD IMPLEMENTATION DEPENDENT. Per-font override is also available in ImFontConfig. + // [Internal] Packing data int PackIdMouseCursors; // Custom texture rectangle ID for white pixel and mouse cursors int PackIdLines; // Custom texture rectangle ID for baked anti-aliased lines @@ -2454,6 +2734,43 @@ struct ImFont IMGUI_API bool IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last); }; +//----------------------------------------------------------------------------- +// [SECTION] Viewports +//----------------------------------------------------------------------------- + +// Flags stored in ImGuiViewport::Flags +enum ImGuiViewportFlags_ +{ + ImGuiViewportFlags_None = 0, + ImGuiViewportFlags_IsPlatformWindow = 1 << 0, // Represent a Platform Window + ImGuiViewportFlags_IsPlatformMonitor = 1 << 1, // Represent a Platform Monitor (unused yet) + ImGuiViewportFlags_OwnedByApp = 1 << 2 // Platform Window: is created/managed by the application (rather than a dear imgui backend) +}; + +// - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. +// - In 'docking' branch with multi-viewport enabled, we extend this concept to have multiple active viewports. +// - In the future we will extend this concept further to also represent Platform Monitor and support a "no main platform window" operation mode. +// - About Main Area vs Work Area: +// - Main Area = entire viewport. +// - Work Area = entire viewport minus sections used by main menu bars (for platform windows), or by task bar (for platform monitor). +// - Windows are generally trying to stay within the Work Area of their host viewport. +struct ImGuiViewport +{ + ImGuiViewportFlags Flags; // See ImGuiViewportFlags_ + ImVec2 Pos; // Main Area: Position of the viewport (Dear Imgui coordinates are the same as OS desktop/native coordinates) + ImVec2 Size; // Main Area: Size of the viewport. + ImVec2 WorkPos; // Work Area: Position of the viewport minus task bars, menus bars, status bars (>= Pos) + ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size) + + ImGuiViewport() { memset(this, 0, sizeof(*this)); } + + // Helpers + ImVec2 GetCenter() const { return ImVec2(Pos.x + Size.x * 0.5f, Pos.y + Size.y * 0.5f); } + ImVec2 GetWorkCenter() const { return ImVec2(WorkPos.x + WorkSize.x * 0.5f, WorkPos.y + WorkSize.y * 0.5f); } +}; + +//----------------------------------------------------------------------------- + #if defined(__clang__) #pragma clang diagnostic pop #elif defined(__GNUC__) diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 71fe735f..90e91aa0 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (demo code) // Help: @@ -38,12 +38,21 @@ // and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. +// Navigating this file: +// - In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. + /* Index of this file: // [SECTION] Forward Declarations, Helpers // [SECTION] Demo Window / ShowDemoWindow() +// - sub section: ShowDemoWindowWidgets() +// - sub section: ShowDemoWindowLayout() +// - sub section: ShowDemoWindowPopups() +// - sub section: ShowDemoWindowTables() +// - sub section: ShowDemoWindowMisc() // [SECTION] About Window / ShowAboutWindow() // [SECTION] Style Editor / ShowStyleEditor() // [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar() @@ -54,8 +63,9 @@ Index of this file: // [SECTION] Example App: Long Text / ShowExampleAppLongText() // [SECTION] Example App: Auto Resize / ShowExampleAppAutoResize() // [SECTION] Example App: Constrained Resize / ShowExampleAppConstrainedResize() -// [SECTION] Example App: Simple Overlay / ShowExampleAppSimpleOverlay() -// [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles() +// [SECTION] Example App: Simple overlay / ShowExampleAppSimpleOverlay() +// [SECTION] Example App: Fullscreen window / ShowExampleAppFullscreen() +// [SECTION] Example App: Manipulating window titles / ShowExampleAppWindowTitles() // [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering() // [SECTION] Example App: Documents Handling / ShowExampleAppDocuments() @@ -68,6 +78,7 @@ Index of this file: #include "imgui.h" #ifndef IMGUI_DISABLE +// System includes #include // toupper #include // INT_MIN, INT_MAX #include // sqrtf, powf, cosf, sinf, floorf, ceilf @@ -132,6 +143,15 @@ Index of this file: #define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B)) #define IM_CLAMP(V, MN, MX) ((V) < (MN) ? (MN) : (V) > (MX) ? (MX) : (V)) +// Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall +#ifndef IMGUI_CDECL +#ifdef _MSC_VER +#define IMGUI_CDECL __cdecl +#else +#define IMGUI_CDECL +#endif +#endif + //----------------------------------------------------------------------------- // [SECTION] Forward Declarations, Helpers //----------------------------------------------------------------------------- @@ -149,6 +169,7 @@ static void ShowExampleAppLongText(bool* p_open); static void ShowExampleAppAutoResize(bool* p_open); static void ShowExampleAppConstrainedResize(bool* p_open); static void ShowExampleAppSimpleOverlay(bool* p_open); +static void ShowExampleAppFullscreen(bool* p_open); static void ShowExampleAppWindowTitles(bool* p_open); static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleMenuFile(); @@ -206,6 +227,7 @@ void ImGui::ShowUserGuide() // - ShowDemoWindowWidgets() // - ShowDemoWindowLayout() // - ShowDemoWindowPopups() +// - ShowDemoWindowTables() // - ShowDemoWindowColumns() // - ShowDemoWindowMisc() //----------------------------------------------------------------------------- @@ -215,6 +237,7 @@ void ImGui::ShowUserGuide() static void ShowDemoWindowWidgets(); static void ShowDemoWindowLayout(); static void ShowDemoWindowPopups(); +static void ShowDemoWindowTables(); static void ShowDemoWindowColumns(); static void ShowDemoWindowMisc(); @@ -230,6 +253,7 @@ void ImGui::ShowDemoWindow(bool* p_open) // Examples Apps (accessible from the "Examples" menu) static bool show_app_main_menu_bar = false; static bool show_app_documents = false; + static bool show_app_console = false; static bool show_app_log = false; static bool show_app_layout = false; @@ -238,6 +262,7 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool show_app_auto_resize = false; static bool show_app_constrained_resize = false; static bool show_app_simple_overlay = false; + static bool show_app_fullscreen = false; static bool show_app_window_titles = false; static bool show_app_custom_rendering = false; @@ -252,6 +277,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize); if (show_app_constrained_resize) ShowExampleAppConstrainedResize(&show_app_constrained_resize); if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay); + if (show_app_fullscreen) ShowExampleAppFullscreen(&show_app_fullscreen); if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); @@ -295,7 +321,8 @@ void ImGui::ShowDemoWindow(bool* p_open) // We specify a default position/size in case there's no data in the .ini file. // We only do it to make the demo applications a little more welcoming, but typically this isn't required. - ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); + const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 650, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver); // Main body of the Demo window starts here. @@ -333,6 +360,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize); ImGui::MenuItem("Constrained-resizing window", NULL, &show_app_constrained_resize); ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay); + ImGui::MenuItem("Fullscreen window", NULL, &show_app_fullscreen); ImGui::MenuItem("Manipulating window titles", NULL, &show_app_window_titles); ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering); ImGui::MenuItem("Documents", NULL, &show_app_documents); @@ -400,7 +428,9 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility."); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); - ImGui::SameLine(); HelpMarker("Set to false to disable blinking cursor, for users who consider it distracting"); + ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting)"); + ImGui::Checkbox("io.ConfigDragClickToInputText", &io.ConfigDragClickToInputText); + ImGui::SameLine(); HelpMarker("Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving)."); ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); @@ -456,23 +486,27 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::CollapsingHeader("Window options")) { - ImGui::Checkbox("No titlebar", &no_titlebar); ImGui::SameLine(150); - ImGui::Checkbox("No scrollbar", &no_scrollbar); ImGui::SameLine(300); - ImGui::Checkbox("No menu", &no_menu); - ImGui::Checkbox("No move", &no_move); ImGui::SameLine(150); - ImGui::Checkbox("No resize", &no_resize); ImGui::SameLine(300); - ImGui::Checkbox("No collapse", &no_collapse); - ImGui::Checkbox("No close", &no_close); ImGui::SameLine(150); - ImGui::Checkbox("No nav", &no_nav); ImGui::SameLine(300); - ImGui::Checkbox("No background", &no_background); - ImGui::Checkbox("No bring to front", &no_bring_to_front); + if (ImGui::BeginTable("split", 3)) + { + ImGui::TableNextColumn(); ImGui::Checkbox("No titlebar", &no_titlebar); + ImGui::TableNextColumn(); ImGui::Checkbox("No scrollbar", &no_scrollbar); + ImGui::TableNextColumn(); ImGui::Checkbox("No menu", &no_menu); + ImGui::TableNextColumn(); ImGui::Checkbox("No move", &no_move); + ImGui::TableNextColumn(); ImGui::Checkbox("No resize", &no_resize); + ImGui::TableNextColumn(); ImGui::Checkbox("No collapse", &no_collapse); + ImGui::TableNextColumn(); ImGui::Checkbox("No close", &no_close); + ImGui::TableNextColumn(); ImGui::Checkbox("No nav", &no_nav); + ImGui::TableNextColumn(); ImGui::Checkbox("No background", &no_background); + ImGui::TableNextColumn(); ImGui::Checkbox("No bring to front", &no_bring_to_front); + ImGui::EndTable(); + } } // All demo contents ShowDemoWindowWidgets(); ShowDemoWindowLayout(); ShowDemoWindowPopups(); - ShowDemoWindowColumns(); + ShowDemoWindowTables(); ShowDemoWindowMisc(); // End of ShowDemoWindow() @@ -557,13 +591,12 @@ static void ShowDemoWindowWidgets() { // Using the _simplified_ one-liner Combo() api here - // See "Combo" section for examples of how to use the more complete BeginCombo()/EndCombo() api. + // See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api. const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; static int item_current = 0; ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); ImGui::SameLine(); HelpMarker( - "Refer to the \"Combo\" section below for an explanation of the full BeginCombo/EndCombo API, " - "and demonstration of various flags.\n"); + "Using the simplified one-liner Combo API here.\nRefer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API."); } { @@ -662,14 +695,13 @@ static void ShowDemoWindowWidgets() } { - // List box + // Using the _simplified_ one-liner ListBox() api here + // See "List boxes" section for examples of how to use the more flexible BeginListBox()/EndListBox() api. const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; static int item_current = 1; - ImGui::ListBox("listbox\n(single select)", &item_current, items, IM_ARRAYSIZE(items), 4); - - //static int listbox_item_current2 = 2; - //ImGui::SetNextItemWidth(-1); - //ImGui::ListBox("##listbox2", &listbox_item_current2, listbox_items, IM_ARRAYSIZE(listbox_items), 4); + ImGui::ListBox("listbox", &item_current, items, IM_ARRAYSIZE(items), 4); + ImGui::SameLine(); HelpMarker( + "Using the simplified one-liner ListBox API here.\nRefer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API."); } ImGui::TreePop(); @@ -982,7 +1014,7 @@ static void ShowDemoWindowWidgets() // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; - static int item_current_idx = 0; // Here our selection data is an index. + static int item_current_idx = 0; // Here we store our selection data as an index. const char* combo_label = items[item_current_idx]; // Label to preview before opening the combo (technically it could be anything) if (ImGui::BeginCombo("combo 1", combo_label, flags)) { @@ -1015,6 +1047,48 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + if (ImGui::TreeNode("List boxes")) + { + // Using the generic BeginListBox() API, you have full control over how to display the combo contents. + // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively + // stored in the object itself, etc.) + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; + static int item_current_idx = 0; // Here we store our selection data as an index. + if (ImGui::BeginListBox("listbox 1")) + { + for (int n = 0; n < IM_ARRAYSIZE(items); n++) + { + const bool is_selected = (item_current_idx == n); + if (ImGui::Selectable(items[n], is_selected)) + item_current_idx = n; + + // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + ImGui::EndListBox(); + } + + // Custom size: use all width, 5 items tall + ImGui::Text("Full-width:"); + if (ImGui::BeginListBox("##listbox 2", ImVec2(-FLT_MIN, 5 * ImGui::GetTextLineHeightWithSpacing()))) + { + for (int n = 0; n < IM_ARRAYSIZE(items); n++) + { + const bool is_selected = (item_current_idx == n); + if (ImGui::Selectable(items[n], is_selected)) + item_current_idx = n; + + // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) + if (is_selected) + ImGui::SetItemDefaultFocus(); + } + ImGui::EndListBox(); + } + + ImGui::TreePop(); + } + if (ImGui::TreeNode("Selectables")) { // Selectable() has 2 overloads: @@ -1076,15 +1150,36 @@ static void ShowDemoWindowWidgets() } if (ImGui::TreeNode("In columns")) { - ImGui::Columns(3, NULL, false); - static bool selected[16] = {}; - for (int i = 0; i < 16; i++) + static bool selected[10] = {}; + + if (ImGui::BeginTable("split1", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) { - char label[32]; sprintf(label, "Item %d", i); - if (ImGui::Selectable(label, &selected[i])) {} - ImGui::NextColumn(); + for (int i = 0; i < 10; i++) + { + char label[32]; + sprintf(label, "Item %d", i); + ImGui::TableNextColumn(); + ImGui::Selectable(label, &selected[i]); // FIXME-TABLE: Selection overlap + } + ImGui::EndTable(); + } + ImGui::Separator(); + if (ImGui::BeginTable("split2", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) + { + for (int i = 0; i < 10; i++) + { + char label[32]; + sprintf(label, "Item %d", i); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Selectable(label, &selected[i], ImGuiSelectableFlags_SpanAllColumns); + ImGui::TableNextColumn(); + ImGui::Text("Some other contents"); + ImGui::TableNextColumn(); + ImGui::Text("123456"); + } + ImGui::EndTable(); } - ImGui::Columns(1); ImGui::TreePop(); } if (ImGui::TreeNode("Grid")) @@ -1306,9 +1401,149 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - // Plot/Graph widgets are currently fairly limited. - // Consider writing your own plotting widget, or using a third-party one - // (for third-party Plot widgets, see 'Wiki->Useful Widgets' or https://github.com/ocornut/imgui/labels/plot%2Fgraph) + // Tabs + if (ImGui::TreeNode("Tabs")) + { + if (ImGui::TreeNode("Basic")) + { + ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None; + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + if (ImGui::BeginTabItem("Avocado")) + { + ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Broccoli")) + { + ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem("Cucumber")) + { + ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah"); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Advanced & Close Button")) + { + // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). + static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable; + ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", &tab_bar_flags, ImGuiTabBarFlags_Reorderable); + ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", &tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); + ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); + ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); + if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) + tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_; + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); + + // Tab Bar + const char* names[4] = { "Artichoke", "Beetroot", "Celery", "Daikon" }; + static bool opened[4] = { true, true, true, true }; // Persistent user state + for (int n = 0; n < IM_ARRAYSIZE(opened); n++) + { + if (n > 0) { ImGui::SameLine(); } + ImGui::Checkbox(names[n], &opened[n]); + } + + // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): + // the underlying bool will be set to false when the tab is closed. + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + for (int n = 0; n < IM_ARRAYSIZE(opened); n++) + if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n], ImGuiTabItemFlags_None)) + { + ImGui::Text("This is the %s tab!", names[n]); + if (n & 1) + ImGui::Text("I am an odd tab."); + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } + + if (ImGui::TreeNode("TabItemButton & Leading/Trailing flags")) + { + static ImVector active_tabs; + static int next_tab_id = 0; + if (next_tab_id == 0) // Initialize with some default tabs + for (int i = 0; i < 3; i++) + active_tabs.push_back(next_tab_id++); + + // TabItemButton() and Leading/Trailing flags are distinct features which we will demo together. + // (It is possible to submit regular tabs with Leading/Trailing flags, or TabItemButton tabs without Leading/Trailing flags... + // but they tend to make more sense together) + static bool show_leading_button = true; + static bool show_trailing_button = true; + ImGui::Checkbox("Show Leading TabItemButton()", &show_leading_button); + ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button); + + // Expose some other flags which are useful to showcase how they interact with Leading/Trailing tabs + static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown; + ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); + if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) + tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); + + if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) + { + // Demo a Leading TabItemButton(): click the "?" button to open a menu + if (show_leading_button) + if (ImGui::TabItemButton("?", ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_NoTooltip)) + ImGui::OpenPopup("MyHelpMenu"); + if (ImGui::BeginPopup("MyHelpMenu")) + { + ImGui::Selectable("Hello!"); + ImGui::EndPopup(); + } + + // Demo Trailing Tabs: click the "+" button to add a new tab (in your app you may want to use a font icon instead of the "+") + // Note that we submit it before the regular tabs, but because of the ImGuiTabItemFlags_Trailing flag it will always appear at the end. + if (show_trailing_button) + if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) + active_tabs.push_back(next_tab_id++); // Add new tab + + // Submit our regular tabs + for (int n = 0; n < active_tabs.Size; ) + { + bool open = true; + char name[16]; + snprintf(name, IM_ARRAYSIZE(name), "%04d", active_tabs[n]); + if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None)) + { + ImGui::Text("This is the %s tab!", name); + ImGui::EndTabItem(); + } + + if (!open) + active_tabs.erase(active_tabs.Data + n); + else + n++; + } + + ImGui::EndTabBar(); + } + ImGui::Separator(); + ImGui::TreePop(); + } + ImGui::TreePop(); + } + + // Plot/Graph widgets are not very good. + // Consider writing your own, or using a third-party one, see: + // - ImPlot https://github.com/epezent/implot + // - others https://github.com/ocornut/imgui/wiki/Useful-Widgets if (ImGui::TreeNode("Plots Widgets")) { static bool animate = true; @@ -1930,18 +2165,18 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - if (ImGui::TreeNode("Querying Status (Active/Focused/Hovered etc.)")) + if (ImGui::TreeNode("Querying Status (Edited/Active/Focused/Hovered etc.)")) { // Select an item type const char* item_names[] = { "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputFloat", - "InputFloat3", "ColorEdit4", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "ListBox" + "InputFloat3", "ColorEdit4", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox" }; static int item_type = 1; ImGui::Combo("Item Type", &item_type, item_names, IM_ARRAYSIZE(item_names), IM_ARRAYSIZE(item_names)); ImGui::SameLine(); - HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions."); + HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered()."); // Submit selected item item so we can query their status in the code following it. bool ret = false; @@ -1960,7 +2195,8 @@ static void ShowDemoWindowWidgets() if (item_type == 9) { ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) if (item_type == 10){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node if (item_type == 11){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. - if (item_type == 12){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + if (item_type == 12){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } + if (item_type == 13){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. @@ -2120,13 +2356,16 @@ static void ShowDemoWindowLayout() } ImGui::EndMenuBar(); } - ImGui::Columns(2); - for (int i = 0; i < 100; i++) + if (ImGui::BeginTable("split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) { - char buf[32]; - sprintf(buf, "%03d", i); - ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); - ImGui::NextColumn(); + for (int i = 0; i < 100; i++) + { + char buf[32]; + sprintf(buf, "%03d", i); + ImGui::TableNextColumn(); + ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + } + ImGui::EndTable(); } ImGui::EndChild(); ImGui::PopStyleVar(); @@ -2328,186 +2567,48 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } - if (ImGui::TreeNode("Tabs")) + if (ImGui::TreeNode("Groups")) { - if (ImGui::TreeNode("Basic")) + HelpMarker( + "BeginGroup() basically locks the horizontal position for new line. " + "EndGroup() bundles the whole group so that you can use \"item\" functions such as " + "IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group."); + ImGui::BeginGroup(); { - ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None; - if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) - { - if (ImGui::BeginTabItem("Avocado")) - { - ImGui::Text("This is the Avocado tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem("Broccoli")) - { - ImGui::Text("This is the Broccoli tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem("Cucumber")) - { - ImGui::Text("This is the Cucumber tab!\nblah blah blah blah blah"); - ImGui::EndTabItem(); - } - ImGui::EndTabBar(); - } - ImGui::Separator(); - ImGui::TreePop(); + ImGui::BeginGroup(); + ImGui::Button("AAA"); + ImGui::SameLine(); + ImGui::Button("BBB"); + ImGui::SameLine(); + ImGui::BeginGroup(); + ImGui::Button("CCC"); + ImGui::Button("DDD"); + ImGui::EndGroup(); + ImGui::SameLine(); + ImGui::Button("EEE"); + ImGui::EndGroup(); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("First group hovered"); } + // Capture the group size and create widgets using the same size + ImVec2 size = ImGui::GetItemRectSize(); + const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f }; + ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size); - if (ImGui::TreeNode("Advanced & Close Button")) - { - // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). - static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable; - ImGui::CheckboxFlags("ImGuiTabBarFlags_Reorderable", &tab_bar_flags, ImGuiTabBarFlags_Reorderable); - ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", &tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); - ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); - ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); - if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) - tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_; - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) - tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) - tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); - - // Tab Bar - const char* names[4] = { "Artichoke", "Beetroot", "Celery", "Daikon" }; - static bool opened[4] = { true, true, true, true }; // Persistent user state - for (int n = 0; n < IM_ARRAYSIZE(opened); n++) - { - if (n > 0) { ImGui::SameLine(); } - ImGui::Checkbox(names[n], &opened[n]); - } - - // Passing a bool* to BeginTabItem() is similar to passing one to Begin(): - // the underlying bool will be set to false when the tab is closed. - if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) - { - for (int n = 0; n < IM_ARRAYSIZE(opened); n++) - if (opened[n] && ImGui::BeginTabItem(names[n], &opened[n], ImGuiTabItemFlags_None)) - { - ImGui::Text("This is the %s tab!", names[n]); - if (n & 1) - ImGui::Text("I am an odd tab."); - ImGui::EndTabItem(); - } - ImGui::EndTabBar(); - } - ImGui::Separator(); - ImGui::TreePop(); - } - - if (ImGui::TreeNode("TabItemButton & Leading/Trailing flags")) - { - static ImVector active_tabs; - static int next_tab_id = 0; - if (next_tab_id == 0) // Initialize with some default tabs - for (int i = 0; i < 3; i++) - active_tabs.push_back(next_tab_id++); - - // TabItemButton() and Leading/Trailing flags are distinct features which we will demo together. - // (It is possible to submit regular tabs with Leading/Trailing flags, or TabItemButton tabs without Leading/Trailing flags... - // but they tend to make more sense together) - static bool show_leading_button = true; - static bool show_trailing_button = true; - ImGui::Checkbox("Show Leading TabItemButton()", &show_leading_button); - ImGui::Checkbox("Show Trailing TabItemButton()", &show_trailing_button); - - // Expose some other flags which are useful to showcase how they interact with Leading/Trailing tabs - static ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_AutoSelectNewTabs | ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_FittingPolicyResizeDown; - ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) - tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyResizeDown); - if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyScroll", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyScroll)) - tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); - - if (ImGui::BeginTabBar("MyTabBar", tab_bar_flags)) - { - // Demo a Leading TabItemButton(): click the "?" button to open a menu - if (show_leading_button) - if (ImGui::TabItemButton("?", ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_NoTooltip)) - ImGui::OpenPopup("MyHelpMenu"); - if (ImGui::BeginPopup("MyHelpMenu")) - { - ImGui::Selectable("Hello!"); - ImGui::EndPopup(); - } - - // Demo Trailing Tabs: click the "+" button to add a new tab (in your app you may want to use a font icon instead of the "+") - // Note that we submit it before the regular tabs, but because of the ImGuiTabItemFlags_Trailing flag it will always appear at the end. - if (show_trailing_button) - if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) - active_tabs.push_back(next_tab_id++); // Add new tab - - // Submit our regular tabs - for (int n = 0; n < active_tabs.Size; ) - { - bool open = true; - char name[16]; - snprintf(name, IM_ARRAYSIZE(name), "%04d", active_tabs[n]); - if (ImGui::BeginTabItem(name, &open, ImGuiTabItemFlags_None)) - { - ImGui::Text("This is the %s tab!", name); - ImGui::EndTabItem(); - } - - if (!open) - active_tabs.erase(active_tabs.Data + n); - else - n++; - } - - ImGui::EndTabBar(); - } - ImGui::Separator(); - ImGui::TreePop(); - } - ImGui::TreePop(); - } - - if (ImGui::TreeNode("Groups")) - { - HelpMarker( - "BeginGroup() basically locks the horizontal position for new line. " - "EndGroup() bundles the whole group so that you can use \"item\" functions such as " - "IsItemHovered()/IsItemActive() or SameLine() etc. on the whole group."); - ImGui::BeginGroup(); - { - ImGui::BeginGroup(); - ImGui::Button("AAA"); - ImGui::SameLine(); - ImGui::Button("BBB"); - ImGui::SameLine(); - ImGui::BeginGroup(); - ImGui::Button("CCC"); - ImGui::Button("DDD"); - ImGui::EndGroup(); - ImGui::SameLine(); - ImGui::Button("EEE"); - ImGui::EndGroup(); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("First group hovered"); - } - // Capture the group size and create widgets using the same size - ImVec2 size = ImGui::GetItemRectSize(); - const float values[5] = { 0.5f, 0.20f, 0.80f, 0.60f, 0.25f }; - ImGui::PlotHistogram("##values", values, IM_ARRAYSIZE(values), 0, NULL, 0.0f, 1.0f, size); - - ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); - ImGui::SameLine(); - ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); - ImGui::EndGroup(); - ImGui::SameLine(); - - ImGui::Button("LEVERAGE\nBUZZWORD", size); - ImGui::SameLine(); - - if (ImGui::ListBoxHeader("List", size)) + ImGui::Button("ACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); + ImGui::SameLine(); + ImGui::Button("REACTION", ImVec2((size.x - ImGui::GetStyle().ItemSpacing.x) * 0.5f, size.y)); + ImGui::EndGroup(); + ImGui::SameLine(); + + ImGui::Button("LEVERAGE\nBUZZWORD", size); + ImGui::SameLine(); + + if (ImGui::BeginListBox("List", size)) { ImGui::Selectable("Selected", true); ImGui::Selectable("Not Selected", false); - ImGui::ListBoxFooter(); + ImGui::EndListBox(); } ImGui::TreePop(); @@ -2872,6 +2973,17 @@ static void ShowDemoWindowLayout() } if (show_columns) { + ImGui::Text("Tables:"); + if (ImGui::BeginTable("table", 4, ImGuiTableFlags_Borders)) + { + for (int n = 0; n < 4; n++) + { + ImGui::TableNextColumn(); + ImGui::Text("Width %.2f", ImGui::GetContentRegionAvail().x); + } + ImGui::EndTable(); + } + ImGui::Text("Columns:"); ImGui::Columns(4); for (int n = 0; n < 4; n++) { @@ -3052,178 +3164,2021 @@ static void ShowDemoWindowPopups() } ImGui::EndPopup(); } - ImGui::EndPopup(); + ImGui::EndPopup(); + } + + // Call the more complete ShowExampleMenuFile which we use in various places of this demo + if (ImGui::Button("File Menu..")) + ImGui::OpenPopup("my_file_popup"); + if (ImGui::BeginPopup("my_file_popup")) + { + ShowExampleMenuFile(); + ImGui::EndPopup(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Context menus")) + { + // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing: + // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) + // OpenPopup(id); + // return BeginPopup(id); + // For more advanced uses you may want to replicate and customize this code. + // See details in BeginPopupContextItem(). + static float value = 0.5f; + ImGui::Text("Value = %.3f (<-- right-click here)", value); + if (ImGui::BeginPopupContextItem("item context menu")) + { + if (ImGui::Selectable("Set to zero")) value = 0.0f; + if (ImGui::Selectable("Set to PI")) value = 3.1415f; + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); + ImGui::EndPopup(); + } + + // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the + // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) + // will toggle the visibility of the popup above. + ImGui::Text("(You can also right-click me to open the same popup as above.)"); + ImGui::OpenPopupOnItemClick("item context menu", 1); + + // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). + // BeginPopupContextItem() will use the last item ID as the popup ID. + // In addition here, we want to include your editable label inside the button label. + // We use the ### operator to override the ID (read FAQ about ID for details) + static char name[32] = "Label1"; + char buf[64]; + sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label + ImGui::Button(buf); + if (ImGui::BeginPopupContextItem()) + { + ImGui::Text("Edit name:"); + ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + ImGui::SameLine(); ImGui::Text("(<-- right-click here)"); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Modals")) + { + ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside."); + + if (ImGui::Button("Delete..")) + ImGui::OpenPopup("Delete?"); + + // Always center this window when appearing + ImVec2 center = ImGui::GetMainViewport()->GetCenter(); + ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + + if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) + { + ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); + ImGui::Separator(); + + //static int unused_i = 0; + //ImGui::Combo("Combo", &unused_i, "Delete\0Delete harder\0"); + + static bool dont_ask_me_next_time = false; + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); + ImGui::Checkbox("Don't ask me next time", &dont_ask_me_next_time); + ImGui::PopStyleVar(); + + if (ImGui::Button("OK", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } + ImGui::SetItemDefaultFocus(); + ImGui::SameLine(); + if (ImGui::Button("Cancel", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } + ImGui::EndPopup(); + } + + if (ImGui::Button("Stacked modals..")) + ImGui::OpenPopup("Stacked 1"); + if (ImGui::BeginPopupModal("Stacked 1", NULL, ImGuiWindowFlags_MenuBar)) + { + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Some menu item")) {} + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + ImGui::Text("Hello from Stacked The First\nUsing style.Colors[ImGuiCol_ModalWindowDimBg] behind it."); + + // Testing behavior of widgets stacking their own regular popups over the modal. + static int item = 1; + static float color[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; + ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); + ImGui::ColorEdit4("color", color); + + if (ImGui::Button("Add another modal..")) + ImGui::OpenPopup("Stacked 2"); + + // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which + // will close the popup. Note that the visibility state of popups is owned by imgui, so the input value + // of the bool actually doesn't matter here. + bool unused_open = true; + if (ImGui::BeginPopupModal("Stacked 2", &unused_open)) + { + ImGui::Text("Hello from Stacked The Second!"); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Menus inside a regular window")) + { + ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); + ImGui::Separator(); + + // Note: As a quirk in this very specific example, we want to differentiate the parent of this menu from the + // parent of the various popup menus above. To do so we are encloding the items in a PushID()/PopID() block + // to make them two different menusets. If we don't, opening any popup above and hovering our menu here would + // open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, + // which is the desired behavior for regular menus. + ImGui::PushID("foo"); + ImGui::MenuItem("Menu item", "CTRL+M"); + if (ImGui::BeginMenu("Menu inside a regular window")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + ImGui::PopID(); + ImGui::Separator(); + ImGui::TreePop(); + } +} + +// Dummy data structure that we use for the Table demo. +// (pre-C++11 doesn't allow us to instantiate ImVector template if this structure if defined inside the demo function) +namespace +{ +// We are passing our own identifier to TableSetupColumn() to facilitate identifying columns in the sorting code. +// This identifier will be passed down into ImGuiTableSortSpec::ColumnUserID. +// But it is possible to omit the user id parameter of TableSetupColumn() and just use the column index instead! (ImGuiTableSortSpec::ColumnIndex) +// If you don't use sorting, you will generally never care about giving column an ID! +enum MyItemColumnID +{ + MyItemColumnID_ID, + MyItemColumnID_Name, + MyItemColumnID_Action, + MyItemColumnID_Quantity, + MyItemColumnID_Description +}; + +struct MyItem +{ + int ID; + const char* Name; + int Quantity; + + // We have a problem which is affecting _only this demo_ and should not affect your code: + // As we don't rely on std:: or other third-party library to compile dear imgui, we only have reliable access to qsort(), + // however qsort doesn't allow passing user data to comparing function. + // As a workaround, we are storing the sort specs in a static/global for the comparing function to access. + // In your own use case you would probably pass the sort specs to your sorting/comparing functions directly and not use a global. + // We could technically call ImGui::TableGetSortSpecs() in CompareWithSortSpecs(), but considering that this function is called + // very often by the sorting algorithm it would be a little wasteful. + static const ImGuiTableSortSpecs* s_current_sort_specs; + + // Compare function to be used by qsort() + static int IMGUI_CDECL CompareWithSortSpecs(const void* lhs, const void* rhs) + { + const MyItem* a = (const MyItem*)lhs; + const MyItem* b = (const MyItem*)rhs; + for (int n = 0; n < s_current_sort_specs->SpecsCount; n++) + { + // Here we identify columns using the ColumnUserID value that we ourselves passed to TableSetupColumn() + // We could also choose to identify columns based on their index (sort_spec->ColumnIndex), which is simpler! + const ImGuiTableColumnSortSpecs* sort_spec = &s_current_sort_specs->Specs[n]; + int delta = 0; + switch (sort_spec->ColumnUserID) + { + case MyItemColumnID_ID: delta = (a->ID - b->ID); break; + case MyItemColumnID_Name: delta = (strcmp(a->Name, b->Name)); break; + case MyItemColumnID_Quantity: delta = (a->Quantity - b->Quantity); break; + case MyItemColumnID_Description: delta = (strcmp(a->Name, b->Name)); break; + default: IM_ASSERT(0); break; + } + if (delta > 0) + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1; + if (delta < 0) + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1 : +1; + } + + // qsort() is instable so always return a way to differenciate items. + // Your own compare function may want to avoid fallback on implicit sort specs e.g. a Name compare if it wasn't already part of the sort specs. + return (a->ID - b->ID); + } +}; +const ImGuiTableSortSpecs* MyItem::s_current_sort_specs = NULL; +} + +// Make the UI compact because there are so many fields +static void PushStyleCompact() +{ + ImGuiStyle& style = ImGui::GetStyle(); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(style.FramePadding.x, (float)(int)(style.FramePadding.y * 0.60f))); + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x, (float)(int)(style.ItemSpacing.y * 0.60f))); +} + +static void PopStyleCompact() +{ + ImGui::PopStyleVar(2); +} + +// Show a combo box with a choice of sizing policies +static void EditTableSizingFlags(ImGuiTableFlags* p_flags) +{ + struct EnumDesc { ImGuiTableFlags Value; const char* Name; const char* Tooltip; }; + static const EnumDesc policies[] = + { + { ImGuiTableFlags_None, "Default", "Use default sizing policy:\n- ImGuiTableFlags_SizingFixedFit if ScrollX is on or if host window has ImGuiWindowFlags_AlwaysAutoResize.\n- ImGuiTableFlags_SizingStretchSame otherwise." }, + { ImGuiTableFlags_SizingFixedFit, "ImGuiTableFlags_SizingFixedFit", "Columns default to _WidthFixed (if resizable) or _WidthAuto (if not resizable), matching contents width." }, + { ImGuiTableFlags_SizingFixedSame, "ImGuiTableFlags_SizingFixedSame", "Columns are all the same width, matching the maximum contents width.\nImplicitly disable ImGuiTableFlags_Resizable and enable ImGuiTableFlags_NoKeepColumnsVisible." }, + { ImGuiTableFlags_SizingStretchProp, "ImGuiTableFlags_SizingStretchProp", "Columns default to _WidthStretch with weights proportional to their widths." }, + { ImGuiTableFlags_SizingStretchSame, "ImGuiTableFlags_SizingStretchSame", "Columns default to _WidthStretch with same weights." } + }; + int idx; + for (idx = 0; idx < IM_ARRAYSIZE(policies); idx++) + if (policies[idx].Value == (*p_flags & ImGuiTableFlags_SizingMask_)) + break; + const char* preview_text = (idx < IM_ARRAYSIZE(policies)) ? policies[idx].Name + (idx > 0 ? strlen("ImGuiTableFlags") : 0) : ""; + if (ImGui::BeginCombo("Sizing Policy", preview_text)) + { + for (int n = 0; n < IM_ARRAYSIZE(policies); n++) + if (ImGui::Selectable(policies[n].Name, idx == n)) + *p_flags = (*p_flags & ~ImGuiTableFlags_SizingMask_) | policies[n].Value; + ImGui::EndCombo(); + } + ImGui::SameLine(); + ImGui::TextDisabled("(?)"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::PushTextWrapPos(ImGui::GetFontSize() * 50.0f); + for (int m = 0; m < IM_ARRAYSIZE(policies); m++) + { + ImGui::Separator(); + ImGui::Text("%s:", policies[m].Name); + ImGui::Separator(); + ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetStyle().IndentSpacing * 0.5f); + ImGui::TextUnformatted(policies[m].Tooltip); + } + ImGui::PopTextWrapPos(); + ImGui::EndTooltip(); + } +} + +static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags) +{ + ImGui::CheckboxFlags("_DefaultHide", p_flags, ImGuiTableColumnFlags_DefaultHide); + ImGui::CheckboxFlags("_DefaultSort", p_flags, ImGuiTableColumnFlags_DefaultSort); + if (ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch)) + *p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthStretch); + if (ImGui::CheckboxFlags("_WidthFixed", p_flags, ImGuiTableColumnFlags_WidthFixed)) + *p_flags &= ~(ImGuiTableColumnFlags_WidthMask_ ^ ImGuiTableColumnFlags_WidthFixed); + ImGui::CheckboxFlags("_NoResize", p_flags, ImGuiTableColumnFlags_NoResize); + ImGui::CheckboxFlags("_NoReorder", p_flags, ImGuiTableColumnFlags_NoReorder); + ImGui::CheckboxFlags("_NoHide", p_flags, ImGuiTableColumnFlags_NoHide); + ImGui::CheckboxFlags("_NoClip", p_flags, ImGuiTableColumnFlags_NoClip); + ImGui::CheckboxFlags("_NoSort", p_flags, ImGuiTableColumnFlags_NoSort); + ImGui::CheckboxFlags("_NoSortAscending", p_flags, ImGuiTableColumnFlags_NoSortAscending); + ImGui::CheckboxFlags("_NoSortDescending", p_flags, ImGuiTableColumnFlags_NoSortDescending); + ImGui::CheckboxFlags("_NoHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth); + ImGui::CheckboxFlags("_PreferSortAscending", p_flags, ImGuiTableColumnFlags_PreferSortAscending); + ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending); + ImGui::CheckboxFlags("_IndentEnable", p_flags, ImGuiTableColumnFlags_IndentEnable); ImGui::SameLine(); HelpMarker("Default for column 0"); + ImGui::CheckboxFlags("_IndentDisable", p_flags, ImGuiTableColumnFlags_IndentDisable); ImGui::SameLine(); HelpMarker("Default for column >0"); +} + +static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags) +{ + ImGui::CheckboxFlags("_IsEnabled", &flags, ImGuiTableColumnFlags_IsEnabled); + ImGui::CheckboxFlags("_IsVisible", &flags, ImGuiTableColumnFlags_IsVisible); + ImGui::CheckboxFlags("_IsSorted", &flags, ImGuiTableColumnFlags_IsSorted); + ImGui::CheckboxFlags("_IsHovered", &flags, ImGuiTableColumnFlags_IsHovered); +} + +static void ShowDemoWindowTables() +{ + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (!ImGui::CollapsingHeader("Tables & Columns")) + return; + + // Using those as a base value to create width/height that are factor of the size of our font + const float TEXT_BASE_WIDTH = ImGui::CalcTextSize("A").x; + const float TEXT_BASE_HEIGHT = ImGui::GetTextLineHeightWithSpacing(); + + ImGui::PushID("Tables"); + + int open_action = -1; + if (ImGui::Button("Open all")) + open_action = 1; + ImGui::SameLine(); + if (ImGui::Button("Close all")) + open_action = 0; + ImGui::SameLine(); + + // Options + static bool disable_indent = false; + ImGui::Checkbox("Disable tree indentation", &disable_indent); + ImGui::SameLine(); + HelpMarker("Disable the indenting of tree nodes so demo tables can use the full window width."); + ImGui::Separator(); + if (disable_indent) + ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, 0.0f); + + // About Styling of tables + // Most settings are configured on a per-table basis via the flags passed to BeginTable() and TableSetupColumns APIs. + // There are however a few settings that a shared and part of the ImGuiStyle structure: + // style.CellPadding // Padding within each cell + // style.Colors[ImGuiCol_TableHeaderBg] // Table header background + // style.Colors[ImGuiCol_TableBorderStrong] // Table outer and header borders + // style.Colors[ImGuiCol_TableBorderLight] // Table inner borders + // style.Colors[ImGuiCol_TableRowBg] // Table row background when ImGuiTableFlags_RowBg is enabled (even rows) + // style.Colors[ImGuiCol_TableRowBgAlt] // Table row background when ImGuiTableFlags_RowBg is enabled (odds rows) + + // Demos + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Basic")) + { + // Here we will showcase three different ways to output a table. + // They are very simple variations of a same thing! + + // [Method 1] Using TableNextRow() to create a new row, and TableSetColumnIndex() to select the column. + // In many situations, this is the most flexible and easy to use pattern. + HelpMarker("Using TableNextRow() + calling TableSetColumnIndex() _before_ each cell, in a loop."); + if (ImGui::BeginTable("table1", 3)) + { + for (int row = 0; row < 4; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Row %d Column %d", row, column); + } + } + ImGui::EndTable(); + } + + // [Method 2] Using TableNextColumn() called multiple times, instead of using a for loop + TableSetColumnIndex(). + // This is generally more convenient when you have code manually submitting the contents of each columns. + HelpMarker("Using TableNextRow() + calling TableNextColumn() _before_ each cell, manually."); + if (ImGui::BeginTable("table2", 3)) + { + for (int row = 0; row < 4; row++) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("Row %d", row); + ImGui::TableNextColumn(); + ImGui::Text("Some contents"); + ImGui::TableNextColumn(); + ImGui::Text("123.456"); + } + ImGui::EndTable(); + } + + // [Method 3] We call TableNextColumn() _before_ each cell. We never call TableNextRow(), + // as TableNextColumn() will automatically wrap around and create new roes as needed. + // This is generally more convenient when your cells all contains the same type of data. + HelpMarker( + "Only using TableNextColumn(), which tends to be convenient for tables where every cells contains the same type of contents.\n" + "This is also more similar to the old NextColumn() function of the Columns API, and provided to facilitate the Columns->Tables API transition."); + if (ImGui::BeginTable("table3", 3)) + { + for (int item = 0; item < 14; item++) + { + ImGui::TableNextColumn(); + ImGui::Text("Item %d", item); + } + ImGui::EndTable(); + } + + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Borders, background")) + { + // Expose a few Borders related flags interactively + enum ContentsType { CT_Text, CT_FillButton }; + static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg; + static bool display_headers = false; + static int contents_type = CT_Text; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg); + ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags, ImGuiTableFlags_Borders); + ImGui::SameLine(); HelpMarker("ImGuiTableFlags_Borders\n = ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterV\n | ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterH"); + ImGui::Indent(); + + ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH); + ImGui::Indent(); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH); + ImGui::Unindent(); + + ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV); + ImGui::Indent(); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags, ImGuiTableFlags_BordersOuterV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV); + ImGui::Unindent(); + + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuter", &flags, ImGuiTableFlags_BordersOuter); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInner", &flags, ImGuiTableFlags_BordersInner); + ImGui::Unindent(); + + ImGui::AlignTextToFramePadding(); ImGui::Text("Cell contents:"); + ImGui::SameLine(); ImGui::RadioButton("Text", &contents_type, CT_Text); + ImGui::SameLine(); ImGui::RadioButton("FillButton", &contents_type, CT_FillButton); + ImGui::Checkbox("Display headers", &display_headers); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appears in Headers"); + PopStyleCompact(); + + if (ImGui::BeginTable("table1", 3, flags)) + { + // Display headers so we can inspect their interaction with borders. + // (Headers are not the main purpose of this section of the demo, so we are not elaborating on them too much. See other sections for details) + if (display_headers) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableHeadersRow(); + } + + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + char buf[32]; + sprintf(buf, "Hello %d,%d", column, row); + if (contents_type == CT_Text) + ImGui::TextUnformatted(buf); + else if (contents_type) + ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Resizable, stretch")) + { + // By default, if we don't enable ScrollX the sizing policy for each columns is "Stretch" + // Each columns maintain a sizing weight, and they will occupy all available width. + static ImGuiTableFlags flags = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV); + ImGui::SameLine(); HelpMarker("Using the _Resizable flag automatically enables the _BordersInnerV flag as well, this is why the resize borders are still showing when unchecking this."); + PopStyleCompact(); + + if (ImGui::BeginTable("table1", 3, flags)) + { + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Hello %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Resizable, fixed")) + { + // Here we use ImGuiTableFlags_SizingFixedFit (even though _ScrollX is not set) + // So columns will adopt the "Fixed" policy and will maintain a fixed width regardless of the whole available width (unless table is small) + // If there is not enough available width to fit all columns, they will however be resized down. + // FIXME-TABLE: Providing a stretch-on-init would make sense especially for tables which don't have saved settings + HelpMarker( + "Using _Resizable + _SizingFixedFit flags.\n" + "Fixed-width columns generally makes more sense if you want to use horizontal scrolling.\n\n" + "Double-click a column border to auto-fit the column to its contents."); + PushStyleCompact(); + static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody; + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX); + PopStyleCompact(); + + if (ImGui::BeginTable("table1", 3, flags)) + { + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Hello %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Resizable, mixed")) + { + HelpMarker( + "Using TableSetupColumn() to alter resizing policy on a per-column basis.\n\n" + "When combining Fixed and Stretch columns, generally you only want one, maybe two trailing columns to use _WidthStretch."); + static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; + + if (ImGui::BeginTable("table1", 3, flags)) + { + ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableHeadersRow(); + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("%s %d,%d", (column == 2) ? "Stretch" : "Fixed", column, row); + } + } + ImGui::EndTable(); + } + if (ImGui::BeginTable("table2", 6, flags)) + { + ImGui::TableSetupColumn("AAA", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("BBB", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("CCC", ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_DefaultHide); + ImGui::TableSetupColumn("DDD", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("EEE", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("FFF", ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_DefaultHide); + ImGui::TableHeadersRow(); + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 6; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("%s %d,%d", (column >= 3) ? "Stretch" : "Fixed", column, row); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Reorderable, hideable, with headers")) + { + HelpMarker( + "Click and drag column headers to reorder columns.\n\n" + "Right-click on a header to open a context menu."); + static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_Reorderable", &flags, ImGuiTableFlags_Reorderable); + ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers)"); + PopStyleCompact(); + + if (ImGui::BeginTable("table1", 3, flags)) + { + // Submit columns name with TableSetupColumn() and call TableHeadersRow() to create a row with a header in each column. + // (Later we will show how TableSetupColumn() has other uses, optional flags, sizing weight etc.) + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableHeadersRow(); + for (int row = 0; row < 6; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Hello %d,%d", column, row); + } + } + ImGui::EndTable(); + } + + // Use outer_size.x == 0.0f instead of default to make the table as tight as possible (only valid when no scrolling and no stretch column) + if (ImGui::BeginTable("table2", 3, flags | ImGuiTableFlags_SizingFixedFit, ImVec2(0.0f, 0.0f))) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableHeadersRow(); + for (int row = 0; row < 6; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Fixed %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Padding")) + { + // First example: showcase use of padding flags and effect of BorderOuterV/BorderInnerV on X padding. + // We don't expose BorderOuterH/BorderInnerH here because they have no effect on X padding. + HelpMarker( + "We often want outer padding activated when any using features which makes the edges of a column visible:\n" + "e.g.:\n" + "- BorderOuterV\n" + "- any form of row selection\n" + "Because of this, activating BorderOuterV sets the default to PadOuterX. Using PadOuterX or NoPadOuterX you can override the default.\n\n" + "Actual padding values are using style.CellPadding.\n\n" + "In this demo we don't show horizontal borders to emphasis how they don't affect default horizontal padding."); + + static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_PadOuterX", &flags1, ImGuiTableFlags_PadOuterX); + ImGui::SameLine(); HelpMarker("Enable outer-most padding (default if ImGuiTableFlags_BordersOuterV is set)"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoPadOuterX", &flags1, ImGuiTableFlags_NoPadOuterX); + ImGui::SameLine(); HelpMarker("Disable outer-most padding (default if ImGuiTableFlags_BordersOuterV is not set)"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoPadInnerX", &flags1, ImGuiTableFlags_NoPadInnerX); + ImGui::SameLine(); HelpMarker("Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off)"); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags1, ImGuiTableFlags_BordersOuterV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags1, ImGuiTableFlags_BordersInnerV); + static bool show_headers = false; + ImGui::Checkbox("show_headers", &show_headers); + PopStyleCompact(); + + if (ImGui::BeginTable("table_padding", 3, flags1)) + { + if (show_headers) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableHeadersRow(); + } + + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + if (row == 0) + { + ImGui::Text("Avail %.2f", ImGui::GetContentRegionAvail().x); + } + else + { + char buf[32]; + sprintf(buf, "Hello %d,%d", column, row); + ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + } + //if (ImGui::TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) + // ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(0, 100, 0, 255)); + } + } + ImGui::EndTable(); + } + + // Second example: set style.CellPadding to (0.0) or a custom value. + // FIXME-TABLE: Vertical border effectively not displayed the same way as horizontal one... + HelpMarker("Setting style.CellPadding to (0,0) or a custom value."); + static ImGuiTableFlags flags2 = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg; + static ImVec2 cell_padding(0.0f, 0.0f); + static bool show_widget_frame_bg = true; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags2, ImGuiTableFlags_Borders); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags2, ImGuiTableFlags_BordersH); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags2, ImGuiTableFlags_BordersV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInner", &flags2, ImGuiTableFlags_BordersInner); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuter", &flags2, ImGuiTableFlags_BordersOuter); + ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags2, ImGuiTableFlags_RowBg); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags2, ImGuiTableFlags_Resizable); + ImGui::Checkbox("show_widget_frame_bg", &show_widget_frame_bg); + ImGui::SliderFloat2("CellPadding", &cell_padding.x, 0.0f, 10.0f, "%.0f"); + PopStyleCompact(); + + ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, cell_padding); + if (ImGui::BeginTable("table_padding_2", 3, flags2)) + { + static char text_bufs[3 * 5][16]; // Mini text storage for 3x5 cells + static bool init = true; + if (!show_widget_frame_bg) + ImGui::PushStyleColor(ImGuiCol_FrameBg, 0); + for (int cell = 0; cell < 3 * 5; cell++) + { + ImGui::TableNextColumn(); + if (init) + strcpy(text_bufs[cell], "edit me"); + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::PushID(cell); + ImGui::InputText("##cell", text_bufs[cell], IM_ARRAYSIZE(text_bufs[cell])); + ImGui::PopID(); + } + if (!show_widget_frame_bg) + ImGui::PopStyleColor(); + init = false; + ImGui::EndTable(); + } + ImGui::PopStyleVar(); + + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Sizing policies")) + { + static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags1, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags1, ImGuiTableFlags_NoHostExtendX); + PopStyleCompact(); + + static ImGuiTableFlags sizing_policy_flags[4] = { ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_SizingFixedSame, ImGuiTableFlags_SizingStretchProp, ImGuiTableFlags_SizingStretchSame }; + for (int table_n = 0; table_n < 4; table_n++) + { + ImGui::PushID(table_n); + ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 30); + EditTableSizingFlags(&sizing_policy_flags[table_n]); + + // To make it easier to understand the different sizing policy, + // For each policy: we display one table where the columns have equal contents width, and one where the columns have different contents width. + if (ImGui::BeginTable("table1", 3, sizing_policy_flags[table_n] | flags1)) + { + for (int row = 0; row < 3; row++) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); ImGui::Text("Oh dear"); + ImGui::TableNextColumn(); ImGui::Text("Oh dear"); + ImGui::TableNextColumn(); ImGui::Text("Oh dear"); + } + ImGui::EndTable(); + } + if (ImGui::BeginTable("table2", 3, sizing_policy_flags[table_n] | flags1)) + { + for (int row = 0; row < 3; row++) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); ImGui::Text("AAAA"); + ImGui::TableNextColumn(); ImGui::Text("BBBBBBBB"); + ImGui::TableNextColumn(); ImGui::Text("CCCCCCCCCCCC"); + } + ImGui::EndTable(); + } + ImGui::PopID(); + } + + ImGui::Spacing(); + ImGui::TextUnformatted("Advanced"); + ImGui::SameLine(); + HelpMarker("This section allows you to interact and see the effect of various sizing policies depending on whether Scroll is enabled and the contents of your columns."); + + enum ContentsType { CT_ShowWidth, CT_ShortText, CT_LongText, CT_Button, CT_FillButton, CT_InputText }; + static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable; + static int contents_type = CT_ShowWidth; + static int column_count = 3; + + PushStyleCompact(); + ImGui::PushID("Advanced"); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30); + EditTableSizingFlags(&flags); + ImGui::Combo("Contents", &contents_type, "Show width\0Short Text\0Long Text\0Button\0Fill Button\0InputText\0"); + if (contents_type == CT_FillButton) + { + ImGui::SameLine(); + HelpMarker("Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop where contents width can feed into auto-column width can feed into contents width."); + } + ImGui::DragInt("Columns", &column_count, 0.1f, 1, 64, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_PreciseWidths", &flags, ImGuiTableFlags_PreciseWidths); + ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth."); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); + ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip); + ImGui::PopItemWidth(); + ImGui::PopID(); + PopStyleCompact(); + + if (ImGui::BeginTable("table2", column_count, flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 7))) + { + for (int cell = 0; cell < 10 * column_count; cell++) + { + ImGui::TableNextColumn(); + int column = ImGui::TableGetColumnIndex(); + int row = ImGui::TableGetRowIndex(); + + ImGui::PushID(cell); + char label[32]; + static char text_buf[32] = ""; + sprintf(label, "Hello %d,%d", column, row); + switch (contents_type) + { + case CT_ShortText: ImGui::TextUnformatted(label); break; + case CT_LongText: ImGui::Text("Some %s text %d,%d\nOver two lines..", column == 0 ? "long" : "longeeer", column, row); break; + case CT_ShowWidth: ImGui::Text("W: %.1f", ImGui::GetContentRegionAvail().x); break; + case CT_Button: ImGui::Button(label); break; + case CT_FillButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break; + case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break; + } + ImGui::PopID(); + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Vertical scrolling, with clipping")) + { + HelpMarker("Here we activate ScrollY, which will create a child window container to allow hosting scrollable contents.\n\nWe also demonstrate using ImGuiListClipper to virtualize the submission of many items."); + static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); + PopStyleCompact(); + + // When using ScrollX or ScrollY we need to specify a size for our table container! + // Otherwise by default the table will fit all available space, like a BeginChild() call. + ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 8); + if (ImGui::BeginTable("table_scrolly", 3, flags, outer_size)) + { + ImGui::TableSetupScrollFreeze(0, 1); // Make top row always visible + ImGui::TableSetupColumn("One", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Two", ImGuiTableColumnFlags_None); + ImGui::TableSetupColumn("Three", ImGuiTableColumnFlags_None); + ImGui::TableHeadersRow(); + + // Demonstrate using clipper for large vertical lists + ImGuiListClipper clipper; + clipper.Begin(1000); + while (clipper.Step()) + { + for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Hello %d,%d", column, row); + } + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Horizontal scrolling")) + { + HelpMarker( + "When ScrollX is enabled, the default sizing policy becomes ImGuiTableFlags_SizingFixedFit, " + "as automatically stretching columns doesn't make much sense with horizontal scrolling.\n\n" + "Also note that as of the current version, you will almost always want to enable ScrollY along with ScrollX," + "because the container window won't automatically extend vertically to fix contents (this may be improved in future versions)."); + static ImGuiTableFlags flags = ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; + static int freeze_cols = 1; + static int freeze_rows = 1; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); + ImGui::SetNextItemWidth(ImGui::GetFrameHeight()); + ImGui::DragInt("freeze_cols", &freeze_cols, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput); + ImGui::SetNextItemWidth(ImGui::GetFrameHeight()); + ImGui::DragInt("freeze_rows", &freeze_rows, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput); + PopStyleCompact(); + + // When using ScrollX or ScrollY we need to specify a size for our table container! + // Otherwise by default the table will fit all available space, like a BeginChild() call. + ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 8); + if (ImGui::BeginTable("table_scrollx", 7, flags, outer_size)) + { + ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows); + ImGui::TableSetupColumn("Line #", ImGuiTableColumnFlags_NoHide); // Make the first column not hideable to match our use of TableSetupScrollFreeze() + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableSetupColumn("Four"); + ImGui::TableSetupColumn("Five"); + ImGui::TableSetupColumn("Six"); + ImGui::TableHeadersRow(); + for (int row = 0; row < 20; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 7; column++) + { + // Both TableNextColumn() and TableSetColumnIndex() return true when a column is visible or performing width measurement. + // Because here we know that: + // - A) all our columns are contributing the same to row height + // - B) column 0 is always visible, + // We only always submit this one column and can skip others. + // More advanced per-column clipping behaviors may benefit from polling the status flags via TableGetColumnFlags(). + if (!ImGui::TableSetColumnIndex(column) && column > 0) + continue; + if (column == 0) + ImGui::Text("Line %d", row); + else + ImGui::Text("Hello world %d,%d", column, row); + } + } + ImGui::EndTable(); + } + + ImGui::Spacing(); + ImGui::TextUnformatted("Stretch + ScrollX"); + ImGui::SameLine(); + HelpMarker( + "Showcase using Stretch columns + ScrollX together: " + "this is rather unusual and only makes sense when specifying an 'inner_width' for the table!\n" + "Without an explicit value, inner_width is == outer_size.x and therefore using Stretch columns + ScrollX together doesn't make sense."); + static ImGuiTableFlags flags2 = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody; + static float inner_width = 1000.0f; + PushStyleCompact(); + ImGui::PushID("flags3"); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags2, ImGuiTableFlags_ScrollX); + ImGui::DragFloat("inner_width", &inner_width, 1.0f, 0.0f, FLT_MAX, "%.1f"); + ImGui::PopItemWidth(); + ImGui::PopID(); + PopStyleCompact(); + if (ImGui::BeginTable("table2", 7, flags2, outer_size, inner_width)) + { + for (int cell = 0; cell < 20 * 7; cell++) + { + ImGui::TableNextColumn(); + ImGui::Text("Hello world %d,%d", ImGui::TableGetColumnIndex(), ImGui::TableGetRowIndex()); + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Columns flags")) + { + // Create a first table just to show all the options/flags we want to make visible in our example! + const int column_count = 3; + const char* column_names[column_count] = { "One", "Two", "Three" }; + static ImGuiTableColumnFlags column_flags[column_count] = { ImGuiTableColumnFlags_DefaultSort, ImGuiTableColumnFlags_None, ImGuiTableColumnFlags_DefaultHide }; + static ImGuiTableColumnFlags column_flags_out[column_count] = { 0, 0, 0 }; // Output from TableGetColumnFlags() + + if (ImGui::BeginTable("table_columns_flags_checkboxes", column_count, ImGuiTableFlags_None)) + { + PushStyleCompact(); + for (int column = 0; column < column_count; column++) + { + ImGui::TableNextColumn(); + ImGui::PushID(column); + ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation + ImGui::Text("'%s'", column_names[column]); + ImGui::Spacing(); + ImGui::Text("Input flags:"); + EditTableColumnsFlags(&column_flags[column]); + ImGui::Spacing(); + ImGui::Text("Output flags:"); + ShowTableColumnsStatusFlags(column_flags_out[column]); + ImGui::PopID(); + } + PopStyleCompact(); + ImGui::EndTable(); + } + + // Create the real table we care about for the example! + // We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above, otherwise in + // a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible + resizing the parent window down) + const ImGuiTableFlags flags + = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY + | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV + | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable; + ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 9); + if (ImGui::BeginTable("table_columns_flags", column_count, flags, outer_size)) + { + for (int column = 0; column < column_count; column++) + ImGui::TableSetupColumn(column_names[column], column_flags[column]); + ImGui::TableHeadersRow(); + for (int column = 0; column < column_count; column++) + column_flags_out[column] = ImGui::TableGetColumnFlags(column); + float indent_step = (float)((int)TEXT_BASE_WIDTH / 2); + for (int row = 0; row < 8; row++) + { + ImGui::Indent(indent_step); // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags. + ImGui::TableNextRow(); + for (int column = 0; column < column_count; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("%s %s", (column == 0) ? "Indented" : "Hello", ImGui::TableGetColumnName(column)); + } + } + ImGui::Unindent(indent_step * 8.0f); + + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Columns widths")) + { + HelpMarker("Using TableSetupColumn() to setup default width."); + + static ImGuiTableFlags flags1 = ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBodyUntilResize; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags1, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags1, ImGuiTableFlags_NoBordersInBodyUntilResize); + PopStyleCompact(); + if (ImGui::BeginTable("table1", 3, flags1)) + { + // We could also set ImGuiTableFlags_SizingFixedFit on the table and all columns will default to ImGuiTableColumnFlags_WidthFixed. + ImGui::TableSetupColumn("one", ImGuiTableColumnFlags_WidthFixed, 100.0f); // Default to 100.0f + ImGui::TableSetupColumn("two", ImGuiTableColumnFlags_WidthFixed, 200.0f); // Default to 200.0f + ImGui::TableSetupColumn("three", ImGuiTableColumnFlags_WidthFixed); // Default to auto + ImGui::TableHeadersRow(); + for (int row = 0; row < 4; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + if (row == 0) + ImGui::Text("(w: %5.1f)", ImGui::GetContentRegionAvail().x); + else + ImGui::Text("Hello %d,%d", column, row); + } + } + ImGui::EndTable(); + } + + HelpMarker("Using TableSetupColumn() to setup explicit width.\n\nUnless _NoKeepColumnsVisible is set, fixed columns with set width may still be shrunk down if there's not enough space in the host."); + + static ImGuiTableFlags flags2 = ImGuiTableFlags_None; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", &flags2, ImGuiTableFlags_NoKeepColumnsVisible); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags2, ImGuiTableFlags_BordersInnerV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags2, ImGuiTableFlags_BordersOuterV); + PopStyleCompact(); + if (ImGui::BeginTable("table2", 4, flags2)) + { + // We could also set ImGuiTableFlags_SizingFixedFit on the table and all columns will default to ImGuiTableColumnFlags_WidthFixed. + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 15.0f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 30.0f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 15.0f); + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 4; column++) + { + ImGui::TableSetColumnIndex(column); + if (row == 0) + ImGui::Text("(w: %5.1f)", ImGui::GetContentRegionAvail().x); + else + ImGui::Text("Hello %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Nested tables")) + { + HelpMarker("This demonstrate embedding a table into another table cell."); + + if (ImGui::BeginTable("table_nested1", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) + { + ImGui::TableSetupColumn("A0"); + ImGui::TableSetupColumn("A1"); + ImGui::TableHeadersRow(); + + ImGui::TableNextColumn(); + ImGui::Text("A0 Row 0"); + { + float rows_height = TEXT_BASE_HEIGHT * 2; + if (ImGui::BeginTable("table_nested2", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) + { + ImGui::TableSetupColumn("B0"); + ImGui::TableSetupColumn("B1"); + ImGui::TableHeadersRow(); + + ImGui::TableNextRow(ImGuiTableRowFlags_None, rows_height); + ImGui::TableNextColumn(); + ImGui::Text("B0 Row 0"); + ImGui::TableNextColumn(); + ImGui::Text("B1 Row 0"); + ImGui::TableNextRow(ImGuiTableRowFlags_None, rows_height); + ImGui::TableNextColumn(); + ImGui::Text("B0 Row 1"); + ImGui::TableNextColumn(); + ImGui::Text("B1 Row 1"); + + ImGui::EndTable(); + } + } + ImGui::TableNextColumn(); ImGui::Text("A1 Row 0"); + ImGui::TableNextColumn(); ImGui::Text("A0 Row 1"); + ImGui::TableNextColumn(); ImGui::Text("A1 Row 1"); + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Row height")) + { + HelpMarker("You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\nWe cannot honor a _maximum_ row height as that would requires a unique clipping rectangle per row."); + if (ImGui::BeginTable("table_row_height", 1, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerV)) + { + for (int row = 0; row < 10; row++) + { + float min_row_height = (float)(int)(TEXT_BASE_HEIGHT * 0.30f * row); + ImGui::TableNextRow(ImGuiTableRowFlags_None, min_row_height); + ImGui::TableNextColumn(); + ImGui::Text("min_row_height = %.2f", min_row_height); + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Outer size")) + { + // Showcasing use of ImGuiTableFlags_NoHostExtendX and ImGuiTableFlags_NoHostExtendY + // Important to that note how the two flags have slightly different behaviors! + ImGui::Text("Using NoHostExtendX and NoHostExtendY:"); + PushStyleCompact(); + static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_ContextMenuInBody | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoHostExtendX; + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX); + ImGui::SameLine(); HelpMarker("Make outer width auto-fit to columns, overriding outer_size.x value.\n\nOnly available when ScrollX/ScrollY are disabled and Stretch columns are not used."); + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendY", &flags, ImGuiTableFlags_NoHostExtendY); + ImGui::SameLine(); HelpMarker("Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit).\n\nOnly available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible."); + PopStyleCompact(); + + ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 5.5f); + if (ImGui::BeginTable("table1", 3, flags, outer_size)) + { + for (int row = 0; row < 10; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableNextColumn(); + ImGui::Text("Cell %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::SameLine(); + ImGui::Text("Hello!"); + + ImGui::Spacing(); + + ImGui::Text("Using explicit size:"); + if (ImGui::BeginTable("table2", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg, ImVec2(TEXT_BASE_WIDTH * 30, 0.0f))) + { + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableNextColumn(); + ImGui::Text("Cell %d,%d", column, row); + } + } + ImGui::EndTable(); + } + ImGui::SameLine(); + if (ImGui::BeginTable("table3", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg, ImVec2(TEXT_BASE_WIDTH * 30, 0.0f))) + { + for (int row = 0; row < 3; row++) + { + ImGui::TableNextRow(0, TEXT_BASE_HEIGHT * 1.5f); + for (int column = 0; column < 3; column++) + { + ImGui::TableNextColumn(); + ImGui::Text("Cell %d,%d", column, row); + } + } + ImGui::EndTable(); + } + + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Background color")) + { + static ImGuiTableFlags flags = ImGuiTableFlags_RowBg; + static int row_bg_type = 1; + static int row_bg_target = 1; + static int cell_bg_type = 1; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags, ImGuiTableFlags_Borders); + ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg); + ImGui::SameLine(); HelpMarker("ImGuiTableFlags_RowBg automatically sets RowBg0 to alternative colors pulled from the Style."); + ImGui::Combo("row bg type", (int*)&row_bg_type, "None\0Red\0Gradient\0"); + ImGui::Combo("row bg target", (int*)&row_bg_target, "RowBg0\0RowBg1\0"); ImGui::SameLine(); HelpMarker("Target RowBg0 to override the alternating odd/even colors,\nTarget RowBg1 to blend with them."); + ImGui::Combo("cell bg type", (int*)&cell_bg_type, "None\0Blue\0"); ImGui::SameLine(); HelpMarker("We are colorizing cells to B1->C2 here."); + IM_ASSERT(row_bg_type >= 0 && row_bg_type <= 2); + IM_ASSERT(row_bg_target >= 0 && row_bg_target <= 1); + IM_ASSERT(cell_bg_type >= 0 && cell_bg_type <= 1); + PopStyleCompact(); + + if (ImGui::BeginTable("table1", 5, flags)) + { + for (int row = 0; row < 6; row++) + { + ImGui::TableNextRow(); + + // Demonstrate setting a row background color with 'ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBgX, ...)' + // We use a transparent color so we can see the one behind in case our target is RowBg1 and RowBg0 was already targeted by the ImGuiTableFlags_RowBg flag. + if (row_bg_type != 0) + { + ImU32 row_bg_color = ImGui::GetColorU32(row_bg_type == 1 ? ImVec4(0.7f, 0.3f, 0.3f, 0.65f) : ImVec4(0.2f + row * 0.1f, 0.2f, 0.2f, 0.65f)); // Flat or Gradient? + ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg0 + row_bg_target, row_bg_color); + } + + // Fill cells + for (int column = 0; column < 5; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("%c%c", 'A' + row, '0' + column); + + // Change background of Cells B1->C2 + // Demonstrate setting a cell background color with 'ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ...)' + // (the CellBg color will be blended over the RowBg and ColumnBg colors) + // We can also pass a column number as a third parameter to TableSetBgColor() and do this outside the column loop. + if (row >= 1 && row <= 2 && column >= 1 && column <= 2 && cell_bg_type == 1) + { + ImU32 cell_bg_color = ImGui::GetColorU32(ImVec4(0.3f, 0.3f, 0.7f, 0.65f)); + ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, cell_bg_color); + } + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Tree view")) + { + static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody; + + if (ImGui::BeginTable("3ways", 3, flags)) + { + // The first column will use the default _WidthStretch when ScrollX is Off and _WidthFixed when ScrollX is On + ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_NoHide); + ImGui::TableSetupColumn("Size", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 12.0f); + ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 18.0f); + ImGui::TableHeadersRow(); + + // Simple storage to output a dummy file-system. + struct MyTreeNode + { + const char* Name; + const char* Type; + int Size; + int ChildIdx; + int ChildCount; + static void DisplayNode(const MyTreeNode* node, const MyTreeNode* all_nodes) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + const bool is_folder = (node->ChildCount > 0); + if (is_folder) + { + bool open = ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::TableNextColumn(); + ImGui::TextDisabled("--"); + ImGui::TableNextColumn(); + ImGui::TextUnformatted(node->Type); + if (open) + { + for (int child_n = 0; child_n < node->ChildCount; child_n++) + DisplayNode(&all_nodes[node->ChildIdx + child_n], all_nodes); + ImGui::TreePop(); + } + } + else + { + ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::TableNextColumn(); + ImGui::Text("%d", node->Size); + ImGui::TableNextColumn(); + ImGui::TextUnformatted(node->Type); + } + } + }; + static const MyTreeNode nodes[] = + { + { "Root", "Folder", -1, 1, 3 }, // 0 + { "Music", "Folder", -1, 4, 2 }, // 1 + { "Textures", "Folder", -1, 6, 3 }, // 2 + { "desktop.ini", "System file", 1024, -1,-1 }, // 3 + { "File1_a.wav", "Audio file", 123000, -1,-1 }, // 4 + { "File1_b.wav", "Audio file", 456000, -1,-1 }, // 5 + { "Image001.png", "Image file", 203128, -1,-1 }, // 6 + { "Copy of Image001.png", "Image file", 203256, -1,-1 }, // 7 + { "Copy of Image001 (Final2).png","Image file", 203512, -1,-1 }, // 8 + }; + + MyTreeNode::DisplayNode(&nodes[0], nodes); + + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Item width")) + { + HelpMarker( + "Showcase using PushItemWidth() and how it is preserved on a per-column basis.\n\n" + "Note that on auto-resizing non-resizable fixed columns, querying the content width for e.g. right-alignment doesn't make sense."); + if (ImGui::BeginTable("table_item_width", 3, ImGuiTableFlags_Borders)) + { + ImGui::TableSetupColumn("small"); + ImGui::TableSetupColumn("half"); + ImGui::TableSetupColumn("right-align"); + ImGui::TableHeadersRow(); + + for (int row = 0; row < 3; row++) + { + ImGui::TableNextRow(); + if (row == 0) + { + // Setup ItemWidth once (instead of setting up every time, which is also possible but less efficient) + ImGui::TableSetColumnIndex(0); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 3.0f); // Small + ImGui::TableSetColumnIndex(1); + ImGui::PushItemWidth(-ImGui::GetContentRegionAvail().x * 0.5f); + ImGui::TableSetColumnIndex(2); + ImGui::PushItemWidth(-FLT_MIN); // Right-aligned + } + + // Draw our contents + static float dummy_f = 0.0f; + ImGui::PushID(row); + ImGui::TableSetColumnIndex(0); + ImGui::SliderFloat("float0", &dummy_f, 0.0f, 1.0f); + ImGui::TableSetColumnIndex(1); + ImGui::SliderFloat("float1", &dummy_f, 0.0f, 1.0f); + ImGui::TableSetColumnIndex(2); + ImGui::SliderFloat("float2", &dummy_f, 0.0f, 1.0f); + ImGui::PopID(); + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + // Demonstrate using TableHeader() calls instead of TableHeadersRow() + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Custom headers")) + { + const int COLUMNS_COUNT = 3; + if (ImGui::BeginTable("table_custom_headers", COLUMNS_COUNT, ImGuiTableFlags_Borders | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) + { + ImGui::TableSetupColumn("Apricot"); + ImGui::TableSetupColumn("Banana"); + ImGui::TableSetupColumn("Cherry"); + + // Dummy entire-column selection storage + // FIXME: It would be nice to actually demonstrate full-featured selection using those checkbox. + static bool column_selected[3] = {}; + + // Instead of calling TableHeadersRow() we'll submit custom headers ourselves + ImGui::TableNextRow(ImGuiTableRowFlags_Headers); + for (int column = 0; column < COLUMNS_COUNT; column++) + { + ImGui::TableSetColumnIndex(column); + const char* column_name = ImGui::TableGetColumnName(column); // Retrieve name passed to TableSetupColumn() + ImGui::PushID(column); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); + ImGui::Checkbox("##checkall", &column_selected[column]); + ImGui::PopStyleVar(); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + ImGui::TableHeader(column_name); + ImGui::PopID(); + } + + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + char buf[32]; + sprintf(buf, "Cell %d,%d", column, row); + ImGui::TableSetColumnIndex(column); + ImGui::Selectable(buf, column_selected[column]); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + // Demonstrate creating custom context menus inside columns, while playing it nice with context menus provided by TableHeadersRow()/TableHeader() + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Context menus")) + { + HelpMarker("By default, right-clicking over a TableHeadersRow()/TableHeader() line will open the default context-menu.\nUsing ImGuiTableFlags_ContextMenuInBody we also allow right-clicking over columns body."); + static ImGuiTableFlags flags1 = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_ContextMenuInBody; + + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_ContextMenuInBody", &flags1, ImGuiTableFlags_ContextMenuInBody); + PopStyleCompact(); + + // Context Menus: first example + // [1.1] Right-click on the TableHeadersRow() line to open the default table context menu. + // [1.2] Right-click in columns also open the default table context menu (if ImGuiTableFlags_ContextMenuInBody is set) + const int COLUMNS_COUNT = 3; + if (ImGui::BeginTable("table_context_menu", COLUMNS_COUNT, flags1)) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + + // [1.1]] Right-click on the TableHeadersRow() line to open the default table context menu. + ImGui::TableHeadersRow(); + + // Submit dummy contents + for (int row = 0; row < 4; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < COLUMNS_COUNT; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Cell %d,%d", column, row); + } + } + ImGui::EndTable(); + } + + // Context Menus: second example + // [2.1] Right-click on the TableHeadersRow() line to open the default table context menu. + // [2.2] Right-click on the ".." to open a custom popup + // [2.3] Right-click in columns to open another custom popup + HelpMarker("Demonstrate mixing table context menu (over header), item context button (over button) and custom per-colum context menu (over column body)."); + ImGuiTableFlags flags2 = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders; + if (ImGui::BeginTable("table_context_menu_2", COLUMNS_COUNT, flags2)) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + + // [2.1] Right-click on the TableHeadersRow() line to open the default table context menu. + ImGui::TableHeadersRow(); + for (int row = 0; row < 4; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < COLUMNS_COUNT; column++) + { + // Submit dummy contents + ImGui::TableSetColumnIndex(column); + ImGui::Text("Cell %d,%d", column, row); + ImGui::SameLine(); + + // [2.2] Right-click on the ".." to open a custom popup + ImGui::PushID(row * COLUMNS_COUNT + column); + ImGui::SmallButton(".."); + if (ImGui::BeginPopupContextItem()) + { + ImGui::Text("This is the popup for Button(\"..\") in Cell %d,%d", column, row); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + ImGui::PopID(); + } + } + + // [2.3] Right-click anywhere in columns to open another custom popup + // (instead of testing for !IsAnyItemHovered() we could also call OpenPopup() with ImGuiPopupFlags_NoOpenOverExistingPopup + // to manage popup priority as the popups triggers, here "are we hovering a column" are overlapping) + int hovered_column = -1; + for (int column = 0; column < COLUMNS_COUNT + 1; column++) + { + ImGui::PushID(column); + if (ImGui::TableGetColumnFlags(column) & ImGuiTableColumnFlags_IsHovered) + hovered_column = column; + if (hovered_column == column && !ImGui::IsAnyItemHovered() && ImGui::IsMouseReleased(1)) + ImGui::OpenPopup("MyPopup"); + if (ImGui::BeginPopup("MyPopup")) + { + if (column == COLUMNS_COUNT) + ImGui::Text("This is a custom popup for unused space after the last column."); + else + ImGui::Text("This is a custom popup for Column %d", column); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + ImGui::PopID(); + } + + ImGui::EndTable(); + ImGui::Text("Hovered column: %d", hovered_column); } + ImGui::TreePop(); + } - // Call the more complete ShowExampleMenuFile which we use in various places of this demo - if (ImGui::Button("File Menu..")) - ImGui::OpenPopup("my_file_popup"); - if (ImGui::BeginPopup("my_file_popup")) + // Demonstrate creating multiple tables with the same ID + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Synced instances")) + { + HelpMarker("Multiple tables with the same identifier will share their settings, width, visibility, order etc."); + for (int n = 0; n < 3; n++) { - ShowExampleMenuFile(); - ImGui::EndPopup(); + char buf[32]; + sprintf(buf, "Synced Table %d", n); + bool open = ImGui::CollapsingHeader(buf, ImGuiTreeNodeFlags_DefaultOpen); + if (open && ImGui::BeginTable("Table", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings)) + { + ImGui::TableSetupColumn("One"); + ImGui::TableSetupColumn("Two"); + ImGui::TableSetupColumn("Three"); + ImGui::TableHeadersRow(); + for (int cell = 0; cell < 9; cell++) + { + ImGui::TableNextColumn(); + ImGui::Text("this cell %d", cell); + } + ImGui::EndTable(); + } } - ImGui::TreePop(); } - if (ImGui::TreeNode("Context menus")) + // Demonstrate using Sorting facilities + // This is a simplified version of the "Advanced" example, where we mostly focus on the code necessary to handle sorting. + // Note that the "Advanced" example also showcase manually triggering a sort (e.g. if item quantities have been modified) + static const char* template_items_names[] = { - // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing: - // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) - // OpenPopup(id); - // return BeginPopup(id); - // For more advanced uses you may want to replicate and customize this code. - // See details in BeginPopupContextItem(). - static float value = 0.5f; - ImGui::Text("Value = %.3f (<-- right-click here)", value); - if (ImGui::BeginPopupContextItem("item context menu")) + "Banana", "Apple", "Cherry", "Watermelon", "Grapefruit", "Strawberry", "Mango", + "Kiwi", "Orange", "Pineapple", "Blueberry", "Plum", "Coconut", "Pear", "Apricot" + }; + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Sorting")) + { + // Create item list + static ImVector items; + if (items.Size == 0) { - if (ImGui::Selectable("Set to zero")) value = 0.0f; - if (ImGui::Selectable("Set to PI")) value = 3.1415f; - ImGui::SetNextItemWidth(-1); - ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); - ImGui::EndPopup(); + items.resize(50, MyItem()); + for (int n = 0; n < items.Size; n++) + { + const int template_n = n % IM_ARRAYSIZE(template_items_names); + MyItem& item = items[n]; + item.ID = n; + item.Name = template_items_names[template_n]; + item.Quantity = (n * n - n) % 20; // Assign default quantities + } } - // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the - // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) - // will toggle the visibility of the popup above. - ImGui::Text("(You can also right-click me to open the same popup as above.)"); - ImGui::OpenPopupOnItemClick("item context menu", 1); + // Options + static ImGuiTableFlags flags = + ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti + | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_NoBordersInBody + | ImGuiTableFlags_ScrollY; + PushStyleCompact(); + ImGui::CheckboxFlags("ImGuiTableFlags_SortMulti", &flags, ImGuiTableFlags_SortMulti); + ImGui::SameLine(); HelpMarker("When sorting is enabled: hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1)."); + ImGui::CheckboxFlags("ImGuiTableFlags_SortTristate", &flags, ImGuiTableFlags_SortTristate); + ImGui::SameLine(); HelpMarker("When sorting is enabled: allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0)."); + PopStyleCompact(); + + if (ImGui::BeginTable("table_sorting", 4, flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 15), 0.0f)) + { + // Declare columns + // We use the "user_id" parameter of TableSetupColumn() to specify a user id that will be stored in the sort specifications. + // This is so our sort function can identify a column given our own identifier. We could also identify them based on their index! + // Demonstrate using a mixture of flags among available sort-related flags: + // - ImGuiTableColumnFlags_DefaultSort + // - ImGuiTableColumnFlags_NoSort / ImGuiTableColumnFlags_NoSortAscending / ImGuiTableColumnFlags_NoSortDescending + // - ImGuiTableColumnFlags_PreferSortAscending / ImGuiTableColumnFlags_PreferSortDescending + ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_ID); + ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name); + ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action); + ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending | ImGuiTableColumnFlags_WidthStretch, 0.0f, MyItemColumnID_Quantity); + ImGui::TableSetupScrollFreeze(0, 1); // Make row always visible + ImGui::TableHeadersRow(); + + // Sort our data if sort specs have been changed! + if (ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs()) + if (sorts_specs->SpecsDirty) + { + MyItem::s_current_sort_specs = sorts_specs; // Store in variable accessible by the sort function. + if (items.Size > 1) + qsort(&items[0], (size_t)items.Size, sizeof(items[0]), MyItem::CompareWithSortSpecs); + MyItem::s_current_sort_specs = NULL; + sorts_specs->SpecsDirty = false; + } - // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). - // BeginPopupContextItem() will use the last item ID as the popup ID. - // In addition here, we want to include your editable label inside the button label. - // We use the ### operator to override the ID (read FAQ about ID for details) - static char name[32] = "Label1"; - char buf[64]; - sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label - ImGui::Button(buf); - if (ImGui::BeginPopupContextItem()) - { - ImGui::Text("Edit name:"); - ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); - if (ImGui::Button("Close")) - ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); + // Demonstrate using clipper for large vertical lists + ImGuiListClipper clipper; + clipper.Begin(items.Size); + while (clipper.Step()) + for (int row_n = clipper.DisplayStart; row_n < clipper.DisplayEnd; row_n++) + { + // Display a data item + MyItem* item = &items[row_n]; + ImGui::PushID(item->ID); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::Text("%04d", item->ID); + ImGui::TableNextColumn(); + ImGui::TextUnformatted(item->Name); + ImGui::TableNextColumn(); + ImGui::SmallButton("None"); + ImGui::TableNextColumn(); + ImGui::Text("%d", item->Quantity); + ImGui::PopID(); + } + ImGui::EndTable(); } - ImGui::SameLine(); ImGui::Text("(<-- right-click here)"); - ImGui::TreePop(); } - if (ImGui::TreeNode("Modals")) + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // [DEBUG] + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Advanced")) { - ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside."); + static ImGuiTableFlags flags = + ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable + | ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti + | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBody + | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY + | ImGuiTableFlags_SizingFixedFit; + + enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable, CT_SelectableSpanRow }; + static int contents_type = CT_SelectableSpanRow; + const char* contents_type_names[] = { "Text", "Button", "SmallButton", "FillButton", "Selectable", "Selectable (span row)" }; + static int freeze_cols = 1; + static int freeze_rows = 1; + static int items_count = IM_ARRAYSIZE(template_items_names) * 2; + static ImVec2 outer_size_value = ImVec2(0.0f, TEXT_BASE_HEIGHT * 12); + static float row_min_height = 0.0f; // Auto + static float inner_width_with_scroll = 0.0f; // Auto-extend + static bool outer_size_enabled = true; + static bool show_headers = true; + static bool show_wrapped_text = false; + //static ImGuiTextFilter filter; + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // FIXME-TABLE: Enabling this results in initial clipped first pass on table which tend to affects column sizing + if (ImGui::TreeNode("Options")) + { + // Make the UI compact because there are so many fields + PushStyleCompact(); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 28.0f); + + if (ImGui::TreeNodeEx("Features:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("ImGuiTableFlags_Reorderable", &flags, ImGuiTableFlags_Reorderable); + ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable); + ImGui::CheckboxFlags("ImGuiTableFlags_Sortable", &flags, ImGuiTableFlags_Sortable); + ImGui::CheckboxFlags("ImGuiTableFlags_NoSavedSettings", &flags, ImGuiTableFlags_NoSavedSettings); + ImGui::CheckboxFlags("ImGuiTableFlags_ContextMenuInBody", &flags, ImGuiTableFlags_ContextMenuInBody); + ImGui::TreePop(); + } - if (ImGui::Button("Delete..")) - ImGui::OpenPopup("Delete?"); + if (ImGui::TreeNodeEx("Decorations:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags, ImGuiTableFlags_BordersOuterV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH); + ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appears in Headers"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers)"); + ImGui::TreePop(); + } - // Always center this window when appearing - ImVec2 center(ImGui::GetIO().DisplaySize.x * 0.5f, ImGui::GetIO().DisplaySize.y * 0.5f); - ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + if (ImGui::TreeNodeEx("Sizing:", ImGuiTreeNodeFlags_DefaultOpen)) + { + EditTableSizingFlags(&flags); + ImGui::SameLine(); HelpMarker("In the Advanced demo we override the policy of each column so those table-wide settings have less effect that typical."); + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendX", &flags, ImGuiTableFlags_NoHostExtendX); + ImGui::SameLine(); HelpMarker("Make outer width auto-fit to columns, overriding outer_size.x value.\n\nOnly available when ScrollX/ScrollY are disabled and Stretch columns are not used."); + ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendY", &flags, ImGuiTableFlags_NoHostExtendY); + ImGui::SameLine(); HelpMarker("Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit).\n\nOnly available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible."); + ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", &flags, ImGuiTableFlags_NoKeepColumnsVisible); + ImGui::SameLine(); HelpMarker("Only available if ScrollX is disabled."); + ImGui::CheckboxFlags("ImGuiTableFlags_PreciseWidths", &flags, ImGuiTableFlags_PreciseWidths); + ImGui::SameLine(); HelpMarker("Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth."); + ImGui::CheckboxFlags("ImGuiTableFlags_NoClip", &flags, ImGuiTableFlags_NoClip); + ImGui::SameLine(); HelpMarker("Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with ScrollFreeze options."); + ImGui::TreePop(); + } - if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) - { - ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); - ImGui::Separator(); + if (ImGui::TreeNodeEx("Padding:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::CheckboxFlags("ImGuiTableFlags_PadOuterX", &flags, ImGuiTableFlags_PadOuterX); + ImGui::CheckboxFlags("ImGuiTableFlags_NoPadOuterX", &flags, ImGuiTableFlags_NoPadOuterX); + ImGui::CheckboxFlags("ImGuiTableFlags_NoPadInnerX", &flags, ImGuiTableFlags_NoPadInnerX); + ImGui::TreePop(); + } - //static int unused_i = 0; - //ImGui::Combo("Combo", &unused_i, "Delete\0Delete harder\0"); + if (ImGui::TreeNodeEx("Scrolling:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX); + ImGui::SameLine(); + ImGui::SetNextItemWidth(ImGui::GetFrameHeight()); + ImGui::DragInt("freeze_cols", &freeze_cols, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput); + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); + ImGui::SameLine(); + ImGui::SetNextItemWidth(ImGui::GetFrameHeight()); + ImGui::DragInt("freeze_rows", &freeze_rows, 0.2f, 0, 9, NULL, ImGuiSliderFlags_NoInput); + ImGui::TreePop(); + } - static bool dont_ask_me_next_time = false; - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); - ImGui::Checkbox("Don't ask me next time", &dont_ask_me_next_time); - ImGui::PopStyleVar(); + if (ImGui::TreeNodeEx("Sorting:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::CheckboxFlags("ImGuiTableFlags_SortMulti", &flags, ImGuiTableFlags_SortMulti); + ImGui::SameLine(); HelpMarker("When sorting is enabled: hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1)."); + ImGui::CheckboxFlags("ImGuiTableFlags_SortTristate", &flags, ImGuiTableFlags_SortTristate); + ImGui::SameLine(); HelpMarker("When sorting is enabled: allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0)."); + ImGui::TreePop(); + } - if (ImGui::Button("OK", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } - ImGui::SetItemDefaultFocus(); - ImGui::SameLine(); - if (ImGui::Button("Cancel", ImVec2(120, 0))) { ImGui::CloseCurrentPopup(); } - ImGui::EndPopup(); + if (ImGui::TreeNodeEx("Other:", ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::Checkbox("show_headers", &show_headers); + ImGui::Checkbox("show_wrapped_text", &show_wrapped_text); + + ImGui::DragFloat2("##OuterSize", &outer_size_value.x); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + ImGui::Checkbox("outer_size", &outer_size_enabled); + ImGui::SameLine(); + HelpMarker("If scrolling is disabled (ScrollX and ScrollY not set):\n" + "- The table is output directly in the parent window.\n" + "- OuterSize.x < 0.0f will right-align the table.\n" + "- OuterSize.x = 0.0f will narrow fit the table unless there are any Stretch column.\n" + "- OuterSize.y then becomes the minimum size for the table, which will extend vertically if there are more rows (unless NoHostExtendY is set)."); + + // From a user point of view we will tend to use 'inner_width' differently depending on whether our table is embedding scrolling. + // To facilitate toying with this demo we will actually pass 0.0f to the BeginTable() when ScrollX is disabled. + ImGui::DragFloat("inner_width (when ScrollX active)", &inner_width_with_scroll, 1.0f, 0.0f, FLT_MAX); + + ImGui::DragFloat("row_min_height", &row_min_height, 1.0f, 0.0f, FLT_MAX); + ImGui::SameLine(); HelpMarker("Specify height of the Selectable item."); + + ImGui::DragInt("items_count", &items_count, 0.1f, 0, 9999); + ImGui::Combo("items_type (first column)", &contents_type, contents_type_names, IM_ARRAYSIZE(contents_type_names)); + //filter.Draw("filter"); + ImGui::TreePop(); + } + + ImGui::PopItemWidth(); + PopStyleCompact(); + ImGui::Spacing(); + ImGui::TreePop(); } - if (ImGui::Button("Stacked modals..")) - ImGui::OpenPopup("Stacked 1"); - if (ImGui::BeginPopupModal("Stacked 1", NULL, ImGuiWindowFlags_MenuBar)) + // Recreate/reset item list if we changed the number of items + static ImVector items; + static ImVector selection; + static bool items_need_sort = false; + if (items.Size != items_count) { - if (ImGui::BeginMenuBar()) + items.resize(items_count, MyItem()); + for (int n = 0; n < items_count; n++) { - if (ImGui::BeginMenu("File")) - { - if (ImGui::MenuItem("Some menu item")) {} - ImGui::EndMenu(); - } - ImGui::EndMenuBar(); + const int template_n = n % IM_ARRAYSIZE(template_items_names); + MyItem& item = items[n]; + item.ID = n; + item.Name = template_items_names[template_n]; + item.Quantity = (template_n == 3) ? 10 : (template_n == 4) ? 20 : 0; // Assign default quantities } - ImGui::Text("Hello from Stacked The First\nUsing style.Colors[ImGuiCol_ModalWindowDimBg] behind it."); + } - // Testing behavior of widgets stacking their own regular popups over the modal. - static int item = 1; - static float color[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; - ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); - ImGui::ColorEdit4("color", color); + const ImDrawList* parent_draw_list = ImGui::GetWindowDrawList(); + const int parent_draw_list_draw_cmd_count = parent_draw_list->CmdBuffer.Size; + ImVec2 table_scroll_cur, table_scroll_max; // For debug display + const ImDrawList* table_draw_list = NULL; // " + + const float inner_width_to_use = (flags & ImGuiTableFlags_ScrollX) ? inner_width_with_scroll : 0.0f; + if (ImGui::BeginTable("table_advanced", 6, flags, outer_size_enabled ? outer_size_value : ImVec2(0, 0), inner_width_to_use)) + { + // Declare columns + // We use the "user_id" parameter of TableSetupColumn() to specify a user id that will be stored in the sort specifications. + // This is so our sort function can identify a column given our own identifier. We could also identify them based on their index! + ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, 0.0f, MyItemColumnID_ID); + ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name); + ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action); + ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending, 0.0f, MyItemColumnID_Quantity); + ImGui::TableSetupColumn("Description", (flags & ImGuiTableFlags_NoHostExtendX) ? 0 : ImGuiTableColumnFlags_WidthStretch, 0.0f, MyItemColumnID_Description); + ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort); + ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows); + + // Sort our data if sort specs have been changed! + ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs(); + if (sorts_specs && sorts_specs->SpecsDirty) + items_need_sort = true; + if (sorts_specs && items_need_sort && items.Size > 1) + { + MyItem::s_current_sort_specs = sorts_specs; // Store in variable accessible by the sort function. + qsort(&items[0], (size_t)items.Size, sizeof(items[0]), MyItem::CompareWithSortSpecs); + MyItem::s_current_sort_specs = NULL; + sorts_specs->SpecsDirty = false; + } + items_need_sort = false; - if (ImGui::Button("Add another modal..")) - ImGui::OpenPopup("Stacked 2"); + // Take note of whether we are currently sorting based on the Quantity field, + // we will use this to trigger sorting when we know the data of this column has been modified. + const bool sorts_specs_using_quantity = (ImGui::TableGetColumnFlags(3) & ImGuiTableColumnFlags_IsSorted) != 0; - // Also demonstrate passing a bool* to BeginPopupModal(), this will create a regular close button which - // will close the popup. Note that the visibility state of popups is owned by imgui, so the input value - // of the bool actually doesn't matter here. - bool unused_open = true; - if (ImGui::BeginPopupModal("Stacked 2", &unused_open)) + // Show headers + if (show_headers) + ImGui::TableHeadersRow(); + + // Show data + // FIXME-TABLE FIXME-NAV: How we can get decent up/down even though we have the buttons here? + ImGui::PushButtonRepeat(true); +#if 1 + // Demonstrate using clipper for large vertical lists + ImGuiListClipper clipper; + clipper.Begin(items.Size); + while (clipper.Step()) { - ImGui::Text("Hello from Stacked The Second!"); - if (ImGui::Button("Close")) - ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); - } + for (int row_n = clipper.DisplayStart; row_n < clipper.DisplayEnd; row_n++) +#else + // Without clipper + { + for (int row_n = 0; row_n < items.Size; row_n++) +#endif + { + MyItem* item = &items[row_n]; + //if (!filter.PassFilter(item->Name)) + // continue; + + const bool item_is_selected = selection.contains(item->ID); + ImGui::PushID(item->ID); + ImGui::TableNextRow(ImGuiTableRowFlags_None, row_min_height); + ImGui::TableNextColumn(); + + // For the demo purpose we can select among different type of items submitted in the first column + char label[32]; + sprintf(label, "%04d", item->ID); + if (contents_type == CT_Text) + ImGui::TextUnformatted(label); + else if (contents_type == CT_Button) + ImGui::Button(label); + else if (contents_type == CT_SmallButton) + ImGui::SmallButton(label); + else if (contents_type == CT_FillButton) + ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); + else if (contents_type == CT_Selectable || contents_type == CT_SelectableSpanRow) + { + ImGuiSelectableFlags selectable_flags = (contents_type == CT_SelectableSpanRow) ? ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap : ImGuiSelectableFlags_None; + if (ImGui::Selectable(label, item_is_selected, selectable_flags, ImVec2(0, row_min_height))) + { + if (ImGui::GetIO().KeyCtrl) + { + if (item_is_selected) + selection.find_erase_unsorted(item->ID); + else + selection.push_back(item->ID); + } + else + { + selection.clear(); + selection.push_back(item->ID); + } + } + } - if (ImGui::Button("Close")) - ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); - } + if (ImGui::TableNextColumn()) + ImGui::TextUnformatted(item->Name); - ImGui::TreePop(); - } + // Here we demonstrate marking our data set as needing to be sorted again if we modified a quantity, + // and we are currently sorting on the column showing the Quantity. + // To avoid triggering a sort while holding the button, we only trigger it when the button has been released. + // You will probably need a more advanced system in your code if you want to automatically sort when a specific entry changes. + if (ImGui::TableNextColumn()) + { + if (ImGui::SmallButton("Chop")) { item->Quantity += 1; } + if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; } + ImGui::SameLine(); + if (ImGui::SmallButton("Eat")) { item->Quantity -= 1; } + if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; } + } - if (ImGui::TreeNode("Menus inside a regular window")) - { - ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); - ImGui::Separator(); + if (ImGui::TableNextColumn()) + ImGui::Text("%d", item->Quantity); - // Note: As a quirk in this very specific example, we want to differentiate the parent of this menu from the - // parent of the various popup menus above. To do so we are encloding the items in a PushID()/PopID() block - // to make them two different menusets. If we don't, opening any popup above and hovering our menu here would - // open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, - // which is the desired behavior for regular menus. - ImGui::PushID("foo"); - ImGui::MenuItem("Menu item", "CTRL+M"); - if (ImGui::BeginMenu("Menu inside a regular window")) - { - ShowExampleMenuFile(); - ImGui::EndMenu(); + ImGui::TableNextColumn(); + if (show_wrapped_text) + ImGui::TextWrapped("Lorem ipsum dolor sit amet"); + else + ImGui::Text("Lorem ipsum dolor sit amet"); + + if (ImGui::TableNextColumn()) + ImGui::Text("1234"); + + ImGui::PopID(); + } + } + ImGui::PopButtonRepeat(); + + // Store some info to display debug details below + table_scroll_cur = ImVec2(ImGui::GetScrollX(), ImGui::GetScrollY()); + table_scroll_max = ImVec2(ImGui::GetScrollMaxX(), ImGui::GetScrollMaxY()); + table_draw_list = ImGui::GetWindowDrawList(); + ImGui::EndTable(); + } + static bool show_debug_details = false; + ImGui::Checkbox("Debug details", &show_debug_details); + if (show_debug_details && table_draw_list) + { + ImGui::SameLine(0.0f, 0.0f); + const int table_draw_list_draw_cmd_count = table_draw_list->CmdBuffer.Size; + if (table_draw_list == parent_draw_list) + ImGui::Text(": DrawCmd: +%d (in same window)", + table_draw_list_draw_cmd_count - parent_draw_list_draw_cmd_count); + else + ImGui::Text(": DrawCmd: +%d (in child window), Scroll: (%.f/%.f) (%.f/%.f)", + table_draw_list_draw_cmd_count - 1, table_scroll_cur.x, table_scroll_max.x, table_scroll_cur.y, table_scroll_max.y); } - ImGui::PopID(); - ImGui::Separator(); ImGui::TreePop(); } + + ImGui::PopID(); + + ShowDemoWindowColumns(); + + if (disable_indent) + ImGui::PopStyleVar(); } +// Demonstrate old/legacy Columns API! +// [2020: Columns are under-featured and not maintained. Prefer using the more flexible and powerful BeginTable() API!] static void ShowDemoWindowColumns() { - if (!ImGui::CollapsingHeader("Columns")) - return; - - ImGui::PushID("Columns"); - - static bool disable_indent = false; - ImGui::Checkbox("Disable tree indentation", &disable_indent); + bool open = ImGui::TreeNode("Legacy Columns API"); ImGui::SameLine(); - HelpMarker("Disable the indenting of tree nodes so demo columns can use the full window width."); - if (disable_indent) - ImGui::PushStyleVar(ImGuiStyleVar_IndentSpacing, 0.0f); + HelpMarker("Columns() is an old API! Prefer using the more flexible and powerful BeginTable() API!"); + if (!open) + return; // Basic columns if (ImGui::TreeNode("Basic")) @@ -3350,40 +5305,16 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } - // Scrolling columns - /* - if (ImGui::TreeNode("Vertical Scrolling")) - { - ImGui::BeginChild("##header", ImVec2(0, ImGui::GetTextLineHeightWithSpacing()+ImGui::GetStyle().ItemSpacing.y)); - ImGui::Columns(3); - ImGui::Text("ID"); ImGui::NextColumn(); - ImGui::Text("Name"); ImGui::NextColumn(); - ImGui::Text("Path"); ImGui::NextColumn(); - ImGui::Columns(1); - ImGui::Separator(); - ImGui::EndChild(); - ImGui::BeginChild("##scrollingregion", ImVec2(0, 60)); - ImGui::Columns(3); - for (int i = 0; i < 10; i++) - { - ImGui::Text("%04d", i); ImGui::NextColumn(); - ImGui::Text("Foobar"); ImGui::NextColumn(); - ImGui::Text("/path/foobar/%04d/", i); ImGui::NextColumn(); - } - ImGui::Columns(1); - ImGui::EndChild(); - ImGui::TreePop(); - } - */ - if (ImGui::TreeNode("Horizontal Scrolling")) { ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f)); ImVec2 child_size = ImVec2(0, ImGui::GetFontSize() * 20.0f); ImGui::BeginChild("##ScrollingRegion", child_size, false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::Columns(10); + + // Also demonstrate using clipper for large vertical lists int ITEMS_COUNT = 2000; - ImGuiListClipper clipper; // Also demonstrate using the clipper for large list + ImGuiListClipper clipper; clipper.Begin(ITEMS_COUNT); while (clipper.Step()) { @@ -3435,9 +5366,7 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } - if (disable_indent) - ImGui::PopStyleVar(); - ImGui::PopID(); + ImGui::TreePop(); } static void ShowDemoWindowMisc() @@ -3695,6 +5624,9 @@ void ImGui::ShowAboutWindow(bool* p_open) #ifdef _MSC_VER ImGui::Text("define: _MSC_VER=%d", _MSC_VER); #endif +#ifdef _MSVC_LANG + ImGui::Text("define: _MSVC_LANG=%d", (int)_MSVC_LANG); +#endif #ifdef __MINGW32__ ImGui::Text("define: __MINGW32__"); #endif @@ -3765,13 +5697,13 @@ void ImGui::ShowAboutWindow(bool* p_open) bool ImGui::ShowStyleSelector(const char* label) { static int style_idx = -1; - if (ImGui::Combo(label, &style_idx, "Classic\0Dark\0Light\0")) + if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0")) { switch (style_idx) { - case 0: ImGui::StyleColorsClassic(); break; - case 1: ImGui::StyleColorsDark(); break; - case 2: ImGui::StyleColorsLight(); break; + case 0: ImGui::StyleColorsDark(); break; + case 1: ImGui::StyleColorsLight(); break; + case 2: ImGui::StyleColorsClassic(); break; } return true; } @@ -3942,6 +5874,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::Text("Main"); ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f"); + ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f"); @@ -4284,7 +6217,7 @@ struct ExampleAppConsole // Portable helpers static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; } static int Strnicmp(const char* s1, const char* s2, int n) { int d = 0; while (n > 0 && (d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; n--; } return d; } - static char* Strdup(const char* s) { size_t len = strlen(s) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } + static char* Strdup(const char* s) { IM_ASSERT(s); size_t len = strlen(s) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; } void ClearLog() @@ -4833,12 +6766,13 @@ static void ShowPlaceholderObject(const char* prefix, int uid) ImGui::PushID(uid); // Text and Tree nodes are less high than framed widgets, using AlignTextToFramePadding() we add vertical spacing to make the tree lines equal high. + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::AlignTextToFramePadding(); bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); - ImGui::NextColumn(); - ImGui::AlignTextToFramePadding(); + ImGui::TableSetColumnIndex(1); ImGui::Text("my sailor is rich"); - ImGui::NextColumn(); + if (node_open) { static float placeholder_members[8] = { 0.0f, 0.0f, 1.0f, 3.1416f, 100.0f, 999.0f }; @@ -4852,11 +6786,14 @@ static void ShowPlaceholderObject(const char* prefix, int uid) else { // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well) + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::AlignTextToFramePadding(); ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet; ImGui::TreeNodeEx("Field", flags, "Field_%d", i); - ImGui::NextColumn(); - ImGui::SetNextItemWidth(-1); + + ImGui::TableSetColumnIndex(1); + ImGui::SetNextItemWidth(-FLT_MIN); if (i >= 5) ImGui::InputFloat("##value", &placeholder_members[i], 1.0f); else @@ -4887,15 +6824,16 @@ static void ShowExampleAppPropertyEditor(bool* p_open) "your cursor horizontally instead of using the Columns() API."); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); - ImGui::Columns(2); - ImGui::Separator(); - - // Iterate placeholder objects (all the same data) - for (int obj_i = 0; obj_i < 3; obj_i++) - ShowPlaceholderObject("Object", obj_i); - - ImGui::Columns(1); - ImGui::Separator(); + if (ImGui::BeginTable("split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable)) + { + // Iterate placeholder objects (all the same data) + for (int obj_i = 0; obj_i < 4; obj_i++) + { + ShowPlaceholderObject("Object", obj_i); + //ImGui::Separator(); + } + ImGui::EndTable(); + } ImGui::PopStyleVar(); ImGui::End(); } @@ -5040,23 +6978,29 @@ static void ShowExampleAppConstrainedResize(bool* p_open) } //----------------------------------------------------------------------------- -// [SECTION] Example App: Simple Overlay / ShowExampleAppSimpleOverlay() +// [SECTION] Example App: Simple overlay / ShowExampleAppSimpleOverlay() //----------------------------------------------------------------------------- // Demonstrate creating a simple static window with no decoration // + a context-menu to choose which corner of the screen to use. static void ShowExampleAppSimpleOverlay(bool* p_open) { - const float DISTANCE = 10.0f; + const float PAD = 10.0f; static int corner = 0; ImGuiIO& io = ImGui::GetIO(); ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; if (corner != -1) { - window_flags |= ImGuiWindowFlags_NoMove; - ImVec2 window_pos = ImVec2((corner & 1) ? io.DisplaySize.x - DISTANCE : DISTANCE, (corner & 2) ? io.DisplaySize.y - DISTANCE : DISTANCE); - ImVec2 window_pos_pivot = ImVec2((corner & 1) ? 1.0f : 0.0f, (corner & 2) ? 1.0f : 0.0f); + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImVec2 work_pos = viewport->WorkPos; // Use work area to avoid menu-bar/task-bar, if any! + ImVec2 work_size = viewport->WorkSize; + ImVec2 window_pos, window_pos_pivot; + window_pos.x = (corner & 1) ? (work_pos.x + work_size.x - PAD) : (work_pos.x + PAD); + window_pos.y = (corner & 2) ? (work_pos.y + work_size.y - PAD) : (work_pos.y + PAD); + window_pos_pivot.x = (corner & 1) ? 1.0f : 0.0f; + window_pos_pivot.y = (corner & 2) ? 1.0f : 0.0f; ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); + window_flags |= ImGuiWindowFlags_NoMove; } ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background if (ImGui::Begin("Example: Simple overlay", p_open, window_flags)) @@ -5081,6 +7025,42 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) ImGui::End(); } +//----------------------------------------------------------------------------- +// [SECTION] Example App: Fullscreen window / ShowExampleAppFullscreen() +//----------------------------------------------------------------------------- + +// Demonstrate creating a window covering the entire screen/viewport +static void ShowExampleAppFullscreen(bool* p_open) +{ + static bool use_work_area = true; + static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; + + // We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.) + // Based on your use case you may want one of the other. + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos); + ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size); + + if (ImGui::Begin("Example: Fullscreen window", p_open, flags)) + { + ImGui::Checkbox("Use work area instead of main area", &use_work_area); + ImGui::SameLine(); + HelpMarker("Main Area = entire viewport,\nWork Area = entire viewport minus sections used by the main menu bars, task bars etc.\n\nEnable the main-menu bar in Examples menu to see the difference."); + + ImGui::CheckboxFlags("ImGuiWindowFlags_NoBackground", &flags, ImGuiWindowFlags_NoBackground); + ImGui::CheckboxFlags("ImGuiWindowFlags_NoDecoration", &flags, ImGuiWindowFlags_NoDecoration); + ImGui::Indent(); + ImGui::CheckboxFlags("ImGuiWindowFlags_NoTitleBar", &flags, ImGuiWindowFlags_NoTitleBar); + ImGui::CheckboxFlags("ImGuiWindowFlags_NoCollapse", &flags, ImGuiWindowFlags_NoCollapse); + ImGui::CheckboxFlags("ImGuiWindowFlags_NoScrollbar", &flags, ImGuiWindowFlags_NoScrollbar); + ImGui::Unindent(); + + if (p_open && ImGui::Button("Close this window")) + *p_open = false; + } + ImGui::End(); +} + //----------------------------------------------------------------------------- // [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles() //----------------------------------------------------------------------------- @@ -5090,16 +7070,19 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) // Read FAQ section "How can I have multiple widgets with the same label?" for details. static void ShowExampleAppWindowTitles(bool*) { + const ImGuiViewport* viewport = ImGui::GetMainViewport(); + const ImVec2 base_pos = viewport->Pos; + // By default, Windows are uniquely identified by their title. // You can use the "##" and "###" markers to manipulate the display/ID. // Using "##" to display same title but have unique identifier. - ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 100), ImGuiCond_FirstUseEver); ImGui::Begin("Same title as another window##1"); ImGui::Text("This is window 1.\nMy title is the same as window 2, but my identifier is unique."); ImGui::End(); - ImGui::SetNextWindowPos(ImVec2(100, 200), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 200), ImGuiCond_FirstUseEver); ImGui::Begin("Same title as another window##2"); ImGui::Text("This is window 2.\nMy title is the same as window 1, but my identifier is unique."); ImGui::End(); @@ -5107,7 +7090,7 @@ static void ShowExampleAppWindowTitles(bool*) // Using "###" to display a changing title but keep a static identifier "AnimatedTitle" char buf[128]; sprintf(buf, "Animated title %c %d###AnimatedTitle", "|/-\\"[(int)(ImGui::GetTime() / 0.25f) & 3], ImGui::GetFrameCount()); - ImGui::SetNextWindowPos(ImVec2(100, 300), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 300), ImGuiCond_FirstUseEver); ImGui::Begin(buf); ImGui::Text("This window has a changing title."); ImGui::End(); @@ -5135,7 +7118,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) { if (ImGui::BeginTabItem("Primitives")) { - ImGui::PushItemWidth(-ImGui::GetFontSize() * 10); + ImGui::PushItemWidth(-ImGui::GetFontSize() * 15); ImDrawList* draw_list = ImGui::GetWindowDrawList(); // Draw gradients @@ -5167,14 +7150,18 @@ static void ShowExampleAppCustomRendering(bool* p_open) static int ngon_sides = 6; static bool circle_segments_override = false; static int circle_segments_override_v = 12; + static bool curve_segments_override = false; + static int curve_segments_override_v = 8; static ImVec4 colf = ImVec4(1.0f, 1.0f, 0.4f, 1.0f); - ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 72.0f, "%.0f"); + ImGui::DragFloat("Size", &sz, 0.2f, 2.0f, 100.0f, "%.0f"); ImGui::DragFloat("Thickness", &thickness, 0.05f, 1.0f, 8.0f, "%.02f"); ImGui::SliderInt("N-gon sides", &ngon_sides, 3, 12); ImGui::Checkbox("##circlesegmentoverride", &circle_segments_override); ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); - if (ImGui::SliderInt("Circle segments", &circle_segments_override_v, 3, 40)) - circle_segments_override = true; + circle_segments_override |= ImGui::SliderInt("Circle segments override", &circle_segments_override_v, 3, 40); + ImGui::Checkbox("##curvessegmentoverride", &curve_segments_override); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + curve_segments_override |= ImGui::SliderInt("Curves segments override", &curve_segments_override_v, 3, 40); ImGui::ColorEdit4("Color", &colf.x); const ImVec2 p = ImGui::GetCursorScreenPos(); @@ -5183,7 +7170,9 @@ static void ShowExampleAppCustomRendering(bool* p_open) const ImDrawCornerFlags corners_none = 0; const ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; const ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; + const float rounding = sz / 5.0f; const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; + const int curve_segments = curve_segments_override ? curve_segments_override_v : 0; float x = p.x + 4.0f; float y = p.y + 4.0f; for (int n = 0; n < 2; n++) @@ -5193,14 +7182,22 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_all, th); x += sz + spacing; // Square with all rounded corners - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_all, th); x += sz + spacing; // Square with all rounded corners + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th);x += sz + spacing; // Triangle //draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th);x+= sz*0.4f + spacing; // Thin triangle draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line - draw_list->AddBezierCurve(ImVec2(x, y), ImVec2(x + sz*1.3f, y + sz*0.3f), ImVec2(x + sz - sz*1.3f, y + sz - sz*0.3f), ImVec2(x + sz, y + sz), col, th); + + // Quadratic Bezier Curve (3 control points) + ImVec2 cp3[3] = { ImVec2(x, y + sz * 0.6f), ImVec2(x + sz * 0.5f, y - sz * 0.4f), ImVec2(x + sz, y + sz) }; + draw_list->AddBezierQuadratic(cp3[0], cp3[1], cp3[2], col, th, curve_segments); x += sz + spacing; + + // Cubic Bezier Curve (4 control points) + ImVec2 cp4[4] = { ImVec2(x, y), ImVec2(x + sz * 1.3f, y + sz * 0.3f), ImVec2(x + sz - sz * 1.3f, y + sz - sz * 0.3f), ImVec2(x + sz, y + sz) }; + draw_list->AddBezierCubic(cp4[0], cp4[1], cp4[2], cp4[3], col, th, curve_segments); + x = p.x + 4; y += sz + spacing; } @@ -5216,7 +7213,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); - ImGui::Dummy(ImVec2((sz + spacing) * 8.8f, (sz + spacing) * 3.0f)); + ImGui::Dummy(ImVec2((sz + spacing) * 10.2f, (sz + spacing) * 3.0f)); ImGui::PopItemWidth(); ImGui::EndTabItem(); } @@ -5571,19 +7568,20 @@ void ShowExampleAppDocuments(bool* p_open) { if (!ImGui::IsPopupOpen("Save?")) ImGui::OpenPopup("Save?"); - if (ImGui::BeginPopupModal("Save?")) + if (ImGui::BeginPopupModal("Save?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::Text("Save change to the following items?"); - ImGui::SetNextItemWidth(-1.0f); - if (ImGui::ListBoxHeader("##", close_queue_unsaved_documents, 6)) + float item_height = ImGui::GetTextLineHeightWithSpacing(); + if (ImGui::BeginChildFrame(ImGui::GetID("frame"), ImVec2(-FLT_MIN, 6.25f * item_height))) { for (int n = 0; n < close_queue.Size; n++) if (close_queue[n]->Dirty) ImGui::Text("%s", close_queue[n]->Name); - ImGui::ListBoxFooter(); + ImGui::EndChildFrame(); } - if (ImGui::Button("Yes", ImVec2(80, 0))) + ImVec2 button_size(ImGui::GetFontSize() * 7.0f, 0.0f); + if (ImGui::Button("Yes", button_size)) { for (int n = 0; n < close_queue.Size; n++) { @@ -5595,7 +7593,7 @@ void ShowExampleAppDocuments(bool* p_open) ImGui::CloseCurrentPopup(); } ImGui::SameLine(); - if (ImGui::Button("No", ImVec2(80, 0))) + if (ImGui::Button("No", button_size)) { for (int n = 0; n < close_queue.Size; n++) close_queue[n]->DoForceClose(); @@ -5603,7 +7601,7 @@ void ShowExampleAppDocuments(bool* p_open) ImGui::CloseCurrentPopup(); } ImGui::SameLine(); - if (ImGui::Button("Cancel", ImVec2(80, 0))) + if (ImGui::Button("Cancel", button_size)) { close_queue.clear(); ImGui::CloseCurrentPopup(); diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 306c296e..5c11a5b3 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (drawing and font code) /* @@ -32,7 +32,11 @@ Index of this file: #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif + #include "imgui_internal.h" +#ifdef IMGUI_ENABLE_FREETYPE +#include "misc/freetype/imgui_freetype.h" +#endif #include // vsnprintf, sscanf, printf #if !defined(alloca) @@ -118,7 +122,7 @@ namespace IMGUI_STB_NAMESPACE #endif #ifndef STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) -#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION +#ifndef IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION // in case the user already have an implementation in another compilation unit #define STBRP_STATIC #define STBRP_ASSERT(x) do { IM_ASSERT(x); } while (0) #define STBRP_SORT ImQsort @@ -131,8 +135,9 @@ namespace IMGUI_STB_NAMESPACE #endif #endif +#ifdef IMGUI_ENABLE_STB_TRUETYPE #ifndef STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in the _same_ compilation unit (e.g. unity builds) -#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION +#ifndef IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION // in case the user already have an implementation in another compilation unit #define STBTT_malloc(x,u) ((void)(u), IM_ALLOC(x)) #define STBTT_free(x,u) ((void)(u), IM_FREE(x)) #define STBTT_assert(x) do { IM_ASSERT(x); } while(0) @@ -153,6 +158,7 @@ namespace IMGUI_STB_NAMESPACE #include "imstb_truetype.h" #endif #endif +#endif // IMGUI_ENABLE_STB_TRUETYPE #if defined(__GNUC__) #pragma GCC diagnostic pop @@ -210,7 +216,7 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst) colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); - colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.25f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.20f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); @@ -222,6 +228,11 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst) colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); + colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); @@ -237,7 +248,7 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f); colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); - colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.70f); + colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.85f); colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_PopupBg] = ImVec4(0.11f, 0.11f, 0.14f, 0.92f); colors[ImGuiCol_Border] = ImVec4(0.50f, 0.50f, 0.50f, 0.50f); @@ -265,7 +276,7 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_Separator] = ImVec4(0.50f, 0.50f, 0.50f, 0.60f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.60f, 0.60f, 0.70f, 1.00f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.70f, 0.70f, 0.90f, 1.00f); - colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.16f); + colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); @@ -277,6 +288,11 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TableHeaderBg] = ImVec4(0.27f, 0.27f, 0.38f, 1.00f); + colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.45f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableBorderLight] = ImVec4(0.26f, 0.26f, 0.28f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; @@ -321,7 +337,7 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) colors[ImGuiCol_Separator] = ImVec4(0.39f, 0.39f, 0.39f, 0.62f); colors[ImGuiCol_SeparatorHovered] = ImVec4(0.14f, 0.44f, 0.80f, 0.78f); colors[ImGuiCol_SeparatorActive] = ImVec4(0.14f, 0.44f, 0.80f, 1.00f); - colors[ImGuiCol_ResizeGrip] = ImVec4(0.80f, 0.80f, 0.80f, 0.56f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.35f, 0.35f, 0.35f, 0.17f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); @@ -333,6 +349,11 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.45f, 0.00f, 1.00f); + colors[ImGuiCol_TableHeaderBg] = ImVec4(0.78f, 0.87f, 0.98f, 1.00f); + colors[ImGuiCol_TableBorderStrong] = ImVec4(0.57f, 0.57f, 0.64f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableBorderLight] = ImVec4(0.68f, 0.68f, 0.74f, 1.00f); // Prefer using Alpha=1.0 here + colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f); colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; @@ -362,8 +383,8 @@ void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) CircleSegmentMaxError = max_error; for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) { - const float radius = i + 1.0f; - const int segment_count = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError); + const float radius = (float)i; + const int segment_count = (i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : 0; CircleSegmentCounts[i] = (ImU8)ImMin(segment_count, 255); } } @@ -390,6 +411,7 @@ void ImDrawList::_ResetForNewFrame() _Path.resize(0); _Splitter.Clear(); CmdBuffer.push_back(ImDrawCmd()); + _FringeScale = 1.0f; } void ImDrawList::_ClearFreeMemory() @@ -665,12 +687,12 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 const ImVec2 opaque_uv = _Data->TexUvWhitePixel; const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw - const bool thick_line = (thickness > 1.0f); + const bool thick_line = (thickness > _FringeScale); if (Flags & ImDrawListFlags_AntiAliasedLines) { // Anti-aliased stroke - const float AA_SIZE = 1.0f; + const float AA_SIZE = _FringeScale; const ImU32 col_trans = col & ~IM_COL32_A_MASK; // Thicknesses <1.0 should behave like thickness 1.0 @@ -681,7 +703,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 // Do we want to draw this line using a texture? // - For now, only draw integer-width lines using textures to avoid issues with the way scaling occurs, could be improved. // - If AA_SIZE is not 1.0f we cannot use the texture path. - const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f); + const bool use_texture = (Flags & ImDrawListFlags_AntiAliasedLinesUseTex) && (integer_thickness < IM_DRAWLIST_TEX_LINES_WIDTH_MAX) && (fractional_thickness <= 0.00001f) && (AA_SIZE == 1.0f); // We should never hit this, because NewFrame() doesn't set ImDrawListFlags_AntiAliasedLinesUseTex unless ImFontAtlasFlags_NoBakedLines is off IM_ASSERT_PARANOID(!use_texture || !(_Data->Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)); @@ -778,14 +800,14 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 { // If we're using textures we only need to emit the left/right edge vertices ImVec4 tex_uvs = _Data->TexUvLines[integer_thickness]; - if (fractional_thickness != 0.0f) + /*if (fractional_thickness != 0.0f) // Currently always zero when use_texture==false! { const ImVec4 tex_uvs_1 = _Data->TexUvLines[integer_thickness + 1]; tex_uvs.x = tex_uvs.x + (tex_uvs_1.x - tex_uvs.x) * fractional_thickness; // inlined ImLerp() tex_uvs.y = tex_uvs.y + (tex_uvs_1.y - tex_uvs.y) * fractional_thickness; tex_uvs.z = tex_uvs.z + (tex_uvs_1.z - tex_uvs.z) * fractional_thickness; tex_uvs.w = tex_uvs.w + (tex_uvs_1.w - tex_uvs.w) * fractional_thickness; - } + }*/ ImVec2 tex_uv0(tex_uvs.x, tex_uvs.y); ImVec2 tex_uv1(tex_uvs.z, tex_uvs.w); for (int i = 0; i < points_count; i++) @@ -923,7 +945,7 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun if (Flags & ImDrawListFlags_AntiAliasedFill) { // Anti-aliased Fill - const float AA_SIZE = 1.0f; + const float AA_SIZE = _FringeScale; const ImU32 col_trans = col & ~IM_COL32_A_MASK; const int idx_count = (points_count - 2)*3 + points_count * 6; const int vtx_count = (points_count * 2); @@ -996,11 +1018,12 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) { - if (radius == 0.0f || a_min_of_12 > a_max_of_12) + if (radius == 0.0f) { _Path.push_back(center); return; } + IM_ASSERT(a_min_of_12 <= a_max_of_12); // For legacy reason the PathArcToFast() always takes angles where 2*PI is represented by 12, // but it is possible to set IM_DRAWLIST_ARCFAST_TESSELATION_MULTIPLIER to a higher value. This should compile to a no-op otherwise. @@ -1024,6 +1047,7 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa _Path.push_back(center); return; } + IM_ASSERT(a_min <= a_max); // Note that we are adding a point at both a_min and a_max. // If you are trying to draw a full closed circle you don't want the overlapping points! @@ -1035,23 +1059,32 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa } } -ImVec2 ImBezierCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) +ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) +{ + float u = 1.0f - t; + float w1 = u * u * u; + float w2 = 3 * u * u * t; + float w3 = 3 * u * t * t; + float w4 = t * t * t; + return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x, w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y); +} + +ImVec2 ImBezierQuadraticCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, float t) { float u = 1.0f - t; - float w1 = u*u*u; - float w2 = 3*u*u*t; - float w3 = 3*u*t*t; - float w4 = t*t*t; - return ImVec2(w1*p1.x + w2*p2.x + w3*p3.x + w4*p4.x, w1*p1.y + w2*p2.y + w3*p3.y + w4*p4.y); + float w1 = u * u; + float w2 = 2 * u * t; + float w3 = t * t; + return ImVec2(w1 * p1.x + w2 * p2.x + w3 * p3.x, w1 * p1.y + w2 * p2.y + w3 * p3.y); } -// Closely mimics BezierClosestPointCasteljauStep() in imgui.cpp -static void PathBezierToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) +// Closely mimics ImBezierCubicClosestPointCasteljau() in imgui.cpp +static void PathBezierCubicCurveToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float tess_tol, int level) { float dx = x4 - x1; float dy = y4 - y1; - float d2 = ((x2 - x4) * dy - (y2 - y4) * dx); - float d3 = ((x3 - x4) * dy - (y3 - y4) * dx); + float d2 = (x2 - x4) * dy - (y2 - y4) * dx; + float d3 = (x3 - x4) * dy - (y3 - y4) * dx; d2 = (d2 >= 0) ? d2 : -d2; d3 = (d3 >= 0) ? d3 : -d3; if ((d2 + d3) * (d2 + d3) < tess_tol * (dx * dx + dy * dy)) @@ -1060,29 +1093,62 @@ static void PathBezierToCasteljau(ImVector* path, float x1, float y1, fl } else if (level < 10) { - float x12 = (x1 + x2)*0.5f, y12 = (y1 + y2)*0.5f; - float x23 = (x2 + x3)*0.5f, y23 = (y2 + y3)*0.5f; - float x34 = (x3 + x4)*0.5f, y34 = (y3 + y4)*0.5f; - float x123 = (x12 + x23)*0.5f, y123 = (y12 + y23)*0.5f; - float x234 = (x23 + x34)*0.5f, y234 = (y23 + y34)*0.5f; - float x1234 = (x123 + x234)*0.5f, y1234 = (y123 + y234)*0.5f; - PathBezierToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); - PathBezierToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); + float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; + float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; + float x34 = (x3 + x4) * 0.5f, y34 = (y3 + y4) * 0.5f; + float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; + float x234 = (x23 + x34) * 0.5f, y234 = (y23 + y34) * 0.5f; + float x1234 = (x123 + x234) * 0.5f, y1234 = (y123 + y234) * 0.5f; + PathBezierCubicCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, x1234, y1234, tess_tol, level + 1); + PathBezierCubicCurveToCasteljau(path, x1234, y1234, x234, y234, x34, y34, x4, y4, tess_tol, level + 1); + } +} + +static void PathBezierQuadraticCurveToCasteljau(ImVector* path, float x1, float y1, float x2, float y2, float x3, float y3, float tess_tol, int level) +{ + float dx = x3 - x1, dy = y3 - y1; + float det = (x2 - x3) * dy - (y2 - y3) * dx; + if (det * det * 4.0f < tess_tol * (dx * dx + dy * dy)) + { + path->push_back(ImVec2(x3, y3)); + } + else if (level < 10) + { + float x12 = (x1 + x2) * 0.5f, y12 = (y1 + y2) * 0.5f; + float x23 = (x2 + x3) * 0.5f, y23 = (y2 + y3) * 0.5f; + float x123 = (x12 + x23) * 0.5f, y123 = (y12 + y23) * 0.5f; + PathBezierQuadraticCurveToCasteljau(path, x1, y1, x12, y12, x123, y123, tess_tol, level + 1); + PathBezierQuadraticCurveToCasteljau(path, x123, y123, x23, y23, x3, y3, tess_tol, level + 1); } } -void ImDrawList::PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) +void ImDrawList::PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments) { ImVec2 p1 = _Path.back(); if (num_segments == 0) { - PathBezierToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); // Auto-tessellated + PathBezierCubicCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); // Auto-tessellated } else { float t_step = 1.0f / (float)num_segments; for (int i_step = 1; i_step <= num_segments; i_step++) - _Path.push_back(ImBezierCalc(p1, p2, p3, p4, t_step * i_step)); + _Path.push_back(ImBezierCubicCalc(p1, p2, p3, p4, t_step * i_step)); + } +} + +void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments) +{ + ImVec2 p1 = _Path.back(); + if (num_segments == 0) + { + PathBezierQuadraticCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, _Data->CurveTessellationTol, 0);// Auto-tessellated + } + else + { + float t_step = 1.0f / (float)num_segments; + for (int i_step = 1; i_step <= num_segments; i_step++) + _Path.push_back(ImBezierQuadraticCalc(p1, p2, p3, t_step * i_step)); } } @@ -1220,7 +1286,7 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu if (num_segments <= 0) { // Automatic segment count - const int radius_idx = (int)radius - 1; + const int radius_idx = (int)radius; if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value else @@ -1250,7 +1316,7 @@ void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, if (num_segments <= 0) { // Automatic segment count - const int radius_idx = (int)radius - 1; + const int radius_idx = (int)radius; if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value else @@ -1296,13 +1362,24 @@ void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, in } // Cubic Bezier takes 4 controls points -void ImDrawList::AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) +void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) { if ((col & IM_COL32_A_MASK) == 0) return; PathLineTo(p1); - PathBezierCurveTo(p2, p3, p4, num_segments); + PathBezierCubicCurveTo(p2, p3, p4, num_segments); + PathStroke(col, false, thickness); +} + +// Quadratic Bezier takes 3 controls points +void ImDrawList::AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + PathLineTo(p1); + PathBezierQuadraticCurveTo(p2, p3, num_segments); PathStroke(col, false, thickness); } @@ -1641,25 +1718,13 @@ void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int ve ImFontConfig::ImFontConfig() { - FontData = NULL; - FontDataSize = 0; + memset(this, 0, sizeof(*this)); FontDataOwnedByAtlas = true; - FontNo = 0; - SizePixels = 0.0f; OversampleH = 3; // FIXME: 2 may be a better default? OversampleV = 1; - PixelSnapH = false; - GlyphExtraSpacing = ImVec2(0.0f, 0.0f); - GlyphOffset = ImVec2(0.0f, 0.0f); - GlyphRanges = NULL; - GlyphMinAdvanceX = 0.0f; GlyphMaxAdvanceX = FLT_MAX; - MergeMode = false; - RasterizerFlags = 0x00; RasterizerMultiply = 1.0f; EllipsisChar = (ImWchar)-1; - memset(Name, 0, sizeof(Name)); - DstFont = NULL; } //----------------------------------------------------------------------------- @@ -1716,17 +1781,8 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3 ImFontAtlas::ImFontAtlas() { - Locked = false; - Flags = ImFontAtlasFlags_None; - TexID = (ImTextureID)NULL; - TexDesiredWidth = 0; + memset(this, 0, sizeof(*this)); TexGlyphPadding = 1; - - TexPixelsAlpha8 = NULL; - TexPixelsRGBA32 = NULL; - TexWidth = TexHeight = 0; - TexUvScale = ImVec2(0.0f, 0.0f); - TexUvWhitePixel = ImVec2(0.0f, 0.0f); PackIdMouseCursors = PackIdLines = -1; } @@ -2012,7 +2068,26 @@ bool ImFontAtlas::GetMouseCursorTexData(ImGuiMouseCursor cursor_type, ImVec2* ou bool ImFontAtlas::Build() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); - return ImFontAtlasBuildWithStbTruetype(this); + + // Select builder + // - Note that we do not reassign to atlas->FontBuilderIO, since it is likely to point to static data which + // may mess with some hot-reloading schemes. If you need to assign to this (for dynamic selection) AND are + // using a hot-reloading scheme that messes up static data, store your own instance of ImFontBuilderIO somewhere + // and point to it instead of pointing directly to return value of the GetBuilderXXX functions. + const ImFontBuilderIO* builder_io = FontBuilderIO; + if (builder_io == NULL) + { +#ifdef IMGUI_ENABLE_FREETYPE + builder_io = ImGuiFreeType::GetBuilderForFreeType(); +#elif defined(IMGUI_ENABLE_STB_TRUETYPE) + builder_io = ImFontAtlasGetBuilderForStbTruetype(); +#else + IM_ASSERT(0); // Invalid Build function +#endif + } + + // Build + return builder_io->FontBuilder_Build(this); } void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_brighten_factor) @@ -2032,6 +2107,7 @@ void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsig data[i] = table[data[i]]; } +#ifdef IMGUI_ENABLE_STB_TRUETYPE // Temporary data for one source font (multiple source fonts can be merged into one destination ImFont) // (C++03 doesn't allow instancing ImVector<> with function-local types so we declare the type here.) struct ImFontBuildSrcData @@ -2069,7 +2145,7 @@ static void UnpackBitVectorToFlatIndexList(const ImBitVector* in, ImVector* out->push_back((int)(((it - it_begin) << 5) + bit_n)); } -bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) +static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) { IM_ASSERT(atlas->ConfigData.Size > 0); @@ -2102,10 +2178,11 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) for (int output_i = 0; output_i < atlas->Fonts.Size && src_tmp.DstIndex == -1; output_i++) if (cfg.DstFont == atlas->Fonts[output_i]) src_tmp.DstIndex = output_i; - IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array? if (src_tmp.DstIndex == -1) + { + IM_ASSERT(src_tmp.DstIndex != -1); // cfg.DstFont not pointing within atlas->Fonts[] array? return false; - + } // Initialize helper structure for font loading and verify that the TTF/OTF data is correct const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo); IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."); @@ -2321,6 +2398,15 @@ bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) return true; } +const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype() +{ + static ImFontBuilderIO io; + io.FontBuilder_Build = ImFontAtlasBuildWithStbTruetype; + return &io; +} + +#endif // IMGUI_ENABLE_STB_TRUETYPE + void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) { if (!font_config->MergeMode) @@ -2363,7 +2449,7 @@ void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opa } } -void ImFontAtlasBuildRender1bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value) +void ImFontAtlasBuildRender8bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value) { IM_ASSERT(x >= 0 && x + w <= atlas->TexWidth); IM_ASSERT(y >= 0 && y + h <= atlas->TexHeight); @@ -2373,6 +2459,16 @@ void ImFontAtlasBuildRender1bppRectFromString(ImFontAtlas* atlas, int x, int y, out_pixel[off_x] = (in_str[off_x] == in_marker_char) ? in_marker_pixel_value : 0x00; } +void ImFontAtlasBuildRender32bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned int in_marker_pixel_value) +{ + IM_ASSERT(x >= 0 && x + w <= atlas->TexWidth); + IM_ASSERT(y >= 0 && y + h <= atlas->TexHeight); + unsigned int* out_pixel = atlas->TexPixelsRGBA32 + x + (y * atlas->TexWidth); + for (int off_y = 0; off_y < h; off_y++, out_pixel += atlas->TexWidth, in_str += w) + for (int off_x = 0; off_x < w; off_x++) + out_pixel[off_x] = (in_str[off_x] == in_marker_char) ? in_marker_pixel_value : IM_COL32_BLACK_TRANS; +} + static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) { ImFontAtlasCustomRect* r = atlas->GetCustomRectByIndex(atlas->PackIdMouseCursors); @@ -2385,15 +2481,30 @@ static void ImFontAtlasBuildRenderDefaultTexData(ImFontAtlas* atlas) IM_ASSERT(r->Width == FONT_ATLAS_DEFAULT_TEX_DATA_W * 2 + 1 && r->Height == FONT_ATLAS_DEFAULT_TEX_DATA_H); const int x_for_white = r->X; const int x_for_black = r->X + FONT_ATLAS_DEFAULT_TEX_DATA_W + 1; - ImFontAtlasBuildRender1bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', 0xFF); - ImFontAtlasBuildRender1bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', 0xFF); + if (atlas->TexPixelsAlpha8 != NULL) + { + ImFontAtlasBuildRender8bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', 0xFF); + ImFontAtlasBuildRender8bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', 0xFF); + } + else + { + ImFontAtlasBuildRender32bppRectFromString(atlas, x_for_white, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, '.', IM_COL32_WHITE); + ImFontAtlasBuildRender32bppRectFromString(atlas, x_for_black, r->Y, FONT_ATLAS_DEFAULT_TEX_DATA_W, FONT_ATLAS_DEFAULT_TEX_DATA_H, FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS, 'X', IM_COL32_WHITE); + } } else { // Render 4 white pixels IM_ASSERT(r->Width == 2 && r->Height == 2); const int offset = (int)r->X + (int)r->Y * w; - atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF; + if (atlas->TexPixelsAlpha8 != NULL) + { + atlas->TexPixelsAlpha8[offset] = atlas->TexPixelsAlpha8[offset + 1] = atlas->TexPixelsAlpha8[offset + w] = atlas->TexPixelsAlpha8[offset + w + 1] = 0xFF; + } + else + { + atlas->TexPixelsRGBA32[offset] = atlas->TexPixelsRGBA32[offset + 1] = atlas->TexPixelsRGBA32[offset + w] = atlas->TexPixelsRGBA32[offset + w + 1] = IM_COL32_WHITE; + } } atlas->TexUvWhitePixel = ImVec2((r->X + 0.5f) * atlas->TexUvScale.x, (r->Y + 0.5f) * atlas->TexUvScale.y); } @@ -2416,10 +2527,30 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) // Write each slice IM_ASSERT(pad_left + line_width + pad_right == r->Width && y < r->Height); // Make sure we're inside the texture bounds before we start writing pixels - unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)]; - memset(write_ptr, 0x00, pad_left); - memset(write_ptr + pad_left, 0xFF, line_width); - memset(write_ptr + pad_left + line_width, 0x00, pad_right); + if (atlas->TexPixelsAlpha8 != NULL) + { + unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)]; + for (unsigned int i = 0; i < pad_left; i++) + *(write_ptr + i) = 0x00; + + for (unsigned int i = 0; i < line_width; i++) + *(write_ptr + pad_left + i) = 0xFF; + + for (unsigned int i = 0; i < pad_right; i++) + *(write_ptr + pad_left + line_width + i) = 0x00; + } + else + { + unsigned int* write_ptr = &atlas->TexPixelsRGBA32[r->X + ((r->Y + y) * atlas->TexWidth)]; + for (unsigned int i = 0; i < pad_left; i++) + *(write_ptr + i) = IM_COL32_BLACK_TRANS; + + for (unsigned int i = 0; i < line_width; i++) + *(write_ptr + pad_left + i) = IM_COL32_WHITE; + + for (unsigned int i = 0; i < pad_right; i++) + *(write_ptr + pad_left + line_width + i) = IM_COL32_BLACK_TRANS; + } // Calculate UVs for this line ImVec2 uv0 = ImVec2((float)(r->X + pad_left - 1), (float)(r->Y + y)) * atlas->TexUvScale; @@ -2454,7 +2585,7 @@ void ImFontAtlasBuildInit(ImFontAtlas* atlas) void ImFontAtlasBuildFinish(ImFontAtlas* atlas) { // Render into our custom data blocks - IM_ASSERT(atlas->TexPixelsAlpha8 != NULL); + IM_ASSERT(atlas->TexPixelsAlpha8 != NULL || atlas->TexPixelsRGBA32 != NULL); ImFontAtlasBuildRenderDefaultTexData(atlas); ImFontAtlasBuildRenderLinesTexData(atlas); @@ -2616,45 +2747,76 @@ const ImWchar* ImFontAtlas::GetGlyphRangesChineseSimplifiedCommon() const ImWchar* ImFontAtlas::GetGlyphRangesJapanese() { - // 1946 common ideograms code points for Japanese - // Sourced from http://theinstructionlimit.com/common-kanji-character-ranges-for-xna-spritefont-rendering - // FIXME: Source a list of the revised 2136 Joyo Kanji list from 2010 and rebuild this. + // 2999 ideograms code points for Japanese + // - 2136 Joyo (meaning "for regular use" or "for common use") Kanji code points + // - 863 Jinmeiyo (meaning "for personal name") Kanji code points + // - Sourced from the character information database of the Information-technology Promotion Agency, Japan + // - https://mojikiban.ipa.go.jp/mji/ + // - Available under the terms of the Creative Commons Attribution-ShareAlike 2.1 Japan (CC BY-SA 2.1 JP). + // - https://creativecommons.org/licenses/by-sa/2.1/jp/deed.en + // - https://creativecommons.org/licenses/by-sa/2.1/jp/legalcode + // - You can generate this code by the script at: + // - https://github.com/vaiorabbit/everyday_use_kanji + // - References: + // - List of Joyo Kanji + // - (Official list by the Agency for Cultural Affairs) https://www.bunka.go.jp/kokugo_nihongo/sisaku/joho/joho/kakuki/14/tosin02/index.html + // - (Wikipedia) https://en.wikipedia.org/wiki/List_of_j%C5%8Dy%C5%8D_kanji + // - List of Jinmeiyo Kanji + // - (Official list by the Ministry of Justice) http://www.moj.go.jp/MINJI/minji86.html + // - (Wikipedia) https://en.wikipedia.org/wiki/Jinmeiy%C5%8D_kanji + // - Missing 1 Joyo Kanji: U+20B9F (Kun'yomi: Shikaru, On'yomi: Shitsu,shichi), see https://github.com/ocornut/imgui/pull/3627 for details. // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. // (Stored as accumulative offsets from the initial unicode codepoint 0x4E00. This encoding is designed to helps us compact the source code size.) static const short accumulative_offsets_from_0x4E00[] = { - 0,1,2,4,1,1,1,1,2,1,6,2,2,1,8,5,7,11,1,2,10,10,8,2,4,20,2,11,8,2,1,2,1,6,2,1,7,5,3,7,1,1,13,7,9,1,4,6,1,2,1,10,1,1,9,2,2,4,5,6,14,1,1,9,3,18, - 5,4,2,2,10,7,1,1,1,3,2,4,3,23,2,10,12,2,14,2,4,13,1,6,10,3,1,7,13,6,4,13,5,2,3,17,2,2,5,7,6,4,1,7,14,16,6,13,9,15,1,1,7,16,4,7,1,19,9,2,7,15, - 2,6,5,13,25,4,14,13,11,25,1,1,1,2,1,2,2,3,10,11,3,3,1,1,4,4,2,1,4,9,1,4,3,5,5,2,7,12,11,15,7,16,4,5,16,2,1,1,6,3,3,1,1,2,7,6,6,7,1,4,7,6,1,1, - 2,1,12,3,3,9,5,8,1,11,1,2,3,18,20,4,1,3,6,1,7,3,5,5,7,2,2,12,3,1,4,2,3,2,3,11,8,7,4,17,1,9,25,1,1,4,2,2,4,1,2,7,1,1,1,3,1,2,6,16,1,2,1,1,3,12, - 20,2,5,20,8,7,6,2,1,1,1,1,6,2,1,2,10,1,1,6,1,3,1,2,1,4,1,12,4,1,3,1,1,1,1,1,10,4,7,5,13,1,15,1,1,30,11,9,1,15,38,14,1,32,17,20,1,9,31,2,21,9, - 4,49,22,2,1,13,1,11,45,35,43,55,12,19,83,1,3,2,3,13,2,1,7,3,18,3,13,8,1,8,18,5,3,7,25,24,9,24,40,3,17,24,2,1,6,2,3,16,15,6,7,3,12,1,9,7,3,3, - 3,15,21,5,16,4,5,12,11,11,3,6,3,2,31,3,2,1,1,23,6,6,1,4,2,6,5,2,1,1,3,3,22,2,6,2,3,17,3,2,4,5,1,9,5,1,1,6,15,12,3,17,2,14,2,8,1,23,16,4,2,23, - 8,15,23,20,12,25,19,47,11,21,65,46,4,3,1,5,6,1,2,5,26,2,1,1,3,11,1,1,1,2,1,2,3,1,1,10,2,3,1,1,1,3,6,3,2,2,6,6,9,2,2,2,6,2,5,10,2,4,1,2,1,2,2, - 3,1,1,3,1,2,9,23,9,2,1,1,1,1,5,3,2,1,10,9,6,1,10,2,31,25,3,7,5,40,1,15,6,17,7,27,180,1,3,2,2,1,1,1,6,3,10,7,1,3,6,17,8,6,2,2,1,3,5,5,8,16,14, - 15,1,1,4,1,2,1,1,1,3,2,7,5,6,2,5,10,1,4,2,9,1,1,11,6,1,44,1,3,7,9,5,1,3,1,1,10,7,1,10,4,2,7,21,15,7,2,5,1,8,3,4,1,3,1,6,1,4,2,1,4,10,8,1,4,5, - 1,5,10,2,7,1,10,1,1,3,4,11,10,29,4,7,3,5,2,3,33,5,2,19,3,1,4,2,6,31,11,1,3,3,3,1,8,10,9,12,11,12,8,3,14,8,6,11,1,4,41,3,1,2,7,13,1,5,6,2,6,12, - 12,22,5,9,4,8,9,9,34,6,24,1,1,20,9,9,3,4,1,7,2,2,2,6,2,28,5,3,6,1,4,6,7,4,2,1,4,2,13,6,4,4,3,1,8,8,3,2,1,5,1,2,2,3,1,11,11,7,3,6,10,8,6,16,16, - 22,7,12,6,21,5,4,6,6,3,6,1,3,2,1,2,8,29,1,10,1,6,13,6,6,19,31,1,13,4,4,22,17,26,33,10,4,15,12,25,6,67,10,2,3,1,6,10,2,6,2,9,1,9,4,4,1,2,16,2, - 5,9,2,3,8,1,8,3,9,4,8,6,4,8,11,3,2,1,1,3,26,1,7,5,1,11,1,5,3,5,2,13,6,39,5,1,5,2,11,6,10,5,1,15,5,3,6,19,21,22,2,4,1,6,1,8,1,4,8,2,4,2,2,9,2, - 1,1,1,4,3,6,3,12,7,1,14,2,4,10,2,13,1,17,7,3,2,1,3,2,13,7,14,12,3,1,29,2,8,9,15,14,9,14,1,3,1,6,5,9,11,3,38,43,20,7,7,8,5,15,12,19,15,81,8,7, - 1,5,73,13,37,28,8,8,1,15,18,20,165,28,1,6,11,8,4,14,7,15,1,3,3,6,4,1,7,14,1,1,11,30,1,5,1,4,14,1,4,2,7,52,2,6,29,3,1,9,1,21,3,5,1,26,3,11,14, - 11,1,17,5,1,2,1,3,2,8,1,2,9,12,1,1,2,3,8,3,24,12,7,7,5,17,3,3,3,1,23,10,4,4,6,3,1,16,17,22,3,10,21,16,16,6,4,10,2,1,1,2,8,8,6,5,3,3,3,39,25, - 15,1,1,16,6,7,25,15,6,6,12,1,22,13,1,4,9,5,12,2,9,1,12,28,8,3,5,10,22,60,1,2,40,4,61,63,4,1,13,12,1,4,31,12,1,14,89,5,16,6,29,14,2,5,49,18,18, - 5,29,33,47,1,17,1,19,12,2,9,7,39,12,3,7,12,39,3,1,46,4,12,3,8,9,5,31,15,18,3,2,2,66,19,13,17,5,3,46,124,13,57,34,2,5,4,5,8,1,1,1,4,3,1,17,5, - 3,5,3,1,8,5,6,3,27,3,26,7,12,7,2,17,3,7,18,78,16,4,36,1,2,1,6,2,1,39,17,7,4,13,4,4,4,1,10,4,2,4,6,3,10,1,19,1,26,2,4,33,2,73,47,7,3,8,2,4,15, - 18,1,29,2,41,14,1,21,16,41,7,39,25,13,44,2,2,10,1,13,7,1,7,3,5,20,4,8,2,49,1,10,6,1,6,7,10,7,11,16,3,12,20,4,10,3,1,2,11,2,28,9,2,4,7,2,15,1, - 27,1,28,17,4,5,10,7,3,24,10,11,6,26,3,2,7,2,2,49,16,10,16,15,4,5,27,61,30,14,38,22,2,7,5,1,3,12,23,24,17,17,3,3,2,4,1,6,2,7,5,1,1,5,1,1,9,4, - 1,3,6,1,8,2,8,4,14,3,5,11,4,1,3,32,1,19,4,1,13,11,5,2,1,8,6,8,1,6,5,13,3,23,11,5,3,16,3,9,10,1,24,3,198,52,4,2,2,5,14,5,4,22,5,20,4,11,6,41, - 1,5,2,2,11,5,2,28,35,8,22,3,18,3,10,7,5,3,4,1,5,3,8,9,3,6,2,16,22,4,5,5,3,3,18,23,2,6,23,5,27,8,1,33,2,12,43,16,5,2,3,6,1,20,4,2,9,7,1,11,2, - 10,3,14,31,9,3,25,18,20,2,5,5,26,14,1,11,17,12,40,19,9,6,31,83,2,7,9,19,78,12,14,21,76,12,113,79,34,4,1,1,61,18,85,10,2,2,13,31,11,50,6,33,159, - 179,6,6,7,4,4,2,4,2,5,8,7,20,32,22,1,3,10,6,7,28,5,10,9,2,77,19,13,2,5,1,4,4,7,4,13,3,9,31,17,3,26,2,6,6,5,4,1,7,11,3,4,2,1,6,2,20,4,1,9,2,6, - 3,7,1,1,1,20,2,3,1,6,2,3,6,2,4,8,1,5,13,8,4,11,23,1,10,6,2,1,3,21,2,2,4,24,31,4,10,10,2,5,192,15,4,16,7,9,51,1,2,1,1,5,1,1,2,1,3,5,3,1,3,4,1, - 3,1,3,3,9,8,1,2,2,2,4,4,18,12,92,2,10,4,3,14,5,25,16,42,4,14,4,2,21,5,126,30,31,2,1,5,13,3,22,5,6,6,20,12,1,14,12,87,3,19,1,8,2,9,9,3,3,23,2, - 3,7,6,3,1,2,3,9,1,3,1,6,3,2,1,3,11,3,1,6,10,3,2,3,1,2,1,5,1,1,11,3,6,4,1,7,2,1,2,5,5,34,4,14,18,4,19,7,5,8,2,6,79,1,5,2,14,8,2,9,2,1,36,28,16, - 4,1,1,1,2,12,6,42,39,16,23,7,15,15,3,2,12,7,21,64,6,9,28,8,12,3,3,41,59,24,51,55,57,294,9,9,2,6,2,15,1,2,13,38,90,9,9,9,3,11,7,1,1,1,5,6,3,2, - 1,2,2,3,8,1,4,4,1,5,7,1,4,3,20,4,9,1,1,1,5,5,17,1,5,2,6,2,4,1,4,5,7,3,18,11,11,32,7,5,4,7,11,127,8,4,3,3,1,10,1,1,6,21,14,1,16,1,7,1,3,6,9,65, - 51,4,3,13,3,10,1,1,12,9,21,110,3,19,24,1,1,10,62,4,1,29,42,78,28,20,18,82,6,3,15,6,84,58,253,15,155,264,15,21,9,14,7,58,40,39, + 0,1,2,4,1,1,1,1,2,1,3,3,2,2,1,5,3,5,7,5,6,1,2,1,7,2,6,3,1,8,1,1,4,1,1,18,2,11,2,6,2,1,2,1,5,1,2,1,3,1,2,1,2,3,3,1,1,2,3,1,1,1,12,7,9,1,4,5,1, + 1,2,1,10,1,1,9,2,2,4,5,6,9,3,1,1,1,1,9,3,18,5,2,2,2,2,1,6,3,7,1,1,1,1,2,2,4,2,1,23,2,10,4,3,5,2,4,10,2,4,13,1,6,1,9,3,1,1,6,6,7,6,3,1,2,11,3, + 2,2,3,2,15,2,2,5,4,3,6,4,1,2,5,2,12,16,6,13,9,13,2,1,1,7,16,4,7,1,19,1,5,1,2,2,7,7,8,2,6,5,4,9,18,7,4,5,9,13,11,8,15,2,1,1,1,2,1,2,2,1,2,2,8, + 2,9,3,3,1,1,4,4,1,1,1,4,9,1,4,3,5,5,2,7,5,3,4,8,2,1,13,2,3,3,1,14,1,1,4,5,1,3,6,1,5,2,1,1,3,3,3,3,1,1,2,7,6,6,7,1,4,7,6,1,1,1,1,1,12,3,3,9,5, + 2,6,1,5,6,1,2,3,18,2,4,14,4,1,3,6,1,1,6,3,5,5,3,2,2,2,2,12,3,1,4,2,3,2,3,11,1,7,4,1,2,1,3,17,1,9,1,24,1,1,4,2,2,4,1,2,7,1,1,1,3,1,2,2,4,15,1, + 1,2,1,1,2,1,5,2,5,20,2,5,9,1,10,8,7,6,1,1,1,1,1,1,6,2,1,2,8,1,1,1,1,5,1,1,3,1,1,1,1,3,1,1,12,4,1,3,1,1,1,1,1,10,3,1,7,5,13,1,2,3,4,6,1,1,30, + 2,9,9,1,15,38,11,3,1,8,24,7,1,9,8,10,2,1,9,31,2,13,6,2,9,4,49,5,2,15,2,1,10,2,1,1,1,2,2,6,15,30,35,3,14,18,8,1,16,10,28,12,19,45,38,1,3,2,3, + 13,2,1,7,3,6,5,3,4,3,1,5,7,8,1,5,3,18,5,3,6,1,21,4,24,9,24,40,3,14,3,21,3,2,1,2,4,2,3,1,15,15,6,5,1,1,3,1,5,6,1,9,7,3,3,2,1,4,3,8,21,5,16,4, + 5,2,10,11,11,3,6,3,2,9,3,6,13,1,2,1,1,1,1,11,12,6,6,1,4,2,6,5,2,1,1,3,3,6,13,3,1,1,5,1,2,3,3,14,2,1,2,2,2,5,1,9,5,1,1,6,12,3,12,3,4,13,2,14, + 2,8,1,17,5,1,16,4,2,2,21,8,9,6,23,20,12,25,19,9,38,8,3,21,40,25,33,13,4,3,1,4,1,2,4,1,2,5,26,2,1,1,2,1,3,6,2,1,1,1,1,1,1,2,3,1,1,1,9,2,3,1,1, + 1,3,6,3,2,1,1,6,6,1,8,2,2,2,1,4,1,2,3,2,7,3,2,4,1,2,1,2,2,1,1,1,1,1,3,1,2,5,4,10,9,4,9,1,1,1,1,1,1,5,3,2,1,6,4,9,6,1,10,2,31,17,8,3,7,5,40,1, + 7,7,1,6,5,2,10,7,8,4,15,39,25,6,28,47,18,10,7,1,3,1,1,2,1,1,1,3,3,3,1,1,1,3,4,2,1,4,1,3,6,10,7,8,6,2,2,1,3,3,2,5,8,7,9,12,2,15,1,1,4,1,2,1,1, + 1,3,2,1,3,3,5,6,2,3,2,10,1,4,2,8,1,1,1,11,6,1,21,4,16,3,1,3,1,4,2,3,6,5,1,3,1,1,3,3,4,6,1,1,10,4,2,7,10,4,7,4,2,9,4,3,1,1,1,4,1,8,3,4,1,3,1, + 6,1,4,2,1,4,7,2,1,8,1,4,5,1,1,2,2,4,6,2,7,1,10,1,1,3,4,11,10,8,21,4,6,1,3,5,2,1,2,28,5,5,2,3,13,1,2,3,1,4,2,1,5,20,3,8,11,1,3,3,3,1,8,10,9,2, + 10,9,2,3,1,1,2,4,1,8,3,6,1,7,8,6,11,1,4,29,8,4,3,1,2,7,13,1,4,1,6,2,6,12,12,2,20,3,2,3,6,4,8,9,2,7,34,5,1,18,6,1,1,4,4,5,7,9,1,2,2,4,3,4,1,7, + 2,2,2,6,2,3,25,5,3,6,1,4,6,7,4,2,1,4,2,13,6,4,4,3,1,5,3,4,4,3,2,1,1,4,1,2,1,1,3,1,11,1,6,3,1,7,3,6,2,8,8,6,9,3,4,11,3,2,10,12,2,5,11,1,6,4,5, + 3,1,8,5,4,6,6,3,5,1,1,3,2,1,2,2,6,17,12,1,10,1,6,12,1,6,6,19,9,6,16,1,13,4,4,15,7,17,6,11,9,15,12,6,7,2,1,2,2,15,9,3,21,4,6,49,18,7,3,2,3,1, + 6,8,2,2,6,2,9,1,3,6,4,4,1,2,16,2,5,2,1,6,2,3,5,3,1,2,5,1,2,1,9,3,1,8,6,4,8,11,3,1,1,1,1,3,1,13,8,4,1,3,2,2,1,4,1,11,1,5,2,1,5,2,5,8,6,1,1,7, + 4,3,8,3,2,7,2,1,5,1,5,2,4,7,6,2,8,5,1,11,4,5,3,6,18,1,2,13,3,3,1,21,1,1,4,1,4,1,1,1,8,1,2,2,7,1,2,4,2,2,9,2,1,1,1,4,3,6,3,12,5,1,1,1,5,6,3,2, + 4,8,2,2,4,2,7,1,8,9,5,2,3,2,1,3,2,13,7,14,6,5,1,1,2,1,4,2,23,2,1,1,6,3,1,4,1,15,3,1,7,3,9,14,1,3,1,4,1,1,5,8,1,3,8,3,8,15,11,4,14,4,4,2,5,5, + 1,7,1,6,14,7,7,8,5,15,4,8,6,5,6,2,1,13,1,20,15,11,9,2,5,6,2,11,2,6,2,5,1,5,8,4,13,19,25,4,1,1,11,1,34,2,5,9,14,6,2,2,6,1,1,14,1,3,14,13,1,6, + 12,21,14,14,6,32,17,8,32,9,28,1,2,4,11,8,3,1,14,2,5,15,1,1,1,1,3,6,4,1,3,4,11,3,1,1,11,30,1,5,1,4,1,5,8,1,1,3,2,4,3,17,35,2,6,12,17,3,1,6,2, + 1,1,12,2,7,3,3,2,1,16,2,8,3,6,5,4,7,3,3,8,1,9,8,5,1,2,1,3,2,8,1,2,9,12,1,1,2,3,8,3,24,12,4,3,7,5,8,3,3,3,3,3,3,1,23,10,3,1,2,2,6,3,1,16,1,16, + 22,3,10,4,11,6,9,7,7,3,6,2,2,2,4,10,2,1,1,2,8,7,1,6,4,1,3,3,3,5,10,12,12,2,3,12,8,15,1,1,16,6,6,1,5,9,11,4,11,4,2,6,12,1,17,5,13,1,4,9,5,1,11, + 2,1,8,1,5,7,28,8,3,5,10,2,17,3,38,22,1,2,18,12,10,4,38,18,1,4,44,19,4,1,8,4,1,12,1,4,31,12,1,14,7,75,7,5,10,6,6,13,3,2,11,11,3,2,5,28,15,6,18, + 18,5,6,4,3,16,1,7,18,7,36,3,5,3,1,7,1,9,1,10,7,2,4,2,6,2,9,7,4,3,32,12,3,7,10,2,23,16,3,1,12,3,31,4,11,1,3,8,9,5,1,30,15,6,12,3,2,2,11,19,9, + 14,2,6,2,3,19,13,17,5,3,3,25,3,14,1,1,1,36,1,3,2,19,3,13,36,9,13,31,6,4,16,34,2,5,4,2,3,3,5,1,1,1,4,3,1,17,3,2,3,5,3,1,3,2,3,5,6,3,12,11,1,3, + 1,2,26,7,12,7,2,14,3,3,7,7,11,25,25,28,16,4,36,1,2,1,6,2,1,9,3,27,17,4,3,4,13,4,1,3,2,2,1,10,4,2,4,6,3,8,2,1,18,1,1,24,2,2,4,33,2,3,63,7,1,6, + 40,7,3,4,4,2,4,15,18,1,16,1,1,11,2,41,14,1,3,18,13,3,2,4,16,2,17,7,15,24,7,18,13,44,2,2,3,6,1,1,7,5,1,7,1,4,3,3,5,10,8,2,3,1,8,1,1,27,4,2,1, + 12,1,2,1,10,6,1,6,7,5,2,3,7,11,5,11,3,6,6,2,3,15,4,9,1,1,2,1,2,11,2,8,12,8,5,4,2,3,1,5,2,2,1,14,1,12,11,4,1,11,17,17,4,3,2,5,5,7,3,1,5,9,9,8, + 2,5,6,6,13,13,2,1,2,6,1,2,2,49,4,9,1,2,10,16,7,8,4,3,2,23,4,58,3,29,1,14,19,19,11,11,2,7,5,1,3,4,6,2,18,5,12,12,17,17,3,3,2,4,1,6,2,3,4,3,1, + 1,1,1,5,1,1,9,1,3,1,3,6,1,8,1,1,2,6,4,14,3,1,4,11,4,1,3,32,1,2,4,13,4,1,2,4,2,1,3,1,11,1,4,2,1,4,4,6,3,5,1,6,5,7,6,3,23,3,5,3,5,3,3,13,3,9,10, + 1,12,10,2,3,18,13,7,160,52,4,2,2,3,2,14,5,4,12,4,6,4,1,20,4,11,6,2,12,27,1,4,1,2,2,7,4,5,2,28,3,7,25,8,3,19,3,6,10,2,2,1,10,2,5,4,1,3,4,1,5, + 3,2,6,9,3,6,2,16,3,3,16,4,5,5,3,2,1,2,16,15,8,2,6,21,2,4,1,22,5,8,1,1,21,11,2,1,11,11,19,13,12,4,2,3,2,3,6,1,8,11,1,4,2,9,5,2,1,11,2,9,1,1,2, + 14,31,9,3,4,21,14,4,8,1,7,2,2,2,5,1,4,20,3,3,4,10,1,11,9,8,2,1,4,5,14,12,14,2,17,9,6,31,4,14,1,20,13,26,5,2,7,3,6,13,2,4,2,19,6,2,2,18,9,3,5, + 12,12,14,4,6,2,3,6,9,5,22,4,5,25,6,4,8,5,2,6,27,2,35,2,16,3,7,8,8,6,6,5,9,17,2,20,6,19,2,13,3,1,1,1,4,17,12,2,14,7,1,4,18,12,38,33,2,10,1,1, + 2,13,14,17,11,50,6,33,20,26,74,16,23,45,50,13,38,33,6,6,7,4,4,2,1,3,2,5,8,7,8,9,3,11,21,9,13,1,3,10,6,7,1,2,2,18,5,5,1,9,9,2,68,9,19,13,2,5, + 1,4,4,7,4,13,3,9,10,21,17,3,26,2,1,5,2,4,5,4,1,7,4,7,3,4,2,1,6,1,1,20,4,1,9,2,2,1,3,3,2,3,2,1,1,1,20,2,3,1,6,2,3,6,2,4,8,1,3,2,10,3,5,3,4,4, + 3,4,16,1,6,1,10,2,4,2,1,1,2,10,11,2,2,3,1,24,31,4,10,10,2,5,12,16,164,15,4,16,7,9,15,19,17,1,2,1,1,5,1,1,1,1,1,3,1,4,3,1,3,1,3,1,2,1,1,3,3,7, + 2,8,1,2,2,2,1,3,4,3,7,8,12,92,2,10,3,1,3,14,5,25,16,42,4,7,7,4,2,21,5,27,26,27,21,25,30,31,2,1,5,13,3,22,5,6,6,11,9,12,1,5,9,7,5,5,22,60,3,5, + 13,1,1,8,1,1,3,3,2,1,9,3,3,18,4,1,2,3,7,6,3,1,2,3,9,1,3,1,3,2,1,3,1,1,1,2,1,11,3,1,6,9,1,3,2,3,1,2,1,5,1,1,4,3,4,1,2,2,4,4,1,7,2,1,2,2,3,5,13, + 18,3,4,14,9,9,4,16,3,7,5,8,2,6,48,28,3,1,1,4,2,14,8,2,9,2,1,15,2,4,3,2,10,16,12,8,7,1,1,3,1,1,1,2,7,4,1,6,4,38,39,16,23,7,15,15,3,2,12,7,21, + 37,27,6,5,4,8,2,10,8,8,6,5,1,2,1,3,24,1,16,17,9,23,10,17,6,1,51,55,44,13,294,9,3,6,2,4,2,2,15,1,1,1,13,21,17,68,14,8,9,4,1,4,9,3,11,7,1,1,1, + 5,6,3,2,1,1,1,2,3,8,1,2,2,4,1,5,5,2,1,4,3,7,13,4,1,4,1,3,1,1,1,5,5,10,1,6,1,5,2,1,5,2,4,1,4,5,7,3,18,2,9,11,32,4,3,3,2,4,7,11,16,9,11,8,13,38, + 32,8,4,2,1,1,2,1,2,4,4,1,1,1,4,1,21,3,11,1,16,1,1,6,1,3,2,4,9,8,57,7,44,1,3,3,13,3,10,1,1,7,5,2,7,21,47,63,3,15,4,7,1,16,1,1,2,8,2,3,42,15,4, + 1,29,7,22,10,3,78,16,12,20,18,4,67,11,5,1,3,15,6,21,31,32,27,18,13,71,35,5,142,4,10,1,2,50,19,33,16,35,37,16,19,27,7,1,133,19,1,4,8,7,20,1,4, + 4,1,10,3,1,6,1,2,51,5,40,15,24,43,22928,11,1,13,154,70,3,1,1,7,4,10,1,2,1,1,2,1,2,1,2,2,1,1,2,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1, + 3,2,1,1,1,1,2,1,1, }; static ImWchar base_ranges[] = // not zero-terminated { @@ -2905,6 +3067,7 @@ void ImFont::AddGlyph(const ImFontConfig* cfg, ImWchar codepoint, float x0, floa ImFontGlyph& glyph = Glyphs.back(); glyph.Codepoint = (unsigned int)codepoint; glyph.Visible = (x0 != x1) && (y0 != y1); + glyph.Colored = false; glyph.X0 = x0; glyph.Y0 = y0; glyph.X1 = x1; @@ -3155,6 +3318,8 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col const ImFontGlyph* glyph = FindGlyph(c); if (!glyph || !glyph->Visible) return; + if (glyph->Colored) + col |= ~IM_COL32_A_MASK; float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; pos.x = IM_FLOOR(pos.x); pos.y = IM_FLOOR(pos.y); @@ -3217,6 +3382,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col ImDrawIdx* idx_write = draw_list->_IdxWritePtr; unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; + const ImU32 col_untinted = col | ~IM_COL32_A_MASK; + while (s < text_end) { if (word_wrap_enabled) @@ -3322,14 +3489,17 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col } } + // Support for untinted glyphs + ImU32 glyph_col = glyph->Colored ? col_untinted : col; + // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: { idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); - vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; - vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; - vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; - vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; + vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = glyph_col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; + vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = glyph_col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; + vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = glyph_col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; + vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = glyph_col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; vtx_write += 4; vtx_current_idx += 4; idx_write += 6; diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index 74a266e1..fbc167d6 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -1,5 +1,5 @@ // dear imgui: Platform Backend for GLFW -// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) +// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) // (Requires: GLFW 3.1+) @@ -231,6 +231,11 @@ bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks) return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan); } +bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks) +{ + return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Unknown); +} + void ImGui_ImplGlfw_Shutdown() { if (g_InstalledCallbacks) diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h index 6abb4056..018f1a1e 100644 --- a/imgui/imgui_impl_glfw.h +++ b/imgui/imgui_impl_glfw.h @@ -1,5 +1,5 @@ // dear imgui: Platform Backend for GLFW -// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..) +// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) // Implemented features: @@ -23,6 +23,7 @@ struct GLFWwindow; IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); +IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index c70cfd6c..f3b2a937 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -13,7 +13,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2020-10-23: OpenGL: Save and restore current GL_PRIMITIVE_RESTART state. +// 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. +// 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. // 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) // 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. // 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. @@ -249,6 +250,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); + glDisable(GL_STENCIL_TEST); glEnable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART if (g_GlVersion >= 310) @@ -259,8 +261,8 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid #endif // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) - bool clip_origin_lower_left = true; #if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) + bool clip_origin_lower_left = true; GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); if (current_clip_origin == GL_UPPER_LEFT) clip_origin_lower_left = false; @@ -273,7 +275,9 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; +#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left +#endif const float ortho_projection[4][4] = { { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, @@ -284,12 +288,12 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glUseProgram(g_ShaderHandle); glUniform1i(g_AttribLocationTex, 0); glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); - + #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER if (g_GlVersion >= 330) glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. #endif - + (void)vertex_array_object; #ifndef IMGUI_IMPL_OPENGL_ES2 glBindVertexArray(vertex_array_object); @@ -343,6 +347,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLboolean last_enable_blend = glIsEnabled(GL_BLEND); GLboolean last_enable_cull_face = glIsEnabled(GL_CULL_FACE); GLboolean last_enable_depth_test = glIsEnabled(GL_DEPTH_TEST); + GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART GLboolean last_enable_primitive_restart = (g_GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; @@ -431,6 +436,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); if (last_enable_cull_face) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); if (last_enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); + if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART if (g_GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } @@ -464,7 +470,7 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // Store our identifier - io.Fonts->TexID = (ImTextureID)(intptr_t)g_FontTexture; + io.Fonts->SetTexID((ImTextureID)(intptr_t)g_FontTexture); // Restore state glBindTexture(GL_TEXTURE_2D, last_texture); @@ -478,7 +484,7 @@ void ImGui_ImplOpenGL3_DestroyFontsTexture() { ImGuiIO& io = ImGui::GetIO(); glDeleteTextures(1, &g_FontTexture); - io.Fonts->TexID = 0; + io.Fonts->SetTexID(0); g_FontTexture = 0; } } diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index f3ab30e7..39caf6b6 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -29,7 +29,8 @@ Index of this file: // [SECTION] ImGuiWindowTempData, ImGuiWindow // [SECTION] Tab bar, Tab item support // [SECTION] Table support -// [SECTION] Internal API +// [SECTION] ImGui internal API +// [SECTION] ImFontAtlas internal API // [SECTION] Test Engine specific hooks (imgui_test_engine) */ @@ -83,6 +84,12 @@ Index of this file: #error Use IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS #endif +// Enable stb_truetype by default unless FreeType is enabled. +// You can compile with both by defining both IMGUI_ENABLE_FREETYPE and IMGUI_ENABLE_STB_TRUETYPE together. +#ifndef IMGUI_ENABLE_FREETYPE +#define IMGUI_ENABLE_STB_TRUETYPE +#endif + //----------------------------------------------------------------------------- // [SECTION] Forward declarations //----------------------------------------------------------------------------- @@ -111,6 +118,10 @@ struct ImGuiStackSizes; // Storage of stack sizes for debugging/asse struct ImGuiStyleMod; // Stacked style modifier, backup of modified data so we can restore it struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) +struct ImGuiTable; // Storage for a table +struct ImGuiTableColumn; // Storage for one column of a table +struct ImGuiTableSettings; // Storage for a table .ini settings +struct ImGuiTableColumnsSettings; // Storage for a column .ini settings struct ImGuiWindow; // Storage for one window struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame) struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session) @@ -174,7 +185,7 @@ namespace ImStb #define IMGUI_DEBUG_LOG_NAV(...) ((void)0) // Disable log // Static Asserts -#if (__cplusplus >= 201100) +#if (__cplusplus >= 201100) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201100) #define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") #else #define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] @@ -245,15 +256,16 @@ namespace ImStb // - Helper: ImRect // - Helper: ImBitArray // - Helper: ImBitVector +// - Helper: ImSpan<>, ImSpanAllocator<> // - Helper: ImPool<> // - Helper: ImChunkStream<> //----------------------------------------------------------------------------- // Helpers: Hashing -IMGUI_API ImU32 ImHashData(const void* data, size_t data_size, ImU32 seed = 0); -IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); +IMGUI_API ImGuiID ImHashData(const void* data, size_t data_size, ImU32 seed = 0); +IMGUI_API ImGuiID ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -static inline ImU32 ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] +static inline ImGuiID ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] #endif // Helpers: Sorting @@ -264,6 +276,7 @@ IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b); // Helpers: Bit manipulation static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; } +static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v & (v - 1)) == 0; } static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } // Helpers: String, Formatting @@ -390,9 +403,10 @@ static inline float ImLinearSweep(float current, float target, float speed) static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } // Helpers: Geometry -IMGUI_API ImVec2 ImBezierCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t); // Cubic Bezier -IMGUI_API ImVec2 ImBezierClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments); // For curves with explicit number of segments -IMGUI_API ImVec2 ImBezierClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol);// For auto-tessellated curves you can use tess_tol = style.CurveTessellationTol +IMGUI_API ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t); +IMGUI_API ImVec2 ImBezierCubicClosestPoint(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, int num_segments); // For curves with explicit number of segments +IMGUI_API ImVec2 ImBezierCubicClosestPointCasteljau(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& p, float tess_tol);// For auto-tessellated curves you can use tess_tol = style.CurveTessellationTol +IMGUI_API ImVec2 ImBezierQuadraticCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, float t); IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& p); IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); @@ -459,8 +473,9 @@ struct IMGUI_API ImRect inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } -inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) +inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) // Works on range [n..n2) { + n2--; while (n <= n2) { int a_mod = (n & 31); @@ -471,6 +486,21 @@ inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) } } +// Helper: ImBitArray class (wrapper over ImBitArray functions) +// Store 1-bit per value. NOT CLEARED by constructor. +template +struct IMGUI_API ImBitArray +{ + ImU32 Storage[(BITCOUNT + 31) >> 5]; + ImBitArray() { } + void ClearAllBits() { memset(Storage, 0, sizeof(Storage)); } + void SetAllBits() { memset(Storage, 255, sizeof(Storage)); } + bool TestBit(int n) const { IM_ASSERT(n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } + void SetBit(int n) { IM_ASSERT(n < BITCOUNT); ImBitArraySetBit(Storage, n); } + void ClearBit(int n) { IM_ASSERT(n < BITCOUNT); ImBitArrayClearBit(Storage, n); } + void SetBitRange(int n, int n2) { ImBitArraySetBitRange(Storage, n, n2); } // Works on range [n..n2) +}; + // Helper: ImBitVector // Store 1-bit per value. struct IMGUI_API ImBitVector @@ -483,6 +513,55 @@ struct IMGUI_API ImBitVector void ClearBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArrayClearBit(Storage.Data, n); } }; +// Helper: ImSpan<> +// Pointing to a span of data we don't own. +template +struct ImSpan +{ + T* Data; + T* DataEnd; + + // Constructors, destructor + inline ImSpan() { Data = DataEnd = NULL; } + inline ImSpan(T* data, int size) { Data = data; DataEnd = data + size; } + inline ImSpan(T* data, T* data_end) { Data = data; DataEnd = data_end; } + + inline void set(T* data, int size) { Data = data; DataEnd = data + size; } + inline void set(T* data, T* data_end) { Data = data; DataEnd = data_end; } + inline int size() const { return (int)(ptrdiff_t)(DataEnd - Data); } + inline int size_in_bytes() const { return (int)(ptrdiff_t)(DataEnd - Data) * (int)sizeof(T); } + inline T& operator[](int i) { T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; } + inline const T& operator[](int i) const { const T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; } + + inline T* begin() { return Data; } + inline const T* begin() const { return Data; } + inline T* end() { return DataEnd; } + inline const T* end() const { return DataEnd; } + + // Utilities + inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < DataEnd); const ptrdiff_t off = it - Data; return (int)off; } +}; + +// Helper: ImSpanAllocator<> +// Facilitate storing multiple chunks into a single large block (the "arena") +template +struct ImSpanAllocator +{ + char* BasePtr; + int TotalSize; + int CurrSpan; + int Offsets[CHUNKS]; + + ImSpanAllocator() { memset(this, 0, sizeof(*this)); } + inline void ReserveBytes(int n, size_t sz) { IM_ASSERT(n == CurrSpan && n < CHUNKS); IM_UNUSED(n); Offsets[CurrSpan++] = TotalSize; TotalSize += (int)sz; } + inline int GetArenaSizeInBytes() { return TotalSize; } + inline void SetArenaBasePtr(void* base_ptr) { BasePtr = (char*)base_ptr; } + inline void* GetSpanPtrBegin(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrSpan == CHUNKS); return (void*)(BasePtr + Offsets[n]); } + inline void* GetSpanPtrEnd(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrSpan == CHUNKS); return (n + 1 < CHUNKS) ? BasePtr + Offsets[n + 1] : (void*)(BasePtr + TotalSize); } + template + inline void GetSpan(int n, ImSpan* span) { span->set((T*)GetSpanPtrBegin(n), (T*)GetSpanPtrEnd(n)); } +}; + // Helper: ImPool<> // Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID over a dense/hot buffer, // Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. @@ -529,6 +608,8 @@ struct IMGUI_API ImChunkStream T* end() { return (T*)(void*)(Buf.Data + Buf.Size); } int offset_from_ptr(const T* p) { IM_ASSERT(p >= begin() && p < end()); const ptrdiff_t off = (const char*)p - Buf.Data; return (int)off; } T* ptr_from_offset(int off) { IM_ASSERT(off >= 4 && off < Buf.Size); return (T*)(void*)(Buf.Data + off); } + void swap(ImChunkStream& rhs) { rhs.Buf.swap(Buf); } + }; //----------------------------------------------------------------------------- @@ -536,6 +617,7 @@ struct IMGUI_API ImChunkStream //----------------------------------------------------------------------------- // ImDrawList: Helper function to calculate a circle's segment count given its radius and a "maximum error" value. +// FIXME: the minimum number of auto-segment may be undesirably high for very small radiuses (e.g. 1.0f) #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 12 #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp((int)((IM_PI * 2.0f) / ImAcos(((_RAD) - (_MAXERROR)) / (_RAD))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) @@ -559,7 +641,7 @@ struct IMGUI_API ImDrawListSharedData // [Internal] Lookup tables ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas - ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius (array index + 1) before we calculate it dynamically (to avoid calculation overhead) + ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius before we calculate it dynamically (to avoid calculation overhead) const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas ImDrawListSharedData(); @@ -570,8 +652,9 @@ struct ImDrawDataBuilder { ImVector Layers[2]; // Global layers for: regular, tooltip - void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } - void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } + void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } + void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } + int GetDrawListCount() const { int count = 0; for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) count += Layers[n].Size; return count; } IMGUI_API void FlattenIntoSingleLayer(); }; @@ -1064,9 +1147,26 @@ struct ImGuiOldColumns // [SECTION] Viewport support //----------------------------------------------------------------------------- -#ifdef IMGUI_HAS_VIEWPORT -// -#endif // #ifdef IMGUI_HAS_VIEWPORT +// ImGuiViewport Private/Internals fields (cardinal sin: we are using inheritance!) +// Every instance of ImGuiViewport is in fact a ImGuiViewportP. +struct ImGuiViewportP : public ImGuiViewport +{ + int DrawListsLastFrame[2]; // Last frame number the background (0) and foreground (1) draw lists were used + ImDrawList* DrawLists[2]; // Convenience background (0) and foreground (1) draw lists. We use them to draw software mouser cursor when io.MouseDrawCursor is set and to draw most debug overlays. + ImDrawData DrawDataP; + ImDrawDataBuilder DrawDataBuilder; + + ImVec2 WorkOffsetMin; // Work Area: Offset from Pos to top-left corner of Work Area. Generally (0,0) or (0,+main_menu_bar_height). Work Area is Full Area but without menu-bars/status-bars (so WorkArea always fit inside Pos/Size!) + ImVec2 WorkOffsetMax; // Work Area: Offset from Pos+Size to bottom-right corner of Work Area. Generally (0,0) or (0,-status_bar_height). + ImVec2 CurrWorkOffsetMin; // Work Area: Offset being built/increased during current frame + ImVec2 CurrWorkOffsetMax; // Work Area: Offset being built/decreased during current frame + + ImGuiViewportP() { DrawListsLastFrame[0] = DrawListsLastFrame[1] = -1; DrawLists[0] = DrawLists[1] = NULL; } + ~ImGuiViewportP() { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); } + ImRect GetMainRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } + ImRect GetWorkRect() const { return ImRect(WorkPos.x, WorkPos.y, WorkPos.x + WorkSize.x, WorkPos.y + WorkSize.y); } + void UpdateWorkRect() { WorkPos = ImVec2(Pos.x + WorkOffsetMin.x, Pos.y + WorkOffsetMin.y); WorkSize = ImVec2(ImMax(0.0f, Size.x - WorkOffsetMin.x + WorkOffsetMax.x), ImMax(0.0f, Size.y - WorkOffsetMin.y + WorkOffsetMax.y)); } +}; //----------------------------------------------------------------------------- // [SECTION] Settings support @@ -1148,10 +1248,11 @@ struct IMGUI_API ImGuiStackSizes //----------------------------------------------------------------------------- typedef void (*ImGuiContextHookCallback)(ImGuiContext* ctx, ImGuiContextHook* hook); -enum ImGuiContextHookType { ImGuiContextHookType_NewFramePre, ImGuiContextHookType_NewFramePost, ImGuiContextHookType_EndFramePre, ImGuiContextHookType_EndFramePost, ImGuiContextHookType_RenderPre, ImGuiContextHookType_RenderPost, ImGuiContextHookType_Shutdown }; +enum ImGuiContextHookType { ImGuiContextHookType_NewFramePre, ImGuiContextHookType_NewFramePost, ImGuiContextHookType_EndFramePre, ImGuiContextHookType_EndFramePost, ImGuiContextHookType_RenderPre, ImGuiContextHookType_RenderPost, ImGuiContextHookType_Shutdown, ImGuiContextHookType_PendingRemoval_ }; struct ImGuiContextHook { + ImGuiID HookId; // A unique ID assigned by AddContextHook() ImGuiContextHookType Type; ImGuiID Owner; ImGuiContextHookCallback Callback; @@ -1203,9 +1304,11 @@ struct ImGuiContext float WheelingWindowTimer; // Item/widgets state and tracking information - ImGuiID HoveredId; // Hovered widget + ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; bool HoveredIdAllowOverlap; + bool HoveredIdUsingMouseWheel; // Hovered widget will use mouse wheel. Blocks scrolling the underlying window. + bool HoveredIdPreviousFrameUsingMouseWheel; bool HoveredIdDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0. float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active @@ -1218,6 +1321,7 @@ struct ImGuiContext bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdHasBeenEditedThisFrame; + bool ActiveIdUsingMouseWheel; // Active widget will want to read mouse wheel. Blocks scrolling the underlying window. ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs. ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array. @@ -1246,6 +1350,9 @@ struct ImGuiContext ImVectorOpenPopupStack; // Which popups are open (persistent) ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + // Viewports + ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. + // Gamepad/keyboard Navigation ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' ImGuiID NavId; // Focused item for navigation @@ -1303,11 +1410,7 @@ struct ImGuiContext bool FocusTabPressed; // // Render - ImDrawData DrawData; // Main ImDrawData instance to pass render information to the user - ImDrawDataBuilder DrawDataBuilder; float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) - ImDrawList BackgroundDrawList; // First draw list to be rendered. - ImDrawList ForegroundDrawList; // Last draw list to be rendered. This is where we the render software mouse cursor (if io.MouseDrawCursor is set) and most debug overlays. ImGuiMouseCursor MouseCursor; // Drag and Drop @@ -1329,6 +1432,13 @@ struct ImGuiContext ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly, ImGuiPayload only holds pointer+size unsigned char DragDropPayloadBufLocal[16]; // Local buffer for small payloads + // Table + ImGuiTable* CurrentTable; + ImPool Tables; + ImVector CurrentTableStack; + ImVector TablesLastTimeActive; // Last used timestamp of each tables (SOA, for efficient GC) + ImVector DrawChannelsTempMergeBuffer; + // Tab bars ImGuiTabBar* CurrentTabBar; ImPool TabBars; @@ -1352,6 +1462,7 @@ struct ImGuiContext float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; + float TooltipSlowDelay; // Time before slow tooltips appears (FIXME: This is temporary until we merge in tooltip timer+priority work) ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once @@ -1366,13 +1477,17 @@ struct ImGuiContext ImGuiTextBuffer SettingsIniData; // In memory .ini settings ImVector SettingsHandlers; // List of .ini settings handlers ImChunkStream SettingsWindows; // ImGuiWindow .ini settings entries + ImChunkStream SettingsTables; // ImGuiTable .ini settings entries ImVector Hooks; // Hooks for extensions (e.g. test engine) + ImGuiID HookIdNext; // Next available HookId // Capture/Logging bool LogEnabled; // Currently capturing ImGuiLogType LogType; // Capture target ImFileHandle LogFile; // If != NULL log to stdout/ file ImGuiTextBuffer LogBuffer; // Accumulation buffer when log to clipboard. This is pointer so our GImGui static constructor doesn't call heap allocators. + const char* LogNextPrefix; + const char* LogNextSuffix; float LogLinePosY; bool LogLineFirstItem; int LogDepthRef; @@ -1393,7 +1508,7 @@ struct ImGuiContext int WantTextInputNextFrame; char TempBuffer[1024 * 3 + 1]; // Temporary text buffer - ImGuiContext(ImFontAtlas* shared_font_atlas) : BackgroundDrawList(&DrawListSharedData), ForegroundDrawList(&DrawListSharedData) + ImGuiContext(ImFontAtlas* shared_font_atlas) { Initialized = false; FontAtlasOwnedByContext = shared_font_atlas ? false : true; @@ -1420,6 +1535,7 @@ struct ImGuiContext HoveredId = HoveredIdPreviousFrame = 0; HoveredIdAllowOverlap = false; + HoveredIdUsingMouseWheel = HoveredIdPreviousFrameUsingMouseWheel = false; HoveredIdDisabled = false; HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; ActiveId = 0; @@ -1431,6 +1547,7 @@ struct ImGuiContext ActiveIdHasBeenPressedBefore = false; ActiveIdHasBeenEditedBefore = false; ActiveIdHasBeenEditedThisFrame = false; + ActiveIdUsingMouseWheel = false; ActiveIdUsingNavDirMask = 0x00; ActiveIdUsingNavInputMask = 0x00; ActiveIdUsingKeyInputMask = 0x00; @@ -1480,8 +1597,6 @@ struct ImGuiContext FocusTabPressed = false; DimBgRatio = 0.0f; - BackgroundDrawList._OwnerName = "##Background"; // Give it a name for debugging - ForegroundDrawList._OwnerName = "##Foreground"; // Give it a name for debugging MouseCursor = ImGuiMouseCursor_Arrow; DragDropActive = DragDropWithinSource = DragDropWithinTarget = false; @@ -1496,6 +1611,7 @@ struct ImGuiContext DragDropHoldJustPressedId = 0; memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); + CurrentTable = NULL; CurrentTabBar = NULL; LastValidMousePos = ImVec2(0.0f, 0.0f); @@ -1510,15 +1626,18 @@ struct ImGuiContext DragSpeedDefaultRatio = 1.0f / 100.0f; ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; + TooltipSlowDelay = 0.50f; PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); PlatformLocaleDecimalPoint = '.'; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; + HookIdNext = 0; LogEnabled = false; LogType = ImGuiLogType_None; + LogNextPrefix = LogNextSuffix = NULL; LogFile = NULL; LogLinePosY = FLT_MAX; LogLineFirstItem = false; @@ -1549,7 +1668,8 @@ struct IMGUI_API ImGuiWindowTempData ImVec2 CursorPos; // Current emitting position, in absolute coordinates. ImVec2 CursorPosPrevLine; ImVec2 CursorStartPos; // Initial position after Begin(), generally ~ window position + WindowPadding. - ImVec2 CursorMaxPos; // Used to implicitly calculate the size of our contents, always growing during the frame. Used to calculate window->ContentSize at the beginning of next frame + ImVec2 CursorMaxPos; // Used to implicitly calculate ContentSize at the beginning of next frame, for scrolling range and auto-resize. Always growing during the frame. + ImVec2 IdealMaxPos; // Used to implicitly calculate ContentSizeIdeal at the beginning of next frame, for auto-resize only. Always growing during the frame. ImVec2 CurrLineSize; ImVec2 PrevLineSize; float CurrLineTextBaseOffset; // Baseline offset (0.0f by default on a new line, generally == style.FramePadding.y when a framed item has been added). @@ -1581,6 +1701,7 @@ struct IMGUI_API ImGuiWindowTempData ImVector ChildWindows; ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state) ImGuiOldColumns* CurrentColumns; // Current columns set + int CurrentTableIdx; // Current table index (into g.Tables) ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() int FocusCounterRegular; // (Legacy Focus/Tabbing system) Sequential counter, start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) @@ -1589,10 +1710,10 @@ struct IMGUI_API ImGuiWindowTempData // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. ImGuiItemFlags ItemFlags; // == g.ItemFlagsStack.back() - float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window - float TextWrapPos; // == TextWrapPosStack.back() [empty == -1.0f] - ImVector ItemWidthStack; - ImVector TextWrapPosStack; + float ItemWidth; // Current item width (>0.0: width in pixels, <0.0: align xx pixels to the right of window). + float TextWrapPos; // Current text wrap pos. + ImVector ItemWidthStack; // Store item widths to restore (attention: .back() is not == ItemWidth) + ImVector TextWrapPosStack; // Store text wrap pos to restore (attention: .back() is not == TextWrapPos) ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting }; @@ -1606,6 +1727,7 @@ struct IMGUI_API ImGuiWindow ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) ImVec2 SizeFull; // Size when non collapsed ImVec2 ContentSize; // Size of contents/scrollable client area (calculated from the extents reach of the cursor) from previous frame. Does not include window decoration or window padding. + ImVec2 ContentSizeIdeal; ImVec2 ContentSizeExplicit; // Size of contents/scrollable client area explicitly request by the user via SetNextWindowContentSize(). ImVec2 WindowPadding; // Window padding at the time of Begin(). float WindowRounding; // Window rounding at the time of Begin(). May be clamped lower to avoid rendering artifacts with title bar, menu bar etc. @@ -1639,11 +1761,12 @@ struct IMGUI_API ImGuiWindow ImS8 AutoFitChildAxises; bool AutoFitOnlyGrows; ImGuiDir AutoPosLastDirection; - int HiddenFramesCanSkipItems; // Hide the window for N frames - int HiddenFramesCannotSkipItems; // Hide the window for N frames while allowing items to be submitted so we can measure their size - ImGuiCond SetWindowPosAllowFlags; // store acceptable condition flags for SetNextWindowPos() use. - ImGuiCond SetWindowSizeAllowFlags; // store acceptable condition flags for SetNextWindowSize() use. - ImGuiCond SetWindowCollapsedAllowFlags; // store acceptable condition flags for SetNextWindowCollapsed() use. + ImS8 HiddenFramesCanSkipItems; // Hide the window for N frames + ImS8 HiddenFramesCannotSkipItems; // Hide the window for N frames while allowing items to be submitted so we can measure their size + ImS8 HiddenFramesForRenderOnly; // Hide the window until frame N at Render() time only + ImGuiCond SetWindowPosAllowFlags : 8; // store acceptable condition flags for SetNextWindowPos() use. + ImGuiCond SetWindowSizeAllowFlags : 8; // store acceptable condition flags for SetNextWindowSize() use. + ImGuiCond SetWindowCollapsedAllowFlags : 8; // store acceptable condition flags for SetNextWindowCollapsed() use. ImVec2 SetWindowPosVal; // store window position when using a non-zero Pivot (position set needs to be processed when we know the window size) ImVec2 SetWindowPosPivot; // store window pivot for positioning. ImVec2(0, 0) when positioning from top-left corner; ImVec2(0.5f, 0.5f) for centering; ImVec2(1, 1) for bottom right. @@ -1805,11 +1928,243 @@ struct ImGuiTabBar //----------------------------------------------------------------------------- #ifdef IMGUI_HAS_TABLE -// + +#define IM_COL32_DISABLE IM_COL32(0,0,0,1) // Special sentinel code which cannot be used as a regular color. +#define IMGUI_TABLE_MAX_COLUMNS 64 // sizeof(ImU64) * 8. This is solely because we frequently encode columns set in a ImU64. +#define IMGUI_TABLE_MAX_DRAW_CHANNELS (4 + 64 * 2) // See TableSetupDrawChannels() + +// Our current column maximum is 64 but we may raise that in the future. +typedef ImS8 ImGuiTableColumnIdx; +typedef ImU8 ImGuiTableDrawChannelIdx; + +// [Internal] sizeof() ~ 104 +// We use the terminology "Enabled" to refer to a column that is not Hidden by user/api. +// We use the terminology "Clipped" to refer to a column that is out of sight because of scrolling/clipping. +// This is in contrast with some user-facing api such as IsItemVisible() / IsRectVisible() which use "Visible" to mean "not clipped". +struct ImGuiTableColumn +{ + ImGuiTableColumnFlags Flags; // Flags after some patching (not directly same as provided by user). See ImGuiTableColumnFlags_ + float WidthGiven; // Final/actual width visible == (MaxX - MinX), locked in TableUpdateLayout(). May be > WidthRequest to honor minimum width, may be < WidthRequest to honor shrinking columns down in tight space. + float MinX; // Absolute positions + float MaxX; + float WidthRequest; // Master width absolute value when !(Flags & _WidthStretch). When Stretch this is derived every frame from StretchWeight in TableUpdateLayout() + float WidthAuto; // Automatic width + float StretchWeight; // Master width weight when (Flags & _WidthStretch). Often around ~1.0f initially. + float InitStretchWeightOrWidth; // Value passed to TableSetupColumn(). For Width it is a content width (_without padding_). + ImRect ClipRect; // Clipping rectangle for the column + ImGuiID UserID; // Optional, value passed to TableSetupColumn() + float WorkMinX; // Contents region min ~(MinX + CellPaddingX + CellSpacingX1) == cursor start position when entering column + float WorkMaxX; // Contents region max ~(MaxX - CellPaddingX - CellSpacingX2) + float ItemWidth; // Current item width for the column, preserved across rows + float ContentMaxXFrozen; // Contents maximum position for frozen rows (apart from headers), from which we can infer content width. + float ContentMaxXUnfrozen; + float ContentMaxXHeadersUsed; // Contents maximum position for headers rows (regardless of freezing). TableHeader() automatically softclip itself + report ideal desired size, to avoid creating extraneous draw calls + float ContentMaxXHeadersIdeal; + ImS16 NameOffset; // Offset into parent ColumnsNames[] + ImGuiTableColumnIdx DisplayOrder; // Index within Table's IndexToDisplayOrder[] (column may be reordered by users) + ImGuiTableColumnIdx IndexWithinEnabledSet; // Index within enabled/visible set (<= IndexToDisplayOrder) + ImGuiTableColumnIdx PrevEnabledColumn; // Index of prev enabled/visible column within Columns[], -1 if first enabled/visible column + ImGuiTableColumnIdx NextEnabledColumn; // Index of next enabled/visible column within Columns[], -1 if last enabled/visible column + ImGuiTableColumnIdx SortOrder; // Index of this column within sort specs, -1 if not sorting on this column, 0 for single-sort, may be >0 on multi-sort + ImGuiTableDrawChannelIdx DrawChannelCurrent; // Index within DrawSplitter.Channels[] + ImGuiTableDrawChannelIdx DrawChannelFrozen; + ImGuiTableDrawChannelIdx DrawChannelUnfrozen; + bool IsEnabled; // Is the column not marked Hidden by the user? (even if off view, e.g. clipped by scrolling). + bool IsEnabledNextFrame; + bool IsVisibleX; // Is actually in view (e.g. overlapping the host window clipping rectangle, not scrolled). + bool IsVisibleY; + bool IsRequestOutput; // Return value for TableSetColumnIndex() / TableNextColumn(): whether we request user to output contents or not. + bool IsSkipItems; // Do we want item submissions to this column to be completely ignored (no layout will happen). + bool IsPreserveWidthAuto; + ImS8 NavLayerCurrent; // ImGuiNavLayer in 1 byte + ImU8 AutoFitQueue; // Queue of 8 values for the next 8 frames to request auto-fit + ImU8 CannotSkipItemsQueue; // Queue of 8 values for the next 8 frames to disable Clipped/SkipItem + ImU8 SortDirection : 2; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending + ImU8 SortDirectionsAvailCount : 2; // Number of available sort directions (0 to 3) + ImU8 SortDirectionsAvailMask : 4; // Mask of available sort directions (1-bit each) + ImU8 SortDirectionsAvailList; // Ordered of available sort directions (2-bits each) + + ImGuiTableColumn() + { + memset(this, 0, sizeof(*this)); + StretchWeight = WidthRequest = -1.0f; + NameOffset = -1; + DisplayOrder = IndexWithinEnabledSet = -1; + PrevEnabledColumn = NextEnabledColumn = -1; + SortOrder = -1; + SortDirection = ImGuiSortDirection_None; + DrawChannelCurrent = DrawChannelFrozen = DrawChannelUnfrozen = (ImU8)-1; + } +}; + +// Transient cell data stored per row. +// sizeof() ~ 6 +struct ImGuiTableCellData +{ + ImU32 BgColor; // Actual color + ImGuiTableColumnIdx Column; // Column number +}; + +// FIXME-TABLE: transient data could be stored in a per-stacked table structure: DrawSplitter, SortSpecs, incoming RowData +struct ImGuiTable +{ + ImGuiID ID; + ImGuiTableFlags Flags; + void* RawData; // Single allocation to hold Columns[], DisplayOrderToIndex[] and RowCellData[] + ImSpan Columns; // Point within RawData[] + ImSpan DisplayOrderToIndex; // Point within RawData[]. Store display order of columns (when not reordered, the values are 0...Count-1) + ImSpan RowCellData; // Point within RawData[]. Store cells background requests for current row. + ImU64 EnabledMaskByDisplayOrder; // Column DisplayOrder -> IsEnabled map + ImU64 EnabledMaskByIndex; // Column Index -> IsEnabled map (== not hidden by user/api) in a format adequate for iterating column without touching cold data + ImU64 VisibleMaskByIndex; // Column Index -> IsVisibleX|IsVisibleY map (== not hidden by user/api && not hidden by scrolling/cliprect) + ImU64 RequestOutputMaskByIndex; // Column Index -> IsVisible || AutoFit (== expect user to submit items) + ImGuiTableFlags SettingsLoadedFlags; // Which data were loaded from the .ini file (e.g. when order is not altered we won't save order) + int SettingsOffset; // Offset in g.SettingsTables + int LastFrameActive; + int ColumnsCount; // Number of columns declared in BeginTable() + int CurrentRow; + int CurrentColumn; + ImS16 InstanceCurrent; // Count of BeginTable() calls with same ID in the same frame (generally 0). This is a little bit similar to BeginCount for a window, but multiple table with same ID look are multiple tables, they are just synched. + ImS16 InstanceInteracted; // Mark which instance (generally 0) of the same ID is being interacted with + float RowPosY1; + float RowPosY2; + float RowMinHeight; // Height submitted to TableNextRow() + float RowTextBaseline; + float RowIndentOffsetX; + ImGuiTableRowFlags RowFlags : 16; // Current row flags, see ImGuiTableRowFlags_ + ImGuiTableRowFlags LastRowFlags : 16; + int RowBgColorCounter; // Counter for alternating background colors (can be fast-forwarded by e.g clipper), not same as CurrentRow because header rows typically don't increase this. + ImU32 RowBgColor[2]; // Background color override for current row. + ImU32 BorderColorStrong; + ImU32 BorderColorLight; + float BorderX1; + float BorderX2; + float HostIndentX; + float MinColumnWidth; + float OuterPaddingX; + float CellPaddingX; // Padding from each borders + float CellPaddingY; + float CellSpacingX1; // Spacing between non-bordered cells + float CellSpacingX2; + float LastOuterHeight; // Outer height from last frame + float LastFirstRowHeight; // Height of first row from last frame + float InnerWidth; // User value passed to BeginTable(), see comments at the top of BeginTable() for details. + float ColumnsGivenWidth; // Sum of current column width + float ColumnsAutoFitWidth; // Sum of ideal column width in order nothing to be clipped, used for auto-fitting and content width submission in outer window + float ResizedColumnNextWidth; + float ResizeLockMinContentsX2; // Lock minimum contents width while resizing down in order to not create feedback loops. But we allow growing the table. + float RefScale; // Reference scale to be able to rescale columns on font/dpi changes. + ImRect OuterRect; // Note: for non-scrolling table, OuterRect.Max.y is often FLT_MAX until EndTable(), unless a height has been specified in BeginTable(). + ImRect InnerRect; // InnerRect but without decoration. As with OuterRect, for non-scrolling tables, InnerRect.Max.y is + ImRect WorkRect; + ImRect InnerClipRect; + ImRect BgClipRect; // We use this to cpu-clip cell background color fill + ImRect Bg0ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG0/1 channel. This tends to be == OuterWindow->ClipRect at BeginTable() because output in BG0/BG1 is cpu-clipped + ImRect Bg2ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG2 channel. This tends to be a correct, tight-fit, because output to BG2 are done by widgets relying on regular ClipRect. + ImRect HostClipRect; // This is used to check if we can eventually merge our columns draw calls into the current draw call of the current window. + ImRect HostBackupWorkRect; // Backup of InnerWindow->WorkRect at the end of BeginTable() + ImRect HostBackupParentWorkRect; // Backup of InnerWindow->ParentWorkRect at the end of BeginTable() + ImRect HostBackupInnerClipRect; // Backup of InnerWindow->ClipRect during PushTableBackground()/PopTableBackground() + ImVec2 HostBackupPrevLineSize; // Backup of InnerWindow->DC.PrevLineSize at the end of BeginTable() + ImVec2 HostBackupCurrLineSize; // Backup of InnerWindow->DC.CurrLineSize at the end of BeginTable() + ImVec2 HostBackupCursorMaxPos; // Backup of InnerWindow->DC.CursorMaxPos at the end of BeginTable() + ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() + ImVec1 HostBackupColumnsOffset; // Backup of OuterWindow->DC.ColumnsOffset at the end of BeginTable() + float HostBackupItemWidth; // Backup of OuterWindow->DC.ItemWidth at the end of BeginTable() + int HostBackupItemWidthStackSize;// Backup of OuterWindow->DC.ItemWidthStack.Size at the end of BeginTable() + ImGuiWindow* OuterWindow; // Parent window for the table + ImGuiWindow* InnerWindow; // Window holding the table data (== OuterWindow or a child window) + ImGuiTextBuffer ColumnsNames; // Contiguous buffer holding columns names + ImDrawListSplitter DrawSplitter; // We carry our own ImDrawList splitter to allow recursion (FIXME: could be stored outside, worst case we need 1 splitter per recursing table) + ImGuiTableColumnSortSpecs SortSpecsSingle; + ImVector SortSpecsMulti; // FIXME-OPT: Using a small-vector pattern would work be good. + ImGuiTableSortSpecs SortSpecs; // Public facing sorts specs, this is what we return in TableGetSortSpecs() + ImGuiTableColumnIdx SortSpecsCount; + ImGuiTableColumnIdx ColumnsEnabledCount; // Number of enabled columns (<= ColumnsCount) + ImGuiTableColumnIdx ColumnsEnabledFixedCount; // Number of enabled columns (<= ColumnsCount) + ImGuiTableColumnIdx DeclColumnsCount; // Count calls to TableSetupColumn() + ImGuiTableColumnIdx HoveredColumnBody; // Index of column whose visible region is being hovered. Important: == ColumnsCount when hovering empty region after the right-most column! + ImGuiTableColumnIdx HoveredColumnBorder; // Index of column whose right-border is being hovered (for resizing). + ImGuiTableColumnIdx AutoFitSingleColumn; // Index of single column requesting auto-fit. + ImGuiTableColumnIdx ResizedColumn; // Index of column being resized. Reset when InstanceCurrent==0. + ImGuiTableColumnIdx LastResizedColumn; // Index of column being resized from previous frame. + ImGuiTableColumnIdx HeldHeaderColumn; // Index of column header being held. + ImGuiTableColumnIdx ReorderColumn; // Index of column being reordered. (not cleared) + ImGuiTableColumnIdx ReorderColumnDir; // -1 or +1 + ImGuiTableColumnIdx LeftMostStretchedColumn; // Index of left-most stretched column. + ImGuiTableColumnIdx RightMostStretchedColumn; // Index of right-most stretched column. + ImGuiTableColumnIdx RightMostEnabledColumn; // Index of right-most non-hidden column. + ImGuiTableColumnIdx ContextPopupColumn; // Column right-clicked on, of -1 if opening context menu from a neutral/empty spot + ImGuiTableColumnIdx FreezeRowsRequest; // Requested frozen rows count + ImGuiTableColumnIdx FreezeRowsCount; // Actual frozen row count (== FreezeRowsRequest, or == 0 when no scrolling offset) + ImGuiTableColumnIdx FreezeColumnsRequest; // Requested frozen columns count + ImGuiTableColumnIdx FreezeColumnsCount; // Actual frozen columns count (== FreezeColumnsRequest, or == 0 when no scrolling offset) + ImGuiTableColumnIdx RowCellDataCurrent; // Index of current RowCellData[] entry in current row + ImGuiTableDrawChannelIdx DummyDrawChannel; // Redirect non-visible columns here. + ImGuiTableDrawChannelIdx Bg2DrawChannelCurrent; // For Selectable() and other widgets drawing across columns after the freezing line. Index within DrawSplitter.Channels[] + ImGuiTableDrawChannelIdx Bg2DrawChannelUnfrozen; + bool IsLayoutLocked; // Set by TableUpdateLayout() which is called when beginning the first row. + bool IsInsideRow; // Set when inside TableBeginRow()/TableEndRow(). + bool IsInitializing; + bool IsSortSpecsDirty; + bool IsUsingHeaders; // Set when the first row had the ImGuiTableRowFlags_Headers flag. + bool IsContextPopupOpen; // Set when default context menu is open (also see: ContextPopupColumn, InstanceInteracted). + bool IsSettingsRequestLoad; + bool IsSettingsDirty; // Set when table settings have changed and needs to be reported into ImGuiTableSetttings data. + bool IsDefaultDisplayOrder; // Set when display order is unchanged from default (DisplayOrder contains 0...Count-1) + bool IsResetAllRequest; + bool IsResetDisplayOrderRequest; + bool IsUnfrozenRows; // Set when we got past the frozen row. + bool IsDefaultSizingPolicy; // Set if user didn't explicitly set a sizing policy in BeginTable() + bool MemoryCompacted; + bool HostSkipItems; // Backup of InnerWindow->SkipItem at the end of BeginTable(), because we will overwrite InnerWindow->SkipItem on a per-column basis + + IMGUI_API ImGuiTable() { memset(this, 0, sizeof(*this)); LastFrameActive = -1; } + IMGUI_API ~ImGuiTable() { IM_FREE(RawData); } +}; + +// sizeof() ~ 12 +struct ImGuiTableColumnSettings +{ + float WidthOrWeight; + ImGuiID UserID; + ImGuiTableColumnIdx Index; + ImGuiTableColumnIdx DisplayOrder; + ImGuiTableColumnIdx SortOrder; + ImU8 SortDirection : 2; + ImU8 IsEnabled : 1; // "Visible" in ini file + ImU8 IsStretch : 1; + + ImGuiTableColumnSettings() + { + WidthOrWeight = 0.0f; + UserID = 0; + Index = -1; + DisplayOrder = SortOrder = -1; + SortDirection = ImGuiSortDirection_None; + IsEnabled = 1; + IsStretch = 0; + } +}; + +// This is designed to be stored in a single ImChunkStream (1 header followed by N ImGuiTableColumnSettings, etc.) +struct ImGuiTableSettings +{ + ImGuiID ID; // Set to 0 to invalidate/delete the setting + ImGuiTableFlags SaveFlags; // Indicate data we want to save using the Resizable/Reorderable/Sortable/Hideable flags (could be using its own flags..) + float RefScale; // Reference scale to be able to rescale columns on font/dpi changes. + ImGuiTableColumnIdx ColumnsCount; + ImGuiTableColumnIdx ColumnsCountMax; // Maximum number of columns this settings instance can store, we can recycle a settings instance with lower number of columns but not higher + bool WantApply; // Set when loaded from .ini data (to enable merging/loading .ini data into an already running context) + + ImGuiTableSettings() { memset(this, 0, sizeof(*this)); } + ImGuiTableColumnSettings* GetColumnSettings() { return (ImGuiTableColumnSettings*)(this + 1); } +}; + #endif // #ifdef IMGUI_HAS_TABLE //----------------------------------------------------------------------------- -// [SECTION] Internal API +// [SECTION] ImGui internal API // No guarantee of forward compatibility here! //----------------------------------------------------------------------------- @@ -1825,7 +2180,7 @@ namespace ImGui IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id); IMGUI_API ImGuiWindow* FindWindowByName(const char* name); IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); - IMGUI_API ImVec2 CalcWindowExpectedSize(ImGuiWindow* window); + IMGUI_API ImVec2 CalcWindowNextAutoFitSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); IMGUI_API bool IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); @@ -1845,7 +2200,9 @@ namespace ImGui // Fonts, drawing IMGUI_API void SetCurrentFont(ImFont* font); inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; } - inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { IM_UNUSED(window); ImGuiContext& g = *GImGui; return &g.ForegroundDrawList; } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. + inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { IM_UNUSED(window); return GetForegroundDrawList(); } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. + IMGUI_API ImDrawList* GetBackgroundDrawList(ImGuiViewport* viewport); // get background draw list for the given viewport. this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. + IMGUI_API ImDrawList* GetForegroundDrawList(ImGuiViewport* viewport); // get foreground draw list for the given viewport. this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. // Init IMGUI_API void Initialize(ImGuiContext* context); @@ -1858,7 +2215,8 @@ namespace ImGui IMGUI_API void UpdateMouseMovingWindowEndFrame(); // Generic context hooks - IMGUI_API void AddContextHook(ImGuiContext* context, const ImGuiContextHook* hook); + IMGUI_API ImGuiID AddContextHook(ImGuiContext* context, const ImGuiContextHook* hook); + IMGUI_API void RemoveContextHook(ImGuiContext* context, ImGuiID hook_to_remove); IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type); // Settings @@ -1915,6 +2273,8 @@ namespace ImGui // Logging/Capture IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer + IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); + IMGUI_API void LogSetNextTextDecoration(const char* prefix, const char* suffix); // Popups, Modals, Tooltips IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); @@ -1951,6 +2311,7 @@ namespace ImGui // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. + IMGUI_API void SetItemUsingMouseWheel(); inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; } inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; } @@ -1977,6 +2338,56 @@ namespace ImGui IMGUI_API float GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm); IMGUI_API float GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset); + // Tables: Candidates for public API + IMGUI_API void TableOpenContextMenu(int column_n = -1); + IMGUI_API void TableSetColumnEnabled(int column_n, bool enabled); + IMGUI_API void TableSetColumnWidth(int column_n, float width); + IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs); + IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. + IMGUI_API float TableGetHeaderRowHeight(); + IMGUI_API void TablePushBackgroundChannel(); + IMGUI_API void TablePopBackgroundChannel(); + + // Tables: Internals + IMGUI_API ImGuiTable* TableFindByID(ImGuiID id); + IMGUI_API bool BeginTableEx(const char* name, ImGuiID id, int columns_count, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0, 0), float inner_width = 0.0f); + IMGUI_API void TableBeginInitMemory(ImGuiTable* table, int columns_count); + IMGUI_API void TableBeginApplyRequests(ImGuiTable* table); + IMGUI_API void TableSetupDrawChannels(ImGuiTable* table); + IMGUI_API void TableUpdateLayout(ImGuiTable* table); + IMGUI_API void TableUpdateBorders(ImGuiTable* table); + IMGUI_API void TableUpdateColumnsWeightFromWidth(ImGuiTable* table); + IMGUI_API void TableDrawBorders(ImGuiTable* table); + IMGUI_API void TableDrawContextMenu(ImGuiTable* table); + IMGUI_API void TableMergeDrawChannels(ImGuiTable* table); + IMGUI_API void TableSortSpecsSanitize(ImGuiTable* table); + IMGUI_API void TableSortSpecsBuild(ImGuiTable* table); + IMGUI_API ImGuiSortDirection TableGetColumnNextSortDirection(ImGuiTableColumn* column); + IMGUI_API void TableFixColumnSortDirection(ImGuiTable* table, ImGuiTableColumn* column); + IMGUI_API float TableGetColumnWidthAuto(ImGuiTable* table, ImGuiTableColumn* column); + IMGUI_API void TableBeginRow(ImGuiTable* table); + IMGUI_API void TableEndRow(ImGuiTable* table); + IMGUI_API void TableBeginCell(ImGuiTable* table, int column_n); + IMGUI_API void TableEndCell(ImGuiTable* table); + IMGUI_API ImRect TableGetCellBgRect(const ImGuiTable* table, int column_n); + IMGUI_API const char* TableGetColumnName(const ImGuiTable* table, int column_n); + IMGUI_API ImGuiID TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no = 0); + IMGUI_API float TableGetMaxColumnWidth(const ImGuiTable* table, int column_n); + IMGUI_API void TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n); + IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table); + IMGUI_API void TableRemove(ImGuiTable* table); + IMGUI_API void TableGcCompactTransientBuffers(ImGuiTable* table); + IMGUI_API void TableGcCompactSettings(); + + // Tables: Settings + IMGUI_API void TableLoadSettings(ImGuiTable* table); + IMGUI_API void TableSaveSettings(ImGuiTable* table); + IMGUI_API void TableResetSettings(ImGuiTable* table); + IMGUI_API ImGuiTableSettings* TableGetBoundSettings(ImGuiTable* table); + IMGUI_API void TableSettingsInstallHandler(ImGuiContext* context); + IMGUI_API ImGuiTableSettings* TableSettingsCreate(ImGuiID id, int columns_count); + IMGUI_API ImGuiTableSettings* TableSettingsFindByID(ImGuiID id); + // Tab Bars IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags); IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); @@ -2002,7 +2413,6 @@ namespace ImGui IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0); IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. - IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); // Render helpers (those functions don't access any ImGui state!) IMGUI_API void RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale = 1.0f); @@ -2032,6 +2442,8 @@ namespace ImGui IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowResizeID(ImGuiWindow* window, int n); // 0..3: corners, 4..7: borders IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); + IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); + IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); // Widgets low-level behaviors IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); @@ -2091,24 +2503,40 @@ namespace ImGui IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); - IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImGuiWindow* window, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); + IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); IMGUI_API void DebugNodeStorage(ImGuiStorage* storage, const char* label); IMGUI_API void DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label); + IMGUI_API void DebugNodeTable(ImGuiTable* table); + IMGUI_API void DebugNodeTableSettings(ImGuiTableSettings* settings); IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); + IMGUI_API void DebugNodeViewport(ImGuiViewportP* viewport); + IMGUI_API void DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb); } // namespace ImGui -// ImFontAtlas internals -IMGUI_API bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas); -IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); -IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); -IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); -IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); -IMGUI_API void ImFontAtlasBuildRender1bppRectFromString(ImFontAtlas* atlas, int atlas_x, int atlas_y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value); -IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); -IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); + +//----------------------------------------------------------------------------- +// [SECTION] ImFontAtlas internal API +//----------------------------------------------------------------------------- + +// This structure is likely to evolve as we add support for incremental atlas updates +struct ImFontBuilderIO +{ + bool (*FontBuilder_Build)(ImFontAtlas* atlas); +}; + +// Helper for font builder +IMGUI_API const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype(); +IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); +IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); +IMGUI_API void ImFontAtlasBuildFinish(ImFontAtlas* atlas); +IMGUI_API void ImFontAtlasBuildRender8bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned char in_marker_pixel_value); +IMGUI_API void ImFontAtlasBuildRender32bppRectFromString(ImFontAtlas* atlas, int x, int y, int w, int h, const char* in_str, char in_marker_char, unsigned int in_marker_pixel_value); +IMGUI_API void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], float in_multiply_factor); +IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride); //----------------------------------------------------------------------------- // [SECTION] Test Engine specific hooks (imgui_test_engine) diff --git a/imgui/imgui_tables.cpp b/imgui/imgui_tables.cpp new file mode 100644 index 00000000..61a84f9f --- /dev/null +++ b/imgui/imgui_tables.cpp @@ -0,0 +1,3950 @@ +// dear imgui, v1.81 +// (tables and columns code) + +/* + +Index of this file: + +// [SECTION] Commentary +// [SECTION] Header mess +// [SECTION] Tables: Main code +// [SECTION] Tables: Row changes +// [SECTION] Tables: Columns changes +// [SECTION] Tables: Columns width management +// [SECTION] Tables: Drawing +// [SECTION] Tables: Sorting +// [SECTION] Tables: Headers +// [SECTION] Tables: Context Menu +// [SECTION] Tables: Settings (.ini data) +// [SECTION] Tables: Garbage Collection +// [SECTION] Tables: Debugging +// [SECTION] Columns, BeginColumns, EndColumns, etc. + +*/ + +// Navigating this file: +// - In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. + +//----------------------------------------------------------------------------- +// [SECTION] Commentary +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// Typical tables call flow: (root level is generally public API): +//----------------------------------------------------------------------------- +// - BeginTable() user begin into a table +// | BeginChild() - (if ScrollX/ScrollY is set) +// | TableBeginInitMemory() - first time table is used +// | TableResetSettings() - on settings reset +// | TableLoadSettings() - on settings load +// | TableBeginApplyRequests() - apply queued resizing/reordering/hiding requests +// | - TableSetColumnWidth() - apply resizing width (for mouse resize, often requested by previous frame) +// | - TableUpdateColumnsWeightFromWidth()- recompute columns weights (of stretch columns) from their respective width +// - TableSetupColumn() user submit columns details (optional) +// - TableSetupScrollFreeze() user submit scroll freeze information (optional) +//----------------------------------------------------------------------------- +// - TableUpdateLayout() [Internal] followup to BeginTable(): setup everything: widths, columns positions, clipping rectangles. Automatically called by the FIRST call to TableNextRow() or TableHeadersRow(). +// | TableSetupDrawChannels() - setup ImDrawList channels +// | TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission +// | TableDrawContextMenu() - draw right-click context menu +//----------------------------------------------------------------------------- +// - TableHeadersRow() or TableHeader() user submit a headers row (optional) +// | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction +// | TableOpenContextMenu() - when right-clicked: trigger opening of the default context menu +// - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers) +// - TableNextRow() user begin into a new row (also automatically called by TableHeadersRow()) +// | TableEndRow() - finish existing row +// | TableBeginRow() - add a new row +// - TableSetColumnIndex() / TableNextColumn() user begin into a cell +// | TableEndCell() - close existing column/cell +// | TableBeginCell() - enter into current column/cell +// - [...] user emit contents +//----------------------------------------------------------------------------- +// - EndTable() user ends the table +// | TableDrawBorders() - draw outer borders, inner vertical borders +// | TableMergeDrawChannels() - merge draw channels if clipping isn't required +// | EndChild() - (if ScrollX/ScrollY is set) +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// TABLE SIZING +//----------------------------------------------------------------------------- +// (Read carefully because this is subtle but it does make sense!) +//----------------------------------------------------------------------------- +// About 'outer_size': +// Its meaning needs to differ slightly depending of if we are using ScrollX/ScrollY flags. +// Default value is ImVec2(0.0f, 0.0f). +// X +// - outer_size.x <= 0.0f -> Right-align from window/work-rect right-most edge. With -FLT_MIN or 0.0f will align exactly on right-most edge. +// - outer_size.x > 0.0f -> Set Fixed width. +// Y with ScrollX/ScrollY disabled: we output table directly in current window +// - outer_size.y < 0.0f -> Bottom-align (but will auto extend, unless _NoHostExtendY is set). Not meaningful is parent window can vertically scroll. +// - outer_size.y = 0.0f -> No minimum height (but will auto extend, unless _NoHostExtendY is set) +// - outer_size.y > 0.0f -> Set Minimum height (but will auto extend, unless _NoHostExtenY is set) +// Y with ScrollX/ScrollY enabled: using a child window for scrolling +// - outer_size.y < 0.0f -> Bottom-align. Not meaningful is parent window can vertically scroll. +// - outer_size.y = 0.0f -> Bottom-align, consistent with BeginChild(). Not recommended unless table is last item in parent window. +// - outer_size.y > 0.0f -> Set Exact height. Recommended when using Scrolling on any axis. +//----------------------------------------------------------------------------- +// Outer size is also affected by the NoHostExtendX/NoHostExtendY flags. +// Important to that note how the two flags have slightly different behaviors! +// - ImGuiTableFlags_NoHostExtendX -> Make outer width auto-fit to columns (overriding outer_size.x value). Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. +// - ImGuiTableFlags_NoHostExtendY -> Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible. +// In theory ImGuiTableFlags_NoHostExtendY could be the default and any non-scrolling tables with outer_size.y != 0.0f would use exact height. +// This would be consistent but perhaps less useful and more confusing (as vertically clipped items are not easily noticeable) +//----------------------------------------------------------------------------- +// About 'inner_width': +// With ScrollX disabled: +// - inner_width -> *ignored* +// With ScrollX enabled: +// - inner_width < 0.0f -> *illegal* fit in known width (right align from outer_size.x) <-- weird +// - inner_width = 0.0f -> fit in outer_width: Fixed size columns will take space they need (if avail, otherwise shrink down), Stretch columns becomes Fixed columns. +// - inner_width > 0.0f -> override scrolling width, generally to be larger than outer_size.x. Fixed column take space they need (if avail, otherwise shrink down), Stretch columns share remaining space! +//----------------------------------------------------------------------------- +// Details: +// - If you want to use Stretch columns with ScrollX, you generally need to specify 'inner_width' otherwise the concept +// of "available space" doesn't make sense. +// - Even if not really useful, we allow 'inner_width < outer_size.x' for consistency and to facilitate understanding +// of what the value does. +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// COLUMNS SIZING POLICIES +//----------------------------------------------------------------------------- +// About overriding column sizing policy and width/weight with TableSetupColumn(): +// We use a default parameter of 'init_width_or_weight == -1'. +// - with ImGuiTableColumnFlags_WidthFixed, init_width <= 0 (default) --> width is automatic +// - with ImGuiTableColumnFlags_WidthFixed, init_width > 0 (explicit) --> width is custom +// - with ImGuiTableColumnFlags_WidthStretch, init_weight <= 0 (default) --> weight is 1.0f +// - with ImGuiTableColumnFlags_WidthStretch, init_weight > 0 (explicit) --> weight is custom +// Widths are specified _without_ CellPadding. If you specify a width of 100.0f, the column will be cover (100.0f + Padding * 2.0f) +// and you can fit a 100.0f wide item in it without clipping and with full padding. +//----------------------------------------------------------------------------- +// About default sizing policy (if you don't specify a ImGuiTableColumnFlags_WidthXXXX flag) +// - with Table policy ImGuiTableFlags_SizingFixedFit --> default Column policy is ImGuiTableColumnFlags_WidthFixed, default Width is equal to contents width +// - with Table policy ImGuiTableFlags_SizingFixedSame --> default Column policy is ImGuiTableColumnFlags_WidthFixed, default Width is max of all contents width +// - with Table policy ImGuiTableFlags_SizingStretchSame --> default Column policy is ImGuiTableColumnFlags_WidthStretch, default Weight is 1.0f +// - with Table policy ImGuiTableFlags_SizingStretchWeight --> default Column policy is ImGuiTableColumnFlags_WidthStretch, default Weight is proportional to contents +// Default Width and default Weight can be overridden when calling TableSetupColumn(). +//----------------------------------------------------------------------------- +// About mixing Fixed/Auto and Stretch columns together: +// - the typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. +// - using mixed policies with ScrollX does not make much sense, as using Stretch columns with ScrollX does not make much sense in the first place! +// that is, unless 'inner_width' is passed to BeginTable() to explicitly provide a total width to layout columns in. +// - when using ImGuiTableFlags_SizingFixedSame with mixed columns, only the Fixed/Auto columns will match their widths to the maximum contents width. +// - when using ImGuiTableFlags_SizingStretchSame with mixed columns, only the Stretch columns will match their weight/widths. +//----------------------------------------------------------------------------- +// About using column width: +// If a column is manual resizable or has a width specified with TableSetupColumn(): +// - you may use GetContentRegionAvail().x to query the width available in a given column. +// - right-side alignment features such as SetNextItemWidth(-x) or PushItemWidth(-x) will rely on this width. +// If the column is not resizable and has no width specified with TableSetupColumn(): +// - its width will be automatic and be the set to the max of items submitted. +// - therefore you generally cannot have ALL items of the columns use e.g. SetNextItemWidth(-FLT_MIN). +// - but if the column has one or more item of known/fixed size, this will become the reference width used by SetNextItemWidth(-FLT_MIN). +//----------------------------------------------------------------------------- + + +//----------------------------------------------------------------------------- +// TABLES CLIPPING/CULLING +//----------------------------------------------------------------------------- +// About clipping/culling of Rows in Tables: +// - For large numbers of rows, it is recommended you use ImGuiListClipper to only submit visible rows. +// ImGuiListClipper is reliant on the fact that rows are of equal height. +// See 'Demo->Tables->Vertical Scrolling' or 'Demo->Tables->Advanced' for a demo of using the clipper. +// - Note that auto-resizing columns don't play well with using the clipper. +// By default a table with _ScrollX but without _Resizable will have column auto-resize. +// So, if you want to use the clipper, make sure to either enable _Resizable, either setup columns width explicitly with _WidthFixed. +//----------------------------------------------------------------------------- +// About clipping/culling of Columns in Tables: +// - Both TableSetColumnIndex() and TableNextColumn() return true when the column is visible or performing +// width measurements. Otherwise, you may skip submitting the contents of a cell/column, BUT ONLY if you know +// it is not going to contribute to row height. +// In many situations, you may skip submitting contents for every columns but one (e.g. the first one). +// - Case A: column is not hidden by user, and at least partially in sight (most common case). +// - Case B: column is clipped / out of sight (because of scrolling or parent ClipRect): TableNextColumn() return false as a hint but we still allow layout output. +// - Case C: column is hidden explicitly by the user (e.g. via the context menu, or _DefaultHide column flag, etc.). +// +// [A] [B] [C] +// TableNextColumn(): true false false -> [userland] when TableNextColumn() / TableSetColumnIndex() return false, user can skip submitting items but only if the column doesn't contribute to row height. +// SkipItems: false false true -> [internal] when SkipItems is true, most widgets will early out if submitted, resulting is no layout output. +// ClipRect: normal zero-width zero-width -> [internal] when ClipRect is zero, ItemAdd() will return false and most widgets will early out mid-way. +// ImDrawList output: normal dummy dummy -> [internal] when using the dummy channel, ImDrawList submissions (if any) will be wasted (because cliprect is zero-width anyway). +// +// - We need distinguish those cases because non-hidden columns that are clipped outside of scrolling bounds should still contribute their height to the row. +// However, in the majority of cases, the contribution to row height is the same for all columns, or the tallest cells are known by the programmer. +//----------------------------------------------------------------------------- +// About clipping/culling of whole Tables: +// - Scrolling tables with a known outer size can be clipped earlier as BeginTable() will return false. +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +// [SECTION] Header mess +//----------------------------------------------------------------------------- + +#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "imgui.h" +#ifndef IMGUI_DISABLE + +#ifndef IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_MATH_OPERATORS +#endif +#include "imgui_internal.h" + +// System includes +#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier +#include // intptr_t +#else +#include // intptr_t +#endif + +// Visual Studio warnings +#ifdef _MSC_VER +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later +#pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types +#endif +#endif + +// Clang/GCC warnings with -Weverything +#if defined(__clang__) +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! +#endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. +#pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 +#pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. +#pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') +#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#elif defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#endif + +//----------------------------------------------------------------------------- +// [SECTION] Tables: Main code +//----------------------------------------------------------------------------- + +// Configuration +static const int TABLE_DRAW_CHANNEL_BG0 = 0; +static const int TABLE_DRAW_CHANNEL_BG2_FROZEN = 1; +static const int TABLE_DRAW_CHANNEL_NOCLIP = 2; // When using ImGuiTableFlags_NoClip (this becomes the last visible channel) +static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded because of clipping assumptions with outer borders rendering. +static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders. +static const float TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER = 0.06f; // Delay/timer before making the hover feedback (color+cursor) visible because tables/columns tends to be more cramped. + +// Helper +inline ImGuiTableFlags TableFixFlags(ImGuiTableFlags flags, ImGuiWindow* outer_window) +{ + // Adjust flags: set default sizing policy + if ((flags & ImGuiTableFlags_SizingMask_) == 0) + flags |= ((flags & ImGuiTableFlags_ScrollX) || (outer_window->Flags & ImGuiWindowFlags_AlwaysAutoResize)) ? ImGuiTableFlags_SizingFixedFit : ImGuiTableFlags_SizingStretchSame; + + // Adjust flags: enable NoKeepColumnsVisible when using ImGuiTableFlags_SizingFixedSame + if ((flags & ImGuiTableFlags_SizingMask_) == ImGuiTableFlags_SizingFixedSame) + flags |= ImGuiTableFlags_NoKeepColumnsVisible; + + // Adjust flags: enforce borders when resizable + if (flags & ImGuiTableFlags_Resizable) + flags |= ImGuiTableFlags_BordersInnerV; + + // Adjust flags: disable NoHostExtendX/NoHostExtendY if we have any scrolling going on + if (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) + flags &= ~(ImGuiTableFlags_NoHostExtendX | ImGuiTableFlags_NoHostExtendY); + + // Adjust flags: NoBordersInBodyUntilResize takes priority over NoBordersInBody + if (flags & ImGuiTableFlags_NoBordersInBodyUntilResize) + flags &= ~ImGuiTableFlags_NoBordersInBody; + + // Adjust flags: disable saved settings if there's nothing to save + if ((flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Sortable)) == 0) + flags |= ImGuiTableFlags_NoSavedSettings; + + // Inherit _NoSavedSettings from top-level window (child windows always have _NoSavedSettings set) +#ifdef IMGUI_HAS_DOCK + ImGuiWindow* window_for_settings = outer_window->RootWindowDockStop; +#else + ImGuiWindow* window_for_settings = outer_window->RootWindow; +#endif + if (window_for_settings->Flags & ImGuiWindowFlags_NoSavedSettings) + flags |= ImGuiTableFlags_NoSavedSettings; + + return flags; +} + +ImGuiTable* ImGui::TableFindByID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + return g.Tables.GetByKey(id); +} + +// Read about "TABLE SIZING" at the top of this file. +bool ImGui::BeginTable(const char* str_id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width) +{ + ImGuiID id = GetID(str_id); + return BeginTableEx(str_id, id, columns_count, flags, outer_size, inner_width); +} + +bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImGuiTableFlags flags, const ImVec2& outer_size, float inner_width) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* outer_window = GetCurrentWindow(); + if (outer_window->SkipItems) // Consistent with other tables + beneficial side effect that assert on miscalling EndTable() will be more visible. + return false; + + // Sanity checks + IM_ASSERT(columns_count > 0 && columns_count <= IMGUI_TABLE_MAX_COLUMNS && "Only 1..64 columns allowed!"); + if (flags & ImGuiTableFlags_ScrollX) + IM_ASSERT(inner_width >= 0.0f); + + // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping rules may evolve. + const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0; + const ImVec2 avail_size = GetContentRegionAvail(); + ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); + ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size); + if (use_child_window && IsClippedEx(outer_rect, 0, false)) + { + ItemSize(outer_rect); + return false; + } + + // Acquire storage for the table + ImGuiTable* table = g.Tables.GetOrAddByKey(id); + const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceCurrent + 1; + const ImGuiID instance_id = id + instance_no; + const ImGuiTableFlags table_last_flags = table->Flags; + if (instance_no > 0) + IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID"); + + // Fix flags + table->IsDefaultSizingPolicy = (flags & ImGuiTableFlags_SizingMask_) == 0; + flags = TableFixFlags(flags, outer_window); + + // Initialize + table->ID = id; + table->Flags = flags; + table->InstanceCurrent = (ImS16)instance_no; + table->LastFrameActive = g.FrameCount; + table->OuterWindow = table->InnerWindow = outer_window; + table->ColumnsCount = columns_count; + table->IsLayoutLocked = false; + table->InnerWidth = inner_width; + table->UserOuterSize = outer_size; + + // When not using a child window, WorkRect.Max will grow as we append contents. + if (use_child_window) + { + // Ensure no vertical scrollbar appears if we only want horizontal one, to make flag consistent + // (we have no other way to disable vertical scrollbar of a window while keeping the horizontal one showing) + ImVec2 override_content_size(FLT_MAX, FLT_MAX); + if ((flags & ImGuiTableFlags_ScrollX) && !(flags & ImGuiTableFlags_ScrollY)) + override_content_size.y = FLT_MIN; + + // Ensure specified width (when not specified, Stretched columns will act as if the width == OuterWidth and + // never lead to any scrolling). We don't handle inner_width < 0.0f, we could potentially use it to right-align + // based on the right side of the child window work rect, which would require knowing ahead if we are going to + // have decoration taking horizontal spaces (typically a vertical scrollbar). + if ((flags & ImGuiTableFlags_ScrollX) && inner_width > 0.0f) + override_content_size.x = inner_width; + + if (override_content_size.x != FLT_MAX || override_content_size.y != FLT_MAX) + SetNextWindowContentSize(ImVec2(override_content_size.x != FLT_MAX ? override_content_size.x : 0.0f, override_content_size.y != FLT_MAX ? override_content_size.y : 0.0f)); + + // Reset scroll if we are reactivating it + if ((table_last_flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) == 0) + SetNextWindowScroll(ImVec2(0.0f, 0.0f)); + + // Create scrolling region (without border and zero window padding) + ImGuiWindowFlags child_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None; + BeginChildEx(name, instance_id, outer_rect.GetSize(), false, child_flags); + table->InnerWindow = g.CurrentWindow; + table->WorkRect = table->InnerWindow->WorkRect; + table->OuterRect = table->InnerWindow->Rect(); + table->InnerRect = table->InnerWindow->InnerRect; + IM_ASSERT(table->InnerWindow->WindowPadding.x == 0.0f && table->InnerWindow->WindowPadding.y == 0.0f && table->InnerWindow->WindowBorderSize == 0.0f); + } + else + { + // For non-scrolling tables, WorkRect == OuterRect == InnerRect. + // But at this point we do NOT have a correct value for .Max.y (unless a height has been explicitly passed in). It will only be updated in EndTable(). + table->WorkRect = table->OuterRect = table->InnerRect = outer_rect; + } + + // Push a standardized ID for both child-using and not-child-using tables + PushOverrideID(instance_id); + + // Backup a copy of host window members we will modify + ImGuiWindow* inner_window = table->InnerWindow; + table->HostIndentX = inner_window->DC.Indent.x; + table->HostClipRect = inner_window->ClipRect; + table->HostSkipItems = inner_window->SkipItems; + table->HostBackupWorkRect = inner_window->WorkRect; + table->HostBackupParentWorkRect = inner_window->ParentWorkRect; + table->HostBackupColumnsOffset = outer_window->DC.ColumnsOffset; + table->HostBackupPrevLineSize = inner_window->DC.PrevLineSize; + table->HostBackupCurrLineSize = inner_window->DC.CurrLineSize; + table->HostBackupCursorMaxPos = inner_window->DC.CursorMaxPos; + table->HostBackupItemWidth = outer_window->DC.ItemWidth; + table->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size; + inner_window->DC.PrevLineSize = inner_window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + + // Padding and Spacing + // - None ........Content..... Pad .....Content........ + // - PadOuter | Pad ..Content..... Pad .....Content.. Pad | + // - PadInner ........Content.. Pad | Pad ..Content........ + // - PadOuter+PadInner | Pad ..Content.. Pad | Pad ..Content.. Pad | + const bool pad_outer_x = (flags & ImGuiTableFlags_NoPadOuterX) ? false : (flags & ImGuiTableFlags_PadOuterX) ? true : (flags & ImGuiTableFlags_BordersOuterV) != 0; + const bool pad_inner_x = (flags & ImGuiTableFlags_NoPadInnerX) ? false : true; + const float inner_spacing_for_border = (flags & ImGuiTableFlags_BordersInnerV) ? TABLE_BORDER_SIZE : 0.0f; + const float inner_spacing_explicit = (pad_inner_x && (flags & ImGuiTableFlags_BordersInnerV) == 0) ? g.Style.CellPadding.x : 0.0f; + const float inner_padding_explicit = (pad_inner_x && (flags & ImGuiTableFlags_BordersInnerV) != 0) ? g.Style.CellPadding.x : 0.0f; + table->CellSpacingX1 = inner_spacing_explicit + inner_spacing_for_border; + table->CellSpacingX2 = inner_spacing_explicit; + table->CellPaddingX = inner_padding_explicit; + table->CellPaddingY = g.Style.CellPadding.y; + + const float outer_padding_for_border = (flags & ImGuiTableFlags_BordersOuterV) ? TABLE_BORDER_SIZE : 0.0f; + const float outer_padding_explicit = pad_outer_x ? g.Style.CellPadding.x : 0.0f; + table->OuterPaddingX = (outer_padding_for_border + outer_padding_explicit) - table->CellPaddingX; + + table->CurrentColumn = -1; + table->CurrentRow = -1; + table->RowBgColorCounter = 0; + table->LastRowFlags = ImGuiTableRowFlags_None; + table->InnerClipRect = (inner_window == outer_window) ? table->WorkRect : inner_window->ClipRect; + table->InnerClipRect.ClipWith(table->WorkRect); // We need this to honor inner_width + table->InnerClipRect.ClipWithFull(table->HostClipRect); + table->InnerClipRect.Max.y = (flags & ImGuiTableFlags_NoHostExtendY) ? ImMin(table->InnerClipRect.Max.y, inner_window->WorkRect.Max.y) : inner_window->ClipRect.Max.y; + + table->RowPosY1 = table->RowPosY2 = table->WorkRect.Min.y; // This is needed somehow + table->RowTextBaseline = 0.0f; // This will be cleared again by TableBeginRow() + table->FreezeRowsRequest = table->FreezeRowsCount = 0; // This will be setup by TableSetupScrollFreeze(), if any + table->FreezeColumnsRequest = table->FreezeColumnsCount = 0; + table->IsUnfrozenRows = true; + table->DeclColumnsCount = 0; + + // Using opaque colors facilitate overlapping elements of the grid + table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong); + table->BorderColorLight = GetColorU32(ImGuiCol_TableBorderLight); + + // Make table current + const int table_idx = g.Tables.GetIndex(table); + g.CurrentTableStack.push_back(ImGuiPtrOrIndex(table_idx)); + g.CurrentTable = table; + outer_window->DC.CurrentTableIdx = table_idx; + if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly. + inner_window->DC.CurrentTableIdx = table_idx; + + if ((table_last_flags & ImGuiTableFlags_Reorderable) && (flags & ImGuiTableFlags_Reorderable) == 0) + table->IsResetDisplayOrderRequest = true; + + // Mark as used + if (table_idx >= g.TablesLastTimeActive.Size) + g.TablesLastTimeActive.resize(table_idx + 1, -1.0f); + g.TablesLastTimeActive[table_idx] = (float)g.Time; + table->MemoryCompacted = false; + + // Setup memory buffer (clear data if columns count changed) + const int stored_size = table->Columns.size(); + if (stored_size != 0 && stored_size != columns_count) + { + IM_FREE(table->RawData); + table->RawData = NULL; + } + if (table->RawData == NULL) + { + TableBeginInitMemory(table, columns_count); + table->IsInitializing = table->IsSettingsRequestLoad = true; + } + if (table->IsResetAllRequest) + TableResetSettings(table); + if (table->IsInitializing) + { + // Initialize + table->SettingsOffset = -1; + table->IsSortSpecsDirty = true; + table->InstanceInteracted = -1; + table->ContextPopupColumn = -1; + table->ReorderColumn = table->ResizedColumn = table->LastResizedColumn = -1; + table->AutoFitSingleColumn = -1; + table->HoveredColumnBody = table->HoveredColumnBorder = -1; + for (int n = 0; n < columns_count; n++) + { + ImGuiTableColumn* column = &table->Columns[n]; + float width_auto = column->WidthAuto; + *column = ImGuiTableColumn(); + column->WidthAuto = width_auto; + column->IsPreserveWidthAuto = true; // Preserve WidthAuto when reinitializing a live table: not technically necessary but remove a visible flicker + column->DisplayOrder = table->DisplayOrderToIndex[n] = (ImGuiTableColumnIdx)n; + column->IsEnabled = column->IsEnabledNextFrame = true; + } + } + + // Load settings + if (table->IsSettingsRequestLoad) + TableLoadSettings(table); + + // Handle DPI/font resize + // This is designed to facilitate DPI changes with the assumption that e.g. style.CellPadding has been scaled as well. + // It will also react to changing fonts with mixed results. It doesn't need to be perfect but merely provide a decent transition. + // FIXME-DPI: Provide consistent standards for reference size. Perhaps using g.CurrentDpiScale would be more self explanatory. + // This is will lead us to non-rounded WidthRequest in columns, which should work but is a poorly tested path. + const float new_ref_scale_unit = g.FontSize; // g.Font->GetCharAdvance('A') ? + if (table->RefScale != 0.0f && table->RefScale != new_ref_scale_unit) + { + const float scale_factor = new_ref_scale_unit / table->RefScale; + //IMGUI_DEBUG_LOG("[table] %08X RefScaleUnit %.3f -> %.3f, scaling width by %.3f\n", table->ID, table->RefScaleUnit, new_ref_scale_unit, scale_factor); + for (int n = 0; n < columns_count; n++) + table->Columns[n].WidthRequest = table->Columns[n].WidthRequest * scale_factor; + } + table->RefScale = new_ref_scale_unit; + + // Disable output until user calls TableNextRow() or TableNextColumn() leading to the TableUpdateLayout() call.. + // This is not strictly necessary but will reduce cases were "out of table" output will be misleading to the user. + // Because we cannot safely assert in EndTable() when no rows have been created, this seems like our best option. + inner_window->SkipItems = true; + + // Clear names + // At this point the ->NameOffset field of each column will be invalid until TableUpdateLayout() or the first call to TableSetupColumn() + if (table->ColumnsNames.Buf.Size > 0) + table->ColumnsNames.Buf.resize(0); + + // Apply queued resizing/reordering/hiding requests + TableBeginApplyRequests(table); + + return true; +} + +// For reference, the average total _allocation count_ for a table is: +// + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables) +// + 1 (for table->RawData allocated below) +// + 1 (for table->ColumnsNames, if names are used) +// + 1 (for table->Splitter._Channels) +// + 2 * active_channels_count (for ImDrawCmd and ImDrawIdx buffers inside channels) +// Where active_channels_count is variable but often == columns_count or columns_count + 1, see TableSetupDrawChannels() for details. +// Unused channels don't perform their +2 allocations. +void ImGui::TableBeginInitMemory(ImGuiTable* table, int columns_count) +{ + // Allocate single buffer for our arrays + ImSpanAllocator<3> span_allocator; + span_allocator.ReserveBytes(0, columns_count * sizeof(ImGuiTableColumn)); + span_allocator.ReserveBytes(1, columns_count * sizeof(ImGuiTableColumnIdx)); + span_allocator.ReserveBytes(2, columns_count * sizeof(ImGuiTableCellData)); + table->RawData = IM_ALLOC(span_allocator.GetArenaSizeInBytes()); + memset(table->RawData, 0, span_allocator.GetArenaSizeInBytes()); + span_allocator.SetArenaBasePtr(table->RawData); + span_allocator.GetSpan(0, &table->Columns); + span_allocator.GetSpan(1, &table->DisplayOrderToIndex); + span_allocator.GetSpan(2, &table->RowCellData); +} + +// Apply queued resizing/reordering/hiding requests +void ImGui::TableBeginApplyRequests(ImGuiTable* table) +{ + // Handle resizing request + // (We process this at the first TableBegin of the frame) + // FIXME-TABLE: Contains columns if our work area doesn't allow for scrolling? + if (table->InstanceCurrent == 0) + { + if (table->ResizedColumn != -1 && table->ResizedColumnNextWidth != FLT_MAX) + TableSetColumnWidth(table->ResizedColumn, table->ResizedColumnNextWidth); + table->LastResizedColumn = table->ResizedColumn; + table->ResizedColumnNextWidth = FLT_MAX; + table->ResizedColumn = -1; + + // Process auto-fit for single column, which is a special case for stretch columns and fixed columns with FixedSame policy. + // FIXME-TABLE: Would be nice to redistribute available stretch space accordingly to other weights, instead of giving it all to siblings. + if (table->AutoFitSingleColumn != -1) + { + TableSetColumnWidth(table->AutoFitSingleColumn, table->Columns[table->AutoFitSingleColumn].WidthAuto); + table->AutoFitSingleColumn = -1; + } + } + + // Handle reordering request + // Note: we don't clear ReorderColumn after handling the request. + if (table->InstanceCurrent == 0) + { + if (table->HeldHeaderColumn == -1 && table->ReorderColumn != -1) + table->ReorderColumn = -1; + table->HeldHeaderColumn = -1; + if (table->ReorderColumn != -1 && table->ReorderColumnDir != 0) + { + // We need to handle reordering across hidden columns. + // In the configuration below, moving C to the right of E will lead to: + // ... C [D] E ---> ... [D] E C (Column name/index) + // ... 2 3 4 ... 2 3 4 (Display order) + const int reorder_dir = table->ReorderColumnDir; + IM_ASSERT(reorder_dir == -1 || reorder_dir == +1); + IM_ASSERT(table->Flags & ImGuiTableFlags_Reorderable); + ImGuiTableColumn* src_column = &table->Columns[table->ReorderColumn]; + ImGuiTableColumn* dst_column = &table->Columns[(reorder_dir == -1) ? src_column->PrevEnabledColumn : src_column->NextEnabledColumn]; + IM_UNUSED(dst_column); + const int src_order = src_column->DisplayOrder; + const int dst_order = dst_column->DisplayOrder; + src_column->DisplayOrder = (ImGuiTableColumnIdx)dst_order; + for (int order_n = src_order + reorder_dir; order_n != dst_order + reorder_dir; order_n += reorder_dir) + table->Columns[table->DisplayOrderToIndex[order_n]].DisplayOrder -= (ImGuiTableColumnIdx)reorder_dir; + IM_ASSERT(dst_column->DisplayOrder == dst_order - reorder_dir); + + // Display order is stored in both columns->IndexDisplayOrder and table->DisplayOrder[], + // rebuild the later from the former. + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImGuiTableColumnIdx)column_n; + table->ReorderColumnDir = 0; + table->IsSettingsDirty = true; + } + } + + // Handle display order reset request + if (table->IsResetDisplayOrderRequest) + { + for (int n = 0; n < table->ColumnsCount; n++) + table->DisplayOrderToIndex[n] = table->Columns[n].DisplayOrder = (ImGuiTableColumnIdx)n; + table->IsResetDisplayOrderRequest = false; + table->IsSettingsDirty = true; + } +} + +// Adjust flags: default width mode + stretch columns are not allowed when auto extending +static void TableSetupColumnFlags(ImGuiTable* table, ImGuiTableColumn* column, ImGuiTableColumnFlags flags_in) +{ + ImGuiTableColumnFlags flags = flags_in; + + // Sizing Policy + if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0) + { + const ImGuiTableFlags table_sizing_policy = (table->Flags & ImGuiTableFlags_SizingMask_); + if (table_sizing_policy == ImGuiTableFlags_SizingFixedFit || table_sizing_policy == ImGuiTableFlags_SizingFixedSame) + flags |= ImGuiTableColumnFlags_WidthFixed; + else + flags |= ImGuiTableColumnFlags_WidthStretch; + } + else + { + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used. + } + + // Resize + if ((table->Flags & ImGuiTableFlags_Resizable) == 0) + flags |= ImGuiTableColumnFlags_NoResize; + + // Sorting + if ((flags & ImGuiTableColumnFlags_NoSortAscending) && (flags & ImGuiTableColumnFlags_NoSortDescending)) + flags |= ImGuiTableColumnFlags_NoSort; + + // Indentation + if ((flags & ImGuiTableColumnFlags_IndentMask_) == 0) + flags |= (table->Columns.index_from_ptr(column) == 0) ? ImGuiTableColumnFlags_IndentEnable : ImGuiTableColumnFlags_IndentDisable; + + // Alignment + //if ((flags & ImGuiTableColumnFlags_AlignMask_) == 0) + // flags |= ImGuiTableColumnFlags_AlignCenter; + //IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_AlignMask_)); // Check that only 1 of each set is used. + + // Preserve status flags + column->Flags = flags | (column->Flags & ImGuiTableColumnFlags_StatusMask_); + + // Build an ordered list of available sort directions + column->SortDirectionsAvailCount = column->SortDirectionsAvailMask = column->SortDirectionsAvailList = 0; + if (table->Flags & ImGuiTableFlags_Sortable) + { + int count = 0, mask = 0, list = 0; + if ((flags & ImGuiTableColumnFlags_PreferSortAscending) != 0 && (flags & ImGuiTableColumnFlags_NoSortAscending) == 0) { mask |= 1 << ImGuiSortDirection_Ascending; list |= ImGuiSortDirection_Ascending << (count << 1); count++; } + if ((flags & ImGuiTableColumnFlags_PreferSortDescending) != 0 && (flags & ImGuiTableColumnFlags_NoSortDescending) == 0) { mask |= 1 << ImGuiSortDirection_Descending; list |= ImGuiSortDirection_Descending << (count << 1); count++; } + if ((flags & ImGuiTableColumnFlags_PreferSortAscending) == 0 && (flags & ImGuiTableColumnFlags_NoSortAscending) == 0) { mask |= 1 << ImGuiSortDirection_Ascending; list |= ImGuiSortDirection_Ascending << (count << 1); count++; } + if ((flags & ImGuiTableColumnFlags_PreferSortDescending) == 0 && (flags & ImGuiTableColumnFlags_NoSortDescending) == 0) { mask |= 1 << ImGuiSortDirection_Descending; list |= ImGuiSortDirection_Descending << (count << 1); count++; } + if ((table->Flags & ImGuiTableFlags_SortTristate) || count == 0) { mask |= 1 << ImGuiSortDirection_None; count++; } + column->SortDirectionsAvailList = (ImU8)list; + column->SortDirectionsAvailMask = (ImU8)mask; + column->SortDirectionsAvailCount = (ImU8)count; + ImGui::TableFixColumnSortDirection(table, column); + } +} + +// Layout columns for the frame. This is in essence the followup to BeginTable(). +// Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first. +// FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for _WidthAuto columns. +// Increase feedback side-effect with widgets relying on WorkRect.Max.x... Maybe provide a default distribution for _WidthAuto columns? +void ImGui::TableUpdateLayout(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(table->IsLayoutLocked == false); + + const ImGuiTableFlags table_sizing_policy = (table->Flags & ImGuiTableFlags_SizingMask_); + table->IsDefaultDisplayOrder = true; + table->ColumnsEnabledCount = 0; + table->EnabledMaskByIndex = 0x00; + table->EnabledMaskByDisplayOrder = 0x00; + table->MinColumnWidth = ImMax(1.0f, g.Style.FramePadding.x * 1.0f); // g.Style.ColumnsMinSpacing; // FIXME-TABLE + + // [Part 1] Apply/lock Enabled and Order states. Calculate auto/ideal width for columns. Count fixed/stretch columns. + // Process columns in their visible orders as we are building the Prev/Next indices. + int count_fixed = 0; // Number of columns that have fixed sizing policies + int count_stretch = 0; // Number of columns that have stretch sizing policies + int last_visible_column_idx = -1; + bool has_auto_fit_request = false; + bool has_resizable = false; + float stretch_sum_width_auto = 0.0f; + float fixed_max_width_auto = 0.0f; + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + { + const int column_n = table->DisplayOrderToIndex[order_n]; + if (column_n != order_n) + table->IsDefaultDisplayOrder = false; + ImGuiTableColumn* column = &table->Columns[column_n]; + + // Clear column setup if not submitted by user. Currently we make it mandatory to call TableSetupColumn() every frame. + // It would easily work without but we're not ready to guarantee it since e.g. names need resubmission anyway. + // We take a slight shortcut but in theory we could be calling TableSetupColumn() here with dummy values, it should yield the same effect. + if (table->DeclColumnsCount <= column_n) + { + TableSetupColumnFlags(table, column, ImGuiTableColumnFlags_None); + column->NameOffset = -1; + column->UserID = 0; + column->InitStretchWeightOrWidth = -1.0f; + } + + // Update Enabled state, mark settings/sortspecs dirty + if (!(table->Flags & ImGuiTableFlags_Hideable) || (column->Flags & ImGuiTableColumnFlags_NoHide)) + column->IsEnabledNextFrame = true; + if (column->IsEnabled != column->IsEnabledNextFrame) + { + column->IsEnabled = column->IsEnabledNextFrame; + table->IsSettingsDirty = true; + if (!column->IsEnabled && column->SortOrder != -1) + table->IsSortSpecsDirty = true; + } + if (column->SortOrder > 0 && !(table->Flags & ImGuiTableFlags_SortMulti)) + table->IsSortSpecsDirty = true; + + // Auto-fit unsized columns + const bool start_auto_fit = (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? (column->WidthRequest < 0.0f) : (column->StretchWeight < 0.0f); + if (start_auto_fit) + column->AutoFitQueue = column->CannotSkipItemsQueue = (1 << 3) - 1; // Fit for three frames + + if (!column->IsEnabled) + { + column->IndexWithinEnabledSet = -1; + continue; + } + + // Mark as enabled and link to previous/next enabled column + column->PrevEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; + column->NextEnabledColumn = -1; + if (last_visible_column_idx != -1) + table->Columns[last_visible_column_idx].NextEnabledColumn = (ImGuiTableColumnIdx)column_n; + column->IndexWithinEnabledSet = table->ColumnsEnabledCount++; + table->EnabledMaskByIndex |= (ImU64)1 << column_n; + table->EnabledMaskByDisplayOrder |= (ImU64)1 << column->DisplayOrder; + last_visible_column_idx = column_n; + IM_ASSERT(column->IndexWithinEnabledSet <= column->DisplayOrder); + + // Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping) + // Combine width from regular rows + width from headers unless requested not to. + if (!column->IsPreserveWidthAuto) + column->WidthAuto = TableGetColumnWidthAuto(table, column); + + // Non-resizable columns keep their requested width (apply user value regardless of IsPreserveWidthAuto) + const bool column_is_resizable = (column->Flags & ImGuiTableColumnFlags_NoResize) == 0; + if (column_is_resizable) + has_resizable = true; + if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && column->InitStretchWeightOrWidth > 0.0f && !column_is_resizable) + column->WidthAuto = column->InitStretchWeightOrWidth; + + if (column->AutoFitQueue != 0x00) + has_auto_fit_request = true; + if (column->Flags & ImGuiTableColumnFlags_WidthStretch) + { + stretch_sum_width_auto += column->WidthAuto; + count_stretch++; + } + else + { + fixed_max_width_auto = ImMax(fixed_max_width_auto, column->WidthAuto); + count_fixed++; + } + } + if ((table->Flags & ImGuiTableFlags_Sortable) && table->SortSpecsCount == 0 && !(table->Flags & ImGuiTableFlags_SortTristate)) + table->IsSortSpecsDirty = true; + table->RightMostEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; + IM_ASSERT(table->RightMostEnabledColumn >= 0); + + // [Part 2] Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible + // to avoid the column fitting having to wait until the first visible frame of the child container (may or not be a good thing). + // FIXME-TABLE: for always auto-resizing columns may not want to do that all the time. + if (has_auto_fit_request && table->OuterWindow != table->InnerWindow) + table->InnerWindow->SkipItems = false; + if (has_auto_fit_request) + table->IsSettingsDirty = true; + + // [Part 3] Fix column flags and record a few extra information. + float sum_width_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns but including spacing/padding. + float stretch_sum_weights = 0.0f; // Sum of all weights for stretch columns. + table->LeftMostStretchedColumn = table->RightMostStretchedColumn = -1; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n))) + continue; + ImGuiTableColumn* column = &table->Columns[column_n]; + + const bool column_is_resizable = (column->Flags & ImGuiTableColumnFlags_NoResize) == 0; + if (column->Flags & ImGuiTableColumnFlags_WidthFixed) + { + // Apply same widths policy + float width_auto = column->WidthAuto; + if (table_sizing_policy == ImGuiTableFlags_SizingFixedSame && (column->AutoFitQueue != 0x00 || !column_is_resizable)) + width_auto = fixed_max_width_auto; + + // Apply automatic width + // Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!) + if (column->AutoFitQueue != 0x00) + column->WidthRequest = width_auto; + else if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !column_is_resizable && (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n))) + column->WidthRequest = width_auto; + + // FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets + // (e.g. TextWrapped) too much. Otherwise what tends to happen is that TextWrapped would output a very + // large height (= first frame scrollbar display very off + clipper would skip lots of items). + // This is merely making the side-effect less extreme, but doesn't properly fixes it. + // FIXME: Move this to ->WidthGiven to avoid temporary lossyless? + // FIXME: This break IsPreserveWidthAuto from not flickering if the stored WidthAuto was smaller. + if (column->AutoFitQueue > 0x01 && table->IsInitializing && !column->IsPreserveWidthAuto) + column->WidthRequest = ImMax(column->WidthRequest, table->MinColumnWidth * 4.0f); // FIXME-TABLE: Another constant/scale? + sum_width_requests += column->WidthRequest; + } + else + { + // Initialize stretch weight + if (column->AutoFitQueue != 0x00 || column->StretchWeight < 0.0f || !column_is_resizable) + { + if (column->InitStretchWeightOrWidth > 0.0f) + column->StretchWeight = column->InitStretchWeightOrWidth; + else if (table_sizing_policy == ImGuiTableFlags_SizingStretchProp) + column->StretchWeight = (column->WidthAuto / stretch_sum_width_auto) * count_stretch; + else + column->StretchWeight = 1.0f; + } + + stretch_sum_weights += column->StretchWeight; + if (table->LeftMostStretchedColumn == -1 || table->Columns[table->LeftMostStretchedColumn].DisplayOrder > column->DisplayOrder) + table->LeftMostStretchedColumn = (ImGuiTableColumnIdx)column_n; + if (table->RightMostStretchedColumn == -1 || table->Columns[table->RightMostStretchedColumn].DisplayOrder < column->DisplayOrder) + table->RightMostStretchedColumn = (ImGuiTableColumnIdx)column_n; + } + column->IsPreserveWidthAuto = false; + sum_width_requests += table->CellPaddingX * 2.0f; + } + table->ColumnsEnabledFixedCount = (ImGuiTableColumnIdx)count_fixed; + + // [Part 4] Apply final widths based on requested widths + const ImRect work_rect = table->WorkRect; + const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); + const float width_avail = ((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f) ? table->InnerClipRect.GetWidth() : work_rect.GetWidth(); + const float width_avail_for_stretched_columns = width_avail - width_spacings - sum_width_requests; + float width_remaining_for_stretched_columns = width_avail_for_stretched_columns; + table->ColumnsGivenWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n))) + continue; + ImGuiTableColumn* column = &table->Columns[column_n]; + + // Allocate width for stretched/weighted columns (StretchWeight gets converted into WidthRequest) + if (column->Flags & ImGuiTableColumnFlags_WidthStretch) + { + float weight_ratio = column->StretchWeight / stretch_sum_weights; + column->WidthRequest = IM_FLOOR(ImMax(width_avail_for_stretched_columns * weight_ratio, table->MinColumnWidth) + 0.01f); + width_remaining_for_stretched_columns -= column->WidthRequest; + } + + // [Resize Rule 1] The right-most Visible column is not resizable if there is at least one Stretch column + // See additional comments in TableSetColumnWidth(). + if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumn != -1) + column->Flags |= ImGuiTableColumnFlags_NoDirectResize_; + + // Assign final width, record width in case we will need to shrink + column->WidthGiven = ImFloor(ImMax(column->WidthRequest, table->MinColumnWidth)); + table->ColumnsGivenWidth += column->WidthGiven; + } + + // [Part 5] Redistribute stretch remainder width due to rounding (remainder width is < 1.0f * number of Stretch column). + // Using right-to-left distribution (more likely to match resizing cursor). + if (width_remaining_for_stretched_columns >= 1.0f && !(table->Flags & ImGuiTableFlags_PreciseWidths)) + for (int order_n = table->ColumnsCount - 1; stretch_sum_weights > 0.0f && width_remaining_for_stretched_columns >= 1.0f && order_n >= 0; order_n--) + { + if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + continue; + ImGuiTableColumn* column = &table->Columns[table->DisplayOrderToIndex[order_n]]; + if (!(column->Flags & ImGuiTableColumnFlags_WidthStretch)) + continue; + column->WidthRequest += 1.0f; + column->WidthGiven += 1.0f; + width_remaining_for_stretched_columns -= 1.0f; + } + + table->HoveredColumnBody = -1; + table->HoveredColumnBorder = -1; + const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table->LastOuterHeight)); + const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0); + + // [Part 6] Setup final position, offset, skip/clip states and clipping rectangles, detect hovered column + // Process columns in their visible orders as we are comparing the visible order and adjusting host_clip_rect while looping. + int visible_n = 0; + bool offset_x_frozen = (table->FreezeColumnsCount > 0); + float offset_x = ((table->FreezeColumnsCount > 0) ? table->OuterRect.Min.x : work_rect.Min.x) + table->OuterPaddingX - table->CellSpacingX1; + ImRect host_clip_rect = table->InnerClipRect; + //host_clip_rect.Max.x += table->CellPaddingX + table->CellSpacingX2; + table->VisibleMaskByIndex = 0x00; + table->RequestOutputMaskByIndex = 0x00; + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + { + const int column_n = table->DisplayOrderToIndex[order_n]; + ImGuiTableColumn* column = &table->Columns[column_n]; + + column->NavLayerCurrent = (ImS8)((table->FreezeRowsCount > 0 || column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main); + + if (offset_x_frozen && table->FreezeColumnsCount == visible_n) + { + offset_x += work_rect.Min.x - table->OuterRect.Min.x; + offset_x_frozen = false; + } + + // Clear status flags + column->Flags &= ~ImGuiTableColumnFlags_StatusMask_; + + if ((table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)) == 0) + { + // Hidden column: clear a few fields and we are done with it for the remainder of the function. + // We set a zero-width clip rect but set Min.y/Max.y properly to not interfere with the clipper. + column->MinX = column->MaxX = column->WorkMinX = column->ClipRect.Min.x = column->ClipRect.Max.x = offset_x; + column->WidthGiven = 0.0f; + column->ClipRect.Min.y = work_rect.Min.y; + column->ClipRect.Max.y = FLT_MAX; + column->ClipRect.ClipWithFull(host_clip_rect); + column->IsVisibleX = column->IsVisibleY = column->IsRequestOutput = false; + column->IsSkipItems = true; + column->ItemWidth = 1.0f; + continue; + } + + // Detect hovered column + if (is_hovering_table && g.IO.MousePos.x >= column->ClipRect.Min.x && g.IO.MousePos.x < column->ClipRect.Max.x) + table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n; + + // Lock start position + column->MinX = offset_x; + + // Lock width based on start position and minimum/maximum width for this position + float max_width = TableGetMaxColumnWidth(table, column_n); + column->WidthGiven = ImMin(column->WidthGiven, max_width); + column->WidthGiven = ImMax(column->WidthGiven, ImMin(column->WidthRequest, table->MinColumnWidth)); + column->MaxX = offset_x + column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f; + + // Lock other positions + // - ClipRect.Min.x: Because merging draw commands doesn't compare min boundaries, we make ClipRect.Min.x match left bounds to be consistent regardless of merging. + // - ClipRect.Max.x: using WorkMaxX instead of MaxX (aka including padding) makes things more consistent when resizing down, tho slightly detrimental to visibility in very-small column. + // - ClipRect.Max.x: using MaxX makes it easier for header to receive hover highlight with no discontinuity and display sorting arrow. + // - FIXME-TABLE: We want equal width columns to have equal (ClipRect.Max.x - WorkMinX) width, which means ClipRect.max.x cannot stray off host_clip_rect.Max.x else right-most column may appear shorter. + column->WorkMinX = column->MinX + table->CellPaddingX + table->CellSpacingX1; + column->WorkMaxX = column->MaxX - table->CellPaddingX - table->CellSpacingX2; // Expected max + column->ItemWidth = ImFloor(column->WidthGiven * 0.65f); + column->ClipRect.Min.x = column->MinX; + column->ClipRect.Min.y = work_rect.Min.y; + column->ClipRect.Max.x = column->MaxX; //column->WorkMaxX; + column->ClipRect.Max.y = FLT_MAX; + column->ClipRect.ClipWithFull(host_clip_rect); + + // Mark column as Clipped (not in sight) + // Note that scrolling tables (where inner_window != outer_window) handle Y clipped earlier in BeginTable() so IsVisibleY really only applies to non-scrolling tables. + // FIXME-TABLE: Because InnerClipRect.Max.y is conservatively ==outer_window->ClipRect.Max.y, we never can mark columns _Above_ the scroll line as not IsVisibleY. + // Taking advantage of LastOuterHeight would yield good results there... + // FIXME-TABLE: Y clipping is disabled because it effectively means not submitting will reduce contents width which is fed to outer_window->DC.CursorMaxPos.x, + // and this may be used (e.g. typically by outer_window using AlwaysAutoResize or outer_window's horizontal scrollbar, but could be something else). + // Possible solution to preserve last known content width for clipped column. Test 'table_reported_size' fails when enabling Y clipping and window is resized small. + column->IsVisibleX = (column->ClipRect.Max.x > column->ClipRect.Min.x); + column->IsVisibleY = true; // (column->ClipRect.Max.y > column->ClipRect.Min.y); + const bool is_visible = column->IsVisibleX; //&& column->IsVisibleY; + if (is_visible) + table->VisibleMaskByIndex |= ((ImU64)1 << column_n); + + // Mark column as requesting output from user. Note that fixed + non-resizable sets are auto-fitting at all times and therefore always request output. + column->IsRequestOutput = is_visible || column->AutoFitQueue != 0 || column->CannotSkipItemsQueue != 0; + if (column->IsRequestOutput) + table->RequestOutputMaskByIndex |= ((ImU64)1 << column_n); + + // Mark column as SkipItems (ignoring all items/layout) + column->IsSkipItems = !column->IsEnabled || table->HostSkipItems; + if (column->IsSkipItems) + IM_ASSERT(!is_visible); + + // Update status flags + column->Flags |= ImGuiTableColumnFlags_IsEnabled; + if (is_visible) + column->Flags |= ImGuiTableColumnFlags_IsVisible; + if (column->SortOrder != -1) + column->Flags |= ImGuiTableColumnFlags_IsSorted; + if (table->HoveredColumnBody == column_n) + column->Flags |= ImGuiTableColumnFlags_IsHovered; + + // Alignment + // FIXME-TABLE: This align based on the whole column width, not per-cell, and therefore isn't useful in + // many cases (to be able to honor this we might be able to store a log of cells width, per row, for + // visible rows, but nav/programmatic scroll would have visible artifacts.) + //if (column->Flags & ImGuiTableColumnFlags_AlignRight) + // column->WorkMinX = ImMax(column->WorkMinX, column->MaxX - column->ContentWidthRowsUnfrozen); + //else if (column->Flags & ImGuiTableColumnFlags_AlignCenter) + // column->WorkMinX = ImLerp(column->WorkMinX, ImMax(column->StartX, column->MaxX - column->ContentWidthRowsUnfrozen), 0.5f); + + // Reset content width variables + column->ContentMaxXFrozen = column->ContentMaxXUnfrozen = column->WorkMinX; + column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX; + + // Don't decrement auto-fit counters until container window got a chance to submit its items + if (table->HostSkipItems == false) + { + column->AutoFitQueue >>= 1; + column->CannotSkipItemsQueue >>= 1; + } + + if (visible_n < table->FreezeColumnsCount) + host_clip_rect.Min.x = ImClamp(column->MaxX + TABLE_BORDER_SIZE, host_clip_rect.Min.x, host_clip_rect.Max.x); + + offset_x += column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f; + visible_n++; + } + + // [Part 7] Detect/store when we are hovering the unused space after the right-most column (so e.g. context menus can react on it) + // Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag, either + // because of using _WidthAuto/_WidthStretch). This will hide the resizing option from the context menu. + const float unused_x1 = ImMax(table->WorkRect.Min.x, table->Columns[table->RightMostEnabledColumn].ClipRect.Max.x); + if (is_hovering_table && table->HoveredColumnBody == -1) + { + if (g.IO.MousePos.x >= unused_x1) + table->HoveredColumnBody = (ImGuiTableColumnIdx)table->ColumnsCount; + } + if (has_resizable == false && (table->Flags & ImGuiTableFlags_Resizable)) + table->Flags &= ~ImGuiTableFlags_Resizable; + + // [Part 8] Lock actual OuterRect/WorkRect right-most position. + // This is done late to handle the case of fixed-columns tables not claiming more widths that they need. + // Because of this we are careful with uses of WorkRect and InnerClipRect before this point. + if (table->RightMostStretchedColumn != -1) + table->Flags &= ~ImGuiTableFlags_NoHostExtendX; + if (table->Flags & ImGuiTableFlags_NoHostExtendX) + { + table->OuterRect.Max.x = table->WorkRect.Max.x = unused_x1; + table->InnerClipRect.Max.x = ImMin(table->InnerClipRect.Max.x, unused_x1); + } + table->InnerWindow->ParentWorkRect = table->WorkRect; + table->BorderX1 = table->InnerClipRect.Min.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : -1.0f); + table->BorderX2 = table->InnerClipRect.Max.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : +1.0f); + + // [Part 9] Allocate draw channels and setup background cliprect + TableSetupDrawChannels(table); + + // [Part 10] Hit testing on borders + if (table->Flags & ImGuiTableFlags_Resizable) + TableUpdateBorders(table); + table->LastFirstRowHeight = 0.0f; + table->IsLayoutLocked = true; + table->IsUsingHeaders = false; + + // [Part 11] Context menu + if (table->IsContextPopupOpen && table->InstanceCurrent == table->InstanceInteracted) + { + const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID); + if (BeginPopupEx(context_menu_id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings)) + { + TableDrawContextMenu(table); + EndPopup(); + } + else + { + table->IsContextPopupOpen = false; + } + } + + // [Part 13] Sanitize and build sort specs before we have a change to use them for display. + // This path will only be exercised when sort specs are modified before header rows (e.g. init or visibility change) + if (table->IsSortSpecsDirty && (table->Flags & ImGuiTableFlags_Sortable)) + TableSortSpecsBuild(table); + + // Initial state + ImGuiWindow* inner_window = table->InnerWindow; + if (table->Flags & ImGuiTableFlags_NoClip) + table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); + else + inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false); +} + +// Process hit-testing on resizing borders. Actual size change will be applied in EndTable() +// - Set table->HoveredColumnBorder with a short delay/timer to reduce feedback noise +// - Submit ahead of table contents and header, use ImGuiButtonFlags_AllowItemOverlap to prioritize widgets +// overlapping the same area. +void ImGui::TableUpdateBorders(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(table->Flags & ImGuiTableFlags_Resizable); + + // At this point OuterRect height may be zero or under actual final height, so we rely on temporal coherency and + // use the final height from last frame. Because this is only affecting _interaction_ with columns, it is not + // really problematic (whereas the actual visual will be displayed in EndTable() and using the current frame height). + // Actual columns highlight/render will be performed in EndTable() and not be affected. + const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS; + const float hit_y1 = table->OuterRect.Min.y; + const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table->LastOuterHeight); + const float hit_y2_head = hit_y1 + table->LastFirstRowHeight; + + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + { + if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + continue; + + const int column_n = table->DisplayOrderToIndex[order_n]; + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_)) + continue; + + // ImGuiTableFlags_NoBordersInBodyUntilResize will be honored in TableDrawBorders() + const float border_y2_hit = (table->Flags & ImGuiTableFlags_NoBordersInBody) ? hit_y2_head : hit_y2_body; + if ((table->Flags & ImGuiTableFlags_NoBordersInBody) && table->IsUsingHeaders == false) + continue; + + if (table->FreezeColumnsCount > 0) + if (column->MaxX < table->Columns[table->DisplayOrderToIndex[table->FreezeColumnsCount - 1]].MaxX) + continue; + + ImGuiID column_id = TableGetColumnResizeID(table, column_n, table->InstanceCurrent); + ImRect hit_rect(column->MaxX - hit_half_width, hit_y1, column->MaxX + hit_half_width, border_y2_hit); + //GetForegroundDrawList()->AddRect(hit_rect.Min, hit_rect.Max, IM_COL32(255, 0, 0, 100)); + KeepAliveID(column_id); + + bool hovered = false, held = false; + bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick); + if (pressed && IsMouseDoubleClicked(0)) + { + TableSetColumnWidthAutoSingle(table, column_n); + ClearActiveID(); + held = hovered = false; + } + if (held) + { + if (table->LastResizedColumn == -1) + table->ResizeLockMinContentsX2 = table->RightMostEnabledColumn != -1 ? table->Columns[table->RightMostEnabledColumn].MaxX : -FLT_MAX; + table->ResizedColumn = (ImGuiTableColumnIdx)column_n; + table->InstanceInteracted = table->InstanceCurrent; + } + if ((hovered && g.HoveredIdTimer > TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER) || held) + { + table->HoveredColumnBorder = (ImGuiTableColumnIdx)column_n; + SetMouseCursor(ImGuiMouseCursor_ResizeEW); + } + } +} + +void ImGui::EndTable() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && "Only call EndTable() if BeginTable() returns true!"); + + // This assert would be very useful to catch a common error... unfortunately it would probably trigger in some + // cases, and for consistency user may sometimes output empty tables (and still benefit from e.g. outer border) + //IM_ASSERT(table->IsLayoutLocked && "Table unused: never called TableNextRow(), is that the intent?"); + + // If the user never got to call TableNextRow() or TableNextColumn(), we call layout ourselves to ensure all our + // code paths are consistent (instead of just hoping that TableBegin/TableEnd will work), get borders drawn, etc. + if (!table->IsLayoutLocked) + TableUpdateLayout(table); + + const ImGuiTableFlags flags = table->Flags; + ImGuiWindow* inner_window = table->InnerWindow; + ImGuiWindow* outer_window = table->OuterWindow; + IM_ASSERT(inner_window == g.CurrentWindow); + IM_ASSERT(outer_window == inner_window || outer_window == inner_window->ParentWindow); + + if (table->IsInsideRow) + TableEndRow(table); + + // Context menu in columns body + if (flags & ImGuiTableFlags_ContextMenuInBody) + if (table->HoveredColumnBody != -1 && !IsAnyItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) + TableOpenContextMenu((int)table->HoveredColumnBody); + + // Finalize table height + inner_window->DC.PrevLineSize = table->HostBackupPrevLineSize; + inner_window->DC.CurrLineSize = table->HostBackupCurrLineSize; + inner_window->DC.CursorMaxPos = table->HostBackupCursorMaxPos; + const float inner_content_max_y = table->RowPosY2; + IM_ASSERT(table->RowPosY2 == inner_window->DC.CursorPos.y); + if (inner_window != outer_window) + inner_window->DC.CursorMaxPos.y = inner_content_max_y; + else if (!(flags & ImGuiTableFlags_NoHostExtendY)) + table->OuterRect.Max.y = table->InnerRect.Max.y = ImMax(table->OuterRect.Max.y, inner_content_max_y); // Patch OuterRect/InnerRect height + table->WorkRect.Max.y = ImMax(table->WorkRect.Max.y, table->OuterRect.Max.y); + table->LastOuterHeight = table->OuterRect.GetHeight(); + + // Setup inner scrolling range + // FIXME: This ideally should be done earlier, in BeginTable() SetNextWindowContentSize call, just like writing to inner_window->DC.CursorMaxPos.y, + // but since the later is likely to be impossible to do we'd rather update both axises together. + if (table->Flags & ImGuiTableFlags_ScrollX) + { + const float outer_padding_for_border = (table->Flags & ImGuiTableFlags_BordersOuterV) ? TABLE_BORDER_SIZE : 0.0f; + float max_pos_x = table->InnerWindow->DC.CursorMaxPos.x; + if (table->RightMostEnabledColumn != -1) + max_pos_x = ImMax(max_pos_x, table->Columns[table->RightMostEnabledColumn].WorkMaxX + table->CellPaddingX + table->OuterPaddingX - outer_padding_for_border); + if (table->ResizedColumn != -1) + max_pos_x = ImMax(max_pos_x, table->ResizeLockMinContentsX2); + table->InnerWindow->DC.CursorMaxPos.x = max_pos_x; + } + + // Pop clipping rect + if (!(flags & ImGuiTableFlags_NoClip)) + inner_window->DrawList->PopClipRect(); + inner_window->ClipRect = inner_window->DrawList->_ClipRectStack.back(); + + // Draw borders + if ((flags & ImGuiTableFlags_Borders) != 0) + TableDrawBorders(table); + +#if 0 + // Strip out dummy channel draw calls + // We have no way to prevent user submitting direct ImDrawList calls into a hidden column (but ImGui:: calls will be clipped out) + // Pros: remove draw calls which will have no effect. since they'll have zero-size cliprect they may be early out anyway. + // Cons: making it harder for users watching metrics/debugger to spot the wasted vertices. + if (table->DummyDrawChannel != (ImGuiTableColumnIdx)-1) + { + ImDrawChannel* dummy_channel = &table->DrawSplitter._Channels[table->DummyDrawChannel]; + dummy_channel->_CmdBuffer.resize(0); + dummy_channel->_IdxBuffer.resize(0); + } +#endif + + // Flatten channels and merge draw calls + table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0); + if ((table->Flags & ImGuiTableFlags_NoClip) == 0) + TableMergeDrawChannels(table); + table->DrawSplitter.Merge(inner_window->DrawList); + + // Update ColumnsAutoFitWidth to get us ahead for host using our size to auto-resize without waiting for next BeginTable() + const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); + table->ColumnsAutoFitWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if (table->EnabledMaskByIndex & ((ImU64)1 << column_n)) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !(column->Flags & ImGuiTableColumnFlags_NoResize)) + table->ColumnsAutoFitWidth += column->WidthRequest; + else + table->ColumnsAutoFitWidth += TableGetColumnWidthAuto(table, column); + } + + // Update scroll + if ((table->Flags & ImGuiTableFlags_ScrollX) == 0 && inner_window != outer_window) + { + inner_window->Scroll.x = 0.0f; + } + else if (table->LastResizedColumn != -1 && table->ResizedColumn == -1 && inner_window->ScrollbarX && table->InstanceInteracted == table->InstanceCurrent) + { + // When releasing a column being resized, scroll to keep the resulting column in sight + const float neighbor_width_to_keep_visible = table->MinColumnWidth + table->CellPaddingX * 2.0f; + ImGuiTableColumn* column = &table->Columns[table->LastResizedColumn]; + if (column->MaxX < table->InnerClipRect.Min.x) + SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x - neighbor_width_to_keep_visible, 1.0f); + else if (column->MaxX > table->InnerClipRect.Max.x) + SetScrollFromPosX(inner_window, column->MaxX - inner_window->Pos.x + neighbor_width_to_keep_visible, 1.0f); + } + + // Apply resizing/dragging at the end of the frame + if (table->ResizedColumn != -1 && table->InstanceCurrent == table->InstanceInteracted) + { + ImGuiTableColumn* column = &table->Columns[table->ResizedColumn]; + const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + TABLE_RESIZE_SEPARATOR_HALF_THICKNESS); + const float new_width = ImFloor(new_x2 - column->MinX - table->CellSpacingX1 - table->CellPaddingX * 2.0f); + table->ResizedColumnNextWidth = new_width; + } + + // Pop from id stack + IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table->ID + table->InstanceCurrent, "Mismatching PushID/PopID!"); + IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= table->HostBackupItemWidthStackSize, "Too many PopItemWidth!"); + PopID(); + + // Restore window data that we modified + const ImVec2 backup_outer_max_pos = outer_window->DC.CursorMaxPos; + inner_window->WorkRect = table->HostBackupWorkRect; + inner_window->ParentWorkRect = table->HostBackupParentWorkRect; + inner_window->SkipItems = table->HostSkipItems; + outer_window->DC.CursorPos = table->OuterRect.Min; + outer_window->DC.ItemWidth = table->HostBackupItemWidth; + outer_window->DC.ItemWidthStack.Size = table->HostBackupItemWidthStackSize; + outer_window->DC.ColumnsOffset = table->HostBackupColumnsOffset; + + // Layout in outer window + // (FIXME: To allow auto-fit and allow desirable effect of SameLine() we dissociate 'used' vs 'ideal' size by overriding + // CursorPosPrevLine and CursorMaxPos manually. That should be a more general layout feature, see same problem e.g. #3414) + if (inner_window != outer_window) + { + EndChild(); + } + else + { + ItemSize(table->OuterRect.GetSize()); + ItemAdd(table->OuterRect, 0); + } + + // Override declared contents width/height to enable auto-resize while not needlessly adding a scrollbar + if (table->Flags & ImGuiTableFlags_NoHostExtendX) + { + // FIXME-TABLE: Could we remove this section? + // ColumnsAutoFitWidth may be one frame ahead here since for Fixed+NoResize is calculated from latest contents + IM_ASSERT((table->Flags & ImGuiTableFlags_ScrollX) == 0); + outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth); + } + else if (table->UserOuterSize.x <= 0.0f) + { + const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f; + outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - table->UserOuterSize.x); + outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth)); + } + else + { + outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, table->OuterRect.Max.x); + } + if (table->UserOuterSize.y <= 0.0f) + { + const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollY) ? inner_window->ScrollbarSizes.y : 0.0f; + outer_window->DC.IdealMaxPos.y = ImMax(outer_window->DC.IdealMaxPos.y, inner_content_max_y + decoration_size - table->UserOuterSize.y); + outer_window->DC.CursorMaxPos.y = ImMax(backup_outer_max_pos.y, ImMin(table->OuterRect.Max.y, inner_content_max_y)); + } + else + { + // OuterRect.Max.y may already have been pushed downward from the initial value (unless ImGuiTableFlags_NoHostExtendY is set) + outer_window->DC.CursorMaxPos.y = ImMax(backup_outer_max_pos.y, table->OuterRect.Max.y); + } + + // Save settings + if (table->IsSettingsDirty) + TableSaveSettings(table); + table->IsInitializing = false; + + // Clear or restore current table, if any + IM_ASSERT(g.CurrentWindow == outer_window && g.CurrentTable == table); + g.CurrentTableStack.pop_back(); + g.CurrentTable = g.CurrentTableStack.Size ? g.Tables.GetByIndex(g.CurrentTableStack.back().Index) : NULL; + outer_window->DC.CurrentTableIdx = g.CurrentTable ? g.Tables.GetIndex(g.CurrentTable) : -1; +} + +// See "COLUMN SIZING POLICIES" comments at the top of this file +// If (init_width_or_weight <= 0.0f) it is ignored +void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, float init_width_or_weight, ImGuiID user_id) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!"); + IM_ASSERT(table->IsLayoutLocked == false && "Need to call call TableSetupColumn() before first row!"); + IM_ASSERT((flags & ImGuiTableColumnFlags_StatusMask_) == 0 && "Illegal to pass StatusMask values to TableSetupColumn()"); + if (table->DeclColumnsCount >= table->ColumnsCount) + { + IM_ASSERT_USER_ERROR(table->DeclColumnsCount < table->ColumnsCount, "Called TableSetupColumn() too many times!"); + return; + } + + ImGuiTableColumn* column = &table->Columns[table->DeclColumnsCount]; + table->DeclColumnsCount++; + + // Assert when passing a width or weight if policy is entirely left to default, to avoid storing width into weight and vice-versa. + // Give a grace to users of ImGuiTableFlags_ScrollX. + if (table->IsDefaultSizingPolicy && (flags & ImGuiTableColumnFlags_WidthMask_) == 0 && (flags & ImGuiTableFlags_ScrollX) == 0) + IM_ASSERT(init_width_or_weight <= 0.0f && "Can only specify width/weight if sizing policy is set explicitly in either Table or Column."); + + // When passing a width automatically enforce WidthFixed policy + // (whereas TableSetupColumnFlags would default to WidthAuto if table is not Resizable) + if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0 && init_width_or_weight > 0.0f) + if ((table->Flags & ImGuiTableFlags_SizingMask_) == ImGuiTableFlags_SizingFixedFit || (table->Flags & ImGuiTableFlags_SizingMask_) == ImGuiTableFlags_SizingFixedSame) + flags |= ImGuiTableColumnFlags_WidthFixed; + + TableSetupColumnFlags(table, column, flags); + column->UserID = user_id; + flags = column->Flags; + + // Initialize defaults + column->InitStretchWeightOrWidth = init_width_or_weight; + if (table->IsInitializing) + { + // Init width or weight + if (column->WidthRequest < 0.0f && column->StretchWeight < 0.0f) + { + if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f) + column->WidthRequest = init_width_or_weight; + if (flags & ImGuiTableColumnFlags_WidthStretch) + column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : -1.0f; + + // Disable auto-fit if an explicit width/weight has been specified + if (init_width_or_weight > 0.0f) + column->AutoFitQueue = 0x00; + } + + // Init default visibility/sort state + if ((flags & ImGuiTableColumnFlags_DefaultHide) && (table->SettingsLoadedFlags & ImGuiTableFlags_Hideable) == 0) + column->IsEnabled = column->IsEnabledNextFrame = false; + if (flags & ImGuiTableColumnFlags_DefaultSort && (table->SettingsLoadedFlags & ImGuiTableFlags_Sortable) == 0) + { + column->SortOrder = 0; // Multiple columns using _DefaultSort will be reassigned unique SortOrder values when building the sort specs. + column->SortDirection = (column->Flags & ImGuiTableColumnFlags_PreferSortDescending) ? (ImS8)ImGuiSortDirection_Descending : (ImU8)(ImGuiSortDirection_Ascending); + } + } + + // Store name (append with zero-terminator in contiguous buffer) + column->NameOffset = -1; + if (label != NULL && label[0] != 0) + { + column->NameOffset = (ImS16)table->ColumnsNames.size(); + table->ColumnsNames.append(label, label + strlen(label) + 1); + } +} + +// [Public] +void ImGui::TableSetupScrollFreeze(int columns, int rows) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && "Need to call TableSetupColumn() after BeginTable()!"); + IM_ASSERT(table->IsLayoutLocked == false && "Need to call TableSetupColumn() before first row!"); + IM_ASSERT(columns >= 0 && columns < IMGUI_TABLE_MAX_COLUMNS); + IM_ASSERT(rows >= 0 && rows < 128); // Arbitrary limit + + table->FreezeColumnsRequest = (table->Flags & ImGuiTableFlags_ScrollX) ? (ImGuiTableColumnIdx)columns : 0; + table->FreezeColumnsCount = (table->InnerWindow->Scroll.x != 0.0f) ? table->FreezeColumnsRequest : 0; + table->FreezeRowsRequest = (table->Flags & ImGuiTableFlags_ScrollY) ? (ImGuiTableColumnIdx)rows : 0; + table->FreezeRowsCount = (table->InnerWindow->Scroll.y != 0.0f) ? table->FreezeRowsRequest : 0; + table->IsUnfrozenRows = (table->FreezeRowsCount == 0); // Make sure this is set before TableUpdateLayout() so ImGuiListClipper can benefit from it.b +} + +int ImGui::TableGetColumnCount() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + return table ? table->ColumnsCount : 0; +} + +const char* ImGui::TableGetColumnName(int column_n) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return NULL; + if (column_n < 0) + column_n = table->CurrentColumn; + return TableGetColumnName(table, column_n); +} + +const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n) +{ + if (table->IsLayoutLocked == false && column_n >= table->DeclColumnsCount) + return ""; // NameOffset is invalid at this point + const ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->NameOffset == -1) + return ""; + return &table->ColumnsNames.Buf[column->NameOffset]; +} + +// For the getter you can use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsEnabled) +void ImGui::TableSetColumnEnabled(int column_n, bool enabled) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL); + if (!table) + return; + if (column_n < 0) + column_n = table->CurrentColumn; + IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount); + ImGuiTableColumn* column = &table->Columns[column_n]; + column->IsEnabledNextFrame = enabled; +} + +// We allow querying for an extra column in order to poll the IsHovered state of the right-most section +ImGuiTableColumnFlags ImGui::TableGetColumnFlags(int column_n) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return ImGuiTableColumnFlags_None; + if (column_n < 0) + column_n = table->CurrentColumn; + if (column_n == table->ColumnsCount) + return (table->HoveredColumnBody == column_n) ? ImGuiTableColumnFlags_IsHovered : ImGuiTableColumnFlags_None; + return table->Columns[column_n].Flags; +} + +// Return the cell rectangle based on currently known height. +// - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations. +// The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it. +// - Important: if ImGuiTableFlags_PadOuterX is set but ImGuiTableFlags_PadInnerX is not set, the outer-most left and right +// columns report a small offset so their CellBgRect can extend up to the outer border. +ImRect ImGui::TableGetCellBgRect(const ImGuiTable* table, int column_n) +{ + const ImGuiTableColumn* column = &table->Columns[column_n]; + float x1 = column->MinX; + float x2 = column->MaxX; + if (column->PrevEnabledColumn == -1) + x1 -= table->CellSpacingX1; + if (column->NextEnabledColumn == -1) + x2 += table->CellSpacingX2; + return ImRect(x1, table->RowPosY1, x2, table->RowPosY2); +} + +// Return the resizing ID for the right-side of the given column. +ImGuiID ImGui::TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no) +{ + IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount); + ImGuiID id = table->ID + 1 + (instance_no * table->ColumnsCount) + column_n; + return id; +} + +// Return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. +int ImGui::TableGetHoveredColumn() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return -1; + return (int)table->HoveredColumnBody; +} + +void ImGui::TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(target != ImGuiTableBgTarget_None); + + if (color == IM_COL32_DISABLE) + color = 0; + + // We cannot draw neither the cell or row background immediately as we don't know the row height at this point in time. + switch (target) + { + case ImGuiTableBgTarget_CellBg: + { + if (table->RowPosY1 > table->InnerClipRect.Max.y) // Discard + return; + if (column_n == -1) + column_n = table->CurrentColumn; + if ((table->VisibleMaskByIndex & ((ImU64)1 << column_n)) == 0) + return; + if (table->RowCellDataCurrent < 0 || table->RowCellData[table->RowCellDataCurrent].Column != column_n) + table->RowCellDataCurrent++; + ImGuiTableCellData* cell_data = &table->RowCellData[table->RowCellDataCurrent]; + cell_data->BgColor = color; + cell_data->Column = (ImGuiTableColumnIdx)column_n; + break; + } + case ImGuiTableBgTarget_RowBg0: + case ImGuiTableBgTarget_RowBg1: + { + if (table->RowPosY1 > table->InnerClipRect.Max.y) // Discard + return; + IM_ASSERT(column_n == -1); + int bg_idx = (target == ImGuiTableBgTarget_RowBg1) ? 1 : 0; + table->RowBgColor[bg_idx] = color; + break; + } + default: + IM_ASSERT(0); + } +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Row changes +//------------------------------------------------------------------------- +// - TableGetRowIndex() +// - TableNextRow() +// - TableBeginRow() [Internal] +// - TableEndRow() [Internal] +//------------------------------------------------------------------------- + +// [Public] Note: for row coloring we use ->RowBgColorCounter which is the same value without counting header rows +int ImGui::TableGetRowIndex() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return 0; + return table->CurrentRow; +} + +// [Public] Starts into the first cell of a new row +void ImGui::TableNextRow(ImGuiTableRowFlags row_flags, float row_min_height) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + + if (!table->IsLayoutLocked) + TableUpdateLayout(table); + if (table->IsInsideRow) + TableEndRow(table); + + table->LastRowFlags = table->RowFlags; + table->RowFlags = row_flags; + table->RowMinHeight = row_min_height; + TableBeginRow(table); + + // We honor min_row_height requested by user, but cannot guarantee per-row maximum height, + // because that would essentially require a unique clipping rectangle per-cell. + table->RowPosY2 += table->CellPaddingY * 2.0f; + table->RowPosY2 = ImMax(table->RowPosY2, table->RowPosY1 + row_min_height); + + // Disable output until user calls TableNextColumn() + table->InnerWindow->SkipItems = true; +} + +// [Internal] Called by TableNextRow() +void ImGui::TableBeginRow(ImGuiTable* table) +{ + ImGuiWindow* window = table->InnerWindow; + IM_ASSERT(!table->IsInsideRow); + + // New row + table->CurrentRow++; + table->CurrentColumn = -1; + table->RowBgColor[0] = table->RowBgColor[1] = IM_COL32_DISABLE; + table->RowCellDataCurrent = -1; + table->IsInsideRow = true; + + // Begin frozen rows + float next_y1 = table->RowPosY2; + if (table->CurrentRow == 0 && table->FreezeRowsCount > 0) + next_y1 = window->DC.CursorPos.y = table->OuterRect.Min.y; + + table->RowPosY1 = table->RowPosY2 = next_y1; + table->RowTextBaseline = 0.0f; + table->RowIndentOffsetX = window->DC.Indent.x - table->HostIndentX; // Lock indent + window->DC.PrevLineTextBaseOffset = 0.0f; + window->DC.CursorMaxPos.y = next_y1; + + // Making the header BG color non-transparent will allow us to overlay it multiple times when handling smooth dragging. + if (table->RowFlags & ImGuiTableRowFlags_Headers) + { + TableSetBgColor(ImGuiTableBgTarget_RowBg0, GetColorU32(ImGuiCol_TableHeaderBg)); + if (table->CurrentRow == 0) + table->IsUsingHeaders = true; + } +} + +// [Internal] Called by TableNextRow() +void ImGui::TableEndRow(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(window == table->InnerWindow); + IM_ASSERT(table->IsInsideRow); + + if (table->CurrentColumn != -1) + TableEndCell(table); + + // Logging + if (g.LogEnabled) + LogRenderedText(NULL, "|"); + + // Position cursor at the bottom of our row so it can be used for e.g. clipping calculation. However it is + // likely that the next call to TableBeginCell() will reposition the cursor to take account of vertical padding. + window->DC.CursorPos.y = table->RowPosY2; + + // Row background fill + const float bg_y1 = table->RowPosY1; + const float bg_y2 = table->RowPosY2; + const bool unfreeze_rows_actual = (table->CurrentRow + 1 == table->FreezeRowsCount); + const bool unfreeze_rows_request = (table->CurrentRow + 1 == table->FreezeRowsRequest); + if (table->CurrentRow == 0) + table->LastFirstRowHeight = bg_y2 - bg_y1; + + const bool is_visible = (bg_y2 >= table->InnerClipRect.Min.y && bg_y1 <= table->InnerClipRect.Max.y); + if (is_visible) + { + // Decide of background color for the row + ImU32 bg_col0 = 0; + ImU32 bg_col1 = 0; + if (table->RowBgColor[0] != IM_COL32_DISABLE) + bg_col0 = table->RowBgColor[0]; + else if (table->Flags & ImGuiTableFlags_RowBg) + bg_col0 = GetColorU32((table->RowBgColorCounter & 1) ? ImGuiCol_TableRowBgAlt : ImGuiCol_TableRowBg); + if (table->RowBgColor[1] != IM_COL32_DISABLE) + bg_col1 = table->RowBgColor[1]; + + // Decide of top border color + ImU32 border_col = 0; + const float border_size = TABLE_BORDER_SIZE; + if (table->CurrentRow > 0 || table->InnerWindow == table->OuterWindow) + if (table->Flags & ImGuiTableFlags_BordersInnerH) + border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight; + + const bool draw_cell_bg_color = table->RowCellDataCurrent >= 0; + const bool draw_strong_bottom_border = unfreeze_rows_actual; + if ((bg_col0 | bg_col1 | border_col) != 0 || draw_strong_bottom_border || draw_cell_bg_color) + { + // In theory we could call SetWindowClipRectBeforeSetChannel() but since we know TableEndRow() is + // always followed by a change of clipping rectangle we perform the smallest overwrite possible here. + if ((table->Flags & ImGuiTableFlags_NoClip) == 0) + window->DrawList->_CmdHeader.ClipRect = table->Bg0ClipRectForDrawCmd.ToVec4(); + table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_BG0); + } + + // Draw row background + // We soft/cpu clip this so all backgrounds and borders can share the same clipping rectangle + if (bg_col0 || bg_col1) + { + ImRect row_rect(table->WorkRect.Min.x, bg_y1, table->WorkRect.Max.x, bg_y2); + row_rect.ClipWith(table->BgClipRect); + if (bg_col0 != 0 && row_rect.Min.y < row_rect.Max.y) + window->DrawList->AddRectFilled(row_rect.Min, row_rect.Max, bg_col0); + if (bg_col1 != 0 && row_rect.Min.y < row_rect.Max.y) + window->DrawList->AddRectFilled(row_rect.Min, row_rect.Max, bg_col1); + } + + // Draw cell background color + if (draw_cell_bg_color) + { + ImGuiTableCellData* cell_data_end = &table->RowCellData[table->RowCellDataCurrent]; + for (ImGuiTableCellData* cell_data = &table->RowCellData[0]; cell_data <= cell_data_end; cell_data++) + { + const ImGuiTableColumn* column = &table->Columns[cell_data->Column]; + ImRect cell_bg_rect = TableGetCellBgRect(table, cell_data->Column); + cell_bg_rect.ClipWith(table->BgClipRect); + cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped + cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->MaxX); + window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor); + } + } + + // Draw top border + if (border_col && bg_y1 >= table->BgClipRect.Min.y && bg_y1 < table->BgClipRect.Max.y) + window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y1), ImVec2(table->BorderX2, bg_y1), border_col, border_size); + + // Draw bottom border at the row unfreezing mark (always strong) + if (draw_strong_bottom_border && bg_y2 >= table->BgClipRect.Min.y && bg_y2 < table->BgClipRect.Max.y) + window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y2), ImVec2(table->BorderX2, bg_y2), table->BorderColorStrong, border_size); + } + + // End frozen rows (when we are past the last frozen row line, teleport cursor and alter clipping rectangle) + // We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark end of row and + // get the new cursor position. + if (unfreeze_rows_request) + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + column->NavLayerCurrent = (ImS8)((column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main); + } + if (unfreeze_rows_actual) + { + IM_ASSERT(table->IsUnfrozenRows == false); + table->IsUnfrozenRows = true; + + // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect + float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); + table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); + table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y; + table->Bg2DrawChannelCurrent = table->Bg2DrawChannelUnfrozen; + IM_ASSERT(table->Bg2ClipRectForDrawCmd.Min.y <= table->Bg2ClipRectForDrawCmd.Max.y); + + float row_height = table->RowPosY2 - table->RowPosY1; + table->RowPosY2 = window->DC.CursorPos.y = table->WorkRect.Min.y + table->RowPosY2 - table->OuterRect.Min.y; + table->RowPosY1 = table->RowPosY2 - row_height; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + column->DrawChannelCurrent = column->DrawChannelUnfrozen; + column->ClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y; + } + + // Update cliprect ahead of TableBeginCell() so clipper can access to new ClipRect->Min.y + SetWindowClipRectBeforeSetChannel(window, table->Columns[0].ClipRect); + table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent); + } + + if (!(table->RowFlags & ImGuiTableRowFlags_Headers)) + table->RowBgColorCounter++; + table->IsInsideRow = false; +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Columns changes +//------------------------------------------------------------------------- +// - TableGetColumnIndex() +// - TableSetColumnIndex() +// - TableNextColumn() +// - TableBeginCell() [Internal] +// - TableEndCell() [Internal] +//------------------------------------------------------------------------- + +int ImGui::TableGetColumnIndex() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return 0; + return table->CurrentColumn; +} + +// [Public] Append into a specific column +bool ImGui::TableSetColumnIndex(int column_n) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return false; + + if (table->CurrentColumn != column_n) + { + if (table->CurrentColumn != -1) + TableEndCell(table); + IM_ASSERT(column_n >= 0 && table->ColumnsCount); + TableBeginCell(table, column_n); + } + + // Return whether the column is visible. User may choose to skip submitting items based on this return value, + // however they shouldn't skip submitting for columns that may have the tallest contribution to row height. + return (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n)) != 0; +} + +// [Public] Append into the next column, wrap and create a new row when already on last column +bool ImGui::TableNextColumn() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return false; + + if (table->IsInsideRow && table->CurrentColumn + 1 < table->ColumnsCount) + { + if (table->CurrentColumn != -1) + TableEndCell(table); + TableBeginCell(table, table->CurrentColumn + 1); + } + else + { + TableNextRow(); + TableBeginCell(table, 0); + } + + // Return whether the column is visible. User may choose to skip submitting items based on this return value, + // however they shouldn't skip submitting for columns that may have the tallest contribution to row height. + int column_n = table->CurrentColumn; + return (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n)) != 0; +} + + +// [Internal] Called by TableSetColumnIndex()/TableNextColumn() +// This is called very frequently, so we need to be mindful of unnecessary overhead. +// FIXME-TABLE FIXME-OPT: Could probably shortcut some things for non-active or clipped columns. +void ImGui::TableBeginCell(ImGuiTable* table, int column_n) +{ + ImGuiTableColumn* column = &table->Columns[column_n]; + ImGuiWindow* window = table->InnerWindow; + table->CurrentColumn = column_n; + + // Start position is roughly ~~ CellRect.Min + CellPadding + Indent + float start_x = column->WorkMinX; + if (column->Flags & ImGuiTableColumnFlags_IndentEnable) + start_x += table->RowIndentOffsetX; // ~~ += window.DC.Indent.x - table->HostIndentX, except we locked it for the row. + + window->DC.CursorPos.x = start_x; + window->DC.CursorPos.y = table->RowPosY1 + table->CellPaddingY; + window->DC.CursorMaxPos.x = window->DC.CursorPos.x; + window->DC.ColumnsOffset.x = start_x - window->Pos.x - window->DC.Indent.x; // FIXME-WORKRECT + window->DC.CurrLineTextBaseOffset = table->RowTextBaseline; + window->DC.NavLayerCurrent = (ImGuiNavLayer)column->NavLayerCurrent; + + window->WorkRect.Min.y = window->DC.CursorPos.y; + window->WorkRect.Min.x = column->WorkMinX; + window->WorkRect.Max.x = column->WorkMaxX; + window->DC.ItemWidth = column->ItemWidth; + + // To allow ImGuiListClipper to function we propagate our row height + if (!column->IsEnabled) + window->DC.CursorPos.y = ImMax(window->DC.CursorPos.y, table->RowPosY2); + + window->SkipItems = column->IsSkipItems; + if (column->IsSkipItems) + { + window->DC.LastItemId = 0; + window->DC.LastItemStatusFlags = 0; + } + + if (table->Flags & ImGuiTableFlags_NoClip) + { + // FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed. + table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); + //IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_NOCLIP); + } + else + { + // FIXME-TABLE: Could avoid this if draw channel is dummy channel? + SetWindowClipRectBeforeSetChannel(window, column->ClipRect); + table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); + } + + // Logging + ImGuiContext& g = *GImGui; + if (g.LogEnabled && !column->IsSkipItems) + { + LogRenderedText(&window->DC.CursorPos, "|"); + g.LogLinePosY = FLT_MAX; + } +} + +// [Internal] Called by TableNextRow()/TableSetColumnIndex()/TableNextColumn() +void ImGui::TableEndCell(ImGuiTable* table) +{ + ImGuiTableColumn* column = &table->Columns[table->CurrentColumn]; + ImGuiWindow* window = table->InnerWindow; + + // Report maximum position so we can infer content size per column. + float* p_max_pos_x; + if (table->RowFlags & ImGuiTableRowFlags_Headers) + p_max_pos_x = &column->ContentMaxXHeadersUsed; // Useful in case user submit contents in header row that is not a TableHeader() call + else + p_max_pos_x = table->IsUnfrozenRows ? &column->ContentMaxXUnfrozen : &column->ContentMaxXFrozen; + *p_max_pos_x = ImMax(*p_max_pos_x, window->DC.CursorMaxPos.x); + table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY); + column->ItemWidth = window->DC.ItemWidth; + + // Propagate text baseline for the entire row + // FIXME-TABLE: Here we propagate text baseline from the last line of the cell.. instead of the first one. + table->RowTextBaseline = ImMax(table->RowTextBaseline, window->DC.PrevLineTextBaseOffset); +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Columns width management +//------------------------------------------------------------------------- +// - TableGetMaxColumnWidth() [Internal] +// - TableGetColumnWidthAuto() [Internal] +// - TableSetColumnWidth() +// - TableSetColumnWidthAutoSingle() [Internal] +// - TableSetColumnWidthAutoAll() [Internal] +// - TableUpdateColumnsWeightFromWidth() [Internal] +//------------------------------------------------------------------------- + +// Maximum column content width given current layout. Use column->MinX so this value on a per-column basis. +float ImGui::TableGetMaxColumnWidth(const ImGuiTable* table, int column_n) +{ + const ImGuiTableColumn* column = &table->Columns[column_n]; + float max_width = FLT_MAX; + const float min_column_distance = table->MinColumnWidth + table->CellPaddingX * 2.0f + table->CellSpacingX1 + table->CellSpacingX2; + if (table->Flags & ImGuiTableFlags_ScrollX) + { + // Frozen columns can't reach beyond visible width else scrolling will naturally break. + if (column->DisplayOrder < table->FreezeColumnsRequest) + { + max_width = (table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - column->DisplayOrder) * min_column_distance) - column->MinX; + max_width = max_width - table->OuterPaddingX - table->CellPaddingX - table->CellSpacingX2; + } + } + else if ((table->Flags & ImGuiTableFlags_NoKeepColumnsVisible) == 0) + { + // If horizontal scrolling if disabled, we apply a final lossless shrinking of columns in order to make + // sure they are all visible. Because of this we also know that all of the columns will always fit in + // table->WorkRect and therefore in table->InnerRect (because ScrollX is off) + // FIXME-TABLE: This is solved incorrectly but also quite a difficult problem to fix as we also want ClipRect width to match. + // See "table_width_distrib" and "table_width_keep_visible" tests + max_width = table->WorkRect.Max.x - (table->ColumnsEnabledCount - column->IndexWithinEnabledSet - 1) * min_column_distance - column->MinX; + //max_width -= table->CellSpacingX1; + max_width -= table->CellSpacingX2; + max_width -= table->CellPaddingX * 2.0f; + max_width -= table->OuterPaddingX; + } + return max_width; +} + +// Note this is meant to be stored in column->WidthAuto, please generally use the WidthAuto field +float ImGui::TableGetColumnWidthAuto(ImGuiTable* table, ImGuiTableColumn* column) +{ + const float content_width_body = ImMax(column->ContentMaxXFrozen, column->ContentMaxXUnfrozen) - column->WorkMinX; + const float content_width_headers = column->ContentMaxXHeadersIdeal - column->WorkMinX; + float width_auto = content_width_body; + if (!(column->Flags & ImGuiTableColumnFlags_NoHeaderWidth)) + width_auto = ImMax(width_auto, content_width_headers); + + // Non-resizable fixed columns preserve their requested width + if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && column->InitStretchWeightOrWidth > 0.0f) + if (!(table->Flags & ImGuiTableFlags_Resizable) || (column->Flags & ImGuiTableColumnFlags_NoResize)) + width_auto = column->InitStretchWeightOrWidth; + + return ImMax(width_auto, table->MinColumnWidth); +} + +// 'width' = inner column width, without padding +void ImGui::TableSetColumnWidth(int column_n, float width) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && table->IsLayoutLocked == false); + IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount); + ImGuiTableColumn* column_0 = &table->Columns[column_n]; + float column_0_width = width; + + // Apply constraints early + // Compare both requested and actual given width to avoid overwriting requested width when column is stuck (minimum size, bounded) + IM_ASSERT(table->MinColumnWidth > 0.0f); + const float min_width = table->MinColumnWidth; + const float max_width = ImMax(min_width, TableGetMaxColumnWidth(table, column_n)); + column_0_width = ImClamp(column_0_width, min_width, max_width); + if (column_0->WidthGiven == column_0_width || column_0->WidthRequest == column_0_width) + return; + + //IMGUI_DEBUG_LOG("TableSetColumnWidth(%d, %.1f->%.1f)\n", column_0_idx, column_0->WidthGiven, column_0_width); + ImGuiTableColumn* column_1 = (column_0->NextEnabledColumn != -1) ? &table->Columns[column_0->NextEnabledColumn] : NULL; + + // In this surprisingly not simple because of how we support mixing Fixed and multiple Stretch columns. + // - All fixed: easy. + // - All stretch: easy. + // - One or more fixed + one stretch: easy. + // - One or more fixed + more than one stretch: tricky. + // Qt when manual resize is enabled only support a single _trailing_ stretch column. + + // When forwarding resize from Wn| to Fn+1| we need to be considerate of the _NoResize flag on Fn+1. + // FIXME-TABLE: Find a way to rewrite all of this so interactions feel more consistent for the user. + // Scenarios: + // - F1 F2 F3 resize from F1| or F2| --> ok: alter ->WidthRequested of Fixed column. Subsequent columns will be offset. + // - F1 F2 F3 resize from F3| --> ok: alter ->WidthRequested of Fixed column. If active, ScrollX extent can be altered. + // - F1 F2 W3 resize from F1| or F2| --> ok: alter ->WidthRequested of Fixed column. If active, ScrollX extent can be altered, but it doesn't make much sense as the Stretch column will always be minimal size. + // - F1 F2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1) + // - W1 W2 W3 resize from W1| or W2| --> ok + // - W1 W2 W3 resize from W3| --> ok: no-op (disabled by Resize Rule 1) + // - W1 F2 F3 resize from F3| --> ok: no-op (disabled by Resize Rule 1) + // - W1 F2 resize from F2| --> ok: no-op (disabled by Resize Rule 1) + // - W1 W2 F3 resize from W1| or W2| --> ok + // - W1 F2 W3 resize from W1| or F2| --> ok + // - F1 W2 F3 resize from W2| --> ok + // - F1 W3 F2 resize from W3| --> ok + // - W1 F2 F3 resize from W1| --> ok: equivalent to resizing |F2. F3 will not move. + // - W1 F2 F3 resize from F2| --> ok + // All resizes from a Wx columns are locking other columns. + + // Possible improvements: + // - W1 W2 W3 resize W1| --> to not be stuck, both W2 and W3 would stretch down. Seems possible to fix. Would be most beneficial to simplify resize of all-weighted columns. + // - W3 F1 F2 resize W3| --> to not be stuck past F1|, both F1 and F2 would need to stretch down, which would be lossy or ambiguous. Seems hard to fix. + + // [Resize Rule 1] Can't resize from right of right-most visible column if there is any Stretch column. Implemented in TableUpdateLayout(). + + // If we have all Fixed columns OR resizing a Fixed column that doesn't come after a Stretch one, we can do an offsetting resize. + // This is the preferred resize path + if (column_0->Flags & ImGuiTableColumnFlags_WidthFixed) + if (!column_1 || table->LeftMostStretchedColumn == -1 || table->Columns[table->LeftMostStretchedColumn].DisplayOrder >= column_0->DisplayOrder) + { + column_0->WidthRequest = column_0_width; + table->IsSettingsDirty = true; + return; + } + + // We can also use previous column if there's no next one (this is used when doing an auto-fit on the right-most stretch column) + if (column_1 == NULL) + column_1 = (column_0->PrevEnabledColumn != -1) ? &table->Columns[column_0->PrevEnabledColumn] : NULL; + if (column_1 == NULL) + return; + + // Resizing from right-side of a Stretch column before a Fixed column forward sizing to left-side of fixed column. + // (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b) + float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width); + column_0_width = column_0->WidthRequest + column_1->WidthRequest - column_1_width; + IM_ASSERT(column_0_width > 0.0f && column_1_width > 0.0f); + column_0->WidthRequest = column_0_width; + column_1->WidthRequest = column_1_width; + if ((column_0->Flags | column_1->Flags) & ImGuiTableColumnFlags_WidthStretch) + TableUpdateColumnsWeightFromWidth(table); + table->IsSettingsDirty = true; +} + +// Disable clipping then auto-fit, will take 2 frames +// (we don't take a shortcut for unclipped columns to reduce inconsistencies when e.g. resizing multiple columns) +void ImGui::TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n) +{ + // Single auto width uses auto-fit + ImGuiTableColumn* column = &table->Columns[column_n]; + if (!column->IsEnabled) + return; + column->CannotSkipItemsQueue = (1 << 0); + table->AutoFitSingleColumn = (ImGuiTableColumnIdx)column_n; +} + +void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table) +{ + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (!column->IsEnabled && !(column->Flags & ImGuiTableColumnFlags_WidthStretch)) // Cannot reset weight of hidden stretch column + continue; + column->CannotSkipItemsQueue = (1 << 0); + column->AutoFitQueue = (1 << 1); + } +} + +void ImGui::TableUpdateColumnsWeightFromWidth(ImGuiTable* table) +{ + IM_ASSERT(table->LeftMostStretchedColumn != -1 && table->RightMostStretchedColumn != -1); + + // Measure existing quantity + float visible_weight = 0.0f; + float visible_width = 0.0f; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (!column->IsEnabled || !(column->Flags & ImGuiTableColumnFlags_WidthStretch)) + continue; + IM_ASSERT(column->StretchWeight > 0.0f); + visible_weight += column->StretchWeight; + visible_width += column->WidthRequest; + } + IM_ASSERT(visible_weight > 0.0f && visible_width > 0.0f); + + // Apply new weights + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (!column->IsEnabled || !(column->Flags & ImGuiTableColumnFlags_WidthStretch)) + continue; + column->StretchWeight = (column->WidthRequest / visible_width) * visible_weight; + IM_ASSERT(column->StretchWeight > 0.0f); + } +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Drawing +//------------------------------------------------------------------------- +// - TablePushBackgroundChannel() [Internal] +// - TablePopBackgroundChannel() [Internal] +// - TableSetupDrawChannels() [Internal] +// - TableMergeDrawChannels() [Internal] +// - TableDrawBorders() [Internal] +//------------------------------------------------------------------------- + +// Bg2 is used by Selectable (and possibly other widgets) to render to the background. +// Unlike our Bg0/1 channel which we uses for RowBg/CellBg/Borders and where we guarantee all shapes to be CPU-clipped, the Bg2 channel being widgets-facing will rely on regular ClipRect. +void ImGui::TablePushBackgroundChannel() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiTable* table = g.CurrentTable; + + // Optimization: avoid SetCurrentChannel() + PushClipRect() + table->HostBackupInnerClipRect = window->ClipRect; + SetWindowClipRectBeforeSetChannel(window, table->Bg2ClipRectForDrawCmd); + table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Bg2DrawChannelCurrent); +} + +void ImGui::TablePopBackgroundChannel() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiTable* table = g.CurrentTable; + ImGuiTableColumn* column = &table->Columns[table->CurrentColumn]; + + // Optimization: avoid PopClipRect() + SetCurrentChannel() + SetWindowClipRectBeforeSetChannel(window, table->HostBackupInnerClipRect); + table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); +} + +// Allocate draw channels. Called by TableUpdateLayout() +// - We allocate them following storage order instead of display order so reordering columns won't needlessly +// increase overall dormant memory cost. +// - We isolate headers draw commands in their own channels instead of just altering clip rects. +// This is in order to facilitate merging of draw commands. +// - After crossing FreezeRowsCount, all columns see their current draw channel changed to a second set of channels. +// - We only use the dummy draw channel so we can push a null clipping rectangle into it without affecting other +// channels, while simplifying per-row/per-cell overhead. It will be empty and discarded when merged. +// - We allocate 1 or 2 background draw channels. This is because we know TablePushBackgroundChannel() is only used for +// horizontal spanning. If we allowed vertical spanning we'd need one background draw channel per merge group (1-4). +// Draw channel allocation (before merging): +// - NoClip --> 2+D+1 channels: bg0/1 + bg2 + foreground (same clip rect == always 1 draw call) +// - Clip --> 2+D+N channels +// - FreezeRows --> 2+D+N*2 (unless scrolling value is zero) +// - FreezeRows || FreezeColunns --> 3+D+N*2 (unless scrolling value is zero) +// Where D is 1 if any column is clipped or hidden (dummy channel) otherwise 0. +void ImGui::TableSetupDrawChannels(ImGuiTable* table) +{ + const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1; + const int channels_for_row = (table->Flags & ImGuiTableFlags_NoClip) ? 1 : table->ColumnsEnabledCount; + const int channels_for_bg = 1 + 1 * freeze_row_multiplier; + const int channels_for_dummy = (table->ColumnsEnabledCount < table->ColumnsCount || table->VisibleMaskByIndex != table->EnabledMaskByIndex) ? +1 : 0; + const int channels_total = channels_for_bg + (channels_for_row * freeze_row_multiplier) + channels_for_dummy; + table->DrawSplitter.Split(table->InnerWindow->DrawList, channels_total); + table->DummyDrawChannel = (ImGuiTableDrawChannelIdx)((channels_for_dummy > 0) ? channels_total - 1 : -1); + table->Bg2DrawChannelCurrent = TABLE_DRAW_CHANNEL_BG2_FROZEN; + table->Bg2DrawChannelUnfrozen = (ImGuiTableDrawChannelIdx)((table->FreezeRowsCount > 0) ? 2 + channels_for_row : TABLE_DRAW_CHANNEL_BG2_FROZEN); + + int draw_channel_current = 2; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->IsVisibleX && column->IsVisibleY) + { + column->DrawChannelFrozen = (ImGuiTableDrawChannelIdx)(draw_channel_current); + column->DrawChannelUnfrozen = (ImGuiTableDrawChannelIdx)(draw_channel_current + (table->FreezeRowsCount > 0 ? channels_for_row + 1 : 0)); + if (!(table->Flags & ImGuiTableFlags_NoClip)) + draw_channel_current++; + } + else + { + column->DrawChannelFrozen = column->DrawChannelUnfrozen = table->DummyDrawChannel; + } + column->DrawChannelCurrent = column->DrawChannelFrozen; + } + + // Initial draw cmd starts with a BgClipRect that matches the one of its host, to facilitate merge draw commands by default. + // All our cell highlight are manually clipped with BgClipRect. When unfreezing it will be made smaller to fit scrolling rect. + // (This technically isn't part of setting up draw channels, but is reasonably related to be done here) + table->BgClipRect = table->InnerClipRect; + table->Bg0ClipRectForDrawCmd = table->OuterWindow->ClipRect; + table->Bg2ClipRectForDrawCmd = table->HostClipRect; + IM_ASSERT(table->BgClipRect.Min.y <= table->BgClipRect.Max.y); +} + +// This function reorder draw channels based on matching clip rectangle, to facilitate merging them. Called by EndTable(). +// For simplicity we call it TableMergeDrawChannels() but in fact it only reorder channels + overwrite ClipRect, +// actual merging is done by table->DrawSplitter.Merge() which is called right after TableMergeDrawChannels(). +// +// Columns where the contents didn't stray off their local clip rectangle can be merged. To achieve +// this we merge their clip rect and make them contiguous in the channel list, so they can be merged +// by the call to DrawSplitter.Merge() following to the call to this function. +// We reorder draw commands by arranging them into a maximum of 4 distinct groups: +// +// 1 group: 2 groups: 2 groups: 4 groups: +// [ 0. ] no freeze [ 0. ] row freeze [ 01 ] col freeze [ 01 ] row+col freeze +// [ .. ] or no scroll [ 2. ] and v-scroll [ .. ] and h-scroll [ 23 ] and v+h-scroll +// +// Each column itself can use 1 channel (row freeze disabled) or 2 channels (row freeze enabled). +// When the contents of a column didn't stray off its limit, we move its channels into the corresponding group +// based on its position (within frozen rows/columns groups or not). +// At the end of the operation our 1-4 groups will each have a ImDrawCmd using the same ClipRect. +// This function assume that each column are pointing to a distinct draw channel, +// otherwise merge_group->ChannelsCount will not match set bit count of merge_group->ChannelsMask. +// +// Column channels will not be merged into one of the 1-4 groups in the following cases: +// - The contents stray off its clipping rectangle (we only compare the MaxX value, not the MinX value). +// Direct ImDrawList calls won't be taken into account by default, if you use them make sure the ImGui:: bounds +// matches, by e.g. calling SetCursorScreenPos(). +// - The channel uses more than one draw command itself. We drop all our attempt at merging stuff here.. +// we could do better but it's going to be rare and probably not worth the hassle. +// Columns for which the draw channel(s) haven't been merged with other will use their own ImDrawCmd. +// +// This function is particularly tricky to understand.. take a breath. +void ImGui::TableMergeDrawChannels(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + ImDrawListSplitter* splitter = &table->DrawSplitter; + const bool has_freeze_v = (table->FreezeRowsCount > 0); + const bool has_freeze_h = (table->FreezeColumnsCount > 0); + IM_ASSERT(splitter->_Current == 0); + + // Track which groups we are going to attempt to merge, and which channels goes into each group. + struct MergeGroup + { + ImRect ClipRect; + int ChannelsCount; + ImBitArray ChannelsMask; + }; + int merge_group_mask = 0x00; + MergeGroup merge_groups[4]; + memset(merge_groups, 0, sizeof(merge_groups)); + + // 1. Scan channels and take note of those which can be merged + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + if ((table->VisibleMaskByIndex & ((ImU64)1 << column_n)) == 0) + continue; + ImGuiTableColumn* column = &table->Columns[column_n]; + + const int merge_group_sub_count = has_freeze_v ? 2 : 1; + for (int merge_group_sub_n = 0; merge_group_sub_n < merge_group_sub_count; merge_group_sub_n++) + { + const int channel_no = (merge_group_sub_n == 0) ? column->DrawChannelFrozen : column->DrawChannelUnfrozen; + + // Don't attempt to merge if there are multiple draw calls within the column + ImDrawChannel* src_channel = &splitter->_Channels[channel_no]; + if (src_channel->_CmdBuffer.Size > 0 && src_channel->_CmdBuffer.back().ElemCount == 0) + src_channel->_CmdBuffer.pop_back(); + if (src_channel->_CmdBuffer.Size != 1) + continue; + + // Find out the width of this merge group and check if it will fit in our column + // (note that we assume that rendering didn't stray on the left direction. we should need a CursorMinPos to detect it) + if (!(column->Flags & ImGuiTableColumnFlags_NoClip)) + { + float content_max_x; + if (!has_freeze_v) + content_max_x = ImMax(column->ContentMaxXUnfrozen, column->ContentMaxXHeadersUsed); // No row freeze + else if (merge_group_sub_n == 0) + content_max_x = ImMax(column->ContentMaxXFrozen, column->ContentMaxXHeadersUsed); // Row freeze: use width before freeze + else + content_max_x = column->ContentMaxXUnfrozen; // Row freeze: use width after freeze + if (content_max_x > column->ClipRect.Max.x) + continue; + } + + const int merge_group_n = (has_freeze_h && column_n < table->FreezeColumnsCount ? 0 : 1) + (has_freeze_v && merge_group_sub_n == 0 ? 0 : 2); + IM_ASSERT(channel_no < IMGUI_TABLE_MAX_DRAW_CHANNELS); + MergeGroup* merge_group = &merge_groups[merge_group_n]; + if (merge_group->ChannelsCount == 0) + merge_group->ClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX); + merge_group->ChannelsMask.SetBit(channel_no); + merge_group->ChannelsCount++; + merge_group->ClipRect.Add(src_channel->_CmdBuffer[0].ClipRect); + merge_group_mask |= (1 << merge_group_n); + } + + // Invalidate current draw channel + // (we don't clear DrawChannelFrozen/DrawChannelUnfrozen solely to facilitate debugging/later inspection of data) + column->DrawChannelCurrent = (ImGuiTableDrawChannelIdx)-1; + } + + // [DEBUG] Display merge groups +#if 0 + if (g.IO.KeyShift) + for (int merge_group_n = 0; merge_group_n < IM_ARRAYSIZE(merge_groups); merge_group_n++) + { + MergeGroup* merge_group = &merge_groups[merge_group_n]; + if (merge_group->ChannelsCount == 0) + continue; + char buf[32]; + ImFormatString(buf, 32, "MG%d:%d", merge_group_n, merge_group->ChannelsCount); + ImVec2 text_pos = merge_group->ClipRect.Min + ImVec2(4, 4); + ImVec2 text_size = CalcTextSize(buf, NULL); + GetForegroundDrawList()->AddRectFilled(text_pos, text_pos + text_size, IM_COL32(0, 0, 0, 255)); + GetForegroundDrawList()->AddText(text_pos, IM_COL32(255, 255, 0, 255), buf, NULL); + GetForegroundDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 255, 0, 255)); + } +#endif + + // 2. Rewrite channel list in our preferred order + if (merge_group_mask != 0) + { + // We skip channel 0 (Bg0/Bg1) and 1 (Bg2 frozen) from the shuffling since they won't move - see channels allocation in TableSetupDrawChannels(). + const int LEADING_DRAW_CHANNELS = 2; + g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized + ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data; + ImBitArray remaining_mask; // We need 132-bit of storage + remaining_mask.ClearAllBits(); + remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count); + remaining_mask.ClearBit(table->Bg2DrawChannelUnfrozen); + IM_ASSERT(has_freeze_v == false || table->Bg2DrawChannelUnfrozen != TABLE_DRAW_CHANNEL_BG2_FROZEN); + int remaining_count = splitter->_Count - (has_freeze_v ? LEADING_DRAW_CHANNELS + 1 : LEADING_DRAW_CHANNELS); + //ImRect host_rect = (table->InnerWindow == table->OuterWindow) ? table->InnerClipRect : table->HostClipRect; + ImRect host_rect = table->HostClipRect; + for (int merge_group_n = 0; merge_group_n < IM_ARRAYSIZE(merge_groups); merge_group_n++) + { + if (int merge_channels_count = merge_groups[merge_group_n].ChannelsCount) + { + MergeGroup* merge_group = &merge_groups[merge_group_n]; + ImRect merge_clip_rect = merge_group->ClipRect; + + // Extend outer-most clip limits to match those of host, so draw calls can be merged even if + // outer-most columns have some outer padding offsetting them from their parent ClipRect. + // The principal cases this is dealing with are: + // - On a same-window table (not scrolling = single group), all fitting columns ClipRect -> will extend and match host ClipRect -> will merge + // - Columns can use padding and have left-most ClipRect.Min.x and right-most ClipRect.Max.x != from host ClipRect -> will extend and match host ClipRect -> will merge + // FIXME-TABLE FIXME-WORKRECT: We are wasting a merge opportunity on tables without scrolling if column doesn't fit + // within host clip rect, solely because of the half-padding difference between window->WorkRect and window->InnerClipRect. + if ((merge_group_n & 1) == 0 || !has_freeze_h) + merge_clip_rect.Min.x = ImMin(merge_clip_rect.Min.x, host_rect.Min.x); + if ((merge_group_n & 2) == 0 || !has_freeze_v) + merge_clip_rect.Min.y = ImMin(merge_clip_rect.Min.y, host_rect.Min.y); + if ((merge_group_n & 1) != 0) + merge_clip_rect.Max.x = ImMax(merge_clip_rect.Max.x, host_rect.Max.x); + if ((merge_group_n & 2) != 0 && (table->Flags & ImGuiTableFlags_NoHostExtendY) == 0) + merge_clip_rect.Max.y = ImMax(merge_clip_rect.Max.y, host_rect.Max.y); +#if 0 + GetOverlayDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, ~0, 1.0f); + GetOverlayDrawList()->AddLine(merge_group->ClipRect.Min, merge_clip_rect.Min, IM_COL32(255, 100, 0, 200)); + GetOverlayDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200)); +#endif + remaining_count -= merge_group->ChannelsCount; + for (int n = 0; n < IM_ARRAYSIZE(remaining_mask.Storage); n++) + remaining_mask.Storage[n] &= ~merge_group->ChannelsMask.Storage[n]; + for (int n = 0; n < splitter->_Count && merge_channels_count != 0; n++) + { + // Copy + overwrite new clip rect + if (!merge_group->ChannelsMask.TestBit(n)) + continue; + merge_group->ChannelsMask.ClearBit(n); + merge_channels_count--; + + ImDrawChannel* channel = &splitter->_Channels[n]; + IM_ASSERT(channel->_CmdBuffer.Size == 1 && merge_clip_rect.Contains(ImRect(channel->_CmdBuffer[0].ClipRect))); + channel->_CmdBuffer[0].ClipRect = merge_clip_rect.ToVec4(); + memcpy(dst_tmp++, channel, sizeof(ImDrawChannel)); + } + } + + // Make sure Bg2DrawChannelUnfrozen appears in the middle of our groups (whereas Bg0/Bg1 and Bg2 frozen are fixed to 0 and 1) + if (merge_group_n == 1 && has_freeze_v) + memcpy(dst_tmp++, &splitter->_Channels[table->Bg2DrawChannelUnfrozen], sizeof(ImDrawChannel)); + } + + // Append unmergeable channels that we didn't reorder at the end of the list + for (int n = 0; n < splitter->_Count && remaining_count != 0; n++) + { + if (!remaining_mask.TestBit(n)) + continue; + ImDrawChannel* channel = &splitter->_Channels[n]; + memcpy(dst_tmp++, channel, sizeof(ImDrawChannel)); + remaining_count--; + } + IM_ASSERT(dst_tmp == g.DrawChannelsTempMergeBuffer.Data + g.DrawChannelsTempMergeBuffer.Size); + memcpy(splitter->_Channels.Data + LEADING_DRAW_CHANNELS, g.DrawChannelsTempMergeBuffer.Data, (splitter->_Count - LEADING_DRAW_CHANNELS) * sizeof(ImDrawChannel)); + } +} + +// FIXME-TABLE: This is a mess, need to redesign how we render borders (as some are also done in TableEndRow) +void ImGui::TableDrawBorders(ImGuiTable* table) +{ + ImGuiWindow* inner_window = table->InnerWindow; + if (!table->OuterWindow->ClipRect.Overlaps(table->OuterRect)) + return; + + ImDrawList* inner_drawlist = inner_window->DrawList; + table->DrawSplitter.SetCurrentChannel(inner_drawlist, TABLE_DRAW_CHANNEL_BG0); + inner_drawlist->PushClipRect(table->Bg0ClipRectForDrawCmd.Min, table->Bg0ClipRectForDrawCmd.Max, false); + + // Draw inner border and resizing feedback + const float border_size = TABLE_BORDER_SIZE; + const float draw_y1 = table->InnerRect.Min.y; + const float draw_y2_body = table->InnerRect.Max.y; + const float draw_y2_head = table->IsUsingHeaders ? ImMin(table->InnerRect.Max.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table->LastFirstRowHeight) : draw_y1; + if (table->Flags & ImGuiTableFlags_BordersInnerV) + { + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + { + if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + continue; + + const int column_n = table->DisplayOrderToIndex[order_n]; + ImGuiTableColumn* column = &table->Columns[column_n]; + const bool is_hovered = (table->HoveredColumnBorder == column_n); + const bool is_resized = (table->ResizedColumn == column_n) && (table->InstanceInteracted == table->InstanceCurrent); + const bool is_resizable = (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_)) == 0; + const bool is_frozen_separator = (table->FreezeColumnsCount != -1 && table->FreezeColumnsCount == order_n + 1); + if (column->MaxX > table->InnerClipRect.Max.x && !is_resized) + continue; + + // Decide whether right-most column is visible + if (column->NextEnabledColumn == -1 && !is_resizable) + if ((table->Flags & ImGuiTableFlags_SizingMask_) != ImGuiTableFlags_SizingFixedSame || (table->Flags & ImGuiTableFlags_NoHostExtendX)) + continue; + if (column->MaxX <= column->ClipRect.Min.x) // FIXME-TABLE FIXME-STYLE: Assume BorderSize==1, this is problematic if we want to increase the border size.. + continue; + + // Draw in outer window so right-most column won't be clipped + // Always draw full height border when being resized/hovered, or on the delimitation of frozen column scrolling. + ImU32 col; + float draw_y2; + if (is_hovered || is_resized || is_frozen_separator) + { + draw_y2 = draw_y2_body; + col = is_resized ? GetColorU32(ImGuiCol_SeparatorActive) : is_hovered ? GetColorU32(ImGuiCol_SeparatorHovered) : table->BorderColorStrong; + } + else + { + draw_y2 = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? draw_y2_head : draw_y2_body; + col = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? table->BorderColorStrong : table->BorderColorLight; + } + + if (draw_y2 > draw_y1) + inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), col, border_size); + } + } + + // Draw outer border + // FIXME: could use AddRect or explicit VLine/HLine helper? + if (table->Flags & ImGuiTableFlags_BordersOuter) + { + // Display outer border offset by 1 which is a simple way to display it without adding an extra draw call + // (Without the offset, in outer_window it would be rendered behind cells, because child windows are above their + // parent. In inner_window, it won't reach out over scrollbars. Another weird solution would be to display part + // of it in inner window, and the part that's over scrollbars in the outer window..) + // Either solution currently won't allow us to use a larger border size: the border would clipped. + const ImRect outer_border = table->OuterRect; + const ImU32 outer_col = table->BorderColorStrong; + if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter) + { + inner_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, ~0, border_size); + } + else if (table->Flags & ImGuiTableFlags_BordersOuterV) + { + inner_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Min.x, outer_border.Max.y), outer_col, border_size); + inner_drawlist->AddLine(ImVec2(outer_border.Max.x, outer_border.Min.y), outer_border.Max, outer_col, border_size); + } + else if (table->Flags & ImGuiTableFlags_BordersOuterH) + { + inner_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Max.x, outer_border.Min.y), outer_col, border_size); + inner_drawlist->AddLine(ImVec2(outer_border.Min.x, outer_border.Max.y), outer_border.Max, outer_col, border_size); + } + } + if ((table->Flags & ImGuiTableFlags_BordersInnerH) && table->RowPosY2 < table->OuterRect.Max.y) + { + // Draw bottom-most row border + const float border_y = table->RowPosY2; + if (border_y >= table->BgClipRect.Min.y && border_y < table->BgClipRect.Max.y) + inner_drawlist->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), table->BorderColorLight, border_size); + } + + inner_drawlist->PopClipRect(); +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Sorting +//------------------------------------------------------------------------- +// - TableGetSortSpecs() +// - TableFixColumnSortDirection() [Internal] +// - TableGetColumnNextSortDirection() [Internal] +// - TableSetColumnSortDirection() [Internal] +// - TableSortSpecsSanitize() [Internal] +// - TableSortSpecsBuild() [Internal] +//------------------------------------------------------------------------- + +// Return NULL if no sort specs (most often when ImGuiTableFlags_Sortable is not set) +// You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since +// last call, or the first time. +// Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()! +ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL); + + if (!(table->Flags & ImGuiTableFlags_Sortable)) + return NULL; + + // Require layout (in case TableHeadersRow() hasn't been called) as it may alter IsSortSpecsDirty in some paths. + if (!table->IsLayoutLocked) + TableUpdateLayout(table); + + if (table->IsSortSpecsDirty) + TableSortSpecsBuild(table); + + return &table->SortSpecs; +} + +static inline ImGuiSortDirection TableGetColumnAvailSortDirection(ImGuiTableColumn* column, int n) +{ + IM_ASSERT(n < column->SortDirectionsAvailCount); + return (column->SortDirectionsAvailList >> (n << 1)) & 0x03; +} + +// Fix sort direction if currently set on a value which is unavailable (e.g. activating NoSortAscending/NoSortDescending) +void ImGui::TableFixColumnSortDirection(ImGuiTable* table, ImGuiTableColumn* column) +{ + if (column->SortOrder == -1 || (column->SortDirectionsAvailMask & (1 << column->SortDirection)) != 0) + return; + column->SortDirection = (ImU8)TableGetColumnAvailSortDirection(column, 0); + table->IsSortSpecsDirty = true; +} + +// Calculate next sort direction that would be set after clicking the column +// - If the PreferSortDescending flag is set, we will default to a Descending direction on the first click. +// - Note that the PreferSortAscending flag is never checked, it is essentially the default and therefore a no-op. +IM_STATIC_ASSERT(ImGuiSortDirection_None == 0 && ImGuiSortDirection_Ascending == 1 && ImGuiSortDirection_Descending == 2); +ImGuiSortDirection ImGui::TableGetColumnNextSortDirection(ImGuiTableColumn* column) +{ + IM_ASSERT(column->SortDirectionsAvailCount > 0); + if (column->SortOrder == -1) + return TableGetColumnAvailSortDirection(column, 0); + for (int n = 0; n < 3; n++) + if (column->SortDirection == TableGetColumnAvailSortDirection(column, n)) + return TableGetColumnAvailSortDirection(column, (n + 1) % column->SortDirectionsAvailCount); + IM_ASSERT(0); + return ImGuiSortDirection_None; +} + +// Note that the NoSortAscending/NoSortDescending flags are processed in TableSortSpecsSanitize(), and they may change/revert +// the value of SortDirection. We could technically also do it here but it would be unnecessary and duplicate code. +void ImGui::TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + + if (!(table->Flags & ImGuiTableFlags_SortMulti)) + append_to_sort_specs = false; + if (!(table->Flags & ImGuiTableFlags_SortTristate)) + IM_ASSERT(sort_direction != ImGuiSortDirection_None); + + ImGuiTableColumnIdx sort_order_max = 0; + if (append_to_sort_specs) + for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++) + sort_order_max = ImMax(sort_order_max, table->Columns[other_column_n].SortOrder); + + ImGuiTableColumn* column = &table->Columns[column_n]; + column->SortDirection = (ImU8)sort_direction; + if (column->SortDirection == ImGuiSortDirection_None) + column->SortOrder = -1; + else if (column->SortOrder == -1 || !append_to_sort_specs) + column->SortOrder = append_to_sort_specs ? sort_order_max + 1 : 0; + + for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++) + { + ImGuiTableColumn* other_column = &table->Columns[other_column_n]; + if (other_column != column && !append_to_sort_specs) + other_column->SortOrder = -1; + TableFixColumnSortDirection(table, other_column); + } + table->IsSettingsDirty = true; + table->IsSortSpecsDirty = true; +} + +void ImGui::TableSortSpecsSanitize(ImGuiTable* table) +{ + IM_ASSERT(table->Flags & ImGuiTableFlags_Sortable); + + // Clear SortOrder from hidden column and verify that there's no gap or duplicate. + int sort_order_count = 0; + ImU64 sort_order_mask = 0x00; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->SortOrder != -1 && !column->IsEnabled) + column->SortOrder = -1; + if (column->SortOrder == -1) + continue; + sort_order_count++; + sort_order_mask |= ((ImU64)1 << column->SortOrder); + IM_ASSERT(sort_order_count < (int)sizeof(sort_order_mask) * 8); + } + + const bool need_fix_linearize = ((ImU64)1 << sort_order_count) != (sort_order_mask + 1); + const bool need_fix_single_sort_order = (sort_order_count > 1) && !(table->Flags & ImGuiTableFlags_SortMulti); + if (need_fix_linearize || need_fix_single_sort_order) + { + ImU64 fixed_mask = 0x00; + for (int sort_n = 0; sort_n < sort_order_count; sort_n++) + { + // Fix: Rewrite sort order fields if needed so they have no gap or duplicate. + // (e.g. SortOrder 0 disappeared, SortOrder 1..2 exists --> rewrite then as SortOrder 0..1) + int column_with_smallest_sort_order = -1; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if ((fixed_mask & ((ImU64)1 << (ImU64)column_n)) == 0 && table->Columns[column_n].SortOrder != -1) + if (column_with_smallest_sort_order == -1 || table->Columns[column_n].SortOrder < table->Columns[column_with_smallest_sort_order].SortOrder) + column_with_smallest_sort_order = column_n; + IM_ASSERT(column_with_smallest_sort_order != -1); + fixed_mask |= ((ImU64)1 << column_with_smallest_sort_order); + table->Columns[column_with_smallest_sort_order].SortOrder = (ImGuiTableColumnIdx)sort_n; + + // Fix: Make sure only one column has a SortOrder if ImGuiTableFlags_MultiSortable is not set. + if (need_fix_single_sort_order) + { + sort_order_count = 1; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if (column_n != column_with_smallest_sort_order) + table->Columns[column_n].SortOrder = -1; + break; + } + } + } + + // Fallback default sort order (if no column had the ImGuiTableColumnFlags_DefaultSort flag) + if (sort_order_count == 0 && !(table->Flags & ImGuiTableFlags_SortTristate)) + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->IsEnabled && !(column->Flags & ImGuiTableColumnFlags_NoSort)) + { + sort_order_count = 1; + column->SortOrder = 0; + column->SortDirection = (ImU8)TableGetColumnAvailSortDirection(column, 0); + break; + } + } + + table->SortSpecsCount = (ImGuiTableColumnIdx)sort_order_count; +} + +void ImGui::TableSortSpecsBuild(ImGuiTable* table) +{ + IM_ASSERT(table->IsSortSpecsDirty); + TableSortSpecsSanitize(table); + + // Write output + table->SortSpecsMulti.resize(table->SortSpecsCount <= 1 ? 0 : table->SortSpecsCount); + ImGuiTableColumnSortSpecs* sort_specs = (table->SortSpecsCount == 0) ? NULL : (table->SortSpecsCount == 1) ? &table->SortSpecsSingle : table->SortSpecsMulti.Data; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->SortOrder == -1) + continue; + IM_ASSERT(column->SortOrder < table->SortSpecsCount); + ImGuiTableColumnSortSpecs* sort_spec = &sort_specs[column->SortOrder]; + sort_spec->ColumnUserID = column->UserID; + sort_spec->ColumnIndex = (ImGuiTableColumnIdx)column_n; + sort_spec->SortOrder = (ImGuiTableColumnIdx)column->SortOrder; + sort_spec->SortDirection = column->SortDirection; + } + table->SortSpecs.Specs = sort_specs; + table->SortSpecs.SpecsCount = table->SortSpecsCount; + table->SortSpecs.SpecsDirty = true; // Mark as dirty for user + table->IsSortSpecsDirty = false; // Mark as not dirty for us +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Headers +//------------------------------------------------------------------------- +// - TableGetHeaderRowHeight() [Internal] +// - TableHeadersRow() +// - TableHeader() +//------------------------------------------------------------------------- + +float ImGui::TableGetHeaderRowHeight() +{ + // Caring for a minor edge case: + // Calculate row height, for the unlikely case that some labels may be taller than others. + // If we didn't do that, uneven header height would highlight but smaller one before the tallest wouldn't catch input for all height. + // In your custom header row you may omit this all together and just call TableNextRow() without a height... + float row_height = GetTextLineHeight(); + int columns_count = TableGetColumnCount(); + for (int column_n = 0; column_n < columns_count; column_n++) + if (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_IsEnabled) + row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y); + row_height += GetStyle().CellPadding.y * 2.0f; + return row_height; +} + +// [Public] This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn(). +// The intent is that advanced users willing to create customized headers would not need to use this helper +// and can create their own! For example: TableHeader() may be preceeded by Checkbox() or other custom widgets. +// See 'Demo->Tables->Custom headers' for a demonstration of implementing a custom version of this. +// This code is constructed to not make much use of internal functions, as it is intended to be a template to copy. +// FIXME-TABLE: TableOpenContextMenu() and TableGetHeaderRowHeight() are not public. +void ImGui::TableHeadersRow() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && "Need to call TableHeadersRow() after BeginTable()!"); + + // Layout if not already done (this is automatically done by TableNextRow, we do it here solely to facilitate stepping in debugger as it is frequent to step in TableUpdateLayout) + if (!table->IsLayoutLocked) + TableUpdateLayout(table); + + // Open row + const float row_y1 = GetCursorScreenPos().y; + const float row_height = TableGetHeaderRowHeight(); + TableNextRow(ImGuiTableRowFlags_Headers, row_height); + if (table->HostSkipItems) // Merely an optimization, you may skip in your own code. + return; + + const int columns_count = TableGetColumnCount(); + for (int column_n = 0; column_n < columns_count; column_n++) + { + if (!TableSetColumnIndex(column_n)) + continue; + + // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them) + // - in your own code you may omit the PushID/PopID all-together, provided you know they won't collide + // - table->InstanceCurrent is only >0 when we use multiple BeginTable/EndTable calls with same identifier. + const char* name = TableGetColumnName(column_n); + PushID(table->InstanceCurrent * table->ColumnsCount + column_n); + TableHeader(name); + PopID(); + } + + // Allow opening popup from the right-most section after the last column. + ImVec2 mouse_pos = ImGui::GetMousePos(); + if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count) + if (mouse_pos.y >= row_y1 && mouse_pos.y < row_y1 + row_height) + TableOpenContextMenu(-1); // Will open a non-column-specific popup. +} + +// Emit a column header (text + optional sort order) +// We cpu-clip text here so that all columns headers can be merged into a same draw call. +// Note that because of how we cpu-clip and display sorting indicators, you _cannot_ use SameLine() after a TableHeader() +void ImGui::TableHeader(const char* label) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + ImGuiTable* table = g.CurrentTable; + IM_ASSERT(table != NULL && "Need to call TableHeader() after BeginTable()!"); + IM_ASSERT(table->CurrentColumn != -1); + const int column_n = table->CurrentColumn; + ImGuiTableColumn* column = &table->Columns[column_n]; + + // Label + if (label == NULL) + label = ""; + const char* label_end = FindRenderedTextEnd(label); + ImVec2 label_size = CalcTextSize(label, label_end, true); + ImVec2 label_pos = window->DC.CursorPos; + + // If we already got a row height, there's use that. + // FIXME-TABLE: Padding problem if the correct outer-padding CellBgRect strays off our ClipRect? + ImRect cell_r = TableGetCellBgRect(table, column_n); + float label_height = ImMax(label_size.y, table->RowMinHeight - table->CellPaddingY * 2.0f); + + // Calculate ideal size for sort order arrow + float w_arrow = 0.0f; + float w_sort_text = 0.0f; + char sort_order_suf[4] = ""; + const float ARROW_SCALE = 0.65f; + if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) + { + w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x); + if (column->SortOrder > 0) + { + ImFormatString(sort_order_suf, IM_ARRAYSIZE(sort_order_suf), "%d", column->SortOrder + 1); + w_sort_text = g.Style.ItemInnerSpacing.x + CalcTextSize(sort_order_suf).x; + } + } + + // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging. + float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow; + column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, column->WorkMaxX); + column->ContentMaxXHeadersIdeal = ImMax(column->ContentMaxXHeadersIdeal, max_pos_x); + + // Keep header highlighted when context menu is open. + const bool selected = (table->IsContextPopupOpen && table->ContextPopupColumn == column_n && table->InstanceInteracted == table->InstanceCurrent); + ImGuiID id = window->GetID(label); + ImRect bb(cell_r.Min.x, cell_r.Min.y, cell_r.Max.x, ImMax(cell_r.Max.y, cell_r.Min.y + label_height + g.Style.CellPadding.y * 2.0f)); + ItemSize(ImVec2(0.0f, label_height)); // Don't declare unclipped width, it'll be fed ContentMaxPosHeadersIdeal + if (!ItemAdd(bb, id)) + return; + + //GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] + //GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] + + // Using AllowItemOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items. + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_AllowItemOverlap); + if (g.ActiveId != id) + SetItemAllowOverlap(); + if (held || hovered || selected) + { + const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + //RenderFrame(bb.Min, bb.Max, col, false, 0.0f); + TableSetBgColor(ImGuiTableBgTarget_CellBg, col, table->CurrentColumn); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + } + else + { + // Submit single cell bg color in the case we didn't submit a full header row + if ((table->RowFlags & ImGuiTableRowFlags_Headers) == 0) + TableSetBgColor(ImGuiTableBgTarget_CellBg, GetColorU32(ImGuiCol_TableHeaderBg), table->CurrentColumn); + } + if (held) + table->HeldHeaderColumn = (ImGuiTableColumnIdx)column_n; + window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f; + + // Drag and drop to re-order columns. + // FIXME-TABLE: Scroll request while reordering a column and it lands out of the scrolling zone. + if (held && (table->Flags & ImGuiTableFlags_Reorderable) && IsMouseDragging(0) && !g.DragDropActive) + { + // While moving a column it will jump on the other side of the mouse, so we also test for MouseDelta.x + table->ReorderColumn = (ImGuiTableColumnIdx)column_n; + table->InstanceInteracted = table->InstanceCurrent; + + // We don't reorder: through the frozen<>unfrozen line, or through a column that is marked with ImGuiTableColumnFlags_NoReorder. + if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < cell_r.Min.x) + if (ImGuiTableColumn* prev_column = (column->PrevEnabledColumn != -1) ? &table->Columns[column->PrevEnabledColumn] : NULL) + if (!((column->Flags | prev_column->Flags) & ImGuiTableColumnFlags_NoReorder)) + if ((column->IndexWithinEnabledSet < table->FreezeColumnsRequest) == (prev_column->IndexWithinEnabledSet < table->FreezeColumnsRequest)) + table->ReorderColumnDir = -1; + if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > cell_r.Max.x) + if (ImGuiTableColumn* next_column = (column->NextEnabledColumn != -1) ? &table->Columns[column->NextEnabledColumn] : NULL) + if (!((column->Flags | next_column->Flags) & ImGuiTableColumnFlags_NoReorder)) + if ((column->IndexWithinEnabledSet < table->FreezeColumnsRequest) == (next_column->IndexWithinEnabledSet < table->FreezeColumnsRequest)) + table->ReorderColumnDir = +1; + } + + // Sort order arrow + const float ellipsis_max = cell_r.Max.x - w_arrow - w_sort_text; + if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) + { + if (column->SortOrder != -1) + { + float x = ImMax(cell_r.Min.x, cell_r.Max.x - w_arrow - w_sort_text); + float y = label_pos.y; + if (column->SortOrder > 0) + { + PushStyleColor(ImGuiCol_Text, GetColorU32(ImGuiCol_Text, 0.70f)); + RenderText(ImVec2(x + g.Style.ItemInnerSpacing.x, y), sort_order_suf); + PopStyleColor(); + x += w_sort_text; + } + RenderArrow(window->DrawList, ImVec2(x, y), GetColorU32(ImGuiCol_Text), column->SortDirection == ImGuiSortDirection_Ascending ? ImGuiDir_Up : ImGuiDir_Down, ARROW_SCALE); + } + + // Handle clicking on column header to adjust Sort Order + if (pressed && table->ReorderColumn != column_n) + { + ImGuiSortDirection sort_direction = TableGetColumnNextSortDirection(column); + TableSetColumnSortDirection(column_n, sort_direction, g.IO.KeyShift); + } + } + + // Render clipped label. Clipping here ensure that in the majority of situations, all our header cells will + // be merged into a single draw call. + //window->DrawList->AddCircleFilled(ImVec2(ellipsis_max, label_pos.y), 40, IM_COL32_WHITE); + RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, label_pos.y + label_height + g.Style.FramePadding.y), ellipsis_max, ellipsis_max, label, label_end, &label_size); + + const bool text_clipped = label_size.x > (ellipsis_max - label_pos.x); + if (text_clipped && hovered && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay) + SetTooltip("%.*s", (int)(label_end - label), label); + + // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden + if (IsMouseReleased(1) && IsItemHovered()) + TableOpenContextMenu(column_n); +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Context Menu +//------------------------------------------------------------------------- +// - TableOpenContextMenu() [Internal] +// - TableDrawContextMenu() [Internal] +//------------------------------------------------------------------------- + +// Use -1 to open menu not specific to a given column. +void ImGui::TableOpenContextMenu(int column_n) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (column_n == -1 && table->CurrentColumn != -1) // When called within a column automatically use this one (for consistency) + column_n = table->CurrentColumn; + if (column_n == table->ColumnsCount) // To facilitate using with TableGetHoveredColumn() + column_n = -1; + IM_ASSERT(column_n >= -1 && column_n < table->ColumnsCount); + if (table->Flags & (ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) + { + table->IsContextPopupOpen = true; + table->ContextPopupColumn = (ImGuiTableColumnIdx)column_n; + table->InstanceInteracted = table->InstanceCurrent; + const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID); + OpenPopupEx(context_menu_id, ImGuiPopupFlags_None); + } +} + +// Output context menu into current window (generally a popup) +// FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data? +void ImGui::TableDrawContextMenu(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + bool want_separator = false; + const int column_n = (table->ContextPopupColumn >= 0 && table->ContextPopupColumn < table->ColumnsCount) ? table->ContextPopupColumn : -1; + ImGuiTableColumn* column = (column_n != -1) ? &table->Columns[column_n] : NULL; + + // Sizing + if (table->Flags & ImGuiTableFlags_Resizable) + { + if (column != NULL) + { + const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled; + if (MenuItem("Size column to fit###SizeOne", NULL, false, can_resize)) + TableSetColumnWidthAutoSingle(table, column_n); + } + + const char* size_all_desc; + if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount && (table->Flags & ImGuiTableFlags_SizingMask_) != ImGuiTableFlags_SizingFixedSame) + size_all_desc = "Size all columns to fit###SizeAll"; // All fixed + else + size_all_desc = "Size all columns to default###SizeAll"; // All stretch or mixed + if (MenuItem(size_all_desc, NULL)) + TableSetColumnWidthAutoAll(table); + want_separator = true; + } + + // Ordering + if (table->Flags & ImGuiTableFlags_Reorderable) + { + if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder)) + table->IsResetDisplayOrderRequest = true; + want_separator = true; + } + + // Reset all (should work but seems unnecessary/noisy to expose?) + //if (MenuItem("Reset all")) + // table->IsResetAllRequest = true; + + // Sorting + // (modify TableOpenContextMenu() to add _Sortable flag if enabling this) +#if 0 + if ((table->Flags & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0) + { + if (want_separator) + Separator(); + want_separator = true; + + bool append_to_sort_specs = g.IO.KeyShift; + if (MenuItem("Sort in Ascending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Ascending, (column->Flags & ImGuiTableColumnFlags_NoSortAscending) == 0)) + TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Ascending, append_to_sort_specs); + if (MenuItem("Sort in Descending Order", NULL, column->SortOrder != -1 && column->SortDirection == ImGuiSortDirection_Descending, (column->Flags & ImGuiTableColumnFlags_NoSortDescending) == 0)) + TableSetColumnSortDirection(table, column_n, ImGuiSortDirection_Descending, append_to_sort_specs); + } +#endif + + // Hiding / Visibility + if (table->Flags & ImGuiTableFlags_Hideable) + { + if (want_separator) + Separator(); + want_separator = true; + + PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true); + for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++) + { + ImGuiTableColumn* other_column = &table->Columns[other_column_n]; + const char* name = TableGetColumnName(table, other_column_n); + if (name == NULL || name[0] == 0) + name = ""; + + // Make sure we can't hide the last active column + bool menu_item_active = (other_column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true; + if (other_column->IsEnabled && table->ColumnsEnabledCount <= 1) + menu_item_active = false; + if (MenuItem(name, NULL, other_column->IsEnabled, menu_item_active)) + other_column->IsEnabledNextFrame = !other_column->IsEnabled; + } + PopItemFlag(); + } +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Settings (.ini data) +//------------------------------------------------------------------------- +// FIXME: The binding/finding/creating flow are too confusing. +//------------------------------------------------------------------------- +// - TableSettingsInit() [Internal] +// - TableSettingsCalcChunkSize() [Internal] +// - TableSettingsCreate() [Internal] +// - TableSettingsFindByID() [Internal] +// - TableGetBoundSettings() [Internal] +// - TableResetSettings() +// - TableSaveSettings() [Internal] +// - TableLoadSettings() [Internal] +// - TableSettingsHandler_ClearAll() [Internal] +// - TableSettingsHandler_ApplyAll() [Internal] +// - TableSettingsHandler_ReadOpen() [Internal] +// - TableSettingsHandler_ReadLine() [Internal] +// - TableSettingsHandler_WriteAll() [Internal] +// - TableSettingsInstallHandler() [Internal] +//------------------------------------------------------------------------- +// [Init] 1: TableSettingsHandler_ReadXXXX() Load and parse .ini file into TableSettings. +// [Main] 2: TableLoadSettings() When table is created, bind Table to TableSettings, serialize TableSettings data into Table. +// [Main] 3: TableSaveSettings() When table properties are modified, serialize Table data into bound or new TableSettings, mark .ini as dirty. +// [Main] 4: TableSettingsHandler_WriteAll() When .ini file is dirty (which can come from other source), save TableSettings into .ini file. +//------------------------------------------------------------------------- + +// Clear and initialize empty settings instance +static void TableSettingsInit(ImGuiTableSettings* settings, ImGuiID id, int columns_count, int columns_count_max) +{ + IM_PLACEMENT_NEW(settings) ImGuiTableSettings(); + ImGuiTableColumnSettings* settings_column = settings->GetColumnSettings(); + for (int n = 0; n < columns_count_max; n++, settings_column++) + IM_PLACEMENT_NEW(settings_column) ImGuiTableColumnSettings(); + settings->ID = id; + settings->ColumnsCount = (ImGuiTableColumnIdx)columns_count; + settings->ColumnsCountMax = (ImGuiTableColumnIdx)columns_count_max; + settings->WantApply = true; +} + +static size_t TableSettingsCalcChunkSize(int columns_count) +{ + return sizeof(ImGuiTableSettings) + (size_t)columns_count * sizeof(ImGuiTableColumnSettings); +} + +ImGuiTableSettings* ImGui::TableSettingsCreate(ImGuiID id, int columns_count) +{ + ImGuiContext& g = *GImGui; + ImGuiTableSettings* settings = g.SettingsTables.alloc_chunk(TableSettingsCalcChunkSize(columns_count)); + TableSettingsInit(settings, id, columns_count, columns_count); + return settings; +} + +// Find existing settings +ImGuiTableSettings* ImGui::TableSettingsFindByID(ImGuiID id) +{ + // FIXME-OPT: Might want to store a lookup map for this? + ImGuiContext& g = *GImGui; + for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) + if (settings->ID == id) + return settings; + return NULL; +} + +// Get settings for a given table, NULL if none +ImGuiTableSettings* ImGui::TableGetBoundSettings(ImGuiTable* table) +{ + if (table->SettingsOffset != -1) + { + ImGuiContext& g = *GImGui; + ImGuiTableSettings* settings = g.SettingsTables.ptr_from_offset(table->SettingsOffset); + IM_ASSERT(settings->ID == table->ID); + if (settings->ColumnsCountMax >= table->ColumnsCount) + return settings; // OK + settings->ID = 0; // Invalidate storage, we won't fit because of a count change + } + return NULL; +} + +// Restore initial state of table (with or without saved settings) +void ImGui::TableResetSettings(ImGuiTable* table) +{ + table->IsInitializing = table->IsSettingsDirty = true; + table->IsResetAllRequest = false; + table->IsSettingsRequestLoad = false; // Don't reload from ini + table->SettingsLoadedFlags = ImGuiTableFlags_None; // Mark as nothing loaded so our initialized data becomes authoritative +} + +void ImGui::TableSaveSettings(ImGuiTable* table) +{ + table->IsSettingsDirty = false; + if (table->Flags & ImGuiTableFlags_NoSavedSettings) + return; + + // Bind or create settings data + ImGuiContext& g = *GImGui; + ImGuiTableSettings* settings = TableGetBoundSettings(table); + if (settings == NULL) + { + settings = TableSettingsCreate(table->ID, table->ColumnsCount); + table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings); + } + settings->ColumnsCount = (ImGuiTableColumnIdx)table->ColumnsCount; + + // Serialize ImGuiTable/ImGuiTableColumn into ImGuiTableSettings/ImGuiTableColumnSettings + IM_ASSERT(settings->ID == table->ID); + IM_ASSERT(settings->ColumnsCount == table->ColumnsCount && settings->ColumnsCountMax >= settings->ColumnsCount); + ImGuiTableColumn* column = table->Columns.Data; + ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings(); + + bool save_ref_scale = false; + settings->SaveFlags = ImGuiTableFlags_None; + for (int n = 0; n < table->ColumnsCount; n++, column++, column_settings++) + { + const float width_or_weight = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? column->StretchWeight : column->WidthRequest; + column_settings->WidthOrWeight = width_or_weight; + column_settings->Index = (ImGuiTableColumnIdx)n; + column_settings->DisplayOrder = column->DisplayOrder; + column_settings->SortOrder = column->SortOrder; + column_settings->SortDirection = column->SortDirection; + column_settings->IsEnabled = column->IsEnabled; + column_settings->IsStretch = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? 1 : 0; + if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) == 0) + save_ref_scale = true; + + // We skip saving some data in the .ini file when they are unnecessary to restore our state. + // Note that fixed width where initial width was derived from auto-fit will always be saved as InitStretchWeightOrWidth will be 0.0f. + // FIXME-TABLE: We don't have logic to easily compare SortOrder to DefaultSortOrder yet so it's always saved when present. + if (width_or_weight != column->InitStretchWeightOrWidth) + settings->SaveFlags |= ImGuiTableFlags_Resizable; + if (column->DisplayOrder != n) + settings->SaveFlags |= ImGuiTableFlags_Reorderable; + if (column->SortOrder != -1) + settings->SaveFlags |= ImGuiTableFlags_Sortable; + if (column->IsEnabled != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0)) + settings->SaveFlags |= ImGuiTableFlags_Hideable; + } + settings->SaveFlags &= table->Flags; + settings->RefScale = save_ref_scale ? table->RefScale : 0.0f; + + MarkIniSettingsDirty(); +} + +void ImGui::TableLoadSettings(ImGuiTable* table) +{ + ImGuiContext& g = *GImGui; + table->IsSettingsRequestLoad = false; + if (table->Flags & ImGuiTableFlags_NoSavedSettings) + return; + + // Bind settings + ImGuiTableSettings* settings; + if (table->SettingsOffset == -1) + { + settings = TableSettingsFindByID(table->ID); + if (settings == NULL) + return; + if (settings->ColumnsCount != table->ColumnsCount) // Allow settings if columns count changed. We could otherwise decide to return... + table->IsSettingsDirty = true; + table->SettingsOffset = g.SettingsTables.offset_from_ptr(settings); + } + else + { + settings = TableGetBoundSettings(table); + } + + table->SettingsLoadedFlags = settings->SaveFlags; + table->RefScale = settings->RefScale; + + // Serialize ImGuiTableSettings/ImGuiTableColumnSettings into ImGuiTable/ImGuiTableColumn + ImGuiTableColumnSettings* column_settings = settings->GetColumnSettings(); + ImU64 display_order_mask = 0; + for (int data_n = 0; data_n < settings->ColumnsCount; data_n++, column_settings++) + { + int column_n = column_settings->Index; + if (column_n < 0 || column_n >= table->ColumnsCount) + continue; + + ImGuiTableColumn* column = &table->Columns[column_n]; + if (settings->SaveFlags & ImGuiTableFlags_Resizable) + { + if (column_settings->IsStretch) + column->StretchWeight = column_settings->WidthOrWeight; + else + column->WidthRequest = column_settings->WidthOrWeight; + column->AutoFitQueue = 0x00; + } + if (settings->SaveFlags & ImGuiTableFlags_Reorderable) + column->DisplayOrder = column_settings->DisplayOrder; + else + column->DisplayOrder = (ImGuiTableColumnIdx)column_n; + display_order_mask |= (ImU64)1 << column->DisplayOrder; + column->IsEnabled = column->IsEnabledNextFrame = column_settings->IsEnabled; + column->SortOrder = column_settings->SortOrder; + column->SortDirection = column_settings->SortDirection; + } + + // Validate and fix invalid display order data + const ImU64 expected_display_order_mask = (settings->ColumnsCount == 64) ? ~0 : ((ImU64)1 << settings->ColumnsCount) - 1; + if (display_order_mask != expected_display_order_mask) + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + table->Columns[column_n].DisplayOrder = (ImGuiTableColumnIdx)column_n; + + // Rebuild index + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImGuiTableColumnIdx)column_n; +} + +static void TableSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) +{ + ImGuiContext& g = *ctx; + for (int i = 0; i != g.Tables.GetSize(); i++) + g.Tables.GetByIndex(i)->SettingsOffset = -1; + g.SettingsTables.clear(); +} + +// Apply to existing windows (if any) +static void TableSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*) +{ + ImGuiContext& g = *ctx; + for (int i = 0; i != g.Tables.GetSize(); i++) + { + ImGuiTable* table = g.Tables.GetByIndex(i); + table->IsSettingsRequestLoad = true; + table->SettingsOffset = -1; + } +} + +static void* TableSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) +{ + ImGuiID id = 0; + int columns_count = 0; + if (sscanf(name, "0x%08X,%d", &id, &columns_count) < 2) + return NULL; + + if (ImGuiTableSettings* settings = ImGui::TableSettingsFindByID(id)) + { + if (settings->ColumnsCountMax >= columns_count) + { + TableSettingsInit(settings, id, columns_count, settings->ColumnsCountMax); // Recycle + return settings; + } + settings->ID = 0; // Invalidate storage, we won't fit because of a count change + } + return ImGui::TableSettingsCreate(id, columns_count); +} + +static void TableSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) +{ + // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v" + ImGuiTableSettings* settings = (ImGuiTableSettings*)entry; + float f = 0.0f; + int column_n = 0, r = 0, n = 0; + + if (sscanf(line, "RefScale=%f", &f) == 1) { settings->RefScale = f; return; } + + if (sscanf(line, "Column %d%n", &column_n, &r) == 1) + { + if (column_n < 0 || column_n >= settings->ColumnsCount) + return; + line = ImStrSkipBlank(line + r); + char c = 0; + ImGuiTableColumnSettings* column = settings->GetColumnSettings() + column_n; + column->Index = (ImGuiTableColumnIdx)column_n; + if (sscanf(line, "UserID=0x%08X%n", (ImU32*)&n, &r)==1) { line = ImStrSkipBlank(line + r); column->UserID = (ImGuiID)n; } + if (sscanf(line, "Width=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->WidthOrWeight = (float)n; column->IsStretch = 0; settings->SaveFlags |= ImGuiTableFlags_Resizable; } + if (sscanf(line, "Weight=%f%n", &f, &r) == 1) { line = ImStrSkipBlank(line + r); column->WidthOrWeight = f; column->IsStretch = 1; settings->SaveFlags |= ImGuiTableFlags_Resizable; } + if (sscanf(line, "Visible=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->IsEnabled = (ImU8)n; settings->SaveFlags |= ImGuiTableFlags_Hideable; } + if (sscanf(line, "Order=%d%n", &n, &r) == 1) { line = ImStrSkipBlank(line + r); column->DisplayOrder = (ImGuiTableColumnIdx)n; settings->SaveFlags |= ImGuiTableFlags_Reorderable; } + if (sscanf(line, "Sort=%d%c%n", &n, &c, &r) == 2) { line = ImStrSkipBlank(line + r); column->SortOrder = (ImGuiTableColumnIdx)n; column->SortDirection = (c == '^') ? ImGuiSortDirection_Descending : ImGuiSortDirection_Ascending; settings->SaveFlags |= ImGuiTableFlags_Sortable; } + } +} + +static void TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) +{ + ImGuiContext& g = *ctx; + for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) + { + if (settings->ID == 0) // Skip ditched settings + continue; + + // TableSaveSettings() may clear some of those flags when we establish that the data can be stripped + // (e.g. Order was unchanged) + const bool save_size = (settings->SaveFlags & ImGuiTableFlags_Resizable) != 0; + const bool save_visible = (settings->SaveFlags & ImGuiTableFlags_Hideable) != 0; + const bool save_order = (settings->SaveFlags & ImGuiTableFlags_Reorderable) != 0; + const bool save_sort = (settings->SaveFlags & ImGuiTableFlags_Sortable) != 0; + if (!save_size && !save_visible && !save_order && !save_sort) + continue; + + buf->reserve(buf->size() + 30 + settings->ColumnsCount * 50); // ballpark reserve + buf->appendf("[%s][0x%08X,%d]\n", handler->TypeName, settings->ID, settings->ColumnsCount); + if (settings->RefScale != 0.0f) + buf->appendf("RefScale=%g\n", settings->RefScale); + ImGuiTableColumnSettings* column = settings->GetColumnSettings(); + for (int column_n = 0; column_n < settings->ColumnsCount; column_n++, column++) + { + // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v" + buf->appendf("Column %-2d", column_n); + if (column->UserID != 0) buf->appendf(" UserID=%08X", column->UserID); + if (save_size && column->IsStretch) buf->appendf(" Weight=%.4f", column->WidthOrWeight); + if (save_size && !column->IsStretch) buf->appendf(" Width=%d", (int)column->WidthOrWeight); + if (save_visible) buf->appendf(" Visible=%d", column->IsEnabled); + if (save_order) buf->appendf(" Order=%d", column->DisplayOrder); + if (save_sort && column->SortOrder != -1) buf->appendf(" Sort=%d%c", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? 'v' : '^'); + buf->append("\n"); + } + buf->append("\n"); + } +} + +void ImGui::TableSettingsInstallHandler(ImGuiContext* context) +{ + ImGuiContext& g = *context; + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "Table"; + ini_handler.TypeHash = ImHashStr("Table"); + ini_handler.ClearAllFn = TableSettingsHandler_ClearAll; + ini_handler.ReadOpenFn = TableSettingsHandler_ReadOpen; + ini_handler.ReadLineFn = TableSettingsHandler_ReadLine; + ini_handler.ApplyAllFn = TableSettingsHandler_ApplyAll; + ini_handler.WriteAllFn = TableSettingsHandler_WriteAll; + g.SettingsHandlers.push_back(ini_handler); +} + +//------------------------------------------------------------------------- +// [SECTION] Tables: Garbage Collection +//------------------------------------------------------------------------- +// - TableRemove() [Internal] +// - TableGcCompactTransientBuffers() [Internal] +// - TableGcCompactSettings() [Internal] +//------------------------------------------------------------------------- + +// Remove Table (currently only used by TestEngine) +void ImGui::TableRemove(ImGuiTable* table) +{ + //IMGUI_DEBUG_LOG("TableRemove() id=0x%08X\n", table->ID); + ImGuiContext& g = *GImGui; + int table_idx = g.Tables.GetIndex(table); + //memset(table->RawData.Data, 0, table->RawData.size_in_bytes()); + //memset(table, 0, sizeof(ImGuiTable)); + g.Tables.Remove(table->ID, table); + g.TablesLastTimeActive[table_idx] = -1.0f; +} + +// Free up/compact internal Table buffers for when it gets unused +void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table) +{ + //IMGUI_DEBUG_LOG("TableGcCompactTransientBuffers() id=0x%08X\n", table->ID); + ImGuiContext& g = *GImGui; + IM_ASSERT(table->MemoryCompacted == false); + table->DrawSplitter.ClearFreeMemory(); + table->SortSpecsMulti.clear(); + table->SortSpecs.Specs = NULL; + table->IsSortSpecsDirty = true; + table->ColumnsNames.clear(); + table->MemoryCompacted = true; + for (int n = 0; n < table->ColumnsCount; n++) + table->Columns[n].NameOffset = -1; + g.TablesLastTimeActive[g.Tables.GetIndex(table)] = -1.0f; +} + +// Compact and remove unused settings data (currently only used by TestEngine) +void ImGui::TableGcCompactSettings() +{ + ImGuiContext& g = *GImGui; + int required_memory = 0; + for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) + if (settings->ID != 0) + required_memory += (int)TableSettingsCalcChunkSize(settings->ColumnsCount); + if (required_memory == g.SettingsTables.Buf.Size) + return; + ImChunkStream new_chunk_stream; + new_chunk_stream.Buf.reserve(required_memory); + for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) + if (settings->ID != 0) + memcpy(new_chunk_stream.alloc_chunk(TableSettingsCalcChunkSize(settings->ColumnsCount)), settings, TableSettingsCalcChunkSize(settings->ColumnsCount)); + g.SettingsTables.swap(new_chunk_stream); +} + + +//------------------------------------------------------------------------- +// [SECTION] Tables: Debugging +//------------------------------------------------------------------------- +// - DebugNodeTable() [Internal] +//------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_METRICS_WINDOW + +static const char* DebugNodeTableGetSizingPolicyDesc(ImGuiTableFlags sizing_policy) +{ + sizing_policy &= ImGuiTableFlags_SizingMask_; + if (sizing_policy == ImGuiTableFlags_SizingFixedFit) { return "FixedFit"; } + if (sizing_policy == ImGuiTableFlags_SizingFixedSame) { return "FixedSame"; } + if (sizing_policy == ImGuiTableFlags_SizingStretchProp) { return "StretchProp"; } + if (sizing_policy == ImGuiTableFlags_SizingStretchSame) { return "StretchSame"; } + return "N/A"; +} + +void ImGui::DebugNodeTable(ImGuiTable* table) +{ + char buf[512]; + char* p = buf; + const char* buf_end = buf + IM_ARRAYSIZE(buf); + const bool is_active = (table->LastFrameActive >= ImGui::GetFrameCount() - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. + ImFormatString(p, buf_end - p, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*"); + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + bool open = TreeNode(table, "%s", buf); + if (!is_active) { PopStyleColor(); } + if (IsItemHovered()) + GetForegroundDrawList()->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255)); + if (IsItemVisible() && table->HoveredColumnBody != -1) + GetForegroundDrawList()->AddRect(GetItemRectMin(), GetItemRectMax(), IM_COL32(255, 255, 0, 255)); + if (!open) + return; + bool clear_settings = SmallButton("Clear settings"); + BulletText("OuterRect: Pos: (%.1f,%.1f) Size: (%.1f,%.1f) Sizing: '%s'", table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.GetWidth(), table->OuterRect.GetHeight(), DebugNodeTableGetSizingPolicyDesc(table->Flags)); + BulletText("ColumnsGivenWidth: %.1f, ColumnsAutoFitWidth: %.1f, InnerWidth: %.1f%s", table->ColumnsGivenWidth, table->ColumnsAutoFitWidth, table->InnerWidth, table->InnerWidth == 0.0f ? " (auto)" : ""); + BulletText("CellPaddingX: %.1f, CellSpacingX: %.1f/%.1f, OuterPaddingX: %.1f", table->CellPaddingX, table->CellSpacingX1, table->CellSpacingX2, table->OuterPaddingX); + BulletText("HoveredColumnBody: %d, HoveredColumnBorder: %d", table->HoveredColumnBody, table->HoveredColumnBorder); + BulletText("ResizedColumn: %d, ReorderColumn: %d, HeldHeaderColumn: %d", table->ResizedColumn, table->ReorderColumn, table->HeldHeaderColumn); + //BulletText("BgDrawChannels: %d/%d", 0, table->BgDrawChannelUnfrozen); + float sum_weights = 0.0f; + for (int n = 0; n < table->ColumnsCount; n++) + if (table->Columns[n].Flags & ImGuiTableColumnFlags_WidthStretch) + sum_weights += table->Columns[n].StretchWeight; + for (int n = 0; n < table->ColumnsCount; n++) + { + ImGuiTableColumn* column = &table->Columns[n]; + const char* name = TableGetColumnName(table, n); + ImFormatString(buf, IM_ARRAYSIZE(buf), + "Column %d order %d '%s': offset %+.2f to %+.2f%s\n" + "Enabled: %d, VisibleX/Y: %d/%d, RequestOutput: %d, SkipItems: %d, DrawChannels: %d,%d\n" + "WidthGiven: %.1f, Request/Auto: %.1f/%.1f, StretchWeight: %.3f (%.1f%%)\n" + "MinX: %.1f, MaxX: %.1f (%+.1f), ClipRect: %.1f to %.1f (+%.1f)\n" + "ContentWidth: %.1f,%.1f, HeadersUsed/Ideal %.1f/%.1f\n" + "Sort: %d%s, UserID: 0x%08X, Flags: 0x%04X: %s%s%s..", + n, column->DisplayOrder, name, column->MinX - table->WorkRect.Min.x, column->MaxX - table->WorkRect.Min.x, (n < table->FreezeColumnsRequest) ? " (Frozen)" : "", + column->IsEnabled, column->IsVisibleX, column->IsVisibleY, column->IsRequestOutput, column->IsSkipItems, column->DrawChannelFrozen, column->DrawChannelUnfrozen, + column->WidthGiven, column->WidthRequest, column->WidthAuto, column->StretchWeight, column->StretchWeight > 0.0f ? (column->StretchWeight / sum_weights) * 100.0f : 0.0f, + column->MinX, column->MaxX, column->MaxX - column->MinX, column->ClipRect.Min.x, column->ClipRect.Max.x, column->ClipRect.Max.x - column->ClipRect.Min.x, + column->ContentMaxXFrozen - column->WorkMinX, column->ContentMaxXUnfrozen - column->WorkMinX, column->ContentMaxXHeadersUsed - column->WorkMinX, column->ContentMaxXHeadersIdeal - column->WorkMinX, + column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? " (Asc)" : (column->SortDirection == ImGuiSortDirection_Descending) ? " (Des)" : "", column->UserID, column->Flags, + (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? "WidthStretch " : "", + (column->Flags & ImGuiTableColumnFlags_WidthFixed) ? "WidthFixed " : "", + (column->Flags & ImGuiTableColumnFlags_NoResize) ? "NoResize " : ""); + Bullet(); + Selectable(buf); + if (IsItemHovered()) + { + ImRect r(column->MinX, table->OuterRect.Min.y, column->MaxX, table->OuterRect.Max.y); + GetForegroundDrawList()->AddRect(r.Min, r.Max, IM_COL32(255, 255, 0, 255)); + } + } + if (ImGuiTableSettings* settings = TableGetBoundSettings(table)) + DebugNodeTableSettings(settings); + if (clear_settings) + table->IsResetAllRequest = true; + TreePop(); +} + +void ImGui::DebugNodeTableSettings(ImGuiTableSettings* settings) +{ + if (!TreeNode((void*)(intptr_t)settings->ID, "Settings 0x%08X (%d columns)", settings->ID, settings->ColumnsCount)) + return; + BulletText("SaveFlags: 0x%08X", settings->SaveFlags); + BulletText("ColumnsCount: %d (max %d)", settings->ColumnsCount, settings->ColumnsCountMax); + for (int n = 0; n < settings->ColumnsCount; n++) + { + ImGuiTableColumnSettings* column_settings = &settings->GetColumnSettings()[n]; + ImGuiSortDirection sort_dir = (column_settings->SortOrder != -1) ? (ImGuiSortDirection)column_settings->SortDirection : ImGuiSortDirection_None; + BulletText("Column %d Order %d SortOrder %d %s Vis %d %s %7.3f UserID 0x%08X", + n, column_settings->DisplayOrder, column_settings->SortOrder, + (sort_dir == ImGuiSortDirection_Ascending) ? "Asc" : (sort_dir == ImGuiSortDirection_Descending) ? "Des" : "---", + column_settings->IsEnabled, column_settings->IsStretch ? "Weight" : "Width ", column_settings->WidthOrWeight, column_settings->UserID); + } + TreePop(); +} + +#else // #ifndef IMGUI_DISABLE_METRICS_WINDOW + +void ImGui::DebugNodeTable(ImGuiTable*) {} +void ImGui::DebugNodeTableSettings(ImGuiTableSettings*) {} + +#endif + + +//------------------------------------------------------------------------- +// [SECTION] Columns, BeginColumns, EndColumns, etc. +// (This is a legacy API, prefer using BeginTable/EndTable!) +//------------------------------------------------------------------------- +// FIXME: sizing is lossy when columns width is very small (default width may turn negative etc.) +//------------------------------------------------------------------------- +// - SetWindowClipRectBeforeSetChannel() [Internal] +// - GetColumnIndex() +// - GetColumnsCount() +// - GetColumnOffset() +// - GetColumnWidth() +// - SetColumnOffset() +// - SetColumnWidth() +// - PushColumnClipRect() [Internal] +// - PushColumnsBackground() [Internal] +// - PopColumnsBackground() [Internal] +// - FindOrCreateColumns() [Internal] +// - GetColumnsID() [Internal] +// - BeginColumns() +// - NextColumn() +// - EndColumns() +// - Columns() +//------------------------------------------------------------------------- + +// [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences, +// they would meddle many times with the underlying ImDrawCmd. +// Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let +// the subsequent single call to SetCurrentChannel() does it things once. +void ImGui::SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect) +{ + ImVec4 clip_rect_vec4 = clip_rect.ToVec4(); + window->ClipRect = clip_rect; + window->DrawList->_CmdHeader.ClipRect = clip_rect_vec4; + window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 1] = clip_rect_vec4; +} + +int ImGui::GetColumnIndex() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0; +} + +int ImGui::GetColumnsCount() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1; +} + +float ImGui::GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm) +{ + return offset_norm * (columns->OffMaxX - columns->OffMinX); +} + +float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset) +{ + return offset / (columns->OffMaxX - columns->OffMinX); +} + +static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; + +static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index) +{ + // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing + // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning. + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(column_index > 0); // We are not supposed to drag column 0. + IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index)); + + float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; + x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); + if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths)) + x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); + + return x; +} + +float ImGui::GetColumnOffset(int column_index) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (columns == NULL) + return 0.0f; + + if (column_index < 0) + column_index = columns->Current; + IM_ASSERT(column_index < columns->Columns.Size); + + const float t = columns->Columns[column_index].OffsetNorm; + const float x_offset = ImLerp(columns->OffMinX, columns->OffMaxX, t); + return x_offset; +} + +static float GetColumnWidthEx(ImGuiOldColumns* columns, int column_index, bool before_resize = false) +{ + if (column_index < 0) + column_index = columns->Current; + + float offset_norm; + if (before_resize) + offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize; + else + offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm; + return ImGui::GetColumnOffsetFromNorm(columns, offset_norm); +} + +float ImGui::GetColumnWidth(int column_index) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (columns == NULL) + return GetContentRegionAvail().x; + + if (column_index < 0) + column_index = columns->Current; + return GetColumnOffsetFromNorm(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm); +} + +void ImGui::SetColumnOffset(int column_index, float offset) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiOldColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + if (column_index < 0) + column_index = columns->Current; + IM_ASSERT(column_index < columns->Columns.Size); + + const bool preserve_width = !(columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths) && (column_index < columns->Count - 1); + const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; + + if (!(columns->Flags & ImGuiOldColumnFlags_NoForceWithinWindow)) + offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index)); + columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX); + + if (preserve_width) + SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width)); +} + +void ImGui::SetColumnWidth(int column_index, float width) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + if (column_index < 0) + column_index = columns->Current; + SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width); +} + +void ImGui::PushColumnClipRect(int column_index) +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (column_index < 0) + column_index = columns->Current; + + ImGuiOldColumnData* column = &columns->Columns[column_index]; + PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false); +} + +// Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns) +void ImGui::PushColumnsBackground() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (columns->Count == 1) + return; + + // Optimization: avoid SetCurrentChannel() + PushClipRect() + columns->HostBackupClipRect = window->ClipRect; + SetWindowClipRectBeforeSetChannel(window, columns->HostInitialClipRect); + columns->Splitter.SetCurrentChannel(window->DrawList, 0); +} + +void ImGui::PopColumnsBackground() +{ + ImGuiWindow* window = GetCurrentWindowRead(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (columns->Count == 1) + return; + + // Optimization: avoid PopClipRect() + SetCurrentChannel() + SetWindowClipRectBeforeSetChannel(window, columns->HostBackupClipRect); + columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); +} + +ImGuiOldColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) +{ + // We have few columns per window so for now we don't need bother much with turning this into a faster lookup. + for (int n = 0; n < window->ColumnsStorage.Size; n++) + if (window->ColumnsStorage[n].ID == id) + return &window->ColumnsStorage[n]; + + window->ColumnsStorage.push_back(ImGuiOldColumns()); + ImGuiOldColumns* columns = &window->ColumnsStorage.back(); + columns->ID = id; + return columns; +} + +ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count) +{ + ImGuiWindow* window = GetCurrentWindow(); + + // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget. + // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer. + PushID(0x11223347 + (str_id ? 0 : columns_count)); + ImGuiID id = window->GetID(str_id ? str_id : "columns"); + PopID(); + + return id; +} + +void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + + IM_ASSERT(columns_count >= 1); + IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported + + // Acquire storage for the columns set + ImGuiID id = GetColumnsID(str_id, columns_count); + ImGuiOldColumns* columns = FindOrCreateColumns(window, id); + IM_ASSERT(columns->ID == id); + columns->Current = 0; + columns->Count = columns_count; + columns->Flags = flags; + window->DC.CurrentColumns = columns; + + columns->HostCursorPosY = window->DC.CursorPos.y; + columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; + columns->HostInitialClipRect = window->ClipRect; + columns->HostBackupParentWorkRect = window->ParentWorkRect; + window->ParentWorkRect = window->WorkRect; + + // Set state for first column + // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect + const float column_padding = g.Style.ItemSpacing.x; + const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize)); + const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f); + const float max_2 = window->WorkRect.Max.x + half_clip_extend_x; + columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f); + columns->OffMaxX = ImMax(ImMin(max_1, max_2) - window->Pos.x, columns->OffMinX + 1.0f); + columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y; + + // Clear data if columns count changed + if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1) + columns->Columns.resize(0); + + // Initialize default widths + columns->IsFirstFrame = (columns->Columns.Size == 0); + if (columns->Columns.Size == 0) + { + columns->Columns.reserve(columns_count + 1); + for (int n = 0; n < columns_count + 1; n++) + { + ImGuiOldColumnData column; + column.OffsetNorm = n / (float)columns_count; + columns->Columns.push_back(column); + } + } + + for (int n = 0; n < columns_count; n++) + { + // Compute clipping rectangle + ImGuiOldColumnData* column = &columns->Columns[n]; + float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n)); + float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f); + column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); + column->ClipRect.ClipWithFull(window->ClipRect); + } + + if (columns->Count > 1) + { + columns->Splitter.Split(window->DrawList, 1 + columns->Count); + columns->Splitter.SetCurrentChannel(window->DrawList, 1); + PushColumnClipRect(0); + } + + // We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user. + float offset_0 = GetColumnOffset(columns->Current); + float offset_1 = GetColumnOffset(columns->Current + 1); + float width = offset_1 - offset_0; + PushItemWidth(width * 0.65f); + window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; +} + +void ImGui::NextColumn() +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems || window->DC.CurrentColumns == NULL) + return; + + ImGuiContext& g = *GImGui; + ImGuiOldColumns* columns = window->DC.CurrentColumns; + + if (columns->Count == 1) + { + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + IM_ASSERT(columns->Current == 0); + return; + } + + // Next column + if (++columns->Current == columns->Count) + columns->Current = 0; + + PopItemWidth(); + + // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect() + // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them), + ImGuiOldColumnData* column = &columns->Columns[columns->Current]; + SetWindowClipRectBeforeSetChannel(window, column->ClipRect); + columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); + + const float column_padding = g.Style.ItemSpacing.x; + columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); + if (columns->Current > 0) + { + // Columns 1+ ignore IndentX (by canceling it out) + // FIXME-COLUMNS: Unnecessary, could be locked? + window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding; + } + else + { + // New row/line: column 0 honor IndentX. + window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); + columns->LineMinY = columns->LineMaxY; + } + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.y = columns->LineMinY; + window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + window->DC.CurrLineTextBaseOffset = 0.0f; + + // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup. + float offset_0 = GetColumnOffset(columns->Current); + float offset_1 = GetColumnOffset(columns->Current + 1); + float width = offset_1 - offset_0; + PushItemWidth(width * 0.65f); + window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; +} + +void ImGui::EndColumns() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + ImGuiOldColumns* columns = window->DC.CurrentColumns; + IM_ASSERT(columns != NULL); + + PopItemWidth(); + if (columns->Count > 1) + { + PopClipRect(); + columns->Splitter.Merge(window->DrawList); + } + + const ImGuiOldColumnFlags flags = columns->Flags; + columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); + window->DC.CursorPos.y = columns->LineMaxY; + if (!(flags & ImGuiOldColumnFlags_GrowParentContentsSize)) + window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent + + // Draw columns borders and handle resize + // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy + bool is_being_resized = false; + if (!(flags & ImGuiOldColumnFlags_NoBorder) && !window->SkipItems) + { + // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers. + const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y); + const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y); + int dragging_column = -1; + for (int n = 1; n < columns->Count; n++) + { + ImGuiOldColumnData* column = &columns->Columns[n]; + float x = window->Pos.x + GetColumnOffset(n); + const ImGuiID column_id = columns->ID + ImGuiID(n); + const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; + const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); + KeepAliveID(column_id); + if (IsClippedEx(column_hit_rect, column_id, false)) + continue; + + bool hovered = false, held = false; + if (!(flags & ImGuiOldColumnFlags_NoResize)) + { + ButtonBehavior(column_hit_rect, column_id, &hovered, &held); + if (hovered || held) + g.MouseCursor = ImGuiMouseCursor_ResizeEW; + if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize)) + dragging_column = n; + } + + // Draw column + const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); + const float xi = IM_FLOOR(x); + window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); + } + + // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame. + if (dragging_column != -1) + { + if (!columns->IsBeingResized) + for (int n = 0; n < columns->Count + 1; n++) + columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm; + columns->IsBeingResized = is_being_resized = true; + float x = GetDraggedColumnOffset(columns, dragging_column); + SetColumnOffset(dragging_column, x); + } + } + columns->IsBeingResized = is_being_resized; + + window->WorkRect = window->ParentWorkRect; + window->ParentWorkRect = columns->HostBackupParentWorkRect; + window->DC.CurrentColumns = NULL; + window->DC.ColumnsOffset.x = 0.0f; + window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); +} + +void ImGui::Columns(int columns_count, const char* id, bool border) +{ + ImGuiWindow* window = GetCurrentWindow(); + IM_ASSERT(columns_count >= 1); + + ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder); + //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior + ImGuiOldColumns* columns = window->DC.CurrentColumns; + if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) + return; + + if (columns != NULL) + EndColumns(); + + if (columns_count != 1) + BeginColumns(id, columns_count, flags); +} + +//------------------------------------------------------------------------- + +#endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index 08a741fb..d2a4d1dc 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.80 WIP +// dear imgui, v1.81 // (widgets code) /* @@ -40,6 +40,7 @@ Index of this file: #endif #include "imgui_internal.h" +// System includes #include // toupper #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t @@ -47,6 +48,10 @@ Index of this file: #include // intptr_t #endif +//------------------------------------------------------------------------- +// Warnings +//------------------------------------------------------------------------- + // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant @@ -72,15 +77,19 @@ Index of this file: #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind -#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked -#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked +#pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif //------------------------------------------------------------------------- // Data //------------------------------------------------------------------------- +// Widgets +static const float DRAGDROP_HOLD_TO_OPEN_TIMER = 0.70f; // Time for drag-hold to activate items accepting the ImGuiButtonFlags_PressedOnDragDropHold button behavior. +static const float DRAG_MOUSE_THRESHOLD_FACTOR = 0.50f; // Multiplier for the default value of io.MouseDragThreshold to make DragFloat/DragInt react faster to mouse drags. + // Those MIN/MAX values are not define because we need to point to them static const signed char IM_S8_MIN = -128; static const signed char IM_S8_MAX = 127; @@ -511,10 +520,9 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) if (IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) { - const float DRAG_DROP_HOLD_TIMER = 0.70f; hovered = true; SetHoveredID(id); - if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, DRAG_DROP_HOLD_TIMER, 0.00f)) + if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, DRAGDROP_HOLD_TO_OPEN_TIMER, 0.00f)) { pressed = true; g.DragDropHoldJustPressedId = id; @@ -684,6 +692,9 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); RenderNavHighlight(bb, id); RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); + + if (g.LogEnabled) + LogSetNextTextDecoration("[", "]"); RenderTextClipped(bb.Min + style.FramePadding, bb.Max - style.FramePadding, label, NULL, &label_size, style.ButtonTextAlign, &bb); // Automatically close popups @@ -1058,7 +1069,10 @@ bool ImGui::Checkbox(const char* label, bool* v) const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id)) + { + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); return false; + } bool hovered, held; bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); @@ -1086,10 +1100,11 @@ bool ImGui::Checkbox(const char* label, bool* v) RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad * 2.0f); } + ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y); if (g.LogEnabled) - LogRenderedText(&total_bb.Min, mixed_value ? "[~]" : *v ? "[x]" : "[ ]"); + LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]"); if (label_size.x > 0.0f) - RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); + RenderText(label_pos, label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); return pressed; @@ -1134,6 +1149,16 @@ bool ImGui::CheckboxFlags(const char* label, unsigned int* flags, unsigned int f return CheckboxFlagsT(label, flags, flags_value); } +bool ImGui::CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value) +{ + return CheckboxFlagsT(label, flags, flags_value); +} + +bool ImGui::CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value) +{ + return CheckboxFlagsT(label, flags, flags_value); +} + bool ImGui::RadioButton(const char* label, bool active) { ImGuiWindow* window = GetCurrentWindow(); @@ -1177,10 +1202,11 @@ bool ImGui::RadioButton(const char* label, bool active) window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); } + ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y); if (g.LogEnabled) - LogRenderedText(&total_bb.Min, active ? "(x)" : "( )"); + LogRenderedText(&label_pos, active ? "(x)" : "( )"); if (label_size.x > 0.0f) - RenderText(ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y), label); + RenderText(label_pos, label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); return pressed; @@ -1364,7 +1390,8 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) // Draw window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); if (g.LogEnabled) - LogRenderedText(&bb.Min, "--------------------------------"); + LogRenderedText(&bb.Min, "--------------------------------\n"); + } if (columns) { @@ -1555,7 +1582,12 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF } RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) - RenderTextClipped(frame_bb.Min + style.FramePadding, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f, 0.0f)); + { + ImVec2 preview_pos = frame_bb.Min + style.FramePadding; + if (g.LogEnabled) + LogSetNextTextDecoration("{", "}"); + RenderTextClipped(preview_pos, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f, 0.0f)); + } if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); @@ -1590,13 +1622,17 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF char name[16]; ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth - // Peak into expected window size so we can position it + // Position the window given a custom constraint (peak into expected window size so we can position it) + // This might be easier to express with an hypothetical SetNextWindowPosConstraints() function. if (ImGuiWindow* popup_window = FindWindowByName(name)) if (popup_window->WasActive) { - ImVec2 size_expected = CalcWindowExpectedSize(popup_window); + // Always override 'AutoPosLastDirection' to not leave a chance for a past value to affect us. + ImVec2 size_expected = CalcWindowNextAutoFitSize(popup_window); if (flags & ImGuiComboFlags_PopupAlignLeft) - popup_window->AutoPosLastDirection = ImGuiDir_Left; + popup_window->AutoPosLastDirection = ImGuiDir_Left; // "Below, Toward Left" + else + popup_window->AutoPosLastDirection = ImGuiDir_Down; // "Below, Toward Right (default)" ImRect r_outer = GetWindowAllowedExtentRect(popup_window); ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox); SetNextWindowPos(pos); @@ -1691,6 +1727,9 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi } EndCombo(); + if (value_changed) + MarkItemEdited(g.CurrentWindow->DC.LastItemId); + return value_changed; } @@ -2083,7 +2122,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings float adjust_delta = 0.0f; - if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && g.IO.MouseDragMaxDistanceSqr[0] > 1.0f * 1.0f) + if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR)) { adjust_delta = g.IO.MouseDelta[axis]; if (g.IO.KeyAlt) @@ -2246,7 +2285,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) format = PatchFormatStringFloatToInt(format); - // Tabbing or CTRL-clicking on Drag turns it into an input box + // Tabbing or CTRL-clicking on Drag turns it into an InputText const bool hovered = ItemHoverable(frame_bb, id); const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); @@ -2267,6 +2306,15 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, FocusableItemUnregister(window); } } + // Experimental: simple click (without moving) turns Drag into an InputText + // FIXME: Currently polling ImGuiConfigFlags_IsTouchScreen, may either poll an hypothetical ImGuiBackendFlags_HasKeyboard and/or an explicit drag settings. + if (g.IO.ConfigDragClickToInputText && temp_input_allowed && !temp_input_is_active) + if (g.ActiveId == id && hovered && g.IO.MouseReleased[0] && !IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR)) + { + g.NavInputId = id; + temp_input_is_active = true; + FocusableItemUnregister(window); + } } if (temp_input_is_active) @@ -2289,6 +2337,8 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); + if (g.LogEnabled) + LogSetNextTextDecoration("{", "}"); RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); if (label_size.x > 0.0f) @@ -2552,7 +2602,7 @@ template TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize) { if (v_min == v_max) - return (TYPE)0.0f; + return v_min; const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); TYPE result; @@ -2901,6 +2951,8 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Display value using user-provided display format so user can add prefix/suffix/decorations to the value. char value_buf[64]; const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format); + if (g.LogEnabled) + LogSetNextTextDecoration("{", "}"); RenderTextClipped(frame_bb.Min, frame_bb.Max, value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f)); if (label_size.x > 0.0f) @@ -3816,9 +3868,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); - PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding); - bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding); - PopStyleVar(3); + bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove); + PopStyleVar(2); PopStyleColor(); if (!child_visible) { @@ -3828,6 +3879,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } draw_window = g.CurrentWindow; // Child window draw_window->DC.NavLayerActiveMaskNext |= (1 << draw_window->DC.NavLayerCurrent); // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. + draw_window->DC.CursorPos += style.FramePadding; inner_size.x -= draw_window->ScrollbarSizes.x; } else @@ -3988,7 +4040,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Edit in progress const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; - const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y - style.FramePadding.y) : (g.FontSize * 0.5f)); + const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y) : (g.FontSize * 0.5f)); const bool is_osx = io.ConfigMacOSXBehaviors; if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0])) @@ -4437,10 +4489,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!(flags & ImGuiInputTextFlags_NoHorizontalScroll)) { const float scroll_increment_x = inner_size.x * 0.25f; + const float visible_width = inner_size.x - style.FramePadding.x; if (cursor_offset.x < state->ScrollX) state->ScrollX = IM_FLOOR(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); - else if (cursor_offset.x - inner_size.x >= state->ScrollX) - state->ScrollX = IM_FLOOR(cursor_offset.x - inner_size.x + scroll_increment_x); + else if (cursor_offset.x - visible_width >= state->ScrollX) + state->ScrollX = IM_FLOOR(cursor_offset.x - visible_width + scroll_increment_x); } else { @@ -4545,14 +4598,17 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_multiline) { - Dummy(text_size); + Dummy(ImVec2(text_size.x, text_size.y + style.FramePadding.y)); EndChild(); EndGroup(); } // Log as text if (g.LogEnabled && (!is_password || is_displaying_hint)) + { + LogSetNextTextDecoration("{", "}"); LogRenderedText(&draw_pos, buf_display, buf_display_end); + } if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); @@ -5759,19 +5815,10 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l text_pos.x -= text_offset_x; if (flags & ImGuiTreeNodeFlags_ClipLabelForTrailingButton) frame_bb.Max.x -= g.FontSize + style.FramePadding.x; + if (g.LogEnabled) - { - // NB: '##' is normally used to hide text (as a library-wide feature), so we need to specify the text range to make sure the ## aren't stripped out here. - const char log_prefix[] = "\n##"; - const char log_suffix[] = "##"; - LogRenderedText(&text_pos, log_prefix, log_prefix + 3); - RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); - LogRenderedText(&text_pos, log_suffix, log_suffix + 2); - } - else - { - RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); - } + LogSetNextTextDecoration("###", "###"); + RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); } else { @@ -5787,7 +5834,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l else if (!is_leaf) RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f); if (g.LogEnabled) - LogRenderedText(&text_pos, ">"); + LogSetNextTextDecoration(">", NULL); RenderText(text_pos, label, label_end, false); } @@ -5873,21 +5920,25 @@ bool ImGui::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) return TreeNodeBehavior(window->GetID(label), flags | ImGuiTreeNodeFlags_CollapsingHeader, label); } -bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags flags) +// p_visible == NULL : regular collapsing header +// p_visible != NULL && *p_visible == true : show a small close button on the corner of the header, clicking the button will set *p_visible = false +// p_visible != NULL && *p_visible == false : do not show the header at all +// Do not mistake this with the Open state of the header itself, which you can adjust with SetNextItemOpen() or ImGuiTreeNodeFlags_DefaultOpen. +bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFlags flags) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - if (p_open && !*p_open) + if (p_visible && !*p_visible) return false; ImGuiID id = window->GetID(label); flags |= ImGuiTreeNodeFlags_CollapsingHeader; - if (p_open) + if (p_visible) flags |= ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; bool is_open = TreeNodeBehavior(id, flags, label); - if (p_open != NULL) + if (p_visible != NULL) { // Create a small overlapping close button // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc. @@ -5899,7 +5950,7 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_open, ImGuiTreeNodeFlags float button_y = window->DC.LastItemRect.Min.y; ImGuiID close_button_id = GetIDWithSeed("#CLOSE", NULL, id); if (CloseButton(close_button_id, ImVec2(button_x, button_y))) - *p_open = false; + *p_visible = false; last_item_backup.Restore(); } @@ -5934,7 +5985,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ItemSize(size, 0.0f); // Fill horizontal space - // We don't support (size < 0.0f) in Selectable() because the ItemSpacing extension would make explicitely right-aligned sizes not visibly match other widgets. + // We don't support (size < 0.0f) in Selectable() because the ItemSpacing extension would make explicitly right-aligned sizes not visibly match other widgets. const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0; const float min_x = span_all_columns ? window->ParentWorkRect.Min.x : pos.x; const float max_x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x; @@ -5949,7 +6000,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl ImRect bb(min_x, pos.y, text_max.x, text_max.y); if ((flags & ImGuiSelectableFlags_NoPadWithHalfSpacing) == 0) { - const float spacing_x = style.ItemSpacing.x; + const float spacing_x = span_all_columns ? 0.0f : style.ItemSpacing.x; const float spacing_y = style.ItemSpacing.y; const float spacing_L = IM_FLOOR(spacing_x * 0.50f); const float spacing_U = IM_FLOOR(spacing_y * 0.50f); @@ -5995,6 +6046,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // which would be advantageous since most selectable are not selected. if (span_all_columns && window->DC.CurrentColumns) PushColumnsBackground(); + else if (span_all_columns && g.CurrentTable) + TablePushBackgroundChannel(); // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; @@ -6043,6 +6096,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (span_all_columns && window->DC.CurrentColumns) PopColumnsBackground(); + else if (span_all_columns && g.CurrentTable) + TablePopBackgroundChannel(); if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); @@ -6069,18 +6124,14 @@ bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags //------------------------------------------------------------------------- // [SECTION] Widgets: ListBox //------------------------------------------------------------------------- +// - BeginListBox() +// - EndListBox() // - ListBox() -// - ListBoxHeader() -// - ListBoxFooter() -//------------------------------------------------------------------------- -// FIXME: This is an old API. We should redesign some of it, rename ListBoxHeader->BeginListBox, ListBoxFooter->EndListBox -// and promote using them over existing ListBox() functions, similarly to change with combo boxes. //------------------------------------------------------------------------- -// FIXME: In principle this function should be called BeginListBox(). We should rename it after re-evaluating if we want to keep the same signature. -// Helper to calculate the size of a listbox and display a label on the right. -// Tip: To have a list filling the entire window width, PushItemWidth(-1) and pass an non-visible label e.g. "##empty" -bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg) +// Tip: To have a list filling the entire window width, use size.x = -FLT_MIN and pass an non-visible label e.g. "##empty" +// Tip: If your vertical size is calculated from an item count (e.g. 10 * item_height) consider adding a fractional part to facilitate seeing scrolling boundaries (e.g. 10.25 * item_height). +bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -6091,12 +6142,12 @@ bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg) const ImGuiID id = GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); - // Size default to hold ~7 items. Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. - ImVec2 size = CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.4f + style.ItemSpacing.y); + // Size default to hold ~7.25 items. + // Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. + ImVec2 size = ImFloor(CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.25f + style.FramePadding.y * 2.0f)); ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); - window->DC.LastItemRect = bb; // Forward storage for ListBoxFooter.. dodgy. g.NextItemData.ClearFlags(); if (!IsRectVisible(bb.Min, bb.Max)) @@ -6106,48 +6157,41 @@ bool ImGui::ListBoxHeader(const char* label, const ImVec2& size_arg) return false; } + // FIXME-OPT: We could omit the BeginGroup() if label_size.x but would need to omit the EndGroup() as well. BeginGroup(); - if (label_size.x > 0) - RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); + if (label_size.x > 0.0f) + { + ImVec2 label_pos = ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y); + RenderText(label_pos, label); + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, label_pos + label_size); + } BeginChildFrame(id, frame_bb.GetSize()); return true; } -// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +// OBSOLETED in 1.81 (from February 2021) bool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items) { - // Size default to hold ~7.25 items. - // We add +25% worth of item height to allow the user to see at a glance if there are more items up/down, without looking at the scrollbar. - // We don't add this extra bit if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size. - // I am expecting that someone will come and complain about this behavior in a remote future, then we can advise on a better solution. - if (height_in_items < 0) - height_in_items = ImMin(items_count, 7); - const ImGuiStyle& style = GetStyle(); - float height_in_items_f = (height_in_items < items_count) ? (height_in_items + 0.25f) : (height_in_items + 0.00f); - - // We include ItemSpacing.y so that a list sized for the exact number of items doesn't make a scrollbar appears. We could also enforce that by passing a flag to BeginChild(). + // If height_in_items == -1, default height is maximum 7. + ImGuiContext& g = *GImGui; + float height_in_items_f = (height_in_items < 0 ? ImMin(items_count, 7) : height_in_items) + 0.25f; ImVec2 size; size.x = 0.0f; - size.y = ImFloor(GetTextLineHeightWithSpacing() * height_in_items_f + style.FramePadding.y * 2.0f); - return ListBoxHeader(label, size); + size.y = GetTextLineHeightWithSpacing() * height_in_items_f + g.Style.FramePadding.y * 2.0f; + return BeginListBox(label, size); } +#endif -// FIXME: In principle this function should be called EndListBox(). We should rename it after re-evaluating if we want to keep the same signature. -void ImGui::ListBoxFooter() +void ImGui::EndListBox() { - ImGuiWindow* parent_window = GetCurrentWindow()->ParentWindow; - const ImRect bb = parent_window->DC.LastItemRect; - const ImGuiStyle& style = GetStyle(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT((window->Flags & ImGuiWindowFlags_ChildWindow) && "Mismatched BeginListBox/EndListBox calls. Did you test the return value of BeginListBox?"); EndChildFrame(); - - // Redeclare item size so that it includes the label (we have stored the full size in LastItemRect) - // We call SameLine() to restore DC.CurrentLine* data - SameLine(); - parent_window->DC.CursorPos = bb.Min; - ItemSize(bb, style.FramePadding.y); - EndGroup(); + EndGroup(); // This is only required to be able to do IsItemXXX query on the whole ListBox including label } bool ImGui::ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_items) @@ -6156,25 +6200,35 @@ bool ImGui::ListBox(const char* label, int* current_item, const char* const item return value_changed; } +// This is merely a helper around BeginListBox(), EndListBox(). +// Considering using those directly to submit custom data or store selection differently. bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items) { - if (!ListBoxHeader(label, items_count, height_in_items)) + ImGuiContext& g = *GImGui; + + // Calculate size from "height_in_items" + if (height_in_items < 0) + height_in_items = ImMin(items_count, 7); + float height_in_items_f = height_in_items + 0.25f; + ImVec2 size(0.0f, ImFloor(GetTextLineHeightWithSpacing() * height_in_items_f + g.Style.FramePadding.y * 2.0f)); + + if (!BeginListBox(label, size)) return false; - // Assume all items have even height (= 1 line of text). If you need items of different or variable sizes you can create a custom version of ListBox() in your code without using the clipper. - ImGuiContext& g = *GImGui; + // Assume all items have even height (= 1 line of text). If you need items of different height, + // you can create a custom version of ListBox() in your code without using the clipper. bool value_changed = false; ImGuiListClipper clipper; clipper.Begin(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to. while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { - const bool item_selected = (i == *current_item); const char* item_text; if (!items_getter(data, i, &item_text)) item_text = "*Unknown item*"; PushID(i); + const bool item_selected = (i == *current_item); if (Selectable(item_text, item_selected)) { *current_item = i; @@ -6184,7 +6238,7 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v SetItemDefaultFocus(); PopID(); } - ListBoxFooter(); + EndListBox(); if (value_changed) MarkItemEdited(g.CurrentWindow->DC.LastItemId); @@ -6198,6 +6252,11 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v // - PlotLines() // - PlotHistogram() //------------------------------------------------------------------------- +// Plot/Graph widgets are not very good. +// Consider writing your own, or using a third-party one, see: +// - ImPlot https://github.com/epezent/implot +// - others https://github.com/ocornut/imgui/wiki/Useful-Widgets +//------------------------------------------------------------------------- int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) { @@ -6461,7 +6520,7 @@ bool ImGui::BeginMenuBar() clip_rect.ClipWith(window->OuterRectClipped); PushClipRect(clip_rect.Min, clip_rect.Max, false); - // We overwrite CursorMaxPos because BeginGroup sets it to CursorPos (essentially the .EmitItem hack in EndMenuBar() would need something analoguous here, maybe a BeginGroupEx() with flags). + // We overwrite CursorMaxPos because BeginGroup sets it to CursorPos (essentially the .EmitItem hack in EndMenuBar() would need something analogous here, maybe a BeginGroupEx() with flags). window->DC.CursorPos = window->DC.CursorMaxPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); window->DC.LayoutType = ImGuiLayoutType_Horizontal; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; @@ -6510,18 +6569,39 @@ void ImGui::EndMenuBar() window->DC.MenuBarAppending = false; } -// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. bool ImGui::BeginMainMenuBar() { ImGuiContext& g = *GImGui; + ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); + ImGuiWindow* menu_bar_window = FindWindowByName("##MainMenuBar"); + + // For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); - SetNextWindowPos(ImVec2(0.0f, 0.0f)); - SetNextWindowSize(ImVec2(g.IO.DisplaySize.x, g.NextWindowData.MenuBarOffsetMinVal.y + g.FontBaseSize + g.Style.FramePadding.y)); + + // Get our rectangle at the top of the work area + if (menu_bar_window == NULL || menu_bar_window->BeginCount == 0) + { + // Set window position + // We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. + // However menu-bar will affect the minimum window size so we'll get the right height. + ImVec2 menu_bar_pos = viewport->Pos + viewport->CurrWorkOffsetMin; + ImVec2 menu_bar_size = ImVec2(viewport->Size.x - viewport->CurrWorkOffsetMin.x + viewport->CurrWorkOffsetMax.x, 1.0f); + SetNextWindowPos(menu_bar_pos); + SetNextWindowSize(menu_bar_size); + } + + // Create window PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); + PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); // Lift normal size constraint, however the presence of a menu-bar will give us the minimum height we want. ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); PopStyleVar(2); + + // Report our size into work area (for next frame) using actual window size + menu_bar_window = GetCurrentWindow(); + if (menu_bar_window->BeginCount == 1) + viewport->CurrWorkOffsetMin.y += menu_bar_window->Size.y; + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); if (!is_open) { @@ -6697,7 +6777,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) if (menu_is_open) { - SetNextWindowPos(popup_pos, ImGuiCond_Always); + SetNextWindowPos(popup_pos, ImGuiCond_Always); // Note: this is super misleading! The value will serve as reference for FindBestWindowPosForPopup(), not actual pos. menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) } else @@ -7201,7 +7281,8 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Actual layout in host window (we don't do it in BeginTabBar() so as not to waste an extra frame) ImGuiWindow* window = g.CurrentWindow; window->DC.CursorPos = tab_bar->BarRect.Min; - ItemSize(ImVec2(tab_bar->WidthAllTabsIdeal, tab_bar->BarRect.GetHeight()), tab_bar->FramePadding.y); + ItemSize(ImVec2(tab_bar->WidthAllTabs, tab_bar->BarRect.GetHeight()), tab_bar->FramePadding.y); + window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, tab_bar->BarRect.Min.x + tab_bar->WidthAllTabsIdeal); } // Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack. @@ -7651,7 +7732,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, hovered |= (g.HoveredId == id); // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) - if (!held) + if (g.ActiveId != id) SetItemAllowOverlap(); // Drag and drop: re-order tabs @@ -7674,7 +7755,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } #if 0 - if (hovered && g.HoveredIdNotActiveTimer > 0.50f && bb.GetWidth() < tab->ContentWidth) + if (hovered && g.HoveredIdNotActiveTimer > TOOLTIP_DELAY && bb.GetWidth() < tab->ContentWidth) { // Enlarge tab display when hovering bb.Max.x = bb.Min.x + IM_FLOOR(ImLerp(bb.GetWidth(), tab->ContentWidth, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f))); @@ -7716,7 +7797,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) - if (text_clipped && g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > 0.50f && IsItemHovered()) + if (text_clipped && g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay && IsItemHovered()) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); @@ -7862,445 +7943,4 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, } -//------------------------------------------------------------------------- -// [SECTION] Widgets: Columns, BeginColumns, EndColumns, etc. -// In the current version, Columns are very weak. Needs to be replaced with a more full-featured system. -//------------------------------------------------------------------------- -// - SetWindowClipRectBeforeSetChannel() [Internal] -// - GetColumnIndex() -// - GetColumnCount() -// - GetColumnOffset() -// - GetColumnWidth() -// - SetColumnOffset() -// - SetColumnWidth() -// - PushColumnClipRect() [Internal] -// - PushColumnsBackground() [Internal] -// - PopColumnsBackground() [Internal] -// - FindOrCreateColumns() [Internal] -// - GetColumnsID() [Internal] -// - BeginColumns() -// - NextColumn() -// - EndColumns() -// - Columns() -//------------------------------------------------------------------------- - -// [Internal] Small optimization to avoid calls to PopClipRect/SetCurrentChannel/PushClipRect in sequences, -// they would meddle many times with the underlying ImDrawCmd. -// Instead, we do a preemptive overwrite of clipping rectangle _without_ altering the command-buffer and let -// the subsequent single call to SetCurrentChannel() does it things once. -void ImGui::SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect) -{ - ImVec4 clip_rect_vec4 = clip_rect.ToVec4(); - window->ClipRect = clip_rect; - window->DrawList->_CmdHeader.ClipRect = clip_rect_vec4; - window->DrawList->_ClipRectStack.Data[window->DrawList->_ClipRectStack.Size - 1] = clip_rect_vec4; -} - -int ImGui::GetColumnIndex() -{ - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.CurrentColumns ? window->DC.CurrentColumns->Current : 0; -} - -int ImGui::GetColumnsCount() -{ - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.CurrentColumns ? window->DC.CurrentColumns->Count : 1; -} - -float ImGui::GetColumnOffsetFromNorm(const ImGuiOldColumns* columns, float offset_norm) -{ - return offset_norm * (columns->OffMaxX - columns->OffMinX); -} - -float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offset) -{ - return offset / (columns->OffMaxX - columns->OffMinX); -} - -static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; - -static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index) -{ - // Active (dragged) column always follow mouse. The reason we need this is that dragging a column to the right edge of an auto-resizing - // window creates a feedback loop because we store normalized positions. So while dragging we enforce absolute positioning. - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(column_index > 0); // We are not supposed to drag column 0. - IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index)); - - float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; - x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); - if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths)) - x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); - - return x; -} - -float ImGui::GetColumnOffset(int column_index) -{ - ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (columns == NULL) - return 0.0f; - - if (column_index < 0) - column_index = columns->Current; - IM_ASSERT(column_index < columns->Columns.Size); - - const float t = columns->Columns[column_index].OffsetNorm; - const float x_offset = ImLerp(columns->OffMinX, columns->OffMaxX, t); - return x_offset; -} - -static float GetColumnWidthEx(ImGuiOldColumns* columns, int column_index, bool before_resize = false) -{ - if (column_index < 0) - column_index = columns->Current; - - float offset_norm; - if (before_resize) - offset_norm = columns->Columns[column_index + 1].OffsetNormBeforeResize - columns->Columns[column_index].OffsetNormBeforeResize; - else - offset_norm = columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm; - return ImGui::GetColumnOffsetFromNorm(columns, offset_norm); -} - -float ImGui::GetColumnWidth(int column_index) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (columns == NULL) - return GetContentRegionAvail().x; - - if (column_index < 0) - column_index = columns->Current; - return GetColumnOffsetFromNorm(columns, columns->Columns[column_index + 1].OffsetNorm - columns->Columns[column_index].OffsetNorm); -} - -void ImGui::SetColumnOffset(int column_index, float offset) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiOldColumns* columns = window->DC.CurrentColumns; - IM_ASSERT(columns != NULL); - - if (column_index < 0) - column_index = columns->Current; - IM_ASSERT(column_index < columns->Columns.Size); - - const bool preserve_width = !(columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths) && (column_index < columns->Count - 1); - const float width = preserve_width ? GetColumnWidthEx(columns, column_index, columns->IsBeingResized) : 0.0f; - - if (!(columns->Flags & ImGuiOldColumnFlags_NoForceWithinWindow)) - offset = ImMin(offset, columns->OffMaxX - g.Style.ColumnsMinSpacing * (columns->Count - column_index)); - columns->Columns[column_index].OffsetNorm = GetColumnNormFromOffset(columns, offset - columns->OffMinX); - - if (preserve_width) - SetColumnOffset(column_index + 1, offset + ImMax(g.Style.ColumnsMinSpacing, width)); -} - -void ImGui::SetColumnWidth(int column_index, float width) -{ - ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - IM_ASSERT(columns != NULL); - - if (column_index < 0) - column_index = columns->Current; - SetColumnOffset(column_index + 1, GetColumnOffset(column_index) + width); -} - -void ImGui::PushColumnClipRect(int column_index) -{ - ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (column_index < 0) - column_index = columns->Current; - - ImGuiOldColumnData* column = &columns->Columns[column_index]; - PushClipRect(column->ClipRect.Min, column->ClipRect.Max, false); -} - -// Get into the columns background draw command (which is generally the same draw command as before we called BeginColumns) -void ImGui::PushColumnsBackground() -{ - ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (columns->Count == 1) - return; - - // Optimization: avoid SetCurrentChannel() + PushClipRect() - columns->HostBackupClipRect = window->ClipRect; - SetWindowClipRectBeforeSetChannel(window, columns->HostInitialClipRect); - columns->Splitter.SetCurrentChannel(window->DrawList, 0); -} - -void ImGui::PopColumnsBackground() -{ - ImGuiWindow* window = GetCurrentWindowRead(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (columns->Count == 1) - return; - - // Optimization: avoid PopClipRect() + SetCurrentChannel() - SetWindowClipRectBeforeSetChannel(window, columns->HostBackupClipRect); - columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); -} - -ImGuiOldColumns* ImGui::FindOrCreateColumns(ImGuiWindow* window, ImGuiID id) -{ - // We have few columns per window so for now we don't need bother much with turning this into a faster lookup. - for (int n = 0; n < window->ColumnsStorage.Size; n++) - if (window->ColumnsStorage[n].ID == id) - return &window->ColumnsStorage[n]; - - window->ColumnsStorage.push_back(ImGuiOldColumns()); - ImGuiOldColumns* columns = &window->ColumnsStorage.back(); - columns->ID = id; - return columns; -} - -ImGuiID ImGui::GetColumnsID(const char* str_id, int columns_count) -{ - ImGuiWindow* window = GetCurrentWindow(); - - // Differentiate column ID with an arbitrary prefix for cases where users name their columns set the same as another widget. - // In addition, when an identifier isn't explicitly provided we include the number of columns in the hash to make it uniquer. - PushID(0x11223347 + (str_id ? 0 : columns_count)); - ImGuiID id = window->GetID(str_id ? str_id : "columns"); - PopID(); - - return id; -} - -void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFlags flags) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - - IM_ASSERT(columns_count >= 1); - IM_ASSERT(window->DC.CurrentColumns == NULL); // Nested columns are currently not supported - - // Acquire storage for the columns set - ImGuiID id = GetColumnsID(str_id, columns_count); - ImGuiOldColumns* columns = FindOrCreateColumns(window, id); - IM_ASSERT(columns->ID == id); - columns->Current = 0; - columns->Count = columns_count; - columns->Flags = flags; - window->DC.CurrentColumns = columns; - - columns->HostCursorPosY = window->DC.CursorPos.y; - columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; - columns->HostInitialClipRect = window->ClipRect; - columns->HostBackupParentWorkRect = window->ParentWorkRect; - window->ParentWorkRect = window->WorkRect; - - // Set state for first column - // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect - const float column_padding = g.Style.ItemSpacing.x; - const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize)); - const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f); - const float max_2 = window->WorkRect.Max.x + half_clip_extend_x; - columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f); - columns->OffMaxX = ImMax(ImMin(max_1, max_2) - window->Pos.x, columns->OffMinX + 1.0f); - columns->LineMinY = columns->LineMaxY = window->DC.CursorPos.y; - - // Clear data if columns count changed - if (columns->Columns.Size != 0 && columns->Columns.Size != columns_count + 1) - columns->Columns.resize(0); - - // Initialize default widths - columns->IsFirstFrame = (columns->Columns.Size == 0); - if (columns->Columns.Size == 0) - { - columns->Columns.reserve(columns_count + 1); - for (int n = 0; n < columns_count + 1; n++) - { - ImGuiOldColumnData column; - column.OffsetNorm = n / (float)columns_count; - columns->Columns.push_back(column); - } - } - - for (int n = 0; n < columns_count; n++) - { - // Compute clipping rectangle - ImGuiOldColumnData* column = &columns->Columns[n]; - float clip_x1 = IM_ROUND(window->Pos.x + GetColumnOffset(n)); - float clip_x2 = IM_ROUND(window->Pos.x + GetColumnOffset(n + 1) - 1.0f); - column->ClipRect = ImRect(clip_x1, -FLT_MAX, clip_x2, +FLT_MAX); - column->ClipRect.ClipWithFull(window->ClipRect); - } - - if (columns->Count > 1) - { - columns->Splitter.Split(window->DrawList, 1 + columns->Count); - columns->Splitter.SetCurrentChannel(window->DrawList, 1); - PushColumnClipRect(0); - } - - // We don't generally store Indent.x inside ColumnsOffset because it may be manipulated by the user. - float offset_0 = GetColumnOffset(columns->Current); - float offset_1 = GetColumnOffset(columns->Current + 1); - float width = offset_1 - offset_0; - PushItemWidth(width * 0.65f); - window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); - window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; -} - -void ImGui::NextColumn() -{ - ImGuiWindow* window = GetCurrentWindow(); - if (window->SkipItems || window->DC.CurrentColumns == NULL) - return; - - ImGuiContext& g = *GImGui; - ImGuiOldColumns* columns = window->DC.CurrentColumns; - - if (columns->Count == 1) - { - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); - IM_ASSERT(columns->Current == 0); - return; - } - - // Next column - if (++columns->Current == columns->Count) - columns->Current = 0; - - PopItemWidth(); - - // Optimization: avoid PopClipRect() + SetCurrentChannel() + PushClipRect() - // (which would needlessly attempt to update commands in the wrong channel, then pop or overwrite them), - ImGuiOldColumnData* column = &columns->Columns[columns->Current]; - SetWindowClipRectBeforeSetChannel(window, column->ClipRect); - columns->Splitter.SetCurrentChannel(window->DrawList, columns->Current + 1); - - const float column_padding = g.Style.ItemSpacing.x; - columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); - if (columns->Current > 0) - { - // Columns 1+ ignore IndentX (by canceling it out) - // FIXME-COLUMNS: Unnecessary, could be locked? - window->DC.ColumnsOffset.x = GetColumnOffset(columns->Current) - window->DC.Indent.x + column_padding; - } - else - { - // New row/line: column 0 honor IndentX. - window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - columns->LineMinY = columns->LineMaxY; - } - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); - window->DC.CursorPos.y = columns->LineMinY; - window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); - window->DC.CurrLineTextBaseOffset = 0.0f; - - // FIXME-COLUMNS: Share code with BeginColumns() - move code on columns setup. - float offset_0 = GetColumnOffset(columns->Current); - float offset_1 = GetColumnOffset(columns->Current + 1); - float width = offset_1 - offset_0; - PushItemWidth(width * 0.65f); - window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; -} - -void ImGui::EndColumns() -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - ImGuiOldColumns* columns = window->DC.CurrentColumns; - IM_ASSERT(columns != NULL); - - PopItemWidth(); - if (columns->Count > 1) - { - PopClipRect(); - columns->Splitter.Merge(window->DrawList); - } - - const ImGuiOldColumnFlags flags = columns->Flags; - columns->LineMaxY = ImMax(columns->LineMaxY, window->DC.CursorPos.y); - window->DC.CursorPos.y = columns->LineMaxY; - if (!(flags & ImGuiOldColumnFlags_GrowParentContentsSize)) - window->DC.CursorMaxPos.x = columns->HostCursorMaxPosX; // Restore cursor max pos, as columns don't grow parent - - // Draw columns borders and handle resize - // The IsBeingResized flag ensure we preserve pre-resize columns width so back-and-forth are not lossy - bool is_being_resized = false; - if (!(flags & ImGuiOldColumnFlags_NoBorder) && !window->SkipItems) - { - // We clip Y boundaries CPU side because very long triangles are mishandled by some GPU drivers. - const float y1 = ImMax(columns->HostCursorPosY, window->ClipRect.Min.y); - const float y2 = ImMin(window->DC.CursorPos.y, window->ClipRect.Max.y); - int dragging_column = -1; - for (int n = 1; n < columns->Count; n++) - { - ImGuiOldColumnData* column = &columns->Columns[n]; - float x = window->Pos.x + GetColumnOffset(n); - const ImGuiID column_id = columns->ID + ImGuiID(n); - const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; - const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); - KeepAliveID(column_id); - if (IsClippedEx(column_hit_rect, column_id, false)) - continue; - - bool hovered = false, held = false; - if (!(flags & ImGuiOldColumnFlags_NoResize)) - { - ButtonBehavior(column_hit_rect, column_id, &hovered, &held); - if (hovered || held) - g.MouseCursor = ImGuiMouseCursor_ResizeEW; - if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize)) - dragging_column = n; - } - - // Draw column - const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); - const float xi = IM_FLOOR(x); - window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); - } - - // Apply dragging after drawing the column lines, so our rendered lines are in sync with how items were displayed during the frame. - if (dragging_column != -1) - { - if (!columns->IsBeingResized) - for (int n = 0; n < columns->Count + 1; n++) - columns->Columns[n].OffsetNormBeforeResize = columns->Columns[n].OffsetNorm; - columns->IsBeingResized = is_being_resized = true; - float x = GetDraggedColumnOffset(columns, dragging_column); - SetColumnOffset(dragging_column, x); - } - } - columns->IsBeingResized = is_being_resized; - - window->WorkRect = window->ParentWorkRect; - window->ParentWorkRect = columns->HostBackupParentWorkRect; - window->DC.CurrentColumns = NULL; - window->DC.ColumnsOffset.x = 0.0f; - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); -} - -void ImGui::Columns(int columns_count, const char* id, bool border) -{ - ImGuiWindow* window = GetCurrentWindow(); - IM_ASSERT(columns_count >= 1); - - ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder); - //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior - ImGuiOldColumns* columns = window->DC.CurrentColumns; - if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) - return; - - if (columns != NULL) - EndColumns(); - - if (columns_count != 1) - BeginColumns(id, columns_count, flags); -} - -//------------------------------------------------------------------------- - #endif // #ifndef IMGUI_DISABLE diff --git a/imgui/imstb_truetype.h b/imgui/imstb_truetype.h index b4bdbd86..fc815d74 100644 --- a/imgui/imstb_truetype.h +++ b/imgui/imstb_truetype.h @@ -4302,7 +4302,7 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex int winding = 0; orig[0] = x; - //orig[1] = y; // [DEAR IMGUI] commmented double assignment + //orig[1] = y; // [DEAR IMGUI] commented double assignment // make sure y never passes through a vertex of the shape y_frac = (float) STBTT_fmod(y, 1.0f); From 793452d8ec8defa99730947d967680ec1afefb13 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 3 Mar 2021 23:53:09 +0100 Subject: [PATCH 0453/1018] add convex hull andrew's algorithm, small test --- gproshan.cpp | 17 ++++++++++ include/geometry/convex_hull.h | 33 ++++++++++++++++++++ src/geometry/convex_hull.cpp | 57 ++++++++++++++++++++++++++++++++++ src/mdict/basis_cosine.cpp | 10 +++--- 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 include/geometry/convex_hull.h create mode 100644 src/geometry/convex_hull.cpp diff --git a/gproshan.cpp b/gproshan.cpp index a0c142af..d95ed598 100644 --- a/gproshan.cpp +++ b/gproshan.cpp @@ -1,7 +1,24 @@ #include "app_viewer.h" +#include "geometry/convex_hull.h" + +using namespace std; int main(int nargs, const char ** args) { + std::vector vv{ {0, 2, 0}, + {1, 0, 0}, + {2, 5, 0}, + {3, 3, 0}, + {4, -1, 0}, + {2, 2, 0}, + {5, 3, 0}, + {6, 4, 0} }; + + gproshan::convex_hull ch(vv); + const std::vector & p = ch; + for(auto v: p) + gproshan_log_var(v); + gproshan::app_viewer app; return app.main(nargs, args); } diff --git a/include/geometry/convex_hull.h b/include/geometry/convex_hull.h new file mode 100644 index 00000000..8c6ed9a2 --- /dev/null +++ b/include/geometry/convex_hull.h @@ -0,0 +1,33 @@ +#ifndef CONVEX_HULL_H +#define CONVEX_HULL_H + +#include "mesh/vertex.h" + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +///< 2D Convex Hull +class convex_hull +{ + private: + std::vector CH; ///< convex hull points clockwise + + public: + convex_hull(std::vector & points); + convex_hull(vertex * points, const size_t & n_points); + operator const std::vector & (); + + private: + void andrew_algorithm(vertex * points, const size_t & n_points); + bool ccw(const vertex & p, const vertex & q, const vertex & r); +}; + + +} // namespace gproshan + +#endif // CONVEX_HULL_H + diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp new file mode 100644 index 00000000..7e2141f6 --- /dev/null +++ b/src/geometry/convex_hull.cpp @@ -0,0 +1,57 @@ +#include "geometry/convex_hull.h" + + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +convex_hull::convex_hull(std::vector & points): convex_hull(points.data(), points.size()) {} + +convex_hull::convex_hull(vertex * points, const size_t & n_points) +{ + andrew_algorithm(points, n_points); +} + +convex_hull::operator const std::vector & () +{ + return CH; +} + +///< Andrew's Convex Hull Algorithm: Competitive Programming 4 +void convex_hull::andrew_algorithm(vertex * points, const size_t & n_points) +{ + std::sort(points, points + n_points); + + index_t t, k = 0; + CH.resize(2 * n_points); + + for(index_t i = 0; i < n_points; ++i) + { + while(k > 1 && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; + CH[k++] = points[i]; + } + + t = k; + for(index_t i = n_points - 2; i > 0; --i) + { + while(k >= t && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; + CH[k++] = points[i]; + } + + //evaluate condition with the first point + while(k >= t && !ccw(CH[k - 2], CH[k - 1], points[0])) --k; + + CH.resize(k); +} + +bool convex_hull::ccw(const vertex & p, const vertex & q, const vertex & r) +{ + return ((q - p) * (r - q)).z > 0; +} + + +} // namespace gproshan + diff --git a/src/mdict/basis_cosine.cpp b/src/mdict/basis_cosine.cpp index 6f1fd955..d6e3d978 100644 --- a/src/mdict/basis_cosine.cpp +++ b/src/mdict/basis_cosine.cpp @@ -17,8 +17,8 @@ void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) real_t d = 1.0 / (n_rot - 1); real_t c; - for(size_t k = 0, ni = 1; ni <= n_freq; ni++ ) - for(real_t alpha = 0; alpha <= 1; alpha += d, k++) + for(size_t k = 0, ni = 1; ni <= n_freq; ++ni) + for(real_t alpha = 0; alpha <= 1; alpha += d, ++k) { c = ni * M_PI / _radio; phi.col(k) = cosine(x, y, c, alpha); @@ -32,7 +32,7 @@ void basis_cosine::plot_basis(ostream & os) os << "set multiplot layout " << n_freq << "," << n_rot << " rowsfirst scale 1.2;" << endl; - for(size_t ni = 1; ni <= n_freq; ni++ ) + for(size_t ni = 1; ni <= n_freq; ++ni) for(real_t alpha = 0; alpha <= 1; alpha += d) { c = ni * M_PI / _radio; @@ -45,8 +45,8 @@ void basis_cosine::plot_atoms(ostream & os, const a_vec & A) real_t d = 1.0 / (n_rot - 1); real_t c; - for(size_t k = 0, ni = 1; ni <= n_freq; ni++ ) - for(real_t alpha = 0; alpha <= 1; alpha += d, k++) + for(size_t k = 0, ni = 1; ni <= n_freq; ++ni) + for(real_t alpha = 0; alpha <= 1; alpha += d, ++k) { c = ni * M_PI / _radio; os << " + " << A(k) << " * "; cosine(os, c, alpha); From dd767152522ff0fc4415ba552fc97f024d5c2658 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 4 Mar 2021 20:57:34 +0100 Subject: [PATCH 0454/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83a8a42e..037a04d1 100644 --- a/README.md +++ b/README.md @@ -133,4 +133,4 @@ MIT License ## Authors/Contributors - [Luciano A. Romero Calla](https://github.com/larc) -- [Lizeth J. Fuentes Pérez](https://github.com/lishh) +- [Lizeth J. Fuentes Pérez](https://github.com/lizonly) From d81f2d7b563f0f01b04f751c26fd2897cc62d19c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 6 Mar 2021 20:53:56 +0100 Subject: [PATCH 0455/1018] convex hull algorithm fixed --- src/geometry/convex_hull.cpp | 11 +++++------ src/mdict/patch.cpp | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp index 7e2141f6..4d5b5480 100644 --- a/src/geometry/convex_hull.cpp +++ b/src/geometry/convex_hull.cpp @@ -25,31 +25,30 @@ void convex_hull::andrew_algorithm(vertex * points, const size_t & n_points) { std::sort(points, points + n_points); - index_t t, k = 0; CH.resize(2 * n_points); + index_t k = 0; for(index_t i = 0; i < n_points; ++i) { while(k > 1 && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; CH[k++] = points[i]; } - t = k; + index_t t = k; for(index_t i = n_points - 2; i > 0; --i) { - while(k >= t && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; + while(k > t && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; CH[k++] = points[i]; } - //evaluate condition with the first point - while(k >= t && !ccw(CH[k - 2], CH[k - 1], points[0])) --k; + while(k > t && !ccw(CH[k - 2], CH[k - 1], points[0])) --k; CH.resize(k); } bool convex_hull::ccw(const vertex & p, const vertex & q, const vertex & r) { - return ((q - p) * (r - q)).z > 0; + return ((q - p) * (r - p)).z > 0; } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f465e738..5abbbd62 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -428,7 +428,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co - if( abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) + if(abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) { a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); From e147b9b960dcb7b98bb60286609935346ab94f0a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 6 Mar 2021 22:55:04 +0100 Subject: [PATCH 0456/1018] update for increment part i++ to ++i --- gproshan.cpp | 15 --- include/raytracing/rt_embree_splat.h | 2 +- src/app_viewer.cpp | 36 +++---- src/features/descriptor.cpp | 6 +- src/features/key_components.cpp | 6 +- src/features/key_points.cpp | 8 +- src/geodesics/dijkstra.cpp | 8 +- src/geodesics/geodesics.cpp | 6 +- src/geodesics/geodesics_ptp.cpp | 30 +++--- src/geodesics/geodesics_ptp.cu | 6 +- src/geodesics/geodesics_ptp_coalescence.cu | 10 +- src/geodesics/heat_method.cpp | 6 +- src/geodesics/sampling.cpp | 2 +- src/geodesics/test_geodesics_ptp.cpp | 26 ++--- src/geodesics/test_geodesics_ptp.cu | 4 +- .../test_geodesics_ptp_coalescence.cu | 12 +-- src/laplacian/fairing_spectral.cpp | 2 +- src/laplacian/fairing_taubin.cpp | 2 +- src/laplacian/laplacian.cpp | 8 +- src/mdict/basis.cpp | 4 +- src/mdict/basis_dct.cpp | 16 +-- src/mdict/image_denoising.cpp | 28 +++--- src/mdict/mdict.cpp | 36 +++---- src/mdict/msparse_coding.cpp | 98 +++++++++---------- src/mdict/patch.cpp | 34 +++---- src/mesh/che.cpp | 94 +++++++++--------- src/mesh/che_fill_hole.cpp | 36 +++---- src/mesh/che_img.cpp | 4 +- src/mesh/che_obj.cpp | 6 +- src/mesh/che_off.cpp | 12 +-- src/mesh/che_ply.cpp | 12 +-- src/mesh/che_poisson.cpp | 20 ++-- src/mesh/che_ptx.cpp | 16 +-- src/mesh/che_sphere.cpp | 6 +- src/mesh/simplification.cpp | 8 +- src/raytracing/raytracing.cpp | 14 +-- src/raytracing/rt_embree.cpp | 2 +- src/raytracing/rt_embree_splat.cpp | 6 +- src/raytracing/rt_optix.cpp | 4 +- src/util.cpp | 4 +- src/viewer/che_viewer.cpp | 6 +- src/viewer/viewer.cpp | 16 +-- 42 files changed, 331 insertions(+), 346 deletions(-) diff --git a/gproshan.cpp b/gproshan.cpp index d95ed598..5a3d8a52 100644 --- a/gproshan.cpp +++ b/gproshan.cpp @@ -1,24 +1,9 @@ #include "app_viewer.h" -#include "geometry/convex_hull.h" using namespace std; int main(int nargs, const char ** args) { - std::vector vv{ {0, 2, 0}, - {1, 0, 0}, - {2, 5, 0}, - {3, 3, 0}, - {4, -1, 0}, - {2, 2, 0}, - {5, 3, 0}, - {6, 4, 0} }; - - gproshan::convex_hull ch(vv); - const std::vector & p = ch; - for(auto v: p) - gproshan_log_var(v); - gproshan::app_viewer app; return app.main(nargs, args); } diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index 16d1e4ce..c803f9b4 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -44,7 +44,7 @@ class embree_splat : public embree float w, sum_w = 0, sigma = radio * pc_radius; - for(index_t i = 0; i < K; i++) + for(index_t i = 0; i < K; ++i) { w = glm::length(p - P[i]); w = exp(-0.5 * w * w / (sigma * sigma)); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c27688c1..f58d0d3a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -42,7 +42,7 @@ int app_viewer::main(int nargs, const char ** args) TIC(time) - for(int i = 1; i < nargs; i++) + for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); TOC(time) @@ -120,7 +120,7 @@ bool paint_holes_vertices(viewer * p_view) mesh.update(); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) if(v >= nv) mesh->heatmap(v) = .25; return false; @@ -214,7 +214,7 @@ bool app_viewer::process_noise(viewer * p_view) std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); @@ -236,7 +236,7 @@ bool app_viewer::process_black_noise(viewer * p_view) std::uniform_int_distribution d_mod_1000(0, 999); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t r = real_t(d_mod_1000(generator)) / 200000; mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); @@ -253,7 +253,7 @@ bool app_viewer::process_threshold(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; return false; @@ -280,7 +280,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) gproshan_log_var(K); K = K < N_MESHES ? K : N_MESHES; - for(index_t k = 0; k < N_MESHES; k++) + for(index_t k = 0; k < N_MESHES; ++k) { if(k) view->add_mesh(new che(*mesh)); view->idx_active_mesh = k; @@ -289,7 +289,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) eigvec.col(k) /= eigvec.col(k).max(); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) view->active_mesh()->heatmap(v) = eigvec(v, k); view->active_mesh().update_vbo(); @@ -321,14 +321,14 @@ bool app_viewer::descriptor_heatmap(viewer * p_view, const descriptor::signature real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { mesh->heatmap(v) = features(v); max_s = max(max_s, mesh->heatmap(v)); } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) /= max_s; } else status = false; @@ -363,7 +363,7 @@ bool app_viewer::process_key_points(viewer * p_view) mesh.selected.clear(); mesh.selected.reserve(kps.size()); - for(index_t i = 0; i < kps.size(); i++) + for(index_t i = 0; i < kps.size(); ++i) mesh.selected.push_back(kps[i]); return false; @@ -381,7 +381,7 @@ bool app_viewer::process_key_components(viewer * p_view) gproshan_debug_var(kcs); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = (real_t) kcs(v) / kcs; return false; @@ -505,7 +505,7 @@ bool app_viewer::process_mask(viewer * p_view) points_out.load(f_points); gproshan_debug_var(points_out.size()); - for(index_t i = 0; i < points_out.size(); i++) + for(index_t i = 0; i < points_out.size(); ++i) mesh.selected.push_back(points_out(i)); mesh->update_normals(); @@ -578,7 +578,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) size_t n_toplesets = limites.size() - 1; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { if(toplesets[v] < n_toplesets) mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); @@ -612,7 +612,7 @@ bool app_viewer::process_voronoi(viewer * p_view) gproshan_log_var(view->time); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { mesh->heatmap(i) = ptp.clusters[i]; mesh->heatmap(i) /= mesh.selected.size() + 1; @@ -793,7 +793,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) index_t k = 2; - for(index_t h = 0; h < n_holes; h++) + for(index_t h = 0; h < n_holes; ++h) if(holes[h]) { old_n_vertices = n_vertices; @@ -820,7 +820,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) a_vec gv(mesh->n_vertices); #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { g = 0; for_star(he, mesh, v) @@ -842,7 +842,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) gproshan_log_var(g_max); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) gv(v) = (gv(v) + g_min) / g; real_t gm = mean(gv); @@ -859,7 +859,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) }; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = f(gv(v)); return false; diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index ba5f3aa5..7bdc751a 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -32,7 +32,7 @@ real_t descriptor::operator () (const index_t & v) const void descriptor::compute_gps() { features = eigvec.tail_cols(eigvec.n_cols - 1); - for(index_t i = 1; i < eigval.size(); i++) + for(index_t i = 1; i < eigval.size(); ++i) features.col(i - 1) /= sqrt(eigval(i)); } @@ -43,7 +43,7 @@ void descriptor::compute_hks() features.zeros(eigvec.n_rows, eigvec.n_cols); #pragma omp parallel for - for(index_t t = 0; t < features.n_cols; t++) + for(index_t t = 0; t < features.n_cols; ++t) features.col(t) = eigvec * exp(-eigval * t); } @@ -60,7 +60,7 @@ void descriptor::compute_wks() features.zeros(eigvec.n_rows, e.n_elem); #pragma omp parallel for - for(index_t t = 0; t < features.n_cols; t++) + for(index_t t = 0; t < features.n_cols; ++t) features.col(t) = eigvec * exp(-pow(e(t) - eigval, 2) / sigma_2) / sum(exp(-pow(e(t) - eigval, 2) / sigma_2)); } diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index e64327c4..e54758b8 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -19,7 +19,7 @@ key_components::key_components(che * mesh, const key_points & kps, const real_t comp_size = new size_t[n_vertices]; #pragma omp parallel for - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) { comp[i] = i; comp_size[i] = 1; @@ -53,10 +53,10 @@ void key_components::compute_kcs(che * mesh, const key_points & kps) geodesics fm(mesh, vector(&kps[0], &kps[0] + kps.size())); radio *= fm.radio(); - for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; i++) + for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) for_star(he, mesh, fm(i)) join(fm(i), mesh->vt(next(he))); - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) if(comp[i] == i && comp_size[i] > 1) comp_idx[i] = n_comp++; else if(comp[i] == i) comp[i] = NIL; diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 5312260e..8071ced9 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -54,7 +54,7 @@ void key_points::compute_kps(che * mesh) // compute faces areas #pragma omp parallel for - for(index_t t = 0; t < n_faces; t++) + for(index_t t = 0; t < n_faces; ++t) { face_areas[t].first = mesh->area_trig(t); face_areas[t].second = t; @@ -66,10 +66,10 @@ void key_points::compute_kps(che * mesh) memset(is_kp, 0, sizeof(bool) * n_vertices); index_t he, k = 0; - for(index_t t = 0; t < n_faces; t++) + for(index_t t = 0; t < n_faces; ++t) { he = che::mtrig * face_areas[t].second; - for(index_t i = 0; i < che::mtrig; i++) + for(index_t i = 0; i < che::mtrig; ++i) { const index_t & v = mesh->vt(he); if(!is_kp[v]) @@ -85,7 +85,7 @@ void key_points::compute_kps(che * mesh) memset(is_kp, 0, sizeof(bool) * n_vertices); #pragma omp parallel for - for(index_t i = 0; i < n_kps; i++) + for(index_t i = 0; i < n_kps; ++i) is_kp[kps[i]] = 1; } diff --git a/src/geodesics/dijkstra.cpp b/src/geodesics/dijkstra.cpp index 57ad57e9..9f1a516a 100644 --- a/src/geodesics/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -20,7 +20,7 @@ dijkstra::dijkstra(che * mesh, index_t src) memset(predecessors, 255, sizeof(real_t)*n_vertices); - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) weights[i] = INFINITY; run(mesh); @@ -44,7 +44,7 @@ index_t & dijkstra::operator[](index_t i) void dijkstra::print(ostream & os) { - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) os< & sources, const params & n_sorted = 0; memset(sorted_index, -1, n_vertices * sizeof(index_t)); - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) dist[v] = INFINITY; assert(sources.size() > 0); @@ -88,7 +88,7 @@ void geodesics::normalize() real_t max = dist[farthest()]; #pragma omp parallel for - for(size_t i = 0; i < n_sorted; i++) + for(size_t i = 0; i < n_sorted; ++i) dist[sorted_index[i]] /= max; } @@ -118,7 +118,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co index_t * color = new index_t[n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) color[v] = GREEN; size_t green_count = n_iter ? n_iter : n_vertices; diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index df451965..a2ea28fb 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -25,13 +25,13 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top memset(inv, -1, sizeof(index_t) * mesh->n_vertices); #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); i++) + for(index_t i = 0; i < toplesets.limits.back(); ++i) { V[i] = mesh->gt(toplesets.index[i]); inv[toplesets.index[i]] = i; } - for(index_t he = 0; he < mesh->n_half_edges; he++) + for(index_t he = 0; he < mesh->n_half_edges; ++he) if(inv[mesh->vt(he)] != NIL && inv[mesh->vt(prev(he))] != NIL && inv[mesh->vt(next(he))] != NIL) F.push_back(inv[mesh->vt(he)]); @@ -50,10 +50,10 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c real_t * error = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) pdist[0][v] = pdist[1][v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) { pdist[0][inv[sources[i]]] = pdist[1][inv[sources[i]]] = 0; if(ptp_out.clusters) ptp_out.clusters[inv[sources[i]]] = i + 1; @@ -76,7 +76,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c n_cond = toplesets.limits[i + 1] - start; #pragma omp parallel for - for(index_t v = start; v < end; v++) + for(index_t v = start; v < end; ++v) { pdist[!d][v] = pdist[d][v]; @@ -95,12 +95,12 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c } #pragma omp parallel for - for(index_t v = start; v < start + n_cond; v++) + for(index_t v = start; v < start + n_cond; ++v) error[v] = abs(pdist[!d][v] - pdist[d][v]) / pdist[d][v]; count = 0; #pragma omp parallel for reduction(+: count) - for(index_t v = start; v < start + n_cond; v++) + for(index_t v = start; v < start + n_cond; ++v) count += error[v] < PTP_TOL; if(n_cond == count) i++; @@ -110,7 +110,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c } #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) ptp_out.dist[v] = inv[v] != NIL ? pdist[!d][inv[v]] : INFINITY; delete [] error; @@ -126,10 +126,10 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t * error = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) pdist[0][v] = pdist[1][v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) { pdist[0][sources[i]] = pdist[1][sources[i]] = 0; if(ptp_out.clusters) ptp_out.clusters[sources[i]] = i + 1; @@ -152,7 +152,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c n_cond = toplesets.limits[i + 1] - start; #pragma omp parallel for - for(index_t vi = start; vi < end; vi++) + for(index_t vi = start; vi < end; ++vi) { const index_t & v = toplesets.index[vi]; pdist[!d][v] = pdist[d][v]; @@ -172,7 +172,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c } #pragma omp parallel for - for(index_t vi = start; vi < start + n_cond; vi++) + for(index_t vi = start; vi < start + n_cond; ++vi) { const index_t & v = toplesets.index[vi]; error[vi] = abs(pdist[!d][v] - pdist[d][v]) / pdist[d][v]; @@ -180,7 +180,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c count = 0; #pragma omp parallel for reduction(+: count) - for(index_t vi = start; vi < start + n_cond; vi++) + for(index_t vi = start; vi < start + n_cond; ++vi) count += error[vi] < PTP_TOL; if(n_cond == count) i++; @@ -267,12 +267,12 @@ void normalize_ptp(real_t * dist, const size_t & n) real_t max_d = 0; #pragma omp parallel for reduction(max: max_d) - for(index_t v = 0; v < n; v++) + for(index_t v = 0; v < n; ++v) if(dist[v] < INFINITY) max_d = max(dist[v], max_d); #pragma omp parallel for - for(index_t v = 0; v < n; v++) + for(index_t v = 0; v < n; ++v) dist[v] /= max_d; } diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index 3a320d46..9e759eab 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -174,10 +174,10 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_dist[sources[i]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); @@ -188,7 +188,7 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r { assert(d_clusters); - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_clusters[sources[i]] = i + 1; cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index 19c97ad6..397d239b 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -61,7 +61,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); #pragma omp parallel for - for(index_t i = 0; i < h_mesh->n_vertices; i++) + for(index_t i = 0; i < h_mesh->n_vertices; ++i) ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; cudaFree(d_clusters[0]); @@ -82,7 +82,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, delete [] inv; #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); i++) + for(index_t i = 0; i < toplesets.limits.back(); ++i) ptp_out.dist[toplesets.index[i]] = h_dist[i]; delete [] h_dist; @@ -102,10 +102,10 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_dist[inv.index[sources[i]]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); @@ -115,7 +115,7 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t { assert(d_clusters[0]); - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_clusters[inv.index[sources[i]]] = i + 1; cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 370ec0b9..14364800 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -87,7 +87,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) { - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t & sum = div(v); @@ -161,11 +161,11 @@ double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & int * hA_row_indices = new int[A.n_nonzero]; #pragma omp parallel for - for(index_t i = 0; i <= A.n_cols; i++) + for(index_t i = 0; i <= A.n_cols; ++i) hA_col_ptrs[i] = A.col_ptrs[i]; #pragma omp parallel for - for(index_t i = 0; i < A.n_nonzero; i++) + for(index_t i = 0; i < A.n_nonzero; ++i) hA_row_indices[i] = A.row_indices[i]; double solve_time = solve_positive_definite_cusolver(A.n_rows, A.n_nonzero, A.values, hA_col_ptrs, hA_row_indices, b.memptr(), x.memptr()); diff --git a/src/geodesics/sampling.cpp b/src/geodesics/sampling.cpp index 4205cf64..773403bc 100644 --- a/src/geodesics/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -22,7 +22,7 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n params.radio = radio; #pragma omp parallel for - for(index_t i = 0; i < n_points; i++) + for(index_t i = 0; i < n_points; ++i) { const index_t & v = points[i]; normals[i] = mesh->normal(v); diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 5e62cade..1288ff6d 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -94,11 +94,11 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #endif index_t t_min = 0; - for(index_t i = 1; i < sizeof(Time) / sizeof(double); i++) + for(index_t i = 1; i < sizeof(Time) / sizeof(double); ++i) if(Time[t_min] > Time[i]) t_min = i; index_t e_min = 0; - for(index_t i = 1; i < sizeof(Error) / sizeof(real_t); i++) + for(index_t i = 1; i < sizeof(Error) / sizeof(real_t); ++i) if(Error[e_min] > Error[i]) e_min = i; fprintf(ftable, "%20s ", ("\\verb|" + filename + '|').c_str()); @@ -157,7 +157,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) index_t dv; map deg; - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->ot_evt(v) == NIL ? 1 : 0; for_star(he, mesh, v) dv++; @@ -175,7 +175,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) index_t * toplesets_dist = new index_t[limits.size() - 1]; os.open(test_path + filename + "_toplesets.dist"); - for(index_t i = 1; i < limits.size(); i++) + for(index_t i = 1; i < limits.size(); ++i) { toplesets_dist[i - 1] = limits[i] - limits[i - 1]; os << i - 1 << " " << toplesets_dist[i - 1] << endl; @@ -185,7 +185,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) sort(toplesets_dist, toplesets_dist + limits.size() - 1); os.open(test_path + filename + "_toplesets_sorted.dist"); - for(index_t i = 0; i < limits.size() - 1; i++) + for(index_t i = 0; i < limits.size() - 1; ++i) os << i << " " << toplesets_dist[i] << endl; os.close(); @@ -220,7 +220,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) double * times_fps = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); os.open(test_path + filename + ".fps"); - for(index_t i = i_samples; i < n_samples; i++) + for(index_t i = i_samples; i < n_samples; ++i) os << i << " " << times_fps[i] << endl; os.close(); @@ -243,7 +243,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons { double t, seconds = INFINITY; - for(int i = 0; i < n_test; i++) + for(int i = 0; i < n_test; ++i) { TIC(t) geodesics fm(mesh, source); TOC(t); seconds = min(seconds, t); @@ -261,7 +261,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vect double t, seconds = INFINITY; real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; i++) + for(int i = 0; i < n_test; ++i) { TIC(t) parallel_toplesets_propagation_cpu(dist, mesh, source, toplesets); TOC(t) seconds = min(seconds, t); @@ -280,7 +280,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e ptime = stime = INFINITY; real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; i++) + for(int i = 0; i < n_test; ++i) { TIC(t) st = heat_method(dist, mesh, source, HEAT_CHOLMOD); TOC(t) ptime = min(t - st, ptime); @@ -302,7 +302,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vect double t, seconds = INFINITY; real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; i++) + for(int i = 0; i < n_test; ++i) { t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); seconds = min(seconds, t); @@ -321,7 +321,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact ptime = stime = INFINITY; real_t * dist = nullptr; - for(int i = 0; i < n_test; i++) + for(int i = 0; i < n_test; ++i) { if(dist) delete [] dist; @@ -348,7 +348,7 @@ real_t * load_exact_geodesics(const string & file, const size_t & n) real_t * exact = new real_t[n]; - for(index_t i = 0; i < n; i++) + for(index_t i = 0; i < n; ++i) is >> exact[i]; is.close(); @@ -360,7 +360,7 @@ real_t compute_error(const real_t * dist, const real_t * exact, const size_t & n real_t error = 0; #pragma omp parallel for reduction(+: error) - for(index_t v = 0; v < n; v++) + for(index_t v = 0; v < n; ++v) if(exact[v] > 0) error += abs(dist[v] - exact[v]) / exact[v]; return error * 100 / (n - s); diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index 2dcb29af..ab5400f2 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -164,10 +164,10 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_dist[sources[i]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 1d4c8357..428e71a3 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -30,7 +30,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc real_t * exact_dist_sorted = new real_t[mesh->n_vertices]; #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { V[i] = mesh->gt(sorted_index[i]); inv[sorted_index[i]] = i; @@ -38,7 +38,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc } #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; he++) + for(index_t he = 0; he < mesh->n_half_edges; ++he) F[he] = inv[mesh->vt(he)]; mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); @@ -148,14 +148,14 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { V[i] = mesh->gt(sorted_index[i]); inv[sorted_index[i]] = i; } #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; he++) + for(index_t he = 0; he < mesh->n_half_edges; ++he) F[he] = inv[mesh->vt(he)]; che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); @@ -214,10 +214,10 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); i++) + for(index_t i = 0; i < sources.size(); ++i) h_dist[inv[sources[i]]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index ec96969a..c1287a94 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -25,7 +25,7 @@ void fairing_spectral::compute(che * mesh) a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) positions[v] = mesh->gt(v); a_sp_mat L, A; diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index 62eb096b..f9fedacb 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -38,7 +38,7 @@ void fairing_taubin::compute(che * mesh) a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) positions[v] = mesh->gt(v); a_mat R; diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 2ba0d508..e0d760aa 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -20,7 +20,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) a_vec SV(n_edges); #pragma omp parallel for - for(index_t e = 0; e < n_edges; e++) + for(index_t e = 0; e < n_edges; ++e) { index_t i = e << 1; @@ -47,7 +47,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) A.eye(n_vertices, n_vertices); #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) A(v, v) = mesh->area_vertex(v); } @@ -64,7 +64,7 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) D.reserve(VectorXi::Constant(n_edges,2)); S.reserve(VectorXi::Constant(n_edges,1)); - for(index_t e = 0; e < n_edges; e++) + for(index_t e = 0; e < n_edges; ++e) { D.insert(e, mesh->vt(mesh->et(e))) = 1; D.insert(e, mesh->vt(next(mesh->et(e)))) = -1; @@ -76,7 +76,7 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) L = D.transpose() * S * D; A.reserve(VectorXi::Constant(n_vertices, 1)); - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) A.insert(v, v) = mesh->area_vertex(v); } diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 5d8fd667..773ff5c4 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -64,7 +64,7 @@ void basis::plot_atoms(const a_mat & A) os << "set pm3d at b;" << endl; os << "unset colorbox;" << endl; - for(index_t i = 0; i < m; i++) + for(index_t i = 0; i < m; ++i) { os << "splot v * cos(u), v * sin(u), 0 "; plot_atoms(os, A.col(i)); @@ -106,7 +106,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) os << "unset colorbox;" << endl; os << "splot \"xyz_" << to_string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; - for(index_t i = 0; i < m; i++) + for(index_t i = 0; i < m; ++i) { os << " v * cos(u), v * sin(u), 0 "; plot_atoms(os, A.col(i)); diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index 9747b544..b787d1c5 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -14,8 +14,8 @@ void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { assert(phi.n_cols == _dim); - for(index_t k = 0, nx = 0; nx < n_freq; nx++) - for(index_t ny = 0; ny < n_freq; ny++, k++) + for(index_t k = 0, nx = 0; nx < n_freq; ++nx) + for(index_t ny = 0; ny < n_freq; ++ny, ++k) phi.col(k) = dct(x, y, nx, ny); } @@ -23,8 +23,8 @@ void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const { assert(phi.n_cols == _dim); - for(index_t k = 0, nx = 0; nx < n_freq; nx++) - for(index_t ny = 0; ny < n_freq; ny++, k++) + for(index_t k = 0, nx = 0; nx < n_freq; ++nx) + for(index_t ny = 0; ny < n_freq; ++ny, ++k) phi.col(k) = !b ? dct(x, y, nx, ny) : dct(y, x, ny, nx); } @@ -32,8 +32,8 @@ void basis_dct::plot_basis(ostream & os) { os << "set multiplot layout " << n_freq << "," << n_freq << " rowsfirst scale 1.2;" << endl; - for(index_t nx = 0; nx < n_freq; nx++) - for(index_t ny = 0; ny < n_freq; ny++) + for(index_t nx = 0; nx < n_freq; ++nx) + for(index_t ny = 0; ny < n_freq; ++ny) { os << "splot v * cos(u), v * sin(u), "; dct(os, nx, ny); os << ";" << endl; } @@ -41,8 +41,8 @@ void basis_dct::plot_basis(ostream & os) void basis_dct::plot_atoms(ostream & os, const a_vec & A) { - for(index_t k = 0, nx = 0; nx < n_freq; nx++) - for(index_t ny = 0; ny < n_freq; ny++, k++) + for(index_t k = 0, nx = 0; nx < n_freq; ++nx) + for(index_t ny = 0; ny < n_freq; ++ny, ++k) { os << " + " << A(k) << " * "; dct(os, nx, ny); } diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 10a3006a..c482e38c 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -27,14 +27,14 @@ void test_image_denoising(const string & file) a_mat X(n, M); - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) + for(index_t x = 0; x < rows; ++x) + for(index_t y = 0; y < cols; ++y) { index_t i = x + y * rows; index_t k = 0; - for(index_t b = y; b < y + p; b++) - for(index_t a = x; a < x + p; a++) + for(index_t b = y; b < y + p; ++b) + for(index_t a = x; a < x + p; ++a) { X(k, i) = image(a, b); k++; @@ -47,10 +47,10 @@ void test_image_denoising(const string & file) a_mat spD = D; CImg imdict; - for(index_t i = 0; i < 16; i++) + for(index_t i = 0; i < 16; ++i) { CImg imrow; - for(index_t j = 0; j < 16; j++) + for(index_t j = 0; j < 16; ++j) imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); imdict.append(imrow, 'y'); @@ -76,10 +76,10 @@ void test_image_denoising(const string & file) gproshan_log_var(norm(D - spD)); CImg imdictlearned; - for(index_t i = 0; i < 16; i++) + for(index_t i = 0; i < 16; ++i) { CImg imrow; - for(index_t j = 0; j < 16; j++) + for(index_t j = 0; j < 16; ++j) imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); imdictlearned.append(imrow, 'y'); @@ -107,14 +107,14 @@ void test_image_denoising(const string & file) CImg image_out = image; image_out.fill(0); - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) + for(index_t x = 0; x < rows; ++x) + for(index_t y = 0; y < cols; ++y) { index_t i = x + y * rows; index_t k = 0; - for(index_t b = y; b < y + p; b++) - for(index_t a = x; a < x + p; a++) + for(index_t b = y; b < y + p; ++b) + for(index_t a = x; a < x + p; ++a) { image_out(a, b) += Y(k, i); k++; @@ -123,8 +123,8 @@ void test_image_denoising(const string & file) rows = image.width(); cols = image.height(); - for(index_t x = 0; x < rows; x++) - for(index_t y = 0; y < cols; y++) + for(index_t x = 0; x < rows; ++x) + for(index_t y = 0; y < cols; ++y) { index_t dx = p, dy = p; if(x < p && x < dx) dx = x + 1; diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index cd03b28c..1dd98d28 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -34,7 +34,7 @@ void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_m tie(aa, selected_atoms) = _OMP(x, D, L); - for(index_t k = 0; k < selected_atoms.size(); k++) + for(index_t k = 0; k < selected_atoms.size(); ++k) { #pragma omp critical alpha.push_back({selected_atoms(k), i, aa(k)}); @@ -46,14 +46,14 @@ a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, co locval.clear(); #pragma omp parallel for - for(index_t i = 0; i < X.n_cols; i++) + for(index_t i = 0; i < X.n_cols; ++i) OMP(locval, X.col(i), i, D, L); arma::umat DI(2, locval.size()); a_vec DV(locval.size()); #pragma omp parallel for - for(index_t k = 0; k < locval.size(); k++) + for(index_t k = 0; k < locval.size(); ++k) { DI(0, k) = locval[k].i; // row DI(1, k) = locval[k].j; // column @@ -83,7 +83,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) sort(locval.begin(), locval.end()); rows.push_back(0); - for(index_t k = 1; k < locval.size(); k++) + for(index_t k = 1; k < locval.size(); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); @@ -92,9 +92,9 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) R = X - D * alpha; #pragma omp parallel for firstprivate(omega, E, U, V, s) - for(index_t j = 0; j < m; j++) + for(index_t j = 0; j < m; ++j) { - for(index_t r = rows[j]; r < rows[j + 1]; r++) + for(index_t r = rows[j]; r < rows[j + 1]; ++r) omega(r - rows[j]) = locval[r].j; if(rows[j + 1] - rows[j]) @@ -138,7 +138,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) { arma::uvec indices = arma::sort_index(V, "descend"); - for(size_t i=0; i< V.size(); i++) + for(size_t i=0; i< V.size(); ++i) if(mask[indices[i]]) return indices[i]; return NIL; @@ -195,7 +195,7 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { a_mat alpha(D.n_cols, X.n_cols); #pragma omp parallel for - for(index_t i = 0; i < X.n_cols; i++) + for(index_t i = 0; i < X.n_cols; ++i) { alpha.col(i) = OMP(X.col(i), D, L); } @@ -216,7 +216,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) R = X - D * alpha; #pragma omp parallel for private(omega, E, U, V, s) - for(index_t j = 0; j < D.n_cols; j++) + for(index_t j = 0; j < D.n_cols; ++j) { omega = find(abs(alpha.row(j)) > 0); if(omega.n_elem) @@ -242,7 +242,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) { arma::uchar_vec mask(A.n_cols); - for(index_t i = 0; i < A.n_cols; i++) + for(index_t i = 0; i < A.n_cols; ++i) mask(i) = phi_basis->freq(i) >= patch::nyquist_factor * p.avg_dist; // 2.5* if it ismin return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); @@ -253,7 +253,7 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, a_mat alpha(A.n_cols, patches.size()); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); i++) + for(index_t i = 0; i < patches.size(); ++i) alpha.col(i) = OMP(patches[i],phi_basis, A, L); return alpha; @@ -264,7 +264,7 @@ a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) a_mat alpha(A.n_cols, patches.size()); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); i++) + for(index_t i = 0; i < patches.size(); ++i) alpha.col(i) = OMP(patches[i], A, L); return alpha; @@ -287,7 +287,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) alpha = OMP_all(patches, A, L); #pragma omp parallel for private(omega, a, aj, D, e, sum, sum_error) - for(index_t j = 0; j < A.n_cols; j++) + for(index_t j = 0; j < A.n_cols; ++j) { //Taking all alphas that uses atom j arma::uvec omega = find(abs(alpha.row(j)) > 0); @@ -332,14 +332,14 @@ a_sp_mat OMP_all(vector & locval, const vector & patches, const locval.clear(); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); i++) + for(index_t i = 0; i < patches.size(); ++i) OMP(locval, patches[i], i, A, L); arma::umat DI(2, locval.size()); a_vec DV(locval.size()); #pragma omp parallel for - for(index_t k = 0; k < locval.size(); k++) + for(index_t k = 0; k < locval.size(); ++k) { DI(0, k) = locval[k].i; // row DI(1, k) = locval[k].j; // column @@ -370,19 +370,19 @@ void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t sort(locval.begin(), locval.end()); rows.push_back(0); - for(index_t k = 1; k < locval.size(); k++) + for(index_t k = 1; k < locval.size(); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); rows.push_back(locval.size()); #pragma omp parallel for private(a, aj, D, e, sum, sum_error) - for(index_t j = 0; j < A.n_cols; j++) + for(index_t j = 0; j < A.n_cols; ++j) { sum.zeros(K, K); sum_error.zeros(K); - for(index_t r = rows[j]; r < rows[j + 1]; r++) + for(index_t r = rows[j]; r < rows[j + 1]; ++r) { const index_t & i = locval[r].j; diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index ab406d68..01f76078 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -78,7 +78,7 @@ void msparse_coding::load_mask() if(V.load(f_mask)) { #pragma omp for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { mask[i] = V(i); } @@ -114,7 +114,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde if(V.load(f_mask)) { #pragma omp for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { mask[i] = V(i); } @@ -130,7 +130,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde // create initial desired percentage sizes #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; cover_cluster[s] = 0; @@ -190,7 +190,7 @@ void msparse_coding::load_sampling(bool save_all) size_t n_seeds = S.n_rows; real_t euc_radio, geo_radio; - for(index_t i = 0; i < n_seeds; i++) + for(index_t i = 0; i < n_seeds; ++i) { patch p; p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); @@ -213,7 +213,7 @@ void msparse_coding::load_sampling(bool save_all) bool covered[mesh->n_vertices]; #pragma omp for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) covered[i] = 0; real_t euc_radio; @@ -273,7 +273,7 @@ void msparse_coding::load_sampling(bool save_all) S.resize(seeds.size()); #pragma omp parallel for - for(index_t i = 0; i < seeds.size(); i++) + for(index_t i = 0; i < seeds.size(); ++i) S(i) = seeds[i]; S.save(tmp_file_path(key_name + ".rsampl")); @@ -289,12 +289,12 @@ void msparse_coding::load_sampling(bool save_all) vector outliers; - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) if(!covered[i]) outliers.push_back(i); a_vec outlv(outliers.size()); - for(index_t i = 0; i < outliers.size(); i++) + for(index_t i = 0; i < outliers.size(); ++i) outlv(i) = outliers[i]; outlv.save(tmp_file_path(key_name + ".outlv")); @@ -312,13 +312,13 @@ void msparse_coding::init_radial_feature_patches() size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_max_size = max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; @@ -337,11 +337,11 @@ void msparse_coding::init_radial_feature_patches() bool * pmask = mask; - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; @@ -358,7 +358,7 @@ void msparse_coding::init_radial_feature_patches() { a_mat AS; AS.resize(m_params.n_patches, 13); - for(index_t i = 0; i < m_params.n_patches; i++) + for(index_t i = 0; i < m_params.n_patches; ++i) { patch & p = patches[i]; @@ -412,12 +412,12 @@ void msparse_coding::init_voronoi_patches() //saving first vertex aka seed vertices #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { vertices[s].push_back(sample(s)); } - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { ptp.clusters[i]--; if(sample(ptp.clusters[i]) != i) @@ -438,7 +438,7 @@ void msparse_coding::init_voronoi_patches() index_t * toplevel = new index_t[mesh->n_vertices]; #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); @@ -449,13 +449,13 @@ void msparse_coding::init_voronoi_patches() size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_max_size = max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; @@ -466,11 +466,11 @@ void msparse_coding::init_voronoi_patches() } bool * pmask = mask; - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; @@ -508,14 +508,14 @@ real_t msparse_coding::execute() draw_patches(312); #pragma omp parallel for - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) patches_map[i].clear(); - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz(mesh, patches_map, s, 0); #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; @@ -556,7 +556,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) real_t max_radio = -1; #pragma omp parallel for reduction(max: max_radio) - for(index_t i = 0; i < m_params.n_patches; i++) + for(index_t i = 0; i < m_params.n_patches; ++i) max_radio = max(max_radio, S(i, 3)); A.eye(phi_basis->dim(), m_params.n_atoms); @@ -564,7 +564,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) size_t total_points = 0; #pragma omp parallel for - for(index_t i = 0; i < m_params.n_patches; i++) + for(index_t i = 0; i < m_params.n_patches; ++i) { a_mat T(3,3); T(0,0) = S(i,4); @@ -592,7 +592,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) #pragma omp atomic total_points += p.xyz.n_cols; - for(index_t j = 0; j < patches[i].xyz.n_cols; j++) + for(index_t j = 0; j < patches[i].xyz.n_cols; ++j) if(patches[i].xyz(2, j) > 2) { gproshan_debug_var(i); @@ -605,8 +605,8 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) vector point_cloud; point_cloud.reserve(total_points); - for(index_t i = 0; i < m_params.n_patches; i++) - for(index_t j = 0; j < patches[i].xyz.n_cols; j++) + for(index_t i = 0; i < m_params.n_patches; ++i) + for(index_t j = 0; j < patches[i].xyz.n_cols; ++j) point_cloud.push_back({ patches[i].xyz(0, j), patches[i].xyz(1, j), patches[i].xyz(2, j) @@ -616,7 +616,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) new_mesh->update_normals(); a_vec n; - for(index_t v = 0, i = 0; i < m_params.n_patches; i++) + for(index_t v = 0, i = 0; i < m_params.n_patches; ++i) { patch & p = patches[i]; @@ -626,7 +626,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); a_vec dy = p.phi * A * alpha.col(i); - for(index_t j = 0; j < patches[i].xyz.n_cols; j++, v++) + for(index_t j = 0; j < patches[i].xyz.n_cols; ++j, ++v) { n = {-dx(j), -dy(j), 1}; n = normalise(p.T * n); @@ -789,14 +789,14 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) gproshan_debug(exists); inp >> featsize; - for(index_t i = 0; i < featsize; i++) + for(index_t i = 0; i < featsize; ++i) { inp>>tmp; v_feat.push_back(tmp); } inp >> tam; - for(index_t i = 0; i < tam; i++) + for(index_t i = 0; i < tam; ++i) { inp >> tmp; v_feat.push_back(tmp); @@ -819,7 +819,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) index_t * toplevel = new index_t[n_vertices]; #pragma omp for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { index_t v = sample(s); patches[s].init(mesh, v, msparse_coding::T, phi_basis->radio(), toplevel); @@ -835,13 +835,13 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) size_t patch_max_size = 0; #pragma omp parallel for reduction(+: patch_avg_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patch_max_size = max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; @@ -851,11 +851,11 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) #endif } - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz(mesh, patches_map, s, mask); #pragma omp parallel for - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; @@ -868,7 +868,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) /* #ifndef NDEBUG CImgList imlist; - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].save(phi_basis->radio(), 16, imlist); imlist.save_ffmpeg_external("tmp/patches.mpg", 5); #endif @@ -878,14 +878,14 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) /*Saving Patches*/ /* ofstream os(tmp_file_path("patch-mat")); - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; p.save_z(os); } os.close(); // DRAW NORMALS DEBUG - for(index_t s = 0; s < m_params.n_patches; s++) + for(index_t s = 0; s < m_params.n_patches; ++s) { viewer::vectors.push_back({patches[s].x(0), patches[s].x(1), patches[s].x(2)}); a_vec r = patches[s].x + 0.02 * patches[s].normal(); @@ -905,7 +905,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) patches_error.resize(m_params.n_patches); #pragma omp parallel for - for(index_t p = 0; p < m_params.n_patches; p++) + for(index_t p = 0; p < m_params.n_patches; ++p) { patch & rp = patches[p]; @@ -919,7 +919,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) sort(patches_error.begin(), patches_error.end()); fprintf(stderr, "error %16s%16s\n", "best", "worst"); - for(index_t i = 0; i < 10; i++) + for(index_t i = 0; i < 10; ++i) { const index_t & best = patches_error[i].second; const index_t & worst = patches_error[m_params.n_patches - i - 1].second; @@ -928,7 +928,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) } #pragma omp parallel for - for(index_t p = 0; p < m_params.n_patches; p++) + for(index_t p = 0; p < m_params.n_patches; ++p) { patch & rp = patches[p]; rp.iscale_xyz(phi_basis->radio()); @@ -936,7 +936,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) } #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { // simple means vertex if(patches_map[v].size() && (!mask || mask(v))) @@ -963,7 +963,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) real_t max_error = -1; #pragma omp parallel for reduction(+: error) reduction(max: max_error) - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); error += dist[v]; @@ -993,7 +993,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) while(count < threshold) { #pragma omp parallel for - for(index_t s = threshold; s < m_params.n_patches; s++) + for(index_t s = threshold; s < m_params.n_patches; ++s) { if(!patches_covered[s-threshold]) diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 5abbbd62..3675f62c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -60,7 +60,7 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev bool patch::exists(index_t idx) { - for(size_t i=1; i < vertices.size(); i++) + for(size_t i=1; i < vertices.size(); ++i) { if(vertices[i] == idx) return true; @@ -70,7 +70,7 @@ bool patch::exists(index_t idx) index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) { - for(size_t i=0; iget_vertex(indexes[i]); c = mesh->get_vertex(v); // central vertices @@ -324,12 +324,12 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & if(mask) { m = 0; - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); ++i) if(mask(vertices[i])) m++; } xyz.set_size(3, m); - for(index_t j = 0, i = 0; i < vertices.size(); i++) + for(index_t j = 0, i = 0; i < vertices.size(); ++i) { if(!mask || mask(vertices[i])) { @@ -361,7 +361,7 @@ void patch::remove_extra_xyz_disjoint(size_t & max_points) { arma::uvec xi; xi.zeros(max_points); - for (size_t i=1; i< max_points; i++) xi[i] = i; + for (size_t i=1; i< max_points; ++i) xi[i] = i; xi = arma::shuffle(xi); xyz = xyz.cols(xi); } @@ -465,7 +465,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & size_t x, y; img.fill(0); // for each x y plus 1, multiply by delta and floor, get i and j - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); ++i) { x = floor((xyz.col(i)[0] + radio) * (imsize - 1) / (2 * radio)); y = floor((xyz.col(i)[1] + radio) * (imsize - 1) / (2 * radio)); @@ -542,7 +542,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl toplevel[v] = 0; vertices.push_back(v); - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); ++i) { const index_t & v = vertices[i]; if(toplevel[v] == n_toplevels) @@ -700,14 +700,14 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) real_t tmp; if(flag) { - for(index_t i = 0; i < xyz.n_cols; i++) + for(index_t i = 0; i < xyz.n_cols; ++i) { xyz(2, i) = (xyz(2, i) - min) / (max - min); } } else { - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); ++i) { tmp = xyz.col(i)[2]; tmp = (max - min) * tmp + min; @@ -720,7 +720,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) void patch::save_z(ostream & os) { index_t i; - for( i = 0; i < vertices.size()-1; i++) + for( i = 0; i < vertices.size()-1; ++i) { os< & vpatches, cons vector distances; link_t link; - for(size_t i = 0; i < vertices.size(); i++) + for(size_t i = 0; i < vertices.size(); ++i) { const index_t & v = vertices[i]; mesh->link(link, v); @@ -757,7 +757,7 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons } /* - for(size_t j = i+1; j < vertices.size(); j++) // replace for 1 ring + for(size_t j = i+1; j < vertices.size(); ++j) // replace for 1 ring { a_vec a = xyz.col(i); a_vec b = xyz.col(j); @@ -781,7 +781,7 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons bool patch::is_covered( bool * covered) { - for(index_t i = 0; i < vertices.size(); i++) + for(index_t i = 0; i < vertices.size(); ++i) if(!covered[i]) return false; return true; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 7a212a52..bcc157de 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -218,7 +218,7 @@ real_t che::quality() real_t q = 0; #pragma omp parallel for reduction(+: q) - for(index_t t = 0; t < n_faces; t++) + for(index_t t = 0; t < n_faces; ++t) q += pdetriq(t) > 0.6; // is confederating good triangle return q * 100 / n_faces; @@ -247,7 +247,7 @@ real_t che::area_surface() const real_t area = 0; #pragma omp parallel for reduction(+: area) - for(index_t i = 0; i < n_faces; i++) + for(index_t i = 0; i < n_faces; ++i) area += area_trig(i); return area; @@ -258,7 +258,7 @@ void che::update_heatmap(const real_t * hm, real_t max_color) if(!hm) { #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) VHC[v] = 0.45; return; @@ -267,13 +267,13 @@ void che::update_heatmap(const real_t * hm, real_t max_color) if(max_color < numeric_limits::epsilon()) { #pragma omp parallel for reduction(max: max_color) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) if(hm[v] < INFINITY) max_color = max(hm[v], max_color); } #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) VHC[v] = hm[v] / max_color; } @@ -313,7 +313,7 @@ void che::update_normals() if(!n_faces) return; #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { vertex & n = VN[v]; @@ -430,7 +430,7 @@ real_t che::mean_edge() const real_t m = 0; #pragma omp parallel for reduction(+: m) - for(index_t e = 0; e < n_edges; e++) + for(index_t e = 0; e < n_edges; ++e) m += *(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); return m / n_edges; @@ -468,7 +468,7 @@ void che::normalize() vertex center; #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { #pragma omp critical center += GT[v]; @@ -479,14 +479,14 @@ void che::normalize() real_t max_norm = 0; #pragma omp parallel for reduction(max : max_norm) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { GT[v] -= center; max_norm = std::max(max_norm, *GT[v]); } #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) GT[v] /= max_norm; } @@ -576,7 +576,7 @@ size_t che::max_degree() const size_t d, md = 0; #pragma omp parallel for private(d) reduction(max: md) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { d = 0; for_star(he, this, v) d++; @@ -650,7 +650,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector new_faces; // each 3 gproshan_debug(removing vertex); - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { if(EVT[v] != NIL) new_vertices.push_back(GT[v]); @@ -853,7 +853,7 @@ void che::remove_non_manifold_vertices() gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; - for(index_t v = removed[0] + 1; v < n_vertices; v++) + for(index_t v = removed[0] + 1; v < n_vertices; ++v) { if(v < removed[r]) { @@ -867,7 +867,7 @@ void che::remove_non_manifold_vertices() } } - for(index_t he = 0; he < n_half_edges; he++) + for(index_t he = 0; he < n_half_edges; ++he) if(VT[he] != NIL) new_faces.push_back(VT[he]); else gproshan_error_var(he); @@ -908,7 +908,7 @@ void che::remove_vertices(const vector & vertices) vector new_faces; // each 3 gproshan_debug(removing vertex); - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { if(EVT[v] != NIL) new_vertices.push_back(GT[v]); @@ -921,7 +921,7 @@ void che::remove_vertices(const vector & vertices) gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; - for(index_t v = removed[0] + 1; v < n_vertices; v++) + for(index_t v = removed[0] + 1; v < n_vertices; ++v) { if(v < removed[r]) { @@ -935,7 +935,7 @@ void che::remove_vertices(const vector & vertices) } } - for(index_t he = 0; he < n_half_edges; he++) + for(index_t he = 0; he < n_half_edges; ++he) if(VT[he] != NIL) new_faces.push_back(VT[he]); else gproshan_error_var(he); @@ -977,7 +977,7 @@ gproshan_debug(fill_holes); memcpy(aVT, VT, sizeof(index_t) * n_half_edges); index_t * t_aVT = aVT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; he++) + for(index_t he = 0; he < mesh->n_half_edges; ++he) t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices - ncv; gproshan_debug(fill_holes); @@ -985,11 +985,11 @@ gproshan_debug(fill_holes); gproshan_debug(fill_holes); index_t * t_aOT = aOT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; he++) + for(index_t he = 0; he < mesh->n_half_edges; ++he) t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges : NIL; gproshan_debug(fill_holes); - for(index_t v, he_v, he_i, i = 0; i < ncv; i++) + for(index_t v, he_v, he_i, i = 0; i < ncv; ++i) { he_i = mesh->EVT[i]; if(he_i != NIL && mesh->VT[next(he_i)] < ncv) @@ -1009,7 +1009,7 @@ gproshan_debug(fill_holes); gproshan_debug(fill_holes); index_t * t_aEVT = aEVT + n_vertices; - for(index_t v = ncv; v < mesh->n_vertices; v++) + for(index_t v = ncv; v < mesh->n_vertices; ++v) t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges : NIL; gproshan_debug(fill_holes); @@ -1018,7 +1018,7 @@ gproshan_debug(fill_holes); bool * common_edge = new bool[mesh->n_edges]; memset(common_edge, 0, sizeof(bool) * mesh->n_edges); - for(index_t he_i, i = 0; i < ncv; i++) + for(index_t he_i, i = 0; i < ncv; ++i) { he_i = mesh->EVT[i]; if(he_i != NIL && mesh->VT[next(he_i)] < ncv) @@ -1026,7 +1026,7 @@ gproshan_debug(fill_holes); } index_t ae = n_edges; - for(index_t e = 0; e < mesh->n_edges; e++) + for(index_t e = 0; e < mesh->n_edges; ++e) if(!common_edge[e]) aET[ae++] = mesh->ET[e] + n_half_edges; @@ -1057,11 +1057,11 @@ gproshan_debug(fill_holes); void che::set_head_vertices(index_t * head, const size_t & n) { - for(index_t v, i = 0; i < n; i++) + for(index_t v, i = 0; i < n; ++i) { v = head[i]; - for(index_t j = i + 1; j < n; j++) + for(index_t j = i + 1; j < n; ++j) if(i == head[j]) { head[j] = v; @@ -1076,7 +1076,7 @@ void che::set_head_vertices(index_t * head, const size_t & n) VT[he] = v; swap(EVT[v], EVT[i]); - for(index_t b = 0; b < n_borders; b++) + for(index_t b = 0; b < n_borders; ++b) { if(BT[b] == i) BT[b] = v; else if(BT[b] == v) BT[b] = i; @@ -1107,7 +1107,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con // init default corr corr_t * corr = new corr_t[n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) corr[v].init(EVT[v]); short * faces_fixed = new short[n_faces]; @@ -1131,7 +1131,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con bool is_collapse; - for(index_t e = 0; e < n_edges; e++) + for(index_t e = 0; e < n_edges; ++e) { //e_d = ne; e_d = sort_edges ? sort_edges[e] : rand() % n_edges; @@ -1211,7 +1211,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con new_faces.reserve(n_faces); index_t dv = 0; - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { if(deleted_vertices[v]) dv++; else @@ -1224,14 +1224,14 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con index_t * map_he = new index_t[n_half_edges]; memset(map_he, 255, sizeof(index_t) * n_half_edges); - for(index_t n_he = 0, he = 0; he < n_half_edges; he++) + for(index_t n_he = 0, he = 0; he < n_half_edges; ++he) if(faces_fixed[trig(he)] > -1) { new_faces.push_back(VT[he] - deleted_vertices[VT[he]]); map_he[he] = n_he++; } - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) corr[v].t = trig(map_he[corr[v].t * che::mtrig]); delete_me(); @@ -1380,7 +1380,7 @@ void che::init(const size_t & n_v, const size_t & n_f) if(n_vertices) VHC = new real_t[n_vertices]; #pragma omp parallel for - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) VC[v] = vcolor; } @@ -1393,7 +1393,7 @@ void che::update_evt_ot_et() vector * he_p_vertex = new vector[n_vertices]; //vertex table - for(index_t he = 0; he < n_half_edges; he++) + for(index_t he = 0; he < n_half_edges; ++he) { EVT[VT[he]] = he; he_p_vertex[VT[he]].push_back(he); @@ -1403,7 +1403,7 @@ void che::update_evt_ot_et() memset(OT, -1, sizeof(index_t) * n_half_edges); vector et; - for(index_t he = 0; he < n_half_edges; he++) + for(index_t he = 0; he < n_half_edges; ++he) { if(OT[he] == NIL) { @@ -1423,7 +1423,7 @@ void che::update_evt_ot_et() } // non manifold two disk - //for(index_t he = 0; he < n_half_edges; he++) + //for(index_t he = 0; he < n_half_edges; ++he) // if(OT[he] != NIL) assert(he == OT[OT[he]]); //edge table @@ -1432,7 +1432,7 @@ void che::update_evt_ot_et() memcpy(ET, et.data(), sizeof(index_t) * n_edges); - for(index_t he = 0; he < n_half_edges; he++) + for(index_t he = 0; he < n_half_edges; ++he) if(OT[he] == NIL && EVT[VT[he]] != NIL) { if(OT[EVT[VT[he]]] == NIL && EVT[VT[he]] != he) @@ -1443,7 +1443,7 @@ void che::update_evt_ot_et() else EVT[VT[he]] = he; } -// for(index_t v = 0; v < n_vertices; v++) +// for(index_t v = 0; v < n_vertices; ++v) // if(EVT[v] >= n_half_edges) // { // gproshan_debug_var(EVT[v]); @@ -1456,7 +1456,7 @@ void che::update_evt_ot_et() void che::update_eht() { #pragma omp parallel for - for(index_t e = 0; e < n_edges; e++) + for(index_t e = 0; e < n_edges; ++e) { EHT[ET[e]] = e; if(OT[ET[e]] != NIL) @@ -1474,7 +1474,7 @@ void che::update_bt() vector borders; - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) if(!border[v] && EVT[v] != NIL && OT[EVT[v]] == NIL) { borders.push_back(v); diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 3b104759..b106d2ba 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -92,7 +92,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const auto add_border_vertices = [&](const index_t & i, const index_t & j, const index_t & delta_v = 0) -> index_t { index_t end_v = j < i ? j + size : j; - for(index_t v = i; v <= end_v; v++) + for(index_t v = i; v <= end_v; ++v) { vmap_border[v % size] = vertices[c].size() + delta_v; vertices[c].push_back(mesh->gt(border_vertices[v % size])); @@ -214,7 +214,7 @@ void split_border(vector > & split_indices, che * mesh, c a_mat means; vertex normal; - for(index_t i = 0; i < n; i++) + for(index_t i = 0; i < n; ++i) { normal = mesh->normal(border_vertices[i]); data(0, i) = normal.x; @@ -235,10 +235,10 @@ void split_border(vector > & split_indices, che * mesh, c // clusters = new index_t[n]; index_t a, b; a = NIL; b = NIL; // review this - for(index_t i = 0; i < n; i++) + for(index_t i = 0; i < n; ++i) { real_t d, d_min = INFINITY; - for(index_t c = 0; c < k; c++) + for(index_t c = 0; c < k; ++c) { d = norm(data.col(i) - means.col(c)); if(d < d_min) @@ -265,7 +265,7 @@ vector * fill_all_holes(che * mesh, const size_t & max_iter) tie(border_vertices, holes) = fill_all_holes_meshes(mesh, max_iter); if(holes) { - for(index_t b = 0; b < mesh->n_borders; b++) + for(index_t b = 0; b < mesh->n_borders; ++b) if(holes[b]) delete holes[b]; } delete [] holes; @@ -285,11 +285,11 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t gproshan_debug(inpainting); - for(index_t b = 0; b < n_borders; b++) + for(index_t b = 0; b < n_borders; ++b) mesh->border(border_vertices[b], b); gproshan_debug(inpainting); - for(index_t b = 0; b < n_borders; b++) + for(index_t b = 0; b < n_borders; ++b) { gproshan_debug_var(b); // vector > split_indices; @@ -302,7 +302,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t } gproshan_debug(inpainting); - for(index_t b = 0; b < n_borders; b++) + for(index_t b = 0; b < n_borders; ++b) if(holes[b]) { gproshan_debug(inpainting); @@ -333,7 +333,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vector tmp_normals(vertices.size()); vertex normal; - for(index_t v = 0; v < vertices.size(); v++) + for(index_t v = 0; v < vertices.size(); ++v) { normal = mesh->normal(front_vertices[v]); if(is_grow) normal = -normal; @@ -363,7 +363,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vector > neighbors(vertices.size()); index_t v, p_v, n_v; - for(v = 0; v < vertices.size(); v++) + for(v = 0; v < vertices.size(); ++v) { n_v = (v + 1) % vertices.size(); p_v = v > 0 ? v - 1: vertices.size() - 1; @@ -545,7 +545,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, for(a_vec r: tmp_vertices) vertices.push_back(vertex(r[0], r[1], r[2])); - for(index_t v = 0; false && v < tmp_vertices.size(); v++) + for(index_t v = 0; false && v < tmp_vertices.size(); ++v) a_vec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); gproshan_debug_var(perimeter); @@ -567,7 +567,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c // PCA -------------------------------------------------------------------------- a_mat V(3, vertices.size()); - for(index_t v = 0; v < vertices.size(); v++) + for(index_t v = 0; v < vertices.size(); ++v) { V(0,v) = vertices[v][0]; V(1,v) = vertices[v][1]; @@ -620,7 +620,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c vector > neighbors(vertices.size()); index_t v, p_v, n_v; - for(v = 0; v < vertices.size(); v++) + for(v = 0; v < vertices.size(); ++v) tmp_vertices[v] = V.col(v); auto push_front = [&front](const border_t & b) @@ -628,7 +628,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c if(b.theta <= M_PI) front.push(b); }; - for(v = 0; v < vertices.size(); v++) + for(v = 0; v < vertices.size(); ++v) { n_v = (v + 1) % vertices.size(); p_v = v > 0 ? v - 1: vertices.size() - 1; @@ -832,7 +832,7 @@ void get_real_tri(che * mesh, vector & select_vertices, vector c++; - for (size_t i = 0; i < d ; i++) + for (size_t i = 0; i < d ; ++i) { if(i < b) { @@ -895,7 +895,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i index_t f = 0; i = 0; - for( ; i< tri_sizes[0]-1; i++) + for( ; i< tri_sizes[0]-1; ++i) { faces[f++] = i; faces[f++] = tri_init; @@ -903,7 +903,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i } i++; - for( ; i < tri_sizes[0] + tri_sizes[1] - 1; i++) + for( ; i < tri_sizes[0] + tri_sizes[1] - 1; ++i) { faces[f++] = i; faces[f++] = tri_init + 1; @@ -911,7 +911,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i } i++; - for( ; i < select_vertices.size() - 1; i++) + for( ; i < select_vertices.size() - 1; ++i) { faces[f++] = i; faces[f++] = tri_init + 2; diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index ddaa7d11..9ad41ac3 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -36,8 +36,8 @@ void che_img::read_file(const string & file) init(img.height() * img.width(), 2 * (img.height() - 1) * (img.width() - 1)); index_t v = 0, he = 0; - for(int i = 0; i < img.width(); i++) - for(int j = 0; j < img.height(); j++) + for(int i = 0; i < img.width(); ++i) + for(int j = 0; j < img.height(); ++j) { if(i && j) { diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index b3152260..3786d27f 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -52,7 +52,7 @@ void che_obj::read_file(const string & file) if(key == "f") { - for(i = 0; ss >> face[i]; i++) + for(i = 0; ss >> face[i]; ++i) ss.ignore(256, ' '); if(i == che::mtrig) @@ -111,13 +111,13 @@ void che_obj::write_file(const che * mesh, const string & file) os << "# faces: " << mesh->n_faces << endl; os << "#\n####\n"; - for(size_t v = 0; v < mesh->n_vertices; v++) + for(size_t v = 0; v < mesh->n_vertices; ++v) os << "v " << mesh->gt(v) << endl; for(index_t he = 0; he < mesh->n_half_edges; ) { os << "f"; - for(index_t i = 0; i < che::mtrig; i++) + for(index_t i = 0; i < che::mtrig; ++i) os << " " << mesh->vt(he++) + 1; os << endl; } diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index fa86c627..e51c3e1a 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -39,7 +39,7 @@ void che_off::read_file(const string & file) init(nv, nf); float x, y, z, r, g, b, a; - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { fscanf(fp, "%f %f %f", &x, &y, &z); GT[v] = { x, y, z }; @@ -60,12 +60,12 @@ void che_off::read_file(const string & file) if(soff[0] == 'C' || soff[1] == 'C') { #pragma omp parallel for - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) VC[i] /= 255; } index_t he = 0; - for(index_t i = 0; i < n_faces; i++) + for(index_t i = 0; i < n_faces; ++i) { fscanf(fp, "%lu", &ne); if(!i && ne > che::mtrig) @@ -78,7 +78,7 @@ void che_off::read_file(const string & file) GT = tGT; } - for(index_t j = 0; j < ne; j++) + for(index_t j = 0; j < ne; ++j) fscanf(fp, "%u", VT + he++); // divide face @@ -101,7 +101,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t os << off << endl; os << mesh->n_vertices << " " << (pointcloud ? 0 : mesh->n_faces) << " 0" << endl; - for(size_t v = 0; v < mesh->n_vertices; v++) + for(size_t v = 0; v < mesh->n_vertices; ++v) { os << mesh->gt(v); @@ -114,7 +114,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t for(index_t he = 0; he < mesh->n_half_edges; ) { os << che::mtrig; - for(index_t i = 0; i < che::mtrig; i++) + for(index_t i = 0; i < che::mtrig; ++i) os << " " << mesh->vt(he++); os << endl; } diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 8849cbcc..8cf945e7 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -89,7 +89,7 @@ void che_ply::read_file(const string & file) if(format == "ascii") { - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { getline(is, str); stringstream ss(str); @@ -118,7 +118,7 @@ void che_ply::read_file(const string & file) }; char * vbuffer = new char[vbytes]; - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { is.read(vbuffer, vbytes); if(big_endian) big_to_little(vbuffer, vbytes); @@ -130,13 +130,13 @@ void che_ply::read_file(const string & file) if(xyz == 4) { float * X = (float *) vbuffer; - for(index_t i = 0; i < 3; i++) + for(index_t i = 0; i < 3; ++i) GT[v][i] = X[i]; } else { double * X = (double *) vbuffer; - for(index_t i = 0; i < 3; i++) + for(index_t i = 0; i < 3; ++i) GT[v][i] = (real_t) X[i]; } } @@ -185,13 +185,13 @@ void che_ply::write_file(const che * mesh, const string & file) os << "property list uchar int vertex_index" << endl; os << "end_header" << endl; - for(size_t v = 0; v < mesh->n_vertices; v++) + for(size_t v = 0; v < mesh->n_vertices; ++v) os << mesh->gt(v) << endl; for(index_t he = 0; he < mesh->n_half_edges; ) { os << che::mtrig; - for(index_t i = 0; i < che::mtrig; i++) + for(index_t i = 0; i < che::mtrig; ++i) os << " " << mesh->vt(he++); os << endl; } diff --git a/src/mesh/che_poisson.cpp b/src/mesh/che_poisson.cpp index a5d8c34b..8f3800af 100644 --- a/src/mesh/che_poisson.cpp +++ b/src/mesh/che_poisson.cpp @@ -15,7 +15,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) if(!k) return; a_mat B(mesh->n_vertices, 3); - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { if(v < old_n_vertices) { @@ -29,7 +29,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) a_sp_mat L, A; laplacian(mesh, L, A); - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) B.row(i) *= -1 / A(i,i); a_sp_mat M; @@ -38,7 +38,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) if(k > 1) M = A * L; while(--k) L *= M; - for(index_t v = 0; v < old_n_vertices; v++) + for(index_t v = 0; v < old_n_vertices; ++v) { B.col(0) -= mesh->gt(v).x * L.col(v); B.col(1) -= mesh->gt(v).y * L.col(v); @@ -53,7 +53,7 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) a_mat X; if(spsolve(X, s * L, s * B)) - for(index_t v = old_n_vertices; v < mesh->n_vertices; v++) + for(index_t v = old_n_vertices; v < mesh->n_vertices; ++v) { mesh->get_vertex(v).x = X(v - old_n_vertices, 0); mesh->get_vertex(v).y = X(v - old_n_vertices, 1); @@ -69,10 +69,10 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) a_mat A(n, n); a_vec pi(2), pj(2); - for(index_t i = 0; i < n; i++) + for(index_t i = 0; i < n; ++i) { pi(0) = P(0, i); pi(1) = P(1, i); - for(index_t j = 0; j < n; j++) + for(index_t j = 0; j < n; ++j) { pj(0) = P(0, j); pj(1) = P(1, j); x = norm(pi - pj); @@ -82,11 +82,11 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) a_mat alpha = solve(A, P.row(2).t()); - for(index_t i = 0; i < H.n_cols; i++) + for(index_t i = 0; i < H.n_cols; ++i) { H(2, i) = 0; pi(0) = H(0, i); pi(1) = H(1, i); - for(index_t j = 0; j < n; j++) + for(index_t j = 0; j < n; ++j) { pj(0) = P(0, j); pj(1) = P(1, j); x = norm(pi - pj); @@ -111,7 +111,7 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t vector sub_mesh_hole; sub_mesh_hole.reserve(n_border_vertices); - for(index_t b = 0; b < n_border_vertices; b++) + for(index_t b = 0; b < n_border_vertices; ++b) if(sorted[b] < old_n_vertices) sub_mesh_hole.push_back(sorted[b]); @@ -130,7 +130,7 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t a_mat H(3, n_vertices - old_n_vertices); - for(index_t i = 0, v = old_n_vertices; v < n_vertices; v++) + for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++v) { H(0, i) = mesh->gt(v).x; H(1, i) = mesh->gt(v).y; diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 83c647f1..43e60abd 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -34,13 +34,13 @@ void che_ptx::read_file(const string & file) fscanf(fp, "%lu %lu", &n_rows, &n_cols); - for(index_t i = 0; i < 12; i++) + for(index_t i = 0; i < 12; ++i) fscanf(fp, "%f", T + i); - for(index_t i = 0; i < 12; i++) + for(index_t i = 0; i < 12; ++i) fscanf(fp, "%f", R + i); - for(index_t i = 0; i < 4; i++) + for(index_t i = 0; i < 4; ++i) fscanf(fp, "%f", tr + i); @@ -61,7 +61,7 @@ void che_ptx::read_file(const string & file) GT[0] = { x, y, z }; VC[0] = { r, g, b }; - for(index_t v = 1; v < n_vertices; v++) + for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b); @@ -74,7 +74,7 @@ void che_ptx::read_file(const string & file) GT[0] = { x, y, z }; VC[0] = { a, a, a }; - for(index_t v = 1; v < n_vertices; v++) + for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); sscanf(line, "%f %f %f %f", &x, &y, &z, &a); @@ -100,8 +100,8 @@ void che_ptx::read_file(const string & file) he -= 3; }; - for(index_t r = 0; r < n_rows - 1; r++) - for(index_t c = 0; c < n_cols - 1; c++) + for(index_t r = 0; r < n_rows - 1; ++r) + for(index_t c = 0; c < n_cols - 1; ++c) { add_trig((c ) + (r ) * n_cols, (c ) + (r + 1) * n_cols, @@ -116,7 +116,7 @@ void che_ptx::read_file(const string & file) rw(n_faces) = he / che::mtrig; #pragma omp parallel for - for(index_t i = 0; i < n_vertices; i++) + for(index_t i = 0; i < n_vertices; ++i) VC[i] /= 255; } diff --git a/src/mesh/che_sphere.cpp b/src/mesh/che_sphere.cpp index 59b7e6ee..1bd7e4e0 100644 --- a/src/mesh/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -31,9 +31,9 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) size_t v, cols = n - 1; - for(index_t i = 0; i < 2 * n - 1; i++) + for(index_t i = 0; i < 2 * n - 1; ++i) { - for(index_t j = 0; j < cols - 1; j++) + for(index_t j = 0; j < cols - 1; ++j) { v = i * cols + j; @@ -57,7 +57,7 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) faces.push_back(v); } - for(index_t j = 0; j < cols - 1; j++) + for(index_t j = 0; j < cols - 1; ++j) { v = (2 * n - 1) * cols + j; diff --git a/src/mesh/simplification.cpp b/src/mesh/simplification.cpp index 11d2cc24..9ffe1bbd 100644 --- a/src/mesh/simplification.cpp +++ b/src/mesh/simplification.cpp @@ -58,7 +58,7 @@ void simplification::execute(const vertex *const & normals) while(--levels) { #pragma omp parallel for private(he, vi) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { corr_v[v] = mesh->corr_vertex(corr[v]); he = corr[v].t * che::mtrig; @@ -72,7 +72,7 @@ void simplification::execute(const vertex *const & normals) corr_aux = mesh->edge_collapse(sort_edges, normals); #pragma omp parallel for private(vi, a, b, c) - for(index_t v = 0; v < n_vertices; v++) + for(index_t v = 0; v < n_vertices; ++v) { vi = v * che::mtrig; a = mesh->corr_vertex(corr_aux[corr_i[vi]]); @@ -102,7 +102,7 @@ void simplification::compute_quadrics() vertex n; #pragma omp parallel for private(n) - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { Q[v].resize(4,4); Q[v].zeros(); @@ -124,7 +124,7 @@ void simplification::compute_quadrics() void simplification::order_edges(index_t * const & sort_edges, real_t * const & error_edges) { #pragma omp parallel for - for(index_t e = 0; e < mesh->n_edges; e++) + for(index_t e = 0; e < mesh->n_edges; ++e) { sort_edges[e] = e; error_edges[e] = compute_error(e); diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 0f5b1c64..e2600466 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -66,14 +66,14 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, if(!n_samples) { #pragma omp parallel for - for(index_t i = 0; i < width; i++) - for(index_t j = 0; j < height; j++) + for(index_t i = 0; i < width; ++i) + for(index_t j = 0; j < height; ++j) img[j * width + i] = glm::vec4(0); } #pragma omp parallel for private(li) - for(index_t i = 0; i < width; i++) - for(index_t j = 0; j < height; j++) + for(index_t i = 0; i < width; ++i) + for(index_t j = 0; j < height; ++j) { //row major glm::vec4 & color = img[j * width + i]; @@ -110,13 +110,13 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); #pragma omp parallel for - for(index_t i = 0; i < windows_size.x; i++) - for(index_t j = 0; j < windows_size.y; j++) + for(index_t i = 0; i < windows_size.x; ++i) + for(index_t j = 0; j < windows_size.y; ++j) { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; - for(index_t s = 0; s < samples; s++) + for(index_t s = 0; s < samples; ++s) { glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, (float(j) + randf(gen)) / windows_size.y diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index fcf304da..aba3f6ec 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -181,7 +181,7 @@ index_t embree::add_pointcloud(const che * mesh) ); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) { pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 3b647b83..87fbe44e 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -38,7 +38,7 @@ index_t embree_splat::add_pointcloud(const che * mesh) ); #pragma omp parallel for - for(index_t i = 0; i < vsplat.size(); i++) + for(index_t i = 0; i < vsplat.size(); ++i) { pxyzr[i] = vsplat[i].xyzr(); normal[i] = vsplat[i].normal(); @@ -82,7 +82,7 @@ void embree_splat::init_splats(const che * mesh) gproshan_log_var(vsplat.size()); #pragma omp parallel for - for(index_t i = 0; i < vsplat.size(); i++) + for(index_t i = 0; i < vsplat.size(); ++i) { const index_t v = n * i; // random, feature aware index @@ -112,7 +112,7 @@ void embree_splat::init_splats(const che * mesh) const vertex & c = mesh->gt(v); splat & s = vsplat[i]; - for(index_t j = 0; j < K; j++) + for(index_t j = 0; j < K; ++j) { dist = INFINITY; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index c03d5ebe..dffa9e1c 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -92,7 +92,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) std::vector optix_meshes(meshes.size()); std::vector optix_trig_flags(meshes.size()); - for(index_t i = 0; i < meshes.size(); i++) + for(index_t i = 0; i < meshes.size(); ++i) add_mesh(optix_meshes[i], optix_trig_flags[i], meshes[i]); OptixAccelBuildOptions optix_accel_opt = {}; @@ -180,7 +180,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(float) * 3); #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; i++) + for(index_t i = 0; i < mesh->n_vertices; ++i) vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); diff --git a/src/util.cpp b/src/util.cpp index b533db37..28fd4d9d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -15,14 +15,14 @@ void copy_real_t_array(float * destination, const float * source, const size_t & void copy_real_t_array(float * destination, const double * source, const size_t & n_elem) { #pragma omp parallel for - for(index_t i = 0; i < n_elem; i++) + for(index_t i = 0; i < n_elem; ++i) destination[i] = source[i]; } void copy_real_t_array(double * destination, const float * source, const size_t & n_elem) { #pragma omp parallel for - for(index_t i = 0; i < n_elem; i++) + for(index_t i = 0; i < n_elem; ++i) destination[i] = source[i]; } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 86271ea2..6b035668 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -51,7 +51,7 @@ void che_viewer::update() vertex pmin(INFINITY, INFINITY, INFINITY); vertex pmax(0, 0, 0); - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) { const vertex & p = mesh->gt(v); @@ -174,14 +174,14 @@ void che_viewer::translate(const vertex & p) v_translate = p; #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->get_vertex(v) += v_translate; } void che_viewer::invert_orientation() { #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; v++) + for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->normal(v) = -mesh->normal(v); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index f3c5287c..f109775c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -114,7 +114,7 @@ bool viewer::run() { if(ImGui::BeginMenu("Select")) { - for(index_t i = 0; i < n_meshes; i++) + for(index_t i = 0; i < n_meshes; ++i) if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; @@ -127,14 +127,14 @@ bool viewer::run() if(ImGui::BeginMenu("Colormap")) { - for(index_t i = 0; i < colormap.size(); i++) + for(index_t i = 0; i < colormap.size(); ++i) if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == idx_colormap, i != idx_colormap)) idx_colormap = i; ImGui::EndMenu(); } - for(index_t i = 0; i < sub_menus.size(); i++) + for(index_t i = 0; i < sub_menus.size(); ++i) { if(ImGui::BeginMenu(sub_menus[i].c_str())) { @@ -340,7 +340,7 @@ void viewer::add_mesh(che * p_mesh) const int & rows = m_window_size[n_meshes][0]; const int & cols = m_window_size[n_meshes][1]; - for(index_t m = 0; m < n_meshes; m++) + for(index_t m = 0; m < n_meshes; ++m) { meshes[m].vx = m % cols; meshes[m].vy = rows - (m / cols) - 1; @@ -464,7 +464,7 @@ bool viewer::menu_save_load_view(viewer * view) if(ImGui::BeginCombo("##loadfile", vfiles[select].c_str())) { - for(index_t i = 0; i < vfiles.size(); i++) + for(index_t i = 0; i < vfiles.size(); ++i) { if(ImGui::Selectable(vfiles[i].c_str(), select == i)) select = i; @@ -826,7 +826,7 @@ void viewer::draw_meshes(shader & program, const bool & normals) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - for(index_t i = 0; i < n_meshes; i++) + for(index_t i = 0; i < n_meshes; ++i) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); @@ -845,7 +845,7 @@ void viewer::draw_selected_vertices(shader & program) { sphere_translations.resize(active_mesh().selected.size()); - for(index_t i = 0; i < active_mesh().selected.size(); i++) + for(index_t i = 0; i < active_mesh().selected.size(); ++i) sphere_translations[i] = active_mesh()->gt(active_mesh().selected[i]); sphere.update_instances_translations(sphere_translations); @@ -862,7 +862,7 @@ void viewer::draw_selected_vertices(shader & program) void viewer::select_border_vertices() { active_mesh().selected.clear(); - for(index_t b = 0; b < active_mesh()->n_borders; b++) + for(index_t b = 0; b < active_mesh()->n_borders; ++b) for_border(he, active_mesh(), active_mesh()->bt(b)) active_mesh().selected.push_back(active_mesh()->vt(he)); } From fcf30006c439e31aedd644431502bd2ad2067935 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 6 Mar 2021 23:49:55 +0100 Subject: [PATCH 0457/1018] update increment i++ to ++i --- src/geodesics/geodesics_ptp.cpp | 8 +++---- src/geodesics/geodesics_ptp.cu | 4 ++-- src/geodesics/geodesics_ptp_coalescence.cu | 4 ++-- src/geodesics/test_geodesics_ptp.cpp | 4 ++-- src/geodesics/test_geodesics_ptp.cu | 6 ++--- .../test_geodesics_ptp_coalescence.cu | 6 ++--- src/laplacian/laplacian.cpp | 2 +- src/mdict/image_denoising.cpp | 10 ++------ src/mdict/mdict.cpp | 4 ++-- src/mdict/msparse_coding.cpp | 8 +++---- src/mdict/patch.cpp | 14 ++++++++--- src/mesh/che.cpp | 24 +++++++++---------- src/mesh/che_fill_hole.cpp | 13 +++++----- src/mesh/che_off.cpp | 6 ++--- src/mesh/che_poisson.cpp | 5 ++-- src/raytracing/raytracing.cpp | 2 +- src/viewer/viewer.cpp | 2 +- 17 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index a2ea28fb..8253f908 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -103,8 +103,8 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c for(index_t v = start; v < start + n_cond; ++v) count += error[v] < PTP_TOL; - if(n_cond == count) i++; - if(j < toplesets.limits.size() - 1) j++; + if(n_cond == count) ++i; + if(j < toplesets.limits.size() - 1) ++j; d = !d; } @@ -183,8 +183,8 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c for(index_t vi = start; vi < start + n_cond; ++vi) count += error[vi] < PTP_TOL; - if(n_cond == count) i++; - if(j < toplesets.limits.size() - 1) j++; + if(n_cond == count) ++i; + if(j < toplesets.limits.size() - 1) ++j; d = !d; } diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index 9e759eab..5d9d4537 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -222,8 +222,8 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r cudaDeviceSynchronize(); if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - i++; - if(j < limits.size() - 1) j++; + ++i; + if(j < limits.size() - 1) ++j; d = !d; } diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index 397d239b..8c53d058 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -149,9 +149,9 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t cudaDeviceSynchronize(); if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - i++; + ++i; - if(j < inv.limits.size() - 1) j++; + if(j < inv.limits.size() - 1) ++j; d = !d; } diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 1288ff6d..397c82ad 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -160,8 +160,8 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->ot_evt(v) == NIL ? 1 : 0; - for_star(he, mesh, v) dv++; - deg[dv]++; + for_star(he, mesh, v) ++dv; + ++deg[dv]; } ofstream os(test_path + filename + ".deg"); diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index ab5400f2..b745645e 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -184,7 +184,7 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ while(i < j) { - n_iter++; + ++n_iter; start = limits[i]; end = limits[j]; n_cond = limits[i + 1] - start; @@ -201,8 +201,8 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ cudaDeviceSynchronize(); if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - i++; - if(j < limits.size() - 1) j++; + ++i; + if(j < limits.size() - 1) ++j; d = !d; } diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 428e71a3..0f3e692c 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -235,7 +235,7 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, while(i < j) { - n_iter++; + ++n_iter; start = limits[i]; end = limits[j]; n_cond = limits[i + 1] - start; @@ -254,8 +254,8 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, cudaDeviceSynchronize(); if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - i++; - if(j < limits.size() - 1) j++; + ++i; + if(j < limits.size() - 1) ++j; d = !d; } diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index e0d760aa..b4eeb10f 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -28,7 +28,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) DI(1, i) = mesh->vt(mesh->et(e)); DV(i) = -1; - i++; + ++i; DI(0, i) = e; DI(1, i) = mesh->vt(next(mesh->et(e))); diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index c482e38c..68ebed3f 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -35,10 +35,7 @@ void test_image_denoising(const string & file) for(index_t b = y; b < y + p; ++b) for(index_t a = x; a < x + p; ++a) - { - X(k, i) = image(a, b); - k++; - } + X(k++, i) = image(a, b); } a_mat D(n, m, arma::fill::randu); @@ -115,10 +112,7 @@ void test_image_denoising(const string & file) for(index_t b = y; b < y + p; ++b) for(index_t a = x; a < x + p; ++a) - { - image_out(a, b) += Y(k, i); - k++; - } + image_out(a, b) += Y(k++, i); } rows = image.width(); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 1dd98d28..54a45ff6 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -129,7 +129,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L DD = D.cols(selected_atoms.head(l + 1)); aa = pinv(DD) * x; r = x - DD * aa; - l++; + ++l; } return {aa, selected_atoms.head(l)}; } @@ -162,7 +162,7 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L DD = D.cols(selected_atoms.head(l + 1)); aa = pinv(DD) * x; r = x - DD * aa; - l++; + ++l; } return {aa, selected_atoms.head(l)}; } diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 01f76078..1a6d0432 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -99,7 +99,7 @@ void msparse_coding::load_mask() { mask[rn] = 1; V(rn) = 1; - k++; + ++k; } } V.save(f_mask); @@ -159,7 +159,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde if( !cover_cluster[clusters[rn]] && percentages_size[clusters[rn]] == 0) { cover_cluster[clusters[rn]] = 1; // It is finished - k++; + ++k; } } @@ -237,7 +237,7 @@ void msparse_coding::load_sampling(bool save_all) if(p.vertices.size() >= 7 ) { for(const index_t & v: p.vertices) - if(!covered[v]) count_cov_patch++; + if(!covered[v]) ++count_cov_patch; count_cov += count_cov_patch; if(count_cov_patch > 0) @@ -1014,7 +1014,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) } alpha.col(s) = sum; patches_covered[s-threshold] = 1; - count++; + ++count; } } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 3675f62c..f7a93d2c 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -325,7 +325,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & { m = 0; for(index_t i = 0; i < vertices.size(); ++i) - if(mask(vertices[i])) m++; + if(mask(vertices[i])) ++m; } xyz.set_size(3, m); @@ -441,7 +441,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co xyz(0, j) = np(0); xyz(1, j) = np(1); xyz(2, j) = np(2); - j++; + ++j; /*if(p == 76) { gproshan_debug_var(np); @@ -466,7 +466,15 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector level) { - level++; - - if(level > k) break; + if(++level > k) break; limits.push_back(i); } @@ -785,13 +783,13 @@ void che::multiplicate_vertices() ET[n_edges] = next(he); EHT[next(he)] = n_edges; EHT[OT[next(he)]] = n_edges; - rw(n_edges)++; + ++rw(n_edges); if(split) { ET[n_edges] = n_half_edges - 3; EHT[n_half_edges - 3] = n_edges; - rw(n_edges)++; + ++rw(n_edges); } }; @@ -813,7 +811,7 @@ void che::multiplicate_vertices() OT[he] = n_half_edges - 3; } - rw(n_vertices)++; + ++rw(n_vertices); } else flip(e); @@ -862,8 +860,8 @@ void che::remove_non_manifold_vertices() } else if(v == removed[r]) { - d++; - r++; + ++d; + ++r; } } @@ -930,8 +928,8 @@ void che::remove_vertices(const vector & vertices) } else if(v == removed[r]) { - d++; - r++; + ++d; + ++r; } } @@ -1095,7 +1093,7 @@ index_t che::link_intersect(const index_t & v_a, const index_t & v_b) for(index_t & he_a: link_a) for(index_t & he_b: link_b) if(VT[he_a] == VT[he_b]) - intersect++; + ++intersect; return intersect; } @@ -1213,7 +1211,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con index_t dv = 0; for(index_t v = 0; v < n_vertices; ++v) { - if(deleted_vertices[v]) dv++; + if(deleted_vertices[v]) ++dv; else { deleted_vertices[v] = dv; diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index b106d2ba..66303034 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -828,8 +828,7 @@ void get_real_tri(che * mesh, vector & select_vertices, vector vertex tri[3]; - if(r == 2) - c++; + if(r == 2) ++c; for (size_t i = 0; i < d ; ++i) @@ -837,17 +836,17 @@ void get_real_tri(che * mesh, vector & select_vertices, vector if(i < b) { tri[0] += mesh->gt(select_vertices[i]); - tri_sizes[0]++; + ++tri_sizes[0]; } else if ( i >= b && i < c) { tri[1] += mesh->gt(select_vertices[i]); - tri_sizes[1]++; + ++tri_sizes[1]; } else { tri[2] += mesh->gt(select_vertices[i]); - tri_sizes[2]++; + ++tri_sizes[2]; } } @@ -902,7 +901,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i faces[f++] = i + 1; } - i++; + ++i; for( ; i < tri_sizes[0] + tri_sizes[1] - 1; ++i) { faces[f++] = i; @@ -910,7 +909,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i faces[f++] = i + 1; } - i++; + ++i; for( ; i < select_vertices.size() - 1; ++i) { faces[f++] = i; diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index e51c3e1a..12cb0c1d 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -84,10 +84,10 @@ void che_off::read_file(const string & file) // divide face if(ne == che::mquad) { - VT[he] = VT[he - ne]; he++; - VT[he] = VT[he - che::mtrig]; he++; + VT[he] = VT[he - ne]; ++he; + VT[he] = VT[he - che::mtrig]; ++he; - i++; + ++i; } } diff --git a/src/mesh/che_poisson.cpp b/src/mesh/che_poisson.cpp index 8f3800af..bf3e75a5 100644 --- a/src/mesh/che_poisson.cpp +++ b/src/mesh/che_poisson.cpp @@ -125,17 +125,16 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t P(0, i) = mesh->gt(b).x; P(1, i) = mesh->gt(b).y; P(2, i) = mesh->gt(b).z; - i++; + ++i; } a_mat H(3, n_vertices - old_n_vertices); - for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++v) + for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++i, ++v) { H(0, i) = mesh->gt(v).x; H(1, i) = mesh->gt(v).y; H(2, i) = mesh->gt(v).z; - i++; } a_vec avg = mean(H, 1); diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index e2600466..827f1e7b 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -93,7 +93,7 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, color = (color * float(n_samples) + li / float(light.size())) / float(n_samples + 1); } - n_samples++; + ++n_samples; } float * raytracing::raycaster( const glm::uvec2 & windows_size, diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index f109775c..50386c07 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -333,7 +333,7 @@ void viewer::add_mesh(che * p_mesh) meshes[n_meshes].init(p_mesh); meshes[n_meshes].log_info(); - n_meshes++; + ++n_meshes; idx_active_mesh = n_meshes - 1; glfwSetWindowTitle(window, active_mesh()->filename().c_str()); From 9ac241537835d18082c2af15c6563ac95b4f85cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 6 Mar 2021 23:55:16 +0100 Subject: [PATCH 0458/1018] cleaning up extra spaces --- include/geodesics/heat_method.h | 2 +- include/geometry/convex_hull.h | 4 ++-- include/mdict/patch.h | 6 +++--- src/app_viewer.cpp | 2 +- src/features/descriptor.cpp | 2 +- src/features/key_points.cpp | 4 ++-- src/geodesics/geodesics_ptp.cpp | 2 +- src/geodesics/heat_method.cpp | 6 +++--- src/geodesics/heat_method.cu | 2 +- src/geometry/convex_hull.cpp | 2 +- src/mdict/mdict.cpp | 6 +++--- src/mdict/msparse_coding.cpp | 34 ++++++++++++++++----------------- src/mdict/patch.cpp | 30 ++++++++++++++--------------- src/mesh/che.cpp | 2 +- src/mesh/che_fill_hole.cpp | 4 ++-- src/raytracing/raytracing.cpp | 4 ++-- src/raytracing/rt_embree.cpp | 2 +- src/raytracing/rt_optix.cpp | 10 +++++----- src/raytracing/rt_optix.cu | 2 +- src/viewer/camera.cpp | 2 +- src/viewer/frame.cpp | 2 +- src/viewer/viewer.cpp | 12 ++++++------ 22 files changed, 71 insertions(+), 71 deletions(-) diff --git a/include/geodesics/heat_method.h b/include/geodesics/heat_method.h index 20c86c27..71f8fc67 100644 --- a/include/geodesics/heat_method.h +++ b/include/geodesics/heat_method.h @@ -35,7 +35,7 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & m, cholmod_common * context); #ifdef GPROSHAN_CUDA -/// +/// double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b); #endif // GPROSHAN_CUDA diff --git a/include/geometry/convex_hull.h b/include/geometry/convex_hull.h index 8c6ed9a2..ce83fd86 100644 --- a/include/geometry/convex_hull.h +++ b/include/geometry/convex_hull.h @@ -1,7 +1,7 @@ #ifndef CONVEX_HULL_H #define CONVEX_HULL_H -#include "mesh/vertex.h" +#include "mesh/vertex.h" #include @@ -10,7 +10,7 @@ namespace gproshan { -///< 2D Convex Hull +///< 2D Convex Hull class convex_hull { private: diff --git a/include/mdict/patch.h b/include/mdict/patch.h index e21fb4e1..d138220b 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include "include_arma.h" @@ -33,7 +33,7 @@ class msparse_coding; typedef function fmask_t; typedef std::map vpatches_t; -/// +/// class patch { public: @@ -64,7 +64,7 @@ class patch void init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, - vector & _vertices, + vector & _vertices, index_t * _toplevel = nullptr); void init_radial_disjoint( real_t & euc_radio, diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index f58d0d3a..d651bcec 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -580,7 +580,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) { - if(toplesets[v] < n_toplesets) + if(toplesets[v] < n_toplesets) mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); } diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 7bdc751a..53bb0060 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -61,7 +61,7 @@ void descriptor::compute_wks() #pragma omp parallel for for(index_t t = 0; t < features.n_cols; ++t) - features.col(t) = eigvec * exp(-pow(e(t) - eigval, 2) / sigma_2) / + features.col(t) = eigvec * exp(-pow(e(t) - eigval, 2) / sigma_2) / sum(exp(-pow(e(t) - eigval, 2) / sigma_2)); } diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 8071ced9..abce564a 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -35,13 +35,13 @@ key_points::~key_points() const index_t & key_points::operator[](const index_t & i) const { assert(i < n_vertices); - return kps[i]; + return kps[i]; } const bool & key_points::operator()(const index_t & i) const { assert(i < n_vertices); - return is_kp[i]; + return is_kp[i]; } const size_t & key_points::size() const diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index 8253f908..2a17bbf6 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -229,7 +229,7 @@ real_t update_step(che * mesh, const real_t * dist, const index_t & he) real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); real_t dis = delta * delta - - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * + (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); real_t p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 14364800..746c4650 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -41,7 +41,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & switch(opt) { - case HEAT_ARMA: + case HEAT_ARMA: if(!spsolve(u, A, u0)) gproshan_error(arma: no solution); break; case HEAT_CHOLMOD: @@ -63,7 +63,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & switch(opt) { - case HEAT_ARMA: + case HEAT_ARMA: if(!spsolve(phi, L, div)) gproshan_error(arma: no solution); break; case HEAT_CHOLMOD: @@ -95,7 +95,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) for_star(he, mesh, v) sum += ( mesh->normal_he(he) * ( mesh->gt_vt(prev(he)) - mesh->gt_vt(next(he)) ) , - - mesh->gradient_he(he, u.memptr()) + - mesh->gradient_he(he, u.memptr()) ); } } diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index dad7e949..6064c74c 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -24,7 +24,7 @@ struct cu_spAxb cudaMemcpy(A_row_indices, hA_row_indices, nnz * sizeof(int), cudaMemcpyHostToDevice); cudaMalloc(&A_values, nnz * sizeof(real_t)); - cudaMemcpy(A_values, hA_values, nnz * sizeof(real_t), cudaMemcpyHostToDevice); + cudaMemcpy(A_values, hA_values, nnz * sizeof(real_t), cudaMemcpyHostToDevice); cudaMalloc(&b, nnz * sizeof(real_t)); cudaMemcpy(b, hb, nnz * sizeof(real_t), cudaMemcpyHostToDevice); diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp index 4d5b5480..bf0c7f10 100644 --- a/src/geometry/convex_hull.cpp +++ b/src/geometry/convex_hull.cpp @@ -1,4 +1,4 @@ -#include "geometry/convex_hull.h" +#include "geometry/convex_hull.h" #include diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 54a45ff6..6d5e4b33 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -24,7 +24,7 @@ bool operator < (const locval_t & a, const locval_t & b) std::ostream & operator << (std::ostream & os, const locval_t & lc) { - return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; + return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; } void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L) @@ -231,7 +231,7 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) } -// MESH DENSE +// MESH DENSE a_vec OMP(const patch & p, const a_mat & A, const size_t & L) { @@ -301,7 +301,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) a(j) = 0; D = patches[i].phi * A; // fetch the discrete dictionary for the patch i - e = patches[i].xyz.row(2).t() - D * a; // getting the rec error for the patch i + e = patches[i].xyz.row(2).t() - D * a; // getting the rec error for the patch i aj = as_scalar(e.t() * D.col(j) / (D.col(j).t() * D.col(j))); sum += aj * aj * patches[i].phi.t() * patches[i].phi; diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 1a6d0432..cdc8b914 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -47,7 +47,7 @@ size_t msparse_coding::T = 5; msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) { A.eye(phi_basis->dim(), m_params.n_atoms); - dist = new real_t[mesh->n_vertices]; + dist = new real_t[mesh->n_vertices]; m_params.n_patches = mesh->n_vertices / m_params.avg_p; @@ -77,7 +77,7 @@ void msparse_coding::load_mask() if(V.load(f_mask)) { - #pragma omp for + #pragma omp for for(index_t i = 0; i < mesh->n_vertices; ++i) { mask[i] = V(i); @@ -113,7 +113,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde if(V.load(f_mask)) { - #pragma omp for + #pragma omp for for(index_t i = 0; i < mesh->n_vertices; ++i) { mask[i] = V(i); @@ -129,7 +129,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde // create initial desired percentage sizes - #pragma omp for + #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) { percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; @@ -194,7 +194,7 @@ void msparse_coding::load_sampling(bool save_all) { patch p; p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); - patches.push_back(move(p)); + patches.push_back(move(p)); } m_params.n_patches = n_seeds; @@ -209,10 +209,10 @@ void msparse_coding::load_sampling(bool save_all) // compute features will be seeds patches_map.resize(mesh->n_vertices); - //Coverage of the points + //Coverage of the points bool covered[mesh->n_vertices]; - #pragma omp for + #pragma omp for for(index_t i = 0; i < mesh->n_vertices; ++i) covered[i] = 0; @@ -351,7 +351,7 @@ void msparse_coding::init_radial_feature_patches() p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - } + } bool save_all = true; if(save_all) @@ -411,7 +411,7 @@ void msparse_coding::init_voronoi_patches() //saving first vertex aka seed vertices - #pragma omp for + #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) { vertices[s].push_back(sample(s)); @@ -437,7 +437,7 @@ void msparse_coding::init_voronoi_patches() { index_t * toplevel = new index_t[mesh->n_vertices]; - #pragma omp for + #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) { patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); @@ -479,7 +479,7 @@ void msparse_coding::init_voronoi_patches() phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); p.compute_avg_distance(mesh, patches_map, s); - } + } gproshan_log(our patches are ready); } @@ -528,7 +528,7 @@ real_t msparse_coding::execute() bool * pmask = mask; TIC(d_time) - real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); + real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); @@ -557,7 +557,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) #pragma omp parallel for reduction(max: max_radio) for(index_t i = 0; i < m_params.n_patches; ++i) - max_radio = max(max_radio, S(i, 3)); + max_radio = max(max_radio, S(i, 3)); A.eye(phi_basis->dim(), m_params.n_atoms); @@ -607,7 +607,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) for(index_t i = 0; i < m_params.n_patches; ++i) for(index_t j = 0; j < patches[i].xyz.n_cols; ++j) - point_cloud.push_back({ patches[i].xyz(0, j), + point_cloud.push_back({ patches[i].xyz(0, j), patches[i].xyz(1, j), patches[i].xyz(2, j) }); @@ -779,9 +779,9 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - string command = "../../harris3d/harris3d " + mesh->filename() + " " + f_feat + " " + tmp_file_path("example.prop"); + string command = "../../harris3d/harris3d " + mesh->filename() + " " + f_feat + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); - system(command.c_str()); + system(command.c_str()); gproshan_debug(created); inp.close(); inp.open(f_feat.c_str(), ifstream::in); @@ -818,7 +818,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) { index_t * toplevel = new index_t[n_vertices]; - #pragma omp for + #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) { index_t v = sample(s); diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index f7a93d2c..34f548c7 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -80,7 +80,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N { // it needs to return both vertices // it needs to filter repeated indexes. - // p should be the maximun + // p should be the maximun index_t a, b, i = 0; vertex min_he; @@ -93,7 +93,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N for_star(he, mesh, v) { a = mesh->vt(next(he)); //index of the next vertex index_t - b = mesh->vt(prev(he)); + b = mesh->vt(prev(he)); va = mesh->gt(a); vb = mesh->gt(b); vv = mesh->gt(v); @@ -107,7 +107,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N if(geo[a] < geo[v]) i = find(vertices.data(), vertices.size(), a); else - i = find(vertices.data(), vertices.size(), b); + i = find(vertices.data(), vertices.size(), b); tmp_angle = acos( (mesh->normal_he(he), N[i]) ); @@ -121,12 +121,12 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; proj_area_face = *(pav * pbv) / 2; - min_he = mesh->normal_he(he); + min_he = mesh->normal_he(he); added = true; } } } - //p = mesh->get_vertex(indexes[i]); + //p = mesh->get_vertex(indexes[i]); //p = p - c ; //p = p - ((p,n)*n); @@ -208,7 +208,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind for(index_t i=1; i < vertices.size(); ++i) { - p = mesh->get_vertex(indexes[i]); + p = mesh->get_vertex(indexes[i]); c = mesh->get_vertex(v); // central vertices p = p - c ; @@ -338,7 +338,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & xyz(1, j) = v.y; xyz(2, j) = v.z; //p idx patche where belongs to - //j: local index + //j: local index //i: global index //if(vpatches[vertices[i]].size() == 0) //vpatches[vertices[i]].push_back({p, j++}); @@ -350,9 +350,9 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & } -double area_tri(double x1, double y1, double x2, double y2, double x3, double y3) -{ - return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); +double area_tri(double x1, double y1, double x2, double y2, double x3, double y3) +{ + return abs((x1*(y2-y3) + x2*(y3-y1)+ x3*(y1-y2))/2.0); } void patch::remove_extra_xyz_disjoint(size_t & max_points) @@ -390,7 +390,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co a_vec np = { r * cos(a), r * sin(a), 0 }; //gproshan_debug_var(np); - // find the closest point + // find the closest point index_t min_v; double min_d = INFINITY; for(index_t v: vertices) @@ -404,7 +404,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co min_v = v; } } - + // forstar to find closest trinagle a_mat abc(3,3); for_star(he, mesh, min_v) @@ -488,7 +488,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorgt(vi); xyz(0, i) = v.x; xyz(1, i) = v.y; @@ -525,7 +525,7 @@ void patch::save(const real_t & radio, const size_t & imsize, CImgList & CImg img(imsize, imsize); size_t x, y; img.fill(0); - // for each x y plus 1, multiply by delta and floor, get i and j + // for each x y plus 1, multiply by delta and floor, get i and j for(index_t i = 0; i < vertices.size(); ++i) { x = floor((xyz.col(i)[0] + radio) * (imsize - 1) / (2 * radio)); @@ -780,7 +780,7 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons { avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; } - else + else { avg_dist = distances[n_elem/2]; } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 085fb05f..3a9c67eb 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1420,7 +1420,7 @@ void che::update_evt_ot_et() } } - // non manifold two disk + // non manifold two disk //for(index_t he = 0; he < n_half_edges; ++he) // if(OT[he] != NIL) assert(he == OT[OT[he]]); diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 66303034..17811579 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -595,8 +595,8 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c //debug(E) - //debug(E * normalise(orientation)) - //debug(dot(E.col(2), orientation)) + //debug(E * normalise(orientation)) + //debug(dot(E.col(2), orientation)) E.col(2) = normalise(dot(orientation, E.col(2)) * E.col(2)); E.col(1) = normalise(cross(E.col(2), E.col(0))); //debug(E) diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 827f1e7b..4bef3997 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -78,7 +78,7 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, //row major glm::vec4 & color = img[j * width + i]; - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, (float(j) + randf(gen)) / height ); @@ -118,7 +118,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, for(index_t s = 0; s < samples; ++s) { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, (float(j) + randf(gen)) / windows_size.y ); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index aba3f6ec..82cc24eb 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -110,7 +110,7 @@ bool embree::occluded(ray_hit & r) void embree::build_bvh(const std::vector & meshes, const bool & pointcloud) { for(auto & m: meshes) - if(!m->n_faces || pointcloud) + if(!m->n_faces || pointcloud) geomID_mesh[add_pointcloud(m)] = {m, true}; else geomID_mesh[add_mesh(m)] = {m, false}; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index dffa9e1c..5f56540f 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -195,8 +195,8 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; - optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); - optix_mesh.triangleArray.numVertices = mesh->n_vertices; + optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); + optix_mesh.triangleArray.numVertices = mesh->n_vertices; optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; @@ -208,9 +208,9 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.flags = &optix_trig_flags; optix_mesh.triangleArray.numSbtRecords = 1; - optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; - optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; - optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; + optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; + optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; + optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; } glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat) diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 31f8ec44..352d578c 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -2,7 +2,7 @@ #include -extern "C" __constant__ void * optix_launch_params; +extern "C" __constant__ void * optix_launch_params; extern "C" __global__ void closest_hit() { diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 8640e99d..c29ddd2f 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -56,7 +56,7 @@ void camera::mouse(int , int state, int x, int y, int w, int h) } else { - momentum = 1.; + momentum = 1.; } r_last = p_drag * p_click.conj() * r_last; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index f75fbaa2..8eb9e5a4 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -67,7 +67,7 @@ void frame::display(const int & width, const int & height, void * buffer) glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, 6); - glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 50386c07..2d0a88ed 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -40,7 +40,7 @@ namespace gproshan { const int viewer::m_window_size[N_MESHES + 1][2] = {{1, 1}, - {1, 1}, {1, 2}, {1, 3}, + {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {2, 3}, {2, 4}, {2, 4}, {2, 5}, {2, 5}, {3, 4}, {3, 4} @@ -92,12 +92,12 @@ bool viewer::run() proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); - view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), - glm::vec3(center[1], center[2], center[3]), + view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), + glm::vec3(center[1], center[2], center[3]), glm::vec3(up[1], up[2], up[3]) ); - // RENDER + // RENDER glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { @@ -696,7 +696,7 @@ bool viewer::raycasting(viewer * view) float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), view->view_mat, view->proj_mat - ); + ); std::thread([](CImg img) { @@ -781,7 +781,7 @@ void viewer::render_embree() } rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, + view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, render_flat, action ); action = false; From 271b1fd4dd6b411a8af8293826409389aea3a9d2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 14:49:51 +0100 Subject: [PATCH 0459/1018] rename che free and alloc methods --- include/config.h | 7 ++---- include/mesh/che.h | 8 +++---- src/mesh/che.cpp | 55 ++++++++++++++++++++++---------------------- src/mesh/che_img.cpp | 2 +- src/mesh/che_off.cpp | 6 ++--- src/mesh/che_ply.cpp | 2 +- src/mesh/che_ptx.cpp | 2 +- 7 files changed, 39 insertions(+), 43 deletions(-) diff --git a/include/config.h b/include/config.h index a0133ae5..9db7cafa 100644 --- a/include/config.h +++ b/include/config.h @@ -1,11 +1,8 @@ #ifndef CONFIG_H #define CONFIG_H -// uncomment this line to disabled debug macros -//#define NDEBUG - -// uncomment this line to compile gproshan with single precision (float) -//#define SINGLE_P +// define to compile gproshan with single precision (float) +#define SINGLE_P // print log messages #define LOG diff --git a/include/mesh/che.h b/include/mesh/che.h index 3a5c25d4..6892f98d 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -37,10 +37,10 @@ class che const size_t n_half_edges = 0; const size_t n_edges = 0; const size_t n_borders = 0; + + std::string filename; ///< get and set data member protected: - std::string filename_; - vertex * GT = nullptr; ///< geometry table : v -> vertex index_t * VT = nullptr; ///< vertex table (faces) : he -> v index_t * OT = nullptr; ///< opposite table : he -> he @@ -129,10 +129,10 @@ class che corr_t find_corr(const vertex & v, const vertex & n, const std::vector & triangles); protected: - void delete_me(); void init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); void init(const std::string & file); - void init(const size_t & n_v, const size_t & n_f); + void free(); + void alloc(const size_t & n_v, const size_t & n_f); virtual void read_file(const std::string & file); private: diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 3a9c67eb..67afd3d6 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -92,7 +92,7 @@ che::che(const che & mesh) che::che(const size_t & n_v, const size_t & n_f) { - init(n_v, n_f); + alloc(n_v, n_f); } che::che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) @@ -102,7 +102,7 @@ che::che(const vertex * vertices, const index_t & n_v, const index_t * faces, co che::~che() { - delete_me(); + free(); } void che::star(star_t & s, const index_t & v) @@ -628,7 +628,7 @@ const string che::name_size() const void che::reload() { - delete_me(); + free(); init(filename_); } @@ -747,7 +747,7 @@ void che::multiplicate_vertices() aEHT[ahe + 8] = aEHT[ahe + 4] = n_edges + he + 2; } - delete_me(); + free(); GT = aGT; VT = aVT; OT = aOT; @@ -873,7 +873,7 @@ void che::remove_non_manifold_vertices() gproshan_debug_var(new_vertices.size()); gproshan_debug_var(new_faces.size()); gproshan_debug(removing vertex); - delete_me(); + free(); gproshan_debug(removing vertex); init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); gproshan_debug(removing vertex); @@ -941,7 +941,7 @@ void che::remove_vertices(const vector & vertices) gproshan_debug_var(new_vertices.size()); gproshan_debug_var(new_faces.size()); gproshan_debug(removing vertex); - delete_me(); + free(); gproshan_debug(removing vertex); init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); gproshan_debug(removing vertex); @@ -1034,7 +1034,7 @@ gproshan_debug(fill_holes); assert(ae == ne); delete [] common_edge; gproshan_debug(fill_holes); - delete_me(); + free(); gproshan_debug(fill_holes); GT = aGT; @@ -1232,7 +1232,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con for(index_t v = 0; v < n_vertices; ++v) corr[v].t = trig(map_he[corr[v].t * che::mtrig]); - delete_me(); + free(); init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); delete [] faces_fixed; @@ -1340,7 +1340,7 @@ corr_t che::find_corr(const vertex & v, const vertex & n, const vector void che::init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) { - init(n_v, n_f); + alloc(n_v, n_f); memcpy(GT, vertices, n_vertices * sizeof(vertex)); memcpy(VT, faces, n_half_edges * sizeof(index_t)); @@ -1354,12 +1354,13 @@ void che::init(const string & file) { filename_ = file; read_file(filename_); + update_evt_ot_et(); update_eht(); update_bt(); } -void che::init(const size_t & n_v, const size_t & n_f) +void che::alloc(const size_t & n_v, const size_t & n_f) { rw(n_vertices) = n_v; rw(n_faces) = n_f; @@ -1382,6 +1383,22 @@ void che::init(const size_t & n_v, const size_t & n_f) VC[v] = vcolor; } +void che::free() +{ + delete [] GT; GT = nullptr; + delete [] VT; VT = nullptr; + delete [] OT; OT = nullptr; + delete [] EVT; EVT = nullptr; + delete [] ET; ET = nullptr; + delete [] EHT; EHT = nullptr; + delete [] BT; BT = nullptr; + delete [] VN; VN = nullptr; + delete [] VC; VC = nullptr; + delete [] VHC; VHC = nullptr; +} + +void che::read_file(const string & ) {} // empty + void che::update_evt_ot_et() { memset(EVT, -1, sizeof(index_t) * n_vertices); @@ -1491,24 +1508,6 @@ void che::update_bt() delete [] border; } -void che::delete_me() -{ - delete [] GT; GT = nullptr; - delete [] VT; VT = nullptr; - delete [] OT; OT = nullptr; - delete [] EVT; EVT = nullptr; - delete [] ET; ET = nullptr; - delete [] EHT; EHT = nullptr; - delete [] BT; BT = nullptr; - delete [] VN; VN = nullptr; - delete [] VC; VC = nullptr; - delete [] VHC; VHC = nullptr; -} - -void che::read_file(const string & ) -{ -} - } // namespace gproshan diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index 9ad41ac3..e44f997e 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -33,7 +33,7 @@ void che_img::read_file(const string & file) { CImg img(file.c_str()); - init(img.height() * img.width(), 2 * (img.height() - 1) * (img.width() - 1)); + alloc(img.height() * img.width(), 2 * (img.height() - 1) * (img.width() - 1)); index_t v = 0, he = 0; for(int i = 0; i < img.width(); ++i) diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 12cb0c1d..6dbbc74e 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -36,7 +36,7 @@ void che_off::read_file(const string & file) fgets(soff, sizeof(soff), fp); fscanf(fp, "%lu %lu %lu", &nv, &nf, &ne); - init(nv, nf); + alloc(nv, nf); float x, y, z, r, g, b, a; for(index_t v = 0; v < n_vertices; ++v) @@ -72,8 +72,8 @@ void che_off::read_file(const string & file) { vertex * tGT = GT; GT = nullptr; - delete_me(); - init(nv, nf * (ne - che::mtrig + 1)); + free(); + alloc(nv, nf * (ne - che::mtrig + 1)); GT = tGT; } diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 8cf945e7..7ac6f274 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -85,7 +85,7 @@ void che_ply::read_file(const string & file) if(str == "format") ss >> format; } - init(n_v, n_f); + alloc(n_v, n_f); if(format == "ascii") { diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 43e60abd..182a6975 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -44,7 +44,7 @@ void che_ptx::read_file(const string & file) fscanf(fp, "%f", tr + i); - init(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); + alloc(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); float x, y, z, a, r, g, b; char line[128]; From 4c5b7a77e902d292a61b04a5e9465fd693787ad6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 15:47:53 +0100 Subject: [PATCH 0460/1018] che: rename class member filename --- include/mesh/che.h | 4 +--- src/geodesics/sampling.cpp | 2 +- src/mdict/msparse_coding.cpp | 2 +- src/mesh/che.cpp | 28 +++++++++------------------- src/mesh/che_sphere.cpp | 2 +- src/viewer/che_viewer.cpp | 2 +- src/viewer/viewer.cpp | 4 ++-- 7 files changed, 16 insertions(+), 28 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index 6892f98d..e0012bbe 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -56,9 +56,9 @@ class che bool manifold = true; public: + che(const che & mesh); che(const size_t & n_v = 0, const size_t & n_f = 0); che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); - che(const che & mesh); virtual ~che(); void star(star_t & s, const index_t & v); @@ -112,8 +112,6 @@ class che size_t max_degree() const; vertex & get_vertex(index_t v); void set_vertices(const vertex *const& positions, size_t n = 0, const index_t & v_i = 0); - void set_filename(const std::string & f); - const std::string & filename() const; const std::string filename_size() const; const std::string name() const; const std::string name_size() const; diff --git a/src/geodesics/sampling.cpp b/src/geodesics/sampling.cpp index 773403bc..dc4993dd 100644 --- a/src/geodesics/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -40,7 +40,7 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n bool load_sampling(vector & points, real_t & radio, che * mesh, size_t n) { - const string & filename = mesh->filename(); + const string & filename = mesh->filename; string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + to_string(n); diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index cdc8b914..6dd7eb98 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -779,7 +779,7 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - string command = "../../harris3d/harris3d " + mesh->filename() + " " + f_feat + " " + tmp_file_path("example.prop"); + string command = "../../harris3d/harris3d " + mesh->filename + " " + f_feat + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); system(command.c_str()); gproshan_debug(created); diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 67afd3d6..57641ed3 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -52,7 +52,7 @@ CHE::CHE(che * mesh) che::che(const che & mesh) { - filename_ = mesh.filename_; + filename = mesh.filename; rw(n_vertices) = mesh.n_vertices; rw(n_faces) = mesh.n_faces; rw(n_half_edges) = mesh.n_half_edges; @@ -438,7 +438,7 @@ real_t che::mean_edge() const size_t che::memory() const { - return sizeof(*this) + n_vertices * (sizeof(vertex) + sizeof(index_t)) + filename_.size() + return sizeof(*this) + n_vertices * (sizeof(vertex) + sizeof(index_t)) + filename.size() + sizeof(index_t) * (3 * n_half_edges + n_edges + n_borders); } @@ -599,26 +599,16 @@ void che::set_vertices(const vertex *const& positions, size_t n, const index_t & memcpy(GT + v_i, positions, sizeof(vertex) * n); } -const string & che::filename() const -{ - return filename_; -} - const string che::filename_size() const { - return filename_ + "_" + to_string(n_vertices); -} - -void che::set_filename(const string & f) -{ - filename_ = f; + return filename + "_" + to_string(n_vertices); } const string che::name() const { - index_t p = filename_.find_last_of('/'); - index_t q = filename_.find_last_of('.'); - return filename_.substr(p + 1, q - p - 1); + index_t p = filename.find_last_of('/'); + index_t q = filename.find_last_of('.'); + return filename.substr(p + 1, q - p - 1); } const string che::name_size() const @@ -629,7 +619,7 @@ const string che::name_size() const void che::reload() { free(); - init(filename_); + init(filename); } void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector & limits, const vector & sources, const index_t & k) @@ -1352,8 +1342,8 @@ void che::init(const vertex * vertices, const index_t & n_v, const index_t * fac void che::init(const string & file) { - filename_ = file; - read_file(filename_); + filename = file; + read_file(filename); update_evt_ot_et(); update_eht(); diff --git a/src/mesh/che_sphere.cpp b/src/mesh/che_sphere.cpp index 1bd7e4e0..5a6997d1 100644 --- a/src/mesh/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -15,7 +15,7 @@ namespace gproshan { che_sphere::che_sphere(const real_t & r, const size_t & n) { - filename_ = "sphere"; + filename = "sphere"; std::vector vertices; std::vector faces; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 6b035668..72a85e8a 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -189,7 +189,7 @@ void che_viewer::log_info() { if(!mesh) return; - gproshan_log_var(mesh->filename()); + gproshan_log_var(mesh->filename); gproshan_log_var(mesh->n_vertices); gproshan_log_var(mesh->n_faces); gproshan_log_var(mesh->n_half_edges); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2d0a88ed..dc4bea97 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -119,7 +119,7 @@ bool viewer::run() { idx_active_mesh = i; sphere_translations.clear(); - glfwSetWindowTitle(window, active_mesh()->filename().c_str()); + glfwSetWindowTitle(window, active_mesh()->filename.c_str()); } ImGui::EndMenu(); @@ -336,7 +336,7 @@ void viewer::add_mesh(che * p_mesh) ++n_meshes; idx_active_mesh = n_meshes - 1; - glfwSetWindowTitle(window, active_mesh()->filename().c_str()); + glfwSetWindowTitle(window, active_mesh()->filename.c_str()); const int & rows = m_window_size[n_meshes][0]; const int & cols = m_window_size[n_meshes][1]; From 27399e9e19f408ac00f1821b524bef124911ac69 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 18:10:09 +0100 Subject: [PATCH 0461/1018] che: update OT, oppositive half edge table --- include/mesh/che.h | 4 +-- src/mesh/che.cpp | 63 +++++++++++++++++++++------------------------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index e0012bbe..b17341d8 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -36,7 +36,6 @@ class che const size_t n_faces = 0; const size_t n_half_edges = 0; const size_t n_edges = 0; - const size_t n_borders = 0; std::string filename; ///< get and set data member @@ -47,11 +46,12 @@ class che index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * ET = nullptr; ///< edge table : e -> he index_t * EHT = nullptr; ///< extra half edge table : he -> e - index_t * BT = nullptr; ///< boundary table : b -> v vertex * VN = nullptr; ///< vertex normals : v -> normal(v) vertex * VC = nullptr; ///< vertex color : v -> color(v) real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) + + std::vector bounds; bool manifold = true; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 57641ed3..83993f42 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -3,10 +3,10 @@ #include "mesh/kdtree.h" #include "include_arma.h" +#include #include #include -#include -#include +#include using namespace std; @@ -1355,14 +1355,15 @@ void che::alloc(const size_t & n_v, const size_t & n_f) rw(n_vertices) = n_v; rw(n_faces) = n_f; rw(n_half_edges) = che::mtrig * n_faces; - rw(n_edges) = 0; + rw(n_edges) = n_half_edges; // max number of edges rw(n_borders) = 0; if(n_vertices) GT = new vertex[n_vertices]; if(n_half_edges) VT = new index_t[n_half_edges]; if(n_half_edges) OT = new index_t[n_half_edges]; if(n_vertices) EVT = new index_t[n_vertices]; - if(n_vertices) EHT = new index_t[n_half_edges]; + if(n_half_edges) ET = new index_t[n_half_edges]; + if(n_half_edges) EHT = new index_t[n_half_edges]; if(n_vertices) VN = new vertex[n_vertices]; if(n_vertices) VC = new vertex[n_vertices]; @@ -1391,52 +1392,44 @@ void che::read_file(const string & ) {} // empty void che::update_evt_ot_et() { - memset(EVT, -1, sizeof(index_t) * n_vertices); - if(!n_faces) return; - - vector * he_p_vertex = new vector[n_vertices]; - - //vertex table + + memset(EVT, -1, sizeof(index_t) * n_vertices); + + map * ot_he = new map[n_vertices]; for(index_t he = 0; he < n_half_edges; ++he) { - EVT[VT[he]] = he; - he_p_vertex[VT[he]].push_back(he); - } + const index_t & u = VT[he]; + const index_t & v = VT[next(he)]; - //opposite table - edge table - memset(OT, -1, sizeof(index_t) * n_half_edges); - - vector et; + EVT[u] = he; + ot_he[u][v] = he + 1; + } + + size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) { + const index_t & u = VT[he]; + const index_t & v = VT[next(he)]; + if(OT[he] == NIL) { - et.push_back(he); - for(index_t h: he_p_vertex[VT[he]]) - { - if(VT[prev(h)] == VT[next(he)]) - { - if(OT[he] == NIL && OT[prev(h)] == NIL) - { - OT[he] = prev(h); - OT[prev(h)] = he; - } - } - } + ET[ne++] = he; + + OT[he] = ot_he[v][u] - 1; + OT[OT[he]] = he; } } + delete [] ot_he; + + rw(n_edges) = ne; + + // non manifold two disk //for(index_t he = 0; he < n_half_edges; ++he) // if(OT[he] != NIL) assert(he == OT[OT[he]]); - //edge table - rw(n_edges) = et.size(); - ET = new index_t[n_edges]; - memcpy(ET, et.data(), sizeof(index_t) * n_edges); - - for(index_t he = 0; he < n_half_edges; ++he) if(OT[he] == NIL && EVT[VT[he]] != NIL) { From 75a44ca69a670b65a6992bd5c0dcd23b053fe8cb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 21:08:15 +0100 Subject: [PATCH 0462/1018] che: updating boundary methods --- include/mesh/che.h | 16 +++--- src/app_viewer.cpp | 2 +- src/mesh/che.cpp | 114 +++++++++++++++---------------------- src/mesh/che_fill_hole.cpp | 12 ++-- src/viewer/che_viewer.cpp | 2 +- src/viewer/viewer.cpp | 12 ++-- 6 files changed, 72 insertions(+), 86 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index b17341d8..a2098de8 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -8,7 +8,7 @@ #include #define for_star(he, mesh, v) for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->ot(prev(he))) != stop ? he : NIL) -#define for_border(he, mesh, v) for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->vt(next(he)))) != stop ? he : NIL) +#define for_boundary(he, mesh, v) for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->vt(next(he)))) != stop ? he : NIL) // geometry processing and shape analysis framework @@ -51,8 +51,6 @@ class che vertex * VC = nullptr; ///< vertex color : v -> color(v) real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) - std::vector bounds; - bool manifold = true; public: @@ -61,11 +59,12 @@ class che che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); virtual ~che(); - void star(star_t & s, const index_t & v); - void link(link_t & l, const index_t & v); - void border(std::vector & border, const index_t & b); - bool is_border_v(const index_t & v) const; - bool is_border_e(const index_t & e) const; + void star(star_t & s, const index_t & v) const; + void link(link_t & l, const index_t & v) const; + std::vector bounds() const; + std::vector boundary(const index_t & v) const; + bool is_vertex_bound(const index_t & v) const; + bool is_edge_bound(const index_t & e) const; void flip(const index_t & e); real_t pdetriq(const index_t & t) const; real_t quality(); @@ -136,7 +135,6 @@ class che private: void update_evt_ot_et(); void update_eht(); - void update_bt(); friend struct CHE; }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index d651bcec..dbac8e53 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -784,7 +784,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) che_viewer & mesh = view->active_mesh(); size_t old_n_vertices, n_vertices = mesh->n_vertices; - size_t n_holes = mesh->n_borders; + size_t n_holes = 0; // FIX_BOUND mesh->n_borders; vector * border_vertices; che ** holes; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 83993f42..094e5cbc 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -57,7 +57,6 @@ che::che(const che & mesh) rw(n_faces) = mesh.n_faces; rw(n_half_edges) = mesh.n_half_edges; rw(n_edges) = mesh.n_edges; - rw(n_borders) = mesh.n_borders; GT = new vertex[n_vertices]; memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); @@ -77,9 +76,6 @@ che::che(const che & mesh) EHT = new index_t[n_half_edges]; memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); - BT = new index_t[n_borders]; - memcpy(BT, mesh.BT, n_borders * sizeof(index_t)); - VN = new vertex[n_vertices]; memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); @@ -105,7 +101,7 @@ che::~che() free(); } -void che::star(star_t & s, const index_t & v) +void che::star(star_t & s, const index_t & v) const { if(v >= n_vertices) return; @@ -113,7 +109,7 @@ void che::star(star_t & s, const index_t & v) s.push_back(he); } -void che::link(link_t & l, const index_t & v) +void che::link(link_t & l, const index_t & v) const { if(v >= n_vertices) return; @@ -125,19 +121,49 @@ void che::link(link_t & l, const index_t & v) } } -void che::border(vector & border, const index_t & b) +///< return a vector of indices of one vertex per boundary +vector che::bounds() const { - for_border(he, this, BT[b]) - border.push_back(VT[he]); + if(!n_faces) return {}; + if(!manifold) return {}; + + vector vbounds; + + bool * is_bound = new bool[n_vertices]; + memset(is_bound, 0, sizeof(bool) * n_vertices); + + for(index_t v = 0; v < n_vertices; ++v) + if(!is_bound[v] && is_vertex_bound(v)) + { + vbounds.push_back(v); + + for_boundary(he, this, v) + is_bound[VT[he]] = true; + } + + delete [] is_bound; + + return vbounds; } -bool che::is_border_v(const index_t & v) const +///< return a vector of the indices of the boundary where v belongs +vector che::boundary(const index_t & v) const { - assert(EVT[v] < n_half_edges); - return OT[EVT[v]] == NIL; + vector vbound; + + for_boundary(he, this, v) + vbound.push_back(VT[he]); + + return vbound; +} + +bool che::is_vertex_bound(const index_t & v) const +{ + assert(v < n_vertices && EVT[v] < n_half_edges); + return EVT[v] != NIL && OT[EVT[v]] == NIL; } -bool che::is_border_e(const index_t & e) const +bool che::is_edge_bound(const index_t & e) const { return OT[ET[e]] == NIL; } @@ -439,7 +465,7 @@ real_t che::mean_edge() const size_t che::memory() const { return sizeof(*this) + n_vertices * (sizeof(vertex) + sizeof(index_t)) + filename.size() - + sizeof(index_t) * (3 * n_half_edges + n_edges + n_borders); + + sizeof(index_t) * (3 * n_half_edges + n_edges); } size_t che::genus() const @@ -566,11 +592,6 @@ const index_t & che::evt(const index_t & v) const return EVT[v]; } -const index_t & che::bt(const index_t & b) const -{ - return BT[b]; -} - size_t che::max_degree() const { size_t d, md = 0; @@ -580,7 +601,7 @@ size_t che::max_degree() const { d = 0; for_star(he, this, v) ++d; - d += is_border_v(v); + d += is_vertex_bound(v); md = max(md, d); } @@ -806,8 +827,6 @@ void che::multiplicate_vertices() else flip(e); rw(n_faces) = n_half_edges / che::mtrig; - - update_bt(); } void che::remove_non_manifold_vertices() @@ -1040,7 +1059,6 @@ gproshan_debug(fill_holes); rw(n_edges) = ne; update_eht(); - update_bt(); } void che::set_head_vertices(index_t * head, const size_t & n) @@ -1064,11 +1082,6 @@ void che::set_head_vertices(index_t * head, const size_t & n) VT[he] = v; swap(EVT[v], EVT[i]); - for(index_t b = 0; b < n_borders; ++b) - { - if(BT[b] == i) BT[b] = v; - else if(BT[b] == v) BT[b] = i; - } } } @@ -1132,7 +1145,7 @@ corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *con //is_border_v(va) && is_border_v(vb) -> is_border_e(e_d) if( !faces_fixed[trig(he_d)] && (ohe_d != NIL ? !faces_fixed[trig(ohe_d)] : true) && - (!(is_border_v(va) && is_border_v(vb)) || is_border_e(e_d)) ) + (!(is_vertex_bound(va) && is_vertex_bound(vb)) || is_edge_bound(e_d)) ) { is_collapse = link_intersect(va, vb) == (1 + (ohe_d != NIL)); @@ -1337,7 +1350,6 @@ void che::init(const vertex * vertices, const index_t & n_v, const index_t * fac update_evt_ot_et(); update_eht(); - update_bt(); } void che::init(const string & file) @@ -1347,7 +1359,6 @@ void che::init(const string & file) update_evt_ot_et(); update_eht(); - update_bt(); } void che::alloc(const size_t & n_v, const size_t & n_f) @@ -1356,7 +1367,6 @@ void che::alloc(const size_t & n_v, const size_t & n_f) rw(n_faces) = n_f; rw(n_half_edges) = che::mtrig * n_faces; rw(n_edges) = n_half_edges; // max number of edges - rw(n_borders) = 0; if(n_vertices) GT = new vertex[n_vertices]; if(n_half_edges) VT = new index_t[n_half_edges]; @@ -1382,20 +1392,20 @@ void che::free() delete [] EVT; EVT = nullptr; delete [] ET; ET = nullptr; delete [] EHT; EHT = nullptr; - delete [] BT; BT = nullptr; delete [] VN; VN = nullptr; delete [] VC; VC = nullptr; delete [] VHC; VHC = nullptr; } -void che::read_file(const string & ) {} // empty +void che::read_file(const string & ) {} /* virtual */ void che::update_evt_ot_et() { if(!n_faces) return; memset(EVT, -1, sizeof(index_t) * n_vertices); - + memset(OT, -1, sizeof(index_t) * n_half_edges); + map * ot_he = new map[n_vertices]; for(index_t he = 0; he < n_half_edges; ++he) { @@ -1417,7 +1427,8 @@ void che::update_evt_ot_et() ET[ne++] = he; OT[he] = ot_he[v][u] - 1; - OT[OT[he]] = he; + if(OT[he] != NIL) + OT[OT[he]] = he; } } @@ -1447,8 +1458,6 @@ void che::update_evt_ot_et() // gproshan_debug_var(EVT[v]); // assert(EVT[v] < n_half_edges); // } - - delete [] he_p_vertex; } void che::update_eht() @@ -1462,35 +1471,6 @@ void che::update_eht() } } -void che::update_bt() -{ - if(!n_faces) return; - if(!manifold) return; - - bool * border = new bool[n_vertices]; - memset(border, 0, sizeof(bool) * n_vertices); - - vector borders; - - for(index_t v = 0; v < n_vertices; ++v) - if(!border[v] && EVT[v] != NIL && OT[EVT[v]] == NIL) - { - borders.push_back(v); - for_border(he, this, v) - border[VT[he]] = true; - } - - rw(n_borders) = borders.size(); - if(n_borders) - { - BT = new index_t[n_borders]; - memcpy(BT, borders.data(), sizeof(index_t) * n_borders); - } - else BT = nullptr; - - delete [] border; -} - } // namespace gproshan diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 17811579..5480b92f 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -265,8 +265,11 @@ vector * fill_all_holes(che * mesh, const size_t & max_iter) tie(border_vertices, holes) = fill_all_holes_meshes(mesh, max_iter); if(holes) { + // FIX_BOUND + /* for(index_t b = 0; b < mesh->n_borders; ++b) if(holes[b]) delete holes[b]; + */ } delete [] holes; return border_vertices; @@ -277,16 +280,18 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t vector * border_vertices = nullptr; che ** holes = nullptr; - const size_t n_borders = mesh->n_borders; + vector bounds = mesh->bounds(); + const size_t n_borders = bounds.size(); + if(!n_borders) return make_tuple(border_vertices, holes); border_vertices = new vector[n_borders]; holes = new che*[n_borders]; gproshan_debug(inpainting); - + for(index_t b = 0; b < n_borders; ++b) - mesh->border(border_vertices[b], b); + border_vertices[b] = mesh->boundary(bounds[b]); gproshan_debug(inpainting); for(index_t b = 0; b < n_borders; ++b) @@ -311,7 +316,6 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t } - gproshan_debug_var(mesh->n_borders); return make_tuple(border_vertices, holes); } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 72a85e8a..219e44f3 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -196,7 +196,7 @@ void che_viewer::log_info() gproshan_log_var(mesh->n_edges); gproshan_log_var(mesh->area_surface()); gproshan_log_var(mesh->is_manifold()); - gproshan_log_var(mesh->n_borders); + gproshan_log_var(mesh->bounds().size()); gproshan_log_var(mesh->memory() / 1E6); gproshan_log_var(mesh->quality()); gproshan_log_var(mesh->genus()); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index dc4bea97..711c8a48 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -861,10 +861,14 @@ void viewer::draw_selected_vertices(shader & program) void viewer::select_border_vertices() { - active_mesh().selected.clear(); - for(index_t b = 0; b < active_mesh()->n_borders; ++b) - for_border(he, active_mesh(), active_mesh()->bt(b)) - active_mesh().selected.push_back(active_mesh()->vt(he)); + che_viewer & mesh = active_mesh(); + + mesh.selected.clear(); + + vector bounds = mesh->bounds(); + for(const index_t & b: bounds) + for_boundary(he, mesh, b) + mesh.selected.push_back(mesh->vt(he)); } void viewer::pick_vertex(int x, int y) From 0a438f6a87a7aa4a1c8245d34f4c4f22e1880bec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 22:28:59 +0100 Subject: [PATCH 0463/1018] che: improving oppositive half edges computation --- .gitignore | 2 +- src/mesh/che.cpp | 30 +++++++++++++----------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 1af9b0f6..1b9d9ba8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -build/* +build*/* bin/* lib/* doc/* diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 094e5cbc..f5b51bcf 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1406,15 +1406,7 @@ void che::update_evt_ot_et() memset(EVT, -1, sizeof(index_t) * n_vertices); memset(OT, -1, sizeof(index_t) * n_half_edges); - map * ot_he = new map[n_vertices]; - for(index_t he = 0; he < n_half_edges; ++he) - { - const index_t & u = VT[he]; - const index_t & v = VT[next(he)]; - - EVT[u] = he; - ot_he[u][v] = he + 1; - } + map * medges = new map[n_vertices]; size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) @@ -1422,20 +1414,24 @@ void che::update_evt_ot_et() const index_t & u = VT[he]; const index_t & v = VT[next(he)]; - if(OT[he] == NIL) + EVT[u] = he; + + index_t & mhe = u < v ? medges[u][v] : medges[v][u]; + if(!mhe) { ET[ne++] = he; - - OT[he] = ot_he[v][u] - 1; - if(OT[he] != NIL) - OT[OT[he]] = he; + mhe = he + 1; + } + else + { + OT[he] = mhe - 1; + OT[OT[he]] = he; } } - delete [] ot_he; - - rw(n_edges) = ne; + rw(n_edges) = ne; + delete [] medges; // non manifold two disk //for(index_t he = 0; he < n_half_edges; ++he) From bb878367f9bc7884b8b0324bb21d644086c02dd4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 7 Mar 2021 22:43:52 +0100 Subject: [PATCH 0464/1018] cleaning up extra spaces tabs --- include/app_viewer.h | 6 +- include/features/descriptor.h | 4 +- include/features/key_components.h | 2 +- include/geodesics/geodesics.h | 8 +- include/geodesics/geodesics_ptp.h | 2 +- include/mdict/basis.h | 2 +- include/mdict/msparse_coding.h | 12 +- include/mdict/patch.h | 20 +-- include/mesh/che.h | 8 +- include/mesh/che_fill_hole.h | 2 +- include/mesh/che_off.h | 2 +- include/mesh/che_ptx.h | 2 +- include/mesh/quaternion.h | 2 +- include/mesh/vertex.h | 8 +- include/raytracing/raytracing.h | 6 +- include/raytracing/rt_embree.h | 8 +- include/raytracing/rt_embree_splat.h | 8 +- include/raytracing/rt_optix.h | 2 +- include/viewer/camera.h | 4 +- include/viewer/che_viewer.h | 2 +- include/viewer/frame.h | 2 +- include/viewer/viewer.h | 26 ++-- src/app_viewer.cpp | 124 +++++++-------- src/features/descriptor.cpp | 12 +- src/features/key_components.cpp | 8 +- src/features/key_points.cpp | 6 +- src/geodesics/geodesics.cpp | 8 +- src/geodesics/geodesics_ptp.cpp | 22 +-- src/geodesics/geodesics_ptp.cu | 18 +-- src/geodesics/geodesics_ptp_coalescence.cu | 18 +-- src/geodesics/heat_method.cpp | 36 ++--- src/geodesics/heat_method.cu | 54 +++---- src/geodesics/sampling.cpp | 4 +- src/geodesics/test_geodesics_ptp.cpp | 60 ++++---- src/geodesics/test_geodesics_ptp.cu | 24 +-- .../test_geodesics_ptp_coalescence.cu | 32 ++-- src/geometry/convex_hull.cpp | 4 +- src/laplacian/laplacian.cpp | 4 +- src/mdict/basis.cpp | 2 +- src/mdict/image_denoising.cpp | 20 +-- src/mdict/mdict.cpp | 44 +++--- src/mdict/msparse_coding.cpp | 144 +++++++++--------- src/mdict/patch.cpp | 122 +++++++-------- src/mesh/che.cpp | 66 ++++---- src/mesh/che_fill_hole.cpp | 4 +- src/mesh/che_img.cpp | 6 +- src/mesh/che_obj.cpp | 34 ++--- src/mesh/che_off.cpp | 12 +- src/mesh/che_ply.cpp | 24 +-- src/mesh/che_ptx.cpp | 18 +-- src/mesh/che_sphere.cpp | 16 +- src/mesh/kdtree.cpp | 2 +- src/raytracing/raytracing.cpp | 8 +- src/raytracing/rt_embree.cpp | 40 ++--- src/raytracing/rt_embree_splat.cpp | 18 +-- src/raytracing/rt_optix.cpp | 26 ++-- src/viewer/camera.cpp | 2 +- src/viewer/che_viewer.cpp | 24 +-- src/viewer/frame.cpp | 10 +- src/viewer/shader.cpp | 12 +- src/viewer/viewer.cpp | 130 ++++++++-------- 61 files changed, 678 insertions(+), 678 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index def5c845..cb752fe9 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -43,12 +43,12 @@ class app_viewer : public viewer public: app_viewer() = default; ~app_viewer(); - + int main(int nargs, const char ** args); - + protected: virtual void init(); - + che * load_mesh(const string & file_path); static bool process_fairing_taubin(viewer * p_view); diff --git a/include/features/descriptor.h b/include/features/descriptor.h index 245316b4..a419cdf3 100644 --- a/include/features/descriptor.h +++ b/include/features/descriptor.h @@ -13,13 +13,13 @@ class descriptor { public: enum signature { GPS, HKS, WKS }; - + private: a_sp_mat L, A; a_vec eigval; a_mat eigvec; a_mat features; - + public: descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); operator bool () const; ///< return true if the features were computed diff --git a/include/features/key_components.h b/include/features/key_components.h index 6a88057a..c70b103e 100644 --- a/include/features/key_components.h +++ b/include/features/key_components.h @@ -26,7 +26,7 @@ class key_components ~key_components(); index_t operator()(const index_t & i); operator const size_t & () const; - + private: void compute_kcs(che * mesh, const key_points & kps); index_t find(const index_t & x); diff --git a/include/geodesics/geodesics.h b/include/geodesics/geodesics.h index 2c63ad47..6379cad3 100644 --- a/include/geodesics/geodesics.h +++ b/include/geodesics/geodesics.h @@ -21,7 +21,7 @@ namespace gproshan { class geodesics { using fm_function_t = std::function; - + public: enum algorithm { FM, ///< Execute Fast Marching algorithm #ifdef GPROSHAN_CUDA @@ -31,7 +31,7 @@ class geodesics PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm HEAT_METHOD ///< Execute Heat Method - cholmod (CPU) }; - + struct params { algorithm alg = FM; ///< specific the algorithm to execute. @@ -50,7 +50,7 @@ class geodesics index_t * sorted_index; ///< Sort vertices by topological level or geodesic distance. size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; - + const size_t & n_vertices; ///< Number of vertices, const reference public: @@ -73,7 +73,7 @@ class geodesics void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_method(che * mesh, const std::vector & sources); - + #ifdef GPROSHAN_CUDA void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); void run_heat_method_gpu(che * mesh, const std::vector & sources); diff --git a/include/geodesics/geodesics_ptp.h b/include/geodesics/geodesics_ptp.h index 0015067a..a1ee79f7 100644 --- a/include/geodesics/geodesics_ptp.h +++ b/include/geodesics/geodesics_ptp.h @@ -19,7 +19,7 @@ struct ptp_out_t { real_t * dist; index_t * clusters; - + ptp_out_t(real_t *const & d, index_t *const & c = nullptr); }; diff --git a/include/mdict/basis.h b/include/mdict/basis.h index 5969cf43..523909f6 100644 --- a/include/mdict/basis.h +++ b/include/mdict/basis.h @@ -31,7 +31,7 @@ class basis void plot_basis(); void plot_atoms(const a_mat & A); void plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p); - + private: virtual void plot_basis(std::ostream & os) = 0; virtual void plot_atoms(std::ostream & os, const a_vec & A) = 0; diff --git a/include/mdict/msparse_coding.h b/include/mdict/msparse_coding.h index 98a335cc..67de6675 100644 --- a/include/mdict/msparse_coding.h +++ b/include/mdict/msparse_coding.h @@ -40,7 +40,7 @@ class msparse_coding a_mat A; ///< dictionary continuous matrix. a_mat alpha; ///< sparse coding matrix. - + real_t s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. @@ -49,15 +49,15 @@ class msparse_coding double d_time; ///< time of operations. real_t * dist; - + bool * mask = nullptr; std::string key_name; - + public: static size_t K; ///< number of iterations KSVD. static size_t L; ///< sparsity, norm L_0, default 10. static size_t T; ///< factor of patches' size, default 5 toplesets. - + public: msparse_coding( che *const & _mesh, ///< pointer to input mesh. basis *const &_phi_basis, ///< pointer to continuous basis. @@ -65,7 +65,7 @@ class msparse_coding ); virtual ~msparse_coding(); - + const real_t & operator[](const index_t & i) const; const index_t & draw_patches(const index_t & p); @@ -95,7 +95,7 @@ class msparse_coding void update_alphas(a_mat & alpha, size_t threshold); void save_alpha(string file); - index_t sample(const index_t & s); + index_t sample(const index_t & s); }; diff --git a/include/mdict/patch.h b/include/mdict/patch.h index d138220b..73105f01 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -45,7 +45,7 @@ class patch double avg_dist; ///< Average distance between points. real_t radio; ///< Radio. size_t min_nv; ///< - + public: static size_t expected_nv; ///< Expected number of patch vertices. static real_t nyquist_factor; ///< nyquist factor @@ -53,7 +53,7 @@ class patch public: patch() = default; ~patch() = default; - + void init( che * mesh, ///< input mesh. const index_t & v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. @@ -66,12 +66,12 @@ class patch const size_t & n_toplevels, vector & _vertices, index_t * _toplevel = nullptr); - + void init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, const index_t & v, - const real_t & delta, + const real_t & delta, const real_t & sum_thres, const real_t & area_thres, const real_t & area_mesh @@ -84,9 +84,9 @@ class patch const index_t & v); void transform(); - + void itransform(); - + void reset_xyz( che * mesh, std::vector & vpatches, const index_t & p, @@ -120,7 +120,7 @@ class patch real_t & proj_area, real_t deviation ); - + private: /// Gather the vertices needed to compute the jet_fit_directions of the patch. @@ -129,7 +129,7 @@ class patch const size_t & n_toplevels, index_t * toplevel ); - + /// Gather the vertices filter by radio in the local coordinates require initialize T and x. void gather_vertices( che * mesh, const index_t & v, @@ -137,7 +137,7 @@ class patch index_t * toplevel ); bool exists(index_t idx); - + /// Initialize transformation matrix T and translation vector x, using CGAL jet_fitting. void jet_fit_directions(che * mesh, const index_t & v @@ -150,7 +150,7 @@ class patch void save_z(ostream & os); index_t find(const index_t * indexes, size_t nc, index_t idx_global); - + friend class msparse_coding; }; diff --git a/include/mesh/che.h b/include/mesh/che.h index a2098de8..d0784643 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -36,7 +36,7 @@ class che const size_t n_faces = 0; const size_t n_half_edges = 0; const size_t n_edges = 0; - + std::string filename; ///< get and set data member protected: @@ -46,11 +46,11 @@ class che index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * ET = nullptr; ///< edge table : e -> he index_t * EHT = nullptr; ///< extra half edge table : he -> e - + vertex * VN = nullptr; ///< vertex normals : v -> normal(v) vertex * VC = nullptr; ///< vertex color : v -> color(v) real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) - + bool manifold = true; public: @@ -58,7 +58,7 @@ class che che(const size_t & n_v = 0, const size_t & n_f = 0); che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); virtual ~che(); - + void star(star_t & s, const index_t & v) const; void link(link_t & l, const index_t & v) const; std::vector bounds() const; diff --git a/include/mesh/che_fill_hole.h b/include/mesh/che_fill_hole.h index 49112c28..94c55382 100644 --- a/include/mesh/che_fill_hole.h +++ b/include/mesh/che_fill_hole.h @@ -45,7 +45,7 @@ struct border_t a_vec a = V[p_v] - V[v]; a_vec b = V[n_v] - V[v]; - + a(2) = b(2) = 0; a_vec r = div * a + (1 - div) * b; diff --git a/include/mesh/che_off.h b/include/mesh/che_off.h index 924b2572..5ea0e6cf 100644 --- a/include/mesh/che_off.h +++ b/include/mesh/che_off.h @@ -14,7 +14,7 @@ class che_off : public che che_off(const std::string & file); che_off(const che_off & mesh); virtual ~che_off(); - + enum type { OFF, NOFF, COFF, CNOFF }; static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool & pointcloud = false); diff --git a/include/mesh/che_ptx.h b/include/mesh/che_ptx.h index 9c49827e..8f6a1da8 100644 --- a/include/mesh/che_ptx.h +++ b/include/mesh/che_ptx.h @@ -14,7 +14,7 @@ class che_ptx : public che che_ptx(const std::string & file); che_ptx(const che_ptx & mesh); virtual ~che_ptx(); - + static void write_file(const che * mesh, const std::string & file); private: diff --git a/include/mesh/quaternion.h b/include/mesh/quaternion.h index e9ac3b67..850551b2 100644 --- a/include/mesh/quaternion.h +++ b/include/mesh/quaternion.h @@ -51,7 +51,7 @@ class quaternion real_t norm2() const; quaternion unit() const; void normalize(); - + friend std::ostream & operator << (std::ostream & os, const quaternion & q); friend std::istream & operator >> (std::istream & is, quaternion & q); }; diff --git a/include/mesh/vertex.h b/include/mesh/vertex.h index fcb97a30..e600fc0c 100644 --- a/include/mesh/vertex.h +++ b/include/mesh/vertex.h @@ -26,11 +26,11 @@ class vertex real_t & operator [] (const index_t & i); const real_t & operator [] (const index_t & i) const; - + vertex unit() const; real_t operator * () const; // norm real_t operator , (const vertex & v) const; // dot product - + vertex operator * (const vertex & v) const; // cross product vertex operator / (const real_t & v) const; // scalar division vertex operator + (const vertex & v) const; @@ -40,8 +40,8 @@ class vertex void operator *= (const real_t & v); // scalar produc void operator /= (const real_t & v); void operator += (const vertex & v); - void operator -= (const vertex & v); - + void operator -= (const vertex & v); + bool operator < (const vertex & v); bool operator == (const vertex & v); diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index 0e015b7b..c0cffdaf 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -33,7 +33,7 @@ class raytracing size_t width; size_t height; size_t n_samples; - + public: glm::vec4 * img; @@ -56,13 +56,13 @@ class raytracing const index_t & samples = 4 ); - + protected: virtual glm::vec4 intersect_li( const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat ) = 0; - + virtual float intersect_depth( const glm::vec3 & org, const glm::vec3 & dir ) = 0; }; diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index f40c3a2e..51d15a7f 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -34,9 +34,9 @@ class embree : public raytracing RTCDevice device; - RTCScene scene; + RTCScene scene; RTCIntersectContext intersect_context; - + public: static float pc_radius; @@ -44,7 +44,7 @@ class embree : public raytracing embree(); embree(const std::vector & meshes, const bool & pointcloud = false); virtual ~embree(); - + protected: bool intersect(ray_hit & r); bool occluded(ray_hit & r); @@ -58,7 +58,7 @@ class embree : public raytracing glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); - + glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); }; diff --git a/include/raytracing/rt_embree_splat.h b/include/raytracing/rt_embree_splat.h index c803f9b4..9991bc9d 100644 --- a/include/raytracing/rt_embree_splat.h +++ b/include/raytracing/rt_embree_splat.h @@ -52,23 +52,23 @@ class embree_splat : public embree color += w * C[i]; sum_w += w; } - + normal /= sum_w; color /= sum_w; return sum_w; } }; - + std::vector vsplat; public: embree_splat(const std::vector & meshes, const bool & pointcloud); - + private: index_t add_pointcloud(const che * mesh); float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); - + void init_splats(const che * mesh); }; diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index bb3a43b3..6c1cdf1b 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -33,7 +33,7 @@ class optix : public raytracing private: glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - + OptixTraversableHandle build_as(const std::vector & meshes); void add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/include/viewer/camera.h b/include/viewer/camera.h index 1fa4c323..2e961f7b 100644 --- a/include/viewer/camera.h +++ b/include/viewer/camera.h @@ -30,10 +30,10 @@ class camera void zoom_in(); void zoom_out(); quaternion current_rotation() const; - + private: quaternion click_to_sphere(int x, int y, int w, int h); - + friend std::ostream & operator << (std::ostream & os, const camera & cam); friend std::istream & operator >> (std::istream & is, camera & cam); }; diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index a7744157..31189b9a 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -31,7 +31,7 @@ class che_viewer GLuint vao; GLuint vbo[6]; - + public: int vx, vy; ///< viewport positions. real_t factor; diff --git a/include/viewer/frame.h b/include/viewer/frame.h index 849840c6..d6f3c267 100644 --- a/include/viewer/frame.h +++ b/include/viewer/frame.h @@ -16,7 +16,7 @@ class frame private: GLuint render_tex; GLuint vao, vbo; - + shader program; public: diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 4ef67426..339c9605 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -38,7 +38,7 @@ class viewer protected: using function_t = bool (*) (viewer *); - + struct process_t { std::string key; @@ -46,7 +46,7 @@ class viewer function_t function; index_t sub_menu; bool selected = false; - + process_t() = default; process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; }; @@ -64,13 +64,13 @@ class viewer shader shader_normals; shader shader_gradient; shader shader_pointcloud; - + camera cam; - + quaternion eye; quaternion center; quaternion up; - + quaternion light; glm::mat4 view_mat; @@ -82,14 +82,14 @@ class viewer enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; index_t render_opt = R_GL; - + frame * render_frame = nullptr; rt::raytracing * rt_embree = nullptr; rt::raytracing * rt_optix = nullptr; bool action = false; - + bool point_normals = true; unsigned int point_size = 1; bool render_pointcloud = false; @@ -107,7 +107,7 @@ class viewer che_viewer sphere; std::vector sphere_translations; shader shader_sphere; - + public: std::vector other_vertices; std::vector vectors; @@ -116,13 +116,13 @@ class viewer public: viewer(int width = 1600, int height = 900); virtual ~viewer(); - + bool run(); - + che_viewer & active_mesh(); void add_process(const int & key, const process_t & process); void add_mesh(che * p_mesh); - + private: void info_gl(); void init_gl(); @@ -165,13 +165,13 @@ class viewer static bool set_render_border(viewer * view); static bool set_render_lines(viewer * view); static bool set_render_flat(viewer * view); - + static bool raycasting(viewer * view); // draw routines void draw_meshes(shader & program, const bool & normals = false); void draw_selected_vertices(shader & program); - + void select_border_vertices(); void pick_vertex(int x, int y); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index dbac8e53..c37f0679 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -21,7 +21,7 @@ che * app_viewer::load_mesh(const string & file_path) { size_t pos = file_path.rfind('.'); assert(pos != string::npos); - + string extension = file_path.substr(pos + 1); if(extension == "off") return new che_off(file_path); @@ -44,12 +44,12 @@ int app_viewer::main(int nargs, const char ** args) for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); - + TOC(time) gproshan_log_var(sizeof(real_t)); gproshan_log_var(time); - + init(); run(); @@ -199,7 +199,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) fill_all_holes(mesh); paint_holes_vertices(p_view); - + return false; } @@ -221,7 +221,7 @@ bool app_viewer::process_noise(viewer * p_view) } mesh->update_normals(); - + return false; } @@ -243,7 +243,7 @@ bool app_viewer::process_black_noise(viewer * p_view) } mesh->update_normals(); - + return false; } @@ -255,7 +255,7 @@ bool app_viewer::process_threshold(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; - + return false; } @@ -265,7 +265,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) che_viewer & mesh = view->active_mesh(); static int K = 20; - + ImGui::InputInt("eigenvectors", &K); if(ImGui::Button("Run")) @@ -287,14 +287,14 @@ bool app_viewer::process_functional_maps(viewer * p_view) eigvec.col(k) -= eigvec.col(k).min(); eigvec.col(k) /= eigvec.col(k).max(); - + #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) view->active_mesh()->heatmap(v) = eigvec(v, k); view->active_mesh().update_vbo(); } - + view->idx_active_mesh = 0; } @@ -314,7 +314,7 @@ bool app_viewer::descriptor_heatmap(viewer * p_view, const descriptor::signature if(ImGui::Button("Run")) { descriptor features(sig, mesh, K); - + if(features) { status = true; @@ -357,7 +357,7 @@ bool app_viewer::process_key_points(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + key_points kps(mesh); mesh.selected.clear(); @@ -365,7 +365,7 @@ bool app_viewer::process_key_points(viewer * p_view) for(index_t i = 0; i < kps.size(); ++i) mesh.selected.push_back(kps[i]); - + return false; } @@ -374,16 +374,16 @@ bool app_viewer::process_key_components(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + key_points kps(mesh); key_components kcs(mesh, kps, .25); - + gproshan_debug_var(kcs); - + #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = (real_t) kcs(v) / kcs; - + return false; } @@ -392,7 +392,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + TIC(view->time) index_t * toplevel = new index_t[mesh->n_vertices]; size_t avg_nvp = 0; @@ -411,19 +411,19 @@ bool app_viewer::process_mdict_patch(viewer * p_view) vdir.z = p.T(0, 2); view->vectors.push_back(mesh->gt(v)); view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); - + vdir.x = p.T(1, 0); vdir.y = p.T(1, 1); vdir.z = p.T(1, 2); view->vectors.push_back(mesh->gt(v)); view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); - + vdir.x = p.T(2, 0); vdir.y = p.T(2, 1); vdir.z = p.T(2, 2); view->vectors.push_back(mesh->gt(v)); view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); - + view->vectors.push_back(mesh->gt(v)); view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * mesh->normal(v)); @@ -432,11 +432,11 @@ bool app_viewer::process_mdict_patch(viewer * p_view) avg_nvp /= mesh.selected.size(); gproshan_debug_var(avg_nvp); - + delete [] toplevel; TOC(view->time) gproshan_debug_var(view->time); - + return false; } @@ -447,13 +447,13 @@ bool app_viewer::process_msparse_coding(viewer * p_view) static msparse_coding::params params; static size_t n = 12; - + assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - + ImGui_InputReal("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); @@ -462,10 +462,10 @@ bool app_viewer::process_msparse_coding(viewer * p_view) { basis_dct phi(n); msparse_coding msc(mesh, &phi, params); - + real_t max_error = msc.execute(); gproshan_log_var(max_error); - + mesh->update_heatmap(&msc[0]); mesh->update_normals(); } @@ -482,10 +482,10 @@ bool app_viewer::process_mask(viewer * p_view) static size_t n = 12; assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); @@ -494,7 +494,7 @@ bool app_viewer::process_mask(viewer * p_view) { basis_dct phi(n); msparse_coding msc(mesh, &phi, params); - + msc.init_radial_feature_patches(); //dict.init_voronoi_patches(); mesh->update_heatmap(&msc[0]); @@ -504,10 +504,10 @@ bool app_viewer::process_mask(viewer * p_view) gproshan_debug_var(f_points); points_out.load(f_points); gproshan_debug_var(points_out.size()); - + for(index_t i = 0; i < points_out.size(); ++i) mesh.selected.push_back(points_out(i)); - + mesh->update_normals(); } @@ -522,17 +522,17 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static msparse_coding::params params; static size_t n = 12; static real_t percentage_size = 100; - static real_t radio_factor = 1; + static real_t radio_factor = 1; assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); - + ImGui_InputReal("percentage_size", &percentage_size, 100, 10, "%.3f"); ImGui_InputReal("radio_factor", &radio_factor, 1, 0.1, "%.3f"); @@ -540,10 +540,10 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) { basis_dct phi(n); msparse_coding msc(mesh, &phi, params); - + view->add_mesh(msc.point_cloud_reconstruction(percentage_size, radio_factor)); } - + return true; } @@ -557,7 +557,7 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) mesh.update(); mesh.log_info(); - + return false; } @@ -566,7 +566,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + if(!mesh.selected.size()) mesh.selected.push_back(0); @@ -588,7 +588,7 @@ bool app_viewer::compute_toplesets(viewer * p_view) delete [] toplesets; delete [] sorted; - + return false; } @@ -597,18 +597,18 @@ bool app_viewer::process_voronoi(viewer * p_view) gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + geodesics::params params; params.cluster = true; - + #ifdef GPROSHAN_CUDA params.alg = geodesics::PTP_GPU; #endif - + TIC(view->time) geodesics ptp(mesh, mesh.selected, params); TOC(view->time) - + gproshan_log_var(view->time); #pragma omp parallel for @@ -617,7 +617,7 @@ bool app_viewer::process_voronoi(viewer * p_view) mesh->heatmap(i) = ptp.clusters[i]; mesh->heatmap(i) /= mesh.selected.size() + 1; } - + return false; } @@ -642,7 +642,7 @@ bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) gproshan_log_var(radio); gproshan_log_var(mesh.selected.size()); gproshan_log_var(view->time); - + return false; } @@ -656,7 +656,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) ImGui::SliderInt("samples", &n, 1, mesh->n_vertices / 6); ImGui::Text("radio: %.3f", radio); - + if(ImGui::Button("Run")) { TIC(view->time) @@ -664,7 +664,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) TOC(view->time) gproshan_log_var(view->time); } - + return true; } @@ -672,10 +672,10 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + static int k = 100; ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices / 6); - + if(ImGui::Button("Run")) { fairing_spectral fair(k); @@ -695,12 +695,12 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) static float step = 0.001; //cin >> step; ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); - + if(ImGui::Button("Run")) { fairing_taubin fair(step); fair.run(mesh); - + mesh->set_vertices(fair.get_postions()); mesh->update_normals(); } @@ -715,7 +715,7 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & if(!mesh.selected.size()) mesh.selected.push_back(0); - + static vector dist; @@ -730,9 +730,9 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & geodesics G(mesh, mesh.selected, params); TOC(view->time) gproshan_log_var(view->time); - + mesh->update_heatmap(&G[0]); - + return false; } @@ -804,7 +804,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) delete [] holes; delete [] border_vertices; paint_holes_vertices(p_view); - + return false; } @@ -831,7 +831,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) } //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); gv(v) = mesh->mean_curvature(v); - + g_max = max(g_max, gv(v)); g_min = min(g_min, gv(v)); } @@ -861,7 +861,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = f(gv(v)); - + return false; } @@ -889,7 +889,7 @@ bool app_viewer::select_multiple(viewer * p_view) che_viewer & mesh = view->active_mesh(); static char line[128] = ""; - + ImGui::InputText("select", line, sizeof(line)); if(ImGui::Button("Add")) @@ -899,7 +899,7 @@ bool app_viewer::select_multiple(viewer * p_view) while(ss >> v) mesh.selected.push_back(v); } - + return true; } diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 53bb0060..d068d9a2 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -10,7 +10,7 @@ descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n { if(!eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs)) return; - + switch(sig) { case GPS: compute_gps(); break; @@ -39,9 +39,9 @@ void descriptor::compute_gps() void descriptor::compute_hks() { eigvec = eigvec % eigvec; // element wise product - + features.zeros(eigvec.n_rows, eigvec.n_cols); - + #pragma omp parallel for for(index_t t = 0; t < features.n_cols; ++t) features.col(t) = eigvec * exp(-eigval * t); @@ -49,16 +49,16 @@ void descriptor::compute_hks() ///< http://imagine.enpc.fr/~aubrym/projects/wks/index.html void descriptor::compute_wks() -{ +{ eigvec = eigvec % eigvec; // element wise product eigval = log(eigval); a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); real_t sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference real_t sigma_2 = 2 * sigma * sigma; - + features.zeros(eigvec.n_rows, e.n_elem); - + #pragma omp parallel for for(index_t t = 0; t < features.n_cols; ++t) features.col(t) = eigvec * exp(-pow(e(t) - eigval, 2) / sigma_2) / diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index e54758b8..a40ab398 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -24,7 +24,7 @@ key_components::key_components(che * mesh, const key_points & kps, const real_t comp[i] = i; comp_size[i] = 1; } - + n_comp = 0; compute_kcs(mesh, kps); } @@ -51,11 +51,11 @@ key_components::operator const size_t & () const void key_components::compute_kcs(che * mesh, const key_points & kps) { geodesics fm(mesh, vector(&kps[0], &kps[0] + kps.size())); - + radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) for_star(he, mesh, fm(i)) join(fm(i), mesh->vt(next(he))); - + for(index_t i = 0; i < n_vertices; ++i) if(comp[i] == i && comp_size[i] > 1) comp_idx[i] = n_comp++; @@ -74,7 +74,7 @@ bool key_components::join(index_t x, index_t y) y = find(y); if(x == y) return 0; - + comp_size[x] += comp_size[y]; comp[y] = x; diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index abce564a..51a56812 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -17,7 +17,7 @@ key_points::key_points(che * mesh, const real_t & percent) n_vertices = mesh->n_vertices; face_areas = new real_idx_t[n_faces]; - + kps = new index_t[n_vertices]; is_kp = new bool[n_vertices]; @@ -61,7 +61,7 @@ void key_points::compute_kps(che * mesh) } sort(face_areas, face_areas + n_faces); - + // compute kps memset(is_kp, 0, sizeof(bool) * n_vertices); @@ -80,7 +80,7 @@ void key_points::compute_kps(che * mesh) he = next(he); } } - + // compute kps memset(is_kp, 0, sizeof(bool) * n_vertices); diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 90eb6587..b27bca44 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -43,7 +43,7 @@ geodesics::~geodesics() const real_t & geodesics::operator[](const index_t & i) const { - + assert(i < n_vertices); return dist[i]; } @@ -153,7 +153,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co black_i = Q.top().second; color[black_i] = BLACK; Q.pop(); - + if(dist[black_i] > radio) break; sorted_index[n_sorted++] = black_i; @@ -179,7 +179,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co if(dp < dv) { dv = dp; - + if(clusters) clusters[v] = dist[mesh->vt(prev(v_he))] < dist[mesh->vt(next(he))] ? clusters[mesh->vt(prev(he))] : clusters[mesh->vt(next(he))]; } @@ -201,7 +201,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< mesh->compute_toplesets(toplesets, sorted_index, limits, sources); double time_ptp; - + TIC(time_ptp) parallel_toplesets_propagation_coalescence_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}); TOC(time_ptp) diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index 2a17bbf6..cfd1882a 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -16,11 +16,11 @@ ptp_out_t::ptp_out_t(real_t *const & d, index_t *const & c): dist(d), clusters(c che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets) { // sort data by levels, must be improve the coalescence - + vector V(toplesets.limits.back()); vector F; F.reserve(mesh->n_half_edges); - + inv = !inv ? new index_t[mesh->n_vertices] : inv; memset(inv, -1, sizeof(index_t) * mesh->n_vertices); @@ -34,7 +34,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top for(index_t he = 0; he < mesh->n_half_edges; ++he) if(inv[mesh->vt(he)] != NIL && inv[mesh->vt(prev(he))] != NIL && inv[mesh->vt(next(he))] != NIL) F.push_back(inv[mesh->vt(he)]); - + return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); } @@ -62,7 +62,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c index_t d = 0; index_t start, end, n_cond, count; index_t i = 1, j = 2; - + // maximum number of iterations index_t iter = 0; index_t max_iter = toplesets.limits.size() << 1; @@ -74,7 +74,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c start = toplesets.limits[i]; end = toplesets.limits[j]; n_cond = toplesets.limits[i + 1] - start; - + #pragma omp parallel for for(index_t v = start; v < end; ++v) { @@ -108,11 +108,11 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c d = !d; } - + #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) ptp_out.dist[v] = inv[v] != NIL ? pdist[!d][inv[v]] : INFINITY; - + delete [] error; delete [] pdist[0]; delete [] pdist[1]; @@ -138,7 +138,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c index_t d = 0; index_t start, end, n_cond, count; index_t i = 1, j = 2; - + // maximum number of iterations index_t iter = 0; index_t max_iter = toplesets.limits.size() << 1; @@ -150,7 +150,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c start = toplesets.limits[i]; end = toplesets.limits[j]; n_cond = toplesets.limits[i + 1] - start; - + #pragma omp parallel for for(index_t vi = start; vi < end; ++vi) { @@ -188,9 +188,9 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c d = !d; } - + delete [] error; - + if(ptp_out.dist != pdist[!d]) { memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices * sizeof(real_t)); diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index 5d9d4537..e887924c 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -20,7 +20,7 @@ namespace gproshan { double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) { cudaDeviceReset(); - + float time; cudaEvent_t start, stop; cudaEventCreate(&start); @@ -44,7 +44,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, real_t * d_error; cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - + index_t d; if(ptp_out.clusters) { @@ -65,7 +65,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, } cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); - + cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); @@ -93,7 +93,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0); - + // BEGIN FPS PTP CHE * h_mesh = new CHE(mesh); @@ -152,7 +152,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do delete [] h_dist; delete [] toplesets; delete [] sorted_index; - + cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); @@ -164,7 +164,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); time_fps = time / 1000; - + cudaEventDestroy(start); cudaEventDestroy(stop); @@ -206,11 +206,11 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r while(i < j && iter++ < max_iter) { if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - + start = limits[i]; end = limits[j]; n_cond = limits[i + 1] - start; - + if(h_clusters) relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], d_sorted, end, start); else @@ -224,7 +224,7 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; if(j < limits.size() - 1) ++j; - + d = !d; } diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index 8c53d058..5dc0028a 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -24,7 +24,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, che * coalescence_mesh = ptp_coalescence(inv, mesh, toplesets); // ------------------------------------------------------ - + cudaDeviceReset(); float time; @@ -53,7 +53,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, { index_t * h_clusters = new index_t[h_mesh->n_vertices]; index_t * d_clusters[2] = {nullptr, nullptr}; - + cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh->n_vertices); cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh->n_vertices); @@ -84,7 +84,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); ++i) ptp_out.dist[toplesets.index[i]] = h_dist[i]; - + delete [] h_dist; // END PTP @@ -133,7 +133,7 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t while(i < j && iter++ < max_iter) { if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - + start = inv.limits[i]; end = inv.limits[j]; n_cond = inv.limits[i + 1] - start; @@ -142,17 +142,17 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], end, start); else relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], end, start); - + cudaDeviceSynchronize(); - + relative_error <<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); cudaDeviceSynchronize(); - + if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; - + if(j < inv.limits.size() - 1) ++j; - + d = !d; } diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 746c4650..7623b82e 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -15,30 +15,30 @@ namespace gproshan { double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) { if(!sources.size()) return 0; - + // build impulse signal a_vec u0(mesh->n_vertices, arma::fill::zeros); for(auto & v: sources) u0(v) = 1; - + // step real_t dt = mesh->mean_edge(); dt *= dt; a_sp_mat L, A; laplacian(mesh, L, A); - + // make L positive-definite L += 1.0e-8 * A; // heat flow for short interval A += dt * L; a_vec u(mesh->n_vertices); - + cholmod_common context; cholmod_l_start(&context); - + double solve_time = 0; - + switch(opt) { case HEAT_ARMA: @@ -55,7 +55,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & } // extract geodesics - + a_vec div(mesh->n_vertices); compute_divergence(mesh, u, div); @@ -79,7 +79,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & real_t min_val = phi.min(); phi -= min_val; phi *= 0.5; - + cholmod_l_finish(&context); return solve_time; @@ -104,12 +104,12 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c { cholmod_sparse * cA = arma_2_cholmod(A, context); cA->stype = 1; - + cholmod_dense * cb = arma_2_cholmod(b, context); cholmod_factor * L = cholmod_l_analyze(cA, context); cholmod_l_factorize(cA, L, context); - + /* fill ratio gproshan_debug_var(L->xsize); gproshan_debug_var(cA->nzmax); @@ -120,7 +120,7 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c TIC(solve_time) cholmod_dense * cx = cholmod_l_solve(CHOLMOD_A, L, cb, context); TOC(solve_time) - + assert(x.n_rows == b.n_rows); copy_real_t_array(x.memptr(), (double *) cx->x, x.n_rows); @@ -130,7 +130,7 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c return solve_time; } - + cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) { cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); @@ -142,13 +142,13 @@ cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) { assert(sizeof(arma::uword) == sizeof(SuiteSparse_long)); - + cholmod_sparse * cS = cholmod_l_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); - + copy_real_t_array((double *) cS->x, S.values, S.n_nonzero); memcpy(cS->p, S.col_ptrs, (S.n_cols + 1) * sizeof(arma::uword)); memcpy(cS->i, S.row_indices, S.n_nonzero * sizeof(arma::uword)); - + return cS; } @@ -159,15 +159,15 @@ double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & { int * hA_col_ptrs = new int[A.n_cols + 1]; int * hA_row_indices = new int[A.n_nonzero]; - + #pragma omp parallel for for(index_t i = 0; i <= A.n_cols; ++i) hA_col_ptrs[i] = A.col_ptrs[i]; - + #pragma omp parallel for for(index_t i = 0; i < A.n_nonzero; ++i) hA_row_indices[i] = A.row_indices[i]; - + double solve_time = solve_positive_definite_cusolver(A.n_rows, A.n_nonzero, A.values, hA_col_ptrs, hA_row_indices, b.memptr(), x.memptr()); delete [] hA_col_ptrs; diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index 6064c74c..6f70ca93 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -14,7 +14,7 @@ struct cu_spAxb { int * A_col_ptrs, * A_row_indices; real_t * A_values, * x, * b; - + cu_spAxb(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx) { cudaMalloc(&A_col_ptrs, (m + 1) * sizeof(int)); @@ -25,7 +25,7 @@ struct cu_spAxb cudaMalloc(&A_values, nnz * sizeof(real_t)); cudaMemcpy(A_values, hA_values, nnz * sizeof(real_t), cudaMemcpyHostToDevice); - + cudaMalloc(&b, nnz * sizeof(real_t)); cudaMemcpy(b, hb, nnz * sizeof(real_t), cudaMemcpyHostToDevice); @@ -51,11 +51,11 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0); - + // solve Ax = b int singularity; - + cusolverSpHandle_t handle_cusolver; cusolverSpCreate(&handle_cusolver); @@ -63,7 +63,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t cusparseCreateMatDescr(&descr); cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL); cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO); - + if(host) { #ifdef SINGLE_P @@ -110,7 +110,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t double solve_positive_definite_cusparse(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx) { cudaDeviceReset(); - + float time; cudaEvent_t start, stop; cudaEventCreate(&start); @@ -118,28 +118,28 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t // allocate A, x, b into device cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb, hx); - + // aux vector y to device real_t * dy; cudaMalloc(&dy, m * sizeof(real_t)); - + cusparseHandle_t handle; cusparseCreate(&handle); // SOLVE Ax = b - + cusparseMatDescr_t descr_M = 0; cusparseMatDescr_t descr_L = 0; - + csric02Info_t info_M = 0; csrsv2Info_t info_L = 0; csrsv2Info_t info_Lt = 0; - + int buffer_size_M; int buffer_size_L; int buffer_size_Lt; int buffer_size; - + void * buffer = 0; int structural_zero; @@ -204,7 +204,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t // SOLVE cudaEventRecord(start, 0); - + #ifdef SINGLE_P cusparseScsrsv2_solve(handle, trans_L, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, data.b, dy, policy_L, buffer); cusparseScsrsv2_solve(handle, trans_Lt, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, dy, data.x, policy_Lt, buffer); @@ -212,16 +212,16 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t cusparseDcsrsv2_solve(handle, trans_L, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, data.b, dy, policy_L, buffer); cusparseDcsrsv2_solve(handle, trans_Lt, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, dy, data.x, policy_Lt, buffer); #endif - + // copy sol x to host cudaMemcpy(hx, data.x, m * sizeof(real_t), cudaMemcpyDeviceToHost); - + // END SOLVE cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); - + // FREE cudaFree(buffer); cusparseDestroyMatDescr(descr_M); @@ -230,7 +230,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t cusparseDestroyCsrsv2Info(info_L); cusparseDestroyCsrsv2Info(info_Lt); cusparseDestroy(handle); - + cudaEventDestroy(start); cudaEventDestroy(stop); @@ -267,12 +267,12 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons cudaStreamCreate(&stream); cusolverSpSetStream(cusolver_handle, stream); cusparseSetStream(cusparse_handle, stream); -*/ +*/ cusparseCreateMatDescr(&descr); cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL); cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO); - + if(host) { csrcholInfoHost_t info; @@ -285,9 +285,9 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons #else cusolverSpDcsrcholBufferInfoHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, &size_iternal, &size_chol); #endif - + buffer = new char[size_chol]; - + #ifdef SINGLE_P cusolverSpScsrcholFactorHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, buffer); cusolverSpScsrcholZeroPivotHost(cusolver_handle, info, 0, &singularity); @@ -299,13 +299,13 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons // SOLVE cudaEventRecord(start, 0); - + #ifdef SINGLE_P cusolverSpScsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); #else cusolverSpDcsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); #endif - + // END SOLVE cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -329,7 +329,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons #else cusolverSpDcsrcholBufferInfo(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, &size_iternal, &size_chol); #endif - + cudaMalloc(&buffer, size_chol); #ifdef SINGLE_P @@ -344,13 +344,13 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons // SOLVE cudaEventRecord(start, 0); - + #ifdef SINGLE_P cusolverSpScsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); #else cusolverSpDcsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); #endif - + // END SOLVE cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -370,7 +370,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons cudaEventDestroy(start); cudaEventDestroy(stop); - + return (double) time / 1000; } diff --git a/src/geodesics/sampling.cpp b/src/geodesics/sampling.cpp index dc4993dd..4dc746d8 100644 --- a/src/geodesics/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -17,10 +17,10 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n normals = new vertex[n_points]; sizes = new size_t[n_points]; index_t ** indexes = new index_t * [n_points]; - + geodesics::params params; params.radio = radio; - + #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 397c82ad..834bb987 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -20,7 +20,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) printf("./test_geodesics [data_path] [test_path] [exact_dist_path] [n_test = 10]\n"); return; } - + const char * data_path = args[1]; const char * test_path = args[2]; const char * exact_dist_path = args[3]; @@ -33,7 +33,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) FILE *ftable = fopen("ptp_results_double.tex", "w"); const char * ptime = "& %6.3lfs "; #endif - + const char * str[2] = {"", "\\bf"}; const char * pspeedup = "& \\bf (%.1lfx) "; const char * pbtime = "& %6s %6.3lfs "; @@ -43,18 +43,18 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) while(cin >> filename) { gproshan_debug_var(filename); - + vector source = { 0 }; - + che * mesh = new che_off(data_path + filename + ".off"); size_t n_vertices = mesh->n_vertices; - + index_t * toplesets = new index_t[n_vertices]; index_t * sorted_index = new index_t[n_vertices]; vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, source); - - + + // PERFORMANCE & ACCURACY ___________________________________________________________________ double Time[7]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse @@ -71,13 +71,13 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #else Time[2] = INFINITY; #endif // GPROSHAN_CUDA - + #ifdef SINGLE_P Time[6] = Time[5] = Time[4] = Time[3] = INFINITY; Error[4] = Error[3] = INFINITY; #else Time[4] = test_heat_method_cholmod(Error[3], Time[3], exact, mesh, source, n_test); - if(n_vertices < 100000) + if(n_vertices < 100000) { #ifdef GPROSHAN_CUDA Time[6] = test_heat_method_gpu(Error[4], Time[5], exact, mesh, source, n_test); @@ -92,15 +92,15 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) Error[4] = INFINITY; } #endif - + index_t t_min = 0; for(index_t i = 1; i < sizeof(Time) / sizeof(double); ++i) if(Time[t_min] > Time[i]) t_min = i; - + index_t e_min = 0; for(index_t i = 1; i < sizeof(Error) / sizeof(real_t); ++i) if(Error[e_min] > Error[i]) e_min = i; - + fprintf(ftable, "%20s ", ("\\verb|" + filename + '|').c_str()); fprintf(ftable, "& %12lu ", n_vertices); @@ -112,7 +112,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fprintf(ftable, pbtime, str[1 == t_min], Time[1]); fprintf(ftable, pspeedup, Time[0] / Time[1]); fprintf(ftable, pberror, str[1 == e_min], Error[1]); - + #ifndef SINGLE_P fprintf(ftable, "& OpenMP "); #endif @@ -141,7 +141,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fprintf(ftable, pspeedup, Time[0] / Time[2]); fprintf(ftable, pberror, str[2 == e_min], Error[2]); fprintf(ftable, "& Cuda "); - + // HEAT FLOW cusparse fprintf(ftable, ptime, Time[6]); fprintf(ftable, pbtime, str[5 == t_min], Time[5]); @@ -152,7 +152,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fprintf(ftable, "\\\\\\hline\n"); #endif - + // DEGREE HISTOGRAM ________________________________________________________________________ index_t dv; @@ -168,7 +168,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(auto & ii: deg) os << ii.first << " " << ii.second << endl; os.close(); - + // TOPLESETS DISTRIBUTION __________________________________________________________________ @@ -189,7 +189,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) os << i << " " << toplesets_dist[i] << endl; os.close(); - + // PTP ITERATION ERROR _____________________________________________________________________ #ifdef GPROSHAN_CUDA // IMPLEMENT: iter_error_parallel_toplesets_propagation_coalescence_cpu @@ -201,7 +201,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #ifndef SINGLE_P os.open(test_path + filename + "_error_double.iter"); - #else + #else os.open(test_path + filename + "_error.iter"); #endif @@ -213,12 +213,12 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // FARTHEST POINT SAMPLING _________________________________________________________________ - + #ifdef GPROSHAN_CUDA // IMPLEMENT: times_farthest_point_sampling_ptp_cpu size_t i_samples = source.size(); size_t n_samples = 1001; double * times_fps = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); - + os.open(test_path + filename + ".fps"); for(index_t i = i_samples; i < n_samples; ++i) os << i << " " << times_fps[i] << endl; @@ -226,7 +226,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) delete [] times_fps; #endif // GPROSHAN_CUDA - + // FREE MEMORY delete mesh; @@ -235,7 +235,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) delete [] exact; delete [] toplesets_dist; } - + fclose(ftable); } @@ -259,7 +259,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; - + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; ++i) { @@ -268,7 +268,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vect } error = compute_error(dist, exact, mesh->n_vertices, source.size()); - + delete [] dist; return seconds; @@ -278,7 +278,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e { double t, st, ptime; ptime = stime = INFINITY; - + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; ++i) { @@ -300,7 +300,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; - + real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; ++i) { @@ -311,7 +311,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vect error = compute_error(dist, exact, mesh->n_vertices, source.size()); delete [] dist; - + return seconds; } @@ -319,12 +319,12 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact { double t, st, ptime; ptime = stime = INFINITY; - + real_t * dist = nullptr; for(int i = 0; i < n_test; ++i) { if(dist) delete [] dist; - + TIC(t) st = heat_method(dist, mesh, source, HEAT_CUDA); TOC(t) ptime = min(t - st, ptime); stime = min(st, stime); @@ -351,7 +351,7 @@ real_t * load_exact_geodesics(const string & file, const size_t & n) for(index_t i = 0; i < n; ++i) is >> exact[i]; is.close(); - + return exact; } diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index b745645e..c2f77adc 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -47,7 +47,7 @@ vector > iter_error_parallel_toplesets_propagation_gpu(che cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); - + delete [] h_dist; cudaFree(d_error); cudaFree(d_dist[0]); @@ -64,7 +64,7 @@ vector > iter_error_parallel_toplesets_propagation_gpu(che cudaEventDestroy(start); cudaEventDestroy(stop); - + return iter_error; } @@ -116,7 +116,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam while(n-- && max_dist > radio) { cudaEventRecord(start, 0); - + limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); @@ -128,7 +128,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam #else cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #endif - + cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time_fps, start, stop); @@ -146,7 +146,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam delete [] h_dist; delete [] toplesets; delete [] sorted_index; - + cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); @@ -175,8 +175,8 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); vector > iter_error; - iter_error.reserve(limits.size()); - + iter_error.reserve(limits.size()); + index_t d = 0; index_t start, end, n_cond; index_t i = 1, j = 2; @@ -190,23 +190,23 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ n_cond = limits[i + 1] - start; relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_sorted, end, start); - + // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(j == limits.size() - 1) iter_error.push_back(make_pair(n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size()))); // end - + relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond, d_sorted); cudaDeviceSynchronize(); - + if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; if(j < limits.size() - 1) ++j; - + d = !d; } - + return iter_error; } diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 0f3e692c..70208177 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -28,7 +28,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc index_t * F = new index_t[mesh->n_faces * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices]; real_t * exact_dist_sorted = new real_t[mesh->n_vertices]; - + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { @@ -49,13 +49,13 @@ vector > iter_error_parallel_toplesets_propagation_coalesc // ------------------------------------------------------ cudaDeviceReset(); - + float time; cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0); - + // BEGIN PTP CHE * h_mesh = new CHE(mesh); @@ -72,7 +72,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); - + delete [] h_dist; cudaFree(d_error); cudaFree(d_dist[0]); @@ -80,7 +80,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc cuda_free_CHE(dd_mesh, d_mesh); // END PTP - + cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); @@ -105,7 +105,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices]; index_t * F = new index_t[mesh->n_faces * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices]; @@ -141,12 +141,12 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector radio) { cudaEventRecord(start, 0); - + limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - + // sort data by levels, must be improve the coalescence - + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { @@ -177,7 +177,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices, d_dist[d], 1, &f); #endif - + cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time_fps, start, stop); @@ -186,12 +186,12 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector 0 || !n) cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); - + samples.push_back(sorted_index[f - 1]); } cublasDestroy(handle); - + delete [] V; delete [] F; delete [] inv; @@ -227,7 +227,7 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, iter_error.reserve(limits.size()); ofstream os("band"); - + index_t d = 0; index_t start, end, n_cond; index_t i = 1, j = 2; @@ -252,11 +252,11 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond); cudaDeviceSynchronize(); - + if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; - if(j < limits.size() - 1) ++j; - + if(j < limits.size() - 1) ++j; + d = !d; } diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp index bf0c7f10..18e8178f 100644 --- a/src/geometry/convex_hull.cpp +++ b/src/geometry/convex_hull.cpp @@ -33,14 +33,14 @@ void convex_hull::andrew_algorithm(vertex * points, const size_t & n_points) while(k > 1 && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; CH[k++] = points[i]; } - + index_t t = k; for(index_t i = n_points - 2; i > 0; --i) { while(k > t && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; CH[k++] = points[i]; } - + while(k > t && !ccw(CH[k - 2], CH[k - 1], points[0])) --k; CH.resize(k); diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index b4eeb10f..c7318afa 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -83,7 +83,7 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) { gproshan_debug(LAPLACIAN); - + laplacian(mesh, L, A); string feigval = tmp_file_path(mesh->name_size() + '_' + to_string(k) + ".L_eigval"); @@ -99,7 +99,7 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat if(!eigs_sym(eigval, eigvec, L, k, "sa")) return 0; - + eigval.save(feigval); eigvec.save(feigvec); } diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 773ff5c4..2a16ae76 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -40,7 +40,7 @@ void basis::plot_basis() os.close(); file = "gnuplot -persist " + file + " &"; - + system(file.c_str()); } diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 68ebed3f..81168af5 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -18,7 +18,7 @@ void test_image_denoising(const string & file) size_t p = 8; // square side of each patche size_t rows = image.width() - p + 1; - size_t cols = image.height() - p + 1; + size_t cols = image.height() - p + 1; size_t n = p * p; // size of each patche size_t m = 256; // number of atoms size_t M = rows * cols; // number of patches @@ -37,12 +37,12 @@ void test_image_denoising(const string & file) for(index_t a = x; a < x + p; ++a) X(k++, i) = image(a, b); } - + a_mat D(n, m, arma::fill::randu); D = normalise(D); a_mat spD = D; - + CImg imdict; for(index_t i = 0; i < 16; ++i) { @@ -61,17 +61,17 @@ void test_image_denoising(const string & file) TIC(time) KSVD(D, X, L, K); TOC(time) - + gproshan_log_var(time); - + TIC(time) sp_KSVD(spD, X, L, K); TOC(time) - + gproshan_log_var(time); gproshan_log_var(norm(D - spD)); - + CImg imdictlearned; for(index_t i = 0; i < 16; ++i) { @@ -89,14 +89,14 @@ void test_image_denoising(const string & file) TIC(time) a_mat Y = D * OMP_all(X, D, L); TOC(time) - + gproshan_log_var(time); - + TIC(time) vector locval; a_mat spY = D * OMP_all(locval, X, D, L); TOC(time) - + gproshan_log_var(time); gproshan_log_var(norm(Y - spY)); diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 6d5e4b33..8ff58aa5 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -79,16 +79,16 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) while(k--) { a_sp_mat alpha = OMP_all(locval, X, D, L); - + sort(locval.begin(), locval.end()); rows.push_back(0); for(index_t k = 1; k < locval.size(); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); - + rows.push_back(locval.size()); - + R = X - D * alpha; #pragma omp parallel for firstprivate(omega, E, U, V, s) @@ -116,16 +116,16 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; + real_t threshold = norm(x) * sigma; a_mat DD; a_vec aa, r = x; - + index_t l = 0; while(norm(r) > threshold && l < L) { selected_atoms(l) = index_max(abs(D.t() * r)); - + DD = D.cols(selected_atoms.head(l + 1)); aa = pinv(DD) * x; r = x - DD * aa; @@ -140,7 +140,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) for(size_t i=0; i< V.size(); ++i) if(mask[indices[i]]) return indices[i]; - + return NIL; } @@ -148,17 +148,17 @@ tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; + real_t threshold = norm(x) * sigma; a_mat DD; - a_vec aa, r = x; - + a_vec aa, r = x; + index_t l = 0; while(norm(r) > threshold && l < L) { // gproshan_debug_var(D.t() * r); selected_atoms(l) = max_index(abs(D.t() * r), mask); - + DD = D.cols(selected_atoms.head(l + 1)); aa = pinv(DD) * x; r = x - DD * aa; @@ -199,7 +199,7 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) { alpha.col(i) = OMP(X.col(i), D, L); } - + return alpha; } @@ -241,10 +241,10 @@ a_vec OMP(const patch & p, const a_mat & A, const size_t & L) a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) { arma::uchar_vec mask(A.n_cols); - + for(index_t i = 0; i < A.n_cols; ++i) mask(i) = phi_basis->freq(i) >= patch::nyquist_factor * p.avg_dist; // 2.5* if it ismin - + return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } @@ -255,7 +255,7 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, #pragma omp parallel for for(index_t i = 0; i < patches.size(); ++i) alpha.col(i) = OMP(patches[i],phi_basis, A, L); - + return alpha; } @@ -266,7 +266,7 @@ a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) #pragma omp parallel for for(index_t i = 0; i < patches.size(); ++i) alpha.col(i) = OMP(patches[i], A, L); - + return alpha; } @@ -279,7 +279,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) a_mat new_A = A; a_mat alpha, D, sum, sum_error; a_vec a, e; - + real_t aj; while(k--) @@ -308,7 +308,7 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) sum_error += aj * patches[i].phi.t() * e; //concat e patches[i].phi.t() * e; //apply svd to update the atom - + } if(omega.size()) @@ -356,9 +356,9 @@ void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t a_mat new_A = A; a_mat D, sum, sum_error; a_vec a, e; - + real_t aj; - + vector locval; vector rows; rows.reserve(A.n_cols + 1); @@ -366,14 +366,14 @@ void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t while(k--) { a_sp_mat alpha = OMP_all(locval, patches, A, L); - + sort(locval.begin(), locval.end()); rows.push_back(0); for(index_t k = 1; k < locval.size(); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); - + rows.push_back(locval.size()); #pragma omp parallel for private(a, aj, D, e, sum, sum_error) diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 6dd7eb98..7f96b438 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -48,9 +48,9 @@ msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, co { A.eye(phi_basis->dim(), m_params.n_atoms); dist = new real_t[mesh->n_vertices]; - + m_params.n_patches = mesh->n_vertices / m_params.avg_p; - + key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); mask = new bool[mesh->n_vertices]; @@ -73,7 +73,7 @@ void msparse_coding::load_mask() string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); - + if(V.load(f_mask)) { @@ -127,7 +127,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde size_t * percentages_size = new size_t[m_params.n_patches]; bool cover_cluster[m_params.n_patches]; - + // create initial desired percentage sizes #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) @@ -142,9 +142,9 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde size_t k = 0; size_t rn = 0; - + while(k < m_params.n_patches) - { + { //gproshan_debug_var(clusters[rn]); rn = distribution(generator); @@ -154,21 +154,21 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde mask[rn] = 1; V(rn) = 1; percentages_size[clusters[rn]]--; - + } if( !cover_cluster[clusters[rn]] && percentages_size[clusters[rn]] == 0) { cover_cluster[clusters[rn]] = 1; // It is finished ++k; - } - + } + } - - + + V.save(f_mask); } - + } void msparse_coding::load_sampling(bool save_all) @@ -196,16 +196,16 @@ void msparse_coding::load_sampling(bool save_all) p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); patches.push_back(move(p)); } - + m_params.n_patches = n_seeds; - + return; } //ELSE IF !S.load(f_sample) - + gproshan_debug(computing sampling); - + // compute features will be seeds patches_map.resize(mesh->n_vertices); @@ -215,7 +215,7 @@ void msparse_coding::load_sampling(bool save_all) #pragma omp for for(index_t i = 0; i < mesh->n_vertices; ++i) covered[i] = 0; - + real_t euc_radio; real_t geo_radio; vector radios; @@ -262,12 +262,12 @@ void msparse_coding::load_sampling(bool save_all) } } } - } + } delete [] invalid_seed; m_params.n_patches = seeds.size(); - + gproshan_log(saving sampling); S.resize(seeds.size()); @@ -275,7 +275,7 @@ void msparse_coding::load_sampling(bool save_all) #pragma omp parallel for for(index_t i = 0; i < seeds.size(); ++i) S(i) = seeds[i]; - + S.save(tmp_file_path(key_name + ".rsampl")); gproshan_debug_var(m_params.sum_thres); @@ -287,7 +287,7 @@ void msparse_coding::load_sampling(bool save_all) #ifndef NDEBUG vector outliers; - + for(index_t i = 0; i < mesh->n_vertices; ++i) if(!covered[i]) @@ -296,7 +296,7 @@ void msparse_coding::load_sampling(bool save_all) a_vec outlv(outliers.size()); for(index_t i = 0; i < outliers.size(); ++i) outlv(i) = outliers[i]; - + outlv.save(tmp_file_path(key_name + ".outlv")); #endif // NDEBUG } @@ -330,13 +330,13 @@ void msparse_coding::init_radial_feature_patches() //Initializing patches gproshan_log(initializing patches); gproshan_debug_var(m_params.n_patches); - + n_vertices = mesh->n_vertices; patches.resize(m_params.n_patches); patches_map.resize(n_vertices); - + bool * pmask = mask; - + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); @@ -351,7 +351,7 @@ void msparse_coding::init_radial_feature_patches() p.compute_avg_distance(mesh, patches_map, s); p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - } + } bool save_all = true; if(save_all) @@ -361,7 +361,7 @@ void msparse_coding::init_radial_feature_patches() for(index_t i = 0; i < m_params.n_patches; ++i) { patch & p = patches[i]; - + AS(i,0) = p.x(0); AS(i,1) = p.x(1); AS(i,2) = p.x(2); @@ -376,10 +376,10 @@ void msparse_coding::init_radial_feature_patches() AS(i,11) = p.T(1,2); AS(i,12) = p.T(2,2); } - + AS.save(tmp_file_path(key_name + ".smp")); } - + gproshan_log(radial patches are ready); } @@ -395,7 +395,7 @@ void msparse_coding::init_voronoi_patches() //FPS samplif_dictng with desired number of sources TIC(d_time) init_sampling(); TOC(d_time) gproshan_debug_var(d_time); - + geodesics::params params; params.cluster = 1; @@ -404,7 +404,7 @@ void msparse_coding::init_voronoi_patches() #ifdef GPROSHAN_CUDA params.alg = geodesics::PTP_GPU; #endif - + geodesics ptp(mesh, sampling, params); TOC(d_time) gproshan_log_var(d_time); @@ -418,13 +418,13 @@ void msparse_coding::init_voronoi_patches() } for(index_t i = 0; i < mesh->n_vertices; ++i) - { + { ptp.clusters[i]--; if(sample(ptp.clusters[i]) != i) vertices[ptp.clusters[i]].push_back(i) ; } - - + + load_mask(vertices, ptp.clusters); //Initializing patches @@ -441,8 +441,8 @@ void msparse_coding::init_voronoi_patches() for(index_t s = 0; s < m_params.n_patches; ++s) { patches[s].init_disjoint(mesh, sample(s), msparse_coding::T, vertices[s], toplevel); - - } + + } #ifndef NDEBUG size_t patch_avg_size = 0; size_t patch_min_size = NIL; @@ -510,7 +510,7 @@ real_t msparse_coding::execute() #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) patches_map[i].clear(); - + for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].reset_xyz(mesh, patches_map, s, 0); @@ -531,7 +531,7 @@ real_t msparse_coding::execute() real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); - + arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(non_zero.size()); real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices); @@ -547,7 +547,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) S.load(tmp_file_path(key_name + ".smp")); alpha.load(tmp_file_path(key_name + ".alpha")); - + m_params.n_patches = S.n_rows; patches.resize(m_params.n_patches); @@ -560,9 +560,9 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) max_radio = max(max_radio, S(i, 3)); A.eye(phi_basis->dim(), m_params.n_atoms); - + size_t total_points = 0; - + #pragma omp parallel for for(index_t i = 0; i < m_params.n_patches; ++i) { @@ -576,22 +576,22 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) T(0,2) = S(i,10); T(1,2) = S(i,11); T(2,2) = S(i,12); - + patch & p = patches[i]; p.init_random({S(i, 0), S(i, 1), S(i, 2)}, T, S(i, 3), max_radio, per, fr); p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - + a_vec x = p.phi * A * alpha.col(i); p.xyz.row(2) = x.t(); - + p.iscale_xyz(phi_basis->radio()); p.itransform(); #pragma omp atomic total_points += p.xyz.n_cols; - + for(index_t j = 0; j < patches[i].xyz.n_cols; ++j) if(patches[i].xyz(2, j) > 2) { @@ -614,7 +614,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); new_mesh->update_normals(); - + a_vec n; for(index_t v = 0, i = 0; i < m_params.n_patches; ++i) { @@ -622,7 +622,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 0); a_vec dx = p.phi * A * alpha.col(i); - + phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); a_vec dy = p.phi * A * alpha.col(i); @@ -631,13 +631,13 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) n = {-dx(j), -dy(j), 1}; n = normalise(p.T * n); new_mesh->normal(v) = {n(0), n(1), n(2)}; - } + } } che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc"), che_off::NOFF); - + gproshan_debug(saved new point cloud); - + return new_mesh; } @@ -703,14 +703,14 @@ void msparse_coding::learning() a_mat R, E, U, V; a_vec s; svd(U, s, V, alpha); - gproshan_debug(svd done!); + gproshan_debug(svd done!); A = U.cols(0, m_params.n_atoms); - gproshan_debug(svd done!); + gproshan_debug(svd done!); A = normalise(A); gproshan_debug_var(phi_basis->radio()); gproshan_debug_var(m_params.n_atoms); // - + phi_basis->plot_atoms(A); KSVD(A, patches, L, K); phi_basis->plot_atoms(A); @@ -731,7 +731,7 @@ void msparse_coding::learning() void msparse_coding::sparse_coding() { gproshan_log(MDICT); - + vector locval; alpha = OMP_all(patches, phi_basis, A, L); } @@ -767,7 +767,7 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; inp.open(f_feat.c_str(), ifstream::in); - + size_t tam; index_t tmp; @@ -778,7 +778,7 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - + string command = "../../harris3d/harris3d " + mesh->filename + " " + f_feat + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); system(command.c_str()); @@ -824,7 +824,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) index_t v = sample(s); patches[s].init(mesh, v, msparse_coding::T, phi_basis->radio(), toplevel); } - + delete [] toplevel; } @@ -865,13 +865,13 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) p.phi = normalise(p.phi); } -/* +/* #ifndef NDEBUG CImgList imlist; for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].save(phi_basis->radio(), 16, imlist); imlist.save_ffmpeg_external("tmp/patches.mpg", 5); -#endif +#endif */ @@ -908,25 +908,25 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) for(index_t p = 0; p < m_params.n_patches; ++p) { patch & rp = patches[p]; - + a_vec x = rp.phi * A * alpha.col(p); - + patches_error[p] = { accu(abs(x - rp.xyz.row(2).t())) / rp.vertices.size(), p }; rp.xyz.row(2) = x.t(); } sort(patches_error.begin(), patches_error.end()); - + fprintf(stderr, "error %16s%16s\n", "best", "worst"); for(index_t i = 0; i < 10; ++i) { const index_t & best = patches_error[i].second; const index_t & worst = patches_error[m_params.n_patches - i - 1].second; - + fprintf(stderr, "%5d:%8u>%8u%8u>%8u\n", i, best, draw_patches(best), worst, draw_patches(worst)); } - + #pragma omp parallel for for(index_t p = 0; p < m_params.n_patches; ++p) { @@ -941,7 +941,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) // simple means vertex if(patches_map[v].size() && (!mask || mask(v))) { - a_vec mv = arma::zeros(3); + a_vec mv = arma::zeros(3); for(auto p: patches_map[v]) mv += patches[p.first].xyz.col(p.second); @@ -969,7 +969,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) error += dist[v]; max_error = max(max_error, dist[v]); } - + error /= mesh->n_vertices; gproshan_debug_var(mesh->n_vertices); @@ -978,7 +978,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) mesh->set_vertices(new_vertices, mesh->n_vertices); che_off::write_file(mesh, "../tmp/recon_mesh"); - + return max_error; } @@ -994,10 +994,10 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) { #pragma omp parallel for for(index_t s = threshold; s < m_params.n_patches; ++s) - { + { if(!patches_covered[s-threshold]) - { + { a_vec sum; sum.zeros(); size_t c = 0; @@ -1008,18 +1008,18 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) if(p.first < threshold || patches_covered[p.first-threshold]) { sum += alpha.col(p.first); - } + } sum /= c; } alpha.col(s) = sum; patches_covered[s-threshold] = 1; ++count; - } + } } } - + // update alphas of choosed patches // update the threshold // repeat until threshold reachs all patches diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 34f548c7..a21c495a 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -37,19 +37,19 @@ void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, cons { radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; - + gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); gather_vertices(mesh, v, radio_, toplevel); if(!_toplevel) delete [] toplevel; -} +} void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) { radio = 1; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; - + gather_vertices(mesh, v, n_toplevels, toplevel); jet_fit_directions(mesh, v); //vertices = _vertices; @@ -81,7 +81,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N // it needs to return both vertices // it needs to filter repeated indexes. // p should be the maximun - + index_t a, b, i = 0; vertex min_he; double area_face = 0, proj_area_face = 0; @@ -108,7 +108,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N i = find(vertices.data(), vertices.size(), a); else i = find(vertices.data(), vertices.size(), b); - + tmp_angle = acos( (mesh->normal_he(he), N[i]) ); if(angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation) // Fullfill conditions @@ -129,7 +129,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N //p = mesh->get_vertex(indexes[i]); //p = p - c ; //p = p - ((p,n)*n); - + area += area_face; proj_area += proj_area_face; @@ -138,7 +138,7 @@ bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N vertices.push_back(v); N.push_back(min_he); } - + return added; } @@ -146,12 +146,12 @@ void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, { this->radio = radio; this->T = T; - + x.resize(3); x(0) = c.x; x(1) = c.y; x(2) = c.z; - + std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() @@ -171,7 +171,7 @@ void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, { double a = abs(dis(gen)) * 2 * M_PI; double r = fr * abs(dis(gen)); - + xyz(0, i) = r * cos(a); xyz(1, i) = r * sin(a); xyz(2, i) = 0; @@ -231,7 +231,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, const index_t & v, - const real_t & delta, + const real_t & delta, const real_t & sum_thres, const real_t & area_thres, const real_t & area_mesh @@ -241,23 +241,23 @@ void patch::init_radial_disjoint( real_t & euc_radio, min_nv = 128; euc_radio = -INFINITY; - + normal_fit_directions(mesh, v); - + a_vec vn = T.col(2); vertex n = { vn(0), vn(1), vn(2) }; - + vertices.push_back(v); - + vector N; N.push_back(n); - + real_t area = 0; real_t proj_area = std::numeric_limits::epsilon(); real_t ratio; - + vertex c = mesh->get_vertex(v); - + geodesics::params params; params.dist_alloc = new real_t[mesh->n_vertices]; params.fun = [&](const index_t & u) -> bool @@ -320,7 +320,7 @@ void patch::itransform() void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); - + if(mask) { m = 0; @@ -358,18 +358,18 @@ double area_tri(double x1, double y1, double x2, double y2, double x3, double y3 void patch::remove_extra_xyz_disjoint(size_t & max_points) { if(vertices.size() > max_points) - { + { arma::uvec xi; xi.zeros(max_points); for (size_t i=1; i< max_points; ++i) xi[i] = i; xi = arma::shuffle(xi); xyz = xyz.cols(xi); } - + } void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) { - + size_t m = std::max (vertices.size(), min_nv); @@ -382,7 +382,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co while(j < m ) { - + // add new vertices // create a random point real_t a = abs(dis(gen)) * 2 * M_PI; @@ -397,14 +397,14 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co { a_vec aux = xyz.col(vpatches[v][p]); aux(2) = 0; - + if(norm(np - aux) < min_d) { min_d = norm(np - aux); min_v = v; } } - + // forstar to find closest trinagle a_mat abc(3,3); for_star(he, mesh, min_v) @@ -412,12 +412,12 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co //discard triangles outside the patch vpatches_t & ma = vpatches[mesh->vt(next(he))]; vpatches_t & mb = vpatches[mesh->vt(prev(he))]; - + if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { arma::uvec xi = { vpatches[min_v][p], ma[p], mb[p] }; abc = xyz.cols(xi); - + //gproshan_debug_var(np); // verify if this is inside a triangle @@ -425,17 +425,17 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co double A1 = area_tri(np(0), np(1), abc(0,1), abc(1,1), abc(0,2), abc(1,2) ); double A2 = area_tri(abc(0,0), abc(1,0), np(0), np(1), abc(0,2), abc(1,2) ); double A3 = area_tri(abc(0,0), abc(1,0), abc(0,1), abc(1,1), np(0), np(1) ); - - - + + + if(abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) { a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); - + a_vec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); np = proj_abc * coef + abc.col(0); - + if(!isnan(np(2))) { xyz(0, j) = np(0); @@ -454,8 +454,8 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co } } - - } + + } } } @@ -480,10 +480,10 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & y = floor((xyz.col(i)[1] + radio) * (imsize - 1) / (2 * radio)); img(x,y) = xyz.col(i)[2]; } - + img.resize(128, 128); imlist.insert(img.normalize(0, 255)); //img.save("tmp/images/test_image.jpg"); - + } void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_toplevels, index_t * toplevel) @@ -545,17 +545,17 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl vertices.reserve(expected_nv); memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); - + link_t link; toplevel[v] = 0; vertices.push_back(v); - + for(index_t i = 0; i < vertices.size(); ++i) { const index_t & v = vertices[i]; if(toplevel[v] == n_toplevels) break; - + mesh->link(link, v); for(const index_t & he: link) { @@ -567,21 +567,21 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl } } - link.clear(); - } + link.clear(); + } } void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, index_t * toplevel) { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); - + if(vertices.size()) vertices.clear(); vertices.reserve(expected_nv); - + priority_queue > qvertices; memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); - + a_vec p(3); link_t link; @@ -592,9 +592,9 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, { index_t v = qvertices.top().second; qvertices.pop(); - + vertices.push_back(v); - + mesh->link(link, v); for(const index_t & he: link) { @@ -607,13 +607,13 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, p = T.t() * (p - x); toplevel[u] = toplevel[v] + 1; - + if(norm(p) < radio) qvertices.push({-norm(p), u}); } link.clear(); } - } + } } /// Compute the principal directions of the patch, centering in the vertex \f$v\f$. @@ -629,14 +629,14 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) in_points.reserve(vertices.size()); for(const index_t & u: vertices) in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); - + My_Monge_form monge_form; My_Monge_via_jet_fitting monge_fit; monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); vertex normal = mesh->normal(v); monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); - + x.set_size(3); x(0) = mesh->gt(v).x; x(1) = mesh->gt(v).y; @@ -662,7 +662,7 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) x(1) = mesh->gt(v).y; x(2) = mesh->gt(v).z; - + vertex nz = mesh->normal(v); vertex nx = mesh->gt_vt_next_evt(v); // GT[VT[next(EVT[v]]] @@ -712,7 +712,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) { xyz(2, i) = (xyz(2, i) - min) / (max - min); } - } + } else { for(index_t i = 0; i < vertices.size(); ++i) @@ -722,7 +722,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) xyz.col(i)[2] = tmp; } } - + } void patch::save_z(ostream & os) @@ -748,7 +748,7 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons for(const index_t & he: link) { const index_t & u = mesh->vt(he); - + for (auto itp:vpatches[u]) { if( itp.first == p) @@ -760,7 +760,7 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons distances.push_back(norm(a - b)); break; } - } + } } } @@ -781,9 +781,9 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; } else - { + { avg_dist = distances[n_elem/2]; - } + } //avg_dist = avg_dist + norm(xyz.col(i)- xyz.col(j)); } @@ -791,7 +791,7 @@ bool patch::is_covered( bool * covered) { for(index_t i = 0; i < vertices.size(); ++i) if(!covered[i]) return false; - + return true; } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index f5b51bcf..cac9d059 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -78,10 +78,10 @@ che::che(const che & mesh) VN = new vertex[n_vertices]; memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); - + VC = new vertex[n_vertices]; memcpy(VC, mesh.VC, n_vertices * sizeof(vertex)); - + VHC = new real_t[n_vertices]; memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); } @@ -121,7 +121,7 @@ void che::link(link_t & l, const index_t & v) const } } -///< return a vector of indices of one vertex per boundary +///< return a vector of indices of one vertex per boundary vector che::bounds() const { if(!n_faces) return {}; @@ -153,7 +153,7 @@ vector che::boundary(const index_t & v) const for_boundary(he, this, v) vbound.push_back(VT[he]); - + return vbound; } @@ -190,7 +190,7 @@ void che::flip(const index_t & e) index_t ot_na = OT[next(ha)]; index_t ot_pb = OT[prev(hb)]; index_t ot_nb = OT[next(hb)]; - + VT[prev(ha)] = vb; VT[ha] = vc; VT[next(ha)] = vd; @@ -289,7 +289,7 @@ void che::update_heatmap(const real_t * hm, real_t max_color) return; } - + if(max_color < numeric_limits::epsilon()) { #pragma omp parallel for reduction(max: max_color) @@ -342,11 +342,11 @@ void che::update_normals() for(index_t v = 0; v < n_vertices; ++v) { vertex & n = VN[v]; - + n = 0; for_star(he, this, v) n += area_trig(trig(he)) * normal_he(he); - + n /= *n; } } @@ -479,7 +479,7 @@ real_t che::mean_curvature(const index_t & v) { real_t h = 0; real_t a = 0; - + for_star(he, this, v) { a += area_trig(trig(he)); @@ -655,7 +655,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector level) { if(++level > k) break; @@ -685,7 +685,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector * medges = new map[n_vertices]; - + size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) { const index_t & u = VT[he]; const index_t & v = VT[next(he)]; - + EVT[u] = he; - + index_t & mhe = u < v ? medges[u][v] : medges[v][u]; if(!mhe) { @@ -1428,15 +1428,15 @@ void che::update_evt_ot_et() OT[OT[he]] = he; } } - + rw(n_edges) = ne; - + delete [] medges; // non manifold two disk //for(index_t he = 0; he < n_half_edges; ++he) // if(OT[he] != NIL) assert(he == OT[OT[he]]); - + for(index_t he = 0; he < n_half_edges; ++he) if(OT[he] == NIL && EVT[VT[he]] != NIL) { diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 5480b92f..7a479013 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -289,7 +289,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t holes = new che*[n_borders]; gproshan_debug(inpainting); - + for(index_t b = 0; b < n_borders; ++b) border_vertices[b] = mesh->boundary(bounds[b]); @@ -806,7 +806,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c return fill_hole_front_angles(vertices, length, -normal, max_iter, !is_grow); else return nullptr; } - + vertices.clear(); vertices.reserve(tmp_vertices.size()); diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index e44f997e..488e1055 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -32,9 +32,9 @@ che_img::~che_img() void che_img::read_file(const string & file) { CImg img(file.c_str()); - + alloc(img.height() * img.width(), 2 * (img.height() - 1) * (img.width() - 1)); - + index_t v = 0, he = 0; for(int i = 0; i < img.width(); ++i) for(int j = 0; j < img.height(); ++j) @@ -49,7 +49,7 @@ void che_img::read_file(const string & file) VT[he++] = (i - 1) * img.height() + j; VT[he++] = (i - 1) * img.height() + j - 1; } - + GT[v++] = vertex(i,j, img(i, j)); } diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 3786d27f..316525f7 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -27,7 +27,7 @@ void che_obj::read_file(const string & file) ifstream is(file); assert(is.good()); - + real_t x, y, z; int face[8], i; @@ -54,19 +54,19 @@ void che_obj::read_file(const string & file) { for(i = 0; ss >> face[i]; ++i) ss.ignore(256, ' '); - + if(i == che::mtrig) { if(face[0] < 0) { - faces.push_back(vertices.size() + face[0]); - faces.push_back(vertices.size() + face[1]); - faces.push_back(vertices.size() + face[2]); + faces.push_back(vertices.size() + face[0]); + faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[2]); } else { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); + faces.push_back(face[0] - 1); + faces.push_back(face[1] - 1); faces.push_back(face[2] - 1); } } @@ -74,30 +74,30 @@ void che_obj::read_file(const string & file) { if(face[0] < 0) { - faces.push_back(vertices.size() + face[0]); - faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[0]); + faces.push_back(vertices.size() + face[1]); faces.push_back(vertices.size() + face[3]); - faces.push_back(vertices.size() + face[1]); - faces.push_back(vertices.size() + face[2]); + faces.push_back(vertices.size() + face[1]); + faces.push_back(vertices.size() + face[2]); faces.push_back(vertices.size() + face[3]); } else { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); + faces.push_back(face[0] - 1); + faces.push_back(face[1] - 1); faces.push_back(face[3] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[2] - 1); + faces.push_back(face[1] - 1); + faces.push_back(face[2] - 1); faces.push_back(face[3] - 1); } } - } + } } is.close(); - + init(vertices.data(), vertices.size(), faces.data(), faces.size() / che::mtrig); } diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 6dbbc74e..3ec76c5b 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -29,15 +29,15 @@ void che_off::read_file(const string & file) { char soff[32]; size_t nv, nf, ne; - + FILE * fp = fopen(file.c_str(), "r"); assert(fp); fgets(soff, sizeof(soff), fp); fscanf(fp, "%lu %lu %lu", &nv, &nf, &ne); - + alloc(nv, nf); - + float x, y, z, r, g, b, a; for(index_t v = 0; v < n_vertices; ++v) { @@ -56,7 +56,7 @@ void che_off::read_file(const string & file) VN[v] = { x, y, z }; } } - + if(soff[0] == 'C' || soff[1] == 'C') { #pragma omp parallel for @@ -100,7 +100,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t os << off << endl; os << mesh->n_vertices << " " << (pointcloud ? 0 : mesh->n_faces) << " 0" << endl; - + for(size_t v = 0; v < mesh->n_vertices; ++v) { os << mesh->gt(v); @@ -109,7 +109,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t os << endl; } - + if(!pointcloud) for(index_t he = 0; he < mesh->n_half_edges; ) { diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 7ac6f274..24c1042d 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -41,17 +41,17 @@ void che_ply::read_file(const string & file) size_t n_v = 0, n_f = 0, b; string str, format, element; - + ifstream is(file); assert(is.good()); - + size_t xyz = 0, vbytes = 0; size_t fn = 0, fbytes = 0; while(getline(is, str) && str != "end_header") { stringstream ss(str); - + str = ""; ss >> str; @@ -61,7 +61,7 @@ void che_ply::read_file(const string & file) if(element == "vertex") ss >> n_v; if(element == "face") ss >> n_f; } - + if(str == "property" && element == "vertex") { ss >> str; @@ -71,7 +71,7 @@ void che_ply::read_file(const string & file) if(str == "x" || str == "y" || str == "z") xyz = b; } - + if(str == "property" && element == "face") { ss >> str; @@ -84,25 +84,25 @@ void che_ply::read_file(const string & file) if(str == "format") ss >> format; } - + alloc(n_v, n_f); - + if(format == "ascii") { for(index_t v = 0; v < n_vertices; ++v) { getline(is, str); stringstream ss(str); - + ss >> GT[v]; } - + index_t p, he = 0; while(n_f--) { getline(is, str); stringstream ss(str); - + ss >> p; while(p--) ss >> VT[he++]; @@ -152,7 +152,7 @@ void che_ply::read_file(const string & file) if(fn == 1) p = *((char *) vbuffer); if(fn == 2) p = *((short *) vbuffer); if(fn == 4) p = *((int *) vbuffer); - + while(p--) { is.read(vbuffer, fbytes); @@ -173,7 +173,7 @@ void che_ply::read_file(const string & file) void che_ply::write_file(const che * mesh, const string & file) { ofstream os(file + ".ply"); - + os << "ply" << endl; os << "format ascii 1.0" << endl; os << "comment generated by gproshan 2020" << endl; diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 182a6975..038d9bbe 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -33,19 +33,19 @@ void che_ptx::read_file(const string & file) float T[12], R[12], tr[4]; fscanf(fp, "%lu %lu", &n_rows, &n_cols); - + for(index_t i = 0; i < 12; ++i) fscanf(fp, "%f", T + i); for(index_t i = 0; i < 12; ++i) fscanf(fp, "%f", R + i); - + for(index_t i = 0; i < 4; ++i) fscanf(fp, "%f", tr + i); - + alloc(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); - + float x, y, z, a, r, g, b; char line[128]; @@ -55,7 +55,7 @@ void che_ptx::read_file(const string & file) fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); rgb = sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b) == 7; - + if(rgb) { GT[0] = { x, y, z }; @@ -91,11 +91,11 @@ void che_ptx::read_file(const string & file) { if(GT[i].is_zero() || GT[j].is_zero() || GT[k].is_zero()) return; - + VT[he++] = i; VT[he++] = j; VT[he++] = k; - + if(pdetriq(trig(he - 1)) < 0.1) he -= 3; }; @@ -106,7 +106,7 @@ void che_ptx::read_file(const string & file) add_trig((c ) + (r ) * n_cols, (c ) + (r + 1) * n_cols, (c + 1) + (r ) * n_cols); - + add_trig((c + 1) + (r + 1) * n_cols, (c + 1) + (r ) * n_cols, (c ) + (r + 1) * n_cols); @@ -117,7 +117,7 @@ void che_ptx::read_file(const string & file) #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) - VC[i] /= 255; + VC[i] /= 255; } void che_ptx::write_file(const che * mesh, const string & file) diff --git a/src/mesh/che_sphere.cpp b/src/mesh/che_sphere.cpp index 5a6997d1..70eca457 100644 --- a/src/mesh/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -25,18 +25,18 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta; phi += delta) for(real_t theta = delta; theta < M_PI - 0.5 * delta; theta += delta) vertices.push_back({r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)}); - + vertices.push_back({0, 0, r}); vertices.push_back({0, 0, -r}); size_t v, cols = n - 1; - + for(index_t i = 0; i < 2 * n - 1; ++i) { for(index_t j = 0; j < cols - 1; ++j) { v = i * cols + j; - + faces.push_back(v); faces.push_back(v + 1); faces.push_back(v + cols); @@ -50,17 +50,17 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) faces.push_back(vertices.size() - 2); faces.push_back(v); faces.push_back(v + cols); - + v = (i + 1) * cols - 1; faces.push_back(vertices.size() - 1); faces.push_back(v + cols); faces.push_back(v); } - + for(index_t j = 0; j < cols - 1; ++j) { v = (2 * n - 1) * cols + j; - + faces.push_back(v + 1); faces.push_back(j); faces.push_back(v); @@ -69,12 +69,12 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) faces.push_back(j); faces.push_back(v + 1); } - + v = (2 * n - 1) * cols; faces.push_back(vertices.size() - 2); faces.push_back(v); faces.push_back(0); - + v = (2 * n) * cols - 1; faces.push_back(vertices.size() - 1); faces.push_back(cols - 1); diff --git a/src/mesh/kdtree.cpp b/src/mesh/kdtree.cpp index 7838a8f8..771e90d0 100644 --- a/src/mesh/kdtree.cpp +++ b/src/mesh/kdtree.cpp @@ -21,7 +21,7 @@ kdtree::~kdtree() void kdtree::build(const index_t & n, const vertex * pc, const index_t & i, const index_t & j, const index_t & d) { - + if(i == j) { nodes[i] = i; diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 4bef3997..b5aff81b 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -33,7 +33,7 @@ bool raytracing::rt_restart(const size_t & w, const size_t & h) return true; } - + if(width != w || height != h) { width = w; @@ -77,7 +77,7 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, { //row major glm::vec4 & color = img[j * width + i]; - + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, (float(j) + randf(gen)) / height ); @@ -102,7 +102,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; - + std::default_random_engine gen; std::uniform_real_distribution randf(0.f, 1.f); @@ -115,7 +115,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; - + for(index_t s = 0; s < samples; ++s) { glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 82cc24eb..62f0021d 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -49,7 +49,7 @@ glm::vec3 embree::ray_hit::dir() const glm::vec3 embree::ray_hit::color(const rt_mesh & mesh) const { const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : - mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::vec3(c.x, c.y, c.z); } @@ -57,8 +57,8 @@ glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const { if(flat || mesh.pointcloud) return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); - - const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + + const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); return glm::normalize(glm::vec3(n.x, n.y, n.z)); } @@ -79,7 +79,7 @@ embree::embree() { device = rtcNewDevice(NULL); scene = rtcNewScene(device); - + rtcInitIntersectContext(&intersect_context); rtcSetDeviceErrorFunction(device, embree_error, NULL); } @@ -128,10 +128,10 @@ index_t embree::add_sphere(const glm::vec4 & xyzr) *pxyzr = xyzr; rtcCommitGeometry(geom); - + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - + return geom_id; } @@ -155,10 +155,10 @@ index_t embree::add_mesh(const che * mesh) memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); - + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - + return geom_id; } @@ -172,14 +172,14 @@ index_t embree::add_pointcloud(const che * mesh) 4 * sizeof(float), mesh->n_vertices ); - + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), mesh->n_vertices ); - + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { @@ -188,10 +188,10 @@ index_t embree::add_pointcloud(const che * mesh) } rtcCommitGeometry(geom); - + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - + return geom_id; } @@ -205,7 +205,7 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 do { glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); - + sum_w += w = pc_radius - glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); position += w * r.position(); normal += w * r.normal(geomID_mesh[r.hit.geomID]); @@ -227,23 +227,23 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const const glm::vec3 wi = normalize(light - position); const float dot_wi_normal = glm::dot(wi, normal); const glm::vec4 L = glm::vec4((dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); - + ray_hit r(position, wi, near); - return (occluded(r) ? 0.6f : 1.f) * L; + return (occluded(r) ? 0.6f : 1.f) * L; } glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) { float total_tfar = 0; - + float near; glm::vec3 position, normal, color; - + glm::vec4 L(0); // while(total_tfar < 0.1) { total_tfar += r.ray.tfar; - + position = r.position(); normal = r.normal(geomID_mesh[r.hit.geomID], flat); color = r.color(geomID_mesh[r.hit.geomID]); @@ -251,9 +251,9 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) near = 1e-5f; if(geomID_mesh[r.hit.geomID].pointcloud) near += pointcloud_hit(position, normal, color, r); - + L += r.ray.tfar * li(light, position, normal, color, near); - + r = ray_hit(r.position(), r.dir()); // if(!intersect(r)) // break; diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index 87fbe44e..c15b5645 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -29,14 +29,14 @@ index_t embree_splat::add_pointcloud(const che * mesh) 4 * sizeof(float), vsplat.size() ); - + glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), vsplat.size() ); - + #pragma omp parallel for for(index_t i = 0; i < vsplat.size(); ++i) { @@ -45,10 +45,10 @@ index_t embree_splat::add_pointcloud(const che * mesh) } rtcCommitGeometry(geom); - + index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); - + return geom_id; } @@ -70,7 +70,7 @@ float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm if(intersect(r)) return pointcloud_hit(position, normal, color, r); } - + return 1e-2f; } @@ -80,12 +80,12 @@ void embree_splat::init_splats(const che * mesh) vsplat.resize((mesh->n_vertices + n - 1) / n); gproshan_log_var(vsplat.size()); - + #pragma omp parallel for for(index_t i = 0; i < vsplat.size(); ++i) { const index_t v = n * i; // random, feature aware index - + std::set points; std::queue q; @@ -107,10 +107,10 @@ void embree_splat::init_splats(const che * mesh) q.pop(); } - + real_t dist, d; const vertex & c = mesh->gt(v); - + splat & s = vsplat[i]; for(index_t j = 0; j < K; ++j) { diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 5f56540f..5c60f51e 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -23,7 +23,7 @@ void optix_log(unsigned int level, const char * tag, const char * message, void optix::optix(const std::vector & meshes) { optixInit(); - + // create context cudaStreamCreate(&stream); @@ -54,11 +54,11 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_launch_params"; optix_pipeline_link_opt.maxTraceDepth = 2; - + std::ifstream ptx_is("rt_optix.ptx"); const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); - + optixModuleCreateFromPTX( optix_context, &optix_module_compile_opt, &optix_pipeline_compile_opt, @@ -88,7 +88,7 @@ optix::~optix() OptixTraversableHandle optix::build_as(const std::vector & meshes) { OptixTraversableHandle optix_as_handle = {}; - + std::vector optix_meshes(meshes.size()); std::vector optix_trig_flags(meshes.size()); @@ -107,7 +107,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) optix_meshes.size(), &optix_gas_buffer_size ); - + void * d_compacted_size; cudaMalloc(&d_compacted_size, sizeof(uint64_t)); @@ -117,10 +117,10 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) void * d_temp_buffer; cudaMalloc(&d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes); - + void * d_output_buffer; cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); - + optixAccelBuild( optix_context, 0, // stream &optix_accel_opt, @@ -134,7 +134,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) &optix_emit_desc, 1 ); - + cudaDeviceSynchronize(); gproshan_error_var(optix_gas_buffer_size.tempSizeInBytes); @@ -162,7 +162,7 @@ gproshan_error_var(compacted_size); cudaFree(d_output_buffer); cudaFree(d_temp_buffer); cudaFree(d_compacted_size); - + return optix_as_handle; } @@ -178,16 +178,16 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, #else glm::vec3 * vertices = new glm::vec3[mesh->n_vertices]; cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(float) * 3); - + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); - + cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); delete [] vertices; #endif // SINGLE_P - + cudaMalloc(&d_index, mesh->n_half_edges * sizeof(index_t)); cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t), cudaMemcpyHostToDevice); @@ -205,7 +205,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; optix_trig_flags = 0; - + optix_mesh.triangleArray.flags = &optix_trig_flags; optix_mesh.triangleArray.numSbtRecords = 1; optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index c29ddd2f..e6aca091 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -44,7 +44,7 @@ void camera::mouse(int , int state, int x, int y, int w, int h) if(state == GLFW_PRESS) p_click = p_drag = p_last = click_to_sphere(x, y, w, h); - + if(state == GLFW_RELEASE) { double timeSinceDrag = (clock() - t_last) / (double) CLOCKS_PER_SEC; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 219e44f3..483b8f30 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -31,7 +31,7 @@ void che_viewer::init(che * mesh, const bool & normalize) { glGenVertexArrays(1, &vao); glGenBuffers(6, vbo); - + this->mesh = mesh; this->normalize = normalize; @@ -47,14 +47,14 @@ void che_viewer::reload() void che_viewer::update() { if(normalize) mesh->normalize(); - + vertex pmin(INFINITY, INFINITY, INFINITY); vertex pmax(0, 0, 0); for(index_t v = 0; v < mesh->n_vertices; ++v) { const vertex & p = mesh->gt(v); - + pmin.x = min(pmin.x, p.x); pmin.y = min(pmin.y, p.y); pmin.z = min(pmin.z, p.z); @@ -63,11 +63,11 @@ void che_viewer::update() pmax.y = max(pmax.y, p.y); pmax.z = max(pmax.z, p.z); } - + translate(-(pmax + pmin) / 2); - + factor = mesh->mean_edge(); - + mesh->update_normals(); mesh->update_heatmap(); @@ -98,7 +98,7 @@ void che_viewer::update_vbo() glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - + // 3 HEAT MAP COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); @@ -113,12 +113,12 @@ void che_viewer::update_vbo() glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - + glBindVertexArray(0); } void che_viewer::update_instances_translations(const vector & translations) -{ +{ n_instances = translations.size(); if(!n_instances) return; @@ -147,7 +147,7 @@ void che_viewer::draw(shader & program) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); else glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); - + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -160,9 +160,9 @@ void che_viewer::draw_point_cloud(shader & program) glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - + glDrawArrays(GL_POINTS, 0, mesh->n_vertices); - + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 8eb9e5a4..481b6c2d 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -13,7 +13,7 @@ frame::frame() program.load_vertex(shaders_path("vertex_frame.glsl")); program.load_fragment(shaders_path("fragment_frame.glsl")); - + const GLfloat vertices[] = { -1, -1, 0, @@ -27,7 +27,7 @@ frame::frame() glGenTextures(1, &render_tex); glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); - + glBindVertexArray(vao); @@ -43,7 +43,7 @@ frame::frame() glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - + glBindVertexArray(0); } @@ -57,7 +57,7 @@ frame::~frame() void frame::display(const int & width, const int & height, void * buffer) { program.enable(); - + glBindVertexArray(vao); glActiveTexture(GL_TEXTURE0); @@ -70,7 +70,7 @@ void frame::display(const int & width, const int & height, void * buffer) glBindBuffer(GL_ARRAY_BUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(0); - + program.disable(); } diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 9628dd1b..362d16c1 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -24,7 +24,7 @@ const GLint & shader::operator () (const string & name) { if(uniform.find(name) != uniform.end()) uniform[name] = glGetUniformLocation(program, name.c_str()); - + return uniform[name]; } @@ -87,7 +87,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) glShaderSource(shader, 1, &(source_c_str), &size); glCompileShader(shader); - + GLint compile_status; glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); @@ -112,7 +112,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) delete[] infoLog; } - + glDeleteShader(shader); return false; } @@ -129,12 +129,12 @@ bool shader::read_source(const std::string & filename, std::string & source) return false; source = ""; - + string line, include; while(getline(is, line)) { stringstream ss(line); - + ss >> include; if(include == "#include") { @@ -145,7 +145,7 @@ bool shader::read_source(const std::string & filename, std::string & source) else source += line + '\n'; } - + is.close(); return true; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 711c8a48..df6d43a1 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -52,11 +52,11 @@ viewer::viewer(int width, int height): window_width(width), window_height(height init_gl(); init_glsl(); init_imgui(); - init_menus(); - + init_menus(); + info_gl(); - - sphere.init(new che_sphere(0.01), false); + + sphere.init(new che_sphere(0.01), false); } viewer::~viewer() @@ -81,7 +81,7 @@ bool viewer::run() eye = vertex(0., 0., -2. * cam.zoom); center = vertex(0., 0., 0.); up = vertex(0., 1., 0.); - + light = vertex(-1., 1., -2.); quaternion r = cam.current_rotation(); @@ -89,27 +89,27 @@ bool viewer::run() eye = r.conj() * eye * r; light = r.conj() * light * r; up = r.conj() * up * r; - + proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), glm::vec3(center[1], center[2], center[3]), glm::vec3(up[1], up[2], up[3]) ); - + // RENDER - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { case R_GL: render_gl(); break; case R_EMBREE: render_embree(); break; case R_OPTIX: render_optix(); break; } - + ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - + if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) @@ -117,7 +117,7 @@ bool viewer::run() for(index_t i = 0; i < n_meshes; ++i) if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { - idx_active_mesh = i; + idx_active_mesh = i; sphere_translations.clear(); glfwSetWindowTitle(window, active_mesh()->filename.c_str()); } @@ -149,10 +149,10 @@ bool viewer::run() ImGui::EndMenu(); } } - + ImGui::EndMainMenuBar(); } - + ImGui::SetNextWindowSize(ImVec2(320, -1)); ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); ImGui::Begin("gproshan"); @@ -176,7 +176,7 @@ bool viewer::run() ImGui::PopID(); } } - + ImGui::End(); // Rendering @@ -224,7 +224,7 @@ void viewer::init_gl() glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif - + window = glfwCreateWindow(window_width, window_height, "gproshan", NULL, NULL); glfwSetWindowUserPointer(window, this); @@ -252,7 +252,7 @@ void viewer::init_imgui() IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void) io; - + ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(window, true); @@ -299,15 +299,15 @@ void viewer::init_glsl() shader_triangles.load_vertex(shaders_path("vertex.glsl")); shader_triangles.load_geometry(shaders_path("geometry.glsl")); shader_triangles.load_fragment(shaders_path("fragment.glsl")); - + shader_normals.load_vertex(shaders_path("vertex_normals.glsl")); shader_normals.load_geometry(shaders_path("geometry_normals.glsl")); shader_normals.load_fragment(shaders_path("fragment_normals.glsl")); - + shader_gradient.load_vertex(shaders_path("vertex_gradient.glsl")); shader_gradient.load_geometry(shaders_path("geometry_gradient.glsl")); shader_gradient.load_fragment(shaders_path("fragment_gradient.glsl")); - + shader_pointcloud.load_vertex(shaders_path("vertex_pointcloud.glsl")); shader_pointcloud.load_fragment(shaders_path("fragment_pointcloud.glsl")); } @@ -334,10 +334,10 @@ void viewer::add_mesh(che * p_mesh) meshes[n_meshes].init(p_mesh); meshes[n_meshes].log_info(); ++n_meshes; - + idx_active_mesh = n_meshes - 1; glfwSetWindowTitle(window, active_mesh()->filename.c_str()); - + const int & rows = m_window_size[n_meshes][0]; const int & cols = m_window_size[n_meshes][1]; for(index_t m = 0; m < n_meshes; ++m) @@ -368,13 +368,13 @@ void viewer::window_size_callback(GLFWwindow * window, int width, int height) void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods) { if(action == GLFW_RELEASE) return; - + if(key == GLFW_KEY_ESCAPE) { glfwSetWindowShouldClose(window, GLFW_TRUE); return; } - + viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureKeyboard) return; @@ -385,10 +385,10 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int a void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - + double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - + if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); else view->cam.mouse(button, action, xpos, ypos, view->window_width, view->window_height); @@ -401,7 +401,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; - + view->cam.motion(x, y, view->window_width, view->window_height); view->action = true; } @@ -411,7 +411,7 @@ void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset { viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; - + if(yoffset > 0) { view->cam.zoom_in(); @@ -430,7 +430,7 @@ bool viewer::menu_help(viewer * view) for(auto & p: view->processes) if(p.second.function != nullptr) fprintf(stderr, "%16s: %s\n", ('[' + p.second.key + ']').c_str(), p.second.name.c_str()); - + return false; } @@ -440,26 +440,26 @@ bool viewer::menu_save_load_view(viewer * view) static char file[128] = "new_view"; - + ImGui::InputText("##savefile", file, sizeof(file)); - + ImGui::SameLine(); - + if(ImGui::Button("Save")) { ofstream os(tmp_file_path("views/" + file)); os << view->cam; os.close(); } - + static index_t select = 0; static vector vfiles; - + vfiles.clear(); for(auto & p: filesystem::directory_iterator(tmp_file_path("views/"))) vfiles.push_back(p.path().string()); - + if(!vfiles.size()) return true; if(ImGui::BeginCombo("##loadfile", vfiles[select].c_str())) @@ -475,9 +475,9 @@ bool viewer::menu_save_load_view(viewer * view) ImGui::EndCombo(); } - + ImGui::SameLine(); - + if(ImGui::Button("Load")) { ifstream is(vfiles[select]); @@ -509,7 +509,7 @@ bool viewer::menu_save_mesh(viewer * view) static char file[128] = "copy"; static int format = 0; static bool pc = false; - + ImGui::InputText("file", file, sizeof(file)); ImGui::Combo("format", &format, ".off\0.obj\0.ply\0\0"); ImGui::Checkbox("point cloud", &pc); @@ -534,7 +534,7 @@ bool viewer::menu_zoom_in(viewer * view) bool viewer::menu_zoom_out(viewer * view) { view->cam.zoom_out(); - + return false; } @@ -609,7 +609,7 @@ bool viewer::set_render_embree(viewer * view) { delete view->rt_embree; view->rt_embree = nullptr; - + view->render_opt = R_GL; } } @@ -624,42 +624,42 @@ bool viewer::set_render_embree(viewer * view) bool viewer::set_render_optix(viewer * view) { view->render_opt = 2; - + return false; } bool viewer::set_render_pointcloud(viewer * view) { view->render_pointcloud = !view->render_pointcloud; - + return false; } bool viewer::set_render_wireframe(viewer * view) { view->render_wireframe = !view->render_wireframe; - + return false; } bool viewer::set_render_wireframe_fill(viewer * view) { view->render_wireframe_fill = !view->render_wireframe_fill; - + return false; } bool viewer::set_render_gradient_field(viewer * view) { view->render_gradient_field = !view->render_gradient_field; - + return false; } bool viewer::set_render_normal_field(viewer * view) { view->render_normal_field = !view->render_normal_field; - + return false; } @@ -667,14 +667,14 @@ bool viewer::set_render_border(viewer * view) { view->render_border = !view->render_border; if(!view->render_border) view->active_mesh().selected.clear(); - + return false; } bool viewer::set_render_lines(viewer * view) { view->render_lines = !view->render_lines; - + return false; } @@ -682,7 +682,7 @@ bool viewer::set_render_flat(viewer * view) { view->render_flat = !view->render_flat; view->action = true; - + return false; } @@ -693,9 +693,9 @@ bool viewer::raycasting(viewer * view) gproshan_log(VIEWER); rt::embree rc({view->active_mesh()}); - + float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), - view->view_mat, view->proj_mat + view->view_mat, view->proj_mat ); std::thread([](CImg img) @@ -707,7 +707,7 @@ bool viewer::raycasting(viewer * view) delete [] frame; #endif // GPROSHAN_EMBREE - + return false; } @@ -745,21 +745,21 @@ void viewer::render_gl() glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom * 0.02); glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - + draw_meshes(shader_normals, true); } - - + + if(render_gradient_field) { glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom * 0.02); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); - + draw_meshes(shader_gradient); } - - + + if(render_border) select_border_vertices(); draw_selected_vertices(shader_sphere); @@ -783,10 +783,10 @@ void viewer::render_embree() rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, render_flat, action ); - + action = false; - if(!render_frame) render_frame = new frame; + if(!render_frame) render_frame = new frame; render_frame->display(viewport_width, viewport_height, rt_embree->img); #endif // GPROSHAN_EMBREE @@ -809,10 +809,10 @@ void viewer::render_optix() rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); - + action = false; - - if(!render_frame) render_frame = new frame; + + if(!render_frame) render_frame = new frame; render_frame->display(viewport_width, viewport_height, rt_optix->img); #endif // GPROSHAN_OPTIX @@ -850,8 +850,8 @@ void viewer::draw_selected_vertices(shader & program) sphere.update_instances_translations(sphere_translations); } - - + + if(sphere_translations.size()) { glViewport(active_mesh().vx * viewport_width, active_mesh().vy * viewport_height, viewport_width, viewport_height); From bb6f7a98ced558394aa0aa181254ef7b350c6f9f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 16 Mar 2021 23:56:03 +0100 Subject: [PATCH 0465/1018] app_viewer: add 2d convex hull process --- include/app_viewer.h | 18 +++++++++++++----- src/app_viewer.cpp | 37 ++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index cb752fe9..286ddd77 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -50,10 +50,12 @@ class app_viewer : public viewer virtual void init(); che * load_mesh(const string & file_path); - + + // Fairing static bool process_fairing_taubin(viewer * p_view); static bool process_fairing_spectral(viewer * p_view); + // Geodesics static bool process_geodesics(viewer * p_view, const geodesics::algorithm & alg); static bool process_geodesics_fm(viewer * p_view); static bool process_geodesics_ptp_cpu(viewer * p_view); @@ -66,39 +68,45 @@ class app_viewer : public viewer static bool process_farthest_point_sampling(viewer * p_view); static bool process_farthest_point_sampling_radio(viewer * p_view); - static bool compute_toplesets(viewer * p_view); + static bool process_compute_toplesets(viewer * p_view); static bool process_voronoi(viewer * p_view); + + // Geometry + static bool process_convex_hull(viewer * p_view); + // Mesh Sparse Coding static bool process_msparse_coding(viewer * p_view); static bool process_mdict_patch(viewer * p_view); static bool process_mask(viewer * p_view); static bool process_pc_reconstruction(viewer * p_view); + // Features static bool process_functional_maps(viewer * p_view); - static bool descriptor_heatmap(viewer * p_view, const descriptor::signature & sig); + static bool process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig); static bool process_gps(viewer * p_view); static bool process_hks(viewer * p_view); static bool process_wks(viewer * p_view); static bool process_key_points(viewer * p_view); static bool process_key_components(viewer * p_view); + // static bool process_poisson(viewer * p_view, const index_t & k); static bool process_poisson_laplacian_1(viewer * p_view); static bool process_poisson_laplacian_2(viewer * p_view); static bool process_poisson_laplacian_3(viewer * p_view); + static bool process_fill_holes(viewer * p_view); static bool process_threshold(viewer * p_view); static bool process_noise(viewer * p_view); static bool process_black_noise(viewer * p_view); static bool process_multiplicate_vertices(viewer * p_view); - static bool process_fill_holes(viewer * p_view); static bool process_delete_vertices(viewer * p_view); static bool process_fill_holes_test(viewer * p_view); static bool process_delete_non_manifold_vertices(viewer * p_view); static bool process_fill_holes_biharmonic_splines(viewer * p_view); static bool process_gaussian_curvature(viewer * p_view); static bool process_edge_collapse(viewer * p_view); - static bool select_multiple(viewer * p_view); + static bool process_select_multiple(viewer * p_view); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c37f0679..94751d37 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -58,6 +58,14 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { + sub_menus.push_back("Geometry"); + add_process(GLFW_KEY_H, {"H", "Convex Hull", process_convex_hull}); + add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); + add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); + add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); + add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); + add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); + sub_menus.push_back("Fairing"); add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); @@ -66,7 +74,6 @@ void app_viewer::init() add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_method}); - #ifdef GPROSHAN_CUDA add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_method_gpu}); @@ -75,7 +82,7 @@ void app_viewer::init() add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); add_process(GLFW_KEY_R, {"R", "Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio}); add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); - add_process(GLFW_KEY_P, {"P", "Toplesets", compute_toplesets}); + add_process(GLFW_KEY_P, {"P", "Toplesets", process_compute_toplesets}); sub_menus.push_back("Sparse Coding"); add_process(GLFW_KEY_I, {"I", "Mesh Sparse Coding", process_msparse_coding}); @@ -83,7 +90,7 @@ void app_viewer::init() add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); - sub_menus.push_back("Signatures"); + sub_menus.push_back("Features"); add_process(GLFW_KEY_2, {"2", "GPS", process_gps}); add_process(GLFW_KEY_3, {"3", "HKS", process_hks}); add_process(GLFW_KEY_4, {"4", "WKS", process_wks}); @@ -99,15 +106,15 @@ void app_viewer::init() add_process(GLFW_KEY_B, {"B", "Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines}); sub_menus.push_back("Others"); + add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", process_select_multiple}); add_process(GLFW_KEY_SLASH, {"SLASH", "Threshold", process_threshold}); add_process(GLFW_KEY_N, {"N", "Noise", process_noise}); add_process(GLFW_KEY_COMMA, {"COMMA", "Black noise", process_black_noise}); - add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); - add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); - add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); - add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); - add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); - add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", select_multiple}); +} + +bool app_viewer::process_convex_hull(viewer * p_view) +{ + return true; } bool paint_holes_vertices(viewer * p_view) @@ -301,7 +308,7 @@ bool app_viewer::process_functional_maps(viewer * p_view) return true; } -bool app_viewer::descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) +bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -339,17 +346,17 @@ bool app_viewer::descriptor_heatmap(viewer * p_view, const descriptor::signature bool app_viewer::process_gps(viewer * p_view) { - return descriptor_heatmap(p_view, descriptor::GPS); + return process_descriptor_heatmap(p_view, descriptor::GPS); } bool app_viewer::process_hks(viewer * p_view) { - return descriptor_heatmap(p_view, descriptor::HKS); + return process_descriptor_heatmap(p_view, descriptor::HKS); } bool app_viewer::process_wks(viewer * p_view) { - return descriptor_heatmap(p_view, descriptor::WKS); + return process_descriptor_heatmap(p_view, descriptor::WKS); } bool app_viewer::process_key_points(viewer * p_view) @@ -561,7 +568,7 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) return false; } -bool app_viewer::compute_toplesets(viewer * p_view) +bool app_viewer::process_compute_toplesets(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; @@ -883,7 +890,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) return false; } -bool app_viewer::select_multiple(viewer * p_view) +bool app_viewer::process_select_multiple(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); From 2a933e207543e33fd65c6862237bdd505a44be2b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Mar 2021 21:25:20 +0100 Subject: [PATCH 0466/1018] app_viewer refactoring --- include/app_viewer.h | 30 +- src/app_viewer.cpp | 795 ++++++++++++++++++++++--------------------- 2 files changed, 418 insertions(+), 407 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 286ddd77..7177bf0f 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -50,7 +50,15 @@ class app_viewer : public viewer virtual void init(); che * load_mesh(const string & file_path); - + + // Geometry + static bool process_convex_hull(viewer * p_view); + static bool process_gaussian_curvature(viewer * p_view); + static bool process_edge_collapse(viewer * p_view); + static bool process_multiplicate_vertices(viewer * p_view); + static bool process_delete_vertices(viewer * p_view); + static bool process_delete_non_manifold_vertices(viewer * p_view); + // Fairing static bool process_fairing_taubin(viewer * p_view); static bool process_fairing_spectral(viewer * p_view); @@ -60,19 +68,14 @@ class app_viewer : public viewer static bool process_geodesics_fm(viewer * p_view); static bool process_geodesics_ptp_cpu(viewer * p_view); static bool process_geodesics_heat_method(viewer * p_view); - #ifdef GPROSHAN_CUDA static bool process_geodesics_ptp_gpu(viewer * p_view); static bool process_geodesics_heat_method_gpu(viewer * p_view); #endif // GPROSHAN_CUDA - static bool process_farthest_point_sampling(viewer * p_view); static bool process_farthest_point_sampling_radio(viewer * p_view); - static bool process_compute_toplesets(viewer * p_view); static bool process_voronoi(viewer * p_view); - - // Geometry - static bool process_convex_hull(viewer * p_view); + static bool process_compute_toplesets(viewer * p_view); // Mesh Sparse Coding static bool process_msparse_coding(viewer * p_view); @@ -89,24 +92,19 @@ class app_viewer : public viewer static bool process_key_points(viewer * p_view); static bool process_key_components(viewer * p_view); - // + // Hole Filling static bool process_poisson(viewer * p_view, const index_t & k); static bool process_poisson_laplacian_1(viewer * p_view); static bool process_poisson_laplacian_2(viewer * p_view); static bool process_poisson_laplacian_3(viewer * p_view); static bool process_fill_holes(viewer * p_view); + static bool process_fill_holes_biharmonic_splines(viewer * p_view); + // Others + static bool process_select_multiple(viewer * p_view); static bool process_threshold(viewer * p_view); static bool process_noise(viewer * p_view); static bool process_black_noise(viewer * p_view); - static bool process_multiplicate_vertices(viewer * p_view); - static bool process_delete_vertices(viewer * p_view); - static bool process_fill_holes_test(viewer * p_view); - static bool process_delete_non_manifold_vertices(viewer * p_view); - static bool process_fill_holes_biharmonic_splines(viewer * p_view); - static bool process_gaussian_curvature(viewer * p_view); - static bool process_edge_collapse(viewer * p_view); - static bool process_select_multiple(viewer * p_view); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 94751d37..3cbf6aa7 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -61,11 +61,11 @@ void app_viewer::init() sub_menus.push_back("Geometry"); add_process(GLFW_KEY_H, {"H", "Convex Hull", process_convex_hull}); add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); - add_process(GLFW_KEY_9, {"9", "Edge Collapse", process_edge_collapse}); - add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); + add_process(GLFW_KEY_8, {"8", "Edge Collapse", process_edge_collapse}); + add_process(GLFW_KEY_9, {"9", "Multiplicate", process_multiplicate_vertices}); add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); - + sub_menus.push_back("Fairing"); add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); @@ -73,12 +73,11 @@ void app_viewer::init() sub_menus.push_back("Geodesics"); add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); - add_process(GLFW_KEY_L, {"L", "Heat Method", process_geodesics_heat_method}); + add_process(GLFW_KEY_M, {"M", "Heat Method", process_geodesics_heat_method}); #ifdef GPROSHAN_CUDA add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_method_gpu}); #endif // GPROSHAN_CUDA - add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); add_process(GLFW_KEY_R, {"R", "Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio}); add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); @@ -87,22 +86,22 @@ void app_viewer::init() sub_menus.push_back("Sparse Coding"); add_process(GLFW_KEY_I, {"I", "Mesh Sparse Coding", process_msparse_coding}); add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); - add_process(GLFW_KEY_F13, {"F13", "MDICT Mask", process_mask}); - add_process(GLFW_KEY_NUM_LOCK , {"Numlock", "PC reconstruction", process_pc_reconstruction}); + add_process(GLFW_KEY_D, {"D", "MDICT Mask", process_mask}); + add_process(GLFW_KEY_L, {"L", "PC reconstruction", process_pc_reconstruction}); sub_menus.push_back("Features"); - add_process(GLFW_KEY_2, {"2", "GPS", process_gps}); - add_process(GLFW_KEY_3, {"3", "HKS", process_hks}); - add_process(GLFW_KEY_4, {"4", "WKS", process_wks}); - add_process(GLFW_KEY_5, {"5", "Functional Maps", process_functional_maps}); + add_process(GLFW_KEY_2, {"2", "Functional Maps", process_functional_maps}); + add_process(GLFW_KEY_3, {"3", "GPS", process_gps}); + add_process(GLFW_KEY_4, {"4", "HKS", process_hks}); + add_process(GLFW_KEY_5, {"5", "WKS", process_wks}); add_process(GLFW_KEY_6, {"6", "Key Points", process_key_points}); add_process(GLFW_KEY_7, {"7", "Key Components", process_key_components}); - sub_menus.push_back("Repair Holes"); + sub_menus.push_back("Hole Filling"); add_process(GLFW_KEY_X, {"X", "Poisson Membrane surface", process_poisson_laplacian_1}); add_process(GLFW_KEY_Y, {"Y", "Poisson Thin-plate surface", process_poisson_laplacian_2}); add_process(GLFW_KEY_Z, {"Z", "Poisson Minimum variation surface", process_poisson_laplacian_3}); - add_process(GLFW_KEY_H, {"H", "Fill hole - planar mesh", process_fill_holes}); + add_process(GLFW_KEY_A, {"A", "Fill hole - planar mesh", process_fill_holes}); add_process(GLFW_KEY_B, {"B", "Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines}); sub_menus.push_back("Others"); @@ -112,36 +111,99 @@ void app_viewer::init() add_process(GLFW_KEY_COMMA, {"COMMA", "Black noise", process_black_noise}); } + +// Geometry + bool app_viewer::process_convex_hull(viewer * p_view) { return true; } -bool paint_holes_vertices(viewer * p_view) +bool app_viewer::process_gaussian_curvature(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - size_t nv = mesh->n_vertices; + real_t g, g_max = -INFINITY, g_min = INFINITY; + vertex a, b; - mesh.update(); + a_vec gv(mesh->n_vertices); + + #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + g = 0; + for_star(he, mesh, v) + { + a = mesh->gt_vt(next(he)) - mesh->gt(v); + b = mesh->gt_vt(prev(he)) - mesh->gt(v); + g += acos((a,b) / (*a * *b)); + } + //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); + gv(v) = mesh->mean_curvature(v); + + g_max = max(g_max, gv(v)); + g_min = min(g_min, gv(v)); + } + + g = g_max - g_min; + gproshan_log_var(g); + gproshan_log_var(g_min); + gproshan_log_var(g_max); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - if(v >= nv) mesh->heatmap(v) = .25; + gv(v) = (gv(v) + g_min) / g; + + real_t gm = mean(gv); + real_t gs = var(gv); + + gproshan_debug_var(gm); + gproshan_debug_var(gs); + + auto f = [&](real_t x, real_t a = 4) -> real_t + { + if(x < gm - a * gs) return 0; + if(x > gm + a * gs) return 1; + return (x - gm) / (2 * a * gs) + 0.5; + }; + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + mesh->heatmap(v) = f(gv(v)); return false; } -bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) +bool app_viewer::process_edge_collapse(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - gproshan_debug(removing vertex); - mesh->remove_non_manifold_vertices(); - gproshan_debug(removing vertex); + index_t levels; + cin >> levels; + + TIC(view->time) simplification sampling(mesh, &mesh->normal(0), levels); TOC(view->time) + gproshan_debug_var(view->time); + + //if(view->n_meshes < 2) + // view->add_mesh(new che(*mesh)); + + return false; +} + +bool app_viewer::process_multiplicate_vertices(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + mesh->multiplicate_vertices(); + mesh.update(); + + mesh.log_info(); return false; } @@ -162,236 +224,268 @@ bool app_viewer::process_delete_vertices(viewer * p_view) return false; } -bool app_viewer::process_poisson(viewer * p_view, const index_t & k) +bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - size_t old_n_vertices = mesh->n_vertices; - delete [] fill_all_holes(mesh); - - TIC(view->time) poisson(mesh, old_n_vertices, k); TOC(view->time) - gproshan_log_var(view->time); - -// paint_holes_vertices(); - mesh.update(); + gproshan_debug(removing vertex); + mesh->remove_non_manifold_vertices(); + gproshan_debug(removing vertex); return false; } -bool app_viewer::process_poisson_laplacian_1(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - return process_poisson(p_view, 1); -} - -bool app_viewer::process_poisson_laplacian_2(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - return process_poisson(p_view, 2); -} -bool app_viewer::process_poisson_laplacian_3(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - return process_poisson(p_view, 3); -} +// Fairing -bool app_viewer::process_fill_holes(viewer * p_view) +bool app_viewer::process_fairing_spectral(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - fill_all_holes(mesh); + static int k = 100; + ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices / 6); - paint_holes_vertices(p_view); + if(ImGui::Button("Run")) + { + fairing_spectral fair(k); + fair.run(mesh); - return false; + mesh->set_vertices(fair.get_postions()); + mesh->update_normals(); + } + + return true; } -bool app_viewer::process_noise(viewer * p_view) +bool app_viewer::process_fairing_taubin(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - std::default_random_engine generator; - std::uniform_int_distribution d_mod_5(0, 4); - std::uniform_int_distribution d_mod_1000(0, 999); + static float step = 0.001; //cin >> step; + ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) + if(ImGui::Button("Run")) { - real_t r = real_t(d_mod_1000(generator)) / 200000; - mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); - } + fairing_taubin fair(step); + fair.run(mesh); - mesh->update_normals(); + mesh->set_vertices(fair.get_postions()); + mesh->update_normals(); + } - return false; + return true; } -bool app_viewer::process_black_noise(viewer * p_view) + +// Geodesics + +bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & alg) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - std::default_random_engine generator; - std::uniform_int_distribution d_mod_5(0, 4); - std::uniform_int_distribution d_mod_1000(0, 999); + if(!mesh.selected.size()) + mesh.selected.push_back(0); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - real_t r = real_t(d_mod_1000(generator)) / 200000; - mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); - } - mesh->update_normals(); + static vector dist; + + if(dist.size() != mesh->n_vertices) + dist.resize(mesh->n_vertices); + + geodesics::params params; + params.alg = alg; + params.dist_alloc = dist.data(); + + TIC(view->time) + geodesics G(mesh, mesh.selected, params); + TOC(view->time) + gproshan_log_var(view->time); + + mesh->update_heatmap(&G[0]); return false; } -bool app_viewer::process_threshold(viewer * p_view) +bool app_viewer::process_geodesics_fm(viewer * p_view) { gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + return process_geodesics(p_view, geodesics::FM); +} - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; +bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + return process_geodesics(p_view, geodesics::PTP_CPU); +} - return false; +bool app_viewer::process_geodesics_heat_method(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + return process_geodesics(p_view, geodesics::HEAT_METHOD); } -bool app_viewer::process_functional_maps(viewer * p_view) +#ifdef GPROSHAN_CUDA + +bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + return process_geodesics(p_view, geodesics::PTP_GPU); +} + +bool app_viewer::process_geodesics_heat_method_gpu(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + return process_geodesics(p_view, geodesics::HEAT_METHOD_GPU); +} + +#endif // GPROSHAN_CUDA + +bool app_viewer::process_farthest_point_sampling(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int K = 20; + static int n = 10; + static real_t radio; - ImGui::InputInt("eigenvectors", &K); + ImGui::SliderInt("samples", &n, 1, mesh->n_vertices / 6); + ImGui::Text("radio: %.3f", radio); if(ImGui::Button("Run")) { - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; - - TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) + TIC(view->time) + load_sampling(mesh.selected, radio, mesh, n); + TOC(view->time) gproshan_log_var(view->time); - - gproshan_log_var(K); - - K = K < N_MESHES ? K : N_MESHES; - for(index_t k = 0; k < N_MESHES; ++k) - { - if(k) view->add_mesh(new che(*mesh)); - view->idx_active_mesh = k; - - eigvec.col(k) -= eigvec.col(k).min(); - eigvec.col(k) /= eigvec.col(k).max(); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - view->active_mesh()->heatmap(v) = eigvec(v, k); - - view->active_mesh().update_vbo(); - } - - view->idx_active_mesh = 0; } return true; } -bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) +bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int K = 50; - static bool status = true; - ImGui::InputInt("eigenvectors", &K); - if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing features."); - - if(ImGui::Button("Run")) - { - descriptor features(sig, mesh, K); + gproshan_input(radio); + real_t radio; cin >> radio; - if(features) - { - status = true; +#ifdef GPROSHAN_CUDA // IMPLEMENT/REVIEW + double time_fps; - real_t max_s = 0; - #pragma omp parallel for reduction(max: max_s) - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - mesh->heatmap(v) = features(v); - max_s = max(max_s, mesh->heatmap(v)); - } + TIC(view->time) + radio = farthest_point_sampling_ptp_gpu(mesh, mesh.selected, time_fps, NIL, radio); + TOC(view->time) + gproshan_log_var(time_fps); +#endif // GPROSHAN_CUDA - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) /= max_s; - } - else status = false; - } + gproshan_log_var(radio); + gproshan_log_var(mesh.selected.size()); + gproshan_log_var(view->time); - return true; + return false; } -bool app_viewer::process_gps(viewer * p_view) +bool app_viewer::process_voronoi(viewer * p_view) { - return process_descriptor_heatmap(p_view, descriptor::GPS); -} + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); -bool app_viewer::process_hks(viewer * p_view) -{ - return process_descriptor_heatmap(p_view, descriptor::HKS); -} + geodesics::params params; + params.cluster = true; -bool app_viewer::process_wks(viewer * p_view) -{ - return process_descriptor_heatmap(p_view, descriptor::WKS); +#ifdef GPROSHAN_CUDA + params.alg = geodesics::PTP_GPU; +#endif + + TIC(view->time) + geodesics ptp(mesh, mesh.selected, params); + TOC(view->time) + + gproshan_log_var(view->time); + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + { + mesh->heatmap(i) = ptp.clusters[i]; + mesh->heatmap(i) /= mesh.selected.size() + 1; + } + + return false; } -bool app_viewer::process_key_points(viewer * p_view) +bool app_viewer::process_compute_toplesets(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - key_points kps(mesh); + if(!mesh.selected.size()) + mesh.selected.push_back(0); - mesh.selected.clear(); - mesh.selected.reserve(kps.size()); + index_t * toplesets = new index_t[mesh->n_vertices]; + index_t * sorted = new index_t[mesh->n_vertices]; + vector limites; + mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); - for(index_t i = 0; i < kps.size(); ++i) - mesh.selected.push_back(kps[i]); + size_t n_toplesets = limites.size() - 1; + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + if(toplesets[v] < n_toplesets) + mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); + } + + gproshan_debug_var(n_toplesets); + + delete [] toplesets; + delete [] sorted; return false; } -bool app_viewer::process_key_components(viewer * p_view) + +// Mesh Sparse Coding + +bool app_viewer::process_msparse_coding(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - key_points kps(mesh); - key_components kcs(mesh, kps, .25); + static msparse_coding::params params; + static size_t n = 12; - gproshan_debug_var(kcs); + assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = (real_t) kcs(v) / kcs; + ImGui_InputReal("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); + ImGui::InputScalar("basis", ImGuiDataType_U64, &n); + ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); + ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::Checkbox("learn", ¶ms.learn); - return false; + if(ImGui::Button("Run")) + { + basis_dct phi(n); + msparse_coding msc(mesh, &phi, params); + + real_t max_error = msc.execute(); + gproshan_log_var(max_error); + + mesh->update_heatmap(&msc[0]); + mesh->update_normals(); + } + + return true; } bool app_viewer::process_mdict_patch(viewer * p_view) @@ -447,39 +541,6 @@ bool app_viewer::process_mdict_patch(viewer * p_view) return false; } -bool app_viewer::process_msparse_coding(viewer * p_view) -{ - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - static msparse_coding::params params; - static size_t n = 12; - - assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - - ImGui_InputReal("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); - ImGui::InputScalar("basis", ImGuiDataType_U64, &n); - ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); - ImGui::Checkbox("learn", ¶ms.learn); - - if(ImGui::Button("Run")) - { - basis_dct phi(n); - msparse_coding msc(mesh, &phi, params); - - real_t max_error = msc.execute(); - gproshan_log_var(max_error); - - mesh->update_heatmap(&msc[0]); - mesh->update_normals(); - } - - return true; -} - bool app_viewer::process_mask(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; @@ -554,236 +615,204 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) return true; } -bool app_viewer::process_multiplicate_vertices(viewer * p_view) + +// Features + +bool app_viewer::process_functional_maps(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - mesh->multiplicate_vertices(); - mesh.update(); + static int K = 20; - mesh.log_info(); + ImGui::InputInt("eigenvectors", &K); - return false; -} + if(ImGui::Button("Run")) + { + a_sp_mat L, A; + a_vec eigval; + a_mat eigvec; -bool app_viewer::process_compute_toplesets(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) + gproshan_log_var(view->time); - if(!mesh.selected.size()) - mesh.selected.push_back(0); + gproshan_log_var(K); - index_t * toplesets = new index_t[mesh->n_vertices]; - index_t * sorted = new index_t[mesh->n_vertices]; - vector limites; - mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); + K = K < N_MESHES ? K : N_MESHES; + for(index_t k = 0; k < N_MESHES; ++k) + { + if(k) view->add_mesh(new che(*mesh)); + view->idx_active_mesh = k; - size_t n_toplesets = limites.size() - 1; + eigvec.col(k) -= eigvec.col(k).min(); + eigvec.col(k) /= eigvec.col(k).max(); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - if(toplesets[v] < n_toplesets) - mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); - } + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + view->active_mesh()->heatmap(v) = eigvec(v, k); - gproshan_debug_var(n_toplesets); + view->active_mesh().update_vbo(); + } - delete [] toplesets; - delete [] sorted; + view->idx_active_mesh = 0; + } - return false; + return true; } -bool app_viewer::process_voronoi(viewer * p_view) +bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - geodesics::params params; - params.cluster = true; + static int K = 50; + static bool status = true; + ImGui::InputInt("eigenvectors", &K); + if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing features."); -#ifdef GPROSHAN_CUDA - params.alg = geodesics::PTP_GPU; -#endif + if(ImGui::Button("Run")) + { + descriptor features(sig, mesh, K); - TIC(view->time) - geodesics ptp(mesh, mesh.selected, params); - TOC(view->time) + if(features) + { + status = true; - gproshan_log_var(view->time); + real_t max_s = 0; + #pragma omp parallel for reduction(max: max_s) + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + mesh->heatmap(v) = features(v); + max_s = max(max_s, mesh->heatmap(v)); + } - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - { - mesh->heatmap(i) = ptp.clusters[i]; - mesh->heatmap(i) /= mesh.selected.size() + 1; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + mesh->heatmap(v) /= max_s; + } + else status = false; } - return false; + return true; } -bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) +bool app_viewer::process_gps(viewer * p_view) { - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - gproshan_input(radio); - real_t radio; cin >> radio; - -#ifdef GPROSHAN_CUDA // IMPLEMENT/REVIEW - double time_fps; - - TIC(view->time) - radio = farthest_point_sampling_ptp_gpu(mesh, mesh.selected, time_fps, NIL, radio); - TOC(view->time) - gproshan_log_var(time_fps); -#endif // GPROSHAN_CUDA + return process_descriptor_heatmap(p_view, descriptor::GPS); +} - gproshan_log_var(radio); - gproshan_log_var(mesh.selected.size()); - gproshan_log_var(view->time); +bool app_viewer::process_hks(viewer * p_view) +{ + return process_descriptor_heatmap(p_view, descriptor::HKS); +} - return false; +bool app_viewer::process_wks(viewer * p_view) +{ + return process_descriptor_heatmap(p_view, descriptor::WKS); } -bool app_viewer::process_farthest_point_sampling(viewer * p_view) +bool app_viewer::process_key_points(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int n = 10; - static real_t radio; + key_points kps(mesh); - ImGui::SliderInt("samples", &n, 1, mesh->n_vertices / 6); - ImGui::Text("radio: %.3f", radio); + mesh.selected.clear(); + mesh.selected.reserve(kps.size()); - if(ImGui::Button("Run")) - { - TIC(view->time) - load_sampling(mesh.selected, radio, mesh, n); - TOC(view->time) - gproshan_log_var(view->time); - } + for(index_t i = 0; i < kps.size(); ++i) + mesh.selected.push_back(kps[i]); - return true; + return false; } -bool app_viewer::process_fairing_spectral(viewer * p_view) +bool app_viewer::process_key_components(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int k = 100; - ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices / 6); + key_points kps(mesh); + key_components kcs(mesh, kps, .25); - if(ImGui::Button("Run")) - { - fairing_spectral fair(k); - fair.run(mesh); + gproshan_debug_var(kcs); - mesh->set_vertices(fair.get_postions()); - mesh->update_normals(); - } + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + mesh->heatmap(v) = (real_t) kcs(v) / kcs; - return true; + return false; } -bool app_viewer::process_fairing_taubin(viewer * p_view) + +// Hole Filling + +bool paint_holes_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static float step = 0.001; //cin >> step; - ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); + size_t nv = mesh->n_vertices; - if(ImGui::Button("Run")) - { - fairing_taubin fair(step); - fair.run(mesh); + mesh.update(); - mesh->set_vertices(fair.get_postions()); - mesh->update_normals(); - } + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + if(v >= nv) mesh->heatmap(v) = .25; - return true; + return false; } -bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & alg) +bool app_viewer::process_poisson(viewer * p_view, const index_t & k) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - - static vector dist; - - if(dist.size() != mesh->n_vertices) - dist.resize(mesh->n_vertices); - - geodesics::params params; - params.alg = alg; - params.dist_alloc = dist.data(); + size_t old_n_vertices = mesh->n_vertices; + delete [] fill_all_holes(mesh); - TIC(view->time) - geodesics G(mesh, mesh.selected, params); - TOC(view->time) + TIC(view->time) poisson(mesh, old_n_vertices, k); TOC(view->time) gproshan_log_var(view->time); - mesh->update_heatmap(&G[0]); +// paint_holes_vertices(); + mesh.update(); return false; } -bool app_viewer::process_geodesics_fm(viewer * p_view) +bool app_viewer::process_poisson_laplacian_1(viewer * p_view) { gproshan_log(APP_VIEWER); - - return process_geodesics(p_view, geodesics::FM); + return process_poisson(p_view, 1); } -bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) +bool app_viewer::process_poisson_laplacian_2(viewer * p_view) { gproshan_log(APP_VIEWER); - - return process_geodesics(p_view, geodesics::PTP_CPU); + return process_poisson(p_view, 2); } -bool app_viewer::process_geodesics_heat_method(viewer * p_view) +bool app_viewer::process_poisson_laplacian_3(viewer * p_view) { gproshan_log(APP_VIEWER); - - return process_geodesics(p_view, geodesics::HEAT_METHOD); + return process_poisson(p_view, 3); } - -#ifdef GPROSHAN_CUDA - -bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) +bool app_viewer::process_fill_holes(viewer * p_view) { gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); - return process_geodesics(p_view, geodesics::PTP_GPU); -} + fill_all_holes(mesh); -bool app_viewer::process_geodesics_heat_method_gpu(viewer * p_view) -{ - gproshan_log(APP_VIEWER); + paint_holes_vertices(p_view); - return process_geodesics(p_view, geodesics::HEAT_METHOD_GPU); + return false; } -#endif // GPROSHAN_CUDA - - bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { gproshan_log(APP_VIEWER); @@ -815,99 +844,83 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) return false; } -bool app_viewer::process_gaussian_curvature(viewer * p_view) + +// Others + +bool app_viewer::process_select_multiple(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - real_t g, g_max = -INFINITY, g_min = INFINITY; - vertex a, b; + static char line[128] = ""; - a_vec gv(mesh->n_vertices); + ImGui::InputText("select", line, sizeof(line)); - #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) - for(index_t v = 0; v < mesh->n_vertices; ++v) + if(ImGui::Button("Add")) { - g = 0; - for_star(he, mesh, v) - { - a = mesh->gt_vt(next(he)) - mesh->gt(v); - b = mesh->gt_vt(prev(he)) - mesh->gt(v); - g += acos((a,b) / (*a * *b)); - } - //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); - gv(v) = mesh->mean_curvature(v); - - g_max = max(g_max, gv(v)); - g_min = min(g_min, gv(v)); + stringstream ss(line); + index_t v; + while(ss >> v) + mesh.selected.push_back(v); } - g = g_max - g_min; - gproshan_log_var(g); - gproshan_log_var(g_min); - gproshan_log_var(g_max); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - gv(v) = (gv(v) + g_min) / g; - - real_t gm = mean(gv); - real_t gs = var(gv); - - gproshan_debug_var(gm); - gproshan_debug_var(gs); + return true; +} - auto f = [&](real_t x, real_t a = 4) -> real_t - { - if(x < gm - a * gs) return 0; - if(x > gm + a * gs) return 1; - return (x - gm) / (2 * a * gs) + 0.5; - }; +bool app_viewer::process_threshold(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); - #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = f(gv(v)); + mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; return false; } -bool app_viewer::process_edge_collapse(viewer * p_view) +bool app_viewer::process_noise(viewer * p_view) { gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - index_t levels; - cin >> levels; + std::default_random_engine generator; + std::uniform_int_distribution d_mod_5(0, 4); + std::uniform_int_distribution d_mod_1000(0, 999); - TIC(view->time) simplification sampling(mesh, &mesh->normal(0), levels); TOC(view->time) - gproshan_debug_var(view->time); + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + real_t r = real_t(d_mod_1000(generator)) / 200000; + mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); + } - //if(view->n_meshes < 2) - // view->add_mesh(new che(*mesh)); + mesh->update_normals(); return false; } -bool app_viewer::process_select_multiple(viewer * p_view) +bool app_viewer::process_black_noise(viewer * p_view) { + gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static char line[128] = ""; - - ImGui::InputText("select", line, sizeof(line)); + std::default_random_engine generator; + std::uniform_int_distribution d_mod_5(0, 4); + std::uniform_int_distribution d_mod_1000(0, 999); - if(ImGui::Button("Add")) + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) { - stringstream ss(line); - index_t v; - while(ss >> v) - mesh.selected.push_back(v); + real_t r = real_t(d_mod_1000(generator)) / 200000; + mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } - return true; + mesh->update_normals(); + + return false; } From 14fa462f9b95079fdf98efe84f27418ba522a155 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Mar 2021 22:24:48 +0100 Subject: [PATCH 0467/1018] 2d convex_hull: add app viewer process --- include/app_viewer.h | 2 ++ include/geometry/convex_hull.h | 10 +++++----- include/mesh/vertex.h | 4 ++-- src/app_viewer.cpp | 11 +++++++++-- src/geometry/convex_hull.cpp | 34 ++++++++++++++++++++++------------ src/mesh/vertex.cpp | 4 ++-- 6 files changed, 42 insertions(+), 23 deletions(-) diff --git a/include/app_viewer.h b/include/app_viewer.h index 7177bf0f..3c0f2890 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -18,6 +18,8 @@ #include "laplacian/fairing_taubin.h" #include "laplacian/fairing_spectral.h" +#include "geometry/convex_hull.h" + #include "geodesics/dijkstra.h" #include "geodesics/geodesics.h" #include "geodesics/sampling.h" diff --git a/include/geometry/convex_hull.h b/include/geometry/convex_hull.h index ce83fd86..4a347181 100644 --- a/include/geometry/convex_hull.h +++ b/include/geometry/convex_hull.h @@ -14,15 +14,15 @@ namespace gproshan { class convex_hull { private: - std::vector CH; ///< convex hull points clockwise + std::vector CH; ///< convex hull points clockwise public: - convex_hull(std::vector & points); - convex_hull(vertex * points, const size_t & n_points); - operator const std::vector & (); + convex_hull(const std::vector & points); + convex_hull(const vertex * points, const size_t & n_points); + operator const std::vector & (); private: - void andrew_algorithm(vertex * points, const size_t & n_points); + void andrew_algorithm(const vertex * points, const size_t & n_points); bool ccw(const vertex & p, const vertex & q, const vertex & r); }; diff --git a/include/mesh/vertex.h b/include/mesh/vertex.h index e600fc0c..908c3a2b 100644 --- a/include/mesh/vertex.h +++ b/include/mesh/vertex.h @@ -42,8 +42,8 @@ class vertex void operator += (const vertex & v); void operator -= (const vertex & v); - bool operator < (const vertex & v); - bool operator == (const vertex & v); + bool operator < (const vertex & v) const; + bool operator == (const vertex & v) const; bool is_zero(); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 3cbf6aa7..654de30b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -59,7 +59,7 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { sub_menus.push_back("Geometry"); - add_process(GLFW_KEY_H, {"H", "Convex Hull", process_convex_hull}); + add_process(GLFW_KEY_H, {"H", "2D Convex Hull", process_convex_hull}); add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); add_process(GLFW_KEY_8, {"8", "Edge Collapse", process_edge_collapse}); add_process(GLFW_KEY_9, {"9", "Multiplicate", process_multiplicate_vertices}); @@ -116,7 +116,14 @@ void app_viewer::init() bool app_viewer::process_convex_hull(viewer * p_view) { - return true; + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + convex_hull ch(&mesh->gt(0), mesh->n_vertices); + mesh.selected = ch; + + return false; } bool app_viewer::process_gaussian_curvature(viewer * p_view) diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp index 18e8178f..3a437000 100644 --- a/src/geometry/convex_hull.cpp +++ b/src/geometry/convex_hull.cpp @@ -2,46 +2,56 @@ #include +#include // geometry processing and shape analysis framework namespace gproshan { -convex_hull::convex_hull(std::vector & points): convex_hull(points.data(), points.size()) {} +convex_hull::convex_hull(const std::vector & points): convex_hull(points.data(), points.size()) {} -convex_hull::convex_hull(vertex * points, const size_t & n_points) +convex_hull::convex_hull(const vertex * points, const size_t & n_points) { andrew_algorithm(points, n_points); } -convex_hull::operator const std::vector & () +convex_hull::operator const std::vector & () { return CH; } ///< Andrew's Convex Hull Algorithm: Competitive Programming 4 -void convex_hull::andrew_algorithm(vertex * points, const size_t & n_points) +void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_points) { - std::sort(points, points + n_points); + std::vector idx(n_points); + std::iota(idx.begin(), idx.end(), 0); + + std::sort(idx.begin(), idx.end(), + [&points](const index_t & i, const index_t & j) + { + return points[i] < points[j]; + }); CH.resize(2 * n_points); index_t k = 0; - for(index_t i = 0; i < n_points; ++i) + for(index_t p = 0; p < n_points; ++p) { - while(k > 1 && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; - CH[k++] = points[i]; + const index_t & i = idx[p]; + while(k > 1 && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; + CH[k++] = i; } index_t t = k; - for(index_t i = n_points - 2; i > 0; --i) + for(index_t p = n_points - 2; p > 0; --p) { - while(k > t && !ccw(CH[k - 2], CH[k - 1], points[i])) --k; - CH[k++] = points[i]; + const index_t & i = idx[p]; + while(k > t && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; + CH[k++] = i; } - while(k > t && !ccw(CH[k - 2], CH[k - 1], points[0])) --k; + while(k > t && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[idx[0]])) --k; CH.resize(k); } diff --git a/src/mesh/vertex.cpp b/src/mesh/vertex.cpp index 0ca0a3dc..00435f78 100644 --- a/src/mesh/vertex.cpp +++ b/src/mesh/vertex.cpp @@ -92,14 +92,14 @@ void vertex::operator -= (const vertex & v) z -= v.z; } -bool vertex::operator < (const vertex & v) +bool vertex::operator < (const vertex & v) const { if(x != v.x) return x < v.x; if(y != v.y) return y < v.y; return z < v.z; } -bool vertex::operator == (const vertex & v) +bool vertex::operator == (const vertex & v) const { return x == v.x && y == v.y && z == v.z; } From a618fd84b0a3b46a3d011725ade52906365cf4cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Mar 2021 22:50:10 +0100 Subject: [PATCH 0468/1018] che: update copy constructor --- src/mesh/che.cpp | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index cac9d059..b2453a89 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -52,37 +52,19 @@ CHE::CHE(che * mesh) che::che(const che & mesh) { - filename = mesh.filename; - rw(n_vertices) = mesh.n_vertices; - rw(n_faces) = mesh.n_faces; - rw(n_half_edges) = mesh.n_half_edges; - rw(n_edges) = mesh.n_edges; + filename = mesh.filename; + + alloc(mesh.n_vertices, mesh.n_faces); + rw(n_edges) = mesh.n_edges; - GT = new vertex[n_vertices]; memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); - - VT = new index_t[n_half_edges]; memcpy(VT, mesh.VT, n_half_edges * sizeof(index_t)); - - OT = new index_t[n_half_edges]; memcpy(OT, mesh.OT, n_half_edges * sizeof(index_t)); - - EVT = new index_t[n_vertices]; memcpy(EVT, mesh.EVT, n_vertices * sizeof(index_t)); - - ET = new index_t[n_edges]; memcpy(ET, mesh.ET, n_edges * sizeof(index_t)); - - EHT = new index_t[n_half_edges]; memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); - - VN = new vertex[n_vertices]; memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); - - VC = new vertex[n_vertices]; memcpy(VC, mesh.VC, n_vertices * sizeof(vertex)); - - VHC = new real_t[n_vertices]; memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); } From 681d70471358a35398fdec4e5ba5d8f4eae2bfdf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 31 Mar 2021 12:47:12 +0200 Subject: [PATCH 0469/1018] che_obj: fast reading --- src/mesh/che.cpp | 26 +++++++------ src/mesh/che_obj.cpp | 92 +++++++++++++++++++++++--------------------- src/mesh/che_off.cpp | 2 +- 3 files changed, 63 insertions(+), 57 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index b2453a89..1952c1b6 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1390,6 +1390,7 @@ void che::update_evt_ot_et() map * medges = new map[n_vertices]; + gproshan_error(OBJ); size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) { @@ -1397,17 +1398,17 @@ void che::update_evt_ot_et() const index_t & v = VT[next(he)]; EVT[u] = he; - - index_t & mhe = u < v ? medges[u][v] : medges[v][u]; - if(!mhe) - { - ET[ne++] = he; - mhe = he + 1; - } - else + + if(OT[he] == NIL) { - OT[he] = mhe - 1; - OT[OT[he]] = he; + index_t & ohe = medges[v][u]; + if(ohe) + { + ET[ne++] = he; + OT[he] = ohe - 1; + OT[ohe - 1] = he; + } + else medges[u][v] = he + 1; } } @@ -1415,9 +1416,10 @@ void che::update_evt_ot_et() delete [] medges; + gproshan_error(OBJ); // non manifold two disk - //for(index_t he = 0; he < n_half_edges; ++he) - // if(OT[he] != NIL) assert(he == OT[OT[he]]); +// for(index_t he = 0; he < n_half_edges; ++he) +// if(OT[he] != NIL && he != OT[OT[he]]) gproshan_error(OT); for(index_t he = 0; he < n_half_edges; ++he) if(OT[he] == NIL && EVT[VT[he]] != NIL) diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 316525f7..c6f1b335 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -1,10 +1,9 @@ #include "mesh/che_obj.h" -#include -#include -#include #include +#include #include +#include using namespace std; @@ -24,81 +23,86 @@ che_obj::che_obj(const che_obj & mesh): che(mesh) void che_obj::read_file(const string & file) { - ifstream is(file); - - assert(is.good()); + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); real_t x, y, z; - int face[8], i; + int v[16], vt[16], vn[16], f; vector vertices; - vector faces; + vector vs; - char line[256]; - string key; + char line[256], key[4]; + char * line_ptr; + int offset; - while(is.getline(line, sizeof(line))) + while(fgets(line, sizeof(line), fp)) { - stringstream ss(line); + key[0] = 0; + line_ptr = line; + + sscanf(line_ptr, "%s%n", key, &offset); + line_ptr += offset; - key = ""; - ss >> key; - - if(key == "v") + if(key[0] == 'v' && !key[1]) // v x y z { - ss >> x >> y >> z; + sscanf(line_ptr, "%f %f %f%n", &x, &y, &z, &offset); vertices.push_back({x, y, z}); } - if(key == "f") + if(key[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... { - for(i = 0; ss >> face[i]; ++i) - ss.ignore(256, ' '); + f = 0; + while(sscanf(line_ptr, "%d/%d/%d%n", v + f, vt + f, vn + f, &offset) > 0) + { + line_ptr += offset; + ++f; + } - if(i == che::mtrig) + if(f == che::mtrig) { - if(face[0] < 0) + if(v[0] < 0) { - faces.push_back(vertices.size() + face[0]); - faces.push_back(vertices.size() + face[1]); - faces.push_back(vertices.size() + face[2]); + vs.push_back(vertices.size() + v[0]); + vs.push_back(vertices.size() + v[1]); + vs.push_back(vertices.size() + v[2]); } else { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[2] - 1); + vs.push_back(v[0] - 1); + vs.push_back(v[1] - 1); + vs.push_back(v[2] - 1); } } - else if(i == che::mquad) + else if(f == che::mquad) { - if(face[0] < 0) + if(v[0] < 0) { - faces.push_back(vertices.size() + face[0]); - faces.push_back(vertices.size() + face[1]); - faces.push_back(vertices.size() + face[3]); + vs.push_back(vertices.size() + v[0]); + vs.push_back(vertices.size() + v[1]); + vs.push_back(vertices.size() + v[3]); - faces.push_back(vertices.size() + face[1]); - faces.push_back(vertices.size() + face[2]); - faces.push_back(vertices.size() + face[3]); + vs.push_back(vertices.size() + v[1]); + vs.push_back(vertices.size() + v[2]); + vs.push_back(vertices.size() + v[3]); } else { - faces.push_back(face[0] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[3] - 1); + vs.push_back(v[0] - 1); + vs.push_back(v[1] - 1); + vs.push_back(v[3] - 1); - faces.push_back(face[1] - 1); - faces.push_back(face[2] - 1); - faces.push_back(face[3] - 1); + vs.push_back(v[1] - 1); + vs.push_back(v[2] - 1); + vs.push_back(v[3] - 1); } } } } - is.close(); + fclose(fp); - init(vertices.data(), vertices.size(), faces.data(), faces.size() / che::mtrig); + init(vertices.data(), vertices.size(), vs.data(), vs.size() / che::mtrig); } void che_obj::write_file(const che * mesh, const string & file) diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 3ec76c5b..4c2c6604 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -1,8 +1,8 @@ #include "mesh/che_off.h" #include -#include #include +#include #include using namespace std; From 913a671f844ff26f699bf9d018f9aad845a7da72 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 31 Mar 2021 16:54:36 +0200 Subject: [PATCH 0470/1018] cleaning up che clases --- include/mesh/che_img.h | 2 -- include/mesh/che_obj.h | 2 -- include/mesh/che_off.h | 2 -- include/mesh/che_ply.h | 2 -- include/mesh/che_ptx.h | 2 -- include/mesh/che_sphere.h | 2 -- src/mesh/che.cpp | 4 ++-- src/mesh/che_img.cpp | 9 +------ src/mesh/che_obj.cpp | 50 +++++++++++++++++++-------------------- src/mesh/che_off.cpp | 9 +------ src/mesh/che_ply.cpp | 4 +--- src/mesh/che_ptx.cpp | 9 +------ src/mesh/che_sphere.cpp | 10 +------- src/viewer/viewer.cpp | 2 ++ 14 files changed, 34 insertions(+), 75 deletions(-) diff --git a/include/mesh/che_img.h b/include/mesh/che_img.h index a7c56a63..87f1af60 100644 --- a/include/mesh/che_img.h +++ b/include/mesh/che_img.h @@ -12,8 +12,6 @@ class che_img : public che { public: che_img(const std::string & file); - che_img(const che_img & mesh); - virtual ~che_img(); private: void read_file(const std::string & file); diff --git a/include/mesh/che_obj.h b/include/mesh/che_obj.h index 22d4c1d6..32c7b88e 100644 --- a/include/mesh/che_obj.h +++ b/include/mesh/che_obj.h @@ -12,8 +12,6 @@ class che_obj : public che { public: che_obj(const std::string & file); - che_obj(const che_obj & mesh); - virtual ~che_obj() = default; static void write_file(const che * mesh, const std::string & file); diff --git a/include/mesh/che_off.h b/include/mesh/che_off.h index 5ea0e6cf..d5a738d3 100644 --- a/include/mesh/che_off.h +++ b/include/mesh/che_off.h @@ -12,8 +12,6 @@ class che_off : public che { public: che_off(const std::string & file); - che_off(const che_off & mesh); - virtual ~che_off(); enum type { OFF, NOFF, COFF, CNOFF }; static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool & pointcloud = false); diff --git a/include/mesh/che_ply.h b/include/mesh/che_ply.h index 6a2400d6..d7919680 100644 --- a/include/mesh/che_ply.h +++ b/include/mesh/che_ply.h @@ -12,8 +12,6 @@ class che_ply : public che { public: che_ply(const std::string & file); - che_ply(const che_ply & mesh); - virtual ~che_ply() = default; static void write_file(const che * mesh, const std::string & file); diff --git a/include/mesh/che_ptx.h b/include/mesh/che_ptx.h index 8f6a1da8..85614893 100644 --- a/include/mesh/che_ptx.h +++ b/include/mesh/che_ptx.h @@ -12,8 +12,6 @@ class che_ptx : public che { public: che_ptx(const std::string & file); - che_ptx(const che_ptx & mesh); - virtual ~che_ptx(); static void write_file(const che * mesh, const std::string & file); diff --git a/include/mesh/che_sphere.h b/include/mesh/che_sphere.h index 3a1247e1..068a331d 100644 --- a/include/mesh/che_sphere.h +++ b/include/mesh/che_sphere.h @@ -12,8 +12,6 @@ class che_sphere : public che { public: che_sphere(const real_t & r = 1, const size_t & n = 6); - che_sphere(const che_sphere & mesh); - virtual ~che_sphere(); }; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 1952c1b6..c0cbbe08 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -53,7 +53,7 @@ CHE::CHE(che * mesh) che::che(const che & mesh) { filename = mesh.filename; - + alloc(mesh.n_vertices, mesh.n_faces); rw(n_edges) = mesh.n_edges; @@ -1398,7 +1398,7 @@ void che::update_evt_ot_et() const index_t & v = VT[next(he)]; EVT[u] = he; - + if(OT[he] == NIL) { index_t & ohe = medges[v][u]; diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index 488e1055..0bbea51a 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -8,6 +8,7 @@ #include + using namespace std; using namespace cimg_library; @@ -21,14 +22,6 @@ che_img::che_img(const string & file) init(file); } -che_img::che_img(const che_img & mesh): che(mesh) -{ -} - -che_img::~che_img() -{ -} - void che_img::read_file(const string & file) { CImg img(file.c_str()); diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index c6f1b335..3193ad58 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -5,6 +5,7 @@ #include #include + using namespace std; @@ -17,10 +18,6 @@ che_obj::che_obj(const string & file) init(file); } -che_obj::che_obj(const che_obj & mesh): che(mesh) -{ -} - void che_obj::read_file(const string & file) { FILE * fp = fopen(file.c_str(), "r"); @@ -30,7 +27,7 @@ void che_obj::read_file(const string & file) int v[16], vt[16], vn[16], f; vector vertices; - vector vs; + vector faces; char line[256], key[4]; char * line_ptr; @@ -40,7 +37,7 @@ void che_obj::read_file(const string & file) { key[0] = 0; line_ptr = line; - + sscanf(line_ptr, "%s%n", key, &offset); line_ptr += offset; @@ -63,38 +60,38 @@ void che_obj::read_file(const string & file) { if(v[0] < 0) { - vs.push_back(vertices.size() + v[0]); - vs.push_back(vertices.size() + v[1]); - vs.push_back(vertices.size() + v[2]); + faces.push_back(vertices.size() + v[0]); + faces.push_back(vertices.size() + v[1]); + faces.push_back(vertices.size() + v[2]); } else { - vs.push_back(v[0] - 1); - vs.push_back(v[1] - 1); - vs.push_back(v[2] - 1); + faces.push_back(v[0] - 1); + faces.push_back(v[1] - 1); + faces.push_back(v[2] - 1); } } else if(f == che::mquad) { if(v[0] < 0) { - vs.push_back(vertices.size() + v[0]); - vs.push_back(vertices.size() + v[1]); - vs.push_back(vertices.size() + v[3]); + faces.push_back(vertices.size() + v[0]); + faces.push_back(vertices.size() + v[1]); + faces.push_back(vertices.size() + v[3]); - vs.push_back(vertices.size() + v[1]); - vs.push_back(vertices.size() + v[2]); - vs.push_back(vertices.size() + v[3]); + faces.push_back(vertices.size() + v[1]); + faces.push_back(vertices.size() + v[2]); + faces.push_back(vertices.size() + v[3]); } else { - vs.push_back(v[0] - 1); - vs.push_back(v[1] - 1); - vs.push_back(v[3] - 1); + faces.push_back(v[0] - 1); + faces.push_back(v[1] - 1); + faces.push_back(v[3] - 1); - vs.push_back(v[1] - 1); - vs.push_back(v[2] - 1); - vs.push_back(v[3] - 1); + faces.push_back(v[1] - 1); + faces.push_back(v[2] - 1); + faces.push_back(v[3] - 1); } } } @@ -102,7 +99,10 @@ void che_obj::read_file(const string & file) fclose(fp); - init(vertices.data(), vertices.size(), vs.data(), vs.size() / che::mtrig); + + alloc(vertices.size(), faces.size() / che::mtrig); + memcpy(GT, vertices.data(), vertices.size() * sizeof(vertex)); + memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } void che_obj::write_file(const che * mesh, const string & file) diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 4c2c6604..2ced44d8 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -5,6 +5,7 @@ #include #include + using namespace std; @@ -17,14 +18,6 @@ che_off::che_off(const string & file) init(file); } -che_off::che_off(const che_off & mesh): che(mesh) -{ -} - -che_off::~che_off() -{ -} - void che_off::read_file(const string & file) { char soff[32]; diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 24c1042d..eff4fd50 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -7,6 +7,7 @@ #include #include + using namespace std; @@ -19,9 +20,6 @@ che_ply::che_ply(const string & file) init(file); } -che_ply::che_ply(const che_ply & mesh): che(mesh) -{ -} void che_ply::read_file(const string & file) { diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 038d9bbe..d02841fe 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -4,6 +4,7 @@ #include #include + using namespace std; @@ -16,14 +17,6 @@ che_ptx::che_ptx(const string & file) init(file); } -che_ptx::che_ptx(const che_ptx & mesh): che(mesh) -{ -} - -che_ptx::~che_ptx() -{ -} - void che_ptx::read_file(const string & file) { FILE * fp = fopen(file.c_str(), "r"); diff --git a/src/mesh/che_sphere.cpp b/src/mesh/che_sphere.cpp index 70eca457..3db05b8f 100644 --- a/src/mesh/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -1,9 +1,9 @@ #include "mesh/che_sphere.h" -#include #include #include #include +#include using namespace std; @@ -83,14 +83,6 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) init(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); } -che_sphere::che_sphere(const che_sphere & mesh): che(mesh) -{ -} - -che_sphere::~che_sphere() -{ -} - } // namespace gproshan diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index df6d43a1..ec0e089b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -72,6 +72,8 @@ viewer::~viewer() delete rt_optix; delete render_frame; + + delete sphere; } bool viewer::run() From 7c0af2e305f14fc890b4cc6032fdc3cc4a2ab162 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 31 Mar 2021 21:42:28 +0200 Subject: [PATCH 0471/1018] che: update evt ot structure --- src/mesh/che.cpp | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index c0cbbe08..469dfc49 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1390,7 +1390,6 @@ void che::update_evt_ot_et() map * medges = new map[n_vertices]; - gproshan_error(OBJ); size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) { @@ -1402,7 +1401,7 @@ void che::update_evt_ot_et() if(OT[he] == NIL) { index_t & ohe = medges[v][u]; - if(ohe) + if(ohe && OT[ohe - 1] == NIL) { ET[ne++] = he; OT[he] = ohe - 1; @@ -1416,10 +1415,6 @@ void che::update_evt_ot_et() delete [] medges; - gproshan_error(OBJ); - // non manifold two disk -// for(index_t he = 0; he < n_half_edges; ++he) -// if(OT[he] != NIL && he != OT[OT[he]]) gproshan_error(OT); for(index_t he = 0; he < n_half_edges; ++he) if(OT[he] == NIL && EVT[VT[he]] != NIL) @@ -1431,13 +1426,6 @@ void che::update_evt_ot_et() } else EVT[VT[he]] = he; } - -// for(index_t v = 0; v < n_vertices; ++v) -// if(EVT[v] >= n_half_edges) -// { -// gproshan_debug_var(EVT[v]); -// assert(EVT[v] < n_half_edges); -// } } void che::update_eht() From ff081ce77ec94b54b9d32ddf9c32eae0331555f7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Apr 2021 01:16:30 +0200 Subject: [PATCH 0472/1018] che_obj: fix reader offset faces, read vertex color --- include/mesh/che.h | 6 +++-- src/mesh/che.cpp | 19 +++++++++++++++ src/mesh/che_obj.cpp | 58 ++++++++++---------------------------------- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index d0784643..ccd2c85d 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -27,8 +27,6 @@ struct corr_t; class che { - vertex vcolor{ 0.75, 0.85, 1.0 }; - public: enum mesh_type { mtrig = 3, mquad = 4 }; ///< meshes_types @@ -38,6 +36,7 @@ class che const size_t n_edges = 0; std::string filename; ///< get and set data member + vertex vcolor{ 0.75, 0.85, 1.0 }; protected: vertex * GT = nullptr; ///< geometry table : v -> vertex @@ -136,6 +135,9 @@ class che void update_evt_ot_et(); void update_eht(); + public: + static std::vector trig_convex_polygon(const index_t * P, const size_t & n); + friend struct CHE; }; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 469dfc49..0d3560ba 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -8,6 +8,7 @@ #include #include + using namespace std; @@ -1440,5 +1441,23 @@ void che::update_eht() } +// static + +vector che::trig_convex_polygon(const index_t * P, const size_t & n) +{ + vector trigs; + + trigs.reserve(che::mtrig * (n - 2)); + for(index_t i = 2; i < n; i++) + { + trigs.push_back(P[0]); + trigs.push_back(P[i - 1]); + trigs.push_back(P[i]); + } + + return trigs; +} + + } // namespace gproshan diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 3193ad58..6cae79f8 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -23,15 +23,16 @@ void che_obj::read_file(const string & file) FILE * fp = fopen(file.c_str(), "r"); assert(fp); - real_t x, y, z; - int v[16], vt[16], vn[16], f; + float x, y, z, r, g, b; + index_t P[16], n; vector vertices; + vector vertices_color; vector faces; char line[256], key[4]; char * line_ptr; - int offset; + index_t offset; while(fgets(line, sizeof(line), fp)) { @@ -43,57 +44,23 @@ void che_obj::read_file(const string & file) if(key[0] == 'v' && !key[1]) // v x y z { - sscanf(line_ptr, "%f %f %f%n", &x, &y, &z, &offset); + n = sscanf(line_ptr, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); vertices.push_back({x, y, z}); + vertices_color.push_back(n == 6 ? vertex{r, g, b} : vcolor); } if(key[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... { - f = 0; - while(sscanf(line_ptr, "%d/%d/%d%n", v + f, vt + f, vn + f, &offset) > 0) + n = 0; + while(sscanf(line_ptr, "%d%*s%n", P + n, &offset) > 0) { line_ptr += offset; - ++f; + P[n] += P[n] > vertices.size() ? vertices.size() : -1; + ++n; } - if(f == che::mtrig) - { - if(v[0] < 0) - { - faces.push_back(vertices.size() + v[0]); - faces.push_back(vertices.size() + v[1]); - faces.push_back(vertices.size() + v[2]); - } - else - { - faces.push_back(v[0] - 1); - faces.push_back(v[1] - 1); - faces.push_back(v[2] - 1); - } - } - else if(f == che::mquad) - { - if(v[0] < 0) - { - faces.push_back(vertices.size() + v[0]); - faces.push_back(vertices.size() + v[1]); - faces.push_back(vertices.size() + v[3]); - - faces.push_back(vertices.size() + v[1]); - faces.push_back(vertices.size() + v[2]); - faces.push_back(vertices.size() + v[3]); - } - else - { - faces.push_back(v[0] - 1); - faces.push_back(v[1] - 1); - faces.push_back(v[3] - 1); - - faces.push_back(v[1] - 1); - faces.push_back(v[2] - 1); - faces.push_back(v[3] - 1); - } - } + for(const index_t & v: trig_convex_polygon(P, n)) + faces.push_back(v); } } @@ -102,6 +69,7 @@ void che_obj::read_file(const string & file) alloc(vertices.size(), faces.size() / che::mtrig); memcpy(GT, vertices.data(), vertices.size() * sizeof(vertex)); + memcpy(VC, vertices_color.data(), vertices_color.size() * sizeof(vertex)); memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } From 84e3e525a029cd5cebbb62966f890f33eeb9dc7e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Apr 2021 11:25:16 +0200 Subject: [PATCH 0473/1018] che_off: load n sides polygons as trigs --- src/mesh/che.cpp | 2 +- src/mesh/che_off.cpp | 48 ++++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 0d3560ba..a95ab3b9 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1448,7 +1448,7 @@ vector che::trig_convex_polygon(const index_t * P, const size_t & n) vector trigs; trigs.reserve(che::mtrig * (n - 2)); - for(index_t i = 2; i < n; i++) + for(index_t i = 2; i < n; ++i) { trigs.push_back(P[0]); trigs.push_back(P[i - 1]); diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 2ced44d8..f3007b48 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -21,13 +21,13 @@ che_off::che_off(const string & file) void che_off::read_file(const string & file) { char soff[32]; - size_t nv, nf, ne; + size_t nv, nf, n; FILE * fp = fopen(file.c_str(), "r"); assert(fp); fgets(soff, sizeof(soff), fp); - fscanf(fp, "%lu %lu %lu", &nv, &nf, &ne); + fscanf(fp, "%lu %lu %lu", &nv, &nf, &n); alloc(nv, nf); @@ -57,34 +57,38 @@ void che_off::read_file(const string & file) VC[i] /= 255; } - index_t he = 0; - for(index_t i = 0; i < n_faces; ++i) + vector faces; + faces.reserve(che::mtrig * n_faces); + + index_t P[32]; + while(nf--) { - fscanf(fp, "%lu", &ne); - if(!i && ne > che::mtrig) - { - vertex * tGT = GT; GT = nullptr; + fscanf(fp, "%lu", &n); + for(index_t i = 0; i < n; ++i) + fscanf(fp, "%u", P + i); - free(); - alloc(nv, nf * (ne - che::mtrig + 1)); + for(const index_t & v: trig_convex_polygon(P, n)) + faces.push_back(v); + } - GT = tGT; - } + fclose(fp); - for(index_t j = 0; j < ne; ++j) - fscanf(fp, "%u", VT + he++); - // divide face - if(ne == che::mquad) - { - VT[he] = VT[he - ne]; ++he; - VT[he] = VT[he - che::mtrig]; ++he; + if(faces.size() != che::mtrig * n_faces) + { + vertex * tGT = GT; GT = nullptr; + vertex * tVC = VC; VC = nullptr; + vertex * tVN = VN; VN = nullptr; - ++i; - } + free(); + alloc(nv, faces.size() / che::mtrig); + + GT = tGT; + VC = tVC; + VN = tVN; } - fclose(fp); + memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } void che_off::write_file(const che * mesh, const string & file, const che_off::type & off, const bool & pointcloud) From b3aab82af06e23b3a693e4bb8796ef3a3e3cadea Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Apr 2021 16:27:07 +0200 Subject: [PATCH 0474/1018] che_ply: load n sides polygons as trigs --- src/mesh/che_obj.cpp | 2 +- src/mesh/che_ply.cpp | 61 +++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 6cae79f8..6513cad6 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -24,7 +24,7 @@ void che_obj::read_file(const string & file) assert(fp); float x, y, z, r, g, b; - index_t P[16], n; + index_t P[32], n; vector vertices; vector vertices_color; diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index eff4fd50..f32192ce 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -20,7 +20,6 @@ che_ply::che_ply(const string & file) init(file); } - void che_ply::read_file(const string & file) { map bytes = { @@ -36,7 +35,8 @@ void che_ply::read_file(const string & file) {"double", 8} }; - size_t n_v = 0, n_f = 0, b; + size_t nv = 0, nf = 0, b; + index_t P[32]; string str, format, element; @@ -56,8 +56,8 @@ void che_ply::read_file(const string & file) if(str == "element") { ss >> element; - if(element == "vertex") ss >> n_v; - if(element == "face") ss >> n_f; + if(element == "vertex") ss >> nv; + if(element == "face") ss >> nf; } if(str == "property" && element == "vertex") @@ -83,7 +83,10 @@ void che_ply::read_file(const string & file) if(str == "format") ss >> format; } - alloc(n_v, n_f); + alloc(nv, nf); + + vector faces; + faces.reserve(che::mtrig * n_faces); if(format == "ascii") { @@ -95,15 +98,17 @@ void che_ply::read_file(const string & file) ss >> GT[v]; } - index_t p, he = 0; - while(n_f--) + while(nf--) { getline(is, str); stringstream ss(str); - ss >> p; - while(p--) - ss >> VT[he++]; + ss >> nv; + for(index_t i = 0; i < nv; ++i) + ss >> P[i]; + + for(const index_t & v: trig_convex_polygon(P, nv)) + faces.push_back(v); } } else // binary_little_endian @@ -111,7 +116,7 @@ void che_ply::read_file(const string & file) bool big_endian = format == "binary_big_endian"; auto big_to_little = [](char * buffer, const index_t & n) { - for(index_t i = 0, j = n - 1; i < j; i++, j--) + for(index_t i = 0, j = n - 1; i < j; ++i, --j) swap(buffer[i], buffer[j]); }; @@ -140,32 +145,46 @@ void che_ply::read_file(const string & file) } } - //char fbuffer[fbytes]; // preparing for a hexagon - index_t p, he = 0; - while(n_f--) + while(nf--) { is.read(vbuffer, fn); if(big_endian) big_to_little(vbuffer, fn); - if(fn == 1) p = *((char *) vbuffer); - if(fn == 2) p = *((short *) vbuffer); - if(fn == 4) p = *((int *) vbuffer); + if(fn == 1) nv = *((char *) vbuffer); + if(fn == 2) nv = *((short *) vbuffer); + if(fn == 4) nv = *((int *) vbuffer); - while(p--) + for(index_t i = 0; i < nv; ++i) { is.read(vbuffer, fbytes); if(big_endian) big_to_little(vbuffer, fbytes); - if(fbytes == 1) VT[he++] = *((char *) vbuffer); - if(fbytes == 2) VT[he++] = *((short *) vbuffer); - if(fbytes == 4) VT[he++] = *((int *) vbuffer); + if(fbytes == 1) P[i] = *((char *) vbuffer); + if(fbytes == 2) P[i] = *((short *) vbuffer); + if(fbytes == 4) P[i] = *((int *) vbuffer); } + + for(const index_t & v: trig_convex_polygon(P, nv)) + faces.push_back(v); } delete [] vbuffer; } is.close(); + + + if(faces.size() != che::mtrig * n_faces) + { + vertex * tGT = GT; GT = nullptr; + + free(); + alloc(nv, faces.size() / che::mtrig); + + GT = tGT; + } + + memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } void che_ply::write_file(const che * mesh, const string & file) From 59d5747990c912cc240827a4f36b75012215e602 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Apr 2021 17:38:29 +0200 Subject: [PATCH 0475/1018] che_ply: fast read file --- src/mesh/che_ply.cpp | 117 +++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 60 deletions(-) diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index f32192ce..2e59fcc9 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -1,10 +1,9 @@ #include "mesh/che_ply.h" -#include -#include -#include #include +#include #include +#include #include @@ -22,7 +21,7 @@ che_ply::che_ply(const string & file) void che_ply::read_file(const string & file) { - map bytes = { + map bytes = { {"char", 1}, {"uchar", 1}, {"short", 2}, @@ -35,52 +34,53 @@ void che_ply::read_file(const string & file) {"double", 8} }; - size_t nv = 0, nf = 0, b; - index_t P[32]; - - string str, format, element; - - ifstream is(file); - assert(is.good()); + FILE * fp = fopen(file.c_str(), "rb"); + assert(fp); + size_t nv = 0, nf = 0, b; size_t xyz = 0, vbytes = 0; size_t fn = 0, fbytes = 0; + index_t P[32]; + + char line[256], str[32], format[32], element[32]; - while(getline(is, str) && str != "end_header") + while(fgets(line, sizeof(line), fp) && line[1] != 'n') // end_header { - stringstream ss(str); + sscanf(line, "%s", str); - str = ""; - ss >> str; + if(str[0] == 'f') // format + sscanf(line, "%*s %s", format); - if(str == "element") + if(str[0] == 'e') // element { - ss >> element; - if(element == "vertex") ss >> nv; - if(element == "face") ss >> nf; + sscanf(line, "%*s %s", element); + if(element[0] == 'v') // vertex + sscanf(line, "%*s %*s %lu", &nv); + if(element[0] == 'f') // face + sscanf(line, "%*s %*s %lu", &nf); } - if(str == "property" && element == "vertex") + if(str[0] == 'p' && element[0] == 'v') // property vertex { - ss >> str; + sscanf(line, "%*s %s", str); vbytes += b = bytes[str]; - ss >> str; - if(str == "x" || str == "y" || str == "z") + sscanf(line, "%*s %*s %s", str); + if(str[0] == 'x' || str[0] == 'y' || str[0] == 'z') xyz = b; } - if(str == "property" && element == "face") + if(str[0] == 'p' && element[0] == 'f') // property face { - ss >> str; - if(str == "list") + sscanf(line, "%*s %s", str); + if(str[0] == 'l') // list { - ss >> str; fn = bytes[str]; - ss >> str; fbytes = bytes[str]; + sscanf(line, "%*s %*s %s", str); + fn = bytes[str]; + sscanf(line, "%*s %*s %*s %s", str); + fbytes = bytes[str]; } } - - if(str == "format") ss >> format; } alloc(nv, nf); @@ -88,57 +88,54 @@ void che_ply::read_file(const string & file) vector faces; faces.reserve(che::mtrig * n_faces); - if(format == "ascii") + if(format[0] == 'a') // ascii { + float x, y, z; for(index_t v = 0; v < n_vertices; ++v) { - getline(is, str); - stringstream ss(str); - - ss >> GT[v]; + fscanf(fp, "%f %f %f", &x, &y, &z); + GT[v] = {x, y, z}; } while(nf--) { - getline(is, str); - stringstream ss(str); - - ss >> nv; + fscanf(fp, "%lu", &nv); for(index_t i = 0; i < nv; ++i) - ss >> P[i]; + fscanf(fp, "%u", P + i); for(const index_t & v: trig_convex_polygon(P, nv)) faces.push_back(v); } } - else // binary_little_endian + else // binary_little_endian or binary_big_endian { - bool big_endian = format == "binary_big_endian"; + bool big_endian = format[7] == 'b'; auto big_to_little = [](char * buffer, const index_t & n) { for(index_t i = 0, j = n - 1; i < j; ++i, --j) swap(buffer[i], buffer[j]); }; - char * vbuffer = new char[vbytes]; + char * buffer = new char[vbytes]; + for(index_t v = 0; v < n_vertices; ++v) { - is.read(vbuffer, vbytes); - if(big_endian) big_to_little(vbuffer, vbytes); + fread(buffer, 1, vbytes, fp); + if(big_endian) big_to_little(buffer, vbytes); if(xyz == sizeof(real_t)) - memcpy(>[v], vbuffer, 3 * sizeof(real_t)); + memcpy(>[v], buffer, 3 * sizeof(real_t)); else { if(xyz == 4) { - float * X = (float *) vbuffer; + float * X = (float *) buffer; for(index_t i = 0; i < 3; ++i) GT[v][i] = X[i]; } else { - double * X = (double *) vbuffer; + double * X = (double *) buffer; for(index_t i = 0; i < 3; ++i) GT[v][i] = (real_t) X[i]; } @@ -147,31 +144,31 @@ void che_ply::read_file(const string & file) while(nf--) { - is.read(vbuffer, fn); - if(big_endian) big_to_little(vbuffer, fn); + fread(buffer, 1, fn, fp); + if(big_endian) big_to_little(buffer, fn); - if(fn == 1) nv = *((char *) vbuffer); - if(fn == 2) nv = *((short *) vbuffer); - if(fn == 4) nv = *((int *) vbuffer); + if(fn == 1) nv = *((char *) buffer); + if(fn == 2) nv = *((short *) buffer); + if(fn == 4) nv = *((int *) buffer); for(index_t i = 0; i < nv; ++i) { - is.read(vbuffer, fbytes); - if(big_endian) big_to_little(vbuffer, fbytes); + fread(buffer, 1, fbytes, fp); + if(big_endian) big_to_little(buffer, fbytes); - if(fbytes == 1) P[i] = *((char *) vbuffer); - if(fbytes == 2) P[i] = *((short *) vbuffer); - if(fbytes == 4) P[i] = *((int *) vbuffer); + if(fbytes == 1) P[i] = *((char *) buffer); + if(fbytes == 2) P[i] = *((short *) buffer); + if(fbytes == 4) P[i] = *((int *) buffer); } for(const index_t & v: trig_convex_polygon(P, nv)) faces.push_back(v); } - delete [] vbuffer; + delete [] buffer; } - is.close(); + fclose(fp); if(faces.size() != che::mtrig * n_faces) From eb7f5b874b2633312eed10ac39d5c411a9268ad5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Apr 2021 20:38:13 +0200 Subject: [PATCH 0476/1018] che_xyz: add reader to xyz point-cloud file format --- include/app_viewer.h | 1 + include/mesh/che_xyz.h | 26 ++++++++++++++++++++ src/app_viewer.cpp | 1 + src/mesh/che_ply.cpp | 2 +- src/mesh/che_xyz.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 include/mesh/che_xyz.h create mode 100644 src/mesh/che_xyz.cpp diff --git a/include/app_viewer.h b/include/app_viewer.h index 3c0f2890..025774d2 100644 --- a/include/app_viewer.h +++ b/include/app_viewer.h @@ -8,6 +8,7 @@ #include "mesh/che_obj.h" #include "mesh/che_ply.h" #include "mesh/che_ptx.h" +#include "mesh/che_xyz.h" #include "mesh/che_img.h" #include "mesh/che_sphere.h" #include "mesh/che_fill_hole.h" diff --git a/include/mesh/che_xyz.h b/include/mesh/che_xyz.h new file mode 100644 index 00000000..65de83e5 --- /dev/null +++ b/include/mesh/che_xyz.h @@ -0,0 +1,26 @@ +#ifndef CHE_XYZ_H +#define CHE_XYZ_H + +#include "mesh/che.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_xyz : public che +{ + public: + che_xyz(const std::string & file); + + static void write_file(const che * mesh, const std::string & file); + + private: + void read_file(const std::string & file); +}; + + +} // namespace gproshan + +#endif // CHE_XYZ_H + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 654de30b..67438c1f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -28,6 +28,7 @@ che * app_viewer::load_mesh(const string & file_path) if(extension == "obj") return new che_obj(file_path); if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); + if(extension == "xyz") return new che_xyz(file_path); return new che_img(file_path); } diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 2e59fcc9..a2222185 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -73,7 +73,7 @@ void che_ply::read_file(const string & file) if(str[0] == 'p' && element[0] == 'f') // property face { sscanf(line, "%*s %s", str); - if(str[0] == 'l') // list + if(str[0] == 'l') // list { sscanf(line, "%*s %*s %s", str); fn = bytes[str]; diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp new file mode 100644 index 00000000..72049882 --- /dev/null +++ b/src/mesh/che_xyz.cpp @@ -0,0 +1,56 @@ +#include "mesh/che_xyz.h" + +#include +#include +#include +#include + + +using namespace std; + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_xyz::che_xyz(const string & file) +{ + init(file); +} + +void che_xyz::read_file(const string & file) +{ + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); + + char line[256], c; + float x, y, z, r, g, b; + size_t n; + + vector vertices; + vector vertices_color; + + while(fgets(line, sizeof(line), fp)) + { + sscanf(line, "%c", &c); + if(c == '#') continue; + + n = sscanf(line, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); + vertices.push_back({x, y, z}); + vertices_color.push_back(n == 6 ? vertex{r, g, b} / 255 : vcolor); + } + + fclose(fp); + + alloc(vertices.size(), 0); + memcpy(GT, vertices.data(), n_vertices * sizeof(vertex)); + memcpy(VC, vertices_color.data(), n_vertices * sizeof(vertex)); +} + +void che_xyz::write_file(const che * mesh, const string & file) +{ +} + + +} // namespace gproshan + From 19d0100ee5505d5b1c43c3102f4cee8ec9f32ac1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 12:15:20 +0200 Subject: [PATCH 0477/1018] update che_off and che_xyz write methods --- include/mesh/che_off.h | 2 +- include/mesh/che_ptx.h | 2 -- include/mesh/che_xyz.h | 2 +- include/mesh/vertex.h | 1 + src/mesh/che_obj.cpp | 2 +- src/mesh/che_off.cpp | 16 ++++++---------- src/mesh/che_ply.cpp | 2 +- src/mesh/che_ptx.cpp | 4 ---- src/mesh/che_xyz.cpp | 22 +++++++++++++++++++++- src/mesh/vertex.cpp | 22 +++++++++++----------- src/viewer/viewer.cpp | 37 +++++++++++++++++++++++++++++++------ 11 files changed, 74 insertions(+), 38 deletions(-) diff --git a/include/mesh/che_off.h b/include/mesh/che_off.h index d5a738d3..b27aafeb 100644 --- a/include/mesh/che_off.h +++ b/include/mesh/che_off.h @@ -13,7 +13,7 @@ class che_off : public che public: che_off(const std::string & file); - enum type { OFF, NOFF, COFF, CNOFF }; + enum type { OFF, NOFF, COFF, NCOFF }; static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool & pointcloud = false); private: diff --git a/include/mesh/che_ptx.h b/include/mesh/che_ptx.h index 85614893..1c0cd59a 100644 --- a/include/mesh/che_ptx.h +++ b/include/mesh/che_ptx.h @@ -13,8 +13,6 @@ class che_ptx : public che public: che_ptx(const std::string & file); - static void write_file(const che * mesh, const std::string & file); - private: void read_file(const std::string & file); }; diff --git a/include/mesh/che_xyz.h b/include/mesh/che_xyz.h index 65de83e5..8d8816f3 100644 --- a/include/mesh/che_xyz.h +++ b/include/mesh/che_xyz.h @@ -13,7 +13,7 @@ class che_xyz : public che public: che_xyz(const std::string & file); - static void write_file(const che * mesh, const std::string & file); + static void write_file(const che * mesh, const std::string & file, const bool & color = false); private: void read_file(const std::string & file); diff --git a/include/mesh/vertex.h b/include/mesh/vertex.h index 908c3a2b..00e53aa4 100644 --- a/include/mesh/vertex.h +++ b/include/mesh/vertex.h @@ -32,6 +32,7 @@ class vertex real_t operator , (const vertex & v) const; // dot product vertex operator * (const vertex & v) const; // cross product + vertex operator * (const real_t & v) const; // scalar product vertex operator / (const real_t & v) const; // scalar division vertex operator + (const vertex & v) const; vertex operator - (const vertex & v) const; diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 6513cad6..87d9439f 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -78,7 +78,7 @@ void che_obj::write_file(const che * mesh, const string & file) ofstream os(file + ".obj"); os << "####\n#\n"; - os << "# OBJ generated by gproshan 2019" << endl; + os << "# OBJ generated by gproshan" << endl; os << "# vertices: " << mesh->n_vertices << endl; os << "# faces: " << mesh->n_faces << endl; os << "#\n####\n"; diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index f3007b48..6632855b 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -102,7 +102,10 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t { os << mesh->gt(v); - if(off == NOFF) os << " " << mesh->normal(v); // NOFF file + if(off == COFF || off == NCOFF) + os << " " << 255 * mesh->color(v) << " 1"; + if(off == NOFF || off == NCOFF) + os << " " << mesh->normal(v); os << endl; } @@ -121,15 +124,8 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t ostream & operator << (ostream & os, const che_off::type & off) { - switch(off) - { - case che_off::OFF : os << "OFF"; break; - case che_off::NOFF : os << "NOFF"; break; - case che_off::COFF : os << "COFF"; break; - case che_off::CNOFF : os << "CNOFF"; break; - } - - return os; + const char * str_off[] = {"OFF", "NOFF", "COFF", "NCOFF"}; + return os << str_off[off]; } diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index a2222185..fee8aca5 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -190,7 +190,7 @@ void che_ply::write_file(const che * mesh, const string & file) os << "ply" << endl; os << "format ascii 1.0" << endl; - os << "comment generated by gproshan 2020" << endl; + os << "comment PLY generated by gproshan" << endl; os << "element vertex " << mesh->n_vertices << endl; os << "property float x" << endl; os << "property float y" << endl; diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index d02841fe..6d479ecb 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -113,10 +113,6 @@ void che_ptx::read_file(const string & file) VC[i] /= 255; } -void che_ptx::write_file(const che * mesh, const string & file) -{ -} - } // namespace gproshan diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp index 72049882..7679b7ef 100644 --- a/src/mesh/che_xyz.cpp +++ b/src/mesh/che_xyz.cpp @@ -47,8 +47,28 @@ void che_xyz::read_file(const string & file) memcpy(VC, vertices_color.data(), n_vertices * sizeof(vertex)); } -void che_xyz::write_file(const che * mesh, const string & file) +void che_xyz::write_file(const che * mesh, const string & file, const bool & color) { + FILE * fp = fopen((file + ".xyz").c_str(), "w"); + assert(fp); + + fprintf(fp, "# XYZ generated by gproshan\n"); + for(index_t i = 0; i < mesh->n_vertices; ++i) + { + const vertex & v = mesh->gt(i); + if(color) + { + vertex c = 255 * mesh->color(i); + fprintf(fp, "%f %f %f ", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, "%d %d %d\n", (int) c.x, (int) c.y, (int) c.z); + } + else + { + fprintf(fp, "%f %f %f\n", (float) v.x, (float) v.y, (float) v.z); + } + } + + fclose(fp); } diff --git a/src/mesh/vertex.cpp b/src/mesh/vertex.cpp index 00435f78..6270e0d8 100644 --- a/src/mesh/vertex.cpp +++ b/src/mesh/vertex.cpp @@ -9,12 +9,7 @@ using namespace std; namespace gproshan { -vertex::vertex(const real_t & x_, const real_t & y_, const real_t & z_) -{ - x = x_; - y = y_; - z = z_; -} +vertex::vertex(const real_t & x_, const real_t & y_, const real_t & z_): x(x_), y(y_), z(z_) {} real_t & vertex::operator [] (const index_t & i) { @@ -43,7 +38,12 @@ real_t vertex::operator , (const vertex & v) const vertex vertex::operator * (const vertex & v) const { - return vertex(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); + return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x}; +} + +vertex vertex::operator * (const real_t & a) const +{ + return a * (*this); } vertex vertex::operator / (const real_t & a) const @@ -53,17 +53,17 @@ vertex vertex::operator / (const real_t & a) const vertex vertex::operator + (const vertex & v) const { - return vertex(x + v.x, y + v.y, z + v.z); + return {x + v.x, y + v.y, z + v.z}; } vertex vertex::operator - (const vertex & v) const { - return vertex(x - v.x, y - v.y, z - v.z); + return {x - v.x, y - v.y, z - v.z}; } vertex vertex::operator - () const { - return vertex(-x, -y, -z); + return {-x, -y, -z}; } void vertex::operator *= (const real_t & a) @@ -113,7 +113,7 @@ bool vertex::is_zero() vertex operator * (const real_t & a, const vertex & v) { - return vertex(a * v.x, a * v.y, a * v.z); + return {a * v.x, a * v.y, a * v.z}; } ostream & operator << (ostream & os, const vertex & v) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index ec0e089b..c6df6e88 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -16,6 +16,7 @@ #include "mesh/che_off.h" #include "mesh/che_obj.h" #include "mesh/che_ply.h" +#include "mesh/che_xyz.h" #include "mesh/che_sphere.h" #ifdef GPROSHAN_EMBREE @@ -510,17 +511,41 @@ bool viewer::menu_save_mesh(viewer * view) static char file[128] = "copy"; static int format = 0; - static bool pc = false; + static int type_off = 0; + static bool point_cloud = false; + static bool vertex_color = false; ImGui::InputText("file", file, sizeof(file)); - ImGui::Combo("format", &format, ".off\0.obj\0.ply\0\0"); - ImGui::Checkbox("point cloud", &pc); + ImGui::Combo("format", &format, ".off\0.obj\0.ply\0.xyz\0\0"); + + switch(format) + { + case 0: + ImGui::Combo("type off", &type_off, "OFF\0NOFF\0COFF\0NCOFF\0\0"); + ImGui::Checkbox("point cloud", &point_cloud); + break; + case 1: + break; + case 2: + break; + case 3: + ImGui::Checkbox("vertex color", &vertex_color); + break; + } if(ImGui::Button("Save")) { - if(format == 0) che_off::write_file(mesh, file, che_off::NOFF, pc); - if(format == 1) che_obj::write_file(mesh, file); - if(format == 2) che_ply::write_file(mesh, file); + switch(format) + { + case 0: che_off::write_file(mesh, file, che_off::type(type_off), point_cloud); + break; + case 1: che_obj::write_file(mesh, file); + break; + case 2: che_ply::write_file(mesh, file); + break; + case 3: che_xyz::write_file(mesh, file, vertex_color); + break; + } } return true; From d50adfdbcbea29e1ff87b6d29be05e1ace03a7bd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 12:46:18 +0200 Subject: [PATCH 0478/1018] update che_off and che_xyz write methods --- include/mesh/che_off.h | 2 -- src/mesh/che_off.cpp | 44 ++++++++++++++++++++++++------------------ src/mesh/che_xyz.cpp | 10 +++------- src/viewer/viewer.cpp | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/include/mesh/che_off.h b/include/mesh/che_off.h index b27aafeb..af5cabe5 100644 --- a/include/mesh/che_off.h +++ b/include/mesh/che_off.h @@ -20,8 +20,6 @@ class che_off : public che void read_file(const std::string & file); }; -std::ostream & operator << (std::ostream & os, const che_off::type & off); - } // namespace gproshan diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 6632855b..d1352946 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; @@ -93,39 +92,46 @@ void che_off::read_file(const string & file) void che_off::write_file(const che * mesh, const string & file, const che_off::type & off, const bool & pointcloud) { - ofstream os(file + ".off"); + static const char * str_off[] = {"OFF", "NOFF", "COFF", "NCOFF"}; - os << off << endl; - os << mesh->n_vertices << " " << (pointcloud ? 0 : mesh->n_faces) << " 0" << endl; + FILE * fp = fopen((file + ".off").c_str(), "w"); + assert(fp); + + fprintf(fp, "%s\n", str_off[off]); + fprintf(fp, "%lu %lu 0\n", mesh->n_vertices, pointcloud ? 0 : mesh->n_faces); - for(size_t v = 0; v < mesh->n_vertices; ++v) + for(size_t i = 0; i < mesh->n_vertices; ++i) { - os << mesh->gt(v); + const vertex & v = mesh->gt(i); + fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); if(off == COFF || off == NCOFF) - os << " " << 255 * mesh->color(v) << " 1"; + { + vertex c = 255 * mesh->color(i); + fprintf(fp, " %d %d %d 1", (int) c.x, (int) c.y, (int) c.z); + } + if(off == NOFF || off == NCOFF) - os << " " << mesh->normal(v); + { + const vertex & n = mesh->normal(i); + fprintf(fp, " %f %f %f", (float) n.x, (float) n.y, (float) n.z); + } - os << endl; + fprintf(fp, "\n"); } if(!pointcloud) + { for(index_t he = 0; he < mesh->n_half_edges; ) { - os << che::mtrig; + fprintf(fp, "%u", che::mtrig); for(index_t i = 0; i < che::mtrig; ++i) - os << " " << mesh->vt(he++); - os << endl; + fprintf(fp, " %u", mesh->vt(he++)); + fprintf(fp, "\n"); } + } - os.close(); -} - -ostream & operator << (ostream & os, const che_off::type & off) -{ - const char * str_off[] = {"OFF", "NOFF", "COFF", "NCOFF"}; - return os << str_off[off]; + fclose(fp); } diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp index 7679b7ef..17064171 100644 --- a/src/mesh/che_xyz.cpp +++ b/src/mesh/che_xyz.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; @@ -56,16 +55,13 @@ void che_xyz::write_file(const che * mesh, const string & file, const bool & col for(index_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->gt(i); + fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); if(color) { vertex c = 255 * mesh->color(i); - fprintf(fp, "%f %f %f ", (float) v.x, (float) v.y, (float) v.z); - fprintf(fp, "%d %d %d\n", (int) c.x, (int) c.y, (int) c.z); - } - else - { - fprintf(fp, "%f %f %f\n", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, " %d %d %d", (int) c.x, (int) c.y, (int) c.z); } + fprintf(fp, "\n"); } fclose(fp); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c6df6e88..084f7470 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -118,7 +118,7 @@ bool viewer::run() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->name()).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; sphere_translations.clear(); From 6b413040783ef645b69b54c65562c7e81f506ca6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 13:31:18 +0200 Subject: [PATCH 0479/1018] update che_obj write method --- include/mesh/che_obj.h | 2 +- src/mesh/che_obj.cpp | 54 ++++++++++++++++++++++++++---------------- src/mesh/che_xyz.cpp | 2 ++ src/viewer/viewer.cpp | 4 +++- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/include/mesh/che_obj.h b/include/mesh/che_obj.h index 32c7b88e..292d17d6 100644 --- a/include/mesh/che_obj.h +++ b/include/mesh/che_obj.h @@ -13,7 +13,7 @@ class che_obj : public che public: che_obj(const std::string & file); - static void write_file(const che * mesh, const std::string & file); + static void write_file(const che * mesh, const std::string & file, const bool & color = false, const bool & pointcloud = false); private: void read_file(const std::string & file); diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 87d9439f..4fc049dd 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -30,31 +30,32 @@ void che_obj::read_file(const string & file) vector vertices_color; vector faces; - char line[256], key[4]; + char line[256], str[64]; char * line_ptr; index_t offset; while(fgets(line, sizeof(line), fp)) { - key[0] = 0; + str[0] = 0; line_ptr = line; - sscanf(line_ptr, "%s%n", key, &offset); + sscanf(line_ptr, "%s%n", str, &offset); line_ptr += offset; - if(key[0] == 'v' && !key[1]) // v x y z + if(str[0] == 'v' && !str[1]) // v x y z { n = sscanf(line_ptr, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); vertices.push_back({x, y, z}); vertices_color.push_back(n == 6 ? vertex{r, g, b} : vcolor); } - if(key[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... + if(str[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... { n = 0; - while(sscanf(line_ptr, "%d%*s%n", P + n, &offset) > 0) + while(sscanf(line_ptr, "%s%n", str, &offset) > 0) { line_ptr += offset; + sscanf(str, "%d%*s", P + n); P[n] += P[n] > vertices.size() ? vertices.size() : -1; ++n; } @@ -73,28 +74,39 @@ void che_obj::read_file(const string & file) memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } -void che_obj::write_file(const che * mesh, const string & file) +void che_obj::write_file(const che * mesh, const string & file, const bool & color, const bool & pointcloud) { - ofstream os(file + ".obj"); + FILE * fp = fopen((file + ".obj").c_str(), "w"); + assert(fp); - os << "####\n#\n"; - os << "# OBJ generated by gproshan" << endl; - os << "# vertices: " << mesh->n_vertices << endl; - os << "# faces: " << mesh->n_faces << endl; - os << "#\n####\n"; + fprintf(fp, "# OBJ generated by gproshan\n"); + fprintf(fp, "# vertices %lu\n", mesh->n_vertices); + fprintf(fp, "# faces %lu\n", mesh->n_faces); - for(size_t v = 0; v < mesh->n_vertices; ++v) - os << "v " << mesh->gt(v) << endl; + for(index_t i = 0; i < mesh->n_vertices; ++i) + { + const vertex & v = mesh->gt(i); + fprintf(fp, "v %f %f %f", (float) v.x, (float) v.y, (float) v.z); + if(color) + { + const vertex & c = mesh->color(i); + fprintf(fp, " %f %f %f", (float) c.x, (float) c.y, (float) c.z); + } + fprintf(fp, "\n"); + } - for(index_t he = 0; he < mesh->n_half_edges; ) + if(!pointcloud) { - os << "f"; - for(index_t i = 0; i < che::mtrig; ++i) - os << " " << mesh->vt(he++) + 1; - os << endl; + for(index_t he = 0; he < mesh->n_half_edges; ) + { + fprintf(fp, "f"); + for(index_t i = 0; i < che::mtrig; ++i) + fprintf(fp, " %u", mesh->vt(he++) + 1); + fprintf(fp, "\n"); + } } - os.close(); + fclose(fp); } diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp index 17064171..728ce43b 100644 --- a/src/mesh/che_xyz.cpp +++ b/src/mesh/che_xyz.cpp @@ -52,6 +52,8 @@ void che_xyz::write_file(const che * mesh, const string & file, const bool & col assert(fp); fprintf(fp, "# XYZ generated by gproshan\n"); + fprintf(fp, "# vertices %lu\n", mesh->n_vertices); + for(index_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->gt(i); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 084f7470..94c71e59 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -525,6 +525,8 @@ bool viewer::menu_save_mesh(viewer * view) ImGui::Checkbox("point cloud", &point_cloud); break; case 1: + ImGui::Checkbox("vertex color", &vertex_color); + ImGui::Checkbox("point cloud", &point_cloud); break; case 2: break; @@ -539,7 +541,7 @@ bool viewer::menu_save_mesh(viewer * view) { case 0: che_off::write_file(mesh, file, che_off::type(type_off), point_cloud); break; - case 1: che_obj::write_file(mesh, file); + case 1: che_obj::write_file(mesh, file, vertex_color, point_cloud); break; case 2: che_ply::write_file(mesh, file); break; From 40df141a2a2b140f5a07a6393312a4c24d637e10 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 15:43:02 +0200 Subject: [PATCH 0480/1018] update che_ply write binary little endian format --- include/mesh/che.h | 2 +- src/mesh/che_obj.cpp | 1 - src/mesh/che_ply.cpp | 46 ++++++++++++++++++++++---------------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index ccd2c85d..d606baf8 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -28,7 +28,7 @@ struct corr_t; class che { public: - enum mesh_type { mtrig = 3, mquad = 4 }; ///< meshes_types + enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types const size_t n_vertices = 0; const size_t n_faces = 0; diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 4fc049dd..9edb78ee 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -3,7 +3,6 @@ #include #include #include -#include using namespace std; diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index fee8aca5..6121ee99 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include @@ -186,31 +185,32 @@ void che_ply::read_file(const string & file) void che_ply::write_file(const che * mesh, const string & file) { - ofstream os(file + ".ply"); - - os << "ply" << endl; - os << "format ascii 1.0" << endl; - os << "comment PLY generated by gproshan" << endl; - os << "element vertex " << mesh->n_vertices << endl; - os << "property float x" << endl; - os << "property float y" << endl; - os << "property float z" << endl; - os << "element face " << mesh->n_faces << endl; - os << "property list uchar int vertex_index" << endl; - os << "end_header" << endl; - - for(size_t v = 0; v < mesh->n_vertices; ++v) - os << mesh->gt(v) << endl; - - for(index_t he = 0; he < mesh->n_half_edges; ) + FILE * fp = fopen((file + ".ply").c_str(), "wb"); + assert(fp); + + const char * type = sizeof(real_t) == 4 ? "float" : "double"; + + fprintf(fp, "ply\n"); + fprintf(fp, "format binary_little_endian 1.0\n"); + fprintf(fp, "comment PLY generated by gproshan\n"); + fprintf(fp, "element vertex %lu\n", mesh->n_vertices); + fprintf(fp, "property %s x\n", type); + fprintf(fp, "property %s y\n", type); + fprintf(fp, "property %s z\n", type); + fprintf(fp, "element face %lu\n", mesh->n_faces); + fprintf(fp, "property list uchar uint vertex_indices\n"); + fprintf(fp, "end_header\n"); + + fwrite(&mesh->gt(0), sizeof(vertex), mesh->n_vertices, fp); + + unsigned char mtrig = che::mtrig; + for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) { - os << che::mtrig; - for(index_t i = 0; i < che::mtrig; ++i) - os << " " << mesh->vt(he++); - os << endl; + fwrite(&mtrig, 1, 1, fp); + fwrite(&mesh->vt(he), sizeof(index_t), che::mtrig, fp); } - os.close(); + fclose(fp); } From 97772bc816f9d2e8e3042f05f6a0f08e5d53147c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 18:22:04 +0200 Subject: [PATCH 0481/1018] che_ply: load color --- include/mesh/che_ply.h | 2 +- src/mesh/che_ply.cpp | 134 ++++++++++++++++++++++++++++++----------- src/viewer/viewer.cpp | 3 +- 3 files changed, 103 insertions(+), 36 deletions(-) diff --git a/include/mesh/che_ply.h b/include/mesh/che_ply.h index d7919680..5269e624 100644 --- a/include/mesh/che_ply.h +++ b/include/mesh/che_ply.h @@ -13,7 +13,7 @@ class che_ply : public che public: che_ply(const std::string & file); - static void write_file(const che * mesh, const std::string & file); + static void write_file(const che * mesh, const std::string & file, const bool & color = false); private: void read_file(const std::string & file); diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 6121ee99..35da7399 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -36,12 +36,13 @@ void che_ply::read_file(const string & file) FILE * fp = fopen(file.c_str(), "rb"); assert(fp); - size_t nv = 0, nf = 0, b; - size_t xyz = 0, vbytes = 0; + size_t nv = 0, nf = 0; + size_t nb, xyz = 0, rgb = 0, vbytes = 0; + index_t ixyz = 0, irgb = 0; size_t fn = 0, fbytes = 0; index_t P[32]; - char line[256], str[32], format[32], element[32]; + char line[512], type[32], str[32], format[32], element[32]; while(fgets(line, sizeof(line), fp) && line[1] != 'n') // end_header { @@ -61,12 +62,21 @@ void che_ply::read_file(const string & file) if(str[0] == 'p' && element[0] == 'v') // property vertex { - sscanf(line, "%*s %s", str); - vbytes += b = bytes[str]; + sscanf(line, "%*s %s %s", type, str); + nb = bytes[type]; + + if(str[0] == 'x') + { + xyz = nb; + ixyz = vbytes; + } + if(str[0] == 'r') + { + rgb = nb; + irgb = vbytes; + } - sscanf(line, "%*s %*s %s", str); - if(str[0] == 'x' || str[0] == 'y' || str[0] == 'z') - xyz = b; + vbytes += nb; } if(str[0] == 'p' && element[0] == 'f') // property face @@ -74,10 +84,10 @@ void che_ply::read_file(const string & file) sscanf(line, "%*s %s", str); if(str[0] == 'l') // list { - sscanf(line, "%*s %*s %s", str); - fn = bytes[str]; - sscanf(line, "%*s %*s %*s %s", str); - fbytes = bytes[str]; + sscanf(line, "%*s %*s %s", type); + fn = bytes[type]; + sscanf(line, "%*s %*s %*s %s", type); + fbytes = bytes[type]; } } } @@ -115,34 +125,71 @@ void che_ply::read_file(const string & file) swap(buffer[i], buffer[j]); }; - char * buffer = new char[vbytes]; + gproshan_log_var(big_endian); + gproshan_log_var(vbytes); + gproshan_log_var(xyz); + gproshan_log_var(rgb); + gproshan_log_var(fn); + gproshan_log_var(fbytes); - for(index_t v = 0; v < n_vertices; ++v) - { - fread(buffer, 1, vbytes, fp); - if(big_endian) big_to_little(buffer, vbytes); + char * buffer = vbytes == sizeof(vertex) ? (char *) GT : new char[vbytes * n_vertices]; - if(xyz == sizeof(real_t)) - memcpy(>[v], buffer, 3 * sizeof(real_t)); - else + fread(buffer, vbytes, n_vertices, fp); + if(big_endian) + { + char * pb = buffer; + while(nv--) { - if(xyz == 4) - { - float * X = (float *) buffer; - for(index_t i = 0; i < 3; ++i) - GT[v][i] = X[i]; - } - else + for(index_t i = 0; i < 3; ++i) + big_to_little(pb + ixyz + i * xyz, xyz); + + if(rgb) { - double * X = (double *) buffer; for(index_t i = 0; i < 3; ++i) - GT[v][i] = (real_t) X[i]; + big_to_little(pb + irgb + i * rgb, rgb); } + + pb += vbytes; + } + } + + if(vbytes != sizeof(vertex)) + { + char * pb = buffer; + for(index_t v = 0; v < n_vertices; ++v) + { + for(index_t i = 0; i < 3; ++i) + if(xyz == 4) + GT[v][i] = (real_t) *(float *) (pb + ixyz + i * xyz); + else + GT[v][i] = (real_t) *(double *) (pb + ixyz + i * xyz); + + pb += vbytes; + } + } + + if(rgb) + { + char * pb = buffer; + for(index_t v = 0; v < n_vertices; ++v) + { + for(index_t i = 0; i < 3; ++i) + if(rgb == 1) + VC[v][i] = ((real_t) *(unsigned char *) (pb + irgb + i * rgb)) / 255; + else + GT[v][i] = (real_t) *(float *) (pb + irgb + i * rgb); + + pb += vbytes; } } + if(buffer != (char *) GT) delete [] buffer; + + while(nf--) { + char buffer[8]; + fread(buffer, 1, fn, fp); if(big_endian) big_to_little(buffer, fn); @@ -163,8 +210,6 @@ void che_ply::read_file(const string & file) for(const index_t & v: trig_convex_polygon(P, nv)) faces.push_back(v); } - - delete [] buffer; } fclose(fp); @@ -173,17 +218,19 @@ void che_ply::read_file(const string & file) if(faces.size() != che::mtrig * n_faces) { vertex * tGT = GT; GT = nullptr; + vertex * tVC = VC; VC = nullptr; free(); alloc(nv, faces.size() / che::mtrig); GT = tGT; + VC = tVC; } memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } -void che_ply::write_file(const che * mesh, const string & file) +void che_ply::write_file(const che * mesh, const string & file, const bool & color) { FILE * fp = fopen((file + ".ply").c_str(), "wb"); assert(fp); @@ -197,11 +244,30 @@ void che_ply::write_file(const che * mesh, const string & file) fprintf(fp, "property %s x\n", type); fprintf(fp, "property %s y\n", type); fprintf(fp, "property %s z\n", type); + if(color) + { + fprintf(fp, "property uchar red\n"); + fprintf(fp, "property uchar green\n"); + fprintf(fp, "property uchar blue\n"); + } fprintf(fp, "element face %lu\n", mesh->n_faces); - fprintf(fp, "property list uchar uint vertex_indices\n"); + fprintf(fp, "property list uchar uint vertex_index\n"); fprintf(fp, "end_header\n"); - fwrite(&mesh->gt(0), sizeof(vertex), mesh->n_vertices, fp); + if(color) + { + unsigned char rgb[3]; + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + fwrite(&mesh->gt(v), sizeof(vertex), 1, fp); + + vertex c = 255 * mesh->color(v); + rgb[0] = c.x; rgb[1] = c.y; rgb[2] = c.z; + + fwrite(rgb, sizeof(rgb), 1, fp); + } + } + else fwrite(&mesh->gt(0), sizeof(vertex), mesh->n_vertices, fp); unsigned char mtrig = che::mtrig; for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 94c71e59..0f91cadd 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -529,6 +529,7 @@ bool viewer::menu_save_mesh(viewer * view) ImGui::Checkbox("point cloud", &point_cloud); break; case 2: + ImGui::Checkbox("vertex color", &vertex_color); break; case 3: ImGui::Checkbox("vertex color", &vertex_color); @@ -543,7 +544,7 @@ bool viewer::menu_save_mesh(viewer * view) break; case 1: che_obj::write_file(mesh, file, vertex_color, point_cloud); break; - case 2: che_ply::write_file(mesh, file); + case 2: che_ply::write_file(mesh, file, vertex_color); break; case 3: che_xyz::write_file(mesh, file, vertex_color); break; From e3c2d79deb4f50e44243e3f24168dd29d3d54a7f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Apr 2021 18:33:22 +0200 Subject: [PATCH 0482/1018] che_ply: loading color --- src/mesh/che_ply.cpp | 49 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 35da7399..00d6d9e5 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -125,21 +125,18 @@ void che_ply::read_file(const string & file) swap(buffer[i], buffer[j]); }; - gproshan_log_var(big_endian); - gproshan_log_var(vbytes); - gproshan_log_var(xyz); - gproshan_log_var(rgb); - gproshan_log_var(fn); - gproshan_log_var(fbytes); - char * buffer = vbytes == sizeof(vertex) ? (char *) GT : new char[vbytes * n_vertices]; fread(buffer, vbytes, n_vertices, fp); if(big_endian) { - char * pb = buffer; - while(nv--) + char * pb; + + #pragma omp parallel for private(pb) + for(index_t v = 0; v < n_vertices; ++v) { + pb = buffer + v * vbytes; + for(index_t i = 0; i < 3; ++i) big_to_little(pb + ixyz + i * xyz, xyz); @@ -148,38 +145,34 @@ void che_ply::read_file(const string & file) for(index_t i = 0; i < 3; ++i) big_to_little(pb + irgb + i * rgb, rgb); } - - pb += vbytes; } } if(vbytes != sizeof(vertex)) { - char * pb = buffer; + char * pb; + + #pragma omp parallel for private(pb) for(index_t v = 0; v < n_vertices; ++v) { + pb = buffer + v * vbytes; + for(index_t i = 0; i < 3; ++i) + { if(xyz == 4) GT[v][i] = (real_t) *(float *) (pb + ixyz + i * xyz); else GT[v][i] = (real_t) *(double *) (pb + ixyz + i * xyz); + } - pb += vbytes; - } - } - - if(rgb) - { - char * pb = buffer; - for(index_t v = 0; v < n_vertices; ++v) - { - for(index_t i = 0; i < 3; ++i) - if(rgb == 1) - VC[v][i] = ((real_t) *(unsigned char *) (pb + irgb + i * rgb)) / 255; - else - GT[v][i] = (real_t) *(float *) (pb + irgb + i * rgb); - - pb += vbytes; + if(rgb) + { + for(index_t i = 0; i < 3; ++i) + if(rgb == 1) + VC[v][i] = ((real_t) *(unsigned char *) (pb + irgb + i * rgb)) / 255; + else + GT[v][i] = (real_t) *(float *) (pb + irgb + i * rgb); + } } } From bf371eaed6757e2b17a3417f507c5c20a511053c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 3 Apr 2021 16:11:29 +0200 Subject: [PATCH 0483/1018] che_ptx: load intensity and rgb --- src/app_viewer.cpp | 2 +- src/mesh/che.cpp | 2 ++ src/mesh/che_ptx.cpp | 6 ++++-- src/viewer/che_viewer.cpp | 1 - 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 67438c1f..e53c24c1 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -764,7 +764,7 @@ bool paint_holes_vertices(viewer * p_view) size_t nv = mesh->n_vertices; - mesh.update(); + // TODO #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index a95ab3b9..5eb35812 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1365,6 +1365,8 @@ void che::alloc(const size_t & n_v, const size_t & n_f) #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) VC[v] = vcolor; + + update_heatmap(); } void che::free() diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 6d479ecb..4b6ec348 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -53,6 +53,7 @@ void che_ptx::read_file(const string & file) { GT[0] = { x, y, z }; VC[0] = { r, g, b }; + VHC[0] = a; for(index_t v = 1; v < n_vertices; ++v) { @@ -60,19 +61,20 @@ void che_ptx::read_file(const string & file) sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b); GT[v] = { x, y, z }; VC[v] = { r, g, b }; + VHC[v] = a; } } else { GT[0] = { x, y, z }; - VC[0] = { a, a, a }; + VHC[0] = a; for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); sscanf(line, "%f %f %f %f", &x, &y, &z, &a); GT[v] = { x, y, z }; - VC[v] = { a, a, a }; + VHC[v] = a; } } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 483b8f30..b1c5388b 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -69,7 +69,6 @@ void che_viewer::update() factor = mesh->mean_edge(); mesh->update_normals(); - mesh->update_heatmap(); update_vbo(); } From 578a4e4a148da96b4637ec73a79d6d2ee5e3b139 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Apr 2021 00:39:45 +0200 Subject: [PATCH 0484/1018] che improve performance of computing opposite half edge --- src/mesh/che.cpp | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 5eb35812..8a6f94d3 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -6,7 +6,6 @@ #include #include #include -#include using namespace std; @@ -1391,10 +1390,23 @@ void che::update_evt_ot_et() memset(EVT, -1, sizeof(index_t) * n_vertices); memset(OT, -1, sizeof(index_t) * n_half_edges); - map * medges = new map[n_vertices]; + vector vnhe; + vnhe.assign(n_vertices, 0); - size_t ne = 0; for(index_t he = 0; he < n_half_edges; ++he) + ++vnhe[VT[he]]; + + vector vhe(n_vertices); + vhe[0] = new index_t[n_half_edges]; + for(index_t v = 1; v < n_vertices; ++v) + vhe[v] = vhe[v - 1] + vnhe[v - 1]; + + vnhe.assign(n_vertices, 0); + for(index_t he = 0; he < n_half_edges; ++he) + vhe[VT[he]][vnhe[VT[he]]++] = he; + + size_t ne = 0; + for(index_t ohe, he = 0; he < n_half_edges; ++he) { const index_t & u = VT[he]; const index_t & v = VT[next(he)]; @@ -1403,20 +1415,29 @@ void che::update_evt_ot_et() if(OT[he] == NIL) { - index_t & ohe = medges[v][u]; - if(ohe && OT[ohe - 1] == NIL) + ohe = NIL; + for(index_t j = 0; j < vnhe[v]; ++j) + { + index_t & h = vhe[v][j]; + if(VT[next(h)] == u) + { + ohe = h; + break; + } + } + + if(ohe != NIL && OT[ohe] == NIL) { ET[ne++] = he; - OT[he] = ohe - 1; - OT[ohe - 1] = he; + OT[he] = ohe; + OT[ohe] = he; } - else medges[u][v] = he + 1; } } - rw(n_edges) = ne; + delete [] vhe[0]; - delete [] medges; + rw(n_edges) = ne; for(index_t he = 0; he < n_half_edges; ++he) From 48af328a044c7dc574753bb6e6867a7a71c8379d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Apr 2021 14:35:58 +0200 Subject: [PATCH 0485/1018] update imgui to v1.82 --- CMakeLists.txt | 32 +-- imgui/imconfig.h | 8 +- imgui/imgui.cpp | 424 ++++++++++++++++++++-------------- imgui/imgui.h | 223 ++++++++++-------- imgui/imgui_demo.cpp | 128 ++++++---- imgui/imgui_draw.cpp | 320 ++++++++++++++++++------- imgui/imgui_impl_glfw.cpp | 2 +- imgui/imgui_impl_opengl3.cpp | 3 +- imgui/imgui_internal.h | 117 ++++++---- imgui/imgui_tables.cpp | 35 +-- imgui/imgui_widgets.cpp | 155 ++++++++----- include/geodesics/geodesics.h | 4 +- 12 files changed, 910 insertions(+), 541 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a31a3f9..01d39124 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_compile_options(-Wall -Wextra) + find_package(CUDAToolkit 11) if(CUDAToolkit_FOUND) enable_language(CUDA) @@ -22,12 +24,12 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80) add_definitions(-DGPROSHAN_CUDA) - include_directories(${CUDAToolkit_INCLUDE_DIRS}) - + include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) + find_package(OptiX 7.2) if(OptiX_INCLUDE) add_definitions(-DGPROSHAN_OPTIX) - include_directories(${OptiX_INCLUDE}) + include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) endif(CUDAToolkit_FOUND) @@ -35,7 +37,7 @@ endif(CUDAToolkit_FOUND) find_package(embree 3.10) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) - include_directories(${embree_INCLUDE_DIRS}) + include_directories(SYSTEM ${embree_INCLUDE_DIRS}) endif(embree_FOUND) @@ -55,17 +57,16 @@ find_package(Boost COMPONENTS thread system) set(THREADS_PREFER_PTHREAD_FLAG ON) -include_directories(${GLEW_INCLUDE_DIRS}) -include_directories(${GLFW3_INCLUDE_DIRS}) -include_directories(${X11_INCLUDE_DIR}) -include_directories(${AMADILLO_INCLUDE_DIR}) -include_directories(${EIGEN3_INCLUDE_DIR}) -include_directories(${SuiteSparse_INCLUDE_DIRS}) -include_directories(${CGAL_INCLUDE_DIRS}) +include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) +include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) +include_directories(SYSTEM ${X11_INCLUDE_DIR}) +include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) +include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR}) +include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) +include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) -include_directories(${gproshan_SOURCE_DIR}/include) -include_directories(${gproshan_SOURCE_DIR}/imgui) +include_directories(SYSTEM ${gproshan_SOURCE_DIR}/imgui) FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) @@ -74,8 +75,10 @@ target_link_libraries(imgui GLEW::GLEW) target_link_libraries(imgui glfw) +include_directories(${gproshan_SOURCE_DIR}/include) add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") + FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) @@ -85,9 +88,6 @@ endif(CUDAToolkit_FOUND) add_library(gproshan SHARED ${cpp_sources} ${cu_sources}) - -target_compile_options(gproshan PRIVATE -Wall -Wno-unused-result) - target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index c5e1650a..39de21c6 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -20,7 +20,9 @@ //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows -// Using dear imgui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +// Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +// DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() +// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. //#define IMGUI_API __declspec( dllexport ) //#define IMGUI_API __declspec( dllimport ) @@ -34,8 +36,8 @@ //#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger window: ShowMetricsWindow() will be empty. //---- Don't implement some functions to reduce linkage requirements. -//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. -//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. +//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) +//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. (imm32.lib/.a) //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index 0e401332..94cd4286 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (main code and documentation) // Help: @@ -15,6 +15,7 @@ // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues +// - Discussions https://github.com/ocornut/imgui/discussions // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). @@ -375,6 +376,25 @@ CODE When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2021/03/12 (1.82) - upgraded ImDrawList::AddRect(), AddRectFilled(), PathRect() to use ImDrawFlags instead of ImDrawCornersFlags. + - ImDrawCornerFlags_TopLeft -> use ImDrawFlags_RoundCornersTopLeft + - ImDrawCornerFlags_BotRight -> use ImDrawFlags_RoundCornersBottomRight + - ImDrawCornerFlags_None -> use ImDrawFlags_RoundCornersNone etc. + flags now sanely defaults to 0 instead of 0x0F, consistent with all other flags in the API. + breaking: the default with rounding > 0.0f is now "round all corners" vs old implicit "round no corners": + - rounding == 0.0f + flags == 0 --> meant no rounding --> unchanged (common use) + - rounding > 0.0f + flags != 0 --> meant rounding --> unchanged (common use) + - rounding == 0.0f + flags != 0 --> meant no rounding --> unchanged (unlikely use) + - rounding > 0.0f + flags == 0 --> meant no rounding --> BREAKING (unlikely use): will now round all corners --> use ImDrawFlags_RoundCornersNone or rounding == 0.0f. + this ONLY matters for hard coded use of 0 + rounding > 0.0f. Use of named ImDrawFlags_RoundCornersNone (new) or ImDrawCornerFlags_None (old) are ok. + the old ImDrawCornersFlags used awkward default values of ~0 or 0xF (4 lower bits set) to signify "round all corners" and we sometimes encouraged using them as shortcuts. + legacy path still support use of hard coded ~0 or any value from 0x1 or 0xF. They will behave the same with legacy paths enabled (will assert otherwise). + - 2021/03/11 (1.82) - removed redirecting functions/enums names that were marked obsolete in 1.66 (September 2018): + - ImGui::SetScrollHere() -> use ImGui::SetScrollHereY() + - 2021/03/11 (1.82) - clarified that ImDrawList::PathArcTo(), ImDrawList::PathArcToFast() won't render with radius < 0.0f. Previously it sorts of accidentally worked but would generally lead to counter-clockwise paths and have an effect on anti-aliasing. + - 2021/03/10 (1.82) - upgraded ImDrawList::AddPolyline() and PathStroke() "bool closed" parameter to "ImDrawFlags flags". The matching ImDrawFlags_Closed value is guaranteed to always stay == 1 in the future. + - 2021/02/22 (1.82) - win32+mingw: Re-enabled IME functions by default even under MinGW. In July 2016, issue #738 had me incorrectly disable those default functions for MinGW. MinGW users should: either link with -limm32, either set their imconfig file with '#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS'. + - 2021/02/17 (1.82) - renamed rarely used style.CircleSegmentMaxError (old default = 1.60f) to style.CircleTessellationMaxError (new default = 0.30f) as the meaning of the value changed. - 2021/02/03 (1.81) - renamed ListBoxHeader(const char* label, ImVec2 size) to BeginListBox(). Kept inline redirection function (will obsolete). - removed ListBoxHeader(const char* label, int items_count, int height_in_items = -1) in favor of specifying size. Kept inline redirection function (will obsolete). - renamed ListBoxFooter() to EndListBox(). Kept inline redirection function (will obsolete). @@ -883,6 +903,7 @@ static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); +static void NavRestoreLayer(ImGuiNavLayer layer); static int FindWindowFocusIndex(ImGuiWindow* window); // Error Checking @@ -909,27 +930,33 @@ static void UpdateViewportsNewFrame(); // [SECTION] CONTEXT AND MEMORY ALLOCATORS //----------------------------------------------------------------------------- +// DLL users: +// - Heaps and globals are not shared across DLL boundaries! +// - You will need to call SetCurrentContext() + SetAllocatorFunctions() for each static/DLL boundary you are calling from. +// - Same apply for hot-reloading mechanisms that are reliant on reloading DLL (note that many hot-reloading mechanism works without DLL). +// - Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. +// - Confused? In a debugger: add GImGui to your watch window and notice how its value changes depending on your current location (which DLL boundary you are in). + // Current context pointer. Implicitly used by all Dear ImGui functions. Always assumed to be != NULL. -// ImGui::CreateContext() will automatically set this pointer if it is NULL. Change to a different context by calling ImGui::SetCurrentContext(). -// 1) Important: globals are not shared across DLL boundaries! If you use DLLs or any form of hot-reloading: you will need to call -// SetCurrentContext() (with the pointer you got from CreateContext) from each unique static/DLL boundary, and after each hot-reloading. -// In your debugger, add GImGui to your watch window and notice how its value changes depending on which location you are currently stepping into. -// 2) Important: Dear ImGui functions are not thread-safe because of this pointer. -// If you want thread-safety to allow N threads to access N different contexts, you can: -// - Change this variable to use thread local storage so each thread can refer to a different context, in imconfig.h: -// struct ImGuiContext; -// extern thread_local ImGuiContext* MyImGuiTLS; -// #define GImGui MyImGuiTLS -// And then define MyImGuiTLS in one of your cpp file. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword. -// - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 -// - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace. +// - ImGui::CreateContext() will automatically set this pointer if it is NULL. +// Change to a different context by calling ImGui::SetCurrentContext(). +// - Important: Dear ImGui functions are not thread-safe because of this pointer. +// If you want thread-safety to allow N threads to access N different contexts: +// - Change this variable to use thread local storage so each thread can refer to a different context, in your imconfig.h: +// struct ImGuiContext; +// extern thread_local ImGuiContext* MyImGuiTLS; +// #define GImGui MyImGuiTLS +// And then define MyImGuiTLS in one of your cpp file. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword. +// - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 +// - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace. +// - DLL users: read comments above. #ifndef GImGui ImGuiContext* GImGui = NULL; #endif // Memory Allocator functions. Use SetAllocatorFunctions() to change them. -// If you use DLL hotreloading you might need to call SetAllocatorFunctions() after reloading code from this file. -// Otherwise, you probably don't want to modify them mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction. +// - You probably don't want to modify those mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction. +// - DLL users: read comments above. #ifndef IMGUI_DISABLE_DEFAULT_ALLOCATORS static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); return malloc(size); } static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); free(ptr); } @@ -937,10 +964,9 @@ static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_d static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(size); IM_ASSERT(0); return NULL; } static void FreeWrapper(void* ptr, void* user_data) { IM_UNUSED(user_data); IM_UNUSED(ptr); IM_ASSERT(0); } #endif - -static void* (*GImAllocatorAllocFunc)(size_t size, void* user_data) = MallocWrapper; -static void (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper; -static void* GImAllocatorUserData = NULL; +static ImGuiMemAllocFunc GImAllocatorAllocFunc = MallocWrapper; +static ImGuiMemFreeFunc GImAllocatorFreeFunc = FreeWrapper; +static void* GImAllocatorUserData = NULL; //----------------------------------------------------------------------------- // [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) @@ -986,7 +1012,7 @@ ImGuiStyle::ImGuiStyle() AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. - CircleSegmentMaxError = 1.60f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. + CircleTessellationMaxError = 0.30f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. // Default theme ImGui::StyleColorsDark(this); @@ -2800,8 +2826,8 @@ void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, const float border_size = g.Style.FrameBorderSize; if (border && border_size > 0.0f) { - window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); - window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, 0, border_size); + window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, 0, border_size); } } @@ -2812,8 +2838,8 @@ void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) const float border_size = g.Style.FrameBorderSize; if (border_size > 0.0f) { - window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, ImDrawCornerFlags_All, border_size); - window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, 0, border_size); + window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, 0, border_size); } } @@ -2839,13 +2865,13 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl bool fully_visible = window->ClipRect.Contains(display_rect); if (!fully_visible) window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); - window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), display_rect.Max - ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, ImDrawCornerFlags_All, THICKNESS); + window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), display_rect.Max - ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, 0, THICKNESS); if (!fully_visible) window->DrawList->PopClipRect(); } if (flags & ImGuiNavHighlightFlags_TypeThin) { - window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, ~0, 1.0f); + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, 1.0f); } } @@ -3016,6 +3042,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) g.ActiveIdTimer = 0.0f; g.ActiveIdHasBeenPressedBefore = false; g.ActiveIdHasBeenEditedBefore = false; + g.ActiveIdMouseButton = -1; if (id != 0) { g.LastActiveId = id; @@ -3114,20 +3141,22 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return IsItemFocused(); // Test for bounding box overlap, as updated as ItemAdd() - if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) + ImGuiItemStatusFlags status_flags = window->DC.LastItemStatusFlags; + if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) return false; IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0); // Flags not supported by this function // Test if we are hovering the right window (our window could be behind another window) - // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable to use IsItemHovered() after EndChild() itself. - // Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was the test that has been running for a long while. - //if (g.HoveredWindow != window) - // return false; - if (g.HoveredRootWindow != window->RootWindow && !(flags & ImGuiHoveredFlags_AllowWhenOverlapped)) - return false; + // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) + // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable + // to use IsItemHovered() after EndChild() itself. Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was + // the test that has been running for a long while. + if (g.HoveredWindow != window && (status_flags & ImGuiItemStatusFlags_HoveredWindow) == 0) + if ((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0) + return false; // Test if another item is active (e.g. being dragged) - if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) + if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) return false; @@ -3222,18 +3251,18 @@ bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. // (Note that we can always TAB out of a widget that doesn't allow tabbing in) - if (g.ActiveId == id && g.FocusTabPressed && !IsActiveIdUsingKey(ImGuiKey_Tab) && g.FocusRequestNextWindow == NULL) + if (g.ActiveId == id && g.TabFocusPressed && !IsActiveIdUsingKey(ImGuiKey_Tab) && g.TabFocusRequestNextWindow == NULL) { - g.FocusRequestNextWindow = window; - g.FocusRequestNextCounterTabStop = window->DC.FocusCounterTabStop + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. + g.TabFocusRequestNextWindow = window; + g.TabFocusRequestNextCounterTabStop = window->DC.FocusCounterTabStop + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. } // Handle focus requests - if (g.FocusRequestCurrWindow == window) + if (g.TabFocusRequestCurrWindow == window) { - if (window->DC.FocusCounterRegular == g.FocusRequestCurrCounterRegular) + if (window->DC.FocusCounterRegular == g.TabFocusRequestCurrCounterRegular) return true; - if (is_tab_stop && window->DC.FocusCounterTabStop == g.FocusRequestCurrCounterTabStop) + if (is_tab_stop && window->DC.FocusCounterTabStop == g.TabFocusRequestCurrCounterTabStop) { g.NavJustTabbedId = id; return true; @@ -3282,7 +3311,7 @@ void* ImGui::MemAlloc(size_t size) { if (ImGuiContext* ctx = GImGui) ctx->IO.MetricsActiveAllocations++; - return GImAllocatorAllocFunc(size, GImAllocatorUserData); + return (*GImAllocatorAllocFunc)(size, GImAllocatorUserData); } // IM_FREE() == ImGui::MemFree() @@ -3291,7 +3320,7 @@ void ImGui::MemFree(void* ptr) if (ptr) if (ImGuiContext* ctx = GImGui) ctx->IO.MetricsActiveAllocations--; - return GImAllocatorFreeFunc(ptr, GImAllocatorUserData); + return (*GImAllocatorFreeFunc)(ptr, GImAllocatorUserData); } const char* ImGui::GetClipboardText() @@ -3328,13 +3357,21 @@ void ImGui::SetCurrentContext(ImGuiContext* ctx) #endif } -void ImGui::SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data) +void ImGui::SetAllocatorFunctions(ImGuiMemAllocFunc alloc_func, ImGuiMemFreeFunc free_func, void* user_data) { GImAllocatorAllocFunc = alloc_func; GImAllocatorFreeFunc = free_func; GImAllocatorUserData = user_data; } +// This is provided to facilitate copying allocators from one static/DLL boundary to another (e.g. retrieve default allocator of your executable address space) +void ImGui::GetAllocatorFunctions(ImGuiMemAllocFunc* p_alloc_func, ImGuiMemFreeFunc* p_free_func, void** p_user_data) +{ + *p_alloc_func = GImAllocatorAllocFunc; + *p_free_func = GImAllocatorFreeFunc; + *p_user_data = GImAllocatorUserData; +} + ImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas) { ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas); @@ -3539,7 +3576,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() { // Handle the edge case of a popup being closed while clicking in its empty space. // If we try to focus it, FocusWindow() > ClosePopupsOverWindow() will accidentally close any parent popups because they are not linked together any more. - ImGuiWindow* root_window = g.HoveredRootWindow; + ImGuiWindow* root_window = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; const bool is_closed_popup = root_window && (root_window->Flags & ImGuiWindowFlags_Popup) && !IsPopupOpen(root_window->PopupId, ImGuiPopupFlags_AnyPopupLevel); if (root_window != NULL && !is_closed_popup) @@ -3682,7 +3719,7 @@ void ImGui::UpdateMouseWheel() const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); const float scale = new_font_scale / window->FontWindowScale; window->FontWindowScale = new_font_scale; - if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) + if (window == window->RootWindow) { const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; SetWindowPos(window, window->Pos + offset, 0); @@ -3731,32 +3768,32 @@ void ImGui::UpdateTabFocus() ImGuiContext& g = *GImGui; // Pressing TAB activate widget focus - g.FocusTabPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); - if (g.ActiveId == 0 && g.FocusTabPressed) + g.TabFocusPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); + if (g.ActiveId == 0 && g.TabFocusPressed) { // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. - g.FocusRequestNextWindow = g.NavWindow; - g.FocusRequestNextCounterRegular = INT_MAX; + g.TabFocusRequestNextWindow = g.NavWindow; + g.TabFocusRequestNextCounterRegular = INT_MAX; if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) - g.FocusRequestNextCounterTabStop = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); + g.TabFocusRequestNextCounterTabStop = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); else - g.FocusRequestNextCounterTabStop = g.IO.KeyShift ? -1 : 0; + g.TabFocusRequestNextCounterTabStop = g.IO.KeyShift ? -1 : 0; } // Turn queued focus request into current one - g.FocusRequestCurrWindow = NULL; - g.FocusRequestCurrCounterRegular = g.FocusRequestCurrCounterTabStop = INT_MAX; - if (g.FocusRequestNextWindow != NULL) + g.TabFocusRequestCurrWindow = NULL; + g.TabFocusRequestCurrCounterRegular = g.TabFocusRequestCurrCounterTabStop = INT_MAX; + if (g.TabFocusRequestNextWindow != NULL) { - ImGuiWindow* window = g.FocusRequestNextWindow; - g.FocusRequestCurrWindow = window; - if (g.FocusRequestNextCounterRegular != INT_MAX && window->DC.FocusCounterRegular != -1) - g.FocusRequestCurrCounterRegular = ImModPositive(g.FocusRequestNextCounterRegular, window->DC.FocusCounterRegular + 1); - if (g.FocusRequestNextCounterTabStop != INT_MAX && window->DC.FocusCounterTabStop != -1) - g.FocusRequestCurrCounterTabStop = ImModPositive(g.FocusRequestNextCounterTabStop, window->DC.FocusCounterTabStop + 1); - g.FocusRequestNextWindow = NULL; - g.FocusRequestNextCounterRegular = g.FocusRequestNextCounterTabStop = INT_MAX; + ImGuiWindow* window = g.TabFocusRequestNextWindow; + g.TabFocusRequestCurrWindow = window; + if (g.TabFocusRequestNextCounterRegular != INT_MAX && window->DC.FocusCounterRegular != -1) + g.TabFocusRequestCurrCounterRegular = ImModPositive(g.TabFocusRequestNextCounterRegular, window->DC.FocusCounterRegular + 1); + if (g.TabFocusRequestNextCounterTabStop != INT_MAX && window->DC.FocusCounterTabStop != -1) + g.TabFocusRequestCurrCounterTabStop = ImModPositive(g.TabFocusRequestNextCounterTabStop, window->DC.FocusCounterTabStop + 1); + g.TabFocusRequestNextWindow = NULL; + g.TabFocusRequestNextCounterRegular = g.TabFocusRequestNextCounterTabStop = INT_MAX; } g.NavIdTabCounter = INT_MAX; @@ -3776,7 +3813,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // Modal windows prevents mouse from hovering behind them. ImGuiWindow* modal_window = GetTopMostPopupModal(); - if (modal_window && g.HoveredRootWindow && !IsWindowChildOf(g.HoveredRootWindow, modal_window)) + if (modal_window && g.HoveredWindow && !IsWindowChildOf(g.HoveredWindow->RootWindow, modal_window)) clear_hovered_windows = true; // Disabled mouse? @@ -3804,7 +3841,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() clear_hovered_windows = true; if (clear_hovered_windows) - g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; + g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL; // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to Dear ImGui + app) if (g.WantCaptureMouseNextFrame != -1) @@ -3839,7 +3876,7 @@ void ImGui::NewFrame() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); ImGuiContext& g = *GImGui; - + // Remove pending delete hooks before frame start. // This deferred removal avoid issues of removal while iterating the hook vector for (int n = g.Hooks.Size - 1; n >= 0; n--) @@ -3878,7 +3915,7 @@ void ImGui::NewFrame() virtual_space.Add(g.Viewports[n]->GetMainRect()); g.DrawListSharedData.ClipRectFullscreen = virtual_space.ToVec4(); g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; - g.DrawListSharedData.SetCircleSegmentMaxError(g.Style.CircleSegmentMaxError); + g.DrawListSharedData.SetCircleTessellationMaxError(g.Style.CircleTessellationMaxError); g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; if (g.Style.AntiAliasedLines) g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; @@ -4128,7 +4165,7 @@ void ImGui::Shutdown(ImGuiContext* context) g.CurrentWindowStack.clear(); g.WindowsById.Clear(); g.NavWindow = NULL; - g.HoveredWindow = g.HoveredRootWindow = g.HoveredWindowUnderMovingWindow = NULL; + g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL; g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; g.MovingWindow = NULL; g.ColorStack.clear(); @@ -4532,7 +4569,6 @@ static void FindHoveredWindow() } g.HoveredWindow = hovered_window; - g.HoveredRootWindow = g.HoveredWindow ? g.HoveredWindow->RootWindow : NULL; g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window; } @@ -4810,6 +4846,8 @@ bool ImGui::IsItemFocused() return true; } +// Important: this can be useful but it is NOT equivalent to the behavior of e.g.Button()! +// Most widgets have specific reactions based on mouse-up/down state, mouse position etc. bool ImGui::IsItemClicked(ImGuiMouseButton mouse_button) { return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); @@ -4998,6 +5036,8 @@ void ImGui::EndChild() // Not navigable into ItemAdd(bb, 0); } + if (g.HoveredWindow == window) + parent_window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredWindow; } g.WithinEndChild = false; g.LogLinePosY = -FLT_MAX; // To enforce a carriage return @@ -5097,9 +5137,10 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) return window; } -static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, ImVec2 new_size) +static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, const ImVec2& size_desired) { ImGuiContext& g = *GImGui; + ImVec2 new_size = size_desired; if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) { // Using -1,-1 on either X/Y axis to preserve the current size. @@ -5370,9 +5411,9 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) { ImVec2 nav_resize_delta; - if (g.NavInputSource == ImGuiInputSource_NavKeyboard && g.IO.KeyShift) + if (g.NavInputSource == ImGuiInputSource_Keyboard && g.IO.KeyShift) nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); - if (g.NavInputSource == ImGuiInputSource_NavGamepad) + if (g.NavInputSource == ImGuiInputSource_Gamepad) nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down); if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f) { @@ -5418,7 +5459,7 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) float rounding = window->WindowRounding; float border_size = window->WindowBorderSize; if (border_size > 0.0f && !(window->Flags & ImGuiWindowFlags_NoBackground)) - window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, ImDrawCornerFlags_All, border_size); + window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, 0, border_size); int border_held = window->ResizeBorderHeld; if (border_held != -1) @@ -5427,7 +5468,7 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); - window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), false, ImMax(2.0f, border_size)); // Thicker than usual + window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), 0, ImMax(2.0f, border_size)); // Thicker than usual } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) { @@ -5476,14 +5517,14 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar } if (override_alpha) bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT); - window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Bot); + window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? 0 : ImDrawFlags_RoundCornersBottom); } // Title bar if (!(flags & ImGuiWindowFlags_NoTitleBar)) { ImU32 title_bar_col = GetColorU32(title_bar_is_highlight ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBg); - window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, window_rounding, ImDrawCornerFlags_Top); + window->DrawList->AddRectFilled(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, window_rounding, ImDrawFlags_RoundCornersTop); } // Menu bar @@ -5491,7 +5532,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar { ImRect menu_bar_rect = window->MenuBarRect(); menu_bar_rect.ClipWith(window->Rect()); // Soft clipping, in particular child window don't have minimum size covering the menu bar so this is useful for them. - window->DrawList->AddRectFilled(menu_bar_rect.Min + ImVec2(window_border_size, 0), menu_bar_rect.Max - ImVec2(window_border_size, 0), GetColorU32(ImGuiCol_MenuBarBg), (flags & ImGuiWindowFlags_NoTitleBar) ? window_rounding : 0.0f, ImDrawCornerFlags_Top); + window->DrawList->AddRectFilled(menu_bar_rect.Min + ImVec2(window_border_size, 0), menu_bar_rect.Max - ImVec2(window_border_size, 0), GetColorU32(ImGuiCol_MenuBarBg), (flags & ImGuiWindowFlags_NoTitleBar) ? window_rounding : 0.0f, ImDrawFlags_RoundCornersTop); if (style.FrameBorderSize > 0.0f && menu_bar_rect.Max.y < window->Pos.y + window->Size.y) window->DrawList->AddLine(menu_bar_rect.GetBL(), menu_bar_rect.GetBR(), GetColorU32(ImGuiCol_Border), style.FrameBorderSize); } @@ -5703,9 +5744,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->PopupId = popup_ref.PopupId; } - if (window_just_appearing_after_hidden_for_resize && !(flags & ImGuiWindowFlags_ChildWindow)) - window->NavLastIds[0] = 0; - // Update ->RootWindow and others pointers (before any possible call to FocusWindow) if (first_begin_of_the_frame) UpdateWindowParentAndRootLinks(window, flags, parent_window); @@ -5848,7 +5886,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { window->Collapsed = !window->Collapsed; MarkIniSettingsDirty(window); - FocusWindow(window); } } else @@ -6111,7 +6148,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) bb.Expand(-g.FontSize - 1.0f); rounding = window->WindowRounding; } - window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, ~0, 3.0f); + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, 0, 3.0f); } // UPDATE RECTANGLES (2- THOSE AFFECTED BY SCROLLING) @@ -6184,7 +6221,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (want_focus) { FocusWindow(window); - NavInitWindow(window, false); + NavInitWindow(window, false); // <-- this is in the way for us to be able to defer and sort reappearing FocusWindow() calls } // Title bar @@ -6355,11 +6392,12 @@ void ImGui::FocusWindow(ImGuiWindow* window) g.NavWindow = window; if (window && g.NavDisableMouseHover) g.NavMousePosDirty = true; - g.NavInitRequest = false; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId g.NavFocusScopeId = 0; g.NavIdIsAlive = false; g.NavLayer = ImGuiNavLayer_Main; + g.NavInitRequest = g.NavMoveRequest = false; + NavUpdateAnyRequestFlag(); //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); } @@ -6414,6 +6452,7 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind FocusWindow(NULL); } +// Important: this alone doesn't alter current ImDrawList state. This is called by PushFont/PopFont only. void ImGui::SetCurrentFont(ImFont* font) { ImGuiContext& g = *GImGui; @@ -6537,30 +6576,28 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function ImGuiContext& g = *GImGui; + if (g.HoveredWindow == NULL) + return false; - if (flags & ImGuiHoveredFlags_AnyWindow) - { - if (g.HoveredWindow == NULL) - return false; - } - else + if ((flags & ImGuiHoveredFlags_AnyWindow) == 0) { + ImGuiWindow* window = g.CurrentWindow; switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) { case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: - if (g.HoveredRootWindow != g.CurrentWindow->RootWindow) + if (g.HoveredWindow->RootWindow != window->RootWindow) return false; break; case ImGuiHoveredFlags_RootWindow: - if (g.HoveredWindow != g.CurrentWindow->RootWindow) + if (g.HoveredWindow != window->RootWindow) return false; break; case ImGuiHoveredFlags_ChildWindows: - if (g.HoveredWindow == NULL || !IsWindowChildOf(g.HoveredWindow, g.CurrentWindow)) + if (!IsWindowChildOf(g.HoveredWindow, window)) return false; break; default: - if (g.HoveredWindow != g.CurrentWindow) + if (g.HoveredWindow != window) return false; break; } @@ -6886,9 +6923,9 @@ void ImGui::SetKeyboardFocusHere(int offset) IM_ASSERT(offset >= -1); // -1 is allowed but not below ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - g.FocusRequestNextWindow = window; - g.FocusRequestNextCounterRegular = window->DC.FocusCounterRegular + 1 + offset; - g.FocusRequestNextCounterTabStop = INT_MAX; + g.TabFocusRequestNextWindow = window; + g.TabFocusRequestNextCounterRegular = window->DC.FocusCounterRegular + 1 + offset; + g.TabFocusRequestNextCounterTabStop = INT_MAX; } void ImGui::SetItemDefaultFocus() @@ -7055,7 +7092,7 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); - IM_ASSERT(g.Style.CircleSegmentMaxError > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.CircleTessellationMaxError > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting!"); // Allows us to avoid a few clamps in color computations IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); @@ -7628,6 +7665,7 @@ void ImGui::BeginGroup() group_data.BackupCurrLineSize = window->DC.CurrLineSize; group_data.BackupCurrLineTextBaseOffset = window->DC.CurrLineTextBaseOffset; group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive; + group_data.BackupHoveredIdIsAlive = g.HoveredId != 0; group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive; group_data.EmitItem = true; @@ -7681,6 +7719,11 @@ void ImGui::EndGroup() window->DC.LastItemId = g.ActiveIdPreviousFrame; window->DC.LastItemRect = group_bb; + // Forward Hovered flag + const bool group_contains_curr_hovered_id = (group_data.BackupHoveredIdIsAlive == false) && g.HoveredId != 0; + if (group_contains_curr_hovered_id) + window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredWindow; + // Forward Edited flag if (group_contains_curr_active_id && g.ActiveIdHasBeenEditedThisFrame) window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; @@ -8450,26 +8493,19 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) // [SECTION] KEYBOARD/GAMEPAD NAVIGATION //----------------------------------------------------------------------------- -// FIXME-NAV: The existence of SetNavID vs SetNavIDWithRectRel vs SetFocusID is incredibly messy and confusing, -// and needs some explanation or serious refactoring. -void ImGui::SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id) +// FIXME-NAV: The existence of SetNavID vs SetFocusID properly needs to be clarified/reworked. +void ImGui::SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; - IM_ASSERT(g.NavWindow); - IM_ASSERT(nav_layer == 0 || nav_layer == 1); + IM_ASSERT(g.NavWindow != NULL); + IM_ASSERT(nav_layer == ImGuiNavLayer_Main || nav_layer == ImGuiNavLayer_Menu); g.NavId = id; + g.NavLayer = (ImGuiNavLayer)nav_layer; g.NavFocusScopeId = focus_scope_id; g.NavWindow->NavLastIds[nav_layer] = id; -} - -void ImGui::SetNavIDWithRectRel(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) -{ - ImGuiContext& g = *GImGui; - SetNavID(id, nav_layer, focus_scope_id); g.NavWindow->NavRectRel[nav_layer] = rect_rel; - g.NavMousePosDirty = true; - g.NavDisableHighlight = false; - g.NavDisableMouseHover = true; + //g.NavDisableHighlight = false; + //g.NavDisableMouseHover = g.NavMousePosDirty = true; } void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) @@ -8768,7 +8804,7 @@ void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags mov static void ImGui::NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window) { ImGuiWindow* parent = nav_window; - while (parent && (parent->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (parent->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) + while (parent && parent->RootWindow != parent && (parent->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) parent = parent->ParentWindow; if (parent && parent != nav_window) parent->NavLastChildNavWindow = nav_window; @@ -8783,17 +8819,23 @@ static ImGuiWindow* ImGui::NavRestoreLastChildNavWindow(ImGuiWindow* window) return window; } -static void NavRestoreLayer(ImGuiNavLayer layer) +void ImGui::NavRestoreLayer(ImGuiNavLayer layer) { ImGuiContext& g = *GImGui; - g.NavLayer = layer; - if (layer == 0) - g.NavWindow = ImGui::NavRestoreLastChildNavWindow(g.NavWindow); + if (layer == ImGuiNavLayer_Main) + g.NavWindow = NavRestoreLastChildNavWindow(g.NavWindow); ImGuiWindow* window = g.NavWindow; - if (layer == 0 && window->NavLastIds[0] != 0) - ImGui::SetNavIDWithRectRel(window->NavLastIds[0], layer, 0, window->NavRectRel[0]); + if (window->NavLastIds[layer] != 0) + { + SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); + g.NavDisableHighlight = false; + g.NavDisableMouseHover = g.NavMousePosDirty = true; + } else - ImGui::NavInitWindow(window, true); + { + g.NavLayer = layer; + NavInitWindow(window, true); + } } static inline void ImGui::NavUpdateAnyRequestFlag() @@ -8811,12 +8853,12 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) IM_ASSERT(window == g.NavWindow); bool init_for_nav = false; if (!(window->Flags & ImGuiWindowFlags_NoNavInputs)) - if (!(window->Flags & ImGuiWindowFlags_ChildWindow) || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) + if (window == window->RootWindow || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) init_for_nav = true; IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from NavInitWindow(), init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); if (init_for_nav) { - SetNavID(0, g.NavLayer, 0); + SetNavID(0, g.NavLayer, 0, ImRect()); g.NavInitRequest = true; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; @@ -8904,17 +8946,17 @@ static void ImGui::NavUpdate() // (do it before we map Keyboard input!) bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; - if (nav_gamepad_active && g.NavInputSource != ImGuiInputSource_NavGamepad) + if (nav_gamepad_active && g.NavInputSource != ImGuiInputSource_Gamepad) { if (io.NavInputs[ImGuiNavInput_Activate] > 0.0f || io.NavInputs[ImGuiNavInput_Input] > 0.0f || io.NavInputs[ImGuiNavInput_Cancel] > 0.0f || io.NavInputs[ImGuiNavInput_Menu] > 0.0f || io.NavInputs[ImGuiNavInput_DpadLeft] > 0.0f || io.NavInputs[ImGuiNavInput_DpadRight] > 0.0f || io.NavInputs[ImGuiNavInput_DpadUp] > 0.0f || io.NavInputs[ImGuiNavInput_DpadDown] > 0.0f) - g.NavInputSource = ImGuiInputSource_NavGamepad; + g.NavInputSource = ImGuiInputSource_Gamepad; } // Update Keyboard->Nav inputs mapping if (nav_keyboard_active) { - #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(io.KeyMap[_KEY])) { io.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_NavKeyboard; } } while (0) + #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(io.KeyMap[_KEY])) { io.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_Keyboard; } } while (0) NAV_MAP_KEY(ImGuiKey_Space, ImGuiNavInput_Activate ); NAV_MAP_KEY(ImGuiKey_Enter, ImGuiNavInput_Input ); NAV_MAP_KEY(ImGuiKey_Escape, ImGuiNavInput_Cancel ); @@ -8935,7 +8977,7 @@ static void ImGui::NavUpdate() io.NavInputsDownDuration[i] = (io.NavInputs[i] > 0.0f) ? (io.NavInputsDownDuration[i] < 0.0f ? 0.0f : io.NavInputsDownDuration[i] + io.DeltaTime) : -1.0f; // Process navigation init request (select first/default focus) - if (g.NavInitResultId != 0 && (!g.NavDisableHighlight || g.NavInitRequestFromMove)) + if (g.NavInitResultId != 0) NavUpdateInitResult(); g.NavInitRequest = false; g.NavInitRequestFromMove = false; @@ -8960,13 +9002,11 @@ static void ImGui::NavUpdate() { // Set mouse position given our knowledge of the navigated item position from last frame if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) - { if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) { io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); io.WantSetMousePos = true; } - } g.NavMousePosDirty = false; } g.NavIdIsAlive = false; @@ -8995,18 +9035,15 @@ static void ImGui::NavUpdate() if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) ClearActiveID(); } - else if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow) && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) + else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) { // Exit child window ImGuiWindow* child_window = g.NavWindow; ImGuiWindow* parent_window = g.NavWindow->ParentWindow; IM_ASSERT(child_window->ChildId != 0); + ImRect child_rect = child_window->Rect(); FocusWindow(parent_window); - SetNavID(child_window->ChildId, 0, 0); - // Reassigning with same value, we're being explicit here. - g.NavIdIsAlive = false; // -V1048 - if (g.NavDisableMouseHover) - g.NavMousePosDirty = true; + SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, ImRect(child_rect.Min - parent_window->Pos, child_rect.Max - parent_window->Pos)); } else if (g.OpenPopupStack.Size > 0) { @@ -9133,7 +9170,7 @@ static void ImGui::NavUpdate() // When using gamepad, we project the reference nav bounding box into window visible area. // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad every movements are relative // (can't focus a visible object like we can with the mouse). - if (g.NavMoveRequest && g.NavInputSource == ImGuiInputSource_NavGamepad && g.NavLayer == ImGuiNavLayer_Main) + if (g.NavMoveRequest && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main) { ImGuiWindow* window = g.NavWindow; ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1, 1), window->InnerRect.Max - window->Pos + ImVec2(1, 1)); @@ -9148,7 +9185,7 @@ static void ImGui::NavUpdate() } // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) - ImRect nav_rect_rel = g.NavWindow ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); + ImRect nav_rect_rel = g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted() ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : ImRect(0, 0, 0, 0); g.NavScoringRect.TranslateY(nav_scoring_rect_offset_y); g.NavScoringRect.Min.x = ImMin(g.NavScoringRect.Min.x + 1.0f, g.NavScoringRect.Max.x); @@ -9175,11 +9212,12 @@ static void ImGui::NavUpdateInitResult() // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); + SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); if (g.NavInitRequestFromMove) - SetNavIDWithRectRel(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); - else - SetNavID(g.NavInitResultId, g.NavLayer, 0); - g.NavWindow->NavRectRel[g.NavLayer] = g.NavInitResultRectRel; + { + g.NavDisableHighlight = false; + g.NavDisableMouseHover = g.NavMousePosDirty = true; + } } // Apply result from previous frame navigation directional move request @@ -9242,7 +9280,9 @@ static void ImGui::NavUpdateMoveResult() g.NavJustMovedToKeyMods = g.NavMoveRequestKeyMods; } IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); - SetNavIDWithRectRel(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); + SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); + g.NavDisableHighlight = false; + g.NavDisableMouseHover = g.NavMousePosDirty = true; } // Handle PageUp/PageDown/Home/End keys @@ -9446,12 +9486,12 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; // FIXME-DOCK: Will need to use RootWindowDockStop g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true; - g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_NavKeyboard : ImGuiInputSource_NavGamepad; + g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; } // Gamepad update g.NavWindowingTimer += g.IO.DeltaTime; - if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavGamepad) + if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_Gamepad) { // Highlight only appears after a brief time holding the button, so that a fast tap on PadMenu (to toggle NavLayer) doesn't add visual noise g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); @@ -9477,7 +9517,7 @@ static void ImGui::NavUpdateWindowing() } // Keyboard: Focus - if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_NavKeyboard) + if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_Keyboard) { // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f @@ -9499,9 +9539,9 @@ static void ImGui::NavUpdateWindowing() if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove)) { ImVec2 move_delta; - if (g.NavInputSource == ImGuiInputSource_NavKeyboard && !g.IO.KeyShift) + if (g.NavInputSource == ImGuiInputSource_Keyboard && !g.IO.KeyShift) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); - if (g.NavInputSource == ImGuiInputSource_NavGamepad) + if (g.NavInputSource == ImGuiInputSource_Gamepad) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); if (move_delta.x != 0.0f || move_delta.y != 0.0f) { @@ -9552,8 +9592,10 @@ static void ImGui::NavUpdateWindowing() g.NavDisableHighlight = false; g.NavDisableMouseHover = true; - // When entering a regular menu bar with the Alt key, we always reinitialize the navigation ID. + // Reinitialize navigation when entering menu bar with the Alt key. const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; + if (new_nav_layer == ImGuiNavLayer_Menu) + g.NavWindow->NavLastIds[new_nav_layer] = 0; NavRestoreLayer(new_nav_layer); } } @@ -9617,27 +9659,45 @@ void ImGui::ClearDragDrop() memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); } -// Call when current ID is active. // When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() +// If the item has an identifier: +// - This assume/require the item to be activated (typically via ButtonBehavior). +// - Therefore if you want to use this with a mouse button other than left mouse button, it is up to the item itself to activate with another button. +// - We then pull and use the mouse button that was used to activate the item and use it to carry on the drag. +// If the item has no identifier: +// - Currently always assume left mouse button. bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + // FIXME-DRAGDROP: While in the common-most "drag from non-zero active id" case we can tell the mouse button, + // in both SourceExtern and id==0 cases we may requires something else (explicit flags or some heuristic). + ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; + bool source_drag_active = false; ImGuiID source_id = 0; ImGuiID source_parent_id = 0; - ImGuiMouseButton mouse_button = ImGuiMouseButton_Left; if (!(flags & ImGuiDragDropFlags_SourceExtern)) { source_id = window->DC.LastItemId; - if (source_id != 0 && g.ActiveId != source_id) // Early out for most common case - return false; - if (g.IO.MouseDown[mouse_button] == false) - return false; - - if (source_id == 0) + if (source_id != 0) + { + // Common path: items with ID + if (g.ActiveId != source_id) + return false; + if (g.ActiveIdMouseButton != -1) + mouse_button = g.ActiveIdMouseButton; + if (g.IO.MouseDown[mouse_button] == false) + return false; + g.ActiveIdAllowOverlap = false; + } + else { + // Uncommon path: items without ID + if (g.IO.MouseDown[mouse_button] == false) + return false; + // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride. if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) @@ -9664,10 +9724,6 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) if (g.ActiveId == source_id) // Allow the underlying widget to display/return hovered during the mouse release frame, else we would get a flicker. g.ActiveIdAllowOverlap = is_hovered; } - else - { - g.ActiveIdAllowOverlap = false; - } if (g.ActiveId != source_id) return false; source_parent_id = window->IDStack.back(); @@ -9870,11 +9926,11 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) { - // FIXME-DRAG: Settle on a proper default visuals for drop target. + // FIXME-DRAGDROP: Settle on a proper default visuals for drop target. r.Expand(3.5f); bool push_clip_rect = !window->ClipRect.Contains(r); if (push_clip_rect) window->DrawList->PushClipRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1)); - window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, ~0, 2.0f); + window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); if (push_clip_rect) window->DrawList->PopClipRect(); } @@ -9909,14 +9965,8 @@ void ImGui::EndDragDropTarget() //----------------------------------------------------------------------------- // Pass text data straight to log (without being displayed) -void ImGui::LogText(const char* fmt, ...) +static inline void LogTextV(ImGuiContext& g, const char* fmt, va_list args) { - ImGuiContext& g = *GImGui; - if (!g.LogEnabled) - return; - - va_list args; - va_start(args, fmt); if (g.LogFile) { g.LogBuffer.Buf.resize(0); @@ -9927,9 +9977,29 @@ void ImGui::LogText(const char* fmt, ...) { g.LogBuffer.appendfv(fmt, args); } +} + +void ImGui::LogText(const char* fmt, ...) +{ + ImGuiContext& g = *GImGui; + if (!g.LogEnabled) + return; + + va_list args; + va_start(args, fmt); + LogTextV(g, fmt, args); va_end(args); } +void ImGui::LogTextV(const char* fmt, va_list args) +{ + ImGuiContext& g = *GImGui; + if (!g.LogEnabled) + return; + + LogTextV(g, fmt, args); +} + // Internal version that takes a position to decide on newline placement and pad items according to their depth. // We split text into individual lines to add current tree level padding // FIXME: This code is a little complicated perhaps, considering simplifying the whole system. @@ -10620,7 +10690,7 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text) #endif // Win32 API IME support (for Asian languages, etc.) -#if defined(_WIN32) && !defined(__GNUC__) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) +#if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) #include #ifdef _MSC_VER @@ -10844,7 +10914,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) BulletText("Table 0x%08X (%d columns, in '%s')", table->ID, table->ColumnsCount, table->OuterWindow->Name); if (IsItemHovered()) - GetForegroundDrawList()->AddRect(table->OuterRect.Min - ImVec2(1, 1), table->OuterRect.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + GetForegroundDrawList()->AddRect(table->OuterRect.Min - ImVec2(1, 1), table->OuterRect.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f); Indent(); char buf[128]; for (int rect_n = 0; rect_n < TRT_Count; rect_n++) @@ -10859,7 +10929,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) Col %d %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), column_n, trt_rects_names[rect_n]); Selectable(buf); if (IsItemHovered()) - GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f); } } else @@ -10868,7 +10938,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImFormatString(buf, IM_ARRAYSIZE(buf), "(%6.1f,%6.1f) (%6.1f,%6.1f) Size (%6.1f,%6.1f) %s", r.Min.x, r.Min.y, r.Max.x, r.Max.y, r.GetWidth(), r.GetHeight(), trt_rects_names[rect_n]); Selectable(buf); if (IsItemHovered()) - GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, ~0, 2.0f); + GetForegroundDrawList()->AddRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1), IM_COL32(255, 255, 0, 255), 0.0f, 0, 2.0f); } } Unindent(); @@ -11004,7 +11074,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("WINDOWING"); Indent(); Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); - Text("HoveredRootWindow: '%s'", g.HoveredRootWindow ? g.HoveredRootWindow->Name : "NULL"); + Text("HoveredWindow->Root: '%s'", g.HoveredWindow ? g.HoveredWindow->RootWindow->Name : "NULL"); Text("HoveredWindowUnderMovingWindow: '%s'", g.HoveredWindowUnderMovingWindow ? g.HoveredWindowUnderMovingWindow->Name : "NULL"); Text("MovingWindow: '%s'", g.MovingWindow ? g.MovingWindow->Name : "NULL"); Unindent(); @@ -11074,7 +11144,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImRect r = Funcs::GetTableRect(table, cfg->ShowTablesRectsType, column_n); ImU32 col = (table->HoveredColumnBody == column_n) ? IM_COL32(255, 255, 128, 255) : IM_COL32(255, 0, 128, 255); float thickness = (table->HoveredColumnBody == column_n) ? 3.0f : 1.0f; - draw_list->AddRect(r.Min, r.Max, col, 0.0f, ~0, thickness); + draw_list->AddRect(r.Min, r.Max, col, 0.0f, 0, thickness); } } else @@ -11192,7 +11262,7 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, { ImDrawListFlags backup_flags = fg_draw_list->Flags; fg_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. - fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); + fg_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), ImDrawFlags_Closed, 1.0f); fg_draw_list->Flags = backup_flags; } } @@ -11219,7 +11289,7 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co for (int n = 0; n < 3; n++, idx_n++) vtxs_rect.Add((triangle[n] = vtx_buffer[idx_buffer ? idx_buffer[idx_n] : idx_n].pos)); if (show_mesh) - out_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), true, 1.0f); // In yellow: mesh triangles + out_draw_list->AddPolyline(triangle, 3, IM_COL32(255, 255, 0, 255), ImDrawFlags_Closed, 1.0f); // In yellow: mesh triangles } // Draw bounding boxes if (show_aabb) @@ -11289,7 +11359,7 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport) viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y, viewport->WorkOffsetMin.x, viewport->WorkOffsetMin.y, viewport->WorkOffsetMax.x, viewport->WorkOffsetMax.y); BulletText("Flags: 0x%04X =%s%s%s", viewport->Flags, - (flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", + (flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", (flags & ImGuiViewportFlags_IsPlatformMonitor) ? " IsPlatformMonitor" : "", (flags & ImGuiViewportFlags_OwnedByApp) ? " OwnedByApp" : ""); for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) diff --git a/imgui/imgui.h b/imgui/imgui.h index 408969ad..874ed8f2 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (headers) // Help: @@ -15,6 +15,7 @@ // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues +// - Discussions https://github.com/ocornut/imgui/discussions /* @@ -27,11 +28,11 @@ Index of this file: // [SECTION] ImGuiStyle // [SECTION] ImGuiIO // [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) -// [SECTION] Obsolete functions // [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) -// [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) +// [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) +// [SECTION] Obsolete functions and types */ @@ -59,8 +60,8 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.81" -#define IMGUI_VERSION_NUM 18100 +#define IMGUI_VERSION "1.82" +#define IMGUI_VERSION_NUM 18200 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE @@ -161,8 +162,8 @@ typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A typedef int ImGuiSortDirection; // -> enum ImGuiSortDirection_ // Enum: A sorting direction (ascending or descending) typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A color target for TableSetBgColor() -typedef int ImDrawCornerFlags; // -> enum ImDrawCornerFlags_ // Flags: for ImDrawList::AddRect(), AddRectFilled() etc. -typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList +typedef int ImDrawFlags; // -> enum ImDrawFlags_ // Flags: for ImDrawList functions +typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList instance typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for InvisibleButton() @@ -193,6 +194,8 @@ typedef void* ImTextureID; // User data for rendering backend to identi typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); // Callback function for ImGui::InputText() typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints() +typedef void* (*ImGuiMemAllocFunc)(size_t sz, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() +typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() // Character types // (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) @@ -255,8 +258,9 @@ struct ImVec4 namespace ImGui { // Context creation and access - // Each context create its own ImFontAtlas by default. You may instance one yourself and pass it to CreateContext() to share a font atlas between imgui contexts. - // None of those functions is reliant on the current context. + // - Each context create its own ImFontAtlas by default. You may instance one yourself and pass it to CreateContext() to share a font atlas between contexts. + // - DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() + // for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for details. IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = destroy current context IMGUI_API ImGuiContext* GetCurrentContext(); @@ -701,7 +705,7 @@ namespace ImGui // - You may manually submit headers using TableNextRow() + TableHeader() calls, but this is only useful in // some advanced use cases (e.g. adding custom widgets in header row). // - Use TableSetupScrollFreeze() to lock columns/rows so they stay visible when scrolled. - IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = 0.0f, ImU32 user_id = 0); + IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = 0.0f, ImGuiID user_id = 0); IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) @@ -711,7 +715,7 @@ namespace ImGui // since last call, or the first time. Make sure to set 'SpecsDirty = false' after sorting, else you may // wastefully sort your data every frame! // - Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). - IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). + IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). // Tables: Miscellaneous functions // - Functions args 'int column_n' treat the default value of -1 as the same as passing the current column index. IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable) @@ -748,10 +752,14 @@ namespace ImGui IMGUI_API void LogFinish(); // stop logging (close file, etc.) IMGUI_API void LogButtons(); // helper to display buttons for logging to tty/file/clipboard IMGUI_API void LogText(const char* fmt, ...) IM_FMTARGS(1); // pass text data straight to log (without being displayed) + IMGUI_API void LogTextV(const char* fmt, va_list args) IM_FMTLIST(1); // Drag and Drop - // - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip as replacement) - IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call when the current item is active. If this return true, you can call SetDragDropPayload() + EndDragDropSource() + // - On source items, call BeginDragDropSource(), if it returns true also call SetDragDropPayload() + EndDragDropSource(). + // - On target candidates, call BeginDragDropTarget(), if it returns true also call AcceptDragDropPayload() + EndDragDropTarget(). + // - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip, see #1725) + // - An item can be both drag source and drop target. + IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call after submitting an item which may be dragged. when this return true, you can call SetDragDropPayload() + EndDragDropSource() IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. IMGUI_API void EndDragDropSource(); // only call EndDragDropSource() if BeginDragDropSource() returns true! IMGUI_API bool BeginDragDropTarget(); // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget() @@ -770,12 +778,12 @@ namespace ImGui IMGUI_API void SetKeyboardFocusHere(int offset = 0); // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget. // Item/Widgets Utilities - // - Most of the functions are referring to the last/previous item we submitted. + // - Most of the functions are referring to the previous Item that has been submitted. // - See Demo Window under "Widgets->Querying Status" for an interactive visualization of most of those functions. IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags = 0); // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options. IMGUI_API bool IsItemActive(); // is the last item active? (e.g. button being held, text field being edited. This will continuously return true while holding mouse button on an item. Items that don't interact will always return false) IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation? - IMGUI_API bool IsItemClicked(ImGuiMouseButton mouse_button = 0); // is the last item clicked? (e.g. button/node just clicked on) == IsMouseClicked(mouse_button) && IsItemHovered() + IMGUI_API bool IsItemClicked(ImGuiMouseButton mouse_button = 0); // is the last item hovered and mouse clicked on? (**) == IsMouseClicked(mouse_button) && IsItemHovered()Important. (**) this it NOT equivalent to the behavior of e.g. Button(). Read comments in function definition. IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling) IMGUI_API bool IsItemEdited(); // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the "bool" return value of many widgets. IMGUI_API bool IsItemActivated(); // was the last item just made active (item was previously inactive). @@ -867,9 +875,11 @@ namespace ImGui IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. // Memory Allocators - // - All those functions are not reliant on the current context. - // - If you reload the contents of imgui.cpp at runtime, you may need to call SetCurrentContext() + SetAllocatorFunctions() again because we use global storage for those. - IMGUI_API void SetAllocatorFunctions(void* (*alloc_func)(size_t sz, void* user_data), void (*free_func)(void* ptr, void* user_data), void* user_data = NULL); + // - Those functions are not reliant on the current context. + // - DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() + // for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. + IMGUI_API void SetAllocatorFunctions(ImGuiMemAllocFunc alloc_func, ImGuiMemFreeFunc free_func, void* user_data = NULL); + IMGUI_API void GetAllocatorFunctions(ImGuiMemAllocFunc* p_alloc_func, ImGuiMemFreeFunc* p_free_func, void** p_user_data); IMGUI_API void* MemAlloc(size_t size); IMGUI_API void MemFree(void* ptr); @@ -936,7 +946,7 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_AllowTabInput = 1 << 10, // Pressing TAB input a '\t' character into the text field ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, // In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter (default is opposite: unfocus with Ctrl+Enter, add line with Enter). ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, // Disable following the cursor horizontally - ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, // Insert mode + ImGuiInputTextFlags_AlwaysOverwrite = 1 << 13, // Overwrite mode ImGuiInputTextFlags_ReadOnly = 1 << 14, // Read-only mode ImGuiInputTextFlags_Password = 1 << 15, // Password mode, display all characters as '*' ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). @@ -946,6 +956,11 @@ enum ImGuiInputTextFlags_ // [Internal] ImGuiInputTextFlags_Multiline = 1 << 20, // For internal use by InputTextMultiline() ImGuiInputTextFlags_NoMarkEdited = 1 << 21 // For internal use by functions using InputText() before reformatting data + + // Obsolete names (will be removed soon) +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + , ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior +#endif }; // Flags for ImGui::TreeNodeEx(), ImGui::CollapsingHeader*() @@ -1723,7 +1738,7 @@ struct ImGuiStyle bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. Latched at the beginning of the frame (copied to ImDrawList). bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. - float CircleSegmentMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. + float CircleTessellationMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. ImVec4 Colors[ImGuiCol_COUNT]; IMGUI_API ImGuiStyle(); @@ -1964,51 +1979,6 @@ struct ImGuiTableSortSpecs ImGuiTableSortSpecs() { memset(this, 0, sizeof(*this)); } }; -//----------------------------------------------------------------------------- -// [SECTION] Obsolete functions -// (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) -// Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. -//----------------------------------------------------------------------------- - -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -namespace ImGui -{ - // OBSOLETED in 1.81 (from February 2021) - IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items - static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } - static inline void ListBoxFooter() { EndListBox(); } - // OBSOLETED in 1.79 (from August 2020) - static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! - // OBSOLETED in 1.78 (from June 2020) - // Old drag/sliders functions that took a 'float power = 1.0' argument instead of flags. - // For shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. - IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power); - IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power); - static inline bool DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } - IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power); - IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format, float power); - static inline bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } - static inline bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } - static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } - static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } - // OBSOLETED in 1.77 (from June 2020) - static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } - // OBSOLETED in 1.72 (from April 2019) - static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } - // OBSOLETED in 1.71 (from June 2019) - static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } - // OBSOLETED in 1.70 (from May 2019) - static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } - // OBSOLETED in 1.69 (from Mar 2019) - static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } - // OBSOLETED in 1.66 (from Sep 2018) - static inline void SetScrollHere(float center_ratio=0.5f){ SetScrollHereY(center_ratio); } -} -#endif - //----------------------------------------------------------------------------- // [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) //----------------------------------------------------------------------------- @@ -2312,21 +2282,27 @@ struct ImDrawListSplitter IMGUI_API void SetCurrentChannel(ImDrawList* draw_list, int channel_idx); }; -enum ImDrawCornerFlags_ +// Flags for ImDrawList functions +// (Legacy: bit 0 must always correspond to ImDrawFlags_Closed to be backward compatible with old API using a bool. Bits 1..3 must be unused) +enum ImDrawFlags_ { - ImDrawCornerFlags_None = 0, - ImDrawCornerFlags_TopLeft = 1 << 0, // 0x1 - ImDrawCornerFlags_TopRight = 1 << 1, // 0x2 - ImDrawCornerFlags_BotLeft = 1 << 2, // 0x4 - ImDrawCornerFlags_BotRight = 1 << 3, // 0x8 - ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, // 0x3 - ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, // 0xC - ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, // 0x5 - ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, // 0xA - ImDrawCornerFlags_All = 0xF // In your function calls you may use ~0 (= all bits sets) instead of ImDrawCornerFlags_All, as a convenience + ImDrawFlags_None = 0, + ImDrawFlags_Closed = 1 << 0, // PathStroke(), AddPolyline(): specify that shape should be closed (Important: this is always == 1 for legacy reason) + ImDrawFlags_RoundCornersTopLeft = 1 << 4, // AddRect(), AddRectFilled(), PathRect(): enable rounding top-left corner only (when rounding > 0.0f, we default to all corners). Was 0x01. + ImDrawFlags_RoundCornersTopRight = 1 << 5, // AddRect(), AddRectFilled(), PathRect(): enable rounding top-right corner only (when rounding > 0.0f, we default to all corners). Was 0x02. + ImDrawFlags_RoundCornersBottomLeft = 1 << 6, // AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-left corner only (when rounding > 0.0f, we default to all corners). Was 0x04. + ImDrawFlags_RoundCornersBottomRight = 1 << 7, // AddRect(), AddRectFilled(), PathRect(): enable rounding bottom-right corner only (when rounding > 0.0f, we default to all corners). Wax 0x08. + ImDrawFlags_RoundCornersNone = 1 << 8, // AddRect(), AddRectFilled(), PathRect(): disable rounding on all corners (when rounding > 0.0f). This is NOT zero, NOT an implicit flag! + ImDrawFlags_RoundCornersTop = ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight, + ImDrawFlags_RoundCornersBottom = ImDrawFlags_RoundCornersBottomLeft | ImDrawFlags_RoundCornersBottomRight, + ImDrawFlags_RoundCornersLeft = ImDrawFlags_RoundCornersBottomLeft | ImDrawFlags_RoundCornersTopLeft, + ImDrawFlags_RoundCornersRight = ImDrawFlags_RoundCornersBottomRight | ImDrawFlags_RoundCornersTopRight, + ImDrawFlags_RoundCornersAll = ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight | ImDrawFlags_RoundCornersBottomLeft | ImDrawFlags_RoundCornersBottomRight, + ImDrawFlags_RoundCornersDefault_ = ImDrawFlags_RoundCornersAll, // Default to ALL corners if none of the _RoundCornersXX flags are specified. + ImDrawFlags_RoundCornersMask_ = ImDrawFlags_RoundCornersAll | ImDrawFlags_RoundCornersNone }; -// Flags for ImDrawList. Those are set automatically by ImGui:: functions from ImGuiIO settings, and generally not manipulated directly. +// Flags for ImDrawList instance. Those are set automatically by ImGui:: functions from ImGuiIO settings, and generally not manipulated directly. // It is however possible to temporarily alter flags between calls to ImDrawList:: functions. enum ImDrawListFlags_ { @@ -2386,8 +2362,8 @@ struct ImDrawList // In future versions we will use textures to provide cheaper and higher-quality circles. // Use AddNgon() and AddNgonFilled() functions if you need to guaranteed a specific number of sides. IMGUI_API void AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness = 1.0f); - IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size), rounding_corners_flags: 4 bits corresponding to which corner to round - IMGUI_API void AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); // a: upper-left, b: lower-right (== upper-left + size) + IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawFlags flags = 0, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size) + IMGUI_API void AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawFlags flags = 0); // a: upper-left, b: lower-right (== upper-left + size) IMGUI_API void AddRectFilledMultiColor(const ImVec2& p_min, const ImVec2& p_max, ImU32 col_upr_left, ImU32 col_upr_right, ImU32 col_bot_right, ImU32 col_bot_left); IMGUI_API void AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness = 1.0f); IMGUI_API void AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col); @@ -2399,7 +2375,7 @@ struct ImDrawList IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); - IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, bool closed, float thickness); + IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness); IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. IMGUI_API void AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments = 0); // Quadratic Bezier (3 control points) @@ -2410,19 +2386,19 @@ struct ImDrawList // - "uv_min" and "uv_max" represent the normalized texture coordinates to use for those corners. Using (0,0)->(1,1) texture coordinates will generally display the entire texture. IMGUI_API void AddImage(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min = ImVec2(0, 0), const ImVec2& uv_max = ImVec2(1, 1), ImU32 col = IM_COL32_WHITE); IMGUI_API void AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, const ImVec2& uv1 = ImVec2(0, 0), const ImVec2& uv2 = ImVec2(1, 0), const ImVec2& uv3 = ImVec2(1, 1), const ImVec2& uv4 = ImVec2(0, 1), ImU32 col = IM_COL32_WHITE); - IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); + IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags = 0); // Stateful path API, add points then finish with PathFillConvex() or PathStroke() inline void PathClear() { _Path.Size = 0; } inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size - 1], &pos, 8) != 0) _Path.push_back(pos); } inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } // Note: Anti-aliased filling requires points to be in clockwise order. - inline void PathStroke(ImU32 col, bool closed, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, closed, thickness); _Path.Size = 0; } - IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 10); + inline void PathStroke(ImU32 col, ImDrawFlags flags = 0, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, flags, thickness); _Path.Size = 0; } + IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 0); IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle IMGUI_API void PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments = 0); // Quadratic Bezier (3 control points) - IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawCornerFlags rounding_corners = ImDrawCornerFlags_All); + IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawFlags flags = 0); // Advanced IMGUI_API void AddCallback(ImDrawCallback callback, void* callback_data); // Your rendering function must check for 'UserCallback' in ImDrawCmd and call the function instead of rendering triangles. @@ -2463,6 +2439,9 @@ struct ImDrawList IMGUI_API void _OnChangedClipRect(); IMGUI_API void _OnChangedTextureID(); IMGUI_API void _OnChangedVtxOffset(); + IMGUI_API int _CalcCircleAutoSegmentCount(float radius) const; + IMGUI_API void _PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step); + IMGUI_API void _PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments); }; // All draw data to render a Dear ImGui frame @@ -2631,11 +2610,12 @@ struct ImFontAtlas //------------------------------------------- // You can request arbitrary rectangles to be packed into the atlas, for your own purposes. - // After calling Build(), you can query the rectangle position and render your pixels. - // You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), - // so you can render e.g. custom colorful icons and use them as regular glyphs. - // Read docs/FONTS.md for more details about using colorful icons. - // Note: this API may be redesigned later in order to support multi-monitor varying DPI settings. + // - After calling Build(), you can query the rectangle position and render your pixels. + // - If you render colored output, set 'atlas->TexPixelsUseColors = true' as this may help some backends decide of prefered texture format. + // - You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), + // so you can render e.g. custom colorful icons and use them as regular glyphs. + // - Read docs/FONTS.md for more details about using colorful icons. + // - Note: this API may be redesigned later in order to support multi-monitor varying DPI settings. IMGUI_API int AddCustomRectRegular(int width, int height); IMGUI_API int AddCustomRectFontGlyph(ImFont* font, ImWchar id, int width, int height, float advance_x, const ImVec2& offset = ImVec2(0, 0)); ImFontAtlasCustomRect* GetCustomRectByIndex(int index) { IM_ASSERT(index >= 0); return &CustomRects[index]; } @@ -2648,14 +2628,15 @@ struct ImFontAtlas // Members //------------------------------------------- - bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert. ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_) ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure. int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height. int TexGlyphPadding; // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0. + bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert. // [Internal] // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you. + bool TexPixelsUseColors; // Tell whether our texture data is known to use colors (rather than just alpha channel), in order to help backend select a format. unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4 int TexWidth; // Texture width calculated during Build(). @@ -2757,7 +2738,7 @@ enum ImGuiViewportFlags_ struct ImGuiViewport { ImGuiViewportFlags Flags; // See ImGuiViewportFlags_ - ImVec2 Pos; // Main Area: Position of the viewport (Dear Imgui coordinates are the same as OS desktop/native coordinates) + ImVec2 Pos; // Main Area: Position of the viewport (Dear ImGui coordinates are the same as OS desktop/native coordinates) ImVec2 Size; // Main Area: Size of the viewport. ImVec2 WorkPos; // Work Area: Position of the viewport minus task bars, menus bars, status bars (>= Pos) ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size) @@ -2769,6 +2750,66 @@ struct ImGuiViewport ImVec2 GetWorkCenter() const { return ImVec2(WorkPos.x + WorkSize.x * 0.5f, WorkPos.y + WorkSize.y * 0.5f); } }; +//----------------------------------------------------------------------------- +// [SECTION] Obsolete functions and types +// (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) +// Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. +//----------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +namespace ImGui +{ + // OBSOLETED in 1.81 (from February 2021) + IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items + static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } + static inline void ListBoxFooter() { EndListBox(); } + // OBSOLETED in 1.79 (from August 2020) + static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! + // OBSOLETED in 1.78 (from June 2020) + // Old drag/sliders functions that took a 'float power = 1.0' argument instead of flags. + // For shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. + IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power); + IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power); + static inline bool DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } + static inline bool DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } + IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power); + IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format, float power); + static inline bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } + static inline bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } + static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } + static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } + // OBSOLETED in 1.77 (from June 2020) + static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } + // OBSOLETED in 1.72 (from April 2019) + static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } + // OBSOLETED in 1.71 (from June 2019) + static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } + // OBSOLETED in 1.70 (from May 2019) + static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } + // OBSOLETED in 1.69 (from Mar 2019) + static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } +} + +// OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect() +typedef ImDrawFlags ImDrawCornerFlags; +enum ImDrawCornerFlags_ +{ + ImDrawCornerFlags_None = ImDrawFlags_RoundCornersNone, // Was == 0 prior to 1.82, this is now == ImDrawFlags_RoundCornersNone which is != 0 and not implicit + ImDrawCornerFlags_TopLeft = ImDrawFlags_RoundCornersTopLeft, // Was == 0x01 (1 << 0) prior to 1.82. Order matches ImDrawFlags_NoRoundCorner* flag (we exploit this internally). + ImDrawCornerFlags_TopRight = ImDrawFlags_RoundCornersTopRight, // Was == 0x02 (1 << 1) prior to 1.82. + ImDrawCornerFlags_BotLeft = ImDrawFlags_RoundCornersBottomLeft, // Was == 0x04 (1 << 2) prior to 1.82. + ImDrawCornerFlags_BotRight = ImDrawFlags_RoundCornersBottomRight, // Was == 0x08 (1 << 3) prior to 1.82. + ImDrawCornerFlags_All = ImDrawFlags_RoundCornersAll, // Was == 0x0F prior to 1.82 + ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, + ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, + ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, + ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight +}; + +#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //----------------------------------------------------------------------------- #if defined(__clang__) diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 90e91aa0..3af6f6a8 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (demo code) // Help: @@ -135,6 +135,16 @@ Index of this file: #define vsnprintf _vsnprintf #endif +// Format specifiers, printing 64-bit hasn't been decently standardized... +// In a real application you should be using PRId64 and PRIu64 from (non-windows) and on Windows define them yourself. +#ifdef _MSC_VER +#define IM_PRId64 "I64d" +#define IM_PRIu64 "I64u" +#else +#define IM_PRId64 "lld" +#define IM_PRIu64 "llu" +#endif + // Helpers macros // We normally try to not use many helpers in imgui_demo.cpp in order to make code easier to copy and paste, // but making an exception here as those are largely simplifying code... @@ -913,7 +923,7 @@ static void ShowDemoWindowWidgets() // so you can safely copy & paste garbled characters into another application. ImGui::TextWrapped( "CJK text will only appears if the font was loaded with the appropriate CJK character ranges. " - "Call io.Font->AddFontFromFileTTF() manually to load extra character ranges. " + "Call io.Fonts->AddFontFromFileTTF() manually to load extra character ranges. " "Read docs/FONTS.md for details."); ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); @@ -1918,12 +1928,12 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); - ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%I64d"); - ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%I64d"); - ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%I64d"); - ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%I64u ms"); - ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%I64u ms"); - ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%I64u ms"); + ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%" IM_PRId64); + ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%" IM_PRId64); + ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%" IM_PRId64); + ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%" IM_PRIu64 " ms"); + ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%" IM_PRIu64 " ms"); + ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%" IM_PRIu64 " ms"); ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one); ImGui::SliderScalar("slider float low log", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", ImGuiSliderFlags_Logarithmic); ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e"); @@ -1932,12 +1942,12 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams"); ImGui::Text("Sliders (reverse)"); - ImGui::SliderScalar("slider s8 reverse", ImGuiDataType_S8, &s8_v, &s8_max, &s8_min, "%d"); - ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u"); + ImGui::SliderScalar("slider s8 reverse", ImGuiDataType_S8, &s8_v, &s8_max, &s8_min, "%d"); + ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u"); ImGui::SliderScalar("slider s32 reverse", ImGuiDataType_S32, &s32_v, &s32_fifty, &s32_zero, "%d"); ImGui::SliderScalar("slider u32 reverse", ImGuiDataType_U32, &u32_v, &u32_fifty, &u32_zero, "%u"); - ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%I64d"); - ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%I64u ms"); + ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%" IM_PRId64); + ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%" IM_PRIu64 " ms"); static bool inputs_step = true; ImGui::Text("Inputs"); @@ -4856,6 +4866,9 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } + // In this example we'll expose most table flags and settings. + // For specific flags and settings refer to the corresponding section for more detailed explanation. + // This section is mostly useful to experiment with combining certain flags or settings with each others. //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // [DEBUG] if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); @@ -4994,7 +5007,7 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } - // Recreate/reset item list if we changed the number of items + // Update item list if we changed the number of items static ImVector items; static ImVector selection; static bool items_need_sort = false; @@ -5016,6 +5029,7 @@ static void ShowDemoWindowTables() ImVec2 table_scroll_cur, table_scroll_max; // For debug display const ImDrawList* table_draw_list = NULL; // " + // Submit table const float inner_width_to_use = (flags & ImGuiTableFlags_ScrollX) ? inner_width_with_scroll : 0.0f; if (ImGui::BeginTable("table_advanced", 6, flags, outer_size_enabled ? outer_size_value : ImVec2(0, 0), inner_width_to_use)) { @@ -5074,9 +5088,9 @@ static void ShowDemoWindowTables() const bool item_is_selected = selection.contains(item->ID); ImGui::PushID(item->ID); ImGui::TableNextRow(ImGuiTableRowFlags_None, row_min_height); - ImGui::TableNextColumn(); // For the demo purpose we can select among different type of items submitted in the first column + ImGui::TableSetColumnIndex(0); char label[32]; sprintf(label, "%04d", item->ID); if (contents_type == CT_Text) @@ -5107,14 +5121,14 @@ static void ShowDemoWindowTables() } } - if (ImGui::TableNextColumn()) + if (ImGui::TableSetColumnIndex(1)) ImGui::TextUnformatted(item->Name); // Here we demonstrate marking our data set as needing to be sorted again if we modified a quantity, // and we are currently sorting on the column showing the Quantity. // To avoid triggering a sort while holding the button, we only trigger it when the button has been released. // You will probably need a more advanced system in your code if you want to automatically sort when a specific entry changes. - if (ImGui::TableNextColumn()) + if (ImGui::TableSetColumnIndex(2)) { if (ImGui::SmallButton("Chop")) { item->Quantity += 1; } if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; } @@ -5123,16 +5137,16 @@ static void ShowDemoWindowTables() if (sorts_specs_using_quantity && ImGui::IsItemDeactivated()) { items_need_sort = true; } } - if (ImGui::TableNextColumn()) + if (ImGui::TableSetColumnIndex(3)) ImGui::Text("%d", item->Quantity); - ImGui::TableNextColumn(); + ImGui::TableSetColumnIndex(4); if (show_wrapped_text) ImGui::TextWrapped("Lorem ipsum dolor sit amet"); else ImGui::Text("Lorem ipsum dolor sit amet"); - if (ImGui::TableNextColumn()) + if (ImGui::TableSetColumnIndex(5)) ImGui::Text("1234"); ImGui::PopID(); @@ -5399,29 +5413,34 @@ static void ShowDemoWindowMisc() ImGui::Text("WantSetMousePos: %d", io.WantSetMousePos); ImGui::Text("NavActive: %d, NavVisible: %d", io.NavActive, io.NavVisible); - // Display Keyboard/Mouse state - if (ImGui::TreeNode("Keyboard, Mouse & Navigation State")) + // Display Mouse state + if (ImGui::TreeNode("Mouse State")) { if (ImGui::IsMousePosValid()) ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); else ImGui::Text("Mouse pos: "); ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); - ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (io.MouseDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } - ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } + ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)){ ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); + ImGui::Text("Pen Pressure: %.1f", io.PenPressure); // Note: currently unused + ImGui::TreePop(); + } - ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (io.KeysDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("%d (0x%X) (%.02f secs)", i, i, io.KeysDownDuration[i]); } - ImGui::Text("Keys pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyPressed(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } - ImGui::Text("Keys release:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyReleased(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } + // Display Keyboard/Mouse state + if (ImGui::TreeNode("Keyboard & Navigation State")) + { + ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyDown(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X) (%.02f secs)", i, i, io.KeysDownDuration[i]); } + ImGui::Text("Keys pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyPressed(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } + ImGui::Text("Keys release:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyReleased(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); - ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. + ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. - ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputs[i]); } + ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f (%.02f secs)", i, io.NavInputs[i], io.NavInputsDownDuration[i]); } ImGui::Text("NavInputs pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] == 0.0f) { ImGui::SameLine(); ImGui::Text("[%d]", i); } - ImGui::Text("NavInputs duration:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] >= 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f", i, io.NavInputsDownDuration[i]); } ImGui::Button("Hovering me sets the\nkeyboard capture flag"); if (ImGui::IsItemHovered()) @@ -5430,7 +5449,6 @@ static void ShowDemoWindowMisc() ImGui::Button("Holding me clears the\nthe keyboard capture flag"); if (ImGui::IsItemActive()) ImGui::CaptureKeyboardFromApp(false); - ImGui::TreePop(); } @@ -6030,22 +6048,42 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f; // When editing the "Circle Segment Max Error" value, draw a preview of its effect on auto-tessellated circles. - ImGui::DragFloat("Circle Segment Max Error", &style.CircleSegmentMaxError, 0.01f, 0.10f, 10.0f, "%.2f"); + ImGui::DragFloat("Circle Tessellation Max Error", &style.CircleTessellationMaxError , 0.005f, 0.10f, 5.0f, "%.2f", ImGuiSliderFlags_AlwaysClamp); if (ImGui::IsItemActive()) { ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos()); ImGui::BeginTooltip(); - ImVec2 p = ImGui::GetCursorScreenPos(); + ImGui::TextUnformatted("(R = radius, N = number of segments)"); + ImGui::Spacing(); ImDrawList* draw_list = ImGui::GetWindowDrawList(); - float RAD_MIN = 10.0f, RAD_MAX = 80.0f; - float off_x = 10.0f; - for (int n = 0; n < 7; n++) + const float min_widget_width = ImGui::CalcTextSize("N: MMM\nR: MMM").x; + for (int n = 0; n < 8; n++) { - const float rad = RAD_MIN + (RAD_MAX - RAD_MIN) * (float)n / (7.0f - 1.0f); - draw_list->AddCircle(ImVec2(p.x + off_x + rad, p.y + RAD_MAX), rad, ImGui::GetColorU32(ImGuiCol_Text), 0); - off_x += 10.0f + rad * 2.0f; + const float RAD_MIN = 5.0f; + const float RAD_MAX = 70.0f; + const float rad = RAD_MIN + (RAD_MAX - RAD_MIN) * (float)n / (8.0f - 1.0f); + + ImGui::BeginGroup(); + + ImGui::Text("R: %.f\nN: %d", rad, draw_list->_CalcCircleAutoSegmentCount(rad)); + + const float canvas_width = IM_MAX(min_widget_width, rad * 2.0f); + const float offset_x = floorf(canvas_width * 0.5f); + const float offset_y = floorf(RAD_MAX); + + const ImVec2 p1 = ImGui::GetCursorScreenPos(); + draw_list->AddCircle(ImVec2(p1.x + offset_x, p1.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text)); + ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2)); + + /* + const ImVec2 p2 = ImGui::GetCursorScreenPos(); + draw_list->AddCircleFilled(ImVec2(p2.x + offset_x, p2.y + offset_y), rad, ImGui::GetColorU32(ImGuiCol_Text)); + ImGui::Dummy(ImVec2(canvas_width, RAD_MAX * 2)); + */ + + ImGui::EndGroup(); + ImGui::SameLine(); } - ImGui::Dummy(ImVec2(off_x, RAD_MAX * 2.0f)); ImGui::EndTooltip(); } ImGui::SameLine(); @@ -7167,9 +7205,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) const ImVec2 p = ImGui::GetCursorScreenPos(); const ImU32 col = ImColor(colf); const float spacing = 10.0f; - const ImDrawCornerFlags corners_none = 0; - const ImDrawCornerFlags corners_all = ImDrawCornerFlags_All; - const ImDrawCornerFlags corners_tl_br = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotRight; + const ImDrawFlags corners_tl_br = ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBottomRight; const float rounding = sz / 5.0f; const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; const int curve_segments = curve_segments_override ? curve_segments_override_v : 0; @@ -7181,8 +7217,8 @@ static void ShowExampleAppCustomRendering(bool* p_open) float th = (n == 0) ? 1.0f : thickness; draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, corners_none, th); x += sz + spacing; // Square - draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_all, th); x += sz + spacing; // Square with all rounded corners + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, ImDrawFlags_None, th); x += sz + spacing; // Square + draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, ImDrawFlags_None, th); x += sz + spacing; // Square with all rounded corners draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th);x += sz + spacing; // Triangle //draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th);x+= sz*0.4f + spacing; // Thin triangle diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 5c11a5b3..c41a1ba0 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (drawing and font code) /* @@ -374,19 +374,22 @@ ImDrawListSharedData::ImDrawListSharedData() const float a = ((float)i * 2 * IM_PI) / (float)IM_ARRAYSIZE(ArcFastVtx); ArcFastVtx[i] = ImVec2(ImCos(a), ImSin(a)); } + ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); } -void ImDrawListSharedData::SetCircleSegmentMaxError(float max_error) +void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) { if (CircleSegmentMaxError == max_error) return; + + IM_ASSERT(max_error > 0.0f); CircleSegmentMaxError = max_error; for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) { const float radius = (float)i; - const int segment_count = (i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : 0; - CircleSegmentCounts[i] = (ImU8)ImMin(segment_count, 255); + CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : 0); } + ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); } // Initialize before use in a new frame. We always have a command ready in the buffer. @@ -543,6 +546,16 @@ void ImDrawList::_OnChangedVtxOffset() curr_cmd->VtxOffset = _CmdHeader.VtxOffset; } +int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const +{ + // Automatic segment count + const int radius_idx = (int)(radius + 0.999999f); // ceil to never reduce accuracy + if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) + return _Data->CircleSegmentCounts[radius_idx]; // Use cached value + else + return IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); +} + // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) { @@ -680,11 +693,12 @@ void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, c // TODO: Thickness anti-aliased lines cap are missing their AA fringe. // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. -void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, bool closed, float thickness) +void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness) { if (points_count < 2) return; + const bool closed = (flags & ImDrawFlags_Closed) != 0; const ImVec2 opaque_uv = _Data->TexUvWhitePixel; const int count = closed ? points_count : points_count - 1; // The number of line segments we need to draw const bool thick_line = (thickness > _FringeScale); @@ -1016,33 +1030,88 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun } } -void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) +void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step) { - if (radius == 0.0f) + if (radius <= 0.0f) { _Path.push_back(center); return; } - IM_ASSERT(a_min_of_12 <= a_max_of_12); + IM_ASSERT(a_min_sample <= a_max_sample); - // For legacy reason the PathArcToFast() always takes angles where 2*PI is represented by 12, - // but it is possible to set IM_DRAWLIST_ARCFAST_TESSELATION_MULTIPLIER to a higher value. This should compile to a no-op otherwise. -#if IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER != 1 - a_min_of_12 *= IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER; - a_max_of_12 *= IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER; -#endif + // Calculate arc auto segment step size + if (a_step <= 0) + a_step = IM_DRAWLIST_ARCFAST_SAMPLE_MAX / _CalcCircleAutoSegmentCount(radius); + + // Make sure we never do steps larger than one quarter of the circle + a_step = ImClamp(a_step, 1, IM_DRAWLIST_ARCFAST_TABLE_SIZE / 4); + + // Normalize a_min_sample to always start lie in [0..IM_DRAWLIST_ARCFAST_SAMPLE_MAX] range. + if (a_min_sample < 0) + { + int normalized_sample = a_min_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + if (normalized_sample < 0) + normalized_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + a_max_sample += (normalized_sample - a_min_sample); + a_min_sample = normalized_sample; + } + + const int sample_range = a_max_sample - a_min_sample; + const int a_next_step = a_step; + + int samples = sample_range + 1; + bool extra_max_sample = false; + if (a_step > 1) + { + samples = sample_range / a_step + 1; + const int overstep = sample_range % a_step; + + if (overstep > 0) + { + extra_max_sample = true; + samples++; + + // When we have overstep to avoid awkwardly looking one long line and one tiny one at the end, + // distribute first step range evenly between them by reducing first step size. + if (sample_range > 0) + a_step -= (a_step - overstep) / 2; + } + } + + _Path.resize(_Path.Size + samples); + ImVec2* out_ptr = _Path.Data + (_Path.Size - samples); - _Path.reserve(_Path.Size + (a_max_of_12 - a_min_of_12 + 1)); - for (int a = a_min_of_12; a <= a_max_of_12; a++) + int sample_index = a_min_sample; + for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step) { - const ImVec2& c = _Data->ArcFastVtx[a % IM_ARRAYSIZE(_Data->ArcFastVtx)]; - _Path.push_back(ImVec2(center.x + c.x * radius, center.y + c.y * radius)); + // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more + if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) + sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + + const ImVec2 s = _Data->ArcFastVtx[sample_index]; + out_ptr->x = center.x + s.x * radius; + out_ptr->y = center.y + s.y * radius; + out_ptr++; } + + if (extra_max_sample) + { + int normalized_max_sample = a_max_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + if (normalized_max_sample < 0) + normalized_max_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + + const ImVec2 s = _Data->ArcFastVtx[normalized_max_sample]; + out_ptr->x = center.x + s.x * radius; + out_ptr->y = center.y + s.y * radius; + out_ptr++; + } + + IM_ASSERT_PARANOID(_Path.Data + _Path.Size == out_ptr); } -void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) +void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) { - if (radius == 0.0f) + if (radius <= 0.0f) { _Path.push_back(center); return; @@ -1059,6 +1128,64 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa } } +// 0: East, 3: South, 6: West, 9: North, 12: East +void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) +{ + if (radius <= 0.0f) + { + _Path.push_back(center); + return; + } + IM_ASSERT(a_min_of_12 <= a_max_of_12); + _PathArcToFastEx(center, radius, a_min_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_max_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, 0); +} + +void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) +{ + if (radius <= 0.0f) + { + _Path.push_back(center); + return; + } + IM_ASSERT(a_min <= a_max); + + if (num_segments > 0) + { + _PathArcToN(center, radius, a_min, a_max, num_segments); + return; + } + + // Automatic segment count + if (radius <= _Data->ArcFastRadiusCutoff) + { + // We are going to use precomputed values for mid samples. + // Determine first and last sample in lookup table that belong to the arc. + const int a_min_sample = (int)ImCeil(IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f)); + const int a_max_sample = (int)( IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f)); + const int a_mid_samples = ImMax(a_max_sample - a_min_sample, 0); + + const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + const bool a_emit_start = (a_min_segment_angle - a_min) > 0.0f; + const bool a_emit_end = (a_max - a_max_segment_angle) > 0.0f; + + _Path.reserve(_Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0))); + if (a_emit_start) + _Path.push_back(ImVec2(center.x + ImCos(a_min) * radius, center.y + ImSin(a_min) * radius)); + if (a_max_sample >= a_min_sample) + _PathArcToFastEx(center, radius, a_min_sample, a_max_sample, 0); + if (a_emit_end) + _Path.push_back(ImVec2(center.x + ImCos(a_max) * radius, center.y + ImSin(a_max) * radius)); + } + else + { + const float arc_length = a_max - a_min; + const int circle_segment_count = _CalcCircleAutoSegmentCount(radius); + const int arc_segment_count = ImMax((int)ImCeil(circle_segment_count * arc_length / (IM_PI * 2.0f)), (int)(2.0f * IM_PI / arc_length)); + _PathArcToN(center, radius, a_min, a_max, arc_segment_count); + } +} + ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) { float u = 1.0f - t; @@ -1152,12 +1279,47 @@ void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, } } -void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawCornerFlags rounding_corners) +IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); +static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) +{ +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) + // ~0 --> ImDrawFlags_RoundCornersAll or 0 + if (flags == ~0) + return ImDrawFlags_RoundCornersAll; + + // Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations) + // 0x01 --> ImDrawFlags_RoundCornersTopLeft (VALUE 0x01 OVERLAPS ImDrawFlags_Closed but ImDrawFlags_Closed is never valid in this path!) + // 0x02 --> ImDrawFlags_RoundCornersTopRight + // 0x03 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight + // 0x04 --> ImDrawFlags_RoundCornersBotLeft + // 0x05 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBotLeft + // ... + // 0x0F --> ImDrawFlags_RoundCornersAll or 0 + // (See all values in ImDrawCornerFlags_) + if (flags >= 0x01 && flags <= 0x0F) + return (flags << 4); + + // We cannot support hard coded 0x00 with 'float rounding > 0.0f' --> replace with ImDrawFlags_RoundCornersNone or use 'float rounding = 0.0f' +#endif + + // If this triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values. + // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc... + IM_ASSERT((flags & 0x0F) == 0 && "Misuse of legacy hardcoded ImDrawCornerFlags values!"); + + if ((flags & ImDrawFlags_RoundCornersMask_) == 0) + flags |= ImDrawFlags_RoundCornersAll; + + return flags; +} + +void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawFlags flags) { - rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((rounding_corners & ImDrawCornerFlags_Top) == ImDrawCornerFlags_Top) || ((rounding_corners & ImDrawCornerFlags_Bot) == ImDrawCornerFlags_Bot) ? 0.5f : 1.0f ) - 1.0f); - rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((rounding_corners & ImDrawCornerFlags_Left) == ImDrawCornerFlags_Left) || ((rounding_corners & ImDrawCornerFlags_Right) == ImDrawCornerFlags_Right) ? 0.5f : 1.0f ) - 1.0f); + flags = FixRectCornerFlags(flags); + rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f ) - 1.0f); + rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f ) - 1.0f); - if (rounding <= 0.0f || rounding_corners == 0) + if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { PathLineTo(a); PathLineTo(ImVec2(b.x, a.y)); @@ -1166,10 +1328,10 @@ void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDr } else { - const float rounding_tl = (rounding_corners & ImDrawCornerFlags_TopLeft) ? rounding : 0.0f; - const float rounding_tr = (rounding_corners & ImDrawCornerFlags_TopRight) ? rounding : 0.0f; - const float rounding_br = (rounding_corners & ImDrawCornerFlags_BotRight) ? rounding : 0.0f; - const float rounding_bl = (rounding_corners & ImDrawCornerFlags_BotLeft) ? rounding : 0.0f; + const float rounding_tl = (flags & ImDrawFlags_RoundCornersTopLeft) ? rounding : 0.0f; + const float rounding_tr = (flags & ImDrawFlags_RoundCornersTopRight) ? rounding : 0.0f; + const float rounding_br = (flags & ImDrawFlags_RoundCornersBottomRight) ? rounding : 0.0f; + const float rounding_bl = (flags & ImDrawFlags_RoundCornersBottomLeft) ? rounding : 0.0f; PathArcToFast(ImVec2(a.x + rounding_tl, a.y + rounding_tl), rounding_tl, 6, 9); PathArcToFast(ImVec2(b.x - rounding_tr, a.y + rounding_tr), rounding_tr, 9, 12); PathArcToFast(ImVec2(b.x - rounding_br, b.y - rounding_br), rounding_br, 0, 3); @@ -1183,35 +1345,35 @@ void ImDrawList::AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float th return; PathLineTo(p1 + ImVec2(0.5f, 0.5f)); PathLineTo(p2 + ImVec2(0.5f, 0.5f)); - PathStroke(col, false, thickness); + PathStroke(col, 0, thickness); } // p_min = upper-left, p_max = lower-right // Note we don't render 1 pixels sized rectangles properly. -void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners, float thickness) +void ImDrawList::AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; if (Flags & ImDrawListFlags_AntiAliasedLines) - PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.50f, 0.50f), rounding, rounding_corners); + PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.50f, 0.50f), rounding, flags); else - PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.49f, 0.49f), rounding, rounding_corners); // Better looking lower-right corner and rounded non-AA shapes. - PathStroke(col, true, thickness); + PathRect(p_min + ImVec2(0.50f, 0.50f), p_max - ImVec2(0.49f, 0.49f), rounding, flags); // Better looking lower-right corner and rounded non-AA shapes. + PathStroke(col, ImDrawFlags_Closed, thickness); } -void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners) +void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding, ImDrawFlags flags) { if ((col & IM_COL32_A_MASK) == 0) return; - if (rounding > 0.0f) + if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { - PathRect(p_min, p_max, rounding, rounding_corners); - PathFillConvex(col); + PrimReserve(6, 4); + PrimRect(p_min, p_max, col); } else { - PrimReserve(6, 4); - PrimRect(p_min, p_max, col); + PathRect(p_min, p_max, rounding, flags); + PathFillConvex(col); } } @@ -1240,7 +1402,7 @@ void ImDrawList::AddQuad(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, c PathLineTo(p2); PathLineTo(p3); PathLineTo(p4); - PathStroke(col, true, thickness); + PathStroke(col, ImDrawFlags_Closed, thickness); } void ImDrawList::AddQuadFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col) @@ -1263,7 +1425,7 @@ void ImDrawList::AddTriangle(const ImVec2& p1, const ImVec2& p2, const ImVec2& p PathLineTo(p1); PathLineTo(p2); PathLineTo(p3); - PathStroke(col, true, thickness); + PathStroke(col, ImDrawFlags_Closed, thickness); } void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col) @@ -1286,11 +1448,7 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu if (num_segments <= 0) { // Automatic segment count - const int radius_idx = (int)radius; - if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) - num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value - else - num_segments = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); + num_segments = _CalcCircleAutoSegmentCount(radius); } else { @@ -1304,7 +1462,7 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu PathArcToFast(center, radius - 0.5f, 0, 12 - 1); else PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); - PathStroke(col, true, thickness); + PathStroke(col, ImDrawFlags_Closed, thickness); } void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) @@ -1316,11 +1474,7 @@ void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, if (num_segments <= 0) { // Automatic segment count - const int radius_idx = (int)radius; - if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) - num_segments = _Data->CircleSegmentCounts[radius_idx]; // Use cached value - else - num_segments = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); + num_segments = _CalcCircleAutoSegmentCount(radius); } else { @@ -1346,7 +1500,7 @@ void ImDrawList::AddNgon(const ImVec2& center, float radius, ImU32 col, int num_ // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); - PathStroke(col, true, thickness); + PathStroke(col, ImDrawFlags_Closed, thickness); } // Guaranteed to honor 'num_segments' @@ -1369,7 +1523,7 @@ void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2 PathLineTo(p1); PathBezierCubicCurveTo(p2, p3, p4, num_segments); - PathStroke(col, false, thickness); + PathStroke(col, 0, thickness); } // Quadratic Bezier takes 3 controls points @@ -1380,7 +1534,7 @@ void ImDrawList::AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const Im PathLineTo(p1); PathBezierQuadraticCurveTo(p2, p3, num_segments); - PathStroke(col, false, thickness); + PathStroke(col, 0, thickness); } void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end, float wrap_width, const ImVec4* cpu_fine_clip_rect) @@ -1449,23 +1603,24 @@ void ImDrawList::AddImageQuad(ImTextureID user_texture_id, const ImVec2& p1, con PopTextureID(); } -void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawCornerFlags rounding_corners) +void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags) { if ((col & IM_COL32_A_MASK) == 0) return; - if (rounding <= 0.0f || (rounding_corners & ImDrawCornerFlags_All) == 0) + flags = FixRectCornerFlags(flags); + if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col); return; } - const bool push_texture_id = _TextureIdStack.empty() || user_texture_id != _TextureIdStack.back(); + const bool push_texture_id = user_texture_id != _CmdHeader.TextureId; if (push_texture_id) PushTextureID(user_texture_id); int vert_start_idx = VtxBuffer.Size; - PathRect(p_min, p_max, rounding, rounding_corners); + PathRect(p_min, p_max, rounding, flags); PathFillConvex(col); int vert_end_idx = VtxBuffer.Size; ImGui::ShadeVertsLinearUV(this, vert_start_idx, vert_end_idx, p_min, p_max, uv_min, uv_max, true); @@ -1823,6 +1978,7 @@ void ImFontAtlas::ClearTexData() IM_FREE(TexPixelsRGBA32); TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; + TexPixelsUseColors = false; } void ImFontAtlas::ClearFonts() @@ -2532,7 +2688,7 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) unsigned char* write_ptr = &atlas->TexPixelsAlpha8[r->X + ((r->Y + y) * atlas->TexWidth)]; for (unsigned int i = 0; i < pad_left; i++) *(write_ptr + i) = 0x00; - + for (unsigned int i = 0; i < line_width; i++) *(write_ptr + pad_left + i) = 0xFF; @@ -2544,7 +2700,7 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) unsigned int* write_ptr = &atlas->TexPixelsRGBA32[r->X + ((r->Y + y) * atlas->TexWidth)]; for (unsigned int i = 0; i < pad_left; i++) *(write_ptr + i) = IM_COL32_BLACK_TRANS; - + for (unsigned int i = 0; i < line_width; i++) *(write_ptr + pad_left + i) = IM_COL32_WHITE; @@ -3582,7 +3738,7 @@ void ImGui::RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float draw_list->PathLineTo(ImVec2(bx - third, by - third)); draw_list->PathLineTo(ImVec2(bx, by)); draw_list->PathLineTo(ImVec2(bx + third * 2.0f, by - third * 2.0f)); - draw_list->PathStroke(col, false, thickness); + draw_list->PathStroke(col, 0, thickness); } void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) @@ -3694,27 +3850,29 @@ void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect const bool fill_R = (inner.Max.x < outer.Max.x); const bool fill_U = (inner.Min.y > outer.Min.y); const bool fill_D = (inner.Max.y < outer.Max.y); - if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopLeft) | (fill_D ? 0 : ImDrawCornerFlags_BotLeft)); - if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawCornerFlags_TopRight) | (fill_D ? 0 : ImDrawCornerFlags_BotRight)); - if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_TopLeft) | (fill_R ? 0 : ImDrawCornerFlags_TopRight)); - if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, (fill_L ? 0 : ImDrawCornerFlags_BotLeft) | (fill_R ? 0 : ImDrawCornerFlags_BotRight)); - if (fill_L && fill_U) draw_list->AddRectFilled(ImVec2(outer.Min.x, outer.Min.y), ImVec2(inner.Min.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopLeft); - if (fill_R && fill_U) draw_list->AddRectFilled(ImVec2(inner.Max.x, outer.Min.y), ImVec2(outer.Max.x, inner.Min.y), col, rounding, ImDrawCornerFlags_TopRight); - if (fill_L && fill_D) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Max.y), ImVec2(inner.Min.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotLeft); - if (fill_R && fill_D) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Max.y), ImVec2(outer.Max.x, outer.Max.y), col, rounding, ImDrawCornerFlags_BotRight); + if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomLeft)); + if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawFlags_RoundCornersTopRight) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomRight)); + if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, (fill_L ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersTopRight)); + if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, (fill_L ? 0 : ImDrawFlags_RoundCornersBottomLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersBottomRight)); + if (fill_L && fill_U) draw_list->AddRectFilled(ImVec2(outer.Min.x, outer.Min.y), ImVec2(inner.Min.x, inner.Min.y), col, rounding, ImDrawFlags_RoundCornersTopLeft); + if (fill_R && fill_U) draw_list->AddRectFilled(ImVec2(inner.Max.x, outer.Min.y), ImVec2(outer.Max.x, inner.Min.y), col, rounding, ImDrawFlags_RoundCornersTopRight); + if (fill_L && fill_D) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Max.y), ImVec2(inner.Min.x, outer.Max.y), col, rounding, ImDrawFlags_RoundCornersBottomLeft); + if (fill_R && fill_D) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Max.y), ImVec2(outer.Max.x, outer.Max.y), col, rounding, ImDrawFlags_RoundCornersBottomRight); } // Helper for ColorPicker4() // NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that. // Spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding altogether. // FIXME: uses ImGui::GetColorU32 -void ImGui::RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags) +void ImGui::RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, ImDrawFlags flags) { + if ((flags & ImDrawFlags_RoundCornersMask_) == 0) + flags = ImDrawFlags_RoundCornersDefault_; if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF) { - ImU32 col_bg1 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(204, 204, 204, 255), col)); - ImU32 col_bg2 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(128, 128, 128, 255), col)); - draw_list->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags); + ImU32 col_bg1 = GetColorU32(ImAlphaBlendColors(IM_COL32(204, 204, 204, 255), col)); + ImU32 col_bg2 = GetColorU32(ImAlphaBlendColors(IM_COL32(128, 128, 128, 255), col)); + draw_list->AddRectFilled(p_min, p_max, col_bg1, rounding, flags); int yi = 0; for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++) @@ -3727,17 +3885,19 @@ void ImGui::RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x); if (x2 <= x1) continue; - int rounding_corners_flags_cell = 0; - if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; } - if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; } - rounding_corners_flags_cell &= rounding_corners_flags; - draw_list->AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell); + ImDrawFlags cell_flags = ImDrawFlags_RoundCornersNone; + if (y1 <= p_min.y) { if (x1 <= p_min.x) cell_flags |= ImDrawFlags_RoundCornersTopLeft; if (x2 >= p_max.x) cell_flags |= ImDrawFlags_RoundCornersTopRight; } + if (y2 >= p_max.y) { if (x1 <= p_min.x) cell_flags |= ImDrawFlags_RoundCornersBottomLeft; if (x2 >= p_max.x) cell_flags |= ImDrawFlags_RoundCornersBottomRight; } + + // Combine flags + cell_flags = (flags == ImDrawFlags_RoundCornersNone || cell_flags == ImDrawFlags_RoundCornersNone) ? ImDrawFlags_RoundCornersNone : (cell_flags & flags); + draw_list->AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), col_bg2, rounding, cell_flags); } } } else { - draw_list->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags); + draw_list->AddRectFilled(p_min, p_max, col, rounding, flags); } } diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index fbc167d6..cd0f51db 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -149,7 +149,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) io.BackendPlatformName = "imgui_impl_glfw"; - // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array. + // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT; io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT; diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index f3b2a937..b582851e 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -13,6 +13,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2021-02-18: OpenGL: Change blending equation to preserve alpha in output buffer. // 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. // 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. // 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) @@ -247,7 +248,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glDisable(GL_STENCIL_TEST); diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index 39caf6b6..ea50a49c 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -214,6 +214,7 @@ namespace ImStb #define IM_NEWLINE "\n" #endif #define IM_TABSIZE (4) +#define IM_MEMALIGN(_OFF,_ALIGN) (((_OFF) + (_ALIGN - 1)) & ~(_ALIGN - 1)) // Memory align e.g. IM_ALIGN(0,4)=0, IM_ALIGN(1,4)=4, IM_ALIGN(4,4)=4, IM_ALIGN(5,4)=8 #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 #define IM_FLOOR(_VAL) ((float)(int)(_VAL)) // ImFloor() is not inlined in MSVC debug builds @@ -448,6 +449,7 @@ struct IMGUI_API ImRect ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } float GetWidth() const { return Max.x - Min.x; } float GetHeight() const { return Max.y - Min.y; } + float GetArea() const { return (Max.x - Min.x) * (Max.y - Min.y); } ImVec2 GetTL() const { return Min; } // Top-left ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left @@ -544,20 +546,22 @@ struct ImSpan // Helper: ImSpanAllocator<> // Facilitate storing multiple chunks into a single large block (the "arena") +// - Usage: call Reserve() N times, allocate GetArenaSizeInBytes() worth, pass it to SetArenaBasePtr(), call GetSpan() N times to retrieve the aligned ranges. template struct ImSpanAllocator { char* BasePtr; - int TotalSize; - int CurrSpan; + int CurrOff; + int CurrIdx; int Offsets[CHUNKS]; + int Sizes[CHUNKS]; ImSpanAllocator() { memset(this, 0, sizeof(*this)); } - inline void ReserveBytes(int n, size_t sz) { IM_ASSERT(n == CurrSpan && n < CHUNKS); IM_UNUSED(n); Offsets[CurrSpan++] = TotalSize; TotalSize += (int)sz; } - inline int GetArenaSizeInBytes() { return TotalSize; } + inline void Reserve(int n, size_t sz, int a=4) { IM_ASSERT(n == CurrIdx && n < CHUNKS); CurrOff = IM_MEMALIGN(CurrOff, a); Offsets[n] = CurrOff; Sizes[n] = (int)sz; CurrIdx++; CurrOff += (int)sz; } + inline int GetArenaSizeInBytes() { return CurrOff; } inline void SetArenaBasePtr(void* base_ptr) { BasePtr = (char*)base_ptr; } - inline void* GetSpanPtrBegin(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrSpan == CHUNKS); return (void*)(BasePtr + Offsets[n]); } - inline void* GetSpanPtrEnd(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrSpan == CHUNKS); return (n + 1 < CHUNKS) ? BasePtr + Offsets[n + 1] : (void*)(BasePtr + TotalSize); } + inline void* GetSpanPtrBegin(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrIdx == CHUNKS); return (void*)(BasePtr + Offsets[n]); } + inline void* GetSpanPtrEnd(int n) { IM_ASSERT(n >= 0 && n < CHUNKS && CurrIdx == CHUNKS); return (void*)(BasePtr + Offsets[n] + Sizes[n]); } template inline void GetSpan(int n, ImSpan* span) { span->set((T*)GetSpanPtrBegin(n), (T*)GetSpanPtrEnd(n)); } }; @@ -591,7 +595,7 @@ struct IMGUI_API ImPool // Helper: ImChunkStream<> // Build and iterate a contiguous stream of variable-sized structures. // This is used by Settings to store persistent data while reducing allocation count. -// We store the chunk size first, and align the final size on 4 bytes boundaries (this what the '(X + 3) & ~3' statement is for) +// We store the chunk size first, and align the final size on 4 bytes boundaries. // The tedious/zealous amount of casting is to avoid -Wcast-align warnings. template struct IMGUI_API ImChunkStream @@ -601,7 +605,7 @@ struct IMGUI_API ImChunkStream void clear() { Buf.clear(); } bool empty() const { return Buf.Size == 0; } int size() const { return Buf.Size; } - T* alloc_chunk(size_t sz) { size_t HDR_SZ = 4; sz = ((HDR_SZ + sz) + 3u) & ~3u; int off = Buf.Size; Buf.resize(off + (int)sz); ((int*)(void*)(Buf.Data + off))[0] = (int)sz; return (T*)(void*)(Buf.Data + off + (int)HDR_SZ); } + T* alloc_chunk(size_t sz) { size_t HDR_SZ = 4; sz = IM_MEMALIGN(HDR_SZ + sz, 4u); int off = Buf.Size; Buf.resize(off + (int)sz); ((int*)(void*)(Buf.Data + off))[0] = (int)sz; return (T*)(void*)(Buf.Data + off + (int)HDR_SZ); } T* begin() { size_t HDR_SZ = 4; if (!Buf.Data) return NULL; return (T*)(void*)(Buf.Data + HDR_SZ); } T* next_chunk(T* p) { size_t HDR_SZ = 4; IM_ASSERT(p >= begin() && p < end()); p = (T*)(void*)((char*)(void*)p + chunk_size(p)); if (p == (T*)(void*)((char*)end() + HDR_SZ)) return (T*)0; IM_ASSERT(p < end()); return p; } int chunk_size(const T* p) { return ((const int*)p)[-1]; } @@ -617,15 +621,30 @@ struct IMGUI_API ImChunkStream //----------------------------------------------------------------------------- // ImDrawList: Helper function to calculate a circle's segment count given its radius and a "maximum error" value. -// FIXME: the minimum number of auto-segment may be undesirably high for very small radiuses (e.g. 1.0f) -#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 12 +// Estimation of number of circle segment based on error is derived using method described in https://stackoverflow.com/a/2244088/15194693 +// Number of segments (N) is calculated using equation: +// N = ceil ( pi / acos(1 - error / r) ) where r > 0, error <= r +// Our equation is significantly simpler that one in the post thanks for choosing segment that is +// perpendicular to X axis. Follow steps in the article from this starting condition and you will +// will get this result. +// +// Rendering circles with an odd number of segments, while mathematically correct will produce +// asymmetrical results on the raster grid. Therefore we're rounding N to next even number (7->8, 8->8, 9->10 etc.) +// +#define IM_ROUNDUP_TO_EVEN(_V) ((((_V) + 1) / 2) * 2) +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 4 #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 -#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp((int)((IM_PI * 2.0f) / ImAcos(((_RAD) - (_MAXERROR)) / (_RAD))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(_RAD,_MAXERROR) ImClamp(IM_ROUNDUP_TO_EVEN((int)ImCeil(IM_PI / ImAcos(1 - ImMin((_MAXERROR), (_RAD)) / (_RAD)))), IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX) -// ImDrawList: You may set this to higher values (e.g. 2 or 3) to increase tessellation of fast rounded corners path. -#ifndef IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER -#define IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER 1 +// Raw equation from IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC rewritten for 'r' and 'error'. +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(_N,_MAXERROR) ((_MAXERROR) / (1 - ImCos(IM_PI / ImMax((float)(_N), IM_PI)))) +#define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_ERROR(_N,_RAD) ((1 - ImCos(IM_PI / ImMax((float)(_N), IM_PI))) / (_RAD)) + +// ImDrawList: Lookup table size for adaptive arc drawing, cover full circle. +#ifndef IM_DRAWLIST_ARCFAST_TABLE_SIZE +#define IM_DRAWLIST_ARCFAST_TABLE_SIZE 48 // Number of samples in lookup table. #endif +#define IM_DRAWLIST_ARCFAST_SAMPLE_MAX IM_DRAWLIST_ARCFAST_TABLE_SIZE // Sample index _PathArcToFastEx() for 360 angle. // Data shared between all ImDrawList instances // You may want to create your own instance of this if you want to use ImDrawList completely without ImGui. In that case, watch out for future changes to this structure. @@ -640,12 +659,13 @@ struct IMGUI_API ImDrawListSharedData ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) // [Internal] Lookup tables - ImVec2 ArcFastVtx[12 * IM_DRAWLIST_ARCFAST_TESSELLATION_MULTIPLIER]; // FIXME: Bake rounded corners fill/borders in atlas + ImVec2 ArcFastVtx[IM_DRAWLIST_ARCFAST_TABLE_SIZE]; // Sample points on the quarter of the circle. + float ArcFastRadiusCutoff; // Cutoff radius after which arc drawing will fallback to slower PathArcTo() ImU8 CircleSegmentCounts[64]; // Precomputed segment count for given radius before we calculate it dynamically (to avoid calculation overhead) const ImVec4* TexUvLines; // UV of anti-aliased lines in the atlas ImDrawListSharedData(); - void SetCircleSegmentMaxError(float max_error); + void SetCircleTessellationMaxError(float max_error); }; struct ImDrawDataBuilder @@ -683,12 +703,13 @@ enum ImGuiItemStatusFlags_ { ImGuiItemStatusFlags_None = 0, ImGuiItemStatusFlags_HoveredRect = 1 << 0, - ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, + ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, // LastItemDisplayRect is valid ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. - ImGuiItemStatusFlags_Deactivated = 1 << 6 // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. + ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. + ImGuiItemStatusFlags_HoveredWindow = 1 << 7 // Override the HoveredWindow test to allow cross-window hover testing. #ifdef IMGUI_ENABLE_TEST_ENGINE , // [imgui_tests only] @@ -803,9 +824,9 @@ enum ImGuiInputSource { ImGuiInputSource_None = 0, ImGuiInputSource_Mouse, - ImGuiInputSource_Nav, - ImGuiInputSource_NavKeyboard, // Only used occasionally for storage, not tested/handled by most code - ImGuiInputSource_NavGamepad, // " + ImGuiInputSource_Keyboard, + ImGuiInputSource_Gamepad, + ImGuiInputSource_Nav, // Stored in g.ActiveIdSource only ImGuiInputSource_COUNT }; @@ -910,7 +931,7 @@ struct ImGuiStyleMod }; // Stacked storage data for BeginGroup()/EndGroup() -struct ImGuiGroupData +struct IMGUI_API ImGuiGroupData { ImGuiID WindowID; ImVec2 BackupCursorPos; @@ -921,6 +942,7 @@ struct ImGuiGroupData float BackupCurrLineTextBaseOffset; ImGuiID BackupActiveIdIsAlive; bool BackupActiveIdPreviousFrameIsAlive; + bool BackupHoveredIdIsAlive; bool EmitItem; }; @@ -1296,7 +1318,6 @@ struct ImGuiContext int WindowsActiveCount; // Number of unique windows submitted by frame ImGuiWindow* CurrentWindow; // Window being drawn into ImGuiWindow* HoveredWindow; // Window the mouse is hovering. Will typically catch mouse inputs. - ImGuiWindow* HoveredRootWindow; // == HoveredWindow ? HoveredWindow->RootWindow : NULL, merely a shortcut to avoid null test in some situation. ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actual window that is moved is generally MovingWindow->RootWindow. ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. @@ -1401,13 +1422,13 @@ struct ImGuiContext bool NavWindowingToggleLayer; // Legacy Focus/Tabbing system (older than Nav, active even if Nav is disabled, misnamed. FIXME-NAV: This needs a redesign!) - ImGuiWindow* FocusRequestCurrWindow; // - ImGuiWindow* FocusRequestNextWindow; // - int FocusRequestCurrCounterRegular; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) - int FocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index - int FocusRequestNextCounterRegular; // Stored for next frame - int FocusRequestNextCounterTabStop; // " - bool FocusTabPressed; // + ImGuiWindow* TabFocusRequestCurrWindow; // + ImGuiWindow* TabFocusRequestNextWindow; // + int TabFocusRequestCurrCounterRegular; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) + int TabFocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index + int TabFocusRequestNextCounterRegular; // Stored for next frame + int TabFocusRequestNextCounterTabStop; // " + bool TabFocusPressed; // // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) @@ -1527,7 +1548,6 @@ struct ImGuiContext WindowsActiveCount = 0; CurrentWindow = NULL; HoveredWindow = NULL; - HoveredRootWindow = NULL; HoveredWindowUnderMovingWindow = NULL; MovingWindow = NULL; WheelingWindow = NULL; @@ -1554,7 +1574,7 @@ struct ImGuiContext ActiveIdClickOffset = ImVec2(-1, -1); ActiveIdWindow = NULL; ActiveIdSource = ImGuiInputSource_None; - ActiveIdMouseButton = 0; + ActiveIdMouseButton = -1; ActiveIdPreviousFrame = 0; ActiveIdPreviousFrameIsAlive = false; ActiveIdPreviousFrameHasBeenEditedBefore = false; @@ -1591,10 +1611,10 @@ struct ImGuiContext NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; - FocusRequestCurrWindow = FocusRequestNextWindow = NULL; - FocusRequestCurrCounterRegular = FocusRequestCurrCounterTabStop = INT_MAX; - FocusRequestNextCounterRegular = FocusRequestNextCounterTabStop = INT_MAX; - FocusTabPressed = false; + TabFocusRequestCurrWindow = TabFocusRequestNextWindow = NULL; + TabFocusRequestCurrCounterRegular = TabFocusRequestCurrCounterTabStop = INT_MAX; + TabFocusRequestNextCounterRegular = TabFocusRequestNextCounterTabStop = INT_MAX; + TabFocusPressed = false; DimBgRatio = 0.0f; MouseCursor = ImGuiMouseCursor_Arrow; @@ -1886,7 +1906,7 @@ struct ImGuiTabBar ImGuiTabBarFlags Flags; ImGuiID ID; // Zero for tab-bars used by docking ImGuiID SelectedTabId; // Selected tab/window - ImGuiID NextSelectedTabId; + ImGuiID NextSelectedTabId; // Next selected tab/window. Will also trigger a scrolling animation ImGuiID VisibleTabId; // Can occasionally be != SelectedTabId (e.g. when previewing contents for CTRL+TAB preview) int CurrFrameVisible; int PrevFrameVisible; @@ -2091,9 +2111,10 @@ struct ImGuiTable ImGuiTableColumnIdx HeldHeaderColumn; // Index of column header being held. ImGuiTableColumnIdx ReorderColumn; // Index of column being reordered. (not cleared) ImGuiTableColumnIdx ReorderColumnDir; // -1 or +1 + ImGuiTableColumnIdx LeftMostEnabledColumn; // Index of left-most non-hidden column. + ImGuiTableColumnIdx RightMostEnabledColumn; // Index of right-most non-hidden column. ImGuiTableColumnIdx LeftMostStretchedColumn; // Index of left-most stretched column. ImGuiTableColumnIdx RightMostStretchedColumn; // Index of right-most stretched column. - ImGuiTableColumnIdx RightMostEnabledColumn; // Index of right-most non-hidden column. ImGuiTableColumnIdx ContextPopupColumn; // Column right-clicked on, of -1 if opening context menu from a neutral/empty spot ImGuiTableColumnIdx FreezeRowsRequest; // Requested frozen rows count ImGuiTableColumnIdx FreezeRowsCount; // Actual frozen row count (== FreezeRowsRequest, or == 0 when no scrolling offset) @@ -2298,8 +2319,7 @@ namespace ImGui IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. - IMGUI_API void SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id); - IMGUI_API void SetNavIDWithRectRel(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); + IMGUI_API void SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); // Focus Scope (WIP) // This is generally used to identify a selection set (multiple of which may be in the same window), as selection @@ -2349,6 +2369,7 @@ namespace ImGui IMGUI_API void TablePopBackgroundChannel(); // Tables: Internals + inline ImGuiTable* GetCurrentTable() { ImGuiContext& g = *GImGui; return g.CurrentTable; } IMGUI_API ImGuiTable* TableFindByID(ImGuiID id); IMGUI_API bool BeginTableEx(const char* name, ImGuiID id, int columns_count, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0, 0), float inner_width = 0.0f); IMGUI_API void TableBeginInitMemory(ImGuiTable* table, int columns_count); @@ -2410,7 +2431,7 @@ namespace ImGui IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known); IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); - IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0); + IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, ImDrawFlags flags = 0); IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. @@ -2436,7 +2457,7 @@ namespace ImGui IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); - IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawCornerFlags rounding_corners); + IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawFlags flags); IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col); IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); @@ -2543,11 +2564,11 @@ IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table //----------------------------------------------------------------------------- #ifdef IMGUI_ENABLE_TEST_ENGINE -extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); -extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); -extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); -extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id, const void* data_id_end); -extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); +extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); +extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); +extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); +extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id, const void* data_id_end); +extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) #define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log diff --git a/imgui/imgui_tables.cpp b/imgui/imgui_tables.cpp index 61a84f9f..edeef738 100644 --- a/imgui/imgui_tables.cpp +++ b/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (tables and columns code) /* @@ -166,7 +166,7 @@ Index of this file: // - Case B: column is clipped / out of sight (because of scrolling or parent ClipRect): TableNextColumn() return false as a hint but we still allow layout output. // - Case C: column is hidden explicitly by the user (e.g. via the context menu, or _DefaultHide column flag, etc.). // -// [A] [B] [C] +// [A] [B] [C] // TableNextColumn(): true false false -> [userland] when TableNextColumn() / TableSetColumnIndex() return false, user can skip submitting items but only if the column doesn't contribute to row height. // SkipItems: false false true -> [internal] when SkipItems is true, most widgets will early out if submitted, resulting is no layout output. // ClipRect: normal zero-width zero-width -> [internal] when ClipRect is zero, ItemAdd() will return false and most widgets will early out mid-way. @@ -538,9 +538,9 @@ void ImGui::TableBeginInitMemory(ImGuiTable* table, int columns_count) { // Allocate single buffer for our arrays ImSpanAllocator<3> span_allocator; - span_allocator.ReserveBytes(0, columns_count * sizeof(ImGuiTableColumn)); - span_allocator.ReserveBytes(1, columns_count * sizeof(ImGuiTableColumnIdx)); - span_allocator.ReserveBytes(2, columns_count * sizeof(ImGuiTableCellData)); + span_allocator.Reserve(0, columns_count * sizeof(ImGuiTableColumn)); + span_allocator.Reserve(1, columns_count * sizeof(ImGuiTableColumnIdx)); + span_allocator.Reserve(2, columns_count * sizeof(ImGuiTableCellData), 4); table->RawData = IM_ALLOC(span_allocator.GetArenaSizeInBytes()); memset(table->RawData, 0, span_allocator.GetArenaSizeInBytes()); span_allocator.SetArenaBasePtr(table->RawData); @@ -635,7 +635,7 @@ static void TableSetupColumnFlags(ImGuiTable* table, ImGuiTableColumn* column, I { IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiTableColumnFlags_WidthMask_)); // Check that only 1 of each set is used. } - + // Resize if ((table->Flags & ImGuiTableFlags_Resizable) == 0) flags |= ImGuiTableColumnFlags_NoResize; @@ -687,13 +687,14 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->ColumnsEnabledCount = 0; table->EnabledMaskByIndex = 0x00; table->EnabledMaskByDisplayOrder = 0x00; + table->LeftMostEnabledColumn = -1; table->MinColumnWidth = ImMax(1.0f, g.Style.FramePadding.x * 1.0f); // g.Style.ColumnsMinSpacing; // FIXME-TABLE // [Part 1] Apply/lock Enabled and Order states. Calculate auto/ideal width for columns. Count fixed/stretch columns. // Process columns in their visible orders as we are building the Prev/Next indices. int count_fixed = 0; // Number of columns that have fixed sizing policies int count_stretch = 0; // Number of columns that have stretch sizing policies - int last_visible_column_idx = -1; + int prev_visible_column_idx = -1; bool has_auto_fit_request = false; bool has_resizable = false; float stretch_sum_width_auto = 0.0f; @@ -741,14 +742,16 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } // Mark as enabled and link to previous/next enabled column - column->PrevEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; + column->PrevEnabledColumn = (ImGuiTableColumnIdx)prev_visible_column_idx; column->NextEnabledColumn = -1; - if (last_visible_column_idx != -1) - table->Columns[last_visible_column_idx].NextEnabledColumn = (ImGuiTableColumnIdx)column_n; + if (prev_visible_column_idx != -1) + table->Columns[prev_visible_column_idx].NextEnabledColumn = (ImGuiTableColumnIdx)column_n; + else + table->LeftMostEnabledColumn = (ImGuiTableColumnIdx)column_n; column->IndexWithinEnabledSet = table->ColumnsEnabledCount++; table->EnabledMaskByIndex |= (ImU64)1 << column_n; table->EnabledMaskByDisplayOrder |= (ImU64)1 << column->DisplayOrder; - last_visible_column_idx = column_n; + prev_visible_column_idx = column_n; IM_ASSERT(column->IndexWithinEnabledSet <= column->DisplayOrder); // Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping) @@ -778,8 +781,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } if ((table->Flags & ImGuiTableFlags_Sortable) && table->SortSpecsCount == 0 && !(table->Flags & ImGuiTableFlags_SortTristate)) table->IsSortSpecsDirty = true; - table->RightMostEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; - IM_ASSERT(table->RightMostEnabledColumn >= 0); + table->RightMostEnabledColumn = (ImGuiTableColumnIdx)prev_visible_column_idx; + IM_ASSERT(table->LeftMostEnabledColumn >= 0 && table->RightMostEnabledColumn >= 0); // [Part 2] Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible // to avoid the column fitting having to wait until the first visible frame of the child container (may or not be a good thing). @@ -805,7 +808,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // Apply same widths policy float width_auto = column->WidthAuto; if (table_sizing_policy == ImGuiTableFlags_SizingFixedSame && (column->AutoFitQueue != 0x00 || !column_is_resizable)) - width_auto = fixed_max_width_auto; + width_auto = fixed_max_width_auto; // Apply automatic width // Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!) @@ -2364,7 +2367,7 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) if ((merge_group_n & 2) != 0 && (table->Flags & ImGuiTableFlags_NoHostExtendY) == 0) merge_clip_rect.Max.y = ImMax(merge_clip_rect.Max.y, host_rect.Max.y); #if 0 - GetOverlayDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, ~0, 1.0f); + GetOverlayDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, 0, 1.0f); GetOverlayDrawList()->AddLine(merge_group->ClipRect.Min, merge_clip_rect.Min, IM_COL32(255, 100, 0, 200)); GetOverlayDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200)); #endif @@ -2477,7 +2480,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) const ImU32 outer_col = table->BorderColorStrong; if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter) { - inner_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, ~0, border_size); + inner_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, 0, border_size); } else if (table->Flags & ImGuiTableFlags_BordersOuterV) { diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index d2a4d1dc..c5ee895d 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.81 +// dear imgui, v1.82 // (widgets code) /* @@ -500,7 +500,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool flags |= ImGuiButtonFlags_PressedOnDefault_; ImGuiWindow* backup_hovered_window = g.HoveredWindow; - const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredRootWindow == window; + const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredWindow && g.HoveredWindow->RootWindow == window; if (flatten_hovered_children) g.HoveredWindow = window; @@ -782,22 +782,30 @@ bool ImGui::ArrowButton(const char* str_id, ImGuiDir dir) } // Button to close a window -bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)//, float size) +bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window. - // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). + // Tweak 1: Shrink hit-testing area if button covers an abnormally large proportion of the visible region. That's in order to facilitate moving the window away. (#3825) + // This may better be applied as a general hit-rect reduction mechanism for all widgets to ensure the area to move window is always accessible? const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); - bool is_clipped = !ItemAdd(bb, id); + ImRect bb_interact = bb; + const float area_to_visible_ratio = window->OuterRectClipped.GetArea() / bb.GetArea(); + if (area_to_visible_ratio < 1.5f) + bb_interact.Expand(ImFloor(bb_interact.GetSize() * -0.25f)); + + // Tweak 2: We intentionally allow interaction when clipped so that a mechanical Alt,Right,Activate sequence can always close a window. + // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). + bool is_clipped = !ItemAdd(bb_interact, id); bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held); + bool pressed = ButtonBehavior(bb_interact, id, &hovered, &held); if (is_clipped) return pressed; // Render + // FIXME: Clarify this mess ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); ImVec2 center = bb.GetCenter(); if (hovered) @@ -866,19 +874,19 @@ void ImGui::Scrollbar(ImGuiAxis axis) // Calculate scrollbar bounding box ImRect bb = GetWindowScrollbarRect(window, axis); - ImDrawCornerFlags rounding_corners = 0; + ImDrawFlags rounding_corners = ImDrawFlags_RoundCornersNone; if (axis == ImGuiAxis_X) { - rounding_corners |= ImDrawCornerFlags_BotLeft; + rounding_corners |= ImDrawFlags_RoundCornersBottomLeft; if (!window->ScrollbarY) - rounding_corners |= ImDrawCornerFlags_BotRight; + rounding_corners |= ImDrawFlags_RoundCornersBottomRight; } else { if ((window->Flags & ImGuiWindowFlags_NoTitleBar) && !(window->Flags & ImGuiWindowFlags_MenuBar)) - rounding_corners |= ImDrawCornerFlags_TopRight; + rounding_corners |= ImDrawFlags_RoundCornersTopRight; if (!window->ScrollbarX) - rounding_corners |= ImDrawCornerFlags_BotRight; + rounding_corners |= ImDrawFlags_RoundCornersBottomRight; } float size_avail = window->InnerRect.Max[axis] - window->InnerRect.Min[axis]; float size_contents = window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f; @@ -891,7 +899,7 @@ void ImGui::Scrollbar(ImGuiAxis axis) // - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar // - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. // Still, the code should probably be made simpler.. -bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float size_avail_v, float size_contents_v, ImDrawCornerFlags rounding_corners) +bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float size_avail_v, float size_contents_v, ImDrawFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -971,7 +979,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa // Render const ImU32 bg_col = GetColorU32(ImGuiCol_ScrollbarBg); const ImU32 grab_col = GetColorU32(held ? ImGuiCol_ScrollbarGrabActive : hovered ? ImGuiCol_ScrollbarGrabHovered : ImGuiCol_ScrollbarGrab, alpha); - window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, bg_col, window->WindowRounding, rounding_corners); + window->DrawList->AddRectFilled(bb_frame.Min, bb_frame.Max, bg_col, window->WindowRounding, flags); ImRect grab_rect; if (axis == ImGuiAxis_X) grab_rect = ImRect(ImLerp(bb.Min.x, bb.Max.x, grab_v_norm), bb.Min.y, ImLerp(bb.Min.x, bb.Max.x, grab_v_norm) + grab_h_pixels, bb.Max.y); @@ -1571,12 +1579,12 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); RenderNavHighlight(frame_bb, id); if (!(flags & ImGuiComboFlags_NoPreview)) - window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Left); + window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersLeft); if (!(flags & ImGuiComboFlags_NoArrowButton)) { ImU32 bg_col = GetColorU32((popup_open || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); - window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawCornerFlags_All : ImDrawCornerFlags_Right); + window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersRight); if (value_x2 + arrow_size - style.FramePadding.x <= frame_bb.Max.x) RenderArrow(window->DrawList, ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), text_col, ImGuiDir_Down, 1.0f); } @@ -2069,12 +2077,35 @@ static const char* ImAtoi(const char* src, TYPE* output) return src; } +// Sanitize format +// - Zero terminate so extra characters after format (e.g. "%f123") don't confuse atof/atoi +// - stb_sprintf.h supports several new modifiers which format numbers in a way that also makes them incompatible atof/atoi. +static void SanitizeFormatString(const char* fmt, char* fmt_out, size_t fmt_out_size) +{ + const char* fmt_end = ImParseFormatFindEnd(fmt); + IM_ASSERT((size_t)(fmt_end - fmt + 1) < fmt_out_size); // Format is too long, let us know if this happens to you! + while (fmt < fmt_end) + { + char c = *(fmt++); + if (c != '\'' && c != '$' && c != '_') // Custom flags provided by stb_sprintf.h. POSIX 2008 also supports '. + *(fmt_out++) = c; + } + *fmt_out = 0; // Zero-terminate +} + template TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v) { const char* fmt_start = ImParseFormatFindStart(format); if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string return v; + + // Sanitize format + char fmt_sanitized[32]; + SanitizeFormatString(fmt_start, fmt_sanitized, IM_ARRAYSIZE(fmt_sanitized)); + fmt_start = fmt_sanitized; + + // Format value with our rounding, and read back char v_str[64]; ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v); const char* p = v_str; @@ -2112,9 +2143,9 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const { ImGuiContext& g = *GImGui; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; - const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool is_clamped = (v_min < v_max); - const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; + const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; + const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); // Default tweak speed if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) @@ -2132,7 +2163,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const } else if (g.ActiveIdSource == ImGuiInputSource_Nav) { - int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; + const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); } @@ -2172,7 +2203,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const if (is_logarithmic) { // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. - const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; + const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1; logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); // Convert to parametric space, apply delta, convert back @@ -2210,9 +2241,9 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const // Clamp values (+ handle overflow/wrap-around for integer types) if (*v != v_cur && is_clamped) { - if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal)) + if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_floating_point)) v_cur = v_min; - if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal)) + if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_floating_point)) v_cur = v_max; } @@ -2603,7 +2634,7 @@ TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, T { if (v_min == v_max) return v_min; - const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); TYPE result; if (is_logarithmic) @@ -2651,7 +2682,7 @@ TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, T else { // Linear slider - if (is_decimal) + if (is_floating_point) { result = ImLerp(v_min, v_max, t); } @@ -2684,14 +2715,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ const ImGuiStyle& style = g.Style; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; - const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); - const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; + const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; + const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const float grab_padding = 2.0f; const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; float grab_sz = style.GrabMinSize; SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); - if (!is_decimal && v_range >= 0) // v_range < 0 may happen on integer overflows + if (!is_floating_point && v_range >= 0) // v_range < 0 may happen on integer overflows grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit grab_sz = ImMin(grab_sz, slider_sz); const float slider_usable_sz = slider_sz - grab_sz; @@ -2703,7 +2734,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ if (is_logarithmic) { // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. - const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; + const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1; logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f); } @@ -2741,7 +2772,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ float input_delta = (axis == ImGuiAxis_X) ? input_delta2.x : -input_delta2.y; if (input_delta != 0.0f) { - const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; + const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; if (decimal_precision > 0) { input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds @@ -3286,7 +3317,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); if (p_clamp_min || p_clamp_max) { - if (DataTypeCompare(data_type, p_clamp_min, p_clamp_max) > 0) + if (p_clamp_min && p_clamp_max && DataTypeCompare(data_type, p_clamp_min, p_clamp_max) > 0) ImSwap(p_clamp_min, p_clamp_max); DataTypeClamp(data_type, p_data, p_clamp_min, p_clamp_max); } @@ -3896,11 +3927,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiInputTextState* state = GetInputTextState(id); const bool focus_requested = FocusableItemRegister(window, id); - const bool focus_requested_by_code = focus_requested && (g.FocusRequestCurrWindow == window && g.FocusRequestCurrCounterRegular == window->DC.FocusCounterRegular); + const bool focus_requested_by_code = focus_requested && (g.TabFocusRequestCurrWindow == window && g.TabFocusRequestCurrCounterRegular == window->DC.FocusCounterRegular); const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code; const bool user_clicked = hovered && io.MouseClicked[0]; - const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_NavKeyboard)); + const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_Keyboard)); const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); @@ -3949,8 +3980,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!is_multiline && focus_requested_by_code) select_all = true; } - if (flags & ImGuiInputTextFlags_AlwaysInsertMode) - state->Stb.insert_mode = 1; + if (flags & ImGuiInputTextFlags_AlwaysOverwrite) + state->Stb.insert_mode = 1; // stb field name is indeed incorrect (see #2863) if (!is_multiline && (focus_requested_by_tab || (user_clicked && io.KeyCtrl))) select_all = true; } @@ -5200,7 +5231,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl const float a1 = (n+1.0f)/6.0f * 2.0f * IM_PI + aeps; const int vert_start_idx = draw_list->VtxBuffer.Size; draw_list->PathArcTo(wheel_center, (wheel_r_inner + wheel_r_outer)*0.5f, a0, a1, segment_per_arc); - draw_list->PathStroke(col_white, false, wheel_thickness); + draw_list->PathStroke(col_white, 0, wheel_thickness); const int vert_end_idx = draw_list->VtxBuffer.Size; // Paint colors over existing vertices @@ -5326,8 +5357,8 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) { float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f); - RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight); - window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft); + RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawFlags_RoundCornersRight); + window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawFlags_RoundCornersLeft); } else { @@ -5336,7 +5367,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl if (col_source.w < 1.0f) RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); else - window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All); + window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding); } RenderNavHighlight(bb, id); if ((flags & ImGuiColorEditFlags_NoBorder) == 0) @@ -5882,7 +5913,7 @@ void ImGui::TreePop() if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) if (g.NavIdIsAlive && (window->DC.TreeJumpToParentOnPopMask & tree_depth_mask)) { - SetNavID(window->IDStack.back(), g.NavLayer, 0); + SetNavID(window->IDStack.back(), g.NavLayer, 0, ImRect()); NavMoveRequestCancel(); } window->DC.TreeJumpToParentOnPopMask &= tree_depth_mask - 1; @@ -6070,8 +6101,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { + SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos)); g.NavDisableHighlight = true; - SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent); } } if (pressed) @@ -6189,6 +6220,7 @@ void ImGui::EndListBox() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; IM_ASSERT((window->Flags & ImGuiWindowFlags_ChildWindow) && "Mismatched BeginListBox/EndListBox calls. Did you test the return value of BeginListBox?"); + IM_UNUSED(window); EndChildFrame(); EndGroup(); // This is only required to be able to do IsItemXXX query on the whole ListBox including label @@ -6549,9 +6581,9 @@ void ImGui::EndMenuBar() const ImGuiNavLayer layer = ImGuiNavLayer_Menu; IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check FocusWindow(window); - SetNavIDWithRectRel(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); - g.NavLayer = layer; + SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. + g.NavDisableMouseHover = g.NavMousePosDirty = true; g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; NavMoveRequestCancel(); } @@ -6582,7 +6614,7 @@ bool ImGui::BeginMainMenuBar() if (menu_bar_window == NULL || menu_bar_window->BeginCount == 0) { // Set window position - // We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. + // We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. // However menu-bar will affect the minimum window size so we'll get the right height. ImVec2 menu_bar_pos = viewport->Pos + viewport->CurrWorkOffsetMin; ImVec2 menu_bar_size = ImVec2(viewport->Size.x - viewport->CurrWorkOffsetMin.x + viewport->CurrWorkOffsetMax.x, 1.0f); @@ -6898,7 +6930,7 @@ namespace ImGui static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label); static float TabBarCalcMaxTabWidth(); static float TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling); - static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImGuiTabBarSection* sections); + static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGuiTabBarSection* sections); static ImGuiTabItem* TabBarScrollingButtons(ImGuiTabBar* tab_bar); static ImGuiTabItem* TabBarTabListPopupButton(ImGuiTabBar* tab_bar); } @@ -7109,12 +7141,12 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) sections[1].Spacing = sections[1].TabCount > 0 && sections[2].TabCount > 0 ? g.Style.ItemInnerSpacing.x : 0.0f; // Setup next selected tab - ImGuiID scroll_track_selected_tab_id = 0; + ImGuiID scroll_to_tab_id = 0; if (tab_bar->NextSelectedTabId) { tab_bar->SelectedTabId = tab_bar->NextSelectedTabId; tab_bar->NextSelectedTabId = 0; - scroll_track_selected_tab_id = tab_bar->SelectedTabId; + scroll_to_tab_id = tab_bar->SelectedTabId; } // Process order change request (we could probably process it when requested but it's just saner to do it in a single spot). @@ -7122,7 +7154,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) { if (TabBarProcessReorder(tab_bar)) if (tab_bar->ReorderRequestTabId == tab_bar->SelectedTabId) - scroll_track_selected_tab_id = tab_bar->ReorderRequestTabId; + scroll_to_tab_id = tab_bar->ReorderRequestTabId; tab_bar->ReorderRequestTabId = 0; } @@ -7130,7 +7162,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) const bool tab_list_popup_button = (tab_bar->Flags & ImGuiTabBarFlags_TabListPopupButton) != 0; if (tab_list_popup_button) if (ImGuiTabItem* tab_to_select = TabBarTabListPopupButton(tab_bar)) // NB: Will alter BarRect.Min.x! - scroll_track_selected_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; + scroll_to_tab_id = tab_bar->SelectedTabId = tab_to_select->ID; // Leading/Trailing tabs will be shrink only if central one aren't visible anymore, so layout the shrink data as: leading, trailing, central // (whereas our tabs are stored as: leading, central, trailing) @@ -7150,8 +7182,8 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) most_recently_selected_tab = tab; if (tab->ID == tab_bar->SelectedTabId) found_selected_tab_id = true; - if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) - scroll_track_selected_tab_id = tab->ID; + if (scroll_to_tab_id == 0 && g.NavJustMovedToId == tab->ID) + scroll_to_tab_id = tab->ID; // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar. // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, @@ -7182,11 +7214,11 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Horizontal scrolling buttons // (note that TabBarScrollButtons() will alter BarRect.Max.x) if ((tab_bar->WidthAllTabsIdeal > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll)) - if (ImGuiTabItem* scroll_track_selected_tab = TabBarScrollingButtons(tab_bar)) + if (ImGuiTabItem* scroll_and_select_tab = TabBarScrollingButtons(tab_bar)) { - scroll_track_selected_tab_id = scroll_track_selected_tab->ID; - if (!(scroll_track_selected_tab->Flags & ImGuiTabItemFlags_Button)) - tab_bar->SelectedTabId = scroll_track_selected_tab_id; + scroll_to_tab_id = scroll_and_select_tab->ID; + if ((scroll_and_select_tab->Flags & ImGuiTabItemFlags_Button) == 0) + tab_bar->SelectedTabId = scroll_to_tab_id; } // Shrink widths if full tabs don't fit in their allocated space @@ -7246,16 +7278,15 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) if (found_selected_tab_id == false) tab_bar->SelectedTabId = 0; if (tab_bar->SelectedTabId == 0 && tab_bar->NextSelectedTabId == 0 && most_recently_selected_tab != NULL) - scroll_track_selected_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID; + scroll_to_tab_id = tab_bar->SelectedTabId = most_recently_selected_tab->ID; // Lock in visible tab tab_bar->VisibleTabId = tab_bar->SelectedTabId; tab_bar->VisibleTabWasSubmitted = false; // Update scrolling - if (scroll_track_selected_tab_id) - if (ImGuiTabItem* scroll_track_selected_tab = TabBarFindTabByID(tab_bar, scroll_track_selected_tab_id)) - TabBarScrollToTab(tab_bar, scroll_track_selected_tab, sections); + if (scroll_to_tab_id != 0) + TabBarScrollToTab(tab_bar, scroll_to_tab_id, sections); tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim); tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget); if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget) @@ -7355,8 +7386,12 @@ static float ImGui::TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling) return ImMax(scrolling, 0.0f); } -static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImGuiTabBarSection* sections) +// Note: we may scroll to tab that are not selected! e.g. using keyboard arrow keys +static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGuiTabBarSection* sections) { + ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id); + if (tab == NULL) + return; if (tab->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) return; @@ -7856,7 +7891,7 @@ void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabI draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding + 0.5f, y1 + rounding + 0.5f), rounding, 6, 9); draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding - 0.5f, y1 + rounding + 0.5f), rounding, 9, 12); draw_list->PathLineTo(ImVec2(bb.Max.x - 0.5f, y2)); - draw_list->PathStroke(GetColorU32(ImGuiCol_Border), false, g.Style.TabBorderSize); + draw_list->PathStroke(GetColorU32(ImGuiCol_Border), 0, g.Style.TabBorderSize); } } diff --git a/include/geodesics/geodesics.h b/include/geodesics/geodesics.h index 6379cad3..a3f2d79c 100644 --- a/include/geodesics/geodesics.h +++ b/include/geodesics/geodesics.h @@ -39,7 +39,7 @@ class geodesics real_t radio = INFINITY; ///< execute until the specific radio. real_t * dist_alloc = nullptr; ///< external dist allocation bool cluster = false; ///< to cluster vertices to closest source. - fm_function_t fun; ///< fun is executed inside FM loop + fm_function_t fun = nullptr; ///< fun is executed inside FM loop }; public: @@ -56,7 +56,7 @@ class geodesics public: geodesics( che * mesh, ///< input triangular mesh. const std::vector & sources, ///< source vertices. - const params & p = {FM, 0, INFINITY, nullptr, false} + const params & p = {FM, 0, INFINITY, nullptr, false, nullptr} ); virtual ~geodesics(); From f4eb0c5b08d3c9aefa461fab3a93e16f44305f4e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Apr 2021 15:10:24 +0200 Subject: [PATCH 0486/1018] cmake add -Wall and -Wextra compile options --- CMakeLists.txt | 2 +- include/geodesics/geodesics.h | 4 ++-- src/geodesics/geodesics.cpp | 8 ++++---- test_image_denoising.cpp | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01d39124..6d311fc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ endif() set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_compile_options(-Wall -Wextra) +add_compile_options(-Wall -Wextra -Wno-unused-result) find_package(CUDAToolkit 11) if(CUDAToolkit_FOUND) diff --git a/include/geodesics/geodesics.h b/include/geodesics/geodesics.h index a3f2d79c..c1075cbd 100644 --- a/include/geodesics/geodesics.h +++ b/include/geodesics/geodesics.h @@ -71,11 +71,11 @@ class geodesics private: void execute(che * mesh, const std::vector & sources, const params & p); void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun); - void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); + void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources); void run_heat_method(che * mesh, const std::vector & sources); #ifdef GPROSHAN_CUDA - void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio); + void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources); void run_heat_method_gpu(che * mesh, const std::vector & sources); #endif // GPROSHAN_CUDA diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index b27bca44..80daa17a 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -98,13 +98,13 @@ void geodesics::execute(che * mesh, const vector & sources, const param { case FM: run_fastmarching(mesh, sources, p.n_iter, p.radio, p.fun); break; - case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources, p.n_iter, p.radio); + case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources); break; case HEAT_METHOD: run_heat_method(mesh, sources); break; #ifdef GPROSHAN_CUDA - case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources, p.n_iter, p.radio); + case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources); break; case HEAT_METHOD_GPU: run_heat_method_gpu(mesh, sources); break; @@ -194,7 +194,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co delete [] color; } -void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) +void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector & sources) { index_t * toplesets = new index_t[n_vertices]; vector limits; @@ -225,7 +225,7 @@ void geodesics::run_heat_method(che * mesh, const vector & sources) #ifdef GPROSHAN_CUDA -void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio) +void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources) { index_t * toplesets = new index_t[n_vertices]; vector limits; diff --git a/test_image_denoising.cpp b/test_image_denoising.cpp index 955d166c..0319c818 100644 --- a/test_image_denoising.cpp +++ b/test_image_denoising.cpp @@ -2,7 +2,8 @@ int main(int nargs, const char ** args) { - gproshan::mdict::test_image_denoising(args[1]); + for(int i = 1; i < nargs; ++i) + gproshan::mdict::test_image_denoising(args[i]); return 0; } From f125d9201f45e39a4b8500ec376a4336b70614c3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Apr 2021 16:15:22 +0200 Subject: [PATCH 0487/1018] solving extra warnings --- src/geodesics/geodesics_ptp_coalescence.cu | 11 +++++++---- src/geodesics/heat_method.cpp | 10 ++++++---- src/geodesics/heat_method.cu | 8 ++++---- src/mdict/msparse_coding.cpp | 2 +- src/mesh/che_fill_hole.cpp | 2 +- src/viewer/camera.cpp | 2 +- src/viewer/viewer.cpp | 6 +++--- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index 5dc0028a..06c49d61 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -41,6 +41,13 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, real_t * h_dist = new real_t[h_mesh->n_vertices]; + if(set_inf) + { + #pragma omp parallel for + for(index_t v = 0; v < h_mesh->n_vertices; ++v) + h_dist[v] = INFINITY; + } + real_t * d_dist[2]; cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); @@ -101,10 +108,6 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); ++i) h_dist[inv.index[sources[i]]] = 0; diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 7623b82e..61db71fd 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -93,10 +93,12 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) sum = 0; for_star(he, mesh, v) - sum += ( - mesh->normal_he(he) * ( mesh->gt_vt(prev(he)) - mesh->gt_vt(next(he)) ) , - - mesh->gradient_he(he, u.memptr()) - ); + { + const vertex & nhe = mesh->normal_he(he); + const vertex & vhe = mesh->gt_vt(prev(he)) - mesh->gt_vt(next(he)); + const vertex & ghe = mesh->gradient_he(he, u.memptr()); + sum += (nhe * vhe , -ghe); + } } } diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index 6f70ca93..21c876df 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -15,7 +15,7 @@ struct cu_spAxb int * A_col_ptrs, * A_row_indices; real_t * A_values, * x, * b; - cu_spAxb(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx) + cu_spAxb(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb) { cudaMalloc(&A_col_ptrs, (m + 1) * sizeof(int)); cudaMemcpy(A_col_ptrs, hA_col_ptrs, (m + 1) * sizeof(int), cudaMemcpyHostToDevice); @@ -75,7 +75,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t else { // allocate A, x, b into device - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb, hx); + cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); cusolverStatus_t status; #ifdef SINGLE_P @@ -117,7 +117,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t cudaEventCreate(&stop); // allocate A, x, b into device - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb, hx); + cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); // aux vector y to device real_t * dy; @@ -317,7 +317,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons } else { - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb, hx); + cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); csrcholInfo_t info; cusolverSpCreateCsrcholInfo(&info); diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 7f96b438..eb9051ff 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -467,7 +467,7 @@ void msparse_coding::init_voronoi_patches() bool * pmask = mask; for(index_t s = 0; s < m_params.n_patches; ++s) - patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s ,[&pmask](const index_t & i) -> bool { return pmask[i]; } ); + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); #pragma omp parallel for for(index_t s = 0; s < m_params.n_patches; ++s) diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index 7a479013..e323d4fb 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -139,7 +139,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const if(i != p.first) { merge_vertices[c].push_back(vertices[c].size() + hole->n_vertices - merge_vertices[!c].size()); - n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1 , hole->n_vertices - merge_vertices[!c].size()); + n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1, hole->n_vertices - merge_vertices[!c].size()); } else merge_vertices[c].push_back(merge_vertices[!c].front()); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index e6aca091..b897352b 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -38,7 +38,7 @@ quaternion camera::current_rotation() const return (p_drag * p_click.conj()) * r_last; } -void camera::mouse(int , int state, int x, int y, int w, int h) +void camera::mouse(int, int state, int x, int y, int w, int h) { quaternion momentum = 1; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0f91cadd..17c5cdaf 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -368,7 +368,7 @@ void viewer::window_size_callback(GLFWwindow * window, int width, int height) view->window_height = height; } -void viewer::keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods) +void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, int) { if(action == GLFW_RELEASE) return; @@ -410,7 +410,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) } } -void viewer::scroll_callback(GLFWwindow * window, double xoffset, double yoffset) +void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; @@ -901,7 +901,7 @@ void viewer::select_border_vertices() mesh.selected.push_back(mesh->vt(he)); } -void viewer::pick_vertex(int x, int y) +void viewer::pick_vertex(int, int) { gproshan_log(VIEWER); } From 861c15ac9dd1eb0c31538a6b4e5f3bd29b749349 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Apr 2021 16:29:06 +0200 Subject: [PATCH 0488/1018] solving extra warnings --- include/mdict/msparse_coding.h | 2 +- include/mdict/patch.h | 3 +-- src/mdict/msparse_coding.cpp | 4 ++-- src/mdict/patch.cpp | 4 ++-- src/raytracing/rt_embree.cpp | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/mdict/msparse_coding.h b/include/mdict/msparse_coding.h index 67de6675..ecfdaa98 100644 --- a/include/mdict/msparse_coding.h +++ b/include/mdict/msparse_coding.h @@ -76,7 +76,7 @@ class msparse_coding void load_mask(); void init_voronoi_patches(); void init_radial_feature_patches(); - void load_sampling(bool save_all); + void load_sampling(); che * point_cloud_reconstruction(real_t per, real_t fr); vector sort_indexes(const vector &v); diff --git a/include/mdict/patch.h b/include/mdict/patch.h index 73105f01..1ab0cc1a 100644 --- a/include/mdict/patch.h +++ b/include/mdict/patch.h @@ -109,8 +109,7 @@ class patch void compute_avg_distance(che * mesh, vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces( const vertex & c, - vertex & n, + bool add_vertex_by_faces( vertex & n, vector & N, double thr_angle, const real_t * geo, diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index eb9051ff..9e92aac5 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -171,7 +171,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde } -void msparse_coding::load_sampling(bool save_all) +void msparse_coding::load_sampling() { size_t featsize; vector all_sorted_features; @@ -303,7 +303,7 @@ void msparse_coding::load_sampling(bool save_all) void msparse_coding::init_radial_feature_patches() { - load_sampling(false); + load_sampling(); load_mask(); #ifndef NDEBUG diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index a21c495a..dd901b84 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -76,7 +76,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(const vertex & c, vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) +bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -266,7 +266,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, ratio = area / proj_area; - if(add_vertex_by_faces(c, n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) + if(add_vertex_by_faces(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); return true; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 62f0021d..a86d67b0 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -68,7 +68,7 @@ glm::vec3 embree::ray_hit::position() const } -void embree_error(void * ptr, RTCError error, const char * str) +void embree_error(void *, RTCError, const char * str) { fprintf(stderr, "EMBREE ERROR: %s\n", str); } From 74d5c81bd3be48f59d4d70ae2a827d9522f1846b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Apr 2021 00:43:26 +0200 Subject: [PATCH 0489/1018] che: add rgb_t struct 3 bytes --- include/mesh/che.h | 25 ++++++++++++++++++++----- src/mesh/che.cpp | 10 +++++----- src/mesh/che_obj.cpp | 6 +++--- src/mesh/che_off.cpp | 20 +++++++------------- src/mesh/che_ply.cpp | 13 ++++--------- src/mesh/che_ptx.cpp | 11 ++++------- src/mesh/che_xyz.cpp | 15 ++++++++------- src/viewer/che_viewer.cpp | 8 ++++---- 8 files changed, 55 insertions(+), 53 deletions(-) diff --git a/include/mesh/che.h b/include/mesh/che.h index d606baf8..d6fe7fc0 100644 --- a/include/mesh/che.h +++ b/include/mesh/che.h @@ -28,7 +28,23 @@ struct corr_t; class che { public: - enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types + enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types + struct rgb_t + { + unsigned char r = 230; + unsigned char g = 240; + unsigned char b = 250; + + unsigned char & operator [] (const index_t & i) + { + return (&r)[i]; + } + + operator vertex () const + { + return {float(r) / 255, float(g) / 255, float(b) / 255}; + } + }; const size_t n_vertices = 0; const size_t n_faces = 0; @@ -36,7 +52,6 @@ class che const size_t n_edges = 0; std::string filename; ///< get and set data member - vertex vcolor{ 0.75, 0.85, 1.0 }; protected: vertex * GT = nullptr; ///< geometry table : v -> vertex @@ -47,7 +62,7 @@ class che index_t * EHT = nullptr; ///< extra half edge table : he -> e vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - vertex * VC = nullptr; ///< vertex color : v -> color(v) + rgb_t * VC = nullptr; ///< vertex color : v -> color(v) real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) bool manifold = true; @@ -71,8 +86,8 @@ class che real_t area_vertex(const index_t & v) const; real_t area_surface() const; void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); - const vertex & color(const index_t & v) const; - vertex & color(const index_t & v); + const rgb_t & rgb(const index_t & v) const; + vertex color(const index_t & v) const; vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; const real_t & heatmap(const index_t & v) const; real_t & heatmap(const index_t & v); diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 8a6f94d3..d336bccf 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -285,13 +285,13 @@ void che::update_heatmap(const real_t * hm, real_t max_color) VHC[v] = hm[v] / max_color; } -const vertex & che::color(const index_t & v) const +const che::rgb_t & che::rgb(const index_t & v) const { assert(VC && v < n_vertices); return VC[v]; } -vertex & che::color(const index_t & v) +vertex che::color(const index_t & v) const { assert(VC && v < n_vertices); return VC[v]; @@ -301,7 +301,7 @@ vertex che::shading_color(const index_t & f, const float & u, const float & v, c { index_t he = f * che::mtrig; - return VC ? u * VC[VT[he]] + v * VC[VT[he + 1]] + w * VC[VT[he + 2]] : vcolor; + return VC ? u * color(VT[he]) + v * color(VT[he + 1]) + w * color(VT[he + 2]) : rgb_t(); } const real_t & che::heatmap(const index_t & v) const @@ -1358,12 +1358,12 @@ void che::alloc(const size_t & n_v, const size_t & n_f) if(n_half_edges) EHT = new index_t[n_half_edges]; if(n_vertices) VN = new vertex[n_vertices]; - if(n_vertices) VC = new vertex[n_vertices]; + if(n_vertices) VC = new rgb_t[n_vertices]; if(n_vertices) VHC = new real_t[n_vertices]; #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - VC[v] = vcolor; + VC[v] = rgb_t(); update_heatmap(); } diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 9edb78ee..53050a5a 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -26,7 +26,7 @@ void che_obj::read_file(const string & file) index_t P[32], n; vector vertices; - vector vertices_color; + vector vertices_color; vector faces; char line[256], str[64]; @@ -45,7 +45,7 @@ void che_obj::read_file(const string & file) { n = sscanf(line_ptr, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); vertices.push_back({x, y, z}); - vertices_color.push_back(n == 6 ? vertex{r, g, b} : vcolor); + vertices_color.push_back(n == 6 ? rgb_t{(unsigned char) (r * 255), (unsigned char) (g * 255), (unsigned char) (b * 255)} : rgb_t()); } if(str[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... @@ -69,7 +69,7 @@ void che_obj::read_file(const string & file) alloc(vertices.size(), faces.size() / che::mtrig); memcpy(GT, vertices.data(), vertices.size() * sizeof(vertex)); - memcpy(VC, vertices_color.data(), vertices_color.size() * sizeof(vertex)); + memcpy(VC, vertices_color.data(), vertices_color.size() * sizeof(rgb_t)); memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index d1352946..5b693748 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -30,7 +30,8 @@ void che_off::read_file(const string & file) alloc(nv, nf); - float x, y, z, r, g, b, a; + float x, y, z; + unsigned char r, g, b, a; for(index_t v = 0; v < n_vertices; ++v) { fscanf(fp, "%f %f %f", &x, &y, &z); @@ -38,7 +39,7 @@ void che_off::read_file(const string & file) if(soff[0] == 'C' || soff[1] == 'C') { - fscanf(fp, "%f %f %f %f", &r, &g, &b, &a); + fscanf(fp, "%hhu %hhu %hhu %hhu", &r, &g, &b, &a); VC[v] = { r, g, b }; } @@ -49,13 +50,6 @@ void che_off::read_file(const string & file) } } - if(soff[0] == 'C' || soff[1] == 'C') - { - #pragma omp parallel for - for(index_t i = 0; i < n_vertices; ++i) - VC[i] /= 255; - } - vector faces; faces.reserve(che::mtrig * n_faces); @@ -76,15 +70,15 @@ void che_off::read_file(const string & file) if(faces.size() != che::mtrig * n_faces) { vertex * tGT = GT; GT = nullptr; - vertex * tVC = VC; VC = nullptr; vertex * tVN = VN; VN = nullptr; + rgb_t * tVC = VC; VC = nullptr; free(); alloc(nv, faces.size() / che::mtrig); GT = tGT; - VC = tVC; VN = tVN; + VC = tVC; } memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); @@ -107,8 +101,8 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t if(off == COFF || off == NCOFF) { - vertex c = 255 * mesh->color(i); - fprintf(fp, " %d %d %d 1", (int) c.x, (int) c.y, (int) c.z); + const rgb_t & c = mesh->rgb(i); + fprintf(fp, " %hhu %hhu %hhu 1", c.r, c.g, c.b); } if(off == NOFF || off == NCOFF) diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 00d6d9e5..95a6ce3e 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -169,9 +169,9 @@ void che_ply::read_file(const string & file) { for(index_t i = 0; i < 3; ++i) if(rgb == 1) - VC[v][i] = ((real_t) *(unsigned char *) (pb + irgb + i * rgb)) / 255; + VC[v][i] = *(unsigned char *) (pb + irgb + i * rgb); else - GT[v][i] = (real_t) *(float *) (pb + irgb + i * rgb); + GT[v][i] = (unsigned char) (*(float *) (pb + irgb + i * rgb)) * 255; } } } @@ -211,7 +211,7 @@ void che_ply::read_file(const string & file) if(faces.size() != che::mtrig * n_faces) { vertex * tGT = GT; GT = nullptr; - vertex * tVC = VC; VC = nullptr; + rgb_t * tVC = VC; VC = nullptr; free(); alloc(nv, faces.size() / che::mtrig); @@ -249,15 +249,10 @@ void che_ply::write_file(const che * mesh, const string & file, const bool & col if(color) { - unsigned char rgb[3]; for(index_t v = 0; v < mesh->n_vertices; ++v) { fwrite(&mesh->gt(v), sizeof(vertex), 1, fp); - - vertex c = 255 * mesh->color(v); - rgb[0] = c.x; rgb[1] = c.y; rgb[2] = c.z; - - fwrite(rgb, sizeof(rgb), 1, fp); + fwrite(&mesh->rgb(v), sizeof(rgb_t), 1, fp); } } else fwrite(&mesh->gt(0), sizeof(vertex), mesh->n_vertices, fp); diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 4b6ec348..6682c355 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -39,7 +39,8 @@ void che_ptx::read_file(const string & file) alloc(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); - float x, y, z, a, r, g, b; + float x, y, z, a; + unsigned char r, g, b; char line[128]; bool rgb = false; @@ -47,7 +48,7 @@ void che_ptx::read_file(const string & file) // vertex 0: x y z a or x y z a r g b fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); - rgb = sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b) == 7; + rgb = sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &a, &r, &g, &b) == 7; if(rgb) { @@ -58,7 +59,7 @@ void che_ptx::read_file(const string & file) for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); - sscanf(line, "%f %f %f %f %f %f %f", &x, &y, &z, &a, &r, &g, &b); + sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &a, &r, &g, &b); GT[v] = { x, y, z }; VC[v] = { r, g, b }; VHC[v] = a; @@ -109,10 +110,6 @@ void che_ptx::read_file(const string & file) rw(n_half_edges) = he; rw(n_faces) = he / che::mtrig; - - #pragma omp parallel for - for(index_t i = 0; i < n_vertices; ++i) - VC[i] /= 255; } diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp index 728ce43b..916365a2 100644 --- a/src/mesh/che_xyz.cpp +++ b/src/mesh/che_xyz.cpp @@ -23,27 +23,28 @@ void che_xyz::read_file(const string & file) assert(fp); char line[256], c; - float x, y, z, r, g, b; + float x, y, z; + unsigned char r, g, b; size_t n; vector vertices; - vector vertices_color; + vector vertices_color; while(fgets(line, sizeof(line), fp)) { sscanf(line, "%c", &c); if(c == '#') continue; - n = sscanf(line, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); + n = sscanf(line, "%f %f %f %hhu %hhu %hhu", &x, &y, &z, &r, &g, &b); vertices.push_back({x, y, z}); - vertices_color.push_back(n == 6 ? vertex{r, g, b} / 255 : vcolor); + vertices_color.push_back(n == 6 ? rgb_t{r, g, b} : rgb_t()); } fclose(fp); alloc(vertices.size(), 0); memcpy(GT, vertices.data(), n_vertices * sizeof(vertex)); - memcpy(VC, vertices_color.data(), n_vertices * sizeof(vertex)); + memcpy(VC, vertices_color.data(), n_vertices * sizeof(rgb_t)); } void che_xyz::write_file(const che * mesh, const string & file, const bool & color) @@ -60,8 +61,8 @@ void che_xyz::write_file(const che * mesh, const string & file, const bool & col fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); if(color) { - vertex c = 255 * mesh->color(i); - fprintf(fp, " %d %d %d", (int) c.x, (int) c.y, (int) c.z); + const rgb_t & c = mesh->rgb(i); + fprintf(fp, " %hhu %hhu %hhu", c.r, c.g, c.b); } fprintf(fp, "\n"); } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index b1c5388b..98f65562 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -91,14 +91,14 @@ void che_viewer::update_vbo() glVertexAttribPointer(1, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - // 2 MESH COLOR + // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->color(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(che::rgb_t), &mesh->rgb(0), GL_STATIC_DRAW); glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 3, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(2, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - // 3 HEAT MAP COLOR + // 3 HEAT MAP glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); glEnableVertexAttribArray(3); From 419ace3d9fd17d170664f3faf755a2d5f9d6bd5a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 7 Apr 2021 23:37:01 +0200 Subject: [PATCH 0490/1018] fix resize on frame display: glviewport --- include/include.h | 1 + include/viewer/viewer.h | 10 ++++ shaders/colormap.glsl | 4 +- shaders/fragment.glsl | 2 +- shaders/fragment_frame.glsl | 4 +- shaders/fragment_pointcloud.glsl | 4 +- shaders/geometry.glsl | 6 +-- shaders/geometry_gradient.glsl | 6 +-- shaders/vertex_frame.glsl | 4 +- shaders/vertex_sphere.glsl | 2 +- src/raytracing/raytracing.cpp | 7 ++- src/raytracing/rt_embree.cpp | 1 - src/raytracing/rt_optix.cpp | 1 - src/viewer/frame.cpp | 2 + src/viewer/shader.cpp | 1 - src/viewer/viewer.cpp | 82 +++++++++++++++----------------- 16 files changed, 70 insertions(+), 67 deletions(-) diff --git a/include/include.h b/include/include.h index 73817b2f..8a13b068 100644 --- a/include/include.h +++ b/include/include.h @@ -3,6 +3,7 @@ #include "config.h" +#include #include #include diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 339c9605..52a9b464 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -131,8 +131,12 @@ class viewer void init_glsl(); void render_gl(); + #ifdef GPROSHAN_EMBREE void render_embree(); + #endif // GPROSHAN_EMBREE + #ifdef GPROSHAN_OPTIX void render_optix(); + #endif // GPROSHAN_OPTIX static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); @@ -155,8 +159,12 @@ class viewer // render options static bool invert_orientation(viewer * view); static bool set_render_gl(viewer * view); + #ifdef GPROSHAN_EMBREE static bool set_render_embree(viewer * view); + #endif // GPROSHAN_EMBREE + #ifdef GPROSHAN_OPTIX static bool set_render_optix(viewer * view); + #endif // GPROSHAN_OPTIX static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); static bool set_render_wireframe_fill(viewer * view); @@ -166,7 +174,9 @@ class viewer static bool set_render_lines(viewer * view); static bool set_render_flat(viewer * view); + #ifdef GPROSHAN_EMBREE static bool raycasting(viewer * view); + #endif // GPROSHAN_EMBREE // draw routines void draw_meshes(shader & program, const bool & normals = false); diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl index f5c25e17..e6c7c4eb 100644 --- a/shaders/colormap.glsl +++ b/shaders/colormap.glsl @@ -4,7 +4,7 @@ float colormap_red_1(float x) { if (x < 0.7520372909206926) return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; - else + else return -1.20830453148990E+01 * x + 1.44337397593436E+01; } @@ -68,7 +68,7 @@ vec3 colormap_2(float x) float colormap_red_3(float x) { - if (x < 0.75) + if (x < 0.75) return 1012.0 * x - 389.0; else return -1.11322769567548E+03 * x + 1.24461193212872E+03; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index ef0561f9..3828734d 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -60,7 +60,7 @@ void main() N = normalize(cross(dFdx(gs_position), dFdy(gs_position))); else N = normalize(gs_normal); - + vec3 L = normalize(light - gs_position); vec3 E = normalize(eye - gs_position); vec3 R = 2 * dot(L, N) * N - L; diff --git a/shaders/fragment_frame.glsl b/shaders/fragment_frame.glsl index adf5df44..d0037e1e 100644 --- a/shaders/fragment_frame.glsl +++ b/shaders/fragment_frame.glsl @@ -1,6 +1,6 @@ #version 410 core -in vec2 UV; +in vec2 uv; out vec4 color; @@ -8,6 +8,6 @@ uniform sampler2D render_tex; void main() { - color = texture(render_tex, UV).rgba; + color = texture(render_tex, uv).rgba; } diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index ef995f46..2a38b023 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -50,11 +50,11 @@ void main() h = 1 - h; color = vec3(0) + (1. - h) * color; } - + if(point_normals) { vec3 N = normalize(vs_normal); - + vec3 L = normalize(light - vs_position); vec3 E = normalize(eye - vs_position); vec3 R = 2 * dot(L, N) * N - L; diff --git a/shaders/geometry.glsl b/shaders/geometry.glsl index 2998d0b7..3bff1831 100644 --- a/shaders/geometry.glsl +++ b/shaders/geometry.glsl @@ -36,7 +36,7 @@ void main() edge_dist = vec3(ha, 0, 0); gl_Position = gl_in[0].gl_Position; EmitVertex(); - + gs_position = vs_position[1]; gs_normal = vs_normal[1]; gs_mesh_color = vs_mesh_color[1]; @@ -44,7 +44,7 @@ void main() edge_dist = vec3(0, hb, 0); gl_Position = gl_in[1].gl_Position; EmitVertex(); - + gs_position = vs_position[2]; gs_normal = vs_normal[2]; gs_mesh_color = vs_mesh_color[2]; @@ -52,7 +52,7 @@ void main() edge_dist = vec3(0, 0, hc); gl_Position = gl_in[2].gl_Position; EmitVertex(); - + EndPrimitive(); } diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index e5dc68e2..f9c80b36 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -17,7 +17,7 @@ void main() vec3 xk = position[2]; vec3 n = normalize(cross(xj - xi, xk - xi)); - + vec3 pij = cross(n, xj - xi); vec3 pjk = cross(n, xk - xj); vec3 pki = cross(n, xi - xk); @@ -29,10 +29,10 @@ void main() gl_Position = proj_mat * model_view_mat * vec4(a, 1.); EmitVertex(); - + gl_Position = proj_mat * model_view_mat * vec4(b, 1.); EmitVertex(); - + EndPrimitive(); } diff --git a/shaders/vertex_frame.glsl b/shaders/vertex_frame.glsl index 9666e273..9175c552 100644 --- a/shaders/vertex_frame.glsl +++ b/shaders/vertex_frame.glsl @@ -2,11 +2,11 @@ layout(location = 0) in vec3 in_position; -out vec2 UV; +out vec2 uv; void main() { gl_Position = vec4(in_position, 1); - UV = ( vec2(in_position.x, in_position.y) + vec2(1) ) / 2; + uv = (vec2(in_position.x, in_position.y) + vec2(1)) / 2; } diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index d22d290e..0396a7c6 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -15,7 +15,7 @@ void main() { vs_position = scale * in_position + in_translation; vs_normal = in_normal; - + gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1); } diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index b5aff81b..3261663e 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -1,8 +1,7 @@ #include "raytracing/raytracing.h" -#include -#include #include +#include // geometry processing and shape analysis framework @@ -25,10 +24,10 @@ bool raytracing::rt_restart(const size_t & w, const size_t & h) { if(width * height < w * h) { - delete [] img; - width = w; height = h; + + delete [] img; img = new glm::vec4[width * height]; return true; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index a86d67b0..ee7f5316 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -4,7 +4,6 @@ #include "util.h" -#include #include #include diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 5c60f51e..cdf5bcff 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -3,7 +3,6 @@ #ifdef GPROSHAN_OPTIX #include -#include #include #include diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 481b6c2d..7871201f 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -58,6 +58,8 @@ void frame::display(const int & width, const int & height, void * buffer) { program.enable(); + glViewport(0, 0, width, height); + glBindVertexArray(vao); glActiveTexture(GL_TEXTURE0); diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 362d16c1..52b735d6 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -4,7 +4,6 @@ #include #include -#include #include diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 17c5cdaf..30e566ed 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -2,13 +2,11 @@ #include #include -#include -#include +#include +#include #include #include -#include #include -#include #include #include @@ -100,7 +98,6 @@ bool viewer::run() glm::vec3(up[1], up[2], up[3]) ); - // RENDER glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { @@ -182,7 +179,6 @@ bool viewer::run() ImGui::End(); - // Rendering ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); @@ -279,8 +275,12 @@ void viewer::init_menus() add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); add_process(GLFW_KEY_F7, {"F7", "Render Wireframe Fill", set_render_wireframe_fill}); add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); +#ifdef GPROSHAN_EMBREE add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); +#endif // GPROSHAN_EMBREE +#ifdef GPROSHAN_OPTIX add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); +#endif // GPROSHAN_OPTIX add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); sub_menus.push_back("Mesh"); @@ -492,7 +492,6 @@ bool viewer::menu_save_load_view(viewer * view) return true; } - bool viewer::menu_reset_mesh(viewer * view) { view->active_mesh().selected.clear(); @@ -618,16 +617,34 @@ bool viewer::set_render_gl(viewer * view) return false; } +#ifdef GPROSHAN_EMBREE bool viewer::set_render_embree(viewer * view) { -#ifdef GPROSHAN_EMBREE + static int rt_opt = 0; + static double time = 0; if(!view->rt_embree) { - ImGui::InputFloat("disk radius", &rt::embree::pc_radius, 0, 0, "%.4f"); + ImGui::Combo("rt opt", &rt_opt, "Mesh\0Splat\0\0"); + ImGui::InputFloat("pc radius", &rt::embree::pc_radius, 0, 0, "%.4f"); if(ImGui::Button("Start")) + { view->render_opt = R_EMBREE; + + TIC(time); + switch(rt_opt) + { + case 0: view->rt_embree = new rt::embree({view->active_mesh()}); + break; + case 1: view->rt_embree = new rt::embree_splat({view->active_mesh()}, true); + break; + } + TOC(time); + + if(!view->render_frame) + view->render_frame = new frame; + } } else { @@ -635,7 +652,7 @@ bool viewer::set_render_embree(viewer * view) ImGui::LabelText("disk radius", "%.4f", rt::embree::pc_radius); - if(ImGui::Button("Reset")) + if(ImGui::Button("Restart")) { delete view->rt_embree; view->rt_embree = nullptr; @@ -645,18 +662,17 @@ bool viewer::set_render_embree(viewer * view) } return true; - -#endif // GPROSHAN_EMBREE - - return false; } +#endif // GPROSHAN_EMBREE +#ifdef GPROSHAN_OPTIX bool viewer::set_render_optix(viewer * view) { view->render_opt = 2; return false; } +#endif // GPROSHAN_OPTIX bool viewer::set_render_pointcloud(viewer * view) { @@ -716,12 +732,9 @@ bool viewer::set_render_flat(viewer * view) return false; } +#ifdef GPROSHAN_EMBREE bool viewer::raycasting(viewer * view) { -#ifdef GPROSHAN_EMBREE - - gproshan_log(VIEWER); - rt::embree rc({view->active_mesh()}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), @@ -736,10 +749,9 @@ bool viewer::raycasting(viewer * view) delete [] frame; -#endif // GPROSHAN_EMBREE - return false; } +#endif // GPROSHAN_EMBREE void viewer::render_gl() { @@ -795,37 +807,22 @@ void viewer::render_gl() draw_selected_vertices(shader_sphere); } +#ifdef GPROSHAN_EMBREE void viewer::render_embree() { -#ifdef GPROSHAN_EMBREE - - if(!rt_embree) - { - double time_build_embree; - TIC(time_build_embree); - - rt_embree = new rt::embree_splat({active_mesh()}, render_pointcloud); - - TOC(time_build_embree); - gproshan_log_var(time_build_embree); - } - rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, - render_flat, action ); + render_flat, action + ); action = false; - - if(!render_frame) render_frame = new frame; render_frame->display(viewport_width, viewport_height, rt_embree->img); - -#endif // GPROSHAN_EMBREE } +#endif // GPROSHAN_EMBREE +#ifdef GPROSHAN_OPTIX void viewer::render_optix() { -#ifdef GPROSHAN_OPTIX - if(!rt_optix) { double time_build_optix; @@ -841,12 +838,9 @@ void viewer::render_optix() view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); action = false; - - if(!render_frame) render_frame = new frame; render_frame->display(viewport_width, viewport_height, rt_optix->img); - -#endif // GPROSHAN_OPTIX } +#endif // GPROSHAN_OPTIX void viewer::draw_meshes(shader & program, const bool & normals) { From ddc581ab4363fb10ea61a6652fe4283695fe4ae1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 7 Apr 2021 23:50:55 +0200 Subject: [PATCH 0491/1018] fix embre optix defines --- src/viewer/viewer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 30e566ed..49833916 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -102,8 +102,12 @@ bool viewer::run() switch(render_opt) { case R_GL: render_gl(); break; + #ifdef GPROSHAN_EMBREE case R_EMBREE: render_embree(); break; + #endif // GPROSHAN_EMBREE + #ifdef GPROSHAN_OPTIX case R_OPTIX: render_optix(); break; + #endif // GPROSHAN_OPTIX } ImGui_ImplOpenGL3_NewFrame(); @@ -277,11 +281,11 @@ void viewer::init_menus() add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); #ifdef GPROSHAN_EMBREE add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); + add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); #endif // GPROSHAN_EMBREE #ifdef GPROSHAN_OPTIX add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); #endif // GPROSHAN_OPTIX - add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); sub_menus.push_back("Mesh"); add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); From 69598a88c4594f276d442c48d8d1a09299c24ab4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Apr 2021 14:48:16 +0200 Subject: [PATCH 0492/1018] embree pick vertex implementation --- include/raytracing/raytracing.h | 1 + include/raytracing/rt_embree.h | 2 ++ include/raytracing/rt_optix.h | 2 ++ include/viewer/che_viewer.h | 3 +++ include/viewer/viewer.h | 2 +- src/mesh/che.cpp | 1 - src/raytracing/rt_embree.cpp | 23 +++++++++++++++++++++++ src/raytracing/rt_optix.cpp | 5 +++++ src/viewer/che_viewer.cpp | 26 ++++++++++++++++++++++++++ src/viewer/viewer.cpp | 4 ++-- 10 files changed, 65 insertions(+), 4 deletions(-) diff --git a/include/raytracing/raytracing.h b/include/raytracing/raytracing.h index c0cffdaf..b0254880 100644 --- a/include/raytracing/raytracing.h +++ b/include/raytracing/raytracing.h @@ -56,6 +56,7 @@ class raytracing const index_t & samples = 4 ); + virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir) = 0; protected: virtual glm::vec4 intersect_li( const glm::vec3 & org, diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index 51d15a7f..b6b6bde3 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -45,6 +45,8 @@ class embree : public raytracing embree(const std::vector & meshes, const bool & pointcloud = false); virtual ~embree(); + virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); + protected: bool intersect(ray_hit & r); bool occluded(ray_hit & r); diff --git a/include/raytracing/rt_optix.h b/include/raytracing/rt_optix.h index 6c1cdf1b..0e994d8c 100644 --- a/include/raytracing/rt_optix.h +++ b/include/raytracing/rt_optix.h @@ -30,6 +30,8 @@ class optix : public raytracing optix(const std::vector & meshes); ~optix(); + virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); + private: glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 31189b9a..9de02802 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -3,6 +3,7 @@ #include "mesh/che.h" #include "viewer/shader.h" +#include "raytracing/raytracing.h" #include "viewer/include_opengl.h" @@ -24,6 +25,7 @@ class che_viewer { protected: che * mesh = nullptr; + rt::raytracing * pick_vertex = nullptr; size_t n_instances = 0; bool normalize = false; @@ -52,6 +54,7 @@ class che_viewer void translate(const vertex & p); void invert_orientation(); + void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat); void log_info(); }; diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 52a9b464..762a25f5 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -183,7 +183,7 @@ class viewer void draw_selected_vertices(shader & program); void select_border_vertices(); - void pick_vertex(int x, int y); + void pick_vertex(const real_t & x, const real_t & y); }; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index d336bccf..99fab67a 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -300,7 +300,6 @@ vertex che::color(const index_t & v) const vertex che::shading_color(const index_t & f, const float & u, const float & v, const float & w) const { index_t he = f * che::mtrig; - return VC ? u * color(VT[he]) + v * color(VT[he + 1]) + w * color(VT[he + 2]) : rgb_t(); } diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index ee7f5316..ce2692e3 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -94,6 +94,29 @@ embree::~embree() rtcReleaseDevice(device); } +index_t embree::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) +{ + ray_hit r(org, dir); + if(!intersect(r)) return NIL; + + index_t he = che::mtrig * r.hit.primID; + float w = 1 - r.hit.u - r.hit.v; + + if(w < r.hit.u) + { + he = che::mtrig * r.hit.primID + 1; + w = r.hit.u; + } + + if(w < r.hit.v) + { + he = che::mtrig * r.hit.primID + 2; + w = r.hit.v; + } + + return geomID_mesh[r.hit.geomID]->vt(he); +} + bool embree::intersect(ray_hit & r) { rtcIntersect1(scene, &intersect_context, &r); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index cdf5bcff..13f31936 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -84,6 +84,11 @@ optix::~optix() { } +index_t optix::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) +{ + return NIL; +} + OptixTraversableHandle optix::build_as(const std::vector & meshes) { OptixTraversableHandle optix_as_handle = {}; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 98f65562..54bc748c 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -4,6 +4,11 @@ #include #include +#ifdef GPROSHAN_EMBREE + #include "raytracing/rt_embree.h" +#endif // GPROSHAN_EMBREE + + using namespace std; @@ -71,6 +76,12 @@ void che_viewer::update() mesh->update_normals(); update_vbo(); + + +#ifdef GPROSHAN_EMBREE + delete pick_vertex; + pick_vertex = new rt::embree({mesh}); +#endif // GPROSHAN_EMBREE } void che_viewer::update_vbo() @@ -184,6 +195,21 @@ void che_viewer::invert_orientation() mesh->normal(v) = -mesh->normal(v); } +void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat) +{ + if(!pick_vertex) return; + + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + glm::vec2 screen = glm::vec2(float(x) / windows_size.x, float(windows_size.y - y) / windows_size.y); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + index_t v = pick_vertex->cast_ray(cam_pos, glm::normalize(p - cam_pos)); + if(v != NIL) selected.push_back(v); +} + void che_viewer::log_info() { if(!mesh) return; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 49833916..818d767c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -899,9 +899,9 @@ void viewer::select_border_vertices() mesh.selected.push_back(mesh->vt(he)); } -void viewer::pick_vertex(int, int) +void viewer::pick_vertex(const real_t & x, const real_t & y) { - gproshan_log(VIEWER); + active_mesh().select(x, y, {viewport_width, viewport_height}, view_mat, proj_mat); } From 29ff997bbae40e1c0664bab6abdfe5414687f16a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Apr 2021 16:23:37 +0200 Subject: [PATCH 0493/1018] pick vertex for point clouds --- include/raytracing/rt_embree.h | 1 + src/raytracing/rt_embree.cpp | 43 +++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/include/raytracing/rt_embree.h b/include/raytracing/rt_embree.h index b6b6bde3..1466419b 100644 --- a/include/raytracing/rt_embree.h +++ b/include/raytracing/rt_embree.h @@ -29,6 +29,7 @@ class embree : public raytracing glm::vec3 dir() const; glm::vec3 color(const rt_mesh & mesh) const; glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const; + index_t closest_vertex(const rt_mesh & mesh) const; glm::vec3 position() const; }; diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index ce2692e3..cc24952e 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -61,6 +61,28 @@ glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const return glm::normalize(glm::vec3(n.x, n.y, n.z)); } +index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const +{ + if(mesh.pointcloud) return hit.primID; + + index_t he = che::mtrig * hit.primID; + float w = 1 - hit.u - hit.v; + + if(w < hit.u) + { + he = che::mtrig * hit.primID + 1; + w = hit.u; + } + + if(w < hit.v) + { + he = che::mtrig * hit.primID + 2; + w = hit.v; + } + + return mesh->vt(he); +} + glm::vec3 embree::ray_hit::position() const { return org() + ray.tfar * dir(); @@ -72,7 +94,7 @@ void embree_error(void *, RTCError, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -float embree::pc_radius = 0.001; +float embree::pc_radius = 0.005; embree::embree() { @@ -97,24 +119,7 @@ embree::~embree() index_t embree::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) { ray_hit r(org, dir); - if(!intersect(r)) return NIL; - - index_t he = che::mtrig * r.hit.primID; - float w = 1 - r.hit.u - r.hit.v; - - if(w < r.hit.u) - { - he = che::mtrig * r.hit.primID + 1; - w = r.hit.u; - } - - if(w < r.hit.v) - { - he = che::mtrig * r.hit.primID + 2; - w = r.hit.v; - } - - return geomID_mesh[r.hit.geomID]->vt(he); + return intersect(r) ? r.closest_vertex(geomID_mesh[r.hit.geomID]) : NIL; } bool embree::intersect(ray_hit & r) From 28b747e5b93efa7bd4bdae6caefa99598d7665e6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Apr 2021 16:31:05 +0200 Subject: [PATCH 0494/1018] add definition GPROSHAN_LOG, GPROSHAN_FLOAT --- CMakeLists.txt | 2 ++ include/config.h | 11 --------- include/include.h | 6 ++--- include/include_arma.h | 2 +- include/viewer/che_viewer.h | 2 +- include/viewer/viewer.h | 4 ++-- src/geodesics/geodesics_ptp.cu | 6 ++--- src/geodesics/geodesics_ptp_coalescence.cu | 2 +- src/geodesics/heat_method.cu | 24 +++++++++---------- src/geodesics/test_geodesics_ptp.cpp | 14 +++++------ src/geodesics/test_geodesics_ptp.cu | 2 +- .../test_geodesics_ptp_coalescence.cu | 2 +- src/raytracing/rt_optix.cpp | 4 ++-- 13 files changed, 35 insertions(+), 46 deletions(-) delete mode 100644 include/config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d311fc3..ae075713 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,8 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) +add_definitions(-DGPROSHAN_FLOAT) +add_definitions(-DGPROSHAN_LOG) find_package(CUDAToolkit 11) if(CUDAToolkit_FOUND) diff --git a/include/config.h b/include/config.h deleted file mode 100644 index 9db7cafa..00000000 --- a/include/config.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -// define to compile gproshan with single precision (float) -#define SINGLE_P - -// print log messages -#define LOG - -#endif - diff --git a/include/include.h b/include/include.h index 8a13b068..328a4bd4 100644 --- a/include/include.h +++ b/include/include.h @@ -1,8 +1,6 @@ #ifndef INCLUDE_H #define INCLUDE_H -#include "config.h" - #include #include #include @@ -18,7 +16,7 @@ namespace gproshan { typedef unsigned int index_t; -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT typedef float real_t; #else typedef double real_t; @@ -28,7 +26,7 @@ typedef unsigned int index_t; #define tmp_file_path(file) (std::string(GPROSHAN_DIR) + "/tmp/" + file) #define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) -#ifdef LOG +#ifdef GPROSHAN_LOG #define gproshan_log_var(vari) std::cerr << "\033[0;33m[LOG] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << std::endl #define gproshan_log(message) fprintf(stderr, "\033[1;31m[LOG] %s: %s\n\033[0m", #message, __FUNCTION__) #else diff --git a/include/include_arma.h b/include/include_arma.h index 968af97d..c4c8d608 100644 --- a/include/include_arma.h +++ b/include/include_arma.h @@ -14,7 +14,7 @@ namespace gproshan { -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT typedef arma::fmat a_mat; typedef arma::fvec a_vec; typedef arma::frowvec a_rowvec; diff --git a/include/viewer/che_viewer.h b/include/viewer/che_viewer.h index 9de02802..d674d0a5 100644 --- a/include/viewer/che_viewer.h +++ b/include/viewer/che_viewer.h @@ -8,7 +8,7 @@ #include "viewer/include_opengl.h" -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT #define glVertex3v(x) glVertex3fv(x) #define GL_REAL GL_FLOAT #else diff --git a/include/viewer/viewer.h b/include/viewer/viewer.h index 762a25f5..b92104b5 100644 --- a/include/viewer/viewer.h +++ b/include/viewer/viewer.h @@ -20,11 +20,11 @@ #include "imgui_impl_opengl3.h" -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT #define ImGui_InputReal ImGui::InputFloat #else #define ImGui_InputReal ImGui::InputDouble -#endif // SINGLE_P +#endif // GPROSHAN_FLOAT #define N_MESHES 12 diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index e887924c..f7f84928 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -135,7 +135,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, samples, limits, sorted_index, d_sorted, d_error); // 1 indexing - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #else cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); @@ -290,7 +290,7 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ { index_t v = sorted ? sorted[i] : i; - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT error[i] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; #else error[i] = fabs(new_dist[v] - old_dist[v]) / old_dist[v]; @@ -332,7 +332,7 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT real_t p = delta + sqrtf(dis); #else real_t p = delta + sqrt(dis); diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index 06c49d61..eb93efb9 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -246,7 +246,7 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) if(dis >= 0) { - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT p = delta + sqrtf(dis); #else p = delta + sqrt(dis); diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index 21c876df..ac46e37e 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -66,7 +66,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t if(host) { - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); #else cusolverSpDcsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); @@ -78,7 +78,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); cusolverStatus_t status; - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT status = cusolverSpScsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); #else status = cusolverSpDcsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); @@ -166,7 +166,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t cusparseCreateCsrsv2Info(&info_L); cusparseCreateCsrsv2Info(&info_Lt); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusparseScsric02_bufferSize(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, &buffer_size_M); cusparseScsrsv2_bufferSize(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, &buffer_size_L); cusparseScsrsv2_bufferSize(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, &buffer_size_Lt); @@ -179,7 +179,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t buffer_size = max(buffer_size_M, max(buffer_size_L, buffer_size_Lt)); cudaMalloc(&buffer, buffer_size); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusparseScsric02_analysis(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); #else cusparseDcsric02_analysis(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); @@ -187,7 +187,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t if(CUSPARSE_STATUS_ZERO_PIVOT == cusparseXcsric02_zeroPivot(handle, info_M, &structural_zero)) printf("A(%d,%d) is missing\n", structural_zero, structural_zero); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusparseScsrsv2_analysis(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, policy_L, buffer); cusparseScsrsv2_analysis(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, policy_Lt, buffer); @@ -205,7 +205,7 @@ double solve_positive_definite_cusparse(const int m, const int nnz, const real_t // SOLVE cudaEventRecord(start, 0); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusparseScsrsv2_solve(handle, trans_L, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, data.b, dy, policy_L, buffer); cusparseScsrsv2_solve(handle, trans_Lt, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, dy, data.x, policy_Lt, buffer); #else @@ -280,7 +280,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons cusolverSpXcsrcholAnalysisHost(cusolver_handle, m, nnz, descr, hA_col_ptrs, hA_row_indices, info); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholBufferInfoHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, &size_iternal, &size_chol); #else cusolverSpDcsrcholBufferInfoHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, &size_iternal, &size_chol); @@ -288,7 +288,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons buffer = new char[size_chol]; - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholFactorHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, buffer); cusolverSpScsrcholZeroPivotHost(cusolver_handle, info, 0, &singularity); #else @@ -300,7 +300,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons // SOLVE cudaEventRecord(start, 0); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); #else cusolverSpDcsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); @@ -324,7 +324,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons cusolverSpXcsrcholAnalysis(cusolver_handle, m, nnz, descr, data.A_col_ptrs, data.A_row_indices, info); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholBufferInfo(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, &size_iternal, &size_chol); #else cusolverSpDcsrcholBufferInfo(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, &size_iternal, &size_chol); @@ -332,7 +332,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons cudaMalloc(&buffer, size_chol); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholFactor(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, buffer); cusolverSpScsrcholZeroPivot(cusolver_handle, info, 0, &singularity); #else @@ -345,7 +345,7 @@ double solve_positive_definite_cusolver_preview(const int m, const int nnz, cons // SOLVE cudaEventRecord(start, 0); - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cusolverSpScsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); #else cusolverSpDcsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 834bb987..c91c44db 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -27,7 +27,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) int n_test = nargs == 5 ? atoi(args[4]) : 10; -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT FILE * ftable = fopen("ptp_results.tex", "w"); #else FILE *ftable = fopen("ptp_results_double.tex", "w"); @@ -72,7 +72,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) Time[2] = INFINITY; #endif // GPROSHAN_CUDA - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT Time[6] = Time[5] = Time[4] = Time[3] = INFINITY; Error[4] = Error[3] = INFINITY; #else @@ -113,18 +113,18 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fprintf(ftable, pspeedup, Time[0] / Time[1]); fprintf(ftable, pberror, str[1 == e_min], Error[1]); - #ifndef SINGLE_P + #ifndef GPROSHAN_FLOAT fprintf(ftable, "& OpenMP "); #endif - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT // PTP GPU fprintf(ftable, pbtime, str[2 == t_min], Time[2]); fprintf(ftable, pspeedup, Time[0] / Time[2]); fprintf(ftable, pberror, str[2 == e_min], Error[2]); #endif - #ifndef SINGLE_P + #ifndef GPROSHAN_FLOAT // HEAT FLOW cholmod fprintf(ftable, ptime, Time[4]); fprintf(ftable, pbtime, str[3 == t_min], Time[3]); @@ -134,7 +134,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #endif fprintf(ftable, "\\\\\n"); - #ifndef SINGLE_P + #ifndef GPROSHAN_FLOAT // PTP GPU fprintf(ftable, "&&& "); fprintf(ftable, pbtime, str[2 == t_min], Time[2]); @@ -199,7 +199,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) system(("mv band " + (test_path + filename + ".band")).c_str()); - #ifndef SINGLE_P + #ifndef GPROSHAN_FLOAT os.open(test_path + filename + "_error_double.iter"); #else os.open(test_path + filename + "_error.iter"); diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index c2f77adc..fe0ba11b 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -123,7 +123,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, samples, limits, sorted_index, d_sorted, d_error); // 1 indexing - #ifdef SINGLE_P + #ifdef GPROSHAN_FLOAT cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); #else cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index 70208177..e25109fa 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -172,7 +172,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices, d_dist[d], 1, &f); #else cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 13f31936..a3fe8ee6 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -176,7 +176,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, void * d_index; -#ifdef SINGLE_P +#ifdef GPROSHAN_FLOAT cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(vertex)); cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); #else @@ -190,7 +190,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); delete [] vertices; -#endif // SINGLE_P +#endif // GPROSHAN_FLOAT cudaMalloc(&d_index, mesh->n_half_edges * sizeof(index_t)); cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t), cudaMemcpyHostToDevice); From cd1547b992702c39178bf170536da510fbb41678 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Apr 2021 16:41:55 +0200 Subject: [PATCH 0495/1018] move include to include/gproshan --- CMakeLists.txt | 2 +- include/{ => gproshan}/app_viewer.h | 0 include/{ => gproshan}/features/descriptor.h | 0 include/{ => gproshan}/features/key_components.h | 0 include/{ => gproshan}/features/key_points.h | 0 include/{ => gproshan}/features/shot.h | 0 include/{ => gproshan}/geodesics/che.cuh | 0 include/{ => gproshan}/geodesics/dijkstra.h | 0 include/{ => gproshan}/geodesics/geodesics.h | 0 include/{ => gproshan}/geodesics/geodesics_ptp.cuh | 0 include/{ => gproshan}/geodesics/geodesics_ptp.h | 0 include/{ => gproshan}/geodesics/geodesics_ptp_coalescence.cuh | 0 include/{ => gproshan}/geodesics/heat_method.h | 0 include/{ => gproshan}/geodesics/sampling.h | 0 include/{ => gproshan}/geodesics/test_geodesics_ptp.cuh | 0 include/{ => gproshan}/geodesics/test_geodesics_ptp.h | 0 .../{ => gproshan}/geodesics/test_geodesics_ptp_coalescence.cuh | 0 include/{ => gproshan}/geodesics/vertex.cuh | 0 include/{ => gproshan}/geometry/convex_hull.h | 0 include/{ => gproshan}/include.h | 0 include/{ => gproshan}/include_arma.h | 0 include/{ => gproshan}/laplacian/fairing.h | 0 include/{ => gproshan}/laplacian/fairing_spectral.h | 0 include/{ => gproshan}/laplacian/fairing_taubin.h | 0 include/{ => gproshan}/laplacian/laplacian.h | 0 include/{ => gproshan}/mdict/basis.h | 0 include/{ => gproshan}/mdict/basis_cosine.h | 0 include/{ => gproshan}/mdict/basis_dct.h | 0 include/{ => gproshan}/mdict/image_denoising.h | 0 include/{ => gproshan}/mdict/mdict.h | 0 include/{ => gproshan}/mdict/msparse_coding.h | 0 include/{ => gproshan}/mdict/patch.h | 0 include/{ => gproshan}/mesh/che.h | 0 include/{ => gproshan}/mesh/che_fill_hole.h | 0 include/{ => gproshan}/mesh/che_img.h | 0 include/{ => gproshan}/mesh/che_obj.h | 0 include/{ => gproshan}/mesh/che_off.h | 0 include/{ => gproshan}/mesh/che_ply.h | 0 include/{ => gproshan}/mesh/che_poisson.h | 0 include/{ => gproshan}/mesh/che_ptx.h | 0 include/{ => gproshan}/mesh/che_sphere.h | 0 include/{ => gproshan}/mesh/che_xyz.h | 0 include/{ => gproshan}/mesh/kdtree.h | 0 include/{ => gproshan}/mesh/quaternion.h | 0 include/{ => gproshan}/mesh/simplification.h | 0 include/{ => gproshan}/mesh/vertex.h | 0 include/{ => gproshan}/raytracing/raytracing.h | 0 include/{ => gproshan}/raytracing/rt_embree.h | 0 include/{ => gproshan}/raytracing/rt_embree_splat.h | 0 include/{ => gproshan}/raytracing/rt_optix.h | 0 include/{ => gproshan}/util.h | 0 include/{ => gproshan}/viewer/camera.h | 0 include/{ => gproshan}/viewer/che_viewer.h | 0 include/{ => gproshan}/viewer/frame.h | 0 include/{ => gproshan}/viewer/include_opengl.h | 0 include/{ => gproshan}/viewer/shader.h | 0 include/{ => gproshan}/viewer/viewer.h | 0 57 files changed, 1 insertion(+), 1 deletion(-) rename include/{ => gproshan}/app_viewer.h (100%) rename include/{ => gproshan}/features/descriptor.h (100%) rename include/{ => gproshan}/features/key_components.h (100%) rename include/{ => gproshan}/features/key_points.h (100%) rename include/{ => gproshan}/features/shot.h (100%) rename include/{ => gproshan}/geodesics/che.cuh (100%) rename include/{ => gproshan}/geodesics/dijkstra.h (100%) rename include/{ => gproshan}/geodesics/geodesics.h (100%) rename include/{ => gproshan}/geodesics/geodesics_ptp.cuh (100%) rename include/{ => gproshan}/geodesics/geodesics_ptp.h (100%) rename include/{ => gproshan}/geodesics/geodesics_ptp_coalescence.cuh (100%) rename include/{ => gproshan}/geodesics/heat_method.h (100%) rename include/{ => gproshan}/geodesics/sampling.h (100%) rename include/{ => gproshan}/geodesics/test_geodesics_ptp.cuh (100%) rename include/{ => gproshan}/geodesics/test_geodesics_ptp.h (100%) rename include/{ => gproshan}/geodesics/test_geodesics_ptp_coalescence.cuh (100%) rename include/{ => gproshan}/geodesics/vertex.cuh (100%) rename include/{ => gproshan}/geometry/convex_hull.h (100%) rename include/{ => gproshan}/include.h (100%) rename include/{ => gproshan}/include_arma.h (100%) rename include/{ => gproshan}/laplacian/fairing.h (100%) rename include/{ => gproshan}/laplacian/fairing_spectral.h (100%) rename include/{ => gproshan}/laplacian/fairing_taubin.h (100%) rename include/{ => gproshan}/laplacian/laplacian.h (100%) rename include/{ => gproshan}/mdict/basis.h (100%) rename include/{ => gproshan}/mdict/basis_cosine.h (100%) rename include/{ => gproshan}/mdict/basis_dct.h (100%) rename include/{ => gproshan}/mdict/image_denoising.h (100%) rename include/{ => gproshan}/mdict/mdict.h (100%) rename include/{ => gproshan}/mdict/msparse_coding.h (100%) rename include/{ => gproshan}/mdict/patch.h (100%) rename include/{ => gproshan}/mesh/che.h (100%) rename include/{ => gproshan}/mesh/che_fill_hole.h (100%) rename include/{ => gproshan}/mesh/che_img.h (100%) rename include/{ => gproshan}/mesh/che_obj.h (100%) rename include/{ => gproshan}/mesh/che_off.h (100%) rename include/{ => gproshan}/mesh/che_ply.h (100%) rename include/{ => gproshan}/mesh/che_poisson.h (100%) rename include/{ => gproshan}/mesh/che_ptx.h (100%) rename include/{ => gproshan}/mesh/che_sphere.h (100%) rename include/{ => gproshan}/mesh/che_xyz.h (100%) rename include/{ => gproshan}/mesh/kdtree.h (100%) rename include/{ => gproshan}/mesh/quaternion.h (100%) rename include/{ => gproshan}/mesh/simplification.h (100%) rename include/{ => gproshan}/mesh/vertex.h (100%) rename include/{ => gproshan}/raytracing/raytracing.h (100%) rename include/{ => gproshan}/raytracing/rt_embree.h (100%) rename include/{ => gproshan}/raytracing/rt_embree_splat.h (100%) rename include/{ => gproshan}/raytracing/rt_optix.h (100%) rename include/{ => gproshan}/util.h (100%) rename include/{ => gproshan}/viewer/camera.h (100%) rename include/{ => gproshan}/viewer/che_viewer.h (100%) rename include/{ => gproshan}/viewer/frame.h (100%) rename include/{ => gproshan}/viewer/include_opengl.h (100%) rename include/{ => gproshan}/viewer/shader.h (100%) rename include/{ => gproshan}/viewer/viewer.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae075713..3c94bd57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,7 +77,7 @@ target_link_libraries(imgui GLEW::GLEW) target_link_libraries(imgui glfw) -include_directories(${gproshan_SOURCE_DIR}/include) +include_directories(${gproshan_SOURCE_DIR}/include/gproshan) add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") diff --git a/include/app_viewer.h b/include/gproshan/app_viewer.h similarity index 100% rename from include/app_viewer.h rename to include/gproshan/app_viewer.h diff --git a/include/features/descriptor.h b/include/gproshan/features/descriptor.h similarity index 100% rename from include/features/descriptor.h rename to include/gproshan/features/descriptor.h diff --git a/include/features/key_components.h b/include/gproshan/features/key_components.h similarity index 100% rename from include/features/key_components.h rename to include/gproshan/features/key_components.h diff --git a/include/features/key_points.h b/include/gproshan/features/key_points.h similarity index 100% rename from include/features/key_points.h rename to include/gproshan/features/key_points.h diff --git a/include/features/shot.h b/include/gproshan/features/shot.h similarity index 100% rename from include/features/shot.h rename to include/gproshan/features/shot.h diff --git a/include/geodesics/che.cuh b/include/gproshan/geodesics/che.cuh similarity index 100% rename from include/geodesics/che.cuh rename to include/gproshan/geodesics/che.cuh diff --git a/include/geodesics/dijkstra.h b/include/gproshan/geodesics/dijkstra.h similarity index 100% rename from include/geodesics/dijkstra.h rename to include/gproshan/geodesics/dijkstra.h diff --git a/include/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h similarity index 100% rename from include/geodesics/geodesics.h rename to include/gproshan/geodesics/geodesics.h diff --git a/include/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh similarity index 100% rename from include/geodesics/geodesics_ptp.cuh rename to include/gproshan/geodesics/geodesics_ptp.cuh diff --git a/include/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h similarity index 100% rename from include/geodesics/geodesics_ptp.h rename to include/gproshan/geodesics/geodesics_ptp.h diff --git a/include/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh similarity index 100% rename from include/geodesics/geodesics_ptp_coalescence.cuh rename to include/gproshan/geodesics/geodesics_ptp_coalescence.cuh diff --git a/include/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h similarity index 100% rename from include/geodesics/heat_method.h rename to include/gproshan/geodesics/heat_method.h diff --git a/include/geodesics/sampling.h b/include/gproshan/geodesics/sampling.h similarity index 100% rename from include/geodesics/sampling.h rename to include/gproshan/geodesics/sampling.h diff --git a/include/geodesics/test_geodesics_ptp.cuh b/include/gproshan/geodesics/test_geodesics_ptp.cuh similarity index 100% rename from include/geodesics/test_geodesics_ptp.cuh rename to include/gproshan/geodesics/test_geodesics_ptp.cuh diff --git a/include/geodesics/test_geodesics_ptp.h b/include/gproshan/geodesics/test_geodesics_ptp.h similarity index 100% rename from include/geodesics/test_geodesics_ptp.h rename to include/gproshan/geodesics/test_geodesics_ptp.h diff --git a/include/geodesics/test_geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh similarity index 100% rename from include/geodesics/test_geodesics_ptp_coalescence.cuh rename to include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh diff --git a/include/geodesics/vertex.cuh b/include/gproshan/geodesics/vertex.cuh similarity index 100% rename from include/geodesics/vertex.cuh rename to include/gproshan/geodesics/vertex.cuh diff --git a/include/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h similarity index 100% rename from include/geometry/convex_hull.h rename to include/gproshan/geometry/convex_hull.h diff --git a/include/include.h b/include/gproshan/include.h similarity index 100% rename from include/include.h rename to include/gproshan/include.h diff --git a/include/include_arma.h b/include/gproshan/include_arma.h similarity index 100% rename from include/include_arma.h rename to include/gproshan/include_arma.h diff --git a/include/laplacian/fairing.h b/include/gproshan/laplacian/fairing.h similarity index 100% rename from include/laplacian/fairing.h rename to include/gproshan/laplacian/fairing.h diff --git a/include/laplacian/fairing_spectral.h b/include/gproshan/laplacian/fairing_spectral.h similarity index 100% rename from include/laplacian/fairing_spectral.h rename to include/gproshan/laplacian/fairing_spectral.h diff --git a/include/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h similarity index 100% rename from include/laplacian/fairing_taubin.h rename to include/gproshan/laplacian/fairing_taubin.h diff --git a/include/laplacian/laplacian.h b/include/gproshan/laplacian/laplacian.h similarity index 100% rename from include/laplacian/laplacian.h rename to include/gproshan/laplacian/laplacian.h diff --git a/include/mdict/basis.h b/include/gproshan/mdict/basis.h similarity index 100% rename from include/mdict/basis.h rename to include/gproshan/mdict/basis.h diff --git a/include/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h similarity index 100% rename from include/mdict/basis_cosine.h rename to include/gproshan/mdict/basis_cosine.h diff --git a/include/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h similarity index 100% rename from include/mdict/basis_dct.h rename to include/gproshan/mdict/basis_dct.h diff --git a/include/mdict/image_denoising.h b/include/gproshan/mdict/image_denoising.h similarity index 100% rename from include/mdict/image_denoising.h rename to include/gproshan/mdict/image_denoising.h diff --git a/include/mdict/mdict.h b/include/gproshan/mdict/mdict.h similarity index 100% rename from include/mdict/mdict.h rename to include/gproshan/mdict/mdict.h diff --git a/include/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h similarity index 100% rename from include/mdict/msparse_coding.h rename to include/gproshan/mdict/msparse_coding.h diff --git a/include/mdict/patch.h b/include/gproshan/mdict/patch.h similarity index 100% rename from include/mdict/patch.h rename to include/gproshan/mdict/patch.h diff --git a/include/mesh/che.h b/include/gproshan/mesh/che.h similarity index 100% rename from include/mesh/che.h rename to include/gproshan/mesh/che.h diff --git a/include/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h similarity index 100% rename from include/mesh/che_fill_hole.h rename to include/gproshan/mesh/che_fill_hole.h diff --git a/include/mesh/che_img.h b/include/gproshan/mesh/che_img.h similarity index 100% rename from include/mesh/che_img.h rename to include/gproshan/mesh/che_img.h diff --git a/include/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h similarity index 100% rename from include/mesh/che_obj.h rename to include/gproshan/mesh/che_obj.h diff --git a/include/mesh/che_off.h b/include/gproshan/mesh/che_off.h similarity index 100% rename from include/mesh/che_off.h rename to include/gproshan/mesh/che_off.h diff --git a/include/mesh/che_ply.h b/include/gproshan/mesh/che_ply.h similarity index 100% rename from include/mesh/che_ply.h rename to include/gproshan/mesh/che_ply.h diff --git a/include/mesh/che_poisson.h b/include/gproshan/mesh/che_poisson.h similarity index 100% rename from include/mesh/che_poisson.h rename to include/gproshan/mesh/che_poisson.h diff --git a/include/mesh/che_ptx.h b/include/gproshan/mesh/che_ptx.h similarity index 100% rename from include/mesh/che_ptx.h rename to include/gproshan/mesh/che_ptx.h diff --git a/include/mesh/che_sphere.h b/include/gproshan/mesh/che_sphere.h similarity index 100% rename from include/mesh/che_sphere.h rename to include/gproshan/mesh/che_sphere.h diff --git a/include/mesh/che_xyz.h b/include/gproshan/mesh/che_xyz.h similarity index 100% rename from include/mesh/che_xyz.h rename to include/gproshan/mesh/che_xyz.h diff --git a/include/mesh/kdtree.h b/include/gproshan/mesh/kdtree.h similarity index 100% rename from include/mesh/kdtree.h rename to include/gproshan/mesh/kdtree.h diff --git a/include/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h similarity index 100% rename from include/mesh/quaternion.h rename to include/gproshan/mesh/quaternion.h diff --git a/include/mesh/simplification.h b/include/gproshan/mesh/simplification.h similarity index 100% rename from include/mesh/simplification.h rename to include/gproshan/mesh/simplification.h diff --git a/include/mesh/vertex.h b/include/gproshan/mesh/vertex.h similarity index 100% rename from include/mesh/vertex.h rename to include/gproshan/mesh/vertex.h diff --git a/include/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h similarity index 100% rename from include/raytracing/raytracing.h rename to include/gproshan/raytracing/raytracing.h diff --git a/include/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h similarity index 100% rename from include/raytracing/rt_embree.h rename to include/gproshan/raytracing/rt_embree.h diff --git a/include/raytracing/rt_embree_splat.h b/include/gproshan/raytracing/rt_embree_splat.h similarity index 100% rename from include/raytracing/rt_embree_splat.h rename to include/gproshan/raytracing/rt_embree_splat.h diff --git a/include/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h similarity index 100% rename from include/raytracing/rt_optix.h rename to include/gproshan/raytracing/rt_optix.h diff --git a/include/util.h b/include/gproshan/util.h similarity index 100% rename from include/util.h rename to include/gproshan/util.h diff --git a/include/viewer/camera.h b/include/gproshan/viewer/camera.h similarity index 100% rename from include/viewer/camera.h rename to include/gproshan/viewer/camera.h diff --git a/include/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h similarity index 100% rename from include/viewer/che_viewer.h rename to include/gproshan/viewer/che_viewer.h diff --git a/include/viewer/frame.h b/include/gproshan/viewer/frame.h similarity index 100% rename from include/viewer/frame.h rename to include/gproshan/viewer/frame.h diff --git a/include/viewer/include_opengl.h b/include/gproshan/viewer/include_opengl.h similarity index 100% rename from include/viewer/include_opengl.h rename to include/gproshan/viewer/include_opengl.h diff --git a/include/viewer/shader.h b/include/gproshan/viewer/shader.h similarity index 100% rename from include/viewer/shader.h rename to include/gproshan/viewer/shader.h diff --git a/include/viewer/viewer.h b/include/gproshan/viewer/viewer.h similarity index 100% rename from include/viewer/viewer.h rename to include/gproshan/viewer/viewer.h From 1e24ee07f2f2456397a9626136c12a3fdd32f92e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Apr 2021 18:31:14 +0200 Subject: [PATCH 0496/1018] upgrading multiplicate vertices --- src/mesh/che.cpp | 152 ++++++++++------------------------------------- 1 file changed, 32 insertions(+), 120 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 99fab67a..1f64c9da 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -673,141 +673,53 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector Date: Thu, 8 Apr 2021 20:11:54 +0200 Subject: [PATCH 0497/1018] simplification: cleaning up, starting over --- include/gproshan/mesh/che.h | 25 +-- include/gproshan/mesh/simplification.h | 6 +- src/app_viewer.cpp | 2 +- src/mesh/che.cpp | 245 +------------------------ src/mesh/simplification.cpp | 78 +------- 5 files changed, 9 insertions(+), 347 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index d6fe7fc0..9b6ed535 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -23,7 +23,6 @@ index_t trig(const index_t & he); index_t next(const index_t & he); index_t prev(const index_t & he); -struct corr_t; class che { @@ -100,7 +99,6 @@ class che vertex gradient_he(const index_t & he, const real_t *const & f) const; vertex gradient(const index_t & v, const real_t *const & f); vertex barycenter(const index_t & t) const; - vertex corr_vertex(corr_t & corr) const; real_t cotan(const index_t & he) const; real_t mean_edge() const; size_t memory() const; @@ -136,8 +134,7 @@ class che void merge(const che * mesh, const std::vector & com_vertices); void set_head_vertices(index_t * head, const size_t & n); index_t link_intersect(const index_t & v_a, const index_t & v_b); - corr_t * edge_collapse(const index_t *const & sort_edges, const vertex *const & normals); - corr_t find_corr(const vertex & v, const vertex & n, const std::vector & triangles); + void edge_collapse(const index_t *const & sort_edges); protected: void init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); @@ -172,26 +169,6 @@ struct CHE CHE(che * mesh); }; -struct corr_t -{ - index_t t; - vertex alpha; - - corr_t() - { - t = NIL; - } - - void init(const index_t & he) - { - t = trig(he) * che::mtrig; - alpha[0] = he == t; - alpha[1] = he == next(t); - alpha[2] = he == prev(t); - t /= che::mtrig; - } -}; - } // namespace gproshan diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 4c1fb8e0..4efcb6e9 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -16,16 +16,14 @@ class simplification private: a_mat * Q; che * mesh; - corr_t * corr; index_t levels; public: - simplification(che * mesh, const vertex *const & normals, const index_t & levels_ = 1); + simplification(che * mesh, const index_t & levels_ = 1); ~simplification(); - operator const corr_t * (); private: - void execute(const vertex *const & normals); + void execute(); void compute_quadrics(); real_t compute_error(const index_t & e); void order_edges(index_t * const & sort_edges, real_t * const & error_edges); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index e53c24c1..30fa5e29 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -193,7 +193,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) index_t levels; cin >> levels; - TIC(view->time) simplification sampling(mesh, &mesh->normal(0), levels); TOC(view->time) + TIC(view->time) simplification sampling(mesh, levels); TOC(view->time) gproshan_debug_var(view->time); //if(view->n_meshes < 2) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 1f64c9da..39f5f9ba 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -415,13 +415,6 @@ vertex che::barycenter(const index_t & t) const return bc / che::mtrig; } -vertex che::corr_vertex(corr_t & corr) const -{ - index_t he = corr.t * che::mtrig; - assert(he < n_half_edges); - return corr.alpha[0] * gt_vt(he) + corr.alpha[1] * gt_vt(next(he)) + corr.alpha[2] * gt_vt(prev(he)); -} - real_t che::cotan(const index_t & he) const { if(he == NIL) return 0; @@ -994,244 +987,8 @@ index_t che::link_intersect(const index_t & v_a, const index_t & v_b) return intersect; } -corr_t * che::edge_collapse(const index_t *const & sort_edges, const vertex *const & normals) -{ - if(n_faces < 2) return nullptr; - - // init default corr - corr_t * corr = new corr_t[n_vertices]; - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - corr[v].init(EVT[v]); - - short * faces_fixed = new short[n_faces]; - memset(faces_fixed, 0, sizeof(short) * n_faces); - index_t * deleted_vertices = new index_t[n_vertices]; - memset(deleted_vertices, 0, sizeof(index_t) * n_vertices); - - index_t e_d, he_d, ohe_d, va, vb; - vertex aux_va, aux_vb; - - // update corr to vertex opposite to edge - auto update_corr_v = [this, &corr](const index_t & he) - { - index_t v = VT[prev(he)]; - if(trig(EVT[v]) == trig(he)) - { - EVT[v] = OT[next(he)]; - corr[v].init(EVT[v]); - } - }; - - bool is_collapse; - - for(index_t e = 0; e < n_edges; ++e) - { - //e_d = ne; - e_d = sort_edges ? sort_edges[e] : rand() % n_edges; - assert(e_d < n_edges); - - he_d = ET[e_d]; - ohe_d = OT[he_d]; - va = VT[he_d]; - vb = VT[next(he_d)]; - - //is_border_v(va) && is_border_v(vb) -> is_border_e(e_d) - if( !faces_fixed[trig(he_d)] && (ohe_d != NIL ? !faces_fixed[trig(ohe_d)] : true) && - (!(is_vertex_bound(va) && is_vertex_bound(vb)) || is_edge_bound(e_d)) ) - { - is_collapse = link_intersect(va, vb) == (1 + (ohe_d != NIL)); - - if(is_collapse) - for_star(he, this, va) - if(faces_fixed[trig(he)]) - { - is_collapse = false; - break; - } - - if(is_collapse) - for_star(he, this, vb) - if(faces_fixed[trig(he)]) - { - is_collapse = false; - break; - } - - if(is_collapse) - { - update_corr_v(he_d); - if(ohe_d != NIL) - update_corr_v(ohe_d); - - for_star(he, this, va) - if(!faces_fixed[trig(he)]) faces_fixed[trig(he)] = 1; - - for_star(he, this, vb) - if(!faces_fixed[trig(he)]) faces_fixed[trig(he)] = 1; - - faces_fixed[trig(he_d)] = -1; - if(ohe_d != NIL) faces_fixed[trig(ohe_d)] = -1; - for_star(he, this, vb) - VT[he] = va; - - deleted_vertices[vb] = 1; - - aux_va = GT[va]; - aux_vb = GT[vb]; - GT[va] = GT[vb] = (GT[va] + GT[vb]) / 2; - - vector he_trigs; - for_star(he, this, va) - if(faces_fixed[trig(he)] > -1) - he_trigs.push_back(trig(he) * che::mtrig); - for_star(he, this, vb) - if(faces_fixed[trig(he)] > -1) - he_trigs.push_back(trig(he) * che::mtrig); - - gproshan_debug_var(va); - corr[va] = find_corr(aux_va, normals[va], he_trigs); - gproshan_debug_var(vb); - corr[vb] = find_corr(aux_vb, normals[vb], he_trigs); - - EVT[vb] = NIL; - } - } - } - - vector new_vertices; - vector new_faces; - new_vertices.reserve(n_vertices); - new_faces.reserve(n_faces); - - index_t dv = 0; - for(index_t v = 0; v < n_vertices; ++v) - { - if(deleted_vertices[v]) ++dv; - else - { - deleted_vertices[v] = dv; - new_vertices.push_back(GT[v]); - } - } - - index_t * map_he = new index_t[n_half_edges]; - memset(map_he, 255, sizeof(index_t) * n_half_edges); - - for(index_t n_he = 0, he = 0; he < n_half_edges; ++he) - if(faces_fixed[trig(he)] > -1) - { - new_faces.push_back(VT[he] - deleted_vertices[VT[he]]); - map_he[he] = n_he++; - } - - for(index_t v = 0; v < n_vertices; ++v) - corr[v].t = trig(map_he[corr[v].t * che::mtrig]); - - free(); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); - - delete [] faces_fixed; - delete [] deleted_vertices; - delete [] map_he; - - return corr; -} - -corr_t che::find_corr(const vertex & v, const vertex & n, const vector & he_trigs) +void che::edge_collapse(const index_t *const & sort_edges) { - real_t d, dist = INFINITY; - corr_t corr, corr_d; - - a_mat A(4, 4, arma::fill::ones); - a_vec x(4); - x(0) = v.x; x(1) = v.y; x(2) = v.z; x(3) = 1; - - a_vec alpha(&corr.alpha.x, 3, false, true); - a_vec a; - vertex aux; - - auto update_dist = [&]() - { - if(d < dist) - { - dist = d; - corr_d = corr; - } - }; - - for(const index_t & he: he_trigs) - { - corr.t = trig(he); - - aux = gt_vt(he); - A(0, 0) = aux.x; A(1, 0) = aux.y; A(2, 0) = aux.z; - - aux = gt_vt(next(he)); - A(0, 1) = aux.x; A(1, 1) = aux.y; A(2, 1) = aux.z; - - aux = gt_vt(prev(he)); - A(0, 2) = aux.x; A(1, 2) = aux.y; A(2, 2) = aux.z; - - A(0, 3) = -n.x; - A(1, 3) = -n.y; - A(2, 3) = -n.z; - A(3, 3) = 0; - - if(solve(a, A, x, arma::solve_opts::no_approx)) - { - gproshan_debug_var(a); - if(all(a >= 0) && sum(a.head(3)) == 1) - { - alpha = a.head(3); - d = 0; - update_dist(); - } - else - { - x = A.submat(0, 0, 3, 2) * alpha; - auto dist_to_edge = [&](const index_t & i, const index_t & j) - { - a_mat B = A.cols(i, j); - a = solve(B, x); - d = norm(x - B * a); - - corr.alpha = 0; - - if(all(a >= 0) && sum(a.head(2)) == 1) - { - corr.alpha[i] = a(0); - corr.alpha[j] = a(1); - update_dist(); - } - else - { - corr.alpha[i] = 1; - d = norm(x - A.col(i)); - update_dist(); - - corr.alpha[i] = 0; - corr.alpha[j] = 1; - d = norm(x - A.col(j)); - update_dist(); - } - }; - - dist_to_edge(0, 1); - dist_to_edge(1, 2); - dist_to_edge(0, 2); - } - } - } - - if(corr_d.t == NIL) - { - gproshan_debug_var(n); - gproshan_debug_var(x); - gproshan_debug_var(A); - } - - return corr_d; } void che::init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) diff --git a/src/mesh/simplification.cpp b/src/mesh/simplification.cpp index 9ffe1bbd..7f8af946 100644 --- a/src/mesh/simplification.cpp +++ b/src/mesh/simplification.cpp @@ -1,5 +1,6 @@ #include "mesh/simplification.h" + using namespace std; @@ -7,94 +8,23 @@ using namespace std; namespace gproshan { -simplification::simplification(che * mesh_, const vertex *const & normals, const index_t & levels_) +simplification::simplification(che * mesh_, const index_t & levels_) { mesh = mesh_; levels = levels_; Q = new a_mat[mesh->n_vertices]; - execute(normals); + execute(); } simplification::~simplification() { if(Q) delete [] Q; - if(corr) delete [] corr; } -simplification::operator const corr_t * () -{ - return corr; -} - -void simplification::execute(const vertex *const & normals) +void simplification::execute() { compute_quadrics(); - - const size_t n_vertices = mesh->n_vertices; - index_t * sort_edges = new index_t[mesh->n_edges]; - real_t * error_edges = new real_t[mesh->n_edges]; - vertex * corr_v = new vertex[n_vertices]; - index_t * corr_i = new index_t[n_vertices * che::mtrig]; - - corr_t * corr_aux; - index_t he, vi; - vertex a, b, c; - - auto add_he_trigs = [this](vector & he_trigs, const corr_t & c) - { - const index_t he = c.t * che::mtrig; - for_star(t_he, mesh, mesh->vt(he)) - he_trigs.push_back(t_he); - for_star(t_he, mesh, mesh->vt(next(he))) - he_trigs.push_back(t_he); - for_star(t_he, mesh, mesh->vt(prev(he))) - he_trigs.push_back(t_he); - }; - - order_edges(sort_edges, error_edges); - corr = mesh->edge_collapse(sort_edges, normals); - - while(--levels) - { - #pragma omp parallel for private(he, vi) - for(index_t v = 0; v < n_vertices; ++v) - { - corr_v[v] = mesh->corr_vertex(corr[v]); - he = corr[v].t * che::mtrig; - vi = v * che::mtrig; - corr_i[vi] = mesh->vt(he); - corr_i[vi + 1] = mesh->vt(next(he)); - corr_i[vi + 2] = mesh->vt(prev(he)); - } - - order_edges(sort_edges, error_edges); - corr_aux = mesh->edge_collapse(sort_edges, normals); - - #pragma omp parallel for private(vi, a, b, c) - for(index_t v = 0; v < n_vertices; ++v) - { - vi = v * che::mtrig; - a = mesh->corr_vertex(corr_aux[corr_i[vi]]); - b = mesh->corr_vertex(corr_aux[corr_i[vi + 1]]); - c = mesh->corr_vertex(corr_aux[corr_i[vi + 2]]); - corr_v[v] = corr[v].alpha[0] * a + corr[v].alpha[1] * b + corr[v].alpha[2] * c; - - vector he_trigs; - add_he_trigs(he_trigs, corr_aux[corr_i[vi]]); - add_he_trigs(he_trigs, corr_aux[corr_i[vi + 1]]); - add_he_trigs(he_trigs, corr_aux[corr_i[vi + 2]]); - - corr[v] = mesh->find_corr(corr_v[v], normals[v], he_trigs); - } - - delete [] corr_aux; - } - - delete [] sort_edges; - delete [] error_edges; - delete [] corr_v; - delete [] corr_i; } void simplification::compute_quadrics() From 696e66722247a79315a4e2824267899500378621 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Apr 2021 17:02:34 +0200 Subject: [PATCH 0498/1018] show mesh info viewer --- src/viewer/viewer.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 818d767c..a08188ca 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -119,7 +119,7 @@ bool viewer::run() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((to_string(i) + ". " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + if(ImGui::MenuItem((to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; sphere_translations.clear(); @@ -157,10 +157,23 @@ bool viewer::run() ImGui::EndMainMenuBar(); } + ImGui::SetNextWindowSize(ImVec2(window_width, -1)); + ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); + ImGui::Begin("status gproshan", nullptr, ImGuiWindowFlags_NoTitleBar); + ImGui::Text("github.com/larc/gproshan"); + ImGui::End(); + ImGui::SetNextWindowSize(ImVec2(320, -1)); - ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); + ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_Once); ImGui::Begin("gproshan"); + ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); + + che_viewer & mesh = active_mesh(); + ImGui::Text("%s", mesh->filename.c_str()); + ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); + ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); + if(render_pointcloud) { ImGui::Checkbox("point_normals", &point_normals); @@ -627,11 +640,11 @@ bool viewer::set_render_embree(viewer * view) static int rt_opt = 0; static double time = 0; + ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); + ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); + if(!view->rt_embree) { - ImGui::Combo("rt opt", &rt_opt, "Mesh\0Splat\0\0"); - ImGui::InputFloat("pc radius", &rt::embree::pc_radius, 0, 0, "%.4f"); - if(ImGui::Button("Start")) { view->render_opt = R_EMBREE; @@ -654,8 +667,6 @@ bool viewer::set_render_embree(viewer * view) { view->render_opt = R_EMBREE; - ImGui::LabelText("disk radius", "%.4f", rt::embree::pc_radius); - if(ImGui::Button("Restart")) { delete view->rt_embree; From 3e0e2c86d87f22c8c3f86fba35b754e06249c59d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Apr 2021 21:36:14 +0200 Subject: [PATCH 0499/1018] viewer: add status_message char [1024] set using ssprintf --- include/gproshan/viewer/viewer.h | 2 ++ src/viewer/viewer.cpp | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index b92104b5..ebe7baff 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -113,6 +113,8 @@ class viewer std::vector vectors; std::vector sub_menus; + char status_message[1024] = {}; + public: viewer(int width = 1600, int height = 900); virtual ~viewer(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index a08188ca..f1fdfc75 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -129,7 +129,7 @@ bool viewer::run() ImGui::EndMenu(); } - if(ImGui::BeginMenu("Colormap")) + if(ImGui::BeginMenu("Color")) { for(index_t i = 0; i < colormap.size(); ++i) if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == idx_colormap, i != idx_colormap)) @@ -160,6 +160,8 @@ bool viewer::run() ImGui::SetNextWindowSize(ImVec2(window_width, -1)); ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); ImGui::Begin("status gproshan", nullptr, ImGuiWindowFlags_NoTitleBar); + ImGui::Text("[] %s", status_message); + ImGui::SameLine(window_width - 180); ImGui::Text("github.com/larc/gproshan"); ImGui::End(); @@ -194,6 +196,7 @@ bool viewer::run() } } + ImGui::PopItemWidth(); ImGui::End(); ImGui::Render(); @@ -398,8 +401,12 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureKeyboard) return; - if(view->processes[key].function) - view->processes[key].selected = !view->processes[key].selected; + process_t & pro = view->processes[key]; + if(pro.function) + { + pro.selected = !pro.selected; + sprintf(view->status_message, "%s", pro.selected ? pro.name.c_str() : ""); + } } void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) @@ -458,11 +465,9 @@ bool viewer::menu_save_load_view(viewer * view) { filesystem::create_directory(tmp_file_path("views/")); - static char file[128] = "new_view"; ImGui::InputText("##savefile", file, sizeof(file)); - ImGui::SameLine(); if(ImGui::Button("Save")) @@ -472,7 +477,6 @@ bool viewer::menu_save_load_view(viewer * view) os.close(); } - static index_t select = 0; static vector vfiles; @@ -505,7 +509,6 @@ bool viewer::menu_save_load_view(viewer * view) is.close(); } - return true; } @@ -565,6 +568,8 @@ bool viewer::menu_save_mesh(viewer * view) case 3: che_xyz::write_file(mesh, file, vertex_color); break; } + + sprintf(view->status_message, "file '%s' saved.", file); } return true; From a1541b66ecafa5384ff917e60132e52405334ab5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Apr 2021 21:50:40 +0200 Subject: [PATCH 0500/1018] viewer: update status message select menu --- src/viewer/viewer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index f1fdfc75..e2e567f8 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -145,9 +145,9 @@ bool viewer::run() for(auto & p: processes) { process_t & pro = p.second; - if(pro.function != nullptr && pro.sub_menu == i) - ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected); + if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) + sprintf(status_message, "%s", pro.selected ? pro.name.c_str() : ""); } ImGui::EndMenu(); From 538f7f2e4fe64b71defd7be91b92f39bd3ee1cf6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Apr 2021 22:39:46 +0200 Subject: [PATCH 0501/1018] define macro glm_vec3 in vertex.h --- include/gproshan/mesh/vertex.h | 3 +++ src/mesh/quaternion.cpp | 10 +++++----- src/viewer/viewer.cpp | 23 ++++++++++------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h index 00e53aa4..6c2072d9 100644 --- a/include/gproshan/mesh/vertex.h +++ b/include/gproshan/mesh/vertex.h @@ -6,6 +6,9 @@ #include +#define glm_vec3(v) glm::vec3((v)[0], (v)[1], (v)[2]) + + // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/mesh/quaternion.cpp b/src/mesh/quaternion.cpp index 30c08594..c0361a1b 100644 --- a/src/mesh/quaternion.cpp +++ b/src/mesh/quaternion.cpp @@ -35,20 +35,20 @@ const quaternion & quaternion::operator = (const vertex & _v) real_t & quaternion::operator [] (int index) { - return (&s)[index]; + return v[index]; } const real_t & quaternion::operator [] (int index) const { - return (&s)[index]; + return v[index]; } void quaternion::toMatrix(real_t Q[4][4]) const { Q[0][0] = s; Q[0][1] = -v.x; Q[0][2] = -v.y; Q[0][3] = -v.z; - Q[1][0] = v.x; Q[1][1] = s; Q[1][2] = -v.z; Q[1][3] = v.y; - Q[2][0] = v.y; Q[2][1] = v.z; Q[2][2] = s; Q[2][3] = -v.x; - Q[3][0] = v.z; Q[3][1] = -v.y; Q[3][2] = v.x; Q[3][3] = s; + Q[1][0] = v.x; Q[1][1] = s; Q[1][2] = -v.z; Q[1][3] = v.y; + Q[2][0] = v.y; Q[2][1] = v.z; Q[2][2] = s; Q[2][3] = -v.x; + Q[3][0] = v.z; Q[3][1] = -v.y; Q[3][2] = v.x; Q[3][3] = s; } real_t & quaternion::re() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e2e567f8..e111c12b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -83,7 +83,7 @@ bool viewer::run() center = vertex(0., 0., 0.); up = vertex(0., 1., 0.); - light = vertex(-1., 1., -2.); + light = vertex(-1., 1., -2.); quaternion r = cam.current_rotation(); @@ -93,10 +93,7 @@ bool viewer::run() proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); - view_mat = glm::lookAt( glm::vec3(eye[1], eye[2], eye[3]), - glm::vec3(center[1], center[2], center[3]), - glm::vec3(up[1], up[2], up[3]) - ); + view_mat = glm::lookAt(glm_vec3(eye), glm_vec3(center), glm_vec3(up)); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) @@ -775,15 +772,15 @@ bool viewer::raycasting(viewer * view) void viewer::render_gl() { - glProgramUniform3f(shader_sphere, shader_sphere("eye"), eye[1], eye[2], eye[3]); - glProgramUniform3f(shader_sphere, shader_sphere("light"), light[1], light[2], light[3]); + glProgramUniform3f(shader_sphere, shader_sphere("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_sphere, shader_sphere("light"), light[0], light[1], light[2]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), idx_colormap); - glProgramUniform3f(shader_triangles, shader_triangles("eye"), eye[1], eye[2], eye[3]); - glProgramUniform3f(shader_triangles, shader_triangles("light"), light[1], light[2], light[3]); + glProgramUniform3f(shader_triangles, shader_triangles("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); glProgramUniform1i(shader_triangles, shader_triangles("render_flat"), render_flat); glProgramUniform1i(shader_triangles, shader_triangles("render_lines"), render_lines); glProgramUniform1i(shader_triangles, shader_triangles("render_wireframe"), render_wireframe_fill); @@ -791,8 +788,8 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), idx_colormap); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[1], eye[2], eye[3]); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[1], light[2], light[3]); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1i(shader_pointcloud, shader_pointcloud("point_normals"), point_normals); @@ -831,7 +828,7 @@ void viewer::render_gl() void viewer::render_embree() { rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, + view_mat, proj_mat, {glm_vec3(light)}, render_flat, action ); @@ -855,7 +852,7 @@ void viewer::render_optix() } rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm::vec3(light[1], light[2], light[3])}, action); + view_mat, proj_mat, {glm_vec3(light)}, action); action = false; render_frame->display(viewport_width, viewport_height, rt_optix->img); From cdd5f19287734f6b0e8602e419492199ba4bc3cc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Apr 2021 23:00:58 +0200 Subject: [PATCH 0502/1018] using glm_vec3 --- src/raytracing/rt_embree.cpp | 8 ++++---- src/raytracing/rt_embree_splat.cpp | 6 +++--- src/raytracing/rt_optix.cpp | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index cc24952e..6f53926a 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -49,7 +49,7 @@ glm::vec3 embree::ray_hit::color(const rt_mesh & mesh) const { const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm::vec3(c.x, c.y, c.z); + return glm_vec3(c); } glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const @@ -58,7 +58,7 @@ glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm::normalize(glm::vec3(n.x, n.y, n.z)); + return glm::normalize(glm_vec3(n)); } index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const @@ -210,8 +210,8 @@ index_t embree::add_pointcloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - pxyzr[i] = glm::vec4(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z, pc_radius); - normal[i] = glm::vec3(mesh->normal(i).x, mesh->normal(i).y, mesh->normal(i).z); + pxyzr[i] = glm::vec4(glm_vec3(mesh->gt(i)), pc_radius); + normal[i] = glm_vec3(mesh->normal(i)); } rtcCommitGeometry(geom); diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp index c15b5645..63a04d6f 100644 --- a/src/raytracing/rt_embree_splat.cpp +++ b/src/raytracing/rt_embree_splat.cpp @@ -128,9 +128,9 @@ void embree_splat::init_splats(const che * mesh) points.erase(u); - s.P[j] = glm::vec3(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z); - s.N[j] = glm::vec3(mesh->normal(u).x, mesh->normal(u).y, mesh->normal(u).z); - s.C[j] = glm::vec3(mesh->color(u).x, mesh->color(u).y, mesh->color(u).z); + s.P[j] = glm_vec3(mesh->gt(u)); + s.N[j] = glm_vec3(mesh->normal(u)); + s.C[j] = glm_vec3(mesh->color(u)); } s.radio = glm::length(s.P[K - 1] - s.P[0]); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index a3fe8ee6..cb7f419d 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -185,7 +185,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) - vertices[i] = glm::vec3(mesh->gt(i).x, mesh->gt(i).y, mesh->gt(i).z); + vertices[i] = glm_vec3(mesh->gt(i)); cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); From 3fed0689e8bc8b93e4fe8f10f03aafda2694c60b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 14 Apr 2021 14:31:14 +0200 Subject: [PATCH 0503/1018] che: fix copy constructor rgb color array --- src/mesh/che.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 39f5f9ba..51ebb008 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -64,7 +64,7 @@ che::che(const che & mesh) memcpy(ET, mesh.ET, n_edges * sizeof(index_t)); memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); - memcpy(VC, mesh.VC, n_vertices * sizeof(vertex)); + memcpy(VC, mesh.VC, n_vertices * sizeof(rgb_t)); memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); } From c06c571d5db6af3cb98df6d4309362e5f571f00b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 16 Apr 2021 14:17:31 +0200 Subject: [PATCH 0504/1018] geodesics: remove cusparse heat method code deprecated on cuda 11.3 --- include/gproshan/geodesics/heat_method.h | 2 +- src/geodesics/heat_method.cu | 130 ----------------------- 2 files changed, 1 insertion(+), 131 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 71f8fc67..0e8ae72c 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -45,7 +45,7 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t /// device only, incomplete cholesky factorization /// https://docs.nvidia.com/cuda/cusparse/index.html#cusparse-lt-t-gt-csric02 -double solve_positive_definite_cusparse(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx); +// REMOVED double solve_positive_definite_cusparse(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx); /// host and device support /// using cusolverSp_LOWLEVEL_PREVIEW.h library diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index ac46e37e..a5425497 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -107,136 +107,6 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t return (double) time / 1000; } -double solve_positive_definite_cusparse(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx) -{ - cudaDeviceReset(); - - float time; - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - - // allocate A, x, b into device - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); - - // aux vector y to device - real_t * dy; - cudaMalloc(&dy, m * sizeof(real_t)); - - cusparseHandle_t handle; - cusparseCreate(&handle); - - // SOLVE Ax = b - - cusparseMatDescr_t descr_M = 0; - cusparseMatDescr_t descr_L = 0; - - csric02Info_t info_M = 0; - csrsv2Info_t info_L = 0; - csrsv2Info_t info_Lt = 0; - - int buffer_size_M; - int buffer_size_L; - int buffer_size_Lt; - int buffer_size; - - void * buffer = 0; - - int structural_zero; - int numerical_zero; - - const real_t alpha = 1.; - const cusparseSolvePolicy_t policy_M = CUSPARSE_SOLVE_POLICY_NO_LEVEL; - const cusparseSolvePolicy_t policy_L = CUSPARSE_SOLVE_POLICY_NO_LEVEL; - const cusparseSolvePolicy_t policy_Lt = CUSPARSE_SOLVE_POLICY_USE_LEVEL; - const cusparseOperation_t trans_L = CUSPARSE_OPERATION_NON_TRANSPOSE; - const cusparseOperation_t trans_Lt = CUSPARSE_OPERATION_TRANSPOSE; - - cusparseCreateMatDescr(&descr_M); - cusparseSetMatIndexBase(descr_M, CUSPARSE_INDEX_BASE_ZERO); - cusparseSetMatType(descr_M, CUSPARSE_MATRIX_TYPE_GENERAL); - - cusparseCreateMatDescr(&descr_L); - cusparseSetMatIndexBase(descr_L, CUSPARSE_INDEX_BASE_ZERO); - cusparseSetMatType(descr_L, CUSPARSE_MATRIX_TYPE_GENERAL); - cusparseSetMatFillMode(descr_L, CUSPARSE_FILL_MODE_LOWER); - cusparseSetMatDiagType(descr_L, CUSPARSE_DIAG_TYPE_NON_UNIT); - - cusparseCreateCsric02Info(&info_M); - cusparseCreateCsrsv2Info(&info_L); - cusparseCreateCsrsv2Info(&info_Lt); - - #ifdef GPROSHAN_FLOAT - cusparseScsric02_bufferSize(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, &buffer_size_M); - cusparseScsrsv2_bufferSize(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, &buffer_size_L); - cusparseScsrsv2_bufferSize(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, &buffer_size_Lt); - #else - cusparseDcsric02_bufferSize(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, &buffer_size_M); - cusparseDcsrsv2_bufferSize(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, &buffer_size_L); - cusparseDcsrsv2_bufferSize(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, &buffer_size_Lt); - #endif - - buffer_size = max(buffer_size_M, max(buffer_size_L, buffer_size_Lt)); - cudaMalloc(&buffer, buffer_size); - - #ifdef GPROSHAN_FLOAT - cusparseScsric02_analysis(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); - #else - cusparseDcsric02_analysis(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); - #endif - if(CUSPARSE_STATUS_ZERO_PIVOT == cusparseXcsric02_zeroPivot(handle, info_M, &structural_zero)) - printf("A(%d,%d) is missing\n", structural_zero, structural_zero); - - #ifdef GPROSHAN_FLOAT - cusparseScsrsv2_analysis(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, policy_L, buffer); - cusparseScsrsv2_analysis(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, policy_Lt, buffer); - - cusparseScsric02(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); - #else - cusparseDcsrsv2_analysis(handle, trans_L, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, policy_L, buffer); - cusparseDcsrsv2_analysis(handle, trans_Lt, m, nnz, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, policy_Lt, buffer); - - cusparseDcsric02(handle, m, nnz, descr_M, data.A_values, data.A_col_ptrs, data.A_row_indices, info_M, policy_M, buffer); - #endif - if(CUSPARSE_STATUS_ZERO_PIVOT == cusparseXcsric02_zeroPivot(handle, info_M, &numerical_zero)) - printf("L(%d,%d) is zero\n", numerical_zero, numerical_zero); - - - // SOLVE - cudaEventRecord(start, 0); - - #ifdef GPROSHAN_FLOAT - cusparseScsrsv2_solve(handle, trans_L, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, data.b, dy, policy_L, buffer); - cusparseScsrsv2_solve(handle, trans_Lt, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, dy, data.x, policy_Lt, buffer); - #else - cusparseDcsrsv2_solve(handle, trans_L, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_L, data.b, dy, policy_L, buffer); - cusparseDcsrsv2_solve(handle, trans_Lt, m, nnz, &alpha, descr_L, data.A_values, data.A_col_ptrs, data.A_row_indices, info_Lt, dy, data.x, policy_Lt, buffer); - #endif - - // copy sol x to host - cudaMemcpy(hx, data.x, m * sizeof(real_t), cudaMemcpyDeviceToHost); - - // END SOLVE - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - - - // FREE - cudaFree(buffer); - cusparseDestroyMatDescr(descr_M); - cusparseDestroyMatDescr(descr_L); - cusparseDestroyCsric02Info(info_M); - cusparseDestroyCsrsv2Info(info_L); - cusparseDestroyCsrsv2Info(info_Lt); - cusparseDestroy(handle); - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return (double) time / 1000; -} - double solve_positive_definite_cusolver_preview(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host) { cudaDeviceReset(); From 1f9360f743d5bc1ce0b3f4791f5a80274338a215 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 19 Apr 2021 16:29:24 +0200 Subject: [PATCH 0505/1018] geodesics: remove fps radio, updating imgui for geodesics --- include/gproshan/app_viewer.h | 1 - src/app_viewer.cpp | 49 +++++++---------------------------- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 025774d2..47df617a 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -76,7 +76,6 @@ class app_viewer : public viewer static bool process_geodesics_heat_method_gpu(viewer * p_view); #endif // GPROSHAN_CUDA static bool process_farthest_point_sampling(viewer * p_view); - static bool process_farthest_point_sampling_radio(viewer * p_view); static bool process_voronoi(viewer * p_view); static bool process_compute_toplesets(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 30fa5e29..d8de5f24 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -80,7 +80,6 @@ void app_viewer::init() add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_method_gpu}); #endif // GPROSHAN_CUDA add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); - add_process(GLFW_KEY_R, {"R", "Geodesic Farthest Point Sampling (radio)", process_farthest_point_sampling_radio}); add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); add_process(GLFW_KEY_P, {"P", "Toplesets", process_compute_toplesets}); @@ -305,35 +304,34 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & if(dist.size() != mesh->n_vertices) dist.resize(mesh->n_vertices); - geodesics::params params; + static geodesics::params params; params.alg = alg; params.dist_alloc = dist.data(); - - TIC(view->time) + + if(ImGui::Button("Run")) + { + TIC(view->time) geodesics G(mesh, mesh.selected, params); - TOC(view->time) - gproshan_log_var(view->time); - - mesh->update_heatmap(&G[0]); + TOC(view->time) + + mesh->update_heatmap(&G[0]); + } - return false; + return true; } bool app_viewer::process_geodesics_fm(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_geodesics(p_view, geodesics::FM); } bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_geodesics(p_view, geodesics::PTP_CPU); } bool app_viewer::process_geodesics_heat_method(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_geodesics(p_view, geodesics::HEAT_METHOD); } @@ -341,13 +339,11 @@ bool app_viewer::process_geodesics_heat_method(viewer * p_view) bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_geodesics(p_view, geodesics::PTP_GPU); } bool app_viewer::process_geodesics_heat_method_gpu(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_geodesics(p_view, geodesics::HEAT_METHOD_GPU); } @@ -375,31 +371,6 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) return true; } -bool app_viewer::process_farthest_point_sampling_radio(viewer * p_view) -{ - gproshan_log(APP_VIEWER); - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - gproshan_input(radio); - real_t radio; cin >> radio; - -#ifdef GPROSHAN_CUDA // IMPLEMENT/REVIEW - double time_fps; - - TIC(view->time) - radio = farthest_point_sampling_ptp_gpu(mesh, mesh.selected, time_fps, NIL, radio); - TOC(view->time) - gproshan_log_var(time_fps); -#endif // GPROSHAN_CUDA - - gproshan_log_var(radio); - gproshan_log_var(mesh.selected.size()); - gproshan_log_var(view->time); - - return false; -} - bool app_viewer::process_voronoi(viewer * p_view) { gproshan_log(APP_VIEWER); From 13d860e6cbea91ab6c5b96d95906bbf92786ae15 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 19 Apr 2021 17:02:14 +0200 Subject: [PATCH 0506/1018] geodesics: updating imgui select algorithm --- include/gproshan/app_viewer.h | 9 +-- include/gproshan/geodesics/geodesics.h | 6 +- src/app_viewer.cpp | 77 ++++++++++---------------- 3 files changed, 32 insertions(+), 60 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 47df617a..f416f7f9 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -67,14 +67,7 @@ class app_viewer : public viewer static bool process_fairing_spectral(viewer * p_view); // Geodesics - static bool process_geodesics(viewer * p_view, const geodesics::algorithm & alg); - static bool process_geodesics_fm(viewer * p_view); - static bool process_geodesics_ptp_cpu(viewer * p_view); - static bool process_geodesics_heat_method(viewer * p_view); - #ifdef GPROSHAN_CUDA - static bool process_geodesics_ptp_gpu(viewer * p_view); - static bool process_geodesics_heat_method_gpu(viewer * p_view); - #endif // GPROSHAN_CUDA + static bool process_geodesics(viewer * p_view); static bool process_farthest_point_sampling(viewer * p_view); static bool process_voronoi(viewer * p_view); static bool process_compute_toplesets(viewer * p_view); diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index c1075cbd..a020a394 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -24,12 +24,12 @@ class geodesics public: enum algorithm { FM, ///< Execute Fast Marching algorithm + PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm + HEAT_METHOD, ///< Execute Heat Method - cholmod (CPU) #ifdef GPROSHAN_CUDA PTP_GPU, ///< Execute Parallel Toplesets Propagation GPU algorithm - HEAT_METHOD_GPU, ///< Execute Heat Method - cusparse (GPU) + HEAT_METHOD_GPU ///< Execute Heat Method - cusparse (GPU) #endif // GPROSHAN_CUDA - PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm - HEAT_METHOD ///< Execute Heat Method - cholmod (CPU) }; struct params diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index d8de5f24..af8120a6 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -62,26 +62,20 @@ void app_viewer::init() sub_menus.push_back("Geometry"); add_process(GLFW_KEY_H, {"H", "2D Convex Hull", process_convex_hull}); add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); - add_process(GLFW_KEY_8, {"8", "Edge Collapse", process_edge_collapse}); - add_process(GLFW_KEY_9, {"9", "Multiplicate", process_multiplicate_vertices}); + add_process(GLFW_KEY_Q, {"Q", "Edge Collapse", process_edge_collapse}); + add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); sub_menus.push_back("Fairing"); - add_process(GLFW_KEY_T, {"T", "Fairing Taubin", process_fairing_taubin}); + add_process(GLFW_KEY_F, {"F", "Fairing Taubin", process_fairing_taubin}); add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); sub_menus.push_back("Geodesics"); - add_process(GLFW_KEY_F, {"F", "Fast Marching", process_geodesics_fm}); - add_process(GLFW_KEY_C, {"C", "Parallel Toplesets Propagation CPU", process_geodesics_ptp_cpu}); - add_process(GLFW_KEY_M, {"M", "Heat Method", process_geodesics_heat_method}); -#ifdef GPROSHAN_CUDA - add_process(GLFW_KEY_G, {"G", "Parallel Toplesets Propagation GPU", process_geodesics_ptp_gpu}); - add_process(GLFW_KEY_Q, {"Q", "Heat Method GPU", process_geodesics_heat_method_gpu}); -#endif // GPROSHAN_CUDA + add_process(GLFW_KEY_G, {"G", "Geodesics", process_geodesics}); add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); - add_process(GLFW_KEY_P, {"P", "Toplesets", process_compute_toplesets}); + add_process(GLFW_KEY_T, {"T", "Toplesets", process_compute_toplesets}); sub_menus.push_back("Sparse Coding"); add_process(GLFW_KEY_I, {"I", "Mesh Sparse Coding", process_msparse_coding}); @@ -98,17 +92,17 @@ void app_viewer::init() add_process(GLFW_KEY_7, {"7", "Key Components", process_key_components}); sub_menus.push_back("Hole Filling"); - add_process(GLFW_KEY_X, {"X", "Poisson Membrane surface", process_poisson_laplacian_1}); - add_process(GLFW_KEY_Y, {"Y", "Poisson Thin-plate surface", process_poisson_laplacian_2}); - add_process(GLFW_KEY_Z, {"Z", "Poisson Minimum variation surface", process_poisson_laplacian_3}); - add_process(GLFW_KEY_A, {"A", "Fill hole - planar mesh", process_fill_holes}); - add_process(GLFW_KEY_B, {"B", "Fill hole - biharmonic splines", process_fill_holes_biharmonic_splines}); + add_process(GLFW_KEY_X, {"X", "Poisson: Membrane surface", process_poisson_laplacian_1}); + add_process(GLFW_KEY_Y, {"Y", "Poisson: Thin-plate surface", process_poisson_laplacian_2}); + add_process(GLFW_KEY_Z, {"Z", "Poisson: Minimum variation surface", process_poisson_laplacian_3}); + add_process(GLFW_KEY_8, {"8", "Fill hole: planar mesh", process_fill_holes}); + add_process(GLFW_KEY_9, {"0", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines}); sub_menus.push_back("Others"); add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", process_select_multiple}); add_process(GLFW_KEY_SLASH, {"SLASH", "Threshold", process_threshold}); add_process(GLFW_KEY_N, {"N", "Noise", process_noise}); - add_process(GLFW_KEY_COMMA, {"COMMA", "Black noise", process_black_noise}); + add_process(GLFW_KEY_B, {"B", "Black noise", process_black_noise}); } @@ -290,7 +284,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) // Geodesics -bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & alg) +bool app_viewer::process_geodesics(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -303,13 +297,27 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & if(dist.size() != mesh->n_vertices) dist.resize(mesh->n_vertices); + + static size_t n_iter_min = 0; + static size_t n_iter_max; static geodesics::params params; - params.alg = alg; - params.dist_alloc = dist.data(); +#ifdef GPROSHAN_CUDA + ImGui::Combo("alg", (int *) ¶ms.alg, "FM\0PTP_CPU\0HEAT_METHOD\0PTP_GPU\0HEAT_METHOD_GPU\0\0"); +#else + ImGui::Combo("alg", (int *) ¶ms.alg, "FM\0PTP_CPU\0HEAT_METHOD\0\0"); +#endif // GPROSHAN_CUDA + + if(params.alg == geodesics::FM) + { + + } + if(ImGui::Button("Run")) { + params.dist_alloc = dist.data(); + TIC(view->time) geodesics G(mesh, mesh.selected, params); TOC(view->time) @@ -320,35 +328,6 @@ bool app_viewer::process_geodesics(viewer * p_view, const geodesics::algorithm & return true; } -bool app_viewer::process_geodesics_fm(viewer * p_view) -{ - return process_geodesics(p_view, geodesics::FM); -} - -bool app_viewer::process_geodesics_ptp_cpu(viewer * p_view) -{ - return process_geodesics(p_view, geodesics::PTP_CPU); -} - -bool app_viewer::process_geodesics_heat_method(viewer * p_view) -{ - return process_geodesics(p_view, geodesics::HEAT_METHOD); -} - -#ifdef GPROSHAN_CUDA - -bool app_viewer::process_geodesics_ptp_gpu(viewer * p_view) -{ - return process_geodesics(p_view, geodesics::PTP_GPU); -} - -bool app_viewer::process_geodesics_heat_method_gpu(viewer * p_view) -{ - return process_geodesics(p_view, geodesics::HEAT_METHOD_GPU); -} - -#endif // GPROSHAN_CUDA - bool app_viewer::process_farthest_point_sampling(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; From 1367f2f7597f6e5d498603e6534e2123ed1ed9b3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 19 Apr 2021 19:55:03 +0200 Subject: [PATCH 0507/1018] geodesics: added process geodesics to app_viewer --- imgui/imconfig.h | 2 +- include/gproshan/viewer/viewer.h | 2 ++ src/app_viewer.cpp | 32 +++++++++++++------------------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index 39de21c6..f774eaf5 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -27,7 +27,7 @@ //#define IMGUI_API __declspec( dllimport ) //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. -//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS +#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS //---- Disable all of Dear ImGui or don't implement standard windows. // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index ebe7baff..897d998d 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -22,8 +22,10 @@ #ifdef GPROSHAN_FLOAT #define ImGui_InputReal ImGui::InputFloat + #define ImGuiDataType_Real ImGuiDataType_Float #else #define ImGui_InputReal ImGui::InputDouble + #define ImGuiDataType_Real ImGuiDataType_Double #endif // GPROSHAN_FLOAT #define N_MESHES 12 diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index af8120a6..a3b6ad1a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -289,39 +289,33 @@ bool app_viewer::process_geodesics(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - if(!mesh.selected.size()) - mesh.selected.push_back(0); - - static vector dist; - - if(dist.size() != mesh->n_vertices) - dist.resize(mesh->n_vertices); - - static size_t n_iter_min = 0; - static size_t n_iter_max; - static geodesics::params params; - + #ifdef GPROSHAN_CUDA ImGui::Combo("alg", (int *) ¶ms.alg, "FM\0PTP_CPU\0HEAT_METHOD\0PTP_GPU\0HEAT_METHOD_GPU\0\0"); #else ImGui::Combo("alg", (int *) ¶ms.alg, "FM\0PTP_CPU\0HEAT_METHOD\0\0"); #endif // GPROSHAN_CUDA - - if(params.alg == geodesics::FM) - { - } + if(params.alg == geodesics::FM) + ImGui_InputReal("radio", ¶ms.radio); if(ImGui::Button("Run")) { + if(!mesh.selected.size()) + mesh.selected.push_back(0); + + if(dist.size() < mesh->n_vertices) + dist.resize(mesh->n_vertices); + params.dist_alloc = dist.data(); - + TIC(view->time) - geodesics G(mesh, mesh.selected, params); + geodesics G(mesh, mesh.selected, params); TOC(view->time) - + + params.radio = G.radio(); mesh->update_heatmap(&G[0]); } From d145bb5be339224917f1d02e862d40b6586c637b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 22 Apr 2021 21:15:34 +0200 Subject: [PATCH 0508/1018] update CHANGELOG --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0af757ee..69544fb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,14 @@ Version History --------------- ### gproshan 3.0.0 +- Add module geometry, including a 2D convex hull algorithm implementation. +- Supported file mesh types include: off, obj, ply, ptx, xyz, and any depth image or image file loaded as a mesh. +- Upgraded version of geodesics module: fast marching algorithm, heat method, and parallel topleset propagation algorithm from our published paper. - Upgraded version of the sparse mesh coding with feature aware sampling module and published paper reference. - Updated save mesh with options for file types, normals, and point-cloud. -- Added render option using [Embree](https://www.embree.org/) ray tracing. -- Implemented the loading and rendering of point-clouds. +- Added render option using [Embree](https://www.embree.org/) ray tracing mesh, point-cloud disks, and splats. +- Implemented the loading and rendering of point clouds. - Added heatmap viewer options and loading vertex color from a file. - New user interface implemented with [ImGui](https://github.com/ocornut/imgui). - Viewer upgraded using GLEW, GLFW, and GLM. + From 3f340344e4f88f7c251c186fc22cf11a8f147150 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 22 Apr 2021 23:37:55 +0200 Subject: [PATCH 0509/1018] geodesics: cleaning up heat method cuda --- include/gproshan/geodesics/heat_method.h | 10 +- src/geodesics/heat_method.cu | 138 ----------------------- 2 files changed, 1 insertion(+), 147 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 0e8ae72c..a828940f 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -37,20 +37,12 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & m, cholmod_common * context); /// double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b); -#endif // GPROSHAN_CUDA /// host and device support /// https://docs.nvidia.com/cuda/cusolver/index.html#cusolver-lt-t-gt-csrlsvchol double solve_positive_definite_cusolver(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host = 0); -/// device only, incomplete cholesky factorization -/// https://docs.nvidia.com/cuda/cusparse/index.html#cusparse-lt-t-gt-csric02 -// REMOVED double solve_positive_definite_cusparse(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx); - -/// host and device support -/// using cusolverSp_LOWLEVEL_PREVIEW.h library -/// no documentation, code base on cuda/samples/7_CUDALibraries/cuSolverSp_LowlevelCholesky -double solve_positive_definite_cusolver_preview(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host = 0); +#endif // GPROSHAN_CUDA } // namespace gproshan diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index a5425497..6447a5fe 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -3,7 +3,6 @@ #include #include -#include // geometry processing and shape analysis framework @@ -107,143 +106,6 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t return (double) time / 1000; } -double solve_positive_definite_cusolver_preview(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host) -{ - cudaDeviceReset(); - - float time; - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - - // SOLVE Ax = b - - cusolverSpHandle_t cusolver_handle = nullptr; - cusparseHandle_t cusparse_handle = nullptr; -// cudaStream_t stream = nullptr; - - cusparseMatDescr_t descr = nullptr; - - size_t size_iternal = 0; - size_t size_chol = 0; - - void * buffer = nullptr; - - int singularity; - - cusolverSpCreate(&cusolver_handle); - cusparseCreate(&cusparse_handle); -/* - cudaStreamCreate(&stream); - cusolverSpSetStream(cusolver_handle, stream); - cusparseSetStream(cusparse_handle, stream); -*/ - cusparseCreateMatDescr(&descr); - cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL); - cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO); - - - if(host) - { - csrcholInfoHost_t info; - cusolverSpCreateCsrcholInfoHost(&info); - - cusolverSpXcsrcholAnalysisHost(cusolver_handle, m, nnz, descr, hA_col_ptrs, hA_row_indices, info); - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholBufferInfoHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, &size_iternal, &size_chol); - #else - cusolverSpDcsrcholBufferInfoHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, &size_iternal, &size_chol); - #endif - - buffer = new char[size_chol]; - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholFactorHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, buffer); - cusolverSpScsrcholZeroPivotHost(cusolver_handle, info, 0, &singularity); - #else - cusolverSpDcsrcholFactorHost(cusolver_handle, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, info, buffer); - cusolverSpDcsrcholZeroPivotHost(cusolver_handle, info, 0, &singularity); - #endif - assert(singularity == -1); - - // SOLVE - cudaEventRecord(start, 0); - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); - #else - cusolverSpDcsrcholSolveHost(cusolver_handle, m, hb, hx, info, buffer); - #endif - - // END SOLVE - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - - // FREE - delete [] (char*) buffer; - cusolverSpDestroyCsrcholInfoHost(info); - } - else - { - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); - - csrcholInfo_t info; - cusolverSpCreateCsrcholInfo(&info); - - cusolverSpXcsrcholAnalysis(cusolver_handle, m, nnz, descr, data.A_col_ptrs, data.A_row_indices, info); - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholBufferInfo(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, &size_iternal, &size_chol); - #else - cusolverSpDcsrcholBufferInfo(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, &size_iternal, &size_chol); - #endif - - cudaMalloc(&buffer, size_chol); - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholFactor(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, buffer); - cusolverSpScsrcholZeroPivot(cusolver_handle, info, 0, &singularity); - #else - cusolverSpDcsrcholFactor(cusolver_handle, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, info, buffer); - cusolverSpDcsrcholZeroPivot(cusolver_handle, info, 0, &singularity); - #endif - -// assert(singularity == -1); - - // SOLVE - cudaEventRecord(start, 0); - - #ifdef GPROSHAN_FLOAT - cusolverSpScsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); - #else - cusolverSpDcsrcholSolve(cusolver_handle, m, data.b, data.x, info, buffer); - #endif - - // END SOLVE - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - - cudaMemcpy(hx, data.x, m * sizeof(real_t), cudaMemcpyDeviceToHost); - - // FREE - cudaFree(buffer); - cusolverSpDestroyCsrcholInfo(info); - } - -// cudaStreamDestroy(stream); - cusparseDestroyMatDescr(descr); - cusparseDestroy(cusparse_handle); - cusolverSpDestroy(cusolver_handle); - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return (double) time / 1000; -} - } // namespace gproshan From b92583934544257bf923290e84b675acd49e5ea6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 23 Apr 2021 20:43:52 +0200 Subject: [PATCH 0510/1018] rt_embree: add RTC_SCENE_FLAG_COMPACT --- src/raytracing/rt_embree.cpp | 2 ++ src/viewer/viewer.cpp | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 6f53926a..6d08b223 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -101,6 +101,8 @@ embree::embree() device = rtcNewDevice(NULL); scene = rtcNewScene(device); + rtcSetSceneFlags(scene, RTC_SCENE_FLAG_COMPACT); + rtcInitIntersectContext(&intersect_context); rtcSetDeviceErrorFunction(device, embree_error, NULL); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e111c12b..226c3f35 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -866,7 +866,6 @@ void viewer::draw_meshes(shader & program, const bool & normals) else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - for(index_t i = 0; i < n_meshes; ++i) { glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); From cb275da016f820b1414be0560b909416c9baa03d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 26 Apr 2021 22:42:09 +0200 Subject: [PATCH 0511/1018] viewer: cleaning up camera class --- include/gproshan/viewer/camera.h | 10 ++----- src/viewer/camera.cpp | 50 ++++++++++---------------------- src/viewer/viewer.cpp | 10 ++++--- 3 files changed, 24 insertions(+), 46 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 2e961f7b..8e08235e 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -3,8 +3,6 @@ #include "mesh/quaternion.h" -#include "viewer/include_opengl.h" - // geometry processing and shape analysis framework namespace gproshan { @@ -17,22 +15,20 @@ class camera quaternion p_drag; quaternion p_last; quaternion r_last; - int t_last; public: double zoom; public: camera(); - void mouse(int button, int state, int x, int y, int w, int h); - void motion(int x, int y, int w, int h); - void idle(); + void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); + void motion(const double & x, const double & y, const int & w, const int & h); void zoom_in(); void zoom_out(); quaternion current_rotation() const; private: - quaternion click_to_sphere(int x, int y, int w, int h); + quaternion click_to_sphere(const double & x, const double & y, const int & w, const int & h); friend std::ostream & operator << (std::ostream & os, const camera & cam); friend std::istream & operator >> (std::istream & is, camera & cam); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index b897352b..227ea0d8 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -1,8 +1,7 @@ #include "viewer/camera.h" #include -#include -#include + using namespace std; @@ -11,23 +10,20 @@ using namespace std; namespace gproshan { -camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(1) {} +camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(2) {} -quaternion camera::click_to_sphere(int x, int y, int w, int h) +quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) { - quaternion p( 0., - 2. * (double) x / (double) w - 1., - 2. * (double) y / (double) h - 1., - 0.); + quaternion p(0, 2 * x / w - 1, 2 * y / h - 1, 0); - if(p.norm2() > 1.) + if(p.norm2() > 1) { p.normalize(); - p.im().z = 0.; + p.im().z = 0; } else { - p.im().z = sqrt(1. - p.norm2()); + p.im().z = sqrt(1 - p.norm2()); } return p; @@ -38,47 +34,33 @@ quaternion camera::current_rotation() const return (p_drag * p_click.conj()) * r_last; } -void camera::mouse(int, int state, int x, int y, int w, int h) +void camera::mouse(const bool & press, const double & x, const double & y, const int & w, const int & h) { - quaternion momentum = 1; - - if(state == GLFW_PRESS) + if(press) + { p_click = p_drag = p_last = click_to_sphere(x, y, w, h); - - if(state == GLFW_RELEASE) + } + else { - double timeSinceDrag = (clock() - t_last) / (double) CLOCKS_PER_SEC; - - if(timeSinceDrag < .1) - { - momentum = p_drag * p_last.conj(); - momentum = (.03 * momentum + .97).unit(); - } - else - { - momentum = 1.; - } - r_last = p_drag * p_click.conj() * r_last; p_click = p_drag = 1.; } } -void camera::motion(int x, int y, int w, int h) +void camera::motion(const double & x, const double & y, const int & w, const int & h) { - t_last = clock(); p_last = p_drag; p_drag = click_to_sphere(x, y, w, h); } void camera::zoom_in() { - zoom -= 0.01; + zoom -= 0.02; } void camera::zoom_out() { - zoom += 0.01; + zoom += 0.02; } ostream & operator << (ostream & os, const camera & cam) @@ -87,7 +69,6 @@ ostream & operator << (ostream & os, const camera & cam) << cam.p_drag << "\n" << cam.p_last << "\n" << cam.r_last << "\n" - << cam.t_last << "\n" << cam.zoom << "\n"; } @@ -97,7 +78,6 @@ istream & operator >> (istream & is, camera & cam) >> cam.p_drag >> cam.p_last >> cam.r_last - >> cam.t_last >> cam.zoom; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 226c3f35..a6196986 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -79,10 +79,8 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { - eye = vertex(0., 0., -2. * cam.zoom); - center = vertex(0., 0., 0.); + eye = vertex(0., 0., -cam.zoom); up = vertex(0., 1., 0.); - light = vertex(-1., 1., -2.); quaternion r = cam.current_rotation(); @@ -415,7 +413,11 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); - else view->cam.mouse(button, action, xpos, ypos, view->window_width, view->window_height); + else if(button == GLFW_MOUSE_BUTTON_RIGHT) + { + } + else + view->cam.mouse(action == GLFW_PRESS, xpos, ypos, view->window_width, view->window_height); } void viewer::cursor_callback(GLFWwindow * window, double x, double y) From 1ef4fadc151b2cfc4da60f1efcbb6b2e6fe06e1b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 29 Apr 2021 18:15:27 +0200 Subject: [PATCH 0512/1018] geodesics: cleaning fast marching code --- include/gproshan/geodesics/geodesics.h | 3 - src/geodesics/geodesics.cpp | 89 +------------------------- src/geodesics/test_geodesics_ptp.cpp | 6 +- 3 files changed, 4 insertions(+), 94 deletions(-) diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index a020a394..ebaaf940 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -2,7 +2,6 @@ #define GEODESICS_H #include "mesh/che.h" -#include "include_arma.h" #include @@ -79,8 +78,6 @@ class geodesics void run_heat_method_gpu(che * mesh, const std::vector & sources); #endif // GPROSHAN_CUDA - real_t update(index_t & d, che * mesh, const index_t & he, vertex & vx); - real_t planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx); }; diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 80daa17a..241cb78e 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -128,7 +128,6 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co greater > > Q; real_t dv, dp; - index_t dir; // dir propagation vertex vx; size_t black_i, v; @@ -174,8 +173,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co dv = dist[v]; for_star(v_he, mesh, v) { - dp = update(dir, mesh, v_he, vx); - //dp = update_step(mesh, dist, v_he); + dp = update_step(mesh, dist, v_he); if(dp < dv) { dv = dp; @@ -256,90 +254,5 @@ void geodesics::run_heat_method_gpu(che * mesh, const vector & sources) #endif // GPROSHAN_CUDA -//d = {NIL, 0, 1} cross edge, next, prev -real_t geodesics::update(index_t & d, che * mesh, const index_t & he, vertex & vx) -{ - d = NIL; - - a_mat X(3,2); - index_t x[3]; - - x[0] = mesh->vt(next(he)); - x[1] = mesh->vt(prev(he)); - x[2] = mesh->vt(he); //update x[2] - - vx = mesh->gt(x[2]); - - vertex v[2]; - v[0] = mesh->gt(x[0]) - vx; - v[1] = mesh->gt(x[1]) - vx; - - X(0, 0) = v[0][0]; - X(1, 0) = v[0][1]; - X(2, 0) = v[0][2]; - - X(0, 1) = v[1][0]; - X(1, 1) = v[1][1]; - X(2, 1) = v[1][2]; - - return planar_update(d, X, x, vx); -} - -real_t geodesics::planar_update(index_t & d, a_mat & X, index_t * x, vertex & vx) -{ - a_mat ones(2,1); - ones.ones(2,1); - - a_mat Q; - if(!inv_sympd(Q, X.t() * X)) - return INFINITY; - - a_mat t(2,1); - - t(0) = dist[x[0]]; - t(1) = dist[x[1]]; - - real_t p; - a_mat delta = ones.t() * Q * t; - real_t dis = as_scalar(delta * delta - (ones.t() * Q * ones) * (as_scalar(t.t() * Q * t) - 1)); - - if(dis >= 0) - { - p = delta(0) + sqrt(dis); - p /= as_scalar(ones.t() * Q * ones); - } - else p = INFINITY; - - a_mat n = X * Q * (t - p * ones); - a_mat cond = Q * X.t() * n; - - a_vec v(3); - - if(t(0) == INFINITY || t(1) == INFINITY || dis < 0 || (cond(0) >= 0 || cond(1) >= 0)) - { - real_t dp[2]; - dp[0] = dist[x[0]] + norm(X.col(0)); - dp[1] = dist[x[1]] + norm(X.col(1)); - - d = dp[1] < dp[0]; - v = X.col(d); - p = dp[d]; - } - else - { - a_mat A(3,2); - A.col(0) = -n; - A.col(1) = X.col(1) - X.col(0); - a_vec b = -X.col(0); - a_mat l = solve(A, b); - v = l(1) * A.col(1) + X.col(0); - } - - vx += *((vertex *) v.memptr()); - - return p; -} - - } // namespace gproshan diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index c91c44db..25ad7432 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -28,11 +28,11 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) int n_test = nargs == 5 ? atoi(args[4]) : 10; #ifdef GPROSHAN_FLOAT - FILE * ftable = fopen("ptp_results.tex", "w"); + FILE * ftable = fopen("test_geodesics_float.md", "w"); #else - FILE *ftable = fopen("ptp_results_double.tex", "w"); + FILE *ftable = fopen("test_geodesics_double.md", "w"); const char * ptime = "& %6.3lfs "; -#endif +#endif // GPROSHAN_FLOAT const char * str[2] = {"", "\\bf"}; const char * pspeedup = "& \\bf (%.1lfx) "; From 5e98431be1521e03ee0170edb0c4e0077bef6f9f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 29 Apr 2021 18:55:12 +0200 Subject: [PATCH 0513/1018] geodesics: fix INFINITY from include --- include/gproshan/geodesics/geodesics.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index ebaaf940..87658bee 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -4,6 +4,7 @@ #include "mesh/che.h" +#include #include From ccaff1ac8bfd597012260a8a01c11c2678ab0b8a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 30 Apr 2021 09:44:02 +0200 Subject: [PATCH 0514/1018] geodesics: rename output test_geodesics --- src/geodesics/test_geodesics_ptp.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 25ad7432..2bb0e304 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -28,11 +28,11 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) int n_test = nargs == 5 ? atoi(args[4]) : 10; #ifdef GPROSHAN_FLOAT - FILE * ftable = fopen("test_geodesics_float.md", "w"); + FILE * ftable = fopen("test_geodesics_float.tex", "w"); #else - FILE *ftable = fopen("test_geodesics_double.md", "w"); + FILE * ftable = fopen("test_geodesics_double.tex", "w"); const char * ptime = "& %6.3lfs "; -#endif // GPROSHAN_FLOAT +#endif const char * str[2] = {"", "\\bf"}; const char * pspeedup = "& \\bf (%.1lfx) "; From e2c7cc69e9a1709a28e5f523aa55b360b56824ad Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 5 May 2021 14:03:18 +0200 Subject: [PATCH 0515/1018] updating cmake project to v3.20 --- CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c94bd57..d576224d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.18) +cmake_minimum_required(VERSION 3.20) project(gproshan VERSION 3.0) @@ -15,7 +15,7 @@ add_compile_options(-Wall -Wextra -Wno-unused-result) add_definitions(-DGPROSHAN_FLOAT) add_definitions(-DGPROSHAN_LOG) -find_package(CUDAToolkit 11) +find_package(CUDAToolkit 11.3) if(CUDAToolkit_FOUND) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 17) @@ -28,7 +28,7 @@ if(CUDAToolkit_FOUND) add_definitions(-DGPROSHAN_CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) - find_package(OptiX 7.2) + find_package(OptiX 7.3) if(OptiX_INCLUDE) add_definitions(-DGPROSHAN_OPTIX) include_directories(SYSTEM ${OptiX_INCLUDE}) @@ -36,7 +36,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -find_package(embree 3.10) +find_package(embree 3.12) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) include_directories(SYSTEM ${embree_INCLUDE_DIRS}) @@ -120,7 +120,7 @@ target_link_libraries(test_gproshan gproshan) if(OptiX_INCLUDE) add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - add_custom_target(ptx_cp_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) + add_custom_target(ptx_cp_sources DEPENDS ptx_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) add_dependencies(test_gproshan ptx_cp_sources) endif(OptiX_INCLUDE) From 818b3f49b0e5a07f4860931e30f8a31d9169accd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 18 May 2021 12:43:33 +0200 Subject: [PATCH 0516/1018] update findoptix --- cmake/FindOptiX.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/FindOptiX.cmake b/cmake/FindOptiX.cmake index aeabce84..0aff2abd 100644 --- a/cmake/FindOptiX.cmake +++ b/cmake/FindOptiX.cmake @@ -1,5 +1,5 @@ # -# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. +# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions From be1cd595b5aa81418883f6d8725a86619d52a09b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 00:23:55 +0200 Subject: [PATCH 0517/1018] update laplacian eigs option sa to sm --- src/laplacian/laplacian.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index c7318afa..9f0e1f8e 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -97,7 +97,7 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat // a_sp_mat D = sqrt(A); // D.for_each([](a_sp_mat::elem_type & val) { val = 1. / val; }); - if(!eigs_sym(eigval, eigvec, L, k, "sa")) + if(!eigs_sym(eigval, eigvec, L, k, "sm")) return 0; eigval.save(feigval); From 1b9386457945ecfd3715a5c9b11bd14309b2f572 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 14:39:21 +0200 Subject: [PATCH 0518/1018] cleaning up laplacian module --- include/gproshan/features/descriptor.h | 7 ++- include/gproshan/laplacian/fairing.h | 6 +-- include/gproshan/laplacian/fairing_spectral.h | 3 +- include/gproshan/laplacian/fairing_taubin.h | 2 +- src/app_viewer.cpp | 6 +-- src/features/descriptor.cpp | 1 + src/laplacian/fairing.cpp | 11 ++--- src/laplacian/fairing_spectral.cpp | 24 +++------- src/laplacian/fairing_taubin.cpp | 44 ++++--------------- src/laplacian/laplacian.cpp | 8 ++-- 10 files changed, 37 insertions(+), 75 deletions(-) diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index a419cdf3..3ee782b7 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -19,10 +19,15 @@ class descriptor a_vec eigval; a_mat eigvec; a_mat features; + size_t n_eigs; public: descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); - operator bool () const; ///< return true if the features were computed + + ///< return true if the features were computed + operator bool () const; + + ///< return norm of the descriptor for the vertex v real_t operator () (const index_t & v) const; private: diff --git a/include/gproshan/laplacian/fairing.h b/include/gproshan/laplacian/fairing.h index 5b86cc92..55fab21f 100644 --- a/include/gproshan/laplacian/fairing.h +++ b/include/gproshan/laplacian/fairing.h @@ -11,13 +11,13 @@ namespace gproshan { class fairing { protected: - vertex * positions; + vertex * vertices = nullptr; public: - fairing(); + fairing() = default; virtual ~fairing(); void run(che * mesh); - vertex * get_postions(); + const vertex * new_vertices(); protected: virtual void compute(che * mesh) = 0; diff --git a/include/gproshan/laplacian/fairing_spectral.h b/include/gproshan/laplacian/fairing_spectral.h index 414c2747..29d094ea 100644 --- a/include/gproshan/laplacian/fairing_spectral.h +++ b/include/gproshan/laplacian/fairing_spectral.h @@ -15,7 +15,7 @@ class fairing_spectral : public fairing public: fairing_spectral(const size_t & k_ = 10); - virtual ~fairing_spectral(); + virtual ~fairing_spectral() = default; private: void compute(che * mesh); @@ -25,3 +25,4 @@ class fairing_spectral : public fairing } // namespace gproshan #endif // FAIRING_SPECTRAL_H + diff --git a/include/gproshan/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h index 26beb49c..547ebc29 100644 --- a/include/gproshan/laplacian/fairing_taubin.h +++ b/include/gproshan/laplacian/fairing_taubin.h @@ -15,7 +15,7 @@ class fairing_taubin : public fairing public: fairing_taubin(const real_t & step_ = 0.01); - virtual ~fairing_taubin(); + virtual ~fairing_taubin() = default; private: void compute(che * mesh); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a3b6ad1a..fdfc3ef8 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -247,14 +247,14 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) che_viewer & mesh = view->active_mesh(); static int k = 100; - ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices / 6); + ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices); if(ImGui::Button("Run")) { fairing_spectral fair(k); fair.run(mesh); - mesh->set_vertices(fair.get_postions()); + mesh->set_vertices(fair.new_vertices()); mesh->update_normals(); } @@ -274,7 +274,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) fairing_taubin fair(step); fair.run(mesh); - mesh->set_vertices(fair.get_postions()); + mesh->set_vertices(fair.new_vertices()); mesh->update_normals(); } diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index d068d9a2..7efb183e 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -2,6 +2,7 @@ #include "laplacian/laplacian.h" + // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/laplacian/fairing.cpp b/src/laplacian/fairing.cpp index cd96a9e6..effd5bab 100644 --- a/src/laplacian/fairing.cpp +++ b/src/laplacian/fairing.cpp @@ -5,14 +5,9 @@ namespace gproshan { -fairing::fairing() -{ - positions = nullptr; -} - fairing::~fairing() { - if(positions) delete [] positions; + delete [] vertices; } void fairing::run(che * mesh) @@ -20,9 +15,9 @@ void fairing::run(che * mesh) compute(mesh); } -vertex * fairing::get_postions() +const vertex * fairing::new_vertices() { - return positions; + return vertices; } diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index c1287a94..551bee7e 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -7,33 +7,21 @@ namespace gproshan { -fairing_spectral::fairing_spectral(const size_t & k_): fairing(), k(k_) -{ -} - -fairing_spectral::~fairing_spectral() -{ - -} +fairing_spectral::fairing_spectral(const size_t & k_): fairing(), k(k_) {} void fairing_spectral::compute(che * mesh) { - double time; - - positions = new vertex[mesh->n_vertices]; - - a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); + delete [] vertices; + vertices = new vertex[mesh->n_vertices]; + memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - positions[v] = mesh->gt(v); + a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); a_sp_mat L, A; a_vec eigval; a_mat eigvec; - TIC(time) k = eigs_laplacian(mesh, eigval, eigvec, L, A, k); TOC(time) - gproshan_debug_var(time); + k = eigs_laplacian(mesh, eigval, eigvec, L, A, k); X = X * eigvec * eigvec.t(); } diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index f9fedacb..f01da430 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -7,48 +7,20 @@ namespace gproshan { -fairing_taubin::fairing_taubin(const real_t & step_): fairing(), step(step_) -{ -} - -fairing_taubin::~fairing_taubin() -{ - -} +fairing_taubin::fairing_taubin(const real_t & step_): fairing(), step(step_) {} void fairing_taubin::compute(che * mesh) { - double time; -/* - a_sp_mat_e Le, Ae; - TIC(time) - laplacian(mesh, Le, Ae); - TOC(time) - cout<<"time laplacian: "<n_vertices]; + memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); - gproshan_debug(compute laplacian); + a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); - TIC(time) laplacian(mesh, L, A); TOC(time) - gproshan_debug_var(time); - - positions = new vertex[mesh->n_vertices]; - - a_mat X((real_t *) positions, 3, mesh->n_vertices, false, true); - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - positions[v] = mesh->gt(v); - - a_mat R; - a_mat AX = A * X.t(); - a_sp_mat M = A + step * L; - - gproshan_debug(solve system); + a_sp_mat L, A; + laplacian(mesh, L, A); - TIC(time) spsolve(R, M, AX); TOC(time) - gproshan_debug_var(time); + a_mat R = spsolve(A + step * L, A * X.t()); X = R.t(); } diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 9f0e1f8e..4de633d7 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -86,13 +86,13 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat laplacian(mesh, L, A); - string feigval = tmp_file_path(mesh->name_size() + '_' + to_string(k) + ".L_eigval"); - string feigvec = tmp_file_path(mesh->name_size() + '_' + to_string(k) + ".L_eigvec"); + string feigval = tmp_file_path(mesh->name_size() + ".eigval"); + string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); gproshan_debug_var(feigval); gproshan_debug_var(feigvec); - if(!eigval.load(feigval) || !eigvec.load(feigvec)) + if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) { // a_sp_mat D = sqrt(A); // D.for_each([](a_sp_mat::elem_type & val) { val = 1. / val; }); @@ -104,7 +104,7 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat eigvec.save(feigvec); } - return eigval.n_elem; + return k < eigval.n_elem ? k : eigval.n_elem; } From f155ebbde2a688d3377f4af6f68c83596824eb0e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 16:04:45 +0200 Subject: [PATCH 0519/1018] fairing spectral viewer demo --- include/gproshan/laplacian/fairing_spectral.h | 6 +++--- src/app_viewer.cpp | 15 +++++++++++---- src/laplacian/fairing_spectral.cpp | 5 +++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/include/gproshan/laplacian/fairing_spectral.h b/include/gproshan/laplacian/fairing_spectral.h index 29d094ea..d16a8a86 100644 --- a/include/gproshan/laplacian/fairing_spectral.h +++ b/include/gproshan/laplacian/fairing_spectral.h @@ -10,11 +10,11 @@ namespace gproshan { class fairing_spectral : public fairing { - private: - size_t k; + public: + size_t n_eigs; public: - fairing_spectral(const size_t & k_ = 10); + fairing_spectral(const size_t & n_eigs_ = 100); virtual ~fairing_spectral() = default; private: diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index fdfc3ef8..14474c72 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -246,12 +246,19 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int k = 100; - ImGui::SliderInt("eigenvectors", &k, 1, mesh->n_vertices); + static vector vertices; + static fairing_spectral fair; + static size_t min_neigs = 1; - if(ImGui::Button("Run")) + if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &mesh->n_vertices)) { - fairing_spectral fair(k); + if(!vertices.size()) + { + vertices.resize(mesh->n_vertices); + memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + } + else mesh->set_vertices(vertices.data()); + fair.run(mesh); mesh->set_vertices(fair.new_vertices()); diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index 551bee7e..93db8210 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -7,7 +7,7 @@ namespace gproshan { -fairing_spectral::fairing_spectral(const size_t & k_): fairing(), k(k_) {} +fairing_spectral::fairing_spectral(const size_t & n_eigs_): n_eigs(n_eigs_) {} void fairing_spectral::compute(che * mesh) { @@ -21,8 +21,9 @@ void fairing_spectral::compute(che * mesh) a_vec eigval; a_mat eigvec; - k = eigs_laplacian(mesh, eigval, eigvec, L, A, k); + n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); + eigvec = eigvec.head_cols(n_eigs); X = X * eigvec * eigvec.t(); } From cdbff819a37e18fdb3c919a2e5d52271c2ea16d3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 16:37:25 +0200 Subject: [PATCH 0520/1018] fairing taubing viewer demo --- include/gproshan/laplacian/fairing_taubin.h | 4 ++-- src/app_viewer.cpp | 13 ++++++++++--- src/laplacian/fairing_taubin.cpp | 2 +- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/gproshan/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h index 547ebc29..0665c194 100644 --- a/include/gproshan/laplacian/fairing_taubin.h +++ b/include/gproshan/laplacian/fairing_taubin.h @@ -10,11 +10,11 @@ namespace gproshan { class fairing_taubin : public fairing { - private: + public: real_t step; public: - fairing_taubin(const real_t & step_ = 0.01); + fairing_taubin(const real_t & step_ = 0.001); virtual ~fairing_taubin() = default; private: diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 14474c72..9b5162a2 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -273,12 +273,19 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static float step = 0.001; //cin >> step; - ImGui::InputFloat("step", &step, 0.001, 1, "%.3f"); + static vector vertices; + static fairing_taubin fair; + ImGui_InputReal("step", &fair.step, 0.001); if(ImGui::Button("Run")) { - fairing_taubin fair(step); + if(!vertices.size()) + { + vertices.resize(mesh->n_vertices); + memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + } + else mesh->set_vertices(vertices.data()); + fair.run(mesh); mesh->set_vertices(fair.new_vertices()); diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index f01da430..dd4cf091 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -7,7 +7,7 @@ namespace gproshan { -fairing_taubin::fairing_taubin(const real_t & step_): fairing(), step(step_) {} +fairing_taubin::fairing_taubin(const real_t & step_): step(step_) {} void fairing_taubin::compute(che * mesh) { From fe6ee26c7a664d21a72feb43e6792a6ff560e6d3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 16:44:23 +0200 Subject: [PATCH 0521/1018] fairing spectral max number of eigs --- src/app_viewer.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 9b5162a2..b8b5d921 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -249,8 +249,12 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) static vector vertices; static fairing_spectral fair; static size_t min_neigs = 1; + static size_t max_neigs = 2000; - if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &mesh->n_vertices)) + if(max_neigs > mesh->n_vertices) + max_neigs = mesh->n_vertices; + + if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &max_neigs)) { if(!vertices.size()) { From dd826b93b0e291e2fb9955e02e1d3deb945ed8d3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 21:17:11 +0200 Subject: [PATCH 0522/1018] descriptors n_eigs --- include/gproshan/features/descriptor.h | 2 +- src/app_viewer.cpp | 7 ++++--- src/features/descriptor.cpp | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index 3ee782b7..16c470a6 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -19,10 +19,10 @@ class descriptor a_vec eigval; a_mat eigvec; a_mat features; - size_t n_eigs; public: descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); + size_t n_eigs(); ///< return true if the features were computed operator bool () const; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b8b5d921..c4c947d3 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -635,18 +635,19 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::s app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int K = 50; + static int n_eigs = 50; static bool status = true; - ImGui::InputInt("eigenvectors", &K); + ImGui::InputInt("n_eigs", &n_eigs); if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing features."); if(ImGui::Button("Run")) { - descriptor features(sig, mesh, K); + descriptor features(sig, mesh, n_eigs); if(features) { status = true; + n_eigs = features.n_eigs(); real_t max_s = 0; #pragma omp parallel for reduction(max: max_s) diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 7efb183e..4db67c7a 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -12,6 +12,12 @@ descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n if(!eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs)) return; + if(eigval.size() > n_eigs) + { + eigval = eigval.head(n_eigs); + eigvec = eigvec.head_cols(n_eigs); + } + switch(sig) { case GPS: compute_gps(); break; @@ -20,6 +26,11 @@ descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n } } +size_t descriptor::n_eigs() +{ + return eigval.n_elem; +} + descriptor::operator bool () const { return features.size() > 0; From c5b62959483816399c0a5f37b4df54d562397870 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 31 May 2021 23:28:21 +0200 Subject: [PATCH 0523/1018] return correct n_eigs (k) in eigs_laplacian function --- src/features/descriptor.cpp | 6 ------ src/laplacian/fairing_spectral.cpp | 2 +- src/laplacian/laplacian.cpp | 13 +++++++------ 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 4db67c7a..992d0520 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -12,12 +12,6 @@ descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n if(!eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs)) return; - if(eigval.size() > n_eigs) - { - eigval = eigval.head(n_eigs); - eigvec = eigvec.head_cols(n_eigs); - } - switch(sig) { case GPS: compute_gps(); break; diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index 93db8210..27781b0a 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -22,8 +22,8 @@ void fairing_spectral::compute(che * mesh) a_mat eigvec; n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); + if(!n_eigs) return; - eigvec = eigvec.head_cols(n_eigs); X = X * eigvec * eigvec.t(); } diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 4de633d7..2fff05bd 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -82,16 +82,11 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) { - gproshan_debug(LAPLACIAN); - laplacian(mesh, L, A); string feigval = tmp_file_path(mesh->name_size() + ".eigval"); string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); - gproshan_debug_var(feigval); - gproshan_debug_var(feigvec); - if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) { // a_sp_mat D = sqrt(A); @@ -104,7 +99,13 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat eigvec.save(feigvec); } - return k < eigval.n_elem ? k : eigval.n_elem; + if(k < eigval.n_elem) + { + eigval = eigval.head(k); + eigvec = eigvec.head_cols(k); + } + + return eigval.n_elem; } From 07ef1d5971941a711af8a7278cda96a58326a47e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 5 Jun 2021 19:29:26 +0200 Subject: [PATCH 0524/1018] viewer: fix gradient field --- shaders/fragment.glsl | 1 - shaders/geometry_gradient.glsl | 5 ++--- shaders/vertex_gradient.glsl | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 3828734d..ed5a664a 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -43,7 +43,6 @@ void main() { vec3 color = idx_colormap > 0 ? colormap(idx_colormap, gs_color) : gs_mesh_color; - // lines if(render_lines) { float h = gs_color; diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index f9c80b36..096ccf34 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -6,7 +6,6 @@ layout (line_strip, max_vertices = 2) out; in float color[]; in vec3 position[]; -uniform float length; uniform mat4 model_view_mat; uniform mat4 proj_mat; @@ -22,10 +21,10 @@ void main() vec3 pjk = cross(n, xk - xj); vec3 pki = cross(n, xi - xk); - vec3 g = normalize(color[0] * pjk + color[1] * pki + color[2] * pij); + vec3 g = color[0] * pjk + color[1] * pki + color[2] * pij; vec3 a = (xi + xj + xk) / 3.0; - vec3 b = a + g * 0.3 * length; + vec3 b = a + g * 50; gl_Position = proj_mat * model_view_mat * vec4(a, 1.); EmitVertex(); diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl index 3b1cafc1..14a1b5b1 100644 --- a/shaders/vertex_gradient.glsl +++ b/shaders/vertex_gradient.glsl @@ -1,7 +1,7 @@ #version 410 core layout (location=0) in vec3 in_position; -layout (location=2) in float in_color; +layout (location=3) in float in_color; out float color; out vec3 position; From 939d95660bee001f28493cd7b6f18f64c7c79bd6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 6 Jun 2021 13:09:43 +0200 Subject: [PATCH 0525/1018] viewer: rename eigenfunctions --- include/gproshan/app_viewer.h | 2 +- src/app_viewer.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index f416f7f9..735052ab 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -79,7 +79,7 @@ class app_viewer : public viewer static bool process_pc_reconstruction(viewer * p_view); // Features - static bool process_functional_maps(viewer * p_view); + static bool process_eigenfuntions(viewer * p_view); static bool process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig); static bool process_gps(viewer * p_view); static bool process_hks(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c4c947d3..af9bd59a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -84,7 +84,7 @@ void app_viewer::init() add_process(GLFW_KEY_L, {"L", "PC reconstruction", process_pc_reconstruction}); sub_menus.push_back("Features"); - add_process(GLFW_KEY_2, {"2", "Functional Maps", process_functional_maps}); + add_process(GLFW_KEY_2, {"2", "Eigenfunctions", process_eigenfuntions}); add_process(GLFW_KEY_3, {"3", "GPS", process_gps}); add_process(GLFW_KEY_4, {"4", "HKS", process_hks}); add_process(GLFW_KEY_5, {"5", "WKS", process_wks}); @@ -588,7 +588,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) // Features -bool app_viewer::process_functional_maps(viewer * p_view) +bool app_viewer::process_eigenfuntions(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); From 6452fbf73a19343a899304dad92f0a8ef93dad98 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Jun 2021 18:34:49 +0200 Subject: [PATCH 0526/1018] viewer: refactoring camera class --- include/gproshan/viewer/camera.h | 8 +++++++- include/gproshan/viewer/viewer.h | 4 ---- src/viewer/camera.cpp | 13 +++++++++++++ src/viewer/viewer.cpp | 21 ++++++++------------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 8e08235e..f1200784 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -3,6 +3,8 @@ #include "mesh/quaternion.h" +#include + // geometry processing and shape analysis framework namespace gproshan { @@ -17,15 +19,19 @@ class camera quaternion r_last; public: + quaternion eye; + quaternion center; + quaternion up; double zoom; public: camera(); + glm::mat4 look_at(const quaternion & r); + quaternion current_rotation() const; void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); void motion(const double & x, const double & y, const int & w, const int & h); void zoom_in(); void zoom_out(); - quaternion current_rotation() const; private: quaternion click_to_sphere(const double & x, const double & y, const int & w, const int & h); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 897d998d..a818d7b8 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -69,10 +69,6 @@ class viewer camera cam; - quaternion eye; - quaternion center; - quaternion up; - quaternion light; glm::mat4 view_mat; diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 227ea0d8..c05a3279 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -2,6 +2,8 @@ #include +#include + using namespace std; @@ -12,6 +14,17 @@ namespace gproshan { camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(2) {} +glm::mat4 camera::look_at(const quaternion & r) +{ + eye = vertex(0, 0, -zoom); + center = vertex(0, 0, 0); + up = vertex(0, 1, 0); + + return glm::lookAt( glm_vec3(r.conj() * eye * r), + glm_vec3(r.conj() * center * r), + glm_vec3(r.conj() * up * r)); +} + quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) { quaternion p(0, 2 * x / w - 1, 2 * y / h - 1, 0); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index a6196986..d5c8f71c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -79,19 +79,14 @@ bool viewer::run() { while(!glfwWindowShouldClose(window)) { - eye = vertex(0., 0., -cam.zoom); - up = vertex(0., 1., 0.); - light = vertex(-1., 1., -2.); - + light = vertex(-1, 1, -2); + quaternion r = cam.current_rotation(); - - eye = r.conj() * eye * r; + light = r.conj() * light * r; - up = r.conj() * up * r; - - + + view_mat = cam.look_at(r); proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); - view_mat = glm::lookAt(glm_vec3(eye), glm_vec3(center), glm_vec3(up)); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) @@ -774,14 +769,14 @@ bool viewer::raycasting(viewer * view) void viewer::render_gl() { - glProgramUniform3f(shader_sphere, shader_sphere("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_sphere, shader_sphere("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_sphere, shader_sphere("light"), light[0], light[1], light[2]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), idx_colormap); - glProgramUniform3f(shader_triangles, shader_triangles("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); glProgramUniform1i(shader_triangles, shader_triangles("render_flat"), render_flat); glProgramUniform1i(shader_triangles, shader_triangles("render_lines"), render_lines); @@ -790,7 +785,7 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), idx_colormap); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), eye[0], eye[1], eye[2]); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); From dc835a60b981718dcc7e97f5a72841cf53b28084 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Jun 2021 19:09:40 +0200 Subject: [PATCH 0527/1018] viewer: refactoring camera class, fix camera position --- src/viewer/camera.cpp | 4 +++- src/viewer/viewer.cpp | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index c05a3279..ca19e7a5 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -20,7 +20,9 @@ glm::mat4 camera::look_at(const quaternion & r) center = vertex(0, 0, 0); up = vertex(0, 1, 0); - return glm::lookAt( glm_vec3(r.conj() * eye * r), + eye = r.conj() * eye * r; + + return glm::lookAt( glm_vec3(eye), glm_vec3(r.conj() * center * r), glm_vec3(r.conj() * up * r)); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d5c8f71c..00b9e063 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -80,11 +80,11 @@ bool viewer::run() while(!glfwWindowShouldClose(window)) { light = vertex(-1, 1, -2); - + quaternion r = cam.current_rotation(); - + light = r.conj() * light * r; - + view_mat = cam.look_at(r); proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); From f32386aa771f9a3437ebfe938935cbd4da93fa0e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 26 Jun 2021 21:57:01 +0200 Subject: [PATCH 0528/1018] viewer: camera class default values --- include/gproshan/viewer/camera.h | 17 ++++++++--------- src/viewer/camera.cpp | 7 +------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index f1200784..10a6c922 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -13,19 +13,18 @@ namespace gproshan { class camera { private: - quaternion p_click; - quaternion p_drag; - quaternion p_last; - quaternion r_last; + quaternion p_click = 1; + quaternion p_drag = 1; + quaternion p_last = 1; + quaternion r_last = 1; public: - quaternion eye; - quaternion center; - quaternion up; - double zoom; + quaternion eye = vertex(0, 0, 2); + quaternion center = vertex(0, 0, 0); + quaternion up = vertex(0, 1, 0); + double zoom = 2; public: - camera(); glm::mat4 look_at(const quaternion & r); quaternion current_rotation() const; void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index ca19e7a5..d6bf70df 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -12,14 +12,9 @@ using namespace std; namespace gproshan { -camera::camera(): p_click(1), p_drag(1), p_last(1), r_last(1), zoom(2) {} - glm::mat4 camera::look_at(const quaternion & r) { - eye = vertex(0, 0, -zoom); - center = vertex(0, 0, 0); - up = vertex(0, 1, 0); - + eye = vertex(0, 0, -zoom); eye = r.conj() * eye * r; return glm::lookAt( glm_vec3(eye), From b4fb3aeb8b1932ef3b1b7c8b5cc43c86b3a0db74 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Jul 2021 12:43:36 +0200 Subject: [PATCH 0529/1018] viewer: fix calling update_vbo --- src/viewer/viewer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 00b9e063..50952080 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -180,7 +180,6 @@ bool viewer::run() ImGui::PushID(pro.name.c_str()); ImGui::Indent(); pro.selected = pro.selected && p.second.function(this); - active_mesh().update_vbo(); ImGui::Unindent(); ImGui::PopID(); } From 551a7596f4f1da2f2384847f799062e72434d7cc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 7 Jul 2021 23:46:29 +0200 Subject: [PATCH 0530/1018] viewer: fix update vbo calls --- include/gproshan/viewer/che_viewer.h | 4 +++ src/viewer/che_viewer.cpp | 45 +++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index d674d0a5..0de2fae0 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -48,6 +48,10 @@ class che_viewer void reload(); void update(); void update_vbo(); + void update_vbo_geometry(); + void update_vbo_normal(); + void update_vbo_color(); + void update_vbo_heatmap(); void update_instances_translations(const std::vector & translations); void draw(shader & program); void draw_point_cloud(shader & program); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 54bc748c..41a0e8e1 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -85,6 +85,14 @@ void che_viewer::update() } void che_viewer::update_vbo() +{ + update_vbo_geometry(); + update_vbo_normal(); + update_vbo_color(); + update_vbo_heatmap(); +} + +void che_viewer::update_vbo_geometry() { glBindVertexArray(vao); @@ -95,6 +103,21 @@ void che_viewer::update_vbo() glVertexAttribPointer(0, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); + // 4 INDEXES + if(!mesh->is_pointcloud()) + { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + + glBindVertexArray(0); +} + +void che_viewer::update_vbo_normal() +{ + glBindVertexArray(vao); + // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); @@ -102,6 +125,13 @@ void che_viewer::update_vbo() glVertexAttribPointer(1, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void che_viewer::update_vbo_color() +{ + glBindVertexArray(vao); + // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(che::rgb_t), &mesh->rgb(0), GL_STATIC_DRAW); @@ -109,6 +139,13 @@ void che_viewer::update_vbo() glVertexAttribPointer(2, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void che_viewer::update_vbo_heatmap() +{ + glBindVertexArray(vao); + // 3 HEAT MAP glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); @@ -116,14 +153,6 @@ void che_viewer::update_vbo() glVertexAttribPointer(3, 1, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); - // 3 INDEXES - if(!mesh->is_pointcloud()) - { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } - glBindVertexArray(0); } From f2eaed537aed5907af3b2dc6f5b806cc3bbdd5e6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 7 Jul 2021 23:47:37 +0200 Subject: [PATCH 0531/1018] viewer: update vbo fairing and geodesics calls --- src/app_viewer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index af9bd59a..04c93c28 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -269,6 +269,9 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) mesh->update_normals(); } + mesh.update_vbo_geometry(); + mesh.update_vbo_normal(); + return true; } @@ -296,6 +299,9 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) mesh->update_normals(); } + mesh.update_vbo_geometry(); + mesh.update_vbo_normal(); + return true; } @@ -335,6 +341,7 @@ bool app_viewer::process_geodesics(viewer * p_view) params.radio = G.radio(); mesh->update_heatmap(&G[0]); + mesh.update_vbo_heatmap(); } return true; @@ -388,6 +395,8 @@ bool app_viewer::process_voronoi(viewer * p_view) mesh->heatmap(i) /= mesh.selected.size() + 1; } + mesh.update_vbo_heatmap(); + return false; } @@ -414,6 +423,8 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); } + mesh.update_vbo_heatmap(); + gproshan_debug_var(n_toplesets); delete [] toplesets; From d16735618eb95f895ecacf56c86841dd22747e04 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 10 Jul 2021 22:37:57 +0200 Subject: [PATCH 0532/1018] app viewer: eigen descriptors viewer code cleaning up --- include/gproshan/app_viewer.h | 5 +---- src/app_viewer.cpp | 34 +++++++++++----------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 735052ab..e86fb8aa 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -80,10 +80,7 @@ class app_viewer : public viewer // Features static bool process_eigenfuntions(viewer * p_view); - static bool process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig); - static bool process_gps(viewer * p_view); - static bool process_hks(viewer * p_view); - static bool process_wks(viewer * p_view); + static bool process_descriptor_heatmap(viewer * p_view); static bool process_key_points(viewer * p_view); static bool process_key_components(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 04c93c28..2722b487 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -85,18 +85,16 @@ void app_viewer::init() sub_menus.push_back("Features"); add_process(GLFW_KEY_2, {"2", "Eigenfunctions", process_eigenfuntions}); - add_process(GLFW_KEY_3, {"3", "GPS", process_gps}); - add_process(GLFW_KEY_4, {"4", "HKS", process_hks}); - add_process(GLFW_KEY_5, {"5", "WKS", process_wks}); - add_process(GLFW_KEY_6, {"6", "Key Points", process_key_points}); - add_process(GLFW_KEY_7, {"7", "Key Components", process_key_components}); + add_process(GLFW_KEY_3, {"3", "Descriptors", process_descriptor_heatmap}); + add_process(GLFW_KEY_4, {"4", "Key Points", process_key_points}); + add_process(GLFW_KEY_5, {"5", "Key Components", process_key_components}); sub_menus.push_back("Hole Filling"); add_process(GLFW_KEY_X, {"X", "Poisson: Membrane surface", process_poisson_laplacian_1}); add_process(GLFW_KEY_Y, {"Y", "Poisson: Thin-plate surface", process_poisson_laplacian_2}); add_process(GLFW_KEY_Z, {"Z", "Poisson: Minimum variation surface", process_poisson_laplacian_3}); - add_process(GLFW_KEY_8, {"8", "Fill hole: planar mesh", process_fill_holes}); - add_process(GLFW_KEY_9, {"0", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines}); + add_process(GLFW_KEY_6, {"6", "Fill hole: planar mesh", process_fill_holes}); + add_process(GLFW_KEY_7, {"7", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines}); sub_menus.push_back("Others"); add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", process_select_multiple}); @@ -641,13 +639,16 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) return true; } -bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::signature & sig) +bool app_viewer::process_descriptor_heatmap(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); static int n_eigs = 50; static bool status = true; + static descriptor::signature sig = descriptor::GPS; + + ImGui::Combo("descriptor", (int *) &sig, "GPS\0HKS\0WKS\0\0"); ImGui::InputInt("n_eigs", &n_eigs); if(!status) ImGui::TextColored({1, 0, 0, 1}, "Error computing features."); @@ -671,6 +672,8 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::s #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) /= max_s; + + mesh.update_vbo_heatmap(); } else status = false; } @@ -678,21 +681,6 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view, const descriptor::s return true; } -bool app_viewer::process_gps(viewer * p_view) -{ - return process_descriptor_heatmap(p_view, descriptor::GPS); -} - -bool app_viewer::process_hks(viewer * p_view) -{ - return process_descriptor_heatmap(p_view, descriptor::HKS); -} - -bool app_viewer::process_wks(viewer * p_view) -{ - return process_descriptor_heatmap(p_view, descriptor::WKS); -} - bool app_viewer::process_key_points(viewer * p_view) { gproshan_log(APP_VIEWER); From 4ba551196b347bfc9c0828eb35eb61193f71cf7f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 16 Jul 2021 23:55:25 +0200 Subject: [PATCH 0533/1018] features: refactoring key points code --- include/gproshan/features/key_points.h | 15 ++----- src/app_viewer.cpp | 2 + src/features/key_points.cpp | 57 +++++++++----------------- 3 files changed, 25 insertions(+), 49 deletions(-) diff --git a/include/gproshan/features/key_points.h b/include/gproshan/features/key_points.h index dac54375..cdee88aa 100644 --- a/include/gproshan/features/key_points.h +++ b/include/gproshan/features/key_points.h @@ -3,34 +3,25 @@ #include "mesh/che.h" -#include - // geometry processing and shape analysis framework namespace gproshan { -typedef std::pair real_idx_t; - class key_points { private: - size_t n_faces; - size_t n_vertices; - real_idx_t * face_areas; - index_t * kps; - bool * is_kp; - size_t n_kps; + std::vector kps; + std::vector is_kp; public: key_points(che * mesh, const real_t & percent = 0.10); - ~key_points(); const index_t & operator[](const index_t & i) const; const bool & operator()(const index_t & i) const; const size_t & size() const; private: - void compute_kps(che * mesh); + void compute_kps_areas(che * mesh, const real_t & percent); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2722b487..2ac51f4a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -713,6 +713,8 @@ bool app_viewer::process_key_components(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = (real_t) kcs(v) / kcs; + mesh.update_vbo_heatmap(); + return false; } diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 51a56812..6d514c8c 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -13,23 +13,7 @@ namespace gproshan { key_points::key_points(che * mesh, const real_t & percent) { - n_faces = mesh->n_faces; - n_vertices = mesh->n_vertices; - - face_areas = new real_idx_t[n_faces]; - - kps = new index_t[n_vertices]; - is_kp = new bool[n_vertices]; - - n_kps = percent * n_vertices; - compute_kps(mesh); -} - -key_points::~key_points() -{ - delete [] face_areas; - delete [] kps; - delete [] is_kp; + compute_kps_areas(mesh, percent); } const index_t & key_points::operator[](const index_t & i) const @@ -46,47 +30,46 @@ const bool & key_points::operator()(const index_t & i) const const size_t & key_points::size() const { - return n_kps; + return kps.size(); } -void key_points::compute_kps(che * mesh) +/// +void key_points::compute_kps_areas(che * mesh, const real_t & percent) { - // compute faces areas + std::pair face_areas; #pragma omp parallel for - for(index_t t = 0; t < n_faces; ++t) + for(index_t t = 0; t < mesh->n_faces; ++t) { - face_areas[t].first = mesh->area_trig(t); - face_areas[t].second = t; +// face_areas[t].first = mesh->area_trig(t); +// face_areas[t].second = t; } - sort(face_areas, face_areas + n_faces); +// sort(face_areas, face_areas + n_faces); - // compute kps - memset(is_kp, 0, sizeof(bool) * n_vertices); - - index_t he, k = 0; - for(index_t t = 0; t < n_faces; ++t) + is_kp.assign(false, mesh->n_vertices); + kps.reserve(mesh->n_vertices); + + for(index_t t = 0; t < mesh->n_faces; ++t) { - he = che::mtrig * face_areas[t].second; + index_t he = che::mtrig;// * face_areas[t].second; for(index_t i = 0; i < che::mtrig; ++i) { const index_t & v = mesh->vt(he); if(!is_kp[v]) { - kps[k++] = v; - is_kp[v] = 1; + kps.push_back(v); + is_kp[v] = true; } he = next(he); } } - // compute kps - memset(is_kp, 0, sizeof(bool) * n_vertices); - + is_kp.assign(false, mesh->n_vertices); + #pragma omp parallel for - for(index_t i = 0; i < n_kps; ++i) - is_kp[kps[i]] = 1; + for(const index_t & v: kps) + is_kp[v] = true; } From 312fd22b6945c4646f1a66bf30c36c556e6fd0db Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 17 Jul 2021 11:59:58 +0200 Subject: [PATCH 0534/1018] features: refactoring key points code --- include/gproshan/features/key_points.h | 4 +-- src/app_viewer.cpp | 16 +++++----- src/features/key_components.cpp | 2 +- src/features/key_points.cpp | 44 ++++++++++---------------- 4 files changed, 26 insertions(+), 40 deletions(-) diff --git a/include/gproshan/features/key_points.h b/include/gproshan/features/key_points.h index cdee88aa..221b04c9 100644 --- a/include/gproshan/features/key_points.h +++ b/include/gproshan/features/key_points.h @@ -16,9 +16,7 @@ class key_points public: key_points(che * mesh, const real_t & percent = 0.10); - const index_t & operator[](const index_t & i) const; - const bool & operator()(const index_t & i) const; - const size_t & size() const; + operator const std::vector & () const; private: void compute_kps_areas(che * mesh, const real_t & percent); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 2ac51f4a..5646f2bb 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -683,19 +683,19 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view) bool app_viewer::process_key_points(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - key_points kps(mesh); + static real_t percent = 0.1; + ImGui_InputReal("percent", &percent, 0.01, 0.1, "%.2f"); - mesh.selected.clear(); - mesh.selected.reserve(kps.size()); - - for(index_t i = 0; i < kps.size(); ++i) - mesh.selected.push_back(kps[i]); + if(ImGui::Button("Run")) + { + key_points kps(mesh); + mesh.selected = kps; + } - return false; + return true; } bool app_viewer::process_key_components(viewer * p_view) diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index a40ab398..4f272670 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -50,7 +50,7 @@ key_components::operator const size_t & () const void key_components::compute_kcs(che * mesh, const key_points & kps) { - geodesics fm(mesh, vector(&kps[0], &kps[0] + kps.size())); + geodesics fm(mesh, kps); radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index 6d514c8c..bc762d7e 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -16,43 +16,30 @@ key_points::key_points(che * mesh, const real_t & percent) compute_kps_areas(mesh, percent); } -const index_t & key_points::operator[](const index_t & i) const +key_points::operator const std::vector & () const { - assert(i < n_vertices); - return kps[i]; + return kps; } -const bool & key_points::operator()(const index_t & i) const -{ - assert(i < n_vertices); - return is_kp[i]; -} - -const size_t & key_points::size() const -{ - return kps.size(); -} - -/// +/// Efficient approach for interest points detection in non-rigid shapes +/// Cristian Jose Lopez Del Alamo; Luciano Arnaldo Romero Calla; Lizeth Joseline Fuentes Perez +/// DOI: 10.1109/CLEI.2015.7359459 void key_points::compute_kps_areas(che * mesh, const real_t & percent) { - std::pair face_areas; + std::vector > face_areas(mesh->n_faces); #pragma omp parallel for - for(index_t t = 0; t < mesh->n_faces; ++t) - { -// face_areas[t].first = mesh->area_trig(t); -// face_areas[t].second = t; - } + for(index_t f = 0; f < mesh->n_faces; ++f) + face_areas[f] = { mesh->area_trig(f), f }; -// sort(face_areas, face_areas + n_faces); + sort(face_areas.begin(), face_areas.end()); - is_kp.assign(false, mesh->n_vertices); + is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); - - for(index_t t = 0; t < mesh->n_faces; ++t) + + for(index_t he, t = 0; t < mesh->n_faces; ++t) { - index_t he = che::mtrig;// * face_areas[t].second; + he = che::mtrig * face_areas[t].second; for(index_t i = 0; i < che::mtrig; ++i) { const index_t & v = mesh->vt(he); @@ -65,8 +52,9 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) } } - is_kp.assign(false, mesh->n_vertices); - + kps.resize(percent * kps.size()); + is_kp.assign(mesh->n_vertices, false); + #pragma omp parallel for for(const index_t & v: kps) is_kp[v] = true; From d474ae4ed6211c346f68677f0b1c98d17c5cd689 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 17 Jul 2021 12:16:36 +0200 Subject: [PATCH 0535/1018] features: refactoring key components code --- include/gproshan/features/key_components.h | 15 ++++++------ src/app_viewer.cpp | 27 +++++++++++----------- src/features/key_components.cpp | 7 +++--- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index c70b103e..f48a0b07 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -2,7 +2,6 @@ #define KEY_COMPONENTS_H #include "mesh/che.h" -#include "features/key_points.h" #include @@ -14,21 +13,21 @@ namespace gproshan { class key_components { private: - real_t radio; - size_t n_vertices; - size_t n_comp; - index_t * comp; - size_t * comp_size; + real_t radio = 0; + size_t n_vertices = 0; + size_t n_comp = 0; + index_t * comp = nullptr; + size_t * comp_size = nullptr; std::map comp_idx; public: - key_components(che * mesh, const key_points & kps, const real_t & r); + key_components(che * mesh, const std::vector & kps, const real_t & r); ~key_components(); index_t operator()(const index_t & i); operator const size_t & () const; private: - void compute_kcs(che * mesh, const key_points & kps); + void compute_kcs(che * mesh, const std::vector & kps); index_t find(const index_t & x); bool join(index_t x, index_t y); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 5646f2bb..b3f84705 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -687,11 +687,9 @@ bool app_viewer::process_key_points(viewer * p_view) che_viewer & mesh = view->active_mesh(); static real_t percent = 0.1; - ImGui_InputReal("percent", &percent, 0.01, 0.1, "%.2f"); - - if(ImGui::Button("Run")) + if(ImGui_InputReal("percent", &percent, 0.01, 0.1, "%.2f")) { - key_points kps(mesh); + key_points kps(mesh, percent); mesh.selected = kps; } @@ -700,22 +698,23 @@ bool app_viewer::process_key_points(viewer * p_view) bool app_viewer::process_key_components(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - key_points kps(mesh); - key_components kcs(mesh, kps, .25); - - gproshan_debug_var(kcs); + static real_t radio = 0.25; + if(ImGui_InputReal("radio", &radio, 0.01, 0.1, "%.2f")) + { + // use the mesh selected points as key points. + key_components kcs(mesh, mesh.selected, radio); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = (real_t) kcs(v) / kcs; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + mesh->heatmap(v) = (real_t) kcs(v) / kcs; - mesh.update_vbo_heatmap(); + mesh.update_vbo_heatmap(); + } - return false; + return true; } diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index 4f272670..0352ca32 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -11,7 +11,7 @@ using namespace std; namespace gproshan { -key_components::key_components(che * mesh, const key_points & kps, const real_t & r): radio(r) +key_components::key_components(che * mesh, const std::vector & kps, const real_t & r): radio(r) { n_vertices = mesh->n_vertices; @@ -48,13 +48,14 @@ key_components::operator const size_t & () const return n_comp; } -void key_components::compute_kcs(che * mesh, const key_points & kps) +void key_components::compute_kcs(che * mesh, const std::vector & kps) { geodesics fm(mesh, kps); radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) - for_star(he, mesh, fm(i)) join(fm(i), mesh->vt(next(he))); + for_star(he, mesh, fm(i)) + join(fm(i), mesh->vt(next(he))); for(index_t i = 0; i < n_vertices; ++i) if(comp[i] == i && comp_size[i] > 1) From df3bd4157078182abe8dddfdf1f35b275c3f5c2c Mon Sep 17 00:00:00 2001 From: Lizeth Date: Sat, 24 Jul 2021 13:31:29 +0200 Subject: [PATCH 0536/1018] che_ptx: save and display image from ptx rgb values --- src/mesh/che_ptx.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 6682c355..96ea4136 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -3,8 +3,11 @@ #include #include #include +#include +#include +using namespace cimg_library; using namespace std; @@ -64,6 +67,12 @@ void che_ptx::read_file(const string & file) VC[v] = { r, g, b }; VHC[v] = a; } + + CImg img((unsigned char *) VC, 3, n_cols, n_rows); + img.permute_axes("yzcx"); + img.save((file+".jpg").c_str()); + + thread([](CImg img) { img.display(); }, img).detach(); } else { From f98cd31444c61a45f29b92b01125c3626f3b9d2b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 26 Jul 2021 20:17:49 +0200 Subject: [PATCH 0537/1018] che_ptx: save and display image from ptx rgb values --- src/mesh/che_ptx.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 96ea4136..2488731b 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -69,10 +69,10 @@ void che_ptx::read_file(const string & file) } CImg img((unsigned char *) VC, 3, n_cols, n_rows); - img.permute_axes("yzcx"); - img.save((file+".jpg").c_str()); + img.permute_axes("zycx"); + img.save((file + ".jpg").c_str()); - thread([](CImg img) { img.display(); }, img).detach(); + thread([](CImg img) { img.mirror("y").display(); }, img).detach(); } else { From 2e9d891b419078ddb0b07ad1c71a4b848ff72603 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Aug 2021 20:28:27 +0200 Subject: [PATCH 0538/1018] laplacian: update area laplace beltrami operator review fairing taubing, spectral --- src/app_viewer.cpp | 7 ++----- src/laplacian/laplacian.cpp | 9 +++------ 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b3f84705..3f7d23e5 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -245,12 +245,9 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) che_viewer & mesh = view->active_mesh(); static vector vertices; - static fairing_spectral fair; static size_t min_neigs = 1; - static size_t max_neigs = 2000; - - if(max_neigs > mesh->n_vertices) - max_neigs = mesh->n_vertices; + static size_t max_neigs = std::min(1000lu, mesh->n_vertices); + static fairing_spectral fair(max_neigs); if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &max_neigs)) { diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 2fff05bd..9cc9d1bb 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -48,7 +48,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - A(v, v) = mesh->area_vertex(v); + A(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); } void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) @@ -77,7 +77,7 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) A.reserve(VectorXi::Constant(n_vertices, 1)); for(index_t v = 0; v < n_vertices; ++v) - A.insert(v, v) = mesh->area_vertex(v); + A.insert(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); } size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) @@ -89,10 +89,7 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) { - // a_sp_mat D = sqrt(A); - // D.for_each([](a_sp_mat::elem_type & val) { val = 1. / val; }); - - if(!eigs_sym(eigval, eigvec, L, k, "sm")) + if(!eigs_sym(eigval, eigvec, A * L * A, k, "sm")) return 0; eigval.save(feigval); From 564ba4eb2b01ea8c116c3e6c8271eef069452368 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 20 Aug 2021 18:41:53 +0200 Subject: [PATCH 0539/1018] Adding DOI Zenodo release --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 037a04d1..2d23539e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ ## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework +![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) +[![DOI](https://zenodo.org/badge/88686093.svg)](https://zenodo.org/badge/latestdoi/88686093) + + ![](https://raw.githubusercontent.com/larc/gproshan/master/gproshan.png) @@ -8,7 +12,6 @@ This framework integrates some algorithms and contributions focus on the areas o ## Build and Run -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) Install all dependencies and run: From 5e94b46b64b781f8e991bc7e3dffc92c95019ec1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 20 Aug 2021 18:45:13 +0200 Subject: [PATCH 0540/1018] update build status badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d23539e..79eba5d9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework -![Build](https://github.com/larc/gproshan_dev/workflows/Build/badge.svg?branch=dev) +[![Build](https://github.com/larc/gproshan_dev/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/larc/gproshan_dev/actions/workflows/build.yml) [![DOI](https://zenodo.org/badge/88686093.svg)](https://zenodo.org/badge/latestdoi/88686093) From fb9a773f4584ffbd412964b59a78d18c1c3f2c2d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 20 Aug 2021 18:51:12 +0200 Subject: [PATCH 0541/1018] Create CITATION.cff --- CITATION.cff | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CITATION.cff diff --git a/CITATION.cff b/CITATION.cff new file mode 100644 index 00000000..72874f69 --- /dev/null +++ b/CITATION.cff @@ -0,0 +1,14 @@ +cff-version: 1.2.0 +message: "If you use this software, please cite it as below." +authors: +- family-names: "Romero Calla" + given-names: "Luciano Arnaldo" + orcid: "https://orcid.org/0000-0002-6186-4027" +- family-names: "Fuentes Perez" + given-names: "Lizeth Joseline" + orcid: "https://orcid.org/0000-0003-1096-2871" +title: "gproshan" +version: 3.0-alpha +doi: 10.5281/zenodo.5227555 +date-released: 2021-08-20 +url: "https://github.com/larc/gproshan" From e68da0ad926e0f3540474a8275b07277fc078a2d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 21 Aug 2021 17:44:28 +0200 Subject: [PATCH 0542/1018] viewer: load mesh time in status bar --- src/app_viewer.cpp | 6 +----- src/viewer/viewer.cpp | 1 + 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 3f7d23e5..f144e60a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -42,14 +42,10 @@ int app_viewer::main(int nargs, const char ** args) } TIC(time) - for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); - TOC(time) - - gproshan_log_var(sizeof(real_t)); - gproshan_log_var(time); + sprintf(status_message, "meshes loaded in %.2fs", time); init(); run(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 50952080..21c9e3cd 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -54,6 +54,7 @@ viewer::viewer(int width, int height): window_width(width), window_height(height init_menus(); info_gl(); + gproshan_log_var(sizeof(real_t)); sphere.init(new che_sphere(0.01), false); } From d267a3be7524ad8828fd2abbe615bae87bb4e748 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 21 Aug 2021 17:59:13 +0200 Subject: [PATCH 0543/1018] viewer: build embree time on status bar --- src/app_viewer.cpp | 2 +- src/viewer/viewer.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index f144e60a..c693c2ee 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -45,7 +45,7 @@ int app_viewer::main(int nargs, const char ** args) for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); TOC(time) - sprintf(status_message, "meshes loaded in %.2fs", time); + sprintf(status_message, "meshes loaded in %.3fs", time); init(); run(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 21c9e3cd..f799033b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -657,6 +657,7 @@ bool viewer::set_render_embree(viewer * view) break; } TOC(time); + sprintf(view->status_message, "build embree in %.3fs", time); if(!view->render_frame) view->render_frame = new frame; From 8d23668ae4d2bffa2e50d40ecf4b1926b125aea3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 31 Aug 2021 12:59:11 +0200 Subject: [PATCH 0544/1018] update imgui to version 1.84.2 --- CMakeLists.txt | 7 +- imgui/CMakeLists.txt | 6 + imgui/imconfig.h | 11 +- imgui/imgui.cpp | 1169 +++++++++++++++++++----------- imgui/imgui.h | 267 ++++--- imgui/imgui_demo.cpp | 356 +++++---- imgui/imgui_draw.cpp | 239 +++--- imgui/imgui_impl_glfw.cpp | 275 ++++--- imgui/imgui_impl_glfw.h | 7 +- imgui/imgui_impl_opengl3.cpp | 374 ++++++---- imgui/imgui_impl_opengl3.h | 42 +- imgui/imgui_internal.h | 454 ++++++++---- imgui/imgui_tables.cpp | 332 ++++++--- imgui/imgui_widgets.cpp | 900 ++++++++++++++--------- imgui/imstb_rectpack.h | 52 +- imgui/imstb_textedit.h | 82 ++- imgui/imstb_truetype.h | 108 +-- include/gproshan/viewer/viewer.h | 6 +- 18 files changed, 2858 insertions(+), 1829 deletions(-) create mode 100644 imgui/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index d576224d..39562d1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,15 +68,10 @@ include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) +add_subdirectory(imgui) include_directories(SYSTEM ${gproshan_SOURCE_DIR}/imgui) -FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) -add_library(imgui SHARED ${imgui_sources}) -target_link_libraries(imgui GLEW::GLEW) -target_link_libraries(imgui glfw) - - include_directories(${gproshan_SOURCE_DIR}/include/gproshan) add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") diff --git a/imgui/CMakeLists.txt b/imgui/CMakeLists.txt new file mode 100644 index 00000000..8456f44b --- /dev/null +++ b/imgui/CMakeLists.txt @@ -0,0 +1,6 @@ +FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) + +add_library(imgui SHARED ${imgui_sources}) +target_link_libraries(imgui GLEW::GLEW) +target_link_libraries(imgui glfw) + diff --git a/imgui/imconfig.h b/imgui/imconfig.h index f774eaf5..a0c86caa 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -27,7 +27,7 @@ //#define IMGUI_API __declspec( dllimport ) //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. -#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS +//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS //---- Disable all of Dear ImGui or don't implement standard windows. // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. @@ -37,13 +37,16 @@ //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) -//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow. (imm32.lib/.a) +//#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW) +//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a) //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. -//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. +//#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) +//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function. //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). +//#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available //---- Include imgui_user.h at the end of imgui.h as a convenience //#define IMGUI_INCLUDE_IMGUI_USER_H @@ -67,7 +70,7 @@ //---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui) // Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided). -// On Windows you may use vcpkg with 'vcpkg install freetype' + 'vcpkg integrate install'. +// On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'. //#define IMGUI_ENABLE_FREETYPE //---- Use stb_truetype to build and rasterize the font atlas (default) diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index 94cd4286..ab27f130 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (main code and documentation) // Help: @@ -11,16 +11,16 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/3488 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/4451 (please post your screenshots/video there!) +// - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary -// - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues // - Discussions https://github.com/ocornut/imgui/discussions // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). // This library is free but needs your support to sustain development and maintenance. -// Businesses: you can support continued development via invoiced technical support, maintenance and sponsoring contracts. Please reach out to "contact AT dearimgui.org". +// Businesses: you can support continued development via invoiced technical support, maintenance and sponsoring contracts. Please reach out to "contact AT dearimgui.com". // Individuals: you can support continued development via donations. See docs/README or web page. // It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. @@ -134,17 +134,17 @@ CODE READ FIRST ---------- - - Remember to read the FAQ (https://www.dearimgui.org/faq) - - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction - or destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, less bugs. + - Remember to check the wonderful Wiki (https://github.com/ocornut/imgui/wiki) + - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction or + destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, fewer bugs. - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build. - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori). - You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links in the FAQ. + You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links in Wiki. - Dear ImGui is a "single pass" rasterizing implementation of the IMGUI paradigm, aimed at ease of use and high-performances. - For every application frame your UI code will be called only once. This is in contrast to e.g. Unity's own implementation of an IMGUI, + For every application frame, your UI code will be called only once. This is in contrast to e.g. Unity's implementation of an IMGUI, where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches. - - Our origin are on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right. + - Our origin is on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right. - This codebase is also optimized to yield decent performances with typical "Debug" builds settings. - Please make sure you have asserts enabled (IM_ASSERT redirects to assert() by default, but can be redirected). If you get an assert, read the messages and comments around the assert. @@ -157,8 +157,8 @@ CODE HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI ---------------------------------------------- - - Overwrite all the sources files except for imconfig.h (if you have made modification to your copy of imconfig.h) - - Or maintain your own branch where you have imconfig.h modified as a top-most commit which you can regularly rebase over master. + - Overwrite all the sources files except for imconfig.h (if you have modified your copy of imconfig.h) + - Or maintain your own branch where you have imconfig.h modified as a top-most commit which you can regularly rebase over "master". - You can also use '#define IMGUI_USER_CONFIG "my_config_file.h" to redirect configuration to your own file. - Read the "API BREAKING CHANGES" section (below). This is where we list occasional API breaking changes. If a function/type has been renamed / or marked obsolete, try to fix the name in your code before it is permanently removed @@ -173,12 +173,12 @@ CODE - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - In the majority of cases you should be able to use unmodified backends files available in the backends/ folder. - Add the Dear ImGui source files + selected backend source files to your projects or using your preferred build system. - It is recommended you build and statically link the .cpp files as part of your project and NOT as shared library (DLL). + It is recommended you build and statically link the .cpp files as part of your project and NOT as a shared library (DLL). - You can later customize the imconfig.h file to tweak some compile-time behavior, such as integrating Dear ImGui types with your own maths types. - When using Dear ImGui, your programming IDE is your friend: follow the declaration of variables, functions and types to find comments about them. - Dear ImGui never touches or knows about your GPU state. The only function that knows about GPU is the draw function that you provide. Effectively it means you can create widgets at any time in your code, regardless of considerations of being in "update" vs "render" - phases of your own application. All rendering information are stored into command-lists that you will retrieve after calling ImGui::Render(). + phases of your own application. All rendering information is stored into command-lists that you will retrieve after calling ImGui::Render(). - Refer to the backends and demo applications in the examples/ folder for instruction on how to setup your code. - If you are running over a standard OS with a common graphics API, you should be able to use unmodified imgui_impl_*** files from the examples/ folder. @@ -186,7 +186,7 @@ CODE HOW A SIMPLE APPLICATION MAY LOOK LIKE -------------------------------------- EXHIBIT 1: USING THE EXAMPLE BACKENDS (= imgui_impl_XXX.cpp files from the backends/ folder). - The sub-folders in examples/ contains examples applications following this structure. + The sub-folders in examples/ contain examples applications following this structure. // Application init: create a dear imgui context, setup some options, load fonts ImGui::CreateContext(); @@ -236,7 +236,7 @@ CODE unsigned char* pixels = NULL; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); - // At this point you've got the texture data and you need to upload that your your graphic system: + // At this point you've got the texture data and you need to upload that to your graphic system: // After we have created the texture, store its pointer/identifier (_in whichever format your engine uses_) in 'io.Fonts->TexID'. // This will be passed back to your via the renderer. Basically ImTextureID == void*. Read FAQ for details about ImTextureID. MyTexture* texture = MyEngine::CreateTextureFromMemoryPixels(pixels, width, height, TEXTURE_TYPE_RGBA32) @@ -255,7 +255,7 @@ CODE io.MouseDown[1] = my_mouse_buttons[1]; // Call NewFrame(), after this point you can use ImGui::* functions anytime - // (So you want to try calling NewFrame() as early as you can in your mainloop to be able to use Dear ImGui everywhere) + // (So you want to try calling NewFrame() as early as you can in your main loop to be able to use Dear ImGui everywhere) ImGui::NewFrame(); // Most of your application code here @@ -275,14 +275,14 @@ CODE // Shutdown ImGui::DestroyContext(); - To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest your application, + To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest of your application, you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! Please read the FAQ and example applications for details about this! HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE --------------------------------------------- - The backends in impl_impl_XXX.cpp files contains many working implementations of a rendering function. + The backends in impl_impl_XXX.cpp files contain many working implementations of a rendering function. void void MyImGuiRenderFunction(ImDrawData* draw_data) { @@ -304,11 +304,11 @@ CODE } else { - // The texture for the draw call is specified by pcmd->TextureId. + // The texture for the draw call is specified by pcmd->GetTexID(). // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization. - MyEngineBindTexture((MyTexture*)pcmd->TextureId); + MyEngineBindTexture((MyTexture*)pcmd->GetTexID()); - // We are using scissoring to clip some objects. All low-level graphics API should supports it. + // We are using scissoring to clip some objects. All low-level graphics API should support it. // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches // (some elements visible outside their bounds) but you can fix that once everything else works! // - Clipping coordinates are provided in imgui coordinates space: @@ -351,7 +351,7 @@ CODE Note that io.NavInputs[] is cleared by EndFrame(). - See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values: 0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks. - - We uses a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. + - We use a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. Your code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://dearimgui.org/controls_sheets - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo @@ -363,7 +363,7 @@ CODE Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. When that happens your backend NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the backends in examples/ do that. - (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse as moving back and forth!) + (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse moving back and forth!) (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want to set a boolean to ignore your other external mouse positions until the external source is moved again.) @@ -373,9 +373,15 @@ CODE Occasionally introducing changes that are breaking the API. We try to make the breakage minor and easy to fix. Below is a change-log of API breaking changes only. If you are using one of the functions listed, expect to have to fix some code. - When you are not sure about a old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. + When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2021/07/26 (1.84) - commented out redirecting functions/enums names that were marked obsolete in 1.67 and 1.69 (March 2019): + - ImGui::GetOverlayDrawList() -> use ImGui::GetForegroundDrawList() + - ImFont::GlyphRangesBuilder -> use ImFontGlyphRangesBuilder + - 2021/05/19 (1.83) - backends: obsoleted direct access to ImDrawCmd::TextureId in favor of calling ImDrawCmd::GetTexID(). + - if you are using official backends from the source tree: you have nothing to do. + - if you have copied old backend code or using your own: change access to draw_cmd->TextureId to draw_cmd->GetTexID(). - 2021/03/12 (1.82) - upgraded ImDrawList::AddRect(), AddRectFilled(), PathRect() to use ImDrawFlags instead of ImDrawCornersFlags. - ImDrawCornerFlags_TopLeft -> use ImDrawFlags_RoundCornersTopLeft - ImDrawCornerFlags_BotRight -> use ImDrawFlags_RoundCornersBottomRight @@ -393,7 +399,7 @@ CODE - ImGui::SetScrollHere() -> use ImGui::SetScrollHereY() - 2021/03/11 (1.82) - clarified that ImDrawList::PathArcTo(), ImDrawList::PathArcToFast() won't render with radius < 0.0f. Previously it sorts of accidentally worked but would generally lead to counter-clockwise paths and have an effect on anti-aliasing. - 2021/03/10 (1.82) - upgraded ImDrawList::AddPolyline() and PathStroke() "bool closed" parameter to "ImDrawFlags flags". The matching ImDrawFlags_Closed value is guaranteed to always stay == 1 in the future. - - 2021/02/22 (1.82) - win32+mingw: Re-enabled IME functions by default even under MinGW. In July 2016, issue #738 had me incorrectly disable those default functions for MinGW. MinGW users should: either link with -limm32, either set their imconfig file with '#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS'. + - 2021/02/22 (1.82) - (*undone in 1.84*) win32+mingw: Re-enabled IME functions by default even under MinGW. In July 2016, issue #738 had me incorrectly disable those default functions for MinGW. MinGW users should: either link with -limm32, either set their imconfig file with '#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS'. - 2021/02/17 (1.82) - renamed rarely used style.CircleSegmentMaxError (old default = 1.60f) to style.CircleTessellationMaxError (new default = 0.30f) as the meaning of the value changed. - 2021/02/03 (1.81) - renamed ListBoxHeader(const char* label, ImVec2 size) to BeginListBox(). Kept inline redirection function (will obsolete). - removed ListBoxHeader(const char* label, int items_count, int height_in_items = -1) in favor of specifying size. Kept inline redirection function (will obsolete). @@ -424,7 +430,7 @@ CODE - 2020/09/25 (1.79) - renamed ImGuiSliderFlags_ClampOnInput to ImGuiSliderFlags_AlwaysClamp. Kept redirection enum (will obsolete sooner because previous name was added recently). - 2020/09/25 (1.79) - renamed style.TabMinWidthForUnselectedCloseButton to style.TabMinWidthForCloseButton. - 2020/09/21 (1.79) - renamed OpenPopupContextItem() back to OpenPopupOnItemClick(), reverting the change from 1.77. For varieties of reason this is more self-explanatory. - - 2020/09/21 (1.79) - removed return value from OpenPopupOnItemClick() - returned true on mouse release on item - because it is inconsistent with other popup APIs and makes others misleading. It's also and unnecessary: you can use IsWindowAppearing() after BeginPopup() for a similar result. + - 2020/09/21 (1.79) - removed return value from OpenPopupOnItemClick() - returned true on mouse release on an item - because it is inconsistent with other popup APIs and makes others misleading. It's also and unnecessary: you can use IsWindowAppearing() after BeginPopup() for a similar result. - 2020/09/17 (1.79) - removed ImFont::DisplayOffset in favor of ImFontConfig::GlyphOffset. DisplayOffset was applied after scaling and not very meaningful/useful outside of being needed by the default ProggyClean font. If you scaled this value after calling AddFontDefault(), this is now done automatically. It was also getting in the way of better font scaling, so let's get rid of it now! - 2020/08/17 (1.78) - obsoleted use of the trailing 'float power=1.0f' parameter for DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(), DragFloatRange2(), DragScalar(), DragScalarN(), SliderFloat(), SliderFloat2(), SliderFloat3(), SliderFloat4(), SliderScalar(), SliderScalarN(), VSliderFloat() and VSliderScalar(). replaced the 'float power=1.0f' argument with integer-based flags defaulting to 0 (as with all our flags). @@ -433,7 +439,7 @@ CODE - if you set the 'power' parameter to 1.0f (same as previous default value): 1/ your compiler may warn on float>int conversion, 2/ everything else will work. 3/ you can replace the 1.0f value with 0 to fix the warning, and be technically correct. - if you set the 'power' parameter to >1.0f (to enable non-linear editing): 1/ your compiler may warn on float>int conversion, 2/ code will assert at runtime, 3/ in case asserts are disabled, the code will not crash and enable the _Logarithmic flag. 4/ you can replace the >1.0f value with ImGuiSliderFlags_Logarithmic to fix the warning/assert and get a _similar_ effect as previous uses of power >1.0f. see https://github.com/ocornut/imgui/issues/3361 for all details. - kept inline redirection functions (will obsolete) apart for: DragFloatRange2(), VSliderFloat(), VSliderScalar(). For those three the 'float power=1.0f' version were removed directly as they were most unlikely ever used. + kept inline redirection functions (will obsolete) apart for: DragFloatRange2(), VSliderFloat(), VSliderScalar(). For those three the 'float power=1.0f' version was removed directly as they were most unlikely ever used. for shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. - obsoleted use of v_min > v_max in DragInt, DragFloat, DragScalar to lock edits (introduced in 1.73, was not demoed nor documented very), will be replaced by a more generic ReadOnly feature. You may use the ImGuiSliderFlags_ReadOnly internal flag in the meantime. - 2020/06/23 (1.77) - removed BeginPopupContextWindow(const char*, int mouse_button, bool also_over_items) in favor of BeginPopupContextWindow(const char*, ImGuiPopupFlags flags) with ImGuiPopupFlags_NoOverItems. @@ -453,7 +459,7 @@ CODE - ImGuiStyleVar_ChildWindowRounding -> use ImGuiStyleVar_ChildRounding - ImGuiTreeNodeFlags_AllowOverlapMode -> use ImGuiTreeNodeFlags_AllowItemOverlap - IMGUI_DISABLE_TEST_WINDOWS -> use IMGUI_DISABLE_DEMO_WINDOWS - - 2019/12/08 (1.75) - obsoleted calling ImDrawList::PrimReserve() with a negative count (which was the vaguely documented and rarely if ever used). Instead we added an explicit PrimUnreserve() API. + - 2019/12/08 (1.75) - obsoleted calling ImDrawList::PrimReserve() with a negative count (which was vaguely documented and rarely if ever used). Instead, we added an explicit PrimUnreserve() API. - 2019/12/06 (1.75) - removed implicit default parameter to IsMouseDragging(int button = 0) to be consistent with other mouse functions (none of the other functions have it). - 2019/11/21 (1.74) - ImFontAtlas::AddCustomRectRegular() now requires an ID larger than 0x110000 (instead of 0x10000) to conform with supporting Unicode planes 1-16 in a future update. ID below 0x110000 will now assert. - 2019/11/19 (1.74) - renamed IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS to IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS for consistency. @@ -481,7 +487,7 @@ CODE - 2019/04/29 (1.70) - removed GetContentRegionAvailWidth(), use GetContentRegionAvail().x instead. Kept inline redirection function (will obsolete). - 2019/03/04 (1.69) - renamed GetOverlayDrawList() to GetForegroundDrawList(). Kept redirection function (will obsolete). - 2019/02/26 (1.69) - renamed ImGuiColorEditFlags_RGB/ImGuiColorEditFlags_HSV/ImGuiColorEditFlags_HEX to ImGuiColorEditFlags_DisplayRGB/ImGuiColorEditFlags_DisplayHSV/ImGuiColorEditFlags_DisplayHex. Kept redirection enums (will obsolete). - - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with an arbitrary small value! + - 2019/02/14 (1.68) - made it illegal/assert when io.DisplayTime == 0.0f (with an exception for the first frame). If for some reason your time step calculation gives you a zero value, replace it with an arbitrarily small value! - 2019/02/01 (1.68) - removed io.DisplayVisibleMin/DisplayVisibleMax (which were marked obsolete and removed from viewport/docking branch already). - 2019/01/06 (1.67) - renamed io.InputCharacters[], marked internal as was always intended. Please don't access directly, and use AddInputCharacter() instead! - 2019/01/06 (1.67) - renamed ImFontAtlas::GlyphRangesBuilder to ImFontGlyphRangesBuilder. Kept redirection typedef (will obsolete). @@ -576,12 +582,12 @@ CODE - 2017/07/20 (1.51) - removed IsPosHoveringAnyWindow(ImVec2), which was partly broken and misleading. ASSERT + redirect user to io.WantCaptureMouse - 2017/05/26 (1.50) - removed ImFontConfig::MergeGlyphCenterV in favor of a more multipurpose ImFontConfig::GlyphOffset. - 2017/05/01 (1.50) - renamed ImDrawList::PathFill() (rarely used directly) to ImDrawList::PathFillConvex() for clarity. - - 2016/11/06 (1.50) - BeginChild(const char*) now applies the stack id to the provided label, consistently with other functions as it should always have been. It shouldn't affect you unless (extremely unlikely) you were appending multiple times to a same child from different locations of the stack id. If that's the case, generate an id with GetId() and use it instead of passing string to BeginChild(). + - 2016/11/06 (1.50) - BeginChild(const char*) now applies the stack id to the provided label, consistently with other functions as it should always have been. It shouldn't affect you unless (extremely unlikely) you were appending multiple times to a same child from different locations of the stack id. If that's the case, generate an id with GetID() and use it instead of passing string to BeginChild(). - 2016/10/15 (1.50) - avoid 'void* user_data' parameter to io.SetClipboardTextFn/io.GetClipboardTextFn pointers. We pass io.ClipboardUserData to it. - 2016/09/25 (1.50) - style.WindowTitleAlign is now a ImVec2 (ImGuiAlign enum was removed). set to (0.5f,0.5f) for horizontal+vertical centering, (0.0f,0.0f) for upper-left, etc. - - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully breakage should be minimal. + - 2016/07/30 (1.50) - SameLine(x) with x>0.0f is now relative to left of column/group if any, and not always to left of window. This was sort of always the intent and hopefully, breakage should be minimal. - 2016/05/12 (1.49) - title bar (using ImGuiCol_TitleBg/ImGuiCol_TitleBgActive colors) isn't rendered over a window background (ImGuiCol_WindowBg color) anymore. - If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you, otherwise if <1.0f you need tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. + If your TitleBg/TitleBgActive alpha was 1.0f or you are using the default theme it will not affect you, otherwise if <1.0f you need to tweak your custom theme to readjust for the fact that we don't draw a WindowBg background behind the title bar. This helper function will convert an old TitleBg/TitleBgActive color into a new one with the same visual output, given the OLD color and the OLD WindowBg color: ImVec4 ConvertTitleBgCol(const ImVec4& win_bg_col, const ImVec4& title_bg_col) { float new_a = 1.0f - ((1.0f - win_bg_col.w) * (1.0f - title_bg_col.w)), k = title_bg_col.w / new_a; return ImVec4((win_bg_col.x * win_bg_col.w + title_bg_col.x) * k, (win_bg_col.y * win_bg_col.w + title_bg_col.y) * k, (win_bg_col.z * win_bg_col.w + title_bg_col.z) * k, new_a); } If this is confusing, pick the RGB value from title bar from an old screenshot and apply this as TitleBg/TitleBgActive. Or you may just create TitleBgActive from a tweaked TitleBg color. @@ -589,7 +595,7 @@ CODE - 2016/05/02 (1.49) - renamed SetNextTreeNodeOpened() to SetNextTreeNodeOpen(), no redirection. - 2016/05/01 (1.49) - obsoleted old signature of CollapsingHeader(const char* label, const char* str_id = NULL, bool display_frame = true, bool default_open = false) as extra parameters were badly designed and rarely used. You can replace the "default_open = true" flag in new API with CollapsingHeader(label, ImGuiTreeNodeFlags_DefaultOpen). - 2016/04/26 (1.49) - changed ImDrawList::PushClipRect(ImVec4 rect) to ImDrawList::PushClipRect(Imvec2 min,ImVec2 max,bool intersect_with_current_clip_rect=false). Note that higher-level ImGui::PushClipRect() is preferable because it will clip at logic/widget level, whereas ImDrawList::PushClipRect() only affect your renderer. - - 2016/04/03 (1.48) - removed style.WindowFillAlphaDefault setting which was redundant. Bake default BG alpha inside style.Colors[ImGuiCol_WindowBg] and all other Bg color values. (ref github issue #337). + - 2016/04/03 (1.48) - removed style.WindowFillAlphaDefault setting which was redundant. Bake default BG alpha inside style.Colors[ImGuiCol_WindowBg] and all other Bg color values. (ref GitHub issue #337). - 2016/04/03 (1.48) - renamed ImGuiCol_TooltipBg to ImGuiCol_PopupBg, used by popups/menus and tooltips. popups/menus were previously using ImGuiCol_WindowBg. (ref github issue #337) - 2016/03/21 (1.48) - renamed GetWindowFont() to GetFont(), GetWindowFontSize() to GetFontSize(). Kept inline redirection function (will obsolete). - 2016/03/02 (1.48) - InputText() completion/history/always callbacks: if you modify the text buffer manually (without using DeleteChars()/InsertChars() helper) you need to maintain the BufTextLen field. added an assert. @@ -620,7 +626,7 @@ CODE - 2015/07/02 (1.42) - renamed SetScrollPosHere() to SetScrollFromCursorPos(). Kept inline redirection function (will obsolete). - 2015/07/02 (1.42) - renamed GetScrollPosY() to GetScrollY(). Necessary to reduce confusion along with other scrolling functions, because positions (e.g. cursor position) are not equivalent to scrolling amount. - 2015/06/14 (1.41) - changed ImageButton() default bg_col parameter from (0,0,0,1) (black) to (0,0,0,0) (transparent) - makes a difference when texture have transparence - - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely be used. Sorry! + - 2015/06/14 (1.41) - changed Selectable() API from (label, selected, size) to (label, selected, flags, size). Size override should have been rarely used. Sorry! - 2015/05/31 (1.40) - renamed GetWindowCollapsed() to IsWindowCollapsed() for consistency. Kept inline redirection function (will obsolete). - 2015/05/31 (1.40) - renamed IsRectClipped() to IsRectVisible() for consistency. Note that return value is opposite! Kept inline redirection function (will obsolete). - 2015/05/27 (1.40) - removed the third 'repeat_if_held' parameter from Button() - sorry! it was rarely used and inconsistent. Use PushButtonRepeat(true) / PopButtonRepeat() to enable repeat on desired buttons. @@ -675,7 +681,7 @@ CODE =========== Q: Where is the documentation? - A: This library is poorly documented at the moment and expects of the user to be acquainted with C/C++. + A: This library is poorly documented at the moment and expects the user to be acquainted with C/C++. - Run the examples/ and explore them. - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. - The demo covers most features of Dear ImGui, so you can read the code and see its output. @@ -685,7 +691,7 @@ CODE - The Wiki (https://github.com/ocornut/imgui/wiki) has many resources and links. - The Glossary (https://github.com/ocornut/imgui/wiki/Glossary) page also may be useful. - Your programming IDE is your friend, find the type or function declaration to find comments - associated to it. + associated with it. Q: What is this library called? Q: Which version should I get? @@ -698,15 +704,15 @@ CODE Q: How to get started? A: Read 'PROGRAMMER GUIDE' above. Read examples/README.txt. - Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or to my application? + Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application? A: You should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! - >> See https://www.dearimgui.org/faq for fully detailed answer. You really want to read this. + >> See https://www.dearimgui.org/faq for a fully detailed answer. You really want to read this. Q. How can I enable keyboard controls? Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) - Q: I integrated Dear ImGui in my engine and little squares are showing instead of text.. - Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around.. - Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries.. + Q: I integrated Dear ImGui in my engine and little squares are showing instead of text... + Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around... + Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries... >> See https://www.dearimgui.org/faq Q&A: Usage @@ -744,16 +750,16 @@ CODE ============== Q: How can I help? - A: - Businesses: please reach out to "contact AT dearimgui.org" if you work in a place using Dear ImGui! + A: - Businesses: please reach out to "contact AT dearimgui.com" if you work in a place using Dear ImGui! We can discuss ways for your company to fund development via invoiced technical support, maintenance or sponsoring contacts. - This is among the most useful thing you can do for Dear ImGui. With increased funding we can hire more people working on this project. + This is among the most useful thing you can do for Dear ImGui. With increased funding, we can hire more people working on this project. - Individuals: you can support continued development via PayPal donations. See README. - - If you are experienced with Dear ImGui and C++, look at the github issues, look at the Wiki, read docs/TODO.txt + - If you are experienced with Dear ImGui and C++, look at the GitHub issues, look at the Wiki, read docs/TODO.txt and see how you want to help and can help! - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. - You may post screenshot or links in the gallery threads (github.com/ocornut/imgui/issues/3488). Visuals are ideal as they inspire other programmers. - But even without visuals, disclosing your use of dear imgui help the library grow credibility, and help other teams and programmers with taking decisions. - - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on github or privately). + You may post screenshot or links in the gallery threads. Visuals are ideal as they inspire other programmers. + But even without visuals, disclosing your use of dear imgui helps the library grow credibility, and help other teams and programmers with taking decisions. + - If you have issues or if you need to hack into the library, even if you don't expect any support it is useful that you share your issues (on GitHub or privately). */ @@ -782,6 +788,11 @@ CODE #include // intptr_t #endif +// [Windows] On non-Visual Studio compilers, we default to IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS unless explicitly enabled +#if defined(_WIN32) && !defined(_MSC_VER) && !defined(IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) +#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS +#endif + // [Windows] OS specific includes (optional) #if defined(_WIN32) && defined(IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) #define IMGUI_DISABLE_WIN32_FUNCTIONS @@ -816,6 +827,9 @@ CODE #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types #endif +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26495) // [Static Analyzer] Variable 'XXX' is uninitialized. Always initialize a member variable (type.6). +#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). #endif // Clang/GCC warnings with -Weverything @@ -858,7 +872,7 @@ static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear // Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by backend) -static const float WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS = 4.0f; // Extend outside and inside windows. Affect FindHoveredWindow(). +static const float WINDOWS_HOVER_PADDING = 4.0f; // Extend outside window for hovering/resizing (maxxed with TouchPadding) and inside windows for borders. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. @@ -897,8 +911,8 @@ static void NavUpdateInitResult(); static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); static void NavEndFrame(); -static bool NavScoreItem(ImGuiNavMoveResult* result, ImRect cand); -static void NavApplyItemToResult(ImGuiNavMoveResult* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel); +static bool NavScoreItem(ImGuiNavItemData* result, ImRect cand); +static void NavApplyItemToResult(ImGuiNavItemData* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel); static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); @@ -933,7 +947,7 @@ static void UpdateViewportsNewFrame(); // DLL users: // - Heaps and globals are not shared across DLL boundaries! // - You will need to call SetCurrentContext() + SetAllocatorFunctions() for each static/DLL boundary you are calling from. -// - Same apply for hot-reloading mechanisms that are reliant on reloading DLL (note that many hot-reloading mechanism works without DLL). +// - Same applies for hot-reloading mechanisms that are reliant on reloading DLL (note that many hot-reloading mechanisms work without DLL). // - Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. // - Confused? In a debugger: add GImGui to your watch window and notice how its value changes depending on your current location (which DLL boundary you are in). @@ -946,16 +960,16 @@ static void UpdateViewportsNewFrame(); // struct ImGuiContext; // extern thread_local ImGuiContext* MyImGuiTLS; // #define GImGui MyImGuiTLS -// And then define MyImGuiTLS in one of your cpp file. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword. -// - Future development aim to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 -// - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from different namespace. +// And then define MyImGuiTLS in one of your cpp files. Note that thread_local is a C++11 keyword, earlier C++ uses compiler-specific keyword. +// - Future development aims to make this context pointer explicit to all calls. Also read https://github.com/ocornut/imgui/issues/586 +// - If you need a finite number of contexts, you may compile and use multiple instances of the ImGui code from a different namespace. // - DLL users: read comments above. #ifndef GImGui ImGuiContext* GImGui = NULL; #endif // Memory Allocator functions. Use SetAllocatorFunctions() to change them. -// - You probably don't want to modify those mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction. +// - You probably don't want to modify that mid-program, and if you use global/static e.g. ImVector<> instances you may need to keep them accessible during program destruction. // - DLL users: read comments above. #ifndef IMGUI_DISABLE_DEFAULT_ALLOCATORS static void* MallocWrapper(size_t size, void* user_data) { IM_UNUSED(user_data); return malloc(size); } @@ -974,7 +988,8 @@ static void* GImAllocatorUserData = NULL; ImGuiStyle::ImGuiStyle() { - Alpha = 1.0f; // Global alpha applies to everything in ImGui + Alpha = 1.0f; // Global alpha applies to everything in Dear ImGui. + DisabledAlpha = 0.60f; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. WindowPadding = ImVec2(8,8); // Padding within a window WindowRounding = 0.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. @@ -1059,7 +1074,7 @@ ImGuiIO::ImGuiIO() DisplaySize = ImVec2(-1.0f, -1.0f); DeltaTime = 1.0f / 60.0f; IniSavingRate = 5.0f; - IniFilename = "imgui.ini"; + IniFilename = "imgui.ini"; // Important: "imgui.ini" is relative to current working dir, most apps will want to lock this to an absolute path (e.g. same path as executables). LogFilename = "imgui_log.txt"; MouseDoubleClickTime = 0.30f; MouseDoubleClickMaxDist = 6.0f; @@ -1133,11 +1148,18 @@ void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) if (InputQueueSurrogate != 0) { if ((c & 0xFC00) != 0xDC00) // Invalid low surrogate + { InputQueueCharacters.push_back(IM_UNICODE_CODEPOINT_INVALID); - else if (IM_UNICODE_CODEPOINT_MAX == (0xFFFF)) // Codepoint will not fit in ImWchar (extra parenthesis around 0xFFFF somehow fixes -Wunreachable-code with Clang) - cp = IM_UNICODE_CODEPOINT_INVALID; + } else + { +#if IM_UNICODE_CODEPOINT_MAX == 0xFFFF + cp = IM_UNICODE_CODEPOINT_INVALID; // Codepoint will not fit in ImWchar +#else cp = (ImWchar)(((InputQueueSurrogate - 0xD800) << 10) + (c - 0xDC00) + 0x10000); +#endif + } + InputQueueSurrogate = 0; } InputQueueCharacters.push_back(cp); @@ -1159,6 +1181,22 @@ void ImGuiIO::ClearInputCharacters() InputQueueCharacters.resize(0); } +void ImGuiIO::AddFocusEvent(bool focused) +{ + if (focused) + return; + + // Clear buttons state when focus is lost + // (this is useful so e.g. releasing Alt after focus loss on Alt-Tab doesn't trigger the Alt menu toggle) + memset(KeysDown, 0, sizeof(KeysDown)); + for (int n = 0; n < IM_ARRAYSIZE(KeysDownDuration); n++) + KeysDownDuration[n] = KeysDownDurationPrev[n] = -1.0f; + KeyCtrl = KeyShift = KeyAlt = KeySuper = false; + KeyMods = KeyModsPrev = ImGuiKeyModFlags_None; + for (int n = 0; n < IM_ARRAYSIZE(NavInputsDownDuration); n++) + NavInputsDownDuration[n] = NavInputsDownDurationPrev[n] = -1.0f; +} + //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (Geometry functions) //----------------------------------------------------------------------------- @@ -1688,7 +1726,7 @@ int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) } // Based on stb_to_utf8() from github.com/nothings/stb/ -static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) +static inline int ImTextCharToUtf8_inline(char* buf, int buf_size, unsigned int c) { if (c < 0x80) { @@ -1723,6 +1761,13 @@ static inline int ImTextCharToUtf8(char* buf, int buf_size, unsigned int c) return 0; } +const char* ImTextCharToUtf8(char out_buf[5], unsigned int c) +{ + int count = ImTextCharToUtf8_inline(out_buf, 5, c); + out_buf[count] = 0; + return out_buf; +} + // Not optimal but we very rarely use this function. int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end) { @@ -1739,20 +1784,20 @@ static inline int ImTextCountUtf8BytesFromChar(unsigned int c) return 3; } -int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end) +int ImTextStrToUtf8(char* out_buf, int out_buf_size, const ImWchar* in_text, const ImWchar* in_text_end) { - char* buf_out = buf; - const char* buf_end = buf + buf_size; - while (buf_out < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text) + char* buf_p = out_buf; + const char* buf_end = out_buf + out_buf_size; + while (buf_p < buf_end - 1 && (!in_text_end || in_text < in_text_end) && *in_text) { unsigned int c = (unsigned int)(*in_text++); if (c < 0x80) - *buf_out++ = (char)c; + *buf_p++ = (char)c; else - buf_out += ImTextCharToUtf8(buf_out, (int)(buf_end - buf_out - 1), c); + buf_p += ImTextCharToUtf8_inline(buf_p, (int)(buf_end - buf_p - 1), c); } - *buf_out = 0; - return (int)(buf_out - buf); + *buf_p = 0; + return (int)(buf_p - out_buf); } int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end) @@ -2489,6 +2534,7 @@ struct ImGuiStyleVarInfo static const ImGuiStyleVarInfo GStyleVarInfo[] = { { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize @@ -2766,7 +2812,7 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con int ellipsis_char_count = 1; if (ellipsis_char == (ImWchar)-1) { - ellipsis_char = (ImWchar)'.'; + ellipsis_char = font->DotChar; ellipsis_char_count = 3; } const ImFontGlyph* glyph = font->FindGlyph(ellipsis_char); @@ -2907,8 +2953,7 @@ ImGuiWindow::~ImGuiWindow() { IM_ASSERT(DrawList == &DrawListInst); IM_DELETE(Name); - for (int i = 0; i != ColumnsStorage.Size; i++) - ColumnsStorage[i].~ImGuiOldColumns(); + ColumnsStorage.clear_destruct(); } ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) @@ -3108,7 +3153,7 @@ void ImGui::MarkItemEdited(ImGuiID id) //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id); g.ActiveIdHasBeenEditedThisFrame = true; g.ActiveIdHasBeenEditedBefore = true; - g.CurrentWindow->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Edited; } static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags) @@ -3138,10 +3183,14 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavDisableMouseHover && !g.NavDisableHighlight) + { + if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + return false; return IsItemFocused(); + } // Test for bounding box overlap, as updated as ItemAdd() - ImGuiItemStatusFlags status_flags = window->DC.LastItemStatusFlags; + ImGuiItemStatusFlags status_flags = g.LastItemData.StatusFlags; if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) return false; IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0); // Flags not supported by this function @@ -3157,7 +3206,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // Test if another item is active (e.g. being dragged) if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) - if (g.ActiveId != 0 && g.ActiveId != window->DC.LastItemId && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) + if (g.ActiveId != 0 && g.ActiveId != g.LastItemData.ID && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) return false; // Test if interactions on this window are blocked by an active popup or modal. @@ -3166,12 +3215,12 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; // Test if the item is disabled - if ((window->DC.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; // Special handling for calling after Begin() which represent the title bar or tab. // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. - if (window->DC.LastItemId == window->MoveId && window->WriteAccessed) + if (g.LastItemData.ID == window->MoveId && window->WriteAccessed) return false; return true; } @@ -3192,7 +3241,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) return false; if (g.NavDisableMouseHover) return false; - if (!IsWindowContentHoverable(window, ImGuiHoveredFlags_None) || (window->DC.ItemFlags & ImGuiItemFlags_Disabled)) + if (!IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) { g.HoveredIdDisabled = true; return false; @@ -3201,9 +3250,21 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) // We exceptionally allow this function to be called with id==0 to allow using it for easy high-level // hover test in widgets code. We could also decide to split this function is two. if (id != 0) - { SetHoveredID(id); + // When disabled we'll return false but still set HoveredId + ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); + if (item_flags & ImGuiItemFlags_Disabled) + { + // Release active id if turning disabled + if (g.ActiveId == id) + ClearActiveID(); + g.HoveredIdDisabled = true; + return false; + } + + if (id != 0) + { // [DEBUG] Item Picker tool! // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered @@ -3229,25 +3290,23 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged return false; } -// This is also inlined in ItemAdd() -// Note: if ImGuiItemStatusFlags_HasDisplayRect is set, user needs to set window->DC.LastItemDisplayRect! -void ImGui::SetLastItemData(ImGuiWindow* window, ImGuiID item_id, ImGuiItemStatusFlags item_flags, const ImRect& item_rect) -{ - window->DC.LastItemId = item_id; - window->DC.LastItemStatusFlags = item_flags; - window->DC.LastItemRect = item_rect; -} - +// Called by ItemAdd() // Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. -bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) +void ImGui::ItemFocusable(ImGuiWindow* window, ImGuiID id) { ImGuiContext& g = *GImGui; + IM_ASSERT(id != 0 && id == g.LastItemData.ID); // Increment counters - const bool is_tab_stop = (window->DC.ItemFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; + // FIXME: ImGuiItemFlags_Disabled should disable more. + const bool is_tab_stop = (g.LastItemData.InFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; window->DC.FocusCounterRegular++; if (is_tab_stop) + { window->DC.FocusCounterTabStop++; + if (g.NavId == id) + g.NavIdTabCounter = window->DC.FocusCounterTabStop; + } // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. // (Note that we can always TAB out of a widget that doesn't allow tabbing in) @@ -3261,25 +3320,21 @@ bool ImGui::FocusableItemRegister(ImGuiWindow* window, ImGuiID id) if (g.TabFocusRequestCurrWindow == window) { if (window->DC.FocusCounterRegular == g.TabFocusRequestCurrCounterRegular) - return true; + { + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_FocusedByCode; + return; + } if (is_tab_stop && window->DC.FocusCounterTabStop == g.TabFocusRequestCurrCounterTabStop) { g.NavJustTabbedId = id; - return true; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_FocusedByTabbing; + return; } // If another item is about to be focused, we clear our own active id if (g.ActiveId == id) ClearActiveID(); } - - return false; -} - -void ImGui::FocusableItemUnregister(ImGuiWindow* window) -{ - window->DC.FocusCounterRegular--; - window->DC.FocusCounterTabStop--; } float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) @@ -3505,8 +3560,9 @@ void ImGui::StartMouseMovingWindow(ImGuiWindow* window) FocusWindow(window); SetActiveID(window->MoveId, window); g.NavDisableHighlight = true; + g.ActiveIdClickOffset = g.IO.MouseClickedPos[0] - window->RootWindow->Pos; g.ActiveIdNoClearOnFocusLoss = true; - g.ActiveIdClickOffset = g.IO.MousePos - window->RootWindow->Pos; + SetActiveIdUsingNavAndKeys(); bool can_move_window = true; if ((window->Flags & ImGuiWindowFlags_NoMove) || (window->RootWindow->Flags & ImGuiWindowFlags_NoMove)) @@ -3542,8 +3598,8 @@ void ImGui::UpdateMouseMovingWindowNewFrame() } else { - ClearActiveID(); g.MovingWindow = NULL; + ClearActiveID(); } } else @@ -3731,10 +3787,17 @@ void ImGui::UpdateMouseWheel() // Mouse wheel scrolling // If a child window has the ImGuiWindowFlags_NoScrollWithMouse flag, we give a chance to scroll its parent + if (g.IO.KeyCtrl) + return; + + // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead + // (we avoid doing it on OSX as it the OS input layer handles this already) + const bool swap_axis = g.IO.KeyShift && !g.IO.ConfigMacOSXBehaviors; + const float wheel_y = swap_axis ? 0.0f : g.IO.MouseWheel; + const float wheel_x = swap_axis ? g.IO.MouseWheel : g.IO.MouseWheelH; // Vertical Mouse Wheel scrolling - const float wheel_y = (g.IO.MouseWheel != 0.0f && !g.IO.KeyShift) ? g.IO.MouseWheel : 0.0f; - if (wheel_y != 0.0f && !g.IO.KeyCtrl) + if (wheel_y != 0.0f) { StartLockWheelingWindow(window); while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.y == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) @@ -3748,8 +3811,7 @@ void ImGui::UpdateMouseWheel() } // Horizontal Mouse Wheel scrolling, or Vertical Mouse Wheel w/ Shift held - const float wheel_x = (g.IO.MouseWheelH != 0.0f && !g.IO.KeyShift) ? g.IO.MouseWheelH : (g.IO.MouseWheel != 0.0f && g.IO.KeyShift) ? g.IO.MouseWheel : 0.0f; - if (wheel_x != 0.0f && !g.IO.KeyCtrl) + if (wheel_x != 0.0f) { StartLockWheelingWindow(window); while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.x == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) @@ -3771,12 +3833,14 @@ void ImGui::UpdateTabFocus() g.TabFocusPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); if (g.ActiveId == 0 && g.TabFocusPressed) { - // Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also - // manipulate the Next fields even, even though they will be turned into Curr fields by the code below. + // - This path is only taken when no widget are active/tabbed-into yet. + // Subsequent tabbing will be processed by FocusableItemRegister() + // - Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also + // manipulate the Next fields here even though they will be turned into Curr fields below. g.TabFocusRequestNextWindow = g.NavWindow; g.TabFocusRequestNextCounterRegular = INT_MAX; if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) - g.TabFocusRequestNextCounterTabStop = g.NavIdTabCounter + 1 + (g.IO.KeyShift ? -1 : 1); + g.TabFocusRequestNextCounterTabStop = g.NavIdTabCounter + (g.IO.KeyShift ? -1 : 0); else g.TabFocusRequestNextCounterTabStop = g.IO.KeyShift ? -1 : 0; } @@ -3803,6 +3867,7 @@ void ImGui::UpdateTabFocus() void ImGui::UpdateHoveredWindowAndCaptureFlags() { ImGuiContext& g = *GImGui; + g.WindowsHoverPadding = ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_HOVER_PADDING, WINDOWS_HOVER_PADDING)); // Find the window hovered by mouse: // - Child windows can extend beyond the limit of their parent so we need to derive HoveredRootWindow from HoveredWindow. @@ -3902,7 +3967,8 @@ void ImGui::NewFrame() g.FramerateSecPerFrameAccum += g.IO.DeltaTime - g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx]; g.FramerateSecPerFrame[g.FramerateSecPerFrameIdx] = g.IO.DeltaTime; g.FramerateSecPerFrameIdx = (g.FramerateSecPerFrameIdx + 1) % IM_ARRAYSIZE(g.FramerateSecPerFrame); - g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)IM_ARRAYSIZE(g.FramerateSecPerFrame))) : FLT_MAX; + g.FramerateSecPerFrameCount = ImMin(g.FramerateSecPerFrameCount + 1, IM_ARRAYSIZE(g.FramerateSecPerFrame)); + g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)g.FramerateSecPerFrameCount)) : FLT_MAX; UpdateViewportsNewFrame(); @@ -4020,7 +4086,7 @@ void ImGui::NewFrame() UpdateTabFocus(); // Mark all windows as not visible and compact unused memory. - IM_ASSERT(g.WindowsFocusOrder.Size == g.Windows.Size); + IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size); const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; for (int i = 0; i != g.Windows.Size; i++) { @@ -4039,6 +4105,9 @@ void ImGui::NewFrame() for (int i = 0; i < g.TablesLastTimeActive.Size; i++) if (g.TablesLastTimeActive[i] >= 0.0f && g.TablesLastTimeActive[i] < memory_compact_start_time) TableGcCompactTransientBuffers(g.Tables.GetByIndex(i)); + for (int i = 0; i < g.TablesTempDataStack.Size; i++) + if (g.TablesTempDataStack[i].LastTimeActive >= 0.0f && g.TablesTempDataStack[i].LastTimeActive < memory_compact_start_time) + TableGcCompactTransientBuffers(&g.TablesTempDataStack[i]); if (g.GcCompactAll) GcCompactTransientMiscBuffers(); g.GcCompactAll = false; @@ -4052,9 +4121,8 @@ void ImGui::NewFrame() g.CurrentWindowStack.resize(0); g.BeginPopupStack.resize(0); g.ItemFlagsStack.resize(0); - g.ItemFlagsStack.push_back(ImGuiItemFlags_Default_); + g.ItemFlagsStack.push_back(ImGuiItemFlags_None); g.GroupStack.resize(0); - ClosePopupsOverWindow(g.NavWindow, false); // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. UpdateDebugToolItemPicker(); @@ -4078,20 +4146,20 @@ void ImGui::UpdateDebugToolItemPicker() if (g.DebugItemPickerActive) { const ImGuiID hovered_id = g.HoveredIdPreviousFrame; - ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); - if (ImGui::IsKeyPressedMap(ImGuiKey_Escape)) + SetMouseCursor(ImGuiMouseCursor_Hand); + if (IsKeyPressedMap(ImGuiKey_Escape)) g.DebugItemPickerActive = false; - if (ImGui::IsMouseClicked(0) && hovered_id) + if (IsMouseClicked(0) && hovered_id) { g.DebugItemPickerBreakId = hovered_id; g.DebugItemPickerActive = false; } - ImGui::SetNextWindowBgAlpha(0.60f); - ImGui::BeginTooltip(); - ImGui::Text("HoveredId: 0x%08X", hovered_id); - ImGui::Text("Press ESC to abort picking."); - ImGui::TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); - ImGui::EndTooltip(); + SetNextWindowBgAlpha(0.60f); + BeginTooltip(); + Text("HoveredId: 0x%08X", hovered_id); + Text("Press ESC to abort picking."); + TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); + EndTooltip(); } } @@ -4113,17 +4181,15 @@ void ImGui::Initialize(ImGuiContext* context) g.SettingsHandlers.push_back(ini_handler); } -#ifdef IMGUI_HAS_TABLE // Add .ini handle for ImGuiTable type TableSettingsInstallHandler(context); -#endif // #ifdef IMGUI_HAS_TABLE // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); g.Viewports.push_back(viewport); #ifdef IMGUI_HAS_DOCK -#endif // #ifdef IMGUI_HAS_DOCK +#endif g.Initialized = true; } @@ -4156,9 +4222,7 @@ void ImGui::Shutdown(ImGuiContext* context) CallContextHooks(&g, ImGuiContextHookType_Shutdown); // Clear everything else - for (int i = 0; i < g.Windows.Size; i++) - IM_DELETE(g.Windows[i]); - g.Windows.clear(); + g.Windows.clear_delete(); g.WindowsFocusOrder.clear(); g.WindowsTempSortBuffer.clear(); g.CurrentWindow = NULL; @@ -4174,16 +4238,14 @@ void ImGui::Shutdown(ImGuiContext* context) g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); - for (int i = 0; i < g.Viewports.Size; i++) - IM_DELETE(g.Viewports[i]); - g.Viewports.clear(); + g.Viewports.clear_delete(); g.TabBars.Clear(); g.CurrentTabBarStack.clear(); g.ShrinkWidthBuffer.clear(); g.Tables.Clear(); - g.CurrentTableStack.clear(); + g.TablesTempDataStack.clear_destruct(); g.DrawChannelsTempMergeBuffer.clear(); g.ClipboardHandlerData.clear(); @@ -4426,11 +4488,15 @@ void ImGui::EndFrame() // Clear Input data for next frame g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.InputQueueCharacters.resize(0); + g.IO.KeyModsPrev = g.IO.KeyMods; // doing it here is better than in NewFrame() as we'll tolerate backend writing to KeyMods. If we want to firmly disallow it we should detect it. memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); CallContextHooks(&g, ImGuiContextHookType_EndFramePost); } +// Prepare the data for rendering so you can call GetDrawData() +// (As with anything within the ImGui:: namspace this doesn't touch your GPU or graphics API at all: +// it is the role of the ImGui_ImplXXXX_RenderDrawData() function provided by the renderer backend) void ImGui::Render() { ImGuiContext& g = *GImGui; @@ -4459,6 +4525,7 @@ void ImGui::Render() for (int n = 0; n != g.Windows.Size; n++) { ImGuiWindow* window = g.Windows[n]; + IM_MSVC_WARNING_SUPPRESS(6011); // Static Analysis false positive "warning C6011: Dereferencing NULL pointer 'window'" if (IsWindowActiveAndVisible(window) && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0 && window != windows_to_render_top_most[0] && window != windows_to_render_top_most[1]) AddRootWindowToDrawData(window); } @@ -4532,10 +4599,11 @@ static void FindHoveredWindow() hovered_window = g.MovingWindow; ImVec2 padding_regular = g.Style.TouchExtraPadding; - ImVec2 padding_for_resize_from_edges = g.IO.ConfigWindowsResizeFromEdges ? ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS)) : padding_regular; + ImVec2 padding_for_resize = g.IO.ConfigWindowsResizeFromEdges ? g.WindowsHoverPadding : padding_regular; for (int i = g.Windows.Size - 1; i >= 0; i--) { ImGuiWindow* window = g.Windows[i]; + IM_MSVC_WARNING_SUPPRESS(28182); // [Static Analyzer] Dereferencing NULL pointer. if (!window->Active || window->Hidden) continue; if (window->Flags & ImGuiWindowFlags_NoMouseInputs) @@ -4546,7 +4614,7 @@ static void FindHoveredWindow() if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) bb.Expand(padding_regular); else - bb.Expand(padding_for_resize_from_edges); + bb.Expand(padding_for_resize); if (!bb.Contains(g.IO.MousePos)) continue; @@ -4562,6 +4630,7 @@ static void FindHoveredWindow() if (hovered_window == NULL) hovered_window = window; + IM_MSVC_WARNING_SUPPRESS(28182); // [Static Analyzer] Dereferencing NULL pointer. if (hovered_window_ignoring_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow)) hovered_window_ignoring_moving_window = window; if (hovered_window && hovered_window_ignoring_moving_window) @@ -4801,10 +4870,7 @@ bool ImGui::IsItemActive() { ImGuiContext& g = *GImGui; if (g.ActiveId) - { - ImGuiWindow* window = g.CurrentWindow; - return g.ActiveId == window->DC.LastItemId; - } + return g.ActiveId == g.LastItemData.ID; return false; } @@ -4812,21 +4878,17 @@ bool ImGui::IsItemActivated() { ImGuiContext& g = *GImGui; if (g.ActiveId) - { - ImGuiWindow* window = g.CurrentWindow; - if (g.ActiveId == window->DC.LastItemId && g.ActiveIdPreviousFrame != window->DC.LastItemId) + if (g.ActiveId == g.LastItemData.ID && g.ActiveIdPreviousFrame != g.LastItemData.ID) return true; - } return false; } bool ImGui::IsItemDeactivated() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDeactivated) - return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Deactivated) != 0; - return (g.ActiveIdPreviousFrame == window->DC.LastItemId && g.ActiveIdPreviousFrame != 0 && g.ActiveId != window->DC.LastItemId); + if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDeactivated) + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Deactivated) != 0; + return (g.ActiveIdPreviousFrame == g.LastItemData.ID && g.ActiveIdPreviousFrame != 0 && g.ActiveId != g.LastItemData.ID); } bool ImGui::IsItemDeactivatedAfterEdit() @@ -4839,9 +4901,7 @@ bool ImGui::IsItemDeactivatedAfterEdit() bool ImGui::IsItemFocused() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - - if (g.NavId != window->DC.LastItemId || g.NavId == 0) + if (g.NavId != g.LastItemData.ID || g.NavId == 0) return false; return true; } @@ -4856,13 +4916,13 @@ bool ImGui::IsItemClicked(ImGuiMouseButton mouse_button) bool ImGui::IsItemToggledOpen() { ImGuiContext& g = *GImGui; - return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; } bool ImGui::IsItemToggledSelection() { ImGuiContext& g = *GImGui; - return (g.CurrentWindow->DC.LastItemStatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; } bool ImGui::IsAnyItemHovered() @@ -4885,14 +4945,14 @@ bool ImGui::IsAnyItemFocused() bool ImGui::IsItemVisible() { - ImGuiWindow* window = GetCurrentWindowRead(); - return window->ClipRect.Overlaps(window->DC.LastItemRect); + ImGuiContext& g = *GImGui; + return g.CurrentWindow->ClipRect.Overlaps(g.LastItemData.Rect); } bool ImGui::IsItemEdited() { - ImGuiWindow* window = GetCurrentWindowRead(); - return (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_Edited) != 0; + ImGuiContext& g = *GImGui; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Edited) != 0; } // Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. @@ -4900,7 +4960,7 @@ bool ImGui::IsItemEdited() void ImGui::SetItemAllowOverlap() { ImGuiContext& g = *GImGui; - ImGuiID id = g.CurrentWindow->DC.LastItemId; + ImGuiID id = g.LastItemData.ID; if (g.HoveredId == id) g.HoveredIdAllowOverlap = true; if (g.ActiveId == id) @@ -4910,29 +4970,39 @@ void ImGui::SetItemAllowOverlap() void ImGui::SetItemUsingMouseWheel() { ImGuiContext& g = *GImGui; - ImGuiID id = g.CurrentWindow->DC.LastItemId; + ImGuiID id = g.LastItemData.ID; if (g.HoveredId == id) g.HoveredIdUsingMouseWheel = true; if (g.ActiveId == id) g.ActiveIdUsingMouseWheel = true; } +void ImGui::SetActiveIdUsingNavAndKeys() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.ActiveId != 0); + g.ActiveIdUsingNavDirMask = ~(ImU32)0; + g.ActiveIdUsingNavInputMask = ~(ImU32)0; + g.ActiveIdUsingKeyInputMask = ~(ImU64)0; + NavMoveRequestCancel(); +} + ImVec2 ImGui::GetItemRectMin() { - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.LastItemRect.Min; + ImGuiContext& g = *GImGui; + return g.LastItemData.Rect.Min; } ImVec2 ImGui::GetItemRectMax() { - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.LastItemRect.Max; + ImGuiContext& g = *GImGui; + return g.LastItemData.Rect.Max; } ImVec2 ImGui::GetItemRectSize() { - ImGuiWindow* window = GetCurrentWindowRead(); - return window->DC.LastItemRect.GetSize(); + ImGuiContext& g = *GImGui; + return g.LastItemData.Rect.GetSize(); } bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) @@ -4975,7 +5045,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b parent_window->DC.CursorPos = child_window->Pos; // Process navigation-in immediately so NavInit can run on first frame - if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayerActiveMask != 0 || child_window->DC.NavHasScroll)) + if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavHasScroll)) { FocusWindow(child_window); NavInitWindow(child_window, false); @@ -5022,13 +5092,13 @@ void ImGui::EndChild() ImGuiWindow* parent_window = g.CurrentWindow; ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz); ItemSize(sz); - if ((window->DC.NavLayerActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) + if ((window->DC.NavLayersActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) { ItemAdd(bb, window->ChildId); RenderNavHighlight(bb, window->ChildId); // When browsing a window that has no activable items (scroll only) we keep a highlight on the child - if (window->DC.NavLayerActiveMask == 0 && window == g.NavWindow) + if (window->DC.NavLayersActiveMask == 0 && window == g.NavWindow) RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); } else @@ -5037,7 +5107,7 @@ void ImGui::EndChild() ItemAdd(bb, 0); } if (g.HoveredWindow == window) - parent_window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredWindow; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredWindow; } g.WithinEndChild = false; g.LogLinePosY = -FLT_MAX; // To enforce a carriage return @@ -5129,7 +5199,12 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0); } - g.WindowsFocusOrder.push_back(window); + if (!(flags & ImGuiWindowFlags_ChildWindow)) + { + g.WindowsFocusOrder.push_back(window); + window->FocusOrder = (short)(g.WindowsFocusOrder.Size - 1); + } + if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus) g.Windows.push_front(window); // Quite slow but rare and only once else @@ -5165,8 +5240,9 @@ static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, const ImVec2& s if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) { ImGuiWindow* window_for_height = window; + const float decoration_up_height = window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight(); new_size = ImMax(new_size, g.Style.WindowMinSize); - new_size.y = ImMax(new_size.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows + new_size.y = ImMax(new_size.y, decoration_up_height + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows } return new_size; } @@ -5195,9 +5271,9 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; - ImVec2 size_decorations = ImVec2(0.0f, window->TitleBarHeight() + window->MenuBarHeight()); + const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); ImVec2 size_pad = window->WindowPadding * 2.0f; - ImVec2 size_desired = size_contents + size_pad + size_decorations; + ImVec2 size_desired = size_contents + size_pad + ImVec2(0.0f, decoration_up_height); if (window->Flags & ImGuiWindowFlags_Tooltip) { // Tooltip always resize @@ -5219,8 +5295,8 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. ImVec2 size_auto_fit_after_constraint = CalcWindowSizeAfterConstraint(window, size_auto_fit); - bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - size_decorations.x < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); - bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - size_decorations.y < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); + bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - 0.0f < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); + bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - decoration_up_height < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); if (will_have_scrollbar_x) size_auto_fit.y += style.ScrollbarSize; if (will_have_scrollbar_y) @@ -5262,53 +5338,64 @@ static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& co *out_size = size_constrained; } +// Data for resizing from corner struct ImGuiResizeGripDef { ImVec2 CornerPosN; ImVec2 InnerDir; int AngleMin12, AngleMax12; }; - static const ImGuiResizeGripDef resize_grip_def[4] = { - { ImVec2(1, 1), ImVec2(-1, -1), 0, 3 }, // Lower-right - { ImVec2(0, 1), ImVec2(+1, -1), 3, 6 }, // Lower-left - { ImVec2(0, 0), ImVec2(+1, +1), 6, 9 }, // Upper-left (Unused) - { ImVec2(1, 0), ImVec2(-1, +1), 9, 12 }, // Upper-right (Unused) + { ImVec2(1, 1), ImVec2(-1, -1), 0, 3 }, // Lower-right + { ImVec2(0, 1), ImVec2(+1, -1), 3, 6 }, // Lower-left + { ImVec2(0, 0), ImVec2(+1, +1), 6, 9 }, // Upper-left (Unused) + { ImVec2(1, 0), ImVec2(-1, +1), 9, 12 } // Upper-right (Unused) }; +// Data for resizing from borders struct ImGuiResizeBorderDef { ImVec2 InnerDir; - ImVec2 CornerPosN1, CornerPosN2; + ImVec2 SegmentN1, SegmentN2; float OuterAngle; }; - static const ImGuiResizeBorderDef resize_border_def[4] = { - { ImVec2(0, +1), ImVec2(0, 0), ImVec2(1, 0), IM_PI * 1.50f }, // Top + { ImVec2(+1, 0), ImVec2(0, 1), ImVec2(0, 0), IM_PI * 1.00f }, // Left { ImVec2(-1, 0), ImVec2(1, 0), ImVec2(1, 1), IM_PI * 0.00f }, // Right - { ImVec2(0, -1), ImVec2(1, 1), ImVec2(0, 1), IM_PI * 0.50f }, // Bottom - { ImVec2(+1, 0), ImVec2(0, 1), ImVec2(0, 0), IM_PI * 1.00f } // Left + { ImVec2(0, +1), ImVec2(0, 0), ImVec2(1, 0), IM_PI * 1.50f }, // Up + { ImVec2(0, -1), ImVec2(1, 1), ImVec2(0, 1), IM_PI * 0.50f } // Down }; static ImRect GetResizeBorderRect(ImGuiWindow* window, int border_n, float perp_padding, float thickness) { ImRect rect = window->Rect(); - if (thickness == 0.0f) rect.Max -= ImVec2(1, 1); - if (border_n == 0) { return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); } // Top - if (border_n == 1) { return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); } // Right - if (border_n == 2) { return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); } // Bottom - if (border_n == 3) { return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding); } // Left + if (thickness == 0.0f) + rect.Max -= ImVec2(1, 1); + if (border_n == ImGuiDir_Left) { return ImRect(rect.Min.x - thickness, rect.Min.y + perp_padding, rect.Min.x + thickness, rect.Max.y - perp_padding); } + if (border_n == ImGuiDir_Right) { return ImRect(rect.Max.x - thickness, rect.Min.y + perp_padding, rect.Max.x + thickness, rect.Max.y - perp_padding); } + if (border_n == ImGuiDir_Up) { return ImRect(rect.Min.x + perp_padding, rect.Min.y - thickness, rect.Max.x - perp_padding, rect.Min.y + thickness); } + if (border_n == ImGuiDir_Down) { return ImRect(rect.Min.x + perp_padding, rect.Max.y - thickness, rect.Max.x - perp_padding, rect.Max.y + thickness); } IM_ASSERT(0); return ImRect(); } // 0..3: corners (Lower-right, Lower-left, Unused, Unused) -// 4..7: borders (Top, Right, Bottom, Left) -ImGuiID ImGui::GetWindowResizeID(ImGuiWindow* window, int n) +ImGuiID ImGui::GetWindowResizeCornerID(ImGuiWindow* window, int n) +{ + IM_ASSERT(n >= 0 && n < 4); + ImGuiID id = window->ID; + id = ImHashStr("#RESIZE", 0, id); + id = ImHashData(&n, sizeof(int), id); + return id; +} + +// Borders (Left, Right, Up, Down) +ImGuiID ImGui::GetWindowResizeBorderID(ImGuiWindow* window, ImGuiDir dir) { - IM_ASSERT(n >= 0 && n <= 7); + IM_ASSERT(dir >= 0 && dir < 4); + int n = (int)dir + 4; ImGuiID id = window->ID; id = ImHashStr("#RESIZE", 0, id); id = ImHashData(&n, sizeof(int), id); @@ -5331,7 +5418,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0; const float grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); const float grip_hover_inner_size = IM_FLOOR(grip_draw_size * 0.75f); - const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS : 0.0f; + const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_HOVER_PADDING : 0.0f; ImVec2 pos_target(FLT_MAX, FLT_MAX); ImVec2 size_target(FLT_MAX, FLT_MAX); @@ -5343,15 +5430,16 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s PushID("#RESIZE"); for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) { - const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; - const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); + const ImGuiResizeGripDef& def = resize_grip_def[resize_grip_n]; + const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, def.CornerPosN); // Using the FlattenChilds button flag we make the resize button accessible even if we are hovering over a child window - ImRect resize_rect(corner - grip.InnerDir * grip_hover_outer_size, corner + grip.InnerDir * grip_hover_inner_size); + bool hovered, held; + ImRect resize_rect(corner - def.InnerDir * grip_hover_outer_size, corner + def.InnerDir * grip_hover_inner_size); if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x); if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y); - bool hovered, held; - ButtonBehavior(resize_rect, window->GetID(resize_grip_n), &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); + ImGuiID resize_grip_id = window->GetID(resize_grip_n); // == GetWindowResizeCornerID() + ButtonBehavior(resize_rect, resize_grip_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); if (hovered || held) g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; @@ -5367,39 +5455,41 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s { // Resize from any of the four corners // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position - ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(grip.InnerDir * grip_hover_outer_size, grip.InnerDir * -grip_hover_inner_size, grip.CornerPosN); // Corner of the window corresponding to our corner grip - ImVec2 clamp_min = ImVec2(grip.CornerPosN.x == 1.0f ? visibility_rect.Min.x : -FLT_MAX, grip.CornerPosN.y == 1.0f ? visibility_rect.Min.y : -FLT_MAX); - ImVec2 clamp_max = ImVec2(grip.CornerPosN.x == 0.0f ? visibility_rect.Max.x : +FLT_MAX, grip.CornerPosN.y == 0.0f ? visibility_rect.Max.y : +FLT_MAX); + ImVec2 clamp_min = ImVec2(def.CornerPosN.x == 1.0f ? visibility_rect.Min.x : -FLT_MAX, def.CornerPosN.y == 1.0f ? visibility_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max = ImVec2(def.CornerPosN.x == 0.0f ? visibility_rect.Max.x : +FLT_MAX, def.CornerPosN.y == 0.0f ? visibility_rect.Max.y : +FLT_MAX); + ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(def.InnerDir * grip_hover_outer_size, def.InnerDir * -grip_hover_inner_size, def.CornerPosN); // Corner of the window corresponding to our corner grip corner_target = ImClamp(corner_target, clamp_min, clamp_max); - CalcResizePosSizeFromAnyCorner(window, corner_target, grip.CornerPosN, &pos_target, &size_target); + CalcResizePosSizeFromAnyCorner(window, corner_target, def.CornerPosN, &pos_target, &size_target); } + + // Only lower-left grip is visible before hovering/activating if (resize_grip_n == 0 || held || hovered) resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip); } for (int border_n = 0; border_n < resize_border_count; border_n++) { + const ImGuiResizeBorderDef& def = resize_border_def[border_n]; + const ImGuiAxis axis = (border_n == ImGuiDir_Left || border_n == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y; + bool hovered, held; - ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); - ButtonBehavior(border_rect, window->GetID(border_n + 4), &hovered, &held, ImGuiButtonFlags_FlattenChildren); + ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_HOVER_PADDING); + ImGuiID border_id = window->GetID(border_n + 4); // == GetWindowResizeBorderID() + ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren); //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) { - g.MouseCursor = (border_n & 1) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; + g.MouseCursor = (axis == ImGuiAxis_X) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; if (held) *border_held = border_n; } if (held) { + ImVec2 clamp_min(border_n == ImGuiDir_Right ? visibility_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down ? visibility_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max(border_n == ImGuiDir_Left ? visibility_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? visibility_rect.Max.y : +FLT_MAX); ImVec2 border_target = window->Pos; - ImVec2 border_posn; - if (border_n == 0) { border_posn = ImVec2(0, 0); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Top - if (border_n == 1) { border_posn = ImVec2(1, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Right - if (border_n == 2) { border_posn = ImVec2(0, 1); border_target.y = (g.IO.MousePos.y - g.ActiveIdClickOffset.y + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Bottom - if (border_n == 3) { border_posn = ImVec2(0, 0); border_target.x = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + WINDOWS_RESIZE_FROM_EDGES_HALF_THICKNESS); } // Left - ImVec2 clamp_min = ImVec2(border_n == 1 ? visibility_rect.Min.x : -FLT_MAX, border_n == 2 ? visibility_rect.Min.y : -FLT_MAX); - ImVec2 clamp_max = ImVec2(border_n == 3 ? visibility_rect.Max.x : +FLT_MAX, border_n == 0 ? visibility_rect.Max.y : +FLT_MAX); + border_target[axis] = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING; border_target = ImClamp(border_target, clamp_min, clamp_max); - CalcResizePosSizeFromAnyCorner(window, border_target, border_posn, &pos_target, &size_target); + CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target); } } PopID(); @@ -5466,8 +5556,8 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) { const ImGuiResizeBorderDef& def = resize_border_def[border_held]; ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.CornerPosN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), 0, ImMax(2.0f, border_size)); // Thicker than usual } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) @@ -5573,8 +5663,8 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl const bool has_collapse_button = !(flags & ImGuiWindowFlags_NoCollapse) && (style.WindowMenuButtonPosition != ImGuiDir_None); // Close & Collapse button are on the Menu NavLayer and don't default focus (unless there's nothing else on that layer) - const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; - window->DC.ItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; + const ImGuiItemFlags item_flags_backup = g.CurrentItemFlags; + g.CurrentItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; // Layout buttons @@ -5611,12 +5701,11 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl *p_open = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.ItemFlags = item_flags_backup; + g.CurrentItemFlags = item_flags_backup; // Title bar text (with: horizontal alignment, avoiding collapse/close button, optional "unsaved document" marker) // FIXME: Refactor text alignment facilities along with RenderText helpers, this is WAY too much messy code.. - const char* UNSAVED_DOCUMENT_MARKER = "*"; - const float marker_size_x = (flags & ImGuiWindowFlags_UnsavedDocument) ? CalcTextSize(UNSAVED_DOCUMENT_MARKER, NULL, false).x : 0.0f; + const float marker_size_x = (flags & ImGuiWindowFlags_UnsavedDocument) ? button_sz * 0.80f : 0.0f; const ImVec2 text_size = CalcTextSize(name, NULL, true) + ImVec2(marker_size_x, 0.0f); // As a nice touch we try to ensure that centered title text doesn't get affected by visibility of Close/Collapse button, @@ -5635,15 +5724,20 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl ImRect layout_r(title_bar_rect.Min.x + pad_l, title_bar_rect.Min.y, title_bar_rect.Max.x - pad_r, title_bar_rect.Max.y); ImRect clip_r(layout_r.Min.x, layout_r.Min.y, ImMin(layout_r.Max.x + g.Style.ItemInnerSpacing.x, title_bar_rect.Max.x), layout_r.Max.y); - //if (g.IO.KeyShift) window->DrawList->AddRect(layout_r.Min, layout_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] - //if (g.IO.KeyCtrl) window->DrawList->AddRect(clip_r.Min, clip_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] - RenderTextClipped(layout_r.Min, layout_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_r); if (flags & ImGuiWindowFlags_UnsavedDocument) { - ImVec2 marker_pos = ImVec2(ImMax(layout_r.Min.x, layout_r.Min.x + (layout_r.GetWidth() - text_size.x) * style.WindowTitleAlign.x) + text_size.x, layout_r.Min.y) + ImVec2(2 - marker_size_x, 0.0f); - ImVec2 off = ImVec2(0.0f, IM_FLOOR(-g.FontSize * 0.25f)); - RenderTextClipped(marker_pos + off, layout_r.Max + off, UNSAVED_DOCUMENT_MARKER, NULL, NULL, ImVec2(0, style.WindowTitleAlign.y), &clip_r); + ImVec2 marker_pos; + marker_pos.x = ImClamp(layout_r.Min.x + (layout_r.GetWidth() - text_size.x) * style.WindowTitleAlign.x + text_size.x, layout_r.Min.x, layout_r.Max.x); + marker_pos.y = (layout_r.Min.y + layout_r.Max.y) * 0.5f; + if (marker_pos.x > layout_r.Min.x) + { + RenderBullet(window->DrawList, marker_pos, GetColorU32(ImGuiCol_Text)); + clip_r.Max.x = ImMin(clip_r.Max.x, marker_pos.x - (int)(marker_size_x * 0.5f)); + } } + //if (g.IO.KeyShift) window->DrawList->AddRect(layout_r.Min, layout_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] + //if (g.IO.KeyCtrl) window->DrawList->AddRect(clip_r.Min, clip_r.Max, IM_COL32(255, 128, 0, 255)); // [DEBUG] + RenderTextClipped(layout_r.Min, layout_r.Max, name, NULL, &text_size, style.WindowTitleAlign, &clip_r); } void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window) @@ -5695,14 +5789,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update the Appearing flag bool window_just_activated_by_user = (window->LastFrameActive < current_frame - 1); // Not using !WasActive because the implicit "Debug" window would always toggle off->on - const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; window_just_activated_by_user |= (window->PopupId != popup_ref.PopupId); // We recycle popups so treat window as activated if popup id changed window_just_activated_by_user |= (window != popup_ref.Window); } - window->Appearing = (window_just_activated_by_user || window_just_appearing_after_hidden_for_resize); + window->Appearing = window_just_activated_by_user; if (window->Appearing) SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, true); @@ -5721,7 +5814,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } // Parent window is latched only on the first call to Begin() of the frame, so further append-calls can be done from a different window stack - ImGuiWindow* parent_window_in_stack = g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back(); + ImGuiWindow* parent_window_in_stack = g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back().Window; ImGuiWindow* parent_window = first_begin_of_the_frame ? ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup)) ? parent_window_in_stack : NULL) : window->ParentWindow; IM_ASSERT(parent_window != NULL || !(flags & ImGuiWindowFlags_ChildWindow)); @@ -5731,7 +5824,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Add to stack // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() - g.CurrentWindowStack.push_back(window); + ImGuiWindowStackData window_stack_data; + window_stack_data.Window = window; + window_stack_data.ParentLastItemDataBackup = g.LastItemData; + g.CurrentWindowStack.push_back(window_stack_data); g.CurrentWindow = window; window->DC.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindow = NULL; @@ -5829,6 +5925,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS // Update contents size from last frame for auto-fitting (or use explicit size) + const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); CalcWindowContentSizes(window, &window->ContentSize, &window->ContentSizeIdeal); if (window->HiddenFramesCanSkipItems > 0) window->HiddenFramesCanSkipItems--; @@ -6191,13 +6288,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; - window->DC.NavLayerActiveMask = window->DC.NavLayerActiveMaskNext; - window->DC.NavLayerActiveMaskNext = 0x00; + window->DC.NavLayersActiveMask = window->DC.NavLayersActiveMaskNext; + window->DC.NavLayersActiveMaskNext = 0x00; window->DC.NavHideHighlightOneFrame = false; window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); window->DC.MenuBarAppending = false; - window->DC.MenuColumns.Update(3, style.ItemSpacing.x, window_just_activated_by_user); + window->DC.MenuColumns.Update(style.ItemSpacing.x, window_just_activated_by_user); window->DC.TreeDepth = 0; window->DC.TreeJumpToParentOnPopMask = 0x00; window->DC.ChildWindows.resize(0); @@ -6243,11 +6340,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). // This is useful to allow creating context menus on title bar only, etc. - SetLastItemData(window, window->MoveId, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect); + g.LastItemData.ID = window->MoveId; + g.LastItemData.InFlags = g.CurrentItemFlags; + g.LastItemData.StatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0; + g.LastItemData.Rect = title_bar_rect; #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) - IMGUI_TEST_ENGINE_ITEM_ADD(window->DC.LastItemRect, window->DC.LastItemId); + IMGUI_TEST_ENGINE_ITEM_ADD(g.LastItemData.Rect, g.LastItemData.ID); #endif } else @@ -6257,8 +6357,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } // Pull/inherit current state - window->DC.ItemFlags = g.ItemFlagsStack.back(); // Inherit from shared stack - window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : 0; // Inherit from parent only // -V595 + window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : window->GetID("#FOCUSSCOPE"); // Inherit from parent only // -V595 PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); @@ -6294,6 +6393,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update the Hidden flag window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0) || (window->HiddenFramesForRenderOnly > 0); + // Disable inputs for requested number of frames + if (window->DisableInputsFrames > 0) + { + window->DisableInputsFrames--; + window->Flags |= ImGuiWindowFlags_NoInputs; + } + // Update the SkipItems flag, used to early out of all items functions (no layout required) bool skip_items = false; if (window->Collapsed || !window->Active || window->Hidden) @@ -6332,25 +6438,33 @@ void ImGui::End() LogFinish(); // Pop from window stack + g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; g.CurrentWindowStack.pop_back(); if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); window->DC.StackSizesOnBegin.CompareWithCurrentState(); - SetCurrentWindow(g.CurrentWindowStack.empty() ? NULL : g.CurrentWindowStack.back()); + SetCurrentWindow(g.CurrentWindowStack.Size == 0 ? NULL : g.CurrentWindowStack.back().Window); } void ImGui::BringWindowToFocusFront(ImGuiWindow* window) { ImGuiContext& g = *GImGui; + IM_ASSERT(window == window->RootWindow); + + const int cur_order = window->FocusOrder; + IM_ASSERT(g.WindowsFocusOrder[cur_order] == window); if (g.WindowsFocusOrder.back() == window) return; - for (int i = g.WindowsFocusOrder.Size - 2; i >= 0; i--) // We can ignore the top-most window - if (g.WindowsFocusOrder[i] == window) - { - memmove(&g.WindowsFocusOrder[i], &g.WindowsFocusOrder[i + 1], (size_t)(g.WindowsFocusOrder.Size - i - 1) * sizeof(ImGuiWindow*)); - g.WindowsFocusOrder[g.WindowsFocusOrder.Size - 1] = window; - break; - } + + const int new_order = g.WindowsFocusOrder.Size - 1; + for (int n = cur_order; n < new_order; n++) + { + g.WindowsFocusOrder[n] = g.WindowsFocusOrder[n + 1]; + g.WindowsFocusOrder[n]->FocusOrder--; + IM_ASSERT(g.WindowsFocusOrder[n]->FocusOrder == n); + } + g.WindowsFocusOrder[new_order] = window; + window->FocusOrder = (short)new_order; } void ImGui::BringWindowToDisplayFront(ImGuiWindow* window) @@ -6430,18 +6544,13 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind { ImGuiContext& g = *GImGui; - int start_idx = g.WindowsFocusOrder.Size - 1; - if (under_this_window != NULL) - { - int under_this_window_idx = FindWindowFocusIndex(under_this_window); - if (under_this_window_idx != -1) - start_idx = under_this_window_idx - 1; - } + const int start_idx = ((under_this_window != NULL) ? FindWindowFocusIndex(under_this_window) : g.WindowsFocusOrder.Size) - 1; for (int i = start_idx; i >= 0; i--) { // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user. ImGuiWindow* window = g.WindowsFocusOrder[i]; - if (window != ignore_window && window->WasActive && !(window->Flags & ImGuiWindowFlags_ChildWindow)) + IM_ASSERT(window == window->RootWindow); + if (window != ignore_window && window->WasActive) if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) { ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window); @@ -6490,24 +6599,53 @@ void ImGui::PopFont() void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiItemFlags item_flags = window->DC.ItemFlags; + ImGuiItemFlags item_flags = g.CurrentItemFlags; IM_ASSERT(item_flags == g.ItemFlagsStack.back()); if (enabled) item_flags |= option; else item_flags &= ~option; - window->DC.ItemFlags = item_flags; + g.CurrentItemFlags = item_flags; g.ItemFlagsStack.push_back(item_flags); } void ImGui::PopItemFlag() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(g.ItemFlagsStack.Size > 1); // Too many calls to PopItemFlag() - we always leave a 0 at the bottom of the stack. g.ItemFlagsStack.pop_back(); - window->DC.ItemFlags = g.ItemFlagsStack.back(); + g.CurrentItemFlags = g.ItemFlagsStack.back(); +} + +// BeginDisabled()/EndDisabled() +// - Those can be nested but this cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep things disabled) +// - Visually this is currently altering alpha, but it is expected that in a future styling system this would work differently. +// - Feedback welcome at https://github.com/ocornut/imgui/issues/211 +// - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. +// - Optimized shortcuts instead of PushStyleVar() + PushItemFlag() +void ImGui::BeginDisabled(bool disabled) +{ + ImGuiContext& g = *GImGui; + bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; + if (!was_disabled && disabled) + { + g.DisabledAlphaBackup = g.Style.Alpha; + g.Style.Alpha *= g.Style.DisabledAlpha; // PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * g.Style.DisabledAlpha); + } + if (was_disabled || disabled) + g.CurrentItemFlags |= ImGuiItemFlags_Disabled; + g.ItemFlagsStack.push_back(g.CurrentItemFlags); +} + +void ImGui::EndDisabled() +{ + ImGuiContext& g = *GImGui; + bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; + //PopItemFlag(); + g.ItemFlagsStack.pop_back(); + g.CurrentItemFlags = g.ItemFlagsStack.back(); + if (was_disabled && (g.CurrentItemFlags & ImGuiItemFlags_Disabled) == 0) + g.Style.Alpha = g.DisabledAlphaBackup; //PopStyleVar(); } // FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. @@ -6934,11 +7072,11 @@ void ImGui::SetItemDefaultFocus() ImGuiWindow* window = g.CurrentWindow; if (!window->Appearing) return; - if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == g.NavWindow->DC.NavLayerCurrent) + if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == window->DC.NavLayerCurrent) { g.NavInitRequest = false; - g.NavInitResultId = g.NavWindow->DC.LastItemId; - g.NavInitResultRectRel = ImRect(g.NavWindow->DC.LastItemRect.Min - g.NavWindow->Pos, g.NavWindow->DC.LastItemRect.Max - g.NavWindow->Pos); + g.NavInitResultId = g.LastItemData.ID; + g.NavInitResultRectRel = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); NavUpdateAnyRequestFlag(); if (!IsItemVisible()) SetScrollHereY(); @@ -7076,7 +7214,7 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() ImGuiContext& g = *GImGui; // Check user IM_ASSERT macro - // (IF YOU GET A WARNING OR COMPILE ERROR HERE: it means you assert macro is incorrectly defined! + // (IF YOU GET A WARNING OR COMPILE ERROR HERE: it means your assert macro is incorrectly defined! // If your macro uses multiple statements, it NEEDS to be surrounded by a 'do { ... } while (0)' block. // This is a common C/C++ idiom to allow multiple statements macros to be used in control flow blocks.) // #define IM_ASSERT(EXPR) if (SomeCode(EXPR)) SomeMoreCode(); // Wrong! @@ -7089,8 +7227,7 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() IM_ASSERT((g.IO.DeltaTime > 0.0f || g.FrameCount == 0) && "Need a positive DeltaTime!"); IM_ASSERT((g.FrameCount == 0 || g.FrameCountEnded == g.FrameCount) && "Forgot to call Render() or EndFrame() at the end of the previous frame?"); IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); - IM_ASSERT(g.IO.Fonts->Fonts.Size > 0 && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); - IM_ASSERT(g.IO.Fonts->Fonts[0]->IsLoaded() && "Font Atlas not built. Did you call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()?"); + IM_ASSERT(g.IO.Fonts->IsBuilt() && "Font Atlas not built! Make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.CircleTessellationMaxError > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting!"); // Allows us to avoid a few clamps in color computations @@ -7155,13 +7292,11 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi ImGuiContext& g = *GImGui; while (g.CurrentWindowStack.Size > 0) { -#ifdef IMGUI_HAS_TABLE while (g.CurrentTable && (g.CurrentTable->OuterWindow == g.CurrentWindow || g.CurrentTable->InnerWindow == g.CurrentWindow)) { if (log_callback) log_callback(user_data, "Recovered from missing EndTable() in '%s'", g.CurrentTable->OuterWindow->Name); EndTable(); } -#endif ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(window != NULL); while (g.CurrentTabBar != NULL) //-V1044 @@ -7331,14 +7466,21 @@ void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. -bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) +bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGuiItemAddFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + // Set item data + g.LastItemData.ID = id; + g.LastItemData.Rect = bb; + g.LastItemData.InFlags = g.CurrentItemFlags; + g.LastItemData.StatusFlags = ImGuiItemStatusFlags_None; + + // Directional navigation processing if (id != 0) { - // Navigation processing runs prior to clipping early-out + // Runs prior to clipping early-out // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests // unfortunately, but it is still limited to one window. It may not scale very well for windows with ten of @@ -7347,7 +7489,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. - window->DC.NavLayerActiveMaskNext |= (1 << window->DC.NavLayerCurrent); + window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) @@ -7362,11 +7504,6 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) } #endif } - - // Equivalent to calling SetLastItemData() - window->DC.LastItemId = id; - window->DC.LastItemRect = bb; - window->DC.LastItemStatusFlags = ImGuiItemStatusFlags_None; g.NextItemData.Flags = ImGuiNextItemDataFlags_None; #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -7380,9 +7517,14 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg) return false; //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] + // Tab stop handling (previously was using internal ItemFocusable() api) + // FIXME-NAV: We would now want to move this above the clipping test, but this would require being able to scroll and currently this would mean an extra frame. (#4079, #343) + if (flags & ImGuiItemAddFlags_Focusable) + ItemFocusable(window, id); + // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (IsMouseHoveringRect(bb.Min, bb.Max)) - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredRect; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; return true; } @@ -7714,24 +7856,24 @@ void ImGui::EndGroup() const bool group_contains_curr_active_id = (group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId; const bool group_contains_prev_active_id = (group_data.BackupActiveIdPreviousFrameIsAlive == false) && (g.ActiveIdPreviousFrameIsAlive == true); if (group_contains_curr_active_id) - window->DC.LastItemId = g.ActiveId; + g.LastItemData.ID = g.ActiveId; else if (group_contains_prev_active_id) - window->DC.LastItemId = g.ActiveIdPreviousFrame; - window->DC.LastItemRect = group_bb; + g.LastItemData.ID = g.ActiveIdPreviousFrame; + g.LastItemData.Rect = group_bb; // Forward Hovered flag const bool group_contains_curr_hovered_id = (group_data.BackupHoveredIdIsAlive == false) && g.HoveredId != 0; if (group_contains_curr_hovered_id) - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HoveredWindow; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredWindow; // Forward Edited flag if (group_contains_curr_active_id && g.ActiveIdHasBeenEditedThisFrame) - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Edited; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Edited; // Forward Deactivated flag - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDeactivated; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDeactivated; if (group_contains_prev_active_id && g.ActiveId != g.ActiveIdPreviousFrame) - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_Deactivated; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Deactivated; g.GroupStack.pop_back(); //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] @@ -7760,24 +7902,29 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window) ImVec2 scroll = window->Scroll; if (window->ScrollTarget.x < FLT_MAX) { + float decoration_total_width = window->ScrollbarSizes.x; float center_x_ratio = window->ScrollTargetCenterRatio.x; float scroll_target_x = window->ScrollTarget.x; - float snap_x_min = 0.0f; - float snap_x_max = window->ScrollMax.x + window->Size.x; if (window->ScrollTargetEdgeSnapDist.x > 0.0f) + { + float snap_x_min = 0.0f; + float snap_x_max = window->ScrollMax.x + window->SizeFull.x - decoration_total_width; scroll_target_x = CalcScrollEdgeSnap(scroll_target_x, snap_x_min, snap_x_max, window->ScrollTargetEdgeSnapDist.x, center_x_ratio); - scroll.x = scroll_target_x - center_x_ratio * (window->SizeFull.x - window->ScrollbarSizes.x); + } + scroll.x = scroll_target_x - center_x_ratio * (window->SizeFull.x - decoration_total_width); } if (window->ScrollTarget.y < FLT_MAX) { - float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); + float decoration_total_height = window->TitleBarHeight() + window->MenuBarHeight() + window->ScrollbarSizes.y; float center_y_ratio = window->ScrollTargetCenterRatio.y; float scroll_target_y = window->ScrollTarget.y; - float snap_y_min = 0.0f; - float snap_y_max = window->ScrollMax.y + window->Size.y - decoration_up_height; if (window->ScrollTargetEdgeSnapDist.y > 0.0f) + { + float snap_y_min = 0.0f; + float snap_y_max = window->ScrollMax.y + window->SizeFull.y - decoration_total_height; scroll_target_y = CalcScrollEdgeSnap(scroll_target_y, snap_y_min, snap_y_max, window->ScrollTargetEdgeSnapDist.y, center_y_ratio); - scroll.y = scroll_target_y - center_y_ratio * (window->SizeFull.y - window->ScrollbarSizes.y - decoration_up_height); + } + scroll.y = scroll_target_y - center_y_ratio * (window->SizeFull.y - decoration_total_height); } scroll.x = IM_FLOOR(ImMax(scroll.x, 0.0f)); scroll.y = IM_FLOOR(ImMax(scroll.y, 0.0f)); @@ -7890,7 +8037,8 @@ void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio) { IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); - local_y -= window->TitleBarHeight() + window->MenuBarHeight(); // FIXME: Would be nice to have a more standardized access to our scrollable/client rect + const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); // FIXME: Would be nice to have a more standardized access to our scrollable/client rect; + local_y -= decoration_up_height; window->ScrollTarget.y = IM_FLOOR(local_y + window->Scroll.y); // Convert local position to scroll offset window->ScrollTargetCenterRatio.y = center_y_ratio; window->ScrollTargetEdgeSnapDist.y = 0.0f; @@ -7913,8 +8061,8 @@ void ImGui::SetScrollHereX(float center_x_ratio) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - float spacing_x = g.Style.ItemSpacing.x; - float target_pos_x = ImLerp(window->DC.LastItemRect.Min.x - spacing_x, window->DC.LastItemRect.Max.x + spacing_x, center_x_ratio); + float spacing_x = ImMax(window->WindowPadding.x, g.Style.ItemSpacing.x); + float target_pos_x = ImLerp(g.LastItemData.Rect.Min.x - spacing_x, g.LastItemData.Rect.Max.x + spacing_x, center_x_ratio); SetScrollFromPosX(window, target_pos_x - window->Pos.x, center_x_ratio); // Convert from absolute to local pos // Tweak: snap on edges when aiming at an item very close to the edge @@ -7926,7 +8074,7 @@ void ImGui::SetScrollHereY(float center_y_ratio) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - float spacing_y = g.Style.ItemSpacing.y; + float spacing_y = ImMax(window->WindowPadding.y, g.Style.ItemSpacing.y); float target_pos_y = ImLerp(window->DC.CursorPosPrevLine.y - spacing_y, window->DC.CursorPosPrevLine.y + window->DC.PrevLineSize.y + spacing_y, center_y_ratio); SetScrollFromPosY(window, target_pos_y - window->Pos.y, center_y_ratio); // Convert from absolute to local pos @@ -8057,6 +8205,11 @@ void ImGui::OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags) OpenPopupEx(g.CurrentWindow->GetID(str_id), popup_flags); } +void ImGui::OpenPopup(ImGuiID id, ImGuiPopupFlags popup_flags) +{ + OpenPopupEx(id, popup_flags); +} + // Mark popup as open (toggle toward open state). // Popups are closed when user click outside, or activate a pressable item, or CloseCurrentPopup() is called within a BeginPopup()/EndPopup() block. // Popup identifiers are relative to the current ID-stack (so OpenPopup and BeginPopup needs to be at the same level). @@ -8305,28 +8458,41 @@ void ImGui::EndPopup() // - This is essentially the same as BeginPopupContextItem() but without the trailing BeginPopup() void ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiPopupFlags popup_flags) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) { - ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! - IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) + ImGuiID id = str_id ? window->GetID(str_id) : g.LastItemData.ID; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! + IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) OpenPopupEx(id, popup_flags); } } // This is a helper to handle the simplest case of associating one named popup to one given widget. -// - You can pass a NULL str_id to use the identifier of the last item. -// - You may want to handle this on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). -// - This is essentially the same as calling OpenPopupOnItemClick() + BeginPopup() but written to avoid -// computing the ID twice because BeginPopupContextXXX functions may be called very frequently. +// - To create a popup associated to the last item, you generally want to pass a NULL value to str_id. +// - To create a popup with a specific identifier, pass it in str_id. +// - This is useful when using using BeginPopupContextItem() on an item which doesn't have an identifier, e.g. a Text() call. +// - This is useful when multiple code locations may want to manipulate/open the same popup, given an explicit id. +// - You may want to handle the whole on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). +// This is essentially the same as: +// id = str_id ? GetID(str_id) : GetItemID(); +// OpenPopupOnItemClick(str_id); +// return BeginPopup(id); +// Which is essentially the same as: +// id = str_id ? GetID(str_id) : GetItemID(); +// if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) +// OpenPopup(id); +// return BeginPopup(id); +// The main difference being that this is tweaked to avoid computing the ID twice. bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flags) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; - ImGuiID id = str_id ? window->GetID(str_id) : window->DC.LastItemId; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! - IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) + ImGuiID id = str_id ? window->GetID(str_id) : g.LastItemData.ID; // If user hasn't passed an ID, we can use the LastItemID. Using LastItemID as a Popup ID won't conflict! + IM_ASSERT(id != 0); // You cannot pass a NULL str_id if the last item has no identifier (e.g. a Text() item) int mouse_button = (popup_flags & ImGuiPopupFlags_MouseButtonMask_); if (IsMouseReleased(mouse_button) && IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) OpenPopupEx(id, popup_flags); @@ -8335,7 +8501,8 @@ bool ImGui::BeginPopupContextItem(const char* str_id, ImGuiPopupFlags popup_flag bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiPopupFlags popup_flags) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; if (!str_id) str_id = "window_context"; ImGuiID id = window->GetID(str_id); @@ -8348,7 +8515,8 @@ bool ImGui::BeginPopupContextWindow(const char* str_id, ImGuiPopupFlags popup_fl bool ImGui::BeginPopupContextVoid(const char* str_id, ImGuiPopupFlags popup_flags) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; if (!str_id) str_id = "void_context"; ImGuiID id = window->GetID(str_id); @@ -8439,7 +8607,7 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s } // Note that this is used for popups, which can overlap the non work-area of individual viewports. -ImRect ImGui::GetWindowAllowedExtentRect(ImGuiWindow* window) +ImRect ImGui::GetPopupAllowedExtentRect(ImGuiWindow* window) { ImGuiContext& g = *GImGui; IM_UNUSED(window); @@ -8453,13 +8621,13 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) { ImGuiContext& g = *GImGui; - ImRect r_outer = GetWindowAllowedExtentRect(window); + ImRect r_outer = GetPopupAllowedExtentRect(window); if (window->Flags & ImGuiWindowFlags_ChildMenu) { // Child menus typically request _any_ position within the parent menu item, and then we move the new menu outside the parent bounds. // This is how we end up with child menus appearing (most-commonly) on the right of the parent menu. IM_ASSERT(g.CurrentWindow == window); - ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.Size - 2]; + ImGuiWindow* parent_window = g.CurrentWindowStack[g.CurrentWindowStack.Size - 2].Window; float horizontal_overlap = g.Style.ItemInnerSpacing.x; // We want some overlap to convey the relative depth of each menu (currently the amount of overlap is hard-coded to style.ItemSpacing.x). ImRect r_avoid; if (parent_window->DC.MenuBarAppending) @@ -8494,13 +8662,13 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) //----------------------------------------------------------------------------- // FIXME-NAV: The existence of SetNavID vs SetFocusID properly needs to be clarified/reworked. -void ImGui::SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) +void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindow != NULL); IM_ASSERT(nav_layer == ImGuiNavLayer_Main || nav_layer == ImGuiNavLayer_Menu); g.NavId = id; - g.NavLayer = (ImGuiNavLayer)nav_layer; + g.NavLayer = nav_layer; g.NavFocusScopeId = focus_scope_id; g.NavWindow->NavLastIds[nav_layer] = id; g.NavWindow->NavRectRel[nav_layer] = rect_rel; @@ -8523,8 +8691,8 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) g.NavLayer = nav_layer; g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; window->NavLastIds[nav_layer] = id; - if (window->DC.LastItemId == id) - window->NavRectRel[nav_layer] = ImRect(window->DC.LastItemRect.Min - window->Pos, window->DC.LastItemRect.Max - window->Pos); + if (g.LastItemData.ID == id) + window->NavRectRel[nav_layer] = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); if (g.ActiveIdSource == ImGuiInputSource_Nav) g.NavDisableMouseHover = true; @@ -8563,7 +8731,7 @@ static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect } // Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057 -static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) +static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -8621,7 +8789,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) else { // Degenerate case: two overlapping buttons with same center, break ties arbitrarily (note that LastItemId here is really the _previous_ item order, but it doesn't matter) - quadrant = (window->DC.LastItemId < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right; + quadrant = (g.LastItemData.ID < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right; } #if IMGUI_DEBUG_NAV_SCORING @@ -8694,7 +8862,7 @@ static bool ImGui::NavScoreItem(ImGuiNavMoveResult* result, ImRect cand) return new_best; } -static void ImGui::NavApplyItemToResult(ImGuiNavMoveResult* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel) +static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel) { result->Window = window; result->ID = id; @@ -8709,19 +8877,20 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con //if (!g.IO.NavActive) // [2017/10/06] Removed this possibly redundant test but I am not sure of all the side-effects yet. Some of the feature here will need to work regardless of using a _NoNavInputs flag. // return; - const ImGuiItemFlags item_flags = window->DC.ItemFlags; + const ImGuiItemFlags item_flags = g.LastItemData.InFlags; const ImRect nav_bb_rel(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); // Process Init Request if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent) { // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback - if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus) || g.NavInitResultId == 0) + const bool candidate_for_nav_default_focus = (item_flags & (ImGuiItemFlags_NoNavDefaultFocus | ImGuiItemFlags_Disabled)) == 0; + if (candidate_for_nav_default_focus || g.NavInitResultId == 0) { g.NavInitResultId = id; g.NavInitResultRectRel = nav_bb_rel; } - if (!(item_flags & ImGuiItemFlags_NoNavDefaultFocus)) + if (candidate_for_nav_default_focus) { g.NavInitRequest = false; // Found a match, clear request NavUpdateAnyRequestFlag(); @@ -8732,7 +8901,7 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy) if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) { - ImGuiNavMoveResult* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; + ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; #if IMGUI_DEBUG_NAV_SCORING // [DEBUG] Score all items in NavWindow at all times if (!g.NavMoveRequest) @@ -8759,7 +8928,6 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con g.NavLayer = window->DC.NavLayerCurrent; g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; g.NavIdIsAlive = true; - g.NavIdTabCounter = window->DC.FocusCounterTabStop; window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel; // Store item bounding box (relative to window position) } } @@ -8851,10 +9019,16 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) { ImGuiContext& g = *GImGui; IM_ASSERT(window == g.NavWindow); + + if (window->Flags & ImGuiWindowFlags_NoNavInputs) + { + g.NavId = g.NavFocusScopeId = 0; + return; + } + bool init_for_nav = false; - if (!(window->Flags & ImGuiWindowFlags_NoNavInputs)) - if (window == window->RootWindow || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) - init_for_nav = true; + if (window == window->RootWindow || (window->Flags & ImGuiWindowFlags_Popup) || (window->NavLastIds[0] == 0) || force_reinit) + init_for_nav = true; IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from NavInitWindow(), init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); if (init_for_nav) { @@ -8968,8 +9142,6 @@ static void ImGui::NavUpdate() io.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; if (io.KeyShift) io.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; - if (io.KeyAlt && !io.KeyCtrl) // AltGR is Alt+Ctrl, also even on keyboards without AltGR we don't want Alt+Ctrl to open menu. - io.NavInputs[ImGuiNavInput_KeyMenu_] = 1.0f; #undef NAV_MAP_KEY } memcpy(io.NavInputsDownDurationPrev, io.NavInputsDownDuration, sizeof(io.NavInputsDownDuration)); @@ -9035,6 +9207,11 @@ static void ImGui::NavUpdate() if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) ClearActiveID(); } + else if (g.NavLayer != ImGuiNavLayer_Main) + { + // Leave the "menu" layer + NavRestoreLayer(ImGuiNavLayer_Main); + } else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) { // Exit child window @@ -9051,11 +9228,6 @@ static void ImGui::NavUpdate() if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); } - else if (g.NavLayer != ImGuiNavLayer_Main) - { - // Leave the "menu" layer - NavRestoreLayer(ImGuiNavLayer_Main); - } else { // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were @@ -9145,7 +9317,7 @@ static void ImGui::NavUpdate() // *Fallback* manual-scroll with Nav directional keys when window has no navigable item ImGuiWindow* window = g.NavWindow; const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * io.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. - if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) { if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) SetScrollX(window, ImFloor(window->Scroll.x + ((g.NavMoveDir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); @@ -9211,6 +9383,7 @@ static void ImGui::NavUpdateInitResult() return; // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) + // FIXME-NAV: On _NavFlattened windows, g.NavWindow will only be updated during subsequent frame. Not a problem currently. IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); if (g.NavInitRequestFromMove) @@ -9236,7 +9409,7 @@ static void ImGui::NavUpdateMoveResult() } // Select which result to use - ImGuiNavMoveResult* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; + ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; // PageUp/PageDown behavior first jumps to the bottom/top mostly visible item, _otherwise_ use the result from the previous/next page. if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) @@ -9303,7 +9476,7 @@ static float ImGui::NavUpdatePageUpPageDown() const bool end_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); if (page_up_held != page_down_held || home_pressed != end_pressed) // If either (not both) are pressed { - if (window->DC.NavLayerActiveMask == 0x00 && window->DC.NavHasScroll) + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) { // Fallback manual-scroll when window has no navigable item if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) @@ -9421,13 +9594,13 @@ static void ImGui::NavEndFrame() } } -static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) // FIXME-OPT O(N) +static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) { ImGuiContext& g = *GImGui; - for (int i = g.WindowsFocusOrder.Size - 1; i >= 0; i--) - if (g.WindowsFocusOrder[i] == window) - return i; - return -1; + IM_UNUSED(g); + int order = window->FocusOrder; + IM_ASSERT(g.WindowsFocusOrder[order] == window); + return order; } static ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // FIXME-OPT O(N) @@ -9461,6 +9634,8 @@ static void NavUpdateWindowingHighlightWindow(int focus_change_dir) static void ImGui::NavUpdateWindowing() { ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + ImGuiWindow* apply_focus_window = NULL; bool apply_toggle_layer = false; @@ -9472,25 +9647,25 @@ static void ImGui::NavUpdateWindowing() // Fade out if (g.NavWindowingTargetAnim && g.NavWindowingTarget == NULL) { - g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha - g.IO.DeltaTime * 10.0f, 0.0f); + g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha - io.DeltaTime * 10.0f, 0.0f); if (g.DimBgRatio <= 0.0f && g.NavWindowingHighlightAlpha <= 0.0f) g.NavWindowingTargetAnim = NULL; } // Start CTRL-TAB or Square+L/R window selection bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); - bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); + bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { - g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; // FIXME-DOCK: Will need to use RootWindowDockStop + g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; - g.NavWindowingToggleLayer = start_windowing_with_keyboard ? false : true; + g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; } // Gamepad update - g.NavWindowingTimer += g.IO.DeltaTime; + g.NavWindowingTimer += io.DeltaTime; if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_Gamepad) { // Highlight only appears after a brief time holding the button, so that a fast tap on PadMenu (to toggle NavLayer) doesn't add visual noise @@ -9522,31 +9697,49 @@ static void ImGui::NavUpdateWindowing() // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f if (IsKeyPressedMap(ImGuiKey_Tab, true)) - NavUpdateWindowingHighlightWindow(g.IO.KeyShift ? +1 : -1); - if (!g.IO.KeyCtrl) + NavUpdateWindowingHighlightWindow(io.KeyShift ? +1 : -1); + if (!io.KeyCtrl) apply_focus_window = g.NavWindowingTarget; } // Keyboard: Press and Release ALT to toggle menu layer - // FIXME: We lack an explicit IO variable for "is the imgui window focused", so compare mouse validity to detect the common case of backend clearing releases all keys on ALT-TAB - if (IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Pressed)) + // - Testing that only Alt is tested prevents Alt+Shift or AltGR from toggling menu layer. + // - AltGR is normally Alt+Ctrl but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). But even on keyboards without AltGR we don't want Alt+Ctrl to open menu anyway. + if (io.KeyMods == ImGuiKeyModFlags_Alt && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) == 0) + { g.NavWindowingToggleLayer = true; - if ((g.ActiveId == 0 || g.ActiveIdAllowOverlap) && g.NavWindowingToggleLayer && IsNavInputTest(ImGuiNavInput_KeyMenu_, ImGuiInputReadMode_Released)) - if (IsMousePosValid(&g.IO.MousePos) == IsMousePosValid(&g.IO.MousePosPrev)) - apply_toggle_layer = true; + g.NavInputSource = ImGuiInputSource_Keyboard; + } + if (g.NavWindowingToggleLayer && g.NavInputSource == ImGuiInputSource_Keyboard) + { + // We cancel toggling nav layer when any text has been typed (generally while holding Alt). (See #370) + // We cancel toggling nav layer when other modifiers are pressed. (See #4439) + if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper) + g.NavWindowingToggleLayer = false; + + // Apply layer toggle on release + // Important: we don't assume that Alt was previously held in order to handle loss of focus when backend calls io.AddFocusEvent(false) + // Important: as before version <18314 we lacked an explicit IO event for focus gain/loss, we also compare mouse validity to detect old backends clearing mouse pos on focus loss. + if (!(io.KeyMods & ImGuiKeyModFlags_Alt) && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) && g.NavWindowingToggleLayer) + if (g.ActiveId == 0 || g.ActiveIdAllowOverlap) + if (IsMousePosValid(&io.MousePos) == IsMousePosValid(&io.MousePosPrev)) + apply_toggle_layer = true; + if (!io.KeyAlt) + g.NavWindowingToggleLayer = false; + } // Move window if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove)) { ImVec2 move_delta; - if (g.NavInputSource == ImGuiInputSource_Keyboard && !g.IO.KeyShift) + if (g.NavInputSource == ImGuiInputSource_Keyboard && !io.KeyShift) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); if (g.NavInputSource == ImGuiInputSource_Gamepad) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); if (move_delta.x != 0.0f || move_delta.y != 0.0f) { const float NAV_MOVE_SPEED = 800.0f; - const float move_speed = ImFloor(NAV_MOVE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); // FIXME: Doesn't handle variable framerate very well + const float move_speed = ImFloor(NAV_MOVE_SPEED * io.DeltaTime * ImMin(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y)); // FIXME: Doesn't handle variable framerate very well ImGuiWindow* moving_window = g.NavWindowingTarget->RootWindow; SetWindowPos(moving_window, moving_window->Pos + move_delta * move_speed, ImGuiCond_Always); MarkIniSettingsDirty(moving_window); @@ -9566,8 +9759,14 @@ static void ImGui::NavUpdateWindowing() if (apply_focus_window->NavLastIds[0] == 0) NavInitWindow(apply_focus_window, false); - // If the window only has a menu layer, select it directly - if (apply_focus_window->DC.NavLayerActiveMask == (1 << ImGuiNavLayer_Menu)) + // If the window has ONLY a menu layer (no main layer), select it directly + // Use NavLayersActiveMaskNext since windows didn't have a chance to be Begin()-ed on this frame, + // so CTRL+Tab where the keys are only held for 1 frame will be able to use correct layers mask since + // the target window as already been previewed once. + // FIXME-NAV: This should be done in NavInit.. or in FocusWindow... However in both of those cases, + // we won't have a guarantee that windows has been visible before and therefore NavLayersActiveMask* + // won't be valid. + if (apply_focus_window->DC.NavLayersActiveMaskNext == (1 << ImGuiNavLayer_Menu)) g.NavLayer = ImGuiNavLayer_Menu; } if (apply_focus_window) @@ -9576,10 +9775,12 @@ static void ImGui::NavUpdateWindowing() // Apply menu/layer toggle if (apply_toggle_layer && g.NavWindow) { + ClearActiveID(); + // Move to parent menu if necessary ImGuiWindow* new_nav_window = g.NavWindow; while (new_nav_window->ParentWindow - && (new_nav_window->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) == 0 + && (new_nav_window->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Menu)) == 0 && (new_nav_window->Flags & ImGuiWindowFlags_ChildWindow) != 0 && (new_nav_window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) == 0) new_nav_window = new_nav_window->ParentWindow; @@ -9593,7 +9794,7 @@ static void ImGui::NavUpdateWindowing() g.NavDisableMouseHover = true; // Reinitialize navigation when entering menu bar with the Alt key. - const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayerActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; + const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; if (new_nav_layer == ImGuiNavLayer_Menu) g.NavWindow->NavLastIds[new_nav_layer] = 0; NavRestoreLayer(new_nav_layer); @@ -9629,6 +9830,7 @@ void ImGui::NavUpdateWindowingOverlay() for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--) { ImGuiWindow* window = g.WindowsFocusOrder[n]; + IM_ASSERT(window != NULL); // Fix static analyzers if (!IsWindowNavFocusable(window)) continue; const char* label = window->Name; @@ -9680,7 +9882,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) ImGuiID source_parent_id = 0; if (!(flags & ImGuiDragDropFlags_SourceExtern)) { - source_id = window->DC.LastItemId; + source_id = g.LastItemData.ID; if (source_id != 0) { // Common path: items with ID @@ -9707,15 +9909,16 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) } // Early out - if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) + if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) return false; // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() // We build a throwaway ID based on current ID stack + relative AABB of items in window. // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. - source_id = window->DC.LastItemId = window->GetIDFromRectangle(window->DC.LastItemRect); - bool is_hovered = ItemHoverable(window->DC.LastItemRect, source_id); + // Rely on keeping other window->LastItemXXX fields intact. + source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); + bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id); if (is_hovered && g.IO.MouseClicked[mouse_button]) { SetActiveID(source_id, window); @@ -9729,10 +9932,8 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) source_parent_id = window->IDStack.back(); source_drag_active = IsMouseDragging(mouse_button); - // Disable navigation and key inputs while dragging - g.ActiveIdUsingNavDirMask = ~(ImU32)0; - g.ActiveIdUsingNavInputMask = ~(ImU32)0; - g.ActiveIdUsingKeyInputMask = ~(ImU64)0; + // Disable navigation and key inputs while dragging + cancel existing request if any + SetActiveIdUsingNavAndKeys(); } else { @@ -9767,13 +9968,13 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) { ImGuiWindow* tooltip_window = g.CurrentWindow; - tooltip_window->SkipItems = true; + tooltip_window->Hidden = tooltip_window->SkipItems = true; tooltip_window->HiddenFramesCanSkipItems = 1; } } if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) - window->DC.LastItemStatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; + g.LastItemData.StatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; return true; } @@ -9873,14 +10074,14 @@ bool ImGui::BeginDragDropTarget() return false; ImGuiWindow* window = g.CurrentWindow; - if (!(window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect)) + if (!(g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect)) return false; ImGuiWindow* hovered_window = g.HoveredWindowUnderMovingWindow; if (hovered_window == NULL || window->RootWindow != hovered_window->RootWindow) return false; - const ImRect& display_rect = (window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? window->DC.LastItemDisplayRect : window->DC.LastItemRect; - ImGuiID id = window->DC.LastItemId; + const ImRect& display_rect = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? g.LastItemData.DisplayRect : g.LastItemData.Rect; + ImGuiID id = g.LastItemData.ID; if (id == 0) id = window->GetIDFromRectangle(display_rect); if (g.DragDropPayload.SourceId == id) @@ -9922,17 +10123,11 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop } // Render default drop visuals + // FIXME-DRAGDROP: Settle on a proper default visuals for drop target. payload.Preview = was_accepted_previously; flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) - { - // FIXME-DRAGDROP: Settle on a proper default visuals for drop target. - r.Expand(3.5f); - bool push_clip_rect = !window->ClipRect.Contains(r); - if (push_clip_rect) window->DrawList->PushClipRect(r.Min - ImVec2(1, 1), r.Max + ImVec2(1, 1)); - window->DrawList->AddRect(r.Min, r.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); - if (push_clip_rect) window->DrawList->PopClipRect(); - } + window->DrawList->AddRect(r.Min - ImVec2(3.5f,3.5f), r.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); g.DragDropAcceptFrameCount = g.FrameCount; payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting os window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() @@ -10494,8 +10689,9 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); } IM_ASSERT(settings->ID == window->ID); - settings->Pos = ImVec2ih((short)window->Pos.x, (short)window->Pos.y); - settings->Size = ImVec2ih((short)window->SizeFull.x, (short)window->SizeFull.y); + settings->Pos = ImVec2ih(window->Pos); + settings->Size = ImVec2ih(window->SizeFull); + settings->Collapsed = window->Collapsed; } @@ -10545,9 +10741,9 @@ static void ImGui::UpdateViewportsNewFrame() ImGuiViewportP* viewport = g.Viewports[n]; // Lock down space taken by menu bars and status bars, reset the offset for fucntions like BeginMainMenuBar() to alter them again. - viewport->WorkOffsetMin = viewport->CurrWorkOffsetMin; - viewport->WorkOffsetMax = viewport->CurrWorkOffsetMax; - viewport->CurrWorkOffsetMin = viewport->CurrWorkOffsetMax = ImVec2(0.0f, 0.0f); + viewport->WorkOffsetMin = viewport->BuildWorkOffsetMin; + viewport->WorkOffsetMax = viewport->BuildWorkOffsetMax; + viewport->BuildWorkOffsetMin = viewport->BuildWorkOffsetMax = ImVec2(0.0f, 0.0f); viewport->UpdateWorkRect(); } } @@ -10804,6 +11000,10 @@ static void MetricsHelpMarker(const char* desc) } } +#ifndef IMGUI_DISABLE_DEMO_WINDOWS +namespace ImGui { void ShowFontAtlas(ImFontAtlas* atlas); } +#endif + void ImGui::ShowMetricsWindow(bool* p_open) { if (!Begin("Dear ImGui Metrics/Debugger", p_open)) @@ -10906,10 +11106,10 @@ void ImGui::ShowMetricsWindow(bool* p_open) cfg->ShowTablesRects |= Combo("##show_table_rects_type", &cfg->ShowTablesRectsType, trt_rects_names, TRT_Count, TRT_Count); if (cfg->ShowTablesRects && g.NavWindow != NULL) { - for (int table_n = 0; table_n < g.Tables.GetSize(); table_n++) + for (int table_n = 0; table_n < g.Tables.GetMapSize(); table_n++) { - ImGuiTable* table = g.Tables.GetByIndex(table_n); - if (table->LastFrameActive < g.FrameCount - 1 || (table->OuterWindow != g.NavWindow && table->InnerWindow != g.NavWindow)) + ImGuiTable* table = g.Tables.TryGetMapData(table_n); + if (table == NULL || table->LastFrameActive < g.FrameCount - 1 || (table->OuterWindow != g.NavWindow && table->InnerWindow != g.NavWindow)) continue; BulletText("Table 0x%08X (%d columns, in '%s')", table->ID, table->ColumnsCount, table->OuterWindow->Name); @@ -10991,22 +11191,36 @@ void ImGui::ShowMetricsWindow(bool* p_open) } // Details for TabBars - if (TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.GetSize())) + if (TreeNode("TabBars", "Tab Bars (%d)", g.TabBars.GetAliveCount())) { - for (int n = 0; n < g.TabBars.GetSize(); n++) - DebugNodeTabBar(g.TabBars.GetByIndex(n), "TabBar"); + for (int n = 0; n < g.TabBars.GetMapSize(); n++) + if (ImGuiTabBar* tab_bar = g.TabBars.TryGetMapData(n)) + { + PushID(tab_bar); + DebugNodeTabBar(tab_bar, "TabBar"); + PopID(); + } TreePop(); } // Details for Tables -#ifdef IMGUI_HAS_TABLE - if (TreeNode("Tables", "Tables (%d)", g.Tables.GetSize())) + if (TreeNode("Tables", "Tables (%d)", g.Tables.GetAliveCount())) { - for (int n = 0; n < g.Tables.GetSize(); n++) - DebugNodeTable(g.Tables.GetByIndex(n)); + for (int n = 0; n < g.Tables.GetMapSize(); n++) + if (ImGuiTable* table = g.Tables.TryGetMapData(n)) + DebugNodeTable(table); TreePop(); } -#endif // #ifdef IMGUI_HAS_TABLE + + // Details for Fonts +#ifndef IMGUI_DISABLE_DEMO_WINDOWS + ImFontAtlas* atlas = g.IO.Fonts; + if (TreeNode("Fonts", "Fonts (%d)", atlas->Fonts.Size)) + { + ShowFontAtlas(atlas); + TreePop(); + } +#endif // Details for Docking #ifdef IMGUI_HAS_DOCK @@ -11046,14 +11260,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) TreePop(); } -#ifdef IMGUI_HAS_TABLE if (TreeNode("SettingsTables", "Settings packed data: Tables: %d bytes", g.SettingsTables.size())) { for (ImGuiTableSettings* settings = g.SettingsTables.begin(); settings != NULL; settings = g.SettingsTables.next_chunk(settings)) DebugNodeTableSettings(settings); TreePop(); } -#endif // #ifdef IMGUI_HAS_TABLE #ifdef IMGUI_HAS_DOCK #endif // #ifdef IMGUI_HAS_DOCK @@ -11069,7 +11281,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Misc Details if (TreeNode("Internal state")) { - const char* input_source_names[] = { "None", "Mouse", "Nav", "NavKeyboard", "NavGamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); + const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Nav", "Clipboard" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); Text("WINDOWING"); Indent(); @@ -11083,7 +11295,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) Indent(); Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); - Text("HoveredId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredId, g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Data is "in-flight" so depending on when the Metrics window is called we may see current frame information or not + Text("ActiveIdUsing: Wheel: %d, NavDirMask: %X, NavInputMask: %X, KeyInputMask: %llX", g.ActiveIdUsingMouseWheel, g.ActiveIdUsingNavDirMask, g.ActiveIdUsingNavInputMask, g.ActiveIdUsingKeyInputMask); + Text("HoveredId: 0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Not displaying g.HoveredId as it is update mid-frame Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); Unindent(); @@ -11127,14 +11340,13 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } -#ifdef IMGUI_HAS_TABLE // Overlay: Display Tables Rectangles if (cfg->ShowTablesRects) { - for (int table_n = 0; table_n < g.Tables.GetSize(); table_n++) + for (int table_n = 0; table_n < g.Tables.GetMapSize(); table_n++) { - ImGuiTable* table = g.Tables.GetByIndex(table_n); - if (table->LastFrameActive < g.FrameCount - 1) + ImGuiTable* table = g.Tables.TryGetMapData(table_n); + if (table == NULL || table->LastFrameActive < g.FrameCount - 1) continue; ImDrawList* draw_list = GetForegroundDrawList(table->OuterWindow); if (cfg->ShowTablesRectsType >= TRT_ColumnsRect) @@ -11154,7 +11366,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } } -#endif // #ifdef IMGUI_HAS_TABLE #ifdef IMGUI_HAS_DOCK // Overlay: Display Docking info @@ -11166,6 +11377,25 @@ void ImGui::ShowMetricsWindow(bool* p_open) End(); } +// [DEBUG] List fonts in a font atlas and display its texture +void ImGui::ShowFontAtlas(ImFontAtlas* atlas) +{ + for (int i = 0; i < atlas->Fonts.Size; i++) + { + ImFont* font = atlas->Fonts[i]; + PushID(font); + DebugNodeFont(font); + PopID(); + } + if (TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) + { + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); + Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), tint_col, border_col); + TreePop(); + } +} + // [DEBUG] Display contents of Columns void ImGui::DebugNodeColumns(ImGuiOldColumns* columns) { @@ -11275,16 +11505,17 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb) { IM_ASSERT(show_mesh || show_aabb); - ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; - ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + draw_cmd->VtxOffset; // Draw wire-frame version of all triangles ImRect clip_rect = draw_cmd->ClipRect; ImRect vtxs_rect(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); ImDrawListFlags backup_flags = out_draw_list->Flags; out_draw_list->Flags &= ~ImDrawListFlags_AntiAliasedLines; // Disable AA on triangle outlines is more readable for very large and thin triangles. - for (unsigned int idx_n = draw_cmd->IdxOffset; idx_n < draw_cmd->IdxOffset + draw_cmd->ElemCount; ) + for (unsigned int idx_n = draw_cmd->IdxOffset, idx_end = draw_cmd->IdxOffset + draw_cmd->ElemCount; idx_n < idx_end; ) { + ImDrawIdx* idx_buffer = (draw_list->IdxBuffer.Size > 0) ? draw_list->IdxBuffer.Data : NULL; // We don't hold on those pointers past iterations as ->AddPolyline() may invalidate them if out_draw_list==draw_list + ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data + draw_cmd->VtxOffset; + ImVec2 triangle[3]; for (int n = 0; n < 3; n++, idx_n++) vtxs_rect.Add((triangle[n] = vtx_buffer[idx_buffer ? idx_buffer[idx_n] : idx_n].pos)); @@ -11300,6 +11531,102 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co out_draw_list->Flags = backup_flags; } +// [DEBUG] Display details for a single font, called by ShowStyleEditor(). +void ImGui::DebugNodeFont(ImFont* font) +{ + bool opened = TreeNode(font, "Font: \"%s\"\n%.2f px, %d glyphs, %d file(s)", + font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size, font->ConfigDataCount); + SameLine(); + if (SmallButton("Set as default")) + GetIO().FontDefault = font; + if (!opened) + return; + + // Display preview text + PushFont(font); + Text("The quick brown fox jumps over the lazy dog"); + PopFont(); + + // Display details + SetNextItemWidth(GetFontSize() * 8); + DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); + SameLine(); MetricsHelpMarker( + "Note than the default embedded font is NOT meant to be scaled.\n\n" + "Font are currently rendered into bitmaps at a given size at the time of building the atlas. " + "You may oversample them to get some flexibility with scaling. " + "You can also render at multiple sizes and select which one to use at runtime.\n\n" + "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)"); + Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); + char c_str[5]; + Text("Fallback character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->FallbackChar), font->FallbackChar); + Text("Ellipsis character: '%s' (U+%04X)", ImTextCharToUtf8(c_str, font->EllipsisChar), font->EllipsisChar); + const int surface_sqrt = (int)ImSqrt((float)font->MetricsTotalSurface); + Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, surface_sqrt, surface_sqrt); + for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) + if (font->ConfigData) + if (const ImFontConfig* cfg = &font->ConfigData[config_i]) + BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d, Offset: (%.1f,%.1f)", + config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH, cfg->GlyphOffset.x, cfg->GlyphOffset.y); + + // Display all glyphs of the fonts in separate pages of 256 characters + if (TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) + { + ImDrawList* draw_list = GetWindowDrawList(); + const ImU32 glyph_col = GetColorU32(ImGuiCol_Text); + const float cell_size = font->FontSize * 1; + const float cell_spacing = GetStyle().ItemSpacing.y; + for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256) + { + // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k) + // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT + // is large // (if ImWchar==ImWchar32 we will do at least about 272 queries here) + if (!(base & 4095) && font->IsGlyphRangeUnused(base, base + 4095)) + { + base += 4096 - 256; + continue; + } + + int count = 0; + for (unsigned int n = 0; n < 256; n++) + if (font->FindGlyphNoFallback((ImWchar)(base + n))) + count++; + if (count <= 0) + continue; + if (!TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) + continue; + + // Draw a 16x16 grid of glyphs + ImVec2 base_pos = GetCursorScreenPos(); + for (unsigned int n = 0; n < 256; n++) + { + // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions + // available here and thus cannot easily generate a zero-terminated UTF-8 encoded string. + ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); + ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); + const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); + draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); + if (glyph) + font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); + if (glyph && IsMouseHoveringRect(cell_p1, cell_p2)) + { + BeginTooltip(); + Text("Codepoint: U+%04X", base + n); + Separator(); + Text("Visible: %d", glyph->Visible); + Text("AdvanceX: %.1f", glyph->AdvanceX); + Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); + Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); + EndTooltip(); + } + } + Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16)); + TreePop(); + } + TreePop(); + } + TreePop(); +} + // [DEBUG] Display contents of ImGuiStorage void ImGui::DebugNodeStorage(ImGuiStorage* storage, const char* label) { @@ -11322,9 +11649,16 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) const char* buf_end = buf + IM_ARRAYSIZE(buf); const bool is_active = (tab_bar->PrevFrameVisible >= GetFrameCount() - 2); p += ImFormatString(p, buf_end - p, "%s 0x%08X (%d tabs)%s", label, tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); - IM_UNUSED(p); + p += ImFormatString(p, buf_end - p, " { "); + for (int tab_n = 0; tab_n < ImMin(tab_bar->Tabs.Size, 3); tab_n++) + { + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + p += ImFormatString(p, buf_end - p, "%s'%s'", + tab_n > 0 ? ", " : "", (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "???"); + } + p += ImFormatString(p, buf_end - p, (tab_bar->Tabs.Size > 3) ? " ... }" : " } "); if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } - bool open = TreeNode(tab_bar, "%s", buf); + bool open = TreeNode(label, "%s", buf); if (!is_active) { PopStyleColor(); } if (is_active && IsItemHovered()) { @@ -11342,7 +11676,7 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) if (SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } SameLine(0, 2); if (SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } SameLine(); Text("%02d%c Tab 0x%08X '%s' Offset: %.1f, Width: %.1f/%.1f", - tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "", tab->Offset, tab->Width, tab->ContentWidth); + tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "???", tab->Offset, tab->Width, tab->ContentWidth); PopID(); } TreePop(); @@ -11401,12 +11735,19 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f) Scrollbar:%s%s", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y, window->ScrollbarX ? "X" : "", window->ScrollbarY ? "Y" : ""); BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); - BulletText("NavLastIds: 0x%08X,0x%08X, NavLayerActiveMask: %X", window->NavLastIds[0], window->NavLastIds[1], window->DC.NavLayerActiveMask); - BulletText("NavLastChildNavWindow: %s", window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); - if (!window->NavRectRel[0].IsInverted()) - BulletText("NavRectRel[0]: (%.1f,%.1f)(%.1f,%.1f)", window->NavRectRel[0].Min.x, window->NavRectRel[0].Min.y, window->NavRectRel[0].Max.x, window->NavRectRel[0].Max.y); - else - BulletText("NavRectRel[0]: "); + for (int layer = 0; layer < ImGuiNavLayer_COUNT; layer++) + { + ImRect r = window->NavRectRel[layer]; + if (r.Min.x >= r.Max.y && r.Min.y >= r.Max.y) + { + BulletText("NavLastIds[%d]: 0x%08X", layer, window->NavLastIds[layer]); + continue; + } + BulletText("NavLastIds[%d]: 0x%08X at +(%.1f,%.1f)(%.1f,%.1f)", layer, window->NavLastIds[layer], r.Min.x, r.Min.y, r.Max.x, r.Max.y); + if (IsItemHovered()) + GetForegroundDrawList(window)->AddRect(r.Min + window->Pos, r.Max + window->Pos, IM_COL32(255, 255, 0, 255)); + } + BulletText("NavLayersActiveMask: %X, NavLastChildNavWindow: %s", window->DC.NavLayersActiveMask, window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } if (window->ParentWindow != NULL) { DebugNodeWindow(window->ParentWindow, "ParentWindow"); } if (window->DC.ChildWindows.Size > 0) { DebugNodeWindowsList(&window->DC.ChildWindows, "ChildWindows"); } @@ -11443,9 +11784,11 @@ void ImGui::DebugNodeWindowsList(ImVector* windows, const char* la #else void ImGui::ShowMetricsWindow(bool*) {} +void ImGui::ShowFontAtlas(ImFontAtlas*) {} void ImGui::DebugNodeColumns(ImGuiOldColumns*) {} void ImGui::DebugNodeDrawList(ImGuiWindow*, const ImDrawList*, const char*) {} void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList*, const ImDrawList*, const ImDrawCmd*, bool, bool) {} +void ImGui::DebugNodeFont(ImFont*) {} void ImGui::DebugNodeStorage(ImGuiStorage*, const char*) {} void ImGui::DebugNodeTabBar(ImGuiTabBar*, const char*) {} void ImGui::DebugNodeWindow(ImGuiWindow*, const char*) {} diff --git a/imgui/imgui.h b/imgui/imgui.h index 874ed8f2..b56cb852 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (headers) // Help: @@ -11,9 +11,9 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/3488 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/4451 (please post your screenshots/video there!) +// - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary -// - Wiki https://github.com/ocornut/imgui/wiki // - Issues & support https://github.com/ocornut/imgui/issues // - Discussions https://github.com/ocornut/imgui/discussions @@ -60,8 +60,8 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.82" -#define IMGUI_VERSION_NUM 18200 +#define IMGUI_VERSION "1.84.2" +#define IMGUI_VERSION_NUM 18405 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE @@ -89,18 +89,31 @@ Index of this file: #endif // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. -#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__clang__) -#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) -#define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) -#elif !defined(IMGUI_USE_STB_SPRINTF) && defined(__GNUC__) && defined(__MINGW32__) +#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) #define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) #define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0))) +#elif !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__)) +#define IM_FMTARGS(FMT) __attribute__((format(printf, FMT, FMT+1))) +#define IM_FMTLIST(FMT) __attribute__((format(printf, FMT, 0))) #else #define IM_FMTARGS(FMT) #define IM_FMTLIST(FMT) #endif +// Disable some of MSVC most aggressive Debug runtime checks in function header/footer (used in some simple/low-level functions) +#if defined(_MSC_VER) && !defined(__clang__) && !defined(IMGUI_DEBUG_PARANOID) +#define IM_MSVC_RUNTIME_CHECKS_OFF __pragma(runtime_checks("",off)) __pragma(check_stack(off)) __pragma(strict_gs_check(push,off)) +#define IM_MSVC_RUNTIME_CHECKS_RESTORE __pragma(runtime_checks("",restore)) __pragma(check_stack()) __pragma(strict_gs_check(pop)) +#else +#define IM_MSVC_RUNTIME_CHECKS_OFF +#define IM_MSVC_RUNTIME_CHECKS_RESTORE +#endif + // Warnings +#ifdef _MSC_VER +#pragma warning (push) +#pragma warning (disable: 26495) // [Static Analyzer] Variable 'XXX' is uninitialized. Always initialize a member variable (type.6). +#endif #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wold-style-cast" @@ -147,7 +160,7 @@ struct ImGuiTextBuffer; // Helper to hold and append into a text buf struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbbb][,ccccc]") struct ImGuiViewport; // A Platform Window (always only one in 'master' branch), in the future may represent Platform Monitor -// Enums/Flags (declared as int for compatibility with old C++, to allow using as flags and to not pollute the top of this file) +// Enums/Flags (declared as int for compatibility with old C++, to allow using as flags without overhead, and to not pollute the top of this file) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! // In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. @@ -187,27 +200,22 @@ typedef int ImGuiTreeNodeFlags; // -> enum ImGuiTreeNodeFlags_ // Flags: f typedef int ImGuiViewportFlags; // -> enum ImGuiViewportFlags_ // Flags: for ImGuiViewport typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: for Begin(), BeginChild() -// Other types -#ifndef ImTextureID // ImTextureID [configurable type: override in imconfig.h with '#define ImTextureID xxx'] -typedef void* ImTextureID; // User data for rendering backend to identify a texture. This is whatever to you want it to be! read the FAQ about ImTextureID for details. +// ImTexture: user data for renderer backend to identify a texture [Compile-time configurable type] +// - To use something else than an opaque void* pointer: override with e.g. '#define ImTextureID MyTextureType*' in your imconfig.h file. +// - This can be whatever to you want it to be! read the FAQ about ImTextureID for details. +#ifndef ImTextureID +typedef void* ImTextureID; // Default: store a pointer or an integer fitting in a pointer (most renderer backends are ok with that) #endif -typedef unsigned int ImGuiID; // A unique ID used by widgets, typically hashed from a stack of string. -typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); // Callback function for ImGui::InputText() -typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints() -typedef void* (*ImGuiMemAllocFunc)(size_t sz, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() -typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() -// Character types -// (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) -typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. -typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. -#ifdef IMGUI_USE_WCHAR32 // ImWchar [configurable type: override in imconfig.h with '#define IMGUI_USE_WCHAR32' to support Unicode planes 1-16] -typedef ImWchar32 ImWchar; -#else -typedef ImWchar16 ImWchar; +// ImDrawIdx: vertex index. [Compile-time configurable type] +// - To use 16-bit indices + allow large meshes: backend need to set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset (recommended). +// - To use 32-bit indices: override with '#define ImDrawIdx unsigned int' in your imconfig.h file. +#ifndef ImDrawIdx +typedef unsigned short ImDrawIdx; // Default: 16-bit (for maximum compatibility with renderer backends) #endif -// Basic scalar data types +// Scalar data types +typedef unsigned int ImGuiID;// A unique ID used by widgets (typically the result of hashing a stack of string) typedef signed char ImS8; // 8-bit signed integer typedef unsigned char ImU8; // 8-bit unsigned integer typedef signed short ImS16; // 16-bit signed integer @@ -226,7 +234,25 @@ typedef signed long long ImS64; // 64-bit signed integer (post C++11) typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) #endif -// 2D vector (often used to store positions or sizes) +// Character types +// (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) +typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. +typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. +#ifdef IMGUI_USE_WCHAR32 // ImWchar [configurable type: override in imconfig.h with '#define IMGUI_USE_WCHAR32' to support Unicode planes 1-16] +typedef ImWchar32 ImWchar; +#else +typedef ImWchar16 ImWchar; +#endif + +// Callback and functions types +typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); // Callback function for ImGui::InputText() +typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints() +typedef void* (*ImGuiMemAllocFunc)(size_t sz, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() +typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() + +// ImVec2: 2D vector used to store positions, sizes etc. [Compile-time configurable type] +// This is a frequently used type in the API. Consider using IM_VEC2_CLASS_EXTRA to create implicit cast from/to our preferred type. +IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec2 { float x, y; @@ -239,7 +265,7 @@ struct ImVec2 #endif }; -// 4D vector (often used to store floating-point colors) +// ImVec4: 4D vector used to store clipping rectangles, colors etc. [Compile-time configurable type] struct ImVec4 { float x, y, z, w; @@ -249,6 +275,7 @@ struct ImVec4 IM_VEC4_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4. #endif }; +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] Dear ImGui end-user API functions @@ -328,7 +355,8 @@ namespace ImGui IMGUI_API float GetWindowWidth(); // get current window width (shortcut for GetWindowSize().x) IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) - // Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). + // Window manipulation + // - Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). IMGUI_API void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0, 0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc. IMGUI_API void SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0); // set next window size. set axis to 0.0f to force an auto-fit on this axis. call before Begin() IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use -1,-1 on either X/Y axis to preserve the current size. Sizes will be rounded down. Use callback to apply non-trivial programmatic constraints. @@ -340,7 +368,7 @@ namespace ImGui IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0, 0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. IMGUI_API void SetWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // (not recommended) set current window collapsed state. prefer using SetNextWindowCollapsed(). IMGUI_API void SetWindowFocus(); // (not recommended) set current window to be focused / top-most. prefer using SetNextWindowFocus(). - IMGUI_API void SetWindowFontScale(float scale); // set font scale. Adjust IO.FontGlobalScale if you want to scale all windows. This is an old API! For correct scaling, prefer to reload font + rebuild ImFontAtlas + call style.ScaleAllSizes(). + IMGUI_API void SetWindowFontScale(float scale); // [OBSOLETE] set font scale. Adjust IO.FontGlobalScale if you want to scale all windows. This is an old API! For correct scaling, prefer to reload font + rebuild ImFontAtlas + call style.ScaleAllSizes(). IMGUI_API void SetWindowPos(const char* name, const ImVec2& pos, ImGuiCond cond = 0); // set named window position. IMGUI_API void SetWindowSize(const char* name, const ImVec2& size, ImGuiCond cond = 0); // set named window size. set axis to 0.0f to force an auto-fit on this axis. IMGUI_API void SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0); // set named window collapsed state @@ -376,7 +404,7 @@ namespace ImGui IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame(). IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. always use this if you modify the style after NewFrame(). IMGUI_API void PopStyleVar(int count = 1); - IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets + IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // == tab stop enable. Allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets IMGUI_API void PopAllowKeyboardFocus(); IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. IMGUI_API void PopButtonRepeat(); @@ -390,6 +418,7 @@ namespace ImGui IMGUI_API void PopTextWrapPos(); // Style read access + // - Use the style editor (ShowStyleEditor() function) to interactively see what the colors are) IMGUI_API ImFont* GetFont(); // get current font IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API @@ -430,11 +459,15 @@ namespace ImGui IMGUI_API float GetFrameHeightWithSpacing(); // ~ FontSize + style.FramePadding.y * 2 + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of framed widgets) // ID stack/scopes - // - Read the FAQ for more details about how ID are handled in dear imgui. If you are creating widgets in a loop you most - // likely want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them. - // - The resulting ID are hashes of the entire stack. + // Read the FAQ (docs/FAQ.md or http://dearimgui.org/faq) for more details about how ID are handled in dear imgui. + // - Those questions are answered and impacted by understanding of the ID stack system: + // - "Q: Why is my widget not reacting when I click on it?" + // - "Q: How can I have widgets with an empty label?" + // - "Q: How can I have multiple widgets with the same label?" + // - Short version: ID are hashes of the entire ID stack. If you are creating widgets in a loop you most likely + // want to push a unique identifier (e.g. object pointer, loop index) to uniquely differentiate them. // - You can also use the "Label##foobar" syntax within widget label to distinguish them from each others. - // - In this header file we use the "label"/"name" terminology to denote a string that will be displayed and used as an ID, + // - In this header file we use the "label"/"name" terminology to denote a string that will be displayed + used as an ID, // whereas "str_id" denote a string that is only used as an ID and not normally displayed. IMGUI_API void PushID(const char* str_id); // push string into the ID stack (will hash string). IMGUI_API void PushID(const char* str_id_begin, const char* str_id_end); // push string into the ID stack (will hash string). @@ -507,8 +540,8 @@ namespace ImGui IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", ImGuiSliderFlags flags = 0); IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL, ImGuiSliderFlags flags = 0); - IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); - IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed = 1.0f, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); + IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed = 1.0f, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); // Widgets: Regular Sliders // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. @@ -597,7 +630,7 @@ namespace ImGui IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); // Widgets: Data Plotting - // - Consider using ImPlot (https://github.com/epezent/implot) + // - Consider using ImPlot (https://github.com/epezent/implot) which is much better! IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); IMGUI_API void PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); @@ -614,13 +647,14 @@ namespace ImGui // - Use BeginMenuBar() on a window ImGuiWindowFlags_MenuBar to append to its menu bar. // - Use BeginMainMenuBar() to create a menu bar at the top of the screen and append to it. // - Use BeginMenu() to create a menu. You can call BeginMenu() multiple time with the same identifier to append more items to it. + // - Not that MenuItem() keyboardshortcuts are displayed as a convenience but _not processed_ by Dear ImGui at the moment. IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window). IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true! IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. IMGUI_API void EndMainMenuBar(); // only call EndMainMenuBar() if BeginMainMenuBar() returns true! IMGUI_API bool BeginMenu(const char* label, bool enabled = true); // create a sub-menu entry. only call EndMenu() if this returns true! IMGUI_API void EndMenu(); // only call EndMenu() if BeginMenu() returns true! - IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true); // return true when activated. shortcuts are displayed for convenience but not processed by ImGui at the moment + IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true); // return true when activated. IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL // Tooltips @@ -638,30 +672,36 @@ namespace ImGui // - You can bypass the hovering restriction by using ImGuiHoveredFlags_AllowWhenBlockedByPopup when calling IsItemHovered() or IsWindowHovered(). // - IMPORTANT: Popup identifiers are relative to the current ID stack, so OpenPopup and BeginPopup generally needs to be at the same level of the stack. // This is sometimes leading to confusing mistakes. May rework this in the future. + // Popups: begin/end functions // - BeginPopup(): query popup state, if open start appending into the window. Call EndPopup() afterwards. ImGuiWindowFlags are forwarded to the window. // - BeginPopupModal(): block every interactions behind the window, cannot be closed by user, add a dimming background, has a title bar. IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // return true if the modal is open, and you can start outputting to it. IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! + // Popups: open/close functions // - OpenPopup(): set popup state to open. ImGuiPopupFlags are available for opening options. // - If not modal: they can be closed by clicking anywhere outside them, or by pressing ESCAPE. // - CloseCurrentPopup(): use inside the BeginPopup()/EndPopup() scope to close manually. // - CloseCurrentPopup() is called by default by Selectable()/MenuItem() when activated (FIXME: need some options). // - Use ImGuiPopupFlags_NoOpenOverExistingPopup to avoid opening a popup if there's already one at the same level. This is equivalent to e.g. testing for !IsAnyPopupOpen() prior to OpenPopup(). + // - Use IsWindowAppearing() after BeginPopup() to tell if a window just opened. IMGUI_API void OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags = 0); // call to mark popup as open (don't call every frame!). - IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. return true when just opened. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) + IMGUI_API void OpenPopup(ImGuiID id, ImGuiPopupFlags popup_flags = 0); // id overload to facilitate calling from nested stacks + IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. Default to ImGuiPopupFlags_MouseButtonRight == 1. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) IMGUI_API void CloseCurrentPopup(); // manually close the popup we have begin-ed into. + // Popups: open+begin combined functions helpers // - Helpers to do OpenPopup+BeginPopup where the Open action is triggered by e.g. hovering an item and right-clicking. // - They are convenient to easily create context menus, hence the name. // - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future. // - IMPORTANT: we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight. - IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. if you can pass a NULL str_id only if the previous item had an id. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! + IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. Use str_id==NULL to associate the popup to previous item. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window. IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows). - // Popups: test function + + // Popups: query functions // - IsPopupOpen(): return true if the popup is open at the current BeginPopup() level of the popup stack. // - IsPopupOpen() with ImGuiPopupFlags_AnyPopupId: return true if any popup is open at the current BeginPopup() level of the popup stack. // - IsPopupOpen() with ImGuiPopupFlags_AnyPopupId + ImGuiPopupFlags_AnyPopupLevel: return true if any popup is open. @@ -697,6 +737,7 @@ namespace ImGui IMGUI_API void TableNextRow(ImGuiTableRowFlags row_flags = 0, float min_row_height = 0.0f); // append into the first cell of a new row. IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return true when column is visible. IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return true when column is visible. + // Tables: Headers & Columns declaration // - Use TableSetupColumn() to specify label, resizing policy, default width/weight, id, various other flags etc. // - Use TableHeadersRow() to create a header row and automatically submit a TableHeader() for each column. @@ -709,6 +750,7 @@ namespace ImGui IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) + // Tables: Sorting // - Call TableGetSortSpecs() to retrieve latest sort specs for the table. NULL when not sorting. // - When 'SpecsDirty == true' you should sort your data. It will be true when sorting specs have changed @@ -716,6 +758,7 @@ namespace ImGui // wastefully sort your data every frame! // - Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). + // Tables: Miscellaneous functions // - Functions args 'int column_n' treat the default value of -1 as the same as passing the current column index. IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable) @@ -723,9 +766,10 @@ namespace ImGui IMGUI_API int TableGetRowIndex(); // return current row index. IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column. IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags. Pass -1 to use current column. + IMGUI_API void TableSetColumnEnabled(int column_n, bool v);// change user accessible enabled/disabled state of a column. Set to false to hide the column. User can use the context menu to change this themselves (right-click in headers, or right-click in columns body with ImGuiTableFlags_ContextMenuInBody) IMGUI_API void TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n = -1); // change the color of a cell, row, or column. See ImGuiTableBgTarget_ flags for details. - // Legacy Columns API (2020: prefer using Tables!) + // Legacy Columns API (prefer using Tables!) // - You can also use SameLine(pos_x) to mimic simplified columns. IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished @@ -767,6 +811,12 @@ namespace ImGui IMGUI_API void EndDragDropTarget(); // only call EndDragDropTarget() if BeginDragDropTarget() returns true! IMGUI_API const ImGuiPayload* GetDragDropPayload(); // peek directly into the current payload from anywhere. may return NULL. use ImGuiPayload::IsDataType() to test for the payload type. + // Disabling [BETA API] + // - Disable all user interactions and dim items visuals (applying style.DisabledAlpha over current colors) + // - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. + IMGUI_API void BeginDisabled(bool disabled = true); + IMGUI_API void EndDisabled(); + // Clipping // - Mouse hovering is affected by ImGui::PushClipRect() calls, unlike direct calls to ImDrawList::PushClipRect() which are render only. IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect); @@ -777,7 +827,7 @@ namespace ImGui IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of a window. IMGUI_API void SetKeyboardFocusHere(int offset = 0); // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget. - // Item/Widgets Utilities + // Item/Widgets Utilities and Query Functions // - Most of the functions are referring to the previous Item that has been submitted. // - See Demo Window under "Widgets->Querying Status" for an interactive visualization of most of those functions. IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags = 0); // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options. @@ -802,7 +852,7 @@ namespace ImGui // - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. // - In 'docking' branch with multi-viewport enabled, we extend this concept to have multiple active viewports. // - In the future we will extend this concept further to also represent Platform Monitor and support a "no main platform window" operation mode. - IMGUI_API ImGuiViewport* GetMainViewport(); // return primary/default viewport. + IMGUI_API ImGuiViewport* GetMainViewport(); // return primary/default viewport. This can never be NULL. // Miscellaneous Utilities IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped. @@ -866,12 +916,14 @@ namespace ImGui // Settings/.Ini Utilities // - The disk functions are automatically called if io.IniFilename != NULL (default is "imgui.ini"). // - Set io.IniFilename to NULL to load/save manually. Read io.WantSaveIniSettings description about handling .ini saving manually. + // - Important: default value "imgui.ini" is relative to current working dir! Most apps will want to lock this to an absolute path (e.g. same path as executables). IMGUI_API void LoadIniSettingsFromDisk(const char* ini_filename); // call after CreateContext() and before the first call to NewFrame(). NewFrame() automatically calls LoadIniSettingsFromDisk(io.IniFilename). IMGUI_API void LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size=0); // call after CreateContext() and before the first call to NewFrame() to provide .ini data from your own data source. IMGUI_API void SaveIniSettingsToDisk(const char* ini_filename); // this is automatically called (if io.IniFilename is not empty) a few seconds after any modification that should be reflected in the .ini file (and also by DestroyContext). IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. // Debug Utilities + // - This is used by the IMGUI_CHECKVERSION() macro. IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. // Memory Allocators @@ -912,7 +964,7 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 16, // Ensure child windows without border uses style.WindowPadding (ignored by default for non-bordered child windows, because more convenient) ImGuiWindowFlags_NoNavInputs = 1 << 18, // No gamepad/keyboard navigation within the window ImGuiWindowFlags_NoNavFocus = 1 << 19, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) - ImGuiWindowFlags_UnsavedDocument = 1 << 20, // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. When used in a tab/docking context, tab is selected on closure and closure is deferred by one frame to allow code to cancel the closure (with a confirmation popup, etc.) without flicker. + ImGuiWindowFlags_UnsavedDocument = 1 << 20, // Display a dot next to the title. When used in a tab/docking context, tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, @@ -952,10 +1004,7 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) - ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) - // [Internal] - ImGuiInputTextFlags_Multiline = 1 << 20, // For internal use by InputTextMultiline() - ImGuiInputTextFlags_NoMarkEdited = 1 << 21 // For internal use by functions using InputText() before reformatting data + ImGuiInputTextFlags_CallbackEdit = 1 << 19 // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) // Obsolete names (will be removed soon) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1053,7 +1102,7 @@ enum ImGuiTabBarFlags_ enum ImGuiTabItemFlags_ { ImGuiTabItemFlags_None = 0, - ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Append '*' to title without affecting the ID, as a convenience to avoid using the ### operator. Also: tab is selected on closure and closure is deferred by one frame to allow code to undo it without flicker. + ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Display a dot next to the title + tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. ImGuiTabItemFlags_NoPushId = 1 << 3, // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() @@ -1147,28 +1196,30 @@ enum ImGuiTableColumnFlags_ { // Input configuration flags ImGuiTableColumnFlags_None = 0, - ImGuiTableColumnFlags_DefaultHide = 1 << 0, // Default as a hidden/disabled column. - ImGuiTableColumnFlags_DefaultSort = 1 << 1, // Default as a sorting column. - ImGuiTableColumnFlags_WidthStretch = 1 << 2, // Column will stretch. Preferable with horizontal scrolling disabled (default if table sizing policy is _SizingStretchSame or _SizingStretchProp). - ImGuiTableColumnFlags_WidthFixed = 1 << 3, // Column will not stretch. Preferable with horizontal scrolling enabled (default if table sizing policy is _SizingFixedFit and table is resizable). - ImGuiTableColumnFlags_NoResize = 1 << 4, // Disable manual resizing. - ImGuiTableColumnFlags_NoReorder = 1 << 5, // Disable manual reordering this column, this will also prevent other columns from crossing over this column. - ImGuiTableColumnFlags_NoHide = 1 << 6, // Disable ability to hide/disable this column. - ImGuiTableColumnFlags_NoClip = 1 << 7, // Disable clipping for this column (all NoClip columns will render in a same draw command). - ImGuiTableColumnFlags_NoSort = 1 << 8, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). - ImGuiTableColumnFlags_NoSortAscending = 1 << 9, // Disable ability to sort in the ascending direction. - ImGuiTableColumnFlags_NoSortDescending = 1 << 10, // Disable ability to sort in the descending direction. - ImGuiTableColumnFlags_NoHeaderWidth = 1 << 11, // Disable header text width contribution to automatic column width. - ImGuiTableColumnFlags_PreferSortAscending = 1 << 12, // Make the initial sort direction Ascending when first sorting on this column (default). - ImGuiTableColumnFlags_PreferSortDescending = 1 << 13, // Make the initial sort direction Descending when first sorting on this column. - ImGuiTableColumnFlags_IndentEnable = 1 << 14, // Use current Indent value when entering cell (default for column 0). - ImGuiTableColumnFlags_IndentDisable = 1 << 15, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored. + ImGuiTableColumnFlags_Disabled = 1 << 0, // Overriding/master disable flag: hide column, won't show in context menu (unlike calling TableSetColumnEnabled() which manipulates the user accessible state) + ImGuiTableColumnFlags_DefaultHide = 1 << 1, // Default as a hidden/disabled column. + ImGuiTableColumnFlags_DefaultSort = 1 << 2, // Default as a sorting column. + ImGuiTableColumnFlags_WidthStretch = 1 << 3, // Column will stretch. Preferable with horizontal scrolling disabled (default if table sizing policy is _SizingStretchSame or _SizingStretchProp). + ImGuiTableColumnFlags_WidthFixed = 1 << 4, // Column will not stretch. Preferable with horizontal scrolling enabled (default if table sizing policy is _SizingFixedFit and table is resizable). + ImGuiTableColumnFlags_NoResize = 1 << 5, // Disable manual resizing. + ImGuiTableColumnFlags_NoReorder = 1 << 6, // Disable manual reordering this column, this will also prevent other columns from crossing over this column. + ImGuiTableColumnFlags_NoHide = 1 << 7, // Disable ability to hide/disable this column. + ImGuiTableColumnFlags_NoClip = 1 << 8, // Disable clipping for this column (all NoClip columns will render in a same draw command). + ImGuiTableColumnFlags_NoSort = 1 << 9, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). + ImGuiTableColumnFlags_NoSortAscending = 1 << 10, // Disable ability to sort in the ascending direction. + ImGuiTableColumnFlags_NoSortDescending = 1 << 11, // Disable ability to sort in the descending direction. + ImGuiTableColumnFlags_NoHeaderLabel = 1 << 12, // TableHeadersRow() will not submit label for this column. Convenient for some small columns. Name will still appear in context menu. + ImGuiTableColumnFlags_NoHeaderWidth = 1 << 13, // Disable header text width contribution to automatic column width. + ImGuiTableColumnFlags_PreferSortAscending = 1 << 14, // Make the initial sort direction Ascending when first sorting on this column (default). + ImGuiTableColumnFlags_PreferSortDescending = 1 << 15, // Make the initial sort direction Descending when first sorting on this column. + ImGuiTableColumnFlags_IndentEnable = 1 << 16, // Use current Indent value when entering cell (default for column 0). + ImGuiTableColumnFlags_IndentDisable = 1 << 17, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored. // Output status flags, read-only via TableGetColumnFlags() - ImGuiTableColumnFlags_IsEnabled = 1 << 20, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags. - ImGuiTableColumnFlags_IsVisible = 1 << 21, // Status: is visible == is enabled AND not clipped by scrolling. - ImGuiTableColumnFlags_IsSorted = 1 << 22, // Status: is currently part of the sort specs - ImGuiTableColumnFlags_IsHovered = 1 << 23, // Status: is hovered by mouse + ImGuiTableColumnFlags_IsEnabled = 1 << 24, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags. + ImGuiTableColumnFlags_IsVisible = 1 << 25, // Status: is visible == is enabled AND not clipped by scrolling. + ImGuiTableColumnFlags_IsSorted = 1 << 26, // Status: is currently part of the sort specs + ImGuiTableColumnFlags_IsHovered = 1 << 27, // Status: is hovered by mouse // [Internal] Combinations and masks ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed, @@ -1355,13 +1406,12 @@ enum ImGuiNavInput_ // [Internal] Don't use directly! This is used internally to differentiate keyboard from gamepad inputs for behaviors that require to differentiate them. // Keyboard behavior that have no corresponding gamepad mapping (e.g. CTRL+TAB) will be directly reading from io.KeysDown[] instead of io.NavInputs[]. - ImGuiNavInput_KeyMenu_, // toggle menu // = io.KeyAlt ImGuiNavInput_KeyLeft_, // move left // = Arrow keys ImGuiNavInput_KeyRight_, // move right ImGuiNavInput_KeyUp_, // move up ImGuiNavInput_KeyDown_, // move down ImGuiNavInput_COUNT, - ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyMenu_ + ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyLeft_ }; // Configuration flags stored in io.ConfigFlags. Set by user/application. @@ -1460,6 +1510,7 @@ enum ImGuiStyleVar_ { // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) ImGuiStyleVar_Alpha, // float Alpha + ImGuiStyleVar_DisabledAlpha, // float DisabledAlpha ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding ImGuiStyleVar_WindowRounding, // float WindowRounding ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize @@ -1531,13 +1582,13 @@ enum ImGuiColorEditFlags_ // Defaults Options. You can set application defaults using SetColorEditOptions(). The intent is that you probably don't want to // override them in most of your calls. Let the user choose via the option menu and/or call SetColorEditOptions() once during startup. - ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_PickerHueBar, + ImGuiColorEditFlags_DefaultOptions_ = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_PickerHueBar, // [Internal] Masks - ImGuiColorEditFlags__DisplayMask = ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_DisplayHex, - ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_Float, - ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_PickerHueBar, - ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV + ImGuiColorEditFlags_DisplayMask_ = ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_DisplayHex, + ImGuiColorEditFlags_DataTypeMask_ = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_Float, + ImGuiColorEditFlags_PickerMask_ = ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_PickerHueBar, + ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1631,6 +1682,7 @@ template void IM_DELETE(T* p) { if (p) { p->~T(); ImGui::MemFree(p // Do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset. //----------------------------------------------------------------------------- +IM_MSVC_RUNTIME_CHECKS_OFF template struct ImVector { @@ -1647,7 +1699,11 @@ struct ImVector inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } - inline ~ImVector() { if (Data) IM_FREE(Data); } + inline ~ImVector() { if (Data) IM_FREE(Data); } // Important: does not destruct anything + + inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } // Important: does not destruct anything + inline void clear_delete() { for (int n = 0; n < Size; n++) IM_DELETE(Data[n]); clear(); } // Important: never called automatically! always explicit. + inline void clear_destruct() { for (int n = 0; n < Size; n++) Data[n].~T(); clear(); } // Important: never called automatically! always explicit. inline bool empty() const { return Size == 0; } inline int size() const { return Size; } @@ -1657,7 +1713,6 @@ struct ImVector inline T& operator[](int i) { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } inline const T& operator[](int i) const { IM_ASSERT(i >= 0 && i < Size); return Data[i]; } - inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } inline T* begin() { return Data; } inline const T* begin() const { return Data; } inline T* end() { return Data + Size; } @@ -1689,6 +1744,7 @@ struct ImVector inline bool find_erase_unsorted(const T& v) { const T* it = find(v); if (it < Data + Size) { erase_unsorted(it); return true; } return false; } inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; return (int)off; } }; +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] ImGuiStyle @@ -1701,6 +1757,7 @@ struct ImVector struct ImGuiStyle { float Alpha; // Global alpha applies to everything in Dear ImGui. + float DisabledAlpha; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. ImVec2 WindowPadding; // Padding within a window. float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. float WindowBorderSize; // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). @@ -1763,7 +1820,7 @@ struct ImGuiIO ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size) float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. - const char* IniFilename; // = "imgui.ini" // Path to .ini file. Set NULL to disable automatic .ini loading/saving, if e.g. you want to manually load/save from memory. + const char* IniFilename; // = "imgui.ini" // Path to .ini file (important: default "imgui.ini" is relative to current working dir!). Set NULL to disable automatic .ini loading/saving or if you want to manually call LoadIniSettingsXXX() / SaveIniSettingsXXX() functions. const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. @@ -1831,6 +1888,7 @@ struct ImGuiIO IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue new character input from an UTF-16 character, it can be a surrogate IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually + IMGUI_API void AddFocusEvent(bool focused); // Notifies Dear ImGui when hosting platform windows lose or gain input focus //------------------------------------------------------------------ // Output - Updated by NewFrame() or EndFrame()/Render() @@ -1845,7 +1903,7 @@ struct ImGuiIO bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). - float Framerate; // Application framerate estimate, in frame per second. Solely for convenience. Rolling average estimation based on io.DeltaTime over 120 frames. + float Framerate; // Rough estimate of application framerate, in frame per second. Solely for convenience. Rolling average estimation based on io.DeltaTime over 120 frames. int MetricsRenderVertices; // Vertices output during last call to Render() int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 int MetricsRenderWindows; // Number of visible windows @@ -1858,6 +1916,7 @@ struct ImGuiIO //------------------------------------------------------------------ ImGuiKeyModFlags KeyMods; // Key mods flags (same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags), updated by NewFrame() + ImGuiKeyModFlags KeyModsPrev; // Previous key mods ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) ImVec2 MouseClickedPos[5]; // Position at time of clicking double MouseClickedTime[5]; // Time of last click (used to figure out double-click) @@ -2224,14 +2283,10 @@ struct ImDrawCmd void* UserCallbackData; // 4-8 // The draw callback code can access this. ImDrawCmd() { memset(this, 0, sizeof(*this)); } // Also ensure our padding fields are zeroed -}; -// Vertex index, default to 16-bit -// To allow large meshes with 16-bit indices: set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset' and handle ImDrawCmd::VtxOffset in the renderer backend (recommended). -// To use 32-bit indices: override with '#define ImDrawIdx unsigned int' in imconfig.h. -#ifndef ImDrawIdx -typedef unsigned short ImDrawIdx; -#endif + // Since 1.83: returns ImTextureID associated with this draw call. Warning: DO NOT assume this is always same as 'TextureId' (we will change this function for an upcoming feature) + inline ImTextureID GetTexID() const { return TextureId; } +}; // Vertex layout #ifndef IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT @@ -2436,6 +2491,7 @@ struct ImDrawList IMGUI_API void _ResetForNewFrame(); IMGUI_API void _ClearFreeMemory(); IMGUI_API void _PopUnusedDrawCmd(); + IMGUI_API void _TryMergeDrawCmds(); IMGUI_API void _OnChangedClipRect(); IMGUI_API void _OnChangedTextureID(); IMGUI_API void _OnChangedVtxOffset(); @@ -2586,7 +2642,7 @@ struct ImFontAtlas IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions. IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel - bool IsBuilt() const { return Fonts.Size > 0 && (TexPixelsAlpha8 != NULL || TexPixelsRGBA32 != NULL); } + bool IsBuilt() const { return Fonts.Size > 0 && TexReady; } // Bit ambiguous: used to detect when user didn't built texture but effectively we should check TexID != 0 except that would be backend dependent... void SetTexID(ImTextureID id) { TexID = id; } //------------------------------------------- @@ -2636,6 +2692,7 @@ struct ImFontAtlas // [Internal] // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you. + bool TexReady; // Set when texture was built matching current font input bool TexPixelsUseColors; // Tell whether our texture data is known to use colors (rather than just alpha channel), in order to help backend select a format. unsigned char* TexPixelsAlpha8; // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight unsigned int* TexPixelsRGBA32; // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4 @@ -2658,7 +2715,7 @@ struct ImFontAtlas #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+ - typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+ + //typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+ #endif }; @@ -2680,8 +2737,9 @@ struct ImFont ImFontAtlas* ContainerAtlas; // 4-8 // out // // What we has been loaded into const ImFontConfig* ConfigData; // 4-8 // in // // Pointer within ContainerAtlas->ConfigData short ConfigDataCount; // 2 // in // ~ 1 // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont. - ImWchar FallbackChar; // 2 // in // = '?' // Replacement character if a glyph isn't found. Only set via SetFallbackChar() - ImWchar EllipsisChar; // 2 // out // = -1 // Character used for ellipsis rendering. + ImWchar FallbackChar; // 2 // out // = FFFD/'?' // Character used if a glyph isn't found. + ImWchar EllipsisChar; // 2 // out // = '...' // Character used for ellipsis rendering. + ImWchar DotChar; // 2 // out // = '.' // Character used for ellipsis rendering (if a single '...' character isn't found) bool DirtyLookupTables; // 1 // out // float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] @@ -2711,7 +2769,6 @@ struct ImFont IMGUI_API void AddGlyph(const ImFontConfig* src_cfg, ImWchar c, float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, float advance_x); IMGUI_API void AddRemapChar(ImWchar dst, ImWchar src, bool overwrite_dst = true); // Makes 'dst' character/glyph points to 'src' character/glyph. Currently needs to be called AFTER fonts have been built. IMGUI_API void SetGlyphVisible(ImWchar c, bool visible); - IMGUI_API void SetFallbackChar(ImWchar c); IMGUI_API bool IsGlyphRangeUnused(unsigned int c_begin, unsigned int c_last); }; @@ -2719,7 +2776,7 @@ struct ImFont // [SECTION] Viewports //----------------------------------------------------------------------------- -// Flags stored in ImGuiViewport::Flags +// Flags stored in ImGuiViewport::Flags, giving indications to the platform backends. enum ImGuiViewportFlags_ { ImGuiViewportFlags_None = 0, @@ -2788,8 +2845,18 @@ namespace ImGui static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } // OBSOLETED in 1.70 (from May 2019) static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } - // OBSOLETED in 1.69 (from Mar 2019) - static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } + + // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) + //static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } // OBSOLETED in 1.69 (from Mar 2019) + //static inline void SetScrollHere(float ratio = 0.5f) { SetScrollHereY(ratio); } // OBSOLETED in 1.66 (from Nov 2018) + //static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } // OBSOLETED in 1.63 (from Aug 2018) + //static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } // OBSOLETED in 1.60 (from Apr 2018) + //static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) + //static inline void ShowTestWindow() { return ShowDemoWindow(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) } // OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect() @@ -2818,6 +2885,10 @@ enum ImDrawCornerFlags_ #pragma GCC diagnostic pop #endif +#ifdef _MSC_VER +#pragma warning (pop) +#endif + // Include imgui_user.h at the end of imgui.h (convenient for user to only explicitly include vanilla imgui.h) #ifdef IMGUI_INCLUDE_IMGUI_USER_H #include "imgui_user.h" diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index 3af6f6a8..d75989d6 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (demo code) // Help: @@ -6,9 +6,9 @@ // - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. // Read imgui.cpp for more details, documentation and comments. -// Get latest version at https://github.com/ocornut/imgui +// Get the latest version at https://github.com/ocornut/imgui -// Message to the person tempted to delete this file when integrating Dear ImGui into their code base: +// Message to the person tempted to delete this file when integrating Dear ImGui into their codebase: // Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other // coders will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available // debug menu of your game/app! Removing this file from your project is hindering access to documentation for everyone @@ -16,19 +16,19 @@ // Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow(). // If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be // linked, you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. -// In other situation, whenever you have Dear ImGui available you probably want this to be available for reference. +// In another situation, whenever you have Dear ImGui available you probably want this to be available for reference. // Thank you, // -Your beloved friend, imgui_demo.cpp (which you won't delete) // Message to beginner C/C++ programmers about the meaning of the 'static' keyword: -// In this demo code, we frequently we use 'static' variables inside functions. A static variable persist across calls, +// In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls, // so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to // gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller // in size. It also happens to be a convenient way of storing simple UI related information as long as your function // doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code, // but most of the real data you would be editing is likely going to be stored outside your functions. -// The Demo code in this file is designed to be easy to copy-and-paste in into your application! +// The Demo code in this file is designed to be easy to copy-and-paste into your application! // Because of this: // - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace. // - We try to declare static variables in the local scope, as close as possible to the code using them. @@ -92,7 +92,8 @@ Index of this file: // Visual Studio warnings #ifdef _MSC_VER -#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). #endif // Clang/GCC warnings with -Weverything @@ -316,6 +317,7 @@ void ImGui::ShowDemoWindow(bool* p_open) static bool no_nav = false; static bool no_background = false; static bool no_bring_to_front = false; + static bool unsaved_document = false; ImGuiWindowFlags window_flags = 0; if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar; @@ -327,6 +329,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (no_nav) window_flags |= ImGuiWindowFlags_NoNav; if (no_background) window_flags |= ImGuiWindowFlags_NoBackground; if (no_bring_to_front) window_flags |= ImGuiWindowFlags_NoBringToFrontOnFocus; + if (unsaved_document) window_flags |= ImGuiWindowFlags_UnsavedDocument; if (no_close) p_open = NULL; // Don't pass our bool* to Begin // We specify a default position/size in case there's no data in the .ini file. @@ -455,7 +458,7 @@ void ImGui::ShowDemoWindow(bool* p_open) { HelpMarker( "Those flags are set by the backends (imgui_impl_xxx files) to specify their capabilities.\n" - "Here we expose then as read-only fields to avoid breaking interactions with your backend."); + "Here we expose them as read-only fields to avoid breaking interactions with your backend."); // Make a local copy to avoid modifying actual backend flags. ImGuiBackendFlags backend_flags = io.BackendFlags; @@ -508,6 +511,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::TableNextColumn(); ImGui::Checkbox("No nav", &no_nav); ImGui::TableNextColumn(); ImGui::Checkbox("No background", &no_background); ImGui::TableNextColumn(); ImGui::Checkbox("No bring to front", &no_bring_to_front); + ImGui::TableNextColumn(); ImGui::Checkbox("Unsaved document", &unsaved_document); ImGui::EndTable(); } } @@ -529,6 +533,10 @@ static void ShowDemoWindowWidgets() if (!ImGui::CollapsingHeader("Widgets")) return; + static bool disable_all = false; // The Checkbox for that is inside the "Disabled" section at the bottom + if (disable_all) + ImGui::BeginDisabled(); + if (ImGui::TreeNode("Basic")) { static int clicked = 0; @@ -1025,8 +1033,8 @@ static void ShowDemoWindowWidgets() // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; static int item_current_idx = 0; // Here we store our selection data as an index. - const char* combo_label = items[item_current_idx]; // Label to preview before opening the combo (technically it could be anything) - if (ImGui::BeginCombo("combo 1", combo_label, flags)) + const char* combo_preview_value = items[item_current_idx]; // Pass in the preview value visible before opening the combo (it could be anything) + if (ImGui::BeginCombo("combo 1", combo_preview_value, flags)) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { @@ -1042,10 +1050,12 @@ static void ShowDemoWindowWidgets() } // Simplified one-liner Combo() API, using values packed in a single constant string + // This is a convenience for when the selection set is small and known at compile-time. static int item_current_2 = 0; ImGui::Combo("combo 2 (one-liner)", &item_current_2, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); // Simplified one-liner Combo() using an array of const char* + // This is not very useful (may obsolete): prefer using BeginCombo()/EndCombo() for full control. static int item_current_3 = -1; // If the selection isn't within 0..count, Combo won't display a preview ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items)); @@ -1112,7 +1122,7 @@ static void ShowDemoWindowWidgets() static bool selection[5] = { false, true, false, false, false }; ImGui::Selectable("1. I am selectable", &selection[0]); ImGui::Selectable("2. I am selectable", &selection[1]); - ImGui::Text("3. I am not selectable"); + ImGui::Text("(I am not selectable)"); ImGui::Selectable("4. I am selectable", &selection[3]); if (ImGui::Selectable("5. I am double clickable", selection[4], ImGuiSelectableFlags_AllowDoubleClick)) if (ImGui::IsMouseDoubleClicked(0)) @@ -1162,7 +1172,7 @@ static void ShowDemoWindowWidgets() { static bool selected[10] = {}; - if (ImGui::BeginTable("split1", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) + if (ImGui::BeginTable("split1", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_Borders)) { for (int i = 0; i < 10; i++) { @@ -1173,8 +1183,8 @@ static void ShowDemoWindowWidgets() } ImGui::EndTable(); } - ImGui::Separator(); - if (ImGui::BeginTable("split2", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) + ImGui::Spacing(); + if (ImGui::BeginTable("split2", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_Borders)) { for (int i = 0; i < 10; i++) { @@ -1553,7 +1563,7 @@ static void ShowDemoWindowWidgets() // Plot/Graph widgets are not very good. // Consider writing your own, or using a third-party one, see: // - ImPlot https://github.com/epezent/implot - // - others https://github.com/ocornut/imgui/wiki/Useful-Widgets + // - others https://github.com/ocornut/imgui/wiki/Useful-Extensions if (ImGui::TreeNode("Plots Widgets")) { static bool animate = true; @@ -1602,7 +1612,7 @@ static void ShowDemoWindowWidgets() }; static int func_type = 0, display_count = 70; ImGui::Separator(); - ImGui::SetNextItemWidth(100); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::Combo("func", &func_type, "Sin\0Saw\0"); ImGui::SameLine(); ImGui::SliderInt("Sample count", &display_count, 1, 400); @@ -2175,24 +2185,28 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - if (ImGui::TreeNode("Querying Status (Edited/Active/Focused/Hovered etc.)")) + if (ImGui::TreeNode("Querying Status (Edited/Active/Hovered etc.)")) { // Select an item type const char* item_names[] = { "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputFloat", - "InputFloat3", "ColorEdit4", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox" + "InputFloat3", "ColorEdit4", "Selectable", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox" }; - static int item_type = 1; + static int item_type = 4; + static bool item_disabled = false; ImGui::Combo("Item Type", &item_type, item_names, IM_ARRAYSIZE(item_names), IM_ARRAYSIZE(item_names)); ImGui::SameLine(); HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered()."); + ImGui::Checkbox("Item Disabled", &item_disabled); // Submit selected item item so we can query their status in the code following it. bool ret = false; static bool b = false; static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f }; static char str[16] = {}; + if (item_disabled) + ImGui::BeginDisabled(true); if (item_type == 0) { ImGui::Text("ITEM: Text"); } // Testing text items with no identifier/interaction if (item_type == 1) { ret = ImGui::Button("ITEM: Button"); } // Testing button if (item_type == 2) { ImGui::PushButtonRepeat(true); ret = ImGui::Button("ITEM: Button"); ImGui::PopButtonRepeat(); } // Testing button (with repeater) @@ -2202,11 +2216,12 @@ static void ShowDemoWindowWidgets() if (item_type == 6) { ret = ImGui::InputFloat("ITEM: InputFloat", col4f, 1.0f); } // Testing +/- buttons on scalar input if (item_type == 7) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) if (item_type == 8) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) - if (item_type == 9) { ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) - if (item_type == 10){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node - if (item_type == 11){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. - if (item_type == 12){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } - if (item_type == 13){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + if (item_type == 9) { ret = ImGui::Selectable("ITEM: Selectable"); } // Testing selectable item + if (item_type == 10){ ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) + if (item_type == 11){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node + if (item_type == 12){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. + if (item_type == 13){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } + if (item_type == 14){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. @@ -2219,6 +2234,7 @@ static void ShowDemoWindowWidgets() "IsItemHovered(_AllowWhenBlockedByPopup) = %d\n" "IsItemHovered(_AllowWhenBlockedByActiveItem) = %d\n" "IsItemHovered(_AllowWhenOverlapped) = %d\n" + "IsItemHovered(_AllowWhenDisabled) = %d\n" "IsItemHovered(_RectOnly) = %d\n" "IsItemActive() = %d\n" "IsItemEdited() = %d\n" @@ -2237,6 +2253,7 @@ static void ShowDemoWindowWidgets() ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlapped), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled), ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly), ImGui::IsItemActive(), ImGui::IsItemEdited(), @@ -2251,6 +2268,9 @@ static void ShowDemoWindowWidgets() ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y ); + if (item_disabled) + ImGui::EndDisabled(); + static bool embed_all_inside_a_child_window = false; ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) @@ -2320,6 +2340,18 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + + // Demonstrate BeginDisabled/EndDisabled using a checkbox located at the bottom of the section (which is a bit odd: + // logically we'd have this checkbox at the top of the section, but we don't want this feature to steal that space) + if (disable_all) + ImGui::EndDisabled(); + + if (ImGui::TreeNode("Disable block")) + { + ImGui::Checkbox("Disable entire section above", &disable_all); + ImGui::SameLine(); HelpMarker("Demonstrate using BeginDisabled()/EndDisabled() across this section."); + ImGui::TreePop(); + } } static void ShowDemoWindowLayout() @@ -2389,10 +2421,10 @@ static void ShowDemoWindowLayout() // You can also call SetNextWindowPos() to position the child window. The parent window will effectively // layout from this position. // - Using ImGui::GetItemRectMin/Max() to query the "item" state (because the child window is an item from - // the POV of the parent window). See 'Demo->Querying Status (Active/Focused/Hovered etc.)' for details. + // the POV of the parent window). See 'Demo->Querying Status (Edited/Active/Hovered etc.)' for details. { static int offset_x = 0; - ImGui::SetNextItemWidth(100); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::DragInt("Offset X", &offset_x, 1.0f, -1000, 1000); ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (float)offset_x); @@ -2414,15 +2446,15 @@ static void ShowDemoWindowLayout() if (ImGui::TreeNode("Widgets Width")) { + static float f = 0.0f; + static bool show_indented_items = true; + ImGui::Checkbox("Show indented items", &show_indented_items); + // Use SetNextItemWidth() to set the width of a single upcoming item. // Use PushItemWidth()/PopItemWidth() to set the width of a group of items. // In real code use you'll probably want to choose width values that are proportional to your font size // e.g. Using '20.0f * GetFontSize()' as width instead of '200.0f', etc. - static float f = 0.0f; - static bool show_indented_items = true; - ImGui::Checkbox("Show indented items", &show_indented_items); - ImGui::Text("SetNextItemWidth/PushItemWidth(100)"); ImGui::SameLine(); HelpMarker("Fixed width."); ImGui::PushItemWidth(100); @@ -2838,6 +2870,8 @@ static void ShowDemoWindowLayout() { for (int item = 0; item < 100; item++) { + if (item > 0) + ImGui::SameLine(); if (enable_track && item == track_item) { ImGui::TextColored(ImVec4(1, 1, 0, 1), "Item %d", item); @@ -2847,7 +2881,6 @@ static void ShowDemoWindowLayout() { ImGui::Text("Item %d", item); } - ImGui::SameLine(); } } float scroll_x = ImGui::GetScrollX(); @@ -3191,46 +3224,84 @@ static void ShowDemoWindowPopups() if (ImGui::TreeNode("Context menus")) { + HelpMarker("\"Context\" functions are simple helpers to associate a Popup to a given Item or Window identifier."); + // BeginPopupContextItem() is a helper to provide common/simple popup behavior of essentially doing: - // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) - // OpenPopup(id); - // return BeginPopup(id); - // For more advanced uses you may want to replicate and customize this code. - // See details in BeginPopupContextItem(). - static float value = 0.5f; - ImGui::Text("Value = %.3f (<-- right-click here)", value); - if (ImGui::BeginPopupContextItem("item context menu")) - { - if (ImGui::Selectable("Set to zero")) value = 0.0f; - if (ImGui::Selectable("Set to PI")) value = 3.1415f; - ImGui::SetNextItemWidth(-FLT_MIN); - ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); - ImGui::EndPopup(); + // if (id == 0) + // id = GetItemID(); // Use last item id + // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) + // OpenPopup(id); + // return BeginPopup(id); + // For advanced advanced uses you may want to replicate and customize this code. + // See more details in BeginPopupContextItem(). + + // Example 1 + // When used after an item that has an ID (e.g. Button), we can skip providing an ID to BeginPopupContextItem(), + // and BeginPopupContextItem() will use the last item ID as the popup ID. + { + const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" }; + for (int n = 0; n < 5; n++) + { + ImGui::Selectable(names[n]); + if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id + { + ImGui::Text("This a popup for \"%s\"!", names[n]); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("Right-click to open popup"); + } } - // We can also use OpenPopupOnItemClick() which is the same as BeginPopupContextItem() but without the - // Begin() call. So here we will make it that clicking on the text field with the right mouse button (1) - // will toggle the visibility of the popup above. - ImGui::Text("(You can also right-click me to open the same popup as above.)"); - ImGui::OpenPopupOnItemClick("item context menu", 1); - - // When used after an item that has an ID (e.g.Button), we can skip providing an ID to BeginPopupContextItem(). - // BeginPopupContextItem() will use the last item ID as the popup ID. - // In addition here, we want to include your editable label inside the button label. - // We use the ### operator to override the ID (read FAQ about ID for details) - static char name[32] = "Label1"; - char buf[64]; - sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label - ImGui::Button(buf); - if (ImGui::BeginPopupContextItem()) + // Example 2 + // Popup on a Text() element which doesn't have an identifier: we need to provide an identifier to BeginPopupContextItem(). + // Using an explicit identifier is also convenient if you want to activate the popups from different locations. { - ImGui::Text("Edit name:"); - ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); - if (ImGui::Button("Close")) - ImGui::CloseCurrentPopup(); - ImGui::EndPopup(); + HelpMarker("Text() elements don't have stable identifiers so we need to provide one."); + static float value = 0.5f; + ImGui::Text("Value = %.3f <-- (1) right-click this value", value); + if (ImGui::BeginPopupContextItem("my popup")) + { + if (ImGui::Selectable("Set to zero")) value = 0.0f; + if (ImGui::Selectable("Set to PI")) value = 3.1415f; + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::DragFloat("##Value", &value, 0.1f, 0.0f, 0.0f); + ImGui::EndPopup(); + } + + // We can also use OpenPopupOnItemClick() to toggle the visibility of a given popup. + // Here we make it that right-clicking this other text element opens the same popup as above. + // The popup itself will be submitted by the code above. + ImGui::Text("(2) Or right-click this text"); + ImGui::OpenPopupOnItemClick("my popup", ImGuiPopupFlags_MouseButtonRight); + + // Back to square one: manually open the same popup. + if (ImGui::Button("(3) Or click this button")) + ImGui::OpenPopup("my popup"); + } + + // Example 3 + // When using BeginPopupContextItem() with an implicit identifier (NULL == use last item ID), + // we need to make sure your item identifier is stable. + // In this example we showcase altering the item label while preserving its identifier, using the ### operator (see FAQ). + { + HelpMarker("Showcase using a popup ID linked to item ID, with the item having a changing label + stable ID using the ### operator."); + static char name[32] = "Label1"; + char buf[64]; + sprintf(buf, "Button: %s###Button", name); // ### operator override ID ignoring the preceding label + ImGui::Button(buf); + if (ImGui::BeginPopupContextItem()) + { + ImGui::Text("Edit name:"); + ImGui::InputText("##edit", name, IM_ARRAYSIZE(name)); + if (ImGui::Button("Close")) + ImGui::CloseCurrentPopup(); + ImGui::EndPopup(); + } + ImGui::SameLine(); ImGui::Text("(<-- right-click here)"); } - ImGui::SameLine(); ImGui::Text("(<-- right-click here)"); ImGui::TreePop(); } @@ -3456,6 +3527,7 @@ static void EditTableSizingFlags(ImGuiTableFlags* p_flags) static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags) { + ImGui::CheckboxFlags("_Disabled", p_flags, ImGuiTableColumnFlags_Disabled); ImGui::SameLine(); HelpMarker("Master disable flag (also hide from context menu)"); ImGui::CheckboxFlags("_DefaultHide", p_flags, ImGuiTableColumnFlags_DefaultHide); ImGui::CheckboxFlags("_DefaultSort", p_flags, ImGuiTableColumnFlags_DefaultSort); if (ImGui::CheckboxFlags("_WidthStretch", p_flags, ImGuiTableColumnFlags_WidthStretch)) @@ -3469,6 +3541,7 @@ static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags) ImGui::CheckboxFlags("_NoSort", p_flags, ImGuiTableColumnFlags_NoSort); ImGui::CheckboxFlags("_NoSortAscending", p_flags, ImGuiTableColumnFlags_NoSortAscending); ImGui::CheckboxFlags("_NoSortDescending", p_flags, ImGuiTableColumnFlags_NoSortDescending); + ImGui::CheckboxFlags("_NoHeaderLabel", p_flags, ImGuiTableColumnFlags_NoHeaderLabel); ImGui::CheckboxFlags("_NoHeaderWidth", p_flags, ImGuiTableColumnFlags_NoHeaderWidth); ImGui::CheckboxFlags("_PreferSortAscending", p_flags, ImGuiTableColumnFlags_PreferSortAscending); ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending); @@ -5461,7 +5534,7 @@ static void ShowDemoWindowMisc() ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); ImGui::PushAllowKeyboardFocus(false); ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf)); - //ImGui::SameLine(); HelpMarker("Use ImGui::PushAllowKeyboardFocus(bool) to disable tabbing through certain widgets."); + ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); ImGui::PopAllowKeyboardFocus(); ImGui::InputText("5", buf, IM_ARRAYSIZE(buf)); ImGui::TreePop(); @@ -5487,6 +5560,7 @@ static void ShowDemoWindowMisc() if (focus_3) ImGui::SetKeyboardFocusHere(); ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf)); if (ImGui::IsItemActive()) has_focus = 3; + ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); ImGui::PopAllowKeyboardFocus(); if (has_focus) @@ -5704,29 +5778,13 @@ void ImGui::ShowAboutWindow(bool* p_open) //----------------------------------------------------------------------------- // [SECTION] Style Editor / ShowStyleEditor() //----------------------------------------------------------------------------- -// - ShowStyleSelector() // - ShowFontSelector() +// - ShowStyleSelector() // - ShowStyleEditor() //----------------------------------------------------------------------------- -// Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options. -// Here we use the simplified Combo() api that packs items into a single literal string. -// Useful for quick combo boxes where the choices are known locally. -bool ImGui::ShowStyleSelector(const char* label) -{ - static int style_idx = -1; - if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0")) - { - switch (style_idx) - { - case 0: ImGui::StyleColorsDark(); break; - case 1: ImGui::StyleColorsLight(); break; - case 2: ImGui::StyleColorsClassic(); break; - } - return true; - } - return false; -} +// Forward declare ShowFontAtlas() which isn't worth putting in public API yet +namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); } // Demo helper function to select among loaded fonts. // Here we use the regular BeginCombo()/EndCombo() api which is more the more flexible one. @@ -5754,92 +5812,23 @@ void ImGui::ShowFontSelector(const char* label) "- If you need to add/remove fonts at runtime (e.g. for DPI change), do it before calling NewFrame()."); } -// [Internal] Display details for a single font, called by ShowStyleEditor(). -static void NodeFont(ImFont* font) +// Demo helper function to select among default colors. See ShowStyleEditor() for more advanced options. +// Here we use the simplified Combo() api that packs items into a single literal string. +// Useful for quick combo boxes where the choices are known locally. +bool ImGui::ShowStyleSelector(const char* label) { - ImGuiIO& io = ImGui::GetIO(); - ImGuiStyle& style = ImGui::GetStyle(); - bool font_details_opened = ImGui::TreeNode(font, "Font: \"%s\"\n%.2f px, %d glyphs, %d file(s)", - font->ConfigData ? font->ConfigData[0].Name : "", font->FontSize, font->Glyphs.Size, font->ConfigDataCount); - ImGui::SameLine(); if (ImGui::SmallButton("Set as default")) { io.FontDefault = font; } - if (!font_details_opened) - return; - - ImGui::PushFont(font); - ImGui::Text("The quick brown fox jumps over the lazy dog"); - ImGui::PopFont(); - ImGui::DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); // Scale only this font - ImGui::SameLine(); HelpMarker( - "Note than the default embedded font is NOT meant to be scaled.\n\n" - "Font are currently rendered into bitmaps at a given size at the time of building the atlas. " - "You may oversample them to get some flexibility with scaling. " - "You can also render at multiple sizes and select which one to use at runtime.\n\n" - "(Glimmer of hope: the atlas system will be rewritten in the future to make scaling more flexible.)"); - ImGui::Text("Ascent: %f, Descent: %f, Height: %f", font->Ascent, font->Descent, font->Ascent - font->Descent); - ImGui::Text("Fallback character: '%c' (U+%04X)", font->FallbackChar, font->FallbackChar); - ImGui::Text("Ellipsis character: '%c' (U+%04X)", font->EllipsisChar, font->EllipsisChar); - const int surface_sqrt = (int)sqrtf((float)font->MetricsTotalSurface); - ImGui::Text("Texture Area: about %d px ~%dx%d px", font->MetricsTotalSurface, surface_sqrt, surface_sqrt); - for (int config_i = 0; config_i < font->ConfigDataCount; config_i++) - if (font->ConfigData) - if (const ImFontConfig* cfg = &font->ConfigData[config_i]) - ImGui::BulletText("Input %d: \'%s\', Oversample: (%d,%d), PixelSnapH: %d, Offset: (%.1f,%.1f)", - config_i, cfg->Name, cfg->OversampleH, cfg->OversampleV, cfg->PixelSnapH, cfg->GlyphOffset.x, cfg->GlyphOffset.y); - if (ImGui::TreeNode("Glyphs", "Glyphs (%d)", font->Glyphs.Size)) + static int style_idx = -1; + if (ImGui::Combo(label, &style_idx, "Dark\0Light\0Classic\0")) { - // Display all glyphs of the fonts in separate pages of 256 characters - const ImU32 glyph_col = ImGui::GetColorU32(ImGuiCol_Text); - for (unsigned int base = 0; base <= IM_UNICODE_CODEPOINT_MAX; base += 256) + switch (style_idx) { - // Skip ahead if a large bunch of glyphs are not present in the font (test in chunks of 4k) - // This is only a small optimization to reduce the number of iterations when IM_UNICODE_MAX_CODEPOINT - // is large // (if ImWchar==ImWchar32 we will do at least about 272 queries here) - if (!(base & 4095) && font->IsGlyphRangeUnused(base, base + 4095)) - { - base += 4096 - 256; - continue; - } - - int count = 0; - for (unsigned int n = 0; n < 256; n++) - if (font->FindGlyphNoFallback((ImWchar)(base + n))) - count++; - if (count <= 0) - continue; - if (!ImGui::TreeNode((void*)(intptr_t)base, "U+%04X..U+%04X (%d %s)", base, base + 255, count, count > 1 ? "glyphs" : "glyph")) - continue; - float cell_size = font->FontSize * 1; - float cell_spacing = style.ItemSpacing.y; - ImVec2 base_pos = ImGui::GetCursorScreenPos(); - ImDrawList* draw_list = ImGui::GetWindowDrawList(); - for (unsigned int n = 0; n < 256; n++) - { - // We use ImFont::RenderChar as a shortcut because we don't have UTF-8 conversion functions - // available here and thus cannot easily generate a zero-terminated UTF-8 encoded string. - ImVec2 cell_p1(base_pos.x + (n % 16) * (cell_size + cell_spacing), base_pos.y + (n / 16) * (cell_size + cell_spacing)); - ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); - const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); - draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); - if (glyph) - font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); - if (glyph && ImGui::IsMouseHoveringRect(cell_p1, cell_p2)) - { - ImGui::BeginTooltip(); - ImGui::Text("Codepoint: U+%04X", base + n); - ImGui::Separator(); - ImGui::Text("Visible: %d", glyph->Visible); - ImGui::Text("AdvanceX: %.1f", glyph->AdvanceX); - ImGui::Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); - ImGui::Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); - ImGui::EndTooltip(); - } - } - ImGui::Dummy(ImVec2((cell_size + cell_spacing) * 16, (cell_size + cell_spacing) * 16)); - ImGui::TreePop(); + case 0: ImGui::StyleColorsDark(); break; + case 1: ImGui::StyleColorsLight(); break; + case 2: ImGui::StyleColorsClassic(); break; } - ImGui::TreePop(); + return true; } - ImGui::TreePop(); + return false; } void ImGui::ShowStyleEditor(ImGuiStyle* ref) @@ -5998,21 +5987,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGuiIO& io = ImGui::GetIO(); ImFontAtlas* atlas = io.Fonts; HelpMarker("Read FAQ and docs/FONTS.md for details on font loading."); - ImGui::PushItemWidth(120); - for (int i = 0; i < atlas->Fonts.Size; i++) - { - ImFont* font = atlas->Fonts[i]; - ImGui::PushID(font); - NodeFont(font); - ImGui::PopID(); - } - if (ImGui::TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) - { - ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); - ImGui::Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0, 0), ImVec2(1, 1), tint_col, border_col); - ImGui::TreePop(); - } + ImGui::ShowFontAtlas(atlas); // Post-baking font scaling. Note that this is NOT the nice way of scaling fonts, read below. // (we enforce hard clamping manually as by default DragFloat/SliderFloat allows CTRL+Click text to get out of bounds). @@ -6024,6 +5999,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "rebuild the font atlas, and call style.ScaleAllSizes() on a reference ImGuiStyle structure.\n" "Using those settings here will give you poor quality results."); static float window_scale = 1.0f; + ImGui::PushItemWidth(ImGui::GetFontSize() * 8); if (ImGui::DragFloat("window scale", &window_scale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp)) // Scale only this window ImGui::SetWindowFontScale(window_scale); ImGui::DragFloat("global scale", &io.FontGlobalScale, 0.005f, MIN_SCALE, MAX_SCALE, "%.2f", ImGuiSliderFlags_AlwaysClamp); // Scale everything @@ -6043,7 +6019,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) HelpMarker("Faster lines using texture data. Require backend to render with bilinear filtering (not point/nearest filtering)."); ImGui::Checkbox("Anti-aliased fill", &style.AntiAliasedFill); - ImGui::PushItemWidth(100); + ImGui::PushItemWidth(ImGui::GetFontSize() * 8); ImGui::DragFloat("Curve Tessellation Tolerance", &style.CurveTessellationTol, 0.02f, 0.10f, 10.0f, "%.2f"); if (style.CurveTessellationTol < 0.10f) style.CurveTessellationTol = 0.10f; @@ -6090,6 +6066,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) HelpMarker("When drawing circle primitives with \"num_segments == 0\" tesselation will be calculated automatically."); ImGui::DragFloat("Global Alpha", &style.Alpha, 0.005f, 0.20f, 1.0f, "%.2f"); // Not exposing zero here so user doesn't "lose" the UI (zero alpha clips all widgets). But application code could have a toggle to switch between zero and non-zero. + ImGui::DragFloat("Disabled Alpha", &style.DisabledAlpha, 0.005f, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Additional alpha multiplier for disabled items (multiply over current value of Alpha)."); ImGui::PopItemWidth(); ImGui::EndTabItem(); @@ -6755,6 +6732,7 @@ static void ShowExampleAppLayout(bool* p_open) ImGui::BeginChild("left pane", ImVec2(150, 0), true); for (int i = 0; i < 100; i++) { + // FIXME: Good candidate to use ImGuiSelectableFlags_SelectOnNav char label[128]; sprintf(label, "MyObject %d", i); if (ImGui::Selectable(label, selected == i)) @@ -7023,12 +7001,12 @@ static void ShowExampleAppConstrainedResize(bool* p_open) // + a context-menu to choose which corner of the screen to use. static void ShowExampleAppSimpleOverlay(bool* p_open) { - const float PAD = 10.0f; static int corner = 0; ImGuiIO& io = ImGui::GetIO(); ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; if (corner != -1) { + const float PAD = 10.0f; const ImGuiViewport* viewport = ImGui::GetMainViewport(); ImVec2 work_pos = viewport->WorkPos; // Use work area to avoid menu-bar/task-bar, if any! ImVec2 work_size = viewport->WorkSize; @@ -7528,6 +7506,16 @@ void ShowExampleAppDocuments(bool* p_open) ImGui::Separator(); + // About the ImGuiWindowFlags_UnsavedDocument / ImGuiTabItemFlags_UnsavedDocument flags. + // They have multiple effects: + // - Display a dot next to the title. + // - Tab is selected when clicking the X close button. + // - Closure is not assumed (will wait for user to stop submitting the tab). + // Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. + // We need to assume closure by default otherwise waiting for "lack of submission" on the next frame would leave an empty + // hole for one-frame, both in the tab-bar and in tab-contents when closing a tab/window. + // The rarely used SetTabItemClosed() function is a way to notify of programmatic closure to avoid the one-frame hole. + // Submit Tab Bar and Tabs { ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0); diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index c41a1ba0..e1d7b7ee 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (drawing and font code) /* @@ -54,9 +54,12 @@ Index of this file: // Visual Studio warnings #ifdef _MSC_VER -#pragma warning (disable: 4127) // condition expression is constant -#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) -#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 4127) // condition expression is constant +#pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) +#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen +#pragma warning (disable: 6255) // [Static Analyzer] _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead. +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer) #endif // Clang/GCC warnings with -Weverything @@ -105,6 +108,9 @@ namespace IMGUI_STB_NAMESPACE #ifdef _MSC_VER #pragma warning (push) #pragma warning (disable: 4456) // declaration of 'xx' hides previous local declaration +#pragma warning (disable: 6011) // (stb_rectpack) Dereferencing NULL pointer 'cur->next'. +#pragma warning (disable: 6385) // (stb_truetype) Reading invalid data from 'buffer': the readable size is '_Old_3`kernel_width' bytes, but '3' bytes may be read. +#pragma warning (disable: 28182) // (stb_rectpack) Dereferencing NULL pointer. 'cur' contains the same NULL value as 'cur->next' did. #endif #if defined(__clang__) @@ -145,7 +151,7 @@ namespace IMGUI_STB_NAMESPACE #define STBTT_sqrt(x) ImSqrt(x) #define STBTT_pow(x,y) ImPow(x,y) #define STBTT_fabs(x) ImFabs(x) -#define STBTT_ifloor(x) ((int)ImFloorStd(x)) +#define STBTT_ifloor(x) ((int)ImFloorSigned(x)) #define STBTT_iceil(x) ((int)ImCeil(x)) #define STBTT_STATIC #define STB_TRUETYPE_IMPLEMENTATION @@ -485,6 +491,18 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) #define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset #define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset +// Try to merge two last draw commands +void ImDrawList::_TryMergeDrawCmds() +{ + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + ImDrawCmd* prev_cmd = curr_cmd - 1; + if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) + { + prev_cmd->ElemCount += curr_cmd->ElemCount; + CmdBuffer.pop_back(); + } +} + // Our scheme may appears a bit unusual, basically we want the most-common calls AddLine AddRect etc. to not have to perform any check so we always have a command ready in the stack. // The cost of figuring out if a new command has to be added or if we can merge is paid in those Update** functions only. void ImDrawList::_OnChangedClipRect() @@ -687,9 +705,11 @@ void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, c } // On AddPolyline() and AddConvexPolyFilled() we intentionally avoid using ImVec2 and superfluous function calls to optimize debug/non-inlined builds. -// Those macros expects l-values. -#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = 1.0f / ImSqrt(d2); VX *= inv_len; VY *= inv_len; } } while (0) -#define IM_FIXNORMAL2F(VX,VY) do { float d2 = VX*VX + VY*VY; if (d2 < 0.5f) d2 = 0.5f; float inv_lensq = 1.0f / d2; VX *= inv_lensq; VY *= inv_lensq; } while (0) +// - Those macros expects l-values and need to be used as their own statement. +// - Those macros are intentionally not surrounded by the 'do {} while (0)' idiom because even that translates to runtime with debug compilers. +#define IM_NORMALIZE2F_OVER_ZERO(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.0f) { float inv_len = ImRsqrt(d2); VX *= inv_len; VY *= inv_len; } } (void)0 +#define IM_FIXNORMAL2F_MAX_INVLEN2 100.0f // 500.0f (see #4053, #3366) +#define IM_FIXNORMAL2F(VX,VY) { float d2 = VX*VX + VY*VY; if (d2 > 0.000001f) { float inv_len2 = 1.0f / d2; if (inv_len2 > IM_FIXNORMAL2F_MAX_INVLEN2) inv_len2 = IM_FIXNORMAL2F_MAX_INVLEN2; VX *= inv_len2; VY *= inv_len2; } } (void)0 // TODO: Thickness anti-aliased lines cap are missing their AA fringe. // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. @@ -1037,7 +1057,6 @@ void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_ _Path.push_back(center); return; } - IM_ASSERT(a_min_sample <= a_max_sample); // Calculate arc auto segment step size if (a_step <= 0) @@ -1046,17 +1065,7 @@ void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_ // Make sure we never do steps larger than one quarter of the circle a_step = ImClamp(a_step, 1, IM_DRAWLIST_ARCFAST_TABLE_SIZE / 4); - // Normalize a_min_sample to always start lie in [0..IM_DRAWLIST_ARCFAST_SAMPLE_MAX] range. - if (a_min_sample < 0) - { - int normalized_sample = a_min_sample % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; - if (normalized_sample < 0) - normalized_sample += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; - a_max_sample += (normalized_sample - a_min_sample); - a_min_sample = normalized_sample; - } - - const int sample_range = a_max_sample - a_min_sample; + const int sample_range = ImAbs(a_max_sample - a_min_sample); const int a_next_step = a_step; int samples = sample_range + 1; @@ -1082,16 +1091,40 @@ void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_ ImVec2* out_ptr = _Path.Data + (_Path.Size - samples); int sample_index = a_min_sample; - for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step) + if (sample_index < 0 || sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) { - // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more - if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) - sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + sample_index = sample_index % IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + if (sample_index < 0) + sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + } - const ImVec2 s = _Data->ArcFastVtx[sample_index]; - out_ptr->x = center.x + s.x * radius; - out_ptr->y = center.y + s.y * radius; - out_ptr++; + if (a_max_sample >= a_min_sample) + { + for (int a = a_min_sample; a <= a_max_sample; a += a_step, sample_index += a_step, a_step = a_next_step) + { + // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more + if (sample_index >= IM_DRAWLIST_ARCFAST_SAMPLE_MAX) + sample_index -= IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + + const ImVec2 s = _Data->ArcFastVtx[sample_index]; + out_ptr->x = center.x + s.x * radius; + out_ptr->y = center.y + s.y * radius; + out_ptr++; + } + } + else + { + for (int a = a_min_sample; a >= a_max_sample; a -= a_step, sample_index -= a_step, a_step = a_next_step) + { + // a_step is clamped to IM_DRAWLIST_ARCFAST_SAMPLE_MAX, so we have guaranteed that it will not wrap over range twice or more + if (sample_index < 0) + sample_index += IM_DRAWLIST_ARCFAST_SAMPLE_MAX; + + const ImVec2 s = _Data->ArcFastVtx[sample_index]; + out_ptr->x = center.x + s.x * radius; + out_ptr->y = center.y + s.y * radius; + out_ptr++; + } } if (extra_max_sample) @@ -1116,7 +1149,6 @@ void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, fl _Path.push_back(center); return; } - IM_ASSERT(a_min <= a_max); // Note that we are adding a point at both a_min and a_max. // If you are trying to draw a full closed circle you don't want the overlapping points! @@ -1136,7 +1168,6 @@ void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_ _Path.push_back(center); return; } - IM_ASSERT(a_min_of_12 <= a_max_of_12); _PathArcToFastEx(center, radius, a_min_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, a_max_of_12 * IM_DRAWLIST_ARCFAST_SAMPLE_MAX / 12, 0); } @@ -1147,7 +1178,6 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa _Path.push_back(center); return; } - IM_ASSERT(a_min <= a_max); if (num_segments > 0) { @@ -1158,28 +1188,33 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa // Automatic segment count if (radius <= _Data->ArcFastRadiusCutoff) { + const bool a_is_reverse = a_max < a_min; + // We are going to use precomputed values for mid samples. // Determine first and last sample in lookup table that belong to the arc. - const int a_min_sample = (int)ImCeil(IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f)); - const int a_max_sample = (int)( IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f)); - const int a_mid_samples = ImMax(a_max_sample - a_min_sample, 0); + const float a_min_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f); + const float a_max_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f); + + const int a_min_sample = a_is_reverse ? (int)ImFloorSigned(a_min_sample_f) : (int)ImCeil(a_min_sample_f); + const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloorSigned(a_max_sample_f); + const int a_mid_samples = a_is_reverse ? ImMax(a_min_sample - a_max_sample, 0) : ImMax(a_max_sample - a_min_sample, 0); const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; - const bool a_emit_start = (a_min_segment_angle - a_min) > 0.0f; - const bool a_emit_end = (a_max - a_max_segment_angle) > 0.0f; + const bool a_emit_start = (a_min_segment_angle - a_min) != 0.0f; + const bool a_emit_end = (a_max - a_max_segment_angle) != 0.0f; _Path.reserve(_Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0))); if (a_emit_start) _Path.push_back(ImVec2(center.x + ImCos(a_min) * radius, center.y + ImSin(a_min) * radius)); - if (a_max_sample >= a_min_sample) + if (a_mid_samples > 0) _PathArcToFastEx(center, radius, a_min_sample, a_max_sample, 0); if (a_emit_end) _Path.push_back(ImVec2(center.x + ImCos(a_max) * radius, center.y + ImSin(a_max) * radius)); } else { - const float arc_length = a_max - a_min; + const float arc_length = ImAbs(a_max - a_min); const int circle_segment_count = _CalcCircleAutoSegmentCount(radius); const int arc_segment_count = ImMax((int)ImCeil(circle_segment_count * arc_length / (IM_PI * 2.0f)), (int)(2.0f * IM_PI / arc_length)); _PathArcToN(center, radius, a_min, a_max, arc_segment_count); @@ -1444,24 +1479,22 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) return; - // Obtain segment count if (num_segments <= 0) { - // Automatic segment count - num_segments = _CalcCircleAutoSegmentCount(radius); + // Use arc with automatic segment count + _PathArcToFastEx(center, radius - 0.5f, 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, 0); + _Path.Size--; } else { // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); - } - // Because we are filling a closed shape we remove 1 from the count of segments/points - const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; - if (num_segments == 12) - PathArcToFast(center, radius - 0.5f, 0, 12 - 1); - else + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; PathArcTo(center, radius - 0.5f, 0.0f, a_max, num_segments - 1); + } + PathStroke(col, ImDrawFlags_Closed, thickness); } @@ -1470,24 +1503,22 @@ void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) return; - // Obtain segment count if (num_segments <= 0) { - // Automatic segment count - num_segments = _CalcCircleAutoSegmentCount(radius); + // Use arc with automatic segment count + _PathArcToFastEx(center, radius, 0, IM_DRAWLIST_ARCFAST_SAMPLE_MAX, 0); + _Path.Size--; } else { // Explicit segment count (still clamp to avoid drawing insanely tessellated shapes) num_segments = ImClamp(num_segments, 3, IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX); - } - // Because we are filling a closed shape we remove 1 from the count of segments/points - const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; - if (num_segments == 12) - PathArcToFast(center, radius, 0, 12 - 1); - else + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = (IM_PI * 2.0f) * ((float)num_segments - 1.0f) / (float)num_segments; PathArcTo(center, radius, 0.0f, a_max, num_segments - 1); + } + PathFillConvex(col); } @@ -1967,6 +1998,7 @@ void ImFontAtlas::ClearInputData() ConfigData.clear(); CustomRects.clear(); PackIdMouseCursors = PackIdLines = -1; + TexReady = false; } void ImFontAtlas::ClearTexData() @@ -1979,14 +2011,14 @@ void ImFontAtlas::ClearTexData() TexPixelsAlpha8 = NULL; TexPixelsRGBA32 = NULL; TexPixelsUseColors = false; + // Important: we leave TexReady untouched } void ImFontAtlas::ClearFonts() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); - for (int i = 0; i < Fonts.Size; i++) - IM_DELETE(Fonts[i]); - Fonts.clear(); + Fonts.clear_delete(); + TexReady = false; } void ImFontAtlas::Clear() @@ -2000,11 +2032,7 @@ void ImFontAtlas::GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_wid { // Build atlas on demand if (TexPixelsAlpha8 == NULL) - { - if (ConfigData.empty()) - AddFontDefault(); Build(); - } *out_pixels = TexPixelsAlpha8; if (out_width) *out_width = TexWidth; @@ -2063,6 +2091,7 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar; // Invalidate texture + TexReady = false; ClearTexData(); return new_font_cfg.DstFont; } @@ -2134,7 +2163,7 @@ ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float si IM_ASSERT(font_cfg.FontData == NULL); font_cfg.FontData = ttf_data; font_cfg.FontDataSize = ttf_size; - font_cfg.SizePixels = size_pixels; + font_cfg.SizePixels = size_pixels > 0.0f ? size_pixels : font_cfg.SizePixels; if (glyph_ranges) font_cfg.GlyphRanges = glyph_ranges; return AddFont(&font_cfg); @@ -2225,6 +2254,10 @@ bool ImFontAtlas::Build() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); + // Default font is none are specified + if (ConfigData.Size == 0) + AddFontDefault(); + // Select builder // - Note that we do not reassign to atlas->FontBuilderIO, since it is likely to point to static data which // may mess with some hot-reloading schemes. If you need to assign to this (for dynamic selection) AND are @@ -2546,9 +2579,8 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) } } - // Cleanup temporary (ImVector doesn't honor destructor) - for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) - src_tmp_array[src_i].~ImFontBuildSrcData(); + // Cleanup + src_tmp_array.clear_destruct(); ImFontAtlasBuildFinish(atlas); return true; @@ -2764,22 +2796,7 @@ void ImFontAtlasBuildFinish(ImFontAtlas* atlas) if (atlas->Fonts[i]->DirtyLookupTables) atlas->Fonts[i]->BuildLookupTable(); - // Ellipsis character is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). - // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character. - // FIXME: Also note that 0x2026 is currently seldom included in our font ranges. Because of this we are more likely to use three individual dots. - for (int i = 0; i < atlas->Fonts.size(); i++) - { - ImFont* font = atlas->Fonts[i]; - if (font->EllipsisChar != (ImWchar)-1) - continue; - const ImWchar ellipsis_variants[] = { (ImWchar)0x2026, (ImWchar)0x0085 }; - for (int j = 0; j < IM_ARRAYSIZE(ellipsis_variants); j++) - if (font->FindGlyphNoFallback(ellipsis_variants[j]) != NULL) // Verify glyph exists - { - font->EllipsisChar = ellipsis_variants[j]; - break; - } - } + atlas->TexReady = true; } // Retrieve list of range (2 int per range, values are inclusive) @@ -2800,6 +2817,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesKorean() 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x3131, 0x3163, // Korean alphabets 0xAC00, 0xD7A3, // Korean characters + 0xFFFD, 0xFFFD, // Invalid 0, }; return &ranges[0]; @@ -2814,6 +2832,7 @@ const ImWchar* ImFontAtlas::GetGlyphRangesChineseFull() 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions 0xFF00, 0xFFEF, // Half-width characters + 0xFFFD, 0xFFFD, // Invalid 0x4e00, 0x9FAF, // CJK Ideograms 0, }; @@ -2890,7 +2909,8 @@ const ImWchar* ImFontAtlas::GetGlyphRangesChineseSimplifiedCommon() 0x2000, 0x206F, // General Punctuation 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions - 0xFF00, 0xFFEF // Half-width characters + 0xFF00, 0xFFEF, // Half-width characters + 0xFFFD, 0xFFFD // Invalid }; static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00) * 2 + 1] = { 0 }; if (!full_ranges[0]) @@ -2979,7 +2999,8 @@ const ImWchar* ImFontAtlas::GetGlyphRangesJapanese() 0x0020, 0x00FF, // Basic Latin + Latin Supplement 0x3000, 0x30FF, // CJK Symbols and Punctuations, Hiragana, Katakana 0x31F0, 0x31FF, // Katakana Phonetic Extensions - 0xFF00, 0xFFEF // Half-width characters + 0xFF00, 0xFFEF, // Half-width characters + 0xFFFD, 0xFFFD // Invalid }; static ImWchar full_ranges[IM_ARRAYSIZE(base_ranges) + IM_ARRAYSIZE(accumulative_offsets_from_0x4E00)*2 + 1] = { 0 }; if (!full_ranges[0]) @@ -3078,8 +3099,9 @@ ImFont::ImFont() { FontSize = 0.0f; FallbackAdvanceX = 0.0f; - FallbackChar = (ImWchar)'?'; + FallbackChar = (ImWchar)-1; EllipsisChar = (ImWchar)-1; + DotChar = (ImWchar)-1; FallbackGlyph = NULL; ContainerAtlas = NULL; ConfigData = NULL; @@ -3110,6 +3132,14 @@ void ImFont::ClearOutputData() MetricsTotalSurface = 0; } +static ImWchar FindFirstExistingGlyph(ImFont* font, const ImWchar* candidate_chars, int candidate_chars_count) +{ + for (int n = 0; n < candidate_chars_count; n++) + if (font->FindGlyphNoFallback(candidate_chars[n]) != NULL) + return candidate_chars[n]; + return (ImWchar)-1; +} + void ImFont::BuildLookupTable() { int max_codepoint = 0; @@ -3152,9 +3182,31 @@ void ImFont::BuildLookupTable() SetGlyphVisible((ImWchar)' ', false); SetGlyphVisible((ImWchar)'\t', false); - // Setup fall-backs + // Ellipsis character is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). + // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character. + // FIXME: Note that 0x2026 is rarely included in our font ranges. Because of this we are more likely to use three individual dots. + const ImWchar ellipsis_chars[] = { (ImWchar)0x2026, (ImWchar)0x0085 }; + const ImWchar dots_chars[] = { (ImWchar)'.', (ImWchar)0xFF0E }; + if (EllipsisChar == (ImWchar)-1) + EllipsisChar = FindFirstExistingGlyph(this, ellipsis_chars, IM_ARRAYSIZE(ellipsis_chars)); + if (DotChar == (ImWchar)-1) + DotChar = FindFirstExistingGlyph(this, dots_chars, IM_ARRAYSIZE(dots_chars)); + + // Setup fallback character + const ImWchar fallback_chars[] = { (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' }; FallbackGlyph = FindGlyphNoFallback(FallbackChar); - FallbackAdvanceX = FallbackGlyph ? FallbackGlyph->AdvanceX : 0.0f; + if (FallbackGlyph == NULL) + { + FallbackChar = FindFirstExistingGlyph(this, fallback_chars, IM_ARRAYSIZE(fallback_chars)); + FallbackGlyph = FindGlyphNoFallback(FallbackChar); + if (FallbackGlyph == NULL) + { + FallbackGlyph = &Glyphs.back(); + FallbackChar = (ImWchar)FallbackGlyph->Codepoint; + } + } + + FallbackAdvanceX = FallbackGlyph->AdvanceX; for (int i = 0; i < max_codepoint + 1; i++) if (IndexAdvanceX[i] < 0.0f) IndexAdvanceX[i] = FallbackAdvanceX; @@ -3179,12 +3231,6 @@ void ImFont::SetGlyphVisible(ImWchar c, bool visible) glyph->Visible = visible ? 1 : 0; } -void ImFont::SetFallbackChar(ImWchar c) -{ - FallbackChar = c; - BuildLookupTable(); -} - void ImFont::GrowIndex(int new_size) { IM_ASSERT(IndexAdvanceX.Size == IndexLookup.Size); @@ -3469,6 +3515,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons return text_size; } +// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const { const ImFontGlyph* glyph = FindGlyph(c); @@ -3483,6 +3530,7 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); } +// Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const { if (!text_end) @@ -3684,6 +3732,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col // - RenderMouseCursor() // - RenderArrowPointingAt() // - RenderRectFilledRangeH() +// - RenderRectFilledWithHole() //----------------------------------------------------------------------------- // Function in need of a redesign (legacy mess) // - RenderColorRectWithAlphaCheckerboard() @@ -3752,7 +3801,7 @@ void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, Im if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) { pos -= offset; - const ImTextureID tex_id = font_atlas->TexID; + ImTextureID tex_id = font_atlas->TexID; draw_list->PushTextureID(tex_id); draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index cd0f51db..8cac7241 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -9,12 +9,16 @@ // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). -// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. // Read online: https://github.com/ocornut/imgui/tree/master/docs // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2021-08-17: *BREAKING CHANGE*: Now using glfwSetWindowFocusCallback() to calling io.AddFocusEvent(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() and forward it to the backend via ImGui_ImplGlfw_WindowFocusCallback(). +// 2021-07-29: *BREAKING CHANGE*: Now using glfwSetCursorEnterCallback(). MousePos is correctly reported when the host platform window is hovered but not focused. If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() callback and forward it to the backend via ImGui_ImplGlfw_CursorEnterCallback(). +// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). // 2020-01-17: Inputs: Disable error callback while assigning mouse cursors because some X11 setup don't have them and it generates errors. // 2019-12-05: Inputs: Added support for new mouse cursors added in GLFW 3.4+ (resizing cursors, not allowed cursor). // 2019-10-18: Misc: Previously installed user callbacks are now restored on shutdown. @@ -52,32 +56,55 @@ #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface -#ifdef GLFW_RESIZE_NESW_CURSOR // let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? +#ifdef GLFW_RESIZE_NESW_CURSOR // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? #define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR #else #define GLFW_HAS_NEW_CURSORS (0) #endif -// Data +// GLFW data enum GlfwClientApi { GlfwClientApi_Unknown, GlfwClientApi_OpenGL, GlfwClientApi_Vulkan }; -static GLFWwindow* g_Window = NULL; // Main window -static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown; -static double g_Time = 0.0; -static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {}; -static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = {}; -static bool g_InstalledCallbacks = false; - -// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. -static GLFWmousebuttonfun g_PrevUserCallbackMousebutton = NULL; -static GLFWscrollfun g_PrevUserCallbackScroll = NULL; -static GLFWkeyfun g_PrevUserCallbackKey = NULL; -static GLFWcharfun g_PrevUserCallbackChar = NULL; +struct ImGui_ImplGlfw_Data +{ + GLFWwindow* Window; + GlfwClientApi ClientApi; + double Time; + GLFWwindow* MouseWindow; + bool MouseJustPressed[ImGuiMouseButton_COUNT]; + GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; + bool InstalledCallbacks; + + // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. + GLFWwindowfocusfun PrevUserCallbackWindowFocus; + GLFWcursorenterfun PrevUserCallbackCursorEnter; + GLFWmousebuttonfun PrevUserCallbackMousebutton; + GLFWscrollfun PrevUserCallbackScroll; + GLFWkeyfun PrevUserCallbackKey; + GLFWcharfun PrevUserCallbackChar; + GLFWmonitorfun PrevUserCallbackMonitor; + + ImGui_ImplGlfw_Data() { memset(this, 0, sizeof(*this)); } +}; + +// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts +// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. +// FIXME: multi-context support is not well tested and probably dysfunctional in this backend. +// - Because glfwPollEvents() process all windows and some events may be called outside of it, you will need to register your own callbacks +// (passing install_callbacks=false in ImGui_ImplGlfw_InitXXX functions), set the current dear imgui context and then call our callbacks. +// - Otherwise we may need to store a GLFWWindow* -> ImGuiContext* map and handle this in the backend, adding a little bit of extra complexity to it. +// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. +static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData() +{ + return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : NULL; +} + +// Functions static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data) { return glfwGetClipboardString((GLFWwindow*)user_data); @@ -90,17 +117,19 @@ static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { - if (g_PrevUserCallbackMousebutton != NULL) - g_PrevUserCallbackMousebutton(window, button, action, mods); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackMousebutton != NULL && window == bd->Window) + bd->PrevUserCallbackMousebutton(window, button, action, mods); - if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed)) - g_MouseJustPressed[button] = true; + if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(bd->MouseJustPressed)) + bd->MouseJustPressed[button] = true; } void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) { - if (g_PrevUserCallbackScroll != NULL) - g_PrevUserCallbackScroll(window, xoffset, yoffset); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackScroll != NULL && window == bd->Window) + bd->PrevUserCallbackScroll(window, xoffset, yoffset); ImGuiIO& io = ImGui::GetIO(); io.MouseWheelH += (float)xoffset; @@ -109,14 +138,18 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { - if (g_PrevUserCallbackKey != NULL) - g_PrevUserCallbackKey(window, key, scancode, action, mods); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackKey != NULL && window == bd->Window) + bd->PrevUserCallbackKey(window, key, scancode, action, mods); ImGuiIO& io = ImGui::GetIO(); - if (action == GLFW_PRESS) - io.KeysDown[key] = true; - if (action == GLFW_RELEASE) - io.KeysDown[key] = false; + if (key >= 0 && key < IM_ARRAYSIZE(io.KeysDown)) + { + if (action == GLFW_PRESS) + io.KeysDown[key] = true; + if (action == GLFW_RELEASE) + io.KeysDown[key] = false; + } // Modifiers are not reliable across systems io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL]; @@ -129,25 +162,57 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int a #endif } +void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackWindowFocus != NULL && window == bd->Window) + bd->PrevUserCallbackWindowFocus(window, focused); + + ImGuiIO& io = ImGui::GetIO(); + io.AddFocusEvent(focused != 0); +} + +void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackCursorEnter != NULL && window == bd->Window) + bd->PrevUserCallbackCursorEnter(window, entered); + + if (entered) + bd->MouseWindow = window; + if (!entered && bd->MouseWindow == window) + bd->MouseWindow = NULL; +} + void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) { - if (g_PrevUserCallbackChar != NULL) - g_PrevUserCallbackChar(window, c); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackChar != NULL && window == bd->Window) + bd->PrevUserCallbackChar(window, c); ImGuiIO& io = ImGui::GetIO(); io.AddInputCharacter(c); } +void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int) +{ + // Unused in 'master' branch but 'docking' branch will use this, so we declare it ahead of it so if you have to install callbacks you can install this one too. +} + static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) { - g_Window = window; - g_Time = 0.0; + ImGuiIO& io = ImGui::GetIO(); + IM_ASSERT(io.BackendPlatformUserData == NULL && "Already initialized a platform backend!"); // Setup backend capabilities flags - ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)(); + io.BackendPlatformUserData = (void*)bd; + io.BackendPlatformName = "imgui_impl_glfw"; io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) - io.BackendPlatformName = "imgui_impl_glfw"; + + bd->Window = window; + bd->Time = 0.0; // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; @@ -175,9 +240,9 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; - io.ClipboardUserData = g_Window; + io.ClipboardUserData = bd->Window; #if defined(_WIN32) - io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window); + io.ImeWindowHandle = (void*)glfwGetWin32Window(bd->Window); #endif // Create mouse cursors @@ -185,39 +250,44 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting. // Missing cursors will return NULL and our _UpdateMouseCursor() function will use the Arrow cursor instead.) GLFWerrorfun prev_error_callback = glfwSetErrorCallback(NULL); - g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); - g_MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); #if GLFW_HAS_NEW_CURSORS - g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR); - g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR); #else - g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); + bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); #endif glfwSetErrorCallback(prev_error_callback); // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. - g_PrevUserCallbackMousebutton = NULL; - g_PrevUserCallbackScroll = NULL; - g_PrevUserCallbackKey = NULL; - g_PrevUserCallbackChar = NULL; + bd->PrevUserCallbackWindowFocus = NULL; + bd->PrevUserCallbackMousebutton = NULL; + bd->PrevUserCallbackScroll = NULL; + bd->PrevUserCallbackKey = NULL; + bd->PrevUserCallbackChar = NULL; + bd->PrevUserCallbackMonitor = NULL; if (install_callbacks) { - g_InstalledCallbacks = true; - g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); - g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); - g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); - g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + bd->InstalledCallbacks = true; + bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback); + bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback); + bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); + bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); + bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); + bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback); } - g_ClientApi = client_api; + bd->ClientApi = client_api; return true; } @@ -238,75 +308,83 @@ bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks) void ImGui_ImplGlfw_Shutdown() { - if (g_InstalledCallbacks) + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + + if (bd->InstalledCallbacks) { - glfwSetMouseButtonCallback(g_Window, g_PrevUserCallbackMousebutton); - glfwSetScrollCallback(g_Window, g_PrevUserCallbackScroll); - glfwSetKeyCallback(g_Window, g_PrevUserCallbackKey); - glfwSetCharCallback(g_Window, g_PrevUserCallbackChar); - g_InstalledCallbacks = false; + glfwSetWindowFocusCallback(bd->Window, bd->PrevUserCallbackWindowFocus); + glfwSetCursorEnterCallback(bd->Window, bd->PrevUserCallbackCursorEnter); + glfwSetMouseButtonCallback(bd->Window, bd->PrevUserCallbackMousebutton); + glfwSetScrollCallback(bd->Window, bd->PrevUserCallbackScroll); + glfwSetKeyCallback(bd->Window, bd->PrevUserCallbackKey); + glfwSetCharCallback(bd->Window, bd->PrevUserCallbackChar); + glfwSetMonitorCallback(bd->PrevUserCallbackMonitor); } for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) - { - glfwDestroyCursor(g_MouseCursors[cursor_n]); - g_MouseCursors[cursor_n] = NULL; - } - g_ClientApi = GlfwClientApi_Unknown; + glfwDestroyCursor(bd->MouseCursors[cursor_n]); + + io.BackendPlatformName = NULL; + io.BackendPlatformUserData = NULL; + IM_DELETE(bd); } static void ImGui_ImplGlfw_UpdateMousePosAndButtons() { - // Update buttons + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); ImGuiIO& io = ImGui::GetIO(); + + const ImVec2 mouse_pos_prev = io.MousePos; + io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); + + // Update mouse buttons + // (if a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame) for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) { - // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. - io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0; - g_MouseJustPressed[i] = false; + io.MouseDown[i] = bd->MouseJustPressed[i] || glfwGetMouseButton(bd->Window, i) != 0; + bd->MouseJustPressed[i] = false; } - // Update mouse position - const ImVec2 mouse_pos_backup = io.MousePos; - io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); #ifdef __EMSCRIPTEN__ - const bool focused = true; // Emscripten + const bool focused = true; #else - const bool focused = glfwGetWindowAttrib(g_Window, GLFW_FOCUSED) != 0; + const bool focused = glfwGetWindowAttrib(bd->Window, GLFW_FOCUSED) != 0; #endif - if (focused) + GLFWwindow* mouse_window = (bd->MouseWindow == bd->Window || focused) ? bd->Window : NULL; + + // Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) + if (io.WantSetMousePos && focused) + glfwSetCursorPos(bd->Window, (double)mouse_pos_prev.x, (double)mouse_pos_prev.y); + + // Set Dear ImGui mouse position from OS position + if (mouse_window != NULL) { - if (io.WantSetMousePos) - { - glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y); - } - else - { - double mouse_x, mouse_y; - glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); - io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); - } + double mouse_x, mouse_y; + glfwGetCursorPos(mouse_window, &mouse_x, &mouse_y); + io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); } } static void ImGui_ImplGlfw_UpdateMouseCursor() { ImGuiIO& io = ImGui::GetIO(); - if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) return; ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) { // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor - glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + glfwSetInputMode(bd->Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); } else { // Show OS mouse cursor // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. - glfwSetCursor(g_Window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); - glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + glfwSetCursor(bd->Window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); + glfwSetInputMode(bd->Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } } @@ -350,21 +428,22 @@ static void ImGui_ImplGlfw_UpdateGamepads() void ImGui_ImplGlfw_NewFrame() { ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame()."); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + IM_ASSERT(bd != NULL && "Did you call ImGui_ImplGlfw_InitForXXX()?"); // Setup display size (every frame to accommodate for window resizing) int w, h; int display_w, display_h; - glfwGetWindowSize(g_Window, &w, &h); - glfwGetFramebufferSize(g_Window, &display_w, &display_h); + glfwGetWindowSize(bd->Window, &w, &h); + glfwGetFramebufferSize(bd->Window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); if (w > 0 && h > 0) io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); // Setup time step double current_time = glfwGetTime(); - io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f); - g_Time = current_time; + io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f); + bd->Time = current_time; ImGui_ImplGlfw_UpdateMousePosAndButtons(); ImGui_ImplGlfw_UpdateMouseCursor(); diff --git a/imgui/imgui_impl_glfw.h b/imgui/imgui_impl_glfw.h index 018f1a1e..5e1fb06d 100644 --- a/imgui/imgui_impl_glfw.h +++ b/imgui/imgui_impl_glfw.h @@ -8,7 +8,8 @@ // [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. // [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). -// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. // Read online: https://github.com/ocornut/imgui/tree/master/docs @@ -20,6 +21,7 @@ #include "imgui.h" // IMGUI_IMPL_API struct GLFWwindow; +struct GLFWmonitor; IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); @@ -30,7 +32,10 @@ IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); // GLFW callbacks // - When calling Init with 'install_callbacks=true': GLFW callbacks will be installed for you. They will call user's previously installed callbacks, if any. // - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call those function yourself from your own GLFW callbacks. +IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); +IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); +IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event); diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index b582851e..5637a0fe 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -7,12 +7,21 @@ // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. -// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. // Read online: https://github.com/ocornut/imgui/tree/master/docs // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions. +// 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader. +// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). +// 2021-06-25: OpenGL: Use OES_vertex_array extension on Emscripten + backup/restore current state. +// 2021-06-21: OpenGL: Destroy individual vertex/fragment shader objects right after they are linked into the main shader. +// 2021-05-24: OpenGL: Access GL_CLIP_ORIGIN when "GL_ARB_clip_control" extension is detected, inside of just OpenGL 4.5 version. +// 2021-05-19: OpenGL: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) +// 2021-04-06: OpenGL: Don't try to read GL_CLIP_ORIGIN unless we're OpenGL 4.5 or greater. // 2021-02-18: OpenGL: Change blending equation to preserve alpha in output buffer. // 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. // 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. @@ -89,42 +98,44 @@ // GL includes #if defined(IMGUI_IMPL_OPENGL_ES2) #include +#if defined(__EMSCRIPTEN__) +#ifndef GL_GLEXT_PROTOTYPES +#define GL_GLEXT_PROTOTYPES +#endif +#include +#endif #elif defined(IMGUI_IMPL_OPENGL_ES3) #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) #include // Use GL ES 3 #else #include // Use GL ES 3 #endif -#else -// About Desktop OpenGL function loaders: -// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. -// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). -// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. -#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) -#include // Needs to be initialized with gl3wInit() in user's code -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) -#include // Needs to be initialized with glewInit() in user's code. -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) -#include // Needs to be initialized with gladLoadGL() in user's code. -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) -#include // Needs to be initialized with gladLoadGL(...) or gladLoaderLoadGL() in user's code. -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) -#ifndef GLFW_INCLUDE_NONE -#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. -#endif -#include // Needs to be initialized with glbinding::Binding::initialize() in user's code. -#include -using namespace gl; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) -#ifndef GLFW_INCLUDE_NONE -#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors. -#endif -#include // Needs to be initialized with glbinding::initialize() in user's code. -#include -using namespace gl; -#else -#include IMGUI_IMPL_OPENGL_LOADER_CUSTOM +#elif !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) +// Modern desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. +// Helper libraries are often used for this purpose! Here we are using our own minimal custom loader based on gl3w. +// In the rest of your app/engine, you can use another loader of your choice (gl3w, glew, glad, glbinding, glext, glLoadGen, etc.). +// If you happen to be developing a new feature for this backend (imgui_impl_opengl3.cpp): +// - You may need to regenerate imgui_impl_opengl3_loader.h to add new symbols. See https://github.com/dearimgui/gl3w_stripped +// - You can temporarily use an unstripped version. See https://github.com/dearimgui/gl3w_stripped/releases +// Changes to this backend using new APIs should be accompanied by a regenerated stripped loader version. +#define IMGL3W_IMPL +#include "imgui_impl_opengl3_loader.h" #endif + +// Vertex arrays are not supported on ES2/WebGL1 unless Emscripten which uses an extension +#ifndef IMGUI_IMPL_OPENGL_ES2 +#define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY +#elif defined(__EMSCRIPTEN__) +#define IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY +#define glBindVertexArray glBindVertexArrayOES +#define glGenVertexArrays glGenVertexArraysOES +#define glDeleteVertexArrays glDeleteVertexArraysOES +#define GL_VERTEX_ARRAY_BINDING GL_VERTEX_ARRAY_BINDING_OES +#endif + +// Desktop GL 2.0+ has glPolygonMode() which GL ES and WebGL don't have. +#ifdef GL_POLYGON_MODE +#define IMGUI_IMPL_HAS_POLYGON_MODE #endif // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. @@ -142,18 +153,56 @@ using namespace gl; #define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART #endif +// Desktop GL use extension detection +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS +#endif + // OpenGL Data -static GLuint g_GlVersion = 0; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) -static char g_GlslVersionString[32] = ""; // Specified by user or detected based on compile time GL settings. -static GLuint g_FontTexture = 0; -static GLuint g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; -static GLint g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; // Uniforms location -static GLuint g_AttribLocationVtxPos = 0, g_AttribLocationVtxUV = 0, g_AttribLocationVtxColor = 0; // Vertex attributes location -static unsigned int g_VboHandle = 0, g_ElementsHandle = 0; +struct ImGui_ImplOpenGL3_Data +{ + GLuint GlVersion; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) + char GlslVersionString[32]; // Specified by user or detected based on compile time GL settings. + GLuint FontTexture; + GLuint ShaderHandle; + GLint AttribLocationTex; // Uniforms location + GLint AttribLocationProjMtx; + GLuint AttribLocationVtxPos; // Vertex attributes location + GLuint AttribLocationVtxUV; + GLuint AttribLocationVtxColor; + unsigned int VboHandle, ElementsHandle; + bool HasClipOrigin; + + ImGui_ImplOpenGL3_Data() { memset(this, 0, sizeof(*this)); } +}; + +// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts +// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. +static ImGui_ImplOpenGL3_Data* ImGui_ImplOpenGL3_GetBackendData() +{ + return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL3_Data*)ImGui::GetIO().BackendRendererUserData : NULL; +} // Functions bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { + ImGuiIO& io = ImGui::GetIO(); + IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!"); + + // Initialize our loader +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) + if (imgl3wInit() != 0) + { + fprintf(stderr, "Failed to initialize OpenGL loader!\n"); + return false; + } +#endif + + // Setup backend capabilities flags + ImGui_ImplOpenGL3_Data* bd = IM_NEW(ImGui_ImplOpenGL3_Data)(); + io.BackendRendererUserData = (void*)bd; + io.BackendRendererName = "imgui_impl_opengl3"; + // Query for GL version (e.g. 320 for GL 3.2) #if !defined(IMGUI_IMPL_OPENGL_ES2) GLint major = 0; @@ -166,85 +215,79 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) const char* gl_version = (const char*)glGetString(GL_VERSION); sscanf(gl_version, "%d.%d", &major, &minor); } - g_GlVersion = (GLuint)(major * 100 + minor * 10); + bd->GlVersion = (GLuint)(major * 100 + minor * 10); #else - g_GlVersion = 200; // GLES 2 + bd->GlVersion = 200; // GLES 2 #endif - // Setup backend capabilities flags - ImGuiIO& io = ImGui::GetIO(); - io.BackendRendererName = "imgui_impl_opengl3"; #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET - if (g_GlVersion >= 320) + if (bd->GlVersion >= 320) io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif // Store GLSL version string so we can refer to it later in case we recreate shaders. // Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. -#if defined(IMGUI_IMPL_OPENGL_ES2) if (glsl_version == NULL) + { +#if defined(IMGUI_IMPL_OPENGL_ES2) glsl_version = "#version 100"; #elif defined(IMGUI_IMPL_OPENGL_ES3) - if (glsl_version == NULL) glsl_version = "#version 300 es"; #elif defined(__APPLE__) - if (glsl_version == NULL) glsl_version = "#version 150"; #else - if (glsl_version == NULL) glsl_version = "#version 130"; #endif - IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(g_GlslVersionString)); - strcpy(g_GlslVersionString, glsl_version); - strcat(g_GlslVersionString, "\n"); - - // Debugging construct to make it easily visible in the IDE and debugger which GL loader has been selected. - // The code actually never uses the 'gl_loader' variable! It is only here so you can read it! - // If auto-detection fails or doesn't select the same GL loader file as used by your application, - // you are likely to get a crash below. - // You can explicitly select a loader by using '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. - const char* gl_loader = "Unknown"; - IM_UNUSED(gl_loader); -#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) - gl_loader = "GL3W"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) - gl_loader = "GLEW"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) - gl_loader = "GLAD"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) - gl_loader = "GLAD2"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) - gl_loader = "glbinding2"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) - gl_loader = "glbinding3"; -#elif defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) - gl_loader = "custom"; -#else - gl_loader = "none"; -#endif + } + IM_ASSERT((int)strlen(glsl_version) + 2 < IM_ARRAYSIZE(bd->GlslVersionString)); + strcpy(bd->GlslVersionString, glsl_version); + strcat(bd->GlslVersionString, "\n"); // Make an arbitrary GL call (we don't actually need the result) - // IF YOU GET A CRASH HERE: it probably means that you haven't initialized the OpenGL function loader used by this code. - // Desktop OpenGL 3/4 need a function loader. See the IMGUI_IMPL_OPENGL_LOADER_xxx explanation above. + // IF YOU GET A CRASH HERE: it probably means the OpenGL function loader didn't do its job. Let us know! GLint current_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); + // Detect extensions we support + bd->HasClipOrigin = (bd->GlVersion >= 450); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS + GLint num_extensions = 0; + glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions); + for (GLint i = 0; i < num_extensions; i++) + { + const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i); + if (extension != NULL && strcmp(extension, "GL_ARB_clip_control") == 0) + bd->HasClipOrigin = true; + } +#endif + return true; } void ImGui_ImplOpenGL3_Shutdown() { + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + ImGui_ImplOpenGL3_DestroyDeviceObjects(); + io.BackendRendererName = NULL; + io.BackendRendererUserData = NULL; + IM_DELETE(bd); } void ImGui_ImplOpenGL3_NewFrame() { - if (!g_ShaderHandle) + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + IM_ASSERT(bd != NULL && "Did you call ImGui_ImplOpenGL3_Init()?"); + + if (!bd->ShaderHandle) ImGui_ImplOpenGL3_CreateDeviceObjects(); } static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) { + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, polygon fill glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); @@ -254,19 +297,22 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glDisable(GL_STENCIL_TEST); glEnable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - if (g_GlVersion >= 310) + if (bd->GlVersion >= 310) glDisable(GL_PRIMITIVE_RESTART); #endif -#ifdef GL_POLYGON_MODE +#ifdef IMGUI_IMPL_HAS_POLYGON_MODE glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) -#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) +#if defined(GL_CLIP_ORIGIN) bool clip_origin_lower_left = true; - GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); - if (current_clip_origin == GL_UPPER_LEFT) - clip_origin_lower_left = false; + if (bd->HasClipOrigin) + { + GLenum current_clip_origin = 0; glGetIntegerv(GL_CLIP_ORIGIN, (GLint*)¤t_clip_origin); + if (current_clip_origin == GL_UPPER_LEFT) + clip_origin_lower_left = false; + } #endif // Setup viewport, orthographic projection matrix @@ -276,7 +322,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; -#if defined(GL_CLIP_ORIGIN) && !defined(__APPLE__) +#if defined(GL_CLIP_ORIGIN) if (!clip_origin_lower_left) { float tmp = T; T = B; B = tmp; } // Swap top and bottom if origin is upper left #endif const float ortho_projection[4][4] = @@ -286,29 +332,29 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid { 0.0f, 0.0f, -1.0f, 0.0f }, { (R+L)/(L-R), (T+B)/(B-T), 0.0f, 1.0f }, }; - glUseProgram(g_ShaderHandle); - glUniform1i(g_AttribLocationTex, 0); - glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); + glUseProgram(bd->ShaderHandle); + glUniform1i(bd->AttribLocationTex, 0); + glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (g_GlVersion >= 330) + if (bd->GlVersion >= 330) glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. #endif (void)vertex_array_object; -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glBindVertexArray(vertex_array_object); #endif // Bind vertex/index buffers and setup attributes for ImDrawVert - glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_ElementsHandle); - glEnableVertexAttribArray(g_AttribLocationVtxPos); - glEnableVertexAttribArray(g_AttribLocationVtxUV); - glEnableVertexAttribArray(g_AttribLocationVtxColor); - glVertexAttribPointer(g_AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos)); - glVertexAttribPointer(g_AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv)); - glVertexAttribPointer(g_AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col)); + glBindBuffer(GL_ARRAY_BUFFER, bd->VboHandle); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd->ElementsHandle); + glEnableVertexAttribArray(bd->AttribLocationVtxPos); + glEnableVertexAttribArray(bd->AttribLocationVtxUV); + glEnableVertexAttribArray(bd->AttribLocationVtxColor); + glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos)); + glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv)); + glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col)); } // OpenGL3 Render function. @@ -322,19 +368,21 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (fb_width <= 0 || fb_height <= 0) return; + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + // Backup GL state GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); glActiveTexture(GL_TEXTURE0); GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - GLuint last_sampler; if (g_GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } + GLuint last_sampler; if (bd->GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } #endif GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif -#ifdef GL_POLYGON_MODE +#ifdef IMGUI_IMPL_HAS_POLYGON_MODE GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); #endif GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); @@ -351,14 +399,14 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - GLboolean last_enable_primitive_restart = (g_GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; + GLboolean last_enable_primitive_restart = (bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; #endif // Setup desired GL state // Recreate the VAO every time (this is to easily allow multiple GL contexts to be rendered to. VAO are not shared among GL contexts) // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound. GLuint vertex_array_object = 0; -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glGenVertexArrays(1, &vertex_array_object); #endif ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); @@ -403,9 +451,9 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); // Bind texture, Draw - glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId); + glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET - if (g_GlVersion >= 320) + if (bd->GlVersion >= 320) glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); else #endif @@ -416,7 +464,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) } // Destroy the temporary VAO -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glDeleteVertexArrays(1, &vertex_array_object); #endif @@ -424,11 +472,11 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (g_GlVersion >= 330) + if (bd->GlVersion >= 330) glBindSampler(0, last_sampler); #endif glActiveTexture(last_active_texture); -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glBindVertexArray(last_vertex_array_object); #endif glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); @@ -440,20 +488,23 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - if (g_GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } + if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } #endif -#ifdef GL_POLYGON_MODE +#ifdef IMGUI_IMPL_HAS_POLYGON_MODE glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); #endif glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); + (void)bd; // Not all compilation paths use this } bool ImGui_ImplOpenGL3_CreateFontsTexture() { - // Build texture atlas ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + + // Build texture atlas unsigned char* pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. @@ -461,17 +512,17 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() // Upload texture to graphics system GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); - glGenTextures(1, &g_FontTexture); - glBindTexture(GL_TEXTURE_2D, g_FontTexture); + glGenTextures(1, &bd->FontTexture); + glBindTexture(GL_TEXTURE_2D, bd->FontTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); -#ifdef GL_UNPACK_ROW_LENGTH +#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); #endif glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); // Store our identifier - io.Fonts->SetTexID((ImTextureID)(intptr_t)g_FontTexture); + io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); // Restore state glBindTexture(GL_TEXTURE_2D, last_texture); @@ -481,23 +532,25 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() void ImGui_ImplOpenGL3_DestroyFontsTexture() { - if (g_FontTexture) + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + if (bd->FontTexture) { - ImGuiIO& io = ImGui::GetIO(); - glDeleteTextures(1, &g_FontTexture); + glDeleteTextures(1, &bd->FontTexture); io.Fonts->SetTexID(0); - g_FontTexture = 0; + bd->FontTexture = 0; } } // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file. static bool CheckShader(GLuint handle, const char* desc) { + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); GLint status = 0, log_length = 0; glGetShaderiv(handle, GL_COMPILE_STATUS, &status); glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length); if ((GLboolean)status == GL_FALSE) - fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s!\n", desc); + fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to compile %s! With GLSL: %s\n", desc, bd->GlslVersionString); if (log_length > 1) { ImVector buf; @@ -511,11 +564,12 @@ static bool CheckShader(GLuint handle, const char* desc) // If you get an error please report on GitHub. You may try different GL context version or GLSL version. static bool CheckProgram(GLuint handle, const char* desc) { + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); GLint status = 0, log_length = 0; glGetProgramiv(handle, GL_LINK_STATUS, &status); glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length); if ((GLboolean)status == GL_FALSE) - fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! (with GLSL '%s')\n", desc, g_GlslVersionString); + fprintf(stderr, "ERROR: ImGui_ImplOpenGL3_CreateDeviceObjects: failed to link %s! With GLSL %s\n", desc, bd->GlslVersionString); if (log_length > 1) { ImVector buf; @@ -528,18 +582,20 @@ static bool CheckProgram(GLuint handle, const char* desc) bool ImGui_ImplOpenGL3_CreateDeviceObjects() { + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + // Backup GL state GLint last_texture, last_array_buffer; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); #endif // Parse GLSL version string int glsl_version = 130; - sscanf(g_GlslVersionString, "#version %d", &glsl_version); + sscanf(bd->GlslVersionString, "#version %d", &glsl_version); const GLchar* vertex_shader_glsl_120 = "uniform mat4 ProjMtx;\n" @@ -570,7 +626,7 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() "}\n"; const GLchar* vertex_shader_glsl_300_es = - "precision mediump float;\n" + "precision highp float;\n" "layout (location = 0) in vec2 Position;\n" "layout (location = 1) in vec2 UV;\n" "layout (location = 2) in vec4 Color;\n" @@ -666,40 +722,46 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() } // Create shaders - const GLchar* vertex_shader_with_version[2] = { g_GlslVersionString, vertex_shader }; - g_VertHandle = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(g_VertHandle, 2, vertex_shader_with_version, NULL); - glCompileShader(g_VertHandle); - CheckShader(g_VertHandle, "vertex shader"); - - const GLchar* fragment_shader_with_version[2] = { g_GlslVersionString, fragment_shader }; - g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(g_FragHandle, 2, fragment_shader_with_version, NULL); - glCompileShader(g_FragHandle); - CheckShader(g_FragHandle, "fragment shader"); - - g_ShaderHandle = glCreateProgram(); - glAttachShader(g_ShaderHandle, g_VertHandle); - glAttachShader(g_ShaderHandle, g_FragHandle); - glLinkProgram(g_ShaderHandle); - CheckProgram(g_ShaderHandle, "shader program"); - - g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); - g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); - g_AttribLocationVtxPos = (GLuint)glGetAttribLocation(g_ShaderHandle, "Position"); - g_AttribLocationVtxUV = (GLuint)glGetAttribLocation(g_ShaderHandle, "UV"); - g_AttribLocationVtxColor = (GLuint)glGetAttribLocation(g_ShaderHandle, "Color"); + const GLchar* vertex_shader_with_version[2] = { bd->GlslVersionString, vertex_shader }; + GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vert_handle, 2, vertex_shader_with_version, NULL); + glCompileShader(vert_handle); + CheckShader(vert_handle, "vertex shader"); + + const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader }; + GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(frag_handle, 2, fragment_shader_with_version, NULL); + glCompileShader(frag_handle); + CheckShader(frag_handle, "fragment shader"); + + // Link + bd->ShaderHandle = glCreateProgram(); + glAttachShader(bd->ShaderHandle, vert_handle); + glAttachShader(bd->ShaderHandle, frag_handle); + glLinkProgram(bd->ShaderHandle); + CheckProgram(bd->ShaderHandle, "shader program"); + + glDetachShader(bd->ShaderHandle, vert_handle); + glDetachShader(bd->ShaderHandle, frag_handle); + glDeleteShader(vert_handle); + glDeleteShader(frag_handle); + + bd->AttribLocationTex = glGetUniformLocation(bd->ShaderHandle, "Texture"); + bd->AttribLocationProjMtx = glGetUniformLocation(bd->ShaderHandle, "ProjMtx"); + bd->AttribLocationVtxPos = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Position"); + bd->AttribLocationVtxUV = (GLuint)glGetAttribLocation(bd->ShaderHandle, "UV"); + bd->AttribLocationVtxColor = (GLuint)glGetAttribLocation(bd->ShaderHandle, "Color"); // Create buffers - glGenBuffers(1, &g_VboHandle); - glGenBuffers(1, &g_ElementsHandle); + glGenBuffers(1, &bd->VboHandle); + glGenBuffers(1, &bd->ElementsHandle); ImGui_ImplOpenGL3_CreateFontsTexture(); // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); -#ifndef IMGUI_IMPL_OPENGL_ES2 +#ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glBindVertexArray(last_vertex_array); #endif @@ -708,13 +770,9 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() void ImGui_ImplOpenGL3_DestroyDeviceObjects() { - if (g_VboHandle) { glDeleteBuffers(1, &g_VboHandle); g_VboHandle = 0; } - if (g_ElementsHandle) { glDeleteBuffers(1, &g_ElementsHandle); g_ElementsHandle = 0; } - if (g_ShaderHandle && g_VertHandle) { glDetachShader(g_ShaderHandle, g_VertHandle); } - if (g_ShaderHandle && g_FragHandle) { glDetachShader(g_ShaderHandle, g_FragHandle); } - if (g_VertHandle) { glDeleteShader(g_VertHandle); g_VertHandle = 0; } - if (g_FragHandle) { glDeleteShader(g_FragHandle); g_FragHandle = 0; } - if (g_ShaderHandle) { glDeleteProgram(g_ShaderHandle); g_ShaderHandle = 0; } - + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + if (bd->VboHandle) { glDeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; } + if (bd->ElementsHandle) { glDeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; } + if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; } ImGui_ImplOpenGL3_DestroyFontsTexture(); } diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index 8c0126d8..b1fb49c7 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -7,15 +7,11 @@ // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. -// You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. // Read online: https://github.com/ocornut/imgui/tree/master/docs -// About Desktop OpenGL function loaders: -// Modern Desktop OpenGL doesn't have a standard portable header file to load OpenGL function pointers. -// Helper libraries are often used for this purpose! Here we are supporting a few common ones (gl3w, glew, glad). -// You may use another loader/header of your choice (glext, glLoadGen, etc.), or chose to manually implement your own. - // About GLSL version: // The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string. // On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es" @@ -40,19 +36,9 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); //#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten //#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android -// Attempt to auto-detect the default Desktop GL loader based on available header files. -// If auto-detection fails or doesn't select the same GL loader file as used by your application, -// you are likely to get a crash in ImGui_ImplOpenGL3_Init(). -// You can explicitly select a loader by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. +// You can explicitly select GLES2 or GLES3 API by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. #if !defined(IMGUI_IMPL_OPENGL_ES2) \ - && !defined(IMGUI_IMPL_OPENGL_ES3) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GL3W) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLEW) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3) \ - && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) + && !defined(IMGUI_IMPL_OPENGL_ES3) // Try to detect GLES on matching platforms #if defined(__APPLE__) @@ -62,26 +48,8 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" #elif defined(__EMSCRIPTEN__) #define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" - -// Otherwise try to detect supported Desktop OpenGL loaders.. -#elif defined(__has_include) -#if __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLEW -#elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLAD -#elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLAD2 -#elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GL3W -#elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING3 -#elif __has_include() - #define IMGUI_IMPL_OPENGL_LOADER_GLBINDING2 -#else - #error "Cannot detect OpenGL loader!" -#endif #else - #define IMGUI_IMPL_OPENGL_LOADER_GL3W // Default to GL3W embedded in our repository +// Otherwise imgui_impl_opengl3_loader.h will be used. #endif #endif diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index ea50a49c..1eb0cb7a 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -43,7 +43,7 @@ Index of this file: //----------------------------------------------------------------------------- #ifndef IMGUI_VERSION -#error Must include imgui.h before imgui_internal.h +#include "imgui.h" #endif #include // FILE*, sscanf @@ -51,10 +51,21 @@ Index of this file: #include // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf #include // INT_MIN, INT_MAX +// Enable SSE intrinsics if available +#if (defined __SSE__ || defined __x86_64__ || defined _M_X64) && !defined(IMGUI_DISABLE_SSE) +#define IMGUI_ENABLE_SSE +#include +#endif + // Visual Studio warnings #ifdef _MSC_VER #pragma warning (push) -#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport) +#pragma warning (disable: 4251) // class 'xxx' needs to have dll-interface to be used by clients of struct 'xxx' // when IMGUI_API is set to__declspec(dllexport) +#pragma warning (disable: 26812) // The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer) +#pragma warning (disable: 26495) // [Static Analyzer] Variable 'XXX' is uninitialized. Always initialize a member variable (type.6). +#if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later +#pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types +#endif #endif // Clang/GCC warnings with -Weverything @@ -64,6 +75,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' #endif #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok, for ImFloorSigned() #pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h #pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h #pragma clang diagnostic ignored "-Wold-style-cast" @@ -76,6 +88,13 @@ Index of this file: #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif +// Helper macros +#if defined(__clang__) +#define IM_NORETURN __attribute__((noreturn)) +#else +#define IM_NORETURN +#endif + // Legacy defines #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 #error Use IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS @@ -104,9 +123,9 @@ struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box -struct ImGuiLastItemDataBackup; // Backup and restore IsItemHovered() internal data +struct ImGuiLastItemData; // Status storage for last submitted items struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only -struct ImGuiNavMoveResult; // Result of a gamepad/keyboard directional navigation move query result +struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions @@ -120,15 +139,17 @@ struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) struct ImGuiTable; // Storage for a table struct ImGuiTableColumn; // Storage for one column of a table +struct ImGuiTableTempData; // Temporary storage for one table (one per table in the stack), shared between tables. struct ImGuiTableSettings; // Storage for a table .ini settings struct ImGuiTableColumnsSettings; // Storage for a column .ini settings struct ImGuiWindow; // Storage for one window -struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame) +struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame, in practice we currently keep it for each window) struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session) // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() +typedef int ImGuiItemAddFlags; // -> enum ImGuiItemAddFlags_ // Flags: for ItemAdd() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() @@ -227,6 +248,13 @@ namespace ImStb #define IMGUI_CDECL #endif +// Warnings +#if defined(_MSC_VER) && !defined(__clang__) +#define IM_MSVC_WARNING_SUPPRESS(XXXX) __pragma(warning(suppress: XXXX)) +#else +#define IM_MSVC_WARNING_SUPPRESS(XXXX) +#endif + // Debug Tools // Use 'Metrics->Tools->Item Picker' to break into the call-stack of a specific item. #ifndef IM_DEBUG_BREAK @@ -303,17 +331,19 @@ static inline bool ImCharIsBlankA(char c) { return c == ' ' || c = static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } // Helpers: UTF-8 <> wchar conversions -IMGUI_API int ImTextStrToUtf8(char* buf, int buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count -IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count -IMGUI_API int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count -IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) -IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 -IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 +IMGUI_API const char* ImTextCharToUtf8(char out_buf[5], unsigned int c); // return out_buf +IMGUI_API int ImTextStrToUtf8(char* out_buf, int out_buf_size, const ImWchar* in_text, const ImWchar* in_text_end); // return output UTF-8 bytes count +IMGUI_API int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* in_text_end); // read one character. return input UTF-8 bytes count +IMGUI_API int ImTextStrFromUtf8(ImWchar* out_buf, int out_buf_size, const char* in_text, const char* in_text_end, const char** in_remaining = NULL); // return input UTF-8 bytes count +IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) +IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 +IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 // Helpers: ImVec2/ImVec4 operators // We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.) // We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. #ifdef IMGUI_DEFINE_MATH_OPERATORS +IM_MSVC_RUNTIME_CHECKS_OFF static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); } static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); } static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } @@ -329,6 +359,7 @@ static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); } static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); } static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); } +IM_MSVC_RUNTIME_CHECKS_RESTORE #endif // Helpers: File System @@ -354,6 +385,7 @@ IMGUI_API ImU64 ImFileWrite(const void* data, ImU64 size, ImU64 coun IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_file_size = NULL, int padding_bytes = 0); // Helpers: Maths +IM_MSVC_RUNTIME_CHECKS_OFF // - Wrapper for standard libs functions. (Note that imgui_demo.cpp does _not_ use them to keep the code easy to copy) #ifndef IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS #define ImFabs(X) fabsf(X) @@ -364,16 +396,23 @@ IMGUI_API void* ImFileLoadToMemory(const char* filename, const char* #define ImAcos(X) acosf(X) #define ImAtan2(Y, X) atan2f((Y), (X)) #define ImAtof(STR) atof(STR) -#define ImFloorStd(X) floorf(X) // We already uses our own ImFloor() { return (float)(int)v } internally so the standard one wrapper is named differently (it's used by e.g. stb_truetype) +//#define ImFloorStd(X) floorf(X) // We use our own, see ImFloor() and ImFloorSigned() #define ImCeil(X) ceilf(X) static inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision static inline double ImPow(double x, double y) { return pow(x, y); } static inline float ImLog(float x) { return logf(x); } // DragBehaviorT/SliderBehaviorT uses ImLog with either float/double and need the precision static inline double ImLog(double x) { return log(x); } +static inline int ImAbs(int x) { return x < 0 ? -x : x; } static inline float ImAbs(float x) { return fabsf(x); } static inline double ImAbs(double x) { return fabs(x); } static inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : ((x > 0.0f) ? 1.0f : 0.0f); } // Sign operator - returns -1, 0 or 1 based on sign of argument static inline double ImSign(double x) { return (x < 0.0) ? -1.0 : ((x > 0.0) ? 1.0 : 0.0); } +#ifdef IMGUI_ENABLE_SSE +static inline float ImRsqrt(float x) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); } +#else +static inline float ImRsqrt(float x) { return 1.0f / sqrtf(x); } +#endif +static inline double ImRsqrt(double x) { return 1.0 / sqrt(x); } #endif // - ImMin/ImMax/ImClamp/ImLerp/ImSwap are used by widgets which support variety of types: signed/unsigned int/long long float/double // (Exceptionally using templates here but we could also redefine them for those types) @@ -394,14 +433,16 @@ static inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) static inline float ImSaturate(float f) { return (f < 0.0f) ? 0.0f : (f > 1.0f) ? 1.0f : f; } static inline float ImLengthSqr(const ImVec2& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y); } static inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); } -static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return 1.0f / ImSqrt(d); return fail_value; } +static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return ImRsqrt(d); return fail_value; } static inline float ImFloor(float f) { return (float)(int)(f); } +static inline float ImFloorSigned(float f) { return (float)((f >= 0 || (int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf() static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } +IM_MSVC_RUNTIME_CHECKS_RESTORE // Helpers: Geometry IMGUI_API ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t); @@ -417,6 +458,7 @@ IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); // Helper: ImVec1 (1D vector) // (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) +IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec1 { float x; @@ -470,6 +512,7 @@ struct IMGUI_API ImRect bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); } }; +IM_MSVC_RUNTIME_CHECKS_RESTORE // Helper: ImBitArray inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } @@ -489,12 +532,12 @@ inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) // Works on ran } // Helper: ImBitArray class (wrapper over ImBitArray functions) -// Store 1-bit per value. NOT CLEARED by constructor. +// Store 1-bit per value. template struct IMGUI_API ImBitArray { ImU32 Storage[(BITCOUNT + 31) >> 5]; - ImBitArray() { } + ImBitArray() { ClearAllBits(); } void ClearAllBits() { memset(Storage, 0, sizeof(Storage)); } void SetAllBits() { memset(Storage, 255, sizeof(Storage)); } bool TestBit(int n) const { IM_ASSERT(n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } @@ -576,20 +619,30 @@ struct IMGUI_API ImPool ImVector Buf; // Contiguous data ImGuiStorage Map; // ID->Index ImPoolIdx FreeIdx; // Next free idx to use + ImPoolIdx AliveCount; // Number of active/alive items (for display purpose) - ImPool() { FreeIdx = 0; } + ImPool() { FreeIdx = AliveCount = 0; } ~ImPool() { Clear(); } T* GetByKey(ImGuiID key) { int idx = Map.GetInt(key, -1); return (idx != -1) ? &Buf[idx] : NULL; } T* GetByIndex(ImPoolIdx n) { return &Buf[n]; } ImPoolIdx GetIndex(const T* p) const { IM_ASSERT(p >= Buf.Data && p < Buf.Data + Buf.Size); return (ImPoolIdx)(p - Buf.Data); } T* GetOrAddByKey(ImGuiID key) { int* p_idx = Map.GetIntRef(key, -1); if (*p_idx != -1) return &Buf[*p_idx]; *p_idx = FreeIdx; return Add(); } bool Contains(const T* p) const { return (p >= Buf.Data && p < Buf.Data + Buf.Size); } - void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Buf[idx].~T(); } Map.Clear(); Buf.clear(); FreeIdx = 0; } - T* Add() { int idx = FreeIdx; if (idx == Buf.Size) { Buf.resize(Buf.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Buf[idx]; } IM_PLACEMENT_NEW(&Buf[idx]) T(); return &Buf[idx]; } + void Clear() { for (int n = 0; n < Map.Data.Size; n++) { int idx = Map.Data[n].val_i; if (idx != -1) Buf[idx].~T(); } Map.Clear(); Buf.clear(); FreeIdx = AliveCount = 0; } + T* Add() { int idx = FreeIdx; if (idx == Buf.Size) { Buf.resize(Buf.Size + 1); FreeIdx++; } else { FreeIdx = *(int*)&Buf[idx]; } IM_PLACEMENT_NEW(&Buf[idx]) T(); AliveCount++; return &Buf[idx]; } void Remove(ImGuiID key, const T* p) { Remove(key, GetIndex(p)); } - void Remove(ImGuiID key, ImPoolIdx idx) { Buf[idx].~T(); *(int*)&Buf[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); } + void Remove(ImGuiID key, ImPoolIdx idx) { Buf[idx].~T(); *(int*)&Buf[idx] = FreeIdx; FreeIdx = idx; Map.SetInt(key, -1); AliveCount--; } void Reserve(int capacity) { Buf.reserve(capacity); Map.Data.reserve(capacity); } - int GetSize() const { return Buf.Size; } + + // To iterate a ImPool: for (int n = 0; n < pool.GetMapSize(); n++) if (T* t = pool.TryGetMapData(n)) { ... } + // Can be avoided if you know .Remove() has never been called on the pool, or AliveCount == GetMapSize() + int GetAliveCount() const { return AliveCount; } // Number of active/alive items in the pool (for display purpose) + int GetBufSize() const { return Buf.Size; } + int GetMapSize() const { return Map.Data.Size; } // It is the map we need iterate to find valid items, since we don't have "alive" storage anywhere + T* TryGetMapData(ImPoolIdx n) { int idx = Map.Data[n].val_i; if (idx == -1) return NULL; return GetByIndex(idx); } +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + int GetSize() { return GetMapSize(); } // For ImPlot: should use GetMapSize() from (IMGUI_VERSION_NUM >= 18304) +#endif }; // Helper: ImChunkStream<> @@ -687,39 +740,58 @@ struct ImDrawDataBuilder enum ImGuiItemFlags_ { ImGuiItemFlags_None = 0, - ImGuiItemFlags_NoTabStop = 1 << 0, // false - ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. - ImGuiItemFlags_Disabled = 1 << 2, // false // [BETA] Disable interactions but doesn't affect visuals yet. See github.com/ocornut/imgui/issues/211 - ImGuiItemFlags_NoNav = 1 << 3, // false - ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false - ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // MenuItem/Selectable() automatically closes current Popup window - ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) - ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. - ImGuiItemFlags_Default_ = 0 + ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing (FIXME: should merge with _NoNav) + ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. + ImGuiItemFlags_Disabled = 1 << 2, // false // Disable interactions but doesn't affect visuals. See BeginDisabled()/EndDisabled(). See github.com/ocornut/imgui/issues/211 + ImGuiItemFlags_NoNav = 1 << 3, // false // Disable keyboard/gamepad directional navigation (FIXME: should merge with _NoTabStop) + ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false // Disable item being a candidate for default focus (e.g. used by title bar items) + ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // Disable MenuItem/Selectable() automatically closing their popup window + ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) + ImGuiItemFlags_ReadOnly = 1 << 7 // false // [ALPHA] Allow hovering interactions but underlying value is not changed. +}; + +// Flags for ItemAdd() +// FIXME-NAV: _Focusable is _ALMOST_ what you would expect to be called '_TabStop' but because SetKeyboardFocusHere() works on items with no TabStop we distinguish Focusable from TabStop. +enum ImGuiItemAddFlags_ +{ + ImGuiItemAddFlags_None = 0, + ImGuiItemAddFlags_Focusable = 1 << 0 // FIXME-NAV: In current/legacy scheme, Focusable+TabStop support are opt-in by widgets. We will transition it toward being opt-out, so this flag is expected to eventually disappear. }; // Storage for LastItem data enum ImGuiItemStatusFlags_ { ImGuiItemStatusFlags_None = 0, - ImGuiItemStatusFlags_HoveredRect = 1 << 0, - ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, // LastItemDisplayRect is valid + ImGuiItemStatusFlags_HoveredRect = 1 << 0, // Mouse position is within item rectangle (does NOT mean that the window is in correct z-order and can be hovered!, this is only one part of the most-common IsItemHovered test) + ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, // window->DC.LastItemDisplayRect is valid ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) - ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected" because reporting the change allows us to handle clipping with less issues. + ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected", only state changes, in order to easily handle clipping with less issues. ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. - ImGuiItemStatusFlags_HoveredWindow = 1 << 7 // Override the HoveredWindow test to allow cross-window hover testing. + ImGuiItemStatusFlags_HoveredWindow = 1 << 7, // Override the HoveredWindow test to allow cross-window hover testing. + ImGuiItemStatusFlags_FocusedByCode = 1 << 8, // Set when the Focusable item just got focused from code. + ImGuiItemStatusFlags_FocusedByTabbing = 1 << 9, // Set when the Focusable item just got focused by Tabbing. + ImGuiItemStatusFlags_Focused = ImGuiItemStatusFlags_FocusedByCode | ImGuiItemStatusFlags_FocusedByTabbing #ifdef IMGUI_ENABLE_TEST_ENGINE , // [imgui_tests only] - ImGuiItemStatusFlags_Openable = 1 << 10, // - ImGuiItemStatusFlags_Opened = 1 << 11, // - ImGuiItemStatusFlags_Checkable = 1 << 12, // - ImGuiItemStatusFlags_Checked = 1 << 13 // + ImGuiItemStatusFlags_Openable = 1 << 20, // + ImGuiItemStatusFlags_Opened = 1 << 21, // + ImGuiItemStatusFlags_Checkable = 1 << 22, // + ImGuiItemStatusFlags_Checked = 1 << 23 // #endif }; +// Extend ImGuiInputTextFlags_ +enum ImGuiInputTextFlagsPrivate_ +{ + // [Internal] + ImGuiInputTextFlags_Multiline = 1 << 26, // For internal use by InputTextMultiline() + ImGuiInputTextFlags_NoMarkEdited = 1 << 27, // For internal use by functions using InputText() before reformatting data + ImGuiInputTextFlags_MergedItem = 1 << 28 // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. +}; + // Extend ImGuiButtonFlags_ enum ImGuiButtonFlagsPrivate_ { @@ -733,7 +805,7 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_FlattenChildren = 1 << 11, // allow interactions even if a child window is overlapping ImGuiButtonFlags_AllowItemOverlap = 1 << 12, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() ImGuiButtonFlags_DontClosePopups = 1 << 13, // disable automatically closing parent popup on press // [UNUSED] - ImGuiButtonFlags_Disabled = 1 << 14, // disable interactions + //ImGuiButtonFlags_Disabled = 1 << 14, // disable interactions -> use BeginDisabled() or ImGuiItemFlags_Disabled ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine ImGuiButtonFlags_NoKeyModifiers = 1 << 16, // disable mouse interaction if a key modifier is held ImGuiButtonFlags_NoHoldingActiveId = 1 << 17, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) @@ -743,6 +815,12 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease }; +// Extend ImGuiComboFlags_ +enum ImGuiComboFlagsPrivate_ +{ + ImGuiComboFlags_CustomPreview = 1 << 20 // enable BeginComboPreview() +}; + // Extend ImGuiSliderFlags_ enum ImGuiSliderFlagsPrivate_ { @@ -755,12 +833,13 @@ enum ImGuiSelectableFlagsPrivate_ { // NB: need to be in sync with last value of ImGuiSelectableFlags_ ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20, - ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release) - ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release) - ImGuiSelectableFlags_SpanAvailWidth = 1 << 23, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) - ImGuiSelectableFlags_DrawHoveredWhenHeld = 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. - ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25, // Set Nav/Focus ID on mouse hover (used by MenuItem) - ImGuiSelectableFlags_NoPadWithHalfSpacing = 1 << 26 // Disable padding each side with ItemSpacing * 0.5f + ImGuiSelectableFlags_SelectOnNav = 1 << 21, // (WIP) Auto-select when moved into. This is not exposed in public API as to handle multi-select and modifiers we will need user to explicitly control focus scope. May be replaced with a BeginSelection() API. + ImGuiSelectableFlags_SelectOnClick = 1 << 22, // Override button behavior to react on Click (default is Click+Release) + ImGuiSelectableFlags_SelectOnRelease = 1 << 23, // Override button behavior to react on Release (default is Click+Release) + ImGuiSelectableFlags_SpanAvailWidth = 1 << 24, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) + ImGuiSelectableFlags_DrawHoveredWhenHeld = 1 << 25, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. + ImGuiSelectableFlags_SetNavIdOnHover = 1 << 26, // Set Nav/Focus ID on mouse hover (used by MenuItem) + ImGuiSelectableFlags_NoPadWithHalfSpacing = 1 << 27 // Disable padding each side with ItemSpacing * 0.5f }; // Extend ImGuiTreeNodeFlags_ @@ -827,6 +906,7 @@ enum ImGuiInputSource ImGuiInputSource_Keyboard, ImGuiInputSource_Gamepad, ImGuiInputSource_Nav, // Stored in g.ActiveIdSource only + ImGuiInputSource_Clipboard, // Currently only used by InputText() ImGuiInputSource_COUNT }; @@ -930,6 +1010,19 @@ struct ImGuiStyleMod ImGuiStyleMod(ImGuiStyleVar idx, ImVec2 v) { VarIdx = idx; BackupFloat[0] = v.x; BackupFloat[1] = v.y; } }; +// Storage data for BeginComboPreview()/EndComboPreview() +struct IMGUI_API ImGuiComboPreviewData +{ + ImRect PreviewRect; + ImVec2 BackupCursorPos; + ImVec2 BackupCursorMaxPos; + ImVec2 BackupCursorPosPrevLine; + float BackupPrevLineTextBaseOffset; + ImGuiLayoutType BackupLayout; + + ImGuiComboPreviewData() { memset(this, 0, sizeof(*this)); } +}; + // Stacked storage data for BeginGroup()/EndGroup() struct IMGUI_API ImGuiGroupData { @@ -949,14 +1042,19 @@ struct IMGUI_API ImGuiGroupData // Simple column measurement, currently used for MenuItem() only.. This is very short-sighted/throw-away code and NOT a generic helper. struct IMGUI_API ImGuiMenuColumns { - float Spacing; - float Width, NextWidth; - float Pos[3], NextWidths[3]; + ImU32 TotalWidth; + ImU32 NextTotalWidth; + ImU16 Spacing; + ImU16 OffsetIcon; // Always zero for now + ImU16 OffsetLabel; // Offsets are locked in Update() + ImU16 OffsetShortcut; + ImU16 OffsetMark; + ImU16 Widths[4]; // Width of: Icon, Label, Shortcut, Mark (accumulators for current frame) ImGuiMenuColumns() { memset(this, 0, sizeof(*this)); } - void Update(int count, float spacing, bool clear); - float DeclColumns(float w0, float w1, float w2); - float CalcExtraSpace(float avail_w) const; + void Update(float spacing, bool window_reappearing); + float DeclColumns(float w_icon, float w_label, float w_shortcut, float w_mark); + void CalcNextTotalWidth(bool update_offsets); }; // Internal state of the currently focused/edited text input box @@ -976,7 +1074,7 @@ struct IMGUI_API ImGuiInputTextState bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection bool Edited; // edited this frame - ImGuiInputTextFlags UserFlags; // Temporarily set while we call user's callback + ImGuiInputTextFlags Flags; // copy of InputText() flags ImGuiInputTextCallback UserCallback; // " void* UserCallbackData; // " @@ -992,6 +1090,9 @@ struct IMGUI_API ImGuiInputTextState void CursorClamp() { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); } bool HasSelection() const { return Stb.select_start != Stb.select_end; } void ClearSelection() { Stb.select_start = Stb.select_end = Stb.cursor; } + int GetCursorPos() const { return Stb.cursor; } + int GetSelectionStart() const { return Stb.select_start; } + int GetSelectionEnd() const { return Stb.select_end; } void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } }; @@ -1009,18 +1110,18 @@ struct ImGuiPopupData ImGuiPopupData() { memset(this, 0, sizeof(*this)); OpenFrameCount = -1; } }; -struct ImGuiNavMoveResult +struct ImGuiNavItemData { - ImGuiWindow* Window; // Best candidate window - ImGuiID ID; // Best candidate ID - ImGuiID FocusScopeId; // Best candidate focus scope ID - float DistBox; // Best candidate box distance to current NavId - float DistCenter; // Best candidate center distance to current NavId - float DistAxial; - ImRect RectRel; // Best candidate bounding box in window relative space + ImGuiWindow* Window; // Init,Move // Best candidate window (result->ItemWindow->RootWindowForNav == request->Window) + ImGuiID ID; // Init,Move // Best candidate item ID + ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID + ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space + float DistBox; // Move // Best candidate box distance to current NavId + float DistCenter; // Move // Best candidate center distance to current NavId + float DistAxial; // Move // Best candidate axial distance to current NavId - ImGuiNavMoveResult() { Clear(); } - void Clear() { Window = NULL; ID = FocusScopeId = 0; DistBox = DistCenter = DistAxial = FLT_MAX; RectRel = ImRect(); } + ImGuiNavItemData() { Clear(); } + void Clear() { Window = NULL; ID = FocusScopeId = 0; RectRel = ImRect(); DistBox = DistCenter = DistAxial = FLT_MAX; } }; enum ImGuiNextWindowDataFlags_ @@ -1053,7 +1154,7 @@ struct ImGuiNextWindowData ImGuiSizeCallback SizeCallback; void* SizeCallbackUserData; float BgAlphaVal; // Override background alpha - ImVec2 MenuBarOffsetMinVal; // *Always on* This is not exposed publicly, so we don't clear it. + ImVec2 MenuBarOffsetMinVal; // (Always on) This is not exposed publicly, so we don't clear it and it doesn't have a corresponding flag (could we? for consistency?) ImGuiNextWindowData() { memset(this, 0, sizeof(*this)); } inline void ClearFlags() { Flags = ImGuiNextWindowDataFlags_None; } @@ -1078,6 +1179,25 @@ struct ImGuiNextItemData inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } // Also cleared manually by ItemAdd()! }; +// Status storage for the last submitted item +struct ImGuiLastItemData +{ + ImGuiID ID; + ImGuiItemFlags InFlags; // See ImGuiItemFlags_ + ImGuiItemStatusFlags StatusFlags; // See ImGuiItemStatusFlags_ + ImRect Rect; + ImRect DisplayRect; + + ImGuiLastItemData() { memset(this, 0, sizeof(*this)); } +}; + +// Data saved for each window pushed into the stack +struct ImGuiWindowStackData +{ + ImGuiWindow* Window; + ImGuiLastItemData ParentLastItemDataBackup; +}; + struct ImGuiShrinkWidthItem { int Index; @@ -1180,14 +1300,21 @@ struct ImGuiViewportP : public ImGuiViewport ImVec2 WorkOffsetMin; // Work Area: Offset from Pos to top-left corner of Work Area. Generally (0,0) or (0,+main_menu_bar_height). Work Area is Full Area but without menu-bars/status-bars (so WorkArea always fit inside Pos/Size!) ImVec2 WorkOffsetMax; // Work Area: Offset from Pos+Size to bottom-right corner of Work Area. Generally (0,0) or (0,-status_bar_height). - ImVec2 CurrWorkOffsetMin; // Work Area: Offset being built/increased during current frame - ImVec2 CurrWorkOffsetMax; // Work Area: Offset being built/decreased during current frame + ImVec2 BuildWorkOffsetMin; // Work Area: Offset being built during current frame. Generally >= 0.0f. + ImVec2 BuildWorkOffsetMax; // Work Area: Offset being built during current frame. Generally <= 0.0f. + + ImGuiViewportP() { DrawListsLastFrame[0] = DrawListsLastFrame[1] = -1; DrawLists[0] = DrawLists[1] = NULL; } + ~ImGuiViewportP() { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); } + + // Calculate work rect pos/size given a set of offset (we have 1 pair of offset for rect locked from last frame data, and 1 pair for currently building rect) + ImVec2 CalcWorkRectPos(const ImVec2& off_min) const { return ImVec2(Pos.x + off_min.x, Pos.y + off_min.y); } + ImVec2 CalcWorkRectSize(const ImVec2& off_min, const ImVec2& off_max) const { return ImVec2(ImMax(0.0f, Size.x - off_min.x + off_max.x), ImMax(0.0f, Size.y - off_min.y + off_max.y)); } + void UpdateWorkRect() { WorkPos = CalcWorkRectPos(WorkOffsetMin); WorkSize = CalcWorkRectSize(WorkOffsetMin, WorkOffsetMax); } // Update public fields - ImGuiViewportP() { DrawListsLastFrame[0] = DrawListsLastFrame[1] = -1; DrawLists[0] = DrawLists[1] = NULL; } - ~ImGuiViewportP() { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); } - ImRect GetMainRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } - ImRect GetWorkRect() const { return ImRect(WorkPos.x, WorkPos.y, WorkPos.x + WorkSize.x, WorkPos.y + WorkSize.y); } - void UpdateWorkRect() { WorkPos = ImVec2(Pos.x + WorkOffsetMin.x, Pos.y + WorkOffsetMin.y); WorkSize = ImVec2(ImMax(0.0f, Size.x - WorkOffsetMin.x + WorkOffsetMax.x), ImMax(0.0f, Size.y - WorkOffsetMin.y + WorkOffsetMax.y)); } + // Helpers to retrieve ImRect (we don't need to store BuildWorkRect as every access tend to change it, hence the code asymmetry) + ImRect GetMainRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } + ImRect GetWorkRect() const { return ImRect(WorkPos.x, WorkPos.y, WorkPos.x + WorkSize.x, WorkPos.y + WorkSize.y); } + ImRect GetBuildWorkRect() const { ImVec2 pos = CalcWorkRectPos(BuildWorkOffsetMin); ImVec2 size = CalcWorkRectSize(BuildWorkOffsetMin, BuildWorkOffsetMax); return ImRect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); } }; //----------------------------------------------------------------------------- @@ -1311,11 +1438,12 @@ struct ImGuiContext // Windows state ImVector Windows; // Windows, sorted in display order, back to front - ImVector WindowsFocusOrder; // Windows, sorted in focus order, back to front. (FIXME: We could only store root windows here! Need to sort out the Docking equivalent which is RootWindowDockStop and is unfortunately a little more dynamic) + ImVector WindowsFocusOrder; // Root windows, sorted in focus order, back to front. ImVector WindowsTempSortBuffer; // Temporary buffer used in EndFrame() to reorder windows so parents are kept before their child - ImVector CurrentWindowStack; + ImVector CurrentWindowStack; ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow* int WindowsActiveCount; // Number of unique windows submitted by frame + ImVec2 WindowsHoverPadding; // Padding around resizable windows for which hovering on counts as hovering the window == ImMax(style.TouchExtraPadding, WINDOWS_HOVER_PADDING) ImGuiWindow* CurrentWindow; // Window being drawn into ImGuiWindow* HoveredWindow; // Window the mouse is hovering. Will typically catch mouse inputs. ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. @@ -1358,8 +1486,10 @@ struct ImGuiContext float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. // Next window/item data - ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions + ImGuiItemFlags CurrentItemFlags; // == g.ItemFlagsStack.back() ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions + ImGuiLastItemData LastItemData; // Storage for last submitted item (setup by ItemAdd) + ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions // Shared stacks ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() @@ -1407,9 +1537,9 @@ struct ImGuiContext ImGuiKeyModFlags NavMoveRequestKeyMods; ImGuiDir NavMoveDir, NavMoveDirLast; // Direction of the move request (left/right/up/down), direction of the previous move request ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? - ImGuiNavMoveResult NavMoveResultLocal; // Best move request candidate within NavWindow - ImGuiNavMoveResult NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) - ImGuiNavMoveResult NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) + ImGuiNavItemData NavMoveResultLocal; // Best move request candidate within NavWindow + ImGuiNavItemData NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) + ImGuiNavItemData NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) ImGuiWindow* NavWrapRequestWindow; // Window which requested trying nav wrap-around. ImGuiNavMoveFlags NavWrapRequestFlags; // Wrap-around operation flags. @@ -1428,7 +1558,7 @@ struct ImGuiContext int TabFocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index int TabFocusRequestNextCounterRegular; // Stored for next frame int TabFocusRequestNextCounterTabStop; // " - bool TabFocusPressed; // + bool TabFocusPressed; // Set in NewFrame() when user pressed Tab // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) @@ -1455,8 +1585,9 @@ struct ImGuiContext // Table ImGuiTable* CurrentTable; + int CurrentTableStackIdx; ImPool Tables; - ImVector CurrentTableStack; + ImVector TablesTempDataStack; ImVector TablesLastTimeActive; // Last used timestamp of each tables (SOA, for efficient GC) ImVector DrawChannelsTempMergeBuffer; @@ -1476,11 +1607,13 @@ struct ImGuiContext float ColorEditLastSat; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips float ColorEditLastColor[3]; ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. + ImGuiComboPreviewData ComboPreviewData; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio + float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? int TooltipOverrideCount; float TooltipSlowDelay; // Time before slow tooltips appears (FIXME: This is temporary until we merge in tooltip timer+priority work) @@ -1523,6 +1656,7 @@ struct ImGuiContext // Misc float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. int FramerateSecPerFrameIdx; + int FramerateSecPerFrameCount; float FramerateSecPerFrameAccum; int WantCaptureMouseNextFrame; // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags int WantCaptureKeyboardNextFrame; @@ -1582,6 +1716,8 @@ struct ImGuiContext LastActiveId = 0; LastActiveIdTimer = 0.0f; + CurrentItemFlags = ImGuiItemFlags_None; + NavWindow = NULL; NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; NavJustTabbedId = NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; @@ -1632,11 +1768,12 @@ struct ImGuiContext memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); CurrentTable = NULL; + CurrentTableStackIdx = -1; CurrentTabBar = NULL; LastValidMousePos = ImVec2(0.0f, 0.0f); TempInputId = 0; - ColorEditOptions = ImGuiColorEditFlags__OptionsDefault; + ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditLastHue = ColorEditLastSat = 0.0f; ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX; SliderCurrentAccum = 0.0f; @@ -1644,6 +1781,7 @@ struct ImGuiContext DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; + DisabledAlphaBackup = 0.0f; ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; TooltipSlowDelay = 0.50f; @@ -1668,7 +1806,7 @@ struct ImGuiContext DebugItemPickerBreakId = 0; memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); - FramerateSecPerFrameIdx = 0; + FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; FramerateSecPerFrameAccum = 0.0f; WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; memset(TempBuffer, 0, sizeof(TempBuffer)); @@ -1698,16 +1836,10 @@ struct IMGUI_API ImGuiWindowTempData ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. ImVec1 GroupOffset; - // Last item status - ImGuiID LastItemId; // ID for last item - ImGuiItemStatusFlags LastItemStatusFlags; // Status flags for last item (see ImGuiItemStatusFlags_) - ImRect LastItemRect; // Interaction rect for last item - ImRect LastItemDisplayRect; // End-user display rect for last item (only valid if LastItemStatusFlags & ImGuiItemStatusFlags_HasDisplayRect) - // Keyboard/Gamepad navigation ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) - int NavLayerActiveMask; // Which layers have been written to (result from previous frame) - int NavLayerActiveMaskNext; // Which layers have been written to (accumulator for current frame) + short NavLayersActiveMask; // Which layers have been written to (result from previous frame) + short NavLayersActiveMaskNext;// Which layers have been written to (accumulator for current frame) ImGuiID NavFocusScopeIdCurrent; // Current focus scope ID while appending bool NavHideHighlightOneFrame; bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) @@ -1729,7 +1861,6 @@ struct IMGUI_API ImGuiWindowTempData // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. - ImGuiItemFlags ItemFlags; // == g.ItemFlagsStack.back() float ItemWidth; // Current item width (>0.0: width in pixels, <0.0: align xx pixels to the right of window). float TextWrapPos; // Current text wrap pos. ImVector ItemWidthStack; // Store item widths to restore (attention: .back() is not == ItemWidth) @@ -1774,8 +1905,9 @@ struct IMGUI_API ImGuiWindow bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) - short BeginOrderWithinParent; // Order within immediate parent window, if we are a child window. Otherwise 0. - short BeginOrderWithinContext; // Order within entire imgui context. This is mostly used for debugging submission order related issues. + short BeginOrderWithinParent; // Begin() order within immediate parent window, if we are a child window. Otherwise 0. + short BeginOrderWithinContext; // Begin() order within entire imgui context. This is mostly used for debugging submission order related issues. + short FocusOrder; // Order within WindowsFocusOrder[], altered when windows are focused. ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) ImS8 AutoFitFramesX, AutoFitFramesY; ImS8 AutoFitChildAxises; @@ -1784,6 +1916,7 @@ struct IMGUI_API ImGuiWindow ImS8 HiddenFramesCanSkipItems; // Hide the window for N frames ImS8 HiddenFramesCannotSkipItems; // Hide the window for N frames while allowing items to be submitted so we can measure their size ImS8 HiddenFramesForRenderOnly; // Hide the window until frame N at Render() time only + ImS8 DisableInputsFrames; // Disable window interactions for N frames ImGuiCond SetWindowPosAllowFlags : 8; // store acceptable condition flags for SetNextWindowPos() use. ImGuiCond SetWindowSizeAllowFlags : 8; // store acceptable condition flags for SetNextWindowSize() use. ImGuiCond SetWindowCollapsedAllowFlags : 8; // store acceptable condition flags for SetNextWindowCollapsed() use. @@ -1849,19 +1982,6 @@ struct IMGUI_API ImGuiWindow ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } }; -// Backup and restore just enough data to be able to use IsItemHovered() on item A after another B in the same window has overwritten the data. -struct ImGuiLastItemDataBackup -{ - ImGuiID LastItemId; - ImGuiItemStatusFlags LastItemStatusFlags; - ImRect LastItemRect; - ImRect LastItemDisplayRect; - - ImGuiLastItemDataBackup() { Backup(); } - void Backup() { ImGuiWindow* window = GImGui->CurrentWindow; LastItemId = window->DC.LastItemId; LastItemStatusFlags = window->DC.LastItemStatusFlags; LastItemRect = window->DC.LastItemRect; LastItemDisplayRect = window->DC.LastItemDisplayRect; } - void Restore() const { ImGuiWindow* window = GImGui->CurrentWindow; window->DC.LastItemId = LastItemId; window->DC.LastItemStatusFlags = LastItemStatusFlags; window->DC.LastItemRect = LastItemRect; window->DC.LastItemDisplayRect = LastItemDisplayRect; } -}; - //----------------------------------------------------------------------------- // [SECTION] Tab bar, Tab item support //----------------------------------------------------------------------------- @@ -1877,11 +1997,12 @@ enum ImGuiTabBarFlagsPrivate_ // Extend ImGuiTabItemFlags_ enum ImGuiTabItemFlagsPrivate_ { + ImGuiTabItemFlags_SectionMask_ = ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing, ImGuiTabItemFlags_NoCloseButton = 1 << 20, // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) ImGuiTabItemFlags_Button = 1 << 21 // Used by TabItemButton, change the tab item behavior to mimic a button }; -// Storage for one active tab item (sizeof() 28~32 bytes) +// Storage for one active tab item (sizeof() 40 bytes) struct ImGuiTabItem { ImGuiID ID; @@ -1891,12 +2012,12 @@ struct ImGuiTabItem float Offset; // Position relative to beginning of tab float Width; // Width currently displayed float ContentWidth; // Width of label, stored during BeginTabItem() call - ImS16 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames + ImS32 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames ImS16 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable ImS16 IndexDuringLayout; // Index only used during TabBarLayout() bool WantClose; // Marked as closed by SetTabItemClosed() - ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; NameOffset = BeginOrder = IndexDuringLayout = -1; } + ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; BeginOrder = IndexDuringLayout = -1; } }; // Storage for a tab bar (sizeof() 152 bytes) @@ -1922,7 +2043,7 @@ struct ImGuiTabBar float ScrollingRectMinX; float ScrollingRectMaxX; ImGuiID ReorderRequestTabId; - ImS8 ReorderRequestDir; + ImS16 ReorderRequestOffset; ImS8 BeginCount; bool WantLayout; bool VisibleTabWasSubmitted; @@ -1938,7 +2059,7 @@ struct ImGuiTabBar int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); } const char* GetTabName(const ImGuiTabItem* tab) const { - IM_ASSERT(tab->NameOffset != -1 && (int)tab->NameOffset < TabsNames.Buf.Size); + IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size); return TabsNames.Buf.Data + tab->NameOffset; } }; @@ -1947,8 +2068,6 @@ struct ImGuiTabBar // [SECTION] Table support //----------------------------------------------------------------------------- -#ifdef IMGUI_HAS_TABLE - #define IM_COL32_DISABLE IM_COL32(0,0,0,1) // Special sentinel code which cannot be used as a regular color. #define IMGUI_TABLE_MAX_COLUMNS 64 // sizeof(ImU64) * 8. This is solely because we frequently encode columns set in a ImU64. #define IMGUI_TABLE_MAX_DRAW_CHANNELS (4 + 64 * 2) // See TableSetupDrawChannels() @@ -1987,10 +2106,11 @@ struct ImGuiTableColumn ImGuiTableColumnIdx NextEnabledColumn; // Index of next enabled/visible column within Columns[], -1 if last enabled/visible column ImGuiTableColumnIdx SortOrder; // Index of this column within sort specs, -1 if not sorting on this column, 0 for single-sort, may be >0 on multi-sort ImGuiTableDrawChannelIdx DrawChannelCurrent; // Index within DrawSplitter.Channels[] - ImGuiTableDrawChannelIdx DrawChannelFrozen; - ImGuiTableDrawChannelIdx DrawChannelUnfrozen; - bool IsEnabled; // Is the column not marked Hidden by the user? (even if off view, e.g. clipped by scrolling). - bool IsEnabledNextFrame; + ImGuiTableDrawChannelIdx DrawChannelFrozen; // Draw channels for frozen rows (often headers) + ImGuiTableDrawChannelIdx DrawChannelUnfrozen; // Draw channels for unfrozen rows + bool IsEnabled; // IsUserEnabled && (Flags & ImGuiTableColumnFlags_Disabled) == 0 + bool IsUserEnabled; // Is the column not marked Hidden by the user? (unrelated to being off view, e.g. clipped by scrolling). + bool IsUserEnabledNextFrame; bool IsVisibleX; // Is actually in view (e.g. overlapping the host window clipping rectangle, not scrolled). bool IsVisibleY; bool IsRequestOutput; // Return value for TableSetColumnIndex() / TableNextColumn(): whether we request user to output contents or not. @@ -2025,12 +2145,13 @@ struct ImGuiTableCellData ImGuiTableColumnIdx Column; // Column number }; -// FIXME-TABLE: transient data could be stored in a per-stacked table structure: DrawSplitter, SortSpecs, incoming RowData +// FIXME-TABLE: more transient data could be stored in a per-stacked table structure: DrawSplitter, SortSpecs, incoming RowData struct ImGuiTable { ImGuiID ID; ImGuiTableFlags Flags; void* RawData; // Single allocation to hold Columns[], DisplayOrderToIndex[] and RowCellData[] + ImGuiTableTempData* TempData; // Transient data while table is active. Point within g.CurrentTableStack[] ImSpan Columns; // Point within RawData[] ImSpan DisplayOrderToIndex; // Point within RawData[]. Store display order of columns (when not reordered, the values are 0...Count-1) ImSpan RowCellData; // Point within RawData[]. Store cells background requests for current row. @@ -2082,22 +2203,13 @@ struct ImGuiTable ImRect Bg0ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG0/1 channel. This tends to be == OuterWindow->ClipRect at BeginTable() because output in BG0/BG1 is cpu-clipped ImRect Bg2ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG2 channel. This tends to be a correct, tight-fit, because output to BG2 are done by widgets relying on regular ClipRect. ImRect HostClipRect; // This is used to check if we can eventually merge our columns draw calls into the current draw call of the current window. - ImRect HostBackupWorkRect; // Backup of InnerWindow->WorkRect at the end of BeginTable() - ImRect HostBackupParentWorkRect; // Backup of InnerWindow->ParentWorkRect at the end of BeginTable() ImRect HostBackupInnerClipRect; // Backup of InnerWindow->ClipRect during PushTableBackground()/PopTableBackground() - ImVec2 HostBackupPrevLineSize; // Backup of InnerWindow->DC.PrevLineSize at the end of BeginTable() - ImVec2 HostBackupCurrLineSize; // Backup of InnerWindow->DC.CurrLineSize at the end of BeginTable() - ImVec2 HostBackupCursorMaxPos; // Backup of InnerWindow->DC.CursorMaxPos at the end of BeginTable() - ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() - ImVec1 HostBackupColumnsOffset; // Backup of OuterWindow->DC.ColumnsOffset at the end of BeginTable() - float HostBackupItemWidth; // Backup of OuterWindow->DC.ItemWidth at the end of BeginTable() - int HostBackupItemWidthStackSize;// Backup of OuterWindow->DC.ItemWidthStack.Size at the end of BeginTable() ImGuiWindow* OuterWindow; // Parent window for the table ImGuiWindow* InnerWindow; // Window holding the table data (== OuterWindow or a child window) ImGuiTextBuffer ColumnsNames; // Contiguous buffer holding columns names - ImDrawListSplitter DrawSplitter; // We carry our own ImDrawList splitter to allow recursion (FIXME: could be stored outside, worst case we need 1 splitter per recursing table) + ImDrawListSplitter* DrawSplitter; // Shortcut to TempData->DrawSplitter while in table. Isolate draw commands per columns to avoid switching clip rect constantly ImGuiTableColumnSortSpecs SortSpecsSingle; - ImVector SortSpecsMulti; // FIXME-OPT: Using a small-vector pattern would work be good. + ImVector SortSpecsMulti; // FIXME-OPT: Using a small-vector pattern would be good. ImGuiTableSortSpecs SortSpecs; // Public facing sorts specs, this is what we return in TableGetSortSpecs() ImGuiTableColumnIdx SortSpecsCount; ImGuiTableColumnIdx ColumnsEnabledCount; // Number of enabled columns (<= ColumnsCount) @@ -2144,6 +2256,29 @@ struct ImGuiTable IMGUI_API ~ImGuiTable() { IM_FREE(RawData); } }; +// Transient data that are only needed between BeginTable() and EndTable(), those buffers are shared (1 per level of stacked table). +// - Accessing those requires chasing an extra pointer so for very frequently used data we leave them in the main table structure. +// - We also leave out of this structure data that tend to be particularly useful for debugging/metrics. +struct ImGuiTableTempData +{ + int TableIndex; // Index in g.Tables.Buf[] pool + float LastTimeActive; // Last timestamp this structure was used + + ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() + ImDrawListSplitter DrawSplitter; + + ImRect HostBackupWorkRect; // Backup of InnerWindow->WorkRect at the end of BeginTable() + ImRect HostBackupParentWorkRect; // Backup of InnerWindow->ParentWorkRect at the end of BeginTable() + ImVec2 HostBackupPrevLineSize; // Backup of InnerWindow->DC.PrevLineSize at the end of BeginTable() + ImVec2 HostBackupCurrLineSize; // Backup of InnerWindow->DC.CurrLineSize at the end of BeginTable() + ImVec2 HostBackupCursorMaxPos; // Backup of InnerWindow->DC.CursorMaxPos at the end of BeginTable() + ImVec1 HostBackupColumnsOffset; // Backup of OuterWindow->DC.ColumnsOffset at the end of BeginTable() + float HostBackupItemWidth; // Backup of OuterWindow->DC.ItemWidth at the end of BeginTable() + int HostBackupItemWidthStackSize;//Backup of OuterWindow->DC.ItemWidthStack.Size at the end of BeginTable() + + IMGUI_API ImGuiTableTempData() { memset(this, 0, sizeof(*this)); LastTimeActive = -1.0f; } +}; + // sizeof() ~ 12 struct ImGuiTableColumnSettings { @@ -2182,8 +2317,6 @@ struct ImGuiTableSettings ImGuiTableColumnSettings* GetColumnSettings() { return (ImGuiTableColumnSettings*)(this + 1); } }; -#endif // #ifdef IMGUI_HAS_TABLE - //----------------------------------------------------------------------------- // [SECTION] ImGui internal API // No guarantee of forward compatibility here! @@ -2205,7 +2338,6 @@ namespace ImGui IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); IMGUI_API bool IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); - IMGUI_API ImRect GetWindowAllowedExtentRect(ImGuiWindow* window); IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); @@ -2258,11 +2390,11 @@ namespace ImGui IMGUI_API ImVec2 ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect); // Basic Accessors - inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemId; } // Get ID of last item (~~ often same ImGui::GetID(label) beforehand) - inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.LastItemStatusFlags; } + inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.LastItemData.ID; } // Get ID of last item (~~ often same ImGui::GetID(label) beforehand) + inline ImGuiItemStatusFlags GetItemStatusFlags(){ ImGuiContext& g = *GImGui; return g.LastItemData.StatusFlags; } + inline ImGuiItemFlags GetItemFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.InFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } - inline ImGuiItemFlags GetItemsFlags() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.ItemFlags; } IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); IMGUI_API void SetFocusID(ImGuiID id, ImGuiWindow* window); IMGUI_API void ClearActiveID(); @@ -2276,21 +2408,30 @@ namespace ImGui // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f); - IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL); + IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemAddFlags flags = 0); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); + IMGUI_API void ItemFocusable(ImGuiWindow* window, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); - IMGUI_API void SetLastItemData(ImGuiWindow* window, ImGuiID item_id, ImGuiItemStatusFlags status_flags, const ImRect& item_rect); - IMGUI_API bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id); // Return true if focus is requested - IMGUI_API void FocusableItemUnregister(ImGuiWindow* window); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); IMGUI_API void PushMultiItemsWidths(int components, float width_full); - IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); - IMGUI_API void PopItemFlag(); IMGUI_API bool IsItemToggledSelection(); // Was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly) IMGUI_API ImVec2 GetContentRegionMaxAbs(); IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess); + // Parameter stacks + IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); + IMGUI_API void PopItemFlag(); + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): + // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool focused = FocusableItemRegister(...)' + // (New) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' + // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() + inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Focusable flag to ItemAdd() + inline IM_NORETURN void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem +#endif + // Logging/Capture IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer @@ -2305,9 +2446,19 @@ namespace ImGui IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); + IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); + IMGUI_API bool BeginViewportSideBar(const char* name, ImGuiViewport* viewport, ImGuiDir dir, float size, ImGuiWindowFlags window_flags); + + // Menus + IMGUI_API bool MenuItemEx(const char* label, const char* icon, const char* shortcut = NULL, bool selected = false, bool enabled = true); + + // Combos + IMGUI_API bool BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags flags); + IMGUI_API bool BeginComboPreview(); + IMGUI_API void EndComboPreview(); // Gamepad/Keyboard Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); @@ -2319,7 +2470,7 @@ namespace ImGui IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. - IMGUI_API void SetNavID(ImGuiID id, int nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); + IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); // Focus Scope (WIP) // This is generally used to identify a selection set (multiple of which may be in the same window), as selection @@ -2332,6 +2483,7 @@ namespace ImGui // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. IMGUI_API void SetItemUsingMouseWheel(); + IMGUI_API void SetActiveIdUsingNavAndKeys(); inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; } inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; } @@ -2360,7 +2512,6 @@ namespace ImGui // Tables: Candidates for public API IMGUI_API void TableOpenContextMenu(int column_n = -1); - IMGUI_API void TableSetColumnEnabled(int column_n, bool enabled); IMGUI_API void TableSetColumnWidth(int column_n, float width); IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs); IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. @@ -2398,6 +2549,7 @@ namespace ImGui IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table); IMGUI_API void TableRemove(ImGuiTable* table); IMGUI_API void TableGcCompactTransientBuffers(ImGuiTable* table); + IMGUI_API void TableGcCompactTransientBuffers(ImGuiTableTempData* table); IMGUI_API void TableGcCompactSettings(); // Tables: Settings @@ -2414,7 +2566,8 @@ namespace ImGui IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); - IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir); + IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int offset); + IMGUI_API void TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, ImVec2 mouse_pos); IMGUI_API bool TabBarProcessReorder(ImGuiTabBar* tab_bar); IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); @@ -2461,7 +2614,8 @@ namespace ImGui IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col); IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); - IMGUI_API ImGuiID GetWindowResizeID(ImGuiWindow* window, int n); // 0..3: corners, 4..7: borders + IMGUI_API ImGuiID GetWindowResizeCornerID(ImGuiWindow* window, int n); // 0..3: corners + IMGUI_API ImGuiID GetWindowResizeBorderID(ImGuiWindow* window, ImGuiDir dir); IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); @@ -2519,12 +2673,14 @@ namespace ImGui // Debug Tools IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); - inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(window->DC.LastItemRect.Min, window->DC.LastItemRect.Max, col); } + inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, col); } inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } + IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); + IMGUI_API void DebugNodeFont(ImFont* font); IMGUI_API void DebugNodeStorage(ImGuiStorage* storage, const char* label); IMGUI_API void DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label); IMGUI_API void DebugNodeTable(ImGuiTable* table); @@ -2572,14 +2728,10 @@ extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) #define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log -#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) if (g.TestEngineHookIdInfo == id) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA)); -#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) if (g.TestEngineHookIdInfo == id) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA), (const void*)(_DATA2)); +#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) if (g.TestEngineHookIdInfo == _ID) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA)); +#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) if (g.TestEngineHookIdInfo == _ID) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA), (const void*)(_DATA2)); #else -#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) do { } while (0) -#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) do { } while (0) -#define IMGUI_TEST_ENGINE_LOG(_FMT,...) do { } while (0) -#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) do { } while (0) -#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) do { } while (0) +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)0) #endif //----------------------------------------------------------------------------- diff --git a/imgui/imgui_tables.cpp b/imgui/imgui_tables.cpp index edeef738..71ac00f6 100644 --- a/imgui/imgui_tables.cpp +++ b/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (tables and columns code) /* @@ -8,6 +8,7 @@ Index of this file: // [SECTION] Commentary // [SECTION] Header mess // [SECTION] Tables: Main code +// [SECTION] Tables: Simple accessors // [SECTION] Tables: Row changes // [SECTION] Tables: Columns changes // [SECTION] Tables: Columns width management @@ -73,7 +74,7 @@ Index of this file: // (Read carefully because this is subtle but it does make sense!) //----------------------------------------------------------------------------- // About 'outer_size': -// Its meaning needs to differ slightly depending of if we are using ScrollX/ScrollY flags. +// Its meaning needs to differ slightly depending on if we are using ScrollX/ScrollY flags. // Default value is ImVec2(0.0f, 0.0f). // X // - outer_size.x <= 0.0f -> Right-align from window/work-rect right-most edge. With -FLT_MIN or 0.0f will align exactly on right-most edge. @@ -90,7 +91,7 @@ Index of this file: // Outer size is also affected by the NoHostExtendX/NoHostExtendY flags. // Important to that note how the two flags have slightly different behaviors! // - ImGuiTableFlags_NoHostExtendX -> Make outer width auto-fit to columns (overriding outer_size.x value). Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. -// - ImGuiTableFlags_NoHostExtendY -> Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible. +// - ImGuiTableFlags_NoHostExtendY -> Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY is disabled. Data below the limit will be clipped and not visible. // In theory ImGuiTableFlags_NoHostExtendY could be the default and any non-scrolling tables with outer_size.y != 0.0f would use exact height. // This would be consistent but perhaps less useful and more confusing (as vertically clipped items are not easily noticeable) //----------------------------------------------------------------------------- @@ -132,7 +133,7 @@ Index of this file: // - the typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. // - using mixed policies with ScrollX does not make much sense, as using Stretch columns with ScrollX does not make much sense in the first place! // that is, unless 'inner_width' is passed to BeginTable() to explicitly provide a total width to layout columns in. -// - when using ImGuiTableFlags_SizingFixedSame with mixed columns, only the Fixed/Auto columns will match their widths to the maximum contents width. +// - when using ImGuiTableFlags_SizingFixedSame with mixed columns, only the Fixed/Auto columns will match their widths to the width of the maximum contents. // - when using ImGuiTableFlags_SizingStretchSame with mixed columns, only the Stretch columns will match their weight/widths. //----------------------------------------------------------------------------- // About using column width: @@ -140,9 +141,9 @@ Index of this file: // - you may use GetContentRegionAvail().x to query the width available in a given column. // - right-side alignment features such as SetNextItemWidth(-x) or PushItemWidth(-x) will rely on this width. // If the column is not resizable and has no width specified with TableSetupColumn(): -// - its width will be automatic and be the set to the max of items submitted. +// - its width will be automatic and be set to the max of items submitted. // - therefore you generally cannot have ALL items of the columns use e.g. SetNextItemWidth(-FLT_MIN). -// - but if the column has one or more item of known/fixed size, this will become the reference width used by SetNextItemWidth(-FLT_MIN). +// - but if the column has one or more items of known/fixed size, this will become the reference width used by SetNextItemWidth(-FLT_MIN). //----------------------------------------------------------------------------- @@ -161,7 +162,7 @@ Index of this file: // - Both TableSetColumnIndex() and TableNextColumn() return true when the column is visible or performing // width measurements. Otherwise, you may skip submitting the contents of a cell/column, BUT ONLY if you know // it is not going to contribute to row height. -// In many situations, you may skip submitting contents for every columns but one (e.g. the first one). +// In many situations, you may skip submitting contents for every column but one (e.g. the first one). // - Case A: column is not hidden by user, and at least partially in sight (most common case). // - Case B: column is clipped / out of sight (because of scrolling or parent ClipRect): TableNextColumn() return false as a hint but we still allow layout output. // - Case C: column is hidden explicitly by the user (e.g. via the context menu, or _DefaultHide column flag, etc.). @@ -172,7 +173,7 @@ Index of this file: // ClipRect: normal zero-width zero-width -> [internal] when ClipRect is zero, ItemAdd() will return false and most widgets will early out mid-way. // ImDrawList output: normal dummy dummy -> [internal] when using the dummy channel, ImDrawList submissions (if any) will be wasted (because cliprect is zero-width anyway). // -// - We need distinguish those cases because non-hidden columns that are clipped outside of scrolling bounds should still contribute their height to the row. +// - We need to distinguish those cases because non-hidden columns that are clipped outside of scrolling bounds should still contribute their height to the row. // However, in the majority of cases, the contribution to row height is the same for all columns, or the tallest cells are known by the programmer. //----------------------------------------------------------------------------- // About clipping/culling of whole Tables: @@ -209,6 +210,8 @@ Index of this file: #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types #endif +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). #endif // Clang/GCC warnings with -Weverything @@ -235,6 +238,19 @@ Index of this file: //----------------------------------------------------------------------------- // [SECTION] Tables: Main code //----------------------------------------------------------------------------- +// - TableFixFlags() [Internal] +// - TableFindByID() [Internal] +// - BeginTable() +// - BeginTableEx() [Internal] +// - TableBeginInitMemory() [Internal] +// - TableBeginApplyRequests() [Internal] +// - TableSetupColumnFlags() [Internal] +// - TableUpdateLayout() [Internal] +// - TableUpdateBorders() [Internal] +// - EndTable() +// - TableSetupColumn() +// - TableSetupScrollFreeze() +//----------------------------------------------------------------------------- // Configuration static const int TABLE_DRAW_CHANNEL_BG0 = 0; @@ -272,12 +288,7 @@ inline ImGuiTableFlags TableFixFlags(ImGuiTableFlags flags, ImGuiWindow* outer_w flags |= ImGuiTableFlags_NoSavedSettings; // Inherit _NoSavedSettings from top-level window (child windows always have _NoSavedSettings set) -#ifdef IMGUI_HAS_DOCK - ImGuiWindow* window_for_settings = outer_window->RootWindowDockStop; -#else - ImGuiWindow* window_for_settings = outer_window->RootWindow; -#endif - if (window_for_settings->Flags & ImGuiWindowFlags_NoSavedSettings) + if (outer_window->RootWindow->Flags & ImGuiWindowFlags_NoSavedSettings) flags |= ImGuiTableFlags_NoSavedSettings; return flags; @@ -327,6 +338,16 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (instance_no > 0) IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID"); + // Acquire temporary buffers + const int table_idx = g.Tables.GetIndex(table); + g.CurrentTableStackIdx++; + if (g.CurrentTableStackIdx + 1 > g.TablesTempDataStack.Size) + g.TablesTempDataStack.resize(g.CurrentTableStackIdx + 1, ImGuiTableTempData()); + ImGuiTableTempData* temp_data = table->TempData = &g.TablesTempDataStack[g.CurrentTableStackIdx]; + temp_data->TableIndex = table_idx; + table->DrawSplitter = &table->TempData->DrawSplitter; + table->DrawSplitter->Clear(); + // Fix flags table->IsDefaultSizingPolicy = (flags & ImGuiTableFlags_SizingMask_) == 0; flags = TableFixFlags(flags, outer_window); @@ -340,7 +361,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->ColumnsCount = columns_count; table->IsLayoutLocked = false; table->InnerWidth = inner_width; - table->UserOuterSize = outer_size; + temp_data->UserOuterSize = outer_size; // When not using a child window, WorkRect.Max will grow as we append contents. if (use_child_window) @@ -389,14 +410,14 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->HostIndentX = inner_window->DC.Indent.x; table->HostClipRect = inner_window->ClipRect; table->HostSkipItems = inner_window->SkipItems; - table->HostBackupWorkRect = inner_window->WorkRect; - table->HostBackupParentWorkRect = inner_window->ParentWorkRect; - table->HostBackupColumnsOffset = outer_window->DC.ColumnsOffset; - table->HostBackupPrevLineSize = inner_window->DC.PrevLineSize; - table->HostBackupCurrLineSize = inner_window->DC.CurrLineSize; - table->HostBackupCursorMaxPos = inner_window->DC.CursorMaxPos; - table->HostBackupItemWidth = outer_window->DC.ItemWidth; - table->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size; + temp_data->HostBackupWorkRect = inner_window->WorkRect; + temp_data->HostBackupParentWorkRect = inner_window->ParentWorkRect; + temp_data->HostBackupColumnsOffset = outer_window->DC.ColumnsOffset; + temp_data->HostBackupPrevLineSize = inner_window->DC.PrevLineSize; + temp_data->HostBackupCurrLineSize = inner_window->DC.CurrLineSize; + temp_data->HostBackupCursorMaxPos = inner_window->DC.CursorMaxPos; + temp_data->HostBackupItemWidth = outer_window->DC.ItemWidth; + temp_data->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size; inner_window->DC.PrevLineSize = inner_window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); // Padding and Spacing @@ -439,8 +460,6 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->BorderColorLight = GetColorU32(ImGuiCol_TableBorderLight); // Make table current - const int table_idx = g.Tables.GetIndex(table); - g.CurrentTableStack.push_back(ImGuiPtrOrIndex(table_idx)); g.CurrentTable = table; outer_window->DC.CurrentTableIdx = table_idx; if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly. @@ -453,13 +472,18 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (table_idx >= g.TablesLastTimeActive.Size) g.TablesLastTimeActive.resize(table_idx + 1, -1.0f); g.TablesLastTimeActive[table_idx] = (float)g.Time; + temp_data->LastTimeActive = (float)g.Time; table->MemoryCompacted = false; // Setup memory buffer (clear data if columns count changed) - const int stored_size = table->Columns.size(); - if (stored_size != 0 && stored_size != columns_count) - { - IM_FREE(table->RawData); + ImGuiTableColumn* old_columns_to_preserve = NULL; + void* old_columns_raw_data = NULL; + const int old_columns_count = table->Columns.size(); + if (old_columns_count != 0 && old_columns_count != columns_count) + { + // Attempt to preserve width on column count change (#4046) + old_columns_to_preserve = table->Columns.Data; + old_columns_raw_data = table->RawData; table->RawData = NULL; } if (table->RawData == NULL) @@ -482,14 +506,24 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG for (int n = 0; n < columns_count; n++) { ImGuiTableColumn* column = &table->Columns[n]; - float width_auto = column->WidthAuto; - *column = ImGuiTableColumn(); - column->WidthAuto = width_auto; - column->IsPreserveWidthAuto = true; // Preserve WidthAuto when reinitializing a live table: not technically necessary but remove a visible flicker + if (old_columns_to_preserve && n < old_columns_count) + { + // FIXME: We don't attempt to preserve column order in this path. + *column = old_columns_to_preserve[n]; + } + else + { + float width_auto = column->WidthAuto; + *column = ImGuiTableColumn(); + column->WidthAuto = width_auto; + column->IsPreserveWidthAuto = true; // Preserve WidthAuto when reinitializing a live table: not technically necessary but remove a visible flicker + column->IsEnabled = column->IsUserEnabled = column->IsUserEnabledNextFrame = true; + } column->DisplayOrder = table->DisplayOrderToIndex[n] = (ImGuiTableColumnIdx)n; - column->IsEnabled = column->IsEnabledNextFrame = true; } } + if (old_columns_raw_data) + IM_FREE(old_columns_raw_data); // Load settings if (table->IsSettingsRequestLoad) @@ -717,16 +751,18 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) column->InitStretchWeightOrWidth = -1.0f; } - // Update Enabled state, mark settings/sortspecs dirty + // Update Enabled state, mark settings and sort specs dirty if (!(table->Flags & ImGuiTableFlags_Hideable) || (column->Flags & ImGuiTableColumnFlags_NoHide)) - column->IsEnabledNextFrame = true; - if (column->IsEnabled != column->IsEnabledNextFrame) + column->IsUserEnabledNextFrame = true; + if (column->IsUserEnabled != column->IsUserEnabledNextFrame) { - column->IsEnabled = column->IsEnabledNextFrame; + column->IsUserEnabled = column->IsUserEnabledNextFrame; table->IsSettingsDirty = true; - if (!column->IsEnabled && column->SortOrder != -1) - table->IsSortSpecsDirty = true; } + column->IsEnabled = column->IsUserEnabled && (column->Flags & ImGuiTableColumnFlags_Disabled) == 0; + + if (column->SortOrder != -1 && !column->IsEnabled) + table->IsSortSpecsDirty = true; if (column->SortOrder > 0 && !(table->Flags & ImGuiTableFlags_SortMulti)) table->IsSortSpecsDirty = true; @@ -1087,7 +1123,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // Initial state ImGuiWindow* inner_window = table->InnerWindow; if (table->Flags & ImGuiTableFlags_NoClip) - table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); + table->DrawSplitter->SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); else inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false); } @@ -1125,9 +1161,8 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) if ((table->Flags & ImGuiTableFlags_NoBordersInBody) && table->IsUsingHeaders == false) continue; - if (table->FreezeColumnsCount > 0) - if (column->MaxX < table->Columns[table->DisplayOrderToIndex[table->FreezeColumnsCount - 1]].MaxX) - continue; + if (!column->IsVisibleX && table->LastResizedColumn != column_n) + continue; ImGuiID column_id = TableGetColumnResizeID(table, column_n, table->InstanceCurrent); ImRect hit_rect(column->MaxX - hit_half_width, hit_y1, column->MaxX + hit_half_width, border_y2_hit); @@ -1175,6 +1210,7 @@ void ImGui::EndTable() const ImGuiTableFlags flags = table->Flags; ImGuiWindow* inner_window = table->InnerWindow; ImGuiWindow* outer_window = table->OuterWindow; + ImGuiTableTempData* temp_data = table->TempData; IM_ASSERT(inner_window == g.CurrentWindow); IM_ASSERT(outer_window == inner_window || outer_window == inner_window->ParentWindow); @@ -1187,9 +1223,9 @@ void ImGui::EndTable() TableOpenContextMenu((int)table->HoveredColumnBody); // Finalize table height - inner_window->DC.PrevLineSize = table->HostBackupPrevLineSize; - inner_window->DC.CurrLineSize = table->HostBackupCurrLineSize; - inner_window->DC.CursorMaxPos = table->HostBackupCursorMaxPos; + inner_window->DC.PrevLineSize = temp_data->HostBackupPrevLineSize; + inner_window->DC.CurrLineSize = temp_data->HostBackupCurrLineSize; + inner_window->DC.CursorMaxPos = temp_data->HostBackupCursorMaxPos; const float inner_content_max_y = table->RowPosY2; IM_ASSERT(table->RowPosY2 == inner_window->DC.CursorPos.y); if (inner_window != outer_window) @@ -1236,10 +1272,11 @@ void ImGui::EndTable() #endif // Flatten channels and merge draw calls - table->DrawSplitter.SetCurrentChannel(inner_window->DrawList, 0); + ImDrawListSplitter* splitter = table->DrawSplitter; + splitter->SetCurrentChannel(inner_window->DrawList, 0); if ((table->Flags & ImGuiTableFlags_NoClip) == 0) TableMergeDrawChannels(table); - table->DrawSplitter.Merge(inner_window->DrawList); + splitter->Merge(inner_window->DrawList); // Update ColumnsAutoFitWidth to get us ahead for host using our size to auto-resize without waiting for next BeginTable() const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); @@ -1281,18 +1318,18 @@ void ImGui::EndTable() // Pop from id stack IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table->ID + table->InstanceCurrent, "Mismatching PushID/PopID!"); - IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= table->HostBackupItemWidthStackSize, "Too many PopItemWidth!"); + IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= temp_data->HostBackupItemWidthStackSize, "Too many PopItemWidth!"); PopID(); // Restore window data that we modified const ImVec2 backup_outer_max_pos = outer_window->DC.CursorMaxPos; - inner_window->WorkRect = table->HostBackupWorkRect; - inner_window->ParentWorkRect = table->HostBackupParentWorkRect; + inner_window->WorkRect = temp_data->HostBackupWorkRect; + inner_window->ParentWorkRect = temp_data->HostBackupParentWorkRect; inner_window->SkipItems = table->HostSkipItems; outer_window->DC.CursorPos = table->OuterRect.Min; - outer_window->DC.ItemWidth = table->HostBackupItemWidth; - outer_window->DC.ItemWidthStack.Size = table->HostBackupItemWidthStackSize; - outer_window->DC.ColumnsOffset = table->HostBackupColumnsOffset; + outer_window->DC.ItemWidth = temp_data->HostBackupItemWidth; + outer_window->DC.ItemWidthStack.Size = temp_data->HostBackupItemWidthStackSize; + outer_window->DC.ColumnsOffset = temp_data->HostBackupColumnsOffset; // Layout in outer window // (FIXME: To allow auto-fit and allow desirable effect of SameLine() we dissociate 'used' vs 'ideal' size by overriding @@ -1315,20 +1352,20 @@ void ImGui::EndTable() IM_ASSERT((table->Flags & ImGuiTableFlags_ScrollX) == 0); outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth); } - else if (table->UserOuterSize.x <= 0.0f) + else if (temp_data->UserOuterSize.x <= 0.0f) { const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f; - outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - table->UserOuterSize.x); + outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - temp_data->UserOuterSize.x); outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth)); } else { outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, table->OuterRect.Max.x); } - if (table->UserOuterSize.y <= 0.0f) + if (temp_data->UserOuterSize.y <= 0.0f) { const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollY) ? inner_window->ScrollbarSizes.y : 0.0f; - outer_window->DC.IdealMaxPos.y = ImMax(outer_window->DC.IdealMaxPos.y, inner_content_max_y + decoration_size - table->UserOuterSize.y); + outer_window->DC.IdealMaxPos.y = ImMax(outer_window->DC.IdealMaxPos.y, inner_content_max_y + decoration_size - temp_data->UserOuterSize.y); outer_window->DC.CursorMaxPos.y = ImMax(backup_outer_max_pos.y, ImMin(table->OuterRect.Max.y, inner_content_max_y)); } else @@ -1344,8 +1381,15 @@ void ImGui::EndTable() // Clear or restore current table, if any IM_ASSERT(g.CurrentWindow == outer_window && g.CurrentTable == table); - g.CurrentTableStack.pop_back(); - g.CurrentTable = g.CurrentTableStack.Size ? g.Tables.GetByIndex(g.CurrentTableStack.back().Index) : NULL; + IM_ASSERT(g.CurrentTableStackIdx >= 0); + g.CurrentTableStackIdx--; + temp_data = g.CurrentTableStackIdx >= 0 ? &g.TablesTempDataStack[g.CurrentTableStackIdx] : NULL; + g.CurrentTable = temp_data ? g.Tables.GetByIndex(temp_data->TableIndex) : NULL; + if (g.CurrentTable) + { + g.CurrentTable->TempData = temp_data; + g.CurrentTable->DrawSplitter = &temp_data->DrawSplitter; + } outer_window->DC.CurrentTableIdx = g.CurrentTable ? g.Tables.GetIndex(g.CurrentTable) : -1; } @@ -1401,7 +1445,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo // Init default visibility/sort state if ((flags & ImGuiTableColumnFlags_DefaultHide) && (table->SettingsLoadedFlags & ImGuiTableFlags_Hideable) == 0) - column->IsEnabled = column->IsEnabledNextFrame = false; + column->IsUserEnabled = column->IsUserEnabledNextFrame = false; if (flags & ImGuiTableColumnFlags_DefaultSort && (table->SettingsLoadedFlags & ImGuiTableFlags_Sortable) == 0) { column->SortOrder = 0; // Multiple columns using _DefaultSort will be reassigned unique SortOrder values when building the sort specs. @@ -1428,13 +1472,38 @@ void ImGui::TableSetupScrollFreeze(int columns, int rows) IM_ASSERT(columns >= 0 && columns < IMGUI_TABLE_MAX_COLUMNS); IM_ASSERT(rows >= 0 && rows < 128); // Arbitrary limit - table->FreezeColumnsRequest = (table->Flags & ImGuiTableFlags_ScrollX) ? (ImGuiTableColumnIdx)columns : 0; + table->FreezeColumnsRequest = (table->Flags & ImGuiTableFlags_ScrollX) ? (ImGuiTableColumnIdx)ImMin(columns, table->ColumnsCount) : 0; table->FreezeColumnsCount = (table->InnerWindow->Scroll.x != 0.0f) ? table->FreezeColumnsRequest : 0; table->FreezeRowsRequest = (table->Flags & ImGuiTableFlags_ScrollY) ? (ImGuiTableColumnIdx)rows : 0; table->FreezeRowsCount = (table->InnerWindow->Scroll.y != 0.0f) ? table->FreezeRowsRequest : 0; table->IsUnfrozenRows = (table->FreezeRowsCount == 0); // Make sure this is set before TableUpdateLayout() so ImGuiListClipper can benefit from it.b + + // Ensure frozen columns are ordered in their section. We still allow multiple frozen columns to be reordered. + for (int column_n = 0; column_n < table->FreezeColumnsRequest; column_n++) + { + int order_n = table->DisplayOrderToIndex[column_n]; + if (order_n != column_n && order_n >= table->FreezeColumnsRequest) + { + ImSwap(table->Columns[table->DisplayOrderToIndex[order_n]].DisplayOrder, table->Columns[table->DisplayOrderToIndex[column_n]].DisplayOrder); + ImSwap(table->DisplayOrderToIndex[order_n], table->DisplayOrderToIndex[column_n]); + } + } } +//----------------------------------------------------------------------------- +// [SECTION] Tables: Simple accessors +//----------------------------------------------------------------------------- +// - TableGetColumnCount() +// - TableGetColumnName() +// - TableGetColumnName() [Internal] +// - TableSetColumnEnabled() +// - TableGetColumnFlags() +// - TableGetCellBgRect() [Internal] +// - TableGetColumnResizeID() [Internal] +// - TableGetHoveredColumn() [Internal] +// - TableSetBgColor() +//----------------------------------------------------------------------------- + int ImGui::TableGetColumnCount() { ImGuiContext& g = *GImGui; @@ -1463,7 +1532,12 @@ const char* ImGui::TableGetColumnName(const ImGuiTable* table, int column_n) return &table->ColumnsNames.Buf[column->NameOffset]; } -// For the getter you can use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsEnabled) +// Change user accessible enabled/disabled state of a column (often perceived as "showing/hiding" from users point of view) +// Note that end-user can use the context menu to change this themselves (right-click in headers, or right-click in columns body with ImGuiTableFlags_ContextMenuInBody) +// - Require table to have the ImGuiTableFlags_Hideable flag because we are manipulating user accessible state. +// - Request will be applied during next layout, which happens on the first call to TableNextRow() after BeginTable(). +// - For the getter you can test (TableGetColumnFlags() & ImGuiTableColumnFlags_IsEnabled) != 0. +// - Alternative: the ImGuiTableColumnFlags_Disabled is an overriding/master disable flag which will also hide the column from context menu. void ImGui::TableSetColumnEnabled(int column_n, bool enabled) { ImGuiContext& g = *GImGui; @@ -1471,11 +1545,12 @@ void ImGui::TableSetColumnEnabled(int column_n, bool enabled) IM_ASSERT(table != NULL); if (!table) return; + IM_ASSERT(table->Flags & ImGuiTableFlags_Hideable); // See comments above if (column_n < 0) column_n = table->CurrentColumn; IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount); ImGuiTableColumn* column = &table->Columns[column_n]; - column->IsEnabledNextFrame = enabled; + column->IsUserEnabledNextFrame = enabled; } // We allow querying for an extra column in order to poll the IsHovered state of the right-most section @@ -1701,7 +1776,7 @@ void ImGui::TableEndRow(ImGuiTable* table) // always followed by a change of clipping rectangle we perform the smallest overwrite possible here. if ((table->Flags & ImGuiTableFlags_NoClip) == 0) window->DrawList->_CmdHeader.ClipRect = table->Bg0ClipRectForDrawCmd.ToVec4(); - table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_BG0); + table->DrawSplitter->SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_BG0); } // Draw row background @@ -1773,7 +1848,7 @@ void ImGui::TableEndRow(ImGuiTable* table) // Update cliprect ahead of TableBeginCell() so clipper can access to new ClipRect->Min.y SetWindowClipRectBeforeSetChannel(window, table->Columns[0].ClipRect); - table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent); + table->DrawSplitter->SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent); } if (!(table->RowFlags & ImGuiTableRowFlags_Headers)) @@ -1881,21 +1956,22 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n) window->SkipItems = column->IsSkipItems; if (column->IsSkipItems) { - window->DC.LastItemId = 0; - window->DC.LastItemStatusFlags = 0; + ImGuiContext& g = *GImGui; + g.LastItemData.ID = 0; + g.LastItemData.StatusFlags = 0; } if (table->Flags & ImGuiTableFlags_NoClip) { // FIXME: if we end up drawing all borders/bg in EndTable, could remove this and just assert that channel hasn't changed. - table->DrawSplitter.SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); + table->DrawSplitter->SetCurrentChannel(window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); //IM_ASSERT(table->DrawSplitter._Current == TABLE_DRAW_CHANNEL_NOCLIP); } else { // FIXME-TABLE: Could avoid this if draw channel is dummy channel? SetWindowClipRectBeforeSetChannel(window, column->ClipRect); - table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); + table->DrawSplitter->SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); } // Logging @@ -1948,6 +2024,7 @@ float ImGui::TableGetMaxColumnWidth(const ImGuiTable* table, int column_n) if (table->Flags & ImGuiTableFlags_ScrollX) { // Frozen columns can't reach beyond visible width else scrolling will naturally break. + // (we use DisplayOrder as within a set of multiple frozen column reordering is possible) if (column->DisplayOrder < table->FreezeColumnsRequest) { max_width = (table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - column->DisplayOrder) * min_column_distance) - column->MinX; @@ -2143,7 +2220,7 @@ void ImGui::TablePushBackgroundChannel() // Optimization: avoid SetCurrentChannel() + PushClipRect() table->HostBackupInnerClipRect = window->ClipRect; SetWindowClipRectBeforeSetChannel(window, table->Bg2ClipRectForDrawCmd); - table->DrawSplitter.SetCurrentChannel(window->DrawList, table->Bg2DrawChannelCurrent); + table->DrawSplitter->SetCurrentChannel(window->DrawList, table->Bg2DrawChannelCurrent); } void ImGui::TablePopBackgroundChannel() @@ -2155,7 +2232,7 @@ void ImGui::TablePopBackgroundChannel() // Optimization: avoid PopClipRect() + SetCurrentChannel() SetWindowClipRectBeforeSetChannel(window, table->HostBackupInnerClipRect); - table->DrawSplitter.SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); + table->DrawSplitter->SetCurrentChannel(window->DrawList, column->DrawChannelCurrent); } // Allocate draw channels. Called by TableUpdateLayout() @@ -2181,7 +2258,7 @@ void ImGui::TableSetupDrawChannels(ImGuiTable* table) const int channels_for_bg = 1 + 1 * freeze_row_multiplier; const int channels_for_dummy = (table->ColumnsEnabledCount < table->ColumnsCount || table->VisibleMaskByIndex != table->EnabledMaskByIndex) ? +1 : 0; const int channels_total = channels_for_bg + (channels_for_row * freeze_row_multiplier) + channels_for_dummy; - table->DrawSplitter.Split(table->InnerWindow->DrawList, channels_total); + table->DrawSplitter->Split(table->InnerWindow->DrawList, channels_total); table->DummyDrawChannel = (ImGuiTableDrawChannelIdx)((channels_for_dummy > 0) ? channels_total - 1 : -1); table->Bg2DrawChannelCurrent = TABLE_DRAW_CHANNEL_BG2_FROZEN; table->Bg2DrawChannelUnfrozen = (ImGuiTableDrawChannelIdx)((table->FreezeRowsCount > 0) ? 2 + channels_for_row : TABLE_DRAW_CHANNEL_BG2_FROZEN); @@ -2245,7 +2322,7 @@ void ImGui::TableSetupDrawChannels(ImGuiTable* table) void ImGui::TableMergeDrawChannels(ImGuiTable* table) { ImGuiContext& g = *GImGui; - ImDrawListSplitter* splitter = &table->DrawSplitter; + ImDrawListSplitter* splitter = table->DrawSplitter; const bool has_freeze_v = (table->FreezeRowsCount > 0); const bool has_freeze_h = (table->FreezeColumnsCount > 0); IM_ASSERT(splitter->_Current == 0); @@ -2256,10 +2333,11 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) ImRect ClipRect; int ChannelsCount; ImBitArray ChannelsMask; + + MergeGroup() { ChannelsCount = 0; } }; int merge_group_mask = 0x00; MergeGroup merge_groups[4]; - memset(merge_groups, 0, sizeof(merge_groups)); // 1. Scan channels and take note of those which can be merged for (int column_n = 0; column_n < table->ColumnsCount; column_n++) @@ -2337,7 +2415,6 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data; ImBitArray remaining_mask; // We need 132-bit of storage - remaining_mask.ClearAllBits(); remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count); remaining_mask.ClearBit(table->Bg2DrawChannelUnfrozen); IM_ASSERT(has_freeze_v == false || table->Bg2DrawChannelUnfrozen != TABLE_DRAW_CHANNEL_BG2_FROZEN); @@ -2416,7 +2493,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) return; ImDrawList* inner_drawlist = inner_window->DrawList; - table->DrawSplitter.SetCurrentChannel(inner_drawlist, TABLE_DRAW_CHANNEL_BG0); + table->DrawSplitter->SetCurrentChannel(inner_drawlist, TABLE_DRAW_CHANNEL_BG0); inner_drawlist->PushClipRect(table->Bg0ClipRectForDrawCmd.Min, table->Bg0ClipRectForDrawCmd.Max, false); // Draw inner border and resizing feedback @@ -2436,7 +2513,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) const bool is_hovered = (table->HoveredColumnBorder == column_n); const bool is_resized = (table->ResizedColumn == column_n) && (table->InstanceInteracted == table->InstanceCurrent); const bool is_resizable = (column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_NoDirectResize_)) == 0; - const bool is_frozen_separator = (table->FreezeColumnsCount != -1 && table->FreezeColumnsCount == order_n + 1); + const bool is_frozen_separator = (table->FreezeColumnsCount == order_n + 1); if (column->MaxX > table->InnerClipRect.Max.x && !is_resized) continue; @@ -2532,8 +2609,7 @@ ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() if (!table->IsLayoutLocked) TableUpdateLayout(table); - if (table->IsSortSpecsDirty) - TableSortSpecsBuild(table); + TableSortSpecsBuild(table); return &table->SortSpecs; } @@ -2672,28 +2748,33 @@ void ImGui::TableSortSpecsSanitize(ImGuiTable* table) void ImGui::TableSortSpecsBuild(ImGuiTable* table) { - IM_ASSERT(table->IsSortSpecsDirty); - TableSortSpecsSanitize(table); + bool dirty = table->IsSortSpecsDirty; + if (dirty) + { + TableSortSpecsSanitize(table); + table->SortSpecsMulti.resize(table->SortSpecsCount <= 1 ? 0 : table->SortSpecsCount); + table->SortSpecs.SpecsDirty = true; // Mark as dirty for user + table->IsSortSpecsDirty = false; // Mark as not dirty for us + } // Write output - table->SortSpecsMulti.resize(table->SortSpecsCount <= 1 ? 0 : table->SortSpecsCount); ImGuiTableColumnSortSpecs* sort_specs = (table->SortSpecsCount == 0) ? NULL : (table->SortSpecsCount == 1) ? &table->SortSpecsSingle : table->SortSpecsMulti.Data; - for (int column_n = 0; column_n < table->ColumnsCount; column_n++) - { - ImGuiTableColumn* column = &table->Columns[column_n]; - if (column->SortOrder == -1) - continue; - IM_ASSERT(column->SortOrder < table->SortSpecsCount); - ImGuiTableColumnSortSpecs* sort_spec = &sort_specs[column->SortOrder]; - sort_spec->ColumnUserID = column->UserID; - sort_spec->ColumnIndex = (ImGuiTableColumnIdx)column_n; - sort_spec->SortOrder = (ImGuiTableColumnIdx)column->SortOrder; - sort_spec->SortDirection = column->SortDirection; - } + if (dirty && sort_specs != NULL) + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + if (column->SortOrder == -1) + continue; + IM_ASSERT(column->SortOrder < table->SortSpecsCount); + ImGuiTableColumnSortSpecs* sort_spec = &sort_specs[column->SortOrder]; + sort_spec->ColumnUserID = column->UserID; + sort_spec->ColumnIndex = (ImGuiTableColumnIdx)column_n; + sort_spec->SortOrder = (ImGuiTableColumnIdx)column->SortOrder; + sort_spec->SortDirection = column->SortDirection; + } + table->SortSpecs.Specs = sort_specs; table->SortSpecs.SpecsCount = table->SortSpecsCount; - table->SortSpecs.SpecsDirty = true; // Mark as dirty for user - table->IsSortSpecsDirty = false; // Mark as not dirty for us } //------------------------------------------------------------------------- @@ -2713,8 +2794,11 @@ float ImGui::TableGetHeaderRowHeight() float row_height = GetTextLineHeight(); int columns_count = TableGetColumnCount(); for (int column_n = 0; column_n < columns_count; column_n++) - if (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_IsEnabled) + { + ImGuiTableColumnFlags flags = TableGetColumnFlags(column_n); + if ((flags & ImGuiTableColumnFlags_IsEnabled) && !(flags & ImGuiTableColumnFlags_NoHeaderLabel)) row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y); + } row_height += GetStyle().CellPadding.y * 2.0f; return row_height; } @@ -2751,7 +2835,7 @@ void ImGui::TableHeadersRow() // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them) // - in your own code you may omit the PushID/PopID all-together, provided you know they won't collide // - table->InstanceCurrent is only >0 when we use multiple BeginTable/EndTable calls with same identifier. - const char* name = TableGetColumnName(column_n); + const char* name = (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_NoHeaderLabel) ? "" : TableGetColumnName(column_n); PushID(table->InstanceCurrent * table->ColumnsCount + column_n); TableHeader(name); PopID(); @@ -2833,7 +2917,6 @@ void ImGui::TableHeader(const char* label) const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); //RenderFrame(bb.Min, bb.Max, col, false, 0.0f); TableSetBgColor(ImGuiTableBgTarget_CellBg, col, table->CurrentColumn); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); } else { @@ -2841,6 +2924,7 @@ void ImGui::TableHeader(const char* label) if ((table->RowFlags & ImGuiTableRowFlags_Headers) == 0) TableSetBgColor(ImGuiTableBgTarget_CellBg, GetColorU32(ImGuiCol_TableHeaderBg), table->CurrentColumn); } + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); if (held) table->HeldHeaderColumn = (ImGuiTableColumnIdx)column_n; window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f; @@ -3006,16 +3090,19 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++) { ImGuiTableColumn* other_column = &table->Columns[other_column_n]; + if (other_column->Flags & ImGuiTableColumnFlags_Disabled) + continue; + const char* name = TableGetColumnName(table, other_column_n); if (name == NULL || name[0] == 0) name = ""; // Make sure we can't hide the last active column bool menu_item_active = (other_column->Flags & ImGuiTableColumnFlags_NoHide) ? false : true; - if (other_column->IsEnabled && table->ColumnsEnabledCount <= 1) + if (other_column->IsUserEnabled && table->ColumnsEnabledCount <= 1) menu_item_active = false; - if (MenuItem(name, NULL, other_column->IsEnabled, menu_item_active)) - other_column->IsEnabledNextFrame = !other_column->IsEnabled; + if (MenuItem(name, NULL, other_column->IsUserEnabled, menu_item_active)) + other_column->IsUserEnabledNextFrame = !other_column->IsUserEnabled; } PopItemFlag(); } @@ -3140,7 +3227,7 @@ void ImGui::TableSaveSettings(ImGuiTable* table) column_settings->DisplayOrder = column->DisplayOrder; column_settings->SortOrder = column->SortOrder; column_settings->SortDirection = column->SortDirection; - column_settings->IsEnabled = column->IsEnabled; + column_settings->IsEnabled = column->IsUserEnabled; column_settings->IsStretch = (column->Flags & ImGuiTableColumnFlags_WidthStretch) ? 1 : 0; if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) == 0) save_ref_scale = true; @@ -3154,7 +3241,7 @@ void ImGui::TableSaveSettings(ImGuiTable* table) settings->SaveFlags |= ImGuiTableFlags_Reorderable; if (column->SortOrder != -1) settings->SaveFlags |= ImGuiTableFlags_Sortable; - if (column->IsEnabled != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0)) + if (column->IsUserEnabled != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) == 0)) settings->SaveFlags |= ImGuiTableFlags_Hideable; } settings->SaveFlags &= table->Flags; @@ -3212,7 +3299,7 @@ void ImGui::TableLoadSettings(ImGuiTable* table) else column->DisplayOrder = (ImGuiTableColumnIdx)column_n; display_order_mask |= (ImU64)1 << column->DisplayOrder; - column->IsEnabled = column->IsEnabledNextFrame = column_settings->IsEnabled; + column->IsUserEnabled = column->IsUserEnabledNextFrame = column_settings->IsEnabled; column->SortOrder = column_settings->SortOrder; column->SortDirection = column_settings->SortDirection; } @@ -3231,8 +3318,9 @@ void ImGui::TableLoadSettings(ImGuiTable* table) static void TableSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) { ImGuiContext& g = *ctx; - for (int i = 0; i != g.Tables.GetSize(); i++) - g.Tables.GetByIndex(i)->SettingsOffset = -1; + for (int i = 0; i != g.Tables.GetMapSize(); i++) + if (ImGuiTable* table = g.Tables.TryGetMapData(i)) + table->SettingsOffset = -1; g.SettingsTables.clear(); } @@ -3240,12 +3328,12 @@ static void TableSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandle static void TableSettingsHandler_ApplyAll(ImGuiContext* ctx, ImGuiSettingsHandler*) { ImGuiContext& g = *ctx; - for (int i = 0; i != g.Tables.GetSize(); i++) - { - ImGuiTable* table = g.Tables.GetByIndex(i); - table->IsSettingsRequestLoad = true; - table->SettingsOffset = -1; - } + for (int i = 0; i != g.Tables.GetMapSize(); i++) + if (ImGuiTable* table = g.Tables.TryGetMapData(i)) + { + table->IsSettingsRequestLoad = true; + table->SettingsOffset = -1; + } } static void* TableSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) @@ -3318,6 +3406,9 @@ static void TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandle for (int column_n = 0; column_n < settings->ColumnsCount; column_n++, column++) { // "Column 0 UserID=0x42AD2D21 Width=100 Visible=1 Order=0 Sort=0v" + bool save_column = column->UserID != 0 || save_size || save_visible || save_order || (save_sort && column->SortOrder != -1); + if (!save_column) + continue; buf->appendf("Column %-2d", column_n); if (column->UserID != 0) buf->appendf(" UserID=%08X", column->UserID); if (save_size && column->IsStretch) buf->appendf(" Weight=%.4f", column->WidthOrWeight); @@ -3371,10 +3462,9 @@ void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table) //IMGUI_DEBUG_LOG("TableGcCompactTransientBuffers() id=0x%08X\n", table->ID); ImGuiContext& g = *GImGui; IM_ASSERT(table->MemoryCompacted == false); - table->DrawSplitter.ClearFreeMemory(); - table->SortSpecsMulti.clear(); table->SortSpecs.Specs = NULL; - table->IsSortSpecsDirty = true; + table->SortSpecsMulti.clear(); + table->IsSortSpecsDirty = true; // FIXME: shouldn't have to leak into user performing a sort table->ColumnsNames.clear(); table->MemoryCompacted = true; for (int n = 0; n < table->ColumnsCount; n++) @@ -3382,6 +3472,12 @@ void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table) g.TablesLastTimeActive[g.Tables.GetIndex(table)] = -1.0f; } +void ImGui::TableGcCompactTransientBuffers(ImGuiTableTempData* temp_data) +{ + temp_data->DrawSplitter.ClearFreeMemory(); + temp_data->LastTimeActive = -1.0f; +} + // Compact and remove unused settings data (currently only used by TestEngine) void ImGui::TableGcCompactSettings() { diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index c5ee895d..e3ac15f1 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.82 +// dear imgui, v1.84 // (widgets code) /* @@ -59,6 +59,8 @@ Index of this file: #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types #endif +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). #endif // Clang/GCC warnings with -Weverything @@ -122,7 +124,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); //------------------------------------------------------------------------- // For InputTextEx() -static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data); +static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source); static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); @@ -350,17 +352,20 @@ void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) const ImGuiStyle& style = g.Style; const float w = CalcItemWidth(); + const char* value_text_begin = &g.TempBuffer[0]; + const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + const ImVec2 value_size = CalcTextSize(value_text_begin, value_text_end, false); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const ImRect value_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2)); - const ImRect total_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x : 0.0f), style.FramePadding.y * 2) + label_size); + + const ImVec2 pos = window->DC.CursorPos; + const ImRect value_bb(pos, pos + ImVec2(w, value_size.y + style.FramePadding.y * 2)); + const ImRect total_bb(pos, pos + ImVec2(w + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), ImMax(value_size.y, label_size.y) + style.FramePadding.y * 2)); ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0)) return; // Render - const char* value_text_begin = &g.TempBuffer[0]; - const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); - RenderTextClipped(value_bb.Min, value_bb.Max, value_text_begin, value_text_end, NULL, ImVec2(0.0f, 0.5f)); + RenderTextClipped(value_bb.Min + style.FramePadding, value_bb.Max, value_text_begin, value_text_end, &value_size, ImVec2(0.0f, 0.0f)); if (label_size.x > 0.0f) RenderText(ImVec2(value_bb.Max.x + style.ItemInnerSpacing.x, value_bb.Min.y + style.FramePadding.y), label); } @@ -483,14 +488,6 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); - if (flags & ImGuiButtonFlags_Disabled) - { - if (out_hovered) *out_hovered = false; - if (out_held) *out_held = false; - if (g.ActiveId == id) ClearActiveID(); - return false; - } - // Default only reacts to left mouse button if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0) flags |= ImGuiButtonFlags_MouseButtonDefault_; @@ -505,7 +502,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool g.HoveredWindow = window; #ifdef IMGUI_ENABLE_TEST_ENGINE - if (id != 0 && window->DC.LastItemId != id) + if (id != 0 && g.LastItemData.ID != id) IMGUI_TEST_ENGINE_ITEM_ADD(bb, id); #endif @@ -522,7 +519,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool { hovered = true; SetHoveredID(id); - if (CalcTypematicRepeatAmount(g.HoveredIdTimer + 0.0001f - g.IO.DeltaTime, g.HoveredIdTimer + 0.0001f, DRAGDROP_HOLD_TO_OPEN_TIMER, 0.00f)) + if (g.HoveredIdTimer - g.IO.DeltaTime <= DRAGDROP_HOLD_TO_OPEN_TIMER && g.HoveredIdTimer >= DRAGDROP_HOLD_TO_OPEN_TIMER) { pressed = true; g.DragDropHoldJustPressedId = id; @@ -683,8 +680,9 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags if (!ItemAdd(bb, id)) return false; - if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) + if (g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat) flags |= ImGuiButtonFlags_Repeat; + bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); @@ -701,7 +699,7 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags //if (pressed && !(flags & ImGuiButtonFlags_DontClosePopups) && (window->Flags & ImGuiWindowFlags_Popup)) // CloseCurrentPopup(); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); return pressed; } @@ -759,7 +757,7 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu if (!ItemAdd(bb, id)) return false; - if (window->DC.ItemFlags & ImGuiItemFlags_ButtonRepeat) + if (g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat) flags |= ImGuiButtonFlags_Repeat; bool hovered, held; @@ -1078,7 +1076,7 @@ bool ImGui::Checkbox(const char* label, bool* v) ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, id)) { - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); return false; } @@ -1094,7 +1092,7 @@ bool ImGui::Checkbox(const char* label, bool* v) RenderNavHighlight(total_bb, id); RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); - bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0; + bool mixed_value = (g.LastItemData.InFlags & ImGuiItemFlags_MixedValue) != 0; if (mixed_value) { // Undocumented tristate/mixed/indeterminate checkbox (#2644) @@ -1114,7 +1112,7 @@ bool ImGui::Checkbox(const char* label, bool* v) if (label_size.x > 0.0f) RenderText(label_pos, label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); return pressed; } @@ -1126,11 +1124,11 @@ bool ImGui::CheckboxFlagsT(const char* label, T* flags, T flags_value) bool pressed; if (!all_on && any_on) { - ImGuiWindow* window = GetCurrentWindow(); - ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; - window->DC.ItemFlags |= ImGuiItemFlags_MixedValue; + ImGuiContext& g = *GImGui; + ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; + g.CurrentItemFlags |= ImGuiItemFlags_MixedValue; pressed = Checkbox(label, &all_on); - window->DC.ItemFlags = backup_item_flags; + g.CurrentItemFlags = backup_item_flags; } else { @@ -1216,7 +1214,7 @@ bool ImGui::RadioButton(const char* label, bool active) if (label_size.x > 0.0f) RenderText(label_pos, label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); return pressed; } @@ -1428,10 +1426,10 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - const ImGuiItemFlags item_flags_backup = window->DC.ItemFlags; - window->DC.ItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; + const ImGuiItemFlags item_flags_backup = g.CurrentItemFlags; + g.CurrentItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; bool item_add = ItemAdd(bb, id); - window->DC.ItemFlags = item_flags_backup; + g.CurrentItemFlags = item_flags_backup; if (!item_add) return false; @@ -1439,10 +1437,12 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float ImRect bb_interact = bb; bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f)); ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap); + if (hovered) + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; // for IsItemHovered(), because bb_interact is larger than bb if (g.ActiveId != id) SetItemAllowOverlap(); - if (held || (g.HoveredId == id && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) + if (held || (hovered && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW); ImRect bb_render = bb; @@ -1532,8 +1532,12 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc //------------------------------------------------------------------------- // [SECTION] Widgets: ComboBox //------------------------------------------------------------------------- +// - CalcMaxPopupHeightFromItemCount() [Internal] // - BeginCombo() +// - BeginComboPopup() [Internal] // - EndCombo() +// - BeginComboPreview() [Internal] +// - EndComboPreview() [Internal] // - Combo() //------------------------------------------------------------------------- @@ -1547,79 +1551,99 @@ static float CalcMaxPopupHeightFromItemCount(int items_count) bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags) { - // Always consume the SetNextWindowSizeConstraint() call in our early return paths ImGuiContext& g = *GImGui; - bool has_window_size_constraint = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) != 0; - g.NextWindowData.Flags &= ~ImGuiNextWindowDataFlags_HasSizeConstraint; - ImGuiWindow* window = GetCurrentWindow(); + + ImGuiNextWindowDataFlags backup_next_window_data_flags = g.NextWindowData.Flags; + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values if (window->SkipItems) return false; - IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together - const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); + IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight(); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const float expected_w = CalcItemWidth(); - const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : expected_w; - const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); - const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : CalcItemWidth(); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); + const ImRect total_bb(bb.Min, bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb)) + if (!ItemAdd(total_bb, id, &bb)) return false; + // Open on click bool hovered, held; - bool pressed = ButtonBehavior(frame_bb, id, &hovered, &held); - bool popup_open = IsPopupOpen(id, ImGuiPopupFlags_None); + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + const ImGuiID popup_id = ImHashStr("##ComboPopup", 0, id); + bool popup_open = IsPopupOpen(popup_id, ImGuiPopupFlags_None); + if ((pressed || g.NavActivateId == id) && !popup_open) + { + OpenPopupEx(popup_id, ImGuiPopupFlags_None); + popup_open = true; + } + // Render shape const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); - const float value_x2 = ImMax(frame_bb.Min.x, frame_bb.Max.x - arrow_size); - RenderNavHighlight(frame_bb, id); + const float value_x2 = ImMax(bb.Min.x, bb.Max.x - arrow_size); + RenderNavHighlight(bb, id); if (!(flags & ImGuiComboFlags_NoPreview)) - window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(value_x2, frame_bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersLeft); + window->DrawList->AddRectFilled(bb.Min, ImVec2(value_x2, bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersLeft); if (!(flags & ImGuiComboFlags_NoArrowButton)) { ImU32 bg_col = GetColorU32((popup_open || hovered) ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); - window->DrawList->AddRectFilled(ImVec2(value_x2, frame_bb.Min.y), frame_bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersRight); - if (value_x2 + arrow_size - style.FramePadding.x <= frame_bb.Max.x) - RenderArrow(window->DrawList, ImVec2(value_x2 + style.FramePadding.y, frame_bb.Min.y + style.FramePadding.y), text_col, ImGuiDir_Down, 1.0f); + window->DrawList->AddRectFilled(ImVec2(value_x2, bb.Min.y), bb.Max, bg_col, style.FrameRounding, (w <= arrow_size) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersRight); + if (value_x2 + arrow_size - style.FramePadding.x <= bb.Max.x) + RenderArrow(window->DrawList, ImVec2(value_x2 + style.FramePadding.y, bb.Min.y + style.FramePadding.y), text_col, ImGuiDir_Down, 1.0f); } - RenderFrameBorder(frame_bb.Min, frame_bb.Max, style.FrameRounding); + RenderFrameBorder(bb.Min, bb.Max, style.FrameRounding); + + // Custom preview + if (flags & ImGuiComboFlags_CustomPreview) + { + g.ComboPreviewData.PreviewRect = ImRect(bb.Min.x, bb.Min.y, value_x2, bb.Max.y); + IM_ASSERT(preview_value == NULL || preview_value[0] == 0); + preview_value = NULL; + } + + // Render preview and label if (preview_value != NULL && !(flags & ImGuiComboFlags_NoPreview)) { - ImVec2 preview_pos = frame_bb.Min + style.FramePadding; if (g.LogEnabled) LogSetNextTextDecoration("{", "}"); - RenderTextClipped(preview_pos, ImVec2(value_x2, frame_bb.Max.y), preview_value, NULL, NULL, ImVec2(0.0f, 0.0f)); + RenderTextClipped(bb.Min + style.FramePadding, ImVec2(value_x2, bb.Max.y), preview_value, NULL, NULL); } if (label_size.x > 0) - RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - - if ((pressed || g.NavActivateId == id) && !popup_open) - { - if (window->DC.NavLayerCurrent == 0) - window->NavLastIds[0] = id; - OpenPopupEx(id, ImGuiPopupFlags_None); - popup_open = true; - } + RenderText(ImVec2(bb.Max.x + style.ItemInnerSpacing.x, bb.Min.y + style.FramePadding.y), label); if (!popup_open) return false; - if (has_window_size_constraint) + g.NextWindowData.Flags = backup_next_window_data_flags; + return BeginComboPopup(popup_id, bb, flags); +} + +bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags flags) +{ + ImGuiContext& g = *GImGui; + if (!IsPopupOpen(popup_id, ImGuiPopupFlags_None)) + { + g.NextWindowData.ClearFlags(); + return false; + } + + // Set popup size + float w = bb.GetWidth(); + if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) { - g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasSizeConstraint; g.NextWindowData.SizeConstraintRect.Min.x = ImMax(g.NextWindowData.SizeConstraintRect.Min.x, w); } else { if ((flags & ImGuiComboFlags_HeightMask_) == 0) flags |= ImGuiComboFlags_HeightRegular; - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiComboFlags_HeightMask_)); // Only one int popup_max_height_in_items = -1; if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; @@ -1627,30 +1651,27 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); } + // This is essentially a specialized version of BeginPopupEx() char name[16]; ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth - // Position the window given a custom constraint (peak into expected window size so we can position it) - // This might be easier to express with an hypothetical SetNextWindowPosConstraints() function. + // Set position given a custom constraint (peak into expected window size so we can position it) + // FIXME: This might be easier to express with an hypothetical SetNextWindowPosConstraints() function? + // FIXME: This might be moved to Begin() or at least around the same spot where Tooltips and other Popups are calling FindBestWindowPosForPopupEx()? if (ImGuiWindow* popup_window = FindWindowByName(name)) if (popup_window->WasActive) { // Always override 'AutoPosLastDirection' to not leave a chance for a past value to affect us. ImVec2 size_expected = CalcWindowNextAutoFitSize(popup_window); - if (flags & ImGuiComboFlags_PopupAlignLeft) - popup_window->AutoPosLastDirection = ImGuiDir_Left; // "Below, Toward Left" - else - popup_window->AutoPosLastDirection = ImGuiDir_Down; // "Below, Toward Right (default)" - ImRect r_outer = GetWindowAllowedExtentRect(popup_window); - ImVec2 pos = FindBestWindowPosForPopupEx(frame_bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, frame_bb, ImGuiPopupPositionPolicy_ComboBox); + popup_window->AutoPosLastDirection = (flags & ImGuiComboFlags_PopupAlignLeft) ? ImGuiDir_Left : ImGuiDir_Down; // Left = "Below, Toward Left", Down = "Below, Toward Right (default)" + ImRect r_outer = GetPopupAllowedExtentRect(popup_window); + ImVec2 pos = FindBestWindowPosForPopupEx(bb.GetBL(), size_expected, &popup_window->AutoPosLastDirection, r_outer, bb, ImGuiPopupPositionPolicy_ComboBox); SetNextWindowPos(pos); } // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx() ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove; - - // Horizontally align ourselves with the framed text - PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(style.FramePadding.x, style.WindowPadding.y)); + PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(g.Style.FramePadding.x, g.Style.WindowPadding.y)); // Horizontally align ourselves with the framed text bool ret = Begin(name, NULL, window_flags); PopStyleVar(); if (!ret) @@ -1667,6 +1688,57 @@ void ImGui::EndCombo() EndPopup(); } +// Call directly after the BeginCombo/EndCombo block. The preview is designed to only host non-interactive elements +// (Experimental, see GitHub issues: #1658, #4168) +bool ImGui::BeginComboPreview() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiComboPreviewData* preview_data = &g.ComboPreviewData; + + if (window->SkipItems || !window->ClipRect.Overlaps(g.LastItemData.Rect)) // FIXME: Because we don't have a ImGuiItemStatusFlags_Visible flag to test last ItemAdd() result + return false; + IM_ASSERT(g.LastItemData.Rect.Min.x == preview_data->PreviewRect.Min.x && g.LastItemData.Rect.Min.y == preview_data->PreviewRect.Min.y); // Didn't call after BeginCombo/EndCombo block or forgot to pass ImGuiComboFlags_CustomPreview flag? + if (!window->ClipRect.Contains(preview_data->PreviewRect)) // Narrower test (optional) + return false; + + // FIXME: This could be contained in a PushWorkRect() api + preview_data->BackupCursorPos = window->DC.CursorPos; + preview_data->BackupCursorMaxPos = window->DC.CursorMaxPos; + preview_data->BackupCursorPosPrevLine = window->DC.CursorPosPrevLine; + preview_data->BackupPrevLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; + preview_data->BackupLayout = window->DC.LayoutType; + window->DC.CursorPos = preview_data->PreviewRect.Min + g.Style.FramePadding; + window->DC.CursorMaxPos = window->DC.CursorPos; + window->DC.LayoutType = ImGuiLayoutType_Horizontal; + PushClipRect(preview_data->PreviewRect.Min, preview_data->PreviewRect.Max, true); + + return true; +} + +void ImGui::EndComboPreview() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiComboPreviewData* preview_data = &g.ComboPreviewData; + + // FIXME: Using CursorMaxPos approximation instead of correct AABB which we will store in ImDrawCmd in the future + ImDrawList* draw_list = window->DrawList; + if (window->DC.CursorMaxPos.x < preview_data->PreviewRect.Max.x && window->DC.CursorMaxPos.y < preview_data->PreviewRect.Max.y) + if (draw_list->CmdBuffer.Size > 1) // Unlikely case that the PushClipRect() didn't create a command + { + draw_list->_CmdHeader.ClipRect = draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 1].ClipRect = draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 2].ClipRect; + draw_list->_TryMergeDrawCmds(); + } + PopClipRect(); + window->DC.CursorPos = preview_data->BackupCursorPos; + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, preview_data->BackupCursorMaxPos); + window->DC.CursorPosPrevLine = preview_data->BackupCursorPosPrevLine; + window->DC.PrevLineTextBaseOffset = preview_data->BackupPrevLineTextBaseOffset; + window->DC.LayoutType = preview_data->BackupLayout; + preview_data->PreviewRect = ImRect(); +} + // Getter for the old Combo() API: const char*[] static bool Items_ArrayGetter(void* data, int idx, const char** out_text) { @@ -1735,8 +1807,9 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi } EndCombo(); + if (value_changed) - MarkItemEdited(g.CurrentWindow->DC.LastItemId); + MarkItemEdited(g.LastItemData.ID); return value_changed; } @@ -1790,7 +1863,7 @@ static const ImGuiDataTypeInfo GDataTypeInfo[] = { sizeof(ImS64), "S64", "%lld", "%lld" }, // ImGuiDataType_S64 { sizeof(ImU64), "U64", "%llu", "%llu" }, #endif - { sizeof(float), "float", "%f", "%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) + { sizeof(float), "float", "%.3f","%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) { sizeof(double), "double","%f", "%lf" }, // ImGuiDataType_Double }; IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); @@ -1977,13 +2050,15 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b { // All other types assign constant // We don't bother handling support for legacy operators since they are a little too crappy. Instead we will later implement a proper expression evaluator in the future. - sscanf(buf, format, p_data); + if (sscanf(buf, format, p_data) < 1) + return false; } else { // Small types need a 32-bit buffer to receive the result from scanf() int v32; - sscanf(buf, format, &v32); + if (sscanf(buf, format, &v32) < 1) + return false; if (data_type == ImGuiDataType_S8) *(ImS8*)p_data = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX); else if (data_type == ImGuiDataType_U8) @@ -2082,6 +2157,7 @@ static const char* ImAtoi(const char* src, TYPE* output) // - stb_sprintf.h supports several new modifiers which format numbers in a way that also makes them incompatible atof/atoi. static void SanitizeFormatString(const char* fmt, char* fmt_out, size_t fmt_out_size) { + IM_UNUSED(fmt_out_size); const char* fmt_end = ImParseFormatFindEnd(fmt); IM_ASSERT((size_t)(fmt_end - fmt + 1) < fmt_out_size); // Format is too long, let us know if this happens to you! while (fmt < fmt_end) @@ -2269,7 +2345,7 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v } if (g.ActiveId != id) return false; - if ((g.CurrentWindow->DC.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) return false; switch (data_type) @@ -2302,12 +2378,14 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); const float w = CalcItemWidth(); + const ImVec2 label_size = CalcTextSize(label, NULL, true); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb)) + if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemAddFlags_Focusable : 0)) return false; // Default format string when passing NULL @@ -2318,11 +2396,10 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, // Tabbing or CTRL-clicking on Drag turns it into an InputText const bool hovered = ItemHoverable(frame_bb, id); - const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = temp_input_allowed && FocusableItemRegister(window, id); + const bool focus_requested = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0; const bool clicked = (hovered && g.IO.MouseClicked[0]); const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) @@ -2332,10 +2409,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, FocusWindow(window); g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id)) - { temp_input_is_active = true; - FocusableItemUnregister(window); - } } // Experimental: simple click (without moving) turns Drag into an InputText // FIXME: Currently polling ImGuiConfigFlags_IsTouchScreen, may either poll an hypothetical ImGuiBackendFlags_HasKeyboard and/or an explicit drag settings. @@ -2344,7 +2418,6 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, { g.NavInputId = id; temp_input_is_active = true; - FocusableItemUnregister(window); } } @@ -2356,7 +2429,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, } // Draw frame - const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); @@ -2375,7 +2448,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); return value_changed; } @@ -2463,6 +2536,7 @@ bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_cu TextEx(label, FindRenderedTextEnd(label)); EndGroup(); PopID(); + return value_changed; } @@ -2875,7 +2949,7 @@ bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flag! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); ImGuiContext& g = *GImGui; - if ((g.CurrentWindow->DC.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) return false; switch (data_type) @@ -2925,8 +2999,9 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); + const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb)) + if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemAddFlags_Focusable : 0)) return false; // Default format string when passing NULL @@ -2937,11 +3012,10 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Tabbing or CTRL-clicking on Slider turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); - const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = temp_input_allowed && FocusableItemRegister(window, id); + const bool focus_requested = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0; const bool clicked = (hovered && g.IO.MouseClicked[0]); if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id) { @@ -2950,10 +3024,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat FocusWindow(window); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id)) - { temp_input_is_active = true; - FocusableItemUnregister(window); - } } } @@ -2965,7 +3036,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat } // Draw frame - const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); @@ -2989,7 +3060,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); return value_changed; } @@ -3113,7 +3184,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d } // Draw frame - const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); + const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); RenderNavHighlight(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); @@ -3280,7 +3351,7 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* ClearActiveID(); g.CurrentWindow->DC.CursorPos = bb.Min; - bool value_changed = InputTextEx(label, NULL, buf, buf_size, bb.GetSize(), flags); + bool value_changed = InputTextEx(label, NULL, buf, buf_size, bb.GetSize(), flags | ImGuiInputTextFlags_MergedItem); if (init) { // First frame we started displaying the InputText widget, we expect it to take the active id. @@ -3368,7 +3439,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data style.FramePadding.x = style.FramePadding.y; ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups; if (flags & ImGuiInputTextFlags_ReadOnly) - button_flags |= ImGuiButtonFlags_Disabled; + BeginDisabled(true); SameLine(0, style.ItemInnerSpacing.x); if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) { @@ -3381,6 +3452,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data DataTypeApplyOp(data_type, '+', p_data, p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); value_changed = true; } + if (flags & ImGuiInputTextFlags_ReadOnly) + EndDisabled(); const char* label_end = FindRenderedTextEnd(label); if (label != label_end) @@ -3399,7 +3472,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format); } if (value_changed) - MarkItemEdited(window->DC.LastItemId); + MarkItemEdited(g.LastItemData.ID); return value_changed; } @@ -3577,12 +3650,12 @@ static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* t namespace ImStb { -static int STB_TEXTEDIT_STRINGLEN(const STB_TEXTEDIT_STRING* obj) { return obj->CurLenW; } -static ImWchar STB_TEXTEDIT_GETCHAR(const STB_TEXTEDIT_STRING* obj, int idx) { return obj->TextW[idx]; } -static float STB_TEXTEDIT_GETWIDTH(STB_TEXTEDIT_STRING* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *GImGui; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } +static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } +static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { return obj->TextW[idx]; } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *GImGui; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; -static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx) +static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) { const ImWchar* text = obj->TextW.Data; const ImWchar* text_remaining = NULL; @@ -3595,19 +3668,20 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* ob r->num_chars = (int)(text_remaining - (text + line_start_idx)); } +// When ImGuiInputTextFlags_Password is set, we don't want actions such as CTRL+Arrow to leak the fact that underlying data are blanks or separators. static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } -static int is_word_boundary_from_right(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (is_separator(obj->TextW[idx - 1]) && !is_separator(obj->TextW[idx]) ) : 1; } -static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } +static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (is_separator(obj->TextW[idx - 1]) && !is_separator(obj->TextW[idx]) ) : 1; } +static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } #ifdef __APPLE__ // FIXME: Move setting to IO structure -static int is_word_boundary_from_left(STB_TEXTEDIT_STRING* obj, int idx) { return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx]) ) : 1; } -static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } +static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx]) ) : 1; } +static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } #else -static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(STB_TEXTEDIT_STRING* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } +static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } #endif #define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h #define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL -static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) +static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n) { ImWchar* dst = obj->TextW.Data + pos; @@ -3623,9 +3697,9 @@ static void STB_TEXTEDIT_DELETECHARS(STB_TEXTEDIT_STRING* obj, int pos, int n) *dst = '\0'; } -static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const ImWchar* new_text, int new_text_len) +static bool STB_TEXTEDIT_INSERTCHARS(ImGuiInputTextState* obj, int pos, const ImWchar* new_text, int new_text_len) { - const bool is_resizable = (obj->UserFlags & ImGuiInputTextFlags_CallbackResize) != 0; + const bool is_resizable = (obj->Flags & ImGuiInputTextFlags_CallbackResize) != 0; const int text_len = obj->CurLenW; IM_ASSERT(pos <= text_len); @@ -3679,7 +3753,7 @@ static bool STB_TEXTEDIT_INSERTCHARS(STB_TEXTEDIT_STRING* obj, int pos, const Im // stb_textedit internally allows for a single undo record to do addition and deletion, but somehow, calling // the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?) -static void stb_textedit_replace(STB_TEXTEDIT_STRING* str, STB_TexteditState* state, const STB_TEXTEDIT_CHARTYPE* text, int text_len) +static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* state, const STB_TEXTEDIT_CHARTYPE* text, int text_len) { stb_text_makeundo_replace(str, state, 0, str->CurLenW, text_len); ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenW); @@ -3762,11 +3836,13 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons } // Return false to discard a character. -static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) { + IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard); unsigned int c = *p_char; // Filter non-printable (NB: isprint is unreliable! see #2467) + bool apply_named_filters = true; if (c < 0x20) { bool pass = false; @@ -3774,22 +3850,26 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); if (!pass) return false; + apply_named_filters = false; // Override named filters below so newline and tabs can still be inserted. } - // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) - if (c == 127) - return false; + if (input_source != ImGuiInputSource_Clipboard) + { + // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) + if (c == 127) + return false; - // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) - if (c >= 0xE000 && c <= 0xF8FF) - return false; + // Filter private Unicode range. GLFW on OSX seems to send private characters for special keys like arrow keys (FIXME) + if (c >= 0xE000 && c <= 0xF8FF) + return false; + } - // Filter Unicode ranges we are not handling in this build. + // Filter Unicode ranges we are not handling in this build if (c > IM_UNICODE_CODEPOINT_MAX) return false; // Generic named filters - if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific)) + if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))) { // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf. // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. @@ -3886,14 +3966,16 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiWindow* draw_window = window; ImVec2 inner_size = frame_size; + ImGuiItemStatusFlags item_status_flags = 0; if (is_multiline) { - if (!ItemAdd(total_bb, id, &frame_bb)) + if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemAddFlags_Focusable)) { ItemSize(total_bb, style.FramePadding.y); EndGroup(); return false; } + item_status_flags = g.LastItemData.StatusFlags; // We reproduce the contents of BeginChildFrame() in order to provide 'label' so our window internal data are easier to read/debug. PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); @@ -3909,15 +3991,18 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ return false; } draw_window = g.CurrentWindow; // Child window - draw_window->DC.NavLayerActiveMaskNext |= (1 << draw_window->DC.NavLayerCurrent); // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. + draw_window->DC.NavLayersActiveMaskNext |= (1 << draw_window->DC.NavLayerCurrent); // This is to ensure that EndChild() will display a navigation highlight so we can "enter" into it. draw_window->DC.CursorPos += style.FramePadding; inner_size.x -= draw_window->ScrollbarSizes.x; } else { + // Support for internal ImGuiInputTextFlags_MergedItem flag, which could be redesigned as an ItemFlags if needed (with test performed in ItemAdd) ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb)) - return false; + if (!(flags & ImGuiInputTextFlags_MergedItem)) + if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemAddFlags_Focusable)) + return false; + item_status_flags = g.LastItemData.StatusFlags; } const bool hovered = ItemHoverable(frame_bb, id); if (hovered) @@ -3926,9 +4011,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We are only allowed to access the state if we are already the active widget. ImGuiInputTextState* state = GetInputTextState(id); - const bool focus_requested = FocusableItemRegister(window, id); - const bool focus_requested_by_code = focus_requested && (g.TabFocusRequestCurrWindow == window && g.TabFocusRequestCurrCounterRegular == window->DC.FocusCounterRegular); - const bool focus_requested_by_tab = focus_requested && !focus_requested_by_code; + const bool focus_requested_by_code = (item_status_flags & ImGuiItemStatusFlags_FocusedByCode) != 0; + const bool focus_requested_by_tabbing = (item_status_flags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool user_clicked = hovered && io.MouseClicked[0]; const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_Keyboard)); @@ -3941,7 +4025,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); - const bool init_make_active = (focus_requested || user_clicked || user_scroll_finish || user_nav_input_start); + const bool init_make_active = (user_clicked || user_scroll_finish || user_nav_input_start || focus_requested_by_code || focus_requested_by_tabbing); const bool init_state = (init_make_active || user_scroll_active); if ((init_state && g.ActiveId != id) || init_changed_specs) { @@ -3982,7 +4066,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } if (flags & ImGuiInputTextFlags_AlwaysOverwrite) state->Stb.insert_mode = 1; // stb field name is indeed incorrect (see #2863) - if (!is_multiline && (focus_requested_by_tab || (user_clicked && io.KeyCtrl))) + if (!is_multiline && (focus_requested_by_tabbing || (user_clicked && io.KeyCtrl))) select_all = true; } @@ -4060,7 +4144,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ backup_current_text_length = state->CurLenA; state->Edited = false; state->BufCapacityA = buf_size; - state->UserFlags = flags; + state->Flags = flags; state->UserCallback = callback; state->UserCallbackData = callback_user_data; @@ -4109,7 +4193,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!io.InputQueueCharacters.contains('\t')) { unsigned int c = '\t'; // Insert TAB - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } @@ -4124,7 +4208,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ unsigned int c = (unsigned int)io.InputQueueCharacters[n]; if (c == '\t' && io.KeyShift) continue; - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } @@ -4188,7 +4272,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (!is_readonly) { unsigned int c = '\n'; // Insert new line - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } } @@ -4241,7 +4325,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ s += ImTextCharFromUtf8(&c, s, NULL); if (c == 0) break; - if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data)) + if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; } @@ -4413,7 +4497,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // Clear temporary user storage - state->UserFlags = 0; + state->Flags = ImGuiInputTextFlags_None; state->UserCallback = NULL; state->UserCallbackData = NULL; } @@ -4597,7 +4681,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { state->CursorAnim += io.DeltaTime; bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f; - ImVec2 cursor_screen_pos = draw_pos + cursor_offset - draw_scroll; + ImVec2 cursor_screen_pos = ImFloor(draw_pos + cursor_offset - draw_scroll); ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f); if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect)) draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_Text)); @@ -4647,7 +4731,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (value_changed && !(flags & ImGuiInputTextFlags_NoMarkEdited)) MarkItemEdited(id); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0) return enter_pressed; else @@ -4698,24 +4782,24 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // If we're not showing any slider there's no point in doing any HSV conversions const ImGuiColorEditFlags flags_untouched = flags; if (flags & ImGuiColorEditFlags_NoInputs) - flags = (flags & (~ImGuiColorEditFlags__DisplayMask)) | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoOptions; + flags = (flags & (~ImGuiColorEditFlags_DisplayMask_)) | ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_NoOptions; // Context menu: display and modify options (before defaults are applied) if (!(flags & ImGuiColorEditFlags_NoOptions)) ColorEditOptionsPopup(col, flags); // Read stored options - if (!(flags & ImGuiColorEditFlags__DisplayMask)) - flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DisplayMask); - if (!(flags & ImGuiColorEditFlags__DataTypeMask)) - flags |= (g.ColorEditOptions & ImGuiColorEditFlags__DataTypeMask); - if (!(flags & ImGuiColorEditFlags__PickerMask)) - flags |= (g.ColorEditOptions & ImGuiColorEditFlags__PickerMask); - if (!(flags & ImGuiColorEditFlags__InputMask)) - flags |= (g.ColorEditOptions & ImGuiColorEditFlags__InputMask); - flags |= (g.ColorEditOptions & ~(ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask)); - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check that only 1 is selected - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected + if (!(flags & ImGuiColorEditFlags_DisplayMask_)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags_DisplayMask_); + if (!(flags & ImGuiColorEditFlags_DataTypeMask_)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags_DataTypeMask_); + if (!(flags & ImGuiColorEditFlags_PickerMask_)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags_PickerMask_); + if (!(flags & ImGuiColorEditFlags_InputMask_)) + flags |= (g.ColorEditOptions & ImGuiColorEditFlags_InputMask_); + flags |= (g.ColorEditOptions & ~(ImGuiColorEditFlags_DisplayMask_ | ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_PickerMask_ | ImGuiColorEditFlags_InputMask_)); + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_DisplayMask_)); // Check that only 1 is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_InputMask_)); // Check that only 1 is selected const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0; const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0; @@ -4803,11 +4887,14 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag char* p = buf; while (*p == '#' || ImCharIsBlankA(*p)) p++; - i[0] = i[1] = i[2] = i[3] = 0; + i[0] = i[1] = i[2] = 0; + i[3] = 0xFF; // alpha default to 255 is not parsed by scanf (e.g. inputting #FFFFFF omitting alpha) + int r; if (alpha) - sscanf(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned) + r = sscanf(p, "%02X%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2], (unsigned int*)&i[3]); // Treat at unsigned (%X is unsigned) else - sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); + r = sscanf(p, "%02X%02X%02X", (unsigned int*)&i[0], (unsigned int*)&i[1], (unsigned int*)&i[2]); + IM_UNUSED(r); // Fixes C6031: Return value ignored: 'sscanf'. } if (!(flags & ImGuiColorEditFlags_NoOptions)) OpenPopupOnItemClick("context"); @@ -4827,7 +4914,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Store current color and open a picker g.ColorPickerRef = col_v4; OpenPopup("picker"); - SetNextWindowPos(window->DC.LastItemRect.GetBL() + ImVec2(-1, style.ItemSpacing.y)); + SetNextWindowPos(g.LastItemData.Rect.GetBL() + ImVec2(-1, style.ItemSpacing.y)); } } if (!(flags & ImGuiColorEditFlags_NoOptions)) @@ -4841,8 +4928,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag TextEx(label, label_display_end); Spacing(); } - ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__PickerMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; - ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags__DisplayMask | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; + ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_PickerMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; + ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags_DisplayMask_ | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes? value_changed |= ColorPicker4("##picker", col, picker_flags, &g.ColorPickerRef.x); EndPopup(); @@ -4884,7 +4971,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Drag and Drop Target // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test. - if ((window->DC.LastItemStatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) + if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) { bool accepted_drag_drop = false; if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) @@ -4906,10 +4993,10 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // When picker is being actively used, use its active id so IsItemActive() will function on ColorEdit4(). if (picker_active_window && g.ActiveId != 0 && g.ActiveIdWindow == picker_active_window) - window->DC.LastItemId = g.ActiveId; + g.LastItemData.ID = g.ActiveId; if (value_changed) - MarkItemEdited(window->DC.LastItemId); + MarkItemEdited(g.LastItemData.ID); return value_changed; } @@ -4962,12 +5049,12 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl ColorPickerOptionsPopup(col, flags); // Read stored options - if (!(flags & ImGuiColorEditFlags__PickerMask)) - flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__PickerMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__PickerMask; - if (!(flags & ImGuiColorEditFlags__InputMask)) - flags |= ((g.ColorEditOptions & ImGuiColorEditFlags__InputMask) ? g.ColorEditOptions : ImGuiColorEditFlags__OptionsDefault) & ImGuiColorEditFlags__InputMask; - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check that only 1 is selected - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check that only 1 is selected + if (!(flags & ImGuiColorEditFlags_PickerMask_)) + flags |= ((g.ColorEditOptions & ImGuiColorEditFlags_PickerMask_) ? g.ColorEditOptions : ImGuiColorEditFlags_DefaultOptions_) & ImGuiColorEditFlags_PickerMask_; + if (!(flags & ImGuiColorEditFlags_InputMask_)) + flags |= ((g.ColorEditOptions & ImGuiColorEditFlags_InputMask_) ? g.ColorEditOptions : ImGuiColorEditFlags_DefaultOptions_) & ImGuiColorEditFlags_InputMask_; + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_PickerMask_)); // Check that only 1 is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_InputMask_)); // Check that only 1 is selected if (!(flags & ImGuiColorEditFlags_NoOptions)) flags |= (g.ColorEditOptions & ImGuiColorEditFlags_AlphaBar); @@ -5113,7 +5200,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if ((flags & ImGuiColorEditFlags_NoLabel)) Text("Current"); - ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoTooltip; + ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf | ImGuiColorEditFlags_NoTooltip; ColorButton("##current", col_v4, (flags & sub_flags_to_forward), ImVec2(square_sz * 3, square_sz * 2)); if (ref_col != NULL) { @@ -5152,9 +5239,9 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if ((flags & ImGuiColorEditFlags_NoInputs) == 0) { PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x); - ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags__DataTypeMask | ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; + ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; ImGuiColorEditFlags sub_flags = (flags & sub_flags_to_forward) | ImGuiColorEditFlags_NoPicker; - if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags_DisplayMask_) == 0) if (ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_DisplayRGB)) { // FIXME: Hackily differentiating using the DragInt (ActiveId != 0 && !ActiveIdAllowOverlap) vs. using the InputText or DropTarget. @@ -5162,9 +5249,9 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl value_changed_fix_hue_wrap = (g.ActiveId != 0 && !g.ActiveIdAllowOverlap); value_changed = true; } - if (flags & ImGuiColorEditFlags_DisplayHSV || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + if (flags & ImGuiColorEditFlags_DisplayHSV || (flags & ImGuiColorEditFlags_DisplayMask_) == 0) value_changed |= ColorEdit4("##hsv", col, sub_flags | ImGuiColorEditFlags_DisplayHSV); - if (flags & ImGuiColorEditFlags_DisplayHex || (flags & ImGuiColorEditFlags__DisplayMask) == 0) + if (flags & ImGuiColorEditFlags_DisplayHex || (flags & ImGuiColorEditFlags_DisplayMask_) == 0) value_changed |= ColorEdit4("##hex", col, sub_flags | ImGuiColorEditFlags_DisplayHex); PopItemWidth(); } @@ -5305,7 +5392,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if (value_changed && memcmp(backup_initial_col, col, components * sizeof(float)) == 0) value_changed = false; if (value_changed) - MarkItemEdited(window->DC.LastItemId); + MarkItemEdited(g.LastItemData.ID); PopID(); @@ -5394,7 +5481,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl // Tooltip if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered) - ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)); + ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)); return pressed; } @@ -5403,18 +5490,18 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl void ImGui::SetColorEditOptions(ImGuiColorEditFlags flags) { ImGuiContext& g = *GImGui; - if ((flags & ImGuiColorEditFlags__DisplayMask) == 0) - flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DisplayMask; - if ((flags & ImGuiColorEditFlags__DataTypeMask) == 0) - flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__DataTypeMask; - if ((flags & ImGuiColorEditFlags__PickerMask) == 0) - flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__PickerMask; - if ((flags & ImGuiColorEditFlags__InputMask) == 0) - flags |= ImGuiColorEditFlags__OptionsDefault & ImGuiColorEditFlags__InputMask; - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DisplayMask)); // Check only 1 option is selected - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__DataTypeMask)); // Check only 1 option is selected - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__PickerMask)); // Check only 1 option is selected - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags__InputMask)); // Check only 1 option is selected + if ((flags & ImGuiColorEditFlags_DisplayMask_) == 0) + flags |= ImGuiColorEditFlags_DefaultOptions_ & ImGuiColorEditFlags_DisplayMask_; + if ((flags & ImGuiColorEditFlags_DataTypeMask_) == 0) + flags |= ImGuiColorEditFlags_DefaultOptions_ & ImGuiColorEditFlags_DataTypeMask_; + if ((flags & ImGuiColorEditFlags_PickerMask_) == 0) + flags |= ImGuiColorEditFlags_DefaultOptions_ & ImGuiColorEditFlags_PickerMask_; + if ((flags & ImGuiColorEditFlags_InputMask_) == 0) + flags |= ImGuiColorEditFlags_DefaultOptions_ & ImGuiColorEditFlags_InputMask_; + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_DisplayMask_)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_DataTypeMask_)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_PickerMask_)); // Check only 1 option is selected + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiColorEditFlags_InputMask_)); // Check only 1 option is selected g.ColorEditOptions = flags; } @@ -5434,9 +5521,9 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags ImVec2 sz(g.FontSize * 3 + g.Style.FramePadding.y * 2, g.FontSize * 3 + g.Style.FramePadding.y * 2); ImVec4 cf(col[0], col[1], col[2], (flags & ImGuiColorEditFlags_NoAlpha) ? 1.0f : col[3]); int cr = IM_F32_TO_INT8_SAT(col[0]), cg = IM_F32_TO_INT8_SAT(col[1]), cb = IM_F32_TO_INT8_SAT(col[2]), ca = (flags & ImGuiColorEditFlags_NoAlpha) ? 255 : IM_F32_TO_INT8_SAT(col[3]); - ColorButton("##preview", cf, (flags & (ImGuiColorEditFlags__InputMask | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)) | ImGuiColorEditFlags_NoTooltip, sz); + ColorButton("##preview", cf, (flags & (ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)) | ImGuiColorEditFlags_NoTooltip, sz); SameLine(); - if ((flags & ImGuiColorEditFlags_InputRGB) || !(flags & ImGuiColorEditFlags__InputMask)) + if ((flags & ImGuiColorEditFlags_InputRGB) || !(flags & ImGuiColorEditFlags_InputMask_)) { if (flags & ImGuiColorEditFlags_NoAlpha) Text("#%02X%02X%02X\nR: %d, G: %d, B: %d\n(%.3f, %.3f, %.3f)", cr, cg, cb, cr, cg, cb, col[0], col[1], col[2]); @@ -5455,23 +5542,23 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) { - bool allow_opt_inputs = !(flags & ImGuiColorEditFlags__DisplayMask); - bool allow_opt_datatype = !(flags & ImGuiColorEditFlags__DataTypeMask); + bool allow_opt_inputs = !(flags & ImGuiColorEditFlags_DisplayMask_); + bool allow_opt_datatype = !(flags & ImGuiColorEditFlags_DataTypeMask_); if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup("context")) return; ImGuiContext& g = *GImGui; ImGuiColorEditFlags opts = g.ColorEditOptions; if (allow_opt_inputs) { - if (RadioButton("RGB", (opts & ImGuiColorEditFlags_DisplayRGB) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayRGB; - if (RadioButton("HSV", (opts & ImGuiColorEditFlags_DisplayHSV) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHSV; - if (RadioButton("Hex", (opts & ImGuiColorEditFlags_DisplayHex) != 0)) opts = (opts & ~ImGuiColorEditFlags__DisplayMask) | ImGuiColorEditFlags_DisplayHex; + if (RadioButton("RGB", (opts & ImGuiColorEditFlags_DisplayRGB) != 0)) opts = (opts & ~ImGuiColorEditFlags_DisplayMask_) | ImGuiColorEditFlags_DisplayRGB; + if (RadioButton("HSV", (opts & ImGuiColorEditFlags_DisplayHSV) != 0)) opts = (opts & ~ImGuiColorEditFlags_DisplayMask_) | ImGuiColorEditFlags_DisplayHSV; + if (RadioButton("Hex", (opts & ImGuiColorEditFlags_DisplayHex) != 0)) opts = (opts & ~ImGuiColorEditFlags_DisplayMask_) | ImGuiColorEditFlags_DisplayHex; } if (allow_opt_datatype) { if (allow_opt_inputs) Separator(); - if (RadioButton("0..255", (opts & ImGuiColorEditFlags_Uint8) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Uint8; - if (RadioButton("0.00..1.00", (opts & ImGuiColorEditFlags_Float) != 0)) opts = (opts & ~ImGuiColorEditFlags__DataTypeMask) | ImGuiColorEditFlags_Float; + if (RadioButton("0..255", (opts & ImGuiColorEditFlags_Uint8) != 0)) opts = (opts & ~ImGuiColorEditFlags_DataTypeMask_) | ImGuiColorEditFlags_Uint8; + if (RadioButton("0.00..1.00", (opts & ImGuiColorEditFlags_Float) != 0)) opts = (opts & ~ImGuiColorEditFlags_DataTypeMask_) | ImGuiColorEditFlags_Float; } if (allow_opt_inputs || allow_opt_datatype) @@ -5506,7 +5593,7 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags) { - bool allow_opt_picker = !(flags & ImGuiColorEditFlags__PickerMask); + bool allow_opt_picker = !(flags & ImGuiColorEditFlags_PickerMask_); bool allow_opt_alpha_bar = !(flags & ImGuiColorEditFlags_NoAlpha) && !(flags & ImGuiColorEditFlags_AlphaBar); if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup("context")) return; @@ -5525,7 +5612,7 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl if (picker_type == 1) picker_flags |= ImGuiColorEditFlags_PickerHueWheel; ImVec2 backup_pos = GetCursorScreenPos(); if (Selectable("##selectable", false, 0, picker_size)) // By default, Selectable() is closing popup - g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags__PickerMask) | (picker_flags & ImGuiColorEditFlags__PickerMask); + g.ColorEditOptions = (g.ColorEditOptions & ~ImGuiColorEditFlags_PickerMask_) | (picker_flags & ImGuiColorEditFlags_PickerMask_); SetCursorScreenPos(backup_pos); ImVec4 previewing_ref_col; memcpy(&previewing_ref_col, ref_col, sizeof(float) * ((picker_flags & ImGuiColorEditFlags_NoAlpha) ? 3 : 4)); @@ -5738,14 +5825,14 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); bool item_add = ItemAdd(interact_bb, id); - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; - window->DC.LastItemDisplayRect = frame_bb; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; + g.LastItemData.DisplayRect = frame_bb; if (!item_add) { if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); - IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); + IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); return is_open; } @@ -5819,7 +5906,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { is_open = !is_open; window->DC.StateStorage->SetInt(id, is_open); - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledOpen; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledOpen; } } if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) @@ -5827,7 +5914,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render const ImU32 text_col = GetColorU32(ImGuiCol_Text); @@ -5858,8 +5945,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false); - RenderNavHighlight(frame_bb, id, nav_highlight_flags); } + RenderNavHighlight(frame_bb, id, nav_highlight_flags); if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); else if (!is_leaf) @@ -5871,7 +5958,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); return is_open; } @@ -5975,14 +6062,14 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFl // FIXME: We can evolve this into user accessible helpers to add extra buttons on title bars, headers, etc. // FIXME: CloseButton can overlap into text, need find a way to clip the text somehow. ImGuiContext& g = *GImGui; - ImGuiLastItemDataBackup last_item_backup; + ImGuiLastItemData last_item_backup = g.LastItemData; float button_size = g.FontSize; - float button_x = ImMax(window->DC.LastItemRect.Min.x, window->DC.LastItemRect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); - float button_y = window->DC.LastItemRect.Min.y; + float button_x = ImMax(g.LastItemData.Rect.Min.x, g.LastItemData.Rect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); + float button_y = g.LastItemData.Rect.Min.y; ImGuiID close_button_id = GetIDWithSeed("#CLOSE", NULL, id); if (CloseButton(close_button_id, ImVec2(button_x, button_y))) *p_visible = false; - last_item_backup.Restore(); + g.LastItemData = last_item_backup; } return is_open; @@ -6052,12 +6139,13 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl } bool item_add; - if (flags & ImGuiSelectableFlags_Disabled) + const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0; + if (disabled_item) { - ImGuiItemFlags backup_item_flags = window->DC.ItemFlags; - window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus; + ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; + g.CurrentItemFlags |= ImGuiItemFlags_Disabled; item_add = ItemAdd(bb, id); - window->DC.ItemFlags = backup_item_flags; + g.CurrentItemFlags = backup_item_flags; } else { @@ -6073,6 +6161,10 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (!item_add) return false; + const bool disabled_global = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; + if (disabled_item && !disabled_global) // Only testing this as an optimization + BeginDisabled(true); + // FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only, // which would be advantageous since most selectable are not selected. if (span_all_columns && window->DC.CurrentColumns) @@ -6085,17 +6177,24 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; } if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } - if (flags & ImGuiSelectableFlags_Disabled) { button_flags |= ImGuiButtonFlags_Disabled; } if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } if (flags & ImGuiSelectableFlags_AllowItemOverlap) { button_flags |= ImGuiButtonFlags_AllowItemOverlap; } - if (flags & ImGuiSelectableFlags_Disabled) - selected = false; - const bool was_selected = selected; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); + // Auto-select when moved into + // - This will be more fully fleshed in the range-select branch + // - This is not exposed as it won't nicely work with some user side handling of shift/control + // - We cannot do 'if (g.NavJustMovedToId != id) { selected = false; pressed = was_selected; }' for two reasons + // - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope()) + // - (2) usage will fail with clipped items + // The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API. + if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == window->DC.NavFocusScopeIdCurrent) + if (g.NavJustMovedToId == id) + selected = pressed = true; + // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) { @@ -6113,7 +6212,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // In this branch, Selectable() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 - window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection; + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render if (held && (flags & ImGuiSelectableFlags_DrawHoveredWhenHeld)) @@ -6122,24 +6221,25 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(bb.Min, bb.Max, col, false, 0.0f); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); } + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); if (span_all_columns && window->DC.CurrentColumns) PopColumnsBackground(); else if (span_all_columns && g.CurrentTable) TablePopBackgroundChannel(); - if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); - if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor(); // Automatically close popups - if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup)) + if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.LastItemData.InFlags & ImGuiItemFlags_SelectableDontClosePopup)) CloseCurrentPopup(); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags); - return pressed; + if (disabled_item && !disabled_global) + EndDisabled(); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + return pressed; //-V1020 } bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) @@ -6271,8 +6371,9 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v PopID(); } EndListBox(); + if (value_changed) - MarkItemEdited(g.CurrentWindow->DC.LastItemId); + MarkItemEdited(g.LastItemData.ID); return value_changed; } @@ -6287,7 +6388,7 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v // Plot/Graph widgets are not very good. // Consider writing your own, or using a third-party one, see: // - ImPlot https://github.com/epezent/implot -// - others https://github.com/ocornut/imgui/wiki/Useful-Widgets +// - others https://github.com/ocornut/imgui/wiki/Useful-Extensions //------------------------------------------------------------------------- int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) @@ -6491,42 +6592,51 @@ void ImGui::Value(const char* prefix, float v, const char* float_format) // - EndMainMenuBar() // - BeginMenu() // - EndMenu() +// - MenuItemEx() [Internal] // - MenuItem() //------------------------------------------------------------------------- // Helpers for internal use -void ImGuiMenuColumns::Update(int count, float spacing, bool clear) +void ImGuiMenuColumns::Update(float spacing, bool window_reappearing) { - IM_ASSERT(count == IM_ARRAYSIZE(Pos)); - IM_UNUSED(count); - Width = NextWidth = 0.0f; - Spacing = spacing; - if (clear) - memset(NextWidths, 0, sizeof(NextWidths)); - for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) - { - if (i > 0 && NextWidths[i] > 0.0f) - Width += Spacing; - Pos[i] = IM_FLOOR(Width); - Width += NextWidths[i]; - NextWidths[i] = 0.0f; - } + if (window_reappearing) + memset(Widths, 0, sizeof(Widths)); + Spacing = (ImU16)spacing; + CalcNextTotalWidth(true); + memset(Widths, 0, sizeof(Widths)); + TotalWidth = NextTotalWidth; + NextTotalWidth = 0; } -float ImGuiMenuColumns::DeclColumns(float w0, float w1, float w2) // not using va_arg because they promote float to double +void ImGuiMenuColumns::CalcNextTotalWidth(bool update_offsets) { - NextWidth = 0.0f; - NextWidths[0] = ImMax(NextWidths[0], w0); - NextWidths[1] = ImMax(NextWidths[1], w1); - NextWidths[2] = ImMax(NextWidths[2], w2); - for (int i = 0; i < IM_ARRAYSIZE(Pos); i++) - NextWidth += NextWidths[i] + ((i > 0 && NextWidths[i] > 0.0f) ? Spacing : 0.0f); - return ImMax(Width, NextWidth); + ImU16 offset = 0; + bool want_spacing = false; + for (int i = 0; i < IM_ARRAYSIZE(Widths); i++) + { + ImU16 width = Widths[i]; + if (want_spacing && width > 0) + offset += Spacing; + want_spacing |= (width > 0); + if (update_offsets) + { + if (i == 1) { OffsetLabel = offset; } + if (i == 2) { OffsetShortcut = offset; } + if (i == 3) { OffsetMark = offset; } + } + offset += width; + } + NextTotalWidth = offset; } -float ImGuiMenuColumns::CalcExtraSpace(float avail_w) const +float ImGuiMenuColumns::DeclColumns(float w_icon, float w_label, float w_shortcut, float w_mark) { - return ImMax(0.0f, avail_w - Width); + Widths[0] = ImMax(Widths[0], (ImU16)w_icon); + Widths[1] = ImMax(Widths[1], (ImU16)w_label); + Widths[2] = ImMax(Widths[2], (ImU16)w_shortcut); + Widths[3] = ImMax(Widths[3], (ImU16)w_mark); + CalcNextTotalWidth(false); + return (float)ImMax(TotalWidth, NextTotalWidth); } // FIXME: Provided a rectangle perhaps e.g. a BeginMenuBarEx() could be used anywhere.. @@ -6579,7 +6689,7 @@ void ImGui::EndMenuBar() // To do so we claim focus back, restore NavId and then process the movement request for yet another frame. // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth the hassle/cost) const ImGuiNavLayer layer = ImGuiNavLayer_Menu; - IM_ASSERT(window->DC.NavLayerActiveMaskNext & (1 << layer)); // Sanity check + IM_ASSERT(window->DC.NavLayersActiveMaskNext & (1 << layer)); // Sanity check FocusWindow(window); SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. @@ -6589,11 +6699,12 @@ void ImGui::EndMenuBar() } } + IM_MSVC_WARNING_SUPPRESS(6011); // Static Analysis false positive "warning C6011: Dereferencing NULL pointer 'window'" IM_ASSERT(window->Flags & ImGuiWindowFlags_MenuBar); IM_ASSERT(window->DC.MenuBarAppending); PopClipRect(); PopID(); - window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->MenuBarRect().Min.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos. + window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->Pos.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos. g.GroupStack.back().EmitItem = false; EndGroup(); // Restore position on layer 0 window->DC.LayoutType = ImGuiLayoutType_Vertical; @@ -6601,46 +6712,63 @@ void ImGui::EndMenuBar() window->DC.MenuBarAppending = false; } -bool ImGui::BeginMainMenuBar() +// Important: calling order matters! +// FIXME: Somehow overlapping with docking tech. +// FIXME: The "rect-cut" aspect of this could be formalized into a lower-level helper (rect-cut: https://halt.software/dead-simple-layouts) +bool ImGui::BeginViewportSideBar(const char* name, ImGuiViewport* viewport_p, ImGuiDir dir, float axis_size, ImGuiWindowFlags window_flags) { - ImGuiContext& g = *GImGui; - ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); - ImGuiWindow* menu_bar_window = FindWindowByName("##MainMenuBar"); - - // For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. - g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); + IM_ASSERT(dir != ImGuiDir_None); - // Get our rectangle at the top of the work area - if (menu_bar_window == NULL || menu_bar_window->BeginCount == 0) + ImGuiWindow* bar_window = FindWindowByName(name); + if (bar_window == NULL || bar_window->BeginCount == 0) { - // Set window position - // We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. - // However menu-bar will affect the minimum window size so we'll get the right height. - ImVec2 menu_bar_pos = viewport->Pos + viewport->CurrWorkOffsetMin; - ImVec2 menu_bar_size = ImVec2(viewport->Size.x - viewport->CurrWorkOffsetMin.x + viewport->CurrWorkOffsetMax.x, 1.0f); - SetNextWindowPos(menu_bar_pos); - SetNextWindowSize(menu_bar_size); + // Calculate and set window size/position + ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)(viewport_p ? viewport_p : GetMainViewport()); + ImRect avail_rect = viewport->GetBuildWorkRect(); + ImGuiAxis axis = (dir == ImGuiDir_Up || dir == ImGuiDir_Down) ? ImGuiAxis_Y : ImGuiAxis_X; + ImVec2 pos = avail_rect.Min; + if (dir == ImGuiDir_Right || dir == ImGuiDir_Down) + pos[axis] = avail_rect.Max[axis] - axis_size; + ImVec2 size = avail_rect.GetSize(); + size[axis] = axis_size; + SetNextWindowPos(pos); + SetNextWindowSize(size); + + // Report our size into work area (for next frame) using actual window size + if (dir == ImGuiDir_Up || dir == ImGuiDir_Left) + viewport->BuildWorkOffsetMin[axis] += axis_size; + else if (dir == ImGuiDir_Down || dir == ImGuiDir_Right) + viewport->BuildWorkOffsetMax[axis] -= axis_size; } - // Create window + window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f); - PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); // Lift normal size constraint, however the presence of a menu-bar will give us the minimum height we want. - ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; - bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar(); + PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); // Lift normal size constraint + bool is_open = Begin(name, NULL, window_flags); PopStyleVar(2); - // Report our size into work area (for next frame) using actual window size - menu_bar_window = GetCurrentWindow(); - if (menu_bar_window->BeginCount == 1) - viewport->CurrWorkOffsetMin.y += menu_bar_window->Size.y; + return is_open; +} + +bool ImGui::BeginMainMenuBar() +{ + ImGuiContext& g = *GImGui; + ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); + // For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set. + // FIXME: This could be generalized as an opt-in way to clamp window->DC.CursorStartPos to avoid SafeArea? + // FIXME: Consider removing support for safe area down the line... it's messy. Nowadays consoles have support for TV calibration in OS settings. + g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f)); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar; + float height = GetFrameHeight(); + bool is_open = BeginViewportSideBar("##MainMenuBar", viewport, ImGuiDir_Up, height, window_flags); g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f); - if (!is_open) - { + + if (is_open) + BeginMenuBar(); + else End(); - return false; - } - return true; //-V1020 + return is_open; } void ImGui::EndMainMenuBar() @@ -6698,6 +6826,10 @@ bool ImGui::BeginMenu(const char* label, bool enabled) // However the final position is going to be different! It is chosen by FindBestWindowPosForPopup(). // e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering. ImVec2 popup_pos, pos = window->DC.CursorPos; + PushID(label); + if (!enabled) + BeginDisabled(); + const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Menu inside an horizontal menu bar @@ -6707,7 +6839,9 @@ bool ImGui::BeginMenu(const char* label, bool enabled) window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; - pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(w, 0.0f)); + ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); + pressed = Selectable("", menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups, ImVec2(w, 0.0f)); + RenderText(text_pos, label); PopStyleVar(); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } @@ -6717,14 +6851,19 @@ bool ImGui::BeginMenu(const char* label, bool enabled) // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f. // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); - float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, 0.0f, IM_FLOOR(g.FontSize * 1.20f)); // Feedback to next frame + float icon_w = 0.0f; // FIXME: This not currently exposed for BeginMenu() however you can call window->DC.MenuColumns.DeclColumns(w, 0, 0, 0) yourself + float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); + float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); - pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(min_w, 0.0f)); - ImU32 text_col = GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled); - RenderArrow(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right); + ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); + pressed = Selectable("", menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + RenderText(text_pos, label); + RenderArrow(window->DrawList, pos + ImVec2(offsets->OffsetMark + extra_w + g.FontSize * 0.30f, 0.0f), GetColorU32(ImGuiCol_Text), ImGuiDir_Right); } + if (!enabled) + EndDisabled(); - const bool hovered = enabled && ItemHoverable(window->DC.LastItemRect, id); + const bool hovered = (g.HoveredId == id) && enabled; if (menuset_is_open) g.NavWindow = backed_nav_window; @@ -6751,6 +6890,8 @@ bool ImGui::BeginMenu(const char* label, bool enabled) moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] } + + // FIXME: Hovering a disabled BeginMenu or MenuItem won't close us if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu) want_close = true; @@ -6794,7 +6935,8 @@ bool ImGui::BeginMenu(const char* label, bool enabled) if (want_close && IsPopupOpen(id, ImGuiPopupFlags_None)) ClosePopupToLevel(g.BeginPopupStack.Size, true); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); + PopID(); if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) { @@ -6836,7 +6978,7 @@ void ImGui::EndMenu() EndPopup(); } -bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled) +bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut, bool selected, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -6849,17 +6991,22 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73), // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only. - ImGuiSelectableFlags flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover | (enabled ? 0 : ImGuiSelectableFlags_Disabled); bool pressed; + PushID(label); + if (!enabled) + BeginDisabled(true); + const ImGuiSelectableFlags flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover; + const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful - // Note that in this situation we render neither the shortcut neither the selected tick mark + // Note that in this situation: we don't render the shortcut, we render a highlight instead of the selected tick mark. float w = label_size.x; window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); - pressed = Selectable(label, false, flags, ImVec2(w, 0.0f)); + pressed = Selectable("", selected, flags, ImVec2(w, 0.0f)); PopStyleVar(); + RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else @@ -6867,27 +7014,40 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo // Menu item inside a vertical menu // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f. // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. - float shortcut_w = shortcut ? CalcTextSize(shortcut, NULL).x : 0.0f; - float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_w, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame - float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); - pressed = Selectable(label, false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f; + float shortcut_w = (shortcut && shortcut[0]) ? CalcTextSize(shortcut, NULL).x : 0.0f; + float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); + float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame + float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); + pressed = Selectable("", false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); + if (icon_w > 0.0f) + RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); if (shortcut_w > 0.0f) { - PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); - RenderText(pos + ImVec2(window->DC.MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false); + PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); + RenderText(pos + ImVec2(offsets->OffsetShortcut + stretch_w, 0.0f), shortcut, NULL, false); PopStyleColor(); } if (selected) - RenderCheckMark(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize * 0.866f); + RenderCheckMark(window->DrawList, pos + ImVec2(offsets->OffsetMark + stretch_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(ImGuiCol_Text), g.FontSize * 0.866f); } + IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); + if (!enabled) + EndDisabled(); + PopID(); - IMGUI_TEST_ENGINE_ITEM_INFO(window->DC.LastItemId, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); return pressed; } +bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, bool enabled) +{ + return MenuItemEx(label, NULL, shortcut, selected, enabled); +} + bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled) { - if (MenuItem(label, shortcut, p_selected ? *p_selected : false, enabled)) + if (MenuItemEx(label, NULL, shortcut, p_selected ? *p_selected : false, enabled)) { if (p_selected) *p_selected = !*p_selected; @@ -6942,12 +7102,17 @@ ImGuiTabBar::ImGuiTabBar() LastTabItemIdx = -1; } +static inline int TabItemGetSectionIdx(const ImGuiTabItem* tab) +{ + return (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; +} + static int IMGUI_CDECL TabItemComparerBySection(const void* lhs, const void* rhs) { const ImGuiTabItem* a = (const ImGuiTabItem*)lhs; const ImGuiTabItem* b = (const ImGuiTabItem*)rhs; - const int a_section = (a->Flags & ImGuiTabItemFlags_Leading) ? 0 : (a->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; - const int b_section = (b->Flags & ImGuiTabItemFlags_Leading) ? 0 : (b->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + const int a_section = TabItemGetSectionIdx(a); + const int b_section = TabItemGetSectionIdx(b); if (a_section != b_section) return a_section - b_section; return (int)(a->IndexDuringLayout - b->IndexDuringLayout); @@ -7116,11 +7281,11 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) tab->IndexDuringLayout = (ImS16)tab_dst_n; // We will need sorting if tabs have changed section (e.g. moved from one of Leading/Central/Trailing to another) - int curr_tab_section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + int curr_tab_section_n = TabItemGetSectionIdx(tab); if (tab_dst_n > 0) { ImGuiTabItem* prev_tab = &tab_bar->Tabs[tab_dst_n - 1]; - int prev_tab_section_n = (prev_tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (prev_tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + int prev_tab_section_n = TabItemGetSectionIdx(prev_tab); if (curr_tab_section_n == 0 && prev_tab_section_n != 0) need_sort_by_section = true; if (prev_tab_section_n == 2 && curr_tab_section_n != 2) @@ -7192,12 +7357,13 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) const bool has_close_button = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) ? false : true; tab->ContentWidth = TabItemCalcSize(tab_name, has_close_button).x; - int section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + int section_n = TabItemGetSectionIdx(tab); ImGuiTabBarSection* section = §ions[section_n]; section->Width += tab->ContentWidth + (section_n == curr_section_n ? g.Style.ItemInnerSpacing.x : 0.0f); curr_section_n = section_n; // Store data so we can build an array sorted by width if we need to shrink tabs down + IM_MSVC_WARNING_SUPPRESS(6385); int shrink_buffer_index = shrink_buffer_indexes[section_n]++; g.ShrinkWidthBuffer[shrink_buffer_index].Index = tab_n; g.ShrinkWidthBuffer[shrink_buffer_index].Width = tab->ContentWidth; @@ -7247,7 +7413,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) if (shrinked_width < 0.0f) continue; - int section_n = (tab->Flags & ImGuiTabItemFlags_Leading) ? 0 : (tab->Flags & ImGuiTabItemFlags_Trailing) ? 2 : 1; + int section_n = TabItemGetSectionIdx(tab); sections[section_n].Width -= (tab->Width - shrinked_width); tab->Width = shrinked_width; } @@ -7392,7 +7558,7 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGui ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id); if (tab == NULL) return; - if (tab->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) + if (tab->Flags & ImGuiTabItemFlags_SectionMask_) return; ImGuiContext& g = *GImGui; @@ -7421,12 +7587,48 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGui } } -void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int dir) +void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int offset) { - IM_ASSERT(dir == -1 || dir == +1); + IM_ASSERT(offset != 0); IM_ASSERT(tab_bar->ReorderRequestTabId == 0); tab_bar->ReorderRequestTabId = tab->ID; - tab_bar->ReorderRequestDir = (ImS8)dir; + tab_bar->ReorderRequestOffset = (ImS16)offset; +} + +void ImGui::TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, const ImGuiTabItem* src_tab, ImVec2 mouse_pos) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(tab_bar->ReorderRequestTabId == 0); + if ((tab_bar->Flags & ImGuiTabBarFlags_Reorderable) == 0) + return; + + const bool is_central_section = (src_tab->Flags & ImGuiTabItemFlags_SectionMask_) == 0; + const float bar_offset = tab_bar->BarRect.Min.x - (is_central_section ? tab_bar->ScrollingTarget : 0); + + // Count number of contiguous tabs we are crossing over + const int dir = (bar_offset + src_tab->Offset) > mouse_pos.x ? -1 : +1; + const int src_idx = tab_bar->Tabs.index_from_ptr(src_tab); + int dst_idx = src_idx; + for (int i = src_idx; i >= 0 && i < tab_bar->Tabs.Size; i += dir) + { + // Reordered tabs must share the same section + const ImGuiTabItem* dst_tab = &tab_bar->Tabs[i]; + if (dst_tab->Flags & ImGuiTabItemFlags_NoReorder) + break; + if ((dst_tab->Flags & ImGuiTabItemFlags_SectionMask_) != (src_tab->Flags & ImGuiTabItemFlags_SectionMask_)) + break; + dst_idx = i; + + // Include spacing after tab, so when mouse cursor is between tabs we would not continue checking further tabs that are not hovered. + const float x1 = bar_offset + dst_tab->Offset - g.Style.ItemInnerSpacing.x; + const float x2 = bar_offset + dst_tab->Offset + dst_tab->Width + g.Style.ItemInnerSpacing.x; + //GetForegroundDrawList()->AddRect(ImVec2(x1, tab_bar->BarRect.Min.y), ImVec2(x2, tab_bar->BarRect.Max.y), IM_COL32(255, 0, 0, 255)); + if ((dir < 0 && mouse_pos.x > x1) || (dir > 0 && mouse_pos.x < x2)) + break; + } + + if (dst_idx != src_idx) + TabBarQueueReorder(tab_bar, src_tab, dst_idx - src_idx); } bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) @@ -7436,19 +7638,23 @@ bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) return false; //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools - int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestDir; + int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestOffset; if (tab2_order < 0 || tab2_order >= tab_bar->Tabs.Size) return false; - // Reordered TabItem must share the same position flags than target + // Reordered tabs must share the same section + // (Note: TabBarQueueReorderFromMousePos() also has a similar test but since we allow direct calls to TabBarQueueReorder() we do it here too) ImGuiTabItem* tab2 = &tab_bar->Tabs[tab2_order]; if (tab2->Flags & ImGuiTabItemFlags_NoReorder) return false; - if ((tab1->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) != (tab2->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing))) + if ((tab1->Flags & ImGuiTabItemFlags_SectionMask_) != (tab2->Flags & ImGuiTabItemFlags_SectionMask_)) return false; ImGuiTabItem item_tmp = *tab1; - *tab1 = *tab2; + ImGuiTabItem* src_tab = (tab_bar->ReorderRequestOffset > 0) ? tab1 + 1 : tab2; + ImGuiTabItem* dst_tab = (tab_bar->ReorderRequestOffset > 0) ? tab1 : tab2 + 1; + const int move_count = (tab_bar->ReorderRequestOffset > 0) ? tab_bar->ReorderRequestOffset : -tab_bar->ReorderRequestOffset; + memmove(dst_tab, src_tab, move_count * sizeof(ImGuiTabItem)); *tab2 = item_tmp; if (tab_bar->Flags & ImGuiTabBarFlags_SaveSettings) @@ -7643,7 +7849,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // If the user called us with *p_open == false, we early out and don't render. // We make a call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); if (p_open && !*p_open) { PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); @@ -7688,7 +7894,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab->Flags = flags; // Append name with zero-terminator - tab->NameOffset = (ImS16)tab_bar->TabsNames.size(); + tab->NameOffset = (ImS32)tab_bar->TabsNames.size(); tab_bar->TabsNames.append(label, label + strlen(label) + 1); // Update selected tab @@ -7730,7 +7936,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const ImVec2 backup_main_cursor_pos = window->DC.CursorPos; // Layout - const bool is_central_section = (tab->Flags & (ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing)) == 0; + const bool is_central_section = (tab->Flags & ImGuiTabItemFlags_SectionMask_) == 0; size.x = tab->Width; if (is_central_section) window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_FLOOR(tab->Offset - tab_bar->ScrollingAnim), 0.0f); @@ -7764,7 +7970,6 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); if (pressed && !is_tab_button) tab_bar->NextSelectedTabId = id; - hovered |= (g.HoveredId == id); // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) if (g.ActiveId != id) @@ -7778,13 +7983,11 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // While moving a tab it will jump on the other side of the mouse, so we also test for MouseDelta.x if (g.IO.MouseDelta.x < 0.0f && g.IO.MousePos.x < bb.Min.x) { - if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) - TabBarQueueReorder(tab_bar, tab, -1); + TabBarQueueReorderFromMousePos(tab_bar, tab, g.IO.MousePos); } else if (g.IO.MouseDelta.x > 0.0f && g.IO.MousePos.x > bb.Max.x) { - if (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) - TabBarQueueReorder(tab_bar, tab, +1); + TabBarQueueReorderFromMousePos(tab_bar, tab, g.IO.MousePos); } } } @@ -7830,8 +8033,11 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, PopClipRect(); window->DC.CursorPos = backup_main_cursor_pos; - // Tooltip (FIXME: Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer) - // We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar (which g.HoveredId ignores) + // Tooltip + // (Won't work over the close button because ItemOverlap systems messes up with HoveredIdTimer-> seems ok) + // (We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar, which g.HoveredId ignores) + // FIXME: This is a mess. + // FIXME: We may want disabled tab to still display the tooltip? if (text_clipped && g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay && IsItemHovered()) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); @@ -7919,14 +8125,7 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, #endif // Render text label (with clipping + alpha gradient) + unsaved marker - const char* TAB_UNSAVED_MARKER = "*"; ImRect text_pixel_clip_bb(bb.Min.x + frame_padding.x, bb.Min.y + frame_padding.y, bb.Max.x - frame_padding.x, bb.Max.y); - if (flags & ImGuiTabItemFlags_UnsavedDocument) - { - text_pixel_clip_bb.Max.x -= CalcTextSize(TAB_UNSAVED_MARKER, NULL, false).x; - ImVec2 unsaved_marker_pos(ImMin(bb.Min.x + frame_padding.x + label_size.x + 2, text_pixel_clip_bb.Max.x), bb.Min.y + frame_padding.y + IM_FLOOR(-g.FontSize * 0.25f)); - RenderTextClippedEx(draw_list, unsaved_marker_pos, bb.Max - frame_padding, TAB_UNSAVED_MARKER, NULL, NULL); - } ImRect text_ellipsis_clip_bb = text_pixel_clip_bb; // Return clipped state ignoring the close button @@ -7936,7 +8135,10 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, //draw_list->AddCircle(text_ellipsis_clip_bb.Min, 3.0f, *out_text_clipped ? IM_COL32(255, 0, 0, 255) : IM_COL32(0, 255, 0, 255)); } - // Close Button + const float button_sz = g.FontSize; + const ImVec2 button_pos(ImMax(bb.Min.x, bb.Max.x - frame_padding.x * 2.0f - button_sz), bb.Min.y); + + // Close Button & Unsaved Marker // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap() // 'hovered' will be true when hovering the Tab but NOT when hovering the close button // 'g.HoveredId==id' will be true when hovering the Tab including when hovering the close button @@ -7944,28 +8146,40 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, bool close_button_pressed = false; bool close_button_visible = false; if (close_button_id != 0) - if (is_contents_visible || bb.GetWidth() >= g.Style.TabMinWidthForCloseButton) + if (is_contents_visible || bb.GetWidth() >= ImMax(button_sz, g.Style.TabMinWidthForCloseButton)) if (g.HoveredId == tab_id || g.HoveredId == close_button_id || g.ActiveId == tab_id || g.ActiveId == close_button_id) close_button_visible = true; + bool unsaved_marker_visible = (flags & ImGuiTabItemFlags_UnsavedDocument) != 0 && (button_pos.x + button_sz <= bb.Max.x); + if (close_button_visible) { - ImGuiLastItemDataBackup last_item_backup; - const float close_button_sz = g.FontSize; + ImGuiLastItemData last_item_backup = g.LastItemData; PushStyleVar(ImGuiStyleVar_FramePadding, frame_padding); - if (CloseButton(close_button_id, ImVec2(bb.Max.x - frame_padding.x * 2.0f - close_button_sz, bb.Min.y))) + if (CloseButton(close_button_id, button_pos)) close_button_pressed = true; PopStyleVar(); - last_item_backup.Restore(); + g.LastItemData = last_item_backup; // Close with middle mouse button if (!(flags & ImGuiTabItemFlags_NoCloseWithMiddleMouseButton) && IsMouseClicked(2)) close_button_pressed = true; - - text_pixel_clip_bb.Max.x -= close_button_sz; + } + else if (unsaved_marker_visible) + { + const ImRect bullet_bb(button_pos, button_pos + ImVec2(button_sz, button_sz) + g.Style.FramePadding * 2.0f); + RenderBullet(draw_list, bullet_bb.GetCenter(), GetColorU32(ImGuiCol_Text)); } + // This is all rather complicated + // (the main idea is that because the close button only appears on hover, we don't want it to alter the ellipsis position) // FIXME: if FramePadding is noticeably large, ellipsis_max_x will be wrong here (e.g. #3497), maybe for consistency that parameter of RenderTextEllipsis() shouldn't exist.. float ellipsis_max_x = close_button_visible ? text_pixel_clip_bb.Max.x : bb.Max.x - 1.0f; + if (close_button_visible || unsaved_marker_visible) + { + text_pixel_clip_bb.Max.x -= close_button_visible ? (button_sz) : (button_sz * 0.80f); + text_ellipsis_clip_bb.Max.x -= unsaved_marker_visible ? (button_sz * 0.80f) : 0.0f; + ellipsis_max_x = text_pixel_clip_bb.Max.x; + } RenderTextEllipsis(draw_list, text_ellipsis_clip_bb.Min, text_ellipsis_clip_bb.Max, text_pixel_clip_bb.Max.x, ellipsis_max_x, label, NULL, &label_size); #if 0 diff --git a/imgui/imstb_rectpack.h b/imgui/imstb_rectpack.h index ff2a85df..39589521 100644 --- a/imgui/imstb_rectpack.h +++ b/imgui/imstb_rectpack.h @@ -34,7 +34,7 @@ // Minor features // Martins Mozeiko // github:IntellectualKitty -// +// // Bugfixes / warning fixes // Jeremy Jaussaud // Fabian Giesen @@ -441,7 +441,7 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt } } tail = tail->next; - } + } } fr.prev_link = best; @@ -602,38 +602,38 @@ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. -Anyone is free to copy, modify, publish, use, compile, sell, or distribute this -software, either in source code form or as a compiled binary, for any purpose, +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. -In jurisdictions that recognize copyright laws, the author or authors of this -software dedicate any and all copyright interest in the software to the public -domain. We make this dedication for the benefit of the public at large and to -the detriment of our heirs and successors. We intend this dedication to be an -overt act of relinquishment in perpetuity of all present and future rights to +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ diff --git a/imgui/imstb_textedit.h b/imgui/imstb_textedit.h index 76446709..2c635b27 100644 --- a/imgui/imstb_textedit.h +++ b/imgui/imstb_textedit.h @@ -1,5 +1,5 @@ // [DEAR IMGUI] -// This is a slightly modified version of stb_textedit.h 1.13. +// This is a slightly modified version of stb_textedit.h 1.13. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) // Grep for [DEAR IMGUI] to find the changes. @@ -19,7 +19,7 @@ // texts, as its performance does not scale and it has limited undo). // // Non-trivial behaviors are modelled after Windows text controls. -// +// // // LICENSE // @@ -217,20 +217,20 @@ // call this with the mouse x,y on a mouse down; it will update the cursor // and reset the selection start/end to the cursor point. the x,y must // be relative to the text widget, with (0,0) being the top left. -// +// // drag: // call this with the mouse x,y on a mouse drag/up; it will update the // cursor and the selection end point -// +// // cut: // call this to delete the current selection; returns true if there was // one. you should FIRST copy the current selection to the system paste buffer. // (To copy, just copy the current selection out of the string yourself.) -// +// // paste: // call this to paste text at the current cursor point or over the current // selection if there is one. -// +// // key: // call this for keyboard inputs sent to the textfield. you can use it // for "key down" events or for "translated" key events. if you need to @@ -241,7 +241,7 @@ // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to // anything other type you wante before including. // -// +// // When rendering, you can read the cursor position and selection state from // the STB_TexteditState. // @@ -716,9 +716,11 @@ static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditSta state->has_preferred_x = 0; return 1; } - // remove the undo since we didn't actually insert the characters - if (state->undostate.undo_point) - --state->undostate.undo_point; + // [DEAR IMGUI] + //// remove the undo since we didn't actually insert the characters + //if (state->undostate.undo_point) + // --state->undostate.undo_point; + // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details) return 0; } @@ -764,7 +766,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, state->insert_mode = !state->insert_mode; break; #endif - + case STB_TEXTEDIT_K_UNDO: stb_text_undo(str, state); state->has_preferred_x = 0; @@ -779,7 +781,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, // if currently there's a selection, move cursor to start of selection if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_first(state); - else + else if (state->cursor > 0) --state->cursor; state->has_preferred_x = 0; @@ -828,7 +830,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, #ifdef STB_TEXTEDIT_MOVEWORDRIGHT case STB_TEXTEDIT_K_WORDRIGHT: - if (STB_TEXT_HAS_SELECTION(state)) + if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_last(str, state); else { state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor); @@ -922,7 +924,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, } break; } - + case STB_TEXTEDIT_K_UP: case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT: case STB_TEXTEDIT_K_PGUP: @@ -1014,7 +1016,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, } state->has_preferred_x = 0; break; - + #ifdef STB_TEXTEDIT_K_TEXTSTART2 case STB_TEXTEDIT_K_TEXTSTART2: #endif @@ -1031,7 +1033,7 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, state->select_start = state->select_end = 0; state->has_preferred_x = 0; break; - + #ifdef STB_TEXTEDIT_K_TEXTSTART2 case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT: #endif @@ -1410,38 +1412,38 @@ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. -Anyone is free to copy, modify, publish, use, compile, sell, or distribute this -software, either in source code form or as a compiled binary, for any purpose, +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. -In jurisdictions that recognize copyright laws, the author or authors of this -software dedicate any and all copyright interest in the software to the public -domain. We make this dedication for the benefit of the public at large and to -the detriment of our heirs and successors. We intend this dedication to be an -overt act of relinquishment in perpetuity of all present and future rights to +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ diff --git a/imgui/imstb_truetype.h b/imgui/imstb_truetype.h index fc815d74..48c20261 100644 --- a/imgui/imstb_truetype.h +++ b/imgui/imstb_truetype.h @@ -51,7 +51,7 @@ // Rob Loach Cort Stratton // Kenney Phillis Jr. github:oyvindjam // Brian Costabile github:vassvik -// +// // VERSION HISTORY // // 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() @@ -212,7 +212,7 @@ // // Advancing for the next character: // Call GlyphHMetrics, and compute 'current_point += SF * advance'. -// +// // // ADVANCED USAGE // @@ -257,7 +257,7 @@ // Curve tessellation 120 LOC \__ 550 LOC Bitmap creation // Bitmap management 100 LOC / // Baked bitmap interface 70 LOC / -// Font name matching & access 150 LOC ---- 150 +// Font name matching & access 150 LOC ---- 150 // C runtime library abstraction 60 LOC ---- 60 // // @@ -350,7 +350,7 @@ int main(int argc, char **argv) } return 0; } -#endif +#endif // // Output: // @@ -364,9 +364,9 @@ int main(int argc, char **argv) // :@@. M@M // @@@o@@@@ // :M@@V:@@. -// +// ////////////////////////////////////////////////////////////////////////////// -// +// // Complete program: print "Hello World!" banner, with bugs // #if 0 @@ -667,7 +667,7 @@ STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, cons // Calling these functions in sequence is roughly equivalent to calling // stbtt_PackFontRanges(). If you more control over the packing of multiple // fonts, or if you want to pack custom data into a font texture, take a look -// at the source to of stbtt_PackFontRanges() and create a custom version +// at the source to of stbtt_PackFontRanges() and create a custom version // using these functions, e.g. call GatherRects multiple times, // building up a single array of rects, then call PackRects once, // then call RenderIntoRects repeatedly. This may result in a @@ -975,7 +975,7 @@ STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, floa // and computing from that can allow drop-out prevention). // // The algorithm has not been optimized at all, so expect it to be slow -// if computing lots of characters or very large sizes. +// if computing lots of characters or very large sizes. @@ -1732,7 +1732,7 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s if (i != 0) num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); - // now start the new one + // now start the new one start_off = !(flags & 1); if (start_off) { // if we start off with an off-curve point, then when we need to find a point on the curve @@ -1785,7 +1785,7 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s int comp_num_verts = 0, i; stbtt_vertex *comp_verts = 0, *tmp = 0; float mtx[6] = {1,0,0,1,0,0}, m, n; - + flags = ttSHORT(comp); comp+=2; gidx = ttSHORT(comp); comp+=2; @@ -1815,7 +1815,7 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s mtx[2] = ttSHORT(comp)/16384.0f; comp+=2; mtx[3] = ttSHORT(comp)/16384.0f; comp+=2; } - + // Find transformation scales. m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]); n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]); @@ -2746,7 +2746,7 @@ static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, i float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0); STBTT_assert(z != NULL); if (!z) return z; - + // round dx down to avoid overshooting if (dxdy < 0) z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy); @@ -2824,7 +2824,7 @@ static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__ac } } } - + e = e->next; } } @@ -3554,7 +3554,7 @@ STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info { int ix0,iy0,ix1,iy1; stbtt__bitmap gbm; - stbtt_vertex *vertices; + stbtt_vertex *vertices; int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); if (scale_x == 0) scale_x = scale_y; @@ -3577,7 +3577,7 @@ STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info if (height) *height = gbm.h; if (xoff ) *xoff = ix0; if (yoff ) *yoff = iy0; - + if (gbm.w && gbm.h) { gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata); if (gbm.pixels) { @@ -3588,7 +3588,7 @@ STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info } STBTT_free(vertices, info->userdata); return gbm.pixels; -} +} STBTT_DEF unsigned char *stbtt_GetGlyphBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int glyph, int *width, int *height, int *xoff, int *yoff) { @@ -3600,7 +3600,7 @@ STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo *info, unsigne int ix0,iy0; stbtt_vertex *vertices; int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices); - stbtt__bitmap gbm; + stbtt__bitmap gbm; stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0); gbm.pixels = output; @@ -3622,7 +3622,7 @@ STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo *info, unsigned char * STBTT_DEF unsigned char *stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo *info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int *width, int *height, int *xoff, int *yoff) { return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff); -} +} STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float *sub_x, float *sub_y, int codepoint) { @@ -3637,7 +3637,7 @@ STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo *info, uns STBTT_DEF unsigned char *stbtt_GetCodepointBitmap(const stbtt_fontinfo *info, float scale_x, float scale_y, int codepoint, int *width, int *height, int *xoff, int *yoff) { return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff); -} +} STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo *info, unsigned char *output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint) { @@ -3762,7 +3762,7 @@ static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *no con->y = 0; con->bottom_y = 0; STBTT__NOTUSED(nodes); - STBTT__NOTUSED(num_nodes); + STBTT__NOTUSED(num_nodes); } static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects) @@ -4147,7 +4147,7 @@ STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char n = 0; for (i=0; i < num_ranges; ++i) n += ranges[i].num_chars; - + rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context); if (rects == NULL) return 0; @@ -4158,7 +4158,7 @@ STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects); stbtt_PackFontRangesPackRects(spc, rects, n); - + return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects); STBTT_free(rects, spc->user_allocator_context); @@ -4319,7 +4319,7 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex int x1 = (int) verts[i ].x, y1 = (int) verts[i ].y; if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; - if (x_inter < x) + if (x_inter < x) winding += (y0 < y1) ? 1 : -1; } } @@ -4345,7 +4345,7 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex y1 = (int)verts[i ].y; if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) { float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0; - if (x_inter < x) + if (x_inter < x) winding += (y0 < y1) ? 1 : -1; } } else { @@ -4357,7 +4357,7 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex if (hits[1][0] < 0) winding += (hits[1][1] < 0 ? -1 : 1); } - } + } } } return winding; @@ -4438,7 +4438,7 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc // invert for y-downwards bitmaps scale_y = -scale_y; - + { int x,y,i,j; float *precompute; @@ -4587,7 +4587,7 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc STBTT_free(verts, info->userdata); } return data; -} +} STBTT_DEF unsigned char * stbtt_GetCodepointSDF(const stbtt_fontinfo *info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int *width, int *height, int *xoff, int *yoff) { @@ -4605,7 +4605,7 @@ STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata) // // check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string -static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2) +static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2) { stbtt_int32 i=0; @@ -4644,7 +4644,7 @@ static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, s return i; } -static int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2) +static int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2) { return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2); } @@ -4773,7 +4773,7 @@ STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index) { - return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index); + return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index); } STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data) @@ -4866,38 +4866,38 @@ This software is available under 2 licenses -- choose whichever you prefer. ------------------------------------------------------------------------------ ALTERNATIVE A - MIT License Copyright (c) 2017 Sean Barrett -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ ALTERNATIVE B - Public Domain (www.unlicense.org) This is free and unencumbered software released into the public domain. -Anyone is free to copy, modify, publish, use, compile, sell, or distribute this -software, either in source code form or as a compiled binary, for any purpose, +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. -In jurisdictions that recognize copyright laws, the author or authors of this -software dedicate any and all copyright interest in the software to the public -domain. We make this dedication for the benefit of the public at large and to -the detriment of our heirs and successors. We intend this dedication to be an -overt act of relinquishment in perpetuity of all present and future rights to +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ */ diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a818d7b8..f4d34af3 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -15,9 +15,9 @@ #include "viewer/include_opengl.h" -#include "imgui.h" -#include "imgui_impl_glfw.h" -#include "imgui_impl_opengl3.h" +#include +#include +#include #ifdef GPROSHAN_FLOAT From cec5b7ef3739260a8e2b29532932ceeb59021e0e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 31 Aug 2021 13:14:56 +0200 Subject: [PATCH 0545/1018] creating apps subdirectory --- CMakeLists.txt | 22 +- apps/CMakeLists.txt | 22 + gproshan.cpp => apps/gproshan.cpp | 0 test_geodesics.cpp => apps/test_geodesics.cpp | 0 .../test_image_denoising.cpp | 0 imgui/imgui_impl_opengl3_loader.h | 730 ++++++++++++++++++ 6 files changed, 753 insertions(+), 21 deletions(-) create mode 100644 apps/CMakeLists.txt rename gproshan.cpp => apps/gproshan.cpp (100%) rename test_geodesics.cpp => apps/test_geodesics.cpp (100%) rename test_image_denoising.cpp => apps/test_image_denoising.cpp (100%) create mode 100644 imgui/imgui_impl_opengl3_loader.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 39562d1a..c5489ca2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -108,24 +108,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -add_executable(test_gproshan gproshan.cpp) -set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) -target_link_libraries(test_gproshan gproshan) - -if(OptiX_INCLUDE) - add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) - set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - add_custom_target(ptx_cp_sources DEPENDS ptx_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) - add_dependencies(test_gproshan ptx_cp_sources) -endif(OptiX_INCLUDE) - - -add_executable(test_geodesics test_geodesics.cpp) -target_link_libraries(test_geodesics gproshan) - - -add_executable(test_image_denoising test_image_denoising.cpp) -target_link_libraries(test_image_denoising gproshan) +add_subdirectory(apps) install(TARGETS gproshan imgui test_gproshan test_geodesics test_image_denoising @@ -133,6 +116,3 @@ install(TARGETS gproshan imgui test_gproshan test_geodesics test_image_denoising LIBRARY DESTINATION ${gproshan_SOURCE_DIR}/lib RUNTIME DESTINATION ${gproshan_SOURCE_DIR}/bin) - -file(MAKE_DIRECTORY tmp) - diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt new file mode 100644 index 00000000..8ea3fc99 --- /dev/null +++ b/apps/CMakeLists.txt @@ -0,0 +1,22 @@ +add_executable(test_gproshan gproshan.cpp) +set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) +target_link_libraries(test_gproshan gproshan) + +if(OptiX_INCLUDE) + add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) + add_custom_target(ptx_cp_sources DEPENDS ptx_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) + add_dependencies(test_gproshan ptx_cp_sources) +endif(OptiX_INCLUDE) + + +add_executable(test_geodesics test_geodesics.cpp) +target_link_libraries(test_geodesics gproshan) + + +add_executable(test_image_denoising test_image_denoising.cpp) +target_link_libraries(test_image_denoising gproshan) + + +file(MAKE_DIRECTORY ${gproshan_SOURCE_DIR}/tmp) + diff --git a/gproshan.cpp b/apps/gproshan.cpp similarity index 100% rename from gproshan.cpp rename to apps/gproshan.cpp diff --git a/test_geodesics.cpp b/apps/test_geodesics.cpp similarity index 100% rename from test_geodesics.cpp rename to apps/test_geodesics.cpp diff --git a/test_image_denoising.cpp b/apps/test_image_denoising.cpp similarity index 100% rename from test_image_denoising.cpp rename to apps/test_image_denoising.cpp diff --git a/imgui/imgui_impl_opengl3_loader.h b/imgui/imgui_impl_opengl3_loader.h new file mode 100644 index 00000000..021921ea --- /dev/null +++ b/imgui/imgui_impl_opengl3_loader.h @@ -0,0 +1,730 @@ +/* + * This file was generated with gl3w_gen.py, part of imgl3w + * (hosted at https://github.com/dearimgui/gl3w_stripped) + * + * This is free and unencumbered software released into the public domain. + * + * Anyone is free to copy, modify, publish, use, compile, sell, or + * distribute this software, either in source code form or as a compiled + * binary, for any purpose, commercial or non-commercial, and by any + * means. + * + * In jurisdictions that recognize copyright laws, the author or authors + * of this software dedicate any and all copyright interest in the + * software to the public domain. We make this dedication for the benefit + * of the public at large and to the detriment of our heirs and + * successors. We intend this dedication to be an overt act of + * relinquishment in perpetuity of all present and future rights to this + * software under copyright law. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +// We embed our own OpenGL loader to not require user to provide their own or to have to use ours, which proved to be endless problems for users. +// Our loader is custom-generated, based on gl3w but automatically filtered to only include enums/functions that we use in this source file. +// Regenerate with: python gl3w_gen.py --imgui-dir /path/to/imgui/ +// see https://github.com/dearimgui/gl3w_stripped for more info. +#ifndef __gl3w_h_ +#define __gl3w_h_ + +// Adapted from KHR/khrplatform.h to avoid including entire file. +typedef float khronos_float_t; +typedef signed char khronos_int8_t; +typedef unsigned char khronos_uint8_t; +typedef signed short int khronos_int16_t; +typedef unsigned short int khronos_uint16_t; +#ifdef _WIN64 +typedef signed long long int khronos_intptr_t; +typedef signed long long int khronos_ssize_t; +#else +typedef signed long int khronos_intptr_t; +typedef signed long int khronos_ssize_t; +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +typedef signed __int64 khronos_int64_t; +typedef unsigned __int64 khronos_uint64_t; +#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100) +#include +typedef int64_t khronos_int64_t; +typedef uint64_t khronos_uint64_t; +#else +typedef signed long long khronos_int64_t; +typedef unsigned long long khronos_uint64_t; +#endif + +#ifndef __gl_glcorearb_h_ +#define __gl_glcorearb_h_ 1 +#ifdef __cplusplus +extern "C" { +#endif +/* +** Copyright 2013-2020 The Khronos Group Inc. +** SPDX-License-Identifier: MIT +** +** This header is generated from the Khronos OpenGL / OpenGL ES XML +** API Registry. The current version of the Registry, generator scripts +** used to make the header, and the header can be found at +** https://github.com/KhronosGroup/OpenGL-Registry +*/ +#if defined(_WIN32) && !defined(APIENTRY) && !defined(__CYGWIN__) && !defined(__SCITECH_SNAP__) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#include +#endif +#ifndef APIENTRY +#define APIENTRY +#endif +#ifndef APIENTRYP +#define APIENTRYP APIENTRY * +#endif +#ifndef GLAPI +#define GLAPI extern +#endif +/* glcorearb.h is for use with OpenGL core profile implementations. +** It should should be placed in the same directory as gl.h and +** included as . +** +** glcorearb.h includes only APIs in the latest OpenGL core profile +** implementation together with APIs in newer ARB extensions which +** can be supported by the core profile. It does not, and never will +** include functionality removed from the core profile, such as +** fixed-function vertex and fragment processing. +** +** Do not #include both and either of or +** in the same source file. +*/ +/* Generated C header for: + * API: gl + * Profile: core + * Versions considered: .* + * Versions emitted: .* + * Default extensions included: glcore + * Additional extensions included: _nomatch_^ + * Extensions removed: _nomatch_^ + */ +#ifndef GL_VERSION_1_0 +typedef void GLvoid; +typedef unsigned int GLenum; + +typedef khronos_float_t GLfloat; +typedef int GLint; +typedef int GLsizei; +typedef unsigned int GLbitfield; +typedef double GLdouble; +typedef unsigned int GLuint; +typedef unsigned char GLboolean; +typedef khronos_uint8_t GLubyte; +#define GL_COLOR_BUFFER_BIT 0x00004000 +#define GL_FALSE 0 +#define GL_TRUE 1 +#define GL_TRIANGLES 0x0004 +#define GL_ONE 1 +#define GL_SRC_ALPHA 0x0302 +#define GL_ONE_MINUS_SRC_ALPHA 0x0303 +#define GL_FRONT_AND_BACK 0x0408 +#define GL_POLYGON_MODE 0x0B40 +#define GL_CULL_FACE 0x0B44 +#define GL_DEPTH_TEST 0x0B71 +#define GL_STENCIL_TEST 0x0B90 +#define GL_VIEWPORT 0x0BA2 +#define GL_BLEND 0x0BE2 +#define GL_SCISSOR_BOX 0x0C10 +#define GL_SCISSOR_TEST 0x0C11 +#define GL_UNPACK_ROW_LENGTH 0x0CF2 +#define GL_PACK_ALIGNMENT 0x0D05 +#define GL_TEXTURE_2D 0x0DE1 +#define GL_UNSIGNED_BYTE 0x1401 +#define GL_UNSIGNED_SHORT 0x1403 +#define GL_UNSIGNED_INT 0x1405 +#define GL_FLOAT 0x1406 +#define GL_RGBA 0x1908 +#define GL_FILL 0x1B02 +#define GL_VERSION 0x1F02 +#define GL_EXTENSIONS 0x1F03 +#define GL_LINEAR 0x2601 +#define GL_TEXTURE_MAG_FILTER 0x2800 +#define GL_TEXTURE_MIN_FILTER 0x2801 +typedef void (APIENTRYP PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode); +typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height); +typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param); +typedef void (APIENTRYP PFNGLTEXIMAGE2DPROC) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels); +typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask); +typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); +typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap); +typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap); +typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param); +typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); +typedef void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *data); +typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGPROC) (GLenum name); +typedef GLboolean (APIENTRYP PFNGLISENABLEDPROC) (GLenum cap); +typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glPolygonMode (GLenum face, GLenum mode); +GLAPI void APIENTRY glScissor (GLint x, GLint y, GLsizei width, GLsizei height); +GLAPI void APIENTRY glTexParameteri (GLenum target, GLenum pname, GLint param); +GLAPI void APIENTRY glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels); +GLAPI void APIENTRY glClear (GLbitfield mask); +GLAPI void APIENTRY glClearColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); +GLAPI void APIENTRY glDisable (GLenum cap); +GLAPI void APIENTRY glEnable (GLenum cap); +GLAPI void APIENTRY glPixelStorei (GLenum pname, GLint param); +GLAPI void APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); +GLAPI void APIENTRY glGetIntegerv (GLenum pname, GLint *data); +GLAPI const GLubyte *APIENTRY glGetString (GLenum name); +GLAPI GLboolean APIENTRY glIsEnabled (GLenum cap); +GLAPI void APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei height); +#endif +#endif /* GL_VERSION_1_0 */ +#ifndef GL_VERSION_1_1 +typedef khronos_float_t GLclampf; +typedef double GLclampd; +#define GL_TEXTURE_BINDING_2D 0x8069 +typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices); +typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture); +typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures); +typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices); +GLAPI void APIENTRY glBindTexture (GLenum target, GLuint texture); +GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures); +GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures); +#endif +#endif /* GL_VERSION_1_1 */ +#ifndef GL_VERSION_1_3 +#define GL_TEXTURE0 0x84C0 +#define GL_ACTIVE_TEXTURE 0x84E0 +typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glActiveTexture (GLenum texture); +#endif +#endif /* GL_VERSION_1_3 */ +#ifndef GL_VERSION_1_4 +#define GL_BLEND_DST_RGB 0x80C8 +#define GL_BLEND_SRC_RGB 0x80C9 +#define GL_BLEND_DST_ALPHA 0x80CA +#define GL_BLEND_SRC_ALPHA 0x80CB +#define GL_FUNC_ADD 0x8006 +typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); +typedef void (APIENTRYP PFNGLBLENDEQUATIONPROC) (GLenum mode); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glBlendFuncSeparate (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); +GLAPI void APIENTRY glBlendEquation (GLenum mode); +#endif +#endif /* GL_VERSION_1_4 */ +#ifndef GL_VERSION_1_5 +typedef khronos_ssize_t GLsizeiptr; +typedef khronos_intptr_t GLintptr; +#define GL_ARRAY_BUFFER 0x8892 +#define GL_ELEMENT_ARRAY_BUFFER 0x8893 +#define GL_ARRAY_BUFFER_BINDING 0x8894 +#define GL_STREAM_DRAW 0x88E0 +typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); +typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); +typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); +typedef void (APIENTRYP PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const void *data, GLenum usage); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glBindBuffer (GLenum target, GLuint buffer); +GLAPI void APIENTRY glDeleteBuffers (GLsizei n, const GLuint *buffers); +GLAPI void APIENTRY glGenBuffers (GLsizei n, GLuint *buffers); +GLAPI void APIENTRY glBufferData (GLenum target, GLsizeiptr size, const void *data, GLenum usage); +#endif +#endif /* GL_VERSION_1_5 */ +#ifndef GL_VERSION_2_0 +typedef char GLchar; +typedef khronos_int16_t GLshort; +typedef khronos_int8_t GLbyte; +typedef khronos_uint16_t GLushort; +#define GL_BLEND_EQUATION_RGB 0x8009 +#define GL_BLEND_EQUATION_ALPHA 0x883D +#define GL_FRAGMENT_SHADER 0x8B30 +#define GL_VERTEX_SHADER 0x8B31 +#define GL_COMPILE_STATUS 0x8B81 +#define GL_LINK_STATUS 0x8B82 +#define GL_INFO_LOG_LENGTH 0x8B84 +#define GL_CURRENT_PROGRAM 0x8B8D +#define GL_UPPER_LEFT 0x8CA2 +typedef void (APIENTRYP PFNGLBLENDEQUATIONSEPARATEPROC) (GLenum modeRGB, GLenum modeAlpha); +typedef void (APIENTRYP PFNGLATTACHSHADERPROC) (GLuint program, GLuint shader); +typedef void (APIENTRYP PFNGLCOMPILESHADERPROC) (GLuint shader); +typedef GLuint (APIENTRYP PFNGLCREATEPROGRAMPROC) (void); +typedef GLuint (APIENTRYP PFNGLCREATESHADERPROC) (GLenum type); +typedef void (APIENTRYP PFNGLDELETEPROGRAMPROC) (GLuint program); +typedef void (APIENTRYP PFNGLDELETESHADERPROC) (GLuint shader); +typedef void (APIENTRYP PFNGLDETACHSHADERPROC) (GLuint program, GLuint shader); +typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index); +typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name); +typedef void (APIENTRYP PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params); +typedef void (APIENTRYP PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +typedef void (APIENTRYP PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params); +typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name); +typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program); +typedef void (APIENTRYP PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); +typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program); +typedef void (APIENTRYP PFNGLUNIFORM1IPROC) (GLint location, GLint v0); +typedef void (APIENTRYP PFNGLUNIFORMMATRIX4FVPROC) (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); +typedef void (APIENTRYP PFNGLVERTEXATTRIBPOINTERPROC) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glBlendEquationSeparate (GLenum modeRGB, GLenum modeAlpha); +GLAPI void APIENTRY glAttachShader (GLuint program, GLuint shader); +GLAPI void APIENTRY glCompileShader (GLuint shader); +GLAPI GLuint APIENTRY glCreateProgram (void); +GLAPI GLuint APIENTRY glCreateShader (GLenum type); +GLAPI void APIENTRY glDeleteProgram (GLuint program); +GLAPI void APIENTRY glDeleteShader (GLuint shader); +GLAPI void APIENTRY glDetachShader (GLuint program, GLuint shader); +GLAPI void APIENTRY glEnableVertexAttribArray (GLuint index); +GLAPI GLint APIENTRY glGetAttribLocation (GLuint program, const GLchar *name); +GLAPI void APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint *params); +GLAPI void APIENTRY glGetProgramInfoLog (GLuint program, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +GLAPI void APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint *params); +GLAPI void APIENTRY glGetShaderInfoLog (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); +GLAPI GLint APIENTRY glGetUniformLocation (GLuint program, const GLchar *name); +GLAPI void APIENTRY glLinkProgram (GLuint program); +GLAPI void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); +GLAPI void APIENTRY glUseProgram (GLuint program); +GLAPI void APIENTRY glUniform1i (GLint location, GLint v0); +GLAPI void APIENTRY glUniformMatrix4fv (GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); +GLAPI void APIENTRY glVertexAttribPointer (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); +#endif +#endif /* GL_VERSION_2_0 */ +#ifndef GL_VERSION_3_0 +typedef khronos_uint16_t GLhalf; +#define GL_MAJOR_VERSION 0x821B +#define GL_MINOR_VERSION 0x821C +#define GL_NUM_EXTENSIONS 0x821D +#define GL_FRAMEBUFFER_SRGB 0x8DB9 +#define GL_VERTEX_ARRAY_BINDING 0x85B5 +typedef void (APIENTRYP PFNGLGETBOOLEANI_VPROC) (GLenum target, GLuint index, GLboolean *data); +typedef void (APIENTRYP PFNGLGETINTEGERI_VPROC) (GLenum target, GLuint index, GLint *data); +typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGIPROC) (GLenum name, GLuint index); +typedef void (APIENTRYP PFNGLBINDVERTEXARRAYPROC) (GLuint array); +typedef void (APIENTRYP PFNGLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint *arrays); +typedef void (APIENTRYP PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint *arrays); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI const GLubyte *APIENTRY glGetStringi (GLenum name, GLuint index); +GLAPI void APIENTRY glBindVertexArray (GLuint array); +GLAPI void APIENTRY glDeleteVertexArrays (GLsizei n, const GLuint *arrays); +GLAPI void APIENTRY glGenVertexArrays (GLsizei n, GLuint *arrays); +#endif +#endif /* GL_VERSION_3_0 */ +#ifndef GL_VERSION_3_1 +#define GL_VERSION_3_1 1 +#define GL_PRIMITIVE_RESTART 0x8F9D +#endif /* GL_VERSION_3_1 */ +#ifndef GL_VERSION_3_2 +#define GL_VERSION_3_2 1 +typedef struct __GLsync *GLsync; +typedef khronos_uint64_t GLuint64; +typedef khronos_int64_t GLint64; +typedef void (APIENTRYP PFNGLDRAWELEMENTSBASEVERTEXPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); +typedef void (APIENTRYP PFNGLGETINTEGER64I_VPROC) (GLenum target, GLuint index, GLint64 *data); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glDrawElementsBaseVertex (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); +#endif +#endif /* GL_VERSION_3_2 */ +#ifndef GL_VERSION_3_3 +#define GL_VERSION_3_3 1 +#define GL_SAMPLER_BINDING 0x8919 +typedef void (APIENTRYP PFNGLBINDSAMPLERPROC) (GLuint unit, GLuint sampler); +#ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glBindSampler (GLuint unit, GLuint sampler); +#endif +#endif /* GL_VERSION_3_3 */ +#ifndef GL_VERSION_4_1 +typedef void (APIENTRYP PFNGLGETFLOATI_VPROC) (GLenum target, GLuint index, GLfloat *data); +typedef void (APIENTRYP PFNGLGETDOUBLEI_VPROC) (GLenum target, GLuint index, GLdouble *data); +#endif /* GL_VERSION_4_1 */ +#ifndef GL_VERSION_4_3 +typedef void (APIENTRY *GLDEBUGPROC)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam); +#endif /* GL_VERSION_4_3 */ +#ifndef GL_VERSION_4_5 +#define GL_CLIP_ORIGIN 0x935C +typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint *param); +typedef void (APIENTRYP PFNGLGETTRANSFORMFEEDBACKI64_VPROC) (GLuint xfb, GLenum pname, GLuint index, GLint64 *param); +#endif /* GL_VERSION_4_5 */ +#ifndef GL_ARB_bindless_texture +typedef khronos_uint64_t GLuint64EXT; +#endif /* GL_ARB_bindless_texture */ +#ifndef GL_ARB_cl_event +struct _cl_context; +struct _cl_event; +#endif /* GL_ARB_cl_event */ +#ifndef GL_ARB_clip_control +#define GL_ARB_clip_control 1 +#endif /* GL_ARB_clip_control */ +#ifndef GL_ARB_debug_output +typedef void (APIENTRY *GLDEBUGPROCARB)(GLenum source,GLenum type,GLuint id,GLenum severity,GLsizei length,const GLchar *message,const void *userParam); +#endif /* GL_ARB_debug_output */ +#ifndef GL_EXT_EGL_image_storage +typedef void *GLeglImageOES; +#endif /* GL_EXT_EGL_image_storage */ +#ifndef GL_EXT_direct_state_access +typedef void (APIENTRYP PFNGLGETFLOATI_VEXTPROC) (GLenum pname, GLuint index, GLfloat *params); +typedef void (APIENTRYP PFNGLGETDOUBLEI_VEXTPROC) (GLenum pname, GLuint index, GLdouble *params); +typedef void (APIENTRYP PFNGLGETPOINTERI_VEXTPROC) (GLenum pname, GLuint index, void **params); +typedef void (APIENTRYP PFNGLGETVERTEXARRAYINTEGERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, GLint *param); +typedef void (APIENTRYP PFNGLGETVERTEXARRAYPOINTERI_VEXTPROC) (GLuint vaobj, GLuint index, GLenum pname, void **param); +#endif /* GL_EXT_direct_state_access */ +#ifndef GL_NV_draw_vulkan_image +typedef void (APIENTRY *GLVULKANPROCNV)(void); +#endif /* GL_NV_draw_vulkan_image */ +#ifndef GL_NV_gpu_shader5 +typedef khronos_int64_t GLint64EXT; +#endif /* GL_NV_gpu_shader5 */ +#ifndef GL_NV_vertex_buffer_unified_memory +typedef void (APIENTRYP PFNGLGETINTEGERUI64I_VNVPROC) (GLenum value, GLuint index, GLuint64EXT *result); +#endif /* GL_NV_vertex_buffer_unified_memory */ +#ifdef __cplusplus +} +#endif +#endif + +#ifndef GL3W_API +#define GL3W_API +#endif + +#ifndef __gl_h_ +#define __gl_h_ +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#define GL3W_OK 0 +#define GL3W_ERROR_INIT -1 +#define GL3W_ERROR_LIBRARY_OPEN -2 +#define GL3W_ERROR_OPENGL_VERSION -3 + +typedef void (*GL3WglProc)(void); +typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc); + +/* gl3w api */ +GL3W_API int imgl3wInit(void); +GL3W_API int imgl3wInit2(GL3WGetProcAddressProc proc); +GL3W_API int imgl3wIsSupported(int major, int minor); +GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); + +/* gl3w internal state */ +union GL3WProcs { + GL3WglProc ptr[52]; + struct { + PFNGLACTIVETEXTUREPROC ActiveTexture; + PFNGLATTACHSHADERPROC AttachShader; + PFNGLBINDBUFFERPROC BindBuffer; + PFNGLBINDSAMPLERPROC BindSampler; + PFNGLBINDTEXTUREPROC BindTexture; + PFNGLBINDVERTEXARRAYPROC BindVertexArray; + PFNGLBLENDEQUATIONPROC BlendEquation; + PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate; + PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate; + PFNGLBUFFERDATAPROC BufferData; + PFNGLCLEARPROC Clear; + PFNGLCLEARCOLORPROC ClearColor; + PFNGLCOMPILESHADERPROC CompileShader; + PFNGLCREATEPROGRAMPROC CreateProgram; + PFNGLCREATESHADERPROC CreateShader; + PFNGLDELETEBUFFERSPROC DeleteBuffers; + PFNGLDELETEPROGRAMPROC DeleteProgram; + PFNGLDELETESHADERPROC DeleteShader; + PFNGLDELETETEXTURESPROC DeleteTextures; + PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays; + PFNGLDETACHSHADERPROC DetachShader; + PFNGLDISABLEPROC Disable; + PFNGLDRAWELEMENTSPROC DrawElements; + PFNGLDRAWELEMENTSBASEVERTEXPROC DrawElementsBaseVertex; + PFNGLENABLEPROC Enable; + PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray; + PFNGLGENBUFFERSPROC GenBuffers; + PFNGLGENTEXTURESPROC GenTextures; + PFNGLGENVERTEXARRAYSPROC GenVertexArrays; + PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; + PFNGLGETINTEGERVPROC GetIntegerv; + PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog; + PFNGLGETPROGRAMIVPROC GetProgramiv; + PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog; + PFNGLGETSHADERIVPROC GetShaderiv; + PFNGLGETSTRINGPROC GetString; + PFNGLGETSTRINGIPROC GetStringi; + PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation; + PFNGLISENABLEDPROC IsEnabled; + PFNGLLINKPROGRAMPROC LinkProgram; + PFNGLPIXELSTOREIPROC PixelStorei; + PFNGLPOLYGONMODEPROC PolygonMode; + PFNGLREADPIXELSPROC ReadPixels; + PFNGLSCISSORPROC Scissor; + PFNGLSHADERSOURCEPROC ShaderSource; + PFNGLTEXIMAGE2DPROC TexImage2D; + PFNGLTEXPARAMETERIPROC TexParameteri; + PFNGLUNIFORM1IPROC Uniform1i; + PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv; + PFNGLUSEPROGRAMPROC UseProgram; + PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer; + PFNGLVIEWPORTPROC Viewport; + } gl; +}; + +GL3W_API extern union GL3WProcs gl3wProcs; + +/* OpenGL functions */ +#define glActiveTexture gl3wProcs.gl.ActiveTexture +#define glAttachShader gl3wProcs.gl.AttachShader +#define glBindBuffer gl3wProcs.gl.BindBuffer +#define glBindSampler gl3wProcs.gl.BindSampler +#define glBindTexture gl3wProcs.gl.BindTexture +#define glBindVertexArray gl3wProcs.gl.BindVertexArray +#define glBlendEquation gl3wProcs.gl.BlendEquation +#define glBlendEquationSeparate gl3wProcs.gl.BlendEquationSeparate +#define glBlendFuncSeparate gl3wProcs.gl.BlendFuncSeparate +#define glBufferData gl3wProcs.gl.BufferData +#define glClear gl3wProcs.gl.Clear +#define glClearColor gl3wProcs.gl.ClearColor +#define glCompileShader gl3wProcs.gl.CompileShader +#define glCreateProgram gl3wProcs.gl.CreateProgram +#define glCreateShader gl3wProcs.gl.CreateShader +#define glDeleteBuffers gl3wProcs.gl.DeleteBuffers +#define glDeleteProgram gl3wProcs.gl.DeleteProgram +#define glDeleteShader gl3wProcs.gl.DeleteShader +#define glDeleteTextures gl3wProcs.gl.DeleteTextures +#define glDeleteVertexArrays gl3wProcs.gl.DeleteVertexArrays +#define glDetachShader gl3wProcs.gl.DetachShader +#define glDisable gl3wProcs.gl.Disable +#define glDrawElements gl3wProcs.gl.DrawElements +#define glDrawElementsBaseVertex gl3wProcs.gl.DrawElementsBaseVertex +#define glEnable gl3wProcs.gl.Enable +#define glEnableVertexAttribArray gl3wProcs.gl.EnableVertexAttribArray +#define glGenBuffers gl3wProcs.gl.GenBuffers +#define glGenTextures gl3wProcs.gl.GenTextures +#define glGenVertexArrays gl3wProcs.gl.GenVertexArrays +#define glGetAttribLocation gl3wProcs.gl.GetAttribLocation +#define glGetIntegerv gl3wProcs.gl.GetIntegerv +#define glGetProgramInfoLog gl3wProcs.gl.GetProgramInfoLog +#define glGetProgramiv gl3wProcs.gl.GetProgramiv +#define glGetShaderInfoLog gl3wProcs.gl.GetShaderInfoLog +#define glGetShaderiv gl3wProcs.gl.GetShaderiv +#define glGetString gl3wProcs.gl.GetString +#define glGetStringi gl3wProcs.gl.GetStringi +#define glGetUniformLocation gl3wProcs.gl.GetUniformLocation +#define glIsEnabled gl3wProcs.gl.IsEnabled +#define glLinkProgram gl3wProcs.gl.LinkProgram +#define glPixelStorei gl3wProcs.gl.PixelStorei +#define glPolygonMode gl3wProcs.gl.PolygonMode +#define glReadPixels gl3wProcs.gl.ReadPixels +#define glScissor gl3wProcs.gl.Scissor +#define glShaderSource gl3wProcs.gl.ShaderSource +#define glTexImage2D gl3wProcs.gl.TexImage2D +#define glTexParameteri gl3wProcs.gl.TexParameteri +#define glUniform1i gl3wProcs.gl.Uniform1i +#define glUniformMatrix4fv gl3wProcs.gl.UniformMatrix4fv +#define glUseProgram gl3wProcs.gl.UseProgram +#define glVertexAttribPointer gl3wProcs.gl.VertexAttribPointer +#define glViewport gl3wProcs.gl.Viewport + +#ifdef __cplusplus +} +#endif + +#endif + +#ifdef IMGL3W_IMPL +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#if defined(_WIN32) +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN 1 +#endif +#include + +static HMODULE libgl; +typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR); +static GL3WglGetProcAddr wgl_get_proc_address; + +static int open_libgl(void) +{ + libgl = LoadLibraryA("opengl32.dll"); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + wgl_get_proc_address = (GL3WglGetProcAddr)GetProcAddress(libgl, "wglGetProcAddress"); + return GL3W_OK; +} + +static void close_libgl(void) { FreeLibrary(libgl); } +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + res = (GL3WglProc)wgl_get_proc_address(proc); + if (!res) + res = (GL3WglProc)GetProcAddress(libgl, proc); + return res; +} +#elif defined(__APPLE__) +#include + +static void *libgl; +static int open_libgl(void) +{ + libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + return GL3W_OK; +} + +static void close_libgl(void) { dlclose(libgl); } + +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + *(void **)(&res) = dlsym(libgl, proc); + return res; +} +#else +#include + +static void *libgl; +static GL3WglProc (*glx_get_proc_address)(const GLubyte *); + +static int open_libgl(void) +{ + libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + return GL3W_ERROR_LIBRARY_OPEN; + *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + return GL3W_OK; +} + +static void close_libgl(void) { dlclose(libgl); } + +static GL3WglProc get_proc(const char *proc) +{ + GL3WglProc res; + res = glx_get_proc_address((const GLubyte *)proc); + if (!res) + *(void **)(&res) = dlsym(libgl, proc); + return res; +} +#endif + +static struct { int major, minor; } version; + +static int parse_version(void) +{ + if (!glGetIntegerv) + return GL3W_ERROR_INIT; + glGetIntegerv(GL_MAJOR_VERSION, &version.major); + glGetIntegerv(GL_MINOR_VERSION, &version.minor); + if (version.major < 3) + return GL3W_ERROR_OPENGL_VERSION; + return GL3W_OK; +} + +static void load_procs(GL3WGetProcAddressProc proc); + +int imgl3wInit(void) +{ + int res = open_libgl(); + if (res) + return res; + atexit(close_libgl); + return imgl3wInit2(get_proc); +} + +int imgl3wInit2(GL3WGetProcAddressProc proc) +{ + load_procs(proc); + return parse_version(); +} + +int imgl3wIsSupported(int major, int minor) +{ + if (major < 3) + return 0; + if (version.major == major) + return version.minor >= minor; + return version.major >= major; +} + +GL3WglProc imgl3wGetProcAddress(const char *proc) { return get_proc(proc); } + +static const char *proc_names[] = { + "glActiveTexture", + "glAttachShader", + "glBindBuffer", + "glBindSampler", + "glBindTexture", + "glBindVertexArray", + "glBlendEquation", + "glBlendEquationSeparate", + "glBlendFuncSeparate", + "glBufferData", + "glClear", + "glClearColor", + "glCompileShader", + "glCreateProgram", + "glCreateShader", + "glDeleteBuffers", + "glDeleteProgram", + "glDeleteShader", + "glDeleteTextures", + "glDeleteVertexArrays", + "glDetachShader", + "glDisable", + "glDrawElements", + "glDrawElementsBaseVertex", + "glEnable", + "glEnableVertexAttribArray", + "glGenBuffers", + "glGenTextures", + "glGenVertexArrays", + "glGetAttribLocation", + "glGetIntegerv", + "glGetProgramInfoLog", + "glGetProgramiv", + "glGetShaderInfoLog", + "glGetShaderiv", + "glGetString", + "glGetStringi", + "glGetUniformLocation", + "glIsEnabled", + "glLinkProgram", + "glPixelStorei", + "glPolygonMode", + "glReadPixels", + "glScissor", + "glShaderSource", + "glTexImage2D", + "glTexParameteri", + "glUniform1i", + "glUniformMatrix4fv", + "glUseProgram", + "glVertexAttribPointer", + "glViewport", +}; + +GL3W_API union GL3WProcs gl3wProcs; + +static void load_procs(GL3WGetProcAddressProc proc) +{ + size_t i; + for (i = 0; i < ARRAY_SIZE(proc_names); i++) + gl3wProcs.ptr[i] = proc(proc_names[i]); +} + +#ifdef __cplusplus +} +#endif +#endif From cb907f7cd7ac2d08b04cb22e3731ec7bade918af Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 1 Sep 2021 01:08:26 +0200 Subject: [PATCH 0546/1018] viewer: update vbo params --- include/gproshan/viewer/che_viewer.h | 6 +++--- src/viewer/che_viewer.cpp | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 0de2fae0..52b024ec 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -49,9 +49,9 @@ class che_viewer void update(); void update_vbo(); void update_vbo_geometry(); - void update_vbo_normal(); - void update_vbo_color(); - void update_vbo_heatmap(); + void update_vbo_normal(const vertex * vnormal = nullptr); + void update_vbo_color(const che::rgb_t * vcolor = nullptr); + void update_vbo_heatmap(const real_t * vheatmap = nullptr); void update_instances_translations(const std::vector & translations); void draw(shader & program); void draw_point_cloud(shader & program); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 41a0e8e1..44c6420f 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -114,13 +114,15 @@ void che_viewer::update_vbo_geometry() glBindVertexArray(0); } -void che_viewer::update_vbo_normal() +void che_viewer::update_vbo_normal(const vertex * vnormal) { + if(!vnormal) vnormal = &mesh->normal(0); + glBindVertexArray(vao); // 1 NORMAL glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->normal(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), vnormal, GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -128,13 +130,15 @@ void che_viewer::update_vbo_normal() glBindVertexArray(0); } -void che_viewer::update_vbo_color() +void che_viewer::update_vbo_color(const che::rgb_t * vcolor) { + if(!vcolor) vcolor = &mesh->rgb(0); + glBindVertexArray(vao); // 2 COLOR glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(che::rgb_t), &mesh->rgb(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(che::rgb_t), vcolor, GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -142,13 +146,15 @@ void che_viewer::update_vbo_color() glBindVertexArray(0); } -void che_viewer::update_vbo_heatmap() +void che_viewer::update_vbo_heatmap(const real_t * vheatmap) { + if(!vheatmap) vheatmap = &mesh->heatmap(0); + glBindVertexArray(vao); // 3 HEAT MAP glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), &mesh->heatmap(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), vheatmap, GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 1, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); From b29201ac53e285bc9774f51ddf4c44191ea4878b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 1 Sep 2021 17:56:30 +0200 Subject: [PATCH 0547/1018] viewer: minor updates update_vbo app_viewer calls --- src/app_viewer.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index c693c2ee..0bc76f32 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -166,7 +166,9 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = f(gv(v)); + gv(v) = f(gv(v)); + + mesh.update_vbo_heatmap(gv.memptr()); return false; } @@ -258,10 +260,10 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) mesh->set_vertices(fair.new_vertices()); mesh->update_normals(); - } - mesh.update_vbo_geometry(); - mesh.update_vbo_normal(); + mesh.update_vbo_geometry(); + mesh.update_vbo_normal(); + } return true; } @@ -288,10 +290,10 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) mesh->set_vertices(fair.new_vertices()); mesh->update_normals(); - } - mesh.update_vbo_geometry(); - mesh.update_vbo_normal(); + mesh.update_vbo_geometry(); + mesh.update_vbo_normal(); + } return true; } @@ -304,7 +306,6 @@ bool app_viewer::process_geodesics(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static vector dist; static geodesics::params params; #ifdef GPROSHAN_CUDA @@ -321,17 +322,13 @@ bool app_viewer::process_geodesics(viewer * p_view) if(!mesh.selected.size()) mesh.selected.push_back(0); - if(dist.size() < mesh->n_vertices) - dist.resize(mesh->n_vertices); - - params.dist_alloc = dist.data(); + params.dist_alloc = &mesh->heatmap(0); TIC(view->time) geodesics G(mesh, mesh.selected, params); TOC(view->time) params.radio = G.radio(); - mesh->update_heatmap(&G[0]); mesh.update_vbo_heatmap(); } From e645a2a744b4e4b42ec29c3a253f697d6e569e51 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 1 Sep 2021 21:27:20 +0200 Subject: [PATCH 0548/1018] viewer: adding new colormap for sets --- include/gproshan/mesh/che.h | 1 + include/gproshan/viewer/viewer.h | 2 +- shaders/colormap.glsl | 121 ++++++++++++++++++++++++++----- src/app_viewer.cpp | 2 + src/mesh/che.cpp | 6 ++ src/viewer/viewer.cpp | 6 ++ 6 files changed, 120 insertions(+), 18 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 9b6ed535..06b53c37 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -86,6 +86,7 @@ class che real_t area_surface() const; void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); const rgb_t & rgb(const index_t & v) const; + rgb_t & rgb(const index_t & v); vertex color(const index_t & v) const; vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; const real_t & heatmap(const index_t & v) const; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index f4d34af3..747efd55 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -54,6 +54,7 @@ class viewer }; static const int m_window_size[N_MESHES + 1][2]; + static const std::vector colormap; GLFWwindow * window = nullptr; @@ -61,7 +62,6 @@ class viewer int viewport_width, viewport_height; unsigned int idx_colormap = 1; // colormap index defined in shaders/colormap.glsl - const std::vector colormap = {"vertex color", "blue", "red", "blue/read"}; shader shader_triangles; shader shader_normals; shader shader_gradient; diff --git a/shaders/colormap.glsl b/shaders/colormap.glsl index e6c7c4eb..0fe11f15 100644 --- a/shaders/colormap.glsl +++ b/shaders/colormap.glsl @@ -2,26 +2,26 @@ float colormap_red_1(float x) { - if (x < 0.7520372909206926) - return (((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; + if(x < 0.7520372909206926) + return(((9.68615208861418E+02 * x - 1.16097242960380E+03) * x + 1.06173672031378E+02) * x - 1.68616613530379E+02) * x + 2.56073136099945E+02; else return -1.20830453148990E+01 * x + 1.44337397593436E+01; } float colormap_green_1(float x) { - if (x < 0.7485333535031721) - return (((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; + if(x < 0.7485333535031721) + return(((-4.58537247030064E+02 * x + 5.67323181593790E+02) * x - 2.56714665792882E+02) * x - 1.14205365680507E+02) * x + 2.47073841488433E+02; else - return ((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; + return((-2.99774273328017E+02 * x + 4.12147041403012E+02) * x - 2.49880079288168E+02) * x + 1.93578601034431E+02; } float colormap_blue_1(float x) { - if (x < 0.7628468501376879) - return ((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; + if(x < 0.7628468501376879) + return((-5.44257972228224E+01 * x + 2.70890554876532E+01) * x - 9.12766750739247E+01) * x + 2.52166182860177E+02; else - return (((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; + return(((4.55621137729287E+04 * x - 1.59960900638524E+05) * x + 2.09530452721547E+05) * x - 1.21704642900945E+05) * x + 2.66644674068694E+04; } vec3 colormap_1(float x) @@ -37,20 +37,20 @@ vec3 colormap_1(float x) float colormap_red_2(float x) { - return ((((1.30858855846896E+03 * x - 2.84649723684787E+03) * x + 1.76048857883363E+03) * x - 3.99775093706324E+02) * x + 2.69759225316811E+01) * x + 2.54587325383574E+02; + return((((1.30858855846896E+03 * x - 2.84649723684787E+03) * x + 1.76048857883363E+03) * x - 3.99775093706324E+02) * x + 2.69759225316811E+01) * x + 2.54587325383574E+02; } float colormap_green_2(float x) { - return ((((-8.85605750526301E+02 * x + 2.20590941129997E+03) * x - 1.50123293069936E+03) * x + 2.38490009587258E+01) * x - 6.03460495073813E+01) * x + 2.54768707485247E+02; + return((((-8.85605750526301E+02 * x + 2.20590941129997E+03) * x - 1.50123293069936E+03) * x + 2.38490009587258E+01) * x - 6.03460495073813E+01) * x + 2.54768707485247E+02; } float colormap_blue_2(float x) { - if (x < 0.2363454401493073) - return (-3.68734834041388E+01 * x - 3.28163398692792E+02) * x + 2.27342862588147E+02; - else if (x < 0.7571054399013519) - return ((((1.60988309475108E+04 * x - 4.18782706486673E+04) * x + 4.14508040221340E+04) * x - 1.88926043556059E+04) * x + 3.50108270140290E+03) * x - 5.28541997751406E+01; + if(x < 0.2363454401493073) + return(-3.68734834041388E+01 * x - 3.28163398692792E+02) * x + 2.27342862588147E+02; + else if(x < 0.7571054399013519) + return((((1.60988309475108E+04 * x - 4.18782706486673E+04) * x + 4.14508040221340E+04) * x - 1.88926043556059E+04) * x + 3.50108270140290E+03) * x - 5.28541997751406E+01; else return 1.68513761929930E+01 * x - 1.06424668227935E+01; } @@ -68,7 +68,7 @@ vec3 colormap_2(float x) float colormap_red_3(float x) { - if (x < 0.75) + if(x < 0.75) return 1012.0 * x - 389.0; else return -1.11322769567548E+03 * x + 1.24461193212872E+03; @@ -76,7 +76,7 @@ float colormap_red_3(float x) float colormap_green_3(float x) { - if (x < 0.5) + if(x < 0.5) return 1012.0 * x - 129.0; else return -1012.0 * x + 899.0; @@ -84,7 +84,7 @@ float colormap_green_3(float x) float colormap_blue_3(float x) { - if (x < 0.25) + if(x < 0.25) return 1012.0 * x + 131.0; else return -1012.0 * x + 643.0; @@ -98,11 +98,98 @@ vec3 colormap_3(float x) return vec3(r, g, b); } + +// https://github.com/kbinani/colormap-shaders/blob/master/shaders/glsl/IDL_CB-Set3.frag + +float colormap_red_4(float x) +{ + if(x < 0.09082479229584027) + return 1.24879652173913E+03 * x + 1.41460000000000E+02; + else if(x < 0.1809653122266933) + return -7.21339920948626E+02 * x + 3.20397233201581E+02; + else if(x < 0.2715720097177793) + return 6.77416996047422E+02 * x + 6.72707509881444E+01; + else if(x < 0.3619607687891861) + return -1.36850782608711E+03 * x + 6.22886666666710E+02; + else if(x < 0.4527609316115322) + return 1.38118774703557E+03 * x - 3.72395256916997E+02; + else if(x < 0.5472860687991931) + return -7.81436521739194E+02 * x + 6.06756521739174E+02; + else if(x < 0.6360981817705944) + return 8.06836521739242E+02 * x - 2.62483188405869E+02; + else if(x < 0.8158623444475089) + return -3.49616157878512E+02 * x + 4.73134258402717E+02; + else if(x < 0.9098023786863947) + return 1.72428853754953E+02 * x + 4.72173913043111E+01; + else + return 5.44142292490101E+02 * x - 2.90968379446626E+02; +} + +float colormap_green_4(float x) +{ + if(x < 0.08778161310534617) + return 4.88563478260870E+02 * x + 2.10796666666667E+02; + else if(x < 0.2697669137324175) + return -6.96835646006769E+02 * x + 3.14852913968545E+02; + else if(x < 0.3622079895714037) + return 5.40799130434797E+02 * x - 1.90200000000068E+01; + else if(x < 0.4519795462045253) + return 3.23774703557373E+01 * x + 1.65134387351785E+02; + else if(x < 0.5466820192751115) + return 4.43064347826088E+02 * x - 2.04876811594176E+01; + else if(x < 0.6368889369442862) + return -1.83472332015826E+02 * x + 3.22028656126484E+02; + else if(x < 0.728402572416003) + return 1.27250988142231E+02 * x + 1.24132411067220E+02; + else if(x < 0.8187333479165154) + return -9.82116600790428E+02 * x + 9.32198616600708E+02; + else if(x < 0.9094607880855196) + return 1.17713438735149E+03 * x - 8.35652173912769E+02; + else + return 2.13339920948864E+01 * x + 2.15502964426857E+02; +} + +float colormap_blue_4(float x) +{ + if(x < 0.09081516507716858) + return -2.27937391304345E+02 * x + 1.99486666666666E+02; + else if(x < 0.1809300436999751) + return 4.33958498023703E+02 * x + 1.39376482213440E+02; + else if(x < 0.2720053156712806) + return -1.14300000000004E+03 * x + 4.24695652173923E+02; + else if(x < 0.3616296568054424) + return 1.08175889328072E+03 * x - 1.80450592885399E+02; + else if(x < 0.4537067088757783) + return -1.22681999999994E+03 * x + 6.54399999999974E+02; + else if(x < 0.5472726179445029) + return 8.30770750988243E+01 * x + 6.00909090909056E+01; + else if(x < 0.6374811920489858) + return 1.36487351778676E+03 * x - 6.41401185770872E+02; + else if(x < 0.7237636846906381) + return -1.27390769230737E+02 * x + 3.09889230769173E+02; + else if(x < 0.8178226469606309) + return -3.01831168831021E+02 * x + 4.36142857142782E+02; + else if(x < 0.9094505664375214) + return 8.47622811970801E+01 * x + 1.19977978543158E+02; + else + return -9.06117391304296E+02 * x + 1.02113405797096E+03; +} + +vec3 colormap_4(float x) +{ + float r = clamp(colormap_red_4(x) / 255.0, 0.0, 1.0); + float g = clamp(colormap_green_4(x) / 255.0, 0.0, 1.0); + float b = clamp(colormap_blue_4(x) / 255.0, 0.0, 1.0); + return vec3(r, g, b); +} + + vec3 colormap(uint i, float x) { if(i == 1) return colormap_1(x); if(i == 2) return colormap_2(x); if(i == 3) return colormap_3(x); + if(i == 4) return colormap_4(x); return vec3(0, 0, 0); } diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 0bc76f32..f424f5f6 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -329,6 +329,8 @@ bool app_viewer::process_geodesics(viewer * p_view) TOC(view->time) params.radio = G.radio(); + + G.normalize(); mesh.update_vbo_heatmap(); } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 51ebb008..a8a1dd0f 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -291,6 +291,12 @@ const che::rgb_t & che::rgb(const index_t & v) const return VC[v]; } +che::rgb_t & che::rgb(const index_t & v) +{ + assert(VC && v < n_vertices); + return VC[v]; +} + vertex che::color(const index_t & v) const { assert(VC && v < n_vertices); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index f799033b..934ea376 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -45,6 +45,12 @@ const int viewer::m_window_size[N_MESHES + 1][2] = {{1, 1}, {2, 5}, {3, 4}, {3, 4} }; +const std::vector viewer::colormap = { "vertex color", + "blue", + "red", + "blue/read", + "set" + }; viewer::viewer(int width, int height): window_width(width), window_height(height) { From b41467280ba31288a3d3c838725b6fff88340a04 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Sep 2021 13:19:19 +0200 Subject: [PATCH 0549/1018] shaders: cleaning up code --- shaders/fragment.glsl | 55 +++----------------------------- shaders/fragment_pointcloud.glsl | 48 ++-------------------------- shaders/fragment_sphere.glsl | 32 ++----------------- shaders/shading.glsl | 49 ++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 1 + 5 files changed, 61 insertions(+), 124 deletions(-) create mode 100644 shaders/shading.glsl diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index ed5a664a..2164db45 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,6 +1,6 @@ #version 410 core -#include colormap.glsl +#include shading.glsl in vec3 gs_position; in vec3 gs_normal; @@ -11,72 +11,27 @@ noperspective in vec3 edge_dist; layout(location = 0) out vec4 frag_color; -uniform uint idx_colormap; + uniform vec3 eye; uniform vec3 light; uniform bool render_flat; -uniform bool render_lines; uniform bool render_wireframe; -float diffuse(vec3 N, vec3 L) -{ - return max(0, dot(N, L)); -} - -float specular(vec3 N, vec3 L, vec3 E) -{ - const float shininess = 4; - vec3 R = 2 * dot(L, N) * N - L; - - return pow(max(0, dot(R, E)), shininess); -} - -float fresnel(vec3 N, vec3 E) -{ - const float sharpness = 10; - float NE = max(0, dot(N, E)); - - return pow(sqrt( 1. - NE * NE ), sharpness); -} void main() { - vec3 color = idx_colormap > 0 ? colormap(idx_colormap, gs_color) : gs_mesh_color; - - if(render_lines) - { - float h = gs_color; - h = h * 40; - h = h - floor(h); - h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); - h = 1 - h; - color = vec3(0) + (1. - h) * color; - } - - vec3 N; - - if(render_flat) - N = normalize(cross(dFdx(gs_position), dFdy(gs_position))); - else - N = normalize(gs_normal); - - vec3 L = normalize(light - gs_position); - vec3 E = normalize(eye - gs_position); - vec3 R = 2 * dot(L, N) * N - L; - vec3 one = vec3(1); - + vec3 color = lines_colormap(gs_mesh_color, gs_color); + vec3 N = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); if(render_wireframe) { vec3 delta = fwidth(edge_dist); vec3 tmp = smoothstep(vec3(0), delta, edge_dist); - float d = min(min(tmp.x, tmp.y), tmp.z); - color = mix(vec3(.2), color, d); } - color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + color = shading(N, normalize(light - gs_position), normalize(eye - gs_position), color); frag_color = vec4(color, 1); } diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 2a38b023..4906cd9d 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -1,6 +1,6 @@ #version 410 core -#include colormap.glsl +#include shading.glsl in vec3 vs_position; in vec3 vs_normal; @@ -9,59 +9,17 @@ in float vs_color; layout(location = 0) out vec4 frag_color; -uniform uint idx_colormap; uniform vec3 eye; uniform vec3 light; -uniform bool render_lines; uniform bool point_normals; -float diffuse(vec3 N, vec3 L) -{ - return max(0, dot(N, L)); -} - -float specular(vec3 N, vec3 L, vec3 E) -{ - const float shininess = 4; - vec3 R = 2 * dot(L, N) * N - L; - - return pow(max(0, dot(R, E)), shininess); -} - -float fresnel(vec3 N, vec3 E) -{ - const float sharpness = 10; - float NE = max(0, dot(N, E)); - - return pow(sqrt( 1. - NE * NE ), sharpness); -} void main() { - vec3 color = idx_colormap > 0 ? colormap(idx_colormap, vs_color) : vs_mesh_color; - - // lines - if(render_lines) - { - float h = vs_color; - h = h * 40; - h = h - floor(h); - h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); - h = 1 - h; - color = vec3(0) + (1. - h) * color; - } + vec3 color = lines_colormap(vs_mesh_color, vs_color); if(point_normals) - { - vec3 N = normalize(vs_normal); - - vec3 L = normalize(light - vs_position); - vec3 E = normalize(eye - vs_position); - vec3 R = 2 * dot(L, N) * N - L; - vec3 one = vec3(1); - - color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; - } + color = shading(normalize(vs_normal), normalize(light - vs_position), normalize(eye - vs_position), color); frag_color = vec4(color, 1); } diff --git a/shaders/fragment_sphere.glsl b/shaders/fragment_sphere.glsl index e5cab4ff..078ff5d8 100644 --- a/shaders/fragment_sphere.glsl +++ b/shaders/fragment_sphere.glsl @@ -1,5 +1,7 @@ #version 410 core +#include shading.glsl + in vec3 vs_position; in vec3 vs_normal; @@ -8,38 +10,10 @@ layout(location = 0) out vec4 frag_color; uniform vec3 eye; uniform vec3 light; -float diffuse(vec3 N, vec3 L) -{ - return max(0, dot(N, L)); -} - -float specular(vec3 N, vec3 L, vec3 E) -{ - const float shininess = 4; - vec3 R = 2 * dot(L, N) * N - L; - - return pow(max(0, dot(R, E)), shininess); -} - -float fresnel(vec3 N, vec3 E) -{ - const float sharpness = 10; - float NE = max(0, dot(N, E)); - - return pow(sqrt( 1. - NE * NE ), sharpness); -} void main() { - vec3 color = vec3(1, 0, 0); - - vec3 N = normalize(vs_normal); - vec3 L = normalize(light - vs_position); - vec3 E = normalize(eye - vs_position); - vec3 R = 2 * dot(L, N) * N - L; - vec3 one = vec3(1); - - color = diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N,E) * one; + vec3 color = shading(normalize(vs_normal), normalize(light - vs_position), normalize(eye - vs_position), vec3(1, 0, 0)); frag_color = vec4(color, 1); } diff --git a/shaders/shading.glsl b/shaders/shading.glsl new file mode 100644 index 00000000..3f3f2f10 --- /dev/null +++ b/shaders/shading.glsl @@ -0,0 +1,49 @@ +#include colormap.glsl + +uniform bool render_lines; +uniform uint idx_colormap; + + +float diffuse(vec3 N, vec3 L) +{ + return max(0, dot(N, L)); +} + +float specular(vec3 N, vec3 L, vec3 E) +{ + const float shininess = 4; + vec3 R = 2 * dot(L, N) * N - L; + + return pow(max(0, dot(R, E)), shininess); +} + +float fresnel(vec3 N, vec3 E) +{ + const float sharpness = 10; + float NE = max(0, dot(N, E)); + + return pow(sqrt(1. - NE * NE ), sharpness); +} + +vec3 lines_colormap(vec3 color, float h) +{ + color = idx_colormap > 0 ? colormap(idx_colormap, h) : color; + + if(render_lines) + { + h = h * 40; + h = h - floor(h); + h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); + h = 1 - h; + color = vec3(0) + (1. - h) * color; + } + + return color; +} + +vec3 shading(vec3 N, vec3 L, vec3 E, vec3 color) +{ + vec3 one = vec3(1); + return diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N, E) * one; +} + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 934ea376..5b9a54f1 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -794,6 +794,7 @@ void viewer::render_gl() glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), idx_colormap); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); + glProgramUniform1i(shader_pointcloud, shader_pointcloud("render_lines"), render_lines); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1i(shader_pointcloud, shader_pointcloud("point_normals"), point_normals); From e8ddec1eafaba5a427717056ce46c6e83c04f02f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Sep 2021 14:10:07 +0200 Subject: [PATCH 0550/1018] viewer: fix invert normal orientation --- src/viewer/viewer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 5b9a54f1..9349b93b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -628,6 +628,7 @@ bool viewer::menu_bgc_black(viewer * view) bool viewer::invert_orientation(viewer * view) { view->active_mesh().invert_orientation(); + view->active_mesh().update_vbo_normal(); return false; } From e0b13f5fa01ff148fecb5ad5b6cc3f88c07722a5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Sep 2021 22:47:11 +0200 Subject: [PATCH 0551/1018] viewer: set color/colormap per mesh --- include/gproshan/viewer/che_viewer.h | 3 ++- include/gproshan/viewer/viewer.h | 1 - src/viewer/viewer.cpp | 14 ++++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 52b024ec..e3c35ad8 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -35,8 +35,9 @@ class che_viewer GLuint vbo[6]; public: - int vx, vy; ///< viewport positions. + int vx, vy; ///< viewport positions. real_t factor; + index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl std::vector selected; public: diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 747efd55..aba027ec 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -61,7 +61,6 @@ class viewer int window_width, window_height; int viewport_width, viewport_height; - unsigned int idx_colormap = 1; // colormap index defined in shaders/colormap.glsl shader shader_triangles; shader shader_normals; shader shader_gradient; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 9349b93b..1a2f8478 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -111,6 +111,8 @@ bool viewer::run() ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); + che_viewer & mesh = active_mesh(); + if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) @@ -120,7 +122,7 @@ bool viewer::run() { idx_active_mesh = i; sphere_translations.clear(); - glfwSetWindowTitle(window, active_mesh()->filename.c_str()); + glfwSetWindowTitle(window, mesh->filename.c_str()); } ImGui::EndMenu(); @@ -129,8 +131,8 @@ bool viewer::run() if(ImGui::BeginMenu("Color")) { for(index_t i = 0; i < colormap.size(); ++i) - if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == idx_colormap, i != idx_colormap)) - idx_colormap = i; + if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) + mesh.idx_colormap = i; ImGui::EndMenu(); } @@ -168,7 +170,6 @@ bool viewer::run() ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); - che_viewer & mesh = active_mesh(); ImGui::Text("%s", mesh->filename.c_str()); ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); @@ -783,7 +784,6 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); - glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), idx_colormap); glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); glProgramUniform1i(shader_triangles, shader_triangles("render_flat"), render_flat); @@ -792,7 +792,6 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_triangles, shader_triangles("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), idx_colormap); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); glProgramUniform1i(shader_pointcloud, shader_pointcloud("render_lines"), render_lines); @@ -874,6 +873,9 @@ void viewer::draw_meshes(shader & program, const bool & normals) for(index_t i = 0; i < n_meshes; ++i) { + glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), meshes[i].idx_colormap); + glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), meshes[i].idx_colormap); + glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); if(normals) From ea45af360f0f96365e0ae6ea4767970f44842a3b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 3 Sep 2021 23:07:20 +0200 Subject: [PATCH 0552/1018] viewer: set render options per mesh (multi mesh view) --- include/gproshan/viewer/che_viewer.h | 14 ++- include/gproshan/viewer/viewer.h | 21 +---- src/viewer/che_viewer.cpp | 17 ++++ src/viewer/viewer.cpp | 133 +++++++++++---------------- 4 files changed, 90 insertions(+), 95 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index e3c35ad8..0f75d668 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -37,13 +37,25 @@ class che_viewer public: int vx, vy; ///< viewport positions. real_t factor; - index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl std::vector selected; + index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl + index_t point_size = 1; + bool point_normals = true; + bool render_pointcloud = false; + bool render_wireframe = false; + bool render_triangles = false; + bool render_gradients = false; + bool render_normals = false; + bool render_border = false; + bool render_lines = false; + bool render_flat = false; + public: che_viewer() = default; virtual ~che_viewer(); che *& operator -> (); + che *const & operator -> () const; operator che *& (); void init(che * mesh, const bool & normalize = true); void reload(); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index aba027ec..12de4d4f 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -87,16 +87,6 @@ class viewer bool action = false; - bool point_normals = true; - unsigned int point_size = 1; - bool render_pointcloud = false; - bool render_wireframe = false; - bool render_wireframe_fill = false; - bool render_gradient_field = false; - bool render_normal_field = false; - bool render_border = false; - bool render_lines = false; - bool render_flat = false; float bgc = 0; std::map processes; @@ -166,9 +156,9 @@ class viewer #endif // GPROSHAN_OPTIX static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); - static bool set_render_wireframe_fill(viewer * view); - static bool set_render_gradient_field(viewer * view); - static bool set_render_normal_field(viewer * view); + static bool set_render_triangles(viewer * view); + static bool set_render_gradients(viewer * view); + static bool set_render_normals(viewer * view); static bool set_render_border(viewer * view); static bool set_render_lines(viewer * view); static bool set_render_flat(viewer * view); @@ -178,10 +168,9 @@ class viewer #endif // GPROSHAN_EMBREE // draw routines - void draw_meshes(shader & program, const bool & normals = false); - void draw_selected_vertices(shader & program); + void draw_selected_vertices(shader & program, const che_viewer & mesh); - void select_border_vertices(); + void select_border_vertices(che_viewer & mesh); void pick_vertex(const real_t & x, const real_t & y); }; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 44c6420f..b07653dc 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -27,6 +27,11 @@ che *& che_viewer::operator -> () return mesh; } +che *const & che_viewer::operator -> () const +{ + return mesh; +} + che_viewer::operator che *& () { return mesh; @@ -183,6 +188,13 @@ void che_viewer::update_instances_translations(const vector & translatio void che_viewer::draw(shader & program) { + glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); + glProgramUniform1i(program, program("render_flat"), render_flat); + glProgramUniform1i(program, program("render_lines"), render_lines); + glProgramUniform1i(program, program("render_wireframe"), render_triangles); + + glPolygonMode(GL_FRONT_AND_BACK, render_wireframe ? GL_LINE : GL_FILL); + program.enable(); glBindVertexArray(vao); @@ -201,6 +213,11 @@ void che_viewer::draw(shader & program) void che_viewer::draw_point_cloud(shader & program) { + glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); + glProgramUniform1i(program, program("render_lines"), render_lines); + glProgramUniform1i(program, program("point_normals"), point_normals); + glProgramUniform1ui(program, program("point_size"), point_size); + program.enable(); glBindVertexArray(vao); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1a2f8478..0126c8fb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -174,10 +174,10 @@ bool viewer::run() ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); - if(render_pointcloud) + if(mesh.render_pointcloud) { - ImGui::Checkbox("point_normals", &point_normals); - ImGui::SliderInt("point_size", (int *) &point_size, 1, 32); + ImGui::Checkbox("point_normals", &mesh.point_normals); + ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); } for(auto & p: processes) @@ -290,7 +290,7 @@ void viewer::init_menus() sub_menus.push_back("Render"); add_process(GLFW_KEY_F5, {"F5", "Render Point Cloud", set_render_pointcloud}); add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); - add_process(GLFW_KEY_F7, {"F7", "Render Wireframe Fill", set_render_wireframe_fill}); + add_process(GLFW_KEY_F7, {"F7", "Render Triangles", set_render_triangles}); add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); #ifdef GPROSHAN_EMBREE add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); @@ -305,8 +305,8 @@ void viewer::init_menus() add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_render_flat}); add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); - add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradient_field}); - add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normal_field}); + add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradients}); + add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normals}); add_process(GLFW_KEY_APOSTROPHE, {"APOSTROPHE", "Select Border Vertices", set_render_border}); add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); } @@ -699,57 +699,66 @@ bool viewer::set_render_optix(viewer * view) bool viewer::set_render_pointcloud(viewer * view) { - view->render_pointcloud = !view->render_pointcloud; + che_viewer & mesh = view->active_mesh(); + mesh.render_pointcloud = !mesh.render_pointcloud; return false; } bool viewer::set_render_wireframe(viewer * view) { - view->render_wireframe = !view->render_wireframe; + che_viewer & mesh = view->active_mesh(); + mesh.render_wireframe = !mesh.render_wireframe; return false; } -bool viewer::set_render_wireframe_fill(viewer * view) +bool viewer::set_render_triangles(viewer * view) { - view->render_wireframe_fill = !view->render_wireframe_fill; + che_viewer & mesh = view->active_mesh(); + mesh.render_triangles = !mesh.render_triangles; return false; } -bool viewer::set_render_gradient_field(viewer * view) +bool viewer::set_render_gradients(viewer * view) { - view->render_gradient_field = !view->render_gradient_field; + che_viewer & mesh = view->active_mesh(); + mesh.render_gradients = !mesh.render_gradients; return false; } -bool viewer::set_render_normal_field(viewer * view) +bool viewer::set_render_normals(viewer * view) { - view->render_normal_field = !view->render_normal_field; + che_viewer & mesh = view->active_mesh(); + mesh.render_normals = !mesh.render_normals; return false; } bool viewer::set_render_border(viewer * view) { - view->render_border = !view->render_border; - if(!view->render_border) view->active_mesh().selected.clear(); + che_viewer & mesh = view->active_mesh(); + mesh.render_border = !mesh.render_border; + if(!mesh.render_border) + mesh.selected.clear(); return false; } bool viewer::set_render_lines(viewer * view) { - view->render_lines = !view->render_lines; + che_viewer & mesh = view->active_mesh(); + mesh.render_lines = !mesh.render_lines; return false; } bool viewer::set_render_flat(viewer * view) { - view->render_flat = !view->render_flat; + che_viewer & mesh = view->active_mesh(); + mesh.render_flat = !mesh.render_flat; view->action = true; return false; @@ -786,47 +795,44 @@ void viewer::render_gl() glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); - glProgramUniform1i(shader_triangles, shader_triangles("render_flat"), render_flat); - glProgramUniform1i(shader_triangles, shader_triangles("render_lines"), render_lines); - glProgramUniform1i(shader_triangles, shader_triangles("render_wireframe"), render_wireframe_fill); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); - glProgramUniform1i(shader_pointcloud, shader_pointcloud("render_lines"), render_lines); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1i(shader_pointcloud, shader_pointcloud("point_normals"), point_normals); - glProgramUniform1ui(shader_pointcloud, shader_pointcloud("point_size"), point_size); + glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom * 0.02); + glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - draw_meshes(shader_triangles); + glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom * 0.02); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); - if(render_normal_field) + for(index_t i = 0; i < n_meshes; ++i) { - glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom * 0.02); - glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - - draw_meshes(shader_normals, true); - } + che_viewer & mesh = meshes[i]; + glViewport(mesh.vx * viewport_width, mesh.vy * viewport_height, viewport_width, viewport_height); + if(mesh->is_pointcloud() || mesh.render_pointcloud) + mesh.draw_point_cloud(shader_pointcloud); + else + mesh.draw(shader_triangles); - if(render_gradient_field) - { - glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom * 0.02); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); - - draw_meshes(shader_gradient); - } + if(mesh.render_normals) + mesh.draw_point_cloud(shader_normals); + if(mesh.render_gradients) + mesh.draw(shader_gradient); - if(render_border) select_border_vertices(); + if(mesh.render_border) + select_border_vertices(mesh); - draw_selected_vertices(shader_sphere); + draw_selected_vertices(shader_sphere, mesh); + } } #ifdef GPROSHAN_EMBREE @@ -834,7 +840,7 @@ void viewer::render_embree() { rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, - render_flat, action + active_mesh().render_flat, action ); action = false; @@ -864,53 +870,24 @@ void viewer::render_optix() } #endif // GPROSHAN_OPTIX -void viewer::draw_meshes(shader & program, const bool & normals) +void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) { - if(render_wireframe) - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - else - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - - for(index_t i = 0; i < n_meshes; ++i) + if(sphere_translations.size() != mesh.selected.size()) { - glProgramUniform1ui(shader_triangles, shader_triangles("idx_colormap"), meshes[i].idx_colormap); - glProgramUniform1ui(shader_pointcloud, shader_pointcloud("idx_colormap"), meshes[i].idx_colormap); + sphere_translations.resize(mesh.selected.size()); - glViewport(meshes[i].vx * viewport_width, meshes[i].vy * viewport_height, viewport_width, viewport_height); - - if(normals) - meshes[i].draw_point_cloud(program); - else if(meshes[i]->is_pointcloud() || render_pointcloud) - meshes[i].draw_point_cloud(shader_pointcloud); - else - meshes[i].draw(program); - } -} - -void viewer::draw_selected_vertices(shader & program) -{ - if(sphere_translations.size() != active_mesh().selected.size()) - { - sphere_translations.resize(active_mesh().selected.size()); - - for(index_t i = 0; i < active_mesh().selected.size(); ++i) - sphere_translations[i] = active_mesh()->gt(active_mesh().selected[i]); + for(index_t i = 0; i < mesh.selected.size(); ++i) + sphere_translations[i] = mesh->gt(mesh.selected[i]); sphere.update_instances_translations(sphere_translations); } - if(sphere_translations.size()) - { - glViewport(active_mesh().vx * viewport_width, active_mesh().vy * viewport_height, viewport_width, viewport_height); sphere.draw(program); - } } -void viewer::select_border_vertices() +void viewer::select_border_vertices(che_viewer & mesh) { - che_viewer & mesh = active_mesh(); - mesh.selected.clear(); vector bounds = mesh->bounds(); From 624bd3a0714a2c0c5657a2593dd76a4465846400 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Sep 2021 15:48:07 +0200 Subject: [PATCH 0553/1018] viewer: adding cam position, zoom on z parameter --- include/gproshan/mesh/quaternion.h | 2 +- include/gproshan/viewer/camera.h | 5 +++-- src/viewer/camera.cpp | 16 ++++++++++------ src/viewer/viewer.cpp | 6 +++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 850551b2..81d15adc 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -12,7 +12,7 @@ namespace gproshan { class quaternion { - protected: + public: real_t s; vertex v; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 10a6c922..c9a42594 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -19,10 +19,10 @@ class camera quaternion r_last = 1; public: - quaternion eye = vertex(0, 0, 2); + quaternion pos = vertex(0, 0, -2); + quaternion eye = vertex(0, 0, -2); quaternion center = vertex(0, 0, 0); quaternion up = vertex(0, 1, 0); - double zoom = 2; public: glm::mat4 look_at(const quaternion & r); @@ -31,6 +31,7 @@ class camera void motion(const double & x, const double & y, const int & w, const int & h); void zoom_in(); void zoom_out(); + const real_t & zoom() const; private: quaternion click_to_sphere(const double & x, const double & y, const int & w, const int & h); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index d6bf70df..b1d7e691 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -14,8 +14,7 @@ namespace gproshan { glm::mat4 camera::look_at(const quaternion & r) { - eye = vertex(0, 0, -zoom); - eye = r.conj() * eye * r; + eye = r.conj() * pos * r; return glm::lookAt( glm_vec3(eye), glm_vec3(r.conj() * center * r), @@ -65,12 +64,17 @@ void camera::motion(const double & x, const double & y, const int & w, const int void camera::zoom_in() { - zoom -= 0.02; + pos.v.z -= 0.02; } void camera::zoom_out() { - zoom += 0.02; + pos.v.z += 0.02; +} + +const real_t & camera::zoom() const +{ + return pos.v.z; } ostream & operator << (ostream & os, const camera & cam) @@ -79,7 +83,7 @@ ostream & operator << (ostream & os, const camera & cam) << cam.p_drag << "\n" << cam.p_last << "\n" << cam.r_last << "\n" - << cam.zoom << "\n"; + << cam.pos << "\n"; } istream & operator >> (istream & is, camera & cam) @@ -88,7 +92,7 @@ istream & operator >> (istream & is, camera & cam) >> cam.p_drag >> cam.p_last >> cam.r_last - >> cam.zoom; + >> cam.pos; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0126c8fb..af3e47de 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -791,7 +791,7 @@ void viewer::render_gl() glProgramUniform3f(shader_sphere, shader_sphere("light"), light[0], light[1], light[2]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom); + glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); @@ -803,11 +803,11 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom * 0.02); + glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom * 0.02); + glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); From 7fc851597466bf0b8b87e72fa8b255ae7a5119e8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 1 Oct 2021 15:21:43 +0200 Subject: [PATCH 0554/1018] app_viewer: computing/visualizing connected components --- include/gproshan/app_viewer.h | 1 + src/app_viewer.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index e86fb8aa..d7a4ad4a 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -56,6 +56,7 @@ class app_viewer : public viewer // Geometry static bool process_convex_hull(viewer * p_view); + static bool process_connected_components(viewer * p_view); static bool process_gaussian_curvature(viewer * p_view); static bool process_edge_collapse(viewer * p_view); static bool process_multiplicate_vertices(viewer * p_view); diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index f424f5f6..ad7083b0 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -57,6 +57,7 @@ void app_viewer::init() { sub_menus.push_back("Geometry"); add_process(GLFW_KEY_H, {"H", "2D Convex Hull", process_convex_hull}); + add_process(GLFW_KEY_C, {"C", "Connected Components", process_connected_components}); add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); add_process(GLFW_KEY_Q, {"Q", "Edge Collapse", process_edge_collapse}); add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); @@ -114,6 +115,16 @@ bool app_viewer::process_convex_hull(viewer * p_view) return false; } +bool app_viewer::process_connected_components(viewer * p_view) +{ + gproshan_log(APP_VIEWER); + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + + return false; +} + bool app_viewer::process_gaussian_curvature(viewer * p_view) { gproshan_log(APP_VIEWER); From 6660291693157ffd6bbd916293a0a5e0610a4957 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 1 Oct 2021 15:29:16 +0200 Subject: [PATCH 0555/1018] viewer: fix camera zoom --- include/gproshan/viewer/camera.h | 2 +- src/viewer/camera.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index c9a42594..6f8992d0 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -31,7 +31,7 @@ class camera void motion(const double & x, const double & y, const int & w, const int & h); void zoom_in(); void zoom_out(); - const real_t & zoom() const; + real_t zoom() const; private: quaternion click_to_sphere(const double & x, const double & y, const int & w, const int & h); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index b1d7e691..77efa92c 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -64,17 +64,17 @@ void camera::motion(const double & x, const double & y, const int & w, const int void camera::zoom_in() { - pos.v.z -= 0.02; + pos.v.z += 0.02; } void camera::zoom_out() { - pos.v.z += 0.02; + pos.v.z -= 0.02; } -const real_t & camera::zoom() const +real_t camera::zoom() const { - return pos.v.z; + return -pos.v.z; } ostream & operator << (ostream & os, const camera & cam) From 43c74b3d14d96fd6455a188ee8f652e123fd7124 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 14 Oct 2021 13:37:22 +0200 Subject: [PATCH 0556/1018] mdict: cleaning up extra includes macos: x11 problem between eigen and cimg --- CMakeLists.txt | 1 - include/gproshan/mdict/basis.h | 4 +++- include/gproshan/mdict/patch.h | 5 +---- src/mdict/msparse_coding.cpp | 4 ---- src/mdict/patch.cpp | 22 ---------------------- 5 files changed, 4 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c5489ca2..ea355d8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,6 @@ set(THREADS_PREFER_PTHREAD_FLAG ON) include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) -include_directories(SYSTEM ${X11_INCLUDE_DIR}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR}) include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 523909f6..86d00902 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -3,9 +3,11 @@ #include "include.h" -#include "include_arma.h" #include +#include "include_arma.h" + + using namespace std; diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 1ab0cc1a..da775125 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -11,14 +11,11 @@ #include "include_arma.h" -#include - #ifdef Success #undef Success #endif -using namespace cimg_library; using namespace std; @@ -104,7 +101,7 @@ class patch const a_vec normal(); bool is_covered( bool * covered); - void save(const real_t & radio, const size_t & imsize, CImgList & imlist); +// void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); void compute_avg_distance(che * mesh, vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 9e92aac5..9a6ad849 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -9,7 +9,6 @@ #include "viewer/viewer.h" #include -#include #include #ifndef CGAL_PATCH_DEFS @@ -24,9 +23,6 @@ #include -using namespace cimg_library; - - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index dd901b84..5e5e12ff 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -517,28 +517,6 @@ const a_vec patch::normal() return T.col(2); } -void patch::save(const real_t & radio, const size_t & imsize, CImgList & imlist) -{ - // Create images with the patches info - - //building the grid - CImg img(imsize, imsize); - size_t x, y; - img.fill(0); - // for each x y plus 1, multiply by delta and floor, get i and j - for(index_t i = 0; i < vertices.size(); ++i) - { - x = floor((xyz.col(i)[0] + radio) * (imsize - 1) / (2 * radio)); - y = floor((xyz.col(i)[1] + radio) * (imsize - 1) / (2 * radio)); - img(x,y) = xyz.col(i)[2]; - } - - img.resize(128, 128); - imlist.insert(img.normalize(0, 255)); - //img.save("tmp/images/test_image.jpg"); - -} - void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_toplevels, index_t * toplevel) { if(vertices.size()) vertices.clear(); From 7faa08c457baf43394c16fbda1a63a388912381d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 14 Oct 2021 14:13:52 +0200 Subject: [PATCH 0557/1018] viewer: fix pick up vertex for macos retina display --- src/viewer/viewer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index af3e47de..1fa321de 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -898,7 +898,9 @@ void viewer::select_border_vertices(che_viewer & mesh) void viewer::pick_vertex(const real_t & x, const real_t & y) { - active_mesh().select(x, y, {viewport_width, viewport_height}, view_mat, proj_mat); + active_mesh().select( x * viewport_width / window_width, + y * viewport_height / window_height, + {viewport_width, viewport_height}, view_mat, proj_mat); } From 9526935fdaf8def49bd54997a5c7249607dd1859 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 14 Oct 2021 16:36:24 +0200 Subject: [PATCH 0558/1018] app_viewer: connected components --- src/app_viewer.cpp | 5 +++++ src/viewer/viewer.cpp | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index ad7083b0..39e75cb6 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -121,6 +121,11 @@ bool app_viewer::process_connected_components(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); + real_t * label = &mesh->heatmap(0); + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + label[v] = -1; return false; } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1fa321de..e3536f00 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -644,6 +644,8 @@ bool viewer::set_render_gl(viewer * view) #ifdef GPROSHAN_EMBREE bool viewer::set_render_embree(viewer * view) { + che_viewer & mesh = view->active_mesh(); + static int rt_opt = 0; static double time = 0; @@ -659,9 +661,9 @@ bool viewer::set_render_embree(viewer * view) TIC(time); switch(rt_opt) { - case 0: view->rt_embree = new rt::embree({view->active_mesh()}); + case 0: view->rt_embree = new rt::embree({mesh}); break; - case 1: view->rt_embree = new rt::embree_splat({view->active_mesh()}, true); + case 1: view->rt_embree = new rt::embree_splat({mesh}, true); break; } TOC(time); From 01c1b475f3c91545ecd676892747dc17fc8a38e3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 16 Oct 2021 22:24:39 +0200 Subject: [PATCH 0559/1018] geodesics: heat method enumerate cuda --- include/gproshan/geodesics/heat_method.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index a828940f..c7d6360f 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -19,7 +19,13 @@ // geometry processing and shape analysis framework namespace gproshan { -enum heat_method_opt { HEAT_ARMA, HEAT_CHOLMOD, HEAT_CUDA }; +enum heat_method_opt { + HEAT_ARMA, + HEAT_CHOLMOD, + #ifdef GPROSHAN_CUDA + HEAT_CUDA + #endif // GPROSHAN_CUDA + }; double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); From 73ed1f0eaeca2a6cb135434a3a2198a39492109e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 17 Oct 2021 14:09:34 +0200 Subject: [PATCH 0560/1018] adding gproshan_cuda library --- CMakeLists.txt | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea355d8d..7e7be297 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) - set(CMAKE_CUDA_ARCHITECTURES 52 53 60 61 62 70 72 75 80) + set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) add_definitions(-DGPROSHAN_CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) @@ -56,9 +56,6 @@ find_package(SuiteSparse REQUIRED) find_package(Boost COMPONENTS thread system) -set(THREADS_PREFER_PTHREAD_FLAG ON) - - include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) @@ -76,13 +73,8 @@ add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) -if(CUDAToolkit_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) - list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) -endif(CUDAToolkit_FOUND) - -add_library(gproshan SHARED ${cpp_sources} ${cu_sources}) +add_library(gproshan SHARED ${cpp_sources}) target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan GLEW::GLEW) @@ -99,11 +91,17 @@ if(embree_FOUND) endif(embree_FOUND) if(CUDAToolkit_FOUND) - target_link_libraries(gproshan CUDA::cudart) - target_link_libraries(gproshan CUDA::cuda_driver) - target_link_libraries(gproshan CUDA::cublas) - target_link_libraries(gproshan CUDA::cusolver) - target_link_libraries(gproshan CUDA::cusparse) + FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) + list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + + add_library(gproshan_cuda SHARED ${cu_sources}) + + target_link_libraries(gproshan_cuda CUDA::cudart) + target_link_libraries(gproshan_cuda CUDA::cublas) + target_link_libraries(gproshan_cuda CUDA::cusolver) + target_link_libraries(gproshan_cuda CUDA::cusparse) + + target_link_libraries(gproshan gproshan_cuda) endif(CUDAToolkit_FOUND) From 7fb85ad1c7d28745a7f51c8650e394c7089bcaa9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 17 Oct 2021 14:45:03 +0200 Subject: [PATCH 0561/1018] adding gproshan_cuda library, cuda_driver dependency for optix --- CMakeLists.txt | 3 ++- include/gproshan/geodesics/heat_method.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7be297..51aed840 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,10 +97,11 @@ if(CUDAToolkit_FOUND) add_library(gproshan_cuda SHARED ${cu_sources}) target_link_libraries(gproshan_cuda CUDA::cudart) + target_link_libraries(gproshan_cuda CUDA::cuda_driver) target_link_libraries(gproshan_cuda CUDA::cublas) target_link_libraries(gproshan_cuda CUDA::cusolver) target_link_libraries(gproshan_cuda CUDA::cusparse) - + target_link_libraries(gproshan gproshan_cuda) endif(CUDAToolkit_FOUND) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index c7d6360f..6c48766f 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -19,7 +19,7 @@ // geometry processing and shape analysis framework namespace gproshan { -enum heat_method_opt { +enum heat_method_opt { HEAT_ARMA, HEAT_CHOLMOD, #ifdef GPROSHAN_CUDA From e62d1a2af8d02b32248dc5dece8f19d1a160ba03 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 17 Oct 2021 21:10:23 +0200 Subject: [PATCH 0562/1018] imgui: update to version 1.85 --- imgui/imconfig.h | 2 +- imgui/imgui.cpp | 1580 ++++++++++++++++++----------- imgui/imgui.h | 56 +- imgui/imgui_demo.cpp | 54 +- imgui/imgui_draw.cpp | 4 +- imgui/imgui_impl_glfw.cpp | 3 +- imgui/imgui_impl_opengl3.cpp | 38 +- imgui/imgui_impl_opengl3.h | 2 +- imgui/imgui_impl_opengl3_loader.h | 142 +-- imgui/imgui_internal.h | 361 ++++--- imgui/imgui_tables.cpp | 10 +- imgui/imgui_widgets.cpp | 269 ++--- 12 files changed, 1556 insertions(+), 965 deletions(-) diff --git a/imgui/imconfig.h b/imgui/imconfig.h index a0c86caa..7082c550 100644 --- a/imgui/imconfig.h +++ b/imgui/imconfig.h @@ -33,7 +33,7 @@ // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended. -//#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger window: ShowMetricsWindow() will be empty. +//#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger and other debug tools: ShowMetricsWindow() and ShowStackToolWindow() will be empty. //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index ab27f130..e6f206e7 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (main code and documentation) // Help: @@ -15,7 +15,10 @@ // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues -// - Discussions https://github.com/ocornut/imgui/discussions + +// Getting Started? +// - For first-time users having issues compiling/linking/running or issues loading fonts: +// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). @@ -79,6 +82,7 @@ CODE // [SECTION] VIEWPORTS // [SECTION] PLATFORM DEPENDENT HELPERS // [SECTION] METRICS/DEBUGGER WINDOW +// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) */ @@ -376,6 +380,7 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2021/08/23 (1.85) - removed GetWindowContentRegionWidth() function. keep inline redirection helper. can use 'GetWindowContentRegionMax().x - GetWindowContentRegionMin().x' instead for generally 'GetContentRegionAvail().x' is more useful. - 2021/07/26 (1.84) - commented out redirecting functions/enums names that were marked obsolete in 1.67 and 1.69 (March 2019): - ImGui::GetOverlayDrawList() -> use ImGui::GetForegroundDrawList() - ImFont::GlyphRangesBuilder -> use ImFontGlyphRangesBuilder @@ -906,30 +911,31 @@ namespace ImGui static void NavUpdate(); static void NavUpdateWindowing(); static void NavUpdateWindowingOverlay(); -static void NavUpdateMoveResult(); -static void NavUpdateInitResult(); +static void NavUpdateCancelRequest(); +static void NavUpdateCreateMoveRequest(); static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); static void NavEndFrame(); -static bool NavScoreItem(ImGuiNavItemData* result, ImRect cand); -static void NavApplyItemToResult(ImGuiNavItemData* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel); -static void NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, ImGuiID id); +static bool NavScoreItem(ImGuiNavItemData* result); +static void NavApplyItemToResult(ImGuiNavItemData* result); +static void NavProcessItem(); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static void NavRestoreLayer(ImGuiNavLayer layer); static int FindWindowFocusIndex(ImGuiWindow* window); -// Error Checking +// Error Checking and Debug Tools static void ErrorCheckNewFrameSanityChecks(); static void ErrorCheckEndFrameSanityChecks(); +static void UpdateDebugToolItemPicker(); +static void UpdateDebugToolStackQueries(); // Misc static void UpdateSettings(); static void UpdateMouseInputs(); static void UpdateMouseWheel(); static void UpdateTabFocus(); -static void UpdateDebugToolItemPicker(); static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); @@ -1181,13 +1187,8 @@ void ImGuiIO::ClearInputCharacters() InputQueueCharacters.resize(0); } -void ImGuiIO::AddFocusEvent(bool focused) +void ImGuiIO::ClearInputKeys() { - if (focused) - return; - - // Clear buttons state when focus is lost - // (this is useful so e.g. releasing Alt after focus loss on Alt-Tab doesn't trigger the Alt menu toggle) memset(KeysDown, 0, sizeof(KeysDown)); for (int n = 0; n < IM_ARRAYSIZE(KeysDownDuration); n++) KeysDownDuration[n] = KeysDownDurationPrev[n] = -1.0f; @@ -1197,6 +1198,13 @@ void ImGuiIO::AddFocusEvent(bool focused) NavInputsDownDuration[n] = NavInputsDownDurationPrev[n] = -1.0f; } +void ImGuiIO::AddFocusEvent(bool focused) +{ + // We intentionally overwrite this and process in NewFrame(), in order to give a chance + // to multi-viewports backends to queue AddFocusEvent(false),AddFocusEvent(true) in same frame. + AppFocusLost = !focused; +} + //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (Geometry functions) //----------------------------------------------------------------------------- @@ -2253,21 +2261,21 @@ void ImGui::CalcListClipping(int items_count, float items_height, int* out_items return; } - // We create the union of the ClipRect and the NavScoringRect which at worst should be 1 page away from ClipRect + // We create the union of the ClipRect and the scoring rect which at worst should be 1 page away from ClipRect ImRect unclipped_rect = window->ClipRect; - if (g.NavMoveRequest) + if (g.NavMoveScoringItems) unclipped_rect.Add(g.NavScoringRect); if (g.NavJustMovedToId && window->NavLastIds[0] == g.NavJustMovedToId) - unclipped_rect.Add(ImRect(window->Pos + window->NavRectRel[0].Min, window->Pos + window->NavRectRel[0].Max)); + unclipped_rect.Add(ImRect(window->Pos + window->NavRectRel[0].Min, window->Pos + window->NavRectRel[0].Max)); // Could store and use NavJustMovedToRectRel const ImVec2 pos = window->DC.CursorPos; int start = (int)((unclipped_rect.Min.y - pos.y) / items_height); int end = (int)((unclipped_rect.Max.y - pos.y) / items_height); // When performing a navigation request, ensure we have one item extra in the direction we are moving to - if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Up) + if (g.NavMoveScoringItems && g.NavMoveClipDir == ImGuiDir_Up) start--; - if (g.NavMoveRequest && g.NavMoveClipDir == ImGuiDir_Down) + if (g.NavMoveScoringItems && g.NavMoveClipDir == ImGuiDir_Down) end++; start = ImClamp(start, 0, items_count); @@ -2961,10 +2969,9 @@ ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); ImGui::KeepAliveID(id); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); return id; } @@ -2973,10 +2980,9 @@ ImGuiID ImGuiWindow::GetID(const void* ptr) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); ImGui::KeepAliveID(id); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_Pointer, ptr); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); return id; } @@ -2985,10 +2991,9 @@ ImGuiID ImGuiWindow::GetID(int n) ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&n, sizeof(n), seed); ImGui::KeepAliveID(id); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_S32, (intptr_t)n); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); return id; } @@ -2996,10 +3001,9 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); return id; } @@ -3007,10 +3011,9 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_Pointer, ptr); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); return id; } @@ -3018,10 +3021,9 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&n, sizeof(n), seed); -#ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO(id, ImGuiDataType_S32, (intptr_t)n); -#endif + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); return id; } @@ -3102,7 +3104,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) if (id) { g.ActiveIdIsAlive = id; - g.ActiveIdSource = (g.NavActivateId == id || g.NavInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; } // Clear declaration of inputs claimed by the widget @@ -3193,7 +3195,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) ImGuiItemStatusFlags status_flags = g.LastItemData.StatusFlags; if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) return false; - IM_ASSERT((flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) == 0); // Flags not supported by this function + IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function // Test if we are hovering the right window (our window could be behind another window) // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) @@ -3279,20 +3281,21 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) return true; } -bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged) +bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!bb.Overlaps(window->ClipRect)) if (id == 0 || (id != g.ActiveId && id != g.NavId)) - if (clip_even_when_logged || !g.LogEnabled) + if (!g.LogEnabled) return true; return false; } // Called by ItemAdd() // Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. -void ImGui::ItemFocusable(ImGuiWindow* window, ImGuiID id) +// [WIP] This will eventually be refactored and moved into NavProcessItem() +void ImGui::ItemInputable(ImGuiWindow* window, ImGuiID id) { ImGuiContext& g = *GImGui; IM_ASSERT(id != 0 && id == g.LastItemData.ID); @@ -3300,7 +3303,6 @@ void ImGui::ItemFocusable(ImGuiWindow* window, ImGuiID id) // Increment counters // FIXME: ImGuiItemFlags_Disabled should disable more. const bool is_tab_stop = (g.LastItemData.InFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; - window->DC.FocusCounterRegular++; if (is_tab_stop) { window->DC.FocusCounterTabStop++; @@ -3310,7 +3312,7 @@ void ImGui::ItemFocusable(ImGuiWindow* window, ImGuiID id) // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. // (Note that we can always TAB out of a widget that doesn't allow tabbing in) - if (g.ActiveId == id && g.TabFocusPressed && !IsActiveIdUsingKey(ImGuiKey_Tab) && g.TabFocusRequestNextWindow == NULL) + if (g.ActiveId == id && g.TabFocusPressed && g.TabFocusRequestNextWindow == NULL) { g.TabFocusRequestNextWindow = window; g.TabFocusRequestNextCounterTabStop = window->DC.FocusCounterTabStop + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. @@ -3319,14 +3321,9 @@ void ImGui::ItemFocusable(ImGuiWindow* window, ImGuiID id) // Handle focus requests if (g.TabFocusRequestCurrWindow == window) { - if (window->DC.FocusCounterRegular == g.TabFocusRequestCurrCounterRegular) - { - g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_FocusedByCode; - return; - } if (is_tab_stop && window->DC.FocusCounterTabStop == g.TabFocusRequestCurrCounterTabStop) { - g.NavJustTabbedId = id; + g.NavJustTabbedId = id; // FIXME-NAV: aim to eventually set in NavUpdate() once we finish the refactor g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_FocusedByTabbing; return; } @@ -3679,13 +3676,15 @@ static void ImGui::UpdateMouseInputs() // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) if (IsMousePosValid(&g.IO.MousePos)) - g.IO.MousePos = g.LastValidMousePos = ImFloor(g.IO.MousePos); + g.IO.MousePos = g.MouseLastValidPos = ImFloor(g.IO.MousePos); // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev)) g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; else g.IO.MouseDelta = ImVec2(0.0f, 0.0f); + + // If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f) g.NavDisableMouseHover = false; @@ -3830,7 +3829,10 @@ void ImGui::UpdateTabFocus() ImGuiContext& g = *GImGui; // Pressing TAB activate widget focus - g.TabFocusPressed = (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab)); + g.TabFocusPressed = false; + if (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) + if (!g.IO.KeyCtrl && !g.IO.KeyAlt && IsKeyPressedMap(ImGuiKey_Tab) && !IsActiveIdUsingKey(ImGuiKey_Tab)) + g.TabFocusPressed = true; if (g.ActiveId == 0 && g.TabFocusPressed) { // - This path is only taken when no widget are active/tabbed-into yet. @@ -3838,7 +3840,6 @@ void ImGui::UpdateTabFocus() // - Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also // manipulate the Next fields here even though they will be turned into Curr fields below. g.TabFocusRequestNextWindow = g.NavWindow; - g.TabFocusRequestNextCounterRegular = INT_MAX; if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) g.TabFocusRequestNextCounterTabStop = g.NavIdTabCounter + (g.IO.KeyShift ? -1 : 0); else @@ -3847,17 +3848,15 @@ void ImGui::UpdateTabFocus() // Turn queued focus request into current one g.TabFocusRequestCurrWindow = NULL; - g.TabFocusRequestCurrCounterRegular = g.TabFocusRequestCurrCounterTabStop = INT_MAX; + g.TabFocusRequestCurrCounterTabStop = INT_MAX; if (g.TabFocusRequestNextWindow != NULL) { ImGuiWindow* window = g.TabFocusRequestNextWindow; g.TabFocusRequestCurrWindow = window; - if (g.TabFocusRequestNextCounterRegular != INT_MAX && window->DC.FocusCounterRegular != -1) - g.TabFocusRequestCurrCounterRegular = ImModPositive(g.TabFocusRequestNextCounterRegular, window->DC.FocusCounterRegular + 1); if (g.TabFocusRequestNextCounterTabStop != INT_MAX && window->DC.FocusCounterTabStop != -1) g.TabFocusRequestCurrCounterTabStop = ImModPositive(g.TabFocusRequestNextCounterTabStop, window->DC.FocusCounterTabStop + 1); g.TabFocusRequestNextWindow = NULL; - g.TabFocusRequestNextCounterRegular = g.TabFocusRequestNextCounterTabStop = INT_MAX; + g.TabFocusRequestNextCounterTabStop = INT_MAX; } g.NavIdTabCounter = INT_MAX; @@ -3867,6 +3866,7 @@ void ImGui::UpdateTabFocus() void ImGui::UpdateHoveredWindowAndCaptureFlags() { ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; g.WindowsHoverPadding = ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_HOVER_PADDING, WINDOWS_HOVER_PADDING)); // Find the window hovered by mouse: @@ -3878,52 +3878,65 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // Modal windows prevents mouse from hovering behind them. ImGuiWindow* modal_window = GetTopMostPopupModal(); - if (modal_window && g.HoveredWindow && !IsWindowChildOf(g.HoveredWindow->RootWindow, modal_window)) + if (modal_window && g.HoveredWindow && !IsWindowChildOf(g.HoveredWindow->RootWindow, modal_window, true)) clear_hovered_windows = true; // Disabled mouse? - if (g.IO.ConfigFlags & ImGuiConfigFlags_NoMouse) + if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) clear_hovered_windows = true; - // We track click ownership. When clicked outside of a window the click is owned by the application and won't report hovering nor request capture even while dragging over our windows afterward. - int mouse_earliest_button_down = -1; + // We track click ownership. When clicked outside of a window the click is owned by the application and + // won't report hovering nor request capture even while dragging over our windows afterward. + const bool has_open_popup = (g.OpenPopupStack.Size > 0); + const bool has_open_modal = (modal_window != NULL); + int mouse_earliest_down = -1; bool mouse_any_down = false; - for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) + for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) { - if (g.IO.MouseClicked[i]) - g.IO.MouseDownOwned[i] = (g.HoveredWindow != NULL) || (g.OpenPopupStack.Size > 0); - mouse_any_down |= g.IO.MouseDown[i]; - if (g.IO.MouseDown[i]) - if (mouse_earliest_button_down == -1 || g.IO.MouseClickedTime[i] < g.IO.MouseClickedTime[mouse_earliest_button_down]) - mouse_earliest_button_down = i; + if (io.MouseClicked[i]) + { + io.MouseDownOwned[i] = (g.HoveredWindow != NULL) || has_open_popup; + io.MouseDownOwnedUnlessPopupClose[i] = (g.HoveredWindow != NULL) || has_open_modal; + } + mouse_any_down |= io.MouseDown[i]; + if (io.MouseDown[i]) + if (mouse_earliest_down == -1 || io.MouseClickedTime[i] < io.MouseClickedTime[mouse_earliest_down]) + mouse_earliest_down = i; } - const bool mouse_avail_to_imgui = (mouse_earliest_button_down == -1) || g.IO.MouseDownOwned[mouse_earliest_button_down]; + const bool mouse_avail = (mouse_earliest_down == -1) || io.MouseDownOwned[mouse_earliest_down]; + const bool mouse_avail_unless_popup_close = (mouse_earliest_down == -1) || io.MouseDownOwnedUnlessPopupClose[mouse_earliest_down]; // If mouse was first clicked outside of ImGui bounds we also cancel out hovering. // FIXME: For patterns of drag and drop across OS windows, we may need to rework/remove this test (first committed 311c0ca9 on 2015/02) const bool mouse_dragging_extern_payload = g.DragDropActive && (g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) != 0; - if (!mouse_avail_to_imgui && !mouse_dragging_extern_payload) + if (!mouse_avail && !mouse_dragging_extern_payload) clear_hovered_windows = true; if (clear_hovered_windows) g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL; - // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to imgui, false = dispatch mouse info to Dear ImGui + app) + // Update io.WantCaptureMouse for the user application (true = dispatch mouse info to Dear ImGui only, false = dispatch mouse to Dear ImGui + underlying app) + // Update io.WantCaptureMouseAllowPopupClose (experimental) to give a chance for app to react to popup closure with a drag if (g.WantCaptureMouseNextFrame != -1) - g.IO.WantCaptureMouse = (g.WantCaptureMouseNextFrame != 0); + { + io.WantCaptureMouse = io.WantCaptureMouseUnlessPopupClose = (g.WantCaptureMouseNextFrame != 0); + } else - g.IO.WantCaptureMouse = (mouse_avail_to_imgui && (g.HoveredWindow != NULL || mouse_any_down)) || (g.OpenPopupStack.Size > 0); + { + io.WantCaptureMouse = (mouse_avail && (g.HoveredWindow != NULL || mouse_any_down)) || has_open_popup; + io.WantCaptureMouseUnlessPopupClose = (mouse_avail_unless_popup_close && (g.HoveredWindow != NULL || mouse_any_down)) || has_open_modal; + } - // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to imgui, false = dispatch keyboard info to Dear ImGui + app) + // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to Dear ImGui only, false = dispatch keyboard info to Dear ImGui + underlying app) if (g.WantCaptureKeyboardNextFrame != -1) - g.IO.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); + io.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); else - g.IO.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); - if (g.IO.NavActive && (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) - g.IO.WantCaptureKeyboard = true; + io.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); + if (io.NavActive && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) + io.WantCaptureKeyboard = true; // Update io.WantTextInput flag, this is to allow systems without a keyboard (e.g. mobile, hand-held) to show a software keyboard if possible - g.IO.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; + io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; } ImGuiKeyModFlags ImGui::GetMergedKeyModFlags() @@ -4049,6 +4062,18 @@ void ImGui::NewFrame() g.DragDropWithinTarget = false; g.DragDropHoldJustPressedId = 0; + // Close popups on focus lost (currently wip/opt-in) + //if (g.IO.AppFocusLost) + // ClosePopupsExceptModals(); + + // Clear buttons state when focus is lost + // (this is useful so e.g. releasing Alt after focus loss on Alt-Tab doesn't trigger the Alt menu toggle) + if (g.IO.AppFocusLost) + { + g.IO.ClearInputKeys(); + g.IO.AppFocusLost = false; + } + // Update keyboard input state // Synchronize io.KeyMods with individual modifiers io.KeyXXX bools g.IO.KeyMods = GetMergedKeyModFlags(); @@ -4124,8 +4149,9 @@ void ImGui::NewFrame() g.ItemFlagsStack.push_back(ImGuiItemFlags_None); g.GroupStack.resize(0); - // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. + // [DEBUG] Update debug features UpdateDebugToolItemPicker(); + UpdateDebugToolStackQueries(); // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. @@ -4138,31 +4164,6 @@ void ImGui::NewFrame() CallContextHooks(&g, ImGuiContextHookType_NewFramePost); } -// [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. -void ImGui::UpdateDebugToolItemPicker() -{ - ImGuiContext& g = *GImGui; - g.DebugItemPickerBreakId = 0; - if (g.DebugItemPickerActive) - { - const ImGuiID hovered_id = g.HoveredIdPreviousFrame; - SetMouseCursor(ImGuiMouseCursor_Hand); - if (IsKeyPressedMap(ImGuiKey_Escape)) - g.DebugItemPickerActive = false; - if (IsMouseClicked(0) && hovered_id) - { - g.DebugItemPickerBreakId = hovered_id; - g.DebugItemPickerActive = false; - } - SetNextWindowBgAlpha(0.60f); - BeginTooltip(); - Text("HoveredId: 0x%08X", hovered_id); - Text("Press ESC to abort picking."); - TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); - EndTooltip(); - } -} - void ImGui::Initialize(ImGuiContext* context) { ImGuiContext& g = *context; @@ -5097,7 +5098,7 @@ void ImGui::EndChild() ItemAdd(bb, window->ChildId); RenderNavHighlight(bb, window->ChildId); - // When browsing a window that has no activable items (scroll only) we keep a highlight on the child + // When browsing a window that has no activable items (scroll only) we keep a highlight on the child (pass g.NavId to trick into always displaying) if (window->DC.NavLayersActiveMask == 0 && window == g.NavWindow) RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); } @@ -5743,9 +5744,11 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window) { window->ParentWindow = parent_window; - window->RootWindow = window->RootWindowForTitleBarHighlight = window->RootWindowForNav = window; + window->RootWindow = window->RootWindowPopupTree = window->RootWindowForTitleBarHighlight = window->RootWindowForNav = window; if (parent_window && (flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Tooltip)) window->RootWindow = parent_window->RootWindow; + if (parent_window && (flags & ImGuiWindowFlags_Popup)) + window->RootWindowPopupTree = parent_window->RootWindowPopupTree; if (parent_window && !(flags & ImGuiWindowFlags_Modal) && (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup))) window->RootWindowForTitleBarHighlight = parent_window->RootWindowForTitleBarHighlight; while (window->RootWindowForNav->Flags & ImGuiWindowFlags_NavFlattened) @@ -5824,12 +5827,12 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Add to stack // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() + g.CurrentWindow = window; ImGuiWindowStackData window_stack_data; window_stack_data.Window = window; window_stack_data.ParentLastItemDataBackup = g.LastItemData; + window_stack_data.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindowStack.push_back(window_stack_data); - g.CurrentWindow = window; - window->DC.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindow = NULL; if (flags & ImGuiWindowFlags_Popup) @@ -6212,16 +6215,21 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha * 0.25f), g.Style.WindowRounding); } - // Since 1.71, child window can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call. + // Child windows can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call (since 1.71) // When using overlapping child windows, this will break the assumption that child z-order is mapped to submission order. - // We disable this when the parent window has zero vertices, which is a common pattern leading to laying out multiple overlapping child. - // We also disabled this when we have dimming overlay behind this specific one child. - // FIXME: More code may rely on explicit sorting of overlapping child window and would need to disable this somehow. Please get in contact if you are affected. + // FIXME: User code may rely on explicit sorting of overlapping child window and would need to disable this somehow. Please get in contact if you are affected (github #4493) { bool render_decorations_in_parent = false; if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !window_is_child_tooltip) - if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_window->DrawList->VtxBuffer.Size > 0) + { + // - We test overlap with the previous child window only (testing all would end up being O(log N) not a good investment here) + // - We disable this when the parent window has zero vertices, which is a common pattern leading to laying out multiple overlapping childs + ImGuiWindow* previous_child = parent_window->DC.ChildWindows.Size >= 2 ? parent_window->DC.ChildWindows[parent_window->DC.ChildWindows.Size - 2] : NULL; + bool previous_child_overlapping = previous_child ? previous_child->Rect().Overlaps(window->Rect()) : false; + bool parent_is_empty = parent_window->DrawList->VtxBuffer.Size > 0; + if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_is_empty && !previous_child_overlapping) render_decorations_in_parent = true; + } if (render_decorations_in_parent) window->DrawList = parent_window->DrawList; @@ -6302,7 +6310,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CurrentColumns = NULL; window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical; - window->DC.FocusCounterRegular = window->DC.FocusCounterTabStop = -1; + window->DC.FocusCounterTabStop = -1; window->DC.ItemWidth = window->ItemWidthDefault; window->DC.TextWrapPos = -1.0f; // disabled @@ -6439,10 +6447,10 @@ void ImGui::End() // Pop from window stack g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; - g.CurrentWindowStack.pop_back(); if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - window->DC.StackSizesOnBegin.CompareWithCurrentState(); + g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithCurrentState(); + g.CurrentWindowStack.pop_back(); SetCurrentWindow(g.CurrentWindowStack.Size == 0 ? NULL : g.CurrentWindowStack.back().Window); } @@ -6510,7 +6518,7 @@ void ImGui::FocusWindow(ImGuiWindow* window) g.NavFocusScopeId = 0; g.NavIdIsAlive = false; g.NavLayer = ImGuiNavLayer_Main; - g.NavInitRequest = g.NavMoveRequest = false; + g.NavInitRequest = g.NavMoveSubmitted = g.NavMoveScoringItems = false; NavUpdateAnyRequestFlag(); //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); } @@ -6543,8 +6551,18 @@ void ImGui::FocusWindow(ImGuiWindow* window) void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window) { ImGuiContext& g = *GImGui; - - const int start_idx = ((under_this_window != NULL) ? FindWindowFocusIndex(under_this_window) : g.WindowsFocusOrder.Size) - 1; + int start_idx = g.WindowsFocusOrder.Size - 1; + if (under_this_window != NULL) + { + // Aim at root window behind us, if we are in a child window that's our own root (see #4640) + int offset = -1; + while (under_this_window->Flags & ImGuiWindowFlags_ChildWindow) + { + under_this_window = under_this_window->ParentWindow; + offset = 0; + } + start_idx = FindWindowFocusIndex(under_this_window) + offset; + } for (int i = start_idx; i >= 0; i--) { // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user. @@ -6618,7 +6636,7 @@ void ImGui::PopItemFlag() } // BeginDisabled()/EndDisabled() -// - Those can be nested but this cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep things disabled) +// - Those can be nested but it cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep everything disabled) // - Visually this is currently altering alpha, but it is expected that in a future styling system this would work differently. // - Feedback welcome at https://github.com/ocornut/imgui/issues/211 // - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. @@ -6635,11 +6653,14 @@ void ImGui::BeginDisabled(bool disabled) if (was_disabled || disabled) g.CurrentItemFlags |= ImGuiItemFlags_Disabled; g.ItemFlagsStack.push_back(g.CurrentItemFlags); + g.DisabledStackSize++; } void ImGui::EndDisabled() { ImGuiContext& g = *GImGui; + IM_ASSERT(g.DisabledStackSize > 0); + g.DisabledStackSize--; bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; //PopItemFlag(); g.ItemFlagsStack.pop_back(); @@ -6683,14 +6704,25 @@ void ImGui::PopTextWrapPos() window->DC.TextWrapPosStack.pop_back(); } -bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent) +static ImGuiWindow* GetCombinedRootWindow(ImGuiWindow* window, bool popup_hierarchy) +{ + window = window->RootWindow; + if (popup_hierarchy) + window = window->RootWindowPopupTree; + return window; +} + +bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, bool popup_hierarchy) { - if (window->RootWindow == potential_parent) + ImGuiWindow* window_root = GetCombinedRootWindow(window, popup_hierarchy); + if (window_root == potential_parent) return true; while (window != NULL) { if (window == potential_parent) return true; + if (window == window_root) // end of chain + return false; window = window->ParentWindow; } return false; @@ -6712,39 +6744,33 @@ bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_b bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { - IM_ASSERT((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0); // Flags not supported by this function + IM_ASSERT((flags & (ImGuiHoveredFlags_AllowWhenOverlapped | ImGuiHoveredFlags_AllowWhenDisabled)) == 0); // Flags not supported by this function ImGuiContext& g = *GImGui; - if (g.HoveredWindow == NULL) + ImGuiWindow* ref_window = g.HoveredWindow; + ImGuiWindow* cur_window = g.CurrentWindow; + if (ref_window == NULL) return false; if ((flags & ImGuiHoveredFlags_AnyWindow) == 0) { - ImGuiWindow* window = g.CurrentWindow; - switch (flags & (ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows)) - { - case ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows: - if (g.HoveredWindow->RootWindow != window->RootWindow) - return false; - break; - case ImGuiHoveredFlags_RootWindow: - if (g.HoveredWindow != window->RootWindow) - return false; - break; - case ImGuiHoveredFlags_ChildWindows: - if (!IsWindowChildOf(g.HoveredWindow, window)) - return false; - break; - default: - if (g.HoveredWindow != window) - return false; - break; - } + IM_ASSERT(cur_window); // Not inside a Begin()/End() + const bool popup_hierarchy = (flags & ImGuiHoveredFlags_NoPopupHierarchy) == 0; + if (flags & ImGuiHoveredFlags_RootWindow) + cur_window = GetCombinedRootWindow(cur_window, popup_hierarchy); + + bool result; + if (flags & ImGuiHoveredFlags_ChildWindows) + result = IsWindowChildOf(ref_window, cur_window, popup_hierarchy); + else + result = (ref_window == cur_window); + if (!result) + return false; } - if (!IsWindowContentHoverable(g.HoveredWindow, flags)) + if (!IsWindowContentHoverable(ref_window, flags)) return false; if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) - if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != g.HoveredWindow->MoveId) + if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != ref_window->MoveId) return false; return true; } @@ -6752,22 +6778,23 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) { ImGuiContext& g = *GImGui; + ImGuiWindow* ref_window = g.NavWindow; + ImGuiWindow* cur_window = g.CurrentWindow; + if (ref_window == NULL) + return false; if (flags & ImGuiFocusedFlags_AnyWindow) - return g.NavWindow != NULL; + return true; + IM_ASSERT(cur_window); // Not inside a Begin()/End() - IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End() - switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows)) - { - case ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows: - return g.NavWindow && g.NavWindow->RootWindow == g.CurrentWindow->RootWindow; - case ImGuiFocusedFlags_RootWindow: - return g.NavWindow == g.CurrentWindow->RootWindow; - case ImGuiFocusedFlags_ChildWindows: - return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow); - default: - return g.NavWindow == g.CurrentWindow; - } + const bool popup_hierarchy = (flags & ImGuiFocusedFlags_NoPopupHierarchy) == 0; + if (flags & ImGuiHoveredFlags_RootWindow) + cur_window = GetCombinedRootWindow(cur_window, popup_hierarchy); + + if (flags & ImGuiHoveredFlags_ChildWindows) + return IsWindowChildOf(ref_window, cur_window, popup_hierarchy); + else + return (ref_window == cur_window); } // Can we focus this window with CTRL+TAB (or PadMenu + PadFocusPrev/PadFocusNext) @@ -7037,6 +7064,7 @@ void ImGui::ActivateItem(ImGuiID id) { ImGuiContext& g = *GImGui; g.NavNextActivateId = id; + g.NavNextActivateFlags = ImGuiActivateFlags_None; } void ImGui::PushFocusScope(ImGuiID id) @@ -7058,12 +7086,16 @@ void ImGui::PopFocusScope() void ImGui::SetKeyboardFocusHere(int offset) { - IM_ASSERT(offset >= -1); // -1 is allowed but not below ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - g.TabFocusRequestNextWindow = window; - g.TabFocusRequestNextCounterRegular = window->DC.FocusCounterRegular + 1 + offset; - g.TabFocusRequestNextCounterTabStop = INT_MAX; + IM_ASSERT(offset >= -1); // -1 is allowed but not below + g.NavWindow = window; + ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; + NavMoveRequestSubmit(ImGuiDir_None, ImGuiDir_None, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + if (offset == -1) + NavMoveRequestResolveWithLastItem(); + else + g.NavTabbingInputableRemaining = offset + 1; } void ImGui::SetItemDefaultFocus() @@ -7072,15 +7104,17 @@ void ImGui::SetItemDefaultFocus() ImGuiWindow* window = g.CurrentWindow; if (!window->Appearing) return; - if (g.NavWindow == window->RootWindowForNav && (g.NavInitRequest || g.NavInitResultId != 0) && g.NavLayer == window->DC.NavLayerCurrent) - { - g.NavInitRequest = false; - g.NavInitResultId = g.LastItemData.ID; - g.NavInitResultRectRel = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); - NavUpdateAnyRequestFlag(); - if (!IsItemVisible()) - SetScrollHereY(); - } + if (g.NavWindow != window->RootWindowForNav || (!g.NavInitRequest && g.NavInitResultId == 0) || g.NavLayer != window->DC.NavLayerCurrent) + return; + + g.NavInitRequest = false; + g.NavInitResultId = g.LastItemData.ID; + g.NavInitResultRectRel = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); + NavUpdateAnyRequestFlag(); + + // Scroll could be done in NavInitRequestApplyResult() via a opt-in flag (we however don't want regular init requests to scroll) + if (!IsItemVisible()) + ScrollToRectEx(window, g.LastItemData.Rect, ImGuiScrollFlags_None); } void ImGui::SetStateStorage(ImGuiStorage* tree) @@ -7132,6 +7166,8 @@ void ImGui::PushOverrideID(ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + if (g.DebugHookIdInfo == id) + DebugHookIdInfo(id, ImGuiDataType_ID, NULL, NULL); window->IDStack.push_back(id); } @@ -7141,11 +7177,10 @@ void ImGui::PushOverrideID(ImGuiID id) ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) { ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - ImGui::KeepAliveID(id); -#ifdef IMGUI_ENABLE_TEST_ENGINE + KeepAliveID(id); ImGuiContext& g = *GImGui; - IMGUI_TEST_ENGINE_ID_INFO2(id, ImGuiDataType_String, str, str_end); -#endif + if (g.DebugHookIdInfo == id) + DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); return id; } @@ -7290,53 +7325,13 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi { // PVS-Studio V1044 is "Loop break conditions do not depend on the number of iterations" ImGuiContext& g = *GImGui; - while (g.CurrentWindowStack.Size > 0) + while (g.CurrentWindowStack.Size > 0) //-V1044 { - while (g.CurrentTable && (g.CurrentTable->OuterWindow == g.CurrentWindow || g.CurrentTable->InnerWindow == g.CurrentWindow)) - { - if (log_callback) log_callback(user_data, "Recovered from missing EndTable() in '%s'", g.CurrentTable->OuterWindow->Name); - EndTable(); - } + ErrorCheckEndWindowRecover(log_callback, user_data); ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(window != NULL); - while (g.CurrentTabBar != NULL) //-V1044 - { - if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); - EndTabBar(); - } - while (window->DC.TreeDepth > 0) - { - if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); - TreePop(); - } - while (g.GroupStack.Size > window->DC.StackSizesOnBegin.SizeOfGroupStack) - { - if (log_callback) log_callback(user_data, "Recovered from missing EndGroup() in '%s'", window->Name); - EndGroup(); - } - while (window->IDStack.Size > 1) - { - if (log_callback) log_callback(user_data, "Recovered from missing PopID() in '%s'", window->Name); - PopID(); - } - while (g.ColorStack.Size > window->DC.StackSizesOnBegin.SizeOfColorStack) - { - if (log_callback) log_callback(user_data, "Recovered from missing PopStyleColor() in '%s' for ImGuiCol_%s", window->Name, GetStyleColorName(g.ColorStack.back().Col)); - PopStyleColor(); - } - while (g.StyleVarStack.Size > window->DC.StackSizesOnBegin.SizeOfStyleVarStack) - { - if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); - PopStyleVar(); - } - while (g.FocusScopeStack.Size > window->DC.StackSizesOnBegin.SizeOfFocusScopeStack) - { - if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); - PopFocusScope(); - } if (g.CurrentWindowStack.Size == 1) { - IM_ASSERT(g.CurrentWindow->IsFallbackWindow); + IM_ASSERT(window->IsFallbackWindow); break; } IM_ASSERT(window == g.CurrentWindow); @@ -7353,6 +7348,66 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi } } +// Must be called before End()/EndChild() +void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data) +{ + ImGuiContext& g = *GImGui; + while (g.CurrentTable && (g.CurrentTable->OuterWindow == g.CurrentWindow || g.CurrentTable->InnerWindow == g.CurrentWindow)) + { + if (log_callback) log_callback(user_data, "Recovered from missing EndTable() in '%s'", g.CurrentTable->OuterWindow->Name); + EndTable(); + } + + ImGuiWindow* window = g.CurrentWindow; + ImGuiStackSizes* stack_sizes = &g.CurrentWindowStack.back().StackSizesOnBegin; + IM_ASSERT(window != NULL); + while (g.CurrentTabBar != NULL) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); + EndTabBar(); + } + while (window->DC.TreeDepth > 0) + { + if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); + TreePop(); + } + while (g.GroupStack.Size > stack_sizes->SizeOfGroupStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing EndGroup() in '%s'", window->Name); + EndGroup(); + } + while (window->IDStack.Size > 1) + { + if (log_callback) log_callback(user_data, "Recovered from missing PopID() in '%s'", window->Name); + PopID(); + } + while (g.DisabledStackSize > stack_sizes->SizeOfDisabledStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing EndDisabled() in '%s'", window->Name); + EndDisabled(); + } + while (g.ColorStack.Size > stack_sizes->SizeOfColorStack) + { + if (log_callback) log_callback(user_data, "Recovered from missing PopStyleColor() in '%s' for ImGuiCol_%s", window->Name, GetStyleColorName(g.ColorStack.back().Col)); + PopStyleColor(); + } + while (g.ItemFlagsStack.Size > stack_sizes->SizeOfItemFlagsStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopItemFlag() in '%s'", window->Name); + PopItemFlag(); + } + while (g.StyleVarStack.Size > stack_sizes->SizeOfStyleVarStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); + PopStyleVar(); + } + while (g.FocusScopeStack.Size > stack_sizes->SizeOfFocusScopeStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); + PopFocusScope(); + } +} + // Save current stack sizes for later compare void ImGuiStackSizes::SetToCurrentState() { @@ -7364,7 +7419,9 @@ void ImGuiStackSizes::SetToCurrentState() SizeOfFontStack = (short)g.FontStack.Size; SizeOfFocusScopeStack = (short)g.FocusScopeStack.Size; SizeOfGroupStack = (short)g.GroupStack.Size; + SizeOfItemFlagsStack = (short)g.ItemFlagsStack.Size; SizeOfBeginPopupStack = (short)g.BeginPopupStack.Size; + SizeOfDisabledStack = (short)g.DisabledStackSize; } // Compare to detect usage errors @@ -7382,6 +7439,8 @@ void ImGuiStackSizes::CompareWithCurrentState() // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. IM_ASSERT(SizeOfGroupStack == g.GroupStack.Size && "BeginGroup/EndGroup Mismatch!"); IM_ASSERT(SizeOfBeginPopupStack == g.BeginPopupStack.Size && "BeginPopup/EndPopup or BeginMenu/EndMenu Mismatch!"); + IM_ASSERT(SizeOfDisabledStack == g.DisabledStackSize && "BeginDisabled/EndDisabled Mismatch!"); + IM_ASSERT(SizeOfItemFlagsStack >= g.ItemFlagsStack.Size && "PushItemFlag/PopItemFlag Mismatch!"); IM_ASSERT(SizeOfColorStack >= g.ColorStack.Size && "PushStyleColor/PopStyleColor Mismatch!"); IM_ASSERT(SizeOfStyleVarStack >= g.StyleVarStack.Size && "PushStyleVar/PopStyleVar Mismatch!"); IM_ASSERT(SizeOfFontStack >= g.FontStack.Size && "PushFont/PopFont Mismatch!"); @@ -7416,7 +7475,6 @@ void ImGuiStackSizes::CompareWithCurrentState() // - GetContentRegionMaxAbs() [Internal] // - GetContentRegionAvail(), // - GetWindowContentRegionMin(), GetWindowContentRegionMax() -// - GetWindowContentRegionWidth() // - BeginGroup() // - EndGroup() // Also see in imgui_widgets: tab bars, columns. @@ -7466,15 +7524,17 @@ void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. -bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGuiItemAddFlags flags) +bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGuiItemFlags extra_flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; // Set item data + // (DisplayRect is left untouched, made valid when ImGuiItemStatusFlags_HasDisplayRect is set) g.LastItemData.ID = id; g.LastItemData.Rect = bb; - g.LastItemData.InFlags = g.CurrentItemFlags; + g.LastItemData.NavRect = nav_bb_arg ? *nav_bb_arg : bb; + g.LastItemData.InFlags = g.CurrentItemFlags | extra_flags; g.LastItemData.StatusFlags = ImGuiItemStatusFlags_None; // Directional navigation processing @@ -7493,7 +7553,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) - NavProcessItem(window, nav_bb_arg ? *nav_bb_arg : bb, id); + NavProcessItem(); // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() #ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX @@ -7512,15 +7572,15 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu #endif // Clipping test - const bool is_clipped = IsClippedEx(bb, id, false); + const bool is_clipped = IsClippedEx(bb, id); if (is_clipped) return false; //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] - // Tab stop handling (previously was using internal ItemFocusable() api) - // FIXME-NAV: We would now want to move this above the clipping test, but this would require being able to scroll and currently this would mean an extra frame. (#4079, #343) - if (flags & ImGuiItemAddFlags_Focusable) - ItemFocusable(window, id); + // [WIP] Tab stop handling (previously was using internal FocusableItemRegister() api) + // FIXME-NAV: We would now want to move this before the clipping test, but this would require being able to scroll and currently this would mean an extra frame. (#4079, #343) + if (extra_flags & ImGuiItemFlags_Inputable) + ItemInputable(window, id); // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (IsMouseHoveringRect(bb.Min, bb.Max)) @@ -7784,14 +7844,9 @@ ImVec2 ImGui::GetWindowContentRegionMax() return window->ContentRegionRect.Max - window->Pos; } -float ImGui::GetWindowContentRegionWidth() -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ContentRegionRect.GetWidth(); -} - // Lock horizontal starting position + capture group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) // Groups are currently a mishmash of functionalities which should perhaps be clarified and separated. +// FIXME-OPT: Could we safely early out on ->SkipItems? void ImGui::BeginGroup() { ImGuiContext& g = *GImGui; @@ -7936,32 +7991,80 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window) return scroll; } +void ImGui::ScrollToItem(ImGuiScrollFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ScrollToRectEx(window, g.LastItemData.NavRect, flags); +} + +void ImGui::ScrollToRect(ImGuiWindow* window, const ImRect& item_rect, ImGuiScrollFlags flags) +{ + ScrollToRectEx(window, item_rect, flags); +} + // Scroll to keep newly navigated item fully into view -ImVec2 ImGui::ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect) +ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGuiScrollFlags flags) { ImGuiContext& g = *GImGui; ImRect window_rect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1)); //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG] - ImVec2 delta_scroll; - if (!window_rect.Contains(item_rect)) + // Check that only one behavior is selected per axis + IM_ASSERT((flags & ImGuiScrollFlags_MaskX_) == 0 || ImIsPowerOfTwo(flags & ImGuiScrollFlags_MaskX_)); + IM_ASSERT((flags & ImGuiScrollFlags_MaskY_) == 0 || ImIsPowerOfTwo(flags & ImGuiScrollFlags_MaskY_)); + + // Defaults + ImGuiScrollFlags in_flags = flags; + if ((flags & ImGuiScrollFlags_MaskX_) == 0 && window->ScrollbarX) + flags |= ImGuiScrollFlags_KeepVisibleEdgeX; + if ((flags & ImGuiScrollFlags_MaskY_) == 0) + flags |= window->Appearing ? ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeY; + + const bool fully_visible_x = item_rect.Min.x >= window_rect.Min.x && item_rect.Max.x <= window_rect.Max.x; + const bool fully_visible_y = item_rect.Min.y >= window_rect.Min.y && item_rect.Max.y <= window_rect.Max.y; + const bool can_be_fully_visible_x = item_rect.GetWidth() <= window_rect.GetWidth(); + const bool can_be_fully_visible_y = item_rect.GetHeight() <= window_rect.GetHeight(); + + if ((flags & ImGuiScrollFlags_KeepVisibleEdgeX) && !fully_visible_x) { - if (window->ScrollbarX && item_rect.Min.x < window_rect.Min.x) - SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x - g.Style.ItemSpacing.x, 0.0f); - else if (window->ScrollbarX && item_rect.Max.x >= window_rect.Max.x) - SetScrollFromPosX(window, item_rect.Max.x - window->Pos.x + g.Style.ItemSpacing.x, 1.0f); - if (item_rect.Min.y < window_rect.Min.y) - SetScrollFromPosY(window, item_rect.Min.y - window->Pos.y - g.Style.ItemSpacing.y, 0.0f); - else if (item_rect.Max.y >= window_rect.Max.y) - SetScrollFromPosY(window, item_rect.Max.y - window->Pos.y + g.Style.ItemSpacing.y, 1.0f); + if (item_rect.Min.x < window_rect.Min.x || !can_be_fully_visible_x) + SetScrollFromPosX(window, item_rect.Min.x - g.Style.ItemSpacing.x - window->Pos.x, 0.0f); + else if (item_rect.Max.x >= window_rect.Max.x) + SetScrollFromPosX(window, item_rect.Max.x + g.Style.ItemSpacing.x - window->Pos.x, 1.0f); + } + else if (((flags & ImGuiScrollFlags_KeepVisibleCenterX) && !fully_visible_x) || (flags & ImGuiScrollFlags_AlwaysCenterX)) + { + float target_x = can_be_fully_visible_x ? ImFloor((item_rect.Min.x + item_rect.Max.x - window->InnerRect.GetWidth()) * 0.5f) : item_rect.Min.x; + SetScrollFromPosX(window, target_x - window->Pos.x, 0.0f); + } - ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); - delta_scroll = next_scroll - window->Scroll; + if ((flags & ImGuiScrollFlags_KeepVisibleEdgeY) && !fully_visible_y) + { + if (item_rect.Min.y < window_rect.Min.y || !can_be_fully_visible_y) + SetScrollFromPosY(window, item_rect.Min.y - g.Style.ItemSpacing.y - window->Pos.y, 0.0f); + else if (item_rect.Max.y >= window_rect.Max.y) + SetScrollFromPosY(window, item_rect.Max.y + g.Style.ItemSpacing.y - window->Pos.y, 1.0f); + } + else if (((flags & ImGuiScrollFlags_KeepVisibleCenterY) && !fully_visible_y) || (flags & ImGuiScrollFlags_AlwaysCenterY)) + { + float target_y = can_be_fully_visible_y ? ImFloor((item_rect.Min.y + item_rect.Max.y - window->InnerRect.GetHeight()) * 0.5f) : item_rect.Min.y; + SetScrollFromPosY(window, target_y - window->Pos.y, 0.0f); } + ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); + ImVec2 delta_scroll = next_scroll - window->Scroll; + // Also scroll parent window to keep us into view if necessary - if (window->Flags & ImGuiWindowFlags_ChildWindow) - delta_scroll += ScrollToBringRectIntoView(window->ParentWindow, ImRect(item_rect.Min - delta_scroll, item_rect.Max - delta_scroll)); + if (!(flags & ImGuiScrollFlags_NoScrollParent) && (window->Flags & ImGuiWindowFlags_ChildWindow)) + { + // FIXME-SCROLL: May be an option? + if ((in_flags & (ImGuiScrollFlags_AlwaysCenterX | ImGuiScrollFlags_KeepVisibleCenterX)) != 0) + in_flags = (in_flags & ~ImGuiScrollFlags_MaskX_) | ImGuiScrollFlags_KeepVisibleEdgeX; + if ((in_flags & (ImGuiScrollFlags_AlwaysCenterY | ImGuiScrollFlags_KeepVisibleCenterY)) != 0) + in_flags = (in_flags & ~ImGuiScrollFlags_MaskY_) | ImGuiScrollFlags_KeepVisibleEdgeY; + delta_scroll += ScrollToRectEx(window->ParentWindow, ImRect(item_rect.Min - delta_scroll, item_rect.Max - delta_scroll), in_flags); + } return delta_scroll; } @@ -8307,6 +8410,21 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to } } +void ImGui::ClosePopupsExceptModals() +{ + ImGuiContext& g = *GImGui; + + int popup_count_to_keep; + for (popup_count_to_keep = g.OpenPopupStack.Size; popup_count_to_keep > 0; popup_count_to_keep--) + { + ImGuiWindow* window = g.OpenPopupStack[popup_count_to_keep - 1].Window; + if (!window || window->Flags & ImGuiWindowFlags_Modal) + break; + } + if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below + ClosePopupToLevel(popup_count_to_keep, true); +} + void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; @@ -8442,7 +8560,7 @@ void ImGui::EndPopup() IM_ASSERT(window->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginPopup()/EndPopup() calls IM_ASSERT(g.BeginPopupStack.Size > 0); - // Make all menus and popups wrap around for now, may need to expose that policy. + // Make all menus and popups wrap around for now, may need to expose that policy (e.g. focus scope could include wrap/loop policy flags used by new move requests) if (g.NavWindow == window) NavMoveRequestTryWrapping(window, ImGuiNavMoveFlags_LoopY); @@ -8662,6 +8780,7 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) //----------------------------------------------------------------------------- // FIXME-NAV: The existence of SetNavID vs SetFocusID properly needs to be clarified/reworked. +// In our terminology those should be interchangeable. Those two functions are merely a legacy artifact, so at minimum naming should be clarified. void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; @@ -8692,7 +8811,7 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; window->NavLastIds[nav_layer] = id; if (g.LastItemData.ID == id) - window->NavRectRel[nav_layer] = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); + window->NavRectRel[nav_layer] = ImRect(g.LastItemData.NavRect.Min - window->Pos, g.LastItemData.NavRect.Max - window->Pos); if (g.ActiveIdSource == ImGuiInputSource_Nav) g.NavDisableMouseHover = true; @@ -8723,7 +8842,7 @@ static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect r.Min.y = ImClamp(r.Min.y, clip_rect.Min.y, clip_rect.Max.y); r.Max.y = ImClamp(r.Max.y, clip_rect.Min.y, clip_rect.Max.y); } - else + else // FIXME: PageUp/PageDown are leaving move_dir == None { r.Min.x = ImClamp(r.Min.x, clip_rect.Min.x, clip_rect.Max.x); r.Max.x = ImClamp(r.Max.x, clip_rect.Min.x, clip_rect.Max.x); @@ -8731,15 +8850,17 @@ static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect } // Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057 -static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) +static bool ImGui::NavScoreItem(ImGuiNavItemData* result) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (g.NavLayer != window->DC.NavLayerCurrent) return false; - const ImRect& curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width) - g.NavScoringCount++; + // FIXME: Those are not good variables names + ImRect cand = g.LastItemData.NavRect; // Current item nav rectangle + const ImRect curr = g.NavScoringRect; // Current modified source rect (NB: we've applied Max.x = Min.x in NavUpdate() to inhibit the effect of having varied item width) + g.NavScoringDebugCount++; // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring if (window->ParentWindow == g.NavWindow) @@ -8801,24 +8922,24 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100)); draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200)); draw_list->AddRectFilled(cand.Max - ImVec2(4, 4), cand.Max + CalcTextSize(buf) + ImVec2(4, 4), IM_COL32(40,0,0,150)); - draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Max, ~0U, buf); + draw_list->AddText(cand.Max, ~0U, buf); } else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate. { - if (IsKeyPressedMap(ImGuiKey_C)) { g.NavMoveDirLast = (ImGuiDir)((g.NavMoveDirLast + 1) & 3); g.IO.KeysDownDuration[g.IO.KeyMap[ImGuiKey_C]] = 0.01f; } if (quadrant == g.NavMoveDir) { ImFormatString(buf, IM_ARRAYSIZE(buf), "%.0f/%.0f", dist_box, dist_center); ImDrawList* draw_list = GetForegroundDrawList(window); draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200)); - draw_list->AddText(g.IO.FontDefault, 13.0f, cand.Min, IM_COL32(255, 255, 255, 255), buf); + draw_list->AddText(cand.Min, IM_COL32(255, 255, 255, 255), buf); } } #endif // Is it in the quadrant we're interesting in moving to? bool new_best = false; - if (quadrant == g.NavMoveDir) + const ImGuiDir move_dir = g.NavMoveDir; + if (quadrant == move_dir) { // Does it beat the current best candidate? if (dist_box < result->DistBox) @@ -8840,7 +8961,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) // Still tied! we need to be extra-careful to make sure everything gets linked properly. We consistently break ties by symbolically moving "later" items // (with higher index) to the right/downwards by an infinitesimal amount since we the current "best" button already (so it must have a lower index), // this is fairly easy. This rule ensures that all buttons with dx==dy==0 will end up being linked in order of appearance along the x axis. - if (((g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance + if (((move_dir == ImGuiDir_Up || move_dir == ImGuiDir_Down) ? dby : dbx) < 0.0f) // moving bj to the right/down decreases distance new_best = true; } } @@ -8853,7 +8974,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) // Disabling it may lead to disconnected graphs when nodes are very spaced out on different axis. Perhaps consider offering this as an option? if (result->DistBox == FLT_MAX && dist_axial < result->DistAxial) // Check axial match if (g.NavLayer == ImGuiNavLayer_Menu && !(g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) - if ((g.NavMoveDir == ImGuiDir_Left && dax < 0.0f) || (g.NavMoveDir == ImGuiDir_Right && dax > 0.0f) || (g.NavMoveDir == ImGuiDir_Up && day < 0.0f) || (g.NavMoveDir == ImGuiDir_Down && day > 0.0f)) + if ((move_dir == ImGuiDir_Left && dax < 0.0f) || (move_dir == ImGuiDir_Right && dax > 0.0f) || (move_dir == ImGuiDir_Up && day < 0.0f) || (move_dir == ImGuiDir_Down && day > 0.0f)) { result->DistAxial = dist_axial; new_best = true; @@ -8862,23 +8983,26 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result, ImRect cand) return new_best; } -static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result, ImGuiWindow* window, ImGuiID id, const ImRect& nav_bb_rel) +static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) { + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; result->Window = window; - result->ID = id; + result->ID = g.LastItemData.ID; result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; - result->RectRel = nav_bb_rel; + result->InFlags = g.LastItemData.InFlags; + result->RectRel = ImRect(g.LastItemData.NavRect.Min - window->Pos, g.LastItemData.NavRect.Max - window->Pos); } // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) -static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, const ImGuiID id) +// This is called after LastItemData is set. +static void ImGui::NavProcessItem() { ImGuiContext& g = *GImGui; - //if (!g.IO.NavActive) // [2017/10/06] Removed this possibly redundant test but I am not sure of all the side-effects yet. Some of the feature here will need to work regardless of using a _NoNavInputs flag. - // return; - + ImGuiWindow* window = g.CurrentWindow; + const ImGuiID id = g.LastItemData.ID; + const ImRect nav_bb = g.LastItemData.NavRect; const ImGuiItemFlags item_flags = g.LastItemData.InFlags; - const ImRect nav_bb_rel(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); // Process Init Request if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent) @@ -8888,7 +9012,7 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con if (candidate_for_nav_default_focus || g.NavInitResultId == 0) { g.NavInitResultId = id; - g.NavInitResultRectRel = nav_bb_rel; + g.NavInitResultRectRel = ImRect(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); } if (candidate_for_nav_default_focus) { @@ -8898,27 +9022,34 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con } // Process Move Request (scoring for navigation) - // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRectScreen + scoring from a rect wrapped according to current wrapping policy) - if ((g.NavId != id || (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) + // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRect + scoring from a rect wrapped according to current wrapping policy) + if (g.NavMoveScoringItems) { - ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; -#if IMGUI_DEBUG_NAV_SCORING - // [DEBUG] Score all items in NavWindow at all times - if (!g.NavMoveRequest) - g.NavMoveDir = g.NavMoveDirLast; - bool new_best = NavScoreItem(result, nav_bb) && g.NavMoveRequest; -#else - bool new_best = g.NavMoveRequest && NavScoreItem(result, nav_bb); -#endif - if (new_best) - NavApplyItemToResult(result, window, id, nav_bb_rel); + if (item_flags & ImGuiItemFlags_Inputable) + g.NavTabbingInputableRemaining--; + + if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) + { + ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; - // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. - const float VISIBLE_RATIO = 0.70f; - if ((g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) - if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) - if (NavScoreItem(&g.NavMoveResultLocalVisibleSet, nav_bb)) - NavApplyItemToResult(&g.NavMoveResultLocalVisibleSet, window, id, nav_bb_rel); + if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + { + if (g.NavTabbingInputableRemaining == 0) + NavMoveRequestResolveWithLastItem(); + } + else + { + if (NavScoreItem(result)) + NavApplyItemToResult(result); + + // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. + const float VISIBLE_RATIO = 0.70f; + if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) + if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) + if (NavScoreItem(&g.NavMoveResultLocalVisible)) + NavApplyItemToResult(&g.NavMoveResultLocalVisible); + } + } } // Update window-relative bounding box of navigated item @@ -8928,43 +9059,77 @@ static void ImGui::NavProcessItem(ImGuiWindow* window, const ImRect& nav_bb, con g.NavLayer = window->DC.NavLayerCurrent; g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; g.NavIdIsAlive = true; - window->NavRectRel[window->DC.NavLayerCurrent] = nav_bb_rel; // Store item bounding box (relative to window position) + window->NavRectRel[window->DC.NavLayerCurrent] = ImRect(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); // Store item bounding box (relative to window position) } } bool ImGui::NavMoveRequestButNoResultYet() { ImGuiContext& g = *GImGui; - return g.NavMoveRequest && g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0; + return g.NavMoveScoringItems && g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0; +} + +// FIXME: ScoringRect is not set +void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.NavWindow != NULL); + + if (move_flags & ImGuiNavMoveFlags_Tabbing) + move_flags |= ImGuiNavMoveFlags_AllowCurrentNavId; + + g.NavMoveSubmitted = g.NavMoveScoringItems = true; + g.NavMoveDir = move_dir; + g.NavMoveDirForDebug = move_dir; + g.NavMoveClipDir = clip_dir; + g.NavMoveFlags = move_flags; + g.NavMoveScrollFlags = scroll_flags; + g.NavMoveForwardToNextFrame = false; + g.NavMoveKeyMods = g.IO.KeyMods; + g.NavTabbingInputableRemaining = 0; + g.NavMoveResultLocal.Clear(); + g.NavMoveResultLocalVisible.Clear(); + g.NavMoveResultOther.Clear(); + NavUpdateAnyRequestFlag(); +} + +void ImGui::NavMoveRequestResolveWithLastItem() +{ + ImGuiContext& g = *GImGui; + g.NavMoveScoringItems = false; // Ensure request doesn't need more processing + NavApplyItemToResult(&g.NavMoveResultLocal); + NavUpdateAnyRequestFlag(); } void ImGui::NavMoveRequestCancel() { ImGuiContext& g = *GImGui; - g.NavMoveRequest = false; + g.NavMoveSubmitted = g.NavMoveScoringItems = false; NavUpdateAnyRequestFlag(); } -void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags) +// Forward will reuse the move request again on the next frame (generally with modifications done to it) +void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags) { ImGuiContext& g = *GImGui; - IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_None); + IM_ASSERT(g.NavMoveForwardToNextFrame == false); NavMoveRequestCancel(); + g.NavMoveForwardToNextFrame = true; g.NavMoveDir = move_dir; g.NavMoveClipDir = clip_dir; - g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; - g.NavMoveRequestFlags = move_flags; - g.NavWindow->NavRectRel[g.NavLayer] = bb_rel; + g.NavMoveFlags = move_flags | ImGuiNavMoveFlags_Forwarded; + g.NavMoveScrollFlags = scroll_flags; } -void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags) +// Navigation wrap-around logic is delayed to the end of the frame because this operation is only valid after entire +// popup is assembled and in case of appended popups it is not clear which EndPopup() call is final. +void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags wrap_flags) { ImGuiContext& g = *GImGui; - - // Navigation wrap-around logic is delayed to the end of the frame because this operation is only valid after entire - // popup is assembled and in case of appended popups it is not clear which EndPopup() call is final. - g.NavWrapRequestWindow = window; - g.NavWrapRequestFlags = move_flags; + IM_ASSERT(wrap_flags != 0); // Call with _WrapX, _WrapY, _LoopX, _LoopY + // In theory we should test for NavMoveRequestButNoResultYet() but there's no point doing it, NavEndFrame() will do the same test + if (g.NavWindow == window && g.NavMoveScoringItems && g.NavLayer == ImGuiNavLayer_Main) + g.NavMoveFlags |= wrap_flags; } // FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0). @@ -8996,20 +9161,20 @@ void ImGui::NavRestoreLayer(ImGuiNavLayer layer) if (window->NavLastIds[layer] != 0) { SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); - g.NavDisableHighlight = false; - g.NavDisableMouseHover = g.NavMousePosDirty = true; } else { g.NavLayer = layer; NavInitWindow(window, true); } + g.NavDisableHighlight = false; + g.NavDisableMouseHover = g.NavMousePosDirty = true; } static inline void ImGui::NavUpdateAnyRequestFlag() { ImGuiContext& g = *GImGui; - g.NavAnyRequest = g.NavMoveRequest || g.NavInitRequest || (IMGUI_DEBUG_NAV_SCORING && g.NavWindow != NULL); + g.NavAnyRequest = g.NavMoveScoringItems || g.NavInitRequest || (IMGUI_DEBUG_NAV_SCORING && g.NavWindow != NULL); if (g.NavAnyRequest) IM_ASSERT(g.NavWindow != NULL); } @@ -9054,7 +9219,7 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() // Mouse (we need a fallback in case the mouse becomes invalid after being used) if (IsMousePosValid(&g.IO.MousePos)) return g.IO.MousePos; - return g.LastValidMousePos; + return g.MouseLastValidPos; } else { @@ -9110,16 +9275,12 @@ static void ImGui::NavUpdate() ImGuiIO& io = g.IO; io.WantSetMousePos = false; - g.NavWrapRequestWindow = NULL; - g.NavWrapRequestFlags = ImGuiNavMoveFlags_None; -#if 0 - if (g.NavScoringCount > 0) IMGUI_DEBUG_LOG("NavScoringCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.FrameCount, g.NavScoringCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); -#endif + //if (g.NavScoringDebugCount > 0) IMGUI_DEBUG_LOG("NavScoringDebugCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.NavScoringDebugCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); // Set input source as Gamepad when buttons are pressed (as some features differs when used with Gamepad vs Keyboard) // (do it before we map Keyboard input!) - bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; if (nav_gamepad_active && g.NavInputSource != ImGuiInputSource_Gamepad) { if (io.NavInputs[ImGuiNavInput_Activate] > 0.0f || io.NavInputs[ImGuiNavInput_Input] > 0.0f || io.NavInputs[ImGuiNavInput_Cancel] > 0.0f || io.NavInputs[ImGuiNavInput_Menu] > 0.0f @@ -9150,24 +9311,17 @@ static void ImGui::NavUpdate() // Process navigation init request (select first/default focus) if (g.NavInitResultId != 0) - NavUpdateInitResult(); + NavInitRequestApplyResult(); g.NavInitRequest = false; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; g.NavJustMovedToId = 0; // Process navigation move request - if (g.NavMoveRequest) - NavUpdateMoveResult(); - - // When a forwarded move request failed, we restore the highlight that we disabled during the forward frame - if (g.NavMoveRequestForward == ImGuiNavForward_ForwardActive) - { - IM_ASSERT(g.NavMoveRequest); - if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) - g.NavDisableHighlight = false; - g.NavMoveRequestForward = ImGuiNavForward_None; - } + if (g.NavMoveSubmitted) + NavMoveRequestApplyResult(); + g.NavTabbingInputableRemaining = 0; + g.NavMoveSubmitted = g.NavMoveScoringItems = false; // Apply application mouse position movement, after we had a chance to process move request result. if (g.NavMousePosDirty && g.NavIdIsAlive) @@ -9178,14 +9332,15 @@ static void ImGui::NavUpdate() { io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); io.WantSetMousePos = true; + //IMGUI_DEBUG_LOG("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); } g.NavMousePosDirty = false; } g.NavIdIsAlive = false; g.NavJustTabbedId = 0; - IM_ASSERT(g.NavLayer == 0 || g.NavLayer == 1); + IM_ASSERT(g.NavLayer == ImGuiNavLayer_Main || g.NavLayer == ImGuiNavLayer_Menu); - // Store our return window (for returning from Layer 1 to Layer 0) and clear it as soon as we step back in our own Layer 0 + // Store our return window (for returning from Menu Layer to Main Layer) and clear it as soon as we step back in our own Layer 0 if (g.NavWindow) NavSaveLastChildNavWindowIntoParent(g.NavWindow); if (g.NavWindow && g.NavWindow->NavLastChildNavWindow != NULL && g.NavLayer == ImGuiNavLayer_Main) @@ -9199,130 +9354,66 @@ static void ImGui::NavUpdate() io.NavVisible = (io.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); // Process NavCancel input (to close a popup, get back to parent, clear focus) - if (IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) - { - IMGUI_DEBUG_LOG_NAV("[nav] ImGuiNavInput_Cancel\n"); - if (g.ActiveId != 0) - { - if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) - ClearActiveID(); - } - else if (g.NavLayer != ImGuiNavLayer_Main) - { - // Leave the "menu" layer - NavRestoreLayer(ImGuiNavLayer_Main); - } - else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) - { - // Exit child window - ImGuiWindow* child_window = g.NavWindow; - ImGuiWindow* parent_window = g.NavWindow->ParentWindow; - IM_ASSERT(child_window->ChildId != 0); - ImRect child_rect = child_window->Rect(); - FocusWindow(parent_window); - SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, ImRect(child_rect.Min - parent_window->Pos, child_rect.Max - parent_window->Pos)); - } - else if (g.OpenPopupStack.Size > 0) - { - // Close open popup/menu - if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) - ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); - } - else - { - // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were - if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) - g.NavWindow->NavLastIds[0] = 0; - g.NavId = g.NavFocusScopeId = 0; - } - } + NavUpdateCancelRequest(); // Process manual activation request - g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = 0; + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavActivateInputId = 0; + g.NavActivateFlags = ImGuiActivateFlags_None; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); + bool input_down = IsNavInputDown(ImGuiNavInput_Input); bool activate_pressed = activate_down && IsNavInputTest(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); + bool input_pressed = input_down && IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed); if (g.ActiveId == 0 && activate_pressed) + { g.NavActivateId = g.NavId; + g.NavActivateFlags = ImGuiActivateFlags_PreferTweak; + } + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && input_pressed) + { + g.NavActivateInputId = g.NavId; + g.NavActivateFlags = ImGuiActivateFlags_PreferInput; + } if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) g.NavActivateDownId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) g.NavActivatePressedId = g.NavId; - if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed)) - g.NavInputId = g.NavId; } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) g.NavDisableHighlight = true; if (g.NavActivateId != 0) IM_ASSERT(g.NavActivateDownId == g.NavActivateId); - g.NavMoveRequest = false; // Process programmatic activation request + // FIXME-NAV: Those should eventually be queued (unlike focus they don't cancel each others) if (g.NavNextActivateId != 0) - g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavInputId = g.NavNextActivateId; + { + if (g.NavNextActivateFlags & ImGuiActivateFlags_PreferInput) + g.NavActivateInputId = g.NavNextActivateId; + else + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavNextActivateId; + g.NavActivateFlags = g.NavNextActivateFlags; + } g.NavNextActivateId = 0; - // Initiate directional inputs request - if (g.NavMoveRequestForward == ImGuiNavForward_None) - { - g.NavMoveDir = ImGuiDir_None; - g.NavMoveRequestFlags = ImGuiNavMoveFlags_None; - if (g.NavWindow && !g.NavWindowingTarget && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) - { - const ImGuiInputReadMode read_mode = ImGuiInputReadMode_Repeat; - if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && (IsNavInputTest(ImGuiNavInput_DpadLeft, read_mode) || IsNavInputTest(ImGuiNavInput_KeyLeft_, read_mode))) { g.NavMoveDir = ImGuiDir_Left; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && (IsNavInputTest(ImGuiNavInput_DpadRight, read_mode) || IsNavInputTest(ImGuiNavInput_KeyRight_, read_mode))) { g.NavMoveDir = ImGuiDir_Right; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && (IsNavInputTest(ImGuiNavInput_DpadUp, read_mode) || IsNavInputTest(ImGuiNavInput_KeyUp_, read_mode))) { g.NavMoveDir = ImGuiDir_Up; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && (IsNavInputTest(ImGuiNavInput_DpadDown, read_mode) || IsNavInputTest(ImGuiNavInput_KeyDown_, read_mode))) { g.NavMoveDir = ImGuiDir_Down; } - } - g.NavMoveClipDir = g.NavMoveDir; - } - else - { - // Forwarding previous request (which has been modified, e.g. wrap around menus rewrite the requests with a starting rectangle at the other side of the window) - // (Preserve g.NavMoveRequestFlags, g.NavMoveClipDir which were set by the NavMoveRequestForward() function) - IM_ASSERT(g.NavMoveDir != ImGuiDir_None && g.NavMoveClipDir != ImGuiDir_None); - IM_ASSERT(g.NavMoveRequestForward == ImGuiNavForward_ForwardQueued); - IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequestForward %d\n", g.NavMoveDir); - g.NavMoveRequestForward = ImGuiNavForward_ForwardActive; - } - - // Update PageUp/PageDown/Home/End scroll - // FIXME-NAV: Consider enabling those keys even without the master ImGuiConfigFlags_NavEnableKeyboard flag? - float nav_scoring_rect_offset_y = 0.0f; - if (nav_keyboard_active) - nav_scoring_rect_offset_y = NavUpdatePageUpPageDown(); - - // If we initiate a movement request and have no current NavId, we initiate a InitDefautRequest that will be used as a fallback if the direction fails to find a match - if (g.NavMoveDir != ImGuiDir_None) - { - g.NavMoveRequest = true; - g.NavMoveRequestKeyMods = io.KeyMods; - g.NavMoveDirLast = g.NavMoveDir; - } - if (g.NavMoveRequest && g.NavId == 0) - { - IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); - g.NavInitRequest = g.NavInitRequestFromMove = true; - // Reassigning with same value, we're being explicit here. - g.NavInitResultId = 0; // -V1048 - g.NavDisableHighlight = false; - } - NavUpdateAnyRequestFlag(); - - // Scrolling - if (g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget) + // Process move requests + NavUpdateCreateMoveRequest(); + NavUpdateAnyRequestFlag(); + + // Scrolling + if (g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget) { // *Fallback* manual-scroll with Nav directional keys when window has no navigable item ImGuiWindow* window = g.NavWindow; const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * io.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. - if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll && g.NavMoveRequest) + const ImGuiDir move_dir = g.NavMoveDir; + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll && move_dir != ImGuiDir_None) { - if (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) - SetScrollX(window, ImFloor(window->Scroll.x + ((g.NavMoveDir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); - if (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) - SetScrollY(window, ImFloor(window->Scroll.y + ((g.NavMoveDir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); + if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) + SetScrollX(window, ImFloor(window->Scroll.x + ((move_dir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); + if (move_dir == ImGuiDir_Up || move_dir == ImGuiDir_Down) + SetScrollY(window, ImFloor(window->Scroll.y + ((move_dir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); } // *Normal* Manual scroll with NavScrollXXX keys @@ -9334,37 +9425,15 @@ static void ImGui::NavUpdate() SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed)); } - // Reset search results - g.NavMoveResultLocal.Clear(); - g.NavMoveResultLocalVisibleSet.Clear(); - g.NavMoveResultOther.Clear(); - - // When using gamepad, we project the reference nav bounding box into window visible area. - // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad every movements are relative - // (can't focus a visible object like we can with the mouse). - if (g.NavMoveRequest && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main) + // Always prioritize mouse highlight if navigation is disabled + if (!nav_keyboard_active && !nav_gamepad_active) { - ImGuiWindow* window = g.NavWindow; - ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1, 1), window->InnerRect.Max - window->Pos + ImVec2(1, 1)); - if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) - { - IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel\n"); - float pad = window->CalcFontSize() * 0.5f; - window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item - window->NavRectRel[g.NavLayer].ClipWithFull(window_rect_rel); - g.NavId = g.NavFocusScopeId = 0; - } + g.NavDisableHighlight = true; + g.NavDisableMouseHover = g.NavMousePosDirty = false; } - // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) - ImRect nav_rect_rel = g.NavWindow && !g.NavWindow->NavRectRel[g.NavLayer].IsInverted() ? g.NavWindow->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); - g.NavScoringRect = g.NavWindow ? ImRect(g.NavWindow->Pos + nav_rect_rel.Min, g.NavWindow->Pos + nav_rect_rel.Max) : ImRect(0, 0, 0, 0); - g.NavScoringRect.TranslateY(nav_scoring_rect_offset_y); - g.NavScoringRect.Min.x = ImMin(g.NavScoringRect.Min.x + 1.0f, g.NavScoringRect.Max.x); - g.NavScoringRect.Max.x = g.NavScoringRect.Min.x; - IM_ASSERT(!g.NavScoringRect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). - //GetForegroundDrawList()->AddRect(g.NavScoringRectScreen.Min, g.NavScoringRectScreen.Max, IM_COL32(255,200,0,255)); // [DEBUG] - g.NavScoringCount = 0; + // [DEBUG] + g.NavScoringDebugCount = 0; #if IMGUI_DEBUG_NAV_RECTS if (g.NavWindow) { @@ -9375,7 +9444,7 @@ static void ImGui::NavUpdate() #endif } -static void ImGui::NavUpdateInitResult() +void ImGui::NavInitRequestApplyResult() { // In very rare cases g.NavWindow may be null (e.g. clearing focus after requesting an init request, which does happen when releasing Alt while clicking on void) ImGuiContext& g = *GImGui; @@ -9386,6 +9455,7 @@ static void ImGui::NavUpdateInitResult() // FIXME-NAV: On _NavFlattened windows, g.NavWindow will only be updated during subsequent frame. Not a problem currently. IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); + g.NavIdIsAlive = true; // Mark as alive from previous frame as we got a result if (g.NavInitRequestFromMove) { g.NavDisableHighlight = false; @@ -9393,14 +9463,118 @@ static void ImGui::NavUpdateInitResult() } } -// Apply result from previous frame navigation directional move request -static void ImGui::NavUpdateMoveResult() +void ImGui::NavUpdateCreateMoveRequest() +{ + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + ImGuiWindow* window = g.NavWindow; + + if (g.NavMoveForwardToNextFrame && window != NULL) + { + // Forwarding previous request (which has been modified, e.g. wrap around menus rewrite the requests with a starting rectangle at the other side of the window) + // (preserve most state, which were already set by the NavMoveRequestForward() function) + IM_ASSERT(g.NavMoveDir != ImGuiDir_None && g.NavMoveClipDir != ImGuiDir_None); + IM_ASSERT(g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded); + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequestForward %d\n", g.NavMoveDir); + } + else + { + // Initiate directional inputs request + g.NavMoveDir = ImGuiDir_None; + g.NavMoveFlags = ImGuiNavMoveFlags_None; + g.NavMoveScrollFlags = ImGuiScrollFlags_None; + if (window && !g.NavWindowingTarget && !(window->Flags & ImGuiWindowFlags_NoNavInputs)) + { + const ImGuiInputReadMode read_mode = ImGuiInputReadMode_Repeat; + if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && (IsNavInputTest(ImGuiNavInput_DpadLeft, read_mode) || IsNavInputTest(ImGuiNavInput_KeyLeft_, read_mode))) { g.NavMoveDir = ImGuiDir_Left; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && (IsNavInputTest(ImGuiNavInput_DpadRight, read_mode) || IsNavInputTest(ImGuiNavInput_KeyRight_, read_mode))) { g.NavMoveDir = ImGuiDir_Right; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && (IsNavInputTest(ImGuiNavInput_DpadUp, read_mode) || IsNavInputTest(ImGuiNavInput_KeyUp_, read_mode))) { g.NavMoveDir = ImGuiDir_Up; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && (IsNavInputTest(ImGuiNavInput_DpadDown, read_mode) || IsNavInputTest(ImGuiNavInput_KeyDown_, read_mode))) { g.NavMoveDir = ImGuiDir_Down; } + } + g.NavMoveClipDir = g.NavMoveDir; + } + + // Update PageUp/PageDown/Home/End scroll + // FIXME-NAV: Consider enabling those keys even without the master ImGuiConfigFlags_NavEnableKeyboard flag? + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + float scoring_rect_offset_y = 0.0f; + if (window && g.NavMoveDir == ImGuiDir_None && nav_keyboard_active) + scoring_rect_offset_y = NavUpdatePageUpPageDown(); + + // [DEBUG] Always send a request +#if IMGUI_DEBUG_NAV_SCORING + if (io.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) + g.NavMoveDirForDebug = (ImGuiDir)((g.NavMoveDirForDebug + 1) & 3); + if (io.KeyCtrl && g.NavMoveDir == ImGuiDir_None) + { + g.NavMoveDir = g.NavMoveDirForDebug; + g.NavMoveFlags |= ImGuiNavMoveFlags_DebugNoResult; + } +#endif + + // Submit + g.NavMoveForwardToNextFrame = false; + if (g.NavMoveDir != ImGuiDir_None) + NavMoveRequestSubmit(g.NavMoveDir, g.NavMoveClipDir, g.NavMoveFlags, g.NavMoveScrollFlags); + + // Moving with no reference triggers a init request (will be used as a fallback if the direction fails to find a match) + if (g.NavMoveSubmitted && g.NavId == 0) + { + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); + g.NavInitRequest = g.NavInitRequestFromMove = true; + g.NavInitResultId = 0; + g.NavDisableHighlight = false; + } + + // When using gamepad, we project the reference nav bounding box into window visible area. + // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad every movements are relative + // (can't focus a visible object like we can with the mouse). + if (g.NavMoveSubmitted && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main && window != NULL) + { + ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1, 1), window->InnerRect.Max - window->Pos + ImVec2(1, 1)); + if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) + { + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel\n"); + float pad = window->CalcFontSize() * 0.5f; + window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item + window->NavRectRel[g.NavLayer].ClipWithFull(window_rect_rel); + g.NavId = g.NavFocusScopeId = 0; + } + } + + // For scoring we use a single segment on the left side our current item bounding box (not touching the edge to avoid box overlap with zero-spaced items) + ImRect scoring_rect; + if (window != NULL) + { + ImRect nav_rect_rel = !window->NavRectRel[g.NavLayer].IsInverted() ? window->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); + scoring_rect = ImRect(window->Pos + nav_rect_rel.Min, window->Pos + nav_rect_rel.Max); + scoring_rect.TranslateY(scoring_rect_offset_y); + scoring_rect.Min.x = ImMin(scoring_rect.Min.x + 1.0f, scoring_rect.Max.x); + scoring_rect.Max.x = scoring_rect.Min.x; + IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). + //GetForegroundDrawList()->AddRect(scoring_rect.Min, scoring_rect.Max, IM_COL32(255,200,0,255)); // [DEBUG] + } + g.NavScoringRect = scoring_rect; +} + +// Apply result from previous frame navigation directional move request. Always called from NavUpdate() +void ImGui::NavMoveRequestApplyResult() { ImGuiContext& g = *GImGui; - if (g.NavMoveResultLocal.ID == 0 && g.NavMoveResultOther.ID == 0) +#if IMGUI_DEBUG_NAV_SCORING + if (g.NavMoveFlags & ImGuiNavMoveFlags_DebugNoResult) // [DEBUG] Scoring all items in NavWindow at all times + return; +#endif + + // Select which result to use + ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : (g.NavMoveResultOther.ID != 0) ? &g.NavMoveResultOther : NULL; + + // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) + if (result == NULL) { - // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) - if (g.NavId != 0) + if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + g.NavMoveFlags |= ImGuiNavMoveFlags_DontSetNavHighlight; + if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) { g.NavDisableHighlight = false; g.NavDisableMouseHover = true; @@ -9408,13 +9582,10 @@ static void ImGui::NavUpdateMoveResult() return; } - // Select which result to use - ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; - // PageUp/PageDown behavior first jumps to the bottom/top mostly visible item, _otherwise_ use the result from the previous/next page. - if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) - if (g.NavMoveResultLocalVisibleSet.ID != 0 && g.NavMoveResultLocalVisibleSet.ID != g.NavId) - result = &g.NavMoveResultLocalVisibleSet; + if (g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) + if (g.NavMoveResultLocalVisible.ID != 0 && g.NavMoveResultLocalVisible.ID != g.NavId) + result = &g.NavMoveResultLocalVisible; // Maybe entering a flattened child from the outside? In this case solve the tie using the regular scoring rules. if (result != &g.NavMoveResultOther && g.NavMoveResultOther.ID != 0 && g.NavMoveResultOther.Window->ParentWindow == g.NavWindow) @@ -9426,8 +9597,9 @@ static void ImGui::NavUpdateMoveResult() if (g.NavLayer == ImGuiNavLayer_Main) { ImVec2 delta_scroll; - if (g.NavMoveRequestFlags & ImGuiNavMoveFlags_ScrollToEdge) + if (g.NavMoveFlags & ImGuiNavMoveFlags_ScrollToEdgeY) { + // FIXME: Should remove this float scroll_target = (g.NavMoveDir == ImGuiDir_Up) ? result->Window->ScrollMax.y : 0.0f; delta_scroll.y = result->Window->Scroll.y - scroll_target; SetScrollY(result->Window, scroll_target); @@ -9435,7 +9607,7 @@ static void ImGui::NavUpdateMoveResult() else { ImRect rect_abs = ImRect(result->RectRel.Min + result->Window->Pos, result->RectRel.Max + result->Window->Pos); - delta_scroll = ScrollToBringRectIntoView(result->Window, rect_abs); + delta_scroll = ScrollToRectEx(result->Window, rect_abs, g.NavMoveScrollFlags); } // Offset our result position so mouse position can be applied immediately after in NavUpdate() @@ -9450,84 +9622,155 @@ static void ImGui::NavUpdateMoveResult() // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; g.NavJustMovedToFocusScopeId = result->FocusScopeId; - g.NavJustMovedToKeyMods = g.NavMoveRequestKeyMods; + g.NavJustMovedToKeyMods = g.NavMoveKeyMods; } + + // Focus IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); - g.NavDisableHighlight = false; - g.NavDisableMouseHover = g.NavMousePosDirty = true; + + // Tabbing: Activates Inputable or Focus non-Inputable + if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && (result->InFlags & ImGuiItemFlags_Inputable)) + { + g.NavNextActivateId = result->ID; + g.NavNextActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState; + g.NavMoveFlags |= ImGuiNavMoveFlags_DontSetNavHighlight; + } + + // Activate + if (g.NavMoveFlags & ImGuiNavMoveFlags_Activate) + { + g.NavNextActivateId = result->ID; + g.NavNextActivateFlags = ImGuiActivateFlags_None; + } + + // Enable nav highlight + if ((g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) + { + g.NavDisableHighlight = false; + g.NavDisableMouseHover = g.NavMousePosDirty = true; + } +} + +// Process NavCancel input (to close a popup, get back to parent, clear focus) +// FIXME: In order to support e.g. Escape to clear a selection we'll need: +// - either to store the equivalent of ActiveIdUsingKeyInputMask for a FocusScope and test for it. +// - either to move most/all of those tests to the epilogue/end functions of the scope they are dealing with (e.g. exit child window in EndChild()) or in EndFrame(), to allow an earlier intercept +static void ImGui::NavUpdateCancelRequest() +{ + ImGuiContext& g = *GImGui; + if (!IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) + return; + + IMGUI_DEBUG_LOG_NAV("[nav] ImGuiNavInput_Cancel\n"); + if (g.ActiveId != 0) + { + if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) + ClearActiveID(); + } + else if (g.NavLayer != ImGuiNavLayer_Main) + { + // Leave the "menu" layer + NavRestoreLayer(ImGuiNavLayer_Main); + } + else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) + { + // Exit child window + ImGuiWindow* child_window = g.NavWindow; + ImGuiWindow* parent_window = g.NavWindow->ParentWindow; + IM_ASSERT(child_window->ChildId != 0); + ImRect child_rect = child_window->Rect(); + FocusWindow(parent_window); + SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, ImRect(child_rect.Min - parent_window->Pos, child_rect.Max - parent_window->Pos)); + } + else if (g.OpenPopupStack.Size > 0) + { + // Close open popup/menu + if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) + ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); + } + else + { + // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were + if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) + g.NavWindow->NavLastIds[0] = 0; + g.NavId = g.NavFocusScopeId = 0; + } } // Handle PageUp/PageDown/Home/End keys +// Called from NavUpdateCreateMoveRequest() which will use our output to create a move request +// FIXME-NAV: This doesn't work properly with NavFlattened siblings as we use NavWindow rectangle for reference +// FIXME-NAV: how to get Home/End to aim at the beginning/end of a 2D grid? static float ImGui::NavUpdatePageUpPageDown() { ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; - if (g.NavMoveDir != ImGuiDir_None || g.NavWindow == NULL) - return 0.0f; - if ((g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main) + ImGuiWindow* window = g.NavWindow; + if ((window->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main) return 0.0f; - ImGuiWindow* window = g.NavWindow; const bool page_up_held = IsKeyDown(io.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); const bool page_down_held = IsKeyDown(io.KeyMap[ImGuiKey_PageDown]) && !IsActiveIdUsingKey(ImGuiKey_PageDown); const bool home_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_Home]) && !IsActiveIdUsingKey(ImGuiKey_Home); const bool end_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); - if (page_up_held != page_down_held || home_pressed != end_pressed) // If either (not both) are pressed + if (page_up_held == page_down_held && home_pressed == end_pressed) // Proceed if either (not both) are pressed, otherwise early out + return 0.0f; + + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) + { + // Fallback manual-scroll when window has no navigable item + if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) + SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); + else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) + SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); + else if (home_pressed) + SetScrollY(window, 0.0f); + else if (end_pressed) + SetScrollY(window, window->ScrollMax.y); + } + else { - if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) + ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; + const float page_offset_y = ImMax(0.0f, window->InnerRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); + float nav_scoring_rect_offset_y = 0.0f; + if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) { - // Fallback manual-scroll when window has no navigable item - if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) - SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); - else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) - SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); - else if (home_pressed) - SetScrollY(window, 0.0f); - else if (end_pressed) - SetScrollY(window, window->ScrollMax.y); + nav_scoring_rect_offset_y = -page_offset_y; + g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) + g.NavMoveClipDir = ImGuiDir_Up; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; } - else + else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) { - ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; - const float page_offset_y = ImMax(0.0f, window->InnerRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); - float nav_scoring_rect_offset_y = 0.0f; - if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) - { - nav_scoring_rect_offset_y = -page_offset_y; - g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) - g.NavMoveClipDir = ImGuiDir_Up; - g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; - } - else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) - { - nav_scoring_rect_offset_y = +page_offset_y; - g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) - g.NavMoveClipDir = ImGuiDir_Down; - g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; - } - else if (home_pressed) - { - // FIXME-NAV: handling of Home/End is assuming that the top/bottom most item will be visible with Scroll.y == 0/ScrollMax.y - // Scrolling will be handled via the ImGuiNavMoveFlags_ScrollToEdge flag, we don't scroll immediately to avoid scrolling happening before nav result. - // Preserve current horizontal position if we have any. - nav_rect_rel.Min.y = nav_rect_rel.Max.y = -window->Scroll.y; - if (nav_rect_rel.IsInverted()) - nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; - g.NavMoveDir = ImGuiDir_Down; - g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdge; - } - else if (end_pressed) - { - nav_rect_rel.Min.y = nav_rect_rel.Max.y = window->ScrollMax.y + window->SizeFull.y - window->Scroll.y; - if (nav_rect_rel.IsInverted()) - nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; - g.NavMoveDir = ImGuiDir_Up; - g.NavMoveRequestFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdge; - } - return nav_scoring_rect_offset_y; + nav_scoring_rect_offset_y = +page_offset_y; + g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) + g.NavMoveClipDir = ImGuiDir_Down; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; + } + else if (home_pressed) + { + // FIXME-NAV: handling of Home/End is assuming that the top/bottom most item will be visible with Scroll.y == 0/ScrollMax.y + // Scrolling will be handled via the ImGuiNavMoveFlags_ScrollToEdgeY flag, we don't scroll immediately to avoid scrolling happening before nav result. + // Preserve current horizontal position if we have any. + nav_rect_rel.Min.y = nav_rect_rel.Max.y = -window->Scroll.y; + if (nav_rect_rel.IsInverted()) + nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; + g.NavMoveDir = ImGuiDir_Down; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdgeY; + // FIXME-NAV: MoveClipDir left to _None, intentional? + } + else if (end_pressed) + { + nav_rect_rel.Min.y = nav_rect_rel.Max.y = window->ScrollMax.y + window->SizeFull.y - window->Scroll.y; + if (nav_rect_rel.IsInverted()) + nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; + g.NavMoveDir = ImGuiDir_Up; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_ScrollToEdgeY; + // FIXME-NAV: MoveClipDir left to _None, intentional? } + return nav_scoring_rect_offset_y; } return 0.0f; } @@ -9541,13 +9784,14 @@ static void ImGui::NavEndFrame() NavUpdateWindowingOverlay(); // Perform wrap-around in menus - ImGuiWindow* window = g.NavWrapRequestWindow; - ImGuiNavMoveFlags move_flags = g.NavWrapRequestFlags; - if (window != NULL && g.NavWindow == window && NavMoveRequestButNoResultYet() && g.NavMoveRequestForward == ImGuiNavForward_None && g.NavLayer == ImGuiNavLayer_Main) + // FIXME-NAV: Wrap (not Loop) support could be handled by the scoring function and then WrapX would function without an extra frame. + ImGuiWindow* window = g.NavWindow; + const ImGuiNavMoveFlags move_flags = g.NavMoveFlags; + const ImGuiNavMoveFlags wanted_flags = ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY; + if (window && NavMoveRequestButNoResultYet() && (g.NavMoveFlags & wanted_flags) && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) { - IM_ASSERT(move_flags != 0); // No points calling this with no wrapping - ImRect bb_rel = window->NavRectRel[0]; - + bool do_forward = false; + ImRect bb_rel = window->NavRectRel[g.NavLayer]; ImGuiDir clip_dir = g.NavMoveDir; if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { @@ -9558,7 +9802,7 @@ static void ImGui::NavEndFrame() bb_rel.TranslateY(-bb_rel.GetHeight()); clip_dir = ImGuiDir_Up; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + do_forward = true; } if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { @@ -9568,28 +9812,34 @@ static void ImGui::NavEndFrame() bb_rel.TranslateY(+bb_rel.GetHeight()); clip_dir = ImGuiDir_Down; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + do_forward = true; } + const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) { bb_rel.Min.y = bb_rel.Max.y = - ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y; + ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y + decoration_up_height; if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(-bb_rel.GetWidth()); clip_dir = ImGuiDir_Left; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + do_forward = true; } if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) { - bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y; + bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y + decoration_up_height; if (move_flags & ImGuiNavMoveFlags_WrapY) { bb_rel.TranslateX(+bb_rel.GetWidth()); clip_dir = ImGuiDir_Right; } - NavMoveRequestForward(g.NavMoveDir, clip_dir, bb_rel, move_flags); + do_forward = true; + } + if (do_forward) + { + window->NavRectRel[g.NavLayer] = bb_rel; + NavMoveRequestForward(g.NavMoveDir, clip_dir, move_flags, g.NavMoveScrollFlags); } } } @@ -9599,6 +9849,7 @@ static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) ImGuiContext& g = *GImGui; IM_UNUSED(g); int order = window->FocusOrder; + IM_ASSERT(window->RootWindow == window); // No child window (not testing _ChildWindow because of docking) IM_ASSERT(g.WindowsFocusOrder[order] == window); return order; } @@ -9653,8 +9904,9 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL-TAB or Square+L/R window selection - bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); - bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab) && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard); + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + const bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); + const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && nav_keyboard_active && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { @@ -9705,7 +9957,7 @@ static void ImGui::NavUpdateWindowing() // Keyboard: Press and Release ALT to toggle menu layer // - Testing that only Alt is tested prevents Alt+Shift or AltGR from toggling menu layer. // - AltGR is normally Alt+Ctrl but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). But even on keyboards without AltGR we don't want Alt+Ctrl to open menu anyway. - if (io.KeyMods == ImGuiKeyModFlags_Alt && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) == 0) + if (nav_keyboard_active && io.KeyMods == ImGuiKeyModFlags_Alt && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) == 0) { g.NavWindowingToggleLayer = true; g.NavInputSource = ImGuiInputSource_Keyboard; @@ -9790,14 +10042,16 @@ static void ImGui::NavUpdateWindowing() FocusWindow(new_nav_window); new_nav_window->NavLastChildNavWindow = old_nav_window; } - g.NavDisableHighlight = false; - g.NavDisableMouseHover = true; - // Reinitialize navigation when entering menu bar with the Alt key. + // Toggle layer const ImGuiNavLayer new_nav_layer = (g.NavWindow->DC.NavLayersActiveMask & (1 << ImGuiNavLayer_Menu)) ? (ImGuiNavLayer)((int)g.NavLayer ^ 1) : ImGuiNavLayer_Main; - if (new_nav_layer == ImGuiNavLayer_Menu) - g.NavWindow->NavLastIds[new_nav_layer] = 0; - NavRestoreLayer(new_nav_layer); + if (new_nav_layer != g.NavLayer) + { + // Reinitialize navigation when entering menu bar with the Alt key (FIXME: could be a properly of the layer?) + if (new_nav_layer == ImGuiNavLayer_Menu) + g.NavWindow->NavLastIds[new_nav_layer] = 0; + NavRestoreLayer(new_nav_layer); + } } } @@ -9890,14 +10144,16 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) return false; if (g.ActiveIdMouseButton != -1) mouse_button = g.ActiveIdMouseButton; - if (g.IO.MouseDown[mouse_button] == false) + if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) return false; g.ActiveIdAllowOverlap = false; } else { // Uncommon path: items without ID - if (g.IO.MouseDown[mouse_button] == false) + if (g.IO.MouseDown[mouse_button] == false || window->SkipItems) + return false; + if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) return false; // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: @@ -9908,10 +10164,6 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) return false; } - // Early out - if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) == 0 && (g.ActiveId == 0 || g.ActiveIdWindow != window)) - return false; - // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() // We build a throwaway ID based on current ID stack + relative AABB of items in window. // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. @@ -10077,7 +10329,7 @@ bool ImGui::BeginDragDropTarget() if (!(g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect)) return false; ImGuiWindow* hovered_window = g.HoveredWindowUnderMovingWindow; - if (hovered_window == NULL || window->RootWindow != hovered_window->RootWindow) + if (hovered_window == NULL || window->RootWindow != hovered_window->RootWindow || window->SkipItems) return false; const ImRect& display_rect = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? g.LastItemData.DisplayRect : g.LastItemData.Rect; @@ -11006,22 +11258,23 @@ namespace ImGui { void ShowFontAtlas(ImFontAtlas* atlas); } void ImGui::ShowMetricsWindow(bool* p_open) { - if (!Begin("Dear ImGui Metrics/Debugger", p_open)) + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + if (cfg->ShowStackTool) + ShowStackToolWindow(&cfg->ShowStackTool); + + if (!Begin("Dear ImGui Metrics/Debugger", p_open) || GetCurrentWindow()->BeginCount > 1) { End(); return; } - ImGuiContext& g = *GImGui; - ImGuiIO& io = g.IO; - ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; - // Basic info Text("Dear ImGui %s", GetVersion()); Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); - Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); - Text("%d active allocations", io.MetricsActiveAllocations); + Text("%d visible windows, %d active allocations", io.MetricsRenderWindows, io.MetricsActiveAllocations); //SameLine(); if (SmallButton("GC")) { g.GcCompactAll = true; } Separator(); @@ -11075,11 +11328,10 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Tools if (TreeNode("Tools")) { - // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. - if (Button("Item Picker..")) - DebugStartItemPicker(); + // Stack Tool is your best friend! + Checkbox("Show stack tool", &cfg->ShowStackTool); SameLine(); - MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); + MetricsHelpMarker("You can also call ImGui::ShowStackToolWindow() from your code."); Checkbox("Show windows begin order", &cfg->ShowWindowsBeginOrder); Checkbox("Show windows rectangles", &cfg->ShowWindowsRects); @@ -11097,8 +11349,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) } Unindent(); } - Checkbox("Show ImDrawCmd mesh when hovering", &cfg->ShowDrawCmdMesh); - Checkbox("Show ImDrawCmd bounding boxes when hovering", &cfg->ShowDrawCmdBoundingBoxes); Checkbox("Show tables rectangles", &cfg->ShowTablesRects); SameLine(); @@ -11145,6 +11395,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } + // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. + if (Button("Item Picker..")) + DebugStartItemPicker(); + SameLine(); + MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); + TreePop(); } @@ -11158,6 +11414,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) drawlist_count += g.Viewports[viewport_i]->DrawDataBuilder.GetDrawListCount(); if (TreeNode("DrawLists", "DrawLists (%d)", drawlist_count)) { + Checkbox("Show ImDrawCmd mesh when hovering", &cfg->ShowDrawCmdMesh); + Checkbox("Show ImDrawCmd bounding boxes when hovering", &cfg->ShowDrawCmdBoundingBoxes); for (int viewport_i = 0; viewport_i < g.Viewports.Size; viewport_i++) { ImGuiViewportP* viewport = g.Viewports[viewport_i]; @@ -11306,7 +11564,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); Text("NavInputSource: %s", input_source_names[g.NavInputSource]); Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); - Text("NavActivateId: 0x%08X, NavInputId: 0x%08X", g.NavActivateId, g.NavInputId); + Text("NavActivateId/DownId/PressedId/InputId: %08X/%08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId, g.NavActivateInputId); + Text("NavActivateFlags: %04X", g.NavActivateFlags); Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); @@ -11781,6 +12040,186 @@ void ImGui::DebugNodeWindowsList(ImVector* windows, const char* la TreePop(); } +//----------------------------------------------------------------------------- +// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) +//----------------------------------------------------------------------------- + +// [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. +void ImGui::UpdateDebugToolItemPicker() +{ + ImGuiContext& g = *GImGui; + g.DebugItemPickerBreakId = 0; + if (!g.DebugItemPickerActive) + return; + + const ImGuiID hovered_id = g.HoveredIdPreviousFrame; + SetMouseCursor(ImGuiMouseCursor_Hand); + if (IsKeyPressedMap(ImGuiKey_Escape)) + g.DebugItemPickerActive = false; + if (IsMouseClicked(0) && hovered_id) + { + g.DebugItemPickerBreakId = hovered_id; + g.DebugItemPickerActive = false; + } + SetNextWindowBgAlpha(0.60f); + BeginTooltip(); + Text("HoveredId: 0x%08X", hovered_id); + Text("Press ESC to abort picking."); + TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); + EndTooltip(); +} + +// [DEBUG] Stack Tool: update queries. Called by NewFrame() +void ImGui::UpdateDebugToolStackQueries() +{ + ImGuiContext& g = *GImGui; + ImGuiStackTool* tool = &g.DebugStackTool; + + // Clear hook when stack tool is not visible + g.DebugHookIdInfo = 0; + if (g.FrameCount != tool->LastActiveFrame + 1) + return; + + // Update queries. The steps are: -1: query Stack, >= 0: query each stack item + // We can only perform 1 ID Info query every frame. This is designed so the GetID() tests are cheap and constant-time + const ImGuiID query_id = g.ActiveId ? g.ActiveId : g.HoveredIdPreviousFrame; + if (tool->QueryId != query_id) + { + tool->QueryId = query_id; + tool->StackLevel = -1; + tool->Results.resize(0); + } + if (query_id == 0) + return; + + // Advance to next stack level when we got our result, or after 2 frames (in case we never get a result) + int stack_level = tool->StackLevel; + if (stack_level >= 0 && stack_level < tool->Results.Size) + if (tool->Results[stack_level].QuerySuccess || tool->Results[stack_level].QueryFrameCount > 2) + tool->StackLevel++; + + // Update hook + stack_level = tool->StackLevel; + if (stack_level == -1) + g.DebugHookIdInfo = query_id; + if (stack_level >= 0 && stack_level < tool->Results.Size) + { + g.DebugHookIdInfo = tool->Results[stack_level].ID; + tool->Results[stack_level].QueryFrameCount++; + } +} + +// [DEBUG] Stack tool: hooks called by GetID() family functions +void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiStackTool* tool = &g.DebugStackTool; + + // Step 0: stack query + // This assume that the ID was computed with the current ID stack, which tends to be the case for our widget. + if (tool->StackLevel == -1) + { + tool->StackLevel++; + tool->Results.resize(window->IDStack.Size + 1, ImGuiStackLevelInfo()); + for (int n = 0; n < window->IDStack.Size + 1; n++) + tool->Results[n].ID = (n < window->IDStack.Size) ? window->IDStack[n] : id; + return; + } + + // Step 1+: query for individual level + IM_ASSERT(tool->StackLevel >= 0); + if (tool->StackLevel != window->IDStack.Size) + return; + ImGuiStackLevelInfo* info = &tool->Results[tool->StackLevel]; + IM_ASSERT(info->ID == id && info->QueryFrameCount > 0); + + int data_len; + switch (data_type) + { + case ImGuiDataType_S32: + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%d", (int)(intptr_t)data_id); + break; + case ImGuiDataType_String: + data_len = data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)strlen((const char*)data_id); + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "\"%.*s\"", data_len, (const char*)data_id); + break; + case ImGuiDataType_Pointer: + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "(void*)0x%p", data_id); + break; + case ImGuiDataType_ID: + if (info->Desc[0] == 0) // PushOverrideID() is often used to avoid hashing twice, which would lead to 2 calls to DebugHookIdInfo(). We prioritize the first one. + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "0x%08X [override]", id); + break; + default: + IM_ASSERT(0); + } + info->QuerySuccess = true; +} + +// Stack Tool: Display UI +void ImGui::ShowStackToolWindow(bool* p_open) +{ + if (!Begin("Dear ImGui Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1) + { + End(); + return; + } + + // Display hovered/active status + ImGuiContext& g = *GImGui; + const ImGuiID hovered_id = g.HoveredIdPreviousFrame; + const ImGuiID active_id = g.ActiveId; +#ifdef IMGUI_ENABLE_TEST_ENGINE + Text("HoveredId: 0x%08X (\"%s\"), ActiveId: 0x%08X (\"%s\")", hovered_id, hovered_id ? ImGuiTestEngine_FindItemDebugLabel(&g, hovered_id) : "", active_id, active_id ? ImGuiTestEngine_FindItemDebugLabel(&g, active_id) : ""); +#else + Text("HoveredId: 0x%08X, ActiveId: 0x%08X", hovered_id, active_id); +#endif + SameLine(); + MetricsHelpMarker("Hover an item with the mouse to display elements of the ID Stack leading to the item's final ID.\nEach level of the stack correspond to a PushID() call.\nAll levels of the stack are hashed together to make the final ID of a widget (ID displayed at the bottom level of the stack).\nRead FAQ entry about the ID stack for details."); + + // Display decorated stack + ImGuiStackTool* tool = &g.DebugStackTool; + tool->LastActiveFrame = g.FrameCount; + if (tool->Results.Size > 0 && BeginTable("##table", 3, ImGuiTableFlags_Borders)) + { + const float id_width = CalcTextSize("0xDDDDDDDD").x; + TableSetupColumn("Seed", ImGuiTableColumnFlags_WidthFixed, id_width); + TableSetupColumn("PushID", ImGuiTableColumnFlags_WidthStretch); + TableSetupColumn("Result", ImGuiTableColumnFlags_WidthFixed, id_width); + TableHeadersRow(); + for (int n = 0; n < tool->Results.Size; n++) + { + ImGuiStackLevelInfo* info = &tool->Results[n]; + TableNextColumn(); + Text("0x%08X", (n > 0) ? tool->Results[n - 1].ID : 0); + + TableNextColumn(); + ImGuiWindow* window = (info->Desc[0] == 0 && n == 0) ? FindWindowByID(info->ID) : NULL; + if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked) + Text("\"%s\" [window]", window->Name); + else if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id) + TextUnformatted(info->Desc); + else if (tool->StackLevel >= tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers. + { +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (const char* label = ImGuiTestEngine_FindItemDebugLabel(&g, info->ID)) // Source: ImGuiTestEngine's ItemInfo() + Text("??? \"%s\"", label); + else +#endif + TextUnformatted("???"); + } + + TableNextColumn(); + Text("0x%08X", info->ID); + if (n == tool->Results.Size - 1) + TableSetBgColor(ImGuiTableBgTarget_CellBg, GetColorU32(ImGuiCol_Header)); + } + EndTable(); + } + End(); +} + #else void ImGui::ShowMetricsWindow(bool*) {} @@ -11796,7 +12235,12 @@ void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings*) {} void ImGui::DebugNodeWindowsList(ImVector*, const char*) {} void ImGui::DebugNodeViewport(ImGuiViewportP*) {} -#endif +void ImGui::ShowStackToolWindow(bool*) {} +void ImGui::DebugHookIdInfo(ImGuiID, ImGuiDataType, const void*, const void*) {} +void ImGui::UpdateDebugToolItemPicker() {} +void ImGui::UpdateDebugToolStackQueries() {} + +#endif // #ifndef IMGUI_DISABLE_METRICS_WINDOW //----------------------------------------------------------------------------- diff --git a/imgui/imgui.h b/imgui/imgui.h index b56cb852..f2606398 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (headers) // Help: @@ -15,7 +15,10 @@ // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues -// - Discussions https://github.com/ocornut/imgui/discussions + +// Getting Started? +// - For first-time users having issues compiling/linking/running or issues loading fonts: +// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. /* @@ -60,8 +63,8 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.84.2" -#define IMGUI_VERSION_NUM 18405 +#define IMGUI_VERSION "1.85" +#define IMGUI_VERSION_NUM 18500 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE @@ -89,7 +92,7 @@ Index of this file: #endif // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. -#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) +#if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) && !defined(__clang__) #define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) #define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0))) #elif !defined(IMGUI_USE_STB_SPRINTF) && (defined(__clang__) || defined(__GNUC__)) @@ -304,6 +307,7 @@ namespace ImGui // Demo, Debug, Information IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window. demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debugger window. display Dear ImGui internals: windows, draw commands, various internal state, etc. + IMGUI_API void ShowStackToolWindow(bool* p_open = NULL); // create Stack Tool window. hover items with mouse to query information about the source of their unique ID. IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. @@ -379,9 +383,8 @@ namespace ImGui // - Those functions are bound to be redesigned (they are confusing, incomplete and the Min/Max return values are in local window coordinates which increases confusion) IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates - IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min (roughly (0,0)-Scroll), in window coordinates - IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates - IMGUI_API float GetWindowContentRegionWidth(); // + IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min for the full window (roughly (0,0)-Scroll), in window coordinates + IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max for the full window (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates // Windows Scrolling IMGUI_API float GetScrollX(); // get scrolling amount [0 .. GetScrollMaxX()] @@ -520,12 +523,12 @@ namespace ImGui IMGUI_API bool Combo(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_max_height_in_items = -1); // Widgets: Drag Sliders - // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped and can go off-bounds. + // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped by default and can go off-bounds. Use ImGuiSliderFlags_AlwaysClamp to always clamp. // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every functions, note that a 'float v[X]' function argument is the same as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Format string may also be set to NULL or use the default format ("%f" or "%d"). // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). - // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits. + // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits if ImGuiSliderFlags_AlwaysClamp is not used. // - Use v_max = FLT_MAX / INT_MAX etc to avoid clamping to a maximum, same with v_min = -FLT_MAX / INT_MIN to avoid clamping to a minimum. // - We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. // - Legacy: Pre-1.78 there are DragXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. @@ -544,7 +547,7 @@ namespace ImGui IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed = 1.0f, const void* p_min = NULL, const void* p_max = NULL, const char* format = NULL, ImGuiSliderFlags flags = 0); // Widgets: Regular Sliders - // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped and can go off-bounds. + // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped by default and can go off-bounds. Use ImGuiSliderFlags_AlwaysClamp to always clamp. // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Format string may also be set to NULL or use the default format ("%f" or "%d"). // - Legacy: Pre-1.78 there are SliderXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. @@ -813,6 +816,7 @@ namespace ImGui // Disabling [BETA API] // - Disable all user interactions and dim items visuals (applying style.DisabledAlpha over current colors) + // - Those can be nested but it cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep everything disabled) // - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. IMGUI_API void BeginDisabled(bool disabled = true); IMGUI_API void EndDisabled(); @@ -1261,9 +1265,11 @@ enum ImGuiTableBgTarget_ enum ImGuiFocusedFlags_ { ImGuiFocusedFlags_None = 0, - ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused - ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy) - ImGuiFocusedFlags_AnyWindow = 1 << 2, // IsWindowFocused(): Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use 'io.WantCaptureMouse' instead! Please read the FAQ! + ImGuiFocusedFlags_ChildWindows = 1 << 0, // Return true if any children of the window is focused + ImGuiFocusedFlags_RootWindow = 1 << 1, // Test from root window (top most parent of the current hierarchy) + ImGuiFocusedFlags_AnyWindow = 1 << 2, // Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use 'io.WantCaptureMouse' instead! Please read the FAQ! + ImGuiFocusedFlags_NoPopupHierarchy = 1 << 3, // Do not consider popup hierarchy (do not treat popup emitter as parent of popup) (when used with _ChildWindows or _RootWindow) + //ImGuiFocusedFlags_DockHierarchy = 1 << 4, // Consider docking hierarchy (treat dockspace host as parent of docked window) (when used with _ChildWindows or _RootWindow) ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows }; @@ -1276,11 +1282,13 @@ enum ImGuiHoveredFlags_ ImGuiHoveredFlags_ChildWindows = 1 << 0, // IsWindowHovered() only: Return true if any children of the window is hovered ImGuiHoveredFlags_RootWindow = 1 << 1, // IsWindowHovered() only: Test from root window (top most parent of the current hierarchy) ImGuiHoveredFlags_AnyWindow = 1 << 2, // IsWindowHovered() only: Return true if any window is hovered - ImGuiHoveredFlags_AllowWhenBlockedByPopup = 1 << 3, // Return true even if a popup window is normally blocking access to this item/window - //ImGuiHoveredFlags_AllowWhenBlockedByModal = 1 << 4, // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet. - ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 5, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. - ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 6, // Return true even if the position is obstructed or overlapped by another window - ImGuiHoveredFlags_AllowWhenDisabled = 1 << 7, // Return true even if the item is disabled + ImGuiHoveredFlags_NoPopupHierarchy = 1 << 3, // IsWindowHovered() only: Do not consider popup hierarchy (do not treat popup emitter as parent of popup) (when used with _ChildWindows or _RootWindow) + //ImGuiHoveredFlags_DockHierarchy = 1 << 4, // IsWindowHovered() only: Consider docking hierarchy (treat dockspace host as parent of docked window) (when used with _ChildWindows or _RootWindow) + ImGuiHoveredFlags_AllowWhenBlockedByPopup = 1 << 5, // Return true even if a popup window is normally blocking access to this item/window + //ImGuiHoveredFlags_AllowWhenBlockedByModal = 1 << 6, // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet. + ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 7, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. + ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 8, // IsItemHovered() only: Return true even if the position is obstructed or overlapped by another window + ImGuiHoveredFlags_AllowWhenDisabled = 1 << 9, // IsItemHovered() only: Return true even if the item is disabled ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows }; @@ -1887,8 +1895,9 @@ struct ImGuiIO IMGUI_API void AddInputCharacter(unsigned int c); // Queue new character input IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue new character input from an UTF-16 character, it can be a surrogate IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string - IMGUI_API void ClearInputCharacters(); // Clear the text input buffer manually IMGUI_API void AddFocusEvent(bool focused); // Notifies Dear ImGui when hosting platform windows lose or gain input focus + IMGUI_API void ClearInputCharacters(); // [Internal] Clear the text input buffer manually + IMGUI_API void ClearInputKeys(); // [Internal] Release all keys //------------------------------------------------------------------ // Output - Updated by NewFrame() or EndFrame()/Render() @@ -1915,6 +1924,7 @@ struct ImGuiIO // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ + bool WantCaptureMouseUnlessPopupClose;// Alternative to WantCaptureMouse: (WantCaptureMouse == true && WantCaptureMouseUnlessPopupClose == false) when a click over void is expected to close a popup. ImGuiKeyModFlags KeyMods; // Key mods flags (same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags), updated by NewFrame() ImGuiKeyModFlags KeyModsPrev; // Previous key mods ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) @@ -1923,7 +1933,8 @@ struct ImGuiIO bool MouseClicked[5]; // Mouse button went from !Down to Down bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? bool MouseReleased[5]; // Mouse button went from Down to !Down - bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window. We don't request mouse capture from the application if click started outside ImGui bounds. + bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. + bool MouseDownOwnedUnlessPopupClose[5];//Track if button was clicked inside a dear imgui window. bool MouseDownWasDoubleClick[5]; // Track if button down was a double-click float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) float MouseDownDurationPrev[5]; // Previous time the mouse button has been down @@ -1934,6 +1945,7 @@ struct ImGuiIO float NavInputsDownDuration[ImGuiNavInput_COUNT]; float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. + bool AppFocusLost; ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16 ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. @@ -2816,6 +2828,8 @@ struct ImGuiViewport #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.85 (from August 2021) + static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } // OBSOLETED in 1.81 (from February 2021) IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index d75989d6..d4bac484 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (demo code) // Help: @@ -294,10 +294,12 @@ void ImGui::ShowDemoWindow(bool* p_open) // Dear ImGui Apps (accessible from the "Tools" menu) static bool show_app_metrics = false; + static bool show_app_stack_tool = false; static bool show_app_style_editor = false; static bool show_app_about = false; if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } + if (show_app_stack_tool) { ImGui::ShowStackToolWindow(&show_app_stack_tool); } if (show_app_about) { ImGui::ShowAboutWindow(&show_app_about); } if (show_app_style_editor) { @@ -379,9 +381,13 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::MenuItem("Documents", NULL, &show_app_documents); ImGui::EndMenu(); } + //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar! if (ImGui::BeginMenu("Tools")) { +#ifndef IMGUI_DISABLE_METRICS_WINDOW ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics); + ImGui::MenuItem("Stack Tool", NULL, &show_app_stack_tool); +#endif ImGui::MenuItem("Style Editor", NULL, &show_app_style_editor); ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about); ImGui::EndMenu(); @@ -1561,16 +1567,17 @@ static void ShowDemoWindowWidgets() } // Plot/Graph widgets are not very good. - // Consider writing your own, or using a third-party one, see: - // - ImPlot https://github.com/epezent/implot - // - others https://github.com/ocornut/imgui/wiki/Useful-Extensions + // Consider using a third-party library such as ImPlot: https://github.com/epezent/implot + // (see others https://github.com/ocornut/imgui/wiki/Useful-Extensions) if (ImGui::TreeNode("Plots Widgets")) { static bool animate = true; ImGui::Checkbox("Animate", &animate); + // Plot as lines and plot as histogram static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); + ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f)); // Fill an array of contiguous float values to plot // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float @@ -1600,7 +1607,6 @@ static void ShowDemoWindowWidgets() sprintf(overlay, "avg %f", average); ImGui::PlotLines("Lines", values, IM_ARRAYSIZE(values), values_offset, overlay, -1.0f, 1.0f, ImVec2(0, 80.0f)); } - ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f)); // Use functions to generate output // FIXME: This is rather awkward because current plot API only pass in indices. @@ -2185,7 +2191,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - if (ImGui::TreeNode("Querying Status (Edited/Active/Hovered etc.)")) + if (ImGui::TreeNode("Querying Item Status (Edited/Active/Hovered etc.)")) { // Select an item type const char* item_names[] = @@ -2271,43 +2277,63 @@ static void ShowDemoWindowWidgets() if (item_disabled) ImGui::EndDisabled(); + char buf[1] = ""; + ImGui::InputText("unused", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_ReadOnly); + ImGui::SameLine(); + HelpMarker("This widget is only here to be able to tab-out of the widgets above and see e.g. Deactivated() status."); + + ImGui::TreePop(); + } + + if (ImGui::TreeNode("Querying Window Status (Focused/Hovered etc.)")) + { static bool embed_all_inside_a_child_window = false; - ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window); + ImGui::Checkbox("Embed everything inside a child window for testing _RootWindow flag.", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), true); // Testing IsWindowFocused() function with its various flags. - // Note that the ImGuiFocusedFlags_XXX flags can be combined. ImGui::BulletText( "IsWindowFocused() = %d\n" "IsWindowFocused(_ChildWindows) = %d\n" + "IsWindowFocused(_ChildWindows|_NoPopupHierarchy) = %d\n" "IsWindowFocused(_ChildWindows|_RootWindow) = %d\n" + "IsWindowFocused(_ChildWindows|_RootWindow|_NoPopupHierarchy) = %d\n" "IsWindowFocused(_RootWindow) = %d\n" + "IsWindowFocused(_RootWindow|_NoPopupHierarchy) = %d\n" "IsWindowFocused(_AnyWindow) = %d\n", ImGui::IsWindowFocused(), ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows), + ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_NoPopupHierarchy), ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_RootWindow), + ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows | ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_NoPopupHierarchy), ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow), + ImGui::IsWindowFocused(ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_NoPopupHierarchy), ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow)); // Testing IsWindowHovered() function with its various flags. - // Note that the ImGuiHoveredFlags_XXX flags can be combined. ImGui::BulletText( "IsWindowHovered() = %d\n" "IsWindowHovered(_AllowWhenBlockedByPopup) = %d\n" "IsWindowHovered(_AllowWhenBlockedByActiveItem) = %d\n" "IsWindowHovered(_ChildWindows) = %d\n" + "IsWindowHovered(_ChildWindows|_NoPopupHierarchy) = %d\n" "IsWindowHovered(_ChildWindows|_RootWindow) = %d\n" - "IsWindowHovered(_ChildWindows|_AllowWhenBlockedByPopup) = %d\n" + "IsWindowHovered(_ChildWindows|_RootWindow|_NoPopupHierarchy) = %d\n" "IsWindowHovered(_RootWindow) = %d\n" + "IsWindowHovered(_RootWindow|_NoPopupHierarchy) = %d\n" + "IsWindowHovered(_ChildWindows|_AllowWhenBlockedByPopup) = %d\n" "IsWindowHovered(_AnyWindow) = %d\n", ImGui::IsWindowHovered(), ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy), ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow), - ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByPopup), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_NoPopupHierarchy), ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow), + ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_NoPopupHierarchy), + ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)); ImGui::BeginChild("child", ImVec2(0, 50), true); @@ -2316,9 +2342,6 @@ static void ShowDemoWindowWidgets() if (embed_all_inside_a_child_window) ImGui::EndChild(); - static char unused_str[] = "This widget is only here to be able to tab-out of the widgets above."; - ImGui::InputText("unused", unused_str, IM_ARRAYSIZE(unused_str), ImGuiInputTextFlags_ReadOnly); - // Calling IsItemHovered() after begin returns the hovered status of the title bar. // This is useful in particular if you want to create a context menu associated to the title bar of a window. static bool test_window = false; @@ -2372,7 +2395,7 @@ static void ShowDemoWindowLayout() ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; if (disable_mouse_wheel) window_flags |= ImGuiWindowFlags_NoScrollWithMouse; - ImGui::BeginChild("ChildL", ImVec2(ImGui::GetWindowContentRegionWidth() * 0.5f, 260), false, window_flags); + ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), false, window_flags); for (int i = 0; i < 100; i++) ImGui::Text("%04d: scrollable region", i); ImGui::EndChild(); @@ -5481,6 +5504,7 @@ static void ShowDemoWindowMisc() // Display ImGuiIO output flags ImGui::Text("WantCaptureMouse: %d", io.WantCaptureMouse); + ImGui::Text("WantCaptureMouseUnlessPopupClose: %d", io.WantCaptureMouseUnlessPopupClose); ImGui::Text("WantCaptureKeyboard: %d", io.WantCaptureKeyboard); ImGui::Text("WantTextInput: %d", io.WantTextInput); ImGui::Text("WantSetMousePos: %d", io.WantSetMousePos); diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index e1d7b7ee..30b100bb 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (drawing and font code) /* @@ -1998,7 +1998,7 @@ void ImFontAtlas::ClearInputData() ConfigData.clear(); CustomRects.clear(); PackIdMouseCursors = PackIdLines = -1; - TexReady = false; + // Important: we leave TexReady untouched } void ImFontAtlas::ClearTexData() diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index 8cac7241..bf2a6543 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -308,8 +308,9 @@ bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks) void ImGui_ImplGlfw_Shutdown() { - ImGuiIO& io = ImGui::GetIO(); ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + IM_ASSERT(bd != NULL && "No platform backend to shutdown, or already shutdown?"); + ImGuiIO& io = ImGui::GetIO(); if (bd->InstalledCallbacks) { diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index 5637a0fe..58664ea1 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -105,6 +105,9 @@ #include #endif #elif defined(IMGUI_IMPL_OPENGL_ES3) +#if defined(__APPLE__) +#include +#endif #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) #include // Use GL ES 3 #else @@ -266,8 +269,9 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) void ImGui_ImplOpenGL3_Shutdown() { - ImGuiIO& io = ImGui::GetIO(); ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + IM_ASSERT(bd != NULL && "No renderer backend to shutdown, or already shutdown?"); + ImGuiIO& io = ImGui::GetIO(); ImGui_ImplOpenGL3_DestroyDeviceObjects(); io.BackendRendererName = NULL; @@ -439,26 +443,22 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) else { // Project scissor/clipping rectangles into framebuffer space - ImVec4 clip_rect; - clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x; - clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y; - clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x; - clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y; - - if (clip_rect.x < fb_width && clip_rect.y < fb_height && clip_rect.z >= 0.0f && clip_rect.w >= 0.0f) - { - // Apply scissor/clipping rectangle - glScissor((int)clip_rect.x, (int)(fb_height - clip_rect.w), (int)(clip_rect.z - clip_rect.x), (int)(clip_rect.w - clip_rect.y)); - - // Bind texture, Draw - glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()); + ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); + ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); + if (clip_max.x < clip_min.x || clip_max.y < clip_min.y) + continue; + + // Apply scissor/clipping rectangle (Y is inverted in OpenGL) + glScissor((int)clip_min.x, (int)(fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y)); + + // Bind texture, Draw + glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET - if (bd->GlVersion >= 320) - glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); - else + if (bd->GlVersion >= 320) + glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); + else #endif - glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); - } + glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); } } } diff --git a/imgui/imgui_impl_opengl3.h b/imgui/imgui_impl_opengl3.h index b1fb49c7..98c9aca1 100644 --- a/imgui/imgui_impl_opengl3.h +++ b/imgui/imgui_impl_opengl3.h @@ -42,7 +42,7 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); // Try to detect GLES on matching platforms #if defined(__APPLE__) -#include "TargetConditionals.h" +#include #endif #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" diff --git a/imgui/imgui_impl_opengl3_loader.h b/imgui/imgui_impl_opengl3_loader.h index 021921ea..9313deda 100644 --- a/imgui/imgui_impl_opengl3_loader.h +++ b/imgui/imgui_impl_opengl3_loader.h @@ -1,3 +1,22 @@ +//----------------------------------------------------------------------------- +// About imgui_impl_opengl3_loader.h: +// +// We embed our own OpenGL loader to not require user to provide their own or to have to use ours, +// which proved to be endless problems for users. +// Our loader is custom-generated, based on gl3w but automatically filtered to only include +// enums/functions that we use in our imgui_impl_opengl3.cpp source file in order to be small. +// +// YOU SHOULD NOT NEED TO INCLUDE/USE THIS DIRECTLY. THIS IS USED BY imgui_impl_opengl3.cpp ONLY. +// THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE. +// +// Regenerate with: +// python gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt +// +// More info: +// https://github.com/dearimgui/gl3w_stripped +// https://github.com/ocornut/imgui/issues/4445 +//----------------------------------------------------------------------------- + /* * This file was generated with gl3w_gen.py, part of imgl3w * (hosted at https://github.com/dearimgui/gl3w_stripped) @@ -26,14 +45,11 @@ * OTHER DEALINGS IN THE SOFTWARE. */ -// We embed our own OpenGL loader to not require user to provide their own or to have to use ours, which proved to be endless problems for users. -// Our loader is custom-generated, based on gl3w but automatically filtered to only include enums/functions that we use in this source file. -// Regenerate with: python gl3w_gen.py --imgui-dir /path/to/imgui/ -// see https://github.com/dearimgui/gl3w_stripped for more info. #ifndef __gl3w_h_ #define __gl3w_h_ // Adapted from KHR/khrplatform.h to avoid including entire file. +#ifndef __khrplatform_h_ typedef float khronos_float_t; typedef signed char khronos_int8_t; typedef unsigned char khronos_uint8_t; @@ -58,6 +74,7 @@ typedef uint64_t khronos_uint64_t; typedef signed long long khronos_int64_t; typedef unsigned long long khronos_uint64_t; #endif +#endif // __khrplatform_h_ #ifndef __gl_glcorearb_h_ #define __gl_glcorearb_h_ 1 @@ -162,6 +179,7 @@ typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap); typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap); typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param); typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); +typedef GLenum (APIENTRYP PFNGLGETERRORPROC) (void); typedef void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *data); typedef const GLubyte *(APIENTRYP PFNGLGETSTRINGPROC) (GLenum name); typedef GLboolean (APIENTRYP PFNGLISENABLEDPROC) (GLenum cap); @@ -177,6 +195,7 @@ GLAPI void APIENTRY glDisable (GLenum cap); GLAPI void APIENTRY glEnable (GLenum cap); GLAPI void APIENTRY glPixelStorei (GLenum pname, GLint param); GLAPI void APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); +GLAPI GLenum APIENTRY glGetError (void); GLAPI void APIENTRY glGetIntegerv (GLenum pname, GLint *data); GLAPI const GLubyte *APIENTRY glGetString (GLenum name); GLAPI GLboolean APIENTRY glIsEnabled (GLenum cap); @@ -416,7 +435,7 @@ GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); /* gl3w internal state */ union GL3WProcs { - GL3WglProc ptr[52]; + GL3WglProc ptr[53]; struct { PFNGLACTIVETEXTUREPROC ActiveTexture; PFNGLATTACHSHADERPROC AttachShader; @@ -448,6 +467,7 @@ union GL3WProcs { PFNGLGENTEXTURESPROC GenTextures; PFNGLGENVERTEXARRAYSPROC GenVertexArrays; PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; + PFNGLGETERRORPROC GetError; PFNGLGETINTEGERVPROC GetIntegerv; PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog; PFNGLGETPROGRAMIVPROC GetProgramiv; @@ -473,61 +493,62 @@ union GL3WProcs { } gl; }; -GL3W_API extern union GL3WProcs gl3wProcs; +GL3W_API extern union GL3WProcs imgl3wProcs; /* OpenGL functions */ -#define glActiveTexture gl3wProcs.gl.ActiveTexture -#define glAttachShader gl3wProcs.gl.AttachShader -#define glBindBuffer gl3wProcs.gl.BindBuffer -#define glBindSampler gl3wProcs.gl.BindSampler -#define glBindTexture gl3wProcs.gl.BindTexture -#define glBindVertexArray gl3wProcs.gl.BindVertexArray -#define glBlendEquation gl3wProcs.gl.BlendEquation -#define glBlendEquationSeparate gl3wProcs.gl.BlendEquationSeparate -#define glBlendFuncSeparate gl3wProcs.gl.BlendFuncSeparate -#define glBufferData gl3wProcs.gl.BufferData -#define glClear gl3wProcs.gl.Clear -#define glClearColor gl3wProcs.gl.ClearColor -#define glCompileShader gl3wProcs.gl.CompileShader -#define glCreateProgram gl3wProcs.gl.CreateProgram -#define glCreateShader gl3wProcs.gl.CreateShader -#define glDeleteBuffers gl3wProcs.gl.DeleteBuffers -#define glDeleteProgram gl3wProcs.gl.DeleteProgram -#define glDeleteShader gl3wProcs.gl.DeleteShader -#define glDeleteTextures gl3wProcs.gl.DeleteTextures -#define glDeleteVertexArrays gl3wProcs.gl.DeleteVertexArrays -#define glDetachShader gl3wProcs.gl.DetachShader -#define glDisable gl3wProcs.gl.Disable -#define glDrawElements gl3wProcs.gl.DrawElements -#define glDrawElementsBaseVertex gl3wProcs.gl.DrawElementsBaseVertex -#define glEnable gl3wProcs.gl.Enable -#define glEnableVertexAttribArray gl3wProcs.gl.EnableVertexAttribArray -#define glGenBuffers gl3wProcs.gl.GenBuffers -#define glGenTextures gl3wProcs.gl.GenTextures -#define glGenVertexArrays gl3wProcs.gl.GenVertexArrays -#define glGetAttribLocation gl3wProcs.gl.GetAttribLocation -#define glGetIntegerv gl3wProcs.gl.GetIntegerv -#define glGetProgramInfoLog gl3wProcs.gl.GetProgramInfoLog -#define glGetProgramiv gl3wProcs.gl.GetProgramiv -#define glGetShaderInfoLog gl3wProcs.gl.GetShaderInfoLog -#define glGetShaderiv gl3wProcs.gl.GetShaderiv -#define glGetString gl3wProcs.gl.GetString -#define glGetStringi gl3wProcs.gl.GetStringi -#define glGetUniformLocation gl3wProcs.gl.GetUniformLocation -#define glIsEnabled gl3wProcs.gl.IsEnabled -#define glLinkProgram gl3wProcs.gl.LinkProgram -#define glPixelStorei gl3wProcs.gl.PixelStorei -#define glPolygonMode gl3wProcs.gl.PolygonMode -#define glReadPixels gl3wProcs.gl.ReadPixels -#define glScissor gl3wProcs.gl.Scissor -#define glShaderSource gl3wProcs.gl.ShaderSource -#define glTexImage2D gl3wProcs.gl.TexImage2D -#define glTexParameteri gl3wProcs.gl.TexParameteri -#define glUniform1i gl3wProcs.gl.Uniform1i -#define glUniformMatrix4fv gl3wProcs.gl.UniformMatrix4fv -#define glUseProgram gl3wProcs.gl.UseProgram -#define glVertexAttribPointer gl3wProcs.gl.VertexAttribPointer -#define glViewport gl3wProcs.gl.Viewport +#define glActiveTexture imgl3wProcs.gl.ActiveTexture +#define glAttachShader imgl3wProcs.gl.AttachShader +#define glBindBuffer imgl3wProcs.gl.BindBuffer +#define glBindSampler imgl3wProcs.gl.BindSampler +#define glBindTexture imgl3wProcs.gl.BindTexture +#define glBindVertexArray imgl3wProcs.gl.BindVertexArray +#define glBlendEquation imgl3wProcs.gl.BlendEquation +#define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate +#define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate +#define glBufferData imgl3wProcs.gl.BufferData +#define glClear imgl3wProcs.gl.Clear +#define glClearColor imgl3wProcs.gl.ClearColor +#define glCompileShader imgl3wProcs.gl.CompileShader +#define glCreateProgram imgl3wProcs.gl.CreateProgram +#define glCreateShader imgl3wProcs.gl.CreateShader +#define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers +#define glDeleteProgram imgl3wProcs.gl.DeleteProgram +#define glDeleteShader imgl3wProcs.gl.DeleteShader +#define glDeleteTextures imgl3wProcs.gl.DeleteTextures +#define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays +#define glDetachShader imgl3wProcs.gl.DetachShader +#define glDisable imgl3wProcs.gl.Disable +#define glDrawElements imgl3wProcs.gl.DrawElements +#define glDrawElementsBaseVertex imgl3wProcs.gl.DrawElementsBaseVertex +#define glEnable imgl3wProcs.gl.Enable +#define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray +#define glGenBuffers imgl3wProcs.gl.GenBuffers +#define glGenTextures imgl3wProcs.gl.GenTextures +#define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays +#define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation +#define glGetError imgl3wProcs.gl.GetError +#define glGetIntegerv imgl3wProcs.gl.GetIntegerv +#define glGetProgramInfoLog imgl3wProcs.gl.GetProgramInfoLog +#define glGetProgramiv imgl3wProcs.gl.GetProgramiv +#define glGetShaderInfoLog imgl3wProcs.gl.GetShaderInfoLog +#define glGetShaderiv imgl3wProcs.gl.GetShaderiv +#define glGetString imgl3wProcs.gl.GetString +#define glGetStringi imgl3wProcs.gl.GetStringi +#define glGetUniformLocation imgl3wProcs.gl.GetUniformLocation +#define glIsEnabled imgl3wProcs.gl.IsEnabled +#define glLinkProgram imgl3wProcs.gl.LinkProgram +#define glPixelStorei imgl3wProcs.gl.PixelStorei +#define glPolygonMode imgl3wProcs.gl.PolygonMode +#define glReadPixels imgl3wProcs.gl.ReadPixels +#define glScissor imgl3wProcs.gl.Scissor +#define glShaderSource imgl3wProcs.gl.ShaderSource +#define glTexImage2D imgl3wProcs.gl.TexImage2D +#define glTexParameteri imgl3wProcs.gl.TexParameteri +#define glUniform1i imgl3wProcs.gl.Uniform1i +#define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv +#define glUseProgram imgl3wProcs.gl.UseProgram +#define glVertexAttribPointer imgl3wProcs.gl.VertexAttribPointer +#define glViewport imgl3wProcs.gl.Viewport #ifdef __cplusplus } @@ -691,6 +712,7 @@ static const char *proc_names[] = { "glGenTextures", "glGenVertexArrays", "glGetAttribLocation", + "glGetError", "glGetIntegerv", "glGetProgramInfoLog", "glGetProgramiv", @@ -715,13 +737,13 @@ static const char *proc_names[] = { "glViewport", }; -GL3W_API union GL3WProcs gl3wProcs; +GL3W_API union GL3WProcs imgl3wProcs; static void load_procs(GL3WGetProcAddressProc proc) { size_t i; for (i = 0; i < ARRAY_SIZE(proc_names); i++) - gl3wProcs.ptr[i] = proc(proc_names[i]); + imgl3wProcs.ptr[i] = proc(proc_names[i]); } #ifdef __cplusplus diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index 1eb0cb7a..fa5dec3e 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -18,12 +18,13 @@ Index of this file: // [SECTION] Generic helpers // [SECTION] ImDrawList support // [SECTION] Widgets support: flags, enums, data structures +// [SECTION] Navigation support // [SECTION] Columns support // [SECTION] Multi-select support // [SECTION] Docking support // [SECTION] Viewport support // [SECTION] Settings support -// [SECTION] Metrics, Debug +// [SECTION] Metrics, Debug tools // [SECTION] Generic context hooks // [SECTION] ImGuiContext (main imgui context) // [SECTION] ImGuiWindowTempData, ImGuiWindow @@ -82,19 +83,13 @@ Index of this file: #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" #pragma clang diagnostic ignored "-Wdouble-promotion" #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wmissing-noreturn" // warning: function 'xxx' could be declared with attribute 'noreturn' #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif -// Helper macros -#if defined(__clang__) -#define IM_NORETURN __attribute__((noreturn)) -#else -#define IM_NORETURN -#endif - // Legacy defines #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 #error Use IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS @@ -148,8 +143,8 @@ struct ImGuiWindowSettings; // Storage for a window .ini settings (we ke // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical +typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later) typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() -typedef int ImGuiItemAddFlags; // -> enum ImGuiItemAddFlags_ // Flags: for ItemAdd() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() @@ -157,6 +152,7 @@ typedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // F typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests typedef int ImGuiNextItemDataFlags; // -> enum ImGuiNextItemDataFlags_ // Flags: for SetNextItemXXX() functions typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// Flags: for SetNextWindowXXX() functions +typedef int ImGuiScrollFlags; // -> enum ImGuiScrollFlags_ // Flags: for ScrollToItem() and navigation requests typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for SeparatorEx() typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() @@ -747,15 +743,8 @@ enum ImGuiItemFlags_ ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false // Disable item being a candidate for default focus (e.g. used by title bar items) ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // Disable MenuItem/Selectable() automatically closing their popup window ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) - ImGuiItemFlags_ReadOnly = 1 << 7 // false // [ALPHA] Allow hovering interactions but underlying value is not changed. -}; - -// Flags for ItemAdd() -// FIXME-NAV: _Focusable is _ALMOST_ what you would expect to be called '_TabStop' but because SetKeyboardFocusHere() works on items with no TabStop we distinguish Focusable from TabStop. -enum ImGuiItemAddFlags_ -{ - ImGuiItemAddFlags_None = 0, - ImGuiItemAddFlags_Focusable = 1 << 0 // FIXME-NAV: In current/legacy scheme, Focusable+TabStop support are opt-in by widgets. We will transition it toward being opt-out, so this flag is expected to eventually disappear. + ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. + ImGuiItemFlags_Inputable = 1 << 8 // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. }; // Storage for LastItem data @@ -763,16 +752,14 @@ enum ImGuiItemStatusFlags_ { ImGuiItemStatusFlags_None = 0, ImGuiItemStatusFlags_HoveredRect = 1 << 0, // Mouse position is within item rectangle (does NOT mean that the window is in correct z-order and can be hovered!, this is only one part of the most-common IsItemHovered test) - ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, // window->DC.LastItemDisplayRect is valid + ImGuiItemStatusFlags_HasDisplayRect = 1 << 1, // g.LastItemData.DisplayRect is valid ImGuiItemStatusFlags_Edited = 1 << 2, // Value exposed by item was edited in the current frame (should match the bool return value of most widgets) ImGuiItemStatusFlags_ToggledSelection = 1 << 3, // Set when Selectable(), TreeNode() reports toggling a selection. We can't report "Selected", only state changes, in order to easily handle clipping with less issues. ImGuiItemStatusFlags_ToggledOpen = 1 << 4, // Set when TreeNode() reports toggling their open state. ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. ImGuiItemStatusFlags_HoveredWindow = 1 << 7, // Override the HoveredWindow test to allow cross-window hover testing. - ImGuiItemStatusFlags_FocusedByCode = 1 << 8, // Set when the Focusable item just got focused from code. - ImGuiItemStatusFlags_FocusedByTabbing = 1 << 9, // Set when the Focusable item just got focused by Tabbing. - ImGuiItemStatusFlags_Focused = ImGuiItemStatusFlags_FocusedByCode | ImGuiItemStatusFlags_FocusedByTabbing + ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8 // Set when the Focusable item just got focused by Tabbing (FIXME: to be removed soon) #ifdef IMGUI_ENABLE_TEST_ENGINE , // [imgui_tests only] @@ -921,49 +908,6 @@ enum ImGuiInputReadMode ImGuiInputReadMode_RepeatFast }; -enum ImGuiNavHighlightFlags_ -{ - ImGuiNavHighlightFlags_None = 0, - ImGuiNavHighlightFlags_TypeDefault = 1 << 0, - ImGuiNavHighlightFlags_TypeThin = 1 << 1, - ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. - ImGuiNavHighlightFlags_NoRounding = 1 << 3 -}; - -enum ImGuiNavDirSourceFlags_ -{ - ImGuiNavDirSourceFlags_None = 0, - ImGuiNavDirSourceFlags_Keyboard = 1 << 0, - ImGuiNavDirSourceFlags_PadDPad = 1 << 1, - ImGuiNavDirSourceFlags_PadLStick = 1 << 2 -}; - -enum ImGuiNavMoveFlags_ -{ - ImGuiNavMoveFlags_None = 0, - ImGuiNavMoveFlags_LoopX = 1 << 0, // On failed request, restart from opposite side - ImGuiNavMoveFlags_LoopY = 1 << 1, - ImGuiNavMoveFlags_WrapX = 1 << 2, // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left) - ImGuiNavMoveFlags_WrapY = 1 << 3, // This is not super useful for provided for completeness - ImGuiNavMoveFlags_AllowCurrentNavId = 1 << 4, // Allow scoring and considering the current NavId as a move target candidate. This is used when the move source is offset (e.g. pressing PageDown actually needs to send a Up move request, if we are pressing PageDown from the bottom-most item we need to stay in place) - ImGuiNavMoveFlags_AlsoScoreVisibleSet = 1 << 5, // Store alternate result in NavMoveResultLocalVisibleSet that only comprise elements that are already fully visible. - ImGuiNavMoveFlags_ScrollToEdge = 1 << 6 -}; - -enum ImGuiNavForward -{ - ImGuiNavForward_None, - ImGuiNavForward_ForwardQueued, - ImGuiNavForward_ForwardActive -}; - -enum ImGuiNavLayer -{ - ImGuiNavLayer_Main = 0, // Main scrolling layer - ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt/ImGuiNavInput_Menu) - ImGuiNavLayer_COUNT -}; - enum ImGuiPopupPositionPolicy { ImGuiPopupPositionPolicy_Default, @@ -1110,20 +1054,6 @@ struct ImGuiPopupData ImGuiPopupData() { memset(this, 0, sizeof(*this)); OpenFrameCount = -1; } }; -struct ImGuiNavItemData -{ - ImGuiWindow* Window; // Init,Move // Best candidate window (result->ItemWindow->RootWindowForNav == request->Window) - ImGuiID ID; // Init,Move // Best candidate item ID - ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID - ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space - float DistBox; // Move // Best candidate box distance to current NavId - float DistCenter; // Move // Best candidate center distance to current NavId - float DistAxial; // Move // Best candidate axial distance to current NavId - - ImGuiNavItemData() { Clear(); } - void Clear() { Window = NULL; ID = FocusScopeId = 0; RectRel = ImRect(); DistBox = DistCenter = DistAxial = FLT_MAX; } -}; - enum ImGuiNextWindowDataFlags_ { ImGuiNextWindowDataFlags_None = 0, @@ -1185,17 +1115,36 @@ struct ImGuiLastItemData ImGuiID ID; ImGuiItemFlags InFlags; // See ImGuiItemFlags_ ImGuiItemStatusFlags StatusFlags; // See ImGuiItemStatusFlags_ - ImRect Rect; - ImRect DisplayRect; + ImRect Rect; // Full rectangle + ImRect NavRect; // Navigation scoring rectangle (not displayed) + ImRect DisplayRect; // Display rectangle (only if ImGuiItemStatusFlags_HasDisplayRect is set) ImGuiLastItemData() { memset(this, 0, sizeof(*this)); } }; +struct IMGUI_API ImGuiStackSizes +{ + short SizeOfIDStack; + short SizeOfColorStack; + short SizeOfStyleVarStack; + short SizeOfFontStack; + short SizeOfFocusScopeStack; + short SizeOfGroupStack; + short SizeOfItemFlagsStack; + short SizeOfBeginPopupStack; + short SizeOfDisabledStack; + + ImGuiStackSizes() { memset(this, 0, sizeof(*this)); } + void SetToCurrentState(); + void CompareWithCurrentState(); +}; + // Data saved for each window pushed into the stack struct ImGuiWindowStackData { ImGuiWindow* Window; ImGuiLastItemData ParentLastItemDataBackup; + ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting }; struct ImGuiShrinkWidthItem @@ -1213,6 +1162,89 @@ struct ImGuiPtrOrIndex ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } }; +//----------------------------------------------------------------------------- +// [SECTION] Navigation support +//----------------------------------------------------------------------------- + +enum ImGuiActivateFlags_ +{ + ImGuiActivateFlags_None = 0, + ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default if keyboard is available. + ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default if keyboard is not available. + ImGuiActivateFlags_TryToPreserveState = 1 << 2 // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) +}; + +// Early work-in-progress API for ScrollToItem() +enum ImGuiScrollFlags_ +{ + ImGuiScrollFlags_None = 0, + ImGuiScrollFlags_KeepVisibleEdgeX = 1 << 0, // If item is not visible: scroll as little as possible on X axis to bring item back into view [default for X axis] + ImGuiScrollFlags_KeepVisibleEdgeY = 1 << 1, // If item is not visible: scroll as little as possible on Y axis to bring item back into view [default for Y axis for windows that are already visible] + ImGuiScrollFlags_KeepVisibleCenterX = 1 << 2, // If item is not visible: scroll to make the item centered on X axis [rarely used] + ImGuiScrollFlags_KeepVisibleCenterY = 1 << 3, // If item is not visible: scroll to make the item centered on Y axis + ImGuiScrollFlags_AlwaysCenterX = 1 << 4, // Always center the result item on X axis [rarely used] + ImGuiScrollFlags_AlwaysCenterY = 1 << 5, // Always center the result item on Y axis [default for Y axis for appearing window) + ImGuiScrollFlags_NoScrollParent = 1 << 6, // Disable forwarding scrolling to parent window if required to keep item/rect visible (only scroll window the function was applied to). + ImGuiScrollFlags_MaskX_ = ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleCenterX | ImGuiScrollFlags_AlwaysCenterX, + ImGuiScrollFlags_MaskY_ = ImGuiScrollFlags_KeepVisibleEdgeY | ImGuiScrollFlags_KeepVisibleCenterY | ImGuiScrollFlags_AlwaysCenterY +}; + +enum ImGuiNavHighlightFlags_ +{ + ImGuiNavHighlightFlags_None = 0, + ImGuiNavHighlightFlags_TypeDefault = 1 << 0, + ImGuiNavHighlightFlags_TypeThin = 1 << 1, + ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. + ImGuiNavHighlightFlags_NoRounding = 1 << 3 +}; + +enum ImGuiNavDirSourceFlags_ +{ + ImGuiNavDirSourceFlags_None = 0, + ImGuiNavDirSourceFlags_Keyboard = 1 << 0, + ImGuiNavDirSourceFlags_PadDPad = 1 << 1, + ImGuiNavDirSourceFlags_PadLStick = 1 << 2 +}; + +enum ImGuiNavMoveFlags_ +{ + ImGuiNavMoveFlags_None = 0, + ImGuiNavMoveFlags_LoopX = 1 << 0, // On failed request, restart from opposite side + ImGuiNavMoveFlags_LoopY = 1 << 1, + ImGuiNavMoveFlags_WrapX = 1 << 2, // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left) + ImGuiNavMoveFlags_WrapY = 1 << 3, // This is not super useful but provided for completeness + ImGuiNavMoveFlags_AllowCurrentNavId = 1 << 4, // Allow scoring and considering the current NavId as a move target candidate. This is used when the move source is offset (e.g. pressing PageDown actually needs to send a Up move request, if we are pressing PageDown from the bottom-most item we need to stay in place) + ImGuiNavMoveFlags_AlsoScoreVisibleSet = 1 << 5, // Store alternate result in NavMoveResultLocalVisible that only comprise elements that are already fully visible (used by PageUp/PageDown) + ImGuiNavMoveFlags_ScrollToEdgeY = 1 << 6, // Force scrolling to min/max (used by Home/End) // FIXME-NAV: Aim to remove or reword, probably unnecessary + ImGuiNavMoveFlags_Forwarded = 1 << 7, + ImGuiNavMoveFlags_DebugNoResult = 1 << 8, // Dummy scoring for debug purpose, don't apply result + ImGuiNavMoveFlags_Tabbing = 1 << 9, // == Focus + Activate if item is Inputable + DontChangeNavHighlight + ImGuiNavMoveFlags_Activate = 1 << 10, + ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 11 // Do not alter the visible state of keyboard vs mouse nav highlight +}; + +enum ImGuiNavLayer +{ + ImGuiNavLayer_Main = 0, // Main scrolling layer + ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt/ImGuiNavInput_Menu) + ImGuiNavLayer_COUNT +}; + +struct ImGuiNavItemData +{ + ImGuiWindow* Window; // Init,Move // Best candidate window (result->ItemWindow->RootWindowForNav == request->Window) + ImGuiID ID; // Init,Move // Best candidate item ID + ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID + ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space + ImGuiItemFlags InFlags; // ????,Move // Best candidate item flags + float DistBox; // Move // Best candidate box distance to current NavId + float DistCenter; // Move // Best candidate center distance to current NavId + float DistAxial; // Move // Best candidate axial distance to current NavId + + ImGuiNavItemData() { Clear(); } + void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; DistBox = DistCenter = DistAxial = FLT_MAX; } +}; + //----------------------------------------------------------------------------- // [SECTION] Columns support //----------------------------------------------------------------------------- @@ -1352,11 +1384,12 @@ struct ImGuiSettingsHandler }; //----------------------------------------------------------------------------- -// [SECTION] Metrics, Debug +// [SECTION] Metrics, Debug Tools //----------------------------------------------------------------------------- struct ImGuiMetricsConfig { + bool ShowStackTool; bool ShowWindowsRects; bool ShowWindowsBeginOrder; bool ShowTablesRects; @@ -1367,6 +1400,7 @@ struct ImGuiMetricsConfig ImGuiMetricsConfig() { + ShowStackTool = false; ShowWindowsRects = false; ShowWindowsBeginOrder = false; ShowTablesRects = false; @@ -1377,19 +1411,25 @@ struct ImGuiMetricsConfig } }; -struct IMGUI_API ImGuiStackSizes +struct ImGuiStackLevelInfo { - short SizeOfIDStack; - short SizeOfColorStack; - short SizeOfStyleVarStack; - short SizeOfFontStack; - short SizeOfFocusScopeStack; - short SizeOfGroupStack; - short SizeOfBeginPopupStack; + ImGuiID ID; + ImS8 QueryFrameCount; // >= 1: Query in progress + bool QuerySuccess; // Obtained result from DebugHookIdInfo() + char Desc[58]; // Arbitrarily sized buffer to hold a result (FIXME: could replace Results[] with a chunk stream?) - ImGuiStackSizes() { memset(this, 0, sizeof(*this)); } - void SetToCurrentState(); - void CompareWithCurrentState(); + ImGuiStackLevelInfo() { memset(this, 0, sizeof(*this)); } +}; + +// State for Stack tool queries +struct ImGuiStackTool +{ + int LastActiveFrame; + int StackLevel; // -1: query stack and resize Results, >= 0: individual stack level + ImGuiID QueryId; // ID to query details for + ImVector Results; + + ImGuiStackTool() { memset(this, 0, sizeof(*this)); } }; //----------------------------------------------------------------------------- @@ -1433,7 +1473,6 @@ struct ImGuiContext bool WithinEndChild; // Set within EndChild() bool GcCompactAll; // Request full GC bool TestEngineHookItems; // Will call test engine hooks: ImGuiTestEngineHook_ItemAdd(), ImGuiTestEngineHook_ItemInfo(), ImGuiTestEngineHook_Log() - ImGuiID TestEngineHookIdInfo; // Will call test engine hooks: ImGuiTestEngineHook_IdInfo() from GetID() void* TestEngine; // Test engine user data // Windows state @@ -1453,6 +1492,7 @@ struct ImGuiContext float WheelingWindowTimer; // Item/widgets state and tracking information + ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; bool HoveredIdAllowOverlap; @@ -1511,37 +1551,43 @@ struct ImGuiContext ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 - ImGuiID NavInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0 + ImGuiID NavActivateInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0; ImGuiActivateFlags_PreferInput will be set and NavActivateId will be 0. + ImGuiActivateFlags NavActivateFlags; ImGuiID NavJustTabbedId; // Just tabbed to this id. ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). ImGuiKeyModFlags NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. + ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. - ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->NavRectRel[], modified for directional navigation scoring. - int NavScoringCount; // Metrics for debugging ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) bool NavDisableMouseHover; // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again. - bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest + + // Navigation: Init & Move Requests + bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest this is to perform early out in ItemAdd() bool NavInitRequest; // Init request for appearing window to select first item bool NavInitRequestFromMove; ImGuiID NavInitResultId; // Init request result (first item of the window, or one for which SetItemDefaultFocus() was called) ImRect NavInitResultRectRel; // Init request result rectangle (relative to parent window) - bool NavMoveRequest; // Move request for this frame - ImGuiNavMoveFlags NavMoveRequestFlags; - ImGuiNavForward NavMoveRequestForward; // None / ForwardQueued / ForwardActive (this is used to navigate sibling parent menus from a child menu) - ImGuiKeyModFlags NavMoveRequestKeyMods; - ImGuiDir NavMoveDir, NavMoveDirLast; // Direction of the move request (left/right/up/down), direction of the previous move request + bool NavMoveSubmitted; // Move request submitted, will process result on next NewFrame() + bool NavMoveScoringItems; // Move request submitted, still scoring incoming items + bool NavMoveForwardToNextFrame; + ImGuiNavMoveFlags NavMoveFlags; + ImGuiScrollFlags NavMoveScrollFlags; + ImGuiKeyModFlags NavMoveKeyMods; + ImGuiDir NavMoveDir; // Direction of the move request (left/right/up/down) + ImGuiDir NavMoveDirForDebug; ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? + ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->NavRectRel[], modified for directional navigation scoring. + int NavScoringDebugCount; // Metrics for debugging + int NavTabbingInputableRemaining; // >0 when counting items for tabbing ImGuiNavItemData NavMoveResultLocal; // Best move request candidate within NavWindow - ImGuiNavItemData NavMoveResultLocalVisibleSet; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) + ImGuiNavItemData NavMoveResultLocalVisible; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) ImGuiNavItemData NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) - ImGuiWindow* NavWrapRequestWindow; // Window which requested trying nav wrap-around. - ImGuiNavMoveFlags NavWrapRequestFlags; // Wrap-around operation flags. // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) ImGuiWindow* NavWindowingTarget; // Target window when doing CTRL+Tab (or Pad Menu + FocusPrev/Next), this window is temporarily displayed top-most! @@ -1554,9 +1600,7 @@ struct ImGuiContext // Legacy Focus/Tabbing system (older than Nav, active even if Nav is disabled, misnamed. FIXME-NAV: This needs a redesign!) ImGuiWindow* TabFocusRequestCurrWindow; // ImGuiWindow* TabFocusRequestNextWindow; // - int TabFocusRequestCurrCounterRegular; // Any item being requested for focus, stored as an index (we on layout to be stable between the frame pressing TAB and the next frame, semi-ouch) int TabFocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index - int TabFocusRequestNextCounterRegular; // Stored for next frame int TabFocusRequestNextCounterTabStop; // " bool TabFocusPressed; // Set in NewFrame() when user pressed Tab @@ -1598,14 +1642,14 @@ struct ImGuiContext ImVector ShrinkWidthBuffer; // Widget state - ImVec2 LastValidMousePos; + ImVec2 MouseLastValidPos; ImGuiInputTextState InputTextState; ImFont InputTextPasswordFont; ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets - float ColorEditLastHue; // Backup of last Hue associated to LastColor[3], so we can restore Hue in lossy RGB<>HSV round trips - float ColorEditLastSat; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips - float ColorEditLastColor[3]; + float ColorEditLastHue; // Backup of last Hue associated to LastColor, so we can restore Hue in lossy RGB<>HSV round trips + float ColorEditLastSat; // Backup of last Saturation associated to LastColor, so we can restore Saturation in lossy RGB<>HSV round trips + ImU32 ColorEditLastColor; // RGB value with alpha set to 0. ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. ImGuiComboPreviewData ComboPreviewData; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. @@ -1613,9 +1657,10 @@ struct ImGuiContext bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio - float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? - int TooltipOverrideCount; + float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() + short DisabledStackSize; + short TooltipOverrideCount; float TooltipSlowDelay; // Time before slow tooltips appears (FIXME: This is temporary until we merge in tooltip timer+priority work) ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once @@ -1650,8 +1695,9 @@ struct ImGuiContext // Debug Tools bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) - ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this id + ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this ID ImGuiMetricsConfig DebugMetricsConfig; + ImGuiStackTool DebugStackTool; // Misc float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. @@ -1676,7 +1722,6 @@ struct ImGuiContext WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; GcCompactAll = false; TestEngineHookItems = false; - TestEngineHookIdInfo = 0; TestEngine = NULL; WindowsActiveCount = 0; @@ -1687,6 +1732,7 @@ struct ImGuiContext WheelingWindow = NULL; WheelingWindowTimer = 0.0f; + DebugHookIdInfo = 0; HoveredId = HoveredIdPreviousFrame = 0; HoveredIdAllowOverlap = false; HoveredIdUsingMouseWheel = HoveredIdPreviousFrameUsingMouseWheel = false; @@ -1719,12 +1765,11 @@ struct ImGuiContext CurrentItemFlags = ImGuiItemFlags_None; NavWindow = NULL; - NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavInputId = 0; + NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavActivateInputId = 0; NavJustTabbedId = NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; + NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; NavJustMovedToKeyMods = ImGuiKeyModFlags_None; NavInputSource = ImGuiInputSource_None; - NavScoringRect = ImRect(); - NavScoringCount = 0; NavLayer = ImGuiNavLayer_Main; NavIdTabCounter = INT_MAX; NavIdIsAlive = false; @@ -1735,21 +1780,23 @@ struct ImGuiContext NavInitRequest = false; NavInitRequestFromMove = false; NavInitResultId = 0; - NavMoveRequest = false; - NavMoveRequestFlags = ImGuiNavMoveFlags_None; - NavMoveRequestForward = ImGuiNavForward_None; - NavMoveRequestKeyMods = ImGuiKeyModFlags_None; - NavMoveDir = NavMoveDirLast = NavMoveClipDir = ImGuiDir_None; - NavWrapRequestWindow = NULL; - NavWrapRequestFlags = ImGuiNavMoveFlags_None; + NavMoveSubmitted = false; + NavMoveScoringItems = false; + NavMoveForwardToNextFrame = false; + NavMoveFlags = ImGuiNavMoveFlags_None; + NavMoveScrollFlags = ImGuiScrollFlags_None; + NavMoveKeyMods = ImGuiKeyModFlags_None; + NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None; + NavScoringDebugCount = 0; + NavTabbingInputableRemaining = 0; NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; TabFocusRequestCurrWindow = TabFocusRequestNextWindow = NULL; - TabFocusRequestCurrCounterRegular = TabFocusRequestCurrCounterTabStop = INT_MAX; - TabFocusRequestNextCounterRegular = TabFocusRequestNextCounterTabStop = INT_MAX; + TabFocusRequestCurrCounterTabStop = INT_MAX; + TabFocusRequestNextCounterTabStop = INT_MAX; TabFocusPressed = false; DimBgRatio = 0.0f; @@ -1771,17 +1818,17 @@ struct ImGuiContext CurrentTableStackIdx = -1; CurrentTabBar = NULL; - LastValidMousePos = ImVec2(0.0f, 0.0f); TempInputId = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditLastHue = ColorEditLastSat = 0.0f; - ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX; + ColorEditLastColor = 0; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; DisabledAlphaBackup = 0.0f; + DisabledStackSize = 0; ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; TooltipSlowDelay = 0.50f; @@ -1856,7 +1903,6 @@ struct IMGUI_API ImGuiWindowTempData int CurrentTableIdx; // Current table index (into g.Tables) ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() - int FocusCounterRegular; // (Legacy Focus/Tabbing system) Sequential counter, start at -1 and increase as assigned via FocusableItemRegister() (FIXME-NAV: Needs redesign) int FocusCounterTabStop; // (Legacy Focus/Tabbing system) Same, but only count widgets which you can Tab through. // Local parameters stacks @@ -1865,7 +1911,6 @@ struct IMGUI_API ImGuiWindowTempData float TextWrapPos; // Current text wrap pos. ImVector ItemWidthStack; // Store item widths to restore (attention: .back() is not == ItemWidth) ImVector TextWrapPosStack; // Store text wrap pos to restore (attention: .back() is not == TextWrapPos) - ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting }; // Storage for one window @@ -1948,8 +1993,9 @@ struct IMGUI_API ImGuiWindow ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) ImDrawList DrawListInst; - ImGuiWindow* ParentWindow; // If we are a child _or_ popup window, this is pointing to our parent. Otherwise NULL. - ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window == Top-level window. + ImGuiWindow* ParentWindow; // If we are a child _or_ popup _or_ docked window, this is pointing to our parent. Otherwise NULL. + ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window. Doesn't cross through popups/dock nodes. + ImGuiWindow* RootWindowPopupTree; // Point to ourself or first ancestor that is not a child window. Cross through popups parent<>child. ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag. @@ -2335,7 +2381,7 @@ namespace ImGui IMGUI_API ImGuiWindow* FindWindowByName(const char* name); IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); IMGUI_API ImVec2 CalcWindowNextAutoFitSize(ImGuiWindow* window); - IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent); + IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, bool popup_hierarchy); IMGUI_API bool IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); @@ -2387,7 +2433,14 @@ namespace ImGui IMGUI_API void SetScrollY(ImGuiWindow* window, float scroll_y); IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio); IMGUI_API void SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio); - IMGUI_API ImVec2 ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& item_rect); + + // Early work-in-progress API (ScrollToItem() will become public) + IMGUI_API void ScrollToItem(ImGuiScrollFlags flags = 0); + IMGUI_API void ScrollToRect(ImGuiWindow* window, const ImRect& rect, ImGuiScrollFlags flags = 0); + IMGUI_API ImVec2 ScrollToRectEx(ImGuiWindow* window, const ImRect& rect, ImGuiScrollFlags flags = 0); +//#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + inline void ScrollToBringRectIntoView(ImGuiWindow* window, const ImRect& rect) { ScrollToRect(window, rect, ImGuiScrollFlags_KeepVisibleEdgeY); } +//#endif // Basic Accessors inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.LastItemData.ID; } // Get ID of last item (~~ often same ImGui::GetID(label) beforehand) @@ -2408,10 +2461,10 @@ namespace ImGui // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f); - IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemAddFlags flags = 0); + IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemFlags extra_flags = 0); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); - IMGUI_API void ItemFocusable(ImGuiWindow* window, ImGuiID id); - IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id, bool clip_even_when_logged); + IMGUI_API void ItemInputable(ImGuiWindow* window, ImGuiID id); + IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); IMGUI_API void PushMultiItemsWidths(int components, float width_full); @@ -2424,12 +2477,14 @@ namespace ImGui IMGUI_API void PopItemFlag(); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // Currently refactoring focus/nav/tabbing system // If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): - // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool focused = FocusableItemRegister(...)' - // (New) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' + // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' + // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' + // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || g.NavActivateInputId == id' (WIP) // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() - inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Focusable flag to ItemAdd() - inline IM_NORETURN void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem + inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() + inline void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem #endif // Logging/Capture @@ -2443,6 +2498,7 @@ namespace ImGui IMGUI_API void OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags = ImGuiPopupFlags_None); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); + IMGUI_API void ClosePopupsExceptModals(); IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); @@ -2450,9 +2506,10 @@ namespace ImGui IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); - IMGUI_API bool BeginViewportSideBar(const char* name, ImGuiViewport* viewport, ImGuiDir dir, float size, ImGuiWindowFlags window_flags); // Menus + IMGUI_API bool BeginViewportSideBar(const char* name, ImGuiViewport* viewport, ImGuiDir dir, float size, ImGuiWindowFlags window_flags); + IMGUI_API bool BeginMenuEx(const char* label, const char* icon, bool enabled = true); IMGUI_API bool MenuItemEx(const char* label, const char* icon, const char* shortcut = NULL, bool selected = false, bool enabled = true); // Combos @@ -2462,9 +2519,13 @@ namespace ImGui // Gamepad/Keyboard Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); + IMGUI_API void NavInitRequestApplyResult(); IMGUI_API bool NavMoveRequestButNoResultYet(); + IMGUI_API void NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); + IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); + IMGUI_API void NavMoveRequestResolveWithLastItem(); IMGUI_API void NavMoveRequestCancel(); - IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, const ImRect& bb_rel, ImGuiNavMoveFlags move_flags); + IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); IMGUI_API float GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode); IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); @@ -2673,10 +2734,12 @@ namespace ImGui // Debug Tools IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); + IMGUI_API void ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, col); } inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); + IMGUI_API void DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end); IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); @@ -2722,14 +2785,12 @@ IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table #ifdef IMGUI_ENABLE_TEST_ENGINE extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); -extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id); -extern void ImGuiTestEngineHook_IdInfo(ImGuiContext* ctx, ImGuiDataType data_type, ImGuiID id, const void* data_id, const void* data_id_end); extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); +extern const char* ImGuiTestEngine_FindItemDebugLabel(ImGuiContext* ctx, ImGuiID id); + #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) #define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log -#define IMGUI_TEST_ENGINE_ID_INFO(_ID,_TYPE,_DATA) if (g.TestEngineHookIdInfo == _ID) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA)); -#define IMGUI_TEST_ENGINE_ID_INFO2(_ID,_TYPE,_DATA,_DATA2) if (g.TestEngineHookIdInfo == _ID) ImGuiTestEngineHook_IdInfo(&g, _TYPE, _ID, (const void*)(_DATA), (const void*)(_DATA2)); #else #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)0) #endif diff --git a/imgui/imgui_tables.cpp b/imgui/imgui_tables.cpp index 71ac00f6..56056ae3 100644 --- a/imgui/imgui_tables.cpp +++ b/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (tables and columns code) /* @@ -324,7 +324,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG const ImVec2 avail_size = GetContentRegionAvail(); ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size); - if (use_child_window && IsClippedEx(outer_rect, 0, false)) + if (use_child_window && IsClippedEx(outer_rect, 0)) { ItemSize(outer_rect); return false; @@ -564,6 +564,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables) // + 1 (for table->RawData allocated below) // + 1 (for table->ColumnsNames, if names are used) +// Shared allocations per number of nested tables // + 1 (for table->Splitter._Channels) // + 2 * active_channels_count (for ImDrawCmd and ImDrawIdx buffers inside channels) // Where active_channels_count is variable but often == columns_count or columns_count + 1, see TableSetupDrawChannels() for details. @@ -1170,7 +1171,7 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) KeepAliveID(column_id); bool hovered = false, held = false; - bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick); + bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_NoNavFocus); if (pressed && IsMouseDoubleClicked(0)) { TableSetColumnWidthAutoSingle(table, column_n); @@ -1479,6 +1480,7 @@ void ImGui::TableSetupScrollFreeze(int columns, int rows) table->IsUnfrozenRows = (table->FreezeRowsCount == 0); // Make sure this is set before TableUpdateLayout() so ImGuiListClipper can benefit from it.b // Ensure frozen columns are ordered in their section. We still allow multiple frozen columns to be reordered. + // FIXME-TABLE: This work for preserving 2143 into 21|43. How about 4321 turning into 21|43? (preserve relative order in each section) for (int column_n = 0; column_n < table->FreezeColumnsRequest; column_n++) { int order_n = table->DisplayOrderToIndex[column_n]; @@ -3987,7 +3989,7 @@ void ImGui::EndColumns() const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); KeepAliveID(column_id); - if (IsClippedEx(column_hit_rect, column_id, false)) + if (IsClippedEx(column_hit_rect, column_id)) // FIXME: Can be removed or replaced with a lower-level test continue; bool hovered = false, held = false; diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index e3ac15f1..7da069b6 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.84 +// dear imgui, v1.85 // (widgets code) /* @@ -152,9 +152,13 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; - ImGuiContext& g = *GImGui; - IM_ASSERT(text != NULL); + + // Accept null ranges + if (text == text_end) + text = text_end = ""; + + // Calculate length const char* text_begin = text; if (text_end == NULL) text_end = text + strlen(text); // FIXME-OPT @@ -201,7 +205,7 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) ImRect line_rect(pos, pos + ImVec2(FLT_MAX, line_height)); while (line < text_end) { - if (IsClippedEx(line_rect, 0, false)) + if (IsClippedEx(line_rect, 0)) break; const char* line_end = (const char*)memchr(line, '\n', text_end - line); @@ -269,6 +273,7 @@ void ImGui::TextV(const char* fmt, va_list args) if (window->SkipItems) return; + // FIXME-OPT: Handle the %s shortcut? ImGuiContext& g = *GImGui; const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); @@ -566,6 +571,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool ClearActiveID(); else SetActiveID(id, window); // Hold on ID + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); g.ActiveIdMouseButton = mouse_button_clicked; FocusWindow(window); } @@ -576,6 +583,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool const bool has_repeated_at_least_once = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; if (!has_repeated_at_least_once) pressed = true; + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); ClearActiveID(); } @@ -600,13 +609,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool bool nav_activated_by_code = (g.NavActivateId == id); bool nav_activated_by_inputs = IsNavInputTest(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); if (nav_activated_by_code || nav_activated_by_inputs) - pressed = true; - if (nav_activated_by_code || nav_activated_by_inputs || g.ActiveId == id) { // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. - g.NavActivateId = id; // This is so SetActiveId assign a Nav source + pressed = true; SetActiveID(id, window); - if ((nav_activated_by_code || nav_activated_by_inputs) && !(flags & ImGuiButtonFlags_NoNavFocus)) + g.ActiveIdSource = ImGuiInputSource_Nav; + if (!(flags & ImGuiButtonFlags_NoNavFocus)) SetFocusID(id, window); } } @@ -1577,7 +1585,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF bool pressed = ButtonBehavior(bb, id, &hovered, &held); const ImGuiID popup_id = ImHashStr("##ComboPopup", 0, id); bool popup_open = IsPopupOpen(popup_id, ImGuiPopupFlags_None); - if ((pressed || g.NavActivateId == id) && !popup_open) + if (pressed && !popup_open) { OpenPopupEx(popup_id, ImGuiPopupFlags_None); popup_open = true; @@ -1791,7 +1799,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi bool value_changed = false; for (int i = 0; i < items_count; i++) { - PushID((void*)(intptr_t)i); + PushID(i); const bool item_selected = (i == *current_item); const char* item_text; if (!items_getter(data, i, &item_text)) @@ -2385,7 +2393,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemAddFlags_Focusable : 0)) + if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemFlags_Inputable : 0)) return false; // Default format string when passing NULL @@ -2399,24 +2407,26 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0; + const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = (hovered && g.IO.MouseClicked[0]); const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); - if (focus_requested || clicked || double_clicked || g.NavActivateId == id || g.NavInputId == id) + if (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id || g.NavActivateInputId == id) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavInputId == id)) - temp_input_is_active = true; + if (temp_input_allowed) + if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavActivateInputId == id) + temp_input_is_active = true; } + // Experimental: simple click (without moving) turns Drag into an InputText - // FIXME: Currently polling ImGuiConfigFlags_IsTouchScreen, may either poll an hypothetical ImGuiBackendFlags_HasKeyboard and/or an explicit drag settings. if (g.IO.ConfigDragClickToInputText && temp_input_allowed && !temp_input_is_active) if (g.ActiveId == id && hovered && g.IO.MouseReleased[0] && !IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR)) { - g.NavInputId = id; + g.NavActivateId = g.NavActivateInputId = id; + g.NavActivateFlags = ImGuiActivateFlags_PreferInput; temp_input_is_active = true; } } @@ -3001,7 +3011,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0; ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemAddFlags_Focusable : 0)) + if (!ItemAdd(total_bb, id, &frame_bb, temp_input_allowed ? ImGuiItemFlags_Inputable : 0)) return false; // Default format string when passing NULL @@ -3015,15 +3025,15 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { - const bool focus_requested = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0; + const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = (hovered && g.IO.MouseClicked[0]); - if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id) + if (input_requested_by_tabbing || clicked || g.NavActivateId == id || g.NavActivateInputId == id) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id)) + if (temp_input_allowed && (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || g.NavActivateInputId == id)) temp_input_is_active = true; } } @@ -3175,7 +3185,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d format = PatchFormatStringFloatToInt(format); const bool hovered = ItemHoverable(frame_bb, id); - if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavInputId == id) + if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavActivateInputId == id) { SetActiveID(id, window); SetFocusID(id, window); @@ -3439,7 +3449,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data style.FramePadding.x = style.FramePadding.y; ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups; if (flags & ImGuiInputTextFlags_ReadOnly) - BeginDisabled(true); + BeginDisabled(); SameLine(0, style.ItemInnerSpacing.x); if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) { @@ -3969,15 +3979,18 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiItemStatusFlags item_status_flags = 0; if (is_multiline) { - if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemAddFlags_Focusable)) + ImVec2 backup_pos = window->DC.CursorPos; + ItemSize(total_bb, style.FramePadding.y); + if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemFlags_Inputable)) { - ItemSize(total_bb, style.FramePadding.y); EndGroup(); return false; } item_status_flags = g.LastItemData.StatusFlags; + window->DC.CursorPos = backup_pos; // We reproduce the contents of BeginChildFrame() in order to provide 'label' so our window internal data are easier to read/debug. + // FIXME-NAV: Pressing NavActivate will trigger general child activation right before triggering our own below. Harmless but bizarre. PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); @@ -4000,7 +4013,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Support for internal ImGuiInputTextFlags_MergedItem flag, which could be redesigned as an ItemFlags if needed (with test performed in ItemAdd) ItemSize(total_bb, style.FramePadding.y); if (!(flags & ImGuiInputTextFlags_MergedItem)) - if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemAddFlags_Focusable)) + if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemFlags_Inputable)) return false; item_status_flags = g.LastItemData.StatusFlags; } @@ -4011,21 +4024,19 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We are only allowed to access the state if we are already the active widget. ImGuiInputTextState* state = GetInputTextState(id); - const bool focus_requested_by_code = (item_status_flags & ImGuiItemStatusFlags_FocusedByCode) != 0; - const bool focus_requested_by_tabbing = (item_status_flags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; + const bool input_requested_by_tabbing = (item_status_flags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; + const bool input_requested_by_nav = (g.ActiveId != id) && ((g.NavActivateInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_Keyboard)); const bool user_clicked = hovered && io.MouseClicked[0]; - const bool user_nav_input_start = (g.ActiveId != id) && ((g.NavInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_Keyboard)); const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); const bool user_scroll_active = is_multiline && state != NULL && g.ActiveId == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); - bool clear_active_id = false; - bool select_all = (g.ActiveId != id) && ((flags & ImGuiInputTextFlags_AutoSelectAll) != 0 || user_nav_input_start) && (!is_multiline); + bool select_all = false; float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); - const bool init_make_active = (user_clicked || user_scroll_finish || user_nav_input_start || focus_requested_by_code || focus_requested_by_tabbing); + const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav || input_requested_by_tabbing); const bool init_state = (init_make_active || user_scroll_active); if ((init_state && g.ActiveId != id) || init_changed_specs) { @@ -4061,13 +4072,20 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->ID = id; state->ScrollX = 0.0f; stb_textedit_initialize_state(&state->Stb, !is_multiline); - if (!is_multiline && focus_requested_by_code) + } + + if (!is_multiline) + { + if (flags & ImGuiInputTextFlags_AutoSelectAll) + select_all = true; + if (input_requested_by_nav && (!recycle_state || !(g.NavActivateFlags & ImGuiActivateFlags_TryToPreserveState))) + select_all = true; + if (input_requested_by_tabbing || (user_clicked && io.KeyCtrl)) select_all = true; } + if (flags & ImGuiInputTextFlags_AlwaysOverwrite) state->Stb.insert_mode = 1; // stb field name is indeed incorrect (see #2863) - if (!is_multiline && (focus_requested_by_tabbing || (user_clicked && io.KeyCtrl))) - select_all = true; } if (g.ActiveId != id && init_make_active) @@ -4100,7 +4118,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Lock the decision of whether we are going to take the path displaying the cursor or selection const bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active); - bool render_selection = state && state->HasSelection() && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); + bool render_selection = state && (state->HasSelection() || select_all) && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); bool value_changed = false; bool enter_pressed = false; @@ -4201,7 +4219,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters. if (io.InputQueueCharacters.Size > 0) { - if (!ignore_char_inputs && !is_readonly && !user_nav_input_start) + if (!ignore_char_inputs && !is_readonly && !input_requested_by_nav) for (int n = 0; n < io.InputQueueCharacters.Size; n++) { // Insert character if they pass filtering @@ -4242,6 +4260,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const bool is_undo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Z)) && !is_readonly && is_undoable); const bool is_redo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Y)) || (is_osx_shift_shortcut && IsKeyPressedMap(ImGuiKey_Z))) && !is_readonly && is_undoable; + // We allow validate/cancel with Nav source (gamepad) to makes it easier to undo an accidental NavInput press with no keyboard wired, but otherwise it isn't very useful. + const bool is_validate_enter = IsKeyPressedMap(ImGuiKey_Enter) || IsKeyPressedMap(ImGuiKey_KeyPadEnter); + const bool is_validate_nav = (IsNavInputTest(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed) && !IsKeyPressedMap(ImGuiKey_Space)) || IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed); + const bool is_cancel = IsKeyPressedMap(ImGuiKey_Escape) || IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed); + if (IsKeyPressedMap(ImGuiKey_LeftArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); } else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } else if (IsKeyPressedMap(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } @@ -4262,7 +4285,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_Enter) || IsKeyPressedMap(ImGuiKey_KeyPadEnter)) + else if (is_validate_enter) { bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0; if (!is_multiline || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl)) @@ -4276,7 +4299,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->OnKeyPressed((int)c); } } - else if (IsKeyPressedMap(ImGuiKey_Escape)) + else if (is_validate_nav) + { + IM_ASSERT(!is_validate_enter); + enter_pressed = clear_active_id = true; + } + else if (is_cancel) { clear_active_id = cancel_edit = true; } @@ -4758,6 +4786,30 @@ bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flag return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha); } +// ColorEdit supports RGB and HSV inputs. In case of RGB input resulting color may have undefined hue and/or saturation. +// Since widget displays both RGB and HSV values we must preserve hue and saturation to prevent these values resetting. +static void ColorEditRestoreHS(const float* col, float* H, float* S, float* V) +{ + // This check is optional. Suppose we have two color widgets side by side, both widgets display different colors, but both colors have hue and/or saturation undefined. + // With color check: hue/saturation is preserved in one widget. Editing color in one widget would reset hue/saturation in another one. + // Without color check: common hue/saturation would be displayed in all widgets that have hue/saturation undefined. + // g.ColorEditLastColor is stored as ImU32 RGB value: this essentially gives us color equality check with reduced precision. + // Tiny external color changes would not be detected and this check would still pass. This is OK, since we only restore hue/saturation _only_ if they are undefined, + // therefore this change flipping hue/saturation from undefined to a very tiny value would still be represented in color picker. + ImGuiContext& g = *GImGui; + if (g.ColorEditLastColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) + return; + + // When S == 0, H is undefined. + // When H == 1 it wraps around to 0. + if (*S == 0.0f || (*H == 0.0f && g.ColorEditLastHue == 1)) + *H = g.ColorEditLastHue; + + // When V == 0, S is undefined. + if (*V == 0.0f) + *S = g.ColorEditLastSat; +} + // Edit colors components (each component in 0.0f..1.0f range). // See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set. // With typical options: Left-click on color square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item. @@ -4813,13 +4865,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag { // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); - if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) - { - if (f[1] == 0) - f[0] = g.ColorEditLastHue; - if (f[2] == 0) - f[1] = g.ColorEditLastSat; - } + ColorEditRestoreHS(col, &f[0], &f[1], &f[2]); } int i[4] = { IM_F32_TO_INT8_UNBOUND(f[0]), IM_F32_TO_INT8_UNBOUND(f[1]), IM_F32_TO_INT8_UNBOUND(f[2]), IM_F32_TO_INT8_UNBOUND(f[3]) }; @@ -4954,7 +5000,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag g.ColorEditLastHue = f[0]; g.ColorEditLastSat = f[1]; ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); - memcpy(g.ColorEditLastColor, f, sizeof(float) * 3); + g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(f[0], f[1], f[2], 0)); } if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV)) ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); @@ -5089,13 +5135,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(R, G, B, H, S, V); - if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) - { - if (S == 0) - H = g.ColorEditLastHue; - if (V == 0) - S = g.ColorEditLastSat; - } + ColorEditRestoreHS(col, &H, &S, &V); } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -5148,6 +5188,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size - 1)); V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); + + // Greatly reduces hue jitter and reset to 0 when hue == 255 and color is rapidly modified using SV square. + if (g.ColorEditLastColor == ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) + H = g.ColorEditLastHue; value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) @@ -5221,10 +5265,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { if (flags & ImGuiColorEditFlags_InputRGB) { - ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10 * 1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]); + ColorConvertHSVtoRGB(H, S, V, col[0], col[1], col[2]); g.ColorEditLastHue = H; g.ColorEditLastSat = S; - memcpy(g.ColorEditLastColor, col, sizeof(float) * 3); + g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)); } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -5278,13 +5322,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl G = col[1]; B = col[2]; ColorConvertRGBtoHSV(R, G, B, H, S, V); - if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately. - { - if (S == 0) - H = g.ColorEditLastHue; - if (V == 0) - S = g.ColorEditLastSat; - } + ColorEditRestoreHS(col, &H, &S, &V); // Fix local Hue as display below will use it immediately. } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -5891,12 +5929,12 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l toggled = true; } - if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Left && is_open) + if (g.NavId == id && g.NavMoveDir == ImGuiDir_Left && is_open) { toggled = true; NavMoveRequestCancel(); } - if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right && !is_open) // If there's something upcoming on the line we may want to give it the priority? + if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right && !is_open) // If there's something upcoming on the line we may want to give it the priority? { toggled = true; NavMoveRequestCancel(); @@ -5967,7 +6005,7 @@ void ImGui::TreePush(const char* str_id) ImGuiWindow* window = GetCurrentWindow(); Indent(); window->DC.TreeDepth++; - PushID(str_id ? str_id : "#TreePush"); + PushID(str_id); } void ImGui::TreePush(const void* ptr_id) @@ -5975,7 +6013,7 @@ void ImGui::TreePush(const void* ptr_id) ImGuiWindow* window = GetCurrentWindow(); Indent(); window->DC.TreeDepth++; - PushID(ptr_id ? ptr_id : (const void*)"#TreePush"); + PushID(ptr_id); } void ImGui::TreePushOverrideID(ImGuiID id) @@ -5984,7 +6022,7 @@ void ImGui::TreePushOverrideID(ImGuiID id) ImGuiWindow* window = g.CurrentWindow; Indent(); window->DC.TreeDepth++; - window->IDStack.push_back(id); + PushOverrideID(id); } void ImGui::TreePop() @@ -6138,20 +6176,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl window->ClipRect.Max.x = window->ParentWorkRect.Max.x; } - bool item_add; const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0; - if (disabled_item) - { - ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; - g.CurrentItemFlags |= ImGuiItemFlags_Disabled; - item_add = ItemAdd(bb, id); - g.CurrentItemFlags = backup_item_flags; - } - else - { - item_add = ItemAdd(bb, id); - } - + const bool item_add = ItemAdd(bb, id, NULL, disabled_item ? ImGuiItemFlags_Disabled : ImGuiItemFlags_None); if (span_all_columns) { window->ClipRect.Min.x = backup_clip_rect_min_x; @@ -6163,7 +6189,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const bool disabled_global = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; if (disabled_item && !disabled_global) // Only testing this as an optimization - BeginDisabled(true); + BeginDisabled(); // FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only, // which would be advantageous since most selectable are not selected. @@ -6200,7 +6226,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { - SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos)); + SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos)); // (bb == NavRect) g.NavDisableHighlight = true; } } @@ -6465,7 +6491,7 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get float v0 = values_getter(data, (0 + values_offset) % values_count); float t0 = 0.0f; ImVec2 tp0 = ImVec2( t0, 1.0f - ImSaturate((v0 - scale_min) * inv_scale) ); // Point in the normalized space of our target rectangle - float histogram_zero_line_t = (scale_min * scale_max < 0.0f) ? (-scale_min * inv_scale) : (scale_min < 0.0f ? 0.0f : 1.0f); // Where does the zero line stands + float histogram_zero_line_t = (scale_min * scale_max < 0.0f) ? (1 + scale_min * inv_scale) : (scale_min < 0.0f ? 0.0f : 1.0f); // Where does the zero line stands const ImU32 col_base = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLines : ImGuiCol_PlotHistogram); const ImU32 col_hovered = GetColorU32((plot_type == ImGuiPlotType_Lines) ? ImGuiCol_PlotLinesHovered : ImGuiCol_PlotHistogramHovered); @@ -6681,21 +6707,21 @@ void ImGui::EndMenuBar() // Nav: When a move request within one of our child menu failed, capture the request to navigate among our siblings. if (NavMoveRequestButNoResultYet() && (g.NavMoveDir == ImGuiDir_Left || g.NavMoveDir == ImGuiDir_Right) && (g.NavWindow->Flags & ImGuiWindowFlags_ChildMenu)) { + // Try to find out if the request is for one of our child menu ImGuiWindow* nav_earliest_child = g.NavWindow; while (nav_earliest_child->ParentWindow && (nav_earliest_child->ParentWindow->Flags & ImGuiWindowFlags_ChildMenu)) nav_earliest_child = nav_earliest_child->ParentWindow; - if (nav_earliest_child->ParentWindow == window && nav_earliest_child->DC.ParentLayoutType == ImGuiLayoutType_Horizontal && g.NavMoveRequestForward == ImGuiNavForward_None) + if (nav_earliest_child->ParentWindow == window && nav_earliest_child->DC.ParentLayoutType == ImGuiLayoutType_Horizontal && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) { // To do so we claim focus back, restore NavId and then process the movement request for yet another frame. - // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth the hassle/cost) + // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth bothering) const ImGuiNavLayer layer = ImGuiNavLayer_Menu; IM_ASSERT(window->DC.NavLayersActiveMaskNext & (1 << layer)); // Sanity check FocusWindow(window); SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. g.NavDisableMouseHover = g.NavMousePosDirty = true; - g.NavMoveRequestForward = ImGuiNavForward_ForwardQueued; - NavMoveRequestCancel(); + NavMoveRequestForward(g.NavMoveDir, g.NavMoveClipDir, g.NavMoveFlags, g.NavMoveScrollFlags); // Repeat } } @@ -6784,7 +6810,7 @@ void ImGui::EndMainMenuBar() End(); } -bool ImGui::BeginMenu(const char* label, bool enabled) +bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -6847,17 +6873,19 @@ bool ImGui::BeginMenu(const char* label, bool enabled) } else { - // Menu inside a menu + // Menu inside a regular/vertical menu // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f. // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); - float icon_w = 0.0f; // FIXME: This not currently exposed for BeginMenu() however you can call window->DC.MenuColumns.DeclColumns(w, 0, 0, 0) yourself + float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f; float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); pressed = Selectable("", menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); RenderText(text_pos, label); + if (icon_w > 0.0f) + RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); RenderArrow(window->DrawList, pos + ImVec2(offsets->OffsetMark + extra_w + g.FontSize * 0.30f, 0.0f), GetColorU32(ImGuiCol_Text), ImGuiDir_Right); } if (!enabled) @@ -6874,38 +6902,30 @@ bool ImGui::BeginMenu(const char* label, bool enabled) // Close menu when not hovering it anymore unless we are moving roughly in the direction of the menu // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers, so menus feels more reactive. bool moving_toward_other_child_menu = false; - ImGuiWindow* child_menu_window = (g.BeginPopupStack.Size < g.OpenPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].SourceWindow == window) ? g.OpenPopupStack[g.BeginPopupStack.Size].Window : NULL; if (g.HoveredWindow == window && child_menu_window != NULL && !(window->Flags & ImGuiWindowFlags_MenuBar)) { - // FIXME-DPI: Values should be derived from a master "scale" factor. + float ref_unit = g.FontSize; // FIXME-DPI ImRect next_window_rect = child_menu_window->Rect(); - ImVec2 ta = g.IO.MousePos - g.IO.MouseDelta; + ImVec2 ta = (g.IO.MousePos - g.IO.MouseDelta); ImVec2 tb = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR(); ImVec2 tc = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR(); - float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, 5.0f, 30.0f); // add a bit of extra slack. - ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues - tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -100.0f); // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus // FIXME: Multiply by fb_scale? - tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +100.0f); + float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, ref_unit * 0.5f, ref_unit * 2.5f); // add a bit of extra slack. + ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues (FIXME: ??) + tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -ref_unit * 8.0f); // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus // FIXME: Multiply by fb_scale? + tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +ref_unit * 8.0f); moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); - //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_within_opened_triangle ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] + //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_toward_other_child_menu ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] } - - // FIXME: Hovering a disabled BeginMenu or MenuItem won't close us if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu) want_close = true; - if (!menu_is_open && hovered && pressed) // Click to open + // Open + if (!menu_is_open && pressed) // Click/activate to open want_open = true; else if (!menu_is_open && hovered && !moving_toward_other_child_menu) // Hover to open want_open = true; - - if (g.NavActivateId == id) - { - want_close = menu_is_open; - want_open = !menu_is_open; - } - if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open + if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open { want_open = true; NavMoveRequestCancel(); @@ -6923,7 +6943,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled) { want_open = true; } - else if (g.NavId == id && g.NavMoveRequest && g.NavMoveDir == ImGuiDir_Down) // Nav-Down to open + else if (g.NavId == id && g.NavMoveDir == ImGuiDir_Down) // Nav-Down to open { want_open = true; NavMoveRequestCancel(); @@ -6962,6 +6982,11 @@ bool ImGui::BeginMenu(const char* label, bool enabled) return menu_is_open; } +bool ImGui::BeginMenu(const char* label, bool enabled) +{ + return BeginMenuEx(label, NULL, enabled); +} + void ImGui::EndMenu() { // Nav: When a left move request _within our child menu_ failed, close ourselves (the _parent_ menu). @@ -6969,11 +6994,12 @@ void ImGui::EndMenu() // However, it means that with the current code, a BeginMenu() from outside another menu or a menu-bar won't be closable with the Left direction. ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (g.NavWindow && g.NavWindow->ParentWindow == window && g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical) - { - ClosePopupToLevel(g.BeginPopupStack.Size, true); - NavMoveRequestCancel(); - } + if (g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical) + if (g.NavWindow && (g.NavWindow->RootWindowForNav->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->RootWindowForNav->ParentWindow == window) + { + ClosePopupToLevel(g.BeginPopupStack.Size, true); + NavMoveRequestCancel(); + } EndPopup(); } @@ -6994,7 +7020,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut bool pressed; PushID(label); if (!enabled) - BeginDisabled(true); + BeginDisabled(); const ImGuiSelectableFlags flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover; const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) @@ -7003,10 +7029,11 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut // Note that in this situation: we don't render the shortcut, we render a highlight instead of the selected tick mark. float w = label_size.x; window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); + ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); pressed = Selectable("", selected, flags, ImVec2(w, 0.0f)); PopStyleVar(); - RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); + RenderText(text_pos, label); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else @@ -7852,9 +7879,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); if (p_open && !*p_open) { - PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); - ItemAdd(ImRect(), id); - PopItemFlag(); + ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus); return false; } @@ -7921,9 +7946,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // and then gets submitted again, the tabs will have 'tab_appearing=true' but 'tab_is_new=false'. if (tab_appearing && (!tab_bar_appearing || tab_is_new)) { - PushItemFlag(ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus, true); - ItemAdd(ImRect(), id); - PopItemFlag(); + ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus); if (is_tab_button) return false; return tab_contents_visible; From 830c64f1bf1b17cd8efb10011712d6a76a60806f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Oct 2021 17:13:52 +0200 Subject: [PATCH 0563/1018] viewer: update default windows size to 1920x1080 --- include/gproshan/viewer/viewer.h | 2 +- src/viewer/viewer.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 12de4d4f..9367dce4 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -103,7 +103,7 @@ class viewer char status_message[1024] = {}; public: - viewer(int width = 1600, int height = 900); + viewer(const int & width = 1920, const int & height = 1080); virtual ~viewer(); bool run(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e3536f00..33688855 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -52,7 +52,7 @@ const std::vector viewer::colormap = { "vertex color", "set" }; -viewer::viewer(int width, int height): window_width(width), window_height(height) +viewer::viewer(const int & width, const int & height): window_width(width), window_height(height) { init_gl(); init_glsl(); From 2afc3a0d668ccedc2fb9423f1d3f89f66f82e54e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Oct 2021 17:54:14 +0200 Subject: [PATCH 0564/1018] geometry: connected components visualization (multiple meshes) --- src/app_viewer.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 39e75cb6..67045785 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -1,6 +1,7 @@ #include "app_viewer.h" #include +#include using namespace std; @@ -127,6 +128,36 @@ bool app_viewer::process_connected_components(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) label[v] = -1; + int nc = 0; + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + if(label[v] < 0) + { + ++nc; + + std::queue q; + q.push(v); + label[v] = nc; + + while(!q.empty()) + { + for_star(he, mesh, q.front()) + if(label[mesh->vt(he)] < 0) + { + label[mesh->vt(he)] = nc; + q.push(mesh->vt(he)); + } + q.pop(); + } + } + } + + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + label[v] /= nc + 1; + + mesh.update_vbo_heatmap(); + return false; } From 56993dfd274710ad8aa7d0512ec6e15b25136ac7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Oct 2021 12:31:40 +0200 Subject: [PATCH 0565/1018] app_viewer: adding connected componets visualization --- src/app_viewer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 67045785..72206bad 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -141,7 +141,10 @@ bool app_viewer::process_connected_components(viewer * p_view) while(!q.empty()) { - for_star(he, mesh, q.front()) + //for_star(he, mesh, q.front()) + link_t ll; + mesh->link(ll, q.front()); + for(const index_t & he: ll) if(label[mesh->vt(he)] < 0) { label[mesh->vt(he)] = nc; @@ -154,8 +157,9 @@ bool app_viewer::process_connected_components(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - label[v] /= nc + 1; + label[v] /= nc; + sprintf(view->status_message, "found %d connected components", nc); mesh.update_vbo_heatmap(); return false; @@ -374,6 +378,7 @@ bool app_viewer::process_geodesics(viewer * p_view) TIC(view->time) geodesics G(mesh, mesh.selected, params); TOC(view->time) + sprintf(view->status_message, "geodesics time: %.3fs", view->time); params.radio = G.radio(); From dbe27477de8e44715d28d47543e1563213878448 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Oct 2021 13:14:51 +0200 Subject: [PATCH 0566/1018] che: refactoring star and link access neighborhood methods --- include/gproshan/mesh/che.h | 7 ++---- src/app_viewer.cpp | 14 ++++++------ src/geodesics/dijkstra.cpp | 10 ++------- src/geodesics/geodesics.cpp | 14 +++++------- src/mdict/patch.cpp | 27 +++++------------------ src/mesh/che.cpp | 43 +++++++++++++++++-------------------- 6 files changed, 40 insertions(+), 75 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 06b53c37..e3433fb7 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -15,9 +15,6 @@ namespace gproshan { -typedef std::vector star_t; // star (vector of he) -typedef std::vector link_t; // link (vector of he) - size_t & rw(const size_t & n); index_t trig(const index_t & he); index_t next(const index_t & he); @@ -72,8 +69,8 @@ class che che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); virtual ~che(); - void star(star_t & s, const index_t & v) const; - void link(link_t & l, const index_t & v) const; + std::vector star(const index_t & v) const; + std::vector link(const index_t & v) const; std::vector bounds() const; std::vector boundary(const index_t & v) const; bool is_vertex_bound(const index_t & v) const; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 72206bad..214068a2 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -141,20 +141,18 @@ bool app_viewer::process_connected_components(viewer * p_view) while(!q.empty()) { - //for_star(he, mesh, q.front()) - link_t ll; - mesh->link(ll, q.front()); - for(const index_t & he: ll) - if(label[mesh->vt(he)] < 0) + for(const index_t & v: mesh->link(q.front())) + if(label[v] < 0) { - label[mesh->vt(he)] = nc; - q.push(mesh->vt(he)); + label[v] = nc; + q.push(v); } + q.pop(); } } } - + #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) label[v] /= nc; diff --git a/src/geodesics/dijkstra.cpp b/src/geodesics/dijkstra.cpp index 9f1a516a..6c275401 100644 --- a/src/geodesics/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -51,7 +51,7 @@ void dijkstra::print(ostream & os) void dijkstra::run(che * mesh) { bool * visited = new bool[n_vertices]; - memset(visited, 0, sizeof(bool)*n_vertices); + memset(visited, 0, sizeof(bool) * n_vertices); visited[source] = true; weights[source] = 0; @@ -68,17 +68,11 @@ void dijkstra::run(che * mesh) for(index_t v = 0; v < n_vertices; ++v) { real_t w; - index_t nv; if(!visited[v]) { - link_t link_he; - mesh->link(link_he, v); - - for(index_t he: link_he) + for(const index_t & nv: mesh->link(v)) { - nv = mesh->vt(next(he)); - if(visited[nv]) { w = weights[nv] + *(mesh->get_vertex(nv) - mesh->get_vertex(v)); diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 241cb78e..54e49fa1 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -130,7 +130,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co real_t dv, dp; vertex vx; - size_t black_i, v; + size_t black_i; index_t c = 0; n_sorted = 0; @@ -159,27 +159,23 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co if(fun && !fun(black_i)) break; - link_t black_link; - mesh->link(black_link, black_i); - for(const index_t & he: black_link) + for(const index_t & v: mesh->link(black_i)) { - v = mesh->vt(he); - if(color[v] == GREEN) color[v] = RED; if(color[v] == RED) { dv = dist[v]; - for_star(v_he, mesh, v) + for_star(he, mesh, v) { - dp = update_step(mesh, dist, v_he); + dp = update_step(mesh, dist, he); if(dp < dv) { dv = dp; if(clusters) - clusters[v] = dist[mesh->vt(prev(v_he))] < dist[mesh->vt(next(he))] ? clusters[mesh->vt(prev(he))] : clusters[mesh->vt(next(he))]; + clusters[v] = clusters[mesh->vt(prev(he))] ? clusters[mesh->vt(prev(he))] : clusters[mesh->vt(next(he))]; } } diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index 5e5e12ff..d4b035cd 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -524,7 +524,6 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl vertices.reserve(expected_nv); memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); - link_t link; toplevel[v] = 0; vertices.push_back(v); @@ -534,18 +533,12 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl if(toplevel[v] == n_toplevels) break; - mesh->link(link, v); - for(const index_t & he: link) - { - const index_t & u = mesh->vt(he); + for(const index_t & u: mesh->link(v)) if(toplevel[u] == NIL) { vertices.push_back(u); toplevel[u] = toplevel[v] + 1; } - } - - link.clear(); } } @@ -561,7 +554,6 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); a_vec p(3); - link_t link; toplevel[v] = 0; qvertices.push({0, v}); @@ -573,10 +565,8 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, vertices.push_back(v); - mesh->link(link, v); - for(const index_t & he: link) + for(const index_t & u: mesh->link(v)) { - const index_t & u = mesh->vt(he); if(toplevel[u] == NIL) { p(0) = mesh->gt(u).x; @@ -589,7 +579,6 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, if(norm(p) < radio) qvertices.push({-norm(p), u}); } - link.clear(); } } } @@ -717,19 +706,14 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons { avg_dist = INFINITY; vector distances; - link_t link; for(size_t i = 0; i < vertices.size(); ++i) { - const index_t & v = vertices[i]; - mesh->link(link, v); - for(const index_t & he: link) + for(const index_t & u: mesh->link(vertices[i])) { - const index_t & u = mesh->vt(he); - - for (auto itp:vpatches[u]) + for(auto itp: vpatches[u]) { - if( itp.first == p) + if(itp.first == p) { a_vec a = xyz.col(i); a_vec b = xyz.col(itp.second); @@ -740,7 +724,6 @@ void patch::compute_avg_distance(che * mesh, vector & vpatches, cons } } } - } /* for(size_t j = i+1; j < vertices.size(); ++j) // replace for 1 ring diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index a8a1dd0f..73e2bbc6 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -83,24 +83,30 @@ che::~che() free(); } -void che::star(star_t & s, const index_t & v) const +vector che::star(const index_t & v) const { - if(v >= n_vertices) return; + assert(v >= n_vertices); + vector vstar; for_star(he, this, v) - s.push_back(he); + vstar.push_back(he); + + return vstar; } -void che::link(link_t & l, const index_t & v) const +vector che::link(const index_t & v) const { - if(v >= n_vertices) return; + assert(v >= n_vertices); + + vector vlink; + + if(is_vertex_bound(v)) + vlink.push_back(VT[next(EVT[v])]); for_star(he, this, v) - { - l.push_back(next(he)); - if(OT[prev(he)] == NIL) - l.push_back(prev(he)); - } + vlink.push_back(VT[prev(he)]); + + return vlink; } ///< return a vector of indices of one vertex per boundary @@ -652,12 +658,8 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector Date: Tue, 19 Oct 2021 18:32:56 +0200 Subject: [PATCH 0567/1018] updating CMakeLists.txt --- .github/workflows/build.yml | 8 -------- CMakeLists.txt | 11 +++-------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 19dcb0d1..3972fc20 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,18 +47,11 @@ jobs: sudo apt -y install cuda - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands run: cmake -E make_directory ${{runner.workspace}}/build - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system shell: bash working-directory: ${{runner.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 run: | export PATH=${CUDA_BIN}:${PATH} cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} @@ -66,6 +59,5 @@ jobs: - name: Build working-directory: ${{runner.workspace}}/build shell: bash - # Execute the build. You can specify a specific target with "--target " run: cmake --build . --config ${{ matrix.config }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 51aed840..1db6c1b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,8 @@ add_compile_options(-Wall -Wextra -Wno-unused-result) add_definitions(-DGPROSHAN_FLOAT) add_definitions(-DGPROSHAN_LOG) -find_package(CUDAToolkit 11.3) + +find_package(CUDAToolkit 11.4) if(CUDAToolkit_FOUND) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 17) @@ -36,7 +37,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -find_package(embree 3.12) +find_package(embree 3.13) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) include_directories(SYSTEM ${embree_INCLUDE_DIRS}) @@ -108,9 +109,3 @@ endif(CUDAToolkit_FOUND) add_subdirectory(apps) - -install(TARGETS gproshan imgui test_gproshan test_geodesics test_image_denoising - ARCHIVE DESTINATION ${gproshan_SOURCE_DIR}/lib - LIBRARY DESTINATION ${gproshan_SOURCE_DIR}/lib - RUNTIME DESTINATION ${gproshan_SOURCE_DIR}/bin) - From 5f652894bc9d2e7a9d1ae286d2950a1db202af29 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Oct 2021 18:32:56 +0200 Subject: [PATCH 0568/1018] updating CMakeLists.txt, remove install --- .github/workflows/build.yml | 8 -------- CMakeLists.txt | 11 +++-------- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 19dcb0d1..3972fc20 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,18 +47,11 @@ jobs: sudo apt -y install cuda - name: Create Build Environment - # Some projects don't allow in-source building, so create a separate build directory - # We'll use this as our working directory for all subsequent commands run: cmake -E make_directory ${{runner.workspace}}/build - name: Configure CMake - # Use a bash shell so we can use the same syntax for environment variable - # access regardless of the host operating system shell: bash working-directory: ${{runner.workspace}}/build - # Note the current convention is to use the -S and -B options here to specify source - # and build directories, but this is only available with CMake 3.13 and higher. - # The CMake binaries on the Github Actions machines are (as of this writing) 3.12 run: | export PATH=${CUDA_BIN}:${PATH} cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} @@ -66,6 +59,5 @@ jobs: - name: Build working-directory: ${{runner.workspace}}/build shell: bash - # Execute the build. You can specify a specific target with "--target " run: cmake --build . --config ${{ matrix.config }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 51aed840..1db6c1b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,8 @@ add_compile_options(-Wall -Wextra -Wno-unused-result) add_definitions(-DGPROSHAN_FLOAT) add_definitions(-DGPROSHAN_LOG) -find_package(CUDAToolkit 11.3) + +find_package(CUDAToolkit 11.4) if(CUDAToolkit_FOUND) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 17) @@ -36,7 +37,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -find_package(embree 3.12) +find_package(embree 3.13) if(embree_FOUND) add_definitions(-DGPROSHAN_EMBREE) include_directories(SYSTEM ${embree_INCLUDE_DIRS}) @@ -108,9 +109,3 @@ endif(CUDAToolkit_FOUND) add_subdirectory(apps) - -install(TARGETS gproshan imgui test_gproshan test_geodesics test_image_denoising - ARCHIVE DESTINATION ${gproshan_SOURCE_DIR}/lib - LIBRARY DESTINATION ${gproshan_SOURCE_DIR}/lib - RUNTIME DESTINATION ${gproshan_SOURCE_DIR}/bin) - From 68c92042276ae1fa0ae3154ad1fc5caa3495b0d4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Oct 2021 17:43:50 +0200 Subject: [PATCH 0569/1018] viewer: moving the camera --- include/gproshan/viewer/camera.h | 6 +++--- src/viewer/camera.cpp | 6 ++---- src/viewer/viewer.cpp | 1 + 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 6f8992d0..203f5f63 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -19,9 +19,9 @@ class camera quaternion r_last = 1; public: - quaternion pos = vertex(0, 0, -2); - quaternion eye = vertex(0, 0, -2); - quaternion center = vertex(0, 0, 0); + quaternion eye; + quaternion pos = vertex(0, 0, -3); + quaternion front = vertex(0, 0, 1); quaternion up = vertex(0, 1, 0); public: diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 77efa92c..4871172f 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -14,11 +14,9 @@ namespace gproshan { glm::mat4 camera::look_at(const quaternion & r) { - eye = r.conj() * pos * r; + eye = r.conj() * (pos + front) * r; - return glm::lookAt( glm_vec3(eye), - glm_vec3(r.conj() * center * r), - glm_vec3(r.conj() * up * r)); + return glm::lookAt(glm_vec3(r.conj() * pos * r), glm_vec3(eye), glm_vec3(r.conj() * up * r)); } quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 33688855..e3e5ca91 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -173,6 +173,7 @@ bool viewer::run() ImGui::Text("%s", mesh->filename.c_str()); ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); + ImGui::DragFloat4("camera center", (float *) &cam.pos, 0.2); if(mesh.render_pointcloud) { From ac039fa9939dd2b80c4701bd9be484c9325a43b4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 23 Oct 2021 16:53:44 +0200 Subject: [PATCH 0570/1018] viewer: using glfwGetWindowContentScale --- src/viewer/viewer.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e3e5ca91..77e7ed0c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -901,9 +901,10 @@ void viewer::select_border_vertices(che_viewer & mesh) void viewer::pick_vertex(const real_t & x, const real_t & y) { - active_mesh().select( x * viewport_width / window_width, - y * viewport_height / window_height, - {viewport_width, viewport_height}, view_mat, proj_mat); + float xscale, yscale; + glfwGetWindowContentScale(window, &xscale, &yscale); + + active_mesh().select(x * xscale, y * yscale, {viewport_width, viewport_height}, view_mat, proj_mat); } From 2e8e10e9288b236f37891c51cfc18537fbbc2b84 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 30 Oct 2021 16:23:40 +0200 Subject: [PATCH 0571/1018] raytracing: fix rt_optix ptx compilation --- apps/CMakeLists.txt | 10 ++++++++-- src/raytracing/rt_optix.cpp | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 8ea3fc99..6626ac44 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -5,8 +5,14 @@ target_link_libraries(test_gproshan gproshan) if(OptiX_INCLUDE) add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - add_custom_target(ptx_cp_sources DEPENDS ptx_sources COMMAND ${CMAKE_COMMAND} -E copy $ $) - add_dependencies(test_gproshan ptx_cp_sources) + set_property(TARGET ptx_sources PROPERTY CUDA_ARCHITECTURES OFF) + target_compile_options(ptx_sources PRIVATE "--keep") + add_custom_target( ptx_tmp DEPENDS ptx_sources + COMMAND ${CMAKE_COMMAND} -E copy + ${gproshan_BINARY_DIR}/apps/rt_optix.ptx + ${gproshan_SOURCE_DIR}/tmp/rt_optix.ptx + ) + add_dependencies(test_gproshan ptx_tmp) endif(OptiX_INCLUDE) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index cb7f419d..29068549 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -54,7 +54,7 @@ optix::optix(const std::vector & meshes) optix_pipeline_link_opt.maxTraceDepth = 2; - std::ifstream ptx_is("rt_optix.ptx"); + std::ifstream ptx_is(tmp_file_path("rt_optix.ptx")); const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); From bac9e27edd69faf292734943ccf8c9daa5fa70d4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 13 Nov 2021 16:37:07 +0100 Subject: [PATCH 0572/1018] rt_optix: fix bug building acceleration structure --- include/gproshan/raytracing/rt_optix.h | 2 +- src/raytracing/rt_optix.cpp | 44 ++++++++++++++++++-------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 0e994d8c..09ff84b3 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -37,7 +37,7 @@ class optix : public raytracing float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); OptixTraversableHandle build_as(const std::vector & meshes); - void add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh); + void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 29068549..54953561 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -94,10 +94,11 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) OptixTraversableHandle optix_as_handle = {}; std::vector optix_meshes(meshes.size()); + std::vector optix_vertex_ptr(meshes.size()); std::vector optix_trig_flags(meshes.size()); for(index_t i = 0; i < meshes.size(); ++i) - add_mesh(optix_meshes[i], optix_trig_flags[i], meshes[i]); + add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i]); OptixAccelBuildOptions optix_accel_opt = {}; optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_NONE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION; @@ -105,13 +106,17 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; OptixAccelBufferSizes optix_gas_buffer_size; - optixAccelComputeMemoryUsage( optix_context, + OptixResult rab = optixAccelComputeMemoryUsage( optix_context, &optix_accel_opt, optix_meshes.data(), optix_meshes.size(), &optix_gas_buffer_size ); +gproshan_error_var(optixGetErrorName(rab)); +gproshan_error_var(optixGetErrorString(rab)); +gproshan_error_var(optix_gas_buffer_size.tempSizeInBytes); +gproshan_error_var(optix_gas_buffer_size.outputSizeInBytes); void * d_compacted_size; cudaMalloc(&d_compacted_size, sizeof(uint64_t)); @@ -125,14 +130,16 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) void * d_output_buffer; cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); - optixAccelBuild( optix_context, +gproshan_error_var(optix_meshes.size()); + + rab = optixAccelBuild( optix_context, 0, // stream &optix_accel_opt, optix_meshes.data(), optix_meshes.size(), - (CUdeviceptr) d_temp_buffer, + (CUdeviceptr) (uintptr_t) d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes, - (CUdeviceptr) d_output_buffer, + (CUdeviceptr) (uintptr_t) d_output_buffer, optix_gas_buffer_size.outputSizeInBytes, &optix_as_handle, &optix_emit_desc, @@ -140,9 +147,13 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) ); cudaDeviceSynchronize(); - -gproshan_error_var(optix_gas_buffer_size.tempSizeInBytes); -gproshan_error_var(optix_gas_buffer_size.outputSizeInBytes); +cudaError_t error = cudaGetLastError(); \ + if( error != cudaSuccess ) \ + { \ + fprintf( stderr, "error (%s: line %d): %s\n", __FILE__, __LINE__, cudaGetErrorString( error ) ); \ + } +gproshan_error_var(optixGetErrorName(rab)); +gproshan_error_var(optixGetErrorString(rab)); uint64_t compacted_size; cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); @@ -152,7 +163,7 @@ gproshan_error_var(compacted_size); void * d_as; cudaMalloc(&d_as, compacted_size); - optixAccelCompact( optix_context, + rab = optixAccelCompact( optix_context, 0, // stream optix_as_handle, (CUdeviceptr) d_as, @@ -162,6 +173,8 @@ gproshan_error_var(compacted_size); cudaDeviceSynchronize(); gproshan_error_var(compacted_size); +gproshan_error_var(optixGetErrorName(rab)); +gproshan_error_var(optixGetErrorString(rab)); cudaFree(d_output_buffer); cudaFree(d_temp_buffer); @@ -170,10 +183,10 @@ gproshan_error_var(compacted_size); return optix_as_handle; } -void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, const che * mesh) +void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh) { - void * d_vertex; - void * d_index; + void * d_vertex = nullptr; + void * d_index = nullptr; #ifdef GPROSHAN_FLOAT @@ -195,13 +208,15 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, cudaMalloc(&d_index, mesh->n_half_edges * sizeof(index_t)); cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t), cudaMemcpyHostToDevice); + d_vertex_ptr = (CUdeviceptr) d_vertex; + optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); optix_mesh.triangleArray.numVertices = mesh->n_vertices; - optix_mesh.triangleArray.vertexBuffers = (CUdeviceptr *) &d_vertex; + optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); @@ -215,6 +230,9 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, uint32_t & optix_trig_flags, optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; + +gproshan_error_var(sizeof(float3) == 3 * sizeof(float)); +gproshan_error_var(sizeof(float3) == sizeof(vertex)); } glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat) From 1c16a005b7f6f349ca6e304f0f7c6bb973e92d84 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 13 Nov 2021 16:54:37 +0100 Subject: [PATCH 0573/1018] rt_optix: cleaning up code, black render frame --- src/raytracing/rt_optix.cpp | 32 ++++++-------------------------- src/viewer/viewer.cpp | 3 +++ 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 54953561..c2e3471b 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -106,17 +106,14 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; OptixAccelBufferSizes optix_gas_buffer_size; - OptixResult rab = optixAccelComputeMemoryUsage( optix_context, + optixAccelComputeMemoryUsage( optix_context, &optix_accel_opt, optix_meshes.data(), optix_meshes.size(), &optix_gas_buffer_size ); -gproshan_error_var(optixGetErrorName(rab)); -gproshan_error_var(optixGetErrorString(rab)); -gproshan_error_var(optix_gas_buffer_size.tempSizeInBytes); -gproshan_error_var(optix_gas_buffer_size.outputSizeInBytes); + void * d_compacted_size; cudaMalloc(&d_compacted_size, sizeof(uint64_t)); @@ -130,16 +127,15 @@ gproshan_error_var(optix_gas_buffer_size.outputSizeInBytes); void * d_output_buffer; cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); -gproshan_error_var(optix_meshes.size()); - rab = optixAccelBuild( optix_context, + optixAccelBuild( optix_context, 0, // stream &optix_accel_opt, optix_meshes.data(), optix_meshes.size(), - (CUdeviceptr) (uintptr_t) d_temp_buffer, + (CUdeviceptr) d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes, - (CUdeviceptr) (uintptr_t) d_output_buffer, + (CUdeviceptr) d_output_buffer, optix_gas_buffer_size.outputSizeInBytes, &optix_as_handle, &optix_emit_desc, @@ -147,23 +143,14 @@ gproshan_error_var(optix_meshes.size()); ); cudaDeviceSynchronize(); -cudaError_t error = cudaGetLastError(); \ - if( error != cudaSuccess ) \ - { \ - fprintf( stderr, "error (%s: line %d): %s\n", __FILE__, __LINE__, cudaGetErrorString( error ) ); \ - } -gproshan_error_var(optixGetErrorName(rab)); -gproshan_error_var(optixGetErrorString(rab)); uint64_t compacted_size; cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); -gproshan_error_var(compacted_size); - void * d_as; cudaMalloc(&d_as, compacted_size); - rab = optixAccelCompact( optix_context, + optixAccelCompact( optix_context, 0, // stream optix_as_handle, (CUdeviceptr) d_as, @@ -172,9 +159,6 @@ gproshan_error_var(compacted_size); ); cudaDeviceSynchronize(); -gproshan_error_var(compacted_size); -gproshan_error_var(optixGetErrorName(rab)); -gproshan_error_var(optixGetErrorString(rab)); cudaFree(d_output_buffer); cudaFree(d_temp_buffer); @@ -188,7 +172,6 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u void * d_vertex = nullptr; void * d_index = nullptr; - #ifdef GPROSHAN_FLOAT cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(vertex)); cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); @@ -230,9 +213,6 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; - -gproshan_error_var(sizeof(float3) == 3 * sizeof(float)); -gproshan_error_var(sizeof(float3) == sizeof(vertex)); } glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 77e7ed0c..405b4ffb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -868,6 +868,9 @@ void viewer::render_optix() rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, action); + if(!render_frame) + render_frame = new frame; + action = false; render_frame->display(viewport_width, viewport_height, rt_optix->img); } From 82989985b443a534de65d9c8f280fd6a4bf63bfd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 14:36:26 +0100 Subject: [PATCH 0574/1018] gproshan: log formatting --- include/gproshan/include.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 328a4bd4..7b029891 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -36,12 +36,12 @@ typedef unsigned int index_t; #define gproshan_input(input) fprintf(stderr, "\033[38;5;210m[INPUT] %s: \033[0m", #input) -#define gproshan_error_var(vari) std::cerr << "\033[0;33m[ERROR] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << "\t\033[38;5;210m" << __FILE__ ":" << __LINE__ << " > " << __FUNCTION__ << "\033[0m" << std::endl -#define gproshan_error(message) fprintf(stderr, "\033[1;31m[ERROR] %s: %s (%s:%d)\n\033[0m", #message, __FUNCTION__, __FILE__, __LINE__) +#define gproshan_error_var(vari) std::cerr << "\033[1;31m[ERROR] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << "\t\033[38;5;210m" << __FILE__ ":" << __LINE__ << " > " << __FUNCTION__ << "\033[0m" << std::endl +#define gproshan_error(message) fprintf(stderr, "\033[1;31m[ERROR]\033[0m %s\033[38;5;210m\t%s:%d > %s\n\033[0m", #message, __FILE__, __LINE__, __FUNCTION__) #ifndef NDEBUG - #define gproshan_debug_var(vari) std::cerr << "\033[0;33m[DEBUG] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << "\t\033[38;5;210m" << __FILE__ ":" << __LINE__ << " > " << __FUNCTION__ << "\033[0m" << std::endl - #define gproshan_debug(message) fprintf(stderr, "\033[1;31m[DEBUG] %s: %s (%s:%d)\n\033[0m", #message, __FUNCTION__, __FILE__, __LINE__) + #define gproshan_debug_var(vari) std::cerr << "\033[1;31m[DEBUG] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << "\t\033[38;5;210m" << __FILE__ ":" << __LINE__ << " > " << __FUNCTION__ << "\033[0m" << std::endl + #define gproshan_debug(message) fprintf(stderr, "\033[1;31m[DEBUG]\033[0m %s\033[38;5;210m\t%s:%d > %s\n\033[0m", #message, __FILE__, __LINE__, __FUNCTION__) #else #define gproshan_debug_var(vari) #define gproshan_debug(message) From ef0f867963de5256c18d1d0bf4ac44617535d781 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 15:54:03 +0100 Subject: [PATCH 0575/1018] rt_optix: creating ray programs --- include/gproshan/geodesics/vertex.cuh | 10 - include/gproshan/raytracing/rt_optix.h | 8 +- include/gproshan/raytracing/rt_optix_params.h | 32 +++ src/raytracing/rt_optix.cpp | 131 ++++++++++- src/raytracing/rt_optix.cu | 217 +++++++++++++++++- 5 files changed, 378 insertions(+), 20 deletions(-) create mode 100644 include/gproshan/raytracing/rt_optix_params.h diff --git a/include/gproshan/geodesics/vertex.cuh b/include/gproshan/geodesics/vertex.cuh index 1ba9338c..27fdfaf6 100644 --- a/include/gproshan/geodesics/vertex.cuh +++ b/include/gproshan/geodesics/vertex.cuh @@ -30,19 +30,12 @@ struct vertex_cu } - - /** - doc product - */ __host__ __device__ vertex_cu operator*(const vertex_cu & v) const { return vertex_cu(y * v.z - z * v.y, -(x * v.z - z * v.x), x * v.y - y * v.x); } - /** - Norma - */ __host__ __device__ real_t operator*() { @@ -63,9 +56,6 @@ struct vertex_cu z /= v; } - /** - cross product - */ __host__ __device__ real_t operator,(const vertex_cu & v) const { diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 09ff84b3..7e269b24 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -24,7 +24,10 @@ class optix : public raytracing CUstream stream; OptixDeviceContext optix_context; - + OptixModule optix_module; + OptixProgramGroup raygen_programs[1]; + OptixProgramGroup miss_programs[2]; + OptixProgramGroup hitgroup_programs[2]; public: optix(const std::vector & meshes); @@ -36,6 +39,9 @@ class optix : public raytracing glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + void create_raygen_programs(); + void create_miss_programs(); + void create_hitgroup_programs(); OptixTraversableHandle build_as(const std::vector & meshes); void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h new file mode 100644 index 00000000..fad94ce9 --- /dev/null +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -0,0 +1,32 @@ +#ifdef GPROSHAN_OPTIX + +#ifndef RT_OPTIX_PARAMS_H +#define RT_OPTIX_PARAMS_H + + +#include "geodesics/vertex.cuh" + + +// geometry processing and shape analysis framework +namespace gproshan::rt { + + +struct launch_params +{ +}; + + +struct TriangleMeshSBTData +{ + vertex_cu color; + vertex_cu * vertex; + vertex_cu * normal; +}; + + +} // namespace gproshan + +#endif // RT_OPTIX_PARAMS_H + +#endif // GPROSHAN_OPTIX + diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index c2e3471b..e5f82533 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -14,6 +14,32 @@ namespace gproshan::rt { +/*! SBT record for a raygen program */ +struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) RaygenRecord +{ + __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; + // just a dummy value - later examples will use more interesting + // data here + void * data; +}; + +/*! SBT record for a miss program */ +struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) MissRecord +{ + __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; + // just a dummy value - later examples will use more interesting + // data here + void * data; +}; + +/*! SBT record for a hitgroup program */ +struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) HitgroupRecord +{ + __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; +// TriangleMeshSBTData data; +}; + + void optix_log(unsigned int level, const char * tag, const char * message, void *) { fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); @@ -33,7 +59,6 @@ optix::optix(const std::vector & meshes) // create module - OptixModule optix_module; OptixModuleCompileOptions optix_module_compile_opt; OptixPipeline optix_pipeline; @@ -69,7 +94,9 @@ optix::optix(const std::vector & meshes) // create programs - + create_raygen_programs(); + create_miss_programs(); + create_hitgroup_programs(); // build as @@ -89,6 +116,105 @@ index_t optix::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) return NIL; } +void optix::create_raygen_programs() +{ + char log[2048]; + size_t sizeof_log = sizeof(log); + + OptixProgramGroupOptions pg_options = {}; + OptixProgramGroupDesc pg_desc = {}; + pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN; + pg_desc.raygen.module = optix_module; + pg_desc.raygen.entryFunctionName = "__raygen__render_frame"; + + optixProgramGroupCreate(optix_context, + &pg_desc, + 1, + &pg_options, + log, &sizeof_log, + &raygen_programs[0] + ); + + if(sizeof_log > 1) gproshan_log_var(log); +} + +void optix::create_miss_programs() +{ + char log[2048]; + size_t sizeof_log = sizeof(log); + + OptixProgramGroupOptions pg_options = {}; + OptixProgramGroupDesc pg_desc = {}; + pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS; + pg_desc.miss.module = optix_module; + + + pg_desc.miss.entryFunctionName = "__miss__radiance"; + + optixProgramGroupCreate(optix_context, + &pg_desc, + 1, + &pg_options, + log, &sizeof_log, + &miss_programs[0] + ); + + if(sizeof_log > 1) gproshan_log_var(log); + + + pg_desc.miss.entryFunctionName = "__miss__shadow"; + + optixProgramGroupCreate(optix_context, + &pg_desc, + 1, + &pg_options, + log, &sizeof_log, + &miss_programs[1] + ); + + if(sizeof_log > 1) gproshan_log_var(log); +} + +void optix::create_hitgroup_programs() +{ + char log[2048]; + size_t sizeof_log = sizeof(log); + + OptixProgramGroupOptions pg_options = {}; + OptixProgramGroupDesc pg_desc = {}; + pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP; + pg_desc.hitgroup.moduleCH = optix_module; + pg_desc.hitgroup.moduleAH = optix_module; + + + pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__radiance"; + pg_desc.hitgroup.entryFunctionNameAH = "__anyhit__radiance"; + + optixProgramGroupCreate(optix_context, + &pg_desc, + 1, + &pg_options, + log, &sizeof_log, + &hitgroup_programs[0] + ); + + if(sizeof_log > 1) gproshan_log_var(log); + + + pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__shadow"; + pg_desc.hitgroup.entryFunctionNameAH = "__anyhit__shadow"; + + optixProgramGroupCreate(optix_context, + &pg_desc, + 1, + &pg_options, + log, &sizeof_log, + &hitgroup_programs[1] + ); + + if(sizeof_log > 1) gproshan_log_var(log); +} + OptixTraversableHandle optix::build_as(const std::vector & meshes) { OptixTraversableHandle optix_as_handle = {}; @@ -160,6 +286,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) cudaDeviceSynchronize(); + cudaFree(d_output_buffer); cudaFree(d_temp_buffer); cudaFree(d_compacted_size); diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 352d578c..9af1b9b5 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,25 +1,228 @@ -#ifdef GPROSHAN_OPTIX +#ifdef GPROSHAN_OPTIX_FAIL + + +#include "raytracing/rt_optix_params.h" + #include -extern "C" __constant__ void * optix_launch_params; -extern "C" __global__ void closest_hit() +// geometry processing and shape analysis framework +namespace gproshan::rt { + + +extern "C" __constant__ launch_params params; + +static __forceinline__ __device__ +void * unpackPointer(uint32_t i0, uint32_t i1) { - const int f = optixGetPrimitiveIndex(); + const uint64_t uptr = static_cast(i0) << 32 | i1; + void * ptr = reinterpret_cast(uptr); + return ptr; } -extern "C" __global__ void any_hit() +static __forceinline__ __device__ +void packPointer(void * ptr, uint32_t & i0, uint32_t & i1) { + const uint64_t uptr = reinterpret_cast(ptr); + i0 = uptr >> 32; + i1 = uptr & 0x00000000ffffffff; } -extern "C" __global__ void miss_hit() +template +static __forceinline__ __device__ T * getPRD() { + const uint32_t u0 = optixGetPayload_0(); + const uint32_t u1 = optixGetPayload_1(); + return reinterpret_cast(unpackPointer(u0, u1)); } -extern "C" __global__ void __raygen__rt() +//------------------------------------------------------------------------------ +// closest hit and anyhit programs for radiance-type rays. +// +// Note eventually we will have to create one pair of those for each +// ray type and each geometry type we want to render; but this +// simple example doesn't use any actual geometries yet, so we only +// create a single, dummy, set of them (we do have to have at least +// one group of them to set up the SBT) +//------------------------------------------------------------------------------ + +extern "C" __global__ void __closesthit__shadow() { + /* not going to be used ... */ +} + +extern "C" __global__ void __closesthit__radiance() +{ + const TriangleMeshSBTData & sbtData = *(const TriangleMeshSBTData *) optixGetSbtDataPointer(); + + // ------------------------------------------------------------------ + // gather some basic hit information + // ------------------------------------------------------------------ + const int primID = optixGetPrimitiveIndex(); + const vec3i index = sbtData.index[primID]; + const float u = optixGetTriangleBarycentrics().x; + const float v = optixGetTriangleBarycentrics().y; + + // ------------------------------------------------------------------ + // compute normal, using either shading normal (if avail), or + // geometry normal (fallback) + // ------------------------------------------------------------------ + const vertex_cu & A = sbtData.vertex[index.x]; + const vertex_cu & B = sbtData.vertex[index.y]; + const vertex_cu & C = sbtData.vertex[index.z]; + vertex_cu Ng = (B - A) * (C - A); + vertex_cu Ns = (sbtData.normal) + ? ((1.f-u-v) * sbtData.normal[index.x] + + u * sbtData.normal[index.y] + + v * sbtData.normal[index.z]) + : Ng; + + // ------------------------------------------------------------------ + // face-forward and normalize normals + // ------------------------------------------------------------------ + const vertex_cu rayDir = (vertex_cu) optixGetWorldRayDirection(); + + if((rayDir , Ng) > 0.f) Ng = -Ng; + Ng /= *Ng; + + if((Ng , Ns) < 0.f) + Ns -= 2.f * (Ng , Ns) * Ng; + Ns /= *Ns; + + // ------------------------------------------------------------------ + // compute diffuse material color, including diffuse texture, if + // available + // ------------------------------------------------------------------ + vertex_cu diffuseColor = sbtData.color; + + // ------------------------------------------------------------------ + // compute shadow + // ------------------------------------------------------------------ + const vertex_cu surfPos + = (1.f-u-v) * sbtData.vertex[index.x] + + u * sbtData.vertex[index.y] + + v * sbtData.vertex[index.z]; + const vertex_cu lightPos(-907.108f, 2205.875f, -400.0267f); + const vertex_cu lightDir = lightPos - surfPos; + + // trace shadow ray: + vertex_cu lightVisibility = 0.f; + // the values we store the PRD pointer in: + uint32_t u0, u1; + packPointer(&lightVisibility, u0, u1); + optixTrace(params.traversable, + surfPos + 1e-3f * Ng, + lightDir, + 1e-3f, // tmin + 1.f-1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + // For shadow rays: skip any/closest hit shaders and terminate on first + // intersection with anything. The miss shader is used to mark if the + // light was visible. + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + u0, u1); + + // ------------------------------------------------------------------ + // final shading: a bit of ambient, a bit of directional ambient, + // and directional component based on shadowing + // ------------------------------------------------------------------ + const float cosDN = 0.1f + .8f * fabsf((rayDir , Ns)); + + vertex_cu &prd = *(vertex_cu * ) getPRD(); + prd = (.1f + (.2f + .8f * lightVisibility) * cosDN) * diffuseColor; } +extern "C" __global__ void __anyhit__radiance() {} + +extern "C" __global__ void __anyhit__shadow() {} + +//------------------------------------------------------------------------------ +// miss program that gets called for any ray that did not have a +// valid intersection +// +// as with the anyhit/closest hit programs, in this example we only +// need to have _some_ dummy function to set up a valid SBT +// ------------------------------------------------------------------------------ + +extern "C" __global__ void __miss__radiance() +{ + vertex_cu &prd = *(vertex_cu *) getPRD(); + // set to constant white as background color + prd = vertex_cu(1.f); +} + +extern "C" __global__ void __miss__shadow() +{ + // we didn't hit anything, so the light is visible + vertex_cu &prd = *(vertex_cu *)getPRD(); + prd = vertex_cu(1.f); +} + +//------------------------------------------------------------------------------ +// ray gen program - the actual rendering happens in here +//------------------------------------------------------------------------------ +extern "C" __global__ void __raygen__render_frame() +{ + // compute a test pattern based on pixel ID + const int ix = optixGetLaunchIndex().x; + const int iy = optixGetLaunchIndex().y; + + const auto &camera = params.camera; + + // our per-ray data for this example. what we initialize it to + // won't matter, since this value will be overwritten by either + // the miss or hit program, anyway + vertex_cu pixelColorPRD = vertex_cu(0.f); + + // the values we store the PRD pointer in: + uint32_t u0, u1; + packPointer(&pixelColorPRD, u0, u1); + + // normalized screen plane position, in [0,1]^2 + const vec2f screen(vec2f(ix+.5f,iy+.5f) + / vec2f(params.frame.size)); + + // generate ray direction + vertex_cu rayDir = normalize(camera.direction + + (screen.x - 0.5f) * camera.horizontal + + (screen.y - 0.5f) * camera.vertical); + + optixTrace(params.traversable, + camera.position, + rayDir, + 0.f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT,//OPTIX_RAY_FLAG_NONE, + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex + u0, u1); + + const int r = int(255.99f*pixelColorPRD.x); + const int g = int(255.99f*pixelColorPRD.y); + const int b = int(255.99f*pixelColorPRD.z); + + // convert to 32-bit rgba value (we explicitly set alpha to 0xff + // to make stb_image_write happy ... + const uint32_t rgba = 0xff000000 + | (r<<0) | (g<<8) | (b<<16); + + // and write to frame buffer ... + const uint32_t fbIndex = ix+iy*params.frame.size.x; + params.frame.colorBuffer[fbIndex] = rgba; +} + + +} // namespace gproshan + #endif // GPROSHAN_OPTIX From 90a444f3f773b80908a0daac7dc4f6637d9e8f24 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 16:00:45 +0100 Subject: [PATCH 0576/1018] mesh: moving che_cu and vertex_cu files --- include/gproshan/geodesics/geodesics_ptp.cuh | 2 +- include/gproshan/geodesics/geodesics_ptp_coalescence.cuh | 2 +- include/gproshan/geodesics/test_geodesics_ptp.cuh | 2 +- include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh | 2 +- include/gproshan/{geodesics => mesh}/che.cuh | 2 +- include/gproshan/{geodesics => mesh}/vertex.cuh | 0 include/gproshan/raytracing/rt_optix_params.h | 4 ++-- src/{geodesics => mesh}/che.cu | 2 +- src/{geodesics => mesh}/vertex.cu | 2 +- src/raytracing/rt_optix.cu | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) rename include/gproshan/{geodesics => mesh}/che.cuh (94%) rename include/gproshan/{geodesics => mesh}/vertex.cuh (100%) rename src/{geodesics => mesh}/che.cu (98%) rename src/{geodesics => mesh}/vertex.cu (87%) diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh index b1f519f7..7e28d5e9 100644 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ b/include/gproshan/geodesics/geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef GEODESICS_PTP_CUH #define GEODESICS_PTP_CUH -#include "geodesics/che.cuh" +#include "mesh/che.cuh" #define NT 64 diff --git a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh index ae627f42..6e916287 100644 --- a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh @@ -1,7 +1,7 @@ #ifndef GEODESICS_PTP_COALESCENCE_CUH #define GEODESICS_PTP_COALESCENCE_CUH -#include "geodesics/che.cuh" +#include "mesh/che.cuh" #include "geodesics/geodesics_ptp.cuh" #include "geodesics/geodesics_ptp.h" diff --git a/include/gproshan/geodesics/test_geodesics_ptp.cuh b/include/gproshan/geodesics/test_geodesics_ptp.cuh index 9e64444e..1423b94b 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_CUH #define TEST_GEODESICS_PTP_CUH -#include "geodesics/che.cuh" +#include "mesh/che.cuh" // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh index a3fa75a6..9b3cc57c 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_COALESCENCE_CUH #define TEST_GEODESICS_PTP_COALESCENCE_CUH -#include "geodesics/che.cuh" +#include "mesh/che.cuh" // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/che.cuh b/include/gproshan/mesh/che.cuh similarity index 94% rename from include/gproshan/geodesics/che.cuh rename to include/gproshan/mesh/che.cuh index f898fd68..ec993d32 100644 --- a/include/gproshan/geodesics/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -1,7 +1,7 @@ #ifndef CHE_CUH #define CHE_CUH -#include "geodesics/vertex.cuh" +#include "mesh/vertex.cuh" #include "../mesh/che.h" diff --git a/include/gproshan/geodesics/vertex.cuh b/include/gproshan/mesh/vertex.cuh similarity index 100% rename from include/gproshan/geodesics/vertex.cuh rename to include/gproshan/mesh/vertex.cuh diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index fad94ce9..f69bee10 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -4,10 +4,10 @@ #define RT_OPTIX_PARAMS_H -#include "geodesics/vertex.cuh" +#include "mesh/vertex.cuh" -// geometry processing and shape analysis framework +// geometry processing and shape analysis framework namespace gproshan::rt { diff --git a/src/geodesics/che.cu b/src/mesh/che.cu similarity index 98% rename from src/geodesics/che.cu rename to src/mesh/che.cu index ee17fd7c..e36d29b5 100644 --- a/src/geodesics/che.cu +++ b/src/mesh/che.cu @@ -1,4 +1,4 @@ -#include "geodesics/che.cuh" +#include "mesh/che.cuh" // geometry processing and shape analysis framework diff --git a/src/geodesics/vertex.cu b/src/mesh/vertex.cu similarity index 87% rename from src/geodesics/vertex.cu rename to src/mesh/vertex.cu index af7ef320..ea88bf9c 100644 --- a/src/geodesics/vertex.cu +++ b/src/mesh/vertex.cu @@ -1,4 +1,4 @@ -#include "geodesics/vertex.cuh" +#include "mesh/vertex.cuh" // geometry processing and shape analysis framework diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 9af1b9b5..b42cd6b2 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -7,7 +7,7 @@ #include -// geometry processing and shape analysis framework +// geometry processing and shape analysis framework namespace gproshan::rt { From 9bfc529a528243d7625d887a851f1e3d4daef8db Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 19:12:27 +0100 Subject: [PATCH 0577/1018] rt_optix: building sbt --- include/gproshan/mesh/che.cuh | 4 +- include/gproshan/mesh/che.h | 11 ++- include/gproshan/mesh/vertex.cuh | 3 +- include/gproshan/raytracing/rt_optix.h | 14 +++ include/gproshan/raytracing/rt_optix_params.h | 10 +- src/mesh/che.cpp | 3 +- src/mesh/che.cu | 9 +- src/raytracing/rt_optix.cpp | 97 +++++++++++-------- 8 files changed, 93 insertions(+), 58 deletions(-) diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index ec993d32..99300a2f 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -2,7 +2,7 @@ #define CHE_CUH #include "mesh/vertex.cuh" -#include "../mesh/che.h" +#include "mesh/che.h" #define cu_for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[cu_prev(he)]) != stop ? he : NIL) @@ -21,7 +21,7 @@ index_t cu_next(index_t he); __host__ __device__ index_t cu_prev(index_t he); -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che); +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index e3433fb7..e9056d9c 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -159,12 +159,13 @@ struct CHE size_t n_faces; size_t n_half_edges; - vertex_cu * GT; - index_t * VT; - index_t * OT; - index_t * EVT; + vertex_cu * GT = nullptr; + vertex_cu * VN = nullptr; + index_t * VT = nullptr; + index_t * OT = nullptr; + index_t * EVT = nullptr; - CHE(che * mesh); + CHE(const che * mesh); }; diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index 27fdfaf6..8025f248 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -1,9 +1,8 @@ #ifndef VERTEX_CUH #define VERTEX_CUH -#include -#include "../include.h" +#include "include.h" // geometry processing and shape analysis framework diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 7e269b24..7926efbd 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -5,6 +5,7 @@ #include "mesh/che.h" #include "raytracing/raytracing.h" +#include "raytracing/rt_optix_params.h" #include #include @@ -24,11 +25,23 @@ class optix : public raytracing CUstream stream; OptixDeviceContext optix_context; + OptixModule optix_module; + OptixModuleCompileOptions optix_module_compile_opt = {}; + + OptixPipeline optix_pipeline; + OptixPipelineCompileOptions optix_pipeline_compile_opt = {}; + OptixPipelineLinkOptions optix_pipeline_link_opt = {}; + OptixProgramGroup raygen_programs[1]; OptixProgramGroup miss_programs[2]; OptixProgramGroup hitgroup_programs[2]; + launch_params params; + + std::vector dd_mesh; + std::vector d_mesh; + public: optix(const std::vector & meshes); ~optix(); @@ -42,6 +55,7 @@ class optix : public raytracing void create_raygen_programs(); void create_miss_programs(); void create_hitgroup_programs(); + void create_pipeline(); OptixTraversableHandle build_as(const std::vector & meshes); void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index f69bee10..22e33d92 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -4,9 +4,6 @@ #define RT_OPTIX_PARAMS_H -#include "mesh/vertex.cuh" - - // geometry processing and shape analysis framework namespace gproshan::rt { @@ -16,11 +13,14 @@ struct launch_params }; -struct TriangleMeshSBTData +struct vertex_cu; + +struct mesh_sbt_data { - vertex_cu color; vertex_cu * vertex; vertex_cu * normal; + vertex_cu * color; + vertex_cu * index; }; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 73e2bbc6..6b8a9519 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -38,13 +38,14 @@ index_t prev(const index_t & he) return che::mtrig * trig(he) + (he + che::mtrig - 1) % che::mtrig; } -CHE::CHE(che * mesh) +CHE::CHE(const che * mesh) { n_vertices = mesh->n_vertices; n_faces = mesh->n_faces; n_half_edges = mesh->n_half_edges; GT = (vertex_cu *) mesh->GT; + VN = (vertex_cu *) mesh->VN; VT = mesh->VT; OT = mesh->OT; EVT = mesh->EVT; diff --git a/src/mesh/che.cu b/src/mesh/che.cu index e36d29b5..60239f58 100644 --- a/src/mesh/che.cu +++ b/src/mesh/che.cu @@ -26,13 +26,19 @@ index_t cu_prev(index_t he) return che::mtrig * cu_trig(he) + (he + che::mtrig - 1) % che::mtrig; } -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che) +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal) { dd_che = (CHE *) malloc(sizeof(CHE)); memcpy(dd_che, h_che, sizeof(CHE)); cudaMalloc(&dd_che->GT, sizeof(vertex_cu) * h_che->n_vertices); cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); + + if(normal) + { + cudaMalloc(&dd_che->VN, sizeof(vertex_cu) * h_che->n_vertices); + cudaMemcpy(dd_che->VN, h_che->VN, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); + } cudaMalloc(&dd_che->VT, sizeof(index_t) * h_che->n_half_edges); cudaMemcpy(dd_che->VT, h_che->VT, sizeof(index_t) * h_che->n_half_edges, cudaMemcpyHostToDevice); @@ -50,6 +56,7 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che) void cuda_free_CHE(CHE *& dd_che, CHE *& d_che) { if(dd_che->GT) cudaFree(dd_che->GT); + if(dd_che->VN) cudaFree(dd_che->VN); if(dd_che->VT) cudaFree(dd_che->VT); if(dd_che->OT) cudaFree(dd_che->OT); if(dd_che->EVT) cudaFree(dd_che->EVT); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index e5f82533..d4407aca 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -1,6 +1,8 @@ +#ifdef GPROSHAN_OPTIX + #include "raytracing/rt_optix.h" -#ifdef GPROSHAN_OPTIX +#include "mesh/che.cuh" #include #include @@ -15,28 +17,28 @@ namespace gproshan::rt { /*! SBT record for a raygen program */ -struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) RaygenRecord +struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) RaygenRecord { - __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; + __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; // just a dummy value - later examples will use more interesting // data here void * data; }; /*! SBT record for a miss program */ -struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) MissRecord +struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) MissRecord { - __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; + __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; // just a dummy value - later examples will use more interesting // data here void * data; }; /*! SBT record for a hitgroup program */ -struct __align__( OPTIX_SBT_RECORD_ALIGNMENT ) HitgroupRecord +struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) HitgroupRecord { - __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; -// TriangleMeshSBTData data; + __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; + mesh_sbt_data mesh; }; @@ -59,12 +61,6 @@ optix::optix(const std::vector & meshes) // create module - OptixModuleCompileOptions optix_module_compile_opt; - - OptixPipeline optix_pipeline; - OptixPipelineCompileOptions optix_pipeline_compile_opt; - OptixPipelineLinkOptions optix_pipeline_link_opt; - optix_module_compile_opt.maxRegisterCount = 50; optix_module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; optix_module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; @@ -75,7 +71,7 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.numPayloadValues = 2; optix_pipeline_compile_opt.numAttributeValues = 2; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_launch_params"; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "params"; optix_pipeline_link_opt.maxTraceDepth = 2; @@ -92,23 +88,27 @@ optix::optix(const std::vector & meshes) &optix_module ); - // create programs + // create programs create_raygen_programs(); create_miss_programs(); create_hitgroup_programs(); // build as - build_as(meshes); // create pipeline - + create_pipeline(); + // build sbt + + } optix::~optix() { + for(index_t i = 0; i < dd_mesh.size(); ++i) + cuda_free_CHE(dd_mesh[i], d_mesh[i]); } index_t optix::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) @@ -215,6 +215,34 @@ void optix::create_hitgroup_programs() if(sizeof_log > 1) gproshan_log_var(log); } +void optix::create_pipeline() +{ + std::vector program_groups; + program_groups.push_back(raygen_programs[0]); + program_groups.push_back(hitgroup_programs[0]); + program_groups.push_back(hitgroup_programs[1]); + program_groups.push_back(miss_programs[0]); + program_groups.push_back(miss_programs[1]); + + char log[2048]; + size_t sizeof_log = sizeof(log); + + optixPipelineCreate(optix_context, + &optix_pipeline_compile_opt, + &optix_pipeline_link_opt, + program_groups.data(), + program_groups.size(), + log, &sizeof_log, + &optix_pipeline + ); + + if(sizeof_log > 1) gproshan_log_var(log); + + optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); + + if(sizeof_log > 1) gproshan_log_var(log); +} + OptixTraversableHandle optix::build_as(const std::vector & meshes) { OptixTraversableHandle optix_as_handle = {}; @@ -286,7 +314,6 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) cudaDeviceSynchronize(); - cudaFree(d_output_buffer); cudaFree(d_temp_buffer); cudaFree(d_compacted_size); @@ -296,42 +323,28 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh) { - void * d_vertex = nullptr; - void * d_index = nullptr; - -#ifdef GPROSHAN_FLOAT - cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(vertex)); - cudaMemcpy(d_vertex, &mesh->gt(0), mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); -#else - glm::vec3 * vertices = new glm::vec3[mesh->n_vertices]; - cudaMalloc(&d_vertex, mesh->n_vertices * sizeof(float) * 3); - - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - vertices[i] = glm_vec3(mesh->gt(i)); - - cudaMemcpy(d_vertex, vertices, mesh->n_vertices * sizeof(vertex), cudaMemcpyHostToDevice); - - delete [] vertices; -#endif // GPROSHAN_FLOAT + CHE * dd_m, * d_m; + CHE h_m(mesh); - cudaMalloc(&d_index, mesh->n_half_edges * sizeof(index_t)); - cudaMemcpy(d_index, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t), cudaMemcpyHostToDevice); + cuda_create_CHE(&h_m, dd_m, d_m); + dd_mesh.push_back(dd_m); + d_mesh.push_back(d_m); - d_vertex_ptr = (CUdeviceptr) d_vertex; + + d_vertex_ptr = (CUdeviceptr) dd_m->GT; optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; - optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); + optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); optix_mesh.triangleArray.numVertices = mesh->n_vertices; optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces; - optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_index; + optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; optix_trig_flags = 0; From 9d001ce088b0bf3171d9a77591492ddf48ec752f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 22:14:24 +0100 Subject: [PATCH 0578/1018] rt_optix: sharing vertex camera params --- include/gproshan/mesh/vertex.cuh | 37 ++++++++++------ include/gproshan/raytracing/rt_optix.h | 7 +++- include/gproshan/raytracing/rt_optix_params.h | 31 +++++++++----- src/mesh/che.cu | 2 +- src/mesh/vertex.cu | 9 +++- src/raytracing/rt_optix.cpp | 22 ++++++---- src/raytracing/rt_optix.cu | 42 +++++++++---------- 7 files changed, 92 insertions(+), 58 deletions(-) diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index 8025f248..d86ba9e3 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -24,31 +24,39 @@ struct vertex_cu } __host__ __device__ - ~vertex_cu() + vertex_cu(const float3 & v) { + x = v.x; + y = v.y; + z = v.z; + } + __host__ __device__ + operator float3() const + { + return make_float3(x, y, z); } __host__ __device__ - vertex_cu operator*(const vertex_cu & v) const + vertex_cu operator * (const vertex_cu & v) const { return vertex_cu(y * v.z - z * v.y, -(x * v.z - z * v.x), x * v.y - y * v.x); } __host__ __device__ - real_t operator*() + real_t operator * () { return sqrt(x * x + y * y + z * z); } __host__ __device__ - vertex_cu operator/(const real_t & a) const + vertex_cu operator / (const real_t & a) const { return vertex_cu(x / a, y / a, z / a); } __host__ __device__ - void operator/=(const real_t & v) + void operator /= (const real_t & v) { x /= v; y /= v; @@ -56,19 +64,19 @@ struct vertex_cu } __host__ __device__ - real_t operator,(const vertex_cu & v) const + real_t operator , (const vertex_cu & v) const { return x * v.x + y * v.y + z * v.z; } __host__ __device__ - vertex_cu operator+(const vertex_cu & v) const + vertex_cu operator + (const vertex_cu & v) const { return vertex_cu(x+v.x, y+v.y, z+v.z); } __host__ __device__ - void operator+=(const vertex_cu & v) + void operator += (const vertex_cu & v) { x += v.x; y += v.y; @@ -76,13 +84,13 @@ struct vertex_cu } __host__ __device__ - vertex_cu operator-(const vertex_cu & v) const + vertex_cu operator - (const vertex_cu & v) const { return vertex_cu(x - v.x, y - v.y, z - v.z); } __host__ __device__ - void operator-=(const vertex_cu & v) + void operator -= (const vertex_cu & v) { x -= v.x; y -= v.y; @@ -90,20 +98,23 @@ struct vertex_cu } __host__ __device__ - vertex_cu operator-() const + vertex_cu operator - () const { return vertex_cu(-x, -y, -z); } __host__ __device__ - real_t & operator[](const int & i) + real_t & operator [] (const int & i) { return (&x)[i]; } }; __host__ __device__ -vertex_cu operator*(const real_t & a, const vertex_cu & v); +vertex_cu operator * (const real_t & a, const vertex_cu & v); + +__host__ __device__ +vertex_cu operator + (const real_t & a, const vertex_cu & v); } // namespace gproshan diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 7926efbd..30f87838 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -28,15 +28,17 @@ class optix : public raytracing OptixModule optix_module; OptixModuleCompileOptions optix_module_compile_opt = {}; - + OptixPipeline optix_pipeline; OptixPipelineCompileOptions optix_pipeline_compile_opt = {}; OptixPipelineLinkOptions optix_pipeline_link_opt = {}; - + OptixProgramGroup raygen_programs[1]; OptixProgramGroup miss_programs[2]; OptixProgramGroup hitgroup_programs[2]; + OptixShaderBindingTable sbt = {}; + launch_params params; std::vector dd_mesh; @@ -56,6 +58,7 @@ class optix : public raytracing void create_miss_programs(); void create_hitgroup_programs(); void create_pipeline(); + void build_sbt(); OptixTraversableHandle build_as(const std::vector & meshes); void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh); }; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 22e33d92..157c61b2 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -4,23 +4,32 @@ #define RT_OPTIX_PARAMS_H -// geometry processing and shape analysis framework -namespace gproshan::rt { +#include "mesh/vertex.h" +#include -struct launch_params -{ -}; +// geometry processing and shape analysis framework +namespace gproshan::rt { -struct vertex_cu; -struct mesh_sbt_data +struct launch_params { - vertex_cu * vertex; - vertex_cu * normal; - vertex_cu * color; - vertex_cu * index; + struct + { + uint32_t * colorBuffer = nullptr; + uint32_t width, height; + } frame; + + struct + { + vertex_cu position; + vertex_cu direction; + vertex_cu horizontal; + vertex_cu vertical; + } camera; + + OptixTraversableHandle traversable; }; diff --git a/src/mesh/che.cu b/src/mesh/che.cu index 60239f58..6119975f 100644 --- a/src/mesh/che.cu +++ b/src/mesh/che.cu @@ -33,7 +33,7 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm cudaMalloc(&dd_che->GT, sizeof(vertex_cu) * h_che->n_vertices); cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); - + if(normal) { cudaMalloc(&dd_che->VN, sizeof(vertex_cu) * h_che->n_vertices); diff --git a/src/mesh/vertex.cu b/src/mesh/vertex.cu index ea88bf9c..50b31628 100644 --- a/src/mesh/vertex.cu +++ b/src/mesh/vertex.cu @@ -6,11 +6,18 @@ namespace gproshan { __host__ __device__ -vertex_cu operator*(const real_t & a, const vertex_cu & v) +vertex_cu operator * (const real_t & a, const vertex_cu & v) { return vertex_cu(a * v.x, a * v.y, a * v.z); } +__host__ __device__ +vertex_cu operator + (const real_t & a, const vertex_cu & v) +{ + return vertex_cu(a + v.x, a + v.y, a + v.z); +} + + } // namespace gproshan diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index d4407aca..4e4ac40e 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -38,7 +38,7 @@ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) MissRecord struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) HitgroupRecord { __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; - mesh_sbt_data mesh; + CHE * mesh; }; @@ -95,14 +95,13 @@ optix::optix(const std::vector & meshes) create_hitgroup_programs(); // build as - build_as(meshes); + params.traversable = build_as(meshes); // create pipeline create_pipeline(); - - // build sbt - + // build sbt + build_sbt(); } optix::~optix() @@ -226,7 +225,7 @@ void optix::create_pipeline() char log[2048]; size_t sizeof_log = sizeof(log); - + optixPipelineCreate(optix_context, &optix_pipeline_compile_opt, &optix_pipeline_link_opt, @@ -235,14 +234,19 @@ void optix::create_pipeline() log, &sizeof_log, &optix_pipeline ); - + if(sizeof_log > 1) gproshan_log_var(log); optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); - + if(sizeof_log > 1) gproshan_log_var(log); } +void optix::build_sbt() +{ + +} + OptixTraversableHandle optix::build_as(const std::vector & meshes) { OptixTraversableHandle optix_as_handle = {}; @@ -330,7 +334,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); - + d_vertex_ptr = (CUdeviceptr) dd_m->GT; optix_mesh = {}; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index b42cd6b2..c7d0bd3e 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,10 +1,13 @@ #ifdef GPROSHAN_OPTIX_FAIL +#include "mesh/che.h" +#include "mesh/vertex.cuh" #include "raytracing/rt_optix_params.h" #include +#include // geometry processing and shape analysis framework @@ -54,13 +57,13 @@ extern "C" __global__ void __closesthit__shadow() extern "C" __global__ void __closesthit__radiance() { - const TriangleMeshSBTData & sbtData = *(const TriangleMeshSBTData *) optixGetSbtDataPointer(); + const CHE & sbtData = *(const CHE *) optixGetSbtDataPointer(); // ------------------------------------------------------------------ // gather some basic hit information // ------------------------------------------------------------------ const int primID = optixGetPrimitiveIndex(); - const vec3i index = sbtData.index[primID]; + const index_t he = primID * che::mtrig; const float u = optixGetTriangleBarycentrics().x; const float v = optixGetTriangleBarycentrics().y; @@ -68,14 +71,15 @@ extern "C" __global__ void __closesthit__radiance() // compute normal, using either shading normal (if avail), or // geometry normal (fallback) // ------------------------------------------------------------------ - const vertex_cu & A = sbtData.vertex[index.x]; - const vertex_cu & B = sbtData.vertex[index.y]; - const vertex_cu & C = sbtData.vertex[index.z]; + const vertex_cu & A = sbtData.GT[sbtData.VT[he]]; + const vertex_cu & B = sbtData.GT[sbtData.VT[he + 1]]; + const vertex_cu & C = sbtData.GT[sbtData.VT[he + 2]]; + vertex_cu Ng = (B - A) * (C - A); - vertex_cu Ns = (sbtData.normal) - ? ((1.f-u-v) * sbtData.normal[index.x] - + u * sbtData.normal[index.y] - + v * sbtData.normal[index.z]) + vertex_cu Ns = (sbtData.VN) + ? ((1.f-u-v) * sbtData.VN[sbtData.VT[he]] + + u * sbtData.VN[sbtData.VT[he + 1]] + + v * sbtData.VN[sbtData.VT[he + 2]]) : Ng; // ------------------------------------------------------------------ @@ -94,15 +98,12 @@ extern "C" __global__ void __closesthit__radiance() // compute diffuse material color, including diffuse texture, if // available // ------------------------------------------------------------------ - vertex_cu diffuseColor = sbtData.color; + vertex_cu diffuseColor(230.0/255, 240.0/255, 250.0/255); // ------------------------------------------------------------------ // compute shadow // ------------------------------------------------------------------ - const vertex_cu surfPos - = (1.f-u-v) * sbtData.vertex[index.x] - + u * sbtData.vertex[index.y] - + v * sbtData.vertex[index.z]; + const vertex_cu surfPos = (1.f - u - v) * A + u * B + v * C; const vertex_cu lightPos(-907.108f, 2205.875f, -400.0267f); const vertex_cu lightDir = lightPos - surfPos; @@ -135,7 +136,7 @@ extern "C" __global__ void __closesthit__radiance() // ------------------------------------------------------------------ const float cosDN = 0.1f + .8f * fabsf((rayDir , Ns)); - vertex_cu &prd = *(vertex_cu * ) getPRD(); + vertex_cu & prd = *(vertex_cu * ) getPRD(); prd = (.1f + (.2f + .8f * lightVisibility) * cosDN) * diffuseColor; } @@ -186,13 +187,12 @@ extern "C" __global__ void __raygen__render_frame() packPointer(&pixelColorPRD, u0, u1); // normalized screen plane position, in [0,1]^2 - const vec2f screen(vec2f(ix+.5f,iy+.5f) - / vec2f(params.frame.size)); + const float xscreen = (ix + .5f) / params.frame.width; + const float yscreen = (iy + .5f) / params.frame.height; // generate ray direction - vertex_cu rayDir = normalize(camera.direction - + (screen.x - 0.5f) * camera.horizontal - + (screen.y - 0.5f) * camera.vertical); + vertex_cu rayDir = camera.direction + (xscreen - 0.5f) * camera.horizontal + (yscreen - 0.5f) * camera.vertical; + rayDir /= *rayDir; optixTrace(params.traversable, camera.position, @@ -217,7 +217,7 @@ extern "C" __global__ void __raygen__render_frame() | (r<<0) | (g<<8) | (b<<16); // and write to frame buffer ... - const uint32_t fbIndex = ix+iy*params.frame.size.x; + const uint32_t fbIndex = ix + iy * params.frame.width; params.frame.colorBuffer[fbIndex] = rgba; } From 71c67cf200b2eb65bf5d415124bdce9d28efdf5e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 22:31:22 +0100 Subject: [PATCH 0579/1018] rt_optix: ptx sharing camera info --- include/gproshan/raytracing/rt_optix_params.h | 7 +--- src/raytracing/rt_optix.cu | 42 +++++++++++-------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 157c61b2..128ba46a 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -4,8 +4,6 @@ #define RT_OPTIX_PARAMS_H -#include "mesh/vertex.h" - #include @@ -23,10 +21,7 @@ struct launch_params struct { - vertex_cu position; - vertex_cu direction; - vertex_cu horizontal; - vertex_cu vertical; + void * data; } camera; OptixTraversableHandle traversable; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index c7d0bd3e..e4b8ed0c 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,4 +1,4 @@ -#ifdef GPROSHAN_OPTIX_FAIL +#ifdef GPROSHAN_OPTIX #include "mesh/che.h" @@ -190,26 +190,32 @@ extern "C" __global__ void __raygen__render_frame() const float xscreen = (ix + .5f) / params.frame.width; const float yscreen = (iy + .5f) / params.frame.height; + vertex_cu * cam_data = (vertex_cu *)camera.data; + vertex_cu & position = cam_data[0]; + vertex_cu & direction = cam_data[1]; + vertex_cu & horizontal = cam_data[2]; + vertex_cu & vertical = cam_data[3]; + // generate ray direction - vertex_cu rayDir = camera.direction + (xscreen - 0.5f) * camera.horizontal + (yscreen - 0.5f) * camera.vertical; + vertex_cu rayDir = direction + (xscreen - 0.5f) * horizontal + (yscreen - 0.5f) * vertical; rayDir /= *rayDir; - optixTrace(params.traversable, - camera.position, - rayDir, - 0.f, // tmin - 1e20f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT,//OPTIX_RAY_FLAG_NONE, - 0, // SBT offset - 2, // SBT stride - 0, // missSBTIndex - u0, u1); - - const int r = int(255.99f*pixelColorPRD.x); - const int g = int(255.99f*pixelColorPRD.y); - const int b = int(255.99f*pixelColorPRD.z); + optixTrace( params.traversable, + position, + rayDir, + 0.f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex + u0, u1); + + const int r = int(255.99f * pixelColorPRD.x); + const int g = int(255.99f * pixelColorPRD.y); + const int b = int(255.99f * pixelColorPRD.z); // convert to 32-bit rgba value (we explicitly set alpha to 0xff // to make stb_image_write happy ... From ad33cd44279faa60a18c40bcebb18a965e78e23d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 14 Nov 2021 23:35:47 +0100 Subject: [PATCH 0580/1018] rt_optix: fix compiling ptx file --- apps/CMakeLists.txt | 11 +++++------ include/gproshan/mesh/vertex.cuh | 6 ------ src/mesh/vertex.cu | 12 ------------ src/raytracing/rt_optix.cu | 20 +++++++++++++++----- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 6626ac44..203c8eed 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -3,13 +3,12 @@ set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) target_link_libraries(test_gproshan gproshan) if(OptiX_INCLUDE) - add_library(ptx_sources OBJECT ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) - set_property(TARGET ptx_sources PROPERTY CUDA_PTX_COMPILATION ON) - set_property(TARGET ptx_sources PROPERTY CUDA_ARCHITECTURES OFF) - target_compile_options(ptx_sources PRIVATE "--keep") - add_custom_target( ptx_tmp DEPENDS ptx_sources + find_package(CUDA) + cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + list(GET ptx_sources 0 ptx_file) + add_custom_target( ptx_tmp DEPENDS ${ptx_file} COMMAND ${CMAKE_COMMAND} -E copy - ${gproshan_BINARY_DIR}/apps/rt_optix.ptx + ${ptx_file} ${gproshan_SOURCE_DIR}/tmp/rt_optix.ptx ) add_dependencies(test_gproshan ptx_tmp) diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index d86ba9e3..754bdef3 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -110,12 +110,6 @@ struct vertex_cu } }; -__host__ __device__ -vertex_cu operator * (const real_t & a, const vertex_cu & v); - -__host__ __device__ -vertex_cu operator + (const real_t & a, const vertex_cu & v); - } // namespace gproshan diff --git a/src/mesh/vertex.cu b/src/mesh/vertex.cu index 50b31628..dd786a7b 100644 --- a/src/mesh/vertex.cu +++ b/src/mesh/vertex.cu @@ -5,18 +5,6 @@ namespace gproshan { -__host__ __device__ -vertex_cu operator * (const real_t & a, const vertex_cu & v) -{ - return vertex_cu(a * v.x, a * v.y, a * v.z); -} - -__host__ __device__ -vertex_cu operator + (const real_t & a, const vertex_cu & v) -{ - return vertex_cu(a + v.x, a + v.y, a + v.z); -} - } // namespace gproshan diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index e4b8ed0c..3242c1f4 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -14,6 +14,19 @@ namespace gproshan::rt { +__host__ __device__ +vertex_cu operator * (const real_t & a, const vertex_cu & v) +{ + return vertex_cu(a * v.x, a * v.y, a * v.z); +} + +__host__ __device__ +vertex_cu operator + (const real_t & a, const vertex_cu & v) +{ + return vertex_cu(a + v.x, a + v.y, a + v.z); +} + + extern "C" __constant__ launch_params params; static __forceinline__ __device__ @@ -50,10 +63,7 @@ static __forceinline__ __device__ T * getPRD() // one group of them to set up the SBT) //------------------------------------------------------------------------------ -extern "C" __global__ void __closesthit__shadow() -{ - /* not going to be used ... */ -} +extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { @@ -77,7 +87,7 @@ extern "C" __global__ void __closesthit__radiance() vertex_cu Ng = (B - A) * (C - A); vertex_cu Ns = (sbtData.VN) - ? ((1.f-u-v) * sbtData.VN[sbtData.VT[he]] + ? ((1.f - u - v) * sbtData.VN[sbtData.VT[he]] + u * sbtData.VN[sbtData.VT[he + 1]] + v * sbtData.VN[sbtData.VT[he + 2]]) : Ng; From 37e0bd17d7361cefc2f99b537eb16e06ffef0b54 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 11:53:39 +0100 Subject: [PATCH 0581/1018] rt_optix: launching rays --- include/gproshan/raytracing/rt_optix.h | 18 +++- include/gproshan/raytracing/rt_optix_params.h | 7 +- src/raytracing/rt_optix.cpp | 89 ++++++++++++++++++- src/raytracing/rt_optix.cu | 30 +++---- 4 files changed, 115 insertions(+), 29 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 30f87838..5042a9b6 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -39,21 +39,35 @@ class optix : public raytracing OptixShaderBindingTable sbt = {}; - launch_params params; + launch_params render_params; + void * launch_params_buffer = nullptr; std::vector dd_mesh; std::vector d_mesh; + void * raygen_records_buffer = nullptr; + void * miss_records_buffer = nullptr; + void * hitgroup_records_buffer = nullptr; + public: optix(const std::vector & meshes); ~optix(); virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); + + void pathtracing( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const std::vector & light, + const bool & flat, + const bool & restart = false + ); + private: glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - + void create_raygen_programs(); void create_miss_programs(); void create_hitgroup_programs(); diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 128ba46a..81ddd851 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -15,14 +15,11 @@ struct launch_params { struct { - uint32_t * colorBuffer = nullptr; + void * color_buffer = nullptr; uint32_t width, height; } frame; - struct - { - void * data; - } camera; + void * camera = nullptr; OptixTraversableHandle traversable; }; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 4e4ac40e..6f435d7f 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -95,13 +95,17 @@ optix::optix(const std::vector & meshes) create_hitgroup_programs(); // build as - params.traversable = build_as(meshes); + render_params.traversable = build_as(meshes); // create pipeline create_pipeline(); // build sbt build_sbt(); + + // launch params + cudaMalloc(&launch_params_buffer, sizeof(launch_params)); + cudaMalloc(&render_params.camera, 4 * sizeof(vertex)); } optix::~optix() @@ -115,6 +119,45 @@ index_t optix::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) return NIL; } +void optix::pathtracing( const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const std::vector & light, + const bool & flat, + const bool & restart + ) +{ + if(rt_restart(windows_size.x, windows_size.y) || restart) + { + n_samples = 0; + + if(!render_params.frame.color_buffer) + cudaFree(render_params.frame.color_buffer); + + render_params.frame.width = windows_size.x; + render_params.frame.height = windows_size.y; + cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(vertex)); + } + + vertex camera[4]; + + + cudaMemcpy(render_params.camera, camera, sizeof(camera), cudaMemcpyHostToDevice); + cudaMemcpy(launch_params_buffer, &render_params, sizeof(launch_params), cudaMemcpyHostToDevice); + + optixLaunch(optix_pipeline, + stream, + (CUdeviceptr) launch_params_buffer, + sizeof(launch_params), + &sbt, + render_params.frame.width, + render_params.frame.height, + 1 + ); + + cudaDeviceSynchronize(); +} + void optix::create_raygen_programs() { char log[2048]; @@ -244,7 +287,49 @@ void optix::create_pipeline() void optix::build_sbt() { - + RaygenRecord raygen_records[1]; + for(int i = 0; i < 1; ++i) + { + RaygenRecord & rec = raygen_records[i]; + optixSbtRecordPackHeader(raygen_programs[i], &rec); + rec.data = nullptr; + } + + cudaMalloc(&raygen_records_buffer, sizeof(RaygenRecord)); + cudaMemcpy(raygen_records_buffer, raygen_records, sizeof(RaygenRecord), cudaMemcpyHostToDevice); + sbt.raygenRecord = (CUdeviceptr) raygen_records_buffer; + + + MissRecord miss_records[2]; + for(int i = 0; i < 2; ++i) + { + MissRecord & rec = miss_records[i]; + optixSbtRecordPackHeader(miss_programs[i], &rec); + rec.data = nullptr; + } + + cudaMalloc(&miss_records_buffer, 2 * sizeof(MissRecord)); + cudaMemcpy(miss_records_buffer, miss_records, sizeof(MissRecord), cudaMemcpyHostToDevice); + sbt.missRecordBase = (CUdeviceptr) miss_records_buffer; + sbt.missRecordStrideInBytes = sizeof(MissRecord); + sbt.missRecordCount = 2; + + + std::vector hitgroup_records; + for(index_t i = 0; i < d_mesh.size(); ++i) + for(index_t r = 0; r < 2; ++r) + { + HitgroupRecord rec; + optixSbtRecordPackHeader(hitgroup_programs[r], &rec); + rec.mesh = d_mesh[i]; + hitgroup_records.push_back(rec); + } + + cudaMalloc(&hitgroup_records_buffer, 2 * sizeof(HitgroupRecord)); + cudaMemcpy(hitgroup_records_buffer, hitgroup_records.data(), hitgroup_records.size() * sizeof(HitgroupRecord), cudaMemcpyHostToDevice); + sbt.hitgroupRecordBase = (CUdeviceptr) hitgroup_records_buffer; + sbt.hitgroupRecordStrideInBytes = sizeof(HitgroupRecord); + sbt.hitgroupRecordCount = hitgroup_records.size(); } OptixTraversableHandle optix::build_as(const std::vector & meshes) diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 3242c1f4..34675d7d 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -27,7 +27,7 @@ vertex_cu operator + (const real_t & a, const vertex_cu & v) } -extern "C" __constant__ launch_params params; +extern "C" __constant__ launch_params optixLaunchParams; static __forceinline__ __device__ void * unpackPointer(uint32_t i0, uint32_t i1) @@ -67,7 +67,7 @@ extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { - const CHE & sbtData = *(const CHE *) optixGetSbtDataPointer(); + const CHE & sbtData = **(const CHE **) optixGetSbtDataPointer(); // ------------------------------------------------------------------ // gather some basic hit information @@ -122,7 +122,7 @@ extern "C" __global__ void __closesthit__radiance() // the values we store the PRD pointer in: uint32_t u0, u1; packPointer(&lightVisibility, u0, u1); - optixTrace(params.traversable, + optixTrace(optixLaunchParams.traversable, surfPos + 1e-3f * Ng, lightDir, 1e-3f, // tmin @@ -185,8 +185,6 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const auto &camera = params.camera; - // our per-ray data for this example. what we initialize it to // won't matter, since this value will be overwritten by either // the miss or hit program, anyway @@ -197,10 +195,10 @@ extern "C" __global__ void __raygen__render_frame() packPointer(&pixelColorPRD, u0, u1); // normalized screen plane position, in [0,1]^2 - const float xscreen = (ix + .5f) / params.frame.width; - const float yscreen = (iy + .5f) / params.frame.height; + const float xscreen = (ix + .5f) / optixLaunchParams.frame.width; + const float yscreen = (iy + .5f) / optixLaunchParams.frame.height; - vertex_cu * cam_data = (vertex_cu *)camera.data; + vertex_cu * cam_data = (vertex_cu *) optixLaunchParams.camera; vertex_cu & position = cam_data[0]; vertex_cu & direction = cam_data[1]; vertex_cu & horizontal = cam_data[2]; @@ -210,7 +208,7 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu rayDir = direction + (xscreen - 0.5f) * horizontal + (yscreen - 0.5f) * vertical; rayDir /= *rayDir; - optixTrace( params.traversable, + optixTrace( optixLaunchParams.traversable, position, rayDir, 0.f, // tmin @@ -223,18 +221,10 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); - const int r = int(255.99f * pixelColorPRD.x); - const int g = int(255.99f * pixelColorPRD.y); - const int b = int(255.99f * pixelColorPRD.z); - - // convert to 32-bit rgba value (we explicitly set alpha to 0xff - // to make stb_image_write happy ... - const uint32_t rgba = 0xff000000 - | (r<<0) | (g<<8) | (b<<16); + const uint32_t fbIndex = ix + iy * optixLaunchParams.frame.width; - // and write to frame buffer ... - const uint32_t fbIndex = ix + iy * params.frame.width; - params.frame.colorBuffer[fbIndex] = rgba; + vertex_cu * frame = (vertex_cu *) optixLaunchParams.frame.color_buffer; + frame[fbIndex] = pixelColorPRD; } From 15a3d3c52c0cdab493df65dc7951e91552d8bb2f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 12:06:07 +0100 Subject: [PATCH 0582/1018] rt_optix: downloading buffer, optixLaunchParams error --- include/gproshan/raytracing/raytracing.h | 2 +- src/raytracing/rt_optix.cpp | 4 +++- src/raytracing/rt_optix.cu | 9 ++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index b0254880..af9d33ce 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -35,7 +35,7 @@ class raytracing size_t n_samples; public: - glm::vec4 * img; + glm::vec4 * img = nullptr; public: raytracing(); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 6f435d7f..935ac24d 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -136,7 +136,7 @@ void optix::pathtracing( const glm::uvec2 & windows_size, render_params.frame.width = windows_size.x; render_params.frame.height = windows_size.y; - cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(vertex)); + cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4)); } vertex camera[4]; @@ -156,6 +156,8 @@ void optix::pathtracing( const glm::uvec2 & windows_size, ); cudaDeviceSynchronize(); + + cudaMemcpy(img, render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4), cudaMemcpyDeviceToHost); } void optix::create_raygen_programs() diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 34675d7d..94f7e7e2 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -198,7 +198,7 @@ extern "C" __global__ void __raygen__render_frame() const float xscreen = (ix + .5f) / optixLaunchParams.frame.width; const float yscreen = (iy + .5f) / optixLaunchParams.frame.height; - vertex_cu * cam_data = (vertex_cu *) optixLaunchParams.camera; + vertex_cu * cam_data = (vertex_cu *) optixLaunchParams.camera; vertex_cu & position = cam_data[0]; vertex_cu & direction = cam_data[1]; vertex_cu & horizontal = cam_data[2]; @@ -223,8 +223,11 @@ extern "C" __global__ void __raygen__render_frame() const uint32_t fbIndex = ix + iy * optixLaunchParams.frame.width; - vertex_cu * frame = (vertex_cu *) optixLaunchParams.frame.color_buffer; - frame[fbIndex] = pixelColorPRD; + float4 * frame = (float4 *) optixLaunchParams.frame.color_buffer; + frame[fbIndex].x = pixelColorPRD.x; + frame[fbIndex].y = pixelColorPRD.y; + frame[fbIndex].z = pixelColorPRD.z; + frame[fbIndex].w = 1; } From 2f6e531b1635a6d670b522841a71165ef0ff04d5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 14:19:33 +0100 Subject: [PATCH 0583/1018] rt_optix: using glm on ptx programs --- include/gproshan/raytracing/rt_optix_params.h | 6 +- src/raytracing/rt_optix.cpp | 17 ++++-- src/raytracing/rt_optix.cu | 55 +++++++++++-------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 81ddd851..8422e577 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -18,8 +18,10 @@ struct launch_params void * color_buffer = nullptr; uint32_t width, height; } frame; - - void * camera = nullptr; + + float light[3]; + float cam_pos[3]; + float inv_proj_view[16]; OptixTraversableHandle traversable; }; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 935ac24d..bbdd06cf 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -10,6 +10,9 @@ #include +#include +#include + // geometry processing and shape analysis framework // raytracing approach @@ -71,7 +74,7 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.numPayloadValues = 2; optix_pipeline_compile_opt.numAttributeValues = 2; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "params"; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optixLaunchParams"; optix_pipeline_link_opt.maxTraceDepth = 2; @@ -105,7 +108,6 @@ optix::optix(const std::vector & meshes) // launch params cudaMalloc(&launch_params_buffer, sizeof(launch_params)); - cudaMalloc(&render_params.camera, 4 * sizeof(vertex)); } optix::~optix() @@ -131,7 +133,7 @@ void optix::pathtracing( const glm::uvec2 & windows_size, { n_samples = 0; - if(!render_params.frame.color_buffer) + if(render_params.frame.color_buffer) cudaFree(render_params.frame.color_buffer); render_params.frame.width = windows_size.x; @@ -139,10 +141,13 @@ void optix::pathtracing( const glm::uvec2 & windows_size, cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4)); } - vertex camera[4]; - + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - cudaMemcpy(render_params.camera, camera, sizeof(camera), cudaMemcpyHostToDevice); + memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); + memcpy(render_params.cam_pos, glm::value_ptr(cam_pos), sizeof(render_params.cam_pos)); + memcpy(render_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(render_params.inv_proj_view)); + cudaMemcpy(launch_params_buffer, &render_params, sizeof(launch_params), cudaMemcpyHostToDevice); optixLaunch(optix_pipeline, diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 94f7e7e2..f17441dd 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -9,6 +9,8 @@ #include #include +#include + // geometry processing and shape analysis framework namespace gproshan::rt { @@ -114,7 +116,7 @@ extern "C" __global__ void __closesthit__radiance() // compute shadow // ------------------------------------------------------------------ const vertex_cu surfPos = (1.f - u - v) * A + u * B + v * C; - const vertex_cu lightPos(-907.108f, 2205.875f, -400.0267f); + const vertex_cu lightPos = { optixLaunchParams.light[0], optixLaunchParams.light[1], optixLaunchParams.light[2] }; const vertex_cu lightDir = lightPos - surfPos; // trace shadow ray: @@ -166,14 +168,14 @@ extern "C" __global__ void __miss__radiance() { vertex_cu &prd = *(vertex_cu *) getPRD(); // set to constant white as background color - prd = vertex_cu(1.f); +// prd = {0, 0, 0}; } extern "C" __global__ void __miss__shadow() { // we didn't hit anything, so the light is visible - vertex_cu &prd = *(vertex_cu *)getPRD(); - prd = vertex_cu(1.f); + vertex_cu &prd = *(vertex_cu *) getPRD(); +// prd = {1, 1, 1}; } //------------------------------------------------------------------------------ @@ -181,36 +183,41 @@ extern "C" __global__ void __miss__shadow() //------------------------------------------------------------------------------ extern "C" __global__ void __raygen__render_frame() { - // compute a test pattern based on pixel ID const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - // our per-ray data for this example. what we initialize it to - // won't matter, since this value will be overwritten by either - // the miss or hit program, anyway - vertex_cu pixelColorPRD = vertex_cu(0.f); + vertex_cu pixelColorPRD; - // the values we store the PRD pointer in: uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); - // normalized screen plane position, in [0,1]^2 - const float xscreen = (ix + .5f) / optixLaunchParams.frame.width; - const float yscreen = (iy + .5f) / optixLaunchParams.frame.height; - - vertex_cu * cam_data = (vertex_cu *) optixLaunchParams.camera; - vertex_cu & position = cam_data[0]; - vertex_cu & direction = cam_data[1]; - vertex_cu & horizontal = cam_data[2]; - vertex_cu & vertical = cam_data[3]; - - // generate ray direction - vertex_cu rayDir = direction + (xscreen - 0.5f) * horizontal + (yscreen - 0.5f) * vertical; - rayDir /= *rayDir; + //const float xscreen = (ix + .5f) / optixLaunchParams.frame.width; + //const float yscreen = (iy + .5f) / optixLaunchParams.frame.height; + glm::vec2 screen = glm::vec2( (ix + .5f) / optixLaunchParams.frame.width, + (iy + .5f) / optixLaunchParams.frame.height + ); + + glm::vec3 cam_pos = glm::vec3( optixLaunchParams.cam_pos[0], + optixLaunchParams.cam_pos[1], + optixLaunchParams.cam_pos[2] + ); + glm::mat4 inv_proj_view; + for(int i = 0; i < 4; ++i) + for(int j = 0; j < 4; ++j) + inv_proj_view[i][j] = optixLaunchParams.inv_proj_view[i * 4 + j]; + + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + glm::vec3 r = p - cam_pos; + + vertex_cu position = {cam_pos.x, cam_pos.y, cam_pos.z}; + vertex_cu ray_dir = {r.x, r.y, r.z}; + ray_dir /= *ray_dir; optixTrace( optixLaunchParams.traversable, position, - rayDir, + ray_dir, 0.f, // tmin 1e20f, // tmax 0.0f, // rayTime From cef701525e4236c2edc104d40db104c8797db324 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 15:49:37 +0100 Subject: [PATCH 0584/1018] rt_optix: setting camera parameters --- include/gproshan/viewer/camera.h | 2 +- src/raytracing/rt_optix.cpp | 10 ++++++++-- src/raytracing/rt_optix.cu | 10 ++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 203f5f63..b9ec27bc 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -20,7 +20,7 @@ class camera public: quaternion eye; - quaternion pos = vertex(0, 0, -3); + quaternion pos = vertex(0, 0, -2); quaternion front = vertex(0, 0, 1); quaternion up = vertex(0, 1, 0); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index bbdd06cf..b89b4a12 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -11,6 +11,7 @@ #include #include +#include #include @@ -143,11 +144,16 @@ void optix::pathtracing( const glm::uvec2 & windows_size, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); - +gproshan_log_var(glm::to_string(cam_pos)); +gproshan_log_var(glm::to_string(inv_proj_view)); memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); memcpy(render_params.cam_pos, glm::value_ptr(cam_pos), sizeof(render_params.cam_pos)); memcpy(render_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(render_params.inv_proj_view)); - + + + for(int i = 0; i < 16; ++i) + gproshan_log_var(render_params.inv_proj_view[i]); + cudaMemcpy(launch_params_buffer, &render_params, sizeof(launch_params), cudaMemcpyHostToDevice); optixLaunch(optix_pipeline, diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index f17441dd..ca753437 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -191,10 +191,8 @@ extern "C" __global__ void __raygen__render_frame() uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); - //const float xscreen = (ix + .5f) / optixLaunchParams.frame.width; - //const float yscreen = (iy + .5f) / optixLaunchParams.frame.height; - glm::vec2 screen = glm::vec2( (ix + .5f) / optixLaunchParams.frame.width, - (iy + .5f) / optixLaunchParams.frame.height + glm::vec2 screen = glm::vec2( (float(ix) + .5f) / optixLaunchParams.frame.width, + (float(iy) + .5f) / optixLaunchParams.frame.height ); glm::vec3 cam_pos = glm::vec3( optixLaunchParams.cam_pos[0], @@ -204,6 +202,7 @@ extern "C" __global__ void __raygen__render_frame() glm::mat4 inv_proj_view; for(int i = 0; i < 4; ++i) for(int j = 0; j < 4; ++j) + //if(ix + iy == 0)printf("m %f\n", optixLaunchParams.inv_proj_view[i * 4 + j]); inv_proj_view[i][j] = optixLaunchParams.inv_proj_view[i * 4 + j]; glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); @@ -215,6 +214,9 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu ray_dir = {r.x, r.y, r.z}; ray_dir /= *ray_dir; + if(ix + iy == 0) + printf("%f %f %f\n", position.x, position.y, position.z); + optixTrace( optixLaunchParams.traversable, position, ray_dir, From 89f1daeb0a2a61a05466bf6090a09876a375ef65 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 20:17:49 +0100 Subject: [PATCH 0585/1018] rt_optix: gen rays program --- include/gproshan/mesh/vertex.cuh | 2 +- include/gproshan/raytracing/rt_optix.h | 6 +- include/gproshan/raytracing/rt_optix_params.h | 2 +- src/raytracing/raytracing.cpp | 8 +-- src/raytracing/rt_optix.cpp | 29 ++++------ src/raytracing/rt_optix.cu | 56 ++++++++++--------- 6 files changed, 50 insertions(+), 53 deletions(-) diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index 754bdef3..a554a171 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -72,7 +72,7 @@ struct vertex_cu __host__ __device__ vertex_cu operator + (const vertex_cu & v) const { - return vertex_cu(x+v.x, y+v.y, z+v.z); + return vertex_cu(x + v.x, y + v.y, z + v.z); } __host__ __device__ diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 5042a9b6..03840cc6 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -54,7 +54,7 @@ class optix : public raytracing ~optix(); virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); - + void pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, @@ -62,12 +62,12 @@ class optix : public raytracing const bool & flat, const bool & restart = false ); - + private: glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - + void create_raygen_programs(); void create_miss_programs(); void create_hitgroup_programs(); diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 8422e577..b4c080fb 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -18,7 +18,7 @@ struct launch_params void * color_buffer = nullptr; uint32_t width, height; } frame; - + float light[3]; float cam_pos[3]; float inv_proj_view[16]; diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 3261663e..018c5a7c 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -55,9 +55,9 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, n_samples = 0; std::default_random_engine gen; - std::uniform_real_distribution randf(0.f, 1.f); + std::uniform_real_distribution randf(0, 1); - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); + glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0, 0, 0, 1)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); glm::vec4 li; @@ -81,11 +81,11 @@ void raytracing::pathtracing( const glm::uvec2 & windows_size, (float(j) + randf(gen)) / height ); - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 view = glm::vec4(screen.x * 2 - 1, screen.y * 2 - 1, 1, 1); glm::vec4 q = inv_proj_view * view; glm::vec3 p = glm::vec3(q * (1.f / q.w)); - li = glm::vec4(0.f); + li = glm::vec4(0); for(auto & l: light) li += intersect_li(cam_pos, glm::normalize(p - cam_pos), l, flat); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index b89b4a12..cd505d55 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -129,14 +129,14 @@ void optix::pathtracing( const glm::uvec2 & windows_size, const bool & flat, const bool & restart ) -{ +{ if(rt_restart(windows_size.x, windows_size.y) || restart) { n_samples = 0; if(render_params.frame.color_buffer) cudaFree(render_params.frame.color_buffer); - + render_params.frame.width = windows_size.x; render_params.frame.height = windows_size.y; cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4)); @@ -144,16 +144,11 @@ void optix::pathtracing( const glm::uvec2 & windows_size, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); -gproshan_log_var(glm::to_string(cam_pos)); -gproshan_log_var(glm::to_string(inv_proj_view)); + memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); memcpy(render_params.cam_pos, glm::value_ptr(cam_pos), sizeof(render_params.cam_pos)); memcpy(render_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(render_params.inv_proj_view)); - - for(int i = 0; i < 16; ++i) - gproshan_log_var(render_params.inv_proj_view[i]); - cudaMemcpy(launch_params_buffer, &render_params, sizeof(launch_params), cudaMemcpyHostToDevice); optixLaunch(optix_pipeline, @@ -190,7 +185,7 @@ void optix::create_raygen_programs() &raygen_programs[0] ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); } void optix::create_miss_programs() @@ -214,7 +209,7 @@ void optix::create_miss_programs() &miss_programs[0] ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); pg_desc.miss.entryFunctionName = "__miss__shadow"; @@ -227,7 +222,7 @@ void optix::create_miss_programs() &miss_programs[1] ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); } void optix::create_hitgroup_programs() @@ -253,7 +248,7 @@ void optix::create_hitgroup_programs() &hitgroup_programs[0] ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__shadow"; @@ -267,7 +262,7 @@ void optix::create_hitgroup_programs() &hitgroup_programs[1] ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); } void optix::create_pipeline() @@ -291,11 +286,11 @@ void optix::create_pipeline() &optix_pipeline ); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); - if(sizeof_log > 1) gproshan_log_var(log); + if(sizeof_log > 1) gproshan_error_var(log); } void optix::build_sbt() @@ -327,7 +322,7 @@ void optix::build_sbt() sbt.missRecordStrideInBytes = sizeof(MissRecord); sbt.missRecordCount = 2; - + std::vector hitgroup_records; for(index_t i = 0; i < d_mesh.size(); ++i) for(index_t r = 0; r < 2; ++r) @@ -337,7 +332,7 @@ void optix::build_sbt() rec.mesh = d_mesh[i]; hitgroup_records.push_back(rec); } - + cudaMalloc(&hitgroup_records_buffer, 2 * sizeof(HitgroupRecord)); cudaMemcpy(hitgroup_records_buffer, hitgroup_records.data(), hitgroup_records.size() * sizeof(HitgroupRecord), cudaMemcpyHostToDevice); sbt.hitgroupRecordBase = (CUdeviceptr) hitgroup_records_buffer; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index ca753437..1d001be4 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -9,8 +9,6 @@ #include #include -#include - // geometry processing and shape analysis framework namespace gproshan::rt { @@ -120,7 +118,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu lightDir = lightPos - surfPos; // trace shadow ray: - vertex_cu lightVisibility = 0.f; + vertex_cu lightVisibility; // the values we store the PRD pointer in: uint32_t u0, u1; packPointer(&lightVisibility, u0, u1); @@ -191,34 +189,38 @@ extern "C" __global__ void __raygen__render_frame() uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); - glm::vec2 screen = glm::vec2( (float(ix) + .5f) / optixLaunchParams.frame.width, - (float(iy) + .5f) / optixLaunchParams.frame.height - ); - - glm::vec3 cam_pos = glm::vec3( optixLaunchParams.cam_pos[0], - optixLaunchParams.cam_pos[1], - optixLaunchParams.cam_pos[2] - ); - glm::mat4 inv_proj_view; - for(int i = 0; i < 4; ++i) - for(int j = 0; j < 4; ++j) - //if(ix + iy == 0)printf("m %f\n", optixLaunchParams.inv_proj_view[i * 4 + j]); - inv_proj_view[i][j] = optixLaunchParams.inv_proj_view[i * 4 + j]; - - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); - glm::vec3 r = p - cam_pos; - - vertex_cu position = {cam_pos.x, cam_pos.y, cam_pos.z}; - vertex_cu ray_dir = {r.x, r.y, r.z}; + const float sx = (float(ix) + .5f) / optixLaunchParams.frame.width; + const float sy = (float(iy) + .5f) / optixLaunchParams.frame.height; + + vertex_cu & cam_pos = *(vertex_cu *) optixLaunchParams.cam_pos; + + vertex_cu ipv[3]; + for(int i = 0; i < 3; ++i) + for(int j = 0; j < 3; ++j) + ipv[i][j] = optixLaunchParams.inv_proj_view[i + j * 4]; + + vertex_cu d = { optixLaunchParams.inv_proj_view[0 * 4 + 3], + optixLaunchParams.inv_proj_view[1 * 4 + 3], + optixLaunchParams.inv_proj_view[2 * 4 + 3] + }; + vertex_cu e = { optixLaunchParams.inv_proj_view[3 * 4 + 0], + optixLaunchParams.inv_proj_view[3 * 4 + 1], + optixLaunchParams.inv_proj_view[3 * 4 + 2] + }; + + float & de = optixLaunchParams.inv_proj_view[15]; + + vertex_cu view = {sx * 2 - 1, sy * 2 - 1, 1}; + vertex_cu q = vertex_cu{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; + vertex_cu p = (1.f / ((d, view) + de)) * q; + vertex_cu ray_dir = p - cam_pos; ray_dir /= *ray_dir; - if(ix + iy == 0) - printf("%f %f %f\n", position.x, position.y, position.z); + if(ix + iy == 0)printf("cam_pos %f %f %f\n", cam_pos.x, cam_pos.y, cam_pos.z); + if(ix + iy == 0)printf("p %f %f %f\n", p.x, p.y, p.z); optixTrace( optixLaunchParams.traversable, - position, + cam_pos, ray_dir, 0.f, // tmin 1e20f, // tmax From 4fd7542837a2ad380100e02f449df4d5c1fb6d47 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 21:31:11 +0100 Subject: [PATCH 0586/1018] rt_optix: rendering without shadows --- include/gproshan/mesh/vertex.cuh | 2 +- src/raytracing/rt_optix.cpp | 6 +- src/raytracing/rt_optix.cu | 148 +++++++++++-------------------- 3 files changed, 57 insertions(+), 99 deletions(-) diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index a554a171..a6776b63 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -44,7 +44,7 @@ struct vertex_cu } __host__ __device__ - real_t operator * () + real_t operator * () const { return sqrt(x * x + y * y + z * z); } diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index cd505d55..61bfdbbd 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -42,7 +42,7 @@ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) MissRecord struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) HitgroupRecord { __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; - CHE * mesh; + CHE * data; }; @@ -329,7 +329,7 @@ void optix::build_sbt() { HitgroupRecord rec; optixSbtRecordPackHeader(hitgroup_programs[r], &rec); - rec.mesh = d_mesh[i]; + rec.data = d_mesh[i]; hitgroup_records.push_back(rec); } @@ -423,7 +423,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u CHE * dd_m, * d_m; CHE h_m(mesh); - cuda_create_CHE(&h_m, dd_m, d_m); + cuda_create_CHE(&h_m, dd_m, d_m, true); dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 1d001be4..bec8f40b 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -26,6 +26,12 @@ vertex_cu operator + (const real_t & a, const vertex_cu & v) return vertex_cu(a + v.x, a + v.y, a + v.z); } +__host__ __device__ +vertex_cu normalize (const vertex_cu & v) +{ + return v / *v; +} + extern "C" __constant__ launch_params optixLaunchParams; @@ -53,132 +59,87 @@ static __forceinline__ __device__ T * getPRD() return reinterpret_cast(unpackPointer(u0, u1)); } -//------------------------------------------------------------------------------ -// closest hit and anyhit programs for radiance-type rays. -// -// Note eventually we will have to create one pair of those for each -// ray type and each geometry type we want to render; but this -// simple example doesn't use any actual geometries yet, so we only -// create a single, dummy, set of them (we do have to have at least -// one group of them to set up the SBT) -//------------------------------------------------------------------------------ extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { - const CHE & sbtData = **(const CHE **) optixGetSbtDataPointer(); + const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); - // ------------------------------------------------------------------ - // gather some basic hit information - // ------------------------------------------------------------------ const int primID = optixGetPrimitiveIndex(); - const index_t he = primID * che::mtrig; + const int he = primID * che::mtrig; const float u = optixGetTriangleBarycentrics().x; const float v = optixGetTriangleBarycentrics().y; - // ------------------------------------------------------------------ - // compute normal, using either shading normal (if avail), or - // geometry normal (fallback) - // ------------------------------------------------------------------ - const vertex_cu & A = sbtData.GT[sbtData.VT[he]]; - const vertex_cu & B = sbtData.GT[sbtData.VT[he + 1]]; - const vertex_cu & C = sbtData.GT[sbtData.VT[he + 2]]; - - vertex_cu Ng = (B - A) * (C - A); - vertex_cu Ns = (sbtData.VN) - ? ((1.f - u - v) * sbtData.VN[sbtData.VT[he]] - + u * sbtData.VN[sbtData.VT[he + 1]] - + v * sbtData.VN[sbtData.VT[he + 2]]) - : Ng; - - // ------------------------------------------------------------------ - // face-forward and normalize normals - // ------------------------------------------------------------------ - const vertex_cu rayDir = (vertex_cu) optixGetWorldRayDirection(); + const int a = mesh.VT[he]; + const int b = mesh.VT[he + 1]; + const int c = mesh.VT[he + 2]; - if((rayDir , Ng) > 0.f) Ng = -Ng; - Ng /= *Ng; + const vertex_cu & A = mesh.GT[a]; + const vertex_cu & B = mesh.GT[b]; + const vertex_cu & C = mesh.GT[c]; - if((Ng , Ns) < 0.f) - Ns -= 2.f * (Ng , Ns) * Ng; - Ns /= *Ns; + const vertex_cu Ng = (B - A) * (C - A); + const vertex_cu normal = mesh.VN ? (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c] : Ng; - // ------------------------------------------------------------------ - // compute diffuse material color, including diffuse texture, if - // available - // ------------------------------------------------------------------ - vertex_cu diffuseColor(230.0/255, 240.0/255, 250.0/255); + const vertex_cu color(230.0f/255, 240.0f/255, 250.0f/255); + const vertex_cu & light = *(vertex_cu *) optixLaunchParams.light; + const vertex_cu position = (1.f - u - v) * A + u * B + v * C; + const vertex_cu wi = normalize(light - position); + const float dot_wi_normal = (wi, normal); - // ------------------------------------------------------------------ - // compute shadow - // ------------------------------------------------------------------ - const vertex_cu surfPos = (1.f - u - v) * A + u * B + v * C; - const vertex_cu lightPos = { optixLaunchParams.light[0], optixLaunchParams.light[1], optixLaunchParams.light[2] }; - const vertex_cu lightDir = lightPos - surfPos; + vertex_cu & L = *(vertex_cu *) getPRD(); + L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; // trace shadow ray: - vertex_cu lightVisibility; + vertex_cu lightVisibility;/* // the values we store the PRD pointer in: + uint32_t u0, u1; packPointer(&lightVisibility, u0, u1); - optixTrace(optixLaunchParams.traversable, - surfPos + 1e-3f * Ng, - lightDir, - 1e-3f, // tmin - 1.f-1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - // For shadow rays: skip any/closest hit shaders and terminate on first - // intersection with anything. The miss shader is used to mark if the - // light was visible. - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - u0, u1); - + optixTrace( optixLaunchParams.traversable, + surfPos + 1e-3f * Ng, + lightDir, + 1e-3f, // tmin + 1.f-1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + // For shadow rays: skip any/closest hit shaders and terminate on first + // intersection with anything. The miss shader is used to mark if the + // light was visible. + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + u0, u1); +*/ // ------------------------------------------------------------------ // final shading: a bit of ambient, a bit of directional ambient, // and directional component based on shadowing // ------------------------------------------------------------------ - const float cosDN = 0.1f + .8f * fabsf((rayDir , Ns)); - - vertex_cu & prd = *(vertex_cu * ) getPRD(); - prd = (.1f + (.2f + .8f * lightVisibility) * cosDN) * diffuseColor; } + extern "C" __global__ void __anyhit__radiance() {} extern "C" __global__ void __anyhit__shadow() {} -//------------------------------------------------------------------------------ -// miss program that gets called for any ray that did not have a -// valid intersection -// -// as with the anyhit/closest hit programs, in this example we only -// need to have _some_ dummy function to set up a valid SBT -// ------------------------------------------------------------------------------ extern "C" __global__ void __miss__radiance() { - vertex_cu &prd = *(vertex_cu *) getPRD(); - // set to constant white as background color -// prd = {0, 0, 0}; + vertex_cu & prd = *(vertex_cu *) getPRD(); + prd = {0, 0, 0}; } extern "C" __global__ void __miss__shadow() { - // we didn't hit anything, so the light is visible - vertex_cu &prd = *(vertex_cu *) getPRD(); -// prd = {1, 1, 1}; + vertex_cu & prd = *(vertex_cu *) getPRD(); + prd = {1, 1, 1}; } -//------------------------------------------------------------------------------ -// ray gen program - the actual rendering happens in here -//------------------------------------------------------------------------------ + extern "C" __global__ void __raygen__render_frame() { const int ix = optixGetLaunchIndex().x; @@ -216,20 +177,17 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu ray_dir = p - cam_pos; ray_dir /= *ray_dir; - if(ix + iy == 0)printf("cam_pos %f %f %f\n", cam_pos.x, cam_pos.y, cam_pos.z); - if(ix + iy == 0)printf("p %f %f %f\n", p.x, p.y, p.z); - optixTrace( optixLaunchParams.traversable, cam_pos, ray_dir, 0.f, // tmin 1e20f, // tmax - 0.0f, // rayTime + 0.0f, // rayTime OptixVisibilityMask(255), OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, - 0, // SBT offset - 2, // SBT stride - 0, // missSBTIndex + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex u0, u1); const uint32_t fbIndex = ix + iy * optixLaunchParams.frame.width; From d85d0a76eaa2a7207901e448836a87efba641b18 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 23:01:05 +0100 Subject: [PATCH 0587/1018] rt_optix: rendering with color --- include/gproshan/mesh/che.cuh | 2 +- include/gproshan/mesh/che.h | 8 +++--- src/mesh/che.cpp | 1 + src/mesh/che.cu | 25 ++++++++++++------ src/raytracing/rt_optix.cpp | 2 +- src/raytracing/rt_optix.cu | 49 ++++++++++++++++------------------- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index 99300a2f..b8dd3ece 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -21,7 +21,7 @@ index_t cu_next(index_t he); __host__ __device__ index_t cu_prev(index_t he); -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false); +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false, const bool & color = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index e9056d9c..34914d13 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -155,16 +155,18 @@ struct vertex_cu; struct CHE { - size_t n_vertices; - size_t n_faces; - size_t n_half_edges; + size_t n_vertices = 0; + size_t n_faces = 0; + size_t n_half_edges = 0; vertex_cu * GT = nullptr; vertex_cu * VN = nullptr; + che::rgb_t * VC = nullptr; index_t * VT = nullptr; index_t * OT = nullptr; index_t * EVT = nullptr; + CHE() = default; CHE(const che * mesh); }; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 6b8a9519..8d3bb837 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -46,6 +46,7 @@ CHE::CHE(const che * mesh) GT = (vertex_cu *) mesh->GT; VN = (vertex_cu *) mesh->VN; + VC = mesh->VC; VT = mesh->VT; OT = mesh->OT; EVT = mesh->EVT; diff --git a/src/mesh/che.cu b/src/mesh/che.cu index 6119975f..6a1f4290 100644 --- a/src/mesh/che.cu +++ b/src/mesh/che.cu @@ -26,10 +26,12 @@ index_t cu_prev(index_t he) return che::mtrig * cu_trig(he) + (he + che::mtrig - 1) % che::mtrig; } -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal) +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal, const bool & color) { - dd_che = (CHE *) malloc(sizeof(CHE)); - memcpy(dd_che, h_che, sizeof(CHE)); + dd_che = new CHE; + dd_che->n_vertices = h_che->n_vertices; + dd_che->n_faces = h_che->n_faces; + dd_che->n_half_edges = h_che->n_half_edges; cudaMalloc(&dd_che->GT, sizeof(vertex_cu) * h_che->n_vertices); cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); @@ -40,6 +42,12 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm cudaMemcpy(dd_che->VN, h_che->VN, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); } + if(color) + { + cudaMalloc(&dd_che->VC, sizeof(che::rgb_t) * h_che->n_vertices); + cudaMemcpy(dd_che->VC, h_che->VC, sizeof(che::rgb_t) * h_che->n_vertices, cudaMemcpyHostToDevice); + } + cudaMalloc(&dd_che->VT, sizeof(index_t) * h_che->n_half_edges); cudaMemcpy(dd_che->VT, h_che->VT, sizeof(index_t) * h_che->n_half_edges, cudaMemcpyHostToDevice); @@ -55,11 +63,12 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm void cuda_free_CHE(CHE *& dd_che, CHE *& d_che) { - if(dd_che->GT) cudaFree(dd_che->GT); - if(dd_che->VN) cudaFree(dd_che->VN); - if(dd_che->VT) cudaFree(dd_che->VT); - if(dd_che->OT) cudaFree(dd_che->OT); - if(dd_che->EVT) cudaFree(dd_che->EVT); + cudaFree(dd_che->GT); + cudaFree(dd_che->VN); + cudaFree(dd_che->VC); + cudaFree(dd_che->VT); + cudaFree(dd_che->OT); + cudaFree(dd_che->EVT); free(dd_che); cudaFree(d_che); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 61bfdbbd..53ae7a33 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -423,7 +423,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u CHE * dd_m, * d_m; CHE h_m(mesh); - cuda_create_CHE(&h_m, dd_m, d_m, true); + cuda_create_CHE(&h_m, dd_m, d_m, true, true); dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index bec8f40b..9d2c0666 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -79,29 +79,31 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu & B = mesh.GT[b]; const vertex_cu & C = mesh.GT[c]; - const vertex_cu Ng = (B - A) * (C - A); + const vertex_cu Ng = normalize((B - A) * (C - A)); const vertex_cu normal = mesh.VN ? (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c] : Ng; - const vertex_cu color(230.0f/255, 240.0f/255, 250.0f/255); + const vertex_cu ca(mesh.VC[a].r, mesh.VC[a].g, mesh.VC[a].b); + const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); + const vertex_cu cc(mesh.VC[c].r, mesh.VC[c].g, mesh.VC[c].b); + + const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; + const vertex_cu & light = *(vertex_cu *) optixLaunchParams.light; const vertex_cu position = (1.f - u - v) * A + u * B + v * C; const vertex_cu wi = normalize(light - position); const float dot_wi_normal = (wi, normal); - vertex_cu & L = *(vertex_cu *) getPRD(); + vertex_cu & L = *getPRD(); L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; - // trace shadow ray: - vertex_cu lightVisibility;/* - // the values we store the PRD pointer in: - + bool occluded = true; uint32_t u0, u1; - packPointer(&lightVisibility, u0, u1); + packPointer(&occluded, u0, u1); optixTrace( optixLaunchParams.traversable, - surfPos + 1e-3f * Ng, - lightDir, - 1e-3f, // tmin - 1.f-1e-3f, // tmax + position + 1e-5f * Ng, + wi, + 1e-5f, // tmin + 1e20f, // tmax 0.0f, // rayTime OptixVisibilityMask(255), // For shadow rays: skip any/closest hit shaders and terminate on first @@ -114,11 +116,8 @@ extern "C" __global__ void __closesthit__radiance() 2, // SBT stride 1, // missSBTIndex u0, u1); -*/ - // ------------------------------------------------------------------ - // final shading: a bit of ambient, a bit of directional ambient, - // and directional component based on shadowing - // ------------------------------------------------------------------ + + if(occluded) L = 0.6 * L; } @@ -129,14 +128,14 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vertex_cu & prd = *(vertex_cu *) getPRD(); - prd = {0, 0, 0}; + bool & prd = *getPRD(); + prd = false; } extern "C" __global__ void __miss__shadow() { - vertex_cu & prd = *(vertex_cu *) getPRD(); - prd = {1, 1, 1}; + bool & prd = *getPRD(); + prd = false; } @@ -145,11 +144,6 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - vertex_cu pixelColorPRD; - - uint32_t u0, u1; - packPointer(&pixelColorPRD, u0, u1); - const float sx = (float(ix) + .5f) / optixLaunchParams.frame.width; const float sy = (float(iy) + .5f) / optixLaunchParams.frame.height; @@ -177,6 +171,9 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu ray_dir = p - cam_pos; ray_dir /= *ray_dir; + vertex_cu pixelColorPRD; + uint32_t u0, u1; + packPointer(&pixelColorPRD, u0, u1); optixTrace( optixLaunchParams.traversable, cam_pos, ray_dir, From 3db00db00631e7a63b8af0849a262652b22c5287 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 23:23:49 +0100 Subject: [PATCH 0588/1018] rt_optix: render flat --- include/gproshan/raytracing/raytracing.h | 20 +++++++++++-------- include/gproshan/raytracing/rt_optix.h | 5 ----- include/gproshan/raytracing/rt_optix_params.h | 1 + src/raytracing/rt_optix.cpp | 16 +-------------- src/raytracing/rt_optix.cu | 9 +++++---- 5 files changed, 19 insertions(+), 32 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index af9d33ce..c267da69 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -56,16 +56,20 @@ class raytracing const index_t & samples = 4 ); - virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir) = 0; + virtual index_t cast_ray( const glm::vec3 &,// org, + const glm::vec3 &// dir + ) { return NIL; }; protected: - virtual glm::vec4 intersect_li( const glm::vec3 & org, - const glm::vec3 & dir, - const glm::vec3 & light, - const bool & flat ) = 0; - - virtual float intersect_depth( const glm::vec3 & org, - const glm::vec3 & dir ) = 0; + virtual glm::vec4 intersect_li( const glm::vec3 &,// org, + const glm::vec3 &,// dir, + const glm::vec3 &,// light, + const bool &// flat + ) { return glm::vec4(0); }; + + virtual float intersect_depth( const glm::vec3 &,// org, + const glm::vec3 &// dir + ) { return 0; }; }; diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 03840cc6..027fddb9 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -53,8 +53,6 @@ class optix : public raytracing optix(const std::vector & meshes); ~optix(); - virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); - void pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, @@ -65,9 +63,6 @@ class optix : public raytracing private: - glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); - float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); - void create_raygen_programs(); void create_miss_programs(); void create_hitgroup_programs(); diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index b4c080fb..362c2a9f 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -19,6 +19,7 @@ struct launch_params uint32_t width, height; } frame; + bool flat; float light[3]; float cam_pos[3]; float inv_proj_view[16]; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 53ae7a33..c84e09d5 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -117,11 +117,6 @@ optix::~optix() cuda_free_CHE(dd_mesh[i], d_mesh[i]); } -index_t optix::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) -{ - return NIL; -} - void optix::pathtracing( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, @@ -145,6 +140,7 @@ void optix::pathtracing( const glm::uvec2 & windows_size, glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + render_params.flat = flat; memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); memcpy(render_params.cam_pos, glm::value_ptr(cam_pos), sizeof(render_params.cam_pos)); memcpy(render_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(render_params.inv_proj_view)); @@ -452,16 +448,6 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; } -glm::vec4 optix::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat) -{ - return glm::vec4(0.f); -} - -float optix::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) -{ - return 0; -} - } // namespace gproshan diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 9d2c0666..046f4d90 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -80,7 +80,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu & C = mesh.GT[c]; const vertex_cu Ng = normalize((B - A) * (C - A)); - const vertex_cu normal = mesh.VN ? (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c] : Ng; + const vertex_cu normal = optixLaunchParams.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; const vertex_cu ca(mesh.VC[a].r, mesh.VC[a].g, mesh.VC[a].b); const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); @@ -95,7 +95,7 @@ extern "C" __global__ void __closesthit__radiance() vertex_cu & L = *getPRD(); L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; - +/* bool occluded = true; uint32_t u0, u1; packPointer(&occluded, u0, u1); @@ -118,6 +118,7 @@ extern "C" __global__ void __closesthit__radiance() u0, u1); if(occluded) L = 0.6 * L; +*/ } @@ -128,8 +129,8 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - bool & prd = *getPRD(); - prd = false; + vertex_cu & prd = *getPRD(); + prd = {0, 0,0}; } extern "C" __global__ void __miss__shadow() From 68442ff1587a94f2f78a04cac33d4a7875577465 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 23:25:45 +0100 Subject: [PATCH 0589/1018] raytracing: renaming to render method --- include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/raytracing/rt_optix.h | 2 +- src/raytracing/raytracing.cpp | 2 +- src/raytracing/rt_optix.cpp | 2 +- src/viewer/viewer.cpp | 9 +++++---- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index c267da69..28e6d5c7 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -42,7 +42,7 @@ class raytracing virtual ~raytracing(); virtual bool rt_restart(const size_t & w, const size_t & h); - virtual void pathtracing( const glm::uvec2 & windows_size, + virtual void render( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 027fddb9..f6daf48b 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -53,7 +53,7 @@ class optix : public raytracing optix(const std::vector & meshes); ~optix(); - void pathtracing( const glm::uvec2 & windows_size, + void render( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 018c5a7c..493c8c69 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -44,7 +44,7 @@ bool raytracing::rt_restart(const size_t & w, const size_t & h) return false; } -void raytracing::pathtracing( const glm::uvec2 & windows_size, +void raytracing::render( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index c84e09d5..5023d3a7 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -117,7 +117,7 @@ optix::~optix() cuda_free_CHE(dd_mesh[i], d_mesh[i]); } -void optix::pathtracing( const glm::uvec2 & windows_size, +void optix::render( const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 405b4ffb..b4bd4b98 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -841,7 +841,7 @@ void viewer::render_gl() #ifdef GPROSHAN_EMBREE void viewer::render_embree() { - rt_embree->pathtracing( glm::uvec2(viewport_width, viewport_height), + rt_embree->render( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, active_mesh().render_flat, action ); @@ -865,12 +865,13 @@ void viewer::render_optix() gproshan_log_var(time_build_optix); } - rt_optix->pathtracing( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm_vec3(light)}, action); + rt_optix->render( glm::uvec2(viewport_width, viewport_height), + view_mat, proj_mat, {glm_vec3(light)}, + active_mesh().render_flat, action); if(!render_frame) render_frame = new frame; - + action = false; render_frame->display(viewport_width, viewport_height, rt_optix->img); } From 7975bc5eaff178911f97569a9e54b9d7f3a17e80 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Nov 2021 23:39:05 +0100 Subject: [PATCH 0590/1018] rt_optix: cleaning up ptx programs --- src/raytracing/rt_optix.cu | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 046f4d90..a8de785d 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -14,19 +14,13 @@ namespace gproshan::rt { -__host__ __device__ +static __forceinline__ __device__ vertex_cu operator * (const real_t & a, const vertex_cu & v) { return vertex_cu(a * v.x, a * v.y, a * v.z); } -__host__ __device__ -vertex_cu operator + (const real_t & a, const vertex_cu & v) -{ - return vertex_cu(a + v.x, a + v.y, a + v.z); -} - -__host__ __device__ +static __forceinline__ __device__ vertex_cu normalize (const vertex_cu & v) { return v / *v; From a36614d422c9355c42bb87a37fe6df49491444d2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 16 Nov 2021 12:06:27 +0100 Subject: [PATCH 0591/1018] rt_optix: shadow rays --- src/raytracing/rt_embree.cpp | 4 ++-- src/raytracing/rt_optix.cpp | 10 ++-------- src/raytracing/rt_optix.cu | 27 ++++++++++----------------- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 6d08b223..4ca98155 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -255,10 +255,10 @@ glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const { const glm::vec3 wi = normalize(light - position); const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec4 L = glm::vec4((dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color, 1); + const glm::vec3 L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; ray_hit r(position, wi, near); - return (occluded(r) ? 0.6f : 1.f) * L; + return glm::vec4((occluded(r) ? 0.4f : 1.f) * L, 1); } glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 5023d3a7..a6360b08 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -20,21 +20,15 @@ namespace gproshan::rt { -/*! SBT record for a raygen program */ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) RaygenRecord { __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; - // just a dummy value - later examples will use more interesting - // data here void * data; }; -/*! SBT record for a miss program */ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) MissRecord { __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; - // just a dummy value - later examples will use more interesting - // data here void * data; }; @@ -313,7 +307,7 @@ void optix::build_sbt() } cudaMalloc(&miss_records_buffer, 2 * sizeof(MissRecord)); - cudaMemcpy(miss_records_buffer, miss_records, sizeof(MissRecord), cudaMemcpyHostToDevice); + cudaMemcpy(miss_records_buffer, miss_records, 2 * sizeof(MissRecord), cudaMemcpyHostToDevice); sbt.missRecordBase = (CUdeviceptr) miss_records_buffer; sbt.missRecordStrideInBytes = sizeof(MissRecord); sbt.missRecordCount = 2; @@ -329,7 +323,7 @@ void optix::build_sbt() hitgroup_records.push_back(rec); } - cudaMalloc(&hitgroup_records_buffer, 2 * sizeof(HitgroupRecord)); + cudaMalloc(&hitgroup_records_buffer, hitgroup_records.size() * sizeof(HitgroupRecord)); cudaMemcpy(hitgroup_records_buffer, hitgroup_records.data(), hitgroup_records.size() * sizeof(HitgroupRecord), cudaMemcpyHostToDevice); sbt.hitgroupRecordBase = (CUdeviceptr) hitgroup_records_buffer; sbt.hitgroupRecordStrideInBytes = sizeof(HitgroupRecord); diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index a8de785d..2a675606 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -80,39 +80,33 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); const vertex_cu cc(mesh.VC[c].r, mesh.VC[c].g, mesh.VC[c].b); - const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; - const vertex_cu & light = *(vertex_cu *) optixLaunchParams.light; + const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex_cu position = (1.f - u - v) * A + u * B + v * C; + const vertex_cu wi = normalize(light - position); const float dot_wi_normal = (wi, normal); vertex_cu & L = *getPRD(); L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; -/* - bool occluded = true; - uint32_t u0, u1; - packPointer(&occluded, u0, u1); + + unsigned int occluded = 1; optixTrace( optixLaunchParams.traversable, - position + 1e-5f * Ng, + position, wi, 1e-5f, // tmin 1e20f, // tmax 0.0f, // rayTime OptixVisibilityMask(255), - // For shadow rays: skip any/closest hit shaders and terminate on first - // intersection with anything. The miss shader is used to mark if the - // light was visible. OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT, + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, 1, // SBT offset 2, // SBT stride 1, // missSBTIndex - u0, u1); + occluded); - if(occluded) L = 0.6 * L; -*/ + if(occluded) L = 0.4 * L; } @@ -129,8 +123,7 @@ extern "C" __global__ void __miss__radiance() extern "C" __global__ void __miss__shadow() { - bool & prd = *getPRD(); - prd = false; + optixSetPayload_0(0); } From f296680fc634d2116b272e397c2d07fe28eac4b0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 16 Nov 2021 19:12:34 +0100 Subject: [PATCH 0592/1018] viewer: fix camera eye parameter --- src/raytracing/rt_optix.cu | 2 +- src/viewer/camera.cpp | 6 +++--- src/viewer/viewer.cpp | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 2a675606..80555850 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -83,7 +83,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu & light = *(vertex_cu *) optixLaunchParams.light; const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex_cu position = (1.f - u - v) * A + u * B + v * C; - + const vertex_cu wi = normalize(light - position); const float dot_wi_normal = (wi, normal); diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 4871172f..95affdc4 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -14,9 +14,9 @@ namespace gproshan { glm::mat4 camera::look_at(const quaternion & r) { - eye = r.conj() * (pos + front) * r; + eye = r.conj() * pos * r; - return glm::lookAt(glm_vec3(r.conj() * pos * r), glm_vec3(eye), glm_vec3(r.conj() * up * r)); + return glm::lookAt(glm_vec3(eye), glm_vec3(r.conj() * (pos + front) * r), glm_vec3(r.conj() * up * r)); } quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) @@ -38,7 +38,7 @@ quaternion camera::click_to_sphere(const double & x, const double & y, const int quaternion camera::current_rotation() const { - return (p_drag * p_click.conj()) * r_last; + return p_drag * p_click.conj() * r_last; } void camera::mouse(const bool & press, const double & x, const double & y, const int & w, const int & h) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index b4bd4b98..e7ed490c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -856,13 +856,13 @@ void viewer::render_optix() { if(!rt_optix) { - double time_build_optix; - TIC(time_build_optix); + static double time; + TIC(time); rt_optix = new rt::optix({active_mesh()}); - TOC(time_build_optix); - gproshan_log_var(time_build_optix); + TOC(time); + sprintf(status_message, "build optix in %.3fs", time); } rt_optix->render( glm::uvec2(viewport_width, viewport_height), From d69a3490923224d7a6dab244df675095133c6e42 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 16 Nov 2021 19:17:28 +0100 Subject: [PATCH 0593/1018] updating CHANGELOG --- CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69544fb5..3114c3da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,13 @@ Version History --------------- ### gproshan 3.0.0 -- Add module geometry, including a 2D convex hull algorithm implementation. +- Added render option using [OptiX](https://developer.nvidia.com/optix) ray tracing mesh, shadows. +- Add module geometry, including a 2D convex hull algorithm implementation and connected components detection. - Supported file mesh types include: off, obj, ply, ptx, xyz, and any depth image or image file loaded as a mesh. - Upgraded version of geodesics module: fast marching algorithm, heat method, and parallel topleset propagation algorithm from our published paper. - Upgraded version of the sparse mesh coding with feature aware sampling module and published paper reference. - Updated save mesh with options for file types, normals, and point-cloud. -- Added render option using [Embree](https://www.embree.org/) ray tracing mesh, point-cloud disks, and splats. +- Added render option using [Embree](https://www.embree.org/) ray tracing mesh, shadows, point-cloud disks, and splats. - Implemented the loading and rendering of point clouds. - Added heatmap viewer options and loading vertex color from a file. - New user interface implemented with [ImGui](https://github.com/ocornut/imgui). From 660ed41a818f9cef7b69b65d0e47129ee371c512 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Nov 2021 13:05:12 +0100 Subject: [PATCH 0594/1018] viewer: refactoring raytracing methods --- include/gproshan/viewer/viewer.h | 7 +-- src/viewer/viewer.cpp | 74 ++++++++++++++------------------ 2 files changed, 33 insertions(+), 48 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 9367dce4..a0005390 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -120,12 +120,7 @@ class viewer void init_glsl(); void render_gl(); - #ifdef GPROSHAN_EMBREE - void render_embree(); - #endif // GPROSHAN_EMBREE - #ifdef GPROSHAN_OPTIX - void render_optix(); - #endif // GPROSHAN_OPTIX + void render_rt(rt::raytracing * rt); static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e7ed490c..46edc08c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -98,13 +98,15 @@ bool viewer::run() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) { - case R_GL: render_gl(); break; - #ifdef GPROSHAN_EMBREE - case R_EMBREE: render_embree(); break; - #endif // GPROSHAN_EMBREE - #ifdef GPROSHAN_OPTIX - case R_OPTIX: render_optix(); break; - #endif // GPROSHAN_OPTIX + case R_GL: + render_gl(); + break; + case R_EMBREE: + render_rt(rt_embree); + break; + case R_OPTIX: + render_rt(rt_optix); + break; } ImGui_ImplOpenGL3_NewFrame(); @@ -694,7 +696,23 @@ bool viewer::set_render_embree(viewer * view) #ifdef GPROSHAN_OPTIX bool viewer::set_render_optix(viewer * view) { - view->render_opt = 2; + che_viewer & mesh = view->active_mesh(); + + view->render_opt = R_OPTIX; + + if(!view->rt_optix) + { + static double time; + TIC(time); + + view->rt_optix = new rt::optix({mesh}); + + TOC(time); + sprintf(view->status_message, "build optix in %.3fs", time); + } + + if(!view->render_frame) + view->render_frame = new frame; return false; } @@ -838,44 +856,16 @@ void viewer::render_gl() } } -#ifdef GPROSHAN_EMBREE -void viewer::render_embree() +void viewer::render_rt(rt::raytracing * rt) { - rt_embree->render( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm_vec3(light)}, - active_mesh().render_flat, action - ); + rt->render( glm::uvec2(viewport_width, viewport_height), + view_mat, proj_mat, {glm_vec3(light)}, + active_mesh().render_flat, action + ); action = false; - render_frame->display(viewport_width, viewport_height, rt_embree->img); + render_frame->display(viewport_width, viewport_height, rt->img); } -#endif // GPROSHAN_EMBREE - -#ifdef GPROSHAN_OPTIX -void viewer::render_optix() -{ - if(!rt_optix) - { - static double time; - TIC(time); - - rt_optix = new rt::optix({active_mesh()}); - - TOC(time); - sprintf(status_message, "build optix in %.3fs", time); - } - - rt_optix->render( glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm_vec3(light)}, - active_mesh().render_flat, action); - - if(!render_frame) - render_frame = new frame; - - action = false; - render_frame->display(viewport_width, viewport_height, rt_optix->img); -} -#endif // GPROSHAN_OPTIX void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) { From 790d099c87f9cd846cb8d0428d3e7050c929047a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 18 Nov 2021 16:30:38 +0100 Subject: [PATCH 0595/1018] viewer: ui for building and rendering with raytracing --- include/gproshan/viewer/viewer.h | 11 ++- src/viewer/viewer.cpp | 120 +++++++++++++++---------------- 2 files changed, 62 insertions(+), 69 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a0005390..c511f5ba 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -75,7 +75,7 @@ class viewer che_viewer meshes[N_MESHES]; size_t n_meshes = 0; - index_t idx_active_mesh = 0; // idx_active_mesh mesh + index_t idx_active_mesh = 0; enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; index_t render_opt = R_GL; @@ -140,15 +140,12 @@ class viewer static bool menu_bgc_white(viewer * view); static bool menu_bgc_black(viewer * view); - // render options - static bool invert_orientation(viewer * view); + static bool setup_raytracing(viewer * view); static bool set_render_gl(viewer * view); - #ifdef GPROSHAN_EMBREE static bool set_render_embree(viewer * view); - #endif // GPROSHAN_EMBREE - #ifdef GPROSHAN_OPTIX static bool set_render_optix(viewer * view); - #endif // GPROSHAN_OPTIX + + static bool invert_orientation(viewer * view); static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); static bool set_render_triangles(viewer * view); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 46edc08c..b7ce5b84 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -295,6 +295,7 @@ void viewer::init_menus() add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); add_process(GLFW_KEY_F7, {"F7", "Render Triangles", set_render_triangles}); add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); + add_process(GLFW_KEY_R, {"R", "Setup Raytracing", setup_raytracing}); #ifdef GPROSHAN_EMBREE add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); @@ -629,94 +630,87 @@ bool viewer::menu_bgc_black(viewer * view) return false; } -bool viewer::invert_orientation(viewer * view) -{ - view->active_mesh().invert_orientation(); - view->active_mesh().update_vbo_normal(); - - return false; -} - -bool viewer::set_render_gl(viewer * view) -{ - view->render_opt = R_GL; - - return false; -} - -#ifdef GPROSHAN_EMBREE -bool viewer::set_render_embree(viewer * view) +bool viewer::setup_raytracing(viewer * view) { che_viewer & mesh = view->active_mesh(); + static int rt = 0; static int rt_opt = 0; static double time = 0; - ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); - ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); + ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); - if(!view->rt_embree) + if(rt != 0) { - if(ImGui::Button("Start")) - { - view->render_opt = R_EMBREE; - - TIC(time); - switch(rt_opt) - { - case 0: view->rt_embree = new rt::embree({mesh}); - break; - case 1: view->rt_embree = new rt::embree_splat({mesh}, true); - break; - } - TOC(time); - sprintf(view->status_message, "build embree in %.3fs", time); - - if(!view->render_frame) - view->render_frame = new frame; - } + ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); + ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); } - else + + if(ImGui::Button("Build")) { - view->render_opt = R_EMBREE; + if(!view->render_frame) + view->render_frame = new frame; - if(ImGui::Button("Restart")) + switch(rt) { - delete view->rt_embree; - view->rt_embree = nullptr; + case R_GL: break; + + case R_EMBREE: + #ifdef GPROSHAN_EMBREE + delete view->rt_embree; + TIC(time); + switch(rt_opt) + { + case 0: view->rt_embree = new rt::embree({mesh}); + break; + case 1: view->rt_embree = new rt::embree_splat({mesh}, true); + break; + } + TOC(time); + sprintf(view->status_message, "build embree in %.3fs", time); + #endif // GPROSHAN_EMBREE + break; - view->render_opt = R_GL; + case R_OPTIX: + #ifdef GPROSHAN_OPTIX + delete view->rt_optix; + TIC(time); + view->rt_optix = new rt::optix({mesh}); + TOC(time); + sprintf(view->status_message, "build optix in %.3fs", time); + #endif // GPROSHAN_OPTIX + break; } } return true; } -#endif // GPROSHAN_EMBREE -#ifdef GPROSHAN_OPTIX -bool viewer::set_render_optix(viewer * view) +bool viewer::set_render_gl(viewer * view) { - che_viewer & mesh = view->active_mesh(); - - view->render_opt = R_OPTIX; - - if(!view->rt_optix) - { - static double time; - TIC(time); + view->render_opt = R_GL; + return false; +} - view->rt_optix = new rt::optix({mesh}); +bool viewer::set_render_embree(viewer * view) +{ + view->render_opt = R_EMBREE; + return false; +} - TOC(time); - sprintf(view->status_message, "build optix in %.3fs", time); - } +bool viewer::set_render_optix(viewer * view) +{ + view->render_opt = R_OPTIX; + return false; +} - if(!view->render_frame) - view->render_frame = new frame; +bool viewer::invert_orientation(viewer * view) +{ + view->active_mesh().invert_orientation(); + view->active_mesh().update_vbo_normal(); return false; } -#endif // GPROSHAN_OPTIX bool viewer::set_render_pointcloud(viewer * view) { @@ -858,6 +852,8 @@ void viewer::render_gl() void viewer::render_rt(rt::raytracing * rt) { + if(!rt) return; + rt->render( glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, active_mesh().render_flat, action From a59f0bbbd0d4bf79d98a2b82bb1e3d5b74969e64 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 24 Nov 2021 15:50:44 +0100 Subject: [PATCH 0596/1018] viewer: rt splats input radius --- src/viewer/viewer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index b7ce5b84..1ed58d99 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -643,7 +643,8 @@ bool viewer::setup_raytracing(viewer * view) if(rt != 0) { ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); - ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); + + if(rt_opt) ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); } if(ImGui::Button("Build")) @@ -694,12 +695,14 @@ bool viewer::set_render_gl(viewer * view) bool viewer::set_render_embree(viewer * view) { + view->action = true; view->render_opt = R_EMBREE; return false; } bool viewer::set_render_optix(viewer * view) { + view->action = true; view->render_opt = R_OPTIX; return false; } From d3c17376ccd0ffd14f340f1d6eff9401d5aa4262 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 25 Nov 2021 18:13:14 +0100 Subject: [PATCH 0597/1018] viewer: measuring and displaying FPS --- src/viewer/viewer.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index b7ce5b84..197cf63b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -84,12 +84,15 @@ viewer::~viewer() bool viewer::run() { + double render_time = 0; + while(!glfwWindowShouldClose(window)) { - light = vertex(-1, 1, -2); + TIC(render_time) quaternion r = cam.current_rotation(); + light = vertex(-1, 1, -2); light = r.conj() * light * r; view_mat = cam.look_at(r); @@ -108,6 +111,9 @@ bool viewer::run() render_rt(rt_optix); break; } + + TOC(render_time); + ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); @@ -173,6 +179,7 @@ bool viewer::run() ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); ImGui::Text("%s", mesh->filename.c_str()); + ImGui::Text("%16s: %.3f", "FPS", 1.0 / render_time); ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); ImGui::DragFloat4("camera center", (float *) &cam.pos, 0.2); From 8669b1bd6b1c69e35985e5b70fb28e2d69241454 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 29 Nov 2021 11:57:58 +0100 Subject: [PATCH 0598/1018] viewer: ray tracing point cloud --- include/gproshan/raytracing/rt_embree.h | 6 +++--- include/gproshan/raytracing/rt_embree_splat.h | 2 +- src/raytracing/rt_embree.cpp | 4 ++-- src/viewer/viewer.cpp | 8 ++++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 1466419b..6b81f3b0 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -38,12 +38,12 @@ class embree : public raytracing RTCScene scene; RTCIntersectContext intersect_context; - public: - static float pc_radius; + protected: + float pc_radius = 1; public: embree(); - embree(const std::vector & meshes, const bool & pointcloud = false); + embree(const std::vector & meshes, const bool & pointcloud = false, const float & pcr = 1); virtual ~embree(); virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); diff --git a/include/gproshan/raytracing/rt_embree_splat.h b/include/gproshan/raytracing/rt_embree_splat.h index 9991bc9d..1240b985 100644 --- a/include/gproshan/raytracing/rt_embree_splat.h +++ b/include/gproshan/raytracing/rt_embree_splat.h @@ -42,7 +42,7 @@ class embree_splat : public embree normal = glm::vec3(0); color = glm::vec3(0); - float w, sum_w = 0, sigma = radio * pc_radius; + float w, sum_w = 0, sigma = radio;// * pc_radius; for(index_t i = 0; i < K; ++i) { diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 4ca98155..43ae001d 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -94,7 +94,6 @@ void embree_error(void *, RTCError, const char * str) fprintf(stderr, "EMBREE ERROR: %s\n", str); } -float embree::pc_radius = 0.005; embree::embree() { @@ -107,8 +106,9 @@ embree::embree() rtcSetDeviceErrorFunction(device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const bool & pointcloud): embree() +embree::embree(const std::vector & meshes, const bool & pointcloud, const float & pcr): embree() { + pc_radius = pcr; build_bvh(meshes, pointcloud); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index d985557c..1e217be0 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -111,7 +111,7 @@ bool viewer::run() render_rt(rt_optix); break; } - + TOC(render_time); @@ -644,14 +644,14 @@ bool viewer::setup_raytracing(viewer * view) static int rt = 0; static int rt_opt = 0; static double time = 0; + static float pc_radius = 0.01; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); if(rt != 0) { ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); - - if(rt_opt) ImGui::InputFloat("pc_radius", &rt::embree::pc_radius, 0, 0, "%.3f"); + ImGui::InputFloat("pc_radius (if render_pointcloud)", &pc_radius, 0, 0, "%.3f"); } if(ImGui::Button("Build")) @@ -669,7 +669,7 @@ bool viewer::setup_raytracing(viewer * view) TIC(time); switch(rt_opt) { - case 0: view->rt_embree = new rt::embree({mesh}); + case 0: view->rt_embree = new rt::embree({mesh}, mesh.render_pointcloud, pc_radius); break; case 1: view->rt_embree = new rt::embree_splat({mesh}, true); break; From 3b3cfe188bc8ac6515ecd34e8f56c1d59b5335a0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 29 Nov 2021 18:04:25 +0100 Subject: [PATCH 0599/1018] raytracing: using pbo on render frame --- include/gproshan/viewer/frame.h | 12 ++++++--- src/viewer/frame.cpp | 48 ++++++++++++++++++++++----------- src/viewer/viewer.cpp | 2 +- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index d6f3c267..ed00f2b9 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -14,16 +14,20 @@ namespace gproshan { class frame { private: - GLuint render_tex; - GLuint vao, vbo; + GLuint render_tex = 0; + GLuint vao = 0; + GLuint vbo = 0; + GLuint pbo = 0; + + size_t pbo_size = 0; shader program; public: - frame(); + frame(const size_t & width, const size_t & height); ~frame(); - void display(const int & width, const int & height, void * buffer); + void display(const int & width, const int & height, void * buffer = nullptr); }; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 7871201f..796716ee 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -2,15 +2,15 @@ #include "include.h" +#include + + // geometry processing and shape analysis framework namespace gproshan { -frame::frame() +frame::frame(const size_t & width, const size_t & height) { - render_tex = 0; - vbo = 0; - program.load_vertex(shaders_path("vertex_frame.glsl")); program.load_fragment(shaders_path("fragment_frame.glsl")); @@ -24,27 +24,33 @@ frame::frame() 1, 1, 0, }; - glGenTextures(1, &render_tex); + glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); - glBindVertexArray(vao); + pbo_size = 4 * sizeof(float) * width * height; + + glGenBuffers(1, &pbo); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + glBufferData(GL_PIXEL_UNPACK_BUFFER, pbo_size, 0, GL_DYNAMIC_COPY); + + glGenTextures(1, &render_tex); glBindTexture(GL_TEXTURE_2D, render_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, 0); - - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } frame::~frame() @@ -52,10 +58,18 @@ frame::~frame() glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); glDeleteTextures(1, &render_tex); + glDeleteBuffers(1, &pbo); } void frame::display(const int & width, const int & height, void * buffer) { + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + void * ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); + memcpy(ptr, buffer, pbo_size); + glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + + program.enable(); glViewport(0, 0, width, height); @@ -64,7 +78,9 @@ void frame::display(const int & width, const int & height, void * buffer) glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, render_tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, buffer); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, 0); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo); glDrawArrays(GL_TRIANGLES, 0, 6); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 1e217be0..9697837d 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -657,7 +657,7 @@ bool viewer::setup_raytracing(viewer * view) if(ImGui::Button("Build")) { if(!view->render_frame) - view->render_frame = new frame; + view->render_frame = new frame(view->viewport_width, view->viewport_height); switch(rt) { From b39d7184f6e7f15fcc47f3044175e69ba7c9094d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 29 Nov 2021 19:46:58 +0100 Subject: [PATCH 0600/1018] raytracing: using opengl buffer to render --- include/gproshan/raytracing/raytracing.h | 15 ++---- include/gproshan/raytracing/rt_optix.h | 15 +++--- include/gproshan/viewer/frame.h | 6 ++- src/raytracing/raytracing.cpp | 69 +++++------------------- src/raytracing/rt_optix.cpp | 17 +++--- src/viewer/frame.cpp | 18 +++---- src/viewer/viewer.cpp | 10 +++- 7 files changed, 55 insertions(+), 95 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 28e6d5c7..6266362f 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -30,19 +30,14 @@ class raytracing std::map geomID_mesh; - size_t width; - size_t height; - size_t n_samples; + size_t n_samples = 0; public: - glm::vec4 * img = nullptr; + raytracing() = default; + virtual ~raytracing() = default; - public: - raytracing(); - virtual ~raytracing(); - - virtual bool rt_restart(const size_t & w, const size_t & h); - virtual void render( const glm::uvec2 & windows_size, + virtual void render(glm::vec4 * img, + const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat, const std::vector & light, diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index f6daf48b..cef21695 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -53,13 +53,14 @@ class optix : public raytracing optix(const std::vector & meshes); ~optix(); - void render( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const std::vector & light, - const bool & flat, - const bool & restart = false - ); + void render(glm::vec4 * img, + const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const std::vector & light, + const bool & flat, + const bool & restart = false + ); private: diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index ed00f2b9..894baa72 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -18,7 +18,7 @@ class frame GLuint vao = 0; GLuint vbo = 0; GLuint pbo = 0; - + size_t pbo_size = 0; shader program; @@ -27,7 +27,9 @@ class frame frame(const size_t & width, const size_t & height); ~frame(); - void display(const int & width, const int & height, void * buffer = nullptr); + operator const GLuint & () const; + + void display(const int & width, const int & height); }; diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 493c8c69..20b4a69e 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -9,50 +9,15 @@ namespace gproshan::rt { -raytracing::raytracing() +void raytracing::render(glm::vec4 * img, + const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const std::vector & light, + const bool & flat, + const bool & restart ) { - width = height = n_samples = 0; - img = nullptr; -} - -raytracing::~raytracing() -{ - if(img) delete [] img; -} - -bool raytracing::rt_restart(const size_t & w, const size_t & h) -{ - if(width * height < w * h) - { - width = w; - height = h; - - delete [] img; - img = new glm::vec4[width * height]; - - return true; - } - - if(width != w || height != h) - { - width = w; - height = h; - - return true; - } - - return false; -} - -void raytracing::render( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const std::vector & light, - const bool & flat, - const bool & restart ) -{ - if(rt_restart(windows_size.x, windows_size.y) || restart) - n_samples = 0; + if(restart) n_samples = 0; std::default_random_engine gen; std::uniform_real_distribution randf(0, 1); @@ -62,23 +27,15 @@ void raytracing::render( const glm::uvec2 & windows_size, glm::vec4 li; - if(!n_samples) - { - #pragma omp parallel for - for(index_t i = 0; i < width; ++i) - for(index_t j = 0; j < height; ++j) - img[j * width + i] = glm::vec4(0); - } - #pragma omp parallel for private(li) - for(index_t i = 0; i < width; ++i) - for(index_t j = 0; j < height; ++j) + for(index_t i = 0; i < windows_size.x; ++i) + for(index_t j = 0; j < windows_size.y; ++j) { //row major - glm::vec4 & color = img[j * width + i]; + glm::vec4 & color = img[j * windows_size.x + i]; - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / width, - (float(j) + randf(gen)) / height + glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, + (float(j) + randf(gen)) / windows_size.y ); glm::vec4 view = glm::vec4(screen.x * 2 - 1, screen.y * 2 - 1, 1, 1); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index a6360b08..66c1b932 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -111,15 +111,16 @@ optix::~optix() cuda_free_CHE(dd_mesh[i], d_mesh[i]); } -void optix::render( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, - const std::vector & light, - const bool & flat, - const bool & restart - ) +void optix::render( glm::vec4 * img, + const glm::uvec2 & windows_size, + const glm::mat4 & view_mat, + const glm::mat4 & proj_mat, + const std::vector & light, + const bool & flat, + const bool & restart + ) { - if(rt_restart(windows_size.x, windows_size.y) || restart) + if(restart) { n_samples = 0; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 796716ee..b6709afd 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -24,7 +24,7 @@ frame::frame(const size_t & width, const size_t & height) 1, 1, 0, }; - + glGenVertexArrays(1, &vao); glGenBuffers(1, &vbo); glBindVertexArray(vao); @@ -49,10 +49,15 @@ frame::frame(const size_t & width, const size_t & height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(GL_TEXTURE_2D, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } +frame::operator const GLuint & () const +{ + return pbo; +} + frame::~frame() { glDeleteBuffers(1, &vbo); @@ -61,15 +66,8 @@ frame::~frame() glDeleteBuffers(1, &pbo); } -void frame::display(const int & width, const int & height, void * buffer) +void frame::display(const int & width, const int & height) { - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); - void * ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); - memcpy(ptr, buffer, pbo_size); - glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - - program.enable(); glViewport(0, 0, width, height); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 9697837d..e7c9d17d 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -864,13 +864,19 @@ void viewer::render_rt(rt::raytracing * rt) { if(!rt) return; - rt->render( glm::uvec2(viewport_width, viewport_height), + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *render_frame); + glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); + + rt->render( img, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, active_mesh().render_flat, action ); + glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + action = false; - render_frame->display(viewport_width, viewport_height, rt->img); + render_frame->display(viewport_width, viewport_height); } void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) From 50c82b192b9dfae32223a6ed7e17733a3874f371 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 29 Nov 2021 21:29:04 +0100 Subject: [PATCH 0601/1018] viewer: resize frame pbo buffer --- include/gproshan/viewer/frame.h | 8 ++++--- include/gproshan/viewer/viewer.h | 4 ++-- src/viewer/frame.cpp | 38 +++++++++++++++++++++----------- src/viewer/viewer.cpp | 28 ++++++++++++----------- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index 894baa72..a3cafd7c 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -19,17 +19,19 @@ class frame GLuint vbo = 0; GLuint pbo = 0; - size_t pbo_size = 0; + size_t width = 0; + size_t height = 0; shader program; public: - frame(const size_t & width, const size_t & height); + frame(); ~frame(); operator const GLuint & () const; - void display(const int & width, const int & height); + bool resize(const size_t & w, const size_t & h); + void display(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index c511f5ba..e796f990 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -80,12 +80,12 @@ class viewer enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; index_t render_opt = R_GL; - frame * render_frame = nullptr; + frame * rt_frame = nullptr; rt::raytracing * rt_embree = nullptr; rt::raytracing * rt_optix = nullptr; - bool action = false; + bool rt_restart = false; float bgc = 0; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index b6709afd..47d2026a 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -9,7 +9,7 @@ namespace gproshan { -frame::frame(const size_t & width, const size_t & height) +frame::frame() { program.load_vertex(shaders_path("vertex_frame.glsl")); program.load_fragment(shaders_path("fragment_frame.glsl")); @@ -36,12 +36,7 @@ frame::frame(const size_t & width, const size_t & height) glBindVertexArray(0); - pbo_size = 4 * sizeof(float) * width * height; - glGenBuffers(1, &pbo); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); - glBufferData(GL_PIXEL_UNPACK_BUFFER, pbo_size, 0, GL_DYNAMIC_COPY); - glGenTextures(1, &render_tex); glBindTexture(GL_TEXTURE_2D, render_tex); @@ -50,12 +45,6 @@ frame::frame(const size_t & width, const size_t & height) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); -} - -frame::operator const GLuint & () const -{ - return pbo; } frame::~frame() @@ -66,7 +55,30 @@ frame::~frame() glDeleteBuffers(1, &pbo); } -void frame::display(const int & width, const int & height) +frame::operator const GLuint & () const +{ + return pbo; +} + +bool frame::resize(const size_t & w, const size_t & h) +{ + if(w == width && height == h) + return false; + + if(w * h > width * height) + { + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(float) * w * h, 0, GL_DYNAMIC_COPY); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + } + + width = w; + height = h; + + return true; +} + +void frame::display() { program.enable(); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index e7c9d17d..507b4f71 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -77,7 +77,7 @@ viewer::~viewer() delete rt_embree; delete rt_optix; - delete render_frame; + delete rt_frame; delete sphere; } @@ -442,7 +442,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(ImGui::GetIO().WantCaptureMouse) return; view->cam.motion(x, y, view->window_width, view->window_height); - view->action = true; + view->rt_restart = true; } } @@ -454,13 +454,13 @@ void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) if(yoffset > 0) { view->cam.zoom_in(); - view->action = true; + view->rt_restart = true; } if(yoffset < 0) { view->cam.zoom_out(); - view->action = true; + view->rt_restart = true; } } @@ -656,8 +656,8 @@ bool viewer::setup_raytracing(viewer * view) if(ImGui::Button("Build")) { - if(!view->render_frame) - view->render_frame = new frame(view->viewport_width, view->viewport_height); + if(!view->rt_frame) + view->rt_frame = new frame; switch(rt) { @@ -702,14 +702,14 @@ bool viewer::set_render_gl(viewer * view) bool viewer::set_render_embree(viewer * view) { - view->action = true; + view->rt_restart = true; view->render_opt = R_EMBREE; return false; } bool viewer::set_render_optix(viewer * view) { - view->action = true; + view->rt_restart = true; view->render_opt = R_OPTIX; return false; } @@ -784,7 +784,7 @@ bool viewer::set_render_flat(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_flat = !mesh.render_flat; - view->action = true; + view->rt_restart = true; return false; } @@ -864,19 +864,21 @@ void viewer::render_rt(rt::raytracing * rt) { if(!rt) return; - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *render_frame); + rt_restart = rt_frame->resize(viewport_width, viewport_height) || rt_restart; + + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); rt->render( img, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, - active_mesh().render_flat, action + active_mesh().render_flat, rt_restart ); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - action = false; - render_frame->display(viewport_width, viewport_height); + rt_restart = false; + rt_frame->display(); } void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) From b5afb18713efc5e0ef26437d2149cc149927ddbe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Nov 2021 00:16:50 +0100 Subject: [PATCH 0602/1018] rt_optix: free allocated memory desctructor --- include/gproshan/raytracing/rt_optix.h | 1 + src/raytracing/rt_optix.cpp | 12 +++++++++--- src/viewer/frame.cpp | 2 +- src/viewer/viewer.cpp | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index cef21695..2f1aa12d 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -48,6 +48,7 @@ class optix : public raytracing void * raygen_records_buffer = nullptr; void * miss_records_buffer = nullptr; void * hitgroup_records_buffer = nullptr; + void * as_buffer = nullptr; public: optix(const std::vector & meshes); diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 66c1b932..7e1fdedc 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -107,6 +107,13 @@ optix::optix(const std::vector & meshes) optix::~optix() { + cudaFree(render_params.frame.color_buffer); + cudaFree(launch_params_buffer); + cudaFree(raygen_records_buffer); + cudaFree(miss_records_buffer); + cudaFree(hitgroup_records_buffer); + cudaFree(as_buffer); + for(index_t i = 0; i < dd_mesh.size(); ++i) cuda_free_CHE(dd_mesh[i], d_mesh[i]); } @@ -389,13 +396,12 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) uint64_t compacted_size; cudaMemcpy(&compacted_size, d_compacted_size, sizeof(uint64_t), cudaMemcpyDeviceToHost); - void * d_as; - cudaMalloc(&d_as, compacted_size); + cudaMalloc(&as_buffer, compacted_size); optixAccelCompact( optix_context, 0, // stream optix_as_handle, - (CUdeviceptr) d_as, + (CUdeviceptr) as_buffer, compacted_size, &optix_as_handle ); diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 47d2026a..1325f42d 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -68,7 +68,7 @@ bool frame::resize(const size_t & w, const size_t & h) if(w * h > width * height) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); - glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(float) * w * h, 0, GL_DYNAMIC_COPY); + glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(float) * w * h, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 507b4f71..da368940 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -867,7 +867,7 @@ void viewer::render_rt(rt::raytracing * rt) rt_restart = rt_frame->resize(viewport_width, viewport_height) || rt_restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *rt_frame); - glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); + glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); rt->render( img, glm::uvec2(viewport_width, viewport_height), view_mat, proj_mat, {glm_vec3(light)}, From c9367eaf7b44f5cc3fa092292008aade85350a35 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Nov 2021 18:34:29 +0100 Subject: [PATCH 0603/1018] rt_embree: removing rt_embree_splat, simple point-cloud rendering already on rt_embree --- include/gproshan/raytracing/rt_embree_splat.h | 81 ---------- src/raytracing/rt_embree_splat.cpp | 144 ------------------ src/viewer/viewer.cpp | 19 +-- 3 files changed, 3 insertions(+), 241 deletions(-) delete mode 100644 include/gproshan/raytracing/rt_embree_splat.h delete mode 100644 src/raytracing/rt_embree_splat.cpp diff --git a/include/gproshan/raytracing/rt_embree_splat.h b/include/gproshan/raytracing/rt_embree_splat.h deleted file mode 100644 index 1240b985..00000000 --- a/include/gproshan/raytracing/rt_embree_splat.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifdef GPROSHAN_EMBREE - -#ifndef RT_EMBREE_SPLAT_H -#define RT_EMBREE_SPLAT_H - -#include "raytracing/rt_embree.h" - - -// geometry processing and shape analysis framework -// raytracing approach -namespace gproshan::rt { - - -class embree_splat : public embree -{ - static const size_t K = 32; - - struct splat - { - glm::vec3 P[K]; - glm::vec3 N[K]; - glm::vec3 C[K]; - float radio; - - const glm::vec4 xyzr() - { - return glm::vec4(P[0], radio); - } - - const glm::vec3 & normal() - { - return N[0]; - } - - const glm::vec3 & color() - { - return C[0]; - } - - float shading(const glm::vec3 & p, glm::vec3 & normal, glm::vec3 & color) - { - normal = glm::vec3(0); - color = glm::vec3(0); - - float w, sum_w = 0, sigma = radio;// * pc_radius; - - for(index_t i = 0; i < K; ++i) - { - w = glm::length(p - P[i]); - w = exp(-0.5 * w * w / (sigma * sigma)); - normal += w * N[i]; - color += w * C[i]; - sum_w += w; - } - - normal /= sum_w; - color /= sum_w; - - return sum_w; - } - }; - - std::vector vsplat; - - public: - embree_splat(const std::vector & meshes, const bool & pointcloud); - - private: - index_t add_pointcloud(const che * mesh); - float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); - - void init_splats(const che * mesh); -}; - - -} // namespace gproshan - -#endif // RT_EMBREE_SPLAT_H - -#endif // GPROSHAN_EMBREE - diff --git a/src/raytracing/rt_embree_splat.cpp b/src/raytracing/rt_embree_splat.cpp deleted file mode 100644 index 63a04d6f..00000000 --- a/src/raytracing/rt_embree_splat.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "raytracing/rt_embree_splat.h" - -#ifdef GPROSHAN_EMBREE - - -#include -#include - - -// geometry processing and shape analysis framework -// raytracing approach -namespace gproshan::rt { - - -embree_splat::embree_splat(const std::vector & meshes, const bool & pointcloud) -{ - build_bvh(meshes, pointcloud); -} - -index_t embree_splat::add_pointcloud(const che * mesh) -{ - init_splats(mesh); - - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); - - glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, - 4 * sizeof(float), - vsplat.size() - ); - - glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_NORMAL, 0, - RTC_FORMAT_FLOAT3, - 3 * sizeof(float), - vsplat.size() - ); - - #pragma omp parallel for - for(index_t i = 0; i < vsplat.size(); ++i) - { - pxyzr[i] = vsplat[i].xyzr(); - normal[i] = vsplat[i].normal(); - } - - rtcCommitGeometry(geom); - - index_t geom_id = rtcAttachGeometry(scene, geom); - rtcReleaseGeometry(geom); - - return geom_id; -} - -float embree_splat::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) -{ - position = r.position(); - float w = vsplat[r.hit.primID].shading(position, normal, color); - // normal = vsplat[r.hit.primID].normal(); - // color = vsplat[r.hit.primID].color(); -/* if(w < 1e-2f) - { - normal = glm::vec3(0); - color = glm::vec3(0); - } -*/ - if(w < 1e-5f) - { - r = ray_hit(r.position(), r.dir()); - if(intersect(r)) - return pointcloud_hit(position, normal, color, r); - } - - return 1e-2f; -} - -void embree_splat::init_splats(const che * mesh) -{ - const size_t n = 10; - vsplat.resize((mesh->n_vertices + n - 1) / n); - - gproshan_log_var(vsplat.size()); - - #pragma omp parallel for - for(index_t i = 0; i < vsplat.size(); ++i) - { - const index_t v = n * i; // random, feature aware index - - std::set points; - std::queue q; - - q.push(v); - points.insert(v); - - index_t u; - while(!q.empty() && points.size() < 2 * K) - { - for_star(he, mesh, q.front()) - { - u = mesh->vt(prev(he)); - if(points.find(u) == points.end()) - { - points.insert(u); - q.push(u); - } - } - - q.pop(); - } - - real_t dist, d; - const vertex & c = mesh->gt(v); - - splat & s = vsplat[i]; - for(index_t j = 0; j < K; ++j) - { - dist = INFINITY; - - for(const index_t & p: points) - { - d = *(mesh->gt(p) - c); - if(d < dist) - { - dist = d; - u = p; - } - } - - points.erase(u); - - s.P[j] = glm_vec3(mesh->gt(u)); - s.N[j] = glm_vec3(mesh->normal(u)); - s.C[j] = glm_vec3(mesh->color(u)); - } - - s.radio = glm::length(s.P[K - 1] - s.P[0]); - } -} - - -} // namespace gproshan - -#endif // GPROSHAN_EMBREE - diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index da368940..a1febb7c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -19,7 +19,6 @@ #ifdef GPROSHAN_EMBREE #include "raytracing/rt_embree.h" - #include "raytracing/rt_embree_splat.h" #endif // GPROSHAN_EMBREE #ifdef GPROSHAN_OPTIX @@ -642,17 +641,11 @@ bool viewer::setup_raytracing(viewer * view) che_viewer & mesh = view->active_mesh(); static int rt = 0; - static int rt_opt = 0; static double time = 0; static float pc_radius = 0.01; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); - - if(rt != 0) - { - ImGui::Combo("rt_opt", &rt_opt, "Mesh\0Splat\0\0"); - ImGui::InputFloat("pc_radius (if render_pointcloud)", &pc_radius, 0, 0, "%.3f"); - } + ImGui::InputFloat("pc_radius (if render_pointcloud)", &pc_radius, 0, 0, "%.3f"); if(ImGui::Button("Build")) { @@ -667,13 +660,7 @@ bool viewer::setup_raytracing(viewer * view) #ifdef GPROSHAN_EMBREE delete view->rt_embree; TIC(time); - switch(rt_opt) - { - case 0: view->rt_embree = new rt::embree({mesh}, mesh.render_pointcloud, pc_radius); - break; - case 1: view->rt_embree = new rt::embree_splat({mesh}, true); - break; - } + view->rt_embree = new rt::embree({mesh}, mesh.render_pointcloud, pc_radius); TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); #endif // GPROSHAN_EMBREE @@ -683,7 +670,7 @@ bool viewer::setup_raytracing(viewer * view) #ifdef GPROSHAN_OPTIX delete view->rt_optix; TIC(time); - view->rt_optix = new rt::optix({mesh}); + view->rt_optix = new rt::optix({mesh}); TOC(time); sprintf(view->status_message, "build optix in %.3fs", time); #endif // GPROSHAN_OPTIX From ba949e8cbdd570723c1c6105c35edad545bb4abd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Dec 2021 15:48:26 +0100 Subject: [PATCH 0604/1018] mesh: adding pts file format parser --- include/gproshan/app_viewer.h | 1 + include/gproshan/mesh/che_pts.h | 26 +++++++++++++ src/app_viewer.cpp | 1 + src/mesh/che_pts.cpp | 68 +++++++++++++++++++++++++++++++++ src/viewer/viewer.cpp | 5 ++- 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 include/gproshan/mesh/che_pts.h create mode 100644 src/mesh/che_pts.cpp diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index d7a4ad4a..2bf3587c 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -9,6 +9,7 @@ #include "mesh/che_ply.h" #include "mesh/che_ptx.h" #include "mesh/che_xyz.h" +#include "mesh/che_pts.h" #include "mesh/che_img.h" #include "mesh/che_sphere.h" #include "mesh/che_fill_hole.h" diff --git a/include/gproshan/mesh/che_pts.h b/include/gproshan/mesh/che_pts.h new file mode 100644 index 00000000..86574c6c --- /dev/null +++ b/include/gproshan/mesh/che_pts.h @@ -0,0 +1,26 @@ +#ifndef CHE_PTS_H +#define CHE_PTS_H + +#include "mesh/che.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_pts : public che +{ + public: + che_pts(const std::string & file); + + static void write_file(const che * mesh, const std::string & file); + + private: + void read_file(const std::string & file); +}; + + +} // namespace gproshan + +#endif // CHE_PTS_H + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 214068a2..03e99940 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -30,6 +30,7 @@ che * app_viewer::load_mesh(const string & file_path) if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); if(extension == "xyz") return new che_xyz(file_path); + if(extension == "pts") return new che_pts(file_path); return new che_img(file_path); } diff --git a/src/mesh/che_pts.cpp b/src/mesh/che_pts.cpp new file mode 100644 index 00000000..f43af657 --- /dev/null +++ b/src/mesh/che_pts.cpp @@ -0,0 +1,68 @@ +#include "mesh/che_pts.h" + +#include +#include +#include + + +using namespace std; + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_pts::che_pts(const string & file) +{ + init(file); +} + +void che_pts::read_file(const string & file) +{ + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); + + float x, y, z; + int intensity; + unsigned char r, g, b; + size_t n; + + fscanf(fp, "%lu", &n); + + alloc(n, 0); + + for(index_t v = 0; v < n_vertices; ++v) + { + n = fscanf(fp, "%f %f %f %d %hhu %hhu %hhu", &x, &y, &z, &intensity, &r, &g, &b); + + GT[v] = {x, y, z}; + if(n == 7) + { + VC[v] = {r, g, b}; + VHC[v] = float(intensity + 2048) / 4095; + } + } + + fclose(fp); +} + +void che_pts::write_file(const che * mesh, const string & file) +{ + FILE * fp = fopen((file + ".pts").c_str(), "w"); + assert(fp); + + fprintf(fp, "%lu\n", mesh->n_vertices); + for(index_t i = 0; i < mesh->n_vertices; ++i) + { + const vertex & v = mesh->gt(i); + const rgb_t & c = mesh->rgb(i); + fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, " %d ", int(mesh->heatmap(i) * 4095) - 2048); + fprintf(fp, "%hhu %hhu %hhu\n", c.r, c.g, c.b); + } + fclose(fp); +} + + +} // namespace gproshan + diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index a1febb7c..0799741f 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -15,6 +15,7 @@ #include "mesh/che_obj.h" #include "mesh/che_ply.h" #include "mesh/che_xyz.h" +#include "mesh/che_pts.h" #include "mesh/che_sphere.h" #ifdef GPROSHAN_EMBREE @@ -546,7 +547,7 @@ bool viewer::menu_save_mesh(viewer * view) static bool vertex_color = false; ImGui::InputText("file", file, sizeof(file)); - ImGui::Combo("format", &format, ".off\0.obj\0.ply\0.xyz\0\0"); + ImGui::Combo("format", &format, ".off\0.obj\0.ply\0.xyz\0.pts\0"); switch(format) { @@ -578,6 +579,8 @@ bool viewer::menu_save_mesh(viewer * view) break; case 3: che_xyz::write_file(mesh, file, vertex_color); break; + case 4: che_pts::write_file(mesh, file); + break; } sprintf(view->status_message, "file '%s' saved.", file); From 24583c82e57c02363751c7f37554153c7fe6e28b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Dec 2021 16:00:22 +0100 Subject: [PATCH 0605/1018] viewer: render point cloud and intensity pts, ptx files --- src/mesh/che_pts.cpp | 6 +++--- src/mesh/che_ptx.cpp | 16 ++++++++-------- src/viewer/che_viewer.cpp | 2 ++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/mesh/che_pts.cpp b/src/mesh/che_pts.cpp index f43af657..f68f9f44 100644 --- a/src/mesh/che_pts.cpp +++ b/src/mesh/che_pts.cpp @@ -30,15 +30,15 @@ void che_pts::read_file(const string & file) fscanf(fp, "%lu", &n); alloc(n, 0); - + for(index_t v = 0; v < n_vertices; ++v) { n = fscanf(fp, "%f %f %f %d %hhu %hhu %hhu", &x, &y, &z, &intensity, &r, &g, &b); - GT[v] = {x, y, z}; + GT[v] = { x, y, z }; if(n == 7) { - VC[v] = {r, g, b}; + VC[v] = { r, g, b }; VHC[v] = float(intensity + 2048) / 4095; } } diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 2488731b..3974142c 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -42,7 +42,7 @@ void che_ptx::read_file(const string & file) alloc(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); - float x, y, z, a; + float x, y, z, intensity; unsigned char r, g, b; char line[128]; @@ -51,21 +51,21 @@ void che_ptx::read_file(const string & file) // vertex 0: x y z a or x y z a r g b fgets(line, sizeof(line), fp); fgets(line, sizeof(line), fp); - rgb = sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &a, &r, &g, &b) == 7; + rgb = sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &intensity, &r, &g, &b) == 7; if(rgb) { GT[0] = { x, y, z }; VC[0] = { r, g, b }; - VHC[0] = a; + VHC[0] = intensity; for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); - sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &a, &r, &g, &b); + sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &intensity, &r, &g, &b); GT[v] = { x, y, z }; VC[v] = { r, g, b }; - VHC[v] = a; + VHC[v] = intensity; } CImg img((unsigned char *) VC, 3, n_cols, n_rows); @@ -77,14 +77,14 @@ void che_ptx::read_file(const string & file) else { GT[0] = { x, y, z }; - VHC[0] = a; + VHC[0] = intensity; for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); - sscanf(line, "%f %f %f %f", &x, &y, &z, &a); + sscanf(line, "%f %f %f %f", &x, &y, &z, &intensity); GT[v] = { x, y, z }; - VHC[v] = a; + VHC[v] = intensity; } } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index b07653dc..509a4d83 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -58,6 +58,8 @@ void che_viewer::update() { if(normalize) mesh->normalize(); + render_pointcloud = mesh->is_pointcloud(); + vertex pmin(INFINITY, INFINITY, INFINITY); vertex pmax(0, 0, 0); From b9fb086ef9842aa696e07905c6a2a2a7ba07070e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Jan 2022 23:23:44 +0100 Subject: [PATCH 0606/1018] update imgui v1.86 --- imgui/imgui.cpp | 1240 +++++++++++++++++++---------- imgui/imgui.h | 67 +- imgui/imgui_demo.cpp | 235 +++++- imgui/imgui_draw.cpp | 77 +- imgui/imgui_impl_glfw.cpp | 1 + imgui/imgui_impl_opengl3.cpp | 21 +- imgui/imgui_impl_opengl3_loader.h | 5 + imgui/imgui_internal.h | 145 ++-- imgui/imgui_tables.cpp | 14 +- imgui/imgui_widgets.cpp | 245 ++++-- 10 files changed, 1386 insertions(+), 664 deletions(-) diff --git a/imgui/imgui.cpp b/imgui/imgui.cpp index e6f206e7..d7653d41 100644 --- a/imgui/imgui.cpp +++ b/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (main code and documentation) // Help: @@ -380,6 +380,8 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2021/12/20 (1.86) - backends: removed obsolete Marmalade backend (imgui_impl_marmalade.cpp) + example. Find last supported version at https://github.com/ocornut/imgui/wiki/Bindings + - 2021/11/04 (1.86) - removed CalcListClipping() function. Prefer using ImGuiListClipper which can return non-contiguous ranges. Please open an issue if you think you really need this function. - 2021/08/23 (1.85) - removed GetWindowContentRegionWidth() function. keep inline redirection helper. can use 'GetWindowContentRegionMax().x - GetWindowContentRegionMin().x' instead for generally 'GetContentRegionAvail().x' is more useful. - 2021/07/26 (1.84) - commented out redirecting functions/enums names that were marked obsolete in 1.67 and 1.69 (March 2019): - ImGui::GetOverlayDrawList() -> use ImGui::GetForegroundDrawList() @@ -723,9 +725,11 @@ CODE Q&A: Usage ---------- - Q: Why is my widget not reacting when I click on it? - Q: How can I have widgets with an empty label? - Q: How can I have multiple widgets with the same label? + Q: About the ID Stack system.. + - Why is my widget not reacting when I click on it? + - How can I have widgets with an empty label? + - How can I have multiple widgets with the same label? + - How can I have multiple windows with the same label? Q: How can I display an image? What is ImTextureID, how does it works? Q: How can I use my own math types instead of ImVec2/ImVec4? Q: How can I interact with standard C++ types (such as std::string and std::vector)? @@ -913,16 +917,20 @@ static void NavUpdateWindowing(); static void NavUpdateWindowingOverlay(); static void NavUpdateCancelRequest(); static void NavUpdateCreateMoveRequest(); +static void NavUpdateCreateTabbingRequest(); static float NavUpdatePageUpPageDown(); static inline void NavUpdateAnyRequestFlag(); +static void NavUpdateCreateWrappingRequest(); static void NavEndFrame(); static bool NavScoreItem(ImGuiNavItemData* result); static void NavApplyItemToResult(ImGuiNavItemData* result); static void NavProcessItem(); +static void NavProcessItemForTabbingRequest(ImGuiID id); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static void NavRestoreLayer(ImGuiNavLayer layer); +static void NavRestoreHighlightAfterMove(); static int FindWindowFocusIndex(ImGuiWindow* window); // Error Checking and Debug Tools @@ -935,11 +943,13 @@ static void UpdateDebugToolStackQueries(); static void UpdateSettings(); static void UpdateMouseInputs(); static void UpdateMouseWheel(); -static void UpdateTabFocus(); static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); +static void RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 col); +static void RenderDimmedBackgrounds(); +static ImGuiWindow* FindBlockingModal(ImGuiWindow* window); // Viewports static void UpdateViewportsNewFrame(); @@ -1940,7 +1950,7 @@ void ImGuiStorage::BuildSortByKey() { struct StaticFunc { - static int IMGUI_CDECL PairCompareByID(const void* lhs, const void* rhs) + static int IMGUI_CDECL PairComparerByID(const void* lhs, const void* rhs) { // We can't just do a subtraction because qsort uses signed integers and subtracting our ID doesn't play well with that. if (((const ImGuiStoragePair*)lhs)->key > ((const ImGuiStoragePair*)rhs)->key) return +1; @@ -1948,8 +1958,7 @@ void ImGuiStorage::BuildSortByKey() return 0; } }; - if (Data.Size > 1) - ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), StaticFunc::PairCompareByID); + ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), StaticFunc::PairComparerByID); } int ImGuiStorage::GetInt(ImGuiID key, int default_val) const @@ -2241,9 +2250,10 @@ static bool GetSkipItemForListClipping() return (g.CurrentTable ? g.CurrentTable->HostSkipItems : g.CurrentWindow->SkipItems); } -// Helper to calculate coarse clipping of large list of evenly sized items. -// NB: Prefer using the ImGuiListClipper higher-level helper if you can! Read comments and instructions there on how those use this sort of pattern. -// NB: 'items_count' is only used to clamp the result, if you don't know your count you can use INT_MAX +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +// Legacy helper to calculate coarse clipping of large list of evenly sized items. +// This legacy API is not ideal because it assume we will return a single contiguous rectangle. +// Prefer using ImGuiListClipper which can returns non-contiguous ranges. void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end) { ImGuiContext& g = *GImGui; @@ -2262,20 +2272,23 @@ void ImGui::CalcListClipping(int items_count, float items_height, int* out_items } // We create the union of the ClipRect and the scoring rect which at worst should be 1 page away from ClipRect - ImRect unclipped_rect = window->ClipRect; + // We don't include g.NavId's rectangle in there (unless g.NavJustMovedToId is set) because the rectangle enlargement can get costly. + ImRect rect = window->ClipRect; if (g.NavMoveScoringItems) - unclipped_rect.Add(g.NavScoringRect); + rect.Add(g.NavScoringNoClipRect); if (g.NavJustMovedToId && window->NavLastIds[0] == g.NavJustMovedToId) - unclipped_rect.Add(ImRect(window->Pos + window->NavRectRel[0].Min, window->Pos + window->NavRectRel[0].Max)); // Could store and use NavJustMovedToRectRel + rect.Add(WindowRectRelToAbs(window, window->NavRectRel[0])); // Could store and use NavJustMovedToRectRel const ImVec2 pos = window->DC.CursorPos; - int start = (int)((unclipped_rect.Min.y - pos.y) / items_height); - int end = (int)((unclipped_rect.Max.y - pos.y) / items_height); + int start = (int)((rect.Min.y - pos.y) / items_height); + int end = (int)((rect.Max.y - pos.y) / items_height); // When performing a navigation request, ensure we have one item extra in the direction we are moving to - if (g.NavMoveScoringItems && g.NavMoveClipDir == ImGuiDir_Up) + // FIXME: Verify this works with tabbing + const bool is_nav_request = (g.NavMoveScoringItems && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); + if (is_nav_request && g.NavMoveClipDir == ImGuiDir_Up) start--; - if (g.NavMoveScoringItems && g.NavMoveClipDir == ImGuiDir_Down) + if (is_nav_request && g.NavMoveClipDir == ImGuiDir_Down) end++; start = ImClamp(start, 0, items_count); @@ -2283,17 +2296,42 @@ void ImGui::CalcListClipping(int items_count, float items_height, int* out_items *out_items_display_start = start; *out_items_display_end = end; } +#endif + +static void ImGuiListClipper_SortAndFuseRanges(ImVector& ranges, int offset = 0) +{ + if (ranges.Size - offset <= 1) + return; + + // Helper to order ranges and fuse them together if possible (bubble sort is fine as we are only sorting 2-3 entries) + for (int sort_end = ranges.Size - offset - 1; sort_end > 0; --sort_end) + for (int i = offset; i < sort_end + offset; ++i) + if (ranges[i].Min > ranges[i + 1].Min) + ImSwap(ranges[i], ranges[i + 1]); + + // Now fuse ranges together as much as possible. + for (int i = 1 + offset; i < ranges.Size; i++) + { + IM_ASSERT(!ranges[i].PosToIndexConvert && !ranges[i - 1].PosToIndexConvert); + if (ranges[i - 1].Max < ranges[i].Min) + continue; + ranges[i - 1].Min = ImMin(ranges[i - 1].Min, ranges[i].Min); + ranges[i - 1].Max = ImMax(ranges[i - 1].Max, ranges[i].Max); + ranges.erase(ranges.Data + i); + i--; + } +} -static void SetCursorPosYAndSetupForPrevLine(float pos_y, float line_height) +static void ImGuiListClipper_SeekCursorAndSetupPrevLine(float pos_y, float line_height) { // Set cursor position and a few other things so that SetScrollHereY() and Columns() can work when seeking cursor. // FIXME: It is problematic that we have to do that here, because custom/equivalent end-user code would stumble on the same issue. - // The clipper should probably have a 4th step to display the last item in a regular manner. + // The clipper should probably have a final step to display the last item in a regular manner, maybe with an opt-out flag for data sets which may have costly seek? ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; float off_y = pos_y - window->DC.CursorPos.y; window->DC.CursorPos.y = pos_y; - window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, pos_y); + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, pos_y - g.Style.ItemSpacing.y); window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y - line_height; // Setting those fields so that SetScrollHereY() can properly function after the end of our clipper usage. window->DC.PrevLineSize.y = (line_height - g.Style.ItemSpacing.y); // If we end up needing more accurate data (to e.g. use SameLine) we may as well make the clipper have a fourth step to let user process and display the last item in their list. if (ImGuiOldColumns* columns = window->DC.CurrentColumns) @@ -2309,6 +2347,15 @@ static void SetCursorPosYAndSetupForPrevLine(float pos_y, float line_height) } } +static void ImGuiListClipper_SeekCursorForItem(ImGuiListClipper* clipper, int item_n) +{ + // StartPosY starts from ItemsFrozen hence the subtraction + // Perform the add and multiply with double to allow seeking through larger ranges + ImGuiListClipperData* data = (ImGuiListClipperData*)clipper->TempData; + float pos_y = (float)((double)clipper->StartPosY + data->LossynessOffset + (double)(item_n - data->ItemsFrozen) * clipper->ItemsHeight); + ImGuiListClipper_SeekCursorAndSetupPrevLine(pos_y, clipper->ItemsHeight); +} + ImGuiListClipper::ImGuiListClipper() { memset(this, 0, sizeof(*this)); @@ -2317,7 +2364,7 @@ ImGuiListClipper::ImGuiListClipper() ImGuiListClipper::~ImGuiListClipper() { - IM_ASSERT(ItemsCount == -1 && "Forgot to call End(), or to Step() until false?"); + End(); } // Use case A: Begin() called from constructor with items_height<0, then called again from Step() in StepNo 1 @@ -2335,28 +2382,54 @@ void ImGuiListClipper::Begin(int items_count, float items_height) StartPosY = window->DC.CursorPos.y; ItemsHeight = items_height; ItemsCount = items_count; - ItemsFrozen = 0; - StepNo = 0; DisplayStart = -1; DisplayEnd = 0; + + // Acquire temporary buffer + if (++g.ClipperTempDataStacked > g.ClipperTempData.Size) + g.ClipperTempData.resize(g.ClipperTempDataStacked, ImGuiListClipperData()); + ImGuiListClipperData* data = &g.ClipperTempData[g.ClipperTempDataStacked - 1]; + data->Reset(this); + data->LossynessOffset = window->DC.CursorStartPosLossyness.y; + TempData = data; } void ImGuiListClipper::End() { - if (ItemsCount < 0) // Already ended - return; - - // In theory here we should assert that ImGui::GetCursorPosY() == StartPosY + DisplayEnd * ItemsHeight, but it feels saner to just seek at the end and not assert/crash the user. - if (ItemsCount < INT_MAX && DisplayStart >= 0) - SetCursorPosYAndSetupForPrevLine(StartPosY + (ItemsCount - ItemsFrozen) * ItemsHeight, ItemsHeight); + // In theory here we should assert that we are already at the right position, but it seems saner to just seek at the end and not assert/crash the user. + ImGuiContext& g = *GImGui; + if (ItemsCount >= 0 && ItemsCount < INT_MAX && DisplayStart >= 0) + ImGuiListClipper_SeekCursorForItem(this, ItemsCount); ItemsCount = -1; - StepNo = 3; + + // Restore temporary buffer and fix back pointers which may be invalidated when nesting + if (ImGuiListClipperData* data = (ImGuiListClipperData*)TempData) + { + IM_ASSERT(data->ListClipper == this); + data->StepNo = data->Ranges.Size; + if (--g.ClipperTempDataStacked > 0) + { + data = &g.ClipperTempData[g.ClipperTempDataStacked - 1]; + data->ListClipper->TempData = data; + } + TempData = NULL; + } +} + +void ImGuiListClipper::ForceDisplayRangeByIndices(int item_min, int item_max) +{ + ImGuiListClipperData* data = (ImGuiListClipperData*)TempData; + IM_ASSERT(DisplayStart < 0); // Only allowed after Begin() and if there has not been a specified range yet. + IM_ASSERT(item_min <= item_max); + if (item_min < item_max) + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(item_min, item_max)); } bool ImGuiListClipper::Step() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + ImGuiListClipperData* data = (ImGuiListClipperData*)TempData; ImGuiTable* table = g.CurrentTable; if (table && table->IsInsideRow) @@ -2364,95 +2437,117 @@ bool ImGuiListClipper::Step() // No items if (ItemsCount == 0 || GetSkipItemForListClipping()) + return (void)End(), false; + + // While we are in frozen row state, keep displaying items one by one, unclipped + // FIXME: Could be stored as a table-agnostic state. + if (data->StepNo == 0 && table != NULL && !table->IsUnfrozenRows) { - End(); - return false; + DisplayStart = data->ItemsFrozen; + DisplayEnd = data->ItemsFrozen + 1; + if (DisplayStart >= ItemsCount) + return (void)End(), false; + data->ItemsFrozen++; + return true; } // Step 0: Let you process the first element (regardless of it being visible or not, so we can measure the element height) - if (StepNo == 0) + bool calc_clipping = false; + if (data->StepNo == 0) { - // While we are in frozen row state, keep displaying items one by one, unclipped - // FIXME: Could be stored as a table-agnostic state. - if (table != NULL && !table->IsUnfrozenRows) - { - DisplayStart = ItemsFrozen; - DisplayEnd = ItemsFrozen + 1; - ItemsFrozen++; - return true; - } - StartPosY = window->DC.CursorPos.y; if (ItemsHeight <= 0.0f) { - // Submit the first item so we can measure its height (generally it is 0..1) - DisplayStart = ItemsFrozen; - DisplayEnd = ItemsFrozen + 1; - StepNo = 1; + // Submit the first item (or range) so we can measure its height (generally the first range is 0..1) + data->Ranges.push_front(ImGuiListClipperRange::FromIndices(data->ItemsFrozen, data->ItemsFrozen + 1)); + DisplayStart = ImMax(data->Ranges[0].Min, data->ItemsFrozen); + DisplayEnd = ImMin(data->Ranges[0].Max, ItemsCount); + if (DisplayStart == DisplayEnd) + return (void)End(), false; + data->StepNo = 1; return true; } - - // Already has item height (given by user in Begin): skip to calculating step - DisplayStart = DisplayEnd; - StepNo = 2; + calc_clipping = true; // If on the first step with known item height, calculate clipping. } - // Step 1: the clipper infer height from first element - if (StepNo == 1) + // Step 1: Let the clipper infer height from first range + if (ItemsHeight <= 0.0f) { - IM_ASSERT(ItemsHeight <= 0.0f); + IM_ASSERT(data->StepNo == 1); if (table) - { - const float pos_y1 = table->RowPosY1; // Using this instead of StartPosY to handle clipper straddling the frozen row - const float pos_y2 = table->RowPosY2; // Using this instead of CursorPos.y to take account of tallest cell. - ItemsHeight = pos_y2 - pos_y1; - window->DC.CursorPos.y = pos_y2; - } - else - { - ItemsHeight = window->DC.CursorPos.y - StartPosY; - } + IM_ASSERT(table->RowPosY1 == StartPosY && table->RowPosY2 == window->DC.CursorPos.y); + + ItemsHeight = (window->DC.CursorPos.y - StartPosY) / (float)(DisplayEnd - DisplayStart); + bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision(StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y); + if (affected_by_floating_point_precision) + ItemsHeight = window->DC.PrevLineSize.y + g.Style.ItemSpacing.y; // FIXME: Technically wouldn't allow multi-line entries. + IM_ASSERT(ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); - StepNo = 2; + calc_clipping = true; // If item height had to be calculated, calculate clipping afterwards. } - // Reached end of list - if (DisplayEnd >= ItemsCount) + // Step 0 or 1: Calculate the actual ranges of visible elements. + const int already_submitted = DisplayEnd; + if (calc_clipping) { - End(); - return false; + if (g.LogEnabled) + { + // If logging is active, do not perform any clipping + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(0, ItemsCount)); + } + else + { + // Add range selected to be included for navigation + const bool is_nav_request = (g.NavMoveScoringItems && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); + if (is_nav_request) + data->Ranges.push_back(ImGuiListClipperRange::FromPositions(g.NavScoringNoClipRect.Min.y, g.NavScoringNoClipRect.Max.y, 0, 0)); + if (is_nav_request && (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && g.NavTabbingDir == -1) + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(ItemsCount - 1, ItemsCount)); + + // Add focused/active item + ImRect nav_rect_abs = ImGui::WindowRectRelToAbs(window, window->NavRectRel[0]); + if (g.NavId != 0 && window->NavLastIds[0] == g.NavId) + data->Ranges.push_back(ImGuiListClipperRange::FromPositions(nav_rect_abs.Min.y, nav_rect_abs.Max.y, 0, 0)); + + // Add visible range + const int off_min = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Up) ? -1 : 0; + const int off_max = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Down) ? 1 : 0; + data->Ranges.push_back(ImGuiListClipperRange::FromPositions(window->ClipRect.Min.y, window->ClipRect.Max.y, off_min, off_max)); + } + + // Convert position ranges to item index ranges + // - Very important: when a starting position is after our maximum item, we set Min to (ItemsCount - 1). This allows us to handle most forms of wrapping. + // - Due to how Selectable extra padding they tend to be "unaligned" with exact unit in the item list, + // which with the flooring/ceiling tend to lead to 2 items instead of one being submitted. + for (int i = 0; i < data->Ranges.Size; i++) + if (data->Ranges[i].PosToIndexConvert) + { + int m1 = (int)(((double)data->Ranges[i].Min - window->DC.CursorPos.y - data->LossynessOffset) / ItemsHeight); + int m2 = (int)((((double)data->Ranges[i].Max - window->DC.CursorPos.y - data->LossynessOffset) / ItemsHeight) + 0.999999f); + data->Ranges[i].Min = ImClamp(already_submitted + m1 + data->Ranges[i].PosToIndexOffsetMin, already_submitted, ItemsCount - 1); + data->Ranges[i].Max = ImClamp(already_submitted + m2 + data->Ranges[i].PosToIndexOffsetMax, data->Ranges[i].Min + 1, ItemsCount); + data->Ranges[i].PosToIndexConvert = false; + } + ImGuiListClipper_SortAndFuseRanges(data->Ranges, data->StepNo); } - // Step 2: calculate the actual range of elements to display, and position the cursor before the first element - if (StepNo == 2) + // Step 0+ (if item height is given in advance) or 1+: Display the next range in line. + if (data->StepNo < data->Ranges.Size) { - IM_ASSERT(ItemsHeight > 0.0f); - - int already_submitted = DisplayEnd; - ImGui::CalcListClipping(ItemsCount - already_submitted, ItemsHeight, &DisplayStart, &DisplayEnd); - DisplayStart += already_submitted; - DisplayEnd += already_submitted; - - // Seek cursor - if (DisplayStart > already_submitted) - SetCursorPosYAndSetupForPrevLine(StartPosY + (DisplayStart - ItemsFrozen) * ItemsHeight, ItemsHeight); - - StepNo = 3; + DisplayStart = ImMax(data->Ranges[data->StepNo].Min, already_submitted); + DisplayEnd = ImMin(data->Ranges[data->StepNo].Max, ItemsCount); + if (DisplayStart > already_submitted) //-V1051 + ImGuiListClipper_SeekCursorForItem(this, DisplayStart); + data->StepNo++; return true; } - // Step 3: the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), + // After the last step: Let the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), // Advance the cursor to the end of the list and then returns 'false' to end the loop. - if (StepNo == 3) - { - // Seek cursor - if (ItemsCount < INT_MAX) - SetCursorPosYAndSetupForPrevLine(StartPosY + (ItemsCount - ItemsFrozen) * ItemsHeight, ItemsHeight); // advance cursor - ItemsCount = -1; - return false; - } + if (ItemsCount < INT_MAX) + ImGuiListClipper_SeekCursorForItem(this, ItemsCount); + ItemsCount = -1; - IM_ASSERT(0); return false; } @@ -3031,7 +3126,7 @@ ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n) ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) { ImGuiID seed = IDStack.back(); - const int r_rel[4] = { (int)(r_abs.Min.x - Pos.x), (int)(r_abs.Min.y - Pos.y), (int)(r_abs.Max.x - Pos.x), (int)(r_abs.Max.y - Pos.y) }; + ImRect r_rel = ImGui::WindowRectAbsToRel(this, r_abs); ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); ImGui::KeepAliveID(id); return id; @@ -3104,7 +3199,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) if (id) { g.ActiveIdIsAlive = id; - g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustTabbedId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; } // Clear declaration of inputs claimed by the widget @@ -3188,42 +3283,46 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) { if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; - return IsItemFocused(); + if (!IsItemFocused()) + return false; } - - // Test for bounding box overlap, as updated as ItemAdd() - ImGuiItemStatusFlags status_flags = g.LastItemData.StatusFlags; - if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) - return false; - IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function - - // Test if we are hovering the right window (our window could be behind another window) - // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) - // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable - // to use IsItemHovered() after EndChild() itself. Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was - // the test that has been running for a long while. - if (g.HoveredWindow != window && (status_flags & ImGuiItemStatusFlags_HoveredWindow) == 0) - if ((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0) + else + { + // Test for bounding box overlap, as updated as ItemAdd() + ImGuiItemStatusFlags status_flags = g.LastItemData.StatusFlags; + if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) return false; + IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function + + // Test if we are hovering the right window (our window could be behind another window) + // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) + // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable + // to use IsItemHovered() after EndChild() itself. Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was + // the test that has been running for a long while. + if (g.HoveredWindow != window && (status_flags & ImGuiItemStatusFlags_HoveredWindow) == 0) + if ((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0) + return false; - // Test if another item is active (e.g. being dragged) - if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) - if (g.ActiveId != 0 && g.ActiveId != g.LastItemData.ID && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) + // Test if another item is active (e.g. being dragged) + if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) + if (g.ActiveId != 0 && g.ActiveId != g.LastItemData.ID && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) + return false; + + // Test if interactions on this window are blocked by an active popup or modal. + // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. + if (!IsWindowContentHoverable(window, flags)) return false; - // Test if interactions on this window are blocked by an active popup or modal. - // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. - if (!IsWindowContentHoverable(window, flags)) - return false; + // Test if the item is disabled + if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + return false; - // Test if the item is disabled - if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) - return false; + // Special handling for calling after Begin() which represent the title bar or tab. + // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. + if (g.LastItemData.ID == window->MoveId && window->WriteAccessed) + return false; + } - // Special handling for calling after Begin() which represent the title bar or tab. - // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. - if (g.LastItemData.ID == window->MoveId && window->WriteAccessed) - return false; return true; } @@ -3292,46 +3391,15 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) return false; } -// Called by ItemAdd() -// Process TAB/Shift+TAB. Be mindful that this function may _clear_ the ActiveID when tabbing out. -// [WIP] This will eventually be refactored and moved into NavProcessItem() -void ImGui::ItemInputable(ImGuiWindow* window, ImGuiID id) +// This is also inlined in ItemAdd() +// Note: if ImGuiItemStatusFlags_HasDisplayRect is set, user needs to set window->DC.LastItemDisplayRect! +void ImGui::SetLastItemData(ImGuiID item_id, ImGuiItemFlags in_flags, ImGuiItemStatusFlags item_flags, const ImRect& item_rect) { ImGuiContext& g = *GImGui; - IM_ASSERT(id != 0 && id == g.LastItemData.ID); - - // Increment counters - // FIXME: ImGuiItemFlags_Disabled should disable more. - const bool is_tab_stop = (g.LastItemData.InFlags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; - if (is_tab_stop) - { - window->DC.FocusCounterTabStop++; - if (g.NavId == id) - g.NavIdTabCounter = window->DC.FocusCounterTabStop; - } - - // Process TAB/Shift-TAB to tab *OUT* of the currently focused item. - // (Note that we can always TAB out of a widget that doesn't allow tabbing in) - if (g.ActiveId == id && g.TabFocusPressed && g.TabFocusRequestNextWindow == NULL) - { - g.TabFocusRequestNextWindow = window; - g.TabFocusRequestNextCounterTabStop = window->DC.FocusCounterTabStop + (g.IO.KeyShift ? (is_tab_stop ? -1 : 0) : +1); // Modulo on index will be applied at the end of frame once we've got the total counter of items. - } - - // Handle focus requests - if (g.TabFocusRequestCurrWindow == window) - { - if (is_tab_stop && window->DC.FocusCounterTabStop == g.TabFocusRequestCurrCounterTabStop) - { - g.NavJustTabbedId = id; // FIXME-NAV: aim to eventually set in NavUpdate() once we finish the refactor - g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_FocusedByTabbing; - return; - } - - // If another item is about to be focused, we clear our own active id - if (g.ActiveId == id) - ClearActiveID(); - } + g.LastItemData.ID = item_id; + g.LastItemData.InFlags = in_flags; + g.LastItemData.StatusFlags = item_flags; + g.LastItemData.Rect = item_rect; } float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) @@ -3660,7 +3728,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() // Find the top-most window between HoveredWindow and the top-most Modal Window. // This is where we can trim the popup stack. ImGuiWindow* modal = GetTopMostPopupModal(); - bool hovered_window_above_modal = g.HoveredWindow && IsWindowAbove(g.HoveredWindow, modal); + bool hovered_window_above_modal = g.HoveredWindow && (modal == NULL || IsWindowAbove(g.HoveredWindow, modal)); ClosePopupsOverWindow(hovered_window_above_modal ? g.HoveredWindow : modal, true); } } @@ -3692,25 +3760,26 @@ static void ImGui::UpdateMouseInputs() for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) { g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f; + g.IO.MouseClickedCount[i] = 0; // Will be filled below g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f; g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i]; g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f; - g.IO.MouseDoubleClicked[i] = false; if (g.IO.MouseClicked[i]) { + bool is_repeated_click = false; if ((float)(g.Time - g.IO.MouseClickedTime[i]) < g.IO.MouseDoubleClickTime) { ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) - g.IO.MouseDoubleClicked[i] = true; - g.IO.MouseClickedTime[i] = -g.IO.MouseDoubleClickTime * 2.0f; // Mark as "old enough" so the third click isn't turned into a double-click + is_repeated_click = true; } + if (is_repeated_click) + g.IO.MouseClickedLastCount[i]++; else - { - g.IO.MouseClickedTime[i] = g.Time; - } + g.IO.MouseClickedLastCount[i] = 1; + g.IO.MouseClickedTime[i] = g.Time; g.IO.MouseClickedPos[i] = g.IO.MousePos; - g.IO.MouseDownWasDoubleClick[i] = g.IO.MouseDoubleClicked[i]; + g.IO.MouseClickedCount[i] = g.IO.MouseClickedLastCount[i]; g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f); g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; } @@ -3722,9 +3791,12 @@ static void ImGui::UpdateMouseInputs() g.IO.MouseDragMaxDistanceAbs[i].x = ImMax(g.IO.MouseDragMaxDistanceAbs[i].x, delta_from_click_pos.x < 0.0f ? -delta_from_click_pos.x : delta_from_click_pos.x); g.IO.MouseDragMaxDistanceAbs[i].y = ImMax(g.IO.MouseDragMaxDistanceAbs[i].y, delta_from_click_pos.y < 0.0f ? -delta_from_click_pos.y : delta_from_click_pos.y); } - if (!g.IO.MouseDown[i] && !g.IO.MouseReleased[i]) - g.IO.MouseDownWasDoubleClick[i] = false; - if (g.IO.MouseClicked[i]) // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation + + // We provide io.MouseDoubleClicked[] as a legacy service + g.IO.MouseDoubleClicked[i] = (g.IO.MouseClickedCount[i] == 2); + + // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation + if (g.IO.MouseClicked[i]) g.NavDisableMouseHover = false; } } @@ -3824,44 +3896,6 @@ void ImGui::UpdateMouseWheel() } } -void ImGui::UpdateTabFocus() -{ - ImGuiContext& g = *GImGui; - - // Pressing TAB activate widget focus - g.TabFocusPressed = false; - if (g.NavWindow && g.NavWindow->Active && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) - if (!g.IO.KeyCtrl && !g.IO.KeyAlt && IsKeyPressedMap(ImGuiKey_Tab) && !IsActiveIdUsingKey(ImGuiKey_Tab)) - g.TabFocusPressed = true; - if (g.ActiveId == 0 && g.TabFocusPressed) - { - // - This path is only taken when no widget are active/tabbed-into yet. - // Subsequent tabbing will be processed by FocusableItemRegister() - // - Note that SetKeyboardFocusHere() sets the Next fields mid-frame. To be consistent we also - // manipulate the Next fields here even though they will be turned into Curr fields below. - g.TabFocusRequestNextWindow = g.NavWindow; - if (g.NavId != 0 && g.NavIdTabCounter != INT_MAX) - g.TabFocusRequestNextCounterTabStop = g.NavIdTabCounter + (g.IO.KeyShift ? -1 : 0); - else - g.TabFocusRequestNextCounterTabStop = g.IO.KeyShift ? -1 : 0; - } - - // Turn queued focus request into current one - g.TabFocusRequestCurrWindow = NULL; - g.TabFocusRequestCurrCounterTabStop = INT_MAX; - if (g.TabFocusRequestNextWindow != NULL) - { - ImGuiWindow* window = g.TabFocusRequestNextWindow; - g.TabFocusRequestCurrWindow = window; - if (g.TabFocusRequestNextCounterTabStop != INT_MAX && window->DC.FocusCounterTabStop != -1) - g.TabFocusRequestCurrCounterTabStop = ImModPositive(g.TabFocusRequestNextCounterTabStop, window->DC.FocusCounterTabStop + 1); - g.TabFocusRequestNextWindow = NULL; - g.TabFocusRequestNextCounterTabStop = INT_MAX; - } - - g.NavIdTabCounter = INT_MAX; -} - // The reason this is exposed in imgui_internal.h is: on touch-based system that don't have hovering, we want to dispatch inputs to the right target (imgui vs imgui+app) void ImGui::UpdateHoveredWindowAndCaptureFlags() { @@ -3878,7 +3912,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // Modal windows prevents mouse from hovering behind them. ImGuiWindow* modal_window = GetTopMostPopupModal(); - if (modal_window && g.HoveredWindow && !IsWindowChildOf(g.HoveredWindow->RootWindow, modal_window, true)) + if (modal_window && g.HoveredWindow && !IsWindowWithinBeginStackOf(g.HoveredWindow->RootWindow, modal_window)) clear_hovered_windows = true; // Disabled mouse? @@ -4107,9 +4141,6 @@ void ImGui::NewFrame() // Mouse wheel scrolling, scale UpdateMouseWheel(); - // Update legacy TAB focus - UpdateTabFocus(); - // Mark all windows as not visible and compact unused memory. IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size); const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; @@ -4130,9 +4161,9 @@ void ImGui::NewFrame() for (int i = 0; i < g.TablesLastTimeActive.Size; i++) if (g.TablesLastTimeActive[i] >= 0.0f && g.TablesLastTimeActive[i] < memory_compact_start_time) TableGcCompactTransientBuffers(g.Tables.GetByIndex(i)); - for (int i = 0; i < g.TablesTempDataStack.Size; i++) - if (g.TablesTempDataStack[i].LastTimeActive >= 0.0f && g.TablesTempDataStack[i].LastTimeActive < memory_compact_start_time) - TableGcCompactTransientBuffers(&g.TablesTempDataStack[i]); + for (int i = 0; i < g.TablesTempData.Size; i++) + if (g.TablesTempData[i].LastTimeActive >= 0.0f && g.TablesTempData[i].LastTimeActive < memory_compact_start_time) + TableGcCompactTransientBuffers(&g.TablesTempData[i]); if (g.GcCompactAll) GcCompactTransientMiscBuffers(); g.GcCompactAll = false; @@ -4245,8 +4276,10 @@ void ImGui::Shutdown(ImGuiContext* context) g.CurrentTabBarStack.clear(); g.ShrinkWidthBuffer.clear(); + g.ClipperTempData.clear_destruct(); + g.Tables.Clear(); - g.TablesTempDataStack.clear_destruct(); + g.TablesTempData.clear_destruct(); g.DrawChannelsTempMergeBuffer.clear(); g.ClipboardHandlerData.clear(); @@ -4287,8 +4320,7 @@ static void AddWindowToSortBuffer(ImVector* out_sorted_windows, Im if (window->Active) { int count = window->DC.ChildWindows.Size; - if (count > 1) - ImQsort(window->DC.ChildWindows.Data, (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); + ImQsort(window->DC.ChildWindows.Data, (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); for (int i = 0; i < count; i++) { ImGuiWindow* child = window->DC.ChildWindows[i]; @@ -4348,11 +4380,15 @@ static void AddWindowToDrawData(ImGuiWindow* window, int layer) } } +static inline int GetWindowDisplayLayer(ImGuiWindow* window) +{ + return (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0; +} + // Layer is locked for the root window, however child windows may use a different viewport (e.g. extruding menu) -static void AddRootWindowToDrawData(ImGuiWindow* window) +static inline void AddRootWindowToDrawData(ImGuiWindow* window) { - int layer = (window->Flags & ImGuiWindowFlags_Tooltip) ? 1 : 0; - AddWindowToDrawData(window, layer); + AddWindowToDrawData(window, GetWindowDisplayLayer(window)); } void ImDrawDataBuilder::FlattenIntoSingleLayer() @@ -4411,6 +4447,84 @@ void ImGui::PopClipRect() window->ClipRect = window->DrawList->_ClipRectStack.back(); } +static void ImGui::RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 col) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + ImGuiViewportP* viewport = (ImGuiViewportP*)GetMainViewport(); + ImRect viewport_rect = viewport->GetMainRect(); + + // Draw behind window by moving the draw command at the FRONT of the draw list + { + // We've already called AddWindowToDrawData() which called DrawList->ChannelsMerge() on DockNodeHost windows, + // and draw list have been trimmed already, hence the explicit recreation of a draw command if missing. + ImDrawList* draw_list = window->RootWindow->DrawList; + if (draw_list->CmdBuffer.Size == 0) + draw_list->AddDrawCmd(); + draw_list->PushClipRect(viewport_rect.Min - ImVec2(1, 1), viewport_rect.Max + ImVec2(1, 1), false); // Ensure ImDrawCmd are not merged + draw_list->AddRectFilled(viewport_rect.Min, viewport_rect.Max, col); + ImDrawCmd cmd = draw_list->CmdBuffer.back(); + IM_ASSERT(cmd.ElemCount == 6); + draw_list->CmdBuffer.pop_back(); + draw_list->CmdBuffer.push_front(cmd); + draw_list->PopClipRect(); + } +} + +ImGuiWindow* ImGui::FindBottomMostVisibleWindowWithinBeginStack(ImGuiWindow* parent_window) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* bottom_most_visible_window = parent_window; + for (int i = FindWindowDisplayIndex(parent_window); i >= 0; i--) + { + ImGuiWindow* window = g.Windows[i]; + if (window->Flags & ImGuiWindowFlags_ChildWindow) + continue; + if (!IsWindowWithinBeginStackOf(window, parent_window)) + break; + if (IsWindowActiveAndVisible(window) && GetWindowDisplayLayer(window) <= GetWindowDisplayLayer(parent_window)) + bottom_most_visible_window = window; + } + return bottom_most_visible_window; +} + +static void ImGui::RenderDimmedBackgrounds() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* modal_window = GetTopMostAndVisiblePopupModal(); + const bool dim_bg_for_modal = (modal_window != NULL); + const bool dim_bg_for_window_list = (g.NavWindowingTargetAnim != NULL && g.NavWindowingTargetAnim->Active); + if (!dim_bg_for_modal && !dim_bg_for_window_list) + return; + + if (dim_bg_for_modal) + { + // Draw dimming behind modal or a begin stack child, whichever comes first in draw order. + ImGuiWindow* dim_behind_window = FindBottomMostVisibleWindowWithinBeginStack(modal_window); + RenderDimmedBackgroundBehindWindow(dim_behind_window, GetColorU32(ImGuiCol_ModalWindowDimBg, g.DimBgRatio)); + } + else if (dim_bg_for_window_list) + { + // Draw dimming behind CTRL+Tab target window + RenderDimmedBackgroundBehindWindow(g.NavWindowingTargetAnim, GetColorU32(ImGuiCol_NavWindowingDimBg, g.DimBgRatio)); + + // Draw border around CTRL+Tab target window + ImGuiWindow* window = g.NavWindowingTargetAnim; + ImGuiViewport* viewport = GetMainViewport(); + float distance = g.FontSize; + ImRect bb = window->Rect(); + bb.Expand(distance); + if (bb.GetWidth() >= viewport->Size.x && bb.GetHeight() >= viewport->Size.y) + bb.Expand(-distance - 1.0f); // If a window fits the entire viewport, adjust its highlight inward + if (window->DrawList->CmdBuffer.Size == 0) + window->DrawList->AddDrawCmd(); + window->DrawList->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size); + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), window->WindowRounding, 0, 3.0f); + window->DrawList->PopClipRect(); + } +} + // This is normally called by Render(). You may want to call it directly if you want to avoid calling Render() but the gain will be very minimal. void ImGui::EndFrame() { @@ -4505,6 +4619,7 @@ void ImGui::Render() if (g.FrameCountEnded != g.FrameCount) EndFrame(); + const bool first_render_of_frame = (g.FrameCountRendered != g.FrameCount); g.FrameCountRendered = g.FrameCount; g.IO.MetricsRenderWindows = 0; @@ -4519,6 +4634,10 @@ void ImGui::Render() AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); } + // Draw modal/window whitening backgrounds + if (first_render_of_frame) + RenderDimmedBackgrounds(); + // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; @@ -4542,7 +4661,7 @@ void ImGui::Render() viewport->DrawDataBuilder.FlattenIntoSingleLayer(); // Draw software mouse cursor if requested by io.MouseDrawCursor flag - if (g.IO.MouseDrawCursor) + if (g.IO.MouseDrawCursor && first_render_of_frame) RenderMouseCursor(GetForegroundDrawList(viewport), g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); // Add foreground ImDrawList (for each active viewport) @@ -4765,7 +4884,14 @@ bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - return g.IO.MouseDoubleClicked[button]; + return g.IO.MouseClickedCount[button] == 2; +} + +int ImGui::GetMouseClickedCount(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseClickedCount[button]; } // Return if a mouse click/drag went past the given threshold. Valid to call during the MouseReleased frame. @@ -5161,6 +5287,29 @@ static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settin window->Collapsed = settings->Collapsed; } +static void UpdateWindowInFocusOrderList(ImGuiWindow* window, bool just_created, ImGuiWindowFlags new_flags) +{ + ImGuiContext& g = *GImGui; + + const bool new_is_explicit_child = (new_flags & ImGuiWindowFlags_ChildWindow) != 0; + const bool child_flag_changed = new_is_explicit_child != window->IsExplicitChild; + if ((just_created || child_flag_changed) && !new_is_explicit_child) + { + IM_ASSERT(!g.WindowsFocusOrder.contains(window)); + g.WindowsFocusOrder.push_back(window); + window->FocusOrder = (short)(g.WindowsFocusOrder.Size - 1); + } + else if (!just_created && child_flag_changed && new_is_explicit_child) + { + IM_ASSERT(g.WindowsFocusOrder[window->FocusOrder] == window); + for (int n = window->FocusOrder + 1; n < g.WindowsFocusOrder.Size; n++) + g.WindowsFocusOrder[n]->FocusOrder--; + g.WindowsFocusOrder.erase(g.WindowsFocusOrder.Data + window->FocusOrder); + window->FocusOrder = -1; + } + window->IsExplicitChild = new_is_explicit_child; +} + static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; @@ -5200,16 +5349,12 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0); } - if (!(flags & ImGuiWindowFlags_ChildWindow)) - { - g.WindowsFocusOrder.push_back(window); - window->FocusOrder = (short)(g.WindowsFocusOrder.Size - 1); - } - if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus) g.Windows.push_front(window); // Quite slow but rare and only once else g.Windows.push_back(window); + UpdateWindowInFocusOrderList(window, true, window->Flags); + return window; } @@ -5316,11 +5461,11 @@ ImVec2 ImGui::CalcWindowNextAutoFitSize(ImGuiWindow* window) return size_final; } -static ImGuiCol GetWindowBgColorIdxFromFlags(ImGuiWindowFlags flags) +static ImGuiCol GetWindowBgColorIdx(ImGuiWindow* window) { - if (flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) + if (window->Flags & (ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_Popup)) return ImGuiCol_PopupBg; - if (flags & ImGuiWindowFlags_ChildWindow) + if (window->Flags & ImGuiWindowFlags_ChildWindow) return ImGuiCol_ChildBg; return ImGuiCol_WindowBg; } @@ -5445,7 +5590,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (hovered || held) g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; - if (held && g.IO.MouseDoubleClicked[0] && resize_grip_n == 0) + if (held && g.IO.MouseClickedCount[0] == 2 && resize_grip_n == 0) { // Manual auto-fit when double-clicking size_target = CalcWindowSizeAfterConstraint(window, size_auto_fit); @@ -5475,7 +5620,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s bool hovered, held; ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_HOVER_PADDING); ImGuiID border_id = window->GetID(border_n + 4); // == GetWindowResizeBorderID() - ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren); + ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) { @@ -5503,7 +5648,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s { ImVec2 nav_resize_delta; if (g.NavInputSource == ImGuiInputSource_Keyboard && g.IO.KeyShift) - nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); + nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_RawKeyboard, ImGuiInputReadMode_Down); if (g.NavInputSource == ImGuiInputSource_Gamepad) nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down); if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f) @@ -5598,7 +5743,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar // Window background if (!(flags & ImGuiWindowFlags_NoBackground)) { - ImU32 bg_col = GetColorU32(GetWindowBgColorIdxFromFlags(flags)); + ImU32 bg_col = GetColorU32(GetWindowBgColorIdx(window)); bool override_alpha = false; float alpha = 1.0f; if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasBgAlpha) @@ -5664,6 +5809,7 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl const bool has_collapse_button = !(flags & ImGuiWindowFlags_NoCollapse) && (style.WindowMenuButtonPosition != ImGuiDir_None); // Close & Collapse button are on the Menu NavLayer and don't default focus (unless there's nothing else on that layer) + // FIXME-NAV: Might want (or not?) to set the equivalent of ImGuiButtonFlags_NoNavFocus so that mouse clicks on standard title bar items don't necessarily set nav/keyboard ref? const ImGuiItemFlags item_flags_backup = g.CurrentItemFlags; g.CurrentItemFlags |= ImGuiItemFlags_NoNavDefaultFocus; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; @@ -5758,6 +5904,36 @@ void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags } } +// When a modal popup is open, newly created windows that want focus (i.e. are not popups and do not specify ImGuiWindowFlags_NoFocusOnAppearing) +// should be positioned behind that modal window, unless the window was created inside the modal begin-stack. +// In case of multiple stacked modals newly created window honors begin stack order and does not go below its own modal parent. +// - Window // FindBlockingModal() returns Modal1 +// - Window // .. returns Modal1 +// - Modal1 // .. returns Modal2 +// - Window // .. returns Modal2 +// - Window // .. returns Modal2 +// - Modal2 // .. returns Modal2 +static ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (g.OpenPopupStack.Size <= 0) + return NULL; + + // Find a modal that has common parent with specified window. Specified window should be positioned behind that modal. + for (int i = g.OpenPopupStack.Size - 1; i >= 0; i--) + { + ImGuiWindow* popup_window = g.OpenPopupStack.Data[i].Window; + if (popup_window == NULL || !popup_window->WasActive || !(popup_window->Flags & ImGuiWindowFlags_Modal)) // Check WasActive, because this code may run before popup renders on current frame. + continue; + if (IsWindowWithinBeginStackOf(window, popup_window)) // Window is rendered over last modal, no render order change needed. + break; + for (ImGuiWindow* parent = popup_window->ParentWindowInBeginStack->RootWindow; parent != NULL; parent = parent->ParentWindowInBeginStack->RootWindow) + if (IsWindowWithinBeginStackOf(window, parent)) + return popup_window; // Place window above its begin stack parent. + } + return NULL; +} + // Push a new Dear ImGui window to add widgets to. // - A default window called "Debug" is automatically stacked at the beginning of every frame so you can use widgets without explicitly calling a Begin/End pair. // - Begin/End can be called multiple times during the frame with the same window name to append content. @@ -5778,6 +5954,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const bool window_just_created = (window == NULL); if (window_just_created) window = CreateNewWindow(name, flags); + else + UpdateWindowInFocusOrderList(window, window_just_created, flags); // Automatically disable manual moving/resizing when NoInputs is set if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) @@ -5834,6 +6012,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window_stack_data.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindowStack.push_back(window_stack_data); g.CurrentWindow = NULL; + if (flags & ImGuiWindowFlags_ChildMenu) + g.BeginMenuCount++; if (flags & ImGuiWindowFlags_Popup) { @@ -5845,7 +6025,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update ->RootWindow and others pointers (before any possible call to FocusWindow) if (first_begin_of_the_frame) + { UpdateWindowParentAndRootLinks(window, flags, parent_window); + window->ParentWindowInBeginStack = parent_window_in_stack; + } // Process SetNextWindow***() calls // (FIXME: Consider splitting the HasXXX flags into X/Y components @@ -5980,7 +6163,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), so verify that we don't have items over the title bar. ImRect title_bar_rect = window->TitleBarRect(); - if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseDoubleClicked[0]) + if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseClickedCount[0] == 2) window->WantCollapseToggle = true; if (window->WantCollapseToggle) { @@ -6100,6 +6283,22 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) want_focus = true; else if ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) == 0) want_focus = true; + + ImGuiWindow* modal = GetTopMostPopupModal(); + if (modal != NULL && !IsWindowWithinBeginStackOf(window, modal)) + { + // Avoid focusing a window that is created outside of active modal. This will prevent active modal from being closed. + // Since window is not focused it would reappear at the same display position like the last time it was visible. + // In case of completely new windows it would go to the top (over current modal), but input to such window would still be blocked by modal. + // Position window behind a modal that is not a begin-parent of this window. + want_focus = false; + if (window == window->RootWindow) + { + ImGuiWindow* blocking_modal = FindBlockingModal(window); + IM_ASSERT(blocking_modal != NULL); + BringWindowToDisplayBehind(window, blocking_modal); + } + } } // Handle manual resize: Resize Grips, Borders, Gamepad @@ -6150,7 +6349,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Inner rectangle // Not affected by window border size. Used by: // - InnerClipRect - // - ScrollToBringRectIntoView() + // - ScrollToRectEx() // - NavUpdatePageUpPageDown() // - Scrollbar() window->InnerRect.Min.x = window->Pos.x; @@ -6197,24 +6396,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DrawList->PushTextureID(g.Font->ContainerAtlas->TexID); PushClipRect(host_rect.Min, host_rect.Max, false); - // Draw modal window background (darkens what is behind them, all viewports) - const bool dim_bg_for_modal = (flags & ImGuiWindowFlags_Modal) && window == GetTopMostPopupModal() && window->HiddenFramesCannotSkipItems <= 0; - const bool dim_bg_for_window_list = g.NavWindowingTargetAnim && (window == g.NavWindowingTargetAnim->RootWindow); - if (dim_bg_for_modal || dim_bg_for_window_list) - { - const ImU32 dim_bg_col = GetColorU32(dim_bg_for_modal ? ImGuiCol_ModalWindowDimBg : ImGuiCol_NavWindowingDimBg, g.DimBgRatio); - window->DrawList->AddRectFilled(viewport_rect.Min, viewport_rect.Max, dim_bg_col); - } - - // Draw navigation selection/windowing rectangle background - if (dim_bg_for_window_list && window == g.NavWindowingTargetAnim) - { - ImRect bb = window->Rect(); - bb.Expand(g.FontSize); - if (!bb.Contains(viewport_rect)) // Avoid drawing if the window covers all the viewport anyway - window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha * 0.25f), g.Style.WindowRounding); - } - // Child windows can render their decoration (bg color, border, scrollbars, etc.) within their parent to save a draw call (since 1.71) // When using overlapping child windows, this will break the assumption that child z-order is mapped to submission order. // FIXME: User code may rely on explicit sorting of overlapping child window and would need to disable this somehow. Please get in contact if you are affected (github #4493) @@ -6242,20 +6423,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DrawList = &window->DrawListInst; } - // Draw navigation selection/windowing rectangle border - if (g.NavWindowingTargetAnim == window) - { - float rounding = ImMax(window->WindowRounding, g.Style.WindowRounding); - ImRect bb = window->Rect(); - bb.Expand(g.FontSize); - if (bb.Contains(viewport_rect)) // If a window fits the entire viewport, adjust its highlight inward - { - bb.Expand(-g.FontSize - 1.0f); - rounding = window->WindowRounding; - } - window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_NavWindowingHighlight, g.NavWindowingHighlightAlpha), rounding, 0, 3.0f); - } - // UPDATE RECTANGLES (2- THOSE AFFECTED BY SCROLLING) // Work rectangle. @@ -6287,7 +6454,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.Indent.x = 0.0f + window->WindowPadding.x - window->Scroll.x; window->DC.GroupOffset.x = 0.0f; window->DC.ColumnsOffset.x = 0.0f; - window->DC.CursorStartPos = window->Pos + ImVec2(window->DC.Indent.x + window->DC.ColumnsOffset.x, decoration_up_height + window->WindowPadding.y - window->Scroll.y); + + // Record the loss of precision of CursorStartPos which can happen due to really large scrolling amount. + // This is used by clipper to compensate and fix the most common use case of large scroll area. Easy and cheap, next best thing compared to switching everything to double or ImU64. + double start_pos_highp_x = (double)window->Pos.x + window->WindowPadding.x - (double)window->Scroll.x + window->DC.ColumnsOffset.x; + double start_pos_highp_y = (double)window->Pos.y + window->WindowPadding.y - (double)window->Scroll.y + decoration_up_height; + window->DC.CursorStartPos = ImVec2((float)start_pos_highp_x, (float)start_pos_highp_y); + window->DC.CursorStartPosLossyness = ImVec2((float)(start_pos_highp_x - window->DC.CursorStartPos.x), (float)(start_pos_highp_y - window->DC.CursorStartPos.y)); window->DC.CursorPos = window->DC.CursorStartPos; window->DC.CursorPosPrevLine = window->DC.CursorPos; window->DC.CursorMaxPos = window->DC.CursorStartPos; @@ -6310,7 +6483,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.CurrentColumns = NULL; window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.ParentLayoutType = parent_window ? parent_window->DC.LayoutType : ImGuiLayoutType_Vertical; - window->DC.FocusCounterTabStop = -1; window->DC.ItemWidth = window->ItemWidthDefault; window->DC.TextWrapPos = -1.0f; // disabled @@ -6348,10 +6520,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). // This is useful to allow creating context menus on title bar only, etc. - g.LastItemData.ID = window->MoveId; - g.LastItemData.InFlags = g.CurrentItemFlags; - g.LastItemData.StatusFlags = IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0; - g.LastItemData.Rect = title_bar_rect; + SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect); #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) @@ -6383,9 +6552,12 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) // FIXME: Doesn't make sense for ChildWindow?? - if (!g.LogEnabled) + { + const bool nav_request = (flags & ImGuiWindowFlags_NavFlattened) && (g.NavAnyRequest && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); + if (!g.LogEnabled && !nav_request) if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) window->HiddenFramesCanSkipItems = 1; + } // Hide along with parent or if parent is collapsed if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) @@ -6399,7 +6571,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->HiddenFramesCanSkipItems = 1; // Update the Hidden flag - window->Hidden = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0) || (window->HiddenFramesForRenderOnly > 0); + bool hidden_regular = (window->HiddenFramesCanSkipItems > 0) || (window->HiddenFramesCannotSkipItems > 0); + window->Hidden = hidden_regular || (window->HiddenFramesForRenderOnly > 0); // Disable inputs for requested number of frames if (window->DisableInputsFrames > 0) @@ -6410,7 +6583,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update the SkipItems flag, used to early out of all items functions (no layout required) bool skip_items = false; - if (window->Collapsed || !window->Active || window->Hidden) + if (window->Collapsed || !window->Active || hidden_regular) if (window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0 && window->HiddenFramesCannotSkipItems <= 0) skip_items = true; window->SkipItems = skip_items; @@ -6447,6 +6620,8 @@ void ImGui::End() // Pop from window stack g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; + if (window->Flags & ImGuiWindowFlags_ChildMenu) + g.BeginMenuCount--; if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithCurrentState(); @@ -6504,6 +6679,34 @@ void ImGui::BringWindowToDisplayBack(ImGuiWindow* window) } } +void ImGui::BringWindowToDisplayBehind(ImGuiWindow* window, ImGuiWindow* behind_window) +{ + IM_ASSERT(window != NULL && behind_window != NULL); + ImGuiContext& g = *GImGui; + window = window->RootWindow; + behind_window = behind_window->RootWindow; + int pos_wnd = FindWindowDisplayIndex(window); + int pos_beh = FindWindowDisplayIndex(behind_window); + if (pos_wnd < pos_beh) + { + size_t copy_bytes = (pos_beh - pos_wnd - 1) * sizeof(ImGuiWindow*); + memmove(&g.Windows.Data[pos_wnd], &g.Windows.Data[pos_wnd + 1], copy_bytes); + g.Windows[pos_beh - 1] = window; + } + else + { + size_t copy_bytes = (pos_wnd - pos_beh) * sizeof(ImGuiWindow*); + memmove(&g.Windows.Data[pos_beh + 1], &g.Windows.Data[pos_beh], copy_bytes); + g.Windows[pos_beh] = window; + } +} + +int ImGui::FindWindowDisplayIndex(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + return g.Windows.index_from_ptr(g.Windows.find(window)); +} + // Moving window to front of display and set focus (which happens to be back of our sorted list) void ImGui::FocusWindow(ImGuiWindow* window) { @@ -6706,9 +6909,14 @@ void ImGui::PopTextWrapPos() static ImGuiWindow* GetCombinedRootWindow(ImGuiWindow* window, bool popup_hierarchy) { - window = window->RootWindow; - if (popup_hierarchy) - window = window->RootWindowPopupTree; + ImGuiWindow* last_window = NULL; + while (last_window != window) + { + last_window = window; + window = window->RootWindow; + if (popup_hierarchy) + window = window->RootWindowPopupTree; + } return window; } @@ -6728,9 +6936,28 @@ bool ImGui::IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, return false; } +bool ImGui::IsWindowWithinBeginStackOf(ImGuiWindow* window, ImGuiWindow* potential_parent) +{ + if (window->RootWindow == potential_parent) + return true; + while (window != NULL) + { + if (window == potential_parent) + return true; + window = window->ParentWindowInBeginStack; + } + return false; +} + bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below) { ImGuiContext& g = *GImGui; + + // It would be saner to ensure that display layer is always reflected in the g.Windows[] order, which would likely requires altering all manipulations of that array + const int display_layer_delta = GetWindowDisplayLayer(potential_above) - GetWindowDisplayLayer(potential_below); + if (display_layer_delta != 0) + return display_layer_delta > 0; + for (int i = g.Windows.Size - 1; i >= 0; i--) { ImGuiWindow* candidate_window = g.Windows[i]; @@ -6785,8 +7012,8 @@ bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags) return false; if (flags & ImGuiFocusedFlags_AnyWindow) return true; - IM_ASSERT(cur_window); // Not inside a Begin()/End() + IM_ASSERT(cur_window); // Not inside a Begin()/End() const bool popup_hierarchy = (flags & ImGuiFocusedFlags_NoPopupHierarchy) == 0; if (flags & ImGuiHoveredFlags_RootWindow) cur_window = GetCombinedRootWindow(cur_window, popup_hierarchy); @@ -7091,11 +7318,16 @@ void ImGui::SetKeyboardFocusHere(int offset) IM_ASSERT(offset >= -1); // -1 is allowed but not below g.NavWindow = window; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; - NavMoveRequestSubmit(ImGuiDir_None, ImGuiDir_None, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. if (offset == -1) - NavMoveRequestResolveWithLastItem(); + { + NavMoveRequestResolveWithLastItem(&g.NavMoveResultLocal); + } else - g.NavTabbingInputableRemaining = offset + 1; + { + g.NavTabbingDir = 1; + g.NavTabbingCounter = offset + 1; + } } void ImGui::SetItemDefaultFocus() @@ -7109,7 +7341,7 @@ void ImGui::SetItemDefaultFocus() g.NavInitRequest = false; g.NavInitResultId = g.LastItemData.ID; - g.NavInitResultRectRel = ImRect(g.LastItemData.Rect.Min - window->Pos, g.LastItemData.Rect.Max - window->Pos); + g.NavInitResultRectRel = WindowRectAbsToRel(window, g.LastItemData.Rect); NavUpdateAnyRequestFlag(); // Scroll could be done in NavInitRequestApplyResult() via a opt-in flag (we however don't want regular init requests to scroll) @@ -7555,6 +7787,11 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) NavProcessItem(); + // [DEBUG] People keep stumbling on this problem and using "" as identifier in the root of a window instead of "##something". + // Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something". + // READ THE FAQ: https://dearimgui.org/faq + IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); + // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() #ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX if (id == g.DebugItemPickerBreakId) @@ -7577,11 +7814,6 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu return false; //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] - // [WIP] Tab stop handling (previously was using internal FocusableItemRegister() api) - // FIXME-NAV: We would now want to move this before the clipping test, but this would require being able to scroll and currently this would mean an extra frame. (#4079, #343) - if (extra_flags & ImGuiItemFlags_Inputable) - ItemInputable(window, id); - // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (IsMouseHoveringRect(bb.Min, bb.Max)) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; @@ -7902,7 +8134,7 @@ void ImGui::EndGroup() window->DC.CurrLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now. ItemSize(group_bb.GetSize()); - ItemAdd(group_bb, 0); + ItemAdd(group_bb, 0, NULL, ImGuiItemFlags_NoTabStop); // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group. // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets. @@ -8023,8 +8255,8 @@ ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGui const bool fully_visible_x = item_rect.Min.x >= window_rect.Min.x && item_rect.Max.x <= window_rect.Max.x; const bool fully_visible_y = item_rect.Min.y >= window_rect.Min.y && item_rect.Max.y <= window_rect.Max.y; - const bool can_be_fully_visible_x = item_rect.GetWidth() <= window_rect.GetWidth(); - const bool can_be_fully_visible_y = item_rect.GetHeight() <= window_rect.GetHeight(); + const bool can_be_fully_visible_x = (item_rect.GetWidth() + g.Style.ItemSpacing.x * 2.0f) <= window_rect.GetWidth(); + const bool can_be_fully_visible_y = (item_rect.GetHeight() + g.Style.ItemSpacing.y * 2.0f) <= window_rect.GetHeight(); if ((flags & ImGuiScrollFlags_KeepVisibleEdgeX) && !fully_visible_x) { @@ -8191,10 +8423,10 @@ void ImGui::SetScrollHereY(float center_y_ratio) void ImGui::BeginTooltip() { - BeginTooltipEx(ImGuiWindowFlags_None, ImGuiTooltipFlags_None); + BeginTooltipEx(ImGuiTooltipFlags_None, ImGuiWindowFlags_None); } -void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags) +void ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags) { ImGuiContext& g = *GImGui; @@ -8223,7 +8455,7 @@ void ImGui::BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags toolt ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; - Begin(window_name, NULL, flags | extra_flags); + Begin(window_name, NULL, flags | extra_window_flags); } void ImGui::EndTooltip() @@ -8234,7 +8466,7 @@ void ImGui::EndTooltip() void ImGui::SetTooltipV(const char* fmt, va_list args) { - BeginTooltipEx(0, ImGuiTooltipFlags_OverridePreviousTooltip); + BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None); TextV(fmt, args); EndTooltip(); } @@ -8302,6 +8534,16 @@ ImGuiWindow* ImGui::GetTopMostPopupModal() return NULL; } +ImGuiWindow* ImGui::GetTopMostAndVisiblePopupModal() +{ + ImGuiContext& g = *GImGui; + for (int n = g.OpenPopupStack.Size - 1; n >= 0; n--) + if (ImGuiWindow* popup = g.OpenPopupStack.Data[n].Window) + if ((popup->Flags & ImGuiWindowFlags_Modal) && IsWindowActiveAndVisible(popup)) + return popup; + return NULL; +} + void ImGui::OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; @@ -8394,7 +8636,7 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to bool ref_window_is_descendent_of_popup = false; for (int n = popup_count_to_keep; n < g.OpenPopupStack.Size; n++) if (ImGuiWindow* popup_window = g.OpenPopupStack[n].Window) - if (popup_window->RootWindow == ref_window->RootWindow) + if (IsWindowWithinBeginStackOf(ref_window, popup_window)) { ref_window_is_descendent_of_popup = true; break; @@ -8467,7 +8709,7 @@ void ImGui::CloseCurrentPopup() ImGuiWindow* parent_popup_window = g.OpenPopupStack[popup_idx - 1].Window; bool close_parent = false; if (popup_window && (popup_window->Flags & ImGuiWindowFlags_ChildMenu)) - if (parent_popup_window == NULL || !(parent_popup_window->Flags & ImGuiWindowFlags_Modal)) + if (parent_popup_window && !(parent_popup_window->Flags & ImGuiWindowFlags_MenuBar)) close_parent = true; if (!close_parent) break; @@ -8495,7 +8737,7 @@ bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) char name[20]; if (flags & ImGuiWindowFlags_ChildMenu) - ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth + ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginMenuCount); // Recycle windows based on depth else ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame @@ -8791,8 +9033,6 @@ void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id g.NavFocusScopeId = focus_scope_id; g.NavWindow->NavLastIds[nav_layer] = id; g.NavWindow->NavRectRel[nav_layer] = rect_rel; - //g.NavDisableHighlight = false; - //g.NavDisableMouseHover = g.NavMousePosDirty = true; } void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) @@ -8811,7 +9051,7 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; window->NavLastIds[nav_layer] = id; if (g.LastItemData.ID == id) - window->NavRectRel[nav_layer] = ImRect(g.LastItemData.NavRect.Min - window->Pos, g.LastItemData.NavRect.Max - window->Pos); + window->NavRectRel[nav_layer] = WindowRectAbsToRel(window, g.LastItemData.NavRect); if (g.ActiveIdSource == ImGuiInputSource_Nav) g.NavDisableMouseHover = true; @@ -8991,7 +9231,7 @@ static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) result->ID = g.LastItemData.ID; result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; result->InFlags = g.LastItemData.InFlags; - result->RectRel = ImRect(g.LastItemData.NavRect.Min - window->Pos, g.LastItemData.NavRect.Max - window->Pos); + result->RectRel = WindowRectAbsToRel(window, g.LastItemData.NavRect); } // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) @@ -9012,7 +9252,7 @@ static void ImGui::NavProcessItem() if (candidate_for_nav_default_focus || g.NavInitResultId == 0) { g.NavInitResultId = id; - g.NavInitResultRectRel = ImRect(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); + g.NavInitResultRectRel = WindowRectAbsToRel(window, nav_bb); } if (candidate_for_nav_default_focus) { @@ -9025,19 +9265,17 @@ static void ImGui::NavProcessItem() // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRect + scoring from a rect wrapped according to current wrapping policy) if (g.NavMoveScoringItems) { - if (item_flags & ImGuiItemFlags_Inputable) - g.NavTabbingInputableRemaining--; - - if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) + const bool is_tab_stop = (item_flags & ImGuiItemFlags_Inputable) && (item_flags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; + const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) != 0; + if (is_tabbing) + { + if (is_tab_stop || (g.NavMoveFlags & ImGuiNavMoveFlags_FocusApi)) + NavProcessItemForTabbingRequest(id); + } + else if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) { ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; - - if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) - { - if (g.NavTabbingInputableRemaining == 0) - NavMoveRequestResolveWithLastItem(); - } - else + if (!is_tabbing) { if (NavScoreItem(result)) NavApplyItemToResult(result); @@ -9059,7 +9297,54 @@ static void ImGui::NavProcessItem() g.NavLayer = window->DC.NavLayerCurrent; g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; g.NavIdIsAlive = true; - window->NavRectRel[window->DC.NavLayerCurrent] = ImRect(nav_bb.Min - window->Pos, nav_bb.Max - window->Pos); // Store item bounding box (relative to window position) + window->NavRectRel[window->DC.NavLayerCurrent] = WindowRectAbsToRel(window, nav_bb); // Store item bounding box (relative to window position) + } +} + +// Handle "scoring" of an item for a tabbing/focusing request initiated by NavUpdateCreateTabbingRequest(). +// Note that SetKeyboardFocusHere() API calls are considered tabbing requests! +// - Case 1: no nav/active id: set result to first eligible item, stop storing. +// - Case 2: tab forward: on ref id set counter, on counter elapse store result +// - Case 3: tab forward wrap: set result to first eligible item (preemptively), on ref id set counter, on next frame if counter hasn't elapsed store result. // FIXME-TABBING: Could be done as a next-frame forwarded request +// - Case 4: tab backward: store all results, on ref id pick prev, stop storing +// - Case 5: tab backward wrap: store all results, on ref id if no result keep storing until last // FIXME-TABBING: Could be done as next-frame forwarded requested +void ImGui::NavProcessItemForTabbingRequest(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + + // Always store in NavMoveResultLocal (unlike directional request which uses NavMoveResultOther on sibling/flattened windows) + ImGuiNavItemData* result = &g.NavMoveResultLocal; + if (g.NavTabbingDir == +1) + { + // Tab Forward or SetKeyboardFocusHere() with >= 0 + if (g.NavTabbingResultFirst.ID == 0) + NavApplyItemToResult(&g.NavTabbingResultFirst); + if (--g.NavTabbingCounter == 0) + NavMoveRequestResolveWithLastItem(result); + else if (g.NavId == id) + g.NavTabbingCounter = 1; + } + else if (g.NavTabbingDir == -1) + { + // Tab Backward + if (g.NavId == id) + { + if (result->ID) + { + g.NavMoveScoringItems = false; + NavUpdateAnyRequestFlag(); + } + } + else + { + NavApplyItemToResult(result); + } + } + else if (g.NavTabbingDir == 0) + { + // Tab Init + if (g.NavTabbingResultFirst.ID == 0) + NavMoveRequestResolveWithLastItem(&g.NavTabbingResultFirst); } } @@ -9086,18 +9371,18 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM g.NavMoveScrollFlags = scroll_flags; g.NavMoveForwardToNextFrame = false; g.NavMoveKeyMods = g.IO.KeyMods; - g.NavTabbingInputableRemaining = 0; + g.NavTabbingCounter = 0; g.NavMoveResultLocal.Clear(); g.NavMoveResultLocalVisible.Clear(); g.NavMoveResultOther.Clear(); NavUpdateAnyRequestFlag(); } -void ImGui::NavMoveRequestResolveWithLastItem() +void ImGui::NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result) { ImGuiContext& g = *GImGui; g.NavMoveScoringItems = false; // Ensure request doesn't need more processing - NavApplyItemToResult(&g.NavMoveResultLocal); + NavApplyItemToResult(result); NavUpdateAnyRequestFlag(); } @@ -9167,6 +9452,11 @@ void ImGui::NavRestoreLayer(ImGuiNavLayer layer) g.NavLayer = layer; NavInitWindow(window, true); } +} + +void ImGui::NavRestoreHighlightAfterMove() +{ + ImGuiContext& g = *GImGui; g.NavDisableHighlight = false; g.NavDisableMouseHover = g.NavMousePosDirty = true; } @@ -9214,7 +9504,8 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) static ImVec2 ImGui::NavCalcPreferredRefPos() { ImGuiContext& g = *GImGui; - if (g.NavDisableHighlight || !g.NavDisableMouseHover || !g.NavWindow) + ImGuiWindow* window = g.NavWindow; + if (g.NavDisableHighlight || !g.NavDisableMouseHover || !window) { // Mouse (we need a fallback in case the mouse becomes invalid after being used) if (IsMousePosValid(&g.IO.MousePos)) @@ -9223,9 +9514,15 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() } else { - // When navigation is active and mouse is disabled, decide on an arbitrary position around the bottom left of the currently navigated item. - const ImRect& rect_rel = g.NavWindow->NavRectRel[g.NavLayer]; - ImVec2 pos = g.NavWindow->Pos + ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); + // When navigation is active and mouse is disabled, pick a position around the bottom left of the currently navigated item + // Take account of upcoming scrolling (maybe set mouse pos should be done in EndFrame?) + ImRect rect_rel = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]); + if (window->LastFrameActive != g.FrameCount && (window->ScrollTarget.x != FLT_MAX || window->ScrollTarget.y != FLT_MAX)) + { + ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); + rect_rel.Translate(window->Scroll - next_scroll); + } + ImVec2 pos = ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); ImGuiViewport* viewport = GetMainViewport(); return ImFloor(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImFloor() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. } @@ -9256,6 +9553,8 @@ float ImGui::GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode) ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor, float fast_factor) { ImVec2 delta(0.0f, 0.0f); + if (dir_sources & ImGuiNavDirSourceFlags_RawKeyboard) + delta += ImVec2((float)IsKeyDown(GetKeyIndex(ImGuiKey_RightArrow)) - (float)IsKeyDown(GetKeyIndex(ImGuiKey_LeftArrow)), (float)IsKeyDown(GetKeyIndex(ImGuiKey_DownArrow)) - (float)IsKeyDown(GetKeyIndex(ImGuiKey_UpArrow))); if (dir_sources & ImGuiNavDirSourceFlags_Keyboard) delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode) - GetNavInputAmount(ImGuiNavInput_KeyLeft_, mode), GetNavInputAmount(ImGuiNavInput_KeyDown_, mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_, mode)); if (dir_sources & ImGuiNavDirSourceFlags_PadDPad) @@ -9320,24 +9619,15 @@ static void ImGui::NavUpdate() // Process navigation move request if (g.NavMoveSubmitted) NavMoveRequestApplyResult(); - g.NavTabbingInputableRemaining = 0; + g.NavTabbingCounter = 0; g.NavMoveSubmitted = g.NavMoveScoringItems = false; - // Apply application mouse position movement, after we had a chance to process move request result. + // Schedule mouse position update (will be done at the bottom of this function, after 1) processing all move requests and 2) updating scrolling) + bool set_mouse_pos = false; if (g.NavMousePosDirty && g.NavIdIsAlive) - { - // Set mouse position given our knowledge of the navigated item position from last frame - if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) - if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) - { - io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); - io.WantSetMousePos = true; - //IMGUI_DEBUG_LOG("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); - } - g.NavMousePosDirty = false; - } - g.NavIdIsAlive = false; - g.NavJustTabbedId = 0; + if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) + set_mouse_pos = true; + g.NavMousePosDirty = false; IM_ASSERT(g.NavLayer == ImGuiNavLayer_Main || g.NavLayer == ImGuiNavLayer_Menu); // Store our return window (for returning from Menu Layer to Main Layer) and clear it as soon as we step back in our own Layer 0 @@ -9399,7 +9689,10 @@ static void ImGui::NavUpdate() // Process move requests NavUpdateCreateMoveRequest(); + if (g.NavMoveDir == ImGuiDir_None) + NavUpdateCreateTabbingRequest(); NavUpdateAnyRequestFlag(); + g.NavIdIsAlive = false; // Scrolling if (g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs) && !g.NavWindowingTarget) @@ -9429,7 +9722,16 @@ static void ImGui::NavUpdate() if (!nav_keyboard_active && !nav_gamepad_active) { g.NavDisableHighlight = true; - g.NavDisableMouseHover = g.NavMousePosDirty = false; + g.NavDisableMouseHover = set_mouse_pos = false; + } + + // Update mouse position if requested + // (This will take into account the possibility that a Scroll was queued in the window to offset our absolute mouse position before scroll has been applied) + if (set_mouse_pos && (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) + { + io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); + io.WantSetMousePos = true; + //IMGUI_DEBUG_LOG("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); } // [DEBUG] @@ -9438,7 +9740,7 @@ static void ImGui::NavUpdate() if (g.NavWindow) { ImDrawList* draw_list = GetForegroundDrawList(g.NavWindow); - if (1) { for (int layer = 0; layer < 2; layer++) draw_list->AddRect(g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Min, g.NavWindow->Pos + g.NavWindow->NavRectRel[layer].Max, IM_COL32(255,200,0,255)); } // [DEBUG] + if (1) { for (int layer = 0; layer < 2; layer++) { ImRect r = WindowRectRelToAbs(g.NavWindow, g.NavWindow->NavRectRel[layer]); draw_list->AddRect(r.Min, r.Max, IM_COL32(255,200,0,255)); } } // [DEBUG] if (1) { ImU32 col = (!g.NavWindow->Hidden) ? IM_COL32(255,0,255,255) : IM_COL32(255,0,0,255); ImVec2 p = NavCalcPreferredRefPos(); char buf[32]; ImFormatString(buf, 32, "%d", g.NavLayer); draw_list->AddCircleFilled(p, 3.0f, col); draw_list->AddText(NULL, 13.0f, p + ImVec2(8,-4), col, buf); } } #endif @@ -9457,10 +9759,7 @@ void ImGui::NavInitRequestApplyResult() SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); g.NavIdIsAlive = true; // Mark as alive from previous frame as we got a result if (g.NavInitRequestFromMove) - { - g.NavDisableHighlight = false; - g.NavDisableMouseHover = g.NavMousePosDirty = true; - } + NavRestoreHighlightAfterMove(); } void ImGui::NavUpdateCreateMoveRequest() @@ -9492,6 +9791,7 @@ void ImGui::NavUpdateCreateMoveRequest() if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && (IsNavInputTest(ImGuiNavInput_DpadDown, read_mode) || IsNavInputTest(ImGuiNavInput_KeyDown_, read_mode))) { g.NavMoveDir = ImGuiDir_Down; } } g.NavMoveClipDir = g.NavMoveDir; + g.NavScoringNoClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX); } // Update PageUp/PageDown/Home/End scroll @@ -9500,6 +9800,11 @@ void ImGui::NavUpdateCreateMoveRequest() float scoring_rect_offset_y = 0.0f; if (window && g.NavMoveDir == ImGuiDir_None && nav_keyboard_active) scoring_rect_offset_y = NavUpdatePageUpPageDown(); + if (scoring_rect_offset_y != 0.0f) + { + g.NavScoringNoClipRect = window->InnerRect; + g.NavScoringNoClipRect.TranslateY(scoring_rect_offset_y); + } // [DEBUG] Always send a request #if IMGUI_DEBUG_NAV_SCORING @@ -9531,7 +9836,7 @@ void ImGui::NavUpdateCreateMoveRequest() // (can't focus a visible object like we can with the mouse). if (g.NavMoveSubmitted && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main && window != NULL) { - ImRect window_rect_rel(window->InnerRect.Min - window->Pos - ImVec2(1, 1), window->InnerRect.Max - window->Pos + ImVec2(1, 1)); + ImRect window_rect_rel = WindowRectAbsToRel(window, ImRect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1))); if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) { IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel\n"); @@ -9547,14 +9852,41 @@ void ImGui::NavUpdateCreateMoveRequest() if (window != NULL) { ImRect nav_rect_rel = !window->NavRectRel[g.NavLayer].IsInverted() ? window->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); - scoring_rect = ImRect(window->Pos + nav_rect_rel.Min, window->Pos + nav_rect_rel.Max); + scoring_rect = WindowRectRelToAbs(window, nav_rect_rel); scoring_rect.TranslateY(scoring_rect_offset_y); scoring_rect.Min.x = ImMin(scoring_rect.Min.x + 1.0f, scoring_rect.Max.x); scoring_rect.Max.x = scoring_rect.Min.x; IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). //GetForegroundDrawList()->AddRect(scoring_rect.Min, scoring_rect.Max, IM_COL32(255,200,0,255)); // [DEBUG] + //if (!g.NavScoringNoClipRect.IsInverted()) { GetForegroundDrawList()->AddRect(g.NavScoringNoClipRect.Min, g.NavScoringNoClipRect.Max, IM_COL32(255, 200, 0, 255)); } // [DEBUG] } g.NavScoringRect = scoring_rect; + g.NavScoringNoClipRect.Add(scoring_rect); +} + +void ImGui::NavUpdateCreateTabbingRequest() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.NavWindow; + IM_ASSERT(g.NavMoveDir == ImGuiDir_None); + if (window == NULL || g.NavWindowingTarget != NULL || (window->Flags & ImGuiWindowFlags_NoNavInputs)) + return; + + const bool tab_pressed = IsKeyPressedMap(ImGuiKey_Tab, true) && !IsActiveIdUsingKey(ImGuiKey_Tab) && !g.IO.KeyCtrl && !g.IO.KeyAlt; + if (!tab_pressed) + return; + + // Initiate tabbing request + // (this is ALWAYS ENABLED, regardless of ImGuiConfigFlags_NavEnableKeyboard flag!) + // Initially this was designed to use counters and modulo arithmetic, but that could not work with unsubmitted items (list clipper). Instead we use a strategy close to other move requests. + // See NavProcessItemForTabbingRequest() for a description of the various forward/backward tabbing cases with and without wrapping. + //// FIXME: We use (g.ActiveId == 0) but (g.NavDisableHighlight == false) might be righter once we can tab through anything + g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; + ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; + ImGuiDir clip_dir = (g.NavTabbingDir < 0) ? ImGuiDir_Up : ImGuiDir_Down; + NavMoveRequestSubmit(ImGuiDir_None, clip_dir, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + g.NavTabbingResultFirst.Clear(); + g.NavTabbingCounter = -1; } // Apply result from previous frame navigation directional move request. Always called from NavUpdate() @@ -9569,16 +9901,18 @@ void ImGui::NavMoveRequestApplyResult() // Select which result to use ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : (g.NavMoveResultOther.ID != 0) ? &g.NavMoveResultOther : NULL; + // Tabbing forward wrap + if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + if ((g.NavTabbingCounter == 1 || g.NavTabbingDir == 0) && g.NavTabbingResultFirst.ID) + result = &g.NavTabbingResultFirst; + // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) if (result == NULL) { if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) g.NavMoveFlags |= ImGuiNavMoveFlags_DontSetNavHighlight; if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) - { - g.NavDisableHighlight = false; - g.NavDisableMouseHover = true; - } + NavRestoreHighlightAfterMove(); return; } @@ -9596,27 +9930,22 @@ void ImGui::NavMoveRequestApplyResult() // Scroll to keep newly navigated item fully into view. if (g.NavLayer == ImGuiNavLayer_Main) { - ImVec2 delta_scroll; if (g.NavMoveFlags & ImGuiNavMoveFlags_ScrollToEdgeY) { // FIXME: Should remove this float scroll_target = (g.NavMoveDir == ImGuiDir_Up) ? result->Window->ScrollMax.y : 0.0f; - delta_scroll.y = result->Window->Scroll.y - scroll_target; SetScrollY(result->Window, scroll_target); } else { - ImRect rect_abs = ImRect(result->RectRel.Min + result->Window->Pos, result->RectRel.Max + result->Window->Pos); - delta_scroll = ScrollToRectEx(result->Window, rect_abs, g.NavMoveScrollFlags); + ImRect rect_abs = WindowRectRelToAbs(result->Window, result->RectRel); + ScrollToRectEx(result->Window, rect_abs, g.NavMoveScrollFlags); } - - // Offset our result position so mouse position can be applied immediately after in NavUpdate() - result->RectRel.TranslateX(-delta_scroll.x); - result->RectRel.TranslateY(-delta_scroll.y); } - ClearActiveID(); g.NavWindow = result->Window; + if (g.ActiveId != result->ID) + ClearActiveID(); if (g.NavId != result->ID) { // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) @@ -9646,10 +9975,7 @@ void ImGui::NavMoveRequestApplyResult() // Enable nav highlight if ((g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) - { - g.NavDisableHighlight = false; - g.NavDisableMouseHover = g.NavMousePosDirty = true; - } + NavRestoreHighlightAfterMove(); } // Process NavCancel input (to close a popup, get back to parent, clear focus) @@ -9672,6 +9998,7 @@ static void ImGui::NavUpdateCancelRequest() { // Leave the "menu" layer NavRestoreLayer(ImGuiNavLayer_Main); + NavRestoreHighlightAfterMove(); } else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) { @@ -9681,7 +10008,8 @@ static void ImGui::NavUpdateCancelRequest() IM_ASSERT(child_window->ChildId != 0); ImRect child_rect = child_window->Rect(); FocusWindow(parent_window); - SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, ImRect(child_rect.Min - parent_window->Pos, child_rect.Max - parent_window->Pos)); + SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, WindowRectAbsToRel(parent_window, child_rect)); + NavRestoreHighlightAfterMove(); } else if (g.OpenPopupStack.Size > 0) { @@ -9708,7 +10036,7 @@ static float ImGui::NavUpdatePageUpPageDown() ImGuiIO& io = g.IO; ImGuiWindow* window = g.NavWindow; - if ((window->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL || g.NavLayer != ImGuiNavLayer_Main) + if ((window->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL) return 0.0f; const bool page_up_held = IsKeyDown(io.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); @@ -9718,6 +10046,9 @@ static float ImGui::NavUpdatePageUpPageDown() if (page_up_held == page_down_held && home_pressed == end_pressed) // Proceed if either (not both) are pressed, otherwise early out return 0.0f; + if (g.NavLayer != ImGuiNavLayer_Main) + NavRestoreLayer(ImGuiNavLayer_Main); + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) { // Fallback manual-scroll when window has no navigable item @@ -9754,7 +10085,7 @@ static float ImGui::NavUpdatePageUpPageDown() // FIXME-NAV: handling of Home/End is assuming that the top/bottom most item will be visible with Scroll.y == 0/ScrollMax.y // Scrolling will be handled via the ImGuiNavMoveFlags_ScrollToEdgeY flag, we don't scroll immediately to avoid scrolling happening before nav result. // Preserve current horizontal position if we have any. - nav_rect_rel.Min.y = nav_rect_rel.Max.y = -window->Scroll.y; + nav_rect_rel.Min.y = nav_rect_rel.Max.y = 0.0f; if (nav_rect_rel.IsInverted()) nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; g.NavMoveDir = ImGuiDir_Down; @@ -9763,7 +10094,7 @@ static float ImGui::NavUpdatePageUpPageDown() } else if (end_pressed) { - nav_rect_rel.Min.y = nav_rect_rel.Max.y = window->ScrollMax.y + window->SizeFull.y - window->Scroll.y; + nav_rect_rel.Min.y = nav_rect_rel.Max.y = window->ContentSize.y; if (nav_rect_rel.IsInverted()) nav_rect_rel.Min.x = nav_rect_rel.Max.x = 0.0f; g.NavMoveDir = ImGuiDir_Up; @@ -9784,64 +10115,66 @@ static void ImGui::NavEndFrame() NavUpdateWindowingOverlay(); // Perform wrap-around in menus + // FIXME-NAV: Wrap may need to apply a weight bias on the other axis. e.g. 4x4 grid with 2 last items missing on last item won't handle LoopY/WrapY correctly. // FIXME-NAV: Wrap (not Loop) support could be handled by the scoring function and then WrapX would function without an extra frame. + const ImGuiNavMoveFlags wanted_flags = ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY; + if (g.NavWindow && NavMoveRequestButNoResultYet() && (g.NavMoveFlags & wanted_flags) && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) + NavUpdateCreateWrappingRequest(); +} + +static void ImGui::NavUpdateCreateWrappingRequest() +{ + ImGuiContext& g = *GImGui; ImGuiWindow* window = g.NavWindow; + + bool do_forward = false; + ImRect bb_rel = window->NavRectRel[g.NavLayer]; + ImGuiDir clip_dir = g.NavMoveDir; const ImGuiNavMoveFlags move_flags = g.NavMoveFlags; - const ImGuiNavMoveFlags wanted_flags = ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY; - if (window && NavMoveRequestButNoResultYet() && (g.NavMoveFlags & wanted_flags) && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) + if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { - bool do_forward = false; - ImRect bb_rel = window->NavRectRel[g.NavLayer]; - ImGuiDir clip_dir = g.NavMoveDir; - if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) - { - bb_rel.Min.x = bb_rel.Max.x = - ImMax(window->SizeFull.x, window->ContentSize.x + window->WindowPadding.x * 2.0f) - window->Scroll.x; - if (move_flags & ImGuiNavMoveFlags_WrapX) - { - bb_rel.TranslateY(-bb_rel.GetHeight()); - clip_dir = ImGuiDir_Up; - } - do_forward = true; - } - if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + bb_rel.Min.x = bb_rel.Max.x = window->ContentSize.x + window->WindowPadding.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) { - bb_rel.Min.x = bb_rel.Max.x = -window->Scroll.x; - if (move_flags & ImGuiNavMoveFlags_WrapX) - { - bb_rel.TranslateY(+bb_rel.GetHeight()); - clip_dir = ImGuiDir_Down; - } - do_forward = true; + bb_rel.TranslateY(-bb_rel.GetHeight()); // Previous row + clip_dir = ImGuiDir_Up; } - const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); - if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + do_forward = true; + } + if (g.NavMoveDir == ImGuiDir_Right && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) + { + bb_rel.Min.x = bb_rel.Max.x = -window->WindowPadding.x; + if (move_flags & ImGuiNavMoveFlags_WrapX) { - bb_rel.Min.y = bb_rel.Max.y = - ImMax(window->SizeFull.y, window->ContentSize.y + window->WindowPadding.y * 2.0f) - window->Scroll.y + decoration_up_height; - if (move_flags & ImGuiNavMoveFlags_WrapY) - { - bb_rel.TranslateX(-bb_rel.GetWidth()); - clip_dir = ImGuiDir_Left; - } - do_forward = true; + bb_rel.TranslateY(+bb_rel.GetHeight()); // Next row + clip_dir = ImGuiDir_Down; } - if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + do_forward = true; + } + if (g.NavMoveDir == ImGuiDir_Up && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = window->ContentSize.y + window->WindowPadding.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) { - bb_rel.Min.y = bb_rel.Max.y = -window->Scroll.y + decoration_up_height; - if (move_flags & ImGuiNavMoveFlags_WrapY) - { - bb_rel.TranslateX(+bb_rel.GetWidth()); - clip_dir = ImGuiDir_Right; - } - do_forward = true; + bb_rel.TranslateX(-bb_rel.GetWidth()); // Previous column + clip_dir = ImGuiDir_Left; } - if (do_forward) + do_forward = true; + } + if (g.NavMoveDir == ImGuiDir_Down && (move_flags & (ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY))) + { + bb_rel.Min.y = bb_rel.Max.y = -window->WindowPadding.y; + if (move_flags & ImGuiNavMoveFlags_WrapY) { - window->NavRectRel[g.NavLayer] = bb_rel; - NavMoveRequestForward(g.NavMoveDir, clip_dir, move_flags, g.NavMoveScrollFlags); + bb_rel.TranslateX(+bb_rel.GetWidth()); // Next column + clip_dir = ImGuiDir_Right; } + do_forward = true; } + if (!do_forward) + return; + window->NavRectRel[g.NavLayer] = bb_rel; + NavMoveRequestForward(g.NavMoveDir, clip_dir, move_flags, g.NavMoveScrollFlags); } static int ImGui::FindWindowFocusIndex(ImGuiWindow* window) @@ -9903,10 +10236,9 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingTargetAnim = NULL; } - // Start CTRL-TAB or Square+L/R window selection - const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + // Start CTRL+Tab or Square+L/R window selection const bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); - const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && nav_keyboard_active && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab); + const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab); if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { @@ -9957,6 +10289,7 @@ static void ImGui::NavUpdateWindowing() // Keyboard: Press and Release ALT to toggle menu layer // - Testing that only Alt is tested prevents Alt+Shift or AltGR from toggling menu layer. // - AltGR is normally Alt+Ctrl but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). But even on keyboards without AltGR we don't want Alt+Ctrl to open menu anyway. + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; if (nav_keyboard_active && io.KeyMods == ImGuiKeyModFlags_Alt && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) == 0) { g.NavWindowingToggleLayer = true; @@ -9985,7 +10318,7 @@ static void ImGui::NavUpdateWindowing() { ImVec2 move_delta; if (g.NavInputSource == ImGuiInputSource_Keyboard && !io.KeyShift) - move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard, ImGuiInputReadMode_Down); + move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_RawKeyboard, ImGuiInputReadMode_Down); if (g.NavInputSource == ImGuiInputSource_Gamepad) move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); if (move_delta.x != 0.0f || move_delta.y != 0.0f) @@ -10003,8 +10336,7 @@ static void ImGui::NavUpdateWindowing() if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindow)) { ClearActiveID(); - g.NavDisableHighlight = false; - g.NavDisableMouseHover = true; + NavRestoreHighlightAfterMove(); apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window); ClosePopupsOverWindow(apply_focus_window, false); FocusWindow(apply_focus_window); @@ -10051,6 +10383,7 @@ static void ImGui::NavUpdateWindowing() if (new_nav_layer == ImGuiNavLayer_Menu) g.NavWindow->NavLastIds[new_nav_layer] = 0; NavRestoreLayer(new_nav_layer); + NavRestoreHighlightAfterMove(); } } } @@ -10157,16 +10490,16 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) return false; // If you want to use BeginDragDropSource() on an item with no unique identifier for interaction, such as Text() or Image(), you need to: - // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag, C) Swallow your programmer pride. + // A) Read the explanation below, B) Use the ImGuiDragDropFlags_SourceAllowNullID flag. if (!(flags & ImGuiDragDropFlags_SourceAllowNullID)) { IM_ASSERT(0); return false; } - // Magic fallback (=somehow reprehensible) to handle items with no assigned ID, e.g. Text(), Image() + // Magic fallback to handle items with no assigned ID, e.g. Text(), Image() // We build a throwaway ID based on current ID stack + relative AABB of items in window. - // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING OF THE WIDGET, so if your widget moves your dragging operation will be canceled. + // THE IDENTIFIER WON'T SURVIVE ANY REPOSITIONING/RESIZINGG OF THE WIDGET, so if your widget moves your dragging operation will be canceled. // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. // Rely on keeping other window->LastItemXXX fields intact. source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); @@ -11183,6 +11516,7 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} // - DebugNodeWindow() [Internal] // - DebugNodeWindowSettings() [Internal] // - DebugNodeWindowsList() [Internal] +// - DebugNodeWindowsListByBeginStackParent() [Internal] //----------------------------------------------------------------------------- #ifndef IMGUI_DISABLE_METRICS_WINDOW @@ -11405,8 +11739,27 @@ void ImGui::ShowMetricsWindow(bool* p_open) } // Windows - DebugNodeWindowsList(&g.Windows, "Windows"); - //DebugNodeWindowsList(&g.WindowsFocusOrder, "WindowsFocusOrder"); + if (TreeNode("Windows", "Windows (%d)", g.Windows.Size)) + { + //SetNextItemOpen(true, ImGuiCond_Once); + DebugNodeWindowsList(&g.Windows, "By display order"); + DebugNodeWindowsList(&g.WindowsFocusOrder, "By focus order (root windows)"); + if (TreeNode("By submission order (begin stack)")) + { + // Here we display windows in their submitted order/hierarchy, however note that the Begin stack doesn't constitute a Parent<>Child relationship! + ImVector& temp_buffer = g.WindowsTempSortBuffer; + temp_buffer.resize(0); + for (int i = 0; i < g.Windows.Size; i++) + if (g.Windows[i]->LastFrameActive + 1 >= g.FrameCount) + temp_buffer.push_back(g.Windows[i]); + struct Func { static int IMGUI_CDECL WindowComparerByBeginOrder(const void* lhs, const void* rhs) { return ((int)(*(const ImGuiWindow* const *)lhs)->BeginOrderWithinContext - (*(const ImGuiWindow* const*)rhs)->BeginOrderWithinContext); } }; + ImQsort(temp_buffer.Data, (size_t)temp_buffer.Size, sizeof(ImGuiWindow*), Func::WindowComparerByBeginOrder); + DebugNodeWindowsListByBeginStackParent(temp_buffer.Data, temp_buffer.Size, NULL); + TreePop(); + } + + TreePop(); + } // DrawLists int drawlist_count = 0; @@ -11685,7 +12038,7 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, } ImDrawList* fg_draw_list = GetForegroundDrawList(window); // Render additional visuals into the top-most draw list - if (window && IsItemHovered()) + if (window && IsItemHovered() && fg_draw_list) fg_draw_list->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 255, 0, 255)); if (!node_open) return; @@ -12030,7 +12383,6 @@ void ImGui::DebugNodeWindowsList(ImVector* windows, const char* la { if (!TreeNode(label, "%s (%d)", label, windows->Size)) return; - Text("(In front-to-back order:)"); for (int i = windows->Size - 1; i >= 0; i--) // Iterate front to back { PushID((*windows)[i]); @@ -12040,6 +12392,24 @@ void ImGui::DebugNodeWindowsList(ImVector* windows, const char* la TreePop(); } +// FIXME-OPT: This is technically suboptimal, but it is simpler this way. +void ImGui::DebugNodeWindowsListByBeginStackParent(ImGuiWindow** windows, int windows_size, ImGuiWindow* parent_in_begin_stack) +{ + for (int i = 0; i < windows_size; i++) + { + ImGuiWindow* window = windows[i]; + if (window->ParentWindowInBeginStack != parent_in_begin_stack) + continue; + char buf[20]; + ImFormatString(buf, IM_ARRAYSIZE(buf), "[%04d] Window", window->BeginOrderWithinContext); + //BulletText("[%04d] Window '%s'", window->BeginOrderWithinContext, window->Name); + DebugNodeWindow(window, buf); + Indent(); + DebugNodeWindowsListByBeginStackParent(windows + i + 1, windows_size - i - 1, window); + Unindent(); + } +} + //----------------------------------------------------------------------------- // [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) //----------------------------------------------------------------------------- @@ -12082,7 +12452,7 @@ void ImGui::UpdateDebugToolStackQueries() // Update queries. The steps are: -1: query Stack, >= 0: query each stack item // We can only perform 1 ID Info query every frame. This is designed so the GetID() tests are cheap and constant-time - const ImGuiID query_id = g.ActiveId ? g.ActiveId : g.HoveredIdPreviousFrame; + const ImGuiID query_id = g.HoveredIdPreviousFrame ? g.HoveredIdPreviousFrame : g.ActiveId; if (tool->QueryId != query_id) { tool->QueryId = query_id; @@ -12160,6 +12530,9 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat // Stack Tool: Display UI void ImGui::ShowStackToolWindow(bool* p_open) { + ImGuiContext& g = *GImGui; + if (!(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize)) + SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 8.0f), ImGuiCond_FirstUseEver); if (!Begin("Dear ImGui Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1) { End(); @@ -12167,7 +12540,6 @@ void ImGui::ShowStackToolWindow(bool* p_open) } // Display hovered/active status - ImGuiContext& g = *GImGui; const ImGuiID hovered_id = g.HoveredIdPreviousFrame; const ImGuiID active_id = g.ActiveId; #ifdef IMGUI_ENABLE_TEST_ENGINE diff --git a/imgui/imgui.h b/imgui/imgui.h index f2606398..e986e6c0 100644 --- a/imgui/imgui.h +++ b/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (headers) // Help: @@ -63,8 +63,8 @@ Index of this file: // Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.85" -#define IMGUI_VERSION_NUM 18500 +#define IMGUI_VERSION "1.86" +#define IMGUI_VERSION_NUM 18600 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) #define IMGUI_HAS_TABLE @@ -152,7 +152,7 @@ struct ImGuiContext; // Dear ImGui context (opaque structure, unl struct ImGuiIO; // Main configuration and I/O between your application and ImGui struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) struct ImGuiListClipper; // Helper to manually clip large list of items -struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame, used by IMGUI_ONCE_UPON_A_FRAME macro +struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame struct ImGuiPayload; // User data payload for drag and drop operations struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) struct ImGuiStorage; // Helper for key->value storage @@ -869,7 +869,6 @@ namespace ImGui IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). IMGUI_API void SetStateStorage(ImGuiStorage* storage); // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it) IMGUI_API ImGuiStorage* GetStateStorage(); - IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // calculate coarse clipping for large list of evenly sized items. Prefer using the ImGuiListClipper higher-level helper if you can. IMGUI_API bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags flags = 0); // helper to create a child window / scrolling region that looks like a normal widget frame IMGUI_API void EndChildFrame(); // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window) @@ -897,9 +896,10 @@ namespace ImGui // - You can also use regular integer: it is forever guaranteed that 0=Left, 1=Right, 2=Middle. // - Dragging operations are only reported after mouse has moved a certain distance away from the initial clicking position (see 'lock_threshold' and 'io.MouseDraggingThreshold') IMGUI_API bool IsMouseDown(ImGuiMouseButton button); // is mouse button held? - IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, bool repeat = false); // did mouse button clicked? (went from !Down to Down) + IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, bool repeat = false); // did mouse button clicked? (went from !Down to Down). Same as GetMouseClickedCount() == 1. IMGUI_API bool IsMouseReleased(ImGuiMouseButton button); // did mouse button released? (went from Down to !Down) - IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button); // did mouse button double-clicked? (note that a double-click will also report IsMouseClicked() == true) + IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button); // did mouse button double-clicked? Same as GetMouseClickedCount() == 2. (note that a double-click will also report IsMouseClicked() == true) + IMGUI_API int GetMouseClickedCount(ImGuiMouseButton button); // return the number of successive mouse-clicks at the time where a click happen (otherwise 0). IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);// is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse available IMGUI_API bool IsAnyMouseDown(); // is any mouse button held? @@ -954,7 +954,7 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_NoMove = 1 << 2, // Disable user moving the window ImGuiWindowFlags_NoScrollbar = 1 << 3, // Disable scrollbars (window can still scroll with mouse or programmatically) ImGuiWindowFlags_NoScrollWithMouse = 1 << 4, // Disable user vertically scrolling with mouse wheel. On child window, mouse wheel will be forwarded to the parent unless NoScrollbar is also set. - ImGuiWindowFlags_NoCollapse = 1 << 5, // Disable user collapsing window by double-clicking on it + ImGuiWindowFlags_NoCollapse = 1 << 5, // Disable user collapsing window by double-clicking on it. Also referred to as Window Menu Button (e.g. within a docking node). ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, // Resize every window to its content every frame ImGuiWindowFlags_NoBackground = 1 << 7, // Disable drawing background color (WindowBg, etc.) and outside border. Similar as using SetNextWindowBgAlpha(0.0f). ImGuiWindowFlags_NoSavedSettings = 1 << 8, // Never load/save settings in .ini file @@ -974,7 +974,7 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, // [Internal] - ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] Allow gamepad/keyboard navigation to cross over parent border to this child (only use on child that have no scrolling!) + ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] On child window: allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. ImGuiWindowFlags_ChildWindow = 1 << 24, // Don't use! For internal use by BeginChild() ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() @@ -1742,7 +1742,7 @@ struct ImVector inline void pop_back() { IM_ASSERT(Size > 0); Size--; } inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } - inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last > it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - count) * sizeof(T)); Size -= (int)count; return Data + off; } + inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last > it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - (size_t)count) * sizeof(T)); Size -= (int)count; return Data + off; } inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; if (it < Data + Size - 1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } inline T* insert(const T* it, const T& v) { IM_ASSERT(it >= Data && it <= Data + Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; } inline bool contains(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; } @@ -1930,12 +1930,13 @@ struct ImGuiIO ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) ImVec2 MouseClickedPos[5]; // Position at time of clicking double MouseClickedTime[5]; // Time of last click (used to figure out double-click) - bool MouseClicked[5]; // Mouse button went from !Down to Down - bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? + bool MouseClicked[5]; // Mouse button went from !Down to Down (same as MouseClickedCount[x] != 0) + bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? (same as MouseClickedCount[x] == 2) + ImU16 MouseClickedCount[5]; // == 0 (not clicked), == 1 (same as MouseClicked[]), == 2 (double-clicked), == 3 (triple-clicked) etc. when going from !Down to Down + ImU16 MouseClickedLastCount[5]; // Count successive number of clicks. Stays valid after mouse release. Reset after another click is done. bool MouseReleased[5]; // Mouse button went from Down to !Down bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. bool MouseDownOwnedUnlessPopupClose[5];//Track if button was clicked inside a dear imgui window. - bool MouseDownWasDoubleClick[5]; // Track if button down was a double-click float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) float MouseDownDurationPrev[5]; // Previous time the mouse button has been down ImVec2 MouseDragMaxDistanceAbs[5]; // Maximum distance, absolute, on each axis, of how much mouse has traveled from the clicking point @@ -2170,10 +2171,12 @@ struct ImGuiStorage }; // Helper: Manually clip large list of items. -// If you are submitting lots of evenly spaced items and you have a random access to the list, you can perform coarse -// clipping based on visibility to save yourself from processing those items at all. +// If you have lots evenly spaced items and you have a random access to the list, you can perform coarse +// clipping based on visibility to only submit items that are in view. // The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped. -// (Dear ImGui already clip items based on their bounds but it needs to measure text size to do so, whereas manual coarse clipping before submission makes this cost and your own data fetching/submission cost almost null) +// (Dear ImGui already clip items based on their bounds but: it needs to first layout the item to do so, and generally +// fetching/submitting your own data incurs additional cost. Coarse clipping using ImGuiListClipper allows you to easily +// scale using lists with tens of thousands of items without a problem) // Usage: // ImGuiListClipper clipper; // clipper.Begin(1000); // We have 1000 elements, evenly spaced. @@ -2182,30 +2185,30 @@ struct ImGuiStorage // ImGui::Text("line number %d", i); // Generally what happens is: // - Clipper lets you process the first element (DisplayStart = 0, DisplayEnd = 1) regardless of it being visible or not. -// - User code submit one element. +// - User code submit that one element. // - Clipper can measure the height of the first element // - Clipper calculate the actual range of elements to display based on the current clipping rectangle, position the cursor before the first visible element. // - User code submit visible elements. +// - The clipper also handles various subtleties related to keyboard/gamepad navigation, wrapping etc. struct ImGuiListClipper { - int DisplayStart; - int DisplayEnd; - - // [Internal] - int ItemsCount; - int StepNo; - int ItemsFrozen; - float ItemsHeight; - float StartPosY; + int DisplayStart; // First item to display, updated by each call to Step() + int DisplayEnd; // End of items to display (exclusive) + int ItemsCount; // [Internal] Number of items + float ItemsHeight; // [Internal] Height of item after a first step and item submission can calculate it + float StartPosY; // [Internal] Cursor position at the time of Begin() or after table frozen rows are all processed + void* TempData; // [Internal] Internal data + // items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step) + // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). IMGUI_API ImGuiListClipper(); IMGUI_API ~ImGuiListClipper(); + IMGUI_API void Begin(int items_count, float items_height = -1.0f); + IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. + IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. - // items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step) - // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). - IMGUI_API void Begin(int items_count, float items_height = -1.0f); // Automatically called by constructor if you passed 'items_count' or by Step() in Step 1. - IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. - IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. + // Call ForceDisplayRangeByIndices() before first call to Step() if you need a range of items to be displayed regardless of visibility. + IMGUI_API void ForceDisplayRangeByIndices(int item_min, int item_max); // item_max is exclusive e.g. use (42, 42+1) to make item 42 always visible BUT due to alignment/padding of certain items it is likely that an extra item may be included on either end of the display range. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79] @@ -2828,6 +2831,8 @@ struct ImGuiViewport #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.86 (from November 2021) + IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Calculate coarse clipping for large list of evenly sized items. Prefer using ImGuiListClipper. // OBSOLETED in 1.85 (from August 2021) static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } // OBSOLETED in 1.81 (from February 2021) diff --git a/imgui/imgui_demo.cpp b/imgui/imgui_demo.cpp index d4bac484..9cbc503a 100644 --- a/imgui/imgui_demo.cpp +++ b/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (demo code) // Help: @@ -200,6 +200,14 @@ static void HelpMarker(const char* desc) } } +// Helper to wire demo markers located in code to a interactive browser +typedef void (*ImGuiDemoMarkerCallback)(const char* file, int line, const char* section, void* user_data); +extern ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback; +extern void* GImGuiDemoMarkerCallbackUserData; +ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback = NULL; +void* GImGuiDemoMarkerCallbackUserData = NULL; +#define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback(__FILE__, __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0) + // Helper to display basic user controls. void ImGui::ShowUserGuide() { @@ -210,7 +218,8 @@ void ImGui::ShowUserGuide() "(double-click to auto fit window to its contents)."); ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); - if (io.FontAllowUserScaling) + ImGui::BulletText("CTRL+Tab to select a window."); + if (io.FontAllowUserScaling) ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents."); ImGui::BulletText("While inputing text:\n"); ImGui::Indent(); @@ -228,7 +237,6 @@ void ImGui::ShowUserGuide() ImGui::BulletText("Return to input text into a widget."); ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window."); ImGui::BulletText("Alt to jump to the menu layer of a window."); - ImGui::BulletText("CTRL+Tab to select a window."); ImGui::Unindent(); } @@ -361,11 +369,13 @@ void ImGui::ShowDemoWindow(bool* p_open) { if (ImGui::BeginMenu("Menu")) { + IMGUI_DEMO_MARKER("Menu/File"); ShowExampleMenuFile(); ImGui::EndMenu(); } if (ImGui::BeginMenu("Examples")) { + IMGUI_DEMO_MARKER("Menu/Examples"); ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar); ImGui::MenuItem("Console", NULL, &show_app_console); ImGui::MenuItem("Log", NULL, &show_app_log); @@ -384,6 +394,7 @@ void ImGui::ShowDemoWindow(bool* p_open) //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar! if (ImGui::BeginMenu("Tools")) { + IMGUI_DEMO_MARKER("Menu/Tools"); #ifndef IMGUI_DISABLE_METRICS_WINDOW ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics); ImGui::MenuItem("Stack Tool", NULL, &show_app_stack_tool); @@ -398,6 +409,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Text("dear imgui says hello. (%s)", IMGUI_VERSION); ImGui::Spacing(); + IMGUI_DEMO_MARKER("Help"); if (ImGui::CollapsingHeader("Help")) { ImGui::Text("ABOUT THIS DEMO:"); @@ -420,6 +432,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::ShowUserGuide(); } + IMGUI_DEMO_MARKER("Configuration"); if (ImGui::CollapsingHeader("Configuration")) { ImGuiIO& io = ImGui::GetIO(); @@ -460,6 +473,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Separator(); } + IMGUI_DEMO_MARKER("Configuration/Backend Flags"); if (ImGui::TreeNode("Backend Flags")) { HelpMarker( @@ -476,6 +490,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Separator(); } + IMGUI_DEMO_MARKER("Configuration/Style"); if (ImGui::TreeNode("Style")) { HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function."); @@ -484,6 +499,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Separator(); } + IMGUI_DEMO_MARKER("Configuration/Capture, Logging"); if (ImGui::TreeNode("Capture/Logging")) { HelpMarker( @@ -503,6 +519,7 @@ void ImGui::ShowDemoWindow(bool* p_open) } } + IMGUI_DEMO_MARKER("Window options"); if (ImGui::CollapsingHeader("Window options")) { if (ImGui::BeginTable("split", 3)) @@ -536,6 +553,7 @@ void ImGui::ShowDemoWindow(bool* p_open) static void ShowDemoWindowWidgets() { + IMGUI_DEMO_MARKER("Widgets"); if (!ImGui::CollapsingHeader("Widgets")) return; @@ -543,8 +561,10 @@ static void ShowDemoWindowWidgets() if (disable_all) ImGui::BeginDisabled(); + IMGUI_DEMO_MARKER("Widgets/Basic"); if (ImGui::TreeNode("Basic")) { + IMGUI_DEMO_MARKER("Widgets/Basic/Button"); static int clicked = 0; if (ImGui::Button("Button")) clicked++; @@ -554,15 +574,18 @@ static void ShowDemoWindowWidgets() ImGui::Text("Thanks for clicking me!"); } + IMGUI_DEMO_MARKER("Widgets/Basic/Checkbox"); static bool check = true; ImGui::Checkbox("checkbox", &check); + IMGUI_DEMO_MARKER("Widgets/Basic/RadioButton"); static int e = 0; ImGui::RadioButton("radio a", &e, 0); ImGui::SameLine(); ImGui::RadioButton("radio b", &e, 1); ImGui::SameLine(); ImGui::RadioButton("radio c", &e, 2); // Color buttons, demonstrate using PushID() to add unique identifier in the ID stack, and changing style. + IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Colored)"); for (int i = 0; i < 7; i++) { if (i > 0) @@ -584,6 +607,7 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); // Arrow buttons with Repeater + IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Repeating)"); static int counter = 0; float spacing = ImGui::GetStyle().ItemInnerSpacing.x; ImGui::PushButtonRepeat(true); @@ -594,6 +618,7 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); ImGui::Text("%d", counter); + IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); ImGui::Text("Hover over me"); if (ImGui::IsItemHovered()) ImGui::SetTooltip("I am a tooltip"); @@ -610,12 +635,12 @@ static void ShowDemoWindowWidgets() } ImGui::Separator(); - ImGui::LabelText("label", "Value"); { // Using the _simplified_ one-liner Combo() api here // See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api. + IMGUI_DEMO_MARKER("Widgets/Basic/Combo"); const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; static int item_current = 0; ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); @@ -626,6 +651,7 @@ static void ShowDemoWindowWidgets() { // To wire InputText() with std::string or any other custom string type, // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. + IMGUI_DEMO_MARKER("Widgets/Basic/InputText"); static char str0[128] = "Hello, world!"; ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0)); ImGui::SameLine(); HelpMarker( @@ -644,6 +670,7 @@ static void ShowDemoWindowWidgets() static char str1[128] = ""; ImGui::InputTextWithHint("input text (w/ hint)", "enter text here", str1, IM_ARRAYSIZE(str1)); + IMGUI_DEMO_MARKER("Widgets/Basic/InputInt, InputFloat"); static int i0 = 123; ImGui::InputInt("input int", &i0); ImGui::SameLine(); HelpMarker( @@ -668,6 +695,7 @@ static void ShowDemoWindowWidgets() } { + IMGUI_DEMO_MARKER("Widgets/Basic/DragInt, DragFloat"); static int i1 = 50, i2 = 42; ImGui::DragInt("drag int", &i1, 1); ImGui::SameLine(); HelpMarker( @@ -683,6 +711,7 @@ static void ShowDemoWindowWidgets() } { + IMGUI_DEMO_MARKER("Widgets/Basic/SliderInt, SliderFloat"); static int i1 = 0; ImGui::SliderInt("slider int", &i1, -1, 3); ImGui::SameLine(); HelpMarker("CTRL+click to input value."); @@ -691,12 +720,14 @@ static void ShowDemoWindowWidgets() ImGui::SliderFloat("slider float", &f1, 0.0f, 1.0f, "ratio = %.3f"); ImGui::SliderFloat("slider float (log)", &f2, -10.0f, 10.0f, "%.4f", ImGuiSliderFlags_Logarithmic); + IMGUI_DEMO_MARKER("Widgets/Basic/SliderAngle"); static float angle = 0.0f; ImGui::SliderAngle("slider angle", &angle); // Using the format string to display a name instead of an integer. // Here we completely omit '%d' from the format string, so it'll only display a name. // This technique can also be used with DragInt(). + IMGUI_DEMO_MARKER("Widgets/Basic/Slider (enum)"); enum Element { Element_Fire, Element_Earth, Element_Air, Element_Water, Element_COUNT }; static int elem = Element_Fire; const char* elems_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" }; @@ -706,6 +737,7 @@ static void ShowDemoWindowWidgets() } { + IMGUI_DEMO_MARKER("Widgets/Basic/ColorEdit3, ColorEdit4"); static float col1[3] = { 1.0f, 0.0f, 0.2f }; static float col2[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::ColorEdit3("color 1", col1); @@ -721,6 +753,7 @@ static void ShowDemoWindowWidgets() { // Using the _simplified_ one-liner ListBox() api here // See "List boxes" section for examples of how to use the more flexible BeginListBox()/EndListBox() api. + IMGUI_DEMO_MARKER("Widgets/Basic/ListBox"); const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pineapple", "Strawberry", "Watermelon" }; static int item_current = 1; ImGui::ListBox("listbox", &item_current, items, IM_ARRAYSIZE(items), 4); @@ -737,8 +770,10 @@ static void ShowDemoWindowWidgets() // if (once) // ImGui::Text("This will be displayed only once."); + IMGUI_DEMO_MARKER("Widgets/Trees"); if (ImGui::TreeNode("Trees")) { + IMGUI_DEMO_MARKER("Widgets/Trees/Basic trees"); if (ImGui::TreeNode("Basic trees")) { for (int i = 0; i < 5; i++) @@ -759,6 +794,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Trees/Advanced, with Selectable nodes"); if (ImGui::TreeNode("Advanced, with Selectable nodes")) { HelpMarker( @@ -786,6 +822,7 @@ static void ShowDemoWindowWidgets() for (int i = 0; i < 6; i++) { // Disable the default "open on single-click behavior" + set Selected flag according to our selection. + // To alter selection we use IsItemClicked() && !IsItemToggledOpen(), so clicking on an arrow doesn't alter selection. ImGuiTreeNodeFlags node_flags = base_flags; const bool is_selected = (selection_mask & (1 << i)) != 0; if (is_selected) @@ -794,7 +831,7 @@ static void ShowDemoWindowWidgets() { // Items 0..2 are Tree Node bool node_open = ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Node %d", i); - if (ImGui::IsItemClicked()) + if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) node_clicked = i; if (test_drag_and_drop && ImGui::BeginDragDropSource()) { @@ -815,7 +852,7 @@ static void ShowDemoWindowWidgets() // use BulletText() or advance the cursor by GetTreeNodeToLabelSpacing() and call Text(). node_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen; // ImGuiTreeNodeFlags_Bullet ImGui::TreeNodeEx((void*)(intptr_t)i, node_flags, "Selectable Leaf %d", i); - if (ImGui::IsItemClicked()) + if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) node_clicked = i; if (test_drag_and_drop && ImGui::BeginDragDropSource()) { @@ -841,6 +878,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Collapsing Headers"); if (ImGui::TreeNode("Collapsing Headers")) { static bool closable_group = true; @@ -864,6 +902,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Bullets"); if (ImGui::TreeNode("Bullets")) { ImGui::BulletText("Bullet point 1"); @@ -878,8 +917,10 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text"); if (ImGui::TreeNode("Text")) { + IMGUI_DEMO_MARKER("Widgets/Text/Colored Text"); if (ImGui::TreeNode("Colorful Text")) { // Using shortcut. You can use PushStyleColor()/PopStyleColor() for more flexibility. @@ -890,6 +931,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text/Word Wrapping"); if (ImGui::TreeNode("Word Wrapping")) { // Using shortcut. You can use PushTextWrapPos()/PopTextWrapPos() for more flexibility. @@ -923,6 +965,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text/UTF-8 Text"); if (ImGui::TreeNode("UTF-8 Text")) { // UTF-8 test with Japanese characters @@ -949,6 +992,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Images"); if (ImGui::TreeNode("Images")) { ImGuiIO& io = ImGui::GetIO(); @@ -1002,6 +1046,8 @@ static void ShowDemoWindowWidgets() ImGui::EndTooltip(); } } + + IMGUI_DEMO_MARKER("Widgets/Images/Textured buttons"); ImGui::TextWrapped("And now some textured buttons.."); static int pressed_count = 0; for (int i = 0; i < 8; i++) @@ -1023,6 +1069,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Combo"); if (ImGui::TreeNode("Combo")) { // Expose flags as checkbox for the demo @@ -1073,6 +1120,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/List Boxes"); if (ImGui::TreeNode("List boxes")) { // Using the generic BeginListBox() API, you have full control over how to display the combo contents. @@ -1115,6 +1163,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables"); if (ImGui::TreeNode("Selectables")) { // Selectable() has 2 overloads: @@ -1123,6 +1172,7 @@ static void ShowDemoWindowWidgets() // - The one taking "bool* p_selected" as a read-write selection information (convenient in some cases) // The earlier is more flexible, as in real application your selection may be stored in many different ways // and not necessarily inside a bool value (e.g. in flags within objects, as an external list, etc). + IMGUI_DEMO_MARKER("Widgets/Selectables/Basic"); if (ImGui::TreeNode("Basic")) { static bool selection[5] = { false, true, false, false, false }; @@ -1135,6 +1185,7 @@ static void ShowDemoWindowWidgets() selection[4] = !selection[4]; ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Single Selection"); if (ImGui::TreeNode("Selection State: Single Selection")) { static int selected = -1; @@ -1147,6 +1198,7 @@ static void ShowDemoWindowWidgets() } ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Multiple Selection"); if (ImGui::TreeNode("Selection State: Multiple Selection")) { HelpMarker("Hold CTRL and click to select multiple items."); @@ -1164,6 +1216,7 @@ static void ShowDemoWindowWidgets() } ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more text into the same line"); if (ImGui::TreeNode("Rendering more text into the same line")) { // Using the Selectable() override that takes "bool* p_selected" parameter, @@ -1174,6 +1227,7 @@ static void ShowDemoWindowWidgets() ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/In columns"); if (ImGui::TreeNode("In columns")) { static bool selected[10] = {}; @@ -1208,6 +1262,7 @@ static void ShowDemoWindowWidgets() } ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Grid"); if (ImGui::TreeNode("Grid")) { static char selected[4][4] = { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; @@ -1240,6 +1295,7 @@ static void ShowDemoWindowWidgets() ImGui::PopStyleVar(); ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Alignment"); if (ImGui::TreeNode("Alignment")) { HelpMarker( @@ -1267,8 +1323,10 @@ static void ShowDemoWindowWidgets() // To wire InputText() with std::string or any other custom string type, // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. + IMGUI_DEMO_MARKER("Widgets/Text Input"); if (ImGui::TreeNode("Text Input")) { + IMGUI_DEMO_MARKER("Widgets/Text Input/Multi-line Text Input"); if (ImGui::TreeNode("Multi-line Text Input")) { // Note: we are using a fixed-sized buffer for simplicity here. See ImGuiInputTextFlags_CallbackResize @@ -1294,6 +1352,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text Input/Filtered Text Input"); if (ImGui::TreeNode("Filtered Text Input")) { struct TextFilters @@ -1316,6 +1375,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text Input/Password input"); if (ImGui::TreeNode("Password Input")) { static char password[64] = "password123"; @@ -1382,6 +1442,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text Input/Resize Callback"); if (ImGui::TreeNode("Resize Callback")) { // To wire InputText() with std::string or any other custom string type, @@ -1428,8 +1489,10 @@ static void ShowDemoWindowWidgets() } // Tabs + IMGUI_DEMO_MARKER("Widgets/Tabs"); if (ImGui::TreeNode("Tabs")) { + IMGUI_DEMO_MARKER("Widgets/Tabs/Basic"); if (ImGui::TreeNode("Basic")) { ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_None; @@ -1456,6 +1519,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Tabs/Advanced & Close Button"); if (ImGui::TreeNode("Advanced & Close Button")) { // Expose a couple of the available flags. In most cases you may just call BeginTabBar() with no flags (0). @@ -1498,6 +1562,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Tabs/TabItemButton & Leading-Trailing flags"); if (ImGui::TreeNode("TabItemButton & Leading/Trailing flags")) { static ImVector active_tabs; @@ -1569,12 +1634,14 @@ static void ShowDemoWindowWidgets() // Plot/Graph widgets are not very good. // Consider using a third-party library such as ImPlot: https://github.com/epezent/implot // (see others https://github.com/ocornut/imgui/wiki/Useful-Extensions) - if (ImGui::TreeNode("Plots Widgets")) + IMGUI_DEMO_MARKER("Widgets/Plotting"); + if (ImGui::TreeNode("Plotting")) { static bool animate = true; ImGui::Checkbox("Animate", &animate); // Plot as lines and plot as histogram + IMGUI_DEMO_MARKER("Widgets/Plotting/PlotLines, PlotHistogram"); static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f)); @@ -1628,6 +1695,7 @@ static void ShowDemoWindowWidgets() ImGui::Separator(); // Animate a simple progress bar + IMGUI_DEMO_MARKER("Widgets/Plotting/ProgressBar"); static float progress = 0.0f, progress_dir = 1.0f; if (animate) { @@ -1649,6 +1717,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Color"); if (ImGui::TreeNode("Color/Picker Widgets")) { static ImVec4 color = ImVec4(114.0f / 255.0f, 144.0f / 255.0f, 154.0f / 255.0f, 200.0f / 255.0f); @@ -1665,18 +1734,22 @@ static void ShowDemoWindowWidgets() ImGui::Checkbox("With HDR", &hdr); ImGui::SameLine(); HelpMarker("Currently all this does is to lift the 0..1 limits on dragging widgets."); ImGuiColorEditFlags misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions); + IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit"); ImGui::Text("Color widget:"); ImGui::SameLine(); HelpMarker( "Click on the color square to open a color picker.\n" "CTRL+click on individual component to input value.\n"); ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags); + IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit (HSV, with Alpha)"); ImGui::Text("Color widget HSV with Alpha:"); ImGui::ColorEdit4("MyColor##2", (float*)&color, ImGuiColorEditFlags_DisplayHSV | misc_flags); + IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit (float display)"); ImGui::Text("Color widget with Float Display:"); ImGui::ColorEdit4("MyColor##2f", (float*)&color, ImGuiColorEditFlags_Float | misc_flags); + IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (with Picker)"); ImGui::Text("Color button with Picker:"); ImGui::SameLine(); HelpMarker( "With the ImGuiColorEditFlags_NoInputs flag you can hide all the slider/text inputs.\n" @@ -1684,6 +1757,7 @@ static void ShowDemoWindowWidgets() "be used for the tooltip and picker popup."); ImGui::ColorEdit4("MyColor##3", (float*)&color, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | misc_flags); + IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (with custom Picker popup)"); ImGui::Text("Color button with Custom Picker Popup:"); // Generate a default palette. The palette will persist and can be edited. @@ -1751,11 +1825,13 @@ static void ShowDemoWindowWidgets() ImGui::EndPopup(); } + IMGUI_DEMO_MARKER("Widgets/Color/ColorButton (simple)"); ImGui::Text("Color button only:"); static bool no_border = false; ImGui::Checkbox("ImGuiColorEditFlags_NoBorder", &no_border); ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80, 80)); + IMGUI_DEMO_MARKER("Widgets/Color/ColorPicker"); ImGui::Text("Color picker:"); static bool alpha = true; static bool alpha_bar = true; @@ -1823,6 +1899,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Drag and Slider Flags"); if (ImGui::TreeNode("Drag/Slider Flags")) { // Demonstrate using advanced flags for DragXXX and SliderXXX functions. Note that the flags are the same! @@ -1856,6 +1933,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Range Widgets"); if (ImGui::TreeNode("Range Widgets")) { static float begin = 10, end = 90; @@ -1866,6 +1944,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Data Types"); if (ImGui::TreeNode("Data Types")) { // DragScalar/InputScalar/SliderScalar functions allow various data types @@ -1915,6 +1994,7 @@ static void ShowDemoWindowWidgets() const float drag_speed = 0.2f; static bool drag_clamp = false; + IMGUI_DEMO_MARKER("Widgets/Data Types/Drags"); ImGui::Text("Drags:"); ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); HelpMarker( @@ -1933,6 +2013,7 @@ static void ShowDemoWindowWidgets() ImGui::DragScalar("drag double", ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, NULL, "%.10f grams"); ImGui::DragScalar("drag double log",ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", ImGuiSliderFlags_Logarithmic); + IMGUI_DEMO_MARKER("Widgets/Data Types/Sliders"); ImGui::Text("Sliders"); ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d"); ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u"); @@ -1965,6 +2046,7 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%" IM_PRId64); ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%" IM_PRIu64 " ms"); + IMGUI_DEMO_MARKER("Widgets/Data Types/Inputs"); static bool inputs_step = true; ImGui::Text("Inputs"); ImGui::Checkbox("Show step buttons", &inputs_step); @@ -1984,6 +2066,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Multi-component Widgets"); if (ImGui::TreeNode("Multi-component Widgets")) { static float vec4f[4] = { 0.10f, 0.20f, 0.30f, 0.44f }; @@ -2015,6 +2098,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Vertical Sliders"); if (ImGui::TreeNode("Vertical Sliders")) { const float spacing = 4; @@ -2079,8 +2163,10 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Drag and drop"); if (ImGui::TreeNode("Drag and Drop")) { + IMGUI_DEMO_MARKER("Widgets/Drag and drop/Standard widgets"); if (ImGui::TreeNode("Drag and drop in standard widgets")) { // ColorEdit widgets automatically act as drag source and drag target. @@ -2095,6 +2181,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Drag and drop/Copy-swap items"); if (ImGui::TreeNode("Drag and drop to copy/swap items")) { enum Mode @@ -2162,6 +2249,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Drag and Drop/Drag to reorder items (simple)"); if (ImGui::TreeNode("Drag to reorder items (simple)")) { // Simple reordering @@ -2191,12 +2279,13 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Querying Item Status (Edited,Active,Hovered etc.)"); if (ImGui::TreeNode("Querying Item Status (Edited/Active/Hovered etc.)")) { // Select an item type const char* item_names[] = { - "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputFloat", + "Text", "Button", "Button (w/ repeat)", "Checkbox", "SliderFloat", "InputText", "InputTextMultiline", "InputFloat", "InputFloat3", "ColorEdit4", "Selectable", "MenuItem", "TreeNode", "TreeNode (w/ double-click)", "Combo", "ListBox" }; static int item_type = 4; @@ -2219,15 +2308,16 @@ static void ShowDemoWindowWidgets() if (item_type == 3) { ret = ImGui::Checkbox("ITEM: Checkbox", &b); } // Testing checkbox if (item_type == 4) { ret = ImGui::SliderFloat("ITEM: SliderFloat", &col4f[0], 0.0f, 1.0f); } // Testing basic item if (item_type == 5) { ret = ImGui::InputText("ITEM: InputText", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which handles tabbing) - if (item_type == 6) { ret = ImGui::InputFloat("ITEM: InputFloat", col4f, 1.0f); } // Testing +/- buttons on scalar input - if (item_type == 7) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) - if (item_type == 8) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) - if (item_type == 9) { ret = ImGui::Selectable("ITEM: Selectable"); } // Testing selectable item - if (item_type == 10){ ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) - if (item_type == 11){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node - if (item_type == 12){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. - if (item_type == 13){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } - if (item_type == 14){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + if (item_type == 6) { ret = ImGui::InputTextMultiline("ITEM: InputTextMultiline", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which uses a child window) + if (item_type == 7) { ret = ImGui::InputFloat("ITEM: InputFloat", col4f, 1.0f); } // Testing +/- buttons on scalar input + if (item_type == 8) { ret = ImGui::InputFloat3("ITEM: InputFloat3", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) + if (item_type == 9) { ret = ImGui::ColorEdit4("ITEM: ColorEdit4", col4f); } // Testing multi-component items (IsItemXXX flags are reported merged) + if (item_type == 10){ ret = ImGui::Selectable("ITEM: Selectable"); } // Testing selectable item + if (item_type == 11){ ret = ImGui::MenuItem("ITEM: MenuItem"); } // Testing menu item (they use ImGuiButtonFlags_PressedOnRelease button policy) + if (item_type == 12){ ret = ImGui::TreeNode("ITEM: TreeNode"); if (ret) ImGui::TreePop(); } // Testing tree node + if (item_type == 13){ ret = ImGui::TreeNodeEx("ITEM: TreeNode w/ ImGuiTreeNodeFlags_OpenOnDoubleClick", ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_NoTreePushOnOpen); } // Testing tree node with ImGuiButtonFlags_PressedOnDoubleClick button policy. + if (item_type == 14){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } + if (item_type == 15){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. @@ -2285,6 +2375,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Querying Window Status (Focused,Hovered etc.)"); if (ImGui::TreeNode("Querying Window Status (Focused/Hovered etc.)")) { static bool embed_all_inside_a_child_window = false; @@ -2369,6 +2460,7 @@ static void ShowDemoWindowWidgets() if (disable_all) ImGui::EndDisabled(); + IMGUI_DEMO_MARKER("Widgets/Disable Block"); if (ImGui::TreeNode("Disable block")) { ImGui::Checkbox("Disable entire section above", &disable_all); @@ -2379,9 +2471,11 @@ static void ShowDemoWindowWidgets() static void ShowDemoWindowLayout() { + IMGUI_DEMO_MARKER("Layout"); if (!ImGui::CollapsingHeader("Layout & Scrolling")) return; + IMGUI_DEMO_MARKER("Layout/Child windows"); if (ImGui::TreeNode("Child windows")) { HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window."); @@ -2467,6 +2561,7 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Widgets Width"); if (ImGui::TreeNode("Widgets Width")) { static float f = 0.0f; @@ -2543,11 +2638,13 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout"); if (ImGui::TreeNode("Basic Horizontal Layout")) { ImGui::TextWrapped("(Use ImGui::SameLine() to keep adding items to the right of the preceding item)"); // Text + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine"); ImGui::Text("Two items: Hello"); ImGui::SameLine(); ImGui::TextColored(ImVec4(1,1,0,1), "Sailor"); @@ -2568,6 +2665,7 @@ static void ShowDemoWindowLayout() ImGui::Text("can fit within a text block."); // Aligned to arbitrary position. Easy/cheap column. + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine (with offset)"); ImGui::Text("Aligned"); ImGui::SameLine(150); ImGui::Text("x=150"); ImGui::SameLine(300); ImGui::Text("x=300"); @@ -2576,6 +2674,7 @@ static void ShowDemoWindowLayout() ImGui::SameLine(300); ImGui::SmallButton("x=300"); // Checkbox + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine (more)"); static bool c1 = false, c2 = false, c3 = false, c4 = false; ImGui::Checkbox("My", &c1); ImGui::SameLine(); ImGui::Checkbox("Tailor", &c2); ImGui::SameLine(); @@ -2607,6 +2706,7 @@ static void ShowDemoWindowLayout() ImGui::PopItemWidth(); // Dummy + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/Dummy"); ImVec2 button_sz(40, 40); ImGui::Button("A", button_sz); ImGui::SameLine(); ImGui::Dummy(button_sz); ImGui::SameLine(); @@ -2614,7 +2714,8 @@ static void ShowDemoWindowLayout() // Manually wrapping // (we should eventually provide this as an automatic layout feature, but for now you can do it manually) - ImGui::Text("Manually wrapping:"); + IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/Manual wrapping"); + ImGui::Text("Manual wrapping:"); ImGuiStyle& style = ImGui::GetStyle(); int buttons_count = 20; float window_visible_x2 = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x; @@ -2632,6 +2733,7 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Groups"); if (ImGui::TreeNode("Groups")) { HelpMarker( @@ -2679,6 +2781,7 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Text Baseline Alignment"); if (ImGui::TreeNode("Text Baseline Alignment")) { { @@ -2797,9 +2900,11 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Scrolling"); if (ImGui::TreeNode("Scrolling")) { // Vertical scroll functions + IMGUI_DEMO_MARKER("Layout/Scrolling/Vertical"); HelpMarker("Use SetScrollHereY() or SetScrollFromPosY() to scroll to a given vertical position."); static int track_item = 50; @@ -2872,6 +2977,7 @@ static void ShowDemoWindowLayout() ImGui::PopID(); // Horizontal scroll functions + IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal"); ImGui::Spacing(); HelpMarker( "Use SetScrollHereX() or SetScrollFromPosX() to scroll to a given horizontal position.\n\n" @@ -2917,6 +3023,7 @@ static void ShowDemoWindowLayout() ImGui::PopID(); // Miscellaneous Horizontal Scrolling Demo + IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal (more)"); HelpMarker( "Horizontal scrolling for a window is enabled via the ImGuiWindowFlags_HorizontalScrollbar flag.\n\n" "You may want to also explicitly specify content width by using SetNextWindowContentWidth() before Begin()."); @@ -2991,6 +3098,7 @@ static void ShowDemoWindowLayout() if (explicit_content_size) ImGui::SetNextWindowContentSize(ImVec2(contents_size_x, 0.0f)); ImGui::Begin("Horizontal contents size demo window", &show_horizontal_contents_size_demo_window, show_h_scrollbar ? ImGuiWindowFlags_HorizontalScrollbar : 0); + IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal contents size demo window"); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 0)); HelpMarker("Test of different widgets react and impact the work rectangle growing when horizontal scrolling is enabled.\n\nUse 'Metrics->Tools->Show windows rectangles' to visualize rectangles."); @@ -3077,6 +3185,7 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Layout/Clipping"); if (ImGui::TreeNode("Clipping")) { static ImVec2 size(100.0f, 100.0f); @@ -3145,6 +3254,7 @@ static void ShowDemoWindowLayout() static void ShowDemoWindowPopups() { + IMGUI_DEMO_MARKER("Popups"); if (!ImGui::CollapsingHeader("Popups & Modal windows")) return; @@ -3166,6 +3276,7 @@ static void ShowDemoWindowPopups() // With popups we have to go through a library call (here OpenPopup) to manipulate the visibility state. // This may be a bit confusing at first but it should quickly make sense. Follow on the examples below. + IMGUI_DEMO_MARKER("Popups/Popups"); if (ImGui::TreeNode("Popups")) { ImGui::TextWrapped( @@ -3234,17 +3345,33 @@ static void ShowDemoWindowPopups() } // Call the more complete ShowExampleMenuFile which we use in various places of this demo - if (ImGui::Button("File Menu..")) + if (ImGui::Button("With a menu..")) ImGui::OpenPopup("my_file_popup"); - if (ImGui::BeginPopup("my_file_popup")) + if (ImGui::BeginPopup("my_file_popup", ImGuiWindowFlags_MenuBar)) { - ShowExampleMenuFile(); + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Edit")) + { + ImGui::MenuItem("Dummy"); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + ImGui::Text("Hello from popup!"); + ImGui::Button("This is a dummy button.."); ImGui::EndPopup(); } ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Popups/Context menus"); if (ImGui::TreeNode("Context menus")) { HelpMarker("\"Context\" functions are simple helpers to associate a Popup to a given Item or Window identifier."); @@ -3329,6 +3456,7 @@ static void ShowDemoWindowPopups() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Popups/Modals"); if (ImGui::TreeNode("Modals")) { ImGui::TextWrapped("Modal windows are like popups but the user cannot close them by clicking outside."); @@ -3404,6 +3532,7 @@ static void ShowDemoWindowPopups() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Popups/Menus inside a regular window"); if (ImGui::TreeNode("Menus inside a regular window")) { ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); @@ -3583,6 +3712,7 @@ static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags) static void ShowDemoWindowTables() { //ImGui::SetNextItemOpen(true, ImGuiCond_Once); + IMGUI_DEMO_MARKER("Tables"); if (!ImGui::CollapsingHeader("Tables & Columns")) return; @@ -3622,6 +3752,7 @@ static void ShowDemoWindowTables() // Demos if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Basic"); if (ImGui::TreeNode("Basic")) { // Here we will showcase three different ways to output a table. @@ -3683,6 +3814,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Borders, background"); if (ImGui::TreeNode("Borders, background")) { // Expose a few Borders related flags interactively @@ -3753,6 +3885,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Resizable, stretch"); if (ImGui::TreeNode("Resizable, stretch")) { // By default, if we don't enable ScrollX the sizing policy for each columns is "Stretch" @@ -3782,6 +3915,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Resizable, fixed"); if (ImGui::TreeNode("Resizable, fixed")) { // Here we use ImGuiTableFlags_SizingFixedFit (even though _ScrollX is not set) @@ -3815,6 +3949,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Resizable, mixed"); if (ImGui::TreeNode("Resizable, mixed")) { HelpMarker( @@ -3864,6 +3999,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Reorderable, hideable, with headers"); if (ImGui::TreeNode("Reorderable, hideable, with headers")) { HelpMarker( @@ -3921,6 +4057,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Padding"); if (ImGui::TreeNode("Padding")) { // First example: showcase use of padding flags and effect of BorderOuterV/BorderInnerV on X padding. @@ -4029,6 +4166,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Explicit widths"); if (ImGui::TreeNode("Sizing policies")) { static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody; @@ -4132,6 +4270,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Vertical scrolling, with clipping"); if (ImGui::TreeNode("Vertical scrolling, with clipping")) { HelpMarker("Here we activate ScrollY, which will create a child window container to allow hosting scrollable contents.\n\nWe also demonstrate using ImGuiListClipper to virtualize the submission of many items."); @@ -4174,6 +4313,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Horizontal scrolling"); if (ImGui::TreeNode("Horizontal scrolling")) { HelpMarker( @@ -4262,6 +4402,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Columns flags"); if (ImGui::TreeNode("Columns flags")) { // Create a first table just to show all the options/flags we want to make visible in our example! @@ -4326,6 +4467,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Columns widths"); if (ImGui::TreeNode("Columns widths")) { HelpMarker("Using TableSetupColumn() to setup default width."); @@ -4391,6 +4533,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Nested tables"); if (ImGui::TreeNode("Nested tables")) { HelpMarker("This demonstrate embedding a table into another table cell."); @@ -4435,6 +4578,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Row height"); if (ImGui::TreeNode("Row height")) { HelpMarker("You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\nWe cannot honor a _maximum_ row height as that would requires a unique clipping rectangle per row."); @@ -4454,6 +4598,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Outer size"); if (ImGui::TreeNode("Outer size")) { // Showcasing use of ImGuiTableFlags_NoHostExtendX and ImGuiTableFlags_NoHostExtendY @@ -4520,6 +4665,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Background color"); if (ImGui::TreeNode("Background color")) { static ImGuiTableFlags flags = ImGuiTableFlags_RowBg; @@ -4577,6 +4723,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Tree view"); if (ImGui::TreeNode("Tree view")) { static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody; @@ -4648,6 +4795,7 @@ static void ShowDemoWindowTables() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Item width"); if (ImGui::TreeNode("Item width")) { HelpMarker( @@ -4693,6 +4841,7 @@ static void ShowDemoWindowTables() // Demonstrate using TableHeader() calls instead of TableHeadersRow() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Custom headers"); if (ImGui::TreeNode("Custom headers")) { const int COLUMNS_COUNT = 3; @@ -4740,6 +4889,7 @@ static void ShowDemoWindowTables() // Demonstrate creating custom context menus inside columns, while playing it nice with context menus provided by TableHeadersRow()/TableHeader() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Context menus"); if (ImGui::TreeNode("Context menus")) { HelpMarker("By default, right-clicking over a TableHeadersRow()/TableHeader() line will open the default context-menu.\nUsing ImGuiTableFlags_ContextMenuInBody we also allow right-clicking over columns body."); @@ -4846,6 +4996,7 @@ static void ShowDemoWindowTables() // Demonstrate creating multiple tables with the same ID if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Synced instances"); if (ImGui::TreeNode("Synced instances")) { HelpMarker("Multiple tables with the same identifier will share their settings, width, visibility, order etc."); @@ -4881,6 +5032,7 @@ static void ShowDemoWindowTables() }; if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Sorting"); if (ImGui::TreeNode("Sorting")) { // Create item list @@ -4968,6 +5120,7 @@ static void ShowDemoWindowTables() //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // [DEBUG] if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Advanced"); if (ImGui::TreeNode("Advanced")) { static ImGuiTableFlags flags = @@ -5284,6 +5437,7 @@ static void ShowDemoWindowTables() // [2020: Columns are under-featured and not maintained. Prefer using the more flexible and powerful BeginTable() API!] static void ShowDemoWindowColumns() { + IMGUI_DEMO_MARKER("Columns (legacy API)"); bool open = ImGui::TreeNode("Legacy Columns API"); ImGui::SameLine(); HelpMarker("Columns() is an old API! Prefer using the more flexible and powerful BeginTable() API!"); @@ -5291,6 +5445,7 @@ static void ShowDemoWindowColumns() return; // Basic columns + IMGUI_DEMO_MARKER("Columns (legacy API)/Basic"); if (ImGui::TreeNode("Basic")) { ImGui::Text("Without border:"); @@ -5335,6 +5490,7 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Columns (legacy API)/Borders"); if (ImGui::TreeNode("Borders")) { // NB: Future columns API should allow automatic horizontal borders. @@ -5370,6 +5526,7 @@ static void ShowDemoWindowColumns() } // Create multiple items in a same cell before switching to next column + IMGUI_DEMO_MARKER("Columns (legacy API)/Mixed items"); if (ImGui::TreeNode("Mixed items")) { ImGui::Columns(3, "mixed"); @@ -5401,6 +5558,7 @@ static void ShowDemoWindowColumns() } // Word wrapping + IMGUI_DEMO_MARKER("Columns (legacy API)/Word-wrapping"); if (ImGui::TreeNode("Word-wrapping")) { ImGui::Columns(2, "word-wrapping"); @@ -5415,6 +5573,7 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Columns (legacy API)/Horizontal Scrolling"); if (ImGui::TreeNode("Horizontal Scrolling")) { ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f)); @@ -5440,6 +5599,7 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Columns (legacy API)/Tree"); if (ImGui::TreeNode("Tree")) { ImGui::Columns(2, "tree", true); @@ -5481,6 +5641,7 @@ static void ShowDemoWindowColumns() static void ShowDemoWindowMisc() { + IMGUI_DEMO_MARKER("Filtering"); if (ImGui::CollapsingHeader("Filtering")) { // Helper class to easy setup a text filter. @@ -5498,6 +5659,7 @@ static void ShowDemoWindowMisc() ImGui::BulletText("%s", lines[i]); } + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus"); if (ImGui::CollapsingHeader("Inputs, Navigation & Focus")) { ImGuiIO& io = ImGui::GetIO(); @@ -5511,6 +5673,7 @@ static void ShowDemoWindowMisc() ImGui::Text("NavActive: %d, NavVisible: %d", io.NavActive, io.NavVisible); // Display Mouse state + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Mouse State"); if (ImGui::TreeNode("Mouse State")) { if (ImGui::IsMousePosValid()) @@ -5518,16 +5681,18 @@ static void ShowDemoWindowMisc() else ImGui::Text("Mouse pos: "); ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); - ImGui::Text("Mouse down:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } - ImGui::Text("Mouse clicked:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse dblclick:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDoubleClicked(i)){ ImGui::SameLine(); ImGui::Text("b%d", i); } - ImGui::Text("Mouse released:"); for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + + int count = IM_ARRAYSIZE(io.MouseDown); + ImGui::Text("Mouse down:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } + ImGui::Text("Mouse clicked:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d (%d)", i, ImGui::GetMouseClickedCount(i)); } + ImGui::Text("Mouse released:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); ImGui::Text("Pen Pressure: %.1f", io.PenPressure); // Note: currently unused ImGui::TreePop(); } // Display Keyboard/Mouse state + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Keyboard & Navigation State"); if (ImGui::TreeNode("Keyboard & Navigation State")) { ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyDown(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X) (%.02f secs)", i, i, io.KeysDownDuration[i]); } @@ -5549,6 +5714,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Tabbing"); if (ImGui::TreeNode("Tabbing")) { ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields."); @@ -5564,6 +5730,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Focus from code"); if (ImGui::TreeNode("Focus from code")) { bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine(); @@ -5605,6 +5772,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Dragging"); if (ImGui::TreeNode("Dragging")) { ImGui::TextWrapped("You can use ImGui::GetMouseDragDelta(0) to query for the dragged amount on any widget."); @@ -5633,6 +5801,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Mouse cursors"); if (ImGui::TreeNode("Mouse cursors")) { const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" }; @@ -5670,6 +5839,7 @@ void ImGui::ShowAboutWindow(bool* p_open) ImGui::End(); return; } + IMGUI_DEMO_MARKER("Tools/About Dear ImGui"); ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); ImGui::Separator(); ImGui::Text("By Omar Cornut and all Dear ImGui contributors."); @@ -5857,6 +6027,7 @@ bool ImGui::ShowStyleSelector(const char* label) void ImGui::ShowStyleEditor(ImGuiStyle* ref) { + IMGUI_DEMO_MARKER("Tools/Style Editor"); // You can pass in a reference ImGuiStyle structure to compare to, revert to and save to // (without a reference style pointer, we will use one compared locally as a reference) ImGuiStyle& style = ImGui::GetStyle(); @@ -6140,6 +6311,7 @@ static void ShowExampleAppMainMenuBar() // (future version will add explicit flags to BeginMenu() to request processing shortcuts) static void ShowExampleMenuFile() { + IMGUI_DEMO_MARKER("Examples/Menu"); ImGui::MenuItem("(demo menu)", NULL, false, false); if (ImGui::MenuItem("New")) {} if (ImGui::MenuItem("Open", "Ctrl+O")) {} @@ -6165,6 +6337,7 @@ static void ShowExampleMenuFile() if (ImGui::MenuItem("Save As..")) {} ImGui::Separator(); + IMGUI_DEMO_MARKER("Examples/Menu/Options"); if (ImGui::BeginMenu("Options")) { static bool enabled = true; @@ -6181,6 +6354,7 @@ static void ShowExampleMenuFile() ImGui::EndMenu(); } + IMGUI_DEMO_MARKER("Examples/Menu/Colors"); if (ImGui::BeginMenu("Colors")) { float sz = ImGui::GetTextLineHeight(); @@ -6201,6 +6375,7 @@ static void ShowExampleMenuFile() // In a real code-base using it would make senses to use this feature from very different code locations. if (ImGui::BeginMenu("Options")) // <-- Append! { + IMGUI_DEMO_MARKER("Examples/Menu/Append to an existing menu"); static bool b = true; ImGui::Checkbox("SomeOption", &b); ImGui::EndMenu(); @@ -6233,6 +6408,7 @@ struct ExampleAppConsole ExampleAppConsole() { + IMGUI_DEMO_MARKER("Examples/Console"); ClearLog(); memset(InputBuf, 0, sizeof(InputBuf)); HistoryPos = -1; @@ -6710,6 +6886,7 @@ static void ShowExampleAppLog(bool* p_open) // Most of the contents of the window will be added by the log.Draw() call. ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); ImGui::Begin("Example: Log", p_open); + IMGUI_DEMO_MARKER("Examples/Log"); if (ImGui::SmallButton("[Debug] Add 5 entries")) { static int counter = 0; @@ -6740,6 +6917,7 @@ static void ShowExampleAppLayout(bool* p_open) ImGui::SetNextWindowSize(ImVec2(500, 440), ImGuiCond_FirstUseEver); if (ImGui::Begin("Example: Simple layout", p_open, ImGuiWindowFlags_MenuBar)) { + IMGUI_DEMO_MARKER("Examples/Simple layout"); if (ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("File")) @@ -6856,6 +7034,7 @@ static void ShowExampleAppPropertyEditor(bool* p_open) ImGui::End(); return; } + IMGUI_DEMO_MARKER("Examples/Property Editor"); HelpMarker( "This example shows how you may implement a property editor using two columns.\n" @@ -6891,6 +7070,7 @@ static void ShowExampleAppLongText(bool* p_open) ImGui::End(); return; } + IMGUI_DEMO_MARKER("Examples/Long text display"); static int test_type = 0; static ImGuiTextBuffer log; @@ -6952,6 +7132,7 @@ static void ShowExampleAppAutoResize(bool* p_open) ImGui::End(); return; } + IMGUI_DEMO_MARKER("Examples/Auto-resizing window"); static int lines = 10; ImGui::TextUnformatted( @@ -7003,6 +7184,7 @@ static void ShowExampleAppConstrainedResize(bool* p_open) ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0; if (ImGui::Begin("Example: Constrained Resize", p_open, flags)) { + IMGUI_DEMO_MARKER("Examples/Constrained Resizing window"); if (ImGui::Button("200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine(); if (ImGui::Button("500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine(); if (ImGui::Button("800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); } @@ -7045,6 +7227,7 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background if (ImGui::Begin("Example: Simple overlay", p_open, window_flags)) { + IMGUI_DEMO_MARKER("Examples/Simple Overlay"); ImGui::Text("Simple overlay\n" "in the corner of the screen.\n" "(right-click to change position)"); ImGui::Separator(); if (ImGui::IsMousePosValid()) @@ -7119,6 +7302,7 @@ static void ShowExampleAppWindowTitles(bool*) // Using "##" to display same title but have unique identifier. ImGui::SetNextWindowPos(ImVec2(base_pos.x + 100, base_pos.y + 100), ImGuiCond_FirstUseEver); ImGui::Begin("Same title as another window##1"); + IMGUI_DEMO_MARKER("Examples/Manipulating window titles"); ImGui::Text("This is window 1.\nMy title is the same as window 2, but my identifier is unique."); ImGui::End(); @@ -7148,6 +7332,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImGui::End(); return; } + IMGUI_DEMO_MARKER("Examples/Custom Rendering"); // Tip: If you do a lot of custom rendering, you probably want to use your own geometrical types and benefit of // overloaded operators, etc. Define IM_VEC2_CLASS_EXTRA in imconfig.h to create implicit conversions between your diff --git a/imgui/imgui_draw.cpp b/imgui/imgui_draw.cpp index 30b100bb..bf1da15b 100644 --- a/imgui/imgui_draw.cpp +++ b/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (drawing and font code) /* @@ -473,6 +473,7 @@ void ImDrawList::_PopUnusedDrawCmd() void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) { + IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; IM_ASSERT(curr_cmd->UserCallback == NULL); if (curr_cmd->ElemCount != 0) @@ -494,6 +495,7 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) // Try to merge two last draw commands void ImDrawList::_TryMergeDrawCmds() { + IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; ImDrawCmd* prev_cmd = curr_cmd - 1; if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) @@ -508,6 +510,7 @@ void ImDrawList::_TryMergeDrawCmds() void ImDrawList::_OnChangedClipRect() { // If current command is used with different settings we need to add a new command + IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; if (curr_cmd->ElemCount != 0 && memcmp(&curr_cmd->ClipRect, &_CmdHeader.ClipRect, sizeof(ImVec4)) != 0) { @@ -530,6 +533,7 @@ void ImDrawList::_OnChangedClipRect() void ImDrawList::_OnChangedTextureID() { // If current command is used with different settings we need to add a new command + IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; if (curr_cmd->ElemCount != 0 && curr_cmd->TextureId != _CmdHeader.TextureId) { @@ -553,6 +557,7 @@ void ImDrawList::_OnChangedVtxOffset() { // We don't need to compare curr_cmd->VtxOffset != _CmdHeader.VtxOffset because we know it'll be different at the time we call this. _VtxCurrentIdx = 0; + IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; //IM_ASSERT(curr_cmd->VtxOffset != _CmdHeader.VtxOffset); // See #3349 if (curr_cmd->ElemCount != 0) @@ -1919,37 +1924,38 @@ ImFontConfig::ImFontConfig() // A work of art lies ahead! (. = white layer, X = black layer, others are blank) // The 2x2 white texels on the top left are the ones we'll use everywhere in Dear ImGui to render filled shapes. -const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 108; // Actual texture will be 2 times that + 1 spacing. +// (This is used when io.MouseDrawCursor = true) +const int FONT_ATLAS_DEFAULT_TEX_DATA_W = 122; // Actual texture will be 2 times that + 1 spacing. const int FONT_ATLAS_DEFAULT_TEX_DATA_H = 27; static const char FONT_ATLAS_DEFAULT_TEX_DATA_PIXELS[FONT_ATLAS_DEFAULT_TEX_DATA_W * FONT_ATLAS_DEFAULT_TEX_DATA_H + 1] = { - "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX " - "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X " - "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X " - "X - X.X - X.....X - X.....X -X...X - X...X- X..X " - "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X " - "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX " - "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX " - "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX " - "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X " - "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X" - "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X" - "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X" - "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X" - "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X" - "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X" - "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X" - "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X " - "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X " - "X.X X..X - -X.......X- X.......X - XX XX - - X..........X " - "XX X..X - - X.....X - X.....X - X.X X.X - - X........X " - " X..X - X...X - X...X - X..X X..X - - X........X " - " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX " - "------------ - X - X -X.....................X- ------------------" - " ----------------------------------- X...XXXXXXXXXXXXX...X - " - " - X..X X..X - " - " - X.X X.X - " - " - XX XX - " + "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX- XX - XX XX " + "..- -X.....X- X.X - X.X -X.....X - X.....X- X..X -X..X X..X" + "--- -XXX.XXX- X...X - X...X -X....X - X....X- X..X -X...X X...X" + "X - X.X - X.....X - X.....X -X...X - X...X- X..X - X...X X...X " + "XX - X.X -X.......X- X.......X -X..X.X - X.X..X- X..X - X...X...X " + "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X- X..XXX - X.....X " + "X..X - X.X - X.X - X.X -XX X.X - X.X XX- X..X..XXX - X...X " + "X...X - X.X - X.X - XX X.X XX - X.X - X.X - X..X..X..XX - X.X " + "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X - X..X..X..X.X - X...X " + "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X -XXX X..X..X..X..X- X.....X " + "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X -X..XX........X..X- X...X...X " + "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X -X...X...........X- X...X X...X " + "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X - X..............X-X...X X...X" + "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X - X.............X-X..X X..X" + "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X - X.............X- XX XX " + "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X - X............X--------------" + "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX - X...........X - " + "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------- X..........X - " + "X.X X..X - -X.......X- X.......X - XX XX - - X..........X - " + "XX X..X - - X.....X - X.....X - X.X X.X - - X........X - " + " X..X - - X...X - X...X - X..X X..X - - X........X - " + " XX - - X.X - X.X - X...XXXXXXXXXXXXX...X - - XXXXXXXXXX - " + "------------- - X - X -X.....................X- ------------------- " + " ----------------------------------- X...XXXXXXXXXXXXX...X - " + " - X..X X..X - " + " - X.X X.X - " + " - XX XX - " }; static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3] = @@ -1963,6 +1969,7 @@ static const ImVec2 FONT_ATLAS_DEFAULT_TEX_CURSOR_DATA[ImGuiMouseCursor_COUNT][3 { ImVec2(73,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNESW { ImVec2(55,0), ImVec2(17,17), ImVec2( 8, 8) }, // ImGuiMouseCursor_ResizeNWSE { ImVec2(91,0), ImVec2(17,22), ImVec2( 5, 0) }, // ImGuiMouseCursor_Hand + { ImVec2(109,0),ImVec2(13,15), ImVec2( 6, 7) }, // ImGuiMouseCursor_NotAllowed }; ImFontAtlas::ImFontAtlas() @@ -3073,8 +3080,8 @@ void ImFontGlyphRangesBuilder::AddText(const char* text, const char* text_end) void ImFontGlyphRangesBuilder::AddRanges(const ImWchar* ranges) { for (; ranges[0]; ranges += 2) - for (ImWchar c = ranges[0]; c <= ranges[1]; c++) - AddChar(c); + for (unsigned int c = ranges[0]; c <= ranges[1] && c <= IM_UNICODE_CODEPOINT_MAX; c++) //-V560 + AddChar((ImWchar)c); } void ImFontGlyphRangesBuilder::BuildRanges(ImVector* out_ranges) @@ -3899,10 +3906,10 @@ void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect const bool fill_R = (inner.Max.x < outer.Max.x); const bool fill_U = (inner.Min.y > outer.Min.y); const bool fill_D = (inner.Max.y < outer.Max.y); - if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomLeft)); - if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, (fill_U ? 0 : ImDrawFlags_RoundCornersTopRight) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomRight)); - if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, (fill_L ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersTopRight)); - if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, (fill_L ? 0 : ImDrawFlags_RoundCornersBottomLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersBottomRight)); + if (fill_L) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Min.y), ImVec2(inner.Min.x, inner.Max.y), col, rounding, ImDrawFlags_RoundCornersNone | (fill_U ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomLeft)); + if (fill_R) draw_list->AddRectFilled(ImVec2(inner.Max.x, inner.Min.y), ImVec2(outer.Max.x, inner.Max.y), col, rounding, ImDrawFlags_RoundCornersNone | (fill_U ? 0 : ImDrawFlags_RoundCornersTopRight) | (fill_D ? 0 : ImDrawFlags_RoundCornersBottomRight)); + if (fill_U) draw_list->AddRectFilled(ImVec2(inner.Min.x, outer.Min.y), ImVec2(inner.Max.x, inner.Min.y), col, rounding, ImDrawFlags_RoundCornersNone | (fill_L ? 0 : ImDrawFlags_RoundCornersTopLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersTopRight)); + if (fill_D) draw_list->AddRectFilled(ImVec2(inner.Min.x, inner.Max.y), ImVec2(inner.Max.x, outer.Max.y), col, rounding, ImDrawFlags_RoundCornersNone | (fill_L ? 0 : ImDrawFlags_RoundCornersBottomLeft) | (fill_R ? 0 : ImDrawFlags_RoundCornersBottomRight)); if (fill_L && fill_U) draw_list->AddRectFilled(ImVec2(outer.Min.x, outer.Min.y), ImVec2(inner.Min.x, inner.Min.y), col, rounding, ImDrawFlags_RoundCornersTopLeft); if (fill_R && fill_U) draw_list->AddRectFilled(ImVec2(inner.Max.x, outer.Min.y), ImVec2(outer.Max.x, inner.Min.y), col, rounding, ImDrawFlags_RoundCornersTopRight); if (fill_L && fill_D) draw_list->AddRectFilled(ImVec2(outer.Min.x, inner.Max.y), ImVec2(inner.Min.x, outer.Max.y), col, rounding, ImDrawFlags_RoundCornersBottomLeft); diff --git a/imgui/imgui_impl_glfw.cpp b/imgui/imgui_impl_glfw.cpp index bf2a6543..1be2c524 100644 --- a/imgui/imgui_impl_glfw.cpp +++ b/imgui/imgui_impl_glfw.cpp @@ -270,6 +270,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. bd->PrevUserCallbackWindowFocus = NULL; + bd->PrevUserCallbackCursorEnter = NULL; bd->PrevUserCallbackMousebutton = NULL; bd->PrevUserCallbackScroll = NULL; bd->PrevUserCallbackKey = NULL; diff --git a/imgui/imgui_impl_opengl3.cpp b/imgui/imgui_impl_opengl3.cpp index 58664ea1..0ed8348d 100644 --- a/imgui/imgui_impl_opengl3.cpp +++ b/imgui/imgui_impl_opengl3.cpp @@ -14,6 +14,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers. // 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions. // 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader. // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). @@ -174,6 +175,8 @@ struct ImGui_ImplOpenGL3_Data GLuint AttribLocationVtxUV; GLuint AttribLocationVtxColor; unsigned int VboHandle, ElementsHandle; + GLsizeiptr VertexBufferSize; + GLsizeiptr IndexBufferSize; bool HasClipOrigin; ImGui_ImplOpenGL3_Data() { memset(this, 0, sizeof(*this)); } @@ -425,8 +428,20 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) const ImDrawList* cmd_list = draw_data->CmdLists[n]; // Upload vertex/index buffers - glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW); + GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); + GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); + if (bd->VertexBufferSize < vtx_buffer_size) + { + bd->VertexBufferSize = vtx_buffer_size; + glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW); + } + if (bd->IndexBufferSize < idx_buffer_size) + { + bd->IndexBufferSize = idx_buffer_size; + glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW); + } + glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data); + glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data); for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { @@ -445,7 +460,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Project scissor/clipping rectangles into framebuffer space ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); - if (clip_max.x < clip_min.x || clip_max.y < clip_min.y) + if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) continue; // Apply scissor/clipping rectangle (Y is inverted in OpenGL) diff --git a/imgui/imgui_impl_opengl3_loader.h b/imgui/imgui_impl_opengl3_loader.h index 9313deda..8452301e 100644 --- a/imgui/imgui_impl_opengl3_loader.h +++ b/imgui/imgui_impl_opengl3_loader.h @@ -249,11 +249,13 @@ typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); typedef void (APIENTRYP PFNGLBUFFERDATAPROC) (GLenum target, GLsizeiptr size, const void *data, GLenum usage); +typedef void (APIENTRYP PFNGLBUFFERSUBDATAPROC) (GLenum target, GLintptr offset, GLsizeiptr size, const void *data); #ifdef GL_GLEXT_PROTOTYPES GLAPI void APIENTRY glBindBuffer (GLenum target, GLuint buffer); GLAPI void APIENTRY glDeleteBuffers (GLsizei n, const GLuint *buffers); GLAPI void APIENTRY glGenBuffers (GLsizei n, GLuint *buffers); GLAPI void APIENTRY glBufferData (GLenum target, GLsizeiptr size, const void *data, GLenum usage); +GLAPI void APIENTRY glBufferSubData (GLenum target, GLintptr offset, GLsizeiptr size, const void *data); #endif #endif /* GL_VERSION_1_5 */ #ifndef GL_VERSION_2_0 @@ -447,6 +449,7 @@ union GL3WProcs { PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate; PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate; PFNGLBUFFERDATAPROC BufferData; + PFNGLBUFFERSUBDATAPROC BufferSubData; PFNGLCLEARPROC Clear; PFNGLCLEARCOLORPROC ClearColor; PFNGLCOMPILESHADERPROC CompileShader; @@ -506,6 +509,7 @@ GL3W_API extern union GL3WProcs imgl3wProcs; #define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate #define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate #define glBufferData imgl3wProcs.gl.BufferData +#define glBufferSubData imgl3wProcs.gl.BufferSubData #define glClear imgl3wProcs.gl.Clear #define glClearColor imgl3wProcs.gl.ClearColor #define glCompileShader imgl3wProcs.gl.CompileShader @@ -692,6 +696,7 @@ static const char *proc_names[] = { "glBlendEquationSeparate", "glBlendFuncSeparate", "glBufferData", + "glBufferSubData", "glClear", "glClearColor", "glCompileShader", diff --git a/imgui/imgui_internal.h b/imgui/imgui_internal.h index fa5dec3e..73d58f6b 100644 --- a/imgui/imgui_internal.h +++ b/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -18,6 +18,7 @@ Index of this file: // [SECTION] Generic helpers // [SECTION] ImDrawList support // [SECTION] Widgets support: flags, enums, data structures +// [SECTION] Clipper support // [SECTION] Navigation support // [SECTION] Columns support // [SECTION] Multi-select support @@ -252,12 +253,19 @@ namespace ImStb #endif // Debug Tools -// Use 'Metrics->Tools->Item Picker' to break into the call-stack of a specific item. +// Use 'Metrics/Debugger->Tools->Item Picker' to break into the call-stack of a specific item. +// This will call IM_DEBUG_BREAK() which you may redefine yourself. See https://github.com/scottt/debugbreak for more reference. #ifndef IM_DEBUG_BREAK -#if defined(__clang__) -#define IM_DEBUG_BREAK() __builtin_debugtrap() -#elif defined (_MSC_VER) +#if defined (_MSC_VER) #define IM_DEBUG_BREAK() __debugbreak() +#elif defined(__clang__) +#define IM_DEBUG_BREAK() __builtin_debugtrap() +#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) +#define IM_DEBUG_BREAK() __asm__ volatile("int $0x03") +#elif defined(__GNUC__) && defined(__thumb__) +#define IM_DEBUG_BREAK() __asm__ volatile(".inst 0xde01") +#elif defined(__GNUC__) && defined(__arm__) && !defined(__thumb__) +#define IM_DEBUG_BREAK() __asm__ volatile(".inst 0xe7f001f0"); #else #define IM_DEBUG_BREAK() IM_ASSERT(0) // It is expected that you define IM_DEBUG_BREAK() into something that will break nicely in a debugger! #endif @@ -294,7 +302,9 @@ static inline ImGuiID ImHash(const void* data, int size, ImU32 seed = 0) { ret #endif // Helpers: Sorting -#define ImQsort qsort +#ifndef ImQsort +static inline void ImQsort(void* base, size_t count, size_t size_of_element, int(IMGUI_CDECL *compare_func)(void const*, void const*)) { if (count > 1) qsort(base, count, size_of_element, compare_func); } +#endif // Helpers: Color Blending IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b); @@ -401,8 +411,8 @@ static inline double ImLog(double x) { return log(x); } static inline int ImAbs(int x) { return x < 0 ? -x : x; } static inline float ImAbs(float x) { return fabsf(x); } static inline double ImAbs(double x) { return fabs(x); } -static inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : ((x > 0.0f) ? 1.0f : 0.0f); } // Sign operator - returns -1, 0 or 1 based on sign of argument -static inline double ImSign(double x) { return (x < 0.0) ? -1.0 : ((x > 0.0) ? 1.0 : 0.0); } +static inline float ImSign(float x) { return (x < 0.0f) ? -1.0f : (x > 0.0f) ? 1.0f : 0.0f; } // Sign operator - returns -1, 0 or 1 based on sign of argument +static inline double ImSign(double x) { return (x < 0.0) ? -1.0 : (x > 0.0) ? 1.0 : 0.0; } #ifdef IMGUI_ENABLE_SSE static inline float ImRsqrt(float x) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); } #else @@ -438,6 +448,7 @@ static inline float ImDot(const ImVec2& a, const ImVec2& b) static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } +static inline bool ImIsFloatAboveGuaranteedIntegerPrecision(float f) { return f <= -16777216 || f >= 16777216; } IM_MSVC_RUNTIME_CHECKS_RESTORE // Helpers: Geometry @@ -1019,8 +1030,6 @@ struct IMGUI_API ImGuiInputTextState bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection bool Edited; // edited this frame ImGuiInputTextFlags Flags; // copy of InputText() flags - ImGuiInputTextCallback UserCallback; // " - void* UserCallbackData; // " ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } @@ -1162,6 +1171,35 @@ struct ImGuiPtrOrIndex ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } }; +//----------------------------------------------------------------------------- +// [SECTION] Clipper support +//----------------------------------------------------------------------------- + +struct ImGuiListClipperRange +{ + int Min; + int Max; + bool PosToIndexConvert; // Begin/End are absolute position (will be converted to indices later) + ImS8 PosToIndexOffsetMin; // Add to Min after converting to indices + ImS8 PosToIndexOffsetMax; // Add to Min after converting to indices + + static ImGuiListClipperRange FromIndices(int min, int max) { ImGuiListClipperRange r = { min, max, false, 0, 0 }; return r; } + static ImGuiListClipperRange FromPositions(float y1, float y2, int off_min, int off_max) { ImGuiListClipperRange r = { (int)y1, (int)y2, true, (ImS8)off_min, (ImS8)off_max }; return r; } +}; + +// Temporary clipper data, buffers shared/reused between instances +struct ImGuiListClipperData +{ + ImGuiListClipper* ListClipper; + float LossynessOffset; + int StepNo; + int ItemsFrozen; + ImVector Ranges; + + ImGuiListClipperData() { memset(this, 0, sizeof(*this)); } + void Reset(ImGuiListClipper* clipper) { ListClipper = clipper; StepNo = ItemsFrozen = 0; Ranges.resize(0); } +}; + //----------------------------------------------------------------------------- // [SECTION] Navigation support //----------------------------------------------------------------------------- @@ -1201,9 +1239,10 @@ enum ImGuiNavHighlightFlags_ enum ImGuiNavDirSourceFlags_ { ImGuiNavDirSourceFlags_None = 0, - ImGuiNavDirSourceFlags_Keyboard = 1 << 0, - ImGuiNavDirSourceFlags_PadDPad = 1 << 1, - ImGuiNavDirSourceFlags_PadLStick = 1 << 2 + ImGuiNavDirSourceFlags_RawKeyboard = 1 << 0, // Raw keyboard (not pulled from nav), faciliate use of some functions before we can unify nav and keys + ImGuiNavDirSourceFlags_Keyboard = 1 << 1, + ImGuiNavDirSourceFlags_PadDPad = 1 << 2, + ImGuiNavDirSourceFlags_PadLStick = 1 << 3 }; enum ImGuiNavMoveFlags_ @@ -1218,9 +1257,10 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_ScrollToEdgeY = 1 << 6, // Force scrolling to min/max (used by Home/End) // FIXME-NAV: Aim to remove or reword, probably unnecessary ImGuiNavMoveFlags_Forwarded = 1 << 7, ImGuiNavMoveFlags_DebugNoResult = 1 << 8, // Dummy scoring for debug purpose, don't apply result - ImGuiNavMoveFlags_Tabbing = 1 << 9, // == Focus + Activate if item is Inputable + DontChangeNavHighlight - ImGuiNavMoveFlags_Activate = 1 << 10, - ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 11 // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_FocusApi = 1 << 9, + ImGuiNavMoveFlags_Tabbing = 1 << 10, // == Focus + Activate if item is Inputable + DontChangeNavHighlight + ImGuiNavMoveFlags_Activate = 1 << 11, + ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 12 // Do not alter the visible state of keyboard vs mouse nav highlight }; enum ImGuiNavLayer @@ -1540,6 +1580,7 @@ struct ImGuiContext ImVectorGroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() ImVectorOpenPopupStack; // Which popups are open (persistent) ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + int BeginMenuCount; // Viewports ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. @@ -1553,7 +1594,6 @@ struct ImGuiContext ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 ImGuiID NavActivateInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0; ImGuiActivateFlags_PreferInput will be set and NavActivateId will be 0. ImGuiActivateFlags NavActivateFlags; - ImGuiID NavJustTabbedId; // Just tabbed to this id. ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). ImGuiKeyModFlags NavJustMovedToKeyMods; @@ -1561,7 +1601,6 @@ struct ImGuiContext ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. - int NavIdTabCounter; // == NavWindow->DC.FocusIdxTabCounter at time of NavId processing bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) @@ -1583,11 +1622,14 @@ struct ImGuiContext ImGuiDir NavMoveDirForDebug; ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? ImRect NavScoringRect; // Rectangle used for scoring, in screen space. Based of window->NavRectRel[], modified for directional navigation scoring. + ImRect NavScoringNoClipRect; // Some nav operations (such as PageUp/PageDown) enforce a region which clipper will attempt to always keep submitted int NavScoringDebugCount; // Metrics for debugging - int NavTabbingInputableRemaining; // >0 when counting items for tabbing + int NavTabbingDir; // Generally -1 or +1, 0 when tabbing without a nav id + int NavTabbingCounter; // >0 when counting items for tabbing ImGuiNavItemData NavMoveResultLocal; // Best move request candidate within NavWindow ImGuiNavItemData NavMoveResultLocalVisible; // Best move request candidate within NavWindow that are mostly visible (when using ImGuiNavMoveFlags_AlsoScoreVisibleSet flag) ImGuiNavItemData NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) + ImGuiNavItemData NavTabbingResultFirst; // First tabbing request candidate within NavWindow and flattened hierarchy // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) ImGuiWindow* NavWindowingTarget; // Target window when doing CTRL+Tab (or Pad Menu + FocusPrev/Next), this window is temporarily displayed top-most! @@ -1597,13 +1639,6 @@ struct ImGuiContext float NavWindowingHighlightAlpha; bool NavWindowingToggleLayer; - // Legacy Focus/Tabbing system (older than Nav, active even if Nav is disabled, misnamed. FIXME-NAV: This needs a redesign!) - ImGuiWindow* TabFocusRequestCurrWindow; // - ImGuiWindow* TabFocusRequestNextWindow; // - int TabFocusRequestCurrCounterTabStop; // Tab item being requested for focus, stored as an index - int TabFocusRequestNextCounterTabStop; // " - bool TabFocusPressed; // Set in NewFrame() when user pressed Tab - // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) ImGuiMouseCursor MouseCursor; @@ -1627,11 +1662,15 @@ struct ImGuiContext ImVector DragDropPayloadBufHeap; // We don't expose the ImVector<> directly, ImGuiPayload only holds pointer+size unsigned char DragDropPayloadBufLocal[16]; // Local buffer for small payloads + // Clipper + int ClipperTempDataStacked; + ImVector ClipperTempData; + // Table ImGuiTable* CurrentTable; - int CurrentTableStackIdx; - ImPool Tables; - ImVector TablesTempDataStack; + int TablesTempDataStacked; // Temporary table data size (because we leave previous instances undestructed, we generally don't use TablesTempData.Size) + ImVector TablesTempData; // Temporary table data (buffers reused/shared across instances, support nesting) + ImPool Tables; // Persistent table data ImVector TablesLastTimeActive; // Last used timestamp of each tables (SOA, for efficient GC) ImVector DrawChannelsTempMergeBuffer; @@ -1763,15 +1802,15 @@ struct ImGuiContext LastActiveIdTimer = 0.0f; CurrentItemFlags = ImGuiItemFlags_None; + BeginMenuCount = 0; NavWindow = NULL; NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavActivateInputId = 0; - NavJustTabbedId = NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; + NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; NavJustMovedToKeyMods = ImGuiKeyModFlags_None; NavInputSource = ImGuiInputSource_None; NavLayer = ImGuiNavLayer_Main; - NavIdTabCounter = INT_MAX; NavIdIsAlive = false; NavMousePosDirty = false; NavDisableHighlight = true; @@ -1788,17 +1827,13 @@ struct ImGuiContext NavMoveKeyMods = ImGuiKeyModFlags_None; NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None; NavScoringDebugCount = 0; - NavTabbingInputableRemaining = 0; + NavTabbingDir = 0; + NavTabbingCounter = 0; NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; - TabFocusRequestCurrWindow = TabFocusRequestNextWindow = NULL; - TabFocusRequestCurrCounterTabStop = INT_MAX; - TabFocusRequestNextCounterTabStop = INT_MAX; - TabFocusPressed = false; - DimBgRatio = 0.0f; MouseCursor = ImGuiMouseCursor_Arrow; @@ -1814,8 +1849,10 @@ struct ImGuiContext DragDropHoldJustPressedId = 0; memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); + ClipperTempDataStacked = 0; + CurrentTable = NULL; - CurrentTableStackIdx = -1; + TablesTempDataStacked = 0; CurrentTabBar = NULL; TempInputId = 0; @@ -1882,6 +1919,7 @@ struct IMGUI_API ImGuiWindowTempData ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. ImVec1 GroupOffset; + ImVec2 CursorStartPosLossyness;// Record the loss of precision of CursorStartPos due to really large scrolling amount. This is used by clipper to compensentate and fix the most common use case of large scroll area. // Keyboard/Gamepad navigation ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) @@ -1903,7 +1941,6 @@ struct IMGUI_API ImGuiWindowTempData int CurrentTableIdx; // Current table index (into g.Tables) ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() - int FocusCounterTabStop; // (Legacy Focus/Tabbing system) Same, but only count widgets which you can Tab through. // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. @@ -1947,6 +1984,7 @@ struct IMGUI_API ImGuiWindow bool Appearing; // Set during the frame where the window is appearing (or re-appearing) bool Hidden; // Do not display (== HiddenFrames*** > 0) bool IsFallbackWindow; // Set on the "Debug##Default" window. + bool IsExplicitChild; // Set when passed _ChildWindow, left to false by BeginDocked() bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) @@ -1994,6 +2032,7 @@ struct IMGUI_API ImGuiWindow ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer) ImDrawList DrawListInst; ImGuiWindow* ParentWindow; // If we are a child _or_ popup _or_ docked window, this is pointing to our parent. Otherwise NULL. + ImGuiWindow* ParentWindowInBeginStack; ImGuiWindow* RootWindow; // Point to ourself or first ancestor that is not a child window. Doesn't cross through popups/dock nodes. ImGuiWindow* RootWindowPopupTree; // Point to ourself or first ancestor that is not a child window. Cross through popups parent<>child. ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. @@ -2067,7 +2106,7 @@ struct ImGuiTabItem }; // Storage for a tab bar (sizeof() 152 bytes) -struct ImGuiTabBar +struct IMGUI_API ImGuiTabBar { ImVector Tabs; ImGuiTabBarFlags Flags; @@ -2192,7 +2231,7 @@ struct ImGuiTableCellData }; // FIXME-TABLE: more transient data could be stored in a per-stacked table structure: DrawSplitter, SortSpecs, incoming RowData -struct ImGuiTable +struct IMGUI_API ImGuiTable { ImGuiID ID; ImGuiTableFlags Flags; @@ -2298,14 +2337,14 @@ struct ImGuiTable bool MemoryCompacted; bool HostSkipItems; // Backup of InnerWindow->SkipItem at the end of BeginTable(), because we will overwrite InnerWindow->SkipItem on a per-column basis - IMGUI_API ImGuiTable() { memset(this, 0, sizeof(*this)); LastFrameActive = -1; } - IMGUI_API ~ImGuiTable() { IM_FREE(RawData); } + ImGuiTable() { memset(this, 0, sizeof(*this)); LastFrameActive = -1; } + ~ImGuiTable() { IM_FREE(RawData); } }; // Transient data that are only needed between BeginTable() and EndTable(), those buffers are shared (1 per level of stacked table). // - Accessing those requires chasing an extra pointer so for very frequently used data we leave them in the main table structure. // - We also leave out of this structure data that tend to be particularly useful for debugging/metrics. -struct ImGuiTableTempData +struct IMGUI_API ImGuiTableTempData { int TableIndex; // Index in g.Tables.Buf[] pool float LastTimeActive; // Last timestamp this structure was used @@ -2322,7 +2361,7 @@ struct ImGuiTableTempData float HostBackupItemWidth; // Backup of OuterWindow->DC.ItemWidth at the end of BeginTable() int HostBackupItemWidthStackSize;//Backup of OuterWindow->DC.ItemWidthStack.Size at the end of BeginTable() - IMGUI_API ImGuiTableTempData() { memset(this, 0, sizeof(*this)); LastTimeActive = -1.0f; } + ImGuiTableTempData() { memset(this, 0, sizeof(*this)); LastTimeActive = -1.0f; } }; // sizeof() ~ 12 @@ -2382,12 +2421,15 @@ namespace ImGui IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); IMGUI_API ImVec2 CalcWindowNextAutoFitSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, bool popup_hierarchy); + IMGUI_API bool IsWindowWithinBeginStackOf(ImGuiWindow* window, ImGuiWindow* potential_parent); IMGUI_API bool IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_below); IMGUI_API bool IsWindowNavFocusable(ImGuiWindow* window); IMGUI_API void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond = 0); IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); IMGUI_API void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); + inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } + inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } // Windows: Display Order and Focus Order IMGUI_API void FocusWindow(ImGuiWindow* window); @@ -2395,6 +2437,9 @@ namespace ImGui IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); + IMGUI_API void BringWindowToDisplayBehind(ImGuiWindow* window, ImGuiWindow* above_window); + IMGUI_API int FindWindowDisplayIndex(ImGuiWindow* window); + IMGUI_API ImGuiWindow* FindBottomMostVisibleWindowWithinBeginStack(ImGuiWindow* window); // Fonts, drawing IMGUI_API void SetCurrentFont(ImFont* font); @@ -2463,8 +2508,8 @@ namespace ImGui IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f); IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemFlags extra_flags = 0); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); - IMGUI_API void ItemInputable(ImGuiWindow* window, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id); + IMGUI_API void SetLastItemData(ImGuiID item_id, ImGuiItemFlags in_flags, ImGuiItemStatusFlags status_flags, const ImRect& item_rect); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); IMGUI_API void PushMultiItemsWidths(int components, float width_full); @@ -2501,9 +2546,10 @@ namespace ImGui IMGUI_API void ClosePopupsExceptModals(); IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); - IMGUI_API void BeginTooltipEx(ImGuiWindowFlags extra_flags, ImGuiTooltipFlags tooltip_flags); + IMGUI_API void BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); + IMGUI_API ImGuiWindow* GetTopMostAndVisiblePopupModal(); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); @@ -2523,7 +2569,7 @@ namespace ImGui IMGUI_API bool NavMoveRequestButNoResultYet(); IMGUI_API void NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); - IMGUI_API void NavMoveRequestResolveWithLastItem(); + IMGUI_API void NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result); IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); @@ -2671,7 +2717,7 @@ namespace ImGui IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); - IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float avail_v, float contents_v, ImDrawFlags flags); + IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 avail_v, ImS64 contents_v, ImDrawFlags flags); IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col); IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); @@ -2751,6 +2797,7 @@ namespace ImGui IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); + IMGUI_API void DebugNodeWindowsListByBeginStackParent(ImGuiWindow** windows, int windows_size, ImGuiWindow* parent_in_begin_stack); IMGUI_API void DebugNodeViewport(ImGuiViewportP* viewport); IMGUI_API void DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb); diff --git a/imgui/imgui_tables.cpp b/imgui/imgui_tables.cpp index 56056ae3..8a3fe93c 100644 --- a/imgui/imgui_tables.cpp +++ b/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (tables and columns code) /* @@ -340,10 +340,9 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // Acquire temporary buffers const int table_idx = g.Tables.GetIndex(table); - g.CurrentTableStackIdx++; - if (g.CurrentTableStackIdx + 1 > g.TablesTempDataStack.Size) - g.TablesTempDataStack.resize(g.CurrentTableStackIdx + 1, ImGuiTableTempData()); - ImGuiTableTempData* temp_data = table->TempData = &g.TablesTempDataStack[g.CurrentTableStackIdx]; + if (++g.TablesTempDataStacked > g.TablesTempData.Size) + g.TablesTempData.resize(g.TablesTempDataStacked, ImGuiTableTempData()); + ImGuiTableTempData* temp_data = table->TempData = &g.TablesTempData[g.TablesTempDataStacked - 1]; temp_data->TableIndex = table_idx; table->DrawSplitter = &table->TempData->DrawSplitter; table->DrawSplitter->Clear(); @@ -1382,9 +1381,8 @@ void ImGui::EndTable() // Clear or restore current table, if any IM_ASSERT(g.CurrentWindow == outer_window && g.CurrentTable == table); - IM_ASSERT(g.CurrentTableStackIdx >= 0); - g.CurrentTableStackIdx--; - temp_data = g.CurrentTableStackIdx >= 0 ? &g.TablesTempDataStack[g.CurrentTableStackIdx] : NULL; + IM_ASSERT(g.TablesTempDataStacked > 0); + temp_data = (--g.TablesTempDataStacked > 0) ? &g.TablesTempData[g.TablesTempDataStacked - 1] : NULL; g.CurrentTable = temp_data ? g.Tables.GetByIndex(temp_data->TableIndex) : NULL; if (g.CurrentTable) { diff --git a/imgui/imgui_widgets.cpp b/imgui/imgui_widgets.cpp index 7da069b6..f199ad40 100644 --- a/imgui/imgui_widgets.cpp +++ b/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.85 +// dear imgui, v1.86 // (widgets code) /* @@ -564,7 +564,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool SetFocusID(id, window); FocusWindow(window); } - if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDoubleClicked[mouse_button_clicked])) + if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseClickedCount[mouse_button_clicked] == 2)) { pressed = true; if (flags & ImGuiButtonFlags_NoHoldingActiveId) @@ -641,7 +641,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if ((release_in || release_anywhere) && !g.DragDropActive) { // Report as pressed when releasing the mouse (this is the most common path) - bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseDownWasDoubleClick[mouse_button]; + bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseReleased[mouse_button] && g.IO.MouseClickedLastCount[mouse_button] == 2; bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps if (!is_double_click_release && !is_repeating_already) pressed = true; @@ -896,7 +896,9 @@ void ImGui::Scrollbar(ImGuiAxis axis) } float size_avail = window->InnerRect.Max[axis] - window->InnerRect.Min[axis]; float size_contents = window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f; - ScrollbarEx(bb, id, axis, &window->Scroll[axis], size_avail, size_contents, rounding_corners); + ImS64 scroll = (ImS64)window->Scroll[axis]; + ScrollbarEx(bb, id, axis, &scroll, (ImS64)size_avail, (ImS64)size_contents, rounding_corners); + window->Scroll[axis] = (float)scroll; } // Vertical/Horizontal scrollbar @@ -905,7 +907,7 @@ void ImGui::Scrollbar(ImGuiAxis axis) // - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar // - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. // Still, the code should probably be made simpler.. -bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, float* p_scroll_v, float size_avail_v, float size_contents_v, ImDrawFlags flags) +bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 size_avail_v, ImS64 size_contents_v, ImDrawFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -936,8 +938,8 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) // But we maintain a minimum size in pixel to allow for the user to still aim inside. IM_ASSERT(ImMax(size_contents_v, size_avail_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers. - const float win_size_v = ImMax(ImMax(size_contents_v, size_avail_v), 1.0f); - const float grab_h_pixels = ImClamp(scrollbar_size_v * (size_avail_v / win_size_v), style.GrabMinSize, scrollbar_size_v); + const ImS64 win_size_v = ImMax(ImMax(size_contents_v, size_avail_v), (ImS64)1); + const float grab_h_pixels = ImClamp(scrollbar_size_v * ((float)size_avail_v / (float)win_size_v), style.GrabMinSize, scrollbar_size_v); const float grab_h_norm = grab_h_pixels / scrollbar_size_v; // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). @@ -945,13 +947,13 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa bool hovered = false; ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); - float scroll_max = ImMax(1.0f, size_contents_v - size_avail_v); - float scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); + const ImS64 scroll_max = ImMax((ImS64)1, size_contents_v - size_avail_v); + float scroll_ratio = ImSaturate((float)*p_scroll_v / (float)scroll_max); float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; // Grab position in normalized space if (held && allow_interaction && grab_h_norm < 1.0f) { - float scrollbar_pos_v = bb.Min[axis]; - float mouse_pos_v = g.IO.MousePos[axis]; + const float scrollbar_pos_v = bb.Min[axis]; + const float mouse_pos_v = g.IO.MousePos[axis]; // Click position in scrollbar normalized space (0.0f->1.0f) const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); @@ -971,10 +973,10 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, floa // Apply scroll (p_scroll_v will generally point on one member of window->Scroll) // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); - *p_scroll_v = IM_ROUND(scroll_v_norm * scroll_max);//(win_size_contents_v - win_size_v)); + *p_scroll_v = (ImS64)(scroll_v_norm * scroll_max); // Update values for rendering - scroll_ratio = ImSaturate(*p_scroll_v / scroll_max); + scroll_ratio = ImSaturate((float)*p_scroll_v / (float)scroll_max); grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; // Update distance to grab now that we have seeked and saturated @@ -1391,11 +1393,20 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) if (g.GroupStack.Size > 0 && g.GroupStack.back().WindowID == window->ID) x1 += window->DC.Indent.x; + // FIXME-WORKRECT: In theory we should simply be using WorkRect.Min.x/Max.x everywhere but it isn't aesthetically what we want, + // need to introduce a variant of WorkRect for that purpose. (#4787) + if (ImGuiTable* table = g.CurrentTable) + { + x1 = table->Columns[table->CurrentColumn].MinX; + x2 = table->Columns[table->CurrentColumn].MaxX; + } + ImGuiOldColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; if (columns) PushColumnsBackground(); // We don't provide our width to the layout so that it doesn't get feed back into AutoFit + // FIXME: This prevents ->CursorMaxPos based bounding box evaluation from working (e.g. TableEndCell) const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness_draw)); ItemSize(ImVec2(0.0f, thickness_layout)); const bool item_visible = ItemAdd(bb, 0); @@ -1424,7 +1435,7 @@ void ImGui::Separator() // Those flags should eventually be overridable by the user ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; - flags |= ImGuiSeparatorFlags_SpanAllColumns; + flags |= ImGuiSeparatorFlags_SpanAllColumns; // NB: this only applies to legacy Columns() api as they relied on Separator() a lot. SeparatorEx(flags); } @@ -2409,7 +2420,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, { const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = (hovered && g.IO.MouseClicked[0]); - const bool double_clicked = (hovered && g.IO.MouseDoubleClicked[0]); + const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2); if (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id || g.NavActivateInputId == id) { SetActiveID(id, window); @@ -3679,17 +3690,18 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* ob } // When ImGuiInputTextFlags_Password is set, we don't want actions such as CTRL+Arrow to leak the fact that underlying data are blanks or separators. -static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|'; } +static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r'; } static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (is_separator(obj->TextW[idx - 1]) && !is_separator(obj->TextW[idx]) ) : 1; } +static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx])) : 1; } static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } +static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } +#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h #ifdef __APPLE__ // FIXME: Move setting to IO structure -static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx]) ) : 1; } -static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } +#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_MAC #else -static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } +static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } +#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_WIN #endif -#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h -#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n) { @@ -3881,11 +3893,12 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Generic named filters if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))) { - // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf. + // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf to use e.g. ',' instead of '.'. // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. // We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point. // Change the default decimal_point with: // ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; + // Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions. ImGuiContext& g = *GImGui; const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint; @@ -3964,7 +3977,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_resizable) IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag! - if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope, + if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope (including the scrollbar) BeginGroup(); const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); @@ -3977,6 +3990,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiWindow* draw_window = window; ImVec2 inner_size = frame_size; ImGuiItemStatusFlags item_status_flags = 0; + ImGuiLastItemData item_data_backup; if (is_multiline) { ImVec2 backup_pos = window->DC.CursorPos; @@ -3987,6 +4001,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ return false; } item_status_flags = g.LastItemData.StatusFlags; + item_data_backup = g.LastItemData; window->DC.CursorPos = backup_pos; // We reproduce the contents of BeginChildFrame() in order to provide 'label' so our window internal data are easier to read/debug. @@ -3994,8 +4009,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); + PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove); - PopStyleVar(2); + PopStyleVar(3); PopStyleColor(); if (!child_visible) { @@ -4163,8 +4179,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->Edited = false; state->BufCapacityA = buf_size; state->Flags = flags; - state->UserCallback = callback; - state->UserCallbackData = callback_user_data; // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget. // Down the line we should have a cleaner library-wide concept of Selected vs Active. @@ -4176,19 +4190,49 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y) : (g.FontSize * 0.5f)); const bool is_osx = io.ConfigMacOSXBehaviors; - if (select_all || (hovered && !is_osx && io.MouseDoubleClicked[0])) + if (select_all) { state->SelectAll(); state->SelectedAllMouseLock = true; } - else if (hovered && is_osx && io.MouseDoubleClicked[0]) + else if (hovered && io.MouseClickedCount[0] >= 2 && !io.KeyShift) { - // Double-click select a word only, OS X style (by simulating keystrokes) - state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT); - state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); + stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); + const int multiclick_count = (io.MouseClickedCount[0] - 2); + if ((multiclick_count % 2) == 0) + { + // Double-click: Select word + // We always use the "Mac" word advance for double-click select vs CTRL+Right which use the platform dependent variant: + // FIXME: There are likely many ways to improve this behavior, but there's no "right" behavior (depends on use-case, software, OS) + const bool is_bol = (state->Stb.cursor == 0) || ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb.cursor - 1) == '\n'; + if (STB_TEXT_HAS_SELECTION(&state->Stb) || !is_bol) + state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT); + //state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); + if (!STB_TEXT_HAS_SELECTION(&state->Stb)) + ImStb::stb_textedit_prep_selection_at_cursor(&state->Stb); + state->Stb.cursor = ImStb::STB_TEXTEDIT_MOVEWORDRIGHT_MAC(state, state->Stb.cursor); + state->Stb.select_end = state->Stb.cursor; + ImStb::stb_textedit_clamp(state, &state->Stb); + } + else + { + // Triple-click: Select line + const bool is_eol = ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb.cursor) == '\n'; + state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART); + state->OnKeyPressed(STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT); + state->OnKeyPressed(STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT); + if (!is_eol && is_multiline) + { + ImSwap(state->Stb.select_start, state->Stb.select_end); + state->Stb.cursor = state->Stb.select_end; + } + state->CursorFollow = false; + } + state->CursorAnimReset(); } else if (io.MouseClicked[0] && !state->SelectedAllMouseLock) { + // FIXME: unselect on late click could be done release? if (hovered) { stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); @@ -4273,7 +4317,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (IsKeyPressedMap(ImGuiKey_PageDown) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGDOWN | k_mask); scroll_y += row_count_per_page * g.FontSize; } else if (IsKeyPressedMap(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } else if (IsKeyPressedMap(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } + else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly && !is_cut) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } else if (IsKeyPressedMap(ImGuiKey_Backspace) && !is_readonly) { if (!state->HasSelection()) @@ -4372,11 +4416,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // Process callbacks and apply result back to user's buffer. + const char* apply_new_text = NULL; + int apply_new_text_length = 0; if (g.ActiveId == id) { IM_ASSERT(state != NULL); - const char* apply_new_text = NULL; - int apply_new_text_length = 0; if (cancel_edit) { // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. @@ -4452,8 +4496,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ callback_data.Flags = flags; callback_data.UserData = callback_user_data; + char* callback_buf = is_readonly ? buf : state->TextA.Data; callback_data.EventKey = event_key; - callback_data.Buf = state->TextA.Data; + callback_data.Buf = callback_buf; callback_data.BufTextLen = state->CurLenA; callback_data.BufSize = state->BufCapacityA; callback_data.BufDirty = false; @@ -4468,7 +4513,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ callback(&callback_data); // Read back what user may have modified - IM_ASSERT(callback_data.Buf == state->TextA.Data); // Invalid to modify those fields + callback_buf = is_readonly ? buf : state->TextA.Data; // Pointer may have been invalidated by a resize callback + IM_ASSERT(callback_data.Buf == callback_buf); // Invalid to modify those fields IM_ASSERT(callback_data.BufSize == state->BufCapacityA); IM_ASSERT(callback_data.Flags == flags); const bool buf_dirty = callback_data.BufDirty; @@ -4495,39 +4541,37 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } } - // Copy result to user buffer - if (apply_new_text) - { - // We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size - // of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used - // without any storage on user's side. - IM_ASSERT(apply_new_text_length >= 0); - if (is_resizable) - { - ImGuiInputTextCallbackData callback_data; - callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; - callback_data.Flags = flags; - callback_data.Buf = buf; - callback_data.BufTextLen = apply_new_text_length; - callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1); - callback_data.UserData = callback_user_data; - callback(&callback_data); - buf = callback_data.Buf; - buf_size = callback_data.BufSize; - apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); - IM_ASSERT(apply_new_text_length <= buf_size); - } - //IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); + // Clear temporary user storage + state->Flags = ImGuiInputTextFlags_None; + } - // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. - ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); - value_changed = true; + // Copy result to user buffer. This can currently only happen when (g.ActiveId == id) + if (apply_new_text != NULL) + { + // We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size + // of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used + // without any storage on user's side. + IM_ASSERT(apply_new_text_length >= 0); + if (is_resizable) + { + ImGuiInputTextCallbackData callback_data; + callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; + callback_data.Flags = flags; + callback_data.Buf = buf; + callback_data.BufTextLen = apply_new_text_length; + callback_data.BufSize = ImMax(buf_size, apply_new_text_length + 1); + callback_data.UserData = callback_user_data; + callback(&callback_data); + buf = callback_data.Buf; + buf_size = callback_data.BufSize; + apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); + IM_ASSERT(apply_new_text_length <= buf_size); } + //IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); - // Clear temporary user storage - state->Flags = ImGuiInputTextFlags_None; - state->UserCallback = NULL; - state->UserCallbackData = NULL; + // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. + ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); + value_changed = true; } // Release active ID at the end of the function (so e.g. pressing Return still does a final application of the value) @@ -4649,7 +4693,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Test if cursor is vertically visible if (cursor_offset.y - g.FontSize < scroll_y) scroll_y = ImMax(0.0f, cursor_offset.y - g.FontSize); - else if (cursor_offset.y - inner_size.y >= scroll_y) + else if (cursor_offset.y - (inner_size.y - style.FramePadding.y * 2.0f) >= scroll_y) scroll_y = cursor_offset.y - inner_size.y + style.FramePadding.y * 2.0f; const float scroll_max_y = ImMax((text_size.y + style.FramePadding.y * 2.0f) - inner_size.y, 0.0f); scroll_y = ImClamp(scroll_y, 0.0f, scroll_max_y); @@ -4741,9 +4785,23 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_multiline) { + // For focus requests to work on our multiline we need to ensure our child ItemAdd() call specifies the ImGuiItemFlags_Inputable (ref issue #4761)... Dummy(ImVec2(text_size.x, text_size.y + style.FramePadding.y)); + ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; + g.CurrentItemFlags |= ImGuiItemFlags_Inputable | ImGuiItemFlags_NoTabStop; EndChild(); + item_data_backup.StatusFlags |= (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredWindow); + g.CurrentItemFlags = backup_item_flags; + + // ...and then we need to undo the group overriding last item data, which gets a bit messy as EndGroup() tries to forward scrollbar being active... + // FIXME: This quite messy/tricky, should attempt to get rid of the child window. EndGroup(); + if (g.LastItemData.ID == 0) + { + g.LastItemData.ID = id; + g.LastItemData.InFlags = item_data_backup.InFlags; + g.LastItemData.StatusFlags = item_data_backup.StatusFlags; + } } // Log as text @@ -5548,7 +5606,7 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags { ImGuiContext& g = *GImGui; - BeginTooltipEx(0, ImGuiTooltipFlags_OverridePreviousTooltip); + BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None); const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; if (text_end > text) { @@ -5919,7 +5977,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l toggled = true; if (flags & ImGuiTreeNodeFlags_OpenOnArrow) toggled |= is_mouse_x_over_arrow && !g.NavDisableMouseHover; // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job - if ((flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) && g.IO.MouseDoubleClicked[0]) + if ((flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) && g.IO.MouseClickedCount[0] == 2) toggled = true; } else if (pressed && g.DragDropHoldJustPressedId == id) @@ -6226,7 +6284,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { - SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos)); // (bb == NavRect) + SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, WindowRectAbsToRel(window, bb)); // (bb == NavRect) g.NavDisableHighlight = true; } } @@ -6810,6 +6868,23 @@ void ImGui::EndMainMenuBar() End(); } +static bool IsRootOfOpenMenuSet() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if ((g.OpenPopupStack.Size <= g.BeginPopupStack.Size) || (window->Flags & ImGuiWindowFlags_ChildMenu)) + return false; + + // Initially we used 'OpenParentId' to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) based on parent ID. + // This would however prevent the use of e.g. PuhsID() user code submitting menus. + // Previously this worked between popup and a first child menu because the first child menu always had the _ChildWindow flag, + // making hovering on parent popup possible while first child menu was focused - but this was generally a bug with other side effects. + // Instead we don't treat Popup specifically (in order to consistently support menu features in them), maybe the first child menu of a Popup + // doesn't have the _ChildWindow flag, and we rely on this IsRootOfOpenMenuSet() check to allow hovering between root window/popup and first chilld menu. + const ImGuiPopupData* upper_popup = &g.OpenPopupStack[g.BeginPopupStack.Size]; + return (/*upper_popup->OpenParentId == window->IDStack.back() &&*/ upper_popup->Window && (upper_popup->Window->Flags & ImGuiWindowFlags_ChildMenu)); +} + bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) { ImGuiWindow* window = GetCurrentWindow(); @@ -6822,8 +6897,9 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) bool menu_is_open = IsPopupOpen(id, ImGuiPopupFlags_None); // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) + // The first menu in a hierarchy isn't so hovering doesn't get accross (otherwise e.g. resizing borders with ImGuiButtonFlags_FlattenChildren would react), but top-most BeginMenu() will bypass that limitation. ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; - if (window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu)) + if (window->Flags & ImGuiWindowFlags_ChildMenu) flags |= ImGuiWindowFlags_ChildWindow; // If a menu with same the ID was already submitted, we will append to it, matching the behavior of Begin(). @@ -6842,11 +6918,12 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) g.MenusIdSubmittedThisFrame.push_back(id); ImVec2 label_size = CalcTextSize(label, NULL, true); - bool pressed; - bool menuset_is_open = !(window->Flags & ImGuiWindowFlags_Popup) && (g.OpenPopupStack.Size > g.BeginPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].OpenParentId == window->IDStack.back()); + + // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent without always being a Child window) + const bool menuset_is_open = IsRootOfOpenMenuSet(); ImGuiWindow* backed_nav_window = g.NavWindow; if (menuset_is_open) - g.NavWindow = window; // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent) + g.NavWindow = window; // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu, // However the final position is going to be different! It is chosen by FindBestWindowPosForPopup(). @@ -6856,6 +6933,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (!enabled) BeginDisabled(); const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; + bool pressed; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Menu inside an horizontal menu bar @@ -6972,7 +7050,9 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (menu_is_open) { SetNextWindowPos(popup_pos, ImGuiCond_Always); // Note: this is super misleading! The value will serve as reference for FindBestWindowPosForPopup(), not actual pos. + PushStyleVar(ImGuiStyleVar_ChildRounding, style.PopupRounding); // First level will use _PopupRounding, subsequent will use _ChildRounding menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + PopStyleVar(); } else { @@ -7015,13 +7095,19 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut ImVec2 pos = window->DC.CursorPos; ImVec2 label_size = CalcTextSize(label, NULL, true); + const bool menuset_is_open = IsRootOfOpenMenuSet(); + ImGuiWindow* backed_nav_window = g.NavWindow; + if (menuset_is_open) + g.NavWindow = window; + // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73), // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only. bool pressed; PushID(label); if (!enabled) BeginDisabled(); - const ImGuiSelectableFlags flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover; + + const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover; const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { @@ -7031,7 +7117,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); - pressed = Selectable("", selected, flags, ImVec2(w, 0.0f)); + pressed = Selectable("", selected, selectable_flags, ImVec2(w, 0.0f)); PopStyleVar(); RenderText(text_pos, label); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). @@ -7046,7 +7132,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); - pressed = Selectable("", false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); if (icon_w > 0.0f) RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); @@ -7063,6 +7149,8 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut if (!enabled) EndDisabled(); PopID(); + if (menuset_is_open) + g.NavWindow = backed_nav_window; return pressed; } @@ -7205,8 +7293,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG // Ensure correct ordering when toggling ImGuiTabBarFlags_Reorderable flag, or when a new tab was added while being not reorderable if ((flags & ImGuiTabBarFlags_Reorderable) != (tab_bar->Flags & ImGuiTabBarFlags_Reorderable) || (tab_bar->TabsAddedNew && !(flags & ImGuiTabBarFlags_Reorderable))) - if (tab_bar->Tabs.Size > 1) - ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByBeginOrder); + ImQsort(tab_bar->Tabs.Data, tab_bar->Tabs.Size, sizeof(ImGuiTabItem), TabItemComparerByBeginOrder); tab_bar->TabsAddedNew = false; // Flags From 1e0cb1016884a103f8a540994849b82642dbac9c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 5 Feb 2022 20:17:39 +0100 Subject: [PATCH 0607/1018] viewer: adding cam_light and scene_lights, and moving camera --- CMakeLists.txt | 4 +-- include/gproshan/viewer/shader.h | 4 +-- include/gproshan/viewer/viewer.h | 3 +- shaders/fragment.glsl | 4 +-- shaders/fragment_pointcloud.glsl | 4 +-- shaders/fragment_sphere.glsl | 4 +-- src/viewer/shader.cpp | 4 +-- src/viewer/viewer.cpp | 54 +++++++++++++++++++------------- 8 files changed, 47 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1db6c1b6..3bb6b250 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,8 +46,8 @@ endif(embree_FOUND) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) -find_package(GLEW 2 REQUIRED) -find_package(glfw3 3 REQUIRED) +find_package(GLEW REQUIRED) +find_package(glfw3 REQUIRED) find_package(glm REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) diff --git a/include/gproshan/viewer/shader.h b/include/gproshan/viewer/shader.h index 5a1b7647..aad74966 100644 --- a/include/gproshan/viewer/shader.h +++ b/include/gproshan/viewer/shader.h @@ -13,9 +13,9 @@ namespace gproshan { class shader { protected: - GLuint program {0}; + GLuint program = 0; std::map uniform; - bool linked; + bool linked = false; public: shader() = default; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index e796f990..e0860082 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -68,7 +68,8 @@ class viewer camera cam; - quaternion light; + quaternion cam_light; + std::vector scene_lights; glm::mat4 view_mat; glm::mat4 proj_mat; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 2164db45..9d4e91b5 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -13,7 +13,7 @@ layout(location = 0) out vec4 frag_color; uniform vec3 eye; -uniform vec3 light; +uniform vec3 cam_light; uniform bool render_flat; uniform bool render_wireframe; @@ -31,7 +31,7 @@ void main() color = mix(vec3(.2), color, d); } - color = shading(N, normalize(light - gs_position), normalize(eye - gs_position), color); + color = shading(N, normalize(cam_light - gs_position), normalize(eye - gs_position), color); frag_color = vec4(color, 1); } diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 4906cd9d..9952196f 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -10,7 +10,7 @@ in float vs_color; layout(location = 0) out vec4 frag_color; uniform vec3 eye; -uniform vec3 light; +uniform vec3 cam_light; uniform bool point_normals; @@ -19,7 +19,7 @@ void main() vec3 color = lines_colormap(vs_mesh_color, vs_color); if(point_normals) - color = shading(normalize(vs_normal), normalize(light - vs_position), normalize(eye - vs_position), color); + color = shading(normalize(vs_normal), normalize(cam_light - vs_position), normalize(eye - vs_position), color); frag_color = vec4(color, 1); } diff --git a/shaders/fragment_sphere.glsl b/shaders/fragment_sphere.glsl index 078ff5d8..e300d7b7 100644 --- a/shaders/fragment_sphere.glsl +++ b/shaders/fragment_sphere.glsl @@ -8,12 +8,12 @@ in vec3 vs_normal; layout(location = 0) out vec4 frag_color; uniform vec3 eye; -uniform vec3 light; +uniform vec3 cam_light; void main() { - vec3 color = shading(normalize(vs_normal), normalize(light - vs_position), normalize(eye - vs_position), vec3(1, 0, 0)); + vec3 color = shading(normalize(vs_normal), normalize(cam_light - vs_position), normalize(eye - vs_position), vec3(1, 0, 0)); frag_color = vec4(color, 1); } diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index 52b735d6..d44e76d3 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -16,7 +16,7 @@ namespace gproshan { shader::~shader() { - if(program) glDeleteProgram(program); + glDeleteProgram(program); } const GLint & shader::operator () (const string & name) @@ -73,7 +73,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) return false; } - if(program == 0) + if(!program) program = glCreateProgram(); // glDetachShader(program, shader); when/where can we need this? diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 0799741f..6cd7cca7 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -92,8 +92,8 @@ bool viewer::run() quaternion r = cam.current_rotation(); - light = vertex(-1, 1, -2); - light = r.conj() * light * r; + cam_light = vertex(-1, 1, -2); + cam_light = r.conj() * cam_light * r; view_mat = cam.look_at(r); proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); @@ -182,7 +182,6 @@ bool viewer::run() ImGui::Text("%16s: %.3f", "FPS", 1.0 / render_time); ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); - ImGui::DragFloat4("camera center", (float *) &cam.pos, 0.2); if(mesh.render_pointcloud) { @@ -426,24 +425,28 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) view->pick_vertex(xpos, ypos); - else if(button == GLFW_MOUSE_BUTTON_RIGHT) - { - } - else + else if(button == GLFW_MOUSE_BUTTON_LEFT) view->cam.mouse(action == GLFW_PRESS, xpos, ypos, view->window_width, view->window_height); } void viewer::cursor_callback(GLFWwindow * window, double x, double y) { - int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT); - if(state == GLFW_PRESS) - { - viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(ImGui::GetIO().WantCaptureMouse) return; + if(ImGui::GetIO().WantCaptureMouse) return; + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + + if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) + { view->cam.motion(x, y, view->window_width, view->window_height); view->rt_restart = true; } + + if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)) + { + view->cam.pos.im().x = 2 * x / view->window_width - 1; + view->cam.pos.im().y = 2 * y / view->window_height - 1; + view->rt_restart = true; + } } void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) @@ -592,14 +595,12 @@ bool viewer::menu_save_mesh(viewer * view) bool viewer::menu_zoom_in(viewer * view) { view->cam.zoom_in(); - return false; } bool viewer::menu_zoom_out(viewer * view) { view->cam.zoom_out(); - return false; } @@ -706,8 +707,10 @@ bool viewer::set_render_optix(viewer * view) bool viewer::invert_orientation(viewer * view) { - view->active_mesh().invert_orientation(); - view->active_mesh().update_vbo_normal(); + che_viewer & mesh = view->active_mesh(); + + mesh.invert_orientation(); + mesh.update_vbo_normal(); return false; } @@ -803,18 +806,18 @@ bool viewer::raycasting(viewer * view) void viewer::render_gl() { glProgramUniform3f(shader_sphere, shader_sphere("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_sphere, shader_sphere("light"), light[0], light[1], light[2]); + glProgramUniform3f(shader_sphere, shader_sphere("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_triangles, shader_triangles("light"), light[0], light[1], light[2]); + glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("light"), light[0], light[1], light[2]); + glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); @@ -859,9 +862,18 @@ void viewer::render_rt(rt::raytracing * rt) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); + scene_lights.clear(); + + che_viewer & mesh = active_mesh(); + for(const index_t & v: mesh.selected) + scene_lights.push_back(glm_vec3(mesh->gt(v))); + + if(!scene_lights.size()) + scene_lights = {glm_vec3(cam_light)}; + rt->render( img, glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, {glm_vec3(light)}, - active_mesh().render_flat, rt_restart + view_mat, proj_mat, scene_lights, + mesh.render_flat, rt_restart ); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); From 4eb3e68a0f1e6167f924e8bd3362a7639140d5dd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 5 Feb 2022 21:42:14 +0100 Subject: [PATCH 0608/1018] rt_optix: refactoring render params --- src/raytracing/rt_optix.cpp | 2 +- src/raytracing/rt_optix.cu | 38 ++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 7e1fdedc..66c241e7 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -69,7 +69,7 @@ optix::optix(const std::vector & meshes) optix_pipeline_compile_opt.numPayloadValues = 2; optix_pipeline_compile_opt.numAttributeValues = 2; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optixLaunchParams"; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "render_params"; optix_pipeline_link_opt.maxTraceDepth = 2; diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index 80555850..f7f0d1a4 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -27,7 +27,7 @@ vertex_cu normalize (const vertex_cu & v) } -extern "C" __constant__ launch_params optixLaunchParams; +extern "C" __constant__ launch_params render_params; static __forceinline__ __device__ void * unpackPointer(uint32_t i0, uint32_t i1) @@ -74,13 +74,13 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu & C = mesh.GT[c]; const vertex_cu Ng = normalize((B - A) * (C - A)); - const vertex_cu normal = optixLaunchParams.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + const vertex_cu normal = render_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; const vertex_cu ca(mesh.VC[a].r, mesh.VC[a].g, mesh.VC[a].b); const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); const vertex_cu cc(mesh.VC[c].r, mesh.VC[c].g, mesh.VC[c].b); - const vertex_cu & light = *(vertex_cu *) optixLaunchParams.light; + const vertex_cu & light = *(vertex_cu *) render_params.light; const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex_cu position = (1.f - u - v) * A + u * B + v * C; @@ -91,7 +91,7 @@ extern "C" __global__ void __closesthit__radiance() L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; unsigned int occluded = 1; - optixTrace( optixLaunchParams.traversable, + optixTrace( render_params.traversable, position, wi, 1e-5f, // tmin @@ -118,7 +118,7 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { vertex_cu & prd = *getPRD(); - prd = {0, 0,0}; + prd = {0, 0, 0}; } extern "C" __global__ void __miss__shadow() @@ -132,26 +132,26 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const float sx = (float(ix) + .5f) / optixLaunchParams.frame.width; - const float sy = (float(iy) + .5f) / optixLaunchParams.frame.height; + const float sx = (float(ix) + .5f) / render_params.frame.width; + const float sy = (float(iy) + .5f) / render_params.frame.height; - vertex_cu & cam_pos = *(vertex_cu *) optixLaunchParams.cam_pos; + vertex_cu & cam_pos = *(vertex_cu *) render_params.cam_pos; vertex_cu ipv[3]; for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) - ipv[i][j] = optixLaunchParams.inv_proj_view[i + j * 4]; + ipv[i][j] = render_params.inv_proj_view[i + j * 4]; - vertex_cu d = { optixLaunchParams.inv_proj_view[0 * 4 + 3], - optixLaunchParams.inv_proj_view[1 * 4 + 3], - optixLaunchParams.inv_proj_view[2 * 4 + 3] + vertex_cu d = { render_params.inv_proj_view[0 * 4 + 3], + render_params.inv_proj_view[1 * 4 + 3], + render_params.inv_proj_view[2 * 4 + 3] }; - vertex_cu e = { optixLaunchParams.inv_proj_view[3 * 4 + 0], - optixLaunchParams.inv_proj_view[3 * 4 + 1], - optixLaunchParams.inv_proj_view[3 * 4 + 2] + vertex_cu e = { render_params.inv_proj_view[3 * 4 + 0], + render_params.inv_proj_view[3 * 4 + 1], + render_params.inv_proj_view[3 * 4 + 2] }; - float & de = optixLaunchParams.inv_proj_view[15]; + float & de = render_params.inv_proj_view[15]; vertex_cu view = {sx * 2 - 1, sy * 2 - 1, 1}; vertex_cu q = vertex_cu{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; @@ -162,7 +162,7 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu pixelColorPRD; uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); - optixTrace( optixLaunchParams.traversable, + optixTrace( render_params.traversable, cam_pos, ray_dir, 0.f, // tmin @@ -175,9 +175,9 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); - const uint32_t fbIndex = ix + iy * optixLaunchParams.frame.width; + const uint32_t fbIndex = ix + iy * render_params.frame.width; - float4 * frame = (float4 *) optixLaunchParams.frame.color_buffer; + float4 * frame = (float4 *) render_params.frame.color_buffer; frame[fbIndex].x = pixelColorPRD.x; frame[fbIndex].y = pixelColorPRD.y; frame[fbIndex].z = pixelColorPRD.z; From c20ffd8239f42fca9748ae95ec74dadd11248595 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Apr 2022 14:41:33 +0200 Subject: [PATCH 0609/1018] viewer: refactor add_process method --- include/gproshan/viewer/viewer.h | 2 +- src/app_viewer.cpp | 60 ++++++++++++++++---------------- src/viewer/viewer.cpp | 52 +++++++++++++-------------- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index e0860082..48eb9e60 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -110,7 +110,7 @@ class viewer bool run(); che_viewer & active_mesh(); - void add_process(const int & key, const process_t & process); + void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); void add_mesh(che * p_mesh); private: diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 03e99940..6abfd888 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -58,48 +58,48 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { sub_menus.push_back("Geometry"); - add_process(GLFW_KEY_H, {"H", "2D Convex Hull", process_convex_hull}); - add_process(GLFW_KEY_C, {"C", "Connected Components", process_connected_components}); - add_process(GLFW_KEY_K, {"K", "Gaussian curvature", process_gaussian_curvature}); - add_process(GLFW_KEY_Q, {"Q", "Edge Collapse", process_edge_collapse}); - add_process(GLFW_KEY_M, {"M", "Multiplicate", process_multiplicate_vertices}); - add_process(GLFW_KEY_DELETE, {"DELETE", "Delete vertices", process_delete_vertices}); - add_process(GLFW_KEY_MINUS, {"MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices}); + add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); + add_process(GLFW_KEY_C, "C", "Connected Components", process_connected_components); + add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); + add_process(GLFW_KEY_Q, "Q", "Edge Collapse", process_edge_collapse); + add_process(GLFW_KEY_M, "M", "Multiplicate", process_multiplicate_vertices); + add_process(GLFW_KEY_DELETE, "DELETE", "Delete vertices", process_delete_vertices); + add_process(GLFW_KEY_MINUS, "MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices); sub_menus.push_back("Fairing"); - add_process(GLFW_KEY_F, {"F", "Fairing Taubin", process_fairing_taubin}); - add_process(GLFW_KEY_E, {"E", "Fairing Spectral", process_fairing_spectral}); + add_process(GLFW_KEY_F, "F", "Fairing Taubin", process_fairing_taubin); + add_process(GLFW_KEY_E, "E", "Fairing Spectral", process_fairing_spectral); sub_menus.push_back("Geodesics"); - add_process(GLFW_KEY_G, {"G", "Geodesics", process_geodesics}); - add_process(GLFW_KEY_S, {"S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling}); - add_process(GLFW_KEY_V, {"V", "Geodesic Voronoi", process_voronoi}); - add_process(GLFW_KEY_T, {"T", "Toplesets", process_compute_toplesets}); + add_process(GLFW_KEY_G, "G", "Geodesics", process_geodesics); + add_process(GLFW_KEY_S, "S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling); + add_process(GLFW_KEY_V, "V", "Geodesic Voronoi", process_voronoi); + add_process(GLFW_KEY_T, "T", "Toplesets", process_compute_toplesets); sub_menus.push_back("Sparse Coding"); - add_process(GLFW_KEY_I, {"I", "Mesh Sparse Coding", process_msparse_coding}); - add_process(GLFW_KEY_J, {"J", "MDICT Patch", process_mdict_patch}); - add_process(GLFW_KEY_D, {"D", "MDICT Mask", process_mask}); - add_process(GLFW_KEY_L, {"L", "PC reconstruction", process_pc_reconstruction}); + add_process(GLFW_KEY_I, "I", "Mesh Sparse Coding", process_msparse_coding); + add_process(GLFW_KEY_J, "J", "MDICT Patch", process_mdict_patch); + add_process(GLFW_KEY_D, "D", "MDICT Mask", process_mask); + add_process(GLFW_KEY_L, "L", "PC reconstruction", process_pc_reconstruction); sub_menus.push_back("Features"); - add_process(GLFW_KEY_2, {"2", "Eigenfunctions", process_eigenfuntions}); - add_process(GLFW_KEY_3, {"3", "Descriptors", process_descriptor_heatmap}); - add_process(GLFW_KEY_4, {"4", "Key Points", process_key_points}); - add_process(GLFW_KEY_5, {"5", "Key Components", process_key_components}); + add_process(GLFW_KEY_2, "2", "Eigenfunctions", process_eigenfuntions); + add_process(GLFW_KEY_3, "3", "Descriptors", process_descriptor_heatmap); + add_process(GLFW_KEY_4, "4", "Key Points", process_key_points); + add_process(GLFW_KEY_5, "5", "Key Components", process_key_components); sub_menus.push_back("Hole Filling"); - add_process(GLFW_KEY_X, {"X", "Poisson: Membrane surface", process_poisson_laplacian_1}); - add_process(GLFW_KEY_Y, {"Y", "Poisson: Thin-plate surface", process_poisson_laplacian_2}); - add_process(GLFW_KEY_Z, {"Z", "Poisson: Minimum variation surface", process_poisson_laplacian_3}); - add_process(GLFW_KEY_6, {"6", "Fill hole: planar mesh", process_fill_holes}); - add_process(GLFW_KEY_7, {"7", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines}); + add_process(GLFW_KEY_X, "X", "Poisson: Membrane surface", process_poisson_laplacian_1); + add_process(GLFW_KEY_Y, "Y", "Poisson: Thin-plate surface", process_poisson_laplacian_2); + add_process(GLFW_KEY_Z, "Z", "Poisson: Minimum variation surface", process_poisson_laplacian_3); + add_process(GLFW_KEY_6, "6", "Fill hole: planar mesh", process_fill_holes); + add_process(GLFW_KEY_7, "7", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines); sub_menus.push_back("Others"); - add_process(GLFW_KEY_SEMICOLON, {"SEMICOLON", "Select multiple vertices", process_select_multiple}); - add_process(GLFW_KEY_SLASH, {"SLASH", "Threshold", process_threshold}); - add_process(GLFW_KEY_N, {"N", "Noise", process_noise}); - add_process(GLFW_KEY_B, {"B", "Black noise", process_black_noise}); + add_process(GLFW_KEY_SEMICOLON, "SEMICOLON", "Select multiple vertices", process_select_multiple); + add_process(GLFW_KEY_SLASH, "SLASH", "Threshold", process_threshold); + add_process(GLFW_KEY_N, "N", "Noise", process_noise); + add_process(GLFW_KEY_B, "B", "Black noise", process_black_noise); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 6cd7cca7..5b4b0dd2 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -287,38 +287,38 @@ void viewer::init_imgui() void viewer::init_menus() { sub_menus.push_back("Viewer"); - add_process(GLFW_KEY_F1, {"F1", "Help", menu_help}); - add_process(GLFW_KEY_PERIOD, {"PERIOD", "Save/Load view", menu_save_load_view}); - add_process(GLFW_KEY_UP, {"UP", "Zoom in", menu_zoom_in}); - add_process(GLFW_KEY_DOWN, {"DOWN", "Zoom out", menu_zoom_out}); - add_process(GLFW_KEY_RIGHT, {"RIGHT", "Background color inc", menu_bgc_inc}); - add_process(GLFW_KEY_LEFT, {"LEFT", "Background color dec", menu_bgc_dec}); - add_process(GLFW_KEY_1, {"1", "Background color white", menu_bgc_white}); - add_process(GLFW_KEY_0, {"0", "Background color black", menu_bgc_black}); + add_process(GLFW_KEY_F1, "F1", "Help", menu_help); + add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", menu_save_load_view); + add_process(GLFW_KEY_UP, "UP", "Zoom in", menu_zoom_in); + add_process(GLFW_KEY_DOWN, "DOWN", "Zoom out", menu_zoom_out); + add_process(GLFW_KEY_RIGHT, "RIGHT", "Background color inc", menu_bgc_inc); + add_process(GLFW_KEY_LEFT, "LEFT", "Background color dec", menu_bgc_dec); + add_process(GLFW_KEY_1, "1", "Background color white", menu_bgc_white); + add_process(GLFW_KEY_0, "0", "Background color black", menu_bgc_black); sub_menus.push_back("Render"); - add_process(GLFW_KEY_F5, {"F5", "Render Point Cloud", set_render_pointcloud}); - add_process(GLFW_KEY_F6, {"F6", "Render Wireframe", set_render_wireframe}); - add_process(GLFW_KEY_F7, {"F7", "Render Triangles", set_render_triangles}); - add_process(GLFW_KEY_F8, {"F8", "Render GL", set_render_gl}); - add_process(GLFW_KEY_R, {"R", "Setup Raytracing", setup_raytracing}); + add_process(GLFW_KEY_F5, "F5", "Render Point Cloud", set_render_pointcloud); + add_process(GLFW_KEY_F6, "F6", "Render Wireframe", set_render_wireframe); + add_process(GLFW_KEY_F7, "F7", "Render Triangles", set_render_triangles); + add_process(GLFW_KEY_F8, "F8", "Render GL", set_render_gl); + add_process(GLFW_KEY_R, "R", "Setup Raytracing", setup_raytracing); #ifdef GPROSHAN_EMBREE - add_process(GLFW_KEY_F9, {"F9", "Render Embree", set_render_embree}); - add_process(GLFW_KEY_ENTER, {"ENTER", "Raycasting", raycasting}); + add_process(GLFW_KEY_F9, "F9", "Render Embree", set_render_embree); + add_process(GLFW_KEY_ENTER, "ENTER", "Raycasting", raycasting); #endif // GPROSHAN_EMBREE #ifdef GPROSHAN_OPTIX - add_process(GLFW_KEY_F10, {"F10", "Render OptiX", set_render_optix}); + add_process(GLFW_KEY_F10, "F10", "Render OptiX", set_render_optix); #endif // GPROSHAN_OPTIX sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, {"BACKSPACE", "Reload/Reset", menu_reset_mesh}); - add_process(GLFW_KEY_TAB, {"TAB", "Render Flat", set_render_flat}); - add_process(GLFW_KEY_SPACE, {"SPACE", "Level Curves", set_render_lines}); - add_process(GLFW_KEY_F2, {"F2", "Invert Orientation", invert_orientation}); - add_process(GLFW_KEY_F3, {"F3", "Gradient Field", set_render_gradients}); - add_process(GLFW_KEY_F4, {"F4", "Normal Field", set_render_normals}); - add_process(GLFW_KEY_APOSTROPHE, {"APOSTROPHE", "Select Border Vertices", set_render_border}); - add_process(GLFW_KEY_W, {"W", "Save Mesh", menu_save_mesh}); + add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", menu_reset_mesh); + add_process(GLFW_KEY_TAB, "TAB", "Render Flat", set_render_flat); + add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", set_render_lines); + add_process(GLFW_KEY_F2, "F2", "Invert Orientation", invert_orientation); + add_process(GLFW_KEY_F3, "F3", "Gradient Field", set_render_gradients); + add_process(GLFW_KEY_F4, "F4", "Normal Field", set_render_normals); + add_process(GLFW_KEY_APOSTROPHE, "APOSTROPHE", "Select Border Vertices", set_render_border); + add_process(GLFW_KEY_W, "W", "Save Mesh", menu_save_mesh); } void viewer::init_glsl() @@ -342,11 +342,11 @@ void viewer::init_glsl() shader_pointcloud.load_fragment(shaders_path("fragment_pointcloud.glsl")); } -void viewer::add_process(const int & key, const process_t & process) +void viewer::add_process(const int & key, const string & skey, const string & name, const function_t & f) { if(processes.find(key) == processes.end()) { - processes[key] = process; + processes[key] = {skey, name, f}; processes[key].sub_menu = sub_menus.size() - 1; } else cerr << "Repeat key: " << key << endl; From 2575f2a9a049e968395852fe6087c6b48afe5de6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Apr 2022 22:08:33 +0200 Subject: [PATCH 0610/1018] scenes: adding scanner_ptx function simulates a 360 degrees scanner returning a ptx point cloud --- include/gproshan/app_viewer.h | 6 ++++++ include/gproshan/scenes/scanner.h | 18 ++++++++++++++++ src/app_viewer.cpp | 34 +++++++++++++++++++++++++++++++ src/scenes/scanner.cpp | 18 ++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 include/gproshan/scenes/scanner.h create mode 100644 src/scenes/scanner.cpp diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 2bf3587c..01c64674 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -20,6 +20,8 @@ #include "laplacian/fairing_taubin.h" #include "laplacian/fairing_spectral.h" +#include "scenes/scanner.h" + #include "geometry/convex_hull.h" #include "geodesics/dijkstra.h" @@ -55,6 +57,10 @@ class app_viewer : public viewer che * load_mesh(const string & file_path); + // Scenes + static bool process_compute_normals(viewer * p_view); + static bool process_simulate_scanner(viewer * p_view); + // Geometry static bool process_convex_hull(viewer * p_view); static bool process_connected_components(viewer * p_view); diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h new file mode 100644 index 00000000..b50ad496 --- /dev/null +++ b/include/gproshan/scenes/scanner.h @@ -0,0 +1,18 @@ +#ifndef SCANNER_H +#define SCANNER_H + +#include "raytracing/raytracing.h" + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); + + +} // namespace gproshan + +#endif // SCANNER_H + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6abfd888..b1a32c6a 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -57,6 +57,10 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { + sub_menus.push_back("Scenes"); + add_process(1001, "", "Compute Normals", process_compute_normals); + add_process(1002, "", "Scan Scene", process_simulate_scanner); + sub_menus.push_back("Geometry"); add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); add_process(GLFW_KEY_C, "C", "Connected Components", process_connected_components); @@ -103,6 +107,36 @@ void app_viewer::init() } +// Scenes + +bool app_viewer::process_compute_normals(viewer * p_view) +{ + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); +} + +bool app_viewer::process_simulate_scanner(viewer * p_view) +{ + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + static size_t n_rows = 2000; + static size_t n_cols = 4000; + static const size_t n_min = 1000; + static const size_t n_max = 1000000; + + ImGui::SliderScalar("n_rows", ImGuiDataType_U64, &n_rows, &n_min, &n_max); + ImGui::SliderScalar("n_cols", ImGuiDataType_U64, &n_cols, &n_min, &n_max); + + if(ImGui::Button("Scan")) + { + view->add_mesh(scanner_ptx(view->rt_embree, n_rows, n_cols, {0, 0, 0})); + } + + return true; +} + + // Geometry bool app_viewer::process_convex_hull(viewer * p_view) diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp new file mode 100644 index 00000000..9f3591c1 --- /dev/null +++ b/src/scenes/scanner.cpp @@ -0,0 +1,18 @@ +#include "scenes/scanner.h" + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) +{ + std::vector ptx; + + return new che(ptx.data(), ptx.size(), nullptr, 0); +} + + +} // namespace gproshan + From 398f126aacaedb10c203dfec5d772afaa598a629 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 01:43:56 +0200 Subject: [PATCH 0611/1018] scenes: updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3114c3da..a16f8b77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version History --------------- ### gproshan 3.0.0 +- Adding Scenes module, virtual point cloud scanners and point cloud normals computation for 3D scenes. - Added render option using [OptiX](https://developer.nvidia.com/optix) ray tracing mesh, shadows. - Add module geometry, including a 2D convex hull algorithm implementation and connected components detection. - Supported file mesh types include: off, obj, ply, ptx, xyz, and any depth image or image file loaded as a mesh. From 17c2680a04ad7e3f04f97be869535e158d228332 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 01:52:51 +0200 Subject: [PATCH 0612/1018] readme: adding embree install section --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79eba5d9..feb1e560 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,22 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.3, cuda >= 11.0, cmake >= 3.18, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot +g++ >= 9.3, cuda >= 11.0, cmake >= 3.18, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot, embree >= 3.13 In Ubuntu you can install them with: sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot +#### Installing Intel Embree + +Intel Embree is a collection of high performance ray tracing kernels that helps graphics application engineers to improve the performance of their photorealistic rendering application ([https://www.embree.org/](https://www.embree.org/)). + +##### Ubuntu (Linux) + +##### MacOS + + brew install embree + ## Contributions From 80a23a5715dcaaf5b20287f4ba7a9724a40b198f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 01:57:06 +0200 Subject: [PATCH 0613/1018] readme: adding embree install section --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index feb1e560..b1c8c5ac 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,18 @@ Intel Embree is a collection of high performance ray tracing kernels that helps ##### Ubuntu (Linux) + # download the key to system keyring + wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ + | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null + + # add signed entry to apt sources and configure the APT client to use Intel repository: + echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list + + sudo apt install intel-basekit + + # configure the enviroment variables + source /opt/intel/oneapi/setvars.sh + ##### MacOS brew install embree From 3b31cd435874a15e4df8244b966df2e17089981d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 02:00:51 +0200 Subject: [PATCH 0614/1018] Update build.yml --- .github/workflows/build.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3972fc20..cec8c54f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,7 +45,15 @@ jobs: sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" sudo apt update sudo apt -y install cuda - + + - name: Install Embree + run: | + wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ + | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null + echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list + sudo apt install intel-renderkit + source /opt/intel/oneapi/setvars.sh + - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 1ea73df0f493e0c720949b046c42812e5d5e0ab7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 02:03:50 +0200 Subject: [PATCH 0615/1018] Update build.yml --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cec8c54f..cd50e808 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -51,7 +51,8 @@ jobs: wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list - sudo apt install intel-renderkit + sudo apt update + sudo apt -y install intel-renderkit source /opt/intel/oneapi/setvars.sh - name: Create Build Environment From 73850aeb59097233dbe311f8941bcee5edde9e25 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 02:10:13 +0200 Subject: [PATCH 0616/1018] Update build.yml --- .github/workflows/build.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cd50e808..aedaa455 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,7 +9,7 @@ jobs: build: name: ${{ matrix.os }} (${{ matrix.config }}) ${{ matrix.cuda }} runs-on: ${{ matrix.os }} - + strategy: fail-fast: false matrix: @@ -19,7 +19,7 @@ jobs: steps: - uses: actions/checkout@v2 - + - name: Install Dependencies run: | sudo apt remove --purge cmake @@ -35,7 +35,7 @@ jobs: libglfw3-dev \ libglm-dev \ cimg-dev - + - name: Install Cuda if: matrix.cuda == 'cuda' run: | @@ -53,7 +53,6 @@ jobs: echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list sudo apt update sudo apt -y install intel-renderkit - source /opt/intel/oneapi/setvars.sh - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build @@ -63,10 +62,10 @@ jobs: working-directory: ${{runner.workspace}}/build run: | export PATH=${CUDA_BIN}:${PATH} - cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{ matrix.config }} + source /opt/intel/oneapi/setvars.sh + cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.config}} - name: Build working-directory: ${{runner.workspace}}/build shell: bash - run: cmake --build . --config ${{ matrix.config }} - + run: cmake --build . --config ${{matrix.config}} From dfd36f324b4a2641c3d077f2ba2dade862d06ad1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Apr 2022 02:18:50 +0200 Subject: [PATCH 0617/1018] Update build.yml --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index aedaa455..596c565c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -68,4 +68,6 @@ jobs: - name: Build working-directory: ${{runner.workspace}}/build shell: bash - run: cmake --build . --config ${{matrix.config}} + run: | + source /opt/intel/oneapi/setvars.sh + cmake --build . --config ${{matrix.config}} From 05c903cafbd50ab798e324ae2ff606c9aabe44db Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Apr 2022 00:44:21 +0200 Subject: [PATCH 0618/1018] gproshan: add embree as default library for ray tracing operations --- CHANGELOG.md | 1 + CMakeLists.txt | 14 +++----------- include/gproshan/raytracing/rt_embree.h | 4 ---- include/gproshan/viewer/viewer.h | 2 -- src/raytracing/rt_embree.cpp | 3 --- src/viewer/che_viewer.cpp | 7 +------ src/viewer/viewer.cpp | 11 ++--------- 7 files changed, 7 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a16f8b77..bc1f1d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version History --------------- ### gproshan 3.0.0 +- Added Intel Embree as default ray tracing library, for ray casting operations and as rendering option with shadows. - Adding Scenes module, virtual point cloud scanners and point cloud normals computation for 3D scenes. - Added render option using [OptiX](https://developer.nvidia.com/optix) ray tracing mesh, shadows. - Add module geometry, including a 2D convex hull algorithm implementation and connected components detection. diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bb6b250..8c891b07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,13 +37,7 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) -find_package(embree 3.13) -if(embree_FOUND) - add_definitions(-DGPROSHAN_EMBREE) - include_directories(SYSTEM ${embree_INCLUDE_DIRS}) -endif(embree_FOUND) - - +find_package(embree 3.13 REQUIRED) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) find_package(GLEW REQUIRED) @@ -57,6 +51,7 @@ find_package(SuiteSparse REQUIRED) find_package(Boost COMPONENTS thread system) +include_directories(SYSTEM ${embree_INCLUDE_DIRS}) include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) @@ -77,6 +72,7 @@ FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) add_library(gproshan SHARED ${cpp_sources}) +target_link_libraries(gproshan embree) target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) @@ -87,10 +83,6 @@ target_link_libraries(gproshan CGAL::CGAL) target_link_libraries(gproshan ${OptiX_LIBRARY}) target_link_libraries(gproshan imgui) -if(embree_FOUND) - target_link_libraries(gproshan embree) -endif(embree_FOUND) - if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 6b81f3b0..8b69289a 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -1,5 +1,3 @@ -#ifdef GPROSHAN_EMBREE - #ifndef RT_EMBREE_H #define RT_EMBREE_H @@ -71,5 +69,3 @@ class embree : public raytracing #endif // RT_EMBREE_H -#endif // GPROSHAN_EMBREE - diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 48eb9e60..60917c4f 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -156,9 +156,7 @@ class viewer static bool set_render_lines(viewer * view); static bool set_render_flat(viewer * view); - #ifdef GPROSHAN_EMBREE static bool raycasting(viewer * view); - #endif // GPROSHAN_EMBREE // draw routines void draw_selected_vertices(shader & program, const che_viewer & mesh); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index 43ae001d..e8108933 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -1,6 +1,5 @@ #include "raytracing/rt_embree.h" -#ifdef GPROSHAN_EMBREE #include "util.h" @@ -306,5 +305,3 @@ float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) } // namespace gproshan -#endif // GPROSHAN_EMBREE - diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 509a4d83..66a7c840 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -4,9 +4,7 @@ #include #include -#ifdef GPROSHAN_EMBREE - #include "raytracing/rt_embree.h" -#endif // GPROSHAN_EMBREE +#include "raytracing/rt_embree.h" using namespace std; @@ -84,11 +82,8 @@ void che_viewer::update() update_vbo(); - -#ifdef GPROSHAN_EMBREE delete pick_vertex; pick_vertex = new rt::embree({mesh}); -#endif // GPROSHAN_EMBREE } void che_viewer::update_vbo() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 5b4b0dd2..eaf834eb 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -18,9 +18,7 @@ #include "mesh/che_pts.h" #include "mesh/che_sphere.h" -#ifdef GPROSHAN_EMBREE - #include "raytracing/rt_embree.h" -#endif // GPROSHAN_EMBREE +#include "raytracing/rt_embree.h" #ifdef GPROSHAN_OPTIX #include "raytracing/rt_optix.h" @@ -302,10 +300,9 @@ void viewer::init_menus() add_process(GLFW_KEY_F7, "F7", "Render Triangles", set_render_triangles); add_process(GLFW_KEY_F8, "F8", "Render GL", set_render_gl); add_process(GLFW_KEY_R, "R", "Setup Raytracing", setup_raytracing); -#ifdef GPROSHAN_EMBREE add_process(GLFW_KEY_F9, "F9", "Render Embree", set_render_embree); add_process(GLFW_KEY_ENTER, "ENTER", "Raycasting", raycasting); -#endif // GPROSHAN_EMBREE + #ifdef GPROSHAN_OPTIX add_process(GLFW_KEY_F10, "F10", "Render OptiX", set_render_optix); #endif // GPROSHAN_OPTIX @@ -661,13 +658,11 @@ bool viewer::setup_raytracing(viewer * view) case R_GL: break; case R_EMBREE: - #ifdef GPROSHAN_EMBREE delete view->rt_embree; TIC(time); view->rt_embree = new rt::embree({mesh}, mesh.render_pointcloud, pc_radius); TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); - #endif // GPROSHAN_EMBREE break; case R_OPTIX: @@ -782,7 +777,6 @@ bool viewer::set_render_flat(viewer * view) return false; } -#ifdef GPROSHAN_EMBREE bool viewer::raycasting(viewer * view) { rt::embree rc({view->active_mesh()}); @@ -801,7 +795,6 @@ bool viewer::raycasting(viewer * view) return false; } -#endif // GPROSHAN_EMBREE void viewer::render_gl() { From 6a0dfca4e3a0732af5e8888993ab6bb75d2b3c1b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Apr 2022 23:29:44 +0200 Subject: [PATCH 0619/1018] gproshan: updating colors and shading methods, gl and rt --- include/gproshan/mesh/che.h | 2 +- src/app_viewer.cpp | 2 +- src/mesh/che.cpp | 29 +++++++---------------------- src/viewer/che_viewer.cpp | 2 +- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 34914d13..0b57f4cb 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -81,7 +81,7 @@ class che real_t area_trig(const index_t & t) const; real_t area_vertex(const index_t & v) const; real_t area_surface() const; - void update_heatmap(const real_t * hm = nullptr, real_t max_color = 0); + void update_heatmap(const real_t * hm = nullptr); const rgb_t & rgb(const index_t & v) const; rgb_t & rgb(const index_t & v); vertex color(const index_t & v) const; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b1a32c6a..1886aa7f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -119,7 +119,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - + static size_t n_rows = 2000; static size_t n_cols = 4000; static const size_t n_min = 1000; diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 8d3bb837..4b82ecdf 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -269,28 +269,18 @@ real_t che::area_surface() const return area; } -void che::update_heatmap(const real_t * hm, real_t max_color) +void che::update_heatmap(const real_t * hm) { if(!hm) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - VHC[v] = 0.45; + VHC[v] = 0.45; // default heatmap value return; } - if(max_color < numeric_limits::epsilon()) - { - #pragma omp parallel for reduction(max: max_color) - for(index_t v = 0; v < n_vertices; ++v) - if(hm[v] < INFINITY) - max_color = max(hm[v], max_color); - } - - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - VHC[v] = hm[v] / max_color; + memcpy(VHC, hm, n_vertices * sizeof(real_t)); } const che::rgb_t & che::rgb(const index_t & v) const @@ -313,8 +303,8 @@ vertex che::color(const index_t & v) const vertex che::shading_color(const index_t & f, const float & u, const float & v, const float & w) const { - index_t he = f * che::mtrig; - return VC ? u * color(VT[he]) + v * color(VT[he + 1]) + w * color(VT[he + 2]) : rgb_t(); + const index_t & he = f * che::mtrig; + return u * color(VT[he]) + v * color(VT[he + 1]) + w * color(VT[he + 2]); } const real_t & che::heatmap(const index_t & v) const @@ -360,9 +350,8 @@ vertex & che::normal(const index_t & v) vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const { - index_t he = f * che::mtrig; - - return {u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]}; + const index_t & he = f * che::mtrig; + return u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]; } vertex che::normal_trig(const index_t & f) const @@ -1034,10 +1023,6 @@ void che::alloc(const size_t & n_v, const size_t & n_f) if(n_vertices) VC = new rgb_t[n_vertices]; if(n_vertices) VHC = new real_t[n_vertices]; - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - VC[v] = rgb_t(); - update_heatmap(); } diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 66a7c840..be7e6cf9 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -158,7 +158,7 @@ void che_viewer::update_vbo_heatmap(const real_t * vheatmap) glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), vheatmap, GL_STATIC_DRAW); glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 1, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(3, 1, GL_REAL, GL_TRUE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); From 38ccd4d6be713c4ab9a99d486799203271a0da22 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 25 Apr 2022 02:28:03 +0200 Subject: [PATCH 0620/1018] che: update trig_convec_polygon static method --- src/mesh/che.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 4b82ecdf..6a2625eb 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1127,13 +1127,18 @@ void che::update_eht() vector che::trig_convex_polygon(const index_t * P, const size_t & n) { vector trigs; - trigs.reserve(che::mtrig * (n - 2)); - for(index_t i = 2; i < n; ++i) + + index_t a = n - 1; + index_t b = 0; + index_t c = 1; + while(a > c) { - trigs.push_back(P[0]); - trigs.push_back(P[i - 1]); - trigs.push_back(P[i]); + trigs.push_back(P[a]); + trigs.push_back(P[b]); + trigs.push_back(P[c]); + + b = (b + 1 == c) ? a-- : c++; } return trigs; From 93f6268f7177d9ee38a7986847f205469724296e Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Thu, 5 May 2022 17:48:57 +0200 Subject: [PATCH 0621/1018] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b1c8c5ac..6d976207 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ Intel Embree is a collection of high performance ray tracing kernels that helps # add signed entry to apt sources and configure the APT client to use Intel repository: echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list - sudo apt install intel-basekit + sudo apt update + sudo apt install intel-renderkit # configure the enviroment variables source /opt/intel/oneapi/setvars.sh From b7a9ed2f3eab7b389f0282fa0df290cc33a106da Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Thu, 5 May 2022 18:03:57 +0200 Subject: [PATCH 0622/1018] Update build.yml --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 596c565c..4e585cfe 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -41,10 +41,10 @@ jobs: run: | wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub + sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo apt update - sudo apt -y install cuda + sudo apt-get update + sudo apt-get -y install cuda - name: Install Embree run: | From d795d2331d5262ca17b37bd0b6132495e1a48d66 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 03:03:02 +0200 Subject: [PATCH 0623/1018] viewer: refactoring proj_view_mat and cam_pos --- include/gproshan/raytracing/raytracing.h | 8 +++--- include/gproshan/raytracing/rt_optix.h | 4 +-- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 3 +- shaders/geometry_gradient.glsl | 7 ++--- shaders/vertex.glsl | 5 ++-- shaders/vertex_gradient.glsl | 5 ++-- shaders/vertex_normals.glsl | 7 ++--- shaders/vertex_pointcloud.glsl | 5 ++-- shaders/vertex_sphere.glsl | 5 ++-- src/raytracing/raytracing.cpp | 14 ++++----- src/raytracing/rt_optix.cpp | 7 ++--- src/viewer/che_viewer.cpp | 5 ++-- src/viewer/viewer.cpp | 36 ++++++++++-------------- 14 files changed, 48 insertions(+), 65 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 6266362f..615a8d6b 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -38,16 +38,16 @@ class raytracing virtual void render(glm::vec4 * img, const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const std::vector & light, const bool & flat, const bool & restart = false ); virtual float * raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const index_t & samples = 4 ); diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 2f1aa12d..c98f6223 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -56,8 +56,8 @@ class optix : public raytracing void render(glm::vec4 * img, const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const std::vector & light, const bool & flat, const bool & restart = false diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 0f75d668..a5e0d24d 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -71,7 +71,7 @@ class che_viewer void translate(const vertex & p); void invert_orientation(); - void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat); + void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); void log_info(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 60917c4f..369220d0 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -71,8 +71,7 @@ class viewer quaternion cam_light; std::vector scene_lights; - glm::mat4 view_mat; - glm::mat4 proj_mat; + glm::mat4 proj_view_mat; che_viewer meshes[N_MESHES]; size_t n_meshes = 0; diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index 096ccf34..8dfc6947 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -6,8 +6,7 @@ layout (line_strip, max_vertices = 2) out; in float color[]; in vec3 position[]; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; void main() { @@ -26,10 +25,10 @@ void main() vec3 a = (xi + xj + xk) / 3.0; vec3 b = a + g * 50; - gl_Position = proj_mat * model_view_mat * vec4(a, 1.); + gl_Position = proj_view_mat * vec4(a, 1.); EmitVertex(); - gl_Position = proj_mat * model_view_mat * vec4(b, 1.); + gl_Position = proj_view_mat * vec4(b, 1.); EmitVertex(); EndPrimitive(); diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index a74a31be..77ae9b2e 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -10,8 +10,7 @@ out vec3 vs_normal; out vec3 vs_mesh_color; out float vs_color; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; void main() { @@ -20,6 +19,6 @@ void main() vs_mesh_color = in_mesh_color; vs_color = in_color; - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); + gl_Position = proj_view_mat * vec4(in_position, 1); } diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl index 14a1b5b1..beefd33c 100644 --- a/shaders/vertex_gradient.glsl +++ b/shaders/vertex_gradient.glsl @@ -6,13 +6,12 @@ layout (location=3) in float in_color; out float color; out vec3 position; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; void main() { color = in_color; position = in_position; - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); + gl_Position = proj_view_mat * vec4(in_position, 1.); } diff --git a/shaders/vertex_normals.glsl b/shaders/vertex_normals.glsl index d4de84ed..1d6811c4 100644 --- a/shaders/vertex_normals.glsl +++ b/shaders/vertex_normals.glsl @@ -5,12 +5,11 @@ layout (location=1) in vec3 in_normal; out vec3 normal; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; void main() { - normal = normalize(vec3(proj_mat * model_view_mat * vec4(in_normal, 0.))); - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1.); + normal = normalize(vec3(proj_view_mat * vec4(in_normal, 0.))); + gl_Position = proj_view_mat * vec4(in_position, 1.); } diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index 52f41cfe..a7729685 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -10,8 +10,7 @@ out vec3 vs_normal; out vec3 vs_mesh_color; out float vs_color; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; uniform uint point_size; void main() @@ -21,7 +20,7 @@ void main() vs_mesh_color = in_mesh_color; vs_color = in_color; - gl_Position = proj_mat * model_view_mat * vec4(in_position, 1); + gl_Position = proj_view_mat * vec4(in_position, 1); gl_PointSize = point_size; } diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index 0396a7c6..68f9b9b5 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -7,8 +7,7 @@ layout (location=3) in vec3 in_translation; out vec3 vs_position; out vec3 vs_normal; -uniform mat4 model_view_mat; -uniform mat4 proj_mat; +uniform mat4 proj_view_mat; uniform float scale; void main() @@ -16,6 +15,6 @@ void main() vs_position = scale * in_position + in_translation; vs_normal = in_normal; - gl_Position = proj_mat * model_view_mat * vec4(vs_position, 1); + gl_Position = proj_view_mat * vec4(vs_position, 1); } diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 20b4a69e..55e7a9cb 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -11,8 +11,8 @@ namespace gproshan::rt { void raytracing::render(glm::vec4 * img, const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const std::vector & light, const bool & flat, const bool & restart ) @@ -22,8 +22,7 @@ void raytracing::render(glm::vec4 * img, std::default_random_engine gen; std::uniform_real_distribution randf(0, 1); - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0, 0, 0, 1)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); glm::vec4 li; @@ -53,8 +52,8 @@ void raytracing::render(glm::vec4 * img, } float * raytracing::raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; @@ -62,8 +61,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, std::default_random_engine gen; std::uniform_real_distribution randf(0.f, 1.f); - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); #pragma omp parallel for for(index_t i = 0; i < windows_size.x; ++i) diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index 66c241e7..fad1cd4f 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -120,8 +120,8 @@ optix::~optix() void optix::render( glm::vec4 * img, const glm::uvec2 & windows_size, - const glm::mat4 & view_mat, - const glm::mat4 & proj_mat, + const glm::mat4 & proj_view_mat, + const glm::vec3 & cam_pos, const std::vector & light, const bool & flat, const bool & restart @@ -139,8 +139,7 @@ void optix::render( glm::vec4 * img, cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4)); } - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); render_params.flat = flat; memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index be7e6cf9..f072f1f7 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -244,12 +244,11 @@ void che_viewer::invert_orientation() mesh->normal(v) = -mesh->normal(v); } -void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & view_mat, const glm::mat4 & proj_mat) +void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { if(!pick_vertex) return; - glm::vec3 cam_pos = glm::vec3(glm::inverse(view_mat) * glm::vec4(0.f, 0.f, 0.f, 1.f)); - glm::mat4 inv_proj_view = glm::inverse(proj_mat * view_mat); + glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); glm::vec2 screen = glm::vec2(float(x) / windows_size.x, float(windows_size.y - y) / windows_size.y); glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); glm::vec4 q = inv_proj_view * view; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index eaf834eb..62bd404d 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -93,8 +93,7 @@ bool viewer::run() cam_light = vertex(-1, 1, -2); cam_light = r.conj() * cam_light * r; - view_mat = cam.look_at(r); - proj_mat = glm::perspective(45.f, float(viewport_width) / float(viewport_height), .01f, 1000.f); + proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); switch(render_opt) @@ -782,7 +781,7 @@ bool viewer::raycasting(viewer * view) rt::embree rc({view->active_mesh()}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), - view->view_mat, view->proj_mat + view->proj_view_mat, glm_vec3(view->cam.eye) ); std::thread([](CImg img) @@ -799,28 +798,23 @@ bool viewer::raycasting(viewer * view) void viewer::render_gl() { glProgramUniform3f(shader_sphere, shader_sphere("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_sphere, shader_sphere("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glProgramUniformMatrix4fv(shader_sphere, shader_sphere("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); - glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glProgramUniformMatrix4fv(shader_triangles, shader_triangles("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); + + glProgramUniform3f(shader_sphere, shader_sphere("cam_light"), cam_light[0], cam_light[1], cam_light[2]); + glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_mat"), 1, 0, &proj_mat[0][0]); - glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); - glProgramUniformMatrix4fv(shader_normals, shader_normals("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_mat"), 1, 0, &proj_mat[0][0]); + glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("model_view_mat"), 1, 0, &view_mat[0][0]); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_mat"), 1, 0, &proj_mat[0][0]); + + glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); for(index_t i = 0; i < n_meshes; ++i) @@ -865,7 +859,7 @@ void viewer::render_rt(rt::raytracing * rt) scene_lights = {glm_vec3(cam_light)}; rt->render( img, glm::uvec2(viewport_width, viewport_height), - view_mat, proj_mat, scene_lights, + proj_view_mat, glm_vec3(cam.eye), scene_lights, mesh.render_flat, rt_restart ); @@ -907,7 +901,7 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) float xscale, yscale; glfwGetWindowContentScale(window, &xscale, &yscale); - active_mesh().select(x * xscale, y * yscale, {viewport_width, viewport_height}, view_mat, proj_mat); + active_mesh().select(x * xscale, y * yscale, {viewport_width, viewport_height}, proj_view_mat, glm_vec3(cam.eye)); } From f8b3c0b733dc62f011dbf88e579d3c8a24af478b Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 11 May 2022 10:48:36 +0200 Subject: [PATCH 0624/1018] implement scanner_ptx --- include/gproshan/raytracing/raytracing.h | 12 +++++++--- include/gproshan/scenes/scanner.h | 2 +- src/scenes/scanner.cpp | 30 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 6266362f..c71c98c5 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -54,7 +54,15 @@ class raytracing virtual index_t cast_ray( const glm::vec3 &,// org, const glm::vec3 &// dir ) { return NIL; }; + + virtual float intersect_depth( const glm::vec3 &,// org, + const glm::vec3 &// dir + ) { return 0; }; + virtual std::tuple cast_ray_intersect_depth( const glm::vec3 & origin,// org, + const glm::vec3 & direction// dir + ) { return { cast_ray(origin, direction), intersect_depth( origin, direction) }; }; + protected: virtual glm::vec4 intersect_li( const glm::vec3 &,// org, const glm::vec3 &,// dir, @@ -62,9 +70,7 @@ class raytracing const bool &// flat ) { return glm::vec4(0); }; - virtual float intersect_depth( const glm::vec3 &,// org, - const glm::vec3 &// dir - ) { return 0; }; + }; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index b50ad496..4aca8ff2 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -10,7 +10,7 @@ namespace gproshan::rt { che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); - +che * scanner_ptx(const che * mesh, const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); } // namespace gproshan diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 9f3591c1..533bb85d 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -13,6 +13,36 @@ che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n return new che(ptx.data(), ptx.size(), nullptr, 0); } +che * scanner_ptx(const che * mesh, const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) +{ + std::vector ptx; + //index_t v = rt->cast_ray(cam_pos, glm::normalize(p - cam_pos)); + //cast_ray_intersect_depth + + // use the raytracing to find the hit points + // save them in ptx.data + + glm::vec3 cam_pos = glm::vec3(cam.x, cam.y, cam.z); + glm::vec3 p; + + std::vector vertices; + const real_t r = 1; + index_t v; + float distance; + + const real_t delta_phi = M_PI / n_rows; + const real_t delta_theta = (2 * M_PI) / n_cols; + + for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) + for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta; theta += delta_theta) + { + p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ); + auto [v, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); + } + + return new che(ptx.data(), ptx.size(), nullptr, 0); +} + } // namespace gproshan From 3dac4698fd51170b5560a7892424f77447f557ec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 18:06:15 +0200 Subject: [PATCH 0625/1018] viewer: adding model matrix --- include/gproshan/mesh/che.h | 2 +- include/gproshan/viewer/camera.h | 2 +- include/gproshan/viewer/che_viewer.h | 10 ++-- shaders/geometry_gradient.glsl | 5 +- shaders/vertex.glsl | 5 +- shaders/vertex_gradient.glsl | 3 +- shaders/vertex_normals.glsl | 3 +- shaders/vertex_pointcloud.glsl | 5 +- shaders/vertex_sphere.glsl | 3 +- src/mesh/che.cpp | 2 +- src/raytracing/rt_optix.cpp | 4 +- src/viewer/che_viewer.cpp | 74 +++++++++++++++------------- src/viewer/viewer.cpp | 8 +-- 13 files changed, 71 insertions(+), 55 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 0b57f4cb..78733fb5 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -77,7 +77,7 @@ class che bool is_edge_bound(const index_t & e) const; void flip(const index_t & e); real_t pdetriq(const index_t & t) const; - real_t quality(); + real_t quality() const; real_t area_trig(const index_t & t) const; real_t area_vertex(const index_t & v) const; real_t area_surface() const; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index b9ec27bc..c5973899 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -20,7 +20,7 @@ class camera public: quaternion eye; - quaternion pos = vertex(0, 0, -2); + quaternion pos = vertex(0, 0, -2.5); quaternion front = vertex(0, 0, 1); quaternion up = vertex(0, 1, 0); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index a5e0d24d..89731b0f 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -28,7 +28,7 @@ class che_viewer rt::raytracing * pick_vertex = nullptr; size_t n_instances = 0; - bool normalize = false; + bool center_mesh = false; vertex v_translate; GLuint vao; @@ -39,6 +39,8 @@ class che_viewer real_t factor; std::vector selected; + glm::mat4 model_mat = glm::mat4(1); + index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl index_t point_size = 1; bool point_normals = true; @@ -57,19 +59,19 @@ class che_viewer che *& operator -> (); che *const & operator -> () const; operator che *& (); - void init(che * mesh, const bool & normalize = true); - void reload(); + void init(che * m, const bool & center = true); void update(); void update_vbo(); void update_vbo_geometry(); void update_vbo_normal(const vertex * vnormal = nullptr); void update_vbo_color(const che::rgb_t * vcolor = nullptr); void update_vbo_heatmap(const real_t * vheatmap = nullptr); - void update_instances_translations(const std::vector & translations); + void update_instances_positions(const std::vector & translations); void draw(shader & program); void draw_point_cloud(shader & program); void translate(const vertex & p); + void scale(const real_t & s); void invert_orientation(); void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); diff --git a/shaders/geometry_gradient.glsl b/shaders/geometry_gradient.glsl index 8dfc6947..8360f547 100644 --- a/shaders/geometry_gradient.glsl +++ b/shaders/geometry_gradient.glsl @@ -7,6 +7,7 @@ in float color[]; in vec3 position[]; uniform mat4 proj_view_mat; +uniform mat4 model_mat; void main() { @@ -25,10 +26,10 @@ void main() vec3 a = (xi + xj + xk) / 3.0; vec3 b = a + g * 50; - gl_Position = proj_view_mat * vec4(a, 1.); + gl_Position = proj_view_mat * model_mat * vec4(a, 1.); EmitVertex(); - gl_Position = proj_view_mat * vec4(b, 1.); + gl_Position = proj_view_mat * model_mat * vec4(b, 1.); EmitVertex(); EndPrimitive(); diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 77ae9b2e..6269b7a9 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -11,14 +11,15 @@ out vec3 vs_mesh_color; out float vs_color; uniform mat4 proj_view_mat; +uniform mat4 model_mat; void main() { - vs_position = in_position; + vs_position = vec3(model_mat * vec4(in_position, 1)); vs_normal = in_normal; vs_mesh_color = in_mesh_color; vs_color = in_color; - gl_Position = proj_view_mat * vec4(in_position, 1); + gl_Position = proj_view_mat * vec4(vs_position, 1); } diff --git a/shaders/vertex_gradient.glsl b/shaders/vertex_gradient.glsl index beefd33c..91566f67 100644 --- a/shaders/vertex_gradient.glsl +++ b/shaders/vertex_gradient.glsl @@ -7,11 +7,12 @@ out float color; out vec3 position; uniform mat4 proj_view_mat; +uniform mat4 model_mat; void main() { color = in_color; position = in_position; - gl_Position = proj_view_mat * vec4(in_position, 1.); + gl_Position = proj_view_mat * model_mat * vec4(in_position, 1.); } diff --git a/shaders/vertex_normals.glsl b/shaders/vertex_normals.glsl index 1d6811c4..8842cbf1 100644 --- a/shaders/vertex_normals.glsl +++ b/shaders/vertex_normals.glsl @@ -6,10 +6,11 @@ layout (location=1) in vec3 in_normal; out vec3 normal; uniform mat4 proj_view_mat; +uniform mat4 model_mat; void main() { normal = normalize(vec3(proj_view_mat * vec4(in_normal, 0.))); - gl_Position = proj_view_mat * vec4(in_position, 1.); + gl_Position = proj_view_mat * model_mat * vec4(in_position, 1.); } diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index a7729685..9e83aa3d 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -11,16 +11,17 @@ out vec3 vs_mesh_color; out float vs_color; uniform mat4 proj_view_mat; +uniform mat4 model_mat; uniform uint point_size; void main() { - vs_position = in_position; + vs_position = vec3(model_mat * vec4(in_position, 1)); vs_normal = in_normal; vs_mesh_color = in_mesh_color; vs_color = in_color; - gl_Position = proj_view_mat * vec4(in_position, 1); + gl_Position = proj_view_mat * vec4(vs_position, 1); gl_PointSize = point_size; } diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index 68f9b9b5..deafc692 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -8,6 +8,7 @@ out vec3 vs_position; out vec3 vs_normal; uniform mat4 proj_view_mat; +uniform mat4 model_mat; uniform float scale; void main() @@ -15,6 +16,6 @@ void main() vs_position = scale * in_position + in_translation; vs_normal = in_normal; - gl_Position = proj_view_mat * vec4(vs_position, 1); + gl_Position = proj_view_mat * model_mat * vec4(vs_position, 1); } diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 6a2625eb..f5ed2764 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -229,7 +229,7 @@ real_t che::pdetriq(const index_t & t) const return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } -real_t che::quality() +real_t che::quality() const { real_t q = 0; diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index fad1cd4f..d452da8e 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -283,11 +283,11 @@ void optix::create_pipeline() &optix_pipeline ); - if(sizeof_log > 1) gproshan_error_var(log); + if(sizeof_log > 1) gproshan_log_var(log); optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); - if(sizeof_log > 1) gproshan_error_var(log); + if(sizeof_log > 1) gproshan_log_var(log); } void optix::build_sbt() diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index f072f1f7..b1ea1c77 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include "raytracing/rt_embree.h" @@ -35,50 +37,51 @@ che_viewer::operator che *& () return mesh; } -void che_viewer::init(che * mesh, const bool & normalize) +void che_viewer::init(che * m, const bool & center) { glGenVertexArrays(1, &vao); glGenBuffers(6, vbo); - this->mesh = mesh; - this->normalize = normalize; + mesh = m; + center_mesh = center; update(); } -void che_viewer::reload() -{ - mesh->reload(); - update(); -} - void che_viewer::update() { - if(normalize) mesh->normalize(); + factor = 1; - render_pointcloud = mesh->is_pointcloud(); + if(center_mesh) + { + vertex pmin(INFINITY, INFINITY, INFINITY); + vertex pmax(0, 0, 0); - vertex pmin(INFINITY, INFINITY, INFINITY); - vertex pmax(0, 0, 0); + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + const vertex & p = mesh->gt(v); - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - const vertex & p = mesh->gt(v); + pmin.x = min(pmin.x, p.x); + pmin.y = min(pmin.y, p.y); + pmin.z = min(pmin.z, p.z); - pmin.x = min(pmin.x, p.x); - pmin.y = min(pmin.y, p.y); - pmin.z = min(pmin.z, p.z); + pmax.x = max(pmax.x, p.x); + pmax.y = max(pmax.y, p.y); + pmax.z = max(pmax.z, p.z); + } - pmax.x = max(pmax.x, p.x); - pmax.y = max(pmax.y, p.y); - pmax.z = max(pmax.z, p.z); - } + factor = pmax.x - pmin.x; + factor = std::max(factor, pmax.y - pmin.y); + factor = std::max(factor, pmax.z - pmin.z); + factor = 2.0 / factor; - translate(-(pmax + pmin) / 2); + translate(- factor * (pmax + pmin) / 2); + scale(factor); + } - factor = mesh->mean_edge(); + factor *= mesh->mean_edge(); - mesh->update_normals(); + render_pointcloud = mesh->is_pointcloud(); update_vbo(); @@ -164,7 +167,7 @@ void che_viewer::update_vbo_heatmap(const real_t * vheatmap) glBindVertexArray(0); } -void che_viewer::update_instances_translations(const vector & translations) +void che_viewer::update_instances_positions(const vector & translations) { n_instances = translations.size(); if(!n_instances) return; @@ -185,6 +188,7 @@ void che_viewer::update_instances_translations(const vector & translatio void che_viewer::draw(shader & program) { + glProgramUniformMatrix4fv(program, program("model_mat"), 1, 0, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); glProgramUniform1i(program, program("render_flat"), render_flat); glProgramUniform1i(program, program("render_lines"), render_lines); @@ -210,6 +214,7 @@ void che_viewer::draw(shader & program) void che_viewer::draw_point_cloud(shader & program) { + glProgramUniformMatrix4fv(program, program("model_mat"), 1, 0, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); glProgramUniform1i(program, program("render_lines"), render_lines); glProgramUniform1i(program, program("point_normals"), point_normals); @@ -230,18 +235,19 @@ void che_viewer::draw_point_cloud(shader & program) void che_viewer::translate(const vertex & p) { - v_translate = p; + model_mat = glm::translate(model_mat, glm_vec3(p)); +} - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->get_vertex(v) += v_translate; +void che_viewer::scale(const real_t & s) +{ + model_mat = glm::scale(model_mat, {s, s, s}); } void che_viewer::invert_orientation() { - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->normal(v) = -mesh->normal(v); +// #pragma omp parallel for +// for(index_t v = 0; v < mesh->n_vertices; ++v) +// mesh->normal(v) = -mesh->normal(v); } void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 62bd404d..c79237d1 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -357,6 +357,7 @@ void viewer::add_mesh(che * p_mesh) return; } + p_mesh->update_normals(); meshes[n_meshes].init(p_mesh); meshes[n_meshes].log_info(); ++n_meshes; @@ -529,8 +530,8 @@ bool viewer::menu_reset_mesh(viewer * view) view->other_vertices.clear(); view->vectors.clear(); - view->active_mesh().reload(); - view->active_mesh().update_vbo(); + view->active_mesh()->reload(); + view->active_mesh().update(); return false; } @@ -822,6 +823,7 @@ void viewer::render_gl() che_viewer & mesh = meshes[i]; glViewport(mesh.vx * viewport_width, mesh.vy * viewport_height, viewport_width, viewport_height); + if(mesh->is_pointcloud() || mesh.render_pointcloud) mesh.draw_point_cloud(shader_pointcloud); else @@ -879,7 +881,7 @@ void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) for(index_t i = 0; i < mesh.selected.size(); ++i) sphere_translations[i] = mesh->gt(mesh.selected[i]); - sphere.update_instances_translations(sphere_translations); + sphere.update_instances_positions(sphere_translations); } if(sphere_translations.size()) From 11c8107c190c46aa9040e4ef6421efb0c842462b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 19:49:19 +0200 Subject: [PATCH 0626/1018] viewer: refactor invert normals --- include/gproshan/mesh/che.h | 1 + include/gproshan/viewer/che_viewer.h | 5 ++++- include/gproshan/viewer/viewer.h | 2 +- src/mesh/che.cpp | 7 +++++++ src/viewer/che_viewer.cpp | 7 ------- src/viewer/viewer.cpp | 6 +++--- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 78733fb5..e057403d 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -89,6 +89,7 @@ class che const real_t & heatmap(const index_t & v) const; real_t & heatmap(const index_t & v); void update_normals(); + void invert_normals(); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 89731b0f..03578694 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -56,9 +56,11 @@ class che_viewer public: che_viewer() = default; virtual ~che_viewer(); + che *& operator -> (); che *const & operator -> () const; operator che *& (); + void init(che * m, const bool & center = true); void update(); void update_vbo(); @@ -67,12 +69,13 @@ class che_viewer void update_vbo_color(const che::rgb_t * vcolor = nullptr); void update_vbo_heatmap(const real_t * vheatmap = nullptr); void update_instances_positions(const std::vector & translations); + void draw(shader & program); void draw_point_cloud(shader & program); void translate(const vertex & p); void scale(const real_t & s); - void invert_orientation(); + void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); void log_info(); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 369220d0..41b52c69 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -145,7 +145,7 @@ class viewer static bool set_render_embree(viewer * view); static bool set_render_optix(viewer * view); - static bool invert_orientation(viewer * view); + static bool invert_normals(viewer * view); static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); static bool set_render_triangles(viewer * view); diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index f5ed2764..65f6749f 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -336,6 +336,13 @@ void che::update_normals() } } +void che::invert_normals() +{ + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + VN[v] = - VN[v]; +} + const vertex & che::normal(const index_t & v) const { assert(VN && v < n_vertices); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index b1ea1c77..fc01a6d8 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -243,13 +243,6 @@ void che_viewer::scale(const real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void che_viewer::invert_orientation() -{ -// #pragma omp parallel for -// for(index_t v = 0; v < mesh->n_vertices; ++v) -// mesh->normal(v) = -mesh->normal(v); -} - void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { if(!pick_vertex) return; diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c79237d1..2077b99b 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -310,7 +310,7 @@ void viewer::init_menus() add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", menu_reset_mesh); add_process(GLFW_KEY_TAB, "TAB", "Render Flat", set_render_flat); add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", set_render_lines); - add_process(GLFW_KEY_F2, "F2", "Invert Orientation", invert_orientation); + add_process(GLFW_KEY_F2, "F2", "Invert Normals", invert_normals); add_process(GLFW_KEY_F3, "F3", "Gradient Field", set_render_gradients); add_process(GLFW_KEY_F4, "F4", "Normal Field", set_render_normals); add_process(GLFW_KEY_APOSTROPHE, "APOSTROPHE", "Select Border Vertices", set_render_border); @@ -700,11 +700,11 @@ bool viewer::set_render_optix(viewer * view) return false; } -bool viewer::invert_orientation(viewer * view) +bool viewer::invert_normals(viewer * view) { che_viewer & mesh = view->active_mesh(); - mesh.invert_orientation(); + mesh->invert_normals(); mesh.update_vbo_normal(); return false; From f5b21c4859ab9bdda0a25dd6e40f1062a4c030ac Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 20:21:12 +0200 Subject: [PATCH 0627/1018] rt_embree: adding model matrix --- include/gproshan/raytracing/rt_embree.h | 12 +++++++++--- src/raytracing/rt_embree.cpp | 26 ++++++++++++++++--------- src/viewer/che_viewer.cpp | 2 +- src/viewer/viewer.cpp | 6 ++++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 8b69289a..fd364d9d 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -41,7 +41,11 @@ class embree : public raytracing public: embree(); - embree(const std::vector & meshes, const bool & pointcloud = false, const float & pcr = 1); + embree( const std::vector & meshes, + const std::vector & model_mats, + const bool & pointcloud = false, + const float & pcr = 1 + ); virtual ~embree(); virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); @@ -50,9 +54,11 @@ class embree : public raytracing bool intersect(ray_hit & r); bool occluded(ray_hit & r); - void build_bvh(const std::vector & meshes, const bool & pointcloud = false); + void build_bvh( const std::vector & meshes, + const std::vector & model_mats, + const bool & pointcloud = false); index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh); + index_t add_mesh(const che * mesh, const glm::mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh); virtual float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index e8108933..f7e352cb 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -105,10 +105,10 @@ embree::embree() rtcSetDeviceErrorFunction(device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const bool & pointcloud, const float & pcr): embree() +embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float & pcr): embree() { pc_radius = pcr; - build_bvh(meshes, pointcloud); + build_bvh(meshes, model_mats, pointcloud); } embree::~embree() @@ -135,13 +135,18 @@ bool embree::occluded(ray_hit & r) return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } -void embree::build_bvh(const std::vector & meshes, const bool & pointcloud) +void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { - for(auto & m: meshes) - if(!m->n_faces || pointcloud) - geomID_mesh[add_pointcloud(m)] = {m, true}; + for(index_t i = 0; i < meshes.size(); ++i) + { + che * mesh = meshes[i]; + const glm::mat4 & model_mat = model_mats[i]; + + if(mesh->is_pointcloud() || pointcloud) + geomID_mesh[add_pointcloud(mesh)] = {mesh, true}; else - geomID_mesh[add_mesh(m)] = {m, false}; + geomID_mesh[add_mesh(mesh, model_mat)] = {mesh, false}; + } rtcCommitScene(scene); } @@ -163,7 +168,7 @@ index_t embree::add_sphere(const glm::vec4 & xyzr) return geom_id; } -index_t embree::add_mesh(const che * mesh) +index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_mat) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); @@ -179,7 +184,10 @@ index_t embree::add_mesh(const che * mesh) mesh->n_faces ); - copy_real_t_array((float *) vertices, (real_t *) &mesh->gt(0), 3 * mesh->n_vertices); + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + vertices[i] = glm::vec3(model_mat * glm::vec4(glm_vec3(mesh->gt(i)), 1)); + memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index fc01a6d8..6e984c1d 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -86,7 +86,7 @@ void che_viewer::update() update_vbo(); delete pick_vertex; - pick_vertex = new rt::embree({mesh}); + pick_vertex = new rt::embree({mesh}, {model_mat}); } void che_viewer::update_vbo() diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2077b99b..c386ae78 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -660,7 +660,7 @@ bool viewer::setup_raytracing(viewer * view) case R_EMBREE: delete view->rt_embree; TIC(time); - view->rt_embree = new rt::embree({mesh}, mesh.render_pointcloud, pc_radius); + view->rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); break; @@ -779,7 +779,9 @@ bool viewer::set_render_flat(viewer * view) bool viewer::raycasting(viewer * view) { - rt::embree rc({view->active_mesh()}); + che_viewer & mesh = view->active_mesh(); + + rt::embree rc({mesh}, {mesh.model_mat}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), view->proj_view_mat, glm_vec3(view->cam.eye) From 1be8dfdd7812b839c492fd426e404ebc0d772c4a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Wed, 11 May 2022 21:05:46 +0200 Subject: [PATCH 0628/1018] finish scanner implementation --- include/gproshan/scenes/scanner.h | 3 ++- src/app_viewer.cpp | 2 +- src/scenes/scanner.cpp | 27 ++++++++++++++++++++------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 4aca8ff2..39dfc0f3 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -10,7 +10,8 @@ namespace gproshan::rt { che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); -che * scanner_ptx(const che * mesh, const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); + } // namespace gproshan diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 1886aa7f..a729f10f 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -130,7 +130,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) if(ImGui::Button("Scan")) { - view->add_mesh(scanner_ptx(view->rt_embree, n_rows, n_cols, {0, 0, 0})); + view->add_mesh(scanner_ptx(mesh, view->rt_embree, n_rows, n_cols, {0, 0, 0})); } return true; diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 533bb85d..e9bd39e3 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -13,22 +13,25 @@ che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n return new che(ptx.data(), ptx.size(), nullptr, 0); } -che * scanner_ptx(const che * mesh, const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) { std::vector ptx; + ptx.reserve(n_cols * n_rows); //index_t v = rt->cast_ray(cam_pos, glm::normalize(p - cam_pos)); //cast_ray_intersect_depth // use the raytracing to find the hit points // save them in ptx.data - glm::vec3 cam_pos = glm::vec3(cam.x, cam.y, cam.z); - glm::vec3 p; + glm::vec3 cam_pos = glm_vec3(cam); + glm::vec3 p, n_v; std::vector vertices; const real_t r = 1; - index_t v; + index_t v_idx; float distance; + + gproshan_log("init"); const real_t delta_phi = M_PI / n_rows; const real_t delta_theta = (2 * M_PI) / n_cols; @@ -36,9 +39,19 @@ che * scanner_ptx(const che * mesh, const raytracing * rt, const size_t & n_rows for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta; theta += delta_theta) { - p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ); - auto [v, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); - } + gproshan_log_var(phi); + gproshan_log_var(theta); + // p is the direction of the ray + p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; + gproshan_log("quering"); + // quering the closest point in the mesh to the hit point and the distance + auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); + gproshan_log("passed"); + // new vertex position + n_v = cam_pos + p * distance; + + ptx.push_back( vertex(n_v.x, n_v.y, n_v.z) ); + } return new che(ptx.data(), ptx.size(), nullptr, 0); } From 5ee72536b358a1aa5c90eaca120a27dba3ffa934 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 22:03:42 +0200 Subject: [PATCH 0629/1018] viewer: fixing pick vertex mouse interaction --- include/gproshan/raytracing/raytracing.h | 10 ++++++ include/gproshan/viewer/che_viewer.h | 1 - shaders/vertex_sphere.glsl | 4 +-- src/raytracing/raytracing.cpp | 44 +++++++++--------------- src/viewer/che_viewer.cpp | 25 +++----------- src/viewer/viewer.cpp | 7 ++-- 6 files changed, 39 insertions(+), 52 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 615a8d6b..49ca6457 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -5,6 +5,7 @@ #include #include +#include #include @@ -32,6 +33,9 @@ class raytracing size_t n_samples = 0; + static std::default_random_engine gen; + static std::uniform_real_distribution randf; + public: raytracing() = default; virtual ~raytracing() = default; @@ -51,6 +55,12 @@ class raytracing const index_t & samples = 4 ); + glm::vec3 ray_view_dir( const real_t & x, const real_t & y, + const glm::vec2 & windows_size, + const glm::mat4 & inv_proj_view, + const glm::vec3 & cam_pos + ); + virtual index_t cast_ray( const glm::vec3 &,// org, const glm::vec3 &// dir ) { return NIL; }; diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 03578694..2198126b 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -36,7 +36,6 @@ class che_viewer public: int vx, vy; ///< viewport positions. - real_t factor; std::vector selected; glm::mat4 model_mat = glm::mat4(1); diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index deafc692..b2b5a7ce 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -13,9 +13,9 @@ uniform float scale; void main() { - vs_position = scale * in_position + in_translation; + vs_position = in_position + in_translation; vs_normal = in_normal; - gl_Position = proj_view_mat * model_mat * vec4(vs_position, 1); + gl_Position = scale * proj_view_mat * model_mat * vec4(vs_position, 1); } diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 55e7a9cb..39cfaa08 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -1,7 +1,6 @@ #include "raytracing/raytracing.h" #include -#include // geometry processing and shape analysis framework @@ -9,6 +8,9 @@ namespace gproshan::rt { +std::default_random_engine raytracing::gen; +std::uniform_real_distribution raytracing::randf; + void raytracing::render(glm::vec4 * img, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, @@ -19,9 +21,6 @@ void raytracing::render(glm::vec4 * img, { if(restart) n_samples = 0; - std::default_random_engine gen; - std::uniform_real_distribution randf(0, 1); - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); glm::vec4 li; @@ -32,18 +31,11 @@ void raytracing::render(glm::vec4 * img, { //row major glm::vec4 & color = img[j * windows_size.x + i]; - - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y - ); - - glm::vec4 view = glm::vec4(screen.x * 2 - 1, screen.y * 2 - 1, 1, 1); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); + glm::vec3 dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); li = glm::vec4(0); for(auto & l: light) - li += intersect_li(cam_pos, glm::normalize(p - cam_pos), l, flat); + li += intersect_li(cam_pos, dir, l, flat); color = (color * float(n_samples) + li / float(light.size())) / float(n_samples + 1); } @@ -58,9 +50,6 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, { float * frame = new float[windows_size.x * windows_size.y]; - std::default_random_engine gen; - std::uniform_real_distribution randf(0.f, 1.f); - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); #pragma omp parallel for @@ -69,19 +58,10 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; + glm::vec3 dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); for(index_t s = 0; s < samples; ++s) - { - glm::vec2 screen = glm::vec2( (float(i) + randf(gen)) / windows_size.x, - (float(j) + randf(gen)) / windows_size.y - ); - - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); - - color += intersect_depth(cam_pos, glm::normalize(p - cam_pos)); - } + color += intersect_depth(cam_pos, dir); color /= samples; } @@ -89,6 +69,16 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, return frame; } +glm::vec3 raytracing::ray_view_dir(const real_t & x, const real_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const glm::vec3 & cam_pos) +{ + glm::vec2 screen = glm::vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); + glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); + glm::vec4 q = inv_proj_view * view; + glm::vec3 p = glm::vec3(q * (1.f / q.w)); + + return glm::normalize(p - cam_pos); +} + } // namespace gproshan diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 6e984c1d..1081ef1c 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -50,8 +51,6 @@ void che_viewer::init(che * m, const bool & center) void che_viewer::update() { - factor = 1; - if(center_mesh) { vertex pmin(INFINITY, INFINITY, INFINITY); @@ -70,17 +69,10 @@ void che_viewer::update() pmax.z = max(pmax.z, p.z); } - factor = pmax.x - pmin.x; - factor = std::max(factor, pmax.y - pmin.y); - factor = std::max(factor, pmax.z - pmin.z); - factor = 2.0 / factor; - - translate(- factor * (pmax + pmin) / 2); - scale(factor); + scale(2.0 / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z})); + translate(- (pmax + pmin) / 2); } - factor *= mesh->mean_edge(); - render_pointcloud = mesh->is_pointcloud(); update_vbo(); @@ -245,15 +237,8 @@ void che_viewer::scale(const real_t & s) void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { - if(!pick_vertex) return; - - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); - glm::vec2 screen = glm::vec2(float(x) / windows_size.x, float(windows_size.y - y) / windows_size.y); - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); - - index_t v = pick_vertex->cast_ray(cam_pos, glm::normalize(p - cam_pos)); + glm::vec3 dir = pick_vertex->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); + index_t v = pick_vertex->cast_ray(cam_pos, dir); if(v != NIL) selected.push_back(v); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index c386ae78..658cc31c 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -60,7 +60,9 @@ viewer::viewer(const int & width, const int & height): window_width(width), wind info_gl(); gproshan_log_var(sizeof(real_t)); - sphere.init(new che_sphere(0.01), false); + che * s = new che_sphere(0.01); + s->update_normals(); + sphere.init(s, false); } viewer::~viewer() @@ -817,7 +819,7 @@ void viewer::render_gl() glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); - glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); + glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom() * 0.02); for(index_t i = 0; i < n_meshes; ++i) @@ -886,6 +888,7 @@ void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) sphere.update_instances_positions(sphere_translations); } + sphere.model_mat = mesh.model_mat; if(sphere_translations.size()) sphere.draw(program); } From 3b7f04442216f4af9d63cf6b2c4b675920923cfe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 23:02:37 +0200 Subject: [PATCH 0630/1018] viewer: fixing pick vertex mouse interaction --- shaders/vertex_sphere.glsl | 4 ++-- src/viewer/viewer.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shaders/vertex_sphere.glsl b/shaders/vertex_sphere.glsl index b2b5a7ce..44eff112 100644 --- a/shaders/vertex_sphere.glsl +++ b/shaders/vertex_sphere.glsl @@ -13,9 +13,9 @@ uniform float scale; void main() { - vs_position = in_position + in_translation; + vs_position = scale * in_position + vec3(model_mat * vec4(in_translation, 1)); vs_normal = in_normal; - gl_Position = scale * proj_view_mat * model_mat * vec4(vs_position, 1); + gl_Position = proj_view_mat * vec4(vs_position, 1); } diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 658cc31c..2f753e37 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -819,7 +819,7 @@ void viewer::render_gl() glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); - glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom() * 0.02); + glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); for(index_t i = 0; i < n_meshes; ++i) From 081225f92cab539b7eb101d3ad6d3a331fa8ee21 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 May 2022 23:26:19 +0200 Subject: [PATCH 0631/1018] viewer: fixing pick vertex multi viewport --- include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/viewer/che_viewer.h | 2 +- src/raytracing/raytracing.cpp | 2 +- src/viewer/che_viewer.cpp | 2 +- src/viewer/viewer.cpp | 8 +++++++- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 49ca6457..70fd3a42 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -55,7 +55,7 @@ class raytracing const index_t & samples = 4 ); - glm::vec3 ray_view_dir( const real_t & x, const real_t & y, + glm::vec3 ray_view_dir( const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const glm::vec3 & cam_pos diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 2198126b..69a12bdb 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -75,7 +75,7 @@ class che_viewer void translate(const vertex & p); void scale(const real_t & s); - void select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); + void select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); void log_info(); }; diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index 39cfaa08..d4044fa7 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -69,7 +69,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, return frame; } -glm::vec3 raytracing::ray_view_dir(const real_t & x, const real_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const glm::vec3 & cam_pos) +glm::vec3 raytracing::ray_view_dir(const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const glm::vec3 & cam_pos) { glm::vec2 screen = glm::vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 1081ef1c..12b00f31 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -235,7 +235,7 @@ void che_viewer::scale(const real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void che_viewer::select(const real_t & x, const real_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) +void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { glm::vec3 dir = pick_vertex->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); index_t v = pick_vertex->cast_ray(cam_pos, dir); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 2f753e37..7bc6f838 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -908,7 +908,13 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) float xscale, yscale; glfwGetWindowContentScale(window, &xscale, &yscale); - active_mesh().select(x * xscale, y * yscale, {viewport_width, viewport_height}, proj_view_mat, glm_vec3(cam.eye)); + index_t ix = x * xscale; + index_t iy = y * yscale; + const int & cols = m_window_size[n_meshes][1]; + + che_viewer & mesh = meshes[cols * (iy / viewport_height) + ix / viewport_width]; + + mesh.select(ix % viewport_width, iy % viewport_height, {viewport_width, viewport_height}, proj_view_mat, glm_vec3(cam.eye)); } From e2f6621175a795691d19a32ce07e5e64d2d7fcdf Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 12 May 2022 16:06:57 +0200 Subject: [PATCH 0632/1018] scanner: adding color to ptx to do: fix the sizes --- include/gproshan/scenes/scanner.h | 2 ++ src/scenes/scanner.cpp | 53 +++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 39dfc0f3..f7ea06be 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -3,6 +3,8 @@ #include "raytracing/raytracing.h" +#include "mesh/che.h" + // geometry processing and shape analysis framework // raytracing approach diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index e9bd39e3..3635c6b6 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -1,4 +1,8 @@ #include "scenes/scanner.h" +#include +#include + +using namespace cimg_library; // geometry processing and shape analysis framework @@ -15,18 +19,14 @@ che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) { - std::vector ptx; - ptx.reserve(n_cols * n_rows); - //index_t v = rt->cast_ray(cam_pos, glm::normalize(p - cam_pos)); - //cast_ray_intersect_depth - - // use the raytracing to find the hit points - // save them in ptx.data + std::vector vertices; + std::vector vertices_color; + vertices.reserve(n_cols * n_rows); + vertices_color.reserve(n_cols * n_rows); glm::vec3 cam_pos = glm_vec3(cam); glm::vec3 p, n_v; - std::vector vertices; const real_t r = 1; index_t v_idx; float distance; @@ -39,21 +39,42 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta; theta += delta_theta) { - gproshan_log_var(phi); - gproshan_log_var(theta); + //gproshan_log_var(phi); + //gproshan_log_var(theta); // p is the direction of the ray p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; - gproshan_log("quering"); // quering the closest point in the mesh to the hit point and the distance auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); - gproshan_log("passed"); - // new vertex position - n_v = cam_pos + p * distance; - ptx.push_back( vertex(n_v.x, n_v.y, n_v.z) ); + + if(v_idx == NIL) + { + vertices.push_back( {0, 0, 0} ); + vertices_color.push_back({0,0,0}); + } + else + { + n_v = cam_pos + p * distance; + vertices.push_back( vertex(n_v.x, n_v.y, n_v.z) ); + vertices_color.push_back( mesh->rgb(v_idx) ); + } } - return new che(ptx.data(), ptx.size(), nullptr, 0); + gproshan_log_var(n_cols * n_cols == vertices.size()); + + che * mesh_ptx = new che(vertices.data(), vertices.size(), nullptr, 0); + memcpy(&mesh_ptx->rgb(0), vertices_color.data(), vertices_color.size() * sizeof(che::rgb_t)); + + + CImg img((unsigned char *) vertices_color.data(), 3, n_cols, n_rows); + img.permute_axes("zycx"); + //img.save((mesh->name() + ".jpg").c_str()); + + std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); + + + return mesh_ptx; + //return new che(ptx.data(), ptx.size(), nullptr, 0); } From 4b158a730488dfe56c9990603dce29be6ba5d870 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Fri, 13 May 2022 20:49:44 +0200 Subject: [PATCH 0633/1018] fixing image size to do: save the image --- src/scenes/scanner.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 3635c6b6..4cfac63e 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -33,11 +33,17 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons gproshan_log("init"); - const real_t delta_phi = M_PI / n_rows; - const real_t delta_theta = (2 * M_PI) / n_cols; + const real_t delta_phi = (2 * M_PI) / n_rows; + const real_t delta_theta = M_PI / n_cols; + /*gproshan_log_var(n_rows); + gproshan_log_var(n_cols); + gproshan_log_var(delta_phi); + gproshan_log_var(delta_theta); + gproshan_log_var( (2 * M_PI - 0.5 * delta_phi)/ delta_phi); + gproshan_log_var((M_PI - 0.5 * delta_theta)/ delta_theta);*/ for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) - for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta; theta += delta_theta) + for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta + delta_theta; theta += delta_theta) { //gproshan_log_var(phi); //gproshan_log_var(theta); @@ -46,7 +52,6 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons // quering the closest point in the mesh to the hit point and the distance auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); - if(v_idx == NIL) { vertices.push_back( {0, 0, 0} ); @@ -60,7 +65,10 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons } } - gproshan_log_var(n_cols * n_cols == vertices.size()); + gproshan_log_var(n_cols * n_rows); + gproshan_log_var(vertices.size()); + + gproshan_log_var(n_cols * n_rows == vertices.size()); che * mesh_ptx = new che(vertices.data(), vertices.size(), nullptr, 0); memcpy(&mesh_ptx->rgb(0), vertices_color.data(), vertices_color.size() * sizeof(che::rgb_t)); From 21acc0fb664e8e9ca89b0fe491eea0175949839a Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Sun, 15 May 2022 13:47:54 +0200 Subject: [PATCH 0634/1018] scanner: save image and add saving ptx function --- include/gproshan/mesh/che_ptx.h | 3 +++ include/gproshan/viewer/che_viewer.h | 2 +- src/app_viewer.cpp | 9 ++++--- src/mesh/che_ptx.cpp | 39 ++++++++++++++++++++++++++++ src/scenes/scanner.cpp | 20 +++++++------- 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/include/gproshan/mesh/che_ptx.h b/include/gproshan/mesh/che_ptx.h index 1c0cd59a..656c42f4 100644 --- a/include/gproshan/mesh/che_ptx.h +++ b/include/gproshan/mesh/che_ptx.h @@ -13,6 +13,9 @@ class che_ptx : public che public: che_ptx(const std::string & file); + static void write_file(const che * mesh, const std::string & file, const size_t & n_rows, const size_t & n_cols); + + private: void read_file(const std::string & file); }; diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 0f75d668..ba32bca0 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -25,7 +25,6 @@ class che_viewer { protected: che * mesh = nullptr; - rt::raytracing * pick_vertex = nullptr; size_t n_instances = 0; bool normalize = false; @@ -38,6 +37,7 @@ class che_viewer int vx, vy; ///< viewport positions. real_t factor; std::vector selected; + rt::raytracing * pick_vertex = nullptr; index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl index_t point_size = 1; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index a729f10f..b2f7004c 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -120,8 +120,8 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static size_t n_rows = 2000; - static size_t n_cols = 4000; + static size_t n_rows = 4000; + static size_t n_cols = 2000; static const size_t n_min = 1000; static const size_t n_max = 1000000; @@ -130,9 +130,12 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) if(ImGui::Button("Scan")) { - view->add_mesh(scanner_ptx(mesh, view->rt_embree, n_rows, n_cols, {0, 0, 0})); + che * ptx_mesh = scanner_ptx(mesh, mesh.pick_vertex, n_rows, n_cols, {0, 0, 0}); + view->add_mesh(ptx_mesh); + che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); } + return true; } diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 3974142c..e1add806 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -121,6 +121,45 @@ void che_ptx::read_file(const string & file) rw(n_faces) = he / che::mtrig; } +void che_ptx::write_file(const che * mesh, const std::string & file, const size_t & n_rows, const size_t & n_cols) +{ + FILE * fp = fopen((file + ".ptx").c_str(), "wb"); + assert(fp); + + const char * type = sizeof(real_t) == 4 ? "float" : "double"; +/* + +1544 nrows +2064 ncols +0 0 0 +1 0 0 +0 1 0 +0 0 1 +1 0 0 0 +0 1 0 0 +0 0 1 0 +0 0 0 1 +*/ + + fprintf(fp, "%lu \n %lu\n", n_rows, n_cols); + fprintf(fp, "0 0 0\n"); + fprintf(fp, "1 0 0\n"); + fprintf(fp, "0 1 0\n"); + fprintf(fp, "0 0 1\n"); + fprintf(fp, "1 0 0 0\n"); + fprintf(fp, "0 1 0 0\n"); + fprintf(fp, "0 0 1 0\n"); + fprintf(fp, "0 0 0 1\n"); + + for(size_t i = 0; i < mesh->n_vertices; ++i) + { + const vertex & v = mesh->gt(i); + const rgb_t & c = mesh->rgb(i); + + fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x, (float) v.y, (float) v.z, (float) 0, c.r, c.g, c.b ); + + } +} } // namespace gproshan diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 4cfac63e..8855ff84 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -30,23 +30,18 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons const real_t r = 1; index_t v_idx; float distance; + gproshan_log("init"); const real_t delta_phi = (2 * M_PI) / n_rows; const real_t delta_theta = M_PI / n_cols; - /*gproshan_log_var(n_rows); - gproshan_log_var(n_cols); - gproshan_log_var(delta_phi); - gproshan_log_var(delta_theta); - gproshan_log_var( (2 * M_PI - 0.5 * delta_phi)/ delta_phi); - gproshan_log_var((M_PI - 0.5 * delta_theta)/ delta_theta);*/ + for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta + delta_theta; theta += delta_theta) + //for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) { - //gproshan_log_var(phi); - //gproshan_log_var(theta); // p is the direction of the ray p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; // quering the closest point in the mesh to the hit point and the distance @@ -76,9 +71,14 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons CImg img((unsigned char *) vertices_color.data(), 3, n_cols, n_rows); img.permute_axes("zycx"); - //img.save((mesh->name() + ".jpg").c_str()); + img.save((mesh->name() + ".jpg").c_str()); + + CImg img2((unsigned char *) vertices_color.data(), 3, n_rows, n_cols); + img2.permute_axes("zycx"); + img2.save((mesh->name() + "2.jpg").c_str()); + - std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); + std::thread([](CImg img) { img.display(); }, img).detach(); return mesh_ptx; From ea5da48f9e0b377edff45e53e78d14e54c290485 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 May 2022 00:07:44 +0200 Subject: [PATCH 0635/1018] viewer: refactoring select vertices --- include/gproshan/viewer/che_viewer.h | 1 - include/gproshan/viewer/viewer.h | 5 ++- src/app_viewer.cpp | 4 +-- src/laplacian/laplacian.cpp | 5 +-- src/viewer/che_viewer.cpp | 1 + src/viewer/viewer.cpp | 54 ++++++++++++++-------------- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 69a12bdb..f364cac6 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -48,7 +48,6 @@ class che_viewer bool render_triangles = false; bool render_gradients = false; bool render_normals = false; - bool render_border = false; bool render_lines = false; bool render_flat = false; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 41b52c69..b13d0366 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -146,21 +146,20 @@ class viewer static bool set_render_optix(viewer * view); static bool invert_normals(viewer * view); + static bool select_border_vertices(viewer * view); + static bool clean_selected_vertices(viewer * view); static bool set_render_pointcloud(viewer * view); static bool set_render_wireframe(viewer * view); static bool set_render_triangles(viewer * view); static bool set_render_gradients(viewer * view); static bool set_render_normals(viewer * view); - static bool set_render_border(viewer * view); static bool set_render_lines(viewer * view); static bool set_render_flat(viewer * view); static bool raycasting(viewer * view); - // draw routines void draw_selected_vertices(shader & program, const che_viewer & mesh); - void select_border_vertices(che_viewer & mesh); void pick_vertex(const real_t & x, const real_t & y); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 1886aa7f..6d28a430 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -63,7 +63,7 @@ void app_viewer::init() sub_menus.push_back("Geometry"); add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); - add_process(GLFW_KEY_C, "C", "Connected Components", process_connected_components); + add_process(GLFW_KEY_O, "O", "Connected Components", process_connected_components); add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); add_process(GLFW_KEY_Q, "Q", "Edge Collapse", process_edge_collapse); add_process(GLFW_KEY_M, "M", "Multiplicate", process_multiplicate_vertices); @@ -103,7 +103,7 @@ void app_viewer::init() add_process(GLFW_KEY_SEMICOLON, "SEMICOLON", "Select multiple vertices", process_select_multiple); add_process(GLFW_KEY_SLASH, "SLASH", "Threshold", process_threshold); add_process(GLFW_KEY_N, "N", "Noise", process_noise); - add_process(GLFW_KEY_B, "B", "Black noise", process_black_noise); + add_process(GLFW_KEY_P, "P", "Black noise", process_black_noise); } diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 9cc9d1bb..9836b2c1 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -48,7 +48,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - A(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); + A(v, v) = mesh->area_vertex(v); } void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) @@ -77,7 +77,8 @@ void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) A.reserve(VectorXi::Constant(n_vertices, 1)); for(index_t v = 0; v < n_vertices; ++v) - A.insert(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); + A.insert(v, v) = mesh->area_vertex(v); + //A.insert(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); } size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 12b00f31..a7869375 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -51,6 +51,7 @@ void che_viewer::init(che * m, const bool & center) void che_viewer::update() { + model_mat = glm::mat4(1); if(center_mesh) { vertex pmin(INFINITY, INFINITY, INFINITY); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 7bc6f838..6ae78f29 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -310,13 +310,14 @@ void viewer::init_menus() sub_menus.push_back("Mesh"); add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", menu_reset_mesh); + add_process(GLFW_KEY_W, "W", "Save Mesh", menu_save_mesh); add_process(GLFW_KEY_TAB, "TAB", "Render Flat", set_render_flat); add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", set_render_lines); add_process(GLFW_KEY_F2, "F2", "Invert Normals", invert_normals); add_process(GLFW_KEY_F3, "F3", "Gradient Field", set_render_gradients); add_process(GLFW_KEY_F4, "F4", "Normal Field", set_render_normals); - add_process(GLFW_KEY_APOSTROPHE, "APOSTROPHE", "Select Border Vertices", set_render_border); - add_process(GLFW_KEY_W, "W", "Save Mesh", menu_save_mesh); + add_process(GLFW_KEY_B, "B", "Select Border Vertices", select_border_vertices); + add_process(GLFW_KEY_C, "C", "Clean Selected Vertices", clean_selected_vertices); } void viewer::init_glsl() @@ -528,12 +529,14 @@ bool viewer::menu_save_load_view(viewer * view) bool viewer::menu_reset_mesh(viewer * view) { - view->active_mesh().selected.clear(); view->other_vertices.clear(); view->vectors.clear(); - view->active_mesh()->reload(); - view->active_mesh().update(); + che_viewer & mesh = view->active_mesh(); + mesh.selected.clear(); + mesh->reload(); + mesh->update_normals(); + mesh.update(); return false; } @@ -712,6 +715,24 @@ bool viewer::invert_normals(viewer * view) return false; } +bool viewer::select_border_vertices(viewer * view) +{ + che_viewer & mesh = view->active_mesh(); + for(const index_t & b: mesh->bounds()) + for_boundary(he, mesh, b) + mesh.selected.push_back(mesh->vt(he)); + + return false; +} + +bool viewer::clean_selected_vertices(viewer * view) +{ + che_viewer & mesh = view->active_mesh(); + mesh.selected.clear(); + + return false; +} + bool viewer::set_render_pointcloud(viewer * view) { che_viewer & mesh = view->active_mesh(); @@ -752,16 +773,6 @@ bool viewer::set_render_normals(viewer * view) return false; } -bool viewer::set_render_border(viewer * view) -{ - che_viewer & mesh = view->active_mesh(); - mesh.render_border = !mesh.render_border; - if(!mesh.render_border) - mesh.selected.clear(); - - return false; -} - bool viewer::set_render_lines(viewer * view) { che_viewer & mesh = view->active_mesh(); @@ -839,9 +850,6 @@ void viewer::render_gl() if(mesh.render_gradients) mesh.draw(shader_gradient); - if(mesh.render_border) - select_border_vertices(mesh); - draw_selected_vertices(shader_sphere, mesh); } } @@ -893,16 +901,6 @@ void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) sphere.draw(program); } -void viewer::select_border_vertices(che_viewer & mesh) -{ - mesh.selected.clear(); - - vector bounds = mesh->bounds(); - for(const index_t & b: bounds) - for_boundary(he, mesh, b) - mesh.selected.push_back(mesh->vt(he)); -} - void viewer::pick_vertex(const real_t & x, const real_t & y) { float xscale, yscale; From 72a8efc224ae93fc3b00d58fc0ee244baed1cf39 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 May 2022 02:01:45 +0200 Subject: [PATCH 0636/1018] viewer: updating draw selected vertices --- include/gproshan/viewer/viewer.h | 58 +++++++------- src/app_viewer.cpp | 9 +-- src/geodesics/test_geodesics_ptp.cpp | 2 +- src/viewer/viewer.cpp | 112 +++++++++++++++------------ 4 files changed, 94 insertions(+), 87 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index b13d0366..9ddb7baf 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -129,37 +129,37 @@ class viewer static void cursor_callback(GLFWwindow * window, double x, double y); static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); - static bool menu_help(viewer * view); - static bool menu_save_load_view(viewer * view); - static bool menu_reset_mesh(viewer * view); - static bool menu_save_mesh(viewer * view); - static bool menu_zoom_in(viewer * view); - static bool menu_zoom_out(viewer * view); - static bool menu_bgc_inc(viewer * view); - static bool menu_bgc_dec(viewer * view); - static bool menu_bgc_white(viewer * view); - static bool menu_bgc_black(viewer * view); - - static bool setup_raytracing(viewer * view); - static bool set_render_gl(viewer * view); - static bool set_render_embree(viewer * view); - static bool set_render_optix(viewer * view); - - static bool invert_normals(viewer * view); - static bool select_border_vertices(viewer * view); - static bool clean_selected_vertices(viewer * view); - static bool set_render_pointcloud(viewer * view); - static bool set_render_wireframe(viewer * view); - static bool set_render_triangles(viewer * view); - static bool set_render_gradients(viewer * view); - static bool set_render_normals(viewer * view); - static bool set_render_lines(viewer * view); - static bool set_render_flat(viewer * view); - - static bool raycasting(viewer * view); + static bool m_help(viewer * view); + static bool m_save_load_view(viewer * view); + static bool m_reset_mesh(viewer * view); + static bool m_save_mesh(viewer * view); + static bool m_normalize_mesh(viewer * view); + static bool m_zoom_in(viewer * view); + static bool m_zoom_out(viewer * view); + static bool m_bgc_inc(viewer * view); + static bool m_bgc_dec(viewer * view); + static bool m_bgc_white(viewer * view); + static bool m_bgc_black(viewer * view); + + static bool m_setup_raytracing(viewer * view); + static bool m_render_gl(viewer * view); + static bool m_render_embree(viewer * view); + static bool m_render_optix(viewer * view); + + static bool m_invert_normals(viewer * view); + static bool m_select_border_vertices(viewer * view); + static bool m_clean_selected_vertices(viewer * view); + static bool m_render_pointcloud(viewer * view); + static bool m_render_wireframe(viewer * view); + static bool m_render_triangles(viewer * view); + static bool m_render_gradients(viewer * view); + static bool m_render_normals(viewer * view); + static bool m_render_lines(viewer * view); + static bool m_render_flat(viewer * view); + + static bool m_raycasting(viewer * view); void draw_selected_vertices(shader & program, const che_viewer & mesh); - void pick_vertex(const real_t & x, const real_t & y); }; diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 6d28a430..b54d6792 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -398,9 +398,6 @@ bool app_viewer::process_geodesics(viewer * p_view) ImGui::Combo("alg", (int *) ¶ms.alg, "FM\0PTP_CPU\0HEAT_METHOD\0\0"); #endif // GPROSHAN_CUDA - if(params.alg == geodesics::FM) - ImGui_InputReal("radio", ¶ms.radio); - if(ImGui::Button("Run")) { if(!mesh.selected.size()) @@ -409,13 +406,11 @@ bool app_viewer::process_geodesics(viewer * p_view) params.dist_alloc = &mesh->heatmap(0); TIC(view->time) - geodesics G(mesh, mesh.selected, params); + geodesics dm(mesh, mesh.selected, params); TOC(view->time) sprintf(view->status_message, "geodesics time: %.3fs", view->time); - params.radio = G.radio(); - - G.normalize(); + dm.normalize(); mesh.update_vbo_heatmap(); } diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 2bb0e304..3dea1f34 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -57,7 +57,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // PERFORMANCE & ACCURACY ___________________________________________________________________ - double Time[7]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse + double Time[7]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse real_t Error[5]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse real_t * exact = load_exact_geodesics(exact_dist_path + filename + ".exact", n_vertices); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 6ae78f29..66e130ac 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -286,38 +286,39 @@ void viewer::init_imgui() void viewer::init_menus() { sub_menus.push_back("Viewer"); - add_process(GLFW_KEY_F1, "F1", "Help", menu_help); - add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", menu_save_load_view); - add_process(GLFW_KEY_UP, "UP", "Zoom in", menu_zoom_in); - add_process(GLFW_KEY_DOWN, "DOWN", "Zoom out", menu_zoom_out); - add_process(GLFW_KEY_RIGHT, "RIGHT", "Background color inc", menu_bgc_inc); - add_process(GLFW_KEY_LEFT, "LEFT", "Background color dec", menu_bgc_dec); - add_process(GLFW_KEY_1, "1", "Background color white", menu_bgc_white); - add_process(GLFW_KEY_0, "0", "Background color black", menu_bgc_black); + add_process(GLFW_KEY_F1, "F1", "Help", m_help); + add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", m_save_load_view); + add_process(GLFW_KEY_UP, "UP", "Zoom in", m_zoom_in); + add_process(GLFW_KEY_DOWN, "DOWN", "Zoom out", m_zoom_out); + add_process(GLFW_KEY_RIGHT, "RIGHT", "Background color inc", m_bgc_inc); + add_process(GLFW_KEY_LEFT, "LEFT", "Background color dec", m_bgc_dec); + add_process(GLFW_KEY_1, "1", "Background color white", m_bgc_white); + add_process(GLFW_KEY_0, "0", "Background color black", m_bgc_black); sub_menus.push_back("Render"); - add_process(GLFW_KEY_F5, "F5", "Render Point Cloud", set_render_pointcloud); - add_process(GLFW_KEY_F6, "F6", "Render Wireframe", set_render_wireframe); - add_process(GLFW_KEY_F7, "F7", "Render Triangles", set_render_triangles); - add_process(GLFW_KEY_F8, "F8", "Render GL", set_render_gl); - add_process(GLFW_KEY_R, "R", "Setup Raytracing", setup_raytracing); - add_process(GLFW_KEY_F9, "F9", "Render Embree", set_render_embree); - add_process(GLFW_KEY_ENTER, "ENTER", "Raycasting", raycasting); + add_process(GLFW_KEY_F5, "F5", "Render Point Cloud", m_render_pointcloud); + add_process(GLFW_KEY_F6, "F6", "Render Wireframe", m_render_wireframe); + add_process(GLFW_KEY_F7, "F7", "Render Triangles", m_render_triangles); + add_process(GLFW_KEY_F8, "F8", "Render GL", m_render_gl); + add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", m_render_lines); + add_process(GLFW_KEY_TAB, "TAB", "Render Flat", m_render_flat); + add_process(GLFW_KEY_R, "R", "Setup Raytracing", m_setup_raytracing); + add_process(GLFW_KEY_F9, "F9", "Render Embree", m_render_embree); + add_process(GLFW_KEY_ENTER, "ENTER", "Raycasting", m_raycasting); #ifdef GPROSHAN_OPTIX - add_process(GLFW_KEY_F10, "F10", "Render OptiX", set_render_optix); + add_process(GLFW_KEY_F10, "F10", "Render OptiX", m_render_optix); #endif // GPROSHAN_OPTIX sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", menu_reset_mesh); - add_process(GLFW_KEY_W, "W", "Save Mesh", menu_save_mesh); - add_process(GLFW_KEY_TAB, "TAB", "Render Flat", set_render_flat); - add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", set_render_lines); - add_process(GLFW_KEY_F2, "F2", "Invert Normals", invert_normals); - add_process(GLFW_KEY_F3, "F3", "Gradient Field", set_render_gradients); - add_process(GLFW_KEY_F4, "F4", "Normal Field", set_render_normals); - add_process(GLFW_KEY_B, "B", "Select Border Vertices", select_border_vertices); - add_process(GLFW_KEY_C, "C", "Clean Selected Vertices", clean_selected_vertices); + add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", m_reset_mesh); + add_process(GLFW_KEY_W, "W", "Save Mesh", m_save_mesh); + add_process(0, "", "Normalize Mesh", m_normalize_mesh); + add_process(GLFW_KEY_F2, "F2", "Invert Normals", m_invert_normals); + add_process(GLFW_KEY_F3, "F3", "Gradient Field", m_render_gradients); + add_process(GLFW_KEY_F4, "F4", "Normal Field", m_render_normals); + add_process(GLFW_KEY_B, "B", "Select Border Vertices", m_select_border_vertices); + add_process(GLFW_KEY_C, "C", "Clean Selected Vertices", m_clean_selected_vertices); } void viewer::init_glsl() @@ -467,7 +468,7 @@ void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) } } -bool viewer::menu_help(viewer * view) +bool viewer::m_help(viewer * view) { for(auto & p: view->processes) if(p.second.function != nullptr) @@ -476,7 +477,7 @@ bool viewer::menu_help(viewer * view) return false; } -bool viewer::menu_save_load_view(viewer * view) +bool viewer::m_save_load_view(viewer * view) { filesystem::create_directory(tmp_file_path("views/")); @@ -527,7 +528,7 @@ bool viewer::menu_save_load_view(viewer * view) return true; } -bool viewer::menu_reset_mesh(viewer * view) +bool viewer::m_reset_mesh(viewer * view) { view->other_vertices.clear(); view->vectors.clear(); @@ -541,7 +542,7 @@ bool viewer::menu_reset_mesh(viewer * view) return false; } -bool viewer::menu_save_mesh(viewer * view) +bool viewer::m_save_mesh(viewer * view) { const che * mesh = view->active_mesh(); @@ -594,19 +595,30 @@ bool viewer::menu_save_mesh(viewer * view) return true; } -bool viewer::menu_zoom_in(viewer * view) +bool viewer::m_normalize_mesh(viewer * view) +{ + che_viewer & mesh = view->active_mesh(); + mesh->normalize(); + mesh.update(); + + view->sphere_translations.clear(); + + return false; +} + +bool viewer::m_zoom_in(viewer * view) { view->cam.zoom_in(); return false; } -bool viewer::menu_zoom_out(viewer * view) +bool viewer::m_zoom_out(viewer * view) { view->cam.zoom_out(); return false; } -bool viewer::menu_bgc_inc(viewer * view) +bool viewer::m_bgc_inc(viewer * view) { if(view->bgc < 1) view->bgc += 0.05; else view->bgc = 1; @@ -616,7 +628,7 @@ bool viewer::menu_bgc_inc(viewer * view) return false; } -bool viewer::menu_bgc_dec(viewer * view) +bool viewer::m_bgc_dec(viewer * view) { if(view->bgc > 0) view->bgc -= 0.05; else view->bgc = 0; @@ -626,7 +638,7 @@ bool viewer::menu_bgc_dec(viewer * view) return false; } -bool viewer::menu_bgc_white(viewer * view) +bool viewer::m_bgc_white(viewer * view) { view->bgc = 1; glClearColor(view->bgc, view->bgc, view->bgc, 1.); @@ -634,7 +646,7 @@ bool viewer::menu_bgc_white(viewer * view) return false; } -bool viewer::menu_bgc_black(viewer * view) +bool viewer::m_bgc_black(viewer * view) { view->bgc = 0; glClearColor(view->bgc, view->bgc, view->bgc, 1.); @@ -642,7 +654,7 @@ bool viewer::menu_bgc_black(viewer * view) return false; } -bool viewer::setup_raytracing(viewer * view) +bool viewer::m_setup_raytracing(viewer * view) { che_viewer & mesh = view->active_mesh(); @@ -685,27 +697,27 @@ bool viewer::setup_raytracing(viewer * view) return true; } -bool viewer::set_render_gl(viewer * view) +bool viewer::m_render_gl(viewer * view) { view->render_opt = R_GL; return false; } -bool viewer::set_render_embree(viewer * view) +bool viewer::m_render_embree(viewer * view) { view->rt_restart = true; view->render_opt = R_EMBREE; return false; } -bool viewer::set_render_optix(viewer * view) +bool viewer::m_render_optix(viewer * view) { view->rt_restart = true; view->render_opt = R_OPTIX; return false; } -bool viewer::invert_normals(viewer * view) +bool viewer::m_invert_normals(viewer * view) { che_viewer & mesh = view->active_mesh(); @@ -715,7 +727,7 @@ bool viewer::invert_normals(viewer * view) return false; } -bool viewer::select_border_vertices(viewer * view) +bool viewer::m_select_border_vertices(viewer * view) { che_viewer & mesh = view->active_mesh(); for(const index_t & b: mesh->bounds()) @@ -725,7 +737,7 @@ bool viewer::select_border_vertices(viewer * view) return false; } -bool viewer::clean_selected_vertices(viewer * view) +bool viewer::m_clean_selected_vertices(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.selected.clear(); @@ -733,7 +745,7 @@ bool viewer::clean_selected_vertices(viewer * view) return false; } -bool viewer::set_render_pointcloud(viewer * view) +bool viewer::m_render_pointcloud(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_pointcloud = !mesh.render_pointcloud; @@ -741,7 +753,7 @@ bool viewer::set_render_pointcloud(viewer * view) return false; } -bool viewer::set_render_wireframe(viewer * view) +bool viewer::m_render_wireframe(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_wireframe = !mesh.render_wireframe; @@ -749,7 +761,7 @@ bool viewer::set_render_wireframe(viewer * view) return false; } -bool viewer::set_render_triangles(viewer * view) +bool viewer::m_render_triangles(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_triangles = !mesh.render_triangles; @@ -757,7 +769,7 @@ bool viewer::set_render_triangles(viewer * view) return false; } -bool viewer::set_render_gradients(viewer * view) +bool viewer::m_render_gradients(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_gradients = !mesh.render_gradients; @@ -765,7 +777,7 @@ bool viewer::set_render_gradients(viewer * view) return false; } -bool viewer::set_render_normals(viewer * view) +bool viewer::m_render_normals(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_normals = !mesh.render_normals; @@ -773,7 +785,7 @@ bool viewer::set_render_normals(viewer * view) return false; } -bool viewer::set_render_lines(viewer * view) +bool viewer::m_render_lines(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_lines = !mesh.render_lines; @@ -781,7 +793,7 @@ bool viewer::set_render_lines(viewer * view) return false; } -bool viewer::set_render_flat(viewer * view) +bool viewer::m_render_flat(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_flat = !mesh.render_flat; @@ -790,7 +802,7 @@ bool viewer::set_render_flat(viewer * view) return false; } -bool viewer::raycasting(viewer * view) +bool viewer::m_raycasting(viewer * view) { che_viewer & mesh = view->active_mesh(); From 6a5bbcd67ebae48f81e4767ac4a9f0e459fcd943 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 May 2022 02:19:58 +0200 Subject: [PATCH 0637/1018] viewer: updated draw selected vertices --- include/gproshan/viewer/che_viewer.h | 2 ++ include/gproshan/viewer/viewer.h | 2 -- src/viewer/che_viewer.cpp | 21 ++++++++++++++++++++- src/viewer/viewer.cpp | 22 +--------------------- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index f364cac6..1114f54f 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -37,6 +37,7 @@ class che_viewer public: int vx, vy; ///< viewport positions. std::vector selected; + std::vector selected_xyz; glm::mat4 model_mat = glm::mat4(1); @@ -70,6 +71,7 @@ class che_viewer void draw(shader & program); void draw_point_cloud(shader & program); + void draw_selected_vertices(che_viewer & sphere, shader & program); void translate(const vertex & p); void scale(const real_t & s); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 9ddb7baf..b9c0421a 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -92,7 +92,6 @@ class viewer std::map processes; che_viewer sphere; - std::vector sphere_translations; shader shader_sphere; public: @@ -159,7 +158,6 @@ class viewer static bool m_raycasting(viewer * view); - void draw_selected_vertices(shader & program, const che_viewer & mesh); void pick_vertex(const real_t & x, const real_t & y); }; diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index a7869375..5308749c 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -75,7 +75,7 @@ void che_viewer::update() } render_pointcloud = mesh->is_pointcloud(); - + selected_xyz.clear(); update_vbo(); delete pick_vertex; @@ -226,6 +226,25 @@ void che_viewer::draw_point_cloud(shader & program) program.disable(); } +void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) +{ + if(selected_xyz.size() != selected.size()) + { + selected_xyz.clear(); + selected_xyz.reserve(selected.size()); + + for(const index_t & v: selected) + selected_xyz.push_back(mesh->gt(v)); + } + + if(selected_xyz.size()) + { + sphere.model_mat = model_mat; + sphere.update_instances_positions(selected_xyz); + sphere.draw(program); + } +} + void che_viewer::translate(const vertex & p) { model_mat = glm::translate(model_mat, glm_vec3(p)); diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 66e130ac..99e42081 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -128,7 +128,6 @@ bool viewer::run() if(ImGui::MenuItem((to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; - sphere_translations.clear(); glfwSetWindowTitle(window, mesh->filename.c_str()); } @@ -601,8 +600,6 @@ bool viewer::m_normalize_mesh(viewer * view) mesh->normalize(); mesh.update(); - view->sphere_translations.clear(); - return false; } @@ -862,7 +859,7 @@ void viewer::render_gl() if(mesh.render_gradients) mesh.draw(shader_gradient); - draw_selected_vertices(shader_sphere, mesh); + mesh.draw_selected_vertices(sphere, shader_sphere); } } @@ -896,23 +893,6 @@ void viewer::render_rt(rt::raytracing * rt) rt_frame->display(); } -void viewer::draw_selected_vertices(shader & program, const che_viewer & mesh) -{ - if(sphere_translations.size() != mesh.selected.size()) - { - sphere_translations.resize(mesh.selected.size()); - - for(index_t i = 0; i < mesh.selected.size(); ++i) - sphere_translations[i] = mesh->gt(mesh.selected[i]); - - sphere.update_instances_positions(sphere_translations); - } - - sphere.model_mat = mesh.model_mat; - if(sphere_translations.size()) - sphere.draw(program); -} - void viewer::pick_vertex(const real_t & x, const real_t & y) { float xscale, yscale; From d7ba4912b553a7d4daf88f4c5a5b1cc489c03b0f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 May 2022 19:00:28 +0200 Subject: [PATCH 0638/1018] fairing_taubing: imgui input step interaction --- src/app_viewer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index b54d6792..060a2e54 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -358,10 +358,9 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) che_viewer & mesh = view->active_mesh(); static vector vertices; - static fairing_taubin fair; - ImGui_InputReal("step", &fair.step, 0.001); + static fairing_taubin fair(0); - if(ImGui::Button("Run")) + if(ImGui_InputReal("step", &fair.step, 0.001)) { if(!vertices.size()) { From c8f6339dace40359e3ff3fbc3f6238bdc67f81e8 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Mon, 23 May 2022 11:46:40 +0200 Subject: [PATCH 0639/1018] scanner: using just one point ready --- src/app_viewer.cpp | 2 +- src/scenes/scanner.cpp | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index d672dd52..c21314ed 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -130,7 +130,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) if(ImGui::Button("Scan")) { - che * ptx_mesh = scanner_ptx(mesh, mesh.pick_vertex, n_rows, n_cols, {0, 0, 0}); + che * ptx_mesh = scanner_ptx(mesh, view->rt_embree, n_rows, n_cols, {0, 0, 0}); view->add_mesh(ptx_mesh); che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); } diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 8855ff84..b6820f05 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -71,11 +71,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons CImg img((unsigned char *) vertices_color.data(), 3, n_cols, n_rows); img.permute_axes("zycx"); - img.save((mesh->name() + ".jpg").c_str()); - - CImg img2((unsigned char *) vertices_color.data(), 3, n_rows, n_cols); - img2.permute_axes("zycx"); - img2.save((mesh->name() + "2.jpg").c_str()); + img.save((mesh->name().substr(0, mesh->name().size()-4) + ".jpg").c_str()); std::thread([](CImg img) { img.display(); }, img).detach(); From 36f5d2275c3e57f864b5f9c1008b836cc3b5ca21 Mon Sep 17 00:00:00 2001 From: Lizeth Joseline Fuentes Perez Date: Thu, 26 May 2022 19:47:53 +0200 Subject: [PATCH 0640/1018] scanner: creating test_scanner.cpp + fixing paths --- apps/CMakeLists.txt | 3 ++ apps/test_scanner.cpp | 82 +++++++++++++++++++++++++++++++ include/gproshan/scenes/scanner.h | 2 +- src/scenes/scanner.cpp | 10 ++-- 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 apps/test_scanner.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 203c8eed..270c06ae 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -22,6 +22,9 @@ target_link_libraries(test_geodesics gproshan) add_executable(test_image_denoising test_image_denoising.cpp) target_link_libraries(test_image_denoising gproshan) +add_executable(test_scanner test_scanner.cpp) +target_link_libraries(test_scanner gproshan) + file(MAKE_DIRECTORY ${gproshan_SOURCE_DIR}/tmp) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp new file mode 100644 index 00000000..577a9b59 --- /dev/null +++ b/apps/test_scanner.cpp @@ -0,0 +1,82 @@ +#include "mesh/che_ply.h" +#include "mesh/che_ptx.h" +#include "scenes/scanner.h" +#include "raytracing/rt_embree.h" +#include "viewer/include_opengl.h" +#include +#include "glm/glm.hpp" +#include "glm/gtc/matrix_transform.hpp" +#include + +void translate(glm::mat4 & model_mat, const gproshan::vertex & p) +{ + model_mat = glm::translate(model_mat, glm_vec3(p)); +} + +void scale(glm::mat4 & model_mat, const gproshan::real_t & s) +{ + model_mat = glm::scale(model_mat, {s, s, s}); +} + +void normalize_coordinates(gproshan::che_ply * mesh, glm::mat4 & model_mat) +{ + + gproshan::vertex pmin(INFINITY, INFINITY, INFINITY); + gproshan::vertex pmax(0, 0, 0); + + for(gproshan::index_t v = 0; v < mesh->n_vertices; ++v) + { + const gproshan::vertex & p = mesh->gt(v); + + pmin.x = std::min(pmin.x, p.x); + pmin.y = std::min(pmin.y, p.y); + pmin.z = std::min(pmin.z, p.z); + + pmax.x = std::max(pmax.x, p.x); + pmax.y = std::max(pmax.y, p.y); + pmax.z = std::max(pmax.z, p.z); + } + + scale(model_mat, 2.0 / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z})); + translate(model_mat, - (pmax + pmin) / 2); + +} + +int main(int argc, char* argv[]) +{ + + gproshan_log_var(argc); + if(argc < 2 || argc > 7) + { + std::cerr << "Correct usage: ./apps/test_scanner n_rows n_cols pc_radius ptx_folder jpg_folder" << std::endl; + return -1; + } + // Fetching the arguments + + size_t n_rows = atoi(argv[2]); //512 + size_t n_cols = atoi(argv[3]); //1024 + + char * ptx_folder = argv[5]; + char * jpg_folder = argv[6]; + + gproshan_log_var(ptx_folder); + gproshan_log_var(jpg_folder); + + gproshan::che_ply * mesh_ply = new gproshan::che_ply(argv[1]); + + //Setting up the ray tracing framework + gproshan::rt::raytracing * rt_embree; + float pc_radius = atof(argv[4]); //0.01; + glm::mat4 model_mat = glm::mat4(1); + + normalize_coordinates(mesh_ply, model_mat); + + rt_embree = new gproshan::rt::embree({mesh_ply},{model_mat}, false, pc_radius); + + gproshan::che * ptx_mesh = scanner_ptx(mesh_ply, rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder); + + std::string ptx_filename = ptx_folder + mesh_ply->name(); + gproshan::che_ptx::write_file(ptx_mesh, ptx_filename, n_rows, n_cols); + + return 0; +} \ No newline at end of file diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index f7ea06be..1963a10f 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -12,7 +12,7 @@ namespace gproshan::rt { che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam, const std::string & file_jpg = ""); } // namespace gproshan diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index b6820f05..bc4f6df3 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -17,7 +17,7 @@ che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n return new che(ptx.data(), ptx.size(), nullptr, 0); } -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam, const std::string & file_jpg) { std::vector vertices; std::vector vertices_color; @@ -44,8 +44,11 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons { // p is the direction of the ray p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; + // quering the closest point in the mesh to the hit point and the distance auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); + //const vertex & c = mesh.pointcloud ? mesh->color(v_idx) : + // mesh->shading_color(v_idx, 1.0 - hit.u - hit.v, hit.u, hit.v); if(v_idx == NIL) { @@ -71,14 +74,13 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons CImg img((unsigned char *) vertices_color.data(), 3, n_cols, n_rows); img.permute_axes("zycx"); - img.save((mesh->name().substr(0, mesh->name().size()-4) + ".jpg").c_str()); - + std::string img_filename = file_jpg + mesh->name() + ".jpg"; + img.save(img_filename.c_str()); std::thread([](CImg img) { img.display(); }, img).detach(); return mesh_ptx; - //return new che(ptx.data(), ptx.size(), nullptr, 0); } From 3fca3e1d9d9769235066ee61aa5cdfab2b54077e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 01:06:34 +0200 Subject: [PATCH 0641/1018] refactoring gproshan library, exporting cmake library --- CMakeLists.txt | 40 +--------- apps/gproshan.cpp | 3 +- apps/test_geodesics.cpp | 3 +- apps/test_image_denoising.cpp | 2 +- gproshanConfig.cmake.in | 28 +++++++ include/gproshan/app_viewer.h | 68 ++++++++-------- include/gproshan/features/descriptor.h | 4 +- include/gproshan/features/key_components.h | 2 +- include/gproshan/features/key_points.h | 2 +- include/gproshan/geodesics/dijkstra.h | 2 +- include/gproshan/geodesics/geodesics.h | 2 +- include/gproshan/geodesics/geodesics_ptp.cuh | 2 +- include/gproshan/geodesics/geodesics_ptp.h | 2 +- .../geodesics/geodesics_ptp_coalescence.cuh | 6 +- include/gproshan/geodesics/heat_method.h | 4 +- include/gproshan/geodesics/sampling.h | 4 +- .../gproshan/geodesics/test_geodesics_ptp.cuh | 2 +- .../gproshan/geodesics/test_geodesics_ptp.h | 4 +- .../test_geodesics_ptp_coalescence.cuh | 2 +- include/gproshan/geometry/convex_hull.h | 2 +- include/gproshan/include_arma.h | 2 +- include/gproshan/laplacian/fairing.h | 2 +- include/gproshan/laplacian/fairing_spectral.h | 2 +- include/gproshan/laplacian/fairing_taubin.h | 2 +- include/gproshan/laplacian/laplacian.h | 4 +- include/gproshan/mdict/basis.h | 4 +- include/gproshan/mdict/basis_cosine.h | 2 +- include/gproshan/mdict/basis_dct.h | 2 +- include/gproshan/mdict/image_denoising.h | 2 +- include/gproshan/mdict/mdict.h | 8 +- include/gproshan/mdict/msparse_coding.h | 10 +-- include/gproshan/mdict/patch.h | 6 +- include/gproshan/mesh/che.cuh | 4 +- include/gproshan/mesh/che.h | 4 +- include/gproshan/mesh/che_fill_hole.h | 4 +- include/gproshan/mesh/che_img.h | 2 +- include/gproshan/mesh/che_obj.h | 2 +- include/gproshan/mesh/che_off.h | 2 +- include/gproshan/mesh/che_ply.h | 2 +- include/gproshan/mesh/che_poisson.h | 2 +- include/gproshan/mesh/che_pts.h | 2 +- include/gproshan/mesh/che_ptx.h | 2 +- include/gproshan/mesh/che_sphere.h | 2 +- include/gproshan/mesh/che_xyz.h | 2 +- include/gproshan/mesh/kdtree.h | 2 +- include/gproshan/mesh/quaternion.h | 2 +- include/gproshan/mesh/simplification.h | 4 +- include/gproshan/mesh/vertex.cuh | 2 +- include/gproshan/mesh/vertex.h | 2 +- include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/raytracing/rt_embree.h | 4 +- include/gproshan/raytracing/rt_optix.h | 6 +- include/gproshan/scenes/scanner.h | 2 +- include/gproshan/util.h | 2 +- include/gproshan/viewer/camera.h | 2 +- include/gproshan/viewer/che_viewer.h | 8 +- include/gproshan/viewer/frame.h | 4 +- include/gproshan/viewer/shader.h | 2 +- include/gproshan/viewer/viewer.h | 12 +-- src/CMakeLists.txt | 77 +++++++++++++++++++ src/app_viewer.cpp | 6 +- src/features/descriptor.cpp | 4 +- src/features/key_components.cpp | 4 +- src/features/key_points.cpp | 2 +- src/features/shot.cpp | 2 +- src/geodesics/dijkstra.cpp | 2 +- src/geodesics/geodesics.cpp | 6 +- src/geodesics/geodesics_ptp.cpp | 2 +- src/geodesics/geodesics_ptp.cu | 4 +- src/geodesics/geodesics_ptp_coalescence.cu | 4 +- src/geodesics/heat_method.cpp | 6 +- src/geodesics/heat_method.cu | 2 +- src/geodesics/sampling.cpp | 6 +- src/geodesics/test_geodesics_ptp.cpp | 8 +- src/geodesics/test_geodesics_ptp.cu | 8 +- .../test_geodesics_ptp_coalescence.cu | 10 +-- src/geometry/convex_hull.cpp | 2 +- src/laplacian/fairing.cpp | 2 +- src/laplacian/fairing_spectral.cpp | 4 +- src/laplacian/fairing_taubin.cpp | 4 +- src/laplacian/laplacian.cpp | 2 +- src/mdict/basis.cpp | 2 +- src/mdict/basis_cosine.cpp | 2 +- src/mdict/basis_dct.cpp | 2 +- src/mdict/image_denoising.cpp | 2 +- src/mdict/mdict.cpp | 4 +- src/mdict/msparse_coding.cpp | 14 ++-- src/mdict/patch.cpp | 6 +- src/mesh/che.cpp | 6 +- src/mesh/che.cu | 2 +- src/mesh/che_fill_hole.cpp | 6 +- src/mesh/che_img.cpp | 2 +- src/mesh/che_obj.cpp | 2 +- src/mesh/che_off.cpp | 2 +- src/mesh/che_ply.cpp | 2 +- src/mesh/che_poisson.cpp | 6 +- src/mesh/che_pts.cpp | 2 +- src/mesh/che_ptx.cpp | 2 +- src/mesh/che_sphere.cpp | 2 +- src/mesh/che_xyz.cpp | 2 +- src/mesh/kdtree.cpp | 2 +- src/mesh/quaternion.cpp | 2 +- src/mesh/simplification.cpp | 2 +- src/mesh/vertex.cpp | 2 +- src/mesh/vertex.cu | 2 +- src/raytracing/raytracing.cpp | 2 +- src/raytracing/rt_embree.cpp | 4 +- src/raytracing/rt_optix.cpp | 4 +- src/raytracing/rt_optix.cu | 6 +- src/scenes/scanner.cpp | 2 +- src/util.cpp | 2 +- src/viewer/camera.cpp | 2 +- src/viewer/che_viewer.cpp | 4 +- src/viewer/frame.cpp | 4 +- src/viewer/shader.cpp | 4 +- src/viewer/viewer.cpp | 18 ++--- 116 files changed, 343 insertions(+), 270 deletions(-) create mode 100644 gproshanConfig.cmake.in create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c891b07..54551962 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) -add_definitions(-DGPROSHAN_FLOAT) +#add_definitions(-DGPROSHAN_FLOAT) add_definitions(-DGPROSHAN_LOG) @@ -64,40 +64,8 @@ add_subdirectory(imgui) include_directories(SYSTEM ${gproshan_SOURCE_DIR}/imgui) -include_directories(${gproshan_SOURCE_DIR}/include/gproshan) -add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") +include_directories(${gproshan_SOURCE_DIR}/include/) - -FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) - -add_library(gproshan SHARED ${cpp_sources}) - -target_link_libraries(gproshan embree) -target_link_libraries(gproshan OpenMP::OpenMP_CXX) -target_link_libraries(gproshan GLEW::GLEW) -target_link_libraries(gproshan glfw) -target_link_libraries(gproshan ${X11_X11_LIB}) -target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) -target_link_libraries(gproshan CGAL::CGAL) -target_link_libraries(gproshan ${OptiX_LIBRARY}) -target_link_libraries(gproshan imgui) - -if(CUDAToolkit_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) - list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) - - add_library(gproshan_cuda SHARED ${cu_sources}) - - target_link_libraries(gproshan_cuda CUDA::cudart) - target_link_libraries(gproshan_cuda CUDA::cuda_driver) - target_link_libraries(gproshan_cuda CUDA::cublas) - target_link_libraries(gproshan_cuda CUDA::cusolver) - target_link_libraries(gproshan_cuda CUDA::cusparse) - - target_link_libraries(gproshan gproshan_cuda) -endif(CUDAToolkit_FOUND) - - -add_subdirectory(apps) +add_subdirectory(src) # gproshan library +add_subdirectory(apps) # gproshan executables diff --git a/apps/gproshan.cpp b/apps/gproshan.cpp index 5a3d8a52..df8360fb 100644 --- a/apps/gproshan.cpp +++ b/apps/gproshan.cpp @@ -1,6 +1,5 @@ -#include "app_viewer.h" +#include -using namespace std; int main(int nargs, const char ** args) { diff --git a/apps/test_geodesics.cpp b/apps/test_geodesics.cpp index d874d4be..20d9c4df 100644 --- a/apps/test_geodesics.cpp +++ b/apps/test_geodesics.cpp @@ -1,4 +1,5 @@ -#include "geodesics/test_geodesics_ptp.h" +#include + int main(int nargs, const char ** args) { diff --git a/apps/test_image_denoising.cpp b/apps/test_image_denoising.cpp index 0319c818..a30fd6c4 100644 --- a/apps/test_image_denoising.cpp +++ b/apps/test_image_denoising.cpp @@ -1,4 +1,4 @@ -#include "mdict/image_denoising.h" +#include int main(int nargs, const char ** args) { diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in new file mode 100644 index 00000000..fde56102 --- /dev/null +++ b/gproshanConfig.cmake.in @@ -0,0 +1,28 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) + +find_dependency(CUDAToolkit 11.4) +if(CUDAToolkit_FOUND) + add_definitions(-DGPROSHAN_CUDA) +endif(CUDAToolkit_FOUND) + +find_dependency(embree 3.13 REQUIRED) +find_dependency(Threads REQUIRED) +find_dependency(OpenMP REQUIRED) +find_dependency(GLEW REQUIRED) +find_dependency(glfw3 REQUIRED) +find_dependency(glm REQUIRED) +find_dependency(X11 REQUIRED) +find_dependency(Armadillo REQUIRED) +find_dependency(CGAL REQUIRED) +find_dependency(Eigen3 REQUIRED) +find_dependency(SuiteSparse REQUIRED) +find_dependency(Boost COMPONENTS thread system) + + +include(${CMAKE_CURRENT_LIST_DIR}/gproshanTargets.cmake) +set_and_check(gproshan_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@") + +check_required_components(gproshan) + diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 01c64674..25177c18 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -1,40 +1,40 @@ #ifndef APP_VIEWER_H #define APP_VIEWER_H -#include "include.h" -#include "viewer/viewer.h" - -#include "mesh/che_off.h" -#include "mesh/che_obj.h" -#include "mesh/che_ply.h" -#include "mesh/che_ptx.h" -#include "mesh/che_xyz.h" -#include "mesh/che_pts.h" -#include "mesh/che_img.h" -#include "mesh/che_sphere.h" -#include "mesh/che_fill_hole.h" -#include "mesh/che_poisson.h" -#include "mesh/simplification.h" - -#include "laplacian/laplacian.h" -#include "laplacian/fairing_taubin.h" -#include "laplacian/fairing_spectral.h" - -#include "scenes/scanner.h" - -#include "geometry/convex_hull.h" - -#include "geodesics/dijkstra.h" -#include "geodesics/geodesics.h" -#include "geodesics/sampling.h" - -#include "mdict/msparse_coding.h" -#include "mdict/basis_dct.h" -#include "mdict/patch.h" - -#include "features/descriptor.h" -#include "features/key_points.h" -#include "features/key_components.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index 16c470a6..fc259bf3 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -1,8 +1,8 @@ #ifndef DESCRIPTOR_H #define DESCRIPTOR_H -#include "mesh/che.h" -#include "include_arma.h" +#include +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index f48a0b07..e490cab0 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -1,7 +1,7 @@ #ifndef KEY_COMPONENTS_H #define KEY_COMPONENTS_H -#include "mesh/che.h" +#include #include diff --git a/include/gproshan/features/key_points.h b/include/gproshan/features/key_points.h index 221b04c9..7170cc1b 100644 --- a/include/gproshan/features/key_points.h +++ b/include/gproshan/features/key_points.h @@ -1,7 +1,7 @@ #ifndef KEY_POINTS_H #define KEY_POINTS_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/dijkstra.h b/include/gproshan/geodesics/dijkstra.h index a1c1484b..1fbc96ec 100644 --- a/include/gproshan/geodesics/dijkstra.h +++ b/include/gproshan/geodesics/dijkstra.h @@ -1,7 +1,7 @@ #ifndef DIJKSTRA_H #define DIJKSTRA_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index 87658bee..f6825ae4 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -1,7 +1,7 @@ #ifndef GEODESICS_H #define GEODESICS_H -#include "mesh/che.h" +#include #include diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh index 7e28d5e9..b881e599 100644 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ b/include/gproshan/geodesics/geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef GEODESICS_PTP_CUH #define GEODESICS_PTP_CUH -#include "mesh/che.cuh" +#include #define NT 64 diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index a1ee79f7..8892a42b 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -6,7 +6,7 @@ topleset: (Topological Level Set) */ -#include "mesh/che.h" +#include #define PTP_TOL 1e-4 diff --git a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh index 6e916287..18a3a919 100644 --- a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh @@ -1,9 +1,9 @@ #ifndef GEODESICS_PTP_COALESCENCE_CUH #define GEODESICS_PTP_COALESCENCE_CUH -#include "mesh/che.cuh" -#include "geodesics/geodesics_ptp.cuh" -#include "geodesics/geodesics_ptp.h" +#include +#include +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 6c48766f..350c8c21 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -10,8 +10,8 @@ #ifndef HEAT_METHOD_H #define HEAT_METHOD_H -#include "mesh/che.h" -#include "include_arma.h" +#include +#include #include diff --git a/include/gproshan/geodesics/sampling.h b/include/gproshan/geodesics/sampling.h index b3dddcf7..7c80842f 100644 --- a/include/gproshan/geodesics/sampling.h +++ b/include/gproshan/geodesics/sampling.h @@ -1,8 +1,8 @@ #ifndef SAMPLING_H #define SAMPLING_H -#include "geodesics/geodesics.h" -#include "geodesics/geodesics_ptp.h" +#include +#include #include diff --git a/include/gproshan/geodesics/test_geodesics_ptp.cuh b/include/gproshan/geodesics/test_geodesics_ptp.cuh index 1423b94b..3ea7614c 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_CUH #define TEST_GEODESICS_PTP_CUH -#include "mesh/che.cuh" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/test_geodesics_ptp.h b/include/gproshan/geodesics/test_geodesics_ptp.h index b4f629c6..5fb98fd3 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.h +++ b/include/gproshan/geodesics/test_geodesics_ptp.h @@ -1,8 +1,8 @@ #ifndef TEST_GEODESICS_PTP_H #define TEST_GEODESICS_PTP_H -#include "geodesics/geodesics.h" -#include "geodesics/geodesics_ptp.h" +#include +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh index 9b3cc57c..34773c9e 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh @@ -1,7 +1,7 @@ #ifndef TEST_GEODESICS_PTP_COALESCENCE_CUH #define TEST_GEODESICS_PTP_COALESCENCE_CUH -#include "mesh/che.cuh" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index 4a347181..bbfcde8f 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -1,7 +1,7 @@ #ifndef CONVEX_HULL_H #define CONVEX_HULL_H -#include "mesh/vertex.h" +#include #include diff --git a/include/gproshan/include_arma.h b/include/gproshan/include_arma.h index c4c8d608..bc67e5bf 100644 --- a/include/gproshan/include_arma.h +++ b/include/gproshan/include_arma.h @@ -1,7 +1,7 @@ #ifndef INCLUDE_ARMA_H #define INCLUDE_ARMA_H -#include "include.h" +#include #define ARMA_ALLOW_FAKE_GCC #include diff --git a/include/gproshan/laplacian/fairing.h b/include/gproshan/laplacian/fairing.h index 55fab21f..f58fddd1 100644 --- a/include/gproshan/laplacian/fairing.h +++ b/include/gproshan/laplacian/fairing.h @@ -1,7 +1,7 @@ #ifndef FAIRING_H #define FAIRING_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/laplacian/fairing_spectral.h b/include/gproshan/laplacian/fairing_spectral.h index d16a8a86..fdafd4b1 100644 --- a/include/gproshan/laplacian/fairing_spectral.h +++ b/include/gproshan/laplacian/fairing_spectral.h @@ -1,7 +1,7 @@ #ifndef FAIRING_SPECTRAL_H #define FAIRING_SPECTRAL_H -#include "laplacian/fairing.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h index 0665c194..3edc3f57 100644 --- a/include/gproshan/laplacian/fairing_taubin.h +++ b/include/gproshan/laplacian/fairing_taubin.h @@ -1,7 +1,7 @@ #ifndef FAIRING_TAUBIN_H #define FAIRING_TAUBIN_H -#include "laplacian/fairing.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/laplacian/laplacian.h b/include/gproshan/laplacian/laplacian.h index fcad4da4..21302fea 100644 --- a/include/gproshan/laplacian/laplacian.h +++ b/include/gproshan/laplacian/laplacian.h @@ -1,8 +1,8 @@ #ifndef LAPLACIAN_H #define LAPLACIAN_H -#include "mesh/che.h" -#include "include_arma.h" +#include +#include #include diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 86d00902..42c4e96a 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -1,11 +1,11 @@ #ifndef BASIS_H #define BASIS_H -#include "include.h" +#include #include -#include "include_arma.h" +#include using namespace std; diff --git a/include/gproshan/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h index 83a8524c..f36c0add 100644 --- a/include/gproshan/mdict/basis_cosine.h +++ b/include/gproshan/mdict/basis_cosine.h @@ -1,7 +1,7 @@ #ifndef BASIS_COSSINE_H #define BASIS_COSSINE_H -#include "mdict/basis.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 76e53482..9698a933 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -1,7 +1,7 @@ #ifndef BASIS_DCT_H #define BASIS_DCT_H -#include "mdict/basis.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/image_denoising.h b/include/gproshan/mdict/image_denoising.h index 8550a7d0..a8592c6d 100644 --- a/include/gproshan/mdict/image_denoising.h +++ b/include/gproshan/mdict/image_denoising.h @@ -1,7 +1,7 @@ #ifndef IMAGE_DENOISING_H #define IMAGE_DENOISING_H -#include "mdict/mdict.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 8289df5b..f5fbea63 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -1,13 +1,13 @@ #ifndef MDICT_H #define MDICT_H -#include "include.h" +#include -#include "mdict/patch.h" -#include "mdict/basis.h" +#include +#include -#include "include_arma.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index ecfdaa98..ad76e23a 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -1,12 +1,12 @@ #ifndef MSPARSE_CODING_H #define MSPARSE_CODING_H -#include "mesh/che.h" -#include "mdict/patch.h" -#include "mdict/mdict.h" -#include "mdict/basis.h" +#include +#include +#include +#include -#include "include_arma.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index da775125..7a3a93fc 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -1,15 +1,15 @@ #ifndef PATCH_H #define PATCH_H -#include "include.h" -#include "mesh/che.h" +#include +#include #include #include #include -#include "include_arma.h" +#include #ifdef Success diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index b8dd3ece..8a8944a4 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -1,8 +1,8 @@ #ifndef CHE_CUH #define CHE_CUH -#include "mesh/vertex.cuh" -#include "mesh/che.h" +#include +#include #define cu_for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[cu_prev(he)]) != stop ? he : NIL) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index e057403d..d12e5c0a 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -1,8 +1,8 @@ #ifndef CHE_H #define CHE_H -#include "include.h" -#include "mesh/vertex.h" +#include +#include #include #include diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 94c55382..9dd8a57a 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -1,8 +1,8 @@ #ifndef CHE_FILL_HOLE_H #define CHE_FILL_HOLE_H -#include "mesh/che.h" -#include "include_arma.h" +#include +#include #include diff --git a/include/gproshan/mesh/che_img.h b/include/gproshan/mesh/che_img.h index 87f1af60..a46b811e 100644 --- a/include/gproshan/mesh/che_img.h +++ b/include/gproshan/mesh/che_img.h @@ -1,7 +1,7 @@ #ifndef CHE_IMG_H #define CHE_IMG_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h index 292d17d6..197012c4 100644 --- a/include/gproshan/mesh/che_obj.h +++ b/include/gproshan/mesh/che_obj.h @@ -1,7 +1,7 @@ #ifndef CHE_OBJ_H #define CHE_OBJ_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_off.h b/include/gproshan/mesh/che_off.h index af5cabe5..56fe2882 100644 --- a/include/gproshan/mesh/che_off.h +++ b/include/gproshan/mesh/che_off.h @@ -1,7 +1,7 @@ #ifndef CHE_OFF_H #define CHE_OFF_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_ply.h b/include/gproshan/mesh/che_ply.h index 5269e624..f5161991 100644 --- a/include/gproshan/mesh/che_ply.h +++ b/include/gproshan/mesh/che_ply.h @@ -1,7 +1,7 @@ #ifndef CHE_PLY_H #define CHE_PLY_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_poisson.h b/include/gproshan/mesh/che_poisson.h index c72ce321..a023d22a 100644 --- a/include/gproshan/mesh/che_poisson.h +++ b/include/gproshan/mesh/che_poisson.h @@ -1,7 +1,7 @@ #ifndef CHE_POISSON_H #define CHE_POISSON_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_pts.h b/include/gproshan/mesh/che_pts.h index 86574c6c..b8d6ea57 100644 --- a/include/gproshan/mesh/che_pts.h +++ b/include/gproshan/mesh/che_pts.h @@ -1,7 +1,7 @@ #ifndef CHE_PTS_H #define CHE_PTS_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_ptx.h b/include/gproshan/mesh/che_ptx.h index 1c0cd59a..48b637a7 100644 --- a/include/gproshan/mesh/che_ptx.h +++ b/include/gproshan/mesh/che_ptx.h @@ -1,7 +1,7 @@ #ifndef CHE_PTX_H #define CHE_PTX_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_sphere.h b/include/gproshan/mesh/che_sphere.h index 068a331d..ce780e63 100644 --- a/include/gproshan/mesh/che_sphere.h +++ b/include/gproshan/mesh/che_sphere.h @@ -1,7 +1,7 @@ #ifndef CHE_SPHERE_H #define CHE_SPHERE_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/che_xyz.h b/include/gproshan/mesh/che_xyz.h index 8d8816f3..f05eda51 100644 --- a/include/gproshan/mesh/che_xyz.h +++ b/include/gproshan/mesh/che_xyz.h @@ -1,7 +1,7 @@ #ifndef CHE_XYZ_H #define CHE_XYZ_H -#include "mesh/che.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/kdtree.h b/include/gproshan/mesh/kdtree.h index 8e9ba6fc..7432c980 100644 --- a/include/gproshan/mesh/kdtree.h +++ b/include/gproshan/mesh/kdtree.h @@ -1,7 +1,7 @@ #ifndef KDTREE_H #define KDTREE_H -#include "mesh/vertex.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 81d15adc..7e0677f3 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -1,7 +1,7 @@ #ifndef QUATERNION_H #define QUATERNION_H -#include "mesh/vertex.h" +#include #include diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 4efcb6e9..926f35c0 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -1,8 +1,8 @@ #ifndef SIMPLIFICATION_H #define SIMPLIFICATION_H -#include "mesh/che.h" -#include "include_arma.h" +#include +#include #include diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh index a6776b63..54b429fc 100644 --- a/include/gproshan/mesh/vertex.cuh +++ b/include/gproshan/mesh/vertex.cuh @@ -2,7 +2,7 @@ #define VERTEX_CUH -#include "include.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h index 6c2072d9..6290eead 100644 --- a/include/gproshan/mesh/vertex.h +++ b/include/gproshan/mesh/vertex.h @@ -1,7 +1,7 @@ #ifndef VERTEX_H #define VERTEX_H -#include "include.h" +#include #include diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 70fd3a42..11939103 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -1,7 +1,7 @@ #ifndef RAYTRACING_H #define RAYTRACING_H -#include "mesh/che.h" +#include #include #include diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index fd364d9d..3274bcfc 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -1,8 +1,8 @@ #ifndef RT_EMBREE_H #define RT_EMBREE_H -#include "mesh/che.h" -#include "raytracing/raytracing.h" +#include +#include #include #include diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index c98f6223..d9cecc09 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -3,9 +3,9 @@ #ifndef RT_OPTIX_H #define RT_OPTIX_H -#include "mesh/che.h" -#include "raytracing/raytracing.h" -#include "raytracing/rt_optix_params.h" +#include +#include +#include #include #include diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index b50ad496..3756f24b 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -1,7 +1,7 @@ #ifndef SCANNER_H #define SCANNER_H -#include "raytracing/raytracing.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 528ea636..446f8a2b 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -1,7 +1,7 @@ #ifndef UTIL_H #define UTIL_H -#include "include.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index c5973899..b04c2699 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -1,7 +1,7 @@ #ifndef CAMERA_H #define CAMERA_H -#include "mesh/quaternion.h" +#include #include diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 1114f54f..5f71a71d 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -1,11 +1,11 @@ #ifndef CHE_VIEWER_H #define CHE_VIEWER_H -#include "mesh/che.h" -#include "viewer/shader.h" -#include "raytracing/raytracing.h" +#include +#include +#include -#include "viewer/include_opengl.h" +#include #ifdef GPROSHAN_FLOAT diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index a3cafd7c..d6186742 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -2,9 +2,9 @@ #define FRAME_H -#include "viewer/shader.h" +#include -#include "viewer/include_opengl.h" +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/viewer/shader.h b/include/gproshan/viewer/shader.h index aad74966..3d85566d 100644 --- a/include/gproshan/viewer/shader.h +++ b/include/gproshan/viewer/shader.h @@ -4,7 +4,7 @@ #include #include -#include "viewer/include_opengl.h" +#include // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index b9c0421a..e73d1f61 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -6,14 +6,14 @@ #include -#include "viewer/camera.h" -#include "viewer/shader.h" -#include "viewer/frame.h" -#include "viewer/che_viewer.h" +#include +#include +#include +#include -#include "raytracing/raytracing.h" +#include -#include "viewer/include_opengl.h" +#include #include #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..a39b2d15 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,77 @@ +add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") + + +FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) + +add_library(gproshan SHARED ${cpp_sources}) + +target_link_libraries(gproshan embree) +target_link_libraries(gproshan OpenMP::OpenMP_CXX) +target_link_libraries(gproshan GLEW::GLEW) +target_link_libraries(gproshan glfw) +target_link_libraries(gproshan ${X11_X11_LIB}) +target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) +target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan CGAL::CGAL) +target_link_libraries(gproshan ${OptiX_LIBRARY}) +target_link_libraries(gproshan imgui) + +if(CUDAToolkit_FOUND) + FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) + list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + + add_library(gproshan_cuda SHARED ${cu_sources}) + + target_link_libraries(gproshan_cuda CUDA::cudart) + target_link_libraries(gproshan_cuda CUDA::cuda_driver) + target_link_libraries(gproshan_cuda CUDA::cublas) + target_link_libraries(gproshan_cuda CUDA::cusolver) + target_link_libraries(gproshan_cuda CUDA::cusparse) + + target_link_libraries(gproshan gproshan_cuda) +endif(CUDAToolkit_FOUND) + + + +set(installable_libs gproshan imgui) +if(TARGET gproshan_cuda) + list(APPEND installable_libs gproshan_cuda) +endif() + +install(TARGETS ${installable_libs} + EXPORT gproshanTargets + DESTINATION lib + ) + +install(EXPORT gproshanTargets + FILE gproshanTargets.cmake + DESTINATION lib/cmake/gproshan + ) + +set(INCLUDE_INSTALL_DIRS "${gproshan_SOURCE_DIR}/include/") + +include(CMakePackageConfigHelpers) + +configure_package_config_file( + ${gproshan_SOURCE_DIR}/gproshanConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake" + INSTALL_DESTINATION "lib/cmake/gproshan" + PATH_VARS INCLUDE_INSTALL_DIRS + ) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake" + VERSION "${gproshan_VERSION_MAJOR}.${gproshan_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion + ) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake + DESTINATION lib/cmake/gproshan + ) + +export( EXPORT gproshanTargets + FILE "${CMAKE_CURRENT_BINARY_DIR}/gproshanTargets.cmake" + ) + diff --git a/src/app_viewer.cpp b/src/app_viewer.cpp index 060a2e54..9c37a96b 100644 --- a/src/app_viewer.cpp +++ b/src/app_viewer.cpp @@ -1,4 +1,4 @@ -#include "app_viewer.h" +#include #include #include @@ -333,7 +333,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &max_neigs)) { - if(!vertices.size()) + if(vertices.size() != mesh->n_vertices) { vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); @@ -362,7 +362,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) if(ImGui_InputReal("step", &fair.step, 0.001)) { - if(!vertices.size()) + if(vertices.size() != mesh->n_vertices) { vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); diff --git a/src/features/descriptor.cpp b/src/features/descriptor.cpp index 992d0520..2ecf3c34 100644 --- a/src/features/descriptor.cpp +++ b/src/features/descriptor.cpp @@ -1,6 +1,6 @@ -#include "features/descriptor.h" +#include -#include "laplacian/laplacian.h" +#include // geometry processing and shape analysis framework diff --git a/src/features/key_components.cpp b/src/features/key_components.cpp index 0352ca32..5f71b671 100644 --- a/src/features/key_components.cpp +++ b/src/features/key_components.cpp @@ -1,6 +1,6 @@ -#include "features/key_components.h" +#include -#include "geodesics/geodesics.h" +#include #include diff --git a/src/features/key_points.cpp b/src/features/key_points.cpp index bc762d7e..67552f73 100644 --- a/src/features/key_points.cpp +++ b/src/features/key_points.cpp @@ -1,4 +1,4 @@ -#include "features/key_points.h" +#include #include #include diff --git a/src/features/shot.cpp b/src/features/shot.cpp index 02c05061..a7fe5670 100644 --- a/src/features/shot.cpp +++ b/src/features/shot.cpp @@ -1,3 +1,3 @@ -#include "features/shot.h" +#include diff --git a/src/geodesics/dijkstra.cpp b/src/geodesics/dijkstra.cpp index 6c275401..bf9bfd6e 100644 --- a/src/geodesics/dijkstra.cpp +++ b/src/geodesics/dijkstra.cpp @@ -1,4 +1,4 @@ -#include "geodesics/dijkstra.h" +#include #include #include diff --git a/src/geodesics/geodesics.cpp b/src/geodesics/geodesics.cpp index 54e49fa1..7e26a52b 100644 --- a/src/geodesics/geodesics.cpp +++ b/src/geodesics/geodesics.cpp @@ -1,7 +1,7 @@ -#include "geodesics/geodesics.h" -#include "geodesics/geodesics_ptp.h" +#include +#include -#include "geodesics/heat_method.h" +#include #include #include diff --git a/src/geodesics/geodesics_ptp.cpp b/src/geodesics/geodesics_ptp.cpp index cfd1882a..dda84d70 100644 --- a/src/geodesics/geodesics_ptp.cpp +++ b/src/geodesics/geodesics_ptp.cpp @@ -1,4 +1,4 @@ -#include "geodesics/geodesics_ptp.h" +#include #include #include diff --git a/src/geodesics/geodesics_ptp.cu b/src/geodesics/geodesics_ptp.cu index f7f84928..c8ce3c04 100644 --- a/src/geodesics/geodesics_ptp.cu +++ b/src/geodesics/geodesics_ptp.cu @@ -1,5 +1,5 @@ -#include "geodesics/geodesics_ptp.cuh" -#include "geodesics/geodesics_ptp.h" +#include +#include #include #include diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/geodesics/geodesics_ptp_coalescence.cu index eb93efb9..985fc227 100644 --- a/src/geodesics/geodesics_ptp_coalescence.cu +++ b/src/geodesics/geodesics_ptp_coalescence.cu @@ -1,6 +1,6 @@ -#include "geodesics/geodesics_ptp_coalescence.cuh" +#include -#include "mesh/che_off.h" +#include #include #include diff --git a/src/geodesics/heat_method.cpp b/src/geodesics/heat_method.cpp index 61db71fd..da8a5a6b 100644 --- a/src/geodesics/heat_method.cpp +++ b/src/geodesics/heat_method.cpp @@ -1,7 +1,7 @@ -#include "geodesics/heat_method.h" +#include -#include "util.h" -#include "laplacian/laplacian.h" +#include +#include #include diff --git a/src/geodesics/heat_method.cu b/src/geodesics/heat_method.cu index 6447a5fe..0e499523 100644 --- a/src/geodesics/heat_method.cu +++ b/src/geodesics/heat_method.cu @@ -1,4 +1,4 @@ -#include "include_arma.h" +#include #include diff --git a/src/geodesics/sampling.cpp b/src/geodesics/sampling.cpp index 4dc746d8..001494ea 100644 --- a/src/geodesics/sampling.cpp +++ b/src/geodesics/sampling.cpp @@ -1,7 +1,7 @@ -#include "geodesics/sampling.h" +#include -#include "geodesics/geodesics_ptp.h" -#include "mesh/che_off.h" +#include +#include #include diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/geodesics/test_geodesics_ptp.cpp index 3dea1f34..32dda01f 100644 --- a/src/geodesics/test_geodesics_ptp.cpp +++ b/src/geodesics/test_geodesics_ptp.cpp @@ -1,8 +1,8 @@ -#include "geodesics/test_geodesics_ptp.h" +#include -#include "mesh/che_off.h" -#include "geodesics/geodesics_ptp.h" -#include "geodesics/heat_method.h" +#include +#include +#include #include diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/geodesics/test_geodesics_ptp.cu index fe0ba11b..4909f9cf 100644 --- a/src/geodesics/test_geodesics_ptp.cu +++ b/src/geodesics/test_geodesics_ptp.cu @@ -1,8 +1,8 @@ -#include "geodesics/test_geodesics_ptp.cuh" -#include "geodesics/test_geodesics_ptp.h" +#include +#include -#include "geodesics/geodesics_ptp.cuh" -#include "geodesics/geodesics_ptp.h" +#include +#include #include #include diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/geodesics/test_geodesics_ptp_coalescence.cu index e25109fa..bbe60d9b 100644 --- a/src/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/geodesics/test_geodesics_ptp_coalescence.cu @@ -1,10 +1,10 @@ -#include "geodesics/test_geodesics_ptp_coalescence.cuh" +#include -#include "geodesics/geodesics_ptp_coalescence.cuh" -#include "geodesics/geodesics_ptp.h" -#include "geodesics/test_geodesics_ptp.h" +#include +#include +#include -#include "mesh/che_off.h" +#include #include #include diff --git a/src/geometry/convex_hull.cpp b/src/geometry/convex_hull.cpp index 3a437000..5b63f628 100644 --- a/src/geometry/convex_hull.cpp +++ b/src/geometry/convex_hull.cpp @@ -1,4 +1,4 @@ -#include "geometry/convex_hull.h" +#include #include diff --git a/src/laplacian/fairing.cpp b/src/laplacian/fairing.cpp index effd5bab..c0d1d20c 100644 --- a/src/laplacian/fairing.cpp +++ b/src/laplacian/fairing.cpp @@ -1,4 +1,4 @@ -#include "laplacian/fairing.h" +#include // geometry processing and shape analysis framework diff --git a/src/laplacian/fairing_spectral.cpp b/src/laplacian/fairing_spectral.cpp index 27781b0a..162c09a2 100644 --- a/src/laplacian/fairing_spectral.cpp +++ b/src/laplacian/fairing_spectral.cpp @@ -1,6 +1,6 @@ -#include "laplacian/fairing_spectral.h" +#include -#include "laplacian/laplacian.h" +#include // geometry processing and shape analysis framework diff --git a/src/laplacian/fairing_taubin.cpp b/src/laplacian/fairing_taubin.cpp index dd4cf091..bc3700c9 100644 --- a/src/laplacian/fairing_taubin.cpp +++ b/src/laplacian/fairing_taubin.cpp @@ -1,6 +1,6 @@ -#include "laplacian/fairing_taubin.h" +#include -#include "laplacian/laplacian.h" +#include // geometry processing and shape analysis framework diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 9836b2c1..9dbb1f82 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -1,4 +1,4 @@ -#include "laplacian/laplacian.h" +#include using namespace std; using namespace Eigen; diff --git a/src/mdict/basis.cpp b/src/mdict/basis.cpp index 2a16ae76..96561634 100644 --- a/src/mdict/basis.cpp +++ b/src/mdict/basis.cpp @@ -1,4 +1,4 @@ -#include "mdict/basis.h" +#include // geometry processing and shape analysis framework diff --git a/src/mdict/basis_cosine.cpp b/src/mdict/basis_cosine.cpp index d6e3d978..5f956790 100644 --- a/src/mdict/basis_cosine.cpp +++ b/src/mdict/basis_cosine.cpp @@ -1,4 +1,4 @@ -#include "mdict/basis_cosine.h" +#include #include diff --git a/src/mdict/basis_dct.cpp b/src/mdict/basis_dct.cpp index b787d1c5..6cbe6165 100644 --- a/src/mdict/basis_dct.cpp +++ b/src/mdict/basis_dct.cpp @@ -1,4 +1,4 @@ -#include "mdict/basis_dct.h" +#include #include diff --git a/src/mdict/image_denoising.cpp b/src/mdict/image_denoising.cpp index 81168af5..eefd5ed9 100644 --- a/src/mdict/image_denoising.cpp +++ b/src/mdict/image_denoising.cpp @@ -1,4 +1,4 @@ -#include "mdict/image_denoising.h" +#include #include diff --git a/src/mdict/mdict.cpp b/src/mdict/mdict.cpp index 8ff58aa5..96918359 100644 --- a/src/mdict/mdict.cpp +++ b/src/mdict/mdict.cpp @@ -1,6 +1,6 @@ -#include "mdict/mdict.h" +#include -#include "geodesics/sampling.h" +#include #include #include diff --git a/src/mdict/msparse_coding.cpp b/src/mdict/msparse_coding.cpp index 9a6ad849..ad9fb817 100644 --- a/src/mdict/msparse_coding.cpp +++ b/src/mdict/msparse_coding.cpp @@ -1,12 +1,12 @@ -#include "mdict/msparse_coding.h" +#include -#include "mdict/mdict.h" -#include "mesh/che_off.h" -#include "mesh/che_poisson.h" -#include "mesh/che_fill_hole.h" -#include "geodesics/sampling.h" +#include +#include +#include +#include +#include -#include "viewer/viewer.h" +#include #include #include diff --git a/src/mdict/patch.cpp b/src/mdict/patch.cpp index d4b035cd..91ecba7d 100644 --- a/src/mdict/patch.cpp +++ b/src/mdict/patch.cpp @@ -1,7 +1,7 @@ -#include "mdict/patch.h" +#include -#include "mdict/msparse_coding.h" -#include "geodesics/geodesics.h" +#include +#include #include #include diff --git a/src/mesh/che.cpp b/src/mesh/che.cpp index 65f6749f..5d23b475 100644 --- a/src/mesh/che.cpp +++ b/src/mesh/che.cpp @@ -1,7 +1,7 @@ -#include "mesh/che.h" +#include -#include "mesh/kdtree.h" -#include "include_arma.h" +#include +#include #include #include diff --git a/src/mesh/che.cu b/src/mesh/che.cu index 6a1f4290..76ed9fec 100644 --- a/src/mesh/che.cu +++ b/src/mesh/che.cu @@ -1,4 +1,4 @@ -#include "mesh/che.cuh" +#include // geometry processing and shape analysis framework diff --git a/src/mesh/che_fill_hole.cpp b/src/mesh/che_fill_hole.cpp index e323d4fb..f8aefbea 100644 --- a/src/mesh/che_fill_hole.cpp +++ b/src/mesh/che_fill_hole.cpp @@ -1,7 +1,7 @@ -#include "mesh/che_fill_hole.h" +#include -#include "mesh/che_off.h" -#include "laplacian/laplacian.h" +#include +#include #include diff --git a/src/mesh/che_img.cpp b/src/mesh/che_img.cpp index 0bbea51a..7342c2f2 100644 --- a/src/mesh/che_img.cpp +++ b/src/mesh/che_img.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_img.h" +#include #include #include diff --git a/src/mesh/che_obj.cpp b/src/mesh/che_obj.cpp index 53050a5a..35989566 100644 --- a/src/mesh/che_obj.cpp +++ b/src/mesh/che_obj.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_obj.h" +#include #include #include diff --git a/src/mesh/che_off.cpp b/src/mesh/che_off.cpp index 5b693748..c468bc45 100644 --- a/src/mesh/che_off.cpp +++ b/src/mesh/che_off.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_off.h" +#include #include #include diff --git a/src/mesh/che_ply.cpp b/src/mesh/che_ply.cpp index 95a6ce3e..5ead8743 100644 --- a/src/mesh/che_ply.cpp +++ b/src/mesh/che_ply.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_ply.h" +#include #include #include diff --git a/src/mesh/che_poisson.cpp b/src/mesh/che_poisson.cpp index bf3e75a5..d52ce9b4 100644 --- a/src/mesh/che_poisson.cpp +++ b/src/mesh/che_poisson.cpp @@ -1,7 +1,7 @@ -#include "mesh/che_poisson.h" +#include -#include "laplacian/laplacian.h" -#include "include_arma.h" +#include +#include using namespace std; diff --git a/src/mesh/che_pts.cpp b/src/mesh/che_pts.cpp index f68f9f44..96f68175 100644 --- a/src/mesh/che_pts.cpp +++ b/src/mesh/che_pts.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_pts.h" +#include #include #include diff --git a/src/mesh/che_ptx.cpp b/src/mesh/che_ptx.cpp index 3974142c..e2b9920e 100644 --- a/src/mesh/che_ptx.cpp +++ b/src/mesh/che_ptx.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_ptx.h" +#include #include #include diff --git a/src/mesh/che_sphere.cpp b/src/mesh/che_sphere.cpp index 3db05b8f..9dd3a228 100644 --- a/src/mesh/che_sphere.cpp +++ b/src/mesh/che_sphere.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_sphere.h" +#include #include #include diff --git a/src/mesh/che_xyz.cpp b/src/mesh/che_xyz.cpp index 916365a2..2040075b 100644 --- a/src/mesh/che_xyz.cpp +++ b/src/mesh/che_xyz.cpp @@ -1,4 +1,4 @@ -#include "mesh/che_xyz.h" +#include #include #include diff --git a/src/mesh/kdtree.cpp b/src/mesh/kdtree.cpp index 771e90d0..b3f57899 100644 --- a/src/mesh/kdtree.cpp +++ b/src/mesh/kdtree.cpp @@ -1,4 +1,4 @@ -#include "mesh/kdtree.h" +#include #include diff --git a/src/mesh/quaternion.cpp b/src/mesh/quaternion.cpp index c0361a1b..85acdb2f 100644 --- a/src/mesh/quaternion.cpp +++ b/src/mesh/quaternion.cpp @@ -1,4 +1,4 @@ -#include "mesh/quaternion.h" +#include #include #include diff --git a/src/mesh/simplification.cpp b/src/mesh/simplification.cpp index 7f8af946..91a0c790 100644 --- a/src/mesh/simplification.cpp +++ b/src/mesh/simplification.cpp @@ -1,4 +1,4 @@ -#include "mesh/simplification.h" +#include using namespace std; diff --git a/src/mesh/vertex.cpp b/src/mesh/vertex.cpp index 6270e0d8..58308521 100644 --- a/src/mesh/vertex.cpp +++ b/src/mesh/vertex.cpp @@ -1,4 +1,4 @@ -#include "mesh/vertex.h" +#include #include diff --git a/src/mesh/vertex.cu b/src/mesh/vertex.cu index dd786a7b..52ec84a4 100644 --- a/src/mesh/vertex.cu +++ b/src/mesh/vertex.cu @@ -1,4 +1,4 @@ -#include "mesh/vertex.cuh" +#include // geometry processing and shape analysis framework diff --git a/src/raytracing/raytracing.cpp b/src/raytracing/raytracing.cpp index d4044fa7..a88673c0 100644 --- a/src/raytracing/raytracing.cpp +++ b/src/raytracing/raytracing.cpp @@ -1,4 +1,4 @@ -#include "raytracing/raytracing.h" +#include #include diff --git a/src/raytracing/rt_embree.cpp b/src/raytracing/rt_embree.cpp index f7e352cb..ac6a0d2a 100644 --- a/src/raytracing/rt_embree.cpp +++ b/src/raytracing/rt_embree.cpp @@ -1,7 +1,7 @@ -#include "raytracing/rt_embree.h" +#include -#include "util.h" +#include #include #include diff --git a/src/raytracing/rt_optix.cpp b/src/raytracing/rt_optix.cpp index d452da8e..d9a7d44e 100644 --- a/src/raytracing/rt_optix.cpp +++ b/src/raytracing/rt_optix.cpp @@ -1,8 +1,8 @@ #ifdef GPROSHAN_OPTIX -#include "raytracing/rt_optix.h" +#include -#include "mesh/che.cuh" +#include #include #include diff --git a/src/raytracing/rt_optix.cu b/src/raytracing/rt_optix.cu index f7f0d1a4..37d1b05b 100644 --- a/src/raytracing/rt_optix.cu +++ b/src/raytracing/rt_optix.cu @@ -1,9 +1,9 @@ #ifdef GPROSHAN_OPTIX -#include "mesh/che.h" -#include "mesh/vertex.cuh" -#include "raytracing/rt_optix_params.h" +#include +#include +#include #include diff --git a/src/scenes/scanner.cpp b/src/scenes/scanner.cpp index 9f3591c1..0e1cfbcd 100644 --- a/src/scenes/scanner.cpp +++ b/src/scenes/scanner.cpp @@ -1,4 +1,4 @@ -#include "scenes/scanner.h" +#include // geometry processing and shape analysis framework diff --git a/src/util.cpp b/src/util.cpp index 28fd4d9d..18d30b16 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,4 +1,4 @@ -#include "util.h" +#include #include diff --git a/src/viewer/camera.cpp b/src/viewer/camera.cpp index 95affdc4..695916b5 100644 --- a/src/viewer/camera.cpp +++ b/src/viewer/camera.cpp @@ -1,4 +1,4 @@ -#include "viewer/camera.h" +#include #include diff --git a/src/viewer/che_viewer.cpp b/src/viewer/che_viewer.cpp index 5308749c..8867351f 100644 --- a/src/viewer/che_viewer.cpp +++ b/src/viewer/che_viewer.cpp @@ -1,4 +1,4 @@ -#include "viewer/che_viewer.h" +#include #include #include @@ -7,7 +7,7 @@ #include -#include "raytracing/rt_embree.h" +#include using namespace std; diff --git a/src/viewer/frame.cpp b/src/viewer/frame.cpp index 1325f42d..a3f31007 100644 --- a/src/viewer/frame.cpp +++ b/src/viewer/frame.cpp @@ -1,6 +1,6 @@ -#include "viewer/frame.h" +#include -#include "include.h" +#include #include diff --git a/src/viewer/shader.cpp b/src/viewer/shader.cpp index d44e76d3..0bced989 100644 --- a/src/viewer/shader.cpp +++ b/src/viewer/shader.cpp @@ -1,6 +1,6 @@ -#include "viewer/shader.h" +#include -#include "include.h" +#include #include #include diff --git a/src/viewer/viewer.cpp b/src/viewer/viewer.cpp index 99e42081..7f169160 100644 --- a/src/viewer/viewer.cpp +++ b/src/viewer/viewer.cpp @@ -1,4 +1,4 @@ -#include "viewer/viewer.h" +#include #include #include @@ -11,17 +11,17 @@ #include #include -#include "mesh/che_off.h" -#include "mesh/che_obj.h" -#include "mesh/che_ply.h" -#include "mesh/che_xyz.h" -#include "mesh/che_pts.h" -#include "mesh/che_sphere.h" +#include +#include +#include +#include +#include +#include -#include "raytracing/rt_embree.h" +#include #ifdef GPROSHAN_OPTIX - #include "raytracing/rt_optix.h" + #include #endif // GPROSHAN_OPTIX From bc19d1cbfed2dd1f0359f56493e385e4e2ac0eba Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 01:18:07 +0200 Subject: [PATCH 0642/1018] setting gproshan_INCLUDE_DIR --- CMakeLists.txt | 2 +- gproshanConfig.cmake.in | 2 +- include/gproshan/viewer/viewer.h | 6 +++--- {imgui => include/imgui}/CMakeLists.txt | 2 +- {imgui => include/imgui}/LICENSE.txt | 0 {imgui => include/imgui}/imconfig.h | 0 {imgui => include/imgui}/imgui.cpp | 0 {imgui => include/imgui}/imgui.h | 0 {imgui => include/imgui}/imgui_demo.cpp | 0 {imgui => include/imgui}/imgui_draw.cpp | 0 {imgui => include/imgui}/imgui_impl_glfw.cpp | 0 {imgui => include/imgui}/imgui_impl_glfw.h | 0 {imgui => include/imgui}/imgui_impl_opengl3.cpp | 0 {imgui => include/imgui}/imgui_impl_opengl3.h | 0 {imgui => include/imgui}/imgui_impl_opengl3_loader.h | 0 {imgui => include/imgui}/imgui_internal.h | 0 {imgui => include/imgui}/imgui_tables.cpp | 0 {imgui => include/imgui}/imgui_widgets.cpp | 0 {imgui => include/imgui}/imstb_rectpack.h | 0 {imgui => include/imgui}/imstb_textedit.h | 0 {imgui => include/imgui}/imstb_truetype.h | 0 src/CMakeLists.txt | 4 ++-- 22 files changed, 8 insertions(+), 8 deletions(-) rename {imgui => include/imgui}/CMakeLists.txt (63%) rename {imgui => include/imgui}/LICENSE.txt (100%) rename {imgui => include/imgui}/imconfig.h (100%) rename {imgui => include/imgui}/imgui.cpp (100%) rename {imgui => include/imgui}/imgui.h (100%) rename {imgui => include/imgui}/imgui_demo.cpp (100%) rename {imgui => include/imgui}/imgui_draw.cpp (100%) rename {imgui => include/imgui}/imgui_impl_glfw.cpp (100%) rename {imgui => include/imgui}/imgui_impl_glfw.h (100%) rename {imgui => include/imgui}/imgui_impl_opengl3.cpp (100%) rename {imgui => include/imgui}/imgui_impl_opengl3.h (100%) rename {imgui => include/imgui}/imgui_impl_opengl3_loader.h (100%) rename {imgui => include/imgui}/imgui_internal.h (100%) rename {imgui => include/imgui}/imgui_tables.cpp (100%) rename {imgui => include/imgui}/imgui_widgets.cpp (100%) rename {imgui => include/imgui}/imstb_rectpack.h (100%) rename {imgui => include/imgui}/imstb_textedit.h (100%) rename {imgui => include/imgui}/imstb_truetype.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 54551962..b567aee3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,7 @@ include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) -add_subdirectory(imgui) +add_subdirectory(include/imgui) include_directories(SYSTEM ${gproshan_SOURCE_DIR}/imgui) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index fde56102..881e2862 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -22,7 +22,7 @@ find_dependency(Boost COMPONENTS thread system) include(${CMAKE_CURRENT_LIST_DIR}/gproshanTargets.cmake) -set_and_check(gproshan_INCLUDE_DIRS "@PACKAGE_INCLUDE_INSTALL_DIRS@") +set_and_check(gproshan_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") check_required_components(gproshan) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index e73d1f61..7cbfec4c 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -15,9 +15,9 @@ #include -#include -#include -#include +#include +#include +#include #ifdef GPROSHAN_FLOAT diff --git a/imgui/CMakeLists.txt b/include/imgui/CMakeLists.txt similarity index 63% rename from imgui/CMakeLists.txt rename to include/imgui/CMakeLists.txt index 8456f44b..b8fd04b7 100644 --- a/imgui/CMakeLists.txt +++ b/include/imgui/CMakeLists.txt @@ -1,4 +1,4 @@ -FILE(GLOB_RECURSE imgui_sources ${gproshan_SOURCE_DIR}/imgui/*.cpp) +FILE(GLOB_RECURSE imgui_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) add_library(imgui SHARED ${imgui_sources}) target_link_libraries(imgui GLEW::GLEW) diff --git a/imgui/LICENSE.txt b/include/imgui/LICENSE.txt similarity index 100% rename from imgui/LICENSE.txt rename to include/imgui/LICENSE.txt diff --git a/imgui/imconfig.h b/include/imgui/imconfig.h similarity index 100% rename from imgui/imconfig.h rename to include/imgui/imconfig.h diff --git a/imgui/imgui.cpp b/include/imgui/imgui.cpp similarity index 100% rename from imgui/imgui.cpp rename to include/imgui/imgui.cpp diff --git a/imgui/imgui.h b/include/imgui/imgui.h similarity index 100% rename from imgui/imgui.h rename to include/imgui/imgui.h diff --git a/imgui/imgui_demo.cpp b/include/imgui/imgui_demo.cpp similarity index 100% rename from imgui/imgui_demo.cpp rename to include/imgui/imgui_demo.cpp diff --git a/imgui/imgui_draw.cpp b/include/imgui/imgui_draw.cpp similarity index 100% rename from imgui/imgui_draw.cpp rename to include/imgui/imgui_draw.cpp diff --git a/imgui/imgui_impl_glfw.cpp b/include/imgui/imgui_impl_glfw.cpp similarity index 100% rename from imgui/imgui_impl_glfw.cpp rename to include/imgui/imgui_impl_glfw.cpp diff --git a/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h similarity index 100% rename from imgui/imgui_impl_glfw.h rename to include/imgui/imgui_impl_glfw.h diff --git a/imgui/imgui_impl_opengl3.cpp b/include/imgui/imgui_impl_opengl3.cpp similarity index 100% rename from imgui/imgui_impl_opengl3.cpp rename to include/imgui/imgui_impl_opengl3.cpp diff --git a/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h similarity index 100% rename from imgui/imgui_impl_opengl3.h rename to include/imgui/imgui_impl_opengl3.h diff --git a/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h similarity index 100% rename from imgui/imgui_impl_opengl3_loader.h rename to include/imgui/imgui_impl_opengl3_loader.h diff --git a/imgui/imgui_internal.h b/include/imgui/imgui_internal.h similarity index 100% rename from imgui/imgui_internal.h rename to include/imgui/imgui_internal.h diff --git a/imgui/imgui_tables.cpp b/include/imgui/imgui_tables.cpp similarity index 100% rename from imgui/imgui_tables.cpp rename to include/imgui/imgui_tables.cpp diff --git a/imgui/imgui_widgets.cpp b/include/imgui/imgui_widgets.cpp similarity index 100% rename from imgui/imgui_widgets.cpp rename to include/imgui/imgui_widgets.cpp diff --git a/imgui/imstb_rectpack.h b/include/imgui/imstb_rectpack.h similarity index 100% rename from imgui/imstb_rectpack.h rename to include/imgui/imstb_rectpack.h diff --git a/imgui/imstb_textedit.h b/include/imgui/imstb_textedit.h similarity index 100% rename from imgui/imstb_textedit.h rename to include/imgui/imstb_textedit.h diff --git a/imgui/imstb_truetype.h b/include/imgui/imstb_truetype.h similarity index 100% rename from imgui/imstb_truetype.h rename to include/imgui/imstb_truetype.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a39b2d15..03575503 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,7 +48,7 @@ install(EXPORT gproshanTargets DESTINATION lib/cmake/gproshan ) -set(INCLUDE_INSTALL_DIRS "${gproshan_SOURCE_DIR}/include/") +set(INCLUDE_INSTALL_DIR "${gproshan_SOURCE_DIR}/include/") include(CMakePackageConfigHelpers) @@ -56,7 +56,7 @@ configure_package_config_file( ${gproshan_SOURCE_DIR}/gproshanConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake" INSTALL_DESTINATION "lib/cmake/gproshan" - PATH_VARS INCLUDE_INSTALL_DIRS + PATH_VARS INCLUDE_INSTALL_DIR ) write_basic_package_version_file( From 72e9d895d1b8c34cea1a1560185f09e0ff3755c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 01:27:00 +0200 Subject: [PATCH 0643/1018] removing eigen dependency --- CMakeLists.txt | 2 -- README.md | 2 +- include/gproshan/laplacian/laplacian.h | 6 ----- src/laplacian/laplacian.cpp | 31 -------------------------- 4 files changed, 1 insertion(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b567aee3..4e4c30d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,6 @@ find_package(glm REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(CGAL REQUIRED) -find_package(Eigen3 REQUIRED) find_package(SuiteSparse REQUIRED) find_package(Boost COMPONENTS thread system) @@ -55,7 +54,6 @@ include_directories(SYSTEM ${embree_INCLUDE_DIRS}) include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) -include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR}) include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) diff --git a/README.md b/README.md index 6d976207..f27ce455 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ g++ >= 9.3, cuda >= 11.0, cmake >= 3.18, armadillo, eigen, cgal, suitesparse, op In Ubuntu you can install them with: - sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot + sudo apt install cmake libarmadillo-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot #### Installing Intel Embree diff --git a/include/gproshan/laplacian/laplacian.h b/include/gproshan/laplacian/laplacian.h index 21302fea..9ddd1796 100644 --- a/include/gproshan/laplacian/laplacian.h +++ b/include/gproshan/laplacian/laplacian.h @@ -4,19 +4,13 @@ #include #include -#include - // geometry processing and shape analysis framework namespace gproshan { -typedef Eigen::SparseMatrix sp_mat_e; - void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A); -void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A); - size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k); diff --git a/src/laplacian/laplacian.cpp b/src/laplacian/laplacian.cpp index 9dbb1f82..c090a67c 100644 --- a/src/laplacian/laplacian.cpp +++ b/src/laplacian/laplacian.cpp @@ -1,7 +1,6 @@ #include using namespace std; -using namespace Eigen; // geometry processing and shape analysis framework @@ -51,36 +50,6 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) A(v, v) = mesh->area_vertex(v); } -void laplacian(const che * mesh, sp_mat_e & L, sp_mat_e & A) -{ - gproshan_debug(LAPLACIAN); - - size_t n_edges = mesh->n_edges; - size_t n_vertices = mesh->n_vertices; - - sp_mat_e D(n_edges, n_vertices); - sp_mat_e S(n_edges, n_edges); - - D.reserve(VectorXi::Constant(n_edges,2)); - S.reserve(VectorXi::Constant(n_edges,1)); - - for(index_t e = 0; e < n_edges; ++e) - { - D.insert(e, mesh->vt(mesh->et(e))) = 1; - D.insert(e, mesh->vt(next(mesh->et(e)))) = -1; - - S.insert(e, e) = (mesh->cotan(mesh->et(e)) + - mesh->cotan(mesh->ot_et(e))) / 2; - } - - L = D.transpose() * S * D; - - A.reserve(VectorXi::Constant(n_vertices, 1)); - for(index_t v = 0; v < n_vertices; ++v) - A.insert(v, v) = mesh->area_vertex(v); - //A.insert(v, v) = 1.0 / sqrt(mesh->area_vertex(v)); -} - size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) { laplacian(mesh, L, A); From a2eea1ec1af291d2587ff54d8b9f68d15dcf3087 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 01:35:02 +0200 Subject: [PATCH 0644/1018] cgal depends on eigen3 --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e4c30d8..432fd06f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ find_package(glfw3 REQUIRED) find_package(glm REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) +find_package(Eigen3 REQUIRED) find_package(CGAL REQUIRED) find_package(SuiteSparse REQUIRED) find_package(Boost COMPONENTS thread system) @@ -55,6 +56,7 @@ include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) +include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) From 9f3425465a4ed1fb8e6f0c814d16825091b81689 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 12:09:27 +0200 Subject: [PATCH 0645/1018] refactoring CMakeLists.txt structure --- CHANGELOG.md | 3 +- CMakeLists.txt | 20 ++--- apps/CMakeLists.txt | 5 +- src/CMakeLists.txt | 78 +------------------ src/gproshan/CMakeLists.txt | 78 +++++++++++++++++++ src/{ => gproshan}/app_viewer.cpp | 0 src/{ => gproshan}/features/descriptor.cpp | 0 .../features/key_components.cpp | 0 src/{ => gproshan}/features/key_points.cpp | 0 src/{ => gproshan}/features/shot.cpp | 0 src/{ => gproshan}/geodesics/dijkstra.cpp | 0 src/{ => gproshan}/geodesics/geodesics.cpp | 0 .../geodesics/geodesics_ptp.cpp | 0 src/{ => gproshan}/geodesics/geodesics_ptp.cu | 0 .../geodesics/geodesics_ptp_coalescence.cu | 0 src/{ => gproshan}/geodesics/heat_method.cpp | 0 src/{ => gproshan}/geodesics/heat_method.cu | 0 src/{ => gproshan}/geodesics/sampling.cpp | 0 .../geodesics/test_geodesics_ptp.cpp | 0 .../geodesics/test_geodesics_ptp.cu | 0 .../test_geodesics_ptp_coalescence.cu | 0 src/{ => gproshan}/geometry/convex_hull.cpp | 0 src/{ => gproshan}/laplacian/fairing.cpp | 0 .../laplacian/fairing_spectral.cpp | 0 .../laplacian/fairing_taubin.cpp | 0 src/{ => gproshan}/laplacian/laplacian.cpp | 0 src/{ => gproshan}/mdict/basis.cpp | 0 src/{ => gproshan}/mdict/basis_cosine.cpp | 0 src/{ => gproshan}/mdict/basis_dct.cpp | 0 src/{ => gproshan}/mdict/image_denoising.cpp | 0 src/{ => gproshan}/mdict/mdict.cpp | 0 src/{ => gproshan}/mdict/msparse_coding.cpp | 0 src/{ => gproshan}/mdict/patch.cpp | 0 src/{ => gproshan}/mesh/che.cpp | 0 src/{ => gproshan}/mesh/che.cu | 0 src/{ => gproshan}/mesh/che_fill_hole.cpp | 0 src/{ => gproshan}/mesh/che_img.cpp | 0 src/{ => gproshan}/mesh/che_obj.cpp | 0 src/{ => gproshan}/mesh/che_off.cpp | 0 src/{ => gproshan}/mesh/che_ply.cpp | 0 src/{ => gproshan}/mesh/che_poisson.cpp | 0 src/{ => gproshan}/mesh/che_pts.cpp | 0 src/{ => gproshan}/mesh/che_ptx.cpp | 0 src/{ => gproshan}/mesh/che_sphere.cpp | 0 src/{ => gproshan}/mesh/che_xyz.cpp | 0 src/{ => gproshan}/mesh/kdtree.cpp | 0 src/{ => gproshan}/mesh/quaternion.cpp | 0 src/{ => gproshan}/mesh/simplification.cpp | 0 src/{ => gproshan}/mesh/vertex.cpp | 0 src/{ => gproshan}/mesh/vertex.cu | 0 src/{ => gproshan}/raytracing/raytracing.cpp | 0 src/{ => gproshan}/raytracing/rt_embree.cpp | 0 src/{ => gproshan}/raytracing/rt_optix.cpp | 0 src/{ => gproshan}/raytracing/rt_optix.cu | 0 src/{ => gproshan}/scenes/scanner.cpp | 0 src/{ => gproshan}/util.cpp | 0 src/{ => gproshan}/viewer/camera.cpp | 0 src/{ => gproshan}/viewer/che_viewer.cpp | 0 src/{ => gproshan}/viewer/frame.cpp | 0 src/{ => gproshan}/viewer/shader.cpp | 0 src/{ => gproshan}/viewer/viewer.cpp | 0 {include => src}/imgui/CMakeLists.txt | 2 + src/imgui/LICENSE.txt | 21 +++++ {include => src}/imgui/imgui.cpp | 0 {include => src}/imgui/imgui_demo.cpp | 0 {include => src}/imgui/imgui_draw.cpp | 0 {include => src}/imgui/imgui_impl_glfw.cpp | 0 {include => src}/imgui/imgui_impl_opengl3.cpp | 0 {include => src}/imgui/imgui_tables.cpp | 0 {include => src}/imgui/imgui_widgets.cpp | 0 70 files changed, 116 insertions(+), 91 deletions(-) create mode 100644 src/gproshan/CMakeLists.txt rename src/{ => gproshan}/app_viewer.cpp (100%) rename src/{ => gproshan}/features/descriptor.cpp (100%) rename src/{ => gproshan}/features/key_components.cpp (100%) rename src/{ => gproshan}/features/key_points.cpp (100%) rename src/{ => gproshan}/features/shot.cpp (100%) rename src/{ => gproshan}/geodesics/dijkstra.cpp (100%) rename src/{ => gproshan}/geodesics/geodesics.cpp (100%) rename src/{ => gproshan}/geodesics/geodesics_ptp.cpp (100%) rename src/{ => gproshan}/geodesics/geodesics_ptp.cu (100%) rename src/{ => gproshan}/geodesics/geodesics_ptp_coalescence.cu (100%) rename src/{ => gproshan}/geodesics/heat_method.cpp (100%) rename src/{ => gproshan}/geodesics/heat_method.cu (100%) rename src/{ => gproshan}/geodesics/sampling.cpp (100%) rename src/{ => gproshan}/geodesics/test_geodesics_ptp.cpp (100%) rename src/{ => gproshan}/geodesics/test_geodesics_ptp.cu (100%) rename src/{ => gproshan}/geodesics/test_geodesics_ptp_coalescence.cu (100%) rename src/{ => gproshan}/geometry/convex_hull.cpp (100%) rename src/{ => gproshan}/laplacian/fairing.cpp (100%) rename src/{ => gproshan}/laplacian/fairing_spectral.cpp (100%) rename src/{ => gproshan}/laplacian/fairing_taubin.cpp (100%) rename src/{ => gproshan}/laplacian/laplacian.cpp (100%) rename src/{ => gproshan}/mdict/basis.cpp (100%) rename src/{ => gproshan}/mdict/basis_cosine.cpp (100%) rename src/{ => gproshan}/mdict/basis_dct.cpp (100%) rename src/{ => gproshan}/mdict/image_denoising.cpp (100%) rename src/{ => gproshan}/mdict/mdict.cpp (100%) rename src/{ => gproshan}/mdict/msparse_coding.cpp (100%) rename src/{ => gproshan}/mdict/patch.cpp (100%) rename src/{ => gproshan}/mesh/che.cpp (100%) rename src/{ => gproshan}/mesh/che.cu (100%) rename src/{ => gproshan}/mesh/che_fill_hole.cpp (100%) rename src/{ => gproshan}/mesh/che_img.cpp (100%) rename src/{ => gproshan}/mesh/che_obj.cpp (100%) rename src/{ => gproshan}/mesh/che_off.cpp (100%) rename src/{ => gproshan}/mesh/che_ply.cpp (100%) rename src/{ => gproshan}/mesh/che_poisson.cpp (100%) rename src/{ => gproshan}/mesh/che_pts.cpp (100%) rename src/{ => gproshan}/mesh/che_ptx.cpp (100%) rename src/{ => gproshan}/mesh/che_sphere.cpp (100%) rename src/{ => gproshan}/mesh/che_xyz.cpp (100%) rename src/{ => gproshan}/mesh/kdtree.cpp (100%) rename src/{ => gproshan}/mesh/quaternion.cpp (100%) rename src/{ => gproshan}/mesh/simplification.cpp (100%) rename src/{ => gproshan}/mesh/vertex.cpp (100%) rename src/{ => gproshan}/mesh/vertex.cu (100%) rename src/{ => gproshan}/raytracing/raytracing.cpp (100%) rename src/{ => gproshan}/raytracing/rt_embree.cpp (100%) rename src/{ => gproshan}/raytracing/rt_optix.cpp (100%) rename src/{ => gproshan}/raytracing/rt_optix.cu (100%) rename src/{ => gproshan}/scenes/scanner.cpp (100%) rename src/{ => gproshan}/util.cpp (100%) rename src/{ => gproshan}/viewer/camera.cpp (100%) rename src/{ => gproshan}/viewer/che_viewer.cpp (100%) rename src/{ => gproshan}/viewer/frame.cpp (100%) rename src/{ => gproshan}/viewer/shader.cpp (100%) rename src/{ => gproshan}/viewer/viewer.cpp (100%) rename {include => src}/imgui/CMakeLists.txt (75%) create mode 100644 src/imgui/LICENSE.txt rename {include => src}/imgui/imgui.cpp (100%) rename {include => src}/imgui/imgui_demo.cpp (100%) rename {include => src}/imgui/imgui_draw.cpp (100%) rename {include => src}/imgui/imgui_impl_glfw.cpp (100%) rename {include => src}/imgui/imgui_impl_opengl3.cpp (100%) rename {include => src}/imgui/imgui_tables.cpp (100%) rename {include => src}/imgui/imgui_widgets.cpp (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1f1d85..02b88f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ Version History --------------- -### gproshan 3.0.0 +### gproshan 3.14 +- Exporting gproshan as cmake library, use find_package(gproshan) in your project. - Added Intel Embree as default ray tracing library, for ray casting operations and as rendering option with shadows. - Adding Scenes module, virtual point cloud scanners and point cloud normals computation for 3D scenes. - Added render option using [OptiX](https://developer.nvidia.com/optix) ray tracing mesh, shadows. diff --git a/CMakeLists.txt b/CMakeLists.txt index 432fd06f..72aebd1a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.22) -project(gproshan VERSION 3.0) +project(gproshan VERSION 3.14) list(APPEND CMAKE_MODULE_PATH "${gproshan_SOURCE_DIR}/cmake") @@ -12,11 +12,11 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) -#add_definitions(-DGPROSHAN_FLOAT) +add_definitions(-DGPROSHAN_FLOAT) add_definitions(-DGPROSHAN_LOG) -find_package(CUDAToolkit 11.4) +find_package(CUDAToolkit 11.7) if(CUDAToolkit_FOUND) enable_language(CUDA) set(CMAKE_CUDA_STANDARD 17) @@ -29,7 +29,7 @@ if(CUDAToolkit_FOUND) add_definitions(-DGPROSHAN_CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) - find_package(OptiX 7.3) + find_package(OptiX 7.4) if(OptiX_INCLUDE) add_definitions(-DGPROSHAN_OPTIX) include_directories(SYSTEM ${OptiX_INCLUDE}) @@ -60,12 +60,6 @@ include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) -add_subdirectory(include/imgui) -include_directories(SYSTEM ${gproshan_SOURCE_DIR}/imgui) - - -include_directories(${gproshan_SOURCE_DIR}/include/) - -add_subdirectory(src) # gproshan library -add_subdirectory(apps) # gproshan executables +add_subdirectory(src) # gproshan library +add_subdirectory(apps) # gproshan executables diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 203c8eed..990bdb53 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -1,10 +1,13 @@ +include_directories(${gproshan_SOURCE_DIR}/include/) + + add_executable(test_gproshan gproshan.cpp) set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) target_link_libraries(test_gproshan gproshan) if(OptiX_INCLUDE) find_package(CUDA) - cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) + cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) list(GET ptx_sources 0 ptx_file) add_custom_target( ptx_tmp DEPENDS ${ptx_file} COMMAND ${CMAKE_COMMAND} -E copy diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 03575503..4c9b0559 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,77 +1,3 @@ -add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") - - -FILE(GLOB_RECURSE cpp_sources ${gproshan_SOURCE_DIR}/src/*.cpp) - -add_library(gproshan SHARED ${cpp_sources}) - -target_link_libraries(gproshan embree) -target_link_libraries(gproshan OpenMP::OpenMP_CXX) -target_link_libraries(gproshan GLEW::GLEW) -target_link_libraries(gproshan glfw) -target_link_libraries(gproshan ${X11_X11_LIB}) -target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) -target_link_libraries(gproshan CGAL::CGAL) -target_link_libraries(gproshan ${OptiX_LIBRARY}) -target_link_libraries(gproshan imgui) - -if(CUDAToolkit_FOUND) - FILE(GLOB_RECURSE cu_sources ${gproshan_SOURCE_DIR}/src/*.cu) - list(REMOVE_ITEM cu_sources ${gproshan_SOURCE_DIR}/src/raytracing/rt_optix.cu) - - add_library(gproshan_cuda SHARED ${cu_sources}) - - target_link_libraries(gproshan_cuda CUDA::cudart) - target_link_libraries(gproshan_cuda CUDA::cuda_driver) - target_link_libraries(gproshan_cuda CUDA::cublas) - target_link_libraries(gproshan_cuda CUDA::cusolver) - target_link_libraries(gproshan_cuda CUDA::cusparse) - - target_link_libraries(gproshan gproshan_cuda) -endif(CUDAToolkit_FOUND) - - - -set(installable_libs gproshan imgui) -if(TARGET gproshan_cuda) - list(APPEND installable_libs gproshan_cuda) -endif() - -install(TARGETS ${installable_libs} - EXPORT gproshanTargets - DESTINATION lib - ) - -install(EXPORT gproshanTargets - FILE gproshanTargets.cmake - DESTINATION lib/cmake/gproshan - ) - -set(INCLUDE_INSTALL_DIR "${gproshan_SOURCE_DIR}/include/") - -include(CMakePackageConfigHelpers) - -configure_package_config_file( - ${gproshan_SOURCE_DIR}/gproshanConfig.cmake.in - "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake" - INSTALL_DESTINATION "lib/cmake/gproshan" - PATH_VARS INCLUDE_INSTALL_DIR - ) - -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake" - VERSION "${gproshan_VERSION_MAJOR}.${gproshan_VERSION_MINOR}" - COMPATIBILITY AnyNewerVersion - ) - -install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake - ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake - DESTINATION lib/cmake/gproshan - ) - -export( EXPORT gproshanTargets - FILE "${CMAKE_CURRENT_BINARY_DIR}/gproshanTargets.cmake" - ) +add_subdirectory(imgui) +add_subdirectory(gproshan) diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt new file mode 100644 index 00000000..36205c87 --- /dev/null +++ b/src/gproshan/CMakeLists.txt @@ -0,0 +1,78 @@ +add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") + +include_directories(${gproshan_SOURCE_DIR}/include/) + +FILE(GLOB_RECURSE cpp_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) + +add_library(gproshan SHARED ${cpp_sources}) + +target_link_libraries(gproshan embree) +target_link_libraries(gproshan OpenMP::OpenMP_CXX) +target_link_libraries(gproshan GLEW::GLEW) +target_link_libraries(gproshan glfw) +target_link_libraries(gproshan ${X11_X11_LIB}) +target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) +target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan CGAL::CGAL) +target_link_libraries(gproshan ${OptiX_LIBRARY}) +target_link_libraries(gproshan imgui) + +if(CUDAToolkit_FOUND) + FILE(GLOB_RECURSE cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cu) + list(REMOVE_ITEM cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/raytracing/rt_optix.cu) + + add_library(gproshan_cuda SHARED ${cu_sources}) + + target_link_libraries(gproshan_cuda CUDA::cudart) + target_link_libraries(gproshan_cuda CUDA::cuda_driver) + target_link_libraries(gproshan_cuda CUDA::cublas) + target_link_libraries(gproshan_cuda CUDA::cusolver) + target_link_libraries(gproshan_cuda CUDA::cusparse) + + target_link_libraries(gproshan gproshan_cuda) +endif(CUDAToolkit_FOUND) + + + +set(installable_libs gproshan imgui) +if(TARGET gproshan_cuda) + list(APPEND installable_libs gproshan_cuda) +endif() + +install(TARGETS ${installable_libs} + EXPORT gproshanTargets + DESTINATION lib + ) + +install(EXPORT gproshanTargets + FILE gproshanTargets.cmake + DESTINATION lib/cmake/gproshan + ) + +set(INCLUDE_INSTALL_DIR "${gproshan_SOURCE_DIR}/include/") + +include(CMakePackageConfigHelpers) + +configure_package_config_file( + ${gproshan_SOURCE_DIR}/gproshanConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake" + INSTALL_DESTINATION "lib/cmake/gproshan" + PATH_VARS INCLUDE_INSTALL_DIR + ) + +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake" + VERSION "${gproshan_VERSION_MAJOR}.${gproshan_VERSION_MINOR}" + COMPATIBILITY AnyNewerVersion + ) + +install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake + DESTINATION lib/cmake/gproshan + ) + +export( EXPORT gproshanTargets + FILE "${CMAKE_CURRENT_BINARY_DIR}/gproshanTargets.cmake" + ) + diff --git a/src/app_viewer.cpp b/src/gproshan/app_viewer.cpp similarity index 100% rename from src/app_viewer.cpp rename to src/gproshan/app_viewer.cpp diff --git a/src/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp similarity index 100% rename from src/features/descriptor.cpp rename to src/gproshan/features/descriptor.cpp diff --git a/src/features/key_components.cpp b/src/gproshan/features/key_components.cpp similarity index 100% rename from src/features/key_components.cpp rename to src/gproshan/features/key_components.cpp diff --git a/src/features/key_points.cpp b/src/gproshan/features/key_points.cpp similarity index 100% rename from src/features/key_points.cpp rename to src/gproshan/features/key_points.cpp diff --git a/src/features/shot.cpp b/src/gproshan/features/shot.cpp similarity index 100% rename from src/features/shot.cpp rename to src/gproshan/features/shot.cpp diff --git a/src/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp similarity index 100% rename from src/geodesics/dijkstra.cpp rename to src/gproshan/geodesics/dijkstra.cpp diff --git a/src/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp similarity index 100% rename from src/geodesics/geodesics.cpp rename to src/gproshan/geodesics/geodesics.cpp diff --git a/src/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp similarity index 100% rename from src/geodesics/geodesics_ptp.cpp rename to src/gproshan/geodesics/geodesics_ptp.cpp diff --git a/src/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu similarity index 100% rename from src/geodesics/geodesics_ptp.cu rename to src/gproshan/geodesics/geodesics_ptp.cu diff --git a/src/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu similarity index 100% rename from src/geodesics/geodesics_ptp_coalescence.cu rename to src/gproshan/geodesics/geodesics_ptp_coalescence.cu diff --git a/src/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp similarity index 100% rename from src/geodesics/heat_method.cpp rename to src/gproshan/geodesics/heat_method.cpp diff --git a/src/geodesics/heat_method.cu b/src/gproshan/geodesics/heat_method.cu similarity index 100% rename from src/geodesics/heat_method.cu rename to src/gproshan/geodesics/heat_method.cu diff --git a/src/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp similarity index 100% rename from src/geodesics/sampling.cpp rename to src/gproshan/geodesics/sampling.cpp diff --git a/src/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp similarity index 100% rename from src/geodesics/test_geodesics_ptp.cpp rename to src/gproshan/geodesics/test_geodesics_ptp.cpp diff --git a/src/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu similarity index 100% rename from src/geodesics/test_geodesics_ptp.cu rename to src/gproshan/geodesics/test_geodesics_ptp.cu diff --git a/src/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu similarity index 100% rename from src/geodesics/test_geodesics_ptp_coalescence.cu rename to src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu diff --git a/src/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp similarity index 100% rename from src/geometry/convex_hull.cpp rename to src/gproshan/geometry/convex_hull.cpp diff --git a/src/laplacian/fairing.cpp b/src/gproshan/laplacian/fairing.cpp similarity index 100% rename from src/laplacian/fairing.cpp rename to src/gproshan/laplacian/fairing.cpp diff --git a/src/laplacian/fairing_spectral.cpp b/src/gproshan/laplacian/fairing_spectral.cpp similarity index 100% rename from src/laplacian/fairing_spectral.cpp rename to src/gproshan/laplacian/fairing_spectral.cpp diff --git a/src/laplacian/fairing_taubin.cpp b/src/gproshan/laplacian/fairing_taubin.cpp similarity index 100% rename from src/laplacian/fairing_taubin.cpp rename to src/gproshan/laplacian/fairing_taubin.cpp diff --git a/src/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp similarity index 100% rename from src/laplacian/laplacian.cpp rename to src/gproshan/laplacian/laplacian.cpp diff --git a/src/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp similarity index 100% rename from src/mdict/basis.cpp rename to src/gproshan/mdict/basis.cpp diff --git a/src/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp similarity index 100% rename from src/mdict/basis_cosine.cpp rename to src/gproshan/mdict/basis_cosine.cpp diff --git a/src/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp similarity index 100% rename from src/mdict/basis_dct.cpp rename to src/gproshan/mdict/basis_dct.cpp diff --git a/src/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp similarity index 100% rename from src/mdict/image_denoising.cpp rename to src/gproshan/mdict/image_denoising.cpp diff --git a/src/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp similarity index 100% rename from src/mdict/mdict.cpp rename to src/gproshan/mdict/mdict.cpp diff --git a/src/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp similarity index 100% rename from src/mdict/msparse_coding.cpp rename to src/gproshan/mdict/msparse_coding.cpp diff --git a/src/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp similarity index 100% rename from src/mdict/patch.cpp rename to src/gproshan/mdict/patch.cpp diff --git a/src/mesh/che.cpp b/src/gproshan/mesh/che.cpp similarity index 100% rename from src/mesh/che.cpp rename to src/gproshan/mesh/che.cpp diff --git a/src/mesh/che.cu b/src/gproshan/mesh/che.cu similarity index 100% rename from src/mesh/che.cu rename to src/gproshan/mesh/che.cu diff --git a/src/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp similarity index 100% rename from src/mesh/che_fill_hole.cpp rename to src/gproshan/mesh/che_fill_hole.cpp diff --git a/src/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp similarity index 100% rename from src/mesh/che_img.cpp rename to src/gproshan/mesh/che_img.cpp diff --git a/src/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp similarity index 100% rename from src/mesh/che_obj.cpp rename to src/gproshan/mesh/che_obj.cpp diff --git a/src/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp similarity index 100% rename from src/mesh/che_off.cpp rename to src/gproshan/mesh/che_off.cpp diff --git a/src/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp similarity index 100% rename from src/mesh/che_ply.cpp rename to src/gproshan/mesh/che_ply.cpp diff --git a/src/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp similarity index 100% rename from src/mesh/che_poisson.cpp rename to src/gproshan/mesh/che_poisson.cpp diff --git a/src/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp similarity index 100% rename from src/mesh/che_pts.cpp rename to src/gproshan/mesh/che_pts.cpp diff --git a/src/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp similarity index 100% rename from src/mesh/che_ptx.cpp rename to src/gproshan/mesh/che_ptx.cpp diff --git a/src/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp similarity index 100% rename from src/mesh/che_sphere.cpp rename to src/gproshan/mesh/che_sphere.cpp diff --git a/src/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp similarity index 100% rename from src/mesh/che_xyz.cpp rename to src/gproshan/mesh/che_xyz.cpp diff --git a/src/mesh/kdtree.cpp b/src/gproshan/mesh/kdtree.cpp similarity index 100% rename from src/mesh/kdtree.cpp rename to src/gproshan/mesh/kdtree.cpp diff --git a/src/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp similarity index 100% rename from src/mesh/quaternion.cpp rename to src/gproshan/mesh/quaternion.cpp diff --git a/src/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp similarity index 100% rename from src/mesh/simplification.cpp rename to src/gproshan/mesh/simplification.cpp diff --git a/src/mesh/vertex.cpp b/src/gproshan/mesh/vertex.cpp similarity index 100% rename from src/mesh/vertex.cpp rename to src/gproshan/mesh/vertex.cpp diff --git a/src/mesh/vertex.cu b/src/gproshan/mesh/vertex.cu similarity index 100% rename from src/mesh/vertex.cu rename to src/gproshan/mesh/vertex.cu diff --git a/src/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp similarity index 100% rename from src/raytracing/raytracing.cpp rename to src/gproshan/raytracing/raytracing.cpp diff --git a/src/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp similarity index 100% rename from src/raytracing/rt_embree.cpp rename to src/gproshan/raytracing/rt_embree.cpp diff --git a/src/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp similarity index 100% rename from src/raytracing/rt_optix.cpp rename to src/gproshan/raytracing/rt_optix.cpp diff --git a/src/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu similarity index 100% rename from src/raytracing/rt_optix.cu rename to src/gproshan/raytracing/rt_optix.cu diff --git a/src/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp similarity index 100% rename from src/scenes/scanner.cpp rename to src/gproshan/scenes/scanner.cpp diff --git a/src/util.cpp b/src/gproshan/util.cpp similarity index 100% rename from src/util.cpp rename to src/gproshan/util.cpp diff --git a/src/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp similarity index 100% rename from src/viewer/camera.cpp rename to src/gproshan/viewer/camera.cpp diff --git a/src/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp similarity index 100% rename from src/viewer/che_viewer.cpp rename to src/gproshan/viewer/che_viewer.cpp diff --git a/src/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp similarity index 100% rename from src/viewer/frame.cpp rename to src/gproshan/viewer/frame.cpp diff --git a/src/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp similarity index 100% rename from src/viewer/shader.cpp rename to src/gproshan/viewer/shader.cpp diff --git a/src/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp similarity index 100% rename from src/viewer/viewer.cpp rename to src/gproshan/viewer/viewer.cpp diff --git a/include/imgui/CMakeLists.txt b/src/imgui/CMakeLists.txt similarity index 75% rename from include/imgui/CMakeLists.txt rename to src/imgui/CMakeLists.txt index b8fd04b7..3e14c582 100644 --- a/include/imgui/CMakeLists.txt +++ b/src/imgui/CMakeLists.txt @@ -1,5 +1,7 @@ FILE(GLOB_RECURSE imgui_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) +include_directories(${gproshan_SOURCE_DIR}/include/imgui/) + add_library(imgui SHARED ${imgui_sources}) target_link_libraries(imgui GLEW::GLEW) target_link_libraries(imgui glfw) diff --git a/src/imgui/LICENSE.txt b/src/imgui/LICENSE.txt new file mode 100644 index 00000000..780533dc --- /dev/null +++ b/src/imgui/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014-2021 Omar Cornut + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/include/imgui/imgui.cpp b/src/imgui/imgui.cpp similarity index 100% rename from include/imgui/imgui.cpp rename to src/imgui/imgui.cpp diff --git a/include/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp similarity index 100% rename from include/imgui/imgui_demo.cpp rename to src/imgui/imgui_demo.cpp diff --git a/include/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp similarity index 100% rename from include/imgui/imgui_draw.cpp rename to src/imgui/imgui_draw.cpp diff --git a/include/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp similarity index 100% rename from include/imgui/imgui_impl_glfw.cpp rename to src/imgui/imgui_impl_glfw.cpp diff --git a/include/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp similarity index 100% rename from include/imgui/imgui_impl_opengl3.cpp rename to src/imgui/imgui_impl_opengl3.cpp diff --git a/include/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp similarity index 100% rename from include/imgui/imgui_tables.cpp rename to src/imgui/imgui_tables.cpp diff --git a/include/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp similarity index 100% rename from include/imgui/imgui_widgets.cpp rename to src/imgui/imgui_widgets.cpp From 264b0cfbc16773a03e93ddfac5bf5c58c1b35ffe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 12:29:18 +0200 Subject: [PATCH 0646/1018] updated README.md --- README.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f27ce455..d90d8306 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework +## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework [![Build](https://github.com/larc/gproshan_dev/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/larc/gproshan_dev/actions/workflows/build.yml) [![DOI](https://zenodo.org/badge/88686093.svg)](https://zenodo.org/badge/latestdoi/88686093) -![](https://raw.githubusercontent.com/larc/gproshan/master/gproshan.png) +![](https://raw.githubusercontent.com/larc/gproshan/master/gproshan.png) This framework integrates some algorithms and contributions focus on the areas of computer graphics, geometry processing and computational geometry. @@ -55,6 +55,29 @@ Intel Embree is a collection of high performance ray tracing kernels that helps brew install embree +## Using gproshan in your project + +To use gproshan as library in your project you need to execute the command to install it: + + sudo make install + +then you will be able to find it in your cmake project: + + find_package(gproshan REQUIRED) + include_directories(SYSTEM ${gproshan_INCLUDE_DIR}) + target_link_libraries(your_target gproshan) + +An example of a code using gproshan: + + #include + + int main(int nargs, const char ** args) + { + gproshan::app_viewer app; + return app.main(nargs, args); + } + + ## Contributions ### CHE implementation From 8790beeebe7aea2e9b1c587393017d893ce5152b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 28 May 2022 12:38:41 +0200 Subject: [PATCH 0647/1018] updating README.md --- README.md | 4 ++++ apps/CMakeLists.txt | 12 ------------ include/imgui/imgui_impl_opengl3_loader.h | 2 +- src/gproshan/CMakeLists.txt | 13 +++++++++++++ 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d90d8306..68111b0d 100644 --- a/README.md +++ b/README.md @@ -63,12 +63,15 @@ To use gproshan as library in your project you need to execute the command to in then you will be able to find it in your cmake project: + ```cmake find_package(gproshan REQUIRED) include_directories(SYSTEM ${gproshan_INCLUDE_DIR}) target_link_libraries(your_target gproshan) + ``` An example of a code using gproshan: + ```cpp #include int main(int nargs, const char ** args) @@ -76,6 +79,7 @@ An example of a code using gproshan: gproshan::app_viewer app; return app.main(nargs, args); } + ``` ## Contributions diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 990bdb53..7b2ecbb0 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -5,18 +5,6 @@ add_executable(test_gproshan gproshan.cpp) set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) target_link_libraries(test_gproshan gproshan) -if(OptiX_INCLUDE) - find_package(CUDA) - cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) - list(GET ptx_sources 0 ptx_file) - add_custom_target( ptx_tmp DEPENDS ${ptx_file} - COMMAND ${CMAKE_COMMAND} -E copy - ${ptx_file} - ${gproshan_SOURCE_DIR}/tmp/rt_optix.ptx - ) - add_dependencies(test_gproshan ptx_tmp) -endif(OptiX_INCLUDE) - add_executable(test_geodesics test_geodesics.cpp) target_link_libraries(test_geodesics gproshan) diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 8452301e..e38d210c 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -110,7 +110,7 @@ extern "C" { ** included as . ** ** glcorearb.h includes only APIs in the latest OpenGL core profile -** implementation together with APIs in newer ARB extensions which +** implementation together with APIs in newer ARB extensions which ** can be supported by the core profile. It does not, and never will ** include functionality removed from the core profile, such as ** fixed-function vertex and fragment processing. diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 36205c87..2d6898fd 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -33,6 +33,19 @@ if(CUDAToolkit_FOUND) endif(CUDAToolkit_FOUND) +if(OptiX_INCLUDE) + find_package(CUDA) + cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) + list(GET ptx_sources 0 ptx_file) + add_custom_target( ptx_tmp DEPENDS ${ptx_file} + COMMAND ${CMAKE_COMMAND} -E copy + ${ptx_file} + ${gproshan_SOURCE_DIR}/tmp/rt_optix.ptx + ) + add_dependencies(gproshan ptx_tmp) +endif(OptiX_INCLUDE) + + set(installable_libs gproshan imgui) if(TARGET gproshan_cuda) From 4b962f61e3c7f49c1251c531c2f6530fe8ac0773 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Sat, 28 May 2022 12:41:10 +0200 Subject: [PATCH 0648/1018] Update README.md --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 68111b0d..8be8aaf0 100644 --- a/README.md +++ b/README.md @@ -63,23 +63,23 @@ To use gproshan as library in your project you need to execute the command to in then you will be able to find it in your cmake project: - ```cmake - find_package(gproshan REQUIRED) - include_directories(SYSTEM ${gproshan_INCLUDE_DIR}) - target_link_libraries(your_target gproshan) - ``` +```cmake +find_package(gproshan REQUIRED) +include_directories(SYSTEM ${gproshan_INCLUDE_DIR}) +target_link_libraries(your_target gproshan) +``` An example of a code using gproshan: - ```cpp - #include +```cpp +#include - int main(int nargs, const char ** args) - { - gproshan::app_viewer app; - return app.main(nargs, args); - } - ``` +int main(int nargs, const char ** args) +{ + gproshan::app_viewer app; + return app.main(nargs, args); +} +``` ## Contributions From fb5c07795a39c37932dee85c25595ee837a9925d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 May 2022 20:38:48 +0200 Subject: [PATCH 0649/1018] adding config.h.in file to set gproshan defines --- .gitignore | 2 ++ CMakeLists.txt | 26 +++++++++++++++++--------- include/gproshan/config.h.in | 7 +++++++ include/gproshan/include.h | 2 ++ include/gproshan/raytracing/rt_optix.h | 15 ++++++++------- src/gproshan/CMakeLists.txt | 2 -- src/gproshan/raytracing/rt_optix.cpp | 17 +++++++++-------- src/gproshan/viewer/viewer.cpp | 2 +- 8 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 include/gproshan/config.h.in diff --git a/.gitignore b/.gitignore index 1b9d9ba8..05aa84b5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ tmp/* *.vscode/* *.DS_Store + +include/gproshan/config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 72aebd1a..b0ce9fe7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,13 +12,13 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) -add_definitions(-DGPROSHAN_FLOAT) -add_definitions(-DGPROSHAN_LOG) find_package(CUDAToolkit 11.7) if(CUDAToolkit_FOUND) enable_language(CUDA) + include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) + set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) @@ -26,17 +26,25 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) - add_definitions(-DGPROSHAN_CUDA) - include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) - find_package(OptiX 7.4) - if(OptiX_INCLUDE) - add_definitions(-DGPROSHAN_OPTIX) - include_directories(SYSTEM ${OptiX_INCLUDE}) - endif(OptiX_INCLUDE) + include_directories(SYSTEM ${OptiX_INCLUDE}) endif(CUDAToolkit_FOUND) +set(GPROSHAN_DIR "${gproshan_SOURCE_DIR}") + +option(GPROSHAN_LOG "Enables logging gproshan messages." ON) +option(GPROSHAN_FLOAT "Compile gproshan with float precision." ON) + +set(GPROSHAN_CUDA ${CUDAToolkit_FOUND}) +set(GPROSHAN_OPTIX ${OptiX_INCLUDE}) + + +configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in + ${gproshan_SOURCE_DIR}/include/gproshan/config.h + ) + + find_package(embree 3.13 REQUIRED) find_package(Threads REQUIRED) find_package(OpenMP REQUIRED) diff --git a/include/gproshan/config.h.in b/include/gproshan/config.h.in new file mode 100644 index 00000000..777c66f6 --- /dev/null +++ b/include/gproshan/config.h.in @@ -0,0 +1,7 @@ +#cmakedefine GPROSHAN_FLOAT +#cmakedefine GPROSHAN_LOG +#cmakedefine GPROSHAN_CUDA +#cmakedefine GPROSHAN_OPTIX + +#cmakedefine GPROSHAN_DIR "@GPROSHAN_DIR@" + diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 7b029891..8a3cffd1 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -1,6 +1,8 @@ #ifndef INCLUDE_H #define INCLUDE_H +#include + #include #include #include diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index d9cecc09..e1e2fce7 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -1,5 +1,3 @@ -#ifdef GPROSHAN_OPTIX - #ifndef RT_OPTIX_H #define RT_OPTIX_H @@ -7,6 +5,9 @@ #include #include + +#ifdef GPROSHAN_OPTIX + #include #include #include @@ -51,7 +52,7 @@ class optix : public raytracing void * as_buffer = nullptr; public: - optix(const std::vector & meshes); + optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); void render(glm::vec4 * img, @@ -70,14 +71,14 @@ class optix : public raytracing void create_hitgroup_programs(); void create_pipeline(); void build_sbt(); - OptixTraversableHandle build_as(const std::vector & meshes); - void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh); + OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); + void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const glm::mat4 & model_mat); }; } // namespace gproshan -#endif // RT_OPTIX_H - #endif // GPROSHAN_OPTIX +#endif // RT_OPTIX_H + diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 2d6898fd..f0b3d42b 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -1,5 +1,3 @@ -add_compile_definitions(GPROSHAN_DIR="${gproshan_SOURCE_DIR}") - include_directories(${gproshan_SOURCE_DIR}/include/) FILE(GLOB_RECURSE cpp_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index d9a7d44e..fec3f1c6 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -1,7 +1,8 @@ -#ifdef GPROSHAN_OPTIX - #include + +#ifdef GPROSHAN_OPTIX + #include #include @@ -45,7 +46,7 @@ void optix_log(unsigned int level, const char * tag, const char * message, void fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } -optix::optix(const std::vector & meshes) +optix::optix(const std::vector & meshes, const std::vector & model_mats) { optixInit(); @@ -93,7 +94,7 @@ optix::optix(const std::vector & meshes) create_hitgroup_programs(); // build as - render_params.traversable = build_as(meshes); + render_params.traversable = build_as(meshes, model_mats); // create pipeline create_pipeline(); @@ -337,7 +338,7 @@ void optix::build_sbt() sbt.hitgroupRecordCount = hitgroup_records.size(); } -OptixTraversableHandle optix::build_as(const std::vector & meshes) +OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) { OptixTraversableHandle optix_as_handle = {}; @@ -346,7 +347,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) std::vector optix_trig_flags(meshes.size()); for(index_t i = 0; i < meshes.size(); ++i) - add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i]); + add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i], model_mats[i]); OptixAccelBuildOptions optix_accel_opt = {}; optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_NONE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION; @@ -414,7 +415,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes) return optix_as_handle; } -void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh) +void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const glm::mat4 & model_mat) { CHE * dd_m, * d_m; CHE h_m(mesh); @@ -430,7 +431,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; - optix_mesh.triangleArray.vertexStrideInBytes = sizeof(vertex); + optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); optix_mesh.triangleArray.numVertices = mesh->n_vertices; optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7f169160..1595c458 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -683,7 +683,7 @@ bool viewer::m_setup_raytracing(viewer * view) #ifdef GPROSHAN_OPTIX delete view->rt_optix; TIC(time); - view->rt_optix = new rt::optix({mesh}); + view->rt_optix = new rt::optix({mesh}, {mesh.model_mat}); TOC(time); sprintf(view->status_message, "build optix in %.3fs", time); #endif // GPROSHAN_OPTIX From 808fbf6f3e6771cbd5d650a6ff19da68e2b424ac Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 May 2022 21:14:15 +0200 Subject: [PATCH 0650/1018] rt_optix: adding model_ mat --- CMakeLists.txt | 4 +++- include/gproshan/raytracing/rt_optix_params.h | 11 +++++++---- src/gproshan/raytracing/rt_optix.cu | 5 ----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b0ce9fe7..14f8fcee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,9 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) find_package(OptiX 7.4) - include_directories(SYSTEM ${OptiX_INCLUDE}) + if(OptiX_INCLUDE) + include_directories(SYSTEM ${OptiX_INCLUDE}) + endif(OptiX_INCLUDE) endif(CUDAToolkit_FOUND) diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 362c2a9f..1e8249e4 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -1,9 +1,12 @@ -#ifdef GPROSHAN_OPTIX - #ifndef RT_OPTIX_PARAMS_H #define RT_OPTIX_PARAMS_H +#include + + +#ifdef GPROSHAN_OPTIX + #include @@ -30,7 +33,7 @@ struct launch_params } // namespace gproshan -#endif // RT_OPTIX_PARAMS_H - #endif // GPROSHAN_OPTIX +#endif // RT_OPTIX_PARAMS_H + diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 37d1b05b..609086e2 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -1,6 +1,3 @@ -#ifdef GPROSHAN_OPTIX - - #include #include #include @@ -187,5 +184,3 @@ extern "C" __global__ void __raygen__render_frame() } // namespace gproshan -#endif // GPROSHAN_OPTIX - From 074179e2ec81c5b872dcfb9d5ee017d49462208c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 May 2022 21:48:34 +0200 Subject: [PATCH 0651/1018] cleaning up scanner branch --- apps/test_scanner.cpp | 42 ++++++++++++------------ include/gproshan/raytracing/raytracing.h | 6 ++-- include/gproshan/scenes/scanner.h | 3 +- src/gproshan/mesh/che_ptx.cpp | 23 +++---------- src/gproshan/scenes/scanner.cpp | 8 ++--- 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index 577a9b59..f54943b1 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -1,12 +1,13 @@ -#include "mesh/che_ply.h" -#include "mesh/che_ptx.h" -#include "scenes/scanner.h" -#include "raytracing/rt_embree.h" -#include "viewer/include_opengl.h" -#include -#include "glm/glm.hpp" -#include "glm/gtc/matrix_transform.hpp" +#include +#include +#include +#include + #include +#include + +#include +#include void translate(glm::mat4 & model_mat, const gproshan::vertex & p) { @@ -18,8 +19,9 @@ void scale(glm::mat4 & model_mat, const gproshan::real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void normalize_coordinates(gproshan::che_ply * mesh, glm::mat4 & model_mat) +glm::mat4 normalize_coordinates(gproshan::che_ply * mesh) { + glm::mat4 model_mat = glm::mat4(1); gproshan::vertex pmin(INFINITY, INFINITY, INFINITY); gproshan::vertex pmax(0, 0, 0); @@ -39,22 +41,22 @@ void normalize_coordinates(gproshan::che_ply * mesh, glm::mat4 & model_mat) scale(model_mat, 2.0 / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z})); translate(model_mat, - (pmax + pmin) / 2); - + + return model_mat; } int main(int argc, char* argv[]) { - - gproshan_log_var(argc); if(argc < 2 || argc > 7) { std::cerr << "Correct usage: ./apps/test_scanner n_rows n_cols pc_radius ptx_folder jpg_folder" << std::endl; return -1; } - // Fetching the arguments - size_t n_rows = atoi(argv[2]); //512 - size_t n_cols = atoi(argv[3]); //1024 + size_t n_rows = atoi(argv[2]); + size_t n_cols = atoi(argv[3]); + + float pc_radius = atof(argv[4]); char * ptx_folder = argv[5]; char * jpg_folder = argv[6]; @@ -64,12 +66,9 @@ int main(int argc, char* argv[]) gproshan::che_ply * mesh_ply = new gproshan::che_ply(argv[1]); - //Setting up the ray tracing framework gproshan::rt::raytracing * rt_embree; - float pc_radius = atof(argv[4]); //0.01; - glm::mat4 model_mat = glm::mat4(1); - normalize_coordinates(mesh_ply, model_mat); + glm::mat4 model_mat = normalize_coordinates(mesh_ply); rt_embree = new gproshan::rt::embree({mesh_ply},{model_mat}, false, pc_radius); @@ -78,5 +77,6 @@ int main(int argc, char* argv[]) std::string ptx_filename = ptx_folder + mesh_ply->name(); gproshan::che_ptx::write_file(ptx_mesh, ptx_filename, n_rows, n_cols); - return 0; -} \ No newline at end of file + return 0; +} + diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 7c6edf92..995f345c 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -64,7 +64,7 @@ class raytracing virtual index_t cast_ray( const glm::vec3 &,// org, const glm::vec3 &// dir ) { return NIL; }; - + virtual float intersect_depth( const glm::vec3 &,// org, const glm::vec3 &// dir ) { return 0; }; @@ -72,7 +72,7 @@ class raytracing virtual std::tuple cast_ray_intersect_depth( const glm::vec3 & origin,// org, const glm::vec3 & direction// dir ) { return { cast_ray(origin, direction), intersect_depth( origin, direction) }; }; - + protected: virtual glm::vec4 intersect_li( const glm::vec3 &,// org, const glm::vec3 &,// dir, @@ -80,7 +80,7 @@ class raytracing const bool &// flat ) { return glm::vec4(0); }; - + }; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index ce980663..9e401c9e 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -1,10 +1,9 @@ #ifndef SCANNER_H #define SCANNER_H +#include #include -#include "mesh/che.h" - // geometry processing and shape analysis framework // raytracing approach diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 57823e49..fbb84513 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -126,22 +126,8 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ FILE * fp = fopen((file + ".ptx").c_str(), "wb"); assert(fp); - const char * type = sizeof(real_t) == 4 ? "float" : "double"; -/* - -1544 nrows -2064 ncols -0 0 0 -1 0 0 -0 1 0 -0 0 1 -1 0 0 0 -0 1 0 0 -0 0 1 0 -0 0 0 1 -*/ - - fprintf(fp, "%lu \n %lu\n", n_rows, n_cols); + fprintf(fp, "%lu\n", n_rows); + fprintf(fp, "%lu\n", n_cols); fprintf(fp, "0 0 0\n"); fprintf(fp, "1 0 0\n"); fprintf(fp, "0 1 0\n"); @@ -155,11 +141,12 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ { const vertex & v = mesh->gt(i); const rgb_t & c = mesh->rgb(i); - + fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x, (float) v.y, (float) v.z, (float) 0, c.r, c.g, c.b ); - + } } + } // namespace gproshan diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 90b44609..7ed54d6f 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -31,7 +31,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons index_t v_idx; float distance; - + gproshan_log("init"); const real_t delta_phi = (2 * M_PI) / n_rows; @@ -45,11 +45,11 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons // p is the direction of the ray p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; - // quering the closest point in the mesh to the hit point and the distance + // quering the closest point in the mesh to the hit point and the distance auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); //const vertex & c = mesh.pointcloud ? mesh->color(v_idx) : // mesh->shading_color(v_idx, 1.0 - hit.u - hit.v, hit.u, hit.v); - + if(v_idx == NIL) { vertices.push_back( {0, 0, 0} ); @@ -79,7 +79,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons std::thread([](CImg img) { img.display(); }, img).detach(); - + return mesh_ptx; } From 7499dfe5a17c0f05b615e831774fcef9623694b8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 29 May 2022 22:01:08 +0200 Subject: [PATCH 0652/1018] preparing to merge with the dev branch --- apps/test_scanner.cpp | 1 + src/gproshan/app_viewer.cpp | 2 ++ src/gproshan/scenes/scanner.cpp | 8 +++----- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index f54943b1..ef498374 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -9,6 +9,7 @@ #include #include + void translate(glm::mat4 & model_mat, const gproshan::vertex & p) { model_mat = glm::translate(model_mat, glm_vec3(p)); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 45baabe3..708b2299 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -113,6 +113,8 @@ bool app_viewer::process_compute_normals(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); + + return false; } bool app_viewer::process_simulate_scanner(viewer * p_view) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 7ed54d6f..7e9c83da 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -1,6 +1,7 @@ #include -#include + #include +#include using namespace cimg_library; @@ -27,10 +28,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons glm::vec3 cam_pos = glm_vec3(cam); glm::vec3 p, n_v; - const real_t r = 1; - index_t v_idx; - float distance; - + const real_t r = 1; gproshan_log("init"); From 819210d59d350ec0613a669d01744bc17eb620c6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 31 May 2022 00:19:01 +0200 Subject: [PATCH 0653/1018] scenes: refactoring scanner function --- include/gproshan/raytracing/raytracing.h | 37 ++++++++------ include/gproshan/raytracing/rt_embree.h | 6 ++- src/gproshan/mesh/che_ptx.cpp | 1 - src/gproshan/raytracing/rt_embree.cpp | 21 +++++++- src/gproshan/scenes/scanner.cpp | 61 ++++++++++-------------- src/gproshan/viewer/che_viewer.cpp | 2 +- 6 files changed, 72 insertions(+), 56 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 995f345c..a0993460 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -15,6 +15,15 @@ namespace gproshan::rt { +struct hit +{ + index_t idx = NIL; + real_t dist = INFINITY; + vertex color; + vertex normal; +}; + + class raytracing { protected: @@ -61,26 +70,24 @@ class raytracing const glm::vec3 & cam_pos ); - virtual index_t cast_ray( const glm::vec3 &,// org, - const glm::vec3 &// dir - ) { return NIL; }; + virtual hit intersect( const glm::vec3 &, // org + const glm::vec3 & //dir + ) { return hit(); } - virtual float intersect_depth( const glm::vec3 &,// org, - const glm::vec3 &// dir - ) { return 0; }; - - virtual std::tuple cast_ray_intersect_depth( const glm::vec3 & origin,// org, - const glm::vec3 & direction// dir - ) { return { cast_ray(origin, direction), intersect_depth( origin, direction) }; }; + virtual index_t closest_vertex( const glm::vec3 &, // org, + const glm::vec3 & // dir + ) { return NIL; }; protected: - virtual glm::vec4 intersect_li( const glm::vec3 &,// org, - const glm::vec3 &,// dir, - const glm::vec3 &,// light, - const bool &// flat + virtual glm::vec4 intersect_li( const glm::vec3 &, // org, + const glm::vec3 &, // dir, + const glm::vec3 &, // light, + const bool & // flat ) { return glm::vec4(0); }; - + virtual float intersect_depth( const glm::vec3 &, // org, + const glm::vec3 & // dir + ) { return 0; }; }; diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 3274bcfc..3305258e 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -27,8 +27,8 @@ class embree : public raytracing glm::vec3 dir() const; glm::vec3 color(const rt_mesh & mesh) const; glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const; - index_t closest_vertex(const rt_mesh & mesh) const; glm::vec3 position() const; + index_t closest_vertex(const rt_mesh & mesh) const; }; @@ -48,7 +48,9 @@ class embree : public raytracing ); virtual ~embree(); - virtual index_t cast_ray(const glm::vec3 & org, const glm::vec3 & dir); + virtual index_t closest_vertex(const glm::vec3 & org, const glm::vec3 & dir); + virtual hit intersect(const glm::vec3 & org, const glm::vec3 & dir); + protected: bool intersect(ray_hit & r); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index fbb84513..c0fc1f0e 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -143,7 +143,6 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ const rgb_t & c = mesh->rgb(i); fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x, (float) v.y, (float) v.z, (float) 0, c.r, c.g, c.b ); - } } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index ac6a0d2a..5ee34953 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -117,12 +117,31 @@ embree::~embree() rtcReleaseDevice(device); } -index_t embree::cast_ray(const glm::vec3 & org, const glm::vec3 & dir) +index_t embree::closest_vertex(const glm::vec3 & org, const glm::vec3 & dir) { ray_hit r(org, dir); return intersect(r) ? r.closest_vertex(geomID_mesh[r.hit.geomID]) : NIL; } +hit embree::intersect(const glm::vec3 & org, const glm::vec3 & dir) +{ + ray_hit r(org, dir); + if(intersect(r)) + { + const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; + const glm::vec3 & color = r.color(mesh); + const glm::vec3 & normal = r.normal(mesh); + + return { r.closest_vertex(mesh), + r.ray.tfar, + {color.x, color.y, color.z}, + {normal.x, normal.y, normal.z} + }; + } + + return hit(); +} + bool embree::intersect(ray_hit & r) { rtcIntersect1(scene, &intersect_context, &r); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 7e9c83da..e7c71867 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -20,64 +20,53 @@ che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam, const std::string & file_jpg) { - std::vector vertices; - std::vector vertices_color; - vertices.reserve(n_cols * n_rows); - vertices_color.reserve(n_cols * n_rows); + che * mesh_ptx = new che(n_cols * n_rows); glm::vec3 cam_pos = glm_vec3(cam); - glm::vec3 p, n_v; - - const real_t r = 1; - - gproshan_log("init"); const real_t delta_phi = (2 * M_PI) / n_rows; const real_t delta_theta = M_PI / n_cols; - - for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) - for(real_t theta = delta_theta; theta < M_PI - 0.5 * delta_theta + delta_theta; theta += delta_theta) - //for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta_phi; phi += delta_phi) + #pragma omp parallel for + for(index_t i = 0; i < n_rows; ++i) + for(index_t j = 0; j < n_cols; ++j) { - // p is the direction of the ray - p = glm::vec3( r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta) ) - cam_pos; + const index_t & v = i * n_cols + j; - // quering the closest point in the mesh to the hit point and the distance - auto [v_idx, distance] = rt->cast_ray_intersect_depth(cam_pos, glm::normalize(p - cam_pos)); - //const vertex & c = mesh.pointcloud ? mesh->color(v_idx) : - // mesh->shading_color(v_idx, 1.0 - hit.u - hit.v, hit.u, hit.v); + const real_t & phi = i * delta_phi; + const real_t & theta = j * delta_theta; + const glm::vec3 & dir = { sin(theta) * cos(phi), + sin(theta) * sin(phi), + cos(theta) + }; - if(v_idx == NIL) + const hit & h = rt->intersect(cam_pos, dir); + + if(h.idx != NIL) { - vertices.push_back( {0, 0, 0} ); - vertices_color.push_back({0,0,0}); + const glm::vec3 & pos = cam_pos + dir * h.dist; + mesh_ptx->get_vertex(v) = {pos.x, pos.y, pos.z}; + mesh_ptx->normal(v) = h.normal; + mesh_ptx->heatmap(v) = h.dist / M_SQRT2; + mesh_ptx->rgb(v) = { (unsigned char) (h.color.x * 255), + (unsigned char) (h.color.y * 255), + (unsigned char) (h.color.z * 255) + }; } else { - n_v = cam_pos + p * distance; - vertices.push_back( vertex(n_v.x, n_v.y, n_v.z) ); - vertices_color.push_back( mesh->rgb(v_idx) ); + mesh_ptx->rgb(v) = {0, 0, 0}; } } - gproshan_log_var(n_cols * n_rows); - gproshan_log_var(vertices.size()); - - gproshan_log_var(n_cols * n_rows == vertices.size()); - - che * mesh_ptx = new che(vertices.data(), vertices.size(), nullptr, 0); - memcpy(&mesh_ptx->rgb(0), vertices_color.data(), vertices_color.size() * sizeof(che::rgb_t)); - - - CImg img((unsigned char *) vertices_color.data(), 3, n_cols, n_rows); + CImg img((unsigned char *) &mesh_ptx->rgb(0), 3, n_cols, n_rows); img.permute_axes("zycx"); + std::string img_filename = file_jpg + mesh->name() + ".jpg"; img.save(img_filename.c_str()); std::thread([](CImg img) { img.display(); }, img).detach(); - return mesh_ptx; } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 8867351f..26e53942 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -258,7 +258,7 @@ void che_viewer::scale(const real_t & s) void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { glm::vec3 dir = pick_vertex->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); - index_t v = pick_vertex->cast_ray(cam_pos, dir); + index_t v = pick_vertex->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); } From d5260efefda41c148d6927046e806bc8f88068a1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 31 May 2022 22:17:22 +0200 Subject: [PATCH 0654/1018] rt_optix: add model matrix --- README.md | 2 +- src/gproshan/raytracing/rt_optix.cpp | 19 +++++++++++++++---- src/gproshan/raytracing/rt_optix.cu | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8be8aaf0..0754d1cf 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ to generate the documentation in *html* and *LaTeX*. ## Viewer -The viewer is done with modern OpenGL and a GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). The viewer was initially based in the viewer of [https://github.com/dgpdec/course](https://github.com/dgpdec/course). +The viewer is done with modern OpenGL and a GUI using Dear ImGui [https://github.com/ocornut/imgui](https://github.com/ocornut/imgui). ## License diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index fec3f1c6..ce87ba2d 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -349,10 +349,10 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const for(index_t i = 0; i < meshes.size(); ++i) add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i], model_mats[i]); - OptixAccelBuildOptions optix_accel_opt = {}; - optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_NONE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION; - optix_accel_opt.motionOptions.numKeys = 1; - optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; + OptixAccelBuildOptions optix_accel_opt = {}; + optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS | + OPTIX_BUILD_FLAG_ALLOW_COMPACTION; + optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; OptixAccelBufferSizes optix_gas_buffer_size; optixAccelComputeMemoryUsage( optix_context, @@ -424,6 +424,14 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); + float h_transform[12] = { model_mat[0][0], model_mat[1][0], model_mat[2][0], model_mat[3][0], + model_mat[0][1], model_mat[1][1], model_mat[2][1], model_mat[3][1], + model_mat[0][2], model_mat[1][2], model_mat[2][2], model_mat[3][2], + }; + + float * d_transform = nullptr; + cudaMalloc(&d_transform, sizeof(h_transform)); + cudaMemcpy(d_transform, h_transform, sizeof(h_transform), cudaMemcpyHostToDevice); d_vertex_ptr = (CUdeviceptr) dd_m->GT; @@ -440,6 +448,9 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces; optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; + optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; + optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_transform; + optix_trig_flags = 0; optix_mesh.triangleArray.flags = &optix_trig_flags; diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 609086e2..7fc898da 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -57,7 +57,8 @@ extern "C" __global__ void __closesthit__radiance() { const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); - const int primID = optixGetPrimitiveIndex(); + const unsigned int primID = optixGetPrimitiveIndex(); + const int he = primID * che::mtrig; const float u = optixGetTriangleBarycentrics().x; const float v = optixGetTriangleBarycentrics().y; @@ -66,9 +67,16 @@ extern "C" __global__ void __closesthit__radiance() const int b = mesh.VT[he + 1]; const int c = mesh.VT[he + 2]; - const vertex_cu & A = mesh.GT[a]; - const vertex_cu & B = mesh.GT[b]; - const vertex_cu & C = mesh.GT[c]; + OptixTraversableHandle gas = optixGetGASTraversableHandle(); + const unsigned int sbtID = optixGetSbtGASIndex(); + const float time = optixGetRayTime(); + + vertex_cu data[3]; + optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); + + const vertex_cu & A = data[0]; + const vertex_cu & B = data[1]; + const vertex_cu & C = data[2]; const vertex_cu Ng = normalize((B - A) * (C - A)); const vertex_cu normal = render_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; From a43ab4334f72b3533c3706f618de703c2304a01a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Jun 2022 23:54:43 +0200 Subject: [PATCH 0655/1018] viewer: rendering rt in viewport per mesh --- include/gproshan/viewer/che_viewer.h | 6 ++- include/gproshan/viewer/viewer.h | 15 ++----- src/gproshan/app_viewer.cpp | 5 +-- src/gproshan/mdict/msparse_coding.cpp | 2 - src/gproshan/viewer/che_viewer.cpp | 14 ++++-- src/gproshan/viewer/frame.cpp | 3 +- src/gproshan/viewer/viewer.cpp | 62 ++++++++++++--------------- 7 files changed, 51 insertions(+), 56 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 3ce02cd3..242bdf71 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -21,6 +21,8 @@ namespace gproshan { +enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; + class che_viewer { protected: @@ -37,12 +39,14 @@ class che_viewer int vx, vy; ///< viewport positions. std::vector selected; std::vector selected_xyz; - rt::raytracing * pick_vertex = nullptr; + rt::raytracing * rt_embree = nullptr; + rt::raytracing * rt_optix = nullptr; glm::mat4 model_mat = glm::mat4(1); index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl index_t point_size = 1; + index_t render_opt = R_GL; bool point_normals = true; bool render_pointcloud = false; bool render_wireframe = false; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 7cbfec4c..a140f20b 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -11,8 +11,6 @@ #include #include -#include - #include #include @@ -77,14 +75,8 @@ class viewer size_t n_meshes = 0; index_t idx_active_mesh = 0; - enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; - index_t render_opt = R_GL; - frame * rt_frame = nullptr; - rt::raytracing * rt_embree = nullptr; - rt::raytracing * rt_optix = nullptr; - bool rt_restart = false; float bgc = 0; @@ -105,12 +97,13 @@ class viewer viewer(const int & width = 1920, const int & height = 1080); virtual ~viewer(); - bool run(); - che_viewer & active_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); void add_mesh(che * p_mesh); + protected: + virtual bool run(); + private: void info_gl(); void init_gl(); @@ -119,7 +112,7 @@ class viewer void init_glsl(); void render_gl(); - void render_rt(rt::raytracing * rt); + void render_rt(che_viewer & mesh); static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 708b2299..85918c1e 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -132,12 +132,11 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) if(ImGui::Button("Scan")) { - che * ptx_mesh = scanner_ptx(mesh, view->rt_embree, n_rows, n_cols, {0, 0, 0}); - view->add_mesh(ptx_mesh); + che * ptx_mesh = scanner_ptx(mesh, mesh.rt_embree, n_rows, n_cols, {0, 0, 0}); che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); + view->add_mesh(ptx_mesh); } - return true; } diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index ad9fb817..a9bf5386 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -6,8 +6,6 @@ #include #include -#include - #include #include diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 26e53942..0551101c 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -19,6 +19,9 @@ namespace gproshan { che_viewer::~che_viewer() { + delete rt_embree; + delete rt_optix; + glDeleteBuffers(5, vbo); glDeleteVertexArrays(1, &vao); } @@ -78,8 +81,8 @@ void che_viewer::update() selected_xyz.clear(); update_vbo(); - delete pick_vertex; - pick_vertex = new rt::embree({mesh}, {model_mat}); + delete rt_embree; + rt_embree = new rt::embree({mesh}, {model_mat}); } void che_viewer::update_vbo() @@ -257,8 +260,11 @@ void che_viewer::scale(const real_t & s) void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) { - glm::vec3 dir = pick_vertex->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); - index_t v = pick_vertex->closest_vertex(cam_pos, dir); + const glm::vec3 & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, + glm::inverse(proj_view_mat), cam_pos); + + const index_t & v = rt_embree->closest_vertex(cam_pos, dir); + if(v != NIL) selected.push_back(v); } diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index a3f31007..a1ceb2d3 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -82,7 +82,8 @@ void frame::display() { program.enable(); - glViewport(0, 0, width, height); + gproshan_debug_var(width); + gproshan_debug_var(height); glBindVertexArray(vao); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 1595c458..2aef7f0a 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -63,6 +63,8 @@ viewer::viewer(const int & width, const int & height): window_width(width), wind che * s = new che_sphere(0.01); s->update_normals(); sphere.init(s, false); + + rt_frame = new frame; } viewer::~viewer() @@ -74,11 +76,7 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); - delete rt_embree; - delete rt_optix; - delete rt_frame; - delete sphere; } @@ -98,18 +96,7 @@ bool viewer::run() proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - switch(render_opt) - { - case R_GL: - render_gl(); - break; - case R_EMBREE: - render_rt(rt_embree); - break; - case R_OPTIX: - render_rt(rt_optix); - break; - } + render_gl(); TOC(render_time); @@ -664,26 +651,23 @@ bool viewer::m_setup_raytracing(viewer * view) if(ImGui::Button("Build")) { - if(!view->rt_frame) - view->rt_frame = new frame; - switch(rt) { case R_GL: break; case R_EMBREE: - delete view->rt_embree; + delete mesh.rt_embree; TIC(time); - view->rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); + mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); break; case R_OPTIX: #ifdef GPROSHAN_OPTIX - delete view->rt_optix; + delete mesh.rt_optix; TIC(time); - view->rt_optix = new rt::optix({mesh}, {mesh.model_mat}); + mesh.rt_optix = new rt::optix({mesh}, {mesh.model_mat}); TOC(time); sprintf(view->status_message, "build optix in %.3fs", time); #endif // GPROSHAN_OPTIX @@ -696,21 +680,24 @@ bool viewer::m_setup_raytracing(viewer * view) bool viewer::m_render_gl(viewer * view) { - view->render_opt = R_GL; + che_viewer & mesh = view->active_mesh(); + mesh.render_opt = R_GL; return false; } bool viewer::m_render_embree(viewer * view) { + che_viewer & mesh = view->active_mesh(); view->rt_restart = true; - view->render_opt = R_EMBREE; + mesh.render_opt = R_EMBREE; return false; } bool viewer::m_render_optix(viewer * view) { + che_viewer & mesh = view->active_mesh(); view->rt_restart = true; - view->render_opt = R_OPTIX; + mesh.render_opt = R_OPTIX; return false; } @@ -848,6 +835,12 @@ void viewer::render_gl() glViewport(mesh.vx * viewport_width, mesh.vy * viewport_height, viewport_width, viewport_height); + if(mesh.render_opt != R_GL) + { + render_rt(mesh); + continue; + } + if(mesh->is_pointcloud() || mesh.render_pointcloud) mesh.draw_point_cloud(shader_pointcloud); else @@ -863,10 +856,8 @@ void viewer::render_gl() } } -void viewer::render_rt(rt::raytracing * rt) +void viewer::render_rt(che_viewer & mesh) { - if(!rt) return; - rt_restart = rt_frame->resize(viewport_width, viewport_height) || rt_restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *rt_frame); @@ -874,17 +865,20 @@ void viewer::render_rt(rt::raytracing * rt) scene_lights.clear(); - che_viewer & mesh = active_mesh(); for(const index_t & v: mesh.selected) scene_lights.push_back(glm_vec3(mesh->gt(v))); if(!scene_lights.size()) scene_lights = {glm_vec3(cam_light)}; - rt->render( img, glm::uvec2(viewport_width, viewport_height), - proj_view_mat, glm_vec3(cam.eye), scene_lights, - mesh.render_flat, rt_restart - ); + rt::raytracing * rt = nullptr; + if(mesh.render_opt == R_EMBREE) rt = mesh.rt_embree; + if(mesh.render_opt == R_OPTIX) rt = mesh.rt_optix; + + rt->render( img, glm::uvec2(viewport_width, viewport_height), + proj_view_mat, glm_vec3(cam.eye), scene_lights, + mesh.render_flat, rt_restart + ); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); From 9fa56d50b1a496f45ff26ccb39fa3442adb13f11 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 3 Jun 2022 01:29:08 +0200 Subject: [PATCH 0656/1018] viewer: hide/show imgui, framebuffer per viewport --- include/gproshan/viewer/viewer.h | 13 ++- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 192 ++++++++++++++++--------------- 3 files changed, 114 insertions(+), 93 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a140f20b..a029996d 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -59,6 +59,8 @@ class viewer int window_width, window_height; int viewport_width, viewport_height; + bool hide_imgui = false; + shader shader_triangles; shader shader_normals; shader shader_gradient; @@ -71,11 +73,13 @@ class viewer glm::mat4 proj_view_mat; + double render_time = 0; + che_viewer meshes[N_MESHES]; size_t n_meshes = 0; index_t idx_active_mesh = 0; - frame * rt_frame = nullptr; + frame * frames = nullptr; bool rt_restart = false; @@ -111,8 +115,10 @@ class viewer void init_menus(); void init_glsl(); + void imgui(); + void render_gl(); - void render_rt(che_viewer & mesh); + void render_rt(che_viewer & mesh, frame & rt_frame); static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); @@ -122,6 +128,9 @@ class viewer static void scroll_callback(GLFWwindow * window, double xoffset, double yoffset); static bool m_help(viewer * view); + static bool m_close(viewer * view); + static bool m_hide_show_imgui(viewer * view); + static bool m_save_load_view(viewer * view); static bool m_reset_mesh(viewer * view); static bool m_save_mesh(viewer * view); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 85918c1e..c4641c8c 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -81,7 +81,7 @@ void app_viewer::init() add_process(GLFW_KEY_T, "T", "Toplesets", process_compute_toplesets); sub_menus.push_back("Sparse Coding"); - add_process(GLFW_KEY_I, "I", "Mesh Sparse Coding", process_msparse_coding); + add_process(GLFW_KEY_U, "U", "Mesh Sparse Coding", process_msparse_coding); add_process(GLFW_KEY_J, "J", "MDICT Patch", process_mdict_patch); add_process(GLFW_KEY_D, "D", "MDICT Mask", process_mask); add_process(GLFW_KEY_L, "L", "PC reconstruction", process_pc_reconstruction); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 2aef7f0a..5cb239ac 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -64,7 +64,7 @@ viewer::viewer(const int & width, const int & height): window_width(width), wind s->update_normals(); sphere.init(s, false); - rt_frame = new frame; + frames = new frame[N_MESHES]; } viewer::~viewer() @@ -76,14 +76,12 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); - delete rt_frame; delete sphere; + delete [] frames; } bool viewer::run() { - double render_time = 0; - while(!glfwWindowShouldClose(window)) { TIC(render_time) @@ -100,104 +98,110 @@ bool viewer::run() TOC(render_time); + imgui(); - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); + glfwSwapBuffers(window); + glfwPollEvents(); + } - che_viewer & mesh = active_mesh(); + return true; +} - if(ImGui::BeginMainMenuBar()) - { - if(ImGui::BeginMenu("Select")) - { - for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) - { - idx_active_mesh = i; - glfwSetWindowTitle(window, mesh->filename.c_str()); - } +void viewer::imgui() +{ + if(hide_imgui) return; - ImGui::EndMenu(); - } + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); - if(ImGui::BeginMenu("Color")) - { - for(index_t i = 0; i < colormap.size(); ++i) - if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) - mesh.idx_colormap = i; + che_viewer & mesh = active_mesh(); - ImGui::EndMenu(); - } - - for(index_t i = 0; i < sub_menus.size(); ++i) - { - if(ImGui::BeginMenu(sub_menus[i].c_str())) + if(ImGui::BeginMainMenuBar()) + { + if(ImGui::BeginMenu("Select")) + { + for(index_t i = 0; i < n_meshes; ++i) + if(ImGui::MenuItem((to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { - for(auto & p: processes) - { - process_t & pro = p.second; - if(pro.function != nullptr && pro.sub_menu == i) - if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) - sprintf(status_message, "%s", pro.selected ? pro.name.c_str() : ""); - } - - ImGui::EndMenu(); + idx_active_mesh = i; + glfwSetWindowTitle(window, mesh->filename.c_str()); } - } - ImGui::EndMainMenuBar(); + ImGui::EndMenu(); } - ImGui::SetNextWindowSize(ImVec2(window_width, -1)); - ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); - ImGui::Begin("status gproshan", nullptr, ImGuiWindowFlags_NoTitleBar); - ImGui::Text("[] %s", status_message); - ImGui::SameLine(window_width - 180); - ImGui::Text("github.com/larc/gproshan"); - ImGui::End(); - - ImGui::SetNextWindowSize(ImVec2(320, -1)); - ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_Once); - ImGui::Begin("gproshan"); - - ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); - - ImGui::Text("%s", mesh->filename.c_str()); - ImGui::Text("%16s: %.3f", "FPS", 1.0 / render_time); - ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); - ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); - - if(mesh.render_pointcloud) + if(ImGui::BeginMenu("Color")) { - ImGui::Checkbox("point_normals", &mesh.point_normals); - ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); + for(index_t i = 0; i < colormap.size(); ++i) + if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) + mesh.idx_colormap = i; + + ImGui::EndMenu(); } - for(auto & p: processes) + for(index_t i = 0; i < sub_menus.size(); ++i) { - process_t & pro = p.second; - if(ImGui::CollapsingHeader(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) + if(ImGui::BeginMenu(sub_menus[i].c_str())) { - ImGui::PushID(pro.name.c_str()); - ImGui::Indent(); - pro.selected = pro.selected && p.second.function(this); - ImGui::Unindent(); - ImGui::PopID(); + for(auto & p: processes) + { + process_t & pro = p.second; + if(pro.function != nullptr && pro.sub_menu == i) + if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) + sprintf(status_message, "%s", pro.selected ? pro.name.c_str() : ""); + } + + ImGui::EndMenu(); } } - ImGui::PopItemWidth(); - ImGui::End(); + ImGui::EndMainMenuBar(); + } - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui::SetNextWindowSize(ImVec2(window_width, -1)); + ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); + ImGui::Begin("status gproshan", nullptr, ImGuiWindowFlags_NoTitleBar); + ImGui::Text("[] %s", status_message); + ImGui::SameLine(window_width - 180); + ImGui::Text("github.com/larc/gproshan"); + ImGui::End(); - glfwSwapBuffers(window); - glfwPollEvents(); + ImGui::SetNextWindowSize(ImVec2(320, -1)); + ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_Once); + ImGui::Begin("gproshan"); + + ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); + + ImGui::Text("%s", mesh->filename.c_str()); + ImGui::Text("%16s: %.3f", "FPS", 1.0 / render_time); + ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); + ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); + + if(mesh.render_pointcloud) + { + ImGui::Checkbox("point_normals", &mesh.point_normals); + ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); } - return true; + for(auto & p: processes) + { + process_t & pro = p.second; + if(ImGui::CollapsingHeader(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::PushID(pro.name.c_str()); + ImGui::Indent(); + pro.selected = pro.selected && p.second.function(this); + ImGui::Unindent(); + ImGui::PopID(); + } + } + + ImGui::PopItemWidth(); + ImGui::End(); + + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } che_viewer & viewer::active_mesh() @@ -273,6 +277,8 @@ void viewer::init_menus() { sub_menus.push_back("Viewer"); add_process(GLFW_KEY_F1, "F1", "Help", m_help); + add_process(GLFW_KEY_ESCAPE, "ESCAPE", "Close", m_close); + add_process(GLFW_KEY_I, "F1", "Hide/Show ImGui", m_hide_show_imgui); add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", m_save_load_view); add_process(GLFW_KEY_UP, "UP", "Zoom in", m_zoom_in); add_process(GLFW_KEY_DOWN, "DOWN", "Zoom out", m_zoom_out); @@ -386,12 +392,6 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in { if(action == GLFW_RELEASE) return; - if(key == GLFW_KEY_ESCAPE) - { - glfwSetWindowShouldClose(window, GLFW_TRUE); - return; - } - viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureKeyboard) return; @@ -463,6 +463,18 @@ bool viewer::m_help(viewer * view) return false; } +bool viewer::m_close(viewer * view) +{ + glfwSetWindowShouldClose(view->window, GLFW_TRUE); + return false; +} + +bool viewer::m_hide_show_imgui(viewer * view) +{ + view->hide_imgui = !view->hide_imgui; + return false; +} + bool viewer::m_save_load_view(viewer * view) { filesystem::create_directory(tmp_file_path("views/")); @@ -837,7 +849,7 @@ void viewer::render_gl() if(mesh.render_opt != R_GL) { - render_rt(mesh); + render_rt(mesh, frames[i]); continue; } @@ -856,11 +868,11 @@ void viewer::render_gl() } } -void viewer::render_rt(che_viewer & mesh) +void viewer::render_rt(che_viewer & mesh, frame & rt_frame) { - rt_restart = rt_frame->resize(viewport_width, viewport_height) || rt_restart; + rt_restart = rt_frame.resize(viewport_width, viewport_height) || rt_restart; - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *rt_frame); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); scene_lights.clear(); @@ -884,7 +896,7 @@ void viewer::render_rt(che_viewer & mesh) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); rt_restart = false; - rt_frame->display(); + rt_frame.display(); } void viewer::pick_vertex(const real_t & x, const real_t & y) From eee1f566c36224ff29764b6793fe0b7a1998ab0f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 3 Jun 2022 13:08:55 +0200 Subject: [PATCH 0657/1018] viewer: adding rt::render_params --- include/gproshan/raytracing/raytracing.h | 13 ++--- include/gproshan/raytracing/render_params.h | 33 +++++++++++++ include/gproshan/viewer/viewer.h | 15 +++--- src/gproshan/raytracing/raytracing.cpp | 37 ++++++++------ src/gproshan/viewer/viewer.cpp | 53 ++++++++++++--------- 5 files changed, 97 insertions(+), 54 deletions(-) create mode 100644 include/gproshan/raytracing/render_params.h diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index a0993460..22f8a210 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -2,6 +2,7 @@ #define RAYTRACING_H #include +#include #include #include @@ -26,6 +27,9 @@ struct hit class raytracing { + public: + bool restart = false; + protected: struct rt_mesh { @@ -49,14 +53,7 @@ class raytracing raytracing() = default; virtual ~raytracing() = default; - virtual void render(glm::vec4 * img, - const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, - const std::vector & light, - const bool & flat, - const bool & restart = false - ); + virtual void render(glm::vec4 * img, const render_params & params, const bool & flat); virtual float * raycaster( const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h new file mode 100644 index 00000000..152bb9c6 --- /dev/null +++ b/include/gproshan/raytracing/render_params.h @@ -0,0 +1,33 @@ +#ifndef RENDER_PARAMS_H +#define RENDER_PARAMS_H + + +#include + +#include + + +// geometry processing and shape analysis framework +namespace gproshan::rt { + + +struct render_params +{ + int window_width = 0; + int window_height = 0; + int viewport_width = 0; + int viewport_height = 0; + int viewport_x = 0; + int viewport_y = 0; + glm::mat4 proj_view_mat; + glm::vec3 cam_pos; + std::vector lights; + bool viewport_is_window = true; +}; + + +} // namespace gproshan + + +#endif // RENDER_PARAMS_H + diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a029996d..11e80443 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -10,8 +10,8 @@ #include #include #include - #include +#include #include #include @@ -56,8 +56,11 @@ class viewer GLFWwindow * window = nullptr; - int window_width, window_height; - int viewport_width, viewport_height; + rt::render_params render_params; + int & window_width = render_params.window_width; + int & window_height = render_params.window_height; + int & viewport_width = render_params.viewport_width; + int & viewport_height = render_params.viewport_height; bool hide_imgui = false; @@ -67,11 +70,7 @@ class viewer shader shader_pointcloud; camera cam; - quaternion cam_light; - std::vector scene_lights; - - glm::mat4 proj_view_mat; double render_time = 0; @@ -81,8 +80,6 @@ class viewer frame * frames = nullptr; - bool rt_restart = false; - float bgc = 0; std::map processes; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index a88673c0..43c98d0c 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -11,33 +11,40 @@ namespace gproshan::rt { std::default_random_engine raytracing::gen; std::uniform_real_distribution raytracing::randf; -void raytracing::render(glm::vec4 * img, - const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, - const std::vector & light, - const bool & flat, - const bool & restart ) +void raytracing::render(glm::vec4 * img, const render_params & params, const bool & flat) { if(restart) n_samples = 0; - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); + glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); + + int window_width = params.window_width; + int window_height = params.window_height; + if(params.viewport_is_window) + { + window_width = params.viewport_width; + window_height = params.viewport_height; + } glm::vec4 li; #pragma omp parallel for private(li) - for(index_t i = 0; i < windows_size.x; ++i) - for(index_t j = 0; j < windows_size.y; ++j) + for(int i = 0; i < params.viewport_width; ++i) + for(int j = 0; j < params.viewport_height; ++j) { //row major - glm::vec4 & color = img[j * windows_size.x + i]; - glm::vec3 dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); + glm::vec4 & color = img[j * params.viewport_width + i]; + const glm::vec3 & dir = ray_view_dir( i + params.viewport_x, + j + params.viewport_y, + {window_width, window_height}, + inv_proj_view, + params.cam_pos + ); li = glm::vec4(0); - for(auto & l: light) - li += intersect_li(cam_pos, dir, l, flat); + for(auto & l: params.lights) + li += intersect_li(params.cam_pos, dir, l, flat); - color = (color * float(n_samples) + li / float(light.size())) / float(n_samples + 1); + color = (color * float(n_samples) + li / float(params.lights.size())) / float(n_samples + 1); } ++n_samples; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 5cb239ac..717d4188 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -50,8 +50,11 @@ const std::vector viewer::colormap = { "vertex color", "set" }; -viewer::viewer(const int & width, const int & height): window_width(width), window_height(height) +viewer::viewer(const int & width, const int & height) { + window_width = width; + window_height = height; + init_gl(); init_glsl(); init_imgui(); @@ -91,7 +94,7 @@ bool viewer::run() cam_light = vertex(-1, 1, -2); cam_light = r.conj() * cam_light * r; - proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); + render_params.proj_view_mat = glm::perspective(45.0f, float(window_width) / float(window_height), 0.01f, 1000.0f) * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); render_gl(); @@ -191,7 +194,7 @@ void viewer::imgui() { ImGui::PushID(pro.name.c_str()); ImGui::Indent(); - pro.selected = pro.selected && p.second.function(this); + pro.selected = pro.selected && pro.function(this); ImGui::Unindent(); ImGui::PopID(); } @@ -398,9 +401,10 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in process_t & pro = view->processes[key]; if(pro.function) { - pro.selected = !pro.selected; + pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; sprintf(view->status_message, "%s", pro.selected ? pro.name.c_str() : ""); } + } void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) @@ -425,14 +429,14 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) { view->cam.motion(x, y, view->window_width, view->window_height); - view->rt_restart = true; + //view->rt_restart = true; } if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)) { view->cam.pos.im().x = 2 * x / view->window_width - 1; view->cam.pos.im().y = 2 * y / view->window_height - 1; - view->rt_restart = true; + //view->rt_restart = true; } } @@ -444,13 +448,13 @@ void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) if(yoffset > 0) { view->cam.zoom_in(); - view->rt_restart = true; + //view->rt_restart = true; } if(yoffset < 0) { view->cam.zoom_out(); - view->rt_restart = true; + //view->rt_restart = true; } } @@ -700,7 +704,7 @@ bool viewer::m_render_gl(viewer * view) bool viewer::m_render_embree(viewer * view) { che_viewer & mesh = view->active_mesh(); - view->rt_restart = true; + //view->rt_restart = true; mesh.render_opt = R_EMBREE; return false; } @@ -708,7 +712,7 @@ bool viewer::m_render_embree(viewer * view) bool viewer::m_render_optix(viewer * view) { che_viewer & mesh = view->active_mesh(); - view->rt_restart = true; + //view->rt_restart = true; mesh.render_opt = R_OPTIX; return false; } @@ -793,7 +797,7 @@ bool viewer::m_render_flat(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_flat = !mesh.render_flat; - view->rt_restart = true; + //view->rt_restart = true; return false; } @@ -805,7 +809,7 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), - view->proj_view_mat, glm_vec3(view->cam.eye) + view->render_params.proj_view_mat, glm_vec3(view->cam.eye) ); std::thread([](CImg img) @@ -829,6 +833,7 @@ void viewer::render_gl() glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); + glm::mat4 & proj_view_mat = render_params.proj_view_mat; glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); @@ -870,11 +875,16 @@ void viewer::render_gl() void viewer::render_rt(che_viewer & mesh, frame & rt_frame) { - rt_restart = rt_frame.resize(viewport_width, viewport_height) || rt_restart; + rt::raytracing * rt = nullptr; + if(mesh.render_opt == R_EMBREE) rt = mesh.rt_embree; + if(mesh.render_opt == R_OPTIX) rt = mesh.rt_optix; + + rt->restart = rt_frame.resize(viewport_width, viewport_height) || rt->restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); + std::vector & scene_lights = render_params.lights; scene_lights.clear(); for(const index_t & v: mesh.selected) @@ -883,19 +893,16 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) if(!scene_lights.size()) scene_lights = {glm_vec3(cam_light)}; - rt::raytracing * rt = nullptr; - if(mesh.render_opt == R_EMBREE) rt = mesh.rt_embree; - if(mesh.render_opt == R_OPTIX) rt = mesh.rt_optix; +// render_params.viewport_x = mesh.vx * viewport_width; +// render_params.viewport_y = mesh.vy * viewport_height; + render_params.cam_pos = glm_vec3(cam.eye); - rt->render( img, glm::uvec2(viewport_width, viewport_height), - proj_view_mat, glm_vec3(cam.eye), scene_lights, - mesh.render_flat, rt_restart - ); + rt->render(img, render_params, mesh.render_flat); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - rt_restart = false; + rt->restart = false; rt_frame.display(); } @@ -910,7 +917,9 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) che_viewer & mesh = meshes[cols * (iy / viewport_height) + ix / viewport_width]; - mesh.select(ix % viewport_width, iy % viewport_height, {viewport_width, viewport_height}, proj_view_mat, glm_vec3(cam.eye)); + mesh.select(ix % viewport_width, iy % viewport_height, + {viewport_width, viewport_height}, + render_params.proj_view_mat, glm_vec3(cam.eye)); } From f3b5113fdf4d81ded08f42af8f10505cdfb053c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 3 Jun 2022 16:11:56 +0200 Subject: [PATCH 0658/1018] gproshan: linking OpenGL and tmp home folder --- CMakeLists.txt | 1 + apps/CMakeLists.txt | 2 +- gproshanConfig.cmake.in | 1 + include/gproshan/include.h | 2 +- include/gproshan/viewer/viewer.h | 2 -- src/gproshan/CMakeLists.txt | 1 + 6 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14f8fcee..c2064b91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,7 @@ configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in find_package(embree 3.13 REQUIRED) find_package(Threads REQUIRED) +find_package(OpenGL REQUIRED) find_package(OpenMP REQUIRED) find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index effbe8ab..4c95ff1a 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -17,5 +17,5 @@ add_executable(test_scanner test_scanner.cpp) target_link_libraries(test_scanner gproshan) -file(MAKE_DIRECTORY ${gproshan_SOURCE_DIR}/tmp) +file(MAKE_DIRECTORY ~/.gproshan/) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 881e2862..915f54e7 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -10,6 +10,7 @@ endif(CUDAToolkit_FOUND) find_dependency(embree 3.13 REQUIRED) find_dependency(Threads REQUIRED) find_dependency(OpenMP REQUIRED) +find_dependency(OpenGL REQUIRED) find_dependency(GLEW REQUIRED) find_dependency(glfw3 REQUIRED) find_dependency(glm REQUIRED) diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 8a3cffd1..fd7198da 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -25,7 +25,7 @@ typedef unsigned int index_t; #endif -#define tmp_file_path(file) (std::string(GPROSHAN_DIR) + "/tmp/" + file) +#define tmp_file_path(file) (std::string("~/.gproshan/") + file) #define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) #ifdef GPROSHAN_LOG diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 11e80443..96de4894 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -87,7 +87,6 @@ class viewer che_viewer sphere; shader shader_sphere; - public: std::vector other_vertices; std::vector vectors; std::vector sub_menus; @@ -105,7 +104,6 @@ class viewer protected: virtual bool run(); - private: void info_gl(); void init_gl(); void init_imgui(); diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index f0b3d42b..cb9dfb62 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(gproshan SHARED ${cpp_sources}) target_link_libraries(gproshan embree) target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan GLEW::GLEW) +target_link_libraries(gproshan OpenGL::OpenGL) target_link_libraries(gproshan glfw) target_link_libraries(gproshan ${X11_X11_LIB}) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) From b0d9193ea31c742ffed5c24cb14858e02e8a8bec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 3 Jun 2022 23:26:35 +0200 Subject: [PATCH 0659/1018] rt_optix: refactoring render params --- .gitignore | 2 ++ include/gproshan/include.h | 2 +- include/gproshan/raytracing/rt_optix.h | 12 ++----- src/gproshan/CMakeLists.txt | 2 +- src/gproshan/raytracing/rt_optix.cpp | 47 +++++++++++--------------- src/gproshan/raytracing/rt_optix.cu | 36 ++++++++++---------- 6 files changed, 44 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index 05aa84b5..a098252e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ tmp/* include/gproshan/config.h + +src/rt_optix.ptx diff --git a/include/gproshan/include.h b/include/gproshan/include.h index fd7198da..fa2fe17f 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -25,7 +25,7 @@ typedef unsigned int index_t; #endif -#define tmp_file_path(file) (std::string("~/.gproshan/") + file) +#define tmp_file_path(file) (std::string(getenv("HOME")) + "/.gproshan/"+ file) #define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) #ifdef GPROSHAN_LOG diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index e1e2fce7..f8540376 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -40,7 +40,7 @@ class optix : public raytracing OptixShaderBindingTable sbt = {}; - launch_params render_params; + launch_params optix_params; void * launch_params_buffer = nullptr; std::vector dd_mesh; @@ -55,15 +55,7 @@ class optix : public raytracing optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); - void render(glm::vec4 * img, - const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, - const std::vector & light, - const bool & flat, - const bool & restart = false - ); - + void render(glm::vec4 * img, const render_params & params, const bool & flat); private: void create_raygen_programs(); diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index cb9dfb62..97b92a63 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -39,7 +39,7 @@ if(OptiX_INCLUDE) add_custom_target( ptx_tmp DEPENDS ${ptx_file} COMMAND ${CMAKE_COMMAND} -E copy ${ptx_file} - ${gproshan_SOURCE_DIR}/tmp/rt_optix.ptx + ${gproshan_SOURCE_DIR}/src/rt_optix.ptx ) add_dependencies(gproshan ptx_tmp) endif(OptiX_INCLUDE) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index ce87ba2d..718f7256 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -70,11 +70,11 @@ optix::optix(const std::vector & meshes, const std::vector & m optix_pipeline_compile_opt.numPayloadValues = 2; optix_pipeline_compile_opt.numAttributeValues = 2; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "render_params"; + optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_params"; - optix_pipeline_link_opt.maxTraceDepth = 2; + optix_pipeline_link_opt.maxTraceDepth = 2; - std::ifstream ptx_is(tmp_file_path("rt_optix.ptx")); + std::ifstream ptx_is(std::string(GPROSHAN_DIR) + "/src/rt_optix.ptx"); const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); @@ -94,7 +94,7 @@ optix::optix(const std::vector & meshes, const std::vector & m create_hitgroup_programs(); // build as - render_params.traversable = build_as(meshes, model_mats); + optix_params.traversable = build_as(meshes, model_mats); // create pipeline create_pipeline(); @@ -108,7 +108,7 @@ optix::optix(const std::vector & meshes, const std::vector & m optix::~optix() { - cudaFree(render_params.frame.color_buffer); + cudaFree(optix_params.frame.color_buffer); cudaFree(launch_params_buffer); cudaFree(raygen_records_buffer); cudaFree(miss_records_buffer); @@ -119,49 +119,42 @@ optix::~optix() cuda_free_CHE(dd_mesh[i], d_mesh[i]); } -void optix::render( glm::vec4 * img, - const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, - const std::vector & light, - const bool & flat, - const bool & restart - ) +void optix::render(glm::vec4 * img, const render_params & params, const bool & flat) { if(restart) { n_samples = 0; - if(render_params.frame.color_buffer) - cudaFree(render_params.frame.color_buffer); + if(optix_params.frame.color_buffer) + cudaFree(optix_params.frame.color_buffer); - render_params.frame.width = windows_size.x; - render_params.frame.height = windows_size.y; - cudaMalloc(&render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4)); + optix_params.frame.width = params.viewport_width; + optix_params.frame.height = params.viewport_height; + cudaMalloc(&optix_params.frame.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4)); } - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); + glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); - render_params.flat = flat; - memcpy(render_params.light, glm::value_ptr(light[0]), sizeof(render_params.light)); - memcpy(render_params.cam_pos, glm::value_ptr(cam_pos), sizeof(render_params.cam_pos)); - memcpy(render_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(render_params.inv_proj_view)); + optix_params.flat = flat; + memcpy(optix_params.light, glm::value_ptr(params.lights[0]), sizeof(optix_params.light)); + memcpy(optix_params.cam_pos, glm::value_ptr(params.cam_pos), sizeof(optix_params.cam_pos)); + memcpy(optix_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(optix_params.inv_proj_view)); - cudaMemcpy(launch_params_buffer, &render_params, sizeof(launch_params), cudaMemcpyHostToDevice); + cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); optixLaunch(optix_pipeline, stream, (CUdeviceptr) launch_params_buffer, sizeof(launch_params), &sbt, - render_params.frame.width, - render_params.frame.height, + optix_params.frame.width, + optix_params.frame.height, 1 ); cudaDeviceSynchronize(); - cudaMemcpy(img, render_params.frame.color_buffer, windows_size.x * windows_size.y * sizeof(glm::vec4), cudaMemcpyDeviceToHost); + cudaMemcpy(img, optix_params.frame.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4), cudaMemcpyDeviceToHost); } void optix::create_raygen_programs() diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 7fc898da..9156aa2c 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -24,7 +24,7 @@ vertex_cu normalize (const vertex_cu & v) } -extern "C" __constant__ launch_params render_params; +extern "C" __constant__ launch_params optix_params; static __forceinline__ __device__ void * unpackPointer(uint32_t i0, uint32_t i1) @@ -79,13 +79,13 @@ extern "C" __global__ void __closesthit__radiance() const vertex_cu & C = data[2]; const vertex_cu Ng = normalize((B - A) * (C - A)); - const vertex_cu normal = render_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + const vertex_cu normal = optix_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; const vertex_cu ca(mesh.VC[a].r, mesh.VC[a].g, mesh.VC[a].b); const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); const vertex_cu cc(mesh.VC[c].r, mesh.VC[c].g, mesh.VC[c].b); - const vertex_cu & light = *(vertex_cu *) render_params.light; + const vertex_cu & light = *(vertex_cu *) optix_params.light; const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex_cu position = (1.f - u - v) * A + u * B + v * C; @@ -96,7 +96,7 @@ extern "C" __global__ void __closesthit__radiance() L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; unsigned int occluded = 1; - optixTrace( render_params.traversable, + optixTrace( optix_params.traversable, position, wi, 1e-5f, // tmin @@ -137,26 +137,26 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const float sx = (float(ix) + .5f) / render_params.frame.width; - const float sy = (float(iy) + .5f) / render_params.frame.height; + const float sx = (float(ix) + .5f) / optix_params.frame.width; + const float sy = (float(iy) + .5f) / optix_params.frame.height; - vertex_cu & cam_pos = *(vertex_cu *) render_params.cam_pos; + vertex_cu & cam_pos = *(vertex_cu *) optix_params.cam_pos; vertex_cu ipv[3]; for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) - ipv[i][j] = render_params.inv_proj_view[i + j * 4]; + ipv[i][j] = optix_params.inv_proj_view[i + j * 4]; - vertex_cu d = { render_params.inv_proj_view[0 * 4 + 3], - render_params.inv_proj_view[1 * 4 + 3], - render_params.inv_proj_view[2 * 4 + 3] + vertex_cu d = { optix_params.inv_proj_view[0 * 4 + 3], + optix_params.inv_proj_view[1 * 4 + 3], + optix_params.inv_proj_view[2 * 4 + 3] }; - vertex_cu e = { render_params.inv_proj_view[3 * 4 + 0], - render_params.inv_proj_view[3 * 4 + 1], - render_params.inv_proj_view[3 * 4 + 2] + vertex_cu e = { optix_params.inv_proj_view[3 * 4 + 0], + optix_params.inv_proj_view[3 * 4 + 1], + optix_params.inv_proj_view[3 * 4 + 2] }; - float & de = render_params.inv_proj_view[15]; + float & de = optix_params.inv_proj_view[15]; vertex_cu view = {sx * 2 - 1, sy * 2 - 1, 1}; vertex_cu q = vertex_cu{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; @@ -167,7 +167,7 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu pixelColorPRD; uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); - optixTrace( render_params.traversable, + optixTrace( optix_params.traversable, cam_pos, ray_dir, 0.f, // tmin @@ -180,9 +180,9 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); - const uint32_t fbIndex = ix + iy * render_params.frame.width; + const uint32_t fbIndex = ix + iy * optix_params.frame.width; - float4 * frame = (float4 *) render_params.frame.color_buffer; + float4 * frame = (float4 *) optix_params.frame.color_buffer; frame[fbIndex].x = pixelColorPRD.x; frame[fbIndex].y = pixelColorPRD.y; frame[fbIndex].z = pixelColorPRD.z; From 81bf6a1ff8640e73c028cd8626ca8413a289d8a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 4 Jun 2022 00:02:44 +0200 Subject: [PATCH 0660/1018] rt_optix: refactoring optix params, multi viewport rendering --- include/gproshan/raytracing/raytracing.h | 3 -- include/gproshan/raytracing/render_params.h | 3 +- include/gproshan/raytracing/rt_optix_params.h | 9 +++--- src/gproshan/raytracing/raytracing.cpp | 5 ++- src/gproshan/raytracing/rt_optix.cpp | 32 +++++++++++++------ src/gproshan/raytracing/rt_optix.cu | 8 ++--- src/gproshan/viewer/viewer.cpp | 19 +++++------ 7 files changed, 44 insertions(+), 35 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 22f8a210..90637716 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -27,9 +27,6 @@ struct hit class raytracing { - public: - bool restart = false; - protected: struct rt_mesh { diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 152bb9c6..4c64a338 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -19,10 +19,11 @@ struct render_params int viewport_height = 0; int viewport_x = 0; int viewport_y = 0; + bool restart = false; + bool viewport_is_window = true; glm::mat4 proj_view_mat; glm::vec3 cam_pos; std::vector lights; - bool viewport_is_window = true; }; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 1e8249e4..dec88fcc 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -16,11 +16,10 @@ namespace gproshan::rt { struct launch_params { - struct - { - void * color_buffer = nullptr; - uint32_t width, height; - } frame; + void * color_buffer = nullptr; + uint32_t window_width, window_height; + uint32_t viewport_width, viewport_height; + uint32_t viewport_x, viewport_y; bool flat; float light[3]; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 43c98d0c..8a6b729b 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -13,9 +13,7 @@ std::uniform_real_distribution raytracing::randf; void raytracing::render(glm::vec4 * img, const render_params & params, const bool & flat) { - if(restart) n_samples = 0; - - glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); + if(params.restart) n_samples = 0; int window_width = params.window_width; int window_height = params.window_height; @@ -25,6 +23,7 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo window_height = params.viewport_height; } + glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); glm::vec4 li; #pragma omp parallel for private(li) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 718f7256..9757f578 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -108,7 +108,7 @@ optix::optix(const std::vector & meshes, const std::vector & m optix::~optix() { - cudaFree(optix_params.frame.color_buffer); + cudaFree(optix_params.color_buffer); cudaFree(launch_params_buffer); cudaFree(raygen_records_buffer); cudaFree(miss_records_buffer); @@ -121,16 +121,28 @@ optix::~optix() void optix::render(glm::vec4 * img, const render_params & params, const bool & flat) { - if(restart) + if(params.restart) { n_samples = 0; - if(optix_params.frame.color_buffer) - cudaFree(optix_params.frame.color_buffer); + if(optix_params.color_buffer) + cudaFree(optix_params.color_buffer); - optix_params.frame.width = params.viewport_width; - optix_params.frame.height = params.viewport_height; - cudaMalloc(&optix_params.frame.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4)); + optix_params.window_width = params.window_width; + optix_params.window_height = params.window_height; + if(params.viewport_is_window) + { + optix_params.window_width = params.viewport_width; + optix_params.window_height = params.viewport_height; + } + + optix_params.viewport_width = params.viewport_width; + optix_params.viewport_height = params.viewport_height; + + optix_params.viewport_x = params.viewport_x; + optix_params.viewport_y = params.viewport_y; + + cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4)); } glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); @@ -147,14 +159,14 @@ void optix::render(glm::vec4 * img, const render_params & params, const bool & f (CUdeviceptr) launch_params_buffer, sizeof(launch_params), &sbt, - optix_params.frame.width, - optix_params.frame.height, + optix_params.viewport_width, + optix_params.viewport_height, 1 ); cudaDeviceSynchronize(); - cudaMemcpy(img, optix_params.frame.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4), cudaMemcpyDeviceToHost); + cudaMemcpy(img, optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4), cudaMemcpyDeviceToHost); } void optix::create_raygen_programs() diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 9156aa2c..236b2442 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -137,8 +137,8 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const float sx = (float(ix) + .5f) / optix_params.frame.width; - const float sy = (float(iy) + .5f) / optix_params.frame.height; + const float sx = (float(ix + optix_params.viewport_x) + .5f) / optix_params.window_width; + const float sy = (float(iy + optix_params.viewport_y) + .5f) / optix_params.window_height; vertex_cu & cam_pos = *(vertex_cu *) optix_params.cam_pos; @@ -180,9 +180,9 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); - const uint32_t fbIndex = ix + iy * optix_params.frame.width; + const uint32_t fbIndex = ix + iy * optix_params.viewport_width; - float4 * frame = (float4 *) optix_params.frame.color_buffer; + float4 * frame = (float4 *) optix_params.color_buffer; frame[fbIndex].x = pixelColorPRD.x; frame[fbIndex].y = pixelColorPRD.y; frame[fbIndex].z = pixelColorPRD.z; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 717d4188..dd5289f1 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -429,14 +429,14 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT)) { view->cam.motion(x, y, view->window_width, view->window_height); - //view->rt_restart = true; + view->render_params.restart = true; } if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)) { view->cam.pos.im().x = 2 * x / view->window_width - 1; view->cam.pos.im().y = 2 * y / view->window_height - 1; - //view->rt_restart = true; + view->render_params.restart = true; } } @@ -448,13 +448,13 @@ void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) if(yoffset > 0) { view->cam.zoom_in(); - //view->rt_restart = true; + view->render_params.restart = true; } if(yoffset < 0) { view->cam.zoom_out(); - //view->rt_restart = true; + view->render_params.restart = true; } } @@ -704,7 +704,7 @@ bool viewer::m_render_gl(viewer * view) bool viewer::m_render_embree(viewer * view) { che_viewer & mesh = view->active_mesh(); - //view->rt_restart = true; + view->render_params.restart = true; mesh.render_opt = R_EMBREE; return false; } @@ -712,7 +712,7 @@ bool viewer::m_render_embree(viewer * view) bool viewer::m_render_optix(viewer * view) { che_viewer & mesh = view->active_mesh(); - //view->rt_restart = true; + view->render_params.restart = true; mesh.render_opt = R_OPTIX; return false; } @@ -797,7 +797,7 @@ bool viewer::m_render_flat(viewer * view) { che_viewer & mesh = view->active_mesh(); mesh.render_flat = !mesh.render_flat; - //view->rt_restart = true; + view->render_params.restart = true; return false; } @@ -871,6 +871,8 @@ void viewer::render_gl() mesh.draw_selected_vertices(sphere, shader_sphere); } + + render_params.restart = false; } void viewer::render_rt(che_viewer & mesh, frame & rt_frame) @@ -879,7 +881,7 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) if(mesh.render_opt == R_EMBREE) rt = mesh.rt_embree; if(mesh.render_opt == R_OPTIX) rt = mesh.rt_optix; - rt->restart = rt_frame.resize(viewport_width, viewport_height) || rt->restart; + render_params.restart = rt_frame.resize(viewport_width, viewport_height) || render_params.restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); @@ -902,7 +904,6 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); - rt->restart = false; rt_frame.display(); } From c32e78607e26a0f02908b8d52a45ec56608811a1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 4 Jun 2022 00:11:55 +0200 Subject: [PATCH 0661/1018] rt render default --- src/gproshan/viewer/viewer.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index dd5289f1..1933e003 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -881,6 +881,8 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) if(mesh.render_opt == R_EMBREE) rt = mesh.rt_embree; if(mesh.render_opt == R_OPTIX) rt = mesh.rt_optix; + if(!rt) return; + render_params.restart = rt_frame.resize(viewport_width, viewport_height) || render_params.restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); @@ -895,8 +897,9 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) if(!scene_lights.size()) scene_lights = {glm_vec3(cam_light)}; -// render_params.viewport_x = mesh.vx * viewport_width; -// render_params.viewport_y = mesh.vy * viewport_height; + //render_params.viewport_x = mesh.vx * viewport_width; + //render_params.viewport_y = mesh.vy * viewport_height; + //render_params.viewport_is_window = false; render_params.cam_pos = glm_vec3(cam.eye); rt->render(img, render_params, mesh.render_flat); From fc04e38636aa9bcd3565bbc3944a1dea235bd568 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 4 Jun 2022 01:59:51 +0200 Subject: [PATCH 0662/1018] viewer: using vertex instead of glm::vec3 --- apps/test_scanner.cpp | 2 +- gproshanConfig.cmake.in | 2 +- include/gproshan/mesh/quaternion.h | 1 + include/gproshan/mesh/vertex.h | 1 + include/gproshan/raytracing/raytracing.h | 24 +++---- include/gproshan/raytracing/render_params.h | 4 +- include/gproshan/raytracing/rt_embree.h | 28 ++++----- include/gproshan/scenes/scanner.h | 5 +- include/gproshan/viewer/che_viewer.h | 2 +- src/gproshan/mesh/quaternion.cpp | 5 ++ src/gproshan/mesh/vertex.cpp | 5 ++ src/gproshan/raytracing/raytracing.cpp | 12 ++-- src/gproshan/raytracing/rt_embree.cpp | 70 +++++++++++---------- src/gproshan/raytracing/rt_optix.cpp | 4 +- src/gproshan/scenes/scanner.cpp | 10 ++- src/gproshan/viewer/che_viewer.cpp | 4 +- src/gproshan/viewer/viewer.cpp | 12 ++-- 17 files changed, 103 insertions(+), 88 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index ef498374..aaa5165c 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -65,7 +65,7 @@ int main(int argc, char* argv[]) gproshan_log_var(ptx_folder); gproshan_log_var(jpg_folder); - gproshan::che_ply * mesh_ply = new gproshan::che_ply(argv[1]); + gproshan::che_ply * mesh_ply = new gproshan::che_ply(argv[1]); gproshan::rt::raytracing * rt_embree; diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 915f54e7..461a70df 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -2,7 +2,7 @@ include(CMakeFindDependencyMacro) -find_dependency(CUDAToolkit 11.4) +find_dependency(CUDAToolkit 11.7) if(CUDAToolkit_FOUND) add_definitions(-DGPROSHAN_CUDA) endif(CUDAToolkit_FOUND) diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 7e0677f3..0abf0b11 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -21,6 +21,7 @@ class quaternion quaternion(real_t s, const vertex & v); quaternion(const vertex & v); + operator const vertex & () const; const quaternion & operator = (real_t s); const quaternion & operator = (const vertex & v); real_t & operator [] (int index); diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h index 6290eead..463f9993 100644 --- a/include/gproshan/mesh/vertex.h +++ b/include/gproshan/mesh/vertex.h @@ -53,6 +53,7 @@ class vertex }; vertex operator * (const real_t & a, const vertex & v); +vertex normalize(const vertex & v); std::ostream & operator << (std::ostream & os, const vertex & v); std::istream & operator >> (std::istream & is, vertex & v); diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 90637716..adc02950 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -54,33 +54,33 @@ class raytracing virtual float * raycaster( const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, + const vertex & cam_pos, const index_t & samples = 4 ); - glm::vec3 ray_view_dir( const index_t & x, const index_t & y, + vertex ray_view_dir( const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, - const glm::vec3 & cam_pos + const vertex & cam_pos ); - virtual hit intersect( const glm::vec3 &, // org - const glm::vec3 & //dir + virtual hit intersect( const vertex &, // org + const vertex & //dir ) { return hit(); } - virtual index_t closest_vertex( const glm::vec3 &, // org, - const glm::vec3 & // dir + virtual index_t closest_vertex( const vertex &, // org, + const vertex & // dir ) { return NIL; }; protected: - virtual glm::vec4 intersect_li( const glm::vec3 &, // org, - const glm::vec3 &, // dir, - const glm::vec3 &, // light, + virtual glm::vec4 intersect_li( const vertex &, // org, + const vertex &, // dir, + const vertex &, // light, const bool & // flat ) { return glm::vec4(0); }; - virtual float intersect_depth( const glm::vec3 &, // org, - const glm::vec3 & // dir + virtual float intersect_depth( const vertex &, // org, + const vertex & // dir ) { return 0; }; }; diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 4c64a338..c2de8aba 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -22,8 +22,8 @@ struct render_params bool restart = false; bool viewport_is_window = true; glm::mat4 proj_view_mat; - glm::vec3 cam_pos; - std::vector lights; + vertex cam_pos; + std::vector lights; }; diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 3305258e..196b46c2 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -18,16 +18,16 @@ class embree : public raytracing protected: struct ray_hit : public RTCRayHit { - ray_hit(const glm::vec3 & p_org = glm::vec3(0.0f), - const glm::vec3 & v_dir = glm::vec3(0.0f), + ray_hit(const vertex & p_org = vertex(0.0f), + const vertex & v_dir = vertex(0.0f), float near = 1e-5f, float far = FLT_MAX); - glm::vec3 org() const; - glm::vec3 dir() const; - glm::vec3 color(const rt_mesh & mesh) const; - glm::vec3 normal(const rt_mesh & mesh, const bool & flat = false) const; - glm::vec3 position() const; + vertex org() const; + vertex dir() const; + vertex color(const rt_mesh & mesh) const; + vertex normal(const rt_mesh & mesh, const bool & flat = false) const; + vertex position() const; index_t closest_vertex(const rt_mesh & mesh) const; }; @@ -48,8 +48,8 @@ class embree : public raytracing ); virtual ~embree(); - virtual index_t closest_vertex(const glm::vec3 & org, const glm::vec3 & dir); - virtual hit intersect(const glm::vec3 & org, const glm::vec3 & dir); + virtual index_t closest_vertex(const vertex & org, const vertex & dir); + virtual hit intersect(const vertex & org, const vertex & dir); protected: @@ -63,13 +63,13 @@ class embree : public raytracing index_t add_mesh(const che * mesh, const glm::mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh); - virtual float pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r); + virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); - glm::vec4 li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near = 1e-5f); - glm::vec4 li(ray_hit r, const glm::vec3 & light, const bool & flat); + glm::vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); + glm::vec4 li(ray_hit r, const vertex & light, const bool & flat); - glm::vec4 intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light, const bool & flat); - float intersect_depth(const glm::vec3 & org, const glm::vec3 & dir); + glm::vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); + float intersect_depth(const vertex & org, const vertex & dir); }; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 9e401c9e..266518f1 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -10,8 +10,9 @@ namespace gproshan::rt { -che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam); -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam, const std::string & file_jpg = ""); +che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos); + +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); } // namespace gproshan diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 242bdf71..be76ad2e 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -80,7 +80,7 @@ class che_viewer void translate(const vertex & p); void scale(const real_t & s); - void select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos); + void select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 85acdb2f..706f95f5 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -16,6 +16,11 @@ quaternion::quaternion(real_t s_, const vertex & v_): s(s_), v(v_) {} quaternion::quaternion(const vertex & v_): s(0), v(v_) {} +quaternion::operator const vertex & () const +{ + return v; +} + const quaternion & quaternion::operator = (real_t _s) { s = _s; diff --git a/src/gproshan/mesh/vertex.cpp b/src/gproshan/mesh/vertex.cpp index 58308521..f31830fb 100644 --- a/src/gproshan/mesh/vertex.cpp +++ b/src/gproshan/mesh/vertex.cpp @@ -116,6 +116,11 @@ vertex operator * (const real_t & a, const vertex & v) return {a * v.x, a * v.y, a * v.z}; } +vertex normalize(const vertex & v) +{ + return v / *v; +} + ostream & operator << (ostream & os, const vertex & v) { os << v.x << " " << v.y << " " << v.z; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 8a6b729b..979312a3 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -32,7 +32,7 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo { //row major glm::vec4 & color = img[j * params.viewport_width + i]; - const glm::vec3 & dir = ray_view_dir( i + params.viewport_x, + const vertex & dir = ray_view_dir( i + params.viewport_x, j + params.viewport_y, {window_width, window_height}, inv_proj_view, @@ -51,7 +51,7 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo float * raytracing::raycaster( const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, - const glm::vec3 & cam_pos, + const vertex & cam_pos, const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; @@ -64,7 +64,7 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; - glm::vec3 dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); + vertex dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); for(index_t s = 0; s < samples; ++s) color += intersect_depth(cam_pos, dir); @@ -75,14 +75,14 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, return frame; } -glm::vec3 raytracing::ray_view_dir(const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const glm::vec3 & cam_pos) +vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const vertex & cam_pos) { glm::vec2 screen = glm::vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); glm::vec4 q = inv_proj_view * view; - glm::vec3 p = glm::vec3(q * (1.f / q.w)); + vertex p = {q.x / q.w, q.y / q.w, q.z / q.w}; - return glm::normalize(p - cam_pos); + return normalize(p - cam_pos); } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 5ee34953..27d6ae3a 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -12,7 +12,7 @@ namespace gproshan::rt { -embree::ray_hit::ray_hit(const glm::vec3 & p_org, const glm::vec3 & v_dir, float near, float far) +embree::ray_hit::ray_hit(const vertex & p_org, const vertex & v_dir, float near, float far) { ray.org_x = p_org.x; ray.org_y = p_org.y; @@ -34,30 +34,31 @@ embree::ray_hit::ray_hit(const glm::vec3 & p_org, const glm::vec3 & v_dir, float hit.instID[0] = RTC_INVALID_GEOMETRY_ID; } -glm::vec3 embree::ray_hit::org() const +vertex embree::ray_hit::org() const { return {ray.org_x, ray.org_y, ray.org_z}; } -glm::vec3 embree::ray_hit::dir() const +vertex embree::ray_hit::dir() const { return {ray.dir_x, ray.dir_y, ray.dir_z}; } -glm::vec3 embree::ray_hit::color(const rt_mesh & mesh) const +vertex embree::ray_hit::color(const rt_mesh & mesh) const { - const vertex & c = mesh.pointcloud ? mesh->color(hit.primID) : - mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm_vec3(c); + if(mesh.pointcloud) + return mesh->color(hit.primID); + + return mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); } -glm::vec3 embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const +vertex embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const { if(flat || mesh.pointcloud) - return glm::normalize(glm::vec3(hit.Ng_x, hit.Ng_y, hit.Ng_z)); + return normalize({hit.Ng_x, hit.Ng_y, hit.Ng_z}); const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); - return glm::normalize(glm_vec3(n)); + return n / *n; } index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const @@ -82,7 +83,7 @@ index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const return mesh->vt(he); } -glm::vec3 embree::ray_hit::position() const +vertex embree::ray_hit::position() const { return org() + ray.tfar * dir(); } @@ -117,20 +118,20 @@ embree::~embree() rtcReleaseDevice(device); } -index_t embree::closest_vertex(const glm::vec3 & org, const glm::vec3 & dir) +index_t embree::closest_vertex(const vertex & org, const vertex & dir) { ray_hit r(org, dir); return intersect(r) ? r.closest_vertex(geomID_mesh[r.hit.geomID]) : NIL; } -hit embree::intersect(const glm::vec3 & org, const glm::vec3 & dir) +hit embree::intersect(const vertex & org, const vertex & dir) { ray_hit r(org, dir); if(intersect(r)) { const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; - const glm::vec3 & color = r.color(mesh); - const glm::vec3 & normal = r.normal(mesh); + const vertex & color = r.color(mesh); + const vertex & normal = r.normal(mesh); return { r.closest_vertex(mesh), r.ray.tfar, @@ -191,7 +192,7 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_mat) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); - glm::vec3 * vertices = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + vertex * vertices = (vertex *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), mesh->n_vertices @@ -205,7 +206,10 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_mat) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) - vertices[i] = glm::vec3(model_mat * glm::vec4(glm_vec3(mesh->gt(i)), 1)); + { + const glm::vec4 & v = model_mat * glm::vec4(glm_vec3(mesh->gt(i)), 1); + vertices[i] = {v.x, v.y, v.z}; + } memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); @@ -228,7 +232,7 @@ index_t embree::add_pointcloud(const che * mesh) mesh->n_vertices ); - glm::vec3 * normal = (glm::vec3 *) rtcSetNewGeometryBuffer( geom, + vertex * normal = (vertex *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), @@ -239,7 +243,7 @@ index_t embree::add_pointcloud(const che * mesh) for(index_t i = 0; i < mesh->n_vertices; ++i) { pxyzr[i] = glm::vec4(glm_vec3(mesh->gt(i)), pc_radius); - normal[i] = glm_vec3(mesh->normal(i)); + normal[i] = mesh->normal(i); } rtcCommitGeometry(geom); @@ -250,18 +254,18 @@ index_t embree::add_pointcloud(const che * mesh) return geom_id; } -float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 & color, ray_hit r) +float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r) { float w, sum_w = 0; - position = glm::vec3(0); - normal = glm::vec3(0); - color = glm::vec3(0); + position = vertex(0); + normal = vertex(0); + color = vertex(0); do { glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); - sum_w += w = pc_radius - glm::length(r.position() - glm::vec3(xyzr[r.hit.primID])); + sum_w += w = 1; //pc_radius - glm::length(r.position() - vertex(xyzr[r.hit.primID])); position += w * r.position(); normal += w * r.normal(geomID_mesh[r.hit.geomID]); color += w * r.color(geomID_mesh[r.hit.geomID]); @@ -277,22 +281,22 @@ float embree::pointcloud_hit(glm::vec3 & position, glm::vec3 & normal, glm::vec3 return sum_w; } -glm::vec4 embree::li(const glm::vec3 & light, const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & color, const float & near) +glm::vec4 embree::li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near) { - const glm::vec3 wi = normalize(light - position); - const float dot_wi_normal = glm::dot(wi, normal); - const glm::vec3 L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; + const vertex wi = normalize(light - position); + const float dot_wi_normal = (wi, normal); + const vertex L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; ray_hit r(position, wi, near); - return glm::vec4((occluded(r) ? 0.4f : 1.f) * L, 1); + return glm::vec4(glm_vec3((occluded(r) ? 0.4f : 1.f) * L), 1); } -glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) +glm::vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) { float total_tfar = 0; float near; - glm::vec3 position, normal, color; + vertex position, normal, color; glm::vec4 L(0); // while(total_tfar < 0.1) @@ -317,13 +321,13 @@ glm::vec4 embree::li(ray_hit r, const glm::vec3 & light, const bool & flat) return L / total_tfar; } -glm::vec4 embree::intersect_li(const glm::vec3 & org, const glm::vec3 & dir, const glm::vec3 & light,const bool & flat) +glm::vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light,const bool & flat) { ray_hit r(org, dir); return intersect(r) ? li(r, light, flat) : glm::vec4(0.f); } -float embree::intersect_depth(const glm::vec3 & org, const glm::vec3 & dir) +float embree::intersect_depth(const vertex & org, const vertex & dir) { ray_hit r(org, dir); return intersect(r) ? r.ray.tfar : 0.f; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 9757f578..0536a6d3 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -148,8 +148,8 @@ void optix::render(glm::vec4 * img, const render_params & params, const bool & f glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); optix_params.flat = flat; - memcpy(optix_params.light, glm::value_ptr(params.lights[0]), sizeof(optix_params.light)); - memcpy(optix_params.cam_pos, glm::value_ptr(params.cam_pos), sizeof(optix_params.cam_pos)); + memcpy(optix_params.light, ¶ms.lights[0], sizeof(optix_params.light)); + memcpy(optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); memcpy(optix_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(optix_params.inv_proj_view)); cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index e7c71867..6bc47b1d 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -11,19 +11,17 @@ using namespace cimg_library; namespace gproshan::rt { -che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam) +che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) { std::vector ptx; return new che(ptx.data(), ptx.size(), nullptr, 0); } -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam, const std::string & file_jpg) +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) { che * mesh_ptx = new che(n_cols * n_rows); - glm::vec3 cam_pos = glm_vec3(cam); - const real_t delta_phi = (2 * M_PI) / n_rows; const real_t delta_theta = M_PI / n_cols; @@ -35,7 +33,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons const real_t & phi = i * delta_phi; const real_t & theta = j * delta_theta; - const glm::vec3 & dir = { sin(theta) * cos(phi), + const vertex & dir = { sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta) }; @@ -44,7 +42,7 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons if(h.idx != NIL) { - const glm::vec3 & pos = cam_pos + dir * h.dist; + const vertex & pos = cam_pos + dir * h.dist; mesh_ptx->get_vertex(v) = {pos.x, pos.y, pos.z}; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 0551101c..2a1415ba 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -258,9 +258,9 @@ void che_viewer::scale(const real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const glm::vec3 & cam_pos) +void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const vertex & cam_pos) { - const glm::vec3 & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, + const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 1933e003..461289c5 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -809,7 +809,7 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), - view->render_params.proj_view_mat, glm_vec3(view->cam.eye) + view->render_params.proj_view_mat, view->cam.eye ); std::thread([](CImg img) @@ -888,19 +888,19 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); - std::vector & scene_lights = render_params.lights; + std::vector & scene_lights = render_params.lights; scene_lights.clear(); for(const index_t & v: mesh.selected) - scene_lights.push_back(glm_vec3(mesh->gt(v))); + scene_lights.push_back(mesh->gt(v)); if(!scene_lights.size()) - scene_lights = {glm_vec3(cam_light)}; + scene_lights = {cam_light}; //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; //render_params.viewport_is_window = false; - render_params.cam_pos = glm_vec3(cam.eye); + render_params.cam_pos = cam.eye; rt->render(img, render_params, mesh.render_flat); @@ -923,7 +923,7 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) mesh.select(ix % viewport_width, iy % viewport_height, {viewport_width, viewport_height}, - render_params.proj_view_mat, glm_vec3(cam.eye)); + render_params.proj_view_mat, cam.eye); } From 4ea0bdf3e029643d7b6595e7868ee1ae0ec64c2a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 4 Jun 2022 02:35:34 +0200 Subject: [PATCH 0663/1018] mesh: rename get_vertex to point --- include/gproshan/mesh/che.h | 2 +- src/gproshan/app_viewer.cpp | 4 ++-- src/gproshan/geodesics/dijkstra.cpp | 2 +- src/gproshan/mdict/msparse_coding.cpp | 6 +++--- src/gproshan/mdict/patch.cpp | 14 +++++++------- src/gproshan/mesh/che.cpp | 2 +- src/gproshan/mesh/che_img.cpp | 2 +- src/gproshan/mesh/che_poisson.cpp | 6 +++--- src/gproshan/mesh/quaternion.cpp | 2 +- src/gproshan/raytracing/rt_embree.cpp | 4 +--- src/gproshan/scenes/scanner.cpp | 25 +++++++++++-------------- src/gproshan/viewer/viewer.cpp | 2 +- 12 files changed, 33 insertions(+), 38 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index d12e5c0a..456bf5b0 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -120,7 +120,7 @@ class che const index_t & evt(const index_t & v) const; const index_t & bt(const index_t & b) const; size_t max_degree() const; - vertex & get_vertex(index_t v); + vertex & point(index_t v); void set_vertices(const vertex *const& positions, size_t n = 0, const index_t & v_i = 0); const std::string filename_size() const; const std::string name() const; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index c4641c8c..a3389f2d 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -939,7 +939,7 @@ bool app_viewer::process_noise(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t r = real_t(d_mod_1000(generator)) / 200000; - mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); + mesh->point(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } mesh->update_normals(); @@ -961,7 +961,7 @@ bool app_viewer::process_black_noise(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t r = real_t(d_mod_1000(generator)) / 200000; - mesh->get_vertex(v) += (!d_mod_5(generator)) * r * mesh->normal(v); + mesh->point(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } mesh->update_normals(); diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index bf9bfd6e..f4d1b5d7 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -75,7 +75,7 @@ void dijkstra::run(che * mesh) { if(visited[nv]) { - w = weights[nv] + *(mesh->get_vertex(nv) - mesh->get_vertex(v)); + w = weights[nv] + *(mesh->point(nv) - mesh->point(v)); if(w < weights[v]) weights[v] = w; diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index a9bf5386..14cb1460 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -248,8 +248,8 @@ void msparse_coding::load_sampling() if(!invalid_seed[v]) { - const vertex & va = mesh->get_vertex(vsf); - const vertex & vb = mesh->get_vertex(v); + const vertex & va = mesh->point(vsf); + const vertex & vb = mesh->point(v); invalid_seed[v] = *(va - vb) < 0.8 * euc_radio; } @@ -959,7 +959,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) #pragma omp parallel for reduction(+: error) reduction(max: max_error) for(index_t v = 0; v < mesh->n_vertices; ++v) { - dist[v] = *(new_vertices[v] - mesh->get_vertex(v)); + dist[v] = *(new_vertices[v] - mesh->point(v)); error += dist[v]; max_error = max(max_error, dist[v]); } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 91ecba7d..b9c7884d 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -126,7 +126,7 @@ bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle } } } - //p = mesh->get_vertex(indexes[i]); + //p = mesh->point(indexes[i]); //p = p - c ; //p = p - ((p,n)*n); @@ -208,8 +208,8 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind for(index_t i=1; i < vertices.size(); ++i) { - p = mesh->get_vertex(indexes[i]); - c = mesh->get_vertex(v); // central vertices + p = mesh->point(indexes[i]); + c = mesh->point(v); // central vertices p = p - c ; p = p - ((p,n)*n); @@ -256,7 +256,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, real_t proj_area = std::numeric_limits::epsilon(); real_t ratio; - vertex c = mesh->get_vertex(v); + vertex c = mesh->point(v); geodesics::params params; params.dist_alloc = new real_t[mesh->n_vertices]; @@ -268,7 +268,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, if(add_vertex_by_faces(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { - euc_radio = max(euc_radio, *(mesh->get_vertex(u) - c)); + euc_radio = max(euc_radio, *(mesh->point(u) - c)); return true; } @@ -292,7 +292,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, vertex p; for(auto & vi: vertices) { - p = mesh->get_vertex(vi); + p = mesh->point(vi); p = p - c ; p = p - ((p, n) * n); @@ -633,7 +633,7 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) vertex nz = mesh->normal(v); vertex nx = mesh->gt_vt_next_evt(v); // GT[VT[next(EVT[v]]] - vertex c = mesh->get_vertex(v); + vertex c = mesh->point(v); vertex ny; nx = nx - c ; nx = nx - ((nx,nz)*nz); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 5d23b475..5e4c7a54 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -592,7 +592,7 @@ size_t che::max_degree() const return md; } -vertex & che::get_vertex(index_t v) +vertex & che::point(index_t v) { return GT[v]; } diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 7342c2f2..3be33835 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -43,7 +43,7 @@ void che_img::read_file(const string & file) VT[he++] = (i - 1) * img.height() + j - 1; } - GT[v++] = vertex(i,j, img(i, j)); + GT[v++] = {i, j, img(i, j)}; } thread([](CImg img) { img.display(); }, img).detach(); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index d52ce9b4..31d1f18d 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -55,9 +55,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) if(spsolve(X, s * L, s * B)) for(index_t v = old_n_vertices; v < mesh->n_vertices; ++v) { - mesh->get_vertex(v).x = X(v - old_n_vertices, 0); - mesh->get_vertex(v).y = X(v - old_n_vertices, 1); - mesh->get_vertex(v).z = X(v - old_n_vertices, 2); + mesh->point(v).x = X(v - old_n_vertices, 0); + mesh->point(v).y = X(v - old_n_vertices, 1); + mesh->point(v).z = X(v - old_n_vertices, 2); } } diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 706f95f5..633280eb 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -24,7 +24,7 @@ quaternion::operator const vertex & () const const quaternion & quaternion::operator = (real_t _s) { s = _s; - v = vertex(0, 0, 0); + v = {0, 0, 0}; return *this; } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 27d6ae3a..96f0ce79 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -257,9 +257,7 @@ index_t embree::add_pointcloud(const che * mesh) float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r) { float w, sum_w = 0; - position = vertex(0); - normal = vertex(0); - color = vertex(0); + position = normal = color = {0, 0, 0}; do { diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 6bc47b1d..7df68e87 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -11,14 +12,7 @@ using namespace cimg_library; namespace gproshan::rt { -che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) -{ - std::vector ptx; - - return new che(ptx.data(), ptx.size(), nullptr, 0); -} - -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) +che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) { che * mesh_ptx = new che(n_cols * n_rows); @@ -33,17 +27,13 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons const real_t & phi = i * delta_phi; const real_t & theta = j * delta_theta; - const vertex & dir = { sin(theta) * cos(phi), - sin(theta) * sin(phi), - cos(theta) - }; + const vertex & dir = {std::sin(theta) * std::cos(phi), std::sin(theta) * std::sin(phi), std::cos(theta)}; const hit & h = rt->intersect(cam_pos, dir); if(h.idx != NIL) { - const vertex & pos = cam_pos + dir * h.dist; - mesh_ptx->get_vertex(v) = {pos.x, pos.y, pos.z}; + mesh_ptx->point(v) = cam_pos + dir * h.dist; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; mesh_ptx->rgb(v) = { (unsigned char) (h.color.x * 255), @@ -57,6 +47,13 @@ che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, cons } } + return mesh_ptx; +} + +che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) +{ + che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); + CImg img((unsigned char *) &mesh_ptx->rgb(0), 3, n_cols, n_rows); img.permute_axes("zycx"); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 461289c5..4195b50e 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -94,7 +94,7 @@ bool viewer::run() cam_light = vertex(-1, 1, -2); cam_light = r.conj() * cam_light * r; - render_params.proj_view_mat = glm::perspective(45.0f, float(window_width) / float(window_height), 0.01f, 1000.0f) * cam.look_at(r); + render_params.proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); render_gl(); From 533a014b2d7efdd685f0b313f6ca8736fdf4dfc9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 4 Jun 2022 13:07:57 +0200 Subject: [PATCH 0664/1018] mesh: refactoring, cleaning up che class --- include/gproshan/mesh/che.h | 119 +++++++++++++++----------- src/gproshan/app_viewer.cpp | 8 +- src/gproshan/mdict/msparse_coding.cpp | 2 +- src/gproshan/mesh/che.cpp | 32 +++---- src/gproshan/mesh/che_poisson.cpp | 2 +- 5 files changed, 87 insertions(+), 76 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 456bf5b0..3dc5edae 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -7,8 +7,11 @@ #include #include -#define for_star(he, mesh, v) for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->ot(prev(he))) != stop ? he : NIL) -#define for_boundary(he, mesh, v) for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->vt(next(he)))) != stop ? he : NIL) +#define for_star(he, mesh, v) \ + for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->ot(prev(he))) != stop ? he : NIL) + +#define for_boundary(he, mesh, v) \ + for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->vt(next(he)))) != stop ? he : NIL) // geometry processing and shape analysis framework @@ -69,27 +72,13 @@ class che che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); virtual ~che(); - std::vector star(const index_t & v) const; - std::vector link(const index_t & v) const; - std::vector bounds() const; - std::vector boundary(const index_t & v) const; - bool is_vertex_bound(const index_t & v) const; - bool is_edge_bound(const index_t & e) const; - void flip(const index_t & e); - real_t pdetriq(const index_t & t) const; - real_t quality() const; - real_t area_trig(const index_t & t) const; - real_t area_vertex(const index_t & v) const; - real_t area_surface() const; - void update_heatmap(const real_t * hm = nullptr); - const rgb_t & rgb(const index_t & v) const; - rgb_t & rgb(const index_t & v); - vertex color(const index_t & v) const; - vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; - const real_t & heatmap(const index_t & v) const; - real_t & heatmap(const index_t & v); - void update_normals(); - void invert_normals(); + // vertex access geometry methods to xyz point values, normals, and gradient + const vertex & gt(const index_t & v) const; + const vertex & gt_vt(const index_t & he) const; + const vertex & gt_vt_next_evt(const index_t & v) const; + const vertex & gt_e(const index_t & e, const bool & op = false) const; + const vertex & point(const index_t & v) const; + vertex & point(const index_t & v); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; @@ -98,48 +87,79 @@ class che vertex gradient_he(const index_t & he, const real_t *const & f) const; vertex gradient(const index_t & v, const real_t *const & f); vertex barycenter(const index_t & t) const; - real_t cotan(const index_t & he) const; - real_t mean_edge() const; - size_t memory() const; - size_t genus() const; - real_t mean_curvature(const index_t & v); + // vertex color methods + const real_t & heatmap(const index_t & v) const; + real_t & heatmap(const index_t & v); + const rgb_t & rgb(const index_t & v) const; + rgb_t & rgb(const index_t & v); + vertex color(const index_t & v) const; + vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; + + // update methods + void reload(); void normalize(); - bool is_pointcloud() const; - bool is_manifold() const; + void merge(const che * mesh, const std::vector & com_vertices); + void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); + void update_heatmap(const real_t * hm = nullptr); + void update_normals(); + void invert_normals(); + void multiplicate_vertices(); + void remove_vertices(const std::vector & vertices); + void remove_non_manifold_vertices(); + void set_head_vertices(index_t * head, const size_t & n); + + // half edge access methods triangular faces and navigation const index_t & vt(const index_t & he) const; - const vertex & gt(const index_t & v) const; - const vertex & gt_vt(const index_t & he) const; - const vertex & gt_vt_next_evt(const index_t & v) const; - const vertex & gt_e(const index_t & e, const bool & op = false); - const index_t & vt_e(const index_t & e, const bool & op = false); + const index_t & vt_e(const index_t & e, const bool & op = false) const; const index_t & et(const index_t & e) const; const index_t & ot_et(const index_t & e) const; const index_t & ot(const index_t & he) const; const index_t & ot_evt(const index_t & v) const; const index_t & evt(const index_t & v) const; const index_t & bt(const index_t & b) const; - size_t max_degree() const; - vertex & point(index_t v); - void set_vertices(const vertex *const& positions, size_t n = 0, const index_t & v_i = 0); - const std::string filename_size() const; + + // topology methods + std::vector star(const index_t & v) const; + std::vector link(const index_t & v) const; + void edge_collapse(const index_t *const & sort_edges); + void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t & k = NIL); + + // boundaray methods + std::vector bounds() const; + std::vector boundary(const index_t & v) const; + bool is_vertex_bound(const index_t & v) const; + bool is_edge_bound(const index_t & e) const; + + // file, name, and system methods const std::string name() const; const std::string name_size() const; - void reload(); - void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t & k = NIL); - void multiplicate_vertices(); - void remove_non_manifold_vertices(); - void remove_vertices(const std::vector & vertices); - void merge(const che * mesh, const std::vector & com_vertices); - void set_head_vertices(index_t * head, const size_t & n); - index_t link_intersect(const index_t & v_a, const index_t & v_b); - void edge_collapse(const index_t *const & sort_edges); + const std::string filename_size() const; + + // mesh information methods + size_t genus() const; + size_t memory() const; + size_t max_degree() const; + real_t quality() const; + real_t mean_edge() const; + real_t area_surface() const; + bool is_manifold() const; + bool is_pointcloud() const; + + // operation methods + void flip(const index_t & e); + real_t cotan(const index_t & he) const; + real_t pdetriq(const index_t & t) const; + real_t area_trig(const index_t & t) const; + real_t area_vertex(const index_t & v) const; + real_t mean_curvature(const index_t & v) const; protected: void init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); void init(const std::string & file); - void free(); void alloc(const size_t & n_v, const size_t & n_f); + void free(); + virtual void read_file(const std::string & file); private: @@ -152,6 +172,7 @@ class che friend struct CHE; }; + struct vertex_cu; struct CHE diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a3389f2d..2cd9ba63 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -342,11 +342,11 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); } - else mesh->set_vertices(vertices.data()); + else mesh->update_vertices(vertices.data()); fair.run(mesh); - mesh->set_vertices(fair.new_vertices()); + mesh->update_vertices(fair.new_vertices()); mesh->update_normals(); mesh.update_vbo_geometry(); @@ -371,11 +371,11 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); } - else mesh->set_vertices(vertices.data()); + else mesh->update_vertices(vertices.data()); fair.run(mesh); - mesh->set_vertices(fair.new_vertices()); + mesh->update_vertices(fair.new_vertices()); mesh->update_normals(); mesh.update_vbo_geometry(); diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 14cb1460..15ed47b0 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -970,7 +970,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) gproshan_debug_var(error); gproshan_debug_var(max_error); - mesh->set_vertices(new_vertices, mesh->n_vertices); + mesh->update_vertices(new_vertices, mesh->n_vertices); che_off::write_file(mesh, "../tmp/recon_mesh"); return max_error; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 5e4c7a54..16782bf1 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1,8 +1,5 @@ #include -#include -#include - #include #include #include @@ -459,7 +456,7 @@ size_t che::genus() const } // The Gauss-Bonnet Scheme -real_t che::mean_curvature(const index_t & v) +real_t che::mean_curvature(const index_t & v) const { real_t h = 0; real_t a = 0; @@ -534,13 +531,13 @@ const vertex & che::gt_vt_next_evt(const index_t & v) const return GT[VT[next(EVT[v])]]; } -const vertex & che::gt_e(const index_t & e, const bool & op) +const vertex & che::gt_e(const index_t & e, const bool & op) const { assert(e < n_edges); return op ? GT[VT[next(ET[e])]] : GT[VT[ET[e]]]; } -const index_t & che::vt_e(const index_t & e, const bool & op) +const index_t & che::vt_e(const index_t & e, const bool & op) const { assert(e < n_edges); return op ? VT[next(ET[e])] : VT[ET[e]]; @@ -592,16 +589,20 @@ size_t che::max_degree() const return md; } -vertex & che::point(index_t v) +const vertex & che::point(const index_t & v) const +{ + return GT[v]; +} + +vertex & che::point(const index_t & v) { return GT[v]; } -void che::set_vertices(const vertex *const& positions, size_t n, const index_t & v_i) +void che::update_vertices(const vertex * positions, const size_t & n, const index_t & v_i) { if(!positions) return; - if(!n) n = n_vertices; - memcpy(GT + v_i, positions, sizeof(vertex) * n); + memcpy(GT + v_i, positions, sizeof(vertex) * (!n) ? n_vertices : n); } const string che::filename_size() const @@ -977,17 +978,6 @@ void che::set_head_vertices(index_t * head, const size_t & n) } } -index_t che::link_intersect(const index_t & v_a, const index_t & v_b) -{ - index_t intersect = 0; - - for(index_t & a: link(v_a)) - for(index_t & b: link(v_b)) - if(a == b) ++intersect; - - return intersect; -} - void che::edge_collapse(const index_t *const & sort_edges) { } diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index 31d1f18d..fa017691 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -155,7 +155,7 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t H = E * H; H.each_col() += avg; - mesh->set_vertices((vertex *) H.memptr(), H.n_cols, old_n_vertices); + mesh->update_vertices((vertex *) H.memptr(), H.n_cols, old_n_vertices); } From ed8f3616f7e16442c1d02eef2b78caa469587ce5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 5 Jun 2022 17:12:31 +0200 Subject: [PATCH 0665/1018] mesh: che class refactoring cleaning up code --- include/gproshan/mesh/che.h | 16 +- include/gproshan/mesh/vertex.h | 4 + src/gproshan/mesh/che.cpp | 1229 +++++++++++++------------ src/gproshan/mesh/che_img.cpp | 2 +- src/gproshan/mesh/vertex.cpp | 21 +- src/gproshan/raytracing/rt_embree.cpp | 14 +- src/gproshan/viewer/viewer.cpp | 2 +- 7 files changed, 663 insertions(+), 625 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 3dc5edae..4ee8f228 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -81,12 +81,11 @@ class che vertex & point(const index_t & v); const vertex & normal(const index_t & v) const; vertex & normal(const index_t & v); - vertex shading_normal(const index_t & f, const float & u, const float & v, const float & w) const; + vertex shading_normal(const index_t & f, const float & u, const float & v) const; vertex normal_trig(const index_t & f) const; vertex normal_he(const index_t & he) const; - vertex gradient_he(const index_t & he, const real_t *const & f) const; - vertex gradient(const index_t & v, const real_t *const & f); - vertex barycenter(const index_t & t) const; + vertex gradient_he(const index_t & he, const real_t * f) const; + vertex gradient(const index_t & v, const real_t * f); // vertex color methods const real_t & heatmap(const index_t & v) const; @@ -94,11 +93,11 @@ class che const rgb_t & rgb(const index_t & v) const; rgb_t & rgb(const index_t & v); vertex color(const index_t & v) const; - vertex shading_color(const index_t & f, const float & u, const float & v, const float & w) const; + vertex shading_color(const index_t & f, const float & u, const float & v) const; // update methods void reload(); - void normalize(); + void normalize_sphere(const real_t & r = 1); void merge(const che * mesh, const std::vector & com_vertices); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); void update_heatmap(const real_t * hm = nullptr); @@ -117,15 +116,14 @@ class che const index_t & ot(const index_t & he) const; const index_t & ot_evt(const index_t & v) const; const index_t & evt(const index_t & v) const; - const index_t & bt(const index_t & b) const; // topology methods std::vector star(const index_t & v) const; std::vector link(const index_t & v) const; - void edge_collapse(const index_t *const & sort_edges); + void edge_collapse(const std::vector & sort_edges); void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t & k = NIL); - // boundaray methods + // boundary methods std::vector bounds() const; std::vector boundary(const index_t & v) const; bool is_vertex_bound(const index_t & v) const; diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h index 463f9993..b061de17 100644 --- a/include/gproshan/mesh/vertex.h +++ b/include/gproshan/mesh/vertex.h @@ -53,6 +53,10 @@ class vertex }; vertex operator * (const real_t & a, const vertex & v); + +vertex cross(const vertex & u, const vertex & v); +real_t dot(const vertex & u, const vertex & v); +real_t norm(const vertex & v); vertex normalize(const vertex & v); std::ostream & operator << (std::ostream & os, const vertex & v); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 16782bf1..6f940e19 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -82,213 +82,138 @@ che::~che() free(); } -vector che::star(const index_t & v) const -{ - assert(v >= n_vertices); - vector vstar; - for_star(he, this, v) - vstar.push_back(he); +// vertex access geometry methods to xyz point values, normals, and gradient - return vstar; +const vertex & che::gt(const index_t & v) const +{ + assert(v < n_vertices); + return GT[v]; } -vector che::link(const index_t & v) const +const vertex & che::gt_vt(const index_t & he) const { - assert(v >= n_vertices); - - vector vlink; - - if(is_vertex_bound(v)) - vlink.push_back(VT[next(EVT[v])]); - - for_star(he, this, v) - vlink.push_back(VT[prev(he)]); - - return vlink; + assert(he < n_half_edges); + return GT[VT[he]]; } -///< return a vector of indices of one vertex per boundary -vector che::bounds() const +const vertex & che::gt_vt_next_evt(const index_t & v) const { - if(!n_faces) return {}; - if(!manifold) return {}; - - vector vbounds; - - bool * is_bound = new bool[n_vertices]; - memset(is_bound, 0, sizeof(bool) * n_vertices); - - for(index_t v = 0; v < n_vertices; ++v) - if(!is_bound[v] && is_vertex_bound(v)) - { - vbounds.push_back(v); - - for_boundary(he, this, v) - is_bound[VT[he]] = true; - } - - delete [] is_bound; - - return vbounds; + assert(v < n_vertices); + return GT[VT[next(EVT[v])]]; } -///< return a vector of the indices of the boundary where v belongs -vector che::boundary(const index_t & v) const +const vertex & che::gt_e(const index_t & e, const bool & op) const { - vector vbound; - - for_boundary(he, this, v) - vbound.push_back(VT[he]); - - return vbound; + assert(e < n_edges); + return op ? GT[VT[next(ET[e])]] : GT[VT[ET[e]]]; } -bool che::is_vertex_bound(const index_t & v) const +const vertex & che::point(const index_t & v) const { - assert(v < n_vertices && EVT[v] < n_half_edges); - return EVT[v] != NIL && OT[EVT[v]] == NIL; + assert(v < n_vertices); + return GT[v]; } -bool che::is_edge_bound(const index_t & e) const +vertex & che::point(const index_t & v) { - return OT[ET[e]] == NIL; + assert(v < n_vertices); + return GT[v]; } -void che::flip(const index_t & e) +const vertex & che::normal(const index_t & v) const { - index_t ha = ET[e]; - index_t hb = OT[ha]; - - if(hb == NIL) - return; - - index_t va = VT[ha]; - index_t vb = VT[hb]; - index_t vc = VT[prev(ha)]; - index_t vd = VT[prev(hb)]; - - index_t et_pa = EHT[prev(ha)]; - index_t et_na = EHT[next(ha)]; - index_t et_pb = EHT[prev(hb)]; - index_t et_nb = EHT[next(hb)]; - - index_t ot_pa = OT[prev(ha)]; - index_t ot_na = OT[next(ha)]; - index_t ot_pb = OT[prev(hb)]; - index_t ot_nb = OT[next(hb)]; - - VT[prev(ha)] = vb; - VT[ha] = vc; - VT[next(ha)] = vd; - VT[prev(hb)] = va; - VT[hb] = vd; - VT[next(hb)] = vc; - - if(ot_pa != NIL) OT[ot_pa] = next(hb); - if(ot_na != NIL) OT[ot_na] = prev(ha); - if(ot_pb != NIL) OT[ot_pb] = next(ha); - if(ot_nb != NIL) OT[ot_nb] = prev(hb); - - OT[prev(ha)] = ot_na; - OT[next(ha)] = ot_pb; - OT[prev(hb)] = ot_nb; - OT[next(hb)] = ot_pa; - - ET[et_pa] = prev(ha); - ET[et_na] = next(ha); - ET[et_pb] = prev(hb); - ET[et_nb] = next(hb); - - EHT[prev(ha)] = EHT[OT[prev(ha)]] = et_pa; - EHT[next(ha)] = EHT[OT[next(ha)]] = et_na; - EHT[prev(hb)] = EHT[OT[prev(hb)]] = et_pb; - EHT[next(hb)] = EHT[OT[next(hb)]] = et_nb; + assert(VN && v < n_vertices); + return VN[v]; +} - if(EVT[va] == next(hb) || EVT[va] == ha) EVT[va] = prev(hb); - if(EVT[vb] == next(ha) || EVT[vb] == hb) EVT[vb] = prev(ha); - if(EVT[vc] == prev(ha)) EVT[vc] = next(hb); - if(EVT[vd] == prev(hb)) EVT[vd] = next(ha); +vertex & che::normal(const index_t & v) +{ + assert(VN && v < n_vertices); + return VN[v]; } -// https://www.mathworks.com/help/pde/ug/pdetriq.html -// 4*sqrt(3)*a -// q = ---------------- -// h1^2+h2^2+h3^2 -real_t che::pdetriq(const index_t & t) const +vertex che::shading_normal(const index_t & f, const float & u, const float & v) const { - index_t he = t * che::mtrig; - real_t h[3] = { - *(GT[VT[next(he)]] - GT[VT[he]]), - *(GT[VT[prev(he)]] - GT[VT[next(he)]]), - *(GT[VT[he]] - GT[VT[prev(he)]]) - }; - return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); + const index_t & he = f * che::mtrig; + return u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]; } -real_t che::quality() const +vertex che::normal_trig(const index_t & f) const { - real_t q = 0; + return normal_he(f * che::mtrig); +} - #pragma omp parallel for reduction(+: q) - for(index_t t = 0; t < n_faces; ++t) - q += pdetriq(t) > 0.6; // is confederating good triangle +vertex che::normal_he(const index_t & he) const +{ + const vertex & a = GT[VT[he]]; + const vertex & b = GT[VT[next(he)]]; + const vertex & c = GT[VT[prev(he)]]; - return q * 100 / n_faces; + return normalize((b - a) * (c - a)); } -real_t che::area_trig(const index_t & t) const +vertex che::gradient_he(const index_t & he, const real_t * f) const { - index_t he = t * che::mtrig; - vertex a = GT[VT[next(he)]] - GT[VT[he]]; - vertex b = GT[VT[prev(he)]] - GT[VT[he]]; + index_t i = VT[he]; + index_t j = VT[next(he)]; + index_t k = VT[prev(he)]; - return *(a * b) / 2; + vertex xi = GT[i]; + vertex xj = GT[j]; + vertex xk = GT[k]; + + vertex n = normal_he(he); + + real_t A2 = area_trig(trig(he)) * 2; + + vertex pij = n * (xj - xi); + vertex pjk = n * (xk - xj); + vertex pki = n * (xi - xk); + + vertex g = (f[i] * pjk + f[j] * pki + f[k] * pij) / A2; + return g / *g; } -real_t che::area_vertex(const index_t & v) const +vertex che::gradient(const index_t & v, const real_t * f) { - real_t area_star = 0; + vertex g; + real_t area, area_star = 0; + for_star(he, this, v) - area_star += area_trig(trig(he)); + { + area = area_trig(trig(he)); + area_star += area; + g += area * gradient_he(he, f); + } - return area_star / 3; + return g / area_star; } -real_t che::area_surface() const -{ - real_t area = 0; - #pragma omp parallel for reduction(+: area) - for(index_t i = 0; i < n_faces; ++i) - area += area_trig(i); +// vertex color methods - return area; +const real_t & che::heatmap(const index_t & v) const +{ + assert(v < n_vertices); + return VHC[v]; } -void che::update_heatmap(const real_t * hm) +real_t & che::heatmap(const index_t & v) { - if(!hm) - { - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - VHC[v] = 0.45; // default heatmap value - - return; - } - - memcpy(VHC, hm, n_vertices * sizeof(real_t)); + assert(v < n_vertices); + return VHC[v]; } const che::rgb_t & che::rgb(const index_t & v) const { - assert(VC && v < n_vertices); + assert(v < n_vertices); return VC[v]; } che::rgb_t & che::rgb(const index_t & v) { - assert(VC && v < n_vertices); + assert(v < n_vertices); return VC[v]; } @@ -298,410 +223,229 @@ vertex che::color(const index_t & v) const return VC[v]; } -vertex che::shading_color(const index_t & f, const float & u, const float & v, const float & w) const +vertex che::shading_color(const index_t & f, const float & u, const float & v) const { const index_t & he = f * che::mtrig; - return u * color(VT[he]) + v * color(VT[he + 1]) + w * color(VT[he + 2]); + return u * color(VT[he]) + v * color(VT[he + 1]) + (1 - u - v) * color(VT[he + 2]); } -const real_t & che::heatmap(const index_t & v) const -{ - assert(VHC && v < n_vertices); - return VHC[v]; -} -real_t & che::heatmap(const index_t & v) +// update methods + +void che::reload() { - assert(VHC && v < n_vertices); - return VHC[v]; + free(); + init(filename); } -void che::update_normals() +void che::normalize_sphere(const real_t & r) { - if(!n_faces) return; + vertex center; #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) { - vertex & n = VN[v]; + #pragma omp critical + center += GT[v]; + } - n = 0; - for_star(he, this, v) - n += area_trig(trig(he)) * normal_he(he); + center /= n_vertices; - n /= *n; + real_t max_norm = 0; + + #pragma omp parallel for reduction(max: max_norm) + for(index_t v = 0; v < n_vertices; ++v) + { + GT[v] -= center; + max_norm = std::max(max_norm, *GT[v]); } -} -void che::invert_normals() -{ #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - VN[v] = - VN[v]; + GT[v] *= r / max_norm; } -const vertex & che::normal(const index_t & v) const +void che::merge(const che * mesh, const vector & com_vertices) { - assert(VN && v < n_vertices); - return VN[v]; -} - -vertex & che::normal(const index_t & v) -{ - assert(VN && v < n_vertices); - return VN[v]; -} - -vertex che::shading_normal(const index_t & f, const float & u, const float & v, const float & w) const -{ - const index_t & he = f * che::mtrig; - return u * VN[VT[he]] + v * VN[VT[he + 1]] + w * VN[VT[he + 2]]; -} - -vertex che::normal_trig(const index_t & f) const -{ - return normal_he(f * che::mtrig); -} - -vertex che::normal_he(const index_t & he) const -{ - vertex n = (GT[VT[next(he)]] - GT[VT[he]]) * (GT[VT[prev(he)]] - GT[VT[he]]); - return n / *n; -} + // TODO +// write_file("big.off"); +// mesh->write_file("small.off"); +gproshan_debug(fill_holes); + size_t ncv = com_vertices.size(); + bool is_open = mesh->VT[next(mesh->EVT[0])] >= ncv; + gproshan_debug_var(is_open); -vertex che::gradient_he(const index_t & he, const real_t *const & f) const -{ - index_t i = VT[he]; - index_t j = VT[next(he)]; - index_t k = VT[prev(he)]; + size_t nv = n_vertices + mesh->n_vertices - ncv; + size_t nf = n_faces + mesh->n_faces; + size_t nh = n_half_edges + mesh->n_half_edges; + size_t ne = n_edges + mesh->n_edges - (ncv - is_open); - vertex xi = GT[i]; - vertex xj = GT[j]; - vertex xk = GT[k]; + vertex * aGT = new vertex[nv]; + index_t * aVT = new index_t[nh]; + index_t * aOT = new index_t[nh]; + index_t * aEVT = new index_t[nv]; + index_t * aET = new index_t[ne]; + index_t * aEHT = new index_t[nh]; - vertex n = normal_he(he); +gproshan_debug(fill_holes); + memcpy(aGT, GT, sizeof(vertex) * n_vertices); + memcpy(aGT + n_vertices, mesh->GT + ncv, sizeof(vertex) * (nv - n_vertices)); - real_t A2 = area_trig(trig(he)) * 2; + memcpy(aVT, VT, sizeof(index_t) * n_half_edges); - vertex pij = n * (xj - xi); - vertex pjk = n * (xk - xj); - vertex pki = n * (xi - xk); + index_t * t_aVT = aVT + n_half_edges; + for(index_t he = 0; he < mesh->n_half_edges; ++he) + t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices - ncv; +gproshan_debug(fill_holes); - vertex g = ( f[i] * pjk + f[j] * pki + f[k] * pij ) / A2; - return g / *g; -} + memcpy(aOT, OT, sizeof(index_t) * n_half_edges); +gproshan_debug(fill_holes); -vertex che::gradient(const index_t & v, const real_t *const & f) -{ - vertex g; - real_t area, area_star = 0; + index_t * t_aOT = aOT + n_half_edges; + for(index_t he = 0; he < mesh->n_half_edges; ++he) + t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges : NIL; +gproshan_debug(fill_holes); - for_star(he, this, v) + for(index_t v, he_v, he_i, i = 0; i < ncv; ++i) { - area = area_trig(trig(he)); - area_star += area; - g += area * gradient_he(he, f); + he_i = mesh->EVT[i]; + if(he_i != NIL && mesh->VT[next(he_i)] < ncv) + { + v = com_vertices[mesh->VT[next(he_i)]]; + he_v = EVT[v]; + aOT[he_v] = he_i + n_half_edges; + aOT[aOT[he_v]] = he_v; + } } - return g / area_star; -} +gproshan_debug(fill_holes); + memcpy(aEVT, EVT, sizeof(index_t) * n_vertices); +gproshan_debug(fill_holes); + if(is_open) + aEVT[com_vertices[0]] = mesh->EVT[0] != NIL ? mesh->EVT[0] + n_half_edges : NIL; -vertex che::barycenter(const index_t & t) const -{ - vertex bc; - index_t tmp, he = t * che::mtrig; - tmp = he; +gproshan_debug(fill_holes); + index_t * t_aEVT = aEVT + n_vertices; + for(index_t v = ncv; v < mesh->n_vertices; ++v) + t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges : NIL; +gproshan_debug(fill_holes); + + memcpy(aET, ET, sizeof(index_t) * n_edges); +gproshan_debug(fill_holes); - do + bool * common_edge = new bool[mesh->n_edges]; + memset(common_edge, 0, sizeof(bool) * mesh->n_edges); + for(index_t he_i, i = 0; i < ncv; ++i) { - bc += GT[VT[he]]; - he = next(he); + he_i = mesh->EVT[i]; + if(he_i != NIL && mesh->VT[next(he_i)] < ncv) + common_edge[mesh->EHT[he_i]] = true; } - while(he != tmp); - - return bc / che::mtrig; -} -real_t che::cotan(const index_t & he) const -{ - if(he == NIL) return 0; - - vertex a = GT[VT[he]] - GT[VT[prev(he)]]; - vertex b = GT[VT[next(he)]] - GT[VT[prev(he)]]; - - return (a, b) / *(a * b); -} + index_t ae = n_edges; + for(index_t e = 0; e < mesh->n_edges; ++e) + if(!common_edge[e]) + aET[ae++] = mesh->ET[e] + n_half_edges; -real_t che::mean_edge() const -{ - real_t m = 0; + gproshan_debug_var(ae == ne); + gproshan_debug_var(ae); + gproshan_debug_var(ne); + assert(ae == ne); + delete [] common_edge; +gproshan_debug(fill_holes); + free(); - #pragma omp parallel for reduction(+: m) - for(index_t e = 0; e < n_edges; ++e) - m += *(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); +gproshan_debug(fill_holes); + GT = aGT; + VT = aVT; + OT = aOT; + EVT = aEVT; + ET = aET; + EHT = aEHT; - return m / n_edges; -} + rw(n_vertices) = nv; + rw(n_faces) = nf; + rw(n_half_edges) = nh; + rw(n_edges) = ne; -size_t che::memory() const -{ - return sizeof(*this) + n_vertices * (sizeof(vertex) + sizeof(index_t)) + filename.size() - + sizeof(index_t) * (3 * n_half_edges + n_edges); + update_eht(); } -size_t che::genus() const +void che::update_vertices(const vertex * positions, const size_t & n, const index_t & v_i) { - size_t g = n_vertices - n_edges + n_faces; - return (g - 2) / (-2); + if(!positions) return; + memcpy(GT + v_i, positions, sizeof(vertex) * (!n ? n_vertices : n)); } -// The Gauss-Bonnet Scheme -real_t che::mean_curvature(const index_t & v) const +void che::update_heatmap(const real_t * hm) { - real_t h = 0; - real_t a = 0; - - for_star(he, this, v) + if(!hm) { - a += area_trig(trig(he)); - h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + VHC[v] = 0.45; // default heatmap value + + return; } - return 0.75 * h / a; + memcpy(VHC, hm, n_vertices * sizeof(real_t)); } -void che::normalize() +void che::update_normals() { - vertex center; + if(!n_faces) return; #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) { - #pragma omp critical - center += GT[v]; - } - - center /= n_vertices; + vertex & n = VN[v]; - real_t max_norm = 0; + n = 0; + for_star(he, this, v) + n += area_trig(trig(he)) * normal_he(he); - #pragma omp parallel for reduction(max : max_norm) - for(index_t v = 0; v < n_vertices; ++v) - { - GT[v] -= center; - max_norm = std::max(max_norm, *GT[v]); + n /= *n; } - - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - GT[v] /= max_norm; } -bool che::is_pointcloud() const +void che::invert_normals() { - return n_faces == 0; + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + VN[v] = - VN[v]; } -bool che::is_manifold() const +void che::multiplicate_vertices() { - return manifold; -} + vertex * old_GT = GT; + index_t * old_VT = VT; + size_t nv = n_vertices; + size_t nf = n_faces; -const index_t & che::vt(const index_t & he) const -{ - assert(he < n_half_edges); - return VT[he]; -} + GT = nullptr; + VT = nullptr; -const vertex & che::gt(const index_t & v) const -{ - assert(v < n_vertices); - return GT[v]; -} + free(); + alloc(nv + nf, 3 * nf); -const vertex & che::gt_vt(const index_t & he) const -{ - assert(he < n_half_edges); - return GT[VT[he]]; -} + memcpy(GT, old_GT, nv * sizeof(vertex)); -const vertex & che::gt_vt_next_evt(const index_t & v) const -{ - assert(v < n_vertices); - return GT[VT[next(EVT[v])]]; -} + #pragma omp parallel for + for(index_t f = 0; f < nf; ++f) + { + const index_t & v = nv + f; + const index_t & old_he = f * che::mtrig; + const index_t & he = 3 * f * che::mtrig; -const vertex & che::gt_e(const index_t & e, const bool & op) const -{ - assert(e < n_edges); - return op ? GT[VT[next(ET[e])]] : GT[VT[ET[e]]]; -} + GT[v] = (GT[old_VT[old_he]] + GT[old_VT[old_he + 1]] + GT[old_VT[old_he + 2]]) / 3; -const index_t & che::vt_e(const index_t & e, const bool & op) const -{ - assert(e < n_edges); - return op ? VT[next(ET[e])] : VT[ET[e]]; -} + VT[he] = old_VT[old_he]; + VT[he + 1] = old_VT[old_he + 1]; + VT[he + 2] = v; -const index_t & che::et(const index_t & e) const -{ - assert(e < n_edges); - return ET[e]; -} - -const index_t & che::ot_et(const index_t & e) const -{ - assert(e < n_edges); - return OT[ET[e]]; -} - -const index_t & che::ot(const index_t & he) const -{ - assert(he < n_half_edges); - return OT[he]; -} - -const index_t & che::ot_evt(const index_t & v) const -{ - assert(v < n_vertices); - return OT[EVT[v]]; -} - -const index_t & che::evt(const index_t & v) const -{ - assert(v < n_vertices); - return EVT[v]; -} - -size_t che::max_degree() const -{ - size_t d, md = 0; - - #pragma omp parallel for private(d) reduction(max: md) - for(index_t v = 0; v < n_vertices; ++v) - { - d = 0; - for_star(he, this, v) ++d; - d += is_vertex_bound(v); - md = max(md, d); - } - - return md; -} - -const vertex & che::point(const index_t & v) const -{ - return GT[v]; -} - -vertex & che::point(const index_t & v) -{ - return GT[v]; -} - -void che::update_vertices(const vertex * positions, const size_t & n, const index_t & v_i) -{ - if(!positions) return; - memcpy(GT + v_i, positions, sizeof(vertex) * (!n) ? n_vertices : n); -} - -const string che::filename_size() const -{ - return filename + "_" + to_string(n_vertices); -} - -const string che::name() const -{ - index_t p = filename.find_last_of('/'); - index_t q = filename.find_last_of('.'); - return filename.substr(p + 1, q - p - 1); -} - -const string che::name_size() const -{ - return name() + "_" + to_string(n_vertices); -} - -void che::reload() -{ - free(); - init(filename); -} - -void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector & limits, const vector & sources, const index_t & k) -{ - if(!sources.size()) return; - - memset(toplesets, -1, sizeof(index_t) * n_vertices); - - index_t level = 0; - - index_t p = 0; - for(const index_t & s: sources) - { - sorted[p++] = s; - - if(toplesets[s] == NIL) - toplesets[s] = level; - } - - limits.push_back(0); - for(index_t i = 0; i < p; ++i) - { - const index_t & v = sorted[i]; - - if(toplesets[v] > level) - { - if(++level > k) break; - - limits.push_back(i); - } - - for(const index_t & u: link(v)) - { - if(toplesets[u] == NIL) - { - toplesets[u] = toplesets[v] + 1; - sorted[p++] = u; - } - } - } - - assert(p <= n_vertices); - limits.push_back(p); -} - -void che::multiplicate_vertices() -{ - vertex * old_GT = GT; - index_t * old_VT = VT; - size_t nv = n_vertices; - size_t nf = n_faces; - - GT = nullptr; - VT = nullptr; - - free(); - alloc(nv + nf, 3 * nf); - - memcpy(GT, old_GT, nv * sizeof(vertex)); - - #pragma omp parallel for - for(index_t f = 0; f < nf; ++f) - { - const index_t & v = nv + f; - const index_t & old_he = f * che::mtrig; - const index_t & he = 3 * f * che::mtrig; - - GT[v] = (GT[old_VT[old_he]] + GT[old_VT[old_he + 1]] + GT[old_VT[old_he + 2]]) / 3; - - VT[he] = old_VT[old_he]; - VT[he + 1] = old_VT[old_he + 1]; - VT[he + 2] = v; - - VT[he + 3] = old_VT[old_he + 1]; - VT[he + 4] = old_VT[old_he + 2]; - VT[he + 5] = v; + VT[he + 3] = old_VT[old_he + 1]; + VT[he + 4] = old_VT[old_he + 2]; + VT[he + 5] = v; VT[he + 6] = old_VT[old_he + 2]; VT[he + 7] = old_VT[old_he]; @@ -722,18 +466,27 @@ void che::multiplicate_vertices() } } -void che::remove_non_manifold_vertices() +void che::remove_vertices(const vector & vertices) { - for( index_t he = 0; he < n_half_edges; he+=3) + if(!vertices.size()) return; + + gproshan_debug(removing vertex); + for(index_t v: vertices) { - if(EVT[VT[he]] == NIL || EVT[VT[he+1]] == NIL || EVT[VT[he+2]] == NIL) + for_star(he, this, v) { VT[he] = NIL; - VT[he + 1] = NIL; - VT[he + 2] = NIL; + VT[prev(he)] = NIL; + VT[next(he)] = NIL; + + gproshan_debug_var(he); + gproshan_debug_var(next(he)); + gproshan_debug_var(prev(he)); } - } + gproshan_debug_var(EVT[v]); + EVT[v] = NIL; + } /* save in vectors */ vector new_vertices; vector removed; @@ -781,27 +534,18 @@ void che::remove_non_manifold_vertices() gproshan_debug(removing vertex); } -void che::remove_vertices(const vector & vertices) +void che::remove_non_manifold_vertices() { - if(!vertices.size()) return; - - gproshan_debug(removing vertex); - for(index_t v: vertices) + for( index_t he = 0; he < n_half_edges; he+=3) { - for_star(he, this, v) + if(EVT[VT[he]] == NIL || EVT[VT[he+1]] == NIL || EVT[VT[he+2]] == NIL) { VT[he] = NIL; - VT[prev(he)] = NIL; - VT[next(he)] = NIL; - - gproshan_debug_var(he); - gproshan_debug_var(next(he)); - gproshan_debug_var(prev(he)); + VT[he + 1] = NIL; + VT[he + 2] = NIL; } - - gproshan_debug_var(EVT[v]); - EVT[v] = NIL; } + /* save in vectors */ vector new_vertices; vector removed; @@ -849,139 +593,418 @@ void che::remove_vertices(const vector & vertices) gproshan_debug(removing vertex); } -void che::merge(const che * mesh, const vector & com_vertices) +void che::set_head_vertices(index_t * head, const size_t & n) { -// write_file("big.off"); -// mesh->write_file("small.off"); -gproshan_debug(fill_holes); - size_t ncv = com_vertices.size(); - bool is_open = mesh->VT[next(mesh->EVT[0])] >= ncv; - gproshan_debug_var(is_open); + for(index_t v, i = 0; i < n; ++i) + { + v = head[i]; - size_t nv = n_vertices + mesh->n_vertices - ncv; - size_t nf = n_faces + mesh->n_faces; - size_t nh = n_half_edges + mesh->n_half_edges; - size_t ne = n_edges + mesh->n_edges - (ncv - is_open); + for(index_t j = i + 1; j < n; ++j) + if(i == head[j]) + { + head[j] = v; + break; + } - vertex * aGT = new vertex[nv]; - index_t * aVT = new index_t[nh]; - index_t * aOT = new index_t[nh]; - index_t * aEVT = new index_t[nv]; - index_t * aET = new index_t[ne]; - index_t * aEHT = new index_t[nh]; + swap(GT[v], GT[i]); -gproshan_debug(fill_holes); - memcpy(aGT, GT, sizeof(vertex) * n_vertices); - memcpy(aGT + n_vertices, mesh->GT + ncv, sizeof(vertex) * (nv - n_vertices)); + for_star(he, this, v) + VT[he] = i; + for_star(he, this, i) + VT[he] = v; - memcpy(aVT, VT, sizeof(index_t) * n_half_edges); + swap(EVT[v], EVT[i]); + } +} - index_t * t_aVT = aVT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; ++he) - t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices - ncv; -gproshan_debug(fill_holes); - memcpy(aOT, OT, sizeof(index_t) * n_half_edges); -gproshan_debug(fill_holes); +// half edge access methods triangular faces and navigation - index_t * t_aOT = aOT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; ++he) - t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges : NIL; -gproshan_debug(fill_holes); +const index_t & che::vt(const index_t & he) const +{ + assert(he < n_half_edges); + return VT[he]; +} - for(index_t v, he_v, he_i, i = 0; i < ncv; ++i) - { - he_i = mesh->EVT[i]; - if(he_i != NIL && mesh->VT[next(he_i)] < ncv) - { - v = com_vertices[mesh->VT[next(he_i)]]; - he_v = EVT[v]; - aOT[he_v] = he_i + n_half_edges; - aOT[aOT[he_v]] = he_v; - } - } +const index_t & che::vt_e(const index_t & e, const bool & op) const +{ + assert(e < n_edges); + return op ? VT[next(ET[e])] : VT[ET[e]]; +} -gproshan_debug(fill_holes); - memcpy(aEVT, EVT, sizeof(index_t) * n_vertices); -gproshan_debug(fill_holes); - if(is_open) - aEVT[com_vertices[0]] = mesh->EVT[0] != NIL ? mesh->EVT[0] + n_half_edges : NIL; +const index_t & che::et(const index_t & e) const +{ + assert(e < n_edges); + return ET[e]; +} -gproshan_debug(fill_holes); - index_t * t_aEVT = aEVT + n_vertices; - for(index_t v = ncv; v < mesh->n_vertices; ++v) - t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges : NIL; -gproshan_debug(fill_holes); +const index_t & che::ot_et(const index_t & e) const +{ + assert(e < n_edges); + return OT[ET[e]]; +} - memcpy(aET, ET, sizeof(index_t) * n_edges); -gproshan_debug(fill_holes); +const index_t & che::ot(const index_t & he) const +{ + assert(he < n_half_edges); + return OT[he]; +} - bool * common_edge = new bool[mesh->n_edges]; - memset(common_edge, 0, sizeof(bool) * mesh->n_edges); - for(index_t he_i, i = 0; i < ncv; ++i) - { - he_i = mesh->EVT[i]; - if(he_i != NIL && mesh->VT[next(he_i)] < ncv) - common_edge[mesh->EHT[he_i]] = true; - } +const index_t & che::ot_evt(const index_t & v) const +{ + assert(v < n_vertices); + return OT[EVT[v]]; +} - index_t ae = n_edges; - for(index_t e = 0; e < mesh->n_edges; ++e) - if(!common_edge[e]) - aET[ae++] = mesh->ET[e] + n_half_edges; +const index_t & che::evt(const index_t & v) const +{ + assert(v < n_vertices); + return EVT[v]; +} - gproshan_debug_var(ae == ne); - gproshan_debug_var(ae); - gproshan_debug_var(ne); - assert(ae == ne); - delete [] common_edge; -gproshan_debug(fill_holes); - free(); -gproshan_debug(fill_holes); - GT = aGT; - VT = aVT; - OT = aOT; - EVT = aEVT; - ET = aET; - EHT = aEHT; +// topology methods - rw(n_vertices) = nv; - rw(n_faces) = nf; - rw(n_half_edges) = nh; - rw(n_edges) = ne; +vector che::star(const index_t & v) const +{ + assert(v >= n_vertices); - update_eht(); + vector vstar; + for_star(he, this, v) + vstar.push_back(he); + + return vstar; } -void che::set_head_vertices(index_t * head, const size_t & n) +vector che::link(const index_t & v) const { - for(index_t v, i = 0; i < n; ++i) + assert(v >= n_vertices); + + vector vlink; + + if(is_vertex_bound(v)) + vlink.push_back(VT[next(EVT[v])]); + + for_star(he, this, v) + vlink.push_back(VT[prev(he)]); + + return vlink; +} + +void che::edge_collapse(const std::vector & sort_edges) +{ + // TODO +} + +void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector & limits, const vector & sources, const index_t & k) +{ + if(!sources.size()) return; + + memset(toplesets, -1, sizeof(index_t) * n_vertices); + + index_t level = 0; + + index_t p = 0; + for(const index_t & s: sources) { - v = head[i]; + sorted[p++] = s; - for(index_t j = i + 1; j < n; ++j) - if(i == head[j]) + if(toplesets[s] == NIL) + toplesets[s] = level; + } + + limits.push_back(0); + for(index_t i = 0; i < p; ++i) + { + const index_t & v = sorted[i]; + + if(toplesets[v] > level) + { + if(++level > k) break; + + limits.push_back(i); + } + + for(const index_t & u: link(v)) + { + if(toplesets[u] == NIL) { - head[j] = v; - break; + toplesets[u] = toplesets[v] + 1; + sorted[p++] = u; } + } + } - swap(GT[v], GT[i]); + assert(p <= n_vertices); + limits.push_back(p); +} - for_star(he, this, v) - VT[he] = i; - for_star(he, this, i) - VT[he] = v; - swap(EVT[v], EVT[i]); +// boundary methods + +///< return a vector of indices of one vertex per boundary +vector che::bounds() const +{ + if(!n_faces) return {}; + if(!manifold) return {}; + + vector vbounds; + + bool * is_bound = new bool[n_vertices]; + memset(is_bound, 0, sizeof(bool) * n_vertices); + + for(index_t v = 0; v < n_vertices; ++v) + if(!is_bound[v] && is_vertex_bound(v)) + { + vbounds.push_back(v); + + for_boundary(he, this, v) + is_bound[VT[he]] = true; + } + + delete [] is_bound; + + return vbounds; +} + +///< return a vector of the indices of the boundary where v belongs +vector che::boundary(const index_t & v) const +{ + vector vbound; + + for_boundary(he, this, v) + vbound.push_back(VT[he]); + + return vbound; +} + +bool che::is_vertex_bound(const index_t & v) const +{ + assert(v < n_vertices && EVT[v] < n_half_edges); + return EVT[v] != NIL && OT[EVT[v]] == NIL; +} + +bool che::is_edge_bound(const index_t & e) const +{ + return OT[ET[e]] == NIL; +} + + +// file, name, and system methods + +const string che::name() const +{ + index_t p = filename.find_last_of('/'); + index_t q = filename.find_last_of('.'); + return filename.substr(p + 1, q - p - 1); +} + +const string che::name_size() const +{ + return name() + "_" + to_string(n_vertices); +} + +const string che::filename_size() const +{ + return filename + "_" + to_string(n_vertices); +} + + +// mesh information methods + +size_t che::genus() const +{ + size_t g = n_vertices - n_edges + n_faces; + return (g - 2) / (-2); +} + +size_t che::memory() const +{ + return sizeof(*this) + + n_vertices * (2 * sizeof(vertex) + sizeof(index_t) + sizeof(real_t) + sizeof(rgb_t)) + + sizeof(index_t) * (3 * n_half_edges + n_edges) + + filename.size(); +} + +size_t che::max_degree() const +{ + size_t d, md = 0; + + #pragma omp parallel for private(d) reduction(max: md) + for(index_t v = 0; v < n_vertices; ++v) + { + d = 0; + for_star(he, this, v) ++d; + d += is_vertex_bound(v); + md = max(md, d); } + + return md; } -void che::edge_collapse(const index_t *const & sort_edges) +real_t che::quality() const { + real_t q = 0; + + #pragma omp parallel for reduction(+: q) + for(index_t t = 0; t < n_faces; ++t) + q += pdetriq(t) > 0.6; // is confederating good triangle + + return q * 100 / n_faces; } +real_t che::mean_edge() const +{ + real_t m = 0; + + #pragma omp parallel for reduction(+: m) + for(index_t e = 0; e < n_edges; ++e) + m += *(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); + + return m / n_edges; +} + +real_t che::area_surface() const +{ + real_t area = 0; + + #pragma omp parallel for reduction(+: area) + for(index_t i = 0; i < n_faces; ++i) + area += area_trig(i); + + return area; +} + +bool che::is_manifold() const +{ + return manifold; +} + +bool che::is_pointcloud() const +{ + return n_faces == 0; +} + + +// operation methods + +void che::flip(const index_t & e) +{ + index_t ha = ET[e]; + index_t hb = OT[ha]; + + if(hb == NIL) + return; + + index_t va = VT[ha]; + index_t vb = VT[hb]; + index_t vc = VT[prev(ha)]; + index_t vd = VT[prev(hb)]; + + index_t et_pa = EHT[prev(ha)]; + index_t et_na = EHT[next(ha)]; + index_t et_pb = EHT[prev(hb)]; + index_t et_nb = EHT[next(hb)]; + + index_t ot_pa = OT[prev(ha)]; + index_t ot_na = OT[next(ha)]; + index_t ot_pb = OT[prev(hb)]; + index_t ot_nb = OT[next(hb)]; + + VT[prev(ha)] = vb; + VT[ha] = vc; + VT[next(ha)] = vd; + VT[prev(hb)] = va; + VT[hb] = vd; + VT[next(hb)] = vc; + + if(ot_pa != NIL) OT[ot_pa] = next(hb); + if(ot_na != NIL) OT[ot_na] = prev(ha); + if(ot_pb != NIL) OT[ot_pb] = next(ha); + if(ot_nb != NIL) OT[ot_nb] = prev(hb); + + OT[prev(ha)] = ot_na; + OT[next(ha)] = ot_pb; + OT[prev(hb)] = ot_nb; + OT[next(hb)] = ot_pa; + + ET[et_pa] = prev(ha); + ET[et_na] = next(ha); + ET[et_pb] = prev(hb); + ET[et_nb] = next(hb); + + EHT[prev(ha)] = EHT[OT[prev(ha)]] = et_pa; + EHT[next(ha)] = EHT[OT[next(ha)]] = et_na; + EHT[prev(hb)] = EHT[OT[prev(hb)]] = et_pb; + EHT[next(hb)] = EHT[OT[next(hb)]] = et_nb; + + if(EVT[va] == next(hb) || EVT[va] == ha) EVT[va] = prev(hb); + if(EVT[vb] == next(ha) || EVT[vb] == hb) EVT[vb] = prev(ha); + if(EVT[vc] == prev(ha)) EVT[vc] = next(hb); + if(EVT[vd] == prev(hb)) EVT[vd] = next(ha); +} + +real_t che::cotan(const index_t & he) const +{ + if(he == NIL) return 0; + + vertex a = GT[VT[he]] - GT[VT[prev(he)]]; + vertex b = GT[VT[next(he)]] - GT[VT[prev(he)]]; + + return (a, b) / *(a * b); +} + +// https://www.mathworks.com/help/pde/ug/pdetriq.html +// 4*sqrt(3)*a +// q = ---------------- +// h1^2+h2^2+h3^2 +real_t che::pdetriq(const index_t & t) const +{ + index_t he = t * che::mtrig; + real_t h[3] = { + *(GT[VT[next(he)]] - GT[VT[he]]), + *(GT[VT[prev(he)]] - GT[VT[next(he)]]), + *(GT[VT[he]] - GT[VT[prev(he)]]) + }; + return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); +} + +real_t che::area_trig(const index_t & t) const +{ + index_t he = t * che::mtrig; + vertex a = GT[VT[next(he)]] - GT[VT[he]]; + vertex b = GT[VT[prev(he)]] - GT[VT[he]]; + + return *(a * b) / 2; +} + +real_t che::area_vertex(const index_t & v) const +{ + real_t area_star = 0; + for_star(he, this, v) + area_star += area_trig(trig(he)); + + return area_star / 3; +} + +// The Gauss-Bonnet Scheme +real_t che::mean_curvature(const index_t & v) const +{ + real_t h = 0; + real_t a = 0; + + for_star(he, this, v) + { + a += area_trig(trig(he)); + h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); + } + + return 0.75 * h / a; +} + + +// protected + void che::init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) { alloc(n_v, n_f); diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 3be33835..814452ce 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -43,7 +43,7 @@ void che_img::read_file(const string & file) VT[he++] = (i - 1) * img.height() + j - 1; } - GT[v++] = {i, j, img(i, j)}; + GT[v++] = {real_t(i), real_t(j), img(i, j)}; } thread([](CImg img) { img.display(); }, img).detach(); diff --git a/src/gproshan/mesh/vertex.cpp b/src/gproshan/mesh/vertex.cpp index f31830fb..6e07fc90 100644 --- a/src/gproshan/mesh/vertex.cpp +++ b/src/gproshan/mesh/vertex.cpp @@ -116,6 +116,21 @@ vertex operator * (const real_t & a, const vertex & v) return {a * v.x, a * v.y, a * v.z}; } +vertex cross(const vertex & u, const vertex & v) +{ + return u * v; +} + +real_t dot(const vertex & u, const vertex & v) +{ + return (u, v); +} + +real_t norm(const vertex & v) +{ + return *v; +} + vertex normalize(const vertex & v) { return v / *v; @@ -123,14 +138,12 @@ vertex normalize(const vertex & v) ostream & operator << (ostream & os, const vertex & v) { - os << v.x << " " << v.y << " " << v.z; - return os; + return os << v.x << " " << v.y << " " << v.z; } istream & operator >> (istream & is, vertex & v) { - is >> v.x >> v.y >> v.z; - return is; + return is >> v.x >> v.y >> v.z; } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 96f0ce79..31ba3824 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -49,7 +49,7 @@ vertex embree::ray_hit::color(const rt_mesh & mesh) const if(mesh.pointcloud) return mesh->color(hit.primID); - return mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + return mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u); } vertex embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const @@ -57,7 +57,7 @@ vertex embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const if(flat || mesh.pointcloud) return normalize({hit.Ng_x, hit.Ng_y, hit.Ng_z}); - const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u, hit.v); + const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u); return n / *n; } @@ -193,10 +193,10 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_mat) RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE); vertex * vertices = (vertex *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT3, 3 * sizeof(float), - mesh->n_vertices - ); + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT3, 3 * sizeof(float), + mesh->n_vertices + ); index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, @@ -261,7 +261,7 @@ float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, do { - glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); + //glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); sum_w += w = 1; //pc_radius - glm::length(r.position() - vertex(xyzr[r.hit.primID])); position += w * r.position(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4195b50e..a59fa7e4 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -600,7 +600,7 @@ bool viewer::m_save_mesh(viewer * view) bool viewer::m_normalize_mesh(viewer * view) { che_viewer & mesh = view->active_mesh(); - mesh->normalize(); + mesh->normalize_sphere(); mesh.update(); return false; From 62a94753e6490849cba1793b1e0d21e6f6cfd0bf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Jun 2022 15:20:06 +0200 Subject: [PATCH 0666/1018] mesh che: refactoring halfedge access methods --- apps/test_scanner.cpp | 2 +- include/gproshan/mesh/che.h | 22 ++++---- src/gproshan/app_viewer.cpp | 26 ++++----- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/features/key_points.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 20 +++---- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 +- .../test_geodesics_ptp_coalescence.cu | 8 +-- src/gproshan/laplacian/fairing_spectral.cpp | 2 +- src/gproshan/laplacian/fairing_taubin.cpp | 2 +- src/gproshan/laplacian/laplacian.cpp | 8 +-- src/gproshan/mdict/msparse_coding.cpp | 6 +- src/gproshan/mdict/patch.cpp | 38 ++++++------- src/gproshan/mesh/che.cpp | 56 +++++++++---------- src/gproshan/mesh/che_fill_hole.cpp | 28 +++++----- src/gproshan/mesh/che_obj.cpp | 4 +- src/gproshan/mesh/che_off.cpp | 4 +- src/gproshan/mesh/che_ply.cpp | 6 +- src/gproshan/mesh/che_poisson.cpp | 24 ++++---- src/gproshan/mesh/che_pts.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_xyz.cpp | 2 +- src/gproshan/mesh/simplification.cpp | 9 ++- src/gproshan/raytracing/rt_embree.cpp | 8 +-- src/gproshan/viewer/che_viewer.cpp | 8 +-- src/gproshan/viewer/viewer.cpp | 4 +- 28 files changed, 150 insertions(+), 151 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index aaa5165c..78235fc4 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -29,7 +29,7 @@ glm::mat4 normalize_coordinates(gproshan::che_ply * mesh) for(gproshan::index_t v = 0; v < mesh->n_vertices; ++v) { - const gproshan::vertex & p = mesh->gt(v); + const gproshan::vertex & p = mesh->point(v); pmin.x = std::min(pmin.x, p.x); pmin.y = std::min(pmin.y, p.y); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 4ee8f228..84808972 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -8,10 +8,10 @@ #include #define for_star(he, mesh, v) \ - for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->ot(prev(he))) != stop ? he : NIL) + for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->twin_he(prev(he))) != stop ? he : NIL) #define for_boundary(he, mesh, v) \ - for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->vt(next(he)))) != stop ? he : NIL) + for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->halfedge(next(he)))) != stop ? he : NIL) // geometry processing and shape analysis framework @@ -73,10 +73,7 @@ class che virtual ~che(); // vertex access geometry methods to xyz point values, normals, and gradient - const vertex & gt(const index_t & v) const; - const vertex & gt_vt(const index_t & he) const; const vertex & gt_vt_next_evt(const index_t & v) const; - const vertex & gt_e(const index_t & e, const bool & op = false) const; const vertex & point(const index_t & v) const; vertex & point(const index_t & v); const vertex & normal(const index_t & v) const; @@ -109,12 +106,15 @@ class che void set_head_vertices(index_t * head, const size_t & n); // half edge access methods triangular faces and navigation - const index_t & vt(const index_t & he) const; - const index_t & vt_e(const index_t & e, const bool & op = false) const; - const index_t & et(const index_t & e) const; - const index_t & ot_et(const index_t & e) const; - const index_t & ot(const index_t & he) const; - const index_t & ot_evt(const index_t & v) const; + const index_t & halfedge(const index_t & he) const; + const index_t & twin_he(const index_t & he) const; + const index_t & edge_u(const index_t & e) const; + const index_t & edge_v(const index_t & e) const; + const index_t & edge_he_0(const index_t & e) const; + const index_t & edge_he_1(const index_t & e) const; + const vertex & vertex_he(const index_t & he) const; + const vertex & vertex_edge_u(const index_t & e) const; + const vertex & vertex_edge_v(const index_t & e) const; const index_t & evt(const index_t & v) const; // topology methods diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 2cd9ba63..e6155ae9 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -149,7 +149,7 @@ bool app_viewer::process_convex_hull(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - convex_hull ch(&mesh->gt(0), mesh->n_vertices); + convex_hull ch(&mesh->point(0), mesh->n_vertices); mesh.selected = ch; return false; @@ -219,8 +219,8 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) g = 0; for_star(he, mesh, v) { - a = mesh->gt_vt(next(he)) - mesh->gt(v); - b = mesh->gt_vt(prev(he)) - mesh->gt(v); + a = mesh->vertex_he(next(he)) - mesh->point(v); + b = mesh->vertex_he(prev(he)) - mesh->point(v); g += acos((a,b) / (*a * *b)); } //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); @@ -340,7 +340,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) if(vertices.size() != mesh->n_vertices) { vertices.resize(mesh->n_vertices); - memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + memcpy(vertices.data(), &mesh->point(0), mesh->n_vertices * sizeof(vertex)); } else mesh->update_vertices(vertices.data()); @@ -369,7 +369,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) if(vertices.size() != mesh->n_vertices) { vertices.resize(mesh->n_vertices); - memcpy(vertices.data(), &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + memcpy(vertices.data(), &mesh->point(0), mesh->n_vertices * sizeof(vertex)); } else mesh->update_vertices(vertices.data()); @@ -564,23 +564,23 @@ bool app_viewer::process_mdict_patch(viewer * p_view) vdir.x = p.T(0, 0); vdir.y = p.T(0, 1); vdir.z = p.T(0, 2); - view->vectors.push_back(mesh->gt(v)); - view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->point(v)); + view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); vdir.x = p.T(1, 0); vdir.y = p.T(1, 1); vdir.z = p.T(1, 2); - view->vectors.push_back(mesh->gt(v)); - view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->point(v)); + view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); vdir.x = p.T(2, 0); vdir.y = p.T(2, 1); vdir.z = p.T(2, 2); - view->vectors.push_back(mesh->gt(v)); - view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * vdir); + view->vectors.push_back(mesh->point(v)); + view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); - view->vectors.push_back(mesh->gt(v)); - view->vectors.push_back(mesh->gt(v) + 3 * mean_edge * mesh->normal(v)); + view->vectors.push_back(mesh->point(v)); + view->vectors.push_back(mesh->point(v) + 3 * mean_edge * mesh->normal(v)); avg_nvp += p.vertices.size(); } diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index 5f71b671..1f2b9b28 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -55,7 +55,7 @@ void key_components::compute_kcs(che * mesh, const std::vector & kps) radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) for_star(he, mesh, fm(i)) - join(fm(i), mesh->vt(next(he))); + join(fm(i), mesh->halfedge(next(he))); for(index_t i = 0; i < n_vertices; ++i) if(comp[i] == i && comp_size[i] > 1) diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 67552f73..5440671e 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -42,7 +42,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) he = che::mtrig * face_areas[t].second; for(index_t i = 0; i < che::mtrig; ++i) { - const index_t & v = mesh->vt(he); + const index_t & v = mesh->halfedge(he); if(!is_kp[v]) { kps.push_back(v); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 7e26a52b..ef8a133b 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -175,7 +175,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co dv = dp; if(clusters) - clusters[v] = clusters[mesh->vt(prev(he))] ? clusters[mesh->vt(prev(he))] : clusters[mesh->vt(next(he))]; + clusters[v] = clusters[mesh->halfedge(prev(he))] ? clusters[mesh->halfedge(prev(he))] : clusters[mesh->halfedge(next(he))]; } } diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index dda84d70..972a198f 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -27,13 +27,13 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); ++i) { - V[i] = mesh->gt(toplesets.index[i]); + V[i] = mesh->point(toplesets.index[i]); inv[toplesets.index[i]] = i; } for(index_t he = 0; he < mesh->n_half_edges; ++he) - if(inv[mesh->vt(he)] != NIL && inv[mesh->vt(prev(he))] != NIL && inv[mesh->vt(next(he))] != NIL) - F.push_back(inv[mesh->vt(he)]); + if(inv[mesh->halfedge(he)] != NIL && inv[mesh->halfedge(prev(he))] != NIL && inv[mesh->halfedge(next(he))] != NIL) + F.push_back(inv[mesh->halfedge(he)]); return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); } @@ -89,7 +89,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c pdist[!d][v] = p; if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->vt(prev(he))] != NIL ? ptp_out.clusters[mesh->vt(prev(he))] : ptp_out.clusters[mesh->vt(next(he))]; + ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(prev(he))] : ptp_out.clusters[mesh->halfedge(next(he))]; } } } @@ -166,7 +166,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c pdist[!d][v] = p; if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->vt(prev(he))] != NIL ? ptp_out.clusters[mesh->vt(prev(he))] : ptp_out.clusters[mesh->vt(next(he))]; + ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(prev(he))] : ptp_out.clusters[mesh->halfedge(next(he))]; } } } @@ -202,13 +202,13 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t update_step(che * mesh, const real_t * dist, const index_t & he) { index_t x[3]; - x[0] = mesh->vt(next(he)); - x[1] = mesh->vt(prev(he)); - x[2] = mesh->vt(he); + x[0] = mesh->halfedge(next(he)); + x[1] = mesh->halfedge(prev(he)); + x[2] = mesh->halfedge(he); vertex X[2]; - X[0] = mesh->gt(x[0]) - mesh->gt(x[2]); - X[1] = mesh->gt(x[1]) - mesh->gt(x[2]); + X[0] = mesh->point(x[0]) - mesh->point(x[2]); + X[1] = mesh->point(x[1]) - mesh->point(x[2]); real_t t[2]; t[0] = dist[x[0]]; diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index da8a5a6b..d53eff9e 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -95,7 +95,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) for_star(he, mesh, v) { const vertex & nhe = mesh->normal_he(he); - const vertex & vhe = mesh->gt_vt(prev(he)) - mesh->gt_vt(next(he)); + const vertex & vhe = mesh->vertex_he(prev(he)) - mesh->vertex_he(next(he)); const vertex & ghe = mesh->gradient_he(he, u.memptr()); sum += (nhe * vhe , -ghe); } diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 32dda01f..30205448 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -159,7 +159,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) map deg; for(index_t v = 0; v < n_vertices; ++v) { - dv = mesh->ot_evt(v) == NIL ? 1 : 0; + dv = mesh->is_vertex_bound(v) ? 1 : 0; for_star(he, mesh, v) ++dv; ++deg[dv]; } diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index bbe60d9b..797afeaf 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -32,14 +32,14 @@ vector > iter_error_parallel_toplesets_propagation_coalesc #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - V[i] = mesh->gt(sorted_index[i]); + V[i] = mesh->point(sorted_index[i]); inv[sorted_index[i]] = i; exact_dist_sorted[i] = exact_dist[sorted_index[i]]; } #pragma omp parallel for for(index_t he = 0; he < mesh->n_half_edges; ++he) - F[he] = inv[mesh->vt(he)]; + F[he] = inv[mesh->halfedge(he)]; mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); @@ -150,13 +150,13 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices; ++i) { - V[i] = mesh->gt(sorted_index[i]); + V[i] = mesh->point(sorted_index[i]); inv[sorted_index[i]] = i; } #pragma omp parallel for for(index_t he = 0; he < mesh->n_half_edges; ++he) - F[he] = inv[mesh->vt(he)]; + F[he] = inv[mesh->halfedge(he)]; che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); diff --git a/src/gproshan/laplacian/fairing_spectral.cpp b/src/gproshan/laplacian/fairing_spectral.cpp index 162c09a2..38f77aa5 100644 --- a/src/gproshan/laplacian/fairing_spectral.cpp +++ b/src/gproshan/laplacian/fairing_spectral.cpp @@ -13,7 +13,7 @@ void fairing_spectral::compute(che * mesh) { delete [] vertices; vertices = new vertex[mesh->n_vertices]; - memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); diff --git a/src/gproshan/laplacian/fairing_taubin.cpp b/src/gproshan/laplacian/fairing_taubin.cpp index bc3700c9..484f1a14 100644 --- a/src/gproshan/laplacian/fairing_taubin.cpp +++ b/src/gproshan/laplacian/fairing_taubin.cpp @@ -13,7 +13,7 @@ void fairing_taubin::compute(che * mesh) { delete [] vertices; vertices = new vertex[mesh->n_vertices]; - memcpy(vertices, &mesh->gt(0), mesh->n_vertices * sizeof(vertex)); + memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp index c090a67c..6cf23362 100644 --- a/src/gproshan/laplacian/laplacian.cpp +++ b/src/gproshan/laplacian/laplacian.cpp @@ -24,18 +24,18 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) index_t i = e << 1; DI(0, i) = e; - DI(1, i) = mesh->vt(mesh->et(e)); + DI(1, i) = mesh->edge_u(e); DV(i) = -1; ++i; DI(0, i) = e; - DI(1, i) = mesh->vt(next(mesh->et(e))); + DI(1, i) = mesh->edge_v(e); DV(i) = 1; SI(0, e) = SI(1, e) = e; - SV(e) = (mesh->cotan(mesh->et(e)) + - mesh->cotan(mesh->ot_et(e))) / 2; + SV(e) = (mesh->cotan(mesh->edge_he_0(e)) + + mesh->cotan(mesh->edge_he_1(e))) / 2; } a_sp_mat D(DI, DV, n_edges, n_vertices); diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 15ed47b0..0997c094 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -943,9 +943,9 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) } else { - V(0, v) = mesh->gt(v).x; - V(1, v) = mesh->gt(v).y; - V(2, v) = mesh->gt(v).z; + V(0, v) = mesh->point(v).x; + V(1, v) = mesh->point(v).y; + V(2, v) = mesh->point(v).z; } } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index b9c7884d..b4e96c90 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -92,11 +92,11 @@ bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle for_star(he, mesh, v) { - a = mesh->vt(next(he)); //index of the next vertex index_t - b = mesh->vt(prev(he)); - va = mesh->gt(a); - vb = mesh->gt(b); - vv = mesh->gt(v); + a = mesh->halfedge(next(he)); //index of the next vertex index_t + b = mesh->halfedge(prev(he)); + va = mesh->point(a); + vb = mesh->point(b); + vv = mesh->point(v); // If is an adjacent face assert(a < mesh->n_vertices); assert(b < mesh->n_vertices); @@ -333,7 +333,7 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & { if(!mask || mask(vertices[i])) { - const vertex & v = mesh->gt(vertices[i]); + const vertex & v = mesh->point(vertices[i]); xyz(0, j) = v.x; xyz(1, j) = v.y; xyz(2, j) = v.z; @@ -410,8 +410,8 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co for_star(he, mesh, min_v) { //discard triangles outside the patch - vpatches_t & ma = vpatches[mesh->vt(next(he))]; - vpatches_t & mb = vpatches[mesh->vt(prev(he))]; + vpatches_t & ma = vpatches[mesh->halfedge(next(he))]; + vpatches_t & mb = vpatches[mesh->halfedge(prev(he))]; if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { @@ -489,7 +489,7 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorgt(vi); + const vertex & v = mesh->point(vi); xyz(0, i) = v.x; xyz(1, i) = v.y; xyz(2, i) = v.z; @@ -569,9 +569,9 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, { if(toplevel[u] == NIL) { - p(0) = mesh->gt(u).x; - p(1) = mesh->gt(u).y; - p(2) = mesh->gt(u).z; + p(0) = mesh->point(u).x; + p(1) = mesh->point(u).y; + p(2) = mesh->point(u).z; p = T.t() * (p - x); toplevel[u] = toplevel[v] + 1; @@ -595,7 +595,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) vector in_points; in_points.reserve(vertices.size()); for(const index_t & u: vertices) - in_points.push_back(DPoint(mesh->gt(u).x, mesh->gt(u).y, mesh->gt(u).z)); + in_points.push_back(DPoint(mesh->point(u).x, mesh->point(u).y, mesh->point(u).z)); My_Monge_form monge_form; My_Monge_via_jet_fitting monge_fit; @@ -605,9 +605,9 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); x.set_size(3); - x(0) = mesh->gt(v).x; - x(1) = mesh->gt(v).y; - x(2) = mesh->gt(v).z; + x(0) = mesh->point(v).x; + x(1) = mesh->point(v).y; + x(2) = mesh->point(v).z; T.set_size(3, 3); T(0, 0) = monge_form.maximal_principal_direction()[0]; @@ -625,9 +625,9 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) void patch::normal_fit_directions(che * mesh, const index_t & v) { x.set_size(3); - x(0) = mesh->gt(v).x; - x(1) = mesh->gt(v).y; - x(2) = mesh->gt(v).z; + x(0) = mesh->point(v).x; + x(1) = mesh->point(v).y; + x(2) = mesh->point(v).z; vertex nz = mesh->normal(v); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 6f940e19..aa9da19c 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -85,30 +85,12 @@ che::~che() // vertex access geometry methods to xyz point values, normals, and gradient -const vertex & che::gt(const index_t & v) const -{ - assert(v < n_vertices); - return GT[v]; -} - -const vertex & che::gt_vt(const index_t & he) const -{ - assert(he < n_half_edges); - return GT[VT[he]]; -} - const vertex & che::gt_vt_next_evt(const index_t & v) const { assert(v < n_vertices); return GT[VT[next(EVT[v])]]; } -const vertex & che::gt_e(const index_t & e, const bool & op) const -{ - assert(e < n_edges); - return op ? GT[VT[next(ET[e])]] : GT[VT[ET[e]]]; -} - const vertex & che::point(const index_t & v) const { assert(v < n_vertices); @@ -620,40 +602,58 @@ void che::set_head_vertices(index_t * head, const size_t & n) // half edge access methods triangular faces and navigation -const index_t & che::vt(const index_t & he) const +const index_t & che::halfedge(const index_t & he) const { assert(he < n_half_edges); return VT[he]; } -const index_t & che::vt_e(const index_t & e, const bool & op) const +const index_t & che::twin_he(const index_t & he) const +{ + assert(he < n_half_edges); + return OT[he]; +} + +const index_t & che::edge_u(const index_t & e) const +{ + assert(e < n_edges); + return VT[ET[e]]; +} + +const index_t & che::edge_v(const index_t & e) const { assert(e < n_edges); - return op ? VT[next(ET[e])] : VT[ET[e]]; + return VT[next(ET[e])]; } -const index_t & che::et(const index_t & e) const +const index_t & che::edge_he_0(const index_t & e) const { assert(e < n_edges); return ET[e]; } -const index_t & che::ot_et(const index_t & e) const +const index_t & che::edge_he_1(const index_t & e) const { assert(e < n_edges); return OT[ET[e]]; } -const index_t & che::ot(const index_t & he) const +const vertex & che::vertex_he(const index_t & he) const { assert(he < n_half_edges); - return OT[he]; + return GT[VT[he]]; } -const index_t & che::ot_evt(const index_t & v) const +const vertex & che::vertex_edge_u(const index_t & e) const { - assert(v < n_vertices); - return OT[EVT[v]]; + assert(e < n_edges); + return GT[VT[ET[e]]]; +} + +const vertex & che::vertex_edge_v(const index_t & e) const +{ + assert(e < n_edges); + return GT[VT[next(ET[e])]]; } const index_t & che::evt(const index_t & v) const diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index f8aefbea..87c86934 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -35,7 +35,7 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, for(const index_t & b: border_vertices) { - v = mesh->gt(b); + v = mesh->point(b); normal_v = mesh->normal(b); edge_v = mesh->gt_vt_next_evt(b) - v; edge_v -= (normal_v, edge_v) * normal_v; @@ -95,7 +95,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const for(index_t v = i; v <= end_v; ++v) { vmap_border[v % size] = vertices[c].size() + delta_v; - vertices[c].push_back(mesh->gt(border_vertices[v % size])); + vertices[c].push_back(mesh->point(border_vertices[v % size])); normal += mesh->normal(border_vertices[v % size]); } return end_v - i + 1; @@ -122,7 +122,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const { reverse(merge_vertices[!c].begin(), merge_vertices[!c].end()); for(index_t v: merge_vertices[!c]) - vertices[c].push_back(hole->gt(v)); + vertices[c].push_back(hole->point(v)); normal = 0; n_v = 0; @@ -134,7 +134,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const } else merge_vertices[c].push_back(merge_vertices[!c].back()); - gen_vertices(merge_vertices[c], vertices[c], mesh->gt(border_vertices[p.second]), mesh->gt(border_vertices[p.first]), hole->n_vertices - merge_vertices[!c].size()); + gen_vertices(merge_vertices[c], vertices[c], mesh->point(border_vertices[p.second]), mesh->point(border_vertices[p.first]), hole->n_vertices - merge_vertices[!c].size()); if(i != p.first) { @@ -166,7 +166,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const for(auto & b: border_vertices) { normal += mesh->normal(b); - vertices[c].push_back(mesh->gt(b)); + vertices[c].push_back(mesh->point(b)); } normal /= vertices[c].size(); @@ -177,7 +177,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const reverse(merge_vertices[!c].begin(), merge_vertices[!c].end()); for(index_t v: merge_vertices[!c]) - vertices[c].push_back(hole->gt(v)); + vertices[c].push_back(hole->point(v)); i = i > 0 ? i - 1 : size - 1; j = (j + 1) % size; @@ -221,9 +221,9 @@ void split_border(vector > & split_indices, che * mesh, c data(1, i) = normal.y; data(2, i) = normal.z; /* - data(3, i) = mesh->gt(border_vertices[i]).x; - data(4, i) = mesh->gt(border_vertices[i]).y; - data(5, i) = mesh->gt(border_vertices[i]).z; + data(3, i) = mesh->point(border_vertices[i]).x; + data(4, i) = mesh->point(border_vertices[i]).y; + data(5, i) = mesh->point(border_vertices[i]).z; */ } @@ -331,7 +331,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vector faces; for(index_t v: front_vertices) - vertices.push_back(mesh->gt(v)); + vertices.push_back(mesh->point(v)); vector tmp_vertices(vertices.size()); vector tmp_normals(vertices.size()); @@ -839,17 +839,17 @@ void get_real_tri(che * mesh, vector & select_vertices, vector { if(i < b) { - tri[0] += mesh->gt(select_vertices[i]); + tri[0] += mesh->point(select_vertices[i]); ++tri_sizes[0]; } else if ( i >= b && i < c) { - tri[1] += mesh->gt(select_vertices[i]); + tri[1] += mesh->point(select_vertices[i]); ++tri_sizes[1]; } else { - tri[2] += mesh->gt(select_vertices[i]); + tri[2] += mesh->point(select_vertices[i]); ++tri_sizes[2]; } } @@ -888,7 +888,7 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i index_t i = 0; for(index_t v: select_vertices) - vertices[i++] = mesh->gt(v); + vertices[i++] = mesh->point(v); vertices[i++] = triangle[0]; vertices[i++] = triangle[1]; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 35989566..65021e9f 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -84,7 +84,7 @@ void che_obj::write_file(const che * mesh, const string & file, const bool & col for(index_t i = 0; i < mesh->n_vertices; ++i) { - const vertex & v = mesh->gt(i); + const vertex & v = mesh->point(i); fprintf(fp, "v %f %f %f", (float) v.x, (float) v.y, (float) v.z); if(color) { @@ -100,7 +100,7 @@ void che_obj::write_file(const che * mesh, const string & file, const bool & col { fprintf(fp, "f"); for(index_t i = 0; i < che::mtrig; ++i) - fprintf(fp, " %u", mesh->vt(he++) + 1); + fprintf(fp, " %u", mesh->halfedge(he++) + 1); fprintf(fp, "\n"); } } diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index c468bc45..aee004f8 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -96,7 +96,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t for(size_t i = 0; i < mesh->n_vertices; ++i) { - const vertex & v = mesh->gt(i); + const vertex & v = mesh->point(i); fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); if(off == COFF || off == NCOFF) @@ -120,7 +120,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t { fprintf(fp, "%u", che::mtrig); for(index_t i = 0; i < che::mtrig; ++i) - fprintf(fp, " %u", mesh->vt(he++)); + fprintf(fp, " %u", mesh->halfedge(he++)); fprintf(fp, "\n"); } } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 5ead8743..bc3c9a6e 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -251,17 +251,17 @@ void che_ply::write_file(const che * mesh, const string & file, const bool & col { for(index_t v = 0; v < mesh->n_vertices; ++v) { - fwrite(&mesh->gt(v), sizeof(vertex), 1, fp); + fwrite(&mesh->point(v), sizeof(vertex), 1, fp); fwrite(&mesh->rgb(v), sizeof(rgb_t), 1, fp); } } - else fwrite(&mesh->gt(0), sizeof(vertex), mesh->n_vertices, fp); + else fwrite(&mesh->point(0), sizeof(vertex), mesh->n_vertices, fp); unsigned char mtrig = che::mtrig; for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) { fwrite(&mtrig, 1, 1, fp); - fwrite(&mesh->vt(he), sizeof(index_t), che::mtrig, fp); + fwrite(&mesh->halfedge(he), sizeof(index_t), che::mtrig, fp); } fclose(fp); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index fa017691..1b8b3a65 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -19,9 +19,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) { if(v < old_n_vertices) { - B(v, 0) = mesh->gt(v).x; - B(v, 1) = mesh->gt(v).y; - B(v, 2) = mesh->gt(v).z; + B(v, 0) = mesh->point(v).x; + B(v, 1) = mesh->point(v).y; + B(v, 2) = mesh->point(v).z; } else B.row(v).zeros(); } @@ -40,9 +40,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) for(index_t v = 0; v < old_n_vertices; ++v) { - B.col(0) -= mesh->gt(v).x * L.col(v); - B.col(1) -= mesh->gt(v).y * L.col(v); - B.col(2) -= mesh->gt(v).z * L.col(v); + B.col(0) -= mesh->point(v).x * L.col(v); + B.col(1) -= mesh->point(v).y * L.col(v); + B.col(2) -= mesh->point(v).z * L.col(v); } L.shed_cols(0, old_n_vertices - 1); @@ -122,9 +122,9 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t index_t i = 0; for(index_t & b: sub_mesh_hole) { - P(0, i) = mesh->gt(b).x; - P(1, i) = mesh->gt(b).y; - P(2, i) = mesh->gt(b).z; + P(0, i) = mesh->point(b).x; + P(1, i) = mesh->point(b).y; + P(2, i) = mesh->point(b).z; ++i; } @@ -132,9 +132,9 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++i, ++v) { - H(0, i) = mesh->gt(v).x; - H(1, i) = mesh->gt(v).y; - H(2, i) = mesh->gt(v).z; + H(0, i) = mesh->point(v).x; + H(1, i) = mesh->point(v).y; + H(2, i) = mesh->point(v).z; } a_vec avg = mean(H, 1); diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index 96f68175..40131284 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -54,7 +54,7 @@ void che_pts::write_file(const che * mesh, const string & file) fprintf(fp, "%lu\n", mesh->n_vertices); for(index_t i = 0; i < mesh->n_vertices; ++i) { - const vertex & v = mesh->gt(i); + const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); fprintf(fp, " %d ", int(mesh->heatmap(i) * 4095) - 2048); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index c0fc1f0e..4a689225 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -139,7 +139,7 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ for(size_t i = 0; i < mesh->n_vertices; ++i) { - const vertex & v = mesh->gt(i); + const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x, (float) v.y, (float) v.z, (float) 0, c.r, c.g, c.b ); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 2040075b..2dbf6ed0 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -57,7 +57,7 @@ void che_xyz::write_file(const che * mesh, const string & file, const bool & col for(index_t i = 0; i < mesh->n_vertices; ++i) { - const vertex & v = mesh->gt(i); + const vertex & v = mesh->point(i); fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); if(color) { diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 91a0c790..0e45d433 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -44,7 +44,7 @@ void simplification::compute_quadrics() p(0) = n.x; p(1) = n.y; p(2) = n.z; - p(3) = -(n, mesh->gt(v)); + p(3) = -(n, mesh->point(v)); Q[v] += p * p.t(); } @@ -78,14 +78,13 @@ real_t simplification::compute_error(const index_t & e) v(2) = ve.z; v(3) = 1; - return as_scalar(v.t() * (Q[mesh->vt_e(e)] + Q[mesh->vt_e(e, true)]) * v); + return as_scalar(v.t() * (Q[mesh->edge_u(e)] + Q[mesh->edge_v(e)]) * v); } vertex simplification::create_vertex(const index_t & e) { - vertex va, vb; - va = mesh->gt_e(e); - vb = mesh->gt_e(e, true); + const vertex & va = mesh->vertex_edge_u(e); + const vertex & vb = mesh->vertex_edge_v(e); return (va + vb) / 2; } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 31ba3824..a7c58ef4 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -80,7 +80,7 @@ index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const w = hit.v; } - return mesh->vt(he); + return mesh->halfedge(he); } vertex embree::ray_hit::position() const @@ -207,11 +207,11 @@ index_t embree::add_mesh(const che * mesh, const glm::mat4 & model_mat) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - const glm::vec4 & v = model_mat * glm::vec4(glm_vec3(mesh->gt(i)), 1); + const glm::vec4 & v = model_mat * glm::vec4(glm_vec3(mesh->point(i)), 1); vertices[i] = {v.x, v.y, v.z}; } - memcpy(tri_idxs, &mesh->vt(0), mesh->n_half_edges * sizeof(index_t)); + memcpy(tri_idxs, &mesh->halfedge(0), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); @@ -242,7 +242,7 @@ index_t embree::add_pointcloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - pxyzr[i] = glm::vec4(glm_vec3(mesh->gt(i)), pc_radius); + pxyzr[i] = glm::vec4(glm_vec3(mesh->point(i)), pc_radius); normal[i] = mesh->normal(i); } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 2a1415ba..6d6f948b 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -62,7 +62,7 @@ void che_viewer::update() for(index_t v = 0; v < mesh->n_vertices; ++v) { - const vertex & p = mesh->gt(v); + const vertex & p = mesh->point(v); pmin.x = min(pmin.x, p.x); pmin.y = min(pmin.y, p.y); @@ -99,7 +99,7 @@ void che_viewer::update_vbo_geometry() // 0 VERTEX glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->gt(0), GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->point(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_REAL, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -108,7 +108,7 @@ void che_viewer::update_vbo_geometry() if(!mesh->is_pointcloud()) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->vt(0), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->halfedge(0), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -237,7 +237,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) selected_xyz.reserve(selected.size()); for(const index_t & v: selected) - selected_xyz.push_back(mesh->gt(v)); + selected_xyz.push_back(mesh->point(v)); } if(selected_xyz.size()) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a59fa7e4..0fcce87a 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -732,7 +732,7 @@ bool viewer::m_select_border_vertices(viewer * view) che_viewer & mesh = view->active_mesh(); for(const index_t & b: mesh->bounds()) for_boundary(he, mesh, b) - mesh.selected.push_back(mesh->vt(he)); + mesh.selected.push_back(mesh->halfedge(he)); return false; } @@ -892,7 +892,7 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) scene_lights.clear(); for(const index_t & v: mesh.selected) - scene_lights.push_back(mesh->gt(v)); + scene_lights.push_back(mesh->point(v)); if(!scene_lights.size()) scene_lights = {cam_light}; From 4a7dcb02e2eddd8be091363ec14c0c5d91f02d86 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Jun 2022 16:35:58 +0200 Subject: [PATCH 0667/1018] mesh che: adding halfedge star iterator per vertex using for(auto & he: mesh->star(v)) --- include/gproshan/mesh/che.h | 48 +++++++++++++++++-- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 4 +- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 +- src/gproshan/mdict/patch.cpp | 4 +- src/gproshan/mesh/che.cpp | 35 ++++++-------- src/gproshan/mesh/simplification.cpp | 2 +- 10 files changed, 70 insertions(+), 33 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 84808972..1a95533f 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -7,8 +7,6 @@ #include #include -#define for_star(he, mesh, v) \ - for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->twin_he(prev(he))) != stop ? he : NIL) #define for_boundary(he, mesh, v) \ for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->halfedge(next(he)))) != stop ? he : NIL) @@ -26,6 +24,50 @@ index_t prev(const index_t & he); class che { + public: + class star_he + { + public: + class star_he_it + { + const che * mesh; + index_t he; + const index_t & he_end; + + public: + star_he_it(const che * p_mesh, const index_t & p_he, const index_t & p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} + star_he_it & operator ++ () + { + he = mesh->OT[prev(he)]; + he = he != he_end ? he : NIL; + return *this; + } + bool operator != (const star_he_it & it) + { + return he != it.he; + } + const index_t & operator * () + { + return he; + } + }; + + private: + const che * mesh; + const index_t & v; + + public: + star_he(const che * p_mesh, const index_t & p_v): mesh(p_mesh), v(p_v) {} + star_he_it begin() + { + return {mesh, mesh->EVT[v], mesh->EVT[v]}; + } + star_he_it end() + { + return {nullptr, NIL, NIL}; + } + }; + public: enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types struct rgb_t @@ -118,7 +160,7 @@ class che const index_t & evt(const index_t & v) const; // topology methods - std::vector star(const index_t & v) const; + che::star_he star(const index_t & v) const; std::vector link(const index_t & v) const; void edge_collapse(const std::vector & sort_edges); void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t & k = NIL); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index e6155ae9..ab3af963 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -217,7 +217,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { g = 0; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { a = mesh->vertex_he(next(he)) - mesh->point(v); b = mesh->vertex_he(prev(he)) - mesh->point(v); diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index 1f2b9b28..c0e33373 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -54,7 +54,7 @@ void key_components::compute_kcs(che * mesh, const std::vector & kps) radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) - for_star(he, mesh, fm(i)) + for(const index_t & he: mesh->star(fm(i))) join(fm(i), mesh->halfedge(next(he))); for(index_t i = 0; i < n_vertices; ++i) diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index ef8a133b..a12f6a09 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -167,7 +167,7 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co if(color[v] == RED) { dv = dist[v]; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { dp = update_step(mesh, dist, he); if(dp < dv) diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 972a198f..2c10350e 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -81,7 +81,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c pdist[!d][v] = pdist[d][v]; real_t p; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { p = update_step(mesh, pdist[d], he); if(p < pdist[!d][v]) @@ -158,7 +158,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c pdist[!d][v] = pdist[d][v]; real_t p; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { p = update_step(mesh, pdist[d], he); if(p < pdist[!d][v]) diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index d53eff9e..aa45288d 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -92,7 +92,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) real_t & sum = div(v); sum = 0; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { const vertex & nhe = mesh->normal_he(he); const vertex & vhe = mesh->vertex_he(prev(he)) - mesh->vertex_he(next(he)); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 30205448..1ee6020a 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -160,7 +160,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->is_vertex_bound(v) ? 1 : 0; - for_star(he, mesh, v) ++dv; + for(const index_t & he: mesh->star(v)) ++dv; ++deg[dv]; } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index b4e96c90..e5261365 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -90,7 +90,7 @@ bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle bool added = false; vertex pav, pbv, va, vb,vv; - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { a = mesh->halfedge(next(he)); //index of the next vertex index_t b = mesh->halfedge(prev(he)); @@ -407,7 +407,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co // forstar to find closest trinagle a_mat abc(3,3); - for_star(he, mesh, min_v) + for(const index_t & he: mesh->star(min_v)) { //discard triangles outside the patch vpatches_t & ma = vpatches[mesh->halfedge(next(he))]; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index aa9da19c..2cae5339 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -162,7 +162,7 @@ vertex che::gradient(const index_t & v, const real_t * f) vertex g; real_t area, area_star = 0; - for_star(he, this, v) + for(const index_t & he: star(v)) { area = area_trig(trig(he)); area_star += area; @@ -383,7 +383,7 @@ void che::update_normals() vertex & n = VN[v]; n = 0; - for_star(he, this, v) + for(const index_t & he: star(v)) n += area_trig(trig(he)) * normal_he(he); n /= *n; @@ -455,7 +455,7 @@ void che::remove_vertices(const vector & vertices) gproshan_debug(removing vertex); for(index_t v: vertices) { - for_star(he, this, v) + for(const index_t & he: star(v)) { VT[he] = NIL; VT[prev(he)] = NIL; @@ -492,7 +492,7 @@ void che::remove_vertices(const vector & vertices) { if(v < removed[r]) { - for_star(he, this, v) + for(const index_t & he: star(v)) if(VT[he] != NIL) VT[he] = v - d; } else if(v == removed[r]) @@ -551,7 +551,7 @@ void che::remove_non_manifold_vertices() { if(v < removed[r]) { - for_star(he, this, v) + for(const index_t & he: star(v)) if(VT[he] != NIL) VT[he] = v - d; } else if(v == removed[r]) @@ -590,10 +590,11 @@ void che::set_head_vertices(index_t * head, const size_t & n) swap(GT[v], GT[i]); - for_star(he, this, v) + for(const index_t & he: star(v)) + VT[he] = i; + + for(const index_t & he: star(i)) VT[he] = i; - for_star(he, this, i) - VT[he] = v; swap(EVT[v], EVT[i]); } @@ -665,15 +666,9 @@ const index_t & che::evt(const index_t & v) const // topology methods -vector che::star(const index_t & v) const +che::star_he che::star(const index_t & v) const { - assert(v >= n_vertices); - - vector vstar; - for_star(he, this, v) - vstar.push_back(he); - - return vstar; + return {this, v}; } vector che::link(const index_t & v) const @@ -685,7 +680,7 @@ vector che::link(const index_t & v) const if(is_vertex_bound(v)) vlink.push_back(VT[next(EVT[v])]); - for_star(he, this, v) + for(const index_t & he: star(v)) vlink.push_back(VT[prev(he)]); return vlink; @@ -834,7 +829,7 @@ size_t che::max_degree() const for(index_t v = 0; v < n_vertices; ++v) { d = 0; - for_star(he, this, v) ++d; + for(const index_t & he: star(v)) ++d; d += is_vertex_bound(v); md = max(md, d); } @@ -981,7 +976,7 @@ real_t che::area_trig(const index_t & t) const real_t che::area_vertex(const index_t & v) const { real_t area_star = 0; - for_star(he, this, v) + for(const index_t & he: star(v)) area_star += area_trig(trig(he)); return area_star / 3; @@ -993,7 +988,7 @@ real_t che::mean_curvature(const index_t & v) const real_t h = 0; real_t a = 0; - for_star(he, this, v) + for(const index_t & he: star(v)) { a += area_trig(trig(he)); h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 0e45d433..247cc445 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -38,7 +38,7 @@ void simplification::compute_quadrics() Q[v].zeros(); a_vec p(4); - for_star(he, mesh, v) + for(const index_t & he: mesh->star(v)) { n = mesh->normal_he(he); p(0) = n.x; From 8dd69e4c4d80617893154118118886127cc585cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Jun 2022 17:16:22 +0200 Subject: [PATCH 0668/1018] mesh che: refactoring mesh boundary methods --- include/gproshan/mesh/che.h | 5 ----- src/gproshan/mdict/patch.cpp | 2 +- src/gproshan/mesh/che.cpp | 23 ++++++++++++----------- src/gproshan/mesh/che_fill_hole.cpp | 4 ++-- src/gproshan/viewer/viewer.cpp | 4 ++-- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 1a95533f..9837df2b 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -8,10 +8,6 @@ #include -#define for_boundary(he, mesh, v) \ - for(index_t stop = mesh->evt(v), he = mesh->evt(v); he != NIL; he = (he = mesh->evt(mesh->halfedge(next(he)))) != stop ? he : NIL) - - // geometry processing and shape analysis framework namespace gproshan { @@ -115,7 +111,6 @@ class che virtual ~che(); // vertex access geometry methods to xyz point values, normals, and gradient - const vertex & gt_vt_next_evt(const index_t & v) const; const vertex & point(const index_t & v) const; vertex & point(const index_t & v); const vertex & normal(const index_t & v) const; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index e5261365..e102bac9 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -631,7 +631,7 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) vertex nz = mesh->normal(v); - vertex nx = mesh->gt_vt_next_evt(v); + vertex nx = mesh->vertex_he(next(mesh->evt(v))); // GT[VT[next(EVT[v]]] vertex c = mesh->point(v); vertex ny; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 2cae5339..fd756ac2 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -85,12 +85,6 @@ che::~che() // vertex access geometry methods to xyz point values, normals, and gradient -const vertex & che::gt_vt_next_evt(const index_t & v) const -{ - assert(v < n_vertices); - return GT[VT[next(EVT[v])]]; -} - const vertex & che::point(const index_t & v) const { assert(v < n_vertices); @@ -673,7 +667,7 @@ che::star_he che::star(const index_t & v) const vector che::link(const index_t & v) const { - assert(v >= n_vertices); + assert(v < n_vertices); vector vlink; @@ -753,8 +747,8 @@ vector che::bounds() const { vbounds.push_back(v); - for_boundary(he, this, v) - is_bound[VT[he]] = true; + for(const index_t & b: boundary(v)) + is_bound[b] = true; } delete [] is_bound; @@ -767,20 +761,27 @@ vector che::boundary(const index_t & v) const { vector vbound; - for_boundary(he, this, v) + index_t he_end = EVT[v]; + index_t he = he_end; + do + { vbound.push_back(VT[he]); + he = EVT[VT[next(he)]]; + } + while(he != NIL && he != he_end); return vbound; } bool che::is_vertex_bound(const index_t & v) const { - assert(v < n_vertices && EVT[v] < n_half_edges); + assert(v < n_vertices); return EVT[v] != NIL && OT[EVT[v]] == NIL; } bool che::is_edge_bound(const index_t & e) const { + assert(e < n_edges); return OT[ET[e]] == NIL; } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 87c86934..b82d1374 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -28,7 +28,7 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, { vector vertices; vertex normal, normal_v, edge_v, v; - a_mat E(3,3); + a_mat E(3, 3); a_vec ve(3); vertices.reserve(border_vertices.size()); @@ -37,7 +37,7 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, { v = mesh->point(b); normal_v = mesh->normal(b); - edge_v = mesh->gt_vt_next_evt(b) - v; + edge_v = mesh->vertex_he(next(mesh->evt(b))) - v; edge_v -= (normal_v, edge_v) * normal_v; E(0, 2) = normal_v.x; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 0fcce87a..32cc2d5c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -731,8 +731,8 @@ bool viewer::m_select_border_vertices(viewer * view) { che_viewer & mesh = view->active_mesh(); for(const index_t & b: mesh->bounds()) - for_boundary(he, mesh, b) - mesh.selected.push_back(mesh->halfedge(he)); + for(const index_t & v: mesh->boundary(b)) + mesh.selected.push_back(v); return false; } From 7da72510e6a836be7b7359dd40a2eb2a87a2afdf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 8 Jun 2022 01:42:30 +0200 Subject: [PATCH 0669/1018] geometry: adding vec template class --- include/gproshan/geometry/vec.h | 199 +++++++++++++++++++++++++++ src/gproshan/app_viewer.cpp | 24 ++++ src/gproshan/laplacian/laplacian.cpp | 2 +- 3 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 include/gproshan/geometry/vec.h diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h new file mode 100644 index 00000000..d997d5ce --- /dev/null +++ b/include/gproshan/geometry/vec.h @@ -0,0 +1,199 @@ +#ifndef VEC_H +#define VEC_H + +#include + +#include +#include +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +/*! + The vec class represents a generic N dimensional vector and implements the vector operations. +*/ +template +class vec +{ + protected: + T values[N] = {}; + + public: + vec() = default; + + vec(const std::initializer_list & list) + { + memcpy(values, list.begin(), sizeof(values)); + } + + virtual ~vec() = default; + + T & operator [] (const index_t & i) + { + assert(i < N); + return values[i]; + } + + const T & operator [] (const index_t & i) const + { + assert(i < N); + return values[i]; + } + + ///< norm or length + T operator * () const + { + T res = 0; + for(const T & v: values) + res += v * v; + return std::sqrt(res); + } + + ///< dot product + T operator , (const vec & v) const + { + T res = 0; + for(index_t i = 0; i < N; ++i) + res += values[i] * v[i]; + return res; + } + + ///< scalar product + vec operator * (const T & a) const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] * a; + return res; + } + + ///< scalar division + vec operator / (const T & a) const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] / a; + return res; + } + + ///< sum of vectors + vec operator + (const vec & v) const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] + v[i]; + return res; + } + + ///< difference of vectors + vec operator - (const vec & v) const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] - v[i]; + return res; + } + + ///< negative of vector + vec operator - () const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = - values[i]; + return res; + } + + ///< scalar product self assign + const vec & operator *= (const T & a) const + { + for(T & v: values) + v *= a; + return *this; + } + + ///< scalar division self assign + const vec & operator /= (const T & a) const + { + for(T & v: values) + v /= a; + return *this; + } + + ///< sum of vectors self assign + const vec & operator += (const vec & v) const + { + for(index_t i = 0; i < N; ++i) + values[i] += v[i]; + return *this; + } + + ///< difference of vectors self assign + const vec & operator -= (const vec & v) const + { + for(index_t i = 0; i < N; ++i) + values[i] -= v[i]; + return *this; + } + + /* + + bool operator < (const vec & v) const; + bool operator == (const vec & v) const; + + bool is_zero();*/ +}; + +///< scalar product +template +vec operator * (const double & a, const vec & v) +{ + return v * a; +} + +///< dot product +template +T dot(const vec & u, const vec & v) +{ + return (u, v); +} + +///< norm or length +template +T norm(const vec & v) +{ + return *v; +} + +///< normalize vector: divide by its norm +template +vec normalize(const vec & v) +{ + return v / *v; +} + +///< std ostream +template +std::ostream & operator << (std::ostream & os, const vec & v) +{ + for(index_t i = 0; i < N - 1; ++i) + os << v[i] << " "; + return os << v[N - 1]; +} + +///< std istream +template +std::istream & operator >> (std::istream & is, vec & v) +{ + for(index_t i = 0; i < N; ++i) + is >> v[i]; + return is; +} + + +} // namespace gproshan + +#endif // VEC_H + diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index ab3af963..5bf529bb 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -1,5 +1,7 @@ #include +#include + #include #include @@ -37,6 +39,28 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { + vec va{1, 2, 3}; + vec vb = {1, 2, 3}; + vec vc; + vc = {1, 2, 3}; + vec vs = va + vb + vc; + + gproshan_log_var((va, vb)); + gproshan_log_var(dot(va, vb)); + gproshan_log_var(*vb); + gproshan_log_var(vc * 2 / 4); + gproshan_log_var(2 * vc / 4); + gproshan_log_var(vs); + + vec dd{1, 1, 1}; + gproshan_log_var(vs - dd); + gproshan_log_var(- vs - dd); + gproshan_log_var(sizeof(vec)); + gproshan_log_var(sizeof(vec)); + gproshan_log_var(sizeof(vec)); + + return 0; + if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp index 6cf23362..e3fe4103 100644 --- a/src/gproshan/laplacian/laplacian.cpp +++ b/src/gproshan/laplacian/laplacian.cpp @@ -59,7 +59,7 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) { - if(!eigs_sym(eigval, eigvec, A * L * A, k, "sm")) + if(!eigs_sym(eigval, eigvec, L, k, "sm")) return 0; eigval.save(feigval); From bedf84b18c5ae8585cb0c6a54c064fb3b32f9343 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 8 Jun 2022 14:27:08 +0200 Subject: [PATCH 0670/1018] vertex: vertex is vec, refactoring code --- apps/test_scanner.cpp | 4 +- include/gproshan/geometry/vec.h | 95 +++++++++++++--- include/gproshan/mesh/vertex.h | 47 +------- include/gproshan/raytracing/rt_embree.h | 4 +- include/gproshan/viewer/camera.h | 6 +- src/gproshan/app_viewer.cpp | 19 ++-- src/gproshan/geodesics/dijkstra.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 12 +- src/gproshan/mdict/msparse_coding.cpp | 4 +- src/gproshan/mdict/patch.cpp | 16 +-- src/gproshan/mesh/che.cpp | 25 ++-- src/gproshan/mesh/che_fill_hole.cpp | 12 +- src/gproshan/mesh/quaternion.cpp | 2 +- src/gproshan/mesh/vertex.cpp | 138 ----------------------- src/gproshan/raytracing/rt_embree.cpp | 5 +- src/gproshan/viewer/che_viewer.cpp | 4 +- src/gproshan/viewer/viewer.cpp | 2 +- 17 files changed, 141 insertions(+), 256 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index 78235fc4..6eb887cb 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -24,8 +24,8 @@ glm::mat4 normalize_coordinates(gproshan::che_ply * mesh) { glm::mat4 model_mat = glm::mat4(1); - gproshan::vertex pmin(INFINITY, INFINITY, INFINITY); - gproshan::vertex pmax(0, 0, 0); + gproshan::vertex pmin = INFINITY; + gproshan::vertex pmax = 0; for(gproshan::index_t v = 0; v < mesh->n_vertices; ++v) { diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index d997d5ce..f6f446c8 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -18,8 +19,17 @@ namespace gproshan { template class vec { - protected: - T values[N] = {}; + public: + union + { + T values[N] = {}; + struct + { + T x; + T y; + T z; + }; + }; public: vec() = default; @@ -29,7 +39,18 @@ class vec memcpy(values, list.begin(), sizeof(values)); } - virtual ~vec() = default; + vec(const T & val) + { + for(T & v: values) + v = val; + } + + const vec & operator = (const T & val) + { + for(T & v: values) + v = val; + return *this; + } T & operator [] (const index_t & i) { @@ -43,8 +64,8 @@ class vec return values[i]; } - ///< norm or length - T operator * () const + ///< norm + T norm() const { T res = 0; for(const T & v: values) @@ -52,6 +73,12 @@ class vec return std::sqrt(res); } + ///< length + T length() const + { + return norm(); + } + ///< dot product T operator , (const vec & v) const { @@ -107,7 +134,7 @@ class vec } ///< scalar product self assign - const vec & operator *= (const T & a) const + const vec & operator *= (const T & a) { for(T & v: values) v *= a; @@ -115,7 +142,7 @@ class vec } ///< scalar division self assign - const vec & operator /= (const T & a) const + const vec & operator /= (const T & a) { for(T & v: values) v /= a; @@ -123,7 +150,7 @@ class vec } ///< sum of vectors self assign - const vec & operator += (const vec & v) const + const vec & operator += (const vec & v) { for(index_t i = 0; i < N; ++i) values[i] += v[i]; @@ -131,21 +158,36 @@ class vec } ///< difference of vectors self assign - const vec & operator -= (const vec & v) const + const vec & operator -= (const vec & v) { for(index_t i = 0; i < N; ++i) values[i] -= v[i]; return *this; } - /* + ///< comparison less than + bool operator < (const vec & v) const + { + if(x != v.x) return x < v.x; + if(y != v.y) return y < v.y; + return z < v.z; + } - bool operator < (const vec & v) const; - bool operator == (const vec & v) const; + ///< comparison equal than + bool operator == (const vec & v) const + { + return x == v.x && y == v.y && z == v.z; + } + + bool is_zero() + { + double eps = std::numeric_limits::epsilon(); - bool is_zero();*/ + return abs(double(x)) < eps && abs(double(y)) < eps && abs(double(z)) < eps; + } }; + ///< scalar product template vec operator * (const double & a, const vec & v) @@ -153,6 +195,20 @@ vec operator * (const double & a, const vec & v) return v * a; } +///< cross product +template +vec operator * (const vec & u, const vec & v) +{ + return {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; +} + +///< cross product +template +vec cross(const vec & u, const vec & v) +{ + return u * v; +} + ///< dot product template T dot(const vec & u, const vec & v) @@ -160,18 +216,25 @@ T dot(const vec & u, const vec & v) return (u, v); } -///< norm or length +///< norm template T norm(const vec & v) { - return *v; + return v.norm(); +} + +///< length +template +T length(const vec & v) +{ + return v.length(); } ///< normalize vector: divide by its norm template vec normalize(const vec & v) { - return v / *v; + return v / norm(v); } ///< std ostream diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h index b061de17..baab0109 100644 --- a/include/gproshan/mesh/vertex.h +++ b/include/gproshan/mesh/vertex.h @@ -2,6 +2,7 @@ #define VERTEX_H #include +#include #include @@ -16,51 +17,7 @@ namespace gproshan { /*! The vertex class represents a 3D point and implements 3D vector operations. */ -class vertex -{ - public: - real_t x; - real_t y; - real_t z; - - public: - vertex(const real_t & x_ = 0, const real_t & y_ = 0, const real_t & z_ = 0); - ~vertex() = default; - - real_t & operator [] (const index_t & i); - const real_t & operator [] (const index_t & i) const; - - vertex unit() const; - real_t operator * () const; // norm - real_t operator , (const vertex & v) const; // dot product - - vertex operator * (const vertex & v) const; // cross product - vertex operator * (const real_t & v) const; // scalar product - vertex operator / (const real_t & v) const; // scalar division - vertex operator + (const vertex & v) const; - vertex operator - (const vertex & v) const; - vertex operator - () const; - - void operator *= (const real_t & v); // scalar produc - void operator /= (const real_t & v); - void operator += (const vertex & v); - void operator -= (const vertex & v); - - bool operator < (const vertex & v) const; - bool operator == (const vertex & v) const; - - bool is_zero(); -}; - -vertex operator * (const real_t & a, const vertex & v); - -vertex cross(const vertex & u, const vertex & v); -real_t dot(const vertex & u, const vertex & v); -real_t norm(const vertex & v); -vertex normalize(const vertex & v); - -std::ostream & operator << (std::ostream & os, const vertex & v); -std::istream & operator >> (std::istream & is, vertex & v); +using vertex = vec; } // namespace gproshan diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 196b46c2..8986b8a3 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -18,8 +18,8 @@ class embree : public raytracing protected: struct ray_hit : public RTCRayHit { - ray_hit(const vertex & p_org = vertex(0.0f), - const vertex & v_dir = vertex(0.0f), + ray_hit(const vertex & p_org = {0, 0, 0}, + const vertex & v_dir = {0, 0, 0}, float near = 1e-5f, float far = FLT_MAX); diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index b04c2699..44f5c381 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -20,9 +20,9 @@ class camera public: quaternion eye; - quaternion pos = vertex(0, 0, -2.5); - quaternion front = vertex(0, 0, 1); - quaternion up = vertex(0, 1, 0); + quaternion pos = vertex{0, 0, -2.5}; + quaternion front = vertex{0, 0, 1}; + quaternion up = vertex{0, 1, 0}; public: glm::mat4 look_at(const quaternion & r); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 5bf529bb..a0a3cec7 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,15 +39,21 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { - vec va{1, 2, 3}; - vec vb = {1, 2, 3}; + vertex n = {}; + gproshan_log_var(n); + + vec va = {1, 2, 3}; + vec vb = {1, 2}; vec vc; vc = {1, 2, 3}; vec vs = va + vb + vc; - + vertex a = {1, 0, 0}; + vertex b = {0, 1, 0}; + gproshan_log_var(norm(a * b) / 2); gproshan_log_var((va, vb)); gproshan_log_var(dot(va, vb)); - gproshan_log_var(*vb); + gproshan_log_var(norm(vb)); + gproshan_log_var(length(vb)); gproshan_log_var(vc * 2 / 4); gproshan_log_var(2 * vc / 4); gproshan_log_var(vs); @@ -58,8 +64,7 @@ int app_viewer::main(int nargs, const char ** args) gproshan_log_var(sizeof(vec)); gproshan_log_var(sizeof(vec)); gproshan_log_var(sizeof(vec)); - - return 0; + gproshan_log_var(sizeof(vertex)); if(nargs < 2) { @@ -245,7 +250,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) { a = mesh->vertex_he(next(he)) - mesh->point(v); b = mesh->vertex_he(prev(he)) - mesh->point(v); - g += acos((a,b) / (*a * *b)); + g += acos((a,b) / (norm(a) * norm(b))); } //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); gv(v) = mesh->mean_curvature(v); diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index f4d1b5d7..1ebe0aca 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -75,7 +75,7 @@ void dijkstra::run(che * mesh) { if(visited[nv]) { - w = weights[nv] + *(mesh->point(nv) - mesh->point(v)); + w = weights[nv] + norm(mesh->point(nv) - mesh->point(v)); if(w < weights[v]) weights[v] = w; diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 2c10350e..10f9e38b 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -238,9 +238,11 @@ real_t update_step(che * mesh, const real_t * dist, const index_t & he) tp[0] = t[0] - p; tp[1] = t[1] - p; - vertex n(tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); + vertex n = { + tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), + tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), + tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) + }; real_t cond[2]; cond[0] = (X[0] , n); @@ -253,8 +255,8 @@ real_t update_step(che * mesh, const real_t * dist, const index_t & he) if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { real_t dp[2]; - dp[0] = dist[x[0]] + *X[0]; - dp[1] = dist[x[1]] + *X[1]; + dp[0] = dist[x[0]] + norm(X[0]); + dp[1] = dist[x[1]] + norm(X[1]); p = dp[dp[1] < dp[0]]; } diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 0997c094..b0609d9d 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -251,7 +251,7 @@ void msparse_coding::load_sampling() const vertex & va = mesh->point(vsf); const vertex & vb = mesh->point(v); - invalid_seed[v] = *(va - vb) < 0.8 * euc_radio; + invalid_seed[v] = norm(va - vb) < 0.8 * euc_radio; } } } @@ -959,7 +959,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) #pragma omp parallel for reduction(+: error) reduction(max: max_error) for(index_t v = 0; v < mesh->n_vertices; ++v) { - dist[v] = *(new_vertices[v] - mesh->point(v)); + dist[v] = length(new_vertices[v] - mesh->point(v)); error += dist[v]; max_error = max(max_error, dist[v]); } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index e102bac9..1f620a84 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -119,7 +119,7 @@ bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle // compute projected area pav = va - vv + ( (n,vv) - (n,va) ) * n; pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; - proj_area_face = *(pav * pbv) / 2; + proj_area_face = norm(pav * pbv) / 2; min_he = mesh->normal_he(he); added = true; @@ -214,9 +214,9 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind p = p - c ; p = p - ((p,n)*n); - if(*p > radio) + if(norm(p) > radio) { - radio = *p; + radio = norm(p); } } } @@ -268,7 +268,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, if(add_vertex_by_faces(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { - euc_radio = max(euc_radio, *(mesh->point(u) - c)); + euc_radio = max(euc_radio, norm(mesh->point(u) - c)); return true; } @@ -297,7 +297,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, p = p - c ; p = p - ((p, n) * n); - radio = max(radio, *p); + radio = max(radio, norm(p)); } geo_radio = geo[vertices.back()]; @@ -639,9 +639,9 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) nx = nx - ((nx,nz)*nz); ny = (nz * nx); - nx.unit(); - ny.unit(); - nz.unit(); + nx = normalize(nx); + ny = normalize(ny); + nz = normalize(nz); T.set_size(3, 3); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index fd756ac2..e39b60bf 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -112,7 +112,7 @@ vertex & che::normal(const index_t & v) vertex che::shading_normal(const index_t & f, const float & u, const float & v) const { const index_t & he = f * che::mtrig; - return u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]; + return normalize(u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]); } vertex che::normal_trig(const index_t & f) const @@ -141,14 +141,11 @@ vertex che::gradient_he(const index_t & he, const real_t * f) const vertex n = normal_he(he); - real_t A2 = area_trig(trig(he)) * 2; - vertex pij = n * (xj - xi); vertex pjk = n * (xk - xj); vertex pki = n * (xi - xk); - vertex g = (f[i] * pjk + f[j] * pki + f[k] * pij) / A2; - return g / *g; + return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } vertex che::gradient(const index_t & v, const real_t * f) @@ -233,7 +230,7 @@ void che::normalize_sphere(const real_t & r) for(index_t v = 0; v < n_vertices; ++v) { GT[v] -= center; - max_norm = std::max(max_norm, *GT[v]); + max_norm = std::max(max_norm, norm(GT[v])); } #pragma omp parallel for @@ -380,7 +377,7 @@ void che::update_normals() for(const index_t & he: star(v)) n += area_trig(trig(he)) * normal_he(he); - n /= *n; + n /= norm(n); } } @@ -855,7 +852,7 @@ real_t che::mean_edge() const #pragma omp parallel for reduction(+: m) for(index_t e = 0; e < n_edges; ++e) - m += *(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); + m += norm(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); return m / n_edges; } @@ -947,7 +944,7 @@ real_t che::cotan(const index_t & he) const vertex a = GT[VT[he]] - GT[VT[prev(he)]]; vertex b = GT[VT[next(he)]] - GT[VT[prev(he)]]; - return (a, b) / *(a * b); + return (a, b) / norm(a * b); } // https://www.mathworks.com/help/pde/ug/pdetriq.html @@ -958,9 +955,9 @@ real_t che::pdetriq(const index_t & t) const { index_t he = t * che::mtrig; real_t h[3] = { - *(GT[VT[next(he)]] - GT[VT[he]]), - *(GT[VT[prev(he)]] - GT[VT[next(he)]]), - *(GT[VT[he]] - GT[VT[prev(he)]]) + norm(GT[VT[next(he)]] - GT[VT[he]]), + norm(GT[VT[prev(he)]] - GT[VT[next(he)]]), + norm(GT[VT[he]] - GT[VT[prev(he)]]) }; return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } @@ -971,7 +968,7 @@ real_t che::area_trig(const index_t & t) const vertex a = GT[VT[next(he)]] - GT[VT[he]]; vertex b = GT[VT[prev(he)]] - GT[VT[he]]; - return *(a * b) / 2; + return norm(a * b) / 2; } real_t che::area_vertex(const index_t & v) const @@ -992,7 +989,7 @@ real_t che::mean_curvature(const index_t & v) const for(const index_t & he: star(v)) { a += area_trig(trig(he)); - h += *(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); + h += norm(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); } return 0.75 * h / a; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index b82d1374..5ffebcc2 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -78,7 +78,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const auto gen_vertices = [&mean_edge](vector & merge_vertices, vector & vertices, const vertex & va, const vertex & vb, const index_t & delta_v = 0) { - real_t L = *(va - vb); + real_t L = length(va - vb); size_t N = L / mean_edge; L = N; @@ -352,11 +352,11 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, tmp_vertices[v](1) = vertices[v].y; tmp_vertices[v](2) = vertices[v].z; - if(v) init_perimeter += *(vertices[v] - vertices[v - 1]); + if(v) init_perimeter += norm(vertices[v] - vertices[v - 1]); } - init_perimeter += *(vertices.back() - vertices.front()); + init_perimeter += norm(vertices.back() - vertices.front()); perimeter = init_perimeter; // length = perimeter / vertices.size(); @@ -547,7 +547,7 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, vertices.clear(); for(a_vec r: tmp_vertices) - vertices.push_back(vertex(r[0], r[1], r[2])); + vertices.push_back({r[0], r[1], r[2]}); for(index_t v = 0; false && v < tmp_vertices.size(); ++v) a_vec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); @@ -580,7 +580,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c if(v) init_perimeter += norm(V.col(v) - V.col(v-1)); } - init_perimeter += *(vertices.back() - vertices.front()); + init_perimeter += norm(vertices.back() - vertices.front()); perimeter = init_perimeter; //debug(perimeter) @@ -813,7 +813,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c for(a_vec r: tmp_vertices) { r = E * r + avg; - vertices.push_back(vertex(r[0], r[1], r[2])); + vertices.push_back({r[0], r[1], r[2]}); } return faces.size() ? new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3) : nullptr; diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 633280eb..86e86fb3 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -10,7 +10,7 @@ using namespace std; namespace gproshan { -quaternion::quaternion(real_t s_, real_t vi, real_t vj, real_t vk): s(s_), v(vi, vj, vk) {} +quaternion::quaternion(real_t s_, real_t vi, real_t vj, real_t vk): s(s_), v{vi, vj, vk} {} quaternion::quaternion(real_t s_, const vertex & v_): s(s_), v(v_) {} diff --git a/src/gproshan/mesh/vertex.cpp b/src/gproshan/mesh/vertex.cpp index 6e07fc90..ce7149f4 100644 --- a/src/gproshan/mesh/vertex.cpp +++ b/src/gproshan/mesh/vertex.cpp @@ -9,143 +9,5 @@ using namespace std; namespace gproshan { -vertex::vertex(const real_t & x_, const real_t & y_, const real_t & z_): x(x_), y(y_), z(z_) {} - -real_t & vertex::operator [] (const index_t & i) -{ - return (&x)[i]; -} - -const real_t & vertex::operator [] (const index_t & i) const -{ - return (&x)[i]; -} - -vertex vertex::unit() const -{ - return *this / **this; -} - -real_t vertex::operator * () const -{ - return sqrt(x * x + y * y + z * z); -} - -real_t vertex::operator , (const vertex & v) const -{ - return x * v.x + y * v.y + z * v.z; -} - -vertex vertex::operator * (const vertex & v) const -{ - return {y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x}; -} - -vertex vertex::operator * (const real_t & a) const -{ - return a * (*this); -} - -vertex vertex::operator / (const real_t & a) const -{ - return (1. / a) * (*this); -} - -vertex vertex::operator + (const vertex & v) const -{ - return {x + v.x, y + v.y, z + v.z}; -} - -vertex vertex::operator - (const vertex & v) const -{ - return {x - v.x, y - v.y, z - v.z}; -} - -vertex vertex::operator - () const -{ - return {-x, -y, -z}; -} - -void vertex::operator *= (const real_t & a) -{ - x *= a; - y *= a; - z *= a; -} - -void vertex::operator /= (const real_t & a) -{ - (*this) *= (1. / a); -} - -void vertex::operator += (const vertex & v) -{ - x += v.x; - y += v.y; - z += v.z; -} - -void vertex::operator -= (const vertex & v) -{ - x -= v.x; - y -= v.y; - z -= v.z; -} - -bool vertex::operator < (const vertex & v) const -{ - if(x != v.x) return x < v.x; - if(y != v.y) return y < v.y; - return z < v.z; -} - -bool vertex::operator == (const vertex & v) const -{ - return x == v.x && y == v.y && z == v.z; -} - -bool vertex::is_zero() -{ - real_t eps = std::numeric_limits::epsilon(); - - return abs(x) < eps && abs(y) < eps && abs(z) < eps; -} - -vertex operator * (const real_t & a, const vertex & v) -{ - return {a * v.x, a * v.y, a * v.z}; -} - -vertex cross(const vertex & u, const vertex & v) -{ - return u * v; -} - -real_t dot(const vertex & u, const vertex & v) -{ - return (u, v); -} - -real_t norm(const vertex & v) -{ - return *v; -} - -vertex normalize(const vertex & v) -{ - return v / *v; -} - -ostream & operator << (ostream & os, const vertex & v) -{ - return os << v.x << " " << v.y << " " << v.z; -} - -istream & operator >> (istream & is, vertex & v) -{ - return is >> v.x >> v.y >> v.z; -} - - } // namespace gproshan diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index a7c58ef4..c73dbff1 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -55,10 +55,9 @@ vertex embree::ray_hit::color(const rt_mesh & mesh) const vertex embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const { if(flat || mesh.pointcloud) - return normalize({hit.Ng_x, hit.Ng_y, hit.Ng_z}); + return normalize(vertex{hit.Ng_x, hit.Ng_y, hit.Ng_z}); - const vertex & n = mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u); - return n / *n; + return mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u); } index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 6d6f948b..c4732fe6 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -57,8 +57,8 @@ void che_viewer::update() model_mat = glm::mat4(1); if(center_mesh) { - vertex pmin(INFINITY, INFINITY, INFINITY); - vertex pmax(0, 0, 0); + vertex pmin = INFINITY; + vertex pmax = 0; for(index_t v = 0; v < mesh->n_vertices; ++v) { diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 32cc2d5c..8dcb327f 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -91,7 +91,7 @@ bool viewer::run() quaternion r = cam.current_rotation(); - cam_light = vertex(-1, 1, -2); + cam_light = vertex{-1, 1, -2}; cam_light = r.conj() * cam_light * r; render_params.proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); From 5750b634656472aa79ac6373a168868df6168387 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 8 Jun 2022 15:32:21 +0200 Subject: [PATCH 0671/1018] template class vec: solving warnings --- include/gproshan/geometry/vec.h | 10 +++++----- src/gproshan/app_viewer.cpp | 27 --------------------------- 2 files changed, 5 insertions(+), 32 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index f6f446c8..a0149957 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -32,14 +32,14 @@ class vec }; public: - vec() = default; - vec(const std::initializer_list & list) { - memcpy(values, list.begin(), sizeof(values)); + int i = -1; + for(const T & v: list) + values[++i] = v; } - vec(const T & val) + vec(const T & val = 0) { for(T & v: values) v = val; @@ -190,7 +190,7 @@ class vec ///< scalar product template -vec operator * (const double & a, const vec & v) +vec operator * (const T & a, const vec & v) { return v * a; } diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a0a3cec7..54ed5835 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,33 +39,6 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { - vertex n = {}; - gproshan_log_var(n); - - vec va = {1, 2, 3}; - vec vb = {1, 2}; - vec vc; - vc = {1, 2, 3}; - vec vs = va + vb + vc; - vertex a = {1, 0, 0}; - vertex b = {0, 1, 0}; - gproshan_log_var(norm(a * b) / 2); - gproshan_log_var((va, vb)); - gproshan_log_var(dot(va, vb)); - gproshan_log_var(norm(vb)); - gproshan_log_var(length(vb)); - gproshan_log_var(vc * 2 / 4); - gproshan_log_var(2 * vc / 4); - gproshan_log_var(vs); - - vec dd{1, 1, 1}; - gproshan_log_var(vs - dd); - gproshan_log_var(- vs - dd); - gproshan_log_var(sizeof(vec)); - gproshan_log_var(sizeof(vec)); - gproshan_log_var(sizeof(vec)); - gproshan_log_var(sizeof(vertex)); - if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); From 6614e1e6ef7bd6b50a716faa8655deb5045d5a44 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Jun 2022 02:54:05 +0200 Subject: [PATCH 0672/1018] mesh: refactoring vertex, adding vec and mat template classes removing glm --- CMakeLists.txt | 1 - apps/test_scanner.cpp | 10 +- gproshanConfig.cmake.in | 1 - include/gproshan/geometry/convex_hull.h | 5 +- include/gproshan/geometry/mat.h | 103 ++++++++++++++++++++ include/gproshan/geometry/vec.h | 25 ++++- include/gproshan/mesh/che.h | 5 +- include/gproshan/mesh/kdtree.h | 5 +- include/gproshan/mesh/quaternion.h | 5 +- include/gproshan/mesh/vertex.h | 26 ----- include/gproshan/raytracing/raytracing.h | 16 ++- include/gproshan/raytracing/render_params.h | 5 +- include/gproshan/raytracing/rt_embree.h | 17 ++-- include/gproshan/raytracing/rt_optix.h | 10 +- include/gproshan/viewer/camera.h | 5 +- include/gproshan/viewer/che_viewer.h | 4 +- include/gproshan/viewer/viewer.h | 2 - src/gproshan/app_viewer.cpp | 9 ++ src/gproshan/mesh/vertex.cpp | 13 --- src/gproshan/raytracing/raytracing.cpp | 24 ++--- src/gproshan/raytracing/rt_embree.cpp | 37 ++++--- src/gproshan/raytracing/rt_optix.cpp | 31 +++--- src/gproshan/viewer/camera.cpp | 4 +- src/gproshan/viewer/che_viewer.cpp | 6 +- src/gproshan/viewer/viewer.cpp | 9 +- 25 files changed, 224 insertions(+), 154 deletions(-) create mode 100644 include/gproshan/geometry/mat.h delete mode 100644 include/gproshan/mesh/vertex.h delete mode 100644 src/gproshan/mesh/vertex.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c2064b91..f09a475f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,6 @@ find_package(OpenGL REQUIRED) find_package(OpenMP REQUIRED) find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) -find_package(glm REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(Eigen3 REQUIRED) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index 6eb887cb..5d2ea998 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -6,8 +6,6 @@ #include #include -#include -#include void translate(glm::mat4 & model_mat, const gproshan::vertex & p) @@ -20,9 +18,9 @@ void scale(glm::mat4 & model_mat, const gproshan::real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -glm::mat4 normalize_coordinates(gproshan::che_ply * mesh) +gproshan::mat4 normalize_coordinates(gproshan::che_ply * mesh) { - glm::mat4 model_mat = glm::mat4(1); + gproshan::mat4 model_mat = gproshan::mat4::identity(); gproshan::vertex pmin = INFINITY; gproshan::vertex pmax = 0; @@ -69,9 +67,9 @@ int main(int argc, char* argv[]) gproshan::rt::raytracing * rt_embree; - glm::mat4 model_mat = normalize_coordinates(mesh_ply); + gproshan::mat4 model_mat = normalize_coordinates(mesh_ply); - rt_embree = new gproshan::rt::embree({mesh_ply},{model_mat}, false, pc_radius); + rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}, false, pc_radius); gproshan::che * ptx_mesh = scanner_ptx(mesh_ply, rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder); diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 461a70df..e4459734 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -13,7 +13,6 @@ find_dependency(OpenMP REQUIRED) find_dependency(OpenGL REQUIRED) find_dependency(GLEW REQUIRED) find_dependency(glfw3 REQUIRED) -find_dependency(glm REQUIRED) find_dependency(X11 REQUIRED) find_dependency(Armadillo REQUIRED) find_dependency(CGAL REQUIRED) diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index bbfcde8f..eb7291e4 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -1,7 +1,7 @@ #ifndef CONVEX_HULL_H #define CONVEX_HULL_H -#include +#include #include @@ -10,6 +10,9 @@ namespace gproshan { +using vertex = vec3; + + ///< 2D Convex Hull class convex_hull { diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h new file mode 100644 index 00000000..000d0281 --- /dev/null +++ b/include/gproshan/geometry/mat.h @@ -0,0 +1,103 @@ +#ifndef MAT_H +#define MAT_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +template +using row = vec; + +template +class mat +{ + public: + row rows[N]; + + public: + mat(const std::initializer_list > & list = {}) + { + int i = -1; + for(const row & r: list) + rows[++i] = r; + } + + row & operator [] (const index_t & i) + { + assert(i < N); + return rows[i]; + } + + const row & operator [] (const index_t & i) const + { + assert(i < N); + return rows[i]; + } + + mat operator * (const mat & b) const + { + mat res; + mat bt = transpose(b); + for(index_t i = 0; i < N; ++i) + for(index_t j = 0; j < N; ++j) + res[i][j] = (rows[i], bt[j]); + return res; + } + + vec operator * (const vec & v) const + { + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = (rows[i], v); + return res; + } + + static mat identity() + { + mat res; + for(index_t i = 0; i < N; ++i) + res[i][i] = 1; + return res; + } + + static mat transpose(const mat & m) + { + mat res; + for(index_t i = 0; i < N; ++i) + for(index_t j = 0; j < N; ++j) + res[i][j] = m[j][i]; + return res; + } +}; + +///< std ostream +template +std::ostream & operator << (std::ostream & os, const mat & m) +{ + for(index_t i = 0; i < N; ++i) + os << m[i] << std::endl; + return os; +} + +///< std istream +template +std::istream & operator >> (std::istream & is, mat & m) +{ + for(index_t i = 0; i < N; ++i) + is >> m[i]; + return is; +} + + +using mat2 = mat; +using mat3 = mat; +using mat4 = mat; + + +} // namespace gproshan + +#endif // MAT_H + diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index a0149957..6b3f0ae1 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -39,17 +39,23 @@ class vec values[++i] = v; } - vec(const T & val = 0) + vec(const vec & v, const T & val = 0) { - for(T & v: values) - v = val; + for(index_t i = 0; i < N - 1; ++i) + values[i] = v[i]; + values[N - 1] = val; + } + + vec(const vec & v) + { + for(index_t i = 0; i < N; ++i) + values[i] = v[i]; } - const vec & operator = (const T & val) + vec(const T & val = 0) { for(T & v: values) v = val; - return *this; } T & operator [] (const index_t & i) @@ -256,6 +262,15 @@ std::istream & operator >> (std::istream & is, vec & v) } +using vec2 = vec; +using vec3 = vec; +using vec4 = vec; + +using uvec2 = vec; +using uvec3 = vec; +using uvec4 = vec; + + } // namespace gproshan #endif // VEC_H diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 9837df2b..334606bb 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -2,7 +2,7 @@ #define CHE_H #include -#include +#include #include #include @@ -18,6 +18,9 @@ index_t next(const index_t & he); index_t prev(const index_t & he); +using vertex = vec3; + + class che { public: diff --git a/include/gproshan/mesh/kdtree.h b/include/gproshan/mesh/kdtree.h index 7432c980..912b1abe 100644 --- a/include/gproshan/mesh/kdtree.h +++ b/include/gproshan/mesh/kdtree.h @@ -1,13 +1,16 @@ #ifndef KDTREE_H #define KDTREE_H -#include +#include // geometry processing and shape analysis framework namespace gproshan { +using vertex = vec3; + + class kdtree { private: diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 0abf0b11..8465d2ac 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -1,7 +1,7 @@ #ifndef QUATERNION_H #define QUATERNION_H -#include +#include #include @@ -10,6 +10,9 @@ namespace gproshan { +using vertex = vec3; + + class quaternion { public: diff --git a/include/gproshan/mesh/vertex.h b/include/gproshan/mesh/vertex.h deleted file mode 100644 index baab0109..00000000 --- a/include/gproshan/mesh/vertex.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef VERTEX_H -#define VERTEX_H - -#include -#include - -#include - - -#define glm_vec3(v) glm::vec3((v)[0], (v)[1], (v)[2]) - - -// geometry processing and shape analysis framework -namespace gproshan { - - -/*! - The vertex class represents a 3D point and implements 3D vector operations. -*/ -using vertex = vec; - - -} // namespace gproshan - -#endif // VERTEX_H - diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index adc02950..9f8239f7 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -8,8 +8,6 @@ #include #include -#include - // geometry processing and shape analysis framework // raytracing approach @@ -50,17 +48,17 @@ class raytracing raytracing() = default; virtual ~raytracing() = default; - virtual void render(glm::vec4 * img, const render_params & params, const bool & flat); + virtual void render(vec4 * img, const render_params & params, const bool & flat); - virtual float * raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, + virtual float * raycaster( const uvec2 & windows_size, + const mat4 & proj_view_mat, const vertex & cam_pos, const index_t & samples = 4 ); vertex ray_view_dir( const index_t & x, const index_t & y, - const glm::vec2 & windows_size, - const glm::mat4 & inv_proj_view, + const uvec2 & windows_size, + const mat4 & inv_proj_view, const vertex & cam_pos ); @@ -73,11 +71,11 @@ class raytracing ) { return NIL; }; protected: - virtual glm::vec4 intersect_li( const vertex &, // org, + virtual vec4 intersect_li( const vertex &, // org, const vertex &, // dir, const vertex &, // light, const bool & // flat - ) { return glm::vec4(0); }; + ) { return vec4(0); }; virtual float intersect_depth( const vertex &, // org, const vertex & // dir diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index c2de8aba..25934269 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -3,8 +3,7 @@ #include - -#include +#include // geometry processing and shape analysis framework @@ -21,7 +20,7 @@ struct render_params int viewport_y = 0; bool restart = false; bool viewport_is_window = true; - glm::mat4 proj_view_mat; + mat4 proj_view_mat; vertex cam_pos; std::vector lights; }; diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 8986b8a3..edd00ae0 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -4,8 +4,9 @@ #include #include +#include + #include -#include // geometry processing and shape analysis framework @@ -42,7 +43,7 @@ class embree : public raytracing public: embree(); embree( const std::vector & meshes, - const std::vector & model_mats, + const std::vector & model_mats, const bool & pointcloud = false, const float & pcr = 1 ); @@ -57,18 +58,18 @@ class embree : public raytracing bool occluded(ray_hit & r); void build_bvh( const std::vector & meshes, - const std::vector & model_mats, + const std::vector & model_mats, const bool & pointcloud = false); - index_t add_sphere(const glm::vec4 & xyzr); - index_t add_mesh(const che * mesh, const glm::mat4 & model_mat); + index_t add_sphere(const vec4 & xyzr); + index_t add_mesh(const che * mesh, const mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh); virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); - glm::vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); - glm::vec4 li(ray_hit r, const vertex & light, const bool & flat); + vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); + vec4 li(ray_hit r, const vertex & light, const bool & flat); - glm::vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); + vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); }; diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index f8540376..941e2996 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -12,8 +12,6 @@ #include #include -#include - // geometry processing and shape analysis framework // raytracing approach @@ -52,10 +50,10 @@ class optix : public raytracing void * as_buffer = nullptr; public: - optix(const std::vector & meshes, const std::vector & model_mats); + optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); - void render(glm::vec4 * img, const render_params & params, const bool & flat); + void render(vec4 * img, const render_params & params, const bool & flat); private: void create_raygen_programs(); @@ -63,8 +61,8 @@ class optix : public raytracing void create_hitgroup_programs(); void create_pipeline(); void build_sbt(); - OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); - void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const glm::mat4 & model_mat); + OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); + void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat); }; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 44f5c381..ffa53d30 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -2,8 +2,7 @@ #define CAMERA_H #include - -#include +#include // geometry processing and shape analysis framework @@ -25,7 +24,7 @@ class camera quaternion up = vertex{0, 1, 0}; public: - glm::mat4 look_at(const quaternion & r); + mat4 look_at(const quaternion & r); quaternion current_rotation() const; void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); void motion(const double & x, const double & y, const int & w, const int & h); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index be76ad2e..5b3cf3aa 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -42,7 +42,7 @@ class che_viewer rt::raytracing * rt_embree = nullptr; rt::raytracing * rt_optix = nullptr; - glm::mat4 model_mat = glm::mat4(1); + mat4 model_mat = mat4::identity(); index_t idx_colormap = 1; // colormap index defined in shaders/colormap.glsl index_t point_size = 1; @@ -80,7 +80,7 @@ class che_viewer void translate(const vertex & p); void scale(const real_t & s); - void select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const vertex & cam_pos); + void select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 96de4894..6cc8a981 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -4,8 +4,6 @@ #include #include -#include - #include #include #include diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 54ed5835..882d9470 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,6 +39,15 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { + mat4 a;/* { + {1, 0, 0, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 1} + };*/ + vec4 b = 4; + gproshan_log_var(a * a); + gproshan_log_var(a * b); if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); diff --git a/src/gproshan/mesh/vertex.cpp b/src/gproshan/mesh/vertex.cpp deleted file mode 100644 index ce7149f4..00000000 --- a/src/gproshan/mesh/vertex.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include - -#include - -using namespace std; - - -// geometry processing and shape analysis framework -namespace gproshan { - - -} // namespace gproshan - diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 979312a3..59b43fda 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -11,7 +11,7 @@ namespace gproshan::rt { std::default_random_engine raytracing::gen; std::uniform_real_distribution raytracing::randf; -void raytracing::render(glm::vec4 * img, const render_params & params, const bool & flat) +void raytracing::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) n_samples = 0; @@ -24,14 +24,14 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo } glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); - glm::vec4 li; + vec4 li; #pragma omp parallel for private(li) for(int i = 0; i < params.viewport_width; ++i) for(int j = 0; j < params.viewport_height; ++j) { //row major - glm::vec4 & color = img[j * params.viewport_width + i]; + vec4 & color = img[j * params.viewport_width + i]; const vertex & dir = ray_view_dir( i + params.viewport_x, j + params.viewport_y, {window_width, window_height}, @@ -39,7 +39,7 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo params.cam_pos ); - li = glm::vec4(0); + li = vec4(0); for(auto & l: params.lights) li += intersect_li(params.cam_pos, dir, l, flat); @@ -49,14 +49,14 @@ void raytracing::render(glm::vec4 * img, const render_params & params, const boo ++n_samples; } -float * raytracing::raycaster( const glm::uvec2 & windows_size, - const glm::mat4 & proj_view_mat, +float * raytracing::raycaster( const uvec2 & windows_size, + const mat4 & proj_view_mat, const vertex & cam_pos, const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; - glm::mat4 inv_proj_view = glm::inverse(proj_view_mat); + mat4 inv_proj_view;// = glm::inverse(proj_view_mat); #pragma omp parallel for for(index_t i = 0; i < windows_size.x; ++i) @@ -75,12 +75,12 @@ float * raytracing::raycaster( const glm::uvec2 & windows_size, return frame; } -vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const glm::vec2 & windows_size, const glm::mat4 & inv_proj_view, const vertex & cam_pos) +vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos) { - glm::vec2 screen = glm::vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); - glm::vec4 view = glm::vec4(screen.x * 2.f - 1.f, screen.y * 2.f - 1.f, 1.f, 1.f); - glm::vec4 q = inv_proj_view * view; - vertex p = {q.x / q.w, q.y / q.w, q.z / q.w}; + vec2 screen = vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); + vec4 view = {screen.x * 2 - 1, screen.y * 2 - 1, 1, 1}; + vec4 q = inv_proj_view * view; + vertex p = q / q[3]; return normalize(p - cam_pos); } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index c73dbff1..a4229fc3 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -105,7 +105,7 @@ embree::embree() rtcSetDeviceErrorFunction(device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float & pcr): embree() +embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float & pcr): embree() { pc_radius = pcr; build_bvh(meshes, model_mats, pointcloud); @@ -154,12 +154,12 @@ bool embree::occluded(ray_hit & r) return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } -void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) +void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { for(index_t i = 0; i < meshes.size(); ++i) { che * mesh = meshes[i]; - const glm::mat4 & model_mat = model_mats[i]; + const mat4 & model_mat = model_mats[i]; if(mesh->is_pointcloud() || pointcloud) geomID_mesh[add_pointcloud(mesh)] = {mesh, true}; @@ -170,11 +170,11 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_vertices; ++i) - { - const glm::vec4 & v = model_mat * glm::vec4(glm_vec3(mesh->point(i)), 1); - vertices[i] = {v.x, v.y, v.z}; - } + vertices[i] = model_mat * vec4(mesh->point(i), 1); memcpy(tri_idxs, &mesh->halfedge(0), mesh->n_half_edges * sizeof(index_t)); @@ -224,7 +221,7 @@ index_t embree::add_pointcloud(const che * mesh) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); - glm::vec4 * pxyzr = (glm::vec4 *) rtcSetNewGeometryBuffer( geom, + vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, 4 * sizeof(float), @@ -241,7 +238,7 @@ index_t embree::add_pointcloud(const che * mesh) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - pxyzr[i] = glm::vec4(glm_vec3(mesh->point(i)), pc_radius); + pxyzr[i] = {mesh->point(i), pc_radius}; normal[i] = mesh->normal(i); } @@ -260,9 +257,9 @@ float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, do { - //glm::vec4 * xyzr = (glm::vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); + vec4 * xyzr = (vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); - sum_w += w = 1; //pc_radius - glm::length(r.position() - vertex(xyzr[r.hit.primID])); + sum_w += w = pc_radius - length(r.position() - vertex(xyzr[r.hit.primID])); position += w * r.position(); normal += w * r.normal(geomID_mesh[r.hit.geomID]); color += w * r.color(geomID_mesh[r.hit.geomID]); @@ -278,24 +275,24 @@ float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, return sum_w; } -glm::vec4 embree::li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near) +vec4 embree::li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near) { const vertex wi = normalize(light - position); const float dot_wi_normal = (wi, normal); const vertex L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; ray_hit r(position, wi, near); - return glm::vec4(glm_vec3((occluded(r) ? 0.4f : 1.f) * L), 1); + return {(occluded(r) ? 0.4f : 1.f) * L, 1}; } -glm::vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) +vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) { float total_tfar = 0; float near; vertex position, normal, color; - glm::vec4 L(0); + vec4 L(0); // while(total_tfar < 0.1) { total_tfar += r.ray.tfar; @@ -318,10 +315,10 @@ glm::vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) return L / total_tfar; } -glm::vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light,const bool & flat) +vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light,const bool & flat) { ray_hit r(org, dir); - return intersect(r) ? li(r, light, flat) : glm::vec4(0.f); + return intersect(r) ? li(r, light, flat) : vec4(0.f); } float embree::intersect_depth(const vertex & org, const vertex & dir) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 0536a6d3..9ce9374b 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -11,10 +11,6 @@ #include -#include -#include -#include - // geometry processing and shape analysis framework // raytracing approach @@ -46,7 +42,7 @@ void optix_log(unsigned int level, const char * tag, const char * message, void fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } -optix::optix(const std::vector & meshes, const std::vector & model_mats) +optix::optix(const std::vector & meshes, const std::vector & model_mats) { optixInit(); @@ -119,7 +115,7 @@ optix::~optix() cuda_free_CHE(dd_mesh[i], d_mesh[i]); } -void optix::render(glm::vec4 * img, const render_params & params, const bool & flat) +void optix::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) { @@ -142,7 +138,7 @@ void optix::render(glm::vec4 * img, const render_params & params, const bool & f optix_params.viewport_x = params.viewport_x; optix_params.viewport_y = params.viewport_y; - cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4)); + cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4)); } glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); @@ -150,7 +146,7 @@ void optix::render(glm::vec4 * img, const render_params & params, const bool & f optix_params.flat = flat; memcpy(optix_params.light, ¶ms.lights[0], sizeof(optix_params.light)); memcpy(optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); - memcpy(optix_params.inv_proj_view, glm::value_ptr(inv_proj_view), sizeof(optix_params.inv_proj_view)); + memcpy(optix_params.inv_proj_view, &inv_proj_view, sizeof(optix_params.inv_proj_view)); cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); @@ -166,7 +162,7 @@ void optix::render(glm::vec4 * img, const render_params & params, const bool & f cudaDeviceSynchronize(); - cudaMemcpy(img, optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(glm::vec4), cudaMemcpyDeviceToHost); + cudaMemcpy(img, optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4), cudaMemcpyDeviceToHost); } void optix::create_raygen_programs() @@ -343,7 +339,7 @@ void optix::build_sbt() sbt.hitgroupRecordCount = hitgroup_records.size(); } -OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) +OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) { OptixTraversableHandle optix_as_handle = {}; @@ -420,7 +416,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const return optix_as_handle; } -void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const glm::mat4 & model_mat) +void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat) { CHE * dd_m, * d_m; CHE h_m(mesh); @@ -429,14 +425,9 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); - float h_transform[12] = { model_mat[0][0], model_mat[1][0], model_mat[2][0], model_mat[3][0], - model_mat[0][1], model_mat[1][1], model_mat[2][1], model_mat[3][1], - model_mat[0][2], model_mat[1][2], model_mat[2][2], model_mat[3][2], - }; - - float * d_transform = nullptr; - cudaMalloc(&d_transform, sizeof(h_transform)); - cudaMemcpy(d_transform, h_transform, sizeof(h_transform), cudaMemcpyHostToDevice); + float * d_model_mat = nullptr; + cudaMalloc(&d_model_mat, sizeof(model_mat)); + cudaMemcpy(d_model_mat, &model_mat, sizeof(model_mat), cudaMemcpyHostToDevice); d_vertex_ptr = (CUdeviceptr) dd_m->GT; @@ -454,7 +445,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; - optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_transform; + optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; optix_trig_flags = 0; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 695916b5..329b091f 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -2,8 +2,6 @@ #include -#include - using namespace std; @@ -12,7 +10,7 @@ using namespace std; namespace gproshan { -glm::mat4 camera::look_at(const quaternion & r) +mat4 camera::look_at(const quaternion & r) { eye = r.conj() * pos * r; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index c4732fe6..9b267154 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -5,8 +5,6 @@ #include #include -#include - #include @@ -54,7 +52,7 @@ void che_viewer::init(che * m, const bool & center) void che_viewer::update() { - model_mat = glm::mat4(1); + model_mat = mat4::identity(); if(center_mesh) { vertex pmin = INFINITY; @@ -258,7 +256,7 @@ void che_viewer::scale(const real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void che_viewer::select(const index_t & x, const index_t & y, const glm::uvec2 & windows_size, const glm::mat4 & proj_view_mat, const vertex & cam_pos) +void che_viewer::select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & proj_view_mat, const vertex & cam_pos) { const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, glm::inverse(proj_view_mat), cam_pos); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 8dcb327f..da308e3e 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -8,9 +8,6 @@ #include #include -#include -#include - #include #include #include @@ -808,7 +805,7 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); - float * frame = rc.raycaster( glm::uvec2(view->viewport_width, view->viewport_height), + float * frame = rc.raycaster( uvec2(view->viewport_width, view->viewport_height), view->render_params.proj_view_mat, view->cam.eye ); @@ -833,7 +830,7 @@ void viewer::render_gl() glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glm::mat4 & proj_view_mat = render_params.proj_view_mat; + mat4 & proj_view_mat = render_params.proj_view_mat; glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); @@ -886,7 +883,7 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) render_params.restart = rt_frame.resize(viewport_width, viewport_height) || render_params.restart; glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); - glm::vec4 * img = (glm::vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); + vec4 * img = (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); std::vector & scene_lights = render_params.lights; scene_lights.clear(); From 4101f2bd43c09dac850bd2392481831c13643a2c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 10 Jun 2022 02:39:09 +0200 Subject: [PATCH 0673/1018] inverse mat, perspective and look at functions --- include/gproshan/geometry/mat.h | 16 +++++++++-- include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/raytracing/render_params.h | 2 +- include/gproshan/viewer/camera.h | 1 + include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 1 + src/gproshan/geometry/mat.cpp | 25 +++++++++++++++++ src/gproshan/raytracing/raytracing.cpp | 7 ++--- src/gproshan/raytracing/rt_optix.cpp | 4 +-- src/gproshan/viewer/camera.cpp | 31 ++++++++++++++++++++- src/gproshan/viewer/che_viewer.cpp | 6 ++-- src/gproshan/viewer/viewer.cpp | 19 +++++++------ 12 files changed, 89 insertions(+), 27 deletions(-) create mode 100644 src/gproshan/geometry/mat.cpp diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 000d0281..94db6345 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -24,6 +24,16 @@ class mat for(const row & r: list) rows[++i] = r; } + + T & operator () (const index_t & i, const index_t & j) + { + return rows[i][j]; + } + + const T & operator () (const index_t & i, const index_t & j) const + { + return rows[i][j]; + } row & operator [] (const index_t & i) { @@ -55,7 +65,7 @@ class mat return res; } - static mat identity() + static mat identity() { mat res; for(index_t i = 0; i < N; ++i) @@ -63,7 +73,7 @@ class mat return res; } - static mat transpose(const mat & m) + static mat transpose(const mat & m) { mat res; for(index_t i = 0; i < N; ++i) @@ -96,6 +106,8 @@ using mat2 = mat; using mat3 = mat; using mat4 = mat; +mat4 inverse(const mat4 & m); + } // namespace gproshan diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 9f8239f7..849ad247 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -51,7 +51,7 @@ class raytracing virtual void render(vec4 * img, const render_params & params, const bool & flat); virtual float * raycaster( const uvec2 & windows_size, - const mat4 & proj_view_mat, + const mat4 & inv_proj_view, const vertex & cam_pos, const index_t & samples = 4 ); diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 25934269..9a918bce 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -20,8 +20,8 @@ struct render_params int viewport_y = 0; bool restart = false; bool viewport_is_window = true; - mat4 proj_view_mat; vertex cam_pos; + mat4 inv_proj_view; std::vector lights; }; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index ffa53d30..734518c6 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -24,6 +24,7 @@ class camera quaternion up = vertex{0, 1, 0}; public: + static mat4 perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far); mat4 look_at(const quaternion & r); quaternion current_rotation() const; void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 5b3cf3aa..43806f15 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -80,7 +80,7 @@ class che_viewer void translate(const vertex & p); void scale(const real_t & s); - void select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & proj_view_mat, const vertex & cam_pos); + void select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 6cc8a981..c75513de 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -59,6 +59,7 @@ class viewer int & window_height = render_params.window_height; int & viewport_width = render_params.viewport_width; int & viewport_height = render_params.viewport_height; + mat4 proj_view_mat; bool hide_imgui = false; diff --git a/src/gproshan/geometry/mat.cpp b/src/gproshan/geometry/mat.cpp new file mode 100644 index 00000000..25287f2a --- /dev/null +++ b/src/gproshan/geometry/mat.cpp @@ -0,0 +1,25 @@ +#include + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +mat4 inverse(const mat4 & m) +{ + mat4 inv; + mat4 mt = mat4::transpose(m); + + a_mat a_inv((real_t *) &inv, 4, 4, false, true); + a_mat a_m((real_t *) &mt, 4, 4, false, true); + + arma::inv(a_inv, a_m); + + return mat4::transpose(inv); +} + + +} // namespace gproshan + diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 59b43fda..b1c7a662 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -23,7 +23,6 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f window_height = params.viewport_height; } - glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); vec4 li; #pragma omp parallel for private(li) @@ -35,7 +34,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f const vertex & dir = ray_view_dir( i + params.viewport_x, j + params.viewport_y, {window_width, window_height}, - inv_proj_view, + params.inv_proj_view, params.cam_pos ); @@ -50,14 +49,12 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f } float * raytracing::raycaster( const uvec2 & windows_size, - const mat4 & proj_view_mat, + const mat4 & inv_proj_view, const vertex & cam_pos, const index_t & samples ) { float * frame = new float[windows_size.x * windows_size.y]; - mat4 inv_proj_view;// = glm::inverse(proj_view_mat); - #pragma omp parallel for for(index_t i = 0; i < windows_size.x; ++i) for(index_t j = 0; j < windows_size.y; ++j) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 9ce9374b..1c7ea89c 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -141,12 +141,10 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4)); } - glm::mat4 inv_proj_view = glm::inverse(params.proj_view_mat); - optix_params.flat = flat; memcpy(optix_params.light, ¶ms.lights[0], sizeof(optix_params.light)); memcpy(optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); - memcpy(optix_params.inv_proj_view, &inv_proj_view, sizeof(optix_params.inv_proj_view)); + memcpy(optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 329b091f..e7d09003 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -13,8 +13,37 @@ namespace gproshan { mat4 camera::look_at(const quaternion & r) { eye = r.conj() * pos * r; + + vec3 p = eye; + vec3 Z = eye - r.conj() * (pos + front) * r; + vec3 Y = r.conj() * up * r; + vec3 X = Y * Z; + X = normalize(X); + Y = normalize(Y); + Z = normalize(Z); + + mat4 view = mat4::identity(); + view[0] = {X, - (X, p)}; + view[1] = {Y, - (Y, p)}; + view[2] = {Z, - (Z, p)}; + view[3] = {0, 0, 0, 1}; + + return view; +} + +mat4 camera::perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far) +{ + const real_t & top = near * std::tan(fovy * M_PI / 180); + const real_t & right = top * aspect; + + mat4 P; + P(0, 0) = near / right; + P(1, 1) = near / top; + P(2, 2) = - (far + near) / (far - near); + P(2, 3) = -2 * far * near / (far - near); + P(3, 2) = -1; - return glm::lookAt(glm_vec3(eye), glm_vec3(r.conj() * (pos + front) * r), glm_vec3(r.conj() * up * r)); + return P; } quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 9b267154..5b35a5e3 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -256,11 +256,9 @@ void che_viewer::scale(const real_t & s) model_mat = glm::scale(model_mat, {s, s, s}); } -void che_viewer::select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & proj_view_mat, const vertex & cam_pos) +void che_viewer::select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, - glm::inverse(proj_view_mat), cam_pos); - + const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, inv_proj_view_mat, cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index da308e3e..73d4b88f 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -91,7 +91,7 @@ bool viewer::run() cam_light = vertex{-1, 1, -2}; cam_light = r.conj() * cam_light * r; - render_params.proj_view_mat = glm::perspective(45.0f, float(viewport_width) / float(viewport_height), 0.01f, 1000.0f) * cam.look_at(r); + proj_view_mat = camera::perspective(45, real_t(viewport_width) / real_t(viewport_height), 0.01, 1000) * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); render_gl(); @@ -806,7 +806,8 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); float * frame = rc.raycaster( uvec2(view->viewport_width, view->viewport_height), - view->render_params.proj_view_mat, view->cam.eye + inverse(view->proj_view_mat), + view->cam.eye ); std::thread([](CImg img) @@ -830,12 +831,11 @@ void viewer::render_gl() glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - mat4 & proj_view_mat = render_params.proj_view_mat; - glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_view_mat"), 1, 0, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_view_mat"), 1, true, &proj_view_mat[0][0]); glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); @@ -897,6 +897,7 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; //render_params.viewport_is_window = false; + render_params.inv_proj_view = inverse(proj_view_mat); render_params.cam_pos = cam.eye; rt->render(img, render_params, mesh.render_flat); @@ -920,7 +921,7 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) mesh.select(ix % viewport_width, iy % viewport_height, {viewport_width, viewport_height}, - render_params.proj_view_mat, cam.eye); + inverse(proj_view_mat), cam.eye); } From 0aa081664f0a7f0fb23349ea966ec2e3fa7bfc47 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Jun 2022 00:09:08 +0200 Subject: [PATCH 0674/1018] camera: added loot_at and perspective functions --- include/gproshan/geometry/mat.h | 2 +- src/gproshan/viewer/camera.cpp | 34 ++++++++++++++++----------------- src/gproshan/viewer/viewer.cpp | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 94db6345..7b09645a 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -24,7 +24,7 @@ class mat for(const row & r: list) rows[++i] = r; } - + T & operator () (const index_t & i, const index_t & j) { return rows[i][j]; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index e7d09003..eece0859 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -13,19 +13,18 @@ namespace gproshan { mat4 camera::look_at(const quaternion & r) { eye = r.conj() * pos * r; - - vec3 p = eye; - vec3 Z = eye - r.conj() * (pos + front) * r; + + vec3 Z = r.conj() * (pos + front) * r - eye; + Z = normalize(Z); vec3 Y = r.conj() * up * r; - vec3 X = Y * Z; - X = normalize(X); Y = normalize(Y); - Z = normalize(Z); + vec3 X = Z * Y; + Y = X * Z; - mat4 view = mat4::identity(); - view[0] = {X, - (X, p)}; - view[1] = {Y, - (Y, p)}; - view[2] = {Z, - (Z, p)}; + mat4 view; + view[0] = {X, -(X, eye)}; + view[1] = {Y, -(Y, eye)}; + view[2] = {-Z, (Z, eye)}; view[3] = {0, 0, 0, 1}; return view; @@ -33,16 +32,15 @@ mat4 camera::look_at(const quaternion & r) mat4 camera::perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far) { - const real_t & top = near * std::tan(fovy * M_PI / 180); - const real_t & right = top * aspect; + const real_t & tan_fovy_2 = std::tan(fovy * M_PI / 360); mat4 P; - P(0, 0) = near / right; - P(1, 1) = near / top; - P(2, 2) = - (far + near) / (far - near); - P(2, 3) = -2 * far * near / (far - near); - P(3, 2) = -1; - + P(0, 0) = 1 / (aspect * tan_fovy_2); + P(1, 1) = 1 / tan_fovy_2; + P(2, 2) = - (far + near) / (far - near); + P(2, 3) = -2 * far * near / (far - near); + P(3, 2) = -1; + return P; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 73d4b88f..d06db9d6 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -86,7 +86,7 @@ bool viewer::run() { TIC(render_time) - quaternion r = cam.current_rotation(); + const quaternion & r = cam.current_rotation(); cam_light = vertex{-1, 1, -2}; cam_light = r.conj() * cam_light * r; From 406fffec6eef5cd97dd6f837db8fbb624b411218 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Jun 2022 01:12:58 +0200 Subject: [PATCH 0675/1018] mesh che: refactoring model mat --- apps/test_scanner.cpp | 43 ++-------------------------- include/gproshan/mesh/che.h | 2 ++ include/gproshan/viewer/camera.h | 2 +- include/gproshan/viewer/che_viewer.h | 3 -- src/gproshan/app_viewer.cpp | 9 ------ src/gproshan/mesh/che.cpp | 33 +++++++++++++++++++++ src/gproshan/raytracing/rt_optix.cu | 6 ++-- src/gproshan/viewer/camera.cpp | 7 ++--- src/gproshan/viewer/che_viewer.cpp | 37 ++---------------------- 9 files changed, 47 insertions(+), 95 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index 5d2ea998..57afbbd9 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -7,43 +7,6 @@ #include - -void translate(glm::mat4 & model_mat, const gproshan::vertex & p) -{ - model_mat = glm::translate(model_mat, glm_vec3(p)); -} - -void scale(glm::mat4 & model_mat, const gproshan::real_t & s) -{ - model_mat = glm::scale(model_mat, {s, s, s}); -} - -gproshan::mat4 normalize_coordinates(gproshan::che_ply * mesh) -{ - gproshan::mat4 model_mat = gproshan::mat4::identity(); - - gproshan::vertex pmin = INFINITY; - gproshan::vertex pmax = 0; - - for(gproshan::index_t v = 0; v < mesh->n_vertices; ++v) - { - const gproshan::vertex & p = mesh->point(v); - - pmin.x = std::min(pmin.x, p.x); - pmin.y = std::min(pmin.y, p.y); - pmin.z = std::min(pmin.z, p.z); - - pmax.x = std::max(pmax.x, p.x); - pmax.y = std::max(pmax.y, p.y); - pmax.z = std::max(pmax.z, p.z); - } - - scale(model_mat, 2.0 / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z})); - translate(model_mat, - (pmax + pmin) / 2); - - return model_mat; -} - int main(int argc, char* argv[]) { if(argc < 2 || argc > 7) @@ -65,11 +28,9 @@ int main(int argc, char* argv[]) gproshan::che_ply * mesh_ply = new gproshan::che_ply(argv[1]); - gproshan::rt::raytracing * rt_embree; - - gproshan::mat4 model_mat = normalize_coordinates(mesh_ply); + const gproshan::mat4 & model_mat = mesh_ply->normalize_box(); - rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}, false, pc_radius); + gproshan::rt::raytracing * rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}, false, pc_radius); gproshan::che * ptx_mesh = scanner_ptx(mesh_ply, rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 334606bb..20a147ad 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -135,6 +136,7 @@ class che // update methods void reload(); void normalize_sphere(const real_t & r = 1); + mat4 normalize_box(const real_t & side = 2) const; void merge(const che * mesh, const std::vector & com_vertices); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); void update_heatmap(const real_t * hm = nullptr); diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 734518c6..c81260eb 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -19,7 +19,7 @@ class camera public: quaternion eye; - quaternion pos = vertex{0, 0, -2.5}; + quaternion pos = vertex{0, 0, -3.14}; quaternion front = vertex{0, 0, 1}; quaternion up = vertex{0, 1, 0}; diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 43806f15..bff51966 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -77,9 +77,6 @@ class che_viewer void draw_point_cloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); - void translate(const vertex & p); - void scale(const real_t & s); - void select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); void log_info(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 882d9470..54ed5835 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,15 +39,6 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { - mat4 a;/* { - {1, 0, 0, 0}, - {0, 1, 0, 0}, - {0, 0, 1, 0}, - {0, 0, 0, 1} - };*/ - vec4 b = 4; - gproshan_log_var(a * a); - gproshan_log_var(a * b); if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index e39b60bf..764720a5 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace std; @@ -238,6 +239,38 @@ void che::normalize_sphere(const real_t & r) GT[v] *= r / max_norm; } +mat4 che::normalize_box(const real_t & side) const +{ + vertex pmin = INFINITY; + vertex pmax = 0; + + for(index_t v = 0; v < n_vertices; ++v) + { + const vertex & p = point(v); + + pmin.x = min(pmin.x, p.x); + pmin.y = min(pmin.y, p.y); + pmin.z = min(pmin.z, p.z); + + pmax.x = max(pmax.x, p.x); + pmax.y = max(pmax.y, p.y); + pmax.z = max(pmax.z, p.z); + } + + mat4 model_mat; + + const real_t & scale = side / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z}); + model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; + + const vertex & translate = - scale * (pmax + pmin) / 2; + model_mat(0, 3) = translate.x; + model_mat(1, 3) = translate.y; + model_mat(2, 3) = translate.z; + model_mat(3, 3) = 1; + + return model_mat; +} + void che::merge(const che * mesh, const vector & com_vertices) { // TODO diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 236b2442..de825565 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -145,13 +145,13 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu ipv[3]; for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) - ipv[i][j] = optix_params.inv_proj_view[i + j * 4]; + ipv[i][j] = optix_params.inv_proj_view[i * 4 + j]; - vertex_cu d = { optix_params.inv_proj_view[0 * 4 + 3], + vertex_cu e = { optix_params.inv_proj_view[0 * 4 + 3], optix_params.inv_proj_view[1 * 4 + 3], optix_params.inv_proj_view[2 * 4 + 3] }; - vertex_cu e = { optix_params.inv_proj_view[3 * 4 + 0], + vertex_cu d = { optix_params.inv_proj_view[3 * 4 + 0], optix_params.inv_proj_view[3 * 4 + 1], optix_params.inv_proj_view[3 * 4 + 2] }; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index eece0859..2f1066e7 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -14,19 +14,18 @@ mat4 camera::look_at(const quaternion & r) { eye = r.conj() * pos * r; - vec3 Z = r.conj() * (pos + front) * r - eye; + vec3 Z = r.conj() * front * r; Z = normalize(Z); vec3 Y = r.conj() * up * r; Y = normalize(Y); vec3 X = Z * Y; - Y = X * Z; mat4 view; view[0] = {X, -(X, eye)}; view[1] = {Y, -(Y, eye)}; view[2] = {-Z, (Z, eye)}; view[3] = {0, 0, 0, 1}; - + return view; } @@ -40,7 +39,7 @@ mat4 camera::perspective(const real_t & fovy, const real_t & aspect, const real_ P(2, 2) = - (far + near) / (far - near); P(2, 3) = -2 * far * near / (far - near); P(3, 2) = -1; - + return P; } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 5b35a5e3..29bb3bce 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -52,28 +52,7 @@ void che_viewer::init(che * m, const bool & center) void che_viewer::update() { - model_mat = mat4::identity(); - if(center_mesh) - { - vertex pmin = INFINITY; - vertex pmax = 0; - - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - const vertex & p = mesh->point(v); - - pmin.x = min(pmin.x, p.x); - pmin.y = min(pmin.y, p.y); - pmin.z = min(pmin.z, p.z); - - pmax.x = max(pmax.x, p.x); - pmax.y = max(pmax.y, p.y); - pmax.z = max(pmax.z, p.z); - } - - scale(2.0 / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z})); - translate(- (pmax + pmin) / 2); - } + model_mat = center_mesh ? mesh->normalize_box(2) : mat4::identity(); render_pointcloud = mesh->is_pointcloud(); selected_xyz.clear(); @@ -182,7 +161,7 @@ void che_viewer::update_instances_positions(const vector & translations) void che_viewer::draw(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, 0, &model_mat[0][0]); + glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); glProgramUniform1i(program, program("render_flat"), render_flat); glProgramUniform1i(program, program("render_lines"), render_lines); @@ -208,7 +187,7 @@ void che_viewer::draw(shader & program) void che_viewer::draw_point_cloud(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, 0, &model_mat[0][0]); + glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); glProgramUniform1i(program, program("render_lines"), render_lines); glProgramUniform1i(program, program("point_normals"), point_normals); @@ -246,16 +225,6 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) } } -void che_viewer::translate(const vertex & p) -{ - model_mat = glm::translate(model_mat, glm_vec3(p)); -} - -void che_viewer::scale(const real_t & s) -{ - model_mat = glm::scale(model_mat, {s, s, s}); -} - void che_viewer::select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, inv_proj_view_mat, cam_pos); From 0f8294cf4a731714cdc35c90d16a9f302562a97a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Jun 2022 01:53:56 +0200 Subject: [PATCH 0676/1018] fixing warnings uvec2 and updating build.yml --- .github/workflows/build.yml | 2 -- README.md | 4 ++-- include/gproshan/geometry/vec.h | 4 ++++ include/gproshan/raytracing/raytracing.h | 4 ++-- include/gproshan/viewer/che_viewer.h | 2 +- src/gproshan/raytracing/raytracing.cpp | 18 +++++++++--------- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 2 +- 8 files changed, 20 insertions(+), 18 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4e585cfe..68a80492 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,13 +27,11 @@ jobs: sudo apt update sudo apt install \ libarmadillo-dev \ - libeigen3-dev \ libcgal-dev \ libsuitesparse-dev \ libopenblas-dev \ libglew-dev \ libglfw3-dev \ - libglm-dev \ cimg-dev - name: Install Cuda diff --git a/README.md b/README.md index 0754d1cf..26721168 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,11 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.3, cuda >= 11.0, cmake >= 3.18, armadillo, eigen, cgal, suitesparse, openblas, glew, glfw3, glm, cimg, gnuplot, embree >= 3.13 +g++ >= 9.3, cuda >= 11.0, cmake >= 3.22, armadillo, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 In Ubuntu you can install them with: - sudo apt install cmake libarmadillo-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev libglm-dev cimg-dev gnuplot + sudo apt install cmake libarmadillo-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev cimg-dev gnuplot #### Installing Intel Embree diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 6b3f0ae1..8713565f 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -270,6 +270,10 @@ using uvec2 = vec; using uvec3 = vec; using uvec4 = vec; +using ivec2 = vec; +using ivec3 = vec; +using ivec4 = vec; + } // namespace gproshan diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 849ad247..4053e13e 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -50,14 +50,14 @@ class raytracing virtual void render(vec4 * img, const render_params & params, const bool & flat); - virtual float * raycaster( const uvec2 & windows_size, + virtual float * raycaster( const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos, const index_t & samples = 4 ); vertex ray_view_dir( const index_t & x, const index_t & y, - const uvec2 & windows_size, + const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos ); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index bff51966..1abceb09 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -77,7 +77,7 @@ class che_viewer void draw_point_cloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); - void select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); + void select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index b1c7a662..49706266 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -32,11 +32,11 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f //row major vec4 & color = img[j * params.viewport_width + i]; const vertex & dir = ray_view_dir( i + params.viewport_x, - j + params.viewport_y, - {window_width, window_height}, - params.inv_proj_view, - params.cam_pos - ); + j + params.viewport_y, + {window_width, window_height}, + params.inv_proj_view, + params.cam_pos + ); li = vec4(0); for(auto & l: params.lights) @@ -48,7 +48,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f ++n_samples; } -float * raytracing::raycaster( const uvec2 & windows_size, +float * raytracing::raycaster( const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos, const index_t & samples ) @@ -56,8 +56,8 @@ float * raytracing::raycaster( const uvec2 & windows_size, float * frame = new float[windows_size.x * windows_size.y]; #pragma omp parallel for - for(index_t i = 0; i < windows_size.x; ++i) - for(index_t j = 0; j < windows_size.y; ++j) + for(int i = 0; i < windows_size.x; ++i) + for(int j = 0; j < windows_size.y; ++j) { //row major float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; @@ -72,7 +72,7 @@ float * raytracing::raycaster( const uvec2 & windows_size, return frame; } -vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos) +vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos) { vec2 screen = vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); vec4 view = {screen.x * 2 - 1, screen.y * 2 - 1, 1, 1}; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 29bb3bce..9987a3ae 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -225,7 +225,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) } } -void che_viewer::select(const index_t & x, const index_t & y, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) +void che_viewer::select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, inv_proj_view_mat, cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index d06db9d6..db482ebf 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -805,7 +805,7 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); - float * frame = rc.raycaster( uvec2(view->viewport_width, view->viewport_height), + float * frame = rc.raycaster( {view->viewport_width, view->viewport_height}, inverse(view->proj_view_mat), view->cam.eye ); From a06ac47b789ba9f641d4526bdab1900412a3e581 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Jun 2022 11:57:57 +0200 Subject: [PATCH 0677/1018] fixing cgal dependency on eigen --- .github/workflows/build.yml | 1 + README.md | 4 ++-- src/gproshan/mdict/msparse_coding.cpp | 19 ------------------- 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 68a80492..9f660069 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,6 +27,7 @@ jobs: sudo apt update sudo apt install \ libarmadillo-dev \ + libeigen3-dev \ libcgal-dev \ libsuitesparse-dev \ libopenblas-dev \ diff --git a/README.md b/README.md index 26721168..60eea387 100644 --- a/README.md +++ b/README.md @@ -25,11 +25,11 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.3, cuda >= 11.0, cmake >= 3.22, armadillo, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 +g++ >= 9.3, cuda >= 11.0, cmake >= 3.22, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 In Ubuntu you can install them with: - sudo apt install cmake libarmadillo-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev cimg-dev gnuplot + sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev cimg-dev gnuplot #### Installing Intel Embree diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index b0609d9d..b911c76b 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -9,31 +9,12 @@ #include #include -#ifndef CGAL_PATCH_DEFS - #define CGAL_PATCH_DEFS - #define CGAL_EIGEN3_ENABLED - #define CGAL_USE_BOOST_PROGRAM_OPTIONS - #define CGAL_USE_GMP - #define DCGAL_USE_MPFR -#endif - -#include -#include - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -typedef real_t DFT; -typedef CGAL::Simple_cartesian Data_Kernel; -typedef Data_Kernel::Point_3 DPoint; -typedef Data_Kernel::Vector_3 DVector; -typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; -typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; - - size_t msparse_coding::L = 12; size_t msparse_coding::K = 10; size_t msparse_coding::T = 5; From a7ae02ec30037a66f3959b08581c4f6fea23669f Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Sat, 11 Jun 2022 12:02:10 +0200 Subject: [PATCH 0678/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9f660069..cc71ed34 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-20.04] + os: [ubuntu-20.04, ubuntu-22.04] config: [Release, Debug] cuda: [cuda, ''] From 8d36ce519f1e0ef3815458709ba79135c753ee78 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Jun 2022 16:15:48 +0200 Subject: [PATCH 0679/1018] mesh che: update merge method, fixing filling holes --- README.md | 2 +- include/gproshan/mesh/che.h | 2 +- src/gproshan/app_viewer.cpp | 22 +--- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 +- src/gproshan/mesh/che.cpp | 117 +++--------------- src/gproshan/mesh/che_fill_hole.cpp | 17 ++- 6 files changed, 38 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index 60eea387..ec45a044 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.3, cuda >= 11.0, cmake >= 3.22, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 +g++ >= 9.4, cuda >= 11.7, cmake >= 3.22, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 In Ubuntu you can install them with: diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 20a147ad..99fcb94a 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -137,7 +137,7 @@ class che void reload(); void normalize_sphere(const real_t & r = 1); mat4 normalize_box(const real_t & side = 2) const; - void merge(const che * mesh, const std::vector & com_vertices); + che * merge(const che * mesh, const std::vector & com_vertices); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); void update_heatmap(const real_t * hm = nullptr); void update_normals(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 54ed5835..34ae682d 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -797,22 +797,6 @@ bool app_viewer::process_key_components(viewer * p_view) // Hole Filling -bool paint_holes_vertices(viewer * p_view) -{ - app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); - - size_t nv = mesh->n_vertices; - - // TODO - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - if(v >= nv) mesh->heatmap(v) = .25; - - return false; -} - bool app_viewer::process_poisson(viewer * p_view, const index_t & k) { app_viewer * view = (app_viewer *) p_view; @@ -824,7 +808,6 @@ bool app_viewer::process_poisson(viewer * p_view, const index_t & k) TIC(view->time) poisson(mesh, old_n_vertices, k); TOC(view->time) gproshan_log_var(view->time); -// paint_holes_vertices(); mesh.update(); return false; @@ -856,7 +839,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) fill_all_holes(mesh); - paint_holes_vertices(p_view); + mesh.update(); return false; } @@ -887,7 +870,8 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) delete [] holes; delete [] border_vertices; - paint_holes_vertices(p_view); + + mesh.update(); return false; } diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 1ee6020a..1890e146 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -160,7 +160,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->is_vertex_bound(v) ? 1 : 0; - for(const index_t & he: mesh->star(v)) ++dv; + for([[maybe_unused]] const index_t & he: mesh->star(v)) ++dv; ++deg[dv]; } diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 764720a5..14f9e67c 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -271,110 +271,33 @@ mat4 che::normalize_box(const real_t & side) const return model_mat; } -void che::merge(const che * mesh, const vector & com_vertices) +///< vcommon correspond to the first vertices of the mesh with indices to the main mesh (this) +che * che::merge(const che * mesh, const vector & vcommon) { - // TODO -// write_file("big.off"); -// mesh->write_file("small.off"); -gproshan_debug(fill_holes); - size_t ncv = com_vertices.size(); - bool is_open = mesh->VT[next(mesh->EVT[0])] >= ncv; - gproshan_debug_var(is_open); - - size_t nv = n_vertices + mesh->n_vertices - ncv; - size_t nf = n_faces + mesh->n_faces; - size_t nh = n_half_edges + mesh->n_half_edges; - size_t ne = n_edges + mesh->n_edges - (ncv - is_open); - - vertex * aGT = new vertex[nv]; - index_t * aVT = new index_t[nh]; - index_t * aOT = new index_t[nh]; - index_t * aEVT = new index_t[nv]; - index_t * aET = new index_t[ne]; - index_t * aEHT = new index_t[nh]; - -gproshan_debug(fill_holes); - memcpy(aGT, GT, sizeof(vertex) * n_vertices); - memcpy(aGT + n_vertices, mesh->GT + ncv, sizeof(vertex) * (nv - n_vertices)); - - memcpy(aVT, VT, sizeof(index_t) * n_half_edges); - - index_t * t_aVT = aVT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; ++he) - t_aVT[he] = mesh->VT[he] < ncv ? com_vertices[mesh->VT[he]] : mesh->VT[he] + n_vertices - ncv; -gproshan_debug(fill_holes); - - memcpy(aOT, OT, sizeof(index_t) * n_half_edges); -gproshan_debug(fill_holes); - - index_t * t_aOT = aOT + n_half_edges; - for(index_t he = 0; he < mesh->n_half_edges; ++he) - t_aOT[he] = mesh->OT[he] != NIL ? mesh->OT[he] + n_half_edges : NIL; -gproshan_debug(fill_holes); - - for(index_t v, he_v, he_i, i = 0; i < ncv; ++i) - { - he_i = mesh->EVT[i]; - if(he_i != NIL && mesh->VT[next(he_i)] < ncv) - { - v = com_vertices[mesh->VT[next(he_i)]]; - he_v = EVT[v]; - aOT[he_v] = he_i + n_half_edges; - aOT[aOT[he_v]] = he_v; - } - } + const size_t & n_vcommon = vcommon.size(); + const size_t & n_vnew = mesh->n_vertices - n_vcommon; -gproshan_debug(fill_holes); - memcpy(aEVT, EVT, sizeof(index_t) * n_vertices); -gproshan_debug(fill_holes); - if(is_open) - aEVT[com_vertices[0]] = mesh->EVT[0] != NIL ? mesh->EVT[0] + n_half_edges : NIL; + che * new_mesh = new che(n_vertices + n_vnew, n_faces + mesh->n_faces); -gproshan_debug(fill_holes); - index_t * t_aEVT = aEVT + n_vertices; - for(index_t v = ncv; v < mesh->n_vertices; ++v) - t_aEVT[v - ncv] = mesh->EVT[v] != NIL ? mesh->EVT[v] + n_half_edges : NIL; -gproshan_debug(fill_holes); + memcpy(new_mesh->GT, GT, sizeof(vertex) * n_vertices); + memcpy(new_mesh->GT + n_vertices, mesh->GT + n_vcommon, sizeof(vertex) * n_vnew); + memcpy(new_mesh->VC, VC, sizeof(rgb_t) * n_vertices); + memcpy(new_mesh->VC + n_vertices, mesh->VC + n_vcommon, sizeof(rgb_t) * n_vnew); + memcpy(new_mesh->VHC, VHC, sizeof(real_t) * n_vertices); + memcpy(new_mesh->VHC + n_vertices, mesh->VHC + n_vcommon, sizeof(real_t) * n_vnew); - memcpy(aET, ET, sizeof(index_t) * n_edges); -gproshan_debug(fill_holes); - - bool * common_edge = new bool[mesh->n_edges]; - memset(common_edge, 0, sizeof(bool) * mesh->n_edges); - for(index_t he_i, i = 0; i < ncv; ++i) - { - he_i = mesh->EVT[i]; - if(he_i != NIL && mesh->VT[next(he_i)] < ncv) - common_edge[mesh->EHT[he_i]] = true; - } + memcpy(new_mesh->VT, VT, sizeof(index_t) * n_half_edges); - index_t ae = n_edges; - for(index_t e = 0; e < mesh->n_edges; ++e) - if(!common_edge[e]) - aET[ae++] = mesh->ET[e] + n_half_edges; - - gproshan_debug_var(ae == ne); - gproshan_debug_var(ae); - gproshan_debug_var(ne); - assert(ae == ne); - delete [] common_edge; -gproshan_debug(fill_holes); - free(); + index_t * tVT = new_mesh->VT + n_half_edges; + for(index_t he = 0; he < mesh->n_half_edges; ++he) + tVT[he] = mesh->VT[he] < vcommon.size() ? vcommon[mesh->VT[he]] : mesh->VT[he] + n_vertices - n_vcommon; -gproshan_debug(fill_holes); - GT = aGT; - VT = aVT; - OT = aOT; - EVT = aEVT; - ET = aET; - EHT = aEHT; + new_mesh->update_evt_ot_et(); + new_mesh->update_eht(); - rw(n_vertices) = nv; - rw(n_faces) = nf; - rw(n_half_edges) = nh; - rw(n_edges) = ne; + new_mesh->update_normals(); - update_eht(); + return new_mesh; } void che::update_vertices(const vertex * positions, const size_t & n, const index_t & v_i) @@ -860,7 +783,7 @@ size_t che::max_degree() const for(index_t v = 0; v < n_vertices; ++v) { d = 0; - for(const index_t & he: star(v)) ++d; + for([[maybe_unused]] const index_t & he: star(v)) ++d; d += is_vertex_bound(v); md = max(md, d); } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 5ffebcc2..4076fbb6 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -259,10 +259,13 @@ void split_border(vector > & split_indices, che * mesh, c vector * fill_all_holes(che * mesh, const size_t & max_iter) { + gproshan_error(holes); vector * border_vertices; che ** holes; + gproshan_error(holes); tie(border_vertices, holes) = fill_all_holes_meshes(mesh, max_iter); + gproshan_error(holes); if(holes) { // FIX_BOUND @@ -271,7 +274,7 @@ vector * fill_all_holes(che * mesh, const size_t & max_iter) if(holes[b]) delete holes[b]; */ } - delete [] holes; + //delete [] holes; return border_vertices; } @@ -289,13 +292,16 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t holes = new che*[n_borders]; gproshan_debug(inpainting); + gproshan_error(holes); for(index_t b = 0; b < n_borders; ++b) border_vertices[b] = mesh->boundary(bounds[b]); gproshan_debug(inpainting); + gproshan_error(holes); for(index_t b = 0; b < n_borders; ++b) { + gproshan_error(holes); gproshan_debug_var(b); // vector > split_indices; // split_border(split_indices, mesh, border_vertices[b]); @@ -304,15 +310,16 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t gproshan_debug(inpainting); if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + to_string(b) + "_" + mesh->name() + ".off")); gproshan_debug(inpainting); + gproshan_error(holes); } - gproshan_debug(inpainting); + che * old = nullptr; for(index_t b = 0; b < n_borders; ++b) if(holes[b]) { - gproshan_debug(inpainting); - mesh->merge(holes[b], border_vertices[b]); - gproshan_debug(inpainting); + old = mesh; + mesh = old->merge(holes[b], border_vertices[b]); + delete old; } From 3e88f33db7b54270535180723346fc41837f12fe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 14 Jun 2022 14:09:09 +0200 Subject: [PATCH 0680/1018] fill holes: testing new approach --- src/gproshan/app_viewer.cpp | 68 +++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 34ae682d..1458256b 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -837,9 +837,73 @@ bool app_viewer::process_fill_holes(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - fill_all_holes(mesh); + // fill_all_holes(mesh); + /*********************************************************************/ + che * fill_mesh = new che(*mesh); + for(index_t & v: mesh->bounds()) + { + const std::vector & vbounds = mesh->boundary(v); + std::vector vertices; + std::vector faces; + + vertex center; + for(const index_t & v: vbounds) + { + vertices.push_back(mesh->point(v)); + center += mesh->point(v); + } + center /= vbounds.size(); + + std::priority_queue > front; + std::vector neigs(vertices.size()); + + auto bprev = [&](const index_t & v) -> index_t & + { + return neigs[v].x; + }; + auto bnext = [&](const index_t & v) -> index_t & + { + return neigs[v].y; + }; + auto push = [&](const uvec3 & p) + { + neigs[p.x] = {p.y, p.z}; + const real_t & angle = 21; + if(angle <= M_PI) + front.push({angle, p.x}); + }; + + push({0, vertices.size() - 1, 1}); + for(index_t i = 1; i < vertices.size() - 1; ++i) + push({i, i - 1, i + 1}); + push({vertices.size() - 1, vertices.size() - 2, 0}); + + std::vector border; + border.assign(true, vertices.size()); + + real_t angle; + index_t v0, v1, v2; + while(!front.empty()) + { + angle = front.top().first; + + if(!(border[v0] && border[v1] && border[v2])) + continue; - mesh.update(); + + } + + // vertices.push_back(center); + + che * old = fill_mesh; + che * hole = new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); + fill_mesh = old->merge(hole, vbounds); + delete old; + delete hole; + } + + view->add_mesh(fill_mesh); + /*********************************************************************/ return false; } From a34eab96b1ee668d23fb8de24f1d1326922a1b30 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 14 Jun 2022 15:22:34 +0200 Subject: [PATCH 0681/1018] che: refactoring, forward declaration star_he and its iterator --- include/gproshan/mesh/che.h | 116 ++++++++++++++++++------------------ src/gproshan/app_viewer.cpp | 14 ++--- src/gproshan/mesh/che.cpp | 81 +++++++++++++++++-------- 3 files changed, 120 insertions(+), 91 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 99fcb94a..956c865d 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -13,78 +13,27 @@ namespace gproshan { -size_t & rw(const size_t & n); -index_t trig(const index_t & he); -index_t next(const index_t & he); -index_t prev(const index_t & he); - - using vertex = vec3; - class che { - public: - class star_he - { - public: - class star_he_it - { - const che * mesh; - index_t he; - const index_t & he_end; - - public: - star_he_it(const che * p_mesh, const index_t & p_he, const index_t & p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} - star_he_it & operator ++ () - { - he = mesh->OT[prev(he)]; - he = he != he_end ? he : NIL; - return *this; - } - bool operator != (const star_he_it & it) - { - return he != it.he; - } - const index_t & operator * () - { - return he; - } - }; - - private: - const che * mesh; - const index_t & v; - - public: - star_he(const che * p_mesh, const index_t & p_v): mesh(p_mesh), v(p_v) {} - star_he_it begin() - { - return {mesh, mesh->EVT[v], mesh->EVT[v]}; - } - star_he_it end() - { - return {nullptr, NIL, NIL}; - } - }; + protected: + static size_t & rw(const size_t & n); + + private: + class star_he; public: enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types + struct rgb_t { unsigned char r = 230; unsigned char g = 240; unsigned char b = 250; - unsigned char & operator [] (const index_t & i) - { - return (&r)[i]; - } - - operator vertex () const - { - return {float(r) / 255, float(g) / 255, float(b) / 255}; - } + unsigned char & operator [] (const index_t & i); + operator vertex () const; }; const size_t n_vertices = 0; @@ -212,6 +161,34 @@ class che friend struct CHE; }; +class che::star_he +{ + class iterator; + + const che * mesh; + const index_t & v; + + public: + star_he(const che * p_mesh, const index_t & p_v); + iterator begin() const; + iterator end() const; +}; + +class che::star_he::iterator +{ + const che * mesh; + index_t he; + const index_t & he_end; + + public: + iterator(const che * p_mesh, const index_t & p_he, const index_t & p_he_end); + iterator & operator ++ (); + bool operator != (const iterator & it) const; + const index_t & operator * (); +}; + + +// aux che structs for cuda struct vertex_cu; @@ -233,6 +210,27 @@ struct CHE }; +// che halfedge functions + +inline index_t trig(const index_t & he) +{ + if(he == NIL) return NIL; + return he / che::mtrig; +} + +inline index_t next(const index_t & he) +{ + if(he == NIL) return NIL; + return che::mtrig * trig(he) + (he + 1) % che::mtrig; +} + +inline index_t prev(const index_t & he) +{ + if(he == NIL) return NIL; + return che::mtrig * trig(he) + (he + che::mtrig - 1) % che::mtrig; +} + + } // namespace gproshan #endif // CHE_H diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 1458256b..0bca94ee 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -845,7 +845,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) const std::vector & vbounds = mesh->boundary(v); std::vector vertices; std::vector faces; - + vertex center; for(const index_t & v: vbounds) { @@ -853,10 +853,10 @@ bool app_viewer::process_fill_holes(viewer * p_view) center += mesh->point(v); } center /= vbounds.size(); - + std::priority_queue > front; std::vector neigs(vertices.size()); - + auto bprev = [&](const index_t & v) -> index_t & { return neigs[v].x; @@ -877,7 +877,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) for(index_t i = 1; i < vertices.size() - 1; ++i) push({i, i - 1, i + 1}); push({vertices.size() - 1, vertices.size() - 2, 0}); - + std::vector border; border.assign(true, vertices.size()); @@ -885,8 +885,8 @@ bool app_viewer::process_fill_holes(viewer * p_view) index_t v0, v1, v2; while(!front.empty()) { - angle = front.top().first; - + angle = front.top().first; + if(!(border[v0] && border[v1] && border[v2])) continue; @@ -894,7 +894,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) } // vertices.push_back(center); - + che * old = fill_mesh; che * hole = new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); fill_mesh = old->merge(hole, vbounds); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 14f9e67c..a9ce9ed9 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -13,42 +13,22 @@ using namespace std; namespace gproshan { -size_t & rw(const size_t & n) +size_t & che::rw(const size_t & n) { return const_cast(n); } -index_t trig(const index_t & he) -{ - if(he == NIL) return NIL; - return he / che::mtrig; -} -index_t next(const index_t & he) +unsigned char & che::rgb_t::operator [] (const index_t & i) { - if(he == NIL) return NIL; - return che::mtrig * trig(he) + (he + 1) % che::mtrig; + return (&r)[i]; } -index_t prev(const index_t & he) +che::rgb_t::operator vertex () const { - if(he == NIL) return NIL; - return che::mtrig * trig(he) + (he + che::mtrig - 1) % che::mtrig; + return {float(r) / 255, float(g) / 255, float(b) / 255}; } -CHE::CHE(const che * mesh) -{ - n_vertices = mesh->n_vertices; - n_faces = mesh->n_faces; - n_half_edges = mesh->n_half_edges; - - GT = (vertex_cu *) mesh->GT; - VN = (vertex_cu *) mesh->VN; - VC = mesh->VC; - VT = mesh->VT; - OT = mesh->OT; - EVT = mesh->EVT; -} che::che(const che & mesh) { @@ -1114,5 +1094,56 @@ vector che::trig_convex_polygon(const index_t * P, const size_t & n) } +// iterator classes methods + +che::star_he::star_he(const che * p_mesh, const index_t & p_v): mesh(p_mesh), v(p_v) {} + +che::star_he::iterator che::star_he::begin() const +{ + return {mesh, mesh->EVT[v], mesh->EVT[v]}; +} + +che::star_he::iterator che::star_he::end() const +{ + return {nullptr, NIL, NIL}; +} + +che::star_he::iterator::iterator(const che * p_mesh, const index_t & p_he, const index_t & p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} + +che::star_he::iterator & che::star_he::iterator::operator ++ () +{ + he = mesh->OT[prev(he)]; + he = he != he_end ? he : NIL; + return *this; +} + +bool che::star_he::iterator::operator != (const iterator & it) const +{ + return he != it.he; +} + +const index_t & che::star_he::iterator::operator * () +{ + return he; +} + + +// cuda auxiliar che struct + +CHE::CHE(const che * mesh) +{ + n_vertices = mesh->n_vertices; + n_faces = mesh->n_faces; + n_half_edges = mesh->n_half_edges; + + GT = (vertex_cu *) mesh->GT; + VN = (vertex_cu *) mesh->VN; + VC = mesh->VC; + VT = mesh->VT; + OT = mesh->OT; + EVT = mesh->EVT; +} + + } // namespace gproshan From 977e9f8168510cd24bf14353b509964f739c1404 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 15 Jun 2022 00:19:11 +0200 Subject: [PATCH 0682/1018] viewer: refactoring max n_meshes, and tmp folder --- apps/CMakeLists.txt | 3 -- include/gproshan/include.h | 10 +++++- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 9 +++-- src/gproshan/app_viewer.cpp | 18 ++++++---- src/gproshan/viewer/viewer.cpp | 51 +++++++++++++++------------- 6 files changed, 53 insertions(+), 40 deletions(-) diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 4c95ff1a..50c007f5 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -16,6 +16,3 @@ target_link_libraries(test_image_denoising gproshan) add_executable(test_scanner test_scanner.cpp) target_link_libraries(test_scanner gproshan) - -file(MAKE_DIRECTORY ~/.gproshan/) - diff --git a/include/gproshan/include.h b/include/gproshan/include.h index fa2fe17f..5aef5573 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -3,6 +3,7 @@ #include +#include #include #include #include @@ -25,7 +26,14 @@ typedef unsigned int index_t; #endif -#define tmp_file_path(file) (std::string(getenv("HOME")) + "/.gproshan/"+ file) +inline std::string tmp_file_path(const std::string & file) +{ + const std::string & gproshan_home = std::string(getenv("HOME")) + "/.gproshan"; + std::filesystem::create_directory(gproshan_home); + + return gproshan_home + "/" + file; +} + #define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) #ifdef GPROSHAN_LOG diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 1abceb09..d7eab59b 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -36,7 +36,7 @@ class che_viewer GLuint vbo[6]; public: - int vx, vy; ///< viewport positions. + int vx = 0, vy = 0; ///< viewport positions. std::vector selected; std::vector selected_xyz; rt::raytracing * rt_embree = nullptr; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index c75513de..677394ea 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -24,8 +24,6 @@ #define ImGuiDataType_Real ImGuiDataType_Double #endif // GPROSHAN_FLOAT -#define N_MESHES 12 - // geometry processing and shape analysis framework namespace gproshan { @@ -49,7 +47,8 @@ class viewer process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; }; - static const int m_window_size[N_MESHES + 1][2]; + static const std::vector m_window_split; + static const size_t max_n_meshes; static const std::vector colormap; @@ -73,7 +72,7 @@ class viewer double render_time = 0; - che_viewer meshes[N_MESHES]; + che_viewer * meshes = nullptr; size_t n_meshes = 0; index_t idx_active_mesh = 0; @@ -98,7 +97,7 @@ class viewer che_viewer & active_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); - void add_mesh(che * p_mesh); + bool add_mesh(che * p_mesh); protected: virtual bool run(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 0bca94ee..b75062af 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -16,8 +16,8 @@ namespace gproshan { app_viewer::~app_viewer() { - for(che * mesh: meshes) - delete mesh; + for(index_t i = 0; i < n_meshes; ++i) + delete meshes[i]; } che * app_viewer::load_mesh(const string & file_path) @@ -679,9 +679,9 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static int K = 20; + static unsigned int K = 20; - ImGui::InputInt("eigenvectors", &K); + ImGui::InputInt("eigenvectors", (int *) &K); if(ImGui::Button("Run")) { @@ -694,10 +694,14 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) gproshan_log_var(K); - K = K < N_MESHES ? K : N_MESHES; - for(index_t k = 0; k < N_MESHES; ++k) + for(index_t k = 0; k < K; ++k) { - if(k) view->add_mesh(new che(*mesh)); + if(k) + { + if(!view->add_mesh(new che(*mesh))) + break; + } + view->idx_active_mesh = k; eigvec.col(k) -= eigvec.col(k).min(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index db482ebf..06d1ed02 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -33,13 +33,17 @@ using namespace std; namespace gproshan { -const int viewer::m_window_size[N_MESHES + 1][2] = {{1, 1}, +const std::vector viewer::m_window_split = { {1, 1}, {1, 1}, {1, 2}, {1, 3}, {2, 2}, {2, 3}, {2, 3}, {2, 4}, {2, 4}, {2, 5}, - {2, 5}, {3, 4}, {3, 4} + {2, 5}, {3, 4}, {3, 4}, + {4, 4}, {4, 4}, {4, 4}, {4, 4}, + {4, 5}, {4, 5}, {4, 5}, {4, 5} }; +const size_t viewer::max_n_meshes = m_window_split.size() - 1; + const std::vector viewer::colormap = { "vertex color", "blue", "red", @@ -64,7 +68,8 @@ viewer::viewer(const int & width, const int & height) s->update_normals(); sphere.init(s, false); - frames = new frame[N_MESHES]; + frames = new frame[max_n_meshes]; + meshes = new che_viewer[max_n_meshes]; } viewer::~viewer() @@ -78,6 +83,7 @@ viewer::~viewer() delete sphere; delete [] frames; + delete [] meshes; } bool viewer::run() @@ -344,25 +350,22 @@ void viewer::add_process(const int & key, const string & skey, const string & na else cerr << "Repeat key: " << key << endl; } -void viewer::add_mesh(che * p_mesh) +bool viewer::add_mesh(che * p_mesh) { - if(n_meshes == N_MESHES) - { - gproshan_log_var(n_meshes == N_MESHES); - gproshan_log_var(n_meshes); - return; - } + if(n_meshes == max_n_meshes) + return false; p_mesh->update_normals(); - meshes[n_meshes].init(p_mesh); - meshes[n_meshes].log_info(); - ++n_meshes; - idx_active_mesh = n_meshes - 1; - glfwSetWindowTitle(window, active_mesh()->filename.c_str()); + che_viewer & mesh = meshes[n_meshes]; + mesh.init(p_mesh); + mesh.log_info(); - const int & rows = m_window_size[n_meshes][0]; - const int & cols = m_window_size[n_meshes][1]; + idx_active_mesh = n_meshes++; + glfwSetWindowTitle(window, mesh->filename.c_str()); + + const int & rows = m_window_split[n_meshes].x; + const int & cols = m_window_split[n_meshes].y; for(index_t m = 0; m < n_meshes; ++m) { meshes[m].vx = m % cols; @@ -370,15 +373,17 @@ void viewer::add_mesh(che * p_mesh) } glfwGetFramebufferSize(window, &viewport_width, &viewport_height); - viewport_width /= m_window_size[n_meshes][1]; - viewport_height /= m_window_size[n_meshes][0]; + viewport_width /= cols; + viewport_height /= rows; + + return true; } void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - view->viewport_width = width / m_window_size[view->n_meshes][1]; - view->viewport_height = height / m_window_size[view->n_meshes][0]; + view->viewport_width = width / m_window_split[view->n_meshes].y; + view->viewport_height = height / m_window_split[view->n_meshes].x; } void viewer::window_size_callback(GLFWwindow * window, int width, int height) @@ -487,7 +492,7 @@ bool viewer::m_save_load_view(viewer * view) if(ImGui::Button("Save")) { - ofstream os(tmp_file_path("views/" + file)); + ofstream os(tmp_file_path(std::string("views/") + file)); os << view->cam; os.close(); } @@ -915,7 +920,7 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) index_t ix = x * xscale; index_t iy = y * yscale; - const int & cols = m_window_size[n_meshes][1]; + const int & cols = m_window_split[n_meshes].y; che_viewer & mesh = meshes[cols * (iy / viewport_height) + ix / viewport_width]; From e6feace2a2ac1be6e73c8b9a8b501b75a0213ac4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jun 2022 16:32:44 +0200 Subject: [PATCH 0683/1018] viewer: applying color opt to all meshes --- include/gproshan/viewer/viewer.h | 2 ++ include/imgui/imconfig.h | 2 +- src/gproshan/viewer/viewer.cpp | 28 ++++++++++++++++++++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 677394ea..581571bb 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -51,6 +51,8 @@ class viewer static const size_t max_n_meshes; static const std::vector colormap; + bool apply_all_meshes = false; + GLFWwindow * window = nullptr; rt::render_params render_params; diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index 7082c550..f2556017 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -27,7 +27,7 @@ //#define IMGUI_API __declspec( dllimport ) //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. -//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS +#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS //---- Disable all of Dear ImGui or don't implement standard windows. // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 06d1ed02..4ffb2b95 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -140,9 +140,17 @@ void viewer::imgui() if(ImGui::BeginMenu("Color")) { for(index_t i = 0; i < colormap.size(); ++i) + { if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) - mesh.idx_colormap = i; - + { + if(apply_all_meshes) + for(index_t m = 0; m < n_meshes; ++m) + meshes[m].idx_colormap = i; + else + mesh.idx_colormap = i; + } + ImGui::Separator(); + } ImGui::EndMenu(); } @@ -156,8 +164,9 @@ void viewer::imgui() if(pro.function != nullptr && pro.sub_menu == i) if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) sprintf(status_message, "%s", pro.selected ? pro.name.c_str() : ""); - } + //ImGui::Separator(); + } ImGui::EndMenu(); } } @@ -173,16 +182,19 @@ void viewer::imgui() ImGui::Text("github.com/larc/gproshan"); ImGui::End(); - ImGui::SetNextWindowSize(ImVec2(320, -1)); + ImGui::SetNextWindowSize(ImVec2(360, -1)); ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_Once); ImGui::Begin("gproshan"); ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); - ImGui::Text("%s", mesh->filename.c_str()); - ImGui::Text("%16s: %.3f", "FPS", 1.0 / render_time); - ImGui::Text("%16s: %10lu", "n_vertices", mesh->n_vertices); - ImGui::Text("%16s: %10lu", "n_faces", mesh->n_faces); + ImGui::Checkbox("apply options to all meshes\nmenus: [color, viewer, render, mesh]", &apply_all_meshes); + if(ImGui::CollapsingHeader(mesh->filename.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) + { + ImGui::Text("%13lu fps", size_t(1.0 / render_time)); + ImGui::Text("%13lu vertices", mesh->n_vertices); + ImGui::Text("%13lu faces", mesh->n_faces); + } if(mesh.render_pointcloud) { From beecb59400330fc71a8e51116b01f6baeddea877 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jun 2022 17:28:16 +0200 Subject: [PATCH 0684/1018] viewer: add check and apply function to all meshes for menus render, mesh, and color for starting --- include/gproshan/viewer/viewer.h | 4 +- src/gproshan/viewer/viewer.cpp | 147 ++++++++++++++++++++----------- 2 files changed, 98 insertions(+), 53 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 581571bb..8ca03740 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -1,8 +1,9 @@ #ifndef VIEWER_H #define VIEWER_H -#include #include +#include +#include #include #include @@ -156,6 +157,7 @@ class viewer static bool m_raycasting(viewer * view); void pick_vertex(const real_t & x, const real_t & y); + void check_apply_all_meshes(const std::function & fun); }; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4ffb2b95..1627dcd2 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -142,13 +142,10 @@ void viewer::imgui() for(index_t i = 0; i < colormap.size(); ++i) { if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) - { - if(apply_all_meshes) - for(index_t m = 0; m < n_meshes; ++m) - meshes[m].idx_colormap = i; - else + check_apply_all_meshes([&](che_viewer & mesh) + { mesh.idx_colormap = i; - } + }); ImGui::Separator(); } ImGui::EndMenu(); @@ -188,18 +185,20 @@ void viewer::imgui() ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); - ImGui::Checkbox("apply options to all meshes\nmenus: [color, viewer, render, mesh]", &apply_all_meshes); + ImGui::Checkbox("apply options to all meshes\nmenus: [color, render, mesh]", &apply_all_meshes); if(ImGui::CollapsingHeader(mesh->filename.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) { ImGui::Text("%13lu fps", size_t(1.0 / render_time)); ImGui::Text("%13lu vertices", mesh->n_vertices); ImGui::Text("%13lu faces", mesh->n_faces); - } - if(mesh.render_pointcloud) - { - ImGui::Checkbox("point_normals", &mesh.point_normals); - ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); + if(mesh.render_pointcloud) + { + ImGui::Indent(); + ImGui::Checkbox("point_normals", &mesh.point_normals); + ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); + ImGui::Unindent(); + } } for(auto & p: processes) @@ -549,11 +548,13 @@ bool viewer::m_reset_mesh(viewer * view) view->other_vertices.clear(); view->vectors.clear(); - che_viewer & mesh = view->active_mesh(); - mesh.selected.clear(); - mesh->reload(); - mesh->update_normals(); - mesh.update(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.selected.clear(); + mesh->reload(); + mesh->update_normals(); + mesh.update(); + }); return false; } @@ -613,9 +614,11 @@ bool viewer::m_save_mesh(viewer * view) bool viewer::m_normalize_mesh(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh->normalize_sphere(); - mesh.update(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh->normalize_sphere(); + mesh.update(); + }); return false; } @@ -688,7 +691,7 @@ bool viewer::m_setup_raytracing(viewer * view) case R_EMBREE: delete mesh.rt_embree; TIC(time); - mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); + mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); // TODO embree pc TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); break; @@ -710,108 +713,136 @@ bool viewer::m_setup_raytracing(viewer * view) bool viewer::m_render_gl(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_opt = R_GL; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_opt = R_GL; + }); + return false; } bool viewer::m_render_embree(viewer * view) { - che_viewer & mesh = view->active_mesh(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_opt = R_EMBREE; + }); view->render_params.restart = true; - mesh.render_opt = R_EMBREE; + return false; } bool viewer::m_render_optix(viewer * view) { - che_viewer & mesh = view->active_mesh(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_opt = R_OPTIX; + }); view->render_params.restart = true; - mesh.render_opt = R_OPTIX; + return false; } bool viewer::m_invert_normals(viewer * view) { - che_viewer & mesh = view->active_mesh(); - - mesh->invert_normals(); - mesh.update_vbo_normal(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh->invert_normals(); + mesh.update_vbo_normal(); + }); return false; } bool viewer::m_select_border_vertices(viewer * view) { - che_viewer & mesh = view->active_mesh(); - for(const index_t & b: mesh->bounds()) - for(const index_t & v: mesh->boundary(b)) - mesh.selected.push_back(v); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + for(const index_t & b: mesh->bounds()) + for(const index_t & v: mesh->boundary(b)) + mesh.selected.push_back(v); + }); return false; } bool viewer::m_clean_selected_vertices(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.selected.clear(); + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.selected.clear(); + }); return false; } bool viewer::m_render_pointcloud(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_pointcloud = !mesh.render_pointcloud; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_pointcloud = !mesh.render_pointcloud; + }); return false; } bool viewer::m_render_wireframe(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_wireframe = !mesh.render_wireframe; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_wireframe = !mesh.render_wireframe; + }); return false; } bool viewer::m_render_triangles(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_triangles = !mesh.render_triangles; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_triangles = !mesh.render_triangles; + }); return false; } bool viewer::m_render_gradients(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_gradients = !mesh.render_gradients; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_gradients = !mesh.render_gradients; + }); return false; } bool viewer::m_render_normals(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_normals = !mesh.render_normals; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_normals = !mesh.render_normals; + }); return false; } bool viewer::m_render_lines(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_lines = !mesh.render_lines; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_lines = !mesh.render_lines; + }); return false; } bool viewer::m_render_flat(viewer * view) { - che_viewer & mesh = view->active_mesh(); - mesh.render_flat = !mesh.render_flat; - view->render_params.restart = true; + view->check_apply_all_meshes([&](che_viewer & mesh) + { + mesh.render_flat = !mesh.render_flat; + view->render_params.restart = true; + }); return false; } @@ -941,6 +972,18 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) inverse(proj_view_mat), cam.eye); } +void viewer::check_apply_all_meshes(const std::function & fun) +{ + if(!apply_all_meshes) + { + fun(active_mesh()); + return; + } + + for(index_t i = 0; i < n_meshes; ++i) + fun(meshes[i]); +} + } // namespace gproshan From 23b93bb0c666eb2c48e66215afaee709e1ee7878 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jun 2022 17:45:17 +0200 Subject: [PATCH 0685/1018] app_viewer: eigenfunctions showing computing time on status bar --- src/gproshan/app_viewer.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index b75062af..a5752e1a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -679,9 +679,9 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static unsigned int K = 20; + static unsigned int n_eigs = 20; - ImGui::InputInt("eigenvectors", (int *) &K); + ImGui::InputInt("eigenvectors", (int *) &n_eigs); if(ImGui::Button("Run")) { @@ -689,29 +689,31 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) a_vec eigval; a_mat eigvec; - TIC(view->time) K = eigs_laplacian(mesh, eigval, eigvec, L, A, K); TOC(view->time) - gproshan_log_var(view->time); + TIC(view->time) + n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); + TOC(view->time) - gproshan_log_var(K); + sprintf(view->status_message, "computing %u eigs in %.3fs", n_eigs, view->time); - for(index_t k = 0; k < K; ++k) + for(index_t k = 0; k < n_eigs; ++k) { - if(k) + if(n_eigs) { if(!view->add_mesh(new che(*mesh))) break; } view->idx_active_mesh = k; + che_viewer & mesh = view->active_mesh(); eigvec.col(k) -= eigvec.col(k).min(); eigvec.col(k) /= eigvec.col(k).max(); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - view->active_mesh()->heatmap(v) = eigvec(v, k); + mesh->heatmap(v) = eigvec(v, k); - view->active_mesh().update_vbo(); + mesh.update_vbo(); } view->idx_active_mesh = 0; From d5e768b7993aff1dc4de98b470f112df1271ecfd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jun 2022 17:55:36 +0200 Subject: [PATCH 0686/1018] rt_embree: fix model_mat on add pointcloud --- include/gproshan/raytracing/rt_embree.h | 2 +- src/gproshan/raytracing/rt_embree.cpp | 32 +++++++++++++------------ src/gproshan/viewer/viewer.cpp | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index edd00ae0..9e9ac1ab 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -63,7 +63,7 @@ class embree : public raytracing index_t add_sphere(const vec4 & xyzr); index_t add_mesh(const che * mesh, const mat4 & model_mat); - virtual index_t add_pointcloud(const che * mesh); + virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index a4229fc3..6782bcb8 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -162,7 +162,7 @@ void embree::build_bvh(const std::vector & meshes, const std::vectoris_pointcloud() || pointcloud) - geomID_mesh[add_pointcloud(mesh)] = {mesh, true}; + geomID_mesh[add_pointcloud(mesh, model_mat)] = {mesh, true}; else geomID_mesh[add_mesh(mesh, model_mat)] = {mesh, false}; } @@ -175,8 +175,9 @@ index_t embree::add_sphere(const vec4 & xyzr) RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1); + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, 4 * sizeof(float), 1 + ); *pxyzr = xyzr; rtcCommitGeometry(geom); @@ -217,28 +218,29 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) return geom_id; } -index_t embree::add_pointcloud(const che * mesh) +index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) { RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_VERTEX, 0, - RTC_FORMAT_FLOAT4, - 4 * sizeof(float), - mesh->n_vertices - ); + RTC_BUFFER_TYPE_VERTEX, 0, + RTC_FORMAT_FLOAT4, + 4 * sizeof(float), + mesh->n_vertices + ); vertex * normal = (vertex *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_NORMAL, 0, - RTC_FORMAT_FLOAT3, - 3 * sizeof(float), - mesh->n_vertices - ); + RTC_BUFFER_TYPE_NORMAL, 0, + RTC_FORMAT_FLOAT3, + 3 * sizeof(float), + mesh->n_vertices + ); #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - pxyzr[i] = {mesh->point(i), pc_radius}; + pxyzr[i] = model_mat * vec4(mesh->point(i), 1); + pxyzr[i][3] = pc_radius; normal[i] = mesh->normal(i); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 1627dcd2..c0689f86 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -691,7 +691,7 @@ bool viewer::m_setup_raytracing(viewer * view) case R_EMBREE: delete mesh.rt_embree; TIC(time); - mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); // TODO embree pc + mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); TOC(time); sprintf(view->status_message, "build embree in %.3fs", time); break; From f7342010d868ada471e88f30fa42d8da82b2c0fb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Jun 2022 18:12:37 +0200 Subject: [PATCH 0687/1018] rt: adding add_light method to struct render params max number of vertex as points of light --- include/gproshan/raytracing/render_params.h | 14 +++++++++++++- src/gproshan/raytracing/raytracing.cpp | 6 +++--- src/gproshan/viewer/viewer.cpp | 9 ++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 9a918bce..943c4f15 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -10,6 +10,8 @@ namespace gproshan::rt { +const unsigned int max_lights = 16; + struct render_params { int window_width = 0; @@ -21,8 +23,18 @@ struct render_params bool restart = false; bool viewport_is_window = true; vertex cam_pos; + vertex lights[16]; + unsigned int n_lights = 0; mat4 inv_proj_view; - std::vector lights; + + bool add_light(const vertex & light) + { + if(n_lights == max_lights) + return false; + + lights[n_lights++] = light; + return true; + } }; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 49706266..78acb4fa 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -39,10 +39,10 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f ); li = vec4(0); - for(auto & l: params.lights) - li += intersect_li(params.cam_pos, dir, l, flat); + for(index_t l = 0; l < params.n_lights; ++l) + li += intersect_li(params.cam_pos, dir, params.lights[l], flat); - color = (color * float(n_samples) + li / float(params.lights.size())) / float(n_samples + 1); + color = (color * float(n_samples) + li / float(params.n_lights)) / float(n_samples + 1); } ++n_samples; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index c0689f86..b0a32d91 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -933,14 +933,13 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); vec4 * img = (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); - std::vector & scene_lights = render_params.lights; - scene_lights.clear(); + render_params.n_lights = 0;; for(const index_t & v: mesh.selected) - scene_lights.push_back(mesh->point(v)); + render_params.add_light(mesh->point(v)); - if(!scene_lights.size()) - scene_lights = {cam_light}; + if(!render_params.n_lights) + render_params.add_light(cam_light); //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; From 95a3e16e3ddd7aaa12e692cfe3b3aeab86222008 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Jun 2022 23:49:46 +0200 Subject: [PATCH 0688/1018] geometry: adding cu_vec class cuda template --- include/gproshan/app_viewer.h | 2 +- include/gproshan/geometry/cu_vec.cuh | 283 +++++++++++++++++++++++++++ src/gproshan/raytracing/rt_optix.cu | 4 +- 3 files changed, 287 insertions(+), 2 deletions(-) create mode 100644 include/gproshan/geometry/cu_vec.cuh diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 25177c18..1bda2311 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -48,7 +48,7 @@ class app_viewer : public viewer public: app_viewer() = default; - ~app_viewer(); + virtual ~app_viewer(); int main(int nargs, const char ** args); diff --git a/include/gproshan/geometry/cu_vec.cuh b/include/gproshan/geometry/cu_vec.cuh new file mode 100644 index 00000000..2d44bc38 --- /dev/null +++ b/include/gproshan/geometry/cu_vec.cuh @@ -0,0 +1,283 @@ +#ifndef CU_VEC_CUH +#define CU_VEC_CUH + +#include + +#include +#include +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +/*! + The cu_vec class represents a generic N dimensional vector and implements the cu_vector operations. +*/ +template +class cu_vec +{ + public: + union + { + T values[N] = {}; + struct + { + T x; + T y; + T z; + }; + }; + + public: + __host__ __device__ + cu_vec(const std::initializer_list & list) + { + int i = -1; + for(const T & v: list) + values[++i] = v; + } + + __host__ __device__ + cu_vec(const cu_vec & v, const T & val = 0) + { + for(index_t i = 0; i < N - 1; ++i) + values[i] = v[i]; + values[N - 1] = val; + } + + __host__ __device__ + cu_vec(const cu_vec & v) + { + for(index_t i = 0; i < N; ++i) + values[i] = v[i]; + } + + __host__ __device__ + cu_vec(const T & val = 0) + { + for(T & v: values) + v = val; + } + + __host__ __device__ + T & operator [] (const index_t & i) + { + assert(i < N); + return values[i]; + } + + __host__ __device__ + const T & operator [] (const index_t & i) const + { + assert(i < N); + return values[i]; + } + + ///< norm + __host__ __device__ + T norm() const + { + T res = 0; + for(const T & v: values) + res += v * v; + return std::sqrt(res); + } + + ///< length + __host__ __device__ + T length() const + { + return norm(); + } + + ///< dot product + __host__ __device__ + T operator , (const cu_vec & v) const + { + T res = 0; + for(index_t i = 0; i < N; ++i) + res += values[i] * v[i]; + return res; + } + + ///< scalar product + __host__ __device__ + cu_vec operator * (const T & a) const + { + cu_vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] * a; + return res; + } + + ///< scalar division + __host__ __device__ + cu_vec operator / (const T & a) const + { + cu_vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] / a; + return res; + } + + ///< sum of cu_vectors + __host__ __device__ + cu_vec operator + (const cu_vec & v) const + { + cu_vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] + v[i]; + return res; + } + + ///< difference of cu_vectors + __host__ __device__ + cu_vec operator - (const cu_vec & v) const + { + cu_vec res; + for(index_t i = 0; i < N; ++i) + res[i] = values[i] - v[i]; + return res; + } + + ///< negative of cu_vector + __host__ __device__ + cu_vec operator - () const + { + cu_vec res; + for(index_t i = 0; i < N; ++i) + res[i] = - values[i]; + return res; + } + + ///< scalar product self assign + __host__ __device__ + const cu_vec & operator *= (const T & a) + { + for(T & v: values) + v *= a; + return *this; + } + + ///< scalar division self assign + __host__ __device__ + const cu_vec & operator /= (const T & a) + { + for(T & v: values) + v /= a; + return *this; + } + + ///< sum of cu_vectors self assign + __host__ __device__ + const cu_vec & operator += (const cu_vec & v) + { + for(index_t i = 0; i < N; ++i) + values[i] += v[i]; + return *this; + } + + ///< difference of cu_vectors self assign + __host__ __device__ + const cu_vec & operator -= (const cu_vec & v) + { + for(index_t i = 0; i < N; ++i) + values[i] -= v[i]; + return *this; + } + + ///< comparison less than + __host__ __device__ + bool operator < (const cu_vec & v) const + { + if(x != v.x) return x < v.x; + if(y != v.y) return y < v.y; + return z < v.z; + } + + ///< comparison equal than + __host__ __device__ + bool operator == (const cu_vec & v) const + { + return x == v.x && y == v.y && z == v.z; + } + + __host__ __device__ + bool is_zero() + { + double eps = std::numeric_limits::epsilon(); + + return abs(double(x)) < eps && abs(double(y)) < eps && abs(double(z)) < eps; + } +}; + + +///< scalar product +template +__host__ __device__ cu_vec operator * (const T & a, const cu_vec & v) +{ + return v * a; +} + +///< cross product +template +__host__ __device__ cu_vec operator * (const cu_vec & u, const cu_vec & v) +{ + return {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; +} + +///< cross product +template +__host__ __device__ cu_vec cross(const cu_vec & u, const cu_vec & v) +{ + return u * v; +} + +///< dot product +template +__host__ __device__ T dot(const cu_vec & u, const cu_vec & v) +{ + return (u, v); +} + +///< norm +template +__host__ __device__ T norm(const cu_vec & v) +{ + return v.norm(); +} + +///< length +template +__host__ __device__ T length(const cu_vec & v) +{ + return v.length(); +} + +///< normalize cu_vector: divide by its norm +template +__host__ __device__ cu_vec normalize(const cu_vec & v) +{ + return v / norm(v); +} + + +using cu_vec2 = cu_vec; +using cu_vec3 = cu_vec; +using cu_vec4 = cu_vec; + +using cu_uvec2 = cu_vec; +using cu_uvec3 = cu_vec; +using cu_uvec4 = cu_vec; + +using cu_ivec2 = cu_vec; +using cu_ivec3 = cu_vec; +using cu_ivec4 = cu_vec; + + +} // namespace gproshan + +#endif // CU_VEC_CUH + diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index de825565..2f62480c 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -1,5 +1,6 @@ #include #include +#include #include @@ -163,8 +164,9 @@ extern "C" __global__ void __raygen__render_frame() vertex_cu p = (1.f / ((d, view) + de)) * q; vertex_cu ray_dir = p - cam_pos; ray_dir /= *ray_dir; - + vertex_cu pixelColorPRD; + cu_vec3 pdsadsa; uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); optixTrace( optix_params.traversable, From 849a36122fd169f778f0d9525e0eb0090593c29a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 22 Jun 2022 02:00:53 +0200 Subject: [PATCH 0689/1018] geometry: removing vertex_cu, adding cuda to vec --- include/gproshan/geometry/cu_vec.cuh | 283 ------------------ include/gproshan/geometry/vec.h | 34 +++ include/gproshan/mesh/che.cuh | 20 +- include/gproshan/mesh/che.h | 22 -- include/gproshan/mesh/vertex.cuh | 117 -------- include/gproshan/raytracing/rt_optix.h | 1 + src/gproshan/geodesics/geodesics_ptp.cu | 13 +- .../geodesics/geodesics_ptp_coalescence.cu | 13 +- src/gproshan/mesh/che.cpp | 17 -- src/gproshan/mesh/che.cu | 24 +- src/gproshan/mesh/vertex.cu | 11 - src/gproshan/raytracing/rt_optix.cpp | 1 - src/gproshan/raytracing/rt_optix.cu | 80 ++--- 13 files changed, 120 insertions(+), 516 deletions(-) delete mode 100644 include/gproshan/geometry/cu_vec.cuh delete mode 100644 include/gproshan/mesh/vertex.cuh delete mode 100644 src/gproshan/mesh/vertex.cu diff --git a/include/gproshan/geometry/cu_vec.cuh b/include/gproshan/geometry/cu_vec.cuh deleted file mode 100644 index 2d44bc38..00000000 --- a/include/gproshan/geometry/cu_vec.cuh +++ /dev/null @@ -1,283 +0,0 @@ -#ifndef CU_VEC_CUH -#define CU_VEC_CUH - -#include - -#include -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -/*! - The cu_vec class represents a generic N dimensional vector and implements the cu_vector operations. -*/ -template -class cu_vec -{ - public: - union - { - T values[N] = {}; - struct - { - T x; - T y; - T z; - }; - }; - - public: - __host__ __device__ - cu_vec(const std::initializer_list & list) - { - int i = -1; - for(const T & v: list) - values[++i] = v; - } - - __host__ __device__ - cu_vec(const cu_vec & v, const T & val = 0) - { - for(index_t i = 0; i < N - 1; ++i) - values[i] = v[i]; - values[N - 1] = val; - } - - __host__ __device__ - cu_vec(const cu_vec & v) - { - for(index_t i = 0; i < N; ++i) - values[i] = v[i]; - } - - __host__ __device__ - cu_vec(const T & val = 0) - { - for(T & v: values) - v = val; - } - - __host__ __device__ - T & operator [] (const index_t & i) - { - assert(i < N); - return values[i]; - } - - __host__ __device__ - const T & operator [] (const index_t & i) const - { - assert(i < N); - return values[i]; - } - - ///< norm - __host__ __device__ - T norm() const - { - T res = 0; - for(const T & v: values) - res += v * v; - return std::sqrt(res); - } - - ///< length - __host__ __device__ - T length() const - { - return norm(); - } - - ///< dot product - __host__ __device__ - T operator , (const cu_vec & v) const - { - T res = 0; - for(index_t i = 0; i < N; ++i) - res += values[i] * v[i]; - return res; - } - - ///< scalar product - __host__ __device__ - cu_vec operator * (const T & a) const - { - cu_vec res; - for(index_t i = 0; i < N; ++i) - res[i] = values[i] * a; - return res; - } - - ///< scalar division - __host__ __device__ - cu_vec operator / (const T & a) const - { - cu_vec res; - for(index_t i = 0; i < N; ++i) - res[i] = values[i] / a; - return res; - } - - ///< sum of cu_vectors - __host__ __device__ - cu_vec operator + (const cu_vec & v) const - { - cu_vec res; - for(index_t i = 0; i < N; ++i) - res[i] = values[i] + v[i]; - return res; - } - - ///< difference of cu_vectors - __host__ __device__ - cu_vec operator - (const cu_vec & v) const - { - cu_vec res; - for(index_t i = 0; i < N; ++i) - res[i] = values[i] - v[i]; - return res; - } - - ///< negative of cu_vector - __host__ __device__ - cu_vec operator - () const - { - cu_vec res; - for(index_t i = 0; i < N; ++i) - res[i] = - values[i]; - return res; - } - - ///< scalar product self assign - __host__ __device__ - const cu_vec & operator *= (const T & a) - { - for(T & v: values) - v *= a; - return *this; - } - - ///< scalar division self assign - __host__ __device__ - const cu_vec & operator /= (const T & a) - { - for(T & v: values) - v /= a; - return *this; - } - - ///< sum of cu_vectors self assign - __host__ __device__ - const cu_vec & operator += (const cu_vec & v) - { - for(index_t i = 0; i < N; ++i) - values[i] += v[i]; - return *this; - } - - ///< difference of cu_vectors self assign - __host__ __device__ - const cu_vec & operator -= (const cu_vec & v) - { - for(index_t i = 0; i < N; ++i) - values[i] -= v[i]; - return *this; - } - - ///< comparison less than - __host__ __device__ - bool operator < (const cu_vec & v) const - { - if(x != v.x) return x < v.x; - if(y != v.y) return y < v.y; - return z < v.z; - } - - ///< comparison equal than - __host__ __device__ - bool operator == (const cu_vec & v) const - { - return x == v.x && y == v.y && z == v.z; - } - - __host__ __device__ - bool is_zero() - { - double eps = std::numeric_limits::epsilon(); - - return abs(double(x)) < eps && abs(double(y)) < eps && abs(double(z)) < eps; - } -}; - - -///< scalar product -template -__host__ __device__ cu_vec operator * (const T & a, const cu_vec & v) -{ - return v * a; -} - -///< cross product -template -__host__ __device__ cu_vec operator * (const cu_vec & u, const cu_vec & v) -{ - return {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; -} - -///< cross product -template -__host__ __device__ cu_vec cross(const cu_vec & u, const cu_vec & v) -{ - return u * v; -} - -///< dot product -template -__host__ __device__ T dot(const cu_vec & u, const cu_vec & v) -{ - return (u, v); -} - -///< norm -template -__host__ __device__ T norm(const cu_vec & v) -{ - return v.norm(); -} - -///< length -template -__host__ __device__ T length(const cu_vec & v) -{ - return v.length(); -} - -///< normalize cu_vector: divide by its norm -template -__host__ __device__ cu_vec normalize(const cu_vec & v) -{ - return v / norm(v); -} - - -using cu_vec2 = cu_vec; -using cu_vec3 = cu_vec; -using cu_vec4 = cu_vec; - -using cu_uvec2 = cu_vec; -using cu_uvec3 = cu_vec; -using cu_uvec4 = cu_vec; - -using cu_ivec2 = cu_vec; -using cu_ivec3 = cu_vec; -using cu_ivec4 = cu_vec; - - -} // namespace gproshan - -#endif // CU_VEC_CUH - diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 8713565f..94a91383 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -8,6 +8,10 @@ #include #include +#ifndef __CUDACC__ + #define __host__ + #define __device__ +#endif // geometry processing and shape analysis framework namespace gproshan { @@ -32,6 +36,7 @@ class vec }; public: + __host__ __device__ vec(const std::initializer_list & list) { int i = -1; @@ -39,6 +44,7 @@ class vec values[++i] = v; } + __host__ __device__ vec(const vec & v, const T & val = 0) { for(index_t i = 0; i < N - 1; ++i) @@ -46,24 +52,28 @@ class vec values[N - 1] = val; } + __host__ __device__ vec(const vec & v) { for(index_t i = 0; i < N; ++i) values[i] = v[i]; } + __host__ __device__ vec(const T & val = 0) { for(T & v: values) v = val; } + __host__ __device__ T & operator [] (const index_t & i) { assert(i < N); return values[i]; } + __host__ __device__ const T & operator [] (const index_t & i) const { assert(i < N); @@ -71,6 +81,7 @@ class vec } ///< norm + __host__ __device__ T norm() const { T res = 0; @@ -80,12 +91,14 @@ class vec } ///< length + __host__ __device__ T length() const { return norm(); } ///< dot product + __host__ __device__ T operator , (const vec & v) const { T res = 0; @@ -95,6 +108,7 @@ class vec } ///< scalar product + __host__ __device__ vec operator * (const T & a) const { vec res; @@ -104,6 +118,7 @@ class vec } ///< scalar division + __host__ __device__ vec operator / (const T & a) const { vec res; @@ -113,6 +128,7 @@ class vec } ///< sum of vectors + __host__ __device__ vec operator + (const vec & v) const { vec res; @@ -122,6 +138,7 @@ class vec } ///< difference of vectors + __host__ __device__ vec operator - (const vec & v) const { vec res; @@ -131,6 +148,7 @@ class vec } ///< negative of vector + __host__ __device__ vec operator - () const { vec res; @@ -140,6 +158,7 @@ class vec } ///< scalar product self assign + __host__ __device__ const vec & operator *= (const T & a) { for(T & v: values) @@ -148,6 +167,7 @@ class vec } ///< scalar division self assign + __host__ __device__ const vec & operator /= (const T & a) { for(T & v: values) @@ -156,6 +176,7 @@ class vec } ///< sum of vectors self assign + __host__ __device__ const vec & operator += (const vec & v) { for(index_t i = 0; i < N; ++i) @@ -164,6 +185,7 @@ class vec } ///< difference of vectors self assign + __host__ __device__ const vec & operator -= (const vec & v) { for(index_t i = 0; i < N; ++i) @@ -172,6 +194,7 @@ class vec } ///< comparison less than + __host__ __device__ bool operator < (const vec & v) const { if(x != v.x) return x < v.x; @@ -180,11 +203,13 @@ class vec } ///< comparison equal than + __host__ __device__ bool operator == (const vec & v) const { return x == v.x && y == v.y && z == v.z; } + __host__ __device__ bool is_zero() { double eps = std::numeric_limits::epsilon(); @@ -196,6 +221,7 @@ class vec ///< scalar product template +__host__ __device__ vec operator * (const T & a, const vec & v) { return v * a; @@ -203,6 +229,7 @@ vec operator * (const T & a, const vec & v) ///< cross product template +__host__ __device__ vec operator * (const vec & u, const vec & v) { return {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; @@ -210,6 +237,7 @@ vec operator * (const vec & u, const vec & v) ///< cross product template +__host__ __device__ vec cross(const vec & u, const vec & v) { return u * v; @@ -217,6 +245,7 @@ vec cross(const vec & u, const vec & v) ///< dot product template +__host__ __device__ T dot(const vec & u, const vec & v) { return (u, v); @@ -224,6 +253,7 @@ T dot(const vec & u, const vec & v) ///< norm template +__host__ __device__ T norm(const vec & v) { return v.norm(); @@ -231,6 +261,7 @@ T norm(const vec & v) ///< length template +__host__ __device__ T length(const vec & v) { return v.length(); @@ -238,6 +269,7 @@ T length(const vec & v) ///< normalize vector: divide by its norm template +__host__ __device__ vec normalize(const vec & v) { return v / norm(v); @@ -245,6 +277,7 @@ vec normalize(const vec & v) ///< std ostream template +__host__ __device__ std::ostream & operator << (std::ostream & os, const vec & v) { for(index_t i = 0; i < N - 1; ++i) @@ -254,6 +287,7 @@ std::ostream & operator << (std::ostream & os, const vec & v) ///< std istream template +__host__ __device__ std::istream & operator >> (std::istream & is, vec & v) { for(index_t i = 0; i < N; ++i) diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index 8a8944a4..dfeac306 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -1,7 +1,6 @@ #ifndef CHE_CUH #define CHE_CUH -#include #include @@ -21,6 +20,25 @@ index_t cu_next(index_t he); __host__ __device__ index_t cu_prev(index_t he); + +struct CHE +{ + size_t n_vertices = 0; + size_t n_faces = 0; + size_t n_half_edges = 0; + + vertex * GT = nullptr; + vertex * VN = nullptr; + che::rgb_t * VC = nullptr; + index_t * VT = nullptr; + index_t * OT = nullptr; + index_t * EVT = nullptr; + + CHE() = default; + CHE(const che * mesh); +}; + + void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false, const bool & color = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 956c865d..c7731ec0 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -188,28 +188,6 @@ class che::star_he::iterator }; -// aux che structs for cuda - -struct vertex_cu; - -struct CHE -{ - size_t n_vertices = 0; - size_t n_faces = 0; - size_t n_half_edges = 0; - - vertex_cu * GT = nullptr; - vertex_cu * VN = nullptr; - che::rgb_t * VC = nullptr; - index_t * VT = nullptr; - index_t * OT = nullptr; - index_t * EVT = nullptr; - - CHE() = default; - CHE(const che * mesh); -}; - - // che halfedge functions inline index_t trig(const index_t & he) diff --git a/include/gproshan/mesh/vertex.cuh b/include/gproshan/mesh/vertex.cuh deleted file mode 100644 index 54b429fc..00000000 --- a/include/gproshan/mesh/vertex.cuh +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef VERTEX_CUH -#define VERTEX_CUH - - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -struct vertex_cu -{ - real_t x; - real_t y; - real_t z; - - __host__ __device__ - vertex_cu(const real_t & x_ = 0, const real_t & y_ = 0, const real_t & z_ = 0) - { - x = x_; - y = y_; - z = z_; - } - - __host__ __device__ - vertex_cu(const float3 & v) - { - x = v.x; - y = v.y; - z = v.z; - } - - __host__ __device__ - operator float3() const - { - return make_float3(x, y, z); - } - - __host__ __device__ - vertex_cu operator * (const vertex_cu & v) const - { - return vertex_cu(y * v.z - z * v.y, -(x * v.z - z * v.x), x * v.y - y * v.x); - } - - __host__ __device__ - real_t operator * () const - { - return sqrt(x * x + y * y + z * z); - } - - __host__ __device__ - vertex_cu operator / (const real_t & a) const - { - return vertex_cu(x / a, y / a, z / a); - } - - __host__ __device__ - void operator /= (const real_t & v) - { - x /= v; - y /= v; - z /= v; - } - - __host__ __device__ - real_t operator , (const vertex_cu & v) const - { - return x * v.x + y * v.y + z * v.z; - } - - __host__ __device__ - vertex_cu operator + (const vertex_cu & v) const - { - return vertex_cu(x + v.x, y + v.y, z + v.z); - } - - __host__ __device__ - void operator += (const vertex_cu & v) - { - x += v.x; - y += v.y; - z += v.z; - } - - __host__ __device__ - vertex_cu operator - (const vertex_cu & v) const - { - return vertex_cu(x - v.x, y - v.y, z - v.z); - } - - __host__ __device__ - void operator -= (const vertex_cu & v) - { - x -= v.x; - y -= v.y; - z -= v.z; - } - - __host__ __device__ - vertex_cu operator - () const - { - return vertex_cu(-x, -y, -z); - } - - __host__ __device__ - real_t & operator [] (const int & i) - { - return (&x)[i]; - } -}; - - -} // namespace gproshan - -#endif // VERTEX_CUH - diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 941e2996..da22cc34 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -2,6 +2,7 @@ #define RT_OPTIX_H #include +#include #include #include diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index c8ce3c04..a04d2b86 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -306,7 +306,7 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) x[1] = mesh->VT[cu_prev(he)]; x[2] = mesh->VT[he]; - vertex_cu X[2]; + vertex X[2]; X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; @@ -344,9 +344,10 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) tp[0] = t[0] - p; tp[1] = t[1] - p; - vertex_cu n(tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); + vertex n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), + tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), + tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) + }; real_t cond[2]; cond[0] = (X[0] , n); @@ -359,8 +360,8 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { real_t dp[2]; - dp[0] = dist[x[0]] + *X[0]; - dp[1] = dist[x[1]] + *X[1]; + dp[0] = dist[x[0]] + norm(X[0]); + dp[1] = dist[x[1]] + norm(X[1]); p = dp[dp[1] < dp[0]]; } diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index 985fc227..1763e61b 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -218,7 +218,7 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) x[1] = mesh->VT[cu_prev(he)]; x[2] = mesh->VT[he]; - vertex_cu X[2]; + vertex X[2]; X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; @@ -258,9 +258,10 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) tp[0] = t[0] - p; tp[1] = t[1] - p; - vertex_cu n(tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) ); + vertex n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), + tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), + tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) + }; real_t cond[2]; cond[0] = (X[0] , n); @@ -273,8 +274,8 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { real_t dp[2]; - dp[0] = dist[x[0]] + *X[0]; - dp[1] = dist[x[1]] + *X[1]; + dp[0] = dist[x[0]] + norm(X[0]); + dp[1] = dist[x[1]] + norm(X[1]); p = dp[dp[1] < dp[0]]; } diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index a9ce9ed9..a8d5eb22 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1128,22 +1128,5 @@ const index_t & che::star_he::iterator::operator * () } -// cuda auxiliar che struct - -CHE::CHE(const che * mesh) -{ - n_vertices = mesh->n_vertices; - n_faces = mesh->n_faces; - n_half_edges = mesh->n_half_edges; - - GT = (vertex_cu *) mesh->GT; - VN = (vertex_cu *) mesh->VN; - VC = mesh->VC; - VT = mesh->VT; - OT = mesh->OT; - EVT = mesh->EVT; -} - - } // namespace gproshan diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 76ed9fec..18fd50c2 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -26,6 +26,22 @@ index_t cu_prev(index_t he) return che::mtrig * cu_trig(he) + (he + che::mtrig - 1) % che::mtrig; } + +CHE::CHE(const che * mesh) +{ + n_vertices = mesh->n_vertices; + n_faces = mesh->n_faces; + n_half_edges = mesh->n_half_edges; + + GT = (vertex *) mesh->GT; + VN = (vertex *) mesh->VN; + VC = mesh->VC; + VT = mesh->VT; + OT = mesh->OT; + EVT = mesh->EVT; +} + + void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal, const bool & color) { dd_che = new CHE; @@ -33,13 +49,13 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm dd_che->n_faces = h_che->n_faces; dd_che->n_half_edges = h_che->n_half_edges; - cudaMalloc(&dd_che->GT, sizeof(vertex_cu) * h_che->n_vertices); - cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); + cudaMalloc(&dd_che->GT, sizeof(vertex) * h_che->n_vertices); + cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex) * h_che->n_vertices, cudaMemcpyHostToDevice); if(normal) { - cudaMalloc(&dd_che->VN, sizeof(vertex_cu) * h_che->n_vertices); - cudaMemcpy(dd_che->VN, h_che->VN, sizeof(vertex_cu) * h_che->n_vertices, cudaMemcpyHostToDevice); + cudaMalloc(&dd_che->VN, sizeof(vertex) * h_che->n_vertices); + cudaMemcpy(dd_che->VN, h_che->VN, sizeof(vertex) * h_che->n_vertices, cudaMemcpyHostToDevice); } if(color) diff --git a/src/gproshan/mesh/vertex.cu b/src/gproshan/mesh/vertex.cu deleted file mode 100644 index 52ec84a4..00000000 --- a/src/gproshan/mesh/vertex.cu +++ /dev/null @@ -1,11 +0,0 @@ -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - - - -} // namespace gproshan - diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 1c7ea89c..5fe8535f 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -3,7 +3,6 @@ #ifdef GPROSHAN_OPTIX -#include #include #include diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 2f62480c..0b38167a 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -1,6 +1,4 @@ -#include -#include -#include +#include #include @@ -12,19 +10,6 @@ namespace gproshan::rt { -static __forceinline__ __device__ -vertex_cu operator * (const real_t & a, const vertex_cu & v) -{ - return vertex_cu(a * v.x, a * v.y, a * v.z); -} - -static __forceinline__ __device__ -vertex_cu normalize (const vertex_cu & v) -{ - return v / *v; -} - - extern "C" __constant__ launch_params optix_params; static __forceinline__ __device__ @@ -72,34 +57,34 @@ extern "C" __global__ void __closesthit__radiance() const unsigned int sbtID = optixGetSbtGASIndex(); const float time = optixGetRayTime(); - vertex_cu data[3]; + vertex data[3]; optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); - const vertex_cu & A = data[0]; - const vertex_cu & B = data[1]; - const vertex_cu & C = data[2]; + const vertex & A = data[0]; + const vertex & B = data[1]; + const vertex & C = data[2]; - const vertex_cu Ng = normalize((B - A) * (C - A)); - const vertex_cu normal = optix_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + const vertex Ng = normalize((B - A) * (C - A)); + const vertex normal = optix_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; - const vertex_cu ca(mesh.VC[a].r, mesh.VC[a].g, mesh.VC[a].b); - const vertex_cu cb(mesh.VC[b].r, mesh.VC[b].g, mesh.VC[b].b); - const vertex_cu cc(mesh.VC[c].r, mesh.VC[c].g, mesh.VC[c].b); + const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; + const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; + const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; - const vertex_cu & light = *(vertex_cu *) optix_params.light; - const vertex_cu color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; - const vertex_cu position = (1.f - u - v) * A + u * B + v * C; + const vertex & light = *(vertex *) optix_params.light; + const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; + const vertex position = (1.f - u - v) * A + u * B + v * C; - const vertex_cu wi = normalize(light - position); + const vertex wi = normalize(light - position); const float dot_wi_normal = (wi, normal); - vertex_cu & L = *getPRD(); + vertex & L = *getPRD(); L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; unsigned int occluded = 1; optixTrace( optix_params.traversable, - position, - wi, + * (float3 *) &position, + * (float3 *) &wi, 1e-5f, // tmin 1e20f, // tmax 0.0f, // rayTime @@ -112,7 +97,7 @@ extern "C" __global__ void __closesthit__radiance() 1, // missSBTIndex occluded); - if(occluded) L = 0.4 * L; + if(occluded) L = 0.4f * L; } @@ -123,7 +108,7 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vertex_cu & prd = *getPRD(); + vertex & prd = *getPRD(); prd = {0, 0, 0}; } @@ -141,37 +126,36 @@ extern "C" __global__ void __raygen__render_frame() const float sx = (float(ix + optix_params.viewport_x) + .5f) / optix_params.window_width; const float sy = (float(iy + optix_params.viewport_y) + .5f) / optix_params.window_height; - vertex_cu & cam_pos = *(vertex_cu *) optix_params.cam_pos; + vertex & cam_pos = *(vertex *) optix_params.cam_pos; - vertex_cu ipv[3]; + vertex ipv[3]; for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) ipv[i][j] = optix_params.inv_proj_view[i * 4 + j]; - vertex_cu e = { optix_params.inv_proj_view[0 * 4 + 3], + vertex e = { optix_params.inv_proj_view[0 * 4 + 3], optix_params.inv_proj_view[1 * 4 + 3], optix_params.inv_proj_view[2 * 4 + 3] }; - vertex_cu d = { optix_params.inv_proj_view[3 * 4 + 0], + vertex d = { optix_params.inv_proj_view[3 * 4 + 0], optix_params.inv_proj_view[3 * 4 + 1], optix_params.inv_proj_view[3 * 4 + 2] }; float & de = optix_params.inv_proj_view[15]; - vertex_cu view = {sx * 2 - 1, sy * 2 - 1, 1}; - vertex_cu q = vertex_cu{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; - vertex_cu p = (1.f / ((d, view) + de)) * q; - vertex_cu ray_dir = p - cam_pos; - ray_dir /= *ray_dir; - - vertex_cu pixelColorPRD; - cu_vec3 pdsadsa; + vertex view = {sx * 2 - 1, sy * 2 - 1, 1}; + vertex q = vertex{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; + vertex p = (1.f / ((d, view) + de)) * q; + vertex ray_dir = normalize(p - cam_pos); + + vertex pixelColorPRD; + uint32_t u0, u1; packPointer(&pixelColorPRD, u0, u1); optixTrace( optix_params.traversable, - cam_pos, - ray_dir, + * (float3 *) &cam_pos, + * (float3 *) &ray_dir, 0.f, // tmin 1e20f, // tmax 0.0f, // rayTime From 48902880612dcf500700b1f0bd1d2d18397b9cb2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 23 Jun 2022 01:21:45 +0200 Subject: [PATCH 0690/1018] geometry: vec template class access x, y, z methods --- include/gproshan/geometry/vec.h | 82 +++++++++++++++++++++----- src/gproshan/app_viewer.cpp | 22 +++---- src/gproshan/geometry/convex_hull.cpp | 3 +- src/gproshan/mdict/msparse_coding.cpp | 8 +-- src/gproshan/mdict/patch.cpp | 44 +++++++------- src/gproshan/mesh/che.cpp | 20 +++---- src/gproshan/mesh/che_fill_hole.cpp | 44 +++++++------- src/gproshan/mesh/che_obj.cpp | 4 +- src/gproshan/mesh/che_off.cpp | 4 +- src/gproshan/mesh/che_poisson.cpp | 30 +++++----- src/gproshan/mesh/che_pts.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_xyz.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 10 ++-- src/gproshan/mesh/simplification.cpp | 12 ++-- src/gproshan/raytracing/raytracing.cpp | 12 ++-- src/gproshan/raytracing/rt_embree.cpp | 16 ++--- src/gproshan/raytracing/rt_optix.cu | 6 +- src/gproshan/scenes/scanner.cpp | 6 +- src/gproshan/viewer/camera.cpp | 10 ++-- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 14 ++--- 22 files changed, 201 insertions(+), 154 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 94a91383..e39816b2 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -24,16 +24,7 @@ template class vec { public: - union - { - T values[N] = {}; - struct - { - T x; - T y; - T z; - }; - }; + T values[N] = {}; public: __host__ __device__ @@ -80,6 +71,49 @@ class vec return values[i]; } + __host__ __device__ + T & x() + { + assert(N > 0); + return values[0]; + } + + __host__ __device__ + const T & x() const + { + assert(N > 0); + return values[0]; + } + + __host__ __device__ + T & y() + { + assert(N > 1); + return values[1]; + } + + __host__ __device__ + const T & y() const + { + assert(N > 1); + return values[1]; + } + + __host__ __device__ + T & z() + { + assert(N > 2); + return values[2]; + } + + __host__ __device__ + const T & z() const + { + assert(N > 2); + return values[2]; + } + + ///< norm __host__ __device__ T norm() const @@ -197,24 +231,33 @@ class vec __host__ __device__ bool operator < (const vec & v) const { - if(x != v.x) return x < v.x; - if(y != v.y) return y < v.y; - return z < v.z; + for(index_t i = 0; i < N; ++i) + if(values[i] != v[i]) + return values[i] < v[i]; + + return false; } ///< comparison equal than __host__ __device__ bool operator == (const vec & v) const { - return x == v.x && y == v.y && z == v.z; + for(index_t i = 0; i < N; ++i) + if(values[i] != v[i]) + return false; + + return true; } __host__ __device__ bool is_zero() { double eps = std::numeric_limits::epsilon(); + for(index_t i = 0; i < N; ++i) + if(abs(values[i]) > eps) + return false; - return abs(double(x)) < eps && abs(double(y)) < eps && abs(double(z)) < eps; + return true; } }; @@ -232,7 +275,14 @@ template __host__ __device__ vec operator * (const vec & u, const vec & v) { - return {u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x}; + const T & ux = u[0]; + const T & uy = u[1]; + const T & uz = u[2]; + const T & vx = v[0]; + const T & vy = v[1]; + const T & vz = v[2]; + + return {uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx}; } ///< cross product diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a5752e1a..69599653 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,6 +39,8 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { + gproshan_error_var(sizeof(vec2)); + gproshan_error_var(sizeof(vec3)); if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); @@ -563,21 +565,15 @@ bool app_viewer::process_mdict_patch(viewer * p_view) for(auto & u: p.vertices) mesh->heatmap(u) = 1; - vdir.x = p.T(0, 0); - vdir.y = p.T(0, 1); - vdir.z = p.T(0, 2); + vdir = {p.T(0, 0), p.T(0, 1), p.T(0, 2)}; view->vectors.push_back(mesh->point(v)); view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); - vdir.x = p.T(1, 0); - vdir.y = p.T(1, 1); - vdir.z = p.T(1, 2); + vdir = {p.T(1, 0), p.T(1, 1), p.T(1, 2)}; view->vectors.push_back(mesh->point(v)); view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); - vdir.x = p.T(2, 0); - vdir.y = p.T(2, 1); - vdir.z = p.T(2, 2); + vdir = {p.T(2, 0), p.T(2, 1), p.T(2, 2)}; view->vectors.push_back(mesh->point(v)); view->vectors.push_back(mesh->point(v) + 3 * mean_edge * vdir); @@ -865,18 +861,18 @@ bool app_viewer::process_fill_holes(viewer * p_view) auto bprev = [&](const index_t & v) -> index_t & { - return neigs[v].x; + return neigs[v].x(); }; auto bnext = [&](const index_t & v) -> index_t & { - return neigs[v].y; + return neigs[v].y(); }; auto push = [&](const uvec3 & p) { - neigs[p.x] = {p.y, p.z}; + neigs[p.x()] = {p.y(), p.z()}; const real_t & angle = 21; if(angle <= M_PI) - front.push({angle, p.x}); + front.push({angle, p.x()}); }; push({0, vertices.size() - 1, 1}); diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 5b63f628..afbdcf39 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -58,7 +58,8 @@ void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_point bool convex_hull::ccw(const vertex & p, const vertex & q, const vertex & r) { - return ((q - p) * (r - p)).z > 0; + // TODO vec2 + return ((q - p) * (r - p)).z() > 0; } diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index b911c76b..02fbec0c 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -863,7 +863,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) for(index_t s = 0; s < m_params.n_patches; ++s) { viewer::vectors.push_back({patches[s].x(0), patches[s].x(1), patches[s].x(2)}); - a_vec r = patches[s].x + 0.02 * patches[s].normal(); + a_vec r = patches[s].x() + 0.02 * patches[s].normal(); viewer::vectors.push_back({r(0), r(1), r(2)}); } */ @@ -924,9 +924,9 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) } else { - V(0, v) = mesh->point(v).x; - V(1, v) = mesh->point(v).y; - V(2, v) = mesh->point(v).z; + V(0, v) = mesh->point(v).x(); + V(1, v) = mesh->point(v).y(); + V(2, v) = mesh->point(v).z(); } } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 1f620a84..10eaf997 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -148,9 +148,9 @@ void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, this->T = T; x.resize(3); - x(0) = c.x; - x(1) = c.y; - x(2) = c.z; + x(0) = c.x(); + x(1) = c.y(); + x(2) = c.z(); std::random_device rd; //Will be used to obtain a seed for the random number engine @@ -203,7 +203,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind if(vertices.size() > min_points) { jet_fit_directions(mesh, v); - n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + n.x() = T(0, 2); n.y() = T(1, 2); n.z() = T(2, 2); radio = -INFINITY; for(index_t i=1; i < vertices.size(); ++i) @@ -286,7 +286,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, else normal_fit_directions(mesh,v); - n.x = T(0, 2); n.y = T(1, 2); n.z = T(2, 2); + n.x() = T(0, 2); n.y() = T(1, 2); n.z() = T(2, 2); radio = -INFINITY; vertex p; @@ -334,9 +334,9 @@ void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & if(!mask || mask(vertices[i])) { const vertex & v = mesh->point(vertices[i]); - xyz(0, j) = v.x; - xyz(1, j) = v.y; - xyz(2, j) = v.z; + xyz(0, j) = v.x(); + xyz(1, j) = v.y(); + xyz(2, j) = v.z(); //p idx patche where belongs to //j: local index //i: global index @@ -490,9 +490,9 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vectorpoint(vi); - xyz(0, i) = v.x; - xyz(1, i) = v.y; - xyz(2, i) = v.z; + xyz(0, i) = v.x(); + xyz(1, i) = v.y(); + xyz(2, i) = v.z(); vpatches[vi][p] = i++; } @@ -569,9 +569,9 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, { if(toplevel[u] == NIL) { - p(0) = mesh->point(u).x; - p(1) = mesh->point(u).y; - p(2) = mesh->point(u).z; + p(0) = mesh->point(u).x(); + p(1) = mesh->point(u).y(); + p(2) = mesh->point(u).z(); p = T.t() * (p - x); toplevel[u] = toplevel[v] + 1; @@ -595,19 +595,19 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) vector in_points; in_points.reserve(vertices.size()); for(const index_t & u: vertices) - in_points.push_back(DPoint(mesh->point(u).x, mesh->point(u).y, mesh->point(u).z)); + in_points.push_back(DPoint(mesh->point(u).x(), mesh->point(u).y(), mesh->point(u).z())); My_Monge_form monge_form; My_Monge_via_jet_fitting monge_fit; monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); vertex normal = mesh->normal(v); - monge_form.comply_wrt_given_normal(DVector(normal.x, normal.y, normal.z)); + monge_form.comply_wrt_given_normal(DVector(normal.x(), normal.y(), normal.z())); x.set_size(3); - x(0) = mesh->point(v).x; - x(1) = mesh->point(v).y; - x(2) = mesh->point(v).z; + x(0) = mesh->point(v).x(); + x(1) = mesh->point(v).y(); + x(2) = mesh->point(v).z(); T.set_size(3, 3); T(0, 0) = monge_form.maximal_principal_direction()[0]; @@ -625,9 +625,9 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) void patch::normal_fit_directions(che * mesh, const index_t & v) { x.set_size(3); - x(0) = mesh->point(v).x; - x(1) = mesh->point(v).y; - x(2) = mesh->point(v).z; + x(0) = mesh->point(v).x(); + x(1) = mesh->point(v).y(); + x(2) = mesh->point(v).z(); vertex nz = mesh->normal(v); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index a8d5eb22..9cc1d163 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -228,24 +228,24 @@ mat4 che::normalize_box(const real_t & side) const { const vertex & p = point(v); - pmin.x = min(pmin.x, p.x); - pmin.y = min(pmin.y, p.y); - pmin.z = min(pmin.z, p.z); + pmin.x() = min(pmin.x(), p.x()); + pmin.y() = min(pmin.y(), p.y()); + pmin.z() = min(pmin.z(), p.z()); - pmax.x = max(pmax.x, p.x); - pmax.y = max(pmax.y, p.y); - pmax.z = max(pmax.z, p.z); + pmax.x() = max(pmax.x(), p.x()); + pmax.y() = max(pmax.y(), p.y()); + pmax.z() = max(pmax.z(), p.z()); } mat4 model_mat; - const real_t & scale = side / std::max({pmax.x - pmin.x, pmax.y - pmin.y, pmax.z - pmin.z}); + const real_t & scale = side / std::max({pmax.x() - pmin.x(), pmax.y() - pmin.y(), pmax.z() - pmin.z()}); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; const vertex & translate = - scale * (pmax + pmin) / 2; - model_mat(0, 3) = translate.x; - model_mat(1, 3) = translate.y; - model_mat(2, 3) = translate.z; + model_mat(0, 3) = translate.x(); + model_mat(1, 3) = translate.y(); + model_mat(2, 3) = translate.z(); model_mat(3, 3) = 1; return model_mat; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 4076fbb6..908a51f1 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -40,18 +40,18 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, edge_v = mesh->vertex_he(next(mesh->evt(b))) - v; edge_v -= (normal_v, edge_v) * normal_v; - E(0, 2) = normal_v.x; - E(1, 2) = normal_v.y; - E(2, 2) = normal_v.z; + E(0, 2) = normal_v.x(); + E(1, 2) = normal_v.y(); + E(2, 2) = normal_v.z(); - E(0, 0) = edge_v.x; - E(1, 0) = edge_v.y; - E(2, 0) = edge_v.z; + E(0, 0) = edge_v.x(); + E(1, 0) = edge_v.y(); + E(2, 0) = edge_v.z(); E.col(1) = cross(E.col(2), E.col(0)); E = normalise(E); - ve(0) = v.x; ve(1) = v.y; ve(2) = v.z; + ve(0) = v.x(); ve(1) = v.y(); ve(2) = v.z(); ve = E.t() * ve; vertices.push_back(*((vertex *) ve.memptr())); @@ -217,13 +217,13 @@ void split_border(vector > & split_indices, che * mesh, c for(index_t i = 0; i < n; ++i) { normal = mesh->normal(border_vertices[i]); - data(0, i) = normal.x; - data(1, i) = normal.y; - data(2, i) = normal.z; + data(0, i) = normal.x(); + data(1, i) = normal.y(); + data(2, i) = normal.z(); /* - data(3, i) = mesh->point(border_vertices[i]).x; - data(4, i) = mesh->point(border_vertices[i]).y; - data(5, i) = mesh->point(border_vertices[i]).z; + data(3, i) = mesh->point(border_vertices[i]).x(); + data(4, i) = mesh->point(border_vertices[i]).y(); + data(5, i) = mesh->point(border_vertices[i]).z(); */ } @@ -350,14 +350,14 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, if(is_grow) normal = -normal; tmp_normals[v].resize(3); - tmp_normals[v](0) = normal.x; - tmp_normals[v](1) = normal.y; - tmp_normals[v](2) = normal.z; + tmp_normals[v](0) = normal.x(); + tmp_normals[v](1) = normal.y(); + tmp_normals[v](2) = normal.z(); tmp_vertices[v].resize(3); - tmp_vertices[v](0) = vertices[v].x; - tmp_vertices[v](1) = vertices[v].y; - tmp_vertices[v](2) = vertices[v].z; + tmp_vertices[v](0) = vertices[v].x(); + tmp_vertices[v](1) = vertices[v].y(); + tmp_vertices[v](2) = vertices[v].z(); if(v) init_perimeter += norm(vertices[v] - vertices[v - 1]); } @@ -595,9 +595,9 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c V.each_col() -= avg; a_vec orientation(3); - orientation(0) = normal.x; - orientation(1) = normal.y; - orientation(2) = normal.z; + orientation(0) = normal.x(); + orientation(1) = normal.y(); + orientation(2) = normal.z(); a_mat E; a_vec eigval; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 65021e9f..ec9e9f3e 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -85,11 +85,11 @@ void che_obj::write_file(const che * mesh, const string & file, const bool & col for(index_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->point(i); - fprintf(fp, "v %f %f %f", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, "v %f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); if(color) { const vertex & c = mesh->color(i); - fprintf(fp, " %f %f %f", (float) c.x, (float) c.y, (float) c.z); + fprintf(fp, " %f %f %f", (float) c.x(), (float) c.y(), (float) c.z()); } fprintf(fp, "\n"); } diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index aee004f8..996cc81a 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -97,7 +97,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t for(size_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->point(i); - fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, "%f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); if(off == COFF || off == NCOFF) { @@ -108,7 +108,7 @@ void che_off::write_file(const che * mesh, const string & file, const che_off::t if(off == NOFF || off == NCOFF) { const vertex & n = mesh->normal(i); - fprintf(fp, " %f %f %f", (float) n.x, (float) n.y, (float) n.z); + fprintf(fp, " %f %f %f", (float) n.x(), (float) n.y(), (float) n.z()); } fprintf(fp, "\n"); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index 1b8b3a65..01945570 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -19,9 +19,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) { if(v < old_n_vertices) { - B(v, 0) = mesh->point(v).x; - B(v, 1) = mesh->point(v).y; - B(v, 2) = mesh->point(v).z; + B(v, 0) = mesh->point(v).x(); + B(v, 1) = mesh->point(v).y(); + B(v, 2) = mesh->point(v).z(); } else B.row(v).zeros(); } @@ -40,9 +40,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) for(index_t v = 0; v < old_n_vertices; ++v) { - B.col(0) -= mesh->point(v).x * L.col(v); - B.col(1) -= mesh->point(v).y * L.col(v); - B.col(2) -= mesh->point(v).z * L.col(v); + B.col(0) -= mesh->point(v).x() * L.col(v); + B.col(1) -= mesh->point(v).y() * L.col(v); + B.col(2) -= mesh->point(v).z() * L.col(v); } L.shed_cols(0, old_n_vertices - 1); @@ -55,9 +55,9 @@ void poisson(che * mesh, const size_t & old_n_vertices, index_t k) if(spsolve(X, s * L, s * B)) for(index_t v = old_n_vertices; v < mesh->n_vertices; ++v) { - mesh->point(v).x = X(v - old_n_vertices, 0); - mesh->point(v).y = X(v - old_n_vertices, 1); - mesh->point(v).z = X(v - old_n_vertices, 2); + mesh->point(v).x() = X(v - old_n_vertices, 0); + mesh->point(v).y() = X(v - old_n_vertices, 1); + mesh->point(v).z() = X(v - old_n_vertices, 2); } } @@ -122,9 +122,9 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t index_t i = 0; for(index_t & b: sub_mesh_hole) { - P(0, i) = mesh->point(b).x; - P(1, i) = mesh->point(b).y; - P(2, i) = mesh->point(b).z; + P(0, i) = mesh->point(b).x(); + P(1, i) = mesh->point(b).y(); + P(2, i) = mesh->point(b).z(); ++i; } @@ -132,9 +132,9 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++i, ++v) { - H(0, i) = mesh->point(v).x; - H(1, i) = mesh->point(v).y; - H(2, i) = mesh->point(v).z; + H(0, i) = mesh->point(v).x(); + H(1, i) = mesh->point(v).y(); + H(2, i) = mesh->point(v).z(); } a_vec avg = mean(H, 1); diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index 40131284..bccd4ba7 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -56,7 +56,7 @@ void che_pts::write_file(const che * mesh, const string & file) { const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); - fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, "%f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); fprintf(fp, " %d ", int(mesh->heatmap(i) * 4095) - 2048); fprintf(fp, "%hhu %hhu %hhu\n", c.r, c.g, c.b); } diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 4a689225..8f79bc79 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -142,7 +142,7 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); - fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x, (float) v.y, (float) v.z, (float) 0, c.r, c.g, c.b ); + fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) 0, c.r, c.g, c.b ); } } diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 2dbf6ed0..0b9b4eed 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -58,7 +58,7 @@ void che_xyz::write_file(const che * mesh, const string & file, const bool & col for(index_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->point(i); - fprintf(fp, "%f %f %f", (float) v.x, (float) v.y, (float) v.z); + fprintf(fp, "%f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); if(color) { const rgb_t & c = mesh->rgb(i); diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 86e86fb3..80331860 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -48,12 +48,12 @@ const real_t & quaternion::operator [] (int index) const return v[index]; } -void quaternion::toMatrix(real_t Q[4][4]) const +void quaternion::toMatrix(real_t Q[4][4]) const // FIX { - Q[0][0] = s; Q[0][1] = -v.x; Q[0][2] = -v.y; Q[0][3] = -v.z; - Q[1][0] = v.x; Q[1][1] = s; Q[1][2] = -v.z; Q[1][3] = v.y; - Q[2][0] = v.y; Q[2][1] = v.z; Q[2][2] = s; Q[2][3] = -v.x; - Q[3][0] = v.z; Q[3][1] = -v.y; Q[3][2] = v.x; Q[3][3] = s; + Q[0][0] = s; Q[0][1] = -v.x(); Q[0][2] = -v.y(); Q[0][3] = -v.z(); + Q[1][0] = v.x(); Q[1][1] = s; Q[1][2] = -v.z(); Q[1][3] = v.y(); + Q[2][0] = v.y(); Q[2][1] = v.z(); Q[2][2] = s; Q[2][3] = -v.x(); + Q[3][0] = v.z(); Q[3][1] = -v.y(); Q[3][2] = v.x(); Q[3][3] = s; } real_t & quaternion::re() diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 247cc445..9a2ff735 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -41,9 +41,9 @@ void simplification::compute_quadrics() for(const index_t & he: mesh->star(v)) { n = mesh->normal_he(he); - p(0) = n.x; - p(1) = n.y; - p(2) = n.z; + p(0) = n.x(); + p(1) = n.y(); + p(2) = n.z(); p(3) = -(n, mesh->point(v)); Q[v] += p * p.t(); @@ -73,9 +73,9 @@ real_t simplification::compute_error(const index_t & e) vertex ve = create_vertex(e); a_vec v(4); - v(0) = ve.x; - v(1) = ve.y; - v(2) = ve.z; + v(0) = ve.x(); + v(1) = ve.y(); + v(2) = ve.z(); v(3) = 1; return as_scalar(v.t() * (Q[mesh->edge_u(e)] + Q[mesh->edge_v(e)]) * v); diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 78acb4fa..ee33a47d 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -53,14 +53,14 @@ float * raytracing::raycaster( const ivec2 & windows_size, const vertex & cam_pos, const index_t & samples ) { - float * frame = new float[windows_size.x * windows_size.y]; + float * frame = new float[windows_size.x() * windows_size.y()]; #pragma omp parallel for - for(int i = 0; i < windows_size.x; ++i) - for(int j = 0; j < windows_size.y; ++j) + for(int i = 0; i < windows_size.x(); ++i) + for(int j = 0; j < windows_size.y(); ++j) { //row major - float & color = frame[(windows_size.y - j - 1) * windows_size.x + i] = 0; + float & color = frame[(windows_size.y() - j - 1) * windows_size.x() + i] = 0; vertex dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); for(index_t s = 0; s < samples; ++s) @@ -74,8 +74,8 @@ float * raytracing::raycaster( const ivec2 & windows_size, vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos) { - vec2 screen = vec2((float(x) + randf(gen)) / windows_size.x, (float(y) + randf(gen)) / windows_size.y); - vec4 view = {screen.x * 2 - 1, screen.y * 2 - 1, 1, 1}; + vec2 screen = vec2((float(x) + randf(gen)) / windows_size.x(), (float(y) + randf(gen)) / windows_size.y()); + vec4 view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1}; vec4 q = inv_proj_view * view; vertex p = q / q[3]; diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 6782bcb8..155b1ffe 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -14,14 +14,14 @@ namespace gproshan::rt { embree::ray_hit::ray_hit(const vertex & p_org, const vertex & v_dir, float near, float far) { - ray.org_x = p_org.x; - ray.org_y = p_org.y; - ray.org_z = p_org.z; + ray.org_x = p_org.x(); + ray.org_y = p_org.y(); + ray.org_z = p_org.z(); ray.tnear = near; - ray.dir_x = v_dir.x; - ray.dir_y = v_dir.y; - ray.dir_z = v_dir.z; + ray.dir_x = v_dir.x(); + ray.dir_y = v_dir.y(); + ray.dir_z = v_dir.z(); ray.time = 0.0f; @@ -134,8 +134,8 @@ hit embree::intersect(const vertex & org, const vertex & dir) return { r.closest_vertex(mesh), r.ray.tfar, - {color.x, color.y, color.z}, - {normal.x, normal.y, normal.z} + {color.x(), color.y(), color.z()}, + {normal.x(), normal.y(), normal.z()} }; } diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 0b38167a..4d76e252 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -169,9 +169,9 @@ extern "C" __global__ void __raygen__render_frame() const uint32_t fbIndex = ix + iy * optix_params.viewport_width; float4 * frame = (float4 *) optix_params.color_buffer; - frame[fbIndex].x = pixelColorPRD.x; - frame[fbIndex].y = pixelColorPRD.y; - frame[fbIndex].z = pixelColorPRD.z; + frame[fbIndex].x = pixelColorPRD.x(); + frame[fbIndex].y = pixelColorPRD.y(); + frame[fbIndex].z = pixelColorPRD.z(); frame[fbIndex].w = 1; } diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 7df68e87..2c5ffafc 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -36,9 +36,9 @@ che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, mesh_ptx->point(v) = cam_pos + dir * h.dist; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; - mesh_ptx->rgb(v) = { (unsigned char) (h.color.x * 255), - (unsigned char) (h.color.y * 255), - (unsigned char) (h.color.z * 255) + mesh_ptx->rgb(v) = { (unsigned char) (h.color.x() * 255), + (unsigned char) (h.color.y() * 255), + (unsigned char) (h.color.z() * 255) }; } else diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 2f1066e7..b67a6440 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -50,11 +50,11 @@ quaternion camera::click_to_sphere(const double & x, const double & y, const int if(p.norm2() > 1) { p.normalize(); - p.im().z = 0; + p.im().z() = 0; } else { - p.im().z = sqrt(1 - p.norm2()); + p.im().z() = sqrt(1 - p.norm2()); } return p; @@ -86,17 +86,17 @@ void camera::motion(const double & x, const double & y, const int & w, const int void camera::zoom_in() { - pos.v.z += 0.02; + pos.v.z() += 0.02; } void camera::zoom_out() { - pos.v.z -= 0.02; + pos.v.z() -= 0.02; } real_t camera::zoom() const { - return -pos.v.z; + return -pos.v.z(); } ostream & operator << (ostream & os, const camera & cam) diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 9987a3ae..87e073ef 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -227,7 +227,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) void che_viewer::select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y - y, windows_size, inv_proj_view_mat, cam_pos); + const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y() - y, windows_size, inv_proj_view_mat, cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b0a32d91..e042d5b9 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -375,8 +375,8 @@ bool viewer::add_mesh(che * p_mesh) idx_active_mesh = n_meshes++; glfwSetWindowTitle(window, mesh->filename.c_str()); - const int & rows = m_window_split[n_meshes].x; - const int & cols = m_window_split[n_meshes].y; + const int & rows = m_window_split[n_meshes].x(); + const int & cols = m_window_split[n_meshes].y(); for(index_t m = 0; m < n_meshes; ++m) { meshes[m].vx = m % cols; @@ -393,8 +393,8 @@ bool viewer::add_mesh(che * p_mesh) void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - view->viewport_width = width / m_window_split[view->n_meshes].y; - view->viewport_height = height / m_window_split[view->n_meshes].x; + view->viewport_width = width / m_window_split[view->n_meshes].y(); + view->viewport_height = height / m_window_split[view->n_meshes].x(); } void viewer::window_size_callback(GLFWwindow * window, int width, int height) @@ -447,8 +447,8 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)) { - view->cam.pos.im().x = 2 * x / view->window_width - 1; - view->cam.pos.im().y = 2 * y / view->window_height - 1; + view->cam.pos.im().x() = 2 * x / view->window_width - 1; + view->cam.pos.im().y() = 2 * y / view->window_height - 1; view->render_params.restart = true; } } @@ -962,7 +962,7 @@ void viewer::pick_vertex(const real_t & x, const real_t & y) index_t ix = x * xscale; index_t iy = y * yscale; - const int & cols = m_window_split[n_meshes].y; + const int & cols = m_window_split[n_meshes].y(); che_viewer & mesh = meshes[cols * (iy / viewport_height) + ix / viewport_width]; From 101198d53ea3df11c58dcff1d94762909849b90f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Jul 2022 17:04:34 +0200 Subject: [PATCH 0691/1018] viewer: automatically select mesh on click multi viewport --- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/app_viewer.cpp | 1 + src/gproshan/viewer/viewer.cpp | 31 +++++++++++++++++-------------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 8ca03740..fadf7bc0 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -156,7 +156,7 @@ class viewer static bool m_raycasting(viewer * view); - void pick_vertex(const real_t & x, const real_t & y); + void pick_vertex(const int & x, const int & y); void check_apply_all_meshes(const std::function & fun); }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 69599653..d8dbf14a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -290,6 +290,7 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) che_viewer & mesh = view->active_mesh(); mesh->multiplicate_vertices(); + mesh->update_normals(); mesh.update(); mesh.log_info(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e042d5b9..d97c3245 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -426,9 +426,21 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); + + if(action == GLFW_RELEASE) + { + float xscale, yscale; + glfwGetWindowContentScale(window, &xscale, &yscale); + + index_t ix = xpos * xscale; + index_t iy = ypos * yscale; + const int & cols = m_window_split[view->n_meshes].y(); - if(mods == GLFW_MOD_SHIFT && action == GLFW_RELEASE) - view->pick_vertex(xpos, ypos); + view->idx_active_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; + + if(mods == GLFW_MOD_SHIFT) + view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); + } else if(button == GLFW_MOUSE_BUTTON_LEFT) view->cam.mouse(action == GLFW_PRESS, xpos, ypos, view->window_width, view->window_height); } @@ -955,20 +967,11 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) rt_frame.display(); } -void viewer::pick_vertex(const real_t & x, const real_t & y) +void viewer::pick_vertex(const int & x, const int & y) { - float xscale, yscale; - glfwGetWindowContentScale(window, &xscale, &yscale); - - index_t ix = x * xscale; - index_t iy = y * yscale; - const int & cols = m_window_split[n_meshes].y(); - - che_viewer & mesh = meshes[cols * (iy / viewport_height) + ix / viewport_width]; + che_viewer & mesh = active_mesh(); - mesh.select(ix % viewport_width, iy % viewport_height, - {viewport_width, viewport_height}, - inverse(proj_view_mat), cam.eye); + mesh.select(x, y, {viewport_width, viewport_height}, inverse(proj_view_mat), cam.eye); } void viewer::check_apply_all_meshes(const std::function & fun) From 12834d529db50a2bddd5eb552a596a468721e9aa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 8 Jul 2022 16:58:27 +0200 Subject: [PATCH 0692/1018] viewer: automatically select mesh on click multi viewport --- src/gproshan/viewer/viewer.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index d97c3245..f0dd21c4 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -427,21 +427,23 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - if(action == GLFW_RELEASE) + if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { float xscale, yscale; glfwGetWindowContentScale(window, &xscale, &yscale); - index_t ix = xpos * xscale; - index_t iy = ypos * yscale; + const index_t & ix = xpos * xscale; + const index_t & iy = ypos * yscale; const int & cols = m_window_split[view->n_meshes].y(); - - view->idx_active_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; - + const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; + if(idx_mesh < view->n_meshes) + view->idx_active_mesh = idx_mesh; + if(mods == GLFW_MOD_SHIFT) view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); } - else if(button == GLFW_MOUSE_BUTTON_LEFT) + + if(button == GLFW_MOUSE_BUTTON_LEFT) view->cam.mouse(action == GLFW_PRESS, xpos, ypos, view->window_width, view->window_height); } From ac446d6c161cb9cd80cb4dcf114d77d2d0b62ec9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 12 Jul 2022 17:36:47 +0200 Subject: [PATCH 0693/1018] viewer: fix autoselect mesh on click multiviewport fix macos linking opengl --- src/gproshan/CMakeLists.txt | 2 +- src/gproshan/app_viewer.cpp | 7 ++++--- src/gproshan/viewer/viewer.cpp | 5 ++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 97b92a63..d02b165a 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -7,7 +7,7 @@ add_library(gproshan SHARED ${cpp_sources}) target_link_libraries(gproshan embree) target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan GLEW::GLEW) -target_link_libraries(gproshan OpenGL::OpenGL) +target_link_libraries(gproshan OpenGL::GL) target_link_libraries(gproshan glfw) target_link_libraries(gproshan ${X11_X11_LIB}) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index d8dbf14a..948f8860 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -875,11 +875,12 @@ bool app_viewer::process_fill_holes(viewer * p_view) if(angle <= M_PI) front.push({angle, p.x()}); }; - - push({0, vertices.size() - 1, 1}); + + index_t nv = vertices.size(); + push({0, nv - 1, 1}); for(index_t i = 1; i < vertices.size() - 1; ++i) push({i, i - 1, i + 1}); - push({vertices.size() - 1, vertices.size() - 2, 0}); + push({nv - 1, nv - 2, 0}); std::vector border; border.assign(true, vertices.size()); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index f0dd21c4..cde60c30 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -406,10 +406,11 @@ void viewer::window_size_callback(GLFWwindow * window, int width, int height) void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, int) { + if(ImGui::GetIO().WantCaptureKeyboard) return; + if(action == GLFW_RELEASE) return; viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(ImGui::GetIO().WantCaptureKeyboard) return; process_t & pro = view->processes[key]; if(pro.function) @@ -422,6 +423,8 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) { + if(ImGui::GetIO().WantCaptureMouse) return; + viewer * view = (viewer *) glfwGetWindowUserPointer(window); double xpos, ypos; From 561904a581163d80702bd2a97540b54f00c6fd93 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 Aug 2022 20:22:52 +0200 Subject: [PATCH 0694/1018] scenes: drawing scanner cam_pos --- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/app_viewer.cpp | 17 +++++++++++++++-- src/gproshan/viewer/viewer.cpp | 18 ++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index fadf7bc0..9d3afb70 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -87,8 +87,8 @@ class viewer che_viewer sphere; shader shader_sphere; + std::vector sphere_points; - std::vector other_vertices; std::vector vectors; std::vector sub_menus; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 948f8860..a3da5a25 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -130,13 +130,26 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) static size_t n_cols = 2000; static const size_t n_min = 1000; static const size_t n_max = 1000000; + static vertex cam_pos = {0, 0, 0}; + + if(view->sphere_points.size() != 1) + { + view->sphere_points.clear(); + view->sphere_points.push_back(cam_pos); + } ImGui::SliderScalar("n_rows", ImGuiDataType_U64, &n_rows, &n_min, &n_max); ImGui::SliderScalar("n_cols", ImGuiDataType_U64, &n_cols, &n_min, &n_max); + if( ImGui_InputReal("cam_pos.x", &cam_pos.x(), 0.001, 2, "%.3lf") || + ImGui_InputReal("cam_pos.y", &cam_pos.y(), 0.001, 2, "%.3lf") || + ImGui_InputReal("cam_pos.z", &cam_pos.z(), 0.001, 2, "%.3lf") ) + { + view->sphere_points[0] = cam_pos; + } if(ImGui::Button("Scan")) { - che * ptx_mesh = scanner_ptx(mesh, mesh.rt_embree, n_rows, n_cols, {0, 0, 0}); + che * ptx_mesh = scanner_ptx(mesh, mesh.rt_embree, n_rows, n_cols, cam_pos); che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); view->add_mesh(ptx_mesh); } @@ -875,7 +888,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) if(angle <= M_PI) front.push({angle, p.x()}); }; - + index_t nv = vertices.size(); push({0, nv - 1, 1}); for(index_t i = 1; i < vertices.size() - 1; ++i) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cde60c30..cd41c2e9 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -407,7 +407,7 @@ void viewer::window_size_callback(GLFWwindow * window, int width, int height) void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, int) { if(ImGui::GetIO().WantCaptureKeyboard) return; - + if(action == GLFW_RELEASE) return; viewer * view = (viewer *) glfwGetWindowUserPointer(window); @@ -424,12 +424,12 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) { if(ImGui::GetIO().WantCaptureMouse) return; - + viewer * view = (viewer *) glfwGetWindowUserPointer(window); double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - + if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { float xscale, yscale; @@ -441,11 +441,11 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; if(idx_mesh < view->n_meshes) view->idx_active_mesh = idx_mesh; - + if(mods == GLFW_MOD_SHIFT) view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); } - + if(button == GLFW_MOUSE_BUTTON_LEFT) view->cam.mouse(action == GLFW_PRESS, xpos, ypos, view->window_width, view->window_height); } @@ -562,7 +562,6 @@ bool viewer::m_save_load_view(viewer * view) bool viewer::m_reset_mesh(viewer * view) { - view->other_vertices.clear(); view->vectors.clear(); view->check_apply_all_meshes([&](che_viewer & mesh) @@ -932,6 +931,13 @@ void viewer::render_gl() mesh.draw(shader_gradient); mesh.draw_selected_vertices(sphere, shader_sphere); + + if(sphere_points.size()) + { + sphere.model_mat = mat4::identity(); + sphere.update_instances_positions(sphere_points); + sphere.draw(shader_sphere); + } } render_params.restart = false; From f13fa7e9be2139d371472f6bf2163414d849ee84 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 Aug 2022 15:41:27 +0200 Subject: [PATCH 0695/1018] viewer: cleaning up --- src/gproshan/app_viewer.cpp | 2 -- src/gproshan/viewer/viewer.cpp | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a3da5a25..a2c47ef3 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -39,8 +39,6 @@ che * app_viewer::load_mesh(const string & file_path) int app_viewer::main(int nargs, const char ** args) { - gproshan_error_var(sizeof(vec2)); - gproshan_error_var(sizeof(vec3)); if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cd41c2e9..89c7f0a1 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -656,7 +656,7 @@ bool viewer::m_bgc_inc(viewer * view) if(view->bgc < 1) view->bgc += 0.05; else view->bgc = 1; - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + glClearColor(view->bgc, view->bgc, view->bgc, 1); return false; } @@ -666,7 +666,7 @@ bool viewer::m_bgc_dec(viewer * view) if(view->bgc > 0) view->bgc -= 0.05; else view->bgc = 0; - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + glClearColor(view->bgc, view->bgc, view->bgc, 1); return false; } @@ -674,7 +674,7 @@ bool viewer::m_bgc_dec(viewer * view) bool viewer::m_bgc_white(viewer * view) { view->bgc = 1; - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + glClearColor(view->bgc, view->bgc, view->bgc, 1); return false; } @@ -682,7 +682,7 @@ bool viewer::m_bgc_white(viewer * view) bool viewer::m_bgc_black(viewer * view) { view->bgc = 0; - glClearColor(view->bgc, view->bgc, view->bgc, 1.); + glClearColor(view->bgc, view->bgc, view->bgc, 1); return false; } From a6f98fdbfcd5fa668436ed858dd61fd8bf487625 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Sep 2022 17:00:04 +0200 Subject: [PATCH 0696/1018] viewer: adding materials --- include/gproshan/viewer/che_viewer.h | 7 +++++++ src/gproshan/viewer/che_viewer.cpp | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index d7eab59b..87e451d2 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -23,10 +23,17 @@ namespace gproshan { enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; + +struct material +{ +}; + + class che_viewer { protected: che * mesh = nullptr; + std::vector materials; size_t n_instances = 0; bool center_mesh = false; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 87e073ef..f0d1eea0 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -174,10 +174,15 @@ void che_viewer::draw(shader & program) glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - if(n_instances) - glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); - else - glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); + if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); + else if(materials.size()) + { + for(auto & m: materials) + { + glDrawElementsBaseVertex(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, 0); + } + } + else glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); From 80613529af6da7e9e95802908794cd88ba15e84e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 8 Sep 2022 17:00:19 +0200 Subject: [PATCH 0697/1018] rt: adding scene lights --- src/gproshan/raytracing/rt_embree.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 155b1ffe..9ddabb09 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -309,7 +309,7 @@ vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) L += r.ray.tfar * li(light, position, normal, color, near); - r = ray_hit(r.position(), r.dir()); +// r = ray_hit(r.position(), r.dir()); // if(!intersect(r)) // break; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 89c7f0a1..617b57a3 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -70,6 +70,8 @@ viewer::viewer(const int & width, const int & height) frames = new frame[max_n_meshes]; meshes = new che_viewer[max_n_meshes]; + + render_params.add_light({-1, 1, -2}); } viewer::~viewer() @@ -94,7 +96,7 @@ bool viewer::run() const quaternion & r = cam.current_rotation(); - cam_light = vertex{-1, 1, -2}; + cam_light = render_params.lights[0]; cam_light = r.conj() * cam_light * r; proj_view_mat = camera::perspective(45, real_t(viewport_width) / real_t(viewport_height), 0.01, 1000) * cam.look_at(r); @@ -201,6 +203,15 @@ void viewer::imgui() } } + if(ImGui::CollapsingHeader("Scene Lights")) + { + for(index_t i = 0; i < render_params.n_lights; ++i) + { + ImGui::DragScalarN("", ImGuiDataType_Real, &render_params.lights[i], 3); + } + } + + for(auto & p: processes) { process_t & pro = p.second; @@ -956,14 +967,6 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); vec4 * img = (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); - render_params.n_lights = 0;; - - for(const index_t & v: mesh.selected) - render_params.add_light(mesh->point(v)); - - if(!render_params.n_lights) - render_params.add_light(cam_light); - //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; //render_params.viewport_is_window = false; From 3756c7e6cd7c2a4b516a68571f1f849992579a76 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 9 Sep 2022 17:34:32 +0200 Subject: [PATCH 0698/1018] raytracing: render optix multiline, viewer gui --- include/gproshan/raytracing/render_params.h | 4 +- include/gproshan/raytracing/rt_optix_params.h | 6 +-- src/gproshan/raytracing/rt_optix.cpp | 3 +- src/gproshan/raytracing/rt_optix.cu | 48 ++++++++++--------- src/gproshan/viewer/viewer.cpp | 13 ++++- 5 files changed, 43 insertions(+), 31 deletions(-) diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 943c4f15..09792688 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -10,8 +10,6 @@ namespace gproshan::rt { -const unsigned int max_lights = 16; - struct render_params { int window_width = 0; @@ -29,7 +27,7 @@ struct render_params bool add_light(const vertex & light) { - if(n_lights == max_lights) + if(n_lights == sizeof(lights) / sizeof(vertex)) return false; lights[n_lights++] = light; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index dec88fcc..448b59c9 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -20,11 +20,11 @@ struct launch_params uint32_t window_width, window_height; uint32_t viewport_width, viewport_height; uint32_t viewport_x, viewport_y; - - bool flat; - float light[3]; + uint32_t n_lights = 0; + float lights[3][16]; float cam_pos[3]; float inv_proj_view[16]; + bool flat; OptixTraversableHandle traversable; }; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 5fe8535f..b10cd3fd 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -141,7 +141,8 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) } optix_params.flat = flat; - memcpy(optix_params.light, ¶ms.lights[0], sizeof(optix_params.light)); + optix_params.n_lights = params.n_lights; + memcpy(optix_params.lights, params.lights, sizeof(optix_params.lights)); memcpy(optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); memcpy(optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 4d76e252..1c137994 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -71,33 +71,37 @@ extern "C" __global__ void __closesthit__radiance() const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; - const vertex & light = *(vertex *) optix_params.light; + const vertex * lights = (vertex *) optix_params.lights; const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex position = (1.f - u - v) * A + u * B + v * C; - const vertex wi = normalize(light - position); - const float dot_wi_normal = (wi, normal); - vertex & L = *getPRD(); - L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; - - unsigned int occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-5f, // tmin - 1e20f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); - if(occluded) L = 0.4f * L; + for(int i = 0; i < optix_params.n_lights; ++i) + { + const vertex wi = normalize(lights[i] - position); + const float dot_wi_normal = (wi, normal); + + unsigned int occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-5f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + + L += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * color; + } + + L /= optix_params.n_lights; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 617b57a3..e15a3de0 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -203,14 +203,23 @@ void viewer::imgui() } } + static char slight[32]; if(ImGui::CollapsingHeader("Scene Lights")) { for(index_t i = 0; i < render_params.n_lights; ++i) { - ImGui::DragScalarN("", ImGuiDataType_Real, &render_params.lights[i], 3); + sprintf(slight, "light %d", i); + ImGui::DragScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3); } + + if(ImGui::Button("add light")) + render_params.add_light({0, 0, 0}); + + ImGui::SameLine(); + if(render_params.n_lights > 1 && ImGui::Button("remove light")) + --render_params.n_lights; } - + for(auto & p: processes) { From 02aba31d5ad7b5fe491ff67bd16c5680c7ec3061 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 13 Sep 2022 14:53:54 +0200 Subject: [PATCH 0699/1018] rt_optix: fix light shadows --- src/gproshan/raytracing/rt_optix.cu | 12 +++++++----- src/gproshan/viewer/viewer.cpp | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 1c137994..16f05710 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -79,16 +79,18 @@ extern "C" __global__ void __closesthit__radiance() for(int i = 0; i < optix_params.n_lights; ++i) { - const vertex wi = normalize(lights[i] - position); - const float dot_wi_normal = (wi, normal); + vertex wi = lights[i] - position; + float light_dist = length(wi); + wi /= light_dist; + float dot_wi_normal = (wi, normal); unsigned int occluded = 1; optixTrace( optix_params.traversable, * (float3 *) &position, * (float3 *) &wi, - 1e-5f, // tmin - 1e20f, // tmax - 0.0f, // rayTime + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime OptixVisibilityMask(255), OPTIX_RAY_FLAG_DISABLE_ANYHIT | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e15a3de0..e5aced75 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -204,20 +204,33 @@ void viewer::imgui() } static char slight[32]; + static real_t light_min = -2; + static real_t light_max = 2; if(ImGui::CollapsingHeader("Scene Lights")) { for(index_t i = 0; i < render_params.n_lights; ++i) { sprintf(slight, "light %d", i); - ImGui::DragScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3); + ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &light_min, &light_max); } if(ImGui::Button("add light")) render_params.add_light({0, 0, 0}); + if(render_params.n_lights > 1) + { + ImGui::SameLine(); + if(ImGui::Button("remove light")) + --render_params.n_lights; + } + ImGui::SameLine(); - if(render_params.n_lights > 1 && ImGui::Button("remove light")) - --render_params.n_lights; + if(ImGui::Button("show lights")) + { + sphere_points.clear(); + for(index_t i = 0; i < render_params.n_lights; ++i) + sphere_points.push_back(render_params.lights[i]); + } } From 5cc120df911721c2af5f288d5e03a8d24aae5956 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 15 Sep 2022 11:46:23 +0200 Subject: [PATCH 0700/1018] raytracing: add selected vertices as lights --- src/gproshan/viewer/viewer.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e5aced75..3cddc27c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -208,6 +208,8 @@ void viewer::imgui() static real_t light_max = 2; if(ImGui::CollapsingHeader("Scene Lights")) { + ImGui::Indent(); + for(index_t i = 0; i < render_params.n_lights; ++i) { sprintf(slight, "light %d", i); @@ -231,6 +233,15 @@ void viewer::imgui() for(index_t i = 0; i < render_params.n_lights; ++i) sphere_points.push_back(render_params.lights[i]); } + + if(ImGui::Button("add selected points as lights")) + { + for(const index_t & v: mesh.selected) + if(!render_params.add_light(mesh.model_mat * vec4(mesh->point(v), 1))) + break; + } + + ImGui::Unindent(); } From 3186cff73fbb8b58e2da2b6838c5287920247dc6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 16 Sep 2022 15:56:56 +0200 Subject: [PATCH 0701/1018] raytracing: updating render params --- include/gproshan/geometry/mat.h | 9 ++++ include/gproshan/raytracing/rt_optix_params.h | 18 ++++--- src/gproshan/raytracing/rt_optix.cpp | 4 +- src/gproshan/raytracing/rt_optix.cu | 52 ++++++------------- 4 files changed, 37 insertions(+), 46 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 7b09645a..31184361 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -18,6 +18,7 @@ class mat row rows[N]; public: + __host__ __device__ mat(const std::initializer_list > & list = {}) { int i = -1; @@ -25,28 +26,33 @@ class mat rows[++i] = r; } + __host__ __device__ T & operator () (const index_t & i, const index_t & j) { return rows[i][j]; } + __host__ __device__ const T & operator () (const index_t & i, const index_t & j) const { return rows[i][j]; } + __host__ __device__ row & operator [] (const index_t & i) { assert(i < N); return rows[i]; } + __host__ __device__ const row & operator [] (const index_t & i) const { assert(i < N); return rows[i]; } + __host__ __device__ mat operator * (const mat & b) const { mat res; @@ -57,6 +63,7 @@ class mat return res; } + __host__ __device__ vec operator * (const vec & v) const { vec res; @@ -65,6 +72,7 @@ class mat return res; } + __host__ __device__ static mat identity() { mat res; @@ -73,6 +81,7 @@ class mat return res; } + __host__ __device__ static mat transpose(const mat & m) { mat res; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 448b59c9..8aa1ae57 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -3,6 +3,7 @@ #include +#include #ifdef GPROSHAN_OPTIX @@ -16,14 +17,17 @@ namespace gproshan::rt { struct launch_params { - void * color_buffer = nullptr; - uint32_t window_width, window_height; - uint32_t viewport_width, viewport_height; - uint32_t viewport_x, viewport_y; + vec4 * color_buffer = nullptr; + uint32_t window_width = 0; + uint32_t window_height = 0; + uint32_t viewport_width = 0; + uint32_t viewport_height = 0; + uint32_t viewport_x = 0; + uint32_t viewport_y = 0; uint32_t n_lights = 0; - float lights[3][16]; - float cam_pos[3]; - float inv_proj_view[16]; + vec3 lights[16]; + vec3 cam_pos; + mat4 inv_proj_view; bool flat; OptixTraversableHandle traversable; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index b10cd3fd..1fab938f 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -143,8 +143,8 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) optix_params.flat = flat; optix_params.n_lights = params.n_lights; memcpy(optix_params.lights, params.lights, sizeof(optix_params.lights)); - memcpy(optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); - memcpy(optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); + memcpy(&optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); + memcpy(&optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 16f05710..86476895 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -13,7 +13,7 @@ namespace gproshan::rt { extern "C" __constant__ launch_params optix_params; static __forceinline__ __device__ -void * unpackPointer(uint32_t i0, uint32_t i1) +void * unpack_pointer(uint32_t i0, uint32_t i1) { const uint64_t uptr = static_cast(i0) << 32 | i1; void * ptr = reinterpret_cast(uptr); @@ -21,7 +21,7 @@ void * unpackPointer(uint32_t i0, uint32_t i1) } static __forceinline__ __device__ -void packPointer(void * ptr, uint32_t & i0, uint32_t & i1) +void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) { const uint64_t uptr = reinterpret_cast(ptr); i0 = uptr >> 32; @@ -33,7 +33,7 @@ static __forceinline__ __device__ T * getPRD() { const uint32_t u0 = optixGetPayload_0(); const uint32_t u1 = optixGetPayload_1(); - return reinterpret_cast(unpackPointer(u0, u1)); + return reinterpret_cast(unpack_pointer(u0, u1)); } @@ -71,12 +71,13 @@ extern "C" __global__ void __closesthit__radiance() const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; - const vertex * lights = (vertex *) optix_params.lights; + const vertex * lights = optix_params.lights; const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex position = (1.f - u - v) * A + u * B + v * C; vertex & L = *getPRD(); + L = {0, 0, 0}; for(int i = 0; i < optix_params.n_lights; ++i) { vertex wi = lights[i] - position; @@ -114,7 +115,7 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vertex & prd = *getPRD(); + vec3 & prd = *getPRD(); prd = {0, 0, 0}; } @@ -132,35 +133,18 @@ extern "C" __global__ void __raygen__render_frame() const float sx = (float(ix + optix_params.viewport_x) + .5f) / optix_params.window_width; const float sy = (float(iy + optix_params.viewport_y) + .5f) / optix_params.window_height; - vertex & cam_pos = *(vertex *) optix_params.cam_pos; + vec4 view = {sx * 2 - 1, sy * 2 - 1, 1, 1}; + vec4 q = optix_params.inv_proj_view * view; + vec3 p = q / q[3]; + vec3 ray_dir = normalize(p - optix_params.cam_pos); - vertex ipv[3]; - for(int i = 0; i < 3; ++i) - for(int j = 0; j < 3; ++j) - ipv[i][j] = optix_params.inv_proj_view[i * 4 + j]; - - vertex e = { optix_params.inv_proj_view[0 * 4 + 3], - optix_params.inv_proj_view[1 * 4 + 3], - optix_params.inv_proj_view[2 * 4 + 3] - }; - vertex d = { optix_params.inv_proj_view[3 * 4 + 0], - optix_params.inv_proj_view[3 * 4 + 1], - optix_params.inv_proj_view[3 * 4 + 2] - }; - - float & de = optix_params.inv_proj_view[15]; - - vertex view = {sx * 2 - 1, sy * 2 - 1, 1}; - vertex q = vertex{(ipv[0], view), (ipv[1], view), (ipv[2], view)} + e; - vertex p = (1.f / ((d, view) + de)) * q; - vertex ray_dir = normalize(p - cam_pos); - - vertex pixelColorPRD; + vec4 & pixel_color = optix_params.color_buffer[ix + iy * optix_params.window_width]; uint32_t u0, u1; - packPointer(&pixelColorPRD, u0, u1); + pack_pointer(&pixel_color, u0, u1); + optixTrace( optix_params.traversable, - * (float3 *) &cam_pos, + * (float3 *) &optix_params.cam_pos, * (float3 *) &ray_dir, 0.f, // tmin 1e20f, // tmax @@ -172,13 +156,7 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); - const uint32_t fbIndex = ix + iy * optix_params.viewport_width; - - float4 * frame = (float4 *) optix_params.color_buffer; - frame[fbIndex].x = pixelColorPRD.x(); - frame[fbIndex].y = pixelColorPRD.y(); - frame[fbIndex].z = pixelColorPRD.z(); - frame[fbIndex].w = 1; + pixel_color[3] = 1; } From cd9e9e2b0d3f857a93d8313c093bf88468f2e18e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 18 Sep 2022 20:37:42 +0200 Subject: [PATCH 0702/1018] rt: adding rt_utils --- include/gproshan/raytracing/raytracing.h | 7 +---- include/gproshan/raytracing/rt_optix_params.h | 14 ++++----- include/gproshan/raytracing/rt_utils.h | 31 +++++++++++++++++++ src/gproshan/raytracing/raytracing.cpp | 15 ++------- src/gproshan/raytracing/rt_optix.cu | 13 ++++---- src/gproshan/viewer/che_viewer.cpp | 2 +- 6 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 include/gproshan/raytracing/rt_utils.h diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 4053e13e..31c3ed82 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -56,12 +57,6 @@ class raytracing const index_t & samples = 4 ); - vertex ray_view_dir( const index_t & x, const index_t & y, - const ivec2 & windows_size, - const mat4 & inv_proj_view, - const vertex & cam_pos - ); - virtual hit intersect( const vertex &, // org const vertex & //dir ) { return hit(); } diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 8aa1ae57..453b6fd4 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -18,13 +18,13 @@ namespace gproshan::rt { struct launch_params { vec4 * color_buffer = nullptr; - uint32_t window_width = 0; - uint32_t window_height = 0; - uint32_t viewport_width = 0; - uint32_t viewport_height = 0; - uint32_t viewport_x = 0; - uint32_t viewport_y = 0; - uint32_t n_lights = 0; + int32_t window_width = 0; + int32_t window_height = 0; + int32_t viewport_width = 0; + int32_t viewport_height = 0; + int32_t viewport_x = 0; + int32_t viewport_y = 0; + int32_t n_lights = 0; vec3 lights[16]; vec3 cam_pos; mat4 inv_proj_view; diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h new file mode 100644 index 00000000..5a2db893 --- /dev/null +++ b/include/gproshan/raytracing/rt_utils.h @@ -0,0 +1,31 @@ +#ifndef RT_UTILS_H +#define RT_UTILS_H + +#include +#include + + +// geometry processing and shape analysis framework +// raytracing approach +namespace gproshan::rt { + + +template +__host__ __device__ +vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) +{ + vec2 screen = { (float(pos.x()) + 0.5f) / windows_size.x(), + (float(pos.y()) + 0.5f) / windows_size.y() + }; + vec view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1}; + vec q = inv_proj_view * view; + vec p = q / q[3]; + + return normalize(p - cam_pos); +} + + +} // namespace gproshan + +#endif // RT_UTILS_H + diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index ee33a47d..34f0b879 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -31,8 +31,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f { //row major vec4 & color = img[j * params.viewport_width + i]; - const vertex & dir = ray_view_dir( i + params.viewport_x, - j + params.viewport_y, + const vertex & dir = ray_view_dir( {i + params.viewport_x, j + params.viewport_y}, {window_width, window_height}, params.inv_proj_view, params.cam_pos @@ -61,7 +60,7 @@ float * raytracing::raycaster( const ivec2 & windows_size, { //row major float & color = frame[(windows_size.y() - j - 1) * windows_size.x() + i] = 0; - vertex dir = ray_view_dir(i, j, windows_size, inv_proj_view, cam_pos); + vertex dir = ray_view_dir({i, j}, windows_size, inv_proj_view, cam_pos); for(index_t s = 0; s < samples; ++s) color += intersect_depth(cam_pos, dir); @@ -72,16 +71,6 @@ float * raytracing::raycaster( const ivec2 & windows_size, return frame; } -vertex raytracing::ray_view_dir(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos) -{ - vec2 screen = vec2((float(x) + randf(gen)) / windows_size.x(), (float(y) + randf(gen)) / windows_size.y()); - vec4 view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1}; - vec4 q = inv_proj_view * view; - vertex p = q / q[3]; - - return normalize(p - cam_pos); -} - } // namespace gproshan diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 86476895..27fa6fac 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -1,4 +1,5 @@ #include +#include #include @@ -130,13 +131,11 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const float sx = (float(ix + optix_params.viewport_x) + .5f) / optix_params.window_width; - const float sy = (float(iy + optix_params.viewport_y) + .5f) / optix_params.window_height; - - vec4 view = {sx * 2 - 1, sy * 2 - 1, 1, 1}; - vec4 q = optix_params.inv_proj_view * view; - vec3 p = q / q[3]; - vec3 ray_dir = normalize(p - optix_params.cam_pos); + const vec3 ray_dir = ray_view_dir( {ix + optix_params.viewport_x, iy + optix_params.viewport_y}, + {optix_params.window_width, optix_params.window_height}, + optix_params.inv_proj_view, + optix_params.cam_pos + ); vec4 & pixel_color = optix_params.color_buffer[ix + iy * optix_params.window_width]; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index f0d1eea0..b255c7fb 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -232,7 +232,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) void che_viewer::select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - const vertex & dir = rt_embree->ray_view_dir(x, windows_size.y() - y, windows_size, inv_proj_view_mat, cam_pos); + const vertex & dir = rt::ray_view_dir({x, windows_size.y() - y}, windows_size, inv_proj_view_mat, cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); From 5106c53ec0c0ba915005dd3fdcb80427a1367a21 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 20 Sep 2022 12:30:10 +0200 Subject: [PATCH 0703/1018] rt_utils: fixing signed/unsigned warnings --- include/gproshan/raytracing/raytracing.h | 4 ---- include/gproshan/viewer/che_viewer.h | 2 +- src/gproshan/raytracing/raytracing.cpp | 3 --- src/gproshan/viewer/che_viewer.cpp | 4 ++-- src/gproshan/viewer/viewer.cpp | 2 +- 5 files changed, 4 insertions(+), 11 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 31c3ed82..f120f9ea 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -7,7 +7,6 @@ #include #include -#include // geometry processing and shape analysis framework @@ -42,9 +41,6 @@ class raytracing size_t n_samples = 0; - static std::default_random_engine gen; - static std::uniform_real_distribution randf; - public: raytracing() = default; virtual ~raytracing() = default; diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 87e451d2..2f2178ce 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -84,7 +84,7 @@ class che_viewer void draw_point_cloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); - void select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); + void select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 34f0b879..f7c7bb5f 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -8,9 +8,6 @@ namespace gproshan::rt { -std::default_random_engine raytracing::gen; -std::uniform_real_distribution raytracing::randf; - void raytracing::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) n_samples = 0; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index b255c7fb..dfc9abae 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -230,9 +230,9 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) } } -void che_viewer::select(const index_t & x, const index_t & y, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) +void che_viewer::select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - const vertex & dir = rt::ray_view_dir({x, windows_size.y() - y}, windows_size, inv_proj_view_mat, cam_pos); + const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 3cddc27c..264e7c43 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -1018,7 +1018,7 @@ void viewer::pick_vertex(const int & x, const int & y) { che_viewer & mesh = active_mesh(); - mesh.select(x, y, {viewport_width, viewport_height}, inverse(proj_view_mat), cam.eye); + mesh.select({x, y}, {viewport_width, viewport_height}, inverse(proj_view_mat), cam.eye); } void viewer::check_apply_all_meshes(const std::function & fun) From 476abda2bbb736aa421b32bff3e2dd604860faaf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 20 Sep 2022 14:51:05 +0200 Subject: [PATCH 0704/1018] rt_embree: fixing light rays --- include/gproshan/raytracing/rt_embree.h | 2 +- include/gproshan/raytracing/rt_utils.h | 27 ++++++++++++++ src/gproshan/raytracing/rt_embree.cpp | 48 +++++++++---------------- src/gproshan/raytracing/rt_optix.cu | 17 ++++----- 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 9e9ac1ab..6d752eeb 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -67,7 +67,7 @@ class embree : public raytracing virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); - vec4 li(ray_hit r, const vertex & light, const bool & flat); + vec4 li(const ray_hit & r, const vertex & light, const bool & flat); vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 5a2db893..01b7a289 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -10,6 +10,33 @@ namespace gproshan::rt { +template +struct random +{ + uint32_t previous; + + __host__ __device__ + random(uint32_t v0, uint32_t v1) + { + uint32_t s = 0; + for(uint32_t i = 0; i < N; ++i) + { + s += 0x9e3779b9; + v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s) ^ ((v1 >> 5) + 0xc8013ea4); + v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s) ^ ((v0 >> 5) + 0x7e95761e); + } + previous = v0; + } + + __host__ __device__ + T operator () () + { + previous = previous * 1664525 + 1013904223; + return T(previous & 0x00FFFFFF) / T(0x01000000); + } +}; + + template __host__ __device__ vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 9ddabb09..79b8999b 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -279,45 +279,31 @@ float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, vec4 embree::li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near) { - const vertex wi = normalize(light - position); - const float dot_wi_normal = (wi, normal); - const vertex L = (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * color; + vertex wi = light - position; + float light_dist = length(wi); + wi /= light_dist; + float dot_wi_normal = (wi, normal); - ray_hit r(position, wi, near); - return {(occluded(r) ? 0.4f : 1.f) * L, 1}; + ray_hit r(position, wi, 1e-3f, light_dist - 1e-3f); + return (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(r) ? 0.4f : 1.0f) * color; } -vec4 embree::li(ray_hit r, const vertex & light, const bool & flat) +vec4 embree::li(const ray_hit & r, const vertex & light, const bool & flat) { - float total_tfar = 0; + const vertex & position = r.position(); + const vertex & normal = r.normal(geomID_mesh[r.hit.geomID], flat); + const vertex & color = r.color(geomID_mesh[r.hit.geomID]); - float near; - vertex position, normal, color; + vertex wi = light - position; + float light_dist = length(wi); + wi /= light_dist; + float dot_wi_normal = (wi, normal); - vec4 L(0); -// while(total_tfar < 0.1) - { - total_tfar += r.ray.tfar; - - position = r.position(); - normal = r.normal(geomID_mesh[r.hit.geomID], flat); - color = r.color(geomID_mesh[r.hit.geomID]); - - near = 1e-5f; - if(geomID_mesh[r.hit.geomID].pointcloud) - near += pointcloud_hit(position, normal, color, r); - - L += r.ray.tfar * li(light, position, normal, color, near); - -// r = ray_hit(r.position(), r.dir()); -// if(!intersect(r)) -// break; - } - - return L / total_tfar; + ray_hit rs(position, wi, 1e-3f, light_dist - 1e-3f); + return (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(rs) ? 0.4f : 1.0f) * vec4{color, 1}; } -vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light,const bool & flat) +vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat) { ray_hit r(org, dir); return intersect(r) ? li(r, light, flat) : vec4(0.f); diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 27fa6fac..0cd1059e 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -16,25 +16,22 @@ extern "C" __constant__ launch_params optix_params; static __forceinline__ __device__ void * unpack_pointer(uint32_t i0, uint32_t i1) { - const uint64_t uptr = static_cast(i0) << 32 | i1; - void * ptr = reinterpret_cast(uptr); - return ptr; + return (void *) (uint64_t(i0) << 32 | i1); } static __forceinline__ __device__ void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) { - const uint64_t uptr = reinterpret_cast(ptr); + const uint64_t uptr = uint64_t(ptr); i0 = uptr >> 32; i1 = uptr & 0x00000000ffffffff; } template -static __forceinline__ __device__ T * getPRD() +static __forceinline__ __device__ +T * ray_data() { - const uint32_t u0 = optixGetPayload_0(); - const uint32_t u1 = optixGetPayload_1(); - return reinterpret_cast(unpack_pointer(u0, u1)); + return (T *) unpack_pointer(optixGetPayload_0(), optixGetPayload_1()); } @@ -76,7 +73,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex position = (1.f - u - v) * A + u * B + v * C; - vertex & L = *getPRD(); + vertex & L = *ray_data(); L = {0, 0, 0}; for(int i = 0; i < optix_params.n_lights; ++i) @@ -116,7 +113,7 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vec3 & prd = *getPRD(); + vec3 & prd = *ray_data(); prd = {0, 0, 0}; } From 4c8dee4b58126c55c6f6b17a3f495e9bbbdb4cb0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 20 Sep 2022 18:09:04 +0200 Subject: [PATCH 0705/1018] rt_optix: updating closesthit radiance --- include/gproshan/raytracing/rt_utils.h | 2 +- src/gproshan/raytracing/rt_optix.cu | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 01b7a289..46b87acd 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -14,7 +14,7 @@ template struct random { uint32_t previous; - + __host__ __device__ random(uint32_t v0, uint32_t v1) { diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 0cd1059e..e459288a 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -62,8 +62,8 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; - const vertex Ng = normalize((B - A) * (C - A)); - const vertex normal = optix_params.flat ? Ng : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + const vertex normal = optix_params.flat ? normalize((B - A) * (C - A)) + : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; @@ -113,8 +113,8 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vec3 & prd = *ray_data(); - prd = {0, 0, 0}; + vec3 & pixel_color = *ray_data(); + pixel_color = {0, 0, 0}; } extern "C" __global__ void __miss__shadow() From 570d89eedc0ef489b8b356cd32af19f38bba8ea5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 23 Sep 2022 17:43:35 +0200 Subject: [PATCH 0706/1018] rt_optix: checking size color buffer in render method --- include/gproshan/raytracing/rt_optix.h | 2 +- src/gproshan/raytracing/rt_optix.cpp | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index da22cc34..adc79045 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -40,7 +40,7 @@ class optix : public raytracing OptixShaderBindingTable sbt = {}; launch_params optix_params; - void * launch_params_buffer = nullptr; + launch_params * optix_params_buffer = nullptr; std::vector dd_mesh; std::vector d_mesh; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 1fab938f..cb74621e 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -98,13 +98,13 @@ optix::optix(const std::vector & meshes, const std::vector & model_ build_sbt(); // launch params - cudaMalloc(&launch_params_buffer, sizeof(launch_params)); + cudaMalloc(&optix_params_buffer, sizeof(launch_params)); } optix::~optix() { cudaFree(optix_params.color_buffer); - cudaFree(launch_params_buffer); + cudaFree(optix_params_buffer); cudaFree(raygen_records_buffer); cudaFree(miss_records_buffer); cudaFree(hitgroup_records_buffer); @@ -120,8 +120,12 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) { n_samples = 0; - if(optix_params.color_buffer) - cudaFree(optix_params.color_buffer); + if(optix_params.viewport_width * optix_params.viewport_height < params.viewport_width * params.viewport_height) + { + if(optix_params.color_buffer) + cudaFree(optix_params.color_buffer); + cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4)); + } optix_params.window_width = params.window_width; optix_params.window_height = params.window_height; @@ -136,8 +140,6 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) optix_params.viewport_x = params.viewport_x; optix_params.viewport_y = params.viewport_y; - - cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4)); } optix_params.flat = flat; @@ -146,11 +148,11 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) memcpy(&optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); memcpy(&optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); - cudaMemcpy(launch_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); optixLaunch(optix_pipeline, stream, - (CUdeviceptr) launch_params_buffer, + (CUdeviceptr) optix_params_buffer, sizeof(launch_params), &sbt, optix_params.viewport_width, From 6b267547fd81454d2666c23bd079d891f1752bd2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 27 Sep 2022 16:28:42 +0200 Subject: [PATCH 0707/1018] rt_optix: fix access to color_buffer --- include/gproshan/raytracing/render_params.h | 22 +++++++-- include/gproshan/raytracing/rt_optix_params.h | 13 +++--- src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/raytracing/rt_optix.cpp | 46 +++++++++---------- src/gproshan/raytracing/rt_optix.cu | 2 +- src/gproshan/viewer/viewer.cpp | 4 +- 6 files changed, 49 insertions(+), 40 deletions(-) diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 09792688..f3c30b8b 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -18,12 +18,12 @@ struct render_params int viewport_height = 0; int viewport_x = 0; int viewport_y = 0; - bool restart = false; - bool viewport_is_window = true; - vertex cam_pos; + int n_lights = 0; vertex lights[16]; - unsigned int n_lights = 0; + vertex cam_pos; mat4 inv_proj_view; + bool restart = false; + bool viewport_is_window = true; bool add_light(const vertex & light) { @@ -33,6 +33,20 @@ struct render_params lights[n_lights++] = light; return true; } + + void log() + { + gproshan_log_var(window_width); + gproshan_log_var(window_height); + gproshan_log_var(viewport_width); + gproshan_log_var(viewport_height); + gproshan_log_var(viewport_x); + gproshan_log_var(viewport_y); + gproshan_log_var(n_lights); + gproshan_log_var(cam_pos); + gproshan_log_var(restart); + gproshan_log_var(viewport_is_window); + } }; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 453b6fd4..903f6fc1 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -18,13 +18,12 @@ namespace gproshan::rt { struct launch_params { vec4 * color_buffer = nullptr; - int32_t window_width = 0; - int32_t window_height = 0; - int32_t viewport_width = 0; - int32_t viewport_height = 0; - int32_t viewport_x = 0; - int32_t viewport_y = 0; - int32_t n_lights = 0; + int buffer_size = 0; + int window_width = 0; + int window_height = 0; + int viewport_x = 0; + int viewport_y = 0; + int n_lights = 0; vec3 lights[16]; vec3 cam_pos; mat4 inv_proj_view; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index f7c7bb5f..41eb98ea 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -35,7 +35,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f ); li = vec4(0); - for(index_t l = 0; l < params.n_lights; ++l) + for(int l = 0; l < params.n_lights; ++l) li += intersect_li(params.cam_pos, dir, params.lights[l], flat); color = (color * float(n_samples) + li / float(params.n_lights)) / float(n_samples + 1); diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index cb74621e..1aa067ac 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -116,32 +116,28 @@ optix::~optix() void optix::render(vec4 * img, const render_params & params, const bool & flat) { - if(params.restart) + if(params.restart) n_samples = 0; + + if(optix_params.buffer_size < params.viewport_width * params.viewport_height) + { + optix_params.buffer_size = params.viewport_width * params.viewport_height; + + if(optix_params.color_buffer) + cudaFree(optix_params.color_buffer); + cudaMalloc(&optix_params.color_buffer, optix_params.buffer_size * sizeof(vec4)); + } + + optix_params.window_width = params.window_width; + optix_params.window_height = params.window_height; + if(params.viewport_is_window) { - n_samples = 0; - - if(optix_params.viewport_width * optix_params.viewport_height < params.viewport_width * params.viewport_height) - { - if(optix_params.color_buffer) - cudaFree(optix_params.color_buffer); - cudaMalloc(&optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4)); - } - - optix_params.window_width = params.window_width; - optix_params.window_height = params.window_height; - if(params.viewport_is_window) - { - optix_params.window_width = params.viewport_width; - optix_params.window_height = params.viewport_height; - } - - optix_params.viewport_width = params.viewport_width; - optix_params.viewport_height = params.viewport_height; - - optix_params.viewport_x = params.viewport_x; - optix_params.viewport_y = params.viewport_y; + optix_params.window_width = params.viewport_width; + optix_params.window_height = params.viewport_height; } + optix_params.viewport_x = params.viewport_x; + optix_params.viewport_y = params.viewport_y; + optix_params.flat = flat; optix_params.n_lights = params.n_lights; memcpy(optix_params.lights, params.lights, sizeof(optix_params.lights)); @@ -155,8 +151,8 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) (CUdeviceptr) optix_params_buffer, sizeof(launch_params), &sbt, - optix_params.viewport_width, - optix_params.viewport_height, + params.viewport_width, + params.viewport_height, 1 ); diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index e459288a..e78da0ee 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -134,7 +134,7 @@ extern "C" __global__ void __raygen__render_frame() optix_params.cam_pos ); - vec4 & pixel_color = optix_params.color_buffer[ix + iy * optix_params.window_width]; + vec4 & pixel_color = optix_params.color_buffer[ix + iy * optixGetLaunchDimensions().x]; uint32_t u0, u1; pack_pointer(&pixel_color, u0, u1); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 264e7c43..0ab43f13 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -210,7 +210,7 @@ void viewer::imgui() { ImGui::Indent(); - for(index_t i = 0; i < render_params.n_lights; ++i) + for(int i = 0; i < render_params.n_lights; ++i) { sprintf(slight, "light %d", i); ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &light_min, &light_max); @@ -230,7 +230,7 @@ void viewer::imgui() if(ImGui::Button("show lights")) { sphere_points.clear(); - for(index_t i = 0; i < render_params.n_lights; ++i) + for(int i = 0; i < render_params.n_lights; ++i) sphere_points.push_back(render_params.lights[i]); } From 305f35974bf92744adec54ad2af69faf41790bfc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 29 Sep 2022 16:59:53 +0200 Subject: [PATCH 0708/1018] update CMakeLists.txt: OptiX 7.5, compile ptx with --optix-ir --- CMakeLists.txt | 2 +- src/gproshan/CMakeLists.txt | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f09a475f..8361b25a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) - find_package(OptiX 7.4) + find_package(OptiX 7.5) if(OptiX_INCLUDE) include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index d02b165a..655807ba 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -33,15 +33,14 @@ endif(CUDAToolkit_FOUND) if(OptiX_INCLUDE) - find_package(CUDA) - cuda_compile_ptx(ptx_sources ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) - list(GET ptx_sources 0 ptx_file) - add_custom_target( ptx_tmp DEPENDS ${ptx_file} - COMMAND ${CMAKE_COMMAND} -E copy - ${ptx_file} - ${gproshan_SOURCE_DIR}/src/rt_optix.ptx + add_custom_target( optix_ptx ALL + COMMAND ${CMAKE_CUDA_COMPILER} --optix-ir + -isystem ${OptiX_INCLUDE} + -isystem ${gproshan_SOURCE_DIR}/include + -o ${gproshan_SOURCE_DIR}/src/rt_optix.ptx + ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu + DEPENDS ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu ) - add_dependencies(gproshan ptx_tmp) endif(OptiX_INCLUDE) From 17671b299c896aa3eff783e9dc38a37ab0bceaf7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 11 Oct 2022 15:28:09 +0200 Subject: [PATCH 0709/1018] rt_optix: added cuda graphics interoperability --- include/gproshan/viewer/frame.h | 9 ++++++- src/gproshan/CMakeLists.txt | 2 +- src/gproshan/raytracing/rt_optix.cpp | 11 +-------- src/gproshan/viewer/frame.cpp | 37 +++++++++++++++++++++++++--- src/gproshan/viewer/viewer.cpp | 9 ++----- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index d6186742..87c51d79 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -2,10 +2,12 @@ #define FRAME_H +#include #include - #include +#include +#include // geometry processing and shape analysis framework namespace gproshan { @@ -24,11 +26,16 @@ class frame shader program; + cudaGraphicsResource * pbo_cuda; + public: frame(); ~frame(); operator const GLuint & () const; + + vec4 * map_pbo(bool cuda = false); + void unmap_pbo(bool cuda = false); bool resize(const size_t & w, const size_t & h); void display(); diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 655807ba..14ce8e80 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -34,7 +34,7 @@ endif(CUDAToolkit_FOUND) if(OptiX_INCLUDE) add_custom_target( optix_ptx ALL - COMMAND ${CMAKE_CUDA_COMPILER} --optix-ir + COMMAND ${CMAKE_CUDA_COMPILER} --optix-ir --use_fast_math -isystem ${OptiX_INCLUDE} -isystem ${gproshan_SOURCE_DIR}/include -o ${gproshan_SOURCE_DIR}/src/rt_optix.ptx diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 1aa067ac..68acdf91 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -118,14 +118,7 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) n_samples = 0; - if(optix_params.buffer_size < params.viewport_width * params.viewport_height) - { - optix_params.buffer_size = params.viewport_width * params.viewport_height; - - if(optix_params.color_buffer) - cudaFree(optix_params.color_buffer); - cudaMalloc(&optix_params.color_buffer, optix_params.buffer_size * sizeof(vec4)); - } + optix_params.color_buffer = img; optix_params.window_width = params.window_width; optix_params.window_height = params.window_height; @@ -157,8 +150,6 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) ); cudaDeviceSynchronize(); - - cudaMemcpy(img, optix_params.color_buffer, params.viewport_width * params.viewport_height * sizeof(vec4), cudaMemcpyDeviceToHost); } void optix::create_raygen_programs() diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index a1ceb2d3..55c9abd8 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -38,6 +38,7 @@ frame::frame() glGenBuffers(1, &pbo); + glGenTextures(1, &render_tex); glBindTexture(GL_TEXTURE_2D, render_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -49,6 +50,9 @@ frame::frame() frame::~frame() { + if(width && height) + cudaGraphicsUnregisterResource(pbo_cuda); + glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); glDeleteTextures(1, &render_tex); @@ -60,6 +64,33 @@ frame::operator const GLuint & () const return pbo; } +vec4 * frame::map_pbo(bool cuda) +{ + if(cuda) + { + vec4 * img = nullptr; + size_t num_bytes = 0; + cudaGraphicsMapResources(1, &pbo_cuda, 0); + cudaGraphicsResourceGetMappedPointer((void **) &img, &num_bytes, pbo_cuda); + return img; + } + + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + return (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); +} + +void frame::unmap_pbo(bool cuda) +{ + if(cuda) + { + cudaGraphicsUnmapResources(1, &pbo_cuda, 0); + return; + } + + glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); +} + bool frame::resize(const size_t & w, const size_t & h) { if(w == width && height == h) @@ -67,9 +98,12 @@ bool frame::resize(const size_t & w, const size_t & h) if(w * h > width * height) { + if(width && height) + cudaGraphicsUnregisterResource(pbo_cuda); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(float) * w * h, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + cudaGraphicsGLRegisterBuffer(&pbo_cuda, pbo, cudaGraphicsMapFlagsNone); } width = w; @@ -82,9 +116,6 @@ void frame::display() { program.enable(); - gproshan_debug_var(width); - gproshan_debug_var(height); - glBindVertexArray(vao); glActiveTexture(GL_TEXTURE0); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 0ab43f13..58acec9e 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -997,19 +997,14 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) render_params.restart = rt_frame.resize(viewport_width, viewport_height) || render_params.restart; - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rt_frame); - vec4 * img = (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); - //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; //render_params.viewport_is_window = false; render_params.inv_proj_view = inverse(proj_view_mat); render_params.cam_pos = cam.eye; - rt->render(img, render_params, mesh.render_flat); - - glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + rt->render(rt_frame.map_pbo(mesh.render_opt == R_OPTIX), render_params, mesh.render_flat); + rt_frame.unmap_pbo(mesh.render_opt == R_OPTIX); rt_frame.display(); } From e1ef647b4688629a73f03a64dbfdab4d83f19043 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 11 Oct 2022 16:01:12 +0200 Subject: [PATCH 0710/1018] rt_optix: fix graphics intereoperability cuda not found --- include/gproshan/viewer/frame.h | 11 ++++++++--- src/gproshan/viewer/frame.cpp | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index 87c51d79..d4c33397 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -6,8 +6,11 @@ #include #include -#include -#include +#ifdef GPROSHAN_CUDA + #include + #include +#endif // GPROSHAN_CUDA + // geometry processing and shape analysis framework namespace gproshan { @@ -26,7 +29,9 @@ class frame shader program; - cudaGraphicsResource * pbo_cuda; + #ifdef GPROSHAN_CUDA + cudaGraphicsResource_t pbo_cuda; + #endif // GPROSHAN_CUDA public: frame(); diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index 55c9abd8..dc6b75cd 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -35,10 +35,8 @@ frame::frame() glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - glGenBuffers(1, &pbo); - glGenTextures(1, &render_tex); glBindTexture(GL_TEXTURE_2D, render_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -50,8 +48,10 @@ frame::frame() frame::~frame() { +#ifdef GPROSHAN_CUDA if(width && height) cudaGraphicsUnregisterResource(pbo_cuda); +#endif // GPROSHAN_CUDA glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); @@ -66,6 +66,7 @@ frame::operator const GLuint & () const vec4 * frame::map_pbo(bool cuda) { +#ifdef GPROSHAN_CUDA if(cuda) { vec4 * img = nullptr; @@ -74,6 +75,7 @@ vec4 * frame::map_pbo(bool cuda) cudaGraphicsResourceGetMappedPointer((void **) &img, &num_bytes, pbo_cuda); return img; } +#endif // GPROSHAN_CUDA glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); return (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); @@ -81,11 +83,13 @@ vec4 * frame::map_pbo(bool cuda) void frame::unmap_pbo(bool cuda) { +#ifdef GPROSHAN_CUDA if(cuda) { cudaGraphicsUnmapResources(1, &pbo_cuda, 0); return; } +#endif // GPROSHAN_CUDA glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); @@ -98,12 +102,14 @@ bool frame::resize(const size_t & w, const size_t & h) if(w * h > width * height) { - if(width && height) - cudaGraphicsUnregisterResource(pbo_cuda); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); glBufferData(GL_PIXEL_UNPACK_BUFFER, 4 * sizeof(float) * w * h, 0, GL_DYNAMIC_DRAW); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + #ifdef GPROSHAN_CUDA + if(width && height) + cudaGraphicsUnregisterResource(pbo_cuda); cudaGraphicsGLRegisterBuffer(&pbo_cuda, pbo, cudaGraphicsMapFlagsNone); + #endif // GPROSHAN_CUDA } width = w; From 63090ca2c2de7244158ce57f2f9f3d90b76f257b Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Tue, 11 Oct 2022 16:36:00 +0200 Subject: [PATCH 0711/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index cc71ed34..1a80ca35 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,7 +18,7 @@ jobs: cuda: [cuda, ''] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install Dependencies run: | From d1f192ab6d06b98e16bc261ac2f17a5a35e24958 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 12 Oct 2022 12:53:48 +0200 Subject: [PATCH 0712/1018] rt_optix: add accumulation n_samples per frame --- include/gproshan/raytracing/rt_optix_params.h | 1 + src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/raytracing/rt_optix.cpp | 5 ++++- src/gproshan/raytracing/rt_optix.cu | 9 ++++----- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 903f6fc1..5a8f4b65 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -23,6 +23,7 @@ struct launch_params int window_height = 0; int viewport_x = 0; int viewport_y = 0; + int n_samples = 0; int n_lights = 0; vec3 lights[16]; vec3 cam_pos; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 41eb98ea..bddc01e2 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -38,7 +38,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f for(int l = 0; l < params.n_lights; ++l) li += intersect_li(params.cam_pos, dir, params.lights[l], flat); - color = (color * float(n_samples) + li / float(params.n_lights)) / float(n_samples + 1); + color = (color * n_samples + li / params.n_lights) / (n_samples + 1); } ++n_samples; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 68acdf91..3eb6e258 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -117,7 +117,8 @@ optix::~optix() void optix::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) n_samples = 0; - + + optix_params.n_samples = n_samples; optix_params.color_buffer = img; optix_params.window_width = params.window_width; @@ -150,6 +151,8 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) ); cudaDeviceSynchronize(); + + ++n_samples; } void optix::create_raygen_programs() diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index e78da0ee..5ecd7b1d 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -73,9 +73,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; const vertex position = (1.f - u - v) * A + u * B + v * C; - vertex & L = *ray_data(); - - L = {0, 0, 0}; + vertex li; for(int i = 0; i < optix_params.n_lights; ++i) { vertex wi = lights[i] - position; @@ -99,10 +97,11 @@ extern "C" __global__ void __closesthit__radiance() 1, // missSBTIndex occluded); - L += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * color; + li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * color; } - L /= optix_params.n_lights; + vertex & pixel_color = *ray_data(); + pixel_color = (pixel_color * optix_params.n_samples + li / optix_params.n_lights) / (optix_params.n_samples + 1); } From 9548ab491578d53a424e0179efd2457b5889c4b0 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Sat, 15 Oct 2022 00:07:37 +0200 Subject: [PATCH 0713/1018] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 375138b0..ec45a044 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework -[![Build](https://github.com/larc/gproshan/actions/workflows/build.yml/badge.svg?branch=alpha)](https://github.com/larc/gproshan/actions/workflows/build.yml) +[![Build](https://github.com/larc/gproshan_dev/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/larc/gproshan_dev/actions/workflows/build.yml) [![DOI](https://zenodo.org/badge/88686093.svg)](https://zenodo.org/badge/latestdoi/88686093) From c3c8f01dd2e8249b86142622fe6703f218ea01e2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 16 Oct 2022 21:56:09 +0200 Subject: [PATCH 0714/1018] rt_embree: fix lighting --- include/gproshan/raytracing/rt_embree.h | 1 - include/gproshan/viewer/frame.h | 2 +- src/gproshan/raytracing/raytracing.cpp | 1 + src/gproshan/raytracing/rt_embree.cpp | 11 ----------- src/gproshan/raytracing/rt_optix.cpp | 2 +- 5 files changed, 3 insertions(+), 14 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 6d752eeb..a3b91b7f 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -66,7 +66,6 @@ class embree : public raytracing virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); - vec4 li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near = 1e-5f); vec4 li(const ray_hit & r, const vertex & light, const bool & flat); vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index d4c33397..c42ac978 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -38,7 +38,7 @@ class frame ~frame(); operator const GLuint & () const; - + vec4 * map_pbo(bool cuda = false); void unmap_pbo(bool cuda = false); diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index bddc01e2..89a61dd1 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -39,6 +39,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f li += intersect_li(params.cam_pos, dir, params.lights[l], flat); color = (color * n_samples + li / params.n_lights) / (n_samples + 1); + color[3] = 1; } ++n_samples; diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 79b8999b..e5306859 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -277,17 +277,6 @@ float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, return sum_w; } -vec4 embree::li(const vertex & light, const vertex & position, const vertex & normal, const vertex & color, const float & near) -{ - vertex wi = light - position; - float light_dist = length(wi); - wi /= light_dist; - float dot_wi_normal = (wi, normal); - - ray_hit r(position, wi, 1e-3f, light_dist - 1e-3f); - return (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(r) ? 0.4f : 1.0f) * color; -} - vec4 embree::li(const ray_hit & r, const vertex & light, const bool & flat) { const vertex & position = r.position(); diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 3eb6e258..bb40b4f2 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -117,7 +117,7 @@ optix::~optix() void optix::render(vec4 * img, const render_params & params, const bool & flat) { if(params.restart) n_samples = 0; - + optix_params.n_samples = n_samples; optix_params.color_buffer = img; From 20d2b21d06c1d6836fc1d5cb944572dca1e644e8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 16 Oct 2022 23:07:12 +0200 Subject: [PATCH 0715/1018] rt_embree: clean up extra code --- include/gproshan/raytracing/rt_embree.h | 5 ++--- src/gproshan/raytracing/rt_embree.cpp | 25 ------------------------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index a3b91b7f..c372e129 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -45,7 +45,7 @@ class embree : public raytracing embree( const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud = false, - const float & pcr = 1 + const float & pcr = 0.01 ); virtual ~embree(); @@ -64,9 +64,8 @@ class embree : public raytracing index_t add_mesh(const che * mesh, const mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual float pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r); - vec4 li(const ray_hit & r, const vertex & light, const bool & flat); + virtual vec4 li(const ray_hit & r, const vertex & light, const bool & flat); vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index e5306859..70c774fd 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -252,31 +252,6 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -float embree::pointcloud_hit(vertex & position, vertex & normal, vertex & color, ray_hit r) -{ - float w, sum_w = 0; - position = normal = color = {0, 0, 0}; - - do - { - vec4 * xyzr = (vec4 *) rtcGetGeometryBufferData(rtcGetGeometry(scene, r.hit.geomID), RTC_BUFFER_TYPE_VERTEX, 0); - - sum_w += w = pc_radius - length(r.position() - vertex(xyzr[r.hit.primID])); - position += w * r.position(); - normal += w * r.normal(geomID_mesh[r.hit.geomID]); - color += w * r.color(geomID_mesh[r.hit.geomID]); - - r = ray_hit(r.position(), r.dir()); - } - while(intersect(r) && r.ray.tfar < pc_radius); - - position /= sum_w; - normal /= sum_w; - color /= sum_w; - - return sum_w; -} - vec4 embree::li(const ray_hit & r, const vertex & light, const bool & flat) { const vertex & position = r.position(); From cc9f9c8a57e16455e0e88289f18fb8b029528f11 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 09:04:28 +0200 Subject: [PATCH 0716/1018] raytracing: generalizing li evaluation --- include/gproshan/raytracing/rt_utils.h | 39 ++++++++++++++++++++++++++ src/gproshan/mesh/che.cu | 4 +-- src/gproshan/raytracing/rt_optix.cu | 25 +++++------------ 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 46b87acd..f8bd90ae 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -4,6 +4,8 @@ #include #include +#include + // geometry processing and shape analysis framework // raytracing approach @@ -37,6 +39,43 @@ struct random }; +template +struct eval_hit +{ + float u, v; + vec position; + vec normal; + vec color; + + __host__ __device__ + eval_hit(const CHE & mesh, const unsigned int & primID, const float & pu, const float & pv) + { + u = pu; + v = pv; + + const int he = primID * che::mtrig; + + const int a = mesh.VT[he]; + const int b = mesh.VT[he + 1]; + const int c = mesh.VT[he + 2]; + + const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; + const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; + const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; + + color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; + normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + } + + + + __host__ __device__ + vec li() + { + } +}; + + template __host__ __device__ vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 18fd50c2..baf9bf07 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -33,8 +33,8 @@ CHE::CHE(const che * mesh) n_faces = mesh->n_faces; n_half_edges = mesh->n_half_edges; - GT = (vertex *) mesh->GT; - VN = (vertex *) mesh->VN; + GT = mesh->GT; + VN = mesh->VN; VC = mesh->VC; VT = mesh->VT; OT = mesh->OT; diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 5ecd7b1d..48ae8595 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -40,16 +40,10 @@ extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); - - const unsigned int primID = optixGetPrimitiveIndex(); - - const int he = primID * che::mtrig; - const float u = optixGetTriangleBarycentrics().x; - const float v = optixGetTriangleBarycentrics().y; - - const int a = mesh.VT[he]; - const int b = mesh.VT[he + 1]; - const int c = mesh.VT[he + 2]; + + const int primID = optixGetPrimitiveIndex(); + float2 bar = optixGetTriangleBarycentrics(); + eval_hit hit(mesh, primID, bar.x, bar.y); OptixTraversableHandle gas = optixGetGASTraversableHandle(); const unsigned int sbtID = optixGetSbtGASIndex(); @@ -62,16 +56,11 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; - const vertex normal = optix_params.flat ? normalize((B - A) * (C - A)) - : (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + const vertex normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; - const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; - const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; - const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; const vertex * lights = optix_params.lights; - const vertex color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; - const vertex position = (1.f - u - v) * A + u * B + v * C; + const vertex position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; vertex li; for(int i = 0; i < optix_params.n_lights; ++i) @@ -97,7 +86,7 @@ extern "C" __global__ void __closesthit__radiance() 1, // missSBTIndex occluded); - li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * color; + li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * hit.color; } vertex & pixel_color = *ray_data(); From 66daf724df1f943edb8125ba3071e8f5dd2c664e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 09:12:36 +0200 Subject: [PATCH 0717/1018] rt_optix: lambda function --- include/gproshan/raytracing/rt_utils.h | 14 +++++------ src/gproshan/raytracing/rt_optix.cu | 34 ++++++++++++++------------ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index f8bd90ae..75313ee1 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -66,16 +66,16 @@ struct eval_hit color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; } - - - - __host__ __device__ - vec li() - { - } }; +template +__host__ __device__ +bool eval_occluded(Fun f) +{ + return f(); +} + template __host__ __device__ vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 48ae8595..42c2c635 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -70,21 +70,25 @@ extern "C" __global__ void __closesthit__radiance() wi /= light_dist; float dot_wi_normal = (wi, normal); - unsigned int occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); + bool occluded = eval_occluded([&]() + { + unsigned int occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + return occluded != 0; + }); li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * hit.color; } From 58dc07cba1b02af52f26951f7d14ee51246b7152 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 11:12:37 +0200 Subject: [PATCH 0718/1018] rt_optix: add eval_li function --- include/gproshan/raytracing/rt_utils.h | 29 ++++++++---- src/gproshan/raytracing/rt_optix.cu | 64 +++++++++++--------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 75313ee1..cd5cd727 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -39,7 +39,8 @@ struct random }; -template + +template struct eval_hit { float u, v; @@ -66,15 +67,27 @@ struct eval_hit color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; } -}; + __host__ __device__ + vec eval_li(const vec * lights, const int & n_lights, Occluded occluded) + { + vec li, wi; + float light_dist, dot_wi_normal; + + for(int i = 0; i < n_lights; ++i) + { + wi = lights[i] - position; + light_dist = length(wi); + wi /= light_dist; + dot_wi_normal = dot(wi, normal); + + li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(position, wi, light_dist) ? 0.4f : 1.0f) * color; + } + + return li; + } +}; -template -__host__ __device__ -bool eval_occluded(Fun f) -{ - return f(); -} template __host__ __device__ diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 42c2c635..c5c4d2ad 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -43,12 +43,34 @@ extern "C" __global__ void __closesthit__radiance() const int primID = optixGetPrimitiveIndex(); float2 bar = optixGetTriangleBarycentrics(); - eval_hit hit(mesh, primID, bar.x, bar.y); OptixTraversableHandle gas = optixGetGASTraversableHandle(); const unsigned int sbtID = optixGetSbtGASIndex(); const float time = optixGetRayTime(); + auto occluded = [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + { + unsigned int occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + + return occluded != 0; + }; + + eval_hit hit(mesh, primID, bar.x, bar.y); + vertex data[3]; optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); @@ -56,42 +78,10 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; - const vertex normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; - - - const vertex * lights = optix_params.lights; - const vertex position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - - vertex li; - for(int i = 0; i < optix_params.n_lights; ++i) - { - vertex wi = lights[i] - position; - float light_dist = length(wi); - wi /= light_dist; - float dot_wi_normal = (wi, normal); - - bool occluded = eval_occluded([&]() - { - unsigned int occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); - return occluded != 0; - }); - - li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded ? 0.4f : 1.0f) * hit.color; - } + hit.normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; + hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; + + vertex li = hit.eval_li(optix_params.lights, optix_params.n_lights, occluded); vertex & pixel_color = *ray_data(); pixel_color = (pixel_color * optix_params.n_samples + li / optix_params.n_lights) / (optix_params.n_samples + 1); From 38ccd014a0c0e9027c3293eb146047f3c487d865 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 13:16:56 +0200 Subject: [PATCH 0719/1018] rt_embree: using eval_hit and eval_li --- include/gproshan/mesh/che.cuh | 18 ----- include/gproshan/mesh/che.h | 19 ++++++ include/gproshan/raytracing/raytracing.h | 15 +++-- include/gproshan/raytracing/rt_embree.h | 16 ++--- include/gproshan/raytracing/rt_utils.h | 9 +-- src/gproshan/mesh/che.cpp | 14 ++++ src/gproshan/mesh/che.cu | 15 ----- src/gproshan/raytracing/raytracing.cpp | 6 +- src/gproshan/raytracing/rt_embree.cpp | 83 ++++++++++-------------- src/gproshan/raytracing/rt_optix.cu | 6 +- 10 files changed, 92 insertions(+), 109 deletions(-) diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index dfeac306..bd79600e 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -21,24 +21,6 @@ __host__ __device__ index_t cu_prev(index_t he); -struct CHE -{ - size_t n_vertices = 0; - size_t n_faces = 0; - size_t n_half_edges = 0; - - vertex * GT = nullptr; - vertex * VN = nullptr; - che::rgb_t * VC = nullptr; - index_t * VT = nullptr; - index_t * OT = nullptr; - index_t * EVT = nullptr; - - CHE() = default; - CHE(const che * mesh); -}; - - void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false, const bool & color = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index c7731ec0..3cd61c4b 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -209,6 +209,25 @@ inline index_t prev(const index_t & he) } +// simple che data structure +struct CHE +{ + size_t n_vertices = 0; + size_t n_faces = 0; + size_t n_half_edges = 0; + + vertex * GT = nullptr; + vertex * VN = nullptr; + che::rgb_t * VC = nullptr; + index_t * VT = nullptr; + index_t * OT = nullptr; + index_t * EVT = nullptr; + + CHE() = default; + CHE(const che * mesh); +}; + + } // namespace gproshan #endif // CHE_H diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index f120f9ea..810b9307 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -28,10 +28,10 @@ class raytracing protected: struct rt_mesh { - che * mesh; + CHE * mesh; bool pointcloud; - che * operator -> () const + CHE * operator -> () const { return mesh; } @@ -62,11 +62,12 @@ class raytracing ) { return NIL; }; protected: - virtual vec4 intersect_li( const vertex &, // org, - const vertex &, // dir, - const vertex &, // light, - const bool & // flat - ) { return vec4(0); }; + virtual vec3 closesthit_radiance( const vertex &, // org + const vertex &, // dir + const vertex *, // lights + const int &, // n_lights + const bool & // flat + ) { return {}; }; virtual float intersect_depth( const vertex &, // org, const vertex & // dir diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index c372e129..6fd86d61 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -26,9 +26,8 @@ class embree : public raytracing vertex org() const; vertex dir() const; - vertex color(const rt_mesh & mesh) const; - vertex normal(const rt_mesh & mesh, const bool & flat = false) const; vertex position() const; + vertex normal() const; index_t closest_vertex(const rt_mesh & mesh) const; }; @@ -54,21 +53,18 @@ class embree : public raytracing protected: - bool intersect(ray_hit & r); - bool occluded(ray_hit & r); - - void build_bvh( const std::vector & meshes, - const std::vector & model_mats, - const bool & pointcloud = false); + void build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud = false); index_t add_sphere(const vec4 & xyzr); index_t add_mesh(const che * mesh, const mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual vec4 li(const ray_hit & r, const vertex & light, const bool & flat); + virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const bool & flat); - vec4 intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); + + bool intersect(ray_hit & r); + bool occluded(ray_hit & r); }; diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index cd5cd727..a8757107 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -53,17 +53,17 @@ struct eval_hit { u = pu; v = pv; - + const int he = primID * che::mtrig; - + const int a = mesh.VT[he]; const int b = mesh.VT[he + 1]; const int c = mesh.VT[he + 2]; - + const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; - + color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; } @@ -78,6 +78,7 @@ struct eval_hit { wi = lights[i] - position; light_dist = length(wi); + wi /= light_dist; dot_wi_normal = dot(wi, normal); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 9cc1d163..f96201e4 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1127,6 +1127,20 @@ const index_t & che::star_he::iterator::operator * () return he; } +CHE::CHE(const che * mesh) +{ + n_vertices = mesh->n_vertices; + n_faces = mesh->n_faces; + n_half_edges = mesh->n_half_edges; + + GT = mesh->GT; + VN = mesh->VN; + VC = mesh->VC; + VT = mesh->VT; + OT = mesh->OT; + EVT = mesh->EVT; +} + } // namespace gproshan diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index baf9bf07..b140bf4a 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -27,21 +27,6 @@ index_t cu_prev(index_t he) } -CHE::CHE(const che * mesh) -{ - n_vertices = mesh->n_vertices; - n_faces = mesh->n_faces; - n_half_edges = mesh->n_half_edges; - - GT = mesh->GT; - VN = mesh->VN; - VC = mesh->VC; - VT = mesh->VT; - OT = mesh->OT; - EVT = mesh->EVT; -} - - void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal, const bool & color) { dd_che = new CHE; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 89a61dd1..04b6e584 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -34,11 +34,9 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f params.cam_pos ); - li = vec4(0); - for(int l = 0; l < params.n_lights; ++l) - li += intersect_li(params.cam_pos, dir, params.lights[l], flat); + vec3 li = closesthit_radiance(params.cam_pos, dir, params.lights, params.n_lights, flat); - color = (color * n_samples + li / params.n_lights) / (n_samples + 1); + color = (color * n_samples + li) / (n_samples + 1); color[3] = 1; } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 70c774fd..c26345ec 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -44,20 +44,14 @@ vertex embree::ray_hit::dir() const return {ray.dir_x, ray.dir_y, ray.dir_z}; } -vertex embree::ray_hit::color(const rt_mesh & mesh) const +vertex embree::ray_hit::normal() const { - if(mesh.pointcloud) - return mesh->color(hit.primID); - - return mesh->shading_color(hit.primID, 1.0 - hit.u - hit.v, hit.u); + return normalize(vec3{hit.Ng_x, hit.Ng_y, hit.Ng_z}); } -vertex embree::ray_hit::normal(const rt_mesh & mesh, const bool & flat) const +vertex embree::ray_hit::position() const { - if(flat || mesh.pointcloud) - return normalize(vertex{hit.Ng_x, hit.Ng_y, hit.Ng_z}); - - return mesh->shading_normal(hit.primID, 1.0 - hit.u - hit.v, hit.u); + return org() + ray.tfar * dir(); } index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const @@ -79,12 +73,7 @@ index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const w = hit.v; } - return mesh->halfedge(he); -} - -vertex embree::ray_hit::position() const -{ - return org() + ray.tfar * dir(); + return mesh->VT[he]; } @@ -128,6 +117,7 @@ hit embree::intersect(const vertex & org, const vertex & dir) ray_hit r(org, dir); if(intersect(r)) { + /* const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; const vertex & color = r.color(mesh); const vertex & normal = r.normal(mesh); @@ -136,35 +126,23 @@ hit embree::intersect(const vertex & org, const vertex & dir) r.ray.tfar, {color.x(), color.y(), color.z()}, {normal.x(), normal.y(), normal.z()} - }; + };*/ } return hit(); } -bool embree::intersect(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - -bool embree::occluded(ray_hit & r) -{ - rtcIntersect1(scene, &intersect_context, &r); - return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; -} - void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { for(index_t i = 0; i < meshes.size(); ++i) { - che * mesh = meshes[i]; + CHE * mesh = new CHE(meshes[i]); const mat4 & model_mat = model_mats[i]; - if(mesh->is_pointcloud() || pointcloud) - geomID_mesh[add_pointcloud(mesh, model_mat)] = {mesh, true}; + if(!mesh->n_faces || pointcloud) + geomID_mesh[add_pointcloud(meshes[i], model_mat)] = {mesh, true}; else - geomID_mesh[add_mesh(mesh, model_mat)] = {mesh, false}; + geomID_mesh[add_mesh(meshes[i], model_mat)] = {mesh, false}; } rtcCommitScene(scene); @@ -252,25 +230,22 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -vec4 embree::li(const ray_hit & r, const vertex & light, const bool & flat) +vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const bool & flat) { - const vertex & position = r.position(); - const vertex & normal = r.normal(geomID_mesh[r.hit.geomID], flat); - const vertex & color = r.color(geomID_mesh[r.hit.geomID]); + ray_hit r(org, dir); + if(!intersect(r)) return {}; - vertex wi = light - position; - float light_dist = length(wi); - wi /= light_dist; - float dot_wi_normal = (wi, normal); + auto shadow = [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + { + ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); + return occluded(ro); + }; - ray_hit rs(position, wi, 1e-3f, light_dist - 1e-3f); - return (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(rs) ? 0.4f : 1.0f) * vec4{color, 1}; -} + eval_hit hit(*geomID_mesh[r.hit.geomID].mesh, r.hit.primID, r.hit.u, r.hit.v); + hit.position = r.position(); + hit.normal = flat ? r.normal() : hit.normal; -vec4 embree::intersect_li(const vertex & org, const vertex & dir, const vertex & light, const bool & flat) -{ - ray_hit r(org, dir); - return intersect(r) ? li(r, light, flat) : vec4(0.f); + return hit.eval_li(lights, n_lights, shadow); } float embree::intersect_depth(const vertex & org, const vertex & dir) @@ -279,6 +254,18 @@ float embree::intersect_depth(const vertex & org, const vertex & dir) return intersect(r) ? r.ray.tfar : 0.f; } +bool embree::intersect(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + +bool embree::occluded(ray_hit & r) +{ + rtcIntersect1(scene, &intersect_context, &r); + return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; +} + } // namespace gproshan diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index c5c4d2ad..9717b6cb 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -40,7 +40,7 @@ extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); - + const int primID = optixGetPrimitiveIndex(); float2 bar = optixGetTriangleBarycentrics(); @@ -70,7 +70,7 @@ extern "C" __global__ void __closesthit__radiance() }; eval_hit hit(mesh, primID, bar.x, bar.y); - + vertex data[3]; optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); @@ -78,7 +78,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; - hit.normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; + hit.normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; vertex li = hit.eval_li(optix_params.lights, optix_params.n_lights, occluded); From 88deef8c78a5b2ba4b6958061558b03ec9d95374 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 15:30:31 +0200 Subject: [PATCH 0720/1018] raytracing: refactoring eval_li function --- include/gproshan/raytracing/rt_utils.h | 35 ++++++++++---------- src/gproshan/raytracing/rt_embree.cpp | 17 ++++------ src/gproshan/raytracing/rt_optix.cu | 46 ++++++++++++-------------- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index a8757107..39d44e72 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -38,9 +38,7 @@ struct random } }; - - -template +template struct eval_hit { float u, v; @@ -68,27 +66,28 @@ struct eval_hit normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; } - __host__ __device__ - vec eval_li(const vec * lights, const int & n_lights, Occluded occluded) - { - vec li, wi; - float light_dist, dot_wi_normal; +}; - for(int i = 0; i < n_lights; ++i) - { - wi = lights[i] - position; - light_dist = length(wi); +template +__host__ __device__ +vec eval_li(const eval_hit & hit, const vec * lights, const int & n_lights, Occluded occluded) +{ + vec li, wi; + float light_dist, dot_wi_normal; - wi /= light_dist; - dot_wi_normal = dot(wi, normal); + for(int i = 0; i < n_lights; ++i) + { + wi = lights[i] - hit.position; + light_dist = length(wi); - li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(position, wi, light_dist) ? 0.4f : 1.0f) * color; - } + wi /= light_dist; + dot_wi_normal = dot(wi, hit.normal); - return li; + li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(hit.position, wi, light_dist) ? 0.4f : 1.0f) * hit.color; } -}; + return li / n_lights; +} template __host__ __device__ diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index c26345ec..c53ddea3 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -29,9 +29,7 @@ embree::ray_hit::ray_hit(const vertex & p_org, const vertex & v_dir, float near, ray.mask = 0; ray.flags = 0; - //hit.primID = RTC_INVALID_GEOMETRY_ID; hit.geomID = RTC_INVALID_GEOMETRY_ID; - hit.instID[0] = RTC_INVALID_GEOMETRY_ID; } vertex embree::ray_hit::org() const @@ -235,17 +233,16 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v ray_hit r(org, dir); if(!intersect(r)) return {}; - auto shadow = [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool - { - ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); - return occluded(ro); - }; - - eval_hit hit(*geomID_mesh[r.hit.geomID].mesh, r.hit.primID, r.hit.u, r.hit.v); + eval_hit hit(*geomID_mesh[r.hit.geomID].mesh, r.hit.primID, r.hit.u, r.hit.v); hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; - return hit.eval_li(lights, n_lights, shadow); + return eval_li( hit, lights, n_lights, + [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + { + ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); + return occluded(ro); + }); } float embree::intersect_depth(const vertex & org, const vertex & dir) diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 9717b6cb..786d6c4d 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -48,29 +48,6 @@ extern "C" __global__ void __closesthit__radiance() const unsigned int sbtID = optixGetSbtGASIndex(); const float time = optixGetRayTime(); - auto occluded = [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool - { - unsigned int occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); - - return occluded != 0; - }; - - eval_hit hit(mesh, primID, bar.x, bar.y); - vertex data[3]; optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); @@ -78,10 +55,31 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; + eval_hit hit(mesh, primID, bar.x, bar.y); hit.normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - vertex li = hit.eval_li(optix_params.lights, optix_params.n_lights, occluded); + vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, + [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + { + unsigned int occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + + return occluded != 0; + }); vertex & pixel_color = *ray_data(); pixel_color = (pixel_color * optix_params.n_samples + li / optix_params.n_lights) / (optix_params.n_samples + 1); From 811b0d19f4f5c81faa577b56c963496a2e4d9aff Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 16:05:46 +0200 Subject: [PATCH 0721/1018] scenes: update scanner ptx --- include/gproshan/raytracing/raytracing.h | 15 ++---- include/gproshan/raytracing/rt_embree.h | 3 +- include/gproshan/raytracing/rt_utils.h | 15 ++++-- src/gproshan/raytracing/rt_embree.cpp | 69 +++++++++++------------- src/gproshan/scenes/scanner.cpp | 6 +-- 5 files changed, 47 insertions(+), 61 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 810b9307..41b2d8fd 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -14,15 +14,6 @@ namespace gproshan::rt { -struct hit -{ - index_t idx = NIL; - real_t dist = INFINITY; - vertex color; - vertex normal; -}; - - class raytracing { protected: @@ -53,9 +44,9 @@ class raytracing const index_t & samples = 4 ); - virtual hit intersect( const vertex &, // org - const vertex & //dir - ) { return hit(); } + virtual eval_hit intersect( const vertex &, // org + const vertex & //dir + ) { return eval_hit(); } virtual index_t closest_vertex( const vertex &, // org, const vertex & // dir diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 6fd86d61..5a724821 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -28,7 +28,6 @@ class embree : public raytracing vertex dir() const; vertex position() const; vertex normal() const; - index_t closest_vertex(const rt_mesh & mesh) const; }; @@ -49,7 +48,7 @@ class embree : public raytracing virtual ~embree(); virtual index_t closest_vertex(const vertex & org, const vertex & dir); - virtual hit intersect(const vertex & org, const vertex & dir); + virtual eval_hit intersect(const vertex & org, const vertex & dir); protected: diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 39d44e72..ed5ace3d 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -41,16 +41,22 @@ struct random template struct eval_hit { - float u, v; + int primID = NIL; + float dist = 0; + float u = 0, v = 0; vec position; vec normal; vec color; __host__ __device__ - eval_hit(const CHE & mesh, const unsigned int & primID, const float & pu, const float & pv) + eval_hit() {} + + __host__ __device__ + eval_hit(const CHE & mesh, const unsigned int & aprimID, const float & au, const float & av) { - u = pu; - v = pv; + primID = aprimID; + u = au; + v = av; const int he = primID * che::mtrig; @@ -65,7 +71,6 @@ struct eval_hit color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; } - }; template diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index c53ddea3..d81b8b77 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -52,29 +52,6 @@ vertex embree::ray_hit::position() const return org() + ray.tfar * dir(); } -index_t embree::ray_hit::closest_vertex(const rt_mesh & mesh) const -{ - if(mesh.pointcloud) return hit.primID; - - index_t he = che::mtrig * hit.primID; - float w = 1 - hit.u - hit.v; - - if(w < hit.u) - { - he = che::mtrig * hit.primID + 1; - w = hit.u; - } - - if(w < hit.v) - { - he = che::mtrig * hit.primID + 2; - w = hit.v; - } - - return mesh->VT[he]; -} - - void embree_error(void *, RTCError, const char * str) { fprintf(stderr, "EMBREE ERROR: %s\n", str); @@ -107,27 +84,41 @@ embree::~embree() index_t embree::closest_vertex(const vertex & org, const vertex & dir) { ray_hit r(org, dir); - return intersect(r) ? r.closest_vertex(geomID_mesh[r.hit.geomID]) : NIL; + if(!intersect(r)) return NIL; + + const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; + if(mesh.pointcloud) + return r.hit.primID; + + index_t he = che::mtrig * r.hit.primID; + float w = 1 - r.hit.u - r.hit.v; + + if(w < r.hit.u) + { + he = che::mtrig * r.hit.primID + 1; + w = r.hit.u; + } + + if(w < r.hit.v) + { + he = che::mtrig * r.hit.primID + 2; + w = r.hit.v; + } + + return mesh->VT[he]; } -hit embree::intersect(const vertex & org, const vertex & dir) +eval_hit embree::intersect(const vertex & org, const vertex & dir) { ray_hit r(org, dir); - if(intersect(r)) - { - /* - const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; - const vertex & color = r.color(mesh); - const vertex & normal = r.normal(mesh); - - return { r.closest_vertex(mesh), - r.ray.tfar, - {color.x(), color.y(), color.z()}, - {normal.x(), normal.y(), normal.z()} - };*/ - } + if(!intersect(r)) return eval_hit(); + + const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; + eval_hit hit(*mesh.mesh, r.hit.primID, r.hit.u, r.hit.v); + hit.dist = r.ray.tfar; + hit.position = r.position(); - return hit(); + return hit; } void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 2c5ffafc..272978f5 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -29,11 +29,11 @@ che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, const real_t & theta = j * delta_theta; const vertex & dir = {std::sin(theta) * std::cos(phi), std::sin(theta) * std::sin(phi), std::cos(theta)}; - const hit & h = rt->intersect(cam_pos, dir); + const eval_hit & h = rt->intersect(cam_pos, dir); - if(h.idx != NIL) + if(h.primID != NIL) { - mesh_ptx->point(v) = cam_pos + dir * h.dist; + mesh_ptx->point(v) = h.position; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; mesh_ptx->rgb(v) = { (unsigned char) (h.color.x() * 255), From 0e6db70a662f91e1fb1c5b622c8e23ba0ff81315 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 16:20:43 +0200 Subject: [PATCH 0722/1018] raytracing: using eval_hit non template --- include/gproshan/raytracing/raytracing.h | 6 +++--- include/gproshan/raytracing/rt_embree.h | 2 +- include/gproshan/raytracing/rt_utils.h | 18 +++++++++++------- src/gproshan/raytracing/rt_embree.cpp | 4 ++-- src/gproshan/scenes/scanner.cpp | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 41b2d8fd..50c305ad 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -44,9 +44,9 @@ class raytracing const index_t & samples = 4 ); - virtual eval_hit intersect( const vertex &, // org - const vertex & //dir - ) { return eval_hit(); } + virtual eval_hit intersect( const vertex &, // org + const vertex & //dir + ) { return {}; } virtual index_t closest_vertex( const vertex &, // org, const vertex & // dir diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 5a724821..fcd4dfa2 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -48,7 +48,7 @@ class embree : public raytracing virtual ~embree(); virtual index_t closest_vertex(const vertex & org, const vertex & dir); - virtual eval_hit intersect(const vertex & org, const vertex & dir); + virtual eval_hit intersect(const vertex & org, const vertex & dir); protected: diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index ed5ace3d..bb7c576d 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -38,21 +38,21 @@ struct random } }; -template -struct eval_hit +template +struct t_eval_hit { int primID = NIL; - float dist = 0; - float u = 0, v = 0; + T dist = 0; + T u = 0, v = 0; vec position; vec normal; vec color; __host__ __device__ - eval_hit() {} + t_eval_hit() {} __host__ __device__ - eval_hit(const CHE & mesh, const unsigned int & aprimID, const float & au, const float & av) + t_eval_hit(const CHE & mesh, const unsigned int & aprimID, const T & au, const T & av) { primID = aprimID; u = au; @@ -75,7 +75,7 @@ struct eval_hit template __host__ __device__ -vec eval_li(const eval_hit & hit, const vec * lights, const int & n_lights, Occluded occluded) +vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, Occluded occluded) { vec li, wi; float light_dist, dot_wi_normal; @@ -94,6 +94,10 @@ vec eval_li(const eval_hit & hit, const vec * lights, const int & return li / n_lights; } + +using eval_hit = t_eval_hit; + + template __host__ __device__ vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index d81b8b77..b8be56c7 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -108,10 +108,10 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) return mesh->VT[he]; } -eval_hit embree::intersect(const vertex & org, const vertex & dir) +eval_hit embree::intersect(const vertex & org, const vertex & dir) { ray_hit r(org, dir); - if(!intersect(r)) return eval_hit(); + if(!intersect(r)) return {}; const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; eval_hit hit(*mesh.mesh, r.hit.primID, r.hit.u, r.hit.v); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 272978f5..74716ea2 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -29,7 +29,7 @@ che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, const real_t & theta = j * delta_theta; const vertex & dir = {std::sin(theta) * std::cos(phi), std::sin(theta) * std::sin(phi), std::cos(theta)}; - const eval_hit & h = rt->intersect(cam_pos, dir); + const eval_hit & h = rt->intersect(cam_pos, dir); if(h.primID != NIL) { From 3569e2dcaa2e347c014f8761761fe74a9771cc1c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 21 Oct 2022 16:36:23 +0200 Subject: [PATCH 0723/1018] rt_optix: fix accumulation radiance --- src/gproshan/raytracing/rt_optix.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 786d6c4d..ebee2ff5 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -82,7 +82,7 @@ extern "C" __global__ void __closesthit__radiance() }); vertex & pixel_color = *ray_data(); - pixel_color = (pixel_color * optix_params.n_samples + li / optix_params.n_lights) / (optix_params.n_samples + 1); + pixel_color = (pixel_color * optix_params.n_samples + li) / (optix_params.n_samples + 1); } From b00098c52d55654d04018d2ebab8a44ee8c563f5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 23 Oct 2022 17:25:06 +0200 Subject: [PATCH 0724/1018] gproshan: update CMakeLists, add native arch cuda --- CMakeLists.txt | 2 +- include/gproshan/raytracing/rt_utils.h | 2 +- src/gproshan/CMakeLists.txt | 13 +++++-------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8361b25a..f6ef21bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) - set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) + set(CMAKE_CUDA_ARCHITECTURES native) find_package(OptiX 7.5) if(OptiX_INCLUDE) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index bb7c576d..2acaedb7 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -41,7 +41,7 @@ struct random template struct t_eval_hit { - int primID = NIL; + unsigned int primID = NIL; T dist = 0; T u = 0, v = 0; vec position; diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 14ce8e80..95bf27f0 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -33,14 +33,11 @@ endif(CUDAToolkit_FOUND) if(OptiX_INCLUDE) - add_custom_target( optix_ptx ALL - COMMAND ${CMAKE_CUDA_COMPILER} --optix-ir --use_fast_math - -isystem ${OptiX_INCLUDE} - -isystem ${gproshan_SOURCE_DIR}/include - -o ${gproshan_SOURCE_DIR}/src/rt_optix.ptx - ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu - DEPENDS ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu - ) + add_library(optix_ptx OBJECT ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) + set_property(TARGET optix_ptx PROPERTY CUDA_PTX_COMPILATION ON) + add_custom_target( copy_optix_ptx ALL + DEPENDS optix_ptx + COMMAND ${CMAKE_COMMAND} -E copy $ ${gproshan_SOURCE_DIR}/src/) endif(OptiX_INCLUDE) From 4dbe3700942c48218ff3c7067235b625a44b35af Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 23 Oct 2022 23:24:54 +0200 Subject: [PATCH 0725/1018] fix github actions cuda build --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f6ef21bd..1b17c067 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,9 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) - set(CMAKE_CUDA_ARCHITECTURES native) + if(NOT DEFINED ENV{GITHUB_ACTIONS}) + set(CMAKE_CUDA_ARCHITECTURES native) + endif() find_package(OptiX 7.5) if(OptiX_INCLUDE) From 9c4d359d98a3753f6a150fae037124c7b8eee3d4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Oct 2022 12:13:15 +0200 Subject: [PATCH 0726/1018] update README and clean up rt comments --- CMakeLists.txt | 4 ++-- README.md | 2 +- include/gproshan/raytracing/raytracing.h | 1 - include/gproshan/raytracing/rt_embree.h | 1 - include/gproshan/raytracing/rt_optix.h | 1 - include/gproshan/raytracing/rt_utils.h | 1 - include/gproshan/scenes/scanner.h | 7 +++---- src/gproshan/raytracing/raytracing.cpp | 1 - src/gproshan/raytracing/rt_embree.cpp | 1 - src/gproshan/raytracing/rt_optix.cpp | 1 - src/gproshan/scenes/scanner.cpp | 9 ++++----- 11 files changed, 10 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b17c067..172fab77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.22) +cmake_minimum_required(VERSION 3.24) project(gproshan VERSION 3.14) @@ -14,7 +14,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) -find_package(CUDAToolkit 11.7) +find_package(CUDAToolkit 11.8) if(CUDAToolkit_FOUND) enable_language(CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) diff --git a/README.md b/README.md index ec45a044..c713d29f 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] ### Dependencies (Linux) -g++ >= 9.4, cuda >= 11.7, cmake >= 3.22, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 +g++ >= 9.4, cuda >= 11.8, cmake >= 3.24, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 In Ubuntu you can install them with: diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 50c305ad..988a0b74 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -10,7 +10,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index fcd4dfa2..89b96b44 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -10,7 +10,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index adc79045..d6935f41 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -15,7 +15,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 2acaedb7..09085340 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -8,7 +8,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 266518f1..16eb8874 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -6,13 +6,12 @@ // geometry processing and shape analysis framework -// raytracing approach -namespace gproshan::rt { +namespace gproshan { -che * scanner_ptx(const raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos); +che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos); -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); +che * scanner_ptx(const che * mesh, rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); } // namespace gproshan diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 04b6e584..fb923b69 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -4,7 +4,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index b8be56c7..9c505c29 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -8,7 +8,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index bb40b4f2..141d7887 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -12,7 +12,6 @@ // geometry processing and shape analysis framework -// raytracing approach namespace gproshan::rt { diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 74716ea2..94ec3839 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -8,11 +8,10 @@ using namespace cimg_library; // geometry processing and shape analysis framework -// raytracing approach -namespace gproshan::rt { +namespace gproshan { -che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) +che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) { che * mesh_ptx = new che(n_cols * n_rows); @@ -29,7 +28,7 @@ che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, const real_t & theta = j * delta_theta; const vertex & dir = {std::sin(theta) * std::cos(phi), std::sin(theta) * std::sin(phi), std::cos(theta)}; - const eval_hit & h = rt->intersect(cam_pos, dir); + const rt::eval_hit & h = rt->intersect(cam_pos, dir); if(h.primID != NIL) { @@ -50,7 +49,7 @@ che * scanner_ptx(raytracing * rt, const size_t & n_rows, const size_t & n_cols, return mesh_ptx; } -che * scanner_ptx(const che * mesh, raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) +che * scanner_ptx(const che * mesh, rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) { che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); From 8b80cd4da1542c06647234249c6a5d9e8a8e7fc8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Oct 2022 17:51:54 +0200 Subject: [PATCH 0727/1018] viewer: add mesh reset normals --- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/viewer/viewer.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 9d3afb70..3eff9fe8 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -100,7 +100,7 @@ class viewer che_viewer & active_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); - bool add_mesh(che * p_mesh); + bool add_mesh(che * p_mesh, const bool & reset_normals = true); protected: virtual bool run(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 58acec9e..cfb310ae 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -405,12 +405,13 @@ void viewer::add_process(const int & key, const string & skey, const string & na else cerr << "Repeat key: " << key << endl; } -bool viewer::add_mesh(che * p_mesh) +bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) { if(n_meshes == max_n_meshes) return false; - p_mesh->update_normals(); + if(reset_normals) + p_mesh->update_normals(); che_viewer & mesh = meshes[n_meshes]; mesh.init(p_mesh); From 1496f595e34b5d30e56f619c1c202a332eb671c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Oct 2022 18:38:01 +0200 Subject: [PATCH 0728/1018] rt_optix: refactoring constructors --- include/gproshan/raytracing/rt_optix.h | 1 + src/gproshan/raytracing/rt_optix.cpp | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index d6935f41..11772731 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -50,6 +50,7 @@ class optix : public raytracing void * as_buffer = nullptr; public: + optix(); optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 141d7887..a0760f16 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -40,7 +40,7 @@ void optix_log(unsigned int level, const char * tag, const char * message, void fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } -optix::optix(const std::vector & meshes, const std::vector & model_mats) +optix::optix() { optixInit(); @@ -87,22 +87,24 @@ optix::optix(const std::vector & meshes, const std::vector & model_ create_miss_programs(); create_hitgroup_programs(); - // build as - optix_params.traversable = build_as(meshes, model_mats); - // create pipeline create_pipeline(); - // build sbt - build_sbt(); - // launch params cudaMalloc(&optix_params_buffer, sizeof(launch_params)); } +optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() +{ + // build as + optix_params.traversable = build_as(meshes, model_mats); + + // build sbt + build_sbt(); +} + optix::~optix() { - cudaFree(optix_params.color_buffer); cudaFree(optix_params_buffer); cudaFree(raygen_records_buffer); cudaFree(miss_records_buffer); From 82fa988a564065f6a045213f9c3ba2680acd6969 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 28 Oct 2022 00:19:30 +0200 Subject: [PATCH 0729/1018] rt_embree: geom meshes data --- include/gproshan/raytracing/raytracing.h | 14 -------- include/gproshan/raytracing/rt_embree.h | 3 +- include/gproshan/raytracing/rt_optix.h | 43 ++++++++++++------------ src/gproshan/raytracing/rt_embree.cpp | 26 +++++++------- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 988a0b74..e7a2d069 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -6,7 +6,6 @@ #include #include -#include // geometry processing and shape analysis framework @@ -16,19 +15,6 @@ namespace gproshan::rt { class raytracing { protected: - struct rt_mesh - { - CHE * mesh; - bool pointcloud; - - CHE * operator -> () const - { - return mesh; - } - }; - - std::map geomID_mesh; - size_t n_samples = 0; public: diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 89b96b44..328973c3 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -34,7 +34,8 @@ class embree : public raytracing RTCScene scene; RTCIntersectContext intersect_context; - protected: + std::vector g_meshes; + float pc_radius = 1; public: diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 11772731..c1d086d6 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -20,34 +20,35 @@ namespace gproshan::rt { class optix : public raytracing { - CUcontext cuda_context; - CUstream stream; + protected: + CUcontext cuda_context; + CUstream stream; - OptixDeviceContext optix_context; + OptixDeviceContext optix_context; - OptixModule optix_module; - OptixModuleCompileOptions optix_module_compile_opt = {}; + OptixModule optix_module; + OptixModuleCompileOptions optix_module_compile_opt = {}; - OptixPipeline optix_pipeline; - OptixPipelineCompileOptions optix_pipeline_compile_opt = {}; - OptixPipelineLinkOptions optix_pipeline_link_opt = {}; + OptixPipeline optix_pipeline; + OptixPipelineCompileOptions optix_pipeline_compile_opt = {}; + OptixPipelineLinkOptions optix_pipeline_link_opt = {}; - OptixProgramGroup raygen_programs[1]; - OptixProgramGroup miss_programs[2]; - OptixProgramGroup hitgroup_programs[2]; + OptixProgramGroup raygen_programs[1]; + OptixProgramGroup miss_programs[2]; + OptixProgramGroup hitgroup_programs[2]; - OptixShaderBindingTable sbt = {}; + OptixShaderBindingTable sbt = {}; - launch_params optix_params; - launch_params * optix_params_buffer = nullptr; + launch_params optix_params; + launch_params * optix_params_buffer = nullptr; - std::vector dd_mesh; - std::vector d_mesh; + std::vector dd_mesh; + std::vector d_mesh; - void * raygen_records_buffer = nullptr; - void * miss_records_buffer = nullptr; - void * hitgroup_records_buffer = nullptr; - void * as_buffer = nullptr; + void * raygen_records_buffer = nullptr; + void * miss_records_buffer = nullptr; + void * hitgroup_records_buffer = nullptr; + void * as_buffer = nullptr; public: optix(); @@ -56,7 +57,7 @@ class optix : public raytracing void render(vec4 * img, const render_params & params, const bool & flat); - private: + protected: void create_raygen_programs(); void create_miss_programs(); void create_hitgroup_programs(); diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 9c505c29..35dc7812 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -85,9 +85,9 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) ray_hit r(org, dir); if(!intersect(r)) return NIL; - const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; - if(mesh.pointcloud) - return r.hit.primID; + const CHE * mesh = g_meshes[r.hit.geomID]; + + if(!mesh->n_faces) return r.hit.primID; index_t he = che::mtrig * r.hit.primID; float w = 1 - r.hit.u - r.hit.v; @@ -112,8 +112,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) ray_hit r(org, dir); if(!intersect(r)) return {}; - const rt_mesh & mesh = geomID_mesh[r.hit.geomID]; - eval_hit hit(*mesh.mesh, r.hit.primID, r.hit.u, r.hit.v); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v); hit.dist = r.ray.tfar; hit.position = r.position(); @@ -122,15 +121,18 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { + g_meshes.resize(meshes.size()); for(index_t i = 0; i < meshes.size(); ++i) { - CHE * mesh = new CHE(meshes[i]); - const mat4 & model_mat = model_mats[i]; + g_meshes[i] = new CHE(meshes[i]); + + if(!meshes[i]->n_faces || pointcloud) + g_meshes[i]->n_faces = 0; + + const index_t & geomID = g_meshes[i]->n_faces ? add_mesh(meshes[i], model_mats[i]) : + add_pointcloud(meshes[i], model_mats[i]); - if(!mesh->n_faces || pointcloud) - geomID_mesh[add_pointcloud(meshes[i], model_mat)] = {mesh, true}; - else - geomID_mesh[add_mesh(meshes[i], model_mat)] = {mesh, false}; + gproshan_error_var(i == geomID); } rtcCommitScene(scene); @@ -223,7 +225,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*geomID_mesh[r.hit.geomID].mesh, r.hit.primID, r.hit.u, r.hit.v); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v); hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; From c33c9a36c7fabe3371ddfcf4eae86b249178505d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 4 Nov 2022 14:49:17 +0100 Subject: [PATCH 0730/1018] small fixes saving ptx and vec is_zero --- include/gproshan/geometry/vec.h | 2 +- src/gproshan/mesh/che_ptx.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index e39816b2..457c0285 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -252,7 +252,7 @@ class vec __host__ __device__ bool is_zero() { - double eps = std::numeric_limits::epsilon(); + T eps = std::numeric_limits::epsilon(); for(index_t i = 0; i < N; ++i) if(abs(values[i]) > eps) return false; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 8f79bc79..3dbf6534 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -141,8 +141,9 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ { const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); + const real_t & h = mesh->heatmap(i); - fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) 0, c.r, c.g, c.b ); + fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) h, c.r, c.g, c.b ); } } From aa9da271b38db950f472c53000127ab409f8835c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 8 Nov 2022 16:57:04 +0100 Subject: [PATCH 0731/1018] che: #14 - normalize sphere model_mat --- include/gproshan/mesh/che.h | 2 +- src/gproshan/mesh/che.cpp | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 3cd61c4b..04c026f9 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -84,7 +84,7 @@ class che // update methods void reload(); - void normalize_sphere(const real_t & r = 1); + mat4 normalize_sphere(const real_t & r = 1) const; mat4 normalize_box(const real_t & side = 2) const; che * merge(const che * mesh, const std::vector & com_vertices); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index f96201e4..2c2d97da 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -192,7 +192,7 @@ void che::reload() init(filename); } -void che::normalize_sphere(const real_t & r) +mat4 che::normalize_sphere(const real_t & r) const { vertex center; @@ -207,16 +207,24 @@ void che::normalize_sphere(const real_t & r) real_t max_norm = 0; - #pragma omp parallel for reduction(max: max_norm) + #pragma omp parallel for reduction(+: max_norm) for(index_t v = 0; v < n_vertices; ++v) - { - GT[v] -= center; - max_norm = std::max(max_norm, norm(GT[v])); - } + max_norm += norm(GT[v] - center); - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - GT[v] *= r / max_norm; + max_norm /= n_vertices; + + mat4 model_mat; + + const real_t & scale = r / max_norm; + model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; + + center *= -scale; + model_mat(0, 3) = center.x(); + model_mat(1, 3) = center.y(); + model_mat(2, 3) = center.z(); + model_mat(3, 3) = 1; + + return model_mat; } mat4 che::normalize_box(const real_t & side) const From 0b1bd03a4aad67c3259fbbaa57a8528fc506890d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 10 Nov 2022 13:58:26 +0100 Subject: [PATCH 0732/1018] removing using namespace std --- include/gproshan/geodesics/geodesics.h | 1 - include/gproshan/geometry/vec.h | 1 + include/gproshan/include_arma.h | 1 + include/gproshan/mdict/basis.h | 6 +----- include/gproshan/mdict/mdict.h | 3 --- include/gproshan/mdict/patch.h | 3 --- include/gproshan/viewer/frame.h | 1 - include/gproshan/viewer/shader.h | 3 ++- include/gproshan/viewer/viewer.h | 8 ++++---- src/gproshan/app_viewer.cpp | 1 - src/gproshan/features/key_components.cpp | 2 -- src/gproshan/features/key_points.cpp | 2 -- src/gproshan/geodesics/dijkstra.cpp | 2 -- src/gproshan/geodesics/geodesics.cpp | 2 -- src/gproshan/geodesics/geodesics_ptp.cpp | 2 -- src/gproshan/geodesics/geodesics_ptp.cu | 2 -- src/gproshan/geodesics/geodesics_ptp_coalescence.cu | 2 -- src/gproshan/geodesics/heat_method.cpp | 2 -- src/gproshan/geodesics/sampling.cpp | 2 -- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 -- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 -- src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu | 2 -- src/gproshan/geometry/convex_hull.cpp | 1 - src/gproshan/laplacian/laplacian.cpp | 2 -- src/gproshan/mdict/patch.cpp | 1 + src/gproshan/mesh/che.cpp | 3 --- src/gproshan/mesh/che_fill_hole.cpp | 2 -- src/gproshan/mesh/che_img.cpp | 2 -- src/gproshan/mesh/che_obj.cpp | 3 --- src/gproshan/mesh/che_off.cpp | 3 --- src/gproshan/mesh/che_ply.cpp | 3 --- src/gproshan/mesh/che_poisson.cpp | 2 -- src/gproshan/mesh/che_pts.cpp | 3 --- src/gproshan/mesh/che_ptx.cpp | 1 - src/gproshan/mesh/che_sphere.cpp | 3 --- src/gproshan/mesh/che_xyz.cpp | 3 --- src/gproshan/mesh/quaternion.cpp | 2 -- src/gproshan/mesh/simplification.cpp | 3 --- src/gproshan/raytracing/rt_optix.cu | 1 - src/gproshan/viewer/camera.cpp | 3 --- src/gproshan/viewer/che_viewer.cpp | 3 --- src/gproshan/viewer/shader.cpp | 3 --- src/gproshan/viewer/viewer.cpp | 3 --- 43 files changed, 10 insertions(+), 92 deletions(-) diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index f6825ae4..20c387c2 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -3,7 +3,6 @@ #include - #include #include diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 457c0285..6f0b91b4 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -13,6 +13,7 @@ #define __device__ #endif + // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/include_arma.h b/include/gproshan/include_arma.h index bc67e5bf..a1ab9303 100644 --- a/include/gproshan/include_arma.h +++ b/include/gproshan/include_arma.h @@ -10,6 +10,7 @@ #define ARMA_NO_DEBUG #endif + // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 42c4e96a..3878eba6 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -2,13 +2,9 @@ #define BASIS_H #include - -#include - #include - -using namespace std; +#include // geometry processing and shape analysis framework diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index f5fbea63..2426e961 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -2,11 +2,8 @@ #define MDICT_H #include - #include #include - - #include diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 7a3a93fc..7d16af85 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -17,9 +17,6 @@ #endif -using namespace std; - - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index c42ac978..05cb5bd0 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -1,7 +1,6 @@ #ifndef FRAME_H #define FRAME_H - #include #include #include diff --git a/include/gproshan/viewer/shader.h b/include/gproshan/viewer/shader.h index 3d85566d..666b90bc 100644 --- a/include/gproshan/viewer/shader.h +++ b/include/gproshan/viewer/shader.h @@ -1,10 +1,11 @@ #ifndef SHADER_H #define SHADER_H +#include + #include #include -#include // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 3eff9fe8..c29aed1b 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -1,10 +1,6 @@ #ifndef VIEWER_H #define VIEWER_H -#include -#include -#include - #include #include #include @@ -12,6 +8,10 @@ #include #include +#include +#include +#include + #include #include #include diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a2c47ef3..151537f2 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -6,7 +6,6 @@ #include -using namespace std; using namespace gproshan::mdict; diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index c0e33373..306562a8 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -4,8 +4,6 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 5440671e..30ce6664 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -4,8 +4,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index 1ebe0aca..742af383 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -3,8 +3,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index a12f6a09..7980adc2 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -6,8 +6,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 10f9e38b..17da3fe3 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -4,8 +4,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index a04d2b86..4c8e6237 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -10,8 +10,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index 1763e61b..ce05801c 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -11,8 +11,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index aa45288d..80ea6c0d 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -5,8 +5,6 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index 001494ea..fadb26bb 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -5,8 +5,6 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 1890e146..531cf1f9 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -6,8 +6,6 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 4909f9cf..8fc29da0 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -11,8 +11,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 797afeaf..b0c76615 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -13,8 +13,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index afbdcf39..d122b728 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -1,6 +1,5 @@ #include - #include #include diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp index e3fe4103..777816e0 100644 --- a/src/gproshan/laplacian/laplacian.cpp +++ b/src/gproshan/laplacian/laplacian.cpp @@ -1,7 +1,5 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 10eaf997..c37742bd 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -17,6 +17,7 @@ #include #include + // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 2c2d97da..841f9343 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -6,9 +6,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 908a51f1..4d0d4329 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -5,8 +5,6 @@ #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 814452ce..a0f66423 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -8,8 +8,6 @@ #include - -using namespace std; using namespace cimg_library; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index ec9e9f3e..3283ca03 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -5,9 +5,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 996cc81a..3ed89a4d 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -5,9 +5,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index bc3c9a6e..38148264 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -6,9 +6,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index 01945570..4ade6482 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -3,8 +3,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index bccd4ba7..836401b7 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -5,9 +5,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 3dbf6534..2a55eb0b 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -8,7 +8,6 @@ #include using namespace cimg_library; -using namespace std; // geometry processing and shape analysis framework diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 9dd3a228..82e50ff2 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -6,9 +6,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 0b9b4eed..44368d0d 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -5,9 +5,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 80331860..09b51662 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -3,8 +3,6 @@ #include #include -using namespace std; - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 9a2ff735..97c7b7ae 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -1,9 +1,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index ebee2ff5..cb763e8d 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -2,7 +2,6 @@ #include #include - #include #include diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index b67a6440..6e63d45b 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -3,9 +3,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index dfc9abae..fd41b73f 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -8,9 +8,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index 0bced989..20d95ec6 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -7,9 +7,6 @@ #include -using namespace std; - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cfb310ae..5ffa9880 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -21,12 +21,9 @@ #include #endif // GPROSHAN_OPTIX - #include - using namespace cimg_library; -using namespace std; // geometry processing and shape analysis framework From b96d306e00245836011522e482f79e4f1895f0d5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 10 Nov 2022 14:06:15 +0100 Subject: [PATCH 0733/1018] adding std:: --- include/gproshan/app_viewer.h | 2 +- include/gproshan/mdict/image_denoising.h | 2 +- include/gproshan/mdict/mdict.h | 16 ++--- include/gproshan/mdict/msparse_coding.h | 6 +- include/gproshan/mdict/patch.h | 8 +-- src/gproshan/app_viewer.cpp | 18 +++--- src/gproshan/geodesics/geodesics.cpp | 24 +++---- src/gproshan/geodesics/geodesics_ptp.cpp | 8 +-- src/gproshan/geodesics/geodesics_ptp.cu | 8 +-- .../geodesics/geodesics_ptp_coalescence.cu | 4 +- src/gproshan/geodesics/sampling.cpp | 8 +-- src/gproshan/geodesics/test_geodesics_ptp.cpp | 20 +++--- src/gproshan/geodesics/test_geodesics_ptp.cu | 14 ++-- .../test_geodesics_ptp_coalescence.cu | 18 +++--- src/gproshan/laplacian/laplacian.cpp | 4 +- src/gproshan/mdict/basis.cpp | 10 +-- src/gproshan/mdict/image_denoising.cpp | 4 +- src/gproshan/mdict/mdict.cpp | 24 +++---- src/gproshan/mdict/msparse_coding.cpp | 34 +++++----- src/gproshan/mdict/patch.cpp | 20 +++--- src/gproshan/mesh/che.cpp | 52 +++++++-------- src/gproshan/mesh/che_fill_hole.cpp | 64 +++++++++---------- src/gproshan/mesh/che_img.cpp | 4 +- src/gproshan/mesh/che_obj.cpp | 12 ++-- src/gproshan/mesh/che_off.cpp | 8 +-- src/gproshan/mesh/che_ply.cpp | 10 +-- src/gproshan/mesh/che_poisson.cpp | 6 +- src/gproshan/mesh/che_pts.cpp | 6 +- src/gproshan/mesh/che_ptx.cpp | 4 +- src/gproshan/mesh/che_xyz.cpp | 10 +-- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/shader.cpp | 8 +-- src/gproshan/viewer/viewer.cpp | 8 +-- 33 files changed, 223 insertions(+), 223 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 1bda2311..5bccab93 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -55,7 +55,7 @@ class app_viewer : public viewer protected: virtual void init(); - che * load_mesh(const string & file_path); + che * load_mesh(const std::string & file_path); // Scenes static bool process_compute_normals(viewer * p_view); diff --git a/include/gproshan/mdict/image_denoising.h b/include/gproshan/mdict/image_denoising.h index a8592c6d..7e91cf04 100644 --- a/include/gproshan/mdict/image_denoising.h +++ b/include/gproshan/mdict/image_denoising.h @@ -9,7 +9,7 @@ namespace gproshan::mdict { -void test_image_denoising(const string & file); +void test_image_denoising(const std::string & file); } // namespace gproshan::mdict diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 2426e961..82414dc6 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -22,9 +22,9 @@ struct locval_t bool operator < (const locval_t & a, const locval_t & b); -void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); +void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); -a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L); +a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t & L); void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); @@ -46,20 +46,20 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); a_vec OMP(const patch & p, const a_mat & A, const size_t & L); a_vec OMP(const patch & p, const a_mat & A, const size_t & L); -a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L); -a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, const size_t & L); +a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t & L); +a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t & L); -void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); +void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k); // MESH SPARSE -void OMP(vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L); +void OMP(std::vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L); -a_sp_mat OMP_all(vector & locval, const vector & patches, const a_mat & A, const size_t & L); +a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t & L); -void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k); +void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k); } // namespace gproshan::mdict diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index ad76e23a..d2b89a4b 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -78,7 +78,7 @@ class msparse_coding void init_radial_feature_patches(); void load_sampling(); che * point_cloud_reconstruction(real_t per, real_t fr); - vector sort_indexes(const vector &v); + std::vector sort_indexes(const std::vector &v); real_t execute_tmp(); @@ -86,14 +86,14 @@ class msparse_coding void learning(); void sparse_coding(); void init_sampling(); - void load_features(vector & v_feat, size_t & featsize); + void load_features(std::vector & v_feat, size_t & featsize); void init_patches( const bool & reset = 1, const fmask_t & mask = nullptr ); real_t mesh_reconstruction(const fmask_t & mask = nullptr); void update_alphas(a_mat & alpha, size_t threshold); - void save_alpha(string file); + void save_alpha(std::string file); index_t sample(const index_t & s); }; diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 7d16af85..5a8b60c3 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -58,7 +58,7 @@ class patch void init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, - vector & _vertices, + std::vector & _vertices, index_t * _toplevel = nullptr); void init_radial_disjoint( real_t & euc_radio, @@ -94,17 +94,17 @@ class patch const fmask_t & mask = nullptr ); void remove_extra_xyz_disjoint(size_t & max_points); - void add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p); + void add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t & p); const a_vec normal(); bool is_covered( bool * covered); // void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); - void compute_avg_distance(che * mesh, vector & vpatches, const index_t & p); + void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); bool add_vertex_by_faces( vertex & n, - vector & N, + std::vector & N, double thr_angle, const real_t * geo, che * mesh, diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 151537f2..5398b64d 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -19,12 +19,12 @@ app_viewer::~app_viewer() delete meshes[i]; } -che * app_viewer::load_mesh(const string & file_path) +che * app_viewer::load_mesh(const std::string & file_path) { size_t pos = file_path.rfind('.'); - assert(pos != string::npos); + assert(pos != std::string::npos); - string extension = file_path.substr(pos + 1); + std::string extension = file_path.substr(pos + 1); if(extension == "off") return new che_off(file_path); if(extension == "obj") return new che_obj(file_path); @@ -345,7 +345,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static vector vertices; + static std::vector vertices; static size_t min_neigs = 1; static size_t max_neigs = std::min(1000lu, mesh->n_vertices); static fairing_spectral fair(max_neigs); @@ -376,7 +376,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static vector vertices; + static std::vector vertices; static fairing_taubin fair(0); if(ImGui_InputReal("step", &fair.step, 0.001)) @@ -499,7 +499,7 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) index_t * toplesets = new index_t[mesh->n_vertices]; index_t * sorted = new index_t[mesh->n_vertices]; - vector limites; + std::vector limites; mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); size_t n_toplesets = limites.size() - 1; @@ -629,7 +629,7 @@ bool app_viewer::process_mask(viewer * p_view) msc.init_radial_feature_patches(); //dict.init_voronoi_patches(); mesh->update_heatmap(&msc[0]); - string f_points = tmp_file_path(string(msc) + ".rsampl"); + std::string f_points = tmp_file_path(std::string(msc) + ".rsampl"); a_vec points_out; gproshan_debug_var(f_points); @@ -931,7 +931,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) size_t old_n_vertices, n_vertices = mesh->n_vertices; size_t n_holes = 0; // FIX_BOUND mesh->n_borders; - vector * border_vertices; + std::vector * border_vertices; che ** holes; tie(border_vertices, holes) = fill_all_holes_meshes(mesh); if(!holes) return true; @@ -968,7 +968,7 @@ bool app_viewer::process_select_multiple(viewer * p_view) if(ImGui::Button("Add")) { - stringstream ss(line); + std::stringstream ss(line); index_t v; while(ss >> v) mesh.selected.push_back(v); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 7980adc2..a0d89b24 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -11,7 +11,7 @@ namespace gproshan { -geodesics::geodesics(che * mesh, const vector & sources, const params & p): n_vertices(mesh->n_vertices) +geodesics::geodesics(che * mesh, const std::vector & sources, const params & p): n_vertices(mesh->n_vertices) { assert(n_vertices > 0); @@ -90,7 +90,7 @@ void geodesics::normalize() dist[sorted_index[i]] /= max; } -void geodesics::execute(che * mesh, const vector & sources, const params & p) +void geodesics::execute(che * mesh, const std::vector & sources, const params & p) { switch(p.alg) { @@ -110,7 +110,7 @@ void geodesics::execute(che * mesh, const vector & sources, const param } } -void geodesics::run_fastmarching(che * mesh, const vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun) +void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -121,9 +121,9 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co size_t green_count = n_iter ? n_iter : n_vertices; - priority_queue, - vector >, - greater > > Q; + priority_queue, + std::vector >, + greater > > Q; real_t dv, dp; vertex vx; @@ -186,10 +186,10 @@ void geodesics::run_fastmarching(che * mesh, const vector & sources, co delete [] color; } -void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector & sources) +void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources) { index_t * toplesets = new index_t[n_vertices]; - vector limits; + std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); double time_ptp; @@ -203,7 +203,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const vector< delete [] toplesets; } -void geodesics::run_heat_method(che * mesh, const vector & sources) +void geodesics::run_heat_method(che * mesh, const std::vector & sources) { double time_total, solve_time; TIC(time_total) @@ -217,10 +217,10 @@ void geodesics::run_heat_method(che * mesh, const vector & sources) #ifdef GPROSHAN_CUDA -void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources) +void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources) { index_t * toplesets = new index_t[n_vertices]; - vector limits; + std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); double time_ptp; @@ -234,7 +234,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const vector< delete [] toplesets; } -void geodesics::run_heat_method_gpu(che * mesh, const vector & sources) +void geodesics::run_heat_method_gpu(che * mesh, const std::vector & sources) { double time_total, solve_time; TIC(time_total) diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 17da3fe3..f7806faf 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -15,8 +15,8 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top { // sort data by levels, must be improve the coalescence - vector V(toplesets.limits.back()); - vector F; + std::vector V(toplesets.limits.back()); + std::vector F; F.reserve(mesh->n_half_edges); inv = !inv ? new index_t[mesh->n_vertices] : inv; @@ -36,7 +36,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); } -void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) +void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) { const size_t n_vertices = mesh->n_vertices; @@ -118,7 +118,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c delete mesh; } -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) { real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices]}; real_t * error = new real_t[mesh->n_vertices]; diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 4c8e6237..3b097166 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -15,7 +15,7 @@ namespace gproshan { -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const vector & sources, const toplesets_t & toplesets) +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) { cudaDeviceReset(); @@ -82,7 +82,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, return time / 1000; } -real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, double & time_fps, size_t n, real_t radio) +real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { cudaDeviceReset(); @@ -110,7 +110,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - vector limits; + std::vector limits; index_t * toplesets = new index_t[h_mesh->n_vertices]; index_t * sorted_index = new index_t[h_mesh->n_vertices]; @@ -169,7 +169,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, do return max_dist; } -index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) +index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index ce05801c..3edbbd50 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -16,7 +16,7 @@ namespace gproshan { -double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const vector & sources, const toplesets_t & toplesets, const bool & set_inf) +double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf) { index_t * inv = nullptr; che * coalescence_mesh = ptp_coalescence(inv, mesh, toplesets); @@ -104,7 +104,7 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, return time / 1000; } -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) +index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) { for(index_t i = 0; i < sources.size(); ++i) h_dist[inv.index[sources[i]]] = 0; diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index fadb26bb..61bf5061 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -10,7 +10,7 @@ namespace gproshan { -index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio) +index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio) { normals = new vertex[n_points]; sizes = new size_t[n_points]; @@ -36,11 +36,11 @@ index_t ** sampling_shape(vector & points, size_t *& sizes, vertex *& n return indexes; } -bool load_sampling(vector & points, real_t & radio, che * mesh, size_t n) +bool load_sampling(std::vector & points, real_t & radio, che * mesh, size_t n) { - const string & filename = mesh->filename; + const std::string & filename = mesh->filename; - string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + to_string(n); + std::string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + to_std::string(n); ifstream is(tmp_file_path(file)); gproshan_log_var(tmp_file_path(file)); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 531cf1f9..504bd262 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -37,19 +37,19 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) const char * pbtime = "& %6s %6.3lfs "; const char * pberror = "& %6s %6.2lf\\%% "; - string filename; + std::string filename; while(cin >> filename) { gproshan_debug_var(filename); - vector source = { 0 }; + std::vector source = { 0 }; che * mesh = new che_off(data_path + filename + ".off"); size_t n_vertices = mesh->n_vertices; index_t * toplesets = new index_t[n_vertices]; index_t * sorted_index = new index_t[n_vertices]; - vector limits; + std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, source); @@ -193,7 +193,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #ifdef GPROSHAN_CUDA // IMPLEMENT: iter_error_parallel_toplesets_propagation_coalescence_cpu double time; - vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); + std::vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); system(("mv band " + (test_path + filename + ".band")).c_str()); @@ -237,7 +237,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fclose(ftable); } -double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const vector & source, const int & n_test) +double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) { double t, seconds = INFINITY; @@ -254,7 +254,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons return seconds; } -double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; @@ -272,7 +272,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const vect return seconds; } -double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const vector & source, const int & n_test) +double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) { double t, st, ptime; ptime = stime = INFINITY; @@ -295,7 +295,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e #ifdef GPROSHAN_CUDA -double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test) { double t, seconds = INFINITY; @@ -313,7 +313,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const vect return seconds; } -double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const vector & source, const int & n_test) +double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) { double t, st, ptime; ptime = stime = INFINITY; @@ -338,7 +338,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact #endif // GPROSHAN_CUDA -real_t * load_exact_geodesics(const string & file, const size_t & n) +real_t * load_exact_geodesics(const std::string & file, const size_t & n) { ifstream is(file); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 8fc29da0..f5a06a7c 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -16,7 +16,7 @@ namespace gproshan { -vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) +std::vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) { cudaDeviceReset(); @@ -44,7 +44,7 @@ vector > iter_error_parallel_toplesets_propagation_gpu(che real_t * d_error; cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); + std::vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); delete [] h_dist; cudaFree(d_error); @@ -67,7 +67,7 @@ vector > iter_error_parallel_toplesets_propagation_gpu(che } /// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & samples, size_t n, real_t radio) +double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, real_t radio) { cudaDeviceReset(); @@ -93,7 +93,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam index_t * d_sorted; cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - vector limits; + std::vector limits; index_t * toplesets = new index_t[h_mesh->n_vertices]; index_t * sorted_index = new index_t[h_mesh->n_vertices]; @@ -159,7 +159,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, vector & sam return times; } -vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) +std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) @@ -172,7 +172,7 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - vector > iter_error; + std::vector > iter_error; iter_error.reserve(limits.size()); index_t d = 0; @@ -192,7 +192,7 @@ vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_ // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(j == limits.size() - 1) - iter_error.push_back(make_pair(n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size()))); + iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size())}); // end relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond, d_sorted); diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index b0c76615..12e6e158 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -18,7 +18,7 @@ namespace gproshan { -vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const vector & sources, const vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) +std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) { // sort data by levels, must be improve the coalescence @@ -69,7 +69,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc real_t * d_error; cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); + std::vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); delete [] h_dist; cudaFree(d_error); @@ -94,7 +94,7 @@ vector > iter_error_parallel_toplesets_propagation_coalesc } /// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector & samples, size_t n, real_t radio) +double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, real_t radio) { cudaDeviceReset(); @@ -118,7 +118,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vectorn_vertices); - vector limits; + std::vector limits; index_t * toplesets = new index_t[mesh->n_vertices]; index_t * sorted_index = new index_t[mesh->n_vertices]; @@ -209,7 +209,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const vector & sources, const vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) +std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) @@ -221,10 +221,10 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - vector > iter_error; + std::vector > iter_error; iter_error.reserve(limits.size()); - ofstream os("band"); + std::ofstream os("band"); index_t d = 0; index_t start, end, n_cond; @@ -240,12 +240,12 @@ vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], end, start); // print band info - os << n_iter << " " << i << " " << j << " " << end - start << endl; + os << n_iter << " " << i << " " << j << " " << end - start << std::endl; // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(j == limits.size() - 1) - iter_error.push_back(make_pair(n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size()))); + iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size())}); // end relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond); diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp index 777816e0..429736da 100644 --- a/src/gproshan/laplacian/laplacian.cpp +++ b/src/gproshan/laplacian/laplacian.cpp @@ -52,8 +52,8 @@ size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat { laplacian(mesh, L, A); - string feigval = tmp_file_path(mesh->name_size() + ".eigval"); - string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); + std::string feigval = tmp_file_path(mesh->name_size() + ".eigval"); + std::string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) { diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index 96561634..f13a5cd9 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -20,7 +20,7 @@ const size_t & basis::dim() const void basis::plot_basis() { - string file = tmp_file_path("basis.gpi"); + std::string file = tmp_file_path("basis.gpi"); ofstream os(file); os << "set term qt size 1000,1000;" << endl; @@ -51,7 +51,7 @@ void basis::plot_atoms(const a_mat & A) size_t s = sqrt(m); s += !(s * s == K); - string file = tmp_file_path("atoms.gpi"); + std::string file = tmp_file_path("atoms.gpi"); ofstream os(file); os << "set term qt size 1000,1000;" << endl; @@ -84,7 +84,7 @@ void basis::plot_atoms(const a_mat & A) void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) { a_mat tmp = xyz.t(); - string data = tmp_file_path("xyz_" + to_string(p) + ".dat"); + std::string data = tmp_file_path("xyz_" + to_std::string(p) + ".dat"); tmp.save(data.c_str(), arma::arma_ascii); size_t K = A.n_rows; @@ -92,7 +92,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) size_t s = sqrt(m); s += !(s * s == K); - string file = tmp_file_path("atoms_patch_"+ to_string(p) + ".gpi"); + std::string file = tmp_file_path("atoms_patch_"+ to_std::string(p) + ".gpi"); ofstream os(file); os << "set term qt size 1000,1000;" << endl; @@ -104,7 +104,7 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) os << "unset key;" << endl; os << "set pm3d at b;" << endl; os << "unset colorbox;" << endl; - os << "splot \"xyz_" << to_string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; + os << "splot \"xyz_" << to_std::string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; for(index_t i = 0; i < m; ++i) { diff --git a/src/gproshan/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp index eefd5ed9..63d3078a 100644 --- a/src/gproshan/mdict/image_denoising.cpp +++ b/src/gproshan/mdict/image_denoising.cpp @@ -10,7 +10,7 @@ using namespace cimg_library; namespace gproshan::mdict { -void test_image_denoising(const string & file) +void test_image_denoising(const std::string & file) { CImg image(file.c_str()); image.resize(128, 128); @@ -93,7 +93,7 @@ void test_image_denoising(const string & file) gproshan_log_var(time); TIC(time) - vector locval; + std::vector locval; a_mat spY = D * OMP_all(locval, X, D, L); TOC(time) diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 96918359..4bdbcccd 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -27,7 +27,7 @@ std::ostream & operator << (std::ostream & os, const locval_t & lc) return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; } -void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L) +void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L) { a_vec aa; arma::uvec selected_atoms; @@ -41,7 +41,7 @@ void OMP(vector & alpha, const a_vec & x, const index_t & i, const a_m } } -a_sp_mat OMP_all(vector & locval, const a_mat & X, const a_mat & D, const size_t & L) +a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t & L) { locval.clear(); @@ -72,8 +72,8 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) a_mat R, E, U, V; a_vec s; - vector locval; - vector rows; + std::vector locval; + std::vector rows; rows.reserve(m + 1); while(k--) @@ -248,7 +248,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } -a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, const size_t & L) +a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t & L) { a_mat alpha(A.n_cols, patches.size()); @@ -259,7 +259,7 @@ a_mat OMP_all(const vector & patches, basis * phi_basis, const a_mat & A, return alpha; } -a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) +a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t & L) { a_mat alpha(A.n_cols, patches.size()); @@ -270,7 +270,7 @@ a_mat OMP_all(const vector & patches, const a_mat & A, const size_t & L) return alpha; } -void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) +void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k) { size_t K = A.n_rows; @@ -322,12 +322,12 @@ void KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) // MESH SPARSE -void OMP(vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L) +void OMP(std::vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L) { OMP(alpha, p.xyz.row(2).t(), i, p.phi * A, L); } -a_sp_mat OMP_all(vector & locval, const vector & patches, const a_mat & A, const size_t & L) +a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t & L) { locval.clear(); @@ -349,7 +349,7 @@ a_sp_mat OMP_all(vector & locval, const vector & patches, const return a_sp_mat(DI, DV, A.n_cols, patches.size()); } -void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t k) +void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k) { size_t K = A.n_rows; @@ -359,8 +359,8 @@ void sp_KSVD(a_mat & A, const vector & patches, const size_t & L, size_t real_t aj; - vector locval; - vector rows; + std::vector locval; + std::vector rows; rows.reserve(A.n_cols + 1); while(k--) diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 02fbec0c..21768bee 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -26,7 +26,7 @@ msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, co m_params.n_patches = mesh->n_vertices / m_params.avg_p; - key_name = mesh->name_size() + '_' + to_string(m_params.delta) + '_' + to_string(m_params.sum_thres) + '_' + to_string(m_params.area_thres); + key_name = mesh->name_size() + '_' + to_std::string(m_params.delta) + '_' + to_std::string(m_params.sum_thres) + '_' + to_std::string(m_params.area_thres); mask = new bool[mesh->n_vertices]; memset(mask, 0, sizeof(bool) * mesh->n_vertices); @@ -44,8 +44,8 @@ msparse_coding::operator const std::string & () const void msparse_coding::load_mask() { - //string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(avg_p) + '_' + to_string(percent) + '_' + to_string(radio) + ".msk"); - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.percent) + ".msk"); + //std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(avg_p) + '_' + to_std::string(percent) + '_' + to_std::string(radio) + ".msk"); + std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(m_params.percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -83,7 +83,7 @@ void msparse_coding::load_mask() void msparse_coding::load_mask(const std::vector * vertices, const index_t * clusters) { - string f_mask = tmp_file_path(mesh->name_size() + '_' + to_string(m_params.avg_p) + '_' + to_string(m_params.percent) + ".msk"); + std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(m_params.avg_p) + '_' + to_std::string(m_params.percent) + ".msk"); arma::uvec V; if(V.load(f_mask)) @@ -149,8 +149,8 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde void msparse_coding::load_sampling() { size_t featsize; - vector all_sorted_features; - vector seeds; + std::vector all_sorted_features; + std::vector seeds; load_features(all_sorted_features, featsize); gproshan_debug_var(all_sorted_features.size()); @@ -193,8 +193,8 @@ void msparse_coding::load_sampling() real_t euc_radio; real_t geo_radio; - vector radios; - vector geo_radios; + std::vector radios; + std::vector geo_radios; size_t count_cov = 0; size_t count_cov_patch = 0; @@ -261,7 +261,7 @@ void msparse_coding::load_sampling() #ifndef NDEBUG - vector outliers; + std::vector outliers; for(index_t i = 0; i < mesh->n_vertices; ++i) @@ -577,7 +577,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) gproshan_debug_var(total_points); - vector point_cloud; + std::vector point_cloud; point_cloud.reserve(total_points); for(index_t i = 0; i < m_params.n_patches; ++i) @@ -609,7 +609,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) } } - che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_string(per) + '_' + to_string(fr) + "_pc"), che_off::NOFF); + che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_std::string(per) + '_' + to_std::string(fr) + "_pc"), che_off::NOFF); gproshan_debug(saved new point cloud); @@ -662,7 +662,7 @@ void msparse_coding::learning() { gproshan_log(MDICT); - string f_dict = tmp_file_path(mesh->name_size() + '_' + to_string(phi_basis->dim()) + '_' + to_string(m_params.n_atoms) + '_' + to_string(m_params.f) + '_' + to_string(L) + ".dict"); + std::string f_dict = tmp_file_path(mesh->name_size() + '_' + to_std::string(phi_basis->dim()) + '_' + to_std::string(m_params.n_atoms) + '_' + to_std::string(m_params.f) + '_' + to_std::string(L) + ".dict"); if(m_params.learn) { @@ -707,7 +707,7 @@ void msparse_coding::sparse_coding() { gproshan_log(MDICT); - vector locval; + std::vector locval; alpha = OMP_all(patches, phi_basis, A, L); } @@ -737,9 +737,9 @@ void msparse_coding::init_sampling() gproshan_debug_var(phi_basis->radio()); } -void msparse_coding::load_features(vector & v_feat, size_t & featsize) +void msparse_coding::load_features(std::vector & v_feat, size_t & featsize) { - string f_feat = tmp_file_path(mesh->name() + ".int"); + std::string f_feat = tmp_file_path(mesh->name() + ".int"); ifstream inp; inp.open(f_feat.c_str(), ifstream::in); @@ -754,7 +754,7 @@ void msparse_coding::load_features(vector & v_feat, size_t & featsize) //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. - string command = "../../harris3d/harris3d " + mesh->filename + " " + f_feat + " " + tmp_file_path("example.prop"); + std::string command = "../../harris3d/harris3d " + mesh->filename + " " + f_feat + " " + tmp_file_path("example.prop"); gproshan_debug_var(command); system(command.c_str()); gproshan_debug(created); @@ -1019,7 +1019,7 @@ const index_t & msparse_coding::draw_patches(const index_t & p) return patches[p].vertices[0]; } -void msparse_coding::save_alpha(string file) +void msparse_coding::save_alpha(std::string file) { alpha.save(file); } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index c37742bd..72a0a099 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -46,7 +46,7 @@ void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, cons if(!_toplevel) delete [] toplevel; } -void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, vector & _vertices, index_t * _toplevel) +void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, std::vector & _vertices, index_t * _toplevel) { radio = 1; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -77,7 +77,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(vertex & n, vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) +bool patch::add_vertex_by_faces(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -250,7 +250,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, vertices.push_back(v); - vector N; + std::vector N; N.push_back(n); real_t area = 0; @@ -318,7 +318,7 @@ void patch::itransform() xyz.each_col() += x; } -void patch::reset_xyz(che * mesh, vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz(che * mesh, std::vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); @@ -368,7 +368,7 @@ void patch::remove_extra_xyz_disjoint(size_t & max_points) } } -void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, const index_t & p) +void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t & p) { size_t m = std::max (vertices.size(), min_nv); @@ -460,7 +460,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, vector & vpatches, co } } -void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, const index_t & p, const fmask_t & mask) { size_t m = vertices.size(); if(mask) @@ -550,7 +550,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, if(vertices.size()) vertices.clear(); vertices.reserve(expected_nv); - priority_queue > qvertices; + priority_queue > qvertices; memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); @@ -593,7 +593,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) //size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; //assert(vertices.size() > min_points); - vector in_points; + std::vector in_points; in_points.reserve(vertices.size()); for(const index_t & u: vertices) in_points.push_back(DPoint(mesh->point(u).x(), mesh->point(u).y(), mesh->point(u).z())); @@ -703,10 +703,10 @@ void patch::save_z(ostream & os) os< & vpatches, const index_t & p) +void patch::compute_avg_distance(che * mesh, std::vector & vpatches, const index_t & p) { avg_dist = INFINITY; - vector distances; + std::vector distances; for(size_t i = 0; i < vertices.size(); ++i) { diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 841f9343..c5146625 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -257,7 +257,7 @@ mat4 che::normalize_box(const real_t & side) const } ///< vcommon correspond to the first vertices of the mesh with indices to the main mesh (this) -che * che::merge(const che * mesh, const vector & vcommon) +che * che::merge(const che * mesh, const std::vector & vcommon) { const size_t & n_vcommon = vcommon.size(); const size_t & n_vnew = mesh->n_vertices - n_vcommon; @@ -380,7 +380,7 @@ void che::multiplicate_vertices() } } -void che::remove_vertices(const vector & vertices) +void che::remove_vertices(const std::vector & vertices) { if(!vertices.size()) return; @@ -402,9 +402,9 @@ void che::remove_vertices(const vector & vertices) EVT[v] = NIL; } /* save in vectors */ - vector new_vertices; - vector removed; - vector new_faces; // each 3 + std::vector new_vertices; + std::vector removed; + std::vector new_faces; // each 3 gproshan_debug(removing vertex); for(index_t v = 0; v < n_vertices; ++v) @@ -461,9 +461,9 @@ void che::remove_non_manifold_vertices() } /* save in vectors */ - vector new_vertices; - vector removed; - vector new_faces; // each 3 + std::vector new_vertices; + std::vector removed; + std::vector new_faces; // each 3 gproshan_debug(removing vertex); for(index_t v = 0; v < n_vertices; ++v) @@ -603,11 +603,11 @@ che::star_he che::star(const index_t & v) const return {this, v}; } -vector che::link(const index_t & v) const +std::vector che::link(const index_t & v) const { assert(v < n_vertices); - vector vlink; + std::vector vlink; if(is_vertex_bound(v)) vlink.push_back(VT[next(EVT[v])]); @@ -623,7 +623,7 @@ void che::edge_collapse(const std::vector & sort_edges) // TODO } -void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector & limits, const vector & sources, const index_t & k) +void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector & limits, const std::vector & sources, const index_t & k) { if(!sources.size()) return; @@ -670,12 +670,12 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, vector che::bounds() const +std::vector che::bounds() const { if(!n_faces) return {}; if(!manifold) return {}; - vector vbounds; + std::vector vbounds; bool * is_bound = new bool[n_vertices]; memset(is_bound, 0, sizeof(bool) * n_vertices); @@ -695,9 +695,9 @@ vector che::bounds() const } ///< return a vector of the indices of the boundary where v belongs -vector che::boundary(const index_t & v) const +std::vector che::boundary(const index_t & v) const { - vector vbound; + std::vector vbound; index_t he_end = EVT[v]; index_t he = he_end; @@ -726,21 +726,21 @@ bool che::is_edge_bound(const index_t & e) const // file, name, and system methods -const string che::name() const +const std::string che::name() const { index_t p = filename.find_last_of('/'); index_t q = filename.find_last_of('.'); return filename.substr(p + 1, q - p - 1); } -const string che::name_size() const +const std::string che::name_size() const { - return name() + "_" + to_string(n_vertices); + return name() + "_" + to_std::string(n_vertices); } -const string che::filename_size() const +const std::string che::filename_size() const { - return filename + "_" + to_string(n_vertices); + return filename + "_" + to_std::string(n_vertices); } @@ -950,7 +950,7 @@ void che::init(const vertex * vertices, const index_t & n_v, const index_t * fac update_eht(); } -void che::init(const string & file) +void che::init(const std::string & file) { filename = file; read_file(filename); @@ -993,7 +993,7 @@ void che::free() delete [] VHC; VHC = nullptr; } -void che::read_file(const string & ) {} /* virtual */ +void che::read_file(const std::string & ) {} /* virtual */ void che::update_evt_ot_et() { @@ -1002,13 +1002,13 @@ void che::update_evt_ot_et() memset(EVT, -1, sizeof(index_t) * n_vertices); memset(OT, -1, sizeof(index_t) * n_half_edges); - vector vnhe; + std::vector vnhe; vnhe.assign(n_vertices, 0); for(index_t he = 0; he < n_half_edges; ++he) ++vnhe[VT[he]]; - vector vhe(n_vertices); + std::vector vhe(n_vertices); vhe[0] = new index_t[n_half_edges]; for(index_t v = 1; v < n_vertices; ++v) vhe[v] = vhe[v - 1] + vnhe[v - 1]; @@ -1078,9 +1078,9 @@ void che::update_eht() // static -vector che::trig_convex_polygon(const index_t * P, const size_t & n) +std::vector che::trig_convex_polygon(const index_t * P, const size_t & n) { - vector trigs; + std::vector trigs; trigs.reserve(che::mtrig * (n - 2)); index_t a = n - 1; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 4d0d4329..6836ebb5 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -15,16 +15,16 @@ bool operator<(const border_t & a, const border_t & b) return a.theta > b.theta; } -a_vec normal_face(const vector & tmp_vertices, const index_t & a_v, const index_t & b_v, const index_t & c_v) +a_vec normal_face(const std::vector & tmp_vertices, const index_t & a_v, const index_t & b_v, const index_t & c_v) { a_vec a = tmp_vertices[c_v] - tmp_vertices[a_v]; a_vec b = tmp_vertices[b_v] - tmp_vertices[a_v]; return normalise(cross(a,b)); } -che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, const size_t & max_iter = 1000) +che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vertices, const size_t & max_iter = 1000) { - vector vertices; + std::vector vertices; vertex normal, normal_v, edge_v, v; a_mat E(3, 3); a_vec ve(3); @@ -58,10 +58,10 @@ che * mesh_simple_fill_hole(che * mesh, const vector & border_vertices, return fill_hole_front_angles(vertices, mesh->mean_edge(), normal, max_iter); } -che * mesh_fill_hole(che * mesh, const vector & border_vertices, const size_t & max_iter, const vector > & split_indices = {}) +che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, const size_t & max_iter, const std::vector > & split_indices = {}) { - vector vertices[2]; - vector merge_vertices[2]; + std::vector vertices[2]; + std::vector merge_vertices[2]; vertex normal; size_t size = border_vertices.size(); @@ -74,7 +74,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const index_t c = 1; real_t mean_edge = mesh->mean_edge(); - auto gen_vertices = [&mean_edge](vector & merge_vertices, vector & vertices, const vertex & va, const vertex & vb, const index_t & delta_v = 0) + auto gen_vertices = [&mean_edge](std::vector & merge_vertices, std::vector & vertices, const vertex & va, const vertex & vb, const index_t & delta_v = 0) { real_t L = length(va - vb); size_t N = L / mean_edge; @@ -205,7 +205,7 @@ che * mesh_fill_hole(che * mesh, const vector & border_vertices, const return hole; } -void split_border(vector > & split_indices, che * mesh, const vector & border_vertices) +void split_border(std::vector > & split_indices, che * mesh, const std::vector & border_vertices) { size_t n = border_vertices.size(); a_mat data(3, n); @@ -255,10 +255,10 @@ void split_border(vector > & split_indices, che * mesh, c } } -vector * fill_all_holes(che * mesh, const size_t & max_iter) +std::vector * fill_all_holes(che * mesh, const size_t & max_iter) { gproshan_error(holes); - vector * border_vertices; + std::vector * border_vertices; che ** holes; gproshan_error(holes); @@ -276,17 +276,17 @@ vector * fill_all_holes(che * mesh, const size_t & max_iter) return border_vertices; } -tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter) +tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter) { - vector * border_vertices = nullptr; + std::vector * border_vertices = nullptr; che ** holes = nullptr; - vector bounds = mesh->bounds(); + std::vector bounds = mesh->bounds(); const size_t n_borders = bounds.size(); if(!n_borders) return make_tuple(border_vertices, holes); - border_vertices = new vector[n_borders]; + border_vertices = new std::vector[n_borders]; holes = new che*[n_borders]; gproshan_debug(inpainting); @@ -301,12 +301,12 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t { gproshan_error(holes); gproshan_debug_var(b); -// vector > split_indices; +// std::vector > split_indices; // split_border(split_indices, mesh, border_vertices[b]); // holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter, { {77, 106}, {67, 106}, {38, 11} }); holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter); gproshan_debug(inpainting); - if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + to_string(b) + "_" + mesh->name() + ".off")); + if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + to_std::string(b) + "_" + mesh->name() + ".off")); gproshan_debug(inpainting); gproshan_error(holes); } @@ -324,7 +324,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t return make_tuple(border_vertices, holes); } -che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, size_t p_iter, bool & is_grow) +che * fill_hole_front_angles_test(che * mesh, std::vector & front_vertices, size_t p_iter, bool & is_grow) { gproshan_debug(filling holes); real_t perimeter = 0.0, init_perimeter = 0.0; @@ -332,14 +332,14 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, real_t length = mesh->mean_edge(); priority_queue front; - vector vertices; - vector faces; + std::vector vertices; + std::vector faces; for(index_t v: front_vertices) vertices.push_back(mesh->point(v)); - vector tmp_vertices(vertices.size()); - vector tmp_normals(vertices.size()); + std::vector tmp_vertices(vertices.size()); + std::vector tmp_normals(vertices.size()); vertex normal; for(index_t v = 0; v < vertices.size(); ++v) @@ -368,8 +368,8 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, bool o = is_grow; - vector is_border(vertices.size()); - vector > neighbors(vertices.size()); + std::vector is_border(vertices.size()); + std::vector > neighbors(vertices.size()); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) @@ -564,14 +564,14 @@ che * fill_hole_front_angles_test(che * mesh, vector & front_vertices, return faces.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); } -che * fill_hole_front_angles(vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) +che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) { size_t p_iter = max_iter; real_t perimeter = 0.0; real_t init_perimeter = 0.0; priority_queue front; - vector faces; + std::vector faces; // PCA -------------------------------------------------------------------------- @@ -624,9 +624,9 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c // END PCA ---------------------------------------------------------------------- - vector tmp_vertices(vertices.size()); - vector is_border(vertices.size()); - vector > neighbors(vertices.size()); + std::vector tmp_vertices(vertices.size()); + std::vector is_border(vertices.size()); + std::vector > neighbors(vertices.size()); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) @@ -824,7 +824,7 @@ che * fill_hole_front_angles(vector & vertices, const real_t & length, c return faces.size() ? new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3) : nullptr; } -void get_real_tri(che * mesh, vector & select_vertices, vector & triangle, vector & tri_sizes ) +void get_real_tri(che * mesh, std::vector & select_vertices, std::vector & triangle, std::vector & tri_sizes ) { // Drawing a triangle in the middle of the border size_t div = select_vertices.size() / 3; @@ -878,7 +878,7 @@ void get_real_tri(che * mesh, vector & select_vertices, vector triangle.push_back( (wp * tri[2]) + wo * (tri[0] + tri[1]) ); } -che * fill_hole_center_triangle(che * mesh, vector & select_vertices, index_t index) +che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index) { size_t n_vertices = select_vertices.size() + 3; size_t n_faces = select_vertices.size() + 4; @@ -886,8 +886,8 @@ che * fill_hole_center_triangle(che * mesh, vector & select_vertices, i vertex * vertices = new vertex[n_vertices]; index_t * faces = new index_t[n_faces * che::mtrig]; - vector triangle; - vector tri_sizes(3,0); + std::vector triangle; + std::vector tri_sizes(3,0); get_real_tri(mesh, select_vertices, triangle, tri_sizes); diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index a0f66423..58df1285 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -15,12 +15,12 @@ using namespace cimg_library; namespace gproshan { -che_img::che_img(const string & file) +che_img::che_img(const std::string & file) { init(file); } -void che_img::read_file(const string & file) +void che_img::read_file(const std::string & file) { CImg img(file.c_str()); diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 3283ca03..84355d40 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -9,12 +9,12 @@ namespace gproshan { -che_obj::che_obj(const string & file) +che_obj::che_obj(const std::string & file) { init(file); } -void che_obj::read_file(const string & file) +void che_obj::read_file(const std::string & file) { FILE * fp = fopen(file.c_str(), "r"); assert(fp); @@ -22,9 +22,9 @@ void che_obj::read_file(const string & file) float x, y, z, r, g, b; index_t P[32], n; - vector vertices; - vector vertices_color; - vector faces; + std::vector vertices; + std::vector vertices_color; + std::vector faces; char line[256], str[64]; char * line_ptr; @@ -70,7 +70,7 @@ void che_obj::read_file(const string & file) memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } -void che_obj::write_file(const che * mesh, const string & file, const bool & color, const bool & pointcloud) +void che_obj::write_file(const che * mesh, const std::string & file, const bool & color, const bool & pointcloud) { FILE * fp = fopen((file + ".obj").c_str(), "w"); assert(fp); diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 3ed89a4d..87c55505 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -9,12 +9,12 @@ namespace gproshan { -che_off::che_off(const string & file) +che_off::che_off(const std::string & file) { init(file); } -void che_off::read_file(const string & file) +void che_off::read_file(const std::string & file) { char soff[32]; size_t nv, nf, n; @@ -47,7 +47,7 @@ void che_off::read_file(const string & file) } } - vector faces; + std::vector faces; faces.reserve(che::mtrig * n_faces); index_t P[32]; @@ -81,7 +81,7 @@ void che_off::read_file(const string & file) memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } -void che_off::write_file(const che * mesh, const string & file, const che_off::type & off, const bool & pointcloud) +void che_off::write_file(const che * mesh, const std::string & file, const che_off::type & off, const bool & pointcloud) { static const char * str_off[] = {"OFF", "NOFF", "COFF", "NCOFF"}; diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 38148264..7f574204 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -10,14 +10,14 @@ namespace gproshan { -che_ply::che_ply(const string & file) +che_ply::che_ply(const std::string & file) { init(file); } -void che_ply::read_file(const string & file) +void che_ply::read_file(const std::string & file) { - map bytes = { + map bytes = { {"char", 1}, {"uchar", 1}, {"short", 2}, @@ -91,7 +91,7 @@ void che_ply::read_file(const string & file) alloc(nv, nf); - vector faces; + std::vector faces; faces.reserve(che::mtrig * n_faces); if(format[0] == 'a') // ascii @@ -220,7 +220,7 @@ void che_ply::read_file(const string & file) memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); } -void che_ply::write_file(const che * mesh, const string & file, const bool & color) +void che_ply::write_file(const che * mesh, const std::string & file, const bool & color) { FILE * fp = fopen((file + ".ply").c_str(), "wb"); assert(fp); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index 4ade6482..ebf28f0b 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -95,18 +95,18 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) } //fill one hole and fit with biharmonic_interp_2 -void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const vector & border_vertices, const index_t & k) +void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t & k) { if(old_n_vertices == n_vertices) return; index_t * rings = new index_t[mesh->n_vertices]; index_t * sorted = new index_t[mesh->n_vertices]; - vector limites; + std::vector limites; mesh->compute_toplesets(rings, sorted, limites, border_vertices, k); const size_t n_border_vertices = limites.back(); - vector sub_mesh_hole; + std::vector sub_mesh_hole; sub_mesh_hole.reserve(n_border_vertices); for(index_t b = 0; b < n_border_vertices; ++b) diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index 836401b7..7b80a020 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -9,12 +9,12 @@ namespace gproshan { -che_pts::che_pts(const string & file) +che_pts::che_pts(const std::string & file) { init(file); } -void che_pts::read_file(const string & file) +void che_pts::read_file(const std::string & file) { FILE * fp = fopen(file.c_str(), "r"); assert(fp); @@ -43,7 +43,7 @@ void che_pts::read_file(const string & file) fclose(fp); } -void che_pts::write_file(const che * mesh, const string & file) +void che_pts::write_file(const che * mesh, const std::string & file) { FILE * fp = fopen((file + ".pts").c_str(), "w"); assert(fp); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 2a55eb0b..a4517f13 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -14,12 +14,12 @@ using namespace cimg_library; namespace gproshan { -che_ptx::che_ptx(const string & file) +che_ptx::che_ptx(const std::string & file) { init(file); } -void che_ptx::read_file(const string & file) +void che_ptx::read_file(const std::string & file) { FILE * fp = fopen(file.c_str(), "r"); assert(fp); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 44368d0d..fd0106a4 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -9,12 +9,12 @@ namespace gproshan { -che_xyz::che_xyz(const string & file) +che_xyz::che_xyz(const std::string & file) { init(file); } -void che_xyz::read_file(const string & file) +void che_xyz::read_file(const std::string & file) { FILE * fp = fopen(file.c_str(), "r"); assert(fp); @@ -24,8 +24,8 @@ void che_xyz::read_file(const string & file) unsigned char r, g, b; size_t n; - vector vertices; - vector vertices_color; + std::vector vertices; + std::vector vertices_color; while(fgets(line, sizeof(line), fp)) { @@ -44,7 +44,7 @@ void che_xyz::read_file(const string & file) memcpy(VC, vertices_color.data(), n_vertices * sizeof(rgb_t)); } -void che_xyz::write_file(const che * mesh, const string & file, const bool & color) +void che_xyz::write_file(const che * mesh, const std::string & file, const bool & color) { FILE * fp = fopen((file + ".xyz").c_str(), "w"); assert(fp); diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index fd41b73f..eaf12964 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -137,7 +137,7 @@ void che_viewer::update_vbo_heatmap(const real_t * vheatmap) glBindVertexArray(0); } -void che_viewer::update_instances_positions(const vector & translations) +void che_viewer::update_instances_positions(const std::vector & translations) { n_instances = translations.size(); if(!n_instances) return; diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index 20d95ec6..f5f46bb4 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -16,7 +16,7 @@ shader::~shader() glDeleteProgram(program); } -const GLint & shader::operator () (const string & name) +const GLint & shader::operator () (const std::string & name) { if(uniform.find(name) != uniform.end()) uniform[name] = glGetUniformLocation(program, name.c_str()); @@ -62,7 +62,7 @@ void shader::disable() const bool shader::load(GLenum shader_type, const std::string & filename) { - string source; + std::string source; if(!read_source(filename, source)) { @@ -126,10 +126,10 @@ bool shader::read_source(const std::string & filename, std::string & source) source = ""; - string line, include; + std::string line, include; while(getline(is, line)) { - stringstream ss(line); + std::stringstream ss(line); ss >> include; if(include == "#include") diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 5ffa9880..2237915c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -127,7 +127,7 @@ void viewer::imgui() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + if(ImGui::MenuItem((to_std::string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; glfwSetWindowTitle(window, mesh->filename.c_str()); @@ -392,7 +392,7 @@ void viewer::init_glsl() shader_pointcloud.load_fragment(shaders_path("fragment_pointcloud.glsl")); } -void viewer::add_process(const int & key, const string & skey, const string & name, const function_t & f) +void viewer::add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f) { if(processes.find(key) == processes.end()) { @@ -568,11 +568,11 @@ bool viewer::m_save_load_view(viewer * view) } static index_t select = 0; - static vector vfiles; + static std::vector vfiles; vfiles.clear(); for(auto & p: filesystem::directory_iterator(tmp_file_path("views/"))) - vfiles.push_back(p.path().string()); + vfiles.push_back(p.path().std::string()); if(!vfiles.size()) return true; From 5f913baee80b20241c223e1cdf0e274e434e95a1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 10 Nov 2022 14:25:37 +0100 Subject: [PATCH 0734/1018] adding std:: --- include/gproshan/geometry/mat.h | 2 +- include/gproshan/geometry/vec.h | 2 +- include/gproshan/mdict/mdict.h | 2 +- include/gproshan/mdict/patch.h | 4 +- src/gproshan/app_viewer.cpp | 8 +- src/gproshan/geodesics/dijkstra.cpp | 4 +- src/gproshan/geodesics/geodesics.cpp | 8 +- src/gproshan/geodesics/geodesics_ptp.cpp | 2 +- src/gproshan/geodesics/sampling.cpp | 12 +-- src/gproshan/geodesics/test_geodesics_ptp.cpp | 32 ++++---- src/gproshan/mdict/basis.cpp | 82 +++++++++---------- src/gproshan/mdict/basis_cosine.cpp | 10 +-- src/gproshan/mdict/basis_dct.cpp | 12 +-- src/gproshan/mdict/mdict.cpp | 4 +- src/gproshan/mdict/msparse_coding.cpp | 38 ++++----- src/gproshan/mdict/patch.cpp | 8 +- src/gproshan/mesh/che.cpp | 18 ++-- src/gproshan/mesh/che_fill_hole.cpp | 10 +-- src/gproshan/mesh/che_ply.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 2 +- src/gproshan/viewer/camera.cpp | 2 +- src/gproshan/viewer/shader.cpp | 6 +- src/gproshan/viewer/viewer.cpp | 8 +- 23 files changed, 139 insertions(+), 139 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 31184361..f46e7f92 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -92,7 +92,7 @@ class mat } }; -///< std ostream +///< std std::ostream template std::ostream & operator << (std::ostream & os, const mat & m) { diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 6f0b91b4..34d0581d 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -326,7 +326,7 @@ vec normalize(const vec & v) return v / norm(v); } -///< std ostream +///< std std::ostream template __host__ __device__ std::ostream & operator << (std::ostream & os, const vec & v) diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 82414dc6..78f2d1de 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -31,7 +31,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); // DENSE -tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L); +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L); a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L); a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask); diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 5a8b60c3..84e80d9f 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -24,7 +24,7 @@ namespace gproshan::mdict { class msparse_coding; -typedef function fmask_t; +typedef std::function fmask_t; typedef std::map vpatches_t; /// @@ -141,7 +141,7 @@ class patch real_t get_min_z(); real_t get_max_z(); - void save_z(ostream & os); + void save_z(std::ostream & os); index_t find(const index_t * indexes, size_t nc, index_t idx_global); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 5398b64d..ef04316a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -240,8 +240,8 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); gv(v) = mesh->mean_curvature(v); - g_max = max(g_max, gv(v)); - g_min = min(g_min, gv(v)); + g_max = std::max(g_max, gv(v)); + g_min = std::min(g_min, gv(v)); } g = g_max - g_min; @@ -282,7 +282,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) che_viewer & mesh = view->active_mesh(); index_t levels; - cin >> levels; + std::cin >> levels; TIC(view->time) simplification sampling(mesh, levels); TOC(view->time) gproshan_debug_var(view->time); @@ -756,7 +756,7 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { mesh->heatmap(v) = features(v); - max_s = max(max_s, mesh->heatmap(v)); + max_s = std::max(max_s, mesh->heatmap(v)); } #pragma omp parallel for diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index 742af383..4b074da9 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -40,10 +40,10 @@ index_t & dijkstra::operator[](index_t i) return predecessors[i]; } -void dijkstra::print(ostream & os) +void dijkstra::print(std::ostream & os) { for(index_t i = 0; i < n_vertices; ++i) - os< & source size_t green_count = n_iter ? n_iter : n_vertices; - priority_queue, + std::priority_queue, std::vector >, - greater > > Q; + std::greater > > Q; real_t dv, dp; vertex vx; @@ -137,7 +137,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dist[s] = 0; if(clusters) clusters[s] = ++c; color[s] = RED; - Q.push(make_pair(dist[s], s)); + Q.push({dist[s], s}); } while(green_count-- && !Q.empty()) @@ -178,7 +178,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source } if(dv < dist[v]) - Q.push(make_pair(dist[v] = dv, v)); + Q.push({dist[v] = dv, v}); } } } diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index f7806faf..0b0dfaa7 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -269,7 +269,7 @@ void normalize_ptp(real_t * dist, const size_t & n) #pragma omp parallel for reduction(max: max_d) for(index_t v = 0; v < n; ++v) if(dist[v] < INFINITY) - max_d = max(dist[v], max_d); + max_d = std::max(dist[v], max_d); #pragma omp parallel for for(index_t v = 0; v < n; ++v) diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index 61bf5061..cdfc101c 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -40,9 +40,9 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si { const std::string & filename = mesh->filename; - std::string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + to_std::string(n); + std::string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + std::to_string(n); - ifstream is(tmp_file_path(file)); + std::ifstream is(tmp_file_path(file)); gproshan_log_var(tmp_file_path(file)); if(is.good()) @@ -71,11 +71,11 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si radio = 0; // IMPLEMENT: farthest_point_sampling_ptp_cpu(mesh, points, time_fps, n); #endif // GPROSHAN_CUDA - ofstream os(tmp_file_path(file)); - os << radio << endl; - os << points.size() << endl; + std::ofstream os(tmp_file_path(file)); + os << radio << std::endl; + os << points.size() << std::endl; for(const index_t & i: points) - os << i << endl; + os << i << std::endl; os.close(); } diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 504bd262..9de8e100 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -38,7 +38,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) const char * pberror = "& %6s %6.2lf\\%% "; std::string filename; - while(cin >> filename) + while(std::cin >> filename) { gproshan_debug_var(filename); @@ -154,7 +154,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // DEGREE HISTOGRAM ________________________________________________________________________ index_t dv; - map deg; + std::map deg; for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->is_vertex_bound(v) ? 1 : 0; @@ -162,9 +162,9 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) ++deg[dv]; } - ofstream os(test_path + filename + ".deg"); + std::ofstream os(test_path + filename + ".deg"); for(auto & ii: deg) - os << ii.first << " " << ii.second << endl; + os << ii.first << " " << ii.second << std::endl; os.close(); @@ -176,7 +176,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(index_t i = 1; i < limits.size(); ++i) { toplesets_dist[i - 1] = limits[i] - limits[i - 1]; - os << i - 1 << " " << toplesets_dist[i - 1] << endl; + os << i - 1 << " " << toplesets_dist[i - 1] << std::endl; } os.close(); @@ -184,7 +184,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) os.open(test_path + filename + "_toplesets_sorted.dist"); for(index_t i = 0; i < limits.size() - 1; ++i) - os << i << " " << toplesets_dist[i] << endl; + os << i << " " << toplesets_dist[i] << std::endl; os.close(); @@ -204,7 +204,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) #endif for(auto & p: iter_error) - os << p.first << " " << p.second << endl; + os << p.first << " " << p.second << std::endl; os.close(); #endif // GPROSHAN_CUDA @@ -219,7 +219,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) os.open(test_path + filename + ".fps"); for(index_t i = i_samples; i < n_samples; ++i) - os << i << " " << times_fps[i] << endl; + os << i << " " << times_fps[i] << std::endl; os.close(); delete [] times_fps; @@ -244,7 +244,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons for(int i = 0; i < n_test; ++i) { TIC(t) geodesics fm(mesh, source); TOC(t); - seconds = min(seconds, t); + seconds = std::min(seconds, t); } geodesics fm(mesh, source); @@ -262,7 +262,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std: for(int i = 0; i < n_test; ++i) { TIC(t) parallel_toplesets_propagation_cpu(dist, mesh, source, toplesets); TOC(t) - seconds = min(seconds, t); + seconds = std::min(seconds, t); } error = compute_error(dist, exact, mesh->n_vertices, source.size()); @@ -281,8 +281,8 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e for(int i = 0; i < n_test; ++i) { TIC(t) st = heat_method(dist, mesh, source, HEAT_CHOLMOD); TOC(t) - ptime = min(t - st, ptime); - stime = min(st, stime); + ptime = std::min(t - st, ptime); + stime = std::min(st, stime); } error = compute_error(dist, exact, mesh->n_vertices, source.size()); @@ -303,7 +303,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std: for(int i = 0; i < n_test; ++i) { t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); - seconds = min(seconds, t); + seconds = std::min(seconds, t); } error = compute_error(dist, exact, mesh->n_vertices, source.size()); @@ -324,8 +324,8 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact if(dist) delete [] dist; TIC(t) st = heat_method(dist, mesh, source, HEAT_CUDA); TOC(t) - ptime = min(t - st, ptime); - stime = min(st, stime); + ptime = std::min(t - st, ptime); + stime = std::min(st, stime); } error = compute_error(dist, exact, mesh->n_vertices, source.size()); @@ -340,7 +340,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact real_t * load_exact_geodesics(const std::string & file, const size_t & n) { - ifstream is(file); + std::ifstream is(file); if(!is.good()) return nullptr; diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index f13a5cd9..4b116b8b 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -21,21 +21,21 @@ const size_t & basis::dim() const void basis::plot_basis() { std::string file = tmp_file_path("basis.gpi"); - ofstream os(file); + std::ofstream os(file); - os << "set term qt size 1000,1000;" << endl; - os << "set isosamples 50,50;" << endl; - os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; - os << "set urange [-pi:pi];" << endl; - os << "unset key;" << endl; - os << "set pm3d at b;" << endl; - os << "unset colorbox;" << endl; + os << "set term qt size 1000,1000;" << std::endl; + os << "set isosamples 50,50;" << std::endl; + os << "set parametric;" << std::endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << std::endl; + os << "set urange [-pi:pi];" << std::endl; + os << "unset key;" << std::endl; + os << "set pm3d at b;" << std::endl; + os << "unset colorbox;" << std::endl; plot_basis(os); - os << "unset multiplot;" << endl; - os << "pause -1;" << endl; + os << "unset multiplot;" << std::endl; + os << "pause -1;" << std::endl; os.close(); @@ -52,27 +52,27 @@ void basis::plot_atoms(const a_mat & A) s += !(s * s == K); std::string file = tmp_file_path("atoms.gpi"); - ofstream os(file); - - os << "set term qt size 1000,1000;" << endl; - os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << endl; - os << "set isosamples 25,25;" << endl; - os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; - os << "set urange [-pi:pi];" << endl; - os << "unset key;" << endl; - os << "set pm3d at b;" << endl; - os << "unset colorbox;" << endl; + std::ofstream os(file); + + os << "set term qt size 1000,1000;" << std::endl; + os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << std::endl; + os << "set isosamples 25,25;" << std::endl; + os << "set parametric;" << std::endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << std::endl; + os << "set urange [-pi:pi];" << std::endl; + os << "unset key;" << std::endl; + os << "set pm3d at b;" << std::endl; + os << "unset colorbox;" << std::endl; for(index_t i = 0; i < m; ++i) { os << "splot v * cos(u), v * sin(u), 0 "; plot_atoms(os, A.col(i)); - os << ";" << endl; + os << ";" << std::endl; } - os << "unset multiplot;" << endl; - os << "pause -1;" << endl; + os << "unset multiplot;" << std::endl; + os << "pause -1;" << std::endl; os.close(); @@ -84,7 +84,7 @@ void basis::plot_atoms(const a_mat & A) void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) { a_mat tmp = xyz.t(); - std::string data = tmp_file_path("xyz_" + to_std::string(p) + ".dat"); + std::string data = tmp_file_path("xyz_" + std::to_string(p) + ".dat"); tmp.save(data.c_str(), arma::arma_ascii); size_t K = A.n_rows; @@ -92,29 +92,29 @@ void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) size_t s = sqrt(m); s += !(s * s == K); - std::string file = tmp_file_path("atoms_patch_"+ to_std::string(p) + ".gpi"); - ofstream os(file); + std::string file = tmp_file_path("atoms_patch_"+ std::to_string(p) + ".gpi"); + std::ofstream os(file); - os << "set term qt size 1000,1000;" << endl; - os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << endl; - os << "set isosamples 25,25;" << endl; - os << "set parametric;" << endl; - os << "set vrange [-"<< 0 << ":" << _radio <<"];" << endl; - os << "set urange [-pi:pi];" << endl; - os << "unset key;" << endl; - os << "set pm3d at b;" << endl; - os << "unset colorbox;" << endl; - os << "splot \"xyz_" << to_std::string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; + os << "set term qt size 1000,1000;" << std::endl; + os << "set multiplot layout " << s << "," << s << " rowsfirst scale 1.2;" << std::endl; + os << "set isosamples 25,25;" << std::endl; + os << "set parametric;" << std::endl; + os << "set vrange [-"<< 0 << ":" << _radio <<"];" << std::endl; + os << "set urange [-pi:pi];" << std::endl; + os << "unset key;" << std::endl; + os << "set pm3d at b;" << std::endl; + os << "unset colorbox;" << std::endl; + os << "splot \"xyz_" << std::to_string(p) << ".dat\" u 1:2:3 with points palette pointsize 2 pointtype 7,"; for(index_t i = 0; i < m; ++i) { os << " v * cos(u), v * sin(u), 0 "; plot_atoms(os, A.col(i)); - os << ";" << endl; + os << ";" << std::endl; } - os << "unset multiplot;" << endl; - os << "pause -1;" << endl; + os << "unset multiplot;" << std::endl; + os << "pause -1;" << std::endl; os.close(); diff --git a/src/gproshan/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp index 5f956790..e2543c0c 100644 --- a/src/gproshan/mdict/basis_cosine.cpp +++ b/src/gproshan/mdict/basis_cosine.cpp @@ -25,22 +25,22 @@ void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) } } -void basis_cosine::plot_basis(ostream & os) +void basis_cosine::plot_basis(std::ostream & os) { real_t d = 1.0 / (n_rot - 1); real_t c; - os << "set multiplot layout " << n_freq << "," << n_rot << " rowsfirst scale 1.2;" << endl; + os << "set multiplot layout " << n_freq << "," << n_rot << " rowsfirst scale 1.2;" << std::endl; for(size_t ni = 1; ni <= n_freq; ++ni) for(real_t alpha = 0; alpha <= 1; alpha += d) { c = ni * M_PI / _radio; - os << "splot v * cos(u), v * sin(u), "; cosine(os, c, alpha); os << ";" << endl; + os << "splot v * cos(u), v * sin(u), "; cosine(os, c, alpha); os << ";" << std::endl; } } -void basis_cosine::plot_atoms(ostream & os, const a_vec & A) +void basis_cosine::plot_atoms(std::ostream & os, const a_vec & A) { real_t d = 1.0 / (n_rot - 1); real_t c; @@ -58,7 +58,7 @@ a_vec basis_cosine::cosine(const a_vec & x, const a_vec & y, const real_t & c, c return cos(c * (alpha * x + (1 - alpha) * y)); } -void basis_cosine::cosine(ostream & os, const real_t & c, const real_t & alpha) +void basis_cosine::cosine(std::ostream & os, const real_t & c, const real_t & alpha) { os << "cos( " << c << " * (" << alpha << " * v * cos(u) + ( 1 - " << alpha << ") * v * sin(u)))"; } diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 6cbe6165..86ad5e1c 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -28,18 +28,18 @@ void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const phi.col(k) = !b ? dct(x, y, nx, ny) : dct(y, x, ny, nx); } -void basis_dct::plot_basis(ostream & os) +void basis_dct::plot_basis(std::ostream & os) { - os << "set multiplot layout " << n_freq << "," << n_freq << " rowsfirst scale 1.2;" << endl; + os << "set multiplot layout " << n_freq << "," << n_freq << " rowsfirst scale 1.2;" << std::endl; for(index_t nx = 0; nx < n_freq; ++nx) for(index_t ny = 0; ny < n_freq; ++ny) { - os << "splot v * cos(u), v * sin(u), "; dct(os, nx, ny); os << ";" << endl; + os << "splot v * cos(u), v * sin(u), "; dct(os, nx, ny); os << ";" << std::endl; } } -void basis_dct::plot_atoms(ostream & os, const a_vec & A) +void basis_dct::plot_atoms(std::ostream & os, const a_vec & A) { for(index_t k = 0, nx = 0; nx < n_freq; ++nx) for(index_t ny = 0; ny < n_freq; ++ny, ++k) @@ -59,7 +59,7 @@ a_vec basis_dct::d_dct(const a_vec & x, const a_vec & y, const index_t & nx, con return - (M_PI * nx / _radio) * (sin(M_PI * nx * x / _radio) % cos(M_PI * ny * y / _radio)); } -void basis_dct::dct(ostream & os, const index_t & nx, const index_t & ny) +void basis_dct::dct(std::ostream & os, const index_t & nx, const index_t & ny) { os << "cos( (pi * v * cos(u) * " << nx << " ) / " << _radio << " ) *"; os << "cos( (pi * v * sin(u) * " << ny << " ) / " << _radio << " )"; @@ -67,7 +67,7 @@ void basis_dct::dct(ostream & os, const index_t & nx, const index_t & ny) real_t basis_dct::freq(const index_t & idx) { - return !idx ? INFINITY : 2 * _radio / max(idx / n_freq, idx % n_freq); + return !idx ? INFINITY : 2 * _radio / std::max(idx / n_freq, idx % n_freq); } diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 4bdbcccd..159698f0 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -113,7 +113,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) // DENSE -tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) { arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; @@ -144,7 +144,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) return NIL; } -tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) { arma::uvec selected_atoms(L); diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 21768bee..d49e1bed 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -26,7 +26,7 @@ msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, co m_params.n_patches = mesh->n_vertices / m_params.avg_p; - key_name = mesh->name_size() + '_' + to_std::string(m_params.delta) + '_' + to_std::string(m_params.sum_thres) + '_' + to_std::string(m_params.area_thres); + key_name = mesh->name_size() + '_' + std::to_string(m_params.delta) + '_' + std::to_string(m_params.sum_thres) + '_' + std::to_string(m_params.area_thres); mask = new bool[mesh->n_vertices]; memset(mask, 0, sizeof(bool) * mesh->n_vertices); @@ -44,8 +44,8 @@ msparse_coding::operator const std::string & () const void msparse_coding::load_mask() { - //std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(avg_p) + '_' + to_std::string(percent) + '_' + to_std::string(radio) + ".msk"); - std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(m_params.percent) + ".msk"); + //std::string f_mask = tmp_file_path(mesh->name_size() + '_' + std::to_string(avg_p) + '_' + std::to_string(percent) + '_' + std::to_string(radio) + ".msk"); + std::string f_mask = tmp_file_path(mesh->name_size() + '_' + std::to_string(m_params.percent) + ".msk"); arma::uvec V; gproshan_log(loading radial mask); @@ -83,7 +83,7 @@ void msparse_coding::load_mask() void msparse_coding::load_mask(const std::vector * vertices, const index_t * clusters) { - std::string f_mask = tmp_file_path(mesh->name_size() + '_' + to_std::string(m_params.avg_p) + '_' + to_std::string(m_params.percent) + ".msk"); + std::string f_mask = tmp_file_path(mesh->name_size() + '_' + std::to_string(m_params.avg_p) + '_' + std::to_string(m_params.percent) + ".msk"); arma::uvec V; if(V.load(f_mask)) @@ -291,10 +291,10 @@ void msparse_coding::init_radial_feature_patches() patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); @@ -428,10 +428,10 @@ void msparse_coding::init_voronoi_patches() patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; //gproshan_debug_var(patch_avg_size); @@ -532,7 +532,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) #pragma omp parallel for reduction(max: max_radio) for(index_t i = 0; i < m_params.n_patches; ++i) - max_radio = max(max_radio, S(i, 3)); + max_radio = std::max(max_radio, S(i, 3)); A.eye(phi_basis->dim(), m_params.n_atoms); @@ -609,7 +609,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) } } - che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + to_std::string(per) + '_' + to_std::string(fr) + "_pc"), che_off::NOFF); + che_off::write_file(new_mesh, tmp_file_path(key_name + '_' + std::to_string(per) + '_' + std::to_string(fr) + "_pc"), che_off::NOFF); gproshan_debug(saved new point cloud); @@ -662,7 +662,7 @@ void msparse_coding::learning() { gproshan_log(MDICT); - std::string f_dict = tmp_file_path(mesh->name_size() + '_' + to_std::string(phi_basis->dim()) + '_' + to_std::string(m_params.n_atoms) + '_' + to_std::string(m_params.f) + '_' + to_std::string(L) + ".dict"); + std::string f_dict = tmp_file_path(mesh->name_size() + '_' + std::to_string(phi_basis->dim()) + '_' + std::to_string(m_params.n_atoms) + '_' + std::to_string(m_params.f) + '_' + std::to_string(L) + ".dict"); if(m_params.learn) { @@ -727,7 +727,7 @@ void msparse_coding::init_sampling() { sampling.reserve(m_params.n_patches); if(!gproshan::load_sampling(sampling, phi_basis->radio(), mesh, m_params.n_patches)) - cerr << "Failed to load sampling" << endl; + std::cerr << "Failed to load sampling" << std::endl; } s_radio = phi_basis->radio(); @@ -740,8 +740,8 @@ void msparse_coding::init_sampling() void msparse_coding::load_features(std::vector & v_feat, size_t & featsize) { std::string f_feat = tmp_file_path(mesh->name() + ".int"); - ifstream inp; - inp.open(f_feat.c_str(), ifstream::in); + std::ifstream inp; + inp.open(f_feat.c_str(), std::ifstream::in); size_t tam; index_t tmp; @@ -759,7 +759,7 @@ void msparse_coding::load_features(std::vector & v_feat, size_t & feats system(command.c_str()); gproshan_debug(created); inp.close(); - inp.open(f_feat.c_str(), ifstream::in); + inp.open(f_feat.c_str(), std::ifstream::in); } gproshan_debug(exists); @@ -814,10 +814,10 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); @@ -852,7 +852,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) /*Saving Patches*/ /* - ofstream os(tmp_file_path("patch-mat")); + std::ofstream os(tmp_file_path("patch-mat")); for(index_t s = 0; s < m_params.n_patches; ++s) { patch & p = patches[s]; @@ -942,7 +942,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) { dist[v] = length(new_vertices[v] - mesh->point(v)); error += dist[v]; - max_error = max(max_error, dist[v]); + max_error = std::max(max_error, dist[v]); } error /= mesh->n_vertices; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 72a0a099..4fee5c42 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -269,7 +269,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, if(add_vertex_by_faces(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { - euc_radio = max(euc_radio, norm(mesh->point(u) - c)); + euc_radio = std::max(euc_radio, norm(mesh->point(u) - c)); return true; } @@ -298,7 +298,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, p = p - c ; p = p - ((p, n) * n); - radio = max(radio, norm(p)); + radio = std::max(radio, norm(p)); } geo_radio = geo[vertices.back()]; @@ -550,7 +550,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, if(vertices.size()) vertices.clear(); vertices.reserve(expected_nv); - priority_queue > qvertices; + std::priority_queue > qvertices; memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); @@ -693,7 +693,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) } -void patch::save_z(ostream & os) +void patch::save_z(std::ostream & os) { index_t i; for( i = 0; i < vertices.size()-1; ++i) diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index c5146625..6fe39965 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -233,13 +233,13 @@ mat4 che::normalize_box(const real_t & side) const { const vertex & p = point(v); - pmin.x() = min(pmin.x(), p.x()); - pmin.y() = min(pmin.y(), p.y()); - pmin.z() = min(pmin.z(), p.z()); + pmin.x() = std::min(pmin.x(), p.x()); + pmin.y() = std::min(pmin.y(), p.y()); + pmin.z() = std::min(pmin.z(), p.z()); - pmax.x() = max(pmax.x(), p.x()); - pmax.y() = max(pmax.y(), p.y()); - pmax.z() = max(pmax.z(), p.z()); + pmax.x() = std::max(pmax.x(), p.x()); + pmax.y() = std::max(pmax.y(), p.y()); + pmax.z() = std::max(pmax.z(), p.z()); } mat4 model_mat; @@ -735,12 +735,12 @@ const std::string che::name() const const std::string che::name_size() const { - return name() + "_" + to_std::string(n_vertices); + return name() + "_" + std::to_string(n_vertices); } const std::string che::filename_size() const { - return filename + "_" + to_std::string(n_vertices); + return filename + "_" + std::to_string(n_vertices); } @@ -770,7 +770,7 @@ size_t che::max_degree() const d = 0; for([[maybe_unused]] const index_t & he: star(v)) ++d; d += is_vertex_bound(v); - md = max(md, d); + md = std::max(md, d); } return md; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 6836ebb5..c3caf6c0 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -248,7 +248,7 @@ void split_border(std::vector > & split_indices, che } if(b != a) { - cerr << b << " " << i << endl; + std::cerr << b << " " << i << std::endl; a = b; } } @@ -276,7 +276,7 @@ std::vector * fill_all_holes(che * mesh, const size_t & max_iter) return border_vertices; } -tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter) +std::tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter) { std::vector * border_vertices = nullptr; che ** holes = nullptr; @@ -306,7 +306,7 @@ tuple *, che **> fill_all_holes_meshes(che * mesh, const si // holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter, { {77, 106}, {67, 106}, {38, 11} }); holes[b] = mesh_fill_hole(mesh, border_vertices[b], max_iter); gproshan_debug(inpainting); - if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + to_std::string(b) + "_" + mesh->name() + ".off")); + if(holes[b]) che_off::write_file(holes[b], tmp_file_path("fill_holes_" + std::to_string(b) + "_" + mesh->name() + ".off")); gproshan_debug(inpainting); gproshan_error(holes); } @@ -330,7 +330,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti real_t perimeter = 0.0, init_perimeter = 0.0; real_t length = mesh->mean_edge(); - priority_queue front; + std::priority_queue front; std::vector vertices; std::vector faces; @@ -570,7 +570,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng real_t perimeter = 0.0; real_t init_perimeter = 0.0; - priority_queue front; + std::priority_queue front; std::vector faces; // PCA -------------------------------------------------------------------------- diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 7f574204..164953e9 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -17,7 +17,7 @@ che_ply::che_ply(const std::string & file) void che_ply::read_file(const std::string & file) { - map bytes = { + std::map bytes = { {"char", 1}, {"uchar", 1}, {"short", 2}, diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 09b51662..3885fe7c 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -201,7 +201,7 @@ quaternion slerp(const quaternion & q0, const quaternion & q1, real_t t) return m * p; } -ostream & operator << (ostream & os, const quaternion & q) +std::ostream & operator << (std::ostream & os, const quaternion & q) { return os << q.s << " " << q.v; } diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 6e63d45b..a09777d8 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -96,7 +96,7 @@ real_t camera::zoom() const return -pos.v.z(); } -ostream & operator << (ostream & os, const camera & cam) +std::ostream & operator << (std::ostream & os, const camera & cam) { return os << cam.p_click << "\n" << cam.p_drag << "\n" diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index f5f46bb4..f201857d 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -66,7 +66,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) if(!read_source(filename, source)) { - cerr << "Not load shader file: " << filename << endl; + std::cerr << "Not load shader file: " << filename << std::endl; return false; } @@ -104,7 +104,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) glGetShaderInfoLog(shader, maxLength, &length, infoLog); - cerr << filename << " GLSL Error: " << infoLog << endl; + std::cerr << filename << " GLSL Error: " << infoLog << std::endl; delete[] infoLog; } @@ -119,7 +119,7 @@ bool shader::load(GLenum shader_type, const std::string & filename) bool shader::read_source(const std::string & filename, std::string & source) { - ifstream is(filename); + std::ifstream is(filename); if(!is.is_open()) return false; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 2237915c..7531a756 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -127,7 +127,7 @@ void viewer::imgui() if(ImGui::BeginMenu("Select")) { for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((to_std::string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + if(ImGui::MenuItem((std::to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; glfwSetWindowTitle(window, mesh->filename.c_str()); @@ -399,7 +399,7 @@ void viewer::add_process(const int & key, const std::string & skey, const std::s processes[key] = {skey, name, f}; processes[key].sub_menu = sub_menus.size() - 1; } - else cerr << "Repeat key: " << key << endl; + else std::cerr << "Repeat key: " << key << std::endl; } bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) @@ -562,7 +562,7 @@ bool viewer::m_save_load_view(viewer * view) if(ImGui::Button("Save")) { - ofstream os(tmp_file_path(std::string("views/") + file)); + std::ofstream os(tmp_file_path(std::string("views/") + file)); os << view->cam; os.close(); } @@ -594,7 +594,7 @@ bool viewer::m_save_load_view(viewer * view) if(ImGui::Button("Load")) { - ifstream is(vfiles[select]); + std::ifstream is(vfiles[select]); is >> view->cam; is.close(); } From 299329c10ba51e571e93682faf011643a6b8488f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 10 Nov 2022 14:54:04 +0100 Subject: [PATCH 0735/1018] gproshan: refactoring std namespace --- include/gproshan/features/shot.h | 7 ------- include/gproshan/geometry/mat.h | 2 +- include/gproshan/geometry/vec.h | 2 +- src/gproshan/app_viewer.cpp | 4 +--- src/gproshan/features/key_points.cpp | 2 +- src/gproshan/features/shot.cpp | 3 --- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 +- src/gproshan/mdict/mdict.cpp | 17 +++++------------ src/gproshan/mdict/msparse_coding.cpp | 8 ++++---- src/gproshan/mdict/patch.cpp | 2 +- src/gproshan/mesh/che.cpp | 4 ++-- src/gproshan/mesh/che_fill_hole.cpp | 4 ++-- src/gproshan/mesh/che_img.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 2 +- src/gproshan/mesh/simplification.cpp | 2 +- src/gproshan/viewer/camera.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 6 +++--- 19 files changed, 28 insertions(+), 47 deletions(-) delete mode 100644 include/gproshan/features/shot.h delete mode 100644 src/gproshan/features/shot.cpp diff --git a/include/gproshan/features/shot.h b/include/gproshan/features/shot.h deleted file mode 100644 index e861b1f2..00000000 --- a/include/gproshan/features/shot.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef SHOT_H -#define SHOT_H - - - -#endif // SHOT_H - diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index f46e7f92..225b6d94 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -101,7 +101,7 @@ std::ostream & operator << (std::ostream & os, const mat & m) return os; } -///< std istream +///< std std::istream template std::istream & operator >> (std::istream & is, mat & m) { diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 34d0581d..e7a8454b 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -336,7 +336,7 @@ std::ostream & operator << (std::ostream & os, const vec & v) return os << v[N - 1]; } -///< std istream +///< std std::istream template __host__ __device__ std::istream & operator >> (std::istream & is, vec & v) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index ef04316a..5e516326 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -931,9 +931,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) size_t old_n_vertices, n_vertices = mesh->n_vertices; size_t n_holes = 0; // FIX_BOUND mesh->n_borders; - std::vector * border_vertices; - che ** holes; - tie(border_vertices, holes) = fill_all_holes_meshes(mesh); + const auto & [border_vertices, holes] = fill_all_holes_meshes(mesh); if(!holes) return true; index_t k = 2; diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 30ce6664..b7bfe30c 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -30,7 +30,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) for(index_t f = 0; f < mesh->n_faces; ++f) face_areas[f] = { mesh->area_trig(f), f }; - sort(face_areas.begin(), face_areas.end()); + std::sort(face_areas.begin(), face_areas.end()); is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); diff --git a/src/gproshan/features/shot.cpp b/src/gproshan/features/shot.cpp deleted file mode 100644 index a7fe5670..00000000 --- a/src/gproshan/features/shot.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include - - diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 9de8e100..efa15777 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -180,7 +180,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) } os.close(); - sort(toplesets_dist, toplesets_dist + limits.size() - 1); + std::sort(toplesets_dist, toplesets_dist + limits.size() - 1); os.open(test_path + filename + "_toplesets_sorted.dist"); for(index_t i = 0; i < limits.size() - 1; ++i) diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 159698f0..04ddd04c 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -29,10 +29,7 @@ std::ostream & operator << (std::ostream & os, const locval_t & lc) void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L) { - a_vec aa; - arma::uvec selected_atoms; - - tie(aa, selected_atoms) = _OMP(x, D, L); + const auto & [aa, selected_atoms] = _OMP(x, D, L); for(index_t k = 0; k < selected_atoms.size(); ++k) { @@ -80,7 +77,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { a_sp_mat alpha = OMP_all(locval, X, D, L); - sort(locval.begin(), locval.end()); + std::sort(locval.begin(), locval.end()); rows.push_back(0); for(index_t k = 1; k < locval.size(); ++k) @@ -170,10 +167,8 @@ std::tuple _OMP(const a_vec & x, const a_mat & D, const size_ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) { a_vec alpha(D.n_cols, arma::fill::zeros); - a_vec aa; - arma::uvec selected_atoms; - tie(aa, selected_atoms) = _OMP(x, D, L); + const auto & [aa, selected_atoms] = _OMP(x, D, L); alpha.elem(selected_atoms) = aa; return alpha; @@ -182,10 +177,8 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) { a_vec alpha(D.n_cols, arma::fill::zeros); - a_vec aa; - arma::uvec selected_atoms; - tie(aa, selected_atoms) = _OMP(x, D, L, mask); + const auto & [aa, selected_atoms] = _OMP(x, D, L, mask); alpha.elem(selected_atoms) = aa; return alpha; @@ -367,7 +360,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si { a_sp_mat alpha = OMP_all(locval, patches, A, L); - sort(locval.begin(), locval.end()); + std::sort(locval.begin(), locval.end()); rows.push_back(0); for(index_t k = 1; k < locval.size(); ++k) diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index d49e1bed..1dbb7ccf 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -169,7 +169,7 @@ void msparse_coding::load_sampling() { patch p; p.init_radial_disjoint(euc_radio, geo_radio, mesh, S(i), m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); - patches.push_back(move(p)); + patches.push_back(std::move(p)); } m_params.n_patches = n_seeds; @@ -217,7 +217,7 @@ void msparse_coding::load_sampling() count_cov += count_cov_patch; if(count_cov_patch > 0) { - patches.push_back(move(p)); + patches.push_back(std::move(p)); seeds.push_back(vsf); radios.push_back(euc_radio); geo_radios.push_back(geo_radio); @@ -749,7 +749,7 @@ void msparse_coding::load_features(std::vector & v_feat, size_t & feats gproshan_debug_var(f_feat); if(inp.fail()) { - inp.clear(ios::failbit); + inp.clear(std::ios::failbit); // call the function using system //g++ -O3 *.cpp -lgsl -lCGAL -o harris3d //cmake -DCMAKE_BUILD_TYPE=Debug .. @@ -891,7 +891,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) rp.xyz.row(2) = x.t(); } - sort(patches_error.begin(), patches_error.end()); + std::sort(patches_error.begin(), patches_error.end()); fprintf(stderr, "error %16s%16s\n", "best", "worst"); for(index_t i = 0; i < 10; ++i) diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 4fee5c42..6430e10e 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -736,7 +736,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, distances.push_back(norm(a - b)); } */ - sort(distances.begin(), distances.end()); + std::sort(distances.begin(), distances.end()); size_t n_elem = distances.size(); if(distances.size()%2 ==0) { diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 6fe39965..fbc9bcc8 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -520,7 +520,7 @@ void che::set_head_vertices(index_t * head, const size_t & n) break; } - swap(GT[v], GT[i]); + std::swap(GT[v], GT[i]); for(const index_t & he: star(v)) VT[he] = i; @@ -528,7 +528,7 @@ void che::set_head_vertices(index_t * head, const size_t & n) for(const index_t & he: star(i)) VT[he] = i; - swap(EVT[v], EVT[i]); + std::swap(EVT[v], EVT[i]); } } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index c3caf6c0..2e7f8e5d 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -369,7 +369,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti bool o = is_grow; std::vector is_border(vertices.size()); - std::vector > neighbors(vertices.size()); + std::vector > neighbors(vertices.size()); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) @@ -626,7 +626,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng std::vector tmp_vertices(vertices.size()); std::vector is_border(vertices.size()); - std::vector > neighbors(vertices.size()); + std::vector > neighbors(vertices.size()); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 58df1285..5e41a5f4 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -44,7 +44,7 @@ void che_img::read_file(const std::string & file) GT[v++] = {real_t(i), real_t(j), img(i, j)}; } - thread([](CImg img) { img.display(); }, img).detach(); + std::thread([](CImg img) { img.display(); }, img).detach(); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 164953e9..f1ee3bd5 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -119,7 +119,7 @@ void che_ply::read_file(const std::string & file) auto big_to_little = [](char * buffer, const index_t & n) { for(index_t i = 0, j = n - 1; i < j; ++i, --j) - swap(buffer[i], buffer[j]); + std::swap(buffer[i], buffer[j]); }; char * buffer = vbytes == sizeof(vertex) ? (char *) GT : new char[vbytes * n_vertices]; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index a4517f13..274f55d9 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -71,7 +71,7 @@ void che_ptx::read_file(const std::string & file) img.permute_axes("zycx"); img.save((file + ".jpg").c_str()); - thread([](CImg img) { img.mirror("y").display(); }, img).detach(); + std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); } else { diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 3885fe7c..31758b83 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -206,7 +206,7 @@ std::ostream & operator << (std::ostream & os, const quaternion & q) return os << q.s << " " << q.v; } -istream & operator >> (istream & is, quaternion & q) +std::istream & operator >> (std::istream & is, quaternion & q) { return is >> q.s >> q.v; } diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 97c7b7ae..b3bf936f 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -57,7 +57,7 @@ void simplification::order_edges(index_t * const & sort_edges, real_t * const & error_edges[e] = compute_error(e); } - sort(sort_edges, sort_edges + mesh->n_edges, + std::sort(sort_edges, sort_edges + mesh->n_edges, [&error_edges](const index_t & a, const index_t & b) { return error_edges[a] < error_edges[b]; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index a09777d8..ef31dd39 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -105,7 +105,7 @@ std::ostream & operator << (std::ostream & os, const camera & cam) << cam.pos << "\n"; } -istream & operator >> (istream & is, camera & cam) +std::istream & operator >> (std::istream & is, camera & cam) { return is >> cam.p_click >> cam.p_drag diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7531a756..b8ef4ad5 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -553,7 +553,7 @@ bool viewer::m_hide_show_imgui(viewer * view) bool viewer::m_save_load_view(viewer * view) { - filesystem::create_directory(tmp_file_path("views/")); + std::filesystem::create_directory(tmp_file_path("views/")); static char file[128] = "new_view"; @@ -571,8 +571,8 @@ bool viewer::m_save_load_view(viewer * view) static std::vector vfiles; vfiles.clear(); - for(auto & p: filesystem::directory_iterator(tmp_file_path("views/"))) - vfiles.push_back(p.path().std::string()); + for(auto & p: std::filesystem::directory_iterator(tmp_file_path("views/"))) + vfiles.push_back(p.path().string()); if(!vfiles.size()) return true; From cc61556bd35446e8a98e36ce00425d342963c2ea Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 10 Nov 2022 16:51:18 +0100 Subject: [PATCH 0736/1018] viewer: fit screen model gui options: none, box, and sphere --- include/gproshan/viewer/che_viewer.h | 5 +++++ src/gproshan/mesh/che.cpp | 21 +++++++++++++-------- src/gproshan/viewer/che_viewer.cpp | 18 +++++++++++++++++- src/gproshan/viewer/viewer.cpp | 8 ++++++-- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 2f2178ce..1f7f9928 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -43,6 +43,10 @@ class che_viewer GLuint vbo[6]; public: + enum fit_screen: int { none, box, sphere }; + + fit_screen opt_fit_screen = box; + int vx = 0, vy = 0; ///< viewport positions. std::vector selected; std::vector selected_xyz; @@ -73,6 +77,7 @@ class che_viewer void init(che * m, const bool & center = true); void update(); + void update_model_mat(); void update_vbo(); void update_vbo_geometry(); void update_vbo_normal(const vertex * vnormal = nullptr); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index fbc9bcc8..b83a7ac4 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -192,27 +192,32 @@ void che::reload() mat4 che::normalize_sphere(const real_t & r) const { vertex center; - #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) { #pragma omp critical center += GT[v]; } - center /= n_vertices; - real_t max_norm = 0; - - #pragma omp parallel for reduction(+: max_norm) + real_t mean_dist = 0; + #pragma omp parallel for reduction(+: mean_dist) for(index_t v = 0; v < n_vertices; ++v) - max_norm += norm(GT[v] - center); + mean_dist += length(GT[v] - center); + mean_dist /= n_vertices; - max_norm /= n_vertices; + real_t sigma_dist = 0; + #pragma omp parallel for reduction(+: sigma_dist) + for(index_t v = 0; v < n_vertices; ++v) + { + const real_t & diff = mean_dist - length(GT[v] - center); + sigma_dist += diff * diff; + } + sigma_dist = sqrt(sigma_dist / n_vertices); mat4 model_mat; - const real_t & scale = r / max_norm; + const real_t & scale = r / (mean_dist + 2 * sigma_dist); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; center *= -scale; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index eaf12964..a94dbfb8 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -49,7 +49,7 @@ void che_viewer::init(che * m, const bool & center) void che_viewer::update() { - model_mat = center_mesh ? mesh->normalize_box(2) : mat4::identity(); + update_model_mat(); render_pointcloud = mesh->is_pointcloud(); selected_xyz.clear(); @@ -59,6 +59,22 @@ void che_viewer::update() rt_embree = new rt::embree({mesh}, {model_mat}); } +void che_viewer::update_model_mat() +{ + switch(opt_fit_screen) + { + case none: + model_mat = mat4::identity(); + break; + case box: + model_mat = mesh->normalize_box(); + break; + case sphere: + model_mat = mesh->normalize_sphere(); + break; + } +} + void che_viewer::update_vbo() { update_vbo_geometry(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b8ef4ad5..61d479e6 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -191,13 +191,17 @@ void viewer::imgui() ImGui::Text("%13lu vertices", mesh->n_vertices); ImGui::Text("%13lu faces", mesh->n_faces); + ImGui::Indent(); + if(ImGui::Combo("fit screen", (int *) &mesh.opt_fit_screen, "none\0box (2x2x2)\0sphere (97.72%)\0\0")) + { + mesh.update_model_mat(); + } if(mesh.render_pointcloud) { - ImGui::Indent(); ImGui::Checkbox("point_normals", &mesh.point_normals); ImGui::SliderInt("point_size", (int *) &mesh.point_size, 1, 32); - ImGui::Unindent(); } + ImGui::Unindent(); } static char slight[32]; From 1ae6ce2514bdf687b8b2d9c9feeb7664dfc04bb6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 15 Nov 2022 13:03:37 +0100 Subject: [PATCH 0737/1018] che: fit sphere base on normal distribution --- src/gproshan/features/key_points.cpp | 2 +- src/gproshan/geometry/convex_hull.cpp | 4 ++-- src/gproshan/mesh/che.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index b7bfe30c..1da52a38 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -30,7 +30,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) for(index_t f = 0; f < mesh->n_faces; ++f) face_areas[f] = { mesh->area_trig(f), f }; - std::sort(face_areas.begin(), face_areas.end()); + std::sort(begin(face_areas), end(face_areas)); is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index d122b728..e034d542 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -24,9 +24,9 @@ convex_hull::operator const std::vector & () void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_points) { std::vector idx(n_points); - std::iota(idx.begin(), idx.end(), 0); + std::iota(begin(idx), end(idx), 0); - std::sort(idx.begin(), idx.end(), + std::sort(begin(idx), end(idx), [&points](const index_t & i, const index_t & j) { return points[i] < points[j]; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index b83a7ac4..e705da8a 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -217,7 +217,7 @@ mat4 che::normalize_sphere(const real_t & r) const mat4 model_mat; - const real_t & scale = r / (mean_dist + 2 * sigma_dist); + const real_t & scale = r / (mean_dist + sigma_dist); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; center *= -scale; diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 82e50ff2..362f376d 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -21,7 +21,7 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta; phi += delta) for(real_t theta = delta; theta < M_PI - 0.5 * delta; theta += delta) - vertices.push_back({r * sin(theta) * cos(phi), r * sin(theta) * sin(phi), r * cos(theta)}); + vertices.push_back({r * std::sin(theta) * std::cos(phi), r * std::sin(theta) * std::sin(phi), r * std::cos(theta)}); vertices.push_back({0, 0, r}); vertices.push_back({0, 0, -r}); From d4e6528bb068b097d265688b52430d41859d5a3c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 17 Nov 2022 15:48:34 +0100 Subject: [PATCH 0738/1018] viewer: add proj_mat and camera aspect, fov, near and far --- include/gproshan/viewer/camera.h | 6 ++++++ include/gproshan/viewer/viewer.h | 1 + src/gproshan/viewer/camera.cpp | 5 +++++ src/gproshan/viewer/viewer.cpp | 7 ++++++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index c81260eb..5064349e 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -22,9 +22,15 @@ class camera quaternion pos = vertex{0, 0, -3.14}; quaternion front = vertex{0, 0, 1}; quaternion up = vertex{0, 1, 0}; + real_t fovy = 45; + real_t aspect = 1; + real_t near = 0.1; + real_t far = 1000; public: static mat4 perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far); + + mat4 perspective(); mat4 look_at(const quaternion & r); quaternion current_rotation() const; void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index c29aed1b..78f37325 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -61,6 +61,7 @@ class viewer int & window_height = render_params.window_height; int & viewport_width = render_params.viewport_width; int & viewport_height = render_params.viewport_height; + mat4 proj_mat; mat4 proj_view_mat; bool hide_imgui = false; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index ef31dd39..7ac035fe 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -26,6 +26,11 @@ mat4 camera::look_at(const quaternion & r) return view; } +mat4 camera::perspective() +{ + return perspective(fovy, aspect, near, far); +} + mat4 camera::perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far) { const real_t & tan_fovy_2 = std::tan(fovy * M_PI / 360); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 61d479e6..a81db1cf 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -96,7 +96,7 @@ bool viewer::run() cam_light = render_params.lights[0]; cam_light = r.conj() * cam_light * r; - proj_view_mat = camera::perspective(45, real_t(viewport_width) / real_t(viewport_height), 0.01, 1000) * cam.look_at(r); + proj_view_mat = proj_mat * cam.look_at(r); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); render_gl(); @@ -432,6 +432,9 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) glfwGetFramebufferSize(window, &viewport_width, &viewport_height); viewport_width /= cols; viewport_height /= rows; + cam.aspect = real_t(viewport_width) / viewport_height; + proj_mat = cam.perspective(); + return true; } @@ -441,6 +444,8 @@ void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int heigh viewer * view = (viewer *) glfwGetWindowUserPointer(window); view->viewport_width = width / m_window_split[view->n_meshes].y(); view->viewport_height = height / m_window_split[view->n_meshes].x(); + view->cam.aspect = real_t(view->viewport_width) / view->viewport_height; + view->proj_mat = view->cam.perspective(); } void viewer::window_size_callback(GLFWwindow * window, int width, int height) From a6d6f97e54112da2496a6b75937ef73bc91e59ca Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Nov 2022 13:39:11 +0100 Subject: [PATCH 0739/1018] scenes: loading material from .mtl files --- include/gproshan/mesh/che.h | 2 +- include/gproshan/scenes/scene.h | 55 ++++++++++++++++ include/gproshan/viewer/camera.h | 2 +- include/gproshan/viewer/che_viewer.h | 10 +-- src/gproshan/app_viewer.cpp | 8 ++- src/gproshan/scenes/scene.cpp | 98 ++++++++++++++++++++++++++++ src/gproshan/viewer/che_viewer.cpp | 4 +- 7 files changed, 166 insertions(+), 13 deletions(-) create mode 100644 include/gproshan/scenes/scene.h create mode 100644 src/gproshan/scenes/scene.cpp diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 04c026f9..c6c564a6 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -53,7 +53,7 @@ class che vertex * VN = nullptr; ///< vertex normals : v -> normal(v) rgb_t * VC = nullptr; ///< vertex color : v -> color(v) - real_t * VHC = nullptr; ///< vertex color heat map : v -> heatmap(v) + real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) bool manifold = true; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h new file mode 100644 index 00000000..c24661ff --- /dev/null +++ b/include/gproshan/scenes/scene.h @@ -0,0 +1,55 @@ +#ifndef SCENE_H +#define SCENE_H + +#include + +#include +#include +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class scene: public che +{ + public: + struct texture + { + }; + + struct material + { + vec3 Ka = {0.2, 0.2, 0.2}; + vec3 Kd = {0.8, 0.8, 0.8}; + vec3 Ks = {1, 1, 1}; + real_t d = 1; // Tr = 0, opposite + real_t Ns = 0; + index_t illum = 1; + index_t map_Ka = NIL; + index_t map_Kd = NIL; + }; + + struct object + { + }; + + protected: + std::unordered_map material_id; + std::vector material_name; + std::vector materials; + + std::unordered_map texture_id; + std::vector texture_name; + std::vector textures; + + public: + bool read_mtl(const std::string & file); +}; + + +} // namespace gproshan + +#endif // SCENE_H + diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 5064349e..7268dd87 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -24,7 +24,7 @@ class camera quaternion up = vertex{0, 1, 0}; real_t fovy = 45; real_t aspect = 1; - real_t near = 0.1; + real_t near = 0.01; real_t far = 1000; public: diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 1f7f9928..a293a3d9 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -24,16 +24,10 @@ namespace gproshan { enum render_type: index_t { R_GL, R_EMBREE, R_OPTIX }; -struct material -{ -}; - - class che_viewer { protected: che * mesh = nullptr; - std::vector materials; size_t n_instances = 0; bool center_mesh = false; @@ -85,8 +79,8 @@ class che_viewer void update_vbo_heatmap(const real_t * vheatmap = nullptr); void update_instances_positions(const std::vector & translations); - void draw(shader & program); - void draw_point_cloud(shader & program); + virtual void draw(shader & program); + virtual void draw_point_cloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); void select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 5e516326..b84c8abc 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -27,7 +28,12 @@ che * app_viewer::load_mesh(const std::string & file_path) std::string extension = file_path.substr(pos + 1); if(extension == "off") return new che_off(file_path); - if(extension == "obj") return new che_obj(file_path); + if(extension == "obj") + { + scene sc; + sc.read_mtl(file_path.substr(0, pos) + ".mtl"); + return new che_obj(file_path); + } if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); if(extension == "xyz") return new che_xyz(file_path); diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp new file mode 100644 index 00000000..12465ef8 --- /dev/null +++ b/src/gproshan/scenes/scene.cpp @@ -0,0 +1,98 @@ +#include "gproshan/scenes/scene.h" + + +// geometry processing and shape analysis framework +namespace gproshan { + + +bool scene::read_mtl(const std::string & file) +{ + gproshan_error_var(file); + + FILE * fp = fopen(file.c_str(), "r"); + if(!fp) return false; + + char line[256], str[64]; + while(fgets(line, sizeof(line), fp)) + { + switch(line[0]) + { + case 'n': // newmtl + { + sscanf(line, "%*s %s", str); + material_id[str] = materials.size(); + material_name.push_back(str); + materials.push_back({}); + break; + } + case 'K': // Ka, Kd, Ks + { + vec3 & rgb = line[1] == 'a' ? materials.back().Ka + : line[1] == 'd' ? materials.back().Kd + : materials.back().Ks; + sscanf(line, "%*s %f %f %f", &rgb.x(), &rgb.y(), &rgb.z()); + break; + } + case 'd': // d + { + real_t & d = materials.back().d; + sscanf(line, "%*s %f", &d); + break; + } + case 'T': // Tr + { + real_t & d = materials.back().d; + sscanf(line, "%*s %f", &d); + d = 1 - d; + break; + } + case 'N': // Ns + { + real_t & d = materials.back().Ns; + sscanf(line, "%*s %f", &d); + break; + } + case 'i': // illum + { + index_t & illum = materials.back().illum; + sscanf(line, "%*s %u", &illum); + break; + } + case 'm': // map_Ka, map_kd + { + index_t & m = line[5] == 'a' ? materials.back().map_Ka : materials.back().map_Kd; + sscanf(line, "%*s %s", str); + if(texture_id.find(str) == texture_id.end()) + { + texture_id[str] = textures.size(); + texture_name.push_back(str); + textures.push_back({}); + } + m = texture_id[str]; + break; + } + } + } + + fclose(fp); + + for(index_t i = 0; i < materials.size(); ++i) + { + const material & m = materials[i]; + gproshan_log_var(material_name[i]); + gproshan_log_var(m.Ka); + gproshan_log_var(m.Kd); + gproshan_log_var(m.Ks); + gproshan_log_var(m.d); + gproshan_log_var(m.Ns); + gproshan_log_var(m.illum); + if(m.map_Ka != NIL) gproshan_log_var(texture_name[m.map_Ka]); + if(m.map_Kd != NIL) gproshan_log_var(texture_name[m.map_Kd]); + } + + return true; +} + + +} // namespace gproshan + diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index a94dbfb8..f956029a 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -188,13 +188,13 @@ void che_viewer::draw(shader & program) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); - else if(materials.size()) + /*else if(materials.size()) { for(auto & m: materials) { glDrawElementsBaseVertex(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, 0); } - } + }*/ else glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); From af572d3ec1db481801a8c76d9ae850756195746c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Nov 2022 14:39:28 +0100 Subject: [PATCH 0740/1018] scenes: loading textures --- include/gproshan/scenes/scene.h | 8 ++++++-- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/scenes/scene.cpp | 36 ++++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index c24661ff..1d11a503 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -17,6 +17,9 @@ class scene: public che public: struct texture { + vec3 * data; + size_t rows = 0; + size_t cols = 0; }; struct material @@ -42,10 +45,11 @@ class scene: public che std::unordered_map texture_id; std::vector texture_name; - std::vector textures; + std::vector textures; public: - bool read_mtl(const std::string & file); + bool load_mtl(const std::string & file); + bool load_texture(const std::string & file); }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index b84c8abc..8665d168 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -31,7 +31,7 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "obj") { scene sc; - sc.read_mtl(file_path.substr(0, pos) + ".mtl"); + sc.load_mtl(file_path.substr(0, pos) + ".mtl"); return new che_obj(file_path); } if(extension == "ply") return new che_ply(file_path); diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 12465ef8..6e387211 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -1,11 +1,15 @@ #include "gproshan/scenes/scene.h" +#include + +using namespace cimg_library; + // geometry processing and shape analysis framework namespace gproshan { -bool scene::read_mtl(const std::string & file) +bool scene::load_mtl(const std::string & file) { gproshan_error_var(file); @@ -22,7 +26,7 @@ bool scene::read_mtl(const std::string & file) sscanf(line, "%*s %s", str); material_id[str] = materials.size(); material_name.push_back(str); - materials.push_back({}); + materials.emplace_back(); break; } case 'K': // Ka, Kd, Ks @@ -66,7 +70,6 @@ bool scene::read_mtl(const std::string & file) { texture_id[str] = textures.size(); texture_name.push_back(str); - textures.push_back({}); } m = texture_id[str]; break; @@ -76,6 +79,16 @@ bool scene::read_mtl(const std::string & file) fclose(fp); + const std::string path = file.substr(0, file.rfind('/') + 1); + for(auto & tex: texture_name) + { + for(char & c: tex) + if(c == '\\') c = '/'; + + if(!load_texture(path + tex)) + return false; + } + for(index_t i = 0; i < materials.size(); ++i) { const material & m = materials[i]; @@ -90,6 +103,23 @@ bool scene::read_mtl(const std::string & file) if(m.map_Kd != NIL) gproshan_log_var(texture_name[m.map_Kd]); } + gproshan_log_var(materials.size()); + gproshan_log_var(textures.size()); + + return true; +} + +bool scene::load_texture(const std::string & file) +{ + CImg img(file.c_str()); + + textures.emplace_back(); + texture & tex = textures.back(); + tex.rows = img.height(); + tex.cols = img.width(); + tex.data = new vec3[tex.rows * tex.cols]; + memcpy((float *) tex.data, img.data(), sizeof(vec3) * tex.rows * tex.cols); + return true; } From 3eeb45583d4f2c5e3b7b393bbf4202295bd70d43 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 18 Nov 2022 16:34:41 +0100 Subject: [PATCH 0741/1018] scenes: loading scene from .obj file --- include/gproshan/scenes/scene.h | 7 ++++++- src/gproshan/app_viewer.cpp | 1 + src/gproshan/scenes/scene.cpp | 29 +++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 1d11a503..b7c26518 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -17,7 +17,7 @@ class scene: public che public: struct texture { - vec3 * data; + vec3 * data = nullptr; size_t rows = 0; size_t cols = 0; }; @@ -36,6 +36,9 @@ class scene: public che struct object { + index_t begin = 0; + index_t end = 0; + index_t idm = NIL; }; protected: @@ -48,6 +51,8 @@ class scene: public che std::vector textures; public: + ~scene(); + bool load_obj(const std::string & file); bool load_mtl(const std::string & file); bool load_texture(const std::string & file); }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 8665d168..add159a7 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -32,6 +32,7 @@ che * app_viewer::load_mesh(const std::string & file_path) { scene sc; sc.load_mtl(file_path.substr(0, pos) + ".mtl"); + sc.load_obj(file_path); return new che_obj(file_path); } if(extension == "ply") return new che_ply(file_path); diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 6e387211..5b3f19b9 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -9,6 +9,35 @@ using namespace cimg_library; namespace gproshan { +scene::~scene() +{ + for(texture & tex: textures) + delete tex.data; +} + +bool scene::load_obj(const std::string & file) +{ + FILE * fp = fopen(file.c_str(), "r"); + if(!fp) return false; + + char line[256], str[64]; + while(fgets(line, sizeof(line), fp)) + { + switch(line[0]) + { + case 'f': + { + + break; + } + } + } + + fclose(fp); + + return true; +} + bool scene::load_mtl(const std::string & file) { gproshan_error_var(file); From b4f51c096344d0c2278dbc92ffbfecdff961ca93 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Nov 2022 02:25:59 +0100 Subject: [PATCH 0742/1018] updated CMakeLists --- CMakeLists.txt | 3 --- src/gproshan/mesh/che_ptx.cpp | 25 +++++++++++++------------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 172fab77..57bb428b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,17 +50,14 @@ configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in find_package(embree 3.13 REQUIRED) -find_package(Threads REQUIRED) find_package(OpenGL REQUIRED) find_package(OpenMP REQUIRED) find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) -find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(Eigen3 REQUIRED) find_package(CGAL REQUIRED) find_package(SuiteSparse REQUIRED) -find_package(Boost COMPONENTS thread system) include_directories(SYSTEM ${embree_INCLUDE_DIRS}) diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 274f55d9..694a9fde 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -25,19 +25,20 @@ void che_ptx::read_file(const std::string & file) assert(fp); size_t n_rows, n_cols; - float T[12], R[12], tr[4]; + vertex p; // scanner position + mat3 A; // scanner axis + mat4 T; // transformation matrix fscanf(fp, "%lu %lu", &n_rows, &n_cols); + fscanf(fp, "%f %f %f", &p.x(), &p.y(), &p.z()); - for(index_t i = 0; i < 12; ++i) - fscanf(fp, "%f", T + i); - - for(index_t i = 0; i < 12; ++i) - fscanf(fp, "%f", R + i); + for(index_t i = 0; i < 3; ++i) + for(index_t j = 0; j < 3; ++j) + fscanf(fp, "%f", &A(i, j)); for(index_t i = 0; i < 4; ++i) - fscanf(fp, "%f", tr + i); - + for(index_t j = 0; j < 4; ++j) + fscanf(fp, "%f", &T(i, j)); alloc(n_rows * n_cols, 2 * (n_rows - 1) * (n_cols - 1)); @@ -54,16 +55,16 @@ void che_ptx::read_file(const std::string & file) if(rgb) { - GT[0] = { x, y, z }; - VC[0] = { r, g, b }; + GT[0] = {x, y, z}; + VC[0] = {r, g, b}; VHC[0] = intensity; for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); sscanf(line, "%f %f %f %f %hhu %hhu %hhu", &x, &y, &z, &intensity, &r, &g, &b); - GT[v] = { x, y, z }; - VC[v] = { r, g, b }; + GT[v] = {x, y, z}; + VC[v] = {r, g, b}; VHC[v] = intensity; } From a63c620b44b3df0144551ab4f91fb16012a93a9e Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 21 Nov 2022 13:45:24 +0100 Subject: [PATCH 0743/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1a80ca35..171c818c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-20.04, ubuntu-22.04] + os: [ubuntu-22.04] config: [Release, Debug] cuda: [cuda, ''] From f8104e9326bbda068c2746f078e5184c10615a82 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Nov 2022 15:37:39 +0100 Subject: [PATCH 0744/1018] updated CMakeLists --- CMakeLists.txt | 4 ++-- src/gproshan/CMakeLists.txt | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 57bb428b..5e9ffd69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,8 +50,8 @@ configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in find_package(embree 3.13 REQUIRED) -find_package(OpenGL REQUIRED) find_package(OpenMP REQUIRED) +find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) find_package(Armadillo REQUIRED) @@ -64,9 +64,9 @@ include_directories(SYSTEM ${embree_INCLUDE_DIRS}) include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) -include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) +include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) add_subdirectory(src) # gproshan library diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 95bf27f0..9b1be818 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -6,13 +6,12 @@ add_library(gproshan SHARED ${cpp_sources}) target_link_libraries(gproshan embree) target_link_libraries(gproshan OpenMP::OpenMP_CXX) -target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan OpenGL::GL) +target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) -target_link_libraries(gproshan ${X11_X11_LIB}) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan CGAL::CGAL) +target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan ${OptiX_LIBRARY}) target_link_libraries(gproshan imgui) From 7e349ea610a624d41edd44d2846097a3c6382e3d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Nov 2022 15:55:07 +0100 Subject: [PATCH 0745/1018] viewer: add gui for cam position --- README.md | 2 +- src/gproshan/mesh/che_ptx.cpp | 4 ++-- src/gproshan/viewer/viewer.cpp | 13 ++++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c713d29f..8818ab7d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## [gproshan](https://github.com/larc/gproshan): a geometry processing and shape analysis framework -[![Build](https://github.com/larc/gproshan_dev/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/larc/gproshan_dev/actions/workflows/build.yml) +[![Build Ubuntu 22.04](https://github.com/larc/gproshan_dev/actions/workflows/build.yml/badge.svg?branch=dev)](https://github.com/larc/gproshan_dev/actions/workflows/build.yml) [![DOI](https://zenodo.org/badge/88686093.svg)](https://zenodo.org/badge/latestdoi/88686093) diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 694a9fde..c6c1bce7 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -76,14 +76,14 @@ void che_ptx::read_file(const std::string & file) } else { - GT[0] = { x, y, z }; + GT[0] = {x, y, z}; VHC[0] = intensity; for(index_t v = 1; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); sscanf(line, "%f %f %f %f", &x, &y, &z, &intensity); - GT[v] = { x, y, z }; + GT[v] = {x, y, z}; VHC[v] = intensity; } } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a81db1cf..6fab4376 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -204,9 +204,16 @@ void viewer::imgui() ImGui::Unindent(); } + static real_t pos_min = -10; + static real_t pos_max = 10; + if(ImGui::CollapsingHeader("Camera")) + { + ImGui::Indent(); + ImGui::SliderScalarN("position", ImGuiDataType_Real, &cam.pos[0], 3, &pos_min, &pos_max); + ImGui::Unindent(); + } + static char slight[32]; - static real_t light_min = -2; - static real_t light_max = 2; if(ImGui::CollapsingHeader("Scene Lights")) { ImGui::Indent(); @@ -214,7 +221,7 @@ void viewer::imgui() for(int i = 0; i < render_params.n_lights; ++i) { sprintf(slight, "light %d", i); - ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &light_min, &light_max); + ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &pos_min, &pos_max); } if(ImGui::Button("add light")) From 3eeb5370bfdbf7d53e92336fd50f5fb8d6a663d5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 21 Nov 2022 18:58:19 +0100 Subject: [PATCH 0746/1018] scene: adding .obj parser che_obj --- include/gproshan/mesh/che.h | 2 + include/gproshan/mesh/che_obj.h | 16 ++++ src/gproshan/app_viewer.cpp | 4 +- src/gproshan/mesh/che.cpp | 7 ++ src/gproshan/mesh/che_obj.cpp | 143 ++++++++++++++++++++------------ src/gproshan/scenes/scene.cpp | 17 ---- 6 files changed, 118 insertions(+), 71 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index c6c564a6..a50fc701 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -32,6 +32,8 @@ class che unsigned char g = 240; unsigned char b = 250; + rgb_t() = default; + rgb_t(const float & fr, const float & fg, const float & fb); unsigned char & operator [] (const index_t & i); operator vertex () const; }; diff --git a/include/gproshan/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h index 197012c4..affb6025 100644 --- a/include/gproshan/mesh/che_obj.h +++ b/include/gproshan/mesh/che_obj.h @@ -3,6 +3,8 @@ #include +#include + // geometry processing and shape analysis framework namespace gproshan { @@ -17,6 +19,20 @@ class che_obj : public che private: void read_file(const std::string & file); + + public: + struct parser + { + std::vector vertices; + std::vector vnormals; + std::vector vtexcoords; + std::vector vcolors; + std::vector faces; + std::vector > objects; + std::unordered_set mtllibs; + + parser(const std::string & file); + }; }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index add159a7..267a2877 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -31,8 +31,8 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "obj") { scene sc; - sc.load_mtl(file_path.substr(0, pos) + ".mtl"); - sc.load_obj(file_path); + //sc.load_mtl(file_path.substr(0, pos) + ".mtl"); + che_obj::parser p(file_path); return new che_obj(file_path); } if(extension == "ply") return new che_ply(file_path); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index e705da8a..bc895332 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -16,6 +16,13 @@ size_t & che::rw(const size_t & n) } +che::rgb_t::rgb_t(const float & fr, const float & fg, const float & fb) +{ + r = (unsigned char) (fr * 255); + g = (unsigned char) (fg * 255); + b = (unsigned char) (fb * 255); +} + unsigned char & che::rgb_t::operator [] (const index_t & i) { return (&r)[i]; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 84355d40..d6ee561b 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -16,58 +16,11 @@ che_obj::che_obj(const std::string & file) void che_obj::read_file(const std::string & file) { - FILE * fp = fopen(file.c_str(), "r"); - assert(fp); - - float x, y, z, r, g, b; - index_t P[32], n; - - std::vector vertices; - std::vector vertices_color; - std::vector faces; - - char line[256], str[64]; - char * line_ptr; - index_t offset; - - while(fgets(line, sizeof(line), fp)) - { - str[0] = 0; - line_ptr = line; - - sscanf(line_ptr, "%s%n", str, &offset); - line_ptr += offset; - - if(str[0] == 'v' && !str[1]) // v x y z - { - n = sscanf(line_ptr, "%f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); - vertices.push_back({x, y, z}); - vertices_color.push_back(n == 6 ? rgb_t{(unsigned char) (r * 255), (unsigned char) (g * 255), (unsigned char) (b * 255)} : rgb_t()); - } - - if(str[0] == 'f') // f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ... - { - n = 0; - while(sscanf(line_ptr, "%s%n", str, &offset) > 0) - { - line_ptr += offset; - sscanf(str, "%d%*s", P + n); - P[n] += P[n] > vertices.size() ? vertices.size() : -1; - ++n; - } - - for(const index_t & v: trig_convex_polygon(P, n)) - faces.push_back(v); - } - } - - fclose(fp); - - - alloc(vertices.size(), faces.size() / che::mtrig); - memcpy(GT, vertices.data(), vertices.size() * sizeof(vertex)); - memcpy(VC, vertices_color.data(), vertices_color.size() * sizeof(rgb_t)); - memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); + parser p(file); + alloc(p.vertices.size(), p.faces.size() / che::mtrig); + memcpy(GT, p.vertices.data(), p.vertices.size() * sizeof(vertex)); + memcpy(VC, p.vcolors.data(), p.vcolors.size() * sizeof(rgb_t)); + memcpy(VT, p.faces.data(), p.faces.size() * sizeof(index_t)); } void che_obj::write_file(const che * mesh, const std::string & file, const bool & color, const bool & pointcloud) @@ -105,6 +58,92 @@ void che_obj::write_file(const che * mesh, const std::string & file, const bool fclose(fp); } +che_obj::parser::parser(const std::string & file) +{ + FILE * fp = fopen(file.c_str(), "r"); + assert(fp); + + std::vector P; + uvec3 vtn; + index_t n; + float x, y, z, r, g, b; + char line[256], str[64]; + + while(fgets(line, sizeof(line), fp)) + { + switch(line[0]) + { + case 'f': + { + P.clear(); vtn = {}; n = 0; + for(index_t i = 2; line[i]; ++i) + switch(line[i]) + { + case '/': ++n; break; + case '\n': + case ' ': + P.push_back(vtn[0]); + vtn = {}; n = 0; + break; + default: + vtn[n] = 10 * vtn[n] + line[i] - '0'; + break; + } + + for(index_t & i: P) + i += i > vertices.size() ? vertices.size() : -1; + + for(const index_t & v: trig_convex_polygon(P.data(), P.size())) + faces.push_back(v); + break; + } + case 'v': + { + n = sscanf(line, "%*s %f %f %f %f %f %f", &x, &y, &z, &r, &g, &b); + switch(line[1]) + { + case ' ': + vertices.push_back({x, y, z}); + n == 6 ? vcolors.emplace_back(r, g, b) : vcolors.emplace_back(); + break; + case 'n': + vnormals.push_back({x, y, z}); + break; + case 't': + vtexcoords.push_back({x, y, z}); + break; + } + break; + } + case 'u': // usemtl + { + sscanf(line, "%*s %s", str); + objects.emplace_back(str, faces.size()); + break; + } + case 'm': // mtllib + { + sscanf(line, "%*s %s", str); + mtllibs.insert(str); + break; + } + } + } + + fclose(fp); + + gproshan_error_var(vertices.size()); + gproshan_error_var(vnormals.size()); + gproshan_error_var(vtexcoords.size()); + gproshan_error_var(vcolors.size()); + gproshan_error_var(faces.size()); + gproshan_error_var(objects.size()); + for(auto & o: objects) + gproshan_log_var(o.first); + for(auto & mtllib: mtllibs) + gproshan_log_var(mtllib); +} + } // namespace gproshan diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 5b3f19b9..d1fe8e6d 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -17,23 +17,6 @@ scene::~scene() bool scene::load_obj(const std::string & file) { - FILE * fp = fopen(file.c_str(), "r"); - if(!fp) return false; - - char line[256], str[64]; - while(fgets(line, sizeof(line), fp)) - { - switch(line[0]) - { - case 'f': - { - - break; - } - } - } - - fclose(fp); return true; } From 4319a57076ad642881ca172284c549b332d3b0dd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 00:48:28 +0100 Subject: [PATCH 0747/1018] scene: fix parsing .obj files --- include/gproshan/mesh/che_obj.h | 4 +-- include/gproshan/scenes/scene.h | 4 +++ src/gproshan/app_viewer.cpp | 4 +-- src/gproshan/mesh/che_obj.cpp | 59 +++++++++++++++++++++++---------- src/gproshan/scenes/scene.cpp | 27 +++++++++++++++ 5 files changed, 76 insertions(+), 22 deletions(-) diff --git a/include/gproshan/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h index affb6025..3a27cea0 100644 --- a/include/gproshan/mesh/che_obj.h +++ b/include/gproshan/mesh/che_obj.h @@ -25,9 +25,9 @@ class che_obj : public che { std::vector vertices; std::vector vnormals; - std::vector vtexcoords; + std::vector vtexcoords; std::vector vcolors; - std::vector faces; + std::vector faces; std::vector > objects; std::unordered_set mtllibs; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index b7c26518..10e367b9 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -50,8 +50,12 @@ class scene: public che std::vector texture_name; std::vector textures; + vertex * texcoords = nullptr; + public: + scene(const std::string & file); ~scene(); + void read_file(const std::string & file); bool load_obj(const std::string & file); bool load_mtl(const std::string & file); bool load_texture(const std::string & file); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 267a2877..ef69cff0 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -30,9 +30,7 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "off") return new che_off(file_path); if(extension == "obj") { - scene sc; - //sc.load_mtl(file_path.substr(0, pos) + ".mtl"); - che_obj::parser p(file_path); + scene sc(file_path); return new che_obj(file_path); } if(extension == "ply") return new che_ply(file_path); diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index d6ee561b..21cac2eb 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -20,7 +20,10 @@ void che_obj::read_file(const std::string & file) alloc(p.vertices.size(), p.faces.size() / che::mtrig); memcpy(GT, p.vertices.data(), p.vertices.size() * sizeof(vertex)); memcpy(VC, p.vcolors.data(), p.vcolors.size() * sizeof(rgb_t)); - memcpy(VT, p.faces.data(), p.faces.size() * sizeof(index_t)); + + #pragma omp parallel for + for(index_t i = 0; i < p.faces.size(); ++i) + VT[i] = p.faces[i].x(); } void che_obj::write_file(const che * mesh, const std::string & file, const bool & color, const bool & pointcloud) @@ -63,9 +66,10 @@ che_obj::parser::parser(const std::string & file) FILE * fp = fopen(file.c_str(), "r"); assert(fp); - std::vector P; + std::vector P; uvec3 vtn; - index_t n; + bool neg = false; + index_t n = 0; float x, y, z, r, g, b; char line[256], str[64]; @@ -75,26 +79,49 @@ che_obj::parser::parser(const std::string & file) { case 'f': { - P.clear(); vtn = {}; n = 0; + P.clear(); vtn = {}; n = 0; neg = false; for(index_t i = 2; line[i]; ++i) + { switch(line[i]) { - case '/': ++n; break; + case '/': + if(neg) vtn[n] = 0 - vtn[n]; + neg = false; + ++n; + break; case '\n': case ' ': - P.push_back(vtn[0]); + if(neg) vtn[n] = 0 - vtn[n]; + if(vtn[0] != 0) P.push_back(vtn); vtn = {}; n = 0; + neg = false; break; + case '-': + neg = true; break; default: - vtn[n] = 10 * vtn[n] + line[i] - '0'; - break; + if('0' <= line[i] && line[i] <= '9') + vtn[n] = 10 * vtn[n] + line[i] - '0'; + } + } + + for(uvec3 & f: P) + for(int i = 0; i < 3; ++i) + { + if(!f[i]) + { + f[i] = NIL; + break; } + f[i] += f[i] > vertices.size() ? vertices.size() : -1; + } - for(index_t & i: P) - i += i > vertices.size() ? vertices.size() : -1; + for(index_t i = 2; i < P.size(); ++i) + { + faces.push_back(P[0]); + faces.push_back(P[i - 1]); + faces.push_back(P[i]); + } - for(const index_t & v: trig_convex_polygon(P.data(), P.size())) - faces.push_back(v); break; } case 'v': @@ -110,7 +137,7 @@ che_obj::parser::parser(const std::string & file) vnormals.push_back({x, y, z}); break; case 't': - vtexcoords.push_back({x, y, z}); + vtexcoords.push_back({x, y}); break; } break; @@ -132,16 +159,14 @@ che_obj::parser::parser(const std::string & file) fclose(fp); + objects.emplace_back("", faces.size()); + gproshan_error_var(vertices.size()); gproshan_error_var(vnormals.size()); gproshan_error_var(vtexcoords.size()); gproshan_error_var(vcolors.size()); gproshan_error_var(faces.size()); gproshan_error_var(objects.size()); - for(auto & o: objects) - gproshan_log_var(o.first); - for(auto & mtllib: mtllibs) - gproshan_log_var(mtllib); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index d1fe8e6d..58d8c807 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -1,5 +1,7 @@ #include "gproshan/scenes/scene.h" +#include "gproshan/mesh/che_obj.h" + #include using namespace cimg_library; @@ -9,14 +11,39 @@ using namespace cimg_library; namespace gproshan { +scene::scene(const std::string & file) +{ + init(file); +} + scene::~scene() { for(texture & tex: textures) delete tex.data; } +void scene::read_file(const std::string & file) +{ + load_obj(file); +} + bool scene::load_obj(const std::string & file) { + const che_obj::parser p(file); + + const std::string path = file.substr(0, file.rfind('/') + 1); + for(auto & m: p.mtllibs) + if(!load_mtl(path + m)) + return false; + + alloc(p.faces.size(), 0); + + #pragma omp parallel for + for(index_t i = 0; i < n_vertices; ++i) + { + const index_t & v = p.faces[i].x(); + GT[i] = p.vertices[v]; + } return true; } From 698b958db666899d174e0f6a8507e524edd0eabd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 15:12:41 +0100 Subject: [PATCH 0748/1018] scene: load obj as point cloud --- include/gproshan/mesh/che_obj.h | 4 ++-- include/gproshan/scenes/scene.h | 2 +- src/gproshan/app_viewer.cpp | 4 ++-- src/gproshan/mesh/che_obj.cpp | 33 +++++++++++++++++++-------------- src/gproshan/scenes/scene.cpp | 16 ++++++++++++++-- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/include/gproshan/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h index 3a27cea0..008e0d74 100644 --- a/include/gproshan/mesh/che_obj.h +++ b/include/gproshan/mesh/che_obj.h @@ -24,10 +24,10 @@ class che_obj : public che struct parser { std::vector vertices; - std::vector vnormals; std::vector vtexcoords; + std::vector vnormals; std::vector vcolors; - std::vector faces; + std::vector trigs; std::vector > objects; std::unordered_set mtllibs; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 10e367b9..bda56706 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -50,7 +50,7 @@ class scene: public che std::vector texture_name; std::vector textures; - vertex * texcoords = nullptr; + vec2 * texcoords = nullptr; public: scene(const std::string & file); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index ef69cff0..0192e51f 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -30,8 +30,8 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "off") return new che_off(file_path); if(extension == "obj") { - scene sc(file_path); - return new che_obj(file_path); + return new scene(file_path); +// return new che_obj(file_path); } if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 21cac2eb..3d0b6bef 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -17,13 +17,13 @@ che_obj::che_obj(const std::string & file) void che_obj::read_file(const std::string & file) { parser p(file); - alloc(p.vertices.size(), p.faces.size() / che::mtrig); + alloc(p.vertices.size(), p.trigs.size() / che::mtrig); memcpy(GT, p.vertices.data(), p.vertices.size() * sizeof(vertex)); memcpy(VC, p.vcolors.data(), p.vcolors.size() * sizeof(rgb_t)); #pragma omp parallel for - for(index_t i = 0; i < p.faces.size(); ++i) - VT[i] = p.faces[i].x(); + for(index_t i = 0; i < p.trigs.size(); ++i) + VT[i] = p.trigs[i].x(); } void che_obj::write_file(const che * mesh, const std::string & file, const bool & color, const bool & pointcloud) @@ -33,7 +33,7 @@ void che_obj::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "# OBJ generated by gproshan\n"); fprintf(fp, "# vertices %lu\n", mesh->n_vertices); - fprintf(fp, "# faces %lu\n", mesh->n_faces); + fprintf(fp, "# trigs %lu\n", mesh->n_faces); for(index_t i = 0; i < mesh->n_vertices; ++i) { @@ -71,7 +71,7 @@ che_obj::parser::parser(const std::string & file) bool neg = false; index_t n = 0; float x, y, z, r, g, b; - char line[256], str[64]; + char line[512], str[512]; while(fgets(line, sizeof(line), fp)) { @@ -92,7 +92,7 @@ che_obj::parser::parser(const std::string & file) case '\n': case ' ': if(neg) vtn[n] = 0 - vtn[n]; - if(vtn[0] != 0) P.push_back(vtn); + if(vtn[0]) P.push_back(vtn); vtn = {}; n = 0; neg = false; break; @@ -110,16 +110,21 @@ che_obj::parser::parser(const std::string & file) if(!f[i]) { f[i] = NIL; - break; + continue; + } + switch(i) + { + case 0: f[i] += f[i] > vertices.size() ? vertices.size() : -1; break; + case 1: f[i] += f[i] > vtexcoords.size() ? vtexcoords.size() : -1; break; + case 2: f[i] += f[i] > vnormals.size() ? vnormals.size() : -1; break; } - f[i] += f[i] > vertices.size() ? vertices.size() : -1; } for(index_t i = 2; i < P.size(); ++i) { - faces.push_back(P[0]); - faces.push_back(P[i - 1]); - faces.push_back(P[i]); + trigs.push_back(P[0]); + trigs.push_back(P[i - 1]); + trigs.push_back(P[i]); } break; @@ -145,7 +150,7 @@ che_obj::parser::parser(const std::string & file) case 'u': // usemtl { sscanf(line, "%*s %s", str); - objects.emplace_back(str, faces.size()); + objects.emplace_back(str, trigs.size()); break; } case 'm': // mtllib @@ -159,13 +164,13 @@ che_obj::parser::parser(const std::string & file) fclose(fp); - objects.emplace_back("", faces.size()); + objects.emplace_back("", trigs.size()); gproshan_error_var(vertices.size()); gproshan_error_var(vnormals.size()); gproshan_error_var(vtexcoords.size()); gproshan_error_var(vcolors.size()); - gproshan_error_var(faces.size()); + gproshan_error_var(trigs.size()); gproshan_error_var(objects.size()); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 58d8c807..6d798e30 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -20,6 +20,7 @@ scene::~scene() { for(texture & tex: textures) delete tex.data; + delete [] texcoords; } void scene::read_file(const std::string & file) @@ -36,13 +37,24 @@ bool scene::load_obj(const std::string & file) if(!load_mtl(path + m)) return false; - alloc(p.faces.size(), 0); + alloc(p.trigs.size(), 0); + texcoords = new vec2[n_vertices]; #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) { - const index_t & v = p.faces[i].x(); + const index_t & v = p.trigs[i].x(); + const index_t & t = p.trigs[i].y(); GT[i] = p.vertices[v]; + texcoords[i] = t != NIL ? p.vtexcoords[t] : vec2{-1, -1}; + } + + #pragma omp parallel for + for(index_t i = 0; i < n_vertices; ++i) + { + const index_t & trig = 3 * (i / 3); + const index_t & n = p.trigs[i].z(); + VN[i] = n != NIL ? p.vnormals[n] : normalize((GT[trig + 1] - GT[trig]) * (GT[trig + 2] - GT[trig])); } return true; From 244d7abdd9d6276f855c140a81b4ac8d8d482dd2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 15:20:25 +0100 Subject: [PATCH 0749/1018] scenes: loading color and ignoring textures with options mtllib file --- src/gproshan/scenes/scene.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 6d798e30..7412e946 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -46,6 +46,7 @@ bool scene::load_obj(const std::string & file) const index_t & v = p.trigs[i].x(); const index_t & t = p.trigs[i].y(); GT[i] = p.vertices[v]; + VC[i] = p.vcolors[v]; texcoords[i] = t != NIL ? p.vtexcoords[t] : vec2{-1, -1}; } @@ -117,6 +118,7 @@ bool scene::load_mtl(const std::string & file) { index_t & m = line[5] == 'a' ? materials.back().map_Ka : materials.back().map_Kd; sscanf(line, "%*s %s", str); + if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) { texture_id[str] = textures.size(); From 2068c97ffccd25c361e9be2ce29c6416adb6aa8b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 16:11:06 +0100 Subject: [PATCH 0750/1018] che_viewer: preparing for polymorphism --- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/scene_viewer.h | 20 ++++++++++ include/gproshan/viewer/viewer.h | 7 ++-- src/gproshan/app_viewer.cpp | 4 +- src/gproshan/viewer/viewer.cpp | 53 ++++++++++++++------------ 5 files changed, 55 insertions(+), 31 deletions(-) create mode 100644 include/gproshan/viewer/scene_viewer.h diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index a293a3d9..4187300b 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -41,7 +41,7 @@ class che_viewer fit_screen opt_fit_screen = box; - int vx = 0, vy = 0; ///< viewport positions. + int vx = 0, vy = 0; ///< viewport positions. std::vector selected; std::vector selected_xyz; rt::raytracing * rt_embree = nullptr; diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h new file mode 100644 index 00000000..d209189d --- /dev/null +++ b/include/gproshan/viewer/scene_viewer.h @@ -0,0 +1,20 @@ +#ifndef SCENE_VIEWER_H +#define SCENE_VIEWER_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class scene_viewer: public che_viewer +{ + virtual void draw(shader & program); +}; + + +} // namespace gproshan + +#endif // SCENE_VIEWER_H + diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 78f37325..8743917b 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -49,7 +49,7 @@ class viewer }; static const std::vector m_window_split; - static const size_t max_n_meshes; + static const size_t max_meshes; static const std::vector colormap; bool apply_all_meshes = false; @@ -75,9 +75,8 @@ class viewer quaternion cam_light; double render_time = 0; - - che_viewer * meshes = nullptr; - size_t n_meshes = 0; + + std::vector meshes; index_t idx_active_mesh = 0; frame * frames = nullptr; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 0192e51f..4ac2e335 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -16,8 +16,8 @@ namespace gproshan { app_viewer::~app_viewer() { - for(index_t i = 0; i < n_meshes; ++i) - delete meshes[i]; + for(auto & m: meshes) + delete *m; } che * app_viewer::load_mesh(const std::string & file_path) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 6fab4376..44ae885c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -39,7 +39,7 @@ const std::vector viewer::m_window_split = { {1, 1}, {4, 5}, {4, 5}, {4, 5}, {4, 5} }; -const size_t viewer::max_n_meshes = m_window_split.size() - 1; +const size_t viewer::max_meshes = m_window_split.size() - 1; const std::vector viewer::colormap = { "vertex color", "blue", @@ -65,8 +65,7 @@ viewer::viewer(const int & width, const int & height) s->update_normals(); sphere.init(s, false); - frames = new frame[max_n_meshes]; - meshes = new che_viewer[max_n_meshes]; + frames = new frame[max_meshes]; render_params.add_light({-1, 1, -2}); } @@ -82,7 +81,9 @@ viewer::~viewer() delete sphere; delete [] frames; - delete [] meshes; + + for(che_viewer * m: meshes) + delete m; } bool viewer::run() @@ -126,12 +127,15 @@ void viewer::imgui() { if(ImGui::BeginMenu("Select")) { - for(index_t i = 0; i < n_meshes; ++i) - if(ImGui::MenuItem((std::to_string(i) + ": " + meshes[i]->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + for(index_t i = 0; i < meshes.size(); ++i) + { + const che_viewer & m = *meshes[i]; + if(ImGui::MenuItem((std::to_string(i) + ": " + m->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) { idx_active_mesh = i; - glfwSetWindowTitle(window, mesh->filename.c_str()); + glfwSetWindowTitle(window, m->filename.c_str()); } + } ImGui::EndMenu(); } @@ -275,7 +279,7 @@ void viewer::imgui() che_viewer & viewer::active_mesh() { - return meshes[idx_active_mesh]; + return *meshes[idx_active_mesh]; } void viewer::info_gl() @@ -415,25 +419,26 @@ void viewer::add_process(const int & key, const std::string & skey, const std::s bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) { - if(n_meshes == max_n_meshes) + if(meshes.size() == max_meshes) return false; if(reset_normals) p_mesh->update_normals(); - che_viewer & mesh = meshes[n_meshes]; + meshes.push_back(new che_viewer); + che_viewer & mesh = *meshes.back(); mesh.init(p_mesh); mesh.log_info(); - idx_active_mesh = n_meshes++; + idx_active_mesh = meshes.size() - 1; glfwSetWindowTitle(window, mesh->filename.c_str()); - const int & rows = m_window_split[n_meshes].x(); - const int & cols = m_window_split[n_meshes].y(); - for(index_t m = 0; m < n_meshes; ++m) + const int & rows = m_window_split[meshes.size()].x(); + const int & cols = m_window_split[meshes.size()].y(); + for(index_t m = 0; m < meshes.size(); ++m) { - meshes[m].vx = m % cols; - meshes[m].vy = rows - (m / cols) - 1; + meshes[m]->vx = m % cols; + meshes[m]->vy = rows - (m / cols) - 1; } glfwGetFramebufferSize(window, &viewport_width, &viewport_height); @@ -449,8 +454,8 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - view->viewport_width = width / m_window_split[view->n_meshes].y(); - view->viewport_height = height / m_window_split[view->n_meshes].x(); + view->viewport_width = width / m_window_split[view->meshes.size()].y(); + view->viewport_height = height / m_window_split[view->meshes.size()].x(); view->cam.aspect = real_t(view->viewport_width) / view->viewport_height; view->proj_mat = view->cam.perspective(); } @@ -495,9 +500,9 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod const index_t & ix = xpos * xscale; const index_t & iy = ypos * yscale; - const int & cols = m_window_split[view->n_meshes].y(); + const int & cols = m_window_split[view->meshes.size()].y(); const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; - if(idx_mesh < view->n_meshes) + if(idx_mesh < view->meshes.size()) view->idx_active_mesh = idx_mesh; if(mods == GLFW_MOD_SHIFT) @@ -965,9 +970,9 @@ void viewer::render_gl() glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); - for(index_t i = 0; i < n_meshes; ++i) + for(index_t i = 0; i < meshes.size(); ++i) { - che_viewer & mesh = meshes[i]; + che_viewer & mesh = *meshes[i]; glViewport(mesh.vx * viewport_width, mesh.vy * viewport_height, viewport_width, viewport_height); @@ -1038,8 +1043,8 @@ void viewer::check_apply_all_meshes(const std::function & fu return; } - for(index_t i = 0; i < n_meshes; ++i) - fun(meshes[i]); + for(auto & m: meshes) + fun(*m); } From 5f61b14743c989a907f637ee40ce1cfe84a1ffc2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 16:32:37 +0100 Subject: [PATCH 0751/1018] viewer: using che_viewer constructor and vector of pointers --- include/gproshan/viewer/che_viewer.h | 3 +-- include/gproshan/viewer/scene_viewer.h | 3 ++- include/gproshan/viewer/viewer.h | 6 ++++-- src/gproshan/viewer/che_viewer.cpp | 19 ++++++++----------- src/gproshan/viewer/viewer.cpp | 23 ++++++++++++----------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 4187300b..52b770d8 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -62,14 +62,13 @@ class che_viewer bool render_flat = false; public: - che_viewer() = default; + che_viewer(che * m); virtual ~che_viewer(); che *& operator -> (); che *const & operator -> () const; operator che *& (); - void init(che * m, const bool & center = true); void update(); void update_model_mat(); void update_vbo(); diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index d209189d..7d305e67 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -10,7 +10,8 @@ namespace gproshan { class scene_viewer: public che_viewer { - virtual void draw(shader & program); + public: + virtual void draw(shader & program); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 8743917b..a9029dc3 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -1,6 +1,7 @@ #ifndef VIEWER_H #define VIEWER_H +#include #include #include #include @@ -51,6 +52,7 @@ class viewer static const std::vector m_window_split; static const size_t max_meshes; static const std::vector colormap; + static che_sphere sphere_data; bool apply_all_meshes = false; @@ -84,8 +86,8 @@ class viewer float bgc = 0; std::map processes; - - che_viewer sphere; + + che_viewer * sphere = nullptr; shader shader_sphere; std::vector sphere_points; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index f956029a..2815cb89 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -12,6 +12,14 @@ namespace gproshan { +che_viewer::che_viewer(che * m): mesh(m) +{ + glGenVertexArrays(1, &vao); + glGenBuffers(6, vbo); + + update(); +} + che_viewer::~che_viewer() { delete rt_embree; @@ -36,17 +44,6 @@ che_viewer::operator che *& () return mesh; } -void che_viewer::init(che * m, const bool & center) -{ - glGenVertexArrays(1, &vao); - glGenBuffers(6, vbo); - - mesh = m; - center_mesh = center; - - update(); -} - void che_viewer::update() { update_model_mat(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 44ae885c..d9f5b363 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include @@ -48,6 +47,8 @@ const std::vector viewer::colormap = { "vertex color", "set" }; +che_sphere viewer::sphere_data = {0.01}; + viewer::viewer(const int & width, const int & height) { window_width = width; @@ -60,10 +61,9 @@ viewer::viewer(const int & width, const int & height) info_gl(); gproshan_log_var(sizeof(real_t)); - - che * s = new che_sphere(0.01); - s->update_normals(); - sphere.init(s, false); + + sphere_data.update_normals(); + sphere = new che_viewer(&sphere_data);; frames = new frame[max_meshes]; @@ -79,7 +79,9 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); + delete *sphere; delete sphere; + delete [] frames; for(che_viewer * m: meshes) @@ -425,9 +427,8 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) if(reset_normals) p_mesh->update_normals(); - meshes.push_back(new che_viewer); + meshes.push_back(new che_viewer(p_mesh)); che_viewer & mesh = *meshes.back(); - mesh.init(p_mesh); mesh.log_info(); idx_active_mesh = meshes.size() - 1; @@ -993,13 +994,13 @@ void viewer::render_gl() if(mesh.render_gradients) mesh.draw(shader_gradient); - mesh.draw_selected_vertices(sphere, shader_sphere); + mesh.draw_selected_vertices(*sphere, shader_sphere); if(sphere_points.size()) { - sphere.model_mat = mat4::identity(); - sphere.update_instances_positions(sphere_points); - sphere.draw(shader_sphere); + sphere->model_mat = mat4::identity(); + sphere->update_instances_positions(sphere_points); + sphere->draw(shader_sphere); } } From 89b0e51585b0fb3e63b6bd88ababc5a0f18b29a6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 16:34:55 +0100 Subject: [PATCH 0752/1018] scene_viewer: adding class to render scenes --- include/gproshan/viewer/viewer.h | 4 ++-- src/gproshan/viewer/scene_viewer.cpp | 1 + src/gproshan/viewer/viewer.cpp | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 src/gproshan/viewer/scene_viewer.cpp diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a9029dc3..09fca0c4 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -77,7 +77,7 @@ class viewer quaternion cam_light; double render_time = 0; - + std::vector meshes; index_t idx_active_mesh = 0; @@ -86,7 +86,7 @@ class viewer float bgc = 0; std::map processes; - + che_viewer * sphere = nullptr; shader shader_sphere; std::vector sphere_points; diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp new file mode 100644 index 00000000..92469751 --- /dev/null +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -0,0 +1 @@ +#include diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index d9f5b363..cb3a9b91 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -61,7 +61,7 @@ viewer::viewer(const int & width, const int & height) info_gl(); gproshan_log_var(sizeof(real_t)); - + sphere_data.update_normals(); sphere = new che_viewer(&sphere_data);; From 210bd58d664d654ccc4103f8e7c7655fddf0c035 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 17:07:31 +0100 Subject: [PATCH 0753/1018] scene_viewer: drawing triangular scene --- CHANGELOG.md | 3 ++- include/gproshan/viewer/scene_viewer.h | 2 +- src/gproshan/viewer/scene_viewer.cpp | 33 ++++++++++++++++++++++++++ src/gproshan/viewer/viewer.cpp | 3 +-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02b88f0b..75e768dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version History --------------- ### gproshan 3.14 +- Added scene rendering: loader from .obj files and .mtl, handling textures - Exporting gproshan as cmake library, use find_package(gproshan) in your project. - Added Intel Embree as default ray tracing library, for ray casting operations and as rendering option with shadows. - Adding Scenes module, virtual point cloud scanners and point cloud normals computation for 3D scenes. @@ -15,5 +16,5 @@ Version History - Implemented the loading and rendering of point clouds. - Added heatmap viewer options and loading vertex color from a file. - New user interface implemented with [ImGui](https://github.com/ocornut/imgui). -- Viewer upgraded using GLEW, GLFW, and GLM. +- Viewer upgraded using GLEW and GLFW3. diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index 7d305e67..d57bb037 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -11,7 +11,7 @@ namespace gproshan { class scene_viewer: public che_viewer { public: - virtual void draw(shader & program); + void draw(shader & program); }; diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 92469751..1287efa9 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -1 +1,34 @@ #include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +void scene_viewer::draw(shader & program) +{ + glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); + glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); + glProgramUniform1i(program, program("render_flat"), render_flat); + glProgramUniform1i(program, program("render_lines"), render_lines); + glProgramUniform1i(program, program("render_wireframe"), render_triangles); + + glPolygonMode(GL_FRONT_AND_BACK, render_wireframe ? GL_LINE : GL_FILL); + + program.enable(); + + glBindVertexArray(vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]); + + + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + program.disable(); + +} + + +} // namespace gproshan + diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cb3a9b91..641b8d64 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -47,7 +47,7 @@ const std::vector viewer::colormap = { "vertex color", "set" }; -che_sphere viewer::sphere_data = {0.01}; +che_sphere viewer::sphere_data{0.01}; viewer::viewer(const int & width, const int & height) { @@ -79,7 +79,6 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); - delete *sphere; delete sphere; delete [] frames; From 79413e5b0de056294fa6bc7733e3a11a02974f26 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 22 Nov 2022 22:40:03 +0100 Subject: [PATCH 0754/1018] scene: rendering triangular scenes fix rt scene --- include/gproshan/mesh/che.h | 3 ++- include/gproshan/scenes/scene.h | 2 ++ include/gproshan/viewer/scene_viewer.h | 5 +++++ src/gproshan/mesh/che.cpp | 5 +++++ src/gproshan/scenes/scene.cpp | 10 ++++++++++ src/gproshan/viewer/scene_viewer.cpp | 10 +++++----- src/gproshan/viewer/viewer.cpp | 4 ++-- 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index a50fc701..6e8ae11b 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -135,7 +135,8 @@ class che real_t mean_edge() const; real_t area_surface() const; bool is_manifold() const; - bool is_pointcloud() const; + virtual bool is_scene() const; + virtual bool is_pointcloud() const; // operation methods void flip(const index_t & e); diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index bda56706..3a743bd1 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -55,6 +55,8 @@ class scene: public che public: scene(const std::string & file); ~scene(); + bool is_scene() const; + bool is_pointcloud() const; void read_file(const std::string & file); bool load_obj(const std::string & file); bool load_mtl(const std::string & file); diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index d57bb037..ee17d391 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -2,6 +2,7 @@ #define SCENE_VIEWER_H #include +#include // geometry processing and shape analysis framework @@ -10,7 +11,11 @@ namespace gproshan { class scene_viewer: public che_viewer { + private: + scene * sc = nullptr; + public: + scene_viewer(scene * p_sc); void draw(shader & program); }; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index bc895332..70fb132d 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -826,6 +826,11 @@ bool che::is_manifold() const return manifold; } +bool che::is_scene() const +{ + return false; +} + bool che::is_pointcloud() const { return n_faces == 0; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 7412e946..8fb18dfe 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -23,6 +23,16 @@ scene::~scene() delete [] texcoords; } +bool scene::is_scene() const +{ + return true; +} + +bool scene::is_pointcloud() const +{ + return false; +} + void scene::read_file(const std::string & file) { load_obj(file); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 1287efa9..2caa1cfb 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -5,6 +5,10 @@ namespace gproshan { +scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) +{ +} + void scene_viewer::draw(shader & program) { glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); @@ -18,11 +22,7 @@ void scene_viewer::draw(shader & program) program.enable(); glBindVertexArray(vao); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]); - - - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDrawArrays(GL_TRIANGLES, 0, mesh->n_vertices); glBindVertexArray(0); program.disable(); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 641b8d64..c032ba97 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -426,7 +427,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) if(reset_normals) p_mesh->update_normals(); - meshes.push_back(new che_viewer(p_mesh)); + meshes.push_back(p_mesh->is_scene() ? new scene_viewer((scene *) p_mesh) : new che_viewer(p_mesh)); che_viewer & mesh = *meshes.back(); mesh.log_info(); @@ -447,7 +448,6 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) cam.aspect = real_t(viewport_width) / viewport_height; proj_mat = cam.perspective(); - return true; } From f322647fcd183990c172b0872f02814adf6de604 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 00:26:10 +0100 Subject: [PATCH 0755/1018] scene_viewer: init gl textures --- include/gproshan/scenes/scene.h | 5 +---- include/gproshan/viewer/scene_viewer.h | 3 +++ src/gproshan/scenes/scene.cpp | 4 ++++ src/gproshan/viewer/scene_viewer.cpp | 28 +++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 3a743bd1..58f5a184 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -5,7 +5,6 @@ #include #include -#include // geometry processing and shape analysis framework @@ -41,12 +40,10 @@ class scene: public che index_t idm = NIL; }; - protected: - std::unordered_map material_id; + public: std::vector material_name; std::vector materials; - std::unordered_map texture_id; std::vector texture_name; std::vector textures; diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index ee17d391..c7851562 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -13,9 +13,12 @@ class scene_viewer: public che_viewer { private: scene * sc = nullptr; + GLuint * gltextures = nullptr; public: scene_viewer(scene * p_sc); + ~scene_viewer(); + void init_texture(const GLuint & gltex, const scene::texture & tex); void draw(shader & program); }; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 8fb18dfe..40d3f6c0 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -2,6 +2,7 @@ #include "gproshan/mesh/che_obj.h" +#include #include using namespace cimg_library; @@ -78,6 +79,9 @@ bool scene::load_mtl(const std::string & file) FILE * fp = fopen(file.c_str(), "r"); if(!fp) return false; + std::unordered_map material_id; + std::unordered_map texture_id; + char line[256], str[64]; while(fgets(line, sizeof(line), fp)) { diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 2caa1cfb..bc45e054 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -7,6 +7,33 @@ namespace gproshan { scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) { + gltextures = new GLuint[sc->textures.size()]; + + glGenTextures(sc->textures.size(), gltextures); + for(index_t i = 0; i < sc->textures.size(); ++i) + { + gproshan_log_var(sc->texture_name[i]); + init_texture(gltextures[i], sc->textures[i]); + } +} + +scene_viewer::~scene_viewer() +{ + glDeleteTextures(sc->textures.size(), gltextures); + delete [] gltextures; +} + +void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex) +{ + glBindTexture(GL_TEXTURE_2D, gltex); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.cols, tex.rows, 0, GL_RGB, GL_FLOAT, tex.data); + glGenerateMipmap(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, 0); } void scene_viewer::draw(shader & program) @@ -26,7 +53,6 @@ void scene_viewer::draw(shader & program) glBindVertexArray(0); program.disable(); - } From 5c3d1be8811f69bd3d445c27adb39414f6c1bc63 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 01:14:16 +0100 Subject: [PATCH 0756/1018] scene: fix parser .obj, testing Kd as vertex color --- include/gproshan/scenes/scene.h | 2 ++ src/gproshan/mesh/che_obj.cpp | 3 +++ src/gproshan/scenes/scene.cpp | 26 ++++++++++++++++++++------ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 58f5a184..f730b404 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -5,6 +5,7 @@ #include #include +#include // geometry processing and shape analysis framework @@ -41,6 +42,7 @@ class scene: public che }; public: + std::unordered_map material_id; std::vector material_name; std::vector materials; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 3d0b6bef..6ba53622 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -103,6 +103,8 @@ che_obj::parser::parser(const std::string & file) vtn[n] = 10 * vtn[n] + line[i] - '0'; } } + if(neg) vtn[n] = 0 - vtn[n]; + if(vtn[0]) P.push_back(vtn); for(uvec3 & f: P) for(int i = 0; i < 3; ++i) @@ -135,6 +137,7 @@ che_obj::parser::parser(const std::string & file) switch(line[1]) { case ' ': + case '\t': vertices.push_back({x, y, z}); n == 6 ? vcolors.emplace_back(r, g, b) : vcolors.emplace_back(); break; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 40d3f6c0..ebfd69d2 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -2,7 +2,6 @@ #include "gproshan/mesh/che_obj.h" -#include #include using namespace cimg_library; @@ -69,6 +68,21 @@ bool scene::load_obj(const std::string & file) VN[i] = n != NIL ? p.vnormals[n] : normalize((GT[trig + 1] - GT[trig]) * (GT[trig + 2] - GT[trig])); } + if(p.objects.size()) + { +// #pragma omp parallel for + for(index_t i = 0; i < p.objects.size() - 1; ++i) + { + const auto & obj = p.objects[i]; + const material & m = materials[material_id[obj.first]]; + gproshan_error_var(m.Kd); + + const index_t & end = p.objects[i + 1].second; + for(index_t j = obj.second; j < end; ++j) + VC[j] = {m.Kd.x(), m.Kd.y(), m.Kd.z()}; + } + } + return true; } @@ -79,13 +93,13 @@ bool scene::load_mtl(const std::string & file) FILE * fp = fopen(file.c_str(), "r"); if(!fp) return false; - std::unordered_map material_id; std::unordered_map texture_id; char line[256], str[64]; while(fgets(line, sizeof(line), fp)) { - switch(line[0]) + sscanf(line, "%s", str); + switch(str[0]) { case 'n': // newmtl { @@ -97,8 +111,8 @@ bool scene::load_mtl(const std::string & file) } case 'K': // Ka, Kd, Ks { - vec3 & rgb = line[1] == 'a' ? materials.back().Ka - : line[1] == 'd' ? materials.back().Kd + vec3 & rgb = str[1] == 'a' ? materials.back().Ka + : str[1] == 'd' ? materials.back().Kd : materials.back().Ks; sscanf(line, "%*s %f %f %f", &rgb.x(), &rgb.y(), &rgb.z()); break; @@ -130,7 +144,7 @@ bool scene::load_mtl(const std::string & file) } case 'm': // map_Ka, map_kd { - index_t & m = line[5] == 'a' ? materials.back().map_Ka : materials.back().map_Kd; + index_t & m = str[5] == 'a' ? materials.back().map_Ka : materials.back().map_Kd; sscanf(line, "%*s %s", str); if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) From f094c2f15647bd7a90e501de2a41f547242233e7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 15:16:29 +0100 Subject: [PATCH 0757/1018] scene: rendering with embree and optix --- src/gproshan/raytracing/rt_embree.cpp | 35 +++++++++++++++++++++------ src/gproshan/raytracing/rt_optix.cpp | 18 ++++++++++++-- src/gproshan/scenes/scene.cpp | 3 +-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 35dc7812..e9b0f601 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -76,6 +76,9 @@ embree::embree(const std::vector & meshes, const std::vector & mode embree::~embree() { + for(CHE * m: g_meshes) + delete m; + rtcReleaseScene(scene); rtcReleaseDevice(device); } @@ -129,8 +132,9 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_faces || pointcloud) g_meshes[i]->n_faces = 0; - const index_t & geomID = g_meshes[i]->n_faces ? add_mesh(meshes[i], model_mats[i]) : - add_pointcloud(meshes[i], model_mats[i]); + const index_t & geomID = g_meshes[i]->n_faces || meshes[i]->is_scene() ? + add_mesh(meshes[i], model_mats[i]) : + add_pointcloud(meshes[i], model_mats[i]); gproshan_error_var(i == geomID); } @@ -166,23 +170,40 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) mesh->n_vertices ); + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + vertices[i] = model_mat * vec4(mesh->point(i), 1); + index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(index_t), - mesh->n_faces + mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_faces ); - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - vertices[i] = model_mat * vec4(mesh->point(i), 1); - memcpy(tri_idxs, &mesh->halfedge(0), mesh->n_half_edges * sizeof(index_t)); + if(mesh->is_scene()) + { + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + tri_idxs[i] = i; + } + else + { + memcpy(tri_idxs, &mesh->halfedge(0), mesh->n_half_edges * sizeof(index_t)); + } rtcCommitGeometry(geom); index_t geom_id = rtcAttachGeometry(scene, geom); rtcReleaseGeometry(geom); + if(mesh->is_scene()) + { + g_meshes[geom_id]->VT = tri_idxs; + g_meshes[geom_id]->n_faces = mesh->n_vertices / 3; + g_meshes[geom_id]->n_half_edges = mesh->n_vertices; + } + return geom_id; } diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index a0760f16..cbf054c3 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -412,6 +412,17 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u CHE * dd_m, * d_m; CHE h_m(mesh); + if(mesh->is_scene()) + { + h_m.n_half_edges = mesh->n_vertices; + h_m.n_faces = mesh->n_vertices / 3; + h_m.VT = new index_t[mesh->n_vertices]; + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + h_m.VT[i] = i; + } + cuda_create_CHE(&h_m, dd_m, d_m, true, true); dd_mesh.push_back(dd_m); d_mesh.push_back(d_m); @@ -427,12 +438,12 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); - optix_mesh.triangleArray.numVertices = mesh->n_vertices; + optix_mesh.triangleArray.numVertices = h_m.n_vertices; optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = mesh->n_faces; + optix_mesh.triangleArray.numIndexTriplets = h_m.n_faces; optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; @@ -445,6 +456,9 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; + + if(mesh->is_scene()) + delete [] h_m.VT; } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index ebfd69d2..ad2b09dd 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -70,12 +70,11 @@ bool scene::load_obj(const std::string & file) if(p.objects.size()) { -// #pragma omp parallel for + #pragma omp parallel for for(index_t i = 0; i < p.objects.size() - 1; ++i) { const auto & obj = p.objects[i]; const material & m = materials[material_id[obj.first]]; - gproshan_error_var(m.Kd); const index_t & end = p.objects[i + 1].second; for(index_t j = obj.second; j < end; ++j) From b08186c477505cf9c4fb49de06b3f61fb9a037fc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 15:37:59 +0100 Subject: [PATCH 0758/1018] che: vertex to rgb_t --- include/gproshan/mesh/che.h | 1 + src/gproshan/CMakeLists.txt | 2 +- src/gproshan/mesh/che.cpp | 2 ++ src/gproshan/raytracing/rt_embree.cpp | 2 +- src/gproshan/scenes/scanner.cpp | 5 +---- src/gproshan/scenes/scene.cpp | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 6e8ae11b..0cee1f5d 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -33,6 +33,7 @@ class che unsigned char b = 250; rgb_t() = default; + rgb_t(const vertex & v); rgb_t(const float & fr, const float & fg, const float & fb); unsigned char & operator [] (const index_t & i); operator vertex () const; diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 9b1be818..f03f5a6c 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -36,7 +36,7 @@ if(OptiX_INCLUDE) set_property(TARGET optix_ptx PROPERTY CUDA_PTX_COMPILATION ON) add_custom_target( copy_optix_ptx ALL DEPENDS optix_ptx - COMMAND ${CMAKE_COMMAND} -E copy $ ${gproshan_SOURCE_DIR}/src/) + COMMAND ${CMAKE_COMMAND} -E copy $ ${gproshan_SOURCE_DIR}/src/) endif(OptiX_INCLUDE) diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 70fb132d..0ce54cd2 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -16,6 +16,8 @@ size_t & che::rw(const size_t & n) } +che::rgb_t::rgb_t(const vertex & v): rgb_t(v.x(), v.y(), v.z()) {} + che::rgb_t::rgb_t(const float & fr, const float & fg, const float & fb) { r = (unsigned char) (fr * 255); diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index e9b0f601..95d44238 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -250,7 +250,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; - return eval_li( hit, lights, n_lights, + return eval_li( hit, lights, n_lights, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 94ec3839..a7dee440 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -35,10 +35,7 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c mesh_ptx->point(v) = h.position; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; - mesh_ptx->rgb(v) = { (unsigned char) (h.color.x() * 255), - (unsigned char) (h.color.y() * 255), - (unsigned char) (h.color.z() * 255) - }; + mesh_ptx->rgb(v) = h.color; } else { diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index ad2b09dd..6caff3ce 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -78,7 +78,7 @@ bool scene::load_obj(const std::string & file) const index_t & end = p.objects[i + 1].second; for(index_t j = obj.second; j < end; ++j) - VC[j] = {m.Kd.x(), m.Kd.y(), m.Kd.z()}; + VC[j] = m.Kd; } } From 3f91a8d5183591226e4f008aabf864014b7c66cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 15:53:03 +0100 Subject: [PATCH 0759/1018] mesh: rename faces to trigs --- include/gproshan/mdict/patch.h | 2 +- include/gproshan/mesh/che.h | 12 +- src/gproshan/app_viewer.cpp | 4 +- src/gproshan/features/key_points.cpp | 6 +- .../test_geodesics_ptp_coalescence.cu | 8 +- src/gproshan/mdict/patch.cpp | 4 +- src/gproshan/mesh/che.cpp | 54 +++---- src/gproshan/mesh/che.cu | 2 +- src/gproshan/mesh/che_fill_hole.cpp | 150 +++++++++--------- src/gproshan/mesh/che_obj.cpp | 2 +- src/gproshan/mesh/che_off.cpp | 14 +- src/gproshan/mesh/che_ply.cpp | 16 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 52 +++--- src/gproshan/raytracing/rt_embree.cpp | 12 +- src/gproshan/raytracing/rt_optix.cpp | 4 +- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 2 +- 18 files changed, 174 insertions(+), 174 deletions(-) diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 84e80d9f..35c6177e 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -103,7 +103,7 @@ class patch void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t & p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); - bool add_vertex_by_faces( vertex & n, + bool add_vertex_by_trigs( vertex & n, std::vector & N, double thr_angle, const real_t * geo, diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 0cee1f5d..36980e55 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -40,7 +40,7 @@ class che }; const size_t n_vertices = 0; - const size_t n_faces = 0; + const size_t n_trigs = 0; const size_t n_half_edges = 0; const size_t n_edges = 0; @@ -48,7 +48,7 @@ class che protected: vertex * GT = nullptr; ///< geometry table : v -> vertex - index_t * VT = nullptr; ///< vertex table (faces) : he -> v + index_t * VT = nullptr; ///< vertex table (trigs) : he -> v index_t * OT = nullptr; ///< opposite table : he -> he index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * ET = nullptr; ///< edge table : e -> he @@ -63,7 +63,7 @@ class che public: che(const che & mesh); che(const size_t & n_v = 0, const size_t & n_f = 0); - che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); + che(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f); virtual ~che(); // vertex access geometry methods to xyz point values, normals, and gradient @@ -99,7 +99,7 @@ class che void remove_non_manifold_vertices(); void set_head_vertices(index_t * head, const size_t & n); - // half edge access methods triangular faces and navigation + // half edge access methods triangular trigs and navigation const index_t & halfedge(const index_t & he) const; const index_t & twin_he(const index_t & he) const; const index_t & edge_u(const index_t & e) const; @@ -148,7 +148,7 @@ class che real_t mean_curvature(const index_t & v) const; protected: - void init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f); + void init(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f); void init(const std::string & file); void alloc(const size_t & n_v, const size_t & n_f); void free(); @@ -217,7 +217,7 @@ inline index_t prev(const index_t & he) struct CHE { size_t n_vertices = 0; - size_t n_faces = 0; + size_t n_trigs = 0; size_t n_half_edges = 0; vertex * GT = nullptr; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 4ac2e335..6710aec5 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -862,7 +862,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) { const std::vector & vbounds = mesh->boundary(v); std::vector vertices; - std::vector faces; + std::vector trigs; vertex center; for(const index_t & v: vbounds) @@ -915,7 +915,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) // vertices.push_back(center); che * old = fill_mesh; - che * hole = new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); + che * hole = new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); fill_mesh = old->merge(hole, vbounds); delete old; delete hole; diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 1da52a38..9a3aa9d5 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -24,10 +24,10 @@ key_points::operator const std::vector & () const /// DOI: 10.1109/CLEI.2015.7359459 void key_points::compute_kps_areas(che * mesh, const real_t & percent) { - std::vector > face_areas(mesh->n_faces); + std::vector > face_areas(mesh->n_trigs); #pragma omp parallel for - for(index_t f = 0; f < mesh->n_faces; ++f) + for(index_t f = 0; f < mesh->n_trigs; ++f) face_areas[f] = { mesh->area_trig(f), f }; std::sort(begin(face_areas), end(face_areas)); @@ -35,7 +35,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); - for(index_t he, t = 0; t < mesh->n_faces; ++t) + for(index_t he, t = 0; t < mesh->n_trigs; ++t) { he = che::mtrig * face_areas[t].second; for(index_t i = 0; i < che::mtrig; ++i) diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 12e6e158..d577519b 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -23,7 +23,7 @@ std::vector > iter_error_parallel_toplesets_propagati // sort data by levels, must be improve the coalescence vertex * V = new vertex[mesh->n_vertices]; - index_t * F = new index_t[mesh->n_faces * che::mtrig]; + index_t * F = new index_t[mesh->n_trigs * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices]; real_t * exact_dist_sorted = new real_t[mesh->n_vertices]; @@ -39,7 +39,7 @@ std::vector > iter_error_parallel_toplesets_propagati for(index_t he = 0; he < mesh->n_half_edges; ++he) F[he] = inv[mesh->halfedge(he)]; - mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); + mesh = new che(V, mesh->n_vertices, F, mesh->n_trigs); delete [] V; delete [] F; @@ -105,7 +105,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect // BEGIN FPS PTP vertex * V = new vertex[mesh->n_vertices]; - index_t * F = new index_t[mesh->n_faces * che::mtrig]; + index_t * F = new index_t[mesh->n_trigs * che::mtrig]; index_t * inv = new index_t[mesh->n_vertices]; @@ -156,7 +156,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect for(index_t he = 0; he < mesh->n_half_edges; ++he) F[he] = inv[mesh->halfedge(he)]; - che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_faces); + che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_trigs); CHE * h_mesh = new CHE(tmp_mesh); CHE * dd_mesh, * d_mesh; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 6430e10e..27f0f9ef 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -77,7 +77,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_faces(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) +bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -267,7 +267,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, ratio = area / proj_area; - if(add_vertex_by_faces(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) + if(add_vertex_by_trigs(n, N, delta, params.dist_alloc, mesh, u, area, proj_area, M_PI / 2.5 ) && (ratio < sum_thres || (area / area_mesh) < area_thres) ) { euc_radio = std::max(euc_radio, norm(mesh->point(u) - c)); return true; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 0ce54cd2..79a35599 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -40,7 +40,7 @@ che::che(const che & mesh) { filename = mesh.filename; - alloc(mesh.n_vertices, mesh.n_faces); + alloc(mesh.n_vertices, mesh.n_trigs); rw(n_edges) = mesh.n_edges; memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); @@ -59,9 +59,9 @@ che::che(const size_t & n_v, const size_t & n_f) alloc(n_v, n_f); } -che::che(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) +che::che(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f) { - init(vertices, n_v, faces, n_f); + init(vertices, n_v, trigs, n_f); } che::~che() @@ -276,7 +276,7 @@ che * che::merge(const che * mesh, const std::vector & vcommon) const size_t & n_vcommon = vcommon.size(); const size_t & n_vnew = mesh->n_vertices - n_vcommon; - che * new_mesh = new che(n_vertices + n_vnew, n_faces + mesh->n_faces); + che * new_mesh = new che(n_vertices + n_vnew, n_trigs + mesh->n_trigs); memcpy(new_mesh->GT, GT, sizeof(vertex) * n_vertices); memcpy(new_mesh->GT + n_vertices, mesh->GT + n_vcommon, sizeof(vertex) * n_vnew); @@ -321,7 +321,7 @@ void che::update_heatmap(const real_t * hm) void che::update_normals() { - if(!n_faces) return; + if(!n_trigs) return; #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) @@ -348,7 +348,7 @@ void che::multiplicate_vertices() vertex * old_GT = GT; index_t * old_VT = VT; size_t nv = n_vertices; - size_t nf = n_faces; + size_t nf = n_trigs; GT = nullptr; VT = nullptr; @@ -418,7 +418,7 @@ void che::remove_vertices(const std::vector & vertices) /* save in vectors */ std::vector new_vertices; std::vector removed; - std::vector new_faces; // each 3 + std::vector new_trigs; // each 3 gproshan_debug(removing vertex); for(index_t v = 0; v < n_vertices; ++v) @@ -450,15 +450,15 @@ void che::remove_vertices(const std::vector & vertices) for(index_t he = 0; he < n_half_edges; ++he) if(VT[he] != NIL) - new_faces.push_back(VT[he]); + new_trigs.push_back(VT[he]); else gproshan_error_var(he); gproshan_debug_var(new_vertices.size()); - gproshan_debug_var(new_faces.size()); + gproshan_debug_var(new_trigs.size()); gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); + init(new_vertices.data(), new_vertices.size(), new_trigs.data(), new_trigs.size() / che::mtrig); gproshan_debug(removing vertex); } @@ -477,7 +477,7 @@ void che::remove_non_manifold_vertices() /* save in vectors */ std::vector new_vertices; std::vector removed; - std::vector new_faces; // each 3 + std::vector new_trigs; // each 3 gproshan_debug(removing vertex); for(index_t v = 0; v < n_vertices; ++v) @@ -509,15 +509,15 @@ void che::remove_non_manifold_vertices() for(index_t he = 0; he < n_half_edges; ++he) if(VT[he] != NIL) - new_faces.push_back(VT[he]); + new_trigs.push_back(VT[he]); else gproshan_error_var(he); gproshan_debug_var(new_vertices.size()); - gproshan_debug_var(new_faces.size()); + gproshan_debug_var(new_trigs.size()); gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_faces.data(), new_faces.size() / che::mtrig); + init(new_vertices.data(), new_vertices.size(), new_trigs.data(), new_trigs.size() / che::mtrig); gproshan_debug(removing vertex); } @@ -547,7 +547,7 @@ void che::set_head_vertices(index_t * head, const size_t & n) } -// half edge access methods triangular faces and navigation +// half edge access methods triangular trigs and navigation const index_t & che::halfedge(const index_t & he) const { @@ -686,7 +686,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector ///< return a vector of indices of one vertex per boundary std::vector che::bounds() const { - if(!n_faces) return {}; + if(!n_trigs) return {}; if(!manifold) return {}; std::vector vbounds; @@ -762,7 +762,7 @@ const std::string che::filename_size() const size_t che::genus() const { - size_t g = n_vertices - n_edges + n_faces; + size_t g = n_vertices - n_edges + n_trigs; return (g - 2) / (-2); } @@ -795,10 +795,10 @@ real_t che::quality() const real_t q = 0; #pragma omp parallel for reduction(+: q) - for(index_t t = 0; t < n_faces; ++t) + for(index_t t = 0; t < n_trigs; ++t) q += pdetriq(t) > 0.6; // is confederating good triangle - return q * 100 / n_faces; + return q * 100 / n_trigs; } real_t che::mean_edge() const @@ -817,7 +817,7 @@ real_t che::area_surface() const real_t area = 0; #pragma omp parallel for reduction(+: area) - for(index_t i = 0; i < n_faces; ++i) + for(index_t i = 0; i < n_trigs; ++i) area += area_trig(i); return area; @@ -835,7 +835,7 @@ bool che::is_scene() const bool che::is_pointcloud() const { - return n_faces == 0; + return n_trigs == 0; } @@ -958,12 +958,12 @@ real_t che::mean_curvature(const index_t & v) const // protected -void che::init(const vertex * vertices, const index_t & n_v, const index_t * faces, const index_t & n_f) +void che::init(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f) { alloc(n_v, n_f); memcpy(GT, vertices, n_vertices * sizeof(vertex)); - memcpy(VT, faces, n_half_edges * sizeof(index_t)); + memcpy(VT, trigs, n_half_edges * sizeof(index_t)); update_evt_ot_et(); update_eht(); @@ -981,8 +981,8 @@ void che::init(const std::string & file) void che::alloc(const size_t & n_v, const size_t & n_f) { rw(n_vertices) = n_v; - rw(n_faces) = n_f; - rw(n_half_edges) = che::mtrig * n_faces; + rw(n_trigs) = n_f; + rw(n_half_edges) = che::mtrig * n_trigs; rw(n_edges) = n_half_edges; // max number of edges if(n_vertices) GT = new vertex[n_vertices]; @@ -1016,7 +1016,7 @@ void che::read_file(const std::string & ) {} /* virtual */ void che::update_evt_ot_et() { - if(!n_faces) return; + if(!n_trigs) return; memset(EVT, -1, sizeof(index_t) * n_vertices); memset(OT, -1, sizeof(index_t) * n_half_edges); @@ -1154,7 +1154,7 @@ const index_t & che::star_he::iterator::operator * () CHE::CHE(const che * mesh) { n_vertices = mesh->n_vertices; - n_faces = mesh->n_faces; + n_trigs = mesh->n_trigs; n_half_edges = mesh->n_half_edges; GT = mesh->GT; diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index b140bf4a..33af8426 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -31,7 +31,7 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm { dd_che = new CHE; dd_che->n_vertices = h_che->n_vertices; - dd_che->n_faces = h_che->n_faces; + dd_che->n_trigs = h_che->n_trigs; dd_che->n_half_edges = h_che->n_half_edges; cudaMalloc(&dd_che->GT, sizeof(vertex) * h_che->n_vertices); diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 2e7f8e5d..7db8db7c 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -333,7 +333,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti std::priority_queue front; std::vector vertices; - std::vector faces; + std::vector trigs; for(index_t v: front_vertices) vertices.push_back(mesh->point(v)); @@ -434,9 +434,9 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti if(top.theta <= a75 || close_vertex) { - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(p_v); is_border[v] = false; @@ -458,13 +458,13 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); - faces.push_back(m_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(m_v); + trigs.push_back(v); + trigs.push_back(p_v); - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(m_v); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(m_v); // m_normal = normalise(normal_face(tmp_vertices, m_v, v, p_v) + normal_face(tmp_vertices, n_v, v, m_v)); tmp_normals.push_back(m_normal); @@ -498,17 +498,17 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti m_vec = top.new_vertex(tmp_vertices, 2./3, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); - faces.push_back(m_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(m_v); + trigs.push_back(v); + trigs.push_back(p_v); - faces.push_back(m_v + 1); - faces.push_back(v); - faces.push_back(m_v); + trigs.push_back(m_v + 1); + trigs.push_back(v); + trigs.push_back(m_v); - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(m_v + 1); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(m_v + 1); // m_normal = normalise(normal_face(tmp_vertices, m_v, v, p_v) + normal_face(tmp_vertices, m_v + 1, v, m_v)); tmp_normals.push_back(m_normal); @@ -560,8 +560,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti gproshan_debug_var(perimeter); // gproshan_debug(filling holes); // gproshan_debug_var(vertices.size()); -// gproshan_debug_var(faces.size()); - return faces.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); +// gproshan_debug_var(trigs.size()); + return trigs.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); } che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) @@ -571,7 +571,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng real_t init_perimeter = 0.0; std::priority_queue front; - std::vector faces; + std::vector trigs; // PCA -------------------------------------------------------------------------- @@ -694,9 +694,9 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng if(top.theta <= a75 || close_vertex) { - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(p_v); is_border[v] = false; @@ -726,13 +726,13 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o); tmp_vertices.push_back(m_vec); - faces.push_back(m_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(m_v); + trigs.push_back(v); + trigs.push_back(p_v); - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(m_v); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(m_v); is_border[v] = false; is_border.push_back(true); @@ -764,17 +764,17 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng m_vec = top.new_vertex(tmp_vertices, 2./3, length, neighbors[v], o); tmp_vertices.push_back(m_vec); - faces.push_back(m_v); - faces.push_back(v); - faces.push_back(p_v); + trigs.push_back(m_v); + trigs.push_back(v); + trigs.push_back(p_v); - faces.push_back(m_v + 1); - faces.push_back(v); - faces.push_back(m_v); + trigs.push_back(m_v + 1); + trigs.push_back(v); + trigs.push_back(m_v); - faces.push_back(n_v); - faces.push_back(v); - faces.push_back(m_v + 1); + trigs.push_back(n_v); + trigs.push_back(v); + trigs.push_back(m_v + 1); is_border[v] = false; is_border.push_back(true); @@ -821,7 +821,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng vertices.push_back({r[0], r[1], r[2]}); } - return faces.size() ? new che(vertices.data(), vertices.size(), faces.data(), faces.size() / 3) : nullptr; + return trigs.size() ? new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3) : nullptr; } void get_real_tri(che * mesh, std::vector & select_vertices, std::vector & triangle, std::vector & tri_sizes ) @@ -881,10 +881,10 @@ void get_real_tri(che * mesh, std::vector & select_vertices, std::vecto che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index) { size_t n_vertices = select_vertices.size() + 3; - size_t n_faces = select_vertices.size() + 4; + size_t n_trigs = select_vertices.size() + 4; vertex * vertices = new vertex[n_vertices]; - index_t * faces = new index_t[n_faces * che::mtrig]; + index_t * trigs = new index_t[n_trigs * che::mtrig]; std::vector triangle; std::vector tri_sizes(3,0); @@ -905,65 +905,65 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic i = 0; for( ; i< tri_sizes[0]-1; ++i) { - faces[f++] = i; - faces[f++] = tri_init; - faces[f++] = i + 1; + trigs[f++] = i; + trigs[f++] = tri_init; + trigs[f++] = i + 1; } ++i; for( ; i < tri_sizes[0] + tri_sizes[1] - 1; ++i) { - faces[f++] = i; - faces[f++] = tri_init + 1; - faces[f++] = i + 1; + trigs[f++] = i; + trigs[f++] = tri_init + 1; + trigs[f++] = i + 1; } ++i; for( ; i < select_vertices.size() - 1; ++i) { - faces[f++] = i; - faces[f++] = tri_init + 2; - faces[f++] = i + 1; + trigs[f++] = i; + trigs[f++] = tri_init + 2; + trigs[f++] = i + 1; } size_t aux_i = tri_sizes[0]; - faces[f++] = aux_i - 1; - faces[f++] = tri_init; - faces[f++] = tri_init + 1; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init; + trigs[f++] = tri_init + 1; - faces[f++] = aux_i - 1; - faces[f++] = tri_init + 1; - faces[f++] = aux_i; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init + 1; + trigs[f++] = aux_i; aux_i = tri_sizes[0] + tri_sizes[1]; - faces[f++] = aux_i - 1; - faces[f++] = tri_init + 1; - faces[f++] = tri_init + 2; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init + 1; + trigs[f++] = tri_init + 2; - faces[f++] = aux_i - 1; - faces[f++] = tri_init + 2; - faces[f++] = aux_i; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init + 2; + trigs[f++] = aux_i; aux_i = select_vertices.size(); - faces[f++] = aux_i - 1; - faces[f++] = tri_init + 2; - faces[f++] = tri_init; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init + 2; + trigs[f++] = tri_init; - faces[f++] = aux_i - 1; - faces[f++] = tri_init; - faces[f++] = 0; + trigs[f++] = aux_i - 1; + trigs[f++] = tri_init; + trigs[f++] = 0; - faces[f++] = tri_init + 2; - faces[f++] = tri_init + 1; - faces[f++] = tri_init; + trigs[f++] = tri_init + 2; + trigs[f++] = tri_init + 1; + trigs[f++] = tri_init; - che * new_off = new che(vertices, n_vertices, faces, n_faces); + che * new_off = new che(vertices, n_vertices, trigs, n_trigs); delete [] vertices; - delete [] faces; + delete [] trigs; return new_off; } diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 6ba53622..94b1f4dd 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -33,7 +33,7 @@ void che_obj::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "# OBJ generated by gproshan\n"); fprintf(fp, "# vertices %lu\n", mesh->n_vertices); - fprintf(fp, "# trigs %lu\n", mesh->n_faces); + fprintf(fp, "# trigs %lu\n", mesh->n_trigs); for(index_t i = 0; i < mesh->n_vertices; ++i) { diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 87c55505..4c9d21ab 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -47,8 +47,8 @@ void che_off::read_file(const std::string & file) } } - std::vector faces; - faces.reserve(che::mtrig * n_faces); + std::vector trigs; + trigs.reserve(che::mtrig * n_trigs); index_t P[32]; while(nf--) @@ -58,27 +58,27 @@ void che_off::read_file(const std::string & file) fscanf(fp, "%u", P + i); for(const index_t & v: trig_convex_polygon(P, n)) - faces.push_back(v); + trigs.push_back(v); } fclose(fp); - if(faces.size() != che::mtrig * n_faces) + if(trigs.size() != che::mtrig * n_trigs) { vertex * tGT = GT; GT = nullptr; vertex * tVN = VN; VN = nullptr; rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, faces.size() / che::mtrig); + alloc(nv, trigs.size() / che::mtrig); GT = tGT; VN = tVN; VC = tVC; } - memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); + memcpy(VT, trigs.data(), trigs.size() * sizeof(index_t)); } void che_off::write_file(const che * mesh, const std::string & file, const che_off::type & off, const bool & pointcloud) @@ -89,7 +89,7 @@ void che_off::write_file(const che * mesh, const std::string & file, const che_o assert(fp); fprintf(fp, "%s\n", str_off[off]); - fprintf(fp, "%lu %lu 0\n", mesh->n_vertices, pointcloud ? 0 : mesh->n_faces); + fprintf(fp, "%lu %lu 0\n", mesh->n_vertices, pointcloud ? 0 : mesh->n_trigs); for(size_t i = 0; i < mesh->n_vertices; ++i) { diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index f1ee3bd5..e7c94b6b 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -91,8 +91,8 @@ void che_ply::read_file(const std::string & file) alloc(nv, nf); - std::vector faces; - faces.reserve(che::mtrig * n_faces); + std::vector trigs; + trigs.reserve(che::mtrig * n_trigs); if(format[0] == 'a') // ascii { @@ -110,7 +110,7 @@ void che_ply::read_file(const std::string & file) fscanf(fp, "%u", P + i); for(const index_t & v: trig_convex_polygon(P, nv)) - faces.push_back(v); + trigs.push_back(v); } } else // binary_little_endian or binary_big_endian @@ -198,26 +198,26 @@ void che_ply::read_file(const std::string & file) } for(const index_t & v: trig_convex_polygon(P, nv)) - faces.push_back(v); + trigs.push_back(v); } } fclose(fp); - if(faces.size() != che::mtrig * n_faces) + if(trigs.size() != che::mtrig * n_trigs) { vertex * tGT = GT; GT = nullptr; rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, faces.size() / che::mtrig); + alloc(nv, trigs.size() / che::mtrig); GT = tGT; VC = tVC; } - memcpy(VT, faces.data(), faces.size() * sizeof(index_t)); + memcpy(VT, trigs.data(), trigs.size() * sizeof(index_t)); } void che_ply::write_file(const che * mesh, const std::string & file, const bool & color) @@ -240,7 +240,7 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "property uchar green\n"); fprintf(fp, "property uchar blue\n"); } - fprintf(fp, "element face %lu\n", mesh->n_faces); + fprintf(fp, "element face %lu\n", mesh->n_trigs); fprintf(fp, "property list uchar uint vertex_index\n"); fprintf(fp, "end_header\n"); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index c6c1bce7..ea4c3a78 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -118,7 +118,7 @@ void che_ptx::read_file(const std::string & file) } rw(n_half_edges) = he; - rw(n_faces) = he / che::mtrig; + rw(n_trigs) = he / che::mtrig; } void che_ptx::write_file(const che * mesh, const std::string & file, const size_t & n_rows, const size_t & n_cols) diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 362f376d..17fdfe32 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -15,7 +15,7 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) filename = "sphere"; std::vector vertices; - std::vector faces; + std::vector trigs; const real_t delta = M_PI / n; @@ -34,50 +34,50 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) { v = i * cols + j; - faces.push_back(v); - faces.push_back(v + 1); - faces.push_back(v + cols); + trigs.push_back(v); + trigs.push_back(v + 1); + trigs.push_back(v + cols); - faces.push_back(v + cols); - faces.push_back(v + 1); - faces.push_back(v + cols + 1); + trigs.push_back(v + cols); + trigs.push_back(v + 1); + trigs.push_back(v + cols + 1); } v = i * cols; - faces.push_back(vertices.size() - 2); - faces.push_back(v); - faces.push_back(v + cols); + trigs.push_back(vertices.size() - 2); + trigs.push_back(v); + trigs.push_back(v + cols); v = (i + 1) * cols - 1; - faces.push_back(vertices.size() - 1); - faces.push_back(v + cols); - faces.push_back(v); + trigs.push_back(vertices.size() - 1); + trigs.push_back(v + cols); + trigs.push_back(v); } for(index_t j = 0; j < cols - 1; ++j) { v = (2 * n - 1) * cols + j; - faces.push_back(v + 1); - faces.push_back(j); - faces.push_back(v); + trigs.push_back(v + 1); + trigs.push_back(j); + trigs.push_back(v); - faces.push_back(j + 1); - faces.push_back(j); - faces.push_back(v + 1); + trigs.push_back(j + 1); + trigs.push_back(j); + trigs.push_back(v + 1); } v = (2 * n - 1) * cols; - faces.push_back(vertices.size() - 2); - faces.push_back(v); - faces.push_back(0); + trigs.push_back(vertices.size() - 2); + trigs.push_back(v); + trigs.push_back(0); v = (2 * n) * cols - 1; - faces.push_back(vertices.size() - 1); - faces.push_back(cols - 1); - faces.push_back(v); + trigs.push_back(vertices.size() - 1); + trigs.push_back(cols - 1); + trigs.push_back(v); - init(vertices.data(), vertices.size(), faces.data(), faces.size() / 3); + init(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); } diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 95d44238..e7d7d529 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -90,7 +90,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) const CHE * mesh = g_meshes[r.hit.geomID]; - if(!mesh->n_faces) return r.hit.primID; + if(!mesh->n_trigs) return r.hit.primID; index_t he = che::mtrig * r.hit.primID; float w = 1 - r.hit.u - r.hit.v; @@ -129,10 +129,10 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_faces || pointcloud) - g_meshes[i]->n_faces = 0; + if(!meshes[i]->n_trigs || pointcloud) + g_meshes[i]->n_trigs = 0; - const index_t & geomID = g_meshes[i]->n_faces || meshes[i]->is_scene() ? + const index_t & geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? add_mesh(meshes[i], model_mats[i]) : add_pointcloud(meshes[i], model_mats[i]); @@ -177,7 +177,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(index_t), - mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_faces + mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_trigs ); @@ -200,7 +200,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) if(mesh->is_scene()) { g_meshes[geom_id]->VT = tri_idxs; - g_meshes[geom_id]->n_faces = mesh->n_vertices / 3; + g_meshes[geom_id]->n_trigs = mesh->n_vertices / 3; g_meshes[geom_id]->n_half_edges = mesh->n_vertices; } diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index cbf054c3..c0fbc929 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -415,7 +415,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u if(mesh->is_scene()) { h_m.n_half_edges = mesh->n_vertices; - h_m.n_faces = mesh->n_vertices / 3; + h_m.n_trigs = mesh->n_vertices / 3; h_m.VT = new index_t[mesh->n_vertices]; #pragma omp parallel for @@ -443,7 +443,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = h_m.n_faces; + optix_mesh.triangleArray.numIndexTriplets = h_m.n_trigs; optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 2815cb89..6b1e9c39 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -254,7 +254,7 @@ void che_viewer::log_info() gproshan_log_var(mesh->filename); gproshan_log_var(mesh->n_vertices); - gproshan_log_var(mesh->n_faces); + gproshan_log_var(mesh->n_trigs); gproshan_log_var(mesh->n_half_edges); gproshan_log_var(mesh->n_edges); gproshan_log_var(mesh->area_surface()); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index c032ba97..10370be3 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -195,7 +195,7 @@ void viewer::imgui() { ImGui::Text("%13lu fps", size_t(1.0 / render_time)); ImGui::Text("%13lu vertices", mesh->n_vertices); - ImGui::Text("%13lu faces", mesh->n_faces); + ImGui::Text("%13lu trigs", mesh->n_trigs); ImGui::Indent(); if(ImGui::Combo("fit screen", (int *) &mesh.opt_fit_screen, "none\0box (2x2x2)\0sphere (97.72%)\0\0")) From 2d3aba7492bc4903316534f3348a71339fa4c82d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Nov 2022 23:50:15 +0100 Subject: [PATCH 0760/1018] scene: added objects list and materials, added struct material.glsl` --- include/gproshan/scenes/scene.h | 6 ++++-- shaders/material.glsl | 13 +++++++++++++ shaders/shading.glsl | 4 ++++ src/gproshan/scenes/scene.cpp | 19 +++++-------------- src/gproshan/viewer/scene_viewer.cpp | 15 ++++++++++++++- 5 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 shaders/material.glsl diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index f730b404..0ae1b575 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -29,6 +29,7 @@ class scene: public che vec3 Ks = {1, 1, 1}; real_t d = 1; // Tr = 0, opposite real_t Ns = 0; + real_t Ni = 0; index_t illum = 1; index_t map_Ka = NIL; index_t map_Kd = NIL; @@ -37,8 +38,7 @@ class scene: public che struct object { index_t begin = 0; - index_t end = 0; - index_t idm = NIL; + index_t material_id = NIL; }; public: @@ -49,6 +49,8 @@ class scene: public che std::vector texture_name; std::vector textures; + std::vector objects; + vec2 * texcoords = nullptr; public: diff --git a/shaders/material.glsl b/shaders/material.glsl new file mode 100644 index 00000000..2968acdf --- /dev/null +++ b/shaders/material.glsl @@ -0,0 +1,13 @@ +struct material +{ + vec3 Ka; + vec3 Kd; + vec3 Ks; + float d; + float Ns; + float Ni; + uint illum; + uint map_Ka; + uint map_Kd; +}; + diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 3f3f2f10..51612f37 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -1,8 +1,11 @@ #include colormap.glsl +#include material.glsl uniform bool render_lines; uniform uint idx_colormap; +uniform material mat; + float diffuse(vec3 N, vec3 L) { @@ -28,6 +31,7 @@ float fresnel(vec3 N, vec3 E) vec3 lines_colormap(vec3 color, float h) { color = idx_colormap > 0 ? colormap(idx_colormap, h) : color; + color = mat.Kd; if(render_lines) { diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 6caff3ce..c52b57c6 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -68,19 +68,10 @@ bool scene::load_obj(const std::string & file) VN[i] = n != NIL ? p.vnormals[n] : normalize((GT[trig + 1] - GT[trig]) * (GT[trig + 2] - GT[trig])); } - if(p.objects.size()) - { - #pragma omp parallel for - for(index_t i = 0; i < p.objects.size() - 1; ++i) - { - const auto & obj = p.objects[i]; - const material & m = materials[material_id[obj.first]]; + for(auto & obj: p.objects) + objects.push_back({obj.second, material_id[obj.first]}); - const index_t & end = p.objects[i + 1].second; - for(index_t j = obj.second; j < end; ++j) - VC[j] = m.Kd; - } - } + gproshan_log_var(objects.size()); return true; } @@ -131,8 +122,8 @@ bool scene::load_mtl(const std::string & file) } case 'N': // Ns { - real_t & d = materials.back().Ns; - sscanf(line, "%*s %f", &d); + real_t & N = str[1] == 's' ? materials.back().Ns : materials.back().Ni; + sscanf(line, "%*s %f", &N); break; } case 'i': // illum diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index bc45e054..1f5b0261 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -49,7 +49,20 @@ void scene_viewer::draw(shader & program) program.enable(); glBindVertexArray(vao); - glDrawArrays(GL_TRIANGLES, 0, mesh->n_vertices); + if(sc->objects.size() == 1) + { + glDrawArrays(GL_TRIANGLES, 0, mesh->n_vertices); + } + else + { + for(index_t i = 0; i < sc->objects.size() - 1; ++i) + { + const scene::object & obj = sc->objects[i]; + const scene::material & mat = sc->materials[obj.material_id]; + glProgramUniform3f(program, program("mat.Kd"), mat.Kd.x(), mat.Kd.y(), mat.Kd.z()); + glDrawArrays(GL_TRIANGLES, obj.begin, sc->objects[i + 1].begin - obj.begin); + } + } glBindVertexArray(0); program.disable(); From d7c2a46a41891aff34a6093ed5464135e7a7c9a3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 24 Nov 2022 03:51:26 +0100 Subject: [PATCH 0761/1018] viewer: rendering textures --- include/gproshan/viewer/scene_viewer.h | 3 ++ shaders/fragment.glsl | 8 ++++ shaders/geometry.glsl | 5 ++ shaders/shading.glsl | 4 -- shaders/vertex.glsl | 3 ++ src/gproshan/scenes/scene.cpp | 10 ++-- src/gproshan/viewer/scene_viewer.cpp | 64 ++++++++++++++++++++++++-- 7 files changed, 85 insertions(+), 12 deletions(-) diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index c7851562..28b939c0 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -14,12 +14,15 @@ class scene_viewer: public che_viewer private: scene * sc = nullptr; GLuint * gltextures = nullptr; + GLuint tex_vbo; public: scene_viewer(scene * p_sc); ~scene_viewer(); void init_texture(const GLuint & gltex, const scene::texture & tex); void draw(shader & program); + void gl_uniform_material(shader & program, const scene::material & mat); + void update_vbo_texcoords(); }; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 9d4e91b5..79f4a55a 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,11 +1,13 @@ #version 410 core #include shading.glsl +#include material.glsl in vec3 gs_position; in vec3 gs_normal; in vec3 gs_mesh_color; in float gs_color; +in vec2 gs_texcoord; noperspective in vec3 edge_dist; @@ -17,10 +19,16 @@ uniform vec3 cam_light; uniform bool render_flat; uniform bool render_wireframe; +uniform sampler2D tex_Ka; +uniform sampler2D tex_Kd; + +uniform material mat; void main() { vec3 color = lines_colormap(gs_mesh_color, gs_color); + color = mat.map_Kd != -1 ? texture(tex_Kd, gs_texcoord.xy).rgb : mat.Kd; + vec3 N = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); if(render_wireframe) diff --git a/shaders/geometry.glsl b/shaders/geometry.glsl index 3bff1831..c9e5c4d8 100644 --- a/shaders/geometry.glsl +++ b/shaders/geometry.glsl @@ -7,11 +7,13 @@ in vec3 vs_position[]; in vec3 vs_normal[]; in vec3 vs_mesh_color[]; in float vs_color[]; +in vec2 vs_texcoord[]; out vec3 gs_position; out vec3 gs_normal; out vec3 gs_mesh_color; out float gs_color; +out vec2 gs_texcoord; noperspective out vec3 edge_dist; @@ -33,6 +35,7 @@ void main() gs_normal = vs_normal[0]; gs_mesh_color = vs_mesh_color[0]; gs_color = vs_color[0]; + gs_texcoord = vs_texcoord[0]; edge_dist = vec3(ha, 0, 0); gl_Position = gl_in[0].gl_Position; EmitVertex(); @@ -41,6 +44,7 @@ void main() gs_normal = vs_normal[1]; gs_mesh_color = vs_mesh_color[1]; gs_color = vs_color[1]; + gs_texcoord = vs_texcoord[1]; edge_dist = vec3(0, hb, 0); gl_Position = gl_in[1].gl_Position; EmitVertex(); @@ -49,6 +53,7 @@ void main() gs_normal = vs_normal[2]; gs_mesh_color = vs_mesh_color[2]; gs_color = vs_color[2]; + gs_texcoord = vs_texcoord[2]; edge_dist = vec3(0, 0, hc); gl_Position = gl_in[2].gl_Position; EmitVertex(); diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 51612f37..3f3f2f10 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -1,11 +1,8 @@ #include colormap.glsl -#include material.glsl uniform bool render_lines; uniform uint idx_colormap; -uniform material mat; - float diffuse(vec3 N, vec3 L) { @@ -31,7 +28,6 @@ float fresnel(vec3 N, vec3 E) vec3 lines_colormap(vec3 color, float h) { color = idx_colormap > 0 ? colormap(idx_colormap, h) : color; - color = mat.Kd; if(render_lines) { diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 6269b7a9..c89e24ea 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -4,11 +4,13 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; layout (location=2) in vec3 in_mesh_color; layout (location=3) in float in_color; +layout (location=4) in vec2 in_texcoord; out vec3 vs_position; out vec3 vs_normal; out vec3 vs_mesh_color; out float vs_color; +out vec2 vs_texcoord; uniform mat4 proj_view_mat; uniform mat4 model_mat; @@ -19,6 +21,7 @@ void main() vs_normal = in_normal; vs_mesh_color = in_mesh_color; vs_color = in_color; + vs_texcoord = in_texcoord; gl_Position = proj_view_mat * vec4(vs_position, 1); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index c52b57c6..d6a91883 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -48,7 +48,8 @@ bool scene::load_obj(const std::string & file) return false; alloc(p.trigs.size(), 0); - texcoords = new vec2[n_vertices]; + if(p.vtexcoords.size()) + texcoords = new vec2[n_vertices]; #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) @@ -139,7 +140,7 @@ bool scene::load_mtl(const std::string & file) if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) { - texture_id[str] = textures.size(); + texture_id[str] = texture_name.size(); texture_name.push_back(str); } m = texture_id[str]; @@ -182,14 +183,15 @@ bool scene::load_mtl(const std::string & file) bool scene::load_texture(const std::string & file) { - CImg img(file.c_str()); + CImg img(file.c_str()); textures.emplace_back(); texture & tex = textures.back(); tex.rows = img.height(); tex.cols = img.width(); tex.data = new vec3[tex.rows * tex.cols]; - memcpy((float *) tex.data, img.data(), sizeof(vec3) * tex.rows * tex.cols); + img.permute_axes("cxyz"); + memcpy((unsigned char *) tex.data, img.data(), tex.rows * tex.cols); return true; } diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 1f5b0261..2820f7dc 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -15,10 +15,14 @@ scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) gproshan_log_var(sc->texture_name[i]); init_texture(gltextures[i], sc->textures[i]); } + + glGenBuffers(1, &tex_vbo); + update_vbo_texcoords(); } scene_viewer::~scene_viewer() { + glDeleteBuffers(1, &tex_vbo); glDeleteTextures(sc->textures.size(), gltextures); delete [] gltextures; } @@ -27,10 +31,13 @@ void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex { glBindTexture(GL_TEXTURE_2D, gltex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.cols, tex.rows, 0, GL_RGB, GL_FLOAT, tex.data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.rows, tex.cols, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); + //che::rgb_t colores = {255, 255, 0}; + //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &colores); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); @@ -59,8 +66,14 @@ void scene_viewer::draw(shader & program) { const scene::object & obj = sc->objects[i]; const scene::material & mat = sc->materials[obj.material_id]; - glProgramUniform3f(program, program("mat.Kd"), mat.Kd.x(), mat.Kd.y(), mat.Kd.z()); + + gl_uniform_material(program, mat); glDrawArrays(GL_TRIANGLES, obj.begin, sc->objects[i + 1].begin - obj.begin); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, 0); } } glBindVertexArray(0); @@ -68,6 +81,49 @@ void scene_viewer::draw(shader & program) program.disable(); } +void scene_viewer::gl_uniform_material(shader & program, const scene::material & mat) +{ + glProgramUniform3f(program, program("mat.Ka"), mat.Ka.x(), mat.Ka.y(), mat.Ka.z()); + glProgramUniform3f(program, program("mat.Kd"), mat.Kd.x(), mat.Kd.y(), mat.Kd.z()); + glProgramUniform3f(program, program("mat.Ks"), mat.Ks.x(), mat.Ks.y(), mat.Ks.z()); + glProgramUniform1f(program, program("mat.d"), mat.d); + glProgramUniform1f(program, program("mat.Ns"), mat.Ns); + glProgramUniform1f(program, program("mat.Ni"), mat.Ni); + glProgramUniform1ui(program, program("mat.illum"), mat.illum); + glProgramUniform1ui(program, program("mat.map_Ka"), mat.map_Ka); + glProgramUniform1ui(program, program("mat.map_Kd"), mat.map_Kd); + + if(mat.map_Ka != NIL) + { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Ka]); + glProgramUniform1ui(program, program("tex_Ka"), 0); + } + + if(mat.map_Kd != NIL) + { + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Kd]); + glProgramUniform1ui(program, program("tex_Kd"), 1); + } +} + +void scene_viewer::update_vbo_texcoords() +{ + if(!sc->texcoords) return; + + glBindVertexArray(vao); + + // 6 TEXTURE COORDS + glBindBuffer(GL_ARRAY_BUFFER, tex_vbo); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vec2), sc->texcoords, GL_STATIC_DRAW); + glEnableVertexAttribArray(4); + glVertexAttribPointer(4, 2, GL_REAL, GL_FALSE, 0, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glBindVertexArray(0); +} + } // namespace gproshan From cd75e0e6b72f219eb37b2b37a6a0c3f7d2d4eb95 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Nov 2022 02:47:30 +0100 Subject: [PATCH 0762/1018] viewer: rendering with textures review rgb_t --- include/gproshan/mesh/che.h | 1 + include/gproshan/scenes/scene.h | 9 +++---- shaders/fragment.glsl | 8 ++++++- shaders/material.glsl | 7 +++--- src/gproshan/mesh/che.cpp | 2 ++ src/gproshan/scenes/scanner.cpp | 2 +- src/gproshan/scenes/scene.cpp | 36 ++++++++++++++++++++-------- src/gproshan/viewer/scene_viewer.cpp | 23 +++++++++--------- 8 files changed, 57 insertions(+), 31 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 36980e55..aeba4a8e 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -35,6 +35,7 @@ class che rgb_t() = default; rgb_t(const vertex & v); rgb_t(const float & fr, const float & fg, const float & fb); + rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb); unsigned char & operator [] (const index_t & i); operator vertex () const; }; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 0ae1b575..67eb1374 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -17,7 +17,7 @@ class scene: public che public: struct texture { - vec3 * data = nullptr; + rgb_t * data = nullptr; size_t rows = 0; size_t cols = 0; }; @@ -30,9 +30,10 @@ class scene: public che real_t d = 1; // Tr = 0, opposite real_t Ns = 0; real_t Ni = 0; - index_t illum = 1; - index_t map_Ka = NIL; - index_t map_Kd = NIL; + int illum = 1; + int map_Ka = -1; + int map_Kd = -1; + int map_Ks = -1; }; struct object diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 79f4a55a..a096da07 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -27,7 +27,13 @@ uniform material mat; void main() { vec3 color = lines_colormap(gs_mesh_color, gs_color); - color = mat.map_Kd != -1 ? texture(tex_Kd, gs_texcoord.xy).rgb : mat.Kd; + vec3 Ka = mat.Ka; + if(mat.map_Ka != -1) + Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; + vec3 Kd = mat.Kd; + if(mat.map_Kd != -1) + Kd *= texture(tex_Kd, gs_texcoord.xy).rgb; + color = Kd; vec3 N = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); diff --git a/shaders/material.glsl b/shaders/material.glsl index 2968acdf..780a5542 100644 --- a/shaders/material.glsl +++ b/shaders/material.glsl @@ -6,8 +6,9 @@ struct material float d; float Ns; float Ni; - uint illum; - uint map_Ka; - uint map_Kd; + int illum; + int map_Ka; + int map_Kd; + int map_Ks; }; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 79a35599..58c0a6b1 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -25,6 +25,8 @@ che::rgb_t::rgb_t(const float & fr, const float & fg, const float & fb) b = (unsigned char) (fb * 255); } +che::rgb_t::rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb): r(cr), g(cg), b(cb) {} + unsigned char & che::rgb_t::operator [] (const index_t & i) { return (&r)[i]; diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index a7dee440..11ac163b 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -39,7 +39,7 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c } else { - mesh_ptx->rgb(v) = {0, 0, 0}; + mesh_ptx->rgb(v) = vertex{0, 0, 0}; } } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index d6a91883..f46883f4 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -48,17 +48,25 @@ bool scene::load_obj(const std::string & file) return false; alloc(p.trigs.size(), 0); - if(p.vtexcoords.size()) - texcoords = new vec2[n_vertices]; #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) { const index_t & v = p.trigs[i].x(); - const index_t & t = p.trigs[i].y(); GT[i] = p.vertices[v]; VC[i] = p.vcolors[v]; - texcoords[i] = t != NIL ? p.vtexcoords[t] : vec2{-1, -1}; + } + + if(p.vtexcoords.size()) + { + texcoords = new vec2[n_vertices]; + + #pragma omp parallel for + for(index_t i = 0; i < n_vertices; ++i) + { + const index_t & t = p.trigs[i].y(); + texcoords[i] = t != NIL ? p.vtexcoords[t] : vec2{-1, -1}; + } } #pragma omp parallel for @@ -129,13 +137,16 @@ bool scene::load_mtl(const std::string & file) } case 'i': // illum { - index_t & illum = materials.back().illum; + int & illum = materials.back().illum; sscanf(line, "%*s %u", &illum); break; } case 'm': // map_Ka, map_kd { - index_t & m = str[5] == 'a' ? materials.back().map_Ka : materials.back().map_Kd; + if(str[4] != 'K') break; + int & m = str[5] == 'a' ? materials.back().map_Ka + : str[5] == 'd' ? materials.back().map_Kd + : materials.back().map_Ks; sscanf(line, "%*s %s", str); if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) @@ -171,8 +182,8 @@ bool scene::load_mtl(const std::string & file) gproshan_log_var(m.d); gproshan_log_var(m.Ns); gproshan_log_var(m.illum); - if(m.map_Ka != NIL) gproshan_log_var(texture_name[m.map_Ka]); - if(m.map_Kd != NIL) gproshan_log_var(texture_name[m.map_Kd]); + if(m.map_Ka > -1) gproshan_log_var(texture_name[m.map_Ka]); + if(m.map_Kd > -1) gproshan_log_var(texture_name[m.map_Kd]); } gproshan_log_var(materials.size()); @@ -184,14 +195,19 @@ bool scene::load_mtl(const std::string & file) bool scene::load_texture(const std::string & file) { CImg img(file.c_str()); + img.mirror('y'); textures.emplace_back(); texture & tex = textures.back(); tex.rows = img.height(); tex.cols = img.width(); - tex.data = new vec3[tex.rows * tex.cols]; + tex.data = new rgb_t[tex.rows * tex.cols]; + gproshan_error_var(img.width()); + gproshan_error_var(img.height()); + gproshan_error_var(img.depth()); + gproshan_error_var(img.spectrum()); img.permute_axes("cxyz"); - memcpy((unsigned char *) tex.data, img.data(), tex.rows * tex.cols); + memcpy((unsigned char *) tex.data, img.data(), sizeof(rgb_t) * tex.rows * tex.cols); return true; } diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 2820f7dc..a8c47203 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -34,10 +34,8 @@ void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.rows, tex.cols, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); - //che::rgb_t colores = {255, 255, 0}; - //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, &colores); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.cols, tex.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); @@ -89,29 +87,30 @@ void scene_viewer::gl_uniform_material(shader & program, const scene::material & glProgramUniform1f(program, program("mat.d"), mat.d); glProgramUniform1f(program, program("mat.Ns"), mat.Ns); glProgramUniform1f(program, program("mat.Ni"), mat.Ni); - glProgramUniform1ui(program, program("mat.illum"), mat.illum); - glProgramUniform1ui(program, program("mat.map_Ka"), mat.map_Ka); - glProgramUniform1ui(program, program("mat.map_Kd"), mat.map_Kd); + glProgramUniform1i(program, program("mat.illum"), mat.illum); + glProgramUniform1i(program, program("mat.map_Ka"), mat.map_Ka); + glProgramUniform1i(program, program("mat.map_Kd"), mat.map_Kd); + glProgramUniform1i(program, program("mat.map_Ks"), mat.map_Ks); - if(mat.map_Ka != NIL) + if(mat.map_Ka > -1) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Ka]); - glProgramUniform1ui(program, program("tex_Ka"), 0); + glProgramUniform1i(program, program("tex_Ka"), 0); } - if(mat.map_Kd != NIL) + if(mat.map_Kd > -1) { glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Kd]); - glProgramUniform1ui(program, program("tex_Kd"), 1); + glProgramUniform1i(program, program("tex_Kd"), 1); } } void scene_viewer::update_vbo_texcoords() { if(!sc->texcoords) return; - + gproshan_error(texcoords); glBindVertexArray(vao); // 6 TEXTURE COORDS From 6bfbccb5fa6b4685fd7b4708fb36fc67512bc6c9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Nov 2022 13:18:45 +0100 Subject: [PATCH 0763/1018] scene_viewer: add specular texture --- shaders/fragment.glsl | 1 + src/gproshan/scenes/scene.cpp | 5 +---- src/gproshan/viewer/scene_viewer.cpp | 9 +++++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index a096da07..064e0d3e 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -21,6 +21,7 @@ uniform bool render_wireframe; uniform sampler2D tex_Ka; uniform sampler2D tex_Kd; +uniform sampler2D tex_Ks; uniform material mat; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index f46883f4..81cf2734 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -184,6 +184,7 @@ bool scene::load_mtl(const std::string & file) gproshan_log_var(m.illum); if(m.map_Ka > -1) gproshan_log_var(texture_name[m.map_Ka]); if(m.map_Kd > -1) gproshan_log_var(texture_name[m.map_Kd]); + if(m.map_Ks > -1) gproshan_log_var(texture_name[m.map_Ks]); } gproshan_log_var(materials.size()); @@ -202,10 +203,6 @@ bool scene::load_texture(const std::string & file) tex.rows = img.height(); tex.cols = img.width(); tex.data = new rgb_t[tex.rows * tex.cols]; - gproshan_error_var(img.width()); - gproshan_error_var(img.height()); - gproshan_error_var(img.depth()); - gproshan_error_var(img.spectrum()); img.permute_axes("cxyz"); memcpy((unsigned char *) tex.data, img.data(), sizeof(rgb_t) * tex.rows * tex.cols); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index a8c47203..d359e670 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -72,6 +72,8 @@ void scene_viewer::draw(shader & program) glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, 0); } } glBindVertexArray(0); @@ -105,6 +107,13 @@ void scene_viewer::gl_uniform_material(shader & program, const scene::material & glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Kd]); glProgramUniform1i(program, program("tex_Kd"), 1); } + + if(mat.map_Ks > -1) + { + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Ks]); + glProgramUniform1i(program, program("tex_Ks"), 2); + } } void scene_viewer::update_vbo_texcoords() From 0fbd93d77c03d305f68c068a81132182feb4e970 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Nov 2022 13:34:06 +0100 Subject: [PATCH 0764/1018] checked unsigned int, using index_t --- include/gproshan/include.h | 6 +++--- include/gproshan/raytracing/rt_utils.h | 4 ++-- src/gproshan/app_viewer.cpp | 5 ++--- src/gproshan/raytracing/rt_optix.cpp | 2 +- src/gproshan/raytracing/rt_optix.cu | 4 ++-- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 5aef5573..7c0837f5 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -17,12 +17,12 @@ namespace gproshan { -typedef unsigned int index_t; +using index_t = unsigned int; #ifdef GPROSHAN_FLOAT - typedef float real_t; + using real_t = float; #else - typedef double real_t; + using real_t = double; #endif diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 09085340..1b88ebd7 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -40,7 +40,7 @@ struct random template struct t_eval_hit { - unsigned int primID = NIL; + index_t primID = NIL; T dist = 0; T u = 0, v = 0; vec position; @@ -51,7 +51,7 @@ struct t_eval_hit t_eval_hit() {} __host__ __device__ - t_eval_hit(const CHE & mesh, const unsigned int & aprimID, const T & au, const T & av) + t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av) { primID = aprimID; u = au; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 6710aec5..fecd0bde 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -691,9 +691,8 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static unsigned int n_eigs = 20; - - ImGui::InputInt("eigenvectors", (int *) &n_eigs); + static size_t n_eigs = 20; + ImGui::InputScalar("n_eigs", ImGuiDataType_U64, &n_eigs); if(ImGui::Button("Run")) { diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index c0fbc929..43ddc744 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -35,7 +35,7 @@ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) HitgroupRecord }; -void optix_log(unsigned int level, const char * tag, const char * message, void *) +void optix_log(index_t level, const char * tag, const char * message, void *) { fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index cb763e8d..846f84f2 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -44,7 +44,7 @@ extern "C" __global__ void __closesthit__radiance() float2 bar = optixGetTriangleBarycentrics(); OptixTraversableHandle gas = optixGetGASTraversableHandle(); - const unsigned int sbtID = optixGetSbtGASIndex(); + const index_t sbtID = optixGetSbtGASIndex(); const float time = optixGetRayTime(); vertex data[3]; @@ -61,7 +61,7 @@ extern "C" __global__ void __closesthit__radiance() vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { - unsigned int occluded = 1; + uint32_t occluded = 1; optixTrace( optix_params.traversable, * (float3 *) &position, * (float3 *) &wi, From 8aff3e5335cc61947db12453e1bd5ac279f56fed Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Nov 2022 15:30:09 +0100 Subject: [PATCH 0765/1018] shader: fragment displaying only diffuse color/texture --- shaders/fragment.glsl | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 064e0d3e..1be26b7f 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -28,13 +28,19 @@ uniform material mat; void main() { vec3 color = lines_colormap(gs_mesh_color, gs_color); + vec3 Ka = mat.Ka; if(mat.map_Ka != -1) Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; + vec3 Kd = mat.Kd; if(mat.map_Kd != -1) - Kd *= texture(tex_Kd, gs_texcoord.xy).rgb; - color = Kd; + Kd = texture(tex_Kd, gs_texcoord.xy).rgb; + + vec3 Ks = mat.Ks; + if(mat.map_Ks != -1) + Ks *= texture(tex_Ks, gs_texcoord.xy).rgb; + vec3 N = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); @@ -46,7 +52,8 @@ void main() color = mix(vec3(.2), color, d); } - color = shading(N, normalize(cam_light - gs_position), normalize(eye - gs_position), color); + color = shading(N, normalize(cam_light - gs_position), normalize(eye - gs_position), Kd); + //color = Ka + Kd + Ks; frag_color = vec4(color, 1); } From 4b75eb2dee347bf9cbda3575193e81c294324ca9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 25 Nov 2022 22:56:32 +0100 Subject: [PATCH 0766/1018] shaders: Blinn - Phong shading model --- include/gproshan/scenes/scene.h | 1 + shaders/fragment.glsl | 21 ++++++++++++++++----- src/gproshan/app_viewer.cpp | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 67eb1374..f1355697 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -34,6 +34,7 @@ class scene: public che int map_Ka = -1; int map_Kd = -1; int map_Ks = -1; + int map_d = -1; }; struct object diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 1be26b7f..cf81db7b 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -32,17 +32,17 @@ void main() vec3 Ka = mat.Ka; if(mat.map_Ka != -1) Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; - + vec3 Kd = mat.Kd; if(mat.map_Kd != -1) - Kd = texture(tex_Kd, gs_texcoord.xy).rgb; - + Kd *= texture(tex_Kd, gs_texcoord.xy).rgb; + vec3 Ks = mat.Ks; if(mat.map_Ks != -1) Ks *= texture(tex_Ks, gs_texcoord.xy).rgb; - vec3 N = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); + vec3 n = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); if(render_wireframe) { @@ -52,7 +52,18 @@ void main() color = mix(vec3(.2), color, d); } - color = shading(N, normalize(cam_light - gs_position), normalize(eye - gs_position), Kd); + vec3 l = cam_light - gs_position; + float r = length(l); + l /= r; + vec3 v = normalize(eye - gs_position); + vec3 h = normalize(l + v); + float lambertian = max(dot(l, n), 0.0); + float specular = pow(max(dot(h, n), 0.0), mat.Ns); + float P = 4; + vec3 La = vec3(0.2, 0.2, 0.2); + + color = Ka * La + (lambertian * Kd + specular * Ks) * P / (r * r); + //color = shading(n, normalize(cam_light - gs_position), normalize(eye - gs_position), Kd); //color = Ka + Kd + Ks; frag_color = vec4(color, 1); } diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index fecd0bde..f0b93609 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -704,7 +704,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); TOC(view->time) - sprintf(view->status_message, "computing %u eigs in %.3fs", n_eigs, view->time); + sprintf(view->status_message, "computing %lu eigs in %.3fs", n_eigs, view->time); for(index_t k = 0; k < n_eigs; ++k) { From da75bd74b9d44cfd8bf66bb947778172b457e815 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 26 Nov 2022 01:00:38 +0100 Subject: [PATCH 0767/1018] scene_viewer: loading more material textures --- include/gproshan/scenes/scene.h | 8 +++-- shaders/fragment.glsl | 2 +- shaders/material.glsl | 2 ++ src/gproshan/scenes/scene.cpp | 20 ++++++----- src/gproshan/viewer/scene_viewer.cpp | 53 +++++++++++++--------------- 5 files changed, 44 insertions(+), 41 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index f1355697..e90f7314 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -17,9 +17,10 @@ class scene: public che public: struct texture { - rgb_t * data = nullptr; - size_t rows = 0; - size_t cols = 0; + unsigned char * data = nullptr; + size_t width = 0; + size_t height = 0; + size_t spectrum = 0; }; struct material @@ -35,6 +36,7 @@ class scene: public che int map_Kd = -1; int map_Ks = -1; int map_d = -1; + int map_bump = -1; }; struct object diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index cf81db7b..994ea880 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -29,7 +29,7 @@ void main() { vec3 color = lines_colormap(gs_mesh_color, gs_color); - vec3 Ka = mat.Ka; + vec3 Ka = mat.Ka * color; if(mat.map_Ka != -1) Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; diff --git a/shaders/material.glsl b/shaders/material.glsl index 780a5542..9227671a 100644 --- a/shaders/material.glsl +++ b/shaders/material.glsl @@ -10,5 +10,7 @@ struct material int map_Ka; int map_Kd; int map_Ks; + int map_d; + int map_bump; }; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 81cf2734..33595ede 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -143,10 +143,11 @@ bool scene::load_mtl(const std::string & file) } case 'm': // map_Ka, map_kd { - if(str[4] != 'K') break; - int & m = str[5] == 'a' ? materials.back().map_Ka - : str[5] == 'd' ? materials.back().map_Kd - : materials.back().map_Ks; + int & m = str[4] == 'K' && str[5] == 'a' ? materials.back().map_Ka + : str[4] == 'K' && str[5] == 'd' ? materials.back().map_Kd + : str[4] == 'K' && str[5] == 's' ? materials.back().map_Ks + : str[4] == 'd' ? materials.back().map_d + : materials.back().map_bump; sscanf(line, "%*s %s", str); if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) @@ -185,6 +186,8 @@ bool scene::load_mtl(const std::string & file) if(m.map_Ka > -1) gproshan_log_var(texture_name[m.map_Ka]); if(m.map_Kd > -1) gproshan_log_var(texture_name[m.map_Kd]); if(m.map_Ks > -1) gproshan_log_var(texture_name[m.map_Ks]); + if(m.map_d > -1) gproshan_log_var(texture_name[m.map_d]); + if(m.map_bump > -1) gproshan_log_var(texture_name[m.map_bump]); } gproshan_log_var(materials.size()); @@ -200,11 +203,12 @@ bool scene::load_texture(const std::string & file) textures.emplace_back(); texture & tex = textures.back(); - tex.rows = img.height(); - tex.cols = img.width(); - tex.data = new rgb_t[tex.rows * tex.cols]; + tex.width = img.width(); + tex.height = img.height(); + tex.spectrum = img.spectrum(); + tex.data = new unsigned char[tex.width * tex.height * tex.spectrum]; img.permute_axes("cxyz"); - memcpy((unsigned char *) tex.data, img.data(), sizeof(rgb_t) * tex.rows * tex.cols); + memcpy(tex.data, img.data(), tex.width * tex.height * tex.spectrum); return true; } diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index d359e670..62cca29a 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -5,16 +5,15 @@ namespace gproshan { +static const int gltex_nums[] = {GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2, GL_TEXTURE3, GL_TEXTURE4}; + scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) { gltextures = new GLuint[sc->textures.size()]; glGenTextures(sc->textures.size(), gltextures); for(index_t i = 0; i < sc->textures.size(); ++i) - { - gproshan_log_var(sc->texture_name[i]); init_texture(gltextures[i], sc->textures[i]); - } glGenBuffers(1, &tex_vbo); update_vbo_texcoords(); @@ -35,7 +34,9 @@ void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.cols, tex.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data); + tex.spectrum == 3 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data) + : tex.spectrum == 4 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data) + : glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, tex.width, tex.height, 0, GL_RED, GL_UNSIGNED_BYTE, tex.data); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); @@ -68,12 +69,11 @@ void scene_viewer::draw(shader & program) gl_uniform_material(program, mat); glDrawArrays(GL_TRIANGLES, obj.begin, sc->objects[i + 1].begin - obj.begin); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, 0); + for(auto & gltex_num: gltex_nums) + { + glActiveTexture(gltex_num); + glBindTexture(GL_TEXTURE_2D, 0); + } } } glBindVertexArray(0); @@ -93,27 +93,22 @@ void scene_viewer::gl_uniform_material(shader & program, const scene::material & glProgramUniform1i(program, program("mat.map_Ka"), mat.map_Ka); glProgramUniform1i(program, program("mat.map_Kd"), mat.map_Kd); glProgramUniform1i(program, program("mat.map_Ks"), mat.map_Ks); + glProgramUniform1i(program, program("mat.map_d"), mat.map_d); + glProgramUniform1i(program, program("mat.map_bump"), mat.map_bump); - if(mat.map_Ka > -1) - { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Ka]); - glProgramUniform1i(program, program("tex_Ka"), 0); - } - - if(mat.map_Kd > -1) + static auto bind_texture = [&](const int & i, const int & map, const char * tex) { - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Kd]); - glProgramUniform1i(program, program("tex_Kd"), 1); - } - - if(mat.map_Ks > -1) - { - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, gltextures[mat.map_Ks]); - glProgramUniform1i(program, program("tex_Ks"), 2); - } + if(map < 0) return; + glActiveTexture(gltex_nums[i]); + glBindTexture(GL_TEXTURE_2D, gltextures[map]); + glProgramUniform1i(program, program(tex), i); + }; + + bind_texture(0, mat.map_Ka, "tex_Ka"); + bind_texture(1, mat.map_Kd, "tex_Kd"); + bind_texture(2, mat.map_Ks, "tex_Ks"); + bind_texture(3, mat.map_d, "tex_d"); + bind_texture(4, mat.map_bump, "tex_bump"); } void scene_viewer::update_vbo_texcoords() From 42a459a8710b91e6228dc0eb8cbc3739b0519d75 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Nov 2022 11:29:15 +0100 Subject: [PATCH 0768/1018] viewer: adding option for rendering material --- include/gproshan/scenes/scene.h | 4 ++-- include/gproshan/viewer/viewer.h | 2 ++ shaders/fragment.glsl | 4 ++-- src/gproshan/app_viewer.cpp | 11 ++++++++--- src/gproshan/viewer/viewer.cpp | 3 ++- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index e90f7314..1ca94a15 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -25,8 +25,8 @@ class scene: public che struct material { - vec3 Ka = {0.2, 0.2, 0.2}; - vec3 Kd = {0.8, 0.8, 0.8}; + vec3 Ka = {0.2f, 0.2f, 0.2f}; + vec3 Kd = {0.8f, 0.8f, 0.8f}; vec3 Ks = {1, 1, 1}; real_t d = 1; // Tr = 0, opposite real_t Ns = 0; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 09fca0c4..9873766b 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,7 @@ class viewer shader shader_normals; shader shader_gradient; shader shader_pointcloud; + scene::material mat; camera cam; quaternion cam_light; diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 994ea880..1cf3cc3d 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -29,11 +29,11 @@ void main() { vec3 color = lines_colormap(gs_mesh_color, gs_color); - vec3 Ka = mat.Ka * color; + vec3 Ka = mat.Ka; if(mat.map_Ka != -1) Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; - vec3 Kd = mat.Kd; + vec3 Kd = color; if(mat.map_Kd != -1) Kd *= texture(tex_Kd, gs_texcoord.xy).rgb; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index f0b93609..4c4feaa4 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -27,12 +27,17 @@ che * app_viewer::load_mesh(const std::string & file_path) std::string extension = file_path.substr(pos + 1); - if(extension == "off") return new che_off(file_path); if(extension == "obj") { - return new scene(file_path); -// return new che_obj(file_path); + scene * sc = new scene(file_path); + if(sc->objects.size() == 1) + { + delete sc; + return new che_obj(file_path); + } + return sc; } + if(extension == "off") return new che_off(file_path); if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); if(extension == "xyz") return new che_xyz(file_path); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 10370be3..65176287 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -42,6 +42,7 @@ const std::vector viewer::m_window_split = { {1, 1}, const size_t viewer::max_meshes = m_window_split.size() - 1; const std::vector viewer::colormap = { "vertex color", + "material scene", "blue", "red", "blue/read", @@ -195,7 +196,7 @@ void viewer::imgui() { ImGui::Text("%13lu fps", size_t(1.0 / render_time)); ImGui::Text("%13lu vertices", mesh->n_vertices); - ImGui::Text("%13lu trigs", mesh->n_trigs); + ImGui::Text("%13lu trigs", mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_trigs); ImGui::Indent(); if(ImGui::Combo("fit screen", (int *) &mesh.opt_fit_screen, "none\0box (2x2x2)\0sphere (97.72%)\0\0")) From 0c4993b53723758c3d43b47f55ebb96d635890a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Nov 2022 14:38:35 +0100 Subject: [PATCH 0769/1018] shader: updated with option for materials --- shaders/fragment.glsl | 40 +------------------- shaders/fragment_pointcloud.glsl | 5 +-- shaders/fragment_sphere.glsl | 6 +-- shaders/shading.glsl | 65 +++++++++++++++++++++----------- shaders/vertex_pointcloud.glsl | 3 ++ src/gproshan/viewer/viewer.cpp | 10 ++--- 6 files changed, 56 insertions(+), 73 deletions(-) diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index 1cf3cc3d..6cae5d5c 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,7 +1,6 @@ #version 410 core #include shading.glsl -#include material.glsl in vec3 gs_position; in vec3 gs_normal; @@ -13,37 +12,15 @@ noperspective in vec3 edge_dist; layout(location = 0) out vec4 frag_color; - -uniform vec3 eye; -uniform vec3 cam_light; uniform bool render_flat; uniform bool render_wireframe; -uniform sampler2D tex_Ka; -uniform sampler2D tex_Kd; -uniform sampler2D tex_Ks; - -uniform material mat; void main() { + vec3 normal = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); vec3 color = lines_colormap(gs_mesh_color, gs_color); - vec3 Ka = mat.Ka; - if(mat.map_Ka != -1) - Ka *= texture(tex_Ka, gs_texcoord.xy).rgb; - - vec3 Kd = color; - if(mat.map_Kd != -1) - Kd *= texture(tex_Kd, gs_texcoord.xy).rgb; - - vec3 Ks = mat.Ks; - if(mat.map_Ks != -1) - Ks *= texture(tex_Ks, gs_texcoord.xy).rgb; - - - vec3 n = render_flat ? normalize(cross(dFdx(gs_position), dFdy(gs_position))) : normalize(gs_normal); - if(render_wireframe) { vec3 delta = fwidth(edge_dist); @@ -52,19 +29,6 @@ void main() color = mix(vec3(.2), color, d); } - vec3 l = cam_light - gs_position; - float r = length(l); - l /= r; - vec3 v = normalize(eye - gs_position); - vec3 h = normalize(l + v); - float lambertian = max(dot(l, n), 0.0); - float specular = pow(max(dot(h, n), 0.0), mat.Ns); - float P = 4; - vec3 La = vec3(0.2, 0.2, 0.2); - - color = Ka * La + (lambertian * Kd + specular * Ks) * P / (r * r); - //color = shading(n, normalize(cam_light - gs_position), normalize(eye - gs_position), Kd); - //color = Ka + Kd + Ks; - frag_color = vec4(color, 1); + frag_color = vec4(shading(color, normal, gs_position, gs_texcoord), 1); } diff --git a/shaders/fragment_pointcloud.glsl b/shaders/fragment_pointcloud.glsl index 9952196f..791d2e6a 100644 --- a/shaders/fragment_pointcloud.glsl +++ b/shaders/fragment_pointcloud.glsl @@ -6,11 +6,10 @@ in vec3 vs_position; in vec3 vs_normal; in vec3 vs_mesh_color; in float vs_color; +in vec2 vs_texcoord; layout(location = 0) out vec4 frag_color; -uniform vec3 eye; -uniform vec3 cam_light; uniform bool point_normals; @@ -19,7 +18,7 @@ void main() vec3 color = lines_colormap(vs_mesh_color, vs_color); if(point_normals) - color = shading(normalize(vs_normal), normalize(cam_light - vs_position), normalize(eye - vs_position), color); + color = shading(color, normalize(vs_normal), vs_position, vs_texcoord); frag_color = vec4(color, 1); } diff --git a/shaders/fragment_sphere.glsl b/shaders/fragment_sphere.glsl index e300d7b7..0892dfba 100644 --- a/shaders/fragment_sphere.glsl +++ b/shaders/fragment_sphere.glsl @@ -7,13 +7,9 @@ in vec3 vs_normal; layout(location = 0) out vec4 frag_color; -uniform vec3 eye; -uniform vec3 cam_light; - void main() { - vec3 color = shading(normalize(vs_normal), normalize(cam_light - vs_position), normalize(eye - vs_position), vec3(1, 0, 0)); - frag_color = vec4(color, 1); + frag_color = vec4(shading(vec3(1, 0, 0), normalize(vs_normal), vs_position, vec2(0)), 1); } diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 3f3f2f10..fd32a180 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -1,29 +1,18 @@ #include colormap.glsl +#include material.glsl -uniform bool render_lines; uniform uint idx_colormap; +uniform bool render_lines; +uniform vec3 eye; +uniform vec3 cam_light; -float diffuse(vec3 N, vec3 L) -{ - return max(0, dot(N, L)); -} - -float specular(vec3 N, vec3 L, vec3 E) -{ - const float shininess = 4; - vec3 R = 2 * dot(L, N) * N - L; - - return pow(max(0, dot(R, E)), shininess); -} +uniform sampler2D tex_Ka; +uniform sampler2D tex_Kd; +uniform sampler2D tex_Ks; -float fresnel(vec3 N, vec3 E) -{ - const float sharpness = 10; - float NE = max(0, dot(N, E)); +uniform material mat; - return pow(sqrt(1. - NE * NE ), sharpness); -} vec3 lines_colormap(vec3 color, float h) { @@ -41,9 +30,41 @@ vec3 lines_colormap(vec3 color, float h) return color; } -vec3 shading(vec3 N, vec3 L, vec3 E, vec3 color) +vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) { - vec3 one = vec3(1); - return diffuse(N, L) * color + 0.1 * specular(N, L, E) * one + 0.25 * fresnel(N, E) * one; + vec3 Ka = vec3(0.4); + vec3 Kd = color; + vec3 Ks = vec3(0.2); + float Ns = 4; + + if(idx_colormap == 5) + { + Ka = mat.Ka; + if(mat.map_Ka != -1) + Ka *= texture(tex_Ka, texcoord).rgb; + + Kd = mat.Kd; + if(mat.map_Kd != -1) + Kd *= texture(tex_Kd, texcoord).rgb; + + Ks = mat.Ks; + if(mat.map_Ks != -1) + Ks *= texture(tex_Ks, texcoord).rgb; + + Ns = mat.Ns; + } + + vec3 l = cam_light - pos; + float r = length(l); + l /= r; + vec3 v = normalize(eye - pos); + vec3 h = normalize(l + v); + float lambertian = max(dot(l, n), 0.0); + float specular = pow(max(dot(h, n), 0.0), Ns); + float P = 8; + vec3 La = vec3(0.2, 0.2, 0.2); + + return Ka * La + (lambertian * Kd + specular * Ks) * P / (r * r); } + diff --git a/shaders/vertex_pointcloud.glsl b/shaders/vertex_pointcloud.glsl index 9e83aa3d..6782aa56 100644 --- a/shaders/vertex_pointcloud.glsl +++ b/shaders/vertex_pointcloud.glsl @@ -4,11 +4,13 @@ layout (location=0) in vec3 in_position; layout (location=1) in vec3 in_normal; layout (location=2) in vec3 in_mesh_color; layout (location=3) in float in_color; +layout (location=4) in vec2 in_texcoord; out vec3 vs_position; out vec3 vs_normal; out vec3 vs_mesh_color; out float vs_color; +out vec2 vs_texcoord; uniform mat4 proj_view_mat; uniform mat4 model_mat; @@ -20,6 +22,7 @@ void main() vs_normal = in_normal; vs_mesh_color = in_mesh_color; vs_color = in_color; + vs_texcoord = in_texcoord; gl_Position = proj_view_mat * vec4(vs_position, 1); gl_PointSize = point_size; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 65176287..67120f39 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -42,11 +42,11 @@ const std::vector viewer::m_window_split = { {1, 1}, const size_t viewer::max_meshes = m_window_split.size() - 1; const std::vector viewer::colormap = { "vertex color", + "blue heatmap", + "red heatmap", + "blue/read heatmap", + "set heatmap", "material scene", - "blue", - "red", - "blue/read", - "set" }; che_sphere viewer::sphere_data{0.01}; @@ -69,7 +69,7 @@ viewer::viewer(const int & width, const int & height) frames = new frame[max_meshes]; - render_params.add_light({-1, 1, -2}); + render_params.add_light({-1, 1, -4}); } viewer::~viewer() From 561160e4b83d4b8af41a8bef9f85165f0cb4a0af Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Nov 2022 20:59:24 +0100 Subject: [PATCH 0770/1018] scene: loading texture CImgExeption --- include/gproshan/scenes/scene.h | 1 + src/gproshan/app_viewer.cpp | 2 +- src/gproshan/scenes/scene.cpp | 34 ++++++++++++++++++++------------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 1ca94a15..d2549e51 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -56,6 +56,7 @@ class scene: public che std::vector objects; vec2 * texcoords = nullptr; + bool load_scene = true; public: scene(const std::string & file); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 4c4feaa4..8475365e 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -30,7 +30,7 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "obj") { scene * sc = new scene(file_path); - if(sc->objects.size() == 1) + if(!sc->is_scene()) { delete sc; return new che_obj(file_path); diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 33595ede..3450b733 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -25,7 +25,7 @@ scene::~scene() bool scene::is_scene() const { - return true; + return load_scene && objects.size() > 1; } bool scene::is_pointcloud() const @@ -35,7 +35,7 @@ bool scene::is_pointcloud() const void scene::read_file(const std::string & file) { - load_obj(file); + load_scene = load_obj(file); } bool scene::load_obj(const std::string & file) @@ -198,17 +198,25 @@ bool scene::load_mtl(const std::string & file) bool scene::load_texture(const std::string & file) { - CImg img(file.c_str()); - img.mirror('y'); - - textures.emplace_back(); - texture & tex = textures.back(); - tex.width = img.width(); - tex.height = img.height(); - tex.spectrum = img.spectrum(); - tex.data = new unsigned char[tex.width * tex.height * tex.spectrum]; - img.permute_axes("cxyz"); - memcpy(tex.data, img.data(), tex.width * tex.height * tex.spectrum); + try + { + CImg img(file.c_str()); + img.mirror('y'); + + textures.emplace_back(); + texture & tex = textures.back(); + tex.width = img.width(); + tex.height = img.height(); + tex.spectrum = img.spectrum(); + tex.data = new unsigned char[tex.width * tex.height * tex.spectrum]; + img.permute_axes("cxyz"); + memcpy(tex.data, img.data(), tex.width * tex.height * tex.spectrum); + } + catch(CImgException & e) + { + gproshan_error_var(e.what()); + return false; + } return true; } From bd79a2b00620eeffb7d3ddb6192b4f8d02c17262 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Nov 2022 23:09:20 +0100 Subject: [PATCH 0771/1018] scene_viewer: added render pointcloud --- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/scene_viewer.h | 1 + shaders/shading.glsl | 9 +++--- src/gproshan/viewer/che_viewer.cpp | 6 +--- src/gproshan/viewer/scene_viewer.cpp | 39 +++++++++++++++++++++++++- src/gproshan/viewer/viewer.cpp | 4 +-- 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 52b770d8..bf58a1dd 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -79,7 +79,7 @@ class che_viewer void update_instances_positions(const std::vector & translations); virtual void draw(shader & program); - virtual void draw_point_cloud(shader & program); + virtual void draw_pointcloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); void select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); diff --git a/include/gproshan/viewer/scene_viewer.h b/include/gproshan/viewer/scene_viewer.h index 28b939c0..7bafe0ea 100644 --- a/include/gproshan/viewer/scene_viewer.h +++ b/include/gproshan/viewer/scene_viewer.h @@ -21,6 +21,7 @@ class scene_viewer: public che_viewer ~scene_viewer(); void init_texture(const GLuint & gltex, const scene::texture & tex); void draw(shader & program); + void draw_pointcloud(shader & program); void gl_uniform_material(shader & program, const scene::material & mat); void update_vbo_texcoords(); }; diff --git a/shaders/shading.glsl b/shaders/shading.glsl index fd32a180..d5352a0c 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -13,6 +13,9 @@ uniform sampler2D tex_Ks; uniform material mat; +const float Lp = 10; +const vec3 La = vec3(0.2, 0.2, 0.2); + vec3 lines_colormap(vec3 color, float h) { @@ -20,7 +23,7 @@ vec3 lines_colormap(vec3 color, float h) if(render_lines) { - h = h * 40; + h = 50 * h; h = h - floor(h); h = (1 / (1 + exp(-100 * (h - .55)))) + (1 / (1 + exp(-100 * (-h + .45)))); h = 1 - h; @@ -61,10 +64,8 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) vec3 h = normalize(l + v); float lambertian = max(dot(l, n), 0.0); float specular = pow(max(dot(h, n), 0.0), Ns); - float P = 8; - vec3 La = vec3(0.2, 0.2, 0.2); - return Ka * La + (lambertian * Kd + specular * Ks) * P / (r * r); + return Ka * La + (lambertian * Kd + specular * Ks) * Lp / (r * r); } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 6b1e9c39..dad29c0f 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -200,7 +200,7 @@ void che_viewer::draw(shader & program) program.disable(); } -void che_viewer::draw_point_cloud(shader & program) +void che_viewer::draw_pointcloud(shader & program) { glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); @@ -211,11 +211,7 @@ void che_viewer::draw_point_cloud(shader & program) program.enable(); glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); - glDrawArrays(GL_POINTS, 0, mesh->n_vertices); - - glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); program.disable(); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 62cca29a..662beca1 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -46,8 +46,8 @@ void scene_viewer::draw(shader & program) { glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); - glProgramUniform1i(program, program("render_flat"), render_flat); glProgramUniform1i(program, program("render_lines"), render_lines); + glProgramUniform1i(program, program("render_flat"), render_flat); glProgramUniform1i(program, program("render_wireframe"), render_triangles); glPolygonMode(GL_FRONT_AND_BACK, render_wireframe ? GL_LINE : GL_FILL); @@ -81,6 +81,43 @@ void scene_viewer::draw(shader & program) program.disable(); } +void scene_viewer::draw_pointcloud(shader & program) +{ + glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); + glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); + glProgramUniform1i(program, program("render_lines"), render_lines); + glProgramUniform1i(program, program("point_normals"), point_normals); + glProgramUniform1ui(program, program("point_size"), point_size); + + program.enable(); + + glBindVertexArray(vao); + if(sc->objects.size() == 1) + { + glDrawArrays(GL_POINTS, 0, mesh->n_vertices); + } + else + { + for(index_t i = 0; i < sc->objects.size() - 1; ++i) + { + const scene::object & obj = sc->objects[i]; + const scene::material & mat = sc->materials[obj.material_id]; + + gl_uniform_material(program, mat); + glDrawArrays(GL_POINTS, obj.begin, sc->objects[i + 1].begin - obj.begin); + + for(auto & gltex_num: gltex_nums) + { + glActiveTexture(gltex_num); + glBindTexture(GL_TEXTURE_2D, 0); + } + } + } + glBindVertexArray(0); + + program.disable(); +} + void scene_viewer::gl_uniform_material(shader & program, const scene::material & mat) { glProgramUniform3f(program, program("mat.Ka"), mat.Ka.x(), mat.Ka.y(), mat.Ka.z()); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 67120f39..23395ddb 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -984,12 +984,12 @@ void viewer::render_gl() } if(mesh->is_pointcloud() || mesh.render_pointcloud) - mesh.draw_point_cloud(shader_pointcloud); + mesh.draw_pointcloud(shader_pointcloud); else mesh.draw(shader_triangles); if(mesh.render_normals) - mesh.draw_point_cloud(shader_normals); + mesh.draw_pointcloud(shader_normals); if(mesh.render_gradients) mesh.draw(shader_gradient); From 20bf0e0c0d0054f55023d6671ffd3ee45feda9d9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 30 Nov 2022 17:33:02 +0100 Subject: [PATCH 0772/1018] vec: refactoring code, using dot and cross --- include/gproshan/geometry/mat.h | 4 +-- include/gproshan/geometry/vec.h | 33 ++++++++----------- src/gproshan/app_viewer.cpp | 7 +++- src/gproshan/geodesics/geodesics_ptp.cpp | 12 +++---- src/gproshan/geodesics/geodesics_ptp.cu | 12 +++---- .../geodesics/geodesics_ptp_coalescence.cu | 12 +++---- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geometry/convex_hull.cpp | 2 +- src/gproshan/mdict/patch.cpp | 18 +++++----- src/gproshan/mesh/che.cpp | 14 ++++---- src/gproshan/mesh/che_fill_hole.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 4 +-- src/gproshan/mesh/simplification.cpp | 2 +- src/gproshan/raytracing/rt_optix.cu | 2 +- src/gproshan/scenes/scene.cpp | 2 +- src/gproshan/viewer/camera.cpp | 8 ++--- 16 files changed, 68 insertions(+), 68 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 225b6d94..558c9644 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -59,7 +59,7 @@ class mat mat bt = transpose(b); for(index_t i = 0; i < N; ++i) for(index_t j = 0; j < N; ++j) - res[i][j] = (rows[i], bt[j]); + res[i][j] = dot(rows[i], bt[j]); return res; } @@ -68,7 +68,7 @@ class mat { vec res; for(index_t i = 0; i < N; ++i) - res[i] = (rows[i], v); + res[i] = dot(rows[i], v); return res; } diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index e7a8454b..1162bee4 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -132,26 +132,26 @@ class vec return norm(); } - ///< dot product + ///< scalar product __host__ __device__ - T operator , (const vec & v) const + vec operator * (const T & a) const { - T res = 0; + vec res; for(index_t i = 0; i < N; ++i) - res += values[i] * v[i]; + res[i] = values[i] * a; return res; } - - ///< scalar product +/* + ///< element wise product __host__ __device__ - vec operator * (const T & a) const + vec operator * (const vec & v) const { vec res; for(index_t i = 0; i < N; ++i) - res[i] = values[i] * a; + res[i] = values[i] * v[i]; return res; } - +*/ ///< scalar division __host__ __device__ vec operator / (const T & a) const @@ -274,7 +274,7 @@ vec operator * (const T & a, const vec & v) ///< cross product template __host__ __device__ -vec operator * (const vec & u, const vec & v) +vec cross(const vec & u, const vec & v) { const T & ux = u[0]; const T & uy = u[1]; @@ -286,20 +286,15 @@ vec operator * (const vec & u, const vec & v) return {uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx}; } -///< cross product -template -__host__ __device__ -vec cross(const vec & u, const vec & v) -{ - return u * v; -} - ///< dot product template __host__ __device__ T dot(const vec & u, const vec & v) { - return (u, v); + T res = 0; + for(index_t i = 0; i < N; ++i) + res += u[i] * v[i]; + return res; } ///< norm diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 8475365e..6c65834a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -48,6 +48,11 @@ che * app_viewer::load_mesh(const std::string & file_path) int app_viewer::main(int nargs, const char ** args) { + //vec3 a = {2, 1, 4}; + //vec3 b = {3, 5, 7}; + //gproshan_log_var(a * b); + //gproshan_log_var(a * b); + if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); @@ -245,7 +250,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) { a = mesh->vertex_he(next(he)) - mesh->point(v); b = mesh->vertex_he(prev(he)) - mesh->point(v); - g += acos((a,b) / (norm(a) * norm(b))); + g += acos(dot(a, b) / (norm(a) * norm(b))); } //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); gv(v) = mesh->mean_curvature(v); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 0b0dfaa7..65b5bda9 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -213,10 +213,10 @@ real_t update_step(che * mesh, const real_t * dist, const index_t & he) t[1] = dist[x[1]]; real_t q[2][2]; - q[0][0] = (X[0], X[0]); - q[0][1] = (X[0], X[1]); - q[1][0] = (X[1], X[0]); - q[1][1] = (X[1], X[1]); + q[0][0] = dot(X[0], X[0]); + q[0][1] = dot(X[0], X[1]); + q[1][0] = dot(X[1], X[0]); + q[1][1] = dot(X[1], X[1]); real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; real_t Q[2][2]; @@ -243,8 +243,8 @@ real_t update_step(che * mesh, const real_t * dist, const index_t & he) }; real_t cond[2]; - cond[0] = (X[0] , n); - cond[1] = (X[1] , n); + cond[0] = dot(X[0] , n); + cond[1] = dot(X[1] , n); real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 3b097166..676ba03e 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -313,10 +313,10 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) t[1] = dist[x[1]]; real_t q[2][2]; - q[0][0] = (X[0], X[0]); - q[0][1] = (X[0], X[1]); - q[1][0] = (X[1], X[0]); - q[1][1] = (X[1], X[1]); + q[0][0] = dot(X[0], X[0]); + q[0][1] = dot(X[0], X[1]); + q[1][0] = dot(X[1], X[0]); + q[1][1] = dot(X[1], X[1]); real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; real_t Q[2][2]; @@ -348,8 +348,8 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) }; real_t cond[2]; - cond[0] = (X[0] , n); - cond[1] = (X[1] , n); + cond[0] = dot(X[0], n); + cond[1] = dot(X[1], n); real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index 3edbbd50..b487741a 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -225,10 +225,10 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) t[1] = dist[x[1]]; real_t q[2][2]; - q[0][0] = (X[0], X[0]); - q[0][1] = (X[0], X[1]); - q[1][0] = (X[1], X[0]); - q[1][1] = (X[1], X[1]); + q[0][0] = dot(X[0], X[0]); + q[0][1] = dot(X[0], X[1]); + q[1][0] = dot(X[1], X[0]); + q[1][1] = dot(X[1], X[1]); real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; real_t Q[2][2]; @@ -262,8 +262,8 @@ real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) }; real_t cond[2]; - cond[0] = (X[0] , n); - cond[1] = (X[1] , n); + cond[0] = dot(X[0] , n); + cond[1] = dot(X[1] , n); real_t c[2]; c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 80ea6c0d..06560f51 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -95,7 +95,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) const vertex & nhe = mesh->normal_he(he); const vertex & vhe = mesh->vertex_he(prev(he)) - mesh->vertex_he(next(he)); const vertex & ghe = mesh->gradient_he(he, u.memptr()); - sum += (nhe * vhe , -ghe); + sum += dot(cross(nhe, vhe), -ghe); } } } diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index e034d542..0b289bc8 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -58,7 +58,7 @@ void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_point bool convex_hull::ccw(const vertex & p, const vertex & q, const vertex & r) { // TODO vec2 - return ((q - p) * (r - p)).z() > 0; + return cross(q - p, r - p).z() > 0; } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 27f0f9ef..777d3d5c 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -110,17 +110,17 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ else i = find(vertices.data(), vertices.size(), b); - tmp_angle = acos( (mesh->normal_he(he), N[i]) ); + tmp_angle = acos(dot(mesh->normal_he(he), N[i])); - if(angle > tmp_angle && tmp_angle < thr_angle && acos( (mesh->normal_he(he), N[0]) ) < deviation) // Fullfill conditions + if(angle > tmp_angle && tmp_angle < thr_angle && acos(dot(mesh->normal_he(he), N[0])) < deviation) // Fullfill conditions { angle = tmp_angle; area_face = mesh->area_trig(he / 3); // compute projected area - pav = va - vv + ( (n,vv) - (n,va) ) * n; - pbv = vb - vv + ( (n,vv) - (n,vb) ) * n; - proj_area_face = norm(pav * pbv) / 2; + pav = va - vv + (dot(n, vv) - dot(n, va)) * n; + pbv = vb - vv + (dot(n, vv) - dot(n, vb)) * n; + proj_area_face = norm(cross(pav, pbv)) / 2; min_he = mesh->normal_he(he); added = true; @@ -213,7 +213,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind c = mesh->point(v); // central vertices p = p - c ; - p = p - ((p,n)*n); + p = p - dot(p, n) * n; if(norm(p) > radio) { @@ -296,7 +296,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, p = mesh->point(vi); p = p - c ; - p = p - ((p, n) * n); + p = p - dot(p, n) * n; radio = std::max(radio, norm(p)); } @@ -637,9 +637,9 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) vertex c = mesh->point(v); vertex ny; nx = nx - c ; - nx = nx - ((nx,nz)*nz); + nx = nx - dot(nx, nz) * nz; - ny = (nz * nx); + ny = cross(nz, nx); nx = normalize(nx); ny = normalize(ny); nz = normalize(nz); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 58c0a6b1..67ef8abf 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -115,7 +115,7 @@ vertex che::normal_he(const index_t & he) const const vertex & b = GT[VT[next(he)]]; const vertex & c = GT[VT[prev(he)]]; - return normalize((b - a) * (c - a)); + return normalize(cross(b - a, c - a)); } vertex che::gradient_he(const index_t & he, const real_t * f) const @@ -130,9 +130,9 @@ vertex che::gradient_he(const index_t & he, const real_t * f) const vertex n = normal_he(he); - vertex pij = n * (xj - xi); - vertex pjk = n * (xk - xj); - vertex pki = n * (xi - xk); + vertex pij = cross(n, xj - xi); + vertex pjk = cross(n, xk - xj); + vertex pki = cross(n, xi - xk); return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } @@ -906,7 +906,7 @@ real_t che::cotan(const index_t & he) const vertex a = GT[VT[he]] - GT[VT[prev(he)]]; vertex b = GT[VT[next(he)]] - GT[VT[prev(he)]]; - return (a, b) / norm(a * b); + return dot(a, b) / norm(cross(a, b)); } // https://www.mathworks.com/help/pde/ug/pdetriq.html @@ -930,7 +930,7 @@ real_t che::area_trig(const index_t & t) const vertex a = GT[VT[next(he)]] - GT[VT[he]]; vertex b = GT[VT[prev(he)]] - GT[VT[he]]; - return norm(a * b) / 2; + return norm(cross(a, b)) / 2; } real_t che::area_vertex(const index_t & v) const @@ -951,7 +951,7 @@ real_t che::mean_curvature(const index_t & v) const for(const index_t & he: star(v)) { a += area_trig(trig(he)); - h += norm(GT[VT[next(he)]] - GT[v]) * (normal(v), normal_he(he)); + h += norm(GT[VT[next(he)]] - GT[v]) * dot(normal(v), normal_he(he)); } return 0.75 * h / a; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 7db8db7c..26e8d12c 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -36,7 +36,7 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert v = mesh->point(b); normal_v = mesh->normal(b); edge_v = mesh->vertex_he(next(mesh->evt(b))) - v; - edge_v -= (normal_v, edge_v) * normal_v; + edge_v -= dot(normal_v, edge_v) * normal_v; E(0, 2) = normal_v.x(); E(1, 2) = normal_v.y(); diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 31758b83..91a93ac0 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -146,7 +146,7 @@ quaternion quaternion::operator * (const quaternion & q) const const vertex & v1(v); const vertex & v2(q.v); - return quaternion(s1*s2 - (v1,v2), s1*v2 + s2*v1 + (v1*v2)); + return quaternion(s1 * s2 - dot(v1, v2), s1 * v2 + s2 * v1 + cross(v1, v2)); } void quaternion::operator *= (const quaternion & q) @@ -171,7 +171,7 @@ real_t quaternion::norm() const real_t quaternion::norm2() const { - return s * s + (v , v); + return s * s + dot(v, v); } quaternion quaternion::unit() const diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index b3bf936f..d10708b4 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -41,7 +41,7 @@ void simplification::compute_quadrics() p(0) = n.x(); p(1) = n.y(); p(2) = n.z(); - p(3) = -(n, mesh->point(v)); + p(3) = -dot(n, mesh->point(v)); Q[v] += p * p.t(); } diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 846f84f2..87d7d0ca 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -55,7 +55,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex & C = data[2]; eval_hit hit(mesh, primID, bar.x, bar.y); - hit.normal = optix_params.flat ? normalize((B - A) * (C - A)) : hit.normal; + hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 3450b733..abfc2449 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -74,7 +74,7 @@ bool scene::load_obj(const std::string & file) { const index_t & trig = 3 * (i / 3); const index_t & n = p.trigs[i].z(); - VN[i] = n != NIL ? p.vnormals[n] : normalize((GT[trig + 1] - GT[trig]) * (GT[trig + 2] - GT[trig])); + VN[i] = n != NIL ? p.vnormals[n] : normalize(cross(GT[trig + 1] - GT[trig], GT[trig + 2] - GT[trig])); } for(auto & obj: p.objects) diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 7ac035fe..9d704e38 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -15,12 +15,12 @@ mat4 camera::look_at(const quaternion & r) Z = normalize(Z); vec3 Y = r.conj() * up * r; Y = normalize(Y); - vec3 X = Z * Y; + vec3 X = cross(Z, Y); mat4 view; - view[0] = {X, -(X, eye)}; - view[1] = {Y, -(Y, eye)}; - view[2] = {-Z, (Z, eye)}; + view[0] = {X, -dot(X, eye.v)}; + view[1] = {Y, -dot(Y, eye.v)}; + view[2] = {-Z, dot(Z, eye.v)}; view[3] = {0, 0, 0, 1}; return view; From 2fe5f18fe499631d247f234e46ef1edd16a8be1e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 30 Nov 2022 21:39:01 +0100 Subject: [PATCH 0773/1018] geodesics: refactoring update_step --- include/gproshan/geodesics/geodesics_ptp.cuh | 3 - include/gproshan/geodesics/geodesics_ptp.h | 50 +++++++++++- include/gproshan/geometry/vec.h | 13 ++- src/gproshan/app_viewer.cpp | 5 -- src/gproshan/geodesics/geodesics.cpp | 7 +- src/gproshan/geodesics/geodesics_ptp.cpp | 77 +++--------------- src/gproshan/geodesics/geodesics_ptp.cu | 81 ++----------------- .../geodesics/geodesics_ptp_coalescence.cu | 10 ++- 8 files changed, 91 insertions(+), 155 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh index b881e599..2c703e5b 100644 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ b/include/gproshan/geodesics/geodesics_ptp.cuh @@ -14,9 +14,6 @@ namespace gproshan { index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); -__forceinline__ __device__ -real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he); - __global__ void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start = 0); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 8892a42b..cc37d7bf 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -41,10 +41,56 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); -real_t update_step(che * mesh, const real_t * dist, const index_t & he); - void normalize_ptp(real_t * dist, const size_t & n); +template +__host__ __device__ +real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) +{ + const vec X[2] = {mesh->GT[x[0]] - mesh->GT[x[2]], + mesh->GT[x[1]] - mesh->GT[x[2]] + }; + + const vec & t = {dist[x[0]], dist[x[1]]}; + + mat q; + q[0][0] = dot(X[0], X[0]); + q[0][1] = dot(X[0], X[1]); + q[1][0] = dot(X[1], X[0]); + q[1][1] = dot(X[1], X[1]); + + const T & det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; + + mat Q; + Q[0][0] = q[1][1] / det; + Q[0][1] = -q[0][1] / det; + Q[1][0] = -q[1][0] / det; + Q[1][1] = q[0][0] / det; + + const T & delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); + const T & dis = delta * delta - + (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * + (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); + + T p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); + + const vec & tp = t - p; + const vec & n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), + tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), + tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) + }; + + const vec & c = Q * vec{dot(X[0], n), dot(X[1], n)}; + + if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) + { + const vec & dp = {dist[x[0]] + norm(X[0]), dist[x[1]] + norm(X[1])}; + p = dp[dp[1] < dp[0]]; + } + + return p; +} + } // namespace gproshan diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 1162bee4..9f57cd58 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -141,7 +141,7 @@ class vec res[i] = values[i] * a; return res; } -/* + ///< element wise product __host__ __device__ vec operator * (const vec & v) const @@ -151,7 +151,7 @@ class vec res[i] = values[i] * v[i]; return res; } -*/ + ///< scalar division __host__ __device__ vec operator / (const T & a) const @@ -201,6 +201,15 @@ class vec return *this; } + ///< element wise product self assign + __host__ __device__ + const vec & operator *= (const vec & v) + { + for(index_t i = 0; i < N; ++i) + values[i] *= v[i]; + return *this; + } + ///< scalar division self assign __host__ __device__ const vec & operator /= (const T & a) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 6c65834a..325fab9b 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -48,11 +48,6 @@ che * app_viewer::load_mesh(const std::string & file_path) int app_viewer::main(int nargs, const char ** args) { - //vec3 a = {2, 1, 4}; - //vec3 b = {3, 5, 7}; - //gproshan_log_var(a * b); - //gproshan_log_var(a * b); - if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index a6b8a114..eb9076a9 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -112,6 +112,8 @@ void geodesics::execute(che * mesh, const std::vector & sources, const void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun) { + CHE cmesh(mesh); + index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -167,7 +169,10 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dv = dist[v]; for(const index_t & he: mesh->star(v)) { - dp = update_step(mesh, dist, he); + dp = update_step(&cmesh, dist, {mesh->halfedge(next(he)), + mesh->halfedge(prev(he)), + mesh->halfedge(he) + }); if(dp < dv) { dv = dp; diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 65b5bda9..5df46d58 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -42,6 +42,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c index_t * inv = nullptr; mesh = ptp_coalescence(inv, mesh, toplesets); + CHE cmesh(mesh); // ------------------------------------------------------ real_t * pdist[2] = {new real_t[mesh->n_vertices], new real_t[mesh->n_vertices]}; @@ -81,7 +82,10 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c real_t p; for(const index_t & he: mesh->star(v)) { - p = update_step(mesh, pdist[d], he); + p = update_step(&cmesh, pdist[d], { mesh->halfedge(next(he)), + mesh->halfedge(prev(he)), + mesh->halfedge(he) + }); if(p < pdist[!d][v]) { pdist[!d][v] = p; @@ -120,6 +124,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) { + CHE cmesh(mesh); real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices]}; real_t * error = new real_t[mesh->n_vertices]; @@ -158,7 +163,10 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t p; for(const index_t & he: mesh->star(v)) { - p = update_step(mesh, pdist[d], he); + p = update_step(&cmesh, pdist[d], { mesh->halfedge(next(he)), + mesh->halfedge(prev(he)), + mesh->halfedge(he) + }); if(p < pdist[!d][v]) { pdist[!d][v] = p; @@ -197,71 +205,6 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c else delete [] pdist[d]; } -real_t update_step(che * mesh, const real_t * dist, const index_t & he) -{ - index_t x[3]; - x[0] = mesh->halfedge(next(he)); - x[1] = mesh->halfedge(prev(he)); - x[2] = mesh->halfedge(he); - - vertex X[2]; - X[0] = mesh->point(x[0]) - mesh->point(x[2]); - X[1] = mesh->point(x[1]) - mesh->point(x[2]); - - real_t t[2]; - t[0] = dist[x[0]]; - t[1] = dist[x[1]]; - - real_t q[2][2]; - q[0][0] = dot(X[0], X[0]); - q[0][1] = dot(X[0], X[1]); - q[1][0] = dot(X[1], X[0]); - q[1][1] = dot(X[1], X[1]); - - real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - real_t Q[2][2]; - Q[0][0] = q[1][1] / det; - Q[0][1] = -q[0][1] / det; - Q[1][0] = -q[1][0] / det; - Q[1][1] = q[0][0] / det; - - real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - real_t dis = delta * delta - - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * - (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); - - real_t p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); - - real_t tp[2]; - tp[0] = t[0] - p; - tp[1] = t[1] - p; - - vertex n = { - tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) - }; - - real_t cond[2]; - cond[0] = dot(X[0] , n); - cond[1] = dot(X[1] , n); - - real_t c[2]; - c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; - c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; - - if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) - { - real_t dp[2]; - dp[0] = dist[x[0]] + norm(X[0]); - dp[1] = dist[x[1]] + norm(X[1]); - - p = dp[dp[1] < dp[0]]; - } - - return p; -} - void normalize_ptp(real_t * dist, const size_t & n) { real_t max_d = 0; diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 676ba03e..b0f28999 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -244,7 +244,10 @@ void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * sorte real_t d; cu_for_star(he, mesh, v) { - d = cu_update_step(mesh, old_dist, he); + d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], + mesh->VT[cu_prev(he)], + mesh->VT[he] + }); if(d < new_dist[v]) new_dist[v] = d; } } @@ -268,7 +271,10 @@ void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_c real_t d; cu_for_star(he, mesh, v) { - d = cu_update_step(mesh, old_dist, he); + d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], + mesh->VT[cu_prev(he)], + mesh->VT[he] + }); if(d < new_dist[v]) { new_dist[v] = d; @@ -296,77 +302,6 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ } } -__forceinline__ __device__ -real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) -{ - index_t x[3]; - x[0] = mesh->VT[cu_next(he)]; - x[1] = mesh->VT[cu_prev(he)]; - x[2] = mesh->VT[he]; - - vertex X[2]; - X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; - X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; - - real_t t[2]; - t[0] = dist[x[0]]; - t[1] = dist[x[1]]; - - real_t q[2][2]; - q[0][0] = dot(X[0], X[0]); - q[0][1] = dot(X[0], X[1]); - q[1][0] = dot(X[1], X[0]); - q[1][1] = dot(X[1], X[1]); - - real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - real_t Q[2][2]; - Q[0][0] = q[1][1] / det; - Q[0][1] = -q[0][1] / det; - Q[1][0] = -q[1][0] / det; - Q[1][1] = q[0][0] / det; - - real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - real_t dis = delta * delta - - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * - (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); - -#ifdef GPROSHAN_FLOAT - real_t p = delta + sqrtf(dis); -#else - real_t p = delta + sqrt(dis); -#endif - - p /= Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]; - - real_t tp[2]; - tp[0] = t[0] - p; - tp[1] = t[1] - p; - - vertex n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) - }; - - real_t cond[2]; - cond[0] = dot(X[0], n); - cond[1] = dot(X[1], n); - - real_t c[2]; - c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; - c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; - - if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) - { - real_t dp[2]; - dp[0] = dist[x[0]] + norm(X[0]); - dp[1] = dist[x[1]] + norm(X[1]); - - p = dp[dp[1] < dp[0]]; - } - - return p; -} - __host__ __device__ bool is_ok::operator()(const real_t & val) const { diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index b487741a..cfb5ea36 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -174,7 +174,10 @@ void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, ind real_t d; cu_for_star(he, mesh, v) { - d = cu_update_step(mesh, old_dist, he); + d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], + mesh->VT[cu_prev(he)], + mesh->VT[he] + }); if(d < new_dist[v]) new_dist[v] = d; } } @@ -197,7 +200,10 @@ void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, ind real_t d; cu_for_star(he, mesh, v) { - d = cu_update_step(mesh, old_dist, he); + d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], + mesh->VT[cu_prev(he)], + mesh->VT[he] + }); if(d < new_dist[v]) { new_dist[v] = d; From abd70845de381c0f64b920399b1c1ecc1743b9b7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Dec 2022 01:32:38 +0100 Subject: [PATCH 0774/1018] geodesics: refactoring relax_ptp cuda kernel and host device function che: defines for he_trig, he_next, and he_prev --- include/gproshan/geodesics/geodesics_ptp.cuh | 5 +- include/gproshan/geodesics/geodesics_ptp.h | 22 +++ .../geodesics/geodesics_ptp_coalescence.cuh | 6 - include/gproshan/mesh/che.cuh | 13 -- include/gproshan/mesh/che.h | 27 +--- src/gproshan/app_viewer.cpp | 4 +- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/features/key_points.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 6 +- src/gproshan/geodesics/geodesics_ptp.cpp | 14 +- src/gproshan/geodesics/geodesics_ptp.cu | 56 +------- .../geodesics/geodesics_ptp_coalescence.cu | 133 +----------------- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 +- .../test_geodesics_ptp_coalescence.cu | 2 +- src/gproshan/mdict/patch.cpp | 12 +- src/gproshan/mesh/che.cpp | 126 ++++++++--------- src/gproshan/mesh/che.cu | 22 --- src/gproshan/mesh/che_fill_hole.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- 20 files changed, 123 insertions(+), 337 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh index 2c703e5b..9d874c07 100644 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ b/include/gproshan/geodesics/geodesics_ptp.cuh @@ -15,10 +15,7 @@ namespace gproshan { index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); __global__ -void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start = 0); - -__global__ -void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * sorted, index_t end, index_t start = 0); +void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); __global__ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index cc37d7bf..9147845b 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -91,6 +91,28 @@ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) return p; } +template +__host__ __device__ +void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) +{ + real_t & ndv = new_dist[v] = old_dist[v]; + + real_t d; + for_star(he, mesh, v) + { + const uvec3 i = {mesh->VT[he_next(he)], mesh->VT[he_prev(he)], mesh->VT[he]}; + + d = update_step(mesh, old_dist, i); + + if(d < ndv) + { + ndv = d; + if(new_clusters) + new_clusters[v] = old_dist[i.y()] < old_dist[i.x()] ? old_clusters[i.y()] : old_clusters[i.x()]; + } + } +} + } // namespace gproshan diff --git a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh index 18a3a919..05a514c3 100644 --- a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh @@ -12,12 +12,6 @@ namespace gproshan { index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); -__global__ -void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start = 0); - -__global__ -void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t end, index_t start = 0); - } // namespace gproshan diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index bd79600e..4a961477 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -4,23 +4,10 @@ #include -#define cu_for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[cu_prev(he)]) != stop ? he : NIL) - - // geometry processing and shape analysis framework namespace gproshan { -__host__ __device__ -index_t cu_trig(index_t he); - -__host__ __device__ -index_t cu_next(index_t he); - -__host__ __device__ -index_t cu_prev(index_t he); - - void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false, const bool & color = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index aeba4a8e..e2b6d123 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -9,6 +9,12 @@ #include +#define he_trig(he) ((he) / 3) +#define he_next(he) (3 * he_trig(he) + ((he) + 1) % 3) +#define he_prev(he) (3 * he_trig(he) + ((he) + 2) % 3) +#define for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[he_prev(he)]) != stop ? he : NIL) + + // geometry processing and shape analysis framework namespace gproshan { @@ -193,27 +199,6 @@ class che::star_he::iterator }; -// che halfedge functions - -inline index_t trig(const index_t & he) -{ - if(he == NIL) return NIL; - return he / che::mtrig; -} - -inline index_t next(const index_t & he) -{ - if(he == NIL) return NIL; - return che::mtrig * trig(he) + (he + 1) % che::mtrig; -} - -inline index_t prev(const index_t & he) -{ - if(he == NIL) return NIL; - return che::mtrig * trig(he) + (he + che::mtrig - 1) % che::mtrig; -} - - // simple che data structure struct CHE { diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 325fab9b..e2911c03 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -243,8 +243,8 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) g = 0; for(const index_t & he: mesh->star(v)) { - a = mesh->vertex_he(next(he)) - mesh->point(v); - b = mesh->vertex_he(prev(he)) - mesh->point(v); + a = mesh->vertex_he(he_next(he)) - mesh->point(v); + b = mesh->vertex_he(he_prev(he)) - mesh->point(v); g += acos(dot(a, b) / (norm(a) * norm(b))); } //gv(v) = (2 * M_PI - g) / mesh->area_vertex(v); diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index 306562a8..a9ef747d 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -53,7 +53,7 @@ void key_components::compute_kcs(che * mesh, const std::vector & kps) radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) for(const index_t & he: mesh->star(fm(i))) - join(fm(i), mesh->halfedge(next(he))); + join(fm(i), mesh->halfedge(he_next(he))); for(index_t i = 0; i < n_vertices; ++i) if(comp[i] == i && comp_size[i] > 1) diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 9a3aa9d5..aaf8f270 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -46,7 +46,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) kps.push_back(v); is_kp[v] = true; } - he = next(he); + he = he_next(he); } } diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index eb9076a9..8c4fcda3 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -169,8 +169,8 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dv = dist[v]; for(const index_t & he: mesh->star(v)) { - dp = update_step(&cmesh, dist, {mesh->halfedge(next(he)), - mesh->halfedge(prev(he)), + dp = update_step(&cmesh, dist, {mesh->halfedge(he_next(he)), + mesh->halfedge(he_prev(he)), mesh->halfedge(he) }); if(dp < dv) @@ -178,7 +178,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dv = dp; if(clusters) - clusters[v] = clusters[mesh->halfedge(prev(he))] ? clusters[mesh->halfedge(prev(he))] : clusters[mesh->halfedge(next(he))]; + clusters[v] = clusters[mesh->halfedge(he_prev(he))] ? clusters[mesh->halfedge(he_prev(he))] : clusters[mesh->halfedge(he_next(he))]; } } diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 5df46d58..464bfc00 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -30,7 +30,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top } for(index_t he = 0; he < mesh->n_half_edges; ++he) - if(inv[mesh->halfedge(he)] != NIL && inv[mesh->halfedge(prev(he))] != NIL && inv[mesh->halfedge(next(he))] != NIL) + if(inv[mesh->halfedge(he)] != NIL && inv[mesh->halfedge(he_prev(he))] != NIL && inv[mesh->halfedge(he_next(he))] != NIL) F.push_back(inv[mesh->halfedge(he)]); return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); @@ -82,8 +82,8 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c real_t p; for(const index_t & he: mesh->star(v)) { - p = update_step(&cmesh, pdist[d], { mesh->halfedge(next(he)), - mesh->halfedge(prev(he)), + p = update_step(&cmesh, pdist[d], { mesh->halfedge(he_next(he)), + mesh->halfedge(he_prev(he)), mesh->halfedge(he) }); if(p < pdist[!d][v]) @@ -91,7 +91,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c pdist[!d][v] = p; if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(prev(he))] : ptp_out.clusters[mesh->halfedge(next(he))]; + ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(he_prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(he_prev(he))] : ptp_out.clusters[mesh->halfedge(he_next(he))]; } } } @@ -163,8 +163,8 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t p; for(const index_t & he: mesh->star(v)) { - p = update_step(&cmesh, pdist[d], { mesh->halfedge(next(he)), - mesh->halfedge(prev(he)), + p = update_step(&cmesh, pdist[d], { mesh->halfedge(he_next(he)), + mesh->halfedge(he_prev(he)), mesh->halfedge(he) }); if(p < pdist[!d][v]) @@ -172,7 +172,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c pdist[!d][v] = p; if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(prev(he))] : ptp_out.clusters[mesh->halfedge(next(he))]; + ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(he_prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(he_prev(he))] : ptp_out.clusters[mesh->halfedge(he_next(he))]; } } } diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index b0f28999..b46437b3 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -209,10 +209,8 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r end = limits[j]; n_cond = limits[i + 1] - start; - if(h_clusters) - relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], d_sorted, end, start); - else - relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_sorted, end, start); + h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) + : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); cudaDeviceSynchronize(); @@ -229,60 +227,14 @@ index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, r return d; } -__global__ -void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * sorted, index_t end, index_t start) -{ - index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; - - if(v < end) - { - v = sorted[v]; - if(v < mesh->n_vertices) - { - new_dist[v] = old_dist[v]; - - real_t d; - cu_for_star(he, mesh, v) - { - d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], - mesh->VT[cu_prev(he)], - mesh->VT[he] - }); - if(d < new_dist[v]) new_dist[v] = d; - } - } - } -} - __global__ -void relax_ptp(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t * sorted, index_t end, index_t start) +void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; if(v < end) - { - v = sorted[v]; - if(v < mesh->n_vertices) - { - new_dist[v] = old_dist[v]; - new_clusters[v] = old_clusters[v]; - - real_t d; - cu_for_star(he, mesh, v) - { - d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], - mesh->VT[cu_prev(he)], - mesh->VT[he] - }); - if(d < new_dist[v]) - { - new_dist[v] = d; - new_clusters[v] = old_dist[mesh->VT[cu_prev(he)]] < old_dist[mesh->VT[cu_next(he)]] ? old_clusters[mesh->VT[cu_prev(he)]] : old_clusters[mesh->VT[cu_next(he)]]; - } - } - } - } + relax_ptp(mesh, new_dist, old_dist, new_clusters, old_clusters, sorted ? sorted[v] : v); } __global__ diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index cfb5ea36..2a31ef32 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -139,10 +139,8 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t end = inv.limits[j]; n_cond = inv.limits[i + 1] - start; - if(h_clusters) - relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], end, start); - else - relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], end, start); + h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end) + : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end); cudaDeviceSynchronize(); @@ -160,133 +158,6 @@ index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t return d; } -__global__ -void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t end, index_t start) -{ - index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; - - if(v < end) - { - if(v < mesh->n_vertices) - { - new_dist[v] = old_dist[v]; - - real_t d; - cu_for_star(he, mesh, v) - { - d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], - mesh->VT[cu_prev(he)], - mesh->VT[he] - }); - if(d < new_dist[v]) new_dist[v] = d; - } - } - } -} - - -__global__ -void relax_ptp_coalescence(CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, index_t end, index_t start) -{ - index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; - - if(v < end) - { - if(v < mesh->n_vertices) - { - new_dist[v] = old_dist[v]; - new_clusters[v] = old_clusters[v]; - - real_t d; - cu_for_star(he, mesh, v) - { - d = update_step(mesh, old_dist, { mesh->VT[cu_next(he)], - mesh->VT[cu_prev(he)], - mesh->VT[he] - }); - if(d < new_dist[v]) - { - new_dist[v] = d; - new_clusters[v] = old_dist[mesh->VT[cu_prev(he)]] < old_dist[mesh->VT[cu_next(he)]] ? old_clusters[mesh->VT[cu_prev(he)]] : old_clusters[mesh->VT[cu_next(he)]]; - } - } - } - } -} - -__forceinline__ __device__ -real_t cu_update_step(CHE * mesh, const real_t * dist, const index_t & he) -{ - index_t x[3]; - x[0] = mesh->VT[cu_next(he)]; - x[1] = mesh->VT[cu_prev(he)]; - x[2] = mesh->VT[he]; - - vertex X[2]; - X[0] = mesh->GT[x[0]] - mesh->GT[x[2]]; - X[1] = mesh->GT[x[1]] - mesh->GT[x[2]]; - - real_t t[2]; - t[0] = dist[x[0]]; - t[1] = dist[x[1]]; - - real_t q[2][2]; - q[0][0] = dot(X[0], X[0]); - q[0][1] = dot(X[0], X[1]); - q[1][0] = dot(X[1], X[0]); - q[1][1] = dot(X[1], X[1]); - - real_t det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; - real_t Q[2][2]; - Q[0][0] = q[1][1] / det; - Q[0][1] = -q[0][1] / det; - Q[1][0] = -q[1][0] / det; - Q[1][1] = q[0][0] / det; - - real_t delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - real_t dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0]*t[0]*Q[0][0] + t[0]*t[1]*(Q[1][0] + Q[0][1]) + t[1]*t[1]*Q[1][1] - 1); - - real_t p; - - if(dis >= 0) - { - #ifdef GPROSHAN_FLOAT - p = delta + sqrtf(dis); - #else - p = delta + sqrt(dis); - #endif - p /= Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]; - } - - real_t tp[2]; - tp[0] = t[0] - p; - tp[1] = t[1] - p; - - vertex n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), - tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), - tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) - }; - - real_t cond[2]; - cond[0] = dot(X[0] , n); - cond[1] = dot(X[1] , n); - - real_t c[2]; - c[0] = cond[0] * Q[0][0] + cond[1] * Q[0][1]; - c[1] = cond[0] * Q[1][0] + cond[1] * Q[1][1]; - - if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) - { - real_t dp[2]; - dp[0] = dist[x[0]] + norm(X[0]); - dp[1] = dist[x[1]] + norm(X[1]); - - p = dp[dp[1] < dp[0]]; - } - - return p; -} - } // namespace gproshan diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 06560f51..025e1658 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -93,7 +93,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) for(const index_t & he: mesh->star(v)) { const vertex & nhe = mesh->normal_he(he); - const vertex & vhe = mesh->vertex_he(prev(he)) - mesh->vertex_he(next(he)); + const vertex & vhe = mesh->vertex_he(he_prev(he)) - mesh->vertex_he(he_next(he)); const vertex & ghe = mesh->gradient_he(he, u.memptr()); sum += dot(cross(nhe, vhe), -ghe); } diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index f5a06a7c..2bac0519 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -187,7 +187,7 @@ std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, co end = limits[j]; n_cond = limits[i + 1] - start; - relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_sorted, end, start); + relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index d577519b..1f468fd3 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -238,7 +238,7 @@ std::vector > iter_error_run_ptp_coalescence_gpu(CHE end = limits[j]; n_cond = limits[i + 1] - start; - relax_ptp_coalescence <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], end, start); + relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end); // print band info os << n_iter << " " << i << " " << j << " " << end - start << std::endl; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 777d3d5c..23f2f034 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -93,8 +93,8 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ for(const index_t & he: mesh->star(v)) { - a = mesh->halfedge(next(he)); //index of the next vertex index_t - b = mesh->halfedge(prev(he)); + a = mesh->halfedge(he_next(he)); //index of the next vertex index_t + b = mesh->halfedge(he_prev(he)); va = mesh->point(a); vb = mesh->point(b); vv = mesh->point(v); @@ -411,8 +411,8 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche for(const index_t & he: mesh->star(min_v)) { //discard triangles outside the patch - vpatches_t & ma = vpatches[mesh->halfedge(next(he))]; - vpatches_t & mb = vpatches[mesh->halfedge(prev(he))]; + vpatches_t & ma = vpatches[mesh->halfedge(he_next(he))]; + vpatches_t & mb = vpatches[mesh->halfedge(he_prev(he))]; if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) { @@ -632,8 +632,8 @@ void patch::normal_fit_directions(che * mesh, const index_t & v) vertex nz = mesh->normal(v); - vertex nx = mesh->vertex_he(next(mesh->evt(v))); -// GT[VT[next(EVT[v]]] + vertex nx = mesh->vertex_he(he_next(mesh->evt(v))); +// GT[VT[he_next(EVT[v]]] vertex c = mesh->point(v); vertex ny; nx = nx - c ; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 67ef8abf..bbba8771 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -112,8 +112,8 @@ vertex che::normal_trig(const index_t & f) const vertex che::normal_he(const index_t & he) const { const vertex & a = GT[VT[he]]; - const vertex & b = GT[VT[next(he)]]; - const vertex & c = GT[VT[prev(he)]]; + const vertex & b = GT[VT[he_next(he)]]; + const vertex & c = GT[VT[he_prev(he)]]; return normalize(cross(b - a, c - a)); } @@ -121,8 +121,8 @@ vertex che::normal_he(const index_t & he) const vertex che::gradient_he(const index_t & he, const real_t * f) const { index_t i = VT[he]; - index_t j = VT[next(he)]; - index_t k = VT[prev(he)]; + index_t j = VT[he_next(he)]; + index_t k = VT[he_prev(he)]; vertex xi = GT[i]; vertex xj = GT[j]; @@ -144,7 +144,7 @@ vertex che::gradient(const index_t & v, const real_t * f) for(const index_t & he: star(v)) { - area = area_trig(trig(he)); + area = area_trig(he_trig(he)); area_star += area; g += area * gradient_he(he, f); } @@ -332,7 +332,7 @@ void che::update_normals() n = 0; for(const index_t & he: star(v)) - n += area_trig(trig(he)) * normal_he(he); + n += area_trig(he_trig(he)) * normal_he(he); n /= norm(n); } @@ -406,12 +406,12 @@ void che::remove_vertices(const std::vector & vertices) for(const index_t & he: star(v)) { VT[he] = NIL; - VT[prev(he)] = NIL; - VT[next(he)] = NIL; + VT[he_prev(he)] = NIL; + VT[he_next(he)] = NIL; gproshan_debug_var(he); - gproshan_debug_var(next(he)); - gproshan_debug_var(prev(he)); + gproshan_debug_var(he_next(he)); + gproshan_debug_var(he_prev(he)); } gproshan_debug_var(EVT[v]); @@ -572,7 +572,7 @@ const index_t & che::edge_u(const index_t & e) const const index_t & che::edge_v(const index_t & e) const { assert(e < n_edges); - return VT[next(ET[e])]; + return VT[he_next(ET[e])]; } const index_t & che::edge_he_0(const index_t & e) const @@ -602,7 +602,7 @@ const vertex & che::vertex_edge_u(const index_t & e) const const vertex & che::vertex_edge_v(const index_t & e) const { assert(e < n_edges); - return GT[VT[next(ET[e])]]; + return GT[VT[he_next(ET[e])]]; } const index_t & che::evt(const index_t & v) const @@ -626,10 +626,10 @@ std::vector che::link(const index_t & v) const std::vector vlink; if(is_vertex_bound(v)) - vlink.push_back(VT[next(EVT[v])]); + vlink.push_back(VT[he_next(EVT[v])]); for(const index_t & he: star(v)) - vlink.push_back(VT[prev(he)]); + vlink.push_back(VT[he_prev(he)]); return vlink; } @@ -720,7 +720,7 @@ std::vector che::boundary(const index_t & v) const do { vbound.push_back(VT[he]); - he = EVT[VT[next(he)]]; + he = EVT[VT[he_next(he)]]; } while(he != NIL && he != he_end); @@ -809,7 +809,7 @@ real_t che::mean_edge() const #pragma omp parallel for reduction(+: m) for(index_t e = 0; e < n_edges; ++e) - m += norm(GT[VT[ET[e]]] - GT[VT[next(ET[e])]]); + m += norm(GT[VT[ET[e]]] - GT[VT[he_next(ET[e])]]); return m / n_edges; } @@ -853,58 +853,58 @@ void che::flip(const index_t & e) index_t va = VT[ha]; index_t vb = VT[hb]; - index_t vc = VT[prev(ha)]; - index_t vd = VT[prev(hb)]; + index_t vc = VT[he_prev(ha)]; + index_t vd = VT[he_prev(hb)]; - index_t et_pa = EHT[prev(ha)]; - index_t et_na = EHT[next(ha)]; - index_t et_pb = EHT[prev(hb)]; - index_t et_nb = EHT[next(hb)]; + index_t et_pa = EHT[he_prev(ha)]; + index_t et_na = EHT[he_next(ha)]; + index_t et_pb = EHT[he_prev(hb)]; + index_t et_nb = EHT[he_next(hb)]; - index_t ot_pa = OT[prev(ha)]; - index_t ot_na = OT[next(ha)]; - index_t ot_pb = OT[prev(hb)]; - index_t ot_nb = OT[next(hb)]; + index_t ot_pa = OT[he_prev(ha)]; + index_t ot_na = OT[he_next(ha)]; + index_t ot_pb = OT[he_prev(hb)]; + index_t ot_nb = OT[he_next(hb)]; - VT[prev(ha)] = vb; + VT[he_prev(ha)] = vb; VT[ha] = vc; - VT[next(ha)] = vd; - VT[prev(hb)] = va; + VT[he_next(ha)] = vd; + VT[he_prev(hb)] = va; VT[hb] = vd; - VT[next(hb)] = vc; + VT[he_next(hb)] = vc; - if(ot_pa != NIL) OT[ot_pa] = next(hb); - if(ot_na != NIL) OT[ot_na] = prev(ha); - if(ot_pb != NIL) OT[ot_pb] = next(ha); - if(ot_nb != NIL) OT[ot_nb] = prev(hb); + if(ot_pa != NIL) OT[ot_pa] = he_next(hb); + if(ot_na != NIL) OT[ot_na] = he_prev(ha); + if(ot_pb != NIL) OT[ot_pb] = he_next(ha); + if(ot_nb != NIL) OT[ot_nb] = he_prev(hb); - OT[prev(ha)] = ot_na; - OT[next(ha)] = ot_pb; - OT[prev(hb)] = ot_nb; - OT[next(hb)] = ot_pa; + OT[he_prev(ha)] = ot_na; + OT[he_next(ha)] = ot_pb; + OT[he_prev(hb)] = ot_nb; + OT[he_next(hb)] = ot_pa; - ET[et_pa] = prev(ha); - ET[et_na] = next(ha); - ET[et_pb] = prev(hb); - ET[et_nb] = next(hb); + ET[et_pa] = he_prev(ha); + ET[et_na] = he_next(ha); + ET[et_pb] = he_prev(hb); + ET[et_nb] = he_next(hb); - EHT[prev(ha)] = EHT[OT[prev(ha)]] = et_pa; - EHT[next(ha)] = EHT[OT[next(ha)]] = et_na; - EHT[prev(hb)] = EHT[OT[prev(hb)]] = et_pb; - EHT[next(hb)] = EHT[OT[next(hb)]] = et_nb; + EHT[he_prev(ha)] = EHT[OT[he_prev(ha)]] = et_pa; + EHT[he_next(ha)] = EHT[OT[he_next(ha)]] = et_na; + EHT[he_prev(hb)] = EHT[OT[he_prev(hb)]] = et_pb; + EHT[he_next(hb)] = EHT[OT[he_next(hb)]] = et_nb; - if(EVT[va] == next(hb) || EVT[va] == ha) EVT[va] = prev(hb); - if(EVT[vb] == next(ha) || EVT[vb] == hb) EVT[vb] = prev(ha); - if(EVT[vc] == prev(ha)) EVT[vc] = next(hb); - if(EVT[vd] == prev(hb)) EVT[vd] = next(ha); + if(EVT[va] == he_next(hb) || EVT[va] == ha) EVT[va] = he_prev(hb); + if(EVT[vb] == he_next(ha) || EVT[vb] == hb) EVT[vb] = he_prev(ha); + if(EVT[vc] == he_prev(ha)) EVT[vc] = he_next(hb); + if(EVT[vd] == he_prev(hb)) EVT[vd] = he_next(ha); } real_t che::cotan(const index_t & he) const { if(he == NIL) return 0; - vertex a = GT[VT[he]] - GT[VT[prev(he)]]; - vertex b = GT[VT[next(he)]] - GT[VT[prev(he)]]; + vertex a = GT[VT[he]] - GT[VT[he_prev(he)]]; + vertex b = GT[VT[he_next(he)]] - GT[VT[he_prev(he)]]; return dot(a, b) / norm(cross(a, b)); } @@ -917,9 +917,9 @@ real_t che::pdetriq(const index_t & t) const { index_t he = t * che::mtrig; real_t h[3] = { - norm(GT[VT[next(he)]] - GT[VT[he]]), - norm(GT[VT[prev(he)]] - GT[VT[next(he)]]), - norm(GT[VT[he]] - GT[VT[prev(he)]]) + norm(GT[VT[he_next(he)]] - GT[VT[he]]), + norm(GT[VT[he_prev(he)]] - GT[VT[he_next(he)]]), + norm(GT[VT[he]] - GT[VT[he_prev(he)]]) }; return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } @@ -927,8 +927,8 @@ real_t che::pdetriq(const index_t & t) const real_t che::area_trig(const index_t & t) const { index_t he = t * che::mtrig; - vertex a = GT[VT[next(he)]] - GT[VT[he]]; - vertex b = GT[VT[prev(he)]] - GT[VT[he]]; + vertex a = GT[VT[he_next(he)]] - GT[VT[he]]; + vertex b = GT[VT[he_prev(he)]] - GT[VT[he]]; return norm(cross(a, b)) / 2; } @@ -937,7 +937,7 @@ real_t che::area_vertex(const index_t & v) const { real_t area_star = 0; for(const index_t & he: star(v)) - area_star += area_trig(trig(he)); + area_star += area_trig(he_trig(he)); return area_star / 3; } @@ -950,8 +950,8 @@ real_t che::mean_curvature(const index_t & v) const for(const index_t & he: star(v)) { - a += area_trig(trig(he)); - h += norm(GT[VT[next(he)]] - GT[v]) * dot(normal(v), normal_he(he)); + a += area_trig(he_trig(he)); + h += norm(GT[VT[he_next(he)]] - GT[v]) * dot(normal(v), normal_he(he)); } return 0.75 * h / a; @@ -1042,7 +1042,7 @@ void che::update_evt_ot_et() for(index_t ohe, he = 0; he < n_half_edges; ++he) { const index_t & u = VT[he]; - const index_t & v = VT[next(he)]; + const index_t & v = VT[he_next(he)]; EVT[u] = he; @@ -1052,7 +1052,7 @@ void che::update_evt_ot_et() for(index_t j = 0; j < vnhe[v]; ++j) { index_t & h = vhe[v][j]; - if(VT[next(h)] == u) + if(VT[he_next(h)] == u) { ohe = h; break; @@ -1138,7 +1138,7 @@ che::star_he::iterator::iterator(const che * p_mesh, const index_t & p_he, const che::star_he::iterator & che::star_he::iterator::operator ++ () { - he = mesh->OT[prev(he)]; + he = mesh->OT[he_prev(he)]; he = he != he_end ? he : NIL; return *this; } diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 33af8426..592cc91a 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -5,28 +5,6 @@ namespace gproshan { -__host__ __device__ -index_t cu_trig(index_t he) -{ - if(he == NIL) return NIL; - return he / che::mtrig; -} - -__host__ __device__ -index_t cu_next(index_t he) -{ - if(he == NIL) return NIL; - return che::mtrig * cu_trig(he) + (he + 1) % che::mtrig; -} - -__host__ __device__ -index_t cu_prev(index_t he) -{ - if(he == NIL) return NIL; - return che::mtrig * cu_trig(he) + (he + che::mtrig - 1) % che::mtrig; -} - - void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal, const bool & color) { dd_che = new CHE; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 26e8d12c..386f50e6 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -35,7 +35,7 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert { v = mesh->point(b); normal_v = mesh->normal(b); - edge_v = mesh->vertex_he(next(mesh->evt(b))) - v; + edge_v = mesh->vertex_he(he_next(mesh->evt(b))) - v; edge_v -= dot(normal_v, edge_v) * normal_v; E(0, 2) = normal_v.x(); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index ea4c3a78..0792f53d 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -101,7 +101,7 @@ void che_ptx::read_file(const std::string & file) VT[he++] = j; VT[he++] = k; - if(pdetriq(trig(he - 1)) < 0.1) + if(pdetriq(he_trig(he - 1)) < 0.1) he -= 3; }; From 37bdacbf08565fb26bcd6129af40270e61b54a7c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Dec 2022 12:39:52 +0100 Subject: [PATCH 0775/1018] geodesics: updating ptp --- include/gproshan/geodesics/geodesics_ptp.h | 10 ++++++ src/gproshan/geodesics/geodesics.cpp | 7 ++-- src/gproshan/geodesics/geodesics_ptp.cpp | 41 ++-------------------- src/gproshan/mesh/che.cpp | 5 +-- 4 files changed, 16 insertions(+), 47 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 9147845b..91b8421d 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -44,6 +44,9 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample void normalize_ptp(real_t * dist, const size_t & n); template +#ifdef __CUDACC__ +__forceinline__ +#endif __host__ __device__ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) { @@ -72,7 +75,11 @@ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); +#ifdef GPROSHAN_FLOAT + T p = (delta + sqrtf(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); +#else T p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); +#endif const vec & tp = t - p; const vec & n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), @@ -92,6 +99,9 @@ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) } template +#ifdef __CUDACC__ +__forceinline__ +#endif __host__ __device__ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 8c4fcda3..0cb813fc 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -41,7 +41,6 @@ geodesics::~geodesics() const real_t & geodesics::operator[](const index_t & i) const { - assert(i < n_vertices); return dist[i]; } @@ -229,9 +228,9 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::ve mesh->compute_toplesets(toplesets, sorted_index, limits, sources); double time_ptp; - if(sources.size() > 1) - time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); - else + //if(sources.size() > 1) + // time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); + //else time_ptp = parallel_toplesets_propagation_coalescence_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 464bfc00..23dfe43c 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -76,25 +76,7 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c #pragma omp parallel for for(index_t v = start; v < end; ++v) - { - pdist[!d][v] = pdist[d][v]; - - real_t p; - for(const index_t & he: mesh->star(v)) - { - p = update_step(&cmesh, pdist[d], { mesh->halfedge(he_next(he)), - mesh->halfedge(he_prev(he)), - mesh->halfedge(he) - }); - if(p < pdist[!d][v]) - { - pdist[!d][v] = p; - - if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(he_prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(he_prev(he))] : ptp_out.clusters[mesh->halfedge(he_next(he))]; - } - } - } + relax_ptp(&cmesh, pdist[!d], pdist[d], ptp_out.clusters, ptp_out.clusters, v); #pragma omp parallel for for(index_t v = start; v < start + n_cond; ++v) @@ -156,26 +138,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c #pragma omp parallel for for(index_t vi = start; vi < end; ++vi) - { - const index_t & v = toplesets.index[vi]; - pdist[!d][v] = pdist[d][v]; - - real_t p; - for(const index_t & he: mesh->star(v)) - { - p = update_step(&cmesh, pdist[d], { mesh->halfedge(he_next(he)), - mesh->halfedge(he_prev(he)), - mesh->halfedge(he) - }); - if(p < pdist[!d][v]) - { - pdist[!d][v] = p; - - if(ptp_out.clusters) - ptp_out.clusters[v] = ptp_out.clusters[mesh->halfedge(he_prev(he))] != NIL ? ptp_out.clusters[mesh->halfedge(he_prev(he))] : ptp_out.clusters[mesh->halfedge(he_next(he))]; - } - } - } + relax_ptp(&cmesh, pdist[!d], pdist[d], ptp_out.clusters, ptp_out.clusters, toplesets.index[vi]); #pragma omp parallel for for(index_t vi = start; vi < start + n_cond; ++vi) diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index bbba8771..302f8a00 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -651,9 +651,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector for(const index_t & s: sources) { sorted[p++] = s; - - if(toplesets[s] == NIL) - toplesets[s] = level; + toplesets[s] = level; } limits.push_back(0); @@ -664,7 +662,6 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector if(toplesets[v] > level) { if(++level > k) break; - limits.push_back(i); } From 406b5c21c6a5afe8b62cb925f4b7c24ef56be86f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 1 Dec 2022 16:32:48 +0100 Subject: [PATCH 0776/1018] geodesics: refactoring ptp code ply: reading color vertex ascii by line --- include/gproshan/geodesics/geodesics_ptp.h | 2 +- .../geodesics/geodesics_ptp_coalescence.cuh | 2 +- src/gproshan/geodesics/geodesics.cpp | 6 +- .../geodesics/geodesics_ptp_coalescence.cu | 132 +++++++++++++----- src/gproshan/geodesics/test_geodesics_ptp.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 31 ++-- 6 files changed, 115 insertions(+), 60 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 91b8421d..453bc0bf 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -31,7 +31,7 @@ struct toplesets_t che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets); -double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf = 1); +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); diff --git a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh index 05a514c3..149209e5 100644 --- a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh @@ -10,7 +10,7 @@ namespace gproshan { -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); +index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr, const index_t * d_sorted = nullptr); } // namespace gproshan diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 0cb813fc..a4030b6a 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -227,11 +227,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::ve std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); - double time_ptp; - //if(sources.size() > 1) - // time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); - //else - time_ptp = parallel_toplesets_propagation_coalescence_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); + double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}, sources.size() == 1); gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu index 2a31ef32..bc547e8d 100644 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu @@ -16,12 +16,37 @@ namespace gproshan { -double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & set_inf) +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) { + CHE h_mesh(mesh); + index_t * inv = nullptr; - che * coalescence_mesh = ptp_coalescence(inv, mesh, toplesets); + if(coalescence) + { + inv = new index_t[mesh->n_vertices]; + h_mesh.GT = new vertex[mesh->n_vertices]; + h_mesh.VT = new index_t[mesh->n_half_edges]; + h_mesh.EVT = new index_t[mesh->n_vertices]; - // ------------------------------------------------------ + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + { + h_mesh.GT[i] = mesh->point(toplesets.index[i]); + inv[toplesets.index[i]] = i; + } + + #pragma omp parallel for + for(index_t he = 0; he < mesh->n_half_edges; ++he) + { + const index_t & v = mesh->halfedge(he); + if(v != NIL) + { + h_mesh.VT[he] = inv[v]; + if(mesh->evt(v) == he) + h_mesh.EVT[inv[v]] = he; + } + } + } cudaDeviceReset(); @@ -31,68 +56,93 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, cudaEventCreate(&stop); cudaEventRecord(start, 0); - // BEGIN PTP - CHE * h_mesh = new CHE(coalescence_mesh); CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); + cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); - real_t * h_dist = new real_t[h_mesh->n_vertices]; + real_t * h_dist = coalescence ? new real_t[h_mesh.n_vertices] : ptp_out.dist; if(set_inf) { #pragma omp parallel for - for(index_t v = 0; v < h_mesh->n_vertices; ++v) + for(index_t v = 0; v < h_mesh.n_vertices; ++v) h_dist[v] = INFINITY; } real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh.n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh.n_vertices); + + index_t * d_sorted = nullptr; + if(!coalescence) + { + cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh.n_vertices); + cudaMemcpy(d_sorted, toplesets.index, sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyHostToDevice); + } real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_error, sizeof(real_t) * h_mesh.n_vertices); - index_t d; - if(ptp_out.clusters) + + index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[h_mesh.n_vertices] + : ptp_out.clusters; + + index_t * d_clusters[2] = {nullptr, nullptr}; + + if(h_clusters) { - index_t * h_clusters = new index_t[h_mesh->n_vertices]; - index_t * d_clusters[2] = {nullptr, nullptr}; + cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh.n_vertices); + cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh.n_vertices); + } - cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh->n_vertices); - cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh->n_vertices); + index_t d = run_ptp_coalescence_gpu(d_mesh, h_mesh.n_vertices, h_dist, d_dist, sources, {toplesets.limits, coalescence ? inv : toplesets.index}, d_error, h_clusters, d_clusters, d_sorted); - d = run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, {toplesets.limits, inv}, d_error, h_clusters, d_clusters); - cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); - #pragma omp parallel for - for(index_t i = 0; i < h_mesh->n_vertices; ++i) - ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; + if(h_clusters) + { + cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); + + if(coalescence) + { + #pragma omp parallel for + for(index_t i = 0; i < h_mesh.n_vertices; ++i) + ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; + + delete [] h_clusters; + } cudaFree(d_clusters[0]); cudaFree(d_clusters[1]); - - delete [] h_clusters; } - else d = run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, {toplesets.limits, inv}, d_error); - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); cuda_free_CHE(dd_mesh, d_mesh); - delete coalescence_mesh; - delete [] inv; + if(coalescence) + { + delete [] h_mesh.GT; + delete [] h_mesh.VT; + delete [] h_mesh.EVT; + } + else + { + cudaFree(d_sorted); + } - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - ptp_out.dist[toplesets.index[i]] = h_dist[i]; + delete [] inv; - delete [] h_dist; + if(coalescence) + { + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + ptp_out.dist[toplesets.index[i]] = h_dist[i]; - // END PTP + delete [] h_dist; + } cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -104,18 +154,24 @@ double parallel_toplesets_propagation_coalescence_gpu(const ptp_out_t & ptp_out, return time / 1000; } -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) +index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters, const index_t * d_sorted) { - for(index_t i = 0; i < sources.size(); ++i) - h_dist[inv.index[sources[i]]] = 0; + if(d_sorted) + { + for(index_t i = 0; i < sources.size(); ++i) + h_dist[inv.index[sources[i]]] = 0; + } + else + { + for(index_t i = 0; i < sources.size(); ++i) + h_dist[sources[i]] = 0; + } cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); if(h_clusters) { - assert(d_clusters[0]); - for(index_t i = 0; i < sources.size(); ++i) h_clusters[inv.index[sources[i]]] = i + 1; diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index efa15777..e77bdea1 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -302,7 +302,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std: real_t * dist = new real_t[mesh->n_vertices]; for(int i = 0; i < n_test; ++i) { - t = parallel_toplesets_propagation_coalescence_gpu(dist, mesh, source, toplesets); + t = parallel_toplesets_propagation_gpu(dist, mesh, source, toplesets); seconds = std::min(seconds, t); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index e7c94b6b..c8a290cc 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include // geometry processing and shape analysis framework @@ -17,18 +17,18 @@ che_ply::che_ply(const std::string & file) void che_ply::read_file(const std::string & file) { - std::map bytes = { - {"char", 1}, - {"uchar", 1}, - {"short", 2}, - {"ushort", 2}, - {"int", 4}, - {"uint", 4}, - {"float", 4}, - {"float32", 4}, - {"float64", 8}, - {"double", 8} - }; + std::unordered_map bytes = { + {"char", 1}, + {"uchar", 1}, + {"short", 2}, + {"ushort", 2}, + {"int", 4}, + {"uint", 4}, + {"float", 4}, + {"float32", 4}, + {"float64", 8}, + {"double", 8} + }; FILE * fp = fopen(file.c_str(), "rb"); assert(fp); @@ -97,9 +97,12 @@ void che_ply::read_file(const std::string & file) if(format[0] == 'a') // ascii { float x, y, z; + unsigned char r, g, b; for(index_t v = 0; v < n_vertices; ++v) { - fscanf(fp, "%f %f %f", &x, &y, &z); + fgets(line, sizeof(line), fp); + if(sscanf(line, "%f %f %f %hhu %hhu %hhu", &x, &y, &z, &r, &g, &b) > 5) + VC[v] = {r, g, b}; GT[v] = {x, y, z}; } From 023074801e87d59cffe982f067c846b469637dc2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Dec 2022 15:06:22 +0100 Subject: [PATCH 0777/1018] geodesics: refactoring ptp gpu --- include/gproshan/geodesics/geodesics_ptp.cuh | 5 +- include/gproshan/geodesics/geodesics_ptp.h | 2 - .../geodesics/geodesics_ptp_coalescence.cuh | 19 -- src/gproshan/geodesics/geodesics_ptp.cu | 256 ++++++++++++------ .../geodesics/geodesics_ptp_coalescence.cu | 219 --------------- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 +- .../test_geodesics_ptp_coalescence.cu | 4 +- 7 files changed, 178 insertions(+), 329 deletions(-) delete mode 100644 include/gproshan/geodesics/geodesics_ptp_coalescence.cuh delete mode 100644 src/gproshan/geodesics/geodesics_ptp_coalescence.cu diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh index 9d874c07..235b2bc1 100644 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ b/include/gproshan/geodesics/geodesics_ptp.cuh @@ -2,6 +2,7 @@ #define GEODESICS_PTP_CUH #include +#include #define NT 64 @@ -12,7 +13,9 @@ namespace gproshan { -index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr); +index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, const index_t & n_vertices, + real_t * h_dist, real_t ** d_dist, const toplesets_t & inv, real_t * d_error, + index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr, index_t * d_sorted = nullptr); __global__ void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 453bc0bf..35593c24 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -33,8 +33,6 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); - void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); diff --git a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh deleted file mode 100644 index 149209e5..00000000 --- a/include/gproshan/geodesics/geodesics_ptp_coalescence.cuh +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef GEODESICS_PTP_COALESCENCE_CUH -#define GEODESICS_PTP_COALESCENCE_CUH - -#include -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr, const index_t * d_sorted = nullptr); - - -} // namespace gproshan - -#endif // GEODESICS_PTP_COALESCENCE_CUH - diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index b46437b3..54f51e6e 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -15,8 +15,38 @@ namespace gproshan { -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) { + CHE h_mesh(mesh); + + index_t * inv = nullptr; + if(coalescence) + { + inv = new index_t[mesh->n_vertices]; + h_mesh.GT = new vertex[mesh->n_vertices]; + h_mesh.VT = new index_t[mesh->n_half_edges]; + h_mesh.EVT = new index_t[mesh->n_vertices]; + + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + { + h_mesh.GT[i] = mesh->point(toplesets.index[i]); + inv[toplesets.index[i]] = i; + } + + #pragma omp parallel for + for(index_t he = 0; he < mesh->n_half_edges; ++he) + { + const index_t & v = mesh->halfedge(he); + if(v != NIL) + { + h_mesh.VT[he] = inv[v]; + if(mesh->evt(v) == he) + h_mesh.EVT[inv[v]] = he; + } + } + } + cudaDeviceReset(); float time; @@ -25,52 +55,92 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, cudaEventCreate(&stop); cudaEventRecord(start, 0); - // BEGIN PTP - CHE * h_mesh = new CHE(mesh); CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); + cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); + + real_t * h_dist = coalescence ? new real_t[h_mesh.n_vertices] : ptp_out.dist; - real_t * h_dist = ptp_out.dist; + if(set_inf) + { + #pragma omp parallel for + for(index_t v = 0; v < h_mesh.n_vertices; ++v) + h_dist[v] = INFINITY; + } real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh.n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh.n_vertices); - index_t * d_sorted; - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); + index_t * d_sorted = nullptr; + if(!coalescence) + { + cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh.n_vertices); + } real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_error, sizeof(real_t) * h_mesh.n_vertices); - index_t d; - if(ptp_out.clusters) + + index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[h_mesh.n_vertices] + : ptp_out.clusters; + + index_t * d_clusters[2] = {nullptr, nullptr}; + + if(h_clusters) + { + cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh.n_vertices); + cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh.n_vertices); + } + + index_t d = run_ptp_gpu(d_mesh, sources, h_mesh.n_vertices, h_dist, d_dist, {toplesets.limits, coalescence ? inv : toplesets.index}, d_error, h_clusters, d_clusters, d_sorted); + + + if(h_clusters) { - index_t * h_clusters = ptp_out.clusters; - index_t * d_clusters[2] = {nullptr, nullptr}; - cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh->n_vertices); - cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh->n_vertices); + cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); - d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, toplesets.limits, toplesets.index, d_sorted, d_error, h_clusters, d_clusters); - cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); + if(coalescence) + { + #pragma omp parallel for + for(index_t i = 0; i < h_mesh.n_vertices; ++i) + ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; + + delete [] h_clusters; + } cudaFree(d_clusters[0]); cudaFree(d_clusters[1]); } - else - { - d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, toplesets.limits, toplesets.index, d_sorted, d_error); - } - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh->n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); - cudaFree(d_sorted); cuda_free_CHE(dd_mesh, d_mesh); - // END PTP + if(coalescence) + { + delete [] h_mesh.GT; + delete [] h_mesh.VT; + delete [] h_mesh.EVT; + } + else + { + cudaFree(d_sorted); + } + + delete [] inv; + + if(coalescence) + { + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + ptp_out.dist[toplesets.index[i]] = h_dist[i]; + + delete [] h_dist; + } cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -82,6 +152,77 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, che * mesh, return time / 1000; } +index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, const index_t & n_vertices, + real_t * h_dist, real_t ** d_dist, const toplesets_t & inv, real_t * d_error, + index_t * h_clusters, index_t ** d_clusters, index_t * d_sorted) +{ + if(d_sorted) + { + cudaMemcpy(d_sorted, inv.index, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + for(index_t i = 0; i < sources.size(); ++i) + h_dist[sources[i]] = 0; + } + else + { + for(index_t i = 0; i < sources.size(); ++i) + h_dist[inv.index[sources[i]]] = 0; + } + + cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + + if(h_clusters) + { + if(d_sorted) + { + for(index_t i = 0; i < sources.size(); ++i) + h_clusters[sources[i]] = i + 1; + } + else + { + for(index_t i = 0; i < sources.size(); ++i) + h_clusters[inv.index[sources[i]]] = i + 1; + } + + cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(d_clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + } + + index_t d = 0; + index_t start, end, n_cond; + index_t i = 1, j = 2; + + // maximum number of iterations + index_t iter = 0; + index_t max_iter = inv.limits.size() << 1; + + while(i < j && iter++ < max_iter) + { + if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size + + start = inv.limits[i]; + end = inv.limits[j]; + n_cond = inv.limits[i + 1] - start; + + h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) + : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); + + cudaDeviceSynchronize(); + + relative_error <<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); + cudaDeviceSynchronize(); + + if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) + ++i; + + if(j < inv.limits.size() - 1) ++j; + + d = !d; + } + + return d; +} + real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { cudaDeviceReset(); @@ -99,6 +240,10 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample cuda_create_CHE(h_mesh, dd_mesh, d_mesh); real_t * h_dist = new real_t[h_mesh->n_vertices]; + #pragma omp parallel for + for(index_t v = 0; v < h_mesh->n_vertices; ++v) + h_dist[v] = INFINITY; + real_t * d_dist[2]; cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); @@ -130,7 +275,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, samples, limits, sorted_index, d_sorted, d_error); + d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT @@ -169,65 +314,6 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample return max_dist; } -index_t run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, real_t * d_error, index_t * h_clusters, index_t ** d_clusters) -{ - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - h_dist[v] = INFINITY; - - for(index_t i = 0; i < sources.size(); ++i) - h_dist[sources[i]] = 0; - - cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - - if(h_clusters) - { - assert(d_clusters); - - for(index_t i = 0; i < sources.size(); ++i) - h_clusters[sources[i]] = i + 1; - - cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - } - - index_t d = 0; - index_t start, end, n_cond; - index_t i = 1, j = 2; - - // maximum number of iterations - index_t iter = 0; - index_t max_iter = limits.size() << 1; - - while(i < j && iter++ < max_iter) - { - if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - - start = limits[i]; - end = limits[j]; - n_cond = limits[i + 1] - start; - - h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) - : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); - - cudaDeviceSynchronize(); - - relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond, d_sorted); - cudaDeviceSynchronize(); - - if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - ++i; - if(j < limits.size() - 1) ++j; - - d = !d; - } - - return d; -} - - __global__ void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) { diff --git a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/geodesics_ptp_coalescence.cu deleted file mode 100644 index bc547e8d..00000000 --- a/src/gproshan/geodesics/geodesics_ptp_coalescence.cu +++ /dev/null @@ -1,219 +0,0 @@ -#include - -#include - -#include -#include -#include -#include - -#include -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) -{ - CHE h_mesh(mesh); - - index_t * inv = nullptr; - if(coalescence) - { - inv = new index_t[mesh->n_vertices]; - h_mesh.GT = new vertex[mesh->n_vertices]; - h_mesh.VT = new index_t[mesh->n_half_edges]; - h_mesh.EVT = new index_t[mesh->n_vertices]; - - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - { - h_mesh.GT[i] = mesh->point(toplesets.index[i]); - inv[toplesets.index[i]] = i; - } - - #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; ++he) - { - const index_t & v = mesh->halfedge(he); - if(v != NIL) - { - h_mesh.VT[he] = inv[v]; - if(mesh->evt(v) == he) - h_mesh.EVT[inv[v]] = he; - } - } - } - - cudaDeviceReset(); - - float time; - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - cudaEventRecord(start, 0); - - - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); - - real_t * h_dist = coalescence ? new real_t[h_mesh.n_vertices] : ptp_out.dist; - - if(set_inf) - { - #pragma omp parallel for - for(index_t v = 0; v < h_mesh.n_vertices; ++v) - h_dist[v] = INFINITY; - } - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh.n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh.n_vertices); - - index_t * d_sorted = nullptr; - if(!coalescence) - { - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh.n_vertices); - cudaMemcpy(d_sorted, toplesets.index, sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyHostToDevice); - } - - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh.n_vertices); - - - index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[h_mesh.n_vertices] - : ptp_out.clusters; - - index_t * d_clusters[2] = {nullptr, nullptr}; - - if(h_clusters) - { - cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh.n_vertices); - cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh.n_vertices); - } - - index_t d = run_ptp_coalescence_gpu(d_mesh, h_mesh.n_vertices, h_dist, d_dist, sources, {toplesets.limits, coalescence ? inv : toplesets.index}, d_error, h_clusters, d_clusters, d_sorted); - - - if(h_clusters) - { - cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); - - if(coalescence) - { - #pragma omp parallel for - for(index_t i = 0; i < h_mesh.n_vertices; ++i) - ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; - - delete [] h_clusters; - } - - cudaFree(d_clusters[0]); - cudaFree(d_clusters[1]); - } - - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); - - cudaFree(d_error); - cudaFree(d_dist[0]); - cudaFree(d_dist[1]); - cuda_free_CHE(dd_mesh, d_mesh); - - if(coalescence) - { - delete [] h_mesh.GT; - delete [] h_mesh.VT; - delete [] h_mesh.EVT; - } - else - { - cudaFree(d_sorted); - } - - delete [] inv; - - if(coalescence) - { - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - ptp_out.dist[toplesets.index[i]] = h_dist[i]; - - delete [] h_dist; - } - - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return time / 1000; -} - -index_t run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters, const index_t * d_sorted) -{ - if(d_sorted) - { - for(index_t i = 0; i < sources.size(); ++i) - h_dist[inv.index[sources[i]]] = 0; - } - else - { - for(index_t i = 0; i < sources.size(); ++i) - h_dist[sources[i]] = 0; - } - - cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - - if(h_clusters) - { - for(index_t i = 0; i < sources.size(); ++i) - h_clusters[inv.index[sources[i]]] = i + 1; - - cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - } - - index_t d = 0; - index_t start, end, n_cond; - index_t i = 1, j = 2; - - // maximum number of iterations - index_t iter = 0; - index_t max_iter = inv.limits.size() << 1; - - while(i < j && iter++ < max_iter) - { - if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - - start = inv.limits[i]; - end = inv.limits[j]; - n_cond = inv.limits[i + 1] - start; - - h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end) - : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end); - - cudaDeviceSynchronize(); - - relative_error <<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); - cudaDeviceSynchronize(); - - if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - ++i; - - if(j < inv.limits.size() - 1) ++j; - - d = !d; - } - - return d; -} - - -} // namespace gproshan - diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 2bac0519..15c06828 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -118,7 +118,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - d = run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, samples, limits, sorted_index, d_sorted, d_error); + d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 1f468fd3..5da1741e 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -1,7 +1,7 @@ #include -#include #include +#include #include #include @@ -163,7 +163,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect cuda_create_CHE(h_mesh, dd_mesh, d_mesh); // exec algorithm - d = run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, samples, {limits, inv}, d_error); + d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, inv}, d_error); // free memory cuda_free_CHE(dd_mesh, d_mesh); From ce7a2f237497136b1a1b676e00f30fe74daddf8e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Dec 2022 15:46:53 +0100 Subject: [PATCH 0778/1018] geodesics (#6): updated ptp gpu code --- include/gproshan/geodesics/geodesics_ptp.h | 3 ++ src/gproshan/geodesics/geodesics_ptp.cu | 35 ++++++++++++---------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 35593c24..65e33d27 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -41,6 +41,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample void normalize_ptp(real_t * dist, const size_t & n); + template #ifdef __CUDACC__ __forceinline__ @@ -96,6 +97,7 @@ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) return p; } + template #ifdef __CUDACC__ __forceinline__ @@ -104,6 +106,7 @@ __host__ __device__ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { real_t & ndv = new_dist[v] = old_dist[v]; + if(new_clusters) new_clusters[v] = old_clusters[v]; real_t d; for_star(he, mesh, v) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 54f51e6e..83a407d9 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -85,7 +85,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[h_mesh.n_vertices] : ptp_out.clusters; - index_t * d_clusters[2] = {nullptr, nullptr}; + index_t * d_clusters[2] = {}; if(h_clusters) { @@ -93,8 +93,22 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh.n_vertices); } - index_t d = run_ptp_gpu(d_mesh, sources, h_mesh.n_vertices, h_dist, d_dist, {toplesets.limits, coalescence ? inv : toplesets.index}, d_error, h_clusters, d_clusters, d_sorted); + const index_t & d = run_ptp_gpu(d_mesh, sources, h_mesh.n_vertices, + h_dist, d_dist, + {toplesets.limits, coalescence ? inv : toplesets.index}, + d_error, + h_clusters, d_clusters, + d_sorted); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); + if(coalescence) + { + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + ptp_out.dist[toplesets.index[i]] = h_dist[i]; + + delete [] h_dist; + } if(h_clusters) { @@ -113,8 +127,6 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * cudaFree(d_clusters[1]); } - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); - cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); @@ -133,15 +145,6 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * delete [] inv; - if(coalescence) - { - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - ptp_out.dist[toplesets.index[i]] = h_dist[i]; - - delete [] h_dist; - } - cudaEventRecord(stop, 0); cudaEventSynchronize(stop); cudaEventElapsedTime(&time, start, stop); @@ -204,12 +207,12 @@ index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, co end = inv.limits[j]; n_cond = inv.limits[i + 1] - start; - h_clusters ? relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) - : relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); + h_clusters ? relax_ptp<<< NB(end - start), NT >>>(d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) + : relax_ptp<<< NB(end - start), NT >>>(d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); cudaDeviceSynchronize(); - relative_error <<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); + relative_error<<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); cudaDeviceSynchronize(); if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) From c5496a74d088ca263d3cb069e32a3e2f724de3f7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Dec 2022 16:40:51 +0100 Subject: [PATCH 0779/1018] geodesics (#16): updating ptp gpu cpu --- include/gproshan/geodesics/geodesics_ptp.h | 4 +- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 161 ++++++++------------- src/gproshan/geodesics/geodesics_ptp.cu | 46 +++--- 4 files changed, 82 insertions(+), 131 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 65e33d27..8037b1bc 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -33,9 +33,7 @@ che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & top double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); -void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); - -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets); +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = false, const bool & set_inf = true); real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index a4030b6a..cac0d921 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -199,7 +199,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const std::ve double time_ptp; TIC(time_ptp) - parallel_toplesets_propagation_coalescence_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}); + parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}, sources.size() == 1); TOC(time_ptp) gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 23dfe43c..d7b3419c 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -11,51 +11,64 @@ namespace gproshan { ptp_out_t::ptp_out_t(real_t *const & d, index_t *const & c): dist(d), clusters(c) {} -che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets) -{ - // sort data by levels, must be improve the coalescence - - std::vector V(toplesets.limits.back()); - std::vector F; - F.reserve(mesh->n_half_edges); - inv = !inv ? new index_t[mesh->n_vertices] : inv; - memset(inv, -1, sizeof(index_t) * mesh->n_vertices); +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) +{ + CHE h_mesh(mesh); + const size_t & n_vertices = h_mesh.n_vertices; - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) + index_t * inv = nullptr; + if(coalescence) { - V[i] = mesh->point(toplesets.index[i]); - inv[toplesets.index[i]] = i; - } - - for(index_t he = 0; he < mesh->n_half_edges; ++he) - if(inv[mesh->halfedge(he)] != NIL && inv[mesh->halfedge(he_prev(he))] != NIL && inv[mesh->halfedge(he_next(he))] != NIL) - F.push_back(inv[mesh->halfedge(he)]); - - return new che(V.data(), toplesets.limits.back(), F.data(), F.size() / che::mtrig); -} + inv = new index_t[n_vertices]; + h_mesh.GT = new vertex[n_vertices]; + h_mesh.EVT = new index_t[n_vertices]; + h_mesh.VT = new index_t[h_mesh.n_half_edges]; -void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) -{ - const size_t n_vertices = mesh->n_vertices; + #pragma omp parallel for + for(index_t i = 0; i < toplesets.limits.back(); ++i) + { + h_mesh.GT[i] = mesh->point(toplesets.index[i]); + inv[toplesets.index[i]] = i; + } - index_t * inv = nullptr; - mesh = ptp_coalescence(inv, mesh, toplesets); - CHE cmesh(mesh); + #pragma omp parallel for + for(index_t he = 0; he < mesh->n_half_edges; ++he) + { + const index_t & v = mesh->halfedge(he); + if(v != NIL) + { + h_mesh.VT[he] = inv[v]; + if(mesh->evt(v) == he) + h_mesh.EVT[inv[v]] = he; + } + } + } - // ------------------------------------------------------ - real_t * pdist[2] = {new real_t[mesh->n_vertices], new real_t[mesh->n_vertices]}; - real_t * error = new real_t[mesh->n_vertices]; + real_t * error = new real_t[n_vertices]; + real_t * dist[2] = { coalescence ? new real_t[n_vertices] : ptp_out.dist, + new real_t[n_vertices] + }; + index_t * clusters[2] = { coalescence && ptp_out.clusters ? new index_t[n_vertices] : ptp_out.clusters, + ptp_out.clusters ? new index_t[n_vertices] : nullptr + }; - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - pdist[0][v] = pdist[1][v] = INFINITY; + if(set_inf) + { + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + dist[0][v] = dist[1][v] = INFINITY; + } for(index_t i = 0; i < sources.size(); ++i) { - pdist[0][inv[sources[i]]] = pdist[1][inv[sources[i]]] = 0; - if(ptp_out.clusters) ptp_out.clusters[inv[sources[i]]] = i + 1; + const index_t & s = sources[i]; + const index_t & v = inv ? inv[s] : s; + + dist[0][v] = dist[1][v] = 0; + + if(ptp_out.clusters) + clusters[0][inv[s]] = clusters[0][inv[s]] = i + 1; } index_t d = 0; @@ -76,11 +89,11 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c #pragma omp parallel for for(index_t v = start; v < end; ++v) - relax_ptp(&cmesh, pdist[!d], pdist[d], ptp_out.clusters, ptp_out.clusters, v); + relax_ptp(&h_mesh, dist[!d], dist[d], clusters[!d], clusters[d], inv ? inv[v] : v); #pragma omp parallel for for(index_t v = start; v < start + n_cond; ++v) - error[v] = abs(pdist[!d][v] - pdist[d][v]) / pdist[d][v]; + error[v] = abs(dist[!d][v] - dist[d][v]) / dist[d][v]; count = 0; #pragma omp parallel for reduction(+: count) @@ -95,77 +108,21 @@ void parallel_toplesets_propagation_coalescence_cpu(const ptp_out_t & ptp_out, c #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.dist[v] = inv[v] != NIL ? pdist[!d][inv[v]] : INFINITY; + dist[!d][v] = dist[d][v]; - delete [] error; - delete [] pdist[0]; - delete [] pdist[1]; - delete [] inv; - delete mesh; -} - -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets) -{ - CHE cmesh(mesh); - real_t * pdist[2] = {ptp_out.dist, new real_t[mesh->n_vertices]}; - real_t * error = new real_t[mesh->n_vertices]; - - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - pdist[0][v] = pdist[1][v] = INFINITY; - - for(index_t i = 0; i < sources.size(); ++i) + if(inv) { - pdist[0][sources[i]] = pdist[1][sources[i]] = 0; - if(ptp_out.clusters) ptp_out.clusters[sources[i]] = i + 1; - } - - index_t d = 0; - index_t start, end, n_cond, count; - index_t i = 1, j = 2; - - // maximum number of iterations - index_t iter = 0; - index_t max_iter = toplesets.limits.size() << 1; - - while(i < j && iter++ < max_iter) - { - if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - - start = toplesets.limits[i]; - end = toplesets.limits[j]; - n_cond = toplesets.limits[i + 1] - start; - #pragma omp parallel for - for(index_t vi = start; vi < end; ++vi) - relax_ptp(&cmesh, pdist[!d], pdist[d], ptp_out.clusters, ptp_out.clusters, toplesets.index[vi]); - - #pragma omp parallel for - for(index_t vi = start; vi < start + n_cond; ++vi) - { - const index_t & v = toplesets.index[vi]; - error[vi] = abs(pdist[!d][v] - pdist[d][v]) / pdist[d][v]; - } - - count = 0; - #pragma omp parallel for reduction(+: count) - for(index_t vi = start; vi < start + n_cond; ++vi) - count += error[vi] < PTP_TOL; - - if(n_cond == count) ++i; - if(j < toplesets.limits.size() - 1) ++j; - - d = !d; + for(index_t v = 0; v < n_vertices; ++v) + dist[!d][v] = dist[d][inv[v]]; } delete [] error; - - if(ptp_out.dist != pdist[!d]) - { - memcpy(ptp_out.dist, pdist[!d], mesh->n_vertices * sizeof(real_t)); - delete [] pdist[!d]; - } - else delete [] pdist[d]; + delete [] dist[0]; + delete [] dist[1]; + delete [] clusters[0]; + delete [] clusters[1]; + delete [] inv; } void normalize_ptp(real_t * dist, const size_t & n) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 83a407d9..fb1068b9 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -18,14 +18,15 @@ namespace gproshan { double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) { CHE h_mesh(mesh); + const size_t & n_vertices = h_mesh.n_vertices; index_t * inv = nullptr; if(coalescence) { - inv = new index_t[mesh->n_vertices]; - h_mesh.GT = new vertex[mesh->n_vertices]; - h_mesh.VT = new index_t[mesh->n_half_edges]; - h_mesh.EVT = new index_t[mesh->n_vertices]; + inv = new index_t[n_vertices]; + h_mesh.GT = new vertex[n_vertices]; + h_mesh.EVT = new index_t[n_vertices]; + h_mesh.VT = new index_t[h_mesh.n_half_edges]; #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); ++i) @@ -49,7 +50,6 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * cudaDeviceReset(); - float time; cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); @@ -147,6 +147,8 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * cudaEventRecord(stop, 0); cudaEventSynchronize(stop); + + float time; cudaEventElapsedTime(&time, start, stop); cudaEventDestroy(start); @@ -159,34 +161,28 @@ index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, co real_t * h_dist, real_t ** d_dist, const toplesets_t & inv, real_t * d_error, index_t * h_clusters, index_t ** d_clusters, index_t * d_sorted) { - if(d_sorted) + for(index_t i = 0; i < sources.size(); ++i) { - cudaMemcpy(d_sorted, inv.index, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - for(index_t i = 0; i < sources.size(); ++i) - h_dist[sources[i]] = 0; - } - else - { - for(index_t i = 0; i < sources.size(); ++i) - h_dist[inv.index[sources[i]]] = 0; + const index_t & s = sources[i]; + const index_t & v = d_sorted ? s: inv.index[s]; + + h_dist[v] = 0; + + if(h_clusters) + h_clusters[v] = i; } cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - if(h_clusters) + if(d_sorted) { - if(d_sorted) - { - for(index_t i = 0; i < sources.size(); ++i) - h_clusters[sources[i]] = i + 1; - } - else - { - for(index_t i = 0; i < sources.size(); ++i) - h_clusters[inv.index[sources[i]]] = i + 1; - } + cudaMemcpy(d_sorted, inv.index, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + } + + if(h_clusters) + { cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(d_clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); } From 1cc986bb75c0cf45dcbf935db7efe8b32f502304 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 2 Dec 2022 22:25:36 +0100 Subject: [PATCH 0780/1018] geodesics (#16): updating ptp cpu and gpu there is a bug in the cpu propagation or display distant map --- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 10 +-- src/gproshan/geodesics/geodesics_ptp.cu | 82 +++++++++++------------- 3 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index cac0d921..2a30467c 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -86,7 +86,7 @@ void geodesics::normalize() #pragma omp parallel for for(size_t i = 0; i < n_sorted; ++i) - dist[sorted_index[i]] /= max; + dist[i] /= max; } void geodesics::execute(che * mesh, const std::vector & sources, const params & p) diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index d7b3419c..7d373b9b 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -68,7 +68,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c dist[0][v] = dist[1][v] = 0; if(ptp_out.clusters) - clusters[0][inv[s]] = clusters[0][inv[s]] = i + 1; + clusters[0][v] = clusters[1][v] = i + 1; } index_t d = 0; @@ -89,7 +89,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c #pragma omp parallel for for(index_t v = start; v < end; ++v) - relax_ptp(&h_mesh, dist[!d], dist[d], clusters[!d], clusters[d], inv ? inv[v] : v); + relax_ptp(&h_mesh, dist[!d], dist[d], clusters[!d], clusters[d], inv ? v : toplesets.index[v]); #pragma omp parallel for for(index_t v = start; v < start + n_cond; ++v) @@ -114,13 +114,13 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - dist[!d][v] = dist[d][inv[v]]; + ptp_out.dist[v] = dist[1][inv[v]]; + + delete [] dist[0]; } delete [] error; - delete [] dist[0]; delete [] dist[1]; - delete [] clusters[0]; delete [] clusters[1]; delete [] inv; } diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index fb1068b9..e00b85c0 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -55,42 +55,38 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * cudaEventCreate(&stop); cudaEventRecord(start, 0); - CHE * dd_mesh, * d_mesh; cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); - real_t * h_dist = coalescence ? new real_t[h_mesh.n_vertices] : ptp_out.dist; + real_t * h_dist = coalescence ? new real_t[n_vertices] : ptp_out.dist; + index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[n_vertices] + : ptp_out.clusters; - if(set_inf) + real_t * d_error = nullptr; + real_t * d_dist[2] = {}; + index_t * d_clusters[2] = {}; + index_t * d_sorted = nullptr; + + cudaMalloc(&d_error, sizeof(real_t) * n_vertices); + cudaMalloc(&d_dist[0], sizeof(real_t) * n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * n_vertices); + + if(h_clusters) { - #pragma omp parallel for - for(index_t v = 0; v < h_mesh.n_vertices; ++v) - h_dist[v] = INFINITY; + cudaMalloc(&d_clusters[0], sizeof(index_t) * n_vertices); + cudaMalloc(&d_clusters[1], sizeof(index_t) * n_vertices); } - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh.n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh.n_vertices); - - index_t * d_sorted = nullptr; + if(!coalescence) { - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh.n_vertices); + cudaMalloc(&d_sorted, sizeof(index_t) * n_vertices); } - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh.n_vertices); - - - index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[h_mesh.n_vertices] - : ptp_out.clusters; - - index_t * d_clusters[2] = {}; - - if(h_clusters) + if(set_inf) { - cudaMalloc(&d_clusters[0], sizeof(index_t) * h_mesh.n_vertices); - cudaMalloc(&d_clusters[1], sizeof(index_t) * h_mesh.n_vertices); + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + h_dist[v] = INFINITY; } const index_t & d = run_ptp_gpu(d_mesh, sources, h_mesh.n_vertices, @@ -100,36 +96,37 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * h_clusters, d_clusters, d_sorted); - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(coalescence) { #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - ptp_out.dist[toplesets.index[i]] = h_dist[i]; + for(index_t v = 0; v < n_vertices; ++v) + ptp_out.dist[v] = h_dist[inv[v]]; delete [] h_dist; } if(h_clusters) { - cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * h_mesh.n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * n_vertices, cudaMemcpyDeviceToHost); if(coalescence) { #pragma omp parallel for - for(index_t i = 0; i < h_mesh.n_vertices; ++i) - ptp_out.clusters[toplesets.index[i]] = h_clusters[i]; + for(index_t v = 0; v < n_vertices; ++v) + ptp_out.clusters[v] = h_clusters[inv[v]]; delete [] h_clusters; } - cudaFree(d_clusters[0]); - cudaFree(d_clusters[1]); } cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); + cudaFree(d_clusters[0]); + cudaFree(d_clusters[1]); + cudaFree(d_sorted); cuda_free_CHE(dd_mesh, d_mesh); if(coalescence) @@ -138,10 +135,6 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * delete [] h_mesh.VT; delete [] h_mesh.EVT; } - else - { - cudaFree(d_sorted); - } delete [] inv; @@ -164,12 +157,12 @@ index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, co for(index_t i = 0; i < sources.size(); ++i) { const index_t & s = sources[i]; - const index_t & v = d_sorted ? s: inv.index[s]; + const index_t & v = d_sorted ? s : inv.index[s]; h_dist[v] = 0; if(h_clusters) - h_clusters[v] = i; + h_clusters[v] = i + 1; } cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); @@ -180,7 +173,6 @@ index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, co cudaMemcpy(d_sorted, inv.index, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); } - if(h_clusters) { cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); @@ -325,16 +317,14 @@ void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * __global__ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted) { - index_t i = blockDim.x * blockIdx.x + threadIdx.x + start; + index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; - if(i < end) + if(v < end) { - index_t v = sorted ? sorted[i] : i; - #ifdef GPROSHAN_FLOAT - error[i] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; + error[v] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; #else - error[i] = fabs(new_dist[v] - old_dist[v]) / old_dist[v]; + error[v] = fabs(new_dist[v] - old_dist[v]) / old_dist[v]; #endif } } From 128fab6aa69c22b0b11669b155827eaa1ee9b0f5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 3 Dec 2022 22:24:39 +0100 Subject: [PATCH 0781/1018] geodesics (#16): factoring run_ptp code cpu and gpu --- include/gproshan/geodesics/geodesics_ptp.cuh | 37 ------ include/gproshan/geodesics/geodesics_ptp.h | 120 ++++++++++++++++++ src/gproshan/geodesics/geodesics_ptp.cpp | 44 ++++--- src/gproshan/geodesics/geodesics_ptp.cu | 106 +++------------- src/gproshan/geodesics/test_geodesics_ptp.cu | 3 +- .../test_geodesics_ptp_coalescence.cu | 3 +- 6 files changed, 167 insertions(+), 146 deletions(-) delete mode 100644 include/gproshan/geodesics/geodesics_ptp.cuh diff --git a/include/gproshan/geodesics/geodesics_ptp.cuh b/include/gproshan/geodesics/geodesics_ptp.cuh deleted file mode 100644 index 235b2bc1..00000000 --- a/include/gproshan/geodesics/geodesics_ptp.cuh +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef GEODESICS_PTP_CUH -#define GEODESICS_PTP_CUH - -#include -#include - - -#define NT 64 -#define NB(x) (x + NT - 1) / NT - - -// geometry processing and shape analysis framework -namespace gproshan { - - -index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, const index_t & n_vertices, - real_t * h_dist, real_t ** d_dist, const toplesets_t & inv, real_t * d_error, - index_t * h_clusters = nullptr, index_t ** d_clusters = nullptr, index_t * d_sorted = nullptr); - -__global__ -void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); - -__global__ -void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); - - -struct is_ok -{ - __host__ __device__ - bool operator()(const real_t & val) const; -}; - - -} // namespace gproshan - -#endif // GEODESICS_PTP_CUH - diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 8037b1bc..e5c71e62 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -8,6 +8,16 @@ #include + +#ifdef __CUDACC__ + #include + #include + #include + + #define NT 64 + #define NB(x) (x + NT - 1) / NT +#endif // __CUDACC__ + #define PTP_TOL 1e-4 @@ -15,6 +25,23 @@ namespace gproshan { +#ifdef __CUDACC__ + +__global__ +void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); + +__global__ +void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); + +struct is_ok +{ + __host__ __device__ + bool operator()(const real_t & val) const; +}; + +#endif // __CUDACC__ + + struct ptp_out_t { real_t * dist; @@ -122,6 +149,99 @@ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clust } } +template +#ifdef __CUDACC__ +__forceinline__ +#else +inline +#endif +index_t run_ptp(const CHE * mesh, const index_t & n_vertices, + const std::vector & sources, const std::vector & limits, + T * error, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted) +{ +#ifdef __CUDACC__ + T * h_dist = dist[2]; + index_t * h_clusters = clusters ? clusters[2] : nullptr; +#endif + + for(index_t i = 0; i < sources.size(); ++i) + { // !coalescence ? + const index_t & v = sorted ? sources[i] : idx[sources[i]]; + + #ifdef __CUDACC__ + h_dist[v] = 0; + if(h_clusters) h_clusters[v] = i + 1; + #else + dist[0][v] = dist[1][v] = 0; + if(clusters && clusters[0]) + clusters[0][v] = clusters[1][v] = i + 1; + #endif + } + +#ifdef __CUDACC__ + cudaMemcpy(dist[0], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(dist[1], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); + if(sorted) + { + cudaMemcpy(sorted, idx, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + } + if(clusters) + { + cudaMemcpy(clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMemcpy(clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); + } +#endif + + const int & max_iter = limits.size() << 1; + + int iter = -1; + index_t count = 0; + index_t i = 1; + index_t j = 2; + while(i < j && ++iter < max_iter) + { + if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size + + const index_t & start = limits[i]; + const index_t & end = limits[j]; + const index_t & n_cond = limits[i + 1] - start; + + real_t *& new_dist = dist[iter & 1]; + real_t *& old_dist = dist[!(iter & 1)]; + + index_t *& new_cluster = clusters[iter & 1]; + index_t *& old_cluster = clusters[!(iter & 1)]; + + #ifdef __CUDACC__ + relax_ptp<<< NB(end - start), NT >>>(mesh, new_dist, old_dist, new_cluster, old_cluster, start, end, sorted); + cudaDeviceSynchronize(); + + relative_error<<< NB(n_cond), NT >>>(error, new_dist, old_dist, start, start + n_cond); + cudaDeviceSynchronize(); + + count = thrust::count_if(thrust::device, error + start, error + start + n_cond, is_ok()); + #else + #pragma omp parallel for + for(index_t v = start; v < end; ++v) + relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? idx[v] : v); + + #pragma omp parallel for + for(index_t v = start; v < start + n_cond; ++v) + error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; + + count = 0; + #pragma omp parallel for reduction(+: count) + for(index_t v = start; v < start + n_cond; ++v) + count += error[v] < PTP_TOL; + #endif + + if(n_cond == count) ++i; + if(j < limits.size() - 1) ++j; + } + + return !(iter & 1); +} + } // namespace gproshan diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 7d373b9b..4eeaff1e 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -71,51 +71,57 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c clusters[0][v] = clusters[1][v] = i + 1; } - index_t d = 0; - index_t start, end, n_cond, count; - index_t i = 1, j = 2; + const int & max_iter = toplesets.limits.size() << 1; - // maximum number of iterations - index_t iter = 0; - index_t max_iter = toplesets.limits.size() << 1; - - while(i < j && iter++ < max_iter) + int iter = -1; + index_t i = 1; + index_t j = 2; + while(i < j && ++iter < max_iter) { if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - start = toplesets.limits[i]; - end = toplesets.limits[j]; - n_cond = toplesets.limits[i + 1] - start; + const index_t & start = toplesets.limits[i]; + const index_t & end = toplesets.limits[j]; + const index_t & n_cond = toplesets.limits[i + 1] - start; + + real_t *& new_dist = dist[iter & 1]; + real_t *& old_dist = dist[!(iter & 1)]; + + index_t *& new_cluster = clusters[iter & 1]; + index_t *& old_cluster = clusters[!(iter & 1)]; #pragma omp parallel for for(index_t v = start; v < end; ++v) - relax_ptp(&h_mesh, dist[!d], dist[d], clusters[!d], clusters[d], inv ? v : toplesets.index[v]); + relax_ptp(&h_mesh, new_dist, old_dist, new_cluster, old_cluster, inv ? v : toplesets.index[v]); #pragma omp parallel for for(index_t v = start; v < start + n_cond; ++v) - error[v] = abs(dist[!d][v] - dist[d][v]) / dist[d][v]; + error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; - count = 0; + index_t count = 0; #pragma omp parallel for reduction(+: count) for(index_t v = start; v < start + n_cond; ++v) count += error[v] < PTP_TOL; if(n_cond == count) ++i; if(j < toplesets.limits.size() - 1) ++j; - - d = !d; } #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - dist[!d][v] = dist[d][v]; - + dist[iter & 1][v] = dist[!(iter & 1)][v]; +/* + for(index_t v = 0; v < n_vertices; ++v) + gproshan_error_var(dist[d][v]); + gproshan_error_var(ptp_out.dist); + gproshan_error_var(dist[0]); + */ if(inv) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) ptp_out.dist[v] = dist[1][inv[v]]; - + delete [] dist[0]; } diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index e00b85c0..92ec255b 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -1,15 +1,12 @@ -#include #include +#include + #include #include #include #include -#include -#include -#include - // geometry processing and shape analysis framework namespace gproshan { @@ -63,20 +60,22 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * : ptp_out.clusters; real_t * d_error = nullptr; - real_t * d_dist[2] = {}; - index_t * d_clusters[2] = {}; + real_t * d_dist[3] = {}; + index_t * d_clusters[3] = {}; index_t * d_sorted = nullptr; - + cudaMalloc(&d_error, sizeof(real_t) * n_vertices); cudaMalloc(&d_dist[0], sizeof(real_t) * n_vertices); cudaMalloc(&d_dist[1], sizeof(real_t) * n_vertices); - + d_dist[2] = h_dist; + if(h_clusters) { cudaMalloc(&d_clusters[0], sizeof(index_t) * n_vertices); cudaMalloc(&d_clusters[1], sizeof(index_t) * n_vertices); + d_clusters[2] = h_clusters; } - + if(!coalescence) { cudaMalloc(&d_sorted, sizeof(index_t) * n_vertices); @@ -89,32 +88,28 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * h_dist[v] = INFINITY; } - const index_t & d = run_ptp_gpu(d_mesh, sources, h_mesh.n_vertices, - h_dist, d_dist, - {toplesets.limits, coalescence ? inv : toplesets.index}, - d_error, - h_clusters, d_clusters, - d_sorted); + const index_t & i = run_ptp(d_mesh, n_vertices, sources, toplesets.limits, d_error, + d_dist, d_clusters, coalescence ? inv : toplesets.index, d_sorted); - cudaMemcpy(h_dist, d_dist[d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[i], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); if(coalescence) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.dist[v] = h_dist[inv[v]]; + ptp_out.dist[i] = h_dist[inv[i]]; delete [] h_dist; } if(h_clusters) { - cudaMemcpy(h_clusters, d_clusters[d], sizeof(index_t) * n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_clusters, d_clusters[i], sizeof(index_t) * n_vertices, cudaMemcpyDeviceToHost); if(coalescence) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.clusters[v] = h_clusters[inv[v]]; + ptp_out.clusters[i] = h_clusters[inv[i]]; delete [] h_clusters; } @@ -150,72 +145,9 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * return time / 1000; } -index_t run_ptp_gpu(const CHE * d_mesh, const std::vector & sources, const index_t & n_vertices, - real_t * h_dist, real_t ** d_dist, const toplesets_t & inv, real_t * d_error, - index_t * h_clusters, index_t ** d_clusters, index_t * d_sorted) -{ - for(index_t i = 0; i < sources.size(); ++i) - { - const index_t & s = sources[i]; - const index_t & v = d_sorted ? s : inv.index[s]; - - h_dist[v] = 0; - - if(h_clusters) - h_clusters[v] = i + 1; - } - - cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - - if(d_sorted) - { - cudaMemcpy(d_sorted, inv.index, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - } - - if(h_clusters) - { - cudaMemcpy(d_clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - } - - index_t d = 0; - index_t start, end, n_cond; - index_t i = 1, j = 2; - - // maximum number of iterations - index_t iter = 0; - index_t max_iter = inv.limits.size() << 1; - - while(i < j && iter++ < max_iter) - { - if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - - start = inv.limits[i]; - end = inv.limits[j]; - n_cond = inv.limits[i + 1] - start; - - h_clusters ? relax_ptp<<< NB(end - start), NT >>>(d_mesh, d_dist[!d], d_dist[d], d_clusters[!d], d_clusters[d], start, end, d_sorted) - : relax_ptp<<< NB(end - start), NT >>>(d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); - - cudaDeviceSynchronize(); - - relative_error<<< NB(n_cond), NT >>>(d_error, d_dist[!d], d_dist[d], start, start + n_cond); - cudaDeviceSynchronize(); - - if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - ++i; - - if(j < inv.limits.size() - 1) ++j; - - d = !d; - } - - return d; -} - real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { + const size_t & n_vertices = mesh->n_vertices; cudaDeviceReset(); float time; @@ -236,9 +168,10 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample h_dist[v] = INFINITY; - real_t * d_dist[2]; + real_t * d_dist[3]; cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); + d_dist[2] = h_dist; real_t * d_error; cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); @@ -266,7 +199,8 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); + const index_t & i = run_ptp(d_mesh, n_vertices, samples, limits, d_error, + d_dist, nullptr, sorted_index, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 15c06828..adbb5e1f 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -118,7 +117,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); +// d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 5da1741e..0f167473 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -1,7 +1,6 @@ #include #include -#include #include #include @@ -163,7 +162,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect cuda_create_CHE(h_mesh, dd_mesh, d_mesh); // exec algorithm - d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, inv}, d_error); + //d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, inv}, d_error); // free memory cuda_free_CHE(dd_mesh, d_mesh); From 88c5c5249bd190fccd72be79b3d30b83f56dfb59 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Dec 2022 16:00:26 +0100 Subject: [PATCH 0782/1018] geodesics (#16): factoring code gpu coalescence updated --- include/gproshan/geodesics/geodesics_ptp.h | 17 ++++---- src/gproshan/geodesics/geodesics_ptp.cpp | 51 ++-------------------- src/gproshan/geodesics/geodesics_ptp.cu | 17 ++++---- 3 files changed, 21 insertions(+), 64 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index e5c71e62..955c5c57 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -56,8 +56,6 @@ struct toplesets_t const index_t *const & index; }; -che * ptp_coalescence(index_t * & inv, const che * mesh, const toplesets_t & toplesets); - double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = false, const bool & set_inf = true); @@ -128,7 +126,7 @@ template __forceinline__ #endif __host__ __device__ -void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) +void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t & v) { real_t & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; @@ -155,9 +153,9 @@ __forceinline__ #else inline #endif -index_t run_ptp(const CHE * mesh, const index_t & n_vertices, - const std::vector & sources, const std::vector & limits, - T * error, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted) +index_t run_ptp(const CHE * mesh, const std::vector & sources, + const std::vector & limits, T * error, T ** dist, index_t ** clusters, + const index_t * idx, index_t * sorted) { #ifdef __CUDACC__ T * h_dist = dist[2]; @@ -179,6 +177,7 @@ index_t run_ptp(const CHE * mesh, const index_t & n_vertices, } #ifdef __CUDACC__ + const size_t & n_vertices = limits.back(); cudaMemcpy(dist[0], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(dist[1], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); if(sorted) @@ -206,8 +205,8 @@ index_t run_ptp(const CHE * mesh, const index_t & n_vertices, const index_t & end = limits[j]; const index_t & n_cond = limits[i + 1] - start; - real_t *& new_dist = dist[iter & 1]; - real_t *& old_dist = dist[!(iter & 1)]; + T *& new_dist = dist[iter & 1]; + T *& old_dist = dist[!(iter & 1)]; index_t *& new_cluster = clusters[iter & 1]; index_t *& old_cluster = clusters[!(iter & 1)]; @@ -223,7 +222,7 @@ index_t run_ptp(const CHE * mesh, const index_t & n_vertices, #else #pragma omp parallel for for(index_t v = start; v < end; ++v) - relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? idx[v] : v); + relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); #pragma omp parallel for for(index_t v = start; v < start + n_cond; ++v) diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 4eeaff1e..86fb7060 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -60,56 +60,13 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c dist[0][v] = dist[1][v] = INFINITY; } - for(index_t i = 0; i < sources.size(); ++i) - { - const index_t & s = sources[i]; - const index_t & v = inv ? inv[s] : s; - - dist[0][v] = dist[1][v] = 0; - - if(ptp_out.clusters) - clusters[0][v] = clusters[1][v] = i + 1; - } - - const int & max_iter = toplesets.limits.size() << 1; - - int iter = -1; - index_t i = 1; - index_t j = 2; - while(i < j && ++iter < max_iter) - { - if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - - const index_t & start = toplesets.limits[i]; - const index_t & end = toplesets.limits[j]; - const index_t & n_cond = toplesets.limits[i + 1] - start; - - real_t *& new_dist = dist[iter & 1]; - real_t *& old_dist = dist[!(iter & 1)]; - - index_t *& new_cluster = clusters[iter & 1]; - index_t *& old_cluster = clusters[!(iter & 1)]; - - #pragma omp parallel for - for(index_t v = start; v < end; ++v) - relax_ptp(&h_mesh, new_dist, old_dist, new_cluster, old_cluster, inv ? v : toplesets.index[v]); - - #pragma omp parallel for - for(index_t v = start; v < start + n_cond; ++v) - error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; - - index_t count = 0; - #pragma omp parallel for reduction(+: count) - for(index_t v = start; v < start + n_cond; ++v) - count += error[v] < PTP_TOL; - - if(n_cond == count) ++i; - if(j < toplesets.limits.size() - 1) ++j; - } + const index_t & i = run_ptp(&h_mesh, sources, toplesets.limits, error, dist, clusters, + coalescence ? inv : toplesets.index, + coalescence ? nullptr : (index_t *) toplesets.index); #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - dist[iter & 1][v] = dist[!(iter & 1)][v]; + dist[!i][v] = dist[i][v]; /* for(index_t v = 0; v < n_vertices; ++v) gproshan_error_var(dist[d][v]); diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 92ec255b..f8578244 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -88,15 +88,16 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * h_dist[v] = INFINITY; } - const index_t & i = run_ptp(d_mesh, n_vertices, sources, toplesets.limits, d_error, - d_dist, d_clusters, coalescence ? inv : toplesets.index, d_sorted); + const index_t & i = run_ptp(d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, + coalescence ? inv : toplesets.index, d_sorted); cudaMemcpy(h_dist, d_dist[i], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); + if(coalescence) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.dist[i] = h_dist[inv[i]]; + ptp_out.dist[v] = h_dist[inv[v]]; delete [] h_dist; } @@ -109,11 +110,10 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.clusters[i] = h_clusters[inv[i]]; + ptp_out.clusters[v] = h_clusters[inv[v]]; delete [] h_clusters; } - } cudaFree(d_error); @@ -147,7 +147,6 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { - const size_t & n_vertices = mesh->n_vertices; cudaDeviceReset(); float time; @@ -199,8 +198,8 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - const index_t & i = run_ptp(d_mesh, n_vertices, samples, limits, d_error, - d_dist, nullptr, sorted_index, d_sorted); + const index_t & i = run_ptp(d_mesh, samples, limits, d_error, d_dist, nullptr, + sorted_index, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT @@ -255,6 +254,8 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ if(v < end) { + v = sorted ? sorted[v] : v; + #ifdef GPROSHAN_FLOAT error[v] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; #else From 6fe4414bce9488f572c57c5fc9a7e2b551b8afbb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Dec 2022 16:29:18 +0100 Subject: [PATCH 0783/1018] geodesics (#16): updated ptp fps gpu --- include/gproshan/geodesics/geodesics_ptp.h | 2 +- src/gproshan/geodesics/geodesics_ptp.cu | 58 +++++++++++----------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 955c5c57..7bf30beb 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -159,7 +159,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, { #ifdef __CUDACC__ T * h_dist = dist[2]; - index_t * h_clusters = clusters ? clusters[2] : nullptr; + index_t * h_clusters = clusters[2]; #endif for(index_t i = 0; i < sources.size(); ++i) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index f8578244..6aad6086 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -147,71 +147,68 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { + CHE h_mesh(mesh); + const size_t & n_vertices = h_mesh.n_vertices; + cudaDeviceReset(); - float time; cudaEvent_t start, stop; cudaEventCreate(&start); cudaEventCreate(&stop); cudaEventRecord(start, 0); - // BEGIN FPS PTP - - CHE * h_mesh = new CHE(mesh); CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); + cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); - real_t * h_dist = new real_t[h_mesh->n_vertices]; - #pragma omp parallel for - for(index_t v = 0; v < h_mesh->n_vertices; ++v) - h_dist[v] = INFINITY; + real_t * h_dist = new real_t[n_vertices]; + real_t * d_error = nullptr; + real_t * d_dist[3] = {}; + index_t * d_clusters[3] = {}; + index_t * d_sorted = nullptr; - real_t * d_dist[3]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); + cudaMalloc(&d_error, sizeof(real_t) * n_vertices); + cudaMalloc(&d_dist[0], sizeof(real_t) * n_vertices); + cudaMalloc(&d_dist[1], sizeof(real_t) * n_vertices); + cudaMalloc(&d_sorted, sizeof(index_t) * n_vertices); d_dist[2] = h_dist; - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - - index_t * d_sorted; - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + h_dist[v] = INFINITY; std::vector limits; - index_t * toplesets = new index_t[h_mesh->n_vertices]; - index_t * sorted_index = new index_t[h_mesh->n_vertices]; + index_t * toplesets = new index_t[n_vertices]; + index_t * sorted_index = new index_t[n_vertices]; cublasHandle_t handle; cublasCreate(&handle); - if(n >= h_mesh->n_vertices) n = h_mesh->n_vertices >> 1; + if(n >= n_vertices) n = n_vertices >> 2; n -= samples.size(); samples.reserve(n); - index_t d; - int f; + int farthest; real_t max_dist = INFINITY; - while(n-- && max_dist > radio) + while(n-- && radio < max_dist) { limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - const index_t & i = run_ptp(d_mesh, samples, limits, d_error, d_dist, nullptr, - sorted_index, d_sorted); + const index_t & i = run_ptp(d_mesh, samples, limits, d_error, d_dist, d_clusters, sorted_index, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT - cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); + cublasIsamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); #else - cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); + cublasIdamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); #endif if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); + cudaMemcpy(&max_dist, d_dist[i] + farthest - 1, sizeof(real_t), cudaMemcpyDeviceToHost); - samples.push_back(f - 1); + samples.push_back(farthest - 1); } cublasDestroy(handle); @@ -226,9 +223,10 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample cudaFree(d_sorted); cuda_free_CHE(dd_mesh, d_mesh); - // END FPS PTP cudaEventRecord(stop, 0); cudaEventSynchronize(stop); + + float time; cudaEventElapsedTime(&time, start, stop); time_fps = time / 1000; From 3afbdf00b59e6dc590630309a7dfaf16877d17ad Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 4 Dec 2022 17:44:51 +0100 Subject: [PATCH 0784/1018] geodesics (#16): pending ptp cpu and non coalescence review code --- include/gproshan/geodesics/geodesics_ptp.h | 8 +++++++- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 9 ++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 7bf30beb..e22366f6 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -225,13 +225,19 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); #pragma omp parallel for - for(index_t v = start; v < start + n_cond; ++v) + for(index_t i = start; i < start + n_cond; ++i) + { + const index_t & v = sorted ? sorted[v] : i; error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; + } count = 0; #pragma omp parallel for reduction(+: count) for(index_t v = start; v < start + n_cond; ++v) + { + const index_t & v = sorted ? sorted[v] : i; count += error[v] < PTP_TOL; + } #endif if(n_cond == count) ++i; diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 2a30467c..f2bd6750 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -227,7 +227,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::ve std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); - double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}, sources.size() == 1); + double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}, true); gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 86fb7060..0b8dd2fb 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -67,17 +67,12 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) dist[!i][v] = dist[i][v]; -/* - for(index_t v = 0; v < n_vertices; ++v) - gproshan_error_var(dist[d][v]); - gproshan_error_var(ptp_out.dist); - gproshan_error_var(dist[0]); - */ + if(inv) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) - ptp_out.dist[v] = dist[1][inv[v]]; + ptp_out.dist[v] = dist[0][inv[v]]; delete [] dist[0]; } From 6d58a39612eadaf03e3640fa28f41e7cb407988f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Dec 2022 14:35:01 +0100 Subject: [PATCH 0785/1018] geodesics (#16): ptp gpu coalescence and non coalescence refactored code ready --- include/gproshan/geodesics/geodesics_ptp.h | 14 ++++++++++---- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cu | 6 ++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index e22366f6..9fd0f7ae 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -35,8 +35,13 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ struct is_ok { + const real_t * error = nullptr; + __host__ __device__ bool operator()(const real_t & val) const; + + __host__ __device__ + bool operator()(const index_t & val) const; }; #endif // __CUDACC__ @@ -215,10 +220,11 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, relax_ptp<<< NB(end - start), NT >>>(mesh, new_dist, old_dist, new_cluster, old_cluster, start, end, sorted); cudaDeviceSynchronize(); - relative_error<<< NB(n_cond), NT >>>(error, new_dist, old_dist, start, start + n_cond); + relative_error<<< NB(n_cond), NT >>>(error, new_dist, old_dist, start, start + n_cond, sorted); cudaDeviceSynchronize(); - count = thrust::count_if(thrust::device, error + start, error + start + n_cond, is_ok()); + count = sorted ? thrust::count_if(thrust::device, sorted + start, sorted + start + n_cond, is_ok{error}) + : thrust::count_if(thrust::device, error + start, error + start + n_cond, is_ok{}); #else #pragma omp parallel for for(index_t v = start; v < end; ++v) @@ -227,7 +233,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, #pragma omp parallel for for(index_t i = start; i < start + n_cond; ++i) { - const index_t & v = sorted ? sorted[v] : i; + const index_t & v = sorted ? sorted[i] : i; error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; } @@ -235,7 +241,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, #pragma omp parallel for reduction(+: count) for(index_t v = start; v < start + n_cond; ++v) { - const index_t & v = sorted ? sorted[v] : i; + const index_t & v = sorted ? sorted[i] : i; count += error[v] < PTP_TOL; } #endif diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index f2bd6750..2790673b 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -227,7 +227,7 @@ void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::ve std::vector limits; mesh->compute_toplesets(toplesets, sorted_index, limits, sources); - double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}, true); + double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 6aad6086..bbd8e7b8 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -268,6 +268,12 @@ bool is_ok::operator()(const real_t & val) const return val < PTP_TOL; } +__host__ __device__ +bool is_ok::operator()(const index_t & i) const +{ + return error[i] < PTP_TOL; +} + } // namespace gproshan From b913e09e982b49f6baa2b9773e718d3f0fa6fb08 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Dec 2022 15:59:10 +0100 Subject: [PATCH 0786/1018] raytracing: Blinn - Phong, adding material information --- include/gproshan/raytracing/raytracing.h | 1 + include/gproshan/raytracing/rt_embree.h | 2 +- include/gproshan/raytracing/rt_utils.h | 67 ++++++++++++++++++------ src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/raytracing/rt_embree.cpp | 4 +- src/gproshan/raytracing/rt_optix.cu | 2 +- src/gproshan/scenes/scanner.cpp | 2 +- 7 files changed, 57 insertions(+), 23 deletions(-) diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index e7a2d069..b9557ab1 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -42,6 +42,7 @@ class raytracing const vertex &, // dir const vertex *, // lights const int &, // n_lights + const vertex &, // cam_pos const bool & // flat ) { return {}; }; diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 328973c3..ed64cad9 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -58,7 +58,7 @@ class embree : public raytracing virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const bool & flat); + virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const vertex & cam_pos, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 1b88ebd7..55354ce9 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -45,49 +45,82 @@ struct t_eval_hit T u = 0, v = 0; vec position; vec normal; - vec color; + vec Ka{0.4f, 0.4f, 0.4f}; + vec Kd; + vec Ks{0.2f, 0.2f, 0.2f}; + T Ns = 4; __host__ __device__ t_eval_hit() {} __host__ __device__ - t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av) + t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, void * mat = nullptr) { primID = aprimID; u = au; v = av; - const int he = primID * che::mtrig; + const index_t he = primID * che::mtrig; - const int a = mesh.VT[he]; - const int b = mesh.VT[he + 1]; - const int c = mesh.VT[he + 2]; + const index_t a = mesh.VT[he]; + const index_t b = mesh.VT[he + 1]; + const index_t c = mesh.VT[he + 2]; const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; - color = ((1.f - u - v) * ca + u * cb + v * cc) / 255; + Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + + if(mat) + {/* + Ka = mat.Ka; + if(mat.map_Ka != -1) + Ka *= texture(tex_Ka, texcoord).rgb; + + Kd = mat.Kd; + if(mat.map_Kd != -1) + Kd *= texture(tex_Kd, texcoord).rgb; + + Ks = mat.Ks; + if(mat.map_Ks != -1) + Ks *= texture(tex_Ks, texcoord).rgb; + + Ns = mat.Ns; + */} } }; template __host__ __device__ -vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, Occluded occluded) +vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) { - vec li, wi; - float light_dist, dot_wi_normal; + const T Lp = 10; + const vec La(0.2f); + + vec li, l, h; + vec v = normalize(eye - hit.position); + const vec & n = hit.normal; for(int i = 0; i < n_lights; ++i) { - wi = lights[i] - hit.position; - light_dist = length(wi); - - wi /= light_dist; - dot_wi_normal = dot(wi, hit.normal); - - li += (dot_wi_normal < 0 ? -dot_wi_normal : dot_wi_normal) * (occluded(hit.position, wi, light_dist) ? 0.4f : 1.0f) * hit.color; + l = lights[i] - hit.position; + const T & r = length(l); + + l /= r; + h = normalize(l + v); + + #ifdef __CUDACC__ + const T & lambertian = max(dot(l, n), 0.f); + const T & specular = pow(max(dot(h, n), 0.f), hit.Ns); + #else + const T & lambertian = std::max(dot(l, n), 0.f); + const T & specular = pow(std::max(dot(h, n), 0.f), hit.Ns); + #endif // __CUDACC__ + + const vec & color = hit.Ka * La + (lambertian * hit.Kd + specular * hit.Ks) * Lp / (r * r); + li += (occluded(hit.position, l, r) ? 0.4f : 1.0f) * color; } return li / n_lights; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index fb923b69..2c3e39fa 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -33,7 +33,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f params.cam_pos ); - vec3 li = closesthit_radiance(params.cam_pos, dir, params.lights, params.n_lights, flat); + vec3 li = closesthit_radiance(params.cam_pos, dir, params.lights, params.n_lights, params.cam_pos, flat); color = (color * n_samples + li) / (n_samples + 1); color[3] = 1; diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index e7d7d529..a84b9b9c 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -241,7 +241,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const bool & flat) +vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -250,7 +250,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; - return eval_li( hit, lights, n_lights, + return eval_li( hit, lights, n_lights, cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 87d7d0ca..e3414555 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -58,7 +58,7 @@ extern "C" __global__ void __closesthit__radiance() hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, + vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { uint32_t occluded = 1; diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 11ac163b..e5c4a340 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -35,7 +35,7 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c mesh_ptx->point(v) = h.position; mesh_ptx->normal(v) = h.normal; mesh_ptx->heatmap(v) = h.dist / M_SQRT2; - mesh_ptx->rgb(v) = h.color; + mesh_ptx->rgb(v) = h.Kd; } else { From a8478a629187520249e0c6b5f0cbc9e9d834f102 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Dec 2022 17:36:52 +0100 Subject: [PATCH 0787/1018] rt_optix: passing scene information --- include/gproshan/raytracing/rt_optix.h | 1 + include/gproshan/raytracing/rt_optix_params.h | 3 +++ include/gproshan/raytracing/rt_utils.h | 14 ++++++++++---- include/gproshan/scenes/scene.h | 9 +++++++++ src/gproshan/raytracing/rt_embree.cpp | 4 ++-- src/gproshan/raytracing/rt_optix.cpp | 18 ++++++++++++++++++ src/gproshan/raytracing/rt_optix.cu | 2 +- src/gproshan/scenes/scene.cpp | 15 +++++++++++++++ 8 files changed, 59 insertions(+), 7 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index c1d086d6..9d772289 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -3,6 +3,7 @@ #include #include +#include #include #include diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index 5a8f4b65..d7379160 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -4,6 +4,7 @@ #include #include +#include #ifdef GPROSHAN_OPTIX @@ -31,6 +32,8 @@ struct launch_params bool flat; OptixTraversableHandle traversable; + + params_scene sc; }; diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 55354ce9..5fac73f7 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -5,6 +5,7 @@ #include #include +#include // geometry processing and shape analysis framework @@ -54,7 +55,7 @@ struct t_eval_hit t_eval_hit() {} __host__ __device__ - t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, void * mat = nullptr) + t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const params_scene & sc) { primID = aprimID; u = au; @@ -73,8 +74,13 @@ struct t_eval_hit Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; - if(mat) - {/* + if(!sc.trig_mat) return; + if(sc.trig_mat[primID] == NIL) return; + + const scene::material & mat = sc.materials[sc.trig_mat[primID]]; + + Kd = mat.Kd; + /* Ka = mat.Ka; if(mat.map_Ka != -1) Ka *= texture(tex_Ka, texcoord).rgb; @@ -88,7 +94,7 @@ struct t_eval_hit Ks *= texture(tex_Ks, texcoord).rgb; Ns = mat.Ns; - */} + */ } }; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index d2549e51..9588178e 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -55,6 +55,7 @@ class scene: public che std::vector objects; + index_t * trig_mat = nullptr; vec2 * texcoords = nullptr; bool load_scene = true; @@ -69,6 +70,14 @@ class scene: public che bool load_texture(const std::string & file); }; +struct params_scene +{ + scene::material * materials = nullptr; + scene::texture * textures = nullptr; + index_t * trig_mat = nullptr; + vec2 * texcoords = nullptr; +}; + } // namespace gproshan diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index a84b9b9c..40a911b0 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -115,7 +115,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, params_scene{}); hit.dist = r.ray.tfar; hit.position = r.position(); @@ -246,7 +246,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, params_scene{}); hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 43ddc744..5431a184 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -113,6 +113,11 @@ optix::~optix() for(index_t i = 0; i < dd_mesh.size(); ++i) cuda_free_CHE(dd_mesh[i], d_mesh[i]); + + cudaFree(optix_params.sc.materials); + cudaFree(optix_params.sc.textures); + cudaFree(optix_params.sc.trig_mat); + cudaFree(optix_params.sc.texcoords); } void optix::render(vec4 * img, const render_params & params, const bool & flat) @@ -458,7 +463,20 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; if(mesh->is_scene()) + { delete [] h_m.VT; + + scene * sc = (scene *) mesh; + cudaMalloc(&optix_params.sc.materials, sc->materials.size() * sizeof(scene::material)); + cudaMalloc(&optix_params.sc.textures, sc->textures.size() * sizeof(scene::texture)); + cudaMalloc(&optix_params.sc.trig_mat, mesh->n_vertices / 3 * sizeof(index_t)); + cudaMalloc(&optix_params.sc.texcoords, mesh->n_vertices * sizeof(vec2)); + + cudaMemcpy(optix_params.sc.materials, sc->materials.data(), sc->materials.size() * sizeof(scene::material), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.textures, sc->textures.data(), sc->textures.size() * sizeof(scene::texture), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.texcoords, sc->texcoords, mesh->n_vertices * sizeof(vec2), cudaMemcpyHostToDevice); + } } diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index e3414555..033e43ed 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -54,7 +54,7 @@ extern "C" __global__ void __closesthit__radiance() const vertex & B = data[1]; const vertex & C = data[2]; - eval_hit hit(mesh, primID, bar.x, bar.y); + eval_hit hit(mesh, primID, bar.x, bar.y, optix_params.sc); hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index abfc2449..4f296242 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -20,6 +20,8 @@ scene::~scene() { for(texture & tex: textures) delete tex.data; + + delete [] trig_mat; delete [] texcoords; } @@ -82,6 +84,19 @@ bool scene::load_obj(const std::string & file) gproshan_log_var(objects.size()); + trig_mat = new index_t[n_vertices / 3]; + memset(trig_mat, -1, sizeof(index_t) * n_vertices / 3); + + #pragma omp parallel for + for(index_t i = 0; i < objects.size() - 1; ++i) + { + const object & obj = objects[i]; + const index_t & n = objects[i + 1].begin; + + for(index_t t = obj.begin; t < n; t += 3) + trig_mat[t / 3] = obj.material_id; + } + return true; } From f8d233fed22ebb6483b8186dfc2803374b75e7c7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 6 Dec 2022 13:11:16 +0100 Subject: [PATCH 0788/1018] rt_optix: rendering with textures --- include/gproshan/raytracing/rt_optix.h | 2 + include/gproshan/raytracing/rt_utils.h | 65 ++++++++++++++++++-------- src/gproshan/raytracing/rt_optix.cpp | 15 +++++- src/gproshan/scenes/scene.cpp | 1 + 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/rt_optix.h index 9d772289..390f289a 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/rt_optix.h @@ -51,6 +51,8 @@ class optix : public raytracing void * hitgroup_records_buffer = nullptr; void * as_buffer = nullptr; + std::vector tex_data; + public: optix(); optix(const std::vector & meshes, const std::vector & model_mats); diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 5fac73f7..632c7216 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -67,9 +67,9 @@ struct t_eval_hit const index_t b = mesh.VT[he + 1]; const index_t c = mesh.VT[he + 2]; - const vertex ca = {float(mesh.VC[a].r), float(mesh.VC[a].g), float(mesh.VC[a].b)}; - const vertex cb = {float(mesh.VC[b].r), float(mesh.VC[b].g), float(mesh.VC[b].b)}; - const vertex cc = {float(mesh.VC[c].r), float(mesh.VC[c].g), float(mesh.VC[c].b)}; + const vec ca = {T(mesh.VC[a].r), T(mesh.VC[a].g), T(mesh.VC[a].b)}; + const vec cb = {T(mesh.VC[b].r), T(mesh.VC[b].g), T(mesh.VC[b].b)}; + const vec cc = {T(mesh.VC[c].r), T(mesh.VC[c].g), T(mesh.VC[c].b)}; Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; @@ -78,23 +78,48 @@ struct t_eval_hit if(sc.trig_mat[primID] == NIL) return; const scene::material & mat = sc.materials[sc.trig_mat[primID]]; + vec texcoord; + if(sc.texcoords) + texcoord = (1.f - u - v) * sc.texcoords[a] + u * sc.texcoords[b] + v * sc.texcoords[c]; + + Ka = mat.Ka; + if(mat.map_Ka != -1) + Ka *= texture(sc.textures[mat.map_Ka], texcoord); Kd = mat.Kd; - /* - Ka = mat.Ka; - if(mat.map_Ka != -1) - Ka *= texture(tex_Ka, texcoord).rgb; + if(mat.map_Kd != -1) + Kd *= texture(sc.textures[mat.map_Kd], texcoord); + + Ks = mat.Ks; + if(mat.map_Ks != -1) + Ks *= texture(sc.textures[mat.map_Ks], texcoord); - Kd = mat.Kd; - if(mat.map_Kd != -1) - Kd *= texture(tex_Kd, texcoord).rgb; + Ns = mat.Ns; + } + + __host__ __device__ + vec texture(const scene::texture & tex, const vec & coord) + { + int i = coord.x() * ((int)tex.width); + int j = coord.y() * ((int)tex.height); + if(i < 0) i = 0; + if(i >= tex.width) i = tex.width - 1; + if(j < 0) j = 0; + if(j >= tex.height) j = tex.height - 1; - Ks = mat.Ks; - if(mat.map_Ks != -1) - Ks *= texture(tex_Ks, texcoord).rgb; + const int k = j * tex.width + i; - Ns = mat.Ns; - */ + che::rgb_t color; + if(tex.spectrum == 3) + { + const che::rgb_t * img = (const che::rgb_t *) tex.data; + color = img[k]; + } + if(tex.spectrum == 1) + { + color.r = color.g = color.b = tex.data[k]; + } + return {T(color.r) / 255, T(color.g) / 255, T(color.b) / 255}; } }; @@ -109,6 +134,8 @@ vec eval_li(const t_eval_hit & hit, const vec * lights, const int vec v = normalize(eye - hit.position); const vec & n = hit.normal; + T lambertian; + T specular; for(int i = 0; i < n_lights; ++i) { l = lights[i] - hit.position; @@ -118,11 +145,11 @@ vec eval_li(const t_eval_hit & hit, const vec * lights, const int h = normalize(l + v); #ifdef __CUDACC__ - const T & lambertian = max(dot(l, n), 0.f); - const T & specular = pow(max(dot(h, n), 0.f), hit.Ns); + lambertian = max(dot(l, n), 0.f); + specular = pow(max(dot(h, n), 0.f), hit.Ns); #else - const T & lambertian = std::max(dot(l, n), 0.f); - const T & specular = pow(std::max(dot(h, n), 0.f), hit.Ns); + lambertian = std::max(dot(l, n), 0.f); + specular = pow(std::max(dot(h, n), 0.f), hit.Ns); #endif // __CUDACC__ const vec & color = hit.Ka * La + (lambertian * hit.Kd + specular * hit.Ks) * Lp / (r * r); diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index 5431a184..b3893e73 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -118,6 +118,9 @@ optix::~optix() cudaFree(optix_params.sc.textures); cudaFree(optix_params.sc.trig_mat); cudaFree(optix_params.sc.texcoords); + + for(unsigned char * data: tex_data) + cudaFree(data); } void optix::render(vec4 * img, const render_params & params, const bool & flat) @@ -472,8 +475,18 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u cudaMalloc(&optix_params.sc.trig_mat, mesh->n_vertices / 3 * sizeof(index_t)); cudaMalloc(&optix_params.sc.texcoords, mesh->n_vertices * sizeof(vec2)); + std::vector textures = sc->textures; + for(scene::texture & tex: textures) + { + unsigned char * h_data = tex.data; + cudaMalloc(&tex.data, tex.width * tex.height * tex.spectrum); + cudaMemcpy(tex.data, h_data, tex.width * tex.height * tex.spectrum, cudaMemcpyHostToDevice); + tex_data.push_back(tex.data); + } + + gproshan_error_var(textures.size()); cudaMemcpy(optix_params.sc.materials, sc->materials.data(), sc->materials.size() * sizeof(scene::material), cudaMemcpyHostToDevice); - cudaMemcpy(optix_params.sc.textures, sc->textures.data(), sc->textures.size() * sizeof(scene::texture), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.textures, textures.data(), textures.size() * sizeof(scene::texture), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.texcoords, sc->texcoords, mesh->n_vertices * sizeof(vec2), cudaMemcpyHostToDevice); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 4f296242..6031f79f 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -223,6 +223,7 @@ bool scene::load_texture(const std::string & file) tex.width = img.width(); tex.height = img.height(); tex.spectrum = img.spectrum(); + gproshan_error_var(tex.spectrum); tex.data = new unsigned char[tex.width * tex.height * tex.spectrum]; img.permute_axes("cxyz"); memcpy(tex.data, img.data(), tex.width * tex.height * tex.spectrum); From b9e6adc61d1599575186c8d962afa04f2629fa0b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 6 Dec 2022 14:25:11 +0100 Subject: [PATCH 0789/1018] raytracing: fix texture function, implemented repeat pattern --- include/gproshan/raytracing/rt_utils.h | 47 ++++++++++++-------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 632c7216..76764430 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -38,6 +38,28 @@ struct random } }; +template +__host__ __device__ +vec texture(const scene::texture & tex, const vec & coord) +{ + const int i = (tex.width + int(coord.x() * (tex.width - 1))) % tex.width; + const int j = (tex.height + int(coord.y() * (tex.height -1))) % tex.height; + const int k = j * tex.width + i; + + che::rgb_t color; + if(tex.spectrum == 3) + { + const che::rgb_t * img = (const che::rgb_t *) tex.data; + color = img[k]; + } + if(tex.spectrum == 1) + { + color.r = color.g = color.b = tex.data[k]; + } + + return {T(color.r) / 255, T(color.g) / 255, T(color.b) / 255}; +} + template struct t_eval_hit { @@ -96,31 +118,6 @@ struct t_eval_hit Ns = mat.Ns; } - - __host__ __device__ - vec texture(const scene::texture & tex, const vec & coord) - { - int i = coord.x() * ((int)tex.width); - int j = coord.y() * ((int)tex.height); - if(i < 0) i = 0; - if(i >= tex.width) i = tex.width - 1; - if(j < 0) j = 0; - if(j >= tex.height) j = tex.height - 1; - - const int k = j * tex.width + i; - - che::rgb_t color; - if(tex.spectrum == 3) - { - const che::rgb_t * img = (const che::rgb_t *) tex.data; - color = img[k]; - } - if(tex.spectrum == 1) - { - color.r = color.g = color.b = tex.data[k]; - } - return {T(color.r) / 255, T(color.g) / 255, T(color.b) / 255}; - } }; template From bca8614b4acb84b7e21b24d66a14f49ddec1e392 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 6 Dec 2022 15:32:56 +0100 Subject: [PATCH 0790/1018] using OptiX 7.6, refactoring host device template functions --- CMakeLists.txt | 2 +- include/gproshan/geodesics/geodesics_ptp.h | 8 +-- include/gproshan/geometry/mat.h | 18 ++--- include/gproshan/geometry/vec.h | 81 +++++++++++----------- include/gproshan/raytracing/rt_utils.h | 14 ++-- src/gproshan/geodesics/geodesics_ptp.cu | 4 +- 6 files changed, 64 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e9ffd69..427f96f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES native) endif() - find_package(OptiX 7.5) + find_package(OptiX 7.6) if(OptiX_INCLUDE) include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 9fd0f7ae..0c32dbd1 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -37,10 +37,10 @@ struct is_ok { const real_t * error = nullptr; - __host__ __device__ + __host_device__ bool operator()(const real_t & val) const; - __host__ __device__ + __host_device__ bool operator()(const index_t & val) const; }; @@ -74,7 +74,7 @@ template #ifdef __CUDACC__ __forceinline__ #endif -__host__ __device__ +__host_device__ real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) { const vec X[2] = {mesh->GT[x[0]] - mesh->GT[x[2]], @@ -130,7 +130,7 @@ template #ifdef __CUDACC__ __forceinline__ #endif -__host__ __device__ +__host_device__ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t & v) { real_t & ndv = new_dist[v] = old_dist[v]; diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 558c9644..eb799302 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -18,7 +18,7 @@ class mat row rows[N]; public: - __host__ __device__ + __host_device__ mat(const std::initializer_list > & list = {}) { int i = -1; @@ -26,33 +26,33 @@ class mat rows[++i] = r; } - __host__ __device__ + __host_device__ T & operator () (const index_t & i, const index_t & j) { return rows[i][j]; } - __host__ __device__ + __host_device__ const T & operator () (const index_t & i, const index_t & j) const { return rows[i][j]; } - __host__ __device__ + __host_device__ row & operator [] (const index_t & i) { assert(i < N); return rows[i]; } - __host__ __device__ + __host_device__ const row & operator [] (const index_t & i) const { assert(i < N); return rows[i]; } - __host__ __device__ + __host_device__ mat operator * (const mat & b) const { mat res; @@ -63,7 +63,7 @@ class mat return res; } - __host__ __device__ + __host_device__ vec operator * (const vec & v) const { vec res; @@ -72,7 +72,7 @@ class mat return res; } - __host__ __device__ + __host_device__ static mat identity() { mat res; @@ -81,7 +81,7 @@ class mat return res; } - __host__ __device__ + __host_device__ static mat transpose(const mat & m) { mat res; diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 9f57cd58..9ab5b904 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -8,10 +8,11 @@ #include #include -#ifndef __CUDACC__ - #define __host__ - #define __device__ -#endif +#ifdef __CUDACC__ + #define __host_device__ __host__ __device__ +#else + #define __host_device__ +#endif // __CUDACC__ // geometry processing and shape analysis framework @@ -28,7 +29,7 @@ class vec T values[N] = {}; public: - __host__ __device__ + __host_device__ vec(const std::initializer_list & list) { int i = -1; @@ -36,7 +37,7 @@ class vec values[++i] = v; } - __host__ __device__ + __host_device__ vec(const vec & v, const T & val = 0) { for(index_t i = 0; i < N - 1; ++i) @@ -44,70 +45,70 @@ class vec values[N - 1] = val; } - __host__ __device__ + __host_device__ vec(const vec & v) { for(index_t i = 0; i < N; ++i) values[i] = v[i]; } - __host__ __device__ + __host_device__ vec(const T & val = 0) { for(T & v: values) v = val; } - __host__ __device__ + __host_device__ T & operator [] (const index_t & i) { assert(i < N); return values[i]; } - __host__ __device__ + __host_device__ const T & operator [] (const index_t & i) const { assert(i < N); return values[i]; } - __host__ __device__ + __host_device__ T & x() { assert(N > 0); return values[0]; } - __host__ __device__ + __host_device__ const T & x() const { assert(N > 0); return values[0]; } - __host__ __device__ + __host_device__ T & y() { assert(N > 1); return values[1]; } - __host__ __device__ + __host_device__ const T & y() const { assert(N > 1); return values[1]; } - __host__ __device__ + __host_device__ T & z() { assert(N > 2); return values[2]; } - __host__ __device__ + __host_device__ const T & z() const { assert(N > 2); @@ -116,7 +117,7 @@ class vec ///< norm - __host__ __device__ + __host_device__ T norm() const { T res = 0; @@ -126,14 +127,14 @@ class vec } ///< length - __host__ __device__ + __host_device__ T length() const { return norm(); } ///< scalar product - __host__ __device__ + __host_device__ vec operator * (const T & a) const { vec res; @@ -143,7 +144,7 @@ class vec } ///< element wise product - __host__ __device__ + __host_device__ vec operator * (const vec & v) const { vec res; @@ -153,7 +154,7 @@ class vec } ///< scalar division - __host__ __device__ + __host_device__ vec operator / (const T & a) const { vec res; @@ -163,7 +164,7 @@ class vec } ///< sum of vectors - __host__ __device__ + __host_device__ vec operator + (const vec & v) const { vec res; @@ -173,7 +174,7 @@ class vec } ///< difference of vectors - __host__ __device__ + __host_device__ vec operator - (const vec & v) const { vec res; @@ -183,7 +184,7 @@ class vec } ///< negative of vector - __host__ __device__ + __host_device__ vec operator - () const { vec res; @@ -193,7 +194,7 @@ class vec } ///< scalar product self assign - __host__ __device__ + __host_device__ const vec & operator *= (const T & a) { for(T & v: values) @@ -202,7 +203,7 @@ class vec } ///< element wise product self assign - __host__ __device__ + __host_device__ const vec & operator *= (const vec & v) { for(index_t i = 0; i < N; ++i) @@ -211,7 +212,7 @@ class vec } ///< scalar division self assign - __host__ __device__ + __host_device__ const vec & operator /= (const T & a) { for(T & v: values) @@ -220,7 +221,7 @@ class vec } ///< sum of vectors self assign - __host__ __device__ + __host_device__ const vec & operator += (const vec & v) { for(index_t i = 0; i < N; ++i) @@ -229,7 +230,7 @@ class vec } ///< difference of vectors self assign - __host__ __device__ + __host_device__ const vec & operator -= (const vec & v) { for(index_t i = 0; i < N; ++i) @@ -238,7 +239,7 @@ class vec } ///< comparison less than - __host__ __device__ + __host_device__ bool operator < (const vec & v) const { for(index_t i = 0; i < N; ++i) @@ -249,7 +250,7 @@ class vec } ///< comparison equal than - __host__ __device__ + __host_device__ bool operator == (const vec & v) const { for(index_t i = 0; i < N; ++i) @@ -259,7 +260,7 @@ class vec return true; } - __host__ __device__ + __host_device__ bool is_zero() { T eps = std::numeric_limits::epsilon(); @@ -274,7 +275,7 @@ class vec ///< scalar product template -__host__ __device__ +__host_device__ vec operator * (const T & a, const vec & v) { return v * a; @@ -282,7 +283,7 @@ vec operator * (const T & a, const vec & v) ///< cross product template -__host__ __device__ +__host_device__ vec cross(const vec & u, const vec & v) { const T & ux = u[0]; @@ -297,7 +298,7 @@ vec cross(const vec & u, const vec & v) ///< dot product template -__host__ __device__ +__host_device__ T dot(const vec & u, const vec & v) { T res = 0; @@ -308,7 +309,7 @@ T dot(const vec & u, const vec & v) ///< norm template -__host__ __device__ +__host_device__ T norm(const vec & v) { return v.norm(); @@ -316,7 +317,7 @@ T norm(const vec & v) ///< length template -__host__ __device__ +__host_device__ T length(const vec & v) { return v.length(); @@ -324,7 +325,7 @@ T length(const vec & v) ///< normalize vector: divide by its norm template -__host__ __device__ +__host_device__ vec normalize(const vec & v) { return v / norm(v); @@ -332,7 +333,7 @@ vec normalize(const vec & v) ///< std std::ostream template -__host__ __device__ +__host_device__ std::ostream & operator << (std::ostream & os, const vec & v) { for(index_t i = 0; i < N - 1; ++i) @@ -342,7 +343,7 @@ std::ostream & operator << (std::ostream & os, const vec & v) ///< std std::istream template -__host__ __device__ +__host_device__ std::istream & operator >> (std::istream & is, vec & v) { for(index_t i = 0; i < N; ++i) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 76764430..0039a5fa 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -17,7 +17,7 @@ struct random { uint32_t previous; - __host__ __device__ + __host_device__ random(uint32_t v0, uint32_t v1) { uint32_t s = 0; @@ -30,7 +30,7 @@ struct random previous = v0; } - __host__ __device__ + __host_device__ T operator () () { previous = previous * 1664525 + 1013904223; @@ -39,7 +39,7 @@ struct random }; template -__host__ __device__ +__host_device__ vec texture(const scene::texture & tex, const vec & coord) { const int i = (tex.width + int(coord.x() * (tex.width - 1))) % tex.width; @@ -73,10 +73,10 @@ struct t_eval_hit vec Ks{0.2f, 0.2f, 0.2f}; T Ns = 4; - __host__ __device__ + __host_device__ t_eval_hit() {} - __host__ __device__ + __host_device__ t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const params_scene & sc) { primID = aprimID; @@ -121,7 +121,7 @@ struct t_eval_hit }; template -__host__ __device__ +__host_device__ vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) { const T Lp = 10; @@ -161,7 +161,7 @@ using eval_hit = t_eval_hit; template -__host__ __device__ +__host_device__ vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) { vec2 screen = { (float(pos.x()) + 0.5f) / windows_size.x(), diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index bbd8e7b8..a8666fe0 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -262,13 +262,13 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ } } -__host__ __device__ +__host_device__ bool is_ok::operator()(const real_t & val) const { return val < PTP_TOL; } -__host__ __device__ +__host_device__ bool is_ok::operator()(const index_t & i) const { return error[i] < PTP_TOL; From 59a171eaa1b15b6ae03827515bed881edf93cca7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 6 Dec 2022 21:52:06 +0100 Subject: [PATCH 0791/1018] rt_embree: rendering with textures, virtual scanner updated --- include/gproshan/raytracing/rt_embree.h | 8 ++-- include/gproshan/raytracing/rt_optix_params.h | 2 +- include/gproshan/raytracing/rt_utils.h | 2 +- include/gproshan/scenes/scene.h | 2 +- src/gproshan/raytracing/rt_embree.cpp | 42 +++++++++++-------- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index ed64cad9..e27fd97a 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -2,6 +2,7 @@ #define RT_EMBREE_H #include +#include #include #include @@ -30,11 +31,12 @@ class embree : public raytracing }; - RTCDevice device; - RTCScene scene; - RTCIntersectContext intersect_context; + RTCDevice rtc_device; + RTCScene rtc_scene; + RTCIntersectContext rtc_intersect_context; std::vector g_meshes; + scene_data sc; float pc_radius = 1; diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/rt_optix_params.h index d7379160..0c6d4902 100644 --- a/include/gproshan/raytracing/rt_optix_params.h +++ b/include/gproshan/raytracing/rt_optix_params.h @@ -33,7 +33,7 @@ struct launch_params OptixTraversableHandle traversable; - params_scene sc; + scene_data sc; }; diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 0039a5fa..57935ca2 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -77,7 +77,7 @@ struct t_eval_hit t_eval_hit() {} __host_device__ - t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const params_scene & sc) + t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const scene_data & sc) { primID = aprimID; u = au; diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 9588178e..c24b2626 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -70,7 +70,7 @@ class scene: public che bool load_texture(const std::string & file); }; -struct params_scene +struct scene_data { scene::material * materials = nullptr; scene::texture * textures = nullptr; diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 40a911b0..0a838f99 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -59,13 +59,13 @@ void embree_error(void *, RTCError, const char * str) embree::embree() { - device = rtcNewDevice(NULL); - scene = rtcNewScene(device); + rtc_device = rtcNewDevice(NULL); + rtc_scene = rtcNewScene(rtc_device); - rtcSetSceneFlags(scene, RTC_SCENE_FLAG_COMPACT); + rtcSetSceneFlags(rtc_scene, RTC_SCENE_FLAG_COMPACT); - rtcInitIntersectContext(&intersect_context); - rtcSetDeviceErrorFunction(device, embree_error, NULL); + rtcInitIntersectContext(&rtc_intersect_context); + rtcSetDeviceErrorFunction(rtc_device, embree_error, NULL); } embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float & pcr): embree() @@ -79,8 +79,8 @@ embree::~embree() for(CHE * m: g_meshes) delete m; - rtcReleaseScene(scene); - rtcReleaseDevice(device); + rtcReleaseScene(rtc_scene); + rtcReleaseDevice(rtc_device); } index_t embree::closest_vertex(const vertex & org, const vertex & dir) @@ -115,7 +115,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, params_scene{}); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, sc); hit.dist = r.ray.tfar; hit.position = r.position(); @@ -139,12 +139,12 @@ void embree::build_bvh(const std::vector & meshes, const std::vectoris_scene()) @@ -202,6 +202,12 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) g_meshes[geom_id]->VT = tri_idxs; g_meshes[geom_id]->n_trigs = mesh->n_vertices / 3; g_meshes[geom_id]->n_half_edges = mesh->n_vertices; + + scene * psc = (scene *) mesh; + sc.materials = psc->materials.data(); + sc.textures = psc->textures.data(); + sc.trig_mat = psc->trig_mat; + sc.texcoords = psc->texcoords; } return geom_id; @@ -209,7 +215,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) { - RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + RTCGeometry geom = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, @@ -235,7 +241,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) rtcCommitGeometry(geom); - index_t geom_id = rtcAttachGeometry(scene, geom); + index_t geom_id = rtcAttachGeometry(rtc_scene, geom); rtcReleaseGeometry(geom); return geom_id; @@ -246,7 +252,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, params_scene{}); + eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, sc); hit.position = r.position(); hit.normal = flat ? r.normal() : hit.normal; @@ -266,13 +272,13 @@ float embree::intersect_depth(const vertex & org, const vertex & dir) bool embree::intersect(ray_hit & r) { - rtcIntersect1(scene, &intersect_context, &r); + rtcIntersect1(rtc_scene, &rtc_intersect_context, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } bool embree::occluded(ray_hit & r) { - rtcIntersect1(scene, &intersect_context, &r); + rtcIntersect1(rtc_scene, &rtc_intersect_context, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } From 8890017b831396f0fbe4bb83f53f359bf98da928 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 7 Dec 2022 22:08:06 +0100 Subject: [PATCH 0792/1018] che_ptx: fix add_trig --- src/gproshan/mesh/che_ptx.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 0792f53d..d7a50b14 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -94,9 +94,6 @@ void che_ptx::read_file(const std::string & file) index_t he = 0; auto add_trig = [&](const index_t & i, const index_t & j, const index_t & k) { - if(GT[i].is_zero() || GT[j].is_zero() || GT[k].is_zero()) - return; - VT[he++] = i; VT[he++] = j; VT[he++] = k; From de7fc61afc67d92b6e5de3f88a3748592f42d120 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Jan 2023 14:43:30 +0100 Subject: [PATCH 0793/1018] scenes (#14): cleaning up error messages --- src/gproshan/mesh/che_obj.cpp | 12 ++++++------ src/gproshan/scenes/scene.cpp | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 94b1f4dd..f8a729b9 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -169,12 +169,12 @@ che_obj::parser::parser(const std::string & file) objects.emplace_back("", trigs.size()); - gproshan_error_var(vertices.size()); - gproshan_error_var(vnormals.size()); - gproshan_error_var(vtexcoords.size()); - gproshan_error_var(vcolors.size()); - gproshan_error_var(trigs.size()); - gproshan_error_var(objects.size()); + gproshan_log_var(vertices.size()); + gproshan_log_var(vnormals.size()); + gproshan_log_var(vtexcoords.size()); + gproshan_log_var(vcolors.size()); + gproshan_log_var(trigs.size()); + gproshan_log_var(objects.size()); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 6031f79f..302f11e9 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -188,6 +188,7 @@ bool scene::load_mtl(const std::string & file) return false; } +/* for(index_t i = 0; i < materials.size(); ++i) { const material & m = materials[i]; @@ -204,6 +205,7 @@ bool scene::load_mtl(const std::string & file) if(m.map_d > -1) gproshan_log_var(texture_name[m.map_d]); if(m.map_bump > -1) gproshan_log_var(texture_name[m.map_bump]); } +*/ gproshan_log_var(materials.size()); gproshan_log_var(textures.size()); @@ -223,7 +225,6 @@ bool scene::load_texture(const std::string & file) tex.width = img.width(); tex.height = img.height(); tex.spectrum = img.spectrum(); - gproshan_error_var(tex.spectrum); tex.data = new unsigned char[tex.width * tex.height * tex.spectrum]; img.permute_axes("cxyz"); memcpy(tex.data, img.data(), tex.width * tex.height * tex.spectrum); From aa6bd606f874413d8ddd566fff91027bf42ad565 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 2 Feb 2023 17:12:55 +0100 Subject: [PATCH 0794/1018] update imgui to v1.89.2 --- include/imgui/LICENSE.txt | 2 +- include/imgui/imconfig.h | 31 +- include/imgui/imgui.h | 885 ++-- include/imgui/imgui_impl_glfw.h | 23 +- include/imgui/imgui_impl_opengl3.h | 6 +- include/imgui/imgui_impl_opengl3_loader.h | 257 +- include/imgui/imgui_internal.h | 871 +++- include/imgui/imstb_rectpack.h | 42 +- include/imgui/imstb_textedit.h | 50 +- include/imgui/imstb_truetype.h | 824 ++-- src/imgui/LICENSE.txt | 2 +- src/imgui/imgui.cpp | 5004 ++++++++++++++------- src/imgui/imgui_demo.cpp | 933 ++-- src/imgui/imgui_draw.cpp | 234 +- src/imgui/imgui_impl_glfw.cpp | 517 ++- src/imgui/imgui_impl_opengl3.cpp | 214 +- src/imgui/imgui_tables.cpp | 161 +- src/imgui/imgui_widgets.cpp | 1333 +++--- 18 files changed, 7310 insertions(+), 4079 deletions(-) diff --git a/include/imgui/LICENSE.txt b/include/imgui/LICENSE.txt index 780533dc..fb715bdc 100644 --- a/include/imgui/LICENSE.txt +++ b/include/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2021 Omar Cornut +Copyright (c) 2014-2023 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index f2556017..ed265082 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -27,13 +27,14 @@ //#define IMGUI_API __declspec( dllimport ) //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. -#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS +//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS +//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87: disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This will be folded into IMGUI_DISABLE_OBSOLETE_FUNCTIONS in a few versions. -//---- Disable all of Dear ImGui or don't implement standard windows. -// It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp. +//---- Disable all of Dear ImGui or don't implement standard windows/tools. +// It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp. //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. -//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended. -//#define IMGUI_DISABLE_METRICS_WINDOW // Disable metrics/debugger and other debug tools: ShowMetricsWindow() and ShowStackToolWindow() will be empty. +//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. +//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowStackToolWindow() will be empty (this was called IMGUI_DISABLE_METRICS_WINDOW before 1.88). //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) @@ -61,12 +62,13 @@ // By default the embedded implementations are declared static and not available outside of Dear ImGui sources files. //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" +//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if enabled //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION -//---- Use stb_printf's faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) -// Requires 'stb_sprintf.h' to be available in the include path. Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf. -// #define IMGUI_USE_STB_SPRINTF +//---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) +// Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h. +//#define IMGUI_USE_STB_SPRINTF //---- Use FreeType to build and rasterize the font atlas (instead of stb_truetype which is embedded by default in Dear ImGui) // Requires FreeType headers to be available in the include path. Requires program to be compiled with 'misc/freetype/imgui_freetype.cpp' (in this repository) + the FreeType library (not provided). @@ -80,12 +82,12 @@ //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4. // This will be inlined as part of ImVec2 and ImVec4 class declarations. /* -#define IM_VEC2_CLASS_EXTRA \ - ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \ +#define IM_VEC2_CLASS_EXTRA \ + constexpr ImVec2(const MyVec2& f) : x(f.x), y(f.y) {} \ operator MyVec2() const { return MyVec2(x,y); } -#define IM_VEC4_CLASS_EXTRA \ - ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \ +#define IM_VEC4_CLASS_EXTRA \ + constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \ operator MyVec4() const { return MyVec4(x,y,z,w); } */ @@ -106,11 +108,6 @@ //#define IM_DEBUG_BREAK IM_ASSERT(0) //#define IM_DEBUG_BREAK __debugbreak() -//---- Debug Tools: Have the Item Picker break in the ItemAdd() function instead of ItemHoverable(), -// (which comes earlier in the code, will catch a few extra items, allow picking items other than Hovered one.) -// This adds a small runtime cost which is why it is not enabled by default. -//#define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX - //---- Debug Tools: Enable slower asserts //#define IMGUI_DEBUG_PARANOID diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index e986e6c0..152f4f09 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (headers) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/4451 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/5243 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues @@ -20,6 +20,12 @@ // - For first-time users having issues compiling/linking/running or issues loading fonts: // please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. +// Library Version +// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM > 12345') +#define IMGUI_VERSION "1.89.2" +#define IMGUI_VERSION_NUM 18920 +#define IMGUI_HAS_TABLE + /* Index of this file: @@ -35,19 +41,19 @@ Index of this file: // [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) +// [SECTION] Platform Dependent Interfaces (ImGuiPlatformImeData) // [SECTION] Obsolete functions and types */ #pragma once -// Configuration file with compile-time options (edit imconfig.h or '#define IMGUI_USER_CONFIG "myfilename.h" from your build system') +// Configuration file with compile-time options +// (edit imconfig.h or '#define IMGUI_USER_CONFIG "myfilename.h" from your build system') #ifdef IMGUI_USER_CONFIG #include IMGUI_USER_CONFIG #endif -#if !defined(IMGUI_DISABLE_INCLUDE_IMCONFIG_H) || defined(IMGUI_INCLUDE_IMCONFIG_H) #include "imconfig.h" -#endif #ifndef IMGUI_DISABLE @@ -61,13 +67,6 @@ Index of this file: #include // ptrdiff_t, NULL #include // memset, memmove, memcpy, strlen, strchr, strcpy, strcmp -// Version -// (Integer encoded as XYYZZ for use in #if preprocessor conditionals. Work in progress versions typically starts at XYY99 then bounce up to XYY00, XYY01 etc. when release tagging happens) -#define IMGUI_VERSION "1.86" -#define IMGUI_VERSION_NUM 18600 -#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) -#define IMGUI_HAS_TABLE - // Define attributes of all API symbols declarations (e.g. for DLL under Windows) // IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default backends files (imgui_impl_xxx.h) // Using dear imgui via a shared library is not recommended, because we don't guarantee backward nor forward ABI compatibility (also function call overhead, as dear imgui is a call-heavy API) @@ -85,11 +84,8 @@ Index of this file: #endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. -#if (__cplusplus >= 201100) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201100) #define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 -#else -#define IM_OFFSETOF(_TYPE,_MEMBER) ((size_t)&(((_TYPE*)0)->_MEMBER)) // Offset of _MEMBER within _TYPE. Old style macro. -#endif +#define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. #if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) && !defined(__clang__) @@ -104,7 +100,7 @@ Index of this file: #endif // Disable some of MSVC most aggressive Debug runtime checks in function header/footer (used in some simple/low-level functions) -#if defined(_MSC_VER) && !defined(__clang__) && !defined(IMGUI_DEBUG_PARANOID) +#if defined(_MSC_VER) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(IMGUI_DEBUG_PARANOID) #define IM_MSVC_RUNTIME_CHECKS_OFF __pragma(runtime_checks("",off)) __pragma(check_stack(off)) __pragma(strict_gs_check(push,off)) #define IM_MSVC_RUNTIME_CHECKS_RESTORE __pragma(runtime_checks("",restore)) __pragma(check_stack()) __pragma(strict_gs_check(pop)) #else @@ -151,9 +147,11 @@ struct ImColor; // Helper functions to create a color that c struct ImGuiContext; // Dear ImGui context (opaque structure, unless including imgui_internal.h) struct ImGuiIO; // Main configuration and I/O between your application and ImGui struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) +struct ImGuiKeyData; // Storage for ImGuiIO and IsKeyDown(), IsKeyPressed() etc functions. struct ImGuiListClipper; // Helper to manually clip large list of items struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame struct ImGuiPayload; // User data payload for drag and drop operations +struct ImGuiPlatformImeData; // Platform IME data for io.SetPlatformImeDataFn() function. struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) struct ImGuiStorage; // Helper for key->value storage struct ImGuiStyle; // Runtime data for styling/colors @@ -163,21 +161,26 @@ struct ImGuiTextBuffer; // Helper to hold and append into a text buf struct ImGuiTextFilter; // Helper to parse and apply text filters (e.g. "aaaaa[,bbbbb][,ccccc]") struct ImGuiViewport; // A Platform Window (always only one in 'master' branch), in the future may represent Platform Monitor -// Enums/Flags (declared as int for compatibility with old C++, to allow using as flags without overhead, and to not pollute the top of this file) +// Enumerations +// - We don't use strongly typed enums much because they add constraints (can't extend in private code, can't store typed in bit fields, extra casting on iteration) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! -// In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +enum ImGuiKey : int; // -> enum ImGuiKey // Enum: A key identifier (ImGuiKey_XXX or ImGuiMod_XXX value) typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type typedef int ImGuiDir; // -> enum ImGuiDir_ // Enum: A cardinal direction -typedef int ImGuiKey; // -> enum ImGuiKey_ // Enum: A key identifier (ImGui-side enum) -typedef int ImGuiNavInput; // -> enum ImGuiNavInput_ // Enum: An input identifier for navigation typedef int ImGuiMouseButton; // -> enum ImGuiMouseButton_ // Enum: A mouse button identifier (0=left, 1=right, 2=middle) -typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor identifier +typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor shape typedef int ImGuiSortDirection; // -> enum ImGuiSortDirection_ // Enum: A sorting direction (ascending or descending) typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A color target for TableSetBgColor() + +// Flags (declared as int for compatibility with old C++, to allow using as flags without overhead, and to not pollute the top of this file) +// - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! +// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. typedef int ImDrawFlags; // -> enum ImDrawFlags_ // Flags: for ImDrawList functions typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList instance typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build @@ -190,7 +193,7 @@ typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: f typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() -typedef int ImGuiKeyModFlags; // -> enum ImGuiKeyModFlags_ // Flags: for io.KeyMods (Ctrl/Shift/Alt/Super) +typedef int ImGuiKeyChord; // -> ImGuiKey | ImGuiMod_XXX // Flags: for storage only for now: an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values. typedef int ImGuiPopupFlags; // -> enum ImGuiPopupFlags_ // Flags: for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. @@ -225,17 +228,8 @@ typedef signed short ImS16; // 16-bit signed integer typedef unsigned short ImU16; // 16-bit unsigned integer typedef signed int ImS32; // 32-bit signed integer == int typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors) -#if defined(_MSC_VER) && !defined(__clang__) -typedef signed __int64 ImS64; // 64-bit signed integer (pre and post C++11 with Visual Studio) -typedef unsigned __int64 ImU64; // 64-bit unsigned integer (pre and post C++11 with Visual Studio) -#elif (defined(__clang__) || defined(__GNUC__)) && (__cplusplus < 201100) -#include -typedef int64_t ImS64; // 64-bit signed integer (pre C++11) -typedef uint64_t ImU64; // 64-bit unsigned integer (pre C++11) -#else -typedef signed long long ImS64; // 64-bit signed integer (post C++11) -typedef unsigned long long ImU64; // 64-bit unsigned integer (post C++11) -#endif +typedef signed long long ImS64; // 64-bit signed integer +typedef unsigned long long ImU64; // 64-bit unsigned integer // Character types // (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) @@ -259,10 +253,10 @@ IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec2 { float x, y; - ImVec2() { x = y = 0.0f; } - ImVec2(float _x, float _y) { x = _x; y = _y; } - float operator[] (size_t idx) const { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. - float& operator[] (size_t idx) { IM_ASSERT(idx <= 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + constexpr ImVec2() : x(0.0f), y(0.0f) { } + constexpr ImVec2(float _x, float _y) : x(_x), y(_y) { } + float operator[] (size_t idx) const { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + float& operator[] (size_t idx) { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. #ifdef IM_VEC2_CLASS_EXTRA IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2. #endif @@ -271,9 +265,9 @@ struct ImVec2 // ImVec4: 4D vector used to store clipping rectangles, colors etc. [Compile-time configurable type] struct ImVec4 { - float x, y, z, w; - ImVec4() { x = y = z = w = 0.0f; } - ImVec4(float _x, float _y, float _z, float _w) { x = _x; y = _y; z = _z; w = _w; } + float x, y, z, w; + constexpr ImVec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) { } + constexpr ImVec4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w) { } #ifdef IM_VEC4_CLASS_EXTRA IM_VEC4_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec4. #endif @@ -307,12 +301,13 @@ namespace ImGui // Demo, Debug, Information IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window. demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debugger window. display Dear ImGui internals: windows, draw commands, various internal state, etc. + IMGUI_API void ShowDebugLogWindow(bool* p_open = NULL); // create Debug Log window. display a simplified log of important dear imgui events. IMGUI_API void ShowStackToolWindow(bool* p_open = NULL); // create Stack Tool window. hover items with mouse to query information about the source of their unique ID. IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. IMGUI_API void ShowFontSelector(const char* label); // add font selector block (not a window), essentially a combo listing the loaded fonts. - IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as a end-user (mouse/keyboard controls). + IMGUI_API void ShowUserGuide(); // add basic help/info block (not a window): how to manipulate ImGui as an end-user (mouse/keyboard controls). IMGUI_API const char* GetVersion(); // get the compiled version string e.g. "1.80 WIP" (essentially the value for IMGUI_VERSION from the compiled version of imgui.cpp) // Styles @@ -367,6 +362,7 @@ namespace ImGui IMGUI_API void SetNextWindowContentSize(const ImVec2& size); // set next window content size (~ scrollable client area, which enforce the range of scrollbars). Not including window decorations (title bar, menu bar, etc.) nor WindowPadding. set an axis to 0.0f to leave it automatic. call before Begin() IMGUI_API void SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // set next window collapsed state. call before Begin() IMGUI_API void SetNextWindowFocus(); // set next window to be focused / top-most. call before Begin() + IMGUI_API void SetNextWindowScroll(const ImVec2& scroll); // set next window scrolling value (use < 0.0f to not affect a given axis). IMGUI_API void SetNextWindowBgAlpha(float alpha); // set next window background color alpha. helper to easily override the Alpha component of ImGuiCol_WindowBg/ChildBg/PopupBg. you may also use ImGuiWindowFlags_NoBackground. IMGUI_API void SetWindowPos(const ImVec2& pos, ImGuiCond cond = 0); // (not recommended) set current window position - call within Begin()/End(). prefer using SetNextWindowPos(), as this may incur tearing and side-effects. IMGUI_API void SetWindowSize(const ImVec2& size, ImGuiCond cond = 0); // (not recommended) set current window size - call within Begin()/End(). set to ImVec2(0, 0) to force an auto-fit. prefer using SetNextWindowSize(), as this may incur tearing and minor side-effects. @@ -384,9 +380,11 @@ namespace ImGui IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min for the full window (roughly (0,0)-Scroll), in window coordinates - IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max for the full window (roughly (0,0)+Size-Scroll) where Size can be override with SetNextWindowContentSize(), in window coordinates + IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max for the full window (roughly (0,0)+Size-Scroll) where Size can be overridden with SetNextWindowContentSize(), in window coordinates // Windows Scrolling + // - Any change of Scroll will be applied at the beginning of next frame in the first call to Begin(). + // - You may instead use SetNextWindowScroll() prior to calling Begin() to avoid this delay, as an alternative to using SetScrollX()/SetScrollY(). IMGUI_API float GetScrollX(); // get scrolling amount [0 .. GetScrollMaxX()] IMGUI_API float GetScrollY(); // get scrolling amount [0 .. GetScrollMaxY()] IMGUI_API void SetScrollX(float scroll_x); // set scrolling amount [0 .. GetScrollMaxX()] @@ -421,7 +419,7 @@ namespace ImGui IMGUI_API void PopTextWrapPos(); // Style read access - // - Use the style editor (ShowStyleEditor() function) to interactively see what the colors are) + // - Use the ShowStyleEditor() function to interactively see/edit the colors. IMGUI_API ImFont* GetFont(); // get current font IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API @@ -439,7 +437,7 @@ namespace ImGui // Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f); // call between widgets or groups to layout them horizontally. X position given in window coordinates. - IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in an horizontal-layout context. + IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in a horizontal-layout context. IMGUI_API void Spacing(); // add vertical spacing. IMGUI_API void Dummy(const ImVec2& size); // add a dummy item of given size. unlike InvisibleButton(), Dummy() won't take the mouse click or be navigable into. IMGUI_API void Indent(float indent_w = 0.0f); // move content position toward the right, by indent_w, or style.IndentSpacing if indent_w <= 0 @@ -503,8 +501,6 @@ namespace ImGui IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size, ImGuiButtonFlags flags = 0); // flexible button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape - IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), const ImVec4& tint_col = ImVec4(1,1,1,1), const ImVec4& border_col = ImVec4(0,0,0,0)); - IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1,1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0,0,0,0), const ImVec4& tint_col = ImVec4(1,1,1,1)); // <0 frame_padding uses default frame padding settings. 0 for no padding IMGUI_API bool Checkbox(const char* label, bool* v); IMGUI_API bool CheckboxFlags(const char* label, int* flags, int flags_value); IMGUI_API bool CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value); @@ -513,7 +509,12 @@ namespace ImGui IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-FLT_MIN, 0), const char* overlay = NULL); IMGUI_API void Bullet(); // draw a small circle + keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses - // Widgets: Combo Box + // Widgets: Images + // - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples + IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), const ImVec4& border_col = ImVec4(0, 0, 0, 0)); + IMGUI_API bool ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); + + // Widgets: Combo Box (Dropdown) // - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. // - The old Combo() api are helpers over BeginCombo()/EndCombo() which are kept available for convenience purpose. This is analogous to how ListBox are created. IMGUI_API bool BeginCombo(const char* label, const char* preview_value, ImGuiComboFlags flags = 0); @@ -524,14 +525,15 @@ namespace ImGui // Widgets: Drag Sliders // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped by default and can go off-bounds. Use ImGuiSliderFlags_AlwaysClamp to always clamp. - // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every functions, note that a 'float v[X]' function argument is the same as 'float* v', the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x + // - For all the Float2/Float3/Float4/Int2/Int3/Int4 versions of every function, note that a 'float v[X]' function argument is the same as 'float* v', + // the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Format string may also be set to NULL or use the default format ("%f" or "%d"). // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits if ImGuiSliderFlags_AlwaysClamp is not used. // - Use v_max = FLT_MAX / INT_MAX etc to avoid clamping to a maximum, same with v_min = -FLT_MAX / INT_MIN to avoid clamping to a minimum. // - We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. - // - Legacy: Pre-1.78 there are DragXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. + // - Legacy: Pre-1.78 there are DragXXX() function signatures that take a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. // If you get a warning converting a float to ImGuiSliderFlags, read https://github.com/ocornut/imgui/issues/3361 IMGUI_API bool DragFloat(const char* label, float* v, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); // If v_min >= v_max we have no bound IMGUI_API bool DragFloat2(const char* label, float v[2], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", ImGuiSliderFlags flags = 0); @@ -550,7 +552,7 @@ namespace ImGui // - CTRL+Click on any slider to turn them into an input box. Manually input values aren't clamped by default and can go off-bounds. Use ImGuiSliderFlags_AlwaysClamp to always clamp. // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Format string may also be set to NULL or use the default format ("%f" or "%d"). - // - Legacy: Pre-1.78 there are SliderXXX() function signatures that takes a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. + // - Legacy: Pre-1.78 there are SliderXXX() function signatures that take a final `float power=1.0f' argument instead of the `ImGuiSliderFlags flags=0' argument. // If you get a warning converting a float to ImGuiSliderFlags, read https://github.com/ocornut/imgui/issues/3361 IMGUI_API bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); // adjust format to decorate the value with a prefix or a suffix for in-slider labels or unit display. IMGUI_API bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format = "%.3f", ImGuiSliderFlags flags = 0); @@ -592,7 +594,7 @@ namespace ImGui IMGUI_API bool ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags flags = 0); IMGUI_API bool ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags flags = 0, const float* ref_col = NULL); - IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, ImVec2 size = ImVec2(0, 0)); // display a color square/button, hover for details, return true when pressed. + IMGUI_API bool ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // display a color square/button, hover for details, return true when pressed. IMGUI_API void SetColorEditOptions(ImGuiColorEditFlags flags); // initialize current options (generally on application startup) if you want to select a default format, picker type, etc. User will be able to change many settings, unless you pass the _NoOptions flag to your calls. // Widgets: Trees @@ -608,7 +610,7 @@ namespace ImGui IMGUI_API bool TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API bool TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API void TreePush(const char* str_id); // ~ Indent()+PushId(). Already called by TreeNode() when returning true, but you can call TreePush/TreePop yourself if desired. - IMGUI_API void TreePush(const void* ptr_id = NULL); // " + IMGUI_API void TreePush(const void* ptr_id); // " IMGUI_API void TreePop(); // ~ Unindent()+PopId() IMGUI_API float GetTreeNodeToLabelSpacing(); // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode IMGUI_API bool CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0); // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop(). @@ -678,7 +680,7 @@ namespace ImGui // Popups: begin/end functions // - BeginPopup(): query popup state, if open start appending into the window. Call EndPopup() afterwards. ImGuiWindowFlags are forwarded to the window. - // - BeginPopupModal(): block every interactions behind the window, cannot be closed by user, add a dimming background, has a title bar. + // - BeginPopupModal(): block every interaction behind the window, cannot be closed by user, add a dimming background, has a title bar. IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // return true if the modal is open, and you can start outputting to it. IMGUI_API void EndPopup(); // only call EndPopup() if BeginPopupXXX() returns true! @@ -690,6 +692,7 @@ namespace ImGui // - CloseCurrentPopup() is called by default by Selectable()/MenuItem() when activated (FIXME: need some options). // - Use ImGuiPopupFlags_NoOpenOverExistingPopup to avoid opening a popup if there's already one at the same level. This is equivalent to e.g. testing for !IsAnyPopupOpen() prior to OpenPopup(). // - Use IsWindowAppearing() after BeginPopup() to tell if a window just opened. + // - IMPORTANT: Notice that for OpenPopupOnItemClick() we exceptionally default flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter IMGUI_API void OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags = 0); // call to mark popup as open (don't call every frame!). IMGUI_API void OpenPopup(ImGuiID id, ImGuiPopupFlags popup_flags = 0); // id overload to facilitate calling from nested stacks IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // helper to open popup when clicked on last item. Default to ImGuiPopupFlags_MouseButtonRight == 1. (note: actually triggers on the mouse _released_ event to be consistent with popup behaviors) @@ -699,7 +702,7 @@ namespace ImGui // - Helpers to do OpenPopup+BeginPopup where the Open action is triggered by e.g. hovering an item and right-clicking. // - They are convenient to easily create context menus, hence the name. // - IMPORTANT: Notice that BeginPopupContextXXX takes ImGuiPopupFlags just like OpenPopup() and unlike BeginPopup(). For full consistency, we may add ImGuiWindowFlags to the BeginPopupContextXXX functions in the future. - // - IMPORTANT: we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight. + // - IMPORTANT: Notice that we exceptionally default their flags to 1 (== ImGuiPopupFlags_MouseButtonRight) for backward compatibility with older API taking 'int mouse_button = 1' parameter, so if you add other flags remember to re-add the ImGuiPopupFlags_MouseButtonRight. IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked on last item. Use str_id==NULL to associate the popup to previous item. If you want to use that on a non-interactive item such as Text() you need to pass in an explicit ID here. read comments in .cpp! IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);// open+begin popup when clicked on current window. IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1); // open+begin popup when clicked in void (where there are no windows). @@ -711,19 +714,17 @@ namespace ImGui IMGUI_API bool IsPopupOpen(const char* str_id, ImGuiPopupFlags flags = 0); // return true if the popup is open. // Tables - // [BETA API] API may evolve slightly! If you use this, please update to the next version when it comes out! // - Full-featured replacement for old Columns API. - // - See Demo->Tables for demo code. - // - See top of imgui_tables.cpp for general commentary. + // - See Demo->Tables for demo code. See top of imgui_tables.cpp for general commentary. // - See ImGuiTableFlags_ and ImGuiTableColumnFlags_ enums for a description of available flags. // The typical call flow is: - // - 1. Call BeginTable(). + // - 1. Call BeginTable(), early out if returning false. // - 2. Optionally call TableSetupColumn() to submit column name/flags/defaults. // - 3. Optionally call TableSetupScrollFreeze() to request scroll freezing of columns/rows. // - 4. Optionally call TableHeadersRow() to submit a header row. Names are pulled from TableSetupColumn() data. // - 5. Populate contents: // - In most situations you can use TableNextRow() + TableSetColumnIndex(N) to start appending into a column. - // - If you are using tables as a sort of grid, where every columns is holding the same type of contents, + // - If you are using tables as a sort of grid, where every column is holding the same type of contents, // you may prefer using TableNextColumn() instead of TableNextRow() + TableSetColumnIndex(). // TableNextColumn() will automatically wrap-around into the next row if needed. // - IMPORTANT: Comparatively to the old Columns() API, we need to call TableNextColumn() for the first column! @@ -736,10 +737,10 @@ namespace ImGui // -------------------------------------------------------------------------------------------------------- // - 5. Call EndTable() IMGUI_API bool BeginTable(const char* str_id, int column, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0.0f, 0.0f), float inner_width = 0.0f); - IMGUI_API void EndTable(); // only call EndTable() if BeginTable() returns true! + IMGUI_API void EndTable(); // only call EndTable() if BeginTable() returns true! IMGUI_API void TableNextRow(ImGuiTableRowFlags row_flags = 0, float min_row_height = 0.0f); // append into the first cell of a new row. - IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return true when column is visible. - IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return true when column is visible. + IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return true when column is visible. + IMGUI_API bool TableSetColumnIndex(int column_n); // append into the specified column. Return true when column is visible. // Tables: Headers & Columns declaration // - Use TableSetupColumn() to specify label, resizing policy, default width/weight, id, various other flags etc. @@ -750,20 +751,17 @@ namespace ImGui // some advanced use cases (e.g. adding custom widgets in header row). // - Use TableSetupScrollFreeze() to lock columns/rows so they stay visible when scrolled. IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = 0.0f, ImGuiID user_id = 0); - IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. - IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu - IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) - - // Tables: Sorting - // - Call TableGetSortSpecs() to retrieve latest sort specs for the table. NULL when not sorting. - // - When 'SpecsDirty == true' you should sort your data. It will be true when sorting specs have changed - // since last call, or the first time. Make sure to set 'SpecsDirty = false' after sorting, else you may - // wastefully sort your data every frame! - // - Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). - IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). - - // Tables: Miscellaneous functions + IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. + IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu + IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) + + // Tables: Sorting & Miscellaneous functions + // - Sorting: call TableGetSortSpecs() to retrieve latest sort specs for the table. NULL when not sorting. + // When 'sort_specs->SpecsDirty == true' you should sort your data. It will be true when sorting specs have + // changed since last call, or the first time. Make sure to set 'SpecsDirty = false' after sorting, + // else you may wastefully sort your data every frame! // - Functions args 'int column_n' treat the default value of -1 as the same as passing the current column index. + IMGUI_API ImGuiTableSortSpecs* TableGetSortSpecs(); // get latest sort specs for the table (NULL if not sorting). Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable(). IMGUI_API int TableGetColumnCount(); // return number of columns (value passed to BeginTable) IMGUI_API int TableGetColumnIndex(); // return current column index. IMGUI_API int TableGetRowIndex(); // return current row index. @@ -784,6 +782,7 @@ namespace ImGui IMGUI_API int GetColumnsCount(); // Tab Bars, Tabs + // - Note: Tabs are automatically created by the docking system (when in 'docking' branch). Use this to create tab bars/tabs yourself. IMGUI_API bool BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0); // create and append into a TabBar IMGUI_API void EndTabBar(); // only call EndTabBar() if BeginTabBar() returns true! IMGUI_API bool BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0); // create a Tab. Returns true if the Tab is selected. @@ -807,7 +806,7 @@ namespace ImGui // - If you stop calling BeginDragDropSource() the payload is preserved however it won't have a preview tooltip (we currently display a fallback "..." tooltip, see #1725) // - An item can be both drag source and drop target. IMGUI_API bool BeginDragDropSource(ImGuiDragDropFlags flags = 0); // call after submitting an item which may be dragged. when this return true, you can call SetDragDropPayload() + EndDragDropSource() - IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. + IMGUI_API bool SetDragDropPayload(const char* type, const void* data, size_t sz, ImGuiCond cond = 0); // type is a user defined string of maximum 32 characters. Strings starting with '_' are reserved for dear imgui internal types. Data is copied and held by imgui. Return true when payload has been accepted. IMGUI_API void EndDragDropSource(); // only call EndDragDropSource() if BeginDragDropSource() returns true! IMGUI_API bool BeginDragDropTarget(); // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget() IMGUI_API const ImGuiPayload* AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags = 0); // accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released. @@ -837,16 +836,17 @@ namespace ImGui IMGUI_API bool IsItemHovered(ImGuiHoveredFlags flags = 0); // is the last item hovered? (and usable, aka not blocked by a popup, etc.). See ImGuiHoveredFlags for more options. IMGUI_API bool IsItemActive(); // is the last item active? (e.g. button being held, text field being edited. This will continuously return true while holding mouse button on an item. Items that don't interact will always return false) IMGUI_API bool IsItemFocused(); // is the last item focused for keyboard/gamepad navigation? - IMGUI_API bool IsItemClicked(ImGuiMouseButton mouse_button = 0); // is the last item hovered and mouse clicked on? (**) == IsMouseClicked(mouse_button) && IsItemHovered()Important. (**) this it NOT equivalent to the behavior of e.g. Button(). Read comments in function definition. + IMGUI_API bool IsItemClicked(ImGuiMouseButton mouse_button = 0); // is the last item hovered and mouse clicked on? (**) == IsMouseClicked(mouse_button) && IsItemHovered()Important. (**) this is NOT equivalent to the behavior of e.g. Button(). Read comments in function definition. IMGUI_API bool IsItemVisible(); // is the last item visible? (items may be out of sight because of clipping/scrolling) IMGUI_API bool IsItemEdited(); // did the last item modify its underlying value this frame? or was pressed? This is generally the same as the "bool" return value of many widgets. IMGUI_API bool IsItemActivated(); // was the last item just made active (item was previously inactive). - IMGUI_API bool IsItemDeactivated(); // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that requires continuous editing. - IMGUI_API bool IsItemDeactivatedAfterEdit(); // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that requires continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item). + IMGUI_API bool IsItemDeactivated(); // was the last item just made inactive (item was previously active). Useful for Undo/Redo patterns with widgets that require continuous editing. + IMGUI_API bool IsItemDeactivatedAfterEdit(); // was the last item just made inactive and made a value change when it was active? (e.g. Slider/Drag moved). Useful for Undo/Redo patterns with widgets that require continuous editing. Note that you may get false positives (some widgets such as Combo()/ListBox()/Selectable() will return true even when clicking an already selected item). IMGUI_API bool IsItemToggledOpen(); // was the last item open state toggled? set by TreeNode(). IMGUI_API bool IsAnyItemHovered(); // is any item hovered? IMGUI_API bool IsAnyItemActive(); // is any item active? IMGUI_API bool IsAnyItemFocused(); // is any item focused? + IMGUI_API ImGuiID GetItemID(); // get ID of last item (~~ often same ImGui::GetID(label) beforehand) IMGUI_API ImVec2 GetItemRectMin(); // get upper-left bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectMax(); // get lower-right bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectSize(); // get size of last item @@ -858,13 +858,15 @@ namespace ImGui // - In the future we will extend this concept further to also represent Platform Monitor and support a "no main platform window" operation mode. IMGUI_API ImGuiViewport* GetMainViewport(); // return primary/default viewport. This can never be NULL. + // Background/Foreground Draw Lists + IMGUI_API ImDrawList* GetBackgroundDrawList(); // this draw list will be the first rendered one. Useful to quickly draw shapes/text behind dear imgui contents. + IMGUI_API ImDrawList* GetForegroundDrawList(); // this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. + // Miscellaneous Utilities IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped. IMGUI_API bool IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max); // test if rectangle (in screen space) is visible / not clipped. to perform coarse clipping on user's side. IMGUI_API double GetTime(); // get global imgui time. incremented by io.DeltaTime every frame. IMGUI_API int GetFrameCount(); // get global imgui frame count. incremented by 1 every frame. - IMGUI_API ImDrawList* GetBackgroundDrawList(); // this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. - IMGUI_API ImDrawList* GetForegroundDrawList(); // this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. IMGUI_API ImDrawListSharedData* GetDrawListSharedData(); // you may use this when creating your own ImDrawList instances. IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). IMGUI_API void SetStateStorage(ImGuiStorage* storage); // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it) @@ -881,17 +883,19 @@ namespace ImGui IMGUI_API void ColorConvertRGBtoHSV(float r, float g, float b, float& out_h, float& out_s, float& out_v); IMGUI_API void ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& out_g, float& out_b); - // Inputs Utilities: Keyboard - // - For 'int user_key_index' you can use your own indices/enums according to how your backend/engine stored them in io.KeysDown[]. - // - We don't know the meaning of those value. You can use GetKeyIndex() to map a ImGuiKey_ value into the user index. - IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key] - IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index]. - IMGUI_API bool IsKeyPressed(int user_key_index, bool repeat = true); // was key pressed (went from !Down to Down)? if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate - IMGUI_API bool IsKeyReleased(int user_key_index); // was key released (went from Down to !Down)? - IMGUI_API int GetKeyPressedAmount(int key_index, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate - IMGUI_API void CaptureKeyboardFromApp(bool want_capture_keyboard_value = true); // attention: misleading name! manually override io.WantCaptureKeyboard flag next frame (said flag is entirely left for your application to handle). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard_value"; after the next NewFrame() call. - - // Inputs Utilities: Mouse + // Inputs Utilities: Keyboard/Mouse/Gamepad + // - the ImGuiKey enum contains all possible keyboard, mouse and gamepad inputs (e.g. ImGuiKey_A, ImGuiKey_MouseLeft, ImGuiKey_GamepadDpadUp...). + // - before v1.87, we used ImGuiKey to carry native/user indices as defined by each backends. About use of those legacy ImGuiKey values: + // - without IMGUI_DISABLE_OBSOLETE_KEYIO (legacy support): you can still use your legacy native/user indices (< 512) according to how your backend/engine stored them in io.KeysDown[], but need to cast them to ImGuiKey. + // - with IMGUI_DISABLE_OBSOLETE_KEYIO (this is the way forward): any use of ImGuiKey will assert with key < 512. GetKeyIndex() is pass-through and therefore deprecated (gone if IMGUI_DISABLE_OBSOLETE_KEYIO is defined). + IMGUI_API bool IsKeyDown(ImGuiKey key); // is key being held. + IMGUI_API bool IsKeyPressed(ImGuiKey key, bool repeat = true); // was key pressed (went from !Down to Down)? if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate + IMGUI_API bool IsKeyReleased(ImGuiKey key); // was key released (went from Down to !Down)? + IMGUI_API int GetKeyPressedAmount(ImGuiKey key, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate + IMGUI_API const char* GetKeyName(ImGuiKey key); // [DEBUG] returns English name of the key. Those names a provided for debugging purpose and are not meant to be saved persistently not compared. + IMGUI_API void SetNextFrameWantCaptureKeyboard(bool want_capture_keyboard); // Override io.WantCaptureKeyboard flag next frame (said flag is left for your application to handle, typically when true it instructs your app to ignore inputs). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard"; after the next NewFrame() call. + + // Inputs Utilities: Mouse specific // - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right. // - You can also use regular integer: it is forever guaranteed that 0=Left, 1=Right, 2=Middle. // - Dragging operations are only reported after mouse has moved a certain distance away from the initial clicking position (see 'lock_threshold' and 'io.MouseDraggingThreshold') @@ -902,15 +906,15 @@ namespace ImGui IMGUI_API int GetMouseClickedCount(ImGuiMouseButton button); // return the number of successive mouse-clicks at the time where a click happen (otherwise 0). IMGUI_API bool IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip = true);// is mouse hovering given bounding rect (in screen space). clipped by current clipping settings, but disregarding of other consideration of focus/window ordering/popup-block. IMGUI_API bool IsMousePosValid(const ImVec2* mouse_pos = NULL); // by convention we use (-FLT_MAX,-FLT_MAX) to denote that there is no mouse available - IMGUI_API bool IsAnyMouseDown(); // is any mouse button held? + IMGUI_API bool IsAnyMouseDown(); // [WILL OBSOLETE] is any mouse button held? This was designed for backends, but prefer having backend maintain a mask of held mouse buttons, because upcoming input queue system will make this invalid. IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve mouse position at the time of opening popup we have BeginPopup() into (helper to avoid user backing that value themselves) IMGUI_API bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f); // is mouse dragging? (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) IMGUI_API ImVec2 GetMouseDragDelta(ImGuiMouseButton button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) IMGUI_API void ResetMouseDragDelta(ImGuiMouseButton button = 0); // - IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired cursor type, reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you - IMGUI_API void SetMouseCursor(ImGuiMouseCursor cursor_type); // set desired cursor type - IMGUI_API void CaptureMouseFromApp(bool want_capture_mouse_value = true); // attention: misleading name! manually override io.WantCaptureMouse flag next frame (said flag is entirely left for your application to handle). This is equivalent to setting "io.WantCaptureMouse = want_capture_mouse_value;" after the next NewFrame() call. + IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired mouse cursor shape. Important: reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you + IMGUI_API void SetMouseCursor(ImGuiMouseCursor cursor_type); // set desired mouse cursor shape + IMGUI_API void SetNextFrameWantCaptureMouse(bool want_capture_mouse); // Override io.WantCaptureMouse flag next frame (said flag is left for your application to handle, typical when true it instucts your app to ignore inputs). This is equivalent to setting "io.WantCaptureMouse = want_capture_mouse;" after the next NewFrame() call. // Clipboard Utilities // - Also see the LogToClipboard() function to capture GUI into clipboard, or easily output text data to the clipboard. @@ -927,7 +931,7 @@ namespace ImGui IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. // Debug Utilities - // - This is used by the IMGUI_CHECKVERSION() macro. + IMGUI_API void DebugTextEncoding(const char* text); IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. // Memory Allocators @@ -946,6 +950,7 @@ namespace ImGui //----------------------------------------------------------------------------- // Flags for ImGui::Begin() +// (Those are per-window flags. There are shared flags in ImGuiIO: io.ConfigWindowsResizeFromEdges and io.ConfigWindowsMoveFromTitleBarOnly) enum ImGuiWindowFlags_ { ImGuiWindowFlags_None = 0, @@ -979,13 +984,11 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() ImGuiWindowFlags_Modal = 1 << 27, // Don't use! For internal use by BeginPopupModal() - ImGuiWindowFlags_ChildMenu = 1 << 28 // Don't use! For internal use by BeginMenu() - - // [Obsolete] - //ImGuiWindowFlags_ResizeFromAnySide = 1 << 17, // --> Set io.ConfigWindowsResizeFromEdges=true and make sure mouse cursors are supported by backend (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) + ImGuiWindowFlags_ChildMenu = 1 << 28, // Don't use! For internal use by BeginMenu() }; // Flags for ImGui::InputText() +// (Those are per-item flags. There are shared flags in ImGuiIO: io.ConfigInputTextCursorBlink and io.ConfigInputTextEnterKeepActive) enum ImGuiInputTextFlags_ { ImGuiInputTextFlags_None = 0, @@ -1008,11 +1011,12 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) - ImGuiInputTextFlags_CallbackEdit = 1 << 19 // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) + ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) + ImGuiInputTextFlags_EscapeClearsAll = 1 << 20, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) // Obsolete names (will be removed soon) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior + ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior #endif }; @@ -1035,7 +1039,7 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_SpanFullWidth = 1 << 12, // Extend hit box to the left-most and right-most edges (bypass the indented area). ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 14, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible - ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog + ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog, }; // Flags for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() functions. @@ -1044,7 +1048,7 @@ enum ImGuiTreeNodeFlags_ // It is therefore guaranteed to be legal to pass a mouse button index in ImGuiPopupFlags. // - For the same reason, we exceptionally default the ImGuiPopupFlags argument of BeginPopupContextXXX functions to 1 instead of 0. // IMPORTANT: because the default parameter is 1 (==ImGuiPopupFlags_MouseButtonRight), if you rely on the default parameter -// and want to another another flag, you need to pass in the ImGuiPopupFlags_MouseButtonRight flag. +// and want to use another flag, you need to pass in the ImGuiPopupFlags_MouseButtonRight flag explicitly. // - Multiple buttons currently cannot be combined/or-ed in those functions (we could allow it later). enum ImGuiPopupFlags_ { @@ -1058,18 +1062,18 @@ enum ImGuiPopupFlags_ ImGuiPopupFlags_NoOpenOverItems = 1 << 6, // For BeginPopupContextWindow(): don't return true when hovering items, only when hovering empty space ImGuiPopupFlags_AnyPopupId = 1 << 7, // For IsPopupOpen(): ignore the ImGuiID parameter and test for any popup. ImGuiPopupFlags_AnyPopupLevel = 1 << 8, // For IsPopupOpen(): search/test at any level of the popup stack (default test in the current level) - ImGuiPopupFlags_AnyPopup = ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel + ImGuiPopupFlags_AnyPopup = ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel, }; // Flags for ImGui::Selectable() enum ImGuiSelectableFlags_ { ImGuiSelectableFlags_None = 0, - ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this don't close parent popup window + ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this doesn't close parent popup window ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Selectable frame can span all columns (text will still fit in current column) ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text - ImGuiSelectableFlags_AllowItemOverlap = 1 << 4 // (WIP) Hit testing to allow subsequent widgets to overlap this one + ImGuiSelectableFlags_AllowItemOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one }; // Flags for ImGui::BeginCombo() @@ -1083,7 +1087,7 @@ enum ImGuiComboFlags_ ImGuiComboFlags_HeightLargest = 1 << 4, // As many fitting items as possible ImGuiComboFlags_NoArrowButton = 1 << 5, // Display on the preview box without the square arrow button ImGuiComboFlags_NoPreview = 1 << 6, // Display only a square arrow button - ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest + ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest, }; // Flags for ImGui::BeginTabBar() @@ -1099,7 +1103,7 @@ enum ImGuiTabBarFlags_ ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 6, // Resize tabs when they don't fit ImGuiTabBarFlags_FittingPolicyScroll = 1 << 7, // Add scroll buttons when tabs don't fit ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll, - ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown + ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown, }; // Flags for ImGui::BeginTabItem() @@ -1113,12 +1117,11 @@ enum ImGuiTabItemFlags_ ImGuiTabItemFlags_NoTooltip = 1 << 4, // Disable tooltip for the given tab ImGuiTabItemFlags_NoReorder = 1 << 5, // Disable reordering this tab or having another tab cross over this tab ImGuiTabItemFlags_Leading = 1 << 6, // Enforce the tab position to the left of the tab bar (after the tab list popup button) - ImGuiTabItemFlags_Trailing = 1 << 7 // Enforce the tab position to the right of the tab bar (before the scrolling buttons) + ImGuiTabItemFlags_Trailing = 1 << 7, // Enforce the tab position to the right of the tab bar (before the scrolling buttons) }; // Flags for ImGui::BeginTable() -// [BETA API] API may evolve slightly! If you use this, please update to the next version when it comes out! -// - Important! Sizing policies have complex and subtle side effects, more so than you would expect. +// - Important! Sizing policies have complex and subtle side effects, much more so than you would expect. // Read comments/demos carefully + experiment with live demos to get acquainted with them. // - The DEFAULT sizing policies are: // - Default to ImGuiTableFlags_SizingFixedFit if ScrollX is on, or if host window has ImGuiWindowFlags_AlwaysAutoResize. @@ -1126,15 +1129,15 @@ enum ImGuiTabItemFlags_ // - When ScrollX is off: // - Table defaults to ImGuiTableFlags_SizingStretchSame -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch with same weight. // - Columns sizing policy allowed: Stretch (default), Fixed/Auto. -// - Fixed Columns will generally obtain their requested width (unless the table cannot fit them all). -// - Stretch Columns will share the remaining width. +// - Fixed Columns (if any) will generally obtain their requested width (unless the table cannot fit them all). +// - Stretch Columns will share the remaining width according to their respective weight. // - Mixed Fixed/Stretch columns is possible but has various side-effects on resizing behaviors. // The typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. // (this is because the visible order of columns have subtle but necessary effects on how they react to manual resizing). // - When ScrollX is on: // - Table defaults to ImGuiTableFlags_SizingFixedFit -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed // - Columns sizing policy allowed: Fixed/Auto mostly. -// - Fixed Columns can be enlarged as needed. Table will show an horizontal scrollbar if needed. +// - Fixed Columns can be enlarged as needed. Table will show a horizontal scrollbar if needed. // - When using auto-resizing (non-resizable) fixed columns, querying the content width to use item right-alignment e.g. SetNextItemWidth(-FLT_MIN) doesn't make sense, would create a feedback loop. // - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable(). // If you specify a value for 'inner_width' then effectively the scrolling space is known and Stretch or mixed Fixed/Stretch columns become meaningful again. @@ -1160,8 +1163,8 @@ enum ImGuiTableFlags_ ImGuiTableFlags_BordersInner = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersInnerH, // Draw inner borders. ImGuiTableFlags_BordersOuter = ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_BordersOuterH, // Draw outer borders. ImGuiTableFlags_Borders = ImGuiTableFlags_BordersInner | ImGuiTableFlags_BordersOuter, // Draw all borders. - ImGuiTableFlags_NoBordersInBody = 1 << 11, // [ALPHA] Disable vertical borders in columns Body (borders will always appears in Headers). -> May move to style - ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 12, // [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers). -> May move to style + ImGuiTableFlags_NoBordersInBody = 1 << 11, // [ALPHA] Disable vertical borders in columns Body (borders will always appear in Headers). -> May move to style + ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 12, // [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers). -> May move to style // Sizing Policy (read above for defaults) ImGuiTableFlags_SizingFixedFit = 1 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching contents width. ImGuiTableFlags_SizingFixedSame = 2 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching the maximum contents width of all columns. Implicitly enable ImGuiTableFlags_NoKeepColumnsVisible. @@ -1175,24 +1178,18 @@ enum ImGuiTableFlags_ // Clipping ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze(). // Padding - ImGuiTableFlags_PadOuterX = 1 << 21, // Default if BordersOuterV is on. Enable outer-most padding. Generally desirable if you have headers. - ImGuiTableFlags_NoPadOuterX = 1 << 22, // Default if BordersOuterV is off. Disable outer-most padding. + ImGuiTableFlags_PadOuterX = 1 << 21, // Default if BordersOuterV is on. Enable outermost padding. Generally desirable if you have headers. + ImGuiTableFlags_NoPadOuterX = 1 << 22, // Default if BordersOuterV is off. Disable outermost padding. ImGuiTableFlags_NoPadInnerX = 1 << 23, // Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off). // Scrolling - ImGuiTableFlags_ScrollX = 1 << 24, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this create a child window, ScrollY is currently generally recommended when using ScrollX. + ImGuiTableFlags_ScrollX = 1 << 24, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this creates a child window, ScrollY is currently generally recommended when using ScrollX. ImGuiTableFlags_ScrollY = 1 << 25, // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. // Sorting ImGuiTableFlags_SortMulti = 1 << 26, // Hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1). ImGuiTableFlags_SortTristate = 1 << 27, // Allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0). // [Internal] Combinations and masks - ImGuiTableFlags_SizingMask_ = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_SizingStretchSame - - // Obsolete names (will be removed soon) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - //, ImGuiTableFlags_ColumnsWidthFixed = ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_ColumnsWidthStretch = ImGuiTableFlags_SizingStretchSame // WIP Tables 2020/12 - //, ImGuiTableFlags_SizingPolicyFixed = ImGuiTableFlags_SizingFixedFit, ImGuiTableFlags_SizingPolicyStretch = ImGuiTableFlags_SizingStretchSame // WIP Tables 2021/01 -#endif + ImGuiTableFlags_SizingMask_ = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_SizingStretchSame, }; // Flags for ImGui::TableSetupColumn() @@ -1229,19 +1226,14 @@ enum ImGuiTableColumnFlags_ ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed, ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable, ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered, - ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30 // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) - - // Obsolete names (will be removed soon) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - //ImGuiTableColumnFlags_WidthAuto = ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoResize, // Column will not stretch and keep resizing based on submitted contents. -#endif + ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30, // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) }; // Flags for ImGui::TableNextRow() enum ImGuiTableRowFlags_ { - ImGuiTableRowFlags_None = 0, - ImGuiTableRowFlags_Headers = 1 << 0 // Identify header row (set default background color + width of its contents accounted different for auto column width) + ImGuiTableRowFlags_None = 0, + ImGuiTableRowFlags_Headers = 1 << 0, // Identify header row (set default background color + width of its contents accounted differently for auto column width) }; // Enum for ImGui::TableSetBgColor() @@ -1249,16 +1241,16 @@ enum ImGuiTableRowFlags_ // - Layer 0: draw with RowBg0 color if set, otherwise draw with ColumnBg0 if set. // - Layer 1: draw with RowBg1 color if set, otherwise draw with ColumnBg1 if set. // - Layer 2: draw with CellBg color if set. -// The purpose of the two row/columns layers is to let you decide if a background color changes should override or blend with the existing color. +// The purpose of the two row/columns layers is to let you decide if a background color change should override or blend with the existing color. // When using ImGuiTableFlags_RowBg on the table, each row has the RowBg0 color automatically set for odd/even rows. // If you set the color of RowBg0 target, your color will override the existing RowBg0 color. // If you set the color of RowBg1 or ColumnBg1 target, your color will blend over the RowBg0 color. enum ImGuiTableBgTarget_ { - ImGuiTableBgTarget_None = 0, - ImGuiTableBgTarget_RowBg0 = 1, // Set row background color 0 (generally used for background, automatically set when ImGuiTableFlags_RowBg is used) - ImGuiTableBgTarget_RowBg1 = 2, // Set row background color 1 (generally used for selection marking) - ImGuiTableBgTarget_CellBg = 3 // Set cell background color (top-most color) + ImGuiTableBgTarget_None = 0, + ImGuiTableBgTarget_RowBg0 = 1, // Set row background color 0 (generally used for background, automatically set when ImGuiTableFlags_RowBg is used) + ImGuiTableBgTarget_RowBg1 = 2, // Set row background color 1 (generally used for selection marking) + ImGuiTableBgTarget_CellBg = 3, // Set cell background color (top-most color) }; // Flags for ImGui::IsWindowFocused() @@ -1270,7 +1262,7 @@ enum ImGuiFocusedFlags_ ImGuiFocusedFlags_AnyWindow = 1 << 2, // Return true if any window is focused. Important: If you are trying to tell how to dispatch your low-level inputs, do NOT use this. Use 'io.WantCaptureMouse' instead! Please read the FAQ! ImGuiFocusedFlags_NoPopupHierarchy = 1 << 3, // Do not consider popup hierarchy (do not treat popup emitter as parent of popup) (when used with _ChildWindows or _RootWindow) //ImGuiFocusedFlags_DockHierarchy = 1 << 4, // Consider docking hierarchy (treat dockspace host as parent of docked window) (when used with _ChildWindows or _RootWindow) - ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows + ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows, }; // Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered() @@ -1289,8 +1281,14 @@ enum ImGuiHoveredFlags_ ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 7, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 8, // IsItemHovered() only: Return true even if the position is obstructed or overlapped by another window ImGuiHoveredFlags_AllowWhenDisabled = 1 << 9, // IsItemHovered() only: Return true even if the item is disabled + ImGuiHoveredFlags_NoNavOverride = 1 << 10, // Disable using gamepad/keyboard navigation state when active, always query mouse. ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, - ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows + ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows, + + // Hovering delays (for tooltips) + ImGuiHoveredFlags_DelayNormal = 1 << 11, // Return true after io.HoverDelayNormal elapsed (~0.30 sec) + ImGuiHoveredFlags_DelayShort = 1 << 12, // Return true after io.HoverDelayShort elapsed (~0.10 sec) + ImGuiHoveredFlags_NoSharedDelay = 1 << 13, // Disable shared delay system where moving from one item to the next keeps the previous timer for a short time (standard for tooltips with long delays) }; // Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload() @@ -1298,8 +1296,8 @@ enum ImGuiDragDropFlags_ { ImGuiDragDropFlags_None = 0, // BeginDragDropSource() flags - ImGuiDragDropFlags_SourceNoPreviewTooltip = 1 << 0, // By default, a successful call to BeginDragDropSource opens a tooltip so you can display a preview or description of the source contents. This flag disable this behavior. - ImGuiDragDropFlags_SourceNoDisableHover = 1 << 1, // By default, when dragging we clear data so that IsItemHovered() will return false, to avoid subsequent user code submitting tooltips. This flag disable this behavior so you can still call IsItemHovered() on the source item. + ImGuiDragDropFlags_SourceNoPreviewTooltip = 1 << 0, // Disable preview tooltip. By default, a successful call to BeginDragDropSource opens a tooltip so you can display a preview or description of the source contents. This flag disables this behavior. + ImGuiDragDropFlags_SourceNoDisableHover = 1 << 1, // By default, when dragging we clear data so that IsItemHovered() will return false, to avoid subsequent user code submitting tooltips. This flag disables this behavior so you can still call IsItemHovered() on the source item. ImGuiDragDropFlags_SourceNoHoldToOpenOthers = 1 << 2, // Disable the behavior that allows to open tree nodes and collapsing header by holding over them while dragging a source item. ImGuiDragDropFlags_SourceAllowNullID = 1 << 3, // Allow items such as Text(), Image() that have no unique identifier to be used as drag source, by manufacturing a temporary identifier based on their window-relative position. This is extremely unusual within the dear imgui ecosystem and so we made it explicit. ImGuiDragDropFlags_SourceExtern = 1 << 4, // External source (from outside of dear imgui), won't attempt to read current item/window info. Will always return true. Only one Extern source can be active simultaneously. @@ -1308,7 +1306,7 @@ enum ImGuiDragDropFlags_ ImGuiDragDropFlags_AcceptBeforeDelivery = 1 << 10, // AcceptDragDropPayload() will returns true even before the mouse button is released. You can then call IsDelivery() to test if the payload needs to be delivered. ImGuiDragDropFlags_AcceptNoDrawDefaultRect = 1 << 11, // Do not draw the default highlight rectangle when hovering over target. ImGuiDragDropFlags_AcceptNoPreviewTooltip = 1 << 12, // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site. - ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect // For peeking ahead and inspecting the payload before delivery. + ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect, // For peeking ahead and inspecting the payload before delivery. }; // Standard Drag and Drop payload types. You can define you own payload types using short strings. Types starting with '_' are defined by Dear ImGui. @@ -1350,10 +1348,15 @@ enum ImGuiSortDirection_ ImGuiSortDirection_Descending = 2 // Descending = 9->0, Z->A etc. }; -// User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array -enum ImGuiKey_ +// A key identifier (ImGuiKey_XXX or ImGuiMod_XXX value): can represent Keyboard, Mouse and Gamepad values. +// All our named keys are >= 512. Keys value 0 to 511 are left unused as legacy native/opaque key values (< 1.87). +// Since >= 1.89 we increased typing (went from int to enum), some legacy code may need a cast to ImGuiKey. +// Read details about the 1.87 and 1.89 transition : https://github.com/ocornut/imgui/issues/4921 +enum ImGuiKey : int { - ImGuiKey_Tab, + // Keyboard + ImGuiKey_None = 0, + ImGuiKey_Tab = 512, // == ImGuiKey_NamedKey_BEGIN ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_UpArrow, @@ -1368,74 +1371,137 @@ enum ImGuiKey_ ImGuiKey_Space, ImGuiKey_Enter, ImGuiKey_Escape, - ImGuiKey_KeyPadEnter, - ImGuiKey_A, // for text edit CTRL+A: select all - ImGuiKey_C, // for text edit CTRL+C: copy - ImGuiKey_V, // for text edit CTRL+V: paste - ImGuiKey_X, // for text edit CTRL+X: cut - ImGuiKey_Y, // for text edit CTRL+Y: redo - ImGuiKey_Z, // for text edit CTRL+Z: undo - ImGuiKey_COUNT -}; - -// To test io.KeyMods (which is a combination of individual fields io.KeyCtrl, io.KeyShift, io.KeyAlt set by user/backend) -enum ImGuiKeyModFlags_ -{ - ImGuiKeyModFlags_None = 0, - ImGuiKeyModFlags_Ctrl = 1 << 0, - ImGuiKeyModFlags_Shift = 1 << 1, - ImGuiKeyModFlags_Alt = 1 << 2, - ImGuiKeyModFlags_Super = 1 << 3 -}; - -// Gamepad/Keyboard navigation -// Keyboard: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. -// Gamepad: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. Backend: set ImGuiBackendFlags_HasGamepad and fill the io.NavInputs[] fields before calling NewFrame(). Note that io.NavInputs[] is cleared by EndFrame(). -// Read instructions in imgui.cpp for more details. Download PNG/PSD at http://dearimgui.org/controls_sheets. -enum ImGuiNavInput_ -{ - // Gamepad Mapping - ImGuiNavInput_Activate, // activate / open / toggle / tweak value // e.g. Cross (PS4), A (Xbox), A (Switch), Space (Keyboard) - ImGuiNavInput_Cancel, // cancel / close / exit // e.g. Circle (PS4), B (Xbox), B (Switch), Escape (Keyboard) - ImGuiNavInput_Input, // text input / on-screen keyboard // e.g. Triang.(PS4), Y (Xbox), X (Switch), Return (Keyboard) - ImGuiNavInput_Menu, // tap: toggle menu / hold: focus, move, resize // e.g. Square (PS4), X (Xbox), Y (Switch), Alt (Keyboard) - ImGuiNavInput_DpadLeft, // move / tweak / resize window (w/ PadMenu) // e.g. D-pad Left/Right/Up/Down (Gamepads), Arrow keys (Keyboard) - ImGuiNavInput_DpadRight, // - ImGuiNavInput_DpadUp, // - ImGuiNavInput_DpadDown, // - ImGuiNavInput_LStickLeft, // scroll / move window (w/ PadMenu) // e.g. Left Analog Stick Left/Right/Up/Down - ImGuiNavInput_LStickRight, // - ImGuiNavInput_LStickUp, // - ImGuiNavInput_LStickDown, // - ImGuiNavInput_FocusPrev, // next window (w/ PadMenu) // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) - ImGuiNavInput_FocusNext, // prev window (w/ PadMenu) // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) - ImGuiNavInput_TweakSlow, // slower tweaks // e.g. L1 or L2 (PS4), LB or LT (Xbox), L or ZL (Switch) - ImGuiNavInput_TweakFast, // faster tweaks // e.g. R1 or R2 (PS4), RB or RT (Xbox), R or ZL (Switch) - - // [Internal] Don't use directly! This is used internally to differentiate keyboard from gamepad inputs for behaviors that require to differentiate them. - // Keyboard behavior that have no corresponding gamepad mapping (e.g. CTRL+TAB) will be directly reading from io.KeysDown[] instead of io.NavInputs[]. - ImGuiNavInput_KeyLeft_, // move left // = Arrow keys - ImGuiNavInput_KeyRight_, // move right - ImGuiNavInput_KeyUp_, // move up - ImGuiNavInput_KeyDown_, // move down + ImGuiKey_LeftCtrl, ImGuiKey_LeftShift, ImGuiKey_LeftAlt, ImGuiKey_LeftSuper, + ImGuiKey_RightCtrl, ImGuiKey_RightShift, ImGuiKey_RightAlt, ImGuiKey_RightSuper, + ImGuiKey_Menu, + ImGuiKey_0, ImGuiKey_1, ImGuiKey_2, ImGuiKey_3, ImGuiKey_4, ImGuiKey_5, ImGuiKey_6, ImGuiKey_7, ImGuiKey_8, ImGuiKey_9, + ImGuiKey_A, ImGuiKey_B, ImGuiKey_C, ImGuiKey_D, ImGuiKey_E, ImGuiKey_F, ImGuiKey_G, ImGuiKey_H, ImGuiKey_I, ImGuiKey_J, + ImGuiKey_K, ImGuiKey_L, ImGuiKey_M, ImGuiKey_N, ImGuiKey_O, ImGuiKey_P, ImGuiKey_Q, ImGuiKey_R, ImGuiKey_S, ImGuiKey_T, + ImGuiKey_U, ImGuiKey_V, ImGuiKey_W, ImGuiKey_X, ImGuiKey_Y, ImGuiKey_Z, + ImGuiKey_F1, ImGuiKey_F2, ImGuiKey_F3, ImGuiKey_F4, ImGuiKey_F5, ImGuiKey_F6, + ImGuiKey_F7, ImGuiKey_F8, ImGuiKey_F9, ImGuiKey_F10, ImGuiKey_F11, ImGuiKey_F12, + ImGuiKey_Apostrophe, // ' + ImGuiKey_Comma, // , + ImGuiKey_Minus, // - + ImGuiKey_Period, // . + ImGuiKey_Slash, // / + ImGuiKey_Semicolon, // ; + ImGuiKey_Equal, // = + ImGuiKey_LeftBracket, // [ + ImGuiKey_Backslash, // \ (this text inhibit multiline comment caused by backslash) + ImGuiKey_RightBracket, // ] + ImGuiKey_GraveAccent, // ` + ImGuiKey_CapsLock, + ImGuiKey_ScrollLock, + ImGuiKey_NumLock, + ImGuiKey_PrintScreen, + ImGuiKey_Pause, + ImGuiKey_Keypad0, ImGuiKey_Keypad1, ImGuiKey_Keypad2, ImGuiKey_Keypad3, ImGuiKey_Keypad4, + ImGuiKey_Keypad5, ImGuiKey_Keypad6, ImGuiKey_Keypad7, ImGuiKey_Keypad8, ImGuiKey_Keypad9, + ImGuiKey_KeypadDecimal, + ImGuiKey_KeypadDivide, + ImGuiKey_KeypadMultiply, + ImGuiKey_KeypadSubtract, + ImGuiKey_KeypadAdd, + ImGuiKey_KeypadEnter, + ImGuiKey_KeypadEqual, + + // Gamepad (some of those are analog values, 0.0f to 1.0f) // NAVIGATION ACTION + // (download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets) + ImGuiKey_GamepadStart, // Menu (Xbox) + (Switch) Start/Options (PS) + ImGuiKey_GamepadBack, // View (Xbox) - (Switch) Share (PS) + ImGuiKey_GamepadFaceLeft, // X (Xbox) Y (Switch) Square (PS) // Tap: Toggle Menu. Hold: Windowing mode (Focus/Move/Resize windows) + ImGuiKey_GamepadFaceRight, // B (Xbox) A (Switch) Circle (PS) // Cancel / Close / Exit + ImGuiKey_GamepadFaceUp, // Y (Xbox) X (Switch) Triangle (PS) // Text Input / On-screen Keyboard + ImGuiKey_GamepadFaceDown, // A (Xbox) B (Switch) Cross (PS) // Activate / Open / Toggle / Tweak + ImGuiKey_GamepadDpadLeft, // D-pad Left // Move / Tweak / Resize Window (in Windowing mode) + ImGuiKey_GamepadDpadRight, // D-pad Right // Move / Tweak / Resize Window (in Windowing mode) + ImGuiKey_GamepadDpadUp, // D-pad Up // Move / Tweak / Resize Window (in Windowing mode) + ImGuiKey_GamepadDpadDown, // D-pad Down // Move / Tweak / Resize Window (in Windowing mode) + ImGuiKey_GamepadL1, // L Bumper (Xbox) L (Switch) L1 (PS) // Tweak Slower / Focus Previous (in Windowing mode) + ImGuiKey_GamepadR1, // R Bumper (Xbox) R (Switch) R1 (PS) // Tweak Faster / Focus Next (in Windowing mode) + ImGuiKey_GamepadL2, // L Trig. (Xbox) ZL (Switch) L2 (PS) [Analog] + ImGuiKey_GamepadR2, // R Trig. (Xbox) ZR (Switch) R2 (PS) [Analog] + ImGuiKey_GamepadL3, // L Stick (Xbox) L3 (Switch) L3 (PS) + ImGuiKey_GamepadR3, // R Stick (Xbox) R3 (Switch) R3 (PS) + ImGuiKey_GamepadLStickLeft, // [Analog] // Move Window (in Windowing mode) + ImGuiKey_GamepadLStickRight, // [Analog] // Move Window (in Windowing mode) + ImGuiKey_GamepadLStickUp, // [Analog] // Move Window (in Windowing mode) + ImGuiKey_GamepadLStickDown, // [Analog] // Move Window (in Windowing mode) + ImGuiKey_GamepadRStickLeft, // [Analog] + ImGuiKey_GamepadRStickRight, // [Analog] + ImGuiKey_GamepadRStickUp, // [Analog] + ImGuiKey_GamepadRStickDown, // [Analog] + + // Aliases: Mouse Buttons (auto-submitted from AddMouseButtonEvent() calls) + // - This is mirroring the data also written to io.MouseDown[], io.MouseWheel, in a format allowing them to be accessed via standard key API. + ImGuiKey_MouseLeft, ImGuiKey_MouseRight, ImGuiKey_MouseMiddle, ImGuiKey_MouseX1, ImGuiKey_MouseX2, ImGuiKey_MouseWheelX, ImGuiKey_MouseWheelY, + + // [Internal] Reserved for mod storage + ImGuiKey_ReservedForModCtrl, ImGuiKey_ReservedForModShift, ImGuiKey_ReservedForModAlt, ImGuiKey_ReservedForModSuper, + ImGuiKey_COUNT, + + // Keyboard Modifiers (explicitly submitted by backend via AddKeyEvent() calls) + // - This is mirroring the data also written to io.KeyCtrl, io.KeyShift, io.KeyAlt, io.KeySuper, in a format allowing + // them to be accessed via standard key API, allowing calls such as IsKeyPressed(), IsKeyReleased(), querying duration etc. + // - Code polling every key (e.g. an interface to detect a key press for input mapping) might want to ignore those + // and prefer using the real keys (e.g. ImGuiKey_LeftCtrl, ImGuiKey_RightCtrl instead of ImGuiMod_Ctrl). + // - In theory the value of keyboard modifiers should be roughly equivalent to a logical or of the equivalent left/right keys. + // In practice: it's complicated; mods are often provided from different sources. Keyboard layout, IME, sticky keys and + // backends tend to interfere and break that equivalence. The safer decision is to relay that ambiguity down to the end-user... + ImGuiMod_None = 0, + ImGuiMod_Ctrl = 1 << 12, // Ctrl + ImGuiMod_Shift = 1 << 13, // Shift + ImGuiMod_Alt = 1 << 14, // Option/Menu + ImGuiMod_Super = 1 << 15, // Cmd/Super/Windows + ImGuiMod_Shortcut = 1 << 11, // Alias for Ctrl (non-macOS) _or_ Super (macOS). + ImGuiMod_Mask_ = 0xF800, // 5-bits + + // [Internal] Prior to 1.87 we required user to fill io.KeysDown[512] using their own native index + the io.KeyMap[] array. + // We are ditching this method but keeping a legacy path for user code doing e.g. IsKeyPressed(MY_NATIVE_KEY_CODE) + ImGuiKey_NamedKey_BEGIN = 512, + ImGuiKey_NamedKey_END = ImGuiKey_COUNT, + ImGuiKey_NamedKey_COUNT = ImGuiKey_NamedKey_END - ImGuiKey_NamedKey_BEGIN, +#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO + ImGuiKey_KeysData_SIZE = ImGuiKey_NamedKey_COUNT, // Size of KeysData[]: only hold named keys + ImGuiKey_KeysData_OFFSET = ImGuiKey_NamedKey_BEGIN, // First key stored in io.KeysData[0]. Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET). +#else + ImGuiKey_KeysData_SIZE = ImGuiKey_COUNT, // Size of KeysData[]: hold legacy 0..512 keycodes + named keys + ImGuiKey_KeysData_OFFSET = 0, // First key stored in io.KeysData[0]. Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET). +#endif + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiKey_ModCtrl = ImGuiMod_Ctrl, ImGuiKey_ModShift = ImGuiMod_Shift, ImGuiKey_ModAlt = ImGuiMod_Alt, ImGuiKey_ModSuper = ImGuiMod_Super, // Renamed in 1.89 + ImGuiKey_KeyPadEnter = ImGuiKey_KeypadEnter, // Renamed in 1.87 +#endif +}; + +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO +// OBSOLETED in 1.88 (from July 2022): ImGuiNavInput and io.NavInputs[]. +// Official backends between 1.60 and 1.86: will keep working and feed gamepad inputs as long as IMGUI_DISABLE_OBSOLETE_KEYIO is not set. +// Custom backends: feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. +enum ImGuiNavInput +{ + ImGuiNavInput_Activate, ImGuiNavInput_Cancel, ImGuiNavInput_Input, ImGuiNavInput_Menu, ImGuiNavInput_DpadLeft, ImGuiNavInput_DpadRight, ImGuiNavInput_DpadUp, ImGuiNavInput_DpadDown, + ImGuiNavInput_LStickLeft, ImGuiNavInput_LStickRight, ImGuiNavInput_LStickUp, ImGuiNavInput_LStickDown, ImGuiNavInput_FocusPrev, ImGuiNavInput_FocusNext, ImGuiNavInput_TweakSlow, ImGuiNavInput_TweakFast, ImGuiNavInput_COUNT, - ImGuiNavInput_InternalStart_ = ImGuiNavInput_KeyLeft_ }; +#endif // Configuration flags stored in io.ConfigFlags. Set by user/application. enum ImGuiConfigFlags_ { ImGuiConfigFlags_None = 0, - ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. NewFrame() will automatically fill io.NavInputs[] based on io.KeysDown[]. - ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. This is mostly to instruct your imgui backend to fill io.NavInputs[]. Backend also needs to set ImGuiBackendFlags_HasGamepad. + ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. + ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. Backend also needs to set ImGuiBackendFlags_HasGamepad. ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your backend, otherwise ImGui will react as if the mouse is jumping around back and forth. ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the backend. ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct backend to not alter mouse cursor shape and visibility. Use if the backend cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. - // User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are not used by core Dear ImGui) + // User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are NOT used by core Dear ImGui) ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. - ImGuiConfigFlags_IsTouchScreen = 1 << 21 // Application is using a touch screen instead of a mouse. + ImGuiConfigFlags_IsTouchScreen = 1 << 21, // Application is using a touch screen instead of a mouse. }; // Backend capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom backend. @@ -1445,7 +1511,7 @@ enum ImGuiBackendFlags_ ImGuiBackendFlags_HasGamepad = 1 << 0, // Backend Platform supports gamepad and currently has one connected. ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Backend Platform supports honoring GetMouseCursor() value to change the OS cursor shape. ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). - ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. + ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3, // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. }; // Enumeration for PushStyleColor() / PopStyleColor() @@ -1481,10 +1547,10 @@ enum ImGuiCol_ ImGuiCol_Separator, ImGuiCol_SeparatorHovered, ImGuiCol_SeparatorActive, - ImGuiCol_ResizeGrip, + ImGuiCol_ResizeGrip, // Resize grip in lower-right and lower-left corners of windows. ImGuiCol_ResizeGripHovered, ImGuiCol_ResizeGripActive, - ImGuiCol_Tab, + ImGuiCol_Tab, // TabItem in a TabBar ImGuiCol_TabHovered, ImGuiCol_TabActive, ImGuiCol_TabUnfocused, @@ -1499,7 +1565,7 @@ enum ImGuiCol_ ImGuiCol_TableRowBg, // Table row background (even rows) ImGuiCol_TableRowBgAlt, // Table row background (odd rows) ImGuiCol_TextSelectedBg, - ImGuiCol_DragDropTarget, + ImGuiCol_DragDropTarget, // Rectangle highlighting a drop target ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active @@ -1511,7 +1577,7 @@ enum ImGuiCol_ // - The enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. // During initialization or between frames, feel free to just poke into ImGuiStyle directly. // - Tip: Use your programming IDE navigation facilities on the names in the _second column_ below to find the actual members and their description. -// In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. // - When changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type. enum ImGuiStyleVar_ @@ -1555,7 +1621,7 @@ enum ImGuiButtonFlags_ // [Internal] ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, - ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft + ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, }; // Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() @@ -1596,16 +1662,15 @@ enum ImGuiColorEditFlags_ ImGuiColorEditFlags_DisplayMask_ = ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV | ImGuiColorEditFlags_DisplayHex, ImGuiColorEditFlags_DataTypeMask_ = ImGuiColorEditFlags_Uint8 | ImGuiColorEditFlags_Float, ImGuiColorEditFlags_PickerMask_ = ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_PickerHueBar, - ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV + ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV, // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex // [renamed in 1.69] -#endif + // ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex // [renamed in 1.69] }; // Flags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. // We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. +// (Those are per-item flags. There are shared flags in ImGuiIO: io.ConfigDragClickToInputText) enum ImGuiSliderFlags_ { ImGuiSliderFlags_None = 0, @@ -1613,11 +1678,11 @@ enum ImGuiSliderFlags_ ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits) ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget - ImGuiSliderFlags_InvalidMask_ = 0x7000000F // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. + ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp // [renamed in 1.79] + ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp, // [renamed in 1.79] #endif }; @@ -1639,7 +1704,7 @@ enum ImGuiMouseCursor_ ImGuiMouseCursor_Arrow = 0, ImGuiMouseCursor_TextInput, // When hovering over InputText, etc. ImGuiMouseCursor_ResizeAll, // (Unused by Dear ImGui functions) - ImGuiMouseCursor_ResizeNS, // When hovering over an horizontal border + ImGuiMouseCursor_ResizeNS, // When hovering over a horizontal border ImGuiMouseCursor_ResizeEW, // When hovering over a vertical border or a column ImGuiMouseCursor_ResizeNESW, // When hovering over the bottom-left corner of a window ImGuiMouseCursor_ResizeNWSE, // When hovering over the bottom-right corner of a window @@ -1654,10 +1719,10 @@ enum ImGuiMouseCursor_ enum ImGuiCond_ { ImGuiCond_None = 0, // No condition (always set the variable), same as _Always - ImGuiCond_Always = 1 << 0, // No condition (always set the variable) + ImGuiCond_Always = 1 << 0, // No condition (always set the variable), same as _None ImGuiCond_Once = 1 << 1, // Set the variable once per runtime session (only the first call will succeed) ImGuiCond_FirstUseEver = 1 << 2, // Set the variable if the object/window has no persistently saved data (no entry in .ini file) - ImGuiCond_Appearing = 1 << 3 // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) + ImGuiCond_Appearing = 1 << 3, // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) }; //----------------------------------------------------------------------------- @@ -1706,7 +1771,7 @@ struct ImVector // Constructors, destructor inline ImVector() { Size = Capacity = 0; Data = NULL; } inline ImVector(const ImVector& src) { Size = Capacity = 0; Data = NULL; operator=(src); } - inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } + inline ImVector& operator=(const ImVector& src) { clear(); resize(src.Size); if (src.Data) memcpy(Data, src.Data, (size_t)Size * sizeof(T)); return *this; } inline ~ImVector() { if (Data) IM_FREE(Data); } // Important: does not destruct anything inline void clear() { if (Data) { Size = Capacity = 0; IM_FREE(Data); Data = NULL; } } // Important: does not destruct anything @@ -1736,13 +1801,14 @@ struct ImVector inline void resize(int new_size, const T& v) { if (new_size > Capacity) reserve(_grow_capacity(new_size)); if (new_size > Size) for (int n = Size; n < new_size; n++) memcpy(&Data[n], &v, sizeof(v)); Size = new_size; } inline void shrink(int new_size) { IM_ASSERT(new_size <= Size); Size = new_size; } // Resize a vector to a smaller size, guaranteed not to cause a reallocation inline void reserve(int new_capacity) { if (new_capacity <= Capacity) return; T* new_data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); if (Data) { memcpy(new_data, Data, (size_t)Size * sizeof(T)); IM_FREE(Data); } Data = new_data; Capacity = new_capacity; } + inline void reserve_discard(int new_capacity) { if (new_capacity <= Capacity) return; if (Data) IM_FREE(Data); Data = (T*)IM_ALLOC((size_t)new_capacity * sizeof(T)); Capacity = new_capacity; } // NB: It is illegal to call push_back/push_front/insert with a reference pointing inside the ImVector data itself! e.g. v.push_back(v[10]) is forbidden. inline void push_back(const T& v) { if (Size == Capacity) reserve(_grow_capacity(Size + 1)); memcpy(&Data[Size], &v, sizeof(v)); Size++; } inline void pop_back() { IM_ASSERT(Size > 0); Size--; } inline void push_front(const T& v) { if (Size == 0) push_back(v); else insert(Data, v); } inline T* erase(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + 1, ((size_t)Size - (size_t)off - 1) * sizeof(T)); Size--; return Data + off; } - inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last > it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - (size_t)count) * sizeof(T)); Size -= (int)count; return Data + off; } + inline T* erase(const T* it, const T* it_last){ IM_ASSERT(it >= Data && it < Data + Size && it_last >= it && it_last <= Data + Size); const ptrdiff_t count = it_last - it; const ptrdiff_t off = it - Data; memmove(Data + off, Data + off + count, ((size_t)Size - (size_t)off - (size_t)count) * sizeof(T)); Size -= (int)count; return Data + off; } inline T* erase_unsorted(const T* it) { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; if (it < Data + Size - 1) memcpy(Data + off, Data + Size - 1, sizeof(T)); Size--; return Data + off; } inline T* insert(const T* it, const T& v) { IM_ASSERT(it >= Data && it <= Data + Size); const ptrdiff_t off = it - Data; if (Size == Capacity) reserve(_grow_capacity(Size + 1)); if (off < (int)Size) memmove(Data + off + 1, Data + off, ((size_t)Size - (size_t)off) * sizeof(T)); memcpy(&Data[off], &v, sizeof(v)); Size++; return Data + off; } inline bool contains(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; } @@ -1769,7 +1835,7 @@ struct ImGuiStyle ImVec2 WindowPadding; // Padding within a window. float WindowRounding; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. float WindowBorderSize; // Thickness of border around windows. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). - ImVec2 WindowMinSize; // Minimum window size. This is a global setting. If you want to constraint individual windows, use SetNextWindowSizeConstraints(). + ImVec2 WindowMinSize; // Minimum window size. This is a global setting. If you want to constrain individual windows, use SetNextWindowSizeConstraints(). ImVec2 WindowTitleAlign; // Alignment for title bar text. Defaults to (0.0f,0.5f) for left-aligned,vertically centered. ImGuiDir WindowMenuButtonPosition; // Side of the collapsing/docking button in the title bar (None/Left/Right). Defaults to ImGuiDir_Left. float ChildRounding; // Radius of child window corners rounding. Set to 0.0f to have rectangular windows. @@ -1792,7 +1858,7 @@ struct ImGuiStyle float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. - float TabMinWidthForCloseButton; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + float TabMinWidthForCloseButton; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -1800,7 +1866,7 @@ struct ImGuiStyle ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). - bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. Latched at the beginning of the frame (copied to ImDrawList). + bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). Latched at the beginning of the frame (copied to ImDrawList). bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. float CircleTessellationMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. @@ -1817,26 +1883,37 @@ struct ImGuiStyle // Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. //----------------------------------------------------------------------------- +// [Internal] Storage used by IsKeyDown(), IsKeyPressed() etc functions. +// If prior to 1.87 you used io.KeysDownDuration[] (which was marked as internal), you should use GetKeyData(key)->DownDuration and *NOT* io.KeysData[key]->DownDuration. +struct ImGuiKeyData +{ + bool Down; // True for if key is down + float DownDuration; // Duration the key has been down (<0.0f: not pressed, 0.0f: just pressed, >0.0f: time held) + float DownDurationPrev; // Last frame duration the key has been down + float AnalogValue; // 0.0f..1.0f for gamepad values +}; + struct ImGuiIO { //------------------------------------------------------------------ - // Configuration (fill once) // Default value + // Configuration // Default value //------------------------------------------------------------------ ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by backend (imgui_impl_xxx files or custom backend) to communicate features supported by the backend. - ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size) - float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. + ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size). May change every frame. + float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. May change every frame. float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. const char* IniFilename; // = "imgui.ini" // Path to .ini file (important: default "imgui.ini" is relative to current working dir!). Set NULL to disable automatic .ini loading/saving or if you want to manually call LoadIniSettingsXXX() / SaveIniSettingsXXX() functions. const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging. - int KeyMap[ImGuiKey_COUNT]; // // Map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. - float KeyRepeatDelay; // = 0.250f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). + float KeyRepeatDelay; // = 0.275f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds. - void* UserData; // = NULL // Store your own data for retrieval by callbacks. + float HoverDelayNormal; // = 0.30 sec // Delay on hovering before IsItemHovered(ImGuiHoveredFlags_DelayNormal) returns true. + float HoverDelayShort; // = 0.10 sec // Delay on hovering before IsItemHovered(ImGuiHoveredFlags_DelayShort) returns true. + void* UserData; // = NULL // Store your own data. ImFontAtlas*Fonts; // // Font atlas: load, rasterize and pack one or more fonts into a single texture. float FontGlobalScale; // = 1.0f // Global scale all fonts @@ -1847,7 +1924,9 @@ struct ImGuiIO // Miscellaneous options bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by backend implementations. bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl. + bool ConfigInputTrickleEventQueue; // = true // Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates. bool ConfigInputTextCursorBlink; // = true // Enable blinking cursor (optional as some users consider it to be distracting). + bool ConfigInputTextEnterKeepActive; // = false // [BETA] Pressing Enter will keep item active and select contents (single-line only). bool ConfigDragClickToInputText; // = false // [BETA] Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving). Not desirable on devices without a keyboard. bool ConfigWindowsResizeFromEdges; // = true // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag) bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. @@ -1873,31 +1952,32 @@ struct ImGuiIO // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) // (default to use native imm32 api on Windows) - void (*ImeSetInputScreenPosFn)(int x, int y); - void* ImeWindowHandle; // = NULL // (Windows) Set this to your HWND to get automatic IME cursor positioning. + void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + void* ImeWindowHandle; // = NULL // [Obsolete] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. +#else + void* _UnusedPadding; // Unused field to keep data structure the same size. +#endif //------------------------------------------------------------------ - // Input - Fill before calling NewFrame() + // Input - Call before calling NewFrame() //------------------------------------------------------------------ - ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.) - bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Others buttons allows us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. - float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. - float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with an horizontal wheel, may not be filled by all backends. - bool KeyCtrl; // Keyboard modifier pressed: Control - bool KeyShift; // Keyboard modifier pressed: Shift - bool KeyAlt; // Keyboard modifier pressed: Alt - bool KeySuper; // Keyboard modifier pressed: Cmd/Super/Windows - bool KeysDown[512]; // Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). - float NavInputs[ImGuiNavInput_COUNT]; // Gamepad inputs. Cleared back to zero by EndFrame(). Keyboard keys will be auto-mapped and be written here by NewFrame(). - - // Functions - IMGUI_API void AddInputCharacter(unsigned int c); // Queue new character input - IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue new character input from an UTF-16 character, it can be a surrogate - IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue new characters input from an UTF-8 string - IMGUI_API void AddFocusEvent(bool focused); // Notifies Dear ImGui when hosting platform windows lose or gain input focus - IMGUI_API void ClearInputCharacters(); // [Internal] Clear the text input buffer manually - IMGUI_API void ClearInputKeys(); // [Internal] Release all keys + // Input Functions + IMGUI_API void AddKeyEvent(ImGuiKey key, bool down); // Queue a new key down/up event. Key should be "translated" (as in, generally ImGuiKey_A matches the key end-user would use to emit an 'A' character) + IMGUI_API void AddKeyAnalogEvent(ImGuiKey key, bool down, float v); // Queue a new key down/up event for analog values (e.g. ImGuiKey_Gamepad_ values). Dead-zones should be handled by the backend. + IMGUI_API void AddMousePosEvent(float x, float y); // Queue a mouse position update. Use -FLT_MAX,-FLT_MAX to signify no mouse (e.g. app not focused and not hovered) + IMGUI_API void AddMouseButtonEvent(int button, bool down); // Queue a mouse button change + IMGUI_API void AddMouseWheelEvent(float wh_x, float wh_y); // Queue a mouse wheel update + IMGUI_API void AddFocusEvent(bool focused); // Queue a gain/loss of focus for the application (generally based on OS/platform focus of your window) + IMGUI_API void AddInputCharacter(unsigned int c); // Queue a new character input + IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue a new character input from a UTF-16 character, it can be a surrogate + IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue a new characters input from a UTF-8 string + + IMGUI_API void SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index = -1); // [Optional] Specify index for legacy <1.87 IsKeyXXX() functions with native indices + specify native keycode, scancode. + IMGUI_API void SetAppAcceptingEvents(bool accepting_events); // Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. + IMGUI_API void ClearInputCharacters(); // [Internal] Clear the text input buffer manually + IMGUI_API void ClearInputKeys(); // [Internal] Release all keys //------------------------------------------------------------------ // Output - Updated by NewFrame() or EndFrame()/Render() @@ -1905,50 +1985,70 @@ struct ImGuiIO // generally easier and more correct to use their state BEFORE calling NewFrame(). See FAQ for details!) //------------------------------------------------------------------ - bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). - bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). - bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). - bool WantSetMousePos; // MousePos has been altered, backend should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. - bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! - bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. - bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). - float Framerate; // Rough estimate of application framerate, in frame per second. Solely for convenience. Rolling average estimation based on io.DeltaTime over 120 frames. - int MetricsRenderVertices; // Vertices output during last call to Render() - int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 - int MetricsRenderWindows; // Number of visible windows - int MetricsActiveWindows; // Number of active windows - int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. - ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. + bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). + bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). + bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). + bool WantSetMousePos; // MousePos has been altered, backend should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. + bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! + bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. + bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). + float Framerate; // Estimate of application framerate (rolling average over 60 frames, based on io.DeltaTime), in frame per second. Solely for convenience. Slow applications may not want to use a moving average or may want to reset underlying buffers occasionally. + int MetricsRenderVertices; // Vertices output during last call to Render() + int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 + int MetricsRenderWindows; // Number of visible windows + int MetricsActiveWindows; // Number of active windows + int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. + ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. + + // Legacy: before 1.87, we required backend to fill io.KeyMap[] (imgui->native map) during initialization and io.KeysDown[] (native indices) every frame. + // This is still temporarily supported as a legacy feature. However the new preferred scheme is for backend to call io.AddKeyEvent(). + // Old (<1.87): ImGui::IsKeyPressed(ImGui::GetIO().KeyMap[ImGuiKey_Space]) --> New (1.87+) ImGui::IsKeyPressed(ImGuiKey_Space) +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + int KeyMap[ImGuiKey_COUNT]; // [LEGACY] Input: map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. The first 512 are now unused and should be kept zero. Legacy backend will write into KeyMap[] using ImGuiKey_ indices which are always >512. + bool KeysDown[ImGuiKey_COUNT]; // [LEGACY] Input: Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). This used to be [512] sized. It is now ImGuiKey_COUNT to allow legacy io.KeysDown[GetKeyIndex(...)] to work without an overflow. + float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. +#endif //------------------------------------------------------------------ // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ - bool WantCaptureMouseUnlessPopupClose;// Alternative to WantCaptureMouse: (WantCaptureMouse == true && WantCaptureMouseUnlessPopupClose == false) when a click over void is expected to close a popup. - ImGuiKeyModFlags KeyMods; // Key mods flags (same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags), updated by NewFrame() - ImGuiKeyModFlags KeyModsPrev; // Previous key mods - ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) - ImVec2 MouseClickedPos[5]; // Position at time of clicking - double MouseClickedTime[5]; // Time of last click (used to figure out double-click) - bool MouseClicked[5]; // Mouse button went from !Down to Down (same as MouseClickedCount[x] != 0) - bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? (same as MouseClickedCount[x] == 2) - ImU16 MouseClickedCount[5]; // == 0 (not clicked), == 1 (same as MouseClicked[]), == 2 (double-clicked), == 3 (triple-clicked) etc. when going from !Down to Down - ImU16 MouseClickedLastCount[5]; // Count successive number of clicks. Stays valid after mouse release. Reset after another click is done. - bool MouseReleased[5]; // Mouse button went from Down to !Down - bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. - bool MouseDownOwnedUnlessPopupClose[5];//Track if button was clicked inside a dear imgui window. - float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) - float MouseDownDurationPrev[5]; // Previous time the mouse button has been down - ImVec2 MouseDragMaxDistanceAbs[5]; // Maximum distance, absolute, on each axis, of how much mouse has traveled from the clicking point - float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point - float KeysDownDuration[512]; // Duration the keyboard key has been down (0.0f == just pressed) - float KeysDownDurationPrev[512]; // Previous duration the key has been down - float NavInputsDownDuration[ImGuiNavInput_COUNT]; - float NavInputsDownDurationPrev[ImGuiNavInput_COUNT]; - float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. - bool AppFocusLost; - ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16 - ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. + // Main Input State + // (this block used to be written by backend, since 1.87 it is best to NOT write to those directly, call the AddXXX functions above instead) + // (reading from those variables is fair game, as they are extremely unlikely to be moving anywhere) + ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.) + bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Other buttons allow us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. + float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. + float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with a horizontal wheel, may not be filled by all backends. + bool KeyCtrl; // Keyboard modifier down: Control + bool KeyShift; // Keyboard modifier down: Shift + bool KeyAlt; // Keyboard modifier down: Alt + bool KeySuper; // Keyboard modifier down: Cmd/Super/Windows + + // Other state maintained from data above + IO function calls + ImGuiKeyChord KeyMods; // Key mods flags (any of ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Alt/ImGuiMod_Super flags, same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags. DOES NOT CONTAINS ImGuiMod_Shortcut which is pretranslated). Read-only, updated by NewFrame() + ImGuiKeyData KeysData[ImGuiKey_KeysData_SIZE]; // Key state for all known keys. Use IsKeyXXX() functions to access this. + bool WantCaptureMouseUnlessPopupClose; // Alternative to WantCaptureMouse: (WantCaptureMouse == true && WantCaptureMouseUnlessPopupClose == false) when a click over void is expected to close a popup. + ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) + ImVec2 MouseClickedPos[5]; // Position at time of clicking + double MouseClickedTime[5]; // Time of last click (used to figure out double-click) + bool MouseClicked[5]; // Mouse button went from !Down to Down (same as MouseClickedCount[x] != 0) + bool MouseDoubleClicked[5]; // Has mouse button been double-clicked? (same as MouseClickedCount[x] == 2) + ImU16 MouseClickedCount[5]; // == 0 (not clicked), == 1 (same as MouseClicked[]), == 2 (double-clicked), == 3 (triple-clicked) etc. when going from !Down to Down + ImU16 MouseClickedLastCount[5]; // Count successive number of clicks. Stays valid after mouse release. Reset after another click is done. + bool MouseReleased[5]; // Mouse button went from Down to !Down + bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. + bool MouseDownOwnedUnlessPopupClose[5]; // Track if button was clicked inside a dear imgui window. + float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) + float MouseDownDurationPrev[5]; // Previous time the mouse button has been down + float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point (used for moving thresholds) + float PenPressure; // Touch/Pen pressure (0.0f to 1.0f, should be >0.0f only when MouseDown[0] == true). Helper storage currently unused by Dear ImGui. + bool AppFocusLost; // Only modify via AddFocusEvent() + bool AppAcceptingEvents; // Only modify via SetAppAcceptingEvents() + ImS8 BackendUsingLegacyKeyArrays; // -1: unknown, 0: using AddKeyEvent(), 1: using legacy io.KeysDown[] + bool BackendUsingLegacyNavInputArray; // 0: using AddKeyAnalogEvent(), 1: writing to legacy io.NavInputs[] directly + ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16() + ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. IMGUI_API ImGuiIO(); }; @@ -1999,7 +2099,7 @@ struct ImGuiInputTextCallbackData // NB: For basic min/max size constraint on each axis you don't need to use the callback! The SetNextWindowSizeConstraints() parameters are enough. struct ImGuiSizeCallbackData { - void* UserData; // Read-only. What user passed to SetNextWindowSizeConstraints() + void* UserData; // Read-only. What user passed to SetNextWindowSizeConstraints(). Generally store an integer or float in here (need reinterpret_cast<>). ImVec2 Pos; // Read-only. Window position, for reference. ImVec2 CurrentSize; // Read-only. Current window size. ImVec2 DesiredSize; // Read-write. Desired size, based on user's mouse position. Write to this field to restrain resizing. @@ -2063,7 +2163,7 @@ struct ImGuiTableSortSpecs #define IM_UNICODE_CODEPOINT_MAX 0xFFFF // Maximum Unicode code point supported by this build. #endif -// Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame. +// Helper: Execute a block of code at maximum once a frame. Convenient if you want to quickly create a UI within deep-nested code that runs multiple times every frame. // Usage: static ImGuiOnceUponAFrame oaf; if (oaf) ImGui::Text("This will be called only once per frame"); struct ImGuiOnceUponAFrame { @@ -2171,7 +2271,7 @@ struct ImGuiStorage }; // Helper: Manually clip large list of items. -// If you have lots evenly spaced items and you have a random access to the list, you can perform coarse +// If you have lots evenly spaced items and you have random access to the list, you can perform coarse // clipping based on visibility to only submit items that are in view. // The clipper calculates the range of visible items and advance the cursor to compensate for the non-visible items we have skipped. // (Dear ImGui already clip items based on their bounds but: it needs to first layout the item to do so, and generally @@ -2216,6 +2316,8 @@ struct ImGuiListClipper }; // Helpers macros to generate 32-bit encoded colors +// User can declare their own format by #defining the 5 _SHIFT/_MASK macros in their imconfig file. +#ifndef IM_COL32_R_SHIFT #ifdef IMGUI_USE_BGRA_PACKED_COLOR #define IM_COL32_R_SHIFT 16 #define IM_COL32_G_SHIFT 8 @@ -2229,6 +2331,7 @@ struct ImGuiListClipper #define IM_COL32_A_SHIFT 24 #define IM_COL32_A_MASK 0xFF000000 #endif +#endif #define IM_COL32(R,G,B,A) (((ImU32)(A)<> IM_COL32_R_SHIFT) & 0xFF) * sc; Value.y = (float)((rgba >> IM_COL32_G_SHIFT) & 0xFF) * sc; Value.z = (float)((rgba >> IM_COL32_B_SHIFT) & 0xFF) * sc; Value.w = (float)((rgba >> IM_COL32_A_SHIFT) & 0xFF) * sc; } - ImColor(float r, float g, float b, float a = 1.0f) { Value.x = r; Value.y = g; Value.z = b; Value.w = a; } - ImColor(const ImVec4& col) { Value = col; } inline operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } inline operator ImVec4() const { return Value; } @@ -2283,16 +2386,16 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c #define ImDrawCallback_ResetRenderState (ImDrawCallback)(-1) // Typically, 1 command = 1 GPU draw call (unless command is a callback) -// - VtxOffset/IdxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled, -// those fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. -// Pre-1.71 backends will typically ignore the VtxOffset/IdxOffset fields. +// - VtxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled, +// this fields allow us to render meshes larger than 64K vertices while keeping 16-bit indices. +// Backends made for <1.71. will typically ignore the VtxOffset fields. // - The ClipRect/TextureId/VtxOffset fields must be contiguous as we memcmp() them together (this is asserted for). struct ImDrawCmd { ImVec4 ClipRect; // 4*4 // Clipping rectangle (x1, y1, x2, y2). Subtract ImDrawData->DisplayPos to get clipping rectangle in "viewport" coordinates ImTextureID TextureId; // 4-8 // User-provided texture ID. Set by user in ImfontAtlas::SetTexID() for fonts or passed to Image*() functions. Ignore if never using images or multiple fonts atlas. unsigned int VtxOffset; // 4 // Start offset in vertex buffer. ImGuiBackendFlags_RendererHasVtxOffset: always 0, otherwise may be >0 to support meshes larger than 64K vertices with 16-bit indices. - unsigned int IdxOffset; // 4 // Start offset in index buffer. Always equal to sum of ElemCount drawn so far. + unsigned int IdxOffset; // 4 // Start offset in index buffer. unsigned int ElemCount; // 4 // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. ImDrawCallback UserCallback; // 4-8 // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. void* UserCallbackData; // 4-8 // The draw callback code can access this. @@ -2314,7 +2417,7 @@ struct ImDrawVert #else // You can override the vertex format layout by defining IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT in imconfig.h // The code expect ImVec2 pos (8 bytes), ImVec2 uv (8 bytes), ImU32 col (4 bytes), but you can re-order them or add other fields as needed to simplify integration in your engine. -// The type has to be described within the macro (you can either declare the struct or use a typedef). This is because ImVec2/ImU32 are likely not declared a the time you'd want to set your type up. +// The type has to be described within the macro (you can either declare the struct or use a typedef). This is because ImVec2/ImU32 are likely not declared at the time you'd want to set your type up. // NOTE: IMGUI DOESN'T CLEAR THE STRUCTURE AND DOESN'T CALL A CONSTRUCTOR SO ANY CUSTOM FIELD WILL BE UNINITIALIZED. IF YOU ADD EXTRA FIELDS (SUCH AS A 'Z' COORDINATES) YOU WILL NEED TO CLEAR THEM DURING RENDER OR TO IGNORE THEM. IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT; #endif @@ -2369,7 +2472,7 @@ enum ImDrawFlags_ ImDrawFlags_RoundCornersRight = ImDrawFlags_RoundCornersBottomRight | ImDrawFlags_RoundCornersTopRight, ImDrawFlags_RoundCornersAll = ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight | ImDrawFlags_RoundCornersBottomLeft | ImDrawFlags_RoundCornersBottomRight, ImDrawFlags_RoundCornersDefault_ = ImDrawFlags_RoundCornersAll, // Default to ALL corners if none of the _RoundCornersXX flags are specified. - ImDrawFlags_RoundCornersMask_ = ImDrawFlags_RoundCornersAll | ImDrawFlags_RoundCornersNone + ImDrawFlags_RoundCornersMask_ = ImDrawFlags_RoundCornersAll | ImDrawFlags_RoundCornersNone, }; // Flags for ImDrawList instance. Those are set automatically by ImGui:: functions from ImGuiIO settings, and generally not manipulated directly. @@ -2378,9 +2481,9 @@ enum ImDrawListFlags_ { ImDrawListFlags_None = 0, ImDrawListFlags_AntiAliasedLines = 1 << 0, // Enable anti-aliased lines/borders (*2 the number of triangles for 1.0f wide line or lines thin enough to be drawn using textures, otherwise *3 the number of triangles) - ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require backend to render with bilinear filtering. + ImDrawListFlags_AntiAliasedLinesUseTex = 1 << 1, // Enable anti-aliased lines/borders using textures when possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). ImDrawListFlags_AntiAliasedFill = 1 << 2, // Enable anti-aliased edge around filled shapes (rounded rectangles, circles). - ImDrawListFlags_AllowVtxOffset = 1 << 3 // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. + ImDrawListFlags_AllowVtxOffset = 1 << 3, // Can emit 'VtxOffset > 0' to allow large meshes. Set when 'ImGuiBackendFlags_RendererHasVtxOffset' is enabled. }; // Draw command list @@ -2402,7 +2505,7 @@ struct ImDrawList // [Internal, used while building lists] unsigned int _VtxCurrentIdx; // [Internal] generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. - const ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) + ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) const char* _OwnerName; // Pointer to owner window's name for debugging ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) @@ -2414,10 +2517,10 @@ struct ImDrawList float _FringeScale; // [Internal] anti-alias fringe is scaled by this value, this helps to keep things sharp while zooming at vertex buffer content // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) - ImDrawList(const ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; } + ImDrawList(ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; } ~ImDrawList() { _ClearFreeMemory(); } - IMGUI_API void PushClipRect(ImVec2 clip_rect_min, ImVec2 clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) + IMGUI_API void PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect = false); // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) IMGUI_API void PushClipRectFullScreen(); IMGUI_API void PopClipRect(); IMGUI_API void PushTextureID(ImTextureID texture_id); @@ -2426,11 +2529,12 @@ struct ImDrawList inline ImVec2 GetClipRectMax() const { const ImVec4& cr = _ClipRectStack.back(); return ImVec2(cr.z, cr.w); } // Primitives + // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. // - For rectangular primitives, "p_min" and "p_max" represent the upper-left and lower-right corners. // - For circle primitives, use "num_segments == 0" to automatically calculate tessellation (preferred). // In older versions (until Dear ImGui 1.77) the AddCircle functions defaulted to num_segments == 12. // In future versions we will use textures to provide cheaper and higher-quality circles. - // Use AddNgon() and AddNgonFilled() functions if you need to guaranteed a specific number of sides. + // Use AddNgon() and AddNgonFilled() functions if you need to guarantee a specific number of sides. IMGUI_API void AddLine(const ImVec2& p1, const ImVec2& p2, ImU32 col, float thickness = 1.0f); IMGUI_API void AddRect(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawFlags flags = 0, float thickness = 1.0f); // a: upper-left, b: lower-right (== upper-left + size) IMGUI_API void AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 col, float rounding = 0.0f, ImDrawFlags flags = 0); // a: upper-left, b: lower-right (== upper-left + size) @@ -2446,7 +2550,7 @@ struct ImDrawList IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness); - IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); // Note: Anti-aliased filling requires points to be in clockwise order. + IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); IMGUI_API void AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments = 0); // Quadratic Bezier (3 control points) @@ -2459,10 +2563,11 @@ struct ImDrawList IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags = 0); // Stateful path API, add points then finish with PathFillConvex() or PathStroke() + // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. inline void PathClear() { _Path.Size = 0; } inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size - 1], &pos, 8) != 0) _Path.push_back(pos); } - inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } // Note: Anti-aliased filling requires points to be in clockwise order. + inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } inline void PathStroke(ImU32 col, ImDrawFlags flags = 0, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, flags, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 0); IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle @@ -2498,8 +2603,8 @@ struct ImDrawList inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } - inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } + inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) + inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) #endif // [Internal helpers] @@ -2614,7 +2719,7 @@ enum ImFontAtlasFlags_ ImFontAtlasFlags_None = 0, ImFontAtlasFlags_NoPowerOfTwoHeight = 1 << 0, // Don't round the height to next power of two ImFontAtlasFlags_NoMouseCursors = 1 << 1, // Don't build software mouse cursors into the atlas (save a little texture memory) - ImFontAtlasFlags_NoBakedLines = 1 << 2 // Don't build thick line textures into the atlas (save a little texture memory). The AntiAliasedLinesUseTex features uses them, otherwise they will be rendered using polygons (more expensive for CPU/GPU). + ImFontAtlasFlags_NoBakedLines = 1 << 2, // Don't build thick line textures into the atlas (save a little texture memory, allow support for point/nearest filtering). The AntiAliasedLinesUseTex features uses them, otherwise they will be rendered using polygons (more expensive for CPU/GPU). }; // Load and rasterize multiple TTF/OTF fonts into a same texture. The font atlas will build a single texture holding: @@ -2633,7 +2738,7 @@ enum ImFontAtlasFlags_ // - Important: By default, AddFontFromMemoryTTF() takes ownership of the data. Even though we are not writing to it, we will free the pointer on destruction. // You can set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed, // - Even though many functions are suffixed with "TTF", OTF data is supported just as well. -// - This is an old API and it is currently awkward for those and and various other reasons! We will address them in the future! +// - This is an old API and it is currently awkward for those and various other reasons! We will address them in the future! struct ImFontAtlas { IMGUI_API ImFontAtlas(); @@ -2657,7 +2762,7 @@ struct ImFontAtlas IMGUI_API bool Build(); // Build pixels data. This is called automatically for you by the GetTexData*** functions. IMGUI_API void GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 1 byte per-pixel IMGUI_API void GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL); // 4 bytes-per-pixel - bool IsBuilt() const { return Fonts.Size > 0 && TexReady; } // Bit ambiguous: used to detect when user didn't built texture but effectively we should check TexID != 0 except that would be backend dependent... + bool IsBuilt() const { return Fonts.Size > 0 && TexReady; } // Bit ambiguous: used to detect when user didn't build texture but effectively we should check TexID != 0 except that would be backend dependent... void SetTexID(ImTextureID id) { TexID = id; } //------------------------------------------- @@ -2668,6 +2773,7 @@ struct ImFontAtlas // NB: Make sure that your string are UTF-8 and NOT in your local code page. In C++11, you can create UTF-8 string literal using the u8"Hello world" syntax. See FAQ for details. // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data. IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin + IMGUI_API const ImWchar* GetGlyphRangesGreek(); // Default + Greek and Coptic IMGUI_API const ImWchar* GetGlyphRangesKorean(); // Default + Korean characters IMGUI_API const ImWchar* GetGlyphRangesJapanese(); // Default + Hiragana, Katakana, Half-Width, Selection of 2999 Ideographs IMGUI_API const ImWchar* GetGlyphRangesChineseFull(); // Default + Half-Width + Japanese Hiragana/Katakana + full set of about 21000 CJK Unified Ideographs @@ -2702,8 +2808,9 @@ struct ImFontAtlas ImFontAtlasFlags Flags; // Build flags (see ImFontAtlasFlags_) ImTextureID TexID; // User data to refer to the texture once it has been uploaded to user's graphic systems. It is passed back to you during rendering via the ImDrawCmd structure. int TexDesiredWidth; // Texture width desired by user before Build(). Must be a power-of-two. If have many glyphs your graphics API have texture size restrictions you may want to increase texture width to decrease height. - int TexGlyphPadding; // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0. + int TexGlyphPadding; // Padding between glyphs within texture in pixels. Defaults to 1. If your rendering method doesn't rely on bilinear filtering you may set this to 0 (will also need to set AntiAliasedLinesUseTex = false). bool Locked; // Marked as Locked by ImGui::NewFrame() so attempt to modify the atlas will assert. + void* UserData; // Store your own atlas related user-data (if e.g. you have multiple font atlas). // [Internal] // NB: Access texture data via GetTexData*() calls! Which will setup a default font for you. @@ -2728,10 +2835,9 @@ struct ImFontAtlas int PackIdMouseCursors; // Custom texture rectangle ID for white pixel and mouse cursors int PackIdLines; // Custom texture rectangle ID for baked anti-aliased lines -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+ + // [Obsolete] + //typedef ImFontAtlasCustomRect CustomRect; // OBSOLETED in 1.72+ //typedef ImFontGlyphRangesBuilder GlyphRangesBuilder; // OBSOLETED in 1.67+ -#endif }; // Font runtime data and rendering @@ -2774,8 +2880,8 @@ struct ImFont // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable. IMGUI_API ImVec2 CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8 IMGUI_API const char* CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const; - IMGUI_API void RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const; - IMGUI_API void RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const; + IMGUI_API void RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c) const; + IMGUI_API void RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width = 0.0f, bool cpu_fine_clip = false) const; // [Internal] Don't use! IMGUI_API void BuildLookupTable(); @@ -2797,7 +2903,7 @@ enum ImGuiViewportFlags_ ImGuiViewportFlags_None = 0, ImGuiViewportFlags_IsPlatformWindow = 1 << 0, // Represent a Platform Window ImGuiViewportFlags_IsPlatformMonitor = 1 << 1, // Represent a Platform Monitor (unused yet) - ImGuiViewportFlags_OwnedByApp = 1 << 2 // Platform Window: is created/managed by the application (rather than a dear imgui backend) + ImGuiViewportFlags_OwnedByApp = 1 << 2, // Platform Window: is created/managed by the application (rather than a dear imgui backend) }; // - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. @@ -2815,6 +2921,9 @@ struct ImGuiViewport ImVec2 WorkPos; // Work Area: Position of the viewport minus task bars, menus bars, status bars (>= Pos) ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size) + // Platform/Backend Dependent Data + void* PlatformHandleRaw; // void* to hold lower-level, platform-native window handle (under Win32 this is expected to be a HWND, unused for other platforms) + ImGuiViewport() { memset(this, 0, sizeof(*this)); } // Helpers @@ -2822,60 +2931,97 @@ struct ImGuiViewport ImVec2 GetWorkCenter() const { return ImVec2(WorkPos.x + WorkSize.x * 0.5f, WorkPos.y + WorkSize.y * 0.5f); } }; +//----------------------------------------------------------------------------- +// [SECTION] Platform Dependent Interfaces +//----------------------------------------------------------------------------- + +// (Optional) Support for IME (Input Method Editor) via the io.SetPlatformImeDataFn() function. +struct ImGuiPlatformImeData +{ + bool WantVisible; // A widget wants the IME to be visible + ImVec2 InputPos; // Position of the input cursor + float InputLineHeight; // Line height + + ImGuiPlatformImeData() { memset(this, 0, sizeof(*this)); } +}; + //----------------------------------------------------------------------------- // [SECTION] Obsolete functions and types // (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) // Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. //----------------------------------------------------------------------------- +namespace ImGui +{ +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + IMGUI_API ImGuiKey GetKeyIndex(ImGuiKey key); // map ImGuiKey_* values into legacy native key index. == io.KeyMap[key] +#else + static inline ImGuiKey GetKeyIndex(ImGuiKey key) { IM_ASSERT(key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END && "ImGuiKey and native_index was merged together and native_index is disabled by IMGUI_DISABLE_OBSOLETE_KEYIO. Please switch to ImGuiKey."); return key; } +#endif +} + #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.89 (from August 2022) + IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // Use new ImageButton() signature (explicit item id, regular FramePadding) + // OBSOLETED in 1.88 (from May 2022) + static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. + static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. // OBSOLETED in 1.86 (from November 2021) IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Calculate coarse clipping for large list of evenly sized items. Prefer using ImGuiListClipper. // OBSOLETED in 1.85 (from August 2021) - static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } + static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } // OBSOLETED in 1.81 (from February 2021) IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } - static inline void ListBoxFooter() { EndListBox(); } - // OBSOLETED in 1.79 (from August 2020) - static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! - // OBSOLETED in 1.78 (from June 2020) - // Old drag/sliders functions that took a 'float power = 1.0' argument instead of flags. - // For shared code, you can version check at compile-time with `#if IMGUI_VERSION_NUM >= 17704`. - IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power); - IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power); - static inline bool DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } - static inline bool DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } - IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power); - IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format, float power); - static inline bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } - static inline bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } - static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } - static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } - // OBSOLETED in 1.77 (from June 2020) - static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } - // OBSOLETED in 1.72 (from April 2019) - static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } - // OBSOLETED in 1.71 (from June 2019) - static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } - // OBSOLETED in 1.70 (from May 2019) - static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } + static inline void ListBoxFooter() { EndListBox(); } // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) - //static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } // OBSOLETED in 1.69 (from Mar 2019) - //static inline void SetScrollHere(float ratio = 0.5f) { SetScrollHereY(ratio); } // OBSOLETED in 1.66 (from Nov 2018) - //static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } // OBSOLETED in 1.63 (from Aug 2018) - //static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } // OBSOLETED in 1.60 (from Apr 2018) - //static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) - //static inline void ShowTestWindow() { return ShowDemoWindow(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) - //static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) - //static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) - //static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) - //static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //-- OBSOLETED in 1.79 (from August 2020) + //static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! + //-- OBSOLETED in 1.78 (from June 2020): Old drag/sliders functions that took a 'float power > 1.0f' argument instead of ImGuiSliderFlags_Logarithmic. See github.com/ocornut/imgui/issues/3361 for details. + //IMGUI_API bool DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power = 1.0f) // OBSOLETED in 1.78 (from June 2020) + //IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power = 1.0f); // OBSOLETED in 1.78 (from June 2020) + //IMGUI_API bool SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power = 1.0f); // OBSOLETED in 1.78 (from June 2020) + //IMGUI_API bool SliderScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_min, const void* p_max, const char* format, float power = 1.0f); // OBSOLETED in 1.78 (from June 2020) + //static inline bool DragFloat(const char* label, float* v, float v_speed, float v_min, float v_max, const char* format, float power = 1.0f) { return DragScalar(label, ImGuiDataType_Float, v, v_speed, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool DragFloat2(const char* label, float v[2], float v_speed, float v_min, float v_max, const char* format, float power = 1.0f) { return DragScalarN(label, ImGuiDataType_Float, v, 2, v_speed, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool DragFloat3(const char* label, float v[3], float v_speed, float v_min, float v_max, const char* format, float power = 1.0f) { return DragScalarN(label, ImGuiDataType_Float, v, 3, v_speed, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool DragFloat4(const char* label, float v[4], float v_speed, float v_min, float v_max, const char* format, float power = 1.0f) { return DragScalarN(label, ImGuiDataType_Float, v, 4, v_speed, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* format, float power = 1.0f) { return SliderScalar(label, ImGuiDataType_Float, v, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* format, float power = 1.0f) { return SliderScalarN(label, ImGuiDataType_Float, v, 2, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* format, float power = 1.0f) { return SliderScalarN(label, ImGuiDataType_Float, v, 3, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //static inline bool SliderFloat4(const char* label, float v[4], float v_min, float v_max, const char* format, float power = 1.0f) { return SliderScalarN(label, ImGuiDataType_Float, v, 4, &v_min, &v_max, format, power); } // OBSOLETED in 1.78 (from June 2020) + //-- OBSOLETED in 1.77 and before + //static inline bool BeginPopupContextWindow(const char* str_id, ImGuiMouseButton mb, bool over_items) { return BeginPopupContextWindow(str_id, mb | (over_items ? 0 : ImGuiPopupFlags_NoOpenOverItems)); } // OBSOLETED in 1.77 (from June 2020) + //static inline void TreeAdvanceToLabelPos() { SetCursorPosX(GetCursorPosX() + GetTreeNodeToLabelSpacing()); } // OBSOLETED in 1.72 (from July 2019) + //static inline void SetNextTreeNodeOpen(bool open, ImGuiCond cond = 0) { SetNextItemOpen(open, cond); } // OBSOLETED in 1.71 (from June 2019) + //static inline float GetContentRegionAvailWidth() { return GetContentRegionAvail().x; } // OBSOLETED in 1.70 (from May 2019) + //static inline ImDrawList* GetOverlayDrawList() { return GetForegroundDrawList(); } // OBSOLETED in 1.69 (from Mar 2019) + //static inline void SetScrollHere(float ratio = 0.5f) { SetScrollHereY(ratio); } // OBSOLETED in 1.66 (from Nov 2018) + //static inline bool IsItemDeactivatedAfterChange() { return IsItemDeactivatedAfterEdit(); } // OBSOLETED in 1.63 (from Aug 2018) + //-- OBSOLETED in 1.60 and before + //static inline bool IsAnyWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_AnyWindow); } // OBSOLETED in 1.60 (from Apr 2018) + //static inline bool IsAnyWindowHovered() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } // OBSOLETED in 1.60 (between Dec 2017 and Apr 2018) + //static inline void ShowTestWindow() { return ShowDemoWindow(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline void SetNextWindowContentWidth(float w) { SetNextWindowContentSize(ImVec2(w, 0.0f)); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //static inline float GetItemsLineHeightWithSpacing() { return GetFrameHeightWithSpacing(); } // OBSOLETED in 1.53 (between Oct 2017 and Dec 2017) + //IMGUI_API bool Begin(char* name, bool* p_open, ImVec2 size_first_use, float bg_alpha = -1.0f, ImGuiWindowFlags flags=0); // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017): Equivalent of using SetNextWindowSize(size, ImGuiCond_FirstUseEver) and SetNextWindowBgAlpha(). + //static inline bool IsRootWindowOrAnyChildHovered() { return IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows); } // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) + //static inline void AlignFirstTextHeightToWidgets() { AlignTextToFramePadding(); } // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) + //static inline void SetNextWindowPosCenter(ImGuiCond c=0) { SetNextWindowPos(GetMainViewport()->GetCenter(), c, ImVec2(0.5f,0.5f)); } // OBSOLETED in 1.52 (between Aug 2017 and Oct 2017) + //static inline bool IsItemHoveredRect() { return IsItemHovered(ImGuiHoveredFlags_RectOnly); } // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017) + //static inline bool IsPosHoveringAnyWindow(const ImVec2&) { IM_ASSERT(0); return false; } // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017): This was misleading and partly broken. You probably want to use the io.WantCaptureMouse flag instead. + //static inline bool IsMouseHoveringAnyWindow() { return IsWindowHovered(ImGuiHoveredFlags_AnyWindow); } // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017) + //static inline bool IsMouseHoveringWindow() { return IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem); } // OBSOLETED in 1.51 (between Jun 2017 and Aug 2017) + //-- OBSOLETED in 1.50 and before + //static inline bool CollapsingHeader(char* label, const char* str_id, bool framed = true, bool default_open = false) { return CollapsingHeader(label, (default_open ? (1 << 5) : 0)); } // OBSOLETED in 1.49 + //static inline ImFont*GetWindowFont() { return GetFont(); } // OBSOLETED in 1.48 + //static inline float GetWindowFontSize() { return GetFontSize(); } // OBSOLETED in 1.48 + //static inline void SetScrollPosHere() { SetScrollHere(); } // OBSOLETED in 1.42 } // OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect() @@ -2891,11 +3037,26 @@ enum ImDrawCornerFlags_ ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, - ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight + ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, }; +// RENAMED and MERGED both ImGuiKey_ModXXX and ImGuiModFlags_XXX into ImGuiMod_XXX (from September 2022) +// RENAMED ImGuiKeyModFlags -> ImGuiModFlags in 1.88 (from April 2022). Exceptionally commented out ahead of obscolescence schedule to reduce confusion and because they were not meant to be used in the first place. +typedef ImGuiKeyChord ImGuiModFlags; // == int. We generally use ImGuiKeyChord to mean "a ImGuiKey or-ed with any number of ImGuiMod_XXX value", but you may store only mods in there. +enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiModFlags_Shift = ImGuiMod_Shift, ImGuiModFlags_Alt = ImGuiMod_Alt, ImGuiModFlags_Super = ImGuiMod_Super }; +//typedef ImGuiKeyChord ImGuiKeyModFlags; // == int +//enum ImGuiKeyModFlags_ { ImGuiKeyModFlags_None = 0, ImGuiKeyModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiKeyModFlags_Shift = ImGuiMod_Shift, ImGuiKeyModFlags_Alt = ImGuiMod_Alt, ImGuiKeyModFlags_Super = ImGuiMod_Super }; + #endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +// RENAMED IMGUI_DISABLE_METRICS_WINDOW > IMGUI_DISABLE_DEBUG_TOOLS in 1.88 (from June 2022) +#if defined(IMGUI_DISABLE_METRICS_WINDOW) && !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && !defined(IMGUI_DISABLE_DEBUG_TOOLS) +#define IMGUI_DISABLE_DEBUG_TOOLS +#endif +#if defined(IMGUI_DISABLE_METRICS_WINDOW) && defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) +#error IMGUI_DISABLE_METRICS_WINDOW was renamed to IMGUI_DISABLE_DEBUG_TOOLS, please use new name. +#endif + //----------------------------------------------------------------------------- #if defined(__clang__) diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index 5e1fb06d..9f5e35c6 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -4,19 +4,15 @@ // Implemented features: // [X] Platform: Clipboard support. +// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW. -// [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). +// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. // Read online: https://github.com/ocornut/imgui/tree/master/docs -// About GLSL version: -// The 'glsl_version' initialization parameter defaults to "#version 150" if NULL. -// Only override if your GL version doesn't handle this GLSL version. Keep NULL if unsure! - #pragma once #include "imgui.h" // IMGUI_IMPL_API @@ -29,11 +25,16 @@ IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool ins IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); -// GLFW callbacks -// - When calling Init with 'install_callbacks=true': GLFW callbacks will be installed for you. They will call user's previously installed callbacks, if any. -// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call those function yourself from your own GLFW callbacks. -IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); -IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); +// GLFW callbacks (installer) +// - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any. +// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks. +IMGUI_IMPL_API void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window); +IMGUI_IMPL_API void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window); + +// GLFW callbacks (individual callbacks to call if you didn't install callbacks) +IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84 +IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84 +IMGUI_IMPL_API void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87 IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); diff --git a/include/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h index 98c9aca1..5baf5abd 100644 --- a/include/imgui/imgui_impl_opengl3.h +++ b/include/imgui/imgui_impl_opengl3.h @@ -13,7 +13,7 @@ // Read online: https://github.com/ocornut/imgui/tree/master/docs // About GLSL version: -// The 'glsl_version' initialization parameter should be NULL (default) or a "#version XXX" string. +// The 'glsl_version' initialization parameter should be nullptr (default) or a "#version XXX" string. // On computer platform the GLSL version default to "#version 130". On OpenGL ES 3 platform it defaults to "#version 300 es" // Only override if your GL version doesn't handle this GLSL version. See GLSL version table at the top of imgui_impl_opengl3.cpp. @@ -21,7 +21,7 @@ #include "imgui.h" // IMGUI_IMPL_API // Backend API -IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = NULL); +IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); @@ -46,7 +46,7 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #endif #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) || (defined(__ANDROID__)) #define IMGUI_IMPL_OPENGL_ES3 // iOS, Android -> GL ES 3, "#version 300 es" -#elif defined(__EMSCRIPTEN__) +#elif defined(__EMSCRIPTEN__) || defined(__amigaos4__) #define IMGUI_IMPL_OPENGL_ES2 // Emscripten -> GL ES 2, "#version 100" #else // Otherwise imgui_impl_opengl3_loader.h will be used. diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index e38d210c..5b6615e9 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -9,6 +9,14 @@ // YOU SHOULD NOT NEED TO INCLUDE/USE THIS DIRECTLY. THIS IS USED BY imgui_impl_opengl3.cpp ONLY. // THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE. // +// IF YOU GET BUILD ERRORS IN THIS FILE (commonly macro redefinitions or function redefinitions): +// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCUDING 'imgui_impl_opengl3_loader.h' +// IN THE SAME COMPILATION UNIT AS ONE OF YOUR FILE WHICH IS USING A THIRD-PARTY OPENGL LOADER. +// (e.g. COULD HAPPEN IF YOU ARE DOING A UNITY/JUMBO BUILD, OR INCLUDING .CPP FILES FROM OTHERS) +// YOU SHOULD NOT BUILD BOTH IN THE SAME COMPILATION UNIT. +// BUT IF YOU REALLY WANT TO, you can '#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM' and imgui_impl_opengl3.cpp +// WILL NOT BE USING OUR LOADER, AND INSTEAD EXPECT ANOTHER/YOUR LOADER TO BE AVAILABLE IN THE COMPILATION UNIT. +// // Regenerate with: // python gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt // @@ -110,7 +118,7 @@ extern "C" { ** included as . ** ** glcorearb.h includes only APIs in the latest OpenGL core profile -** implementation together with APIs in newer ARB extensions which +** implementation together with APIs in newer ARB extensions which ** can be supported by the core profile. It does not, and never will ** include functionality removed from the core profile, such as ** fixed-function vertex and fragment processing. @@ -164,6 +172,8 @@ typedef khronos_uint8_t GLubyte; #define GL_FLOAT 0x1406 #define GL_RGBA 0x1908 #define GL_FILL 0x1B02 +#define GL_VENDOR 0x1F00 +#define GL_RENDERER 0x1F01 #define GL_VERSION 0x1F02 #define GL_EXTENSIONS 0x1F03 #define GL_LINEAR 0x2601 @@ -177,6 +187,7 @@ typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask); typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap); typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap); +typedef void (APIENTRYP PFNGLFLUSHPROC) (void); typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param); typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); typedef GLenum (APIENTRYP PFNGLGETERRORPROC) (void); @@ -193,6 +204,7 @@ GLAPI void APIENTRY glClear (GLbitfield mask); GLAPI void APIENTRY glClearColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); GLAPI void APIENTRY glDisable (GLenum cap); GLAPI void APIENTRY glEnable (GLenum cap); +GLAPI void APIENTRY glFlush (void); GLAPI void APIENTRY glPixelStorei (GLenum pname, GLint param); GLAPI void APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *pixels); GLAPI GLenum APIENTRY glGetError (void); @@ -244,6 +256,7 @@ typedef khronos_intptr_t GLintptr; #define GL_ARRAY_BUFFER 0x8892 #define GL_ELEMENT_ARRAY_BUFFER 0x8893 #define GL_ARRAY_BUFFER_BINDING 0x8894 +#define GL_ELEMENT_ARRAY_BUFFER_BINDING 0x8895 #define GL_STREAM_DRAW 0x88E0 typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); @@ -264,7 +277,13 @@ typedef khronos_int16_t GLshort; typedef khronos_int8_t GLbyte; typedef khronos_uint16_t GLushort; #define GL_BLEND_EQUATION_RGB 0x8009 +#define GL_VERTEX_ATTRIB_ARRAY_ENABLED 0x8622 +#define GL_VERTEX_ATTRIB_ARRAY_SIZE 0x8623 +#define GL_VERTEX_ATTRIB_ARRAY_STRIDE 0x8624 +#define GL_VERTEX_ATTRIB_ARRAY_TYPE 0x8625 +#define GL_VERTEX_ATTRIB_ARRAY_POINTER 0x8645 #define GL_BLEND_EQUATION_ALPHA 0x883D +#define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED 0x886A #define GL_FRAGMENT_SHADER 0x8B30 #define GL_VERTEX_SHADER 0x8B31 #define GL_COMPILE_STATUS 0x8B81 @@ -280,6 +299,7 @@ typedef GLuint (APIENTRYP PFNGLCREATESHADERPROC) (GLenum type); typedef void (APIENTRYP PFNGLDELETEPROGRAMPROC) (GLuint program); typedef void (APIENTRYP PFNGLDELETESHADERPROC) (GLuint shader); typedef void (APIENTRYP PFNGLDETACHSHADERPROC) (GLuint program, GLuint shader); +typedef void (APIENTRYP PFNGLDISABLEVERTEXATTRIBARRAYPROC) (GLuint index); typedef void (APIENTRYP PFNGLENABLEVERTEXATTRIBARRAYPROC) (GLuint index); typedef GLint (APIENTRYP PFNGLGETATTRIBLOCATIONPROC) (GLuint program, const GLchar *name); typedef void (APIENTRYP PFNGLGETPROGRAMIVPROC) (GLuint program, GLenum pname, GLint *params); @@ -287,6 +307,8 @@ typedef void (APIENTRYP PFNGLGETPROGRAMINFOLOGPROC) (GLuint program, GLsizei buf typedef void (APIENTRYP PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params); typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name); +typedef void (APIENTRYP PFNGLGETVERTEXATTRIBIVPROC) (GLuint index, GLenum pname, GLint *params); +typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERVPROC) (GLuint index, GLenum pname, void **pointer); typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program); typedef void (APIENTRYP PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program); @@ -302,6 +324,7 @@ GLAPI GLuint APIENTRY glCreateShader (GLenum type); GLAPI void APIENTRY glDeleteProgram (GLuint program); GLAPI void APIENTRY glDeleteShader (GLuint shader); GLAPI void APIENTRY glDetachShader (GLuint program, GLuint shader); +GLAPI void APIENTRY glDisableVertexAttribArray (GLuint index); GLAPI void APIENTRY glEnableVertexAttribArray (GLuint index); GLAPI GLint APIENTRY glGetAttribLocation (GLuint program, const GLchar *name); GLAPI void APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint *params); @@ -309,6 +332,8 @@ GLAPI void APIENTRY glGetProgramInfoLog (GLuint program, GLsizei bufSize, GLsize GLAPI void APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint *params); GLAPI void APIENTRY glGetShaderInfoLog (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog); GLAPI GLint APIENTRY glGetUniformLocation (GLuint program, const GLchar *name); +GLAPI void APIENTRY glGetVertexAttribiv (GLuint index, GLenum pname, GLint *params); +GLAPI void APIENTRY glGetVertexAttribPointerv (GLuint index, GLenum pname, void **pointer); GLAPI void APIENTRY glLinkProgram (GLuint program); GLAPI void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); GLAPI void APIENTRY glUseProgram (GLuint program); @@ -437,122 +462,130 @@ GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); /* gl3w internal state */ union GL3WProcs { - GL3WglProc ptr[53]; + GL3WglProc ptr[58]; struct { - PFNGLACTIVETEXTUREPROC ActiveTexture; - PFNGLATTACHSHADERPROC AttachShader; - PFNGLBINDBUFFERPROC BindBuffer; - PFNGLBINDSAMPLERPROC BindSampler; - PFNGLBINDTEXTUREPROC BindTexture; - PFNGLBINDVERTEXARRAYPROC BindVertexArray; - PFNGLBLENDEQUATIONPROC BlendEquation; - PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate; - PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate; - PFNGLBUFFERDATAPROC BufferData; - PFNGLBUFFERSUBDATAPROC BufferSubData; - PFNGLCLEARPROC Clear; - PFNGLCLEARCOLORPROC ClearColor; - PFNGLCOMPILESHADERPROC CompileShader; - PFNGLCREATEPROGRAMPROC CreateProgram; - PFNGLCREATESHADERPROC CreateShader; - PFNGLDELETEBUFFERSPROC DeleteBuffers; - PFNGLDELETEPROGRAMPROC DeleteProgram; - PFNGLDELETESHADERPROC DeleteShader; - PFNGLDELETETEXTURESPROC DeleteTextures; - PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays; - PFNGLDETACHSHADERPROC DetachShader; - PFNGLDISABLEPROC Disable; - PFNGLDRAWELEMENTSPROC DrawElements; - PFNGLDRAWELEMENTSBASEVERTEXPROC DrawElementsBaseVertex; - PFNGLENABLEPROC Enable; - PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray; - PFNGLGENBUFFERSPROC GenBuffers; - PFNGLGENTEXTURESPROC GenTextures; - PFNGLGENVERTEXARRAYSPROC GenVertexArrays; - PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; - PFNGLGETERRORPROC GetError; - PFNGLGETINTEGERVPROC GetIntegerv; - PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog; - PFNGLGETPROGRAMIVPROC GetProgramiv; - PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog; - PFNGLGETSHADERIVPROC GetShaderiv; - PFNGLGETSTRINGPROC GetString; - PFNGLGETSTRINGIPROC GetStringi; - PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation; - PFNGLISENABLEDPROC IsEnabled; - PFNGLLINKPROGRAMPROC LinkProgram; - PFNGLPIXELSTOREIPROC PixelStorei; - PFNGLPOLYGONMODEPROC PolygonMode; - PFNGLREADPIXELSPROC ReadPixels; - PFNGLSCISSORPROC Scissor; - PFNGLSHADERSOURCEPROC ShaderSource; - PFNGLTEXIMAGE2DPROC TexImage2D; - PFNGLTEXPARAMETERIPROC TexParameteri; - PFNGLUNIFORM1IPROC Uniform1i; - PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv; - PFNGLUSEPROGRAMPROC UseProgram; - PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer; - PFNGLVIEWPORTPROC Viewport; + PFNGLACTIVETEXTUREPROC ActiveTexture; + PFNGLATTACHSHADERPROC AttachShader; + PFNGLBINDBUFFERPROC BindBuffer; + PFNGLBINDSAMPLERPROC BindSampler; + PFNGLBINDTEXTUREPROC BindTexture; + PFNGLBINDVERTEXARRAYPROC BindVertexArray; + PFNGLBLENDEQUATIONPROC BlendEquation; + PFNGLBLENDEQUATIONSEPARATEPROC BlendEquationSeparate; + PFNGLBLENDFUNCSEPARATEPROC BlendFuncSeparate; + PFNGLBUFFERDATAPROC BufferData; + PFNGLBUFFERSUBDATAPROC BufferSubData; + PFNGLCLEARPROC Clear; + PFNGLCLEARCOLORPROC ClearColor; + PFNGLCOMPILESHADERPROC CompileShader; + PFNGLCREATEPROGRAMPROC CreateProgram; + PFNGLCREATESHADERPROC CreateShader; + PFNGLDELETEBUFFERSPROC DeleteBuffers; + PFNGLDELETEPROGRAMPROC DeleteProgram; + PFNGLDELETESHADERPROC DeleteShader; + PFNGLDELETETEXTURESPROC DeleteTextures; + PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays; + PFNGLDETACHSHADERPROC DetachShader; + PFNGLDISABLEPROC Disable; + PFNGLDISABLEVERTEXATTRIBARRAYPROC DisableVertexAttribArray; + PFNGLDRAWELEMENTSPROC DrawElements; + PFNGLDRAWELEMENTSBASEVERTEXPROC DrawElementsBaseVertex; + PFNGLENABLEPROC Enable; + PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray; + PFNGLFLUSHPROC Flush; + PFNGLGENBUFFERSPROC GenBuffers; + PFNGLGENTEXTURESPROC GenTextures; + PFNGLGENVERTEXARRAYSPROC GenVertexArrays; + PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; + PFNGLGETERRORPROC GetError; + PFNGLGETINTEGERVPROC GetIntegerv; + PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog; + PFNGLGETPROGRAMIVPROC GetProgramiv; + PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog; + PFNGLGETSHADERIVPROC GetShaderiv; + PFNGLGETSTRINGPROC GetString; + PFNGLGETSTRINGIPROC GetStringi; + PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation; + PFNGLGETVERTEXATTRIBPOINTERVPROC GetVertexAttribPointerv; + PFNGLGETVERTEXATTRIBIVPROC GetVertexAttribiv; + PFNGLISENABLEDPROC IsEnabled; + PFNGLLINKPROGRAMPROC LinkProgram; + PFNGLPIXELSTOREIPROC PixelStorei; + PFNGLPOLYGONMODEPROC PolygonMode; + PFNGLREADPIXELSPROC ReadPixels; + PFNGLSCISSORPROC Scissor; + PFNGLSHADERSOURCEPROC ShaderSource; + PFNGLTEXIMAGE2DPROC TexImage2D; + PFNGLTEXPARAMETERIPROC TexParameteri; + PFNGLUNIFORM1IPROC Uniform1i; + PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv; + PFNGLUSEPROGRAMPROC UseProgram; + PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer; + PFNGLVIEWPORTPROC Viewport; } gl; }; GL3W_API extern union GL3WProcs imgl3wProcs; /* OpenGL functions */ -#define glActiveTexture imgl3wProcs.gl.ActiveTexture -#define glAttachShader imgl3wProcs.gl.AttachShader -#define glBindBuffer imgl3wProcs.gl.BindBuffer -#define glBindSampler imgl3wProcs.gl.BindSampler -#define glBindTexture imgl3wProcs.gl.BindTexture -#define glBindVertexArray imgl3wProcs.gl.BindVertexArray -#define glBlendEquation imgl3wProcs.gl.BlendEquation -#define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate -#define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate -#define glBufferData imgl3wProcs.gl.BufferData -#define glBufferSubData imgl3wProcs.gl.BufferSubData -#define glClear imgl3wProcs.gl.Clear -#define glClearColor imgl3wProcs.gl.ClearColor -#define glCompileShader imgl3wProcs.gl.CompileShader -#define glCreateProgram imgl3wProcs.gl.CreateProgram -#define glCreateShader imgl3wProcs.gl.CreateShader -#define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers -#define glDeleteProgram imgl3wProcs.gl.DeleteProgram -#define glDeleteShader imgl3wProcs.gl.DeleteShader -#define glDeleteTextures imgl3wProcs.gl.DeleteTextures -#define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays -#define glDetachShader imgl3wProcs.gl.DetachShader -#define glDisable imgl3wProcs.gl.Disable -#define glDrawElements imgl3wProcs.gl.DrawElements -#define glDrawElementsBaseVertex imgl3wProcs.gl.DrawElementsBaseVertex -#define glEnable imgl3wProcs.gl.Enable -#define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray -#define glGenBuffers imgl3wProcs.gl.GenBuffers -#define glGenTextures imgl3wProcs.gl.GenTextures -#define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays -#define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation -#define glGetError imgl3wProcs.gl.GetError -#define glGetIntegerv imgl3wProcs.gl.GetIntegerv -#define glGetProgramInfoLog imgl3wProcs.gl.GetProgramInfoLog -#define glGetProgramiv imgl3wProcs.gl.GetProgramiv -#define glGetShaderInfoLog imgl3wProcs.gl.GetShaderInfoLog -#define glGetShaderiv imgl3wProcs.gl.GetShaderiv -#define glGetString imgl3wProcs.gl.GetString -#define glGetStringi imgl3wProcs.gl.GetStringi -#define glGetUniformLocation imgl3wProcs.gl.GetUniformLocation -#define glIsEnabled imgl3wProcs.gl.IsEnabled -#define glLinkProgram imgl3wProcs.gl.LinkProgram -#define glPixelStorei imgl3wProcs.gl.PixelStorei -#define glPolygonMode imgl3wProcs.gl.PolygonMode -#define glReadPixels imgl3wProcs.gl.ReadPixels -#define glScissor imgl3wProcs.gl.Scissor -#define glShaderSource imgl3wProcs.gl.ShaderSource -#define glTexImage2D imgl3wProcs.gl.TexImage2D -#define glTexParameteri imgl3wProcs.gl.TexParameteri -#define glUniform1i imgl3wProcs.gl.Uniform1i -#define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv -#define glUseProgram imgl3wProcs.gl.UseProgram -#define glVertexAttribPointer imgl3wProcs.gl.VertexAttribPointer -#define glViewport imgl3wProcs.gl.Viewport +#define glActiveTexture imgl3wProcs.gl.ActiveTexture +#define glAttachShader imgl3wProcs.gl.AttachShader +#define glBindBuffer imgl3wProcs.gl.BindBuffer +#define glBindSampler imgl3wProcs.gl.BindSampler +#define glBindTexture imgl3wProcs.gl.BindTexture +#define glBindVertexArray imgl3wProcs.gl.BindVertexArray +#define glBlendEquation imgl3wProcs.gl.BlendEquation +#define glBlendEquationSeparate imgl3wProcs.gl.BlendEquationSeparate +#define glBlendFuncSeparate imgl3wProcs.gl.BlendFuncSeparate +#define glBufferData imgl3wProcs.gl.BufferData +#define glBufferSubData imgl3wProcs.gl.BufferSubData +#define glClear imgl3wProcs.gl.Clear +#define glClearColor imgl3wProcs.gl.ClearColor +#define glCompileShader imgl3wProcs.gl.CompileShader +#define glCreateProgram imgl3wProcs.gl.CreateProgram +#define glCreateShader imgl3wProcs.gl.CreateShader +#define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers +#define glDeleteProgram imgl3wProcs.gl.DeleteProgram +#define glDeleteShader imgl3wProcs.gl.DeleteShader +#define glDeleteTextures imgl3wProcs.gl.DeleteTextures +#define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays +#define glDetachShader imgl3wProcs.gl.DetachShader +#define glDisable imgl3wProcs.gl.Disable +#define glDisableVertexAttribArray imgl3wProcs.gl.DisableVertexAttribArray +#define glDrawElements imgl3wProcs.gl.DrawElements +#define glDrawElementsBaseVertex imgl3wProcs.gl.DrawElementsBaseVertex +#define glEnable imgl3wProcs.gl.Enable +#define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray +#define glFlush imgl3wProcs.gl.Flush +#define glGenBuffers imgl3wProcs.gl.GenBuffers +#define glGenTextures imgl3wProcs.gl.GenTextures +#define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays +#define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation +#define glGetError imgl3wProcs.gl.GetError +#define glGetIntegerv imgl3wProcs.gl.GetIntegerv +#define glGetProgramInfoLog imgl3wProcs.gl.GetProgramInfoLog +#define glGetProgramiv imgl3wProcs.gl.GetProgramiv +#define glGetShaderInfoLog imgl3wProcs.gl.GetShaderInfoLog +#define glGetShaderiv imgl3wProcs.gl.GetShaderiv +#define glGetString imgl3wProcs.gl.GetString +#define glGetStringi imgl3wProcs.gl.GetStringi +#define glGetUniformLocation imgl3wProcs.gl.GetUniformLocation +#define glGetVertexAttribPointerv imgl3wProcs.gl.GetVertexAttribPointerv +#define glGetVertexAttribiv imgl3wProcs.gl.GetVertexAttribiv +#define glIsEnabled imgl3wProcs.gl.IsEnabled +#define glLinkProgram imgl3wProcs.gl.LinkProgram +#define glPixelStorei imgl3wProcs.gl.PixelStorei +#define glPolygonMode imgl3wProcs.gl.PolygonMode +#define glReadPixels imgl3wProcs.gl.ReadPixels +#define glScissor imgl3wProcs.gl.Scissor +#define glShaderSource imgl3wProcs.gl.ShaderSource +#define glTexImage2D imgl3wProcs.gl.TexImage2D +#define glTexParameteri imgl3wProcs.gl.TexParameteri +#define glUniform1i imgl3wProcs.gl.Uniform1i +#define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv +#define glUseProgram imgl3wProcs.gl.UseProgram +#define glVertexAttribPointer imgl3wProcs.gl.VertexAttribPointer +#define glViewport imgl3wProcs.gl.Viewport #ifdef __cplusplus } @@ -709,10 +742,12 @@ static const char *proc_names[] = { "glDeleteVertexArrays", "glDetachShader", "glDisable", + "glDisableVertexAttribArray", "glDrawElements", "glDrawElementsBaseVertex", "glEnable", "glEnableVertexAttribArray", + "glFlush", "glGenBuffers", "glGenTextures", "glGenVertexArrays", @@ -726,6 +761,8 @@ static const char *proc_names[] = { "glGetString", "glGetStringi", "glGetUniformLocation", + "glGetVertexAttribPointerv", + "glGetVertexAttribiv", "glIsEnabled", "glLinkProgram", "glPixelStorei", diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index 73d58f6b..e57eab77 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (internal structures/api) // You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! @@ -18,6 +18,7 @@ Index of this file: // [SECTION] Generic helpers // [SECTION] ImDrawList support // [SECTION] Widgets support: flags, enums, data structures +// [SECTION] Inputs support // [SECTION] Clipper support // [SECTION] Navigation support // [SECTION] Columns support @@ -25,6 +26,7 @@ Index of this file: // [SECTION] Docking support // [SECTION] Viewport support // [SECTION] Settings support +// [SECTION] Localization support // [SECTION] Metrics, Debug tools // [SECTION] Generic context hooks // [SECTION] ImGuiContext (main imgui context) @@ -54,7 +56,7 @@ Index of this file: #include // INT_MIN, INT_MAX // Enable SSE intrinsics if available -#if (defined __SSE__ || defined __x86_64__ || defined _M_X64) && !defined(IMGUI_DISABLE_SSE) +#if (defined __SSE__ || defined __x86_64__ || defined _M_X64 || (defined(_M_IX86_FP) && (_M_IX86_FP >= 1))) && !defined(IMGUI_DISABLE_SSE) #define IMGUI_ENABLE_SSE #include #endif @@ -120,6 +122,7 @@ struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDat struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiLastItemData; // Status storage for last submitted items +struct ImGuiLocEntry; // A localization entry. struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions @@ -135,6 +138,7 @@ struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) struct ImGuiTable; // Storage for a table struct ImGuiTableColumn; // Storage for one column of a table +struct ImGuiTableInstanceData; // Storage for one instance of a same table struct ImGuiTableTempData; // Temporary storage for one table (one per table in the stack), shared between tables. struct ImGuiTableSettings; // Storage for a table .ini settings struct ImGuiTableColumnsSettings; // Storage for a column .ini settings @@ -142,14 +146,19 @@ struct ImGuiWindow; // Storage for one window struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame, in practice we currently keep it for each window) struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session) +// Enumerations // Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists. +enum ImGuiLocKey : int; // -> enum ImGuiLocKey // Enum: a localization entry for translation. typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical + +// Flags typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later) -typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag() -typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for DC.LastItemStatusFlags +typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags +typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: for IsKeyPressed(), IsMouseClicked(), SetKeyOwner(), SetItemKeyOwner() etc. +typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag(), g.LastItemData.InFlags +typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for g.LastItemData.StatusFlags typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() -typedef int ImGuiNavDirSourceFlags; // -> enum ImGuiNavDirSourceFlags_ // Flags: for GetNavInputAmount2d() typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests typedef int ImGuiNextItemDataFlags; // -> enum ImGuiNextItemDataFlags_ // Flags: for SetNextItemXXX() functions typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// Flags: for SetNextWindowXXX() functions @@ -191,23 +200,31 @@ namespace ImStb // [SECTION] Macros //----------------------------------------------------------------------------- -// Debug Logging -#ifndef IMGUI_DEBUG_LOG -#define IMGUI_DEBUG_LOG(_FMT,...) printf("[%05d] " _FMT, GImGui->FrameCount, __VA_ARGS__) +// Debug Printing Into TTY +// (since IMGUI_VERSION_NUM >= 18729: IMGUI_DEBUG_LOG was reworked into IMGUI_DEBUG_PRINTF (and removed framecount from it). If you were using a #define IMGUI_DEBUG_LOG please rename) +#ifndef IMGUI_DEBUG_PRINTF +#ifndef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS +#define IMGUI_DEBUG_PRINTF(_FMT,...) printf(_FMT, __VA_ARGS__) +#else +#define IMGUI_DEBUG_PRINTF(_FMT,...) ((void)0) +#endif #endif -// Debug Logging for selected systems. Remove the '((void)0) //' to enable. -//#define IMGUI_DEBUG_LOG_POPUP IMGUI_DEBUG_LOG // Enable log -//#define IMGUI_DEBUG_LOG_NAV IMGUI_DEBUG_LOG // Enable log -#define IMGUI_DEBUG_LOG_POPUP(...) ((void)0) // Disable log -#define IMGUI_DEBUG_LOG_NAV(...) ((void)0) // Disable log +// Debug Logging for ShowDebugLogWindow(). This is designed for relatively rare events so please don't spam. +#ifndef IMGUI_DISABLE_DEBUG_TOOLS +#define IMGUI_DEBUG_LOG(...) ImGui::DebugLog(__VA_ARGS__) +#else +#define IMGUI_DEBUG_LOG(...) ((void)0) +#endif +#define IMGUI_DEBUG_LOG_ACTIVEID(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventActiveId) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_FOCUS(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFocus) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_POPUP(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_NAV(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventNav) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) // Static Asserts -#if (__cplusplus >= 201100) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201100) #define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") -#else -#define IM_STATIC_ASSERT(_COND) typedef char static_assertion_##__line__[(_COND)?1:-1] -#endif // "Paranoid" Debug Asserts are meant to only be enabled during specific debugging/work, otherwise would slow down the code too much. // We currently don't have many of those so the effect is currently negligible, but onward intent to add more aggressive ones in the code. @@ -231,8 +248,10 @@ namespace ImStb #else #define IM_NEWLINE "\n" #endif +#ifndef IM_TABSIZE // Until we move this to runtime and/or add proper tab support, at least allow users to compile-time override #define IM_TABSIZE (4) -#define IM_MEMALIGN(_OFF,_ALIGN) (((_OFF) + (_ALIGN - 1)) & ~(_ALIGN - 1)) // Memory align e.g. IM_ALIGN(0,4)=0, IM_ALIGN(1,4)=4, IM_ALIGN(4,4)=4, IM_ALIGN(5,4)=8 +#endif +#define IM_MEMALIGN(_OFF,_ALIGN) (((_OFF) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1)) // Memory align e.g. IM_ALIGN(0,4)=0, IM_ALIGN(1,4)=4, IM_ALIGN(4,4)=4, IM_ALIGN(5,4)=8 #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 #define IM_FLOOR(_VAL) ((float)(int)(_VAL)) // ImFloor() is not inlined in MSVC debug builds @@ -279,7 +298,8 @@ namespace ImStb // - Helpers: Hashing // - Helpers: Sorting // - Helpers: Bit manipulation -// - Helpers: String, Formatting +// - Helpers: String +// - Helpers: Formatting // - Helpers: UTF-8 <> wchar conversions // - Helpers: ImVec2/ImVec4 operators // - Helpers: Maths @@ -292,14 +312,12 @@ namespace ImStb // - Helper: ImSpan<>, ImSpanAllocator<> // - Helper: ImPool<> // - Helper: ImChunkStream<> +// - Helper: ImGuiTextIndex //----------------------------------------------------------------------------- // Helpers: Hashing IMGUI_API ImGuiID ImHashData(const void* data, size_t data_size, ImU32 seed = 0); IMGUI_API ImGuiID ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -static inline ImGuiID ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68] -#endif // Helpers: Sorting #ifndef ImQsort @@ -314,7 +332,7 @@ static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v & (v - 1)) == 0; } static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } -// Helpers: String, Formatting +// Helpers: String IMGUI_API int ImStricmp(const char* str1, const char* str2); IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); @@ -327,14 +345,23 @@ IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* bu IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); IMGUI_API void ImStrTrimBlanks(char* str); IMGUI_API const char* ImStrSkipBlank(const char* str); +IM_MSVC_RUNTIME_CHECKS_OFF +static inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; } +static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } +static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } +IM_MSVC_RUNTIME_CHECKS_RESTORE + +// Helpers: Formatting IMGUI_API int ImFormatString(char* buf, size_t buf_size, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) IM_FMTLIST(3); +IMGUI_API void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, const char* fmt, ...) IM_FMTARGS(3); +IMGUI_API void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API const char* ImParseFormatFindStart(const char* format); IMGUI_API const char* ImParseFormatFindEnd(const char* format); IMGUI_API const char* ImParseFormatTrimDecorations(const char* format, char* buf, size_t buf_size); +IMGUI_API void ImParseFormatSanitizeForPrinting(const char* fmt_in, char* fmt_out, size_t fmt_out_size); +IMGUI_API const char* ImParseFormatSanitizeForScanning(const char* fmt_in, char* fmt_out, size_t fmt_out_size); IMGUI_API int ImParseFormatPrecision(const char* format, int default_value); -static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } -static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } // Helpers: UTF-8 <> wchar conversions IMGUI_API const char* ImTextCharToUtf8(char out_buf[5], unsigned int c); // return out_buf @@ -441,14 +468,16 @@ static inline float ImLengthSqr(const ImVec2& lhs) static inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); } static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return ImRsqrt(d); return fail_value; } static inline float ImFloor(float f) { return (float)(int)(f); } -static inline float ImFloorSigned(float f) { return (float)((f >= 0 || (int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf() +static inline float ImFloorSigned(float f) { return (float)((f >= 0 || (float)(int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf() static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } +static inline ImVec2 ImFloorSigned(const ImVec2& v) { return ImVec2(ImFloorSigned(v.x), ImFloorSigned(v.y)); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } static inline bool ImIsFloatAboveGuaranteedIntegerPrecision(float f) { return f <= -16777216 || f >= 16777216; } +static inline float ImExponentialMovingAverage(float avg, float sample, int n) { avg -= avg / n; avg += sample / n; return avg; } IM_MSVC_RUNTIME_CHECKS_RESTORE // Helpers: Geometry @@ -469,17 +498,17 @@ IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec1 { float x; - ImVec1() { x = 0.0f; } - ImVec1(float _x) { x = _x; } + constexpr ImVec1() : x(0.0f) { } + constexpr ImVec1(float _x) : x(_x) { } }; // Helper: ImVec2ih (2D vector, half-size integer, for long-term packed storage) struct ImVec2ih { short x, y; - ImVec2ih() { x = y = 0; } - ImVec2ih(short _x, short _y) { x = _x; y = _y; } - explicit ImVec2ih(const ImVec2& rhs) { x = (short)rhs.x; y = (short)rhs.y; } + constexpr ImVec2ih() : x(0), y(0) {} + constexpr ImVec2ih(short _x, short _y) : x(_x), y(_y) {} + constexpr explicit ImVec2ih(const ImVec2& rhs) : x((short)rhs.x), y((short)rhs.y) {} }; // Helper: ImRect (2D axis aligned bounding-box) @@ -489,10 +518,10 @@ struct IMGUI_API ImRect ImVec2 Min; // Upper-left ImVec2 Max; // Lower-right - ImRect() : Min(0.0f, 0.0f), Max(0.0f, 0.0f) {} - ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} - ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} - ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} + constexpr ImRect() : Min(0.0f, 0.0f), Max(0.0f, 0.0f) {} + constexpr ImRect(const ImVec2& min, const ImVec2& max) : Min(min), Max(max) {} + constexpr ImRect(const ImVec4& v) : Min(v.x, v.y), Max(v.z, v.w) {} + constexpr ImRect(float x1, float y1, float x2, float y2) : Min(x1, y1), Max(x2, y2) {} ImVec2 GetCenter() const { return ImVec2((Min.x + Max.x) * 0.5f, (Min.y + Max.y) * 0.5f); } ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); } @@ -540,17 +569,18 @@ inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) // Works on ran // Helper: ImBitArray class (wrapper over ImBitArray functions) // Store 1-bit per value. -template -struct IMGUI_API ImBitArray +template +struct ImBitArray { ImU32 Storage[(BITCOUNT + 31) >> 5]; ImBitArray() { ClearAllBits(); } void ClearAllBits() { memset(Storage, 0, sizeof(Storage)); } void SetAllBits() { memset(Storage, 255, sizeof(Storage)); } - bool TestBit(int n) const { IM_ASSERT(n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } - void SetBit(int n) { IM_ASSERT(n < BITCOUNT); ImBitArraySetBit(Storage, n); } - void ClearBit(int n) { IM_ASSERT(n < BITCOUNT); ImBitArrayClearBit(Storage, n); } - void SetBitRange(int n, int n2) { ImBitArraySetBitRange(Storage, n, n2); } // Works on range [n..n2) + bool TestBit(int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } + void SetBit(int n) { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); ImBitArraySetBit(Storage, n); } + void ClearBit(int n) { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); ImBitArrayClearBit(Storage, n); } + void SetBitRange(int n, int n2) { n += OFFSET; n2 += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT && n2 > n && n2 <= BITCOUNT); ImBitArraySetBitRange(Storage, n, n2); } // Works on range [n..n2) + bool operator[](int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } }; // Helper: ImBitVector @@ -621,7 +651,7 @@ struct ImSpanAllocator // Honor constructor/destructor. Add/remove invalidate all pointers. Indexes have the same lifetime as the associated object. typedef int ImPoolIdx; template -struct IMGUI_API ImPool +struct ImPool { ImVector Buf; // Contiguous data ImGuiStorage Map; // ID->Index @@ -658,7 +688,7 @@ struct IMGUI_API ImPool // We store the chunk size first, and align the final size on 4 bytes boundaries. // The tedious/zealous amount of casting is to avoid -Wcast-align warnings. template -struct IMGUI_API ImChunkStream +struct ImChunkStream { ImVector Buf; @@ -676,6 +706,20 @@ struct IMGUI_API ImChunkStream }; +// Helper: ImGuiTextIndex<> +// Maintain a line index for a text buffer. This is a strong candidate to be moved into the public API. +struct ImGuiTextIndex +{ + ImVector LineOffsets; + int EndOffset = 0; // Because we don't own text buffer we need to maintain EndOffset (may bake in LineOffsets?) + + void clear() { LineOffsets.clear(); EndOffset = 0; } + int size() { return LineOffsets.Size; } + const char* get_line_begin(const char* base, int n) { return base + LineOffsets[n]; } + const char* get_line_end(const char* base, int n) { return base + (n + 1 < LineOffsets.Size ? (LineOffsets[n + 1] - 1) : EndOffset); } + void append(const char* base, int old_size, int new_size); +}; + //----------------------------------------------------------------------------- // [SECTION] ImDrawList support //----------------------------------------------------------------------------- @@ -690,7 +734,6 @@ struct IMGUI_API ImChunkStream // // Rendering circles with an odd number of segments, while mathematically correct will produce // asymmetrical results on the raster grid. Therefore we're rounding N to next even number (7->8, 8->8, 9->10 etc.) -// #define IM_ROUNDUP_TO_EVEN(_V) ((((_V) + 1) / 2) * 2) #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MIN 4 #define IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_MAX 512 @@ -718,6 +761,9 @@ struct IMGUI_API ImDrawListSharedData ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() ImDrawListFlags InitialFlags; // Initial flags at the beginning of the frame (it is possible to alter flags on a per-drawlist basis afterwards) + // [Internal] Temp write buffer + ImVector TempBuffer; + // [Internal] Lookup tables ImVec2 ArcFastVtx[IM_DRAWLIST_ARCFAST_TABLE_SIZE]; // Sample points on the quarter of the circle. float ArcFastRadiusCutoff; // Cutoff radius after which arc drawing will fallback to slower PathArcTo() @@ -742,10 +788,14 @@ struct ImDrawDataBuilder // [SECTION] Widgets support: flags, enums, data structures //----------------------------------------------------------------------------- -// Transient per-window flags, reset at the beginning of the frame. For child window, inherited from parent on first Begin(). +// Flags used by upcoming items +// - input: PushItemFlag() manipulates g.CurrentItemFlags, ItemAdd() calls may add extra flags. +// - output: stored in g.LastItemData.InFlags +// Current window shared by all windows. // This is going to be exposed in imgui.h when stabilized enough. enum ImGuiItemFlags_ { + // Controlled by user ImGuiItemFlags_None = 0, ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing (FIXME: should merge with _NoNav) ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. @@ -755,10 +805,14 @@ enum ImGuiItemFlags_ ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // Disable MenuItem/Selectable() automatically closing their popup window ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. - ImGuiItemFlags_Inputable = 1 << 8 // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. + ImGuiItemFlags_NoWindowHoverableCheck = 1 << 8, // false // Disable hoverable check in ItemHoverable() + + // Controlled by widget code + ImGuiItemFlags_Inputable = 1 << 10, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. }; -// Storage for LastItem data +// Status flags for an already submitted item +// - output: stored in g.LastItemData.StatusFlags enum ImGuiItemStatusFlags_ { ImGuiItemStatusFlags_None = 0, @@ -770,14 +824,14 @@ enum ImGuiItemStatusFlags_ ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. ImGuiItemStatusFlags_HoveredWindow = 1 << 7, // Override the HoveredWindow test to allow cross-window hover testing. - ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8 // Set when the Focusable item just got focused by Tabbing (FIXME: to be removed soon) + ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8, // Set when the Focusable item just got focused by Tabbing (FIXME: to be removed soon) + ImGuiItemStatusFlags_Visible = 1 << 9, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()). #ifdef IMGUI_ENABLE_TEST_ENGINE - , // [imgui_tests only] - ImGuiItemStatusFlags_Openable = 1 << 20, // + ImGuiItemStatusFlags_Openable = 1 << 20, // Item is an openable (e.g. TreeNode) ImGuiItemStatusFlags_Opened = 1 << 21, // - ImGuiItemStatusFlags_Checkable = 1 << 22, // - ImGuiItemStatusFlags_Checked = 1 << 23 // + ImGuiItemStatusFlags_Checkable = 1 << 22, // Item is a checkable (e.g. CheckBox, MenuItem) + ImGuiItemStatusFlags_Checked = 1 << 23, // #endif }; @@ -787,7 +841,7 @@ enum ImGuiInputTextFlagsPrivate_ // [Internal] ImGuiInputTextFlags_Multiline = 1 << 26, // For internal use by InputTextMultiline() ImGuiInputTextFlags_NoMarkEdited = 1 << 27, // For internal use by functions using InputText() before reformatting data - ImGuiInputTextFlags_MergedItem = 1 << 28 // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. + ImGuiInputTextFlags_MergedItem = 1 << 28, // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. }; // Extend ImGuiButtonFlags_ @@ -807,23 +861,25 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine ImGuiButtonFlags_NoKeyModifiers = 1 << 16, // disable mouse interaction if a key modifier is held ImGuiButtonFlags_NoHoldingActiveId = 1 << 17, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) - ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated + ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated (FIXME: this is essentially used everytime an item uses ImGuiItemFlags_NoNav, but because legacy specs don't requires LastItemData to be set ButtonBehavior(), we can't poll g.LastItemData.InFlags) ImGuiButtonFlags_NoHoveredOnFocus = 1 << 19, // don't report as hovered when nav focus is on this item + ImGuiButtonFlags_NoSetKeyOwner = 1 << 20, // don't set key/input owner on the initial click (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) + ImGuiButtonFlags_NoTestKeyOwner = 1 << 21, // don't test key/input owner when polling the key (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) ImGuiButtonFlags_PressedOnMask_ = ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere | ImGuiButtonFlags_PressedOnRelease | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_PressedOnDragDropHold, - ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease + ImGuiButtonFlags_PressedOnDefault_ = ImGuiButtonFlags_PressedOnClickRelease, }; // Extend ImGuiComboFlags_ enum ImGuiComboFlagsPrivate_ { - ImGuiComboFlags_CustomPreview = 1 << 20 // enable BeginComboPreview() + ImGuiComboFlags_CustomPreview = 1 << 20, // enable BeginComboPreview() }; // Extend ImGuiSliderFlags_ enum ImGuiSliderFlagsPrivate_ { ImGuiSliderFlags_Vertical = 1 << 20, // Should this slider be orientated vertically? - ImGuiSliderFlags_ReadOnly = 1 << 21 + ImGuiSliderFlags_ReadOnly = 1 << 21, }; // Extend ImGuiSelectableFlags_ @@ -835,35 +891,35 @@ enum ImGuiSelectableFlagsPrivate_ ImGuiSelectableFlags_SelectOnClick = 1 << 22, // Override button behavior to react on Click (default is Click+Release) ImGuiSelectableFlags_SelectOnRelease = 1 << 23, // Override button behavior to react on Release (default is Click+Release) ImGuiSelectableFlags_SpanAvailWidth = 1 << 24, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus) - ImGuiSelectableFlags_DrawHoveredWhenHeld = 1 << 25, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow. - ImGuiSelectableFlags_SetNavIdOnHover = 1 << 26, // Set Nav/Focus ID on mouse hover (used by MenuItem) - ImGuiSelectableFlags_NoPadWithHalfSpacing = 1 << 27 // Disable padding each side with ItemSpacing * 0.5f + ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25, // Set Nav/Focus ID on mouse hover (used by MenuItem) + ImGuiSelectableFlags_NoPadWithHalfSpacing = 1 << 26, // Disable padding each side with ItemSpacing * 0.5f + ImGuiSelectableFlags_NoSetKeyOwner = 1 << 27, // Don't set key/input owner on the initial click (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) }; // Extend ImGuiTreeNodeFlags_ enum ImGuiTreeNodeFlagsPrivate_ { - ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 20 + ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 20, }; enum ImGuiSeparatorFlags_ { - ImGuiSeparatorFlags_None = 0, - ImGuiSeparatorFlags_Horizontal = 1 << 0, // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar - ImGuiSeparatorFlags_Vertical = 1 << 1, - ImGuiSeparatorFlags_SpanAllColumns = 1 << 2 + ImGuiSeparatorFlags_None = 0, + ImGuiSeparatorFlags_Horizontal = 1 << 0, // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar + ImGuiSeparatorFlags_Vertical = 1 << 1, + ImGuiSeparatorFlags_SpanAllColumns = 1 << 2, }; enum ImGuiTextFlags_ { - ImGuiTextFlags_None = 0, - ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0 + ImGuiTextFlags_None = 0, + ImGuiTextFlags_NoWidthForLargeClippedText = 1 << 0, }; enum ImGuiTooltipFlags_ { - ImGuiTooltipFlags_None = 0, - ImGuiTooltipFlags_OverridePreviousTooltip = 1 << 0 // Override will clear/ignore previously submitted tooltip (defaults to append) + ImGuiTooltipFlags_None = 0, + ImGuiTooltipFlags_OverridePreviousTooltip = 1 << 0, // Override will clear/ignore previously submitted tooltip (defaults to append) }; // FIXME: this is in development, not exposed/functional as a generic feature yet. @@ -880,7 +936,7 @@ enum ImGuiLogType ImGuiLogType_TTY, ImGuiLogType_File, ImGuiLogType_Buffer, - ImGuiLogType_Clipboard + ImGuiLogType_Clipboard, }; // X/Y enums are fixed to 0/1 so they may be used to index ImVec2 @@ -894,36 +950,14 @@ enum ImGuiAxis enum ImGuiPlotType { ImGuiPlotType_Lines, - ImGuiPlotType_Histogram -}; - -enum ImGuiInputSource -{ - ImGuiInputSource_None = 0, - ImGuiInputSource_Mouse, - ImGuiInputSource_Keyboard, - ImGuiInputSource_Gamepad, - ImGuiInputSource_Nav, // Stored in g.ActiveIdSource only - ImGuiInputSource_Clipboard, // Currently only used by InputText() - ImGuiInputSource_COUNT -}; - -// FIXME-NAV: Clarify/expose various repeat delay/rate -enum ImGuiInputReadMode -{ - ImGuiInputReadMode_Down, - ImGuiInputReadMode_Pressed, - ImGuiInputReadMode_Released, - ImGuiInputReadMode_Repeat, - ImGuiInputReadMode_RepeatSlow, - ImGuiInputReadMode_RepeatFast + ImGuiPlotType_Histogram, }; enum ImGuiPopupPositionPolicy { ImGuiPopupPositionPolicy_Default, ImGuiPopupPositionPolicy_ComboBox, - ImGuiPopupPositionPolicy_Tooltip + ImGuiPopupPositionPolicy_Tooltip, }; struct ImGuiDataTypeTempStorage @@ -945,14 +979,14 @@ enum ImGuiDataTypePrivate_ { ImGuiDataType_String = ImGuiDataType_COUNT + 1, ImGuiDataType_Pointer, - ImGuiDataType_ID + ImGuiDataType_ID, }; // Stacked color modifier, backup of modified data so we can restore it struct ImGuiColorMod { - ImGuiCol Col; - ImVec4 BackupValue; + ImGuiCol Col; + ImVec4 BackupValue; }; // Stacked style modifier, backup of modified data so we can restore it. Data type inferred from the variable. @@ -1016,6 +1050,7 @@ struct IMGUI_API ImGuiMenuColumns // For a given item ID, access with ImGui::GetInputTextState() struct IMGUI_API ImGuiInputTextState { + ImGuiContext* Ctx; // parent dear imgui context ImGuiID ID; // widget id owning the text state int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not. ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. @@ -1029,9 +1064,9 @@ struct IMGUI_API ImGuiInputTextState bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection bool Edited; // edited this frame - ImGuiInputTextFlags Flags; // copy of InputText() flags + ImGuiInputTextFlags Flags; // copy of InputText() flags. may be used to check if e.g. ImGuiInputTextFlags_Password is set. - ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } + ImGuiInputTextState(ImGuiContext* ctx) { memset(this, 0, sizeof(*this)); Ctx = ctx;} void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } int GetUndoAvailCount() const { return Stb.undostate.undo_point; } @@ -1054,13 +1089,14 @@ struct ImGuiPopupData { ImGuiID PopupId; // Set on OpenPopup() ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() - ImGuiWindow* SourceWindow; // Set on OpenPopup() copy of NavWindow at the time of opening the popup + ImGuiWindow* BackupNavWindow;// Set on OpenPopup(), a NavWindow that will be restored on popup close + int ParentNavLayer; // Resolved on BeginPopup(). Actually a ImGuiNavLayer type (declared down below), initialized to -1 which is not part of an enum, but serves well-enough as "not any of layers" value int OpenFrameCount; // Set on OpenPopup() ImGuiID OpenParentId; // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup - ImGuiPopupData() { memset(this, 0, sizeof(*this)); OpenFrameCount = -1; } + ImGuiPopupData() { memset(this, 0, sizeof(*this)); ParentNavLayer = OpenFrameCount = -1; } }; enum ImGuiNextWindowDataFlags_ @@ -1073,7 +1109,7 @@ enum ImGuiNextWindowDataFlags_ ImGuiNextWindowDataFlags_HasSizeConstraint = 1 << 4, ImGuiNextWindowDataFlags_HasFocus = 1 << 5, ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6, - ImGuiNextWindowDataFlags_HasScroll = 1 << 7 + ImGuiNextWindowDataFlags_HasScroll = 1 << 7, }; // Storage for SetNexWindow** functions @@ -1103,7 +1139,7 @@ enum ImGuiNextItemDataFlags_ { ImGuiNextItemDataFlags_None = 0, ImGuiNextItemDataFlags_HasWidth = 1 << 0, - ImGuiNextItemDataFlags_HasOpen = 1 << 1 + ImGuiNextItemDataFlags_HasOpen = 1 << 1, }; struct ImGuiNextItemData @@ -1160,6 +1196,7 @@ struct ImGuiShrinkWidthItem { int Index; float Width; + float InitialWidth; }; struct ImGuiPtrOrIndex @@ -1171,6 +1208,174 @@ struct ImGuiPtrOrIndex ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } }; +//----------------------------------------------------------------------------- +// [SECTION] Inputs support +//----------------------------------------------------------------------------- + +typedef ImBitArray ImBitArrayForNamedKeys; + +// [Internal] Key ranges +#define ImGuiKey_LegacyNativeKey_BEGIN 0 +#define ImGuiKey_LegacyNativeKey_END 512 +#define ImGuiKey_Keyboard_BEGIN (ImGuiKey_NamedKey_BEGIN) +#define ImGuiKey_Keyboard_END (ImGuiKey_GamepadStart) +#define ImGuiKey_Gamepad_BEGIN (ImGuiKey_GamepadStart) +#define ImGuiKey_Gamepad_END (ImGuiKey_GamepadRStickDown + 1) +#define ImGuiKey_Mouse_BEGIN (ImGuiKey_MouseLeft) +#define ImGuiKey_Mouse_END (ImGuiKey_MouseWheelY + 1) +#define ImGuiKey_Aliases_BEGIN (ImGuiKey_Mouse_BEGIN) +#define ImGuiKey_Aliases_END (ImGuiKey_Mouse_END) + +// [Internal] Named shortcuts for Navigation +#define ImGuiKey_NavKeyboardTweakSlow ImGuiMod_Ctrl +#define ImGuiKey_NavKeyboardTweakFast ImGuiMod_Shift +#define ImGuiKey_NavGamepadTweakSlow ImGuiKey_GamepadL1 +#define ImGuiKey_NavGamepadTweakFast ImGuiKey_GamepadR1 +#define ImGuiKey_NavGamepadActivate ImGuiKey_GamepadFaceDown +#define ImGuiKey_NavGamepadCancel ImGuiKey_GamepadFaceRight +#define ImGuiKey_NavGamepadMenu ImGuiKey_GamepadFaceLeft +#define ImGuiKey_NavGamepadInput ImGuiKey_GamepadFaceUp + +enum ImGuiInputEventType +{ + ImGuiInputEventType_None = 0, + ImGuiInputEventType_MousePos, + ImGuiInputEventType_MouseWheel, + ImGuiInputEventType_MouseButton, + ImGuiInputEventType_Key, + ImGuiInputEventType_Text, + ImGuiInputEventType_Focus, + ImGuiInputEventType_COUNT +}; + +enum ImGuiInputSource +{ + ImGuiInputSource_None = 0, + ImGuiInputSource_Mouse, + ImGuiInputSource_Keyboard, + ImGuiInputSource_Gamepad, + ImGuiInputSource_Clipboard, // Currently only used by InputText() + ImGuiInputSource_Nav, // Stored in g.ActiveIdSource only + ImGuiInputSource_COUNT +}; + +// FIXME: Structures in the union below need to be declared as anonymous unions appears to be an extension? +// Using ImVec2() would fail on Clang 'union member 'MousePos' has a non-trivial default constructor' +struct ImGuiInputEventMousePos { float PosX, PosY; }; +struct ImGuiInputEventMouseWheel { float WheelX, WheelY; }; +struct ImGuiInputEventMouseButton { int Button; bool Down; }; +struct ImGuiInputEventKey { ImGuiKey Key; bool Down; float AnalogValue; }; +struct ImGuiInputEventText { unsigned int Char; }; +struct ImGuiInputEventAppFocused { bool Focused; }; + +struct ImGuiInputEvent +{ + ImGuiInputEventType Type; + ImGuiInputSource Source; + union + { + ImGuiInputEventMousePos MousePos; // if Type == ImGuiInputEventType_MousePos + ImGuiInputEventMouseWheel MouseWheel; // if Type == ImGuiInputEventType_MouseWheel + ImGuiInputEventMouseButton MouseButton; // if Type == ImGuiInputEventType_MouseButton + ImGuiInputEventKey Key; // if Type == ImGuiInputEventType_Key + ImGuiInputEventText Text; // if Type == ImGuiInputEventType_Text + ImGuiInputEventAppFocused AppFocused; // if Type == ImGuiInputEventType_Focus + }; + bool AddedByTestEngine; + + ImGuiInputEvent() { memset(this, 0, sizeof(*this)); } +}; + +// Input function taking an 'ImGuiID owner_id' argument defaults to (ImGuiKeyOwner_Any == 0) aka don't test ownership, which matches legacy behavior. +#define ImGuiKeyOwner_Any ((ImGuiID)0) // Accept key that have an owner, UNLESS a call to SetKeyOwner() explicitly used ImGuiInputFlags_LockThisFrame or ImGuiInputFlags_LockUntilRelease. +#define ImGuiKeyOwner_None ((ImGuiID)-1) // Require key to have no owner. + +typedef ImS16 ImGuiKeyRoutingIndex; + +// Routing table entry (sizeof() == 16 bytes) +struct ImGuiKeyRoutingData +{ + ImGuiKeyRoutingIndex NextEntryIndex; + ImU16 Mods; // Technically we'd only need 4-bits but for simplify we store ImGuiMod_ values which need 16-bits. ImGuiMod_Shortcut is already translated to Ctrl/Super. + ImU8 RoutingNextScore; // Lower is better (0: perfect score) + ImGuiID RoutingCurr; + ImGuiID RoutingNext; + + ImGuiKeyRoutingData() { NextEntryIndex = -1; Mods = 0; RoutingNextScore = 255; RoutingCurr = RoutingNext = ImGuiKeyOwner_None; } +}; + +// Routing table: maintain a desired owner for each possible key-chord (key + mods), and setup owner in NewFrame() when mods are matching. +// Stored in main context (1 instance) +struct ImGuiKeyRoutingTable +{ + ImGuiKeyRoutingIndex Index[ImGuiKey_NamedKey_COUNT]; // Index of first entry in Entries[] + ImVector Entries; + ImVector EntriesNext; // Double-buffer to avoid reallocation (could use a shared buffer) + + ImGuiKeyRoutingTable() { Clear(); } + void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Index); n++) Index[n] = -1; Entries.clear(); EntriesNext.clear(); } +}; + +// This extends ImGuiKeyData but only for named keys (legacy keys don't support the new features) +// Stored in main context (1 per named key). In the future it might be merged into ImGuiKeyData. +struct ImGuiKeyOwnerData +{ + ImGuiID OwnerCurr; + ImGuiID OwnerNext; + bool LockThisFrame; // Reading this key requires explicit owner id (until end of frame). Set by ImGuiInputFlags_LockThisFrame. + bool LockUntilRelease; // Reading this key requires explicit owner id (until key is released). Set by ImGuiInputFlags_LockUntilRelease. When this is true LockThisFrame is always true as well. + + ImGuiKeyOwnerData() { OwnerCurr = OwnerNext = ImGuiKeyOwner_None; LockThisFrame = LockUntilRelease = false; } +}; + +// Flags for extended versions of IsKeyPressed(), IsMouseClicked(), Shortcut(), SetKeyOwner(), SetItemKeyOwner() +// Don't mistake with ImGuiInputTextFlags! (for ImGui::InputText() function) +enum ImGuiInputFlags_ +{ + // Flags for IsKeyPressed(), IsMouseClicked(), Shortcut() + ImGuiInputFlags_None = 0, + ImGuiInputFlags_Repeat = 1 << 0, // Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. + ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) + ImGuiInputFlags_RepeatRateNavMove = 1 << 2, // Repeat rate: Fast + ImGuiInputFlags_RepeatRateNavTweak = 1 << 3, // Repeat rate: Faster + ImGuiInputFlags_RepeatRateMask_ = ImGuiInputFlags_RepeatRateDefault | ImGuiInputFlags_RepeatRateNavMove | ImGuiInputFlags_RepeatRateNavTweak, + + // Flags for SetItemKeyOwner() + ImGuiInputFlags_CondHovered = 1 << 4, // Only set if item is hovered (default to both) + ImGuiInputFlags_CondActive = 1 << 5, // Only set if item is active (default to both) + ImGuiInputFlags_CondDefault_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, + ImGuiInputFlags_CondMask_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, + + // Flags for SetKeyOwner(), SetItemKeyOwner() + ImGuiInputFlags_LockThisFrame = 1 << 6, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. + ImGuiInputFlags_LockUntilRelease = 1 << 7, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. + + // Routing policies for Shortcut() + low-level SetShortcutRouting() + // - The general idea is that several callers register interest in a shortcut, and only one owner gets it. + // - When a policy (other than _RouteAlways) is set, Shortcut() will register itself with SetShortcutRouting(), + // allowing the system to decide where to route the input among other route-aware calls. + // - Shortcut() uses ImGuiInputFlags_RouteFocused by default: meaning that a simple Shortcut() poll + // will register a route and only succeed when parent window is in the focus stack and if no-one + // with a higher priority is claiming the shortcut. + // - Using ImGuiInputFlags_RouteAlways is roughly equivalent to doing e.g. IsKeyPressed(key) + testing mods. + // - Priorities: GlobalHigh > Focused (when owner is active item) > Global > Focused (when focused window) > GlobalLow. + // - Can select only 1 policy among all available. + ImGuiInputFlags_RouteFocused = 1 << 8, // (Default) Register focused route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. + ImGuiInputFlags_RouteGlobalLow = 1 << 9, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority. + ImGuiInputFlags_RouteGlobal = 1 << 10, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText). + ImGuiInputFlags_RouteGlobalHigh = 1 << 11, // Register route globally (highest priority: unlikely you need to use that: will interfere with every active items) + ImGuiInputFlags_RouteMask_ = ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteGlobalLow | ImGuiInputFlags_RouteGlobalHigh, // _Always not part of this! + ImGuiInputFlags_RouteAlways = 1 << 12, // Do not register route, poll keys directly. + ImGuiInputFlags_RouteUnlessBgFocused= 1 << 13, // Global routes will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. + ImGuiInputFlags_RouteExtraMask_ = ImGuiInputFlags_RouteAlways | ImGuiInputFlags_RouteUnlessBgFocused, + + // [Internal] Mask of which function support which flags + ImGuiInputFlags_SupportedByIsKeyPressed = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_, + ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteExtraMask_, + ImGuiInputFlags_SupportedBySetKeyOwner = ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease, + ImGuiInputFlags_SupportedBySetItemKeyOwner = ImGuiInputFlags_SupportedBySetKeyOwner | ImGuiInputFlags_CondMask_, +}; + //----------------------------------------------------------------------------- // [SECTION] Clipper support //----------------------------------------------------------------------------- @@ -1209,7 +1414,7 @@ enum ImGuiActivateFlags_ ImGuiActivateFlags_None = 0, ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default if keyboard is available. ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default if keyboard is not available. - ImGuiActivateFlags_TryToPreserveState = 1 << 2 // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) + ImGuiActivateFlags_TryToPreserveState = 1 << 2, // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) }; // Early work-in-progress API for ScrollToItem() @@ -1224,7 +1429,7 @@ enum ImGuiScrollFlags_ ImGuiScrollFlags_AlwaysCenterY = 1 << 5, // Always center the result item on Y axis [default for Y axis for appearing window) ImGuiScrollFlags_NoScrollParent = 1 << 6, // Disable forwarding scrolling to parent window if required to keep item/rect visible (only scroll window the function was applied to). ImGuiScrollFlags_MaskX_ = ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleCenterX | ImGuiScrollFlags_AlwaysCenterX, - ImGuiScrollFlags_MaskY_ = ImGuiScrollFlags_KeepVisibleEdgeY | ImGuiScrollFlags_KeepVisibleCenterY | ImGuiScrollFlags_AlwaysCenterY + ImGuiScrollFlags_MaskY_ = ImGuiScrollFlags_KeepVisibleEdgeY | ImGuiScrollFlags_KeepVisibleCenterY | ImGuiScrollFlags_AlwaysCenterY, }; enum ImGuiNavHighlightFlags_ @@ -1233,16 +1438,7 @@ enum ImGuiNavHighlightFlags_ ImGuiNavHighlightFlags_TypeDefault = 1 << 0, ImGuiNavHighlightFlags_TypeThin = 1 << 1, ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. - ImGuiNavHighlightFlags_NoRounding = 1 << 3 -}; - -enum ImGuiNavDirSourceFlags_ -{ - ImGuiNavDirSourceFlags_None = 0, - ImGuiNavDirSourceFlags_RawKeyboard = 1 << 0, // Raw keyboard (not pulled from nav), faciliate use of some functions before we can unify nav and keys - ImGuiNavDirSourceFlags_Keyboard = 1 << 1, - ImGuiNavDirSourceFlags_PadDPad = 1 << 2, - ImGuiNavDirSourceFlags_PadLStick = 1 << 3 + ImGuiNavHighlightFlags_NoRounding = 1 << 3, }; enum ImGuiNavMoveFlags_ @@ -1260,13 +1456,13 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_FocusApi = 1 << 9, ImGuiNavMoveFlags_Tabbing = 1 << 10, // == Focus + Activate if item is Inputable + DontChangeNavHighlight ImGuiNavMoveFlags_Activate = 1 << 11, - ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 12 // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 12, // Do not alter the visible state of keyboard vs mouse nav highlight }; enum ImGuiNavLayer { ImGuiNavLayer_Main = 0, // Main scrolling layer - ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt/ImGuiNavInput_Menu) + ImGuiNavLayer_Menu = 1, // Menu layer (access with Alt) ImGuiNavLayer_COUNT }; @@ -1297,24 +1493,24 @@ enum ImGuiOldColumnFlags_ ImGuiOldColumnFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers ImGuiOldColumnFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns ImGuiOldColumnFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window - ImGuiOldColumnFlags_GrowParentContentsSize = 1 << 4 // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. + ImGuiOldColumnFlags_GrowParentContentsSize = 1 << 4, // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - , ImGuiColumnsFlags_None = ImGuiOldColumnFlags_None, + ImGuiColumnsFlags_None = ImGuiOldColumnFlags_None, ImGuiColumnsFlags_NoBorder = ImGuiOldColumnFlags_NoBorder, ImGuiColumnsFlags_NoResize = ImGuiOldColumnFlags_NoResize, ImGuiColumnsFlags_NoPreserveWidths = ImGuiOldColumnFlags_NoPreserveWidths, ImGuiColumnsFlags_NoForceWithinWindow = ImGuiOldColumnFlags_NoForceWithinWindow, - ImGuiColumnsFlags_GrowParentContentsSize = ImGuiOldColumnFlags_GrowParentContentsSize + ImGuiColumnsFlags_GrowParentContentsSize = ImGuiOldColumnFlags_GrowParentContentsSize, #endif }; struct ImGuiOldColumnData { - float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) + float OffsetNorm; // Column start offset, normalized 0.0 (far left) -> 1.0 (far right) float OffsetNormBeforeResize; - ImGuiOldColumnFlags Flags; // Not exposed + ImGuiOldColumnFlags Flags; // Not exposed ImRect ClipRect; ImGuiOldColumnData() { memset(this, 0, sizeof(*this)); } @@ -1423,12 +1619,51 @@ struct ImGuiSettingsHandler ImGuiSettingsHandler() { memset(this, 0, sizeof(*this)); } }; +//----------------------------------------------------------------------------- +// [SECTION] Localization support +//----------------------------------------------------------------------------- + +// This is experimental and not officially supported, it'll probably fall short of features, if/when it does we may backtrack. +enum ImGuiLocKey : int +{ + ImGuiLocKey_TableSizeOne, + ImGuiLocKey_TableSizeAllFit, + ImGuiLocKey_TableSizeAllDefault, + ImGuiLocKey_TableResetOrder, + ImGuiLocKey_WindowingMainMenuBar, + ImGuiLocKey_WindowingPopup, + ImGuiLocKey_WindowingUntitled, + ImGuiLocKey_COUNT +}; + +struct ImGuiLocEntry +{ + ImGuiLocKey Key; + const char* Text; +}; + + //----------------------------------------------------------------------------- // [SECTION] Metrics, Debug Tools //----------------------------------------------------------------------------- +enum ImGuiDebugLogFlags_ +{ + // Event types + ImGuiDebugLogFlags_None = 0, + ImGuiDebugLogFlags_EventActiveId = 1 << 0, + ImGuiDebugLogFlags_EventFocus = 1 << 1, + ImGuiDebugLogFlags_EventPopup = 1 << 2, + ImGuiDebugLogFlags_EventNav = 1 << 3, + ImGuiDebugLogFlags_EventClipper = 1 << 4, + ImGuiDebugLogFlags_EventIO = 1 << 5, + ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventIO, + ImGuiDebugLogFlags_OutputToTTY = 1 << 10, // Also send output to TTY +}; + struct ImGuiMetricsConfig { + bool ShowDebugLog; bool ShowStackTool; bool ShowWindowsRects; bool ShowWindowsBeginOrder; @@ -1440,14 +1675,10 @@ struct ImGuiMetricsConfig ImGuiMetricsConfig() { - ShowStackTool = false; - ShowWindowsRects = false; - ShowWindowsBeginOrder = false; - ShowTablesRects = false; + ShowDebugLog = ShowStackTool = ShowWindowsRects = ShowWindowsBeginOrder = ShowTablesRects = false; ShowDrawCmdMesh = true; ShowDrawCmdBoundingBoxes = true; - ShowWindowsRectsType = -1; - ShowTablesRectsType = -1; + ShowWindowsRectsType = ShowTablesRectsType = -1; } }; @@ -1456,7 +1687,8 @@ struct ImGuiStackLevelInfo ImGuiID ID; ImS8 QueryFrameCount; // >= 1: Query in progress bool QuerySuccess; // Obtained result from DebugHookIdInfo() - char Desc[58]; // Arbitrarily sized buffer to hold a result (FIXME: could replace Results[] with a chunk stream?) + ImGuiDataType DataType : 8; + char Desc[57]; // Arbitrarily sized buffer to hold a result (FIXME: could replace Results[] with a chunk stream?) FIXME: Now that we added CTRL+C this should be fixed. ImGuiStackLevelInfo() { memset(this, 0, sizeof(*this)); } }; @@ -1468,8 +1700,10 @@ struct ImGuiStackTool int StackLevel; // -1: query stack and resize Results, >= 0: individual stack level ImGuiID QueryId; // ID to query details for ImVector Results; + bool CopyToClipboardOnCtrlC; + float CopyToClipboardLastTime; - ImGuiStackTool() { memset(this, 0, sizeof(*this)); } + ImGuiStackTool() { memset(this, 0, sizeof(*this)); CopyToClipboardLastTime = -FLT_MAX; } }; //----------------------------------------------------------------------------- @@ -1491,7 +1725,7 @@ struct ImGuiContextHook }; //----------------------------------------------------------------------------- -// [SECTION] ImGuiContext (main imgui context) +// [SECTION] ImGuiContext (main Dear ImGui context) //----------------------------------------------------------------------------- struct ImGuiContext @@ -1499,6 +1733,8 @@ struct ImGuiContext bool Initialized; bool FontAtlasOwnedByContext; // IO.Fonts-> is owned by the ImGuiContext and will be destructed along with it. ImGuiIO IO; + ImVector InputEventsQueue; // Input events which will be tricked/written into IO structure. + ImVector InputEventsTrail; // Past input events processed in NewFrame(). This is to allow domain-specific application to access e.g mouse/pen trail. ImGuiStyle Style; ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. @@ -1529,15 +1765,16 @@ struct ImGuiContext ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actual window that is moved is generally MovingWindow->RootWindow. ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. ImVec2 WheelingWindowRefMousePos; - float WheelingWindowTimer; + int WheelingWindowStartFrame; // This may be set one frame before WheelingWindow is != NULL + float WheelingWindowReleaseTimer; + ImVec2 WheelingWindowWheelRemainder; + ImVec2 WheelingAxisAvg; // Item/widgets state and tracking information ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; bool HoveredIdAllowOverlap; - bool HoveredIdUsingMouseWheel; // Hovered widget will use mouse wheel. Blocks scrolling the underlying window. - bool HoveredIdPreviousFrameUsingMouseWheel; bool HoveredIdDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0. float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active @@ -1550,10 +1787,6 @@ struct ImGuiContext bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdHasBeenEditedThisFrame; - bool ActiveIdUsingMouseWheel; // Active widget will want to read mouse wheel. Blocks scrolling the underlying window. - ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) - ImU32 ActiveIdUsingNavInputMask; // Active widget will want to read those nav inputs. - ImU64 ActiveIdUsingKeyInputMask; // Active widget will want to read those key inputs. When we grow the ImGuiKey enum we'll need to either to order the enum to make useful keys come first, either redesign this into e.g. a small array. ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) @@ -1565,8 +1798,22 @@ struct ImGuiContext ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. + // [EXPERIMENTAL] Key/Input Ownership + Shortcut Routing system + // - The idea is that instead of "eating" a given key, we can link to an owner. + // - Input query can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_None (== -1) or a custom ID. + // - Routing is requested ahead of time for a given chord (Key + Mods) and granted in NewFrame(). + ImGuiKeyOwnerData KeysOwnerData[ImGuiKey_NamedKey_COUNT]; + ImGuiKeyRoutingTable KeysRoutingTable; + ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) + bool ActiveIdUsingAllKeyboardKeys; // Active widget will want to read all keyboard keys inputs. (FIXME: This is a shortcut for not taking ownership of 100+ keys but perhaps best to not have the inconsistency) +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + ImU32 ActiveIdUsingNavInputMask; // If you used this. Since (IMGUI_VERSION_NUM >= 18804) : 'g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel);' becomes 'SetKeyOwner(ImGuiKey_Escape, g.ActiveId) and/or SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId);' +#endif + // Next window/item data - ImGuiItemFlags CurrentItemFlags; // == g.ItemFlagsStack.back() + ImGuiID CurrentFocusScopeId; // == g.FocusScopeStack.back() + ImGuiItemFlags CurrentItemFlags; // == g.ItemFlagsStack.back() + ImGuiID DebugLocateId; // Storage for DebugLocateItemOnHover() feature: this is read by ItemAdd() so we keep it in a hot/cached location ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions ImGuiLastItemData LastItemData; // Storage for last submitted item (setup by ItemAdd) ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions @@ -1575,7 +1822,7 @@ struct ImGuiContext ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() - ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - not inherited by Begin(), unless child window + ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin() ImVectorItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() ImVectorGroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() ImVectorOpenPopupStack; // Which popups are open (persistent) @@ -1586,17 +1833,17 @@ struct ImGuiContext ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. // Gamepad/keyboard Navigation - ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusWindow' + ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' ImGuiID NavId; // Focused item for navigation ImGuiID NavFocusScopeId; // Identify a selection scope (selection code often wants to "clear other items" when landing on an item of the selection set) - ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0, also set when calling ActivateItem() - ImGuiID NavActivateDownId; // ~~ IsNavInputDown(ImGuiNavInput_Activate) ? NavId : 0 - ImGuiID NavActivatePressedId; // ~~ IsNavInputPressed(ImGuiNavInput_Activate) ? NavId : 0 - ImGuiID NavActivateInputId; // ~~ IsNavInputPressed(ImGuiNavInput_Input) ? NavId : 0; ImGuiActivateFlags_PreferInput will be set and NavActivateId will be 0. + ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() + ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0 + ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat) + ImGuiID NavActivateInputId; // ~~ IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadInput) ? NavId : 0; ImGuiActivateFlags_PreferInput will be set and NavActivateId will be 0. ImGuiActivateFlags NavActivateFlags; ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). - ImGuiKeyModFlags NavJustMovedToKeyMods; + ImGuiKeyChord NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. @@ -1617,7 +1864,7 @@ struct ImGuiContext bool NavMoveForwardToNextFrame; ImGuiNavMoveFlags NavMoveFlags; ImGuiScrollFlags NavMoveScrollFlags; - ImGuiKeyModFlags NavMoveKeyMods; + ImGuiKeyChord NavMoveKeyMods; ImGuiDir NavMoveDir; // Direction of the move request (left/right/up/down) ImGuiDir NavMoveDirForDebug; ImGuiDir NavMoveClipDir; // FIXME-NAV: Describe the purpose of this better. Might want to rename? @@ -1632,12 +1879,16 @@ struct ImGuiContext ImGuiNavItemData NavTabbingResultFirst; // First tabbing request candidate within NavWindow and flattened hierarchy // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) + ImGuiKeyChord ConfigNavWindowingKeyNext; // = ImGuiMod_Ctrl | ImGuiKey_Tab, for reconfiguration (see #4828) + ImGuiKeyChord ConfigNavWindowingKeyPrev; // = ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab ImGuiWindow* NavWindowingTarget; // Target window when doing CTRL+Tab (or Pad Menu + FocusPrev/Next), this window is temporarily displayed top-most! ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f, so the fade-out can stay on it. ImGuiWindow* NavWindowingListWindow; // Internal window actually listing the CTRL+Tab contents float NavWindowingTimer; float NavWindowingHighlightAlpha; bool NavWindowingToggleLayer; + ImVec2 NavWindowingAccumDeltaPos; + ImVec2 NavWindowingAccumDeltaSize; // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) @@ -1666,7 +1917,7 @@ struct ImGuiContext int ClipperTempDataStacked; ImVector ClipperTempData; - // Table + // Tables ImGuiTable* CurrentTable; int TablesTempDataStacked; // Temporary table data size (because we leave previous instances undestructed, we generally don't use TablesTempData.Size) ImVector TablesTempData; // Temporary table data (buffers reused/shared across instances, support nesting) @@ -1680,6 +1931,12 @@ struct ImGuiContext ImVector CurrentTabBarStack; ImVector ShrinkWidthBuffer; + // Hover Delay system + ImGuiID HoverDelayId; + ImGuiID HoverDelayIdPreviousFrame; + float HoverDelayTimer; // Currently used IsItemHovered(), generally inferred from g.HoveredIdTimer but kept uncleared until clear timer elapse. + float HoverDelayClearTimer; // Currently used IsItemHovered(): grace time before g.TooltipHoverTimer gets cleared. + // Widget state ImVec2 MouseLastValidPos; ImGuiInputTextState InputTextState; @@ -1691,6 +1948,7 @@ struct ImGuiContext ImU32 ColorEditLastColor; // RGB value with alpha set to 0. ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. ImGuiComboPreviewData ComboPreviewData; + float SliderGrabClickOffset; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? bool DragCurrentAccumDirty; @@ -1700,13 +1958,12 @@ struct ImGuiContext float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() short DisabledStackSize; short TooltipOverrideCount; - float TooltipSlowDelay; // Time before slow tooltips appears (FIXME: This is temporary until we merge in tooltip timer+priority work) ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once // Platform support - ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor - ImVec2 PlatformImeLastPos; + ImGuiPlatformImeData PlatformImeData; // Data updated by current frame + ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data (when changing we will call io.SetPlatformImeDataFn char PlatformLocaleDecimalPoint; // '.' or *localeconv()->decimal_point // Settings @@ -1719,6 +1976,9 @@ struct ImGuiContext ImVector Hooks; // Hooks for extensions (e.g. test engine) ImGuiID HookIdNext; // Next available HookId + // Localization + const char* LocalizationTable[ImGuiLocKey_COUNT]; + // Capture/Logging bool LogEnabled; // Currently capturing ImGuiLogType LogType; // Capture target @@ -1733,22 +1993,28 @@ struct ImGuiContext int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. // Debug Tools + ImGuiDebugLogFlags DebugLogFlags; + ImGuiTextBuffer DebugLogBuf; + ImGuiTextIndex DebugLogIndex; + ImU8 DebugLocateFrames; // For DebugLocateItemOnHover(). This is used together with DebugLocateId which is in a hot/cached spot above. bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) + ImU8 DebugItemPickerMouseButton; ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this ID ImGuiMetricsConfig DebugMetricsConfig; ImGuiStackTool DebugStackTool; // Misc - float FramerateSecPerFrame[120]; // Calculate estimate of framerate for user over the last 2 seconds. + float FramerateSecPerFrame[60]; // Calculate estimate of framerate for user over the last 60 frames.. int FramerateSecPerFrameIdx; int FramerateSecPerFrameCount; float FramerateSecPerFrameAccum; - int WantCaptureMouseNextFrame; // Explicit capture via CaptureKeyboardFromApp()/CaptureMouseFromApp() sets those flags - int WantCaptureKeyboardNextFrame; + int WantCaptureMouseNextFrame; // Explicit capture override via SetNextFrameWantCaptureMouse()/SetNextFrameWantCaptureKeyboard(). Default to -1. + int WantCaptureKeyboardNextFrame; // " int WantTextInputNextFrame; - char TempBuffer[1024 * 3 + 1]; // Temporary text buffer + ImVector TempBuffer; // Temporary text buffer ImGuiContext(ImFontAtlas* shared_font_atlas) + : InputTextState(this) { Initialized = false; FontAtlasOwnedByContext = shared_font_atlas ? false : true; @@ -1769,12 +2035,12 @@ struct ImGuiContext HoveredWindowUnderMovingWindow = NULL; MovingWindow = NULL; WheelingWindow = NULL; - WheelingWindowTimer = 0.0f; + WheelingWindowStartFrame = -1; + WheelingWindowReleaseTimer = 0.0f; DebugHookIdInfo = 0; HoveredId = HoveredIdPreviousFrame = 0; HoveredIdAllowOverlap = false; - HoveredIdUsingMouseWheel = HoveredIdPreviousFrameUsingMouseWheel = false; HoveredIdDisabled = false; HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; ActiveId = 0; @@ -1786,10 +2052,6 @@ struct ImGuiContext ActiveIdHasBeenPressedBefore = false; ActiveIdHasBeenEditedBefore = false; ActiveIdHasBeenEditedThisFrame = false; - ActiveIdUsingMouseWheel = false; - ActiveIdUsingNavDirMask = 0x00; - ActiveIdUsingNavInputMask = 0x00; - ActiveIdUsingKeyInputMask = 0x00; ActiveIdClickOffset = ImVec2(-1, -1); ActiveIdWindow = NULL; ActiveIdSource = ImGuiInputSource_None; @@ -1801,6 +2063,13 @@ struct ImGuiContext LastActiveId = 0; LastActiveIdTimer = 0.0f; + ActiveIdUsingNavDirMask = 0x00; + ActiveIdUsingAllKeyboardKeys = false; +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + ActiveIdUsingNavInputMask = 0x00; +#endif + + CurrentFocusScopeId = 0; CurrentItemFlags = ImGuiItemFlags_None; BeginMenuCount = 0; @@ -1808,7 +2077,7 @@ struct ImGuiContext NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavActivateInputId = 0; NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; - NavJustMovedToKeyMods = ImGuiKeyModFlags_None; + NavJustMovedToKeyMods = ImGuiMod_None; NavInputSource = ImGuiInputSource_None; NavLayer = ImGuiNavLayer_Main; NavIdIsAlive = false; @@ -1824,12 +2093,14 @@ struct ImGuiContext NavMoveForwardToNextFrame = false; NavMoveFlags = ImGuiNavMoveFlags_None; NavMoveScrollFlags = ImGuiScrollFlags_None; - NavMoveKeyMods = ImGuiKeyModFlags_None; + NavMoveKeyMods = ImGuiMod_None; NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None; NavScoringDebugCount = 0; NavTabbingDir = 0; NavTabbingCounter = 0; + ConfigNavWindowingKeyNext = ImGuiMod_Ctrl | ImGuiKey_Tab; + ConfigNavWindowingKeyPrev = ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab; NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; @@ -1855,28 +2126,34 @@ struct ImGuiContext TablesTempDataStacked = 0; CurrentTabBar = NULL; + HoverDelayId = HoverDelayIdPreviousFrame = 0; + HoverDelayTimer = HoverDelayClearTimer = 0.0f; + TempInputId = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditLastHue = ColorEditLastSat = 0.0f; ColorEditLastColor = 0; + SliderGrabClickOffset = 0.0f; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; + ScrollbarClickDeltaToGrabCenter = 0.0f; DisabledAlphaBackup = 0.0f; DisabledStackSize = 0; - ScrollbarClickDeltaToGrabCenter = 0.0f; TooltipOverrideCount = 0; - TooltipSlowDelay = 0.50f; - PlatformImePos = PlatformImeLastPos = ImVec2(FLT_MAX, FLT_MAX); + PlatformImeData.InputPos = ImVec2(0.0f, 0.0f); + PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission PlatformLocaleDecimalPoint = '.'; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; HookIdNext = 0; + memset(LocalizationTable, 0, sizeof(LocalizationTable)); + LogEnabled = false; LogType = ImGuiLogType_None; LogNextPrefix = LogNextSuffix = NULL; @@ -1886,14 +2163,17 @@ struct ImGuiContext LogDepthRef = 0; LogDepthToExpand = LogDepthToExpandDefault = 2; + DebugLogFlags = ImGuiDebugLogFlags_OutputToTTY; + DebugLocateId = 0; + DebugLocateFrames = 0; DebugItemPickerActive = false; + DebugItemPickerMouseButton = ImGuiMouseButton_Left; DebugItemPickerBreakId = 0; memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; FramerateSecPerFrameAccum = 0.0f; WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; - memset(TempBuffer, 0, sizeof(TempBuffer)); } }; @@ -1916,6 +2196,8 @@ struct IMGUI_API ImGuiWindowTempData ImVec2 PrevLineSize; float CurrLineTextBaseOffset; // Baseline offset (0.0f by default on a new line, generally == style.FramePadding.y when a framed item has been added). float PrevLineTextBaseOffset; + bool IsSameLine; + bool IsSetPos; ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. ImVec1 GroupOffset; @@ -1925,7 +2207,6 @@ struct IMGUI_API ImGuiWindowTempData ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) short NavLayersActiveMask; // Which layers have been written to (result from previous frame) short NavLayersActiveMaskNext;// Which layers have been written to (accumulator for current frame) - ImGuiID NavFocusScopeIdCurrent; // Current focus scope ID while appending bool NavHideHighlightOneFrame; bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) @@ -1956,6 +2237,7 @@ struct IMGUI_API ImGuiWindow char* Name; // Window name, owned by the window. ImGuiID ID; // == ImHashStr(Name) ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_ + ImGuiViewportP* Viewport; // Always set in Begin(). Inactive windows may have a NULL value here if their viewport was discarded. ImVec2 Pos; // Position (always rounded-up to nearest pixel) ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) ImVec2 SizeFull; // Size when non collapsed @@ -1965,6 +2247,9 @@ struct IMGUI_API ImGuiWindow ImVec2 WindowPadding; // Window padding at the time of Begin(). float WindowRounding; // Window rounding at the time of Begin(). May be clamped lower to avoid rendering artifacts with title bar, menu bar etc. float WindowBorderSize; // Window border size at the time of Begin(). + float DecoOuterSizeX1, DecoOuterSizeY1; // Left/Up offsets. Sum of non-scrolling outer decorations (X1 generally == 0.0f. Y1 generally = TitleBarHeight + MenuBarHeight). Locked during Begin(). + float DecoOuterSizeX2, DecoOuterSizeY2; // Right/Down offsets (X2 generally == ScrollbarSize.x, Y2 == ScrollbarSizes.y). + float DecoInnerSizeX1, DecoInnerSizeY1; // Applied AFTER/OVER InnerRect. Specialized for Tables as they use specialized form of clipping and frozen rows/columns are inside InnerRect (and not part of regular decoration sizes). int NameBufLen; // Size of buffer storing Name. May be larger than strlen(Name)! ImGuiID MoveId; // == window->GetID("#MOVE") ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window) @@ -1988,6 +2273,7 @@ struct IMGUI_API ImGuiWindow bool HasCloseButton; // Set when the window has a close button (p_open != NULL) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) + short BeginCountPreviousFrame; // Number of Begin() during the previous frame short BeginOrderWithinParent; // Begin() order within immediate parent window, if we are a child window. Otherwise 0. short BeginOrderWithinContext; // Begin() order within entire imgui context. This is mostly used for debugging submission order related issues. short FocusOrder; // Order within WindowsFocusOrder[], altered when windows are focused. @@ -2041,6 +2327,7 @@ struct IMGUI_API ImGuiWindow ImGuiWindow* NavLastChildNavWindow; // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.) ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space + ImGuiID NavRootFocusScopeId; // Focus Scope ID at the time of Begin() int MemoryDrawListIdxCapacity; // Backup of last idx/vtx count, so when waking up the window we can preallocate and avoid iterative alloc/copy int MemoryDrawListVtxCapacity; @@ -2053,12 +2340,9 @@ struct IMGUI_API ImGuiWindow ImGuiID GetID(const char* str, const char* str_end = NULL); ImGuiID GetID(const void* ptr); ImGuiID GetID(int n); - ImGuiID GetIDNoKeepAlive(const char* str, const char* str_end = NULL); - ImGuiID GetIDNoKeepAlive(const void* ptr); - ImGuiID GetIDNoKeepAlive(int n); ImGuiID GetIDFromRectangle(const ImRect& r_abs); - // We don't use g.FontSize because the window may be != g.CurrentWidow. + // We don't use g.FontSize because the window may be != g.CurrentWindow. ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } @@ -2076,7 +2360,7 @@ enum ImGuiTabBarFlagsPrivate_ { ImGuiTabBarFlags_DockNode = 1 << 20, // Part of a dock node [we don't use this in the master branch but it facilitate branch syncing to keep this around] ImGuiTabBarFlags_IsFocused = 1 << 21, - ImGuiTabBarFlags_SaveSettings = 1 << 22 // FIXME: Settings are handled by the docking system, this only request the tab bar to mark settings dirty when reordering tabs + ImGuiTabBarFlags_SaveSettings = 1 << 22, // FIXME: Settings are handled by the docking system, this only request the tab bar to mark settings dirty when reordering tabs }; // Extend ImGuiTabItemFlags_ @@ -2084,7 +2368,7 @@ enum ImGuiTabItemFlagsPrivate_ { ImGuiTabItemFlags_SectionMask_ = ImGuiTabItemFlags_Leading | ImGuiTabItemFlags_Trailing, ImGuiTabItemFlags_NoCloseButton = 1 << 20, // Track whether p_open was set or not (we'll need this info on the next frame to recompute ContentWidth during layout) - ImGuiTabItemFlags_Button = 1 << 21 // Used by TabItemButton, change the tab item behavior to mimic a button + ImGuiTabItemFlags_Button = 1 << 21, // Used by TabItemButton, change the tab item behavior to mimic a button }; // Storage for one active tab item (sizeof() 40 bytes) @@ -2097,12 +2381,13 @@ struct ImGuiTabItem float Offset; // Position relative to beginning of tab float Width; // Width currently displayed float ContentWidth; // Width of label, stored during BeginTabItem() call + float RequestedWidth; // Width optionally requested by caller, -1.0f is unused ImS32 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames ImS16 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable ImS16 IndexDuringLayout; // Index only used during TabBarLayout() bool WantClose; // Marked as closed by SetTabItemClosed() - ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; NameOffset = -1; BeginOrder = IndexDuringLayout = -1; } + ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; RequestedWidth = -1.0f; NameOffset = -1; BeginOrder = IndexDuringLayout = -1; } }; // Storage for a tab bar (sizeof() 152 bytes) @@ -2230,7 +2515,17 @@ struct ImGuiTableCellData ImGuiTableColumnIdx Column; // Column number }; -// FIXME-TABLE: more transient data could be stored in a per-stacked table structure: DrawSplitter, SortSpecs, incoming RowData +// Per-instance data that needs preserving across frames (seemingly most others do not need to be preserved aside from debug needs, does that needs they could be moved to ImGuiTableTempData ?) +struct ImGuiTableInstanceData +{ + float LastOuterHeight; // Outer height from last frame + float LastFirstRowHeight; // Height of first row from last frame (FIXME: this is used as "header height" and may be reworked) + float LastFrozenHeight; // Height of frozen section from last frame + + ImGuiTableInstanceData() { LastOuterHeight = LastFirstRowHeight = LastFrozenHeight = 0.0f; } +}; + +// FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs, incoming RowData struct IMGUI_API ImGuiTable { ImGuiID ID; @@ -2272,11 +2567,10 @@ struct IMGUI_API ImGuiTable float CellPaddingY; float CellSpacingX1; // Spacing between non-bordered cells float CellSpacingX2; - float LastOuterHeight; // Outer height from last frame - float LastFirstRowHeight; // Height of first row from last frame float InnerWidth; // User value passed to BeginTable(), see comments at the top of BeginTable() for details. float ColumnsGivenWidth; // Sum of current column width float ColumnsAutoFitWidth; // Sum of ideal column width in order nothing to be clipped, used for auto-fitting and content width submission in outer window + float ColumnsStretchSumWeights; // Sum of weight of all enabled stretching columns float ResizedColumnNextWidth; float ResizeLockMinContentsX2; // Lock minimum contents width while resizing down in order to not create feedback loops. But we allow growing the table. float RefScale; // Reference scale to be able to rescale columns on font/dpi changes. @@ -2284,7 +2578,7 @@ struct IMGUI_API ImGuiTable ImRect InnerRect; // InnerRect but without decoration. As with OuterRect, for non-scrolling tables, InnerRect.Max.y is ImRect WorkRect; ImRect InnerClipRect; - ImRect BgClipRect; // We use this to cpu-clip cell background color fill + ImRect BgClipRect; // We use this to cpu-clip cell background color fill, evolve during the frame as we cross frozen rows boundaries ImRect Bg0ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG0/1 channel. This tends to be == OuterWindow->ClipRect at BeginTable() because output in BG0/BG1 is cpu-clipped ImRect Bg2ClipRectForDrawCmd; // Actual ImDrawCmd clip rect for BG2 channel. This tends to be a correct, tight-fit, because output to BG2 are done by widgets relying on regular ClipRect. ImRect HostClipRect; // This is used to check if we can eventually merge our columns draw calls into the current draw call of the current window. @@ -2293,6 +2587,8 @@ struct IMGUI_API ImGuiTable ImGuiWindow* InnerWindow; // Window holding the table data (== OuterWindow or a child window) ImGuiTextBuffer ColumnsNames; // Contiguous buffer holding columns names ImDrawListSplitter* DrawSplitter; // Shortcut to TempData->DrawSplitter while in table. Isolate draw commands per columns to avoid switching clip rect constantly + ImGuiTableInstanceData InstanceDataFirst; + ImVector InstanceDataExtra; // FIXME-OPT: Using a small-vector pattern would be good. ImGuiTableColumnSortSpecs SortSpecsSingle; ImVector SortSpecsMulti; // FIXME-OPT: Using a small-vector pattern would be good. ImGuiTableSortSpecs SortSpecs; // Public facing sorts specs, this is what we return in TableGetSortSpecs() @@ -2334,6 +2630,8 @@ struct IMGUI_API ImGuiTable bool IsResetDisplayOrderRequest; bool IsUnfrozenRows; // Set when we got past the frozen row. bool IsDefaultSizingPolicy; // Set if user didn't explicitly set a sizing policy in BeginTable() + bool HasScrollbarYCurr; // Whether ANY instance of this table had a vertical scrollbar during the current frame. + bool HasScrollbarYPrev; // Whether ANY instance of this table had a vertical scrollbar during the previous. bool MemoryCompacted; bool HostSkipItems; // Backup of InnerWindow->SkipItem at the end of BeginTable(), because we will overwrite InnerWindow->SkipItem on a per-column basis @@ -2449,10 +2747,11 @@ namespace ImGui IMGUI_API ImDrawList* GetForegroundDrawList(ImGuiViewport* viewport); // get foreground draw list for the given viewport. this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. // Init - IMGUI_API void Initialize(ImGuiContext* context); - IMGUI_API void Shutdown(ImGuiContext* context); // Since 1.60 this is a _private_ function. You can call DestroyContext() to destroy the context created by CreateContext(). + IMGUI_API void Initialize(); + IMGUI_API void Shutdown(); // Since 1.60 this is a _private_ function. You can call DestroyContext() to destroy the context created by CreateContext(). // NewFrame + IMGUI_API void UpdateInputEvents(bool trickle_fast_inputs); IMGUI_API void UpdateHoveredWindowAndCaptureFlags(); IMGUI_API void StartMouseMovingWindow(ImGuiWindow* window); IMGUI_API void UpdateMouseMovingWindowNewFrame(); @@ -2463,6 +2762,9 @@ namespace ImGui IMGUI_API void RemoveContextHook(ImGuiContext* context, ImGuiID hook_to_remove); IMGUI_API void CallContextHooks(ImGuiContext* context, ImGuiContextHookType type); + // Viewports + IMGUI_API void SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport); + // Settings IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); @@ -2470,10 +2772,15 @@ namespace ImGui IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name); + IMGUI_API void AddSettingsHandler(const ImGuiSettingsHandler* handler); + IMGUI_API void RemoveSettingsHandler(const char* type_name); IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); + // Localization + IMGUI_API void LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count); + inline const char* LocalizeGetMsg(ImGuiLocKey key) { ImGuiContext& g = *GImGui; const char* msg = g.LocalizationTable[key]; return msg ? msg : "*Missing Text*"; } + // Scrolling - IMGUI_API void SetNextWindowScroll(const ImVec2& scroll); // Use -1.0f on one axis to leave as-is IMGUI_API void SetScrollX(ImGuiWindow* window, float scroll_x); IMGUI_API void SetScrollY(ImGuiWindow* window, float scroll_y); IMGUI_API void SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio); @@ -2488,7 +2795,6 @@ namespace ImGui //#endif // Basic Accessors - inline ImGuiID GetItemID() { ImGuiContext& g = *GImGui; return g.LastItemData.ID; } // Get ID of last item (~~ often same ImGui::GetID(label) beforehand) inline ImGuiItemStatusFlags GetItemStatusFlags(){ ImGuiContext& g = *GImGui; return g.LastItemData.StatusFlags; } inline ImGuiItemFlags GetItemFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.InFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } @@ -2505,7 +2811,7 @@ namespace ImGui // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); - IMGUI_API void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f); + inline void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f) { ItemSize(bb.GetSize(), text_baseline_y); } // FIXME: This is a misleading API since we expect CursorPos to be bb.Min. IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemFlags extra_flags = 0); IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id); @@ -2517,21 +2823,10 @@ namespace ImGui IMGUI_API ImVec2 GetContentRegionMaxAbs(); IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess); - // Parameter stacks + // Parameter stacks (shared) IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PopItemFlag(); -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - // Currently refactoring focus/nav/tabbing system - // If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): - // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' - // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' - // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || g.NavActivateInputId == id' (WIP) - // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() - inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() - inline void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem -#endif - // Logging/Capture IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. IMGUI_API void LogToBuffer(int auto_open_depth = -1); // Start logging/capturing to internal buffer @@ -2573,37 +2868,105 @@ namespace ImGui IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); - IMGUI_API float GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode); - IMGUI_API ImVec2 GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor = 0.0f, float fast_factor = 0.0f); - IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. + IMGUI_API void SetNavWindow(ImGuiWindow* window); IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); - // Focus Scope (WIP) - // This is generally used to identify a selection set (multiple of which may be in the same window), as selection - // patterns generally need to react (e.g. clear selection) when landing on an item of the set. - IMGUI_API void PushFocusScope(ImGuiID id); - IMGUI_API void PopFocusScope(); - inline ImGuiID GetFocusedFocusScope() { ImGuiContext& g = *GImGui; return g.NavFocusScopeId; } // Focus scope which is actually active - inline ImGuiID GetFocusScope() { ImGuiContext& g = *GImGui; return g.CurrentWindow->DC.NavFocusScopeIdCurrent; } // Focus scope we are outputting into, set by PushFocusScope() - // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. - IMGUI_API void SetItemUsingMouseWheel(); - IMGUI_API void SetActiveIdUsingNavAndKeys(); - inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } - inline bool IsActiveIdUsingNavInput(ImGuiNavInput input) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavInputMask & (1 << input)) != 0; } - inline bool IsActiveIdUsingKey(ImGuiKey key) { ImGuiContext& g = *GImGui; IM_ASSERT(key < 64); return (g.ActiveIdUsingKeyInputMask & ((ImU64)1 << key)) != 0; } + inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; } + inline bool IsNamedKeyOrModKey(ImGuiKey key) { return (key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END) || key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super || key == ImGuiMod_Shortcut; } + inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; } + inline bool IsKeyboardKey(ImGuiKey key) { return key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END; } + inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } + inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } + inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } + inline ImGuiKeyChord ConvertShortcutMod(ImGuiKeyChord key_chord) { ImGuiContext& g = *GImGui; IM_ASSERT_PARANOID(key_chord & ImGuiMod_Shortcut); return (key_chord & ~ImGuiMod_Shortcut) | (g.IO.ConfigMacOSXBehaviors ? ImGuiMod_Super : ImGuiMod_Ctrl); } + inline ImGuiKey ConvertSingleModFlagToKey(ImGuiKey key) + { + ImGuiContext& g = *GImGui; + if (key == ImGuiMod_Ctrl) return ImGuiKey_ReservedForModCtrl; + if (key == ImGuiMod_Shift) return ImGuiKey_ReservedForModShift; + if (key == ImGuiMod_Alt) return ImGuiKey_ReservedForModAlt; + if (key == ImGuiMod_Super) return ImGuiKey_ReservedForModSuper; + if (key == ImGuiMod_Shortcut) return (g.IO.ConfigMacOSXBehaviors ? ImGuiKey_ReservedForModSuper : ImGuiKey_ReservedForModCtrl); + return key; + } + + IMGUI_API ImGuiKeyData* GetKeyData(ImGuiKey key); + IMGUI_API void GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size); + inline ImGuiKey MouseButtonToKey(ImGuiMouseButton button) { IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); return (ImGuiKey)(ImGuiKey_MouseLeft + button); } IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f); - inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { ImGuiContext& g = *GImGui; const int key_index = g.IO.KeyMap[key]; return (key_index >= 0) ? IsKeyPressed(key_index, repeat) : false; } - inline bool IsNavInputDown(ImGuiNavInput n) { ImGuiContext& g = *GImGui; return g.IO.NavInputs[n] > 0.0f; } - inline bool IsNavInputTest(ImGuiNavInput n, ImGuiInputReadMode rm) { return (GetNavInputAmount(n, rm) > 0.0f); } - IMGUI_API ImGuiKeyModFlags GetMergedKeyModFlags(); + IMGUI_API ImVec2 GetKeyMagnitude2d(ImGuiKey key_left, ImGuiKey key_right, ImGuiKey key_up, ImGuiKey key_down); + IMGUI_API float GetNavTweakPressedAmount(ImGuiAxis axis); + IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); + IMGUI_API void GetTypematicRepeatRate(ImGuiInputFlags flags, float* repeat_delay, float* repeat_rate); + IMGUI_API void SetActiveIdUsingAllKeyboardKeys(); + inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } + + // [EXPERIMENTAL] Low-Level: Key/Input Ownership + // - The idea is that instead of "eating" a given input, we can link to an owner id. + // - Ownership is most often claimed as a result of reacting to a press/down event (but occasionally may be claimed ahead). + // - Input queries can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_None (== -1) or a custom ID. + // - Legacy input queries (without specifying an owner or _Any or _None) are equivalent to using ImGuiKeyOwner_Any (== 0). + // - Input ownership is automatically released on the frame after a key is released. Therefore: + // - for ownership registration happening as a result of a down/press event, the SetKeyOwner() call may be done once (common case). + // - for ownership registration happening ahead of a down/press event, the SetKeyOwner() call needs to be made every frame (happens if e.g. claiming ownership on hover). + // - SetItemKeyOwner() is a shortcut for common simple case. A custom widget will probably want to call SetKeyOwner() multiple times directly based on its interaction state. + // - This is marked experimental because not all widgets are fully honoring the Set/Test idioms. We will need to move forward step by step. + // Please open a GitHub Issue to submit your usage scenario or if there's a use case you need solved. + IMGUI_API ImGuiID GetKeyOwner(ImGuiKey key); + IMGUI_API void SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API void SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags = 0); // Set key owner to last item if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. + IMGUI_API bool TestKeyOwner(ImGuiKey key, ImGuiID owner_id); // Test that key is either not owned, either owned by 'owner_id' + inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(key); IM_ASSERT(IsNamedKey(key)); return &GImGui->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; } + + // [EXPERIMENTAL] High-Level: Input Access functions w/ support for Key/Input Ownership + // - Important: legacy IsKeyPressed(ImGuiKey, bool repeat=true) _DEFAULTS_ to repeat, new IsKeyPressed() requires _EXPLICIT_ ImGuiInputFlags_Repeat flag. + // - Expected to be later promoted to public API, the prototypes are designed to replace existing ones (since owner_id can default to Any == 0) + // - Specifying a value for 'ImGuiID owner' will test that EITHER the key is NOT owned (UNLESS locked), EITHER the key is owned by 'owner'. + // Legacy functions use ImGuiKeyOwner_Any meaning that they typically ignore ownership, unless a call to SetKeyOwner() explicitly used ImGuiInputFlags_LockThisFrame or ImGuiInputFlags_LockUntilRelease. + // - Binding generators may want to ignore those for now, or suffix them with Ex() until we decide if this gets moved into public API. + IMGUI_API bool IsKeyDown(ImGuiKey key, ImGuiID owner_id); + IMGUI_API bool IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); // Important: when transitioning from old to new IsKeyPressed(): old API has "bool repeat = true", so would default to repeat. New API requiress explicit ImGuiInputFlags_Repeat. + IMGUI_API bool IsKeyReleased(ImGuiKey key, ImGuiID owner_id); + IMGUI_API bool IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id); + IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API bool IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id); + + // [EXPERIMENTAL] Shortcut Routing + // - ImGuiKeyChord = a ImGuiKey optionally OR-red with ImGuiMod_Alt/ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Super. + // ImGuiKey_C (accepted by functions taking ImGuiKey or ImGuiKeyChord) + // ImGuiKey_C | ImGuiMod_Ctrl (accepted by functions taking ImGuiKeyChord) + // ONLY ImGuiMod_XXX values are legal to 'OR' with an ImGuiKey. You CANNOT 'OR' two ImGuiKey values. + // - When using one of the routing flags (e.g. ImGuiInputFlags_RouteFocused): routes requested ahead of time given a chord (key + modifiers) and a routing policy. + // - Routes are resolved during NewFrame(): if keyboard modifiers are matching current ones: SetKeyOwner() is called + route is granted for the frame. + // - Route is granted to a single owner. When multiple requests are made we have policies to select the winning route. + // - Multiple read sites may use the same owner id and will all get the granted route. + // - For routing: when owner_id is 0 we use the current Focus Scope ID as a default owner in order to identify our location. + IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); + IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); + IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id); + IMGUI_API ImGuiKeyRoutingData* GetShortcutRoutingData(ImGuiKeyChord key_chord); + + // [EXPERIMENTAL] Focus Scope + // This is generally used to identify a unique input location (for e.g. a selection set) + // There is one per window (automatically set in Begin), but: + // - Selection patterns generally need to react (e.g. clear a selection) when landing on one item of the set. + // So in order to identify a set multiple lists in same window may each need a focus scope. + // If you imagine an hypothetical BeginSelectionGroup()/EndSelectionGroup() api, it would likely call PushFocusScope()/EndFocusScope() + // - Shortcut routing also use focus scope as a default location identifier if an owner is not provided. + // We don't use the ID Stack for this as it is common to want them separate. + IMGUI_API void PushFocusScope(ImGuiID id); + IMGUI_API void PopFocusScope(); + inline ImGuiID GetCurrentFocusScope() { ImGuiContext& g = *GImGui; return g.CurrentFocusScopeId; } // Focus scope we are outputting into, set by PushFocusScope() // Drag and Drop + IMGUI_API bool IsDragDropActive(); IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); IMGUI_API void ClearDragDrop(); IMGUI_API bool IsDragDropPayloadBeingAccepted(); + IMGUI_API void RenderDragDropTargetRect(const ImRect& bb); // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) IMGUI_API void SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect); @@ -2638,7 +3001,9 @@ namespace ImGui IMGUI_API void TableUpdateColumnsWeightFromWidth(ImGuiTable* table); IMGUI_API void TableDrawBorders(ImGuiTable* table); IMGUI_API void TableDrawContextMenu(ImGuiTable* table); + IMGUI_API bool TableBeginContextMenuPopup(ImGuiTable* table); IMGUI_API void TableMergeDrawChannels(ImGuiTable* table); + inline ImGuiTableInstanceData* TableGetInstanceData(ImGuiTable* table, int instance_no) { if (instance_no == 0) return &table->InstanceDataFirst; return &table->InstanceDataExtra[instance_no - 1]; } IMGUI_API void TableSortSpecsSanitize(ImGuiTable* table); IMGUI_API void TableSortSpecsBuild(ImGuiTable* table); IMGUI_API ImGuiSortDirection TableGetColumnNextSortDirection(ImGuiTableColumn* column); @@ -2664,7 +3029,7 @@ namespace ImGui IMGUI_API void TableSaveSettings(ImGuiTable* table); IMGUI_API void TableResetSettings(ImGuiTable* table); IMGUI_API ImGuiTableSettings* TableGetBoundSettings(ImGuiTable* table); - IMGUI_API void TableSettingsInstallHandler(ImGuiContext* context); + IMGUI_API void TableSettingsAddSettingsHandler(); IMGUI_API ImGuiTableSettings* TableSettingsCreate(ImGuiID id, int columns_count); IMGUI_API ImGuiTableSettings* TableSettingsFindByID(ImGuiID id); @@ -2676,8 +3041,9 @@ namespace ImGui IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int offset); IMGUI_API void TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, ImVec2 mouse_pos); IMGUI_API bool TabBarProcessReorder(ImGuiTabBar* tab_bar); - IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags); - IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button); + IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window); + IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button_or_unsaved_marker); + IMGUI_API ImVec2 TabItemCalcSize(ImGuiWindow* window); IMGUI_API void TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col); IMGUI_API void TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImVec2 frame_padding, const char* label, ImGuiID tab_id, ImGuiID close_button_id, bool is_contents_visible, bool* out_just_closed, bool* out_text_clipped); @@ -2694,47 +3060,44 @@ namespace ImGui IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, ImDrawFlags flags = 0); IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. + IMGUI_API void RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); // Render helpers (those functions don't access any ImGui state!) IMGUI_API void RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir dir, float scale = 1.0f); IMGUI_API void RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col); IMGUI_API void RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float sz); - IMGUI_API void RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); IMGUI_API void RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col); IMGUI_API void RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, ImU32 col, float x_start_norm, float x_end_norm, float rounding); - IMGUI_API void RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding); - -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - // [1.71: 2019/06/07: Updating prototypes of some of the internal functions. Leaving those for reference for a short while] - inline void RenderArrow(ImVec2 pos, ImGuiDir dir, float scale=1.0f) { ImGuiWindow* window = GetCurrentWindow(); RenderArrow(window->DrawList, pos, GetColorU32(ImGuiCol_Text), dir, scale); } - inline void RenderBullet(ImVec2 pos) { ImGuiWindow* window = GetCurrentWindow(); RenderBullet(window->DrawList, pos, GetColorU32(ImGuiCol_Text)); } -#endif + IMGUI_API void RenderRectFilledWithHole(ImDrawList* draw_list, const ImRect& outer, const ImRect& inner, ImU32 col, float rounding); // Widgets IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); + IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); + IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col); + IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); + IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); + IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); + + // Widgets: Window Decorations IMGUI_API bool CloseButton(ImGuiID id, const ImVec2& pos); IMGUI_API bool CollapseButton(ImGuiID id, const ImVec2& pos); - IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API void Scrollbar(ImGuiAxis axis); IMGUI_API bool ScrollbarEx(const ImRect& bb, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 avail_v, ImS64 contents_v, ImDrawFlags flags); - IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col); IMGUI_API ImRect GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis); IMGUI_API ImGuiID GetWindowResizeCornerID(ImGuiWindow* window, int n); // 0..3: corners IMGUI_API ImGuiID GetWindowResizeBorderID(ImGuiWindow* window, ImGuiDir dir); - IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); - IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); - IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); // Widgets low-level behaviors IMGUI_API bool ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags = 0); IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags); IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); - IMGUI_API bool SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f); + IMGUI_API bool SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f, ImU32 bg_col = 0); IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL); - IMGUI_API bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0); // Consume previous SetNextItemOpen() data, if any. May return true when logging IMGUI_API void TreePushOverrideID(ImGuiID id); + IMGUI_API void TreeNodeSetOpen(ImGuiID id, bool open); + IMGUI_API bool TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags); // Return open state. Consume previous SetNextItemOpen() data, if any. May return true when logging. // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. // To use them externally (for custom widget) you may need an "extern template" statement in your code in order to link to existing instances and silence Clang warnings (see #2036). @@ -2743,14 +3106,14 @@ namespace ImGui template IMGUI_API T ScaleValueFromRatioT(ImGuiDataType data_type, float t, T v_min, T v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_size); template IMGUI_API bool DragBehaviorT(ImGuiDataType data_type, T* v, float v_speed, T v_min, T v_max, const char* format, ImGuiSliderFlags flags); template IMGUI_API bool SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, T* v, T v_min, T v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); - template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); + template IMGUI_API T RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, T v); template IMGUI_API bool CheckboxFlagsT(const char* label, T* flags, T flags_value); // Data type helpers IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format); IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const void* arg_1, const void* arg_2); - IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format); + IMGUI_API bool DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format); IMGUI_API int DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2); IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max); @@ -2759,7 +3122,7 @@ namespace ImGui IMGUI_API bool TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags); IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min = NULL, const void* p_clamp_max = NULL); inline bool TempInputIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputId == id); } - inline ImGuiInputTextState* GetInputTextState(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.InputTextState.ID == id) ? &g.InputTextState : NULL; } // Get input text state if active + inline ImGuiInputTextState* GetInputTextState(ImGuiID id) { ImGuiContext& g = *GImGui; return (id != 0 && g.InputTextState.ID == id) ? &g.InputTextState : NULL; } // Get input text state if active // Color IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags); @@ -2778,29 +3141,56 @@ namespace ImGui IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); + // Debug Log + IMGUI_API void DebugLog(const char* fmt, ...) IM_FMTARGS(1); + IMGUI_API void DebugLogV(const char* fmt, va_list args) IM_FMTLIST(1); + // Debug Tools IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); IMGUI_API void ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); + IMGUI_API void ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + IMGUI_API void DebugLocateItem(ImGuiID target_id); // Call sparingly: only 1 at the same time! + IMGUI_API void DebugLocateItemOnHover(ImGuiID target_id); // Only call on reaction to a mouse Hover: because only 1 at the same time! + IMGUI_API void DebugLocateItemResolveWithLastItem(); inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, col); } inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } - IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); IMGUI_API void DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end); IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); IMGUI_API void DebugNodeFont(ImFont* font); + IMGUI_API void DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph); IMGUI_API void DebugNodeStorage(ImGuiStorage* storage, const char* label); IMGUI_API void DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label); IMGUI_API void DebugNodeTable(ImGuiTable* table); IMGUI_API void DebugNodeTableSettings(ImGuiTableSettings* settings); + IMGUI_API void DebugNodeInputTextState(ImGuiInputTextState* state); IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); IMGUI_API void DebugNodeWindowsListByBeginStackParent(ImGuiWindow** windows, int windows_size, ImGuiWindow* parent_in_begin_stack); IMGUI_API void DebugNodeViewport(ImGuiViewportP* viewport); + IMGUI_API void DebugRenderKeyboardPreview(ImDrawList* draw_list); IMGUI_API void DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb); + // Obsolete functions +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + inline void SetItemUsingMouseWheel() { SetItemKeyOwner(ImGuiKey_MouseWheelY); } // Changed in 1.89 + inline bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0) { return TreeNodeUpdateNextOpen(id, flags); } // Renamed in 1.89 + + // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): + // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' + // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' + // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || g.NavActivateInputId == id' (WIP) + // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() + inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() + inline void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem +#endif +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { IM_ASSERT(IsNamedKey(key)); return IsKeyPressed(key, repeat); } // Removed in 1.87: Mapping from named key is always identity! +#endif + } // namespace ImGui @@ -2815,7 +3205,9 @@ struct ImFontBuilderIO }; // Helper for font builder +#ifdef IMGUI_ENABLE_STB_TRUETYPE IMGUI_API const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype(); +#endif IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); @@ -2839,7 +3231,8 @@ extern const char* ImGuiTestEngine_FindItemDebugLabel(ImGuiContext* ctx, ImGuiI #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) #define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log #else -#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)0) +#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) ((void)0) +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)g) #endif //----------------------------------------------------------------------------- diff --git a/include/imgui/imstb_rectpack.h b/include/imgui/imstb_rectpack.h index 39589521..f6917e7a 100644 --- a/include/imgui/imstb_rectpack.h +++ b/include/imgui/imstb_rectpack.h @@ -1,15 +1,19 @@ // [DEAR IMGUI] -// This is a slightly modified version of stb_rect_pack.h 1.00. -// Those changes would need to be pushed into nothings/stb: -// - Added STBRP__CDECL +// This is a slightly modified version of stb_rect_pack.h 1.01. // Grep for [DEAR IMGUI] to find the changes. - -// stb_rect_pack.h - v1.00 - public domain - rectangle packing +// +// stb_rect_pack.h - v1.01 - public domain - rectangle packing // Sean Barrett 2014 // // Useful for e.g. packing rectangular textures into an atlas. // Does not do rotation. // +// Before #including, +// +// #define STB_RECT_PACK_IMPLEMENTATION +// +// in the file that you want to have the implementation. +// // Not necessarily the awesomest packing method, but better than // the totally naive one in stb_truetype (which is primarily what // this is meant to replace). @@ -41,6 +45,7 @@ // // Version history: // +// 1.01 (2021-07-11) always use large rect mode, expose STBRP__MAXVAL in public section // 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles // 0.99 (2019-02-07) warning fixes // 0.11 (2017-03-03) return packing success/fail result @@ -81,11 +86,10 @@ typedef struct stbrp_context stbrp_context; typedef struct stbrp_node stbrp_node; typedef struct stbrp_rect stbrp_rect; -#ifdef STBRP_LARGE_RECTS typedef int stbrp_coord; -#else -typedef unsigned short stbrp_coord; -#endif + +#define STBRP__MAXVAL 0x7fffffff +// Mostly for internal use, but this is the maximum supported coordinate value. STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); // Assign packed locations to rectangles. The rectangles are of type @@ -213,10 +217,9 @@ struct stbrp_context #define STBRP_ASSERT assert #endif -// [DEAR IMGUI] Added STBRP__CDECL #ifdef _MSC_VER #define STBRP__NOTUSED(v) (void)(v) -#define STBRP__CDECL __cdecl +#define STBRP__CDECL __cdecl #else #define STBRP__NOTUSED(v) (void)sizeof(v) #define STBRP__CDECL @@ -262,9 +265,6 @@ STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_ou STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) { int i; -#ifndef STBRP_LARGE_RECTS - STBRP_ASSERT(width <= 0xffff && height <= 0xffff); -#endif for (i=0; i < num_nodes-1; ++i) nodes[i].next = &nodes[i+1]; @@ -283,11 +283,7 @@ STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, context->extra[0].y = 0; context->extra[0].next = &context->extra[1]; context->extra[1].x = (stbrp_coord) width; -#ifdef STBRP_LARGE_RECTS context->extra[1].y = (1<<30); -#else - context->extra[1].y = 65535; -#endif context->extra[1].next = NULL; } @@ -433,7 +429,7 @@ static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int widt if (y <= best_y) { if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { best_x = xpos; - STBRP_ASSERT(y <= best_y); + //STBRP_ASSERT(y <= best_y); [DEAR IMGUI] best_y = y; best_waste = waste; best = prev; @@ -529,7 +525,6 @@ static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, i return res; } -// [DEAR IMGUI] Added STBRP__CDECL static int STBRP__CDECL rect_height_compare(const void *a, const void *b) { const stbrp_rect *p = (const stbrp_rect *) a; @@ -541,7 +536,6 @@ static int STBRP__CDECL rect_height_compare(const void *a, const void *b) return (p->w > q->w) ? -1 : (p->w < q->w); } -// [DEAR IMGUI] Added STBRP__CDECL static int STBRP__CDECL rect_original_order(const void *a, const void *b) { const stbrp_rect *p = (const stbrp_rect *) a; @@ -549,12 +543,6 @@ static int STBRP__CDECL rect_original_order(const void *a, const void *b) return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); } -#ifdef STBRP_LARGE_RECTS -#define STBRP__MAXVAL 0xffffffff -#else -#define STBRP__MAXVAL 0xffff -#endif - STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) { int i, all_rects_packed = 1; diff --git a/include/imgui/imstb_textedit.h b/include/imgui/imstb_textedit.h index 2c635b27..a8a82311 100644 --- a/include/imgui/imstb_textedit.h +++ b/include/imgui/imstb_textedit.h @@ -1,10 +1,11 @@ // [DEAR IMGUI] -// This is a slightly modified version of stb_textedit.h 1.13. +// This is a slightly modified version of stb_textedit.h 1.14. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) +// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000) // Grep for [DEAR IMGUI] to find the changes. -// stb_textedit.h - v1.13 - public domain - Sean Barrett +// stb_textedit.h - v1.14 - public domain - Sean Barrett // Development of this library was sponsored by RAD Game Tools // // This C header file implements the guts of a multi-line text-editing @@ -35,6 +36,7 @@ // // VERSION HISTORY // +// 1.14 (2021-07-11) page up/down, various fixes // 1.13 (2019-02-07) fix bug in undo size management // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield @@ -58,6 +60,7 @@ // Ulf Winklemann: move-by-word in 1.1 // Fabian Giesen: secondary key inputs in 1.5 // Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6 +// Louis Schnellbach: page up/down in 1.14 // // Bugfixes: // Scott Graham @@ -93,8 +96,8 @@ // moderate sizes. The undo system does no memory allocations, so // it grows STB_TexteditState by the worst-case storage which is (in bytes): // -// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT -// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT +// [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATECOUNT +// + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHARCOUNT // // // Implementation mode: @@ -522,29 +525,14 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s int z = STB_TEXTEDIT_STRINGLEN(str); int i=0, first; - if (n == z) { - // if it's at the end, then find the last line -- simpler than trying to - // explicitly handle this case in the regular code - if (single_line) { - STB_TEXTEDIT_LAYOUTROW(&r, str, 0); - find->y = 0; - find->first_char = 0; - find->length = z; - find->height = r.ymax - r.ymin; - find->x = r.x1; - } else { - find->y = 0; - find->x = 0; - find->height = 1; - while (i < z) { - STB_TEXTEDIT_LAYOUTROW(&r, str, i); - prev_start = i; - i += r.num_chars; - } - find->first_char = i; - find->length = 0; - find->prev_first = prev_start; - } + if (n == z && single_line) { + // special case if it's at the end (may not be needed?) + STB_TEXTEDIT_LAYOUTROW(&r, str, 0); + find->y = 0; + find->first_char = 0; + find->length = z; + find->height = r.ymax - r.ymin; + find->x = r.x1; return; } @@ -555,9 +543,13 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s STB_TEXTEDIT_LAYOUTROW(&r, str, i); if (n < i + r.num_chars) break; + if (i + r.num_chars == z && z > 0 && STB_TEXTEDIT_GETCHAR(str, z - 1) != STB_TEXTEDIT_NEWLINE) // [DEAR IMGUI] special handling for last line + break; // [DEAR IMGUI] prev_start = i; i += r.num_chars; find->y += r.baseline_y_delta; + if (i == z) // [DEAR IMGUI] + break; // [DEAR IMGUI] } find->first_char = first = i; @@ -716,10 +708,6 @@ static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditSta state->has_preferred_x = 0; return 1; } - // [DEAR IMGUI] - //// remove the undo since we didn't actually insert the characters - //if (state->undostate.undo_point) - // --state->undostate.undo_point; // note: paste failure will leave deleted selection, may be restored with an undo (see https://github.com/nothings/stb/issues/734 for details) return 0; } diff --git a/include/imgui/imstb_truetype.h b/include/imgui/imstb_truetype.h index 48c20261..35c827e6 100644 --- a/include/imgui/imstb_truetype.h +++ b/include/imgui/imstb_truetype.h @@ -1,10 +1,19 @@ // [DEAR IMGUI] -// This is a slightly modified version of stb_truetype.h 1.20. +// This is a slightly modified version of stb_truetype.h 1.26. // Mostly fixing for compiler and static analyzer warnings. // Grep for [DEAR IMGUI] to find the changes. -// stb_truetype.h - v1.20 - public domain -// authored from 2009-2016 by Sean Barrett / RAD Game Tools +// stb_truetype.h - v1.26 - public domain +// authored from 2009-2021 by Sean Barrett / RAD Game Tools +// +// ======================================================================= +// +// NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES +// +// This library does no range checking of the offsets found in the file, +// meaning an attacker can use it to read arbitrary memory. +// +// ======================================================================= // // This library processes TrueType files: // parse files @@ -37,11 +46,11 @@ // Daniel Ribeiro Maciel // // Bug/warning reports/fixes: -// "Zer" on mollyrocket Fabian "ryg" Giesen -// Cass Everitt Martins Mozeiko -// stoiko (Haemimont Games) Cap Petschulat -// Brian Hook Omar Cornut -// Walter van Niftrik github:aloucks +// "Zer" on mollyrocket Fabian "ryg" Giesen github:NiLuJe +// Cass Everitt Martins Mozeiko github:aloucks +// stoiko (Haemimont Games) Cap Petschulat github:oyvindjam +// Brian Hook Omar Cornut github:vassvik +// Walter van Niftrik Ryan Griege // David Gow Peter LaValle // David Given Sergey Popov // Ivan-Assen Ivanov Giumo X. Clanjor @@ -49,11 +58,17 @@ // Johan Duparc Thomas Fields // Hou Qiming Derek Vinyard // Rob Loach Cort Stratton -// Kenney Phillis Jr. github:oyvindjam -// Brian Costabile github:vassvik +// Kenney Phillis Jr. Brian Costabile +// Ken Voskuil (kaesve) // // VERSION HISTORY // +// 1.26 (2021-08-28) fix broken rasterizer +// 1.25 (2021-07-11) many fixes +// 1.24 (2020-02-05) fix warning +// 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) +// 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined +// 1.21 (2019-02-25) fix warning // 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() // 1.19 (2018-02-11) GPOS kerning, STBTT_fmod // 1.18 (2018-01-29) add missing function @@ -248,19 +263,6 @@ // recommend it. // // -// SOURCE STATISTICS (based on v0.6c, 2050 LOC) -// -// Documentation & header file 520 LOC \___ 660 LOC documentation -// Sample code 140 LOC / -// Truetype parsing 620 LOC ---- 620 LOC TrueType -// Software rasterization 240 LOC \. -// Curve tessellation 120 LOC \__ 550 LOC Bitmap creation -// Bitmap management 100 LOC / -// Baked bitmap interface 70 LOC / -// Font name matching & access 150 LOC ---- 150 -// C runtime library abstraction 60 LOC ---- 60 -// -// // PERFORMANCE MEASUREMENTS FOR 1.06: // // 32-bit 64-bit @@ -275,8 +277,8 @@ //// SAMPLE PROGRAMS //// // -// Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless -// +// Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless. +// See "tests/truetype_demo_win32.c" for a complete version. #if 0 #define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation #include "stb_truetype.h" @@ -302,6 +304,8 @@ void my_stbtt_initfont(void) void my_stbtt_print(float x, float y, char *text) { // assume orthographic projection with units = screen pixels, origin at top left + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ftex); glBegin(GL_QUADS); @@ -309,10 +313,10 @@ void my_stbtt_print(float x, float y, char *text) if (*text >= 32 && *text < 128) { stbtt_aligned_quad q; stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9 - glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y0); - glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y0); - glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y1); - glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y1); + glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y0); + glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y0); + glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y1); + glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y1); } ++text; } @@ -719,7 +723,7 @@ struct stbtt_fontinfo int numGlyphs; // number of glyphs, needed for range checking - int loca,head,glyf,hhea,hmtx,kern,gpos; // table locations as offset from start of .ttf + int loca,head,glyf,hhea,hmtx,kern,gpos,svg; // table locations as offset from start of .ttf int index_map; // a cmap mapping for our chosen character encoding int indexToLocFormat; // format needed to map from glyph index to glyph @@ -802,6 +806,18 @@ STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1); // as above, but takes one or more glyph indices for greater efficiency +typedef struct stbtt_kerningentry +{ + int glyph1; // use stbtt_FindGlyphIndex + int glyph2; + int advance; +} stbtt_kerningentry; + +STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info); +STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length); +// Retrieves a complete list of all of the kerning pairs provided by the font +// stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write. +// The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1) ////////////////////////////////////////////////////////////////////////////// // @@ -846,6 +862,12 @@ STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, s STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices); // frees the data allocated above +STBTT_DEF unsigned char *stbtt_FindSVGDoc(const stbtt_fontinfo *info, int gl); +STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo *info, int unicode_codepoint, const char **svg); +STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo *info, int gl, const char **svg); +// fills svg with the character's SVG data. +// returns data size or 0 if SVG not found. + ////////////////////////////////////////////////////////////////////////////// // // BITMAP RENDERING @@ -1347,6 +1369,22 @@ static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict) return stbtt__cff_get_index(&cff); } +// since most people won't use this, find this table the first time it's needed +static int stbtt__get_svg(stbtt_fontinfo *info) +{ + stbtt_uint32 t; + if (info->svg < 0) { + t = stbtt__find_table(info->data, info->fontstart, "SVG "); + if (t) { + stbtt_uint32 offset = ttULONG(info->data + t + 2); + info->svg = t + offset; + } else { + info->svg = 0; + } + } + return info->svg; +} + static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart) { stbtt_uint32 cmap, t; @@ -1426,6 +1464,8 @@ static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, in else info->numGlyphs = 0xffff; + info->svg = -1; + // find a cmap encoding table we understand *now* to avoid searching // later. (todo: could make this installable) // the same regardless of glyph. @@ -1509,12 +1549,12 @@ STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codep search += 2; { - stbtt_uint16 offset, start; + stbtt_uint16 offset, start, last; stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1); - STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item)); start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item); - if (unicode_codepoint < start) + last = ttUSHORT(data + endCount + 2*item); + if (unicode_codepoint < start || unicode_codepoint > last) return 0; offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item); @@ -1774,7 +1814,7 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s } } num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy); - } else if (numberOfContours == -1) { + } else if (numberOfContours < 0) { // Compound shapes. int more = 1; stbtt_uint8 *comp = data + g + 10; @@ -1841,7 +1881,7 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s if (comp_verts) STBTT_free(comp_verts, info->userdata); return 0; } - if (num_vertices > 0) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); //-V595 + if (num_vertices > 0 && vertices) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex)); STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex)); if (vertices) STBTT_free(vertices, info->userdata); vertices = tmp; @@ -1851,9 +1891,6 @@ static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, s // More components ? more = flags & (1<<5); } - } else if (numberOfContours < 0) { - // @TODO other compound variations? - STBTT_assert(0); } else { // numberOfCounters == 0, do nothing } @@ -1971,7 +2008,7 @@ static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int gly start = end; } } - if (fdselector == -1) stbtt__new_buf(NULL, 0); + if (fdselector == -1) return stbtt__new_buf(NULL, 0); // [DEAR IMGUI] fixed, see #6007 and nothings/stb#1422 return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector)); } @@ -2107,7 +2144,7 @@ static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, st subrs = stbtt__cid_get_glyph_subrs(info, glyph_index); has_subrs = 1; } - // fallthrough + // FALLTHROUGH case 0x1D: // callgsubr if (sp < 1) return STBTT__CSERR("call(g|)subr stack"); v = (int) s[--sp]; @@ -2212,7 +2249,7 @@ static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, st } break; default: - if (b0 != 255 && b0 != 28 && (b0 < 32 || b0 > 254)) //-V560 + if (b0 != 255 && b0 != 28 && b0 < 32) return STBTT__CSERR("reserved operator"); // push immediate @@ -2282,7 +2319,49 @@ STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_inde } } -static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) +STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info) +{ + stbtt_uint8 *data = info->data + info->kern; + + // we only look at the first table. it must be 'horizontal' and format 0. + if (!info->kern) + return 0; + if (ttUSHORT(data+2) < 1) // number of tables, need at least 1 + return 0; + if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format + return 0; + + return ttUSHORT(data+10); +} + +STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length) +{ + stbtt_uint8 *data = info->data + info->kern; + int k, length; + + // we only look at the first table. it must be 'horizontal' and format 0. + if (!info->kern) + return 0; + if (ttUSHORT(data+2) < 1) // number of tables, need at least 1 + return 0; + if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format + return 0; + + length = ttUSHORT(data+10); + if (table_length < length) + length = table_length; + + for (k = 0; k < length; k++) + { + table[k].glyph1 = ttUSHORT(data+18+(k*6)); + table[k].glyph2 = ttUSHORT(data+20+(k*6)); + table[k].advance = ttSHORT(data+22+(k*6)); + } + + return length; +} + +static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) { stbtt_uint8 *data = info->data + info->kern; stbtt_uint32 needle, straw; @@ -2312,245 +2391,225 @@ static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph return 0; } -static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph) -{ - stbtt_uint16 coverageFormat = ttUSHORT(coverageTable); - switch(coverageFormat) { - case 1: { - stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2); - - // Binary search. - stbtt_int32 l=0, r=glyphCount-1, m; - int straw, needle=glyph; - while (l <= r) { - stbtt_uint8 *glyphArray = coverageTable + 4; - stbtt_uint16 glyphID; - m = (l + r) >> 1; - glyphID = ttUSHORT(glyphArray + 2 * m); - straw = glyphID; - if (needle < straw) - r = m - 1; - else if (needle > straw) - l = m + 1; - else { - return m; - } +static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph) +{ + stbtt_uint16 coverageFormat = ttUSHORT(coverageTable); + switch (coverageFormat) { + case 1: { + stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2); + + // Binary search. + stbtt_int32 l=0, r=glyphCount-1, m; + int straw, needle=glyph; + while (l <= r) { + stbtt_uint8 *glyphArray = coverageTable + 4; + stbtt_uint16 glyphID; + m = (l + r) >> 1; + glyphID = ttUSHORT(glyphArray + 2 * m); + straw = glyphID; + if (needle < straw) + r = m - 1; + else if (needle > straw) + l = m + 1; + else { + return m; } - } break; - - case 2: { - stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2); - stbtt_uint8 *rangeArray = coverageTable + 4; - - // Binary search. - stbtt_int32 l=0, r=rangeCount-1, m; - int strawStart, strawEnd, needle=glyph; - while (l <= r) { - stbtt_uint8 *rangeRecord; - m = (l + r) >> 1; - rangeRecord = rangeArray + 6 * m; - strawStart = ttUSHORT(rangeRecord); - strawEnd = ttUSHORT(rangeRecord + 2); - if (needle < strawStart) - r = m - 1; - else if (needle > strawEnd) - l = m + 1; - else { - stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4); - return startCoverageIndex + glyph - strawStart; - } + } + break; + } + + case 2: { + stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2); + stbtt_uint8 *rangeArray = coverageTable + 4; + + // Binary search. + stbtt_int32 l=0, r=rangeCount-1, m; + int strawStart, strawEnd, needle=glyph; + while (l <= r) { + stbtt_uint8 *rangeRecord; + m = (l + r) >> 1; + rangeRecord = rangeArray + 6 * m; + strawStart = ttUSHORT(rangeRecord); + strawEnd = ttUSHORT(rangeRecord + 2); + if (needle < strawStart) + r = m - 1; + else if (needle > strawEnd) + l = m + 1; + else { + stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4); + return startCoverageIndex + glyph - strawStart; } - } break; + } + break; + } - default: { - // There are no other cases. - STBTT_assert(0); - } break; - } + default: return -1; // unsupported + } - return -1; + return -1; } static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable, int glyph) { - stbtt_uint16 classDefFormat = ttUSHORT(classDefTable); - switch(classDefFormat) - { - case 1: { - stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2); - stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4); - stbtt_uint8 *classDef1ValueArray = classDefTable + 6; - - if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount) - return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID)); - - // [DEAR IMGUI] Commented to fix static analyzer warning - //classDefTable = classDef1ValueArray + 2 * glyphCount; - } break; - - case 2: { - stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2); - stbtt_uint8 *classRangeRecords = classDefTable + 4; - - // Binary search. - stbtt_int32 l=0, r=classRangeCount-1, m; - int strawStart, strawEnd, needle=glyph; - while (l <= r) { - stbtt_uint8 *classRangeRecord; - m = (l + r) >> 1; - classRangeRecord = classRangeRecords + 6 * m; - strawStart = ttUSHORT(classRangeRecord); - strawEnd = ttUSHORT(classRangeRecord + 2); - if (needle < strawStart) - r = m - 1; - else if (needle > strawEnd) - l = m + 1; - else - return (stbtt_int32)ttUSHORT(classRangeRecord + 4); - } + stbtt_uint16 classDefFormat = ttUSHORT(classDefTable); + switch (classDefFormat) + { + case 1: { + stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2); + stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4); + stbtt_uint8 *classDef1ValueArray = classDefTable + 6; - // [DEAR IMGUI] Commented to fix static analyzer warning - //classDefTable = classRangeRecords + 6 * classRangeCount; - } break; + if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount) + return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID)); + break; + } - default: { - // There are no other cases. - STBTT_assert(0); - } break; - } + case 2: { + stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2); + stbtt_uint8 *classRangeRecords = classDefTable + 4; + + // Binary search. + stbtt_int32 l=0, r=classRangeCount-1, m; + int strawStart, strawEnd, needle=glyph; + while (l <= r) { + stbtt_uint8 *classRangeRecord; + m = (l + r) >> 1; + classRangeRecord = classRangeRecords + 6 * m; + strawStart = ttUSHORT(classRangeRecord); + strawEnd = ttUSHORT(classRangeRecord + 2); + if (needle < strawStart) + r = m - 1; + else if (needle > strawEnd) + l = m + 1; + else + return (stbtt_int32)ttUSHORT(classRangeRecord + 4); + } + break; + } - return -1; + default: + return -1; // Unsupported definition type, return an error. + } + + // "All glyphs not assigned to a class fall into class 0". (OpenType spec) + return 0; } // Define to STBTT_assert(x) if you want to break on unimplemented formats. #define STBTT_GPOS_TODO_assert(x) -static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) -{ - stbtt_uint16 lookupListOffset; - stbtt_uint8 *lookupList; - stbtt_uint16 lookupCount; - stbtt_uint8 *data; - stbtt_int32 i; - - if (!info->gpos) return 0; - - data = info->data + info->gpos; - - if (ttUSHORT(data+0) != 1) return 0; // Major version 1 - if (ttUSHORT(data+2) != 0) return 0; // Minor version 0 - - lookupListOffset = ttUSHORT(data+8); - lookupList = data + lookupListOffset; - lookupCount = ttUSHORT(lookupList); - - for (i=0; i> 1; - pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m; - secondGlyph = ttUSHORT(pairValue); - straw = secondGlyph; - if (needle < straw) - r = m - 1; - else if (needle > straw) - l = m + 1; - else { - stbtt_int16 xAdvance = ttSHORT(pairValue + 2); - return xAdvance; - } - } - } break; - - case 2: { - stbtt_uint16 valueFormat1 = ttUSHORT(table + 4); - stbtt_uint16 valueFormat2 = ttUSHORT(table + 6); - - stbtt_uint16 classDef1Offset = ttUSHORT(table + 8); - stbtt_uint16 classDef2Offset = ttUSHORT(table + 10); - int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1); - int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2); - - stbtt_uint16 class1Count = ttUSHORT(table + 12); - stbtt_uint16 class2Count = ttUSHORT(table + 14); - STBTT_assert(glyph1class < class1Count); - STBTT_assert(glyph2class < class2Count); - - // TODO: Support more formats. - STBTT_GPOS_TODO_assert(valueFormat1 == 4); - if (valueFormat1 != 4) return 0; - STBTT_GPOS_TODO_assert(valueFormat2 == 0); - if (valueFormat2 != 0) return 0; - - if (glyph1class >= 0 && glyph1class < class1Count && glyph2class >= 0 && glyph2class < class2Count) { - stbtt_uint8 *class1Records = table + 16; - stbtt_uint8 *class2Records = class1Records + 2 * (glyph1class * class2Count); - stbtt_int16 xAdvance = ttSHORT(class2Records + 2 * glyph2class); - return xAdvance; - } - } break; - - default: { - // There are no other cases. - STBTT_assert(0); - break; - } // [DEAR IMGUI] removed ; - } - } - break; - } // [DEAR IMGUI] removed ; +static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2) +{ + stbtt_uint16 lookupListOffset; + stbtt_uint8 *lookupList; + stbtt_uint16 lookupCount; + stbtt_uint8 *data; + stbtt_int32 i, sti; + + if (!info->gpos) return 0; + + data = info->data + info->gpos; + + if (ttUSHORT(data+0) != 1) return 0; // Major version 1 + if (ttUSHORT(data+2) != 0) return 0; // Minor version 0 + + lookupListOffset = ttUSHORT(data+8); + lookupList = data + lookupListOffset; + lookupCount = ttUSHORT(lookupList); + + for (i=0; i= pairSetCount) return 0; + + needle=glyph2; + r=pairValueCount-1; + l=0; + + // Binary search. + while (l <= r) { + stbtt_uint16 secondGlyph; + stbtt_uint8 *pairValue; + m = (l + r) >> 1; + pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m; + secondGlyph = ttUSHORT(pairValue); + straw = secondGlyph; + if (needle < straw) + r = m - 1; + else if (needle > straw) + l = m + 1; + else { + stbtt_int16 xAdvance = ttSHORT(pairValue + 2); + return xAdvance; + } + } + } else + return 0; + break; + } + + case 2: { + stbtt_uint16 valueFormat1 = ttUSHORT(table + 4); + stbtt_uint16 valueFormat2 = ttUSHORT(table + 6); + if (valueFormat1 == 4 && valueFormat2 == 0) { // Support more formats? + stbtt_uint16 classDef1Offset = ttUSHORT(table + 8); + stbtt_uint16 classDef2Offset = ttUSHORT(table + 10); + int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1); + int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2); + + stbtt_uint16 class1Count = ttUSHORT(table + 12); + stbtt_uint16 class2Count = ttUSHORT(table + 14); + stbtt_uint8 *class1Records, *class2Records; + stbtt_int16 xAdvance; + + if (glyph1class < 0 || glyph1class >= class1Count) return 0; // malformed + if (glyph2class < 0 || glyph2class >= class2Count) return 0; // malformed + + class1Records = table + 16; + class2Records = class1Records + 2 * (glyph1class * class2Count); + xAdvance = ttSHORT(class2Records + 2 * glyph2class); + return xAdvance; + } else + return 0; + break; + } default: - // TODO: Implement other stuff. - break; - } - } + return 0; // Unsupported position format + } + } + } - return 0; + return 0; } STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int g2) @@ -2559,8 +2618,7 @@ STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int if (info->gpos) xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2); - - if (info->kern) + else if (info->kern) xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2); return xAdvance; @@ -2621,6 +2679,45 @@ STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v) STBTT_free(v, info->userdata); } +STBTT_DEF stbtt_uint8 *stbtt_FindSVGDoc(const stbtt_fontinfo *info, int gl) +{ + int i; + stbtt_uint8 *data = info->data; + stbtt_uint8 *svg_doc_list = data + stbtt__get_svg((stbtt_fontinfo *) info); + + int numEntries = ttUSHORT(svg_doc_list); + stbtt_uint8 *svg_docs = svg_doc_list + 2; + + for(i=0; i= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2))) + return svg_doc; + } + return 0; +} + +STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo *info, int gl, const char **svg) +{ + stbtt_uint8 *data = info->data; + stbtt_uint8 *svg_doc; + + if (info->svg == 0) + return 0; + + svg_doc = stbtt_FindSVGDoc(info, gl); + if (svg_doc != NULL) { + *svg = (char *) data + info->svg + ttULONG(svg_doc + 4); + return ttULONG(svg_doc + 8); + } else { + return 0; + } +} + +STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo *info, int unicode_codepoint, const char **svg) +{ + return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svg); +} + ////////////////////////////////////////////////////////////////////////////// // // antialiasing software rasterizer @@ -2970,6 +3067,23 @@ static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edg } } +static float stbtt__sized_trapezoid_area(float height, float top_width, float bottom_width) +{ + STBTT_assert(top_width >= 0); + STBTT_assert(bottom_width >= 0); + return (top_width + bottom_width) / 2.0f * height; +} + +static float stbtt__position_trapezoid_area(float height, float tx0, float tx1, float bx0, float bx1) +{ + return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0); +} + +static float stbtt__sized_triangle_area(float height, float width) +{ + return height * width / 2; +} + static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top) { float y_bottom = y_top+1; @@ -3024,13 +3138,13 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, float height; // simple case, only spans one pixel int x = (int) x_top; - height = sy1 - sy0; + height = (sy1 - sy0) * e->direction; STBTT_assert(x >= 0 && x < len); - scanline[x] += e->direction * (1-((x_top - x) + (x_bottom-x))/2) * height; - scanline_fill[x] += e->direction * height; // everything right of this pixel is filled + scanline[x] += stbtt__position_trapezoid_area(height, x_top, x+1.0f, x_bottom, x+1.0f); + scanline_fill[x] += height; // everything right of this pixel is filled } else { int x,x1,x2; - float y_crossing, step, sign, area; + float y_crossing, y_final, step, sign, area; // covers 2+ pixels if (x_top > x_bottom) { // flip scanline vertically; signed area is the same @@ -3042,32 +3156,83 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, dx = -dx; dy = -dy; t = x0, x0 = xb, xb = t; - // [DEAR IMGUI] Fix static analyzer warning - (void)dx; // [ImGui: fix static analyzer warning] } + STBTT_assert(dy >= 0); + STBTT_assert(dx >= 0); x1 = (int) x_top; x2 = (int) x_bottom; // compute intersection with y axis at x1+1 - y_crossing = (x1+1 - x0) * dy + y_top; + y_crossing = y_top + dy * (x1+1 - x0); + + // compute intersection with y axis at x2 + y_final = y_top + dy * (x2 - x0); + + // x1 x_top x2 x_bottom + // y_top +------|-----+------------+------------+--------|---+------------+ + // | | | | | | + // | | | | | | + // sy0 | Txxxxx|............|............|............|............| + // y_crossing | *xxxxx.......|............|............|............| + // | | xxxxx..|............|............|............| + // | | /- xx*xxxx........|............|............| + // | | dy < | xxxxxx..|............|............| + // y_final | | \- | xx*xxx.........|............| + // sy1 | | | | xxxxxB...|............| + // | | | | | | + // | | | | | | + // y_bottom +------------+------------+------------+------------+------------+ + // + // goal is to measure the area covered by '.' in each pixel + + // if x2 is right at the right edge of x1, y_crossing can blow up, github #1057 + // @TODO: maybe test against sy1 rather than y_bottom? + if (y_crossing > y_bottom) + y_crossing = y_bottom; sign = e->direction; - // area of the rectangle covered from y0..y_crossing + + // area of the rectangle covered from sy0..y_crossing area = sign * (y_crossing-sy0); - // area of the triangle (x_top,y0), (x+1,y0), (x+1,y_crossing) - scanline[x1] += area * (1-((x_top - x1)+(x1+1-x1))/2); - step = sign * dy; + // area of the triangle (x_top,sy0), (x1+1,sy0), (x1+1,y_crossing) + scanline[x1] += stbtt__sized_triangle_area(area, x1+1 - x_top); + + // check if final y_crossing is blown up; no test case for this + if (y_final > y_bottom) { + int denom = (x2 - (x1+1)); + y_final = y_bottom; + if (denom != 0) { // [DEAR IMGUI] Avoid div by zero (https://github.com/nothings/stb/issues/1316) + dy = (y_final - y_crossing ) / denom; // if denom=0, y_final = y_crossing, so y_final <= y_bottom + } + } + + // in second pixel, area covered by line segment found in first pixel + // is always a rectangle 1 wide * the height of that line segment; this + // is exactly what the variable 'area' stores. it also gets a contribution + // from the line segment within it. the THIRD pixel will get the first + // pixel's rectangle contribution, the second pixel's rectangle contribution, + // and its own contribution. the 'own contribution' is the same in every pixel except + // the leftmost and rightmost, a trapezoid that slides down in each pixel. + // the second pixel's contribution to the third pixel will be the + // rectangle 1 wide times the height change in the second pixel, which is dy. + + step = sign * dy * 1; // dy is dy/dx, change in y for every 1 change in x, + // which multiplied by 1-pixel-width is how much pixel area changes for each step in x + // so the area advances by 'step' every time + for (x = x1+1; x < x2; ++x) { - scanline[x] += area + step/2; + scanline[x] += area + step/2; // area of trapezoid is 1*step/2 area += step; } - y_crossing += dy * (x2 - (x1+1)); - - STBTT_assert(STBTT_fabs(area) <= 1.01f); + STBTT_assert(STBTT_fabs(area) <= 1.01f); // accumulated error from area += step unless we round step down + STBTT_assert(sy1 > y_final-0.01f); - scanline[x2] += area + sign * (1-((x2-x2)+(x_bottom-x2))/2) * (sy1-y_crossing); + // area covered in the last pixel is the rectangle from all the pixels to the left, + // plus the trapezoid filled by the line segment in this pixel all the way to the right edge + scanline[x2] += area + sign * stbtt__position_trapezoid_area(sy1-y_final, (float) x2, x2+1.0f, x_bottom, x2+1.0f); + // the rest of the line is filled based on the total height of the line segment in this pixel scanline_fill[x2] += sign * (sy1-sy0); } } else { @@ -3075,6 +3240,9 @@ static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, // clipping logic. since this does not match the intended use // of this library, we use a different, very slow brute // force implementation + // note though that this does happen some of the time because + // x_top and x_bottom can be extrapolated at the top & bottom of + // the shape and actually lie outside the bounding box int x; for (x=0; x < len; ++x) { // cases: @@ -3989,6 +4157,7 @@ static float stbtt__oversample_shift(int oversample) STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) { int i,j,k; + int missing_glyph_added = 0; k=0; for (i=0; i < num_ranges; ++i) { @@ -4000,7 +4169,7 @@ STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stb int x0,y0,x1,y1; int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j]; int glyph = stbtt_FindGlyphIndex(info, codepoint); - if (glyph == 0 && spc->skip_missing) { + if (glyph == 0 && (spc->skip_missing || missing_glyph_added)) { rects[k].w = rects[k].h = 0; } else { stbtt_GetGlyphBitmapBoxSubpixel(info,glyph, @@ -4010,6 +4179,8 @@ STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stb &x0,&y0,&x1,&y1); rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1); rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1); + if (glyph == 0) + missing_glyph_added = 1; } ++k; } @@ -4044,7 +4215,7 @@ STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo *info // rects array must be big enough to accommodate all characters in the given ranges STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects) { - int i,j,k, return_value = 1; + int i,j,k, missing_glyph = -1, return_value = 1; // save current values int old_h_over = spc->h_oversample; @@ -4109,6 +4280,13 @@ STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const bc->yoff = (float) y0 * recip_v + sub_y; bc->xoff2 = (x0 + r->w) * recip_h + sub_x; bc->yoff2 = (y0 + r->h) * recip_v + sub_y; + + if (glyph == 0) + missing_glyph = j; + } else if (spc->skip_missing) { + return_value = 0; + } else if (r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) { + ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph]; } else { return_value = 0; // if any fail, report failure } @@ -4132,7 +4310,7 @@ STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges) { stbtt_fontinfo info; - int i,j,n, return_value; // [DEAR IMGUI] removed = 1 + int i, j, n, return_value; // [DEAR IMGUI] removed = 1; //stbrp_context *context = (stbrp_context *) spc->pack_info; stbrp_rect *rects; @@ -4301,15 +4479,14 @@ static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex float y_frac; int winding = 0; - orig[0] = x; - //orig[1] = y; // [DEAR IMGUI] commented double assignment - // make sure y never passes through a vertex of the shape y_frac = (float) STBTT_fmod(y, 1.0f); if (y_frac < 0.01f) y += 0.01f; else if (y_frac > 0.99f) y -= 0.01f; + + orig[0] = x; orig[1] = y; // test a ray from (-infinity,y) to (x,y) @@ -4371,35 +4548,35 @@ static float stbtt__cuberoot( float x ) return (float) STBTT_pow( x,1.0f/3.0f); } -// x^3 + c*x^2 + b*x + a = 0 +// x^3 + a*x^2 + b*x + c = 0 static int stbtt__solve_cubic(float a, float b, float c, float* r) { - float s = -a / 3; - float p = b - a*a / 3; - float q = a * (2*a*a - 9*b) / 27 + c; + float s = -a / 3; + float p = b - a*a / 3; + float q = a * (2*a*a - 9*b) / 27 + c; float p3 = p*p*p; - float d = q*q + 4*p3 / 27; - if (d >= 0) { - float z = (float) STBTT_sqrt(d); - float u = (-q + z) / 2; - float v = (-q - z) / 2; - u = stbtt__cuberoot(u); - v = stbtt__cuberoot(v); - r[0] = s + u + v; - return 1; - } else { - float u = (float) STBTT_sqrt(-p/3); - float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; // p3 must be negative, since d is negative - float m = (float) STBTT_cos(v); + float d = q*q + 4*p3 / 27; + if (d >= 0) { + float z = (float) STBTT_sqrt(d); + float u = (-q + z) / 2; + float v = (-q - z) / 2; + u = stbtt__cuberoot(u); + v = stbtt__cuberoot(v); + r[0] = s + u + v; + return 1; + } else { + float u = (float) STBTT_sqrt(-p/3); + float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; // p3 must be negative, since d is negative + float m = (float) STBTT_cos(v); float n = (float) STBTT_cos(v-3.141592/2)*1.732050808f; - r[0] = s + u * 2 * m; - r[1] = s - u * (m + n); - r[2] = s - u * (m - n); + r[0] = s + u * 2 * m; + r[1] = s - u * (m + n); + r[2] = s - u * (m - n); //STBTT_assert( STBTT_fabs(((r[0]+a)*r[0]+b)*r[0]+c) < 0.05f); // these asserts may not be safe at all scales, though they're in bezier t parameter units so maybe? //STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); //STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); - return 3; + return 3; } } @@ -4410,12 +4587,7 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc int w,h; unsigned char *data; - // if one scale is 0, use same scale for both - if (scale_x == 0) scale_x = scale_y; - if (scale_y == 0) { - if (scale_x == 0) return NULL; // if both scales are 0, return NULL - scale_y = scale_x; - } + if (scale == 0) return NULL; stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1); @@ -4481,18 +4653,17 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc for (i=0; i < num_verts; ++i) { float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y; - // check against every point here rather than inside line/curve primitives -- @TODO: wrong if multiple 'moves' in a row produce a garbage point, and given culling, probably more efficient to do within line/curve - float dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy); - if (dist2 < min_dist*min_dist) - min_dist = (float) STBTT_sqrt(dist2); - - if (verts[i].type == STBTT_vline) { + if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) { float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y; + float dist,dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy); + if (dist2 < min_dist*min_dist) + min_dist = (float) STBTT_sqrt(dist2); + // coarse culling against bbox //if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && // sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) - float dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i]; + dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i]; STBTT_assert(i != 0); if (dist < min_dist) { // check position along line @@ -4519,7 +4690,8 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc float ax = x1-x0, ay = y1-y0; float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2; float mx = x0 - sx, my = y0 - sy; - float res[3],px,py,t,it; + float res[3] = {0.f,0.f,0.f}; + float px,py,t,it,dist2; float a_inv = precompute[i]; if (a_inv == 0.0) { // if a_inv is 0, it's 2nd degree so use quadratic formula float a = 3*(ax*bx + ay*by); @@ -4546,6 +4718,10 @@ STBTT_DEF unsigned char * stbtt_GetGlyphSDF(const stbtt_fontinfo *info, float sc float d = (mx*ax+my*ay) * a_inv; num = stbtt__solve_cubic(b, c, d, res); } + dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy); + if (dist2 < min_dist*min_dist) + min_dist = (float) STBTT_sqrt(dist2); + if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) { t = res[0], it = 1.0f - t; px = it*it*x0 + 2*t*it*x1 + t*t*x2; @@ -4805,6 +4981,12 @@ STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const // FULL VERSION HISTORY // +// 1.25 (2021-07-11) many fixes +// 1.24 (2020-02-05) fix warning +// 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) +// 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined +// 1.21 (2019-02-25) fix warning +// 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() // 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod // 1.18 (2018-01-29) add missing function // 1.17 (2017-07-23) make more arguments const; doc fix diff --git a/src/imgui/LICENSE.txt b/src/imgui/LICENSE.txt index 780533dc..fb715bdc 100644 --- a/src/imgui/LICENSE.txt +++ b/src/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2021 Omar Cornut +Copyright (c) 2014-2023 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index d7653d41..3faca98f 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (main code and documentation) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/4451 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/5243 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues @@ -65,11 +65,13 @@ CODE // [SECTION] MISC HELPERS/UTILITIES (Color functions) // [SECTION] ImGuiStorage // [SECTION] ImGuiTextFilter -// [SECTION] ImGuiTextBuffer +// [SECTION] ImGuiTextBuffer, ImGuiTextIndex // [SECTION] ImGuiListClipper // [SECTION] STYLING // [SECTION] RENDER HELPERS +// [SECTION] INITIALIZATION, SHUTDOWN // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +// [SECTION] INPUTS // [SECTION] ERROR CHECKING // [SECTION] LAYOUT // [SECTION] SCROLLING @@ -79,9 +81,11 @@ CODE // [SECTION] DRAG AND DROP // [SECTION] LOGGING/CAPTURING // [SECTION] SETTINGS -// [SECTION] VIEWPORTS +// [SECTION] LOCALIZATION +// [SECTION] VIEWPORTS, PLATFORM WINDOWS // [SECTION] PLATFORM DEPENDENT HELPERS // [SECTION] METRICS/DEBUGGER WINDOW +// [SECTION] DEBUG LOG WINDOW // [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) */ @@ -100,6 +104,7 @@ CODE - Easy to hack and improve. - Minimize setup and maintenance. - Minimize state storage on user side. + - Minimize state synchronization. - Portable, minimize dependencies, run on target (consoles, phones, etc.). - Efficient runtime and memory consumption. @@ -123,14 +128,13 @@ CODE - Hold SHIFT or use mouse to select text. - CTRL+Left/Right to word jump. - CTRL+Shift+Left/Right to select words. - - CTRL+A our Double-Click to select all. + - CTRL+A or Double-Click to select all. - CTRL+X,CTRL+C,CTRL+V to use OS clipboard/ - CTRL+Z,CTRL+Y to undo/redo. - ESCAPE to revert text to its original value. - - You can apply arithmetic operators +,*,/ on numerical values. Use +- to subtract (because - would set a negative value!) - Controls are automatically adjusted for OSX to match standard OSX text editing operations. - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard. - - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. See suggested mappings in imgui.h ImGuiNavInput_ + download PNG/PSD at http://dearimgui.org/controls_sheets + - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. Download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets PROGRAMMER GUIDE @@ -254,9 +258,9 @@ CODE io.DeltaTime = 1.0f/60.0f; // set the time elapsed since the previous frame (in seconds) io.DisplaySize.x = 1920.0f; // set the current display width io.DisplaySize.y = 1280.0f; // set the current display height here - io.MousePos = my_mouse_pos; // set the mouse position - io.MouseDown[0] = my_mouse_buttons[0]; // set the mouse button states - io.MouseDown[1] = my_mouse_buttons[1]; + io.AddMousePosEvent(mouse_x, mouse_y); // update mouse position + io.AddMouseButtonEvent(0, mouse_b[0]); // update mouse button states + io.AddMouseButtonEvent(1, mouse_b[1]); // update mouse button states // Call NewFrame(), after this point you can use ImGui::* functions anytime // (So you want to try calling NewFrame() as early as you can in your main loop to be able to use Dear ImGui everywhere) @@ -288,12 +292,14 @@ CODE --------------------------------------------- The backends in impl_impl_XXX.cpp files contain many working implementations of a rendering function. - void void MyImGuiRenderFunction(ImDrawData* draw_data) + void MyImGuiRenderFunction(ImDrawData* draw_data) { // TODO: Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled + // TODO: Setup texture sampling state: sample with bilinear filtering (NOT point/nearest filtering). Use 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines;' to allow point/nearest filtering. // TODO: Setup viewport covering draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize // TODO: Setup orthographic projection matrix cover draw_data->DisplayPos to draw_data->DisplayPos + draw_data->DisplaySize // TODO: Setup shader: vertex { float2 pos, float2 uv, u32 color }, fragment shader sample color from 1 texture, multiply by vertex color. + ImVec2 clip_off = draw_data->DisplayPos; for (int n = 0; n < draw_data->CmdListsCount; n++) { const ImDrawList* cmd_list = draw_data->CmdLists[n]; @@ -308,9 +314,11 @@ CODE } else { - // The texture for the draw call is specified by pcmd->GetTexID(). - // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization. - MyEngineBindTexture((MyTexture*)pcmd->GetTexID()); + // Project scissor/clipping rectangles into framebuffer space + ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); + ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); + if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) + continue; // We are using scissoring to clip some objects. All low-level graphics API should support it. // - If your engine doesn't support scissoring yet, you may ignore this at first. You will get some small glitches @@ -321,14 +329,16 @@ CODE // - In the interest of supporting multi-viewport applications (see 'docking' branch on github), // always subtract draw_data->DisplayPos from clipping bounds to convert them to your viewport space. // - Note that pcmd->ClipRect contains Min+Max bounds. Some graphics API may use Min+Max, other may use Min+Size (size being Max-Min) - ImVec2 pos = draw_data->DisplayPos; - MyEngineScissor((int)(pcmd->ClipRect.x - pos.x), (int)(pcmd->ClipRect.y - pos.y), (int)(pcmd->ClipRect.z - pos.x), (int)(pcmd->ClipRect.w - pos.y)); + MyEngineSetScissor(clip_min.x, clip_min.y, clip_max.x, clip_max.y); + + // The texture for the draw call is specified by pcmd->GetTexID(). + // The vast majority of draw calls will use the Dear ImGui texture atlas, which value you have set yourself during initialization. + MyEngineBindTexture((MyTexture*)pcmd->GetTexID()); // Render 'pcmd->ElemCount/3' indexed triangles. // By default the indices ImDrawIdx are 16-bit, you can change them to 32-bit in imconfig.h if your engine doesn't support 16-bit indices. - MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer, vtx_buffer); + MyEngineDrawIndexedTriangles(pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer + pcmd->IdxOffset, vtx_buffer, pcmd->VtxOffset); } - idx_buffer += pcmd->ElemCount; } } } @@ -337,31 +347,27 @@ CODE USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS ------------------------------------------ - The gamepad/keyboard navigation is fairly functional and keeps being improved. - - Gamepad support is particularly useful to use Dear ImGui on a console system (e.g. PS4, Switch, XB1) without a mouse! - - You can ask questions and report issues at https://github.com/ocornut/imgui/issues/787 + - Gamepad support is particularly useful to use Dear ImGui on a console system (e.g. PlayStation, Switch, Xbox) without a mouse! - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable. - Keyboard: - - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. - NewFrame() will automatically fill io.NavInputs[] based on your io.KeysDown[] + io.KeyMap[] arrays. - - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), the io.WantCaptureKeyboard flag - will be set. For more advanced uses, you may want to read from: + - Application: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. + - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), + the io.WantCaptureKeyboard flag will be set. For more advanced uses, you may want to read from: - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. Please reach out if you think the game vs navigation input sharing could be improved. - Gamepad: - - Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. - - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + fill the io.NavInputs[] fields before calling NewFrame(). - Note that io.NavInputs[] is cleared by EndFrame(). - - See 'enum ImGuiNavInput_' in imgui.h for a description of inputs. For each entry of io.NavInputs[], set the following values: - 0.0f= not held. 1.0f= fully held. Pass intermediate 0.0f..1.0f values for analog triggers/sticks. - - We use a simple >0.0f test for activation testing, and won't attempt to test for a dead-zone. - Your code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). + - Application: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. + - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + call io.AddKeyEvent/AddKeyAnalogEvent() with ImGuiKey_Gamepad_XXX keys. + For analog values (0.0f to 1.0f), backend is responsible to handling a dead-zone and rescaling inputs accordingly. + Backend code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). + - BEFORE 1.87, BACKENDS USED TO WRITE TO io.NavInputs[]. This is now obsolete. Please call io functions instead! - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://dearimgui.org/controls_sheets - - If you need to share inputs between your game and the imgui parts, the easiest approach is to go all-or-nothing, with a buttons combo - to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. + - If you need to share inputs between your game and the Dear ImGui interface, the easiest approach is to go all-or-nothing, + with a buttons combo to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. - Mouse: - - PS4 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. + - PS4/PS5 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + uSynergy.c (on your console/tablet/phone app) to share your PC mouse/keyboard. - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. @@ -380,6 +386,79 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2022/10/26 (1.89) - commented out redirecting OpenPopupContextItem() which was briefly the name of OpenPopupOnItemClick() from 1.77 to 1.79. + - 2022/10/12 (1.89) - removed runtime patching of invalid "%f"/"%0.f" format strings for DragInt()/SliderInt(). This was obsoleted in 1.61 (May 2018). See 1.61 changelog for details. + - 2022/09/26 (1.89) - renamed and merged keyboard modifiers key enums and flags into a same set. Kept inline redirection enums (will obsolete). + - ImGuiKey_ModCtrl and ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl + - ImGuiKey_ModShift and ImGuiModFlags_Shift -> ImGuiMod_Shift + - ImGuiKey_ModAlt and ImGuiModFlags_Alt -> ImGuiMod_Alt + - ImGuiKey_ModSuper and ImGuiModFlags_Super -> ImGuiMod_Super + the ImGuiKey_ModXXX were introduced in 1.87 and mostly used by backends. + the ImGuiModFlags_XXX have been exposed in imgui.h but not really used by any public api only by third-party extensions. + exceptionally commenting out the older ImGuiKeyModFlags_XXX names ahead of obsolescence schedule to reduce confusion and because they were not meant to be used anyway. + - 2022/09/20 (1.89) - ImGuiKey is now a typed enum, allowing ImGuiKey_XXX symbols to be named in debuggers. + this will require uses of legacy backend-dependent indices to be casted, e.g. + - with imgui_impl_glfw: IsKeyPressed(GLFW_KEY_A) -> IsKeyPressed((ImGuiKey)GLFW_KEY_A); + - with imgui_impl_win32: IsKeyPressed('A') -> IsKeyPressed((ImGuiKey)'A') + - etc. However if you are upgrading code you might well use the better, backend-agnostic IsKeyPressed(ImGuiKey_A) now! + - 2022/09/12 (1.89) - removed the bizarre legacy default argument for 'TreePush(const void* ptr = NULL)', always pass a pointer value explicitly. NULL/nullptr is ok but require cast, e.g. TreePush((void*)nullptr); + - 2022/09/05 (1.89) - commented out redirecting functions/enums names that were marked obsolete in 1.77 and 1.78 (June 2020): + - DragScalar(), DragScalarN(), DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(): For old signatures ending with (..., const char* format, float power = 1.0f) -> use (..., format ImGuiSliderFlags_Logarithmic) if power != 1.0f. + - SliderScalar(), SliderScalarN(), SliderFloat(), SliderFloat2(), SliderFloat3(), SliderFloat4(): For old signatures ending with (..., const char* format, float power = 1.0f) -> use (..., format ImGuiSliderFlags_Logarithmic) if power != 1.0f. + - BeginPopupContextWindow(const char*, ImGuiMouseButton, bool) -> use BeginPopupContextWindow(const char*, ImGuiPopupFlags) + - 2022/09/02 (1.89) - obsoleted using SetCursorPos()/SetCursorScreenPos() to extend parent window/cell boundaries. + this relates to when moving the cursor position beyond current boundaries WITHOUT submitting an item. + - previously this would make the window content size ~200x200: + Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End(); + - instead, please submit an item: + Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + Dummy(ImVec2(0,0)) + End(); + - alternative: + Begin(...) + Dummy(ImVec2(200,200)) + End(); + - content size is now only extended when submitting an item! + - with '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' this will now be detected and assert. + - without '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' this will silently be fixed until we obsolete it. + - 2022/08/03 (1.89) - changed signature of ImageButton() function. Kept redirection function (will obsolete). + - added 'const char* str_id' parameter + removed 'int frame_padding = -1' parameter. + - old signature: bool ImageButton(ImTextureID tex_id, ImVec2 size, ImVec2 uv0 = ImVec2(0,0), ImVec2 uv1 = ImVec2(1,1), int frame_padding = -1, ImVec4 bg_col = ImVec4(0,0,0,0), ImVec4 tint_col = ImVec4(1,1,1,1)); + - used the ImTextureID value to create an ID. This was inconsistent with other functions, led to ID conflicts, and caused problems with engines using transient ImTextureID values. + - had a FramePadding override which was inconsistent with other functions and made the already-long signature even longer. + - new signature: bool ImageButton(const char* str_id, ImTextureID tex_id, ImVec2 size, ImVec2 uv0 = ImVec2(0,0), ImVec2 uv1 = ImVec2(1,1), ImVec4 bg_col = ImVec4(0,0,0,0), ImVec4 tint_col = ImVec4(1,1,1,1)); + - requires an explicit identifier. You may still use e.g. PushID() calls and then pass an empty identifier. + - always uses style.FramePadding for padding, to be consistent with other buttons. You may use PushStyleVar() to alter this. + - 2022/07/08 (1.89) - inputs: removed io.NavInputs[] and ImGuiNavInput enum (following 1.87 changes). + - Official backends from 1.87+ -> no issue. + - Official backends from 1.60 to 1.86 -> will build and convert gamepad inputs, unless IMGUI_DISABLE_OBSOLETE_KEYIO is defined. Need updating! + - Custom backends not writing to io.NavInputs[] -> no issue. + - Custom backends writing to io.NavInputs[] -> will build and convert gamepad inputs, unless IMGUI_DISABLE_OBSOLETE_KEYIO is defined. Need fixing! + - TL;DR: Backends should call io.AddKeyEvent()/io.AddKeyAnalogEvent() with ImGuiKey_GamepadXXX values instead of filling io.NavInput[]. + - 2022/06/15 (1.88) - renamed IMGUI_DISABLE_METRICS_WINDOW to IMGUI_DISABLE_DEBUG_TOOLS for correctness. kept support for old define (will obsolete). + - 2022/05/03 (1.88) - backends: osx: removed ImGui_ImplOSX_HandleEvent() from backend API in favor of backend automatically handling event capture. All ImGui_ImplOSX_HandleEvent() calls should be removed as they are now unnecessary. + - 2022/04/05 (1.88) - inputs: renamed ImGuiKeyModFlags to ImGuiModFlags. Kept inline redirection enums (will obsolete). This was never used in public API functions but technically present in imgui.h and ImGuiIO. + - 2022/01/20 (1.87) - inputs: reworded gamepad IO. + - Backend writing to io.NavInputs[] -> backend should call io.AddKeyEvent()/io.AddKeyAnalogEvent() with ImGuiKey_GamepadXXX values. + - 2022/01/19 (1.87) - sliders, drags: removed support for legacy arithmetic operators (+,+-,*,/) when inputing text. This doesn't break any api/code but a feature that used to be accessible by end-users (which seemingly no one used). + - 2022/01/17 (1.87) - inputs: reworked mouse IO. + - Backend writing to io.MousePos -> backend should call io.AddMousePosEvent() + - Backend writing to io.MouseDown[] -> backend should call io.AddMouseButtonEvent() + - Backend writing to io.MouseWheel -> backend should call io.AddMouseWheelEvent() + - Backend writing to io.MouseHoveredViewport -> backend should call io.AddMouseViewportEvent() [Docking branch w/ multi-viewports only] + note: for all calls to IO new functions, the Dear ImGui context should be bound/current. + read https://github.com/ocornut/imgui/issues/4921 for details. + - 2022/01/10 (1.87) - inputs: reworked keyboard IO. Removed io.KeyMap[], io.KeysDown[] in favor of calling io.AddKeyEvent(). Removed GetKeyIndex(), now unecessary. All IsKeyXXX() functions now take ImGuiKey values. All features are still functional until IMGUI_DISABLE_OBSOLETE_KEYIO is defined. Read Changelog and Release Notes for details. + - IsKeyPressed(MY_NATIVE_KEY_XXX) -> use IsKeyPressed(ImGuiKey_XXX) + - IsKeyPressed(GetKeyIndex(ImGuiKey_XXX)) -> use IsKeyPressed(ImGuiKey_XXX) + - Backend writing to io.KeyMap[],io.KeysDown[] -> backend should call io.AddKeyEvent() (+ call io.SetKeyEventNativeData() if you want legacy user code to stil function with legacy key codes). + - Backend writing to io.KeyCtrl, io.KeyShift.. -> backend should call io.AddKeyEvent() with ImGuiMod_XXX values. *IF YOU PULLED CODE BETWEEN 2021/01/10 and 2021/01/27: We used to have a io.AddKeyModsEvent() function which was now replaced by io.AddKeyEvent() with ImGuiMod_XXX values.* + - one case won't work with backward compatibility: if your custom backend used ImGuiKey as mock native indices (e.g. "io.KeyMap[ImGuiKey_A] = ImGuiKey_A") because those values are now larger than the legacy KeyDown[] array. Will assert. + - inputs: added ImGuiKey_ModCtrl/ImGuiKey_ModShift/ImGuiKey_ModAlt/ImGuiKey_ModSuper values to submit keyboard modifiers using io.AddKeyEvent(), instead of writing directly to io.KeyCtrl, io.KeyShift, io.KeyAlt, io.KeySuper. + - 2022/01/05 (1.87) - inputs: renamed ImGuiKey_KeyPadEnter to ImGuiKey_KeypadEnter to align with new symbols. Kept redirection enum. + - 2022/01/05 (1.87) - removed io.ImeSetInputScreenPosFn() in favor of more flexible io.SetPlatformImeDataFn(). Removed 'void* io.ImeWindowHandle' in favor of writing to 'void* ImGuiViewport::PlatformHandleRaw'. + - 2022/01/01 (1.87) - commented out redirecting functions/enums names that were marked obsolete in 1.69, 1.70, 1.71, 1.72 (March-July 2019) + - ImGui::SetNextTreeNodeOpen() -> use ImGui::SetNextItemOpen() + - ImGui::GetContentRegionAvailWidth() -> use ImGui::GetContentRegionAvail().x + - ImGui::TreeAdvanceToLabelPos() -> use ImGui::SetCursorPosX(ImGui::GetCursorPosX() + ImGui::GetTreeNodeToLabelSpacing()); + - ImFontAtlas::CustomRect -> use ImFontAtlasCustomRect + - ImGuiColorEditFlags_RGB/HSV/HEX -> use ImGuiColorEditFlags_DisplayRGB/HSV/Hex - 2021/12/20 (1.86) - backends: removed obsolete Marmalade backend (imgui_impl_marmalade.cpp) + example. Find last supported version at https://github.com/ocornut/imgui/wiki/Bindings - 2021/11/04 (1.86) - removed CalcListClipping() function. Prefer using ImGuiListClipper which can return non-contiguous ranges. Please open an issue if you think you really need this function. - 2021/08/23 (1.85) - removed GetWindowContentRegionWidth() function. keep inline redirection helper. can use 'GetWindowContentRegionMax().x - GetWindowContentRegionMin().x' instead for generally 'GetContentRegionAvail().x' is more useful. @@ -730,7 +809,7 @@ CODE - How can I have widgets with an empty label? - How can I have multiple widgets with the same label? - How can I have multiple windows with the same label? - Q: How can I display an image? What is ImTextureID, how does it works? + Q: How can I display an image? What is ImTextureID, how does it work? Q: How can I use my own math types instead of ImVec2/ImVec4? Q: How can I interact with standard C++ types (such as std::string and std::vector)? Q: How can I display custom shapes? (using low-level ImDrawList API) @@ -789,7 +868,6 @@ CODE #include "imgui_internal.h" // System includes -#include // toupper #include // vsnprintf, sscanf, printf #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t @@ -836,7 +914,7 @@ CODE #if defined(_MSC_VER) && _MSC_VER >= 1922 // MSVC 2019 16.2 or later #pragma warning (disable: 5054) // operator '|': deprecated between enumerations of different types #endif -#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to an 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). #pragma warning (disable: 26495) // [Static Analyzer] Variable 'XXX' is uninitialized. Always initialize a member variable (type.6). #pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). #endif @@ -859,7 +937,7 @@ CODE #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #elif defined(__GNUC__) -// We disable -Wpragmas because GCC doesn't provide an has_warning equivalent and some forks/patches may not following the warning/version association. +// We disable -Wpragmas because GCC doesn't provide a has_warning equivalent and some forks/patches may not follow the warning/version association. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size @@ -883,7 +961,7 @@ static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time // Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by backend) static const float WINDOWS_HOVER_PADDING = 4.0f; // Extend outside window for hovering/resizing (maxxed with TouchPadding) and inside windows for borders. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. -static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 2.00f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. +static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 0.70f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. //------------------------------------------------------------------------- // [SECTION] FORWARD DECLARATIONS @@ -907,7 +985,7 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSetti // Platform Dependents default implementation for IO functions static const char* GetClipboardTextFn_DefaultImpl(void* user_data); static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); -static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); +static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data); namespace ImGui { @@ -939,13 +1017,17 @@ static void ErrorCheckEndFrameSanityChecks(); static void UpdateDebugToolItemPicker(); static void UpdateDebugToolStackQueries(); -// Misc -static void UpdateSettings(); +// Inputs +static void UpdateKeyboardInputs(); static void UpdateMouseInputs(); static void UpdateMouseWheel(); +static void UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt); + +// Misc +static void UpdateSettings(); static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); static void RenderWindowOuterBorders(ImGuiWindow* window); -static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); +static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, bool handle_borders_and_resize_grips, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); static void RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 col); static void RenderDimmedBackgrounds(); @@ -1027,12 +1109,12 @@ ImGuiStyle::ImGuiStyle() ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar - GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar + GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. - TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appears on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -1040,7 +1122,7 @@ ImGuiStyle::ImGuiStyle() DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. - AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering. + AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. CircleTessellationMaxError = 0.30f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. @@ -1082,7 +1164,7 @@ ImGuiIO::ImGuiIO() { // Most fields are initialized with zero memset(this, 0, sizeof(*this)); - IM_ASSERT(IM_ARRAYSIZE(ImGuiIO::MouseDown) == ImGuiMouseButton_COUNT && IM_ARRAYSIZE(ImGuiIO::MouseClicked) == ImGuiMouseButton_COUNT); // Our pre-C++11 IM_STATIC_ASSERT() macros triggers warning on modern compilers so we don't use it here. + IM_STATIC_ASSERT(IM_ARRAYSIZE(ImGuiIO::MouseDown) == ImGuiMouseButton_COUNT && IM_ARRAYSIZE(ImGuiIO::MouseClicked) == ImGuiMouseButton_COUNT); // Settings ConfigFlags = ImGuiConfigFlags_None; @@ -1094,10 +1176,14 @@ ImGuiIO::ImGuiIO() LogFilename = "imgui_log.txt"; MouseDoubleClickTime = 0.30f; MouseDoubleClickMaxDist = 6.0f; +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO for (int i = 0; i < ImGuiKey_COUNT; i++) KeyMap[i] = -1; +#endif KeyRepeatDelay = 0.275f; KeyRepeatRate = 0.050f; + HoverDelayNormal = 0.30f; + HoverDelayShort = 0.10f; UserData = NULL; Fonts = NULL; @@ -1113,7 +1199,10 @@ ImGuiIO::ImGuiIO() #else ConfigMacOSXBehaviors = false; #endif + ConfigInputTrickleEventQueue = true; ConfigInputTextCursorBlink = true; + ConfigInputTextEnterKeepActive = false; + ConfigDragClickToInputText = false; ConfigWindowsResizeFromEdges = true; ConfigWindowsMoveFromTitleBarOnly = false; ConfigMemoryCompactTimer = 60.0f; @@ -1124,38 +1213,48 @@ ImGuiIO::ImGuiIO() GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; ClipboardUserData = NULL; - ImeSetInputScreenPosFn = ImeSetInputScreenPosFn_DefaultImpl; - ImeWindowHandle = NULL; + SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl; // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX); MouseDragThreshold = 6.0f; for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; - for (int i = 0; i < IM_ARRAYSIZE(KeysDownDuration); i++) KeysDownDuration[i] = KeysDownDurationPrev[i] = -1.0f; - for (int i = 0; i < IM_ARRAYSIZE(NavInputsDownDuration); i++) NavInputsDownDuration[i] = -1.0f; + for (int i = 0; i < IM_ARRAYSIZE(KeysData); i++) { KeysData[i].DownDuration = KeysData[i].DownDurationPrev = -1.0f; } + AppAcceptingEvents = true; + BackendUsingLegacyKeyArrays = (ImS8)-1; + BackendUsingLegacyNavInputArray = true; // assume using legacy array until proven wrong } // Pass in translated ASCII characters for text input. // - with glfw you can get those from the callback set in glfwSetCharCallback() // - on Windows you can get those using ToAscii+keyboard state, or via the WM_CHAR message +// FIXME: Should in theory be called "AddCharacterEvent()" to be consistent with new API void ImGuiIO::AddInputCharacter(unsigned int c) { - if (c != 0) - InputQueueCharacters.push_back(c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + if (c == 0 || !AppAcceptingEvents) + return; + + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_Text; + e.Source = ImGuiInputSource_Keyboard; + e.Text.Char = c; + g.InputEventsQueue.push_back(e); } // UTF16 strings use surrogate pairs to encode codepoints >= 0x10000, so // we should save the high surrogate. void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) { - if (c == 0 && InputQueueSurrogate == 0) + if ((c == 0 && InputQueueSurrogate == 0) || !AppAcceptingEvents) return; if ((c & 0xFC00) == 0xD800) // High surrogate, must save { if (InputQueueSurrogate != 0) - InputQueueCharacters.push_back(IM_UNICODE_CODEPOINT_INVALID); + AddInputCharacter(IM_UNICODE_CODEPOINT_INVALID); InputQueueSurrogate = c; return; } @@ -1165,7 +1264,7 @@ void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) { if ((c & 0xFC00) != 0xDC00) // Invalid low surrogate { - InputQueueCharacters.push_back(IM_UNICODE_CODEPOINT_INVALID); + AddInputCharacter(IM_UNICODE_CODEPOINT_INVALID); } else { @@ -1178,41 +1277,230 @@ void ImGuiIO::AddInputCharacterUTF16(ImWchar16 c) InputQueueSurrogate = 0; } - InputQueueCharacters.push_back(cp); + AddInputCharacter((unsigned)cp); } void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { + if (!AppAcceptingEvents) + return; while (*utf8_chars != 0) { unsigned int c = 0; utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); if (c != 0) - InputQueueCharacters.push_back((ImWchar)c); + AddInputCharacter(c); } } +// FIXME: Perhaps we could clear queued events as well? void ImGuiIO::ClearInputCharacters() { InputQueueCharacters.resize(0); } +// FIXME: Perhaps we could clear queued events as well? void ImGuiIO::ClearInputKeys() { +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO memset(KeysDown, 0, sizeof(KeysDown)); - for (int n = 0; n < IM_ARRAYSIZE(KeysDownDuration); n++) - KeysDownDuration[n] = KeysDownDurationPrev[n] = -1.0f; +#endif + for (int n = 0; n < IM_ARRAYSIZE(KeysData); n++) + { + KeysData[n].Down = false; + KeysData[n].DownDuration = -1.0f; + KeysData[n].DownDurationPrev = -1.0f; + } KeyCtrl = KeyShift = KeyAlt = KeySuper = false; - KeyMods = KeyModsPrev = ImGuiKeyModFlags_None; - for (int n = 0; n < IM_ARRAYSIZE(NavInputsDownDuration); n++) - NavInputsDownDuration[n] = NavInputsDownDurationPrev[n] = -1.0f; + KeyMods = ImGuiMod_None; + MousePos = ImVec2(-FLT_MAX, -FLT_MAX); + for (int n = 0; n < IM_ARRAYSIZE(MouseDown); n++) + { + MouseDown[n] = false; + MouseDownDuration[n] = MouseDownDurationPrev[n] = -1.0f; + } + MouseWheel = MouseWheelH = 0.0f; +} + +static ImGuiInputEvent* FindLatestInputEvent(ImGuiInputEventType type, int arg = -1) +{ + ImGuiContext& g = *GImGui; + for (int n = g.InputEventsQueue.Size - 1; n >= 0; n--) + { + ImGuiInputEvent* e = &g.InputEventsQueue[n]; + if (e->Type != type) + continue; + if (type == ImGuiInputEventType_Key && e->Key.Key != arg) + continue; + if (type == ImGuiInputEventType_MouseButton && e->MouseButton.Button != arg) + continue; + return e; + } + return NULL; +} + +// Queue a new key down/up event. +// - ImGuiKey key: Translated key (as in, generally ImGuiKey_A matches the key end-user would use to emit an 'A' character) +// - bool down: Is the key down? use false to signify a key release. +// - float analog_value: 0.0f..1.0f +void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) +{ + //if (e->Down) { IMGUI_DEBUG_LOG_IO("AddKeyEvent() Key='%s' %d, NativeKeycode = %d, NativeScancode = %d\n", ImGui::GetKeyName(e->Key), e->Down, e->NativeKeycode, e->NativeScancode); } + if (key == ImGuiKey_None || !AppAcceptingEvents) + return; + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(ImGui::IsNamedKeyOrModKey(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. + IM_ASSERT(!ImGui::IsAliasKey(key)); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. + IM_ASSERT(key != ImGuiMod_Shortcut); // We could easily support the translation here but it seems saner to not accept it (TestEngine perform a translation itself) + + // Verify that backend isn't mixing up using new io.AddKeyEvent() api and old io.KeysDown[] + io.KeyMap[] data. +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + IM_ASSERT((BackendUsingLegacyKeyArrays == -1 || BackendUsingLegacyKeyArrays == 0) && "Backend needs to either only use io.AddKeyEvent(), either only fill legacy io.KeysDown[] + io.KeyMap[]. Not both!"); + if (BackendUsingLegacyKeyArrays == -1) + for (int n = ImGuiKey_NamedKey_BEGIN; n < ImGuiKey_NamedKey_END; n++) + IM_ASSERT(KeyMap[n] == -1 && "Backend needs to either only use io.AddKeyEvent(), either only fill legacy io.KeysDown[] + io.KeyMap[]. Not both!"); + BackendUsingLegacyKeyArrays = 0; +#endif + if (ImGui::IsGamepadKey(key)) + BackendUsingLegacyNavInputArray = false; + + // Filter duplicate (in particular: key mods and gamepad analog values are commonly spammed) + const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_Key, (int)key); + const ImGuiKeyData* key_data = ImGui::GetKeyData(key); + const bool latest_key_down = latest_event ? latest_event->Key.Down : key_data->Down; + const float latest_key_analog = latest_event ? latest_event->Key.AnalogValue : key_data->AnalogValue; + if (latest_key_down == down && latest_key_analog == analog_value) + return; + + // Add event + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_Key; + e.Source = ImGui::IsGamepadKey(key) ? ImGuiInputSource_Gamepad : ImGuiInputSource_Keyboard; + e.Key.Key = key; + e.Key.Down = down; + e.Key.AnalogValue = analog_value; + g.InputEventsQueue.push_back(e); +} + +void ImGuiIO::AddKeyEvent(ImGuiKey key, bool down) +{ + if (!AppAcceptingEvents) + return; + AddKeyAnalogEvent(key, down, down ? 1.0f : 0.0f); +} + +// [Optional] Call after AddKeyEvent(). +// Specify native keycode, scancode + Specify index for legacy <1.87 IsKeyXXX() functions with native indices. +// If you are writing a backend in 2022 or don't use IsKeyXXX() with native values that are not ImGuiKey values, you can avoid calling this. +void ImGuiIO::SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index) +{ + if (key == ImGuiKey_None) + return; + IM_ASSERT(ImGui::IsNamedKey(key)); // >= 512 + IM_ASSERT(native_legacy_index == -1 || ImGui::IsLegacyKey((ImGuiKey)native_legacy_index)); // >= 0 && <= 511 + IM_UNUSED(native_keycode); // Yet unused + IM_UNUSED(native_scancode); // Yet unused + + // Build native->imgui map so old user code can still call key functions with native 0..511 values. +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + const int legacy_key = (native_legacy_index != -1) ? native_legacy_index : native_keycode; + if (!ImGui::IsLegacyKey((ImGuiKey)legacy_key)) + return; + KeyMap[legacy_key] = key; + KeyMap[key] = legacy_key; +#else + IM_UNUSED(key); + IM_UNUSED(native_legacy_index); +#endif +} + +// Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. +void ImGuiIO::SetAppAcceptingEvents(bool accepting_events) +{ + AppAcceptingEvents = accepting_events; +} + +// Queue a mouse move event +void ImGuiIO::AddMousePosEvent(float x, float y) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + if (!AppAcceptingEvents) + return; + + // Apply same flooring as UpdateMouseInputs() + ImVec2 pos((x > -FLT_MAX) ? ImFloorSigned(x) : x, (y > -FLT_MAX) ? ImFloorSigned(y) : y); + + // Filter duplicate + const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_MousePos); + const ImVec2 latest_pos = latest_event ? ImVec2(latest_event->MousePos.PosX, latest_event->MousePos.PosY) : g.IO.MousePos; + if (latest_pos.x == pos.x && latest_pos.y == pos.y) + return; + + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_MousePos; + e.Source = ImGuiInputSource_Mouse; + e.MousePos.PosX = pos.x; + e.MousePos.PosY = pos.y; + g.InputEventsQueue.push_back(e); +} + +void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); + if (!AppAcceptingEvents) + return; + + // Filter duplicate + const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_MouseButton, (int)mouse_button); + const bool latest_button_down = latest_event ? latest_event->MouseButton.Down : g.IO.MouseDown[mouse_button]; + if (latest_button_down == down) + return; + + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_MouseButton; + e.Source = ImGuiInputSource_Mouse; + e.MouseButton.Button = mouse_button; + e.MouseButton.Down = down; + g.InputEventsQueue.push_back(e); +} + +// Queue a mouse wheel event (most mouse/API will only have a Y component) +void ImGuiIO::AddMouseWheelEvent(float wheel_x, float wheel_y) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + + // Filter duplicate (unlike most events, wheel values are relative and easy to filter) + if (!AppAcceptingEvents || (wheel_x == 0.0f && wheel_y == 0.0f)) + return; + + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_MouseWheel; + e.Source = ImGuiInputSource_Mouse; + e.MouseWheel.WheelX = wheel_x; + e.MouseWheel.WheelY = wheel_y; + g.InputEventsQueue.push_back(e); } void ImGuiIO::AddFocusEvent(bool focused) { - // We intentionally overwrite this and process in NewFrame(), in order to give a chance - // to multi-viewports backends to queue AddFocusEvent(false),AddFocusEvent(true) in same frame. - AppFocusLost = !focused; + ImGuiContext& g = *GImGui; + IM_ASSERT(&g.IO == this && "Can only add events to current context."); + + // Filter duplicate + const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_Focus); + const bool latest_focused = latest_event ? latest_event->AppFocused.Focused : !g.IO.AppFocusLost; + if (latest_focused == focused) + return; + + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_Focus; + e.AppFocused.Focused = focused; + g.InputEventsQueue.push_back(e); } //----------------------------------------------------------------------------- @@ -1343,14 +1631,14 @@ ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, int ImStricmp(const char* str1, const char* str2) { int d; - while ((d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; } + while ((d = ImToUpper(*str2) - ImToUpper(*str1)) == 0 && *str1) { str1++; str2++; } return d; } int ImStrnicmp(const char* str1, const char* str2, size_t count) { int d = 0; - while (count > 0 && (d = toupper(*str2) - toupper(*str1)) == 0 && *str1) { str1++; str2++; count--; } + while (count > 0 && (d = ImToUpper(*str2) - ImToUpper(*str1)) == 0 && *str1) { str1++; str2++; count--; } return d; } @@ -1417,14 +1705,14 @@ const char* ImStristr(const char* haystack, const char* haystack_end, const char if (!needle_end) needle_end = needle + strlen(needle); - const char un0 = (char)toupper(*needle); + const char un0 = (char)ImToUpper(*needle); while ((!haystack_end && *haystack) || (haystack_end && haystack < haystack_end)) { - if (toupper(*haystack) == un0) + if (ImToUpper(*haystack) == un0) { const char* b = needle + 1; for (const char* a = haystack + 1; b < needle_end; a++, b++) - if (toupper(*a) != toupper(*b)) + if (ImToUpper(*a) != ImToUpper(*b)) break; if (b == needle_end) return haystack; @@ -1468,8 +1756,12 @@ const char* ImStrSkipBlank(const char* str) // designed using two-passes worst case, which probably could be improved using the stbsp_vsprintfcb() function.) #ifdef IMGUI_USE_STB_SPRINTF #define STB_SPRINTF_IMPLEMENTATION +#ifdef IMGUI_STB_SPRINTF_FILENAME +#include IMGUI_STB_SPRINTF_FILENAME +#else #include "stb_sprintf.h" #endif +#endif #if defined(_MSC_VER) && !defined(vsnprintf) #define vsnprintf _vsnprintf @@ -1509,6 +1801,25 @@ int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) } #endif // #ifdef IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS +void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, const char* fmt, ...) +{ + ImGuiContext& g = *GImGui; + va_list args; + va_start(args, fmt); + int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); + *out_buf = g.TempBuffer.Data; + if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } + va_end(args); +} + +void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args) +{ + ImGuiContext& g = *GImGui; + int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); + *out_buf = g.TempBuffer.Data; + if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } +} + // CRC32 needs a 1KB lookup table (not cache friendly) // Although the code to generate the table is simple and shorter than the table itself, using a const table allows us to easily: // - avoid an unnecessary branch/memory tap, - keep the ImHashXXX functions usable by static constructors, - make it thread-safe. @@ -2070,18 +2381,15 @@ void ImGuiStorage::SetAllInt(int v) //----------------------------------------------------------------------------- // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]" -ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) +ImGuiTextFilter::ImGuiTextFilter(const char* default_filter) //-V1077 { + InputBuf[0] = 0; + CountGrep = 0; if (default_filter) { ImStrncpy(InputBuf, default_filter, IM_ARRAYSIZE(InputBuf)); Build(); } - else - { - InputBuf[0] = 0; - CountGrep = 0; - } } bool ImGuiTextFilter::Draw(const char* label, float width) @@ -2168,7 +2476,7 @@ bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const } //----------------------------------------------------------------------------- -// [SECTION] ImGuiTextBuffer +// [SECTION] ImGuiTextBuffer, ImGuiTextIndex //----------------------------------------------------------------------------- // On some platform vsnprintf() takes va_list by reference and modifies it. @@ -2236,6 +2544,20 @@ void ImGuiTextBuffer::appendfv(const char* fmt, va_list args) va_end(args_copy); } +void ImGuiTextIndex::append(const char* base, int old_size, int new_size) +{ + IM_ASSERT(old_size >= 0 && new_size >= old_size && new_size >= EndOffset); + if (old_size == new_size) + return; + if (EndOffset == 0 || base[EndOffset - 1] == '\n') + LineOffsets.push_back(EndOffset); + const char* base_end = base + new_size; + for (const char* p = base + old_size; (p = (const char*)memchr(p, '\n', base_end - p)) != 0; ) + if (++p < base_end) // Don't push a trailing offset on last \n + LineOffsets.push_back((int)(intptr_t)(p - base)); + EndOffset = ImMax(EndOffset, new_size); +} + //----------------------------------------------------------------------------- // [SECTION] ImGuiListClipper // This is currently not as flexible/powerful as it should be and really confusing/spaghetti, mostly because we changed @@ -2252,7 +2574,7 @@ static bool GetSkipItemForListClipping() #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // Legacy helper to calculate coarse clipping of large list of evenly sized items. -// This legacy API is not ideal because it assume we will return a single contiguous rectangle. +// This legacy API is not ideal because it assumes we will return a single contiguous rectangle. // Prefer using ImGuiListClipper which can returns non-contiguous ranges. void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end) { @@ -2367,13 +2689,11 @@ ImGuiListClipper::~ImGuiListClipper() End(); } -// Use case A: Begin() called from constructor with items_height<0, then called again from Step() in StepNo 1 -// Use case B: Begin() called from constructor with items_height>0 -// FIXME-LEGACY: Ideally we should remove the Begin/End functions but they are part of the legacy API we still support. This is why some of the code in Step() calling Begin() and reassign some fields, spaghetti style. void ImGuiListClipper::Begin(int items_count, float items_height) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + IMGUI_DEBUG_LOG_CLIPPER("Clipper: Begin(%d,%.2f) in '%s'\n", items_count, items_height, window->Name); if (ImGuiTable* table = g.CurrentTable) if (table->IsInsideRow) @@ -2396,15 +2716,15 @@ void ImGuiListClipper::Begin(int items_count, float items_height) void ImGuiListClipper::End() { - // In theory here we should assert that we are already at the right position, but it seems saner to just seek at the end and not assert/crash the user. ImGuiContext& g = *GImGui; - if (ItemsCount >= 0 && ItemsCount < INT_MAX && DisplayStart >= 0) - ImGuiListClipper_SeekCursorForItem(this, ItemsCount); - ItemsCount = -1; - - // Restore temporary buffer and fix back pointers which may be invalidated when nesting if (ImGuiListClipperData* data = (ImGuiListClipperData*)TempData) { + // In theory here we should assert that we are already at the right position, but it seems saner to just seek at the end and not assert/crash the user. + IMGUI_DEBUG_LOG_CLIPPER("Clipper: End() in '%s'\n", g.CurrentWindow->Name); + if (ItemsCount >= 0 && ItemsCount < INT_MAX && DisplayStart >= 0) + ImGuiListClipper_SeekCursorForItem(this, ItemsCount); + + // Restore temporary buffer and fix back pointers which may be invalidated when nesting IM_ASSERT(data->ListClipper == this); data->StepNo = data->Ranges.Size; if (--g.ClipperTempDataStacked > 0) @@ -2414,6 +2734,7 @@ void ImGuiListClipper::End() } TempData = NULL; } + ItemsCount = -1; } void ImGuiListClipper::ForceDisplayRangeByIndices(int item_min, int item_max) @@ -2425,29 +2746,29 @@ void ImGuiListClipper::ForceDisplayRangeByIndices(int item_min, int item_max) data->Ranges.push_back(ImGuiListClipperRange::FromIndices(item_min, item_max)); } -bool ImGuiListClipper::Step() +static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImGuiListClipperData* data = (ImGuiListClipperData*)TempData; + ImGuiListClipperData* data = (ImGuiListClipperData*)clipper->TempData; + IM_ASSERT(data != NULL && "Called ImGuiListClipper::Step() too many times, or before ImGuiListClipper::Begin() ?"); ImGuiTable* table = g.CurrentTable; if (table && table->IsInsideRow) ImGui::TableEndRow(table); // No items - if (ItemsCount == 0 || GetSkipItemForListClipping()) - return (void)End(), false; + if (clipper->ItemsCount == 0 || GetSkipItemForListClipping()) + return false; // While we are in frozen row state, keep displaying items one by one, unclipped // FIXME: Could be stored as a table-agnostic state. if (data->StepNo == 0 && table != NULL && !table->IsUnfrozenRows) { - DisplayStart = data->ItemsFrozen; - DisplayEnd = data->ItemsFrozen + 1; - if (DisplayStart >= ItemsCount) - return (void)End(), false; - data->ItemsFrozen++; + clipper->DisplayStart = data->ItemsFrozen; + clipper->DisplayEnd = ImMin(data->ItemsFrozen + 1, clipper->ItemsCount); + if (clipper->DisplayStart < clipper->DisplayEnd) + data->ItemsFrozen++; return true; } @@ -2455,15 +2776,13 @@ bool ImGuiListClipper::Step() bool calc_clipping = false; if (data->StepNo == 0) { - StartPosY = window->DC.CursorPos.y; - if (ItemsHeight <= 0.0f) + clipper->StartPosY = window->DC.CursorPos.y; + if (clipper->ItemsHeight <= 0.0f) { // Submit the first item (or range) so we can measure its height (generally the first range is 0..1) data->Ranges.push_front(ImGuiListClipperRange::FromIndices(data->ItemsFrozen, data->ItemsFrozen + 1)); - DisplayStart = ImMax(data->Ranges[0].Min, data->ItemsFrozen); - DisplayEnd = ImMin(data->Ranges[0].Max, ItemsCount); - if (DisplayStart == DisplayEnd) - return (void)End(), false; + clipper->DisplayStart = ImMax(data->Ranges[0].Min, data->ItemsFrozen); + clipper->DisplayEnd = ImMin(data->Ranges[0].Max, clipper->ItemsCount); data->StepNo = 1; return true; } @@ -2471,29 +2790,29 @@ bool ImGuiListClipper::Step() } // Step 1: Let the clipper infer height from first range - if (ItemsHeight <= 0.0f) + if (clipper->ItemsHeight <= 0.0f) { IM_ASSERT(data->StepNo == 1); if (table) - IM_ASSERT(table->RowPosY1 == StartPosY && table->RowPosY2 == window->DC.CursorPos.y); + IM_ASSERT(table->RowPosY1 == clipper->StartPosY && table->RowPosY2 == window->DC.CursorPos.y); - ItemsHeight = (window->DC.CursorPos.y - StartPosY) / (float)(DisplayEnd - DisplayStart); - bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision(StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y); + clipper->ItemsHeight = (window->DC.CursorPos.y - clipper->StartPosY) / (float)(clipper->DisplayEnd - clipper->DisplayStart); + bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision(clipper->StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y); if (affected_by_floating_point_precision) - ItemsHeight = window->DC.PrevLineSize.y + g.Style.ItemSpacing.y; // FIXME: Technically wouldn't allow multi-line entries. + clipper->ItemsHeight = window->DC.PrevLineSize.y + g.Style.ItemSpacing.y; // FIXME: Technically wouldn't allow multi-line entries. - IM_ASSERT(ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); + IM_ASSERT(clipper->ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); calc_clipping = true; // If item height had to be calculated, calculate clipping afterwards. } // Step 0 or 1: Calculate the actual ranges of visible elements. - const int already_submitted = DisplayEnd; + const int already_submitted = clipper->DisplayEnd; if (calc_clipping) { if (g.LogEnabled) { // If logging is active, do not perform any clipping - data->Ranges.push_back(ImGuiListClipperRange::FromIndices(0, ItemsCount)); + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(0, clipper->ItemsCount)); } else { @@ -2502,7 +2821,7 @@ bool ImGuiListClipper::Step() if (is_nav_request) data->Ranges.push_back(ImGuiListClipperRange::FromPositions(g.NavScoringNoClipRect.Min.y, g.NavScoringNoClipRect.Max.y, 0, 0)); if (is_nav_request && (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && g.NavTabbingDir == -1) - data->Ranges.push_back(ImGuiListClipperRange::FromIndices(ItemsCount - 1, ItemsCount)); + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(clipper->ItemsCount - 1, clipper->ItemsCount)); // Add focused/active item ImRect nav_rect_abs = ImGui::WindowRectRelToAbs(window, window->NavRectRel[0]); @@ -2522,10 +2841,10 @@ bool ImGuiListClipper::Step() for (int i = 0; i < data->Ranges.Size; i++) if (data->Ranges[i].PosToIndexConvert) { - int m1 = (int)(((double)data->Ranges[i].Min - window->DC.CursorPos.y - data->LossynessOffset) / ItemsHeight); - int m2 = (int)((((double)data->Ranges[i].Max - window->DC.CursorPos.y - data->LossynessOffset) / ItemsHeight) + 0.999999f); - data->Ranges[i].Min = ImClamp(already_submitted + m1 + data->Ranges[i].PosToIndexOffsetMin, already_submitted, ItemsCount - 1); - data->Ranges[i].Max = ImClamp(already_submitted + m2 + data->Ranges[i].PosToIndexOffsetMax, data->Ranges[i].Min + 1, ItemsCount); + int m1 = (int)(((double)data->Ranges[i].Min - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight); + int m2 = (int)((((double)data->Ranges[i].Max - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight) + 0.999999f); + data->Ranges[i].Min = ImClamp(already_submitted + m1 + data->Ranges[i].PosToIndexOffsetMin, already_submitted, clipper->ItemsCount - 1); + data->Ranges[i].Max = ImClamp(already_submitted + m2 + data->Ranges[i].PosToIndexOffsetMax, data->Ranges[i].Min + 1, clipper->ItemsCount); data->Ranges[i].PosToIndexConvert = false; } ImGuiListClipper_SortAndFuseRanges(data->Ranges, data->StepNo); @@ -2534,23 +2853,45 @@ bool ImGuiListClipper::Step() // Step 0+ (if item height is given in advance) or 1+: Display the next range in line. if (data->StepNo < data->Ranges.Size) { - DisplayStart = ImMax(data->Ranges[data->StepNo].Min, already_submitted); - DisplayEnd = ImMin(data->Ranges[data->StepNo].Max, ItemsCount); - if (DisplayStart > already_submitted) //-V1051 - ImGuiListClipper_SeekCursorForItem(this, DisplayStart); + clipper->DisplayStart = ImMax(data->Ranges[data->StepNo].Min, already_submitted); + clipper->DisplayEnd = ImMin(data->Ranges[data->StepNo].Max, clipper->ItemsCount); + if (clipper->DisplayStart > already_submitted) //-V1051 + ImGuiListClipper_SeekCursorForItem(clipper, clipper->DisplayStart); data->StepNo++; return true; } // After the last step: Let the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), // Advance the cursor to the end of the list and then returns 'false' to end the loop. - if (ItemsCount < INT_MAX) - ImGuiListClipper_SeekCursorForItem(this, ItemsCount); - ItemsCount = -1; + if (clipper->ItemsCount < INT_MAX) + ImGuiListClipper_SeekCursorForItem(clipper, clipper->ItemsCount); return false; } +bool ImGuiListClipper::Step() +{ + ImGuiContext& g = *GImGui; + bool need_items_height = (ItemsHeight <= 0.0f); + bool ret = ImGuiListClipper_StepInternal(this); + if (ret && (DisplayStart == DisplayEnd)) + ret = false; + if (g.CurrentTable && g.CurrentTable->IsUnfrozenRows == false) + IMGUI_DEBUG_LOG_CLIPPER("Clipper: Step(): inside frozen table row.\n"); + if (need_items_height && ItemsHeight > 0.0f) + IMGUI_DEBUG_LOG_CLIPPER("Clipper: Step(): computed ItemsHeight: %.2f.\n", ItemsHeight); + if (ret) + { + IMGUI_DEBUG_LOG_CLIPPER("Clipper: Step(): display %d to %d.\n", DisplayStart, DisplayEnd); + } + else + { + IMGUI_DEBUG_LOG_CLIPPER("Clipper: Step(): End.\n"); + End(); + } + return ret; +} + //----------------------------------------------------------------------------- // [SECTION] STYLING //----------------------------------------------------------------------------- @@ -2617,6 +2958,11 @@ void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) void ImGui::PopStyleColor(int count) { ImGuiContext& g = *GImGui; + if (g.ColorStack.Size < count) + { + IM_ASSERT_USER_ERROR(g.ColorStack.Size > count, "Calling PopStyleColor() too many times: stack underflow."); + count = g.ColorStack.Size; + } while (count > 0) { ImGuiColorMod& backup = g.ColorStack.back(); @@ -2701,6 +3047,11 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) void ImGui::PopStyleVar(int count) { ImGuiContext& g = *GImGui; + if (g.StyleVarStack.Size < count) + { + IM_ASSERT_USER_ERROR(g.StyleVarStack.Size > count, "Calling PopStyleVar() too many times: stack underflow."); + count = g.StyleVarStack.Size; + } while (count > 0) { // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. @@ -3024,111 +3375,312 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl } } +void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); + for (int n = 0; n < g.Viewports.Size; n++) + { + ImGuiViewportP* viewport = g.Viewports[n]; + ImDrawList* draw_list = GetForegroundDrawList(viewport); + ImFontAtlas* font_atlas = draw_list->_Data->Font->ContainerAtlas; + ImVec2 offset, size, uv[4]; + if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) + { + const ImVec2 pos = base_pos - offset; + const float scale = base_scale; + ImTextureID tex_id = font_atlas->TexID; + draw_list->PushTextureID(tex_id); + draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill); + draw_list->PopTextureID(); + } + } +} + //----------------------------------------------------------------------------- -// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +// [SECTION] INITIALIZATION, SHUTDOWN //----------------------------------------------------------------------------- -// ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods -ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst(NULL) +// Internal state access - if you want to share Dear ImGui state between modules (e.g. DLL) or allocate it yourself +// Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module +ImGuiContext* ImGui::GetCurrentContext() { - memset(this, 0, sizeof(*this)); - Name = ImStrdup(name); - NameBufLen = (int)strlen(name) + 1; - ID = ImHashStr(name); - IDStack.push_back(ID); - MoveId = GetID("#MOVE"); - ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); - ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); - AutoFitFramesX = AutoFitFramesY = -1; - AutoPosLastDirection = ImGuiDir_None; - SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; - SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); - LastFrameActive = -1; - LastTimeActive = -1.0f; - FontWindowScale = 1.0f; - SettingsOffset = -1; - DrawList = &DrawListInst; - DrawList->_Data = &context->DrawListSharedData; - DrawList->_OwnerName = Name; + return GImGui; } -ImGuiWindow::~ImGuiWindow() +void ImGui::SetCurrentContext(ImGuiContext* ctx) { - IM_ASSERT(DrawList == &DrawListInst); - IM_DELETE(Name); - ColumnsStorage.clear_destruct(); +#ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC + IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. +#else + GImGui = ctx; +#endif } -ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) +void ImGui::SetAllocatorFunctions(ImGuiMemAllocFunc alloc_func, ImGuiMemFreeFunc free_func, void* user_data) { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - ImGui::KeepAliveID(id); - ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); - return id; + GImAllocatorAllocFunc = alloc_func; + GImAllocatorFreeFunc = free_func; + GImAllocatorUserData = user_data; } -ImGuiID ImGuiWindow::GetID(const void* ptr) +// This is provided to facilitate copying allocators from one static/DLL boundary to another (e.g. retrieve default allocator of your executable address space) +void ImGui::GetAllocatorFunctions(ImGuiMemAllocFunc* p_alloc_func, ImGuiMemFreeFunc* p_free_func, void** p_user_data) { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); - ImGui::KeepAliveID(id); - ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); - return id; + *p_alloc_func = GImAllocatorAllocFunc; + *p_free_func = GImAllocatorFreeFunc; + *p_user_data = GImAllocatorUserData; } -ImGuiID ImGuiWindow::GetID(int n) +ImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas) { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&n, sizeof(n), seed); - ImGui::KeepAliveID(id); - ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); - return id; + ImGuiContext* prev_ctx = GetCurrentContext(); + ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas); + SetCurrentContext(ctx); + Initialize(); + if (prev_ctx != NULL) + SetCurrentContext(prev_ctx); // Restore previous context if any, else keep new one. + return ctx; } -ImGuiID ImGuiWindow::GetIDNoKeepAlive(const char* str, const char* str_end) +void ImGui::DestroyContext(ImGuiContext* ctx) { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); - return id; + ImGuiContext* prev_ctx = GetCurrentContext(); + if (ctx == NULL) //-V1051 + ctx = prev_ctx; + SetCurrentContext(ctx); + Shutdown(); + SetCurrentContext((prev_ctx != ctx) ? prev_ctx : NULL); + IM_DELETE(ctx); } -ImGuiID ImGuiWindow::GetIDNoKeepAlive(const void* ptr) +// IMPORTANT: ###xxx suffixes must be same in ALL languages +static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); - ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); - return id; -} + { ImGuiLocKey_TableSizeOne, "Size column to fit###SizeOne" }, + { ImGuiLocKey_TableSizeAllFit, "Size all columns to fit###SizeAll" }, + { ImGuiLocKey_TableSizeAllDefault, "Size all columns to default###SizeAll" }, + { ImGuiLocKey_TableResetOrder, "Reset order###ResetOrder" }, + { ImGuiLocKey_WindowingMainMenuBar, "(Main menu bar)" }, + { ImGuiLocKey_WindowingPopup, "(Popup)" }, + { ImGuiLocKey_WindowingUntitled, "(Untitled)" }, +}; -ImGuiID ImGuiWindow::GetIDNoKeepAlive(int n) +void ImGui::Initialize() { - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&n, sizeof(n), seed); ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); - return id; + IM_ASSERT(!g.Initialized && !g.SettingsLoaded); + + // Add .ini handle for ImGuiWindow and ImGuiTable types + { + ImGuiSettingsHandler ini_handler; + ini_handler.TypeName = "Window"; + ini_handler.TypeHash = ImHashStr("Window"); + ini_handler.ClearAllFn = WindowSettingsHandler_ClearAll; + ini_handler.ReadOpenFn = WindowSettingsHandler_ReadOpen; + ini_handler.ReadLineFn = WindowSettingsHandler_ReadLine; + ini_handler.ApplyAllFn = WindowSettingsHandler_ApplyAll; + ini_handler.WriteAllFn = WindowSettingsHandler_WriteAll; + AddSettingsHandler(&ini_handler); + } + TableSettingsAddSettingsHandler(); + + // Setup default localization table + LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS)); + + // Create default viewport + ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); + g.Viewports.push_back(viewport); + g.TempBuffer.resize(1024 * 3 + 1, 0); + +#ifdef IMGUI_HAS_DOCK +#endif + + g.Initialized = true; } -// This is only used in rare/specific situations to manufacture an ID out of nowhere. -ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) +// This function is merely here to free heap allocations. +void ImGui::Shutdown() { - ImGuiID seed = IDStack.back(); + // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) + ImGuiContext& g = *GImGui; + if (g.IO.Fonts && g.FontAtlasOwnedByContext) + { + g.IO.Fonts->Locked = false; + IM_DELETE(g.IO.Fonts); + } + g.IO.Fonts = NULL; + g.DrawListSharedData.TempBuffer.clear(); + + // Cleanup of other data are conditional on actually having initialized Dear ImGui. + if (!g.Initialized) + return; + + // Save settings (unless we haven't attempted to load them: CreateContext/DestroyContext without a call to NewFrame shouldn't save an empty file) + if (g.SettingsLoaded && g.IO.IniFilename != NULL) + SaveIniSettingsToDisk(g.IO.IniFilename); + + CallContextHooks(&g, ImGuiContextHookType_Shutdown); + + // Clear everything else + g.Windows.clear_delete(); + g.WindowsFocusOrder.clear(); + g.WindowsTempSortBuffer.clear(); + g.CurrentWindow = NULL; + g.CurrentWindowStack.clear(); + g.WindowsById.Clear(); + g.NavWindow = NULL; + g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL; + g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; + g.MovingWindow = NULL; + + g.KeysRoutingTable.Clear(); + + g.ColorStack.clear(); + g.StyleVarStack.clear(); + g.FontStack.clear(); + g.OpenPopupStack.clear(); + g.BeginPopupStack.clear(); + + g.Viewports.clear_delete(); + + g.TabBars.Clear(); + g.CurrentTabBarStack.clear(); + g.ShrinkWidthBuffer.clear(); + + g.ClipperTempData.clear_destruct(); + + g.Tables.Clear(); + g.TablesTempData.clear_destruct(); + g.DrawChannelsTempMergeBuffer.clear(); + + g.ClipboardHandlerData.clear(); + g.MenusIdSubmittedThisFrame.clear(); + g.InputTextState.ClearFreeMemory(); + + g.SettingsWindows.clear(); + g.SettingsHandlers.clear(); + + if (g.LogFile) + { +#ifndef IMGUI_DISABLE_TTY_FUNCTIONS + if (g.LogFile != stdout) +#endif + ImFileClose(g.LogFile); + g.LogFile = NULL; + } + g.LogBuffer.clear(); + g.DebugLogBuf.clear(); + g.DebugLogIndex.clear(); + + g.Initialized = false; +} + +// No specific ordering/dependency support, will see as needed +ImGuiID ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) +{ + ImGuiContext& g = *ctx; + IM_ASSERT(hook->Callback != NULL && hook->HookId == 0 && hook->Type != ImGuiContextHookType_PendingRemoval_); + g.Hooks.push_back(*hook); + g.Hooks.back().HookId = ++g.HookIdNext; + return g.HookIdNext; +} + +// Deferred removal, avoiding issue with changing vector while iterating it +void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) +{ + ImGuiContext& g = *ctx; + IM_ASSERT(hook_id != 0); + for (int n = 0; n < g.Hooks.Size; n++) + if (g.Hooks[n].HookId == hook_id) + g.Hooks[n].Type = ImGuiContextHookType_PendingRemoval_; +} + +// Call context hooks (used by e.g. test engine) +// We assume a small number of hooks so all stored in same array +void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type) +{ + ImGuiContext& g = *ctx; + for (int n = 0; n < g.Hooks.Size; n++) + if (g.Hooks[n].Type == hook_type) + g.Hooks[n].Callback(&g, &g.Hooks[n]); +} + + +//----------------------------------------------------------------------------- +// [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +//----------------------------------------------------------------------------- + +// ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods +ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst(NULL) +{ + memset(this, 0, sizeof(*this)); + Name = ImStrdup(name); + NameBufLen = (int)strlen(name) + 1; + ID = ImHashStr(name); + IDStack.push_back(ID); + MoveId = GetID("#MOVE"); + ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); + ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); + AutoFitFramesX = AutoFitFramesY = -1; + AutoPosLastDirection = ImGuiDir_None; + SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; + SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); + LastFrameActive = -1; + LastTimeActive = -1.0f; + FontWindowScale = 1.0f; + SettingsOffset = -1; + DrawList = &DrawListInst; + DrawList->_Data = &context->DrawListSharedData; + DrawList->_OwnerName = Name; +} + +ImGuiWindow::~ImGuiWindow() +{ + IM_ASSERT(DrawList == &DrawListInst); + IM_DELETE(Name); + ColumnsStorage.clear_destruct(); +} + +ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); + ImGuiContext& g = *GImGui; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); + return id; +} + +ImGuiID ImGuiWindow::GetID(const void* ptr) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); + ImGuiContext& g = *GImGui; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); + return id; +} + +ImGuiID ImGuiWindow::GetID(int n) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&n, sizeof(n), seed); + ImGuiContext& g = *GImGui; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); + return id; +} + +// This is only used in rare/specific situations to manufacture an ID out of nowhere. +ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) +{ + ImGuiID seed = IDStack.back(); ImRect r_rel = ImGui::WindowRectAbsToRel(this, r_abs); ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); - ImGui::KeepAliveID(id); return id; } @@ -3178,9 +3730,21 @@ void ImGui::GcAwakeTransientWindowBuffers(ImGuiWindow* window) void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) { ImGuiContext& g = *GImGui; + + // While most behaved code would make an effort to not steal active id during window move/drag operations, + // we at least need to be resilient to it. Cancelling the move is rather aggressive and users of 'master' branch + // may prefer the weird ill-defined half working situation ('docking' did assert), so may need to rework that. + if (g.MovingWindow != NULL && g.ActiveId == g.MovingWindow->MoveId) + { + IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() cancel MovingWindow\n"); + g.MovingWindow = NULL; + } + + // Set active id g.ActiveIdIsJustActivated = (g.ActiveId != id); if (g.ActiveIdIsJustActivated) { + IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() old:0x%08X (window \"%s\") -> new:0x%08X (window \"%s\")\n", g.ActiveId, g.ActiveIdWindow ? g.ActiveIdWindow->Name : "", id, window ? window->Name : ""); g.ActiveIdTimer = 0.0f; g.ActiveIdHasBeenPressedBefore = false; g.ActiveIdHasBeenEditedBefore = false; @@ -3199,15 +3763,16 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) if (id) { g.ActiveIdIsAlive = id; - g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustMovedToId == id) ? ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustMovedToId == id) ? (ImGuiInputSource)ImGuiInputSource_Nav : ImGuiInputSource_Mouse; } // Clear declaration of inputs claimed by the widget // (Please note that this is WIP and not all keys/inputs are thoroughly declared by all widgets yet) - g.ActiveIdUsingMouseWheel = false; g.ActiveIdUsingNavDirMask = 0x00; + g.ActiveIdUsingAllKeyboardKeys = false; +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO g.ActiveIdUsingNavInputMask = 0x00; - g.ActiveIdUsingKeyInputMask = 0x00; +#endif } void ImGui::ClearActiveID() @@ -3220,7 +3785,6 @@ void ImGui::SetHoveredID(ImGuiID id) ImGuiContext& g = *GImGui; g.HoveredId = id; g.HoveredIdAllowOverlap = false; - g.HoveredIdUsingMouseWheel = false; if (id != 0 && g.HoveredIdPreviousFrame != id) g.HoveredIdTimer = g.HoveredIdNotActiveTimer = 0.0f; } @@ -3231,6 +3795,8 @@ ImGuiID ImGui::GetHoveredID() return g.HoveredId ? g.HoveredId : g.HoveredIdPreviousFrame; } +// This is called by ItemAdd(). +// Code not using ItemAdd() may need to call this manually otherwise ActiveId will be cleared. In IMGUI_VERSION_NUM < 18717 this was called by GetID(). void ImGui::KeepAliveID(ImGuiID id) { ImGuiContext& g = *GImGui; @@ -3243,7 +3809,7 @@ void ImGui::KeepAliveID(ImGuiID id) void ImGui::MarkItemEdited(ImGuiID id) { // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). - // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need need to fill the data. + // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need to fill the data. ImGuiContext& g = *GImGui; IM_ASSERT(g.ActiveId == id || g.ActiveId == 0 || g.DragDropActive); IM_UNUSED(id); // Avoid unused variable warnings when asserts are compiled out. @@ -3263,11 +3829,17 @@ static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFla if (focused_root_window->WasActive && focused_root_window != window->RootWindow) { // For the purpose of those flags we differentiate "standard popup" from "modal popup" - // NB: The order of those two tests is important because Modal windows are also Popups. + // NB: The 'else' is important because Modal windows are also Popups. + bool want_inhibit = false; if (focused_root_window->Flags & ImGuiWindowFlags_Modal) - return false; - if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup)) - return false; + want_inhibit = true; + else if ((focused_root_window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + want_inhibit = true; + + // Inhibit hover unless the window is within the stack of our modal/popup + if (want_inhibit) + if (!ImGui::IsWindowWithinBeginStackOf(window->RootWindow, focused_root_window)) + return false; } return true; } @@ -3279,7 +3851,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (g.NavDisableMouseHover && !g.NavDisableHighlight) + if (g.NavDisableMouseHover && !g.NavDisableHighlight && !(flags & ImGuiHoveredFlags_NoNavOverride)) { if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; @@ -3294,6 +3866,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function + // Done with rectangle culling so we can perform heavier checks now // Test if we are hovering the right window (our window could be behind another window) // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) // [2017/10/16] Reverted commit 344d48be3 and testing RootWindow instead. I believe it is correct to NOT test for RootWindow but this leaves us unable @@ -3310,7 +3883,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // Test if interactions on this window are blocked by an active popup or modal. // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. - if (!IsWindowContentHoverable(window, flags)) + if (!IsWindowContentHoverable(window, flags) && !(g.LastItemData.InFlags & ImGuiItemFlags_NoWindowHoverableCheck)) return false; // Test if the item is disabled @@ -3323,6 +3896,24 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; } + // Handle hover delay + // (some ideas: https://www.nngroup.com/articles/timing-exposing-content) + float delay; + if (flags & ImGuiHoveredFlags_DelayNormal) + delay = g.IO.HoverDelayNormal; + else if (flags & ImGuiHoveredFlags_DelayShort) + delay = g.IO.HoverDelayShort; + else + delay = 0.0f; + if (delay > 0.0f) + { + ImGuiID hover_delay_id = (g.LastItemData.ID != 0) ? g.LastItemData.ID : window->GetIDFromRectangle(g.LastItemData.Rect); + if ((flags & ImGuiHoveredFlags_NoSharedDelay) && (g.HoverDelayIdPreviousFrame != hover_delay_id)) + g.HoverDelayTimer = 0.0f; + g.HoverDelayId = hover_delay_id; + return g.HoverDelayTimer >= delay; + } + return true; } @@ -3340,9 +3931,10 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) return false; if (!IsMouseHoveringRect(bb.Min, bb.Max)) return false; - if (g.NavDisableMouseHover) - return false; - if (!IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) + + // Done with rectangle culling so we can perform heavier checks now. + ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); + if (!(item_flags & ImGuiItemFlags_NoWindowHoverableCheck) && !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) { g.HoveredIdDisabled = true; return false; @@ -3354,7 +3946,6 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) SetHoveredID(id); // When disabled we'll return false but still set HoveredId - ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); if (item_flags & ImGuiItemFlags_Disabled) { // Release active id if turning disabled @@ -3369,17 +3960,20 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) // [DEBUG] Item Picker tool! // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered - // items if we perform the test in ItemAdd(), but that would incur a small runtime cost. - // #define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX in imconfig.h if you want this check to also be performed in ItemAdd(). + // items if we performed the test in ItemAdd(), but that would incur a small runtime cost. if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); if (g.DebugItemPickerBreakId == id) IM_DEBUG_BREAK(); } + if (g.NavDisableMouseHover) + return false; + return true; } +// FIXME: This is inlined/duplicated in ItemAdd() bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) { ImGuiContext& g = *GImGui; @@ -3461,143 +4055,63 @@ const char* ImGui::GetVersion() return IMGUI_VERSION; } -// Internal state access - if you want to share Dear ImGui state between modules (e.g. DLL) or allocate it yourself -// Note that we still point to some static data and members (such as GFontAtlas), so the state instance you end up using will point to the static data within its module -ImGuiContext* ImGui::GetCurrentContext() +ImGuiIO& ImGui::GetIO() { - return GImGui; + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); + return GImGui->IO; } -void ImGui::SetCurrentContext(ImGuiContext* ctx) +// Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() +ImDrawData* ImGui::GetDrawData() { -#ifdef IMGUI_SET_CURRENT_CONTEXT_FUNC - IMGUI_SET_CURRENT_CONTEXT_FUNC(ctx); // For custom thread-based hackery you may want to have control over this. -#else - GImGui = ctx; -#endif + ImGuiContext& g = *GImGui; + ImGuiViewportP* viewport = g.Viewports[0]; + return viewport->DrawDataP.Valid ? &viewport->DrawDataP : NULL; } -void ImGui::SetAllocatorFunctions(ImGuiMemAllocFunc alloc_func, ImGuiMemFreeFunc free_func, void* user_data) +double ImGui::GetTime() { - GImAllocatorAllocFunc = alloc_func; - GImAllocatorFreeFunc = free_func; - GImAllocatorUserData = user_data; + return GImGui->Time; } -// This is provided to facilitate copying allocators from one static/DLL boundary to another (e.g. retrieve default allocator of your executable address space) -void ImGui::GetAllocatorFunctions(ImGuiMemAllocFunc* p_alloc_func, ImGuiMemFreeFunc* p_free_func, void** p_user_data) +int ImGui::GetFrameCount() { - *p_alloc_func = GImAllocatorAllocFunc; - *p_free_func = GImAllocatorFreeFunc; - *p_user_data = GImAllocatorUserData; + return GImGui->FrameCount; } -ImGuiContext* ImGui::CreateContext(ImFontAtlas* shared_font_atlas) +static ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) { - ImGuiContext* ctx = IM_NEW(ImGuiContext)(shared_font_atlas); - if (GImGui == NULL) - SetCurrentContext(ctx); - Initialize(ctx); - return ctx; + // Create the draw list on demand, because they are not frequently used for all viewports + ImGuiContext& g = *GImGui; + IM_ASSERT(drawlist_no < IM_ARRAYSIZE(viewport->DrawLists)); + ImDrawList* draw_list = viewport->DrawLists[drawlist_no]; + if (draw_list == NULL) + { + draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData); + draw_list->_OwnerName = drawlist_name; + viewport->DrawLists[drawlist_no] = draw_list; + } + + // Our ImDrawList system requires that there is always a command + if (viewport->DrawListsLastFrame[drawlist_no] != g.FrameCount) + { + draw_list->_ResetForNewFrame(); + draw_list->PushTextureID(g.IO.Fonts->TexID); + draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false); + viewport->DrawListsLastFrame[drawlist_no] = g.FrameCount; + } + return draw_list; } -void ImGui::DestroyContext(ImGuiContext* ctx) +ImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport) { - if (ctx == NULL) - ctx = GImGui; - Shutdown(ctx); - if (GImGui == ctx) - SetCurrentContext(NULL); - IM_DELETE(ctx); + return GetViewportDrawList((ImGuiViewportP*)viewport, 0, "##Background"); } -// No specific ordering/dependency support, will see as needed -ImGuiID ImGui::AddContextHook(ImGuiContext* ctx, const ImGuiContextHook* hook) +ImDrawList* ImGui::GetBackgroundDrawList() { - ImGuiContext& g = *ctx; - IM_ASSERT(hook->Callback != NULL && hook->HookId == 0 && hook->Type != ImGuiContextHookType_PendingRemoval_); - g.Hooks.push_back(*hook); - g.Hooks.back().HookId = ++g.HookIdNext; - return g.HookIdNext; -} - -// Deferred removal, avoiding issue with changing vector while iterating it -void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) -{ - ImGuiContext& g = *ctx; - IM_ASSERT(hook_id != 0); - for (int n = 0; n < g.Hooks.Size; n++) - if (g.Hooks[n].HookId == hook_id) - g.Hooks[n].Type = ImGuiContextHookType_PendingRemoval_; -} - -// Call context hooks (used by e.g. test engine) -// We assume a small number of hooks so all stored in same array -void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type) -{ - ImGuiContext& g = *ctx; - for (int n = 0; n < g.Hooks.Size; n++) - if (g.Hooks[n].Type == hook_type) - g.Hooks[n].Callback(&g, &g.Hooks[n]); -} - -ImGuiIO& ImGui::GetIO() -{ - IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); - return GImGui->IO; -} - -// Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() -ImDrawData* ImGui::GetDrawData() -{ - ImGuiContext& g = *GImGui; - ImGuiViewportP* viewport = g.Viewports[0]; - return viewport->DrawDataP.Valid ? &viewport->DrawDataP : NULL; -} - -double ImGui::GetTime() -{ - return GImGui->Time; -} - -int ImGui::GetFrameCount() -{ - return GImGui->FrameCount; -} - -static ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) -{ - // Create the draw list on demand, because they are not frequently used for all viewports - ImGuiContext& g = *GImGui; - IM_ASSERT(drawlist_no < IM_ARRAYSIZE(viewport->DrawLists)); - ImDrawList* draw_list = viewport->DrawLists[drawlist_no]; - if (draw_list == NULL) - { - draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData); - draw_list->_OwnerName = drawlist_name; - viewport->DrawLists[drawlist_no] = draw_list; - } - - // Our ImDrawList system requires that there is always a command - if (viewport->DrawListsLastFrame[drawlist_no] != g.FrameCount) - { - draw_list->_ResetForNewFrame(); - draw_list->PushTextureID(g.IO.Fonts->TexID); - draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false); - viewport->DrawListsLastFrame[drawlist_no] = g.FrameCount; - } - return draw_list; -} - -ImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport) -{ - return GetViewportDrawList((ImGuiViewportP*)viewport, 0, "##Background"); -} - -ImDrawList* ImGui::GetBackgroundDrawList() -{ - ImGuiContext& g = *GImGui; - return GetBackgroundDrawList(g.Viewports[0]); + ImGuiContext& g = *GImGui; + return GetBackgroundDrawList(g.Viewports[0]); } ImDrawList* ImGui::GetForegroundDrawList(ImGuiViewport* viewport) @@ -3627,7 +4141,7 @@ void ImGui::StartMouseMovingWindow(ImGuiWindow* window) g.NavDisableHighlight = true; g.ActiveIdClickOffset = g.IO.MouseClickedPos[0] - window->RootWindow->Pos; g.ActiveIdNoClearOnFocusLoss = true; - SetActiveIdUsingNavAndKeys(); + SetActiveIdUsingAllKeyboardKeys(); bool can_move_window = true; if ((window->Flags & ImGuiWindowFlags_NoMove) || (window->RootWindow->Flags & ImGuiWindowFlags_NoMove)) @@ -3654,11 +4168,7 @@ void ImGui::UpdateMouseMovingWindowNewFrame() if (g.IO.MouseDown[0] && IsMousePosValid(&g.IO.MousePos)) { ImVec2 pos = g.IO.MousePos - g.ActiveIdClickOffset; - if (moving_window->Pos.x != pos.x || moving_window->Pos.y != pos.y) - { - MarkIniSettingsDirty(moving_window); - SetWindowPos(moving_window, pos, ImGuiCond_Always); - } + SetWindowPos(moving_window, pos, ImGuiCond_Always); FocusWindow(g.MovingWindow); } else @@ -3738,164 +4248,6 @@ static bool IsWindowActiveAndVisible(ImGuiWindow* window) return (window->Active) && (!window->Hidden); } -static void ImGui::UpdateMouseInputs() -{ - ImGuiContext& g = *GImGui; - - // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) - if (IsMousePosValid(&g.IO.MousePos)) - g.IO.MousePos = g.MouseLastValidPos = ImFloor(g.IO.MousePos); - - // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta - if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MousePosPrev)) - g.IO.MouseDelta = g.IO.MousePos - g.IO.MousePosPrev; - else - g.IO.MouseDelta = ImVec2(0.0f, 0.0f); - - // If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. - if (g.IO.MouseDelta.x != 0.0f || g.IO.MouseDelta.y != 0.0f) - g.NavDisableMouseHover = false; - - g.IO.MousePosPrev = g.IO.MousePos; - for (int i = 0; i < IM_ARRAYSIZE(g.IO.MouseDown); i++) - { - g.IO.MouseClicked[i] = g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] < 0.0f; - g.IO.MouseClickedCount[i] = 0; // Will be filled below - g.IO.MouseReleased[i] = !g.IO.MouseDown[i] && g.IO.MouseDownDuration[i] >= 0.0f; - g.IO.MouseDownDurationPrev[i] = g.IO.MouseDownDuration[i]; - g.IO.MouseDownDuration[i] = g.IO.MouseDown[i] ? (g.IO.MouseDownDuration[i] < 0.0f ? 0.0f : g.IO.MouseDownDuration[i] + g.IO.DeltaTime) : -1.0f; - if (g.IO.MouseClicked[i]) - { - bool is_repeated_click = false; - if ((float)(g.Time - g.IO.MouseClickedTime[i]) < g.IO.MouseDoubleClickTime) - { - ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); - if (ImLengthSqr(delta_from_click_pos) < g.IO.MouseDoubleClickMaxDist * g.IO.MouseDoubleClickMaxDist) - is_repeated_click = true; - } - if (is_repeated_click) - g.IO.MouseClickedLastCount[i]++; - else - g.IO.MouseClickedLastCount[i] = 1; - g.IO.MouseClickedTime[i] = g.Time; - g.IO.MouseClickedPos[i] = g.IO.MousePos; - g.IO.MouseClickedCount[i] = g.IO.MouseClickedLastCount[i]; - g.IO.MouseDragMaxDistanceAbs[i] = ImVec2(0.0f, 0.0f); - g.IO.MouseDragMaxDistanceSqr[i] = 0.0f; - } - else if (g.IO.MouseDown[i]) - { - // Maintain the maximum distance we reaching from the initial click position, which is used with dragging threshold - ImVec2 delta_from_click_pos = IsMousePosValid(&g.IO.MousePos) ? (g.IO.MousePos - g.IO.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); - g.IO.MouseDragMaxDistanceSqr[i] = ImMax(g.IO.MouseDragMaxDistanceSqr[i], ImLengthSqr(delta_from_click_pos)); - g.IO.MouseDragMaxDistanceAbs[i].x = ImMax(g.IO.MouseDragMaxDistanceAbs[i].x, delta_from_click_pos.x < 0.0f ? -delta_from_click_pos.x : delta_from_click_pos.x); - g.IO.MouseDragMaxDistanceAbs[i].y = ImMax(g.IO.MouseDragMaxDistanceAbs[i].y, delta_from_click_pos.y < 0.0f ? -delta_from_click_pos.y : delta_from_click_pos.y); - } - - // We provide io.MouseDoubleClicked[] as a legacy service - g.IO.MouseDoubleClicked[i] = (g.IO.MouseClickedCount[i] == 2); - - // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation - if (g.IO.MouseClicked[i]) - g.NavDisableMouseHover = false; - } -} - -static void StartLockWheelingWindow(ImGuiWindow* window) -{ - ImGuiContext& g = *GImGui; - if (g.WheelingWindow == window) - return; - g.WheelingWindow = window; - g.WheelingWindowRefMousePos = g.IO.MousePos; - g.WheelingWindowTimer = WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER; -} - -void ImGui::UpdateMouseWheel() -{ - ImGuiContext& g = *GImGui; - - // Reset the locked window if we move the mouse or after the timer elapses - if (g.WheelingWindow != NULL) - { - g.WheelingWindowTimer -= g.IO.DeltaTime; - if (IsMousePosValid() && ImLengthSqr(g.IO.MousePos - g.WheelingWindowRefMousePos) > g.IO.MouseDragThreshold * g.IO.MouseDragThreshold) - g.WheelingWindowTimer = 0.0f; - if (g.WheelingWindowTimer <= 0.0f) - { - g.WheelingWindow = NULL; - g.WheelingWindowTimer = 0.0f; - } - } - - if (g.IO.MouseWheel == 0.0f && g.IO.MouseWheelH == 0.0f) - return; - - if ((g.ActiveId != 0 && g.ActiveIdUsingMouseWheel) || (g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrameUsingMouseWheel)) - return; - - ImGuiWindow* window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow; - if (!window || window->Collapsed) - return; - - // Zoom / Scale window - // FIXME-OBSOLETE: This is an old feature, it still works but pretty much nobody is using it and may be best redesigned. - if (g.IO.MouseWheel != 0.0f && g.IO.KeyCtrl && g.IO.FontAllowUserScaling) - { - StartLockWheelingWindow(window); - const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); - const float scale = new_font_scale / window->FontWindowScale; - window->FontWindowScale = new_font_scale; - if (window == window->RootWindow) - { - const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; - SetWindowPos(window, window->Pos + offset, 0); - window->Size = ImFloor(window->Size * scale); - window->SizeFull = ImFloor(window->SizeFull * scale); - } - return; - } - - // Mouse wheel scrolling - // If a child window has the ImGuiWindowFlags_NoScrollWithMouse flag, we give a chance to scroll its parent - if (g.IO.KeyCtrl) - return; - - // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead - // (we avoid doing it on OSX as it the OS input layer handles this already) - const bool swap_axis = g.IO.KeyShift && !g.IO.ConfigMacOSXBehaviors; - const float wheel_y = swap_axis ? 0.0f : g.IO.MouseWheel; - const float wheel_x = swap_axis ? g.IO.MouseWheel : g.IO.MouseWheelH; - - // Vertical Mouse Wheel scrolling - if (wheel_y != 0.0f) - { - StartLockWheelingWindow(window); - while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.y == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) - window = window->ParentWindow; - if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)) - { - float max_step = window->InnerRect.GetHeight() * 0.67f; - float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step)); - SetScrollY(window, window->Scroll.y - wheel_y * scroll_step); - } - } - - // Horizontal Mouse Wheel scrolling, or Vertical Mouse Wheel w/ Shift held - if (wheel_x != 0.0f) - { - StartLockWheelingWindow(window); - while ((window->Flags & ImGuiWindowFlags_ChildWindow) && ((window->ScrollMax.x == 0.0f) || ((window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)))) - window = window->ParentWindow; - if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)) - { - float max_step = window->InnerRect.GetWidth() * 0.67f; - float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step)); - SetScrollX(window, window->Scroll.x - wheel_x * scroll_step); - } - } -} - // The reason this is exposed in imgui_internal.h is: on touch-based system that don't have hovering, we want to dispatch inputs to the right target (imgui vs imgui+app) void ImGui::UpdateHoveredWindowAndCaptureFlags() { @@ -3973,17 +4325,6 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; } -ImGuiKeyModFlags ImGui::GetMergedKeyModFlags() -{ - ImGuiContext& g = *GImGui; - ImGuiKeyModFlags key_mod_flags = ImGuiKeyModFlags_None; - if (g.IO.KeyCtrl) { key_mod_flags |= ImGuiKeyModFlags_Ctrl; } - if (g.IO.KeyShift) { key_mod_flags |= ImGuiKeyModFlags_Shift; } - if (g.IO.KeyAlt) { key_mod_flags |= ImGuiKeyModFlags_Alt; } - if (g.IO.KeySuper) { key_mod_flags |= ImGuiKeyModFlags_Super; } - return key_mod_flags; -} - void ImGui::NewFrame() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); @@ -4060,15 +4401,20 @@ void ImGui::NewFrame() if (g.HoveredId && g.ActiveId != g.HoveredId) g.HoveredIdNotActiveTimer += g.IO.DeltaTime; g.HoveredIdPreviousFrame = g.HoveredId; - g.HoveredIdPreviousFrameUsingMouseWheel = g.HoveredIdUsingMouseWheel; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; - g.HoveredIdUsingMouseWheel = false; g.HoveredIdDisabled = false; - // Update ActiveId data (clear reference to active widget if the widget isn't alive anymore) - if (g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId && g.ActiveId != 0) + // Clear ActiveID if the item is not alive anymore. + // In 1.87, the common most call to KeepAliveID() was moved from GetID() to ItemAdd(). + // As a result, custom widget using ButtonBehavior() _without_ ItemAdd() need to call KeepAliveID() themselves. + if (g.ActiveId != 0 && g.ActiveIdIsAlive != g.ActiveId && g.ActiveIdPreviousFrame == g.ActiveId) + { + IMGUI_DEBUG_LOG_ACTIVEID("NewFrame(): ClearActiveID() because it isn't marked alive anymore!\n"); ClearActiveID(); + } + + // Update ActiveId data (clear reference to active widget if the widget isn't alive anymore) if (g.ActiveId) g.ActiveIdTimer += g.IO.DeltaTime; g.LastActiveIdTimer += g.IO.DeltaTime; @@ -4084,8 +4430,41 @@ void ImGui::NewFrame() if (g.ActiveId == 0) { g.ActiveIdUsingNavDirMask = 0x00; + g.ActiveIdUsingAllKeyboardKeys = false; +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO g.ActiveIdUsingNavInputMask = 0x00; - g.ActiveIdUsingKeyInputMask = 0x00; +#endif + } + +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + if (g.ActiveId == 0) + g.ActiveIdUsingNavInputMask = 0; + else if (g.ActiveIdUsingNavInputMask != 0) + { + // If your custom widget code used: { g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); } + // Since IMGUI_VERSION_NUM >= 18804 it should be: { SetKeyOwner(ImGuiKey_Escape, g.ActiveId); SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId); } + if (g.ActiveIdUsingNavInputMask & (1 << ImGuiNavInput_Cancel)) + SetKeyOwner(ImGuiKey_Escape, g.ActiveId); + if (g.ActiveIdUsingNavInputMask & ~(1 << ImGuiNavInput_Cancel)) + IM_ASSERT(0); // Other values unsupported + } +#endif + + // Update hover delay for IsItemHovered() with delays and tooltips + g.HoverDelayIdPreviousFrame = g.HoverDelayId; + if (g.HoverDelayId != 0) + { + //if (g.IO.MouseDelta.x == 0.0f && g.IO.MouseDelta.y == 0.0f) // Need design/flags + g.HoverDelayTimer += g.IO.DeltaTime; + g.HoverDelayClearTimer = 0.0f; + g.HoverDelayId = 0; + } + else if (g.HoverDelayTimer > 0.0f) + { + // This gives a little bit of leeway before clearing the hover timer, allowing mouse to cross gaps + g.HoverDelayClearTimer += g.IO.DeltaTime; + if (g.HoverDelayClearTimer >= ImMax(0.20f, g.IO.DeltaTime * 2.0f)) // ~6 frames at 30 Hz + allow for low framerate + g.HoverDelayTimer = g.HoverDelayClearTimer = 0.0f; // May want a decaying timer, in which case need to clamp at max first, based on max of caller last requested timer. } // Drag and drop @@ -4100,20 +4479,17 @@ void ImGui::NewFrame() //if (g.IO.AppFocusLost) // ClosePopupsExceptModals(); - // Clear buttons state when focus is lost - // (this is useful so e.g. releasing Alt after focus loss on Alt-Tab doesn't trigger the Alt menu toggle) - if (g.IO.AppFocusLost) - { - g.IO.ClearInputKeys(); - g.IO.AppFocusLost = false; - } + // Process input queue (trickle as many events as possible) + g.InputEventsTrail.resize(0); + UpdateInputEvents(g.IO.ConfigInputTrickleEventQueue); // Update keyboard input state - // Synchronize io.KeyMods with individual modifiers io.KeyXXX bools - g.IO.KeyMods = GetMergedKeyModFlags(); - memcpy(g.IO.KeysDownDurationPrev, g.IO.KeysDownDuration, sizeof(g.IO.KeysDownDuration)); - for (int i = 0; i < IM_ARRAYSIZE(g.IO.KeysDown); i++) - g.IO.KeysDownDuration[i] = g.IO.KeysDown[i] ? (g.IO.KeysDownDuration[i] < 0.0f ? 0.0f : g.IO.KeysDownDuration[i] + g.IO.DeltaTime) : -1.0f; + UpdateKeyboardInputs(); + + //IM_ASSERT(g.IO.KeyCtrl == IsKeyDown(ImGuiKey_LeftCtrl) || IsKeyDown(ImGuiKey_RightCtrl)); + //IM_ASSERT(g.IO.KeyShift == IsKeyDown(ImGuiKey_LeftShift) || IsKeyDown(ImGuiKey_RightShift)); + //IM_ASSERT(g.IO.KeyAlt == IsKeyDown(ImGuiKey_LeftAlt) || IsKeyDown(ImGuiKey_RightAlt)); + //IM_ASSERT(g.IO.KeySuper == IsKeyDown(ImGuiKey_LeftSuper) || IsKeyDown(ImGuiKey_RightSuper)); // Update gamepad/keyboard navigation NavUpdate(); @@ -4136,7 +4512,10 @@ void ImGui::NewFrame() g.MouseCursor = ImGuiMouseCursor_Arrow; g.WantCaptureMouseNextFrame = g.WantCaptureKeyboardNextFrame = g.WantTextInputNextFrame = -1; - g.PlatformImePos = ImVec2(1.0f, 1.0f); // OS Input Method Editor showing on top-left of our window by default + + // Platform IME data: reset for the frame + g.PlatformImeDataPrev = g.PlatformImeData; + g.PlatformImeData.WantVisible = false; // Mouse wheel scrolling, scale UpdateMouseWheel(); @@ -4148,9 +4527,10 @@ void ImGui::NewFrame() { ImGuiWindow* window = g.Windows[i]; window->WasActive = window->Active; - window->BeginCount = 0; window->Active = false; window->WriteAccessed = false; + window->BeginCountPreviousFrame = window->BeginCount; + window->BeginCount = 0; // Garbage collect transient buffers of recently unused windows if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) @@ -4183,10 +4563,12 @@ void ImGui::NewFrame() // [DEBUG] Update debug features UpdateDebugToolItemPicker(); UpdateDebugToolStackQueries(); + if (g.DebugLocateFrames > 0 && --g.DebugLocateFrames == 0) + g.DebugLocateId = 0; // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. - // This fallback is particularly important as it avoid ImGui:: calls from crashing. + // This fallback is particularly important as it prevents ImGui:: calls from crashing. g.WithinFrameScopeWithImplicitWindow = true; SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); Begin("Debug##Default"); @@ -4195,148 +4577,40 @@ void ImGui::NewFrame() CallContextHooks(&g, ImGuiContextHookType_NewFramePost); } -void ImGui::Initialize(ImGuiContext* context) +// FIXME: Add a more explicit sort order in the window structure. +static int IMGUI_CDECL ChildWindowComparer(const void* lhs, const void* rhs) { - ImGuiContext& g = *context; - IM_ASSERT(!g.Initialized && !g.SettingsLoaded); - - // Add .ini handle for ImGuiWindow type - { - ImGuiSettingsHandler ini_handler; - ini_handler.TypeName = "Window"; - ini_handler.TypeHash = ImHashStr("Window"); - ini_handler.ClearAllFn = WindowSettingsHandler_ClearAll; - ini_handler.ReadOpenFn = WindowSettingsHandler_ReadOpen; - ini_handler.ReadLineFn = WindowSettingsHandler_ReadLine; - ini_handler.ApplyAllFn = WindowSettingsHandler_ApplyAll; - ini_handler.WriteAllFn = WindowSettingsHandler_WriteAll; - g.SettingsHandlers.push_back(ini_handler); - } - - // Add .ini handle for ImGuiTable type - TableSettingsInstallHandler(context); - - // Create default viewport - ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); - g.Viewports.push_back(viewport); - -#ifdef IMGUI_HAS_DOCK -#endif - - g.Initialized = true; + const ImGuiWindow* const a = *(const ImGuiWindow* const *)lhs; + const ImGuiWindow* const b = *(const ImGuiWindow* const *)rhs; + if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup)) + return d; + if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip)) + return d; + return (a->BeginOrderWithinParent - b->BeginOrderWithinParent); } -// This function is merely here to free heap allocations. -void ImGui::Shutdown(ImGuiContext* context) +static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window) { - // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) - ImGuiContext& g = *context; - if (g.IO.Fonts && g.FontAtlasOwnedByContext) + out_sorted_windows->push_back(window); + if (window->Active) { - g.IO.Fonts->Locked = false; - IM_DELETE(g.IO.Fonts); + int count = window->DC.ChildWindows.Size; + ImQsort(window->DC.ChildWindows.Data, (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); + for (int i = 0; i < count; i++) + { + ImGuiWindow* child = window->DC.ChildWindows[i]; + if (child->Active) + AddWindowToSortBuffer(out_sorted_windows, child); + } } - g.IO.Fonts = NULL; - - // Cleanup of other data are conditional on actually having initialized Dear ImGui. - if (!g.Initialized) - return; - - // Save settings (unless we haven't attempted to load them: CreateContext/DestroyContext without a call to NewFrame shouldn't save an empty file) - if (g.SettingsLoaded && g.IO.IniFilename != NULL) - { - ImGuiContext* backup_context = GImGui; - SetCurrentContext(&g); - SaveIniSettingsToDisk(g.IO.IniFilename); - SetCurrentContext(backup_context); - } - - CallContextHooks(&g, ImGuiContextHookType_Shutdown); - - // Clear everything else - g.Windows.clear_delete(); - g.WindowsFocusOrder.clear(); - g.WindowsTempSortBuffer.clear(); - g.CurrentWindow = NULL; - g.CurrentWindowStack.clear(); - g.WindowsById.Clear(); - g.NavWindow = NULL; - g.HoveredWindow = g.HoveredWindowUnderMovingWindow = NULL; - g.ActiveIdWindow = g.ActiveIdPreviousFrameWindow = NULL; - g.MovingWindow = NULL; - g.ColorStack.clear(); - g.StyleVarStack.clear(); - g.FontStack.clear(); - g.OpenPopupStack.clear(); - g.BeginPopupStack.clear(); - - g.Viewports.clear_delete(); - - g.TabBars.Clear(); - g.CurrentTabBarStack.clear(); - g.ShrinkWidthBuffer.clear(); - - g.ClipperTempData.clear_destruct(); - - g.Tables.Clear(); - g.TablesTempData.clear_destruct(); - g.DrawChannelsTempMergeBuffer.clear(); - - g.ClipboardHandlerData.clear(); - g.MenusIdSubmittedThisFrame.clear(); - g.InputTextState.ClearFreeMemory(); - - g.SettingsWindows.clear(); - g.SettingsHandlers.clear(); - - if (g.LogFile) - { -#ifndef IMGUI_DISABLE_TTY_FUNCTIONS - if (g.LogFile != stdout) -#endif - ImFileClose(g.LogFile); - g.LogFile = NULL; - } - g.LogBuffer.clear(); - - g.Initialized = false; -} - -// FIXME: Add a more explicit sort order in the window structure. -static int IMGUI_CDECL ChildWindowComparer(const void* lhs, const void* rhs) -{ - const ImGuiWindow* const a = *(const ImGuiWindow* const *)lhs; - const ImGuiWindow* const b = *(const ImGuiWindow* const *)rhs; - if (int d = (a->Flags & ImGuiWindowFlags_Popup) - (b->Flags & ImGuiWindowFlags_Popup)) - return d; - if (int d = (a->Flags & ImGuiWindowFlags_Tooltip) - (b->Flags & ImGuiWindowFlags_Tooltip)) - return d; - return (a->BeginOrderWithinParent - b->BeginOrderWithinParent); -} - -static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window) -{ - out_sorted_windows->push_back(window); - if (window->Active) - { - int count = window->DC.ChildWindows.Size; - ImQsort(window->DC.ChildWindows.Data, (size_t)count, sizeof(ImGuiWindow*), ChildWindowComparer); - for (int i = 0; i < count; i++) - { - ImGuiWindow* child = window->DC.ChildWindows[i]; - if (child->Active) - AddWindowToSortBuffer(out_sorted_windows, child); - } - } -} +} static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) { - // Remove trailing command if unused. - // Technically we could return directly instead of popping, but this make things looks neat in Metrics/Debugger window as well. - draw_list->_PopUnusedDrawCmd(); if (draw_list->CmdBuffer.Size == 0) return; + if (draw_list->CmdBuffer.Size == 1 && draw_list->CmdBuffer[0].ElemCount == 0 && draw_list->CmdBuffer[0].UserCallback == NULL) + return; // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. // May trigger for you if you are using PrimXXX functions incorrectly. @@ -4422,8 +4696,10 @@ static void SetupViewportDrawData(ImGuiViewportP* viewport, ImVectorFramebufferScale = io.DisplayFramebufferScale; for (int n = 0; n < draw_lists->Size; n++) { - draw_data->TotalVtxCount += draw_lists->Data[n]->VtxBuffer.Size; - draw_data->TotalIdxCount += draw_lists->Data[n]->IdxBuffer.Size; + ImDrawList* draw_list = draw_lists->Data[n]; + draw_list->_PopUnusedDrawCmd(); + draw_data->TotalVtxCount += draw_list->VtxBuffer.Size; + draw_data->TotalIdxCount += draw_list->IdxBuffer.Size; } } @@ -4459,6 +4735,7 @@ static void ImGui::RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 { // We've already called AddWindowToDrawData() which called DrawList->ChannelsMerge() on DockNodeHost windows, // and draw list have been trimmed already, hence the explicit recreation of a draw command if missing. + // FIXME: This is creating complication, might be simpler if we could inject a drawlist in drawdata at a given position and not attempt to manipulate ImDrawCmd order. ImDrawList* draw_list = window->RootWindow->DrawList; if (draw_list->CmdBuffer.Size == 0) draw_list->AddDrawCmd(); @@ -4469,6 +4746,7 @@ static void ImGui::RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 draw_list->CmdBuffer.pop_back(); draw_list->CmdBuffer.push_front(cmd); draw_list->PopClipRect(); + draw_list->AddDrawCmd(); // We need to create a command as CmdBuffer.back().IdxOffset won't be correct if we append to same command. } } @@ -4493,6 +4771,8 @@ static void ImGui::RenderDimmedBackgrounds() { ImGuiContext& g = *GImGui; ImGuiWindow* modal_window = GetTopMostAndVisiblePopupModal(); + if (g.DimBgRatio <= 0.0f && g.NavWindowingHighlightAlpha <= 0.0f) + return; const bool dim_bg_for_modal = (modal_window != NULL); const bool dim_bg_for_window_list = (g.NavWindowingTargetAnim != NULL && g.NavWindowingTargetAnim->Active); if (!dim_bg_for_modal && !dim_bg_for_window_list) @@ -4540,12 +4820,9 @@ void ImGui::EndFrame() ErrorCheckEndFrameSanityChecks(); - // Notify OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) - if (g.IO.ImeSetInputScreenPosFn && (g.PlatformImeLastPos.x == FLT_MAX || ImLengthSqr(g.PlatformImeLastPos - g.PlatformImePos) > 0.0001f)) - { - g.IO.ImeSetInputScreenPosFn((int)g.PlatformImePos.x, (int)g.PlatformImePos.y); - g.PlatformImeLastPos = g.PlatformImePos; - } + // Notify Platform/OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) + if (g.IO.SetPlatformImeDataFn && memcmp(&g.PlatformImeData, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) + g.IO.SetPlatformImeDataFn(GetMainViewport(), &g.PlatformImeData); // Hide implicit/fallback "Debug" window if it hasn't been used g.WithinFrameScopeWithImplicitWindow = false; @@ -4601,10 +4878,9 @@ void ImGui::EndFrame() g.IO.Fonts->Locked = false; // Clear Input data for next frame + g.IO.AppFocusLost = false; g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.InputQueueCharacters.resize(0); - g.IO.KeyModsPrev = g.IO.KeyMods; // doing it here is better than in NewFrame() as we'll tolerate backend writing to KeyMods. If we want to firmly disallow it we should detect it. - memset(g.IO.NavInputs, 0, sizeof(g.IO.NavInputs)); CallContextHooks(&g, ImGuiContextHookType_EndFramePost); } @@ -4653,6 +4929,10 @@ void ImGui::Render() if (windows_to_render_top_most[n] && IsWindowActiveAndVisible(windows_to_render_top_most[n])) // NavWindowingTarget is always temporarily displayed as the top-most window AddRootWindowToDrawData(windows_to_render_top_most[n]); + // Draw software mouse cursor if requested by io.MouseDrawCursor flag + if (g.IO.MouseDrawCursor && first_render_of_frame && g.MouseCursor != ImGuiMouseCursor_None) + RenderMouseCursor(g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); + // Setup ImDrawData structures for end-user g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = 0; for (int n = 0; n < g.Viewports.Size; n++) @@ -4660,10 +4940,6 @@ void ImGui::Render() ImGuiViewportP* viewport = g.Viewports[n]; viewport->DrawDataBuilder.FlattenIntoSingleLayer(); - // Draw software mouse cursor if requested by io.MouseDrawCursor flag - if (g.IO.MouseDrawCursor && first_render_of_frame) - RenderMouseCursor(GetForegroundDrawList(viewport), g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); - // Add foreground ImDrawList (for each active viewport) if (viewport->DrawLists[1] != NULL) AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport)); @@ -4761,381 +5037,145 @@ static void FindHoveredWindow() g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window; } -// Test if mouse cursor is hovering given rectangle -// NB- Rectangle is clipped by our current clip setting -// NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding) -bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip) +bool ImGui::IsItemActive() { ImGuiContext& g = *GImGui; - - // Clip - ImRect rect_clipped(r_min, r_max); - if (clip) - rect_clipped.ClipWith(g.CurrentWindow->ClipRect); - - // Expand for touch input - const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); - if (!rect_for_touch.Contains(g.IO.MousePos)) - return false; - return true; + if (g.ActiveId) + return g.ActiveId == g.LastItemData.ID; + return false; } -int ImGui::GetKeyIndex(ImGuiKey imgui_key) +bool ImGui::IsItemActivated() { - IM_ASSERT(imgui_key >= 0 && imgui_key < ImGuiKey_COUNT); ImGuiContext& g = *GImGui; - return g.IO.KeyMap[imgui_key]; + if (g.ActiveId) + if (g.ActiveId == g.LastItemData.ID && g.ActiveIdPreviousFrame != g.LastItemData.ID) + return true; + return false; } -// Note that dear imgui doesn't know the semantic of each entry of io.KeysDown[]! -// Use your own indices/enums according to how your backend/engine stored them into io.KeysDown[]! -bool ImGui::IsKeyDown(int user_key_index) +bool ImGui::IsItemDeactivated() { - if (user_key_index < 0) - return false; ImGuiContext& g = *GImGui; - IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); - return g.IO.KeysDown[user_key_index]; -} - -// t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) -// t1 = current time (e.g.: g.Time) -// An event is triggered at: -// t = 0.0f t = repeat_delay, t = repeat_delay + repeat_rate*N -int ImGui::CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate) -{ - if (t1 == 0.0f) - return 1; - if (t0 >= t1) - return 0; - if (repeat_rate <= 0.0f) - return (t0 < repeat_delay) && (t1 >= repeat_delay); - const int count_t0 = (t0 < repeat_delay) ? -1 : (int)((t0 - repeat_delay) / repeat_rate); - const int count_t1 = (t1 < repeat_delay) ? -1 : (int)((t1 - repeat_delay) / repeat_rate); - const int count = count_t1 - count_t0; - return count; + if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDeactivated) + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Deactivated) != 0; + return (g.ActiveIdPreviousFrame == g.LastItemData.ID && g.ActiveIdPreviousFrame != 0 && g.ActiveId != g.LastItemData.ID); } -int ImGui::GetKeyPressedAmount(int key_index, float repeat_delay, float repeat_rate) +bool ImGui::IsItemDeactivatedAfterEdit() { ImGuiContext& g = *GImGui; - if (key_index < 0) - return 0; - IM_ASSERT(key_index >= 0 && key_index < IM_ARRAYSIZE(g.IO.KeysDown)); - const float t = g.IO.KeysDownDuration[key_index]; - return CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, repeat_delay, repeat_rate); + return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEditedBefore || (g.ActiveId == 0 && g.ActiveIdHasBeenEditedBefore)); } -bool ImGui::IsKeyPressed(int user_key_index, bool repeat) +// == GetItemID() == GetFocusID() +bool ImGui::IsItemFocused() { ImGuiContext& g = *GImGui; - if (user_key_index < 0) + if (g.NavId != g.LastItemData.ID || g.NavId == 0) return false; - IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); - const float t = g.IO.KeysDownDuration[user_key_index]; - if (t == 0.0f) - return true; - if (repeat && t > g.IO.KeyRepeatDelay) - return GetKeyPressedAmount(user_key_index, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; - return false; + return true; } -bool ImGui::IsKeyReleased(int user_key_index) +// Important: this can be useful but it is NOT equivalent to the behavior of e.g.Button()! +// Most widgets have specific reactions based on mouse-up/down state, mouse position etc. +bool ImGui::IsItemClicked(ImGuiMouseButton mouse_button) { - ImGuiContext& g = *GImGui; - if (user_key_index < 0) return false; - IM_ASSERT(user_key_index >= 0 && user_key_index < IM_ARRAYSIZE(g.IO.KeysDown)); - return g.IO.KeysDownDurationPrev[user_key_index] >= 0.0f && !g.IO.KeysDown[user_key_index]; + return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); } -bool ImGui::IsMouseDown(ImGuiMouseButton button) +bool ImGui::IsItemToggledOpen() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - return g.IO.MouseDown[button]; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; } -bool ImGui::IsMouseClicked(ImGuiMouseButton button, bool repeat) +bool ImGui::IsItemToggledSelection() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - const float t = g.IO.MouseDownDuration[button]; - if (t == 0.0f) - return true; - - if (repeat && t > g.IO.KeyRepeatDelay) - { - // FIXME: 2019/05/03: Our old repeat code was wrong here and led to doubling the repeat rate, which made it an ok rate for repeat on mouse hold. - int amount = CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate * 0.50f); - if (amount > 0) - return true; - } - return false; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; } -bool ImGui::IsMouseReleased(ImGuiMouseButton button) +bool ImGui::IsAnyItemHovered() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - return g.IO.MouseReleased[button]; + return g.HoveredId != 0 || g.HoveredIdPreviousFrame != 0; } -bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) +bool ImGui::IsAnyItemActive() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - return g.IO.MouseClickedCount[button] == 2; + return g.ActiveId != 0; } -int ImGui::GetMouseClickedCount(ImGuiMouseButton button) +bool ImGui::IsAnyItemFocused() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - return g.IO.MouseClickedCount[button]; + return g.NavId != 0 && !g.NavDisableHighlight; } -// Return if a mouse click/drag went past the given threshold. Valid to call during the MouseReleased frame. -// [Internal] This doesn't test if the button is pressed -bool ImGui::IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold) +bool ImGui::IsItemVisible() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - if (lock_threshold < 0.0f) - lock_threshold = g.IO.MouseDragThreshold; - return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) != 0; } -bool ImGui::IsMouseDragging(ImGuiMouseButton button, float lock_threshold) +bool ImGui::IsItemEdited() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - if (!g.IO.MouseDown[button]) - return false; - return IsMouseDragPastThreshold(button, lock_threshold); + return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Edited) != 0; } -ImVec2 ImGui::GetMousePos() +// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. +// FIXME: Although this is exposed, its interaction and ideal idiom with using ImGuiButtonFlags_AllowItemOverlap flag are extremely confusing, need rework. +void ImGui::SetItemAllowOverlap() { ImGuiContext& g = *GImGui; - return g.IO.MousePos; + ImGuiID id = g.LastItemData.ID; + if (g.HoveredId == id) + g.HoveredIdAllowOverlap = true; + if (g.ActiveId == id) + g.ActiveIdAllowOverlap = true; } -// NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! -ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() +// FIXME: It might be undesirable that this will likely disable KeyOwner-aware shortcuts systems. Consider a more fine-tuned version for the two users of this function. +void ImGui::SetActiveIdUsingAllKeyboardKeys() { ImGuiContext& g = *GImGui; - if (g.BeginPopupStack.Size > 0) - return g.OpenPopupStack[g.BeginPopupStack.Size - 1].OpenMousePos; - return g.IO.MousePos; + IM_ASSERT(g.ActiveId != 0); + g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_COUNT) - 1; + g.ActiveIdUsingAllKeyboardKeys = true; + NavMoveRequestCancel(); } -// We typically use ImVec2(-FLT_MAX,-FLT_MAX) to denote an invalid mouse position. -bool ImGui::IsMousePosValid(const ImVec2* mouse_pos) +ImGuiID ImGui::GetItemID() { - // The assert is only to silence a false-positive in XCode Static Analysis. - // Because GImGui is not dereferenced in every code path, the static analyzer assume that it may be NULL (which it doesn't for other functions). - IM_ASSERT(GImGui != NULL); - const float MOUSE_INVALID = -256000.0f; - ImVec2 p = mouse_pos ? *mouse_pos : GImGui->IO.MousePos; - return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID; + ImGuiContext& g = *GImGui; + return g.LastItemData.ID; } -bool ImGui::IsAnyMouseDown() +ImVec2 ImGui::GetItemRectMin() { ImGuiContext& g = *GImGui; - for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) - if (g.IO.MouseDown[n]) - return true; - return false; + return g.LastItemData.Rect.Min; } -// Return the delta from the initial clicking position while the mouse button is clicked or was just released. -// This is locked and return 0.0f until the mouse moves past a distance threshold at least once. -// NB: This is only valid if IsMousePosValid(). backends in theory should always keep mouse position valid when dragging even outside the client window. -ImVec2 ImGui::GetMouseDragDelta(ImGuiMouseButton button, float lock_threshold) +ImVec2 ImGui::GetItemRectMax() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - if (lock_threshold < 0.0f) - lock_threshold = g.IO.MouseDragThreshold; - if (g.IO.MouseDown[button] || g.IO.MouseReleased[button]) - if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold) - if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MouseClickedPos[button])) - return g.IO.MousePos - g.IO.MouseClickedPos[button]; - return ImVec2(0.0f, 0.0f); + return g.LastItemData.Rect.Max; } -void ImGui::ResetMouseDragDelta(ImGuiMouseButton button) +ImVec2 ImGui::GetItemRectSize() { ImGuiContext& g = *GImGui; - IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); - // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr - g.IO.MouseClickedPos[button] = g.IO.MousePos; + return g.LastItemData.Rect.GetSize(); } -ImGuiMouseCursor ImGui::GetMouseCursor() +bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) { - return GImGui->MouseCursor; -} - -void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) -{ - GImGui->MouseCursor = cursor_type; -} - -void ImGui::CaptureKeyboardFromApp(bool capture) -{ - GImGui->WantCaptureKeyboardNextFrame = capture ? 1 : 0; -} - -void ImGui::CaptureMouseFromApp(bool capture) -{ - GImGui->WantCaptureMouseNextFrame = capture ? 1 : 0; -} - -bool ImGui::IsItemActive() -{ - ImGuiContext& g = *GImGui; - if (g.ActiveId) - return g.ActiveId == g.LastItemData.ID; - return false; -} - -bool ImGui::IsItemActivated() -{ - ImGuiContext& g = *GImGui; - if (g.ActiveId) - if (g.ActiveId == g.LastItemData.ID && g.ActiveIdPreviousFrame != g.LastItemData.ID) - return true; - return false; -} - -bool ImGui::IsItemDeactivated() -{ - ImGuiContext& g = *GImGui; - if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDeactivated) - return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Deactivated) != 0; - return (g.ActiveIdPreviousFrame == g.LastItemData.ID && g.ActiveIdPreviousFrame != 0 && g.ActiveId != g.LastItemData.ID); -} - -bool ImGui::IsItemDeactivatedAfterEdit() -{ - ImGuiContext& g = *GImGui; - return IsItemDeactivated() && (g.ActiveIdPreviousFrameHasBeenEditedBefore || (g.ActiveId == 0 && g.ActiveIdHasBeenEditedBefore)); -} - -// == GetItemID() == GetFocusID() -bool ImGui::IsItemFocused() -{ - ImGuiContext& g = *GImGui; - if (g.NavId != g.LastItemData.ID || g.NavId == 0) - return false; - return true; -} - -// Important: this can be useful but it is NOT equivalent to the behavior of e.g.Button()! -// Most widgets have specific reactions based on mouse-up/down state, mouse position etc. -bool ImGui::IsItemClicked(ImGuiMouseButton mouse_button) -{ - return IsMouseClicked(mouse_button) && IsItemHovered(ImGuiHoveredFlags_None); -} - -bool ImGui::IsItemToggledOpen() -{ - ImGuiContext& g = *GImGui; - return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; -} - -bool ImGui::IsItemToggledSelection() -{ - ImGuiContext& g = *GImGui; - return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; -} - -bool ImGui::IsAnyItemHovered() -{ - ImGuiContext& g = *GImGui; - return g.HoveredId != 0 || g.HoveredIdPreviousFrame != 0; -} - -bool ImGui::IsAnyItemActive() -{ - ImGuiContext& g = *GImGui; - return g.ActiveId != 0; -} - -bool ImGui::IsAnyItemFocused() -{ - ImGuiContext& g = *GImGui; - return g.NavId != 0 && !g.NavDisableHighlight; -} - -bool ImGui::IsItemVisible() -{ - ImGuiContext& g = *GImGui; - return g.CurrentWindow->ClipRect.Overlaps(g.LastItemData.Rect); -} - -bool ImGui::IsItemEdited() -{ - ImGuiContext& g = *GImGui; - return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Edited) != 0; -} - -// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. -// FIXME: Although this is exposed, its interaction and ideal idiom with using ImGuiButtonFlags_AllowItemOverlap flag are extremely confusing, need rework. -void ImGui::SetItemAllowOverlap() -{ - ImGuiContext& g = *GImGui; - ImGuiID id = g.LastItemData.ID; - if (g.HoveredId == id) - g.HoveredIdAllowOverlap = true; - if (g.ActiveId == id) - g.ActiveIdAllowOverlap = true; -} - -void ImGui::SetItemUsingMouseWheel() -{ - ImGuiContext& g = *GImGui; - ImGuiID id = g.LastItemData.ID; - if (g.HoveredId == id) - g.HoveredIdUsingMouseWheel = true; - if (g.ActiveId == id) - g.ActiveIdUsingMouseWheel = true; -} - -void ImGui::SetActiveIdUsingNavAndKeys() -{ - ImGuiContext& g = *GImGui; - IM_ASSERT(g.ActiveId != 0); - g.ActiveIdUsingNavDirMask = ~(ImU32)0; - g.ActiveIdUsingNavInputMask = ~(ImU32)0; - g.ActiveIdUsingKeyInputMask = ~(ImU64)0; - NavMoveRequestCancel(); -} - -ImVec2 ImGui::GetItemRectMin() -{ - ImGuiContext& g = *GImGui; - return g.LastItemData.Rect.Min; -} - -ImVec2 ImGui::GetItemRectMax() -{ - ImGuiContext& g = *GImGui; - return g.LastItemData.Rect.Max; -} - -ImVec2 ImGui::GetItemRectSize() -{ - ImGuiContext& g = *GImGui; - return g.LastItemData.Rect.GetSize(); -} - -bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* parent_window = g.CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* parent_window = g.CurrentWindow; flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_ChildWindow; flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag @@ -5145,21 +5185,22 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b ImVec2 size = ImFloor(size_arg); const int auto_fit_axises = ((size.x == 0.0f) ? (1 << ImGuiAxis_X) : 0x00) | ((size.y == 0.0f) ? (1 << ImGuiAxis_Y) : 0x00); if (size.x <= 0.0f) - size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too much issues) + size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too many issues) if (size.y <= 0.0f) size.y = ImMax(content_avail.y + size.y, 4.0f); SetNextWindowSize(size); // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. + const char* temp_window_name; if (name) - ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%s_%08X", parent_window->Name, name, id); + ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%s_%08X", parent_window->Name, name, id); else - ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%s/%08X", parent_window->Name, id); + ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%08X", parent_window->Name, id); const float backup_border_size = g.Style.ChildBorderSize; if (!border) g.Style.ChildBorderSize = 0.0f; - bool ret = Begin(g.TempBuffer, NULL, flags); + bool ret = Begin(temp_window_name, NULL, flags); g.Style.ChildBorderSize = backup_border_size; ImGuiWindow* child_window = g.CurrentWindow; @@ -5291,7 +5332,7 @@ static void UpdateWindowInFocusOrderList(ImGuiWindow* window, bool just_created, { ImGuiContext& g = *GImGui; - const bool new_is_explicit_child = (new_flags & ImGuiWindowFlags_ChildWindow) != 0; + const bool new_is_explicit_child = (new_flags & ImGuiWindowFlags_ChildWindow) != 0 && ((new_flags & ImGuiWindowFlags_Popup) == 0 || (new_flags & ImGuiWindowFlags_ChildMenu) != 0); const bool child_flag_changed = new_is_explicit_child != window->IsExplicitChild; if ((just_created || child_flag_changed) && !new_is_explicit_child) { @@ -5333,7 +5374,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); ApplyWindowSettings(window, settings); } - window->DC.CursorStartPos = window->DC.CursorMaxPos = window->Pos; // So first call to CalcContentSize() doesn't return crazy values + window->DC.CursorStartPos = window->DC.CursorMaxPos = window->DC.IdealMaxPos = window->Pos; // So first call to CalcWindowContentSizes() doesn't return crazy values if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) { @@ -5353,7 +5394,6 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) g.Windows.push_front(window); // Quite slow but rare and only once else g.Windows.push_back(window); - UpdateWindowInFocusOrderList(window, true, window->Flags); return window; } @@ -5386,9 +5426,9 @@ static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, const ImVec2& s if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) { ImGuiWindow* window_for_height = window; - const float decoration_up_height = window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight(); new_size = ImMax(new_size, g.Style.WindowMinSize); - new_size.y = ImMax(new_size.y, decoration_up_height + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows + const float minimum_height = window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f); + new_size.y = ImMax(new_size.y, minimum_height); // Reduce artifacts with very small windows } return new_size; } @@ -5417,9 +5457,10 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; - const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); + const float decoration_w_without_scrollbars = window->DecoOuterSizeX1 + window->DecoOuterSizeX2 - window->ScrollbarSizes.x; + const float decoration_h_without_scrollbars = window->DecoOuterSizeY1 + window->DecoOuterSizeY2 - window->ScrollbarSizes.y; ImVec2 size_pad = window->WindowPadding * 2.0f; - ImVec2 size_desired = size_contents + size_pad + ImVec2(0.0f, decoration_up_height); + ImVec2 size_desired = size_contents + size_pad + ImVec2(decoration_w_without_scrollbars, decoration_h_without_scrollbars); if (window->Flags & ImGuiWindowFlags_Tooltip) { // Tooltip always resize @@ -5434,15 +5475,14 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont if (is_popup || is_menu) // Popups and menus bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) size_min = ImMin(size_min, ImVec2(4.0f, 4.0f)); - // FIXME-VIEWPORT-WORKAREA: May want to use GetWorkSize() instead of Size depending on the type of windows? - ImVec2 avail_size = ImGui::GetMainViewport()->Size; + ImVec2 avail_size = ImGui::GetMainViewport()->WorkSize; ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, avail_size - style.DisplaySafeAreaPadding * 2.0f)); // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. ImVec2 size_auto_fit_after_constraint = CalcWindowSizeAfterConstraint(window, size_auto_fit); - bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - 0.0f < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); - bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - decoration_up_height < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); + bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - decoration_w_without_scrollbars < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); + bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - decoration_h_without_scrollbars < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); if (will_have_scrollbar_x) size_auto_fit.y += style.ScrollbarSize; if (will_have_scrollbar_y) @@ -5549,7 +5589,7 @@ ImGuiID ImGui::GetWindowResizeBorderID(ImGuiWindow* window, ImGuiDir dir) } // Handle resize for: Resize Grips, Borders, Gamepad -// Return true when using auto-fit (double click on resize grip) +// Return true when using auto-fit (double-click on resize grip) static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect) { ImGuiContext& g = *GImGui; @@ -5557,7 +5597,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if ((flags & ImGuiWindowFlags_NoResize) || (flags & ImGuiWindowFlags_AlwaysAutoResize) || window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) return false; - if (window->WasActive == false) // Early out to avoid running this code for e.g. an hidden implicit/fallback Debug window. + if (window->WasActive == false) // Early out to avoid running this code for e.g. a hidden implicit/fallback Debug window. return false; bool ret_auto_fit = false; @@ -5585,6 +5625,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (resize_rect.Min.x > resize_rect.Max.x) ImSwap(resize_rect.Min.x, resize_rect.Max.x); if (resize_rect.Min.y > resize_rect.Max.y) ImSwap(resize_rect.Min.y, resize_rect.Max.y); ImGuiID resize_grip_id = window->GetID(resize_grip_n); // == GetWindowResizeCornerID() + ItemAdd(resize_rect, resize_grip_id, NULL, ImGuiItemFlags_NoNav); ButtonBehavior(resize_rect, resize_grip_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); if (hovered || held) @@ -5620,6 +5661,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s bool hovered, held; ImRect border_rect = GetResizeBorderRect(window, border_n, grip_hover_inner_size, WINDOWS_HOVER_PADDING); ImGuiID border_id = window->GetID(border_n + 4); // == GetWindowResizeBorderID() + ItemAdd(border_rect, border_id, NULL, ImGuiItemFlags_NoNav); ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) @@ -5644,23 +5686,31 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s window->DC.NavLayerCurrent = ImGuiNavLayer_Main; // Navigation resize (keyboard/gamepad) + // FIXME: This cannot be moved to NavUpdateWindowing() because CalcWindowSizeAfterConstraint() need to callback into user. + // Not even sure the callback works here. if (g.NavWindowingTarget && g.NavWindowingTarget->RootWindow == window) { - ImVec2 nav_resize_delta; + ImVec2 nav_resize_dir; if (g.NavInputSource == ImGuiInputSource_Keyboard && g.IO.KeyShift) - nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_RawKeyboard, ImGuiInputReadMode_Down); + nav_resize_dir = GetKeyMagnitude2d(ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_UpArrow, ImGuiKey_DownArrow); if (g.NavInputSource == ImGuiInputSource_Gamepad) - nav_resize_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_Down); - if (nav_resize_delta.x != 0.0f || nav_resize_delta.y != 0.0f) + nav_resize_dir = GetKeyMagnitude2d(ImGuiKey_GamepadDpadLeft, ImGuiKey_GamepadDpadRight, ImGuiKey_GamepadDpadUp, ImGuiKey_GamepadDpadDown); + if (nav_resize_dir.x != 0.0f || nav_resize_dir.y != 0.0f) { const float NAV_RESIZE_SPEED = 600.0f; - nav_resize_delta *= ImFloor(NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y)); - nav_resize_delta = ImMax(nav_resize_delta, visibility_rect.Min - window->Pos - window->Size); + const float resize_step = NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y); + g.NavWindowingAccumDeltaSize += nav_resize_dir * resize_step; + g.NavWindowingAccumDeltaSize = ImMax(g.NavWindowingAccumDeltaSize, visibility_rect.Min - window->Pos - window->Size); // We need Pos+Size >= visibility_rect.Min, so Size >= visibility_rect.Min - Pos, so size_delta >= visibility_rect.Min - window->Pos - window->Size g.NavWindowingToggleLayer = false; g.NavDisableMouseHover = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); - // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck. - size_target = CalcWindowSizeAfterConstraint(window, window->SizeFull + nav_resize_delta); + ImVec2 accum_floored = ImFloor(g.NavWindowingAccumDeltaSize); + if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) + { + // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck. + size_target = CalcWindowSizeAfterConstraint(window, window->SizeFull + accum_floored); + g.NavWindowingAccumDeltaSize -= accum_floored; + } } } @@ -5680,7 +5730,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s return ret_auto_fit; } -static inline void ClampWindowRect(ImGuiWindow* window, const ImRect& visibility_rect) +static inline void ClampWindowPos(ImGuiWindow* window, const ImRect& visibility_rect) { ImGuiContext& g = *GImGui; ImVec2 size_for_clamping = window->Size; @@ -5715,7 +5765,7 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) // Draw background and borders // Draw and handle scrollbars -void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size) +void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, bool handle_borders_and_resize_grips, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size) { ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; @@ -5726,7 +5776,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar window->SkipItems = false; // Draw window + handle manual resize - // As we highlight the title bar when want_focus is set, multiple reappearing windows will have have their title bar highlighted on their reappearing frame. + // As we highlight the title bar when want_focus is set, multiple reappearing windows will have their title bar highlighted on their reappearing frame. const float window_rounding = window->WindowRounding; const float window_border_size = window->WindowBorderSize; if (window->Collapsed) @@ -5780,7 +5830,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar Scrollbar(ImGuiAxis_Y); // Render resize grips (after their input handling so we don't have a frame of latency) - if (!(flags & ImGuiWindowFlags_NoResize)) + if (handle_borders_and_resize_grips && !(flags & ImGuiWindowFlags_NoResize)) { for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) { @@ -5794,7 +5844,8 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar } // Borders - RenderWindowOuterBorders(window); + if (handle_borders_and_resize_grips) + RenderWindowOuterBorders(window); } } @@ -5923,7 +5974,9 @@ static ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) for (int i = g.OpenPopupStack.Size - 1; i >= 0; i--) { ImGuiWindow* popup_window = g.OpenPopupStack.Data[i].Window; - if (popup_window == NULL || !popup_window->WasActive || !(popup_window->Flags & ImGuiWindowFlags_Modal)) // Check WasActive, because this code may run before popup renders on current frame. + if (popup_window == NULL || !(popup_window->Flags & ImGuiWindowFlags_Modal)) + continue; + if (!popup_window->Active && !popup_window->WasActive) // Check WasActive, because this code may run before popup renders on current frame, also check Active to handle newly created windows. continue; if (IsWindowWithinBeginStackOf(window, popup_window)) // Window is rendered over last modal, no render order change needed. break; @@ -5954,8 +6007,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const bool window_just_created = (window == NULL); if (window_just_created) window = CreateNewWindow(name, flags); - else - UpdateWindowInFocusOrderList(window, window_just_created, flags); // Automatically disable manual moving/resizing when NoInputs is set if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) @@ -5983,6 +6034,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update Flags, LastFrameActive, BeginOrderXXX fields if (first_begin_of_the_frame) { + UpdateWindowInFocusOrderList(window, window_just_created, flags); window->Flags = (ImGuiWindowFlags)flags; window->LastFrameActive = current_frame; window->LastTimeActive = (float)g.Time; @@ -6011,25 +6063,31 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window_stack_data.ParentLastItemDataBackup = g.LastItemData; window_stack_data.StackSizesOnBegin.SetToCurrentState(); g.CurrentWindowStack.push_back(window_stack_data); - g.CurrentWindow = NULL; if (flags & ImGuiWindowFlags_ChildMenu) g.BeginMenuCount++; + // Update ->RootWindow and others pointers (before any possible call to FocusWindow) + if (first_begin_of_the_frame) + { + UpdateWindowParentAndRootLinks(window, flags, parent_window); + window->ParentWindowInBeginStack = parent_window_in_stack; + } + + // Add to focus scope stack + PushFocusScope(window->ID); + window->NavRootFocusScopeId = g.CurrentFocusScopeId; + g.CurrentWindow = NULL; + + // Add to popup stack if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; popup_ref.Window = window; + popup_ref.ParentNavLayer = parent_window_in_stack->DC.NavLayerCurrent; g.BeginPopupStack.push_back(popup_ref); window->PopupId = popup_ref.PopupId; } - // Update ->RootWindow and others pointers (before any possible call to FocusWindow) - if (first_begin_of_the_frame) - { - UpdateWindowParentAndRootLinks(window, flags, parent_window); - window->ParentWindowInBeginStack = parent_window_in_stack; - } - // Process SetNextWindow***() calls // (FIXME: Consider splitting the HasXXX flags into X/Y components bool window_pos_set_by_api = false; @@ -6085,6 +6143,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { // Initialize const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) + const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); window->Active = true; window->HasCloseButton = (p_open != NULL); window->ClipRect = ImVec4(-FLT_MAX, -FLT_MAX, +FLT_MAX, +FLT_MAX); @@ -6111,7 +6170,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // UPDATE CONTENTS SIZE, UPDATE HIDDEN STATUS // Update contents size from last frame for auto-fitting (or use explicit size) - const bool window_just_appearing_after_hidden_for_resize = (window->HiddenFramesCannotSkipItems > 0); CalcWindowContentSizes(window, &window->ContentSize, &window->ContentSizeIdeal); if (window->HiddenFramesCanSkipItems > 0) window->HiddenFramesCanSkipItems--; @@ -6141,6 +6199,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // SELECT VIEWPORT // FIXME-VIEWPORT: In the docking/viewport branch, this is the point where we select the current viewport (which may affect the style) + + ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); + SetWindowViewport(window, viewport); SetCurrentWindow(window); // LOCK BORDER SIZE AND PADDING FOR THE FRAME (so that altering them doesn't cause inconsistencies) @@ -6157,6 +6218,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; + bool use_current_size_for_scrollbar_x = window_just_created; + bool use_current_size_for_scrollbar_y = window_just_created; + // Collapse window by double-clicking on title bar // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse)) @@ -6168,6 +6232,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window->WantCollapseToggle) { window->Collapsed = !window->Collapsed; + if (!window->Collapsed) + use_current_size_for_scrollbar_y = true; MarkIniSettingsDirty(window); } } @@ -6179,10 +6245,17 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // SIZE + // Outer Decoration Sizes + // (we need to clear ScrollbarSize immediatly as CalcWindowAutoFitSize() needs it and can be called from other locations). + const ImVec2 scrollbar_sizes_from_last_frame = window->ScrollbarSizes; + window->DecoOuterSizeX1 = 0.0f; + window->DecoOuterSizeX2 = 0.0f; + window->DecoOuterSizeY1 = window->TitleBarHeight() + window->MenuBarHeight(); + window->DecoOuterSizeY2 = 0.0f; + window->ScrollbarSizes = ImVec2(0.0f, 0.0f); + // Calculate auto-fit size, handle automatic resize const ImVec2 size_auto_fit = CalcWindowAutoFitSize(window, window->ContentSizeIdeal); - bool use_current_size_for_scrollbar_x = window_just_created; - bool use_current_size_for_scrollbar_y = window_just_created; if ((flags & ImGuiWindowFlags_AlwaysAutoResize) && !window->Collapsed) { // Using SetNextWindowSize() overrides ImGuiWindowFlags_AlwaysAutoResize, so it can be used on tooltips/popups, etc. @@ -6219,9 +6292,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->SizeFull = CalcWindowSizeAfterConstraint(window, window->SizeFull); window->Size = window->Collapsed && !(flags & ImGuiWindowFlags_ChildWindow) ? window->TitleBarRect().GetSize() : window->SizeFull; - // Decoration size - const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); - // POSITION // Popup latch its initial position, will position itself when it appears next frame @@ -6254,7 +6324,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Calculate the range of allowed position for that window (to be movable and visible past safe area padding) // When clamping to stay visible, we will enforce that window->Pos stays inside of visibility_rect. - ImGuiViewportP* viewport = (ImGuiViewportP*)(void*)GetMainViewport(); ImRect viewport_rect(viewport->GetMainRect()); ImRect viewport_work_rect(viewport->GetWorkRect()); ImVec2 visibility_padding = ImMax(style.DisplayWindowPadding, style.DisplaySafeAreaPadding); @@ -6262,9 +6331,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Clamp position/size so window stays visible within its viewport or monitor // Ignore zero-sized display explicitly to avoid losing positions if a window manager reports zero-sized window when initializing or minimizing. - if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) + if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow)) if (viewport_rect.GetWidth() > 0.0f && viewport_rect.GetHeight() > 0.0f) - ClampWindowRect(window, visibility_rect); + ClampWindowPos(window, visibility_rect); window->Pos = ImFloor(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) @@ -6301,6 +6370,18 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } } + // [Test Engine] Register whole window in the item system +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (g.TestEngineHookItems) + { + IM_ASSERT(window->IDStack.Size == 1); + window->IDStack.Size = 0; // As window->IDStack[0] == window->ID here, make sure TestEngine doesn't erroneously see window as parent of itself. + IMGUI_TEST_ENGINE_ITEM_ADD(window->Rect(), window->ID); + IMGUI_TEST_ENGINE_ITEM_INFO(window->ID, window->Name, (g.HoveredWindow == window) ? ImGuiItemStatusFlags_HoveredRect : 0); + window->IDStack.Size = 1; + } +#endif + // Handle manual resize: Resize Grips, Borders, Gamepad int border_held = -1; ImU32 resize_grip_col[4] = {}; @@ -6317,9 +6398,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (!window->Collapsed) { // When reading the current size we need to read it after size constraints have been applied. - // When we use InnerRect here we are intentionally reading last frame size, same for ScrollbarSizes values before we set them again. - ImVec2 avail_size_from_current_frame = ImVec2(window->SizeFull.x, window->SizeFull.y - decoration_up_height); - ImVec2 avail_size_from_last_frame = window->InnerRect.GetSize() + window->ScrollbarSizes; + // Intentionally use previous frame values for InnerRect and ScrollbarSizes. + // And when we use window->DecorationUp here it doesn't have ScrollbarSizes.y applied yet. + ImVec2 avail_size_from_current_frame = ImVec2(window->SizeFull.x, window->SizeFull.y - (window->DecoOuterSizeY1 + window->DecoOuterSizeY2)); + ImVec2 avail_size_from_last_frame = window->InnerRect.GetSize() + scrollbar_sizes_from_last_frame; ImVec2 needed_size_from_last_frame = window_just_created ? ImVec2(0, 0) : window->ContentSize + window->WindowPadding * 2.0f; float size_x_for_scrollbars = use_current_size_for_scrollbar_x ? avail_size_from_current_frame.x : avail_size_from_last_frame.x; float size_y_for_scrollbars = use_current_size_for_scrollbar_y ? avail_size_from_current_frame.y : avail_size_from_last_frame.y; @@ -6329,10 +6411,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window->ScrollbarX && !window->ScrollbarY) window->ScrollbarY = (needed_size_from_last_frame.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar); window->ScrollbarSizes = ImVec2(window->ScrollbarY ? style.ScrollbarSize : 0.0f, window->ScrollbarX ? style.ScrollbarSize : 0.0f); + + // Amend the partially filled window->DecorationXXX values. + window->DecoOuterSizeX2 += window->ScrollbarSizes.x; + window->DecoOuterSizeY2 += window->ScrollbarSizes.y; } // UPDATE RECTANGLES (1- THOSE NOT AFFECTED BY SCROLLING) - // Update various regions. Variables they depends on should be set above in this function. + // Update various regions. Variables they depend on should be set above in this function. // We set this up after processing the resize grip so that our rectangles doesn't lag by a frame. // Outer rectangle @@ -6352,10 +6438,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // - ScrollToRectEx() // - NavUpdatePageUpPageDown() // - Scrollbar() - window->InnerRect.Min.x = window->Pos.x; - window->InnerRect.Min.y = window->Pos.y + decoration_up_height; - window->InnerRect.Max.x = window->Pos.x + window->Size.x - window->ScrollbarSizes.x; - window->InnerRect.Max.y = window->Pos.y + window->Size.y - window->ScrollbarSizes.y; + window->InnerRect.Min.x = window->Pos.x + window->DecoOuterSizeX1; + window->InnerRect.Min.y = window->Pos.y + window->DecoOuterSizeY1; + window->InnerRect.Max.x = window->Pos.x + window->Size.x - window->DecoOuterSizeX2; + window->InnerRect.Max.y = window->Pos.y + window->Size.y - window->DecoOuterSizeY2; // Inner clipping rectangle. // Will extend a little bit outside the normal work region. @@ -6388,6 +6474,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Apply scrolling window->Scroll = CalcNextScrollFromScrollTargetAndClamp(window); window->ScrollTarget = ImVec2(FLT_MAX, FLT_MAX); + window->DecoInnerSizeX1 = window->DecoInnerSizeY1 = 0.0f; // DRAWING @@ -6417,7 +6504,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Handle title bar, scrollbar, resize grips and resize borders const ImGuiWindow* window_to_highlight = g.NavWindowingTarget ? g.NavWindowingTarget : g.NavWindow; const bool title_bar_is_highlight = want_focus || (window_to_highlight && window->RootWindowForTitleBarHighlight == window_to_highlight->RootWindowForTitleBarHighlight); - RenderWindowDecorations(window, title_bar_rect, title_bar_is_highlight, resize_grip_count, resize_grip_col, resize_grip_draw_size); + const bool handle_borders_and_resize_grips = true; // This exists to facilitate merge with 'docking' branch. + RenderWindowDecorations(window, title_bar_rect, title_bar_is_highlight, handle_borders_and_resize_grips, resize_grip_count, resize_grip_col, resize_grip_draw_size); if (render_decorations_in_parent) window->DrawList = &window->DrawListInst; @@ -6432,8 +6520,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // - BeginTabBar() for right-most edge const bool allow_scrollbar_x = !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar); const bool allow_scrollbar_y = !(flags & ImGuiWindowFlags_NoScrollbar); - const float work_rect_size_x = (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : ImMax(allow_scrollbar_x ? window->ContentSize.x : 0.0f, window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); - const float work_rect_size_y = (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : ImMax(allow_scrollbar_y ? window->ContentSize.y : 0.0f, window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); + const float work_rect_size_x = (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : ImMax(allow_scrollbar_x ? window->ContentSize.x : 0.0f, window->Size.x - window->WindowPadding.x * 2.0f - (window->DecoOuterSizeX1 + window->DecoOuterSizeX2))); + const float work_rect_size_y = (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : ImMax(allow_scrollbar_y ? window->ContentSize.y : 0.0f, window->Size.y - window->WindowPadding.y * 2.0f - (window->DecoOuterSizeY1 + window->DecoOuterSizeY2))); window->WorkRect.Min.x = ImFloor(window->InnerRect.Min.x - window->Scroll.x + ImMax(window->WindowPadding.x, window->WindowBorderSize)); window->WorkRect.Min.y = ImFloor(window->InnerRect.Min.y - window->Scroll.y + ImMax(window->WindowPadding.y, window->WindowBorderSize)); window->WorkRect.Max.x = window->WorkRect.Min.x + work_rect_size_x; @@ -6444,21 +6532,21 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // FIXME-OBSOLETE: window->ContentRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. // Used by: // - Mouse wheel scrolling + many other things - window->ContentRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x; - window->ContentRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + decoration_up_height; - window->ContentRegionRect.Max.x = window->ContentRegionRect.Min.x + (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : (window->Size.x - window->WindowPadding.x * 2.0f - window->ScrollbarSizes.x)); - window->ContentRegionRect.Max.y = window->ContentRegionRect.Min.y + (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : (window->Size.y - window->WindowPadding.y * 2.0f - decoration_up_height - window->ScrollbarSizes.y)); + window->ContentRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x + window->DecoOuterSizeX1; + window->ContentRegionRect.Min.y = window->Pos.y - window->Scroll.y + window->WindowPadding.y + window->DecoOuterSizeY1; + window->ContentRegionRect.Max.x = window->ContentRegionRect.Min.x + (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : (window->Size.x - window->WindowPadding.x * 2.0f - (window->DecoOuterSizeX1 + window->DecoOuterSizeX2))); + window->ContentRegionRect.Max.y = window->ContentRegionRect.Min.y + (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : (window->Size.y - window->WindowPadding.y * 2.0f - (window->DecoOuterSizeY1 + window->DecoOuterSizeY2))); // Setup drawing context // (NB: That term "drawing context / DC" lost its meaning a long time ago. Initially was meant to hold transient data only. Nowadays difference between window-> and window->DC-> is dubious.) - window->DC.Indent.x = 0.0f + window->WindowPadding.x - window->Scroll.x; + window->DC.Indent.x = window->DecoOuterSizeX1 + window->WindowPadding.x - window->Scroll.x; window->DC.GroupOffset.x = 0.0f; window->DC.ColumnsOffset.x = 0.0f; // Record the loss of precision of CursorStartPos which can happen due to really large scrolling amount. // This is used by clipper to compensate and fix the most common use case of large scroll area. Easy and cheap, next best thing compared to switching everything to double or ImU64. - double start_pos_highp_x = (double)window->Pos.x + window->WindowPadding.x - (double)window->Scroll.x + window->DC.ColumnsOffset.x; - double start_pos_highp_y = (double)window->Pos.y + window->WindowPadding.y - (double)window->Scroll.y + decoration_up_height; + double start_pos_highp_x = (double)window->Pos.x + window->WindowPadding.x - (double)window->Scroll.x + window->DecoOuterSizeX1 + window->DC.ColumnsOffset.x; + double start_pos_highp_y = (double)window->Pos.y + window->WindowPadding.y - (double)window->Scroll.y + window->DecoOuterSizeY1; window->DC.CursorStartPos = ImVec2((float)start_pos_highp_x, (float)start_pos_highp_y); window->DC.CursorStartPosLossyness = ImVec2((float)(start_pos_highp_x - window->DC.CursorStartPos.x), (float)(start_pos_highp_y - window->DC.CursorStartPos.y)); window->DC.CursorPos = window->DC.CursorStartPos; @@ -6467,6 +6555,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.IdealMaxPos = window->DC.CursorStartPos; window->DC.CurrLineSize = window->DC.PrevLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset = 0.0f; + window->DC.IsSameLine = window->DC.IsSetPos = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.NavLayersActiveMask = window->DC.NavLayersActiveMaskNext; @@ -6514,7 +6603,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) /* //if (g.NavWindow == window && g.ActiveId == 0) if (g.ActiveId == window->MoveId) - if (g.IO.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) + if (g.IO.KeyCtrl && IsKeyPressed(ImGuiKey_C)) LogToClipboard(); */ @@ -6522,6 +6611,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // This is useful to allow creating context menus on title bar only, etc. SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect); + // [DEBUG] +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (g.DebugLocateId != 0 && (window->ID == g.DebugLocateId || window->MoveId == g.DebugLocateId)) + DebugLocateItemResolveWithLastItem(); +#endif + + // [Test Engine] Register title bar / tab #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) IMGUI_TEST_ENGINE_ITEM_ADD(g.LastItemData.Rect, g.LastItemData.ID); @@ -6533,9 +6629,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) SetCurrentWindow(window); } - // Pull/inherit current state - window->DC.NavFocusScopeIdCurrent = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window->DC.NavFocusScopeIdCurrent : window->GetID("#FOCUSSCOPE"); // Inherit from parent only // -V595 - PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); // Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default "Debug" window is unused) @@ -6613,11 +6706,15 @@ void ImGui::End() if (window->DC.CurrentColumns) EndColumns(); PopClipRect(); // Inner window clip rectangle + PopFocusScope(); // Stop logging if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging LogFinish(); + if (window->DC.IsSetPos) + ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + // Pop from window stack g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; if (window->Flags & ImGuiWindowFlags_ChildMenu) @@ -6714,20 +6811,17 @@ void ImGui::FocusWindow(ImGuiWindow* window) if (g.NavWindow != window) { - g.NavWindow = window; + SetNavWindow(window); if (window && g.NavDisableMouseHover) g.NavMousePosDirty = true; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId - g.NavFocusScopeId = 0; - g.NavIdIsAlive = false; g.NavLayer = ImGuiNavLayer_Main; - g.NavInitRequest = g.NavMoveSubmitted = g.NavMoveScoringItems = false; - NavUpdateAnyRequestFlag(); - //IMGUI_DEBUG_LOG("FocusWindow(\"%s\")\n", window ? window->Name : NULL); - } + g.NavFocusScopeId = window ? window->NavRootFocusScopeId : 0; + g.NavIdIsAlive = false; - // Close popups if any - ClosePopupsOverWindow(window, false); + // Close popups if any + ClosePopupsOverWindow(window, false); + } // Move the root window to the top of the pile IM_ASSERT(window == NULL || window->RootWindow != NULL); @@ -7065,6 +7159,9 @@ void ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond) const ImVec2 old_pos = window->Pos; window->Pos = ImFloor(pos); ImVec2 offset = window->Pos - old_pos; + if (offset.x == 0.0f && offset.y == 0.0f) + return; + MarkIniSettingsDirty(window); window->DC.CursorPos += offset; // As we happen to move the window while it is being appended to (which is a bad idea - will smear) let's at least offset the cursor window->DC.CursorMaxPos += offset; // And more importantly we need to offset CursorMaxPos/CursorStartPos this so ContentSize calculation doesn't get affected. window->DC.IdealMaxPos += offset; @@ -7099,26 +7196,19 @@ void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond con window->SetWindowSizeAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); // Set - if (size.x > 0.0f) - { - window->AutoFitFramesX = 0; - window->SizeFull.x = IM_FLOOR(size.x); - } - else - { - window->AutoFitFramesX = 2; + ImVec2 old_size = window->SizeFull; + window->AutoFitFramesX = (size.x <= 0.0f) ? 2 : 0; + window->AutoFitFramesY = (size.y <= 0.0f) ? 2 : 0; + if (size.x <= 0.0f) window->AutoFitOnlyGrows = false; - } - if (size.y > 0.0f) - { - window->AutoFitFramesY = 0; - window->SizeFull.y = IM_FLOOR(size.y); - } else - { - window->AutoFitFramesY = 2; + window->SizeFull.x = IM_FLOOR(size.x); + if (size.y <= 0.0f) window->AutoFitOnlyGrows = false; - } + else + window->SizeFull.y = IM_FLOOR(size.y); + if (old_size.x != window->SizeFull.x || old_size.y != window->SizeFull.y) + MarkIniSettingsDirty(window); } void ImGui::SetWindowSize(const ImVec2& size, ImGuiCond cond) @@ -7278,179 +7368,1439 @@ ImVec2 ImGui::GetFontTexUvWhitePixel() return GImGui->DrawListSharedData.TexUvWhitePixel; } -void ImGui::SetWindowFontScale(float scale) +void ImGui::SetWindowFontScale(float scale) +{ + IM_ASSERT(scale > 0.0f); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = GetCurrentWindow(); + window->FontWindowScale = scale; + g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); +} + +void ImGui::ActivateItem(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.NavNextActivateId = id; + g.NavNextActivateFlags = ImGuiActivateFlags_None; +} + +void ImGui::PushFocusScope(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.FocusScopeStack.push_back(id); + g.CurrentFocusScopeId = id; +} + +void ImGui::PopFocusScope() +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.FocusScopeStack.Size > 0); // Too many PopFocusScope() ? + g.FocusScopeStack.pop_back(); + g.CurrentFocusScopeId = g.FocusScopeStack.Size ? g.FocusScopeStack.back() : 0; +} + +// Note: this will likely be called ActivateItem() once we rework our Focus/Activation system! +void ImGui::SetKeyboardFocusHere(int offset) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(offset >= -1); // -1 is allowed but not below + IMGUI_DEBUG_LOG_ACTIVEID("SetKeyboardFocusHere(%d) in window \"%s\"\n", offset, window->Name); + + // It makes sense in the vast majority of cases to never interrupt a drag and drop. + // When we refactor this function into ActivateItem() we may want to make this an option. + // MovingWindow is protected from most user inputs using SetActiveIdUsingNavAndKeys(), but + // is also automatically dropped in the event g.ActiveId is stolen. + if (g.DragDropActive || g.MovingWindow != NULL) + { + IMGUI_DEBUG_LOG_ACTIVEID("SetKeyboardFocusHere() ignored while DragDropActive!\n"); + return; + } + + SetNavWindow(window); + + ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; + NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + if (offset == -1) + { + NavMoveRequestResolveWithLastItem(&g.NavMoveResultLocal); + } + else + { + g.NavTabbingDir = 1; + g.NavTabbingCounter = offset + 1; + } +} + +void ImGui::SetItemDefaultFocus() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (!window->Appearing) + return; + if (g.NavWindow != window->RootWindowForNav || (!g.NavInitRequest && g.NavInitResultId == 0) || g.NavLayer != window->DC.NavLayerCurrent) + return; + + g.NavInitRequest = false; + g.NavInitResultId = g.LastItemData.ID; + g.NavInitResultRectRel = WindowRectAbsToRel(window, g.LastItemData.Rect); + NavUpdateAnyRequestFlag(); + + // Scroll could be done in NavInitRequestApplyResult() via an opt-in flag (we however don't want regular init requests to scroll) + if (!IsItemVisible()) + ScrollToRectEx(window, g.LastItemData.Rect, ImGuiScrollFlags_None); +} + +void ImGui::SetStateStorage(ImGuiStorage* tree) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + window->DC.StateStorage = tree ? tree : &window->StateStorage; +} + +ImGuiStorage* ImGui::GetStateStorage() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->DC.StateStorage; +} + +void ImGui::PushID(const char* str_id) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetID(str_id); + window->IDStack.push_back(id); +} + +void ImGui::PushID(const char* str_id_begin, const char* str_id_end) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetID(str_id_begin, str_id_end); + window->IDStack.push_back(id); +} + +void ImGui::PushID(const void* ptr_id) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetID(ptr_id); + window->IDStack.push_back(id); +} + +void ImGui::PushID(int int_id) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiID id = window->GetID(int_id); + window->IDStack.push_back(id); +} + +// Push a given id value ignoring the ID stack as a seed. +void ImGui::PushOverrideID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (g.DebugHookIdInfo == id) + DebugHookIdInfo(id, ImGuiDataType_ID, NULL, NULL); + window->IDStack.push_back(id); +} + +// Helper to avoid a common series of PushOverrideID -> GetID() -> PopID() call +// (note that when using this pattern, TestEngine's "Stack Tool" will tend to not display the intermediate stack level. +// for that to work we would need to do PushOverrideID() -> ItemAdd() -> PopID() which would alter widget code a little more) +ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) +{ + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); + ImGuiContext& g = *GImGui; + if (g.DebugHookIdInfo == id) + DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); + return id; +} + +void ImGui::PopID() +{ + ImGuiWindow* window = GImGui->CurrentWindow; + IM_ASSERT(window->IDStack.Size > 1); // Too many PopID(), or could be popping in a wrong/different window? + window->IDStack.pop_back(); +} + +ImGuiID ImGui::GetID(const char* str_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id); +} + +ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(str_id_begin, str_id_end); +} + +ImGuiID ImGui::GetID(const void* ptr_id) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->GetID(ptr_id); +} + +bool ImGui::IsRectVisible(const ImVec2& size) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); +} + +bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); +} + + +//----------------------------------------------------------------------------- +// [SECTION] INPUTS +//----------------------------------------------------------------------------- +// - GetKeyData() [Internal] +// - GetKeyIndex() [Internal] +// - GetKeyName() +// - GetKeyChordName() [Internal] +// - CalcTypematicRepeatAmount() [Internal] +// - GetTypematicRepeatRate() [Internal] +// - GetKeyPressedAmount() [Internal] +// - GetKeyMagnitude2d() [Internal] +//----------------------------------------------------------------------------- +// - UpdateKeyRoutingTable() [Internal] +// - GetRoutingIdFromOwnerId() [Internal] +// - GetShortcutRoutingData() [Internal] +// - CalcRoutingScore() [Internal] +// - SetShortcutRouting() [Internal] +// - TestShortcutRouting() [Internal] +//----------------------------------------------------------------------------- +// - IsKeyDown() +// - IsKeyPressed() +// - IsKeyReleased() +//----------------------------------------------------------------------------- +// - IsMouseDown() +// - IsMouseClicked() +// - IsMouseReleased() +// - IsMouseDoubleClicked() +// - GetMouseClickedCount() +// - IsMouseHoveringRect() [Internal] +// - IsMouseDragPastThreshold() [Internal] +// - IsMouseDragging() +// - GetMousePos() +// - GetMousePosOnOpeningCurrentPopup() +// - IsMousePosValid() +// - IsAnyMouseDown() +// - GetMouseDragDelta() +// - ResetMouseDragDelta() +// - GetMouseCursor() +// - SetMouseCursor() +//----------------------------------------------------------------------------- +// - UpdateAliasKey() +// - GetMergedModsFromKeys() +// - UpdateKeyboardInputs() +// - UpdateMouseInputs() +//----------------------------------------------------------------------------- +// - LockWheelingWindow [Internal] +// - FindBestWheelingWindow [Internal] +// - UpdateMouseWheel() [Internal] +//----------------------------------------------------------------------------- +// - SetNextFrameWantCaptureKeyboard() +// - SetNextFrameWantCaptureMouse() +//----------------------------------------------------------------------------- +// - GetInputSourceName() [Internal] +// - DebugPrintInputEvent() [Internal] +// - UpdateInputEvents() [Internal] +//----------------------------------------------------------------------------- +// - GetKeyOwner() [Internal] +// - TestKeyOwner() [Internal] +// - SetKeyOwner() [Internal] +// - SetItemKeyOwner() [Internal] +// - Shortcut() [Internal] +//----------------------------------------------------------------------------- + +ImGuiKeyData* ImGui::GetKeyData(ImGuiKey key) +{ + ImGuiContext& g = *GImGui; + + // Special storage location for mods + if (key & ImGuiMod_Mask_) + key = ConvertSingleModFlagToKey(key); + + int index; +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + IM_ASSERT(key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_NamedKey_END); + if (IsLegacyKey(key)) + index = (g.IO.KeyMap[key] != -1) ? g.IO.KeyMap[key] : key; // Remap native->imgui or imgui->native + else + index = key; +#else + IM_ASSERT(IsNamedKey(key) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend & user code."); + index = key - ImGuiKey_NamedKey_BEGIN; +#endif + return &g.IO.KeysData[index]; +} + +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO +ImGuiKey ImGui::GetKeyIndex(ImGuiKey key) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(IsNamedKey(key)); + const ImGuiKeyData* key_data = GetKeyData(key); + return (ImGuiKey)(key_data - g.IO.KeysData); +} +#endif + +// Those names a provided for debugging purpose and are not meant to be saved persistently not compared. +static const char* const GKeyNames[] = +{ + "Tab", "LeftArrow", "RightArrow", "UpArrow", "DownArrow", "PageUp", "PageDown", + "Home", "End", "Insert", "Delete", "Backspace", "Space", "Enter", "Escape", + "LeftCtrl", "LeftShift", "LeftAlt", "LeftSuper", "RightCtrl", "RightShift", "RightAlt", "RightSuper", "Menu", + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", + "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", + "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", + "Apostrophe", "Comma", "Minus", "Period", "Slash", "Semicolon", "Equal", "LeftBracket", + "Backslash", "RightBracket", "GraveAccent", "CapsLock", "ScrollLock", "NumLock", "PrintScreen", + "Pause", "Keypad0", "Keypad1", "Keypad2", "Keypad3", "Keypad4", "Keypad5", "Keypad6", + "Keypad7", "Keypad8", "Keypad9", "KeypadDecimal", "KeypadDivide", "KeypadMultiply", + "KeypadSubtract", "KeypadAdd", "KeypadEnter", "KeypadEqual", + "GamepadStart", "GamepadBack", + "GamepadFaceLeft", "GamepadFaceRight", "GamepadFaceUp", "GamepadFaceDown", + "GamepadDpadLeft", "GamepadDpadRight", "GamepadDpadUp", "GamepadDpadDown", + "GamepadL1", "GamepadR1", "GamepadL2", "GamepadR2", "GamepadL3", "GamepadR3", + "GamepadLStickLeft", "GamepadLStickRight", "GamepadLStickUp", "GamepadLStickDown", + "GamepadRStickLeft", "GamepadRStickRight", "GamepadRStickUp", "GamepadRStickDown", + "MouseLeft", "MouseRight", "MouseMiddle", "MouseX1", "MouseX2", "MouseWheelX", "MouseWheelY", + "ModCtrl", "ModShift", "ModAlt", "ModSuper", // ReservedForModXXX are showing the ModXXX names. +}; +IM_STATIC_ASSERT(ImGuiKey_NamedKey_COUNT == IM_ARRAYSIZE(GKeyNames)); + +const char* ImGui::GetKeyName(ImGuiKey key) +{ +#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO + IM_ASSERT((IsNamedKey(key) || key == ImGuiKey_None) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); +#else + if (IsLegacyKey(key)) + { + ImGuiIO& io = GetIO(); + if (io.KeyMap[key] == -1) + return "N/A"; + IM_ASSERT(IsNamedKey((ImGuiKey)io.KeyMap[key])); + key = (ImGuiKey)io.KeyMap[key]; + } +#endif + if (key == ImGuiKey_None) + return "None"; + if (key & ImGuiMod_Mask_) + key = ConvertSingleModFlagToKey(key); + if (!IsNamedKey(key)) + return "Unknown"; + + return GKeyNames[key - ImGuiKey_NamedKey_BEGIN]; +} + +// ImGuiMod_Shortcut is translated to either Ctrl or Super. +void ImGui::GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size) +{ + ImGuiContext& g = *GImGui; + if (key_chord & ImGuiMod_Shortcut) + key_chord = ConvertShortcutMod(key_chord); + ImFormatString(out_buf, (size_t)out_buf_size, "%s%s%s%s%s", + (key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "", + (key_chord & ImGuiMod_Shift) ? "Shift+" : "", + (key_chord & ImGuiMod_Alt) ? "Alt+" : "", + (key_chord & ImGuiMod_Super) ? (g.IO.ConfigMacOSXBehaviors ? "Cmd+" : "Super+") : "", + GetKeyName((ImGuiKey)(key_chord & ~ImGuiMod_Mask_))); +} + +// t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) +// t1 = current time (e.g.: g.Time) +// An event is triggered at: +// t = 0.0f t = repeat_delay, t = repeat_delay + repeat_rate*N +int ImGui::CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate) +{ + if (t1 == 0.0f) + return 1; + if (t0 >= t1) + return 0; + if (repeat_rate <= 0.0f) + return (t0 < repeat_delay) && (t1 >= repeat_delay); + const int count_t0 = (t0 < repeat_delay) ? -1 : (int)((t0 - repeat_delay) / repeat_rate); + const int count_t1 = (t1 < repeat_delay) ? -1 : (int)((t1 - repeat_delay) / repeat_rate); + const int count = count_t1 - count_t0; + return count; +} + +void ImGui::GetTypematicRepeatRate(ImGuiInputFlags flags, float* repeat_delay, float* repeat_rate) +{ + ImGuiContext& g = *GImGui; + switch (flags & ImGuiInputFlags_RepeatRateMask_) + { + case ImGuiInputFlags_RepeatRateNavMove: *repeat_delay = g.IO.KeyRepeatDelay * 0.72f; *repeat_rate = g.IO.KeyRepeatRate * 0.80f; return; + case ImGuiInputFlags_RepeatRateNavTweak: *repeat_delay = g.IO.KeyRepeatDelay * 0.72f; *repeat_rate = g.IO.KeyRepeatRate * 0.30f; return; + case ImGuiInputFlags_RepeatRateDefault: default: *repeat_delay = g.IO.KeyRepeatDelay * 1.00f; *repeat_rate = g.IO.KeyRepeatRate * 1.00f; return; + } +} + +// Return value representing the number of presses in the last time period, for the given repeat rate +// (most often returns 0 or 1. The result is generally only >1 when RepeatRate is smaller than DeltaTime, aka large DeltaTime or fast RepeatRate) +int ImGui::GetKeyPressedAmount(ImGuiKey key, float repeat_delay, float repeat_rate) +{ + ImGuiContext& g = *GImGui; + const ImGuiKeyData* key_data = GetKeyData(key); + if (!key_data->Down) // In theory this should already be encoded as (DownDuration < 0.0f), but testing this facilitates eating mechanism (until we finish work on key ownership) + return 0; + const float t = key_data->DownDuration; + return CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, repeat_delay, repeat_rate); +} + +// Return 2D vector representing the combination of four cardinal direction, with analog value support (for e.g. ImGuiKey_GamepadLStick* values). +ImVec2 ImGui::GetKeyMagnitude2d(ImGuiKey key_left, ImGuiKey key_right, ImGuiKey key_up, ImGuiKey key_down) +{ + return ImVec2( + GetKeyData(key_right)->AnalogValue - GetKeyData(key_left)->AnalogValue, + GetKeyData(key_down)->AnalogValue - GetKeyData(key_up)->AnalogValue); +} + +// Rewrite routing data buffers to strip old entries + sort by key to make queries not touch scattered data. +// Entries D,A,B,B,A,C,B --> A,A,B,B,B,C,D +// Index A:1 B:2 C:5 D:0 --> A:0 B:2 C:5 D:6 +// See 'Metrics->Key Owners & Shortcut Routing' to visualize the result of that operation. +static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) +{ + ImGuiContext& g = *GImGui; + rt->EntriesNext.resize(0); + for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) + { + const int new_routing_start_idx = rt->EntriesNext.Size; + ImGuiKeyRoutingData* routing_entry; + for (int old_routing_idx = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; old_routing_idx != -1; old_routing_idx = routing_entry->NextEntryIndex) + { + routing_entry = &rt->Entries[old_routing_idx]; + routing_entry->RoutingCurr = routing_entry->RoutingNext; // Update entry + routing_entry->RoutingNext = ImGuiKeyOwner_None; + routing_entry->RoutingNextScore = 255; + if (routing_entry->RoutingCurr == ImGuiKeyOwner_None) + continue; + rt->EntriesNext.push_back(*routing_entry); // Write alive ones into new buffer + + // Apply routing to owner if there's no owner already (RoutingCurr == None at this point) + if (routing_entry->Mods == g.IO.KeyMods) + { + ImGuiKeyOwnerData* owner_data = ImGui::GetKeyOwnerData(key); + if (owner_data->OwnerCurr == ImGuiKeyOwner_None) + owner_data->OwnerCurr = routing_entry->RoutingCurr; + } + } + + // Rewrite linked-list + rt->Index[key - ImGuiKey_NamedKey_BEGIN] = (ImGuiKeyRoutingIndex)(new_routing_start_idx < rt->EntriesNext.Size ? new_routing_start_idx : -1); + for (int n = new_routing_start_idx; n < rt->EntriesNext.Size; n++) + rt->EntriesNext[n].NextEntryIndex = (ImGuiKeyRoutingIndex)((n + 1 < rt->EntriesNext.Size) ? n + 1 : -1); + } + rt->Entries.swap(rt->EntriesNext); // Swap new and old indexes +} + +// owner_id may be None/Any, but routing_id needs to be always be set, so we default to GetCurrentFocusScope(). +static inline ImGuiID GetRoutingIdFromOwnerId(ImGuiID owner_id) +{ + ImGuiContext& g = *GImGui; + return (owner_id != ImGuiKeyOwner_None && owner_id != ImGuiKeyOwner_Any) ? owner_id : g.CurrentFocusScopeId; +} + +ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) +{ + // Majority of shortcuts will be Key + any number of Mods + // We accept _Single_ mod with ImGuiKey_None. + // - Shortcut(ImGuiKey_S | ImGuiMod_Ctrl); // Legal + // - Shortcut(ImGuiKey_S | ImGuiMod_Ctrl | ImGuiMod_Shift); // Legal + // - Shortcut(ImGuiMod_Ctrl); // Legal + // - Shortcut(ImGuiMod_Ctrl | ImGuiMod_Shift); // Not legal + ImGuiContext& g = *GImGui; + ImGuiKeyRoutingTable* rt = &g.KeysRoutingTable; + ImGuiKeyRoutingData* routing_data; + if (key_chord & ImGuiMod_Shortcut) + key_chord = ConvertShortcutMod(key_chord); + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); + if (key == ImGuiKey_None) + key = ConvertSingleModFlagToKey(mods); + IM_ASSERT(IsNamedKey(key)); + + // Get (in the majority of case, the linked list will have one element so this should be 2 reads. + // Subsequent elements will be contiguous in memory as list is sorted/rebuilt in NewFrame). + for (ImGuiKeyRoutingIndex idx = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; idx != -1; idx = routing_data->NextEntryIndex) + { + routing_data = &rt->Entries[idx]; + if (routing_data->Mods == mods) + return routing_data; + } + + // Add to linked-list + ImGuiKeyRoutingIndex routing_data_idx = (ImGuiKeyRoutingIndex)rt->Entries.Size; + rt->Entries.push_back(ImGuiKeyRoutingData()); + routing_data = &rt->Entries[routing_data_idx]; + routing_data->Mods = (ImU16)mods; + routing_data->NextEntryIndex = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; // Setup linked list + rt->Index[key - ImGuiKey_NamedKey_BEGIN] = routing_data_idx; + return routing_data; +} + +// Current score encoding (lower is highest priority): +// - 0: ImGuiInputFlags_RouteGlobalHigh +// - 1: ImGuiInputFlags_RouteFocused (if item active) +// - 2: ImGuiInputFlags_RouteGlobal +// - 3+: ImGuiInputFlags_RouteFocused (if window in focus-stack) +// - 254: ImGuiInputFlags_RouteGlobalLow +// - 255: never route +// 'flags' should include an explicit routing policy +static int CalcRoutingScore(ImGuiWindow* location, ImGuiID owner_id, ImGuiInputFlags flags) +{ + if (flags & ImGuiInputFlags_RouteFocused) + { + ImGuiContext& g = *GImGui; + ImGuiWindow* focused = g.NavWindow; + + // ActiveID gets top priority + // (we don't check g.ActiveIdUsingAllKeys here. Routing is applied but if input ownership is tested later it may discard it) + if (owner_id != 0 && g.ActiveId == owner_id) + return 1; + + // Score based on distance to focused window (lower is better) + // Assuming both windows are submitting a routing request, + // - When Window....... is focused -> Window scores 3 (best), Window/ChildB scores 255 (no match) + // - When Window/ChildB is focused -> Window scores 4, Window/ChildB scores 3 (best) + // Assuming only WindowA is submitting a routing request, + // - When Window/ChildB is focused -> Window scores 4 (best), Window/ChildB doesn't have a score. + if (focused != NULL && focused->RootWindow == location->RootWindow) + for (int next_score = 3; focused != NULL; next_score++) + { + if (focused == location) + { + IM_ASSERT(next_score < 255); + return next_score; + } + focused = (focused->RootWindow != focused) ? focused->ParentWindow : NULL; // FIXME: This could be later abstracted as a focus path + } + return 255; + } + + // ImGuiInputFlags_RouteGlobalHigh is default, so calls without flags are not conditional + if (flags & ImGuiInputFlags_RouteGlobal) + return 2; + if (flags & ImGuiInputFlags_RouteGlobalLow) + return 254; + return 0; +} + +// Request a desired route for an input chord (key + mods). +// Return true if the route is available this frame. +// - Routes and key ownership are attributed at the beginning of next frame based on best score and mod state. +// (Conceptually this does a "Submit for next frame" + "Test for current frame". +// As such, it could be called TrySetXXX or SubmitXXX, or the Submit and Test operations should be separate.) +// - Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default) +// - Using 'owner_id == ImGuiKeyOwner_None': allows disabling/locking a shortcut. +bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +{ + ImGuiContext& g = *GImGui; + if ((flags & ImGuiInputFlags_RouteMask_) == 0) + flags |= ImGuiInputFlags_RouteGlobalHigh; // IMPORTANT: This is the default for SetShortcutRouting() but NOT Shortcut() + else + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteMask_)); // Check that only 1 routing flag is used + + if (flags & ImGuiInputFlags_RouteUnlessBgFocused) + if (g.NavWindow == NULL) + return false; + if (flags & ImGuiInputFlags_RouteAlways) + return true; + + const int score = CalcRoutingScore(g.CurrentWindow, owner_id, flags); + if (score == 255) + return false; + + // Submit routing for NEXT frame (assuming score is sufficient) + // FIXME: Could expose a way to use a "serve last" policy for same score resolution (using <= instead of <). + ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord); + const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id); + //const bool set_route = (flags & ImGuiInputFlags_ServeLast) ? (score <= routing_data->RoutingNextScore) : (score < routing_data->RoutingNextScore); + if (score < routing_data->RoutingNextScore) + { + routing_data->RoutingNext = routing_id; + routing_data->RoutingNextScore = (ImU8)score; + } + + // Return routing state for CURRENT frame + return routing_data->RoutingCurr == routing_id; +} + +// Currently unused by core (but used by tests) +// Note: this cannot be turned into GetShortcutRouting() because we do the owner_id->routing_id translation, name would be more misleading. +bool ImGui::TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id) +{ + const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id); + ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord); // FIXME: Could avoid creating entry. + return routing_data->RoutingCurr == routing_id; +} + +// Note that Dear ImGui doesn't know the meaning/semantic of ImGuiKey from 0..511: they are legacy native keycodes. +// Consider transitioning from 'IsKeyDown(MY_ENGINE_KEY_A)' (<1.87) to IsKeyDown(ImGuiKey_A) (>= 1.87) +bool ImGui::IsKeyDown(ImGuiKey key) +{ + return IsKeyDown(key, ImGuiKeyOwner_Any); +} + +bool ImGui::IsKeyDown(ImGuiKey key, ImGuiID owner_id) +{ + const ImGuiKeyData* key_data = GetKeyData(key); + if (!key_data->Down) + return false; + if (!TestKeyOwner(key, owner_id)) + return false; + return true; +} + +bool ImGui::IsKeyPressed(ImGuiKey key, bool repeat) +{ + return IsKeyPressed(key, ImGuiKeyOwner_Any, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None); +} + +// Important: unless legacy IsKeyPressed(ImGuiKey, bool repeat=true) which DEFAULT to repeat, this requires EXPLICIT repeat. +bool ImGui::IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) +{ + const ImGuiKeyData* key_data = GetKeyData(key); + if (!key_data->Down) // In theory this should already be encoded as (DownDuration < 0.0f), but testing this facilitates eating mechanism (until we finish work on key ownership) + return false; + const float t = key_data->DownDuration; + if (t < 0.0f) + return false; + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByIsKeyPressed) == 0); // Passing flags not supported by this function! + + bool pressed = (t == 0.0f); + if (!pressed && ((flags & ImGuiInputFlags_Repeat) != 0)) + { + float repeat_delay, repeat_rate; + GetTypematicRepeatRate(flags, &repeat_delay, &repeat_rate); + pressed = (t > repeat_delay) && GetKeyPressedAmount(key, repeat_delay, repeat_rate) > 0; + } + if (!pressed) + return false; + if (!TestKeyOwner(key, owner_id)) + return false; + return true; +} + +bool ImGui::IsKeyReleased(ImGuiKey key) +{ + return IsKeyReleased(key, ImGuiKeyOwner_Any); +} + +bool ImGui::IsKeyReleased(ImGuiKey key, ImGuiID owner_id) +{ + const ImGuiKeyData* key_data = GetKeyData(key); + if (key_data->DownDurationPrev < 0.0f || key_data->Down) + return false; + if (!TestKeyOwner(key, owner_id)) + return false; + return true; +} + +bool ImGui::IsMouseDown(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseDown[button] && TestKeyOwner(MouseButtonToKey(button), ImGuiKeyOwner_Any); // should be same as IsKeyDown(MouseButtonToKey(button), ImGuiKeyOwner_Any), but this allows legacy code hijacking the io.Mousedown[] array. +} + +bool ImGui::IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseDown[button] && TestKeyOwner(MouseButtonToKey(button), owner_id); // Should be same as IsKeyDown(MouseButtonToKey(button), owner_id), but this allows legacy code hijacking the io.Mousedown[] array. +} + +bool ImGui::IsMouseClicked(ImGuiMouseButton button, bool repeat) +{ + return IsMouseClicked(button, ImGuiKeyOwner_Any, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None); +} + +bool ImGui::IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (!g.IO.MouseDown[button]) // In theory this should already be encoded as (DownDuration < 0.0f), but testing this facilitates eating mechanism (until we finish work on key ownership) + return false; + const float t = g.IO.MouseDownDuration[button]; + if (t < 0.0f) + return false; + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByIsKeyPressed) == 0); // Passing flags not supported by this function! + + const bool repeat = (flags & ImGuiInputFlags_Repeat) != 0; + const bool pressed = (t == 0.0f) || (repeat && t > g.IO.KeyRepeatDelay && CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0); + if (!pressed) + return false; + + if (!TestKeyOwner(MouseButtonToKey(button), owner_id)) + return false; + + return true; +} + +bool ImGui::IsMouseReleased(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseReleased[button] && TestKeyOwner(MouseButtonToKey(button), ImGuiKeyOwner_Any); // Should be same as IsKeyReleased(MouseButtonToKey(button), ImGuiKeyOwner_Any) +} + +bool ImGui::IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseReleased[button] && TestKeyOwner(MouseButtonToKey(button), owner_id); // Should be same as IsKeyReleased(MouseButtonToKey(button), owner_id) +} + +bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseClickedCount[button] == 2 && TestKeyOwner(MouseButtonToKey(button), ImGuiKeyOwner_Any); +} + +int ImGui::GetMouseClickedCount(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseClickedCount[button]; +} + +// Test if mouse cursor is hovering given rectangle +// NB- Rectangle is clipped by our current clip setting +// NB- Expand the rectangle to be generous on imprecise inputs systems (g.Style.TouchExtraPadding) +bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool clip) +{ + ImGuiContext& g = *GImGui; + + // Clip + ImRect rect_clipped(r_min, r_max); + if (clip) + rect_clipped.ClipWith(g.CurrentWindow->ClipRect); + + // Expand for touch input + const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); + if (!rect_for_touch.Contains(g.IO.MousePos)) + return false; + return true; +} + +// Return if a mouse click/drag went past the given threshold. Valid to call during the MouseReleased frame. +// [Internal] This doesn't test if the button is pressed +bool ImGui::IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + return g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold; +} + +bool ImGui::IsMouseDragging(ImGuiMouseButton button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (!g.IO.MouseDown[button]) + return false; + return IsMouseDragPastThreshold(button, lock_threshold); +} + +ImVec2 ImGui::GetMousePos() +{ + ImGuiContext& g = *GImGui; + return g.IO.MousePos; +} + +// NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! +ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() +{ + ImGuiContext& g = *GImGui; + if (g.BeginPopupStack.Size > 0) + return g.OpenPopupStack[g.BeginPopupStack.Size - 1].OpenMousePos; + return g.IO.MousePos; +} + +// We typically use ImVec2(-FLT_MAX,-FLT_MAX) to denote an invalid mouse position. +bool ImGui::IsMousePosValid(const ImVec2* mouse_pos) +{ + // The assert is only to silence a false-positive in XCode Static Analysis. + // Because GImGui is not dereferenced in every code path, the static analyzer assume that it may be NULL (which it doesn't for other functions). + IM_ASSERT(GImGui != NULL); + const float MOUSE_INVALID = -256000.0f; + ImVec2 p = mouse_pos ? *mouse_pos : GImGui->IO.MousePos; + return p.x >= MOUSE_INVALID && p.y >= MOUSE_INVALID; +} + +// [WILL OBSOLETE] This was designed for backends, but prefer having backend maintain a mask of held mouse buttons, because upcoming input queue system will make this invalid. +bool ImGui::IsAnyMouseDown() +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < IM_ARRAYSIZE(g.IO.MouseDown); n++) + if (g.IO.MouseDown[n]) + return true; + return false; +} + +// Return the delta from the initial clicking position while the mouse button is clicked or was just released. +// This is locked and return 0.0f until the mouse moves past a distance threshold at least once. +// NB: This is only valid if IsMousePosValid(). backends in theory should always keep mouse position valid when dragging even outside the client window. +ImVec2 ImGui::GetMouseDragDelta(ImGuiMouseButton button, float lock_threshold) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + if (lock_threshold < 0.0f) + lock_threshold = g.IO.MouseDragThreshold; + if (g.IO.MouseDown[button] || g.IO.MouseReleased[button]) + if (g.IO.MouseDragMaxDistanceSqr[button] >= lock_threshold * lock_threshold) + if (IsMousePosValid(&g.IO.MousePos) && IsMousePosValid(&g.IO.MouseClickedPos[button])) + return g.IO.MousePos - g.IO.MouseClickedPos[button]; + return ImVec2(0.0f, 0.0f); +} + +void ImGui::ResetMouseDragDelta(ImGuiMouseButton button) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + // NB: We don't need to reset g.IO.MouseDragMaxDistanceSqr + g.IO.MouseClickedPos[button] = g.IO.MousePos; +} + +// Get desired mouse cursor shape. +// Important: this is meant to be used by a platform backend, it is reset in ImGui::NewFrame(), +// updated during the frame, and locked in EndFrame()/Render(). +// If you use software rendering by setting io.MouseDrawCursor then Dear ImGui will render those for you +ImGuiMouseCursor ImGui::GetMouseCursor() +{ + ImGuiContext& g = *GImGui; + return g.MouseCursor; +} + +void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) +{ + ImGuiContext& g = *GImGui; + g.MouseCursor = cursor_type; +} + +static void UpdateAliasKey(ImGuiKey key, bool v, float analog_value) +{ + IM_ASSERT(ImGui::IsAliasKey(key)); + ImGuiKeyData* key_data = ImGui::GetKeyData(key); + key_data->Down = v; + key_data->AnalogValue = analog_value; +} + +// [Internal] Do not use directly +static ImGuiKeyChord GetMergedModsFromKeys() +{ + ImGuiKeyChord mods = 0; + if (ImGui::IsKeyDown(ImGuiMod_Ctrl)) { mods |= ImGuiMod_Ctrl; } + if (ImGui::IsKeyDown(ImGuiMod_Shift)) { mods |= ImGuiMod_Shift; } + if (ImGui::IsKeyDown(ImGuiMod_Alt)) { mods |= ImGuiMod_Alt; } + if (ImGui::IsKeyDown(ImGuiMod_Super)) { mods |= ImGuiMod_Super; } + return mods; +} + +static void ImGui::UpdateKeyboardInputs() +{ + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + + // Import legacy keys or verify they are not used +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + if (io.BackendUsingLegacyKeyArrays == 0) + { + // Backend used new io.AddKeyEvent() API: Good! Verify that old arrays are never written to externally. + for (int n = 0; n < ImGuiKey_LegacyNativeKey_END; n++) + IM_ASSERT((io.KeysDown[n] == false || IsKeyDown((ImGuiKey)n)) && "Backend needs to either only use io.AddKeyEvent(), either only fill legacy io.KeysDown[] + io.KeyMap[]. Not both!"); + } + else + { + if (g.FrameCount == 0) + for (int n = ImGuiKey_LegacyNativeKey_BEGIN; n < ImGuiKey_LegacyNativeKey_END; n++) + IM_ASSERT(g.IO.KeyMap[n] == -1 && "Backend is not allowed to write to io.KeyMap[0..511]!"); + + // Build reverse KeyMap (Named -> Legacy) + for (int n = ImGuiKey_NamedKey_BEGIN; n < ImGuiKey_NamedKey_END; n++) + if (io.KeyMap[n] != -1) + { + IM_ASSERT(IsLegacyKey((ImGuiKey)io.KeyMap[n])); + io.KeyMap[io.KeyMap[n]] = n; + } + + // Import legacy keys into new ones + for (int n = ImGuiKey_LegacyNativeKey_BEGIN; n < ImGuiKey_LegacyNativeKey_END; n++) + if (io.KeysDown[n] || io.BackendUsingLegacyKeyArrays == 1) + { + const ImGuiKey key = (ImGuiKey)(io.KeyMap[n] != -1 ? io.KeyMap[n] : n); + IM_ASSERT(io.KeyMap[n] == -1 || IsNamedKey(key)); + io.KeysData[key].Down = io.KeysDown[n]; + if (key != n) + io.KeysDown[key] = io.KeysDown[n]; // Allow legacy code using io.KeysDown[GetKeyIndex()] with old backends + io.BackendUsingLegacyKeyArrays = 1; + } + if (io.BackendUsingLegacyKeyArrays == 1) + { + GetKeyData(ImGuiMod_Ctrl)->Down = io.KeyCtrl; + GetKeyData(ImGuiMod_Shift)->Down = io.KeyShift; + GetKeyData(ImGuiMod_Alt)->Down = io.KeyAlt; + GetKeyData(ImGuiMod_Super)->Down = io.KeySuper; + } + } + +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + if (io.BackendUsingLegacyNavInputArray && nav_gamepad_active) + { + #define MAP_LEGACY_NAV_INPUT_TO_KEY1(_KEY, _NAV1) do { io.KeysData[_KEY].Down = (io.NavInputs[_NAV1] > 0.0f); io.KeysData[_KEY].AnalogValue = io.NavInputs[_NAV1]; } while (0) + #define MAP_LEGACY_NAV_INPUT_TO_KEY2(_KEY, _NAV1, _NAV2) do { io.KeysData[_KEY].Down = (io.NavInputs[_NAV1] > 0.0f) || (io.NavInputs[_NAV2] > 0.0f); io.KeysData[_KEY].AnalogValue = ImMax(io.NavInputs[_NAV1], io.NavInputs[_NAV2]); } while (0) + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadFaceDown, ImGuiNavInput_Activate); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadFaceRight, ImGuiNavInput_Cancel); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadFaceLeft, ImGuiNavInput_Menu); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadFaceUp, ImGuiNavInput_Input); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadDpadLeft, ImGuiNavInput_DpadLeft); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadDpadRight, ImGuiNavInput_DpadRight); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadDpadUp, ImGuiNavInput_DpadUp); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadDpadDown, ImGuiNavInput_DpadDown); + MAP_LEGACY_NAV_INPUT_TO_KEY2(ImGuiKey_GamepadL1, ImGuiNavInput_FocusPrev, ImGuiNavInput_TweakSlow); + MAP_LEGACY_NAV_INPUT_TO_KEY2(ImGuiKey_GamepadR1, ImGuiNavInput_FocusNext, ImGuiNavInput_TweakFast); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadLStickLeft, ImGuiNavInput_LStickLeft); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadLStickRight, ImGuiNavInput_LStickRight); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadLStickUp, ImGuiNavInput_LStickUp); + MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadLStickDown, ImGuiNavInput_LStickDown); + #undef NAV_MAP_KEY + } +#endif +#endif + + // Update aliases + for (int n = 0; n < ImGuiMouseButton_COUNT; n++) + UpdateAliasKey(MouseButtonToKey(n), io.MouseDown[n], io.MouseDown[n] ? 1.0f : 0.0f); + UpdateAliasKey(ImGuiKey_MouseWheelX, io.MouseWheelH != 0.0f, io.MouseWheelH); + UpdateAliasKey(ImGuiKey_MouseWheelY, io.MouseWheel != 0.0f, io.MouseWheel); + + // Synchronize io.KeyMods and io.KeyXXX values. + // - New backends (1.87+): send io.AddKeyEvent(ImGuiMod_XXX) -> -> (here) deriving io.KeyMods + io.KeyXXX from key array. + // - Legacy backends: set io.KeyXXX bools -> (above) set key array from io.KeyXXX -> (here) deriving io.KeyMods + io.KeyXXX from key array. + // So with legacy backends the 4 values will do a unnecessary back-and-forth but it makes the code simpler and future facing. + io.KeyMods = GetMergedModsFromKeys(); + io.KeyCtrl = (io.KeyMods & ImGuiMod_Ctrl) != 0; + io.KeyShift = (io.KeyMods & ImGuiMod_Shift) != 0; + io.KeyAlt = (io.KeyMods & ImGuiMod_Alt) != 0; + io.KeySuper = (io.KeyMods & ImGuiMod_Super) != 0; + + // Clear gamepad data if disabled + if ((io.BackendFlags & ImGuiBackendFlags_HasGamepad) == 0) + for (int i = ImGuiKey_Gamepad_BEGIN; i < ImGuiKey_Gamepad_END; i++) + { + io.KeysData[i - ImGuiKey_KeysData_OFFSET].Down = false; + io.KeysData[i - ImGuiKey_KeysData_OFFSET].AnalogValue = 0.0f; + } + + // Update keys + for (int i = 0; i < ImGuiKey_KeysData_SIZE; i++) + { + ImGuiKeyData* key_data = &io.KeysData[i]; + key_data->DownDurationPrev = key_data->DownDuration; + key_data->DownDuration = key_data->Down ? (key_data->DownDuration < 0.0f ? 0.0f : key_data->DownDuration + io.DeltaTime) : -1.0f; + } + + // Update keys/input owner (named keys only): one entry per key + for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) + { + ImGuiKeyData* key_data = &io.KeysData[key - ImGuiKey_KeysData_OFFSET]; + ImGuiKeyOwnerData* owner_data = &g.KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; + owner_data->OwnerCurr = owner_data->OwnerNext; + if (!key_data->Down) // Important: ownership is released on the frame after a release. Ensure a 'MouseDown -> CloseWindow -> MouseUp' chain doesn't lead to someone else seeing the MouseUp. + owner_data->OwnerNext = ImGuiKeyOwner_None; + owner_data->LockThisFrame = owner_data->LockUntilRelease = owner_data->LockUntilRelease && key_data->Down; // Clear LockUntilRelease when key is not Down anymore + } + + UpdateKeyRoutingTable(&g.KeysRoutingTable); +} + +static void ImGui::UpdateMouseInputs() +{ + ImGuiContext& g = *GImGui; + ImGuiIO& io = g.IO; + + // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) + if (IsMousePosValid(&io.MousePos)) + io.MousePos = g.MouseLastValidPos = ImFloorSigned(io.MousePos); + + // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta + if (IsMousePosValid(&io.MousePos) && IsMousePosValid(&io.MousePosPrev)) + io.MouseDelta = io.MousePos - io.MousePosPrev; + else + io.MouseDelta = ImVec2(0.0f, 0.0f); + + // If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. + if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) + g.NavDisableMouseHover = false; + + io.MousePosPrev = io.MousePos; + for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) + { + io.MouseClicked[i] = io.MouseDown[i] && io.MouseDownDuration[i] < 0.0f; + io.MouseClickedCount[i] = 0; // Will be filled below + io.MouseReleased[i] = !io.MouseDown[i] && io.MouseDownDuration[i] >= 0.0f; + io.MouseDownDurationPrev[i] = io.MouseDownDuration[i]; + io.MouseDownDuration[i] = io.MouseDown[i] ? (io.MouseDownDuration[i] < 0.0f ? 0.0f : io.MouseDownDuration[i] + io.DeltaTime) : -1.0f; + if (io.MouseClicked[i]) + { + bool is_repeated_click = false; + if ((float)(g.Time - io.MouseClickedTime[i]) < io.MouseDoubleClickTime) + { + ImVec2 delta_from_click_pos = IsMousePosValid(&io.MousePos) ? (io.MousePos - io.MouseClickedPos[i]) : ImVec2(0.0f, 0.0f); + if (ImLengthSqr(delta_from_click_pos) < io.MouseDoubleClickMaxDist * io.MouseDoubleClickMaxDist) + is_repeated_click = true; + } + if (is_repeated_click) + io.MouseClickedLastCount[i]++; + else + io.MouseClickedLastCount[i] = 1; + io.MouseClickedTime[i] = g.Time; + io.MouseClickedPos[i] = io.MousePos; + io.MouseClickedCount[i] = io.MouseClickedLastCount[i]; + io.MouseDragMaxDistanceSqr[i] = 0.0f; + } + else if (io.MouseDown[i]) + { + // Maintain the maximum distance we reaching from the initial click position, which is used with dragging threshold + float delta_sqr_click_pos = IsMousePosValid(&io.MousePos) ? ImLengthSqr(io.MousePos - io.MouseClickedPos[i]) : 0.0f; + io.MouseDragMaxDistanceSqr[i] = ImMax(io.MouseDragMaxDistanceSqr[i], delta_sqr_click_pos); + } + + // We provide io.MouseDoubleClicked[] as a legacy service + io.MouseDoubleClicked[i] = (io.MouseClickedCount[i] == 2); + + // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation + if (io.MouseClicked[i]) + g.NavDisableMouseHover = false; + } +} + +static void LockWheelingWindow(ImGuiWindow* window, float wheel_amount) +{ + ImGuiContext& g = *GImGui; + if (window) + g.WheelingWindowReleaseTimer = ImMin(g.WheelingWindowReleaseTimer + ImAbs(wheel_amount) * WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER, WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER); + else + g.WheelingWindowReleaseTimer = 0.0f; + if (g.WheelingWindow == window) + return; + IMGUI_DEBUG_LOG_IO("LockWheelingWindow() \"%s\"\n", window ? window->Name : "NULL"); + g.WheelingWindow = window; + g.WheelingWindowRefMousePos = g.IO.MousePos; + if (window == NULL) + { + g.WheelingWindowStartFrame = -1; + g.WheelingAxisAvg = ImVec2(0.0f, 0.0f); + } +} + +static ImGuiWindow* FindBestWheelingWindow(const ImVec2& wheel) +{ + // For each axis, find window in the hierarchy that may want to use scrolling + ImGuiContext& g = *GImGui; + ImGuiWindow* windows[2] = { NULL, NULL }; + for (int axis = 0; axis < 2; axis++) + if (wheel[axis] != 0.0f) + for (ImGuiWindow* window = windows[axis] = g.HoveredWindow; window->Flags & ImGuiWindowFlags_ChildWindow; window = windows[axis] = window->ParentWindow) + { + // Bubble up into parent window if: + // - a child window doesn't allow any scrolling. + // - a child window has the ImGuiWindowFlags_NoScrollWithMouse flag. + //// - a child window doesn't need scrolling because it is already at the edge for the direction we are going in (FIXME-WIP) + const bool has_scrolling = (window->ScrollMax[axis] != 0.0f); + const bool inputs_disabled = (window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs); + //const bool scrolling_past_limits = (wheel_v < 0.0f) ? (window->Scroll[axis] <= 0.0f) : (window->Scroll[axis] >= window->ScrollMax[axis]); + if (has_scrolling && !inputs_disabled) // && !scrolling_past_limits) + break; // select this window + } + if (windows[0] == NULL && windows[1] == NULL) + return NULL; + + // If there's only one window or only one axis then there's no ambiguity + if (windows[0] == windows[1] || windows[0] == NULL || windows[1] == NULL) + return windows[1] ? windows[1] : windows[0]; + + // If candidate are different windows we need to decide which one to prioritize + // - First frame: only find a winner if one axis is zero. + // - Subsequent frames: only find a winner when one is more than the other. + if (g.WheelingWindowStartFrame == -1) + g.WheelingWindowStartFrame = g.FrameCount; + if ((g.WheelingWindowStartFrame == g.FrameCount && wheel.x != 0.0f && wheel.y != 0.0f) || (g.WheelingAxisAvg.x == g.WheelingAxisAvg.y)) + { + g.WheelingWindowWheelRemainder = wheel; + return NULL; + } + return (g.WheelingAxisAvg.x > g.WheelingAxisAvg.y) ? windows[0] : windows[1]; +} + +// Called by NewFrame() +void ImGui::UpdateMouseWheel() +{ + // Reset the locked window if we move the mouse or after the timer elapses. + // FIXME: Ideally we could refactor to have one timer for "changing window w/ same axis" and a shorter timer for "changing window or axis w/ other axis" (#3795) + ImGuiContext& g = *GImGui; + if (g.WheelingWindow != NULL) + { + g.WheelingWindowReleaseTimer -= g.IO.DeltaTime; + if (IsMousePosValid() && ImLengthSqr(g.IO.MousePos - g.WheelingWindowRefMousePos) > g.IO.MouseDragThreshold * g.IO.MouseDragThreshold) + g.WheelingWindowReleaseTimer = 0.0f; + if (g.WheelingWindowReleaseTimer <= 0.0f) + LockWheelingWindow(NULL, 0.0f); + } + + ImVec2 wheel; + wheel.x = TestKeyOwner(ImGuiKey_MouseWheelX, ImGuiKeyOwner_None) ? g.IO.MouseWheelH : 0.0f; + wheel.y = TestKeyOwner(ImGuiKey_MouseWheelY, ImGuiKeyOwner_None) ? g.IO.MouseWheel : 0.0f; + + //IMGUI_DEBUG_LOG("MouseWheel X:%.3f Y:%.3f\n", wheel_x, wheel_y); + ImGuiWindow* mouse_window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow; + if (!mouse_window || mouse_window->Collapsed) + return; + + // Zoom / Scale window + // FIXME-OBSOLETE: This is an old feature, it still works but pretty much nobody is using it and may be best redesigned. + if (wheel.y != 0.0f && g.IO.KeyCtrl && g.IO.FontAllowUserScaling) + { + LockWheelingWindow(mouse_window, wheel.y); + ImGuiWindow* window = mouse_window; + const float new_font_scale = ImClamp(window->FontWindowScale + g.IO.MouseWheel * 0.10f, 0.50f, 2.50f); + const float scale = new_font_scale / window->FontWindowScale; + window->FontWindowScale = new_font_scale; + if (window == window->RootWindow) + { + const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; + SetWindowPos(window, window->Pos + offset, 0); + window->Size = ImFloor(window->Size * scale); + window->SizeFull = ImFloor(window->SizeFull * scale); + } + return; + } + if (g.IO.KeyCtrl) + return; + + // Mouse wheel scrolling + // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead + // (we avoid doing it on OSX as it the OS input layer handles this already) + const bool swap_axis = g.IO.KeyShift && !g.IO.ConfigMacOSXBehaviors; + if (swap_axis) + { + wheel.x = wheel.y; + wheel.y = 0.0f; + } + + // Maintain a rough average of moving magnitude on both axises + // FIXME: should by based on wall clock time rather than frame-counter + g.WheelingAxisAvg.x = ImExponentialMovingAverage(g.WheelingAxisAvg.x, ImAbs(wheel.x), 30); + g.WheelingAxisAvg.y = ImExponentialMovingAverage(g.WheelingAxisAvg.y, ImAbs(wheel.y), 30); + + // In the rare situation where FindBestWheelingWindow() had to defer first frame of wheeling due to ambiguous main axis, reinject it now. + wheel += g.WheelingWindowWheelRemainder; + g.WheelingWindowWheelRemainder = ImVec2(0.0f, 0.0f); + if (wheel.x == 0.0f && wheel.y == 0.0f) + return; + + // Mouse wheel scrolling: find target and apply + // - don't renew lock if axis doesn't apply on the window. + // - select a main axis when both axises are being moved. + if (ImGuiWindow* window = (g.WheelingWindow ? g.WheelingWindow : FindBestWheelingWindow(wheel))) + if (!(window->Flags & ImGuiWindowFlags_NoScrollWithMouse) && !(window->Flags & ImGuiWindowFlags_NoMouseInputs)) + { + bool do_scroll[2] = { wheel.x != 0.0f && window->ScrollMax.x != 0.0f, wheel.y != 0.0f && window->ScrollMax.y != 0.0f }; + if (do_scroll[ImGuiAxis_X] && do_scroll[ImGuiAxis_Y]) + do_scroll[(g.WheelingAxisAvg.x > g.WheelingAxisAvg.y) ? ImGuiAxis_Y : ImGuiAxis_X] = false; + if (do_scroll[ImGuiAxis_X]) + { + LockWheelingWindow(window, wheel.x); + float max_step = window->InnerRect.GetWidth() * 0.67f; + float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step)); + SetScrollX(window, window->Scroll.x - wheel.x * scroll_step); + } + if (do_scroll[ImGuiAxis_Y]) + { + LockWheelingWindow(window, wheel.y); + float max_step = window->InnerRect.GetHeight() * 0.67f; + float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step)); + SetScrollY(window, window->Scroll.y - wheel.y * scroll_step); + } + } +} + +void ImGui::SetNextFrameWantCaptureKeyboard(bool want_capture_keyboard) { - IM_ASSERT(scale > 0.0f); ImGuiContext& g = *GImGui; - ImGuiWindow* window = GetCurrentWindow(); - window->FontWindowScale = scale; - g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); + g.WantCaptureKeyboardNextFrame = want_capture_keyboard ? 1 : 0; } -void ImGui::ActivateItem(ImGuiID id) +void ImGui::SetNextFrameWantCaptureMouse(bool want_capture_mouse) { ImGuiContext& g = *GImGui; - g.NavNextActivateId = id; - g.NavNextActivateFlags = ImGuiActivateFlags_None; + g.WantCaptureMouseNextFrame = want_capture_mouse ? 1 : 0; } -void ImGui::PushFocusScope(ImGuiID id) +#ifndef IMGUI_DISABLE_DEBUG_TOOLS +static const char* GetInputSourceName(ImGuiInputSource source) { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - g.FocusScopeStack.push_back(window->DC.NavFocusScopeIdCurrent); - window->DC.NavFocusScopeIdCurrent = id; + const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Nav", "Clipboard" }; + IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT && source >= 0 && source < ImGuiInputSource_COUNT); + return input_source_names[source]; } - -void ImGui::PopFocusScope() +static void DebugPrintInputEvent(const char* prefix, const ImGuiInputEvent* e) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(g.FocusScopeStack.Size > 0); // Too many PopFocusScope() ? - window->DC.NavFocusScopeIdCurrent = g.FocusScopeStack.back(); - g.FocusScopeStack.pop_back(); + if (e->Type == ImGuiInputEventType_MousePos) { if (e->MousePos.PosX == -FLT_MAX && e->MousePos.PosY == -FLT_MAX) IMGUI_DEBUG_LOG_IO("%s: MousePos (-FLT_MAX, -FLT_MAX)\n", prefix); else IMGUI_DEBUG_LOG_IO("%s: MousePos (%.1f, %.1f)\n", prefix, e->MousePos.PosX, e->MousePos.PosY); return; } + if (e->Type == ImGuiInputEventType_MouseButton) { IMGUI_DEBUG_LOG_IO("%s: MouseButton %d %s\n", prefix, e->MouseButton.Button, e->MouseButton.Down ? "Down" : "Up"); return; } + if (e->Type == ImGuiInputEventType_MouseWheel) { IMGUI_DEBUG_LOG_IO("%s: MouseWheel (%.3f, %.3f)\n", prefix, e->MouseWheel.WheelX, e->MouseWheel.WheelY); return; } + if (e->Type == ImGuiInputEventType_Key) { IMGUI_DEBUG_LOG_IO("%s: Key \"%s\" %s\n", prefix, ImGui::GetKeyName(e->Key.Key), e->Key.Down ? "Down" : "Up"); return; } + if (e->Type == ImGuiInputEventType_Text) { IMGUI_DEBUG_LOG_IO("%s: Text: %c (U+%08X)\n", prefix, e->Text.Char, e->Text.Char); return; } + if (e->Type == ImGuiInputEventType_Focus) { IMGUI_DEBUG_LOG_IO("%s: AppFocused %d\n", prefix, e->AppFocused.Focused); return; } } +#endif -void ImGui::SetKeyboardFocusHere(int offset) +// Process input queue +// We always call this with the value of 'bool g.IO.ConfigInputTrickleEventQueue'. +// - trickle_fast_inputs = false : process all events, turn into flattened input state (e.g. successive down/up/down/up will be lost) +// - trickle_fast_inputs = true : process as many events as possible (successive down/up/down/up will be trickled over several frames so nothing is lost) (new feature in 1.87) +void ImGui::UpdateInputEvents(bool trickle_fast_inputs) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(offset >= -1); // -1 is allowed but not below - g.NavWindow = window; - ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; - NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. - if (offset == -1) - { - NavMoveRequestResolveWithLastItem(&g.NavMoveResultLocal); - } - else + ImGuiIO& io = g.IO; + + // Only trickle chars<>key when working with InputText() + // FIXME: InputText() could parse event trail? + // FIXME: Could specialize chars<>keys trickling rules for control keys (those not typically associated to characters) + const bool trickle_interleaved_keys_and_text = (trickle_fast_inputs && g.WantTextInputNextFrame == 1); + + bool mouse_moved = false, mouse_wheeled = false, key_changed = false, text_inputted = false; + int mouse_button_changed = 0x00; + ImBitArray key_changed_mask; + + int event_n = 0; + for (; event_n < g.InputEventsQueue.Size; event_n++) { - g.NavTabbingDir = 1; - g.NavTabbingCounter = offset + 1; + ImGuiInputEvent* e = &g.InputEventsQueue[event_n]; + if (e->Type == ImGuiInputEventType_MousePos) + { + // Trickling Rule: Stop processing queued events if we already handled a mouse button change + ImVec2 event_pos(e->MousePos.PosX, e->MousePos.PosY); + if (trickle_fast_inputs && (mouse_button_changed != 0 || mouse_wheeled || key_changed || text_inputted)) + break; + io.MousePos = event_pos; + mouse_moved = true; + } + else if (e->Type == ImGuiInputEventType_MouseButton) + { + // Trickling Rule: Stop processing queued events if we got multiple action on the same button + const ImGuiMouseButton button = e->MouseButton.Button; + IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); + if (trickle_fast_inputs && ((mouse_button_changed & (1 << button)) || mouse_wheeled)) + break; + io.MouseDown[button] = e->MouseButton.Down; + mouse_button_changed |= (1 << button); + } + else if (e->Type == ImGuiInputEventType_MouseWheel) + { + // Trickling Rule: Stop processing queued events if we got multiple action on the event + if (trickle_fast_inputs && (mouse_moved || mouse_button_changed != 0)) + break; + io.MouseWheelH += e->MouseWheel.WheelX; + io.MouseWheel += e->MouseWheel.WheelY; + mouse_wheeled = true; + } + else if (e->Type == ImGuiInputEventType_Key) + { + // Trickling Rule: Stop processing queued events if we got multiple action on the same button + ImGuiKey key = e->Key.Key; + IM_ASSERT(key != ImGuiKey_None); + ImGuiKeyData* key_data = GetKeyData(key); + const int key_data_index = (int)(key_data - g.IO.KeysData); + if (trickle_fast_inputs && key_data->Down != e->Key.Down && (key_changed_mask.TestBit(key_data_index) || text_inputted || mouse_button_changed != 0)) + break; + key_data->Down = e->Key.Down; + key_data->AnalogValue = e->Key.AnalogValue; + key_changed = true; + key_changed_mask.SetBit(key_data_index); + + // Allow legacy code using io.KeysDown[GetKeyIndex()] with new backends +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + io.KeysDown[key_data_index] = key_data->Down; + if (io.KeyMap[key_data_index] != -1) + io.KeysDown[io.KeyMap[key_data_index]] = key_data->Down; +#endif + } + else if (e->Type == ImGuiInputEventType_Text) + { + // Trickling Rule: Stop processing queued events if keys/mouse have been interacted with + if (trickle_fast_inputs && ((key_changed && trickle_interleaved_keys_and_text) || mouse_button_changed != 0 || mouse_moved || mouse_wheeled)) + break; + unsigned int c = e->Text.Char; + io.InputQueueCharacters.push_back(c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); + if (trickle_interleaved_keys_and_text) + text_inputted = true; + } + else if (e->Type == ImGuiInputEventType_Focus) + { + // We intentionally overwrite this and process in NewFrame(), in order to give a chance + // to multi-viewports backends to queue AddFocusEvent(false) + AddFocusEvent(true) in same frame. + const bool focus_lost = !e->AppFocused.Focused; + io.AppFocusLost = focus_lost; + } + else + { + IM_ASSERT(0 && "Unknown event!"); + } } -} -void ImGui::SetItemDefaultFocus() -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (!window->Appearing) - return; - if (g.NavWindow != window->RootWindowForNav || (!g.NavInitRequest && g.NavInitResultId == 0) || g.NavLayer != window->DC.NavLayerCurrent) - return; + // Record trail (for domain-specific applications wanting to access a precise trail) + //if (event_n != 0) IMGUI_DEBUG_LOG_IO("Processed: %d / Remaining: %d\n", event_n, g.InputEventsQueue.Size - event_n); + for (int n = 0; n < event_n; n++) + g.InputEventsTrail.push_back(g.InputEventsQueue[n]); - g.NavInitRequest = false; - g.NavInitResultId = g.LastItemData.ID; - g.NavInitResultRectRel = WindowRectAbsToRel(window, g.LastItemData.Rect); - NavUpdateAnyRequestFlag(); + // [DEBUG] +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (event_n != 0 && (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO)) + for (int n = 0; n < g.InputEventsQueue.Size; n++) + DebugPrintInputEvent(n < event_n ? "Processed" : "Remaining", &g.InputEventsQueue[n]); +#endif - // Scroll could be done in NavInitRequestApplyResult() via a opt-in flag (we however don't want regular init requests to scroll) - if (!IsItemVisible()) - ScrollToRectEx(window, g.LastItemData.Rect, ImGuiScrollFlags_None); -} + // Remaining events will be processed on the next frame + if (event_n == g.InputEventsQueue.Size) + g.InputEventsQueue.resize(0); + else + g.InputEventsQueue.erase(g.InputEventsQueue.Data, g.InputEventsQueue.Data + event_n); -void ImGui::SetStateStorage(ImGuiStorage* tree) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - window->DC.StateStorage = tree ? tree : &window->StateStorage; + // Clear buttons state when focus is lost + // - this is useful so e.g. releasing Alt after focus loss on Alt-Tab doesn't trigger the Alt menu toggle. + // - we clear in EndFrame() and not now in order allow application/user code polling this flag + // (e.g. custom backend may want to clear additional data, custom widgets may want to react with a "canceling" event). + if (g.IO.AppFocusLost) + g.IO.ClearInputKeys(); } -ImGuiStorage* ImGui::GetStateStorage() +ImGuiID ImGui::GetKeyOwner(ImGuiKey key) { - ImGuiWindow* window = GImGui->CurrentWindow; - return window->DC.StateStorage; -} + if (!IsNamedKeyOrModKey(key)) + return ImGuiKeyOwner_None; -void ImGui::PushID(const char* str_id) -{ ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiID id = window->GetIDNoKeepAlive(str_id); - window->IDStack.push_back(id); -} + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + ImGuiID owner_id = owner_data->OwnerCurr; -void ImGui::PushID(const char* str_id_begin, const char* str_id_end) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiID id = window->GetIDNoKeepAlive(str_id_begin, str_id_end); - window->IDStack.push_back(id); -} + if (g.ActiveIdUsingAllKeyboardKeys && owner_id != g.ActiveId && owner_id != ImGuiKeyOwner_Any) + if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) + return ImGuiKeyOwner_None; -void ImGui::PushID(const void* ptr_id) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiID id = window->GetIDNoKeepAlive(ptr_id); - window->IDStack.push_back(id); + return owner_id; } -void ImGui::PushID(int int_id) +// TestKeyOwner(..., ID) : (owner == None || owner == ID) +// TestKeyOwner(..., None) : (owner == None) +// TestKeyOwner(..., Any) : no owner test +// All paths are also testing for key not being locked, for the rare cases that key have been locked with using ImGuiInputFlags_LockXXX flags. +bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) { + if (!IsNamedKeyOrModKey(key)) + return true; + ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImGuiID id = window->GetIDNoKeepAlive(int_id); - window->IDStack.push_back(id); + if (g.ActiveIdUsingAllKeyboardKeys && owner_id != g.ActiveId && owner_id != ImGuiKeyOwner_Any) + if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) + return false; + + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + if (owner_id == ImGuiKeyOwner_Any) + return (owner_data->LockThisFrame == false); + + // Note: SetKeyOwner() sets OwnerCurr. It is not strictly required for most mouse routing overlap (because of ActiveId/HoveredId + // are acting as filter before this has a chance to filter), but sane as soon as user tries to look into things. + // Setting OwnerCurr in SetKeyOwner() is more consistent than testing OwnerNext here: would be inconsistent with getter and other functions. + if (owner_data->OwnerCurr != owner_id) + { + if (owner_data->LockThisFrame) + return false; + if (owner_data->OwnerCurr != ImGuiKeyOwner_None) + return false; + } + + return true; } -// Push a given id value ignoring the ID stack as a seed. -void ImGui::PushOverrideID(ImGuiID id) +// _LockXXX flags are useful to lock keys away from code which is not input-owner aware. +// When using _LockXXX flags, you can use ImGuiKeyOwner_Any to lock keys from everyone. +// - SetKeyOwner(..., None) : clears owner +// - SetKeyOwner(..., Any, !Lock) : illegal (assert) +// - SetKeyOwner(..., Any or None, Lock) : set lock +void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (g.DebugHookIdInfo == id) - DebugHookIdInfo(id, ImGuiDataType_ID, NULL, NULL); - window->IDStack.push_back(id); + IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function! + + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + owner_data->OwnerCurr = owner_data->OwnerNext = owner_id; + + // We cannot lock by default as it would likely break lots of legacy code. + // In the case of using LockUntilRelease while key is not down we still lock during the frame (no key_data->Down test) + owner_data->LockUntilRelease = (flags & ImGuiInputFlags_LockUntilRelease) != 0; + owner_data->LockThisFrame = (flags & ImGuiInputFlags_LockThisFrame) != 0 || (owner_data->LockUntilRelease); } -// Helper to avoid a common series of PushOverrideID -> GetID() -> PopID() call -// (note that when using this pattern, TestEngine's "Stack Tool" will tend to not display the intermediate stack level. -// for that to work we would need to do PushOverrideID() -> ItemAdd() -> PopID() which would alter widget code a little more) -ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) +// This is more or less equivalent to: +// if (IsItemHovered() || IsItemActive()) +// SetKeyOwner(key, GetItemID()); +// Extensive uses of that (e.g. many calls for a single item) may want to manually perform the tests once and then call SetKeyOwner() multiple times. +// More advanced usage scenarios may want to call SetKeyOwner() manually based on different condition. +// Worth noting is that only one item can be hovered and only one item can be active, therefore this usage pattern doesn't need to bother with routing and priority. +void ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags) { - ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - KeepAliveID(id); ImGuiContext& g = *GImGui; - if (g.DebugHookIdInfo == id) - DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); - return id; + ImGuiID id = g.LastItemData.ID; + if (id == 0 || (g.HoveredId != id && g.ActiveId != id)) + return; + if ((flags & ImGuiInputFlags_CondMask_) == 0) + flags |= ImGuiInputFlags_CondDefault_; + if ((g.HoveredId == id && (flags & ImGuiInputFlags_CondHovered)) || (g.ActiveId == id && (flags & ImGuiInputFlags_CondActive))) + { + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetItemKeyOwner) == 0); // Passing flags not supported by this function! + SetKeyOwner(key, id, flags & ~ImGuiInputFlags_CondMask_); + } } -void ImGui::PopID() +bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) { - ImGuiWindow* window = GImGui->CurrentWindow; - IM_ASSERT(window->IDStack.Size > 1); // Too many PopID(), or could be popping in a wrong/different window? - window->IDStack.pop_back(); -} + ImGuiContext& g = *GImGui; -ImGuiID ImGui::GetID(const char* str_id) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(str_id); -} + // When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any. + if ((flags & ImGuiInputFlags_RouteMask_) == 0) + flags |= ImGuiInputFlags_RouteFocused; + if (!SetShortcutRouting(key_chord, owner_id, flags)) + return false; -ImGuiID ImGui::GetID(const char* str_id_begin, const char* str_id_end) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(str_id_begin, str_id_end); -} + if (key_chord & ImGuiMod_Shortcut) + key_chord = ConvertShortcutMod(key_chord); + ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); + if (g.IO.KeyMods != mods) + return false; -ImGuiID ImGui::GetID(const void* ptr_id) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->GetID(ptr_id); -} + // Special storage location for mods + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + if (key == ImGuiKey_None) + key = ConvertSingleModFlagToKey(mods); -bool ImGui::IsRectVisible(const ImVec2& size) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); -} + if (!IsKeyPressed(key, owner_id, (flags & (ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateMask_)))) + return false; + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByShortcut) == 0); // Passing flags not supported by this function! -bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); + return true; } @@ -7460,9 +8810,12 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) // Helper function to verify ABI compatibility between caller code and compiled version of Dear ImGui. // Verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit -// If the user has inconsistent compilation settings, imgui configuration #define, packing pragma, etc. your user code -// may see different structures than what imgui.cpp sees, which is problematic. -// We usually require settings to be in imconfig.h to make sure that they are accessible to all compilation units involved with Dear ImGui. +// If this triggers you have an issue: +// - Most commonly: mismatched headers and compiled code version. +// - Or: mismatched configuration #define, compilation settings, packing pragma etc. +// The configuration settings mentioned in imconfig.h must be set for all compilation units involved with Dear ImGui, +// which is way it is required you put them in your imconfig file (and not just before including imgui.h). +// Otherwise it is possible that different compilation units would see different structure layout bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx) { bool error = false; @@ -7476,6 +8829,38 @@ bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, si return !error; } +// Until 1.89 (IMGUI_VERSION_NUM < 18814) it was legal to use SetCursorPos() to extend the boundary of a parent (e.g. window or table cell) +// This is causing issues and ambiguity and we need to retire that. +// See https://github.com/ocornut/imgui/issues/5548 for more details. +// [Scenario 1] +// Previously this would make the window content size ~200x200: +// Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + End(); // NOT OK +// Instead, please submit an item: +// Begin(...) + SetCursorScreenPos(GetCursorScreenPos() + ImVec2(200,200)) + Dummy(ImVec2(0,0)) + End(); // OK +// Alternative: +// Begin(...) + Dummy(ImVec2(200,200)) + End(); // OK +// [Scenario 2] +// For reference this is one of the issue what we aim to fix with this change: +// BeginGroup() + SomeItem("foobar") + SetCursorScreenPos(GetCursorScreenPos()) + EndGroup() +// The previous logic made SetCursorScreenPos(GetCursorScreenPos()) have a side-effect! It would erroneously incorporate ItemSpacing.y after the item into content size, making the group taller! +// While this code is a little twisted, no-one would expect SetXXX(GetXXX()) to have a side-effect. Using vertical alignment patterns could trigger this issue. +void ImGui::ErrorCheckUsingSetCursorPosToExtendParentBoundaries() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(window->DC.IsSetPos); + window->DC.IsSetPos = false; +#ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (window->DC.CursorPos.x <= window->DC.CursorMaxPos.x && window->DC.CursorPos.y <= window->DC.CursorMaxPos.y) + return; + if (window->SkipItems) + return; + IM_ASSERT(0 && "Code uses SetCursorPos()/SetCursorScreenPos() to extend window/parent boundaries. Please submit an item e.g. Dummy() to validate extent."); +#else + window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); +#endif +} + static void ImGui::ErrorCheckNewFrameSanityChecks() { ImGuiContext& g = *GImGui; @@ -7496,16 +8881,19 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() IM_ASSERT(g.IO.DisplaySize.x >= 0.0f && g.IO.DisplaySize.y >= 0.0f && "Invalid DisplaySize value!"); IM_ASSERT(g.IO.Fonts->IsBuilt() && "Font Atlas not built! Make sure you called ImGui_ImplXXXX_NewFrame() function for renderer backend, which should call io.Fonts->GetTexDataAsRGBA32() / GetTexDataAsAlpha8()"); IM_ASSERT(g.Style.CurveTessellationTol > 0.0f && "Invalid style setting!"); - IM_ASSERT(g.Style.CircleTessellationMaxError > 0.0f && "Invalid style setting!"); + IM_ASSERT(g.Style.CircleTessellationMaxError > 0.0f && "Invalid style setting!"); IM_ASSERT(g.Style.Alpha >= 0.0f && g.Style.Alpha <= 1.0f && "Invalid style setting!"); // Allows us to avoid a few clamps in color computations IM_ASSERT(g.Style.WindowMinSize.x >= 1.0f && g.Style.WindowMinSize.y >= 1.0f && "Invalid style setting."); IM_ASSERT(g.Style.WindowMenuButtonPosition == ImGuiDir_None || g.Style.WindowMenuButtonPosition == ImGuiDir_Left || g.Style.WindowMenuButtonPosition == ImGuiDir_Right); - for (int n = 0; n < ImGuiKey_COUNT; n++) - IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < IM_ARRAYSIZE(g.IO.KeysDown) && "io.KeyMap[] contains an out of bound value (need to be 0..512, or -1 for unmapped key)"); + IM_ASSERT(g.Style.ColorButtonPosition == ImGuiDir_Left || g.Style.ColorButtonPosition == ImGuiDir_Right); +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + for (int n = ImGuiKey_NamedKey_BEGIN; n < ImGuiKey_COUNT; n++) + IM_ASSERT(g.IO.KeyMap[n] >= -1 && g.IO.KeyMap[n] < ImGuiKey_LegacyNativeKey_END && "io.KeyMap[] contains an out of bound value (need to be 0..511, or -1 for unmapped key)"); // Check: required key mapping (we intentionally do NOT check all keys to not pressure user into setting up everything, but Space is required and was only added in 1.60 WIP) - if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) + if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && g.IO.BackendUsingLegacyKeyArrays == 1) IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); +#endif // Check: the io.ConfigWindowsResizeFromEdges option requires backend to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) @@ -7520,13 +8908,13 @@ static void ImGui::ErrorCheckEndFrameSanityChecks() // One possible reason leading to this assert is that your backends update inputs _AFTER_ NewFrame(). // It is known that when some modal native windows called mid-frame takes focus away, some backends such as GLFW will // send key release events mid-frame. This would normally trigger this assertion and lead to sheared inputs. - // We silently accommodate for this case by ignoring/ the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0), + // We silently accommodate for this case by ignoring the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0), // while still correctly asserting on mid-frame key press events. - const ImGuiKeyModFlags key_mod_flags = GetMergedKeyModFlags(); - IM_ASSERT((key_mod_flags == 0 || g.IO.KeyMods == key_mod_flags) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); - IM_UNUSED(key_mod_flags); + const ImGuiKeyChord key_mods = GetMergedModsFromKeys(); + IM_ASSERT((key_mods == 0 || g.IO.KeyMods == key_mods) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); + IM_UNUSED(key_mods); - // Recover from errors + // [EXPERIMENTAL] Recover from errors: You may call this yourself before EndFrame(). //ErrorCheckEndFrameRecover(); // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you @@ -7566,7 +8954,6 @@ void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, voi IM_ASSERT(window->IsFallbackWindow); break; } - IM_ASSERT(window == g.CurrentWindow); if (window->Flags & ImGuiWindowFlags_ChildWindow) { if (log_callback) log_callback(user_data, "Recovered from missing EndChild() for '%s'", window->Name); @@ -7633,7 +9020,7 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); PopStyleVar(); } - while (g.FocusScopeStack.Size > stack_sizes->SizeOfFocusScopeStack) //-V1044 + while (g.FocusScopeStack.Size > stack_sizes->SizeOfFocusScopeStack + 1) //-V1044 { if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); PopFocusScope(); @@ -7709,7 +9096,7 @@ void ImGuiStackSizes::CompareWithCurrentState() // - GetWindowContentRegionMin(), GetWindowContentRegionMax() // - BeginGroup() // - EndGroup() -// Also see in imgui_widgets: tab bars, columns. +// Also see in imgui_widgets: tab bars, and in imgui_tables: tables, columns. //----------------------------------------------------------------------------- // Advance cursor given item size for layout. @@ -7726,14 +9113,16 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) // In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor, // but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect. const float offset_to_match_baseline_y = (text_baseline_y >= 0) ? ImMax(0.0f, window->DC.CurrLineTextBaseOffset - text_baseline_y) : 0.0f; - const float line_height = ImMax(window->DC.CurrLineSize.y, size.y + offset_to_match_baseline_y); + + const float line_y1 = window->DC.IsSameLine ? window->DC.CursorPosPrevLine.y : window->DC.CursorPos.y; + const float line_height = ImMax(window->DC.CurrLineSize.y, /*ImMax(*/window->DC.CursorPos.y - line_y1/*, 0.0f)*/ + size.y + offset_to_match_baseline_y); // Always align ourselves on pixel boundaries //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; - window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y; + window->DC.CursorPosPrevLine.y = line_y1; window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line - window->DC.CursorPos.y = IM_FLOOR(window->DC.CursorPos.y + line_height + g.Style.ItemSpacing.y); // Next line + window->DC.CursorPos.y = IM_FLOOR(line_y1 + line_height + g.Style.ItemSpacing.y); // Next line window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] @@ -7742,17 +9131,13 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) window->DC.CurrLineSize.y = 0.0f; window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); window->DC.CurrLineTextBaseOffset = 0.0f; + window->DC.IsSameLine = window->DC.IsSetPos = false; // Horizontal layout mode if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) SameLine(); } -void ImGui::ItemSize(const ImRect& bb, float text_baseline_y) -{ - ItemSize(bb.GetSize(), text_baseline_y); -} - // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. @@ -7772,6 +9157,8 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // Directional navigation processing if (id != 0) { + KeepAliveID(id); + // Runs prior to clipping early-out // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests @@ -7781,25 +9168,19 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. - window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); - if (g.NavId == id || g.NavAnyRequest) - if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) - if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) - NavProcessItem(); + if (!(g.LastItemData.InFlags & ImGuiItemFlags_NoNav)) + { + window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); + if (g.NavId == id || g.NavAnyRequest) + if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) + if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) + NavProcessItem(); + } // [DEBUG] People keep stumbling on this problem and using "" as identifier in the root of a window instead of "##something". // Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something". // READ THE FAQ: https://dearimgui.org/faq IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); - - // [DEBUG] Item Picker tool, when enabling the "extended" version we perform the check in ItemAdd() -#ifdef IMGUI_DEBUG_TOOL_ITEM_PICKER_EX - if (id == g.DebugItemPickerBreakId) - { - IM_DEBUG_BREAK(); - g.DebugItemPickerBreakId = 0; - } -#endif } g.NextItemData.Flags = ImGuiNextItemDataFlags_None; @@ -7809,12 +9190,26 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu #endif // Clipping test - const bool is_clipped = IsClippedEx(bb, id); - if (is_clipped) - return false; + // (FIXME: This is a modified copy of IsClippedEx() so we can reuse the is_rect_visible value) + //const bool is_clipped = IsClippedEx(bb, id); + //if (is_clipped) + // return false; + const bool is_rect_visible = bb.Overlaps(window->ClipRect); + if (!is_rect_visible) + if (id == 0 || (id != g.ActiveId && id != g.NavId)) + if (!g.LogEnabled) + return false; + + // [DEBUG] +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (id != 0 && id == g.DebugLocateId) + DebugLocateItemResolveWithLastItem(); +#endif //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) + if (is_rect_visible) + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Visible; if (IsMouseHoveringRect(bb.Min, bb.Max)) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; return true; @@ -7827,25 +9222,28 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // spacing_w >= 0 : enforce spacing amount void ImGui::SameLine(float offset_from_start_x, float spacing_w) { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return; - ImGuiContext& g = *GImGui; if (offset_from_start_x != 0.0f) { - if (spacing_w < 0.0f) spacing_w = 0.0f; + if (spacing_w < 0.0f) + spacing_w = 0.0f; window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + offset_from_start_x + spacing_w + window->DC.GroupOffset.x + window->DC.ColumnsOffset.x; window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; } else { - if (spacing_w < 0.0f) spacing_w = g.Style.ItemSpacing.x; + if (spacing_w < 0.0f) + spacing_w = g.Style.ItemSpacing.x; window->DC.CursorPos.x = window->DC.CursorPosPrevLine.x + spacing_w; window->DC.CursorPos.y = window->DC.CursorPosPrevLine.y; } window->DC.CurrLineSize = window->DC.PrevLineSize; window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; + window->DC.IsSameLine = true; } ImVec2 ImGui::GetCursorScreenPos() @@ -7854,11 +9252,15 @@ ImVec2 ImGui::GetCursorScreenPos() return window->DC.CursorPos; } +// 2022/08/05: Setting cursor position also extend boundaries (via modifying CursorMaxPos) used to compute window size, group size etc. +// I believe this was is a judicious choice but it's probably being relied upon (it has been the case since 1.31 and 1.50) +// It would be sane if we requested user to use SetCursorPos() + Dummy(ImVec2(0,0)) to extend CursorMaxPos... void ImGui::SetCursorScreenPos(const ImVec2& pos) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos = pos; - window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); + //window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); + window->DC.IsSetPos = true; } // User generally sees positions in window coordinates. Internally we store CursorPos in absolute screen coordinates because it is more convenient. @@ -7885,21 +9287,24 @@ void ImGui::SetCursorPos(const ImVec2& local_pos) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos = window->Pos - window->Scroll + local_pos; - window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); + //window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, window->DC.CursorPos); + window->DC.IsSetPos = true; } void ImGui::SetCursorPosX(float x) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos.x = window->Pos.x - window->Scroll.x + x; - window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x); + //window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x); + window->DC.IsSetPos = true; } void ImGui::SetCursorPosY(float y) { ImGuiWindow* window = GetCurrentWindow(); window->DC.CursorPos.y = window->Pos.y - window->Scroll.y + y; - window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y); + //window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y); + window->DC.IsSetPos = true; } ImVec2 ImGui::GetCursorStartPos() @@ -7990,7 +9395,8 @@ float ImGui::CalcItemWidth() // The 4.0f here may be changed to match CalcItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) { - ImGuiWindow* window = GImGui->CurrentWindow; + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; ImVec2 region_max; if (size.x < 0.0f || size.y < 0.0f) @@ -8115,6 +9521,9 @@ void ImGui::EndGroup() ImGuiGroupData& group_data = g.GroupStack.back(); IM_ASSERT(group_data.WindowID == window->ID); // EndGroup() in wrong window? + if (window->DC.IsSetPos) + ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos)); window->DC.CursorPos = group_data.BackupCursorPos; @@ -8137,7 +9546,7 @@ void ImGui::EndGroup() ItemAdd(group_bb, 0, NULL, ImGuiItemFlags_NoTabStop); // If the current ActiveId was declared within the boundary of our group, we copy it to LastItemId so IsItemActive(), IsItemDeactivated() etc. will be functional on the entire group. - // It would be be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets. + // It would be neater if we replaced window.DC.LastItemId by e.g. 'bool LastItemIsActive', but would put a little more burden on individual widgets. // Also if you grep for LastItemId you'll notice it is only used in that context. // (The two tests not the same because ActiveIdIsAlive is an ID itself, in order to be able to handle ActiveId being overwritten during the frame.) const bool group_contains_curr_active_id = (group_data.BackupActiveIdIsAlive != g.ActiveId) && (g.ActiveIdIsAlive == g.ActiveId) && g.ActiveId; @@ -8187,38 +9596,24 @@ static float CalcScrollEdgeSnap(float target, float snap_min, float snap_max, fl static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window) { ImVec2 scroll = window->Scroll; - if (window->ScrollTarget.x < FLT_MAX) - { - float decoration_total_width = window->ScrollbarSizes.x; - float center_x_ratio = window->ScrollTargetCenterRatio.x; - float scroll_target_x = window->ScrollTarget.x; - if (window->ScrollTargetEdgeSnapDist.x > 0.0f) - { - float snap_x_min = 0.0f; - float snap_x_max = window->ScrollMax.x + window->SizeFull.x - decoration_total_width; - scroll_target_x = CalcScrollEdgeSnap(scroll_target_x, snap_x_min, snap_x_max, window->ScrollTargetEdgeSnapDist.x, center_x_ratio); - } - scroll.x = scroll_target_x - center_x_ratio * (window->SizeFull.x - decoration_total_width); - } - if (window->ScrollTarget.y < FLT_MAX) + ImVec2 decoration_size(window->DecoOuterSizeX1 + window->DecoInnerSizeX1 + window->DecoOuterSizeX2, window->DecoOuterSizeY1 + window->DecoInnerSizeY1 + window->DecoOuterSizeY2); + for (int axis = 0; axis < 2; axis++) { - float decoration_total_height = window->TitleBarHeight() + window->MenuBarHeight() + window->ScrollbarSizes.y; - float center_y_ratio = window->ScrollTargetCenterRatio.y; - float scroll_target_y = window->ScrollTarget.y; - if (window->ScrollTargetEdgeSnapDist.y > 0.0f) + if (window->ScrollTarget[axis] < FLT_MAX) { - float snap_y_min = 0.0f; - float snap_y_max = window->ScrollMax.y + window->SizeFull.y - decoration_total_height; - scroll_target_y = CalcScrollEdgeSnap(scroll_target_y, snap_y_min, snap_y_max, window->ScrollTargetEdgeSnapDist.y, center_y_ratio); + float center_ratio = window->ScrollTargetCenterRatio[axis]; + float scroll_target = window->ScrollTarget[axis]; + if (window->ScrollTargetEdgeSnapDist[axis] > 0.0f) + { + float snap_min = 0.0f; + float snap_max = window->ScrollMax[axis] + window->SizeFull[axis] - decoration_size[axis]; + scroll_target = CalcScrollEdgeSnap(scroll_target, snap_min, snap_max, window->ScrollTargetEdgeSnapDist[axis], center_ratio); + } + scroll[axis] = scroll_target - center_ratio * (window->SizeFull[axis] - decoration_size[axis]); } - scroll.y = scroll_target_y - center_y_ratio * (window->SizeFull.y - decoration_total_height); - } - scroll.x = IM_FLOOR(ImMax(scroll.x, 0.0f)); - scroll.y = IM_FLOOR(ImMax(scroll.y, 0.0f)); - if (!window->Collapsed && !window->SkipItems) - { - scroll.x = ImMin(scroll.x, window->ScrollMax.x); - scroll.y = ImMin(scroll.y, window->ScrollMax.y); + scroll[axis] = IM_FLOOR(ImMax(scroll[axis], 0.0f)); + if (!window->Collapsed && !window->SkipItems) + scroll[axis] = ImMin(scroll[axis], window->ScrollMax[axis]); } return scroll; } @@ -8239,8 +9634,11 @@ void ImGui::ScrollToRect(ImGuiWindow* window, const ImRect& item_rect, ImGuiScro ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGuiScrollFlags flags) { ImGuiContext& g = *GImGui; - ImRect window_rect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1)); - //GetForegroundDrawList(window)->AddRect(window_rect.Min, window_rect.Max, IM_COL32_WHITE); // [DEBUG] + ImRect scroll_rect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1)); + scroll_rect.Min.x = ImMin(scroll_rect.Min.x + window->DecoInnerSizeX1, scroll_rect.Max.x); + scroll_rect.Min.y = ImMin(scroll_rect.Min.y + window->DecoInnerSizeY1, scroll_rect.Max.y); + //GetForegroundDrawList(window)->AddRect(item_rect.Min, item_rect.Max, IM_COL32(255,0,0,255), 0.0f, 0, 5.0f); // [DEBUG] + //GetForegroundDrawList(window)->AddRect(scroll_rect.Min, scroll_rect.Max, IM_COL32_WHITE); // [DEBUG] // Check that only one behavior is selected per axis IM_ASSERT((flags & ImGuiScrollFlags_MaskX_) == 0 || ImIsPowerOfTwo(flags & ImGuiScrollFlags_MaskX_)); @@ -8253,35 +9651,39 @@ ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGui if ((flags & ImGuiScrollFlags_MaskY_) == 0) flags |= window->Appearing ? ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeY; - const bool fully_visible_x = item_rect.Min.x >= window_rect.Min.x && item_rect.Max.x <= window_rect.Max.x; - const bool fully_visible_y = item_rect.Min.y >= window_rect.Min.y && item_rect.Max.y <= window_rect.Max.y; - const bool can_be_fully_visible_x = (item_rect.GetWidth() + g.Style.ItemSpacing.x * 2.0f) <= window_rect.GetWidth(); - const bool can_be_fully_visible_y = (item_rect.GetHeight() + g.Style.ItemSpacing.y * 2.0f) <= window_rect.GetHeight(); + const bool fully_visible_x = item_rect.Min.x >= scroll_rect.Min.x && item_rect.Max.x <= scroll_rect.Max.x; + const bool fully_visible_y = item_rect.Min.y >= scroll_rect.Min.y && item_rect.Max.y <= scroll_rect.Max.y; + const bool can_be_fully_visible_x = (item_rect.GetWidth() + g.Style.ItemSpacing.x * 2.0f) <= scroll_rect.GetWidth() || (window->AutoFitFramesX > 0) || (window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0; + const bool can_be_fully_visible_y = (item_rect.GetHeight() + g.Style.ItemSpacing.y * 2.0f) <= scroll_rect.GetHeight() || (window->AutoFitFramesY > 0) || (window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0; if ((flags & ImGuiScrollFlags_KeepVisibleEdgeX) && !fully_visible_x) { - if (item_rect.Min.x < window_rect.Min.x || !can_be_fully_visible_x) + if (item_rect.Min.x < scroll_rect.Min.x || !can_be_fully_visible_x) SetScrollFromPosX(window, item_rect.Min.x - g.Style.ItemSpacing.x - window->Pos.x, 0.0f); - else if (item_rect.Max.x >= window_rect.Max.x) + else if (item_rect.Max.x >= scroll_rect.Max.x) SetScrollFromPosX(window, item_rect.Max.x + g.Style.ItemSpacing.x - window->Pos.x, 1.0f); } else if (((flags & ImGuiScrollFlags_KeepVisibleCenterX) && !fully_visible_x) || (flags & ImGuiScrollFlags_AlwaysCenterX)) { - float target_x = can_be_fully_visible_x ? ImFloor((item_rect.Min.x + item_rect.Max.x - window->InnerRect.GetWidth()) * 0.5f) : item_rect.Min.x; - SetScrollFromPosX(window, target_x - window->Pos.x, 0.0f); + if (can_be_fully_visible_x) + SetScrollFromPosX(window, ImFloor((item_rect.Min.x + item_rect.Max.x) * 0.5f) - window->Pos.x, 0.5f); + else + SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x, 0.0f); } if ((flags & ImGuiScrollFlags_KeepVisibleEdgeY) && !fully_visible_y) { - if (item_rect.Min.y < window_rect.Min.y || !can_be_fully_visible_y) + if (item_rect.Min.y < scroll_rect.Min.y || !can_be_fully_visible_y) SetScrollFromPosY(window, item_rect.Min.y - g.Style.ItemSpacing.y - window->Pos.y, 0.0f); - else if (item_rect.Max.y >= window_rect.Max.y) + else if (item_rect.Max.y >= scroll_rect.Max.y) SetScrollFromPosY(window, item_rect.Max.y + g.Style.ItemSpacing.y - window->Pos.y, 1.0f); } else if (((flags & ImGuiScrollFlags_KeepVisibleCenterY) && !fully_visible_y) || (flags & ImGuiScrollFlags_AlwaysCenterY)) { - float target_y = can_be_fully_visible_y ? ImFloor((item_rect.Min.y + item_rect.Max.y - window->InnerRect.GetHeight()) * 0.5f) : item_rect.Min.y; - SetScrollFromPosY(window, target_y - window->Pos.y, 0.0f); + if (can_be_fully_visible_y) + SetScrollFromPosY(window, ImFloor((item_rect.Min.y + item_rect.Max.y) * 0.5f) - window->Pos.y, 0.5f); + else + SetScrollFromPosY(window, item_rect.Min.y - window->Pos.y, 0.0f); } ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); @@ -8356,7 +9758,7 @@ void ImGui::SetScrollY(float scroll_y) // - local_pos = (absolution_pos - window->Pos) // - So local_x/local_y are 0.0f for a position at the upper-left corner of a window, // and generally local_x/local_y are >(padding+decoration) && <(size-padding-decoration) when in the visible area. -// - They mostly exists because of legacy API. +// - They mostly exist because of legacy API. // Following the rules above, when trying to work with scrolling code, consider that: // - SetScrollFromPosY(0.0f) == SetScrollY(0.0f + scroll.y) == has no effect! // - SetScrollFromPosY(-scroll.y) == SetScrollY(-scroll.y + scroll.y) == SetScrollY(0.0f) == reset scroll. Of course writing SetScrollY(0.0f) directly then makes more sense @@ -8364,7 +9766,7 @@ void ImGui::SetScrollY(float scroll_y) void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio) { IM_ASSERT(center_x_ratio >= 0.0f && center_x_ratio <= 1.0f); - window->ScrollTarget.x = IM_FLOOR(local_x + window->Scroll.x); // Convert local position to scroll offset + window->ScrollTarget.x = IM_FLOOR(local_x - window->DecoOuterSizeX1 - window->DecoInnerSizeX1 + window->Scroll.x); // Convert local position to scroll offset window->ScrollTargetCenterRatio.x = center_x_ratio; window->ScrollTargetEdgeSnapDist.x = 0.0f; } @@ -8372,9 +9774,7 @@ void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio) { IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); - const float decoration_up_height = window->TitleBarHeight() + window->MenuBarHeight(); // FIXME: Would be nice to have a more standardized access to our scrollable/client rect; - local_y -= decoration_up_height; - window->ScrollTarget.y = IM_FLOOR(local_y + window->Scroll.y); // Convert local position to scroll offset + window->ScrollTarget.y = IM_FLOOR(local_y - window->DecoOuterSizeY1 - window->DecoInnerSizeY1 + window->Scroll.y); // Convert local position to scroll offset window->ScrollTargetCenterRatio.y = center_y_ratio; window->ScrollTargetEdgeSnapDist.y = 0.0f; } @@ -8547,7 +9947,9 @@ ImGuiWindow* ImGui::GetTopMostAndVisiblePopupModal() void ImGui::OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; - OpenPopupEx(g.CurrentWindow->GetID(str_id), popup_flags); + ImGuiID id = g.CurrentWindow->GetID(str_id); + IMGUI_DEBUG_LOG_POPUP("[popup] OpenPopup(\"%s\" -> 0x%08X\n", str_id, id); + OpenPopupEx(id, popup_flags); } void ImGui::OpenPopup(ImGuiID id, ImGuiPopupFlags popup_flags) @@ -8572,13 +9974,13 @@ void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags) ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. popup_ref.PopupId = id; popup_ref.Window = NULL; - popup_ref.SourceWindow = g.NavWindow; + popup_ref.BackupNavWindow = g.NavWindow; // When popup closes focus may be restored to NavWindow (depend on window type). popup_ref.OpenFrameCount = g.FrameCount; popup_ref.OpenParentId = parent_window->IDStack.back(); popup_ref.OpenPopupPos = NavCalcPreferredRefPos(); popup_ref.OpenMousePos = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : popup_ref.OpenPopupPos; - IMGUI_DEBUG_LOG_POPUP("OpenPopupEx(0x%08X)\n", id); + IMGUI_DEBUG_LOG_POPUP("[popup] OpenPopupEx(0x%08X)\n", id); if (g.OpenPopupStack.Size < current_stack_size + 1) { g.OpenPopupStack.push_back(popup_ref); @@ -8647,7 +10049,7 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to } if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below { - IMGUI_DEBUG_LOG_POPUP("ClosePopupsOverWindow(\"%s\") -> ClosePopupToLevel(%d)\n", ref_window->Name, popup_count_to_keep); + IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupsOverWindow(\"%s\")\n", ref_window ? ref_window->Name : ""); ClosePopupToLevel(popup_count_to_keep, restore_focus_to_window_under_popup); } } @@ -8670,16 +10072,17 @@ void ImGui::ClosePopupsExceptModals() void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; - IMGUI_DEBUG_LOG_POPUP("ClosePopupToLevel(%d), restore_focus_to_window_under_popup=%d\n", remaining, restore_focus_to_window_under_popup); + IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupToLevel(%d), restore_focus_to_window_under_popup=%d\n", remaining, restore_focus_to_window_under_popup); IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); // Trim open popup stack - ImGuiWindow* focus_window = g.OpenPopupStack[remaining].SourceWindow; ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window; + ImGuiWindow* popup_backup_nav_window = g.OpenPopupStack[remaining].BackupNavWindow; g.OpenPopupStack.resize(remaining); if (restore_focus_to_window_under_popup) { + ImGuiWindow* focus_window = (popup_window && popup_window->Flags & ImGuiWindowFlags_ChildMenu) ? popup_window->ParentWindow : popup_backup_nav_window; if (focus_window && !focus_window->WasActive && popup_window) { // Fallback @@ -8715,7 +10118,7 @@ void ImGui::CloseCurrentPopup() break; popup_idx--; } - IMGUI_DEBUG_LOG_POPUP("CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); + IMGUI_DEBUG_LOG_POPUP("[popup] CloseCurrentPopup %d -> %d\n", g.BeginPopupStack.Size - 1, popup_idx); ClosePopupToLevel(popup_idx, true); // A common pattern is to close a popup when selecting a menu item/selectable that will open another window. @@ -8758,7 +10161,8 @@ bool ImGui::BeginPopup(const char* str_id, ImGuiWindowFlags flags) return false; } flags |= ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings; - return BeginPopupEx(g.CurrentWindow->GetID(str_id), flags); + ImGuiID id = g.CurrentWindow->GetID(str_id); + return BeginPopupEx(id, flags); } // If 'p_open' is specified for a modal popup window, the popup will have a regular close button which will close the popup. @@ -8837,7 +10241,7 @@ void ImGui::OpenPopupOnItemClick(const char* str_id, ImGuiPopupFlags popup_flags // - You may want to handle the whole on user side if you have specific needs (e.g. tweaking IsItemHovered() parameters). // This is essentially the same as: // id = str_id ? GetID(str_id) : GetItemID(); -// OpenPopupOnItemClick(str_id); +// OpenPopupOnItemClick(str_id, ImGuiPopupFlags_MouseButtonRight); // return BeginPopup(id); // Which is essentially the same as: // id = str_id ? GetID(str_id) : GetItemID(); @@ -8933,7 +10337,7 @@ ImVec2 ImGui::FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& s const float avail_w = (dir == ImGuiDir_Left ? r_avoid.Min.x : r_outer.Max.x) - (dir == ImGuiDir_Right ? r_avoid.Max.x : r_outer.Min.x); const float avail_h = (dir == ImGuiDir_Up ? r_avoid.Min.y : r_outer.Max.y) - (dir == ImGuiDir_Down ? r_avoid.Max.y : r_outer.Min.y); - // If there not enough room on one axis, there's no point in positioning on a side on this axis (e.g. when not enough width, use a top/bottom position to maximize available width) + // If there's not enough room on one axis, there's no point in positioning on a side on this axis (e.g. when not enough width, use a top/bottom position to maximize available width) if (avail_w < size.x && (dir == ImGuiDir_Left || dir == ImGuiDir_Right)) continue; if (avail_h < size.y && (dir == ImGuiDir_Up || dir == ImGuiDir_Down)) @@ -8998,8 +10402,7 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) } if (window->Flags & ImGuiWindowFlags_Popup) { - ImRect r_avoid = ImRect(window->Pos.x - 1, window->Pos.y - 1, window->Pos.x + 1, window->Pos.y + 1); - return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Default); + return FindBestWindowPosForPopupEx(window->Pos, window->Size, &window->AutoPosLastDirection, r_outer, ImRect(window->Pos, window->Pos), ImGuiPopupPositionPolicy_Default); // Ideally we'd disable r_avoid here } if (window->Flags & ImGuiWindowFlags_Tooltip) { @@ -9021,8 +10424,22 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) // [SECTION] KEYBOARD/GAMEPAD NAVIGATION //----------------------------------------------------------------------------- -// FIXME-NAV: The existence of SetNavID vs SetFocusID properly needs to be clarified/reworked. -// In our terminology those should be interchangeable. Those two functions are merely a legacy artifact, so at minimum naming should be clarified. +// FIXME-NAV: The existence of SetNavID vs SetFocusID vs FocusWindow() needs to be clarified/reworked. +// In our terminology those should be interchangeable, yet right now this is super confusing. +// Those two functions are merely a legacy artifact, so at minimum naming should be clarified. + +void ImGui::SetNavWindow(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (g.NavWindow != window) + { + IMGUI_DEBUG_LOG_FOCUS("[focus] SetNavWindow(\"%s\")\n", window ? window->Name : ""); + g.NavWindow = window; + } + g.NavInitRequest = g.NavMoveSubmitted = g.NavMoveScoringItems = false; + NavUpdateAnyRequestFlag(); +} + void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; @@ -9040,15 +10457,15 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) ImGuiContext& g = *GImGui; IM_ASSERT(id != 0); - // Assume that SetFocusID() is called in the context where its window->DC.NavLayerCurrent and window->DC.NavFocusScopeIdCurrent are valid. + if (g.NavWindow != window) + SetNavWindow(window); + + // Assume that SetFocusID() is called in the context where its window->DC.NavLayerCurrent and g.CurrentFocusScopeId are valid. // Note that window may be != g.CurrentWindow (e.g. SetFocusID call in InputTextEx for multi-line text) const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; - if (g.NavWindow != window) - g.NavInitRequest = false; - g.NavWindow = window; g.NavId = id; g.NavLayer = nav_layer; - g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; + g.NavFocusScopeId = g.CurrentFocusScopeId; window->NavLastIds[nav_layer] = id; if (g.LastItemData.ID == id) window->NavRectRel[nav_layer] = WindowRectAbsToRel(window, g.LastItemData.NavRect); @@ -9112,7 +10529,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) } // We perform scoring on items bounding box clipped by the current clipping rectangle on the other axis (clipping on our movement axis would give us equal scores for all clipped items) - // For example, this ensure that items in one column are not reached when moving vertically from items in another column. + // For example, this ensures that items in one column are not reached when moving vertically from items in another column. NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect); // Compute distance between boxes @@ -9176,7 +10593,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) } #endif - // Is it in the quadrant we're interesting in moving to? + // Is it in the quadrant we're interested in moving to? bool new_best = false; const ImGuiDir move_dir = g.NavMoveDir; if (quadrant == move_dir) @@ -9229,7 +10646,7 @@ static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) ImGuiWindow* window = g.CurrentWindow; result->Window = window; result->ID = g.LastItemData.ID; - result->FocusScopeId = window->DC.NavFocusScopeIdCurrent; + result->FocusScopeId = g.CurrentFocusScopeId; result->InFlags = g.LastItemData.InFlags; result->RectRel = WindowRectAbsToRel(window, g.LastItemData.NavRect); } @@ -9245,10 +10662,10 @@ static void ImGui::NavProcessItem() const ImGuiItemFlags item_flags = g.LastItemData.InFlags; // Process Init Request - if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent) + if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent && (item_flags & ImGuiItemFlags_Disabled) == 0) { // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback - const bool candidate_for_nav_default_focus = (item_flags & (ImGuiItemFlags_NoNavDefaultFocus | ImGuiItemFlags_Disabled)) == 0; + const bool candidate_for_nav_default_focus = (item_flags & ImGuiItemFlags_NoNavDefaultFocus) == 0; if (candidate_for_nav_default_focus || g.NavInitResultId == 0) { g.NavInitResultId = id; @@ -9272,7 +10689,7 @@ static void ImGui::NavProcessItem() if (is_tab_stop || (g.NavMoveFlags & ImGuiNavMoveFlags_FocusApi)) NavProcessItemForTabbingRequest(id); } - else if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & (ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNav))) + else if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & ImGuiItemFlags_Disabled)) { ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; if (!is_tabbing) @@ -9293,9 +10710,10 @@ static void ImGui::NavProcessItem() // Update window-relative bounding box of navigated item if (g.NavId == id) { - g.NavWindow = window; // Always refresh g.NavWindow, because some operations such as FocusItem() don't have a window. + if (g.NavWindow != window) + SetNavWindow(window); // Always refresh g.NavWindow, because some operations such as FocusItem() may not have a window. g.NavLayer = window->DC.NavLayerCurrent; - g.NavFocusScopeId = window->DC.NavFocusScopeIdCurrent; + g.NavFocusScopeId = g.CurrentFocusScopeId; g.NavIdIsAlive = true; window->NavRectRel[window->DC.NavLayerCurrent] = WindowRectAbsToRel(window, nav_bb); // Store item bounding box (relative to window position) } @@ -9371,10 +10789,11 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM g.NavMoveScrollFlags = scroll_flags; g.NavMoveForwardToNextFrame = false; g.NavMoveKeyMods = g.IO.KeyMods; - g.NavTabbingCounter = 0; g.NavMoveResultLocal.Clear(); g.NavMoveResultLocalVisible.Clear(); g.NavMoveResultOther.Clear(); + g.NavTabbingCounter = 0; + g.NavTabbingResultFirst.Clear(); NavUpdateAnyRequestFlag(); } @@ -9441,7 +10860,12 @@ void ImGui::NavRestoreLayer(ImGuiNavLayer layer) { ImGuiContext& g = *GImGui; if (layer == ImGuiNavLayer_Main) - g.NavWindow = NavRestoreLastChildNavWindow(g.NavWindow); + { + ImGuiWindow* prev_nav_window = g.NavWindow; + g.NavWindow = NavRestoreLastChildNavWindow(g.NavWindow); // FIXME-NAV: Should clear ongoing nav requests? + if (prev_nav_window) + IMGUI_DEBUG_LOG_FOCUS("[focus] NavRestoreLayer: from \"%s\" to SetNavWindow(\"%s\")\n", prev_nav_window->Name, g.NavWindow->Name); + } ImGuiWindow* window = g.NavWindow; if (window->NavLastIds[layer] != 0) { @@ -9477,7 +10901,8 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) if (window->Flags & ImGuiWindowFlags_NoNavInputs) { - g.NavId = g.NavFocusScopeId = 0; + g.NavId = 0; + g.NavFocusScopeId = window->NavRootFocusScopeId; return; } @@ -9487,7 +10912,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from NavInitWindow(), init_for_nav=%d, window=\"%s\", layer=%d\n", init_for_nav, window->Name, g.NavLayer); if (init_for_nav) { - SetNavID(0, g.NavLayer, 0, ImRect()); + SetNavID(0, g.NavLayer, window->NavRootFocusScopeId, ImRect()); g.NavInitRequest = true; g.NavInitRequestFromMove = false; g.NavInitResultId = 0; @@ -9497,7 +10922,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) else { g.NavId = window->NavLastIds[0]; - g.NavFocusScopeId = 0; + g.NavFocusScopeId = window->NavRootFocusScopeId; } } @@ -9508,9 +10933,10 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() if (g.NavDisableHighlight || !g.NavDisableMouseHover || !window) { // Mouse (we need a fallback in case the mouse becomes invalid after being used) - if (IsMousePosValid(&g.IO.MousePos)) - return g.IO.MousePos; - return g.MouseLastValidPos; + // The +1.0f offset when stored by OpenPopupEx() allows reopening this or another popup (same or another mouse button) while not moving the mouse, it is pretty standard. + // In theory we could move that +1.0f offset in OpenPopupEx() + ImVec2 p = IsMousePosValid(&g.IO.MousePos) ? g.IO.MousePos : g.MouseLastValidPos; + return ImVec2(p.x + 1.0f, p.y); } else { @@ -9528,44 +10954,27 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() } } -float ImGui::GetNavInputAmount(ImGuiNavInput n, ImGuiInputReadMode mode) +float ImGui::GetNavTweakPressedAmount(ImGuiAxis axis) { ImGuiContext& g = *GImGui; - if (mode == ImGuiInputReadMode_Down) - return g.IO.NavInputs[n]; // Instant, read analog input (0.0f..1.0f, as provided by user) - - const float t = g.IO.NavInputsDownDuration[n]; - if (t < 0.0f && mode == ImGuiInputReadMode_Released) // Return 1.0f when just released, no repeat, ignore analog input. - return (g.IO.NavInputsDownDurationPrev[n] >= 0.0f ? 1.0f : 0.0f); - if (t < 0.0f) - return 0.0f; - if (mode == ImGuiInputReadMode_Pressed) // Return 1.0f when just pressed, no repeat, ignore analog input. - return (t == 0.0f) ? 1.0f : 0.0f; - if (mode == ImGuiInputReadMode_Repeat) - return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 0.72f, g.IO.KeyRepeatRate * 0.80f); - if (mode == ImGuiInputReadMode_RepeatSlow) - return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 1.25f, g.IO.KeyRepeatRate * 2.00f); - if (mode == ImGuiInputReadMode_RepeatFast) - return (float)CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay * 0.72f, g.IO.KeyRepeatRate * 0.30f); - return 0.0f; -} + float repeat_delay, repeat_rate; + GetTypematicRepeatRate(ImGuiInputFlags_RepeatRateNavTweak, &repeat_delay, &repeat_rate); -ImVec2 ImGui::GetNavInputAmount2d(ImGuiNavDirSourceFlags dir_sources, ImGuiInputReadMode mode, float slow_factor, float fast_factor) -{ - ImVec2 delta(0.0f, 0.0f); - if (dir_sources & ImGuiNavDirSourceFlags_RawKeyboard) - delta += ImVec2((float)IsKeyDown(GetKeyIndex(ImGuiKey_RightArrow)) - (float)IsKeyDown(GetKeyIndex(ImGuiKey_LeftArrow)), (float)IsKeyDown(GetKeyIndex(ImGuiKey_DownArrow)) - (float)IsKeyDown(GetKeyIndex(ImGuiKey_UpArrow))); - if (dir_sources & ImGuiNavDirSourceFlags_Keyboard) - delta += ImVec2(GetNavInputAmount(ImGuiNavInput_KeyRight_, mode) - GetNavInputAmount(ImGuiNavInput_KeyLeft_, mode), GetNavInputAmount(ImGuiNavInput_KeyDown_, mode) - GetNavInputAmount(ImGuiNavInput_KeyUp_, mode)); - if (dir_sources & ImGuiNavDirSourceFlags_PadDPad) - delta += ImVec2(GetNavInputAmount(ImGuiNavInput_DpadRight, mode) - GetNavInputAmount(ImGuiNavInput_DpadLeft, mode), GetNavInputAmount(ImGuiNavInput_DpadDown, mode) - GetNavInputAmount(ImGuiNavInput_DpadUp, mode)); - if (dir_sources & ImGuiNavDirSourceFlags_PadLStick) - delta += ImVec2(GetNavInputAmount(ImGuiNavInput_LStickRight, mode) - GetNavInputAmount(ImGuiNavInput_LStickLeft, mode), GetNavInputAmount(ImGuiNavInput_LStickDown, mode) - GetNavInputAmount(ImGuiNavInput_LStickUp, mode)); - if (slow_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakSlow)) - delta *= slow_factor; - if (fast_factor != 0.0f && IsNavInputDown(ImGuiNavInput_TweakFast)) - delta *= fast_factor; - return delta; + ImGuiKey key_less, key_more; + if (g.NavInputSource == ImGuiInputSource_Gamepad) + { + key_less = (axis == ImGuiAxis_X) ? ImGuiKey_GamepadDpadLeft : ImGuiKey_GamepadDpadUp; + key_more = (axis == ImGuiAxis_X) ? ImGuiKey_GamepadDpadRight : ImGuiKey_GamepadDpadDown; + } + else + { + key_less = (axis == ImGuiAxis_X) ? ImGuiKey_LeftArrow : ImGuiKey_UpArrow; + key_more = (axis == ImGuiAxis_X) ? ImGuiKey_RightArrow : ImGuiKey_DownArrow; + } + float amount = (float)GetKeyPressedAmount(key_more, repeat_delay, repeat_rate) - (float)GetKeyPressedAmount(key_less, repeat_delay, repeat_rate); + if (amount != 0.0f && IsKeyDown(key_less) && IsKeyDown(key_more)) // Cancel when opposite directions are held, regardless of repeat phase + amount = 0.0f; + return amount; } static void ImGui::NavUpdate() @@ -9574,39 +10983,22 @@ static void ImGui::NavUpdate() ImGuiIO& io = g.IO; io.WantSetMousePos = false; - //if (g.NavScoringDebugCount > 0) IMGUI_DEBUG_LOG("NavScoringDebugCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.NavScoringDebugCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); + //if (g.NavScoringDebugCount > 0) IMGUI_DEBUG_LOG_NAV("[nav] NavScoringDebugCount %d for '%s' layer %d (Init:%d, Move:%d)\n", g.NavScoringDebugCount, g.NavWindow ? g.NavWindow->Name : "NULL", g.NavLayer, g.NavInitRequest || g.NavInitResultId != 0, g.NavMoveRequest); - // Set input source as Gamepad when buttons are pressed (as some features differs when used with Gamepad vs Keyboard) - // (do it before we map Keyboard input!) - const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + // Set input source based on which keys are last pressed (as some features differs when used with Gamepad vs Keyboard) + // FIXME-NAV: Now that keys are separated maybe we can get rid of NavInputSource? const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; - if (nav_gamepad_active && g.NavInputSource != ImGuiInputSource_Gamepad) - { - if (io.NavInputs[ImGuiNavInput_Activate] > 0.0f || io.NavInputs[ImGuiNavInput_Input] > 0.0f || io.NavInputs[ImGuiNavInput_Cancel] > 0.0f || io.NavInputs[ImGuiNavInput_Menu] > 0.0f - || io.NavInputs[ImGuiNavInput_DpadLeft] > 0.0f || io.NavInputs[ImGuiNavInput_DpadRight] > 0.0f || io.NavInputs[ImGuiNavInput_DpadUp] > 0.0f || io.NavInputs[ImGuiNavInput_DpadDown] > 0.0f) - g.NavInputSource = ImGuiInputSource_Gamepad; - } - - // Update Keyboard->Nav inputs mapping + const ImGuiKey nav_gamepad_keys_to_change_source[] = { ImGuiKey_GamepadFaceRight, ImGuiKey_GamepadFaceLeft, ImGuiKey_GamepadFaceUp, ImGuiKey_GamepadFaceDown, ImGuiKey_GamepadDpadRight, ImGuiKey_GamepadDpadLeft, ImGuiKey_GamepadDpadUp, ImGuiKey_GamepadDpadDown }; + if (nav_gamepad_active) + for (ImGuiKey key : nav_gamepad_keys_to_change_source) + if (IsKeyDown(key)) + g.NavInputSource = ImGuiInputSource_Gamepad; + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + const ImGuiKey nav_keyboard_keys_to_change_source[] = { ImGuiKey_Space, ImGuiKey_Enter, ImGuiKey_Escape, ImGuiKey_RightArrow, ImGuiKey_LeftArrow, ImGuiKey_UpArrow, ImGuiKey_DownArrow }; if (nav_keyboard_active) - { - #define NAV_MAP_KEY(_KEY, _NAV_INPUT) do { if (IsKeyDown(io.KeyMap[_KEY])) { io.NavInputs[_NAV_INPUT] = 1.0f; g.NavInputSource = ImGuiInputSource_Keyboard; } } while (0) - NAV_MAP_KEY(ImGuiKey_Space, ImGuiNavInput_Activate ); - NAV_MAP_KEY(ImGuiKey_Enter, ImGuiNavInput_Input ); - NAV_MAP_KEY(ImGuiKey_Escape, ImGuiNavInput_Cancel ); - NAV_MAP_KEY(ImGuiKey_LeftArrow, ImGuiNavInput_KeyLeft_ ); - NAV_MAP_KEY(ImGuiKey_RightArrow,ImGuiNavInput_KeyRight_); - NAV_MAP_KEY(ImGuiKey_UpArrow, ImGuiNavInput_KeyUp_ ); - NAV_MAP_KEY(ImGuiKey_DownArrow, ImGuiNavInput_KeyDown_ ); - if (io.KeyCtrl) - io.NavInputs[ImGuiNavInput_TweakSlow] = 1.0f; - if (io.KeyShift) - io.NavInputs[ImGuiNavInput_TweakFast] = 1.0f; - #undef NAV_MAP_KEY - } - memcpy(io.NavInputsDownDurationPrev, io.NavInputsDownDuration, sizeof(io.NavInputsDownDuration)); - for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) - io.NavInputsDownDuration[i] = (io.NavInputs[i] > 0.0f) ? (io.NavInputsDownDuration[i] < 0.0f ? 0.0f : io.NavInputsDownDuration[i] + io.DeltaTime) : -1.0f; + for (ImGuiKey key : nav_keyboard_keys_to_change_source) + if (IsKeyDown(key)) + g.NavInputSource = ImGuiInputSource_Keyboard; // Process navigation init request (select first/default focus) if (g.NavInitResultId != 0) @@ -9651,10 +11043,10 @@ static void ImGui::NavUpdate() g.NavActivateFlags = ImGuiActivateFlags_None; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { - bool activate_down = IsNavInputDown(ImGuiNavInput_Activate); - bool input_down = IsNavInputDown(ImGuiNavInput_Input); - bool activate_pressed = activate_down && IsNavInputTest(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed); - bool input_pressed = input_down && IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed); + const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate)); + const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, false)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, false))); + const bool input_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Enter)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput)); + const bool input_pressed = input_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Enter, false)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, false))); if (g.ActiveId == 0 && activate_pressed) { g.NavActivateId = g.NavId; @@ -9709,13 +11101,17 @@ static void ImGui::NavUpdate() SetScrollY(window, ImFloor(window->Scroll.y + ((move_dir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); } - // *Normal* Manual scroll with NavScrollXXX keys + // *Normal* Manual scroll with LStick // Next movement request will clamp the NavId reference rectangle to the visible area, so navigation will resume within those bounds. - ImVec2 scroll_dir = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down, 1.0f / 10.0f, 10.0f); - if (scroll_dir.x != 0.0f && window->ScrollbarX) - SetScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed)); - if (scroll_dir.y != 0.0f) - SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed)); + if (nav_gamepad_active) + { + const ImVec2 scroll_dir = GetKeyMagnitude2d(ImGuiKey_GamepadLStickLeft, ImGuiKey_GamepadLStickRight, ImGuiKey_GamepadLStickUp, ImGuiKey_GamepadLStickDown); + const float tweak_factor = IsKeyDown(ImGuiKey_NavGamepadTweakSlow) ? 1.0f / 10.0f : IsKeyDown(ImGuiKey_NavGamepadTweakFast) ? 10.0f : 1.0f; + if (scroll_dir.x != 0.0f && window->ScrollbarX) + SetScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed * tweak_factor)); + if (scroll_dir.y != 0.0f) + SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed * tweak_factor)); + } } // Always prioritize mouse highlight if navigation is disabled @@ -9731,7 +11127,7 @@ static void ImGui::NavUpdate() { io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); io.WantSetMousePos = true; - //IMGUI_DEBUG_LOG("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); + //IMGUI_DEBUG_LOG_IO("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); } // [DEBUG] @@ -9755,7 +11151,7 @@ void ImGui::NavInitRequestApplyResult() // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) // FIXME-NAV: On _NavFlattened windows, g.NavWindow will only be updated during subsequent frame. Not a problem currently. - IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: ApplyResult: NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); g.NavIdIsAlive = true; // Mark as alive from previous frame as we got a result if (g.NavInitRequestFromMove) @@ -9767,6 +11163,8 @@ void ImGui::NavUpdateCreateMoveRequest() ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; ImGuiWindow* window = g.NavWindow; + const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; if (g.NavMoveForwardToNextFrame && window != NULL) { @@ -9784,11 +11182,11 @@ void ImGui::NavUpdateCreateMoveRequest() g.NavMoveScrollFlags = ImGuiScrollFlags_None; if (window && !g.NavWindowingTarget && !(window->Flags & ImGuiWindowFlags_NoNavInputs)) { - const ImGuiInputReadMode read_mode = ImGuiInputReadMode_Repeat; - if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && (IsNavInputTest(ImGuiNavInput_DpadLeft, read_mode) || IsNavInputTest(ImGuiNavInput_KeyLeft_, read_mode))) { g.NavMoveDir = ImGuiDir_Left; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && (IsNavInputTest(ImGuiNavInput_DpadRight, read_mode) || IsNavInputTest(ImGuiNavInput_KeyRight_, read_mode))) { g.NavMoveDir = ImGuiDir_Right; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && (IsNavInputTest(ImGuiNavInput_DpadUp, read_mode) || IsNavInputTest(ImGuiNavInput_KeyUp_, read_mode))) { g.NavMoveDir = ImGuiDir_Up; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && (IsNavInputTest(ImGuiNavInput_DpadDown, read_mode) || IsNavInputTest(ImGuiNavInput_KeyDown_, read_mode))) { g.NavMoveDir = ImGuiDir_Down; } + const ImGuiInputFlags repeat_mode = ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateNavMove; + if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadLeft, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_LeftArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Left; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadRight, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_RightArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Right; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadUp, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_UpArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Up; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadDown, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_DownArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Down; } } g.NavMoveClipDir = g.NavMoveDir; g.NavScoringNoClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX); @@ -9796,7 +11194,6 @@ void ImGui::NavUpdateCreateMoveRequest() // Update PageUp/PageDown/Home/End scroll // FIXME-NAV: Consider enabling those keys even without the master ImGuiConfigFlags_NavEnableKeyboard flag? - const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; float scoring_rect_offset_y = 0.0f; if (window && g.NavMoveDir == ImGuiDir_None && nav_keyboard_active) scoring_rect_offset_y = NavUpdatePageUpPageDown(); @@ -9808,7 +11205,7 @@ void ImGui::NavUpdateCreateMoveRequest() // [DEBUG] Always send a request #if IMGUI_DEBUG_NAV_SCORING - if (io.KeyCtrl && IsKeyPressedMap(ImGuiKey_C)) + if (io.KeyCtrl && IsKeyPressed(ImGuiKey_C)) g.NavMoveDirForDebug = (ImGuiDir)((g.NavMoveDirForDebug + 1) & 3); if (io.KeyCtrl && g.NavMoveDir == ImGuiDir_None) { @@ -9822,28 +11219,34 @@ void ImGui::NavUpdateCreateMoveRequest() if (g.NavMoveDir != ImGuiDir_None) NavMoveRequestSubmit(g.NavMoveDir, g.NavMoveClipDir, g.NavMoveFlags, g.NavMoveScrollFlags); - // Moving with no reference triggers a init request (will be used as a fallback if the direction fails to find a match) + // Moving with no reference triggers an init request (will be used as a fallback if the direction fails to find a match) if (g.NavMoveSubmitted && g.NavId == 0) { - IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", g.NavWindow->Name, g.NavLayer); + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", window ? window->Name : "", g.NavLayer); g.NavInitRequest = g.NavInitRequestFromMove = true; g.NavInitResultId = 0; g.NavDisableHighlight = false; } // When using gamepad, we project the reference nav bounding box into window visible area. - // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad every movements are relative + // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad all movements are relative // (can't focus a visible object like we can with the mouse). - if (g.NavMoveSubmitted && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main && window != NULL) + if (g.NavMoveSubmitted && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main && window != NULL)// && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded)) { - ImRect window_rect_rel = WindowRectAbsToRel(window, ImRect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1))); - if (!window_rect_rel.Contains(window->NavRectRel[g.NavLayer])) + bool clamp_x = (g.NavMoveFlags & (ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapX)) == 0; + bool clamp_y = (g.NavMoveFlags & (ImGuiNavMoveFlags_LoopY | ImGuiNavMoveFlags_WrapY)) == 0; + ImRect inner_rect_rel = WindowRectAbsToRel(window, ImRect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1))); + if ((clamp_x || clamp_y) && !inner_rect_rel.Contains(window->NavRectRel[g.NavLayer])) { - IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel\n"); - float pad = window->CalcFontSize() * 0.5f; - window_rect_rel.Expand(ImVec2(-ImMin(window_rect_rel.GetWidth(), pad), -ImMin(window_rect_rel.GetHeight(), pad))); // Terrible approximation for the intent of starting navigation from first fully visible item - window->NavRectRel[g.NavLayer].ClipWithFull(window_rect_rel); - g.NavId = g.NavFocusScopeId = 0; + //IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel for gamepad move\n"); + float pad_x = ImMin(inner_rect_rel.GetWidth(), window->CalcFontSize() * 0.5f); + float pad_y = ImMin(inner_rect_rel.GetHeight(), window->CalcFontSize() * 0.5f); // Terrible approximation for the intent of starting navigation from first fully visible item + inner_rect_rel.Min.x = clamp_x ? (inner_rect_rel.Min.x + pad_x) : -FLT_MAX; + inner_rect_rel.Max.x = clamp_x ? (inner_rect_rel.Max.x - pad_x) : +FLT_MAX; + inner_rect_rel.Min.y = clamp_y ? (inner_rect_rel.Min.y + pad_y) : -FLT_MAX; + inner_rect_rel.Max.y = clamp_y ? (inner_rect_rel.Max.y - pad_y) : +FLT_MAX; + window->NavRectRel[g.NavLayer].ClipWithFull(inner_rect_rel); + g.NavId = 0; } } @@ -9856,7 +11259,7 @@ void ImGui::NavUpdateCreateMoveRequest() scoring_rect.TranslateY(scoring_rect_offset_y); scoring_rect.Min.x = ImMin(scoring_rect.Min.x + 1.0f, scoring_rect.Max.x); scoring_rect.Max.x = scoring_rect.Min.x; - IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allows us to remove extraneous ImFabs() calls in NavScoreItem(). + IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allow us to remove extraneous ImFabs() calls in NavScoreItem(). //GetForegroundDrawList()->AddRect(scoring_rect.Min, scoring_rect.Max, IM_COL32(255,200,0,255)); // [DEBUG] //if (!g.NavScoringNoClipRect.IsInverted()) { GetForegroundDrawList()->AddRect(g.NavScoringNoClipRect.Min, g.NavScoringNoClipRect.Max, IM_COL32(255, 200, 0, 255)); } // [DEBUG] } @@ -9872,7 +11275,7 @@ void ImGui::NavUpdateCreateTabbingRequest() if (window == NULL || g.NavWindowingTarget != NULL || (window->Flags & ImGuiWindowFlags_NoNavInputs)) return; - const bool tab_pressed = IsKeyPressedMap(ImGuiKey_Tab, true) && !IsActiveIdUsingKey(ImGuiKey_Tab) && !g.IO.KeyCtrl && !g.IO.KeyAlt; + const bool tab_pressed = IsKeyPressed(ImGuiKey_Tab, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat) && !g.IO.KeyCtrl && !g.IO.KeyAlt; if (!tab_pressed) return; @@ -9885,7 +11288,6 @@ void ImGui::NavUpdateCreateTabbingRequest() ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; ImGuiDir clip_dir = (g.NavTabbingDir < 0) ? ImGuiDir_Up : ImGuiDir_Down; NavMoveRequestSubmit(ImGuiDir_None, clip_dir, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. - g.NavTabbingResultFirst.Clear(); g.NavTabbingCounter = -1; } @@ -9906,7 +11308,7 @@ void ImGui::NavMoveRequestApplyResult() if ((g.NavTabbingCounter == 1 || g.NavTabbingDir == 0) && g.NavTabbingResultFirst.ID) result = &g.NavTabbingResultFirst; - // In a situation when there is no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) + // In a situation when there are no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) if (result == NULL) { if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) @@ -9943,7 +11345,11 @@ void ImGui::NavMoveRequestApplyResult() } } - g.NavWindow = result->Window; + if (g.NavWindow != result->Window) + { + IMGUI_DEBUG_LOG_FOCUS("[focus] NavMoveRequest: SetNavWindow(\"%s\")\n", result->Window->Name); + g.NavWindow = result->Window; + } if (g.ActiveId != result->ID) ClearActiveID(); if (g.NavId != result->ID) @@ -9985,14 +11391,15 @@ void ImGui::NavMoveRequestApplyResult() static void ImGui::NavUpdateCancelRequest() { ImGuiContext& g = *GImGui; - if (!IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed)) + const bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + const bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + if (!(nav_keyboard_active && IsKeyPressed(ImGuiKey_Escape, ImGuiKeyOwner_None)) && !(nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadCancel, ImGuiKeyOwner_None))) return; - IMGUI_DEBUG_LOG_NAV("[nav] ImGuiNavInput_Cancel\n"); + IMGUI_DEBUG_LOG_NAV("[nav] NavUpdateCancelRequest()\n"); if (g.ActiveId != 0) { - if (!IsActiveIdUsingNavInput(ImGuiNavInput_Cancel)) - ClearActiveID(); + ClearActiveID(); } else if (g.NavLayer != ImGuiNavLayer_Main) { @@ -10011,18 +11418,17 @@ static void ImGui::NavUpdateCancelRequest() SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, WindowRectAbsToRel(parent_window, child_rect)); NavRestoreHighlightAfterMove(); } - else if (g.OpenPopupStack.Size > 0) + else if (g.OpenPopupStack.Size > 0 && g.OpenPopupStack.back().Window != NULL && !(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) { // Close open popup/menu - if (!(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) - ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); + ClosePopupToLevel(g.OpenPopupStack.Size - 1, true); } else { // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) g.NavWindow->NavLastIds[0] = 0; - g.NavId = g.NavFocusScopeId = 0; + g.NavId = 0; } } @@ -10033,16 +11439,14 @@ static void ImGui::NavUpdateCancelRequest() static float ImGui::NavUpdatePageUpPageDown() { ImGuiContext& g = *GImGui; - ImGuiIO& io = g.IO; - ImGuiWindow* window = g.NavWindow; if ((window->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL) return 0.0f; - const bool page_up_held = IsKeyDown(io.KeyMap[ImGuiKey_PageUp]) && !IsActiveIdUsingKey(ImGuiKey_PageUp); - const bool page_down_held = IsKeyDown(io.KeyMap[ImGuiKey_PageDown]) && !IsActiveIdUsingKey(ImGuiKey_PageDown); - const bool home_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_Home]) && !IsActiveIdUsingKey(ImGuiKey_Home); - const bool end_pressed = IsKeyPressed(io.KeyMap[ImGuiKey_End]) && !IsActiveIdUsingKey(ImGuiKey_End); + const bool page_up_held = IsKeyDown(ImGuiKey_PageUp, ImGuiKeyOwner_None); + const bool page_down_held = IsKeyDown(ImGuiKey_PageDown, ImGuiKeyOwner_None); + const bool home_pressed = IsKeyPressed(ImGuiKey_Home, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat); + const bool end_pressed = IsKeyPressed(ImGuiKey_End, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat); if (page_up_held == page_down_held && home_pressed == end_pressed) // Proceed if either (not both) are pressed, otherwise early out return 0.0f; @@ -10052,9 +11456,9 @@ static float ImGui::NavUpdatePageUpPageDown() if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) { // Fallback manual-scroll when window has no navigable item - if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) + if (IsKeyPressed(ImGuiKey_PageUp, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat)) SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); - else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) + else if (IsKeyPressed(ImGuiKey_PageDown, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat)) SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); else if (home_pressed) SetScrollY(window, 0.0f); @@ -10066,14 +11470,14 @@ static float ImGui::NavUpdatePageUpPageDown() ImRect& nav_rect_rel = window->NavRectRel[g.NavLayer]; const float page_offset_y = ImMax(0.0f, window->InnerRect.GetHeight() - window->CalcFontSize() * 1.0f + nav_rect_rel.GetHeight()); float nav_scoring_rect_offset_y = 0.0f; - if (IsKeyPressed(io.KeyMap[ImGuiKey_PageUp], true)) + if (IsKeyPressed(ImGuiKey_PageUp, true)) { nav_scoring_rect_offset_y = -page_offset_y; g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) g.NavMoveClipDir = ImGuiDir_Up; g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; } - else if (IsKeyPressed(io.KeyMap[ImGuiKey_PageDown], true)) + else if (IsKeyPressed(ImGuiKey_PageDown, true)) { nav_scoring_rect_offset_y = +page_offset_y; g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) @@ -10208,7 +11612,10 @@ static void NavUpdateWindowingHighlightWindow(int focus_change_dir) if (!window_target) window_target = FindWindowNavFocusable((focus_change_dir < 0) ? (g.WindowsFocusOrder.Size - 1) : 0, i_current, focus_change_dir); if (window_target) // Don't reset windowing target if there's a single window in the list + { g.NavWindowingTarget = g.NavWindowingTargetAnim = window_target; + g.NavWindowingAccumDeltaPos = g.NavWindowingAccumDeltaSize = ImVec2(0.0f, 0.0f); + } g.NavWindowingToggleLayer = false; } @@ -10237,13 +11644,18 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL+Tab or Square+L/R window selection - const bool start_windowing_with_gamepad = allow_windowing && !g.NavWindowingTarget && IsNavInputTest(ImGuiNavInput_Menu, ImGuiInputReadMode_Pressed); - const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && io.KeyCtrl && IsKeyPressedMap(ImGuiKey_Tab); + const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); + const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); + const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && IsKeyPressed(ImGuiKey_NavGamepadMenu, 0, ImGuiInputFlags_None); + const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && (keyboard_next_window || keyboard_prev_window); // Note: enabled even without NavEnableKeyboard! if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; + g.NavWindowingAccumDeltaPos = g.NavWindowingAccumDeltaSize = ImVec2(0.0f, 0.0f); g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; } @@ -10256,7 +11668,7 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // Select window to focus - const int focus_change_dir = (int)IsNavInputTest(ImGuiNavInput_FocusPrev, ImGuiInputReadMode_RepeatSlow) - (int)IsNavInputTest(ImGuiNavInput_FocusNext, ImGuiInputReadMode_RepeatSlow); + const int focus_change_dir = (int)IsKeyPressed(ImGuiKey_GamepadL1) - (int)IsKeyPressed(ImGuiKey_GamepadR1); if (focus_change_dir != 0) { NavUpdateWindowingHighlightWindow(focus_change_dir); @@ -10264,7 +11676,7 @@ static void ImGui::NavUpdateWindowing() } // Single press toggles NavLayer, long press with L/R apply actual focus on release (until then the window was merely rendered top-most) - if (!IsNavInputDown(ImGuiNavInput_Menu)) + if (!IsKeyDown(ImGuiKey_NavGamepadMenu)) { g.NavWindowingToggleLayer &= (g.NavWindowingHighlightAlpha < 1.0f); // Once button was held long enough we don't consider it a tap-to-toggle-layer press anymore. if (g.NavWindowingToggleLayer && g.NavWindow) @@ -10279,18 +11691,19 @@ static void ImGui::NavUpdateWindowing() if (g.NavWindowingTarget && g.NavInputSource == ImGuiInputSource_Keyboard) { // Visuals only appears after a brief time after pressing TAB the first time, so that a fast CTRL+TAB doesn't add visual noise + ImGuiKeyChord shared_mods = ((g.ConfigNavWindowingKeyNext ? g.ConfigNavWindowingKeyNext : ImGuiMod_Mask_) & (g.ConfigNavWindowingKeyPrev ? g.ConfigNavWindowingKeyPrev : ImGuiMod_Mask_)) & ImGuiMod_Mask_; + IM_ASSERT(shared_mods != 0); // Next/Prev shortcut currently needs a shared modifier to "hold", otherwise Prev actions would keep cycling between two windows. g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f - if (IsKeyPressedMap(ImGuiKey_Tab, true)) - NavUpdateWindowingHighlightWindow(io.KeyShift ? +1 : -1); - if (!io.KeyCtrl) + if (keyboard_next_window || keyboard_prev_window) + NavUpdateWindowingHighlightWindow(keyboard_next_window ? -1 : +1); + else if ((io.KeyMods & shared_mods) != shared_mods) apply_focus_window = g.NavWindowingTarget; } // Keyboard: Press and Release ALT to toggle menu layer // - Testing that only Alt is tested prevents Alt+Shift or AltGR from toggling menu layer. // - AltGR is normally Alt+Ctrl but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). But even on keyboards without AltGR we don't want Alt+Ctrl to open menu anyway. - const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - if (nav_keyboard_active && io.KeyMods == ImGuiKeyModFlags_Alt && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) == 0) + if (nav_keyboard_active && IsKeyPressed(ImGuiMod_Alt, ImGuiKeyOwner_None)) { g.NavWindowingToggleLayer = true; g.NavInputSource = ImGuiInputSource_Keyboard; @@ -10299,36 +11712,41 @@ static void ImGui::NavUpdateWindowing() { // We cancel toggling nav layer when any text has been typed (generally while holding Alt). (See #370) // We cancel toggling nav layer when other modifiers are pressed. (See #4439) - if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper) + // We cancel toggling nav layer if an owner has claimed the key. + if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_None) == false) g.NavWindowingToggleLayer = false; // Apply layer toggle on release - // Important: we don't assume that Alt was previously held in order to handle loss of focus when backend calls io.AddFocusEvent(false) // Important: as before version <18314 we lacked an explicit IO event for focus gain/loss, we also compare mouse validity to detect old backends clearing mouse pos on focus loss. - if (!(io.KeyMods & ImGuiKeyModFlags_Alt) && (io.KeyModsPrev & ImGuiKeyModFlags_Alt) && g.NavWindowingToggleLayer) + if (IsKeyReleased(ImGuiMod_Alt) && g.NavWindowingToggleLayer) if (g.ActiveId == 0 || g.ActiveIdAllowOverlap) if (IsMousePosValid(&io.MousePos) == IsMousePosValid(&io.MousePosPrev)) apply_toggle_layer = true; - if (!io.KeyAlt) + if (!IsKeyDown(ImGuiMod_Alt)) g.NavWindowingToggleLayer = false; } // Move window if (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoMove)) { - ImVec2 move_delta; + ImVec2 nav_move_dir; if (g.NavInputSource == ImGuiInputSource_Keyboard && !io.KeyShift) - move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_RawKeyboard, ImGuiInputReadMode_Down); + nav_move_dir = GetKeyMagnitude2d(ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_UpArrow, ImGuiKey_DownArrow); if (g.NavInputSource == ImGuiInputSource_Gamepad) - move_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_PadLStick, ImGuiInputReadMode_Down); - if (move_delta.x != 0.0f || move_delta.y != 0.0f) + nav_move_dir = GetKeyMagnitude2d(ImGuiKey_GamepadLStickLeft, ImGuiKey_GamepadLStickRight, ImGuiKey_GamepadLStickUp, ImGuiKey_GamepadLStickDown); + if (nav_move_dir.x != 0.0f || nav_move_dir.y != 0.0f) { const float NAV_MOVE_SPEED = 800.0f; - const float move_speed = ImFloor(NAV_MOVE_SPEED * io.DeltaTime * ImMin(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y)); // FIXME: Doesn't handle variable framerate very well - ImGuiWindow* moving_window = g.NavWindowingTarget->RootWindow; - SetWindowPos(moving_window, moving_window->Pos + move_delta * move_speed, ImGuiCond_Always); - MarkIniSettingsDirty(moving_window); + const float move_step = NAV_MOVE_SPEED * io.DeltaTime * ImMin(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); + g.NavWindowingAccumDeltaPos += nav_move_dir * move_step; g.NavDisableMouseHover = true; + ImVec2 accum_floored = ImFloor(g.NavWindowingAccumDeltaPos); + if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) + { + ImGuiWindow* moving_window = g.NavWindowingTarget->RootWindow; + SetWindowPos(moving_window, moving_window->Pos + accum_floored, ImGuiCond_Always); + g.NavWindowingAccumDeltaPos -= accum_floored; + } } } @@ -10392,10 +11810,10 @@ static void ImGui::NavUpdateWindowing() static const char* GetFallbackWindowNameForWindowingList(ImGuiWindow* window) { if (window->Flags & ImGuiWindowFlags_Popup) - return "(Popup)"; + return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingPopup); if ((window->Flags & ImGuiWindowFlags_MenuBar) && strcmp(window->Name, "##MainMenuBar") == 0) - return "(Main menu bar)"; - return "(Untitled)"; + return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingMainMenuBar); + return ImGui::LocalizeGetMsg(ImGuiLocKey_WindowingUntitled); } // Overlay displayed when using CTRL+TAB. Called by EndFrame(). @@ -10434,6 +11852,12 @@ void ImGui::NavUpdateWindowingOverlay() // [SECTION] DRAG AND DROP //----------------------------------------------------------------------------- +bool ImGui::IsDragDropActive() +{ + ImGuiContext& g = *GImGui; + return g.DragDropActive; +} + void ImGui::ClearDragDrop() { ImGuiContext& g = *GImGui; @@ -10503,6 +11927,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) // We don't need to maintain/call ClearActiveID() as releasing the button will early out this function and trigger !ActiveIdIsAlive. // Rely on keeping other window->LastItemXXX fields intact. source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); + KeepAliveID(source_id); bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id); if (is_hovered && g.IO.MouseClicked[mouse_button]) { @@ -10518,7 +11943,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) source_drag_active = IsMouseDragging(mouse_button); // Disable navigation and key inputs while dragging + cancel existing request if any - SetActiveIdUsingNavAndKeys(); + SetActiveIdUsingAllKeyboardKeys(); } else { @@ -10622,6 +12047,7 @@ bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_s } payload.DataFrameCount = g.FrameCount; + // Return whether the payload has been accepted return (g.DragDropAcceptFrameCount == g.FrameCount) || (g.DragDropAcceptFrameCount == g.FrameCount - 1); } @@ -10649,7 +12075,7 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) } // We don't use BeginDragDropTargetCustom() and duplicate its code because: -// 1) we use LastItemRectHoveredRect which handles items that pushes a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. +// 1) we use LastItemRectHoveredRect which handles items that push a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. // 2) and it's faster. as this code may be very frequently called, we want to early out as fast as we can. // Also note how the HoveredWindow test is positioned differently in both functions (in both functions we optimize for the cheapest early out case) bool ImGui::BeginDragDropTarget() @@ -10668,7 +12094,10 @@ bool ImGui::BeginDragDropTarget() const ImRect& display_rect = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasDisplayRect) ? g.LastItemData.DisplayRect : g.LastItemData.Rect; ImGuiID id = g.LastItemData.ID; if (id == 0) + { id = window->GetIDFromRectangle(display_rect); + KeepAliveID(id); + } if (g.DragDropPayload.SourceId == id) return false; @@ -10708,9 +12137,8 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop } // Render default drop visuals - // FIXME-DRAGDROP: Settle on a proper default visuals for drop target. payload.Preview = was_accepted_previously; - flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that lives for 1 frame) + flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that live for 1 frame) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) window->DrawList->AddRect(r.Min - ImVec2(3.5f,3.5f), r.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); @@ -10722,10 +12150,16 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop return &payload; } +// FIXME-DRAGDROP: Settle on a proper default visuals for drop target. +void ImGui::RenderDragDropTargetRect(const ImRect& bb) +{ + GetWindowDrawList()->AddRect(bb.Min - ImVec2(3.5f, 3.5f), bb.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); +} + const ImGuiPayload* ImGui::GetDragDropPayload() { ImGuiContext& g = *GImGui; - return g.DragDropActive ? &g.DragDropPayload : NULL; + return (g.DragDropActive && g.DragDropPayload.DataFrameCount != -1) ? &g.DragDropPayload : NULL; } // We don't really use/need this now, but added it for the sake of consistency and because we might need it later. @@ -11082,6 +12516,20 @@ ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name) return CreateNewWindowSettings(name); } +void ImGui::AddSettingsHandler(const ImGuiSettingsHandler* handler) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(FindSettingsHandler(handler->TypeName) == NULL); + g.SettingsHandlers.push_back(*handler); +} + +void ImGui::RemoveSettingsHandler(const char* type_name) +{ + ImGuiContext& g = *GImGui; + if (ImGuiSettingsHandler* handler = FindSettingsHandler(type_name)) + g.SettingsHandlers.erase(handler); +} + ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) { ImGuiContext& g = *GImGui; @@ -11107,7 +12555,8 @@ void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) char* file_data = (char*)ImFileLoadToMemory(ini_filename, "rb", &file_data_size); if (!file_data) return; - LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); + if (file_data_size > 0) + LoadIniSettingsFromMemory(file_data, (size_t)file_data_size); IM_FREE(file_data); } @@ -11294,10 +12743,23 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl } +//----------------------------------------------------------------------------- +// [SECTION] LOCALIZATION +//----------------------------------------------------------------------------- + +void ImGui::LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count) +{ + ImGuiContext& g = *GImGui; + for (int n = 0; n < count; n++) + g.LocalizationTable[entries[n].Key] = entries[n].Text; +} + + //----------------------------------------------------------------------------- // [SECTION] VIEWPORTS, PLATFORM WINDOWS //----------------------------------------------------------------------------- // - GetMainViewport() +// - SetWindowViewport() [Internal] // - UpdateViewportsNewFrame() [Internal] // (this section is more complete in the 'docking' branch) //----------------------------------------------------------------------------- @@ -11308,6 +12770,11 @@ ImGuiViewport* ImGui::GetMainViewport() return g.Viewports[0]; } +void ImGui::SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport) +{ + window->Viewport = viewport; +} + // Update viewports and monitor infos static void ImGui::UpdateViewportsNewFrame() { @@ -11478,25 +12945,37 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text) #pragma comment(lib, "imm32") #endif -static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y) +static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data) { // Notify OS Input Method Editor of text input position - ImGuiIO& io = ImGui::GetIO(); - if (HWND hwnd = (HWND)io.ImeWindowHandle) - if (HIMC himc = ::ImmGetContext(hwnd)) - { - COMPOSITIONFORM cf; - cf.ptCurrentPos.x = x; - cf.ptCurrentPos.y = y; - cf.dwStyle = CFS_FORCE_POSITION; - ::ImmSetCompositionWindow(himc, &cf); - ::ImmReleaseContext(hwnd, himc); - } + HWND hwnd = (HWND)viewport->PlatformHandleRaw; +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (hwnd == 0) + hwnd = (HWND)ImGui::GetIO().ImeWindowHandle; +#endif + if (hwnd == 0) + return; + + //::ImmAssociateContextEx(hwnd, NULL, data->WantVisible ? IACE_DEFAULT : 0); + if (HIMC himc = ::ImmGetContext(hwnd)) + { + COMPOSITIONFORM composition_form = {}; + composition_form.ptCurrentPos.x = (LONG)data->InputPos.x; + composition_form.ptCurrentPos.y = (LONG)data->InputPos.y; + composition_form.dwStyle = CFS_FORCE_POSITION; + ::ImmSetCompositionWindow(himc, &composition_form); + CANDIDATEFORM candidate_form = {}; + candidate_form.dwStyle = CFS_CANDIDATEPOS; + candidate_form.ptCurrentPos.x = (LONG)data->InputPos.x; + candidate_form.ptCurrentPos.y = (LONG)data->InputPos.y; + ::ImmSetCandidateWindow(himc, &candidate_form); + ::ImmReleaseContext(hwnd, himc); + } } #else -static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} +static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport*, ImGuiPlatformImeData*) {} #endif @@ -11505,11 +12984,15 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} //----------------------------------------------------------------------------- // - RenderViewportThumbnail() [Internal] // - RenderViewportsThumbnails() [Internal] +// - DebugTextEncoding() // - MetricsHelpMarker() [Internal] +// - ShowFontAtlas() [Internal] // - ShowMetricsWindow() // - DebugNodeColumns() [Internal] // - DebugNodeDrawList() [Internal] // - DebugNodeDrawCmdShowMeshAndBoundingBox() [Internal] +// - DebugNodeFont() [Internal] +// - DebugNodeFontGlyph() [Internal] // - DebugNodeStorage() [Internal] // - DebugNodeTabBar() [Internal] // - DebugNodeViewport() [Internal] @@ -11519,7 +13002,7 @@ static void ImeSetInputScreenPosFn_DefaultImpl(int, int) {} // - DebugNodeWindowsListByBeginStackParent() [Internal] //----------------------------------------------------------------------------- -#ifndef IMGUI_DISABLE_METRICS_WINDOW +#ifndef IMGUI_DISABLE_DEBUG_TOOLS void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* viewport, const ImRect& bb) { @@ -11572,11 +13055,96 @@ static void RenderViewportsThumbnails() ImGui::Dummy(bb_full.GetSize() * SCALE); } +// Draw an arbitrary US keyboard layout to visualize translated keys +void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list) +{ + const ImVec2 key_size = ImVec2(35.0f, 35.0f); + const float key_rounding = 3.0f; + const ImVec2 key_face_size = ImVec2(25.0f, 25.0f); + const ImVec2 key_face_pos = ImVec2(5.0f, 3.0f); + const float key_face_rounding = 2.0f; + const ImVec2 key_label_pos = ImVec2(7.0f, 4.0f); + const ImVec2 key_step = ImVec2(key_size.x - 1.0f, key_size.y - 1.0f); + const float key_row_offset = 9.0f; + + ImVec2 board_min = GetCursorScreenPos(); + ImVec2 board_max = ImVec2(board_min.x + 3 * key_step.x + 2 * key_row_offset + 10.0f, board_min.y + 3 * key_step.y + 10.0f); + ImVec2 start_pos = ImVec2(board_min.x + 5.0f - key_step.x, board_min.y); + + struct KeyLayoutData { int Row, Col; const char* Label; ImGuiKey Key; }; + const KeyLayoutData keys_to_display[] = + { + { 0, 0, "", ImGuiKey_Tab }, { 0, 1, "Q", ImGuiKey_Q }, { 0, 2, "W", ImGuiKey_W }, { 0, 3, "E", ImGuiKey_E }, { 0, 4, "R", ImGuiKey_R }, + { 1, 0, "", ImGuiKey_CapsLock }, { 1, 1, "A", ImGuiKey_A }, { 1, 2, "S", ImGuiKey_S }, { 1, 3, "D", ImGuiKey_D }, { 1, 4, "F", ImGuiKey_F }, + { 2, 0, "", ImGuiKey_LeftShift },{ 2, 1, "Z", ImGuiKey_Z }, { 2, 2, "X", ImGuiKey_X }, { 2, 3, "C", ImGuiKey_C }, { 2, 4, "V", ImGuiKey_V } + }; + + // Elements rendered manually via ImDrawList API are not clipped automatically. + // While not strictly necessary, here IsItemVisible() is used to avoid rendering these shapes when they are out of view. + Dummy(board_max - board_min); + if (!IsItemVisible()) + return; + draw_list->PushClipRect(board_min, board_max, true); + for (int n = 0; n < IM_ARRAYSIZE(keys_to_display); n++) + { + const KeyLayoutData* key_data = &keys_to_display[n]; + ImVec2 key_min = ImVec2(start_pos.x + key_data->Col * key_step.x + key_data->Row * key_row_offset, start_pos.y + key_data->Row * key_step.y); + ImVec2 key_max = key_min + key_size; + draw_list->AddRectFilled(key_min, key_max, IM_COL32(204, 204, 204, 255), key_rounding); + draw_list->AddRect(key_min, key_max, IM_COL32(24, 24, 24, 255), key_rounding); + ImVec2 face_min = ImVec2(key_min.x + key_face_pos.x, key_min.y + key_face_pos.y); + ImVec2 face_max = ImVec2(face_min.x + key_face_size.x, face_min.y + key_face_size.y); + draw_list->AddRect(face_min, face_max, IM_COL32(193, 193, 193, 255), key_face_rounding, ImDrawFlags_None, 2.0f); + draw_list->AddRectFilled(face_min, face_max, IM_COL32(252, 252, 252, 255), key_face_rounding); + ImVec2 label_min = ImVec2(key_min.x + key_label_pos.x, key_min.y + key_label_pos.y); + draw_list->AddText(label_min, IM_COL32(64, 64, 64, 255), key_data->Label); + if (ImGui::IsKeyDown(key_data->Key)) + draw_list->AddRectFilled(key_min, key_max, IM_COL32(255, 0, 0, 128), key_rounding); + } + draw_list->PopClipRect(); +} + +// Helper tool to diagnose between text encoding issues and font loading issues. Pass your UTF-8 string and verify that there are correct. +void ImGui::DebugTextEncoding(const char* str) +{ + Text("Text: \"%s\"", str); + if (!BeginTable("list", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) + return; + TableSetupColumn("Offset"); + TableSetupColumn("UTF-8"); + TableSetupColumn("Glyph"); + TableSetupColumn("Codepoint"); + TableHeadersRow(); + for (const char* p = str; *p != 0; ) + { + unsigned int c; + const int c_utf8_len = ImTextCharFromUtf8(&c, p, NULL); + TableNextColumn(); + Text("%d", (int)(p - str)); + TableNextColumn(); + for (int byte_index = 0; byte_index < c_utf8_len; byte_index++) + { + if (byte_index > 0) + SameLine(); + Text("0x%02X", (int)(unsigned char)p[byte_index]); + } + TableNextColumn(); + if (GetFont()->FindGlyphNoFallback((ImWchar)c)) + TextUnformatted(p, p + c_utf8_len); + else + TextUnformatted((c == IM_UNICODE_CODEPOINT_INVALID) ? "[invalid]" : "[missing]"); + TableNextColumn(); + Text("U+%04X", (int)c); + p += c_utf8_len; + } + EndTable(); +} + // Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds. static void MetricsHelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered()) + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) { ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); @@ -11586,15 +13154,32 @@ static void MetricsHelpMarker(const char* desc) } } -#ifndef IMGUI_DISABLE_DEMO_WINDOWS -namespace ImGui { void ShowFontAtlas(ImFontAtlas* atlas); } -#endif +// [DEBUG] List fonts in a font atlas and display its texture +void ImGui::ShowFontAtlas(ImFontAtlas* atlas) +{ + for (int i = 0; i < atlas->Fonts.Size; i++) + { + ImFont* font = atlas->Fonts[i]; + PushID(font); + DebugNodeFont(font); + PopID(); + } + if (TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) + { + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); + Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), tint_col, border_col); + TreePop(); + } +} void ImGui::ShowMetricsWindow(bool* p_open) { ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + if (cfg->ShowDebugLog) + ShowDebugLogWindow(&cfg->ShowDebugLog); if (cfg->ShowStackTool) ShowStackToolWindow(&cfg->ShowStackTool); @@ -11627,19 +13212,20 @@ void ImGui::ShowMetricsWindow(bool* p_open) { static ImRect GetTableRect(ImGuiTable* table, int rect_type, int n) { + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); // Always using last submitted instance if (rect_type == TRT_OuterRect) { return table->OuterRect; } else if (rect_type == TRT_InnerRect) { return table->InnerRect; } else if (rect_type == TRT_WorkRect) { return table->WorkRect; } else if (rect_type == TRT_HostClipRect) { return table->HostClipRect; } else if (rect_type == TRT_InnerClipRect) { return table->InnerClipRect; } else if (rect_type == TRT_BackgroundClipRect) { return table->BgClipRect; } - else if (rect_type == TRT_ColumnsRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->MinX, table->InnerClipRect.Min.y, c->MaxX, table->InnerClipRect.Min.y + table->LastOuterHeight); } + else if (rect_type == TRT_ColumnsRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->MinX, table->InnerClipRect.Min.y, c->MaxX, table->InnerClipRect.Min.y + table_instance->LastOuterHeight); } else if (rect_type == TRT_ColumnsWorkRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->WorkRect.Min.y, c->WorkMaxX, table->WorkRect.Max.y); } else if (rect_type == TRT_ColumnsClipRect) { ImGuiTableColumn* c = &table->Columns[n]; return c->ClipRect; } - else if (rect_type == TRT_ColumnsContentHeadersUsed){ ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersUsed, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } // Note: y1/y2 not always accurate - else if (rect_type == TRT_ColumnsContentHeadersIdeal){ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersIdeal, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } - else if (rect_type == TRT_ColumnsContentFrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXFrozen, table->InnerClipRect.Min.y + table->LastFirstRowHeight); } - else if (rect_type == TRT_ColumnsContentUnfrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y + table->LastFirstRowHeight, c->ContentMaxXUnfrozen, table->InnerClipRect.Max.y); } + else if (rect_type == TRT_ColumnsContentHeadersUsed){ ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersUsed, table->InnerClipRect.Min.y + table_instance->LastFirstRowHeight); } // Note: y1/y2 not always accurate + else if (rect_type == TRT_ColumnsContentHeadersIdeal){ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersIdeal, table->InnerClipRect.Min.y + table_instance->LastFirstRowHeight); } + else if (rect_type == TRT_ColumnsContentFrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXFrozen, table->InnerClipRect.Min.y + table_instance->LastFrozenHeight); } + else if (rect_type == TRT_ColumnsContentUnfrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y + table_instance->LastFrozenHeight, c->ContentMaxXUnfrozen, table->InnerClipRect.Max.y); } IM_ASSERT(0); return ImRect(); } @@ -11662,8 +13248,32 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Tools if (TreeNode("Tools")) { + bool show_encoding_viewer = TreeNode("UTF-8 Encoding viewer"); + SameLine(); + MetricsHelpMarker("You can also call ImGui::DebugTextEncoding() from your code with a given string to test that your UTF-8 encoding settings are correct."); + if (show_encoding_viewer) + { + static char buf[100] = ""; + SetNextItemWidth(-FLT_MIN); + InputText("##Text", buf, IM_ARRAYSIZE(buf)); + if (buf[0] != 0) + DebugTextEncoding(buf); + TreePop(); + } + + // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. + if (Checkbox("Show Item Picker", &g.DebugItemPickerActive) && g.DebugItemPickerActive) + DebugStartItemPicker(); + SameLine(); + MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); + + // Stack Tool is your best friend! + Checkbox("Show Debug Log", &cfg->ShowDebugLog); + SameLine(); + MetricsHelpMarker("You can also call ImGui::ShowDebugLogWindow() from your code."); + // Stack Tool is your best friend! - Checkbox("Show stack tool", &cfg->ShowStackTool); + Checkbox("Show Stack Tool", &cfg->ShowStackTool); SameLine(); MetricsHelpMarker("You can also call ImGui::ShowStackToolWindow() from your code."); @@ -11729,12 +13339,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } - // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. - if (Button("Item Picker..")) - DebugStartItemPicker(); - SameLine(); - MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); - TreePop(); } @@ -11795,8 +13399,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) { for (int i = 0; i < g.OpenPopupStack.Size; i++) { - ImGuiWindow* window = g.OpenPopupStack[i].Window; - BulletText("PopupID: %08x, Window: '%s'%s%s", g.OpenPopupStack[i].PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? " ChildWindow" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? " ChildMenu" : ""); + // As it's difficult to interact with tree nodes while popups are open, we display everything inline. + const ImGuiPopupData* popup_data = &g.OpenPopupStack[i]; + ImGuiWindow* window = popup_data->Window; + BulletText("PopupID: %08x, Window: '%s' (%s%s), BackupNavWindow '%s', ParentWindow '%s'", + popup_data->PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? "Child;" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? "Menu;" : "", + popup_data->BackupNavWindow ? popup_data->BackupNavWindow->Name : "NULL", window && window->ParentWindow ? window->ParentWindow->Name : "NULL"); } TreePop(); } @@ -11824,14 +13432,19 @@ void ImGui::ShowMetricsWindow(bool* p_open) } // Details for Fonts -#ifndef IMGUI_DISABLE_DEMO_WINDOWS ImFontAtlas* atlas = g.IO.Fonts; if (TreeNode("Fonts", "Fonts (%d)", atlas->Fonts.Size)) { ShowFontAtlas(atlas); TreePop(); } -#endif + + // Details for InputText + if (TreeNode("InputText")) + { + DebugNodeInputTextState(&g.InputTextState); + TreePop(); + } // Details for Docking #ifdef IMGUI_HAS_DOCK @@ -11889,11 +13502,100 @@ void ImGui::ShowMetricsWindow(bool* p_open) TreePop(); } - // Misc Details - if (TreeNode("Internal state")) + if (TreeNode("Inputs")) { - const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Nav", "Clipboard" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT); + Text("KEYBOARD/GAMEPAD/MOUSE KEYS"); + { + // We iterate both legacy native range and named ImGuiKey ranges, which is a little odd but this allows displaying the data for old/new backends. + // User code should never have to go through such hoops: old code may use native keycodes, new code may use ImGuiKey codes. + Indent(); +#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; +#else + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array + //Text("Legacy raw:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key++) { if (io.KeysDown[key]) { SameLine(); Text("\"%s\" %d", GetKeyName(key), key); } } +#endif + Text("Keys down:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !IsKeyDown(key)) continue; SameLine(); Text(IsNamedKey(key) ? "\"%s\"" : "\"%s\" %d", GetKeyName(key), key); SameLine(); Text("(%.02f)", GetKeyData(key)->DownDuration); } + Text("Keys pressed:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !IsKeyPressed(key)) continue; SameLine(); Text(IsNamedKey(key) ? "\"%s\"" : "\"%s\" %d", GetKeyName(key), key); } + Text("Keys released:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !IsKeyReleased(key)) continue; SameLine(); Text(IsNamedKey(key) ? "\"%s\"" : "\"%s\" %d", GetKeyName(key), key); } + Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); + Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; SameLine(); Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. + DebugRenderKeyboardPreview(GetWindowDrawList()); + Unindent(); + } + + Text("MOUSE STATE"); + { + Indent(); + if (IsMousePosValid()) + Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); + else + Text("Mouse pos: "); + Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); + int count = IM_ARRAYSIZE(io.MouseDown); + Text("Mouse down:"); for (int i = 0; i < count; i++) if (IsMouseDown(i)) { SameLine(); Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } + Text("Mouse clicked:"); for (int i = 0; i < count; i++) if (IsMouseClicked(i)) { SameLine(); Text("b%d (%d)", i, io.MouseClickedCount[i]); } + Text("Mouse released:"); for (int i = 0; i < count; i++) if (IsMouseReleased(i)) { SameLine(); Text("b%d", i); } + Text("Mouse wheel: %.1f", io.MouseWheel); + Text("Pen Pressure: %.1f", io.PenPressure); // Note: currently unused + Unindent(); + } + + Text("MOUSE WHEELING"); + { + Indent(); + Text("WheelingWindow: '%s'", g.WheelingWindow ? g.WheelingWindow->Name : "NULL"); + Text("WheelingWindowReleaseTimer: %.2f", g.WheelingWindowReleaseTimer); + Text("WheelingAxisAvg[] = { %.3f, %.3f }, Main Axis: %s", g.WheelingAxisAvg.x, g.WheelingAxisAvg.y, (g.WheelingAxisAvg.x > g.WheelingAxisAvg.y) ? "X" : (g.WheelingAxisAvg.x < g.WheelingAxisAvg.y) ? "Y" : ""); + Unindent(); + } + + Text("KEY OWNERS"); + { + Indent(); + if (BeginListBox("##owners", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 6))) + { + for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) + { + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + if (owner_data->OwnerCurr == ImGuiKeyOwner_None) + continue; + Text("%s: 0x%08X%s", GetKeyName(key), owner_data->OwnerCurr, + owner_data->LockUntilRelease ? " LockUntilRelease" : owner_data->LockThisFrame ? " LockThisFrame" : ""); + DebugLocateItemOnHover(owner_data->OwnerCurr); + } + EndListBox(); + } + Unindent(); + } + Text("SHORTCUT ROUTING"); + { + Indent(); + if (BeginListBox("##routes", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 6))) + { + for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) + { + ImGuiKeyRoutingTable* rt = &g.KeysRoutingTable; + for (ImGuiKeyRoutingIndex idx = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; idx != -1; ) + { + char key_chord_name[64]; + ImGuiKeyRoutingData* routing_data = &rt->Entries[idx]; + GetKeyChordName(key | routing_data->Mods, key_chord_name, IM_ARRAYSIZE(key_chord_name)); + Text("%s: 0x%08X", key_chord_name, routing_data->RoutingCurr); + DebugLocateItemOnHover(routing_data->RoutingCurr); + idx = routing_data->NextEntryIndex; + } + } + EndListBox(); + } + Text("(ActiveIdUsing: AllKeyboardKeys: %d, NavDirMask: 0x%X)", g.ActiveIdUsingAllKeyboardKeys, g.ActiveIdUsingNavDirMask); + Unindent(); + } + TreePop(); + } + if (TreeNode("Internal state")) + { Text("WINDOWING"); Indent(); Text("HoveredWindow: '%s'", g.HoveredWindow ? g.HoveredWindow->Name : "NULL"); @@ -11904,18 +13606,22 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("ITEMS"); Indent(); - Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, input_source_names[g.ActiveIdSource]); + Text("ActiveId: 0x%08X/0x%08X (%.2f sec), AllowOverlap: %d, Source: %s", g.ActiveId, g.ActiveIdPreviousFrame, g.ActiveIdTimer, g.ActiveIdAllowOverlap, GetInputSourceName(g.ActiveIdSource)); + DebugLocateItemOnHover(g.ActiveId); Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); - Text("ActiveIdUsing: Wheel: %d, NavDirMask: %X, NavInputMask: %X, KeyInputMask: %llX", g.ActiveIdUsingMouseWheel, g.ActiveIdUsingNavDirMask, g.ActiveIdUsingNavInputMask, g.ActiveIdUsingKeyInputMask); + Text("ActiveIdUsing: AllKeyboardKeys: %d, NavDirMask: %X", g.ActiveIdUsingAllKeyboardKeys, g.ActiveIdUsingNavDirMask); Text("HoveredId: 0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Not displaying g.HoveredId as it is update mid-frame + Text("HoverDelayId: 0x%08X, Timer: %.2f, ClearTimer: %.2f", g.HoverDelayId, g.HoverDelayTimer, g.HoverDelayClearTimer); Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); + DebugLocateItemOnHover(g.DragDropPayload.SourceId); Unindent(); Text("NAV,FOCUS"); Indent(); Text("NavWindow: '%s'", g.NavWindow ? g.NavWindow->Name : "NULL"); Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); - Text("NavInputSource: %s", input_source_names[g.NavInputSource]); + DebugLocateItemOnHover(g.NavId); + Text("NavInputSource: %s", GetInputSourceName(g.NavInputSource)); Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); Text("NavActivateId/DownId/PressedId/InputId: %08X/%08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId, g.NavActivateInputId); Text("NavActivateFlags: %04X", g.NavActivateFlags); @@ -11989,25 +13695,6 @@ void ImGui::ShowMetricsWindow(bool* p_open) End(); } -// [DEBUG] List fonts in a font atlas and display its texture -void ImGui::ShowFontAtlas(ImFontAtlas* atlas) -{ - for (int i = 0; i < atlas->Fonts.Size; i++) - { - ImFont* font = atlas->Fonts[i]; - PushID(font); - DebugNodeFont(font); - PopID(); - } - if (TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) - { - ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); - Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), tint_col, border_col); - TreePop(); - } -} - // [DEBUG] Display contents of Columns void ImGui::DebugNodeColumns(ImGuiOldColumns* columns) { @@ -12217,17 +13904,13 @@ void ImGui::DebugNodeFont(ImFont* font) ImVec2 cell_p2(cell_p1.x + cell_size, cell_p1.y + cell_size); const ImFontGlyph* glyph = font->FindGlyphNoFallback((ImWchar)(base + n)); draw_list->AddRect(cell_p1, cell_p2, glyph ? IM_COL32(255, 255, 255, 100) : IM_COL32(255, 255, 255, 50)); - if (glyph) - font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); - if (glyph && IsMouseHoveringRect(cell_p1, cell_p2)) + if (!glyph) + continue; + font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); + if (IsMouseHoveringRect(cell_p1, cell_p2)) { BeginTooltip(); - Text("Codepoint: U+%04X", base + n); - Separator(); - Text("Visible: %d", glyph->Visible); - Text("AdvanceX: %.1f", glyph->AdvanceX); - Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); - Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); + DebugNodeFontGlyph(font, glyph); EndTooltip(); } } @@ -12239,6 +13922,16 @@ void ImGui::DebugNodeFont(ImFont* font) TreePop(); } +void ImGui::DebugNodeFontGlyph(ImFont*, const ImFontGlyph* glyph) +{ + Text("Codepoint: U+%04X", glyph->Codepoint); + Separator(); + Text("Visible: %d", glyph->Visible); + Text("AdvanceX: %.1f", glyph->AdvanceX); + Text("Pos: (%.2f,%.2f)->(%.2f,%.2f)", glyph->X0, glyph->Y0, glyph->X1, glyph->Y1); + Text("UV: (%.3f,%.3f)->(%.3f,%.3f)", glyph->U0, glyph->V0, glyph->U1, glyph->V1); +} + // [DEBUG] Display contents of ImGuiStorage void ImGui::DebugNodeStorage(ImGuiStorage* storage, const char* label) { @@ -12287,7 +13980,7 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) PushID(tab); if (SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } SameLine(0, 2); if (SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } SameLine(); - Text("%02d%c Tab 0x%08X '%s' Offset: %.1f, Width: %.1f/%.1f", + Text("%02d%c Tab 0x%08X '%s' Offset: %.2f, Width: %.2f/%.2f", tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "???", tab->Offset, tab->Width, tab->ContentWidth); PopID(); } @@ -12351,13 +14044,10 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) { ImRect r = window->NavRectRel[layer]; if (r.Min.x >= r.Max.y && r.Min.y >= r.Max.y) - { BulletText("NavLastIds[%d]: 0x%08X", layer, window->NavLastIds[layer]); - continue; - } - BulletText("NavLastIds[%d]: 0x%08X at +(%.1f,%.1f)(%.1f,%.1f)", layer, window->NavLastIds[layer], r.Min.x, r.Min.y, r.Max.x, r.Max.y); - if (IsItemHovered()) - GetForegroundDrawList(window)->AddRect(r.Min + window->Pos, r.Max + window->Pos, IM_COL32(255, 255, 0, 255)); + else + BulletText("NavLastIds[%d]: 0x%08X at +(%.1f,%.1f)(%.1f,%.1f)", layer, window->NavLastIds[layer], r.Min.x, r.Min.y, r.Max.x, r.Max.y); + DebugLocateItemOnHover(window->NavLastIds[layer]); } BulletText("NavLayersActiveMask: %X, NavLastChildNavWindow: %s", window->DC.NavLayersActiveMask, window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } @@ -12410,10 +14100,126 @@ void ImGui::DebugNodeWindowsListByBeginStackParent(ImGuiWindow** windows, int wi } } +//----------------------------------------------------------------------------- +// [SECTION] DEBUG LOG WINDOW +//----------------------------------------------------------------------------- + +void ImGui::DebugLog(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + DebugLogV(fmt, args); + va_end(args); +} + +void ImGui::DebugLogV(const char* fmt, va_list args) +{ + ImGuiContext& g = *GImGui; + const int old_size = g.DebugLogBuf.size(); + g.DebugLogBuf.appendf("[%05d] ", g.FrameCount); + g.DebugLogBuf.appendfv(fmt, args); + if (g.DebugLogFlags & ImGuiDebugLogFlags_OutputToTTY) + IMGUI_DEBUG_PRINTF("%s", g.DebugLogBuf.begin() + old_size); + g.DebugLogIndex.append(g.DebugLogBuf.c_str(), old_size, g.DebugLogBuf.size()); +} + +void ImGui::ShowDebugLogWindow(bool* p_open) +{ + ImGuiContext& g = *GImGui; + if (!(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize)) + SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 12.0f), ImGuiCond_FirstUseEver); + if (!Begin("Dear ImGui Debug Log", p_open) || GetCurrentWindow()->BeginCount > 1) + { + End(); + return; + } + + AlignTextToFramePadding(); + Text("Log events:"); + SameLine(); CheckboxFlags("All", &g.DebugLogFlags, ImGuiDebugLogFlags_EventMask_); + SameLine(); CheckboxFlags("ActiveId", &g.DebugLogFlags, ImGuiDebugLogFlags_EventActiveId); + SameLine(); CheckboxFlags("Focus", &g.DebugLogFlags, ImGuiDebugLogFlags_EventFocus); + SameLine(); CheckboxFlags("Popup", &g.DebugLogFlags, ImGuiDebugLogFlags_EventPopup); + SameLine(); CheckboxFlags("Nav", &g.DebugLogFlags, ImGuiDebugLogFlags_EventNav); + SameLine(); CheckboxFlags("Clipper", &g.DebugLogFlags, ImGuiDebugLogFlags_EventClipper); + SameLine(); CheckboxFlags("IO", &g.DebugLogFlags, ImGuiDebugLogFlags_EventIO); + + if (SmallButton("Clear")) + { + g.DebugLogBuf.clear(); + g.DebugLogIndex.clear(); + } + SameLine(); + if (SmallButton("Copy")) + SetClipboardText(g.DebugLogBuf.c_str()); + BeginChild("##log", ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); + + ImGuiListClipper clipper; + clipper.Begin(g.DebugLogIndex.size()); + while (clipper.Step()) + for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) + { + const char* line_begin = g.DebugLogIndex.get_line_begin(g.DebugLogBuf.c_str(), line_no); + const char* line_end = g.DebugLogIndex.get_line_end(g.DebugLogBuf.c_str(), line_no); + TextUnformatted(line_begin, line_end); + ImRect text_rect = g.LastItemData.Rect; + if (IsItemHovered()) + for (const char* p = line_begin; p < line_end - 10; p++) + { + ImGuiID id = 0; + if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X') || sscanf(p + 2, "%X", &id) != 1) + continue; + ImVec2 p0 = CalcTextSize(line_begin, p); + ImVec2 p1 = CalcTextSize(p, p + 10); + g.LastItemData.Rect = ImRect(text_rect.Min + ImVec2(p0.x, 0.0f), text_rect.Min + ImVec2(p0.x + p1.x, p1.y)); + if (IsMouseHoveringRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, true)) + DebugLocateItemOnHover(id); + p += 10; + } + } + if (GetScrollY() >= GetScrollMaxY()) + SetScrollHereY(1.0f); + EndChild(); + + End(); +} + //----------------------------------------------------------------------------- // [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) //----------------------------------------------------------------------------- +static const ImU32 DEBUG_LOCATE_ITEM_COLOR = IM_COL32(0, 255, 0, 255); // Green + +void ImGui::DebugLocateItem(ImGuiID target_id) +{ + ImGuiContext& g = *GImGui; + g.DebugLocateId = target_id; + g.DebugLocateFrames = 2; +} + +void ImGui::DebugLocateItemOnHover(ImGuiID target_id) +{ + if (target_id == 0 || !IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenBlockedByPopup)) + return; + ImGuiContext& g = *GImGui; + DebugLocateItem(target_id); + GetForegroundDrawList(g.CurrentWindow)->AddRect(g.LastItemData.Rect.Min - ImVec2(3.0f, 3.0f), g.LastItemData.Rect.Max + ImVec2(3.0f, 3.0f), DEBUG_LOCATE_ITEM_COLOR); +} + +void ImGui::DebugLocateItemResolveWithLastItem() +{ + ImGuiContext& g = *GImGui; + ImGuiLastItemData item_data = g.LastItemData; + g.DebugLocateId = 0; + ImDrawList* draw_list = GetForegroundDrawList(g.CurrentWindow); + ImRect r = item_data.Rect; + r.Expand(3.0f); + ImVec2 p1 = g.IO.MousePos; + ImVec2 p2 = ImVec2((p1.x < r.Min.x) ? r.Min.x : (p1.x > r.Max.x) ? r.Max.x : p1.x, (p1.y < r.Min.y) ? r.Min.y : (p1.y > r.Max.y) ? r.Max.y : p1.y); + draw_list->AddRect(r.Min, r.Max, DEBUG_LOCATE_ITEM_COLOR); + draw_list->AddLine(p1, p2, DEBUG_LOCATE_ITEM_COLOR); +} + // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. void ImGui::UpdateDebugToolItemPicker() { @@ -12424,18 +14230,26 @@ void ImGui::UpdateDebugToolItemPicker() const ImGuiID hovered_id = g.HoveredIdPreviousFrame; SetMouseCursor(ImGuiMouseCursor_Hand); - if (IsKeyPressedMap(ImGuiKey_Escape)) + if (IsKeyPressed(ImGuiKey_Escape)) g.DebugItemPickerActive = false; - if (IsMouseClicked(0) && hovered_id) + const bool change_mapping = g.IO.KeyMods == (ImGuiMod_Ctrl | ImGuiMod_Shift); + if (!change_mapping && IsMouseClicked(g.DebugItemPickerMouseButton) && hovered_id) { g.DebugItemPickerBreakId = hovered_id; g.DebugItemPickerActive = false; } - SetNextWindowBgAlpha(0.60f); + for (int mouse_button = 0; mouse_button < 3; mouse_button++) + if (change_mapping && IsMouseClicked(mouse_button)) + g.DebugItemPickerMouseButton = (ImU8)mouse_button; + SetNextWindowBgAlpha(0.70f); BeginTooltip(); Text("HoveredId: 0x%08X", hovered_id); Text("Press ESC to abort picking."); - TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click to break in debugger!"); + const char* mouse_button_names[] = { "Left", "Right", "Middle" }; + if (change_mapping) + Text("Remap w/ Ctrl+Shift: click anywhere to select new mouse button."); + else + TextColored(GetStyleColorVec4(hovered_id ? ImGuiCol_Text : ImGuiCol_TextDisabled), "Click %s Button to break in debugger! (remap w/ Ctrl+Shift)", mouse_button_names[g.DebugItemPickerMouseButton]); EndTooltip(); } @@ -12487,7 +14301,7 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat ImGuiStackTool* tool = &g.DebugStackTool; // Step 0: stack query - // This assume that the ID was computed with the current ID stack, which tends to be the case for our widget. + // This assumes that the ID was computed with the current ID stack, which tends to be the case for our widget. if (tool->StackLevel == -1) { tool->StackLevel++; @@ -12504,27 +14318,44 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat ImGuiStackLevelInfo* info = &tool->Results[tool->StackLevel]; IM_ASSERT(info->ID == id && info->QueryFrameCount > 0); - int data_len; switch (data_type) { case ImGuiDataType_S32: ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%d", (int)(intptr_t)data_id); break; case ImGuiDataType_String: - data_len = data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)strlen((const char*)data_id); - ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "\"%.*s\"", data_len, (const char*)data_id); + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "%.*s", data_id_end ? (int)((const char*)data_id_end - (const char*)data_id) : (int)strlen((const char*)data_id), (const char*)data_id); break; case ImGuiDataType_Pointer: ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "(void*)0x%p", data_id); break; case ImGuiDataType_ID: - if (info->Desc[0] == 0) // PushOverrideID() is often used to avoid hashing twice, which would lead to 2 calls to DebugHookIdInfo(). We prioritize the first one. - ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "0x%08X [override]", id); + if (info->Desc[0] != 0) // PushOverrideID() is often used to avoid hashing twice, which would lead to 2 calls to DebugHookIdInfo(). We prioritize the first one. + return; + ImFormatString(info->Desc, IM_ARRAYSIZE(info->Desc), "0x%08X [override]", id); break; default: IM_ASSERT(0); } info->QuerySuccess = true; + info->DataType = data_type; +} + +static int StackToolFormatLevelInfo(ImGuiStackTool* tool, int n, bool format_for_ui, char* buf, size_t buf_size) +{ + ImGuiStackLevelInfo* info = &tool->Results[n]; + ImGuiWindow* window = (info->Desc[0] == 0 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL; + if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked) + return ImFormatString(buf, buf_size, format_for_ui ? "\"%s\" [window]" : "%s", window->Name); + if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id) + return ImFormatString(buf, buf_size, (format_for_ui && info->DataType == ImGuiDataType_String) ? "\"%s\"" : "%s", info->Desc); + if (tool->StackLevel < tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers. + return (*buf = 0); +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (const char* label = ImGuiTestEngine_FindItemDebugLabel(GImGui, info->ID)) // Source: ImGuiTestEngine's ItemInfo() + return ImFormatString(buf, buf_size, format_for_ui ? "??? \"%s\"" : "%s", label); +#endif + return ImFormatString(buf, buf_size, "???"); } // Stack Tool: Display UI @@ -12540,6 +14371,7 @@ void ImGui::ShowStackToolWindow(bool* p_open) } // Display hovered/active status + ImGuiStackTool* tool = &g.DebugStackTool; const ImGuiID hovered_id = g.HoveredIdPreviousFrame; const ImGuiID active_id = g.ActiveId; #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -12550,8 +14382,33 @@ void ImGui::ShowStackToolWindow(bool* p_open) SameLine(); MetricsHelpMarker("Hover an item with the mouse to display elements of the ID Stack leading to the item's final ID.\nEach level of the stack correspond to a PushID() call.\nAll levels of the stack are hashed together to make the final ID of a widget (ID displayed at the bottom level of the stack).\nRead FAQ entry about the ID stack for details."); + // CTRL+C to copy path + const float time_since_copy = (float)g.Time - tool->CopyToClipboardLastTime; + Checkbox("Ctrl+C: copy path to clipboard", &tool->CopyToClipboardOnCtrlC); + SameLine(); + TextColored((time_since_copy >= 0.0f && time_since_copy < 0.75f && ImFmod(time_since_copy, 0.25f) < 0.25f * 0.5f) ? ImVec4(1.f, 1.f, 0.3f, 1.f) : ImVec4(), "*COPIED*"); + if (tool->CopyToClipboardOnCtrlC && IsKeyDown(ImGuiMod_Ctrl) && IsKeyPressed(ImGuiKey_C)) + { + tool->CopyToClipboardLastTime = (float)g.Time; + char* p = g.TempBuffer.Data; + char* p_end = p + g.TempBuffer.Size; + for (int stack_n = 0; stack_n < tool->Results.Size && p + 3 < p_end; stack_n++) + { + *p++ = '/'; + char level_desc[256]; + StackToolFormatLevelInfo(tool, stack_n, false, level_desc, IM_ARRAYSIZE(level_desc)); + for (int n = 0; level_desc[n] && p + 2 < p_end; n++) + { + if (level_desc[n] == '/') + *p++ = '\\'; + *p++ = level_desc[n]; + } + } + *p = '\0'; + SetClipboardText(g.TempBuffer.Data); + } + // Display decorated stack - ImGuiStackTool* tool = &g.DebugStackTool; tool->LastActiveFrame = g.FrameCount; if (tool->Results.Size > 0 && BeginTable("##table", 3, ImGuiTableFlags_Borders)) { @@ -12565,23 +14422,9 @@ void ImGui::ShowStackToolWindow(bool* p_open) ImGuiStackLevelInfo* info = &tool->Results[n]; TableNextColumn(); Text("0x%08X", (n > 0) ? tool->Results[n - 1].ID : 0); - TableNextColumn(); - ImGuiWindow* window = (info->Desc[0] == 0 && n == 0) ? FindWindowByID(info->ID) : NULL; - if (window) // Source: window name (because the root ID don't call GetID() and so doesn't get hooked) - Text("\"%s\" [window]", window->Name); - else if (info->QuerySuccess) // Source: GetID() hooks (prioritize over ItemInfo() because we frequently use patterns like: PushID(str), Button("") where they both have same id) - TextUnformatted(info->Desc); - else if (tool->StackLevel >= tool->Results.Size) // Only start using fallback below when all queries are done, so during queries we don't flickering ??? markers. - { -#ifdef IMGUI_ENABLE_TEST_ENGINE - if (const char* label = ImGuiTestEngine_FindItemDebugLabel(&g, info->ID)) // Source: ImGuiTestEngine's ItemInfo() - Text("??? \"%s\"", label); - else -#endif - TextUnformatted("???"); - } - + StackToolFormatLevelInfo(tool, n, true, g.TempBuffer.Data, g.TempBuffer.Size); + TextUnformatted(g.TempBuffer.Data); TableNextColumn(); Text("0x%08X", info->ID); if (n == tool->Results.Size - 1) @@ -12607,12 +14450,15 @@ void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings*) {} void ImGui::DebugNodeWindowsList(ImVector*, const char*) {} void ImGui::DebugNodeViewport(ImGuiViewportP*) {} +void ImGui::DebugLog(const char*, ...) {} +void ImGui::DebugLogV(const char*, va_list) {} +void ImGui::ShowDebugLogWindow(bool*) {} void ImGui::ShowStackToolWindow(bool*) {} void ImGui::DebugHookIdInfo(ImGuiID, ImGuiDataType, const void*, const void*) {} void ImGui::UpdateDebugToolItemPicker() {} void ImGui::UpdateDebugToolStackQueries() {} -#endif // #ifndef IMGUI_DISABLE_METRICS_WINDOW +#endif // #ifndef IMGUI_DISABLE_DEBUG_TOOLS //----------------------------------------------------------------------------- diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 9cbc503a..c1b7dbe5 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (demo code) // Help: @@ -8,11 +8,15 @@ // Read imgui.cpp for more details, documentation and comments. // Get the latest version at https://github.com/ocornut/imgui +// ------------------------------------------------- +// PLEASE DO NOT REMOVE THIS FILE FROM YOUR PROJECT! +// ------------------------------------------------- // Message to the person tempted to delete this file when integrating Dear ImGui into their codebase: -// Do NOT remove this file from your project! Think again! It is the most useful reference code that you and other -// coders will want to refer to and call. Have the ImGui::ShowDemoWindow() function wired in an always-available -// debug menu of your game/app! Removing this file from your project is hindering access to documentation for everyone -// in your team, likely leading you to poorer usage of the library. +// Think again! It is the most useful reference code that you and other coders will want to refer to and call. +// Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of your game/app! +// Also include Metrics! ItemPicker! DebugLog! and other debug features. +// Removing this file from your project is hindering access to documentation for everyone in your team, +// likely leading you to poorer usage of the library. // Everything in this file will be stripped out by the linker if you don't call ImGui::ShowDemoWindow(). // If you want to link core Dear ImGui in your shipped builds but want a thorough guarantee that the demo will not be // linked, you can setup your imconfig.h with #define IMGUI_DISABLE_DEMO_WINDOWS and those functions will be empty. @@ -39,22 +43,25 @@ // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. // Navigating this file: -// - In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. /* Index of this file: -// [SECTION] Forward Declarations, Helpers +// [SECTION] Forward Declarations +// [SECTION] Helpers // [SECTION] Demo Window / ShowDemoWindow() +// - ShowDemoWindow() // - sub section: ShowDemoWindowWidgets() // - sub section: ShowDemoWindowLayout() // - sub section: ShowDemoWindowPopups() // - sub section: ShowDemoWindowTables() -// - sub section: ShowDemoWindowMisc() +// - sub section: ShowDemoWindowInputs() // [SECTION] About Window / ShowAboutWindow() // [SECTION] Style Editor / ShowStyleEditor() +// [SECTION] User Guide / ShowUserGuide() // [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar() // [SECTION] Example App: Debug Console / ShowExampleAppConsole() // [SECTION] Example App: Debug Log / ShowExampleAppLog() @@ -92,8 +99,9 @@ Index of this file: // Visual Studio warnings #ifdef _MSC_VER +#pragma warning (disable: 4127) // condition expression is constant #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen -#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). +#pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to an 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). #endif // Clang/GCC warnings with -Weverything @@ -185,12 +193,25 @@ static void ShowExampleAppWindowTitles(bool* p_open); static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleMenuFile(); +// We split the contents of the big ShowDemoWindow() function into smaller functions +// (because the link time of very large functions grow non-linearly) +static void ShowDemoWindowWidgets(); +static void ShowDemoWindowLayout(); +static void ShowDemoWindowPopups(); +static void ShowDemoWindowTables(); +static void ShowDemoWindowColumns(); +static void ShowDemoWindowInputs(); + +//----------------------------------------------------------------------------- +// [SECTION] Helpers +//----------------------------------------------------------------------------- + // Helper to display a little (?) mark which shows a tooltip when hovered. // In your own code you may want to display an actual icon if you are using a merged icon fonts (see docs/FONTS.md) static void HelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered()) + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) { ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); @@ -200,79 +221,38 @@ static void HelpMarker(const char* desc) } } -// Helper to wire demo markers located in code to a interactive browser +// Helper to wire demo markers located in code to an interactive browser typedef void (*ImGuiDemoMarkerCallback)(const char* file, int line, const char* section, void* user_data); -extern ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback; -extern void* GImGuiDemoMarkerCallbackUserData; -ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback = NULL; -void* GImGuiDemoMarkerCallbackUserData = NULL; +extern ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback; +extern void* GImGuiDemoMarkerCallbackUserData; +ImGuiDemoMarkerCallback GImGuiDemoMarkerCallback = NULL; +void* GImGuiDemoMarkerCallbackUserData = NULL; #define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback(__FILE__, __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0) -// Helper to display basic user controls. -void ImGui::ShowUserGuide() -{ - ImGuiIO& io = ImGui::GetIO(); - ImGui::BulletText("Double-click on title bar to collapse window."); - ImGui::BulletText( - "Click and drag on lower corner to resize window\n" - "(double-click to auto fit window to its contents)."); - ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); - ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); - ImGui::BulletText("CTRL+Tab to select a window."); - if (io.FontAllowUserScaling) - ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents."); - ImGui::BulletText("While inputing text:\n"); - ImGui::Indent(); - ImGui::BulletText("CTRL+Left/Right to word jump."); - ImGui::BulletText("CTRL+A or double-click to select all."); - ImGui::BulletText("CTRL+X/C/V to use clipboard cut/copy/paste."); - ImGui::BulletText("CTRL+Z,CTRL+Y to undo/redo."); - ImGui::BulletText("ESCAPE to revert."); - ImGui::BulletText("You can apply arithmetic operators +,*,/ on numerical values.\nUse +- to subtract."); - ImGui::Unindent(); - ImGui::BulletText("With keyboard navigation enabled:"); - ImGui::Indent(); - ImGui::BulletText("Arrow keys to navigate."); - ImGui::BulletText("Space to activate a widget."); - ImGui::BulletText("Return to input text into a widget."); - ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window."); - ImGui::BulletText("Alt to jump to the menu layer of a window."); - ImGui::Unindent(); -} - //----------------------------------------------------------------------------- // [SECTION] Demo Window / ShowDemoWindow() //----------------------------------------------------------------------------- +// - ShowDemoWindow() // - ShowDemoWindowWidgets() // - ShowDemoWindowLayout() // - ShowDemoWindowPopups() // - ShowDemoWindowTables() // - ShowDemoWindowColumns() -// - ShowDemoWindowMisc() +// - ShowDemoWindowInputs() //----------------------------------------------------------------------------- -// We split the contents of the big ShowDemoWindow() function into smaller functions -// (because the link time of very large functions grow non-linearly) -static void ShowDemoWindowWidgets(); -static void ShowDemoWindowLayout(); -static void ShowDemoWindowPopups(); -static void ShowDemoWindowTables(); -static void ShowDemoWindowColumns(); -static void ShowDemoWindowMisc(); - // Demonstrate most Dear ImGui features (this is big function!) // You may execute this function to experiment with the UI and understand what it does. // You may then search for keywords in the code when you are interested by a specific feature. void ImGui::ShowDemoWindow(bool* p_open) { // Exceptionally add an extra assert here for people confused about initial Dear ImGui setup - // Most ImGui functions would normally just crash if the context is missing. + // Most functions would normally just crash if the context is missing. IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); // Examples Apps (accessible from the "Examples" menu) static bool show_app_main_menu_bar = false; static bool show_app_documents = false; - static bool show_app_console = false; static bool show_app_log = false; static bool show_app_layout = false; @@ -287,7 +267,6 @@ void ImGui::ShowDemoWindow(bool* p_open) if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); - if (show_app_console) ShowExampleAppConsole(&show_app_console); if (show_app_log) ShowExampleAppLog(&show_app_log); if (show_app_layout) ShowExampleAppLayout(&show_app_layout); @@ -300,15 +279,21 @@ void ImGui::ShowDemoWindow(bool* p_open) if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); - // Dear ImGui Apps (accessible from the "Tools" menu) + // Dear ImGui Tools/Apps (accessible from the "Tools" menu) static bool show_app_metrics = false; + static bool show_app_debug_log = false; static bool show_app_stack_tool = false; - static bool show_app_style_editor = false; static bool show_app_about = false; + static bool show_app_style_editor = false; - if (show_app_metrics) { ImGui::ShowMetricsWindow(&show_app_metrics); } - if (show_app_stack_tool) { ImGui::ShowStackToolWindow(&show_app_stack_tool); } - if (show_app_about) { ImGui::ShowAboutWindow(&show_app_about); } + if (show_app_metrics) + ImGui::ShowMetricsWindow(&show_app_metrics); + if (show_app_debug_log) + ImGui::ShowDebugLogWindow(&show_app_debug_log); + if (show_app_stack_tool) + ImGui::ShowStackToolWindow(&show_app_stack_tool); + if (show_app_about) + ImGui::ShowAboutWindow(&show_app_about); if (show_app_style_editor) { ImGui::Begin("Dear ImGui Style Editor", &show_app_style_editor); @@ -357,10 +342,8 @@ void ImGui::ShowDemoWindow(bool* p_open) } // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details. - // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align) //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); - // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets. ImGui::PushItemWidth(ImGui::GetFontSize() * -12); @@ -395,10 +378,14 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::BeginMenu("Tools")) { IMGUI_DEMO_MARKER("Menu/Tools"); -#ifndef IMGUI_DISABLE_METRICS_WINDOW - ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics); - ImGui::MenuItem("Stack Tool", NULL, &show_app_stack_tool); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + const bool has_debug_tools = true; +#else + const bool has_debug_tools = false; #endif + ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools); + ImGui::MenuItem("Debug Log", NULL, &show_app_debug_log, has_debug_tools); + ImGui::MenuItem("Stack Tool", NULL, &show_app_stack_tool, has_debug_tools); ImGui::MenuItem("Style Editor", NULL, &show_app_style_editor); ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about); ImGui::EndMenu(); @@ -406,7 +393,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::EndMenuBar(); } - ImGui::Text("dear imgui says hello. (%s)", IMGUI_VERSION); + ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); ImGui::Spacing(); IMGUI_DEMO_MARKER("Help"); @@ -454,13 +441,17 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); ImGui::Text("<>"); } - if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Space))) + if (ImGui::IsKeyPressed(ImGuiKey_Space)) io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse; } ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility."); + ImGui::Checkbox("io.ConfigInputTrickleEventQueue", &io.ConfigInputTrickleEventQueue); + ImGui::SameLine(); HelpMarker("Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates."); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); - ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting)"); + ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting)."); + ImGui::Checkbox("io.ConfigInputTextEnterKeepActive", &io.ConfigInputTextEnterKeepActive); + ImGui::SameLine(); HelpMarker("Pressing Enter will keep item active and select contents (single-line only)."); ImGui::Checkbox("io.ConfigDragClickToInputText", &io.ConfigDragClickToInputText); ImGui::SameLine(); HelpMarker("Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving)."); ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); @@ -481,6 +472,7 @@ void ImGui::ShowDemoWindow(bool* p_open) "Here we expose them as read-only fields to avoid breaking interactions with your backend."); // Make a local copy to avoid modifying actual backend flags. + // FIXME: We don't use BeginDisabled() to keep label bright, maybe we need a BeginReadonly() equivalent.. ImGuiBackendFlags backend_flags = io.BackendFlags; ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", &backend_flags, ImGuiBackendFlags_HasGamepad); ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &backend_flags, ImGuiBackendFlags_HasMouseCursors); @@ -544,7 +536,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ShowDemoWindowLayout(); ShowDemoWindowPopups(); ShowDemoWindowTables(); - ShowDemoWindowMisc(); + ShowDemoWindowInputs(); // End of ShowDemoWindow() ImGui::PopItemWidth(); @@ -618,22 +610,6 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); ImGui::Text("%d", counter); - IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); - ImGui::Text("Hover over me"); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("I am a tooltip"); - - ImGui::SameLine(); - ImGui::Text("- or me"); - if (ImGui::IsItemHovered()) - { - ImGui::BeginTooltip(); - ImGui::Text("I am a fancy tooltip"); - static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; - ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); - ImGui::EndTooltip(); - } - ImGui::Separator(); ImGui::LabelText("label", "Value"); @@ -658,7 +634,7 @@ static void ShowDemoWindowWidgets() "USER:\n" "Hold SHIFT or use mouse to select text.\n" "CTRL+Left/Right to word jump.\n" - "CTRL+A or double-click to select all.\n" + "CTRL+A or Double-Click to select all.\n" "CTRL+X,CTRL+C,CTRL+V clipboard.\n" "CTRL+Z,CTRL+Y undo/redo.\n" "ESCAPE to revert.\n\n" @@ -673,10 +649,6 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Basic/InputInt, InputFloat"); static int i0 = 123; ImGui::InputInt("input int", &i0); - ImGui::SameLine(); HelpMarker( - "You can apply arithmetic operators +,*,/ on numerical values.\n" - " e.g. [ 100 ], input \'*2\', result becomes [ 200 ]\n" - "Use +- to subtract."); static float f0 = 0.001f; ImGui::InputFloat("input float", &f0, 0.01f, 1.0f, "%.3f"); @@ -761,6 +733,40 @@ static void ShowDemoWindowWidgets() "Using the simplified one-liner ListBox API here.\nRefer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API."); } + { + // Tooltips + IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); + ImGui::AlignTextToFramePadding(); + ImGui::Text("Tooltips:"); + + ImGui::SameLine(); + ImGui::Button("Button"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("I am a tooltip"); + + ImGui::SameLine(); + ImGui::Button("Fancy"); + if (ImGui::IsItemHovered()) + { + ImGui::BeginTooltip(); + ImGui::Text("I am a fancy tooltip"); + static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; + ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); + ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime())); + ImGui::EndTooltip(); + } + + ImGui::SameLine(); + ImGui::Button("Delayed"); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal)) // Delay best used on items that highlight on hover, so this not a great example! + ImGui::SetTooltip("I am a tooltip with a delay."); + + ImGui::SameLine(); + HelpMarker( + "Tooltip are created by using the IsItemHovered() function over any kind of item."); + + } + ImGui::TreePop(); } @@ -979,7 +985,7 @@ static void ShowDemoWindowWidgets() // Note that characters values are preserved even by InputText() if the font cannot be displayed, // so you can safely copy & paste garbled characters into another application. ImGui::TextWrapped( - "CJK text will only appears if the font was loaded with the appropriate CJK character ranges. " + "CJK text will only appear if the font was loaded with the appropriate CJK character ranges. " "Call io.Fonts->AddFontFromFileTTF() manually to load extra character ranges. " "Read docs/FONTS.md for details."); ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. @@ -1052,15 +1058,21 @@ static void ShowDemoWindowWidgets() static int pressed_count = 0; for (int i = 0; i < 8; i++) { + // UV coordinates are often (0.0f, 0.0f) and (1.0f, 1.0f) to display an entire textures. + // Here are trying to display only a 32x32 pixels area of the texture, hence the UV computation. + // Read about UV coordinates here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples ImGui::PushID(i); - int frame_padding = -1 + i; // -1 == uses default padding (style.FramePadding) - ImVec2 size = ImVec2(32.0f, 32.0f); // Size of the image we want to make visible - ImVec2 uv0 = ImVec2(0.0f, 0.0f); // UV coordinates for lower-left - ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32.0f / my_tex_h);// UV coordinates for (32,32) in our texture - ImVec4 bg_col = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); // Black background - ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint - if (ImGui::ImageButton(my_tex_id, size, uv0, uv1, frame_padding, bg_col, tint_col)) + if (i > 0) + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(i - 1.0f, i - 1.0f)); + ImVec2 size = ImVec2(32.0f, 32.0f); // Size of the image we want to make visible + ImVec2 uv0 = ImVec2(0.0f, 0.0f); // UV coordinates for lower-left + ImVec2 uv1 = ImVec2(32.0f / my_tex_w, 32.0f / my_tex_h); // UV coordinates for (32,32) in our texture + ImVec4 bg_col = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); // Black background + ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint + if (ImGui::ImageButton("", my_tex_id, size, uv0, uv1, bg_col, tint_col)) pressed_count += 1; + if (i > 0) + ImGui::PopStyleVar(); ImGui::PopID(); ImGui::SameLine(); } @@ -1072,6 +1084,7 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Combo"); if (ImGui::TreeNode("Combo")) { + // Combo Boxes are also called "Dropdown" in other systems // Expose flags as checkbox for the demo static ImGuiComboFlags flags = 0; ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", &flags, ImGuiComboFlags_PopupAlignLeft); @@ -1436,7 +1449,7 @@ static void ShowDemoWindowWidgets() static char buf3[64]; static int edit_count = 0; ImGui::InputText("Edit", buf3, 64, ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count); - ImGui::SameLine(); HelpMarker("Here we toggle the casing of the first character on every edits + count edits."); + ImGui::SameLine(); HelpMarker("Here we toggle the casing of the first character on every edit + count edits."); ImGui::SameLine(); ImGui::Text("(%d)", edit_count); ImGui::TreePop(); @@ -1856,10 +1869,9 @@ static void ShowDemoWindowWidgets() ImGui::Combo("Display Mode", &display_mode, "Auto/Current\0None\0RGB Only\0HSV Only\0Hex Only\0"); ImGui::SameLine(); HelpMarker( "ColorEdit defaults to displaying RGB inputs if you don't specify a display mode, " - "but the user can change it with a right-click.\n\nColorPicker defaults to displaying RGB+HSV+Hex " + "but the user can change it with a right-click on those inputs.\n\nColorPicker defaults to displaying RGB+HSV+Hex " "if you don't specify a display mode.\n\nYou can change the defaults using SetColorEditOptions()."); - ImGui::Combo("Picker Mode", &picker_mode, "Auto/Current\0Hue bar + SV rect\0Hue wheel + SV triangle\0"); - ImGui::SameLine(); HelpMarker("User can right-click the picker to change mode."); + ImGui::SameLine(); HelpMarker("When not specified explicitly (Auto/Current mode), user can right-click the picker to change mode."); ImGuiColorEditFlags flags = misc_flags; if (!alpha) flags |= ImGuiColorEditFlags_NoAlpha; // This is by default if you call ColorPicker3() instead of ColorPicker4() if (alpha_bar) flags |= ImGuiColorEditFlags_AlphaBar; @@ -1883,6 +1895,15 @@ static void ShowDemoWindowWidgets() if (ImGui::Button("Default: Float + HDR + Hue Wheel")) ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel); + // Always both a small version of both types of pickers (to make it more visible in the demo to people who are skimming quickly through it) + ImGui::Text("Both types:"); + float w = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.y) * 0.40f; + ImGui::SetNextItemWidth(w); + ImGui::ColorPicker3("##MyColor##5", (float*)&color, ImGuiColorEditFlags_PickerHueBar | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha); + ImGui::SameLine(); + ImGui::SetNextItemWidth(w); + ImGui::ColorPicker3("##MyColor##6", (float*)&color, ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_NoSidePreview | ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha); + // HSV encoded support (to avoid RGB<>HSV round trips and singularities when S==0 or V==0) static ImVec4 color_hsv(0.23f, 1.0f, 1.0f, 1.0f); // Stored as HSV! ImGui::Spacing(); @@ -1953,7 +1974,7 @@ static void ShowDemoWindowWidgets() // - integer/float/double // To avoid polluting the public API with all possible combinations, we use the ImGuiDataType enum // to pass the type, and passing all arguments by pointer. - // This is the reason the test code below creates local variables to hold "zero" "one" etc. for each types. + // This is the reason the test code below creates local variables to hold "zero" "one" etc. for each type. // In practice, if you frequently use a given type that is not covered by the normal API entry points, // you can wrap it yourself inside a 1 line function which can take typed argument as value instead of void*, // and then pass their address to the generic function. For example: @@ -1998,13 +2019,14 @@ static void ShowDemoWindowWidgets() ImGui::Text("Drags:"); ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); HelpMarker( - "As with every widgets in dear imgui, we never modify values unless there is a user interaction.\n" + "As with every widget in dear imgui, we never modify values unless there is a user interaction.\n" "You can override the clamping limits by using CTRL+Click to input a value."); ImGui::DragScalar("drag s8", ImGuiDataType_S8, &s8_v, drag_speed, drag_clamp ? &s8_zero : NULL, drag_clamp ? &s8_fifty : NULL); ImGui::DragScalar("drag u8", ImGuiDataType_U8, &u8_v, drag_speed, drag_clamp ? &u8_zero : NULL, drag_clamp ? &u8_fifty : NULL, "%u ms"); ImGui::DragScalar("drag s16", ImGuiDataType_S16, &s16_v, drag_speed, drag_clamp ? &s16_zero : NULL, drag_clamp ? &s16_fifty : NULL); ImGui::DragScalar("drag u16", ImGuiDataType_U16, &u16_v, drag_speed, drag_clamp ? &u16_zero : NULL, drag_clamp ? &u16_fifty : NULL, "%u ms"); ImGui::DragScalar("drag s32", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL); + ImGui::DragScalar("drag s32 hex", ImGuiDataType_S32, &s32_v, drag_speed, drag_clamp ? &s32_zero : NULL, drag_clamp ? &s32_fifty : NULL, "0x%08X"); ImGui::DragScalar("drag u32", ImGuiDataType_U32, &u32_v, drag_speed, drag_clamp ? &u32_zero : NULL, drag_clamp ? &u32_fifty : NULL, "%u ms"); ImGui::DragScalar("drag s64", ImGuiDataType_S64, &s64_v, drag_speed, drag_clamp ? &s64_zero : NULL, drag_clamp ? &s64_fifty : NULL); ImGui::DragScalar("drag u64", ImGuiDataType_U64, &u64_v, drag_speed, drag_clamp ? &u64_zero : NULL, drag_clamp ? &u64_fifty : NULL); @@ -2022,6 +2044,7 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider s32 low", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty,"%d"); ImGui::SliderScalar("slider s32 high", ImGuiDataType_S32, &s32_v, &s32_hi_a, &s32_hi_b, "%d"); ImGui::SliderScalar("slider s32 full", ImGuiDataType_S32, &s32_v, &s32_min, &s32_max, "%d"); + ImGui::SliderScalar("slider s32 hex", ImGuiDataType_S32, &s32_v, &s32_zero, &s32_fifty, "0x%04X"); ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); @@ -2055,9 +2078,9 @@ static void ShowDemoWindowWidgets() ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d"); ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u"); ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d"); - ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%08X", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%04X"); ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u"); - ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X", ImGuiInputTextFlags_CharsHexadecimal); + ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X"); ImGui::InputScalar("input s64", ImGuiDataType_S64, &s64_v, inputs_step ? &s64_one : NULL); ImGui::InputScalar("input u64", ImGuiDataType_U64, &u64_v, inputs_step ? &u64_one : NULL); ImGui::InputScalar("input float", ImGuiDataType_Float, &f32_v, inputs_step ? &f32_one : NULL); @@ -2295,7 +2318,7 @@ static void ShowDemoWindowWidgets() HelpMarker("Testing how various types of items are interacting with the IsItemXXX functions. Note that the bool return value of most ImGui function is generally equivalent to calling ImGui::IsItemHovered()."); ImGui::Checkbox("Item Disabled", &item_disabled); - // Submit selected item item so we can query their status in the code following it. + // Submit selected items so we can query their status in the code following it. bool ret = false; static bool b = false; static float col4f[4] = { 1.0f, 0.5, 0.0f, 1.0f }; @@ -2319,6 +2342,10 @@ static void ShowDemoWindowWidgets() if (item_type == 14){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::Combo("ITEM: Combo", ¤t, items, IM_ARRAYSIZE(items)); } if (item_type == 15){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } + bool hovered_delay_none = ImGui::IsItemHovered(); + bool hovered_delay_short = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort); + bool hovered_delay_normal = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal); + // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. // Because BulletText is an item itself and that would affect the output of IsItemXXX functions, @@ -2363,6 +2390,8 @@ static void ShowDemoWindowWidgets() ImGui::GetItemRectMax().x, ImGui::GetItemRectMax().y, ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y ); + ImGui::BulletText( + "w/ Hovering Delay: None = %d, Fast %d, Normal = %d", hovered_delay_none, hovered_delay_short, hovered_delay_normal); if (item_disabled) ImGui::EndDisabled(); @@ -2467,6 +2496,26 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); HelpMarker("Demonstrate using BeginDisabled()/EndDisabled() across this section."); ImGui::TreePop(); } + + IMGUI_DEMO_MARKER("Widgets/Text Filter"); + if (ImGui::TreeNode("Text Filter")) + { + // Helper class to easy setup a text filter. + // You may want to implement a more feature-full filtering scheme in your own application. + HelpMarker("Not a widget per-se, but ImGuiTextFilter is a helper to perform simple filtering on text strings."); + static ImGuiTextFilter filter; + ImGui::Text("Filter usage:\n" + " \"\" display all lines\n" + " \"xxx\" display lines containing \"xxx\"\n" + " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n" + " \"-xxx\" hide lines containing \"xxx\""); + filter.Draw(); + const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" }; + for (int i = 0; i < IM_ARRAYSIZE(lines); i++) + if (filter.PassFilter(lines[i])) + ImGui::BulletText("%s", lines[i]); + ImGui::TreePop(); + } } static void ShowDemoWindowLayout() @@ -3193,59 +3242,58 @@ static void ShowDemoWindowLayout() ImGui::DragFloat2("size", (float*)&size, 0.5f, 1.0f, 200.0f, "%.0f"); ImGui::TextWrapped("(Click and drag to scroll)"); + HelpMarker( + "(Left) Using ImGui::PushClipRect():\n" + "Will alter ImGui hit-testing logic + ImDrawList rendering.\n" + "(use this if you want your clipping rectangle to affect interactions)\n\n" + "(Center) Using ImDrawList::PushClipRect():\n" + "Will alter ImDrawList rendering only.\n" + "(use this as a shortcut if you are only using ImDrawList calls)\n\n" + "(Right) Using ImDrawList::AddText() with a fine ClipRect:\n" + "Will alter only this specific ImDrawList::AddText() rendering.\n" + "This is often used internally to avoid altering the clipping rectangle and minimize draw calls."); + for (int n = 0; n < 3; n++) { if (n > 0) ImGui::SameLine(); - ImGui::PushID(n); - ImGui::BeginGroup(); // Lock X position - ImGui::InvisibleButton("##empty", size); + ImGui::PushID(n); + ImGui::InvisibleButton("##canvas", size); if (ImGui::IsItemActive() && ImGui::IsMouseDragging(ImGuiMouseButton_Left)) { offset.x += ImGui::GetIO().MouseDelta.x; offset.y += ImGui::GetIO().MouseDelta.y; } + ImGui::PopID(); + if (!ImGui::IsItemVisible()) // Skip rendering as ImDrawList elements are not clipped. + continue; + const ImVec2 p0 = ImGui::GetItemRectMin(); const ImVec2 p1 = ImGui::GetItemRectMax(); const char* text_str = "Line 1 hello\nLine 2 clip me!"; const ImVec2 text_pos = ImVec2(p0.x + offset.x, p0.y + offset.y); ImDrawList* draw_list = ImGui::GetWindowDrawList(); - switch (n) { case 0: - HelpMarker( - "Using ImGui::PushClipRect():\n" - "Will alter ImGui hit-testing logic + ImDrawList rendering.\n" - "(use this if you want your clipping rectangle to affect interactions)"); ImGui::PushClipRect(p0, p1, true); draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); draw_list->AddText(text_pos, IM_COL32_WHITE, text_str); ImGui::PopClipRect(); break; case 1: - HelpMarker( - "Using ImDrawList::PushClipRect():\n" - "Will alter ImDrawList rendering only.\n" - "(use this as a shortcut if you are only using ImDrawList calls)"); draw_list->PushClipRect(p0, p1, true); draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); draw_list->AddText(text_pos, IM_COL32_WHITE, text_str); draw_list->PopClipRect(); break; case 2: - HelpMarker( - "Using ImDrawList::AddText() with a fine ClipRect:\n" - "Will alter only this specific ImDrawList::AddText() rendering.\n" - "(this is often used internally to avoid altering the clipping rectangle and minimize draw calls)"); ImVec4 clip_rect(p0.x, p0.y, p1.x, p1.y); // AddText() takes a ImVec4* here so let's convert. draw_list->AddRectFilled(p0, p1, IM_COL32(90, 90, 120, 255)); draw_list->AddText(ImGui::GetFont(), ImGui::GetFontSize(), text_pos, IM_COL32_WHITE, text_str, NULL, 0.0f, &clip_rect); break; } - ImGui::EndGroup(); - ImGui::PopID(); } ImGui::TreePop(); @@ -3382,7 +3430,7 @@ static void ShowDemoWindowPopups() // if (IsItemHovered() && IsMouseReleased(ImGuiMouseButton_Right)) // OpenPopup(id); // return BeginPopup(id); - // For advanced advanced uses you may want to replicate and customize this code. + // For advanced uses you may want to replicate and customize this code. // See more details in BeginPopupContextItem(). // Example 1 @@ -3390,11 +3438,14 @@ static void ShowDemoWindowPopups() // and BeginPopupContextItem() will use the last item ID as the popup ID. { const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" }; + static int selected = -1; for (int n = 0; n < 5; n++) { - ImGui::Selectable(names[n]); + if (ImGui::Selectable(names[n], selected == n)) + selected = n; if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id { + selected = n; ImGui::Text("This a popup for \"%s\"!", names[n]); if (ImGui::Button("Close")) ImGui::CloseCurrentPopup(); @@ -3411,7 +3462,7 @@ static void ShowDemoWindowPopups() { HelpMarker("Text() elements don't have stable identifiers so we need to provide one."); static float value = 0.5f; - ImGui::Text("Value = %.3f <-- (1) right-click this value", value); + ImGui::Text("Value = %.3f <-- (1) right-click this text", value); if (ImGui::BeginPopupContextItem("my popup")) { if (ImGui::Selectable("Set to zero")) value = 0.0f; @@ -3538,26 +3589,19 @@ static void ShowDemoWindowPopups() ImGui::TextWrapped("Below we are testing adding menu items to a regular window. It's rather unusual but should work!"); ImGui::Separator(); - // Note: As a quirk in this very specific example, we want to differentiate the parent of this menu from the - // parent of the various popup menus above. To do so we are encloding the items in a PushID()/PopID() block - // to make them two different menusets. If we don't, opening any popup above and hovering our menu here would - // open it. This is because once a menu is active, we allow to switch to a sibling menu by just hovering on it, - // which is the desired behavior for regular menus. - ImGui::PushID("foo"); ImGui::MenuItem("Menu item", "CTRL+M"); if (ImGui::BeginMenu("Menu inside a regular window")) { ShowExampleMenuFile(); ImGui::EndMenu(); } - ImGui::PopID(); ImGui::Separator(); ImGui::TreePop(); } } // Dummy data structure that we use for the Table demo. -// (pre-C++11 doesn't allow us to instantiate ImVector template if this structure if defined inside the demo function) +// (pre-C++11 doesn't allow us to instantiate ImVector template if this structure is defined inside the demo function) namespace { // We are passing our own identifier to TableSetupColumn() to facilitate identifying columns in the sorting code. @@ -3776,7 +3820,7 @@ static void ShowDemoWindowTables() } // [Method 2] Using TableNextColumn() called multiple times, instead of using a for loop + TableSetColumnIndex(). - // This is generally more convenient when you have code manually submitting the contents of each columns. + // This is generally more convenient when you have code manually submitting the contents of each column. HelpMarker("Using TableNextRow() + calling TableNextColumn() _before_ each cell, manually."); if (ImGui::BeginTable("table2", 3)) { @@ -3794,10 +3838,10 @@ static void ShowDemoWindowTables() } // [Method 3] We call TableNextColumn() _before_ each cell. We never call TableNextRow(), - // as TableNextColumn() will automatically wrap around and create new roes as needed. + // as TableNextColumn() will automatically wrap around and create new rows as needed. // This is generally more convenient when your cells all contains the same type of data. HelpMarker( - "Only using TableNextColumn(), which tends to be convenient for tables where every cells contains the same type of contents.\n" + "Only using TableNextColumn(), which tends to be convenient for tables where every cell contains the same type of contents.\n" "This is also more similar to the old NextColumn() function of the Columns API, and provided to facilitate the Columns->Tables API transition."); if (ImGui::BeginTable("table3", 3)) { @@ -3849,7 +3893,7 @@ static void ShowDemoWindowTables() ImGui::SameLine(); ImGui::RadioButton("Text", &contents_type, CT_Text); ImGui::SameLine(); ImGui::RadioButton("FillButton", &contents_type, CT_FillButton); ImGui::Checkbox("Display headers", &display_headers); - ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appears in Headers"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers"); PopStyleCompact(); if (ImGui::BeginTable("table1", 3, flags)) @@ -3874,7 +3918,7 @@ static void ShowDemoWindowTables() sprintf(buf, "Hello %d,%d", column, row); if (contents_type == CT_Text) ImGui::TextUnformatted(buf); - else if (contents_type) + else if (contents_type == CT_FillButton) ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); } } @@ -3888,8 +3932,8 @@ static void ShowDemoWindowTables() IMGUI_DEMO_MARKER("Tables/Resizable, stretch"); if (ImGui::TreeNode("Resizable, stretch")) { - // By default, if we don't enable ScrollX the sizing policy for each columns is "Stretch" - // Each columns maintain a sizing weight, and they will occupy all available width. + // By default, if we don't enable ScrollX the sizing policy for each column is "Stretch" + // All columns maintain a sizing weight, and they will occupy all available width. static ImGuiTableFlags flags = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody; PushStyleCompact(); ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); @@ -4011,7 +4055,7 @@ static void ShowDemoWindowTables() ImGui::CheckboxFlags("ImGuiTableFlags_Reorderable", &flags, ImGuiTableFlags_Reorderable); ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable); ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); - ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers)"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)"); PopStyleCompact(); if (ImGui::BeginTable("table1", 3, flags)) @@ -4069,7 +4113,7 @@ static void ShowDemoWindowTables() "- any form of row selection\n" "Because of this, activating BorderOuterV sets the default to PadOuterX. Using PadOuterX or NoPadOuterX you can override the default.\n\n" "Actual padding values are using style.CellPadding.\n\n" - "In this demo we don't show horizontal borders to emphasis how they don't affect default horizontal padding."); + "In this demo we don't show horizontal borders to emphasize how they don't affect default horizontal padding."); static ImGuiTableFlags flags1 = ImGuiTableFlags_BordersV; PushStyleCompact(); @@ -4418,14 +4462,16 @@ static void ShowDemoWindowTables() { ImGui::TableNextColumn(); ImGui::PushID(column); - ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation + ImGui::AlignTextToFramePadding(); // FIXME-TABLE: Workaround for wrong text baseline propagation across columns ImGui::Text("'%s'", column_names[column]); ImGui::Spacing(); ImGui::Text("Input flags:"); EditTableColumnsFlags(&column_flags[column]); ImGui::Spacing(); ImGui::Text("Output flags:"); + ImGui::BeginDisabled(); ShowTableColumnsStatusFlags(column_flags_out[column]); + ImGui::EndDisabled(); ImGui::PopID(); } PopStyleCompact(); @@ -4536,7 +4582,7 @@ static void ShowDemoWindowTables() IMGUI_DEMO_MARKER("Tables/Nested tables"); if (ImGui::TreeNode("Nested tables")) { - HelpMarker("This demonstrate embedding a table into another table cell."); + HelpMarker("This demonstrates embedding a table into another table cell."); if (ImGui::BeginTable("table_nested1", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) { @@ -4581,7 +4627,7 @@ static void ShowDemoWindowTables() IMGUI_DEMO_MARKER("Tables/Row height"); if (ImGui::TreeNode("Row height")) { - HelpMarker("You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\nWe cannot honor a _maximum_ row height as that would requires a unique clipping rectangle per row."); + HelpMarker("You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\nWe cannot honor a _maximum_ row height as that would require a unique clipping rectangle per row."); if (ImGui::BeginTable("table_row_height", 1, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerV)) { for (int row = 0; row < 10; row++) @@ -4830,7 +4876,7 @@ static void ShowDemoWindowTables() ImGui::TableSetColumnIndex(1); ImGui::SliderFloat("float1", &dummy_f, 0.0f, 1.0f); ImGui::TableSetColumnIndex(2); - ImGui::SliderFloat("float2", &dummy_f, 0.0f, 1.0f); + ImGui::SliderFloat("##float2", &dummy_f, 0.0f, 1.0f); // No visible label since right-aligned ImGui::PopID(); } ImGui::EndTable(); @@ -5000,18 +5046,23 @@ static void ShowDemoWindowTables() if (ImGui::TreeNode("Synced instances")) { HelpMarker("Multiple tables with the same identifier will share their settings, width, visibility, order etc."); + + static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings; + ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); + ImGui::CheckboxFlags("ImGuiTableFlags_SizingFixedFit", &flags, ImGuiTableFlags_SizingFixedFit); for (int n = 0; n < 3; n++) { char buf[32]; sprintf(buf, "Synced Table %d", n); bool open = ImGui::CollapsingHeader(buf, ImGuiTreeNodeFlags_DefaultOpen); - if (open && ImGui::BeginTable("Table", 3, ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings)) + if (open && ImGui::BeginTable("Table", 3, flags, ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 5))) { ImGui::TableSetupColumn("One"); ImGui::TableSetupColumn("Two"); ImGui::TableSetupColumn("Three"); ImGui::TableHeadersRow(); - for (int cell = 0; cell < 9; cell++) + const int cell_count = (n == 1) ? 27 : 9; // Make second table have a scrollbar to verify that additional decoration is not affecting column positions. + for (int cell = 0; cell < cell_count; cell++) { ImGui::TableNextColumn(); ImGui::Text("this cell %d", cell); @@ -5143,7 +5194,7 @@ static void ShowDemoWindowTables() static bool show_headers = true; static bool show_wrapped_text = false; //static ImGuiTextFilter filter; - //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // FIXME-TABLE: Enabling this results in initial clipped first pass on table which tend to affects column sizing + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); // FIXME-TABLE: Enabling this results in initial clipped first pass on table which tend to affect column sizing if (ImGui::TreeNode("Options")) { // Make the UI compact because there are so many fields @@ -5170,8 +5221,8 @@ static void ShowDemoWindowTables() ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH); ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH); ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH); - ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appears in Headers"); - ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appears in Headers)"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body (borders will always appear in Headers"); + ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)"); ImGui::TreePop(); } @@ -5234,7 +5285,7 @@ static void ShowDemoWindowTables() HelpMarker("If scrolling is disabled (ScrollX and ScrollY not set):\n" "- The table is output directly in the parent window.\n" "- OuterSize.x < 0.0f will right-align the table.\n" - "- OuterSize.x = 0.0f will narrow fit the table unless there are any Stretch column.\n" + "- OuterSize.x = 0.0f will narrow fit the table unless there are any Stretch columns.\n" "- OuterSize.y then becomes the minimum size for the table, which will extend vertically if there are more rows (unless NoHostExtendY is set)."); // From a user point of view we will tend to use 'inner_width' differently depending on whether our table is embedding scrolling. @@ -5639,82 +5690,121 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } -static void ShowDemoWindowMisc() -{ - IMGUI_DEMO_MARKER("Filtering"); - if (ImGui::CollapsingHeader("Filtering")) - { - // Helper class to easy setup a text filter. - // You may want to implement a more feature-full filtering scheme in your own application. - static ImGuiTextFilter filter; - ImGui::Text("Filter usage:\n" - " \"\" display all lines\n" - " \"xxx\" display lines containing \"xxx\"\n" - " \"xxx,yyy\" display lines containing \"xxx\" or \"yyy\"\n" - " \"-xxx\" hide lines containing \"xxx\""); - filter.Draw(); - const char* lines[] = { "aaa1.c", "bbb1.c", "ccc1.c", "aaa2.cpp", "bbb2.cpp", "ccc2.cpp", "abc.h", "hello, world" }; - for (int i = 0; i < IM_ARRAYSIZE(lines); i++) - if (filter.PassFilter(lines[i])) - ImGui::BulletText("%s", lines[i]); - } +namespace ImGui { extern ImGuiKeyData* GetKeyData(ImGuiKey key); } - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus"); - if (ImGui::CollapsingHeader("Inputs, Navigation & Focus")) +static void ShowDemoWindowInputs() +{ + IMGUI_DEMO_MARKER("Inputs & Focus"); + if (ImGui::CollapsingHeader("Inputs & Focus")) { ImGuiIO& io = ImGui::GetIO(); - // Display ImGuiIO output flags - ImGui::Text("WantCaptureMouse: %d", io.WantCaptureMouse); - ImGui::Text("WantCaptureMouseUnlessPopupClose: %d", io.WantCaptureMouseUnlessPopupClose); - ImGui::Text("WantCaptureKeyboard: %d", io.WantCaptureKeyboard); - ImGui::Text("WantTextInput: %d", io.WantTextInput); - ImGui::Text("WantSetMousePos: %d", io.WantSetMousePos); - ImGui::Text("NavActive: %d, NavVisible: %d", io.NavActive, io.NavVisible); - - // Display Mouse state - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Mouse State"); - if (ImGui::TreeNode("Mouse State")) + // Display inputs submitted to ImGuiIO + IMGUI_DEMO_MARKER("Inputs & Focus/Inputs"); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (ImGui::TreeNode("Inputs")) { + HelpMarker( + "This is a simplified view. See more detailed input state:\n" + "- in 'Tools->Metrics/Debugger->Inputs'.\n" + "- in 'Tools->Debug Log->IO'."); if (ImGui::IsMousePosValid()) ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); else ImGui::Text("Mouse pos: "); ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y); - - int count = IM_ARRAYSIZE(io.MouseDown); - ImGui::Text("Mouse down:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } - ImGui::Text("Mouse clicked:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseClicked(i)) { ImGui::SameLine(); ImGui::Text("b%d (%d)", i, ImGui::GetMouseClickedCount(i)); } - ImGui::Text("Mouse released:"); for (int i = 0; i < count; i++) if (ImGui::IsMouseReleased(i)) { ImGui::SameLine(); ImGui::Text("b%d", i); } + ImGui::Text("Mouse down:"); + for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); - ImGui::Text("Pen Pressure: %.1f", io.PenPressure); // Note: currently unused + + // We iterate both legacy native range and named ImGuiKey ranges, which is a little odd but this allows displaying the data for old/new backends. + // User code should never have to go through such hoops: old code may use native keycodes, new code may use ImGuiKey codes. +#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; +#else + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array +#endif + ImGui::Text("Keys down:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); ImGui::SameLine(); ImGui::Text("(%.02f)", ImGui::GetKeyData(key)->DownDuration); } + ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); + ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. + ImGui::TreePop(); } - // Display Keyboard/Mouse state - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Keyboard & Navigation State"); - if (ImGui::TreeNode("Keyboard & Navigation State")) + // Display ImGuiIO output flags + IMGUI_DEMO_MARKER("Inputs & Focus/Outputs"); + ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (ImGui::TreeNode("Outputs")) { - ImGui::Text("Keys down:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyDown(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X) (%.02f secs)", i, i, io.KeysDownDuration[i]); } - ImGui::Text("Keys pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyPressed(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } - ImGui::Text("Keys release:"); for (int i = 0; i < IM_ARRAYSIZE(io.KeysDown); i++) if (ImGui::IsKeyReleased(i)) { ImGui::SameLine(); ImGui::Text("%d (0x%X)", i, i); } - ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); - ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. + HelpMarker( + "The value of io.WantCaptureMouse and io.WantCaptureKeyboard are normally set by Dear ImGui " + "to instruct your application of how to route inputs. Typically, when a value is true, it means " + "Dear ImGui wants the corresponding inputs and we expect the underlying application to ignore them.\n\n" + "The most typical case is: when hovering a window, Dear ImGui set io.WantCaptureMouse to true, " + "and underlying application should ignore mouse inputs (in practice there are many and more subtle " + "rules leading to how those flags are set)."); + ImGui::Text("io.WantCaptureMouse: %d", io.WantCaptureMouse); + ImGui::Text("io.WantCaptureMouseUnlessPopupClose: %d", io.WantCaptureMouseUnlessPopupClose); + ImGui::Text("io.WantCaptureKeyboard: %d", io.WantCaptureKeyboard); + ImGui::Text("io.WantTextInput: %d", io.WantTextInput); + ImGui::Text("io.WantSetMousePos: %d", io.WantSetMousePos); + ImGui::Text("io.NavActive: %d, io.NavVisible: %d", io.NavActive, io.NavVisible); + + IMGUI_DEMO_MARKER("Inputs & Focus/Outputs/WantCapture override"); + if (ImGui::TreeNode("WantCapture override")) + { + HelpMarker( + "Hovering the colored canvas will override io.WantCaptureXXX fields.\n" + "Notice how normally (when set to none), the value of io.WantCaptureKeyboard would be false when hovering and true when clicking."); + static int capture_override_mouse = -1; + static int capture_override_keyboard = -1; + const char* capture_override_desc[] = { "None", "Set to false", "Set to true" }; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderInt("SetNextFrameWantCaptureMouse() on hover", &capture_override_mouse, -1, +1, capture_override_desc[capture_override_mouse + 1], ImGuiSliderFlags_AlwaysClamp); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 15); + ImGui::SliderInt("SetNextFrameWantCaptureKeyboard() on hover", &capture_override_keyboard, -1, +1, capture_override_desc[capture_override_keyboard + 1], ImGuiSliderFlags_AlwaysClamp); + + ImGui::ColorButton("##panel", ImVec4(0.7f, 0.1f, 0.7f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, ImVec2(128.0f, 96.0f)); // Dummy item + if (ImGui::IsItemHovered() && capture_override_mouse != -1) + ImGui::SetNextFrameWantCaptureMouse(capture_override_mouse == 1); + if (ImGui::IsItemHovered() && capture_override_keyboard != -1) + ImGui::SetNextFrameWantCaptureKeyboard(capture_override_keyboard == 1); - ImGui::Text("NavInputs down:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputs[i] > 0.0f) { ImGui::SameLine(); ImGui::Text("[%d] %.2f (%.02f secs)", i, io.NavInputs[i], io.NavInputsDownDuration[i]); } - ImGui::Text("NavInputs pressed:"); for (int i = 0; i < IM_ARRAYSIZE(io.NavInputs); i++) if (io.NavInputsDownDuration[i] == 0.0f) { ImGui::SameLine(); ImGui::Text("[%d]", i); } + ImGui::TreePop(); + } + ImGui::TreePop(); + } - ImGui::Button("Hovering me sets the\nkeyboard capture flag"); - if (ImGui::IsItemHovered()) - ImGui::CaptureKeyboardFromApp(true); - ImGui::SameLine(); - ImGui::Button("Holding me clears the\nthe keyboard capture flag"); - if (ImGui::IsItemActive()) - ImGui::CaptureKeyboardFromApp(false); + // Display mouse cursors + IMGUI_DEMO_MARKER("Inputs & Focus/Mouse Cursors"); + if (ImGui::TreeNode("Mouse Cursors")) + { + const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" }; + IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); + + ImGuiMouseCursor current = ImGui::GetMouseCursor(); + ImGui::Text("Current mouse cursor = %d: %s", current, mouse_cursors_names[current]); + ImGui::BeginDisabled(true); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &io.BackendFlags, ImGuiBackendFlags_HasMouseCursors); + ImGui::EndDisabled(); + + ImGui::Text("Hover to see mouse cursors:"); + ImGui::SameLine(); HelpMarker( + "Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. " + "If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, " + "otherwise your backend needs to handle it."); + for (int i = 0; i < ImGuiMouseCursor_COUNT; i++) + { + char label[32]; + sprintf(label, "Mouse cursor %d: %s", i, mouse_cursors_names[i]); + ImGui::Bullet(); ImGui::Selectable(label, false); + if (ImGui::IsItemHovered()) + ImGui::SetMouseCursor(i); + } ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Tabbing"); + IMGUI_DEMO_MARKER("Inputs & Focus/Tabbing"); if (ImGui::TreeNode("Tabbing")) { ImGui::Text("Use TAB/SHIFT+TAB to cycle through keyboard editable fields."); @@ -5730,7 +5820,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Focus from code"); + IMGUI_DEMO_MARKER("Inputs & Focus/Focus from code"); if (ImGui::TreeNode("Focus from code")) { bool focus_1 = ImGui::Button("Focus on 1"); ImGui::SameLine(); @@ -5772,7 +5862,7 @@ static void ShowDemoWindowMisc() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Dragging"); + IMGUI_DEMO_MARKER("Inputs & Focus/Dragging"); if (ImGui::TreeNode("Dragging")) { ImGui::TextWrapped("You can use ImGui::GetMouseDragDelta(0) to query for the dragged amount on any widget."); @@ -5800,30 +5890,6 @@ static void ShowDemoWindowMisc() ImGui::Text("io.MouseDelta: (%.1f, %.1f)", mouse_delta.x, mouse_delta.y); ImGui::TreePop(); } - - IMGUI_DEMO_MARKER("Inputs, Navigation & Focus/Mouse cursors"); - if (ImGui::TreeNode("Mouse cursors")) - { - const char* mouse_cursors_names[] = { "Arrow", "TextInput", "ResizeAll", "ResizeNS", "ResizeEW", "ResizeNESW", "ResizeNWSE", "Hand", "NotAllowed" }; - IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); - - ImGuiMouseCursor current = ImGui::GetMouseCursor(); - ImGui::Text("Current mouse cursor = %d: %s", current, mouse_cursors_names[current]); - ImGui::Text("Hover to see mouse cursors:"); - ImGui::SameLine(); HelpMarker( - "Your application can render a different mouse cursor based on what ImGui::GetMouseCursor() returns. " - "If software cursor rendering (io.MouseDrawCursor) is set ImGui will draw the right cursor for you, " - "otherwise your backend needs to handle it."); - for (int i = 0; i < ImGuiMouseCursor_COUNT; i++) - { - char label[32]; - sprintf(label, "Mouse cursor %d: %s", i, mouse_cursors_names[i]); - ImGui::Bullet(); ImGui::Selectable(label, false); - if (ImGui::IsItemHovered()) - ImGui::SetMouseCursor(i); - } - ImGui::TreePop(); - } } } @@ -5868,6 +5934,9 @@ void ImGui::ShowAboutWindow(bool* p_open) #ifdef IMGUI_DISABLE_OBSOLETE_FUNCTIONS ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS"); #endif +#ifdef IMGUI_DISABLE_OBSOLETE_KEYIO + ImGui::Text("define: IMGUI_DISABLE_OBSOLETE_KEYIO"); +#endif #ifdef IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS ImGui::Text("define: IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS"); #endif @@ -5981,7 +6050,7 @@ void ImGui::ShowAboutWindow(bool* p_open) namespace ImGui { IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); } // Demo helper function to select among loaded fonts. -// Here we use the regular BeginCombo()/EndCombo() api which is more the more flexible one. +// Here we use the regular BeginCombo()/EndCombo() api which is the more flexible one. void ImGui::ShowFontSelector(const char* label) { ImGuiIO& io = ImGui::GetIO(); @@ -6273,6 +6342,40 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::PopItemWidth(); } +//----------------------------------------------------------------------------- +// [SECTION] User Guide / ShowUserGuide() +//----------------------------------------------------------------------------- + +void ImGui::ShowUserGuide() +{ + ImGuiIO& io = ImGui::GetIO(); + ImGui::BulletText("Double-click on title bar to collapse window."); + ImGui::BulletText( + "Click and drag on lower corner to resize window\n" + "(double-click to auto fit window to its contents)."); + ImGui::BulletText("CTRL+Click on a slider or drag box to input value as text."); + ImGui::BulletText("TAB/SHIFT+TAB to cycle through keyboard editable fields."); + ImGui::BulletText("CTRL+Tab to select a window."); + if (io.FontAllowUserScaling) + ImGui::BulletText("CTRL+Mouse Wheel to zoom window contents."); + ImGui::BulletText("While inputing text:\n"); + ImGui::Indent(); + ImGui::BulletText("CTRL+Left/Right to word jump."); + ImGui::BulletText("CTRL+A or double-click to select all."); + ImGui::BulletText("CTRL+X/C/V to use clipboard cut/copy/paste."); + ImGui::BulletText("CTRL+Z,CTRL+Y to undo/redo."); + ImGui::BulletText("ESCAPE to revert."); + ImGui::Unindent(); + ImGui::BulletText("With keyboard navigation enabled:"); + ImGui::Indent(); + ImGui::BulletText("Arrow keys to navigate."); + ImGui::BulletText("Space to activate a widget."); + ImGui::BulletText("Return to input text into a widget."); + ImGui::BulletText("Escape to deactivate a widget, close popup, exit child window."); + ImGui::BulletText("Alt to jump to the menu layer of a window."); + ImGui::Unindent(); +} + //----------------------------------------------------------------------------- // [SECTION] Example App: Main Menu Bar / ShowExampleAppMainMenuBar() //----------------------------------------------------------------------------- @@ -6507,72 +6610,76 @@ struct ExampleAppConsole // Reserve enough left-over height for 1 separator + 1 input text const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); - ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar); - if (ImGui::BeginPopupContextWindow()) + if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar)) { - if (ImGui::Selectable("Clear")) ClearLog(); - ImGui::EndPopup(); - } + if (ImGui::BeginPopupContextWindow()) + { + if (ImGui::Selectable("Clear")) ClearLog(); + ImGui::EndPopup(); + } - // Display every line as a separate entry so we can change their color or add custom widgets. - // If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end()); - // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping - // to only process visible items. The clipper will automatically measure the height of your first item and then - // "seek" to display only items in the visible area. - // To use the clipper we can replace your standard loop: - // for (int i = 0; i < Items.Size; i++) - // With: - // ImGuiListClipper clipper; - // clipper.Begin(Items.Size); - // while (clipper.Step()) - // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) - // - That your items are evenly spaced (same height) - // - That you have cheap random access to your elements (you can access them given their index, - // without processing all the ones before) - // You cannot this code as-is if a filter is active because it breaks the 'cheap random-access' property. - // We would need random-access on the post-filtered list. - // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices - // or offsets of items that passed the filtering test, recomputing this array when user changes the filter, - // and appending newly elements as they are inserted. This is left as a task to the user until we can manage - // to improve this example code! - // If your items are of variable height: - // - Split them into same height items would be simpler and facilitate random-seeking into your list. - // - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items. - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing - if (copy_to_clipboard) - ImGui::LogToClipboard(); - for (int i = 0; i < Items.Size; i++) - { - const char* item = Items[i]; - if (!Filter.PassFilter(item)) - continue; + // Display every line as a separate entry so we can change their color or add custom widgets. + // If you only want raw text you can use ImGui::TextUnformatted(log.begin(), log.end()); + // NB- if you have thousands of entries this approach may be too inefficient and may require user-side clipping + // to only process visible items. The clipper will automatically measure the height of your first item and then + // "seek" to display only items in the visible area. + // To use the clipper we can replace your standard loop: + // for (int i = 0; i < Items.Size; i++) + // With: + // ImGuiListClipper clipper; + // clipper.Begin(Items.Size); + // while (clipper.Step()) + // for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) + // - That your items are evenly spaced (same height) + // - That you have cheap random access to your elements (you can access them given their index, + // without processing all the ones before) + // You cannot this code as-is if a filter is active because it breaks the 'cheap random-access' property. + // We would need random-access on the post-filtered list. + // A typical application wanting coarse clipping and filtering may want to pre-compute an array of indices + // or offsets of items that passed the filtering test, recomputing this array when user changes the filter, + // and appending newly elements as they are inserted. This is left as a task to the user until we can manage + // to improve this example code! + // If your items are of variable height: + // - Split them into same height items would be simpler and facilitate random-seeking into your list. + // - Consider using manual call to IsRectVisible() and skipping extraneous decoration from your items. + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing + if (copy_to_clipboard) + ImGui::LogToClipboard(); + for (int i = 0; i < Items.Size; i++) + { + const char* item = Items[i]; + if (!Filter.PassFilter(item)) + continue; - // Normally you would store more information in your item than just a string. - // (e.g. make Items[] an array of structure, store color/type etc.) - ImVec4 color; - bool has_color = false; - if (strstr(item, "[error]")) { color = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); has_color = true; } - else if (strncmp(item, "# ", 2) == 0) { color = ImVec4(1.0f, 0.8f, 0.6f, 1.0f); has_color = true; } - if (has_color) - ImGui::PushStyleColor(ImGuiCol_Text, color); - ImGui::TextUnformatted(item); - if (has_color) - ImGui::PopStyleColor(); - } - if (copy_to_clipboard) - ImGui::LogFinish(); + // Normally you would store more information in your item than just a string. + // (e.g. make Items[] an array of structure, store color/type etc.) + ImVec4 color; + bool has_color = false; + if (strstr(item, "[error]")) { color = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); has_color = true; } + else if (strncmp(item, "# ", 2) == 0) { color = ImVec4(1.0f, 0.8f, 0.6f, 1.0f); has_color = true; } + if (has_color) + ImGui::PushStyleColor(ImGuiCol_Text, color); + ImGui::TextUnformatted(item); + if (has_color) + ImGui::PopStyleColor(); + } + if (copy_to_clipboard) + ImGui::LogFinish(); - if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) - ImGui::SetScrollHereY(1.0f); - ScrollToBottom = false; + // Keep up at the bottom of the scroll region if we were already at the bottom at the beginning of the frame. + // Using a scrollbar or mouse-wheel will take away from the bottom edge. + if (ScrollToBottom || (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())) + ImGui::SetScrollHereY(1.0f); + ScrollToBottom = false; - ImGui::PopStyleVar(); + ImGui::PopStyleVar(); + } ImGui::EndChild(); ImGui::Separator(); // Command-line bool reclaim_focus = false; - ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory; + ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_EscapeClearsAll | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory; if (ImGui::InputText("Input", InputBuf, IM_ARRAYSIZE(InputBuf), input_text_flags, &TextEditCallbackStub, (void*)this)) { char* s = InputBuf; @@ -6814,63 +6921,66 @@ struct ExampleAppLog Filter.Draw("Filter", -100.0f); ImGui::Separator(); - ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); - - if (clear) - Clear(); - if (copy) - ImGui::LogToClipboard(); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); - const char* buf = Buf.begin(); - const char* buf_end = Buf.end(); - if (Filter.IsActive()) + if (ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) { - // In this example we don't use the clipper when Filter is enabled. - // This is because we don't have a random access on the result on our filter. - // A real application processing logs with ten of thousands of entries may want to store the result of - // search/filter.. especially if the filtering function is not trivial (e.g. reg-exp). - for (int line_no = 0; line_no < LineOffsets.Size; line_no++) - { - const char* line_start = buf + LineOffsets[line_no]; - const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end; - if (Filter.PassFilter(line_start, line_end)) - ImGui::TextUnformatted(line_start, line_end); - } - } - else - { - // The simplest and easy way to display the entire buffer: - // ImGui::TextUnformatted(buf_begin, buf_end); - // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward - // to skip non-visible lines. Here we instead demonstrate using the clipper to only process lines that are - // within the visible area. - // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them - // on your side is recommended. Using ImGuiListClipper requires - // - A) random access into your data - // - B) items all being the same height, - // both of which we can handle since we an array pointing to the beginning of each line of text. - // When using the filter (in the block of code above) we don't have random access into the data to display - // anymore, which is why we don't use the clipper. Storing or skimming through the search result would make - // it possible (and would be recommended if you want to search through tens of thousands of entries). - ImGuiListClipper clipper; - clipper.Begin(LineOffsets.Size); - while (clipper.Step()) - { - for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) + if (clear) + Clear(); + if (copy) + ImGui::LogToClipboard(); + + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + const char* buf = Buf.begin(); + const char* buf_end = Buf.end(); + if (Filter.IsActive()) + { + // In this example we don't use the clipper when Filter is enabled. + // This is because we don't have random access to the result of our filter. + // A real application processing logs with ten of thousands of entries may want to store the result of + // search/filter.. especially if the filtering function is not trivial (e.g. reg-exp). + for (int line_no = 0; line_no < LineOffsets.Size; line_no++) { const char* line_start = buf + LineOffsets[line_no]; const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end; - ImGui::TextUnformatted(line_start, line_end); + if (Filter.PassFilter(line_start, line_end)) + ImGui::TextUnformatted(line_start, line_end); } } - clipper.End(); - } - ImGui::PopStyleVar(); - - if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) - ImGui::SetScrollHereY(1.0f); + else + { + // The simplest and easy way to display the entire buffer: + // ImGui::TextUnformatted(buf_begin, buf_end); + // And it'll just work. TextUnformatted() has specialization for large blob of text and will fast-forward + // to skip non-visible lines. Here we instead demonstrate using the clipper to only process lines that are + // within the visible area. + // If you have tens of thousands of items and their processing cost is non-negligible, coarse clipping them + // on your side is recommended. Using ImGuiListClipper requires + // - A) random access into your data + // - B) items all being the same height, + // both of which we can handle since we have an array pointing to the beginning of each line of text. + // When using the filter (in the block of code above) we don't have random access into the data to display + // anymore, which is why we don't use the clipper. Storing or skimming through the search result would make + // it possible (and would be recommended if you want to search through tens of thousands of entries). + ImGuiListClipper clipper; + clipper.Begin(LineOffsets.Size); + while (clipper.Step()) + { + for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) + { + const char* line_start = buf + LineOffsets[line_no]; + const char* line_end = (line_no + 1 < LineOffsets.Size) ? (buf + LineOffsets[line_no + 1] - 1) : buf_end; + ImGui::TextUnformatted(line_start, line_end); + } + } + clipper.End(); + } + ImGui::PopStyleVar(); + // Keep up at the bottom of the scroll region if we were already at the bottom at the beginning of the frame. + // Using a scrollbar or mouse-wheel will take away from the bottom edge. + if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); + } ImGui::EndChild(); ImGui::End(); } @@ -6922,7 +7032,7 @@ static void ShowExampleAppLayout(bool* p_open) { if (ImGui::BeginMenu("File")) { - if (ImGui::MenuItem("Close")) *p_open = false; + if (ImGui::MenuItem("Close", "Ctrl+W")) { *p_open = false; } ImGui::EndMenu(); } ImGui::EndMenuBar(); @@ -7150,51 +7260,82 @@ static void ShowExampleAppAutoResize(bool* p_open) //----------------------------------------------------------------------------- // Demonstrate creating a window with custom resize constraints. +// Note that size constraints currently don't work on a docked window (when in 'docking' branch) static void ShowExampleAppConstrainedResize(bool* p_open) { struct CustomConstraints { // Helper functions to demonstrate programmatic constraints - static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y); } - static void Step(ImGuiSizeCallbackData* data) { float step = (float)(int)(intptr_t)data->UserData; data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step); } + // FIXME: This doesn't take account of decoration size (e.g. title bar), library should make this easier. + static void AspectRatio(ImGuiSizeCallbackData* data) { float aspect_ratio = *(float*)data->UserData; data->DesiredSize.x = IM_MAX(data->CurrentSize.x, data->CurrentSize.y); data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio); } + static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->CurrentSize.x, data->CurrentSize.y); } + static void Step(ImGuiSizeCallbackData* data) { float step = *(float*)data->UserData; data->DesiredSize = ImVec2((int)(data->CurrentSize.x / step + 0.5f) * step, (int)(data->CurrentSize.y / step + 0.5f) * step); } }; const char* test_desc[] = { + "Between 100x100 and 500x500", + "At least 100x100", "Resize vertical only", "Resize horizontal only", - "Width > 100, Height > 100", - "Width 400-500", - "Height 400-500", + "Width Between 400 and 500", + "Custom: Aspect Ratio 16:9", "Custom: Always Square", "Custom: Fixed Steps (100)", }; + // Options static bool auto_resize = false; - static int type = 0; + static bool window_padding = true; + static int type = 5; // Aspect Ratio static int display_lines = 10; - if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Vertical only - if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Horizontal only - if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); // Width > 100, Height > 100 - if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width 400-500 - if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 400), ImVec2(-1, 500)); // Height 400-500 - if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square - if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)(intptr_t)100); // Fixed Step - ImGuiWindowFlags flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0; - if (ImGui::Begin("Example: Constrained Resize", p_open, flags)) + // Submit constraint + float aspect_ratio = 16.0f / 9.0f; + float fixed_step = 100.0f; + if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(500, 500)); // Between 100x100 and 500x500 + if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); // Width > 100, Height > 100 + if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Vertical only + if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Horizontal only + if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width Between and 400 and 500 + if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio); // Aspect ratio + if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square + if (type == 7) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)&fixed_step); // Fixed Step + + // Submit window + if (!window_padding) + ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f)); + const ImGuiWindowFlags window_flags = auto_resize ? ImGuiWindowFlags_AlwaysAutoResize : 0; + const bool window_open = ImGui::Begin("Example: Constrained Resize", p_open, window_flags); + if (!window_padding) + ImGui::PopStyleVar(); + if (window_open) { IMGUI_DEMO_MARKER("Examples/Constrained Resizing window"); - if (ImGui::Button("200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine(); - if (ImGui::Button("500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine(); - if (ImGui::Button("800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); } - ImGui::SetNextItemWidth(200); - ImGui::Combo("Constraint", &type, test_desc, IM_ARRAYSIZE(test_desc)); - ImGui::SetNextItemWidth(200); - ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100); - ImGui::Checkbox("Auto-resize", &auto_resize); - for (int i = 0; i < display_lines; i++) - ImGui::Text("%*sHello, sailor! Making this line long enough for the example.", i * 4, ""); + if (ImGui::GetIO().KeyShift) + { + // Display a dummy viewport (in your real app you would likely use ImageButton() to display a texture. + ImVec2 avail_size = ImGui::GetContentRegionAvail(); + ImVec2 pos = ImGui::GetCursorScreenPos(); + ImGui::ColorButton("viewport", ImVec4(0.5f, 0.2f, 0.5f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, avail_size); + ImGui::SetCursorScreenPos(ImVec2(pos.x + 10, pos.y + 10)); + ImGui::Text("%.2f x %.2f", avail_size.x, avail_size.y); + } + else + { + ImGui::Text("(Hold SHIFT to display a dummy viewport)"); + if (ImGui::Button("Set 200x200")) { ImGui::SetWindowSize(ImVec2(200, 200)); } ImGui::SameLine(); + if (ImGui::Button("Set 500x500")) { ImGui::SetWindowSize(ImVec2(500, 500)); } ImGui::SameLine(); + if (ImGui::Button("Set 800x200")) { ImGui::SetWindowSize(ImVec2(800, 200)); } + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 20); + ImGui::Combo("Constraint", &type, test_desc, IM_ARRAYSIZE(test_desc)); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 20); + ImGui::DragInt("Lines", &display_lines, 0.2f, 1, 100); + ImGui::Checkbox("Auto-resize", &auto_resize); + ImGui::Checkbox("Window padding", &window_padding); + for (int i = 0; i < display_lines; i++) + ImGui::Text("%*sHello, sailor! Making this line long enough for the example.", i * 4, ""); + } } ImGui::End(); } @@ -7207,28 +7348,34 @@ static void ShowExampleAppConstrainedResize(bool* p_open) // + a context-menu to choose which corner of the screen to use. static void ShowExampleAppSimpleOverlay(bool* p_open) { - static int corner = 0; + static int location = 0; ImGuiIO& io = ImGui::GetIO(); ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav; - if (corner != -1) + if (location >= 0) { const float PAD = 10.0f; const ImGuiViewport* viewport = ImGui::GetMainViewport(); ImVec2 work_pos = viewport->WorkPos; // Use work area to avoid menu-bar/task-bar, if any! ImVec2 work_size = viewport->WorkSize; ImVec2 window_pos, window_pos_pivot; - window_pos.x = (corner & 1) ? (work_pos.x + work_size.x - PAD) : (work_pos.x + PAD); - window_pos.y = (corner & 2) ? (work_pos.y + work_size.y - PAD) : (work_pos.y + PAD); - window_pos_pivot.x = (corner & 1) ? 1.0f : 0.0f; - window_pos_pivot.y = (corner & 2) ? 1.0f : 0.0f; + window_pos.x = (location & 1) ? (work_pos.x + work_size.x - PAD) : (work_pos.x + PAD); + window_pos.y = (location & 2) ? (work_pos.y + work_size.y - PAD) : (work_pos.y + PAD); + window_pos_pivot.x = (location & 1) ? 1.0f : 0.0f; + window_pos_pivot.y = (location & 2) ? 1.0f : 0.0f; ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); window_flags |= ImGuiWindowFlags_NoMove; } + else if (location == -2) + { + // Center window + ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); + window_flags |= ImGuiWindowFlags_NoMove; + } ImGui::SetNextWindowBgAlpha(0.35f); // Transparent background if (ImGui::Begin("Example: Simple overlay", p_open, window_flags)) { IMGUI_DEMO_MARKER("Examples/Simple Overlay"); - ImGui::Text("Simple overlay\n" "in the corner of the screen.\n" "(right-click to change position)"); + ImGui::Text("Simple overlay\n" "(right-click to change position)"); ImGui::Separator(); if (ImGui::IsMousePosValid()) ImGui::Text("Mouse Position: (%.1f,%.1f)", io.MousePos.x, io.MousePos.y); @@ -7236,11 +7383,12 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) ImGui::Text("Mouse Position: "); if (ImGui::BeginPopupContextWindow()) { - if (ImGui::MenuItem("Custom", NULL, corner == -1)) corner = -1; - if (ImGui::MenuItem("Top-left", NULL, corner == 0)) corner = 0; - if (ImGui::MenuItem("Top-right", NULL, corner == 1)) corner = 1; - if (ImGui::MenuItem("Bottom-left", NULL, corner == 2)) corner = 2; - if (ImGui::MenuItem("Bottom-right", NULL, corner == 3)) corner = 3; + if (ImGui::MenuItem("Custom", NULL, location == -1)) location = -1; + if (ImGui::MenuItem("Center", NULL, location == -2)) location = -2; + if (ImGui::MenuItem("Top-left", NULL, location == 0)) location = 0; + if (ImGui::MenuItem("Top-right", NULL, location == 1)) location = 1; + if (ImGui::MenuItem("Bottom-left", NULL, location == 2)) location = 2; + if (ImGui::MenuItem("Bottom-right", NULL, location == 3)) location = 3; if (p_open && ImGui::MenuItem("Close")) *p_open = false; ImGui::EndPopup(); } @@ -7256,7 +7404,7 @@ static void ShowExampleAppSimpleOverlay(bool* p_open) static void ShowExampleAppFullscreen(bool* p_open) { static bool use_work_area = true; - static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; + static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings; // We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.) // Based on your use case you may want one of the other. @@ -7288,8 +7436,8 @@ static void ShowExampleAppFullscreen(bool* p_open) // [SECTION] Example App: Manipulating Window Titles / ShowExampleAppWindowTitles() //----------------------------------------------------------------------------- -// Demonstrate using "##" and "###" in identifiers to manipulate ID generation. -// This apply to all regular items as well. +// Demonstrate the use of "##" and "###" in identifiers to manipulate ID generation. +// This applies to all regular items as well. // Read FAQ section "How can I have multiple widgets with the same label?" for details. static void ShowExampleAppWindowTitles(bool*) { @@ -7509,8 +7657,8 @@ static void ShowExampleAppCustomRendering(bool* p_open) // Context menu (under default mouse threshold) ImVec2 drag_delta = ImGui::GetMouseDragDelta(ImGuiMouseButton_Right); - if (opt_enable_context_menu && ImGui::IsMouseReleased(ImGuiMouseButton_Right) && drag_delta.x == 0.0f && drag_delta.y == 0.0f) - ImGui::OpenPopupOnItemClick("context"); + if (opt_enable_context_menu && drag_delta.x == 0.0f && drag_delta.y == 0.0f) + ImGui::OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); if (ImGui::BeginPopup("context")) { if (adding_line) @@ -7694,7 +7842,8 @@ void ShowExampleAppDocuments(bool* p_open) if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0)) for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) app.Documents[doc_n].DoQueueClose(); - if (ImGui::MenuItem("Exit", "Alt+F4")) {} + if (ImGui::MenuItem("Exit", "Ctrl+F4") && p_open) + *p_open = false; ImGui::EndMenu(); } ImGui::EndMenuBar(); diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index bf1da15b..2d423cd8 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (drawing and font code) /* @@ -39,25 +39,12 @@ Index of this file: #endif #include // vsnprintf, sscanf, printf -#if !defined(alloca) -#if defined(__GLIBC__) || defined(__sun) || defined(__APPLE__) || defined(__NEWLIB__) -#include // alloca (glibc uses . Note that Cygwin may have _WIN32 defined, so the order matters here) -#elif defined(_WIN32) -#include // alloca -#if !defined(alloca) -#define alloca _alloca // for clang with MS Codegen -#endif -#else -#include // alloca -#endif -#endif // Visual Studio warnings #ifdef _MSC_VER #pragma warning (disable: 4127) // condition expression is constant #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff) #pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen -#pragma warning (disable: 6255) // [Static Analyzer] _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead. #pragma warning (disable: 26451) // [Static Analyzer] Arithmetic overflow : Using operator 'xxx' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator 'xxx' to avoid overflow(io.2). #pragma warning (disable: 26812) // [Static Analyzer] The enum type 'xxx' is unscoped. Prefer 'enum class' over 'enum' (Enum.3). [MSVC Static Analyzer) #endif @@ -67,9 +54,6 @@ Index of this file: #if __has_warning("-Wunknown-warning-option") #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' // not all warnings are known by all Clang versions and they tend to be rename-happy.. so ignoring warnings triggers new warnings on some configuration. Great! #endif -#if __has_warning("-Walloca") -#pragma clang diagnostic ignored "-Walloca" // warning: use of function '__builtin_alloca' is discouraged -#endif #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok. @@ -90,7 +74,7 @@ Index of this file: #endif //------------------------------------------------------------------------- -// [SECTION] STB libraries implementation +// [SECTION] STB libraries implementation (for stb_truetype and stb_rect_pack) //------------------------------------------------------------------------- // Compile time options: @@ -393,7 +377,7 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) for (int i = 0; i < IM_ARRAYSIZE(CircleSegmentCounts); i++) { const float radius = (float)i; - CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : 0); + CircleSegmentCounts[i] = (ImU8)((i > 0) ? IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, CircleSegmentMaxError) : IM_DRAWLIST_ARCFAST_SAMPLE_MAX); } ArcFastRadiusCutoff = IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC_R(IM_DRAWLIST_ARCFAST_SAMPLE_MAX, CircleSegmentMaxError); } @@ -402,10 +386,9 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) void ImDrawList::_ResetForNewFrame() { // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. - // (those should be IM_STATIC_ASSERT() in theory but with our pre C++11 setup the whole check doesn't compile with GCC) - IM_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); - IM_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); - IM_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); + IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); + IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); + IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); CmdBuffer.resize(0); IdxBuffer.resize(0); @@ -464,11 +447,13 @@ void ImDrawList::AddDrawCmd() // Note that this leaves the ImDrawList in a state unfit for further commands, as most code assume that CmdBuffer.Size > 0 && CmdBuffer.back().UserCallback == NULL void ImDrawList::_PopUnusedDrawCmd() { - if (CmdBuffer.Size == 0) - return; - ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; - if (curr_cmd->ElemCount == 0 && curr_cmd->UserCallback == NULL) + while (CmdBuffer.Size > 0) + { + ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; + if (curr_cmd->ElemCount != 0 || curr_cmd->UserCallback != NULL) + return;// break; CmdBuffer.pop_back(); + } } void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) @@ -488,9 +473,10 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) } // Compare ClipRect, TextureId and VtxOffset with a single memcmp() -#define ImDrawCmd_HeaderSize (IM_OFFSETOF(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) -#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset -#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset +#define ImDrawCmd_HeaderSize (IM_OFFSETOF(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) +#define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset +#define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset +#define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset) // Try to merge two last draw commands void ImDrawList::_TryMergeDrawCmds() @@ -498,7 +484,7 @@ void ImDrawList::_TryMergeDrawCmds() IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; ImDrawCmd* prev_cmd = curr_cmd - 1; - if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) + if (ImDrawCmd_HeaderCompare(curr_cmd, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && curr_cmd->UserCallback == NULL && prev_cmd->UserCallback == NULL) { prev_cmd->ElemCount += curr_cmd->ElemCount; CmdBuffer.pop_back(); @@ -521,7 +507,7 @@ void ImDrawList::_OnChangedClipRect() // Try to merge with previous command if it matches, else use current command ImDrawCmd* prev_cmd = curr_cmd - 1; - if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && prev_cmd->UserCallback == NULL) + if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) { CmdBuffer.pop_back(); return; @@ -544,7 +530,7 @@ void ImDrawList::_OnChangedTextureID() // Try to merge with previous command if it matches, else use current command ImDrawCmd* prev_cmd = curr_cmd - 1; - if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && prev_cmd->UserCallback == NULL) + if (curr_cmd->ElemCount == 0 && CmdBuffer.Size > 1 && ImDrawCmd_HeaderCompare(&_CmdHeader, prev_cmd) == 0 && ImDrawCmd_AreSequentialIdxOffset(prev_cmd, curr_cmd) && prev_cmd->UserCallback == NULL) { CmdBuffer.pop_back(); return; @@ -580,7 +566,7 @@ int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const } // Render-level scissoring. This is passed down to your render function but not used for CPU-side coarse clipping. Prefer using higher-level ImGui::PushClipRect() to affect logic (hit-testing and widget culling) -void ImDrawList::PushClipRect(ImVec2 cr_min, ImVec2 cr_max, bool intersect_with_current_clip_rect) +void ImDrawList::PushClipRect(const ImVec2& cr_min, const ImVec2& cr_max, bool intersect_with_current_clip_rect) { ImVec4 cr(cr_min.x, cr_min.y, cr_max.x, cr_max.y); if (intersect_with_current_clip_rect) @@ -753,7 +739,8 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 // Temporary buffer // The first items are normals at each line point, then after that there are either 2 or 4 temp points for each line point - ImVec2* temp_normals = (ImVec2*)alloca(points_count * ((use_texture || !thick_line) ? 3 : 5) * sizeof(ImVec2)); //-V630 + _Data->TempBuffer.reserve_discard(points_count * ((use_texture || !thick_line) ? 3 : 5)); + ImVec2* temp_normals = _Data->TempBuffer.Data; ImVec2* temp_points = temp_normals + points_count; // Calculate normals (tangents) for each line segment @@ -973,7 +960,8 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 } } -// We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. +// - We intentionally avoid using ImVec2 and its math operators here to reduce cost to a minimum for debug/non-inlined builds. +// - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) { if (points_count < 3) @@ -1000,7 +988,8 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun } // Compute normals - ImVec2* temp_normals = (ImVec2*)alloca(points_count * sizeof(ImVec2)); //-V630 + _Data->TempBuffer.reserve_discard(points_count); + ImVec2* temp_normals = _Data->TempBuffer.Data; for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) { const ImVec2& p0 = points[i0]; @@ -1057,7 +1046,7 @@ void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_coun void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step) { - if (radius <= 0.0f) + if (radius < 0.5f) { _Path.push_back(center); return; @@ -1149,7 +1138,7 @@ void ImDrawList::_PathArcToFastEx(const ImVec2& center, float radius, int a_min_ void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) { - if (radius <= 0.0f) + if (radius < 0.5f) { _Path.push_back(center); return; @@ -1168,7 +1157,7 @@ void ImDrawList::_PathArcToN(const ImVec2& center, float radius, float a_min, fl // 0: East, 3: South, 6: West, 9: North, 12: East void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12) { - if (radius <= 0.0f) + if (radius < 0.5f) { _Path.push_back(center); return; @@ -1178,7 +1167,7 @@ void ImDrawList::PathArcToFast(const ImVec2& center, float radius, int a_min_of_ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments) { - if (radius <= 0.0f) + if (radius < 0.5f) { _Path.push_back(center); return; @@ -1206,8 +1195,8 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; const float a_max_segment_angle = a_max_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; - const bool a_emit_start = (a_min_segment_angle - a_min) != 0.0f; - const bool a_emit_end = (a_max - a_max_segment_angle) != 0.0f; + const bool a_emit_start = ImAbs(a_min_segment_angle - a_min) >= 1e-5f; + const bool a_emit_end = ImAbs(a_max - a_max_segment_angle) >= 1e-5f; _Path.reserve(_Path.Size + (a_mid_samples + 1 + (a_emit_start ? 1 : 0) + (a_emit_end ? 1 : 0))); if (a_emit_start) @@ -1294,6 +1283,7 @@ void ImDrawList::PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, cons ImVec2 p1 = _Path.back(); if (num_segments == 0) { + IM_ASSERT(_Data->CurveTessellationTol > 0.0f); PathBezierCubicCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, p4.x, p4.y, _Data->CurveTessellationTol, 0); // Auto-tessellated } else @@ -1309,6 +1299,7 @@ void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, ImVec2 p1 = _Path.back(); if (num_segments == 0) { + IM_ASSERT(_Data->CurveTessellationTol > 0.0f); PathBezierQuadraticCurveToCasteljau(&_Path, p1.x, p1.y, p2.x, p2.y, p3.x, p3.y, _Data->CurveTessellationTol, 0);// Auto-tessellated } else @@ -1323,6 +1314,7 @@ IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) { #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // Obsoleted in 1.82 (from February 2021) // Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) // ~0 --> ImDrawFlags_RoundCornersAll or 0 if (flags == ~0) @@ -1359,7 +1351,7 @@ void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDr rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f ) - 1.0f); rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f ) - 1.0f); - if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) + if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { PathLineTo(a); PathLineTo(ImVec2(b.x, a.y)); @@ -1405,7 +1397,7 @@ void ImDrawList::AddRectFilled(const ImVec2& p_min, const ImVec2& p_max, ImU32 c { if ((col & IM_COL32_A_MASK) == 0) return; - if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) + if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { PrimReserve(6, 4); PrimRect(p_min, p_max, col); @@ -1481,7 +1473,7 @@ void ImDrawList::AddTriangleFilled(const ImVec2& p1, const ImVec2& p2, const ImV void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness) { - if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) + if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) return; if (num_segments <= 0) @@ -1505,7 +1497,7 @@ void ImDrawList::AddCircle(const ImVec2& center, float radius, ImU32 col, int nu void ImDrawList::AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments) { - if ((col & IM_COL32_A_MASK) == 0 || radius <= 0.0f) + if ((col & IM_COL32_A_MASK) == 0 || radius < 0.5f) return; if (num_segments <= 0) @@ -1645,7 +1637,7 @@ void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_mi return; flags = FixRectCornerFlags(flags); - if (rounding <= 0.0f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) + if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { AddImage(user_texture_id, p_min, p_max, uv_min, uv_max, col); return; @@ -1733,13 +1725,13 @@ void ImDrawListSplitter::Merge(ImDrawList* draw_list) for (int i = 1; i < _Count; i++) { ImDrawChannel& ch = _Channels[i]; - - // Equivalent of PopUnusedDrawCmd() for this channel's cmdbuffer and except we don't need to test for UserCallback. - if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0) + if (ch._CmdBuffer.Size > 0 && ch._CmdBuffer.back().ElemCount == 0 && ch._CmdBuffer.back().UserCallback == NULL) // Equivalent of PopUnusedDrawCmd() ch._CmdBuffer.pop_back(); if (ch._CmdBuffer.Size > 0 && last_cmd != NULL) { + // Do not include ImDrawCmd_AreSequentialIdxOffset() in the compare as we rebuild IdxOffset values ourselves. + // Manipulating IdxOffset (e.g. by reordering draw commands like done by RenderDimmedBackgroundBehindWindow()) is not supported within a splitter. ImDrawCmd* next_cmd = &ch._CmdBuffer[0]; if (ImDrawCmd_HeaderCompare(last_cmd, next_cmd) == 0 && last_cmd->UserCallback == NULL && next_cmd->UserCallback == NULL) { @@ -2297,10 +2289,11 @@ void ImFontAtlasBuildMultiplyCalcLookupTable(unsigned char out_table[256], fl void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table[256], unsigned char* pixels, int x, int y, int w, int h, int stride) { + IM_ASSERT_PARANOID(w <= stride); unsigned char* data = pixels + x + y * stride; - for (int j = h; j > 0; j--, data += stride) - for (int i = 0; i < w; i++) - data[i] = table[data[i]]; + for (int j = h; j > 0; j--, data += stride - w) + for (int i = w; i > 0; i--, data++) + *data = table[*data]; } #ifdef IMGUI_ENABLE_STB_TRUETYPE @@ -2317,7 +2310,7 @@ struct ImFontBuildSrcData int GlyphsHighest; // Highest requested codepoint int GlyphsCount; // Glyph count (excluding missing glyphs and glyphs already set by an earlier source font) ImBitVector GlyphsSet; // Glyph bit map (random access, 1-bit per codepoint. This will be a maximum of 8KB) - ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsMap) + ImVector GlyphsList; // Glyph codepoints list (flattened version of GlyphsSet) }; // Temporary data for one destination ImFont* (multiple source fonts can be merged into one destination ImFont) @@ -2637,8 +2630,8 @@ void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opa for (int i = 0; i < pack_rects.Size; i++) if (pack_rects[i].was_packed) { - user_rects[i].X = pack_rects[i].x; - user_rects[i].Y = pack_rects[i].y; + user_rects[i].X = (unsigned short)pack_rects[i].x; + user_rects[i].Y = (unsigned short)pack_rects[i].y; IM_ASSERT(pack_rects[i].w == user_rects[i].Width && pack_rects[i].h == user_rects[i].Height); atlas->TexHeight = ImMax(atlas->TexHeight, pack_rects[i].y + pack_rects[i].h); } @@ -2738,13 +2731,13 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) { unsigned int* write_ptr = &atlas->TexPixelsRGBA32[r->X + ((r->Y + y) * atlas->TexWidth)]; for (unsigned int i = 0; i < pad_left; i++) - *(write_ptr + i) = IM_COL32_BLACK_TRANS; + *(write_ptr + i) = IM_COL32(255, 255, 255, 0); for (unsigned int i = 0; i < line_width; i++) *(write_ptr + pad_left + i) = IM_COL32_WHITE; for (unsigned int i = 0; i < pad_right; i++) - *(write_ptr + pad_left + line_width + i) = IM_COL32_BLACK_TRANS; + *(write_ptr + pad_left + line_width + i) = IM_COL32(255, 255, 255, 0); } // Calculate UVs for this line @@ -2817,6 +2810,17 @@ const ImWchar* ImFontAtlas::GetGlyphRangesDefault() return &ranges[0]; } +const ImWchar* ImFontAtlas::GetGlyphRangesGreek() +{ + static const ImWchar ranges[] = + { + 0x0020, 0x00FF, // Basic Latin + Latin Supplement + 0x0370, 0x03FF, // Greek and Coptic + 0, + }; + return &ranges[0]; +} + const ImWchar* ImFontAtlas::GetGlyphRangesKorean() { static const ImWchar ranges[] = @@ -3329,11 +3333,21 @@ const ImFontGlyph* ImFont::FindGlyphNoFallback(ImWchar c) const return &Glyphs.Data[i]; } -const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const +// Wrapping skips upcoming blanks +static inline const char* CalcWordWrapNextLineStartA(const char* text, const char* text_end) { - // Simple word-wrapping for English, not full-featured. Please submit failing cases! - // FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.) + while (text < text_end && ImCharIsBlankA(*text)) + text++; + if (*text == '\n') + text++; + return text; +} +// Simple word-wrapping for English, not full-featured. Please submit failing cases! +// This will return the next location to wrap from. If no wrapping if necessary, this will fast-forward to e.g. text_end. +// FIXME: Much possible improvements (don't cut things like "word !", "word!!!" but cut within "word,,,,", more sensible support for punctuations, support for Unicode punctuations, etc.) +const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const +{ // For references, possible wrap point marked with ^ // "aaa bbb, ccc,ddd. eee fff. ggg!" // ^ ^ ^ ^ ^__ ^ ^ @@ -3345,7 +3359,6 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c // Cut words that cannot possibly fit within one line. // e.g.: "The tropical fish" with ~5 characters worth of width --> "The tr" "opical" "fish" - float line_width = 0.0f; float word_width = 0.0f; float blank_width = 0.0f; @@ -3425,6 +3438,10 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c s = next_s; } + // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. + // +1 may not be a character start point in UTF-8 but it's ok because caller loops use (text >= word_wrap_eol). + if (s == text && text < text_end) + return s + 1; return s; } @@ -3449,11 +3466,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons { // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature. if (!word_wrap_eol) - { word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - line_width); - if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. - word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below - } if (s >= word_wrap_eol) { @@ -3462,13 +3475,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons text_size.y += line_height; line_width = 0.0f; word_wrap_eol = NULL; - - // Wrapping skips upcoming blanks - while (s < text_end) - { - const char c = *s; - if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } - } + s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks continue; } } @@ -3523,7 +3530,7 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons } // Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. -void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, ImWchar c) const +void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, ImWchar c) const { const ImFontGlyph* glyph = FindGlyph(c); if (!glyph || !glyph->Visible) @@ -3531,38 +3538,48 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col if (glyph->Colored) col |= ~IM_COL32_A_MASK; float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; - pos.x = IM_FLOOR(pos.x); - pos.y = IM_FLOOR(pos.y); + float x = IM_FLOOR(pos.x); + float y = IM_FLOOR(pos.y); draw_list->PrimReserve(6, 4); - draw_list->PrimRectUV(ImVec2(pos.x + glyph->X0 * scale, pos.y + glyph->Y0 * scale), ImVec2(pos.x + glyph->X1 * scale, pos.y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); + draw_list->PrimRectUV(ImVec2(x + glyph->X0 * scale, y + glyph->Y0 * scale), ImVec2(x + glyph->X1 * scale, y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); } // Note: as with every ImDrawList drawing function, this expects that the font atlas texture is bound. -void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const +void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, float wrap_width, bool cpu_fine_clip) const { if (!text_end) text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. // Align to be pixel perfect - pos.x = IM_FLOOR(pos.x); - pos.y = IM_FLOOR(pos.y); - float x = pos.x; - float y = pos.y; + float x = IM_FLOOR(pos.x); + float y = IM_FLOOR(pos.y); if (y > clip_rect.w) return; + const float start_x = x; const float scale = size / FontSize; const float line_height = FontSize * scale; const bool word_wrap_enabled = (wrap_width > 0.0f); - const char* word_wrap_eol = NULL; // Fast-forward to first visible line const char* s = text_begin; - if (y + line_height < clip_rect.y && !word_wrap_enabled) + if (y + line_height < clip_rect.y) while (y + line_height < clip_rect.y && s < text_end) { - s = (const char*)memchr(s, '\n', text_end - s); - s = s ? s + 1 : text_end; + const char* line_end = (const char*)memchr(s, '\n', text_end - s); + const char* line_next = line_end ? line_end + 1 : text_end; + if (word_wrap_enabled) + { + // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA(). + // If the specs for CalcWordWrapPositionA() were reworked to optionally return on \n we could combine both. + // However it is still better than nothing performing the fast-forward! + s = CalcWordWrapPositionA(scale, s, line_next, wrap_width); + s = CalcWordWrapNextLineStartA(s, text_end); + } + else + { + s = line_next; + } y += line_height; } @@ -3594,6 +3611,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; const ImU32 col_untinted = col | ~IM_COL32_A_MASK; + const char* word_wrap_eol = NULL; while (s < text_end) { @@ -3601,24 +3619,14 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col { // Calculate how far we can render. Requires two passes on the string data but keeps the code simple and not intrusive for what's essentially an uncommon feature. if (!word_wrap_eol) - { - word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - pos.x)); - if (word_wrap_eol == s) // Wrap_width is too small to fit anything. Force displaying 1 character to minimize the height discontinuity. - word_wrap_eol++; // +1 may not be a character start point in UTF-8 but it's ok because we use s >= word_wrap_eol below - } + word_wrap_eol = CalcWordWrapPositionA(scale, s, text_end, wrap_width - (x - start_x)); if (s >= word_wrap_eol) { - x = pos.x; + x = start_x; y += line_height; word_wrap_eol = NULL; - - // Wrapping skips upcoming blanks - while (s < text_end) - { - const char c = *s; - if (ImCharIsBlankA(c)) { s++; } else if (c == '\n') { s++; break; } else { break; } - } + s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks continue; } } @@ -3640,7 +3648,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col { if (c == '\n') { - x = pos.x; + x = start_x; y += line_height; if (y > clip_rect.w) break; // break out of main loop @@ -3736,7 +3744,6 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col // - RenderArrow() // - RenderBullet() // - RenderCheckMark() -// - RenderMouseCursor() // - RenderArrowPointingAt() // - RenderRectFilledRangeH() // - RenderRectFilledWithHole() @@ -3797,27 +3804,6 @@ void ImGui::RenderCheckMark(ImDrawList* draw_list, ImVec2 pos, ImU32 col, float draw_list->PathStroke(col, 0, thickness); } -void ImGui::RenderMouseCursor(ImDrawList* draw_list, ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) -{ - if (mouse_cursor == ImGuiMouseCursor_None) - return; - IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); - - ImFontAtlas* font_atlas = draw_list->_Data->Font->ContainerAtlas; - ImVec2 offset, size, uv[4]; - if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) - { - pos -= offset; - ImTextureID tex_id = font_atlas->TexID; - draw_list->PushTextureID(tex_id); - draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border); - draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill); - draw_list->PopTextureID(); - } -} - // Render an arrow. 'pos' is position of the arrow tip. half_sz.x is length from base to tip. half_sz.y is length on each side. void ImGui::RenderArrowPointingAt(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, ImGuiDir direction, ImU32 col) { @@ -3900,7 +3886,7 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im draw_list->PathFillConvex(col); } -void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, ImRect outer, ImRect inner, ImU32 col, float rounding) +void ImGui::RenderRectFilledWithHole(ImDrawList* draw_list, const ImRect& outer, const ImRect& inner, ImU32 col, float rounding) { const bool fill_L = (inner.Min.x > outer.Min.x); const bool fill_R = (inner.Max.x < outer.Max.x); diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index 1be2c524..d0dedf64 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -5,9 +5,9 @@ // Implemented features: // [X] Platform: Clipboard support. +// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). -// [X] Platform: Keyboard arrays indexed using GLFW_KEY_* codes, e.g. ImGui::IsKeyPressed(GLFW_KEY_SPACE). // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. @@ -16,6 +16,22 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-01-04: Inputs: Fixed mods state on Linux when using Alt-GR text input (e.g. German keyboard layout), could lead to broken text input. Revert a 2022/01/17 change were we resumed using mods provided by GLFW, turns out they were faulty. +// 2022-11-22: Perform a dummy glfwGetError() read to cancel missing names with glfwGetKeyName(). (#5908) +// 2022-10-18: Perform a dummy glfwGetError() read to cancel missing mouse cursors errors. Using GLFW_VERSION_COMBINED directly. (#5785) +// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. +// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). +// 2022-09-01: Inputs: Honor GLFW_CURSOR_DISABLED by not setting mouse position. +// 2022-04-30: Inputs: Fixed ImGui_ImplGlfw_TranslateUntranslatedKey() for lower case letters on OSX. +// 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11. +// 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend. +// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. +// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. +// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). +// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates. +// 2022-01-12: *BREAKING CHANGE*: Now using glfwSetCursorPosCallback(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetCursorPosCallback() and forward it to the backend via ImGui_ImplGlfw_CursorPosCallback(). +// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. +// 2022-01-05: Inputs: Converting GLFW untranslated keycodes back to translated keycodes (in the ImGui_ImplGlfw_KeyCallback() function) in order to match the behavior of every other backend, and facilitate the use of GLFW with lettered-shortcuts API. // 2021-08-17: *BREAKING CHANGE*: Now using glfwSetWindowFocusCallback() to calling io.AddFocusEvent(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() and forward it to the backend via ImGui_ImplGlfw_WindowFocusCallback(). // 2021-07-29: *BREAKING CHANGE*: Now using glfwSetCursorEnterCallback(). MousePos is correctly reported when the host platform window is hovered but not focused. If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() callback and forward it to the backend via ImGui_ImplGlfw_CursorEnterCallback(). // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). @@ -44,6 +60,13 @@ #include "imgui.h" #include "imgui_impl_glfw.h" +// Clang warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#endif + // GLFW #include #ifdef _WIN32 @@ -51,16 +74,16 @@ #define GLFW_EXPOSE_NATIVE_WIN32 #include // for glfwGetWin32Window #endif -#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING -#define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED -#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity -#define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale -#define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface -#ifdef GLFW_RESIZE_NESW_CURSOR // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? -#define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR + +// We gather version tests as define in order to easily see which features are version-dependent. +#define GLFW_VERSION_COMBINED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 + GLFW_VERSION_REVISION) +#ifdef GLFW_RESIZE_NESW_CURSOR // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? +#define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_COMBINED >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR #else -#define GLFW_HAS_NEW_CURSORS (0) +#define GLFW_HAS_NEW_CURSORS (0) #endif +#define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api +#define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName() // GLFW data enum GlfwClientApi @@ -76,12 +99,13 @@ struct ImGui_ImplGlfw_Data GlfwClientApi ClientApi; double Time; GLFWwindow* MouseWindow; - bool MouseJustPressed[ImGuiMouseButton_COUNT]; GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; + ImVec2 LastValidMousePos; bool InstalledCallbacks; // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. GLFWwindowfocusfun PrevUserCallbackWindowFocus; + GLFWcursorposfun PrevUserCallbackCursorPos; GLFWcursorenterfun PrevUserCallbackCursorEnter; GLFWmousebuttonfun PrevUserCallbackMousebutton; GLFWscrollfun PrevUserCallbackScroll; @@ -89,7 +113,7 @@ struct ImGui_ImplGlfw_Data GLFWcharfun PrevUserCallbackChar; GLFWmonitorfun PrevUserCallbackMonitor; - ImGui_ImplGlfw_Data() { memset(this, 0, sizeof(*this)); } + ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); } }; // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts @@ -101,7 +125,7 @@ struct ImGui_ImplGlfw_Data // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData() { - return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : NULL; + return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; } // Functions @@ -115,79 +139,257 @@ static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) glfwSetClipboardString((GLFWwindow*)user_data, text); } +static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) +{ + switch (key) + { + case GLFW_KEY_TAB: return ImGuiKey_Tab; + case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; + case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow; + case GLFW_KEY_UP: return ImGuiKey_UpArrow; + case GLFW_KEY_DOWN: return ImGuiKey_DownArrow; + case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp; + case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown; + case GLFW_KEY_HOME: return ImGuiKey_Home; + case GLFW_KEY_END: return ImGuiKey_End; + case GLFW_KEY_INSERT: return ImGuiKey_Insert; + case GLFW_KEY_DELETE: return ImGuiKey_Delete; + case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace; + case GLFW_KEY_SPACE: return ImGuiKey_Space; + case GLFW_KEY_ENTER: return ImGuiKey_Enter; + case GLFW_KEY_ESCAPE: return ImGuiKey_Escape; + case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe; + case GLFW_KEY_COMMA: return ImGuiKey_Comma; + case GLFW_KEY_MINUS: return ImGuiKey_Minus; + case GLFW_KEY_PERIOD: return ImGuiKey_Period; + case GLFW_KEY_SLASH: return ImGuiKey_Slash; + case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon; + case GLFW_KEY_EQUAL: return ImGuiKey_Equal; + case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket; + case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash; + case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket; + case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent; + case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock; + case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock; + case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock; + case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen; + case GLFW_KEY_PAUSE: return ImGuiKey_Pause; + case GLFW_KEY_KP_0: return ImGuiKey_Keypad0; + case GLFW_KEY_KP_1: return ImGuiKey_Keypad1; + case GLFW_KEY_KP_2: return ImGuiKey_Keypad2; + case GLFW_KEY_KP_3: return ImGuiKey_Keypad3; + case GLFW_KEY_KP_4: return ImGuiKey_Keypad4; + case GLFW_KEY_KP_5: return ImGuiKey_Keypad5; + case GLFW_KEY_KP_6: return ImGuiKey_Keypad6; + case GLFW_KEY_KP_7: return ImGuiKey_Keypad7; + case GLFW_KEY_KP_8: return ImGuiKey_Keypad8; + case GLFW_KEY_KP_9: return ImGuiKey_Keypad9; + case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal; + case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide; + case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; + case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract; + case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd; + case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter; + case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual; + case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift; + case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl; + case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt; + case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper; + case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift; + case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl; + case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt; + case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper; + case GLFW_KEY_MENU: return ImGuiKey_Menu; + case GLFW_KEY_0: return ImGuiKey_0; + case GLFW_KEY_1: return ImGuiKey_1; + case GLFW_KEY_2: return ImGuiKey_2; + case GLFW_KEY_3: return ImGuiKey_3; + case GLFW_KEY_4: return ImGuiKey_4; + case GLFW_KEY_5: return ImGuiKey_5; + case GLFW_KEY_6: return ImGuiKey_6; + case GLFW_KEY_7: return ImGuiKey_7; + case GLFW_KEY_8: return ImGuiKey_8; + case GLFW_KEY_9: return ImGuiKey_9; + case GLFW_KEY_A: return ImGuiKey_A; + case GLFW_KEY_B: return ImGuiKey_B; + case GLFW_KEY_C: return ImGuiKey_C; + case GLFW_KEY_D: return ImGuiKey_D; + case GLFW_KEY_E: return ImGuiKey_E; + case GLFW_KEY_F: return ImGuiKey_F; + case GLFW_KEY_G: return ImGuiKey_G; + case GLFW_KEY_H: return ImGuiKey_H; + case GLFW_KEY_I: return ImGuiKey_I; + case GLFW_KEY_J: return ImGuiKey_J; + case GLFW_KEY_K: return ImGuiKey_K; + case GLFW_KEY_L: return ImGuiKey_L; + case GLFW_KEY_M: return ImGuiKey_M; + case GLFW_KEY_N: return ImGuiKey_N; + case GLFW_KEY_O: return ImGuiKey_O; + case GLFW_KEY_P: return ImGuiKey_P; + case GLFW_KEY_Q: return ImGuiKey_Q; + case GLFW_KEY_R: return ImGuiKey_R; + case GLFW_KEY_S: return ImGuiKey_S; + case GLFW_KEY_T: return ImGuiKey_T; + case GLFW_KEY_U: return ImGuiKey_U; + case GLFW_KEY_V: return ImGuiKey_V; + case GLFW_KEY_W: return ImGuiKey_W; + case GLFW_KEY_X: return ImGuiKey_X; + case GLFW_KEY_Y: return ImGuiKey_Y; + case GLFW_KEY_Z: return ImGuiKey_Z; + case GLFW_KEY_F1: return ImGuiKey_F1; + case GLFW_KEY_F2: return ImGuiKey_F2; + case GLFW_KEY_F3: return ImGuiKey_F3; + case GLFW_KEY_F4: return ImGuiKey_F4; + case GLFW_KEY_F5: return ImGuiKey_F5; + case GLFW_KEY_F6: return ImGuiKey_F6; + case GLFW_KEY_F7: return ImGuiKey_F7; + case GLFW_KEY_F8: return ImGuiKey_F8; + case GLFW_KEY_F9: return ImGuiKey_F9; + case GLFW_KEY_F10: return ImGuiKey_F10; + case GLFW_KEY_F11: return ImGuiKey_F11; + case GLFW_KEY_F12: return ImGuiKey_F12; + default: return ImGuiKey_None; + } +} + +// X11 does not include current pressed/released modifier key in 'mods' flags submitted by GLFW +// See https://github.com/ocornut/imgui/issues/6034 and https://github.com/glfw/glfw/issues/1630 +static void ImGui_ImplGlfw_UpdateKeyModifiers() +{ + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + io.AddKeyEvent(ImGuiMod_Ctrl, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Shift, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Alt, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); +} + void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackMousebutton != NULL && window == bd->Window) + if (bd->PrevUserCallbackMousebutton != nullptr && window == bd->Window) bd->PrevUserCallbackMousebutton(window, button, action, mods); - if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(bd->MouseJustPressed)) - bd->MouseJustPressed[button] = true; + ImGui_ImplGlfw_UpdateKeyModifiers(); + + ImGuiIO& io = ImGui::GetIO(); + if (button >= 0 && button < ImGuiMouseButton_COUNT) + io.AddMouseButtonEvent(button, action == GLFW_PRESS); } void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackScroll != NULL && window == bd->Window) + if (bd->PrevUserCallbackScroll != nullptr && window == bd->Window) bd->PrevUserCallbackScroll(window, xoffset, yoffset); ImGuiIO& io = ImGui::GetIO(); - io.MouseWheelH += (float)xoffset; - io.MouseWheel += (float)yoffset; + io.AddMouseWheelEvent((float)xoffset, (float)yoffset); } -void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) +static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) { - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackKey != NULL && window == bd->Window) - bd->PrevUserCallbackKey(window, key, scancode, action, mods); - - ImGuiIO& io = ImGui::GetIO(); - if (key >= 0 && key < IM_ARRAYSIZE(io.KeysDown)) +#if GLFW_HAS_GETKEYNAME && !defined(__EMSCRIPTEN__) + // GLFW 3.1+ attempts to "untranslate" keys, which goes the opposite of what every other framework does, making using lettered shortcuts difficult. + // (It had reasons to do so: namely GLFW is/was more likely to be used for WASD-type game controls rather than lettered shortcuts, but IHMO the 3.1 change could have been done differently) + // See https://github.com/glfw/glfw/issues/1502 for details. + // Adding a workaround to undo this (so our keys are translated->untranslated->translated, likely a lossy process). + // This won't cover edge cases but this is at least going to cover common cases. + if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_EQUAL) + return key; + GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); + const char* key_name = glfwGetKeyName(key, scancode); + glfwSetErrorCallback(prev_error_callback); +#if (GLFW_VERSION_COMBINED >= 3300) // Eat errors (see #5908) + (void)glfwGetError(NULL); +#endif + if (key_name && key_name[0] != 0 && key_name[1] == 0) { - if (action == GLFW_PRESS) - io.KeysDown[key] = true; - if (action == GLFW_RELEASE) - io.KeysDown[key] = false; + const char char_names[] = "`-=[]\\,;\'./"; + const int char_keys[] = { GLFW_KEY_GRAVE_ACCENT, GLFW_KEY_MINUS, GLFW_KEY_EQUAL, GLFW_KEY_LEFT_BRACKET, GLFW_KEY_RIGHT_BRACKET, GLFW_KEY_BACKSLASH, GLFW_KEY_COMMA, GLFW_KEY_SEMICOLON, GLFW_KEY_APOSTROPHE, GLFW_KEY_PERIOD, GLFW_KEY_SLASH, 0 }; + IM_ASSERT(IM_ARRAYSIZE(char_names) == IM_ARRAYSIZE(char_keys)); + if (key_name[0] >= '0' && key_name[0] <= '9') { key = GLFW_KEY_0 + (key_name[0] - '0'); } + else if (key_name[0] >= 'A' && key_name[0] <= 'Z') { key = GLFW_KEY_A + (key_name[0] - 'A'); } + else if (key_name[0] >= 'a' && key_name[0] <= 'z') { key = GLFW_KEY_A + (key_name[0] - 'a'); } + else if (const char* p = strchr(char_names, key_name[0])) { key = char_keys[p - char_names]; } } - - // Modifiers are not reliable across systems - io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL]; - io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT]; - io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT]; -#ifdef _WIN32 - io.KeySuper = false; + // if (action == GLFW_PRESS) printf("key %d scancode %d name '%s'\n", key, scancode, key_name); #else - io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER]; + IM_UNUSED(scancode); #endif + return key; +} + +void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, int action, int mods) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackKey != nullptr && window == bd->Window) + bd->PrevUserCallbackKey(window, keycode, scancode, action, mods); + + if (action != GLFW_PRESS && action != GLFW_RELEASE) + return; + + ImGui_ImplGlfw_UpdateKeyModifiers(); + + keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode); + + ImGuiIO& io = ImGui::GetIO(); + ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode); + io.AddKeyEvent(imgui_key, (action == GLFW_PRESS)); + io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code) } void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackWindowFocus != NULL && window == bd->Window) + if (bd->PrevUserCallbackWindowFocus != nullptr && window == bd->Window) bd->PrevUserCallbackWindowFocus(window, focused); ImGuiIO& io = ImGui::GetIO(); io.AddFocusEvent(focused != 0); } +void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + if (bd->PrevUserCallbackCursorPos != nullptr && window == bd->Window) + bd->PrevUserCallbackCursorPos(window, x, y); + if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) + return; + + ImGuiIO& io = ImGui::GetIO(); + io.AddMousePosEvent((float)x, (float)y); + bd->LastValidMousePos = ImVec2((float)x, (float)y); +} + +// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position, +// so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984) void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackCursorEnter != NULL && window == bd->Window) + if (bd->PrevUserCallbackCursorEnter != nullptr && window == bd->Window) bd->PrevUserCallbackCursorEnter(window, entered); + if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) + return; + ImGuiIO& io = ImGui::GetIO(); if (entered) + { bd->MouseWindow = window; - if (!entered && bd->MouseWindow == window) - bd->MouseWindow = NULL; + io.AddMousePosEvent(bd->LastValidMousePos.x, bd->LastValidMousePos.y); + } + else if (!entered && bd->MouseWindow == window) + { + bd->LastValidMousePos = io.MousePos; + bd->MouseWindow = nullptr; + io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); + } } void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackChar != NULL && window == bd->Window) + if (bd->PrevUserCallbackChar != nullptr && window == bd->Window) bd->PrevUserCallbackChar(window, c); ImGuiIO& io = ImGui::GetIO(); @@ -199,10 +401,53 @@ void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int) // Unused in 'master' branch but 'docking' branch will use this, so we declare it ahead of it so if you have to install callbacks you can install this one too. } +void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!"); + IM_ASSERT(bd->Window == window); + + bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback); + bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback); + bd->PrevUserCallbackCursorPos = glfwSetCursorPosCallback(window, ImGui_ImplGlfw_CursorPosCallback); + bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); + bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); + bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); + bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); + bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback); + bd->InstalledCallbacks = true; +} + +void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + IM_ASSERT(bd->InstalledCallbacks == true && "Callbacks not installed!"); + IM_ASSERT(bd->Window == window); + + glfwSetWindowFocusCallback(window, bd->PrevUserCallbackWindowFocus); + glfwSetCursorEnterCallback(window, bd->PrevUserCallbackCursorEnter); + glfwSetCursorPosCallback(window, bd->PrevUserCallbackCursorPos); + glfwSetMouseButtonCallback(window, bd->PrevUserCallbackMousebutton); + glfwSetScrollCallback(window, bd->PrevUserCallbackScroll); + glfwSetKeyCallback(window, bd->PrevUserCallbackKey); + glfwSetCharCallback(window, bd->PrevUserCallbackChar); + glfwSetMonitorCallback(bd->PrevUserCallbackMonitor); + bd->InstalledCallbacks = false; + bd->PrevUserCallbackWindowFocus = nullptr; + bd->PrevUserCallbackCursorEnter = nullptr; + bd->PrevUserCallbackCursorPos = nullptr; + bd->PrevUserCallbackMousebutton = nullptr; + bd->PrevUserCallbackScroll = nullptr; + bd->PrevUserCallbackKey = nullptr; + bd->PrevUserCallbackChar = nullptr; + bd->PrevUserCallbackMonitor = nullptr; +} + static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) { ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.BackendPlatformUserData == NULL && "Already initialized a platform backend!"); + IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); + //printf("GLFW_VERSION: %d.%d.%d (%d)", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION, GLFW_VERSION_COMBINED); // Setup backend capabilities flags ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)(); @@ -214,42 +459,20 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw bd->Window = window; bd->Time = 0.0; - // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array. - io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; - io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT; - io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT; - io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; - io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; - io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP; - io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN; - io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; - io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; - io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT; - io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; - io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; - io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE; - io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; - io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; - io.KeyMap[ImGuiKey_KeyPadEnter] = GLFW_KEY_KP_ENTER; - io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; - io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; - io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; - io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; - io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; - io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; - io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; io.ClipboardUserData = bd->Window; + + // Set platform dependent data in viewport #if defined(_WIN32) - io.ImeWindowHandle = (void*)glfwGetWin32Window(bd->Window); + ImGui::GetMainViewport()->PlatformHandleRaw = (void*)glfwGetWin32Window(bd->Window); #endif // Create mouse cursors // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist, // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting. - // Missing cursors will return NULL and our _UpdateMouseCursor() function will use the Arrow cursor instead.) - GLFWerrorfun prev_error_callback = glfwSetErrorCallback(NULL); + // Missing cursors will return nullptr and our _UpdateMouseCursor() function will use the Arrow cursor instead.) + GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); bd->MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); bd->MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); @@ -267,26 +490,13 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); #endif glfwSetErrorCallback(prev_error_callback); +#if (GLFW_VERSION_COMBINED >= 3300) // Eat errors (see #5785) + (void)glfwGetError(NULL); +#endif // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. - bd->PrevUserCallbackWindowFocus = NULL; - bd->PrevUserCallbackCursorEnter = NULL; - bd->PrevUserCallbackMousebutton = NULL; - bd->PrevUserCallbackScroll = NULL; - bd->PrevUserCallbackKey = NULL; - bd->PrevUserCallbackChar = NULL; - bd->PrevUserCallbackMonitor = NULL; if (install_callbacks) - { - bd->InstalledCallbacks = true; - bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback); - bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback); - bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); - bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); - bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); - bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); - bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback); - } + ImGui_ImplGlfw_InstallCallbacks(window); bd->ClientApi = client_api; return true; @@ -310,61 +520,50 @@ bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks) void ImGui_ImplGlfw_Shutdown() { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != NULL && "No platform backend to shutdown, or already shutdown?"); + IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); ImGuiIO& io = ImGui::GetIO(); if (bd->InstalledCallbacks) - { - glfwSetWindowFocusCallback(bd->Window, bd->PrevUserCallbackWindowFocus); - glfwSetCursorEnterCallback(bd->Window, bd->PrevUserCallbackCursorEnter); - glfwSetMouseButtonCallback(bd->Window, bd->PrevUserCallbackMousebutton); - glfwSetScrollCallback(bd->Window, bd->PrevUserCallbackScroll); - glfwSetKeyCallback(bd->Window, bd->PrevUserCallbackKey); - glfwSetCharCallback(bd->Window, bd->PrevUserCallbackChar); - glfwSetMonitorCallback(bd->PrevUserCallbackMonitor); - } + ImGui_ImplGlfw_RestoreCallbacks(bd->Window); for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) glfwDestroyCursor(bd->MouseCursors[cursor_n]); - io.BackendPlatformName = NULL; - io.BackendPlatformUserData = NULL; + io.BackendPlatformName = nullptr; + io.BackendPlatformUserData = nullptr; IM_DELETE(bd); } -static void ImGui_ImplGlfw_UpdateMousePosAndButtons() +static void ImGui_ImplGlfw_UpdateMouseData() { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); ImGuiIO& io = ImGui::GetIO(); - const ImVec2 mouse_pos_prev = io.MousePos; - io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); - - // Update mouse buttons - // (if a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame) - for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) + if (glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) { - io.MouseDown[i] = bd->MouseJustPressed[i] || glfwGetMouseButton(bd->Window, i) != 0; - bd->MouseJustPressed[i] = false; + io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); + return; } #ifdef __EMSCRIPTEN__ - const bool focused = true; + const bool is_app_focused = true; #else - const bool focused = glfwGetWindowAttrib(bd->Window, GLFW_FOCUSED) != 0; + const bool is_app_focused = glfwGetWindowAttrib(bd->Window, GLFW_FOCUSED) != 0; #endif - GLFWwindow* mouse_window = (bd->MouseWindow == bd->Window || focused) ? bd->Window : NULL; - - // Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) - if (io.WantSetMousePos && focused) - glfwSetCursorPos(bd->Window, (double)mouse_pos_prev.x, (double)mouse_pos_prev.y); - - // Set Dear ImGui mouse position from OS position - if (mouse_window != NULL) + if (is_app_focused) { - double mouse_x, mouse_y; - glfwGetCursorPos(mouse_window, &mouse_x, &mouse_y); - io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); + // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) + if (io.WantSetMousePos) + glfwSetCursorPos(bd->Window, (double)io.MousePos.x, (double)io.MousePos.y); + + // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured) + if (is_app_focused && bd->MouseWindow == nullptr) + { + double mouse_x, mouse_y; + glfwGetCursorPos(bd->Window, &mouse_x, &mouse_y); + io.AddMousePosEvent((float)mouse_x, (float)mouse_y); + bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y); + } } } @@ -390,48 +589,64 @@ static void ImGui_ImplGlfw_UpdateMouseCursor() } } +// Update gamepad inputs +static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } static void ImGui_ImplGlfw_UpdateGamepads() { ImGuiIO& io = ImGui::GetIO(); - memset(io.NavInputs, 0, sizeof(io.NavInputs)); - if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) + if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. return; - // Update gamepad inputs - #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; } - #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; } + io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; +#if GLFW_HAS_GAMEPAD_API + GLFWgamepadstate gamepad; + if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad)) + return; + #define MAP_BUTTON(KEY_NO, BUTTON_NO, _UNUSED) do { io.AddKeyEvent(KEY_NO, gamepad.buttons[BUTTON_NO] != 0); } while (0) + #define MAP_ANALOG(KEY_NO, AXIS_NO, _UNUSED, V0, V1) do { float v = gamepad.axes[AXIS_NO]; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0) +#else int axes_count = 0, buttons_count = 0; const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count); const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count); - MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A - MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B - MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X - MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y - MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left - MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right - MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up - MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down - MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB - MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB - MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB - MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB - MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f); - MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f); - MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f); - MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f); + if (axes_count == 0 || buttons_count == 0) + return; + #define MAP_BUTTON(KEY_NO, _UNUSED, BUTTON_NO) do { io.AddKeyEvent(KEY_NO, (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS)); } while (0) + #define MAP_ANALOG(KEY_NO, _UNUSED, AXIS_NO, V0, V1) do { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0) +#endif + io.BackendFlags |= ImGuiBackendFlags_HasGamepad; + MAP_BUTTON(ImGuiKey_GamepadStart, GLFW_GAMEPAD_BUTTON_START, 7); + MAP_BUTTON(ImGuiKey_GamepadBack, GLFW_GAMEPAD_BUTTON_BACK, 6); + MAP_BUTTON(ImGuiKey_GamepadFaceLeft, GLFW_GAMEPAD_BUTTON_X, 2); // Xbox X, PS Square + MAP_BUTTON(ImGuiKey_GamepadFaceRight, GLFW_GAMEPAD_BUTTON_B, 1); // Xbox B, PS Circle + MAP_BUTTON(ImGuiKey_GamepadFaceUp, GLFW_GAMEPAD_BUTTON_Y, 3); // Xbox Y, PS Triangle + MAP_BUTTON(ImGuiKey_GamepadFaceDown, GLFW_GAMEPAD_BUTTON_A, 0); // Xbox A, PS Cross + MAP_BUTTON(ImGuiKey_GamepadDpadLeft, GLFW_GAMEPAD_BUTTON_DPAD_LEFT, 13); + MAP_BUTTON(ImGuiKey_GamepadDpadRight, GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, 11); + MAP_BUTTON(ImGuiKey_GamepadDpadUp, GLFW_GAMEPAD_BUTTON_DPAD_UP, 10); + MAP_BUTTON(ImGuiKey_GamepadDpadDown, GLFW_GAMEPAD_BUTTON_DPAD_DOWN, 12); + MAP_BUTTON(ImGuiKey_GamepadL1, GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, 4); + MAP_BUTTON(ImGuiKey_GamepadR1, GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, 5); + MAP_ANALOG(ImGuiKey_GamepadL2, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, 4, -0.75f, +1.0f); + MAP_ANALOG(ImGuiKey_GamepadR2, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, 5, -0.75f, +1.0f); + MAP_BUTTON(ImGuiKey_GamepadL3, GLFW_GAMEPAD_BUTTON_LEFT_THUMB, 8); + MAP_BUTTON(ImGuiKey_GamepadR3, GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, 9); + MAP_ANALOG(ImGuiKey_GamepadLStickLeft, GLFW_GAMEPAD_AXIS_LEFT_X, 0, -0.25f, -1.0f); + MAP_ANALOG(ImGuiKey_GamepadLStickRight, GLFW_GAMEPAD_AXIS_LEFT_X, 0, +0.25f, +1.0f); + MAP_ANALOG(ImGuiKey_GamepadLStickUp, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, -0.25f, -1.0f); + MAP_ANALOG(ImGuiKey_GamepadLStickDown, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, +0.25f, +1.0f); + MAP_ANALOG(ImGuiKey_GamepadRStickLeft, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, -0.25f, -1.0f); + MAP_ANALOG(ImGuiKey_GamepadRStickRight, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, +0.25f, +1.0f); + MAP_ANALOG(ImGuiKey_GamepadRStickUp, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, -0.25f, -1.0f); + MAP_ANALOG(ImGuiKey_GamepadRStickDown, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, +0.25f, +1.0f); #undef MAP_BUTTON #undef MAP_ANALOG - if (axes_count > 0 && buttons_count > 0) - io.BackendFlags |= ImGuiBackendFlags_HasGamepad; - else - io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; } void ImGui_ImplGlfw_NewFrame() { ImGuiIO& io = ImGui::GetIO(); ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != NULL && "Did you call ImGui_ImplGlfw_InitForXXX()?"); + IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); // Setup display size (every frame to accommodate for window resizing) int w, h; @@ -440,16 +655,20 @@ void ImGui_ImplGlfw_NewFrame() glfwGetFramebufferSize(bd->Window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); if (w > 0 && h > 0) - io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); + io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h); // Setup time step double current_time = glfwGetTime(); io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f); bd->Time = current_time; - ImGui_ImplGlfw_UpdateMousePosAndButtons(); + ImGui_ImplGlfw_UpdateMouseData(); ImGui_ImplGlfw_UpdateMouseCursor(); // Update game controllers (if enabled and available) ImGui_ImplGlfw_UpdateGamepads(); } + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index 0ed8348d..c98110d8 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -14,6 +14,11 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2022-11-09: OpenGL: Reverted use of glBufferSubData(), too many corruptions issues + old issues seemingly can't be reproed with Intel drivers nowadays (revert 2021-12-15 and 2022-05-23 changes). +// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. +// 2022-09-27: OpenGL: Added ability to '#define IMGUI_IMPL_OPENGL_DEBUG'. +// 2022-05-23: OpenGL: Reworking 2021-12-15 "Using buffer orphaning" so it only happens on Intel GPU, seems to cause problems otherwise. (#4468, #4825, #4832, #5127). +// 2022-05-13: OpenGL: Fix state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states. // 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers. // 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions. // 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader. @@ -54,7 +59,7 @@ // 2018-06-08: Misc: Extracted imgui_impl_opengl3.cpp/.h away from the old combined GLFW/SDL+OpenGL3 examples. // 2018-06-08: OpenGL: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. // 2018-05-25: OpenGL: Removed unnecessary backup/restore of GL_ELEMENT_ARRAY_BUFFER_BINDING since this is part of the VAO state. -// 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a NULL pointer. +// 2018-05-14: OpenGL: Making the call to glBindSampler() optional so 3.2 context won't fail if the function is a nullptr pointer. // 2018-03-06: OpenGL: Added const char* glsl_version parameter to ImGui_ImplOpenGL3_Init() so user can override the GLSL version e.g. "#version 150". // 2018-02-23: OpenGL: Create the VAO in the render function so the setup can more easily be used with multiple shared GL context. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself. @@ -95,10 +100,28 @@ #else #include // intptr_t #endif +#if defined(__APPLE__) +#include +#endif + +// Clang warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#endif +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types +#endif // GL includes #if defined(IMGUI_IMPL_OPENGL_ES2) -#include +#if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) +#include // Use GL ES 2 +#else +#include // Use GL ES 2 +#endif #if defined(__EMSCRIPTEN__) #ifndef GL_GLEXT_PROTOTYPES #define GL_GLEXT_PROTOTYPES @@ -106,9 +129,6 @@ #include #endif #elif defined(IMGUI_IMPL_OPENGL_ES3) -#if defined(__APPLE__) -#include -#endif #if (defined(__APPLE__) && (TARGET_OS_IOS || TARGET_OS_TV)) #include // Use GL ES 3 #else @@ -162,6 +182,15 @@ #define IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS #endif +// [Debugging] +//#define IMGUI_IMPL_OPENGL_DEBUG +#ifdef IMGUI_IMPL_OPENGL_DEBUG +#include +#define GL_CALL(_CALL) do { _CALL; GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "GL error 0x%x returned from '%s'.\n", gl_err, #_CALL); } while (0) // Call with error check +#else +#define GL_CALL(_CALL) _CALL // Call without error check +#endif + // OpenGL Data struct ImGui_ImplOpenGL3_Data { @@ -178,22 +207,47 @@ struct ImGui_ImplOpenGL3_Data GLsizeiptr VertexBufferSize; GLsizeiptr IndexBufferSize; bool HasClipOrigin; + bool UseBufferSubData; - ImGui_ImplOpenGL3_Data() { memset(this, 0, sizeof(*this)); } + ImGui_ImplOpenGL3_Data() { memset((void*)this, 0, sizeof(*this)); } }; // Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. static ImGui_ImplOpenGL3_Data* ImGui_ImplOpenGL3_GetBackendData() { - return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL3_Data*)ImGui::GetIO().BackendRendererUserData : NULL; + return ImGui::GetCurrentContext() ? (ImGui_ImplOpenGL3_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; } +// OpenGL vertex attribute state (for ES 1.0 and ES 2.0 only) +#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY +struct ImGui_ImplOpenGL3_VtxAttribState +{ + GLint Enabled, Size, Type, Normalized, Stride; + GLvoid* Ptr; + + void GetState(GLint index) + { + glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &Enabled); + glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_SIZE, &Size); + glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_TYPE, &Type); + glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, &Normalized); + glGetVertexAttribiv(index, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &Stride); + glGetVertexAttribPointerv(index, GL_VERTEX_ATTRIB_ARRAY_POINTER, &Ptr); + } + void SetState(GLint index) + { + glVertexAttribPointer(index, Size, Type, (GLboolean)Normalized, Stride, Ptr); + if (Enabled) glEnableVertexAttribArray(index); else glDisableVertexAttribArray(index); + } +}; +#endif + // Functions bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.BackendRendererUserData == NULL && "Already initialized a renderer backend!"); + IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); // Initialize our loader #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) @@ -222,18 +276,32 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) sscanf(gl_version, "%d.%d", &major, &minor); } bd->GlVersion = (GLuint)(major * 100 + minor * 10); + + bd->UseBufferSubData = false; + /* + // Query vendor to enable glBufferSubData kludge +#ifdef _WIN32 + if (const char* vendor = (const char*)glGetString(GL_VENDOR)) + if (strncmp(vendor, "Intel", 5) == 0) + bd->UseBufferSubData = true; +#endif + */ #else bd->GlVersion = 200; // GLES 2 #endif +#ifdef IMGUI_IMPL_OPENGL_DEBUG + printf("GL_MAJOR_VERSION = %d\nGL_MINOR_VERSION = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", major, minor, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] +#endif + #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET if (bd->GlVersion >= 320) io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif // Store GLSL version string so we can refer to it later in case we recreate shaders. - // Note: GLSL version is NOT the same as GL version. Leave this to NULL if unsure. - if (glsl_version == NULL) + // Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure. + if (glsl_version == nullptr) { #if defined(IMGUI_IMPL_OPENGL_ES2) glsl_version = "#version 100"; @@ -262,7 +330,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) for (GLint i = 0; i < num_extensions; i++) { const char* extension = (const char*)glGetStringi(GL_EXTENSIONS, i); - if (extension != NULL && strcmp(extension, "GL_ARB_clip_control") == 0) + if (extension != nullptr && strcmp(extension, "GL_ARB_clip_control") == 0) bd->HasClipOrigin = true; } #endif @@ -273,19 +341,19 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) void ImGui_ImplOpenGL3_Shutdown() { ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - IM_ASSERT(bd != NULL && "No renderer backend to shutdown, or already shutdown?"); + IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); ImGuiIO& io = ImGui::GetIO(); ImGui_ImplOpenGL3_DestroyDeviceObjects(); - io.BackendRendererName = NULL; - io.BackendRendererUserData = NULL; + io.BackendRendererName = nullptr; + io.BackendRendererUserData = nullptr; IM_DELETE(bd); } void ImGui_ImplOpenGL3_NewFrame() { ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - IM_ASSERT(bd != NULL && "Did you call ImGui_ImplOpenGL3_Init()?"); + IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplOpenGL3_Init()?"); if (!bd->ShaderHandle) ImGui_ImplOpenGL3_CreateDeviceObjects(); @@ -324,7 +392,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid // Setup viewport, orthographic projection matrix // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps. - glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height); + GL_CALL(glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height)); float L = draw_data->DisplayPos.x; float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; float T = draw_data->DisplayPos.y; @@ -354,14 +422,14 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid #endif // Bind vertex/index buffers and setup attributes for ImDrawVert - glBindBuffer(GL_ARRAY_BUFFER, bd->VboHandle); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd->ElementsHandle); - glEnableVertexAttribArray(bd->AttribLocationVtxPos); - glEnableVertexAttribArray(bd->AttribLocationVtxUV); - glEnableVertexAttribArray(bd->AttribLocationVtxColor); - glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos)); - glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv)); - glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col)); + GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, bd->VboHandle)); + GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bd->ElementsHandle)); + GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxPos)); + GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxUV)); + GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxColor)); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos))); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv))); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col))); } // OpenGL3 Render function. @@ -386,6 +454,13 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLuint last_sampler; if (bd->GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } #endif GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); +#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY + // This is part of VAO on OpenGL 3.0+ and OpenGL ES 3.0+. + GLint last_element_array_buffer; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &last_element_array_buffer); + ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_pos; last_vtx_attrib_state_pos.GetState(bd->AttribLocationVtxPos); + ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_uv; last_vtx_attrib_state_uv.GetState(bd->AttribLocationVtxUV); + ImGui_ImplOpenGL3_VtxAttribState last_vtx_attrib_state_color; last_vtx_attrib_state_color.GetState(bd->AttribLocationVtxColor); +#endif #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif @@ -414,7 +489,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // The renderer would actually work without any VAO bound, but then our VertexAttrib calls would overwrite the default one currently bound. GLuint vertex_array_object = 0; #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY - glGenVertexArrays(1, &vertex_array_object); + GL_CALL(glGenVertexArrays(1, &vertex_array_object)); #endif ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); @@ -428,25 +503,40 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) const ImDrawList* cmd_list = draw_data->CmdLists[n]; // Upload vertex/index buffers - GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); - GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); - if (bd->VertexBufferSize < vtx_buffer_size) + // - OpenGL drivers are in a very sorry state nowadays.... + // During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports + // of leaks on Intel GPU when using multi-viewports on Windows. + // - After this we kept hearing of various display corruptions issues. We started disabling on non-Intel GPU, but issues still got reported on Intel. + // - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code. + // We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path. + // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues. + const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); + const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); + if (bd->UseBufferSubData) { - bd->VertexBufferSize = vtx_buffer_size; - glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, NULL, GL_STREAM_DRAW); + if (bd->VertexBufferSize < vtx_buffer_size) + { + bd->VertexBufferSize = vtx_buffer_size; + GL_CALL(glBufferData(GL_ARRAY_BUFFER, bd->VertexBufferSize, nullptr, GL_STREAM_DRAW)); + } + if (bd->IndexBufferSize < idx_buffer_size) + { + bd->IndexBufferSize = idx_buffer_size; + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, nullptr, GL_STREAM_DRAW)); + } + GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data)); + GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data)); } - if (bd->IndexBufferSize < idx_buffer_size) + else { - bd->IndexBufferSize = idx_buffer_size; - glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, NULL, GL_STREAM_DRAW); + GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW)); + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW)); } - glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data); - glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data); for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; - if (pcmd->UserCallback != NULL) + if (pcmd->UserCallback != nullptr) { // User callback, registered via ImDrawList::AddCallback() // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) @@ -464,23 +554,23 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) continue; // Apply scissor/clipping rectangle (Y is inverted in OpenGL) - glScissor((int)clip_min.x, (int)(fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y)); + GL_CALL(glScissor((int)clip_min.x, (int)((float)fb_height - clip_max.y), (int)(clip_max.x - clip_min.x), (int)(clip_max.y - clip_min.y))); // Bind texture, Draw - glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID()); + GL_CALL(glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->GetTexID())); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET if (bd->GlVersion >= 320) - glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset); + GL_CALL(glDrawElementsBaseVertex(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)), (GLint)pcmd->VtxOffset)); else #endif - glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx))); + GL_CALL(glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, (void*)(intptr_t)(pcmd->IdxOffset * sizeof(ImDrawIdx)))); } } } // Destroy the temporary VAO #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY - glDeleteVertexArrays(1, &vertex_array_object); + GL_CALL(glDeleteVertexArrays(1, &vertex_array_object)); #endif // Restore modified GL state @@ -495,6 +585,12 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) glBindVertexArray(last_vertex_array_object); #endif glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); +#ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer); + last_vtx_attrib_state_pos.SetState(bd->AttribLocationVtxPos); + last_vtx_attrib_state_uv.SetState(bd->AttribLocationVtxUV); + last_vtx_attrib_state_color.SetState(bd->AttribLocationVtxColor); +#endif glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha); glBlendFuncSeparate(last_blend_src_rgb, last_blend_dst_rgb, last_blend_src_alpha, last_blend_dst_alpha); if (last_enable_blend) glEnable(GL_BLEND); else glDisable(GL_BLEND); @@ -525,22 +621,23 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. // Upload texture to graphics system + // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling) GLint last_texture; - glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); - glGenTextures(1, &bd->FontTexture); - glBindTexture(GL_TEXTURE_2D, bd->FontTexture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); + GL_CALL(glGenTextures(1, &bd->FontTexture)); + GL_CALL(glBindTexture(GL_TEXTURE_2D, bd->FontTexture)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); #ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); #endif - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); // Store our identifier io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); // Restore state - glBindTexture(GL_TEXTURE_2D, last_texture); + GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); return true; } @@ -570,7 +667,7 @@ static bool CheckShader(GLuint handle, const char* desc) { ImVector buf; buf.resize((int)(log_length + 1)); - glGetShaderInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); + glGetShaderInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin()); fprintf(stderr, "%s\n", buf.begin()); } return (GLboolean)status == GL_TRUE; @@ -589,7 +686,7 @@ static bool CheckProgram(GLuint handle, const char* desc) { ImVector buf; buf.resize((int)(log_length + 1)); - glGetProgramInfoLog(handle, log_length, NULL, (GLchar*)buf.begin()); + glGetProgramInfoLog(handle, log_length, nullptr, (GLchar*)buf.begin()); fprintf(stderr, "%s\n", buf.begin()); } return (GLboolean)status == GL_TRUE; @@ -713,8 +810,8 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() "}\n"; // Select shaders matching our GLSL versions - const GLchar* vertex_shader = NULL; - const GLchar* fragment_shader = NULL; + const GLchar* vertex_shader = nullptr; + const GLchar* fragment_shader = nullptr; if (glsl_version < 130) { vertex_shader = vertex_shader_glsl_120; @@ -739,13 +836,13 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() // Create shaders const GLchar* vertex_shader_with_version[2] = { bd->GlslVersionString, vertex_shader }; GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vert_handle, 2, vertex_shader_with_version, NULL); + glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr); glCompileShader(vert_handle); CheckShader(vert_handle, "vertex shader"); const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader }; GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(frag_handle, 2, fragment_shader_with_version, NULL); + glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr); glCompileShader(frag_handle); CheckShader(frag_handle, "fragment shader"); @@ -791,3 +888,10 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects() if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; } ImGui_ImplOpenGL3_DestroyFontsTexture(); } + +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif +#if defined(__clang__) +#pragma clang diagnostic pop +#endif diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 8a3fe93c..47fc8a28 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (tables and columns code) /* @@ -24,7 +24,7 @@ Index of this file: */ // Navigating this file: -// - In Visual Studio IDE: CTRL+comma ("Edit.NavigateTo") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. //----------------------------------------------------------------------------- @@ -361,6 +361,8 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->IsLayoutLocked = false; table->InnerWidth = inner_width; temp_data->UserOuterSize = outer_size; + if (instance_no > 0 && table->InstanceDataExtra.Size < instance_no) + table->InstanceDataExtra.push_back(ImGuiTableInstanceData()); // When not using a child window, WorkRect.Max will grow as we append contents. if (use_child_window) @@ -393,6 +395,14 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->OuterRect = table->InnerWindow->Rect(); table->InnerRect = table->InnerWindow->InnerRect; IM_ASSERT(table->InnerWindow->WindowPadding.x == 0.0f && table->InnerWindow->WindowPadding.y == 0.0f && table->InnerWindow->WindowBorderSize == 0.0f); + + // When using multiple instances, ensure they have the same amount of horizontal decorations (aka vertical scrollbar) so stretched columns can be aligned) + if (instance_no == 0) + { + table->HasScrollbarYPrev = table->HasScrollbarYCurr; + table->HasScrollbarYCurr = false; + } + table->HasScrollbarYCurr |= (table->InnerWindow->ScrollMax.y > 0.0f); } else { @@ -537,7 +547,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (table->RefScale != 0.0f && table->RefScale != new_ref_scale_unit) { const float scale_factor = new_ref_scale_unit / table->RefScale; - //IMGUI_DEBUG_LOG("[table] %08X RefScaleUnit %.3f -> %.3f, scaling width by %.3f\n", table->ID, table->RefScaleUnit, new_ref_scale_unit, scale_factor); + //IMGUI_DEBUG_PRINT("[table] %08X RefScaleUnit %.3f -> %.3f, scaling width by %.3f\n", table->ID, table->RefScaleUnit, new_ref_scale_unit, scale_factor); for (int n = 0; n < columns_count; n++) table->Columns[n].WidthRequest = table->Columns[n].WidthRequest * scale_factor; } @@ -886,11 +896,13 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) sum_width_requests += table->CellPaddingX * 2.0f; } table->ColumnsEnabledFixedCount = (ImGuiTableColumnIdx)count_fixed; + table->ColumnsStretchSumWeights = stretch_sum_weights; // [Part 4] Apply final widths based on requested widths const ImRect work_rect = table->WorkRect; const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); - const float width_avail = ((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f) ? table->InnerClipRect.GetWidth() : work_rect.GetWidth(); + const float width_removed = (table->HasScrollbarYPrev && !table->InnerWindow->ScrollbarY) ? g.Style.ScrollbarSize : 0.0f; // To synchronize decoration width of synched tables with mismatching scrollbar state (#5920) + const float width_avail = ImMax(1.0f, (((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f) ? table->InnerClipRect.GetWidth() : work_rect.GetWidth()) - width_removed); const float width_avail_for_stretched_columns = width_avail - width_spacings - sum_width_requests; float width_remaining_for_stretched_columns = width_avail_for_stretched_columns; table->ColumnsGivenWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount; @@ -933,10 +945,19 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) width_remaining_for_stretched_columns -= 1.0f; } + // Determine if table is hovered which will be used to flag columns as hovered. + // - In principle we'd like to use the equivalent of IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), + // but because our item is partially submitted at this point we use ItemHoverable() and a workaround (temporarily + // clear ActiveId, which is equivalent to the change provided by _AllowWhenBLockedByActiveItem). + // - This allows columns to be marked as hovered when e.g. clicking a button inside the column, or using drag and drop. + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); table->HoveredColumnBody = -1; table->HoveredColumnBorder = -1; - const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table->LastOuterHeight)); + const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table_instance->LastOuterHeight)); + const ImGuiID backup_active_id = g.ActiveId; + g.ActiveId = 0; const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0); + g.ActiveId = backup_active_id; // [Part 6] Setup final position, offset, skip/clip states and clipping rectangles, detect hovered column // Process columns in their visible orders as we are comparing the visible order and adjusting host_clip_rect while looping. @@ -952,7 +973,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) const int column_n = table->DisplayOrderToIndex[order_n]; ImGuiTableColumn* column = &table->Columns[column_n]; - column->NavLayerCurrent = (ImS8)((table->FreezeRowsCount > 0 || column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main); + column->NavLayerCurrent = (ImS8)(table->FreezeRowsCount > 0 ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main); // Use Count NOT request so Header line changes layer when frozen if (offset_x_frozen && table->FreezeColumnsCount == visible_n) { @@ -1096,23 +1117,15 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // [Part 10] Hit testing on borders if (table->Flags & ImGuiTableFlags_Resizable) TableUpdateBorders(table); - table->LastFirstRowHeight = 0.0f; + table_instance->LastFirstRowHeight = 0.0f; table->IsLayoutLocked = true; table->IsUsingHeaders = false; // [Part 11] Context menu - if (table->IsContextPopupOpen && table->InstanceCurrent == table->InstanceInteracted) + if (TableBeginContextMenuPopup(table)) { - const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID); - if (BeginPopupEx(context_menu_id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings)) - { - TableDrawContextMenu(table); - EndPopup(); - } - else - { - table->IsContextPopupOpen = false; - } + TableDrawContextMenu(table); + EndPopup(); } // [Part 13] Sanitize and build sort specs before we have a change to use them for display. @@ -1120,6 +1133,13 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (table->IsSortSpecsDirty && (table->Flags & ImGuiTableFlags_Sortable)) TableSortSpecsBuild(table); + // [Part 14] Setup inner window decoration size (for scrolling / nav tracking to properly take account of frozen rows/columns) + if (table->FreezeColumnsRequest > 0) + table->InnerWindow->DecoInnerSizeX1 = table->Columns[table->DisplayOrderToIndex[table->FreezeColumnsRequest - 1]].MaxX - table->OuterRect.Min.x; + if (table->FreezeRowsRequest > 0) + table->InnerWindow->DecoInnerSizeY1 = table_instance->LastFrozenHeight; + table_instance->LastFrozenHeight = 0.0f; + // Initial state ImGuiWindow* inner_window = table->InnerWindow; if (table->Flags & ImGuiTableFlags_NoClip) @@ -1141,10 +1161,11 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) // use the final height from last frame. Because this is only affecting _interaction_ with columns, it is not // really problematic (whereas the actual visual will be displayed in EndTable() and using the current frame height). // Actual columns highlight/render will be performed in EndTable() and not be affected. + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS; const float hit_y1 = table->OuterRect.Min.y; - const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table->LastOuterHeight); - const float hit_y2_head = hit_y1 + table->LastFirstRowHeight; + const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table_instance->LastOuterHeight); + const float hit_y2_head = hit_y1 + table_instance->LastFirstRowHeight; for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { @@ -1166,8 +1187,8 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) ImGuiID column_id = TableGetColumnResizeID(table, column_n, table->InstanceCurrent); ImRect hit_rect(column->MaxX - hit_half_width, hit_y1, column->MaxX + hit_half_width, border_y2_hit); + ItemAdd(hit_rect, column_id, NULL, ImGuiItemFlags_NoNav); //GetForegroundDrawList()->AddRect(hit_rect.Min, hit_rect.Max, IM_COL32(255, 0, 0, 100)); - KeepAliveID(column_id); bool hovered = false, held = false; bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_NoNavFocus); @@ -1223,6 +1244,7 @@ void ImGui::EndTable() TableOpenContextMenu((int)table->HoveredColumnBody); // Finalize table height + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); inner_window->DC.PrevLineSize = temp_data->HostBackupPrevLineSize; inner_window->DC.CurrLineSize = temp_data->HostBackupCurrLineSize; inner_window->DC.CursorMaxPos = temp_data->HostBackupCursorMaxPos; @@ -1233,7 +1255,7 @@ void ImGui::EndTable() else if (!(flags & ImGuiTableFlags_NoHostExtendY)) table->OuterRect.Max.y = table->InnerRect.Max.y = ImMax(table->OuterRect.Max.y, inner_content_max_y); // Patch OuterRect/InnerRect height table->WorkRect.Max.y = ImMax(table->WorkRect.Max.y, table->OuterRect.Max.y); - table->LastOuterHeight = table->OuterRect.GetHeight(); + table_instance->LastOuterHeight = table->OuterRect.GetHeight(); // Setup inner scrolling range // FIXME: This ideally should be done earlier, in BeginTable() SetNextWindowContentSize call, just like writing to inner_window->DC.CursorMaxPos.y, @@ -1279,17 +1301,23 @@ void ImGui::EndTable() splitter->Merge(inner_window->DrawList); // Update ColumnsAutoFitWidth to get us ahead for host using our size to auto-resize without waiting for next BeginTable() - const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); - table->ColumnsAutoFitWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount; + float auto_fit_width_for_fixed = 0.0f; + float auto_fit_width_for_stretched = 0.0f; + float auto_fit_width_for_stretched_min = 0.0f; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) if (table->EnabledMaskByIndex & ((ImU64)1 << column_n)) { ImGuiTableColumn* column = &table->Columns[column_n]; - if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !(column->Flags & ImGuiTableColumnFlags_NoResize)) - table->ColumnsAutoFitWidth += column->WidthRequest; + float column_width_request = ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !(column->Flags & ImGuiTableColumnFlags_NoResize)) ? column->WidthRequest : TableGetColumnWidthAuto(table, column); + if (column->Flags & ImGuiTableColumnFlags_WidthFixed) + auto_fit_width_for_fixed += column_width_request; else - table->ColumnsAutoFitWidth += TableGetColumnWidthAuto(table, column); + auto_fit_width_for_stretched += column_width_request; + if ((column->Flags & ImGuiTableColumnFlags_WidthStretch) && (column->Flags & ImGuiTableColumnFlags_NoResize) != 0) + auto_fit_width_for_stretched_min = ImMax(auto_fit_width_for_stretched_min, column_width_request / (column->StretchWeight / table->ColumnsStretchSumWeights)); } + const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsEnabledCount - 1); + table->ColumnsAutoFitWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount + auto_fit_width_for_fixed + ImMax(auto_fit_width_for_stretched, auto_fit_width_for_stretched_min); // Update scroll if ((table->Flags & ImGuiTableFlags_ScrollX) == 0 && inner_window != outer_window) @@ -1569,18 +1597,21 @@ ImGuiTableColumnFlags ImGui::TableGetColumnFlags(int column_n) // Return the cell rectangle based on currently known height. // - Important: we generally don't know our row height until the end of the row, so Max.y will be incorrect in many situations. -// The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it. +// The only case where this is correct is if we provided a min_row_height to TableNextRow() and don't go below it, or in TableEndRow() when we locked that height. // - Important: if ImGuiTableFlags_PadOuterX is set but ImGuiTableFlags_PadInnerX is not set, the outer-most left and right // columns report a small offset so their CellBgRect can extend up to the outer border. +// FIXME: But the rendering code in TableEndRow() nullifies that with clamping required for scrolling. ImRect ImGui::TableGetCellBgRect(const ImGuiTable* table, int column_n) { const ImGuiTableColumn* column = &table->Columns[column_n]; float x1 = column->MinX; float x2 = column->MaxX; - if (column->PrevEnabledColumn == -1) - x1 -= table->CellSpacingX1; - if (column->NextEnabledColumn == -1) - x2 += table->CellSpacingX2; + //if (column->PrevEnabledColumn == -1) + // x1 -= table->OuterPaddingX; + //if (column->NextEnabledColumn == -1) + // x2 += table->OuterPaddingX; + x1 = ImMax(x1, table->WorkRect.Min.x); + x2 = ImMin(x2, table->WorkRect.Max.x); return ImRect(x1, table->RowPosY1, x2, table->RowPosY2); } @@ -1710,6 +1741,8 @@ void ImGui::TableBeginRow(ImGuiTable* table) table->RowTextBaseline = 0.0f; table->RowIndentOffsetX = window->DC.Indent.x - table->HostIndentX; // Lock indent window->DC.PrevLineTextBaseOffset = 0.0f; + window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + window->DC.IsSameLine = window->DC.IsSetPos = false; window->DC.CursorMaxPos.y = next_y1; // Making the header BG color non-transparent will allow us to overlay it multiple times when handling smooth dragging. @@ -1746,7 +1779,7 @@ void ImGui::TableEndRow(ImGuiTable* table) const bool unfreeze_rows_actual = (table->CurrentRow + 1 == table->FreezeRowsCount); const bool unfreeze_rows_request = (table->CurrentRow + 1 == table->FreezeRowsRequest); if (table->CurrentRow == 0) - table->LastFirstRowHeight = bg_y2 - bg_y1; + TableGetInstanceData(table, table->InstanceCurrent)->LastFirstRowHeight = bg_y2 - bg_y1; const bool is_visible = (bg_y2 >= table->InnerClipRect.Min.y && bg_y1 <= table->InnerClipRect.Max.y); if (is_visible) @@ -1797,10 +1830,12 @@ void ImGui::TableEndRow(ImGuiTable* table) ImGuiTableCellData* cell_data_end = &table->RowCellData[table->RowCellDataCurrent]; for (ImGuiTableCellData* cell_data = &table->RowCellData[0]; cell_data <= cell_data_end; cell_data++) { + // As we render the BG here we need to clip things (for layout we would not) + // FIXME: This cancels the OuterPadding addition done by TableGetCellBgRect(), need to keep it while rendering correctly while scrolling. const ImGuiTableColumn* column = &table->Columns[cell_data->Column]; ImRect cell_bg_rect = TableGetCellBgRect(table, cell_data->Column); cell_bg_rect.ClipWith(table->BgClipRect); - cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped + cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped when scrolling cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->MaxX); window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor); } @@ -1820,17 +1855,15 @@ void ImGui::TableEndRow(ImGuiTable* table) // get the new cursor position. if (unfreeze_rows_request) for (int column_n = 0; column_n < table->ColumnsCount; column_n++) - { - ImGuiTableColumn* column = &table->Columns[column_n]; - column->NavLayerCurrent = (ImS8)((column_n < table->FreezeColumnsCount) ? ImGuiNavLayer_Menu : ImGuiNavLayer_Main); - } + table->Columns[column_n].NavLayerCurrent = ImGuiNavLayer_Main; if (unfreeze_rows_actual) { IM_ASSERT(table->IsUnfrozenRows == false); + const float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); table->IsUnfrozenRows = true; + TableGetInstanceData(table, table->InstanceCurrent)->LastFrozenHeight = y0 - table->OuterRect.Min.y; // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect - float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y; table->Bg2DrawChannelCurrent = table->Bg2DrawChannelUnfrozen; @@ -1989,6 +2022,9 @@ void ImGui::TableEndCell(ImGuiTable* table) ImGuiTableColumn* column = &table->Columns[table->CurrentColumn]; ImGuiWindow* window = table->InnerWindow; + if (window->DC.IsSetPos) + ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + // Report maximum position so we can infer content size per column. float* p_max_pos_x; if (table->RowFlags & ImGuiTableRowFlags_Headers) @@ -2083,7 +2119,7 @@ void ImGui::TableSetColumnWidth(int column_n, float width) if (column_0->WidthGiven == column_0_width || column_0->WidthRequest == column_0_width) return; - //IMGUI_DEBUG_LOG("TableSetColumnWidth(%d, %.1f->%.1f)\n", column_0_idx, column_0->WidthGiven, column_0_width); + //IMGUI_DEBUG_PRINT("TableSetColumnWidth(%d, %.1f->%.1f)\n", column_0_idx, column_0->WidthGiven, column_0_width); ImGuiTableColumn* column_1 = (column_0->NextEnabledColumn != -1) ? &table->Columns[column_0->NextEnabledColumn] : NULL; // In this surprisingly not simple because of how we support mixing Fixed and multiple Stretch columns. @@ -2353,7 +2389,7 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) // Don't attempt to merge if there are multiple draw calls within the column ImDrawChannel* src_channel = &splitter->_Channels[channel_no]; - if (src_channel->_CmdBuffer.Size > 0 && src_channel->_CmdBuffer.back().ElemCount == 0) + if (src_channel->_CmdBuffer.Size > 0 && src_channel->_CmdBuffer.back().ElemCount == 0 && src_channel->_CmdBuffer.back().UserCallback == NULL) // Equivalent of PopUnusedDrawCmd() src_channel->_CmdBuffer.pop_back(); if (src_channel->_CmdBuffer.Size != 1) continue; @@ -2497,10 +2533,11 @@ void ImGui::TableDrawBorders(ImGuiTable* table) inner_drawlist->PushClipRect(table->Bg0ClipRectForDrawCmd.Min, table->Bg0ClipRectForDrawCmd.Max, false); // Draw inner border and resizing feedback + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); const float border_size = TABLE_BORDER_SIZE; const float draw_y1 = table->InnerRect.Min.y; const float draw_y2_body = table->InnerRect.Max.y; - const float draw_y2_head = table->IsUsingHeaders ? ImMin(table->InnerRect.Max.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table->LastFirstRowHeight) : draw_y1; + const float draw_y2_head = table->IsUsingHeaders ? ImMin(table->InnerRect.Max.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table_instance->LastFirstRowHeight) : draw_y1; if (table->Flags & ImGuiTableFlags_BordersInnerV) { for (int order_n = 0; order_n < table->ColumnsCount; order_n++) @@ -2982,7 +3019,7 @@ void ImGui::TableHeader(const char* label) RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, label_pos.y + label_height + g.Style.FramePadding.y), ellipsis_max, ellipsis_max, label, label_end, &label_size); const bool text_clipped = label_size.x > (ellipsis_max - label_pos.x); - if (text_clipped && hovered && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay) + if (text_clipped && hovered && g.ActiveId == 0 && IsItemHovered(ImGuiHoveredFlags_DelayNormal)) SetTooltip("%.*s", (int)(label_end - label), label); // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden @@ -3017,6 +3054,17 @@ void ImGui::TableOpenContextMenu(int column_n) } } +bool ImGui::TableBeginContextMenuPopup(ImGuiTable* table) +{ + if (!table->IsContextPopupOpen || table->InstanceCurrent != table->InstanceInteracted) + return false; + const ImGuiID context_menu_id = ImHashStr("##ContextMenu", 0, table->ID); + if (BeginPopupEx(context_menu_id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings)) + return true; + table->IsContextPopupOpen = false; + return false; +} + // Output context menu into current window (generally a popup) // FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data? void ImGui::TableDrawContextMenu(ImGuiTable* table) @@ -3036,15 +3084,15 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) if (column != NULL) { const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled; - if (MenuItem("Size column to fit###SizeOne", NULL, false, can_resize)) + if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableSizeOne), NULL, false, can_resize)) // "###SizeOne" TableSetColumnWidthAutoSingle(table, column_n); } const char* size_all_desc; if (table->ColumnsEnabledFixedCount == table->ColumnsEnabledCount && (table->Flags & ImGuiTableFlags_SizingMask_) != ImGuiTableFlags_SizingFixedSame) - size_all_desc = "Size all columns to fit###SizeAll"; // All fixed + size_all_desc = LocalizeGetMsg(ImGuiLocKey_TableSizeAllFit); // "###SizeAll" All fixed else - size_all_desc = "Size all columns to default###SizeAll"; // All stretch or mixed + size_all_desc = LocalizeGetMsg(ImGuiLocKey_TableSizeAllDefault); // "###SizeAll" All stretch or mixed if (MenuItem(size_all_desc, NULL)) TableSetColumnWidthAutoAll(table); want_separator = true; @@ -3053,7 +3101,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) // Ordering if (table->Flags & ImGuiTableFlags_Reorderable) { - if (MenuItem("Reset order", NULL, false, !table->IsDefaultDisplayOrder)) + if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetOrder), NULL, false, !table->IsDefaultDisplayOrder)) table->IsResetDisplayOrderRequest = true; want_separator = true; } @@ -3422,9 +3470,8 @@ static void TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandle } } -void ImGui::TableSettingsInstallHandler(ImGuiContext* context) +void ImGui::TableSettingsAddSettingsHandler() { - ImGuiContext& g = *context; ImGuiSettingsHandler ini_handler; ini_handler.TypeName = "Table"; ini_handler.TypeHash = ImHashStr("Table"); @@ -3433,7 +3480,7 @@ void ImGui::TableSettingsInstallHandler(ImGuiContext* context) ini_handler.ReadLineFn = TableSettingsHandler_ReadLine; ini_handler.ApplyAllFn = TableSettingsHandler_ApplyAll; ini_handler.WriteAllFn = TableSettingsHandler_WriteAll; - g.SettingsHandlers.push_back(ini_handler); + AddSettingsHandler(&ini_handler); } //------------------------------------------------------------------------- @@ -3447,7 +3494,7 @@ void ImGui::TableSettingsInstallHandler(ImGuiContext* context) // Remove Table (currently only used by TestEngine) void ImGui::TableRemove(ImGuiTable* table) { - //IMGUI_DEBUG_LOG("TableRemove() id=0x%08X\n", table->ID); + //IMGUI_DEBUG_PRINT("TableRemove() id=0x%08X\n", table->ID); ImGuiContext& g = *GImGui; int table_idx = g.Tables.GetIndex(table); //memset(table->RawData.Data, 0, table->RawData.size_in_bytes()); @@ -3459,7 +3506,7 @@ void ImGui::TableRemove(ImGuiTable* table) // Free up/compact internal Table buffers for when it gets unused void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table) { - //IMGUI_DEBUG_LOG("TableGcCompactTransientBuffers() id=0x%08X\n", table->ID); + //IMGUI_DEBUG_PRINT("TableGcCompactTransientBuffers() id=0x%08X\n", table->ID); ImGuiContext& g = *GImGui; IM_ASSERT(table->MemoryCompacted == false); table->SortSpecs.Specs = NULL; @@ -3503,7 +3550,7 @@ void ImGui::TableGcCompactSettings() // - DebugNodeTable() [Internal] //------------------------------------------------------------------------- -#ifndef IMGUI_DISABLE_METRICS_WINDOW +#ifndef IMGUI_DISABLE_DEBUG_TOOLS static const char* DebugNodeTableGetSizingPolicyDesc(ImGuiTableFlags sizing_policy) { @@ -3531,6 +3578,8 @@ void ImGui::DebugNodeTable(ImGuiTable* table) GetForegroundDrawList()->AddRect(GetItemRectMin(), GetItemRectMax(), IM_COL32(255, 255, 0, 255)); if (!open) return; + if (table->InstanceCurrent > 0) + ImGui::Text("** %d instances of same table! Some data below will refer to last instance.", table->InstanceCurrent + 1); bool clear_settings = SmallButton("Clear settings"); BulletText("OuterRect: Pos: (%.1f,%.1f) Size: (%.1f,%.1f) Sizing: '%s'", table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.GetWidth(), table->OuterRect.GetHeight(), DebugNodeTableGetSizingPolicyDesc(table->Flags)); BulletText("ColumnsGivenWidth: %.1f, ColumnsAutoFitWidth: %.1f, InnerWidth: %.1f%s", table->ColumnsGivenWidth, table->ColumnsAutoFitWidth, table->InnerWidth, table->InnerWidth == 0.0f ? " (auto)" : ""); @@ -3595,7 +3644,7 @@ void ImGui::DebugNodeTableSettings(ImGuiTableSettings* settings) TreePop(); } -#else // #ifndef IMGUI_DISABLE_METRICS_WINDOW +#else // #ifndef IMGUI_DISABLE_DEBUG_TOOLS void ImGui::DebugNodeTable(ImGuiTable*) {} void ImGui::DebugNodeTableSettings(ImGuiTableSettings*) {} @@ -3935,6 +3984,7 @@ void ImGui::NextColumn() { // New row/line: column 0 honor IndentX. window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); + window->DC.IsSameLine = false; columns->LineMinY = columns->LineMaxY; } window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); @@ -3986,8 +4036,7 @@ void ImGui::EndColumns() const ImGuiID column_id = columns->ID + ImGuiID(n); const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); - KeepAliveID(column_id); - if (IsClippedEx(column_hit_rect, column_id)) // FIXME: Can be removed or replaced with a lower-level test + if (!ItemAdd(column_hit_rect, column_id, NULL, ImGuiItemFlags_NoNav)) continue; bool hovered = false, held = false; diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index f199ad40..0a1ea678 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.86 +// dear imgui, v1.89.2 // (widgets code) /* @@ -41,7 +41,6 @@ Index of this file: #include "imgui_internal.h" // System includes -#include // toupper #if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier #include // intptr_t #else @@ -82,6 +81,7 @@ Index of this file: #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#pragma GCC diagnostic ignored "-Wdeprecated-enum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated #endif //------------------------------------------------------------------------- @@ -126,7 +126,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); // For InputTextEx() static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source); static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); -static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); +static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); //------------------------------------------------------------------------- // [SECTION] Widgets: Text, etc. @@ -166,7 +166,21 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); const float wrap_pos_x = window->DC.TextWrapPos; const bool wrap_enabled = (wrap_pos_x >= 0.0f); - if (text_end - text > 2000 && !wrap_enabled) + if (text_end - text <= 2000 || wrap_enabled) + { + // Common case + const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f; + const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); + + ImRect bb(text_pos, text_pos + text_size); + ItemSize(text_size, 0.0f); + if (!ItemAdd(bb, 0)) + return; + + // Render (we don't hide text after ## in this end-user function) + RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width); + } + else { // Long text! // Perform manual coarse clipping to optimize for long multi-line text @@ -239,19 +253,6 @@ void ImGui::TextEx(const char* text, const char* text_end, ImGuiTextFlags flags) ItemSize(text_size, 0.0f); ItemAdd(bb, 0); } - else - { - const float wrap_width = wrap_enabled ? CalcWrapWidthForPos(window->DC.CursorPos, wrap_pos_x) : 0.0f; - const ImVec2 text_size = CalcTextSize(text_begin, text_end, false, wrap_width); - - ImRect bb(text_pos, text_pos + text_size); - ItemSize(text_size, 0.0f); - if (!ItemAdd(bb, 0)) - return; - - // Render (we don't hide text after ## in this end-user function) - RenderTextWrapped(bb.Min, text_begin, text_end, wrap_width); - } } void ImGui::TextUnformatted(const char* text, const char* text_end) @@ -274,9 +275,9 @@ void ImGui::TextV(const char* fmt, va_list args) return; // FIXME-OPT: Handle the %s shortcut? - ImGuiContext& g = *GImGui; - const char* text_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); - TextEx(g.TempBuffer, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); + const char* text, *text_end; + ImFormatStringToTempBufferV(&text, &text_end, fmt, args); + TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); } void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) @@ -357,8 +358,8 @@ void ImGui::LabelTextV(const char* label, const char* fmt, va_list args) const ImGuiStyle& style = g.Style; const float w = CalcItemWidth(); - const char* value_text_begin = &g.TempBuffer[0]; - const char* value_text_end = value_text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + const char* value_text_begin, *value_text_end; + ImFormatStringToTempBufferV(&value_text_begin, &value_text_end, fmt, args); const ImVec2 value_size = CalcTextSize(value_text_begin, value_text_end, false); const ImVec2 label_size = CalcTextSize(label, NULL, true); @@ -393,8 +394,8 @@ void ImGui::BulletTextV(const char* fmt, va_list args) ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; - const char* text_begin = g.TempBuffer; - const char* text_end = text_begin + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); + const char* text_begin, *text_end; + ImFormatStringToTempBufferV(&text_begin, &text_end, fmt, args); const ImVec2 label_size = CalcTextSize(text_begin, text_end, false); const ImVec2 total_size = ImVec2(g.FontSize + (label_size.x > 0.0f ? (label_size.x + style.FramePadding.x * 2) : 0.0f), label_size.y); // Empty text doesn't add padding ImVec2 pos = window->DC.CursorPos; @@ -540,22 +541,28 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool hovered = false; // Mouse handling + const ImGuiID test_owner_id = (flags & ImGuiButtonFlags_NoTestKeyOwner) ? ImGuiKeyOwner_Any : id; if (hovered) { + // Poll mouse buttons + // - 'mouse_button_clicked' is generally carried into ActiveIdMouseButton when setting ActiveId. + // - Technically we only need some values in one code path, but since this is gated by hovered test this is fine. + int mouse_button_clicked = -1; + int mouse_button_released = -1; + for (int button = 0; button < 3; button++) + if (flags & (ImGuiButtonFlags_MouseButtonLeft << button)) // Handle ImGuiButtonFlags_MouseButtonRight and ImGuiButtonFlags_MouseButtonMiddle here. + { + if (IsMouseClicked(button, test_owner_id) && mouse_button_clicked == -1) { mouse_button_clicked = button; } + if (IsMouseReleased(button, test_owner_id) && mouse_button_released == -1) { mouse_button_released = button; } + } + + // Process initial action if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) { - // Poll buttons - int mouse_button_clicked = -1; - int mouse_button_released = -1; - if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseClicked[0]) { mouse_button_clicked = 0; } - else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseClicked[1]) { mouse_button_clicked = 1; } - else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseClicked[2]) { mouse_button_clicked = 2; } - if ((flags & ImGuiButtonFlags_MouseButtonLeft) && g.IO.MouseReleased[0]) { mouse_button_released = 0; } - else if ((flags & ImGuiButtonFlags_MouseButtonRight) && g.IO.MouseReleased[1]) { mouse_button_released = 1; } - else if ((flags & ImGuiButtonFlags_MouseButtonMiddle) && g.IO.MouseReleased[2]) { mouse_button_released = 2; } - if (mouse_button_clicked != -1 && g.ActiveId != id) { + if (!(flags & ImGuiButtonFlags_NoSetKeyOwner)) + SetKeyOwner(MouseButtonToKey(mouse_button_clicked), id); if (flags & (ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnClickReleaseAnywhere)) { SetActiveID(id, window); @@ -577,21 +584,23 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool FocusWindow(window); } } - if ((flags & ImGuiButtonFlags_PressedOnRelease) && mouse_button_released != -1) + if (flags & ImGuiButtonFlags_PressedOnRelease) { - // Repeat mode trumps on release behavior - const bool has_repeated_at_least_once = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; - if (!has_repeated_at_least_once) - pressed = true; - if (!(flags & ImGuiButtonFlags_NoNavFocus)) - SetFocusID(id, window); - ClearActiveID(); + if (mouse_button_released != -1) + { + const bool has_repeated_at_least_once = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; // Repeat mode trumps on release behavior + if (!has_repeated_at_least_once) + pressed = true; + if (!(flags & ImGuiButtonFlags_NoNavFocus)) + SetFocusID(id, window); + ClearActiveID(); + } } // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. if (g.ActiveId == id && (flags & ImGuiButtonFlags_Repeat)) - if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, true)) + if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, test_owner_id, ImGuiInputFlags_Repeat)) pressed = true; } @@ -607,7 +616,15 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (g.NavActivateDownId == id) { bool nav_activated_by_code = (g.NavActivateId == id); - bool nav_activated_by_inputs = IsNavInputTest(ImGuiNavInput_Activate, (flags & ImGuiButtonFlags_Repeat) ? ImGuiInputReadMode_Repeat : ImGuiInputReadMode_Pressed); + bool nav_activated_by_inputs = (g.NavActivatePressedId == id); + if (!nav_activated_by_inputs && (flags & ImGuiButtonFlags_Repeat)) + { + // Avoid pressing both keys from triggering double amount of repeat events + const ImGuiKeyData* key1 = GetKeyData(ImGuiKey_Space); + const ImGuiKeyData* key2 = GetKeyData(ImGuiKey_NavGamepadActivate); + const float t1 = ImMax(key1->DownDuration, key2->DownDuration); + nav_activated_by_inputs = CalcTypematicRepeatAmount(t1 - g.IO.DeltaTime, t1, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; + } if (nav_activated_by_code || nav_activated_by_inputs) { // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. @@ -629,8 +646,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; const int mouse_button = g.ActiveIdMouseButton; - IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); - if (g.IO.MouseDown[mouse_button]) + if (IsMouseDown(mouse_button, test_owner_id)) { held = true; } @@ -643,7 +659,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Report as pressed when releasing the mouse (this is the most common path) bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseReleased[mouse_button] && g.IO.MouseClickedLastCount[mouse_button] == 2; bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps - if (!is_double_click_release && !is_repeating_already) + bool is_button_avail_or_owned = TestKeyOwner(MouseButtonToKey(mouse_button), test_owner_id); + if (!is_double_click_release && !is_repeating_already && is_button_avail_or_owned) pressed = true; } ClearActiveID(); @@ -731,6 +748,7 @@ bool ImGui::SmallButton(const char* label) // Then you can keep 'str_id' empty or the same for all your buttons (instead of creating a string based on a non-string id) bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg, ImGuiButtonFlags flags) { + ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; @@ -748,16 +766,17 @@ bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg, ImGuiBut bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, str_id, g.LastItemData.StatusFlags); return pressed; } bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiButtonFlags flags) { + ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - ImGuiContext& g = *GImGui; const ImGuiID id = window->GetID(str_id); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); const float default_size = GetFrameHeight(); @@ -778,6 +797,7 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding); RenderArrow(window->DrawList, bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), text_col, dir); + IMGUI_TEST_ENGINE_ITEM_INFO(id, str_id, g.LastItemData.StatusFlags); return pressed; } @@ -839,9 +859,8 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) // Render ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); - ImVec2 center = bb.GetCenter(); if (hovered || held) - window->DrawList->AddCircleFilled(center/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col, 12); + window->DrawList->AddCircleFilled(bb.GetCenter()/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col, 12); RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold @@ -853,7 +872,7 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) ImGuiID ImGui::GetWindowScrollbarID(ImGuiWindow* window, ImGuiAxis axis) { - return window->GetIDNoKeepAlive(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); + return window->GetID(axis == ImGuiAxis_X ? "#SCROLLX" : "#SCROLLY"); } // Return scrollbar rectangle, must only be called for corresponding axis if window->ScrollbarX/Y is set. @@ -874,9 +893,7 @@ void ImGui::Scrollbar(ImGuiAxis axis) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - const ImGuiID id = GetWindowScrollbarID(window, axis); - KeepAliveID(id); // Calculate scrollbar bounding box ImRect bb = GetWindowScrollbarRect(window, axis); @@ -945,6 +962,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). bool held = false; bool hovered = false; + ItemAdd(bb_frame, id, NULL, ImGuiItemFlags_NoNav); ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); const ImS64 scroll_max = ImMax((ImS64)1, size_contents_v - size_avail_v); @@ -1024,14 +1042,15 @@ void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& // ImageButton() is flawed as 'id' is always derived from 'texture_id' (see #2464 #1390) // We provide this internal helper to write your own variant while we figure out how to redesign the public ImageButton() API. -bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec2& padding, const ImVec4& bg_col, const ImVec4& tint_col) +bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2); + const ImVec2 padding = g.Style.FramePadding; + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2.0f); ItemSize(bb); if (!ItemAdd(bb, id)) return false; @@ -1050,9 +1069,21 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size return pressed; } -// frame_padding < 0: uses FramePadding from style (default) -// frame_padding = 0: no framing -// frame_padding > 0: set framing size +bool ImGui::ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return false; + + return ImageButtonEx(window->GetID(str_id), user_texture_id, size, uv0, uv1, bg_col, tint_col); +} + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +// Legacy API obsoleted in 1.89. Two differences with new ImageButton() +// - new ImageButton() requires an explicit 'const char* str_id' Old ImageButton() used opaque imTextureId (created issue with: multiple buttons with same image, transient texture id values, opaque computation of ID) +// - new ImageButton() always use style.FramePadding Old ImageButton() had an override argument. +// If you need to change padding with new ImageButton() you can use PushStyleVar(ImGuiStyleVar_FramePadding, value), consistent with other Button functions. bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) { ImGuiContext& g = *GImGui; @@ -1065,9 +1096,14 @@ bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const I const ImGuiID id = window->GetID("#image"); PopID(); - const ImVec2 padding = (frame_padding >= 0) ? ImVec2((float)frame_padding, (float)frame_padding) : g.Style.FramePadding; - return ImageButtonEx(id, user_texture_id, size, uv0, uv1, padding, bg_col, tint_col); + if (frame_padding >= 0) + PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2((float)frame_padding, (float)frame_padding)); + bool ret = ImageButtonEx(id, user_texture_id, size, uv0, uv1, bg_col, tint_col); + if (frame_padding >= 0) + PopStyleVar(); + return ret; } +#endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS bool ImGui::Checkbox(const char* label, bool* v) { @@ -1282,7 +1318,7 @@ void ImGui::Bullet() ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; - const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + g.Style.FramePadding.y * 2), g.FontSize); + const float line_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y * 2), g.FontSize); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(g.FontSize, line_height)); ItemSize(bb); if (!ItemAdd(bb, 0)) @@ -1338,6 +1374,7 @@ void ImGui::NewLine() ImGuiContext& g = *GImGui; const ImGuiLayoutType backup_layout_type = window->DC.LayoutType; window->DC.LayoutType = ImGuiLayoutType_Vertical; + window->DC.IsSameLine = false; if (window->DC.CurrLineSize.y > 0.0f) // In the event that we are on a line with items that is smaller that FontSize high, we will preserve its height. ItemSize(ImVec2(0, 0)); else @@ -1440,16 +1477,12 @@ void ImGui::Separator() } // Using 'hover_visibility_delay' allows us to hide the highlight and mouse cursor for a short time, which can be convenient to reduce visual noise. -bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay) +bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay, ImU32 bg_col) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - const ImGuiItemFlags item_flags_backup = g.CurrentItemFlags; - g.CurrentItemFlags |= ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus; - bool item_add = ItemAdd(bb, id); - g.CurrentItemFlags = item_flags_backup; - if (!item_add) + if (!ItemAdd(bb, id, NULL, ImGuiItemFlags_NoNav)) return false; bool hovered, held; @@ -1492,7 +1525,9 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float } } - // Render + // Render at new position + if (bg_col & IM_COL32_A_MASK) + window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, bg_col, 0.0f); const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : (hovered && g.HoveredIdTimer >= hover_visibility_delay) ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); window->DrawList->AddRectFilled(bb_render.Min, bb_render.Max, col, 0.0f); @@ -1533,7 +1568,7 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc width_excess -= width_to_remove_per_item * count_same_width; } - // Round width and redistribute remainder left-to-right (could make it an option of the function?) + // Round width and redistribute remainder // Ensure that e.g. the right-most tab of a shrunk tab-bar always reaches exactly at the same distance from the right-most edge of the tab bar separator. width_excess = 0.0f; for (int n = 0; n < count; n++) @@ -1542,10 +1577,13 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc width_excess += items[n].Width - width_rounded; items[n].Width = width_rounded; } - if (width_excess > 0.0f) - for (int n = 0; n < count; n++) - if (items[n].Index < (int)(width_excess + 0.01f)) - items[n].Width += 1.0f; + while (width_excess > 0.0f) + for (int n = 0; n < count && width_excess > 0.0f; n++) + { + float width_to_add = ImMin(items[n].InitialWidth - items[n].Width, 1.0f); + items[n].Width += width_to_add; + width_excess -= width_to_add; + } } //------------------------------------------------------------------------- @@ -1715,7 +1753,7 @@ bool ImGui::BeginComboPreview() ImGuiWindow* window = g.CurrentWindow; ImGuiComboPreviewData* preview_data = &g.ComboPreviewData; - if (window->SkipItems || !window->ClipRect.Overlaps(g.LastItemData.Rect)) // FIXME: Because we don't have a ImGuiItemStatusFlags_Visible flag to test last ItemAdd() result + if (window->SkipItems || !(g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible)) return false; IM_ASSERT(g.LastItemData.Rect.Min.x == preview_data->PreviewRect.Min.x && g.LastItemData.Rect.Min.y == preview_data->PreviewRect.Min.y); // Didn't call after BeginCombo/EndCombo block or forgot to pass ImGuiComboFlags_CustomPreview flag? if (!window->ClipRect.Contains(preview_data->PreviewRect)) // Narrower test (optional) @@ -1730,6 +1768,7 @@ bool ImGui::BeginComboPreview() window->DC.CursorPos = preview_data->PreviewRect.Min + g.Style.FramePadding; window->DC.CursorMaxPos = window->DC.CursorPos; window->DC.LayoutType = ImGuiLayoutType_Horizontal; + window->DC.IsSameLine = false; PushClipRect(preview_data->PreviewRect.Min, preview_data->PreviewRect.Max, true); return true; @@ -1755,6 +1794,7 @@ void ImGui::EndComboPreview() window->DC.CursorPosPrevLine = preview_data->BackupCursorPosPrevLine; window->DC.PrevLineTextBaseOffset = preview_data->BackupPrevLineTextBaseOffset; window->DC.LayoutType = preview_data->BackupLayout; + window->DC.IsSameLine = false; preview_data->PreviewRect = ImRect(); } @@ -1857,11 +1897,11 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa //------------------------------------------------------------------------- // [SECTION] Data Type and Data Formatting Helpers [Internal] //------------------------------------------------------------------------- -// - PatchFormatStringFloatToInt() // - DataTypeGetInfo() // - DataTypeFormatString() // - DataTypeApplyOp() // - DataTypeApplyOpFromText() +// - DataTypeCompare() // - DataTypeClamp() // - GetMinimumStepAtDecimalPrecision // - RoundScalarWithFormat<>() @@ -1887,30 +1927,6 @@ static const ImGuiDataTypeInfo GDataTypeInfo[] = }; IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); -// FIXME-LEGACY: Prior to 1.61 our DragInt() function internally used floats and because of this the compile-time default value for format was "%.0f". -// Even though we changed the compile-time default, we expect users to have carried %f around, which would break the display of DragInt() calls. -// To honor backward compatibility we are rewriting the format string, unless IMGUI_DISABLE_OBSOLETE_FUNCTIONS is enabled. What could possibly go wrong?! -static const char* PatchFormatStringFloatToInt(const char* fmt) -{ - if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '0' && fmt[3] == 'f' && fmt[4] == 0) // Fast legacy path for "%.0f" which is expected to be the most common case. - return "%d"; - const char* fmt_start = ImParseFormatFindStart(fmt); // Find % (if any, and ignore %%) - const char* fmt_end = ImParseFormatFindEnd(fmt_start); // Find end of format specifier, which itself is an exercise of confidence/recklessness (because snprintf is dependent on libc or user). - if (fmt_end > fmt_start && fmt_end[-1] == 'f') - { -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - if (fmt_start == fmt && fmt_end[0] == 0) - return "%d"; - ImGuiContext& g = *GImGui; - ImFormatString(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), "%.*s%%d%s", (int)(fmt_start - fmt), fmt, fmt_end); // Honor leading and trailing decorations, but lose alignment/precision. - return g.TempBuffer; -#else - IM_ASSERT(0 && "DragInt(): Invalid format string!"); // Old versions used a default parameter of "%.0f", please replace with e.g. "%d" -#endif - } - return fmt; -} - const ImGuiDataTypeInfo* ImGui::DataTypeGetInfo(ImGuiDataType data_type) { IM_ASSERT(data_type >= 0 && data_type < ImGuiDataType_COUNT); @@ -1992,24 +2008,10 @@ void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const // User can input math operators (e.g. +100) to edit a numerical values. // NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess.. -bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format) +bool ImGui::DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format) { while (ImCharIsBlankA(*buf)) buf++; - - // We don't support '-' op because it would conflict with inputing negative value. - // Instead you can use +-100 to subtract from an existing value - char op = buf[0]; - if (op == '+' || op == '*' || op == '/') - { - buf++; - while (ImCharIsBlankA(*buf)) - buf++; - } - else - { - op = 0; - } if (!buf[0]) return false; @@ -2018,66 +2020,20 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b ImGuiDataTypeTempStorage data_backup; memcpy(&data_backup, p_data, type_info->Size); - if (format == NULL) + // Sanitize format + // For float/double we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in, so force them into %f and %lf + char format_sanitized[32]; + if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) format = type_info->ScanFmt; - - // FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point.. - int arg1i = 0; - if (data_type == ImGuiDataType_S32) - { - int* v = (int*)p_data; - int arg0i = *v; - float arg1f = 0.0f; - if (op && sscanf(initial_value_buf, format, &arg0i) < 1) - return false; - // Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision - if (op == '+') { if (sscanf(buf, "%d", &arg1i)) *v = (int)(arg0i + arg1i); } // Add (use "+-" to subtract) - else if (op == '*') { if (sscanf(buf, "%f", &arg1f)) *v = (int)(arg0i * arg1f); } // Multiply - else if (op == '/') { if (sscanf(buf, "%f", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); } // Divide - else { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; } // Assign constant - } - else if (data_type == ImGuiDataType_Float) - { - // For floats we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in - format = "%f"; - float* v = (float*)p_data; - float arg0f = *v, arg1f = 0.0f; - if (op && sscanf(initial_value_buf, format, &arg0f) < 1) - return false; - if (sscanf(buf, format, &arg1f) < 1) - return false; - if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) - else if (op == '*') { *v = arg0f * arg1f; } // Multiply - else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide - else { *v = arg1f; } // Assign constant - } - else if (data_type == ImGuiDataType_Double) - { - format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis - double* v = (double*)p_data; - double arg0f = *v, arg1f = 0.0; - if (op && sscanf(initial_value_buf, format, &arg0f) < 1) - return false; - if (sscanf(buf, format, &arg1f) < 1) - return false; - if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract) - else if (op == '*') { *v = arg0f * arg1f; } // Multiply - else if (op == '/') { if (arg1f != 0.0f) *v = arg0f / arg1f; } // Divide - else { *v = arg1f; } // Assign constant - } - else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64) - { - // All other types assign constant - // We don't bother handling support for legacy operators since they are a little too crappy. Instead we will later implement a proper expression evaluator in the future. - if (sscanf(buf, format, p_data) < 1) - return false; - } else + format = ImParseFormatSanitizeForScanning(format, format_sanitized, IM_ARRAYSIZE(format_sanitized)); + + // Small types need a 32-bit buffer to receive the result from scanf() + int v32 = 0; + if (sscanf(buf, format, type_info->Size >= 4 ? p_data : &v32) < 1) + return false; + if (type_info->Size < 4) { - // Small types need a 32-bit buffer to receive the result from scanf() - int v32; - if (sscanf(buf, format, &v32) < 1) - return false; if (data_type == ImGuiDataType_S8) *(ImS8*)p_data = (ImS8)ImClamp(v32, (int)IM_S8_MIN, (int)IM_S8_MAX); else if (data_type == ImGuiDataType_U8) @@ -2159,45 +2115,17 @@ static float GetMinimumStepAtDecimalPrecision(int decimal_precision) } template -static const char* ImAtoi(const char* src, TYPE* output) -{ - int negative = 0; - if (*src == '-') { negative = 1; src++; } - if (*src == '+') { src++; } - TYPE v = 0; - while (*src >= '0' && *src <= '9') - v = (v * 10) + (*src++ - '0'); - *output = negative ? -v : v; - return src; -} - -// Sanitize format -// - Zero terminate so extra characters after format (e.g. "%f123") don't confuse atof/atoi -// - stb_sprintf.h supports several new modifiers which format numbers in a way that also makes them incompatible atof/atoi. -static void SanitizeFormatString(const char* fmt, char* fmt_out, size_t fmt_out_size) -{ - IM_UNUSED(fmt_out_size); - const char* fmt_end = ImParseFormatFindEnd(fmt); - IM_ASSERT((size_t)(fmt_end - fmt + 1) < fmt_out_size); // Format is too long, let us know if this happens to you! - while (fmt < fmt_end) - { - char c = *(fmt++); - if (c != '\'' && c != '$' && c != '_') // Custom flags provided by stb_sprintf.h. POSIX 2008 also supports '. - *(fmt_out++) = c; - } - *fmt_out = 0; // Zero-terminate -} - -template TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, TYPE v) { + IM_UNUSED(data_type); + IM_ASSERT(data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double); const char* fmt_start = ImParseFormatFindStart(format); if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string return v; // Sanitize format char fmt_sanitized[32]; - SanitizeFormatString(fmt_start, fmt_sanitized, IM_ARRAYSIZE(fmt_sanitized)); + ImParseFormatSanitizeForPrinting(fmt_start, fmt_sanitized, IM_ARRAYSIZE(fmt_sanitized)); fmt_start = fmt_sanitized; // Format value with our rounding, and read back @@ -2206,10 +2134,8 @@ TYPE ImGui::RoundScalarWithFormatT(const char* format, ImGuiDataType data_type, const char* p = v_str; while (*p == ' ') p++; - if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) - v = (TYPE)ImAtof(p); - else - ImAtoi(p, (SIGNEDTYPE*)&v); + v = (TYPE)ImAtof(p); + return v; } @@ -2259,7 +2185,10 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const else if (g.ActiveIdSource == ImGuiInputSource_Nav) { const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; - adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; + const bool tweak_slow = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakSlow : ImGuiKey_NavKeyboardTweakSlow); + const bool tweak_fast = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakFast : ImGuiKey_NavKeyboardTweakFast); + const float tweak_factor = tweak_slow ? 1.0f / 1.0f : tweak_fast ? 10.0f : 1.0f; + adjust_delta = GetNavTweakPressedAmount(axis) * tweak_factor; v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); } adjust_delta *= v_speed; @@ -2313,8 +2242,8 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const } // Round to user desired precision based on format string - if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) - v_cur = RoundScalarWithFormatT(format, data_type, v_cur); + if (is_floating_point && !(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_cur = RoundScalarWithFormatT(format, data_type, v_cur); // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. g.DragCurrentAccumDirty = false; @@ -2357,6 +2286,7 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v ImGuiContext& g = *GImGui; if (g.ActiveId == id) { + // Those are the things we can do easily outside the DragBehaviorT<> template, saves code generation. if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0]) ClearActiveID(); else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) @@ -2410,29 +2340,23 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) - format = PatchFormatStringFloatToInt(format); - // Tabbing or CTRL-clicking on Drag turns it into an InputText const bool hovered = ItemHoverable(frame_bb, id); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { + // Tabbing or CTRL-clicking on Drag turns it into an InputText const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; - const bool clicked = (hovered && g.IO.MouseClicked[0]); - const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2); - if (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id || g.NavActivateInputId == id) - { - SetActiveID(id, window); - SetFocusID(id, window); - FocusWindow(window); - g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (temp_input_allowed) - if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavActivateInputId == id) - temp_input_is_active = true; - } + const bool clicked = hovered && IsMouseClicked(0, id); + const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2 && TestKeyOwner(ImGuiKey_MouseLeft, id)); + const bool make_active = (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id || g.NavActivateInputId == id); + if (make_active && (clicked || double_clicked)) + SetKeyOwner(ImGuiKey_MouseLeft, id); + if (make_active && temp_input_allowed) + if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavActivateInputId == id) + temp_input_is_active = true; - // Experimental: simple click (without moving) turns Drag into an InputText + // (Optional) simple click (without moving) turns Drag into an InputText if (g.IO.ConfigDragClickToInputText && temp_input_allowed && !temp_input_is_active) if (g.ActiveId == id && hovered && g.IO.MouseReleased[0] && !IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR)) { @@ -2440,6 +2364,14 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, g.NavActivateFlags = ImGuiActivateFlags_PreferInput; temp_input_is_active = true; } + + if (make_active && !temp_input_is_active) + { + SetActiveID(id, window); + SetFocusID(id, window); + FocusWindow(window); + g.ActiveIdUsingNavDirMask = (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); + } } if (temp_input_is_active) @@ -2615,35 +2547,6 @@ bool ImGui::DragIntRange2(const char* label, int* v_current_min, int* v_current_ return value_changed; } -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - -// Obsolete versions with power parameter. See https://github.com/ocornut/imgui/issues/3361 for details. -bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, float v_speed, const void* p_min, const void* p_max, const char* format, float power) -{ - ImGuiSliderFlags drag_flags = ImGuiSliderFlags_None; - if (power != 1.0f) - { - IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); - IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds - drag_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths - } - return DragScalar(label, data_type, p_data, v_speed, p_min, p_max, format, drag_flags); -} - -bool ImGui::DragScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, float v_speed, const void* p_min, const void* p_max, const char* format, float power) -{ - ImGuiSliderFlags drag_flags = ImGuiSliderFlags_None; - if (power != 1.0f) - { - IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); - IM_ASSERT(p_min != NULL && p_max != NULL); // When using a power curve the drag needs to have known bounds - drag_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths - } - return DragScalarN(label, data_type, p_data, components, v_speed, p_min, p_max, format, drag_flags); -} - -#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS - //------------------------------------------------------------------------- // [SECTION] Widgets: SliderScalar, SliderFloat, SliderInt, etc. //------------------------------------------------------------------------- @@ -2694,7 +2597,6 @@ float ImGui::ScaleRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, T v_max_fudged = -logarithmic_zero_epsilon; float result; - if (v_clamped <= v_min_fudged) result = 0.0f; // Workaround for values that are in-range but below our fudge else if (v_clamped >= v_max_fudged) @@ -2718,91 +2620,81 @@ float ImGui::ScaleRatioFromValueT(ImGuiDataType data_type, TYPE v, TYPE v_min, T return flipped ? (1.0f - result) : result; } - - // Linear slider - return (float)((FLOATTYPE)(SIGNEDTYPE)(v_clamped - v_min) / (FLOATTYPE)(SIGNEDTYPE)(v_max - v_min)); + else + { + // Linear slider + return (float)((FLOATTYPE)(SIGNEDTYPE)(v_clamped - v_min) / (FLOATTYPE)(SIGNEDTYPE)(v_max - v_min)); + } } // Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT) template TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize) { - if (v_min == v_max) + // We special-case the extents because otherwise our logarithmic fudging can lead to "mathematically correct" + // but non-intuitive behaviors like a fully-left slider not actually reaching the minimum value. Also generally simpler. + if (t <= 0.0f || v_min == v_max) return v_min; - const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + if (t >= 1.0f) + return v_max; - TYPE result; + TYPE result = (TYPE)0; if (is_logarithmic) { - // We special-case the extents because otherwise our fudging can lead to "mathematically correct" but non-intuitive behaviors like a fully-left slider not actually reaching the minimum value - if (t <= 0.0f) - result = v_min; - else if (t >= 1.0f) - result = v_max; - else - { - bool flipped = v_max < v_min; // Check if range is "backwards" - - // Fudge min/max to avoid getting silly results close to zero - FLOATTYPE v_min_fudged = (ImAbs((FLOATTYPE)v_min) < logarithmic_zero_epsilon) ? ((v_min < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_min; - FLOATTYPE v_max_fudged = (ImAbs((FLOATTYPE)v_max) < logarithmic_zero_epsilon) ? ((v_max < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_max; + // Fudge min/max to avoid getting silly results close to zero + FLOATTYPE v_min_fudged = (ImAbs((FLOATTYPE)v_min) < logarithmic_zero_epsilon) ? ((v_min < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_min; + FLOATTYPE v_max_fudged = (ImAbs((FLOATTYPE)v_max) < logarithmic_zero_epsilon) ? ((v_max < 0.0f) ? -logarithmic_zero_epsilon : logarithmic_zero_epsilon) : (FLOATTYPE)v_max; - if (flipped) - ImSwap(v_min_fudged, v_max_fudged); + const bool flipped = v_max < v_min; // Check if range is "backwards" + if (flipped) + ImSwap(v_min_fudged, v_max_fudged); - // Awkward special case - we need ranges of the form (-100 .. 0) to convert to (-100 .. -epsilon), not (-100 .. epsilon) - if ((v_max == 0.0f) && (v_min < 0.0f)) - v_max_fudged = -logarithmic_zero_epsilon; + // Awkward special case - we need ranges of the form (-100 .. 0) to convert to (-100 .. -epsilon), not (-100 .. epsilon) + if ((v_max == 0.0f) && (v_min < 0.0f)) + v_max_fudged = -logarithmic_zero_epsilon; - float t_with_flip = flipped ? (1.0f - t) : t; // t, but flipped if necessary to account for us flipping the range + float t_with_flip = flipped ? (1.0f - t) : t; // t, but flipped if necessary to account for us flipping the range - if ((v_min * v_max) < 0.0f) // Range crosses zero, so we have to do this in two parts - { - float zero_point_center = (-(float)ImMin(v_min, v_max)) / ImAbs((float)v_max - (float)v_min); // The zero point in parametric space - float zero_point_snap_L = zero_point_center - zero_deadzone_halfsize; - float zero_point_snap_R = zero_point_center + zero_deadzone_halfsize; - if (t_with_flip >= zero_point_snap_L && t_with_flip <= zero_point_snap_R) - result = (TYPE)0.0f; // Special case to make getting exactly zero possible (the epsilon prevents it otherwise) - else if (t_with_flip < zero_point_center) - result = (TYPE)-(logarithmic_zero_epsilon * ImPow(-v_min_fudged / logarithmic_zero_epsilon, (FLOATTYPE)(1.0f - (t_with_flip / zero_point_snap_L)))); - else - result = (TYPE)(logarithmic_zero_epsilon * ImPow(v_max_fudged / logarithmic_zero_epsilon, (FLOATTYPE)((t_with_flip - zero_point_snap_R) / (1.0f - zero_point_snap_R)))); - } - else if ((v_min < 0.0f) || (v_max < 0.0f)) // Entirely negative slider - result = (TYPE)-(-v_max_fudged * ImPow(-v_min_fudged / -v_max_fudged, (FLOATTYPE)(1.0f - t_with_flip))); + if ((v_min * v_max) < 0.0f) // Range crosses zero, so we have to do this in two parts + { + float zero_point_center = (-(float)ImMin(v_min, v_max)) / ImAbs((float)v_max - (float)v_min); // The zero point in parametric space + float zero_point_snap_L = zero_point_center - zero_deadzone_halfsize; + float zero_point_snap_R = zero_point_center + zero_deadzone_halfsize; + if (t_with_flip >= zero_point_snap_L && t_with_flip <= zero_point_snap_R) + result = (TYPE)0.0f; // Special case to make getting exactly zero possible (the epsilon prevents it otherwise) + else if (t_with_flip < zero_point_center) + result = (TYPE)-(logarithmic_zero_epsilon * ImPow(-v_min_fudged / logarithmic_zero_epsilon, (FLOATTYPE)(1.0f - (t_with_flip / zero_point_snap_L)))); else - result = (TYPE)(v_min_fudged * ImPow(v_max_fudged / v_min_fudged, (FLOATTYPE)t_with_flip)); + result = (TYPE)(logarithmic_zero_epsilon * ImPow(v_max_fudged / logarithmic_zero_epsilon, (FLOATTYPE)((t_with_flip - zero_point_snap_R) / (1.0f - zero_point_snap_R)))); } + else if ((v_min < 0.0f) || (v_max < 0.0f)) // Entirely negative slider + result = (TYPE)-(-v_max_fudged * ImPow(-v_min_fudged / -v_max_fudged, (FLOATTYPE)(1.0f - t_with_flip))); + else + result = (TYPE)(v_min_fudged * ImPow(v_max_fudged / v_min_fudged, (FLOATTYPE)t_with_flip)); } else { // Linear slider + const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); if (is_floating_point) { result = ImLerp(v_min, v_max, t); } - else + else if (t < 1.0) { // - For integer values we want the clicking position to match the grab box so we round above // This code is carefully tuned to work with large values (e.g. high ranges of U64) while preserving this property.. // - Not doing a *1.0 multiply at the end of a range as it tends to be lossy. While absolute aiming at a large s64/u64 // range is going to be imprecise anyway, with this check we at least make the edge values matches expected limits. - if (t < 1.0) - { - FLOATTYPE v_new_off_f = (SIGNEDTYPE)(v_max - v_min) * t; - result = (TYPE)((SIGNEDTYPE)v_min + (SIGNEDTYPE)(v_new_off_f + (FLOATTYPE)(v_min > v_max ? -0.5 : 0.5))); - } - else - { - result = v_max; - } + FLOATTYPE v_new_off_f = (SIGNEDTYPE)(v_max - v_min) * t; + result = (TYPE)((SIGNEDTYPE)v_min + (SIGNEDTYPE)(v_new_off_f + (FLOATTYPE)(v_min > v_max ? -0.5 : 0.5))); } } return result; } -// FIXME: Move more of the code into SliderBehavior() +// FIXME: Try to move more of the code into shared SliderBehavior() template bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb) { @@ -2812,13 +2704,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); + const SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); - const float grab_padding = 2.0f; + // Calculate bounds + const float grab_padding = 2.0f; // FIXME: Should be part of style. const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; float grab_sz = style.GrabMinSize; - SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); - if (!is_floating_point && v_range >= 0) // v_range < 0 may happen on integer overflows - grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit + if (!is_floating_point && v_range >= 0) // v_range < 0 may happen on integer overflows + grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit grab_sz = ImMin(grab_sz, slider_sz); const float slider_usable_sz = slider_sz - grab_sz; const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f; @@ -2849,7 +2742,17 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ else { const float mouse_abs_pos = g.IO.MousePos[axis]; - clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f; + if (g.ActiveIdIsJustActivated) + { + float grab_t = ScaleRatioFromValueT(data_type, *v, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); + if (axis == ImGuiAxis_Y) + grab_t = 1.0f - grab_t; + const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t); + const bool clicked_around_grab = (mouse_abs_pos >= grab_pos - grab_sz * 0.5f - 1.0f) && (mouse_abs_pos <= grab_pos + grab_sz * 0.5f + 1.0f); // No harm being extra generous here. + g.SliderGrabClickOffset = (clicked_around_grab && is_floating_point) ? mouse_abs_pos - grab_pos : 0.0f; + } + if (slider_usable_sz > 0.0f) + clicked_t = ImSaturate((mouse_abs_pos - g.SliderGrabClickOffset - slider_usable_pos_min) / slider_usable_sz); if (axis == ImGuiAxis_Y) clicked_t = 1.0f - clicked_t; set_new_value = true; @@ -2863,25 +2766,26 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ g.SliderCurrentAccumDirty = false; } - const ImVec2 input_delta2 = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 0.0f, 0.0f); - float input_delta = (axis == ImGuiAxis_X) ? input_delta2.x : -input_delta2.y; + float input_delta = (axis == ImGuiAxis_X) ? GetNavTweakPressedAmount(axis) : -GetNavTweakPressedAmount(axis); if (input_delta != 0.0f) { + const bool tweak_slow = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakSlow : ImGuiKey_NavKeyboardTweakSlow); + const bool tweak_fast = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakFast : ImGuiKey_NavKeyboardTweakFast); const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; if (decimal_precision > 0) { input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds - if (IsNavInputDown(ImGuiNavInput_TweakSlow)) + if (tweak_slow) input_delta /= 10.0f; } else { - if ((v_range >= -100.0f && v_range <= 100.0f) || IsNavInputDown(ImGuiNavInput_TweakSlow)) + if ((v_range >= -100.0f && v_range <= 100.0f) || tweak_slow) input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps else input_delta /= 100.0f; } - if (IsNavInputDown(ImGuiNavInput_TweakFast)) + if (tweak_fast) input_delta *= 10.0f; g.SliderCurrentAccum += input_delta; @@ -2910,8 +2814,8 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ // Calculate what our "new" clicked_t will be, and thus how far we actually moved the slider, and subtract this from the accumulator TYPE v_new = ScaleValueFromRatioT(data_type, clicked_t, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); - if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) - v_new = RoundScalarWithFormatT(format, data_type, v_new); + if (is_floating_point && !(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_new = RoundScalarWithFormatT(format, data_type, v_new); float new_clicked_t = ScaleRatioFromValueT(data_type, v_new, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); if (delta > 0) @@ -2929,8 +2833,8 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ TYPE v_new = ScaleValueFromRatioT(data_type, clicked_t, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); // Round to user desired precision based on format string - if (!(flags & ImGuiSliderFlags_NoRoundToFormat)) - v_new = RoundScalarWithFormatT(format, data_type, v_new); + if (is_floating_point && !(flags & ImGuiSliderFlags_NoRoundToFormat)) + v_new = RoundScalarWithFormatT(format, data_type, v_new); // Apply result if (*v != v_new) @@ -2969,6 +2873,7 @@ bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flag! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + // Those are the things we can do easily outside the SliderBehaviorT<> template, saves code generation. ImGuiContext& g = *GImGui; if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) return false; @@ -3028,24 +2933,27 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) - format = PatchFormatStringFloatToInt(format); - // Tabbing or CTRL-clicking on Slider turns it into an input box const bool hovered = ItemHoverable(frame_bb, id); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { + // Tabbing or CTRL-clicking on Slider turns it into an input box const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; - const bool clicked = (hovered && g.IO.MouseClicked[0]); - if (input_requested_by_tabbing || clicked || g.NavActivateId == id || g.NavActivateInputId == id) + const bool clicked = hovered && IsMouseClicked(0, id); + const bool make_active = (input_requested_by_tabbing || clicked || g.NavActivateId == id || g.NavActivateInputId == id); + if (make_active && clicked) + SetKeyOwner(ImGuiKey_MouseLeft, id); + if (make_active && temp_input_allowed) + if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || g.NavActivateInputId == id) + temp_input_is_active = true; + + if (make_active && !temp_input_is_active) { SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); - if (temp_input_allowed && (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || g.NavActivateInputId == id)) - temp_input_is_active = true; } } @@ -3192,12 +3100,13 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d // Default format string when passing NULL if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.) - format = PatchFormatStringFloatToInt(format); const bool hovered = ItemHoverable(frame_bb, id); - if ((hovered && g.IO.MouseClicked[0]) || g.NavActivateId == id || g.NavActivateInputId == id) + const bool clicked = hovered && IsMouseClicked(0, id); + if (clicked || g.NavActivateId == id || g.NavActivateInputId == id) { + if (clicked) + SetKeyOwner(ImGuiKey_MouseLeft, id); SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); @@ -3240,39 +3149,14 @@ bool ImGui::VSliderInt(const char* label, const ImVec2& size, int* v, int v_min, return VSliderScalar(label, size, ImGuiDataType_S32, v, &v_min, &v_max, format, flags); } -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - -// Obsolete versions with power parameter. See https://github.com/ocornut/imgui/issues/3361 for details. -bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, float power) -{ - ImGuiSliderFlags slider_flags = ImGuiSliderFlags_None; - if (power != 1.0f) - { - IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); - slider_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths - } - return SliderScalar(label, data_type, p_data, p_min, p_max, format, slider_flags); -} - -bool ImGui::SliderScalarN(const char* label, ImGuiDataType data_type, void* v, int components, const void* v_min, const void* v_max, const char* format, float power) -{ - ImGuiSliderFlags slider_flags = ImGuiSliderFlags_None; - if (power != 1.0f) - { - IM_ASSERT(power == 1.0f && "Call function with ImGuiSliderFlags_Logarithmic flags instead of using the old 'float power' function!"); - slider_flags |= ImGuiSliderFlags_Logarithmic; // Fallback for non-asserting paths - } - return SliderScalarN(label, data_type, v, components, v_min, v_max, format, slider_flags); -} - -#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS - //------------------------------------------------------------------------- // [SECTION] Widgets: InputScalar, InputFloat, InputInt, etc. //------------------------------------------------------------------------- // - ImParseFormatFindStart() [Internal] // - ImParseFormatFindEnd() [Internal] // - ImParseFormatTrimDecorations() [Internal] +// - ImParseFormatSanitizeForPrinting() [Internal] +// - ImParseFormatSanitizeForScanning() [Internal] // - ImParseFormatPrecision() [Internal] // - TempInputTextScalar() [Internal] // - InputScalar() @@ -3336,6 +3220,57 @@ const char* ImParseFormatTrimDecorations(const char* fmt, char* buf, size_t buf_ return buf; } +// Sanitize format +// - Zero terminate so extra characters after format (e.g. "%f123") don't confuse atof/atoi +// - stb_sprintf.h supports several new modifiers which format numbers in a way that also makes them incompatible atof/atoi. +void ImParseFormatSanitizeForPrinting(const char* fmt_in, char* fmt_out, size_t fmt_out_size) +{ + const char* fmt_end = ImParseFormatFindEnd(fmt_in); + IM_UNUSED(fmt_out_size); + IM_ASSERT((size_t)(fmt_end - fmt_in + 1) < fmt_out_size); // Format is too long, let us know if this happens to you! + while (fmt_in < fmt_end) + { + char c = *fmt_in++; + if (c != '\'' && c != '$' && c != '_') // Custom flags provided by stb_sprintf.h. POSIX 2008 also supports '. + *(fmt_out++) = c; + } + *fmt_out = 0; // Zero-terminate +} + +// - For scanning we need to remove all width and precision fields "%3.7f" -> "%f". BUT don't strip types like "%I64d" which includes digits. ! "%07I64d" -> "%I64d" +const char* ImParseFormatSanitizeForScanning(const char* fmt_in, char* fmt_out, size_t fmt_out_size) +{ + const char* fmt_end = ImParseFormatFindEnd(fmt_in); + const char* fmt_out_begin = fmt_out; + IM_UNUSED(fmt_out_size); + IM_ASSERT((size_t)(fmt_end - fmt_in + 1) < fmt_out_size); // Format is too long, let us know if this happens to you! + bool has_type = false; + while (fmt_in < fmt_end) + { + char c = *fmt_in++; + if (!has_type && ((c >= '0' && c <= '9') || c == '.')) + continue; + has_type |= ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); // Stop skipping digits + if (c != '\'' && c != '$' && c != '_') // Custom flags provided by stb_sprintf.h. POSIX 2008 also supports '. + *(fmt_out++) = c; + } + *fmt_out = 0; // Zero-terminate + return fmt_out_begin; +} + +template +static const char* ImAtoi(const char* src, TYPE* output) +{ + int negative = 0; + if (*src == '-') { negative = 1; src++; } + if (*src == '+') { src++; } + TYPE v = 0; + while (*src >= '0' && *src <= '9') + v = (v * 10) + (*src++ - '0'); + *output = negative ? -v : v; + return src; +} + // Parse display precision back from the display format string // FIXME: This is still used by some navigation code path to infer a minimum tweak step, but we should aim to rework widgets so it isn't needed. int ImParseFormatPrecision(const char* fmt, int default_precision) @@ -3382,13 +3317,19 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* return value_changed; } +static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(ImGuiDataType data_type, const char* format) +{ + if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) + return ImGuiInputTextFlags_CharsScientific; + const char format_last_char = format[0] ? format[strlen(format) - 1] : 0; + return (format_last_char == 'x' || format_last_char == 'X') ? ImGuiInputTextFlags_CharsHexadecimal : ImGuiInputTextFlags_CharsDecimal; +} + // Note that Drag/Slider functions are only forwarding the min/max values clamping values if the ImGuiSliderFlags_AlwaysClamp flag is set! // This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility. // However this may not be ideal for all uses, as some user code may break on out of bound values. bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max) { - ImGuiContext& g = *GImGui; - char fmt_buf[32]; char data_buf[32]; format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); @@ -3396,7 +3337,8 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG ImStrTrimBlanks(data_buf); ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; - flags |= ((data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) ? ImGuiInputTextFlags_CharsScientific : ImGuiInputTextFlags_CharsDecimal); + flags |= InputScalar_DefaultCharsFilter(data_type, format); + bool value_changed = false; if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) { @@ -3406,7 +3348,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG memcpy(&data_backup, p_data, data_type_size); // Apply new value (or operations) then clamp - DataTypeApplyOpFromText(data_buf, g.InputTextState.InitialTextA.Data, data_type, p_data, NULL); + DataTypeApplyFromText(data_buf, data_type, p_data, format); if (p_clamp_min || p_clamp_max) { if (p_clamp_min && p_clamp_max && DataTypeCompare(data_type, p_clamp_min, p_clamp_max) > 0) @@ -3439,12 +3381,12 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data char buf[64]; DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); - bool value_changed = false; - if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) - flags |= ImGuiInputTextFlags_CharsDecimal; - flags |= ImGuiInputTextFlags_AutoSelectAll; - flags |= ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. + // Testing ActiveId as a minor optimization as filtering is not needed until active + if (g.ActiveId == 0 && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) + flags |= InputScalar_DefaultCharsFilter(data_type, format); + flags |= ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. + bool value_changed = false; if (p_step != NULL) { const float button_size = GetFrameHeight(); @@ -3453,7 +3395,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data PushID(label); SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view - value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format); + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags); // Step buttons const ImVec2 backup_frame_padding = style.FramePadding; @@ -3490,7 +3433,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data else { if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) - value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format); + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); } if (value_changed) MarkItemEdited(g.LastItemData.ID); @@ -3588,7 +3531,11 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f // - InputText() // - InputTextWithHint() // - InputTextMultiline() +// - InputTextGetCharInfo() [Internal] +// - InputTextReindexLines() [Internal] +// - InputTextReindexLinesRange() [Internal] // - InputTextEx() [Internal] +// - DebugNodeInputTextState() [Internal] //------------------------------------------------------------------------- bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) @@ -3604,7 +3551,7 @@ bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, co bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { - IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() + IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() or InputTextEx() manually if you need multi-line + hint. return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data); } @@ -3622,9 +3569,9 @@ static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** return line_count; } -static ImVec2 InputTextCalcTextSizeW(const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line) +static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; ImFont* font = g.Font; const float line_height = g.FontSize; const float scale = line_height / font->FontSize; @@ -3673,14 +3620,14 @@ namespace ImStb static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { return obj->TextW[idx]; } -static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *GImGui; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) { const ImWchar* text = obj->TextW.Data; const ImWchar* text_remaining = NULL; - const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true); + const ImVec2 size = InputTextCalcTextSizeW(obj->Ctx, text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true); r->x0 = 0.0f; r->x1 = size.x; r->baseline_y_delta = size.y; @@ -3695,13 +3642,10 @@ static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx])) : 1; } static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } -#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h -#ifdef __APPLE__ // FIXME: Move setting to IO structure -#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_MAC -#else static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } -#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_WIN -#endif +static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { ImGuiContext& g = *obj->Ctx; if (g.IO.ConfigMacOSXBehaviors) return STB_TEXTEDIT_MOVEWORDRIGHT_MAC(obj, idx); else return STB_TEXTEDIT_MOVEWORDRIGHT_WIN(obj, idx); } +#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h +#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n) { @@ -3779,11 +3723,12 @@ static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* st { stb_text_makeundo_replace(str, state, 0, str->CurLenW, text_len); ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenW); + state->cursor = state->select_start = state->select_end = 0; if (text_len <= 0) return; if (ImStb::STB_TEXTEDIT_INSERTCHARS(str, 0, text, text_len)) { - state->cursor = text_len; + state->cursor = state->select_start = state->select_end = text_len; state->has_preferred_x = 0; return; } @@ -3868,7 +3813,7 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f if (c < 0x20) { bool pass = false; - pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); + pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); // Note that an Enter KEY will emit \r and be ignored (we poll for KEY in InputText() code) pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); if (!pass) return false; @@ -3902,6 +3847,13 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f ImGuiContext& g = *GImGui; const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint; + // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block) + // While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may + // scratch their head as to why it works in numerical fields vs in generic text fields it would require support in the font. + if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | ImGuiInputTextFlags_CharsHexadecimal)) + if (c >= 0xFF01 && c <= 0xFF5E) + c = c - 0xFF01 + 0x21; + // Allow 0-9 . - + * / if (flags & ImGuiInputTextFlags_CharsDecimal) if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/')) @@ -3920,11 +3872,13 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Turn a-z into A-Z if (flags & ImGuiInputTextFlags_CharsUppercase) if (c >= 'a' && c <= 'z') - *p_char = (c += (unsigned int)('A' - 'a')); + c += (unsigned int)('A' - 'a'); if (flags & ImGuiInputTextFlags_CharsNoBlank) if (ImCharIsBlankW(c)) return false; + + *p_char = c; } // Custom callback filter @@ -3946,6 +3900,41 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f return true; } +// Find the shortest single replacement we can make to get the new text from the old text. +// Important: needs to be run before TextW is rewritten with the new characters because calling STB_TEXTEDIT_GETCHAR() at the end. +// FIXME: Ideally we should transition toward (1) making InsertChars()/DeleteChars() update undo-stack (2) discourage (and keep reconcile) or obsolete (and remove reconcile) accessing buffer directly. +static void InputTextReconcileUndoStateAfterUserCallback(ImGuiInputTextState* state, const char* new_buf_a, int new_length_a) +{ + ImGuiContext& g = *GImGui; + const ImWchar* old_buf = state->TextW.Data; + const int old_length = state->CurLenW; + const int new_length = ImTextCountCharsFromUtf8(new_buf_a, new_buf_a + new_length_a); + g.TempBuffer.reserve_discard((new_length + 1) * sizeof(ImWchar)); + ImWchar* new_buf = (ImWchar*)(void*)g.TempBuffer.Data; + ImTextStrFromUtf8(new_buf, new_length + 1, new_buf_a, new_buf_a + new_length_a); + + const int shorter_length = ImMin(old_length, new_length); + int first_diff; + for (first_diff = 0; first_diff < shorter_length; first_diff++) + if (old_buf[first_diff] != new_buf[first_diff]) + break; + if (first_diff == old_length && first_diff == new_length) + return; + + int old_last_diff = old_length - 1; + int new_last_diff = new_length - 1; + for (; old_last_diff >= first_diff && new_last_diff >= first_diff; old_last_diff--, new_last_diff--) + if (old_buf[old_last_diff] != new_buf[new_last_diff]) + break; + + const int insert_len = new_last_diff - first_diff + 1; + const int delete_len = old_last_diff - first_diff + 1; + if (insert_len > 0 || delete_len > 0) + if (STB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb.undostate, first_diff, delete_len, insert_len)) + for (int i = 0; i < delete_len; i++) + p[i] = ImStb::STB_TEXTEDIT_GETCHAR(state, first_diff + i); +} + // Edit a string of text // - buf_size account for the zero-terminator, so a buf_size of 6 can hold "Hello" but not "Hello!". // This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match @@ -4066,17 +4055,21 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->InitialTextA.resize(buf_len + 1); // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string. memcpy(state->InitialTextA.Data, buf, buf_len + 1); + // Preserve cursor position and undo/redo stack if we come back to same widget + // FIXME: Since we reworked this on 2022/06, may want to differenciate recycle_cursor vs recycle_undostate? + bool recycle_state = (state->ID == id && !init_changed_specs); + if (recycle_state && (state->CurLenA != buf_len || (state->TextAIsValid && strncmp(state->TextA.Data, buf, buf_len) != 0))) + recycle_state = false; + // Start edition const char* buf_end = NULL; + state->ID = id; state->TextW.resize(buf_size + 1); // wchar count <= UTF-8 count. we use +1 to make sure that .Data is always pointing to at least an empty string. state->TextA.resize(0); state->TextAIsValid = false; // TextA is not valid yet (we will display buf until then) state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, buf_size, buf, NULL, &buf_end); state->CurLenA = (int)(buf_end - buf); // We can't get the result from ImStrncpy() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8. - // Preserve cursor position and undo/redo stack if we come back to same widget - // FIXME: For non-readonly widgets we might be able to require that TextAIsValid && TextA == buf ? (untested) and discard undo stack if user buffer has changed. - const bool recycle_state = (state->ID == id && !init_changed_specs); if (recycle_state) { // Recycle existing cursor/selection/undo stack but clamp position @@ -4085,7 +4078,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } else { - state->ID = id; state->ScrollX = 0.0f; stb_textedit_initialize_state(&state->Stb, !is_multiline); } @@ -4110,18 +4102,24 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ SetActiveID(id, window); SetFocusID(id, window); FocusWindow(window); - - // Declare our inputs - IM_ASSERT(ImGuiNavInput_COUNT < 32); + } + if (g.ActiveId == id) + { + // Declare some inputs, the other are registered and polled via Shortcut() routing system. + if (user_clicked) + SetKeyOwner(ImGuiKey_MouseLeft, id); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); if (is_multiline || (flags & ImGuiInputTextFlags_CallbackHistory)) g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); - g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); - g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Home) | ((ImU64)1 << ImGuiKey_End); + SetKeyOwner(ImGuiKey_Home, id); + SetKeyOwner(ImGuiKey_End, id); if (is_multiline) - g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_PageUp) | ((ImU64)1 << ImGuiKey_PageDown); - if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. - g.ActiveIdUsingKeyInputMask |= ((ImU64)1 << ImGuiKey_Tab); + { + SetKeyOwner(ImGuiKey_PageUp, id); + SetKeyOwner(ImGuiKey_PageDown, id); + } + if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. + SetKeyOwner(ImGuiKey_Tab, id); } // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function) @@ -4133,10 +4131,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ clear_active_id = true; // Lock the decision of whether we are going to take the path displaying the cursor or selection - const bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active); + bool render_cursor = (g.ActiveId == id) || (state && user_scroll_active); bool render_selection = state && (state->HasSelection() || select_all) && (RENDER_SELECTION_WHEN_INACTIVE || render_cursor); bool value_changed = false; - bool enter_pressed = false; + bool validated = false; // When read-only we always use the live data passed to the function // FIXME-OPT: Because our selection/cursor code currently needs the wide text we need to convert it when active, which is not ideal :( @@ -4232,10 +4230,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } else if (io.MouseClicked[0] && !state->SelectedAllMouseLock) { - // FIXME: unselect on late click could be done release? if (hovered) { - stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); + if (io.KeyShift) + stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y); + else + stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); state->CursorAnimReset(); } } @@ -4248,16 +4248,15 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (state->SelectedAllMouseLock && !io.MouseDown[0]) state->SelectedAllMouseLock = false; - // It is ill-defined whether the backend needs to send a \t character when pressing the TAB keys. - // Win32 and GLFW naturally do it but not SDL. + // We expect backends to emit a Tab key but some also emit a Tab character which we ignore (#2467, #1336) + // (For Tab and Enter: Win32/SFML/Allegro are sending both keys and chars, GLFW and SDL are only sending keys. For Space they all send all threes) const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); - if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressedMap(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) - if (!io.InputQueueCharacters.contains('\t')) - { - unsigned int c = '\t'; // Insert TAB - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) - state->OnKeyPressed((int)c); - } + if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressed(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) + { + unsigned int c = '\t'; // Insert TAB + if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + state->OnKeyPressed((int)c); + } // Process regular text input (before we check for Return because using some IME will effectively send a Return?) // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters. @@ -4268,7 +4267,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Insert character if they pass filtering unsigned int c = (unsigned int)io.InputQueueCharacters[n]; - if (c == '\t' && io.KeyShift) + if (c == '\t') // Skip Tab, see above. continue; if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); @@ -4280,45 +4279,46 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // Process other shortcuts/key-presses - bool cancel_edit = false; + bool revert_edit = false; if (g.ActiveId == id && !g.ActiveIdIsJustActivated && !clear_active_id) { IM_ASSERT(state != NULL); - IM_ASSERT(io.KeyMods == GetMergedKeyModFlags() && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); // We rarely do this check, but if anything let's do it here. const int row_count_per_page = ImMax((int)((inner_size.y - style.FramePadding.y) / g.FontSize), 1); state->Stb.row_count_per_page = row_count_per_page; const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_osx = io.ConfigMacOSXBehaviors; - const bool is_osx_shift_shortcut = is_osx && (io.KeyMods == (ImGuiKeyModFlags_Super | ImGuiKeyModFlags_Shift)); const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End - const bool is_ctrl_key_only = (io.KeyMods == ImGuiKeyModFlags_Ctrl); - const bool is_shift_key_only = (io.KeyMods == ImGuiKeyModFlags_Shift); - const bool is_shortcut_key = g.IO.ConfigMacOSXBehaviors ? (io.KeyMods == ImGuiKeyModFlags_Super) : (io.KeyMods == ImGuiKeyModFlags_Ctrl); - const bool is_cut = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_X)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Delete))) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); - const bool is_copy = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_C)) || (is_ctrl_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_password && (!is_multiline || state->HasSelection()); - const bool is_paste = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_V)) || (is_shift_key_only && IsKeyPressedMap(ImGuiKey_Insert))) && !is_readonly; - const bool is_undo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Z)) && !is_readonly && is_undoable); - const bool is_redo = ((is_shortcut_key && IsKeyPressedMap(ImGuiKey_Y)) || (is_osx_shift_shortcut && IsKeyPressedMap(ImGuiKey_Z))) && !is_readonly && is_undoable; + // Using Shortcut() with ImGuiInputFlags_RouteFocused (default policy) to allow routing operations for other code (e.g. calling window trying to use CTRL+A and CTRL+B: formet would be handled by InputText) + // Otherwise we could simply assume that we own the keys as we are active. + const ImGuiInputFlags f_repeat = ImGuiInputFlags_Repeat; + const bool is_cut = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_X, id, f_repeat) || Shortcut(ImGuiMod_Shift | ImGuiKey_Delete, id, f_repeat)) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); + const bool is_copy = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_C, id) || Shortcut(ImGuiMod_Ctrl | ImGuiKey_Insert, id)) && !is_password && (!is_multiline || state->HasSelection()); + const bool is_paste = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_V, id, f_repeat) || Shortcut(ImGuiMod_Shift | ImGuiKey_Insert, id, f_repeat)) && !is_readonly; + const bool is_undo = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_Z, id, f_repeat)) && !is_readonly && is_undoable; + const bool is_redo = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_Y, id, f_repeat) || (is_osx && Shortcut(ImGuiMod_Shortcut | ImGuiMod_Shift | ImGuiKey_Z, id, f_repeat))) && !is_readonly && is_undoable; + const bool is_select_all = Shortcut(ImGuiMod_Shortcut | ImGuiKey_A, id); // We allow validate/cancel with Nav source (gamepad) to makes it easier to undo an accidental NavInput press with no keyboard wired, but otherwise it isn't very useful. - const bool is_validate_enter = IsKeyPressedMap(ImGuiKey_Enter) || IsKeyPressedMap(ImGuiKey_KeyPadEnter); - const bool is_validate_nav = (IsNavInputTest(ImGuiNavInput_Activate, ImGuiInputReadMode_Pressed) && !IsKeyPressedMap(ImGuiKey_Space)) || IsNavInputTest(ImGuiNavInput_Input, ImGuiInputReadMode_Pressed); - const bool is_cancel = IsKeyPressedMap(ImGuiKey_Escape) || IsNavInputTest(ImGuiNavInput_Cancel, ImGuiInputReadMode_Pressed); - - if (IsKeyPressedMap(ImGuiKey_LeftArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_DownArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMin(draw_window->Scroll.y + g.FontSize, GetScrollMaxY())); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTEND : STB_TEXTEDIT_K_DOWN) | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_PageUp) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGUP | k_mask); scroll_y -= row_count_per_page * g.FontSize; } - else if (IsKeyPressedMap(ImGuiKey_PageDown) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGDOWN | k_mask); scroll_y += row_count_per_page * g.FontSize; } - else if (IsKeyPressedMap(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_Delete) && !is_readonly && !is_cut) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } - else if (IsKeyPressedMap(ImGuiKey_Backspace) && !is_readonly) + const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; + const bool is_enter_pressed = IsKeyPressed(ImGuiKey_Enter, true) || IsKeyPressed(ImGuiKey_KeypadEnter, true); + const bool is_gamepad_validate = nav_gamepad_active && (IsKeyPressed(ImGuiKey_NavGamepadActivate, false) || IsKeyPressed(ImGuiKey_NavGamepadInput, false)); + const bool is_cancel = Shortcut(ImGuiKey_Escape, id, f_repeat) || (nav_gamepad_active && Shortcut(ImGuiKey_NavGamepadCancel, id, f_repeat)); + + // FIXME: Should use more Shortcut() and reduce IsKeyPressed()+SetKeyOwner(), but requires modifiers combination to be taken account of. + if (IsKeyPressed(ImGuiKey_LeftArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); } + else if (IsKeyPressed(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } + else if (IsKeyPressed(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } + else if (IsKeyPressed(ImGuiKey_DownArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMin(draw_window->Scroll.y + g.FontSize, GetScrollMaxY())); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTEND : STB_TEXTEDIT_K_DOWN) | k_mask); } + else if (IsKeyPressed(ImGuiKey_PageUp) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGUP | k_mask); scroll_y -= row_count_per_page * g.FontSize; } + else if (IsKeyPressed(ImGuiKey_PageDown) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGDOWN | k_mask); scroll_y += row_count_per_page * g.FontSize; } + else if (IsKeyPressed(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } + else if (IsKeyPressed(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } + else if (IsKeyPressed(ImGuiKey_Delete) && !is_readonly && !is_cut) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } + else if (IsKeyPressed(ImGuiKey_Backspace) && !is_readonly) { if (!state->HasSelection()) { @@ -4329,12 +4329,17 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); } - else if (is_validate_enter) + else if (is_enter_pressed || is_gamepad_validate) { + // Determine if we turn Enter into a \n character bool ctrl_enter_for_new_line = (flags & ImGuiInputTextFlags_CtrlEnterForNewLine) != 0; - if (!is_multiline || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl)) + if (!is_multiline || is_gamepad_validate || (ctrl_enter_for_new_line && !io.KeyCtrl) || (!ctrl_enter_for_new_line && io.KeyCtrl)) { - enter_pressed = clear_active_id = true; + validated = true; + if (io.ConfigInputTextEnterKeepActive && !is_multiline) + state->SelectAll(); // No need to scroll + else + clear_active_id = true; } else if (!is_readonly) { @@ -4343,21 +4348,32 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->OnKeyPressed((int)c); } } - else if (is_validate_nav) - { - IM_ASSERT(!is_validate_enter); - enter_pressed = clear_active_id = true; - } else if (is_cancel) { - clear_active_id = cancel_edit = true; + if (flags & ImGuiInputTextFlags_EscapeClearsAll) + { + if (state->CurLenA > 0) + { + revert_edit = true; + } + else + { + render_cursor = render_selection = false; + clear_active_id = true; + } + } + else + { + clear_active_id = revert_edit = true; + render_cursor = render_selection = false; + } } else if (is_undo || is_redo) { state->OnKeyPressed(is_undo ? STB_TEXTEDIT_K_UNDO : STB_TEXTEDIT_K_REDO); state->ClearSelection(); } - else if (is_shortcut_key && IsKeyPressedMap(ImGuiKey_A)) + else if (is_select_all) { state->SelectAll(); state->CursorFollow = true; @@ -4421,11 +4437,19 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (g.ActiveId == id) { IM_ASSERT(state != NULL); - if (cancel_edit) + if (revert_edit && !is_readonly) { - // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. - if (!is_readonly && strcmp(buf, state->InitialTextA.Data) != 0) + if (flags & ImGuiInputTextFlags_EscapeClearsAll) + { + // Clear input + apply_new_text = ""; + apply_new_text_length = 0; + STB_TEXTEDIT_CHARTYPE empty_string; + stb_textedit_replace(state, &state->Stb, &empty_string, 0); + } + else if (strcmp(buf, state->InitialTextA.Data) != 0) { + // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. // Push records into the undo stack so we can CTRL+Z the revert operation itself apply_new_text = state->InitialTextA.Data; apply_new_text_length = state->InitialTextA.Size - 1; @@ -4439,22 +4463,24 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } } + // Apply ASCII value + if (!is_readonly) + { + state->TextAIsValid = true; + state->TextA.resize(state->TextW.Size * 4 + 1); + ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); + } + // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. // This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize). - bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); + const bool apply_edit_back_to_user_buffer = !revert_edit || (validated && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); if (apply_edit_back_to_user_buffer) { // Apply new value immediately - copy modified buffer back // Note that as soon as the input box is active, the in-widget value gets priority over any underlying modification of the input buffer // FIXME: We actually always render 'buf' when calling DrawList->AddText, making the comment above incorrect. // FIXME-OPT: CPU waste to do this every time the widget is active, should mark dirty state from the stb_textedit callbacks. - if (!is_readonly) - { - state->TextAIsValid = true; - state->TextA.resize(state->TextW.Size * 4 + 1); - ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); - } // User callback if ((flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackHistory | ImGuiInputTextFlags_CallbackEdit | ImGuiInputTextFlags_CallbackAlways)) != 0) @@ -4463,18 +4489,18 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment. ImGuiInputTextFlags event_flag = 0; - ImGuiKey event_key = ImGuiKey_COUNT; - if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressedMap(ImGuiKey_Tab)) + ImGuiKey event_key = ImGuiKey_None; + if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressed(ImGuiKey_Tab)) { event_flag = ImGuiInputTextFlags_CallbackCompletion; event_key = ImGuiKey_Tab; } - else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_UpArrow)) + else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressed(ImGuiKey_UpArrow)) { event_flag = ImGuiInputTextFlags_CallbackHistory; event_key = ImGuiKey_UpArrow; } - else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressedMap(ImGuiKey_DownArrow)) + else if ((flags & ImGuiInputTextFlags_CallbackHistory) != 0 && IsKeyPressed(ImGuiKey_DownArrow)) { event_flag = ImGuiInputTextFlags_CallbackHistory; event_key = ImGuiKey_DownArrow; @@ -4523,9 +4549,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (callback_data.SelectionEnd != utf8_selection_end || buf_dirty) { state->Stb.select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb.select_start : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } if (buf_dirty) { + IM_ASSERT((flags & ImGuiInputTextFlags_ReadOnly) == 0); IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! + InputTextReconcileUndoStateAfterUserCallback(state, callback_data.Buf, callback_data.BufTextLen); // FIXME: Move the rest of this block inside function and rename to InputTextReconcileStateAfterUserCallback() ? if (callback_data.BufTextLen > backup_current_text_length && is_resizable) - state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length)); + state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length)); // Worse case scenario resize state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, callback_data.Buf, NULL); state->CurLenA = callback_data.BufTextLen; // Assume correct length and valid UTF-8 from user, saves us an extra strlen() state->CursorAnimReset(); @@ -4540,9 +4568,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ apply_new_text_length = state->CurLenA; } } - - // Clear temporary user storage - state->Flags = ImGuiInputTextFlags_None; } // Copy result to user buffer. This can currently only happen when (g.ActiveId == id) @@ -4567,7 +4592,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); IM_ASSERT(apply_new_text_length <= buf_size); } - //IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); + //IMGUI_DEBUG_PRINT("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); @@ -4656,11 +4681,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ searches_result_line_no[1] = line_count; // Calculate 2d position by finding the beginning of the line and measuring distance - cursor_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x; + cursor_offset.x = InputTextCalcTextSizeW(&g, ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x; cursor_offset.y = searches_result_line_no[0] * g.FontSize; if (searches_result_line_no[1] >= 0) { - select_start_offset.x = InputTextCalcTextSizeW(ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x; + select_start_offset.x = InputTextCalcTextSizeW(&g, ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x; select_start_offset.y = searches_result_line_no[1] * g.FontSize; } @@ -4729,7 +4754,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } else { - ImVec2 rect_size = InputTextCalcTextSizeW(p, text_selected_end, &p, NULL, true); + ImVec2 rect_size = InputTextCalcTextSizeW(&g, p, text_selected_end, &p, NULL, true); if (rect_size.x <= 0.0f) rect_size.x = IM_FLOOR(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos + ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); @@ -4760,7 +4785,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Notify OS of text input position for advanced IME (-1 x offset so that Windows IME can cover our cursor. Bit of an extra nicety.) if (!is_readonly) - g.PlatformImePos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize); + { + g.PlatformImeData.WantVisible = true; + g.PlatformImeData.InputPos = ImVec2(cursor_screen_pos.x - 1.0f, cursor_screen_pos.y - g.FontSize); + g.PlatformImeData.InputLineHeight = g.FontSize; + } } } else @@ -4819,11 +4848,47 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0) - return enter_pressed; + return validated; else return value_changed; } +void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state) +{ +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + ImGuiContext& g = *GImGui; + ImStb::STB_TexteditState* stb_state = &state->Stb; + ImStb::StbUndoState* undo_state = &stb_state->undostate; + Text("ID: 0x%08X, ActiveID: 0x%08X", state->ID, g.ActiveId); + DebugLocateItemOnHover(state->ID); + Text("CurLenW: %d, CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenA, state->CurLenW, stb_state->cursor, stb_state->select_start, stb_state->select_end); + Text("has_preferred_x: %d (%.2f)", stb_state->has_preferred_x, stb_state->preferred_x); + Text("undo_point: %d, redo_point: %d, undo_char_point: %d, redo_char_point: %d", undo_state->undo_point, undo_state->redo_point, undo_state->undo_char_point, undo_state->redo_char_point); + if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 15), true)) // Visualize undo state + { + PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + for (int n = 0; n < STB_TEXTEDIT_UNDOSTATECOUNT; n++) + { + ImStb::StbUndoRecord* undo_rec = &undo_state->undo_rec[n]; + const char undo_rec_type = (n < undo_state->undo_point) ? 'u' : (n >= undo_state->redo_point) ? 'r' : ' '; + if (undo_rec_type == ' ') + BeginDisabled(); + char buf[64] = ""; + if (undo_rec_type != ' ' && undo_rec->char_storage != -1) + ImTextStrToUtf8(buf, IM_ARRAYSIZE(buf), undo_state->undo_char + undo_rec->char_storage, undo_state->undo_char + undo_rec->char_storage + undo_rec->insert_length); + Text("%c [%02d] where %03d, insert %03d, delete %03d, char_storage %03d \"%s\"", + undo_rec_type, n, undo_rec->where, undo_rec->insert_length, undo_rec->delete_length, undo_rec->char_storage, buf); + if (undo_rec_type == ' ') + EndDisabled(); + } + PopStyleVar(); + } + EndChild(); +#else + IM_UNUSED(state); +#endif +} + //------------------------------------------------------------------------- // [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. //------------------------------------------------------------------------- @@ -4973,7 +5038,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]); } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); } } else if ((flags & ImGuiColorEditFlags_DisplayHex) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) @@ -5001,7 +5066,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag IM_UNUSED(r); // Fixes C6031: Return value ignored: 'sscanf'. } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); } ImGuiWindow* picker_active_window = NULL; @@ -5018,32 +5083,37 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Store current color and open a picker g.ColorPickerRef = col_v4; OpenPopup("picker"); - SetNextWindowPos(g.LastItemData.Rect.GetBL() + ImVec2(-1, style.ItemSpacing.y)); + SetNextWindowPos(g.LastItemData.Rect.GetBL() + ImVec2(0.0f, style.ItemSpacing.y)); } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); if (BeginPopup("picker")) { - picker_active_window = g.CurrentWindow; - if (label != label_display_end) + if (g.CurrentWindow->BeginCount == 1) { - TextEx(label, label_display_end); - Spacing(); + picker_active_window = g.CurrentWindow; + if (label != label_display_end) + { + TextEx(label, label_display_end); + Spacing(); + } + ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_PickerMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; + ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags_DisplayMask_ | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; + SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes? + value_changed |= ColorPicker4("##picker", col, picker_flags, &g.ColorPickerRef.x); } - ImGuiColorEditFlags picker_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_PickerMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaBar; - ImGuiColorEditFlags picker_flags = (flags_untouched & picker_flags_to_forward) | ImGuiColorEditFlags_DisplayMask_ | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_AlphaPreviewHalf; - SetNextItemWidth(square_sz * 12.0f); // Use 256 + bar sizes? - value_changed |= ColorPicker4("##picker", col, picker_flags, &g.ColorPickerRef.x); EndPopup(); } } if (label != label_display_end && !(flags & ImGuiColorEditFlags_NoLabel)) { - const float text_offset_x = (flags & ImGuiColorEditFlags_NoInputs) ? w_button : w_full + style.ItemInnerSpacing.x; - window->DC.CursorPos = ImVec2(pos.x + text_offset_x, pos.y + style.FramePadding.y); + // Position not necessarily next to last submitted button (e.g. if style.ColorButtonPosition == ImGuiDir_Left), + // but we need to use SameLine() to setup baseline correctly. Might want to refactor SameLine() to simplify this. + SameLine(0.0f, style.ItemInnerSpacing.x); + window->DC.CursorPos.x = pos.x + ((flags & ImGuiColorEditFlags_NoInputs) ? w_button : w_full + style.ItemInnerSpacing.x); TextEx(label, label_display_end); } @@ -5080,7 +5150,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag bool accepted_drag_drop = false; if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) { - memcpy((float*)col, payload->Data, sizeof(float) * 3); // Preserve alpha if any //-V512 + memcpy((float*)col, payload->Data, sizeof(float) * 3); // Preserve alpha if any //-V512 //-V1086 value_changed = accepted_drag_drop = true; } if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F)) @@ -5099,7 +5169,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if (picker_active_window && g.ActiveId != 0 && g.ActiveIdWindow == picker_active_window) g.LastItemData.ID = g.ActiveId; - if (value_changed) + if (value_changed && g.LastItemData.ID != 0) // In case of ID collision, the second EndGroup() won't catch g.ActiveId MarkItemEdited(g.LastItemData.ID); return value_changed; @@ -5236,7 +5306,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl } } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); } else if (flags & ImGuiColorEditFlags_PickerHueBar) { @@ -5253,7 +5323,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) - OpenPopupOnItemClick("context"); + OpenPopupOnItemClick("context", ImGuiPopupFlags_MouseButtonRight); // Hue bar logic SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); @@ -5487,7 +5557,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if (value_changed && memcmp(backup_initial_col, col, components * sizeof(float)) == 0) value_changed = false; - if (value_changed) + if (value_changed && g.LastItemData.ID != 0) // In case of ID collision, the second EndGroup() won't catch g.ActiveId MarkItemEdited(g.LastItemData.ID); PopID(); @@ -5499,7 +5569,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl // FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip. // 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. // Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set. -bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, ImVec2 size) +bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, const ImVec2& size_arg) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -5507,11 +5577,8 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl ImGuiContext& g = *GImGui; const ImGuiID id = window->GetID(desc_id); - float default_size = GetFrameHeight(); - if (size.x == 0.0f) - size.x = default_size; - if (size.y == 0.0f) - size.y = default_size; + const float default_size = GetFrameHeight(); + const ImVec2 size(size_arg.x == 0.0f ? default_size : size_arg.x, size_arg.y == 0.0f ? default_size : size_arg.y); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); if (!ItemAdd(bb, id)) @@ -5809,9 +5876,9 @@ bool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char if (window->SkipItems) return false; - ImGuiContext& g = *GImGui; - const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); - return TreeNodeBehavior(window->GetID(str_id), flags, g.TempBuffer, label_end); + const char* label, *label_end; + ImFormatStringToTempBufferV(&label, &label_end, fmt, args); + return TreeNodeBehavior(window->GetID(str_id), flags, label, label_end); } bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) @@ -5820,12 +5887,19 @@ bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char if (window->SkipItems) return false; + const char* label, *label_end; + ImFormatStringToTempBufferV(&label, &label_end, fmt, args); + return TreeNodeBehavior(window->GetID(ptr_id), flags, label, label_end); +} + +void ImGui::TreeNodeSetOpen(ImGuiID id, bool open) +{ ImGuiContext& g = *GImGui; - const char* label_end = g.TempBuffer + ImFormatStringV(g.TempBuffer, IM_ARRAYSIZE(g.TempBuffer), fmt, args); - return TreeNodeBehavior(window->GetID(ptr_id), flags, g.TempBuffer, label_end); + ImGuiStorage* storage = g.CurrentWindow->DC.StateStorage; + storage->SetInt(id, open ? 1 : 0); } -bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) +bool ImGui::TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags) { if (flags & ImGuiTreeNodeFlags_Leaf) return true; @@ -5841,7 +5915,7 @@ bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) if (g.NextItemData.OpenCond & ImGuiCond_Always) { is_open = g.NextItemData.OpenVal; - storage->SetInt(id, is_open); + TreeNodeSetOpen(id, is_open); } else { @@ -5850,7 +5924,7 @@ bool ImGui::TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags) if (stored_value == -1) { is_open = g.NextItemData.OpenVal; - storage->SetInt(id, is_open); + TreeNodeSetOpen(id, is_open); } else { @@ -5916,7 +5990,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero. const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; - bool is_open = TreeNodeBehaviorIsOpen(id, flags); + bool is_open = TreeNodeUpdateNextOpen(id, flags); if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); @@ -6259,6 +6333,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; } + if (flags & ImGuiSelectableFlags_NoSetKeyOwner) { button_flags |= ImGuiButtonFlags_NoSetKeyOwner; } if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } @@ -6275,7 +6350,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope()) // - (2) usage will fail with clipped items // The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API. - if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == window->DC.NavFocusScopeIdCurrent) + if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == g.CurrentFocusScopeId) if (g.NavJustMovedToId == id) selected = pressed = true; @@ -6284,7 +6359,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { - SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, WindowRectAbsToRel(window, bb)); // (bb == NavRect) + SetNavID(id, window->DC.NavLayerCurrent, g.CurrentFocusScopeId, WindowRectAbsToRel(window, bb)); // (bb == NavRect) g.NavDisableHighlight = true; } } @@ -6299,8 +6374,6 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render - if (held && (flags & ImGuiSelectableFlags_DrawHoveredWhenHeld)) - hovered = true; if (hovered || selected) { const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); @@ -6749,6 +6822,7 @@ bool ImGui::BeginMenuBar() // We overwrite CursorMaxPos because BeginGroup sets it to CursorPos (essentially the .EmitItem hack in EndMenuBar() would need something analogous here, maybe a BeginGroupEx() with flags). window->DC.CursorPos = window->DC.CursorMaxPos = ImVec2(bar_rect.Min.x + window->DC.MenuBarOffset.x, bar_rect.Min.y + window->DC.MenuBarOffset.y); window->DC.LayoutType = ImGuiLayoutType_Horizontal; + window->DC.IsSameLine = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Menu; window->DC.MenuBarAppending = true; AlignTextToFramePadding(); @@ -6774,7 +6848,7 @@ void ImGui::EndMenuBar() // To do so we claim focus back, restore NavId and then process the movement request for yet another frame. // This involve a one-frame delay which isn't very problematic in this situation. We could remove it by scoring in advance for multiple window (probably not worth bothering) const ImGuiNavLayer layer = ImGuiNavLayer_Menu; - IM_ASSERT(window->DC.NavLayersActiveMaskNext & (1 << layer)); // Sanity check + IM_ASSERT(window->DC.NavLayersActiveMaskNext & (1 << layer)); // Sanity check (FIXME: Seems unnecessary) FocusWindow(window); SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. @@ -6792,6 +6866,7 @@ void ImGui::EndMenuBar() g.GroupStack.back().EmitItem = false; EndGroup(); // Restore position on layer 0 window->DC.LayoutType = ImGuiLayoutType_Vertical; + window->DC.IsSameLine = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.MenuBarAppending = false; } @@ -6875,14 +6950,19 @@ static bool IsRootOfOpenMenuSet() if ((g.OpenPopupStack.Size <= g.BeginPopupStack.Size) || (window->Flags & ImGuiWindowFlags_ChildMenu)) return false; - // Initially we used 'OpenParentId' to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) based on parent ID. + // Initially we used 'upper_popup->OpenParentId == window->IDStack.back()' to differentiate multiple menu sets from each others + // (e.g. inside menu bar vs loose menu items) based on parent ID. // This would however prevent the use of e.g. PuhsID() user code submitting menus. // Previously this worked between popup and a first child menu because the first child menu always had the _ChildWindow flag, // making hovering on parent popup possible while first child menu was focused - but this was generally a bug with other side effects. // Instead we don't treat Popup specifically (in order to consistently support menu features in them), maybe the first child menu of a Popup - // doesn't have the _ChildWindow flag, and we rely on this IsRootOfOpenMenuSet() check to allow hovering between root window/popup and first chilld menu. + // doesn't have the _ChildWindow flag, and we rely on this IsRootOfOpenMenuSet() check to allow hovering between root window/popup and first child menu. + // In the end, lack of ID check made it so we could no longer differentiate between separate menu sets. To compensate for that, we at least check parent window nav layer. + // This fixes the most common case of menu opening on hover when moving between window content and menu bar. Multiple different menu sets in same nav layer would still + // open on hover, but that should be a lesser problem, because if such menus are close in proximity in window content then it won't feel weird and if they are far apart + // it likely won't be a problem anyone runs into. const ImGuiPopupData* upper_popup = &g.OpenPopupStack[g.BeginPopupStack.Size]; - return (/*upper_popup->OpenParentId == window->IDStack.back() &&*/ upper_popup->Window && (upper_popup->Window->Flags & ImGuiWindowFlags_ChildMenu)); + return (window->DC.NavLayerCurrent == upper_popup->ParentNavLayer && upper_popup->Window && (upper_popup->Window->Flags & ImGuiWindowFlags_ChildMenu)); } bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) @@ -6897,10 +6977,10 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) bool menu_is_open = IsPopupOpen(id, ImGuiPopupFlags_None); // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) - // The first menu in a hierarchy isn't so hovering doesn't get accross (otherwise e.g. resizing borders with ImGuiButtonFlags_FlattenChildren would react), but top-most BeginMenu() will bypass that limitation. - ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; + // The first menu in a hierarchy isn't so hovering doesn't get across (otherwise e.g. resizing borders with ImGuiButtonFlags_FlattenChildren would react), but top-most BeginMenu() will bypass that limitation. + ImGuiWindowFlags window_flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus; if (window->Flags & ImGuiWindowFlags_ChildMenu) - flags |= ImGuiWindowFlags_ChildWindow; + window_flags |= ImGuiWindowFlags_ChildWindow; // If a menu with same the ID was already submitted, we will append to it, matching the behavior of Begin(). // We are relying on a O(N) search - so O(N log N) over the frame - which seems like the most efficient for the expected small amount of BeginMenu() calls per frame. @@ -6908,7 +6988,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (g.MenusIdSubmittedThisFrame.contains(id)) { if (menu_is_open) - menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + menu_is_open = BeginPopupEx(id, window_flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) else g.NextWindowData.ClearFlags(); // we behave like Begin() and need to consume those values return menu_is_open; @@ -6920,10 +7000,10 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) ImVec2 label_size = CalcTextSize(label, NULL, true); // Odd hack to allow hovering across menus of a same menu-set (otherwise we wouldn't be able to hover parent without always being a Child window) + // This is only done for items for the menu set and not the full parent window. const bool menuset_is_open = IsRootOfOpenMenuSet(); - ImGuiWindow* backed_nav_window = g.NavWindow; if (menuset_is_open) - g.NavWindow = window; + PushItemFlag(ImGuiItemFlags_NoWindowHoverableCheck, true); // The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu, // However the final position is going to be different! It is chosen by FindBestWindowPosForPopup(). @@ -6934,6 +7014,9 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) BeginDisabled(); const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; bool pressed; + + // We use ImGuiSelectableFlags_NoSetKeyOwner to allow down on one menu item, move, up on another. + const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_NoSetKeyOwner | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Menu inside an horizontal menu bar @@ -6944,7 +7027,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); - pressed = Selectable("", menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups, ImVec2(w, 0.0f)); + pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, 0.0f)); RenderText(text_pos, label); PopStyleVar(); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). @@ -6960,7 +7043,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); - pressed = Selectable("", menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + pressed = Selectable("", menu_is_open, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); RenderText(text_pos, label); if (icon_w > 0.0f) RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); @@ -6969,9 +7052,9 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (!enabled) EndDisabled(); - const bool hovered = (g.HoveredId == id) && enabled; + const bool hovered = (g.HoveredId == id) && enabled && !g.NavDisableMouseHover; if (menuset_is_open) - g.NavWindow = backed_nav_window; + PopItemFlag(); bool want_open = false; bool want_close = false; @@ -6979,29 +7062,37 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) { // Close menu when not hovering it anymore unless we are moving roughly in the direction of the menu // Implement http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown to avoid using timers, so menus feels more reactive. - bool moving_toward_other_child_menu = false; - ImGuiWindow* child_menu_window = (g.BeginPopupStack.Size < g.OpenPopupStack.Size && g.OpenPopupStack[g.BeginPopupStack.Size].SourceWindow == window) ? g.OpenPopupStack[g.BeginPopupStack.Size].Window : NULL; - if (g.HoveredWindow == window && child_menu_window != NULL && !(window->Flags & ImGuiWindowFlags_MenuBar)) + bool moving_toward_child_menu = false; + ImGuiPopupData* child_popup = (g.BeginPopupStack.Size < g.OpenPopupStack.Size) ? &g.OpenPopupStack[g.BeginPopupStack.Size] : NULL; // Popup candidate (testing below) + ImGuiWindow* child_menu_window = (child_popup && child_popup->Window && child_popup->Window->ParentWindow == window) ? child_popup->Window : NULL; + if (g.HoveredWindow == window && child_menu_window != NULL) { float ref_unit = g.FontSize; // FIXME-DPI + float child_dir = (window->Pos.x < child_menu_window->Pos.x) ? 1.0f : -1.0f; ImRect next_window_rect = child_menu_window->Rect(); ImVec2 ta = (g.IO.MousePos - g.IO.MouseDelta); - ImVec2 tb = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetTL() : next_window_rect.GetTR(); - ImVec2 tc = (window->Pos.x < child_menu_window->Pos.x) ? next_window_rect.GetBL() : next_window_rect.GetBR(); + ImVec2 tb = (child_dir > 0.0f) ? next_window_rect.GetTL() : next_window_rect.GetTR(); + ImVec2 tc = (child_dir > 0.0f) ? next_window_rect.GetBL() : next_window_rect.GetBR(); float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, ref_unit * 0.5f, ref_unit * 2.5f); // add a bit of extra slack. - ta.x += (window->Pos.x < child_menu_window->Pos.x) ? -0.5f : +0.5f; // to avoid numerical issues (FIXME: ??) - tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -ref_unit * 8.0f); // triangle is maximum 200 high to limit the slope and the bias toward large sub-menus // FIXME: Multiply by fb_scale? + ta.x += child_dir * -0.5f; + tb.x += child_dir * ref_unit; + tc.x += child_dir * ref_unit; + tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -ref_unit * 8.0f); // triangle has maximum height to limit the slope and the bias toward large sub-menus tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +ref_unit * 8.0f); - moving_toward_other_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); - //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_toward_other_child_menu ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] + moving_toward_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); + //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_toward_child_menu ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] } - if (menu_is_open && !hovered && g.HoveredWindow == window && g.HoveredIdPreviousFrame != 0 && g.HoveredIdPreviousFrame != id && !moving_toward_other_child_menu) + + // The 'HovereWindow == window' check creates an inconsistency (e.g. moving away from menu slowly tends to hit same window, whereas moving away fast does not) + // But we also need to not close the top-menu menu when moving over void. Perhaps we should extend the triangle check to a larger polygon. + // (Remember to test this on BeginPopup("A")->BeginMenu("B") sequence which behaves slightly differently as B isn't a Child of A and hovering isn't shared.) + if (menu_is_open && !hovered && g.HoveredWindow == window && !moving_toward_child_menu && !g.NavDisableMouseHover) want_close = true; // Open if (!menu_is_open && pressed) // Click/activate to open want_open = true; - else if (!menu_is_open && hovered && !moving_toward_other_child_menu) // Hover to open + else if (!menu_is_open && hovered && !moving_toward_child_menu) // Hover to open want_open = true; if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open { @@ -7036,23 +7127,32 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Openable | (menu_is_open ? ImGuiItemStatusFlags_Opened : 0)); PopID(); - if (!menu_is_open && want_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) + if (want_open && !menu_is_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) { - // Don't recycle same menu level in the same frame, first close the other menu and yield for a frame. + // Don't reopen/recycle same menu level in the same frame, first close the other menu and yield for a frame. OpenPopup(label); - return false; } - - menu_is_open |= want_open; - if (want_open) + else if (want_open) + { + menu_is_open = true; OpenPopup(label); + } if (menu_is_open) { - SetNextWindowPos(popup_pos, ImGuiCond_Always); // Note: this is super misleading! The value will serve as reference for FindBestWindowPosForPopup(), not actual pos. + ImGuiLastItemData last_item_in_parent = g.LastItemData; + SetNextWindowPos(popup_pos, ImGuiCond_Always); // Note: misleading: the value will serve as reference for FindBestWindowPosForPopup(), not actual pos. PushStyleVar(ImGuiStyleVar_ChildRounding, style.PopupRounding); // First level will use _PopupRounding, subsequent will use _ChildRounding - menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) + menu_is_open = BeginPopupEx(id, window_flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) PopStyleVar(); + if (menu_is_open) + { + // Restore LastItemData so IsItemXXXX functions can work after BeginMenu()/EndMenu() + // (This fixes using IsItemClicked() and IsItemHovered(), but IsItemHovered() also relies on its support for ImGuiItemFlags_NoWindowHoverableCheck) + g.LastItemData = last_item_in_parent; + if (g.HoveredWindow == window) + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredWindow; + } } else { @@ -7069,17 +7169,18 @@ bool ImGui::BeginMenu(const char* label, bool enabled) void ImGui::EndMenu() { - // Nav: When a left move request _within our child menu_ failed, close ourselves (the _parent_ menu). - // A menu doesn't close itself because EndMenuBar() wants the catch the last Left<>Right inputs. - // However, it means that with the current code, a BeginMenu() from outside another menu or a menu-bar won't be closable with the Left direction. + // Nav: When a left move request our menu failed, close ourselves. ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - if (g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet() && window->DC.LayoutType == ImGuiLayoutType_Vertical) - if (g.NavWindow && (g.NavWindow->RootWindowForNav->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->RootWindowForNav->ParentWindow == window) - { - ClosePopupToLevel(g.BeginPopupStack.Size, true); - NavMoveRequestCancel(); - } + IM_ASSERT(window->Flags & ImGuiWindowFlags_Popup); // Mismatched BeginMenu()/EndMenu() calls + ImGuiWindow* parent_window = window->ParentWindow; // Should always be != NULL is we passed assert. + if (window->BeginCount == window->BeginCountPreviousFrame) + if (g.NavMoveDir == ImGuiDir_Left && NavMoveRequestButNoResultYet()) + if (g.NavWindow && (g.NavWindow->RootWindowForNav == window) && parent_window->DC.LayoutType == ImGuiLayoutType_Vertical) + { + ClosePopupToLevel(g.BeginPopupStack.Size - 1, true); + NavMoveRequestCancel(); + } EndPopup(); } @@ -7095,10 +7196,10 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut ImVec2 pos = window->DC.CursorPos; ImVec2 label_size = CalcTextSize(label, NULL, true); + // See BeginMenuEx() for comments about this. const bool menuset_is_open = IsRootOfOpenMenuSet(); - ImGuiWindow* backed_nav_window = g.NavWindow; if (menuset_is_open) - g.NavWindow = window; + PushItemFlag(ImGuiItemFlags_NoWindowHoverableCheck, true); // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73), // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only. @@ -7107,7 +7208,8 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut if (!enabled) BeginDisabled(); - const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_SetNavIdOnHover; + // We use ImGuiSelectableFlags_NoSetKeyOwner to allow down on one menu item, move, up on another. + const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_NoSetKeyOwner | ImGuiSelectableFlags_SetNavIdOnHover; const ImGuiMenuColumns* offsets = &window->DC.MenuColumns; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { @@ -7119,7 +7221,8 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); pressed = Selectable("", selected, selectable_flags, ImVec2(w, 0.0f)); PopStyleVar(); - RenderText(text_pos, label); + if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) + RenderText(text_pos, label); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else @@ -7133,24 +7236,27 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); - RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); - if (icon_w > 0.0f) - RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); - if (shortcut_w > 0.0f) + if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) { - PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); - RenderText(pos + ImVec2(offsets->OffsetShortcut + stretch_w, 0.0f), shortcut, NULL, false); - PopStyleColor(); + RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); + if (icon_w > 0.0f) + RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); + if (shortcut_w > 0.0f) + { + PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]); + RenderText(pos + ImVec2(offsets->OffsetShortcut + stretch_w, 0.0f), shortcut, NULL, false); + PopStyleColor(); + } + if (selected) + RenderCheckMark(window->DrawList, pos + ImVec2(offsets->OffsetMark + stretch_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(ImGuiCol_Text), g.FontSize * 0.866f); } - if (selected) - RenderCheckMark(window->DrawList, pos + ImVec2(offsets->OffsetMark + stretch_w + g.FontSize * 0.40f, g.FontSize * 0.134f * 0.5f), GetColorU32(ImGuiCol_Text), g.FontSize * 0.866f); } IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (selected ? ImGuiItemStatusFlags_Checked : 0)); if (!enabled) EndDisabled(); PopID(); if (menuset_is_open) - g.NavWindow = backed_nav_window; + PopItemFlag(); return pressed; } @@ -7202,7 +7308,7 @@ struct ImGuiTabBarSection namespace ImGui { static void TabBarLayout(ImGuiTabBar* tab_bar); - static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label); + static ImU32 TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label, ImGuiWindow* docked_window); static float TabBarCalcMaxTabWidth(); static float TabBarScrollClamp(ImGuiTabBar* tab_bar, float scrolling); static void TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGuiTabBarSection* sections); @@ -7468,8 +7574,8 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window. const char* tab_name = tab_bar->GetTabName(tab); - const bool has_close_button = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) ? false : true; - tab->ContentWidth = TabItemCalcSize(tab_name, has_close_button).x; + const bool has_close_button_or_unsaved_marker = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) == 0 || (tab->Flags & ImGuiTabItemFlags_UnsavedDocument); + tab->ContentWidth = (tab->RequestedWidth >= 0.0f) ? tab->RequestedWidth : TabItemCalcSize(tab_name, has_close_button_or_unsaved_marker).x; int section_n = TabItemGetSectionIdx(tab); ImGuiTabBarSection* section = §ions[section_n]; @@ -7478,12 +7584,10 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Store data so we can build an array sorted by width if we need to shrink tabs down IM_MSVC_WARNING_SUPPRESS(6385); - int shrink_buffer_index = shrink_buffer_indexes[section_n]++; - g.ShrinkWidthBuffer[shrink_buffer_index].Index = tab_n; - g.ShrinkWidthBuffer[shrink_buffer_index].Width = tab->ContentWidth; - - IM_ASSERT(tab->ContentWidth > 0.0f); - tab->Width = tab->ContentWidth; + ImGuiShrinkWidthItem* shrink_width_item = &g.ShrinkWidthBuffer[shrink_buffer_indexes[section_n]++]; + shrink_width_item->Index = tab_n; + shrink_width_item->Width = shrink_width_item->InitialWidth = tab->ContentWidth; + tab->Width = ImMax(tab->ContentWidth, 1.0f); } // Compute total ideal width (used for e.g. auto-resizing a window) @@ -7513,7 +7617,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) width_excess = (section_0_w + section_2_w) - tab_bar->BarRect.GetWidth(); // Excess used to shrink leading/trailing section // With ImGuiTabBarFlags_FittingPolicyScroll policy, we will only shrink leading/trailing if the central section is not visible anymore - if (width_excess > 0.0f && ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown) || !central_section_is_visible)) + if (width_excess >= 1.0f && ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyResizeDown) || !central_section_is_visible)) { int shrink_data_count = (central_section_is_visible ? sections[1].TabCount : sections[0].TabCount + sections[2].TabCount); int shrink_data_offset = (central_section_is_visible ? sections[0].TabCount + sections[2].TabCount : 0); @@ -7527,6 +7631,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) if (shrinked_width < 0.0f) continue; + shrinked_width = ImMax(1.0f, shrinked_width); int section_n = TabItemGetSectionIdx(tab); sections[section_n].Width -= (tab->Width - shrinked_width); tab->Width = shrinked_width; @@ -7596,9 +7701,11 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, tab_bar->BarRect.Min.x + tab_bar->WidthAllTabsIdeal); } -// Dockables uses Name/ID in the global namespace. Non-dockable items use the ID stack. -static ImU32 ImGui::TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label) +// Dockable windows uses Name/ID in the global namespace. Non-dockable items use the ID stack. +static ImU32 ImGui::TabBarCalcTabID(ImGuiTabBar* tab_bar, const char* label, ImGuiWindow* docked_window) { + IM_ASSERT(docked_window == NULL); // master branch only + IM_UNUSED(docked_window); if (tab_bar->Flags & ImGuiTabBarFlags_DockNode) { ImGuiID id = ImHashStr(label); @@ -7640,7 +7747,9 @@ void ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id) // Called on manual closure attempt void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { - IM_ASSERT(!(tab->Flags & ImGuiTabItemFlags_Button)); + if (tab->Flags & ImGuiTabItemFlags_Button) + return; // A button appended with TabItemButton(). + if (!(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) { // This will remove a frame of lag for selecting another tab on closure. @@ -7903,7 +8012,7 @@ bool ImGui::BeginTabItem(const char* label, bool* p_open, ImGuiTabItemFlags f } IM_ASSERT(!(flags & ImGuiTabItemFlags_Button)); // BeginTabItem() Can't be used with button flags, use TabItemButton() instead! - bool ret = TabItemEx(tab_bar, label, p_open, flags); + bool ret = TabItemEx(tab_bar, label, p_open, flags, NULL); if (ret && !(flags & ImGuiTabItemFlags_NoPushId)) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_bar->LastTabItemIdx]; @@ -7944,29 +8053,32 @@ bool ImGui::TabItemButton(const char* label, ImGuiTabItemFlags flags) IM_ASSERT_USER_ERROR(tab_bar != NULL, "Needs to be called between BeginTabBar() and EndTabBar()!"); return false; } - return TabItemEx(tab_bar, label, NULL, flags | ImGuiTabItemFlags_Button | ImGuiTabItemFlags_NoReorder); + return TabItemEx(tab_bar, label, NULL, flags | ImGuiTabItemFlags_Button | ImGuiTabItemFlags_NoReorder, NULL); } -bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags) +bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window) { // Layout whole tab bar if not already done + ImGuiContext& g = *GImGui; if (tab_bar->WantLayout) + { + ImGuiNextItemData backup_next_item_data = g.NextItemData; TabBarLayout(tab_bar); - - ImGuiContext& g = *GImGui; + g.NextItemData = backup_next_item_data; + } ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; const ImGuiStyle& style = g.Style; - const ImGuiID id = TabBarCalcTabID(tab_bar, label); + const ImGuiID id = TabBarCalcTabID(tab_bar, label, docked_window); // If the user called us with *p_open == false, we early out and don't render. // We make a call to ItemAdd() so that attempts to use a contextual popup menu with an implicit ID won't use an older ID. IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); if (p_open && !*p_open) { - ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus); + ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav); return false; } @@ -7979,9 +8091,6 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, else if (p_open == NULL) flags |= ImGuiTabItemFlags_NoCloseButton; - // Calculate tab contents size - ImVec2 size = TabItemCalcSize(label, p_open != NULL); - // Acquire tab data ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, id); bool tab_is_new = false; @@ -7990,33 +8099,48 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, tab_bar->Tabs.push_back(ImGuiTabItem()); tab = &tab_bar->Tabs.back(); tab->ID = id; - tab->Width = size.x; - tab_bar->TabsAddedNew = true; - tab_is_new = true; + tab_bar->TabsAddedNew = tab_is_new = true; } tab_bar->LastTabItemIdx = (ImS16)tab_bar->Tabs.index_from_ptr(tab); + + // Calculate tab contents size + ImVec2 size = TabItemCalcSize(label, (p_open != NULL) || (flags & ImGuiTabItemFlags_UnsavedDocument)); + tab->RequestedWidth = -1.0f; + if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) + size.x = tab->RequestedWidth = g.NextItemData.Width; + if (tab_is_new) + tab->Width = ImMax(1.0f, size.x); tab->ContentWidth = size.x; tab->BeginOrder = tab_bar->TabsActiveCount++; const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); const bool tab_bar_focused = (tab_bar->Flags & ImGuiTabBarFlags_IsFocused) != 0; const bool tab_appearing = (tab->LastFrameVisible + 1 < g.FrameCount); + const bool tab_just_unsaved = (flags & ImGuiTabItemFlags_UnsavedDocument) && !(tab->Flags & ImGuiTabItemFlags_UnsavedDocument); const bool is_tab_button = (flags & ImGuiTabItemFlags_Button) != 0; tab->LastFrameVisible = g.FrameCount; tab->Flags = flags; - // Append name with zero-terminator - tab->NameOffset = (ImS32)tab_bar->TabsNames.size(); - tab_bar->TabsNames.append(label, label + strlen(label) + 1); + // Append name _WITH_ the zero-terminator + if (docked_window != NULL) + { + IM_ASSERT(docked_window == NULL); // master branch only + } + else + { + tab->NameOffset = (ImS32)tab_bar->TabsNames.size(); + tab_bar->TabsNames.append(label, label + strlen(label) + 1); + } // Update selected tab - if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) - if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) - if (!is_tab_button) + if (!is_tab_button) + { + if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) + if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) tab_bar->NextSelectedTabId = id; // New tabs gets activated - if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // SetSelected can only be passed on explicit tab bar - if (!is_tab_button) + if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // _SetSelected can only be passed on explicit tab bar tab_bar->NextSelectedTabId = id; + } // Lock visibility // (Note: tab_contents_visible != tab_selected... because CTRL+TAB operations may preview some tabs without selecting them!) @@ -8033,7 +8157,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // and then gets submitted again, the tabs will have 'tab_appearing=true' but 'tab_is_new=false'. if (tab_appearing && (!tab_bar_appearing || tab_is_new)) { - ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav | ImGuiItemFlags_NoNavDefaultFocus); + ItemAdd(ImRect(), id, NULL, ImGuiItemFlags_NoNav); if (is_tab_button) return false; return tab_contents_visible; @@ -8131,7 +8255,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const ImGuiID close_button_id = p_open ? GetIDWithSeed("#CLOSE", NULL, id) : 0; bool just_closed; bool text_clipped; - TabItemLabelAndCloseButton(display_draw_list, bb, flags, tab_bar->FramePadding, label, id, close_button_id, tab_contents_visible, &just_closed, &text_clipped); + TabItemLabelAndCloseButton(display_draw_list, bb, tab_just_unsaved ? (flags & ~ImGuiTabItemFlags_UnsavedDocument) : flags, tab_bar->FramePadding, label, id, close_button_id, tab_contents_visible, &just_closed, &text_clipped); if (just_closed && p_open != NULL) { *p_open = false; @@ -8148,9 +8272,10 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // (We test IsItemHovered() to discard e.g. when another item is active or drag and drop over the tab bar, which g.HoveredId ignores) // FIXME: This is a mess. // FIXME: We may want disabled tab to still display the tooltip? - if (text_clipped && g.HoveredId == id && !held && g.HoveredIdNotActiveTimer > g.TooltipSlowDelay && IsItemHovered()) + if (text_clipped && g.HoveredId == id && !held) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) - SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); + if (IsItemHovered(ImGuiHoveredFlags_DelayNormal)) + SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); IM_ASSERT(!is_tab_button || !(tab_bar->SelectedTabId == tab->ID && is_tab_button)); // TabItemButton should not be selected if (is_tab_button) @@ -8168,24 +8293,30 @@ void ImGui::SetTabItemClosed(const char* label) if (is_within_manual_tab_bar) { ImGuiTabBar* tab_bar = g.CurrentTabBar; - ImGuiID tab_id = TabBarCalcTabID(tab_bar, label); + ImGuiID tab_id = TabBarCalcTabID(tab_bar, label, NULL); if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id)) tab->WantClose = true; // Will be processed by next call to TabBarLayout() } } -ImVec2 ImGui::TabItemCalcSize(const char* label, bool has_close_button) +ImVec2 ImGui::TabItemCalcSize(const char* label, bool has_close_button_or_unsaved_marker) { ImGuiContext& g = *GImGui; ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 size = ImVec2(label_size.x + g.Style.FramePadding.x, label_size.y + g.Style.FramePadding.y * 2.0f); - if (has_close_button) + if (has_close_button_or_unsaved_marker) size.x += g.Style.FramePadding.x + (g.Style.ItemInnerSpacing.x + g.FontSize); // We use Y intentionally to fit the close button circle. else size.x += g.Style.FramePadding.x + 1.0f; return ImVec2(ImMin(size.x, TabBarCalcMaxTabWidth()), size.y); } +ImVec2 ImGui::TabItemCalcSize(ImGuiWindow*) +{ + IM_ASSERT(0); // This function exists to facilitate merge with 'docking' branch. + return ImVec2(0.0f, 0.0f); +} + void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabItemFlags flags, ImU32 col) { // While rendering tabs, we trim 1 pixel off the top of our bounding box so they can fit within a regular frame height while looking "detached" from it. From f2f7f5b87b4493ddaf3903127b2e99acfb98b42e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Feb 2023 16:27:20 +0100 Subject: [PATCH 0795/1018] rt_optix: fix pixel color --- include/gproshan/raytracing/rt_utils.h | 7 ++++++- src/gproshan/raytracing/rt_optix.cu | 12 +++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 57935ca2..6f4fa370 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -72,6 +72,7 @@ struct t_eval_hit vec Kd; vec Ks{0.2f, 0.2f, 0.2f}; T Ns = 4; + T d = 1; __host_device__ t_eval_hit() {} @@ -117,6 +118,10 @@ struct t_eval_hit Ks *= texture(sc.textures[mat.map_Ks], texcoord); Ns = mat.Ns; + + d = mat.d; +// if(mat.map_d != -1) +// d = texture(sc.textures[mat.map_d], texcoord); } }; @@ -124,7 +129,7 @@ template __host_device__ vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) { - const T Lp = 10; + const T Lp = 8; const vec La(0.2f); vec li, l, h; diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/rt_optix.cu index 033e43ed..6837ff5e 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/rt_optix.cu @@ -58,7 +58,7 @@ extern "C" __global__ void __closesthit__radiance() hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - vertex li = eval_li(hit, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, + vec3 li = eval_li(hit, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { uint32_t occluded = 1; @@ -80,8 +80,8 @@ extern "C" __global__ void __closesthit__radiance() return occluded != 0; }); - vertex & pixel_color = *ray_data(); - pixel_color = (pixel_color * optix_params.n_samples + li) / (optix_params.n_samples + 1); + vec4 & pixel_color = *ray_data(); + pixel_color = (pixel_color * optix_params.n_samples + vec4(li, 1)) / (optix_params.n_samples + 1); } @@ -92,8 +92,8 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vec3 & pixel_color = *ray_data(); - pixel_color = {0, 0, 0}; + vec4 & pixel_color = *ray_data(); + pixel_color = {0, 0, 0, 0}; } extern "C" __global__ void __miss__shadow() @@ -130,8 +130,6 @@ extern "C" __global__ void __raygen__render_frame() 2, // SBT stride 0, // missSBTIndex u0, u1); - - pixel_color[3] = 1; } From 29b17cc25ef533c9168d5adfd50f1ee353a86274 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 14 Feb 2023 16:10:30 +0100 Subject: [PATCH 0796/1018] geodesics: fix #16: error in ptp cpu code it should work everything, pending testing of fps and update test code --- include/gproshan/geodesics/geodesics_ptp.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 0c32dbd1..17cad6ef 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -231,17 +231,17 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); #pragma omp parallel for - for(index_t i = start; i < start + n_cond; ++i) + for(index_t k = start; k < start + n_cond; ++k) { - const index_t & v = sorted ? sorted[i] : i; + const index_t & v = sorted ? sorted[k] : k; error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; } count = 0; #pragma omp parallel for reduction(+: count) - for(index_t v = start; v < start + n_cond; ++v) + for(index_t k = start; k < start + n_cond; ++k) { - const index_t & v = sorted ? sorted[i] : i; + const index_t & v = sorted ? sorted[k] : k; count += error[v] < PTP_TOL; } #endif From 78585f0d9d02e5deedd9cbb6214f5d44161a760e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 14 Feb 2023 16:31:57 +0100 Subject: [PATCH 0797/1018] update: MacOS building and run, solving warnings --- CMakeLists.txt | 1 + src/gproshan/CMakeLists.txt | 1 + src/gproshan/app_viewer.cpp | 8 ++++---- src/gproshan/viewer/viewer.cpp | 12 ++++++------ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 427f96f5..ba4f3ba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) +find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) find_package(Eigen3 REQUIRED) find_package(CGAL REQUIRED) diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index f03f5a6c..9f85e358 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -9,6 +9,7 @@ target_link_libraries(gproshan OpenMP::OpenMP_CXX) target_link_libraries(gproshan OpenGL::GL) target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) +target_link_libraries(gproshan X11::X11) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan CGAL::CGAL) target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index e2911c03..305254ab 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -58,7 +58,7 @@ int app_viewer::main(int nargs, const char ** args) for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); TOC(time) - sprintf(status_message, "meshes loaded in %.3fs", time); + snprintf(status_message, sizeof(status_message), "meshes loaded in %.3fs", time); init(); run(); @@ -220,7 +220,7 @@ bool app_viewer::process_connected_components(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) label[v] /= nc; - sprintf(view->status_message, "found %d connected components", nc); + snprintf(view->status_message, sizeof(view->status_message), "found %d connected components", nc); mesh.update_vbo_heatmap(); return false; @@ -436,7 +436,7 @@ bool app_viewer::process_geodesics(viewer * p_view) TIC(view->time) geodesics dm(mesh, mesh.selected, params); TOC(view->time) - sprintf(view->status_message, "geodesics time: %.3fs", view->time); + snprintf(view->status_message, sizeof(view->status_message), "geodesics time: %.3fs", view->time); dm.normalize(); mesh.update_vbo_heatmap(); @@ -709,7 +709,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); TOC(view->time) - sprintf(view->status_message, "computing %lu eigs in %.3fs", n_eigs, view->time); + snprintf(view->status_message, sizeof(view->status_message), "computing %lu eigs in %.3fs", n_eigs, view->time); for(index_t k = 0; k < n_eigs; ++k) { diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 23395ddb..ce4101f2 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -166,7 +166,7 @@ void viewer::imgui() process_t & pro = p.second; if(pro.function != nullptr && pro.sub_menu == i) if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) - sprintf(status_message, "%s", pro.selected ? pro.name.c_str() : ""); + snprintf(status_message, sizeof(status_message), "%s", pro.selected ? pro.name.c_str() : ""); //ImGui::Separator(); } @@ -227,7 +227,7 @@ void viewer::imgui() for(int i = 0; i < render_params.n_lights; ++i) { - sprintf(slight, "light %d", i); + snprintf(slight, sizeof(slight), "light %d", i); ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &pos_min, &pos_max); } @@ -480,7 +480,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in if(pro.function) { pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; - sprintf(view->status_message, "%s", pro.selected ? pro.name.c_str() : ""); + snprintf(view->status_message, sizeof(view->status_message), "%s", pro.selected ? pro.name.c_str() : ""); } } @@ -686,7 +686,7 @@ bool viewer::m_save_mesh(viewer * view) break; } - sprintf(view->status_message, "file '%s' saved.", file); + snprintf(view->status_message, sizeof(view->status_message), "file '%s' saved.", file); } return true; @@ -773,7 +773,7 @@ bool viewer::m_setup_raytracing(viewer * view) TIC(time); mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); TOC(time); - sprintf(view->status_message, "build embree in %.3fs", time); + snprintf(view->status_message, sizeof(view->status_message), "build embree in %.3fs", time); break; case R_OPTIX: @@ -782,7 +782,7 @@ bool viewer::m_setup_raytracing(viewer * view) TIC(time); mesh.rt_optix = new rt::optix({mesh}, {mesh.model_mat}); TOC(time); - sprintf(view->status_message, "build optix in %.3fs", time); + snprintf(view->status_message, sizeof(view->status_message), "build optix in %.3fs", time); #endif // GPROSHAN_OPTIX break; } From eaa2f4edd83757d0694f69999226c63d3ed52643 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 10 Mar 2023 02:57:44 +0100 Subject: [PATCH 0798/1018] raytracing: updating to embree4 --- CMakeLists.txt | 4 ++-- include/gproshan/raytracing/rt_embree.h | 3 +-- src/gproshan/raytracing/rt_embree.cpp | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba4f3ba5..f105aeba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) -find_package(CUDAToolkit 11.8) +find_package(CUDAToolkit 12) if(CUDAToolkit_FOUND) enable_language(CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) @@ -49,7 +49,7 @@ configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in ) -find_package(embree 3.13 REQUIRED) +find_package(embree 4 REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index e27fd97a..52edc42a 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -7,7 +7,7 @@ #include -#include +#include // geometry processing and shape analysis framework @@ -33,7 +33,6 @@ class embree : public raytracing RTCDevice rtc_device; RTCScene rtc_scene; - RTCIntersectContext rtc_intersect_context; std::vector g_meshes; scene_data sc; diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 0a838f99..347b923d 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -64,7 +64,6 @@ embree::embree() rtcSetSceneFlags(rtc_scene, RTC_SCENE_FLAG_COMPACT); - rtcInitIntersectContext(&rtc_intersect_context); rtcSetDeviceErrorFunction(rtc_device, embree_error, NULL); } @@ -272,13 +271,13 @@ float embree::intersect_depth(const vertex & org, const vertex & dir) bool embree::intersect(ray_hit & r) { - rtcIntersect1(rtc_scene, &rtc_intersect_context, &r); + rtcIntersect1(rtc_scene, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } bool embree::occluded(ray_hit & r) { - rtcIntersect1(rtc_scene, &rtc_intersect_context, &r); + rtcIntersect1(rtc_scene, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } From 62ae64d3616ea6f6b938ecdb52a30be6f67f13b8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 13 Mar 2023 02:36:17 +0100 Subject: [PATCH 0799/1018] rt_embree: update to embree4 --- src/gproshan/raytracing/rt_embree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 347b923d..0d4aff9e 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -25,7 +25,7 @@ embree::ray_hit::ray_hit(const vertex & p_org, const vertex & v_dir, float near, ray.time = 0.0f; ray.tfar = far; - ray.mask = 0; + ray.mask = -1; ray.flags = 0; hit.geomID = RTC_INVALID_GEOMETRY_ID; From 556f762e72eeac70e73f0aa9c08d193d48e75edd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 13 Mar 2023 02:46:05 +0100 Subject: [PATCH 0800/1018] github actions install embree4 --- .github/workflows/build.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 171c818c..5d250146 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,11 +47,8 @@ jobs: - name: Install Embree run: | - wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ - | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null - echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list - sudo apt update - sudo apt -y install intel-renderkit + tar xzf embree-4.0.1.x86_64.linux.tar.gz + source embree-4.0.1.x86_64.linux/embree-vars.sh - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 1df63e47a33bec489326923b4746211cd8e0f39a Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 13 Mar 2023 02:47:25 +0100 Subject: [PATCH 0801/1018] Update build.yml --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5d250146..7c3fe013 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,8 +47,8 @@ jobs: - name: Install Embree run: | - tar xzf embree-4.0.1.x86_64.linux.tar.gz - source embree-4.0.1.x86_64.linux/embree-vars.sh + tar xzf embree-4.0.1.x86_64.linux.tar.gz + source embree-4.0.1.x86_64.linux/embree-vars.sh - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 035ba94009cc70941e9c698be5f549c4d91693c3 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 13 Mar 2023 02:48:27 +0100 Subject: [PATCH 0802/1018] Update build.yml --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7c3fe013..1364376b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,8 +47,8 @@ jobs: - name: Install Embree run: | - tar xzf embree-4.0.1.x86_64.linux.tar.gz - source embree-4.0.1.x86_64.linux/embree-vars.sh + tar xzf embree-4.0.1.x86_64.linux.tar.gz + source embree-4.0.1.x86_64.linux/embree-vars.sh - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From edd775043bfc7f8fc0f3ecd69660ad8e9b1415d6 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 13 Mar 2023 02:53:31 +0100 Subject: [PATCH 0803/1018] Update build.yml --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1364376b..5fd9b1b1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,8 @@ jobs: libopenblas-dev \ libglew-dev \ libglfw3-dev \ - cimg-dev + cimg-dev \ + wget - name: Install Cuda if: matrix.cuda == 'cuda' @@ -47,6 +48,7 @@ jobs: - name: Install Embree run: | + wget https://github.com/embree/embree/releases/download/v4.0.1/embree-4.0.1.x86_64.linux.tar.gz tar xzf embree-4.0.1.x86_64.linux.tar.gz source embree-4.0.1.x86_64.linux/embree-vars.sh From 57ef5ea19048b812b938e72def7d42d44a853274 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 13 Mar 2023 02:57:04 +0100 Subject: [PATCH 0804/1018] Update build.yml --- .github/workflows/build.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5fd9b1b1..0b7087c1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -60,12 +60,10 @@ jobs: working-directory: ${{runner.workspace}}/build run: | export PATH=${CUDA_BIN}:${PATH} - source /opt/intel/oneapi/setvars.sh cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.config}} - name: Build working-directory: ${{runner.workspace}}/build shell: bash run: | - source /opt/intel/oneapi/setvars.sh cmake --build . --config ${{matrix.config}} From 0a9e756c7f20cf1784e2e3c370e01d4d4ac5d2bb Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 13 Mar 2023 03:04:00 +0100 Subject: [PATCH 0805/1018] Update build.yml --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0b7087c1..d53afdc9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,10 +47,10 @@ jobs: sudo apt-get -y install cuda - name: Install Embree + working-directory: /opt run: | wget https://github.com/embree/embree/releases/download/v4.0.1/embree-4.0.1.x86_64.linux.tar.gz tar xzf embree-4.0.1.x86_64.linux.tar.gz - source embree-4.0.1.x86_64.linux/embree-vars.sh - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build @@ -59,6 +59,7 @@ jobs: shell: bash working-directory: ${{runner.workspace}}/build run: | + source /opt/embree-4.0.1.x86_64.linux/embree-vars.sh export PATH=${CUDA_BIN}:${PATH} cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.config}} From 9fce4fc4d3f6a048f55e9421c7d2daa02c89390a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Mar 2023 15:59:36 +0100 Subject: [PATCH 0806/1018] update gproshanConfig.cmake.in --- gproshanConfig.cmake.in | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index e4459734..60f16a53 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -2,12 +2,12 @@ include(CMakeFindDependencyMacro) -find_dependency(CUDAToolkit 11.7) +find_dependency(CUDAToolkit 12) if(CUDAToolkit_FOUND) add_definitions(-DGPROSHAN_CUDA) endif(CUDAToolkit_FOUND) -find_dependency(embree 3.13 REQUIRED) +find_dependency(embree 4 REQUIRED) find_dependency(Threads REQUIRED) find_dependency(OpenMP REQUIRED) find_dependency(OpenGL REQUIRED) From 2debbf1c95dfa825d00846fe98adcf7faaa530ee Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Mar 2023 16:07:56 +0100 Subject: [PATCH 0807/1018] update imgui 1.89.4 --- include/imgui/imconfig.h | 2 + include/imgui/imgui.h | 117 ++- include/imgui/imgui_impl_glfw.h | 8 +- include/imgui/imgui_impl_opengl3.h | 2 +- include/imgui/imgui_impl_opengl3_loader.h | 17 +- include/imgui/imgui_internal.h | 228 +++--- src/imgui/imgui.cpp | 896 +++++++++++++--------- src/imgui/imgui_demo.cpp | 216 +++--- src/imgui/imgui_draw.cpp | 90 +-- src/imgui/imgui_impl_glfw.cpp | 155 +++- src/imgui/imgui_impl_opengl3.cpp | 16 +- src/imgui/imgui_tables.cpp | 153 ++-- src/imgui/imgui_widgets.cpp | 339 +++++--- 13 files changed, 1363 insertions(+), 876 deletions(-) diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index ed265082..876cf32f 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -90,6 +90,8 @@ constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \ operator MyVec4() const { return MyVec4(x,y,z,w); } */ +//---- ...Or use Dear ImGui's own very basic math operators. +//#define IMGUI_DEFINE_MATH_OPERATORS //---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices. // Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices). diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index 152f4f09..beb4feb1 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (headers) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/5243 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/5886 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues @@ -22,8 +22,8 @@ // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM > 12345') -#define IMGUI_VERSION "1.89.2" -#define IMGUI_VERSION_NUM 18920 +#define IMGUI_VERSION "1.89.4" +#define IMGUI_VERSION_NUM 18940 #define IMGUI_HAS_TABLE /* @@ -37,7 +37,7 @@ Index of this file: // [SECTION] ImGuiStyle // [SECTION] ImGuiIO // [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) -// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) +// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor) // [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) @@ -405,8 +405,8 @@ namespace ImGui IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame(). IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. always use this if you modify the style after NewFrame(). IMGUI_API void PopStyleVar(int count = 1); - IMGUI_API void PushAllowKeyboardFocus(bool allow_keyboard_focus); // == tab stop enable. Allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets - IMGUI_API void PopAllowKeyboardFocus(); + IMGUI_API void PushTabStop(bool tab_stop); // == tab stop enable. Allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets + IMGUI_API void PopTabStop(); IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. IMGUI_API void PopButtonRepeat(); @@ -493,6 +493,7 @@ namespace ImGui IMGUI_API void LabelTextV(const char* label, const char* fmt, va_list args) IM_FMTLIST(2); IMGUI_API void BulletText(const char* fmt, ...) IM_FMTARGS(1); // shortcut for Bullet()+Text() IMGUI_API void BulletTextV(const char* fmt, va_list args) IM_FMTLIST(1); + IMGUI_API void SeparatorText(const char* label); // currently: formatted text with an horizontal line // Widgets: Main // - Most widgets return true when the value has been changed or when pressed/selected @@ -664,8 +665,8 @@ namespace ImGui // Tooltips // - Tooltip are windows following the mouse. They do not take focus away. - IMGUI_API void BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). - IMGUI_API void EndTooltip(); + IMGUI_API bool BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). + IMGUI_API void EndTooltip(); // only call EndTooltip() if BeginTooltip() returns true! IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip(). IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); @@ -1014,10 +1015,8 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) ImGuiInputTextFlags_EscapeClearsAll = 1 << 20, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) - // Obsolete names (will be removed soon) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior -#endif + // Obsolete names + //ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior }; // Flags for ImGui::TreeNodeEx(), ImGui::CollapsingHeader*() @@ -1352,6 +1351,7 @@ enum ImGuiSortDirection_ // All our named keys are >= 512. Keys value 0 to 511 are left unused as legacy native/opaque key values (< 1.87). // Since >= 1.89 we increased typing (went from int to enum), some legacy code may need a cast to ImGuiKey. // Read details about the 1.87 and 1.89 transition : https://github.com/ocornut/imgui/issues/4921 +// Note that "Keys" related to physical keys and are not the same concept as input "Characters", the later are submitted via io.AddInputCharacter(). enum ImGuiKey : int { // Keyboard @@ -1459,15 +1459,16 @@ enum ImGuiKey : int // [Internal] Prior to 1.87 we required user to fill io.KeysDown[512] using their own native index + the io.KeyMap[] array. // We are ditching this method but keeping a legacy path for user code doing e.g. IsKeyPressed(MY_NATIVE_KEY_CODE) + // If you need to iterate all keys (for e.g. an input mapper) you may use ImGuiKey_NamedKey_BEGIN..ImGuiKey_NamedKey_END. ImGuiKey_NamedKey_BEGIN = 512, ImGuiKey_NamedKey_END = ImGuiKey_COUNT, ImGuiKey_NamedKey_COUNT = ImGuiKey_NamedKey_END - ImGuiKey_NamedKey_BEGIN, #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO - ImGuiKey_KeysData_SIZE = ImGuiKey_NamedKey_COUNT, // Size of KeysData[]: only hold named keys - ImGuiKey_KeysData_OFFSET = ImGuiKey_NamedKey_BEGIN, // First key stored in io.KeysData[0]. Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET). + ImGuiKey_KeysData_SIZE = ImGuiKey_NamedKey_COUNT, // Size of KeysData[]: only hold named keys + ImGuiKey_KeysData_OFFSET = ImGuiKey_NamedKey_BEGIN, // Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET) index. #else - ImGuiKey_KeysData_SIZE = ImGuiKey_COUNT, // Size of KeysData[]: hold legacy 0..512 keycodes + named keys - ImGuiKey_KeysData_OFFSET = 0, // First key stored in io.KeysData[0]. Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET). + ImGuiKey_KeysData_SIZE = ImGuiKey_COUNT, // Size of KeysData[]: hold legacy 0..512 keycodes + named keys + ImGuiKey_KeysData_OFFSET = 0, // Accesses to io.KeysData[] must use (key - ImGuiKey_KeysData_OFFSET) index. #endif #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1492,7 +1493,7 @@ enum ImGuiNavInput enum ImGuiConfigFlags_ { ImGuiConfigFlags_None = 0, - ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. + ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. Enable full Tabbing + directional arrows + space/enter to activate. ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. Backend also needs to set ImGuiBackendFlags_HasGamepad. ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your backend, otherwise ImGui will react as if the mouse is jumping around back and forth. ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. @@ -1608,6 +1609,9 @@ enum ImGuiStyleVar_ ImGuiStyleVar_TabRounding, // float TabRounding ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign + ImGuiStyleVar_SeparatorTextBorderSize,// float SeparatorTextBorderSize + ImGuiStyleVar_SeparatorTextAlign, // ImVec2 SeparatorTextAlign + ImGuiStyleVar_SeparatorTextPadding,// ImVec2 SeparatorTextPadding ImGuiStyleVar_COUNT }; @@ -1664,8 +1668,8 @@ enum ImGuiColorEditFlags_ ImGuiColorEditFlags_PickerMask_ = ImGuiColorEditFlags_PickerHueWheel | ImGuiColorEditFlags_PickerHueBar, ImGuiColorEditFlags_InputMask_ = ImGuiColorEditFlags_InputRGB | ImGuiColorEditFlags_InputHSV, - // Obsolete names (will be removed) - // ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex // [renamed in 1.69] + // Obsolete names + //ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_DisplayRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_DisplayHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_DisplayHex // [renamed in 1.69] }; // Flags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. @@ -1680,10 +1684,8 @@ enum ImGuiSliderFlags_ ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. - // Obsolete names (will be removed) -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp, // [renamed in 1.79] -#endif + // Obsolete names + //ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp, // [renamed in 1.79] }; // Identify a mouse button. @@ -1862,6 +1864,9 @@ struct ImGuiStyle ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. + float SeparatorTextBorderSize; // Thickkness of border in SeparatorText() + ImVec2 SeparatorTextAlign; // Alignment of text within the separator. Defaults to (0.0f, 0.5f) (left aligned, center). + ImVec2 SeparatorTextPadding; // Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y. ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. @@ -1932,6 +1937,14 @@ struct ImGuiIO bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. float ConfigMemoryCompactTimer; // = 60.0f // Timer (in seconds) to free transient windows/tables memory buffers when unused. Set to -1.0f to disable. + // Debug options + // - tools to test correct Begin/End and BeginChild/EndChild behaviors. + // - presently Begn()/End() and BeginChild()EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() + // this is inconsistent with other BeginXXX functions and create confusion for many users. + // - we expect to update the API eventually. In the meanwhile we provided tools to facilitate checking user-code behavior. + bool ConfigDebugBeginReturnValueOnce; // = false // First-time calls to Begin()/BeginChild() will return false. NEEDS TO BE SET AT APPLICATION BOOT TIME if you don't want to miss windows. + bool ConfigDebugBeginReturnValueLoop; // = false // Some calls to Begin()/BeginChild() will return false. Will cycle through window depths then repeat. Suggested use: add "io.ConfigDebugBeginReturnValue = io.KeyShift" in your main loop then occasionally press SHIFT. Windows should be flickering while running. + //------------------------------------------------------------------ // Platform Functions // (the imgui_impl_xxxx backend files are setting those up for you) @@ -1968,7 +1981,7 @@ struct ImGuiIO IMGUI_API void AddKeyAnalogEvent(ImGuiKey key, bool down, float v); // Queue a new key down/up event for analog values (e.g. ImGuiKey_Gamepad_ values). Dead-zones should be handled by the backend. IMGUI_API void AddMousePosEvent(float x, float y); // Queue a mouse position update. Use -FLT_MAX,-FLT_MAX to signify no mouse (e.g. app not focused and not hovered) IMGUI_API void AddMouseButtonEvent(int button, bool down); // Queue a mouse button change - IMGUI_API void AddMouseWheelEvent(float wh_x, float wh_y); // Queue a mouse wheel update + IMGUI_API void AddMouseWheelEvent(float wheel_x, float wheel_y); // Queue a mouse wheel update. wheel_y<0: scroll down, wheel_y>0: scroll up, wheel_x<0: scroll right, wheel_x>0: scroll left. IMGUI_API void AddFocusEvent(bool focused); // Queue a gain/loss of focus for the application (generally based on OS/platform focus of your window) IMGUI_API void AddInputCharacter(unsigned int c); // Queue a new character input IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue a new character input from a UTF-16 character, it can be a surrogate @@ -2013,13 +2026,15 @@ struct ImGuiIO // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ + ImGuiContext* Ctx; // Parent UI context (needs to be set explicitly by parent). + // Main Input State // (this block used to be written by backend, since 1.87 it is best to NOT write to those directly, call the AddXXX functions above instead) // (reading from those variables is fair game, as they are extremely unlikely to be moving anywhere) ImVec2 MousePos; // Mouse position, in pixels. Set to ImVec2(-FLT_MAX, -FLT_MAX) if mouse is unavailable (on another screen, etc.) bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Other buttons allow us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. - float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. - float MouseWheelH; // Mouse wheel Horizontal. Most users don't have a mouse with a horizontal wheel, may not be filled by all backends. + float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. >0 scrolls Up, <0 scrolls Down. Hold SHIFT to turn vertical scroll into horizontal scroll. + float MouseWheelH; // Mouse wheel Horizontal. >0 scrolls Left, <0 scrolls Right. Most users don't have a mouse with a horizontal wheel, may not be filled by all backends. bool KeyCtrl; // Keyboard modifier down: Control bool KeyShift; // Keyboard modifier down: Shift bool KeyAlt; // Keyboard modifier down: Alt @@ -2068,6 +2083,7 @@ struct ImGuiIO // - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. struct ImGuiInputTextCallbackData { + ImGuiContext* Ctx; // Parent UI context ImGuiInputTextFlags EventFlag; // One ImGuiInputTextFlags_Callback* // Read-only ImGuiInputTextFlags Flags; // What user passed to InputText() // Read-only void* UserData; // What user passed to InputText() // Read-only @@ -2152,7 +2168,7 @@ struct ImGuiTableSortSpecs }; //----------------------------------------------------------------------------- -// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, ImColor) +// [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor) //----------------------------------------------------------------------------- // Helper: Unicode defines @@ -2292,6 +2308,7 @@ struct ImGuiStorage // - The clipper also handles various subtleties related to keyboard/gamepad navigation, wrapping etc. struct ImGuiListClipper { + ImGuiContext* Ctx; // Parent UI context int DisplayStart; // First item to display, updated by each call to Step() int DisplayEnd; // End of items to display (exclusive) int ItemsCount; // [Internal] Number of items @@ -2315,6 +2332,32 @@ struct ImGuiListClipper #endif }; +// Helpers: ImVec2/ImVec4 operators +// - It is important that we are keeping those disabled by default so they don't leak in user space. +// - This is in order to allow user enabling implicit cast operators between ImVec2/ImVec4 and their own types (using IM_VEC2_CLASS_EXTRA in imconfig.h) +// - You can use '#define IMGUI_DEFINE_MATH_OPERATORS' to import our operators, provided as a courtesy. +// - We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. +#ifdef IMGUI_DEFINE_MATH_OPERATORS +#define IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED +IM_MSVC_RUNTIME_CHECKS_OFF +static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); } +static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); } +static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } +static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } +static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } +static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); } +static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } +static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } +static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } +static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } +static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; } +static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; } +static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); } +static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); } +static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); } +IM_MSVC_RUNTIME_CHECKS_RESTORE +#endif + // Helpers macros to generate 32-bit encoded colors // User can declare their own format by #defining the 5 _SHIFT/_MASK macros in their imconfig file. #ifndef IM_COL32_R_SHIFT @@ -2602,10 +2645,9 @@ struct ImDrawList inline void PrimWriteIdx(ImDrawIdx idx) { *_IdxWritePtr = idx; _IdxWritePtr++; } inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) - inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) -#endif + // Obsolete names + //inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) + //inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) // [Internal helpers] IMGUI_API void _ResetForNewFrame(); @@ -2657,7 +2699,7 @@ struct ImFontConfig bool PixelSnapH; // false // Align every glyph to pixel boundary. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1. ImVec2 GlyphExtraSpacing; // 0, 0 // Extra spacing (in pixels) between glyphs. Only X axis is supported for now. ImVec2 GlyphOffset; // 0, 0 // Offset all glyphs from this font input. - const ImWchar* GlyphRanges; // NULL // Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. + const ImWchar* GlyphRanges; // NULL // THE ARRAY DATA NEEDS TO PERSIST AS LONG AS THE FONT IS ALIVE. Pointer to a user-provided list of Unicode range (2 value per range, values are inclusive, zero-terminated list). float GlyphMinAdvanceX; // 0 // Minimum AdvanceX for glyphs, set Min to align font icons, set both Min/Max to enforce mono-space font float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights. @@ -2859,8 +2901,10 @@ struct ImFont const ImFontConfig* ConfigData; // 4-8 // in // // Pointer within ContainerAtlas->ConfigData short ConfigDataCount; // 2 // in // ~ 1 // Number of ImFontConfig involved in creating this font. Bigger than 1 when merging multiple font sources into one ImFont. ImWchar FallbackChar; // 2 // out // = FFFD/'?' // Character used if a glyph isn't found. - ImWchar EllipsisChar; // 2 // out // = '...' // Character used for ellipsis rendering. - ImWchar DotChar; // 2 // out // = '.' // Character used for ellipsis rendering (if a single '...' character isn't found) + ImWchar EllipsisChar; // 2 // out // = '...'/'.'// Character used for ellipsis rendering. + short EllipsisCharCount; // 1 // out // 1 or 3 + float EllipsisWidth; // 4 // out // Width + float EllipsisCharStep; // 4 // out // Step between characters when EllipsisCount > 0 bool DirtyLookupTables; // 1 // out // float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] @@ -2963,6 +3007,9 @@ namespace ImGui #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.89.4 (from March 2023) + static inline void PushAllowKeyboardFocus(bool tab_stop) { PushTabStop(tab_stop); } + static inline void PopAllowKeyboardFocus() { PopTabStop(); } // OBSOLETED in 1.89 (from August 2022) IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // Use new ImageButton() signature (explicit item id, regular FramePadding) // OBSOLETED in 1.88 (from May 2022) diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index 9f5e35c6..f2f34059 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -25,13 +25,17 @@ IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool ins IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); -// GLFW callbacks (installer) +// GLFW callbacks install // - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any. // - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks. IMGUI_IMPL_API void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window); IMGUI_IMPL_API void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window); -// GLFW callbacks (individual callbacks to call if you didn't install callbacks) +// GFLW callbacks options: +// - Set 'chain_for_all_windows=true' to enable chaining callbacks for all windows (including secondary viewports created by backends or by user) +IMGUI_IMPL_API void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows); + +// GLFW callbacks (individual callbacks to call yourself if you didn't install callbacks) IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84 IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84 IMGUI_IMPL_API void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87 diff --git a/include/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h index 5baf5abd..3a95f052 100644 --- a/include/imgui/imgui_impl_opengl3.h +++ b/include/imgui/imgui_impl_opengl3.h @@ -5,7 +5,7 @@ // Implemented features: // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! -// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. +// [x] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 5b6615e9..84a5a4a3 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -309,6 +309,7 @@ typedef void (APIENTRYP PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSi typedef GLint (APIENTRYP PFNGLGETUNIFORMLOCATIONPROC) (GLuint program, const GLchar *name); typedef void (APIENTRYP PFNGLGETVERTEXATTRIBIVPROC) (GLuint index, GLenum pname, GLint *params); typedef void (APIENTRYP PFNGLGETVERTEXATTRIBPOINTERVPROC) (GLuint index, GLenum pname, void **pointer); +typedef GLboolean (APIENTRYP PFNGLISPROGRAMPROC) (GLuint program); typedef void (APIENTRYP PFNGLLINKPROGRAMPROC) (GLuint program); typedef void (APIENTRYP PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); typedef void (APIENTRYP PFNGLUSEPROGRAMPROC) (GLuint program); @@ -334,6 +335,7 @@ GLAPI void APIENTRY glGetShaderInfoLog (GLuint shader, GLsizei bufSize, GLsizei GLAPI GLint APIENTRY glGetUniformLocation (GLuint program, const GLchar *name); GLAPI void APIENTRY glGetVertexAttribiv (GLuint index, GLenum pname, GLint *params); GLAPI void APIENTRY glGetVertexAttribPointerv (GLuint index, GLenum pname, void **pointer); +GLAPI GLboolean APIENTRY glIsProgram (GLuint program); GLAPI void APIENTRY glLinkProgram (GLuint program); GLAPI void APIENTRY glShaderSource (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length); GLAPI void APIENTRY glUseProgram (GLuint program); @@ -462,7 +464,7 @@ GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); /* gl3w internal state */ union GL3WProcs { - GL3WglProc ptr[58]; + GL3WglProc ptr[59]; struct { PFNGLACTIVETEXTUREPROC ActiveTexture; PFNGLATTACHSHADERPROC AttachShader; @@ -509,6 +511,7 @@ union GL3WProcs { PFNGLGETVERTEXATTRIBPOINTERVPROC GetVertexAttribPointerv; PFNGLGETVERTEXATTRIBIVPROC GetVertexAttribiv; PFNGLISENABLEDPROC IsEnabled; + PFNGLISPROGRAMPROC IsProgram; PFNGLLINKPROGRAMPROC LinkProgram; PFNGLPIXELSTOREIPROC PixelStorei; PFNGLPOLYGONMODEPROC PolygonMode; @@ -573,6 +576,7 @@ GL3W_API extern union GL3WProcs imgl3wProcs; #define glGetVertexAttribPointerv imgl3wProcs.gl.GetVertexAttribPointerv #define glGetVertexAttribiv imgl3wProcs.gl.GetVertexAttribiv #define glIsEnabled imgl3wProcs.gl.IsEnabled +#define glIsProgram imgl3wProcs.gl.IsProgram #define glLinkProgram imgl3wProcs.gl.LinkProgram #define glPixelStorei imgl3wProcs.gl.PixelStorei #define glPolygonMode imgl3wProcs.gl.PolygonMode @@ -685,7 +689,13 @@ static int parse_version(void) return GL3W_ERROR_INIT; glGetIntegerv(GL_MAJOR_VERSION, &version.major); glGetIntegerv(GL_MINOR_VERSION, &version.minor); - if (version.major < 3) + if (version.major == 0 && version.minor == 0) + { + // Query GL_VERSION in desktop GL 2.x, the string will start with "." + const char* gl_version = (const char*)glGetString(GL_VERSION); + sscanf(gl_version, "%d.%d", &version.major, &version.minor); + } + if (version.major < 2) return GL3W_ERROR_OPENGL_VERSION; return GL3W_OK; } @@ -709,7 +719,7 @@ int imgl3wInit2(GL3WGetProcAddressProc proc) int imgl3wIsSupported(int major, int minor) { - if (major < 3) + if (major < 2) return 0; if (version.major == major) return version.minor >= minor; @@ -764,6 +774,7 @@ static const char *proc_names[] = { "glGetVertexAttribPointerv", "glGetVertexAttribiv", "glIsEnabled", + "glIsProgram", "glLinkProgram", "glPixelStorei", "glPolygonMode", diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index e57eab77..20494ab4 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,10 +1,12 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (internal structures/api) -// You may use this file to debug, understand or extend ImGui features but we don't provide any guarantee of forward compatibility! -// Set: -// #define IMGUI_DEFINE_MATH_OPERATORS -// To implement maths operators for ImVec2 (disabled by default to not collide with using IM_VEC2_CLASS_EXTRA along with your own math types+operators) +// You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. +// To implement maths operators for ImVec2 (disabled by default to not conflict with using IM_VEC2_CLASS_EXTRA with your own math types+operators), use: +/* +#define IMGUI_DEFINE_MATH_OPERATORS +#include "imgui_internal.h" +*/ /* @@ -93,6 +95,12 @@ Index of this file: #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead #endif +// In 1.89.4, we moved the implementation of "courtesy maths operators" from imgui_internal.h in imgui.h +// As they are frequently requested, we do not want to encourage to many people using imgui_internal.h +#if defined(IMGUI_DEFINE_MATH_OPERATORS) && !defined(IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED) +#error Please '#define IMGUI_DEFINE_MATH_OPERATORS' _BEFORE_ including imgui.h! +#endif + // Legacy defines #ifdef IMGUI_DISABLE_FORMAT_STRING_FUNCTIONS // Renamed in 1.74 #error Use IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS @@ -118,6 +126,7 @@ struct ImDrawListSharedData; // Data shared between all ImDrawList instan struct ImGuiColorMod; // Stacked color modifier, backup of modified data so we can restore it struct ImGuiContext; // Main Dear ImGui context struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine +struct ImGuiDataVarInfo; // Variable information (e.g. to avoid style variables from an enum) struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box @@ -316,8 +325,8 @@ namespace ImStb //----------------------------------------------------------------------------- // Helpers: Hashing -IMGUI_API ImGuiID ImHashData(const void* data, size_t data_size, ImU32 seed = 0); -IMGUI_API ImGuiID ImHashStr(const char* data, size_t data_size = 0, ImU32 seed = 0); +IMGUI_API ImGuiID ImHashData(const void* data, size_t data_size, ImGuiID seed = 0); +IMGUI_API ImGuiID ImHashStr(const char* data, size_t data_size = 0, ImGuiID seed = 0); // Helpers: Sorting #ifndef ImQsort @@ -372,29 +381,6 @@ IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 -// Helpers: ImVec2/ImVec4 operators -// We are keeping those disabled by default so they don't leak in user space, to allow user enabling implicit cast operators between ImVec2 and their own types (using IM_VEC2_CLASS_EXTRA etc.) -// We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. -#ifdef IMGUI_DEFINE_MATH_OPERATORS -IM_MSVC_RUNTIME_CHECKS_OFF -static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); } -static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); } -static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } -static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } -static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } -static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); } -static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } -static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } -static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } -static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } -static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; } -static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; } -static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); } -static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); } -static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); } -IM_MSVC_RUNTIME_CHECKS_RESTORE -#endif - // Helpers: File System #ifdef IMGUI_DISABLE_FILE_FUNCTIONS #define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS @@ -548,9 +534,12 @@ struct IMGUI_API ImRect bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); } }; -IM_MSVC_RUNTIME_CHECKS_RESTORE // Helper: ImBitArray +#define IM_BITARRAY_TESTBIT(_ARRAY, _N) ((_ARRAY[(_N) >> 5] & ((ImU32)1 << ((_N) & 31))) != 0) // Macro version of ImBitArrayTestBit(): ensure args have side-effect or are costly! +#define IM_BITARRAY_CLEARBIT(_ARRAY, _N) ((_ARRAY[(_N) >> 5] &= ~((ImU32)1 << ((_N) & 31)))) // Macro version of ImBitArrayClearBit(): ensure args have side-effect or are costly! +inline size_t ImBitArrayGetStorageSizeInBytes(int bitcount) { return (size_t)((bitcount + 31) >> 5) << 2; } +inline void ImBitArrayClearAllBits(ImU32* arr, int bitcount){ memset(arr, 0, ImBitArrayGetStorageSizeInBytes(bitcount)); } inline bool ImBitArrayTestBit(const ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); return (arr[n >> 5] & mask) != 0; } inline void ImBitArrayClearBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] &= ~mask; } inline void ImBitArraySetBit(ImU32* arr, int n) { ImU32 mask = (ImU32)1 << (n & 31); arr[n >> 5] |= mask; } @@ -567,6 +556,8 @@ inline void ImBitArraySetBitRange(ImU32* arr, int n, int n2) // Works on ran } } +typedef ImU32* ImBitArrayPtr; // Name for use in structs + // Helper: ImBitArray class (wrapper over ImBitArray functions) // Store 1-bit per value. template @@ -576,11 +567,11 @@ struct ImBitArray ImBitArray() { ClearAllBits(); } void ClearAllBits() { memset(Storage, 0, sizeof(Storage)); } void SetAllBits() { memset(Storage, 255, sizeof(Storage)); } - bool TestBit(int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } + bool TestBit(int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return IM_BITARRAY_TESTBIT(Storage, n); } void SetBit(int n) { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); ImBitArraySetBit(Storage, n); } void ClearBit(int n) { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); ImBitArrayClearBit(Storage, n); } void SetBitRange(int n, int n2) { n += OFFSET; n2 += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT && n2 > n && n2 <= BITCOUNT); ImBitArraySetBitRange(Storage, n, n2); } // Works on range [n..n2) - bool operator[](int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return ImBitArrayTestBit(Storage, n); } + bool operator[](int n) const { n += OFFSET; IM_ASSERT(n >= 0 && n < BITCOUNT); return IM_BITARRAY_TESTBIT(Storage, n); } }; // Helper: ImBitVector @@ -590,10 +581,11 @@ struct IMGUI_API ImBitVector ImVector Storage; void Create(int sz) { Storage.resize((sz + 31) >> 5); memset(Storage.Data, 0, (size_t)Storage.Size * sizeof(Storage.Data[0])); } void Clear() { Storage.clear(); } - bool TestBit(int n) const { IM_ASSERT(n < (Storage.Size << 5)); return ImBitArrayTestBit(Storage.Data, n); } + bool TestBit(int n) const { IM_ASSERT(n < (Storage.Size << 5)); return IM_BITARRAY_TESTBIT(Storage.Data, n); } void SetBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArraySetBit(Storage.Data, n); } void ClearBit(int n) { IM_ASSERT(n < (Storage.Size << 5)); ImBitArrayClearBit(Storage.Data, n); } }; +IM_MSVC_RUNTIME_CHECKS_RESTORE // Helper: ImSpan<> // Pointing to a span of data we don't own. @@ -797,10 +789,10 @@ enum ImGuiItemFlags_ { // Controlled by user ImGuiItemFlags_None = 0, - ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing (FIXME: should merge with _NoNav) + ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing. This is a "lighter" version of ImGuiItemFlags_NoNav. ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. ImGuiItemFlags_Disabled = 1 << 2, // false // Disable interactions but doesn't affect visuals. See BeginDisabled()/EndDisabled(). See github.com/ocornut/imgui/issues/211 - ImGuiItemFlags_NoNav = 1 << 3, // false // Disable keyboard/gamepad directional navigation (FIXME: should merge with _NoTabStop) + ImGuiItemFlags_NoNav = 1 << 3, // false // Disable any form of focusing (keyboard/gamepad directional navigation and SetKeyboardFocusHere() calls) ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false // Disable item being a candidate for default focus (e.g. used by title bar items) ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // Disable MenuItem/Selectable() automatically closing their popup window ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) @@ -827,11 +819,13 @@ enum ImGuiItemStatusFlags_ ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8, // Set when the Focusable item just got focused by Tabbing (FIXME: to be removed soon) ImGuiItemStatusFlags_Visible = 1 << 9, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()). + // Additional status + semantic for ImGuiTestEngine #ifdef IMGUI_ENABLE_TEST_ENGINE ImGuiItemStatusFlags_Openable = 1 << 20, // Item is an openable (e.g. TreeNode) - ImGuiItemStatusFlags_Opened = 1 << 21, // + ImGuiItemStatusFlags_Opened = 1 << 21, // Opened status ImGuiItemStatusFlags_Checkable = 1 << 22, // Item is a checkable (e.g. CheckBox, MenuItem) - ImGuiItemStatusFlags_Checked = 1 << 23, // + ImGuiItemStatusFlags_Checked = 1 << 23, // Checked status + ImGuiItemStatusFlags_Inputable = 1 << 24, // Item is a text-inputable (e.g. InputText, SliderXXX, DragXXX) #endif }; @@ -960,6 +954,14 @@ enum ImGuiPopupPositionPolicy ImGuiPopupPositionPolicy_Tooltip, }; +struct ImGuiDataVarInfo +{ + ImGuiDataType Type; + ImU32 Count; // 1+ + ImU32 Offset; // Offset in parent structure + void* GetVarPtr(void* parent) const { return (void*)((unsigned char*)parent + Offset); } +}; + struct ImGuiDataTypeTempStorage { ImU8 Data[8]; // Can fit any data up to ImGuiDataType_COUNT @@ -1050,7 +1052,7 @@ struct IMGUI_API ImGuiMenuColumns // For a given item ID, access with ImGui::GetInputTextState() struct IMGUI_API ImGuiInputTextState { - ImGuiContext* Ctx; // parent dear imgui context + ImGuiContext* Ctx; // parent UI context (needs to be set explicitly by parent). ImGuiID ID; // widget id owning the text state int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not. ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. @@ -1066,7 +1068,7 @@ struct IMGUI_API ImGuiInputTextState bool Edited; // edited this frame ImGuiInputTextFlags Flags; // copy of InputText() flags. may be used to check if e.g. ImGuiInputTextFlags_Password is set. - ImGuiInputTextState(ImGuiContext* ctx) { memset(this, 0, sizeof(*this)); Ctx = ctx;} + ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } int GetUndoAvailCount() const { return Stb.undostate.undo_point; } @@ -1180,8 +1182,8 @@ struct IMGUI_API ImGuiStackSizes short SizeOfDisabledStack; ImGuiStackSizes() { memset(this, 0, sizeof(*this)); } - void SetToCurrentState(); - void CompareWithCurrentState(); + void SetToContextState(ImGuiContext* ctx); + void CompareWithContextState(ImGuiContext* ctx); }; // Data saved for each window pushed into the stack @@ -1212,6 +1214,7 @@ struct ImGuiPtrOrIndex // [SECTION] Inputs support //----------------------------------------------------------------------------- +// Bit array for named keys typedef ImBitArray ImBitArrayForNamedKeys; // [Internal] Key ranges @@ -1412,8 +1415,8 @@ struct ImGuiListClipperData enum ImGuiActivateFlags_ { ImGuiActivateFlags_None = 0, - ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default if keyboard is available. - ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default if keyboard is not available. + ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default for Enter key. + ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default for Space key and if keyboard is not used. ImGuiActivateFlags_TryToPreserveState = 1 << 2, // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) }; @@ -1599,6 +1602,7 @@ struct ImGuiWindowSettings ImVec2ih Size; bool Collapsed; bool WantApply; // Set when loaded from .ini data (to enable merging/loading .ini data into an already running context) + bool WantDelete; // Set to invalidate/delete the settings entry ImGuiWindowSettings() { memset(this, 0, sizeof(*this)); } char* GetName() { return (char*)(this + 1); } @@ -1663,23 +1667,16 @@ enum ImGuiDebugLogFlags_ struct ImGuiMetricsConfig { - bool ShowDebugLog; - bool ShowStackTool; - bool ShowWindowsRects; - bool ShowWindowsBeginOrder; - bool ShowTablesRects; - bool ShowDrawCmdMesh; - bool ShowDrawCmdBoundingBoxes; - int ShowWindowsRectsType; - int ShowTablesRectsType; - - ImGuiMetricsConfig() - { - ShowDebugLog = ShowStackTool = ShowWindowsRects = ShowWindowsBeginOrder = ShowTablesRects = false; - ShowDrawCmdMesh = true; - ShowDrawCmdBoundingBoxes = true; - ShowWindowsRectsType = ShowTablesRectsType = -1; - } + bool ShowDebugLog = false; + bool ShowStackTool = false; + bool ShowWindowsRects = false; + bool ShowWindowsBeginOrder = false; + bool ShowTablesRects = false; + bool ShowDrawCmdMesh = true; + bool ShowDrawCmdBoundingBoxes = true; + bool ShowAtlasTintedWithTextColor = false; + int ShowWindowsRectsType = -1; + int ShowTablesRectsType = -1; }; struct ImGuiStackLevelInfo @@ -1836,10 +1833,9 @@ struct ImGuiContext ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' ImGuiID NavId; // Focused item for navigation ImGuiID NavFocusScopeId; // Identify a selection scope (selection code often wants to "clear other items" when landing on an item of the selection set) - ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() - ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0 - ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat) - ImGuiID NavActivateInputId; // ~~ IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadInput) ? NavId : 0; ImGuiActivateFlags_PreferInput will be set and NavActivateId will be 0. + ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() + ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0 + ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat) ImGuiActivateFlags NavActivateFlags; ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). @@ -1943,9 +1939,11 @@ struct ImGuiContext ImFont InputTextPasswordFont; ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets - float ColorEditLastHue; // Backup of last Hue associated to LastColor, so we can restore Hue in lossy RGB<>HSV round trips - float ColorEditLastSat; // Backup of last Saturation associated to LastColor, so we can restore Saturation in lossy RGB<>HSV round trips - ImU32 ColorEditLastColor; // RGB value with alpha set to 0. + ImGuiID ColorEditCurrentID; // Set temporarily while inside of the parent-most ColorEdit4/ColorPicker4 (because they call each others). + ImGuiID ColorEditSavedID; // ID we are saving/restoring HS for + float ColorEditSavedHue; // Backup of last Hue associated to LastColor, so we can restore Hue in lossy RGB<>HSV round trips + float ColorEditSavedSat; // Backup of last Saturation associated to LastColor, so we can restore Saturation in lossy RGB<>HSV round trips + ImU32 ColorEditSavedColor; // RGB value with alpha set to 0. ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. ImGuiComboPreviewData ComboPreviewData; float SliderGrabClickOffset; @@ -1996,7 +1994,9 @@ struct ImGuiContext ImGuiDebugLogFlags DebugLogFlags; ImGuiTextBuffer DebugLogBuf; ImGuiTextIndex DebugLogIndex; + ImU8 DebugLogClipperAutoDisableFrames; ImU8 DebugLocateFrames; // For DebugLocateItemOnHover(). This is used together with DebugLocateId which is in a hot/cached spot above. + ImS8 DebugBeginReturnValueCullDepth; // Cycle between 0..9 then wrap around. bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) ImU8 DebugItemPickerMouseButton; ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this ID @@ -2014,8 +2014,10 @@ struct ImGuiContext ImVector TempBuffer; // Temporary text buffer ImGuiContext(ImFontAtlas* shared_font_atlas) - : InputTextState(this) { + IO.Ctx = this; + InputTextState.Ctx = this; + Initialized = false; FontAtlasOwnedByContext = shared_font_atlas ? false : true; Font = NULL; @@ -2074,7 +2076,7 @@ struct ImGuiContext BeginMenuCount = 0; NavWindow = NULL; - NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = NavActivateInputId = 0; + NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = 0; NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; NavJustMovedToKeyMods = ImGuiMod_None; @@ -2131,8 +2133,9 @@ struct ImGuiContext TempInputId = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; - ColorEditLastHue = ColorEditLastSat = 0.0f; - ColorEditLastColor = 0; + ColorEditCurrentID = ColorEditSavedID = 0; + ColorEditSavedHue = ColorEditSavedSat = 0.0f; + ColorEditSavedColor = 0; SliderGrabClickOffset = 0.0f; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; @@ -2165,7 +2168,9 @@ struct ImGuiContext DebugLogFlags = ImGuiDebugLogFlags_OutputToTTY; DebugLocateId = 0; + DebugLogClipperAutoDisableFrames = 0; DebugLocateFrames = 0; + DebugBeginReturnValueCullDepth = -1; DebugItemPickerActive = false; DebugItemPickerMouseButton = ImGuiMouseButton_Left; DebugItemPickerBreakId = 0; @@ -2234,6 +2239,7 @@ struct IMGUI_API ImGuiWindowTempData // Storage for one window struct IMGUI_API ImGuiWindow { + ImGuiContext* Ctx; // Parent UI context (needs to be set explicitly by parent). char* Name; // Window name, owned by the window. ImGuiID ID; // == ImHashStr(Name) ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_ @@ -2344,10 +2350,10 @@ struct IMGUI_API ImGuiWindow // We don't use g.FontSize because the window may be != g.CurrentWindow. ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } - float CalcFontSize() const { ImGuiContext& g = *GImGui; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } - float TitleBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } + float CalcFontSize() const { ImGuiContext& g = *Ctx; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } + float TitleBarHeight() const { ImGuiContext& g = *Ctx; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } - float MenuBarHeight() const { ImGuiContext& g = *GImGui; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } + float MenuBarHeight() const { ImGuiContext& g = *Ctx; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } }; @@ -2384,7 +2390,7 @@ struct ImGuiTabItem float RequestedWidth; // Width optionally requested by caller, -1.0f is unused ImS32 NameOffset; // When Window==NULL, offset to name within parent ImGuiTabBar::TabsNames ImS16 BeginOrder; // BeginTabItem() order, used to re-order tabs after toggling ImGuiTabBarFlags_Reorderable - ImS16 IndexDuringLayout; // Index only used during TabBarLayout() + ImS16 IndexDuringLayout; // Index only used during TabBarLayout(). Tabs gets reordered so 'Tabs[n].IndexDuringLayout == n' but may mismatch during additions. bool WantClose; // Marked as closed by SetTabItemClosed() ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; RequestedWidth = -1.0f; NameOffset = -1; BeginOrder = IndexDuringLayout = -1; } @@ -2426,12 +2432,6 @@ struct IMGUI_API ImGuiTabBar ImGuiTextBuffer TabsNames; // For non-docking tab bar we re-append names in a contiguous buffer. ImGuiTabBar(); - int GetTabOrder(const ImGuiTabItem* tab) const { return Tabs.index_from_ptr(tab); } - const char* GetTabName(const ImGuiTabItem* tab) const - { - IM_ASSERT(tab->NameOffset != -1 && tab->NameOffset < TabsNames.Buf.Size); - return TabsNames.Buf.Data + tab->NameOffset; - } }; //----------------------------------------------------------------------------- @@ -2439,12 +2439,11 @@ struct IMGUI_API ImGuiTabBar //----------------------------------------------------------------------------- #define IM_COL32_DISABLE IM_COL32(0,0,0,1) // Special sentinel code which cannot be used as a regular color. -#define IMGUI_TABLE_MAX_COLUMNS 64 // sizeof(ImU64) * 8. This is solely because we frequently encode columns set in a ImU64. -#define IMGUI_TABLE_MAX_DRAW_CHANNELS (4 + 64 * 2) // See TableSetupDrawChannels() +#define IMGUI_TABLE_MAX_COLUMNS 512 // May be further lifted // Our current column maximum is 64 but we may raise that in the future. -typedef ImS8 ImGuiTableColumnIdx; -typedef ImU8 ImGuiTableDrawChannelIdx; +typedef ImS16 ImGuiTableColumnIdx; +typedef ImU16 ImGuiTableDrawChannelIdx; // [Internal] sizeof() ~ 104 // We use the terminology "Enabled" to refer to a column that is not Hidden by user/api. @@ -2515,14 +2514,15 @@ struct ImGuiTableCellData ImGuiTableColumnIdx Column; // Column number }; -// Per-instance data that needs preserving across frames (seemingly most others do not need to be preserved aside from debug needs, does that needs they could be moved to ImGuiTableTempData ?) +// Per-instance data that needs preserving across frames (seemingly most others do not need to be preserved aside from debug needs. Does that means they could be moved to ImGuiTableTempData?) struct ImGuiTableInstanceData { + ImGuiID TableInstanceID; float LastOuterHeight; // Outer height from last frame float LastFirstRowHeight; // Height of first row from last frame (FIXME: this is used as "header height" and may be reworked) float LastFrozenHeight; // Height of frozen section from last frame - ImGuiTableInstanceData() { LastOuterHeight = LastFirstRowHeight = LastFrozenHeight = 0.0f; } + ImGuiTableInstanceData() { TableInstanceID = 0; LastOuterHeight = LastFirstRowHeight = LastFrozenHeight = 0.0f; } }; // FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs, incoming RowData @@ -2535,10 +2535,9 @@ struct IMGUI_API ImGuiTable ImSpan Columns; // Point within RawData[] ImSpan DisplayOrderToIndex; // Point within RawData[]. Store display order of columns (when not reordered, the values are 0...Count-1) ImSpan RowCellData; // Point within RawData[]. Store cells background requests for current row. - ImU64 EnabledMaskByDisplayOrder; // Column DisplayOrder -> IsEnabled map - ImU64 EnabledMaskByIndex; // Column Index -> IsEnabled map (== not hidden by user/api) in a format adequate for iterating column without touching cold data - ImU64 VisibleMaskByIndex; // Column Index -> IsVisibleX|IsVisibleY map (== not hidden by user/api && not hidden by scrolling/cliprect) - ImU64 RequestOutputMaskByIndex; // Column Index -> IsVisible || AutoFit (== expect user to submit items) + ImBitArrayPtr EnabledMaskByDisplayOrder; // Column DisplayOrder -> IsEnabled map + ImBitArrayPtr EnabledMaskByIndex; // Column Index -> IsEnabled map (== not hidden by user/api) in a format adequate for iterating column without touching cold data + ImBitArrayPtr VisibleMaskByIndex; // Column Index -> IsVisibleX|IsVisibleY map (== not hidden by user/api && not hidden by scrolling/cliprect) ImGuiTableFlags SettingsLoadedFlags; // Which data were loaded from the .ini file (e.g. when order is not altered we won't save order) int SettingsOffset; // Offset in g.SettingsTables int LastFrameActive; @@ -2726,6 +2725,7 @@ namespace ImGui IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); IMGUI_API void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); + IMGUI_API void SetWindowHiddendAndSkipItemsForCurrentFrame(ImGuiWindow* window); inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } @@ -2769,13 +2769,16 @@ namespace ImGui IMGUI_API void MarkIniSettingsDirty(); IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window); IMGUI_API void ClearIniSettings(); - IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); - IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id); - IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name); IMGUI_API void AddSettingsHandler(const ImGuiSettingsHandler* handler); IMGUI_API void RemoveSettingsHandler(const char* type_name); IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name); + // Settings - Windows + IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name); + IMGUI_API ImGuiWindowSettings* FindWindowSettingsByID(ImGuiID id); + IMGUI_API ImGuiWindowSettings* FindWindowSettingsByWindow(ImGuiWindow* window); + IMGUI_API void ClearWindowSettings(const char* name); + // Localization IMGUI_API void LocalizeRegisterEntries(const ImGuiLocEntry* entries, int count); inline const char* LocalizeGetMsg(ImGuiLocKey key) { ImGuiContext& g = *GImGui; const char* msg = g.LocalizationTable[key]; return msg ? msg : "*Missing Text*"; } @@ -2808,6 +2811,7 @@ namespace ImGui IMGUI_API void MarkItemEdited(ImGuiID id); // Mark data associated to given item as "edited", used by IsItemDeactivatedAfterEdit() function. IMGUI_API void PushOverrideID(ImGuiID id); // Push given value as-is at the top of the ID stack (whereas PushID combines old and new hashes) IMGUI_API ImGuiID GetIDWithSeed(const char* str_id_begin, const char* str_id_end, ImGuiID seed); + IMGUI_API ImGuiID GetIDWithSeed(int n, ImGuiID seed); // Basic Helpers for widget code IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); @@ -2826,6 +2830,7 @@ namespace ImGui // Parameter stacks (shared) IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); IMGUI_API void PopItemFlag(); + IMGUI_API const ImGuiDataVarInfo* GetStyleVarInfo(ImGuiStyleVar idx); // Logging/Capture IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. @@ -2841,7 +2846,7 @@ namespace ImGui IMGUI_API void ClosePopupsExceptModals(); IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); - IMGUI_API void BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); + IMGUI_API bool BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImGuiWindow* GetTopMostAndVisiblePopupModal(); @@ -3003,7 +3008,8 @@ namespace ImGui IMGUI_API void TableDrawContextMenu(ImGuiTable* table); IMGUI_API bool TableBeginContextMenuPopup(ImGuiTable* table); IMGUI_API void TableMergeDrawChannels(ImGuiTable* table); - inline ImGuiTableInstanceData* TableGetInstanceData(ImGuiTable* table, int instance_no) { if (instance_no == 0) return &table->InstanceDataFirst; return &table->InstanceDataExtra[instance_no - 1]; } + inline ImGuiTableInstanceData* TableGetInstanceData(ImGuiTable* table, int instance_no) { if (instance_no == 0) return &table->InstanceDataFirst; return &table->InstanceDataExtra[instance_no - 1]; } + inline ImGuiID TableGetInstanceID(ImGuiTable* table, int instance_no) { return TableGetInstanceData(table, instance_no)->TableInstanceID; } IMGUI_API void TableSortSpecsSanitize(ImGuiTable* table); IMGUI_API void TableSortSpecsBuild(ImGuiTable* table); IMGUI_API ImGuiSortDirection TableGetColumnNextSortDirection(ImGuiTableColumn* column); @@ -3015,7 +3021,7 @@ namespace ImGui IMGUI_API void TableEndCell(ImGuiTable* table); IMGUI_API ImRect TableGetCellBgRect(const ImGuiTable* table, int column_n); IMGUI_API const char* TableGetColumnName(const ImGuiTable* table, int column_n); - IMGUI_API ImGuiID TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no = 0); + IMGUI_API ImGuiID TableGetColumnResizeID(ImGuiTable* table, int column_n, int instance_no = 0); IMGUI_API float TableGetMaxColumnWidth(const ImGuiTable* table, int column_n); IMGUI_API void TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n); IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table); @@ -3034,12 +3040,18 @@ namespace ImGui IMGUI_API ImGuiTableSettings* TableSettingsFindByID(ImGuiID id); // Tab Bars + inline ImGuiTabBar* GetCurrentTabBar() { ImGuiContext& g = *GImGui; return g.CurrentTabBar; } IMGUI_API bool BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& bb, ImGuiTabBarFlags flags); IMGUI_API ImGuiTabItem* TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id); + IMGUI_API ImGuiTabItem* TabBarFindTabByOrder(ImGuiTabBar* tab_bar, int order); + IMGUI_API ImGuiTabItem* TabBarGetCurrentTab(ImGuiTabBar* tab_bar); + inline int TabBarGetTabOrder(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { return tab_bar->Tabs.index_from_ptr(tab); } + IMGUI_API const char* TabBarGetTabName(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); - IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int offset); - IMGUI_API void TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, ImVec2 mouse_pos); + IMGUI_API void TabBarQueueFocus(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); + IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, int offset); + IMGUI_API void TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImVec2 mouse_pos); IMGUI_API bool TabBarProcessReorder(ImGuiTabBar* tab_bar); IMGUI_API bool TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, ImGuiTabItemFlags flags, ImGuiWindow* docked_window); IMGUI_API ImVec2 TabItemCalcSize(const char* label, bool has_close_button_or_unsaved_marker); @@ -3074,8 +3086,9 @@ namespace ImGui IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); - IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col); + IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0); IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); + IMGUI_API void SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_width); IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); @@ -3130,7 +3143,7 @@ namespace ImGui IMGUI_API void ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags); // Plot - IMGUI_API int PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size); + IMGUI_API int PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, const ImVec2& size_arg); // Shade functions (write over already created vertices) IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1); @@ -3182,7 +3195,7 @@ namespace ImGui // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' - // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || g.NavActivateInputId == id' (WIP) + // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))' (WIP) // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() inline void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem @@ -3222,14 +3235,15 @@ IMGUI_API void ImFontAtlasBuildMultiplyRectAlpha8(const unsigned char table //----------------------------------------------------------------------------- #ifdef IMGUI_ENABLE_TEST_ENGINE -extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, const ImRect& bb, ImGuiID id); +extern void ImGuiTestEngineHook_ItemAdd(ImGuiContext* ctx, ImGuiID id, const ImRect& bb, const ImGuiLastItemData* item_data); // item_data may be NULL extern void ImGuiTestEngineHook_ItemInfo(ImGuiContext* ctx, ImGuiID id, const char* label, ImGuiItemStatusFlags flags); extern void ImGuiTestEngineHook_Log(ImGuiContext* ctx, const char* fmt, ...); extern const char* ImGuiTestEngine_FindItemDebugLabel(ImGuiContext* ctx, ImGuiID id); -#define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _BB, _ID) // Register item bounding box -#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) -#define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log +// In IMGUI_VERSION_NUM >= 18934: changed IMGUI_TEST_ENGINE_ITEM_ADD(bb,id) to IMGUI_TEST_ENGINE_ITEM_ADD(id,bb,item_data); +#define IMGUI_TEST_ENGINE_ITEM_ADD(_ID,_BB,_ITEM_DATA) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _ID, _BB, _ITEM_DATA) // Register item bounding box +#define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) +#define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log #else #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) ((void)0) #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)g) diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index 3faca98f..fa8580cd 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (main code and documentation) // Help: @@ -11,7 +11,7 @@ // - FAQ http://dearimgui.org/faq // - Homepage & latest https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/5243 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/5886 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues @@ -39,14 +39,13 @@ Index of this file: DOCUMENTATION - MISSION STATEMENT -- END-USER GUIDE +- CONTROLS GUIDE - PROGRAMMER GUIDE - READ FIRST - HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI - GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE - HOW A SIMPLE APPLICATION MAY LOOK LIKE - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE - - USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - API BREAKING CHANGES (read me when you update!) - FREQUENTLY ASKED QUESTIONS (FAQ) - Read all answers online: https://www.dearimgui.org/faq, or in docs/FAQ.md (with a Markdown viewer) @@ -114,27 +113,73 @@ CODE - Limited layout features, intricate layouts are typically crafted in code. - END-USER GUIDE + CONTROLS GUIDE ============== - - Double-click on title bar to collapse window. - - Click upper right corner to close a window, available when 'bool* p_open' is passed to ImGui::Begin(). - - Click and drag on lower right corner to resize window (double-click to auto fit window to its contents). - - Click and drag on any empty space to move window. - - TAB/SHIFT+TAB to cycle through keyboard editable fields. - - CTRL+Click on a slider or drag box to input value as text. - - Use mouse wheel to scroll. - - Text editor: - - Hold SHIFT or use mouse to select text. - - CTRL+Left/Right to word jump. - - CTRL+Shift+Left/Right to select words. - - CTRL+A or Double-Click to select all. - - CTRL+X,CTRL+C,CTRL+V to use OS clipboard/ - - CTRL+Z,CTRL+Y to undo/redo. - - ESCAPE to revert text to its original value. - - Controls are automatically adjusted for OSX to match standard OSX text editing operations. - - General Keyboard controls: enable with ImGuiConfigFlags_NavEnableKeyboard. - - General Gamepad controls: enable with ImGuiConfigFlags_NavEnableGamepad. Download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets + - MOUSE CONTROLS + - Mouse wheel: Scroll vertically. + - SHIFT+Mouse wheel: Scroll horizontally. + - Click [X]: Close a window, available when 'bool* p_open' is passed to ImGui::Begin(). + - Click ^, Double-Click title: Collapse window. + - Drag on corner/border: Resize window (double-click to auto fit window to its contents). + - Drag on any empty space: Move window (unless io.ConfigWindowsMoveFromTitleBarOnly = true). + - Left-click outside popup: Close popup stack (right-click over underlying popup: Partially close popup stack). + + - TEXT EDITOR + - Hold SHIFT or Drag Mouse: Select text. + - CTRL+Left/Right: Word jump. + - CTRL+Shift+Left/Right: Select words. + - CTRL+A or Double-Click: Select All. + - CTRL+X, CTRL+C, CTRL+V: Use OS clipboard. + - CTRL+Z, CTRL+Y: Undo, Redo. + - ESCAPE: Revert text to its original value. + - On OSX, controls are automatically adjusted to match standard OSX text editing shortcuts and behaviors. + + - KEYBOARD CONTROLS + - Basic: + - Tab, SHIFT+Tab Cycle through text editable fields. + - CTRL+Tab, CTRL+Shift+Tab Cycle through windows. + - CTRL+Click Input text into a Slider or Drag widget. + - Extended features with `io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard`: + - Tab, SHIFT+Tab: Cycle through every items. + - Arrow keys Move through items using directional navigation. Tweak value. + - Arrow keys + Alt, Shift Tweak slower, tweak faster (when using arrow keys). + - Enter Activate item (prefer text input when possible). + - Space Activate item (prefer tweaking with arrows when possible). + - Escape Deactivate item, leave child window, close popup. + - Page Up, Page Down Previous page, next page. + - Home, End Scroll to top, scroll to bottom. + - Alt Toggle between scrolling layer and menu layer. + - CTRL+Tab then Ctrl+Arrows Move window. Hold SHIFT to resize instead of moving. + - Output when ImGuiConfigFlags_NavEnableKeyboard set, + - io.WantCaptureKeyboard flag is set when keyboard is claimed. + - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. + - io.NavVisible: true when the navigation cursor is visible (usually goes to back false when mouse is used). + + - GAMEPAD CONTROLS + - Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. + - Particularly useful to use Dear ImGui on a console system (e.g. PlayStation, Switch, Xbox) without a mouse! + - Download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets + - Backend support: backend needs to: + - Set 'io.BackendFlags |= ImGuiBackendFlags_HasGamepad' + call io.AddKeyEvent/AddKeyAnalogEvent() with ImGuiKey_Gamepad_XXX keys. + - For analog values (0.0f to 1.0f), backend is responsible to handling a dead-zone and rescaling inputs accordingly. + Backend code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). + - BEFORE 1.87, BACKENDS USED TO WRITE TO io.NavInputs[]. This is now obsolete. Please call io functions instead! + - If you need to share inputs between your game and the Dear ImGui interface, the easiest approach is to go all-or-nothing, + with a buttons combo to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. + + - REMOTE INPUTS SHARING & MOUSE EMULATION + - PS4/PS5 users: Consider emulating a mouse cursor with DualShock touch pad or a spare analog stick as a mouse-emulation fallback. + - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + run examples/libs/synergy/uSynergy.c (on your console/tablet/phone app) + in order to share your PC mouse/keyboard. + - See https://github.com/ocornut/imgui/wiki/Useful-Extensions#remoting for other remoting solutions. + - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. + Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs Dear ImGui to move your mouse cursor along with navigation movements. + When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. + When that happens your backend NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the backends in examples/ do that. + (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, Dear ImGui will misbehave as it will see your mouse moving back & forth!) + (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want + to set a boolean to ignore your other external mouse positions until the external source is moved again.) PROGRAMMER GUIDE @@ -344,40 +389,6 @@ CODE } - USING GAMEPAD/KEYBOARD NAVIGATION CONTROLS - ------------------------------------------ - - The gamepad/keyboard navigation is fairly functional and keeps being improved. - - Gamepad support is particularly useful to use Dear ImGui on a console system (e.g. PlayStation, Switch, Xbox) without a mouse! - - The initial focus was to support game controllers, but keyboard is becoming increasingly and decently usable. - - Keyboard: - - Application: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard to enable. - - When keyboard navigation is active (io.NavActive + ImGuiConfigFlags_NavEnableKeyboard), - the io.WantCaptureKeyboard flag will be set. For more advanced uses, you may want to read from: - - io.NavActive: true when a window is focused and it doesn't have the ImGuiWindowFlags_NoNavInputs flag set. - - io.NavVisible: true when the navigation cursor is visible (and usually goes false when mouse is used). - - or query focus information with e.g. IsWindowFocused(ImGuiFocusedFlags_AnyWindow), IsItemFocused() etc. functions. - Please reach out if you think the game vs navigation input sharing could be improved. - - Gamepad: - - Application: Set io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad to enable. - - Backend: Set io.BackendFlags |= ImGuiBackendFlags_HasGamepad + call io.AddKeyEvent/AddKeyAnalogEvent() with ImGuiKey_Gamepad_XXX keys. - For analog values (0.0f to 1.0f), backend is responsible to handling a dead-zone and rescaling inputs accordingly. - Backend code will probably need to transform your raw inputs (such as e.g. remapping your 0.2..0.9 raw input range to 0.0..1.0 imgui range, etc.). - - BEFORE 1.87, BACKENDS USED TO WRITE TO io.NavInputs[]. This is now obsolete. Please call io functions instead! - - You can download PNG/PSD files depicting the gamepad controls for common controllers at: http://dearimgui.org/controls_sheets - - If you need to share inputs between your game and the Dear ImGui interface, the easiest approach is to go all-or-nothing, - with a buttons combo to toggle the target. Please reach out if you think the game vs navigation input sharing could be improved. - - Mouse: - - PS4/PS5 users: Consider emulating a mouse cursor with DualShock4 touch pad or a spare analog stick as a mouse-emulation fallback. - - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + uSynergy.c (on your console/tablet/phone app) to share your PC mouse/keyboard. - - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. - Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs dear imgui to move your mouse cursor along with navigation movements. - When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. - When that happens your backend NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the backends in examples/ do that. - (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, imgui will misbehave as it will see your mouse moving back and forth!) - (In a setup when you may not have easy control over the mouse cursor, e.g. uSynergy.c doesn't expose moving remote mouse cursor, you may want - to set a boolean to ignore your other external mouse positions until the external source is moved again.) - - API BREAKING CHANGES ==================== @@ -386,21 +397,35 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. - - 2022/10/26 (1.89) - commented out redirecting OpenPopupContextItem() which was briefly the name of OpenPopupOnItemClick() from 1.77 to 1.79. - - 2022/10/12 (1.89) - removed runtime patching of invalid "%f"/"%0.f" format strings for DragInt()/SliderInt(). This was obsoleted in 1.61 (May 2018). See 1.61 changelog for details. - - 2022/09/26 (1.89) - renamed and merged keyboard modifiers key enums and flags into a same set. Kept inline redirection enums (will obsolete). - - ImGuiKey_ModCtrl and ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl - - ImGuiKey_ModShift and ImGuiModFlags_Shift -> ImGuiMod_Shift - - ImGuiKey_ModAlt and ImGuiModFlags_Alt -> ImGuiMod_Alt - - ImGuiKey_ModSuper and ImGuiModFlags_Super -> ImGuiMod_Super - the ImGuiKey_ModXXX were introduced in 1.87 and mostly used by backends. - the ImGuiModFlags_XXX have been exposed in imgui.h but not really used by any public api only by third-party extensions. - exceptionally commenting out the older ImGuiKeyModFlags_XXX names ahead of obsolescence schedule to reduce confusion and because they were not meant to be used anyway. - - 2022/09/20 (1.89) - ImGuiKey is now a typed enum, allowing ImGuiKey_XXX symbols to be named in debuggers. - this will require uses of legacy backend-dependent indices to be casted, e.g. - - with imgui_impl_glfw: IsKeyPressed(GLFW_KEY_A) -> IsKeyPressed((ImGuiKey)GLFW_KEY_A); - - with imgui_impl_win32: IsKeyPressed('A') -> IsKeyPressed((ImGuiKey)'A') - - etc. However if you are upgrading code you might well use the better, backend-agnostic IsKeyPressed(ImGuiKey_A) now! + - 2023/03/14 (1.89.4) - commented out redirecting enums/functions names that were marked obsolete two years ago: + - ImGuiSliderFlags_ClampOnInput -> use ImGuiSliderFlags_AlwaysClamp + - ImGuiInputTextFlags_AlwaysInsertMode -> use ImGuiInputTextFlags_AlwaysOverwrite + - ImDrawList::AddBezierCurve() -> use ImDrawList::AddBezierCubic() + - ImDrawList::PathBezierCurveTo() -> use ImDrawList::PathBezierCubicCurveTo() + - 2023/03/09 (1.89.4) - renamed PushAllowKeyboardFocus()/PopAllowKeyboardFocus() to PushTabStop()/PopTabStop(). Kept inline redirection functions (will obsolete). + - 2023/03/09 (1.89.4) - tooltips: Added 'bool' return value to BeginTooltip() for API consistency. Please only submit contents and call EndTooltip() if BeginTooltip() returns true. In reality the function will _currently_ always return true, but further changes down the line may change this, best to clarify API sooner. + - 2023/02/15 (1.89.4) - moved the optional "courtesy maths operators" implementation from imgui_internal.h in imgui.h. + Even though we encourage using your own maths types and operators by setting up IM_VEC2_CLASS_EXTRA, + it has been frequently requested by people to use our own. We had an opt-in define which was + previously fulfilled in imgui_internal.h. It is now fulfilled in imgui.h. (#6164) + - OK: #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui.h" / #include "imgui_internal.h" + - Error: #include "imgui.h" / #define IMGUI_DEFINE_MATH_OPERATORS / #include "imgui_internal.h" + - 2023/02/07 (1.89.3) - backends: renamed "imgui_impl_sdl.cpp" to "imgui_impl_sdl2.cpp" and "imgui_impl_sdl.h" to "imgui_impl_sdl2.h". (#6146) This is in prevision for the future release of SDL3. + - 2022/10/26 (1.89) - commented out redirecting OpenPopupContextItem() which was briefly the name of OpenPopupOnItemClick() from 1.77 to 1.79. + - 2022/10/12 (1.89) - removed runtime patching of invalid "%f"/"%0.f" format strings for DragInt()/SliderInt(). This was obsoleted in 1.61 (May 2018). See 1.61 changelog for details. + - 2022/09/26 (1.89) - renamed and merged keyboard modifiers key enums and flags into a same set. Kept inline redirection enums (will obsolete). + - ImGuiKey_ModCtrl and ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl + - ImGuiKey_ModShift and ImGuiModFlags_Shift -> ImGuiMod_Shift + - ImGuiKey_ModAlt and ImGuiModFlags_Alt -> ImGuiMod_Alt + - ImGuiKey_ModSuper and ImGuiModFlags_Super -> ImGuiMod_Super + the ImGuiKey_ModXXX were introduced in 1.87 and mostly used by backends. + the ImGuiModFlags_XXX have been exposed in imgui.h but not really used by any public api only by third-party extensions. + exceptionally commenting out the older ImGuiKeyModFlags_XXX names ahead of obsolescence schedule to reduce confusion and because they were not meant to be used anyway. + - 2022/09/20 (1.89) - ImGuiKey is now a typed enum, allowing ImGuiKey_XXX symbols to be named in debuggers. + this will require uses of legacy backend-dependent indices to be casted, e.g. + - with imgui_impl_glfw: IsKeyPressed(GLFW_KEY_A) -> IsKeyPressed((ImGuiKey)GLFW_KEY_A); + - with imgui_impl_win32: IsKeyPressed('A') -> IsKeyPressed((ImGuiKey)'A') + - etc. However if you are upgrading code you might well use the better, backend-agnostic IsKeyPressed(ImGuiKey_A) now! - 2022/09/12 (1.89) - removed the bizarre legacy default argument for 'TreePush(const void* ptr = NULL)', always pass a pointer value explicitly. NULL/nullptr is ok but require cast, e.g. TreePush((void*)nullptr); - 2022/09/05 (1.89) - commented out redirecting functions/enums names that were marked obsolete in 1.77 and 1.78 (June 2020): - DragScalar(), DragScalarN(), DragFloat(), DragFloat2(), DragFloat3(), DragFloat4(): For old signatures ending with (..., const char* format, float power = 1.0f) -> use (..., format ImGuiSliderFlags_Logarithmic) if power != 1.0f. @@ -859,12 +884,12 @@ CODE #define _CRT_SECURE_NO_WARNINGS #endif -#include "imgui.h" -#ifndef IMGUI_DISABLE - #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif + +#include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_internal.h" // System includes @@ -983,8 +1008,8 @@ static void WindowSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSetti static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf); // Platform Dependents default implementation for IO functions -static const char* GetClipboardTextFn_DefaultImpl(void* user_data); -static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); +static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx); +static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text); static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data); namespace ImGui @@ -1003,7 +1028,7 @@ static void NavEndFrame(); static bool NavScoreItem(ImGuiNavItemData* result); static void NavApplyItemToResult(ImGuiNavItemData* result); static void NavProcessItem(); -static void NavProcessItemForTabbingRequest(ImGuiID id); +static void NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flags, ImGuiNavMoveFlags move_flags); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); @@ -1118,6 +1143,9 @@ ImGuiStyle::ImGuiStyle() ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. + SeparatorTextBorderSize = 3.0f; // Thickkness of border in SeparatorText() + SeparatorTextAlign = ImVec2(0.0f,0.5f);// Alignment of text within the separator. Defaults to (0.0f, 0.5f) (left aligned, center). + SeparatorTextPadding = ImVec2(20.0f,3.f);// Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y. DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. @@ -1155,6 +1183,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) LogSliderDeadzone = ImFloor(LogSliderDeadzone * scale_factor); TabRounding = ImFloor(TabRounding * scale_factor); TabMinWidthForCloseButton = (TabMinWidthForCloseButton != FLT_MAX) ? ImFloor(TabMinWidthForCloseButton * scale_factor) : FLT_MAX; + SeparatorTextPadding = ImFloor(SeparatorTextPadding * scale_factor); DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); @@ -1206,14 +1235,13 @@ ImGuiIO::ImGuiIO() ConfigWindowsResizeFromEdges = true; ConfigWindowsMoveFromTitleBarOnly = false; ConfigMemoryCompactTimer = 60.0f; + ConfigDebugBeginReturnValueOnce = false; + ConfigDebugBeginReturnValueLoop = false; // Platform Functions + // Note: Initialize() will setup default clipboard/ime handlers. BackendPlatformName = BackendRendererName = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; - GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations - SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; - ClipboardUserData = NULL; - SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl; // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); @@ -1232,8 +1260,8 @@ ImGuiIO::ImGuiIO() // FIXME: Should in theory be called "AddCharacterEvent()" to be consistent with new API void ImGuiIO::AddInputCharacter(unsigned int c) { - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; if (c == 0 || !AppAcceptingEvents) return; @@ -1288,8 +1316,7 @@ void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) { unsigned int c = 0; utf8_chars += ImTextCharFromUtf8(&c, utf8_chars, NULL); - if (c != 0) - AddInputCharacter(c); + AddInputCharacter(c); } } @@ -1346,10 +1373,10 @@ static ImGuiInputEvent* FindLatestInputEvent(ImGuiInputEventType type, int arg = void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) { //if (e->Down) { IMGUI_DEBUG_LOG_IO("AddKeyEvent() Key='%s' %d, NativeKeycode = %d, NativeScancode = %d\n", ImGui::GetKeyName(e->Key), e->Down, e->NativeKeycode, e->NativeScancode); } + IM_ASSERT(Ctx != NULL); if (key == ImGuiKey_None || !AppAcceptingEvents) return; - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + ImGuiContext& g = *Ctx; IM_ASSERT(ImGui::IsNamedKeyOrModKey(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. IM_ASSERT(!ImGui::IsAliasKey(key)); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. IM_ASSERT(key != ImGuiMod_Shortcut); // We could easily support the translation here but it seems saner to not accept it (TestEngine perform a translation itself) @@ -1424,8 +1451,8 @@ void ImGuiIO::SetAppAcceptingEvents(bool accepting_events) // Queue a mouse move event void ImGuiIO::AddMousePosEvent(float x, float y) { - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; if (!AppAcceptingEvents) return; @@ -1448,8 +1475,8 @@ void ImGuiIO::AddMousePosEvent(float x, float y) void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) { - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; IM_ASSERT(mouse_button >= 0 && mouse_button < ImGuiMouseButton_COUNT); if (!AppAcceptingEvents) return; @@ -1468,11 +1495,11 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) g.InputEventsQueue.push_back(e); } -// Queue a mouse wheel event (most mouse/API will only have a Y component) +// Queue a mouse wheel event (some mouse/API may only have a Y component) void ImGuiIO::AddMouseWheelEvent(float wheel_x, float wheel_y) { - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; // Filter duplicate (unlike most events, wheel values are relative and easy to filter) if (!AppAcceptingEvents || (wheel_x == 0.0f && wheel_y == 0.0f)) @@ -1488,8 +1515,8 @@ void ImGuiIO::AddMouseWheelEvent(float wheel_x, float wheel_y) void ImGuiIO::AddFocusEvent(bool focused) { - ImGuiContext& g = *GImGui; - IM_ASSERT(&g.IO == this && "Can only add events to current context."); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; // Filter duplicate const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_Focus); @@ -1806,18 +1833,36 @@ void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, ImGuiContext& g = *GImGui; va_list args; va_start(args, fmt); - int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); - *out_buf = g.TempBuffer.Data; - if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } + if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) + { + const char* buf = va_arg(args, const char*); // Skip formatting when using "%s" + *out_buf = buf; + if (out_buf_end) { *out_buf_end = buf + strlen(buf); } + } + else + { + int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); + *out_buf = g.TempBuffer.Data; + if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } + } va_end(args); } void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args) { ImGuiContext& g = *GImGui; - int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); - *out_buf = g.TempBuffer.Data; - if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } + if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) + { + const char* buf = va_arg(args, const char*); // Skip formatting when using "%s" + *out_buf = buf; + if (out_buf_end) { *out_buf_end = buf + strlen(buf); } + } + else + { + int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); + *out_buf = g.TempBuffer.Data; + if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } + } } // CRC32 needs a 1KB lookup table (not cache friendly) @@ -1846,7 +1891,7 @@ static const ImU32 GCrc32LookupTable[256] = // Known size hash // It is ok to call ImHashData on a string with known length but the ### operator won't be supported. // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. -ImGuiID ImHashData(const void* data_p, size_t data_size, ImU32 seed) +ImGuiID ImHashData(const void* data_p, size_t data_size, ImGuiID seed) { ImU32 crc = ~seed; const unsigned char* data = (const unsigned char*)data_p; @@ -1862,7 +1907,7 @@ ImGuiID ImHashData(const void* data_p, size_t data_size, ImU32 seed) // - If we reach ### in the string we discard the hash so far and reset to the seed. // - We don't do 'current += 2; continue;' after handling ### to keep the code smaller/faster (measured ~10% diff in Debug build) // FIXME-OPT: Replace with e.g. FNV1a hash? CRC32 pretty much randomly access 1KB. Need to do proper measurements. -ImGuiID ImHashStr(const char* data_p, size_t data_size, ImU32 seed) +ImGuiID ImHashStr(const char* data_p, size_t data_size, ImGuiID seed) { seed = ~seed; ImU32 crc = seed; @@ -1904,7 +1949,7 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode) // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); - ImVector buf; + ImVector buf; buf.resize(filename_wsize + mode_wsize); ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize); ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize); @@ -1967,6 +2012,8 @@ void* ImFileLoadToMemory(const char* filename, const char* mode, size_t* out_f // [SECTION] MISC HELPERS/UTILITIES (ImText* functions) //----------------------------------------------------------------------------- +IM_MSVC_RUNTIME_CHECKS_OFF + // Convert UTF-8 to 32-bit character, process single character input. // A nearly-branchless UTF-8 decoder, based on work of Christopher Wellons (https://github.com/skeeto/branchless-utf8). // We handle UTF-8 decoding error by skipping forward. @@ -1978,7 +2025,7 @@ int ImTextCharFromUtf8(unsigned int* out_char, const char* in_text, const char* static const int shiftc[] = { 0, 18, 12, 6, 0 }; static const int shifte[] = { 0, 6, 4, 2, 0 }; int len = lengths[*(const unsigned char*)in_text >> 3]; - int wanted = len + !len; + int wanted = len + (len ? 0 : 1); if (in_text_end == NULL) in_text_end = in_text + wanted; // Max length, nulls will be taken into account. @@ -2030,8 +2077,6 @@ int ImTextStrFromUtf8(ImWchar* buf, int buf_size, const char* in_text, const cha { unsigned int c; in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); - if (c == 0) - break; *buf_out++ = (ImWchar)c; } *buf_out = 0; @@ -2047,8 +2092,6 @@ int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end) { unsigned int c; in_text += ImTextCharFromUtf8(&c, in_text, in_text_end); - if (c == 0) - break; char_count++; } return char_count; @@ -2142,6 +2185,7 @@ int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_e } return bytes_count; } +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (Color functions) @@ -2681,6 +2725,8 @@ static void ImGuiListClipper_SeekCursorForItem(ImGuiListClipper* clipper, int it ImGuiListClipper::ImGuiListClipper() { memset(this, 0, sizeof(*this)); + Ctx = ImGui::GetCurrentContext(); + IM_ASSERT(Ctx != NULL); ItemsCount = -1; } @@ -2691,7 +2737,7 @@ ImGuiListClipper::~ImGuiListClipper() void ImGuiListClipper::Begin(int items_count, float items_height) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; ImGuiWindow* window = g.CurrentWindow; IMGUI_DEBUG_LOG_CLIPPER("Clipper: Begin(%d,%.2f) in '%s'\n", items_count, items_height, window->Name); @@ -2716,7 +2762,7 @@ void ImGuiListClipper::Begin(int items_count, float items_height) void ImGuiListClipper::End() { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; if (ImGuiListClipperData* data = (ImGuiListClipperData*)TempData) { // In theory here we should assert that we are already at the right position, but it seems saner to just seek at the end and not assert/crash the user. @@ -2748,7 +2794,7 @@ void ImGuiListClipper::ForceDisplayRangeByIndices(int item_min, int item_max) static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *clipper->Ctx; ImGuiWindow* window = g.CurrentWindow; ImGuiListClipperData* data = (ImGuiListClipperData*)clipper->TempData; IM_ASSERT(data != NULL && "Called ImGuiListClipper::Step() too many times, or before ImGuiListClipper::Begin() ?"); @@ -2871,7 +2917,7 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) bool ImGuiListClipper::Step() { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; bool need_items_height = (ItemsHeight <= 0.0f); bool ret = ImGuiListClipper_StepInternal(this); if (ret && (DisplayStart == DisplayEnd)) @@ -2972,15 +3018,7 @@ void ImGui::PopStyleColor(int count) } } -struct ImGuiStyleVarInfo -{ - ImGuiDataType Type; - ImU32 Count; - ImU32 Offset; - void* GetVarPtr(ImGuiStyle* style) const { return (void*)((unsigned char*)style + Offset); } -}; - -static const ImGuiStyleVarInfo GStyleVarInfo[] = +static const ImGuiDataVarInfo GStyleVarInfo[] = { { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha @@ -3007,41 +3045,44 @@ static const ImGuiStyleVarInfo GStyleVarInfo[] = { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign + { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextBorderSize) },// ImGuiStyleVar_SeparatorTextBorderSize + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextAlign) }, // ImGuiStyleVar_SeparatorTextAlign + { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextPadding) }, // ImGuiStyleVar_SeparatorTextPadding }; -static const ImGuiStyleVarInfo* GetStyleVarInfo(ImGuiStyleVar idx) +const ImGuiDataVarInfo* ImGui::GetStyleVarInfo(ImGuiStyleVar idx) { IM_ASSERT(idx >= 0 && idx < ImGuiStyleVar_COUNT); - IM_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); + IM_STATIC_ASSERT(IM_ARRAYSIZE(GStyleVarInfo) == ImGuiStyleVar_COUNT); return &GStyleVarInfo[idx]; } void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) { - const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + ImGuiContext& g = *GImGui; + const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) { - ImGuiContext& g = *GImGui; float* pvar = (float*)var_info->GetVarPtr(&g.Style); g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } - IM_ASSERT(0 && "Called PushStyleVar() float variant but variable is not a float!"); + IM_ASSERT_USER_ERROR(0, "Called PushStyleVar() variant with wrong type!"); } void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) { - const ImGuiStyleVarInfo* var_info = GetStyleVarInfo(idx); + ImGuiContext& g = *GImGui; + const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) { - ImGuiContext& g = *GImGui; ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); *pvar = val; return; } - IM_ASSERT(0 && "Called PushStyleVar() ImVec2 variant but variable is not a ImVec2!"); + IM_ASSERT_USER_ERROR(0, "Called PushStyleVar() variant with wrong type!"); } void ImGui::PopStyleVar(int count) @@ -3056,7 +3097,7 @@ void ImGui::PopStyleVar(int count) { // We avoid a generic memcpy(data, &backup.Backup.., GDataTypeSize[info->Type] * info->Count), the overhead in Debug is not worth it. ImGuiStyleMod& backup = g.StyleVarStack.back(); - const ImGuiStyleVarInfo* info = GetStyleVarInfo(backup.VarIdx); + const ImGuiDataVarInfo* info = GetStyleVarInfo(backup.VarIdx); void* data = info->GetVarPtr(&g.Style); if (info->Type == ImGuiDataType_Float && info->Count == 1) { ((float*)data)[0] = backup.BackupFloat[0]; } else if (info->Type == ImGuiDataType_Float && info->Count == 2) { ((float*)data)[0] = backup.BackupFloat[0]; ((float*)data)[1] = backup.BackupFloat[1]; } @@ -3236,7 +3277,6 @@ void ImGui::RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, cons LogRenderedText(&pos_min, text, text_display_end); } - // Another overly complex function until we reorganize everything into a nice all-in-one helper. // This is made more complex because we have dissociated the layout rectangle (pos_min..pos_max) which define _where_ the ellipsis is, from actual clipping of text and limit of the ellipsis display. // This is because in the context of tabs we selectively hide part of the text when the Close Button appears, but we don't want the ellipsis to move. @@ -3260,30 +3300,12 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con const ImFont* font = draw_list->_Data->Font; const float font_size = draw_list->_Data->FontSize; + const float font_scale = font_size / font->FontSize; const char* text_end_ellipsis = NULL; - - ImWchar ellipsis_char = font->EllipsisChar; - int ellipsis_char_count = 1; - if (ellipsis_char == (ImWchar)-1) - { - ellipsis_char = font->DotChar; - ellipsis_char_count = 3; - } - const ImFontGlyph* glyph = font->FindGlyph(ellipsis_char); - - float ellipsis_glyph_width = glyph->X1; // Width of the glyph with no padding on either side - float ellipsis_total_width = ellipsis_glyph_width; // Full width of entire ellipsis - - if (ellipsis_char_count > 1) - { - // Full ellipsis size without free spacing after it. - const float spacing_between_dots = 1.0f * (draw_list->_Data->FontSize / font->FontSize); - ellipsis_glyph_width = glyph->X1 - glyph->X0 + spacing_between_dots; - ellipsis_total_width = ellipsis_glyph_width * (float)ellipsis_char_count - spacing_between_dots; - } + const float ellipsis_width = font->EllipsisWidth * font_scale; // We can now claim the space between pos_max.x and ellipsis_max.x - const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_total_width) - pos_min.x, 1.0f); + const float text_avail_width = ImMax((ImMax(pos_max.x, ellipsis_max_x) - ellipsis_width) - pos_min.x, 1.0f); float text_size_clipped_x = font->CalcTextSizeA(font_size, text_avail_width, 0.0f, text, text_end_full, &text_end_ellipsis).x; if (text == text_end_ellipsis && text_end_ellipsis < text_end_full) { @@ -3300,13 +3322,10 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con // Render text, render ellipsis RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f)); - float ellipsis_x = pos_min.x + text_size_clipped_x; - if (ellipsis_x + ellipsis_total_width <= ellipsis_max_x) - for (int i = 0; i < ellipsis_char_count; i++) - { - font->RenderChar(draw_list, font_size, ImVec2(ellipsis_x, pos_min.y), GetColorU32(ImGuiCol_Text), ellipsis_char); - ellipsis_x += ellipsis_glyph_width; - } + ImVec2 ellipsis_pos = ImFloor(ImVec2(pos_min.x + text_size_clipped_x, pos_min.y)); + if (ellipsis_pos.x + ellipsis_width <= ellipsis_max_x) + for (int i = 0; i < font->EllipsisCharCount; i++, ellipsis_pos.x += font->EllipsisCharStep * font_scale) + font->RenderChar(draw_list, font_size, ellipsis_pos, GetColorU32(ImGuiCol_Text), font->EllipsisChar); } else { @@ -3379,24 +3398,26 @@ void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCurso { ImGuiContext& g = *GImGui; IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); + ImFontAtlas* font_atlas = g.DrawListSharedData.Font->ContainerAtlas; for (int n = 0; n < g.Viewports.Size; n++) { + // We scale cursor with current viewport/monitor, however Windows 10 for its own hardware cursor seems to be using a different scale factor. + ImVec2 offset, size, uv[4]; + if (!font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) + continue; ImGuiViewportP* viewport = g.Viewports[n]; + const ImVec2 pos = base_pos - offset; + const float scale = base_scale; + if (!viewport->GetMainRect().Overlaps(ImRect(pos, pos + ImVec2(size.x + 2, size.y + 2) * scale))) + continue; ImDrawList* draw_list = GetForegroundDrawList(viewport); - ImFontAtlas* font_atlas = draw_list->_Data->Font->ContainerAtlas; - ImVec2 offset, size, uv[4]; - if (font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) - { - const ImVec2 pos = base_pos - offset; - const float scale = base_scale; - ImTextureID tex_id = font_atlas->TexID; - draw_list->PushTextureID(tex_id); - draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); - draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border); - draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill); - draw_list->PopTextureID(); - } + ImTextureID tex_id = font_atlas->TexID; + draw_list->PushTextureID(tex_id); + draw_list->AddImage(tex_id, pos + ImVec2(1, 0) * scale, pos + (ImVec2(1, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos + ImVec2(2, 0) * scale, pos + (ImVec2(2, 0) + size) * scale, uv[2], uv[3], col_shadow); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[2], uv[3], col_border); + draw_list->AddImage(tex_id, pos, pos + size * scale, uv[0], uv[1], col_fill); + draw_list->PopTextureID(); } } @@ -3491,6 +3512,12 @@ void ImGui::Initialize() // Setup default localization table LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS)); + // Setup default platform clipboard/IME handlers. + g.IO.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations + g.IO.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; + g.IO.ClipboardUserData = (void*)&g; // Default implementation use the ImGuiContext as user data (ideally those would be arguments to the function) + g.IO.SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl; + // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); g.Viewports.push_back(viewport); @@ -3615,9 +3642,10 @@ void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type) //----------------------------------------------------------------------------- // ImGuiWindow is mostly a dumb struct. It merely has a constructor and a few helper methods -ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst(NULL) +ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, const char* name) : DrawListInst(NULL) { memset(this, 0, sizeof(*this)); + Ctx = ctx; Name = ImStrdup(name); NameBufLen = (int)strlen(name) + 1; ID = ImHashStr(name); @@ -3627,14 +3655,14 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) : DrawListInst ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f); AutoFitFramesX = AutoFitFramesY = -1; AutoPosLastDirection = ImGuiDir_None; - SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; + SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = 0; SetWindowPosVal = SetWindowPosPivot = ImVec2(FLT_MAX, FLT_MAX); LastFrameActive = -1; LastTimeActive = -1.0f; FontWindowScale = 1.0f; SettingsOffset = -1; DrawList = &DrawListInst; - DrawList->_Data = &context->DrawListSharedData; + DrawList->_Data = &Ctx->DrawListSharedData; DrawList->_OwnerName = Name; } @@ -3649,7 +3677,7 @@ ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; if (g.DebugHookIdInfo == id) ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); return id; @@ -3659,7 +3687,7 @@ ImGuiID ImGuiWindow::GetID(const void* ptr) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; if (g.DebugHookIdInfo == id) ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); return id; @@ -3669,7 +3697,7 @@ ImGuiID ImGuiWindow::GetID(int n) { ImGuiID seed = IDStack.back(); ImGuiID id = ImHashData(&n, sizeof(n), seed); - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; if (g.DebugHookIdInfo == id) ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); return id; @@ -3763,7 +3791,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) if (id) { g.ActiveIdIsAlive = id; - g.ActiveIdSource = (g.NavActivateId == id || g.NavActivateInputId == id || g.NavJustMovedToId == id) ? (ImGuiInputSource)ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + g.ActiveIdSource = (g.NavActivateId == id || g.NavJustMovedToId == id) ? (ImGuiInputSource)ImGuiInputSource_Nav : ImGuiInputSource_Mouse; } // Clear declaration of inputs claimed by the widget @@ -3891,7 +3919,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; // Special handling for calling after Begin() which represent the title bar or tab. - // When the window is collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. + // When the window is skipped/collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. if (g.LastItemData.ID == window->MoveId && window->WriteAccessed) return false; } @@ -3986,14 +4014,14 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) } // This is also inlined in ItemAdd() -// Note: if ImGuiItemStatusFlags_HasDisplayRect is set, user needs to set window->DC.LastItemDisplayRect! +// Note: if ImGuiItemStatusFlags_HasDisplayRect is set, user needs to set g.LastItemData.DisplayRect. void ImGui::SetLastItemData(ImGuiID item_id, ImGuiItemFlags in_flags, ImGuiItemStatusFlags item_flags, const ImRect& item_rect) { ImGuiContext& g = *GImGui; g.LastItemData.ID = item_id; g.LastItemData.InFlags = in_flags; g.LastItemData.StatusFlags = item_flags; - g.LastItemData.Rect = item_rect; + g.LastItemData.Rect = g.LastItemData.NavRect = item_rect; } float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) @@ -4358,6 +4386,11 @@ void ImGui::NewFrame() g.FramerateSecPerFrameCount = ImMin(g.FramerateSecPerFrameCount + 1, IM_ARRAYSIZE(g.FramerateSecPerFrame)); g.IO.Framerate = (g.FramerateSecPerFrameAccum > 0.0f) ? (1.0f / (g.FramerateSecPerFrameAccum / (float)g.FramerateSecPerFrameCount)) : FLT_MAX; + // Process input queue (trickle as many events as possible), turn events into writes to IO structure + g.InputEventsTrail.resize(0); + UpdateInputEvents(g.IO.ConfigInputTrickleEventQueue); + + // Update viewports (after processing input queue, so io.MouseHoveredViewport is set) UpdateViewportsNewFrame(); // Setup current font and draw list shared data @@ -4479,10 +4512,6 @@ void ImGui::NewFrame() //if (g.IO.AppFocusLost) // ClosePopupsExceptModals(); - // Process input queue (trickle as many events as possible) - g.InputEventsTrail.resize(0); - UpdateInputEvents(g.IO.ConfigInputTrickleEventQueue); - // Update keyboard input state UpdateKeyboardInputs(); @@ -4565,6 +4594,11 @@ void ImGui::NewFrame() UpdateDebugToolStackQueries(); if (g.DebugLocateFrames > 0 && --g.DebugLocateFrames == 0) g.DebugLocateId = 0; + if (g.DebugLogClipperAutoDisableFrames > 0 && --g.DebugLogClipperAutoDisableFrames == 0) + { + DebugLog("(Auto-disabled ImGuiDebugLogFlags_EventClipper to avoid spamming)\n"); + g.DebugLogFlags &= ~ImGuiDebugLogFlags_EventClipper; + } // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. @@ -4574,6 +4608,13 @@ void ImGui::NewFrame() Begin("Debug##Default"); IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); + // [DEBUG] When io.ConfigDebugBeginReturnValue is set, we make Begin()/BeginChild() return false at different level of the window-stack, + // allowing to validate correct Begin/End behavior in user code. + if (g.IO.ConfigDebugBeginReturnValueLoop) + g.DebugBeginReturnValueCullDepth = (g.DebugBeginReturnValueCullDepth == -1) ? 0 : ((g.DebugBeginReturnValueCullDepth + ((g.FrameCount % 4) == 0 ? 1 : 0)) % 10); + else + g.DebugBeginReturnValueCullDepth = -1; + CallContextHooks(&g, ImGuiContextHookType_NewFramePost); } @@ -4821,8 +4862,24 @@ void ImGui::EndFrame() ErrorCheckEndFrameSanityChecks(); // Notify Platform/OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) - if (g.IO.SetPlatformImeDataFn && memcmp(&g.PlatformImeData, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) - g.IO.SetPlatformImeDataFn(GetMainViewport(), &g.PlatformImeData); + ImGuiPlatformImeData* ime_data = &g.PlatformImeData; + if (g.IO.SetPlatformImeDataFn && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) + { + IMGUI_DEBUG_LOG_IO("Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); + ImGuiViewport* viewport = GetMainViewport(); +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (viewport->PlatformHandleRaw == NULL && g.IO.ImeWindowHandle != NULL) + { + viewport->PlatformHandleRaw = g.IO.ImeWindowHandle; + g.IO.SetPlatformImeDataFn(viewport, ime_data); + viewport->PlatformHandleRaw = NULL; + } + else +#endif + { + g.IO.SetPlatformImeDataFn(viewport, ime_data); + } + } // Hide implicit/fallback "Debug" window if it hasn't been used g.WithinFrameScopeWithImplicitWindow = false; @@ -5351,32 +5408,22 @@ static void UpdateWindowInFocusOrderList(ImGuiWindow* window, bool just_created, window->IsExplicitChild = new_is_explicit_child; } -static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) +static void InitOrLoadWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settings) { - ImGuiContext& g = *GImGui; - //IMGUI_DEBUG_LOG("CreateNewWindow '%s', flags = 0x%08X\n", name, flags); - - // Create window the first time - ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name); - window->Flags = flags; - g.WindowsById.SetVoidPtr(window->ID, window); - - // Default/arbitrary window position. Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. + // Initial window state with e.g. default/arbitrary window position + // Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); window->Pos = main_viewport->Pos + ImVec2(60, 60); + window->SetWindowPosAllowFlags = window->SetWindowSizeAllowFlags = window->SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; - // User can disable loading and saving of settings. Tooltip and child windows also don't store settings. - if (!(flags & ImGuiWindowFlags_NoSavedSettings)) - if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID)) - { - // Retrieve settings from .ini file - window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); - SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); - ApplyWindowSettings(window, settings); - } + if (settings != NULL) + { + SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false); + ApplyWindowSettings(window, settings); + } window->DC.CursorStartPos = window->DC.CursorMaxPos = window->DC.IdealMaxPos = window->Pos; // So first call to CalcWindowContentSizes() doesn't return crazy values - if ((flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) + if ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) != 0) { window->AutoFitFramesX = window->AutoFitFramesY = 2; window->AutoFitOnlyGrows = false; @@ -5389,6 +5436,23 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) window->AutoFitFramesY = 2; window->AutoFitOnlyGrows = (window->AutoFitFramesX > 0) || (window->AutoFitFramesY > 0); } +} + +static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) +{ + // Create window the first time + //IMGUI_DEBUG_LOG("CreateNewWindow '%s', flags = 0x%08X\n", name, flags); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name); + window->Flags = flags; + g.WindowsById.SetVoidPtr(window->ID, window); + + ImGuiWindowSettings* settings = NULL; + if (!(flags & ImGuiWindowFlags_NoSavedSettings)) + if ((settings = ImGui::FindWindowSettingsByWindow(window)) != 0) + window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings); + + InitOrLoadWindowSettings(window, settings); if (flags & ImGuiWindowFlags_NoBringToFrontOnFocus) g.Windows.push_front(window); // Quite slow but rare and only once @@ -5782,7 +5846,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar if (window->Collapsed) { // Title bar only - float backup_border_size = style.FrameBorderSize; + const float backup_border_size = style.FrameBorderSize; g.Style.FrameBorderSize = window->WindowBorderSize; ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && !g.NavDisableHighlight) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed); RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding); @@ -5834,12 +5898,15 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar { for (int resize_grip_n = 0; resize_grip_n < resize_grip_count; resize_grip_n++) { + const ImU32 col = resize_grip_col[resize_grip_n]; + if ((col & IM_COL32_A_MASK) == 0) + continue; const ImGuiResizeGripDef& grip = resize_grip_def[resize_grip_n]; const ImVec2 corner = ImLerp(window->Pos, window->Pos + window->Size, grip.CornerPosN); window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(window_border_size, resize_grip_draw_size) : ImVec2(resize_grip_draw_size, window_border_size))); window->DrawList->PathLineTo(corner + grip.InnerDir * ((resize_grip_n & 1) ? ImVec2(resize_grip_draw_size, window_border_size) : ImVec2(window_border_size, resize_grip_draw_size))); window->DrawList->PathArcToFast(ImVec2(corner.x + grip.InnerDir.x * (window_rounding + window_border_size), corner.y + grip.InnerDir.y * (window_rounding + window_border_size)), window_rounding, grip.AngleMin12, grip.AngleMax12); - window->DrawList->PathFillConvex(resize_grip_col[resize_grip_n]); + window->DrawList->PathFillConvex(col); } } @@ -6061,7 +6128,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) ImGuiWindowStackData window_stack_data; window_stack_data.Window = window; window_stack_data.ParentLastItemDataBackup = g.LastItemData; - window_stack_data.StackSizesOnBegin.SetToCurrentState(); + window_stack_data.StackSizesOnBegin.SetToContextState(&g); g.CurrentWindowStack.push_back(window_stack_data); if (flags & ImGuiWindowFlags_ChildMenu) g.BeginMenuCount++; @@ -6370,13 +6437,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } } - // [Test Engine] Register whole window in the item system + // [Test Engine] Register whole window in the item system (before submitting further decorations) #ifdef IMGUI_ENABLE_TEST_ENGINE if (g.TestEngineHookItems) { IM_ASSERT(window->IDStack.Size == 1); window->IDStack.Size = 0; // As window->IDStack[0] == window->ID here, make sure TestEngine doesn't erroneously see window as parent of itself. - IMGUI_TEST_ENGINE_ITEM_ADD(window->Rect(), window->ID); + IMGUI_TEST_ENGINE_ITEM_ADD(window->ID, window->Rect(), NULL); IMGUI_TEST_ENGINE_ITEM_INFO(window->ID, window->Name, (g.HoveredWindow == window) ? ImGuiItemStatusFlags_HoveredRect : 0); window->IDStack.Size = 1; } @@ -6617,10 +6684,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) DebugLocateItemResolveWithLastItem(); #endif - // [Test Engine] Register title bar / tab + // [Test Engine] Register title bar / tab with MoveId. #ifdef IMGUI_ENABLE_TEST_ENGINE if (!(window->Flags & ImGuiWindowFlags_NoTitleBar)) - IMGUI_TEST_ENGINE_ITEM_ADD(g.LastItemData.Rect, g.LastItemData.ID); + IMGUI_TEST_ENGINE_ITEM_ADD(g.LastItemData.ID, g.LastItemData.Rect, &g.LastItemData); #endif } else @@ -6682,6 +6749,15 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->SkipItems = skip_items; } + // [DEBUG] io.ConfigDebugBeginReturnValue override return value to test Begin/End and BeginChild/EndChild behaviors. + // (The implicit fallback window is NOT automatically ended allowing it to always be able to receive commands without crashing) + if (!window->IsFallbackWindow && ((g.IO.ConfigDebugBeginReturnValueOnce && window_just_created) || (g.IO.ConfigDebugBeginReturnValueLoop && g.DebugBeginReturnValueCullDepth == g.CurrentWindowStack.Size))) + { + if (window->AutoFitFramesX > 0) { window->AutoFitFramesX++; } + if (window->AutoFitFramesY > 0) { window->AutoFitFramesY++; } + return false; + } + return !window->SkipItems; } @@ -6721,7 +6797,7 @@ void ImGui::End() g.BeginMenuCount--; if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithCurrentState(); + g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithContextState(&g); g.CurrentWindowStack.pop_back(); SetCurrentWindow(g.CurrentWindowStack.Size == 0 ? NULL : g.CurrentWindowStack.back().Window); } @@ -6966,13 +7042,12 @@ void ImGui::EndDisabled() g.Style.Alpha = g.DisabledAlphaBackup; //PopStyleVar(); } -// FIXME: Look into renaming this once we have settled the new Focus/Activation/TabStop system. -void ImGui::PushAllowKeyboardFocus(bool allow_keyboard_focus) +void ImGui::PushTabStop(bool tab_stop) { - PushItemFlag(ImGuiItemFlags_NoTabStop, !allow_keyboard_focus); + PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); } -void ImGui::PopAllowKeyboardFocus() +void ImGui::PopTabStop() { PopItemFlag(); } @@ -7240,6 +7315,12 @@ void ImGui::SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const I window->HitTestHoleOffset = ImVec2ih(pos - window->Pos); } +void ImGui::SetWindowHiddendAndSkipItemsForCurrentFrame(ImGuiWindow* window) +{ + window->Hidden = window->SkipItems = true; + window->HiddenFramesCanSkipItems = 1; +} + void ImGui::SetWindowCollapsed(bool collapsed, ImGuiCond cond) { SetWindowCollapsed(GImGui->CurrentWindow, collapsed, cond); @@ -7447,7 +7528,7 @@ void ImGui::SetItemDefaultFocus() NavUpdateAnyRequestFlag(); // Scroll could be done in NavInitRequestApplyResult() via an opt-in flag (we however don't want regular init requests to scroll) - if (!IsItemVisible()) + if (!window->ClipRect.Contains(g.LastItemData.Rect)) ScrollToRectEx(window, g.LastItemData.Rect, ImGuiScrollFlags_None); } @@ -7517,6 +7598,15 @@ ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) return id; } +ImGuiID ImGui::GetIDWithSeed(int n, ImGuiID seed) +{ + ImGuiID id = ImHashData(&n, sizeof(n), seed); + ImGuiContext& g = *GImGui; + if (g.DebugHookIdInfo == id) + DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); + return id; +} + void ImGui::PopID() { ImGuiWindow* window = GImGui->CurrentWindow; @@ -7626,18 +7716,14 @@ ImGuiKeyData* ImGui::GetKeyData(ImGuiKey key) if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(key); - int index; #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO IM_ASSERT(key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_NamedKey_END); - if (IsLegacyKey(key)) - index = (g.IO.KeyMap[key] != -1) ? g.IO.KeyMap[key] : key; // Remap native->imgui or imgui->native - else - index = key; + if (IsLegacyKey(key) && g.IO.KeyMap[key] != -1) + key = (ImGuiKey)g.IO.KeyMap[key]; // Remap native->imgui or imgui->native #else IM_ASSERT(IsNamedKey(key) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend & user code."); - index = key - ImGuiKey_NamedKey_BEGIN; #endif - return &g.IO.KeysData[index]; + return &g.IO.KeysData[key - ImGuiKey_KeysData_OFFSET]; } #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -8491,7 +8577,8 @@ void ImGui::UpdateMouseWheel() // Mouse wheel scrolling // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead - // (we avoid doing it on OSX as it the OS input layer handles this already) + // - We avoid doing it on OSX as it the OS input layer handles this already. + // - However this means when running on OSX over Emcripten, Shift+WheelY will incur two swappings (1 in OS, 1 here), cancelling the feature. const bool swap_axis = g.IO.KeyShift && !g.IO.ConfigMacOSXBehaviors; if (swap_axis) { @@ -8873,6 +8960,13 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() // #define IM_ASSERT(EXPR) do { if (SomeCode(EXPR)) SomeMoreCode(); } while (0) // Correct! if (true) IM_ASSERT(1); else IM_ASSERT(0); + // Emscripten backends are often imprecise in their submission of DeltaTime. (#6114, #3644) + // Ideally the Emscripten app/backend should aim to fix or smooth this value and avoid feeding zero, but we tolerate it. +#ifdef __EMSCRIPTEN__ + if (g.IO.DeltaTime <= 0.0f && g.FrameCount > 0) + g.IO.DeltaTime = 0.00001f; +#endif + // Check user data // (We pass an error message in the assert expression to make it visible to programmers who are not using a debugger, as most assert handlers display their argument) IM_ASSERT(g.Initialized); @@ -8923,7 +9017,9 @@ static void ImGui::ErrorCheckEndFrameSanityChecks() { if (g.CurrentWindowStack.Size > 1) { + ImGuiWindow* window = g.CurrentWindowStack.back().Window; // <-- This window was not Ended! IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); + IM_UNUSED(window); while (g.CurrentWindowStack.Size > 1) End(); } @@ -9028,9 +9124,9 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo } // Save current stack sizes for later compare -void ImGuiStackSizes::SetToCurrentState() +void ImGuiStackSizes::SetToContextState(ImGuiContext* ctx) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; ImGuiWindow* window = g.CurrentWindow; SizeOfIDStack = (short)window->IDStack.Size; SizeOfColorStack = (short)g.ColorStack.Size; @@ -9044,9 +9140,9 @@ void ImGuiStackSizes::SetToCurrentState() } // Compare to detect usage errors -void ImGuiStackSizes::CompareWithCurrentState() +void ImGuiStackSizes::CompareWithContextState(ImGuiContext* ctx) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; ImGuiWindow* window = g.CurrentWindow; IM_UNUSED(window); @@ -9186,7 +9282,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu #ifdef IMGUI_ENABLE_TEST_ENGINE if (id != 0) - IMGUI_TEST_ENGINE_ITEM_ADD(nav_bb_arg ? *nav_bb_arg : bb, id); + IMGUI_TEST_ENGINE_ITEM_ADD(id, g.LastItemData.NavRect, &g.LastItemData); #endif // Clipping test @@ -9821,12 +9917,12 @@ void ImGui::SetScrollHereY(float center_y_ratio) // [SECTION] TOOLTIPS //----------------------------------------------------------------------------- -void ImGui::BeginTooltip() +bool ImGui::BeginTooltip() { - BeginTooltipEx(ImGuiTooltipFlags_None, ImGuiWindowFlags_None); + return BeginTooltipEx(ImGuiTooltipFlags_None, ImGuiWindowFlags_None); } -void ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags) +bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags) { ImGuiContext& g = *GImGui; @@ -9850,12 +9946,17 @@ void ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags ext if (window->Active) { // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. - window->Hidden = true; - window->HiddenFramesCanSkipItems = 1; // FIXME: This may not be necessary? + SetWindowHiddendAndSkipItemsForCurrentFrame(window); ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; Begin(window_name, NULL, flags | extra_window_flags); + // 2023-03-09: Added bool return value to the API, but currently always returning true. + // If this ever returns false we need to update BeginDragDropSource() accordingly. + //if (!ret) + // End(); + //return ret; + return true; } void ImGui::EndTooltip() @@ -9866,7 +9967,8 @@ void ImGui::EndTooltip() void ImGui::SetTooltipV(const char* fmt, va_list args) { - BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None); + if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None)) + return; TextV(fmt, args); EndTooltip(); } @@ -9948,7 +10050,7 @@ void ImGui::OpenPopup(const char* str_id, ImGuiPopupFlags popup_flags) { ImGuiContext& g = *GImGui; ImGuiID id = g.CurrentWindow->GetID(str_id); - IMGUI_DEBUG_LOG_POPUP("[popup] OpenPopup(\"%s\" -> 0x%08X\n", str_id, id); + IMGUI_DEBUG_LOG_POPUP("[popup] OpenPopup(\"%s\" -> 0x%08X)\n", str_id, id); OpenPopupEx(id, popup_flags); } @@ -9968,7 +10070,7 @@ void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags) const int current_stack_size = g.BeginPopupStack.Size; if (popup_flags & ImGuiPopupFlags_NoOpenOverExistingPopup) - if (IsPopupOpen(0u, ImGuiPopupFlags_AnyPopupId)) + if (IsPopupOpen((ImGuiID)0, ImGuiPopupFlags_AnyPopupId)) return; ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. @@ -10680,30 +10782,25 @@ static void ImGui::NavProcessItem() // Process Move Request (scoring for navigation) // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRect + scoring from a rect wrapped according to current wrapping policy) - if (g.NavMoveScoringItems) + if (g.NavMoveScoringItems && (item_flags & ImGuiItemFlags_Disabled) == 0) { - const bool is_tab_stop = (item_flags & ImGuiItemFlags_Inputable) && (item_flags & (ImGuiItemFlags_NoTabStop | ImGuiItemFlags_Disabled)) == 0; const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) != 0; if (is_tabbing) { - if (is_tab_stop || (g.NavMoveFlags & ImGuiNavMoveFlags_FocusApi)) - NavProcessItemForTabbingRequest(id); + NavProcessItemForTabbingRequest(id, item_flags, g.NavMoveFlags); } - else if ((g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) && !(item_flags & ImGuiItemFlags_Disabled)) + else if (g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) { ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; - if (!is_tabbing) - { - if (NavScoreItem(result)) - NavApplyItemToResult(result); - - // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. - const float VISIBLE_RATIO = 0.70f; - if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) - if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) - if (NavScoreItem(&g.NavMoveResultLocalVisible)) - NavApplyItemToResult(&g.NavMoveResultLocalVisible); - } + if (NavScoreItem(result)) + NavApplyItemToResult(result); + + // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. + const float VISIBLE_RATIO = 0.70f; + if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) + if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) + if (NavScoreItem(&g.NavMoveResultLocalVisible)) + NavApplyItemToResult(&g.NavMoveResultLocalVisible); } } @@ -10726,18 +10823,31 @@ static void ImGui::NavProcessItem() // - Case 3: tab forward wrap: set result to first eligible item (preemptively), on ref id set counter, on next frame if counter hasn't elapsed store result. // FIXME-TABBING: Could be done as a next-frame forwarded request // - Case 4: tab backward: store all results, on ref id pick prev, stop storing // - Case 5: tab backward wrap: store all results, on ref id if no result keep storing until last // FIXME-TABBING: Could be done as next-frame forwarded requested -void ImGui::NavProcessItemForTabbingRequest(ImGuiID id) +void ImGui::NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flags, ImGuiNavMoveFlags move_flags) { ImGuiContext& g = *GImGui; + if ((move_flags & ImGuiNavMoveFlags_FocusApi) == 0) + if (g.NavLayer != g.CurrentWindow->DC.NavLayerCurrent) + return; + + // - Can always land on an item when using API call. + // - Tabbing with _NavEnableKeyboard (space/enter/arrows): goes through every item. + // - Tabbing without _NavEnableKeyboard: goes through inputable items only. + bool can_stop; + if (move_flags & ImGuiNavMoveFlags_FocusApi) + can_stop = true; + else + can_stop = (item_flags & ImGuiItemFlags_NoTabStop) == 0 && ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) || (item_flags & ImGuiItemFlags_Inputable)); + // Always store in NavMoveResultLocal (unlike directional request which uses NavMoveResultOther on sibling/flattened windows) ImGuiNavItemData* result = &g.NavMoveResultLocal; if (g.NavTabbingDir == +1) { // Tab Forward or SetKeyboardFocusHere() with >= 0 - if (g.NavTabbingResultFirst.ID == 0) + if (can_stop && g.NavTabbingResultFirst.ID == 0) NavApplyItemToResult(&g.NavTabbingResultFirst); - if (--g.NavTabbingCounter == 0) + if (can_stop && g.NavTabbingCounter > 0 && --g.NavTabbingCounter == 0) NavMoveRequestResolveWithLastItem(result); else if (g.NavId == id) g.NavTabbingCounter = 1; @@ -10753,16 +10863,18 @@ void ImGui::NavProcessItemForTabbingRequest(ImGuiID id) NavUpdateAnyRequestFlag(); } } - else + else if (can_stop) { + // Keep applying until reaching NavId NavApplyItemToResult(result); } } else if (g.NavTabbingDir == 0) { - // Tab Init - if (g.NavTabbingResultFirst.ID == 0) - NavMoveRequestResolveWithLastItem(&g.NavTabbingResultFirst); + if (can_stop && g.NavId == id) + NavMoveRequestResolveWithLastItem(result); + if (can_stop && g.NavTabbingResultFirst.ID == 0) // Tab init + NavApplyItemToResult(&g.NavTabbingResultFirst); } } @@ -11039,7 +11151,7 @@ static void ImGui::NavUpdate() NavUpdateCancelRequest(); // Process manual activation request - g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavActivateInputId = 0; + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = 0; g.NavActivateFlags = ImGuiActivateFlags_None; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { @@ -11054,12 +11166,12 @@ static void ImGui::NavUpdate() } if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && input_pressed) { - g.NavActivateInputId = g.NavId; + g.NavActivateId = g.NavId; g.NavActivateFlags = ImGuiActivateFlags_PreferInput; } - if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_down) + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && (activate_down || input_down)) g.NavActivateDownId = g.NavId; - if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && activate_pressed) + if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && (activate_pressed || input_pressed)) g.NavActivatePressedId = g.NavId; } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) @@ -11071,10 +11183,7 @@ static void ImGui::NavUpdate() // FIXME-NAV: Those should eventually be queued (unlike focus they don't cancel each others) if (g.NavNextActivateId != 0) { - if (g.NavNextActivateFlags & ImGuiActivateFlags_PreferInput) - g.NavActivateInputId = g.NavNextActivateId; - else - g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavNextActivateId; + g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = g.NavNextActivateId; g.NavActivateFlags = g.NavNextActivateFlags; } g.NavNextActivateId = 0; @@ -11229,16 +11338,21 @@ void ImGui::NavUpdateCreateMoveRequest() } // When using gamepad, we project the reference nav bounding box into window visible area. - // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, since with gamepad all movements are relative - // (can't focus a visible object like we can with the mouse). + // This is to allow resuming navigation inside the visible area after doing a large amount of scrolling, + // since with gamepad all movements are relative (can't focus a visible object like we can with the mouse). if (g.NavMoveSubmitted && g.NavInputSource == ImGuiInputSource_Gamepad && g.NavLayer == ImGuiNavLayer_Main && window != NULL)// && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded)) { bool clamp_x = (g.NavMoveFlags & (ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapX)) == 0; bool clamp_y = (g.NavMoveFlags & (ImGuiNavMoveFlags_LoopY | ImGuiNavMoveFlags_WrapY)) == 0; ImRect inner_rect_rel = WindowRectAbsToRel(window, ImRect(window->InnerRect.Min - ImVec2(1, 1), window->InnerRect.Max + ImVec2(1, 1))); + + // Take account of changing scroll to handle triggering a new move request on a scrolling frame. (#6171) + // Otherwise 'inner_rect_rel' would be off on the move result frame. + inner_rect_rel.Translate(CalcNextScrollFromScrollTargetAndClamp(window) - window->Scroll); + if ((clamp_x || clamp_y) && !inner_rect_rel.Contains(window->NavRectRel[g.NavLayer])) { - //IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel for gamepad move\n"); + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: clamp NavRectRel for gamepad move\n"); float pad_x = ImMin(inner_rect_rel.GetWidth(), window->CalcFontSize() * 0.5f); float pad_y = ImMin(inner_rect_rel.GetHeight(), window->CalcFontSize() * 0.5f); // Terrible approximation for the intent of starting navigation from first fully visible item inner_rect_rel.Min.x = clamp_x ? (inner_rect_rel.Min.x + pad_x) : -FLT_MAX; @@ -11283,8 +11397,11 @@ void ImGui::NavUpdateCreateTabbingRequest() // (this is ALWAYS ENABLED, regardless of ImGuiConfigFlags_NavEnableKeyboard flag!) // Initially this was designed to use counters and modulo arithmetic, but that could not work with unsubmitted items (list clipper). Instead we use a strategy close to other move requests. // See NavProcessItemForTabbingRequest() for a description of the various forward/backward tabbing cases with and without wrapping. - //// FIXME: We use (g.ActiveId == 0) but (g.NavDisableHighlight == false) might be righter once we can tab through anything - g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; + const bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; + if (nav_keyboard_active) + g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.NavDisableHighlight == true && g.ActiveId == 0) ? 0 : +1; + else + g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; ImGuiDir clip_dir = (g.NavTabbingDir < 0) ? ImGuiDir_Up : ImGuiDir_Down; NavMoveRequestSubmit(ImGuiDir_None, clip_dir, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. @@ -11304,7 +11421,7 @@ void ImGui::NavMoveRequestApplyResult() ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : (g.NavMoveResultOther.ID != 0) ? &g.NavMoveResultOther : NULL; // Tabbing forward wrap - if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && result == NULL) if ((g.NavTabbingCounter == 1 || g.NavTabbingDir == 0) && g.NavTabbingResultFirst.ID) result = &g.NavTabbingResultFirst; @@ -11974,13 +12091,12 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) { // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. - BeginTooltip(); + bool ret = BeginTooltip(); + IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddendAndSkipItemsForCurrentFrame(). + IM_UNUSED(ret); + if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) - { - ImGuiWindow* tooltip_window = g.CurrentWindow; - tooltip_window->Hidden = tooltip_window->SkipItems = true; - tooltip_window->HiddenFramesCanSkipItems = 1; - } + SetWindowHiddendAndSkipItemsForCurrentFrame(g.CurrentWindow); } if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) @@ -12129,12 +12245,13 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop const bool was_accepted_previously = (g.DragDropAcceptIdPrev == g.DragDropTargetId); ImRect r = g.DragDropTargetRect; float r_surface = r.GetWidth() * r.GetHeight(); - if (r_surface <= g.DragDropAcceptIdCurrRectSurface) - { - g.DragDropAcceptFlags = flags; - g.DragDropAcceptIdCurr = g.DragDropTargetId; - g.DragDropAcceptIdCurrRectSurface = r_surface; - } + if (r_surface > g.DragDropAcceptIdCurrRectSurface) + return NULL; + + g.DragDropAcceptFlags = flags; + g.DragDropAcceptIdCurr = g.DragDropTargetId; + g.DragDropAcceptIdCurrRectSurface = r_surface; + //IMGUI_DEBUG_LOG("AcceptDragDropPayload(): %08X: accept\n", g.DragDropTargetId); // Render default drop visuals payload.Preview = was_accepted_previously; @@ -12143,10 +12260,11 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop window->DrawList->AddRect(r.Min - ImVec2(3.5f,3.5f), r.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); g.DragDropAcceptFrameCount = g.FrameCount; - payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting os window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() + payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting OS window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery)) return NULL; + //IMGUI_DEBUG_LOG("AcceptDragDropPayload(): %08X: return payload\n", g.DragDropTargetId); return &payload; } @@ -12162,13 +12280,16 @@ const ImGuiPayload* ImGui::GetDragDropPayload() return (g.DragDropActive && g.DragDropPayload.DataFrameCount != -1) ? &g.DragDropPayload : NULL; } -// We don't really use/need this now, but added it for the sake of consistency and because we might need it later. void ImGui::EndDragDropTarget() { ImGuiContext& g = *GImGui; IM_ASSERT(g.DragDropActive); IM_ASSERT(g.DragDropWithinTarget); g.DragDropWithinTarget = false; + + // Clear drag and drop state payload right after delivery + if (g.DragDropPayload.Delivery) + ClearDragDrop(); } //----------------------------------------------------------------------------- @@ -12402,10 +12523,10 @@ void ImGui::LogButtons() #endif const bool log_to_file = Button("Log To File"); SameLine(); const bool log_to_clipboard = Button("Log To Clipboard"); SameLine(); - PushAllowKeyboardFocus(false); + PushTabStop(false); SetNextItemWidth(80.0f); SliderInt("Default Depth", &g.LogDepthToExpandDefault, 0, 9, NULL); - PopAllowKeyboardFocus(); + PopTabStop(); PopID(); // Start logging at the end of the function so that the buttons don't appear in the log @@ -12423,15 +12544,17 @@ void ImGui::LogButtons() //----------------------------------------------------------------------------- // - UpdateSettings() [Internal] // - MarkIniSettingsDirty() [Internal] -// - CreateNewWindowSettings() [Internal] -// - FindWindowSettings() [Internal] -// - FindOrCreateWindowSettings() [Internal] // - FindSettingsHandler() [Internal] // - ClearIniSettings() [Internal] // - LoadIniSettingsFromDisk() // - LoadIniSettingsFromMemory() // - SaveIniSettingsToDisk() // - SaveIniSettingsToMemory() +//----------------------------------------------------------------------------- +// - CreateNewWindowSettings() [Internal] +// - FindWindowSettingsByID() [Internal] +// - FindWindowSettingsByWindow() [Internal] +// - ClearWindowSettings() [Internal] // - WindowSettingsHandler_***() [Internal] //----------------------------------------------------------------------------- @@ -12478,44 +12601,6 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window) g.SettingsDirtyTimer = g.IO.IniSavingRate; } -ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) -{ - ImGuiContext& g = *GImGui; - -#if !IMGUI_DEBUG_INI_SETTINGS - // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() - // Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier. - if (const char* p = strstr(name, "###")) - name = p; -#endif - const size_t name_len = strlen(name); - - // Allocate chunk - const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1; - ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size); - IM_PLACEMENT_NEW(settings) ImGuiWindowSettings(); - settings->ID = ImHashStr(name, name_len); - memcpy(settings->GetName(), name, name_len + 1); // Store with zero terminator - - return settings; -} - -ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id) -{ - ImGuiContext& g = *GImGui; - for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) - if (settings->ID == id) - return settings; - return NULL; -} - -ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name) -{ - if (ImGuiWindowSettings* settings = FindWindowSettings(ImHashStr(name))) - return settings; - return CreateNewWindowSettings(name); -} - void ImGui::AddSettingsHandler(const ImGuiSettingsHandler* handler) { ImGuiContext& g = *GImGui; @@ -12540,6 +12625,7 @@ ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) return NULL; } +// Clear all settings (windows, tables, docking etc.) void ImGui::ClearIniSettings() { ImGuiContext& g = *GImGui; @@ -12664,6 +12750,62 @@ const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) return g.SettingsIniData.c_str(); } +ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) +{ + ImGuiContext& g = *GImGui; + +#if !IMGUI_DEBUG_INI_SETTINGS + // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() + // Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier. + if (const char* p = strstr(name, "###")) + name = p; +#endif + const size_t name_len = strlen(name); + + // Allocate chunk + const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1; + ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size); + IM_PLACEMENT_NEW(settings) ImGuiWindowSettings(); + settings->ID = ImHashStr(name, name_len); + memcpy(settings->GetName(), name, name_len + 1); // Store with zero terminator + + return settings; +} + +// We don't provide a FindWindowSettingsByName() because Docking system doesn't always hold on names. +// This is called once per window .ini entry + once per newly instantiated window. +ImGuiWindowSettings* ImGui::FindWindowSettingsByID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) + if (settings->ID == id) + return settings; + return NULL; +} + +// This is faster if you are holding on a Window already as we don't need to perform a search. +ImGuiWindowSettings* ImGui::FindWindowSettingsByWindow(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + if (window->SettingsOffset != -1) + return g.SettingsWindows.ptr_from_offset(window->SettingsOffset); + return FindWindowSettingsByID(window->ID); +} + +// This will revert window to its initial state, including enabling the ImGuiCond_FirstUseEver/ImGuiCond_Once conditions once more. +void ImGui::ClearWindowSettings(const char* name) +{ + //IMGUI_DEBUG_LOG("ClearWindowSettings('%s')\n", name); + ImGuiWindow* window = FindWindowByName(name); + if (window != NULL) + { + window->Flags |= ImGuiWindowFlags_NoSavedSettings; + InitOrLoadWindowSettings(window, NULL); + } + if (ImGuiWindowSettings* settings = window ? FindWindowSettingsByWindow(window) : FindWindowSettingsByID(ImHashStr(name))) + settings->WantDelete = true; +} + static void WindowSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) { ImGuiContext& g = *ctx; @@ -12674,9 +12816,12 @@ static void WindowSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandl static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) { - ImGuiWindowSettings* settings = ImGui::FindOrCreateWindowSettings(name); - ImGuiID id = settings->ID; - *settings = ImGuiWindowSettings(); // Clear existing if recycling previous entry + ImGuiID id = ImHashStr(name); + ImGuiWindowSettings* settings = ImGui::FindWindowSettingsByID(id); + if (settings) + *settings = ImGuiWindowSettings(); // Clear existing if recycling previous entry + else + settings = ImGui::CreateNewWindowSettings(name); settings->ID = id; settings->WantApply = true; return (void*)settings; @@ -12716,7 +12861,7 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl if (window->Flags & ImGuiWindowFlags_NoSavedSettings) continue; - ImGuiWindowSettings* settings = (window->SettingsOffset != -1) ? g.SettingsWindows.ptr_from_offset(window->SettingsOffset) : ImGui::FindWindowSettings(window->ID); + ImGuiWindowSettings* settings = ImGui::FindWindowSettingsByWindow(window); if (!settings) { settings = ImGui::CreateNewWindowSettings(window->Name); @@ -12727,12 +12872,15 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl settings->Size = ImVec2ih(window->SizeFull); settings->Collapsed = window->Collapsed; + settings->WantDelete = false; } // Write to text buffer buf->reserve(buf->size() + g.SettingsWindows.size() * 6); // ballpark reserve for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) { + if (settings->WantDelete) + continue; const char* settings_name = settings->GetName(); buf->appendf("[%s][%s]\n", handler->TypeName, settings_name); buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y); @@ -12820,9 +12968,9 @@ static void ImGui::UpdateViewportsNewFrame() // Win32 clipboard implementation // We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown() -static const char* GetClipboardTextFn_DefaultImpl(void*) +static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *(ImGuiContext*)user_data_ctx; g.ClipboardHandlerData.clear(); if (!::OpenClipboard(NULL)) return NULL; @@ -12883,8 +13031,9 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text) } } -static const char* GetClipboardTextFn_DefaultImpl(void*) +static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) { + ImGuiContext& g = *(ImGuiContext*)user_data_ctx; if (!main_clipboard) PasteboardCreate(kPasteboardClipboard, &main_clipboard); PasteboardSynchronize(main_clipboard); @@ -12902,7 +13051,6 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) CFDataRef cf_data; if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr) { - ImGuiContext& g = *GImGui; g.ClipboardHandlerData.clear(); int length = (int)CFDataGetLength(cf_data); g.ClipboardHandlerData.resize(length + 1); @@ -12919,15 +13067,15 @@ static const char* GetClipboardTextFn_DefaultImpl(void*) #else // Local Dear ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers. -static const char* GetClipboardTextFn_DefaultImpl(void*) +static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *(ImGuiContext*)user_data_ctx; return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin(); } -static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *(ImGuiContext*)user_data_ctx; g.ClipboardHandlerData.clear(); const char* text_end = text + strlen(text); g.ClipboardHandlerData.resize((int)(text_end - text) + 1); @@ -12949,10 +13097,6 @@ static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatf { // Notify OS Input Method Editor of text input position HWND hwnd = (HWND)viewport->PlatformHandleRaw; -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - if (hwnd == 0) - hwnd = (HWND)ImGui::GetIO().ImeWindowHandle; -#endif if (hwnd == 0) return; @@ -13144,9 +13288,8 @@ void ImGui::DebugTextEncoding(const char* str) static void MetricsHelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) && ImGui::BeginTooltip()) { - ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); ImGui::TextUnformatted(desc); ImGui::PopTextWrapPos(); @@ -13164,10 +13307,13 @@ void ImGui::ShowFontAtlas(ImFontAtlas* atlas) DebugNodeFont(font); PopID(); } - if (TreeNode("Atlas texture", "Atlas texture (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) + if (TreeNode("Font Atlas", "Font Atlas (%dx%d pixels)", atlas->TexWidth, atlas->TexHeight)) { - ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); - ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); + ImGuiContext& g = *GImGui; + ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; + Checkbox("Tint with Text Color", &cfg->ShowAtlasTintedWithTextColor); // Using text color ensure visibility of core atlas data, but will alter custom colored icons + ImVec4 tint_col = cfg->ShowAtlasTintedWithTextColor ? GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + ImVec4 border_col = GetStyleColorVec4(ImGuiCol_Border); Image(atlas->TexID, ImVec2((float)atlas->TexWidth, (float)atlas->TexHeight), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f), tint_col, border_col); TreePop(); } @@ -13339,6 +13485,10 @@ void ImGui::ShowMetricsWindow(bool* p_open) } } + Checkbox("Debug Begin/BeginChild return value", &io.ConfigDebugBeginReturnValueLoop); + SameLine(); + MetricsHelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running."); + TreePop(); } @@ -13507,7 +13657,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("KEYBOARD/GAMEPAD/MOUSE KEYS"); { // We iterate both legacy native range and named ImGuiKey ranges, which is a little odd but this allows displaying the data for old/new backends. - // User code should never have to go through such hoops: old code may use native keycodes, new code may use ImGuiKey codes. + // User code should never have to go through such hoops! You can generally iterate between ImGuiKey_NamedKey_BEGIN and ImGuiKey_NamedKey_END. Indent(); #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; @@ -13623,7 +13773,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) DebugLocateItemOnHover(g.NavId); Text("NavInputSource: %s", GetInputSourceName(g.NavInputSource)); Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); - Text("NavActivateId/DownId/PressedId/InputId: %08X/%08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId, g.NavActivateInputId); + Text("NavActivateId/DownId/PressedId: %08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId); Text("NavActivateFlags: %04X", g.NavActivateFlags); Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); @@ -13907,9 +14057,8 @@ void ImGui::DebugNodeFont(ImFont* font) if (!glyph) continue; font->RenderChar(draw_list, cell_size, cell_p1, glyph_col, (ImWchar)(base + n)); - if (IsMouseHoveringRect(cell_p1, cell_p2)) + if (IsMouseHoveringRect(cell_p1, cell_p2) && BeginTooltip()) { - BeginTooltip(); DebugNodeFontGlyph(font, glyph); EndTooltip(); } @@ -13959,7 +14108,7 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; p += ImFormatString(p, buf_end - p, "%s'%s'", - tab_n > 0 ? ", " : "", (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "???"); + tab_n > 0 ? ", " : "", TabBarGetTabName(tab_bar, tab)); } p += ImFormatString(p, buf_end - p, (tab_bar->Tabs.Size > 3) ? " ... }" : " } "); if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } @@ -13976,12 +14125,12 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) { for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { - const ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; + ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; PushID(tab); if (SmallButton("<")) { TabBarQueueReorder(tab_bar, tab, -1); } SameLine(0, 2); if (SmallButton(">")) { TabBarQueueReorder(tab_bar, tab, +1); } SameLine(); Text("%02d%c Tab 0x%08X '%s' Offset: %.2f, Width: %.2f/%.2f", - tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, (tab->NameOffset != -1) ? tab_bar->GetTabName(tab) : "???", tab->Offset, tab->Width, tab->ContentWidth); + tab_n, (tab->ID == tab_bar->SelectedTabId) ? '*' : ' ', tab->ID, TabBarGetTabName(tab_bar, tab), tab->Offset, tab->Width, tab->ContentWidth); PopID(); } TreePop(); @@ -14065,8 +14214,12 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings* settings) { + if (settings->WantDelete) + BeginDisabled(); Text("0x%08X \"%s\" Pos (%d,%d) Size (%d,%d) Collapsed=%d", settings->ID, settings->GetName(), settings->Pos.x, settings->Pos.y, settings->Size.x, settings->Size.y, settings->Collapsed); + if (settings->WantDelete) + EndDisabled(); } void ImGui::DebugNodeWindowsList(ImVector* windows, const char* label) @@ -14141,7 +14294,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) SameLine(); CheckboxFlags("Focus", &g.DebugLogFlags, ImGuiDebugLogFlags_EventFocus); SameLine(); CheckboxFlags("Popup", &g.DebugLogFlags, ImGuiDebugLogFlags_EventPopup); SameLine(); CheckboxFlags("Nav", &g.DebugLogFlags, ImGuiDebugLogFlags_EventNav); - SameLine(); CheckboxFlags("Clipper", &g.DebugLogFlags, ImGuiDebugLogFlags_EventClipper); + SameLine(); if (CheckboxFlags("Clipper", &g.DebugLogFlags, ImGuiDebugLogFlags_EventClipper)) { g.DebugLogClipperAutoDisableFrames = 2; } if (IsItemHovered()) SetTooltip("Clipper log auto-disabled after 2 frames"); SameLine(); CheckboxFlags("IO", &g.DebugLogFlags, ImGuiDebugLogFlags_EventIO); if (SmallButton("Clear")) @@ -14242,7 +14395,8 @@ void ImGui::UpdateDebugToolItemPicker() if (change_mapping && IsMouseClicked(mouse_button)) g.DebugItemPickerMouseButton = (ImU8)mouse_button; SetNextWindowBgAlpha(0.70f); - BeginTooltip(); + if (!BeginTooltip()) + return; Text("HoveredId: 0x%08X", hovered_id); Text("Press ESC to abort picking."); const char* mouse_button_names[] = { "Left", "Right", "Middle" }; diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index c1b7dbe5..6f6e73d4 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (demo code) // Help: @@ -38,7 +38,7 @@ // - We try to declare static variables in the local scope, as close as possible to the code using them. // - We never use any of the helpers/facilities used internally by Dear ImGui, unless available in the public API. // - We never use maths operators on ImVec2/ImVec4. For our other sources files we use them, and they are provided -// by imgui_internal.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional +// by imgui.h using the IMGUI_DEFINE_MATH_OPERATORS define. For your own sources file they are optional // and require you either enable those, either provide your own via IM_VEC2_CLASS_EXTRA in imconfig.h. // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. @@ -211,9 +211,8 @@ static void ShowDemoWindowInputs(); static void HelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort)) + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) && ImGui::BeginTooltip()) { - ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); ImGui::TextUnformatted(desc); ImGui::PopTextWrapPos(); @@ -426,6 +425,7 @@ void ImGui::ShowDemoWindow(bool* p_open) if (ImGui::TreeNode("Configuration##2")) { + ImGui::SeparatorText("General"); ImGui::CheckboxFlags("io.ConfigFlags: NavEnableKeyboard", &io.ConfigFlags, ImGuiConfigFlags_NavEnableKeyboard); ImGui::SameLine(); HelpMarker("Enable keyboard controls."); ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", &io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); @@ -448,6 +448,10 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility."); ImGui::Checkbox("io.ConfigInputTrickleEventQueue", &io.ConfigInputTrickleEventQueue); ImGui::SameLine(); HelpMarker("Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates."); + ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); + ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + + ImGui::SeparatorText("Widgets"); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting)."); ImGui::Checkbox("io.ConfigInputTextEnterKeepActive", &io.ConfigInputTextEnterKeepActive); @@ -457,11 +461,19 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); - ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); - ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + ImGui::Checkbox("io.ConfigMacOSXBehaviors", &io.ConfigMacOSXBehaviors); ImGui::Text("Also see Style->Rendering for rendering options."); + + ImGui::SeparatorText("Debug"); + ImGui::BeginDisabled(); + ImGui::Checkbox("io.ConfigDebugBeginReturnValueOnce", &io.ConfigDebugBeginReturnValueOnce); // . + ImGui::EndDisabled(); + ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover"); + ImGui::Checkbox("io.ConfigDebugBeginReturnValueLoop", &io.ConfigDebugBeginReturnValueLoop); + ImGui::SameLine(); HelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running."); + ImGui::TreePop(); - ImGui::Separator(); + ImGui::Spacing(); } IMGUI_DEMO_MARKER("Configuration/Backend Flags"); @@ -479,7 +491,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &backend_flags, ImGuiBackendFlags_HasSetMousePos); ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); ImGui::TreePop(); - ImGui::Separator(); + ImGui::Spacing(); } IMGUI_DEMO_MARKER("Configuration/Style"); @@ -488,7 +500,7 @@ void ImGui::ShowDemoWindow(bool* p_open) HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function."); ImGui::ShowStyleEditor(); ImGui::TreePop(); - ImGui::Separator(); + ImGui::Spacing(); } IMGUI_DEMO_MARKER("Configuration/Capture, Logging"); @@ -556,6 +568,8 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Basic"); if (ImGui::TreeNode("Basic")) { + ImGui::SeparatorText("General"); + IMGUI_DEMO_MARKER("Widgets/Basic/Button"); static int clicked = 0; if (ImGui::Button("Button")) @@ -610,20 +624,42 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); ImGui::Text("%d", counter); - ImGui::Separator(); - ImGui::LabelText("label", "Value"); - { - // Using the _simplified_ one-liner Combo() api here - // See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api. - IMGUI_DEMO_MARKER("Widgets/Basic/Combo"); - const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; - static int item_current = 0; - ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); - ImGui::SameLine(); HelpMarker( - "Using the simplified one-liner Combo API here.\nRefer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API."); + // Tooltips + IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); + //ImGui::AlignTextToFramePadding(); + ImGui::Text("Tooltips:"); + + ImGui::SameLine(); + ImGui::SmallButton("Button"); + if (ImGui::IsItemHovered()) + ImGui::SetTooltip("I am a tooltip"); + + ImGui::SameLine(); + ImGui::SmallButton("Fancy"); + if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) + { + ImGui::Text("I am a fancy tooltip"); + static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; + ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); + ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime())); + ImGui::EndTooltip(); + } + + ImGui::SameLine(); + ImGui::SmallButton("Delayed"); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal)) // With a delay + ImGui::SetTooltip("I am a tooltip with a delay."); + + ImGui::SameLine(); + HelpMarker( + "Tooltip are created by using the IsItemHovered() function over any kind of item."); } + ImGui::LabelText("label", "Value"); + + ImGui::SeparatorText("Inputs"); + { // To wire InputText() with std::string or any other custom string type, // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. @@ -666,6 +702,8 @@ static void ShowDemoWindowWidgets() ImGui::InputFloat3("input float3", vec4a); } + ImGui::SeparatorText("Drags"); + { IMGUI_DEMO_MARKER("Widgets/Basic/DragInt, DragFloat"); static int i1 = 50, i2 = 42; @@ -682,6 +720,8 @@ static void ShowDemoWindowWidgets() ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns"); } + ImGui::SeparatorText("Sliders"); + { IMGUI_DEMO_MARKER("Widgets/Basic/SliderInt, SliderFloat"); static int i1 = 0; @@ -708,6 +748,8 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); HelpMarker("Using the format string parameter to display a name instead of the underlying integer."); } + ImGui::SeparatorText("Selectors/Pickers"); + { IMGUI_DEMO_MARKER("Widgets/Basic/ColorEdit3, ColorEdit4"); static float col1[3] = { 1.0f, 0.0f, 0.2f }; @@ -722,6 +764,17 @@ static void ShowDemoWindowWidgets() ImGui::ColorEdit4("color 2", col2); } + { + // Using the _simplified_ one-liner Combo() api here + // See "Combo" section for examples of how to use the more flexible BeginCombo()/EndCombo() api. + IMGUI_DEMO_MARKER("Widgets/Basic/Combo"); + const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIIIIII", "JJJJ", "KKKKKKK" }; + static int item_current = 0; + ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); + ImGui::SameLine(); HelpMarker( + "Using the simplified one-liner Combo API here.\nRefer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API."); + } + { // Using the _simplified_ one-liner ListBox() api here // See "List boxes" section for examples of how to use the more flexible BeginListBox()/EndListBox() api. @@ -733,40 +786,6 @@ static void ShowDemoWindowWidgets() "Using the simplified one-liner ListBox API here.\nRefer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API."); } - { - // Tooltips - IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); - ImGui::AlignTextToFramePadding(); - ImGui::Text("Tooltips:"); - - ImGui::SameLine(); - ImGui::Button("Button"); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("I am a tooltip"); - - ImGui::SameLine(); - ImGui::Button("Fancy"); - if (ImGui::IsItemHovered()) - { - ImGui::BeginTooltip(); - ImGui::Text("I am a fancy tooltip"); - static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; - ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); - ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime())); - ImGui::EndTooltip(); - } - - ImGui::SameLine(); - ImGui::Button("Delayed"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal)) // Delay best used on items that highlight on hover, so this not a great example! - ImGui::SetTooltip("I am a tooltip with a delay."); - - ImGui::SameLine(); - HelpMarker( - "Tooltip are created by using the IsItemHovered() function over any kind of item."); - - } - ImGui::TreePop(); } @@ -1026,16 +1045,17 @@ static void ShowDemoWindowWidgets() float my_tex_w = (float)io.Fonts->TexWidth; float my_tex_h = (float)io.Fonts->TexHeight; { + static bool use_text_color_for_tint = false; + ImGui::Checkbox("Use Text Color for Tint", &use_text_color_for_tint); ImGui::Text("%.0fx%.0f", my_tex_w, my_tex_h); ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right - ImVec4 tint_col = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint - ImVec4 border_col = ImVec4(1.0f, 1.0f, 1.0f, 0.5f); // 50% opaque white + ImVec4 tint_col = use_text_color_for_tint ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint + ImVec4 border_col = ImGui::GetStyleColorVec4(ImGuiCol_Border); ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, tint_col, border_col); - if (ImGui::IsItemHovered()) + if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) { - ImGui::BeginTooltip(); float region_sz = 32.0f; float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; float region_y = io.MousePos.y - pos.y - region_sz * 0.5f; @@ -1689,7 +1709,7 @@ static void ShowDemoWindowWidgets() } // Use functions to generate output - // FIXME: This is rather awkward because current plot API only pass in indices. + // FIXME: This is actually VERY awkward because current plot API only pass in indices. // We probably want an API passing floats and user provide sample rate/count. struct Funcs { @@ -1697,7 +1717,7 @@ static void ShowDemoWindowWidgets() static float Saw(void*, int i) { return (i & 1) ? 1.0f : -1.0f; } }; static int func_type = 0, display_count = 70; - ImGui::Separator(); + ImGui::SeparatorText("Functions"); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::Combo("func", &func_type, "Sin\0Saw\0"); ImGui::SameLine(); @@ -1740,6 +1760,7 @@ static void ShowDemoWindowWidgets() static bool drag_and_drop = true; static bool options_menu = true; static bool hdr = false; + ImGui::SeparatorText("Options"); ImGui::Checkbox("With Alpha Preview", &alpha_preview); ImGui::Checkbox("With Half Alpha Preview", &alpha_half_preview); ImGui::Checkbox("With Drag and Drop", &drag_and_drop); @@ -1748,6 +1769,7 @@ static void ShowDemoWindowWidgets() ImGuiColorEditFlags misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions); IMGUI_DEMO_MARKER("Widgets/Color/ColorEdit"); + ImGui::SeparatorText("Inline color editor"); ImGui::Text("Color widget:"); ImGui::SameLine(); HelpMarker( "Click on the color square to open a color picker.\n" @@ -1845,7 +1867,7 @@ static void ShowDemoWindowWidgets() ImGui::ColorButton("MyColor##3c", *(ImVec4*)&color, misc_flags | (no_border ? ImGuiColorEditFlags_NoBorder : 0), ImVec2(80, 80)); IMGUI_DEMO_MARKER("Widgets/Color/ColorPicker"); - ImGui::Text("Color picker:"); + ImGui::SeparatorText("Color picker"); static bool alpha = true; static bool alpha_bar = true; static bool side_preview = true; @@ -2016,7 +2038,7 @@ static void ShowDemoWindowWidgets() const float drag_speed = 0.2f; static bool drag_clamp = false; IMGUI_DEMO_MARKER("Widgets/Data Types/Drags"); - ImGui::Text("Drags:"); + ImGui::SeparatorText("Drags"); ImGui::Checkbox("Clamp integers to 0..50", &drag_clamp); ImGui::SameLine(); HelpMarker( "As with every widget in dear imgui, we never modify values unless there is a user interaction.\n" @@ -2036,7 +2058,7 @@ static void ShowDemoWindowWidgets() ImGui::DragScalar("drag double log",ImGuiDataType_Double, &f64_v, 0.0005f, &f64_zero, &f64_one, "0 < %.10f < 1", ImGuiSliderFlags_Logarithmic); IMGUI_DEMO_MARKER("Widgets/Data Types/Sliders"); - ImGui::Text("Sliders"); + ImGui::SeparatorText("Sliders"); ImGui::SliderScalar("slider s8 full", ImGuiDataType_S8, &s8_v, &s8_min, &s8_max, "%d"); ImGui::SliderScalar("slider u8 full", ImGuiDataType_U8, &u8_v, &u8_min, &u8_max, "%u"); ImGui::SliderScalar("slider s16 full", ImGuiDataType_S16, &s16_v, &s16_min, &s16_max, "%d"); @@ -2061,7 +2083,7 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider double low log",ImGuiDataType_Double, &f64_v, &f64_zero, &f64_one, "%.10f", ImGuiSliderFlags_Logarithmic); ImGui::SliderScalar("slider double high", ImGuiDataType_Double, &f64_v, &f64_lo_a, &f64_hi_a, "%e grams"); - ImGui::Text("Sliders (reverse)"); + ImGui::SeparatorText("Sliders (reverse)"); ImGui::SliderScalar("slider s8 reverse", ImGuiDataType_S8, &s8_v, &s8_max, &s8_min, "%d"); ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u"); ImGui::SliderScalar("slider s32 reverse", ImGuiDataType_S32, &s32_v, &s32_fifty, &s32_zero, "%d"); @@ -2071,7 +2093,7 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Data Types/Inputs"); static bool inputs_step = true; - ImGui::Text("Inputs"); + ImGui::SeparatorText("Inputs"); ImGui::Checkbox("Show step buttons", &inputs_step); ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d"); ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u"); @@ -2095,22 +2117,23 @@ static void ShowDemoWindowWidgets() static float vec4f[4] = { 0.10f, 0.20f, 0.30f, 0.44f }; static int vec4i[4] = { 1, 5, 100, 255 }; + ImGui::SeparatorText("2-wide"); ImGui::InputFloat2("input float2", vec4f); ImGui::DragFloat2("drag float2", vec4f, 0.01f, 0.0f, 1.0f); ImGui::SliderFloat2("slider float2", vec4f, 0.0f, 1.0f); ImGui::InputInt2("input int2", vec4i); ImGui::DragInt2("drag int2", vec4i, 1, 0, 255); ImGui::SliderInt2("slider int2", vec4i, 0, 255); - ImGui::Spacing(); + ImGui::SeparatorText("3-wide"); ImGui::InputFloat3("input float3", vec4f); ImGui::DragFloat3("drag float3", vec4f, 0.01f, 0.0f, 1.0f); ImGui::SliderFloat3("slider float3", vec4f, 0.0f, 1.0f); ImGui::InputInt3("input int3", vec4i); ImGui::DragInt3("drag int3", vec4i, 1, 0, 255); ImGui::SliderInt3("slider int3", vec4i, 0, 255); - ImGui::Spacing(); + ImGui::SeparatorText("4-wide"); ImGui::InputFloat4("input float4", vec4f); ImGui::DragFloat4("drag float4", vec4f, 0.01f, 0.0f, 1.0f); ImGui::SliderFloat4("slider float4", vec4f, 0.0f, 1.0f); @@ -2527,6 +2550,8 @@ static void ShowDemoWindowLayout() IMGUI_DEMO_MARKER("Layout/Child windows"); if (ImGui::TreeNode("Child windows")) { + ImGui::SeparatorText("Child windows"); + HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window."); static bool disable_mouse_wheel = false; static bool disable_menu = false; @@ -2579,7 +2604,7 @@ static void ShowDemoWindowLayout() ImGui::PopStyleVar(); } - ImGui::Separator(); + ImGui::SeparatorText("Misc/Advanced"); // Demonstrate a few extra things // - Changing ImGuiCol_ChildBg (which is transparent black in default styles) @@ -3343,8 +3368,7 @@ static void ShowDemoWindowPopups() ImGui::TextUnformatted(selected_fish == -1 ? "" : names[selected_fish]); if (ImGui::BeginPopup("my_select_popup")) { - ImGui::Text("Aquarium"); - ImGui::Separator(); + ImGui::SeparatorText("Aquarium"); for (int i = 0; i < IM_ARRAYSIZE(names); i++) if (ImGui::Selectable(names[i])) selected_fish = i; @@ -3521,7 +3545,7 @@ static void ShowDemoWindowPopups() if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!\n\n"); + ImGui::Text("All those beautiful files will be deleted.\nThis operation cannot be undone!"); ImGui::Separator(); //static int unused_i = 0; @@ -3704,9 +3728,8 @@ static void EditTableSizingFlags(ImGuiTableFlags* p_flags) } ImGui::SameLine(); ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered()) + if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) { - ImGui::BeginTooltip(); ImGui::PushTextWrapPos(ImGui::GetFontSize() * 50.0f); for (int m = 0; m < IM_ARRAYSIZE(policies); m++) { @@ -5718,13 +5741,15 @@ static void ShowDemoWindowInputs() ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); // We iterate both legacy native range and named ImGuiKey ranges, which is a little odd but this allows displaying the data for old/new backends. - // User code should never have to go through such hoops: old code may use native keycodes, new code may use ImGuiKey codes. + // User code should never have to go through such hoops! You can generally iterate between ImGuiKey_NamedKey_BEGIN and ImGuiKey_NamedKey_END. #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; + ImGuiKey start_key = ImGuiKey_NamedKey_BEGIN; #else struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array + ImGuiKey start_key = (ImGuiKey)0; #endif - ImGui::Text("Keys down:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); ImGui::SameLine(); ImGui::Text("(%.02f)", ImGui::GetKeyData(key)->DownDuration); } + ImGui::Text("Keys down:"); for (ImGuiKey key = start_key; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); ImGui::SameLine(); ImGui::Text("(%.02f)", ImGui::GetKeyData(key)->DownDuration); } ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. @@ -5812,10 +5837,10 @@ static void ShowDemoWindowInputs() ImGui::InputText("1", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); - ImGui::PushAllowKeyboardFocus(false); + ImGui::PushTabStop(false); ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf)); ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); - ImGui::PopAllowKeyboardFocus(); + ImGui::PopTabStop(); ImGui::InputText("5", buf, IM_ARRAYSIZE(buf)); ImGui::TreePop(); } @@ -5837,12 +5862,12 @@ static void ShowDemoWindowInputs() ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); if (ImGui::IsItemActive()) has_focus = 2; - ImGui::PushAllowKeyboardFocus(false); + ImGui::PushTabStop(false); if (focus_3) ImGui::SetKeyboardFocusHere(); ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf)); if (ImGui::IsItemActive()) has_focus = 3; ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); - ImGui::PopAllowKeyboardFocus(); + ImGui::PopTabStop(); if (has_focus) ImGui::Text("Item with focus: %d", has_focus); @@ -5993,6 +6018,9 @@ void ImGui::ShowAboutWindow(bool* p_open) #endif #ifdef __clang_version__ ImGui::Text("define: __clang_version__=%s", __clang_version__); +#endif +#ifdef __EMSCRIPTEN__ + ImGui::Text("define: __EMSCRIPTEN__"); #endif ImGui::Separator(); ImGui::Text("io.BackendPlatformName: %s", io.BackendPlatformName ? io.BackendPlatformName : "NULL"); @@ -6142,7 +6170,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) { if (ImGui::BeginTabItem("Sizes")) { - ImGui::Text("Main"); + ImGui::SeparatorText("Main"); ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f"); @@ -6152,22 +6180,24 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SliderFloat("IndentSpacing", &style.IndentSpacing, 0.0f, 30.0f, "%.0f"); ImGui::SliderFloat("ScrollbarSize", &style.ScrollbarSize, 1.0f, 20.0f, "%.0f"); ImGui::SliderFloat("GrabMinSize", &style.GrabMinSize, 1.0f, 20.0f, "%.0f"); - ImGui::Text("Borders"); + + ImGui::SeparatorText("Borders"); ImGui::SliderFloat("WindowBorderSize", &style.WindowBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("ChildBorderSize", &style.ChildBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f"); - ImGui::Text("Rounding"); + + ImGui::SeparatorText("Rounding"); ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("ChildRounding", &style.ChildRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("FrameRounding", &style.FrameRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("PopupRounding", &style.PopupRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("ScrollbarRounding", &style.ScrollbarRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f"); - ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f"); - ImGui::Text("Alignment"); + + ImGui::SeparatorText("Widgets"); ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); int window_menu_button_position = style.WindowMenuButtonPosition + 1; if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0")) @@ -6177,9 +6207,13 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content."); ImGui::SliderFloat2("SelectableTextAlign", (float*)&style.SelectableTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content."); - ImGui::Text("Safe Area Padding"); - ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); - ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); + ImGui::SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f"); + ImGui::SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f"); + ImGui::SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%0.f"); + ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f"); + + ImGui::SeparatorText("Misc"); + ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); ImGui::EndTabItem(); } @@ -6289,10 +6323,11 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) // When editing the "Circle Segment Max Error" value, draw a preview of its effect on auto-tessellated circles. ImGui::DragFloat("Circle Tessellation Max Error", &style.CircleTessellationMaxError , 0.005f, 0.10f, 5.0f, "%.2f", ImGuiSliderFlags_AlwaysClamp); - if (ImGui::IsItemActive()) - { + const bool show_samples = ImGui::IsItemActive(); + if (show_samples) ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos()); - ImGui::BeginTooltip(); + if (show_samples && ImGui::BeginTooltip()) + { ImGui::TextUnformatted("(R = radius, N = number of segments)"); ImGui::Spacing(); ImDrawList* draw_list = ImGui::GetWindowDrawList(); @@ -6489,6 +6524,7 @@ static void ShowExampleMenuFile() IM_ASSERT(0); } if (ImGui::MenuItem("Checked", NULL, true)) {} + ImGui::Separator(); if (ImGui::MenuItem("Quit", "Alt+F4")) {} } diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 2d423cd8..031c3e7d 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (drawing and font code) /* @@ -26,13 +26,12 @@ Index of this file: #define _CRT_SECURE_NO_WARNINGS #endif -#include "imgui.h" -#ifndef IMGUI_DISABLE - #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif +#include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_internal.h" #ifdef IMGUI_ENABLE_FREETYPE #include "misc/freetype/imgui_freetype.h" @@ -389,6 +388,8 @@ void ImDrawList::_ResetForNewFrame() IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); + if (_Splitter._Count > 1) + _Splitter.Merge(this); CmdBuffer.resize(0); IdxBuffer.resize(0); @@ -706,7 +707,7 @@ void ImDrawList::PrimQuadUV(const ImVec2& a, const ImVec2& b, const ImVec2& c, c // We avoid using the ImVec2 math operators here to reduce cost to a minimum for debug/non-inlined builds. void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 col, ImDrawFlags flags, float thickness) { - if (points_count < 2) + if (points_count < 2 || (col & IM_COL32_A_MASK) == 0) return; const bool closed = (flags & ImDrawFlags_Closed) != 0; @@ -964,7 +965,7 @@ void ImDrawList::AddPolyline(const ImVec2* points, const int points_count, ImU32 // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. void ImDrawList::AddConvexPolyFilled(const ImVec2* points, const int points_count, ImU32 col) { - if (points_count < 3) + if (points_count < 3 || (col & IM_COL32_A_MASK) == 0) return; const ImVec2 uv = _Data->TexUvWhitePixel; @@ -2382,7 +2383,12 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; src_tmp.SrcRanges = cfg.GlyphRanges ? cfg.GlyphRanges : atlas->GetGlyphRangesDefault(); for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) + { + // Check for valid range. This may also help detect *some* dangling pointers, because a common + // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent. + IM_ASSERT(src_range[0] <= src_range[1]); src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]); + } dst_tmp.SrcCount++; dst_tmp.GlyphsHighest = ImMax(dst_tmp.GlyphsHighest, src_tmp.GlyphsHighest); } @@ -2937,19 +2943,19 @@ const ImWchar* ImFontAtlas::GetGlyphRangesJapanese() // 2999 ideograms code points for Japanese // - 2136 Joyo (meaning "for regular use" or "for common use") Kanji code points // - 863 Jinmeiyo (meaning "for personal name") Kanji code points - // - Sourced from the character information database of the Information-technology Promotion Agency, Japan - // - https://mojikiban.ipa.go.jp/mji/ - // - Available under the terms of the Creative Commons Attribution-ShareAlike 2.1 Japan (CC BY-SA 2.1 JP). - // - https://creativecommons.org/licenses/by-sa/2.1/jp/deed.en - // - https://creativecommons.org/licenses/by-sa/2.1/jp/legalcode - // - You can generate this code by the script at: - // - https://github.com/vaiorabbit/everyday_use_kanji + // - Sourced from official information provided by the government agencies of Japan: + // - List of Joyo Kanji by the Agency for Cultural Affairs + // - https://www.bunka.go.jp/kokugo_nihongo/sisaku/joho/joho/kijun/naikaku/kanji/ + // - List of Jinmeiyo Kanji by the Ministry of Justice + // - http://www.moj.go.jp/MINJI/minji86.html + // - Available under the terms of the Creative Commons Attribution 4.0 International (CC BY 4.0). + // - https://creativecommons.org/licenses/by/4.0/legalcode + // - You can generate this code by the script at: + // - https://github.com/vaiorabbit/everyday_use_kanji // - References: // - List of Joyo Kanji - // - (Official list by the Agency for Cultural Affairs) https://www.bunka.go.jp/kokugo_nihongo/sisaku/joho/joho/kakuki/14/tosin02/index.html // - (Wikipedia) https://en.wikipedia.org/wiki/List_of_j%C5%8Dy%C5%8D_kanji // - List of Jinmeiyo Kanji - // - (Official list by the Ministry of Justice) http://www.moj.go.jp/MINJI/minji86.html // - (Wikipedia) https://en.wikipedia.org/wiki/Jinmeiy%C5%8D_kanji // - Missing 1 Joyo Kanji: U+20B9F (Kun'yomi: Shikaru, On'yomi: Shitsu,shichi), see https://github.com/ocornut/imgui/pull/3627 for details. // You can use ImFontGlyphRangesBuilder to create your own ranges derived from this, by merging existing ranges or adding new characters. @@ -3112,7 +3118,8 @@ ImFont::ImFont() FallbackAdvanceX = 0.0f; FallbackChar = (ImWchar)-1; EllipsisChar = (ImWchar)-1; - DotChar = (ImWchar)-1; + EllipsisWidth = EllipsisCharStep = 0.0f; + EllipsisCharCount = 0; FallbackGlyph = NULL; ContainerAtlas = NULL; ConfigData = NULL; @@ -3200,8 +3207,20 @@ void ImFont::BuildLookupTable() const ImWchar dots_chars[] = { (ImWchar)'.', (ImWchar)0xFF0E }; if (EllipsisChar == (ImWchar)-1) EllipsisChar = FindFirstExistingGlyph(this, ellipsis_chars, IM_ARRAYSIZE(ellipsis_chars)); - if (DotChar == (ImWchar)-1) - DotChar = FindFirstExistingGlyph(this, dots_chars, IM_ARRAYSIZE(dots_chars)); + const ImWchar dot_char = FindFirstExistingGlyph(this, dots_chars, IM_ARRAYSIZE(dots_chars)); + if (EllipsisChar != (ImWchar)-1) + { + EllipsisCharCount = 1; + EllipsisWidth = EllipsisCharStep = FindGlyph(EllipsisChar)->X1; + } + else if (dot_char != (ImWchar)-1) + { + const ImFontGlyph* glyph = FindGlyph(dot_char); + EllipsisChar = dot_char; + EllipsisCharCount = 3; + EllipsisCharStep = (glyph->X1 - glyph->X0) + 1.0f; + EllipsisWidth = EllipsisCharStep * 3.0f - 1.0f; + } // Setup fallback character const ImWchar fallback_chars[] = { (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' }; @@ -3369,6 +3388,7 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c bool inside_word = true; const char* s = text; + IM_ASSERT(text_end != NULL); while (s < text_end) { unsigned int c = (unsigned int)*s; @@ -3377,8 +3397,6 @@ const char* ImFont::CalcWordWrapPositionA(float scale, const char* text, const c next_s = s + 1; else next_s = s + ImTextCharFromUtf8(&c, s, text_end); - if (c == 0) - break; if (c < 32) { @@ -3484,15 +3502,9 @@ ImVec2 ImFont::CalcTextSizeA(float size, float max_width, float wrap_width, cons const char* prev_s = s; unsigned int c = (unsigned int)*s; if (c < 0x80) - { s += 1; - } else - { s += ImTextCharFromUtf8(&c, s, text_end); - if (c == 0) // Malformed UTF-8? - break; - } if (c < 32) { @@ -3567,18 +3579,17 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im while (y + line_height < clip_rect.y && s < text_end) { const char* line_end = (const char*)memchr(s, '\n', text_end - s); - const char* line_next = line_end ? line_end + 1 : text_end; if (word_wrap_enabled) { // FIXME-OPT: This is not optimal as do first do a search for \n before calling CalcWordWrapPositionA(). // If the specs for CalcWordWrapPositionA() were reworked to optionally return on \n we could combine both. // However it is still better than nothing performing the fast-forward! - s = CalcWordWrapPositionA(scale, s, line_next, wrap_width); + s = CalcWordWrapPositionA(scale, s, line_end ? line_end : text_end, wrap_width); s = CalcWordWrapNextLineStartA(s, text_end); } else { - s = line_next; + s = line_end ? line_end + 1 : text_end; } y += line_height; } @@ -3605,10 +3616,9 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im const int idx_count_max = (int)(text_end - s) * 6; const int idx_expected_size = draw_list->IdxBuffer.Size + idx_count_max; draw_list->PrimReserve(idx_count_max, vtx_count_max); - - ImDrawVert* vtx_write = draw_list->_VtxWritePtr; - ImDrawIdx* idx_write = draw_list->_IdxWritePtr; - unsigned int vtx_current_idx = draw_list->_VtxCurrentIdx; + ImDrawVert* vtx_write = draw_list->_VtxWritePtr; + ImDrawIdx* idx_write = draw_list->_IdxWritePtr; + unsigned int vtx_index = draw_list->_VtxCurrentIdx; const ImU32 col_untinted = col | ~IM_COL32_A_MASK; const char* word_wrap_eol = NULL; @@ -3634,15 +3644,9 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im // Decode and advance source unsigned int c = (unsigned int)*s; if (c < 0x80) - { s += 1; - } else - { s += ImTextCharFromUtf8(&c, s, text_end); - if (c == 0) // Malformed UTF-8? - break; - } if (c < 32) { @@ -3713,14 +3717,14 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im // We are NOT calling PrimRectUV() here because non-inlined causes too much overhead in a debug builds. Inlined here: { - idx_write[0] = (ImDrawIdx)(vtx_current_idx); idx_write[1] = (ImDrawIdx)(vtx_current_idx+1); idx_write[2] = (ImDrawIdx)(vtx_current_idx+2); - idx_write[3] = (ImDrawIdx)(vtx_current_idx); idx_write[4] = (ImDrawIdx)(vtx_current_idx+2); idx_write[5] = (ImDrawIdx)(vtx_current_idx+3); vtx_write[0].pos.x = x1; vtx_write[0].pos.y = y1; vtx_write[0].col = glyph_col; vtx_write[0].uv.x = u1; vtx_write[0].uv.y = v1; vtx_write[1].pos.x = x2; vtx_write[1].pos.y = y1; vtx_write[1].col = glyph_col; vtx_write[1].uv.x = u2; vtx_write[1].uv.y = v1; vtx_write[2].pos.x = x2; vtx_write[2].pos.y = y2; vtx_write[2].col = glyph_col; vtx_write[2].uv.x = u2; vtx_write[2].uv.y = v2; vtx_write[3].pos.x = x1; vtx_write[3].pos.y = y2; vtx_write[3].col = glyph_col; vtx_write[3].uv.x = u1; vtx_write[3].uv.y = v2; + idx_write[0] = (ImDrawIdx)(vtx_index); idx_write[1] = (ImDrawIdx)(vtx_index + 1); idx_write[2] = (ImDrawIdx)(vtx_index + 2); + idx_write[3] = (ImDrawIdx)(vtx_index); idx_write[4] = (ImDrawIdx)(vtx_index + 2); idx_write[5] = (ImDrawIdx)(vtx_index + 3); vtx_write += 4; - vtx_current_idx += 4; + vtx_index += 4; idx_write += 6; } } @@ -3734,7 +3738,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im draw_list->CmdBuffer[draw_list->CmdBuffer.Size - 1].ElemCount -= (idx_expected_size - draw_list->IdxBuffer.Size); draw_list->_VtxWritePtr = vtx_write; draw_list->_IdxWritePtr = idx_write; - draw_list->_VtxCurrentIdx = vtx_current_idx; + draw_list->_VtxCurrentIdx = vtx_index; } //----------------------------------------------------------------------------- diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index d0dedf64..29d502b0 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -1,7 +1,7 @@ // dear imgui: Platform Backend for GLFW // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) -// (Requires: GLFW 3.1+) +// (Requires: GLFW 3.1+. Prefer GLFW 3.3+ or GLFW 3.4+ for full feature support.) // Implemented features: // [X] Platform: Clipboard support. @@ -16,6 +16,8 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-03-14: Emscripten: Avoid using glfwGetError() and glfwGetGamepadState() which are not correctly implemented in Emscripten emulation. (#6240) +// 2023-02-03: Emscripten: Registering custom low-level mouse wheel handler to get more accurate scrolling impulses on Emscripten. (#4019, #6096) // 2023-01-04: Inputs: Fixed mods state on Linux when using Alt-GR text input (e.g. German keyboard layout), could lead to broken text input. Revert a 2022/01/17 change were we resumed using mods provided by GLFW, turns out they were faulty. // 2022-11-22: Perform a dummy glfwGetError() read to cancel missing names with glfwGetKeyName(). (#5908) // 2022-10-18: Perform a dummy glfwGetError() read to cancel missing mouse cursors errors. Using GLFW_VERSION_COMBINED directly. (#5785) @@ -69,10 +71,20 @@ // GLFW #include + #ifdef _WIN32 #undef APIENTRY #define GLFW_EXPOSE_NATIVE_WIN32 -#include // for glfwGetWin32Window +#include // for glfwGetWin32Window() +#endif +#ifdef __APPLE__ +#define GLFW_EXPOSE_NATIVE_COCOA +#include // for glfwGetCocoaWindow() +#endif + +#ifdef __EMSCRIPTEN__ +#include +#include #endif // We gather version tests as define in order to easily see which features are version-dependent. @@ -84,6 +96,7 @@ #endif #define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api #define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName() +#define GLFW_HAS_GETERROR (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetError() // GLFW data enum GlfwClientApi @@ -102,6 +115,7 @@ struct ImGui_ImplGlfw_Data GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; ImVec2 LastValidMousePos; bool InstalledCallbacks; + bool CallbacksChainForAllWindows; // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. GLFWwindowfocusfun PrevUserCallbackWindowFocus; @@ -264,10 +278,16 @@ static void ImGui_ImplGlfw_UpdateKeyModifiers() io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); } +static bool ImGui_ImplGlfw_ShouldChainCallback(GLFWwindow* window) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + return bd->CallbacksChainForAllWindows ? true : (window == bd->Window); +} + void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackMousebutton != nullptr && window == bd->Window) + if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackMousebutton(window, button, action, mods); ImGui_ImplGlfw_UpdateKeyModifiers(); @@ -280,9 +300,14 @@ void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int acti void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackScroll != nullptr && window == bd->Window) + if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackScroll(window, xoffset, yoffset); +#ifdef __EMSCRIPTEN__ + // Ignore GLFW events: will be processed in ImGui_ImplEmscripten_WheelCallback(). + return; +#endif + ImGuiIO& io = ImGui::GetIO(); io.AddMouseWheelEvent((float)xoffset, (float)yoffset); } @@ -300,7 +325,7 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); const char* key_name = glfwGetKeyName(key, scancode); glfwSetErrorCallback(prev_error_callback); -#if (GLFW_VERSION_COMBINED >= 3300) // Eat errors (see #5908) +#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) (void)glfwGetError(NULL); #endif if (key_name && key_name[0] != 0 && key_name[1] == 0) @@ -323,7 +348,7 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, int action, int mods) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackKey != nullptr && window == bd->Window) + if (bd->PrevUserCallbackKey != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackKey(window, keycode, scancode, action, mods); if (action != GLFW_PRESS && action != GLFW_RELEASE) @@ -342,7 +367,7 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackWindowFocus != nullptr && window == bd->Window) + if (bd->PrevUserCallbackWindowFocus != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackWindowFocus(window, focused); ImGuiIO& io = ImGui::GetIO(); @@ -352,7 +377,7 @@ void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackCursorPos != nullptr && window == bd->Window) + if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackCursorPos(window, x, y); if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) return; @@ -367,7 +392,7 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackCursorEnter != nullptr && window == bd->Window) + if (bd->PrevUserCallbackCursorEnter != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackCursorEnter(window, entered); if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) return; @@ -389,7 +414,7 @@ void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackChar != nullptr && window == bd->Window) + if (bd->PrevUserCallbackChar != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackChar(window, c); ImGuiIO& io = ImGui::GetIO(); @@ -401,6 +426,24 @@ void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int) // Unused in 'master' branch but 'docking' branch will use this, so we declare it ahead of it so if you have to install callbacks you can install this one too. } +#ifdef __EMSCRIPTEN__ +static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void*) +{ + // Mimic Emscripten_HandleWheel() in SDL. + // Corresponding equivalent in GLFW JS emulation layer has incorrect quantizing preventing small values. See #6096 + float multiplier = 0.0f; + if (ev->deltaMode == DOM_DELTA_PIXEL) { multiplier = 1.0f / 100.0f; } // 100 pixels make up a step. + else if (ev->deltaMode == DOM_DELTA_LINE) { multiplier = 1.0f / 3.0f; } // 3 lines make up a step. + else if (ev->deltaMode == DOM_DELTA_PAGE) { multiplier = 80.0f; } // A page makes up 80 steps. + float wheel_x = ev->deltaX * -multiplier; + float wheel_y = ev->deltaY * -multiplier; + ImGuiIO& io = ImGui::GetIO(); + io.AddMouseWheelEvent(wheel_x, wheel_y); + //IMGUI_DEBUG_LOG("[Emsc] mode %d dx: %.2f, dy: %.2f, dz: %.2f --> feed %.2f %.2f\n", (int)ev->deltaMode, ev->deltaX, ev->deltaY, ev->deltaZ, wheel_x, wheel_y); + return EM_TRUE; +} +#endif + void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); @@ -443,6 +486,16 @@ void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window) bd->PrevUserCallbackMonitor = nullptr; } +// Set to 'true' to enable chaining installed callbacks for all windows (including secondary viewports created by backends or by user. +// This is 'false' by default meaning we only chain callbacks for the main viewport. +// We cannot set this to 'true' by default because user callbacks code may be not testing the 'window' parameter of their callback. +// If you set this to 'true' your user callback code will need to make sure you are testing the 'window' parameter. +void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + bd->CallbacksChainForAllWindows = chain_for_all_windows; +} + static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) { ImGuiIO& io = ImGui::GetIO(); @@ -463,11 +516,6 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; io.ClipboardUserData = bd->Window; - // Set platform dependent data in viewport -#if defined(_WIN32) - ImGui::GetMainViewport()->PlatformHandleRaw = (void*)glfwGetWin32Window(bd->Window); -#endif - // Create mouse cursors // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist, // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting. @@ -490,13 +538,29 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); #endif glfwSetErrorCallback(prev_error_callback); -#if (GLFW_VERSION_COMBINED >= 3300) // Eat errors (see #5785) +#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) (void)glfwGetError(NULL); #endif // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. if (install_callbacks) ImGui_ImplGlfw_InstallCallbacks(window); + // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096) + // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves. + // FIXME: May break chaining in case user registered their own Emscripten callback? +#ifdef __EMSCRIPTEN__ + emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, false, ImGui_ImplEmscripten_WheelCallback); +#endif + + // Set platform dependent data in viewport + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); +#ifdef _WIN32 + main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window); +#elif defined(__APPLE__) + main_viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(bd->Window); +#else + IM_UNUSED(main_viewport); +#endif bd->ClientApi = client_api; return true; @@ -545,24 +609,29 @@ static void ImGui_ImplGlfw_UpdateMouseData() return; } + // (those braces are here to reduce diff with multi-viewports support in 'docking' branch) + { + GLFWwindow* window = bd->Window; + #ifdef __EMSCRIPTEN__ - const bool is_app_focused = true; + const bool is_window_focused = true; #else - const bool is_app_focused = glfwGetWindowAttrib(bd->Window, GLFW_FOCUSED) != 0; + const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0; #endif - if (is_app_focused) - { - // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) - if (io.WantSetMousePos) - glfwSetCursorPos(bd->Window, (double)io.MousePos.x, (double)io.MousePos.y); - - // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured) - if (is_app_focused && bd->MouseWindow == nullptr) + if (is_window_focused) { - double mouse_x, mouse_y; - glfwGetCursorPos(bd->Window, &mouse_x, &mouse_y); - io.AddMousePosEvent((float)mouse_x, (float)mouse_y); - bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y); + // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) + if (io.WantSetMousePos) + glfwSetCursorPos(window, (double)io.MousePos.x, (double)io.MousePos.y); + + // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured) + if (bd->MouseWindow == nullptr) + { + double mouse_x, mouse_y; + glfwGetCursorPos(window, &mouse_x, &mouse_y); + bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y); + io.AddMousePosEvent((float)mouse_x, (float)mouse_y); + } } } } @@ -575,17 +644,21 @@ static void ImGui_ImplGlfw_UpdateMouseCursor() return; ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); - if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) - { - // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor - glfwSetInputMode(bd->Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - } - else + // (those braces are here to reduce diff with multi-viewports support in 'docking' branch) { - // Show OS mouse cursor - // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. - glfwSetCursor(bd->Window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); - glfwSetInputMode(bd->Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + GLFWwindow* window = bd->Window; + if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) + { + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + } + else + { + // Show OS mouse cursor + // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. + glfwSetCursor(window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } } } @@ -598,7 +671,7 @@ static void ImGui_ImplGlfw_UpdateGamepads() return; io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; -#if GLFW_HAS_GAMEPAD_API +#if GLFW_HAS_GAMEPAD_API && !defined(__EMSCRIPTEN__) GLFWgamepadstate gamepad; if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad)) return; diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index c98110d8..134479fd 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -5,7 +5,7 @@ // Implemented features: // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! -// [x] Renderer: Desktop GL only: Support for large meshes (64k+ vertices) with 16-bit indices. +// [x] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. @@ -14,11 +14,12 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-03-06: OpenGL: Fixed restoration of a potentially deleted OpenGL program, by calling glIsProgram(). (#6220, #6224) // 2022-11-09: OpenGL: Reverted use of glBufferSubData(), too many corruptions issues + old issues seemingly can't be reproed with Intel drivers nowadays (revert 2021-12-15 and 2022-05-23 changes). // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. // 2022-09-27: OpenGL: Added ability to '#define IMGUI_IMPL_OPENGL_DEBUG'. // 2022-05-23: OpenGL: Reworking 2021-12-15 "Using buffer orphaning" so it only happens on Intel GPU, seems to cause problems otherwise. (#4468, #4825, #4832, #5127). -// 2022-05-13: OpenGL: Fix state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states. +// 2022-05-13: OpenGL: Fixed state corruption on OpenGL ES 2.0 due to not preserving GL_ELEMENT_ARRAY_BUFFER_BINDING and vertex attribute states. // 2021-12-15: OpenGL: Using buffer orphaning + glBufferSubData(), seems to fix leaks with multi-viewports with some Intel HD drivers. // 2021-08-23: OpenGL: Fixed ES 3.0 shader ("#version 300 es") use normal precision floats to avoid wobbly rendering at HD resolutions. // 2021-08-19: OpenGL: Embed and use our own minimal GL loader (imgui_impl_opengl3_loader.h), removing requirement and support for third-party loader. @@ -107,12 +108,14 @@ // Clang warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast -#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast +#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness #endif #if defined(__GNUC__) #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types +#pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind +#pragma GCC diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' +#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types #endif // GL includes @@ -574,7 +577,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) #endif // Restore modified GL state - glUseProgram(last_program); + // This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220. + if (glIsProgram(last_program)) glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER if (bd->GlVersion >= 330) diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 47fc8a28..3bacc34a 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (tables and columns code) /* @@ -188,12 +188,12 @@ Index of this file: #define _CRT_SECURE_NO_WARNINGS #endif -#include "imgui.h" -#ifndef IMGUI_DISABLE - #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif + +#include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_internal.h" // System includes @@ -315,7 +315,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG return false; // Sanity checks - IM_ASSERT(columns_count > 0 && columns_count <= IMGUI_TABLE_MAX_COLUMNS && "Only 1..64 columns allowed!"); + IM_ASSERT(columns_count > 0 && columns_count < IMGUI_TABLE_MAX_COLUMNS); if (flags & ImGuiTableFlags_ScrollX) IM_ASSERT(inner_width >= 0.0f); @@ -332,11 +332,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // Acquire storage for the table ImGuiTable* table = g.Tables.GetOrAddByKey(id); - const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceCurrent + 1; - const ImGuiID instance_id = id + instance_no; const ImGuiTableFlags table_last_flags = table->Flags; - if (instance_no > 0) - IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID"); // Acquire temporary buffers const int table_idx = g.Tables.GetIndex(table); @@ -352,17 +348,32 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG flags = TableFixFlags(flags, outer_window); // Initialize + const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceCurrent + 1; table->ID = id; table->Flags = flags; - table->InstanceCurrent = (ImS16)instance_no; table->LastFrameActive = g.FrameCount; table->OuterWindow = table->InnerWindow = outer_window; table->ColumnsCount = columns_count; table->IsLayoutLocked = false; table->InnerWidth = inner_width; temp_data->UserOuterSize = outer_size; - if (instance_no > 0 && table->InstanceDataExtra.Size < instance_no) - table->InstanceDataExtra.push_back(ImGuiTableInstanceData()); + + // Instance data (for instance 0, TableID == TableInstanceID) + ImGuiID instance_id; + table->InstanceCurrent = (ImS16)instance_no; + if (instance_no > 0) + { + IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID"); + if (table->InstanceDataExtra.Size < instance_no) + table->InstanceDataExtra.push_back(ImGuiTableInstanceData()); + instance_id = GetIDWithSeed(instance_no, GetIDWithSeed("##Instances", NULL, id)); // Push "##Instance" followed by (int)instance_no in ID stack. + } + else + { + instance_id = id; + } + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); + table_instance->TableInstanceID = instance_id; // When not using a child window, WorkRect.Max will grow as we append contents. if (use_child_window) @@ -412,7 +423,9 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG } // Push a standardized ID for both child-using and not-child-using tables - PushOverrideID(instance_id); + PushOverrideID(id); + if (instance_no > 0) + PushOverrideID(instance_id); // FIXME: Somehow this is not resolved by stack-tool, even tho GetIDWithSeed() submitted the symbol. // Backup a copy of host window members we will modify ImGuiWindow* inner_window = table->InnerWindow; @@ -581,16 +594,22 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG void ImGui::TableBeginInitMemory(ImGuiTable* table, int columns_count) { // Allocate single buffer for our arrays - ImSpanAllocator<3> span_allocator; + const int columns_bit_array_size = (int)ImBitArrayGetStorageSizeInBytes(columns_count); + ImSpanAllocator<6> span_allocator; span_allocator.Reserve(0, columns_count * sizeof(ImGuiTableColumn)); span_allocator.Reserve(1, columns_count * sizeof(ImGuiTableColumnIdx)); span_allocator.Reserve(2, columns_count * sizeof(ImGuiTableCellData), 4); + for (int n = 3; n < 6; n++) + span_allocator.Reserve(n, columns_bit_array_size); table->RawData = IM_ALLOC(span_allocator.GetArenaSizeInBytes()); memset(table->RawData, 0, span_allocator.GetArenaSizeInBytes()); span_allocator.SetArenaBasePtr(table->RawData); span_allocator.GetSpan(0, &table->Columns); span_allocator.GetSpan(1, &table->DisplayOrderToIndex); span_allocator.GetSpan(2, &table->RowCellData); + table->EnabledMaskByDisplayOrder = (ImU32*)span_allocator.GetSpanPtrBegin(3); + table->EnabledMaskByIndex = (ImU32*)span_allocator.GetSpanPtrBegin(4); + table->VisibleMaskByIndex = (ImU32*)span_allocator.GetSpanPtrBegin(5); } // Apply queued resizing/reordering/hiding requests @@ -729,8 +748,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) const ImGuiTableFlags table_sizing_policy = (table->Flags & ImGuiTableFlags_SizingMask_); table->IsDefaultDisplayOrder = true; table->ColumnsEnabledCount = 0; - table->EnabledMaskByIndex = 0x00; - table->EnabledMaskByDisplayOrder = 0x00; + ImBitArrayClearAllBits(table->EnabledMaskByIndex, table->ColumnsCount); + ImBitArrayClearAllBits(table->EnabledMaskByDisplayOrder, table->ColumnsCount); table->LeftMostEnabledColumn = -1; table->MinColumnWidth = ImMax(1.0f, g.Style.FramePadding.x * 1.0f); // g.Style.ColumnsMinSpacing; // FIXME-TABLE @@ -795,8 +814,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) else table->LeftMostEnabledColumn = (ImGuiTableColumnIdx)column_n; column->IndexWithinEnabledSet = table->ColumnsEnabledCount++; - table->EnabledMaskByIndex |= (ImU64)1 << column_n; - table->EnabledMaskByDisplayOrder |= (ImU64)1 << column->DisplayOrder; + ImBitArraySetBit(table->EnabledMaskByIndex, column_n); + ImBitArraySetBit(table->EnabledMaskByDisplayOrder, column->DisplayOrder); prev_visible_column_idx = column_n; IM_ASSERT(column->IndexWithinEnabledSet <= column->DisplayOrder); @@ -844,7 +863,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->LeftMostStretchedColumn = table->RightMostStretchedColumn = -1; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) { - if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n))) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) continue; ImGuiTableColumn* column = &table->Columns[column_n]; @@ -860,7 +879,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!) if (column->AutoFitQueue != 0x00) column->WidthRequest = width_auto; - else if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !column_is_resizable && (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n))) + else if ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !column_is_resizable && column->IsRequestOutput) column->WidthRequest = width_auto; // FIXME-TABLE: Increase minimum size during init frame to avoid biasing auto-fitting widgets @@ -908,7 +927,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->ColumnsGivenWidth = width_spacings + (table->CellPaddingX * 2.0f) * table->ColumnsEnabledCount; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) { - if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n))) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) continue; ImGuiTableColumn* column = &table->Columns[column_n]; @@ -935,7 +954,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (width_remaining_for_stretched_columns >= 1.0f && !(table->Flags & ImGuiTableFlags_PreciseWidths)) for (int order_n = table->ColumnsCount - 1; stretch_sum_weights > 0.0f && width_remaining_for_stretched_columns >= 1.0f && order_n >= 0; order_n--) { - if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) continue; ImGuiTableColumn* column = &table->Columns[table->DisplayOrderToIndex[order_n]]; if (!(column->Flags & ImGuiTableColumnFlags_WidthStretch)) @@ -966,8 +985,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) float offset_x = ((table->FreezeColumnsCount > 0) ? table->OuterRect.Min.x : work_rect.Min.x) + table->OuterPaddingX - table->CellSpacingX1; ImRect host_clip_rect = table->InnerClipRect; //host_clip_rect.Max.x += table->CellPaddingX + table->CellSpacingX2; - table->VisibleMaskByIndex = 0x00; - table->RequestOutputMaskByIndex = 0x00; + ImBitArrayClearAllBits(table->VisibleMaskByIndex, table->ColumnsCount); for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { const int column_n = table->DisplayOrderToIndex[order_n]; @@ -984,7 +1002,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // Clear status flags column->Flags &= ~ImGuiTableColumnFlags_StatusMask_; - if ((table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n)) == 0) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) { // Hidden column: clear a few fields and we are done with it for the remainder of the function. // We set a zero-width clip rect but set Min.y/Max.y properly to not interfere with the clipper. @@ -1037,12 +1055,10 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) column->IsVisibleY = true; // (column->ClipRect.Max.y > column->ClipRect.Min.y); const bool is_visible = column->IsVisibleX; //&& column->IsVisibleY; if (is_visible) - table->VisibleMaskByIndex |= ((ImU64)1 << column_n); + ImBitArraySetBit(table->VisibleMaskByIndex, column_n); // Mark column as requesting output from user. Note that fixed + non-resizable sets are auto-fitting at all times and therefore always request output. column->IsRequestOutput = is_visible || column->AutoFitQueue != 0 || column->CannotSkipItemsQueue != 0; - if (column->IsRequestOutput) - table->RequestOutputMaskByIndex |= ((ImU64)1 << column_n); // Mark column as SkipItems (ignoring all items/layout) column->IsSkipItems = !column->IsEnabled || table->HostSkipItems; @@ -1128,12 +1144,12 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) EndPopup(); } - // [Part 13] Sanitize and build sort specs before we have a change to use them for display. + // [Part 12] Sanitize and build sort specs before we have a change to use them for display. // This path will only be exercised when sort specs are modified before header rows (e.g. init or visibility change) if (table->IsSortSpecsDirty && (table->Flags & ImGuiTableFlags_Sortable)) TableSortSpecsBuild(table); - // [Part 14] Setup inner window decoration size (for scrolling / nav tracking to properly take account of frozen rows/columns) + // [Part 13] Setup inner window decoration size (for scrolling / nav tracking to properly take account of frozen rows/columns) if (table->FreezeColumnsRequest > 0) table->InnerWindow->DecoInnerSizeX1 = table->Columns[table->DisplayOrderToIndex[table->FreezeColumnsRequest - 1]].MaxX - table->OuterRect.Min.x; if (table->FreezeRowsRequest > 0) @@ -1169,7 +1185,7 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { - if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) continue; const int column_n = table->DisplayOrderToIndex[order_n]; @@ -1305,7 +1321,7 @@ void ImGui::EndTable() float auto_fit_width_for_stretched = 0.0f; float auto_fit_width_for_stretched_min = 0.0f; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) - if (table->EnabledMaskByIndex & ((ImU64)1 << column_n)) + if (IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) { ImGuiTableColumn* column = &table->Columns[column_n]; float column_width_request = ((column->Flags & ImGuiTableColumnFlags_WidthFixed) && !(column->Flags & ImGuiTableColumnFlags_NoResize)) ? column->WidthRequest : TableGetColumnWidthAuto(table, column); @@ -1345,8 +1361,10 @@ void ImGui::EndTable() } // Pop from id stack - IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table->ID + table->InstanceCurrent, "Mismatching PushID/PopID!"); + IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table_instance->TableInstanceID, "Mismatching PushID/PopID!"); IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= temp_data->HostBackupItemWidthStackSize, "Too many PopItemWidth!"); + if (table->InstanceCurrent > 0) + PopID(); PopID(); // Restore window data that we modified @@ -1616,11 +1634,11 @@ ImRect ImGui::TableGetCellBgRect(const ImGuiTable* table, int column_n) } // Return the resizing ID for the right-side of the given column. -ImGuiID ImGui::TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no) +ImGuiID ImGui::TableGetColumnResizeID(ImGuiTable* table, int column_n, int instance_no) { IM_ASSERT(column_n >= 0 && column_n < table->ColumnsCount); - ImGuiID id = table->ID + 1 + (instance_no * table->ColumnsCount) + column_n; - return id; + ImGuiID instance_id = TableGetInstanceID(table, instance_no); + return instance_id + 1 + column_n; // FIXME: #6140: still not ideal } // Return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. @@ -1651,7 +1669,7 @@ void ImGui::TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n return; if (column_n == -1) column_n = table->CurrentColumn; - if ((table->VisibleMaskByIndex & ((ImU64)1 << column_n)) == 0) + if (!IM_BITARRAY_TESTBIT(table->VisibleMaskByIndex, column_n)) return; if (table->RowCellDataCurrent < 0 || table->RowCellData[table->RowCellDataCurrent].Column != column_n) table->RowCellDataCurrent++; @@ -1926,7 +1944,7 @@ bool ImGui::TableSetColumnIndex(int column_n) // Return whether the column is visible. User may choose to skip submitting items based on this return value, // however they shouldn't skip submitting for columns that may have the tallest contribution to row height. - return (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n)) != 0; + return table->Columns[column_n].IsRequestOutput; } // [Public] Append into the next column, wrap and create a new row when already on last column @@ -1951,8 +1969,7 @@ bool ImGui::TableNextColumn() // Return whether the column is visible. User may choose to skip submitting items based on this return value, // however they shouldn't skip submitting for columns that may have the tallest contribution to row height. - int column_n = table->CurrentColumn; - return (table->RequestOutputMaskByIndex & ((ImU64)1 << column_n)) != 0; + return table->Columns[table->CurrentColumn].IsRequestOutput; } @@ -1982,10 +1999,6 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n) window->WorkRect.Max.x = column->WorkMaxX; window->DC.ItemWidth = column->ItemWidth; - // To allow ImGuiListClipper to function we propagate our row height - if (!column->IsEnabled) - window->DC.CursorPos.y = ImMax(window->DC.CursorPos.y, table->RowPosY2); - window->SkipItems = column->IsSkipItems; if (column->IsSkipItems) { @@ -2032,7 +2045,8 @@ void ImGui::TableEndCell(ImGuiTable* table) else p_max_pos_x = table->IsUnfrozenRows ? &column->ContentMaxXUnfrozen : &column->ContentMaxXFrozen; *p_max_pos_x = ImMax(*p_max_pos_x, window->DC.CursorMaxPos.x); - table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY); + if (column->IsEnabled) + table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY); column->ItemWidth = window->DC.ItemWidth; // Propagate text baseline for the entire row @@ -2292,7 +2306,7 @@ void ImGui::TableSetupDrawChannels(ImGuiTable* table) const int freeze_row_multiplier = (table->FreezeRowsCount > 0) ? 2 : 1; const int channels_for_row = (table->Flags & ImGuiTableFlags_NoClip) ? 1 : table->ColumnsEnabledCount; const int channels_for_bg = 1 + 1 * freeze_row_multiplier; - const int channels_for_dummy = (table->ColumnsEnabledCount < table->ColumnsCount || table->VisibleMaskByIndex != table->EnabledMaskByIndex) ? +1 : 0; + const int channels_for_dummy = (table->ColumnsEnabledCount < table->ColumnsCount || (memcmp(table->VisibleMaskByIndex, table->EnabledMaskByIndex, ImBitArrayGetStorageSizeInBytes(table->ColumnsCount)) != 0)) ? +1 : 0; const int channels_total = channels_for_bg + (channels_for_row * freeze_row_multiplier) + channels_for_dummy; table->DrawSplitter->Split(table->InnerWindow->DrawList, channels_total); table->DummyDrawChannel = (ImGuiTableDrawChannelIdx)((channels_for_dummy > 0) ? channels_total - 1 : -1); @@ -2366,19 +2380,26 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) // Track which groups we are going to attempt to merge, and which channels goes into each group. struct MergeGroup { - ImRect ClipRect; - int ChannelsCount; - ImBitArray ChannelsMask; - - MergeGroup() { ChannelsCount = 0; } + ImRect ClipRect; + int ChannelsCount; + ImBitArrayPtr ChannelsMask; }; int merge_group_mask = 0x00; - MergeGroup merge_groups[4]; + MergeGroup merge_groups[4] = {}; + + // Use a reusable temp buffer for the merge masks as they are dynamically sized. + const int max_draw_channels = (4 + table->ColumnsCount * 2); + const int size_for_masks_bitarrays_one = (int)ImBitArrayGetStorageSizeInBytes(max_draw_channels); + g.TempBuffer.reserve(size_for_masks_bitarrays_one * 5); + memset(g.TempBuffer.Data, 0, size_for_masks_bitarrays_one * 5); + for (int n = 0; n < IM_ARRAYSIZE(merge_groups); n++) + merge_groups[n].ChannelsMask = (ImBitArrayPtr)(void*)(g.TempBuffer.Data + (size_for_masks_bitarrays_one * n)); + ImBitArrayPtr remaining_mask = (ImBitArrayPtr)(void*)(g.TempBuffer.Data + (size_for_masks_bitarrays_one * 4)); // 1. Scan channels and take note of those which can be merged for (int column_n = 0; column_n < table->ColumnsCount; column_n++) { - if ((table->VisibleMaskByIndex & ((ImU64)1 << column_n)) == 0) + if (!IM_BITARRAY_TESTBIT(table->VisibleMaskByIndex, column_n)) continue; ImGuiTableColumn* column = &table->Columns[column_n]; @@ -2410,11 +2431,11 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) } const int merge_group_n = (has_freeze_h && column_n < table->FreezeColumnsCount ? 0 : 1) + (has_freeze_v && merge_group_sub_n == 0 ? 0 : 2); - IM_ASSERT(channel_no < IMGUI_TABLE_MAX_DRAW_CHANNELS); + IM_ASSERT(channel_no < max_draw_channels); MergeGroup* merge_group = &merge_groups[merge_group_n]; if (merge_group->ChannelsCount == 0) merge_group->ClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX); - merge_group->ChannelsMask.SetBit(channel_no); + ImBitArraySetBit(merge_group->ChannelsMask, channel_no); merge_group->ChannelsCount++; merge_group->ClipRect.Add(src_channel->_CmdBuffer[0].ClipRect); merge_group_mask |= (1 << merge_group_n); @@ -2450,9 +2471,8 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) const int LEADING_DRAW_CHANNELS = 2; g.DrawChannelsTempMergeBuffer.resize(splitter->_Count - LEADING_DRAW_CHANNELS); // Use shared temporary storage so the allocation gets amortized ImDrawChannel* dst_tmp = g.DrawChannelsTempMergeBuffer.Data; - ImBitArray remaining_mask; // We need 132-bit of storage - remaining_mask.SetBitRange(LEADING_DRAW_CHANNELS, splitter->_Count); - remaining_mask.ClearBit(table->Bg2DrawChannelUnfrozen); + ImBitArraySetBitRange(remaining_mask, LEADING_DRAW_CHANNELS, splitter->_Count); + ImBitArrayClearBit(remaining_mask, table->Bg2DrawChannelUnfrozen); IM_ASSERT(has_freeze_v == false || table->Bg2DrawChannelUnfrozen != TABLE_DRAW_CHANNEL_BG2_FROZEN); int remaining_count = splitter->_Count - (has_freeze_v ? LEADING_DRAW_CHANNELS + 1 : LEADING_DRAW_CHANNELS); //ImRect host_rect = (table->InnerWindow == table->OuterWindow) ? table->InnerClipRect : table->HostClipRect; @@ -2485,14 +2505,14 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) GetOverlayDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200)); #endif remaining_count -= merge_group->ChannelsCount; - for (int n = 0; n < IM_ARRAYSIZE(remaining_mask.Storage); n++) - remaining_mask.Storage[n] &= ~merge_group->ChannelsMask.Storage[n]; + for (int n = 0; n < (size_for_masks_bitarrays_one >> 2); n++) + remaining_mask[n] &= ~merge_group->ChannelsMask[n]; for (int n = 0; n < splitter->_Count && merge_channels_count != 0; n++) { // Copy + overwrite new clip rect - if (!merge_group->ChannelsMask.TestBit(n)) + if (!IM_BITARRAY_TESTBIT(merge_group->ChannelsMask, n)) continue; - merge_group->ChannelsMask.ClearBit(n); + IM_BITARRAY_CLEARBIT(merge_group->ChannelsMask, n); merge_channels_count--; ImDrawChannel* channel = &splitter->_Channels[n]; @@ -2510,7 +2530,7 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) // Append unmergeable channels that we didn't reorder at the end of the list for (int n = 0; n < splitter->_Count && remaining_count != 0; n++) { - if (!remaining_mask.TestBit(n)) + if (!IM_BITARRAY_TESTBIT(remaining_mask, n)) continue; ImDrawChannel* channel = &splitter->_Channels[n]; memcpy(dst_tmp++, channel, sizeof(ImDrawChannel)); @@ -2542,7 +2562,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) { for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { - if (!(table->EnabledMaskByDisplayOrder & ((ImU64)1 << order_n))) + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) continue; const int column_n = table->DisplayOrderToIndex[order_n]; @@ -2870,10 +2890,9 @@ void ImGui::TableHeadersRow() continue; // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them) - // - in your own code you may omit the PushID/PopID all-together, provided you know they won't collide - // - table->InstanceCurrent is only >0 when we use multiple BeginTable/EndTable calls with same identifier. + // In your own code you may omit the PushID/PopID all-together, provided you know they won't collide. const char* name = (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_NoHeaderLabel) ? "" : TableGetColumnName(column_n); - PushID(table->InstanceCurrent * table->ColumnsCount + column_n); + PushID(column_n); TableHeader(name); PopID(); } diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index 0a1ea678..ab43ac05 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.2 +// dear imgui, v1.89.4 // (widgets code) /* @@ -32,12 +32,12 @@ Index of this file: #define _CRT_SECURE_NO_WARNINGS #endif -#include "imgui.h" -#ifndef IMGUI_DISABLE - #ifndef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS #endif + +#include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_internal.h" // System includes @@ -274,7 +274,6 @@ void ImGui::TextV(const char* fmt, va_list args) if (window->SkipItems) return; - // FIXME-OPT: Handle the %s shortcut? const char* text, *text_end; ImFormatStringToTempBufferV(&text, &text_end, fmt, args); TextEx(text, text_end, ImGuiTextFlags_NoWidthForLargeClippedText); @@ -291,10 +290,7 @@ void ImGui::TextColored(const ImVec4& col, const char* fmt, ...) void ImGui::TextColoredV(const ImVec4& col, const char* fmt, va_list args) { PushStyleColor(ImGuiCol_Text, col); - if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) - TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting - else - TextV(fmt, args); + TextV(fmt, args); PopStyleColor(); } @@ -310,10 +306,7 @@ void ImGui::TextDisabledV(const char* fmt, va_list args) { ImGuiContext& g = *GImGui; PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]); - if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) - TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting - else - TextV(fmt, args); + TextV(fmt, args); PopStyleColor(); } @@ -328,13 +321,10 @@ void ImGui::TextWrapped(const char* fmt, ...) void ImGui::TextWrappedV(const char* fmt, va_list args) { ImGuiContext& g = *GImGui; - bool need_backup = (g.CurrentWindow->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set + const bool need_backup = (g.CurrentWindow->DC.TextWrapPos < 0.0f); // Keep existing wrap position if one is already set if (need_backup) PushTextWrapPos(0.0f); - if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) - TextEx(va_arg(args, const char*), NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting - else - TextV(fmt, args); + TextV(fmt, args); if (need_backup) PopTextWrapPos(); } @@ -508,8 +498,9 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool g.HoveredWindow = window; #ifdef IMGUI_ENABLE_TEST_ENGINE + // Alternate registration spot, for when caller didn't use ItemAdd() if (id != 0 && g.LastItemData.ID != id) - IMGUI_TEST_ENGINE_ITEM_ADD(bb, id); + IMGUI_TEST_ENGINE_ITEM_ADD(id, bb, NULL); #endif bool pressed = false; @@ -619,10 +610,11 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool bool nav_activated_by_inputs = (g.NavActivatePressedId == id); if (!nav_activated_by_inputs && (flags & ImGuiButtonFlags_Repeat)) { - // Avoid pressing both keys from triggering double amount of repeat events + // Avoid pressing multiple keys from triggering excessive amount of repeat events const ImGuiKeyData* key1 = GetKeyData(ImGuiKey_Space); - const ImGuiKeyData* key2 = GetKeyData(ImGuiKey_NavGamepadActivate); - const float t1 = ImMax(key1->DownDuration, key2->DownDuration); + const ImGuiKeyData* key2 = GetKeyData(ImGuiKey_Enter); + const ImGuiKeyData* key3 = GetKeyData(ImGuiKey_NavGamepadActivate); + const float t1 = ImMax(ImMax(key1->DownDuration, key2->DownDuration), key3->DownDuration); nav_activated_by_inputs = CalcTypematicRepeatAmount(t1 - g.IO.DeltaTime, t1, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0; } if (nav_activated_by_code || nav_activated_by_inputs) @@ -1042,7 +1034,7 @@ void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& // ImageButton() is flawed as 'id' is always derived from 'texture_id' (see #2464 #1390) // We provide this internal helper to write your own variant while we figure out how to redesign the public ImageButton() API. -bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col) +bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -1056,7 +1048,7 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size return false; bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held); + bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); @@ -1394,6 +1386,7 @@ void ImGui::AlignTextToFramePadding() } // Horizontal/vertical separating line +// FIXME: Surprisingly, this seemingly simple widget is adjacent to MANY different legacy/tricky layout issues. void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) { ImGuiWindow* window = GetCurrentWindow(); @@ -1403,20 +1396,19 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) ImGuiContext& g = *GImGui; IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical))); // Check that only 1 option is selected - float thickness_draw = 1.0f; - float thickness_layout = 0.0f; + const float thickness = 1.0f; // Cannot use g.Style.SeparatorTextSize yet for various reasons. if (flags & ImGuiSeparatorFlags_Vertical) { - // Vertical separator, for menu bars (use current line height). Not exposed because it is misleading and it doesn't have an effect on regular layout. + // Vertical separator, for menu bars (use current line height). float y1 = window->DC.CursorPos.y; float y2 = window->DC.CursorPos.y + window->DC.CurrLineSize.y; - const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + thickness_draw, y2)); - ItemSize(ImVec2(thickness_layout, 0.0f)); + const ImRect bb(ImVec2(window->DC.CursorPos.x, y1), ImVec2(window->DC.CursorPos.x + thickness, y2)); + ItemSize(ImVec2(thickness, 0.0f)); if (!ItemAdd(bb, 0)) return; // Draw - window->DrawList->AddLine(ImVec2(bb.Min.x, bb.Min.y), ImVec2(bb.Min.x, bb.Max.y), GetColorU32(ImGuiCol_Separator)); + window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Separator)); if (g.LogEnabled) LogText(" |"); } @@ -1444,13 +1436,14 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) // We don't provide our width to the layout so that it doesn't get feed back into AutoFit // FIXME: This prevents ->CursorMaxPos based bounding box evaluation from working (e.g. TableEndCell) - const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness_draw)); - ItemSize(ImVec2(0.0f, thickness_layout)); + const float thickness_for_layout = (thickness == 1.0f) ? 0.0f : thickness; // FIXME: See 1.70/1.71 Separator() change: makes legacy 1-px separator not affect layout yet. Should change. + const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness)); + ItemSize(ImVec2(0.0f, thickness_for_layout)); const bool item_visible = ItemAdd(bb, 0); if (item_visible) { // Draw - window->DrawList->AddLine(bb.Min, ImVec2(bb.Max.x, bb.Min.y), GetColorU32(ImGuiCol_Separator)); + window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Separator)); if (g.LogEnabled) LogRenderedText(&bb.Min, "--------------------------------\n"); @@ -1476,6 +1469,71 @@ void ImGui::Separator() SeparatorEx(flags); } +void ImGui::SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_w) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiStyle& style = g.Style; + + const ImVec2 label_size = CalcTextSize(label, label_end, false); + const ImVec2 pos = window->DC.CursorPos; + const ImVec2 padding = style.SeparatorTextPadding; + + const float separator_thickness = style.SeparatorTextBorderSize; + const ImVec2 min_size(label_size.x + extra_w + padding.x * 2.0f, ImMax(label_size.y + padding.y * 2.0f, separator_thickness)); + const ImRect bb(pos, ImVec2(window->WorkRect.Max.x, pos.y + min_size.y)); + const float text_baseline_y = ImFloor((bb.GetHeight() - label_size.y) * style.SeparatorTextAlign.y + 0.99999f); //ImMax(padding.y, ImFloor((style.SeparatorTextSize - label_size.y) * 0.5f)); + ItemSize(min_size, text_baseline_y); + if (!ItemAdd(bb, id)) + return; + + const float sep1_x1 = pos.x; + const float sep2_x2 = bb.Max.x; + const float seps_y = ImFloor((bb.Min.y + bb.Max.y) * 0.5f + 0.99999f); + + const float label_avail_w = ImMax(0.0f, sep2_x2 - sep1_x1 - padding.x * 2.0f); + const ImVec2 label_pos(pos.x + padding.x + ImMax(0.0f, (label_avail_w - label_size.x - extra_w) * style.SeparatorTextAlign.x), pos.y + text_baseline_y); // FIXME-ALIGN + + // This allows using SameLine() to position something in the 'extra_w' + window->DC.CursorPosPrevLine.x = label_pos.x + label_size.x; + + const ImU32 separator_col = GetColorU32(ImGuiCol_Separator); + if (label_size.x > 0.0f) + { + const float sep1_x2 = label_pos.x - style.ItemSpacing.x; + const float sep2_x1 = label_pos.x + label_size.x + extra_w + style.ItemSpacing.x; + if (sep1_x2 > sep1_x1 && separator_thickness > 0.0f) + window->DrawList->AddLine(ImVec2(sep1_x1, seps_y), ImVec2(sep1_x2, seps_y), separator_col, separator_thickness); + if (sep2_x2 > sep2_x1 && separator_thickness > 0.0f) + window->DrawList->AddLine(ImVec2(sep2_x1, seps_y), ImVec2(sep2_x2, seps_y), separator_col, separator_thickness); + if (g.LogEnabled) + LogSetNextTextDecoration("---", NULL); + RenderTextEllipsis(window->DrawList, label_pos, ImVec2(bb.Max.x, bb.Max.y + style.ItemSpacing.y), bb.Max.x, bb.Max.x, label, label_end, &label_size); + } + else + { + if (g.LogEnabled) + LogText("---"); + if (separator_thickness > 0.0f) + window->DrawList->AddLine(ImVec2(sep1_x1, seps_y), ImVec2(sep2_x2, seps_y), separator_col, separator_thickness); + } +} + +void ImGui::SeparatorText(const char* label) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return; + + // The SeparatorText() vs SeparatorTextEx() distinction is designed to be considerate that we may want: + // - allow headers to be draggable items (would require a stable ID + a noticeable highlight) + // - this high-level entry point to allow formatting? (may require ID separate from formatted string) + // - because of this we probably can't turn 'const char* label' into 'const char* fmt, ...' + // Otherwise, we can decide that users wanting to drag this would layout a dedicated drag-item, + // and then we can turn this into a format function. + SeparatorTextEx(0, label, FindRenderedTextEnd(label), 0.0f); +} + // Using 'hover_visibility_delay' allows us to hide the highlight and mouse cursor for a short time, which can be convenient to reduce visual noise. bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend, float hover_visibility_delay, ImU32 bg_col) { @@ -1705,7 +1763,12 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags if (flags & ImGuiComboFlags_HeightRegular) popup_max_height_in_items = 8; else if (flags & ImGuiComboFlags_HeightSmall) popup_max_height_in_items = 4; else if (flags & ImGuiComboFlags_HeightLarge) popup_max_height_in_items = 20; - SetNextWindowSizeConstraints(ImVec2(w, 0.0f), ImVec2(FLT_MAX, CalcMaxPopupHeightFromItemCount(popup_max_height_in_items))); + ImVec2 constraint_min(0.0f, 0.0f), constraint_max(FLT_MAX, FLT_MAX); + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.x <= 0.0f) // Don't apply constraints if user specified a size + constraint_min.x = w; + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0 || g.NextWindowData.SizeVal.y <= 0.0f) + constraint_max.y = CalcMaxPopupHeightFromItemCount(popup_max_height_in_items); + SetNextWindowSizeConstraints(constraint_min, constraint_max); } // This is essentially a specialized version of BeginPopupEx() @@ -2349,18 +2412,18 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = hovered && IsMouseClicked(0, id); const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2 && TestKeyOwner(ImGuiKey_MouseLeft, id)); - const bool make_active = (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id || g.NavActivateInputId == id); + const bool make_active = (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id); if (make_active && (clicked || double_clicked)) SetKeyOwner(ImGuiKey_MouseLeft, id); if (make_active && temp_input_allowed) - if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || g.NavActivateInputId == id) + if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) temp_input_is_active = true; // (Optional) simple click (without moving) turns Drag into an InputText if (g.IO.ConfigDragClickToInputText && temp_input_allowed && !temp_input_is_active) if (g.ActiveId == id && hovered && g.IO.MouseReleased[0] && !IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR)) { - g.NavActivateId = g.NavActivateInputId = id; + g.NavActivateId = id; g.NavActivateFlags = ImGuiActivateFlags_PreferInput; temp_input_is_active = true; } @@ -2401,7 +2464,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (temp_input_allowed ? ImGuiItemStatusFlags_Inputable : 0)); return value_changed; } @@ -2941,11 +3004,11 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat // Tabbing or CTRL-clicking on Slider turns it into an input box const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = hovered && IsMouseClicked(0, id); - const bool make_active = (input_requested_by_tabbing || clicked || g.NavActivateId == id || g.NavActivateInputId == id); + const bool make_active = (input_requested_by_tabbing || clicked || g.NavActivateId == id); if (make_active && clicked) SetKeyOwner(ImGuiKey_MouseLeft, id); if (make_active && temp_input_allowed) - if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || g.NavActivateInputId == id) + if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) temp_input_is_active = true; if (make_active && !temp_input_is_active) @@ -2989,7 +3052,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (label_size.x > 0.0f) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (temp_input_allowed ? ImGuiItemStatusFlags_Inputable : 0)); return value_changed; } @@ -3103,7 +3166,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d const bool hovered = ItemHoverable(frame_bb, id); const bool clicked = hovered && IsMouseClicked(0, id); - if (clicked || g.NavActivateId == id || g.NavActivateInputId == id) + if (clicked || g.NavActivateId == id) { if (clicked) SetKeyOwner(ImGuiKey_MouseLeft, id); @@ -3396,7 +3459,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); - IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); // Step buttons const ImVec2 backup_frame_padding = style.FramePadding; @@ -3780,7 +3843,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons return; // Contrary to STB_TEXTEDIT_INSERTCHARS() this is working in the UTF8 buffer, hence the mildly similar code (until we remove the U16 buffer altogether!) - ImGuiContext& g = *GImGui; + ImGuiContext& g = *Ctx; ImGuiInputTextState* edit_state = &g.InputTextState; IM_ASSERT(edit_state->ID != 0 && g.ActiveId == edit_state->ID); IM_ASSERT(Buf == edit_state->TextA.Data); @@ -3884,8 +3947,9 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // Custom callback filter if (flags & ImGuiInputTextFlags_CallbackCharFilter) { + ImGuiContext& g = *GImGui; ImGuiInputTextCallbackData callback_data; - memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); + callback_data.Ctx = &g; callback_data.EventFlag = ImGuiInputTextFlags_CallbackCharFilter; callback_data.EventChar = (ImWchar)c; callback_data.Flags = flags; @@ -4030,7 +4094,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiInputTextState* state = GetInputTextState(id); const bool input_requested_by_tabbing = (item_status_flags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; - const bool input_requested_by_nav = (g.ActiveId != id) && ((g.NavActivateInputId == id) || (g.NavActivateId == id && g.NavInputSource == ImGuiInputSource_Keyboard)); + const bool input_requested_by_nav = (g.ActiveId != id) && ((g.NavActivateId == id) && ((g.NavActivateFlags & ImGuiActivateFlags_PreferInput) || (g.NavInputSource == ImGuiInputSource_Keyboard))); const bool user_clicked = hovered && io.MouseClicked[0]; const bool user_scroll_finish = is_multiline && state != NULL && g.ActiveId == 0 && g.ActiveIdPreviousFrame == GetWindowScrollbarID(draw_window, ImGuiAxis_Y); @@ -4096,6 +4160,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->Stb.insert_mode = 1; // stb field name is indeed incorrect (see #2863) } + const bool is_osx = io.ConfigMacOSXBehaviors; if (g.ActiveId != id && init_make_active) { IM_ASSERT(state && state->ID == id); @@ -4118,8 +4183,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ SetKeyOwner(ImGuiKey_PageUp, id); SetKeyOwner(ImGuiKey_PageDown, id); } + if (is_osx) + SetKeyOwner(ImGuiMod_Alt, id); if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. - SetKeyOwner(ImGuiKey_Tab, id); + SetShortcutRouting(ImGuiKey_Tab, id); } // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function) @@ -4187,7 +4254,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y) : (g.FontSize * 0.5f)); - const bool is_osx = io.ConfigMacOSXBehaviors; if (select_all) { state->SelectAll(); @@ -4250,8 +4316,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We expect backends to emit a Tab key but some also emit a Tab character which we ignore (#2467, #1336) // (For Tab and Enter: Win32/SFML/Allegro are sending both keys and chars, GLFW and SDL are only sending keys. For Space they all send all threes) - const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); - if ((flags & ImGuiInputTextFlags_AllowTabInput) && IsKeyPressed(ImGuiKey_Tab) && !ignore_char_inputs && !io.KeyShift && !is_readonly) + if ((flags & ImGuiInputTextFlags_AllowTabInput) && Shortcut(ImGuiKey_Tab, id) && !is_readonly) { unsigned int c = '\t'; // Insert TAB if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) @@ -4260,6 +4325,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Process regular text input (before we check for Return because using some IME will effectively send a Return?) // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters. + const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); if (io.InputQueueCharacters.Size > 0) { if (!ignore_char_inputs && !is_readonly && !input_requested_by_nav) @@ -4288,7 +4354,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state->Stb.row_count_per_page = row_count_per_page; const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); - const bool is_osx = io.ConfigMacOSXBehaviors; const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End @@ -4317,7 +4382,16 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (IsKeyPressed(ImGuiKey_PageDown) && is_multiline) { state->OnKeyPressed(STB_TEXTEDIT_K_PGDOWN | k_mask); scroll_y += row_count_per_page * g.FontSize; } else if (IsKeyPressed(ImGuiKey_Home)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTSTART | k_mask : STB_TEXTEDIT_K_LINESTART | k_mask); } else if (IsKeyPressed(ImGuiKey_End)) { state->OnKeyPressed(io.KeyCtrl ? STB_TEXTEDIT_K_TEXTEND | k_mask : STB_TEXTEDIT_K_LINEEND | k_mask); } - else if (IsKeyPressed(ImGuiKey_Delete) && !is_readonly && !is_cut) { state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); } + else if (IsKeyPressed(ImGuiKey_Delete) && !is_readonly && !is_cut) + { + if (!state->HasSelection()) + { + // OSX doesn't seem to have Super+Delete to delete until end-of-line, so we don't emulate that (as opposed to Super+Backspace) + if (is_wordmove_key_down) + state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); + } + state->OnKeyPressed(STB_TEXTEDIT_K_DELETE | k_mask); + } else if (IsKeyPressed(ImGuiKey_Backspace) && !is_readonly) { if (!state->HasSelection()) @@ -4407,12 +4481,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const int clipboard_len = (int)strlen(clipboard); ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len + 1) * sizeof(ImWchar)); int clipboard_filtered_len = 0; - for (const char* s = clipboard; *s; ) + for (const char* s = clipboard; *s != 0; ) { unsigned int c; s += ImTextCharFromUtf8(&c, s, NULL); - if (c == 0) - break; if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; @@ -4490,7 +4562,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment. ImGuiInputTextFlags event_flag = 0; ImGuiKey event_key = ImGuiKey_None; - if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && IsKeyPressed(ImGuiKey_Tab)) + if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && Shortcut(ImGuiKey_Tab, id)) { event_flag = ImGuiInputTextFlags_CallbackCompletion; event_key = ImGuiKey_Tab; @@ -4517,7 +4589,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (event_flag) { ImGuiInputTextCallbackData callback_data; - memset(&callback_data, 0, sizeof(ImGuiInputTextCallbackData)); + callback_data.Ctx = &g; callback_data.EventFlag = event_flag; callback_data.Flags = flags; callback_data.UserData = callback_user_data; @@ -4580,6 +4652,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_resizable) { ImGuiInputTextCallbackData callback_data; + callback_data.Ctx = &g; callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; callback_data.Flags = flags; callback_data.Buf = buf; @@ -4846,7 +4919,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (value_changed && !(flags & ImGuiInputTextFlags_NoMarkEdited)) MarkItemEdited(id); - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); if ((flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0) return validated; else @@ -4861,7 +4934,7 @@ void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state) ImStb::StbUndoState* undo_state = &stb_state->undostate; Text("ID: 0x%08X, ActiveID: 0x%08X", state->ID, g.ActiveId); DebugLocateItemOnHover(state->ID); - Text("CurLenW: %d, CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenA, state->CurLenW, stb_state->cursor, stb_state->select_start, stb_state->select_end); + Text("CurLenW: %d, CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenW, state->CurLenA, stb_state->cursor, stb_state->select_start, stb_state->select_end); Text("has_preferred_x: %d (%.2f)", stb_state->has_preferred_x, stb_state->preferred_x); Text("undo_point: %d, redo_point: %d, undo_char_point: %d, redo_char_point: %d", undo_state->undo_point, undo_state->redo_point, undo_state->undo_char_point, undo_state->redo_char_point); if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 15), true)) // Visualize undo state @@ -4909,28 +4982,32 @@ bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flag return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha); } +static void ColorEditRestoreH(const float* col, float* H) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(g.ColorEditCurrentID != 0); + if (g.ColorEditSavedID != g.ColorEditCurrentID || g.ColorEditSavedColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) + return; + *H = g.ColorEditSavedHue; +} + // ColorEdit supports RGB and HSV inputs. In case of RGB input resulting color may have undefined hue and/or saturation. // Since widget displays both RGB and HSV values we must preserve hue and saturation to prevent these values resetting. static void ColorEditRestoreHS(const float* col, float* H, float* S, float* V) { - // This check is optional. Suppose we have two color widgets side by side, both widgets display different colors, but both colors have hue and/or saturation undefined. - // With color check: hue/saturation is preserved in one widget. Editing color in one widget would reset hue/saturation in another one. - // Without color check: common hue/saturation would be displayed in all widgets that have hue/saturation undefined. - // g.ColorEditLastColor is stored as ImU32 RGB value: this essentially gives us color equality check with reduced precision. - // Tiny external color changes would not be detected and this check would still pass. This is OK, since we only restore hue/saturation _only_ if they are undefined, - // therefore this change flipping hue/saturation from undefined to a very tiny value would still be represented in color picker. ImGuiContext& g = *GImGui; - if (g.ColorEditLastColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) + IM_ASSERT(g.ColorEditCurrentID != 0); + if (g.ColorEditSavedID != g.ColorEditCurrentID || g.ColorEditSavedColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) return; // When S == 0, H is undefined. // When H == 1 it wraps around to 0. - if (*S == 0.0f || (*H == 0.0f && g.ColorEditLastHue == 1)) - *H = g.ColorEditLastHue; + if (*S == 0.0f || (*H == 0.0f && g.ColorEditSavedHue == 1)) + *H = g.ColorEditSavedHue; // When V == 0, S is undefined. if (*V == 0.0f) - *S = g.ColorEditLastSat; + *S = g.ColorEditSavedSat; } // Edit colors components (each component in 0.0f..1.0f range). @@ -4953,6 +5030,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag BeginGroup(); PushID(label); + const bool set_current_color_edit_id = (g.ColorEditCurrentID == 0); + if (set_current_color_edit_id) + g.ColorEditCurrentID = window->IDStack.back(); // If we're not showing any slider there's no point in doing any HSV conversions const ImGuiColorEditFlags flags_untouched = flags; @@ -4986,7 +5066,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV)) { - // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. + // Hue is lost when converting from grayscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); ColorEditRestoreHS(col, &f[0], &f[1], &f[2]); } @@ -5125,10 +5205,11 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag f[n] = i[n] / 255.0f; if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB)) { - g.ColorEditLastHue = f[0]; - g.ColorEditLastSat = f[1]; + g.ColorEditSavedHue = f[0]; + g.ColorEditSavedSat = f[1]; ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]); - g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(f[0], f[1], f[2], 0)); + g.ColorEditSavedID = g.ColorEditCurrentID; + g.ColorEditSavedColor = ColorConvertFloat4ToU32(ImVec4(f[0], f[1], f[2], 0)); } if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV)) ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]); @@ -5140,6 +5221,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag col[3] = f[3]; } + if (set_current_color_edit_id) + g.ColorEditCurrentID = 0; PopID(); EndGroup(); @@ -5213,6 +5296,9 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl g.NextItemData.ClearFlags(); PushID(label); + const bool set_current_color_edit_id = (g.ColorEditCurrentID == 0); + if (set_current_color_edit_id) + g.ColorEditCurrentID = window->IDStack.back(); BeginGroup(); if (!(flags & ImGuiColorEditFlags_NoSidePreview)) @@ -5261,7 +5347,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl float R = col[0], G = col[1], B = col[2]; if (flags & ImGuiColorEditFlags_InputRGB) { - // Hue is lost when converting from greyscale rgb (saturation=0). Restore it. + // Hue is lost when converting from grayscale rgb (saturation=0). Restore it. ColorConvertRGBtoHSV(R, G, B, H, S, V); ColorEditRestoreHS(col, &H, &S, &V); } @@ -5316,10 +5402,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size - 1)); V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); - - // Greatly reduces hue jitter and reset to 0 when hue == 255 and color is rapidly modified using SV square. - if (g.ColorEditLastColor == ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0))) - H = g.ColorEditLastHue; + ColorEditRestoreH(col, &H); // Greatly reduces hue jitter and reset to 0 when hue == 255 and color is rapidly modified using SV square. value_changed = value_changed_sv = true; } if (!(flags & ImGuiColorEditFlags_NoOptions)) @@ -5394,9 +5477,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if (flags & ImGuiColorEditFlags_InputRGB) { ColorConvertHSVtoRGB(H, S, V, col[0], col[1], col[2]); - g.ColorEditLastHue = H; - g.ColorEditLastSat = S; - g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)); + g.ColorEditSavedHue = H; + g.ColorEditSavedSat = S; + g.ColorEditSavedID = g.ColorEditCurrentID; + g.ColorEditSavedColor = ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)); } else if (flags & ImGuiColorEditFlags_InputHSV) { @@ -5560,6 +5644,8 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if (value_changed && g.LastItemData.ID != 0) // In case of ID collision, the second EndGroup() won't catch g.ActiveId MarkItemEdited(g.LastItemData.ID); + if (set_current_color_edit_id) + g.ColorEditCurrentID = 0; PopID(); return value_changed; @@ -5673,7 +5759,8 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags { ImGuiContext& g = *GImGui; - BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None); + if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None)) + return; const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; if (text_end > text) { @@ -6548,7 +6635,7 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v // - others https://github.com/ocornut/imgui/wiki/Useful-Extensions //------------------------------------------------------------------------- -int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, ImVec2 frame_size) +int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset, const char* overlay_text, float scale_min, float scale_max, const ImVec2& size_arg) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -6559,10 +6646,7 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get const ImGuiID id = window->GetID(label); const ImVec2 label_size = CalcTextSize(label, NULL, true); - if (frame_size.x == 0.0f) - frame_size.x = CalcItemWidth(); - if (frame_size.y == 0.0f) - frame_size.y = label_size.y + (style.FramePadding.y * 2); + const ImVec2 frame_size = CalcItemSize(size_arg, CalcItemWidth(), label_size.y + style.FramePadding.y * 2.0f); const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); @@ -7027,7 +7111,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); - pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, 0.0f)); + pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, label_size.y)); RenderText(text_pos, label); PopStyleVar(); window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). @@ -7043,7 +7127,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); - pressed = Selectable("", menu_is_open, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + pressed = Selectable("", menu_is_open, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, label_size.y)); RenderText(text_pos, label); if (icon_w > 0.0f) RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon); @@ -7235,7 +7319,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); - pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f)); + pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, label_size.y)); if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) { RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label); @@ -7287,11 +7371,17 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool* p_selected, // - TabBarCalcTabID() [Internal] // - TabBarCalcMaxTabWidth() [Internal] // - TabBarFindTabById() [Internal] +// - TabBarFindTabByOrder() [Internal] +// - TabBarGetCurrentTab() [Internal] +// - TabBarGetTabName() [Internal] // - TabBarRemoveTab() [Internal] // - TabBarCloseTab() [Internal] // - TabBarScrollClamp() [Internal] // - TabBarScrollToTab() [Internal] -// - TabBarQueueChangeTabOrder() [Internal] +// - TabBarQueueFocus() [Internal] +// - TabBarQueueReorder() [Internal] +// - TabBarProcessReorderFromMousePos() [Internal] +// - TabBarProcessReorder() [Internal] // - TabBarScrollingButtons() [Internal] // - TabBarTabListPopupButton() [Internal] //------------------------------------------------------------------------- @@ -7416,6 +7506,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG tab_bar->ItemSpacingY = g.Style.ItemSpacing.y; tab_bar->FramePadding = g.Style.FramePadding; tab_bar->TabsActiveCount = 0; + tab_bar->LastTabItemIdx = -1; tab_bar->BeginCount = 1; // Set cursor pos in a way which only be used in the off-chance the user erroneously submits item before BeginTabItem(): items will overlap @@ -7464,6 +7555,7 @@ void ImGui::EndTabBar() if (tab_bar->BeginCount > 1) window->DC.CursorPos = tab_bar->BackupCursorPos; + tab_bar->LastTabItemIdx = -1; if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) PopID(); @@ -7573,7 +7665,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Refresh tab width immediately, otherwise changes of style e.g. style.FramePadding.x would noticeably lag in the tab bar. // Additionally, when using TabBarAddTab() to manipulate tab bar order we occasionally insert new tabs that don't have a width yet, // and we cannot wait for the next BeginTabItem() call. We cannot compute this width within TabBarAddTab() because font size depends on the active window. - const char* tab_name = tab_bar->GetTabName(tab); + const char* tab_name = TabBarGetTabName(tab_bar, tab); const bool has_close_button_or_unsaved_marker = (tab->Flags & ImGuiTabItemFlags_NoCloseButton) == 0 || (tab->Flags & ImGuiTabItemFlags_UnsavedDocument); tab->ContentWidth = (tab->RequestedWidth >= 0.0f) ? tab->RequestedWidth : TabItemCalcSize(tab_name, has_close_button_or_unsaved_marker).x; @@ -7652,6 +7744,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) { ImGuiTabItem* tab = &tab_bar->Tabs[section_tab_index + tab_n]; tab->Offset = tab_offset; + tab->NameOffset = -1; tab_offset += tab->Width + (tab_n < section->TabCount - 1 ? g.Style.ItemInnerSpacing.x : 0.0f); } tab_bar->WidthAllTabs += ImMax(section->Width + section->Spacing, 0.0f); @@ -7659,6 +7752,9 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) section_tab_index += section->TabCount; } + // Clear name buffers + tab_bar->TabsNames.Buf.resize(0); + // If we have lost the selected tab, select the next most recently active one if (found_selected_tab_id == false) tab_bar->SelectedTabId = 0; @@ -7690,10 +7786,6 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) tab_bar->ScrollingRectMinX = tab_bar->BarRect.Min.x + sections[0].Width + sections[0].Spacing; tab_bar->ScrollingRectMaxX = tab_bar->BarRect.Max.x - sections[2].Width - sections[1].Spacing; - // Clear name buffers - if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) - tab_bar->TabsNames.Buf.resize(0); - // Actual layout in host window (we don't do it in BeginTabBar() so as not to waste an extra frame) ImGuiWindow* window = g.CurrentWindow; window->DC.CursorPos = tab_bar->BarRect.Min; @@ -7734,7 +7826,30 @@ ImGuiTabItem* ImGui::TabBarFindTabByID(ImGuiTabBar* tab_bar, ImGuiID tab_id) return NULL; } -// The *TabId fields be already set by the docking system _before_ the actual TabItem was created, so we clear them regardless. +// Order = visible order, not submission order! (which is tab->BeginOrder) +ImGuiTabItem* ImGui::TabBarFindTabByOrder(ImGuiTabBar* tab_bar, int order) +{ + if (order < 0 || order >= tab_bar->Tabs.Size) + return NULL; + return &tab_bar->Tabs[order]; +} + +ImGuiTabItem* ImGui::TabBarGetCurrentTab(ImGuiTabBar* tab_bar) +{ + if (tab_bar->LastTabItemIdx <= 0 || tab_bar->LastTabItemIdx >= tab_bar->Tabs.Size) + return NULL; + return &tab_bar->Tabs[tab_bar->LastTabItemIdx]; +} + +const char* ImGui::TabBarGetTabName(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) +{ + if (tab->NameOffset == -1) + return "N/A"; + IM_ASSERT(tab->NameOffset < tab_bar->TabsNames.Buf.Size); + return tab_bar->TabsNames.Buf.Data + tab->NameOffset; +} + +// The *TabId fields are already set by the docking system _before_ the actual TabItem was created, so we clear them regardless. void ImGui::TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id) { if (ImGuiTabItem* tab = TabBarFindTabByID(tab_bar, tab_id)) @@ -7765,7 +7880,7 @@ void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) { // Actually select before expecting closure attempt (on an UnsavedDocument tab user is expect to e.g. show a popup) if (tab_bar->VisibleTabId != tab->ID) - tab_bar->NextSelectedTabId = tab->ID; + TabBarQueueFocus(tab_bar, tab); } } @@ -7786,7 +7901,7 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGui ImGuiContext& g = *GImGui; float margin = g.FontSize * 1.0f; // When to scroll to make Tab N+1 visible always make a bit of N visible to suggest more scrolling area (since we don't have a scrollbar) - int order = tab_bar->GetTabOrder(tab); + int order = TabBarGetTabOrder(tab_bar, tab); // Scrolling happens only in the central section (leading/trailing sections are not scrolling) // FIXME: This is all confusing. @@ -7810,7 +7925,12 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGui } } -void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, int offset) +void ImGui::TabBarQueueFocus(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) +{ + tab_bar->NextSelectedTabId = tab->ID; +} + +void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, int offset) { IM_ASSERT(offset != 0); IM_ASSERT(tab_bar->ReorderRequestTabId == 0); @@ -7818,7 +7938,7 @@ void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, const ImGuiTabItem* tab, in tab_bar->ReorderRequestOffset = (ImS16)offset; } -void ImGui::TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, const ImGuiTabItem* src_tab, ImVec2 mouse_pos) +void ImGui::TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, ImGuiTabItem* src_tab, ImVec2 mouse_pos) { ImGuiContext& g = *GImGui; IM_ASSERT(tab_bar->ReorderRequestTabId == 0); @@ -7861,7 +7981,7 @@ bool ImGui::TabBarProcessReorder(ImGuiTabBar* tab_bar) return false; //IM_ASSERT(tab_bar->Flags & ImGuiTabBarFlags_Reorderable); // <- this may happen when using debug tools - int tab2_order = tab_bar->GetTabOrder(tab1) + tab_bar->ReorderRequestOffset; + int tab2_order = TabBarGetTabOrder(tab_bar, tab1) + tab_bar->ReorderRequestOffset; if (tab2_order < 0 || tab2_order >= tab_bar->Tabs.Size) return false; @@ -7921,7 +8041,7 @@ static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) if (select_dir != 0) if (ImGuiTabItem* tab_item = TabBarFindTabByID(tab_bar, tab_bar->SelectedTabId)) { - int selected_order = tab_bar->GetTabOrder(tab_item); + int selected_order = TabBarGetTabOrder(tab_bar, tab_item); int target_order = selected_order + select_dir; // Skip tab item buttons until another tab item is found or end is reached @@ -7973,7 +8093,7 @@ static ImGuiTabItem* ImGui::TabBarTabListPopupButton(ImGuiTabBar* tab_bar) if (tab->Flags & ImGuiTabItemFlags_Button) continue; - const char* tab_name = tab_bar->GetTabName(tab); + const char* tab_name = TabBarGetTabName(tab_bar, tab); if (Selectable(tab_name, tab_bar->SelectedTabId == tab->ID)) tab_to_select = tab; } @@ -8137,9 +8257,9 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, { if (tab_appearing && (tab_bar->Flags & ImGuiTabBarFlags_AutoSelectNewTabs) && tab_bar->NextSelectedTabId == 0) if (!tab_bar_appearing || tab_bar->SelectedTabId == 0) - tab_bar->NextSelectedTabId = id; // New tabs gets activated + TabBarQueueFocus(tab_bar, tab); // New tabs gets activated if ((flags & ImGuiTabItemFlags_SetSelected) && (tab_bar->SelectedTabId != id)) // _SetSelected can only be passed on explicit tab bar - tab_bar->NextSelectedTabId = id; + TabBarQueueFocus(tab_bar, tab); } // Lock visibility @@ -8203,7 +8323,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); if (pressed && !is_tab_button) - tab_bar->NextSelectedTabId = id; + TabBarQueueFocus(tab_bar, tab); // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) if (g.ActiveId != id) @@ -8244,9 +8364,8 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); - if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1))) - if (!is_tab_button) - tab_bar->NextSelectedTabId = id; + if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1)) && !is_tab_button) + TabBarQueueFocus(tab_bar, tab); if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) flags |= ImGuiTabItemFlags_NoCloseWithMiddleMouseButton; From aa51f6dd5ff03e2a442c950e9a6ce1a3a7cb9b54 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Mar 2023 17:17:59 +0200 Subject: [PATCH 0808/1018] app_viewer: cleaning log messages --- src/gproshan/app_viewer.cpp | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 305254ab..1ec3e977 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -1,6 +1,5 @@ #include -#include #include #include @@ -169,7 +168,6 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) bool app_viewer::process_convex_hull(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -181,7 +179,6 @@ bool app_viewer::process_convex_hull(viewer * p_view) bool app_viewer::process_connected_components(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -228,7 +225,6 @@ bool app_viewer::process_connected_components(viewer * p_view) bool app_viewer::process_gaussian_curvature(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -287,7 +283,6 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) bool app_viewer::process_edge_collapse(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -305,7 +300,6 @@ bool app_viewer::process_edge_collapse(viewer * p_view) bool app_viewer::process_multiplicate_vertices(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -320,7 +314,6 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) bool app_viewer::process_delete_vertices(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -336,7 +329,6 @@ bool app_viewer::process_delete_vertices(viewer * p_view) bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -469,7 +461,6 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) bool app_viewer::process_voronoi(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -500,7 +491,6 @@ bool app_viewer::process_voronoi(viewer * p_view) bool app_viewer::process_compute_toplesets(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -569,7 +559,6 @@ bool app_viewer::process_msparse_coding(viewer * p_view) bool app_viewer::process_mdict_patch(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -837,25 +826,21 @@ bool app_viewer::process_poisson(viewer * p_view, const index_t & k) bool app_viewer::process_poisson_laplacian_1(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_poisson(p_view, 1); } bool app_viewer::process_poisson_laplacian_2(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_poisson(p_view, 2); } bool app_viewer::process_poisson_laplacian_3(viewer * p_view) { - gproshan_log(APP_VIEWER); return process_poisson(p_view, 3); } bool app_viewer::process_fill_holes(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -933,7 +918,6 @@ bool app_viewer::process_fill_holes(viewer * p_view) bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -986,7 +970,6 @@ bool app_viewer::process_select_multiple(viewer * p_view) bool app_viewer::process_threshold(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -998,7 +981,6 @@ bool app_viewer::process_threshold(viewer * p_view) bool app_viewer::process_noise(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); @@ -1020,7 +1002,6 @@ bool app_viewer::process_noise(viewer * p_view) bool app_viewer::process_black_noise(viewer * p_view) { - gproshan_log(APP_VIEWER); app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); From 2d530a4e40149e111a97d50e675770410d383c07 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Mar 2023 17:56:26 +0200 Subject: [PATCH 0809/1018] points: add sampling 4points function --- include/gproshan/app_viewer.h | 2 ++ include/gproshan/geometry/points.h | 37 ++++++++++++++++++++++++++++ include/gproshan/viewer/che_viewer.h | 2 ++ src/gproshan/app_viewer.cpp | 28 +++++++++++++++++++++ src/gproshan/viewer/che_viewer.cpp | 5 ++++ 5 files changed, 74 insertions(+) create mode 100644 include/gproshan/geometry/points.h diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 5bccab93..e7d8b99e 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -22,6 +22,7 @@ #include +#include #include #include @@ -62,6 +63,7 @@ class app_viewer : public viewer static bool process_simulate_scanner(viewer * p_view); // Geometry + static bool process_sampling_4points(viewer * p_view); static bool process_convex_hull(viewer * p_view); static bool process_connected_components(viewer * p_view); static bool process_gaussian_curvature(viewer * p_view); diff --git a/include/gproshan/geometry/points.h b/include/gproshan/geometry/points.h new file mode 100644 index 00000000..962c9475 --- /dev/null +++ b/include/gproshan/geometry/points.h @@ -0,0 +1,37 @@ +#ifndef POINTS_H +#define POINTS_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +template +std::vector > sampling_4points(const size_t & n, const vec & a, const vec & b, const vec & c, const vec & d) +{ + std::vector > points; + points.reserve((n + 1) * (n + 1)); + + /* + a --- b + | | + d --- c + */ + + const real_t alpha = 1.0 / n; + for(index_t i = 0; i <= n; ++i) + for(index_t j = 0; j <= n; ++j) + points.push_back( j * alpha * (i * alpha * a + (1.0 - i * alpha) * d) + + (1.0 - j * alpha) * (i * alpha * b + (1.0 - i * alpha) * c) + ); + + return points; +} + + +} // namespace gproshan + +#endif // POINTS_H + diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index bf58a1dd..3911ffa2 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -78,6 +78,8 @@ class che_viewer void update_vbo_heatmap(const real_t * vheatmap = nullptr); void update_instances_positions(const std::vector & translations); + const vertex & selected_point(const index_t & i) const; + virtual void draw(shader & program); virtual void draw_pointcloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 1ec3e977..476a6bc3 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -72,6 +72,7 @@ void app_viewer::init() add_process(1002, "", "Scan Scene", process_simulate_scanner); sub_menus.push_back("Geometry"); + add_process(1003, "", "Sampling 4points", process_sampling_4points); add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); add_process(GLFW_KEY_O, "O", "Connected Components", process_connected_components); add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); @@ -165,6 +166,33 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) // Geometry +bool app_viewer::process_sampling_4points(viewer * p_view) +{ + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + static size_t n = 10; + + if(mesh.selected.size() < 4) + { + ImGui::Text("Select 4 points."); + return true; + } + + ImGui::InputScalar("resolution", ImGuiDataType_U64, &n); + + if(ImGui::Button("Run")) + { + std::vector points = sampling_4points(n, mesh.selected_point(0), + mesh.selected_point(1), + mesh.selected_point(2), + mesh.selected_point(3) + ); + view->add_mesh(new che(points.data(), points.size(), nullptr, 0)); + } + + return true; +} bool app_viewer::process_convex_hull(viewer * p_view) { diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index dad29c0f..99d6849b 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -169,6 +169,11 @@ void che_viewer::update_instances_positions(const std::vector & translat glBindVertexArray(0); } +const vertex & che_viewer::selected_point(const index_t & i) const +{ + return mesh->point(selected[i]); +} + void che_viewer::draw(shader & program) { glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); From 23400330c2ec043876b13353e2a5d0b940ebc678 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Mar 2023 18:28:47 +0200 Subject: [PATCH 0810/1018] points: update sampling 4points --- include/gproshan/geometry/points.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/gproshan/geometry/points.h b/include/gproshan/geometry/points.h index 962c9475..2b78bce4 100644 --- a/include/gproshan/geometry/points.h +++ b/include/gproshan/geometry/points.h @@ -20,12 +20,11 @@ std::vector > sampling_4points(const size_t & n, const vec & a, d --- c */ - const real_t alpha = 1.0 / n; + const size_t n2 = n * n; for(index_t i = 0; i <= n; ++i) for(index_t j = 0; j <= n; ++j) - points.push_back( j * alpha * (i * alpha * a + (1.0 - i * alpha) * d) + - (1.0 - j * alpha) * (i * alpha * b + (1.0 - i * alpha) * c) - ); + points.push_back((j * (i * a + (n - i) * d) + + (n - j) * (i * b + (n - i) * c)) / n2); return points; } From e86c939192fc6ef4f6850e544099747d6a9c39b3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Mar 2023 01:19:22 +0200 Subject: [PATCH 0811/1018] che_pcd (io): adding pcd reader and writer current status: only ascii and x y z point clouds --- include/gproshan/app_viewer.h | 1 + include/gproshan/mesh/che_pcd.h | 26 ++++++++ src/gproshan/app_viewer.cpp | 1 + src/gproshan/mesh/che_pcd.cpp | 112 ++++++++++++++++++++++++++++++++ src/gproshan/viewer/viewer.cpp | 5 +- 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 include/gproshan/mesh/che_pcd.h create mode 100644 src/gproshan/mesh/che_pcd.cpp diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index e7d8b99e..47ac5826 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/include/gproshan/mesh/che_pcd.h b/include/gproshan/mesh/che_pcd.h new file mode 100644 index 00000000..1a0c1052 --- /dev/null +++ b/include/gproshan/mesh/che_pcd.h @@ -0,0 +1,26 @@ +#ifndef CHE_PCD_H +#define CHE_PCD_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_pcd : public che +{ + public: + che_pcd(const std::string & file); + + static void write_file(const che * mesh, const std::string & file, const bool & color = false); + + private: + void read_file(const std::string & file); +}; + + +} // namespace gproshan + +#endif // CHE_PCD_H + diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 476a6bc3..73ddf39d 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -41,6 +41,7 @@ che * app_viewer::load_mesh(const std::string & file_path) if(extension == "ptx") return new che_ptx(file_path); if(extension == "xyz") return new che_xyz(file_path); if(extension == "pts") return new che_pts(file_path); + if(extension == "pcd") return new che_pcd(file_path); return new che_img(file_path); } diff --git a/src/gproshan/mesh/che_pcd.cpp b/src/gproshan/mesh/che_pcd.cpp new file mode 100644 index 00000000..7bbafff6 --- /dev/null +++ b/src/gproshan/mesh/che_pcd.cpp @@ -0,0 +1,112 @@ +#include + +#include +#include +#include +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_pcd::che_pcd(const std::string & file) +{ + init(file); +} + +void che_pcd::read_file(const std::string & file) +{ + std::unordered_map bytes = { + {"char", 1}, + {"uchar", 1}, + {"short", 2}, + {"ushort", 2}, + {"int", 4}, + {"uint", 4}, + {"float", 4}, + {"float32", 4}, + {"float64", 8}, + {"double", 8} + }; + + FILE * fp = fopen(file.c_str(), "rb"); + assert(fp); + + size_t n_points = 0; + char line[512], type[32], str[32], format[32], element[32]; + + while(fgets(line, sizeof(line), fp) && str[0] != 'D') // DATA is the end header + { + sscanf(line, "%s", str); + + switch(str[0]) + { + case 'V': // VERSION / VIEWPOINT + break; + case 'F': // FIELDS + break; + case 'S': // SIZE + break; + case 'T': // TYPE + break; + case 'C': // COUNT + break; + case 'W': // WIDTH + break; + case 'H': // HEIGHT + break; + case 'P': // POINTS + sscanf(line, "%*s %lu", &n_points); + break; + case 'D': // DATA + sscanf(line, "%*s %s", format); + break; + } + } + + alloc(n_points, 0); + + + if(format[0] == 'a') // ascii + { + float x, y, z; + for(index_t v = 0; v < n_vertices; ++v) + { + fgets(line, sizeof(line), fp); + sscanf(line, "%f %f %f", &x, &y, &z); + GT[v] = {x, y, z}; + } + } +} + +void che_pcd::write_file(const che * mesh, const std::string & file, const bool & color) +{ + FILE * fp = fopen((file + ".pcd").c_str(), "wb"); + assert(fp); + + fprintf(fp, "# .PCD v.7 - Point Cloud Data file format\n"); + fprintf(fp, "# written by gproshan\n"); + fprintf(fp, "VERSION .7\n"); + fprintf(fp, "FIELDS x y z\n"); + fprintf(fp, "SIZE 4 4 4\n"); + fprintf(fp, "TYPE F F F\n"); + fprintf(fp, "COUNT 1 1 1\n"); + fprintf(fp, "WIDTH %lu\n", mesh->n_vertices); + fprintf(fp, "HEIGHT 1\n"); + fprintf(fp, "VIEWPOINT 0 0 0 1 0 0 0\n"); + fprintf(fp, "POINTS %lu\n", mesh->n_vertices); + fprintf(fp, "DATA ascii\n"); + + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + const vertex & p = mesh->point(v); + fprintf(fp, "%f %f %f\n", p.x(), p.y(), p.z()); + } + + fclose(fp); +} + + +} // namespace gproshan + diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index ce4101f2..c1aaa8d4 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -650,7 +651,7 @@ bool viewer::m_save_mesh(viewer * view) static bool vertex_color = false; ImGui::InputText("file", file, sizeof(file)); - ImGui::Combo("format", &format, ".off\0.obj\0.ply\0.xyz\0.pts\0"); + ImGui::Combo("format", &format, ".off\0.obj\0.ply\0.xyz\0.pts\0.pcd\0"); switch(format) { @@ -684,6 +685,8 @@ bool viewer::m_save_mesh(viewer * view) break; case 4: che_pts::write_file(mesh, file); break; + case 5: che_pcd::write_file(mesh, file); + break; } snprintf(view->status_message, sizeof(view->status_message), "file '%s' saved.", file); From dfacb3cc45d8d5c201bc6e350ba36984242d6807 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Mar 2023 15:59:59 +0200 Subject: [PATCH 0812/1018] rt_embree: fix select vertex in a point cloud --- src/gproshan/raytracing/rt_embree.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index 0d4aff9e..b6066885 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -214,7 +214,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) { - RTCGeometry geom = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT); + RTCGeometry geom = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_DISC_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, @@ -222,20 +222,20 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) 4 * sizeof(float), mesh->n_vertices ); - +/* vertex * normal = (vertex *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), mesh->n_vertices ); - +*/ #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { pxyzr[i] = model_mat * vec4(mesh->point(i), 1); pxyzr[i][3] = pc_radius; - normal[i] = mesh->normal(i); +// normal[i] = mesh->normal(i); } rtcCommitGeometry(geom); From c68048715dfa7a1bded1040b6ebb5cc1323d9ce7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 31 Mar 2023 17:13:43 +0200 Subject: [PATCH 0813/1018] app_viewer: creating point cloud from 4points samples function --- src/gproshan/app_viewer.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 73ddf39d..8309c173 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -173,24 +173,34 @@ bool app_viewer::process_sampling_4points(viewer * p_view) che_viewer & mesh = view->active_mesh(); static size_t n = 10; - - if(mesh.selected.size() < 4) - { - ImGui::Text("Select 4 points."); - return true; - } + static std::vector points; ImGui::InputScalar("resolution", ImGuiDataType_U64, &n); - if(ImGui::Button("Run")) + if(ImGui::Button("Clean")) points.clear(); + + ImGui::SameLine(); + if(mesh.selected.size() > 3) { - std::vector points = sampling_4points(n, mesh.selected_point(0), - mesh.selected_point(1), - mesh.selected_point(2), - mesh.selected_point(3) - ); - view->add_mesh(new che(points.data(), points.size(), nullptr, 0)); + if(ImGui::Button("Add Samples")) + { + std::vector samples = sampling_4points(n, mesh.selected_point(0), + mesh.selected_point(1), + mesh.selected_point(2), + mesh.selected_point(3) + ); + + for(auto & p: samples) + points.push_back(p); + + mesh.selected.clear(); + } } + else ImGui::Text("Select 4 points."); + + ImGui::Text("Total added points: %lu", points.size()); + if(ImGui::Button("Create Point Cloud")) + view->add_mesh(new che(points.data(), points.size(), nullptr, 0)); return true; } From a36edf645f0a63cd850cca6da6f84c31c991ca7f Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Tue, 18 Apr 2023 14:58:10 +0200 Subject: [PATCH 0814/1018] Update build.yml --- .github/workflows/build.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d53afdc9..1743eea2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -49,8 +49,10 @@ jobs: - name: Install Embree working-directory: /opt run: | - wget https://github.com/embree/embree/releases/download/v4.0.1/embree-4.0.1.x86_64.linux.tar.gz - tar xzf embree-4.0.1.x86_64.linux.tar.gz + wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null + echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list + sudo apt update + sudo apt install intel-renderkit - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 8bf6aca14f353a6e6d35cad03b6105ea13c15ed6 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Tue, 18 Apr 2023 15:12:24 +0200 Subject: [PATCH 0815/1018] Update build.yml --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1743eea2..4b2c073a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,6 +53,7 @@ jobs: echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list sudo apt update sudo apt install intel-renderkit + sudo ln -s /opt/intel/oneapi/embree/4.0.1/lib/cmake/embree /opt/intel/oneapi/embree/4.0.1/lib/cmake/embree-4.0.1 - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 0f69bd4cd9cdc4d40cf873aba78f91e3427f23bc Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Tue, 18 Apr 2023 15:14:06 +0200 Subject: [PATCH 0816/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4b2c073a..6872ad51 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -62,7 +62,7 @@ jobs: shell: bash working-directory: ${{runner.workspace}}/build run: | - source /opt/embree-4.0.1.x86_64.linux/embree-vars.sh + source /opt/intel/oneapi/setvars.sh export PATH=${CUDA_BIN}:${PATH} cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=${{matrix.config}} From 914b9b0d3144588483913836e6c80f2978cb6c31 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Tue, 18 Apr 2023 15:51:30 +0200 Subject: [PATCH 0817/1018] Update build.yml --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6872ad51..211e1145 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -70,4 +70,5 @@ jobs: working-directory: ${{runner.workspace}}/build shell: bash run: | + source /opt/intel/oneapi/setvars.sh cmake --build . --config ${{matrix.config}} From b2ac92103401c4db44fde4dcb318aa02b9a8203e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 20 Apr 2023 16:35:59 +0200 Subject: [PATCH 0818/1018] update to OptiX 7.7 --- CMakeLists.txt | 2 +- src/gproshan/raytracing/rt_optix.cpp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f105aeba..db2e1171 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES native) endif() - find_package(OptiX 7.6) + find_package(OptiX 7.7) if(OptiX_INCLUDE) include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/rt_optix.cpp index b3893e73..8d0ffeb3 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/rt_optix.cpp @@ -72,14 +72,14 @@ optix::optix() const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); - optixModuleCreateFromPTX( optix_context, - &optix_module_compile_opt, - &optix_pipeline_compile_opt, - str_ptx_code.c_str(), - str_ptx_code.size(), - nullptr, nullptr, // log message - &optix_module - ); + optixModuleCreate( optix_context, + &optix_module_compile_opt, + &optix_pipeline_compile_opt, + str_ptx_code.c_str(), + str_ptx_code.size(), + nullptr, nullptr, // log message + &optix_module + ); // create programs From 0e734609c18cb53ab6dbb271bb29f3cb7ab7730a Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Thu, 20 Apr 2023 23:57:35 +0200 Subject: [PATCH 0819/1018] Update README.md --- README.md | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/README.md b/README.md index 8818ab7d..a0ecce16 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,8 @@ Intel Embree is a collection of high performance ray tracing kernels that helps ##### Ubuntu (Linux) - # download the key to system keyring - wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ - | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null - - # add signed entry to apt sources and configure the APT client to use Intel repository: + wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list - sudo apt update sudo apt install intel-renderkit From d533547800f20d05f72920a624a06c70616265c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Apr 2023 17:36:32 +0200 Subject: [PATCH 0820/1018] viewer: implementing shadow mapping, adding depth shader --- include/gproshan/viewer/viewer.h | 1 + shaders/fragment_depth.glsl | 9 +++++++++ shaders/vertex_depth.glsl | 12 ++++++++++++ src/gproshan/viewer/che_viewer.cpp | 11 ++--------- src/gproshan/viewer/viewer.cpp | 6 +++++- 5 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 shaders/fragment_depth.glsl create mode 100644 shaders/vertex_depth.glsl diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 9873766b..b6449f63 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -73,6 +73,7 @@ class viewer shader shader_normals; shader shader_gradient; shader shader_pointcloud; + shader shader_depth; scene::material mat; camera cam; diff --git a/shaders/fragment_depth.glsl b/shaders/fragment_depth.glsl new file mode 100644 index 00000000..16d7e223 --- /dev/null +++ b/shaders/fragment_depth.glsl @@ -0,0 +1,9 @@ +#version 410 core + +out vec4 color; + +void main() +{ + color = vec4(1); +} + diff --git a/shaders/vertex_depth.glsl b/shaders/vertex_depth.glsl new file mode 100644 index 00000000..b961b5b9 --- /dev/null +++ b/shaders/vertex_depth.glsl @@ -0,0 +1,12 @@ +#version 410 core + +layout (location=0) in vec3 in_position; + +uniform mat4 proj_view_mat; +uniform mat4 model_mat; + +void main() +{ + gl_Position = proj_view_mat * model_mat * vec4(in_position, 1); +} + diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 99d6849b..d91a148b 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -189,15 +189,8 @@ void che_viewer::draw(shader & program) glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - if(n_instances) glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances); - /*else if(materials.size()) - { - for(auto & m: materials) - { - glDrawElementsBaseVertex(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, 0); - } - }*/ - else glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); + n_instances ? glDrawElementsInstanced(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0, n_instances) + : glDrawElements(GL_TRIANGLES, mesh->n_half_edges, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindVertexArray(0); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index c1aaa8d4..8f166fb0 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -409,6 +409,9 @@ void viewer::init_glsl() shader_pointcloud.load_vertex(shaders_path("vertex_pointcloud.glsl")); shader_pointcloud.load_fragment(shaders_path("fragment_pointcloud.glsl")); + + shader_depth.load_vertex(shaders_path("vertex_depth.glsl")); + shader_depth.load_fragment(shaders_path("fragment_depth.glsl")); } void viewer::add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f) @@ -967,6 +970,7 @@ void viewer::render_gl() glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, true, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_view_mat"), 1, true, &proj_view_mat[0][0]); glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + glProgramUniformMatrix4fv(shader_depth, shader_depth("proj_view_mat"), 1, true, &proj_view_mat[0][0]); glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); @@ -989,7 +993,7 @@ void viewer::render_gl() if(mesh->is_pointcloud() || mesh.render_pointcloud) mesh.draw_pointcloud(shader_pointcloud); else - mesh.draw(shader_triangles); + mesh.draw(shader_depth); if(mesh.render_normals) mesh.draw_pointcloud(shader_normals); From b7efc22c85a879770bbdd799ffe5285d1fe3b074 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 25 Apr 2023 18:15:21 +0200 Subject: [PATCH 0821/1018] viewer: added uniform method to shader class --- include/gproshan/viewer/shader.h | 14 ++++++-- src/gproshan/viewer/che_viewer.cpp | 20 ++++++------ src/gproshan/viewer/scene_viewer.cpp | 46 +++++++++++++------------- src/gproshan/viewer/shader.cpp | 48 +++++++++++++++++++++++----- src/gproshan/viewer/viewer.cpp | 30 ++++++++--------- 5 files changed, 99 insertions(+), 59 deletions(-) diff --git a/include/gproshan/viewer/shader.h b/include/gproshan/viewer/shader.h index 666b90bc..765e3672 100644 --- a/include/gproshan/viewer/shader.h +++ b/include/gproshan/viewer/shader.h @@ -2,9 +2,10 @@ #define SHADER_H #include +#include +#include #include -#include // geometry processing and shape analysis framework @@ -15,13 +16,11 @@ class shader { protected: GLuint program = 0; - std::map uniform; bool linked = false; public: shader() = default; ~shader(); - const GLint & operator () (const std::string & name); operator GLuint () const; void load_vertex(const std::string & filename); void load_fragment(const std::string & filename); @@ -29,6 +28,15 @@ class shader void enable(); void disable() const; + void uniform(const std::string & name, bool value); + void uniform(const std::string & name, int value); + void uniform(const std::string & name, unsigned int value); + void uniform(const std::string & name, float value); + void uniform(const std::string & name, const vec3 & value); + void uniform(const std::string & name, const vec4 & value); + void uniform(const std::string & name, const mat3 & value); + void uniform(const std::string & name, const mat4 & value); + protected: bool load(GLenum shader_type, const std::string & filename); bool read_source(const std::string & filename, std::string & source); diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index d91a148b..a24b13c3 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -176,11 +176,11 @@ const vertex & che_viewer::selected_point(const index_t & i) const void che_viewer::draw(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); - glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); - glProgramUniform1i(program, program("render_flat"), render_flat); - glProgramUniform1i(program, program("render_lines"), render_lines); - glProgramUniform1i(program, program("render_wireframe"), render_triangles); + program.uniform("model_mat", model_mat); + program.uniform("idx_colormap", idx_colormap); + program.uniform("render_lines", render_lines); + program.uniform("render_flat", render_flat); + program.uniform("render_wireframe", render_triangles); glPolygonMode(GL_FRONT_AND_BACK, render_wireframe ? GL_LINE : GL_FILL); @@ -200,11 +200,11 @@ void che_viewer::draw(shader & program) void che_viewer::draw_pointcloud(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); - glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); - glProgramUniform1i(program, program("render_lines"), render_lines); - glProgramUniform1i(program, program("point_normals"), point_normals); - glProgramUniform1ui(program, program("point_size"), point_size); + program.uniform("model_mat", model_mat); + program.uniform("idx_colormap", idx_colormap); + program.uniform("render_lines", render_lines); + program.uniform("point_normals", point_normals); + program.uniform("point_size", point_size); program.enable(); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 662beca1..d70f1ccd 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -44,11 +44,11 @@ void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex void scene_viewer::draw(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); - glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); - glProgramUniform1i(program, program("render_lines"), render_lines); - glProgramUniform1i(program, program("render_flat"), render_flat); - glProgramUniform1i(program, program("render_wireframe"), render_triangles); + program.uniform("model_mat", model_mat); + program.uniform("idx_colormap", idx_colormap); + program.uniform("render_lines", render_lines); + program.uniform("render_flat", render_flat); + program.uniform("render_wireframe", render_triangles); glPolygonMode(GL_FRONT_AND_BACK, render_wireframe ? GL_LINE : GL_FILL); @@ -83,11 +83,11 @@ void scene_viewer::draw(shader & program) void scene_viewer::draw_pointcloud(shader & program) { - glProgramUniformMatrix4fv(program, program("model_mat"), 1, true, &model_mat[0][0]); - glProgramUniform1ui(program, program("idx_colormap"), idx_colormap); - glProgramUniform1i(program, program("render_lines"), render_lines); - glProgramUniform1i(program, program("point_normals"), point_normals); - glProgramUniform1ui(program, program("point_size"), point_size); + program.uniform("model_mat", model_mat); + program.uniform("idx_colormap", idx_colormap); + program.uniform("render_lines", render_lines); + program.uniform("point_normals", point_normals); + program.uniform("point_size", point_size); program.enable(); @@ -120,25 +120,25 @@ void scene_viewer::draw_pointcloud(shader & program) void scene_viewer::gl_uniform_material(shader & program, const scene::material & mat) { - glProgramUniform3f(program, program("mat.Ka"), mat.Ka.x(), mat.Ka.y(), mat.Ka.z()); - glProgramUniform3f(program, program("mat.Kd"), mat.Kd.x(), mat.Kd.y(), mat.Kd.z()); - glProgramUniform3f(program, program("mat.Ks"), mat.Ks.x(), mat.Ks.y(), mat.Ks.z()); - glProgramUniform1f(program, program("mat.d"), mat.d); - glProgramUniform1f(program, program("mat.Ns"), mat.Ns); - glProgramUniform1f(program, program("mat.Ni"), mat.Ni); - glProgramUniform1i(program, program("mat.illum"), mat.illum); - glProgramUniform1i(program, program("mat.map_Ka"), mat.map_Ka); - glProgramUniform1i(program, program("mat.map_Kd"), mat.map_Kd); - glProgramUniform1i(program, program("mat.map_Ks"), mat.map_Ks); - glProgramUniform1i(program, program("mat.map_d"), mat.map_d); - glProgramUniform1i(program, program("mat.map_bump"), mat.map_bump); + program.uniform("mat.Ka", mat.Ka); + program.uniform("mat.Kd", mat.Kd); + program.uniform("mat.Ks", mat.Ks); + program.uniform("mat.d", mat.d); + program.uniform("mat.Ns", mat.Ns); + program.uniform("mat.Ni", mat.Ni); + program.uniform("mat.illum", mat.illum); + program.uniform("mat.map_Ka", mat.map_Ka); + program.uniform("mat.map_Kd", mat.map_Kd); + program.uniform("mat.map_Ks", mat.map_Ks); + program.uniform("mat.map_d", mat.map_d); + program.uniform("mat.map_bump", mat.map_bump); static auto bind_texture = [&](const int & i, const int & map, const char * tex) { if(map < 0) return; glActiveTexture(gltex_nums[i]); glBindTexture(GL_TEXTURE_2D, gltextures[map]); - glProgramUniform1i(program, program(tex), i); + program.uniform(tex, i); }; bind_texture(0, mat.map_Ka, "tex_Ka"); diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index f201857d..e79e9d7e 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -16,14 +16,6 @@ shader::~shader() glDeleteProgram(program); } -const GLint & shader::operator () (const std::string & name) -{ - if(uniform.find(name) != uniform.end()) - uniform[name] = glGetUniformLocation(program, name.c_str()); - - return uniform[name]; -} - shader::operator GLuint() const { return program; @@ -60,6 +52,46 @@ void shader::disable() const glUseProgram(0); } +void shader::uniform(const std::string & name, bool value) +{ + glProgramUniform1i(program, glGetUniformLocation(program, name.c_str()), value); +} + +void shader::uniform(const std::string & name, int value) +{ + glProgramUniform1i(program, glGetUniformLocation(program, name.c_str()), value); +} + +void shader::uniform(const std::string & name, unsigned int value) +{ + glProgramUniform1ui(program, glGetUniformLocation(program, name.c_str()), value); +} + +void shader::uniform(const std::string & name, float value) +{ + glProgramUniform1f(program, glGetUniformLocation(program, name.c_str()), value); +} + +void shader::uniform(const std::string & name, const vec3 & value) +{ + glProgramUniform3fv(program, glGetUniformLocation(program, name.c_str()), 1, &value[0]); +} + +void shader::uniform(const std::string & name, const vec4 & value) +{ + glProgramUniform4fv(program, glGetUniformLocation(program, name.c_str()), 1, &value[0]); +} + +void shader::uniform(const std::string & name, const mat3 & value) +{ + glProgramUniformMatrix3fv(program, glGetUniformLocation(program, name.c_str()), 1, true, &value[0][0]); +} + +void shader::uniform(const std::string & name, const mat4 & value) +{ + glProgramUniformMatrix4fv(program, glGetUniformLocation(program, name.c_str()), 1, true, &value[0][0]); +} + bool shader::load(GLenum shader_type, const std::string & filename) { std::string source; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 8f166fb0..79bf30c6 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -957,25 +957,25 @@ bool viewer::m_raycasting(viewer * view) void viewer::render_gl() { - glProgramUniform3f(shader_sphere, shader_sphere("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_triangles, shader_triangles("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("eye"), cam.eye[0], cam.eye[1], cam.eye[2]); + shader_sphere.uniform("eye", cam.eye.v); + shader_triangles.uniform("eye", cam.eye.v); + shader_pointcloud.uniform("eye", cam.eye.v); - glProgramUniform3f(shader_sphere, shader_sphere("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glProgramUniform3f(shader_triangles, shader_triangles("cam_light"), cam_light[0], cam_light[1], cam_light[2]); - glProgramUniform3f(shader_pointcloud, shader_pointcloud("cam_light"), cam_light[0], cam_light[1], cam_light[2]); + shader_sphere.uniform("cam_light", cam_light); + shader_triangles.uniform("cam_light", cam_light); + shader_pointcloud.uniform("cam_light", cam_light); - glProgramUniformMatrix4fv(shader_sphere, shader_sphere("proj_view_mat"), 1, true, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_triangles, shader_triangles("proj_view_mat"), 1, true, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_pointcloud, shader_pointcloud("proj_view_mat"), 1, true, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_normals, shader_normals("proj_view_mat"), 1, true, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_gradient, shader_gradient("proj_view_mat"), 1, true, &proj_view_mat[0][0]); - glProgramUniformMatrix4fv(shader_depth, shader_depth("proj_view_mat"), 1, true, &proj_view_mat[0][0]); + shader_sphere.uniform("proj_view_mat", proj_view_mat); + shader_triangles.uniform("proj_view_mat", proj_view_mat); + shader_pointcloud.uniform("proj_view_mat", proj_view_mat); + shader_normals.uniform("proj_view_mat", proj_view_mat); + shader_gradient.uniform("proj_view_mat", proj_view_mat); + shader_depth.uniform("proj_view_mat", proj_view_mat); - glProgramUniform1f(shader_normals, shader_normals("length"), cam.zoom() * 0.02); - glProgramUniform1f(shader_gradient, shader_gradient("length"), cam.zoom() * 0.02); + shader_normals.uniform("length", cam.zoom() * 0.02f); + shader_gradient.uniform("length", cam.zoom() * 0.02f); - glProgramUniform1f(shader_sphere, shader_sphere("scale"), cam.zoom()); + shader_sphere.uniform("scale", cam.zoom()); for(index_t i = 0; i < meshes.size(); ++i) From 9a23e9ef3c7ad6fa1a075fdd9ec21e840aecc99c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 May 2023 12:42:11 +0200 Subject: [PATCH 0822/1018] viewer: undo triangles shader line --- src/gproshan/viewer/viewer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 79bf30c6..e82be459 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -993,7 +993,7 @@ void viewer::render_gl() if(mesh->is_pointcloud() || mesh.render_pointcloud) mesh.draw_pointcloud(shader_pointcloud); else - mesh.draw(shader_depth); + mesh.draw(shader_triangles); if(mesh.render_normals) mesh.draw_pointcloud(shader_normals); From 50478ffa62342b1551686a925c58257e8781ea15 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 May 2023 15:18:36 +0200 Subject: [PATCH 0823/1018] che_ply: loading normals --- src/gproshan/mesh/che_ply.cpp | 82 ++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index c8a290cc..f97eadd2 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -18,29 +18,40 @@ che_ply::che_ply(const std::string & file) void che_ply::read_file(const std::string & file) { std::unordered_map bytes = { - {"char", 1}, - {"uchar", 1}, - {"short", 2}, - {"ushort", 2}, - {"int", 4}, - {"uint", 4}, - {"float", 4}, - {"float32", 4}, - {"float64", 8}, - {"double", 8} + {"char" , 1}, + {"uchar" , 1}, + {"short" , 2}, + {"ushort" , 2}, + {"int" , 4}, + {"uint" , 4}, + {"float" , 4}, + {"float32" , 4}, + {"float64" , 8}, + {"double" , 8} }; FILE * fp = fopen(file.c_str(), "rb"); assert(fp); - size_t nv = 0, nf = 0; - size_t nb, xyz = 0, rgb = 0, vbytes = 0; - index_t ixyz = 0, irgb = 0; + size_t nv = 0; + size_t nf = 0; + size_t xyz = 0; + size_t rgb = 0; + size_t normal = 0; + size_t vbytes = 0; + index_t ixyz = 0, irgb = 0, inormal = 0; size_t fn = 0, fbytes = 0; index_t P[32]; char line[512], type[32], str[32], format[32], element[32]; + auto add_vproperty = [&vbytes](size_t & p, index_t & i, const size_t & nb) + { + p = nb; + i = vbytes; + vbytes += nb; + }; + while(fgets(line, sizeof(line), fp) && line[1] != 'n') // end_header { sscanf(line, "%s", str); @@ -60,20 +71,18 @@ void che_ply::read_file(const std::string & file) if(str[0] == 'p' && element[0] == 'v') // property vertex { sscanf(line, "%*s %s %s", type, str); - nb = bytes[type]; - if(str[0] == 'x') + switch(str[0]) { - xyz = nb; - ixyz = vbytes; + case 'x': add_vproperty(xyz, ixyz, bytes[type]); + break; + case 'r': add_vproperty(rgb, irgb, bytes[type]); + break; + case 'n': add_vproperty(normal, inormal, bytes[type]); + break; + default: vbytes += bytes[type]; + break; } - if(str[0] == 'r') - { - rgb = nb; - irgb = vbytes; - } - - vbytes += nb; } if(str[0] == 'p' && element[0] == 'f') // property face @@ -130,12 +139,10 @@ void che_ply::read_file(const std::string & file) fread(buffer, vbytes, n_vertices, fp); if(big_endian) { - char * pb; - - #pragma omp parallel for private(pb) + #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) { - pb = buffer + v * vbytes; + char * pb = buffer + v * vbytes; for(index_t i = 0; i < 3; ++i) big_to_little(pb + ixyz + i * xyz, xyz); @@ -145,6 +152,12 @@ void che_ply::read_file(const std::string & file) for(index_t i = 0; i < 3; ++i) big_to_little(pb + irgb + i * rgb, rgb); } + + if(normal) + { + for(index_t i = 0; i < 3; ++i) + big_to_little(pb + inormal + i * normal, normal); + } } } @@ -171,7 +184,18 @@ void che_ply::read_file(const std::string & file) if(rgb == 1) VC[v][i] = *(unsigned char *) (pb + irgb + i * rgb); else - GT[v][i] = (unsigned char) (*(float *) (pb + irgb + i * rgb)) * 255; + VC[v][i] = (unsigned char) (*(float *) (pb + irgb + i * rgb)) * 255; + } + + if(normal) + { + for(index_t i = 0; i < 3; ++i) + { + if(normal == 4) + VN[v][i] = (real_t) *(float *) (pb + inormal + i * normal); + else + VN[v][i] = (real_t) *(double *) (pb + inormal + i * normal); + } } } } From ed532e2192556509715454dec15973cc5b9b720f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 May 2023 15:29:40 +0200 Subject: [PATCH 0824/1018] che_ply: fix normal loading --- src/gproshan/mesh/che_ply.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index f97eadd2..369fe27d 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -49,7 +49,6 @@ void che_ply::read_file(const std::string & file) { p = nb; i = vbytes; - vbytes += nb; }; while(fgets(line, sizeof(line), fp) && line[1] != 'n') // end_header @@ -71,18 +70,20 @@ void che_ply::read_file(const std::string & file) if(str[0] == 'p' && element[0] == 'v') // property vertex { sscanf(line, "%*s %s %s", type, str); + const size_t & nb = bytes[type]; switch(str[0]) { - case 'x': add_vproperty(xyz, ixyz, bytes[type]); + case 'x': add_vproperty(xyz, ixyz, nb); break; - case 'r': add_vproperty(rgb, irgb, bytes[type]); - break; - case 'n': add_vproperty(normal, inormal, bytes[type]); - break; - default: vbytes += bytes[type]; + case 'r': add_vproperty(rgb, irgb, nb); break; + case 'n': + if(str[1] == 'x') + add_vproperty(normal, inormal, nb); } + + vbytes += nb; } if(str[0] == 'p' && element[0] == 'f') // property face From ec8b54039254527f2623db0af21db89999470dea Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 May 2023 01:09:53 +0200 Subject: [PATCH 0825/1018] rt_embree: render point cloud as unoriented disk fix normal orientation --- include/gproshan/raytracing/rt_embree.h | 2 +- include/gproshan/raytracing/rt_utils.h | 9 ++++++++- src/gproshan/raytracing/rt_embree.cpp | 12 ++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/rt_embree.h index 52edc42a..1fa101fb 100644 --- a/include/gproshan/raytracing/rt_embree.h +++ b/include/gproshan/raytracing/rt_embree.h @@ -26,7 +26,7 @@ class embree : public raytracing vertex org() const; vertex dir() const; - vertex position() const; + vertex pos() const; vertex normal() const; }; diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/rt_utils.h index 6f4fa370..3646096c 100644 --- a/include/gproshan/raytracing/rt_utils.h +++ b/include/gproshan/raytracing/rt_utils.h @@ -69,7 +69,7 @@ struct t_eval_hit vec position; vec normal; vec Ka{0.4f, 0.4f, 0.4f}; - vec Kd; + vec Kd{0.9f, 0.94f, 0.98f}; vec Ks{0.2f, 0.2f, 0.2f}; T Ns = 4; T d = 1; @@ -77,6 +77,13 @@ struct t_eval_hit __host_device__ t_eval_hit() {} + __host_device__ + t_eval_hit(const CHE & pc, const index_t & aprimID) + { + primID = aprimID; + normal = pc.VN[primID]; + } + __host_device__ t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const scene_data & sc) { diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/rt_embree.cpp index b6066885..eb937225 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/rt_embree.cpp @@ -46,7 +46,7 @@ vertex embree::ray_hit::normal() const return normalize(vec3{hit.Ng_x, hit.Ng_y, hit.Ng_z}); } -vertex embree::ray_hit::position() const +vertex embree::ray_hit::pos() const { return org() + ray.tfar * dir(); } @@ -116,7 +116,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, sc); hit.dist = r.ray.tfar; - hit.position = r.position(); + hit.position = r.pos(); return hit; } @@ -251,8 +251,12 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v ray_hit r(org, dir); if(!intersect(r)) return {}; - eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, sc); - hit.position = r.position(); + const CHE * mesh = g_meshes[r.hit.geomID]; + + eval_hit hit; + if(mesh->n_trigs) + hit = {*mesh, r.hit.primID, r.hit.u, r.hit.v, sc}; + hit.position = r.pos(); hit.normal = flat ? r.normal() : hit.normal; return eval_li( hit, lights, n_lights, cam_pos, From ed617f9bdd67825c07725b397d5f04d4481b5823 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 May 2023 21:35:35 +0200 Subject: [PATCH 0826/1018] raytracing: renaming source files --- apps/test_scanner.cpp | 2 +- .../raytracing/{rt_embree.h => embree.h} | 0 .../raytracing/{rt_optix.h => optix.h} | 2 +- .../{rt_optix_params.h => optix_params.h} | 0 include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/raytracing/render_params.h | 7 +- .../raytracing/{rt_utils.h => utils.h} | 0 src/gproshan/CMakeLists.txt | 4 +- .../raytracing/{rt_embree.cpp => embree.cpp} | 2 +- .../raytracing/{rt_optix.cpp => optix.cpp} | 2 +- .../raytracing/{rt_optix.cu => optix.cu} | 4 +- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 4 +- src/optix.ptx | 955 ++++++++++++++++++ 14 files changed, 970 insertions(+), 16 deletions(-) rename include/gproshan/raytracing/{rt_embree.h => embree.h} (100%) rename include/gproshan/raytracing/{rt_optix.h => optix.h} (97%) rename include/gproshan/raytracing/{rt_optix_params.h => optix_params.h} (100%) rename include/gproshan/raytracing/{rt_utils.h => utils.h} (100%) rename src/gproshan/raytracing/{rt_embree.cpp => embree.cpp} (99%) rename src/gproshan/raytracing/{rt_optix.cpp => optix.cpp} (99%) rename src/gproshan/raytracing/{rt_optix.cu => optix.cu} (97%) create mode 100644 src/optix.ptx diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index 57afbbd9..ebd1ac66 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include diff --git a/include/gproshan/raytracing/rt_embree.h b/include/gproshan/raytracing/embree.h similarity index 100% rename from include/gproshan/raytracing/rt_embree.h rename to include/gproshan/raytracing/embree.h diff --git a/include/gproshan/raytracing/rt_optix.h b/include/gproshan/raytracing/optix.h similarity index 97% rename from include/gproshan/raytracing/rt_optix.h rename to include/gproshan/raytracing/optix.h index 390f289a..70fb32c5 100644 --- a/include/gproshan/raytracing/rt_optix.h +++ b/include/gproshan/raytracing/optix.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include #ifdef GPROSHAN_OPTIX diff --git a/include/gproshan/raytracing/rt_optix_params.h b/include/gproshan/raytracing/optix_params.h similarity index 100% rename from include/gproshan/raytracing/rt_optix_params.h rename to include/gproshan/raytracing/optix_params.h diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index b9557ab1..73273826 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -3,7 +3,7 @@ #include #include -#include +#include #include diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index f3c30b8b..2edba67e 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -1,6 +1,5 @@ -#ifndef RENDER_PARAMS_H -#define RENDER_PARAMS_H - +#ifndef RT_RENDER_PARAMS_H +#define RT_RENDER_PARAMS_H #include #include @@ -53,5 +52,5 @@ struct render_params } // namespace gproshan -#endif // RENDER_PARAMS_H +#endif // RT_RENDER_PARAMS_H diff --git a/include/gproshan/raytracing/rt_utils.h b/include/gproshan/raytracing/utils.h similarity index 100% rename from include/gproshan/raytracing/rt_utils.h rename to include/gproshan/raytracing/utils.h diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 9f85e358..a1136e33 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -18,7 +18,7 @@ target_link_libraries(gproshan imgui) if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cu) - list(REMOVE_ITEM cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/raytracing/rt_optix.cu) + list(REMOVE_ITEM cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/raytracing/optix.cu) add_library(gproshan_cuda SHARED ${cu_sources}) @@ -33,7 +33,7 @@ endif(CUDAToolkit_FOUND) if(OptiX_INCLUDE) - add_library(optix_ptx OBJECT ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/rt_optix.cu) + add_library(optix_ptx OBJECT ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/optix.cu) set_property(TARGET optix_ptx PROPERTY CUDA_PTX_COMPILATION ON) add_custom_target( copy_optix_ptx ALL DEPENDS optix_ptx diff --git a/src/gproshan/raytracing/rt_embree.cpp b/src/gproshan/raytracing/embree.cpp similarity index 99% rename from src/gproshan/raytracing/rt_embree.cpp rename to src/gproshan/raytracing/embree.cpp index eb937225..da2d2dda 100644 --- a/src/gproshan/raytracing/rt_embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -1,4 +1,4 @@ -#include +#include #include diff --git a/src/gproshan/raytracing/rt_optix.cpp b/src/gproshan/raytracing/optix.cpp similarity index 99% rename from src/gproshan/raytracing/rt_optix.cpp rename to src/gproshan/raytracing/optix.cpp index 8d0ffeb3..df1cf88b 100644 --- a/src/gproshan/raytracing/rt_optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef GPROSHAN_OPTIX diff --git a/src/gproshan/raytracing/rt_optix.cu b/src/gproshan/raytracing/optix.cu similarity index 97% rename from src/gproshan/raytracing/rt_optix.cu rename to src/gproshan/raytracing/optix.cu index 6837ff5e..2e5cf672 100644 --- a/src/gproshan/raytracing/rt_optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -1,6 +1,6 @@ #include -#include -#include +#include +#include #include #include diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index a24b13c3..20dfed19 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include // geometry processing and shape analysis framework diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e82be459..d99da2c7 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -16,10 +16,10 @@ #include #include -#include +#include #ifdef GPROSHAN_OPTIX - #include + #include #endif // GPROSHAN_OPTIX #include diff --git a/src/optix.ptx b/src/optix.ptx new file mode 100644 index 00000000..1c56da64 --- /dev/null +++ b/src/optix.ptx @@ -0,0 +1,955 @@ +// +// Generated by NVIDIA NVVM Compiler +// +// Compiler Build ID: CL-32688072 +// Cuda compilation tools, release 12.1, V12.1.105 +// Based on NVVM 7.0.1 +// + +.version 8.1 +.target sm_61 +.address_size 64 + + // .globl __closesthit__shadow +.extern .const .align 8 .b8 optix_params[352]; + +.visible .entry __closesthit__shadow() +{ + + + + ret; + +} + // .globl __closesthit__radiance +.visible .entry __closesthit__radiance() +{ + .reg .pred %p<43>; + .reg .b16 %rs<77>; + .reg .f32 %f<439>; + .reg .b32 %r<136>; + .reg .b64 %rd<165>; + + + // begin inline asm + call (%rd50), _optix_get_sbt_data_ptr_64, (); + // end inline asm + ld.u64 %rd53, [%rd50]; + // begin inline asm + call (%r9), _optix_read_primitive_idx, (); + // end inline asm + // begin inline asm + call (%f105, %f106), _optix_get_triangle_barycentrics, (); + // end inline asm + // begin inline asm + call (%rd51), _optix_get_gas_traversable_handle, (); + // end inline asm + // begin inline asm + call (%r10), _optix_read_sbt_gas_idx, (); + // end inline asm + // begin inline asm + call (%f107), _optix_get_ray_time, (); + // end inline asm + // begin inline asm + call (%f108, %f109, %f110, %f111, %f112, %f113, %f114, %f115, %f116), _optix_get_triangle_vertex_data, (%rd51, %r9, %r10, %f107); + // end inline asm + mul.lo.s32 %r13, %r9, 3; + ld.u64 %rd54, [%rd53+48]; + mul.wide.u32 %rd55, %r13, 4; + add.s64 %rd56, %rd54, %rd55; + add.s32 %r14, %r13, 1; + mul.wide.u32 %rd57, %r14, 4; + add.s64 %rd58, %rd54, %rd57; + add.s32 %r15, %r13, 2; + mul.wide.u32 %rd59, %r15, 4; + add.s64 %rd60, %rd54, %rd59; + ld.u32 %r16, [%rd56]; + cvt.u64.u32 %rd1, %r16; + ld.u64 %rd61, [%rd53+40]; + mul.wide.u32 %rd62, %r16, 3; + add.s64 %rd63, %rd61, %rd62; + ld.u8 %rs31, [%rd63]; + cvt.rn.f32.u16 %f125, %rs31; + ld.u8 %rs32, [%rd63+1]; + cvt.rn.f32.u16 %f126, %rs32; + ld.u8 %rs33, [%rd63+2]; + cvt.rn.f32.u16 %f127, %rs33; + ld.u32 %r17, [%rd58]; + cvt.u64.u32 %rd2, %r17; + mul.wide.u32 %rd64, %r17, 3; + add.s64 %rd65, %rd61, %rd64; + ld.u8 %rs34, [%rd65]; + cvt.rn.f32.u16 %f128, %rs34; + ld.u8 %rs35, [%rd65+1]; + cvt.rn.f32.u16 %f129, %rs35; + ld.u8 %rs36, [%rd65+2]; + cvt.rn.f32.u16 %f130, %rs36; + ld.u32 %r18, [%rd60]; + cvt.u64.u32 %rd3, %r18; + mul.wide.u32 %rd66, %r18, 3; + add.s64 %rd67, %rd61, %rd66; + ld.u8 %rs37, [%rd67]; + cvt.rn.f32.u16 %f131, %rs37; + ld.u8 %rs38, [%rd67+1]; + cvt.rn.f32.u16 %f132, %rs38; + ld.u8 %rs39, [%rd67+2]; + cvt.rn.f32.u16 %f133, %rs39; + mov.f32 %f134, 0f3F800000; + sub.f32 %f135, %f134, %f105; + sub.f32 %f12, %f135, %f106; + mul.f32 %f136, %f105, %f128; + mul.f32 %f137, %f105, %f129; + mul.f32 %f138, %f105, %f130; + fma.rn.f32 %f139, %f12, %f125, %f136; + fma.rn.f32 %f140, %f12, %f126, %f137; + fma.rn.f32 %f141, %f12, %f127, %f138; + fma.rn.f32 %f142, %f106, %f131, %f139; + fma.rn.f32 %f143, %f106, %f132, %f140; + fma.rn.f32 %f144, %f106, %f133, %f141; + div.rn.f32 %f425, %f142, 0f437F0000; + div.rn.f32 %f424, %f143, 0f437F0000; + div.rn.f32 %f423, %f144, 0f437F0000; + ld.u64 %rd68, [%rd53+32]; + mul.wide.u32 %rd69, %r16, 12; + add.s64 %rd70, %rd68, %rd69; + ld.f32 %f145, [%rd70]; + ld.f32 %f146, [%rd70+4]; + ld.f32 %f147, [%rd70+8]; + mul.wide.u32 %rd71, %r17, 12; + add.s64 %rd72, %rd68, %rd71; + ld.f32 %f148, [%rd72]; + mul.f32 %f149, %f148, %f105; + ld.f32 %f150, [%rd72+4]; + mul.f32 %f151, %f150, %f105; + ld.f32 %f152, [%rd72+8]; + mul.f32 %f153, %f152, %f105; + fma.rn.f32 %f154, %f12, %f145, %f149; + fma.rn.f32 %f155, %f12, %f146, %f151; + fma.rn.f32 %f156, %f12, %f147, %f153; + mul.wide.u32 %rd73, %r18, 12; + add.s64 %rd74, %rd68, %rd73; + ld.f32 %f157, [%rd74]; + ld.f32 %f158, [%rd74+4]; + ld.f32 %f159, [%rd74+8]; + fma.rn.f32 %f429, %f157, %f106, %f154; + fma.rn.f32 %f430, %f158, %f106, %f155; + fma.rn.f32 %f431, %f159, %f106, %f156; + ld.const.u64 %rd4, [optix_params+336]; + setp.eq.s64 %p1, %rd4, 0; + mov.f32 %f419, 0f40800000; + mov.f32 %f420, 0f3E4CCCCD; + mov.f32 %f421, 0f3E4CCCCD; + mov.f32 %f422, 0f3E4CCCCD; + mov.f32 %f426, 0f3ECCCCCD; + mov.f32 %f427, 0f3ECCCCCD; + mov.f32 %f428, 0f3ECCCCCD; + @%p1 bra $L__BB1_41; + + cvta.to.global.u64 %rd75, %rd4; + mul.wide.u32 %rd76, %r9, 4; + add.s64 %rd77, %rd75, %rd76; + ld.global.u32 %r2, [%rd77]; + setp.eq.s32 %p2, %r2, -1; + @%p2 bra $L__BB1_41; + + ld.const.u64 %rd5, [optix_params+320]; + ld.const.u64 %rd6, [optix_params+344]; + setp.eq.s64 %p3, %rd6, 0; + mov.f32 %f408, 0f00000000; + mov.f32 %f409, %f408; + @%p3 bra $L__BB1_4; + + cvta.to.global.u64 %rd78, %rd6; + shl.b64 %rd79, %rd1, 3; + add.s64 %rd80, %rd78, %rd79; + ld.global.f32 %f169, [%rd80]; + ld.global.f32 %f170, [%rd80+4]; + shl.b64 %rd81, %rd2, 3; + add.s64 %rd82, %rd78, %rd81; + ld.global.f32 %f171, [%rd82]; + mul.f32 %f172, %f171, %f105; + ld.global.f32 %f173, [%rd82+4]; + mul.f32 %f174, %f173, %f105; + fma.rn.f32 %f175, %f12, %f169, %f172; + fma.rn.f32 %f176, %f12, %f170, %f174; + shl.b64 %rd83, %rd3, 3; + add.s64 %rd84, %rd78, %rd83; + ld.global.f32 %f177, [%rd84]; + ld.global.f32 %f178, [%rd84+4]; + fma.rn.f32 %f408, %f177, %f106, %f175; + fma.rn.f32 %f409, %f178, %f106, %f176; + +$L__BB1_4: + cvta.to.global.u64 %rd85, %rd5; + mul.wide.u32 %rd86, %r2, 72; + add.s64 %rd7, %rd85, %rd86; + ld.global.f32 %f428, [%rd7]; + ld.global.f32 %f427, [%rd7+4]; + ld.global.f32 %f426, [%rd7+8]; + ld.global.u32 %r3, [%rd7+52]; + setp.eq.s32 %p4, %r3, -1; + @%p4 bra $L__BB1_16; + + ld.const.u64 %rd87, [optix_params+328]; + cvta.to.global.u64 %rd88, %rd87; + mul.wide.s32 %rd89, %r3, 32; + add.s64 %rd90, %rd88, %rd89; + add.s64 %rd8, %rd90, 8; + ld.global.u64 %rd9, [%rd90+8]; + add.s64 %rd91, %rd9, -1; + cvt.rn.f32.u64 %f179, %rd91; + mul.f32 %f180, %f408, %f179; + cvt.rzi.s32.f32 %r19, %f180; + cvt.s64.s32 %rd92, %r19; + add.s64 %rd10, %rd9, %rd92; + or.b64 %rd93, %rd10, %rd9; + and.b64 %rd94, %rd93, -4294967296; + setp.eq.s64 %p5, %rd94, 0; + @%p5 bra $L__BB1_7; + + rem.u64 %rd158, %rd10, %rd9; + bra.uni $L__BB1_8; + +$L__BB1_7: + cvt.u32.u64 %r20, %rd9; + cvt.u32.u64 %r21, %rd10; + rem.u32 %r22, %r21, %r20; + cvt.u64.u32 %rd158, %r22; + +$L__BB1_8: + ld.global.u64 %rd14, [%rd8+8]; + add.s64 %rd95, %rd14, -1; + cvt.rn.f32.u64 %f181, %rd95; + mul.f32 %f182, %f409, %f181; + cvt.rzi.s32.f32 %r23, %f182; + cvt.s64.s32 %rd96, %r23; + add.s64 %rd15, %rd14, %rd96; + or.b64 %rd97, %rd15, %rd14; + and.b64 %rd98, %rd97, -4294967296; + setp.eq.s64 %p6, %rd98, 0; + @%p6 bra $L__BB1_10; + + rem.u64 %rd159, %rd15, %rd14; + bra.uni $L__BB1_11; + +$L__BB1_10: + cvt.u32.u64 %r24, %rd14; + cvt.u32.u64 %r25, %rd15; + rem.u32 %r26, %r25, %r24; + cvt.u64.u32 %rd159, %r26; + +$L__BB1_11: + cvt.s64.s32 %rd99, %rd159; + mul.lo.s64 %rd100, %rd99, %rd9; + and.b64 %rd101, %rd158, 4294967295; + add.s64 %rd19, %rd100, %rd101; + ld.global.u64 %rd20, [%rd8+16]; + setp.ne.s64 %p7, %rd20, 3; + mov.u16 %rs64, 250; + mov.u16 %rs63, 240; + mov.u16 %rs62, 230; + @%p7 bra $L__BB1_13; + + cvt.s64.s32 %rd102, %rd19; + ld.global.u64 %rd103, [%rd8+-8]; + mul.lo.s64 %rd104, %rd102, 3; + add.s64 %rd105, %rd103, %rd104; + ld.u8 %rs62, [%rd105]; + ld.u8 %rs63, [%rd105+1]; + ld.u8 %rs64, [%rd105+2]; + +$L__BB1_13: + setp.ne.s64 %p8, %rd20, 1; + @%p8 bra $L__BB1_15; + + cvt.s64.s32 %rd106, %rd19; + ld.global.u64 %rd107, [%rd8+-8]; + add.s64 %rd108, %rd107, %rd106; + ld.u8 %rs62, [%rd108]; + mov.u16 %rs63, %rs62; + mov.u16 %rs64, %rs62; + +$L__BB1_15: + and.b16 %rs43, %rs62, 255; + cvt.rn.f32.u16 %f183, %rs43; + div.rn.f32 %f184, %f183, 0f437F0000; + and.b16 %rs44, %rs63, 255; + cvt.rn.f32.u16 %f185, %rs44; + div.rn.f32 %f186, %f185, 0f437F0000; + and.b16 %rs45, %rs64, 255; + cvt.rn.f32.u16 %f187, %rs45; + div.rn.f32 %f188, %f187, 0f437F0000; + mul.f32 %f428, %f184, %f428; + mul.f32 %f427, %f186, %f427; + mul.f32 %f426, %f188, %f426; + +$L__BB1_16: + ld.global.f32 %f425, [%rd7+12]; + ld.global.f32 %f424, [%rd7+16]; + ld.global.f32 %f423, [%rd7+20]; + ld.global.u32 %r4, [%rd7+56]; + setp.eq.s32 %p9, %r4, -1; + @%p9 bra $L__BB1_28; + + ld.const.u64 %rd109, [optix_params+328]; + cvta.to.global.u64 %rd110, %rd109; + mul.wide.s32 %rd111, %r4, 32; + add.s64 %rd112, %rd110, %rd111; + add.s64 %rd21, %rd112, 8; + ld.global.u64 %rd22, [%rd112+8]; + add.s64 %rd113, %rd22, -1; + cvt.rn.f32.u64 %f189, %rd113; + mul.f32 %f190, %f408, %f189; + cvt.rzi.s32.f32 %r27, %f190; + cvt.s64.s32 %rd114, %r27; + add.s64 %rd23, %rd22, %rd114; + or.b64 %rd115, %rd23, %rd22; + and.b64 %rd116, %rd115, -4294967296; + setp.eq.s64 %p10, %rd116, 0; + @%p10 bra $L__BB1_19; + + rem.u64 %rd160, %rd23, %rd22; + bra.uni $L__BB1_20; + +$L__BB1_19: + cvt.u32.u64 %r28, %rd22; + cvt.u32.u64 %r29, %rd23; + rem.u32 %r30, %r29, %r28; + cvt.u64.u32 %rd160, %r30; + +$L__BB1_20: + ld.global.u64 %rd27, [%rd21+8]; + add.s64 %rd117, %rd27, -1; + cvt.rn.f32.u64 %f191, %rd117; + mul.f32 %f192, %f409, %f191; + cvt.rzi.s32.f32 %r31, %f192; + cvt.s64.s32 %rd118, %r31; + add.s64 %rd28, %rd27, %rd118; + or.b64 %rd119, %rd28, %rd27; + and.b64 %rd120, %rd119, -4294967296; + setp.eq.s64 %p11, %rd120, 0; + @%p11 bra $L__BB1_22; + + rem.u64 %rd161, %rd28, %rd27; + bra.uni $L__BB1_23; + +$L__BB1_22: + cvt.u32.u64 %r32, %rd27; + cvt.u32.u64 %r33, %rd28; + rem.u32 %r34, %r33, %r32; + cvt.u64.u32 %rd161, %r34; + +$L__BB1_23: + cvt.s64.s32 %rd121, %rd161; + mul.lo.s64 %rd122, %rd121, %rd22; + and.b64 %rd123, %rd160, 4294967295; + add.s64 %rd32, %rd122, %rd123; + ld.global.u64 %rd33, [%rd21+16]; + setp.ne.s64 %p12, %rd33, 3; + mov.u16 %rs70, 250; + mov.u16 %rs69, 240; + mov.u16 %rs68, 230; + @%p12 bra $L__BB1_25; + + cvt.s64.s32 %rd124, %rd32; + ld.global.u64 %rd125, [%rd21+-8]; + mul.lo.s64 %rd126, %rd124, 3; + add.s64 %rd127, %rd125, %rd126; + ld.u8 %rs68, [%rd127]; + ld.u8 %rs69, [%rd127+1]; + ld.u8 %rs70, [%rd127+2]; + +$L__BB1_25: + setp.ne.s64 %p13, %rd33, 1; + @%p13 bra $L__BB1_27; + + cvt.s64.s32 %rd128, %rd32; + ld.global.u64 %rd129, [%rd21+-8]; + add.s64 %rd130, %rd129, %rd128; + ld.u8 %rs68, [%rd130]; + mov.u16 %rs69, %rs68; + mov.u16 %rs70, %rs68; + +$L__BB1_27: + and.b16 %rs49, %rs68, 255; + cvt.rn.f32.u16 %f193, %rs49; + div.rn.f32 %f194, %f193, 0f437F0000; + and.b16 %rs50, %rs69, 255; + cvt.rn.f32.u16 %f195, %rs50; + div.rn.f32 %f196, %f195, 0f437F0000; + and.b16 %rs51, %rs70, 255; + cvt.rn.f32.u16 %f197, %rs51; + div.rn.f32 %f198, %f197, 0f437F0000; + mul.f32 %f425, %f194, %f425; + mul.f32 %f424, %f196, %f424; + mul.f32 %f423, %f198, %f423; + +$L__BB1_28: + ld.global.f32 %f422, [%rd7+24]; + ld.global.f32 %f421, [%rd7+28]; + ld.global.f32 %f420, [%rd7+32]; + ld.global.u32 %r5, [%rd7+60]; + setp.eq.s32 %p14, %r5, -1; + @%p14 bra $L__BB1_40; + + ld.const.u64 %rd131, [optix_params+328]; + cvta.to.global.u64 %rd132, %rd131; + mul.wide.s32 %rd133, %r5, 32; + add.s64 %rd134, %rd132, %rd133; + add.s64 %rd34, %rd134, 8; + ld.global.u64 %rd35, [%rd134+8]; + add.s64 %rd135, %rd35, -1; + cvt.rn.f32.u64 %f199, %rd135; + mul.f32 %f200, %f408, %f199; + cvt.rzi.s32.f32 %r35, %f200; + cvt.s64.s32 %rd136, %r35; + add.s64 %rd36, %rd35, %rd136; + or.b64 %rd137, %rd36, %rd35; + and.b64 %rd138, %rd137, -4294967296; + setp.eq.s64 %p15, %rd138, 0; + @%p15 bra $L__BB1_31; + + rem.u64 %rd162, %rd36, %rd35; + bra.uni $L__BB1_32; + +$L__BB1_31: + cvt.u32.u64 %r36, %rd35; + cvt.u32.u64 %r37, %rd36; + rem.u32 %r38, %r37, %r36; + cvt.u64.u32 %rd162, %r38; + +$L__BB1_32: + ld.global.u64 %rd40, [%rd34+8]; + add.s64 %rd139, %rd40, -1; + cvt.rn.f32.u64 %f201, %rd139; + mul.f32 %f202, %f409, %f201; + cvt.rzi.s32.f32 %r39, %f202; + cvt.s64.s32 %rd140, %r39; + add.s64 %rd41, %rd40, %rd140; + or.b64 %rd141, %rd41, %rd40; + and.b64 %rd142, %rd141, -4294967296; + setp.eq.s64 %p16, %rd142, 0; + @%p16 bra $L__BB1_34; + + rem.u64 %rd163, %rd41, %rd40; + bra.uni $L__BB1_35; + +$L__BB1_34: + cvt.u32.u64 %r40, %rd40; + cvt.u32.u64 %r41, %rd41; + rem.u32 %r42, %r41, %r40; + cvt.u64.u32 %rd163, %r42; + +$L__BB1_35: + cvt.s64.s32 %rd143, %rd163; + mul.lo.s64 %rd144, %rd143, %rd35; + and.b64 %rd145, %rd162, 4294967295; + add.s64 %rd45, %rd144, %rd145; + ld.global.u64 %rd46, [%rd34+16]; + setp.ne.s64 %p17, %rd46, 3; + mov.u16 %rs76, 250; + mov.u16 %rs75, 240; + mov.u16 %rs74, 230; + @%p17 bra $L__BB1_37; + + cvt.s64.s32 %rd146, %rd45; + ld.global.u64 %rd147, [%rd34+-8]; + mul.lo.s64 %rd148, %rd146, 3; + add.s64 %rd149, %rd147, %rd148; + ld.u8 %rs74, [%rd149]; + ld.u8 %rs75, [%rd149+1]; + ld.u8 %rs76, [%rd149+2]; + +$L__BB1_37: + setp.ne.s64 %p18, %rd46, 1; + @%p18 bra $L__BB1_39; + + cvt.s64.s32 %rd150, %rd45; + ld.global.u64 %rd151, [%rd34+-8]; + add.s64 %rd152, %rd151, %rd150; + ld.u8 %rs74, [%rd152]; + mov.u16 %rs75, %rs74; + mov.u16 %rs76, %rs74; + +$L__BB1_39: + and.b16 %rs55, %rs74, 255; + cvt.rn.f32.u16 %f203, %rs55; + div.rn.f32 %f204, %f203, 0f437F0000; + and.b16 %rs56, %rs75, 255; + cvt.rn.f32.u16 %f205, %rs56; + div.rn.f32 %f206, %f205, 0f437F0000; + and.b16 %rs57, %rs76, 255; + cvt.rn.f32.u16 %f207, %rs57; + div.rn.f32 %f208, %f207, 0f437F0000; + mul.f32 %f422, %f204, %f422; + mul.f32 %f421, %f206, %f421; + mul.f32 %f420, %f208, %f420; + +$L__BB1_40: + ld.global.f32 %f419, [%rd7+40]; + +$L__BB1_41: + ld.const.u8 %rs58, [optix_params+304]; + setp.eq.s16 %p19, %rs58, 0; + @%p19 bra $L__BB1_43; + + sub.f32 %f209, %f111, %f108; + sub.f32 %f210, %f116, %f110; + sub.f32 %f211, %f112, %f109; + mul.f32 %f212, %f211, %f210; + sub.f32 %f213, %f115, %f109; + sub.f32 %f214, %f113, %f110; + mul.f32 %f215, %f214, %f213; + sub.f32 %f216, %f212, %f215; + sub.f32 %f217, %f114, %f108; + mul.f32 %f218, %f214, %f217; + mul.f32 %f219, %f209, %f210; + sub.f32 %f220, %f218, %f219; + mul.f32 %f221, %f209, %f213; + mul.f32 %f222, %f211, %f217; + sub.f32 %f223, %f221, %f222; + fma.rn.f32 %f224, %f216, %f216, 0f00000000; + fma.rn.f32 %f225, %f220, %f220, %f224; + fma.rn.f32 %f226, %f223, %f223, %f225; + sqrt.rn.f32 %f227, %f226; + div.rn.f32 %f431, %f223, %f227; + div.rn.f32 %f430, %f220, %f227; + div.rn.f32 %f429, %f216, %f227; + +$L__BB1_43: + mul.f32 %f231, %f111, %f105; + fma.rn.f32 %f232, %f12, %f108, %f231; + mul.f32 %f233, %f112, %f105; + fma.rn.f32 %f234, %f12, %f109, %f233; + mul.f32 %f235, %f113, %f105; + fma.rn.f32 %f236, %f12, %f110, %f235; + fma.rn.f32 %f73, %f114, %f106, %f232; + fma.rn.f32 %f74, %f115, %f106, %f234; + fma.rn.f32 %f75, %f116, %f106, %f236; + ld.const.f32 %f237, [optix_params+228]; + sub.f32 %f238, %f237, %f73; + ld.const.v2.f32 {%f239, %f240}, [optix_params+232]; + sub.f32 %f243, %f239, %f74; + sub.f32 %f244, %f240, %f75; + fma.rn.f32 %f245, %f238, %f238, 0f00000000; + mov.f32 %f436, 0f00000000; + fma.rn.f32 %f246, %f243, %f243, %f245; + fma.rn.f32 %f247, %f244, %f244, %f246; + sqrt.rn.f32 %f248, %f247; + div.rn.f32 %f76, %f238, %f248; + div.rn.f32 %f77, %f243, %f248; + div.rn.f32 %f78, %f244, %f248; + ld.const.u32 %r6, [optix_params+32]; + setp.lt.s32 %p20, %r6, 1; + mov.f32 %f437, %f436; + mov.f32 %f438, %f436; + @%p20 bra $L__BB1_55; + + ld.const.u64 %rd47, [optix_params+312]; + mov.u64 %rd164, optix_params; + mul.f32 %f252, %f419, 0f3F000000; + cvt.rzi.f32.f32 %f253, %f252; + add.f32 %f254, %f253, %f253; + sub.f32 %f255, %f419, %f254; + abs.f32 %f79, %f255; + mul.f32 %f80, %f428, 0f3E4CCCCD; + mul.f32 %f81, %f427, 0f3E4CCCCD; + mul.f32 %f82, %f426, 0f3E4CCCCD; + mov.u32 %r135, 0; + bra.uni $L__BB1_45; + +$L__BB1_48: + abs.f32 %f406, %f91; + setp.eq.f32 %p30, %f91, 0f00000000; + setp.eq.f32 %p31, %f406, 0f7F800000; + or.pred %p32, %p30, %p31; + @%p32 bra $L__BB1_52; + bra.uni $L__BB1_49; + +$L__BB1_52: + setp.lt.f32 %p39, %f419, 0f00000000; + add.f32 %f352, %f91, %f91; + mov.b32 %r53, %f352; + xor.b32 %r54, %r53, 2139095040; + selp.b32 %r55, %r54, %r53, %p39; + and.b32 %r56, %r55, 2147483647; + setp.eq.f32 %p40, %f79, 0f3F800000; + selp.b32 %r57, %r55, %r56, %p40; + mov.b32 %f435, %r57; + bra.uni $L__BB1_54; + +$L__BB1_49: + mov.f32 %f435, 0f3F800000; + setp.eq.f32 %p33, %f91, 0fBF800000; + setp.eq.f32 %p34, %f94, 0f7F800000; + and.pred %p35, %p33, %p34; + @%p35 bra $L__BB1_54; + + setp.geu.f32 %p36, %f91, 0f00000000; + mov.f32 %f435, %f93; + @%p36 bra $L__BB1_54; + + setp.eq.f32 %p37, %f79, 0f3F800000; + neg.f32 %f349, %f93; + selp.f32 %f350, %f349, %f93, %p37; + cvt.rmi.f32.f32 %f351, %f419; + setp.neu.f32 %p38, %f351, %f419; + selp.f32 %f435, 0f7FFFFFFF, %f350, %p38; + bra.uni $L__BB1_54; + +$L__BB1_45: + ld.const.f32 %f257, [%rd164+36]; + sub.f32 %f258, %f257, %f73; + ld.const.f32 %f259, [%rd164+40]; + sub.f32 %f260, %f259, %f74; + ld.const.f32 %f261, [%rd164+44]; + sub.f32 %f262, %f261, %f75; + fma.rn.f32 %f263, %f258, %f258, 0f00000000; + mov.f32 %f264, 0f00000000; + fma.rn.f32 %f265, %f260, %f260, %f263; + fma.rn.f32 %f266, %f262, %f262, %f265; + sqrt.rn.f32 %f86, %f266; + div.rn.f32 %f87, %f258, %f86; + div.rn.f32 %f88, %f260, %f86; + div.rn.f32 %f89, %f262, %f86; + add.f32 %f267, %f87, %f76; + add.f32 %f268, %f88, %f77; + add.f32 %f269, %f89, %f78; + fma.rn.f32 %f270, %f267, %f267, 0f00000000; + fma.rn.f32 %f271, %f268, %f268, %f270; + fma.rn.f32 %f272, %f269, %f269, %f271; + sqrt.rn.f32 %f273, %f272; + div.rn.f32 %f274, %f267, %f273; + div.rn.f32 %f275, %f268, %f273; + div.rn.f32 %f276, %f269, %f273; + fma.rn.f32 %f277, %f87, %f429, 0f00000000; + fma.rn.f32 %f278, %f88, %f430, %f277; + fma.rn.f32 %f90, %f89, %f431, %f278; + fma.rn.f32 %f279, %f274, %f429, 0f00000000; + fma.rn.f32 %f280, %f275, %f430, %f279; + fma.rn.f32 %f281, %f276, %f431, %f280; + max.f32 %f91, %f281, %f264; + abs.f32 %f92, %f91; + setp.lt.f32 %p21, %f92, 0f00800000; + mul.f32 %f282, %f92, 0f4B800000; + selp.f32 %f283, %f282, %f92, %p21; + selp.f32 %f284, 0fC1C00000, 0f00000000, %p21; + mov.b32 %r44, %f283; + add.s32 %r45, %r44, -1060439283; + and.b32 %r46, %r45, -8388608; + sub.s32 %r47, %r44, %r46; + mov.b32 %f285, %r47; + cvt.rn.f32.s32 %f286, %r46; + mov.f32 %f287, 0f34000000; + fma.rn.f32 %f288, %f286, %f287, %f284; + add.f32 %f289, %f285, 0fBF800000; + add.f32 %f290, %f285, 0f3F800000; + mov.f32 %f435, 0f3F800000; + rcp.approx.ftz.f32 %f291, %f290; + add.f32 %f292, %f289, %f289; + mul.f32 %f293, %f292, %f291; + mul.f32 %f294, %f293, %f293; + sub.f32 %f295, %f289, %f293; + add.f32 %f296, %f295, %f295; + neg.f32 %f297, %f293; + fma.rn.f32 %f298, %f297, %f289, %f296; + mul.rn.f32 %f299, %f291, %f298; + mov.f32 %f300, 0f3B52E7DB; + mov.f32 %f301, 0f3A2C32E4; + fma.rn.f32 %f302, %f301, %f294, %f300; + mov.f32 %f303, 0f3C93BB73; + fma.rn.f32 %f304, %f302, %f294, %f303; + mov.f32 %f305, 0f3DF6384F; + fma.rn.f32 %f306, %f304, %f294, %f305; + mul.rn.f32 %f307, %f306, %f294; + mov.f32 %f308, 0f3FB8AA3B; + fma.rn.f32 %f309, %f293, %f308, %f288; + sub.f32 %f310, %f288, %f309; + fma.rn.f32 %f311, %f293, %f308, %f310; + fma.rn.f32 %f312, %f299, %f308, %f311; + mov.f32 %f313, 0f32A55E34; + fma.rn.f32 %f314, %f293, %f313, %f312; + mul.f32 %f315, %f307, 0f40400000; + fma.rn.f32 %f316, %f315, %f299, %f314; + fma.rn.f32 %f317, %f307, %f293, %f316; + add.rn.f32 %f318, %f309, %f317; + neg.f32 %f319, %f309; + add.rn.f32 %f320, %f318, %f319; + neg.f32 %f321, %f320; + add.rn.f32 %f322, %f317, %f321; + mul.rn.f32 %f323, %f318, %f419; + neg.f32 %f324, %f323; + fma.rn.f32 %f325, %f318, %f419, %f324; + fma.rn.f32 %f326, %f322, %f419, %f325; + cvt.rni.f32.f32 %f327, %f323; + sub.f32 %f328, %f323, %f327; + add.f32 %f329, %f326, %f328; + mov.f32 %f330, 0f3AAF85ED; + mov.f32 %f331, 0f391FCB8E; + fma.rn.f32 %f332, %f331, %f329, %f330; + mov.f32 %f333, 0f3C1D9856; + fma.rn.f32 %f334, %f332, %f329, %f333; + mov.f32 %f335, 0f3D6357BB; + fma.rn.f32 %f336, %f334, %f329, %f335; + mov.f32 %f337, 0f3E75FDEC; + fma.rn.f32 %f338, %f336, %f329, %f337; + mov.f32 %f339, 0f3F317218; + fma.rn.f32 %f340, %f338, %f329, %f339; + fma.rn.f32 %f341, %f340, %f329, %f435; + cvt.rzi.s32.f32 %r48, %f327; + setp.gt.f32 %p22, %f327, 0f00000000; + selp.b32 %r49, 0, -2097152000, %p22; + add.s32 %r50, %r49, 2130706432; + mov.b32 %f342, %r50; + mul.f32 %f343, %f341, %f342; + shl.b32 %r51, %r48, 23; + sub.s32 %r52, %r51, %r49; + mov.b32 %f344, %r52; + mul.f32 %f345, %f343, %f344; + abs.f32 %f346, %f323; + setp.gt.f32 %p23, %f346, 0f43180000; + setp.lt.f32 %p24, %f323, 0f00000000; + selp.f32 %f347, 0f00000000, 0f7F800000, %p24; + selp.f32 %f93, %f347, %f345, %p23; + setp.eq.f32 %p25, %f91, 0f3F800000; + setp.eq.f32 %p26, %f419, 0f00000000; + or.pred %p27, %p25, %p26; + @%p27 bra $L__BB1_54; + + abs.f32 %f405, %f91; + setp.gtu.f32 %p28, %f405, 0f7F800000; + @%p28 bra $L__BB1_53; + + abs.f32 %f94, %f419; + setp.gtu.f32 %p29, %f94, 0f7F800000; + @%p29 bra $L__BB1_53; + bra.uni $L__BB1_48; + +$L__BB1_53: + add.rn.f32 %f435, %f91, %f419; + +$L__BB1_54: + max.f32 %f362, %f90, %f264; + mul.f32 %f363, %f435, %f422; + fma.rn.f32 %f364, %f362, %f425, %f363; + mul.f32 %f365, %f435, %f421; + fma.rn.f32 %f366, %f362, %f424, %f365; + mul.f32 %f367, %f435, %f420; + fma.rn.f32 %f368, %f362, %f423, %f367; + mul.f32 %f369, %f364, 0f41000000; + mul.f32 %f370, %f366, 0f41000000; + mul.f32 %f371, %f368, 0f41000000; + mul.f32 %f372, %f86, %f86; + div.rn.f32 %f373, %f369, %f372; + div.rn.f32 %f374, %f370, %f372; + div.rn.f32 %f375, %f371, %f372; + add.f32 %f376, %f80, %f373; + add.f32 %f377, %f81, %f374; + add.f32 %f378, %f82, %f375; + add.f32 %f360, %f86, 0fBA83126F; + mov.f32 %f359, 0f3A83126F; + mov.u32 %r91, 255; + mov.u32 %r92, 13; + mov.u32 %r94, 2; + mov.u32 %r97, 1; + mov.u32 %r128, 0; + // begin inline asm + call(%r58,%r59,%r60,%r61,%r62,%r63,%r64,%r65,%r66,%r67,%r68,%r69,%r70,%r71,%r72,%r73,%r74,%r75,%r76,%r77,%r78,%r79,%r80,%r81,%r82,%r83,%r84,%r85,%r86,%r87,%r88,%r89),_optix_trace_typed_32,(%r128,%rd47,%f73,%f74,%f75,%f87,%f88,%f89,%f359,%f360,%f264,%r91,%r92,%r97,%r94,%r97,%r97,%r97,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128); + // end inline asm + setp.eq.s32 %p41, %r58, 0; + selp.f32 %f379, 0f3F800000, 0f3ECCCCCD, %p41; + fma.rn.f32 %f436, %f379, %f376, %f436; + fma.rn.f32 %f437, %f379, %f377, %f437; + fma.rn.f32 %f438, %f379, %f378, %f438; + add.s64 %rd164, %rd164, 12; + add.s32 %r135, %r135, 1; + setp.lt.s32 %p42, %r135, %r6; + @%p42 bra $L__BB1_45; + +$L__BB1_55: + cvt.rn.f32.s32 %f380, %r6; + div.rn.f32 %f381, %f436, %f380; + div.rn.f32 %f382, %f437, %f380; + div.rn.f32 %f383, %f438, %f380; + mov.u32 %r130, 0; + // begin inline asm + call (%r129), _optix_get_payload, (%r130); + // end inline asm + mov.u32 %r132, 1; + // begin inline asm + call (%r131), _optix_get_payload, (%r132); + // end inline asm + cvt.u64.u32 %rd155, %r129; + cvt.u64.u32 %rd156, %r131; + bfi.b64 %rd157, %rd155, %rd156, 32, 32; + ld.const.u32 %r133, [optix_params+28]; + cvt.rn.f32.s32 %f384, %r133; + ld.f32 %f385, [%rd157]; + fma.rn.f32 %f386, %f385, %f384, %f381; + ld.f32 %f387, [%rd157+4]; + fma.rn.f32 %f388, %f387, %f384, %f382; + ld.f32 %f389, [%rd157+8]; + fma.rn.f32 %f390, %f389, %f384, %f383; + ld.f32 %f391, [%rd157+12]; + fma.rn.f32 %f392, %f391, %f384, 0f3F800000; + add.s32 %r134, %r133, 1; + cvt.rn.f32.s32 %f393, %r134; + div.rn.f32 %f394, %f386, %f393; + div.rn.f32 %f395, %f388, %f393; + div.rn.f32 %f396, %f390, %f393; + div.rn.f32 %f397, %f392, %f393; + st.f32 [%rd157], %f394; + st.f32 [%rd157+4], %f395; + st.f32 [%rd157+8], %f396; + st.f32 [%rd157+12], %f397; + ret; + +} + // .globl __anyhit__radiance +.visible .entry __anyhit__radiance() +{ + + + + ret; + +} + // .globl __anyhit__shadow +.visible .entry __anyhit__shadow() +{ + + + + ret; + +} + // .globl __miss__radiance +.visible .entry __miss__radiance() +{ + .reg .b32 %r<5>; + .reg .b64 %rd<4>; + + + mov.u32 %r2, 0; + // begin inline asm + call (%r1), _optix_get_payload, (%r2); + // end inline asm + mov.u32 %r4, 1; + // begin inline asm + call (%r3), _optix_get_payload, (%r4); + // end inline asm + cvt.u64.u32 %rd1, %r1; + cvt.u64.u32 %rd2, %r3; + bfi.b64 %rd3, %rd1, %rd2, 32, 32; + st.u32 [%rd3], %r2; + st.u32 [%rd3+4], %r2; + st.u32 [%rd3+8], %r2; + st.u32 [%rd3+12], %r2; + ret; + +} + // .globl __miss__shadow +.visible .entry __miss__shadow() +{ + .reg .b32 %r<3>; + + + mov.u32 %r2, 0; + // begin inline asm + call _optix_set_payload, (%r2, %r2); + // end inline asm + ret; + +} + // .globl __raygen__render_frame +.visible .entry __raygen__render_frame() +{ + .reg .f32 %f<80>; + .reg .b32 %r<90>; + .reg .b64 %rd<6>; + + + // begin inline asm + call (%r1), _optix_get_launch_index_x, (); + // end inline asm + // begin inline asm + call (%r5), _optix_get_launch_index_y, (); + // end inline asm + ld.const.v2.u32 {%r81, %r82}, [optix_params+16]; + mov.u32 %r80, 0; + mov.u32 %r44, 1; + add.s32 %r85, %r82, %r1; + ld.const.u32 %r86, [optix_params+24]; + add.s32 %r87, %r86, %r5; + ld.const.u32 %r88, [optix_params+12]; + cvt.rn.f32.s32 %f10, %r85; + add.f32 %f11, %f10, 0f3F000000; + cvt.rn.f32.s32 %f12, %r88; + div.rn.f32 %f13, %f11, %f12; + cvt.rn.f32.s32 %f14, %r87; + add.f32 %f15, %f14, 0f3F000000; + cvt.rn.f32.s32 %f16, %r81; + div.rn.f32 %f17, %f15, %f16; + fma.rn.f32 %f18, %f13, 0f40000000, 0fBF800000; + fma.rn.f32 %f19, %f17, 0f40000000, 0fBF800000; + ld.const.v2.f32 {%f20, %f21}, [optix_params+240]; + fma.rn.f32 %f24, %f20, %f18, 0f00000000; + mov.f32 %f9, 0f00000000; + fma.rn.f32 %f25, %f21, %f19, %f24; + ld.const.v2.f32 {%f26, %f27}, [optix_params+248]; + add.f32 %f30, %f25, %f26; + add.f32 %f31, %f30, %f27; + ld.const.v2.f32 {%f32, %f33}, [optix_params+256]; + fma.rn.f32 %f36, %f32, %f18, 0f00000000; + fma.rn.f32 %f37, %f33, %f19, %f36; + ld.const.v2.f32 {%f38, %f39}, [optix_params+264]; + add.f32 %f42, %f37, %f38; + add.f32 %f43, %f42, %f39; + ld.const.v2.f32 {%f44, %f45}, [optix_params+272]; + fma.rn.f32 %f48, %f44, %f18, 0f00000000; + fma.rn.f32 %f49, %f45, %f19, %f48; + ld.const.v2.f32 {%f50, %f51}, [optix_params+280]; + add.f32 %f54, %f49, %f50; + add.f32 %f55, %f54, %f51; + ld.const.v2.f32 {%f56, %f57}, [optix_params+288]; + fma.rn.f32 %f60, %f56, %f18, 0f00000000; + fma.rn.f32 %f61, %f57, %f19, %f60; + ld.const.v2.f32 {%f62, %f63}, [optix_params+296]; + add.f32 %f66, %f61, %f62; + add.f32 %f67, %f66, %f63; + div.rn.f32 %f68, %f31, %f67; + div.rn.f32 %f69, %f43, %f67; + div.rn.f32 %f70, %f55, %f67; + ld.const.f32 %f1, [optix_params+228]; + sub.f32 %f71, %f68, %f1; + ld.const.v2.f32 {%f72, %f73}, [optix_params+232]; + sub.f32 %f74, %f69, %f72; + sub.f32 %f75, %f70, %f73; + fma.rn.f32 %f76, %f71, %f71, 0f00000000; + fma.rn.f32 %f77, %f74, %f74, %f76; + fma.rn.f32 %f78, %f75, %f75, %f77; + sqrt.rn.f32 %f79, %f78; + div.rn.f32 %f4, %f71, %f79; + div.rn.f32 %f5, %f74, %f79; + div.rn.f32 %f6, %f75, %f79; + ld.const.u64 %rd2, [optix_params]; + // begin inline asm + call (%r7), _optix_get_launch_dimension_x, (); + // end inline asm + mad.lo.s32 %r89, %r7, %r5, %r1; + mul.wide.u32 %rd3, %r89, 16; + add.s64 %rd4, %rd2, %rd3; + shr.u64 %rd5, %rd4, 32; + cvt.u32.u64 %r49, %rd5; + cvt.u32.u64 %r50, %rd4; + ld.const.u64 %rd1, [optix_params+312]; + mov.f32 %f8, 0f60AD78EC; + mov.u32 %r43, 255; + mov.u32 %r48, 2; + // begin inline asm + call(%r10,%r11,%r12,%r13,%r14,%r15,%r16,%r17,%r18,%r19,%r20,%r21,%r22,%r23,%r24,%r25,%r26,%r27,%r28,%r29,%r30,%r31,%r32,%r33,%r34,%r35,%r36,%r37,%r38,%r39,%r40,%r41),_optix_trace_typed_32,(%r80,%rd1,%f1,%f72,%f73,%f4,%f5,%f6,%f9,%f8,%f9,%r43,%r44,%r80,%r48,%r80,%r48,%r49,%r50,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80); + // end inline asm + ret; + +} + From 3e67e918aea398d645167faf4e360bb4259534f3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 21 May 2023 17:36:03 +0200 Subject: [PATCH 0827/1018] che_img: loading rgb images, and depth images --- src/gproshan/mesh/che_img.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 5e41a5f4..44cbee28 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -22,7 +22,7 @@ che_img::che_img(const std::string & file) void che_img::read_file(const std::string & file) { - CImg img(file.c_str()); + CImg img(file.c_str()); alloc(img.height() * img.width(), 2 * (img.height() - 1) * (img.width() - 1)); @@ -32,19 +32,27 @@ void che_img::read_file(const std::string & file) { if(i && j) { - VT[he++] = v; - VT[he++] = (i - 1) * img.height() + j; - VT[he++] = i * img.height() + j - 1; - VT[he++] = i * img.height() + j - 1; VT[he++] = (i - 1) * img.height() + j; + VT[he++] = v; + VT[he++] = (i - 1) * img.height() + j - 1; + VT[he++] = (i - 1) * img.height() + j; + VT[he++] = i * img.height() + j - 1; } - GT[v++] = {real_t(i), real_t(j), img(i, j)}; + const int & c = img.width() - i - 1; + const int & r = img.height() - j - 1; + + GT[v] = {real_t(i), real_t(j), real_t(img.spectrum() == 1 ? - img(c, r) : 0)}; + + if(img.spectrum() == 3) + VC[v] = {img(c, r, 0), img(c, r, 1), img(c, r, 2)}; + + ++v; } - std::thread([](CImg img) { img.display(); }, img).detach(); +// std::thread([](CImg img) { img.display(); }, img).detach(); } From 6d34db5d30ac00778909f3a0f647c6e7f6fe1ba7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 May 2023 16:54:53 +0200 Subject: [PATCH 0828/1018] geometry: minor updates --- include/gproshan/geometry/mat.h | 2 +- include/gproshan/geometry/points.h | 2 +- include/gproshan/geometry/vec.h | 14 +++++++------- src/gproshan/viewer/scene_viewer.cpp | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index eb799302..bff29b00 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -15,7 +15,7 @@ template class mat { public: - row rows[N]; + row rows[N] = {}; public: __host_device__ diff --git a/include/gproshan/geometry/points.h b/include/gproshan/geometry/points.h index 2b78bce4..00ed5d03 100644 --- a/include/gproshan/geometry/points.h +++ b/include/gproshan/geometry/points.h @@ -23,7 +23,7 @@ std::vector > sampling_4points(const size_t & n, const vec & a, const size_t n2 = n * n; for(index_t i = 0; i <= n; ++i) for(index_t j = 0; j <= n; ++j) - points.push_back((j * (i * a + (n - i) * d) + + points.push_back((j * (i * a + (n - i) * d) + (n - j) * (i * b + (n - i) * c)) / n2); return points; diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 9ab5b904..b2cd026c 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -29,6 +29,13 @@ class vec T values[N] = {}; public: + __host_device__ + vec(const T & val = 0) + { + for(T & v: values) + v = val; + } + __host_device__ vec(const std::initializer_list & list) { @@ -52,13 +59,6 @@ class vec values[i] = v[i]; } - __host_device__ - vec(const T & val = 0) - { - for(T & v: values) - v = val; - } - __host_device__ T & operator [] (const index_t & i) { diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index d70f1ccd..b7ad834a 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -34,9 +34,9 @@ void scene_viewer::init_texture(const GLuint & gltex, const scene::texture & tex glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); - tex.spectrum == 3 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data) - : tex.spectrum == 4 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data) - : glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, tex.width, tex.height, 0, GL_RED, GL_UNSIGNED_BYTE, tex.data); + tex.spectrum == 3 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tex.data) + : tex.spectrum == 4 ? glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data) + : glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, tex.width, tex.height, 0, GL_RED, GL_UNSIGNED_BYTE, tex.data); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); From 5b015cc3cd58aaa06637819850fd1baab8ff6ef8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 May 2023 16:55:07 +0200 Subject: [PATCH 0829/1018] update gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a098252e..94647d14 100644 --- a/.gitignore +++ b/.gitignore @@ -6,10 +6,10 @@ tmp/* *.swp *.swo *.user +*.ptx *.vscode/* *.DS_Store include/gproshan/config.h -src/rt_optix.ptx From 4793185e843ec652ef095cf30de550b8aa76d154 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 May 2023 16:55:51 +0200 Subject: [PATCH 0830/1018] fix .gitignore --- src/optix.ptx | 955 -------------------------------------------------- 1 file changed, 955 deletions(-) delete mode 100644 src/optix.ptx diff --git a/src/optix.ptx b/src/optix.ptx deleted file mode 100644 index 1c56da64..00000000 --- a/src/optix.ptx +++ /dev/null @@ -1,955 +0,0 @@ -// -// Generated by NVIDIA NVVM Compiler -// -// Compiler Build ID: CL-32688072 -// Cuda compilation tools, release 12.1, V12.1.105 -// Based on NVVM 7.0.1 -// - -.version 8.1 -.target sm_61 -.address_size 64 - - // .globl __closesthit__shadow -.extern .const .align 8 .b8 optix_params[352]; - -.visible .entry __closesthit__shadow() -{ - - - - ret; - -} - // .globl __closesthit__radiance -.visible .entry __closesthit__radiance() -{ - .reg .pred %p<43>; - .reg .b16 %rs<77>; - .reg .f32 %f<439>; - .reg .b32 %r<136>; - .reg .b64 %rd<165>; - - - // begin inline asm - call (%rd50), _optix_get_sbt_data_ptr_64, (); - // end inline asm - ld.u64 %rd53, [%rd50]; - // begin inline asm - call (%r9), _optix_read_primitive_idx, (); - // end inline asm - // begin inline asm - call (%f105, %f106), _optix_get_triangle_barycentrics, (); - // end inline asm - // begin inline asm - call (%rd51), _optix_get_gas_traversable_handle, (); - // end inline asm - // begin inline asm - call (%r10), _optix_read_sbt_gas_idx, (); - // end inline asm - // begin inline asm - call (%f107), _optix_get_ray_time, (); - // end inline asm - // begin inline asm - call (%f108, %f109, %f110, %f111, %f112, %f113, %f114, %f115, %f116), _optix_get_triangle_vertex_data, (%rd51, %r9, %r10, %f107); - // end inline asm - mul.lo.s32 %r13, %r9, 3; - ld.u64 %rd54, [%rd53+48]; - mul.wide.u32 %rd55, %r13, 4; - add.s64 %rd56, %rd54, %rd55; - add.s32 %r14, %r13, 1; - mul.wide.u32 %rd57, %r14, 4; - add.s64 %rd58, %rd54, %rd57; - add.s32 %r15, %r13, 2; - mul.wide.u32 %rd59, %r15, 4; - add.s64 %rd60, %rd54, %rd59; - ld.u32 %r16, [%rd56]; - cvt.u64.u32 %rd1, %r16; - ld.u64 %rd61, [%rd53+40]; - mul.wide.u32 %rd62, %r16, 3; - add.s64 %rd63, %rd61, %rd62; - ld.u8 %rs31, [%rd63]; - cvt.rn.f32.u16 %f125, %rs31; - ld.u8 %rs32, [%rd63+1]; - cvt.rn.f32.u16 %f126, %rs32; - ld.u8 %rs33, [%rd63+2]; - cvt.rn.f32.u16 %f127, %rs33; - ld.u32 %r17, [%rd58]; - cvt.u64.u32 %rd2, %r17; - mul.wide.u32 %rd64, %r17, 3; - add.s64 %rd65, %rd61, %rd64; - ld.u8 %rs34, [%rd65]; - cvt.rn.f32.u16 %f128, %rs34; - ld.u8 %rs35, [%rd65+1]; - cvt.rn.f32.u16 %f129, %rs35; - ld.u8 %rs36, [%rd65+2]; - cvt.rn.f32.u16 %f130, %rs36; - ld.u32 %r18, [%rd60]; - cvt.u64.u32 %rd3, %r18; - mul.wide.u32 %rd66, %r18, 3; - add.s64 %rd67, %rd61, %rd66; - ld.u8 %rs37, [%rd67]; - cvt.rn.f32.u16 %f131, %rs37; - ld.u8 %rs38, [%rd67+1]; - cvt.rn.f32.u16 %f132, %rs38; - ld.u8 %rs39, [%rd67+2]; - cvt.rn.f32.u16 %f133, %rs39; - mov.f32 %f134, 0f3F800000; - sub.f32 %f135, %f134, %f105; - sub.f32 %f12, %f135, %f106; - mul.f32 %f136, %f105, %f128; - mul.f32 %f137, %f105, %f129; - mul.f32 %f138, %f105, %f130; - fma.rn.f32 %f139, %f12, %f125, %f136; - fma.rn.f32 %f140, %f12, %f126, %f137; - fma.rn.f32 %f141, %f12, %f127, %f138; - fma.rn.f32 %f142, %f106, %f131, %f139; - fma.rn.f32 %f143, %f106, %f132, %f140; - fma.rn.f32 %f144, %f106, %f133, %f141; - div.rn.f32 %f425, %f142, 0f437F0000; - div.rn.f32 %f424, %f143, 0f437F0000; - div.rn.f32 %f423, %f144, 0f437F0000; - ld.u64 %rd68, [%rd53+32]; - mul.wide.u32 %rd69, %r16, 12; - add.s64 %rd70, %rd68, %rd69; - ld.f32 %f145, [%rd70]; - ld.f32 %f146, [%rd70+4]; - ld.f32 %f147, [%rd70+8]; - mul.wide.u32 %rd71, %r17, 12; - add.s64 %rd72, %rd68, %rd71; - ld.f32 %f148, [%rd72]; - mul.f32 %f149, %f148, %f105; - ld.f32 %f150, [%rd72+4]; - mul.f32 %f151, %f150, %f105; - ld.f32 %f152, [%rd72+8]; - mul.f32 %f153, %f152, %f105; - fma.rn.f32 %f154, %f12, %f145, %f149; - fma.rn.f32 %f155, %f12, %f146, %f151; - fma.rn.f32 %f156, %f12, %f147, %f153; - mul.wide.u32 %rd73, %r18, 12; - add.s64 %rd74, %rd68, %rd73; - ld.f32 %f157, [%rd74]; - ld.f32 %f158, [%rd74+4]; - ld.f32 %f159, [%rd74+8]; - fma.rn.f32 %f429, %f157, %f106, %f154; - fma.rn.f32 %f430, %f158, %f106, %f155; - fma.rn.f32 %f431, %f159, %f106, %f156; - ld.const.u64 %rd4, [optix_params+336]; - setp.eq.s64 %p1, %rd4, 0; - mov.f32 %f419, 0f40800000; - mov.f32 %f420, 0f3E4CCCCD; - mov.f32 %f421, 0f3E4CCCCD; - mov.f32 %f422, 0f3E4CCCCD; - mov.f32 %f426, 0f3ECCCCCD; - mov.f32 %f427, 0f3ECCCCCD; - mov.f32 %f428, 0f3ECCCCCD; - @%p1 bra $L__BB1_41; - - cvta.to.global.u64 %rd75, %rd4; - mul.wide.u32 %rd76, %r9, 4; - add.s64 %rd77, %rd75, %rd76; - ld.global.u32 %r2, [%rd77]; - setp.eq.s32 %p2, %r2, -1; - @%p2 bra $L__BB1_41; - - ld.const.u64 %rd5, [optix_params+320]; - ld.const.u64 %rd6, [optix_params+344]; - setp.eq.s64 %p3, %rd6, 0; - mov.f32 %f408, 0f00000000; - mov.f32 %f409, %f408; - @%p3 bra $L__BB1_4; - - cvta.to.global.u64 %rd78, %rd6; - shl.b64 %rd79, %rd1, 3; - add.s64 %rd80, %rd78, %rd79; - ld.global.f32 %f169, [%rd80]; - ld.global.f32 %f170, [%rd80+4]; - shl.b64 %rd81, %rd2, 3; - add.s64 %rd82, %rd78, %rd81; - ld.global.f32 %f171, [%rd82]; - mul.f32 %f172, %f171, %f105; - ld.global.f32 %f173, [%rd82+4]; - mul.f32 %f174, %f173, %f105; - fma.rn.f32 %f175, %f12, %f169, %f172; - fma.rn.f32 %f176, %f12, %f170, %f174; - shl.b64 %rd83, %rd3, 3; - add.s64 %rd84, %rd78, %rd83; - ld.global.f32 %f177, [%rd84]; - ld.global.f32 %f178, [%rd84+4]; - fma.rn.f32 %f408, %f177, %f106, %f175; - fma.rn.f32 %f409, %f178, %f106, %f176; - -$L__BB1_4: - cvta.to.global.u64 %rd85, %rd5; - mul.wide.u32 %rd86, %r2, 72; - add.s64 %rd7, %rd85, %rd86; - ld.global.f32 %f428, [%rd7]; - ld.global.f32 %f427, [%rd7+4]; - ld.global.f32 %f426, [%rd7+8]; - ld.global.u32 %r3, [%rd7+52]; - setp.eq.s32 %p4, %r3, -1; - @%p4 bra $L__BB1_16; - - ld.const.u64 %rd87, [optix_params+328]; - cvta.to.global.u64 %rd88, %rd87; - mul.wide.s32 %rd89, %r3, 32; - add.s64 %rd90, %rd88, %rd89; - add.s64 %rd8, %rd90, 8; - ld.global.u64 %rd9, [%rd90+8]; - add.s64 %rd91, %rd9, -1; - cvt.rn.f32.u64 %f179, %rd91; - mul.f32 %f180, %f408, %f179; - cvt.rzi.s32.f32 %r19, %f180; - cvt.s64.s32 %rd92, %r19; - add.s64 %rd10, %rd9, %rd92; - or.b64 %rd93, %rd10, %rd9; - and.b64 %rd94, %rd93, -4294967296; - setp.eq.s64 %p5, %rd94, 0; - @%p5 bra $L__BB1_7; - - rem.u64 %rd158, %rd10, %rd9; - bra.uni $L__BB1_8; - -$L__BB1_7: - cvt.u32.u64 %r20, %rd9; - cvt.u32.u64 %r21, %rd10; - rem.u32 %r22, %r21, %r20; - cvt.u64.u32 %rd158, %r22; - -$L__BB1_8: - ld.global.u64 %rd14, [%rd8+8]; - add.s64 %rd95, %rd14, -1; - cvt.rn.f32.u64 %f181, %rd95; - mul.f32 %f182, %f409, %f181; - cvt.rzi.s32.f32 %r23, %f182; - cvt.s64.s32 %rd96, %r23; - add.s64 %rd15, %rd14, %rd96; - or.b64 %rd97, %rd15, %rd14; - and.b64 %rd98, %rd97, -4294967296; - setp.eq.s64 %p6, %rd98, 0; - @%p6 bra $L__BB1_10; - - rem.u64 %rd159, %rd15, %rd14; - bra.uni $L__BB1_11; - -$L__BB1_10: - cvt.u32.u64 %r24, %rd14; - cvt.u32.u64 %r25, %rd15; - rem.u32 %r26, %r25, %r24; - cvt.u64.u32 %rd159, %r26; - -$L__BB1_11: - cvt.s64.s32 %rd99, %rd159; - mul.lo.s64 %rd100, %rd99, %rd9; - and.b64 %rd101, %rd158, 4294967295; - add.s64 %rd19, %rd100, %rd101; - ld.global.u64 %rd20, [%rd8+16]; - setp.ne.s64 %p7, %rd20, 3; - mov.u16 %rs64, 250; - mov.u16 %rs63, 240; - mov.u16 %rs62, 230; - @%p7 bra $L__BB1_13; - - cvt.s64.s32 %rd102, %rd19; - ld.global.u64 %rd103, [%rd8+-8]; - mul.lo.s64 %rd104, %rd102, 3; - add.s64 %rd105, %rd103, %rd104; - ld.u8 %rs62, [%rd105]; - ld.u8 %rs63, [%rd105+1]; - ld.u8 %rs64, [%rd105+2]; - -$L__BB1_13: - setp.ne.s64 %p8, %rd20, 1; - @%p8 bra $L__BB1_15; - - cvt.s64.s32 %rd106, %rd19; - ld.global.u64 %rd107, [%rd8+-8]; - add.s64 %rd108, %rd107, %rd106; - ld.u8 %rs62, [%rd108]; - mov.u16 %rs63, %rs62; - mov.u16 %rs64, %rs62; - -$L__BB1_15: - and.b16 %rs43, %rs62, 255; - cvt.rn.f32.u16 %f183, %rs43; - div.rn.f32 %f184, %f183, 0f437F0000; - and.b16 %rs44, %rs63, 255; - cvt.rn.f32.u16 %f185, %rs44; - div.rn.f32 %f186, %f185, 0f437F0000; - and.b16 %rs45, %rs64, 255; - cvt.rn.f32.u16 %f187, %rs45; - div.rn.f32 %f188, %f187, 0f437F0000; - mul.f32 %f428, %f184, %f428; - mul.f32 %f427, %f186, %f427; - mul.f32 %f426, %f188, %f426; - -$L__BB1_16: - ld.global.f32 %f425, [%rd7+12]; - ld.global.f32 %f424, [%rd7+16]; - ld.global.f32 %f423, [%rd7+20]; - ld.global.u32 %r4, [%rd7+56]; - setp.eq.s32 %p9, %r4, -1; - @%p9 bra $L__BB1_28; - - ld.const.u64 %rd109, [optix_params+328]; - cvta.to.global.u64 %rd110, %rd109; - mul.wide.s32 %rd111, %r4, 32; - add.s64 %rd112, %rd110, %rd111; - add.s64 %rd21, %rd112, 8; - ld.global.u64 %rd22, [%rd112+8]; - add.s64 %rd113, %rd22, -1; - cvt.rn.f32.u64 %f189, %rd113; - mul.f32 %f190, %f408, %f189; - cvt.rzi.s32.f32 %r27, %f190; - cvt.s64.s32 %rd114, %r27; - add.s64 %rd23, %rd22, %rd114; - or.b64 %rd115, %rd23, %rd22; - and.b64 %rd116, %rd115, -4294967296; - setp.eq.s64 %p10, %rd116, 0; - @%p10 bra $L__BB1_19; - - rem.u64 %rd160, %rd23, %rd22; - bra.uni $L__BB1_20; - -$L__BB1_19: - cvt.u32.u64 %r28, %rd22; - cvt.u32.u64 %r29, %rd23; - rem.u32 %r30, %r29, %r28; - cvt.u64.u32 %rd160, %r30; - -$L__BB1_20: - ld.global.u64 %rd27, [%rd21+8]; - add.s64 %rd117, %rd27, -1; - cvt.rn.f32.u64 %f191, %rd117; - mul.f32 %f192, %f409, %f191; - cvt.rzi.s32.f32 %r31, %f192; - cvt.s64.s32 %rd118, %r31; - add.s64 %rd28, %rd27, %rd118; - or.b64 %rd119, %rd28, %rd27; - and.b64 %rd120, %rd119, -4294967296; - setp.eq.s64 %p11, %rd120, 0; - @%p11 bra $L__BB1_22; - - rem.u64 %rd161, %rd28, %rd27; - bra.uni $L__BB1_23; - -$L__BB1_22: - cvt.u32.u64 %r32, %rd27; - cvt.u32.u64 %r33, %rd28; - rem.u32 %r34, %r33, %r32; - cvt.u64.u32 %rd161, %r34; - -$L__BB1_23: - cvt.s64.s32 %rd121, %rd161; - mul.lo.s64 %rd122, %rd121, %rd22; - and.b64 %rd123, %rd160, 4294967295; - add.s64 %rd32, %rd122, %rd123; - ld.global.u64 %rd33, [%rd21+16]; - setp.ne.s64 %p12, %rd33, 3; - mov.u16 %rs70, 250; - mov.u16 %rs69, 240; - mov.u16 %rs68, 230; - @%p12 bra $L__BB1_25; - - cvt.s64.s32 %rd124, %rd32; - ld.global.u64 %rd125, [%rd21+-8]; - mul.lo.s64 %rd126, %rd124, 3; - add.s64 %rd127, %rd125, %rd126; - ld.u8 %rs68, [%rd127]; - ld.u8 %rs69, [%rd127+1]; - ld.u8 %rs70, [%rd127+2]; - -$L__BB1_25: - setp.ne.s64 %p13, %rd33, 1; - @%p13 bra $L__BB1_27; - - cvt.s64.s32 %rd128, %rd32; - ld.global.u64 %rd129, [%rd21+-8]; - add.s64 %rd130, %rd129, %rd128; - ld.u8 %rs68, [%rd130]; - mov.u16 %rs69, %rs68; - mov.u16 %rs70, %rs68; - -$L__BB1_27: - and.b16 %rs49, %rs68, 255; - cvt.rn.f32.u16 %f193, %rs49; - div.rn.f32 %f194, %f193, 0f437F0000; - and.b16 %rs50, %rs69, 255; - cvt.rn.f32.u16 %f195, %rs50; - div.rn.f32 %f196, %f195, 0f437F0000; - and.b16 %rs51, %rs70, 255; - cvt.rn.f32.u16 %f197, %rs51; - div.rn.f32 %f198, %f197, 0f437F0000; - mul.f32 %f425, %f194, %f425; - mul.f32 %f424, %f196, %f424; - mul.f32 %f423, %f198, %f423; - -$L__BB1_28: - ld.global.f32 %f422, [%rd7+24]; - ld.global.f32 %f421, [%rd7+28]; - ld.global.f32 %f420, [%rd7+32]; - ld.global.u32 %r5, [%rd7+60]; - setp.eq.s32 %p14, %r5, -1; - @%p14 bra $L__BB1_40; - - ld.const.u64 %rd131, [optix_params+328]; - cvta.to.global.u64 %rd132, %rd131; - mul.wide.s32 %rd133, %r5, 32; - add.s64 %rd134, %rd132, %rd133; - add.s64 %rd34, %rd134, 8; - ld.global.u64 %rd35, [%rd134+8]; - add.s64 %rd135, %rd35, -1; - cvt.rn.f32.u64 %f199, %rd135; - mul.f32 %f200, %f408, %f199; - cvt.rzi.s32.f32 %r35, %f200; - cvt.s64.s32 %rd136, %r35; - add.s64 %rd36, %rd35, %rd136; - or.b64 %rd137, %rd36, %rd35; - and.b64 %rd138, %rd137, -4294967296; - setp.eq.s64 %p15, %rd138, 0; - @%p15 bra $L__BB1_31; - - rem.u64 %rd162, %rd36, %rd35; - bra.uni $L__BB1_32; - -$L__BB1_31: - cvt.u32.u64 %r36, %rd35; - cvt.u32.u64 %r37, %rd36; - rem.u32 %r38, %r37, %r36; - cvt.u64.u32 %rd162, %r38; - -$L__BB1_32: - ld.global.u64 %rd40, [%rd34+8]; - add.s64 %rd139, %rd40, -1; - cvt.rn.f32.u64 %f201, %rd139; - mul.f32 %f202, %f409, %f201; - cvt.rzi.s32.f32 %r39, %f202; - cvt.s64.s32 %rd140, %r39; - add.s64 %rd41, %rd40, %rd140; - or.b64 %rd141, %rd41, %rd40; - and.b64 %rd142, %rd141, -4294967296; - setp.eq.s64 %p16, %rd142, 0; - @%p16 bra $L__BB1_34; - - rem.u64 %rd163, %rd41, %rd40; - bra.uni $L__BB1_35; - -$L__BB1_34: - cvt.u32.u64 %r40, %rd40; - cvt.u32.u64 %r41, %rd41; - rem.u32 %r42, %r41, %r40; - cvt.u64.u32 %rd163, %r42; - -$L__BB1_35: - cvt.s64.s32 %rd143, %rd163; - mul.lo.s64 %rd144, %rd143, %rd35; - and.b64 %rd145, %rd162, 4294967295; - add.s64 %rd45, %rd144, %rd145; - ld.global.u64 %rd46, [%rd34+16]; - setp.ne.s64 %p17, %rd46, 3; - mov.u16 %rs76, 250; - mov.u16 %rs75, 240; - mov.u16 %rs74, 230; - @%p17 bra $L__BB1_37; - - cvt.s64.s32 %rd146, %rd45; - ld.global.u64 %rd147, [%rd34+-8]; - mul.lo.s64 %rd148, %rd146, 3; - add.s64 %rd149, %rd147, %rd148; - ld.u8 %rs74, [%rd149]; - ld.u8 %rs75, [%rd149+1]; - ld.u8 %rs76, [%rd149+2]; - -$L__BB1_37: - setp.ne.s64 %p18, %rd46, 1; - @%p18 bra $L__BB1_39; - - cvt.s64.s32 %rd150, %rd45; - ld.global.u64 %rd151, [%rd34+-8]; - add.s64 %rd152, %rd151, %rd150; - ld.u8 %rs74, [%rd152]; - mov.u16 %rs75, %rs74; - mov.u16 %rs76, %rs74; - -$L__BB1_39: - and.b16 %rs55, %rs74, 255; - cvt.rn.f32.u16 %f203, %rs55; - div.rn.f32 %f204, %f203, 0f437F0000; - and.b16 %rs56, %rs75, 255; - cvt.rn.f32.u16 %f205, %rs56; - div.rn.f32 %f206, %f205, 0f437F0000; - and.b16 %rs57, %rs76, 255; - cvt.rn.f32.u16 %f207, %rs57; - div.rn.f32 %f208, %f207, 0f437F0000; - mul.f32 %f422, %f204, %f422; - mul.f32 %f421, %f206, %f421; - mul.f32 %f420, %f208, %f420; - -$L__BB1_40: - ld.global.f32 %f419, [%rd7+40]; - -$L__BB1_41: - ld.const.u8 %rs58, [optix_params+304]; - setp.eq.s16 %p19, %rs58, 0; - @%p19 bra $L__BB1_43; - - sub.f32 %f209, %f111, %f108; - sub.f32 %f210, %f116, %f110; - sub.f32 %f211, %f112, %f109; - mul.f32 %f212, %f211, %f210; - sub.f32 %f213, %f115, %f109; - sub.f32 %f214, %f113, %f110; - mul.f32 %f215, %f214, %f213; - sub.f32 %f216, %f212, %f215; - sub.f32 %f217, %f114, %f108; - mul.f32 %f218, %f214, %f217; - mul.f32 %f219, %f209, %f210; - sub.f32 %f220, %f218, %f219; - mul.f32 %f221, %f209, %f213; - mul.f32 %f222, %f211, %f217; - sub.f32 %f223, %f221, %f222; - fma.rn.f32 %f224, %f216, %f216, 0f00000000; - fma.rn.f32 %f225, %f220, %f220, %f224; - fma.rn.f32 %f226, %f223, %f223, %f225; - sqrt.rn.f32 %f227, %f226; - div.rn.f32 %f431, %f223, %f227; - div.rn.f32 %f430, %f220, %f227; - div.rn.f32 %f429, %f216, %f227; - -$L__BB1_43: - mul.f32 %f231, %f111, %f105; - fma.rn.f32 %f232, %f12, %f108, %f231; - mul.f32 %f233, %f112, %f105; - fma.rn.f32 %f234, %f12, %f109, %f233; - mul.f32 %f235, %f113, %f105; - fma.rn.f32 %f236, %f12, %f110, %f235; - fma.rn.f32 %f73, %f114, %f106, %f232; - fma.rn.f32 %f74, %f115, %f106, %f234; - fma.rn.f32 %f75, %f116, %f106, %f236; - ld.const.f32 %f237, [optix_params+228]; - sub.f32 %f238, %f237, %f73; - ld.const.v2.f32 {%f239, %f240}, [optix_params+232]; - sub.f32 %f243, %f239, %f74; - sub.f32 %f244, %f240, %f75; - fma.rn.f32 %f245, %f238, %f238, 0f00000000; - mov.f32 %f436, 0f00000000; - fma.rn.f32 %f246, %f243, %f243, %f245; - fma.rn.f32 %f247, %f244, %f244, %f246; - sqrt.rn.f32 %f248, %f247; - div.rn.f32 %f76, %f238, %f248; - div.rn.f32 %f77, %f243, %f248; - div.rn.f32 %f78, %f244, %f248; - ld.const.u32 %r6, [optix_params+32]; - setp.lt.s32 %p20, %r6, 1; - mov.f32 %f437, %f436; - mov.f32 %f438, %f436; - @%p20 bra $L__BB1_55; - - ld.const.u64 %rd47, [optix_params+312]; - mov.u64 %rd164, optix_params; - mul.f32 %f252, %f419, 0f3F000000; - cvt.rzi.f32.f32 %f253, %f252; - add.f32 %f254, %f253, %f253; - sub.f32 %f255, %f419, %f254; - abs.f32 %f79, %f255; - mul.f32 %f80, %f428, 0f3E4CCCCD; - mul.f32 %f81, %f427, 0f3E4CCCCD; - mul.f32 %f82, %f426, 0f3E4CCCCD; - mov.u32 %r135, 0; - bra.uni $L__BB1_45; - -$L__BB1_48: - abs.f32 %f406, %f91; - setp.eq.f32 %p30, %f91, 0f00000000; - setp.eq.f32 %p31, %f406, 0f7F800000; - or.pred %p32, %p30, %p31; - @%p32 bra $L__BB1_52; - bra.uni $L__BB1_49; - -$L__BB1_52: - setp.lt.f32 %p39, %f419, 0f00000000; - add.f32 %f352, %f91, %f91; - mov.b32 %r53, %f352; - xor.b32 %r54, %r53, 2139095040; - selp.b32 %r55, %r54, %r53, %p39; - and.b32 %r56, %r55, 2147483647; - setp.eq.f32 %p40, %f79, 0f3F800000; - selp.b32 %r57, %r55, %r56, %p40; - mov.b32 %f435, %r57; - bra.uni $L__BB1_54; - -$L__BB1_49: - mov.f32 %f435, 0f3F800000; - setp.eq.f32 %p33, %f91, 0fBF800000; - setp.eq.f32 %p34, %f94, 0f7F800000; - and.pred %p35, %p33, %p34; - @%p35 bra $L__BB1_54; - - setp.geu.f32 %p36, %f91, 0f00000000; - mov.f32 %f435, %f93; - @%p36 bra $L__BB1_54; - - setp.eq.f32 %p37, %f79, 0f3F800000; - neg.f32 %f349, %f93; - selp.f32 %f350, %f349, %f93, %p37; - cvt.rmi.f32.f32 %f351, %f419; - setp.neu.f32 %p38, %f351, %f419; - selp.f32 %f435, 0f7FFFFFFF, %f350, %p38; - bra.uni $L__BB1_54; - -$L__BB1_45: - ld.const.f32 %f257, [%rd164+36]; - sub.f32 %f258, %f257, %f73; - ld.const.f32 %f259, [%rd164+40]; - sub.f32 %f260, %f259, %f74; - ld.const.f32 %f261, [%rd164+44]; - sub.f32 %f262, %f261, %f75; - fma.rn.f32 %f263, %f258, %f258, 0f00000000; - mov.f32 %f264, 0f00000000; - fma.rn.f32 %f265, %f260, %f260, %f263; - fma.rn.f32 %f266, %f262, %f262, %f265; - sqrt.rn.f32 %f86, %f266; - div.rn.f32 %f87, %f258, %f86; - div.rn.f32 %f88, %f260, %f86; - div.rn.f32 %f89, %f262, %f86; - add.f32 %f267, %f87, %f76; - add.f32 %f268, %f88, %f77; - add.f32 %f269, %f89, %f78; - fma.rn.f32 %f270, %f267, %f267, 0f00000000; - fma.rn.f32 %f271, %f268, %f268, %f270; - fma.rn.f32 %f272, %f269, %f269, %f271; - sqrt.rn.f32 %f273, %f272; - div.rn.f32 %f274, %f267, %f273; - div.rn.f32 %f275, %f268, %f273; - div.rn.f32 %f276, %f269, %f273; - fma.rn.f32 %f277, %f87, %f429, 0f00000000; - fma.rn.f32 %f278, %f88, %f430, %f277; - fma.rn.f32 %f90, %f89, %f431, %f278; - fma.rn.f32 %f279, %f274, %f429, 0f00000000; - fma.rn.f32 %f280, %f275, %f430, %f279; - fma.rn.f32 %f281, %f276, %f431, %f280; - max.f32 %f91, %f281, %f264; - abs.f32 %f92, %f91; - setp.lt.f32 %p21, %f92, 0f00800000; - mul.f32 %f282, %f92, 0f4B800000; - selp.f32 %f283, %f282, %f92, %p21; - selp.f32 %f284, 0fC1C00000, 0f00000000, %p21; - mov.b32 %r44, %f283; - add.s32 %r45, %r44, -1060439283; - and.b32 %r46, %r45, -8388608; - sub.s32 %r47, %r44, %r46; - mov.b32 %f285, %r47; - cvt.rn.f32.s32 %f286, %r46; - mov.f32 %f287, 0f34000000; - fma.rn.f32 %f288, %f286, %f287, %f284; - add.f32 %f289, %f285, 0fBF800000; - add.f32 %f290, %f285, 0f3F800000; - mov.f32 %f435, 0f3F800000; - rcp.approx.ftz.f32 %f291, %f290; - add.f32 %f292, %f289, %f289; - mul.f32 %f293, %f292, %f291; - mul.f32 %f294, %f293, %f293; - sub.f32 %f295, %f289, %f293; - add.f32 %f296, %f295, %f295; - neg.f32 %f297, %f293; - fma.rn.f32 %f298, %f297, %f289, %f296; - mul.rn.f32 %f299, %f291, %f298; - mov.f32 %f300, 0f3B52E7DB; - mov.f32 %f301, 0f3A2C32E4; - fma.rn.f32 %f302, %f301, %f294, %f300; - mov.f32 %f303, 0f3C93BB73; - fma.rn.f32 %f304, %f302, %f294, %f303; - mov.f32 %f305, 0f3DF6384F; - fma.rn.f32 %f306, %f304, %f294, %f305; - mul.rn.f32 %f307, %f306, %f294; - mov.f32 %f308, 0f3FB8AA3B; - fma.rn.f32 %f309, %f293, %f308, %f288; - sub.f32 %f310, %f288, %f309; - fma.rn.f32 %f311, %f293, %f308, %f310; - fma.rn.f32 %f312, %f299, %f308, %f311; - mov.f32 %f313, 0f32A55E34; - fma.rn.f32 %f314, %f293, %f313, %f312; - mul.f32 %f315, %f307, 0f40400000; - fma.rn.f32 %f316, %f315, %f299, %f314; - fma.rn.f32 %f317, %f307, %f293, %f316; - add.rn.f32 %f318, %f309, %f317; - neg.f32 %f319, %f309; - add.rn.f32 %f320, %f318, %f319; - neg.f32 %f321, %f320; - add.rn.f32 %f322, %f317, %f321; - mul.rn.f32 %f323, %f318, %f419; - neg.f32 %f324, %f323; - fma.rn.f32 %f325, %f318, %f419, %f324; - fma.rn.f32 %f326, %f322, %f419, %f325; - cvt.rni.f32.f32 %f327, %f323; - sub.f32 %f328, %f323, %f327; - add.f32 %f329, %f326, %f328; - mov.f32 %f330, 0f3AAF85ED; - mov.f32 %f331, 0f391FCB8E; - fma.rn.f32 %f332, %f331, %f329, %f330; - mov.f32 %f333, 0f3C1D9856; - fma.rn.f32 %f334, %f332, %f329, %f333; - mov.f32 %f335, 0f3D6357BB; - fma.rn.f32 %f336, %f334, %f329, %f335; - mov.f32 %f337, 0f3E75FDEC; - fma.rn.f32 %f338, %f336, %f329, %f337; - mov.f32 %f339, 0f3F317218; - fma.rn.f32 %f340, %f338, %f329, %f339; - fma.rn.f32 %f341, %f340, %f329, %f435; - cvt.rzi.s32.f32 %r48, %f327; - setp.gt.f32 %p22, %f327, 0f00000000; - selp.b32 %r49, 0, -2097152000, %p22; - add.s32 %r50, %r49, 2130706432; - mov.b32 %f342, %r50; - mul.f32 %f343, %f341, %f342; - shl.b32 %r51, %r48, 23; - sub.s32 %r52, %r51, %r49; - mov.b32 %f344, %r52; - mul.f32 %f345, %f343, %f344; - abs.f32 %f346, %f323; - setp.gt.f32 %p23, %f346, 0f43180000; - setp.lt.f32 %p24, %f323, 0f00000000; - selp.f32 %f347, 0f00000000, 0f7F800000, %p24; - selp.f32 %f93, %f347, %f345, %p23; - setp.eq.f32 %p25, %f91, 0f3F800000; - setp.eq.f32 %p26, %f419, 0f00000000; - or.pred %p27, %p25, %p26; - @%p27 bra $L__BB1_54; - - abs.f32 %f405, %f91; - setp.gtu.f32 %p28, %f405, 0f7F800000; - @%p28 bra $L__BB1_53; - - abs.f32 %f94, %f419; - setp.gtu.f32 %p29, %f94, 0f7F800000; - @%p29 bra $L__BB1_53; - bra.uni $L__BB1_48; - -$L__BB1_53: - add.rn.f32 %f435, %f91, %f419; - -$L__BB1_54: - max.f32 %f362, %f90, %f264; - mul.f32 %f363, %f435, %f422; - fma.rn.f32 %f364, %f362, %f425, %f363; - mul.f32 %f365, %f435, %f421; - fma.rn.f32 %f366, %f362, %f424, %f365; - mul.f32 %f367, %f435, %f420; - fma.rn.f32 %f368, %f362, %f423, %f367; - mul.f32 %f369, %f364, 0f41000000; - mul.f32 %f370, %f366, 0f41000000; - mul.f32 %f371, %f368, 0f41000000; - mul.f32 %f372, %f86, %f86; - div.rn.f32 %f373, %f369, %f372; - div.rn.f32 %f374, %f370, %f372; - div.rn.f32 %f375, %f371, %f372; - add.f32 %f376, %f80, %f373; - add.f32 %f377, %f81, %f374; - add.f32 %f378, %f82, %f375; - add.f32 %f360, %f86, 0fBA83126F; - mov.f32 %f359, 0f3A83126F; - mov.u32 %r91, 255; - mov.u32 %r92, 13; - mov.u32 %r94, 2; - mov.u32 %r97, 1; - mov.u32 %r128, 0; - // begin inline asm - call(%r58,%r59,%r60,%r61,%r62,%r63,%r64,%r65,%r66,%r67,%r68,%r69,%r70,%r71,%r72,%r73,%r74,%r75,%r76,%r77,%r78,%r79,%r80,%r81,%r82,%r83,%r84,%r85,%r86,%r87,%r88,%r89),_optix_trace_typed_32,(%r128,%rd47,%f73,%f74,%f75,%f87,%f88,%f89,%f359,%f360,%f264,%r91,%r92,%r97,%r94,%r97,%r97,%r97,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128,%r128); - // end inline asm - setp.eq.s32 %p41, %r58, 0; - selp.f32 %f379, 0f3F800000, 0f3ECCCCCD, %p41; - fma.rn.f32 %f436, %f379, %f376, %f436; - fma.rn.f32 %f437, %f379, %f377, %f437; - fma.rn.f32 %f438, %f379, %f378, %f438; - add.s64 %rd164, %rd164, 12; - add.s32 %r135, %r135, 1; - setp.lt.s32 %p42, %r135, %r6; - @%p42 bra $L__BB1_45; - -$L__BB1_55: - cvt.rn.f32.s32 %f380, %r6; - div.rn.f32 %f381, %f436, %f380; - div.rn.f32 %f382, %f437, %f380; - div.rn.f32 %f383, %f438, %f380; - mov.u32 %r130, 0; - // begin inline asm - call (%r129), _optix_get_payload, (%r130); - // end inline asm - mov.u32 %r132, 1; - // begin inline asm - call (%r131), _optix_get_payload, (%r132); - // end inline asm - cvt.u64.u32 %rd155, %r129; - cvt.u64.u32 %rd156, %r131; - bfi.b64 %rd157, %rd155, %rd156, 32, 32; - ld.const.u32 %r133, [optix_params+28]; - cvt.rn.f32.s32 %f384, %r133; - ld.f32 %f385, [%rd157]; - fma.rn.f32 %f386, %f385, %f384, %f381; - ld.f32 %f387, [%rd157+4]; - fma.rn.f32 %f388, %f387, %f384, %f382; - ld.f32 %f389, [%rd157+8]; - fma.rn.f32 %f390, %f389, %f384, %f383; - ld.f32 %f391, [%rd157+12]; - fma.rn.f32 %f392, %f391, %f384, 0f3F800000; - add.s32 %r134, %r133, 1; - cvt.rn.f32.s32 %f393, %r134; - div.rn.f32 %f394, %f386, %f393; - div.rn.f32 %f395, %f388, %f393; - div.rn.f32 %f396, %f390, %f393; - div.rn.f32 %f397, %f392, %f393; - st.f32 [%rd157], %f394; - st.f32 [%rd157+4], %f395; - st.f32 [%rd157+8], %f396; - st.f32 [%rd157+12], %f397; - ret; - -} - // .globl __anyhit__radiance -.visible .entry __anyhit__radiance() -{ - - - - ret; - -} - // .globl __anyhit__shadow -.visible .entry __anyhit__shadow() -{ - - - - ret; - -} - // .globl __miss__radiance -.visible .entry __miss__radiance() -{ - .reg .b32 %r<5>; - .reg .b64 %rd<4>; - - - mov.u32 %r2, 0; - // begin inline asm - call (%r1), _optix_get_payload, (%r2); - // end inline asm - mov.u32 %r4, 1; - // begin inline asm - call (%r3), _optix_get_payload, (%r4); - // end inline asm - cvt.u64.u32 %rd1, %r1; - cvt.u64.u32 %rd2, %r3; - bfi.b64 %rd3, %rd1, %rd2, 32, 32; - st.u32 [%rd3], %r2; - st.u32 [%rd3+4], %r2; - st.u32 [%rd3+8], %r2; - st.u32 [%rd3+12], %r2; - ret; - -} - // .globl __miss__shadow -.visible .entry __miss__shadow() -{ - .reg .b32 %r<3>; - - - mov.u32 %r2, 0; - // begin inline asm - call _optix_set_payload, (%r2, %r2); - // end inline asm - ret; - -} - // .globl __raygen__render_frame -.visible .entry __raygen__render_frame() -{ - .reg .f32 %f<80>; - .reg .b32 %r<90>; - .reg .b64 %rd<6>; - - - // begin inline asm - call (%r1), _optix_get_launch_index_x, (); - // end inline asm - // begin inline asm - call (%r5), _optix_get_launch_index_y, (); - // end inline asm - ld.const.v2.u32 {%r81, %r82}, [optix_params+16]; - mov.u32 %r80, 0; - mov.u32 %r44, 1; - add.s32 %r85, %r82, %r1; - ld.const.u32 %r86, [optix_params+24]; - add.s32 %r87, %r86, %r5; - ld.const.u32 %r88, [optix_params+12]; - cvt.rn.f32.s32 %f10, %r85; - add.f32 %f11, %f10, 0f3F000000; - cvt.rn.f32.s32 %f12, %r88; - div.rn.f32 %f13, %f11, %f12; - cvt.rn.f32.s32 %f14, %r87; - add.f32 %f15, %f14, 0f3F000000; - cvt.rn.f32.s32 %f16, %r81; - div.rn.f32 %f17, %f15, %f16; - fma.rn.f32 %f18, %f13, 0f40000000, 0fBF800000; - fma.rn.f32 %f19, %f17, 0f40000000, 0fBF800000; - ld.const.v2.f32 {%f20, %f21}, [optix_params+240]; - fma.rn.f32 %f24, %f20, %f18, 0f00000000; - mov.f32 %f9, 0f00000000; - fma.rn.f32 %f25, %f21, %f19, %f24; - ld.const.v2.f32 {%f26, %f27}, [optix_params+248]; - add.f32 %f30, %f25, %f26; - add.f32 %f31, %f30, %f27; - ld.const.v2.f32 {%f32, %f33}, [optix_params+256]; - fma.rn.f32 %f36, %f32, %f18, 0f00000000; - fma.rn.f32 %f37, %f33, %f19, %f36; - ld.const.v2.f32 {%f38, %f39}, [optix_params+264]; - add.f32 %f42, %f37, %f38; - add.f32 %f43, %f42, %f39; - ld.const.v2.f32 {%f44, %f45}, [optix_params+272]; - fma.rn.f32 %f48, %f44, %f18, 0f00000000; - fma.rn.f32 %f49, %f45, %f19, %f48; - ld.const.v2.f32 {%f50, %f51}, [optix_params+280]; - add.f32 %f54, %f49, %f50; - add.f32 %f55, %f54, %f51; - ld.const.v2.f32 {%f56, %f57}, [optix_params+288]; - fma.rn.f32 %f60, %f56, %f18, 0f00000000; - fma.rn.f32 %f61, %f57, %f19, %f60; - ld.const.v2.f32 {%f62, %f63}, [optix_params+296]; - add.f32 %f66, %f61, %f62; - add.f32 %f67, %f66, %f63; - div.rn.f32 %f68, %f31, %f67; - div.rn.f32 %f69, %f43, %f67; - div.rn.f32 %f70, %f55, %f67; - ld.const.f32 %f1, [optix_params+228]; - sub.f32 %f71, %f68, %f1; - ld.const.v2.f32 {%f72, %f73}, [optix_params+232]; - sub.f32 %f74, %f69, %f72; - sub.f32 %f75, %f70, %f73; - fma.rn.f32 %f76, %f71, %f71, 0f00000000; - fma.rn.f32 %f77, %f74, %f74, %f76; - fma.rn.f32 %f78, %f75, %f75, %f77; - sqrt.rn.f32 %f79, %f78; - div.rn.f32 %f4, %f71, %f79; - div.rn.f32 %f5, %f74, %f79; - div.rn.f32 %f6, %f75, %f79; - ld.const.u64 %rd2, [optix_params]; - // begin inline asm - call (%r7), _optix_get_launch_dimension_x, (); - // end inline asm - mad.lo.s32 %r89, %r7, %r5, %r1; - mul.wide.u32 %rd3, %r89, 16; - add.s64 %rd4, %rd2, %rd3; - shr.u64 %rd5, %rd4, 32; - cvt.u32.u64 %r49, %rd5; - cvt.u32.u64 %r50, %rd4; - ld.const.u64 %rd1, [optix_params+312]; - mov.f32 %f8, 0f60AD78EC; - mov.u32 %r43, 255; - mov.u32 %r48, 2; - // begin inline asm - call(%r10,%r11,%r12,%r13,%r14,%r15,%r16,%r17,%r18,%r19,%r20,%r21,%r22,%r23,%r24,%r25,%r26,%r27,%r28,%r29,%r30,%r31,%r32,%r33,%r34,%r35,%r36,%r37,%r38,%r39,%r40,%r41),_optix_trace_typed_32,(%r80,%rd1,%f1,%f72,%f73,%f4,%f5,%f6,%f9,%f8,%f9,%r43,%r44,%r80,%r48,%r80,%r48,%r49,%r50,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80,%r80); - // end inline asm - ret; - -} - From bd3524bb8de4ff47dce492991f72d301349e0514 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 26 May 2023 01:32:42 +0200 Subject: [PATCH 0831/1018] che_ptx: display image in debug --- src/gproshan/mesh/che_ptx.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index d7a50b14..ae86ac43 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -68,11 +68,13 @@ void che_ptx::read_file(const std::string & file) VHC[v] = intensity; } + #ifndef NDEBUG CImg img((unsigned char *) VC, 3, n_cols, n_rows); img.permute_axes("zycx"); img.save((file + ".jpg").c_str()); std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); + #endif // NDEBUG } else { From 59cf2c2aa6754b8f3641977e5c7136b382963400 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 4 Jul 2023 16:17:25 +0200 Subject: [PATCH 0832/1018] optix: input ptx file --- include/gproshan/raytracing/optix.h | 2 +- src/gproshan/raytracing/optix.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index 70fb32c5..dc0afed5 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -54,7 +54,7 @@ class optix : public raytracing std::vector tex_data; public: - optix(); + optix(const std::string & ptx = "/src/optix.ptx"); optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index df1cf88b..75a678d9 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -40,7 +40,7 @@ void optix_log(index_t level, const char * tag, const char * message, void *) fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } -optix::optix() +optix::optix(const std::string & ptx) { optixInit(); @@ -68,7 +68,7 @@ optix::optix() optix_pipeline_link_opt.maxTraceDepth = 2; - std::ifstream ptx_is(std::string(GPROSHAN_DIR) + "/src/rt_optix.ptx"); + std::ifstream ptx_is(std::string(GPROSHAN_DIR) + ptx); const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); From e15d6cac890ba885f3173a78b105f378d8bd0740 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 5 Jul 2023 14:44:57 +0200 Subject: [PATCH 0833/1018] optix: adding void pointer in optix_params --- include/gproshan/raytracing/optix_params.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index 0c6d4902..e455e8f7 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -34,6 +34,8 @@ struct launch_params OptixTraversableHandle traversable; scene_data sc; + + void * other = nullptr; }; From 33665ce5d6f279f968e31d86524500df371c1f39 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 10 Jul 2023 17:59:38 +0200 Subject: [PATCH 0834/1018] update imgui v1.89.7 --- include/imgui/imgui.h | 174 +++- include/imgui/imgui_impl_glfw.h | 1 + include/imgui/imgui_impl_opengl3.h | 5 + include/imgui/imgui_impl_opengl3_loader.h | 8 +- include/imgui/imgui_internal.h | 161 +++- src/imgui/imgui.cpp | 1005 ++++++++++++++------- src/imgui/imgui_demo.cpp | 241 +++-- src/imgui/imgui_draw.cpp | 50 +- src/imgui/imgui_impl_glfw.cpp | 75 +- src/imgui/imgui_impl_opengl3.cpp | 70 +- src/imgui/imgui_tables.cpp | 127 ++- src/imgui/imgui_widgets.cpp | 339 ++++--- 12 files changed, 1498 insertions(+), 758 deletions(-) diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index beb4feb1..03e503e6 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,29 +1,31 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (headers) // Help: -// - Read FAQ at http://dearimgui.org/faq +// - Read FAQ at http://dearimgui.com/faq // - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. // Read imgui.cpp for details, links and comments. // Resources: -// - FAQ http://dearimgui.org/faq -// - Homepage & latest https://github.com/ocornut/imgui +// - FAQ http://dearimgui.com/faq +// - Homepage https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/5886 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/6478 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) +// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues // Getting Started? -// - For first-time users having issues compiling/linking/running or issues loading fonts: +// - Read https://github.com/ocornut/imgui/wiki/Getting-Started +// - For first-time users having issues compiling/linking/running/loading fonts: // please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. // Library Version -// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM > 12345') -#define IMGUI_VERSION "1.89.4" -#define IMGUI_VERSION_NUM 18940 +// (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') +#define IMGUI_VERSION "1.89.7" +#define IMGUI_VERSION_NUM 18971 #define IMGUI_HAS_TABLE /* @@ -167,6 +169,7 @@ struct ImGuiViewport; // A Platform Window (always only one in 'ma // In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. enum ImGuiKey : int; // -> enum ImGuiKey // Enum: A key identifier (ImGuiKey_XXX or ImGuiMod_XXX value) +enum ImGuiMouseSource : int; // -> enum ImGuiMouseSource // Enum; A mouse input source identifier (Mouse, TouchScreen, Pen) typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type @@ -255,8 +258,8 @@ struct ImVec2 float x, y; constexpr ImVec2() : x(0.0f), y(0.0f) { } constexpr ImVec2(float _x, float _y) : x(_x), y(_y) { } - float operator[] (size_t idx) const { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. - float& operator[] (size_t idx) { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine. + float& operator[] (size_t idx) { IM_ASSERT(idx == 0 || idx == 1); return ((float*)(void*)(char*)this)[idx]; } // We very rarely use this [] operator, so the assert overhead is fine. + float operator[] (size_t idx) const { IM_ASSERT(idx == 0 || idx == 1); return ((const float*)(const void*)(const char*)this)[idx]; } #ifdef IM_VEC2_CLASS_EXTRA IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2. #endif @@ -460,7 +463,7 @@ namespace ImGui IMGUI_API float GetFrameHeightWithSpacing(); // ~ FontSize + style.FramePadding.y * 2 + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of framed widgets) // ID stack/scopes - // Read the FAQ (docs/FAQ.md or http://dearimgui.org/faq) for more details about how ID are handled in dear imgui. + // Read the FAQ (docs/FAQ.md or http://dearimgui.com/faq) for more details about how ID are handled in dear imgui. // - Those questions are answered and impacted by understanding of the ID stack system: // - "Q: Why is my widget not reacting when I click on it?" // - "Q: How can I have widgets with an empty label?" @@ -664,12 +667,21 @@ namespace ImGui IMGUI_API bool MenuItem(const char* label, const char* shortcut, bool* p_selected, bool enabled = true); // return true when activated + toggle (*p_selected) if p_selected != NULL // Tooltips - // - Tooltip are windows following the mouse. They do not take focus away. - IMGUI_API bool BeginTooltip(); // begin/append a tooltip window. to create full-featured tooltip (with any kind of items). - IMGUI_API void EndTooltip(); // only call EndTooltip() if BeginTooltip() returns true! - IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip, typically use with ImGui::IsItemHovered(). override any previous call to SetTooltip(). + // - Tooltips are windows following the mouse. They do not take focus away. + // - A tooltip window can contain items of any types. SetTooltip() is a shortcut for the 'if (BeginTooltip()) { Text(...); EndTooltip(); }' idiom. + IMGUI_API bool BeginTooltip(); // begin/append a tooltip window. + IMGUI_API void EndTooltip(); // only call EndTooltip() if BeginTooltip()/BeginItemTooltip() returns true! + IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip. Often used after a ImGui::IsItemHovered() check. Override any previous call to SetTooltip(). IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); + // Tooltips: helpers for showing a tooltip when hovering an item + // - BeginItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_Tooltip) && BeginTooltip())' idiom. + // - SetItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_Tooltip)) { SetTooltip(...); }' idiom. + // - Where 'ImGuiHoveredFlags_Tooltip' itself is a shortcut to use 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on active input type. For mouse it defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. + IMGUI_API bool BeginItemTooltip(); // begin/append a tooltip window if preceding item was hovered. + IMGUI_API void SetItemTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip if preceeding item was hovered. override any previous call to SetTooltip(). + IMGUI_API void SetItemTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); + // Popups, Modals // - They block normal mouse hovering detection (and therefore most mouse interactions) behind them. // - If not modal: they can be closed by clicking anywhere outside them, or by pressing ESCAPE. @@ -831,6 +843,9 @@ namespace ImGui IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of a window. IMGUI_API void SetKeyboardFocusHere(int offset = 0); // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget. + // Overlapping mode + IMGUI_API void SetNextItemAllowOverlap(); // allow next item to be overlapped by a subsequent item. Useful with invisible buttons, selectable, treenode covering an area where subsequent items may need to be added. Note that both Selectable() and TreeNode() have dedicated flags doing this. + // Item/Widgets Utilities and Query Functions // - Most of the functions are referring to the previous Item that has been submitted. // - See Demo Window under "Widgets->Querying Status" for an interactive visualization of most of those functions. @@ -851,7 +866,6 @@ namespace ImGui IMGUI_API ImVec2 GetItemRectMin(); // get upper-left bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectMax(); // get lower-right bounding rectangle of the last item (screen space) IMGUI_API ImVec2 GetItemRectSize(); // get size of last item - IMGUI_API void SetItemAllowOverlap(); // allow last item to be overlapped by a subsequent item. sometimes useful with invisible buttons, selectables, etc. to catch unused area. // Viewports // - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. @@ -1025,7 +1039,7 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_None = 0, ImGuiTreeNodeFlags_Selected = 1 << 0, // Draw as selected ImGuiTreeNodeFlags_Framed = 1 << 1, // Draw frame with background (e.g. for CollapsingHeader) - ImGuiTreeNodeFlags_AllowItemOverlap = 1 << 2, // Hit testing to allow subsequent widgets to overlap this one + ImGuiTreeNodeFlags_AllowOverlap = 1 << 2, // Hit testing to allow subsequent widgets to overlap this one ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes) ImGuiTreeNodeFlags_DefaultOpen = 1 << 5, // Default node to be open @@ -1039,6 +1053,10 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 14, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog, + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiTreeNodeFlags_AllowItemOverlap = ImGuiTreeNodeFlags_AllowOverlap, // Renamed in 1.89.7 +#endif }; // Flags for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() functions. @@ -1072,7 +1090,11 @@ enum ImGuiSelectableFlags_ ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Selectable frame can span all columns (text will still fit in current column) ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text - ImGuiSelectableFlags_AllowItemOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one + ImGuiSelectableFlags_AllowOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiSelectableFlags_AllowItemOverlap = ImGuiSelectableFlags_AllowOverlap, // Renamed in 1.89.7 +#endif }; // Flags for ImGui::BeginCombo() @@ -1278,16 +1300,30 @@ enum ImGuiHoveredFlags_ ImGuiHoveredFlags_AllowWhenBlockedByPopup = 1 << 5, // Return true even if a popup window is normally blocking access to this item/window //ImGuiHoveredFlags_AllowWhenBlockedByModal = 1 << 6, // Return true even if a modal popup window is normally blocking access to this item/window. FIXME-TODO: Unavailable yet. ImGuiHoveredFlags_AllowWhenBlockedByActiveItem = 1 << 7, // Return true even if an active item is blocking access to this item/window. Useful for Drag and Drop patterns. - ImGuiHoveredFlags_AllowWhenOverlapped = 1 << 8, // IsItemHovered() only: Return true even if the position is obstructed or overlapped by another window - ImGuiHoveredFlags_AllowWhenDisabled = 1 << 9, // IsItemHovered() only: Return true even if the item is disabled - ImGuiHoveredFlags_NoNavOverride = 1 << 10, // Disable using gamepad/keyboard navigation state when active, always query mouse. + ImGuiHoveredFlags_AllowWhenOverlappedByItem = 1 << 8, // IsItemHovered() only: Return true even if the item uses AllowOverlap mode and is overlapped by another hoverable item. + ImGuiHoveredFlags_AllowWhenOverlappedByWindow = 1 << 9, // IsItemHovered() only: Return true even if the position is obstructed or overlapped by another window. + ImGuiHoveredFlags_AllowWhenDisabled = 1 << 10, // IsItemHovered() only: Return true even if the item is disabled + ImGuiHoveredFlags_NoNavOverride = 1 << 11, // IsItemHovered() only: Disable using gamepad/keyboard navigation state when active, always query mouse + ImGuiHoveredFlags_AllowWhenOverlapped = ImGuiHoveredFlags_AllowWhenOverlappedByItem | ImGuiHoveredFlags_AllowWhenOverlappedByWindow, ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows, - // Hovering delays (for tooltips) - ImGuiHoveredFlags_DelayNormal = 1 << 11, // Return true after io.HoverDelayNormal elapsed (~0.30 sec) - ImGuiHoveredFlags_DelayShort = 1 << 12, // Return true after io.HoverDelayShort elapsed (~0.10 sec) - ImGuiHoveredFlags_NoSharedDelay = 1 << 13, // Disable shared delay system where moving from one item to the next keeps the previous timer for a short time (standard for tooltips with long delays) + // Tooltips mode + // - typically used in IsItemHovered() + SetTooltip() sequence. + // - this is a shortcut to pull flags from 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' where you can reconfigure desired behavior. + // e.g. 'TooltipHoveredFlagsForMouse' defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. + // - for frequently actioned or hovered items providing a tooltip, you want may to use ImGuiHoveredFlags_ForTooltip (stationary + delay) so the tooltip doesn't show too often. + // - for items which main purpose is to be hovered, or items with low affordance, or in less consistent apps, prefer no delay or shorter delay. + ImGuiHoveredFlags_ForTooltip = 1 << 11, // Shortcut for standard flags when using IsItemHovered() + SetTooltip() sequence. + + // (Advanced) Mouse Hovering delays. + // - generally you can use ImGuiHoveredFlags_ForTooltip to use application-standardized flags. + // - use those if you need specific overrides. + ImGuiHoveredFlags_Stationary = 1 << 12, // Require mouse to be stationary for style.HoverStationaryDelay (~0.15 sec) _at least one time_. After this, can move on same item/window. Using the stationary test tends to reduces the need for a long delay. + ImGuiHoveredFlags_DelayNone = 1 << 13, // IsItemHovered() only: Return true immediately (default). As this is the default you generally ignore this. + ImGuiHoveredFlags_DelayShort = 1 << 14, // IsItemHovered() only: Return true after style.HoverDelayShort elapsed (~0.15 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). + ImGuiHoveredFlags_DelayNormal = 1 << 15, // IsItemHovered() only: Return true after style.HoverDelayNormal elapsed (~0.40 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). + ImGuiHoveredFlags_NoSharedDelay = 1 << 16, // IsItemHovered() only: Disable shared delay system where moving from one item to the next keeps the previous timer for a short time (standard for tooltips with long delays) }; // Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload() @@ -1407,7 +1443,7 @@ enum ImGuiKey : int ImGuiKey_KeypadEqual, // Gamepad (some of those are analog values, 0.0f to 1.0f) // NAVIGATION ACTION - // (download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets) + // (download controller mapping PNG/PSD at http://dearimgui.com/controls_sheets) ImGuiKey_GamepadStart, // Menu (Xbox) + (Switch) Start/Options (PS) ImGuiKey_GamepadBack, // View (Xbox) - (Switch) Share (PS) ImGuiKey_GamepadFaceLeft, // X (Xbox) Y (Switch) Square (PS) // Tap: Toggle Menu. Hold: Windowing mode (Focus/Move/Resize windows) @@ -1715,6 +1751,18 @@ enum ImGuiMouseCursor_ ImGuiMouseCursor_COUNT }; +// Enumeration for AddMouseSourceEvent() actual source of Mouse Input data. +// Historically we use "Mouse" terminology everywhere to indicate pointer data, e.g. MousePos, IsMousePressed(), io.AddMousePosEvent() +// But that "Mouse" data can come from different source which occasionally may be useful for application to know about. +// You can submit a change of pointer type using io.AddMouseSourceEvent(). +enum ImGuiMouseSource : int +{ + ImGuiMouseSource_Mouse = 0, // Input is coming from an actual mouse. + ImGuiMouseSource_TouchScreen, // Input is coming from a touch screen (no hovering prior to initial press, less precise initial press aiming, dual-axis wheeling possible). + ImGuiMouseSource_Pen, // Input is coming from a pressure/magnetic pen (often used in conjunction with high-sampling rates). + ImGuiMouseSource_COUNT +}; + // Enumeration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions // Represent a condition. // Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always. @@ -1877,6 +1925,14 @@ struct ImGuiStyle float CircleTessellationMaxError; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. ImVec4 Colors[ImGuiCol_COUNT]; + // Behaviors + // (It is possible to modify those fields mid-frame if specific behavior need it, unlike e.g. configuration fields in ImGuiIO) + float HoverStationaryDelay; // Delay for IsItemHovered(ImGuiHoveredFlags_Stationary). Time required to consider mouse stationary. + float HoverDelayShort; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayShort). Usually used along with HoverStationaryDelay. + float HoverDelayNormal; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayNormal). " + ImGuiHoveredFlags HoverFlagsForTooltipMouse;// Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. + ImGuiHoveredFlags HoverFlagsForTooltipNav; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. + IMGUI_API ImGuiStyle(); IMGUI_API void ScaleAllSizes(float scale_factor); }; @@ -1911,13 +1967,6 @@ struct ImGuiIO float IniSavingRate; // = 5.0f // Minimum time between saving positions/sizes to .ini file, in seconds. const char* IniFilename; // = "imgui.ini" // Path to .ini file (important: default "imgui.ini" is relative to current working dir!). Set NULL to disable automatic .ini loading/saving or if you want to manually call LoadIniSettingsXXX() / SaveIniSettingsXXX() functions. const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). - float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. - float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. - float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging. - float KeyRepeatDelay; // = 0.275f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). - float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds. - float HoverDelayNormal; // = 0.30 sec // Delay on hovering before IsItemHovered(ImGuiHoveredFlags_DelayNormal) returns true. - float HoverDelayShort; // = 0.10 sec // Delay on hovering before IsItemHovered(ImGuiHoveredFlags_DelayShort) returns true. void* UserData; // = NULL // Store your own data. ImFontAtlas*Fonts; // // Font atlas: load, rasterize and pack one or more fonts into a single texture. @@ -1937,13 +1986,32 @@ struct ImGuiIO bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. float ConfigMemoryCompactTimer; // = 60.0f // Timer (in seconds) to free transient windows/tables memory buffers when unused. Set to -1.0f to disable. + // Inputs Behaviors + // (other variables, ones which are expected to be tweaked within UI code, are exposed in ImGuiStyle) + float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds. + float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels. + float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging. + float KeyRepeatDelay; // = 0.275f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.). + float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds. + + //------------------------------------------------------------------ // Debug options - // - tools to test correct Begin/End and BeginChild/EndChild behaviors. - // - presently Begn()/End() and BeginChild()EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() - // this is inconsistent with other BeginXXX functions and create confusion for many users. - // - we expect to update the API eventually. In the meanwhile we provided tools to facilitate checking user-code behavior. - bool ConfigDebugBeginReturnValueOnce; // = false // First-time calls to Begin()/BeginChild() will return false. NEEDS TO BE SET AT APPLICATION BOOT TIME if you don't want to miss windows. - bool ConfigDebugBeginReturnValueLoop; // = false // Some calls to Begin()/BeginChild() will return false. Will cycle through window depths then repeat. Suggested use: add "io.ConfigDebugBeginReturnValue = io.KeyShift" in your main loop then occasionally press SHIFT. Windows should be flickering while running. + //------------------------------------------------------------------ + + // Tools to test correct Begin/End and BeginChild/EndChild behaviors. + // Presently Begin()/End() and BeginChild()/EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() + // This is inconsistent with other BeginXXX functions and create confusion for many users. + // We expect to update the API eventually. In the meanwhile we provide tools to facilitate checking user-code behavior. + bool ConfigDebugBeginReturnValueOnce;// = false // First-time calls to Begin()/BeginChild() will return false. NEEDS TO BE SET AT APPLICATION BOOT TIME if you don't want to miss windows. + bool ConfigDebugBeginReturnValueLoop;// = false // Some calls to Begin()/BeginChild() will return false. Will cycle through window depths then repeat. Suggested use: add "io.ConfigDebugBeginReturnValue = io.KeyShift" in your main loop then occasionally press SHIFT. Windows should be flickering while running. + + // Option to deactivate io.AddFocusEvent(false) handling. May facilitate interactions with a debugger when focus loss leads to clearing inputs data. + // Backends may have other side-effects on focus loss, so this will reduce side-effects but not necessary remove all of them. + // Consider using e.g. Win32's IsDebuggerPresent() as an additional filter (or see ImOsIsDebuggerPresent() in imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version). + bool ConfigDebugIgnoreFocusLoss; // = false // Ignore io.AddFocusEvent(false), consequently not calling io.ClearInputKeys() in input processing. + + // Options to audit .ini data + bool ConfigDebugIniSettings; // = false // Save .ini data with extra comments (particularly helpful for Docking, but makes saving slower) //------------------------------------------------------------------ // Platform Functions @@ -1982,6 +2050,7 @@ struct ImGuiIO IMGUI_API void AddMousePosEvent(float x, float y); // Queue a mouse position update. Use -FLT_MAX,-FLT_MAX to signify no mouse (e.g. app not focused and not hovered) IMGUI_API void AddMouseButtonEvent(int button, bool down); // Queue a mouse button change IMGUI_API void AddMouseWheelEvent(float wheel_x, float wheel_y); // Queue a mouse wheel update. wheel_y<0: scroll down, wheel_y>0: scroll up, wheel_x<0: scroll right, wheel_x>0: scroll left. + IMGUI_API void AddMouseSourceEvent(ImGuiMouseSource source); // Queue a mouse source change (Mouse/TouchScreen/Pen) IMGUI_API void AddFocusEvent(bool focused); // Queue a gain/loss of focus for the application (generally based on OS/platform focus of your window) IMGUI_API void AddInputCharacter(unsigned int c); // Queue a new character input IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue a new character input from a UTF-16 character, it can be a surrogate @@ -2035,6 +2104,7 @@ struct ImGuiIO bool MouseDown[5]; // Mouse buttons: 0=left, 1=right, 2=middle + extras (ImGuiMouseButton_COUNT == 5). Dear ImGui mostly uses left and right buttons. Other buttons allow us to track if the mouse is being used by your application + available to user as a convenience via IsMouse** API. float MouseWheel; // Mouse wheel Vertical: 1 unit scrolls about 5 lines text. >0 scrolls Up, <0 scrolls Down. Hold SHIFT to turn vertical scroll into horizontal scroll. float MouseWheelH; // Mouse wheel Horizontal. >0 scrolls Left, <0 scrolls Right. Most users don't have a mouse with a horizontal wheel, may not be filled by all backends. + ImGuiMouseSource MouseSource; // Mouse actual input peripheral (Mouse/TouchScreen/Pen). bool KeyCtrl; // Keyboard modifier down: Control bool KeyShift; // Keyboard modifier down: Shift bool KeyAlt; // Keyboard modifier down: Alt @@ -2054,6 +2124,7 @@ struct ImGuiIO bool MouseReleased[5]; // Mouse button went from Down to !Down bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. bool MouseDownOwnedUnlessPopupClose[5]; // Track if button was clicked inside a dear imgui window. + bool MouseWheelRequestAxisSwap; // On a non-Mac system, holding SHIFT requests WheelY to perform the equivalent of a WheelX event. On a Mac system this is already enforced by the system. float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) float MouseDownDurationPrev[5]; // Previous time the mouse button has been down float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point (used for moving thresholds) @@ -2324,11 +2395,13 @@ struct ImGuiListClipper IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. - // Call ForceDisplayRangeByIndices() before first call to Step() if you need a range of items to be displayed regardless of visibility. - IMGUI_API void ForceDisplayRangeByIndices(int item_min, int item_max); // item_max is exclusive e.g. use (42, 42+1) to make item 42 always visible BUT due to alignment/padding of certain items it is likely that an extra item may be included on either end of the display range. + // Call IncludeRangeByIndices() *BEFORE* first call to Step() if you need a range of items to not be clipped, regardless of their visibility. + // (Due to alignment / padding of certain items it is possible that an extra item may be included on either end of the display range). + IMGUI_API void IncludeRangeByIndices(int item_begin, int item_end); // item_end is exclusive e.g. use (42, 42+1) to make item 42 never clipped. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79] + inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeRangeByIndices(item_begin, item_end); } // [renamed in 1.89.6] + //inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79] #endif }; @@ -2336,7 +2409,6 @@ struct ImGuiListClipper // - It is important that we are keeping those disabled by default so they don't leak in user space. // - This is in order to allow user enabling implicit cast operators between ImVec2/ImVec4 and their own types (using IM_VEC2_CLASS_EXTRA in imconfig.h) // - You can use '#define IMGUI_DEFINE_MATH_OPERATORS' to import our operators, provided as a courtesy. -// - We unfortunately don't have a unary- operator for ImVec2 because this would needs to be defined inside the class itself. #ifdef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED IM_MSVC_RUNTIME_CHECKS_OFF @@ -2346,6 +2418,7 @@ static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); } +static inline ImVec2 operator-(const ImVec2& lhs) { return ImVec2(-lhs.x, -lhs.y); } static inline ImVec2& operator*=(ImVec2& lhs, const float rhs) { lhs.x *= rhs; lhs.y *= rhs; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const float rhs) { lhs.x /= rhs; lhs.y /= rhs; return lhs; } static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; return lhs; } @@ -2812,7 +2885,8 @@ struct ImFontAtlas //------------------------------------------- // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list) - // NB: Make sure that your string are UTF-8 and NOT in your local code page. In C++11, you can create UTF-8 string literal using the u8"Hello world" syntax. See FAQ for details. + // NB: Make sure that your string are UTF-8 and NOT in your local code page. + // Read https://github.com/ocornut/imgui/blob/master/docs/FONTS.md/#about-utf-8-encoding for details. // NB: Consider using ImFontGlyphRangesBuilder to build glyph ranges from textual data. IMGUI_API const ImWchar* GetGlyphRangesDefault(); // Basic Latin, Extended Latin IMGUI_API const ImWchar* GetGlyphRangesGreek(); // Default + Greek and Coptic @@ -3007,6 +3081,8 @@ namespace ImGui #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.89.7 (from June 2023) + IMGUI_API void SetItemAllowOverlap(); // Use SetNextItemAllowOverlap() before item. // OBSOLETED in 1.89.4 (from March 2023) static inline void PushAllowKeyboardFocus(bool tab_stop) { PushTabStop(tab_stop); } static inline void PopAllowKeyboardFocus() { PopTabStop(); } @@ -3019,12 +3095,12 @@ namespace ImGui IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Calculate coarse clipping for large list of evenly sized items. Prefer using ImGuiListClipper. // OBSOLETED in 1.85 (from August 2021) static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } - // OBSOLETED in 1.81 (from February 2021) - IMGUI_API bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1); // Helper to calculate size from items_count and height_in_items - static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } - static inline void ListBoxFooter() { EndListBox(); } // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) + //-- OBSOLETED in 1.81 (from February 2021) + //static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } + //static inline bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1) { float height = GetTextLineHeightWithSpacing() * ((height_in_items < 0 ? ImMin(items_count, 7) : height_in_items) + 0.25f) + GetStyle().FramePadding.y * 2.0f; return BeginListBox(label, ImVec2(0.0f, height)); } // Helper to calculate size from items_count and height_in_items + //static inline void ListBoxFooter() { EndListBox(); } //-- OBSOLETED in 1.79 (from August 2020) //static inline void OpenPopupContextItem(const char* str_id = NULL, ImGuiMouseButton mb = 1) { OpenPopupOnItemClick(str_id, mb); } // Bool return value removed. Use IsWindowAppearing() in BeginPopup() instead. Renamed in 1.77, renamed back in 1.79. Sorry! //-- OBSOLETED in 1.78 (from June 2020): Old drag/sliders functions that took a 'float power > 1.0f' argument instead of ImGuiSliderFlags_Logarithmic. See github.com/ocornut/imgui/issues/3361 for details. diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index f2f34059..698b0d4b 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -4,6 +4,7 @@ // Implemented features: // [X] Platform: Clipboard support. +// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only). // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). diff --git a/include/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h index 3a95f052..77d11801 100644 --- a/include/imgui/imgui_impl_opengl3.h +++ b/include/imgui/imgui_impl_opengl3.h @@ -7,6 +7,11 @@ // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). +// About WebGL/ES: +// - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES. +// - This is done automatically on iOS, Android and Emscripten targets. +// - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h. + // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 84a5a4a3..7ca72e37 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -154,6 +154,8 @@ typedef khronos_uint8_t GLubyte; #define GL_ONE 1 #define GL_SRC_ALPHA 0x0302 #define GL_ONE_MINUS_SRC_ALPHA 0x0303 +#define GL_FRONT 0x0404 +#define GL_BACK 0x0405 #define GL_FRONT_AND_BACK 0x0408 #define GL_POLYGON_MODE 0x0B40 #define GL_CULL_FACE 0x0B44 @@ -373,6 +375,8 @@ GLAPI void APIENTRY glGenVertexArrays (GLsizei n, GLuint *arrays); typedef struct __GLsync *GLsync; typedef khronos_uint64_t GLuint64; typedef khronos_int64_t GLint64; +#define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002 +#define GL_CONTEXT_PROFILE_MASK 0x9126 typedef void (APIENTRYP PFNGLDRAWELEMENTSBASEVERTEXPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLint basevertex); typedef void (APIENTRYP PFNGLGETINTEGER64I_VPROC) (GLenum target, GLuint index, GLint64 *data); #ifdef GL_GLEXT_PROTOTYPES @@ -692,8 +696,8 @@ static int parse_version(void) if (version.major == 0 && version.minor == 0) { // Query GL_VERSION in desktop GL 2.x, the string will start with "." - const char* gl_version = (const char*)glGetString(GL_VERSION); - sscanf(gl_version, "%d.%d", &version.major, &version.minor); + if (const char* gl_version = (const char*)glGetString(GL_VERSION)) + sscanf(gl_version, "%d.%d", &version.major, &version.minor); } if (version.major < 2) return GL3W_ERROR_OPENGL_VERSION; diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index 20494ab4..30990481 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (internal structures/api) // You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. @@ -130,6 +130,7 @@ struct ImGuiDataVarInfo; // Variable information (e.g. to avoid style struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box +struct ImGuiInputTextDeactivateData;// Short term storage to backup text of a deactivating InputText() while another is stealing active id struct ImGuiLastItemData; // Status storage for last submitted items struct ImGuiLocEntry; // A localization entry. struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only @@ -163,6 +164,7 @@ typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // E // Flags typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later) typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags +typedef int ImGuiFocusRequestFlags; // -> enum ImGuiFocusRequestFlags_ // Flags: for FocusWindow(); typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: for IsKeyPressed(), IsMouseClicked(), SetKeyOwner(), SetItemKeyOwner() etc. typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag(), g.LastItemData.InFlags typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for g.LastItemData.StatusFlags @@ -229,6 +231,7 @@ namespace ImStb #define IMGUI_DEBUG_LOG_FOCUS(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFocus) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_POPUP(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_NAV(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventNav) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_SELECTION(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection)IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) @@ -265,6 +268,8 @@ namespace ImStb #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 #define IM_FLOOR(_VAL) ((float)(int)(_VAL)) // ImFloor() is not inlined in MSVC debug builds #define IM_ROUND(_VAL) ((float)(int)((_VAL) + 0.5f)) // +#define IM_STRINGIFY_HELPER(_X) #_X +#define IM_STRINGIFY(_X) IM_STRINGIFY_HELPER(_X) // Preprocessor idiom to stringify e.g. an integer. // Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall #ifdef _MSC_VER @@ -476,7 +481,6 @@ IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, c IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); inline float ImTriangleArea(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ImFabs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y))) * 0.5f; } -IMGUI_API ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy); // Helper: ImVec1 (1D vector) // (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) @@ -798,6 +802,7 @@ enum ImGuiItemFlags_ ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. ImGuiItemFlags_NoWindowHoverableCheck = 1 << 8, // false // Disable hoverable check in ItemHoverable() + ImGuiItemflags_AllowOverlap = 1 << 9, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. // Controlled by widget code ImGuiItemFlags_Inputable = 1 << 10, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. @@ -829,6 +834,14 @@ enum ImGuiItemStatusFlags_ #endif }; +// Extend ImGuiHoveredFlags_ +enum ImGuiHoveredFlagsPrivate_ +{ + ImGuiHoveredFlags_DelayMask_ = ImGuiHoveredFlags_DelayNone | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay, + ImGuiHoveredFlags_AllowedMaskForIsWindowHovered = ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_NoPopupHierarchy | ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_Stationary, + ImGuiHoveredFlags_AllowedMaskForIsItemHovered = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped | ImGuiHoveredFlags_AllowWhenDisabled | ImGuiHoveredFlags_NoNavOverride | ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayMask_, +}; + // Extend ImGuiInputTextFlags_ enum ImGuiInputTextFlagsPrivate_ { @@ -849,7 +862,7 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_PressedOnDragDropHold = 1 << 9, // return true when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) ImGuiButtonFlags_Repeat = 1 << 10, // hold to repeat ImGuiButtonFlags_FlattenChildren = 1 << 11, // allow interactions even if a child window is overlapping - ImGuiButtonFlags_AllowItemOverlap = 1 << 12, // require previous frame HoveredId to either match id or be null before being usable, use along with SetItemAllowOverlap() + ImGuiButtonFlags_AllowOverlap = 1 << 12, // require previous frame HoveredId to either match id or be null before being usable. ImGuiButtonFlags_DontClosePopups = 1 << 13, // disable automatically closing parent popup on press // [UNUSED] //ImGuiButtonFlags_Disabled = 1 << 14, // disable interactions -> use BeginDisabled() or ImGuiItemFlags_Disabled ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine @@ -894,6 +907,7 @@ enum ImGuiSelectableFlagsPrivate_ enum ImGuiTreeNodeFlagsPrivate_ { ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 20, + ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 21,// (FIXME-WIP) Turn Down arrow into an Up arrow, but reversed trees (#6517) }; enum ImGuiSeparatorFlags_ @@ -901,7 +915,17 @@ enum ImGuiSeparatorFlags_ ImGuiSeparatorFlags_None = 0, ImGuiSeparatorFlags_Horizontal = 1 << 0, // Axis default to current layout type, so generally Horizontal unless e.g. in a menu bar ImGuiSeparatorFlags_Vertical = 1 << 1, - ImGuiSeparatorFlags_SpanAllColumns = 1 << 2, + ImGuiSeparatorFlags_SpanAllColumns = 1 << 2, // Make separator cover all columns of a legacy Columns() set. +}; + +// Flags for FocusWindow(). This is not called ImGuiFocusFlags to avoid confusion with public-facing ImGuiFocusedFlags. +// FIXME: Once we finishing replacing more uses of GetTopMostPopupModal()+IsWindowWithinBeginStackOf() +// and FindBlockingModal() with this, we may want to change the flag to be opt-out instead of opt-in. +enum ImGuiFocusRequestFlags_ +{ + ImGuiFocusRequestFlags_None = 0, + ImGuiFocusRequestFlags_RestoreFocusedChild = 1 << 0, // Find last focused child (if any) and focus it instead. + ImGuiFocusRequestFlags_UnlessBelowModal = 1 << 1, // Do not set focus if the window is below a modal. }; enum ImGuiTextFlags_ @@ -913,7 +937,7 @@ enum ImGuiTextFlags_ enum ImGuiTooltipFlags_ { ImGuiTooltipFlags_None = 0, - ImGuiTooltipFlags_OverridePreviousTooltip = 1 << 0, // Override will clear/ignore previously submitted tooltip (defaults to append) + ImGuiTooltipFlags_OverridePrevious = 1 << 1, // Clear/ignore previously submitted tooltip (defaults to append) }; // FIXME: this is in development, not exposed/functional as a generic feature yet. @@ -1048,6 +1072,15 @@ struct IMGUI_API ImGuiMenuColumns void CalcNextTotalWidth(bool update_offsets); }; +// Internal temporary state for deactivating InputText() instances. +struct IMGUI_API ImGuiInputTextDeactivatedState +{ + ImGuiID ID; // widget id owning the text state (which just got deactivated) + ImVector TextA; // text buffer + + ImGuiInputTextDeactivatedState() { memset(this, 0, sizeof(*this)); } + void ClearFreeMemory() { ID = 0; TextA.clear(); } +}; // Internal state of the currently focused/edited text input box // For a given item ID, access with ImGui::GetInputTextState() struct IMGUI_API ImGuiInputTextState @@ -1147,13 +1180,14 @@ enum ImGuiNextItemDataFlags_ struct ImGuiNextItemData { ImGuiNextItemDataFlags Flags; + ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemflags_AllowOverlap. float Width; // Set by SetNextItemWidth() ImGuiID FocusScopeId; // Set by SetNextItemMultiSelectData() (!= 0 signify value has been set, so it's an alternate version of HasSelectionData, we don't use Flags for this because they are cleared too early. This is mostly used for debugging) ImGuiCond OpenCond; bool OpenVal; // Set by SetNextItemOpen() ImGuiNextItemData() { memset(this, 0, sizeof(*this)); } - inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; } // Also cleared manually by ItemAdd()! + inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! }; // Status storage for the last submitted item @@ -1254,19 +1288,18 @@ enum ImGuiInputEventType enum ImGuiInputSource { ImGuiInputSource_None = 0, - ImGuiInputSource_Mouse, + ImGuiInputSource_Mouse, // Note: may be Mouse or TouchScreen or Pen. See io.MouseSource to distinguish them. ImGuiInputSource_Keyboard, ImGuiInputSource_Gamepad, ImGuiInputSource_Clipboard, // Currently only used by InputText() - ImGuiInputSource_Nav, // Stored in g.ActiveIdSource only ImGuiInputSource_COUNT }; // FIXME: Structures in the union below need to be declared as anonymous unions appears to be an extension? // Using ImVec2() would fail on Clang 'union member 'MousePos' has a non-trivial default constructor' -struct ImGuiInputEventMousePos { float PosX, PosY; }; -struct ImGuiInputEventMouseWheel { float WheelX, WheelY; }; -struct ImGuiInputEventMouseButton { int Button; bool Down; }; +struct ImGuiInputEventMousePos { float PosX, PosY; ImGuiMouseSource MouseSource; }; +struct ImGuiInputEventMouseWheel { float WheelX, WheelY; ImGuiMouseSource MouseSource; }; +struct ImGuiInputEventMouseButton { int Button; bool Down; ImGuiMouseSource MouseSource; }; struct ImGuiInputEventKey { ImGuiKey Key; bool Down; float AnalogValue; }; struct ImGuiInputEventText { unsigned int Char; }; struct ImGuiInputEventAppFocused { bool Focused; }; @@ -1275,6 +1308,7 @@ struct ImGuiInputEvent { ImGuiInputEventType Type; ImGuiInputSource Source; + ImU32 EventId; // Unique, sequential increasing integer to identify an event (if you need to correlate them to other data). union { ImGuiInputEventMousePos MousePos; // if Type == ImGuiInputEventType_MousePos @@ -1383,6 +1417,7 @@ enum ImGuiInputFlags_ // [SECTION] Clipper support //----------------------------------------------------------------------------- +// Note that Max is exclusive, so perhaps should be using a Begin/End convention. struct ImGuiListClipperRange { int Min; @@ -1451,15 +1486,17 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_LoopY = 1 << 1, ImGuiNavMoveFlags_WrapX = 1 << 2, // On failed request, request from opposite side one line down (when NavDir==right) or one line up (when NavDir==left) ImGuiNavMoveFlags_WrapY = 1 << 3, // This is not super useful but provided for completeness + ImGuiNavMoveFlags_WrapMask_ = ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_LoopY | ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_WrapY, ImGuiNavMoveFlags_AllowCurrentNavId = 1 << 4, // Allow scoring and considering the current NavId as a move target candidate. This is used when the move source is offset (e.g. pressing PageDown actually needs to send a Up move request, if we are pressing PageDown from the bottom-most item we need to stay in place) ImGuiNavMoveFlags_AlsoScoreVisibleSet = 1 << 5, // Store alternate result in NavMoveResultLocalVisible that only comprise elements that are already fully visible (used by PageUp/PageDown) ImGuiNavMoveFlags_ScrollToEdgeY = 1 << 6, // Force scrolling to min/max (used by Home/End) // FIXME-NAV: Aim to remove or reword, probably unnecessary ImGuiNavMoveFlags_Forwarded = 1 << 7, ImGuiNavMoveFlags_DebugNoResult = 1 << 8, // Dummy scoring for debug purpose, don't apply result - ImGuiNavMoveFlags_FocusApi = 1 << 9, + ImGuiNavMoveFlags_FocusApi = 1 << 9, // Requests from focus API can land/focus/activate items even if they are marked with _NoTabStop (see NavProcessItemForTabbingRequest() for details) ImGuiNavMoveFlags_Tabbing = 1 << 10, // == Focus + Activate if item is Inputable + DontChangeNavHighlight - ImGuiNavMoveFlags_Activate = 1 << 11, - ImGuiNavMoveFlags_DontSetNavHighlight = 1 << 12, // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_Activate = 1 << 11, // Activate/select target item. + ImGuiNavMoveFlags_NoSelect = 1 << 12, // Don't trigger selection by not setting g.NavJustMovedTo + ImGuiNavMoveFlags_NoSetNavHighlight = 1 << 13, // Do not alter the visible state of keyboard vs mouse nav highlight }; enum ImGuiNavLayer @@ -1630,6 +1667,7 @@ struct ImGuiSettingsHandler // This is experimental and not officially supported, it'll probably fall short of features, if/when it does we may backtrack. enum ImGuiLocKey : int { + ImGuiLocKey_VersionStr, ImGuiLocKey_TableSizeOne, ImGuiLocKey_TableSizeAllFit, ImGuiLocKey_TableSizeAllDefault, @@ -1660,8 +1698,9 @@ enum ImGuiDebugLogFlags_ ImGuiDebugLogFlags_EventPopup = 1 << 2, ImGuiDebugLogFlags_EventNav = 1 << 3, ImGuiDebugLogFlags_EventClipper = 1 << 4, - ImGuiDebugLogFlags_EventIO = 1 << 5, - ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventIO, + ImGuiDebugLogFlags_EventSelection = 1 << 5, + ImGuiDebugLogFlags_EventIO = 1 << 6, + ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO, ImGuiDebugLogFlags_OutputToTTY = 1 << 10, // Also send output to TTY }; @@ -1730,8 +1769,6 @@ struct ImGuiContext bool Initialized; bool FontAtlasOwnedByContext; // IO.Fonts-> is owned by the ImGuiContext and will be destructed along with it. ImGuiIO IO; - ImVector InputEventsQueue; // Input events which will be tricked/written into IO structure. - ImVector InputEventsTrail; // Past input events processed in NewFrame(). This is to allow domain-specific application to access e.g mouse/pen trail. ImGuiStyle Style; ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. @@ -1748,6 +1785,12 @@ struct ImGuiContext bool TestEngineHookItems; // Will call test engine hooks: ImGuiTestEngineHook_ItemAdd(), ImGuiTestEngineHook_ItemInfo(), ImGuiTestEngineHook_Log() void* TestEngine; // Test engine user data + // Inputs + ImVector InputEventsQueue; // Input events which will be trickled/written into IO structure. + ImVector InputEventsTrail; // Past input events processed in NewFrame(). This is to allow domain-specific application to access e.g mouse/pen trail. + ImGuiMouseSource InputEventsNextMouseSource; + ImU32 InputEventsNextEventId; + // Windows state ImVector Windows; // Windows, sorted in display order, back to front ImVector WindowsFocusOrder; // Root windows, sorted in focus order, back to front. @@ -1786,7 +1829,7 @@ struct ImGuiContext bool ActiveIdHasBeenEditedThisFrame; ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; - ImGuiInputSource ActiveIdSource; // Activating with mouse or nav (gamepad/keyboard) + ImGuiInputSource ActiveIdSource; // Activating source: ImGuiInputSource_Mouse OR ImGuiInputSource_Keyboard OR ImGuiInputSource_Gamepad int ActiveIdMouseButton; ImGuiID ActiveIdPreviousFrame; bool ActiveIdPreviousFrameIsAlive; @@ -1842,7 +1885,7 @@ struct ImGuiContext ImGuiKeyChord NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiActivateFlags NavNextActivateFlags; - ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS WILL ONLY BE None or NavGamepad or NavKeyboard. + ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS CAN ONLY BE ImGuiInputSource_Keyboard or ImGuiInputSource_Mouse ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) @@ -1853,8 +1896,7 @@ struct ImGuiContext bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest this is to perform early out in ItemAdd() bool NavInitRequest; // Init request for appearing window to select first item bool NavInitRequestFromMove; - ImGuiID NavInitResultId; // Init request result (first item of the window, or one for which SetItemDefaultFocus() was called) - ImRect NavInitResultRectRel; // Init request result rectangle (relative to parent window) + ImGuiNavItemData NavInitResult; // Init request result (first item of the window, or one for which SetItemDefaultFocus() was called) bool NavMoveSubmitted; // Move request submitted, will process result on next NewFrame() bool NavMoveScoringItems; // Move request submitted, still scoring incoming items bool NavMoveForwardToNextFrame; @@ -1888,7 +1930,6 @@ struct ImGuiContext // Render float DimBgRatio; // 0.0..1.0 animation when fading in a dimming background (for modal window and CTRL+TAB list) - ImGuiMouseCursor MouseCursor; // Drag and Drop bool DragDropActive; @@ -1928,14 +1969,21 @@ struct ImGuiContext ImVector ShrinkWidthBuffer; // Hover Delay system - ImGuiID HoverDelayId; - ImGuiID HoverDelayIdPreviousFrame; - float HoverDelayTimer; // Currently used IsItemHovered(), generally inferred from g.HoveredIdTimer but kept uncleared until clear timer elapse. - float HoverDelayClearTimer; // Currently used IsItemHovered(): grace time before g.TooltipHoverTimer gets cleared. + ImGuiID HoverItemDelayId; + ImGuiID HoverItemDelayIdPreviousFrame; + float HoverItemDelayTimer; // Currently used by IsItemHovered() + float HoverItemDelayClearTimer; // Currently used by IsItemHovered(): grace time before g.TooltipHoverTimer gets cleared. + ImGuiID HoverItemUnlockedStationaryId; // Mouse has once been stationary on this item. Only reset after departing the item. + ImGuiID HoverWindowUnlockedStationaryId; // Mouse has once been stationary on this window. Only reset after departing the window. + + // Mouse state + ImGuiMouseCursor MouseCursor; + float MouseStationaryTimer; // Time the mouse has been stationary (with some loose heuristic) + ImVec2 MouseLastValidPos; // Widget state - ImVec2 MouseLastValidPos; ImGuiInputTextState InputTextState; + ImGuiInputTextDeactivatedState InputTextDeactivatedState; ImFont InputTextPasswordFont; ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets @@ -2031,6 +2079,9 @@ struct ImGuiContext TestEngineHookItems = false; TestEngine = NULL; + InputEventsNextMouseSource = ImGuiMouseSource_Mouse; + InputEventsNextEventId = 1; + WindowsActiveCount = 0; CurrentWindow = NULL; HoveredWindow = NULL; @@ -2080,7 +2131,7 @@ struct ImGuiContext NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; NavJustMovedToKeyMods = ImGuiMod_None; - NavInputSource = ImGuiInputSource_None; + NavInputSource = ImGuiInputSource_Keyboard; NavLayer = ImGuiNavLayer_Main; NavIdIsAlive = false; NavMousePosDirty = false; @@ -2089,7 +2140,6 @@ struct ImGuiContext NavAnyRequest = false; NavInitRequest = false; NavInitRequestFromMove = false; - NavInitResultId = 0; NavMoveSubmitted = false; NavMoveScoringItems = false; NavMoveForwardToNextFrame = false; @@ -2108,7 +2158,6 @@ struct ImGuiContext NavWindowingToggleLayer = false; DimBgRatio = 0.0f; - MouseCursor = ImGuiMouseCursor_Arrow; DragDropActive = DragDropWithinSource = DragDropWithinTarget = false; DragDropSourceFlags = ImGuiDragDropFlags_None; @@ -2128,8 +2177,11 @@ struct ImGuiContext TablesTempDataStacked = 0; CurrentTabBar = NULL; - HoverDelayId = HoverDelayIdPreviousFrame = 0; - HoverDelayTimer = HoverDelayClearTimer = 0.0f; + HoverItemDelayId = HoverItemDelayIdPreviousFrame = HoverItemUnlockedStationaryId = HoverWindowUnlockedStationaryId = 0; + HoverItemDelayTimer = HoverItemDelayClearTimer = 0.0f; + + MouseCursor = ImGuiMouseCursor_Arrow; + MouseStationaryTimer = 0.0f; TempInputId = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; @@ -2206,14 +2258,15 @@ struct IMGUI_API ImGuiWindowTempData ImVec1 Indent; // Indentation / start position from left of window (increased by TreePush/TreePop, etc.) ImVec1 ColumnsOffset; // Offset to the current column (if ColumnsCurrent > 0). FIXME: This and the above should be a stack to allow use cases like Tree->Column->Tree. Need revamp columns API. ImVec1 GroupOffset; - ImVec2 CursorStartPosLossyness;// Record the loss of precision of CursorStartPos due to really large scrolling amount. This is used by clipper to compensentate and fix the most common use case of large scroll area. + ImVec2 CursorStartPosLossyness;// Record the loss of precision of CursorStartPos due to really large scrolling amount. This is used by clipper to compensate and fix the most common use case of large scroll area. // Keyboard/Gamepad navigation ImGuiNavLayer NavLayerCurrent; // Current layer, 0..31 (we currently only use 0..1) short NavLayersActiveMask; // Which layers have been written to (result from previous frame) short NavLayersActiveMaskNext;// Which layers have been written to (accumulator for current frame) + bool NavIsScrollPushableX; // Set when current work location may be scrolled horizontally when moving left / right. This is generally always true UNLESS within a column. bool NavHideHighlightOneFrame; - bool NavHasScroll; // Set when scrolling can be used (ScrollMax > 0.0f) + bool NavWindowHasScrollY; // Set per window when scrolling can be used (== ScrollMax.y > 0.0f) // Miscellaneous bool MenuBarAppending; // FIXME: Remove this @@ -2333,6 +2386,7 @@ struct IMGUI_API ImGuiWindow ImGuiWindow* NavLastChildNavWindow; // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.) ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) ImRect NavRectRel[ImGuiNavLayer_COUNT]; // Reference rectangle, in window relative space + ImVec2 NavPreferredScoringPosRel[ImGuiNavLayer_COUNT]; // Preferred X/Y position updated when moving on a given axis, reset to FLT_MAX. ImGuiID NavRootFocusScopeId; // Focus Scope ID at the time of Begin() int MemoryDrawListIdxCapacity; // Backup of last idx/vtx count, so when waking up the window we can preallocate and avoid iterative alloc/copy @@ -2445,7 +2499,7 @@ struct IMGUI_API ImGuiTabBar typedef ImS16 ImGuiTableColumnIdx; typedef ImU16 ImGuiTableDrawChannelIdx; -// [Internal] sizeof() ~ 104 +// [Internal] sizeof() ~ 112 // We use the terminology "Enabled" to refer to a column that is not Hidden by user/api. // We use the terminology "Clipped" to refer to a column that is out of sight because of scrolling/clipping. // This is in contrast with some user-facing api such as IsItemVisible() / IsRectVisible() which use "Visible" to mean "not clipped". @@ -2491,7 +2545,7 @@ struct ImGuiTableColumn ImU8 SortDirection : 2; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending ImU8 SortDirectionsAvailCount : 2; // Number of available sort directions (0 to 3) ImU8 SortDirectionsAvailMask : 4; // Mask of available sort directions (1-bit each) - ImU8 SortDirectionsAvailList; // Ordered of available sort directions (2-bits each) + ImU8 SortDirectionsAvailList; // Ordered list of available sort directions (2-bits each, total 8-bits) ImGuiTableColumn() { @@ -2526,6 +2580,7 @@ struct ImGuiTableInstanceData }; // FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs, incoming RowData +// sizeof() ~ 580 bytes + heap allocs described in TableBeginInitMemory() struct IMGUI_API ImGuiTable { ImGuiID ID; @@ -2641,6 +2696,7 @@ struct IMGUI_API ImGuiTable // Transient data that are only needed between BeginTable() and EndTable(), those buffers are shared (1 per level of stacked table). // - Accessing those requires chasing an extra pointer so for very frequently used data we leave them in the main table structure. // - We also leave out of this structure data that tend to be particularly useful for debugging/metrics. +// sizeof() ~ 112 bytes. struct IMGUI_API ImGuiTableTempData { int TableIndex; // Index in g.Tables.Buf[] pool @@ -2728,10 +2784,11 @@ namespace ImGui IMGUI_API void SetWindowHiddendAndSkipItemsForCurrentFrame(ImGuiWindow* window); inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } + inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } // Windows: Display Order and Focus Order - IMGUI_API void FocusWindow(ImGuiWindow* window); - IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window); + IMGUI_API void FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags = 0); + IMGUI_API void FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window, ImGuiViewport* filter_viewport, ImGuiFocusRequestFlags flags); IMGUI_API void BringWindowToFocusFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayFront(ImGuiWindow* window); IMGUI_API void BringWindowToDisplayBack(ImGuiWindow* window); @@ -2817,7 +2874,8 @@ namespace ImGui IMGUI_API void ItemSize(const ImVec2& size, float text_baseline_y = -1.0f); inline void ItemSize(const ImRect& bb, float text_baseline_y = -1.0f) { ItemSize(bb.GetSize(), text_baseline_y); } // FIXME: This is a misleading API since we expect CursorPos to be bb.Min. IMGUI_API bool ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb = NULL, ImGuiItemFlags extra_flags = 0); - IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id); + IMGUI_API bool ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flags); + IMGUI_API bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags = 0); IMGUI_API bool IsClippedEx(const ImRect& bb, ImGuiID id); IMGUI_API void SetLastItemData(ImGuiID item_id, ImGuiItemFlags in_flags, ImGuiItemStatusFlags status_flags, const ImRect& item_rect); IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); @@ -2850,6 +2908,7 @@ namespace ImGui IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImGuiWindow* GetTopMostAndVisiblePopupModal(); + IMGUI_API ImGuiWindow* FindBlockingModal(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); @@ -2873,10 +2932,17 @@ namespace ImGui IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); - IMGUI_API void ActivateItem(ImGuiID id); // Remotely activate a button, checkbox, tree node etc. given its unique ID. activation is queued and processed on the next frame when the item is encountered again. + IMGUI_API void NavClearPreferredPosForAxis(ImGuiAxis axis); + IMGUI_API void NavUpdateCurrentWindowIsScrollPushableX(); IMGUI_API void SetNavWindow(ImGuiWindow* window); IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); + // Focus/Activation + // This should be part of a larger set of API: FocusItem(offset = -1), FocusItemByID(id), ActivateItem(offset = -1), ActivateItemByID(id) etc. which are + // much harder to design and implement than expected. I have a couple of private branches on this matter but it's not simple. For now implementing the easy ones. + IMGUI_API void FocusItem(); // Focus last item (no selection/activation). + IMGUI_API void ActivateItemByID(ImGuiID id); // Activate an item by ID (button, checkbox, tree node etc.). Activation is queued and processed on the next frame when the item is encountered again. + // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; } @@ -2887,9 +2953,9 @@ namespace ImGui inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } inline ImGuiKeyChord ConvertShortcutMod(ImGuiKeyChord key_chord) { ImGuiContext& g = *GImGui; IM_ASSERT_PARANOID(key_chord & ImGuiMod_Shortcut); return (key_chord & ~ImGuiMod_Shortcut) | (g.IO.ConfigMacOSXBehaviors ? ImGuiMod_Super : ImGuiMod_Ctrl); } - inline ImGuiKey ConvertSingleModFlagToKey(ImGuiKey key) + inline ImGuiKey ConvertSingleModFlagToKey(ImGuiContext* ctx, ImGuiKey key) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; if (key == ImGuiMod_Ctrl) return ImGuiKey_ReservedForModCtrl; if (key == ImGuiMod_Shift) return ImGuiKey_ReservedForModShift; if (key == ImGuiMod_Alt) return ImGuiKey_ReservedForModAlt; @@ -2898,7 +2964,8 @@ namespace ImGui return key; } - IMGUI_API ImGuiKeyData* GetKeyData(ImGuiKey key); + IMGUI_API ImGuiKeyData* GetKeyData(ImGuiContext* ctx, ImGuiKey key); + inline ImGuiKeyData* GetKeyData(ImGuiKey key) { ImGuiContext& g = *GImGui; return GetKeyData(&g, key); } IMGUI_API void GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size); inline ImGuiKey MouseButtonToKey(ImGuiMouseButton button) { IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); return (ImGuiKey)(ImGuiKey_MouseLeft + button); } IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f); @@ -2922,9 +2989,10 @@ namespace ImGui // Please open a GitHub Issue to submit your usage scenario or if there's a use case you need solved. IMGUI_API ImGuiID GetKeyOwner(ImGuiKey key); IMGUI_API void SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API void SetKeyOwnersForKeyChord(ImGuiKeyChord key, ImGuiID owner_id, ImGuiInputFlags flags = 0); IMGUI_API void SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags = 0); // Set key owner to last item if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. IMGUI_API bool TestKeyOwner(ImGuiKey key, ImGuiID owner_id); // Test that key is either not owned, either owned by 'owner_id' - inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(key); IM_ASSERT(IsNamedKey(key)); return &GImGui->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; } + inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiContext* ctx, ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(ctx, key); IM_ASSERT(IsNamedKey(key)); return &ctx->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; } // [EXPERIMENTAL] High-Level: Input Access functions w/ support for Key/Input Ownership // - Important: legacy IsKeyPressed(ImGuiKey, bool repeat=true) _DEFAULTS_ to repeat, new IsKeyPressed() requires _EXPLICIT_ ImGuiInputFlags_Repeat flag. @@ -3087,7 +3155,7 @@ namespace ImGui IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0); - IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags); + IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags, float thickness = 1.0f); IMGUI_API void SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_width); IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); IMGUI_API bool CheckboxFlags(const char* label, ImU64* flags, ImU64 flags_value); @@ -3132,6 +3200,7 @@ namespace ImGui // InputText IMGUI_API bool InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API void InputTextDeactivateHook(ImGuiID id); IMGUI_API bool TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags); IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min = NULL, const void* p_clamp_max = NULL); inline bool TempInputIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputId == id); } diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index fa8580cd..39fcd707 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,23 +1,25 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (main code and documentation) // Help: -// - Read FAQ at http://dearimgui.org/faq +// - Read FAQ at http://dearimgui.com/faq // - Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. // Read imgui.cpp for details, links and comments. // Resources: -// - FAQ http://dearimgui.org/faq -// - Homepage & latest https://github.com/ocornut/imgui +// - FAQ http://dearimgui.com/faq +// - Homepage https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/5886 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/6478 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) +// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues // Getting Started? -// - For first-time users having issues compiling/linking/running or issues loading fonts: +// - Read https://github.com/ocornut/imgui/wiki/Getting-Started +// - For first-time users having issues compiling/linking/running/loading fonts: // please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. @@ -48,7 +50,7 @@ DOCUMENTATION - HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE - API BREAKING CHANGES (read me when you update!) - FREQUENTLY ASKED QUESTIONS (FAQ) - - Read all answers online: https://www.dearimgui.org/faq, or in docs/FAQ.md (with a Markdown viewer) + - Read all answers online: https://www.dearimgui.com/faq, or in docs/FAQ.md (with a Markdown viewer) CODE (search for "[SECTION]" in the code to find them) @@ -159,7 +161,7 @@ CODE - GAMEPAD CONTROLS - Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. - Particularly useful to use Dear ImGui on a console system (e.g. PlayStation, Switch, Xbox) without a mouse! - - Download controller mapping PNG/PSD at http://dearimgui.org/controls_sheets + - Download controller mapping PNG/PSD at http://dearimgui.com/controls_sheets - Backend support: backend needs to: - Set 'io.BackendFlags |= ImGuiBackendFlags_HasGamepad' + call io.AddKeyEvent/AddKeyAnalogEvent() with ImGuiKey_Gamepad_XXX keys. - For analog values (0.0f to 1.0f), backend is responsible to handling a dead-zone and rescaling inputs accordingly. @@ -286,7 +288,7 @@ CODE // Build and load the texture atlas into a texture // (In the examples/ app this is usually done within the ImGui_ImplXXX_Init() function from one of the demo Renderer) int width, height; - unsigned char* pixels = NULL; + unsigned char* pixels = nullptr; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // At this point you've got the texture data and you need to upload that to your graphic system: @@ -397,7 +399,17 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. - - 2023/03/14 (1.89.4) - commented out redirecting enums/functions names that were marked obsolete two years ago: + - 2023/06/28 (1.89.7) - overlapping items: obsoleted 'SetItemAllowOverlap()' (called after item) in favor of calling 'SetNextItemAllowOverlap()' (called before item). 'SetItemAllowOverlap()' didn't and couldn't work reliably since 1.89 (2022-11-15). + - 2023/06/28 (1.89.7) - overlapping items: renamed 'ImGuiTreeNodeFlags_AllowItemOverlap' to 'ImGuiTreeNodeFlags_AllowOverlap', 'ImGuiSelectableFlags_AllowItemOverlap' to 'ImGuiSelectableFlags_AllowOverlap'. Kept redirecting enums (will obsolete). + - 2023/06/28 (1.89.7) - overlapping items: IsItemHovered() now by default return false when querying an item using AllowOverlap mode which is being overlapped. Use ImGuiHoveredFlags_AllowWhenOverlappedByItem to revert to old behavior. + - 2023/06/20 (1.89.7) - moved io.HoverDelayShort/io.HoverDelayNormal to style.HoverDelayShort/style.HoverDelayNormal. As the fields were added in 1.89 and expected to be left unchanged by most users, or only tweaked once during app initialization, we are exceptionally accepting the breakage. + - 2023/05/30 (1.89.6) - backends: renamed "imgui_impl_sdlrenderer.cpp" to "imgui_impl_sdlrenderer2.cpp" and "imgui_impl_sdlrenderer.h" to "imgui_impl_sdlrenderer2.h". This is in prevision for the future release of SDL3. + - 2023/05/22 (1.89.6) - listbox: commented out obsolete/redirecting functions that were marked obsolete more than two years ago: + - ListBoxHeader() -> use BeginListBox() (note how two variants of ListBoxHeader() existed. Check commented versions in imgui.h for reference) + - ListBoxFooter() -> use EndListBox() + - 2023/05/15 (1.89.6) - clipper: commented out obsolete redirection constructor 'ImGuiListClipper(int items_count, float items_height = -1.0f)' that was marked obsolete in 1.79. Use default constructor + clipper.Begin(). + - 2023/05/15 (1.89.6) - clipper: renamed ImGuiListClipper::ForceDisplayRangeByIndices() to ImGuiListClipper::IncludeRangeByIndices(). + - 2023/03/14 (1.89.4) - commented out redirecting enums/functions names that were marked obsolete two years ago: - ImGuiSliderFlags_ClampOnInput -> use ImGuiSliderFlags_AlwaysClamp - ImGuiInputTextFlags_AlwaysInsertMode -> use ImGuiInputTextFlags_AlwaysOverwrite - ImDrawList::AddBezierCurve() -> use ImDrawList::AddBezierCubic() @@ -783,7 +795,7 @@ CODE ================================ Read all answers online: - https://www.dearimgui.org/faq or https://github.com/ocornut/imgui/blob/master/docs/FAQ.md (same url) + https://www.dearimgui.com/faq or https://github.com/ocornut/imgui/blob/master/docs/FAQ.md (same url) Read all answers locally (with a text editor or ideally a Markdown viewer): docs/FAQ.md Some answers are copied down here to facilitate searching in code. @@ -807,7 +819,7 @@ CODE Q: What is this library called? Q: Which version should I get? >> This library is called "Dear ImGui", please don't call it "ImGui" :) - >> See https://www.dearimgui.org/faq for details. + >> See https://www.dearimgui.com/faq for details. Q&A: Integration ================ @@ -817,14 +829,14 @@ CODE Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application? A: You should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! - >> See https://www.dearimgui.org/faq for a fully detailed answer. You really want to read this. + >> See https://www.dearimgui.com/faq for a fully detailed answer. You really want to read this. Q. How can I enable keyboard controls? Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) Q: I integrated Dear ImGui in my engine and little squares are showing instead of text... Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around... Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries... - >> See https://www.dearimgui.org/faq + >> See https://www.dearimgui.com/faq Q&A: Usage ---------- @@ -838,7 +850,7 @@ CODE Q: How can I use my own math types instead of ImVec2/ImVec4? Q: How can I interact with standard C++ types (such as std::string and std::vector)? Q: How can I display custom shapes? (using low-level ImDrawList API) - >> See https://www.dearimgui.org/faq + >> See https://www.dearimgui.com/faq Q&A: Fonts, Text ================ @@ -848,7 +860,7 @@ CODE Q: How can I easily use icons in my application? Q: How can I load multiple fonts? Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? - >> See https://www.dearimgui.org/faq and https://github.com/ocornut/imgui/edit/master/docs/FONTS.md + >> See https://www.dearimgui.com/faq and https://github.com/ocornut/imgui/edit/master/docs/FONTS.md Q&A: Concerns ============= @@ -857,7 +869,7 @@ CODE Q: Can you create elaborate/serious tools with Dear ImGui? Q: Can you reskin the look of Dear ImGui? Q: Why using C++ (as opposed to C)? - >> See https://www.dearimgui.org/faq + >> See https://www.dearimgui.com/faq Q&A: Community ============== @@ -977,7 +989,6 @@ CODE // Debug options #define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL #define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window -#define IMGUI_DEBUG_INI_SETTINGS 0 // Save additional comments in .ini file (particularly helps for Docking, but makes saving slower) // When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch. static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in @@ -988,6 +999,9 @@ static const float WINDOWS_HOVER_PADDING = 4.0f; // Exten static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 0.70f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. +// Tooltip offset +static const ImVec2 TOOLTIP_DEFAULT_OFFSET = ImVec2(16, 10); // Multiplied by g.Style.MouseCursorScale + //------------------------------------------------------------------------- // [SECTION] FORWARD DECLARATIONS //------------------------------------------------------------------------- @@ -1056,7 +1070,6 @@ static void RenderWindowDecorations(ImGuiWindow* window, const ImRec static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); static void RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 col); static void RenderDimmedBackgrounds(); -static ImGuiWindow* FindBlockingModal(ImGuiWindow* window); // Viewports static void UpdateViewportsNewFrame(); @@ -1155,6 +1168,13 @@ ImGuiStyle::ImGuiStyle() CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. CircleTessellationMaxError = 0.30f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. + // Behaviors + HoverStationaryDelay = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_Stationary). Time required to consider mouse stationary. + HoverDelayShort = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayShort). Usually used along with HoverStationaryDelay. + HoverDelayNormal = 0.40f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayNormal). " + HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. + HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. + // Default theme ImGui::StyleColorsDark(this); } @@ -1203,16 +1223,10 @@ ImGuiIO::ImGuiIO() IniSavingRate = 5.0f; IniFilename = "imgui.ini"; // Important: "imgui.ini" is relative to current working dir, most apps will want to lock this to an absolute path (e.g. same path as executables). LogFilename = "imgui_log.txt"; - MouseDoubleClickTime = 0.30f; - MouseDoubleClickMaxDist = 6.0f; #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO for (int i = 0; i < ImGuiKey_COUNT; i++) KeyMap[i] = -1; #endif - KeyRepeatDelay = 0.275f; - KeyRepeatRate = 0.050f; - HoverDelayNormal = 0.30f; - HoverDelayShort = 0.10f; UserData = NULL; Fonts = NULL; @@ -1221,6 +1235,12 @@ ImGuiIO::ImGuiIO() FontAllowUserScaling = false; DisplayFramebufferScale = ImVec2(1.0f, 1.0f); + MouseDoubleClickTime = 0.30f; + MouseDoubleClickMaxDist = 6.0f; + MouseDragThreshold = 6.0f; + KeyRepeatDelay = 0.275f; + KeyRepeatRate = 0.050f; + // Miscellaneous options MouseDrawCursor = false; #ifdef __APPLE__ @@ -1246,7 +1266,7 @@ ImGuiIO::ImGuiIO() // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); MousePosPrev = ImVec2(-FLT_MAX, -FLT_MAX); - MouseDragThreshold = 6.0f; + MouseSource = ImGuiMouseSource_Mouse; for (int i = 0; i < IM_ARRAYSIZE(MouseDownDuration); i++) MouseDownDuration[i] = MouseDownDurationPrev[i] = -1.0f; for (int i = 0; i < IM_ARRAYSIZE(KeysData); i++) { KeysData[i].DownDuration = KeysData[i].DownDurationPrev = -1.0f; } AppAcceptingEvents = true; @@ -1268,6 +1288,7 @@ void ImGuiIO::AddInputCharacter(unsigned int c) ImGuiInputEvent e; e.Type = ImGuiInputEventType_Text; e.Source = ImGuiInputSource_Keyboard; + e.EventId = g.InputEventsNextEventId++; e.Text.Char = c; g.InputEventsQueue.push_back(e); } @@ -1349,9 +1370,9 @@ void ImGuiIO::ClearInputKeys() MouseWheel = MouseWheelH = 0.0f; } -static ImGuiInputEvent* FindLatestInputEvent(ImGuiInputEventType type, int arg = -1) +static ImGuiInputEvent* FindLatestInputEvent(ImGuiContext* ctx, ImGuiInputEventType type, int arg = -1) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; for (int n = g.InputEventsQueue.Size - 1; n >= 0; n--) { ImGuiInputEvent* e = &g.InputEventsQueue[n]; @@ -1370,6 +1391,8 @@ static ImGuiInputEvent* FindLatestInputEvent(ImGuiInputEventType type, int arg = // - ImGuiKey key: Translated key (as in, generally ImGuiKey_A matches the key end-user would use to emit an 'A' character) // - bool down: Is the key down? use false to signify a key release. // - float analog_value: 0.0f..1.0f +// IMPORTANT: THIS FUNCTION AND OTHER "ADD" GRABS THE CONTEXT FROM OUR INSTANCE. +// WE NEED TO ENSURE THAT ALL FUNCTION CALLS ARE FULLFILLING THIS, WHICH IS WHY GetKeyData() HAS AN EXPLICIT CONTEXT. void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) { //if (e->Down) { IMGUI_DEBUG_LOG_IO("AddKeyEvent() Key='%s' %d, NativeKeycode = %d, NativeScancode = %d\n", ImGui::GetKeyName(e->Key), e->Down, e->NativeKeycode, e->NativeScancode); } @@ -1378,7 +1401,7 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) return; ImGuiContext& g = *Ctx; IM_ASSERT(ImGui::IsNamedKeyOrModKey(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. - IM_ASSERT(!ImGui::IsAliasKey(key)); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. + IM_ASSERT(ImGui::IsAliasKey(key) == false); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. IM_ASSERT(key != ImGuiMod_Shortcut); // We could easily support the translation here but it seems saner to not accept it (TestEngine perform a translation itself) // Verify that backend isn't mixing up using new io.AddKeyEvent() api and old io.KeysDown[] + io.KeyMap[] data. @@ -1393,8 +1416,8 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) BackendUsingLegacyNavInputArray = false; // Filter duplicate (in particular: key mods and gamepad analog values are commonly spammed) - const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_Key, (int)key); - const ImGuiKeyData* key_data = ImGui::GetKeyData(key); + const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_Key, (int)key); + const ImGuiKeyData* key_data = ImGui::GetKeyData(&g, key); const bool latest_key_down = latest_event ? latest_event->Key.Down : key_data->Down; const float latest_key_analog = latest_event ? latest_event->Key.AnalogValue : key_data->AnalogValue; if (latest_key_down == down && latest_key_analog == analog_value) @@ -1404,6 +1427,7 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) ImGuiInputEvent e; e.Type = ImGuiInputEventType_Key; e.Source = ImGui::IsGamepadKey(key) ? ImGuiInputSource_Gamepad : ImGuiInputSource_Keyboard; + e.EventId = g.InputEventsNextEventId++; e.Key.Key = key; e.Key.Down = down; e.Key.AnalogValue = analog_value; @@ -1460,7 +1484,7 @@ void ImGuiIO::AddMousePosEvent(float x, float y) ImVec2 pos((x > -FLT_MAX) ? ImFloorSigned(x) : x, (y > -FLT_MAX) ? ImFloorSigned(y) : y); // Filter duplicate - const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_MousePos); + const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_MousePos); const ImVec2 latest_pos = latest_event ? ImVec2(latest_event->MousePos.PosX, latest_event->MousePos.PosY) : g.IO.MousePos; if (latest_pos.x == pos.x && latest_pos.y == pos.y) return; @@ -1468,8 +1492,10 @@ void ImGuiIO::AddMousePosEvent(float x, float y) ImGuiInputEvent e; e.Type = ImGuiInputEventType_MousePos; e.Source = ImGuiInputSource_Mouse; + e.EventId = g.InputEventsNextEventId++; e.MousePos.PosX = pos.x; e.MousePos.PosY = pos.y; + e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; g.InputEventsQueue.push_back(e); } @@ -1482,7 +1508,7 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) return; // Filter duplicate - const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_MouseButton, (int)mouse_button); + const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_MouseButton, (int)mouse_button); const bool latest_button_down = latest_event ? latest_event->MouseButton.Down : g.IO.MouseDown[mouse_button]; if (latest_button_down == down) return; @@ -1490,8 +1516,10 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) ImGuiInputEvent e; e.Type = ImGuiInputEventType_MouseButton; e.Source = ImGuiInputSource_Mouse; + e.EventId = g.InputEventsNextEventId++; e.MouseButton.Button = mouse_button; e.MouseButton.Down = down; + e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; g.InputEventsQueue.push_back(e); } @@ -1508,24 +1536,36 @@ void ImGuiIO::AddMouseWheelEvent(float wheel_x, float wheel_y) ImGuiInputEvent e; e.Type = ImGuiInputEventType_MouseWheel; e.Source = ImGuiInputSource_Mouse; + e.EventId = g.InputEventsNextEventId++; e.MouseWheel.WheelX = wheel_x; e.MouseWheel.WheelY = wheel_y; + e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; g.InputEventsQueue.push_back(e); } +// This is not a real event, the data is latched in order to be stored in actual Mouse events. +// This is so that duplicate events (e.g. Windows sending extraneous WM_MOUSEMOVE) gets filtered and are not leading to actual source changes. +void ImGuiIO::AddMouseSourceEvent(ImGuiMouseSource source) +{ + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; + g.InputEventsNextMouseSource = source; +} + void ImGuiIO::AddFocusEvent(bool focused) { IM_ASSERT(Ctx != NULL); ImGuiContext& g = *Ctx; // Filter duplicate - const ImGuiInputEvent* latest_event = FindLatestInputEvent(ImGuiInputEventType_Focus); + const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_Focus); const bool latest_focused = latest_event ? latest_event->AppFocused.Focused : !g.IO.AppFocusLost; - if (latest_focused == focused) + if (latest_focused == focused || (ConfigDebugIgnoreFocusLoss && !focused)) return; ImGuiInputEvent e; e.Type = ImGuiInputEventType_Focus; + e.EventId = g.InputEventsNextEventId++; e.AppFocused.Focused = focused; g.InputEventsQueue.push_back(e); } @@ -2725,9 +2765,6 @@ static void ImGuiListClipper_SeekCursorForItem(ImGuiListClipper* clipper, int it ImGuiListClipper::ImGuiListClipper() { memset(this, 0, sizeof(*this)); - Ctx = ImGui::GetCurrentContext(); - IM_ASSERT(Ctx != NULL); - ItemsCount = -1; } ImGuiListClipper::~ImGuiListClipper() @@ -2737,6 +2774,9 @@ ImGuiListClipper::~ImGuiListClipper() void ImGuiListClipper::Begin(int items_count, float items_height) { + if (Ctx == NULL) + Ctx = ImGui::GetCurrentContext(); + ImGuiContext& g = *Ctx; ImGuiWindow* window = g.CurrentWindow; IMGUI_DEBUG_LOG_CLIPPER("Clipper: Begin(%d,%.2f) in '%s'\n", items_count, items_height, window->Name); @@ -2762,10 +2802,10 @@ void ImGuiListClipper::Begin(int items_count, float items_height) void ImGuiListClipper::End() { - ImGuiContext& g = *Ctx; if (ImGuiListClipperData* data = (ImGuiListClipperData*)TempData) { // In theory here we should assert that we are already at the right position, but it seems saner to just seek at the end and not assert/crash the user. + ImGuiContext& g = *Ctx; IMGUI_DEBUG_LOG_CLIPPER("Clipper: End() in '%s'\n", g.CurrentWindow->Name); if (ItemsCount >= 0 && ItemsCount < INT_MAX && DisplayStart >= 0) ImGuiListClipper_SeekCursorForItem(this, ItemsCount); @@ -2783,13 +2823,13 @@ void ImGuiListClipper::End() ItemsCount = -1; } -void ImGuiListClipper::ForceDisplayRangeByIndices(int item_min, int item_max) +void ImGuiListClipper::IncludeRangeByIndices(int item_begin, int item_end) { ImGuiListClipperData* data = (ImGuiListClipperData*)TempData; IM_ASSERT(DisplayStart < 0); // Only allowed after Begin() and if there has not been a specified range yet. - IM_ASSERT(item_min <= item_max); - if (item_min < item_max) - data->Ranges.push_back(ImGuiListClipperRange::FromIndices(item_min, item_max)); + IM_ASSERT(item_begin <= item_end); + if (item_begin < item_end) + data->Ranges.push_back(ImGuiListClipperRange::FromIndices(item_begin, item_end)); } static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) @@ -3234,6 +3274,9 @@ void ImGui::RenderTextWrapped(ImVec2 pos, const char* text, const char* text_end // Default clip_rect uses (pos_min,pos_max) // Handle clipping on CPU immediately (vs typically let the GPU clip the triangles that are overlapping the clipping rectangle edges) +// FIXME-OPT: Since we have or calculate text_size we could coarse clip whole block immediately, especally for text above draw_list->DrawList. +// Effectively as this is called from widget doing their own coarse clipping it's not very valuable presently. Next time function will take +// better advantage of the render function taking size into account for coarse clipping. void ImGui::RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_display_end, const ImVec2* text_size_if_known, const ImVec2& align, const ImRect* clip_rect) { // Perform CPU side clipping for single clipped element to avoid using scissor state @@ -3481,6 +3524,7 @@ void ImGui::DestroyContext(ImGuiContext* ctx) // IMPORTANT: ###xxx suffixes must be same in ALL languages static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { + { ImGuiLocKey_VersionStr, "Dear ImGui " IMGUI_VERSION " (" IM_STRINGIFY(IMGUI_VERSION_NUM) ")" }, { ImGuiLocKey_TableSizeOne, "Size column to fit###SizeOne" }, { ImGuiLocKey_TableSizeAllFit, "Size all columns to fit###SizeAll" }, { ImGuiLocKey_TableSizeAllDefault, "Size all columns to default###SizeAll" }, @@ -3587,6 +3631,7 @@ void ImGui::Shutdown() g.ClipboardHandlerData.clear(); g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); + g.InputTextDeactivatedState.ClearFreeMemory(); g.SettingsWindows.clear(); g.SettingsHandlers.clear(); @@ -3664,6 +3709,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, const char* name) : DrawListInst(NUL DrawList = &DrawListInst; DrawList->_Data = &Ctx->DrawListSharedData; DrawList->_OwnerName = Name; + NavPreferredScoringPosRel[0] = NavPreferredScoringPosRel[1] = ImVec2(FLT_MAX, FLT_MAX); } ImGuiWindow::~ImGuiWindow() @@ -3718,7 +3764,10 @@ static void SetCurrentWindow(ImGuiWindow* window) g.CurrentWindow = window; g.CurrentTable = window && window->DC.CurrentTableIdx != -1 ? g.Tables.GetByIndex(window->DC.CurrentTableIdx) : NULL; if (window) + { g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); + ImGui::NavUpdateCurrentWindowIsScrollPushableX(); + } } void ImGui::GcCompactTransientMiscBuffers() @@ -3759,13 +3808,23 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) { ImGuiContext& g = *GImGui; - // While most behaved code would make an effort to not steal active id during window move/drag operations, - // we at least need to be resilient to it. Cancelling the move is rather aggressive and users of 'master' branch - // may prefer the weird ill-defined half working situation ('docking' did assert), so may need to rework that. - if (g.MovingWindow != NULL && g.ActiveId == g.MovingWindow->MoveId) + // Clear previous active id + if (g.ActiveId != 0) { - IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() cancel MovingWindow\n"); - g.MovingWindow = NULL; + // While most behaved code would make an effort to not steal active id during window move/drag operations, + // we at least need to be resilient to it. Canceling the move is rather aggressive and users of 'master' branch + // may prefer the weird ill-defined half working situation ('docking' did assert), so may need to rework that. + if (g.MovingWindow != NULL && g.ActiveId == g.MovingWindow->MoveId) + { + IMGUI_DEBUG_LOG_ACTIVEID("SetActiveID() cancel MovingWindow\n"); + g.MovingWindow = NULL; + } + + // This could be written in a more general way (e.g associate a hook to ActiveId), + // but since this is currently quite an exception we'll leave it as is. + // One common scenario leading to this is: pressing Key ->NavMoveRequestApplyResult() -> ClearActiveId() + if (g.InputTextState.ID == g.ActiveId) + InputTextDeactivateHook(g.ActiveId); } // Set active id @@ -3791,7 +3850,8 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) if (id) { g.ActiveIdIsAlive = id; - g.ActiveIdSource = (g.NavActivateId == id || g.NavJustMovedToId == id) ? (ImGuiInputSource)ImGuiInputSource_Nav : ImGuiInputSource_Mouse; + g.ActiveIdSource = (g.NavActivateId == id || g.NavJustMovedToId == id) ? g.NavInputSource : ImGuiInputSource_Mouse; + IM_ASSERT(g.ActiveIdSource != ImGuiInputSource_None); } // Clear declaration of inputs claimed by the widget @@ -3839,15 +3899,21 @@ void ImGui::MarkItemEdited(ImGuiID id) // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need to fill the data. ImGuiContext& g = *GImGui; - IM_ASSERT(g.ActiveId == id || g.ActiveId == 0 || g.DragDropActive); - IM_UNUSED(id); // Avoid unused variable warnings when asserts are compiled out. + if (g.ActiveId == id || g.ActiveId == 0) + { + g.ActiveIdHasBeenEditedThisFrame = true; + g.ActiveIdHasBeenEditedBefore = true; + } + + // We accept a MarkItemEdited() on drag and drop targets (see https://github.com/ocornut/imgui/issues/1875#issuecomment-978243343) + // We accept 'ActiveIdPreviousFrame == id' for InputText() returning an edit after it has been taken ActiveId away (#4714) + IM_ASSERT(g.DragDropActive || g.ActiveId == id || g.ActiveId == 0 || g.ActiveIdPreviousFrame == id); + //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id); - g.ActiveIdHasBeenEditedThisFrame = true; - g.ActiveIdHasBeenEditedBefore = true; g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Edited; } -static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags) +bool ImGui::IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flags) { // An active popup disable hovering on other windows (apart from its own children) // FIXME-OPT: This could be cached/stored within the window. @@ -3866,12 +3932,22 @@ static inline bool IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFla // Inhibit hover unless the window is within the stack of our modal/popup if (want_inhibit) - if (!ImGui::IsWindowWithinBeginStackOf(window->RootWindow, focused_root_window)) + if (!IsWindowWithinBeginStackOf(window->RootWindow, focused_root_window)) return false; } return true; } +static inline float CalcDelayFromHoveredFlags(ImGuiHoveredFlags flags) +{ + ImGuiContext& g = *GImGui; + if (flags & ImGuiHoveredFlags_DelayShort) + return g.Style.HoverDelayShort; + if (flags & ImGuiHoveredFlags_DelayNormal) + return g.Style.HoverDelayNormal; + return 0.0f; +} + // This is roughly matching the behavior of internal-facing ItemHoverable() // - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered() // - this should work even for non-interactive items that have no ID, so we cannot use LastItemId @@ -3879,12 +3955,17 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT((flags & ~ImGuiHoveredFlags_AllowedMaskForIsItemHovered) == 0 && "Invalid flags for IsItemHovered()!"); + if (g.NavDisableMouseHover && !g.NavDisableHighlight && !(flags & ImGuiHoveredFlags_NoNavOverride)) { if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; if (!IsItemFocused()) return false; + + if (flags & ImGuiHoveredFlags_ForTooltip) + flags |= g.Style.HoverFlagsForTooltipNav; } else { @@ -3892,6 +3973,10 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) ImGuiItemStatusFlags status_flags = g.LastItemData.StatusFlags; if (!(status_flags & ImGuiItemStatusFlags_HoveredRect)) return false; + + if (flags & ImGuiHoveredFlags_ForTooltip) + flags |= g.Style.HoverFlagsForTooltipMouse; + IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function // Done with rectangle culling so we can perform heavier checks now @@ -3901,12 +3986,13 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // to use IsItemHovered() after EndChild() itself. Until a solution is found I believe reverting to the test from 2017/09/27 is safe since this was // the test that has been running for a long while. if (g.HoveredWindow != window && (status_flags & ImGuiItemStatusFlags_HoveredWindow) == 0) - if ((flags & ImGuiHoveredFlags_AllowWhenOverlapped) == 0) + if ((flags & ImGuiHoveredFlags_AllowWhenOverlappedByWindow) == 0) return false; // Test if another item is active (e.g. being dragged) + const ImGuiID id = g.LastItemData.ID; if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) - if (g.ActiveId != 0 && g.ActiveId != g.LastItemData.ID && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) + if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) return false; // Test if interactions on this window are blocked by an active popup or modal. @@ -3920,48 +4006,60 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // Special handling for calling after Begin() which represent the title bar or tab. // When the window is skipped/collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. - if (g.LastItemData.ID == window->MoveId && window->WriteAccessed) + if (id == window->MoveId && window->WriteAccessed) return false; + + // Test if using AllowOverlap and overlapped + if ((g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap) && id != 0) + if ((flags & ImGuiHoveredFlags_AllowWhenOverlappedByItem) == 0) + if (g.HoveredIdPreviousFrame != g.LastItemData.ID) + return false; } // Handle hover delay // (some ideas: https://www.nngroup.com/articles/timing-exposing-content) - float delay; - if (flags & ImGuiHoveredFlags_DelayNormal) - delay = g.IO.HoverDelayNormal; - else if (flags & ImGuiHoveredFlags_DelayShort) - delay = g.IO.HoverDelayShort; - else - delay = 0.0f; - if (delay > 0.0f) + const float delay = CalcDelayFromHoveredFlags(flags); + if (delay > 0.0f || (flags & ImGuiHoveredFlags_Stationary)) { ImGuiID hover_delay_id = (g.LastItemData.ID != 0) ? g.LastItemData.ID : window->GetIDFromRectangle(g.LastItemData.Rect); - if ((flags & ImGuiHoveredFlags_NoSharedDelay) && (g.HoverDelayIdPreviousFrame != hover_delay_id)) - g.HoverDelayTimer = 0.0f; - g.HoverDelayId = hover_delay_id; - return g.HoverDelayTimer >= delay; + if ((flags & ImGuiHoveredFlags_NoSharedDelay) && (g.HoverItemDelayIdPreviousFrame != hover_delay_id)) + g.HoverItemDelayTimer = 0.0f; + g.HoverItemDelayId = hover_delay_id; + + // When changing hovered item we requires a bit of stationary delay before activating hover timer, + // but once unlocked on a given item we also moving. + //if (g.HoverDelayTimer >= delay && (g.HoverDelayTimer - g.IO.DeltaTime < delay || g.MouseStationaryTimer - g.IO.DeltaTime < g.Style.HoverStationaryDelay)) { IMGUI_DEBUG_LOG("HoverDelayTimer = %f/%f, MouseStationaryTimer = %f\n", g.HoverDelayTimer, delay, g.MouseStationaryTimer); } + if ((flags & ImGuiHoveredFlags_Stationary) != 0 && g.HoverItemUnlockedStationaryId != hover_delay_id) + return false; + + if (g.HoverItemDelayTimer < delay) + return false; } return true; } // Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered(). -bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) +// (this does not rely on LastItemData it can be called from a ButtonBehavior() call not following an ItemAdd() call) +// FIXME-LEGACY: the 'ImGuiItemFlags item_flags' parameter was added on 2023-06-28. +// If you used this ii your legacy/custom widgets code: +// - Commonly: if your ItemHoverable() call comes after an ItemAdd() call: pass 'item_flags = g.LastItemData.InFlags'. +// - Rare: otherwise you may pass 'item_flags = 0' (ImGuiItemFlags_None) unless you want to benefit from special behavior handled by ItemHoverable. +bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flags) { ImGuiContext& g = *GImGui; - if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap) - return false; - ImGuiWindow* window = g.CurrentWindow; if (g.HoveredWindow != window) return false; - if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) - return false; if (!IsMouseHoveringRect(bb.Min, bb.Max)) return false; + if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap) + return false; + if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) + return false; + // Done with rectangle culling so we can perform heavier checks now. - ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); if (!(item_flags & ImGuiItemFlags_NoWindowHoverableCheck) && !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) { g.HoveredIdDisabled = true; @@ -3971,13 +4069,28 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id) // We exceptionally allow this function to be called with id==0 to allow using it for easy high-level // hover test in widgets code. We could also decide to split this function is two. if (id != 0) + { + // Drag source doesn't report as hovered + if (g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover)) + return false; + SetHoveredID(id); + // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. + // This allows using patterns where a later submitted widget overlaps a previous one. Generally perceived as a front-to-back hit-test. + if (item_flags & ImGuiItemflags_AllowOverlap) + { + g.HoveredIdAllowOverlap = true; + if (g.HoveredIdPreviousFrame != id) + return false; + } + } + // When disabled we'll return false but still set HoveredId if (item_flags & ImGuiItemFlags_Disabled) { // Release active id if turning disabled - if (g.ActiveId == id) + if (g.ActiveId == id && id != 0) ClearActiveID(); g.HoveredIdDisabled = true; return false; @@ -4251,10 +4364,10 @@ void ImGui::UpdateMouseMovingWindowEndFrame() if (g.HoveredIdDisabled) g.MovingWindow = NULL; } - else if (root_window == NULL && g.NavWindow != NULL && GetTopMostPopupModal() == NULL) + else if (root_window == NULL && g.NavWindow != NULL) { // Clicking on void disable focus - FocusWindow(NULL); + FocusWindow(NULL, ImGuiFocusRequestFlags_UnlessBelowModal); } } @@ -4483,21 +4596,33 @@ void ImGui::NewFrame() } #endif + // Record when we have been stationary as this state is preserved while over same item. + // FIXME: The way this is expressed means user cannot alter HoverStationaryDelay during the frame to use varying values. + // To allow this we should store HoverItemMaxStationaryTime+ID and perform the >= check in IsItemHovered() function. + if (g.HoverItemDelayId != 0 && g.MouseStationaryTimer >= g.Style.HoverStationaryDelay) + g.HoverItemUnlockedStationaryId = g.HoverItemDelayId; + else if (g.HoverItemDelayId == 0) + g.HoverItemUnlockedStationaryId = 0; + if (g.HoveredWindow != NULL && g.MouseStationaryTimer >= g.Style.HoverStationaryDelay) + g.HoverWindowUnlockedStationaryId = g.HoveredWindow->ID; + else if (g.HoveredWindow == NULL) + g.HoverWindowUnlockedStationaryId = 0; + // Update hover delay for IsItemHovered() with delays and tooltips - g.HoverDelayIdPreviousFrame = g.HoverDelayId; - if (g.HoverDelayId != 0) + g.HoverItemDelayIdPreviousFrame = g.HoverItemDelayId; + if (g.HoverItemDelayId != 0) { - //if (g.IO.MouseDelta.x == 0.0f && g.IO.MouseDelta.y == 0.0f) // Need design/flags - g.HoverDelayTimer += g.IO.DeltaTime; - g.HoverDelayClearTimer = 0.0f; - g.HoverDelayId = 0; + g.HoverItemDelayTimer += g.IO.DeltaTime; + g.HoverItemDelayClearTimer = 0.0f; + g.HoverItemDelayId = 0; } - else if (g.HoverDelayTimer > 0.0f) + else if (g.HoverItemDelayTimer > 0.0f) { // This gives a little bit of leeway before clearing the hover timer, allowing mouse to cross gaps - g.HoverDelayClearTimer += g.IO.DeltaTime; - if (g.HoverDelayClearTimer >= ImMax(0.20f, g.IO.DeltaTime * 2.0f)) // ~6 frames at 30 Hz + allow for low framerate - g.HoverDelayTimer = g.HoverDelayClearTimer = 0.0f; // May want a decaying timer, in which case need to clamp at max first, based on max of caller last requested timer. + // We could expose 0.25f as style.HoverClearDelay but I am not sure of the logic yet, this is particularly subtle. + g.HoverItemDelayClearTimer += g.IO.DeltaTime; + if (g.HoverItemDelayClearTimer >= ImMax(0.25f, g.IO.DeltaTime * 2.0f)) // ~7 frames at 30 Hz + allow for low framerate + g.HoverItemDelayTimer = g.HoverItemDelayClearTimer = 0.0f; // May want a decaying timer, in which case need to clamp at max first, based on max of caller last requested timer. } // Drag and drop @@ -4579,7 +4704,7 @@ void ImGui::NewFrame() // Closing the focused window restore focus to the first active root window in descending z-order if (g.NavWindow && !g.NavWindow->WasActive) - FocusTopMostWindowUnderOne(NULL, NULL); + FocusTopMostWindowUnderOne(NULL, NULL, NULL, ImGuiFocusRequestFlags_RestoreFocusedChild); // No window should be open at the beginning of the frame. // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. @@ -4865,7 +4990,7 @@ void ImGui::EndFrame() ImGuiPlatformImeData* ime_data = &g.PlatformImeData; if (g.IO.SetPlatformImeDataFn && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) { - IMGUI_DEBUG_LOG_IO("Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); + IMGUI_DEBUG_LOG_IO("[io] Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); ImGuiViewport* viewport = GetMainViewport(); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS if (viewport->PlatformHandleRaw == NULL && g.IO.ImeWindowHandle != NULL) @@ -5183,17 +5308,28 @@ bool ImGui::IsItemEdited() return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Edited) != 0; } +// Allow next item to be overlapped by subsequent items. +// This works by requiring HoveredId to match for two subsequent frames, +// so if a following items overwrite it our interactions will naturally be disabled. +void ImGui::SetNextItemAllowOverlap() +{ + ImGuiContext& g = *GImGui; + g.NextItemData.ItemFlags |= ImGuiItemflags_AllowOverlap; +} + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. -// FIXME: Although this is exposed, its interaction and ideal idiom with using ImGuiButtonFlags_AllowItemOverlap flag are extremely confusing, need rework. +// FIXME-LEGACY: Use SetNextItemAllowOverlap() *before* your item instead. void ImGui::SetItemAllowOverlap() { ImGuiContext& g = *GImGui; ImGuiID id = g.LastItemData.ID; if (g.HoveredId == id) g.HoveredIdAllowOverlap = true; - if (g.ActiveId == id) + if (g.ActiveId == id) // Before we made this obsolete, most calls to SetItemAllowOverlap() used to avoid this path by testing g.ActiveId != id. g.ActiveIdAllowOverlap = true; } +#endif // FIXME: It might be undesirable that this will likely disable KeyOwner-aware shortcuts systems. Consider a more fine-tuned version for the two users of this function. void ImGui::SetActiveIdUsingAllKeyboardKeys() @@ -5270,12 +5406,16 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b parent_window->DC.CursorPos = child_window->Pos; // Process navigation-in immediately so NavInit can run on first frame - if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavHasScroll)) + // Can enter a child if (A) it has navigatable items or (B) it can be scrolled. + const ImGuiID temp_id_for_activation = ImHashStr("##Child", 0, id); + if (g.ActiveId == temp_id_for_activation) + ClearActiveID(); + if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY)) { FocusWindow(child_window); NavInitWindow(child_window, false); - SetActiveID(id + 1, child_window); // Steal ActiveId with another arbitrary id so that key-press won't activate child item - g.ActiveIdSource = ImGuiInputSource_Nav; + SetActiveID(temp_id_for_activation, child_window); // Steal ActiveId with another arbitrary id so that key-press won't activate child item + g.ActiveIdSource = g.NavInputSource; } return ret; } @@ -5317,7 +5457,7 @@ void ImGui::EndChild() ImGuiWindow* parent_window = g.CurrentWindow; ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz); ItemSize(sz); - if ((window->DC.NavLayersActiveMask != 0 || window->DC.NavHasScroll) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) + if ((window->DC.NavLayersActiveMask != 0 || window->DC.NavWindowHasScrollY) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) { ItemAdd(bb, window->ChildId); RenderNavHighlight(bb, window->ChildId); @@ -5330,6 +5470,10 @@ void ImGui::EndChild() { // Not navigable into ItemAdd(bb, 0); + + // But when flattened we directly reach items, adjust active layer mask accordingly + if (window->Flags & ImGuiWindowFlags_NavFlattened) + parent_window->DC.NavLayersActiveMaskNext |= window->DC.NavLayersActiveMaskNext; } if (g.HoveredWindow == window) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredWindow; @@ -5670,6 +5814,11 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s const float grip_hover_inner_size = IM_FLOOR(grip_draw_size * 0.75f); const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_HOVER_PADDING : 0.0f; + ImRect clamp_rect = visibility_rect; + const bool window_move_from_title_bar = g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar); + if (window_move_from_title_bar) + clamp_rect.Min.y -= window->TitleBarHeight(); + ImVec2 pos_target(FLT_MAX, FLT_MAX); ImVec2 size_target(FLT_MAX, FLT_MAX); @@ -5706,8 +5855,8 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s { // Resize from any of the four corners // We don't use an incremental MouseDelta but rather compute an absolute target size based on mouse position - ImVec2 clamp_min = ImVec2(def.CornerPosN.x == 1.0f ? visibility_rect.Min.x : -FLT_MAX, def.CornerPosN.y == 1.0f ? visibility_rect.Min.y : -FLT_MAX); - ImVec2 clamp_max = ImVec2(def.CornerPosN.x == 0.0f ? visibility_rect.Max.x : +FLT_MAX, def.CornerPosN.y == 0.0f ? visibility_rect.Max.y : +FLT_MAX); + ImVec2 clamp_min = ImVec2(def.CornerPosN.x == 1.0f ? clamp_rect.Min.x : -FLT_MAX, (def.CornerPosN.y == 1.0f || (def.CornerPosN.y == 0.0f && window_move_from_title_bar)) ? clamp_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max = ImVec2(def.CornerPosN.x == 0.0f ? clamp_rect.Max.x : +FLT_MAX, def.CornerPosN.y == 0.0f ? clamp_rect.Max.y : +FLT_MAX); ImVec2 corner_target = g.IO.MousePos - g.ActiveIdClickOffset + ImLerp(def.InnerDir * grip_hover_outer_size, def.InnerDir * -grip_hover_inner_size, def.CornerPosN); // Corner of the window corresponding to our corner grip corner_target = ImClamp(corner_target, clamp_min, clamp_max); CalcResizePosSizeFromAnyCorner(window, corner_target, def.CornerPosN, &pos_target, &size_target); @@ -5736,8 +5885,8 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s } if (held) { - ImVec2 clamp_min(border_n == ImGuiDir_Right ? visibility_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down ? visibility_rect.Min.y : -FLT_MAX); - ImVec2 clamp_max(border_n == ImGuiDir_Left ? visibility_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? visibility_rect.Max.y : +FLT_MAX); + ImVec2 clamp_min(border_n == ImGuiDir_Right ? clamp_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down || (border_n == ImGuiDir_Up && window_move_from_title_bar) ? clamp_rect.Min.y : -FLT_MAX); + ImVec2 clamp_max(border_n == ImGuiDir_Left ? clamp_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? clamp_rect.Max.y : +FLT_MAX); ImVec2 border_target = window->Pos; border_target[axis] = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING; border_target = ImClamp(border_target, clamp_min, clamp_max); @@ -5764,7 +5913,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s const float NAV_RESIZE_SPEED = 600.0f; const float resize_step = NAV_RESIZE_SPEED * g.IO.DeltaTime * ImMin(g.IO.DisplayFramebufferScale.x, g.IO.DisplayFramebufferScale.y); g.NavWindowingAccumDeltaSize += nav_resize_dir * resize_step; - g.NavWindowingAccumDeltaSize = ImMax(g.NavWindowingAccumDeltaSize, visibility_rect.Min - window->Pos - window->Size); // We need Pos+Size >= visibility_rect.Min, so Size >= visibility_rect.Min - Pos, so size_delta >= visibility_rect.Min - window->Pos - window->Size + g.NavWindowingAccumDeltaSize = ImMax(g.NavWindowingAccumDeltaSize, clamp_rect.Min - window->Pos - window->Size); // We need Pos+Size >= clmap_rect.Min, so Size >= clmap_rect.Min - Pos, so size_delta >= clmap_rect.Min - window->Pos - window->Size g.NavWindowingToggleLayer = false; g.NavDisableMouseHover = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); @@ -6025,31 +6174,35 @@ void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags // When a modal popup is open, newly created windows that want focus (i.e. are not popups and do not specify ImGuiWindowFlags_NoFocusOnAppearing) // should be positioned behind that modal window, unless the window was created inside the modal begin-stack. // In case of multiple stacked modals newly created window honors begin stack order and does not go below its own modal parent. -// - Window // FindBlockingModal() returns Modal1 -// - Window // .. returns Modal1 +// - WindowA // FindBlockingModal() returns Modal1 +// - WindowB // .. returns Modal1 // - Modal1 // .. returns Modal2 -// - Window // .. returns Modal2 -// - Window // .. returns Modal2 +// - WindowC // .. returns Modal2 +// - WindowD // .. returns Modal2 // - Modal2 // .. returns Modal2 -static ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) +// - WindowE // .. returns NULL +// Notes: +// - FindBlockingModal(NULL) == NULL is generally equivalent to GetTopMostPopupModal() == NULL. +// Only difference is here we check for ->Active/WasActive but it may be unecessary. +ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) { ImGuiContext& g = *GImGui; if (g.OpenPopupStack.Size <= 0) return NULL; // Find a modal that has common parent with specified window. Specified window should be positioned behind that modal. - for (int i = g.OpenPopupStack.Size - 1; i >= 0; i--) + for (int i = 0; i < g.OpenPopupStack.Size; i++) { ImGuiWindow* popup_window = g.OpenPopupStack.Data[i].Window; if (popup_window == NULL || !(popup_window->Flags & ImGuiWindowFlags_Modal)) continue; if (!popup_window->Active && !popup_window->WasActive) // Check WasActive, because this code may run before popup renders on current frame, also check Active to handle newly created windows. continue; - if (IsWindowWithinBeginStackOf(window, popup_window)) // Window is rendered over last modal, no render order change needed. - break; - for (ImGuiWindow* parent = popup_window->ParentWindowInBeginStack->RootWindow; parent != NULL; parent = parent->ParentWindowInBeginStack->RootWindow) - if (IsWindowWithinBeginStackOf(window, parent)) - return popup_window; // Place window above its begin stack parent. + if (window == NULL) // FindBlockingModal(NULL) test for if FocusWindow(NULL) is naturally possible via a mouse click. + return popup_window; + if (IsWindowWithinBeginStackOf(window, popup_window)) // Window may be over modal + continue; + return popup_window; // Place window right below first block modal } return NULL; } @@ -6419,22 +6572,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) want_focus = true; else if ((flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) == 0) want_focus = true; - - ImGuiWindow* modal = GetTopMostPopupModal(); - if (modal != NULL && !IsWindowWithinBeginStackOf(window, modal)) - { - // Avoid focusing a window that is created outside of active modal. This will prevent active modal from being closed. - // Since window is not focused it would reappear at the same display position like the last time it was visible. - // In case of completely new windows it would go to the top (over current modal), but input to such window would still be blocked by modal. - // Position window behind a modal that is not a begin-parent of this window. - want_focus = false; - if (window == window->RootWindow) - { - ImGuiWindow* blocking_modal = FindBlockingModal(window); - IM_ASSERT(blocking_modal != NULL); - BringWindowToDisplayBehind(window, blocking_modal); - } - } } // [Test Engine] Register whole window in the item system (before submitting further decorations) @@ -6561,8 +6698,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // - We disable this when the parent window has zero vertices, which is a common pattern leading to laying out multiple overlapping childs ImGuiWindow* previous_child = parent_window->DC.ChildWindows.Size >= 2 ? parent_window->DC.ChildWindows[parent_window->DC.ChildWindows.Size - 2] : NULL; bool previous_child_overlapping = previous_child ? previous_child->Rect().Overlaps(window->Rect()) : false; - bool parent_is_empty = parent_window->DrawList->VtxBuffer.Size > 0; - if (window->DrawList->CmdBuffer.back().ElemCount == 0 && parent_is_empty && !previous_child_overlapping) + bool parent_is_empty = (parent_window->DrawList->VtxBuffer.Size == 0); + if (window->DrawList->CmdBuffer.back().ElemCount == 0 && !parent_is_empty && !previous_child_overlapping) render_decorations_in_parent = true; } if (render_decorations_in_parent) @@ -6627,8 +6764,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.NavLayersActiveMask = window->DC.NavLayersActiveMaskNext; window->DC.NavLayersActiveMaskNext = 0x00; + window->DC.NavIsScrollPushableX = true; window->DC.NavHideHighlightOneFrame = false; - window->DC.NavHasScroll = (window->ScrollMax.y > 0.0f); + window->DC.NavWindowHasScrollY = (window->ScrollMax.y > 0.0f); window->DC.MenuBarAppending = false; window->DC.MenuColumns.Update(style.ItemSpacing.x, window_just_activated_by_user); @@ -6651,11 +6789,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->AutoFitFramesY--; // Apply focus (we need to call FocusWindow() AFTER setting DC.CursorStartPos so our initial navigation reference rectangle can start around there) + // We ImGuiFocusRequestFlags_UnlessBelowModal to: + // - Avoid focusing a window that is created outside of a modal. This will prevent active modal from being closed. + // - Position window behind the modal that is not a begin-parent of this window. if (want_focus) - { - FocusWindow(window); + FocusWindow(window, ImGuiFocusRequestFlags_UnlessBelowModal); + if (want_focus && window == g.NavWindow) NavInitWindow(window, false); // <-- this is in the way for us to be able to defer and sort reappearing FocusWindow() calls - } // Title bar if (!(flags & ImGuiWindowFlags_NoTitleBar)) @@ -6881,10 +7021,25 @@ int ImGui::FindWindowDisplayIndex(ImGuiWindow* window) } // Moving window to front of display and set focus (which happens to be back of our sorted list) -void ImGui::FocusWindow(ImGuiWindow* window) +void ImGui::FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags) { ImGuiContext& g = *GImGui; + // Modal check? + if ((flags & ImGuiFocusRequestFlags_UnlessBelowModal) && (g.NavWindow != window)) // Early out in common case. + if (ImGuiWindow* blocking_modal = FindBlockingModal(window)) + { + IMGUI_DEBUG_LOG_FOCUS("[focus] FocusWindow(\"%s\", UnlessBelowModal): prevented by \"%s\".\n", window ? window->Name : "", blocking_modal->Name); + if (window && window == window->RootWindow && (window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus) == 0) + BringWindowToDisplayBehind(window, blocking_modal); // Still bring to right below modal. + return; + } + + // Find last focused child (if any) and focus it instead. + if ((flags & ImGuiFocusRequestFlags_RestoreFocusedChild) && window != NULL) + window = NavRestoreLastChildNavWindow(window); + + // Apply focus if (g.NavWindow != window) { SetNavWindow(window); @@ -6921,9 +7076,10 @@ void ImGui::FocusWindow(ImGuiWindow* window) BringWindowToDisplayFront(display_front_window); } -void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window) +void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWindow* ignore_window, ImGuiViewport* filter_viewport, ImGuiFocusRequestFlags flags) { ImGuiContext& g = *GImGui; + IM_UNUSED(filter_viewport); // Unused in master branch. int start_idx = g.WindowsFocusOrder.Size - 1; if (under_this_window != NULL) { @@ -6941,15 +7097,15 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user. ImGuiWindow* window = g.WindowsFocusOrder[i]; IM_ASSERT(window == window->RootWindow); - if (window != ignore_window && window->WasActive) - if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) - { - ImGuiWindow* focus_window = NavRestoreLastChildNavWindow(window); - FocusWindow(focus_window); - return; - } + if (window == ignore_window || !window->WasActive) + continue; + if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) + { + FocusWindow(window, flags); + return; + } } - FocusWindow(NULL); + FocusWindow(NULL, flags); } // Important: this alone doesn't alter current ImDrawList state. This is called by PushFont/PopFont only. @@ -7140,7 +7296,8 @@ bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_b bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { - IM_ASSERT((flags & (ImGuiHoveredFlags_AllowWhenOverlapped | ImGuiHoveredFlags_AllowWhenDisabled)) == 0); // Flags not supported by this function + IM_ASSERT((flags & ~ImGuiHoveredFlags_AllowedMaskForIsWindowHovered) == 0 && "Invalid flags for IsWindowHovered()!"); + ImGuiContext& g = *GImGui; ImGuiWindow* ref_window = g.HoveredWindow; ImGuiWindow* cur_window = g.CurrentWindow; @@ -7168,6 +7325,17 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) if (!(flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) if (g.ActiveId != 0 && !g.ActiveIdAllowOverlap && g.ActiveId != ref_window->MoveId) return false; + + // When changing hovered window we requires a bit of stationary delay before activating hover timer. + // FIXME: We don't support delay other than stationary one for now, other delay would need a way + // to fullfill the possibility that multiple IsWindowHovered() with varying flag could return true + // for different windows of the hierarchy. Possibly need a Hash(Current+Flags) ==> (Timer) cache. + // We can implement this for _Stationary because the data is linked to HoveredWindow rather than CurrentWindow. + if (flags & ImGuiHoveredFlags_ForTooltip) + flags |= g.Style.HoverFlagsForTooltipMouse; + if ((flags & ImGuiHoveredFlags_Stationary) != 0 && g.HoverWindowUnlockedStationaryId != ref_window->ID) + return false; + return true; } @@ -7458,13 +7626,6 @@ void ImGui::SetWindowFontScale(float scale) g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); } -void ImGui::ActivateItem(ImGuiID id) -{ - ImGuiContext& g = *GImGui; - g.NavNextActivateId = id; - g.NavNextActivateFlags = ImGuiActivateFlags_None; -} - void ImGui::PushFocusScope(ImGuiID id) { ImGuiContext& g = *GImGui; @@ -7480,13 +7641,40 @@ void ImGui::PopFocusScope() g.CurrentFocusScopeId = g.FocusScopeStack.Size ? g.FocusScopeStack.back() : 0; } +// Focus = move navigation cursor, set scrolling, set focus window. +void ImGui::FocusItem() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + IMGUI_DEBUG_LOG_FOCUS("FocusItem(0x%08x) in window \"%s\"\n", g.LastItemData.ID, window->Name); + if (g.DragDropActive || g.MovingWindow != NULL) // FIXME: Opt-in flags for this? + { + IMGUI_DEBUG_LOG_FOCUS("FocusItem() ignored while DragDropActive!\n"); + return; + } + + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSelect; + ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; + SetNavWindow(window); + NavMoveRequestSubmit(ImGuiDir_None, ImGuiDir_Up, move_flags, scroll_flags); + NavMoveRequestResolveWithLastItem(&g.NavMoveResultLocal); +} + +void ImGui::ActivateItemByID(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.NavNextActivateId = id; + g.NavNextActivateFlags = ImGuiActivateFlags_None; +} + // Note: this will likely be called ActivateItem() once we rework our Focus/Activation system! +// But ActivateItem() should function without altering scroll/focus? void ImGui::SetKeyboardFocusHere(int offset) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; IM_ASSERT(offset >= -1); // -1 is allowed but not below - IMGUI_DEBUG_LOG_ACTIVEID("SetKeyboardFocusHere(%d) in window \"%s\"\n", offset, window->Name); + IMGUI_DEBUG_LOG_FOCUS("SetKeyboardFocusHere(%d) in window \"%s\"\n", offset, window->Name); // It makes sense in the vast majority of cases to never interrupt a drag and drop. // When we refactor this function into ActivateItem() we may want to make this an option. @@ -7494,14 +7682,15 @@ void ImGui::SetKeyboardFocusHere(int offset) // is also automatically dropped in the event g.ActiveId is stolen. if (g.DragDropActive || g.MovingWindow != NULL) { - IMGUI_DEBUG_LOG_ACTIVEID("SetKeyboardFocusHere() ignored while DragDropActive!\n"); + IMGUI_DEBUG_LOG_FOCUS("SetKeyboardFocusHere() ignored while DragDropActive!\n"); return; } SetNavWindow(window); + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_Activate | ImGuiNavMoveFlags_FocusApi; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; - NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, move_flags, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. if (offset == -1) { NavMoveRequestResolveWithLastItem(&g.NavMoveResultLocal); @@ -7519,12 +7708,11 @@ void ImGui::SetItemDefaultFocus() ImGuiWindow* window = g.CurrentWindow; if (!window->Appearing) return; - if (g.NavWindow != window->RootWindowForNav || (!g.NavInitRequest && g.NavInitResultId == 0) || g.NavLayer != window->DC.NavLayerCurrent) + if (g.NavWindow != window->RootWindowForNav || (!g.NavInitRequest && g.NavInitResult.ID == 0) || g.NavLayer != window->DC.NavLayerCurrent) return; g.NavInitRequest = false; - g.NavInitResultId = g.LastItemData.ID; - g.NavInitResultRectRel = WindowRectAbsToRel(window, g.LastItemData.Rect); + NavApplyItemToResult(&g.NavInitResult); NavUpdateAnyRequestFlag(); // Scroll could be done in NavInitRequestApplyResult() via an opt-in flag (we however don't want regular init requests to scroll) @@ -7708,13 +7896,13 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) // - Shortcut() [Internal] //----------------------------------------------------------------------------- -ImGuiKeyData* ImGui::GetKeyData(ImGuiKey key) +ImGuiKeyData* ImGui::GetKeyData(ImGuiContext* ctx, ImGuiKey key) { - ImGuiContext& g = *GImGui; + ImGuiContext& g = *ctx; // Special storage location for mods if (key & ImGuiMod_Mask_) - key = ConvertSingleModFlagToKey(key); + key = ConvertSingleModFlagToKey(ctx, key); #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO IM_ASSERT(key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_NamedKey_END); @@ -7763,22 +7951,22 @@ IM_STATIC_ASSERT(ImGuiKey_NamedKey_COUNT == IM_ARRAYSIZE(GKeyNames)); const char* ImGui::GetKeyName(ImGuiKey key) { + ImGuiContext& g = *GImGui; #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO - IM_ASSERT((IsNamedKey(key) || key == ImGuiKey_None) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); + IM_ASSERT((IsNamedKeyOrModKey(key) || key == ImGuiKey_None) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); #else if (IsLegacyKey(key)) { - ImGuiIO& io = GetIO(); - if (io.KeyMap[key] == -1) + if (g.IO.KeyMap[key] == -1) return "N/A"; - IM_ASSERT(IsNamedKey((ImGuiKey)io.KeyMap[key])); - key = (ImGuiKey)io.KeyMap[key]; + IM_ASSERT(IsNamedKey((ImGuiKey)g.IO.KeyMap[key])); + key = (ImGuiKey)g.IO.KeyMap[key]; } #endif if (key == ImGuiKey_None) return "None"; if (key & ImGuiMod_Mask_) - key = ConvertSingleModFlagToKey(key); + key = ConvertSingleModFlagToKey(&g, key); if (!IsNamedKey(key)) return "Unknown"; @@ -7873,7 +8061,7 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) // Apply routing to owner if there's no owner already (RoutingCurr == None at this point) if (routing_entry->Mods == g.IO.KeyMods) { - ImGuiKeyOwnerData* owner_data = ImGui::GetKeyOwnerData(key); + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); if (owner_data->OwnerCurr == ImGuiKeyOwner_None) owner_data->OwnerCurr = routing_entry->RoutingCurr; } @@ -7910,7 +8098,7 @@ ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); if (key == ImGuiKey_None) - key = ConvertSingleModFlagToKey(mods); + key = ConvertSingleModFlagToKey(&g, mods); IM_ASSERT(IsNamedKey(key)); // Get (in the majority of case, the linked list will have one element so this should be 2 reads. @@ -8416,6 +8604,13 @@ static void ImGui::UpdateMouseInputs() ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; + // Mouse Wheel swapping flag + // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead + // - We avoid doing it on OSX as it the OS input layer handles this already. + // - FIXME: However this means when running on OSX over Emscripten, Shift+WheelY will incur two swapping (1 in OS, 1 here), canceling the feature. + // - FIXME: When we can distinguish e.g. touchpad scroll events from mouse ones, we'll set this accordingly based on input source. + io.MouseWheelRequestAxisSwap = io.KeyShift && !io.ConfigMacOSXBehaviors; + // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) if (IsMousePosValid(&io.MousePos)) io.MousePos = g.MouseLastValidPos = ImFloorSigned(io.MousePos); @@ -8426,6 +8621,13 @@ static void ImGui::UpdateMouseInputs() else io.MouseDelta = ImVec2(0.0f, 0.0f); + // Update stationary timer. + // FIXME: May need to rework again to have some tolerance for occasional small movement, while being functional on high-framerates. + const float mouse_stationary_threshold = (io.MouseSource == ImGuiMouseSource_Mouse) ? 2.0f : 3.0f; // Slightly higher threshold for ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen, may need rework. + const bool mouse_stationary = (ImLengthSqr(io.MouseDelta) <= mouse_stationary_threshold * mouse_stationary_threshold); + g.MouseStationaryTimer = mouse_stationary ? (g.MouseStationaryTimer + io.DeltaTime) : 0.0f; + //IMGUI_DEBUG_LOG("%.4f\n", g.MouseStationaryTimer); + // If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) g.NavDisableMouseHover = false; @@ -8481,7 +8683,7 @@ static void LockWheelingWindow(ImGuiWindow* window, float wheel_amount) g.WheelingWindowReleaseTimer = 0.0f; if (g.WheelingWindow == window) return; - IMGUI_DEBUG_LOG_IO("LockWheelingWindow() \"%s\"\n", window ? window->Name : "NULL"); + IMGUI_DEBUG_LOG_IO("[io] LockWheelingWindow() \"%s\"\n", window ? window->Name : "NULL"); g.WheelingWindow = window; g.WheelingWindowRefMousePos = g.IO.MousePos; if (window == NULL) @@ -8576,15 +8778,9 @@ void ImGui::UpdateMouseWheel() return; // Mouse wheel scrolling - // As a standard behavior holding SHIFT while using Vertical Mouse Wheel triggers Horizontal scroll instead - // - We avoid doing it on OSX as it the OS input layer handles this already. - // - However this means when running on OSX over Emcripten, Shift+WheelY will incur two swappings (1 in OS, 1 here), cancelling the feature. - const bool swap_axis = g.IO.KeyShift && !g.IO.ConfigMacOSXBehaviors; - if (swap_axis) - { - wheel.x = wheel.y; - wheel.y = 0.0f; - } + // Read about io.MouseWheelRequestAxisSwap and its issue on Mac+Emscripten in UpdateMouseInputs() + if (g.IO.MouseWheelRequestAxisSwap) + wheel = ImVec2(wheel.y, 0.0f); // Maintain a rough average of moving magnitude on both axises // FIXME: should by based on wall clock time rather than frame-counter @@ -8638,19 +8834,25 @@ void ImGui::SetNextFrameWantCaptureMouse(bool want_capture_mouse) #ifndef IMGUI_DISABLE_DEBUG_TOOLS static const char* GetInputSourceName(ImGuiInputSource source) { - const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Nav", "Clipboard" }; + const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Clipboard" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT && source >= 0 && source < ImGuiInputSource_COUNT); return input_source_names[source]; } +static const char* GetMouseSourceName(ImGuiMouseSource source) +{ + const char* mouse_source_names[] = { "Mouse", "TouchScreen", "Pen" }; + IM_ASSERT(IM_ARRAYSIZE(mouse_source_names) == ImGuiMouseSource_COUNT && source >= 0 && source < ImGuiMouseSource_COUNT); + return mouse_source_names[source]; +} static void DebugPrintInputEvent(const char* prefix, const ImGuiInputEvent* e) { ImGuiContext& g = *GImGui; - if (e->Type == ImGuiInputEventType_MousePos) { if (e->MousePos.PosX == -FLT_MAX && e->MousePos.PosY == -FLT_MAX) IMGUI_DEBUG_LOG_IO("%s: MousePos (-FLT_MAX, -FLT_MAX)\n", prefix); else IMGUI_DEBUG_LOG_IO("%s: MousePos (%.1f, %.1f)\n", prefix, e->MousePos.PosX, e->MousePos.PosY); return; } - if (e->Type == ImGuiInputEventType_MouseButton) { IMGUI_DEBUG_LOG_IO("%s: MouseButton %d %s\n", prefix, e->MouseButton.Button, e->MouseButton.Down ? "Down" : "Up"); return; } - if (e->Type == ImGuiInputEventType_MouseWheel) { IMGUI_DEBUG_LOG_IO("%s: MouseWheel (%.3f, %.3f)\n", prefix, e->MouseWheel.WheelX, e->MouseWheel.WheelY); return; } - if (e->Type == ImGuiInputEventType_Key) { IMGUI_DEBUG_LOG_IO("%s: Key \"%s\" %s\n", prefix, ImGui::GetKeyName(e->Key.Key), e->Key.Down ? "Down" : "Up"); return; } - if (e->Type == ImGuiInputEventType_Text) { IMGUI_DEBUG_LOG_IO("%s: Text: %c (U+%08X)\n", prefix, e->Text.Char, e->Text.Char); return; } - if (e->Type == ImGuiInputEventType_Focus) { IMGUI_DEBUG_LOG_IO("%s: AppFocused %d\n", prefix, e->AppFocused.Focused); return; } + if (e->Type == ImGuiInputEventType_MousePos) { if (e->MousePos.PosX == -FLT_MAX && e->MousePos.PosY == -FLT_MAX) IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (-FLT_MAX, -FLT_MAX)\n", prefix); else IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (%.1f, %.1f) (%s)\n", prefix, e->MousePos.PosX, e->MousePos.PosY, GetMouseSourceName(e->MouseWheel.MouseSource)); return; } + if (e->Type == ImGuiInputEventType_MouseButton) { IMGUI_DEBUG_LOG_IO("[io] %s: MouseButton %d %s (%s)\n", prefix, e->MouseButton.Button, e->MouseButton.Down ? "Down" : "Up", GetMouseSourceName(e->MouseWheel.MouseSource)); return; } + if (e->Type == ImGuiInputEventType_MouseWheel) { IMGUI_DEBUG_LOG_IO("[io] %s: MouseWheel (%.3f, %.3f) (%s)\n", prefix, e->MouseWheel.WheelX, e->MouseWheel.WheelY, GetMouseSourceName(e->MouseWheel.MouseSource)); return; } + if (e->Type == ImGuiInputEventType_Key) { IMGUI_DEBUG_LOG_IO("[io] %s: Key \"%s\" %s\n", prefix, ImGui::GetKeyName(e->Key.Key), e->Key.Down ? "Down" : "Up"); return; } + if (e->Type == ImGuiInputEventType_Text) { IMGUI_DEBUG_LOG_IO("[io] %s: Text: %c (U+%08X)\n", prefix, e->Text.Char, e->Text.Char); return; } + if (e->Type == ImGuiInputEventType_Focus) { IMGUI_DEBUG_LOG_IO("[io] %s: AppFocused %d\n", prefix, e->AppFocused.Focused); return; } } #endif @@ -8683,6 +8885,7 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) if (trickle_fast_inputs && (mouse_button_changed != 0 || mouse_wheeled || key_changed || text_inputted)) break; io.MousePos = event_pos; + io.MouseSource = e->MousePos.MouseSource; mouse_moved = true; } else if (e->Type == ImGuiInputEventType_MouseButton) @@ -8692,7 +8895,10 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); if (trickle_fast_inputs && ((mouse_button_changed & (1 << button)) || mouse_wheeled)) break; + if (trickle_fast_inputs && e->MouseButton.MouseSource == ImGuiMouseSource_TouchScreen && mouse_moved) // #2702: TouchScreen have no initial hover. + break; io.MouseDown[button] = e->MouseButton.Down; + io.MouseSource = e->MouseButton.MouseSource; mouse_button_changed |= (1 << button); } else if (e->Type == ImGuiInputEventType_MouseWheel) @@ -8702,6 +8908,7 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) break; io.MouseWheelH += e->MouseWheel.WheelX; io.MouseWheel += e->MouseWheel.WheelY; + io.MouseSource = e->MouseWheel.MouseSource; mouse_wheeled = true; } else if (e->Type == ImGuiInputEventType_Key) @@ -8780,7 +8987,7 @@ ImGuiID ImGui::GetKeyOwner(ImGuiKey key) return ImGuiKeyOwner_None; ImGuiContext& g = *GImGui; - ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); ImGuiID owner_id = owner_data->OwnerCurr; if (g.ActiveIdUsingAllKeyboardKeys && owner_id != g.ActiveId && owner_id != ImGuiKeyOwner_Any) @@ -8804,7 +9011,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) return false; - ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); if (owner_id == ImGuiKeyOwner_Any) return (owner_data->LockThisFrame == false); @@ -8832,7 +9039,8 @@ void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function! - ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + ImGuiContext& g = *GImGui; + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); owner_data->OwnerCurr = owner_data->OwnerNext = owner_id; // We cannot lock by default as it would likely break lots of legacy code. @@ -8841,6 +9049,17 @@ void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) owner_data->LockThisFrame = (flags & ImGuiInputFlags_LockThisFrame) != 0 || (owner_data->LockUntilRelease); } +// Rarely used helper +void ImGui::SetKeyOwnersForKeyChord(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +{ + if (key_chord & ImGuiMod_Ctrl) { SetKeyOwner(ImGuiMod_Ctrl, owner_id, flags); } + if (key_chord & ImGuiMod_Shift) { SetKeyOwner(ImGuiMod_Shift, owner_id, flags); } + if (key_chord & ImGuiMod_Alt) { SetKeyOwner(ImGuiMod_Alt, owner_id, flags); } + if (key_chord & ImGuiMod_Super) { SetKeyOwner(ImGuiMod_Super, owner_id, flags); } + if (key_chord & ImGuiMod_Shortcut) { SetKeyOwner(ImGuiMod_Shortcut, owner_id, flags); } + if (key_chord & ~ImGuiMod_Mask_) { SetKeyOwner((ImGuiKey)(key_chord & ~ImGuiMod_Mask_), owner_id, flags); } +} + // This is more or less equivalent to: // if (IsItemHovered() || IsItemActive()) // SetKeyOwner(key, GetItemID()); @@ -8881,7 +9100,7 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags // Special storage location for mods ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); if (key == ImGuiKey_None) - key = ConvertSingleModFlagToKey(mods); + key = ConvertSingleModFlagToKey(&g, mods); if (!IsKeyPressed(key, owner_id, (flags & (ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateMask_)))) return false; @@ -9116,6 +9335,11 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); PopStyleVar(); } + while (g.FontStack.Size > stack_sizes->SizeOfFontStack) //-V1044 + { + if (log_callback) log_callback(user_data, "Recovered from missing PopFont() in '%s'", window->Name); + PopFont(); + } while (g.FocusScopeStack.Size > stack_sizes->SizeOfFocusScopeStack + 1) //-V1044 { if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); @@ -9247,7 +9471,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu g.LastItemData.ID = id; g.LastItemData.Rect = bb; g.LastItemData.NavRect = nav_bb_arg ? *nav_bb_arg : bb; - g.LastItemData.InFlags = g.CurrentItemFlags | extra_flags; + g.LastItemData.InFlags = g.CurrentItemFlags | g.NextItemData.ItemFlags | extra_flags; g.LastItemData.StatusFlags = ImGuiItemStatusFlags_None; // Directional navigation processing @@ -9275,10 +9499,11 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // [DEBUG] People keep stumbling on this problem and using "" as identifier in the root of a window instead of "##something". // Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something". - // READ THE FAQ: https://dearimgui.org/faq + // READ THE FAQ: https://dearimgui.com/faq IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); } g.NextItemData.Flags = ImGuiNextItemDataFlags_None; + g.NextItemData.ItemFlags = ImGuiItemFlags_None; #ifdef IMGUI_ENABLE_TEST_ENGINE if (id != 0) @@ -9292,7 +9517,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // return false; const bool is_rect_visible = bb.Overlaps(window->ClipRect); if (!is_rect_visible) - if (id == 0 || (id != g.ActiveId && id != g.NavId)) + if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId)) if (!g.LogEnabled) return false; @@ -9302,6 +9527,8 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu DebugLocateItemResolveWithLastItem(); #endif //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] + //if ((g.LastItemData.InFlags & ImGuiItemFlags_NoNav) == 0) + // window->DrawList->AddRect(g.LastItemData.NavRect.Min, g.LastItemData.NavRect.Max, IM_COL32(255,255,0,255)); // [DEBUG] // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (is_rect_visible) @@ -9922,26 +10149,35 @@ bool ImGui::BeginTooltip() return BeginTooltipEx(ImGuiTooltipFlags_None, ImGuiWindowFlags_None); } +bool ImGui::BeginItemTooltip() +{ + if (!IsItemHovered(ImGuiHoveredFlags_ForTooltip)) + return false; + return BeginTooltipEx(ImGuiTooltipFlags_None, ImGuiWindowFlags_None); +} + bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags) { ImGuiContext& g = *GImGui; if (g.DragDropWithinSource || g.DragDropWithinTarget) { - // The default tooltip position is a little offset to give space to see the context menu (it's also clamped within the current viewport/monitor) - // In the context of a dragging tooltip we try to reduce that offset and we enforce following the cursor. - // Whatever we do we want to call SetNextWindowPos() to enforce a tooltip position and disable clipping the tooltip without our display area, like regular tooltip do. + // Drag and Drop tooltips are positioning differently than other tooltips: + // - offset visibility to increase visibility around mouse. + // - never clamp within outer viewport boundary. + // We call SetNextWindowPos() to enforce position and disable clamping. + // See FindBestWindowPosForPopup() for positionning logic of other tooltips (not drag and drop ones). //ImVec2 tooltip_pos = g.IO.MousePos - g.ActiveIdClickOffset - g.Style.WindowPadding; - ImVec2 tooltip_pos = g.IO.MousePos + ImVec2(16 * g.Style.MouseCursorScale, 8 * g.Style.MouseCursorScale); + ImVec2 tooltip_pos = g.IO.MousePos + TOOLTIP_DEFAULT_OFFSET * g.Style.MouseCursorScale; SetNextWindowPos(tooltip_pos); SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f); //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :( - tooltip_flags |= ImGuiTooltipFlags_OverridePreviousTooltip; + tooltip_flags |= ImGuiTooltipFlags_OverridePrevious; } char window_name[16]; ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", g.TooltipOverrideCount); - if (tooltip_flags & ImGuiTooltipFlags_OverridePreviousTooltip) + if (tooltip_flags & ImGuiTooltipFlags_OverridePrevious) if (ImGuiWindow* window = FindWindowByName(window_name)) if (window->Active) { @@ -9965,22 +10201,40 @@ void ImGui::EndTooltip() End(); } +void ImGui::SetTooltip(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + SetTooltipV(fmt, args); + va_end(args); +} + void ImGui::SetTooltipV(const char* fmt, va_list args) { - if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None)) + if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePrevious, ImGuiWindowFlags_None)) return; TextV(fmt, args); EndTooltip(); } -void ImGui::SetTooltip(const char* fmt, ...) +// Shortcut to use 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav'. +// Defaults to == ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort when using the mouse. +void ImGui::SetItemTooltip(const char* fmt, ...) { va_list args; va_start(args, fmt); - SetTooltipV(fmt, args); + if (IsItemHovered(ImGuiHoveredFlags_ForTooltip)) + SetTooltipV(fmt, args); va_end(args); } +void ImGui::SetItemTooltipV(const char* fmt, va_list args) +{ + if (IsItemHovered(ImGuiHoveredFlags_ForTooltip)) + SetTooltipV(fmt, args); +} + + //----------------------------------------------------------------------------- // [SECTION] POPUPS //----------------------------------------------------------------------------- @@ -10026,6 +10280,7 @@ bool ImGui::IsPopupOpen(const char* str_id, ImGuiPopupFlags popup_flags) return IsPopupOpen(id, popup_flags); } +// Also see FindBlockingModal(NULL) ImGuiWindow* ImGui::GetTopMostPopupModal() { ImGuiContext& g = *GImGui; @@ -10036,6 +10291,7 @@ ImGuiWindow* ImGui::GetTopMostPopupModal() return NULL; } +// See Demo->Stacked Modal to confirm what this is for. ImGuiWindow* ImGui::GetTopMostAndVisiblePopupModal() { ImGuiContext& g = *GImGui; @@ -10164,7 +10420,7 @@ void ImGui::ClosePopupsExceptModals() for (popup_count_to_keep = g.OpenPopupStack.Size; popup_count_to_keep > 0; popup_count_to_keep--) { ImGuiWindow* window = g.OpenPopupStack[popup_count_to_keep - 1].Window; - if (!window || window->Flags & ImGuiWindowFlags_Modal) + if (!window || (window->Flags & ImGuiWindowFlags_Modal)) break; } if (popup_count_to_keep < g.OpenPopupStack.Size) // This test is not required but it allows to set a convenient breakpoint on the statement below @@ -10186,16 +10442,9 @@ void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_ { ImGuiWindow* focus_window = (popup_window && popup_window->Flags & ImGuiWindowFlags_ChildMenu) ? popup_window->ParentWindow : popup_backup_nav_window; if (focus_window && !focus_window->WasActive && popup_window) - { - // Fallback - FocusTopMostWindowUnderOne(popup_window, NULL); - } + FocusTopMostWindowUnderOne(popup_window, NULL, NULL, ImGuiFocusRequestFlags_RestoreFocusedChild); // Fallback else - { - if (g.NavLayer == ImGuiNavLayer_Main && focus_window) - focus_window = NavRestoreLastChildNavWindow(focus_window); - FocusWindow(focus_window); - } + FocusWindow(focus_window, (g.NavLayer == ImGuiNavLayer_Main) ? ImGuiFocusRequestFlags_RestoreFocusedChild : ImGuiFocusRequestFlags_None); } } @@ -10508,15 +10757,20 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) } if (window->Flags & ImGuiWindowFlags_Tooltip) { - // Position tooltip (always follows mouse) - float sc = g.Style.MouseCursorScale; - ImVec2 ref_pos = NavCalcPreferredRefPos(); + // Position tooltip (always follows mouse + clamp within outer boundaries) + // Note that drag and drop tooltips are NOT using this path: BeginTooltipEx() manually sets their position. + // In theory we could handle both cases in same location, but requires a bit of shuffling as drag and drop tooltips are calling SetWindowPos() leading to 'window_pos_set_by_api' being set in Begin() + IM_ASSERT(g.CurrentWindow == window); + const float scale = g.Style.MouseCursorScale; + const ImVec2 ref_pos = NavCalcPreferredRefPos(); + const ImVec2 tooltip_pos = ref_pos + TOOLTIP_DEFAULT_OFFSET * scale; ImRect r_avoid; if (!g.NavDisableHighlight && g.NavDisableMouseHover && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos)) r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8); else - r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * sc, ref_pos.y + 24 * sc); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important. - return FindBestWindowPosForPopupEx(ref_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Tooltip); + r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * scale, ref_pos.y + 24 * scale); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important. + //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255, 0, 255, 255)); + return FindBestWindowPosForPopupEx(tooltip_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Tooltip); } IM_ASSERT(0); return window->Pos; @@ -10542,6 +10796,12 @@ void ImGui::SetNavWindow(ImGuiWindow* window) NavUpdateAnyRequestFlag(); } +void ImGui::NavClearPreferredPosForAxis(ImGuiAxis axis) +{ + ImGuiContext& g = *GImGui; + g.NavWindow->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer][axis] = FLT_MAX; +} + void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel) { ImGuiContext& g = *GImGui; @@ -10552,6 +10812,10 @@ void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id g.NavFocusScopeId = focus_scope_id; g.NavWindow->NavLastIds[nav_layer] = id; g.NavWindow->NavRectRel[nav_layer] = rect_rel; + + // Clear preferred scoring position (NavMoveRequestApplyResult() will tend to restore it) + NavClearPreferredPosForAxis(ImGuiAxis_X); + NavClearPreferredPosForAxis(ImGuiAxis_Y); } void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) @@ -10572,42 +10836,32 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) if (g.LastItemData.ID == id) window->NavRectRel[nav_layer] = WindowRectAbsToRel(window, g.LastItemData.NavRect); - if (g.ActiveIdSource == ImGuiInputSource_Nav) + if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) g.NavDisableMouseHover = true; else g.NavDisableHighlight = true; + + // Clear preferred scoring position (NavMoveRequestApplyResult() will tend to restore it) + NavClearPreferredPosForAxis(ImGuiAxis_X); + NavClearPreferredPosForAxis(ImGuiAxis_Y); } -ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy) +static ImGuiDir ImGetDirQuadrantFromDelta(float dx, float dy) { if (ImFabs(dx) > ImFabs(dy)) return (dx > 0.0f) ? ImGuiDir_Right : ImGuiDir_Left; return (dy > 0.0f) ? ImGuiDir_Down : ImGuiDir_Up; } -static float inline NavScoreItemDistInterval(float a0, float a1, float b0, float b1) +static float inline NavScoreItemDistInterval(float cand_min, float cand_max, float curr_min, float curr_max) { - if (a1 < b0) - return a1 - b0; - if (b1 < a0) - return a0 - b1; + if (cand_max < curr_min) + return cand_max - curr_min; + if (curr_max < cand_min) + return cand_min - curr_max; return 0.0f; } -static void inline NavClampRectToVisibleAreaForMoveDir(ImGuiDir move_dir, ImRect& r, const ImRect& clip_rect) -{ - if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) - { - r.Min.y = ImClamp(r.Min.y, clip_rect.Min.y, clip_rect.Max.y); - r.Max.y = ImClamp(r.Max.y, clip_rect.Min.y, clip_rect.Max.y); - } - else // FIXME: PageUp/PageDown are leaving move_dir == None - { - r.Min.x = ImClamp(r.Min.x, clip_rect.Min.x, clip_rect.Max.x); - r.Max.x = ImClamp(r.Max.x, clip_rect.Min.x, clip_rect.Max.x); - } -} - // Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057 static bool ImGui::NavScoreItem(ImGuiNavItemData* result) { @@ -10630,10 +10884,6 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window } - // We perform scoring on items bounding box clipped by the current clipping rectangle on the other axis (clipping on our movement axis would give us equal scores for all clipped items) - // For example, this ensures that items in one column are not reached when moving vertically from items in another column. - NavClampRectToVisibleAreaForMoveDir(g.NavMoveClipDir, cand, window->ClipRect); - // Compute distance between boxes // FIXME-NAV: Introducing biases for vertical navigation, needs to be removed. float dbx = NavScoreItemDistInterval(cand.Min.x, cand.Max.x, curr.Min.x, curr.Max.x); @@ -10672,32 +10922,41 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) quadrant = (g.LastItemData.ID < g.NavId) ? ImGuiDir_Left : ImGuiDir_Right; } + const ImGuiDir move_dir = g.NavMoveDir; #if IMGUI_DEBUG_NAV_SCORING - char buf[128]; - if (IsMouseHoveringRect(cand.Min, cand.Max)) + char buf[200]; + if (g.IO.KeyCtrl) // Hold CTRL to preview score in matching quadrant. CTRL+Arrow to rotate. { - ImFormatString(buf, IM_ARRAYSIZE(buf), "dbox (%.2f,%.2f->%.4f)\ndcen (%.2f,%.2f->%.4f)\nd (%.2f,%.2f->%.4f)\nnav %c, quadrant %c", dbx, dby, dist_box, dcx, dcy, dist_center, dax, day, dist_axial, "WENS"[g.NavMoveDir], "WENS"[quadrant]); - ImDrawList* draw_list = GetForegroundDrawList(window); - draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255,200,0,100)); - draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255,255,0,200)); - draw_list->AddRectFilled(cand.Max - ImVec2(4, 4), cand.Max + CalcTextSize(buf) + ImVec2(4, 4), IM_COL32(40,0,0,150)); - draw_list->AddText(cand.Max, ~0U, buf); - } - else if (g.IO.KeyCtrl) // Hold to preview score in matching quadrant. Press C to rotate. - { - if (quadrant == g.NavMoveDir) + if (quadrant == move_dir) { ImFormatString(buf, IM_ARRAYSIZE(buf), "%.0f/%.0f", dist_box, dist_center); ImDrawList* draw_list = GetForegroundDrawList(window); - draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 200)); + draw_list->AddRectFilled(cand.Min, cand.Max, IM_COL32(255, 0, 0, 80)); + draw_list->AddRectFilled(cand.Min, cand.Min + CalcTextSize(buf), IM_COL32(255, 0, 0, 200)); draw_list->AddText(cand.Min, IM_COL32(255, 255, 255, 255), buf); } } + const bool debug_hovering = IsMouseHoveringRect(cand.Min, cand.Max); + const bool debug_tty = (g.IO.KeyCtrl && IsKeyPressed(ImGuiKey_Space)); + if (debug_hovering || debug_tty) + { + ImFormatString(buf, IM_ARRAYSIZE(buf), + "d-box (%7.3f,%7.3f) -> %7.3f\nd-center (%7.3f,%7.3f) -> %7.3f\nd-axial (%7.3f,%7.3f) -> %7.3f\nnav %c, quadrant %c", + dbx, dby, dist_box, dcx, dcy, dist_center, dax, day, dist_axial, "-WENS"[move_dir+1], "-WENS"[quadrant+1]); + if (debug_hovering) + { + ImDrawList* draw_list = GetForegroundDrawList(window); + draw_list->AddRect(curr.Min, curr.Max, IM_COL32(255, 200, 0, 100)); + draw_list->AddRect(cand.Min, cand.Max, IM_COL32(255, 255, 0, 200)); + draw_list->AddRectFilled(cand.Max - ImVec2(4, 4), cand.Max + CalcTextSize(buf) + ImVec2(4, 4), IM_COL32(40, 0, 0, 200)); + draw_list->AddText(cand.Max, ~0U, buf); + } + if (debug_tty) { IMGUI_DEBUG_LOG_NAV("id 0x%08X\n%s\n", g.LastItemData.ID, buf); } + } #endif // Is it in the quadrant we're interested in moving to? bool new_best = false; - const ImGuiDir move_dir = g.NavMoveDir; if (quadrant == move_dir) { // Does it beat the current best candidate? @@ -10753,6 +11012,15 @@ static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) result->RectRel = WindowRectAbsToRel(window, g.LastItemData.NavRect); } +// True when current work location may be scrolled horizontally when moving left / right. +// This is generally always true UNLESS within a column. We don't have a vertical equivalent. +void ImGui::NavUpdateCurrentWindowIsScrollPushableX() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + window->DC.NavIsScrollPushableX = (g.CurrentTable == NULL && window->DC.CurrentColumns == NULL); +} + // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) // This is called after LastItemData is set. static void ImGui::NavProcessItem() @@ -10760,18 +11028,24 @@ static void ImGui::NavProcessItem() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiID id = g.LastItemData.ID; - const ImRect nav_bb = g.LastItemData.NavRect; const ImGuiItemFlags item_flags = g.LastItemData.InFlags; + // When inside a container that isn't scrollable with Left<>Right, clip NavRect accordingly (#2221) + if (window->DC.NavIsScrollPushableX == false) + { + g.LastItemData.NavRect.Min.x = ImClamp(g.LastItemData.NavRect.Min.x, window->ClipRect.Min.x, window->ClipRect.Max.x); + g.LastItemData.NavRect.Max.x = ImClamp(g.LastItemData.NavRect.Max.x, window->ClipRect.Min.x, window->ClipRect.Max.x); + } + const ImRect nav_bb = g.LastItemData.NavRect; + // Process Init Request if (g.NavInitRequest && g.NavLayer == window->DC.NavLayerCurrent && (item_flags & ImGuiItemFlags_Disabled) == 0) { // Even if 'ImGuiItemFlags_NoNavDefaultFocus' is on (typically collapse/close button) we record the first ResultId so they can be used as a fallback const bool candidate_for_nav_default_focus = (item_flags & ImGuiItemFlags_NoNavDefaultFocus) == 0; - if (candidate_for_nav_default_focus || g.NavInitResultId == 0) + if (candidate_for_nav_default_focus || g.NavInitResult.ID == 0) { - g.NavInitResultId = id; - g.NavInitResultRectRel = WindowRectAbsToRel(window, nav_bb); + NavApplyItemToResult(&g.NavInitResult); } if (candidate_for_nav_default_focus) { @@ -10804,7 +11078,7 @@ static void ImGui::NavProcessItem() } } - // Update window-relative bounding box of navigated item + // Update information for currently focused/navigated item if (g.NavId == id) { if (g.NavWindow != window) @@ -10812,7 +11086,7 @@ static void ImGui::NavProcessItem() g.NavLayer = window->DC.NavLayerCurrent; g.NavFocusScopeId = g.CurrentFocusScopeId; g.NavIdIsAlive = true; - window->NavRectRel[window->DC.NavLayerCurrent] = WindowRectAbsToRel(window, nav_bb); // Store item bounding box (relative to window position) + window->NavRectRel[window->DC.NavLayerCurrent] = WindowRectAbsToRel(window, nav_bb); // Store item bounding box (relative to window position) } } @@ -10942,10 +11216,12 @@ void ImGui::NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNav void ImGui::NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags wrap_flags) { ImGuiContext& g = *GImGui; - IM_ASSERT(wrap_flags != 0); // Call with _WrapX, _WrapY, _LoopX, _LoopY - // In theory we should test for NavMoveRequestButNoResultYet() but there's no point doing it, NavEndFrame() will do the same test + IM_ASSERT((wrap_flags & ImGuiNavMoveFlags_WrapMask_ ) != 0 && (wrap_flags & ~ImGuiNavMoveFlags_WrapMask_) == 0); // Call with _WrapX, _WrapY, _LoopX, _LoopY + + // In theory we should test for NavMoveRequestButNoResultYet() but there's no point doing it: + // as NavEndFrame() will do the same test. It will end up calling NavUpdateCreateWrappingRequest(). if (g.NavWindow == window && g.NavMoveScoringItems && g.NavLayer == ImGuiNavLayer_Main) - g.NavMoveFlags |= wrap_flags; + g.NavMoveFlags = (g.NavMoveFlags & ~ImGuiNavMoveFlags_WrapMask_) | wrap_flags; } // FIXME: This could be replaced by updating a frame number in each window when (window == NavWindow) and (NavLayer == 0). @@ -11027,8 +11303,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) SetNavID(0, g.NavLayer, window->NavRootFocusScopeId, ImRect()); g.NavInitRequest = true; g.NavInitRequestFromMove = false; - g.NavInitResultId = 0; - g.NavInitResultRectRel = ImRect(); + g.NavInitResult.ID = 0; NavUpdateAnyRequestFlag(); } else @@ -11113,12 +11388,12 @@ static void ImGui::NavUpdate() g.NavInputSource = ImGuiInputSource_Keyboard; // Process navigation init request (select first/default focus) - if (g.NavInitResultId != 0) + g.NavJustMovedToId = 0; + if (g.NavInitResult.ID != 0) NavInitRequestApplyResult(); g.NavInitRequest = false; g.NavInitRequestFromMove = false; - g.NavInitResultId = 0; - g.NavJustMovedToId = 0; + g.NavInitResult.ID = 0; // Process navigation move request if (g.NavMoveSubmitted) @@ -11202,7 +11477,7 @@ static void ImGui::NavUpdate() ImGuiWindow* window = g.NavWindow; const float scroll_speed = IM_ROUND(window->CalcFontSize() * 100 * io.DeltaTime); // We need round the scrolling speed because sub-pixel scroll isn't reliably supported. const ImGuiDir move_dir = g.NavMoveDir; - if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll && move_dir != ImGuiDir_None) + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavWindowHasScrollY && move_dir != ImGuiDir_None) { if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) SetScrollX(window, ImFloor(window->Scroll.x + ((move_dir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); @@ -11242,11 +11517,11 @@ static void ImGui::NavUpdate() // [DEBUG] g.NavScoringDebugCount = 0; #if IMGUI_DEBUG_NAV_RECTS - if (g.NavWindow) + if (ImGuiWindow* debug_window = g.NavWindow) { - ImDrawList* draw_list = GetForegroundDrawList(g.NavWindow); - if (1) { for (int layer = 0; layer < 2; layer++) { ImRect r = WindowRectRelToAbs(g.NavWindow, g.NavWindow->NavRectRel[layer]); draw_list->AddRect(r.Min, r.Max, IM_COL32(255,200,0,255)); } } // [DEBUG] - if (1) { ImU32 col = (!g.NavWindow->Hidden) ? IM_COL32(255,0,255,255) : IM_COL32(255,0,0,255); ImVec2 p = NavCalcPreferredRefPos(); char buf[32]; ImFormatString(buf, 32, "%d", g.NavLayer); draw_list->AddCircleFilled(p, 3.0f, col); draw_list->AddText(NULL, 13.0f, p + ImVec2(8,-4), col, buf); } + ImDrawList* draw_list = GetForegroundDrawList(debug_window); + int layer = g.NavLayer; /* for (int layer = 0; layer < 2; layer++)*/ { ImRect r = WindowRectRelToAbs(debug_window, debug_window->NavRectRel[layer]); draw_list->AddRect(r.Min, r.Max, IM_COL32(255, 200, 0, 255)); } + //if (1) { ImU32 col = (!debug_window->Hidden) ? IM_COL32(255,0,255,255) : IM_COL32(255,0,0,255); ImVec2 p = NavCalcPreferredRefPos(); char buf[32]; ImFormatString(buf, 32, "%d", g.NavLayer); draw_list->AddCircleFilled(p, 3.0f, col); draw_list->AddText(NULL, 13.0f, p + ImVec2(8,-4), col, buf); } } #endif } @@ -11258,15 +11533,48 @@ void ImGui::NavInitRequestApplyResult() if (!g.NavWindow) return; + ImGuiNavItemData* result = &g.NavInitResult; + if (g.NavId != result->ID) + { + g.NavJustMovedToId = result->ID; + g.NavJustMovedToFocusScopeId = result->FocusScopeId; + g.NavJustMovedToKeyMods = 0; + } + // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) // FIXME-NAV: On _NavFlattened windows, g.NavWindow will only be updated during subsequent frame. Not a problem currently. - IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: ApplyResult: NavID 0x%08X in Layer %d Window \"%s\"\n", g.NavInitResultId, g.NavLayer, g.NavWindow->Name); - SetNavID(g.NavInitResultId, g.NavLayer, 0, g.NavInitResultRectRel); + IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: ApplyResult: NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); + SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); g.NavIdIsAlive = true; // Mark as alive from previous frame as we got a result if (g.NavInitRequestFromMove) NavRestoreHighlightAfterMove(); } +// Bias scoring rect ahead of scoring + update preferred pos (if missing) using source position +static void NavBiasScoringRect(ImRect& r, ImVec2& preferred_pos_rel, ImGuiDir move_dir, ImGuiNavMoveFlags move_flags) +{ + // Bias initial rect + ImGuiContext& g = *GImGui; + const ImVec2 rel_to_abs_offset = g.NavWindow->DC.CursorStartPos; + + // Initialize bias on departure if we don't have any. So mouse-click + arrow will record bias. + // - We default to L/U bias, so moving down from a large source item into several columns will land on left-most column. + // - But each successful move sets new bias on one axis, only cleared when using mouse. + if ((move_flags & ImGuiNavMoveFlags_Forwarded) == 0) + { + if (preferred_pos_rel.x == FLT_MAX) + preferred_pos_rel.x = ImMin(r.Min.x + 1.0f, r.Max.x) - rel_to_abs_offset.x; + if (preferred_pos_rel.y == FLT_MAX) + preferred_pos_rel.y = r.GetCenter().y - rel_to_abs_offset.y; + } + + // Apply general bias on the other axis + if ((move_dir == ImGuiDir_Up || move_dir == ImGuiDir_Down) && preferred_pos_rel.x != FLT_MAX) + r.Min.x = r.Max.x = preferred_pos_rel.x + rel_to_abs_offset.x; + else if ((move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) && preferred_pos_rel.y != FLT_MAX) + r.Min.y = r.Max.y = preferred_pos_rel.y + rel_to_abs_offset.y; +} + void ImGui::NavUpdateCreateMoveRequest() { ImGuiContext& g = *GImGui; @@ -11312,13 +11620,15 @@ void ImGui::NavUpdateCreateMoveRequest() g.NavScoringNoClipRect.TranslateY(scoring_rect_offset_y); } - // [DEBUG] Always send a request + // [DEBUG] Always send a request when holding CTRL. Hold CTRL + Arrow change the direction. #if IMGUI_DEBUG_NAV_SCORING - if (io.KeyCtrl && IsKeyPressed(ImGuiKey_C)) - g.NavMoveDirForDebug = (ImGuiDir)((g.NavMoveDirForDebug + 1) & 3); - if (io.KeyCtrl && g.NavMoveDir == ImGuiDir_None) + //if (io.KeyCtrl && IsKeyPressed(ImGuiKey_C)) + // g.NavMoveDirForDebug = (ImGuiDir)((g.NavMoveDirForDebug + 1) & 3); + if (io.KeyCtrl) { - g.NavMoveDir = g.NavMoveDirForDebug; + if (g.NavMoveDir == ImGuiDir_None) + g.NavMoveDir = g.NavMoveDirForDebug; + g.NavMoveClipDir = g.NavMoveDir; g.NavMoveFlags |= ImGuiNavMoveFlags_DebugNoResult; } #endif @@ -11333,7 +11643,7 @@ void ImGui::NavUpdateCreateMoveRequest() { IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", window ? window->Name : "", g.NavLayer); g.NavInitRequest = g.NavInitRequestFromMove = true; - g.NavInitResultId = 0; + g.NavInitResult.ID = 0; g.NavDisableHighlight = false; } @@ -11371,8 +11681,8 @@ void ImGui::NavUpdateCreateMoveRequest() ImRect nav_rect_rel = !window->NavRectRel[g.NavLayer].IsInverted() ? window->NavRectRel[g.NavLayer] : ImRect(0, 0, 0, 0); scoring_rect = WindowRectRelToAbs(window, nav_rect_rel); scoring_rect.TranslateY(scoring_rect_offset_y); - scoring_rect.Min.x = ImMin(scoring_rect.Min.x + 1.0f, scoring_rect.Max.x); - scoring_rect.Max.x = scoring_rect.Min.x; + if (g.NavMoveSubmitted) + NavBiasScoringRect(scoring_rect, window->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer], g.NavMoveDir, g.NavMoveFlags); IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allow us to remove extraneous ImFabs() calls in NavScoreItem(). //GetForegroundDrawList()->AddRect(scoring_rect.Min, scoring_rect.Max, IM_COL32(255,200,0,255)); // [DEBUG] //if (!g.NavScoringNoClipRect.IsInverted()) { GetForegroundDrawList()->AddRect(g.NavScoringNoClipRect.Min, g.NavScoringNoClipRect.Max, IM_COL32(255, 200, 0, 255)); } // [DEBUG] @@ -11402,9 +11712,10 @@ void ImGui::NavUpdateCreateTabbingRequest() g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.NavDisableHighlight == true && g.ActiveId == 0) ? 0 : +1; else g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_Activate; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; ImGuiDir clip_dir = (g.NavTabbingDir < 0) ? ImGuiDir_Up : ImGuiDir_Down; - NavMoveRequestSubmit(ImGuiDir_None, clip_dir, ImGuiNavMoveFlags_Tabbing, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. + NavMoveRequestSubmit(ImGuiDir_None, clip_dir, move_flags, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. g.NavTabbingCounter = -1; } @@ -11426,12 +11737,15 @@ void ImGui::NavMoveRequestApplyResult() result = &g.NavTabbingResultFirst; // In a situation when there are no results but NavId != 0, re-enable the Navigation highlight (because g.NavId is not considered as a possible result) + const ImGuiAxis axis = (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? ImGuiAxis_Y : ImGuiAxis_X; if (result == NULL) { if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) - g.NavMoveFlags |= ImGuiNavMoveFlags_DontSetNavHighlight; - if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) + g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavHighlight; + if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavHighlight) == 0) NavRestoreHighlightAfterMove(); + NavClearPreferredPosForAxis(axis); // On a failed move, clear preferred pos for this axis. + IMGUI_DEBUG_LOG_NAV("[nav] NavMoveSubmitted but not led to a result!\n"); return; } @@ -11449,17 +11763,15 @@ void ImGui::NavMoveRequestApplyResult() // Scroll to keep newly navigated item fully into view. if (g.NavLayer == ImGuiNavLayer_Main) { + ImRect rect_abs = WindowRectRelToAbs(result->Window, result->RectRel); + ScrollToRectEx(result->Window, rect_abs, g.NavMoveScrollFlags); + if (g.NavMoveFlags & ImGuiNavMoveFlags_ScrollToEdgeY) { - // FIXME: Should remove this + // FIXME: Should remove this? Or make more precise: use ScrollToRectEx() with edge? float scroll_target = (g.NavMoveDir == ImGuiDir_Up) ? result->Window->ScrollMax.y : 0.0f; SetScrollY(result->Window, scroll_target); } - else - { - ImRect rect_abs = WindowRectRelToAbs(result->Window, result->RectRel); - ScrollToRectEx(result->Window, rect_abs, g.NavMoveScrollFlags); - } } if (g.NavWindow != result->Window) @@ -11469,7 +11781,7 @@ void ImGui::NavMoveRequestApplyResult() } if (g.ActiveId != result->ID) ClearActiveID(); - if (g.NavId != result->ID) + if (g.NavId != result->ID && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSelect) == 0) { // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; @@ -11477,27 +11789,35 @@ void ImGui::NavMoveRequestApplyResult() g.NavJustMovedToKeyMods = g.NavMoveKeyMods; } - // Focus + // Apply new NavID/Focus IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); + ImVec2 preferred_scoring_pos_rel = g.NavWindow->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer]; SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); - // Tabbing: Activates Inputable or Focus non-Inputable - if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && (result->InFlags & ImGuiItemFlags_Inputable)) + // Restore last preferred position for current axis + // (storing in RootWindowForNav-> as the info is desirable at the beginning of a Move Request. In theory all storage should use RootWindowForNav..) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) == 0) { - g.NavNextActivateId = result->ID; - g.NavNextActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState; - g.NavMoveFlags |= ImGuiNavMoveFlags_DontSetNavHighlight; + preferred_scoring_pos_rel[axis] = result->RectRel.GetCenter()[axis]; + g.NavWindow->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer] = preferred_scoring_pos_rel; } + // Tabbing: Activates Inputable, otherwise only Focus + if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && (result->InFlags & ImGuiItemFlags_Inputable) == 0) + g.NavMoveFlags &= ~ImGuiNavMoveFlags_Activate; + // Activate if (g.NavMoveFlags & ImGuiNavMoveFlags_Activate) { g.NavNextActivateId = result->ID; g.NavNextActivateFlags = ImGuiActivateFlags_None; + g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavHighlight; + if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + g.NavNextActivateFlags |= ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState; } // Enable nav highlight - if ((g.NavMoveFlags & ImGuiNavMoveFlags_DontSetNavHighlight) == 0) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavHighlight) == 0) NavRestoreHighlightAfterMove(); } @@ -11570,7 +11890,7 @@ static float ImGui::NavUpdatePageUpPageDown() if (g.NavLayer != ImGuiNavLayer_Main) NavRestoreLayer(ImGuiNavLayer_Main); - if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavHasScroll) + if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavWindowHasScrollY) { // Fallback manual-scroll when window has no navigable item if (IsKeyPressed(ImGuiKey_PageUp, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat)) @@ -11638,8 +11958,7 @@ static void ImGui::NavEndFrame() // Perform wrap-around in menus // FIXME-NAV: Wrap may need to apply a weight bias on the other axis. e.g. 4x4 grid with 2 last items missing on last item won't handle LoopY/WrapY correctly. // FIXME-NAV: Wrap (not Loop) support could be handled by the scoring function and then WrapX would function without an extra frame. - const ImGuiNavMoveFlags wanted_flags = ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX | ImGuiNavMoveFlags_WrapY | ImGuiNavMoveFlags_LoopY; - if (g.NavWindow && NavMoveRequestButNoResultYet() && (g.NavMoveFlags & wanted_flags) && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) + if (g.NavWindow && NavMoveRequestButNoResultYet() && (g.NavMoveFlags & ImGuiNavMoveFlags_WrapMask_) && (g.NavMoveFlags & ImGuiNavMoveFlags_Forwarded) == 0) NavUpdateCreateWrappingRequest(); } @@ -11651,7 +11970,9 @@ static void ImGui::NavUpdateCreateWrappingRequest() bool do_forward = false; ImRect bb_rel = window->NavRectRel[g.NavLayer]; ImGuiDir clip_dir = g.NavMoveDir; + const ImGuiNavMoveFlags move_flags = g.NavMoveFlags; + //const ImGuiAxis move_axis = (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? ImGuiAxis_Y : ImGuiAxis_X; if (g.NavMoveDir == ImGuiDir_Left && (move_flags & (ImGuiNavMoveFlags_WrapX | ImGuiNavMoveFlags_LoopX))) { bb_rel.Min.x = bb_rel.Max.x = window->ContentSize.x + window->WindowPadding.x; @@ -11695,6 +12016,8 @@ static void ImGui::NavUpdateCreateWrappingRequest() if (!do_forward) return; window->NavRectRel[g.NavLayer] = bb_rel; + NavClearPreferredPosForAxis(ImGuiAxis_X); + NavClearPreferredPosForAxis(ImGuiAxis_Y); NavMoveRequestForward(g.NavMoveDir, clip_dir, move_flags, g.NavMoveScrollFlags); } @@ -11748,7 +12071,7 @@ static void ImGui::NavUpdateWindowing() bool apply_toggle_layer = false; ImGuiWindow* modal_window = GetTopMostPopupModal(); - bool allow_windowing = (modal_window == NULL); + bool allow_windowing = (modal_window == NULL); // FIXME: This prevent CTRL+TAB from being usable with windows that are inside the Begin-stack of that modal. if (!allow_windowing) g.NavWindowingTarget = NULL; @@ -11761,10 +12084,11 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL+Tab or Square+L/R window selection + const ImGuiID owner_id = ImHashStr("###NavUpdateWindowing"); const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); - const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); + const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, owner_id, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); + const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, owner_id, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && IsKeyPressed(ImGuiKey_NavGamepadMenu, 0, ImGuiInputFlags_None); const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && (keyboard_next_window || keyboard_prev_window); // Note: enabled even without NavEnableKeyboard! if (start_windowing_with_gamepad || start_windowing_with_keyboard) @@ -11775,6 +12099,10 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingAccumDeltaPos = g.NavWindowingAccumDeltaSize = ImVec2(0.0f, 0.0f); g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; + + // Register ownership of our mods. Using ImGuiInputFlags_RouteGlobalHigh in the Shortcut() calls instead would probably be correct but may have more side-effects. + if (keyboard_next_window || keyboard_prev_window) + SetKeyOwnersForKeyChord((g.ConfigNavWindowingKeyNext | g.ConfigNavWindowingKeyPrev) & ImGuiMod_Mask_, owner_id); } // Gamepad update @@ -11872,9 +12200,9 @@ static void ImGui::NavUpdateWindowing() { ClearActiveID(); NavRestoreHighlightAfterMove(); - apply_focus_window = NavRestoreLastChildNavWindow(apply_focus_window); ClosePopupsOverWindow(apply_focus_window, false); - FocusWindow(apply_focus_window); + FocusWindow(apply_focus_window, ImGuiFocusRequestFlags_RestoreFocusedChild); + apply_focus_window = g.NavWindow; if (apply_focus_window->NavLastIds[0] == 0) NavInitWindow(apply_focus_window, false); @@ -12045,7 +12373,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) // Rely on keeping other window->LastItemXXX fields intact. source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); KeepAliveID(source_id); - bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id); + bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id, g.LastItemData.InFlags); if (is_hovered && g.IO.MouseClicked[mouse_button]) { SetActiveID(source_id, window); @@ -12647,6 +12975,7 @@ void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) } // Zero-tolerance, no error reporting, cheap .ini parsing +// Set ini_size==0 to let us use strlen(ini_data). Do not call this function with a 0 if your buffer is actually empty! void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) { ImGuiContext& g = *GImGui; @@ -12754,12 +13083,13 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name) { ImGuiContext& g = *GImGui; -#if !IMGUI_DEBUG_INI_SETTINGS - // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() - // Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier. - if (const char* p = strstr(name, "###")) - name = p; -#endif + if (g.IO.ConfigDebugIniSettings == false) + { + // Skip to the "###" marker if any. We don't skip past to match the behavior of GetID() + // Preserve the full string when ConfigDebugVerboseIniSettings is set to make .ini inspection easier. + if (const char* p = strstr(name, "###")) + name = p; + } const size_t name_len = strlen(name); // Allocate chunk @@ -12778,7 +13108,7 @@ ImGuiWindowSettings* ImGui::FindWindowSettingsByID(ImGuiID id) { ImGuiContext& g = *GImGui; for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings)) - if (settings->ID == id) + if (settings->ID == id && !settings->WantDelete) return settings; return NULL; } @@ -13242,7 +13572,7 @@ void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list) draw_list->AddRectFilled(face_min, face_max, IM_COL32(252, 252, 252, 255), key_face_rounding); ImVec2 label_min = ImVec2(key_min.x + key_label_pos.x, key_min.y + key_label_pos.y); draw_list->AddText(label_min, IM_COL32(64, 64, 64, 255), key_data->Label); - if (ImGui::IsKeyDown(key_data->Key)) + if (IsKeyDown(key_data->Key)) draw_list->AddRectFilled(key_min, key_max, IM_COL32(255, 0, 0, 128), key_rounding); } draw_list->PopClipRect(); @@ -13252,7 +13582,7 @@ void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list) void ImGui::DebugTextEncoding(const char* str) { Text("Text: \"%s\"", str); - if (!BeginTable("list", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) + if (!BeginTable("##DebugTextEncoding", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Resizable)) return; TableSetupColumn("Offset"); TableSetupColumn("UTF-8"); @@ -13288,7 +13618,7 @@ void ImGui::DebugTextEncoding(const char* str) static void MetricsHelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) && ImGui::BeginTooltip()) + if (ImGui::BeginItemTooltip()) { ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); ImGui::TextUnformatted(desc); @@ -13383,7 +13713,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) else if (rect_type == WRT_InnerRect) { return window->InnerRect; } else if (rect_type == WRT_InnerClipRect) { return window->InnerClipRect; } else if (rect_type == WRT_WorkRect) { return window->WorkRect; } - else if (rect_type == WRT_Content) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } + else if (rect_type == WRT_Content) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSize); } else if (rect_type == WRT_ContentIdeal) { ImVec2 min = window->InnerRect.Min - window->Scroll + window->WindowPadding; return ImRect(min, min + window->ContentSizeIdeal); } else if (rect_type == WRT_ContentRegionRect) { return window->ContentRegionRect; } IM_ASSERT(0); @@ -13620,11 +13950,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("\"%s\"", g.IO.IniFilename); else TextUnformatted(""); + Checkbox("io.ConfigDebugIniSettings", &io.ConfigDebugIniSettings); Text("SettingsDirtyTimer %.2f", g.SettingsDirtyTimer); if (TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) { for (int n = 0; n < g.SettingsHandlers.Size; n++) - BulletText("%s", g.SettingsHandlers[n].TypeName); + BulletText("\"%s\"", g.SettingsHandlers[n].TypeName); TreePop(); } if (TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) @@ -13687,6 +14018,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("Mouse clicked:"); for (int i = 0; i < count; i++) if (IsMouseClicked(i)) { SameLine(); Text("b%d (%d)", i, io.MouseClickedCount[i]); } Text("Mouse released:"); for (int i = 0; i < count; i++) if (IsMouseReleased(i)) { SameLine(); Text("b%d", i); } Text("Mouse wheel: %.1f", io.MouseWheel); + Text("MouseStationaryTimer: %.2f", g.MouseStationaryTimer); + Text("Mouse source: %s", GetMouseSourceName(io.MouseSource)); Text("Pen Pressure: %.1f", io.PenPressure); // Note: currently unused Unindent(); } @@ -13707,7 +14040,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) { for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { - ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(key); + ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); if (owner_data->OwnerCurr == ImGuiKeyOwner_None) continue; Text("%s: 0x%08X%s", GetKeyName(key), owner_data->OwnerCurr, @@ -13761,7 +14094,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("ActiveIdWindow: '%s'", g.ActiveIdWindow ? g.ActiveIdWindow->Name : "NULL"); Text("ActiveIdUsing: AllKeyboardKeys: %d, NavDirMask: %X", g.ActiveIdUsingAllKeyboardKeys, g.ActiveIdUsingNavDirMask); Text("HoveredId: 0x%08X (%.2f sec), AllowOverlap: %d", g.HoveredIdPreviousFrame, g.HoveredIdTimer, g.HoveredIdAllowOverlap); // Not displaying g.HoveredId as it is update mid-frame - Text("HoverDelayId: 0x%08X, Timer: %.2f, ClearTimer: %.2f", g.HoverDelayId, g.HoverDelayTimer, g.HoverDelayClearTimer); + Text("HoverItemDelayId: 0x%08X, Timer: %.2f, ClearTimer: %.2f", g.HoverItemDelayId, g.HoverItemDelayTimer, g.HoverItemDelayClearTimer); Text("DragDrop: %d, SourceId = 0x%08X, Payload \"%s\" (%d bytes)", g.DragDropActive, g.DragDropPayload.SourceId, g.DragDropPayload.DataType, g.DragDropPayload.DataSize); DebugLocateItemOnHover(g.DragDropPayload.SourceId); Unindent(); @@ -14102,13 +14435,11 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) char* p = buf; const char* buf_end = buf + IM_ARRAYSIZE(buf); const bool is_active = (tab_bar->PrevFrameVisible >= GetFrameCount() - 2); - p += ImFormatString(p, buf_end - p, "%s 0x%08X (%d tabs)%s", label, tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); - p += ImFormatString(p, buf_end - p, " { "); + p += ImFormatString(p, buf_end - p, "%s 0x%08X (%d tabs)%s {", label, tab_bar->ID, tab_bar->Tabs.Size, is_active ? "" : " *Inactive*"); for (int tab_n = 0; tab_n < ImMin(tab_bar->Tabs.Size, 3); tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[tab_n]; - p += ImFormatString(p, buf_end - p, "%s'%s'", - tab_n > 0 ? ", " : "", TabBarGetTabName(tab_bar, tab)); + p += ImFormatString(p, buf_end - p, "%s'%s'", tab_n > 0 ? ", " : "", TabBarGetTabName(tab_bar, tab)); } p += ImFormatString(p, buf_end - p, (tab_bar->Tabs.Size > 3) ? " ... }" : " } "); if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } @@ -14198,6 +14529,9 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) BulletText("NavLastIds[%d]: 0x%08X at +(%.1f,%.1f)(%.1f,%.1f)", layer, window->NavLastIds[layer], r.Min.x, r.Min.y, r.Max.x, r.Max.y); DebugLocateItemOnHover(window->NavLastIds[layer]); } + const ImVec2* pr = window->NavPreferredScoringPosRel; + for (int layer = 0; layer < ImGuiNavLayer_COUNT; layer++) + BulletText("NavPreferredScoringPosRel[%d] = {%.1f,%.1f)", layer, (pr[layer].x == FLT_MAX ? -99999.0f : pr[layer].x), (pr[layer].y == FLT_MAX ? -99999.0f : pr[layer].y)); // Display as 99999.0f so it looks neater. BulletText("NavLayersActiveMask: %X, NavLastChildNavWindow: %s", window->DC.NavLayersActiveMask, window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } if (window->ParentWindow != NULL) { DebugNodeWindow(window->ParentWindow, "ParentWindow"); } @@ -14287,14 +14621,13 @@ void ImGui::ShowDebugLogWindow(bool* p_open) return; } - AlignTextToFramePadding(); - Text("Log events:"); - SameLine(); CheckboxFlags("All", &g.DebugLogFlags, ImGuiDebugLogFlags_EventMask_); + CheckboxFlags("All", &g.DebugLogFlags, ImGuiDebugLogFlags_EventMask_); SameLine(); CheckboxFlags("ActiveId", &g.DebugLogFlags, ImGuiDebugLogFlags_EventActiveId); SameLine(); CheckboxFlags("Focus", &g.DebugLogFlags, ImGuiDebugLogFlags_EventFocus); SameLine(); CheckboxFlags("Popup", &g.DebugLogFlags, ImGuiDebugLogFlags_EventPopup); SameLine(); CheckboxFlags("Nav", &g.DebugLogFlags, ImGuiDebugLogFlags_EventNav); SameLine(); if (CheckboxFlags("Clipper", &g.DebugLogFlags, ImGuiDebugLogFlags_EventClipper)) { g.DebugLogClipperAutoDisableFrames = 2; } if (IsItemHovered()) SetTooltip("Clipper log auto-disabled after 2 frames"); + //SameLine(); CheckboxFlags("Selection", &g.DebugLogFlags, ImGuiDebugLogFlags_EventSelection); SameLine(); CheckboxFlags("IO", &g.DebugLogFlags, ImGuiDebugLogFlags_EventIO); if (SmallButton("Clear")) @@ -14317,7 +14650,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) TextUnformatted(line_begin, line_end); ImRect text_rect = g.LastItemData.Rect; if (IsItemHovered()) - for (const char* p = line_begin; p < line_end - 10; p++) + for (const char* p = line_begin; p <= line_end - 10; p++) { ImGuiID id = 0; if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X') || sscanf(p + 2, "%X", &id) != 1) diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 6f6e73d4..431cd4c7 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,10 +1,12 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (demo code) // Help: -// - Read FAQ at http://dearimgui.org/faq -// - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. +// - Read FAQ at http://dearimgui.com/faq // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. +// - Need help integrating Dear ImGui in your codebase? +// - Read Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started +// - Read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. // Read imgui.cpp for more details, documentation and comments. // Get the latest version at https://github.com/ocornut/imgui @@ -211,7 +213,7 @@ static void ShowDemoWindowInputs(); static void HelpMarker(const char* desc) { ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort) && ImGui::BeginTooltip()) + if (ImGui::BeginItemTooltip()) { ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f); ImGui::TextUnformatted(desc); @@ -398,23 +400,21 @@ void ImGui::ShowDemoWindow(bool* p_open) IMGUI_DEMO_MARKER("Help"); if (ImGui::CollapsingHeader("Help")) { - ImGui::Text("ABOUT THIS DEMO:"); + ImGui::SeparatorText("ABOUT THIS DEMO:"); ImGui::BulletText("Sections below are demonstrating many aspects of the library."); ImGui::BulletText("The \"Examples\" menu above leads to more demo contents."); ImGui::BulletText("The \"Tools\" menu above gives access to: About Box, Style Editor,\n" "and Metrics/Debugger (general purpose Dear ImGui debugging tool)."); - ImGui::Separator(); - ImGui::Text("PROGRAMMER GUIDE:"); + ImGui::SeparatorText("PROGRAMMER GUIDE:"); ImGui::BulletText("See the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); ImGui::BulletText("See comments in imgui.cpp."); ImGui::BulletText("See example applications in the examples/ folder."); - ImGui::BulletText("Read the FAQ at http://www.dearimgui.org/faq/"); + ImGui::BulletText("Read the FAQ at http://www.dearimgui.com/faq/"); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); - ImGui::Separator(); - ImGui::Text("USER GUIDE:"); + ImGui::SeparatorText("USER GUIDE:"); ImGui::ShowUserGuide(); } @@ -471,6 +471,10 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover"); ImGui::Checkbox("io.ConfigDebugBeginReturnValueLoop", &io.ConfigDebugBeginReturnValueLoop); ImGui::SameLine(); HelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running."); + ImGui::Checkbox("io.ConfigDebugIgnoreFocusLoss", &io.ConfigDebugIgnoreFocusLoss); + ImGui::SameLine(); HelpMarker("Option to deactivate io.AddFocusEvent(false) handling. May facilitate interactions with a debugger when focus loss leads to clearing inputs data."); + ImGui::Checkbox("io.ConfigDebugIniSettings", &io.ConfigDebugIniSettings); + ImGui::SameLine(); HelpMarker("Option to save .ini data with extra comments (particularly helpful for Docking, but makes saving slower)."); ImGui::TreePop(); ImGui::Spacing(); @@ -483,13 +487,13 @@ void ImGui::ShowDemoWindow(bool* p_open) "Those flags are set by the backends (imgui_impl_xxx files) to specify their capabilities.\n" "Here we expose them as read-only fields to avoid breaking interactions with your backend."); - // Make a local copy to avoid modifying actual backend flags. - // FIXME: We don't use BeginDisabled() to keep label bright, maybe we need a BeginReadonly() equivalent.. - ImGuiBackendFlags backend_flags = io.BackendFlags; - ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", &backend_flags, ImGuiBackendFlags_HasGamepad); - ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &backend_flags, ImGuiBackendFlags_HasMouseCursors); - ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &backend_flags, ImGuiBackendFlags_HasSetMousePos); - ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &backend_flags, ImGuiBackendFlags_RendererHasVtxOffset); + // FIXME: Maybe we need a BeginReadonly() equivalent to keep label bright? + ImGui::BeginDisabled(); + ImGui::CheckboxFlags("io.BackendFlags: HasGamepad", &io.BackendFlags, ImGuiBackendFlags_HasGamepad); + ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &io.BackendFlags, ImGuiBackendFlags_HasMouseCursors); + ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &io.BackendFlags, ImGuiBackendFlags_HasSetMousePos); + ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &io.BackendFlags, ImGuiBackendFlags_RendererHasVtxOffset); + ImGui::EndDisabled(); ImGui::TreePop(); ImGui::Spacing(); } @@ -624,37 +628,8 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); ImGui::Text("%d", counter); - { - // Tooltips - IMGUI_DEMO_MARKER("Widgets/Basic/Tooltips"); - //ImGui::AlignTextToFramePadding(); - ImGui::Text("Tooltips:"); - - ImGui::SameLine(); - ImGui::SmallButton("Button"); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("I am a tooltip"); - - ImGui::SameLine(); - ImGui::SmallButton("Fancy"); - if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) - { - ImGui::Text("I am a fancy tooltip"); - static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; - ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); - ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime())); - ImGui::EndTooltip(); - } - - ImGui::SameLine(); - ImGui::SmallButton("Delayed"); - if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal)) // With a delay - ImGui::SetTooltip("I am a tooltip with a delay."); - - ImGui::SameLine(); - HelpMarker( - "Tooltip are created by using the IsItemHovered() function over any kind of item."); - } + ImGui::Button("Tooltip"); + ImGui::SetItemTooltip("I am a tooltip"); ImGui::LabelText("label", "Value"); @@ -744,7 +719,7 @@ static void ShowDemoWindowWidgets() static int elem = Element_Fire; const char* elems_names[Element_COUNT] = { "Fire", "Earth", "Air", "Water" }; const char* elem_name = (elem >= 0 && elem < Element_COUNT) ? elems_names[elem] : "Unknown"; - ImGui::SliderInt("slider enum", &elem, 0, Element_COUNT - 1, elem_name); + ImGui::SliderInt("slider enum", &elem, 0, Element_COUNT - 1, elem_name); // Use ImGuiSliderFlags_NoInput flag to disable CTRL+Click here. ImGui::SameLine(); HelpMarker("Using the format string parameter to display a name instead of the underlying integer."); } @@ -789,6 +764,85 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Tooltips"); + if (ImGui::TreeNode("Tooltips")) + { + // Tooltips are windows following the mouse. They do not take focus away. + ImGui::SeparatorText("General"); + + // Typical use cases: + // - Short-form (text only): SetItemTooltip("Hello"); + // - Short-form (any contents): if (BeginItemTooltip()) { Text("Hello"); EndTooltip(); } + + // - Full-form (text only): if (IsItemHovered(...)) { SetTooltip("Hello"); } + // - Full-form (any contents): if (IsItemHovered(...) && BeginTooltip()) { Text("Hello"); EndTooltip(); } + + HelpMarker( + "Tooltip are typically created by using a IsItemHovered() + SetTooltip() sequence.\n\n" + "We provide a helper SetItemTooltip() function to perform the two with standards flags."); + + ImVec2 sz = ImVec2(-FLT_MIN, 0.0f); + + ImGui::Button("Basic", sz); + ImGui::SetItemTooltip("I am a tooltip"); + + ImGui::Button("Fancy", sz); + if (ImGui::BeginItemTooltip()) + { + ImGui::Text("I am a fancy tooltip"); + static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; + ImGui::PlotLines("Curve", arr, IM_ARRAYSIZE(arr)); + ImGui::Text("Sin(time) = %f", sinf((float)ImGui::GetTime())); + ImGui::EndTooltip(); + } + + ImGui::SeparatorText("Always On"); + + // Showcase NOT relying on a IsItemHovered() to emit a tooltip. + // Here the tooltip is always emitted when 'always_on == true'. + static int always_on = 0; + ImGui::RadioButton("Off", &always_on, 0); + ImGui::SameLine(); + ImGui::RadioButton("Always On (Simple)", &always_on, 1); + ImGui::SameLine(); + ImGui::RadioButton("Always On (Advanced)", &always_on, 2); + if (always_on == 1) + ImGui::SetTooltip("I am following you around."); + else if (always_on == 2 && ImGui::BeginTooltip()) + { + ImGui::ProgressBar(sinf((float)ImGui::GetTime()) * 0.5f + 0.5f, ImVec2(ImGui::GetFontSize() * 25, 0.0f)); + ImGui::EndTooltip(); + } + + ImGui::SeparatorText("Custom"); + + // The following examples are passed for documentation purpose but may not be useful to most users. + // Passing ImGuiHoveredFlags_Tooltip to IsItemHovered() will pull ImGuiHoveredFlags flags values from + // 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on whether mouse or gamepad/keyboard is being used. + // With default settings, ImGuiHoveredFlags_Tooltip is equivalent to ImGuiHoveredFlags_DelayShort + ImGuiHoveredFlags_Stationary. + ImGui::Button("Manual", sz); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip)) + ImGui::SetTooltip("I am a manually emitted tooltip"); + + ImGui::Button("DelayNone", sz); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNone)) + ImGui::SetTooltip("I am a tooltip with no delay."); + + ImGui::Button("DelayShort", sz); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_NoSharedDelay)) + ImGui::SetTooltip("I am a tooltip with a short delay (%0.2f sec).", ImGui::GetStyle().HoverDelayShort); + + ImGui::Button("DelayLong", sz); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay)) + ImGui::SetTooltip("I am a tooltip with a long delay (%0.2f sec)", ImGui::GetStyle().HoverDelayNormal); + + ImGui::Button("Stationary", sz); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary)) + ImGui::SetTooltip("I am a tooltip requiring mouse to be stationary before activating."); + + ImGui::TreePop(); + } + // Testing ImGuiOnceUponAFrame helper. //static ImGuiOnceUponAFrame once; //for (int i = 0; i < 5; i++) @@ -1054,7 +1108,7 @@ static void ShowDemoWindowWidgets() ImVec4 tint_col = use_text_color_for_tint ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint ImVec4 border_col = ImGui::GetStyleColorVec4(ImGuiCol_Border); ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, tint_col, border_col); - if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) + if (ImGui::BeginItemTooltip()) { float region_sz = 32.0f; float region_x = io.MousePos.x - pos.x - region_sz * 0.5f; @@ -1390,7 +1444,15 @@ static void ShowDemoWindowWidgets() { struct TextFilters { - // Return 0 (pass) if the character is 'i' or 'm' or 'g' or 'u' or 'i' + // Modify character input by altering 'data->Eventchar' (ImGuiInputTextFlags_CallbackCharFilter callback) + static int FilterCasingSwap(ImGuiInputTextCallbackData* data) + { + if (data->EventChar >= 'a' && data->EventChar <= 'z') { data->EventChar -= 'a' - 'A'; } // Lowercase becomes uppercase + else if (data->EventChar >= 'A' && data->EventChar <= 'Z') { data->EventChar += 'a' - 'A'; } // Uppercase becomes lowercase + return 0; + } + + // Return 0 (pass) if the character is 'i' or 'm' or 'g' or 'u' or 'i', otherwise return 1 (filter out) static int FilterImGuiLetters(ImGuiInputTextCallbackData* data) { if (data->EventChar < 256 && strchr("imgui", (char)data->EventChar)) @@ -1399,12 +1461,13 @@ static void ShowDemoWindowWidgets() } }; - static char buf1[64] = ""; ImGui::InputText("default", buf1, 64); - static char buf2[64] = ""; ImGui::InputText("decimal", buf2, 64, ImGuiInputTextFlags_CharsDecimal); - static char buf3[64] = ""; ImGui::InputText("hexadecimal", buf3, 64, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase); - static char buf4[64] = ""; ImGui::InputText("uppercase", buf4, 64, ImGuiInputTextFlags_CharsUppercase); - static char buf5[64] = ""; ImGui::InputText("no blank", buf5, 64, ImGuiInputTextFlags_CharsNoBlank); - static char buf6[64] = ""; ImGui::InputText("\"imgui\" letters", buf6, 64, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); + static char buf1[32] = ""; ImGui::InputText("default", buf1, 32); + static char buf2[32] = ""; ImGui::InputText("decimal", buf2, 32, ImGuiInputTextFlags_CharsDecimal); + static char buf3[32] = ""; ImGui::InputText("hexadecimal", buf3, 32, ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase); + static char buf4[32] = ""; ImGui::InputText("uppercase", buf4, 32, ImGuiInputTextFlags_CharsUppercase); + static char buf5[32] = ""; ImGui::InputText("no blank", buf5, 32, ImGuiInputTextFlags_CharsNoBlank); + static char buf6[32] = ""; ImGui::InputText("casing swap", buf6, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterCasingSwap); // Use CharFilter callback to replace characters. + static char buf7[32] = ""; ImGui::InputText("\"imgui\"", buf7, 32, ImGuiInputTextFlags_CallbackCharFilter, TextFilters::FilterImGuiLetters); // Use CharFilter callback to disable some characters. ImGui::TreePop(); } @@ -2366,8 +2429,10 @@ static void ShowDemoWindowWidgets() if (item_type == 15){ const char* items[] = { "Apple", "Banana", "Cherry", "Kiwi" }; static int current = 1; ret = ImGui::ListBox("ITEM: ListBox", ¤t, items, IM_ARRAYSIZE(items), IM_ARRAYSIZE(items)); } bool hovered_delay_none = ImGui::IsItemHovered(); + bool hovered_delay_stationary = ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary); bool hovered_delay_short = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayShort); bool hovered_delay_normal = ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal); + bool hovered_delay_tooltip = ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip); // = Normal + Stationary // Display the values of IsItemHovered() and other common item state functions. // Note that the ImGuiHoveredFlags_XXX flags can be combined. @@ -2379,7 +2444,8 @@ static void ShowDemoWindowWidgets() "IsItemHovered() = %d\n" "IsItemHovered(_AllowWhenBlockedByPopup) = %d\n" "IsItemHovered(_AllowWhenBlockedByActiveItem) = %d\n" - "IsItemHovered(_AllowWhenOverlapped) = %d\n" + "IsItemHovered(_AllowWhenOverlappedByItem) = %d\n" + "IsItemHovered(_AllowWhenOverlappedByWindow) = %d\n" "IsItemHovered(_AllowWhenDisabled) = %d\n" "IsItemHovered(_RectOnly) = %d\n" "IsItemActive() = %d\n" @@ -2398,7 +2464,8 @@ static void ShowDemoWindowWidgets() ImGui::IsItemHovered(), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), - ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlapped), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlappedByItem), + ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenOverlappedByWindow), ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled), ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly), ImGui::IsItemActive(), @@ -2414,7 +2481,13 @@ static void ShowDemoWindowWidgets() ImGui::GetItemRectSize().x, ImGui::GetItemRectSize().y ); ImGui::BulletText( - "w/ Hovering Delay: None = %d, Fast %d, Normal = %d", hovered_delay_none, hovered_delay_short, hovered_delay_normal); + "with Hovering Delay or Stationary test:\n" + "IsItemHovered() = = %d\n" + "IsItemHovered(_Stationary) = %d\n" + "IsItemHovered(_DelayShort) = %d\n" + "IsItemHovered(_DelayNormal) = %d\n" + "IsItemHovered(_Tooltip) = %d", + hovered_delay_none, hovered_delay_stationary, hovered_delay_short, hovered_delay_normal, hovered_delay_tooltip); if (item_disabled) ImGui::EndDisabled(); @@ -2466,7 +2539,8 @@ static void ShowDemoWindowWidgets() "IsWindowHovered(_RootWindow) = %d\n" "IsWindowHovered(_RootWindow|_NoPopupHierarchy) = %d\n" "IsWindowHovered(_ChildWindows|_AllowWhenBlockedByPopup) = %d\n" - "IsWindowHovered(_AnyWindow) = %d\n", + "IsWindowHovered(_AnyWindow) = %d\n" + "IsWindowHovered(_Stationary) = %d\n", ImGui::IsWindowHovered(), ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup), ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem), @@ -2477,7 +2551,8 @@ static void ShowDemoWindowWidgets() ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow), ImGui::IsWindowHovered(ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_NoPopupHierarchy), ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_AllowWhenBlockedByPopup), - ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow)); + ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow), + ImGui::IsWindowHovered(ImGuiHoveredFlags_Stationary)); ImGui::BeginChild("child", ImVec2(0, 50), true); ImGui::Text("This is another child window for testing the _ChildWindows flag."); @@ -2775,7 +2850,7 @@ static void ShowDemoWindowLayout() ImGui::PushID(i); ImGui::ListBox("", &selection[i], items, IM_ARRAYSIZE(items)); ImGui::PopID(); - //if (ImGui::IsItemHovered()) ImGui::SetTooltip("ListBox %d hovered", i); + //ImGui::SetItemTooltip("ListBox %d hovered", i); } ImGui::PopItemWidth(); @@ -2828,8 +2903,7 @@ static void ShowDemoWindowLayout() ImGui::SameLine(); ImGui::Button("EEE"); ImGui::EndGroup(); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("First group hovered"); + ImGui::SetItemTooltip("First group hovered"); } // Capture the group size and create widgets using the same size ImVec2 size = ImGui::GetItemRectSize(); @@ -3390,8 +3464,7 @@ static void ShowDemoWindowPopups() ImGui::Separator(); ImGui::Text("Tooltip here"); - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("I am a tooltip over a popup"); + ImGui::SetItemTooltip("I am a tooltip over a popup"); if (ImGui::Button("Stacked Popup")) ImGui::OpenPopup("another popup"); @@ -3475,8 +3548,7 @@ static void ShowDemoWindowPopups() ImGui::CloseCurrentPopup(); ImGui::EndPopup(); } - if (ImGui::IsItemHovered()) - ImGui::SetTooltip("Right-click to open popup"); + ImGui::SetItemTooltip("Right-click to open popup"); } } @@ -3728,7 +3800,7 @@ static void EditTableSizingFlags(ImGuiTableFlags* p_flags) } ImGui::SameLine(); ImGui::TextDisabled("(?)"); - if (ImGui::IsItemHovered() && ImGui::BeginTooltip()) + if (ImGui::BeginItemTooltip()) { ImGui::PushTextWrapPos(ImGui::GetFontSize() * 50.0f); for (int m = 0; m < IM_ARRAYSIZE(policies); m++) @@ -5426,7 +5498,7 @@ static void ShowDemoWindowTables() ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); else if (contents_type == CT_Selectable || contents_type == CT_SelectableSpanRow) { - ImGuiSelectableFlags selectable_flags = (contents_type == CT_SelectableSpanRow) ? ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap : ImGuiSelectableFlags_None; + ImGuiSelectableFlags selectable_flags = (contents_type == CT_SelectableSpanRow) ? ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap : ImGuiSelectableFlags_None; if (ImGui::Selectable(label, item_is_selected, selectable_flags, ImVec2(0, row_min_height))) { if (ImGui::GetIO().KeyCtrl) @@ -5713,8 +5785,6 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } -namespace ImGui { extern ImGuiKeyData* GetKeyData(ImGuiKey key); } - static void ShowDemoWindowInputs() { IMGUI_DEMO_MARKER("Inputs & Focus"); @@ -5749,7 +5819,7 @@ static void ShowDemoWindowInputs() struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array ImGuiKey start_key = (ImGuiKey)0; #endif - ImGui::Text("Keys down:"); for (ImGuiKey key = start_key; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); ImGui::SameLine(); ImGui::Text("(%.02f)", ImGui::GetKeyData(key)->DownDuration); } + ImGui::Text("Keys down:"); for (ImGuiKey key = start_key; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); } ImGui::Text("Keys mods: %s%s%s%s", io.KeyCtrl ? "CTRL " : "", io.KeyShift ? "SHIFT " : "", io.KeyAlt ? "ALT " : "", io.KeySuper ? "SUPER " : ""); ImGui::Text("Chars queue:"); for (int i = 0; i < io.InputQueueCharacters.Size; i++) { ImWchar c = io.InputQueueCharacters[i]; ImGui::SameLine(); ImGui::Text("\'%c\' (0x%04X)", (c > ' ' && c <= 255) ? (char)c : '?', c); } // FIXME: We should convert 'c' to UTF-8 here but the functions are not public. @@ -5931,10 +6001,11 @@ void ImGui::ShowAboutWindow(bool* p_open) return; } IMGUI_DEMO_MARKER("Tools/About Dear ImGui"); - ImGui::Text("Dear ImGui %s", ImGui::GetVersion()); + ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); ImGui::Separator(); ImGui::Text("By Omar Cornut and all Dear ImGui contributors."); ImGui::Text("Dear ImGui is licensed under the MIT License, see LICENSE for more information."); + ImGui::Text("If your company uses this, please consider sponsoring the project!"); static bool show_config_info = false; ImGui::Checkbox("Config/Build Information", &show_config_info); @@ -6209,11 +6280,25 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SameLine(); HelpMarker("Alignment applies when a selectable is larger than its text content."); ImGui::SliderFloat("SeparatorTextBorderSize", &style.SeparatorTextBorderSize, 0.0f, 10.0f, "%.0f"); ImGui::SliderFloat2("SeparatorTextAlign", (float*)&style.SeparatorTextAlign, 0.0f, 1.0f, "%.2f"); - ImGui::SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%0.f"); + ImGui::SliderFloat2("SeparatorTextPadding", (float*)&style.SeparatorTextPadding, 0.0f, 40.0f, "%.0f"); ImGui::SliderFloat("LogSliderDeadzone", &style.LogSliderDeadzone, 0.0f, 12.0f, "%.0f"); + ImGui::SeparatorText("Tooltips"); + for (int n = 0; n < 2; n++) + if (ImGui::TreeNodeEx(n == 0 ? "HoverFlagsForTooltipMouse" : "HoverFlagsForTooltipNav")) + { + ImGuiHoveredFlags* p = (n == 0) ? &style.HoverFlagsForTooltipMouse : &style.HoverFlagsForTooltipNav; + ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNone", p, ImGuiHoveredFlags_DelayNone); + ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayShort", p, ImGuiHoveredFlags_DelayShort); + ImGui::CheckboxFlags("ImGuiHoveredFlags_DelayNormal", p, ImGuiHoveredFlags_DelayNormal); + ImGui::CheckboxFlags("ImGuiHoveredFlags_Stationary", p, ImGuiHoveredFlags_Stationary); + ImGui::CheckboxFlags("ImGuiHoveredFlags_NoSharedDelay", p, ImGuiHoveredFlags_NoSharedDelay); + ImGui::TreePop(); + } + ImGui::SeparatorText("Misc"); ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); + ImGui::EndTabItem(); } @@ -7443,7 +7528,7 @@ static void ShowExampleAppFullscreen(bool* p_open) static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings; // We demonstrate using the full viewport area or the work area (without menu-bars, task-bars etc.) - // Based on your use case you may want one of the other. + // Based on your use case you may want one or the other. const ImGuiViewport* viewport = ImGui::GetMainViewport(); ImGui::SetNextWindowPos(use_work_area ? viewport->WorkPos : viewport->Pos); ImGui::SetNextWindowSize(use_work_area ? viewport->WorkSize : viewport->Size); @@ -7995,8 +8080,8 @@ void ShowExampleAppDocuments(bool* p_open) for (int n = 0; n < close_queue.Size; n++) if (close_queue[n]->Dirty) ImGui::Text("%s", close_queue[n]->Name); - ImGui::EndChildFrame(); } + ImGui::EndChildFrame(); ImVec2 button_size(ImGui::GetFontSize() * 7.0f, 0.0f); if (ImGui::Button("Yes", button_size)) diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 031c3e7d..a50255b5 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (drawing and font code) /* @@ -2553,13 +2553,10 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) // 9. Setup ImFont and glyphs for runtime for (int src_i = 0; src_i < src_tmp_array.Size; src_i++) { - ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; - if (src_tmp.GlyphsCount == 0) - continue; - // When merging fonts with MergeMode=true: // - We can have multiple input fonts writing into a same destination font. // - dst_font->ConfigData is != from cfg which is our source configuration. + ImFontBuildSrcData& src_tmp = src_tmp_array[src_i]; ImFontConfig& cfg = atlas->ConfigData[src_i]; ImFont* dst_font = cfg.DstFont; @@ -2623,6 +2620,9 @@ void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opa ImVector& user_rects = atlas->CustomRects; IM_ASSERT(user_rects.Size >= 1); // We expect at least the default custom rects to be registered, else something went wrong. +#ifdef __GNUC__ + if (user_rects.Size < 1) { __builtin_unreachable(); } // Workaround for GCC bug if IM_ASSERT() is defined to conditionally throw (see #5343) +#endif ImVector pack_rects; pack_rects.resize(user_rects.Size); @@ -3200,7 +3200,25 @@ void ImFont::BuildLookupTable() SetGlyphVisible((ImWchar)' ', false); SetGlyphVisible((ImWchar)'\t', false); - // Ellipsis character is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). + // Setup Fallback character + const ImWchar fallback_chars[] = { (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' }; + FallbackGlyph = FindGlyphNoFallback(FallbackChar); + if (FallbackGlyph == NULL) + { + FallbackChar = FindFirstExistingGlyph(this, fallback_chars, IM_ARRAYSIZE(fallback_chars)); + FallbackGlyph = FindGlyphNoFallback(FallbackChar); + if (FallbackGlyph == NULL) + { + FallbackGlyph = &Glyphs.back(); + FallbackChar = (ImWchar)FallbackGlyph->Codepoint; + } + } + FallbackAdvanceX = FallbackGlyph->AdvanceX; + for (int i = 0; i < max_codepoint + 1; i++) + if (IndexAdvanceX[i] < 0.0f) + IndexAdvanceX[i] = FallbackAdvanceX; + + // Setup Ellipsis character. It is required for rendering elided text. We prefer using U+2026 (horizontal ellipsis). // However some old fonts may contain ellipsis at U+0085. Here we auto-detect most suitable ellipsis character. // FIXME: Note that 0x2026 is rarely included in our font ranges. Because of this we are more likely to use three individual dots. const ImWchar ellipsis_chars[] = { (ImWchar)0x2026, (ImWchar)0x0085 }; @@ -3221,25 +3239,6 @@ void ImFont::BuildLookupTable() EllipsisCharStep = (glyph->X1 - glyph->X0) + 1.0f; EllipsisWidth = EllipsisCharStep * 3.0f - 1.0f; } - - // Setup fallback character - const ImWchar fallback_chars[] = { (ImWchar)IM_UNICODE_CODEPOINT_INVALID, (ImWchar)'?', (ImWchar)' ' }; - FallbackGlyph = FindGlyphNoFallback(FallbackChar); - if (FallbackGlyph == NULL) - { - FallbackChar = FindFirstExistingGlyph(this, fallback_chars, IM_ARRAYSIZE(fallback_chars)); - FallbackGlyph = FindGlyphNoFallback(FallbackChar); - if (FallbackGlyph == NULL) - { - FallbackGlyph = &Glyphs.back(); - FallbackChar = (ImWchar)FallbackGlyph->Codepoint; - } - } - - FallbackAdvanceX = FallbackGlyph->AdvanceX; - for (int i = 0; i < max_codepoint + 1; i++) - if (IndexAdvanceX[i] < 0.0f) - IndexAdvanceX[i] = FallbackAdvanceX; } // API is designed this way to avoid exposing the 4K page size @@ -3790,6 +3789,7 @@ void ImGui::RenderArrow(ImDrawList* draw_list, ImVec2 pos, ImU32 col, ImGuiDir d void ImGui::RenderBullet(ImDrawList* draw_list, ImVec2 pos, ImU32 col) { + // FIXME-OPT: This should be baked in font. draw_list->AddCircleFilled(pos, draw_list->_Data->FontSize * 0.20f, col, 8); } diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index 29d502b0..0858cddb 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -5,6 +5,7 @@ // Implemented features: // [X] Platform: Clipboard support. +// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only). // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). @@ -16,6 +17,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-06-12: Accept glfwGetTime() not returning a monotonically increasing value. This seems to happens on some Windows setup when peripherals disconnect, and is likely to also happen on browser + Emscripten. (#6491) +// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen on Windows ONLY, using a custom WndProc hook. (#2702) +// 2023-03-16: Inputs: Fixed key modifiers handling on secondary viewports (docking branch). Broken on 2023/01/04. (#6248, #6034) // 2023-03-14: Emscripten: Avoid using glfwGetError() and glfwGetGamepadState() which are not correctly implemented in Emscripten emulation. (#6240) // 2023-02-03: Emscripten: Registering custom low-level mouse wheel handler to get more accurate scrolling impulses on Emscripten. (#4019, #6096) // 2023-01-04: Inputs: Fixed mods state on Linux when using Alt-GR text input (e.g. German keyboard layout), could lead to broken text input. Revert a 2022/01/17 change were we resumed using mods provided by GLFW, turns out they were faulty. @@ -126,6 +130,9 @@ struct ImGui_ImplGlfw_Data GLFWkeyfun PrevUserCallbackKey; GLFWcharfun PrevUserCallbackChar; GLFWmonitorfun PrevUserCallbackMonitor; +#ifdef _WIN32 + WNDPROC GlfwWndProc; +#endif ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); } }; @@ -268,14 +275,13 @@ static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) // X11 does not include current pressed/released modifier key in 'mods' flags submitted by GLFW // See https://github.com/ocornut/imgui/issues/6034 and https://github.com/glfw/glfw/issues/1630 -static void ImGui_ImplGlfw_UpdateKeyModifiers() +static void ImGui_ImplGlfw_UpdateKeyModifiers(GLFWwindow* window) { ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - io.AddKeyEvent(ImGuiMod_Ctrl, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Shift, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Alt, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(bd->Window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(bd->Window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Ctrl, (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Shift, (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Alt, (glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); + io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); } static bool ImGui_ImplGlfw_ShouldChainCallback(GLFWwindow* window) @@ -290,7 +296,7 @@ void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int acti if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackMousebutton(window, button, action, mods); - ImGui_ImplGlfw_UpdateKeyModifiers(); + ImGui_ImplGlfw_UpdateKeyModifiers(window); ImGuiIO& io = ImGui::GetIO(); if (button >= 0 && button < ImGuiMouseButton_COUNT) @@ -326,7 +332,7 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) const char* key_name = glfwGetKeyName(key, scancode); glfwSetErrorCallback(prev_error_callback); #if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) - (void)glfwGetError(NULL); + (void)glfwGetError(nullptr); #endif if (key_name && key_name[0] != 0 && key_name[1] == 0) { @@ -354,7 +360,7 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i if (action != GLFW_PRESS && action != GLFW_RELEASE) return; - ImGui_ImplGlfw_UpdateKeyModifiers(); + ImGui_ImplGlfw_UpdateKeyModifiers(window); keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode); @@ -444,6 +450,35 @@ static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEven } #endif +#ifdef _WIN32 +// GLFW doesn't allow to distinguish Mouse vs TouchScreen vs Pen. +// Add support for Win32 (based on imgui_impl_win32), because we rely on _TouchScreen info to trickle inputs differently. +static ImGuiMouseSource GetMouseSourceFromMessageExtraInfo() +{ + LPARAM extra_info = ::GetMessageExtraInfo(); + if ((extra_info & 0xFFFFFF80) == 0xFF515700) + return ImGuiMouseSource_Pen; + if ((extra_info & 0xFFFFFF80) == 0xFF515780) + return ImGuiMouseSource_TouchScreen; + return ImGuiMouseSource_Mouse; +} +static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + switch (msg) + { + case WM_MOUSEMOVE: case WM_NCMOUSEMOVE: + case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: case WM_LBUTTONUP: + case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: case WM_RBUTTONUP: + case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: case WM_MBUTTONUP: + case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: case WM_XBUTTONUP: + ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo()); + break; + } + return ::CallWindowProc(bd->GlfwWndProc, hWnd, msg, wParam, lParam); +} +#endif + void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window) { ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); @@ -539,7 +574,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw #endif glfwSetErrorCallback(prev_error_callback); #if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) - (void)glfwGetError(NULL); + (void)glfwGetError(nullptr); #endif // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. @@ -549,7 +584,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves. // FIXME: May break chaining in case user registered their own Emscripten callback? #ifdef __EMSCRIPTEN__ - emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, NULL, false, ImGui_ImplEmscripten_WheelCallback); + emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, ImGui_ImplEmscripten_WheelCallback); #endif // Set platform dependent data in viewport @@ -562,6 +597,13 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw IM_UNUSED(main_viewport); #endif + // Windows: register a WndProc hook so we can intercept some messages. +#ifdef _WIN32 + bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); + IM_ASSERT(bd->GlfwWndProc != nullptr); + ::SetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); +#endif + bd->ClientApi = client_api; return true; } @@ -593,8 +635,16 @@ void ImGui_ImplGlfw_Shutdown() for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) glfwDestroyCursor(bd->MouseCursors[cursor_n]); + // Windows: register a WndProc hook so we can intercept some messages. +#ifdef _WIN32 + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ::SetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->GlfwWndProc); + bd->GlfwWndProc = nullptr; +#endif + io.BackendPlatformName = nullptr; io.BackendPlatformUserData = nullptr; + io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad); IM_DELETE(bd); } @@ -731,7 +781,10 @@ void ImGui_ImplGlfw_NewFrame() io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h); // Setup time step + // (Accept glfwGetTime() not returning a monotonically increasing value. Seems to happens on disconnecting peripherals and probably on VMs and Emscripten, see #6491, #6189, #6114, #3644) double current_time = glfwGetTime(); + if (current_time <= bd->Time) + current_time = bd->Time + 0.00001f; io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f); bd->Time = current_time; diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index 134479fd..f3184e75 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -7,6 +7,11 @@ // [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! // [x] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). +// About WebGL/ES: +// - You need to '#define IMGUI_IMPL_OPENGL_ES2' or '#define IMGUI_IMPL_OPENGL_ES3' to use WebGL or OpenGL ES. +// - This is done automatically on iOS, Android and Emscripten targets. +// - For other targets, the define needs to be visible from the imgui_impl_opengl3.cpp compilation unit. If unsure, define globally or in imconfig.h. + // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. @@ -14,6 +19,11 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-06-20: OpenGL: Fixed erroneous use glGetIntegerv(GL_CONTEXT_PROFILE_MASK) on contexts lower than 3.2. (#6539, #6333) +// 2023-05-09: OpenGL: Support for glBindSampler() backup/restore on ES3. (#6375) +// 2023-04-18: OpenGL: Restore front and back polygon mode separately when supported by context. (#6333) +// 2023-03-23: OpenGL: Properly restoring "no shader program bound" if it was the case prior to running the rendering function. (#6267, #6220, #6224) +// 2023-03-15: OpenGL: Fixed GL loader crash when GL_VERSION returns NULL. (#6154, #4445, #3530) // 2023-03-06: OpenGL: Fixed restoration of a potentially deleted OpenGL program, by calling glIsProgram(). (#6220, #6224) // 2022-11-09: OpenGL: Reverted use of glBufferSubData(), too many corruptions issues + old issues seemingly can't be reproed with Intel drivers nowadays (revert 2021-12-15 and 2022-05-23 changes). // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. @@ -105,17 +115,20 @@ #include #endif -// Clang warnings with -Weverything +// Clang/GCC warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used +#pragma clang diagnostic ignored "-Wnonportable-system-include-path" +#pragma clang diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader) #endif #if defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' -#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types +#pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader) #endif // GL includes @@ -170,8 +183,8 @@ #define IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET #endif -// Desktop GL 3.3+ has glBindSampler() -#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_3) +// Desktop GL 3.3+ and GL ES 3.0+ have glBindSampler() +#if !defined(IMGUI_IMPL_OPENGL_ES2) && (defined(IMGUI_IMPL_OPENGL_ES3) || defined(GL_VERSION_3_3)) #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER #endif @@ -199,6 +212,10 @@ struct ImGui_ImplOpenGL3_Data { GLuint GlVersion; // Extracted at runtime using GL_MAJOR_VERSION, GL_MINOR_VERSION queries (e.g. 320 for GL 3.2) char GlslVersionString[32]; // Specified by user or detected based on compile time GL settings. + bool GlProfileIsES2; + bool GlProfileIsES3; + bool GlProfileIsCompat; + GLint GlProfileMask; GLuint FontTexture; GLuint ShaderHandle; GLint AttribLocationTex; // Uniforms location @@ -267,7 +284,12 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) io.BackendRendererName = "imgui_impl_opengl3"; // Query for GL version (e.g. 320 for GL 3.2) -#if !defined(IMGUI_IMPL_OPENGL_ES2) +#if defined(IMGUI_IMPL_OPENGL_ES2) + // GLES 2 + bd->GlVersion = 200; + bd->GlProfileIsES2 = true; +#else + // Desktop or GLES 3 GLint major = 0; GLint minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); @@ -279,6 +301,15 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) sscanf(gl_version, "%d.%d", &major, &minor); } bd->GlVersion = (GLuint)(major * 100 + minor * 10); +#if defined(GL_CONTEXT_PROFILE_MASK) + if (bd->GlVersion >= 320) + glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask); + bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0; +#endif + +#if defined(IMGUI_IMPL_OPENGL_ES3) + bd->GlProfileIsES3 = true; +#endif bd->UseBufferSubData = false; /* @@ -289,12 +320,10 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) bd->UseBufferSubData = true; #endif */ -#else - bd->GlVersion = 200; // GLES 2 #endif #ifdef IMGUI_IMPL_OPENGL_DEBUG - printf("GL_MAJOR_VERSION = %d\nGL_MINOR_VERSION = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", major, minor, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] + printf("GlVersion = %d\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2 = %d, GlProfileIsES3 = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] #endif #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET @@ -350,6 +379,7 @@ void ImGui_ImplOpenGL3_Shutdown() ImGui_ImplOpenGL3_DestroyDeviceObjects(); io.BackendRendererName = nullptr; io.BackendRendererUserData = nullptr; + io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; IM_DELETE(bd); } @@ -415,8 +445,8 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (bd->GlVersion >= 330) - glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 may set that otherwise. + if (bd->GlVersion >= 330 || bd->GlProfileIsES3) + glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise. #endif (void)vertex_array_object; @@ -454,7 +484,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - GLuint last_sampler; if (bd->GlVersion >= 330) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } + GLuint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } #endif GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY @@ -578,10 +608,10 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Restore modified GL state // This "glIsProgram()" check is required because if the program is "pending deletion" at the time of binding backup, it will have been deleted by now and will cause an OpenGL error. See #6220. - if (glIsProgram(last_program)) glUseProgram(last_program); + if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (bd->GlVersion >= 330) + if (bd->GlVersion >= 330 || bd->GlProfileIsES3) glBindSampler(0, last_sampler); #endif glActiveTexture(last_active_texture); @@ -607,8 +637,18 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) #endif #ifdef IMGUI_IMPL_HAS_POLYGON_MODE - glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); -#endif + // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons + if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) + { + glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); + glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); + } + else + { + glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); + } +#endif // IMGUI_IMPL_HAS_POLYGON_MODE + glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); (void)bd; // Not all compilation paths use this diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 3bacc34a..d3967a5f 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (tables and columns code) /* @@ -80,20 +80,20 @@ Index of this file: // - outer_size.x <= 0.0f -> Right-align from window/work-rect right-most edge. With -FLT_MIN or 0.0f will align exactly on right-most edge. // - outer_size.x > 0.0f -> Set Fixed width. // Y with ScrollX/ScrollY disabled: we output table directly in current window -// - outer_size.y < 0.0f -> Bottom-align (but will auto extend, unless _NoHostExtendY is set). Not meaningful is parent window can vertically scroll. +// - outer_size.y < 0.0f -> Bottom-align (but will auto extend, unless _NoHostExtendY is set). Not meaningful if parent window can vertically scroll. // - outer_size.y = 0.0f -> No minimum height (but will auto extend, unless _NoHostExtendY is set) // - outer_size.y > 0.0f -> Set Minimum height (but will auto extend, unless _NoHostExtenY is set) // Y with ScrollX/ScrollY enabled: using a child window for scrolling -// - outer_size.y < 0.0f -> Bottom-align. Not meaningful is parent window can vertically scroll. +// - outer_size.y < 0.0f -> Bottom-align. Not meaningful if parent window can vertically scroll. // - outer_size.y = 0.0f -> Bottom-align, consistent with BeginChild(). Not recommended unless table is last item in parent window. // - outer_size.y > 0.0f -> Set Exact height. Recommended when using Scrolling on any axis. //----------------------------------------------------------------------------- // Outer size is also affected by the NoHostExtendX/NoHostExtendY flags. -// Important to that note how the two flags have slightly different behaviors! +// Important to note how the two flags have slightly different behaviors! // - ImGuiTableFlags_NoHostExtendX -> Make outer width auto-fit to columns (overriding outer_size.x value). Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. // - ImGuiTableFlags_NoHostExtendY -> Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY is disabled. Data below the limit will be clipped and not visible. // In theory ImGuiTableFlags_NoHostExtendY could be the default and any non-scrolling tables with outer_size.y != 0.0f would use exact height. -// This would be consistent but perhaps less useful and more confusing (as vertically clipped items are not easily noticeable) +// This would be consistent but perhaps less useful and more confusing (as vertically clipped items are not useful and not easily noticeable). //----------------------------------------------------------------------------- // About 'inner_width': // With ScrollX disabled: @@ -112,15 +112,16 @@ Index of this file: //----------------------------------------------------------------------------- // COLUMNS SIZING POLICIES +// (Reference: ImGuiTableFlags_SizingXXX flags and ImGuiTableColumnFlags_WidthXXX flags) //----------------------------------------------------------------------------- // About overriding column sizing policy and width/weight with TableSetupColumn(): -// We use a default parameter of 'init_width_or_weight == -1'. +// We use a default parameter of -1 for 'init_width'/'init_weight'. // - with ImGuiTableColumnFlags_WidthFixed, init_width <= 0 (default) --> width is automatic // - with ImGuiTableColumnFlags_WidthFixed, init_width > 0 (explicit) --> width is custom // - with ImGuiTableColumnFlags_WidthStretch, init_weight <= 0 (default) --> weight is 1.0f // - with ImGuiTableColumnFlags_WidthStretch, init_weight > 0 (explicit) --> weight is custom // Widths are specified _without_ CellPadding. If you specify a width of 100.0f, the column will be cover (100.0f + Padding * 2.0f) -// and you can fit a 100.0f wide item in it without clipping and with full padding. +// and you can fit a 100.0f wide item in it without clipping and with padding honored. //----------------------------------------------------------------------------- // About default sizing policy (if you don't specify a ImGuiTableColumnFlags_WidthXXXX flag) // - with Table policy ImGuiTableFlags_SizingFixedFit --> default Column policy is ImGuiTableColumnFlags_WidthFixed, default Width is equal to contents width @@ -134,10 +135,10 @@ Index of this file: // - using mixed policies with ScrollX does not make much sense, as using Stretch columns with ScrollX does not make much sense in the first place! // that is, unless 'inner_width' is passed to BeginTable() to explicitly provide a total width to layout columns in. // - when using ImGuiTableFlags_SizingFixedSame with mixed columns, only the Fixed/Auto columns will match their widths to the width of the maximum contents. -// - when using ImGuiTableFlags_SizingStretchSame with mixed columns, only the Stretch columns will match their weight/widths. +// - when using ImGuiTableFlags_SizingStretchSame with mixed columns, only the Stretch columns will match their weights/widths. //----------------------------------------------------------------------------- // About using column width: -// If a column is manual resizable or has a width specified with TableSetupColumn(): +// If a column is manually resizable or has a width specified with TableSetupColumn(): // - you may use GetContentRegionAvail().x to query the width available in a given column. // - right-side alignment features such as SetNextItemWidth(-x) or PushItemWidth(-x) will rely on this width. // If the column is not resizable and has no width specified with TableSetupColumn(): @@ -151,7 +152,7 @@ Index of this file: // TABLES CLIPPING/CULLING //----------------------------------------------------------------------------- // About clipping/culling of Rows in Tables: -// - For large numbers of rows, it is recommended you use ImGuiListClipper to only submit visible rows. +// - For large numbers of rows, it is recommended you use ImGuiListClipper to submit only visible rows. // ImGuiListClipper is reliant on the fact that rows are of equal height. // See 'Demo->Tables->Vertical Scrolling' or 'Demo->Tables->Advanced' for a demo of using the clipper. // - Note that auto-resizing columns don't play well with using the clipper. @@ -168,7 +169,7 @@ Index of this file: // - Case C: column is hidden explicitly by the user (e.g. via the context menu, or _DefaultHide column flag, etc.). // // [A] [B] [C] -// TableNextColumn(): true false false -> [userland] when TableNextColumn() / TableSetColumnIndex() return false, user can skip submitting items but only if the column doesn't contribute to row height. +// TableNextColumn(): true false false -> [userland] when TableNextColumn() / TableSetColumnIndex() returns false, user can skip submitting items but only if the column doesn't contribute to row height. // SkipItems: false false true -> [internal] when SkipItems is true, most widgets will early out if submitted, resulting is no layout output. // ClipRect: normal zero-width zero-width -> [internal] when ClipRect is zero, ItemAdd() will return false and most widgets will early out mid-way. // ImDrawList output: normal dummy dummy -> [internal] when using the dummy channel, ImDrawList submissions (if any) will be wasted (because cliprect is zero-width anyway). @@ -319,7 +320,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (flags & ImGuiTableFlags_ScrollX) IM_ASSERT(inner_width >= 0.0f); - // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping rules may evolve. + // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping criteria may evolve. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0; const ImVec2 avail_size = GetContentRegionAvail(); ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); @@ -366,7 +367,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG IM_ASSERT(table->ColumnsCount == columns_count && "BeginTable(): Cannot change columns count mid-frame while preserving same ID"); if (table->InstanceDataExtra.Size < instance_no) table->InstanceDataExtra.push_back(ImGuiTableInstanceData()); - instance_id = GetIDWithSeed(instance_no, GetIDWithSeed("##Instances", NULL, id)); // Push "##Instance" followed by (int)instance_no in ID stack. + instance_id = GetIDWithSeed(instance_no, GetIDWithSeed("##Instances", NULL, id)); // Push "##Instances" followed by (int)instance_no in ID stack. } else { @@ -477,12 +478,13 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->IsUnfrozenRows = true; table->DeclColumnsCount = 0; - // Using opaque colors facilitate overlapping elements of the grid + // Using opaque colors facilitate overlapping lines of the grid, otherwise we'd need to improve TableDrawBorders() table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong); table->BorderColorLight = GetColorU32(ImGuiCol_TableBorderLight); // Make table current g.CurrentTable = table; + outer_window->DC.NavIsScrollPushableX = false; // Shortcut for NavUpdateCurrentWindowIsScrollPushableX(); outer_window->DC.CurrentTableIdx = table_idx; if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly. inner_window->DC.CurrentTableIdx = table_idx; @@ -490,7 +492,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if ((table_last_flags & ImGuiTableFlags_Reorderable) && (flags & ImGuiTableFlags_Reorderable) == 0) table->IsResetDisplayOrderRequest = true; - // Mark as used + // Mark as used to avoid GC if (table_idx >= g.TablesLastTimeActive.Size) g.TablesLastTimeActive.resize(table_idx + 1, -1.0f); g.TablesLastTimeActive[table_idx] = (float)g.Time; @@ -583,13 +585,13 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG } // For reference, the average total _allocation count_ for a table is: -// + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables) +// + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables[]) // + 1 (for table->RawData allocated below) // + 1 (for table->ColumnsNames, if names are used) -// Shared allocations per number of nested tables +// Shared allocations for the maximum number of simultaneously nested tables (generally a very small number) // + 1 (for table->Splitter._Channels) // + 2 * active_channels_count (for ImDrawCmd and ImDrawIdx buffers inside channels) -// Where active_channels_count is variable but often == columns_count or columns_count + 1, see TableSetupDrawChannels() for details. +// Where active_channels_count is variable but often == columns_count or == columns_count + 1, see TableSetupDrawChannels() for details. // Unused channels don't perform their +2 allocations. void ImGui::TableBeginInitMemory(ImGuiTable* table, int columns_count) { @@ -616,7 +618,7 @@ void ImGui::TableBeginInitMemory(ImGuiTable* table, int columns_count) void ImGui::TableBeginApplyRequests(ImGuiTable* table) { // Handle resizing request - // (We process this at the first TableBegin of the frame) + // (We process this in the TableBegin() of the first instance of each table) // FIXME-TABLE: Contains columns if our work area doesn't allow for scrolling? if (table->InstanceCurrent == 0) { @@ -661,8 +663,7 @@ void ImGui::TableBeginApplyRequests(ImGuiTable* table) table->Columns[table->DisplayOrderToIndex[order_n]].DisplayOrder -= (ImGuiTableColumnIdx)reorder_dir; IM_ASSERT(dst_column->DisplayOrder == dst_order - reorder_dir); - // Display order is stored in both columns->IndexDisplayOrder and table->DisplayOrder[], - // rebuild the later from the former. + // Display order is stored in both columns->IndexDisplayOrder and table->DisplayOrder[]. Rebuild later from the former. for (int column_n = 0; column_n < table->ColumnsCount; column_n++) table->DisplayOrderToIndex[table->Columns[column_n].DisplayOrder] = (ImGuiTableColumnIdx)column_n; table->ReorderColumnDir = 0; @@ -736,8 +737,8 @@ static void TableSetupColumnFlags(ImGuiTable* table, ImGuiTableColumn* column, I } } -// Layout columns for the frame. This is in essence the followup to BeginTable(). -// Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() to be called first. +// Layout columns for the frame. This is in essence the followup to BeginTable() and this is our largest function. +// Runs on the first call to TableNextRow(), to give a chance for TableSetupColumn() and other TableSetupXXXXX() functions to be called first. // FIXME-TABLE: Our width (and therefore our WorkRect) will be minimal in the first frame for _WidthAuto columns. // Increase feedback side-effect with widgets relying on WorkRect.Max.x... Maybe provide a default distribution for _WidthAuto columns? void ImGui::TableUpdateLayout(ImGuiTable* table) @@ -858,8 +859,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->IsSettingsDirty = true; // [Part 3] Fix column flags and record a few extra information. - float sum_width_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns but including spacing/padding. - float stretch_sum_weights = 0.0f; // Sum of all weights for stretch columns. + float sum_width_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns but including spacing/padding. + float stretch_sum_weights = 0.0f; // Sum of all weights for stretch columns. table->LeftMostStretchedColumn = table->RightMostStretchedColumn = -1; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) { @@ -975,7 +976,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table_instance->LastOuterHeight)); const ImGuiID backup_active_id = g.ActiveId; g.ActiveId = 0; - const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0); + const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0, ImGuiItemFlags_None); g.ActiveId = backup_active_id; // [Part 6] Setup final position, offset, skip/clip states and clipping rectangles, detect hovered column @@ -1144,7 +1145,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) EndPopup(); } - // [Part 12] Sanitize and build sort specs before we have a change to use them for display. + // [Part 12] Sanitize and build sort specs before we have a chance to use them for display. // This path will only be exercised when sort specs are modified before header rows (e.g. init or visibility change) if (table->IsSortSpecsDirty && (table->Flags & ImGuiTableFlags_Sortable)) TableSortSpecsBuild(table); @@ -1165,9 +1166,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } // Process hit-testing on resizing borders. Actual size change will be applied in EndTable() -// - Set table->HoveredColumnBorder with a short delay/timer to reduce feedback noise -// - Submit ahead of table contents and header, use ImGuiButtonFlags_AllowItemOverlap to prioritize widgets -// overlapping the same area. +// - Set table->HoveredColumnBorder with a short delay/timer to reduce visual feedback noise. void ImGui::TableUpdateBorders(ImGuiTable* table) { ImGuiContext& g = *GImGui; @@ -1207,7 +1206,7 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) //GetForegroundDrawList()->AddRect(hit_rect.Min, hit_rect.Max, IM_COL32(255, 0, 0, 100)); bool hovered = false, held = false; - bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_NoNavFocus); + bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick | ImGuiButtonFlags_NoNavFocus); if (pressed && IsMouseDoubleClicked(0)) { TableSetColumnWidthAutoSingle(table, column_n); @@ -1436,6 +1435,7 @@ void ImGui::EndTable() g.CurrentTable->DrawSplitter = &temp_data->DrawSplitter; } outer_window->DC.CurrentTableIdx = g.CurrentTable ? g.Tables.GetIndex(g.CurrentTable) : -1; + NavUpdateCurrentWindowIsScrollPushableX(); } // See "COLUMN SIZING POLICIES" comments at the top of this file @@ -1641,7 +1641,7 @@ ImGuiID ImGui::TableGetColumnResizeID(ImGuiTable* table, int column_n, int insta return instance_id + 1 + column_n; // FIXME: #6140: still not ideal } -// Return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. +// Return -1 when table is not hovered. return columns_count if hovering the unused space at the right of the right-most visible column. int ImGui::TableGetHoveredColumn() { ImGuiContext& g = *GImGui; @@ -1978,6 +1978,7 @@ bool ImGui::TableNextColumn() // FIXME-TABLE FIXME-OPT: Could probably shortcut some things for non-active or clipped columns. void ImGui::TableBeginCell(ImGuiTable* table, int column_n) { + ImGuiContext& g = *GImGui; ImGuiTableColumn* column = &table->Columns[column_n]; ImGuiWindow* window = table->InnerWindow; table->CurrentColumn = column_n; @@ -2002,7 +2003,6 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n) window->SkipItems = column->IsSkipItems; if (column->IsSkipItems) { - ImGuiContext& g = *GImGui; g.LastItemData.ID = 0; g.LastItemData.StatusFlags = 0; } @@ -2021,7 +2021,6 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n) } // Logging - ImGuiContext& g = *GImGui; if (g.LogEnabled && !column->IsSkipItems) { LogRenderedText(&window->DC.CursorPos, "|"); @@ -2141,7 +2140,7 @@ void ImGui::TableSetColumnWidth(int column_n, float width) // - All stretch: easy. // - One or more fixed + one stretch: easy. // - One or more fixed + more than one stretch: tricky. - // Qt when manual resize is enabled only support a single _trailing_ stretch column. + // Qt when manual resize is enabled only supports a single _trailing_ stretch column, we support more cases here. // When forwarding resize from Wn| to Fn+1| we need to be considerate of the _NoResize flag on Fn+1. // FIXME-TABLE: Find a way to rewrite all of this so interactions feel more consistent for the user. @@ -2224,7 +2223,7 @@ void ImGui::TableUpdateColumnsWeightFromWidth(ImGuiTable* table) { IM_ASSERT(table->LeftMostStretchedColumn != -1 && table->RightMostStretchedColumn != -1); - // Measure existing quantity + // Measure existing quantities float visible_weight = 0.0f; float visible_width = 0.0f; for (int column_n = 0; column_n < table->ColumnsCount; column_n++) @@ -2381,11 +2380,11 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) struct MergeGroup { ImRect ClipRect; - int ChannelsCount; - ImBitArrayPtr ChannelsMask; + int ChannelsCount = 0; + ImBitArrayPtr ChannelsMask = NULL; }; int merge_group_mask = 0x00; - MergeGroup merge_groups[4] = {}; + MergeGroup merge_groups[4]; // Use a reusable temp buffer for the merge masks as they are dynamically sized. const int max_draw_channels = (4 + table->ColumnsCount * 2); @@ -2499,11 +2498,9 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) merge_clip_rect.Max.x = ImMax(merge_clip_rect.Max.x, host_rect.Max.x); if ((merge_group_n & 2) != 0 && (table->Flags & ImGuiTableFlags_NoHostExtendY) == 0) merge_clip_rect.Max.y = ImMax(merge_clip_rect.Max.y, host_rect.Max.y); -#if 0 - GetOverlayDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, 0, 1.0f); - GetOverlayDrawList()->AddLine(merge_group->ClipRect.Min, merge_clip_rect.Min, IM_COL32(255, 100, 0, 200)); - GetOverlayDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200)); -#endif + //GetForegroundDrawList()->AddRect(merge_group->ClipRect.Min, merge_group->ClipRect.Max, IM_COL32(255, 0, 0, 200), 0.0f, 0, 1.0f); // [DEBUG] + //GetForegroundDrawList()->AddLine(merge_group->ClipRect.Min, merge_clip_rect.Min, IM_COL32(255, 100, 0, 200)); + //GetForegroundDrawList()->AddLine(merge_group->ClipRect.Max, merge_clip_rect.Max, IM_COL32(255, 100, 0, 200)); remaining_count -= merge_group->ChannelsCount; for (int n = 0; n < (size_for_masks_bitarrays_one >> 2); n++) remaining_mask[n] &= ~merge_group->ChannelsMask[n]; @@ -2667,7 +2664,6 @@ ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() TableUpdateLayout(table); TableSortSpecsBuild(table); - return &table->SortSpecs; } @@ -2786,7 +2782,7 @@ void ImGui::TableSortSpecsSanitize(ImGuiTable* table) } } - // Fallback default sort order (if no column had the ImGuiTableColumnFlags_DefaultSort flag) + // Fallback default sort order (if no column with the ImGuiTableColumnFlags_DefaultSort flag) if (sort_order_count == 0 && !(table->Flags & ImGuiTableFlags_SortTristate)) for (int column_n = 0; column_n < table->ColumnsCount; column_n++) { @@ -2963,11 +2959,9 @@ void ImGui::TableHeader(const char* label) //GetForegroundDrawList()->AddRect(cell_r.Min, cell_r.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] //GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] - // Using AllowItemOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items. + // Using AllowOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items. bool hovered, held; - bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_AllowItemOverlap); - if (g.ActiveId != id) - SetItemAllowOverlap(); + bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_AllowOverlap); if (held || hovered || selected) { const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); @@ -3007,7 +3001,7 @@ void ImGui::TableHeader(const char* label) } // Sort order arrow - const float ellipsis_max = cell_r.Max.x - w_arrow - w_sort_text; + const float ellipsis_max = ImMax(cell_r.Max.x - w_arrow - w_sort_text, label_pos.x); if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) { if (column->SortOrder != -1) @@ -3038,8 +3032,8 @@ void ImGui::TableHeader(const char* label) RenderTextEllipsis(window->DrawList, label_pos, ImVec2(ellipsis_max, label_pos.y + label_height + g.Style.FramePadding.y), ellipsis_max, ellipsis_max, label, label_end, &label_size); const bool text_clipped = label_size.x > (ellipsis_max - label_pos.x); - if (text_clipped && hovered && g.ActiveId == 0 && IsItemHovered(ImGuiHoveredFlags_DelayNormal)) - SetTooltip("%.*s", (int)(label_end - label), label); + if (text_clipped && hovered && g.ActiveId == 0) + SetItemTooltip("%.*s", (int)(label_end - label), label); // We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden if (IsMouseReleased(1) && IsItemHovered()) @@ -3477,12 +3471,12 @@ static void TableSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandle if (!save_column) continue; buf->appendf("Column %-2d", column_n); - if (column->UserID != 0) buf->appendf(" UserID=%08X", column->UserID); - if (save_size && column->IsStretch) buf->appendf(" Weight=%.4f", column->WidthOrWeight); - if (save_size && !column->IsStretch) buf->appendf(" Width=%d", (int)column->WidthOrWeight); - if (save_visible) buf->appendf(" Visible=%d", column->IsEnabled); - if (save_order) buf->appendf(" Order=%d", column->DisplayOrder); - if (save_sort && column->SortOrder != -1) buf->appendf(" Sort=%d%c", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? 'v' : '^'); + if (column->UserID != 0) { buf->appendf(" UserID=%08X", column->UserID); } + if (save_size && column->IsStretch) { buf->appendf(" Weight=%.4f", column->WidthOrWeight); } + if (save_size && !column->IsStretch) { buf->appendf(" Width=%d", (int)column->WidthOrWeight); } + if (save_visible) { buf->appendf(" Visible=%d", column->IsEnabled); } + if (save_order) { buf->appendf(" Order=%d", column->DisplayOrder); } + if (save_sort && column->SortOrder != -1) { buf->appendf(" Sort=%d%c", column->SortOrder, (column->SortDirection == ImGuiSortDirection_Ascending) ? 'v' : '^'); } buf->append("\n"); } buf->append("\n"); @@ -3530,7 +3524,7 @@ void ImGui::TableGcCompactTransientBuffers(ImGuiTable* table) IM_ASSERT(table->MemoryCompacted == false); table->SortSpecs.Specs = NULL; table->SortSpecsMulti.clear(); - table->IsSortSpecsDirty = true; // FIXME: shouldn't have to leak into user performing a sort + table->IsSortSpecsDirty = true; // FIXME: In theory shouldn't have to leak into user performing a sort on resume. table->ColumnsNames.clear(); table->MemoryCompacted = true; for (int n = 0; n < table->ColumnsCount; n++) @@ -3583,13 +3577,9 @@ static const char* DebugNodeTableGetSizingPolicyDesc(ImGuiTableFlags sizing_poli void ImGui::DebugNodeTable(ImGuiTable* table) { - char buf[512]; - char* p = buf; - const char* buf_end = buf + IM_ARRAYSIZE(buf); - const bool is_active = (table->LastFrameActive >= ImGui::GetFrameCount() - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. - ImFormatString(p, buf_end - p, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*"); + const bool is_active = (table->LastFrameActive >= GetFrameCount() - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } - bool open = TreeNode(table, "%s", buf); + bool open = TreeNode(table, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*"); if (!is_active) { PopStyleColor(); } if (IsItemHovered()) GetForegroundDrawList()->AddRect(table->OuterRect.Min, table->OuterRect.Max, IM_COL32(255, 255, 0, 255)); @@ -3598,7 +3588,7 @@ void ImGui::DebugNodeTable(ImGuiTable* table) if (!open) return; if (table->InstanceCurrent > 0) - ImGui::Text("** %d instances of same table! Some data below will refer to last instance.", table->InstanceCurrent + 1); + Text("** %d instances of same table! Some data below will refer to last instance.", table->InstanceCurrent + 1); bool clear_settings = SmallButton("Clear settings"); BulletText("OuterRect: Pos: (%.1f,%.1f) Size: (%.1f,%.1f) Sizing: '%s'", table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.GetWidth(), table->OuterRect.GetHeight(), DebugNodeTableGetSizingPolicyDesc(table->Flags)); BulletText("ColumnsGivenWidth: %.1f, ColumnsAutoFitWidth: %.1f, InnerWidth: %.1f%s", table->ColumnsGivenWidth, table->ColumnsAutoFitWidth, table->InnerWidth, table->InnerWidth == 0.0f ? " (auto)" : ""); @@ -3614,6 +3604,7 @@ void ImGui::DebugNodeTable(ImGuiTable* table) { ImGuiTableColumn* column = &table->Columns[n]; const char* name = TableGetColumnName(table, n); + char buf[512]; ImFormatString(buf, IM_ARRAYSIZE(buf), "Column %d order %d '%s': offset %+.2f to %+.2f%s\n" "Enabled: %d, VisibleX/Y: %d/%d, RequestOutput: %d, SkipItems: %d, DrawChannels: %d,%d\n" @@ -3902,6 +3893,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFl columns->Count = columns_count; columns->Flags = flags; window->DC.CurrentColumns = columns; + window->DC.NavIsScrollPushableX = false; // Shortcut for NavUpdateCurrentWindowIsScrollPushableX(); columns->HostCursorPosY = window->DC.CursorPos.y; columns->HostCursorMaxPosX = window->DC.CursorMaxPos.x; @@ -4092,6 +4084,7 @@ void ImGui::EndColumns() window->DC.CurrentColumns = NULL; window->DC.ColumnsOffset.x = 0.0f; window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + NavUpdateCurrentWindowIsScrollPushableX(); } void ImGui::Columns(int columns_count, const char* id, bool border) diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index ab43ac05..43132447 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.4 +// dear imgui, v1.89.7 // (widgets code) /* @@ -492,6 +492,14 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0) flags |= ImGuiButtonFlags_PressedOnDefault_; + // Default behavior inherited from item flags + // Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that. + ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); + if (flags & ImGuiButtonFlags_AllowOverlap) + item_flags |= ImGuiItemflags_AllowOverlap; + if (flags & ImGuiButtonFlags_Repeat) + item_flags |= ImGuiItemFlags_ButtonRepeat; + ImGuiWindow* backup_hovered_window = g.HoveredWindow; const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredWindow && g.HoveredWindow->RootWindow == window; if (flatten_hovered_children) @@ -504,11 +512,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool #endif bool pressed = false; - bool hovered = ItemHoverable(bb, id); - - // Drag source doesn't report as hovered - if (hovered && g.DragDropActive && g.DragDropPayload.SourceId == id && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoDisableHover)) - hovered = false; + bool hovered = ItemHoverable(bb, id, item_flags); // Special mode for Drag and Drop where holding button pressed for a long time while dragging another item triggers the button if (g.DragDropActive && (flags & ImGuiButtonFlags_PressedOnDragDropHold) && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) @@ -527,10 +531,6 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (flatten_hovered_children) g.HoveredWindow = backup_hovered_window; - // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. This allows using patterns where a later submitted widget overlaps a previous one. - if (hovered && (flags & ImGuiButtonFlags_AllowItemOverlap) && (g.HoveredIdPreviousFrame != id && g.HoveredIdPreviousFrame != 0)) - hovered = false; - // Mouse handling const ImGuiID test_owner_id = (flags & ImGuiButtonFlags_NoTestKeyOwner) ? ImGuiKeyOwner_Any : id; if (hovered) @@ -579,7 +579,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool { if (mouse_button_released != -1) { - const bool has_repeated_at_least_once = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; // Repeat mode trumps on release behavior + const bool has_repeated_at_least_once = (item_flags & ImGuiItemFlags_ButtonRepeat) && g.IO.MouseDownDurationPrev[mouse_button_released] >= g.IO.KeyRepeatDelay; // Repeat mode trumps on release behavior if (!has_repeated_at_least_once) pressed = true; if (!(flags & ImGuiButtonFlags_NoNavFocus)) @@ -590,7 +590,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. - if (g.ActiveId == id && (flags & ImGuiButtonFlags_Repeat)) + if (g.ActiveId == id && (item_flags & ImGuiItemFlags_ButtonRepeat)) if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, test_owner_id, ImGuiInputFlags_Repeat)) pressed = true; } @@ -608,7 +608,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool { bool nav_activated_by_code = (g.NavActivateId == id); bool nav_activated_by_inputs = (g.NavActivatePressedId == id); - if (!nav_activated_by_inputs && (flags & ImGuiButtonFlags_Repeat)) + if (!nav_activated_by_inputs && (item_flags & ImGuiItemFlags_ButtonRepeat)) { // Avoid pressing multiple keys from triggering excessive amount of repeat events const ImGuiKeyData* key1 = GetKeyData(ImGuiKey_Space); @@ -622,7 +622,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Set active id so it can be queried by user via IsItemActive(), equivalent of holding the mouse button. pressed = true; SetActiveID(id, window); - g.ActiveIdSource = ImGuiInputSource_Nav; + g.ActiveIdSource = g.NavInputSource; if (!(flags & ImGuiButtonFlags_NoNavFocus)) SetFocusID(id, window); } @@ -638,7 +638,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool g.ActiveIdClickOffset = g.IO.MousePos - bb.Min; const int mouse_button = g.ActiveIdMouseButton; - if (IsMouseDown(mouse_button, test_owner_id)) + if (mouse_button == -1) + { + // Fallback for the rare situation were g.ActiveId was set programmatically or from another widget (e.g. #6304). + ClearActiveID(); + } + else if (IsMouseDown(mouse_button, test_owner_id)) { held = true; } @@ -650,7 +655,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool { // Report as pressed when releasing the mouse (this is the most common path) bool is_double_click_release = (flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseReleased[mouse_button] && g.IO.MouseClickedLastCount[mouse_button] == 2; - bool is_repeating_already = (flags & ImGuiButtonFlags_Repeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps + bool is_repeating_already = (item_flags & ImGuiItemFlags_ButtonRepeat) && g.IO.MouseDownDurationPrev[mouse_button] >= g.IO.KeyRepeatDelay; // Repeat mode trumps bool is_button_avail_or_owned = TestKeyOwner(MouseButtonToKey(mouse_button), test_owner_id); if (!is_double_click_release && !is_repeating_already && is_button_avail_or_owned) pressed = true; @@ -660,7 +665,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (!(flags & ImGuiButtonFlags_NoNavFocus)) g.NavDisableHighlight = true; } - else if (g.ActiveIdSource == ImGuiInputSource_Nav) + else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) { // When activated using Nav, we hold on the ActiveID until activation button is released if (g.NavActivateDownId != id) @@ -697,9 +702,6 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags if (!ItemAdd(bb, id)) return false; - if (g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat) - flags |= ImGuiButtonFlags_Repeat; - bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); @@ -776,9 +778,6 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu if (!ItemAdd(bb, id)) return false; - if (g.LastItemData.InFlags & ImGuiItemFlags_ButtonRepeat) - flags |= ImGuiButtonFlags_Repeat; - bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); @@ -827,7 +826,7 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); ImVec2 center = bb.GetCenter(); if (hovered) - window->DrawList->AddCircleFilled(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col, 12); + window->DrawList->AddCircleFilled(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col); float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; ImU32 cross_col = GetColorU32(ImGuiCol_Text); @@ -852,7 +851,7 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); if (hovered || held) - window->DrawList->AddCircleFilled(bb.GetCenter()/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col, 12); + window->DrawList->AddCircleFilled(bb.GetCenter()/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col); RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold @@ -1163,10 +1162,8 @@ bool ImGui::CheckboxFlagsT(const char* label, T* flags, T flags_value) if (!all_on && any_on) { ImGuiContext& g = *GImGui; - ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; - g.CurrentItemFlags |= ImGuiItemFlags_MixedValue; + g.NextItemData.ItemFlags |= ImGuiItemFlags_MixedValue; pressed = Checkbox(label, &all_on); - g.CurrentItemFlags = backup_item_flags; } else { @@ -1233,17 +1230,18 @@ bool ImGui::RadioButton(const char* label, bool active) MarkItemEdited(id); RenderNavHighlight(total_bb, id); - window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), 16); + const int num_segment = window->DrawList->_CalcCircleAutoSegmentCount(radius); + window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), num_segment); if (active) { const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); - window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark), 16); + window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark)); } if (style.FrameBorderSize > 0.0f) { - window->DrawList->AddCircle(center + ImVec2(1, 1), radius, GetColorU32(ImGuiCol_BorderShadow), 16, style.FrameBorderSize); - window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), 16, style.FrameBorderSize); + window->DrawList->AddCircle(center + ImVec2(1, 1), radius, GetColorU32(ImGuiCol_BorderShadow), num_segment, style.FrameBorderSize); + window->DrawList->AddCircle(center, radius, GetColorU32(ImGuiCol_Border), num_segment, style.FrameBorderSize); } ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y); @@ -1386,8 +1384,9 @@ void ImGui::AlignTextToFramePadding() } // Horizontal/vertical separating line -// FIXME: Surprisingly, this seemingly simple widget is adjacent to MANY different legacy/tricky layout issues. -void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) +// FIXME: Surprisingly, this seemingly trivial widget is a victim of many different legacy/tricky layout issues. +// Note how thickness == 1.0f is handled specifically as not moving CursorPos by 'thickness', but other values are. +void ImGui::SeparatorEx(ImGuiSeparatorFlags flags, float thickness) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) @@ -1395,8 +1394,8 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) ImGuiContext& g = *GImGui; IM_ASSERT(ImIsPowerOfTwo(flags & (ImGuiSeparatorFlags_Horizontal | ImGuiSeparatorFlags_Vertical))); // Check that only 1 option is selected + IM_ASSERT(thickness > 0.0f); - const float thickness = 1.0f; // Cannot use g.Style.SeparatorTextSize yet for various reasons. if (flags & ImGuiSeparatorFlags_Vertical) { // Vertical separator, for menu bars (use current line height). @@ -1430,6 +1429,8 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) x2 = table->Columns[table->CurrentColumn].MaxX; } + // Before Tables API happened, we relied on Separator() to span all columns of a Columns() set. + // We currently don't need to provide the same feature for tables because tables naturally have border features. ImGuiOldColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; if (columns) PushColumnsBackground(); @@ -1439,8 +1440,8 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags) const float thickness_for_layout = (thickness == 1.0f) ? 0.0f : thickness; // FIXME: See 1.70/1.71 Separator() change: makes legacy 1-px separator not affect layout yet. Should change. const ImRect bb(ImVec2(x1, window->DC.CursorPos.y), ImVec2(x2, window->DC.CursorPos.y + thickness)); ItemSize(ImVec2(0.0f, thickness_for_layout)); - const bool item_visible = ItemAdd(bb, 0); - if (item_visible) + + if (ItemAdd(bb, 0)) { // Draw window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Separator)); @@ -1463,10 +1464,11 @@ void ImGui::Separator() if (window->SkipItems) return; - // Those flags should eventually be overridable by the user + // Those flags should eventually be configurable by the user + // FIXME: We cannot g.Style.SeparatorTextBorderSize for thickness as it relates to SeparatorText() which is a decorated separator, not defaulting to 1.0f. ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; flags |= ImGuiSeparatorFlags_SpanAllColumns; // NB: this only applies to legacy Columns() api as they relied on Separator() a lot. - SeparatorEx(flags); + SeparatorEx(flags, 1.0f); } void ImGui::SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_w) @@ -1526,8 +1528,8 @@ void ImGui::SeparatorText(const char* label) return; // The SeparatorText() vs SeparatorTextEx() distinction is designed to be considerate that we may want: - // - allow headers to be draggable items (would require a stable ID + a noticeable highlight) - // - this high-level entry point to allow formatting? (may require ID separate from formatted string) + // - allow separator-text to be draggable items (would require a stable ID + a noticeable highlight) + // - this high-level entry point to allow formatting? (which in turns may require ID separate from formatted string) // - because of this we probably can't turn 'const char* label' into 'const char* fmt, ...' // Otherwise, we can decide that users wanting to drag this would layout a dedicated drag-item, // and then we can turn this into a format function. @@ -1543,14 +1545,20 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float if (!ItemAdd(bb, id, NULL, ImGuiItemFlags_NoNav)) return false; + // FIXME: AFAIK the only leftover reason for passing ImGuiButtonFlags_AllowOverlap here is + // to allow caller of SplitterBehavior() to call SetItemAllowOverlap() after the item. + // Nowadays we would instead want to use SetNextItemAllowOverlap() before the item. + ImGuiButtonFlags button_flags = ImGuiButtonFlags_FlattenChildren; +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + button_flags |= ImGuiButtonFlags_AllowOverlap; +#endif + bool hovered, held; ImRect bb_interact = bb; bb_interact.Expand(axis == ImGuiAxis_Y ? ImVec2(0.0f, hover_extend) : ImVec2(hover_extend, 0.0f)); - ButtonBehavior(bb_interact, id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap); + ButtonBehavior(bb_interact, id, &hovered, &held, button_flags); if (hovered) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; // for IsItemHovered(), because bb_interact is larger than bb - if (g.ActiveId != id) - SetItemAllowOverlap(); if (held || (hovered && g.HoveredIdPreviousFrame == id && g.HoveredIdTimer >= hover_visibility_delay)) SetMouseCursor(axis == ImGuiAxis_Y ? ImGuiMouseCursor_ResizeNS : ImGuiMouseCursor_ResizeEW); @@ -1819,7 +1827,7 @@ bool ImGui::BeginComboPreview() if (window->SkipItems || !(g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible)) return false; IM_ASSERT(g.LastItemData.Rect.Min.x == preview_data->PreviewRect.Min.x && g.LastItemData.Rect.Min.y == preview_data->PreviewRect.Min.y); // Didn't call after BeginCombo/EndCombo block or forgot to pass ImGuiComboFlags_CustomPreview flag? - if (!window->ClipRect.Contains(preview_data->PreviewRect)) // Narrower test (optional) + if (!window->ClipRect.Overlaps(preview_data->PreviewRect)) // Narrower test (optional) return false; // FIXME: This could be contained in a PushWorkRect() api @@ -1918,7 +1926,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi const char* item_text; if (!items_getter(data, i, &item_text)) item_text = "*Unknown item*"; - if (Selectable(item_text, item_selected)) + if (Selectable(item_text, item_selected) && *current_item != i) { value_changed = true; *current_item = i; @@ -2084,7 +2092,8 @@ bool ImGui::DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void memcpy(&data_backup, p_data, type_info->Size); // Sanitize format - // For float/double we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in, so force them into %f and %lf + // - For float/double we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in, so force them into %f and %lf + // - In theory could treat empty format as using default, but this would only cover rare/bizarre case of using InputScalar() + integer + format string without %. char format_sanitized[32]; if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) format = type_info->ScanFmt; @@ -2245,7 +2254,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const if (g.IO.KeyShift) adjust_delta *= 10.0f; } - else if (g.ActiveIdSource == ImGuiInputSource_Nav) + else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) { const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; const bool tweak_slow = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakSlow : ImGuiKey_NavKeyboardTweakSlow); @@ -2352,7 +2361,7 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v // Those are the things we can do easily outside the DragBehaviorT<> template, saves code generation. if (g.ActiveIdSource == ImGuiInputSource_Mouse && !g.IO.MouseDown[0]) ClearActiveID(); - else if (g.ActiveIdSource == ImGuiInputSource_Nav && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) + else if ((g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) && g.NavActivatePressedId == id && !g.ActiveIdIsJustActivated) ClearActiveID(); } if (g.ActiveId != id) @@ -2404,7 +2413,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { @@ -2821,7 +2830,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ set_new_value = true; } } - else if (g.ActiveIdSource == ImGuiInputSource_Nav) + else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) { if (g.ActiveIdIsJustActivated) { @@ -2997,7 +3006,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { @@ -3164,7 +3173,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); const bool clicked = hovered && IsMouseClicked(0, id); if (clicked || g.NavActivateId == id) { @@ -3267,7 +3276,7 @@ const char* ImParseFormatFindEnd(const char* fmt) } // Extract the format out of a format string with leading or trailing decorations -// fmt = "blah blah" -> return fmt +// fmt = "blah blah" -> return "" // fmt = "%.3f" -> return fmt // fmt = "hello %.3f" -> return fmt + 6 // fmt = "%.3f hello" -> return buf written with "%.3f" @@ -3275,7 +3284,7 @@ const char* ImParseFormatTrimDecorations(const char* fmt, char* buf, size_t buf_ { const char* fmt_start = ImParseFormatFindStart(fmt); if (fmt_start[0] != '%') - return fmt; + return ""; const char* fmt_end = ImParseFormatFindEnd(fmt_start); if (fmt_end[0] == 0) // If we only have leading decoration, we don't need to copy the data. return fmt_start; @@ -3300,7 +3309,7 @@ void ImParseFormatSanitizeForPrinting(const char* fmt_in, char* fmt_out, size_t *fmt_out = 0; // Zero-terminate } -// - For scanning we need to remove all width and precision fields "%3.7f" -> "%f". BUT don't strip types like "%I64d" which includes digits. ! "%07I64d" -> "%I64d" +// - For scanning we need to remove all width and precision fields and flags "%+3.7f" -> "%f". BUT don't strip types like "%I64d" which includes digits. ! "%07I64d" -> "%I64d" const char* ImParseFormatSanitizeForScanning(const char* fmt_in, char* fmt_out, size_t fmt_out_size) { const char* fmt_end = ImParseFormatFindEnd(fmt_in); @@ -3311,7 +3320,7 @@ const char* ImParseFormatSanitizeForScanning(const char* fmt_in, char* fmt_out, while (fmt_in < fmt_end) { char c = *fmt_in++; - if (!has_type && ((c >= '0' && c <= '9') || c == '.')) + if (!has_type && ((c >= '0' && c <= '9') || c == '.' || c == '+' || c == '#')) continue; has_type |= ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); // Stop skipping digits if (c != '\'' && c != '$' && c != '_') // Custom flags provided by stb_sprintf.h. POSIX 2008 also supports '. @@ -3393,9 +3402,14 @@ static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(ImGuiDataType d // However this may not be ideal for all uses, as some user code may break on out of bound values. bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min, const void* p_clamp_max) { + // FIXME: May need to clarify display behavior if format doesn't contain %. + // "%d" -> "%d" / "There are %d items" -> "%d" / "items" -> "%d" (fallback). Also see #6405 + const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type); char fmt_buf[32]; char data_buf[32]; format = ImParseFormatTrimDecorations(format, fmt_buf, IM_ARRAYSIZE(fmt_buf)); + if (format[0] == 0) + format = type_info->PrintFmt; DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); ImStrTrimBlanks(data_buf); @@ -3406,7 +3420,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) { // Backup old value - size_t data_type_size = DataTypeGetInfo(data_type)->Size; + size_t data_type_size = type_info->Size; ImGuiDataTypeTempStorage data_backup; memcpy(&data_backup, p_data, data_type_size); @@ -3450,7 +3464,12 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data flags |= ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. bool value_changed = false; - if (p_step != NULL) + if (p_step == NULL) + { + if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + } + else { const float button_size = GetFrameHeight(); @@ -3493,11 +3512,6 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data PopID(); EndGroup(); } - else - { - if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) - value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); - } if (value_changed) MarkItemEdited(g.LastItemData.ID); @@ -3699,10 +3713,34 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* ob r->num_chars = (int)(text_remaining - (text + line_start_idx)); } -// When ImGuiInputTextFlags_Password is set, we don't want actions such as CTRL+Arrow to leak the fact that underlying data are blanks or separators. -static bool is_separator(unsigned int c) { return ImCharIsBlankW(c) || c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r'; } -static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (is_separator(obj->TextW[idx - 1]) && !is_separator(obj->TextW[idx]) ) : 1; } -static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) { if (obj->Flags & ImGuiInputTextFlags_Password) return 0; return idx > 0 ? (!is_separator(obj->TextW[idx - 1]) && is_separator(obj->TextW[idx])) : 1; } +static bool is_separator(unsigned int c) +{ + return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r' || c=='.' || c=='!'; +} + +static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) +{ + // When ImGuiInputTextFlags_Password is set, we don't want actions such as CTRL+Arrow to leak the fact that underlying data are blanks or separators. + if ((obj->Flags & ImGuiInputTextFlags_Password) || idx <= 0) + return 0; + + bool prev_white = ImCharIsBlankW(obj->TextW[idx - 1]); + bool prev_separ = is_separator(obj->TextW[idx - 1]); + bool curr_white = ImCharIsBlankW(obj->TextW[idx]); + bool curr_separ = is_separator(obj->TextW[idx]); + return ((prev_white || prev_separ) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ); +} +static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) +{ + if ((obj->Flags & ImGuiInputTextFlags_Password) || idx <= 0) + return 0; + + bool prev_white = ImCharIsBlankW(obj->TextW[idx]); + bool prev_separ = is_separator(obj->TextW[idx]); + bool curr_white = ImCharIsBlankW(obj->TextW[idx - 1]); + bool curr_separ = is_separator(obj->TextW[idx - 1]); + return ((prev_white) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ); +} static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } @@ -3835,6 +3873,10 @@ void ImGuiInputTextCallbackData::DeleteChars(int pos, int bytes_count) void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, const char* new_text_end) { + // Accept null ranges + if (new_text == new_text_end) + return; + const bool is_resizable = (Flags & ImGuiInputTextFlags_CallbackResize) != 0; const int new_text_len = new_text_end ? (int)(new_text_end - new_text) : (int)strlen(new_text); if (new_text_len + BufTextLen >= BufSize) @@ -3999,6 +4041,29 @@ static void InputTextReconcileUndoStateAfterUserCallback(ImGuiInputTextState* st p[i] = ImStb::STB_TEXTEDIT_GETCHAR(state, first_diff + i); } +// As InputText() retain textual data and we currently provide a path for user to not retain it (via local variables) +// we need some form of hook to reapply data back to user buffer on deactivation frame. (#4714) +// It would be more desirable that we discourage users from taking advantage of the "user not retaining data" trick, +// but that more likely be attractive when we do have _NoLiveEdit flag available. +void ImGui::InputTextDeactivateHook(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + ImGuiInputTextState* state = &g.InputTextState; + if (id == 0 || state->ID != id) + return; + g.InputTextDeactivatedState.ID = state->ID; + if (state->Flags & ImGuiInputTextFlags_ReadOnly) + { + g.InputTextDeactivatedState.TextA.resize(0); // In theory this data won't be used, but clear to be neat. + } + else + { + IM_ASSERT(state->TextA.Data != 0); + g.InputTextDeactivatedState.TextA.resize(state->CurLenA + 1); + memcpy(g.InputTextDeactivatedState.TextA.Data, state->TextA.Data, state->CurLenA + 1); + } +} + // Edit a string of text // - buf_size account for the zero-terminator, so a buf_size of 6 can hold "Hello" but not "Hello!". // This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match @@ -4086,7 +4151,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ return false; item_status_flags = g.LastItemData.StatusFlags; } - const bool hovered = ItemHoverable(frame_bb, id); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); if (hovered) g.MouseCursor = ImGuiMouseCursor_TextInput; @@ -4104,7 +4169,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; - const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); + const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); // state != NULL means its our state. const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav || input_requested_by_tabbing); const bool init_state = (init_make_active || user_scroll_active); if ((init_state && g.ActiveId != id) || init_changed_specs) @@ -4113,6 +4178,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ state = &g.InputTextState; state->CursorAnimReset(); + // Backup state of deactivating item so they'll have a chance to do a write to output buffer on the same frame they report IsItemDeactivatedAfterEdit (#4714) + InputTextDeactivateHook(state->ID); + // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar) // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode) const int buf_len = (int)strlen(buf); @@ -4248,7 +4316,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Although we are active we don't prevent mouse from hovering other elements unless we are interacting right now with the widget. // Down the line we should have a cleaner library-wide concept of Selected vs Active. g.ActiveIdAllowOverlap = !io.MouseDown[0]; - g.WantTextInputNextFrame = 1; // Edit in progress const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; @@ -4516,6 +4583,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Clear input apply_new_text = ""; apply_new_text_length = 0; + value_changed |= (buf[0] != 0); STB_TEXTEDIT_CHARTYPE empty_string; stb_textedit_replace(state, &state->Stb, &empty_string, 0); } @@ -4525,6 +4593,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Push records into the undo stack so we can CTRL+Z the revert operation itself apply_new_text = state->InitialTextA.Data; apply_new_text_length = state->InitialTextA.Size - 1; + value_changed = true; ImVector w_text; if (apply_new_text_length > 0) { @@ -4638,10 +4707,24 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { apply_new_text = state->TextA.Data; apply_new_text_length = state->CurLenA; + value_changed = true; } } } + // Handle reapplying final data on deactivation (see InputTextDeactivateHook() for details) + if (g.InputTextDeactivatedState.ID == id) + { + if (g.ActiveId != id && IsItemDeactivatedAfterEdit() && !is_readonly) + { + apply_new_text = g.InputTextDeactivatedState.TextA.Data; + apply_new_text_length = g.InputTextDeactivatedState.TextA.Size - 1; + value_changed |= (strcmp(g.InputTextDeactivatedState.TextA.Data, buf) != 0); + //IMGUI_DEBUG_LOG("InputText(): apply Deactivated data for 0x%08X: \"%.*s\".\n", id, apply_new_text_length, apply_new_text); + } + g.InputTextDeactivatedState.ID = 0; + } + // Copy result to user buffer. This can currently only happen when (g.ActiveId == id) if (apply_new_text != NULL) { @@ -4669,12 +4752,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size)); - value_changed = true; } // Release active ID at the end of the function (so e.g. pressing Return still does a final application of the value) - if (clear_active_id && g.ActiveId == id) + // Otherwise request text input ahead for next frame. + if (g.ActiveId == id && clear_active_id) ClearActiveID(); + else if (g.ActiveId == id) + g.WantTextInputNextFrame = 1; // Render frame if (!is_multiline) @@ -4889,11 +4974,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // For focus requests to work on our multiline we need to ensure our child ItemAdd() call specifies the ImGuiItemFlags_Inputable (ref issue #4761)... Dummy(ImVec2(text_size.x, text_size.y + style.FramePadding.y)); - ImGuiItemFlags backup_item_flags = g.CurrentItemFlags; - g.CurrentItemFlags |= ImGuiItemFlags_Inputable | ImGuiItemFlags_NoTabStop; + g.NextItemData.ItemFlags |= ImGuiItemFlags_Inputable | ImGuiItemFlags_NoTabStop; EndChild(); item_data_backup.StatusFlags |= (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredWindow); - g.CurrentItemFlags = backup_item_flags; // ...and then we need to undo the group overriding last item data, which gets a bit messy as EndGroup() tries to forward scrollbar being active... // FIXME: This quite messy/tricky, should attempt to get rid of the child window. @@ -5582,7 +5665,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl float sin_hue_angle = ImSin(H * 2.0f * IM_PI); ImVec2 hue_cursor_pos(wheel_center.x + cos_hue_angle * (wheel_r_inner + wheel_r_outer) * 0.5f, wheel_center.y + sin_hue_angle * (wheel_r_inner + wheel_r_outer) * 0.5f); float hue_cursor_rad = value_changed_h ? wheel_thickness * 0.65f : wheel_thickness * 0.55f; - int hue_cursor_segments = ImClamp((int)(hue_cursor_rad / 1.4f), 9, 32); + int hue_cursor_segments = draw_list->_CalcCircleAutoSegmentCount(hue_cursor_rad); // Lock segment count so the +1 one matches others. draw_list->AddCircleFilled(hue_cursor_pos, hue_cursor_rad, hue_color32, hue_cursor_segments); draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad + 1, col_midgrey, hue_cursor_segments); draw_list->AddCircle(hue_cursor_pos, hue_cursor_rad, col_white, hue_cursor_segments); @@ -5592,13 +5675,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl ImVec2 trb = wheel_center + ImRotate(triangle_pb, cos_hue_angle, sin_hue_angle); ImVec2 trc = wheel_center + ImRotate(triangle_pc, cos_hue_angle, sin_hue_angle); ImVec2 uv_white = GetFontTexUvWhitePixel(); - draw_list->PrimReserve(6, 6); + draw_list->PrimReserve(3, 3); draw_list->PrimVtx(tra, uv_white, hue_color32); - draw_list->PrimVtx(trb, uv_white, hue_color32); - draw_list->PrimVtx(trc, uv_white, col_white); - draw_list->PrimVtx(tra, uv_white, 0); draw_list->PrimVtx(trb, uv_white, col_black); - draw_list->PrimVtx(trc, uv_white, 0); + draw_list->PrimVtx(trc, uv_white, col_white); draw_list->AddTriangle(tra, trb, trc, col_midgrey, 1.5f); sv_cursor_pos = ImLerp(ImLerp(trc, tra, ImSaturate(S)), trb, ImSaturate(1 - V)); } @@ -5621,9 +5701,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range) float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f; - draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, user_col32_striped_of_alpha, 12); - draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad + 1, col_midgrey, 12); - draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, col_white, 12); + int sv_cursor_segments = draw_list->_CalcCircleAutoSegmentCount(sv_cursor_rad); // Lock segment count so the +1 one matches others. + draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, user_col32_striped_of_alpha, sv_cursor_segments); + draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad + 1, col_midgrey, sv_cursor_segments); + draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad, col_white, sv_cursor_segments); // Render alpha bar if (alpha_bar) @@ -5729,7 +5810,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl } // Tooltip - if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered) + if (!(flags & ImGuiColorEditFlags_NoTooltip) && hovered && IsItemHovered(ImGuiHoveredFlags_ForTooltip)) ColorTooltip(desc_id, &col.x, flags & (ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf)); return pressed; @@ -5759,7 +5840,7 @@ void ImGui::ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags { ImGuiContext& g = *GImGui; - if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePreviousTooltip, ImGuiWindowFlags_None)) + if (!BeginTooltipEx(ImGuiTooltipFlags_OverridePrevious, ImGuiWindowFlags_None)) return; const char* text_end = text ? FindRenderedTextEnd(text, NULL) : text; if (text_end > text) @@ -6094,8 +6175,8 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l } ImGuiButtonFlags button_flags = ImGuiTreeNodeFlags_None; - if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) - button_flags |= ImGuiButtonFlags_AllowItemOverlap; + if ((flags & ImGuiTreeNodeFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap)) + button_flags |= ImGuiButtonFlags_AllowOverlap; if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; @@ -6151,11 +6232,13 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (g.NavId == id && g.NavMoveDir == ImGuiDir_Left && is_open) { toggled = true; + NavClearPreferredPosForAxis(ImGuiAxis_X); NavMoveRequestCancel(); } if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right && !is_open) // If there's something upcoming on the line we may want to give it the priority? { toggled = true; + NavClearPreferredPosForAxis(ImGuiAxis_X); NavMoveRequestCancel(); } @@ -6166,8 +6249,6 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledOpen; } } - if (flags & ImGuiTreeNodeFlags_AllowItemOverlap) - SetItemAllowOverlap(); // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 @@ -6185,9 +6266,9 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col); else if (!is_leaf) - RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y), text_col, is_open ? ImGuiDir_Down : ImGuiDir_Right, 1.0f); + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 1.0f); else // Leaf without bullet, left-adjusted text - text_pos.x -= text_offset_x; + text_pos.x -= text_offset_x -padding.x; if (flags & ImGuiTreeNodeFlags_ClipLabelForTrailingButton) frame_bb.Max.x -= g.FontSize + style.FramePadding.x; @@ -6207,7 +6288,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); else if (!is_leaf) - RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ImGuiDir_Down : ImGuiDir_Right, 0.70f); + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 0.70f); if (g.LogEnabled) LogSetNextTextDecoration(">", NULL); RenderText(text_pos, label, label_end, false); @@ -6311,7 +6392,7 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFl ImGuiID id = window->GetID(label); flags |= ImGuiTreeNodeFlags_CollapsingHeader; if (p_visible) - flags |= ImGuiTreeNodeFlags_AllowItemOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; + flags |= ImGuiTreeNodeFlags_AllowOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; bool is_open = TreeNodeBehavior(id, flags, label); if (p_visible != NULL) { @@ -6340,7 +6421,7 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFl // Tip: pass a non-visible label (e.g. "##hello") then you can use the space to draw other text or image. // But you need to make sure the ID is unique, e.g. enclose calls in PushID/PopID or use ##unique_id. -// With this scheme, ImGuiSelectableFlags_SpanAllColumns and ImGuiSelectableFlags_AllowItemOverlap are also frequently used flags. +// With this scheme, ImGuiSelectableFlags_SpanAllColumns and ImGuiSelectableFlags_AllowOverlap are also frequently used flags. // FIXME: Selectable() with (size.x == 0.0f) and (SelectableTextAlign.x > 0.0f) followed by SameLine() is currently not supported. bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags flags, const ImVec2& size_arg) { @@ -6424,7 +6505,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } - if (flags & ImGuiSelectableFlags_AllowItemOverlap) { button_flags |= ImGuiButtonFlags_AllowItemOverlap; } + if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } const bool was_selected = selected; bool hovered, held; @@ -6453,9 +6534,6 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (pressed) MarkItemEdited(id); - if (flags & ImGuiSelectableFlags_AllowItemOverlap) - SetItemAllowOverlap(); - // In this branch, Selectable() cannot toggle the selection so this will never trigger. if (selected != was_selected) //-V547 g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; @@ -6545,20 +6623,6 @@ bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) return true; } -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -// OBSOLETED in 1.81 (from February 2021) -bool ImGui::ListBoxHeader(const char* label, int items_count, int height_in_items) -{ - // If height_in_items == -1, default height is maximum 7. - ImGuiContext& g = *GImGui; - float height_in_items_f = (height_in_items < 0 ? ImMin(items_count, 7) : height_in_items) + 0.25f; - ImVec2 size; - size.x = 0.0f; - size.y = GetTextLineHeightWithSpacing() * height_in_items_f + g.Style.FramePadding.y * 2.0f; - return BeginListBox(label, size); -} -#endif - void ImGui::EndListBox() { ImGuiContext& g = *GImGui; @@ -6654,7 +6718,7 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get ItemSize(total_bb, style.FramePadding.y); if (!ItemAdd(total_bb, 0, &frame_bb)) return -1; - const bool hovered = ItemHoverable(frame_bb, id); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); // Determine scale from values if not specified if (scale_min == FLT_MAX || scale_max == FLT_MAX) @@ -7022,7 +7086,7 @@ void ImGui::EndMainMenuBar() // FIXME: With this strategy we won't be able to restore a NULL focus. ImGuiContext& g = *GImGui; if (g.CurrentWindow == g.NavWindow && g.NavLayer == ImGuiNavLayer_Main && !g.NavAnyRequest) - FocusTopMostWindowUnderOne(g.NavWindow, NULL); + FocusTopMostWindowUnderOne(g.NavWindow, NULL, NULL, ImGuiFocusRequestFlags_UnlessBelowModal | ImGuiFocusRequestFlags_RestoreFocusedChild); End(); } @@ -7036,9 +7100,9 @@ static bool IsRootOfOpenMenuSet() // Initially we used 'upper_popup->OpenParentId == window->IDStack.back()' to differentiate multiple menu sets from each others // (e.g. inside menu bar vs loose menu items) based on parent ID. - // This would however prevent the use of e.g. PuhsID() user code submitting menus. + // This would however prevent the use of e.g. PushID() user code submitting menus. // Previously this worked between popup and a first child menu because the first child menu always had the _ChildWindow flag, - // making hovering on parent popup possible while first child menu was focused - but this was generally a bug with other side effects. + // making hovering on parent popup possible while first child menu was focused - but this was generally a bug with other side effects. // Instead we don't treat Popup specifically (in order to consistently support menu features in them), maybe the first child menu of a Popup // doesn't have the _ChildWindow flag, and we rely on this IsRootOfOpenMenuSet() check to allow hovering between root window/popup and first child menu. // In the end, lack of ID check made it so we could no longer differentiate between separate menu sets. To compensate for that, we at least check parent window nav layer. @@ -7046,7 +7110,9 @@ static bool IsRootOfOpenMenuSet() // open on hover, but that should be a lesser problem, because if such menus are close in proximity in window content then it won't feel weird and if they are far apart // it likely won't be a problem anyone runs into. const ImGuiPopupData* upper_popup = &g.OpenPopupStack[g.BeginPopupStack.Size]; - return (window->DC.NavLayerCurrent == upper_popup->ParentNavLayer && upper_popup->Window && (upper_popup->Window->Flags & ImGuiWindowFlags_ChildMenu)); + if (window->DC.NavLayerCurrent != upper_popup->ParentNavLayer) + return false; + return upper_popup->Window && (upper_popup->Window->Flags & ImGuiWindowFlags_ChildMenu) && ImGui::IsWindowChildOf(upper_popup->Window, window, true); } bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) @@ -7563,6 +7629,12 @@ void ImGui::EndTabBar() g.CurrentTabBar = g.CurrentTabBarStack.empty() ? NULL : GetTabBarFromTabBarRef(g.CurrentTabBarStack.back()); } +// Scrolling happens only in the central section (leading/trailing sections are not scrolling) +static float TabBarCalcScrollableWidth(ImGuiTabBar* tab_bar, ImGuiTabBarSection* sections) +{ + return tab_bar->BarRect.GetWidth() - sections[0].Width - sections[2].Width - sections[1].Spacing; +} + // This is called only once a frame before by the first call to ItemTab() // The reason we're not calling it in BeginTabBar() is to leave a chance to the user to call the SetTabItemClosed() functions. static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) @@ -7765,9 +7837,23 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) tab_bar->VisibleTabId = tab_bar->SelectedTabId; tab_bar->VisibleTabWasSubmitted = false; - // Update scrolling + // Apply request requests if (scroll_to_tab_id != 0) TabBarScrollToTab(tab_bar, scroll_to_tab_id, sections); + else if ((tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll) && IsMouseHoveringRect(tab_bar->BarRect.Min, tab_bar->BarRect.Max, true) && IsWindowContentHoverable(g.CurrentWindow)) + { + const float wheel = g.IO.MouseWheelRequestAxisSwap ? g.IO.MouseWheel : g.IO.MouseWheelH; + const ImGuiKey wheel_key = g.IO.MouseWheelRequestAxisSwap ? ImGuiKey_MouseWheelY : ImGuiKey_MouseWheelX; + if (TestKeyOwner(wheel_key, tab_bar->ID) && wheel != 0.0f) + { + const float scroll_step = wheel * TabBarCalcScrollableWidth(tab_bar, sections) / 3.0f; + tab_bar->ScrollingTargetDistToVisibility = 0.0f; + tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget - scroll_step); + } + SetKeyOwner(wheel_key, tab_bar->ID); + } + + // Update scrolling tab_bar->ScrollingAnim = TabBarScrollClamp(tab_bar, tab_bar->ScrollingAnim); tab_bar->ScrollingTarget = TabBarScrollClamp(tab_bar, tab_bar->ScrollingTarget); if (tab_bar->ScrollingAnim != tab_bar->ScrollingTarget) @@ -7904,8 +7990,7 @@ static void ImGui::TabBarScrollToTab(ImGuiTabBar* tab_bar, ImGuiID tab_id, ImGui int order = TabBarGetTabOrder(tab_bar, tab); // Scrolling happens only in the central section (leading/trailing sections are not scrolling) - // FIXME: This is all confusing. - float scrollable_width = tab_bar->BarRect.GetWidth() - sections[0].Width - sections[2].Width - sections[1].Spacing; + float scrollable_width = TabBarCalcScrollableWidth(tab_bar, sections); // We make all tabs positions all relative Sections[0].Width to make code simpler float tab_x1 = tab->Offset - sections[0].Width + (order > sections[0].TabCount - 1 ? -margin : 0.0f); @@ -8317,7 +8402,8 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, } // Click to Select a tab - ImGuiButtonFlags button_flags = ((is_tab_button ? ImGuiButtonFlags_PressedOnClickRelease : ImGuiButtonFlags_PressedOnClick) | ImGuiButtonFlags_AllowItemOverlap); + // Allow the close button to overlap + ImGuiButtonFlags button_flags = ((is_tab_button ? ImGuiButtonFlags_PressedOnClickRelease : ImGuiButtonFlags_PressedOnClick) | ImGuiButtonFlags_AllowOverlap); if (g.DragDropActive) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; bool hovered, held; @@ -8325,10 +8411,6 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, if (pressed && !is_tab_button) TabBarQueueFocus(tab_bar, tab); - // Allow the close button to overlap unless we are dragging (in which case we don't want any overlapping tabs to be hovered) - if (g.ActiveId != id) - SetItemAllowOverlap(); - // Drag and drop: re-order tabs if (held && !tab_appearing && IsMouseDragging(0)) { @@ -8393,8 +8475,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // FIXME: We may want disabled tab to still display the tooltip? if (text_clipped && g.HoveredId == id && !held) if (!(tab_bar->Flags & ImGuiTabBarFlags_NoTooltip) && !(tab->Flags & ImGuiTabItemFlags_NoTooltip)) - if (IsItemHovered(ImGuiHoveredFlags_DelayNormal)) - SetTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); + SetItemTooltip("%.*s", (int)(FindRenderedTextEnd(label) - label), label); IM_ASSERT(!is_tab_button || !(tab_bar->SelectedTabId == tab->ID && is_tab_button)); // TabItemButton should not be selected if (is_tab_button) From 24f607bd32e157fbd11ff4c08983a5dcad96260e Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Mon, 17 Jul 2023 17:33:39 +0200 Subject: [PATCH 0835/1018] Update build.yml --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 211e1145..6d2b361a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,7 +53,6 @@ jobs: echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list sudo apt update sudo apt install intel-renderkit - sudo ln -s /opt/intel/oneapi/embree/4.0.1/lib/cmake/embree /opt/intel/oneapi/embree/4.0.1/lib/cmake/embree-4.0.1 - name: Create Build Environment run: cmake -E make_directory ${{runner.workspace}}/build From 5015651cf408e436b5954052ae8e4ab4243ff04d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 24 Jul 2023 17:58:04 +0200 Subject: [PATCH 0836/1018] scanner: fix room scanner orientation y axis --- include/gproshan/raytracing/embree.h | 4 ++-- src/gproshan/app_viewer.cpp | 6 +++--- src/gproshan/scenes/scanner.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 1fa101fb..7ef1a6cc 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -21,8 +21,8 @@ class embree : public raytracing { ray_hit(const vertex & p_org = {0, 0, 0}, const vertex & v_dir = {0, 0, 0}, - float near = 1e-5f, - float far = FLT_MAX); + float near = 0, + float far = 1e20f); vertex org() const; vertex dir() const; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 8309c173..78dd4315 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -134,10 +134,10 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - static size_t n_rows = 4000; + static size_t n_rows = 5000; static size_t n_cols = 2000; - static const size_t n_min = 1000; - static const size_t n_max = 1000000; + static const size_t n_min = 100; + static const size_t n_max = 10000; static vertex cam_pos = {0, 0, 0}; if(view->sphere_points.size() != 1) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index e5c4a340..5e7834ac 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -26,7 +26,7 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c const real_t & phi = i * delta_phi; const real_t & theta = j * delta_theta; - const vertex & dir = {std::sin(theta) * std::cos(phi), std::sin(theta) * std::sin(phi), std::cos(theta)}; + const vertex & dir = {std::sin(theta) * std::cos(phi), std::cos(theta), std::sin(theta) * std::sin(phi)}; const rt::eval_hit & h = rt->intersect(cam_pos, dir); From 6e1d0517902f66b57089f74465047f110083ddc6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 26 Jul 2023 17:59:29 +0200 Subject: [PATCH 0837/1018] scanner: fix orientation --- src/gproshan/scenes/scanner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 5e7834ac..dac74565 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -26,7 +26,7 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c const real_t & phi = i * delta_phi; const real_t & theta = j * delta_theta; - const vertex & dir = {std::sin(theta) * std::cos(phi), std::cos(theta), std::sin(theta) * std::sin(phi)}; + const vertex & dir = {std::sin(theta) * std::sin(phi), std::cos(theta), std::sin(theta) * std::cos(phi)}; const rt::eval_hit & h = rt->intersect(cam_pos, dir); From 3547c39ce449ca8c35c0f0b72c769e21794a0749 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Jul 2023 13:50:03 +0200 Subject: [PATCH 0838/1018] cleaning up warnings adding TODOs --- include/gproshan/mesh/che_pcd.h | 2 +- include/gproshan/{mesh => pointcloud}/kdtree.h | 0 src/gproshan/app_viewer.cpp | 11 ++++++++--- src/gproshan/mesh/che.cpp | 1 + src/gproshan/mesh/che_fill_hole.cpp | 6 +++--- src/gproshan/mesh/che_pcd.cpp | 4 ++-- src/gproshan/{mesh => pointcloud}/kdtree.cpp | 7 +++---- 7 files changed, 18 insertions(+), 13 deletions(-) rename include/gproshan/{mesh => pointcloud}/kdtree.h (100%) rename src/gproshan/{mesh => pointcloud}/kdtree.cpp (67%) diff --git a/include/gproshan/mesh/che_pcd.h b/include/gproshan/mesh/che_pcd.h index 1a0c1052..fbf363b9 100644 --- a/include/gproshan/mesh/che_pcd.h +++ b/include/gproshan/mesh/che_pcd.h @@ -13,7 +13,7 @@ class che_pcd : public che public: che_pcd(const std::string & file); - static void write_file(const che * mesh, const std::string & file, const bool & color = false); + static void write_file(const che * mesh, const std::string & file); private: void read_file(const std::string & file); diff --git a/include/gproshan/mesh/kdtree.h b/include/gproshan/pointcloud/kdtree.h similarity index 100% rename from include/gproshan/mesh/kdtree.h rename to include/gproshan/pointcloud/kdtree.h diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 78dd4315..6dea83dc 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -126,6 +126,9 @@ bool app_viewer::process_compute_normals(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); + gproshan_log_var(mesh->n_vertices); + // TODO + return false; } @@ -883,6 +886,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); + // TODO // fill_all_holes(mesh); /*********************************************************************/ che * fill_mesh = new che(*mesh); @@ -902,7 +906,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) std::priority_queue > front; std::vector neigs(vertices.size()); - +/* auto bprev = [&](const index_t & v) -> index_t & { return neigs[v].x(); @@ -911,6 +915,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) { return neigs[v].y(); }; +*/ auto push = [&](const uvec3 & p) { neigs[p.x()] = {p.y(), p.z()}; @@ -928,11 +933,11 @@ bool app_viewer::process_fill_holes(viewer * p_view) std::vector border; border.assign(true, vertices.size()); - real_t angle; +// real_t angle; index_t v0, v1, v2; while(!front.empty()) { - angle = front.top().first; +// angle = front.top().first; if(!(border[v0] && border[v1] && border[v2])) continue; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 302f8a00..474c33e6 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -636,6 +636,7 @@ std::vector che::link(const index_t & v) const void che::edge_collapse(const std::vector & sort_edges) { + gproshan_error_var(sort_edges.size()); // TODO } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 386f50e6..a0223596 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -65,7 +65,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c vertex normal; size_t size = border_vertices.size(); - index_t i, j, n_v; + index_t i = NIL, j, n_v; che * hole = nullptr; che * aux_hole; @@ -205,7 +205,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c return hole; } -void split_border(std::vector > & split_indices, che * mesh, const std::vector & border_vertices) +void split_border(std::vector > & , che * mesh, const std::vector & border_vertices) { size_t n = border_vertices.size(); a_mat data(3, n); @@ -878,7 +878,7 @@ void get_real_tri(che * mesh, std::vector & select_vertices, std::vecto triangle.push_back( (wp * tri[2]) + wo * (tri[0] + tri[1]) ); } -che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index) +che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t ) { size_t n_vertices = select_vertices.size() + 3; size_t n_trigs = select_vertices.size() + 4; diff --git a/src/gproshan/mesh/che_pcd.cpp b/src/gproshan/mesh/che_pcd.cpp index 7bbafff6..228961db 100644 --- a/src/gproshan/mesh/che_pcd.cpp +++ b/src/gproshan/mesh/che_pcd.cpp @@ -34,7 +34,7 @@ void che_pcd::read_file(const std::string & file) assert(fp); size_t n_points = 0; - char line[512], type[32], str[32], format[32], element[32]; + char line[512], str[32], format[32]; while(fgets(line, sizeof(line), fp) && str[0] != 'D') // DATA is the end header { @@ -80,7 +80,7 @@ void che_pcd::read_file(const std::string & file) } } -void che_pcd::write_file(const che * mesh, const std::string & file, const bool & color) +void che_pcd::write_file(const che * mesh, const std::string & file) { FILE * fp = fopen((file + ".pcd").c_str(), "wb"); assert(fp); diff --git a/src/gproshan/mesh/kdtree.cpp b/src/gproshan/pointcloud/kdtree.cpp similarity index 67% rename from src/gproshan/mesh/kdtree.cpp rename to src/gproshan/pointcloud/kdtree.cpp index b3f57899..8c3c0cf0 100644 --- a/src/gproshan/mesh/kdtree.cpp +++ b/src/gproshan/pointcloud/kdtree.cpp @@ -1,4 +1,4 @@ -#include +#include #include @@ -19,12 +19,11 @@ kdtree::~kdtree() delete [] nodes; } -void kdtree::build(const index_t & n, const vertex * pc, const index_t & i, const index_t & j, const index_t & d) +void kdtree::build(const index_t & n, const vertex * , const index_t & i, const index_t & j, const index_t & ) { - if(i == j) { - nodes[i] = i; + nodes[n] = i; return; } } From b6e4df083539c86ab78461792691f51c3379e995 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Jul 2023 15:38:19 +0200 Subject: [PATCH 0839/1018] knn: add grid knn class --- include/gproshan/pointcloud/knn.h | 52 +++++++++++++++++++++++++++++++ src/gproshan/app_viewer.cpp | 3 ++ src/gproshan/pointcloud/knn.cpp | 33 ++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 include/gproshan/pointcloud/knn.h create mode 100644 src/gproshan/pointcloud/knn.cpp diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h new file mode 100644 index 00000000..0cd24fde --- /dev/null +++ b/include/gproshan/pointcloud/knn.h @@ -0,0 +1,52 @@ +#ifndef KNN_H +#define KNN_H + +#include +#include + +#include +#include + + +inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res = 1000) +{ + p = (res - 1) * (0.5f * p + 1); + return {(unsigned int) p.x(), (unsigned int) p.y(), (unsigned int) p.z()}; +} + + +template<> +struct std::hash +{ + std::size_t operator () (const gproshan::uvec3 & p) const noexcept + { + return p.x() * 1e6 + p.y() * 1e3 + p.z(); + } +}; + + +// geometry processing and shape analysis framework +namespace gproshan { + + +using point = vec3; + + +class knn // grid +{ + private: + std::vector points; + std::unordered_map > grid; + + public: + knn(const point * pc, const size_t & n_points, const mat4 & transform); + virtual ~knn() = default; + + private: +}; + + +} // namespace gproshan + +#endif // KNN_H + diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 6dea83dc..a909ccbc 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -129,6 +130,8 @@ bool app_viewer::process_compute_normals(viewer * p_view) gproshan_log_var(mesh->n_vertices); // TODO + knn grid(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + return false; } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp new file mode 100644 index 00000000..e9d519af --- /dev/null +++ b/src/gproshan/pointcloud/knn.cpp @@ -0,0 +1,33 @@ +#include + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +knn::knn(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) +{ + double build_time = 0; + + TIC(build_time); + + for(index_t i = 0; i < n_points; ++i) + { + point & p = points[i]; + p = transform * vec4(pc[i], 1); + + grid[hash(p)].push_back(i); + } + + TOC(build_time); + + gproshan_log_var(build_time); + gproshan_log_var(grid.size()); + gproshan_log_var(double(n_points) / grid.size()); +} + + +} // namespace gproshan + From da5b110f16f6f705dfa45f03e5757e23a2ff677e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 27 Jul 2023 16:44:15 +0200 Subject: [PATCH 0840/1018] knn: grid knn query --- include/gproshan/pointcloud/knn.h | 13 ++++---- src/gproshan/app_viewer.cpp | 9 +++++- src/gproshan/pointcloud/knn.cpp | 51 ++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 0cd24fde..0d34b16f 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -8,7 +8,7 @@ #include -inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res = 1000) +inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res) { p = (res - 1) * (0.5f * p + 1); return {(unsigned int) p.x(), (unsigned int) p.y(), (unsigned int) p.z()}; @@ -20,7 +20,7 @@ struct std::hash { std::size_t operator () (const gproshan::uvec3 & p) const noexcept { - return p.x() * 1e6 + p.y() * 1e3 + p.z(); + return p.x() * 1e12 + p.y() * 1e6 + p.z(); } }; @@ -32,17 +32,18 @@ namespace gproshan { using point = vec3; -class knn // grid +class grid_knn // grid { private: + float res = 1000; std::vector points; std::unordered_map > grid; public: - knn(const point * pc, const size_t & n_points, const mat4 & transform); - virtual ~knn() = default; + grid_knn(const point * pc, const size_t & n_points, const mat4 & transform); + virtual ~grid_knn() = default; - private: + std::vector operator () (const point & p, int knn); }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a909ccbc..1e7e97df 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -130,7 +130,14 @@ bool app_viewer::process_compute_normals(viewer * p_view) gproshan_log_var(mesh->n_vertices); // TODO - knn grid(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + + if(mesh.selected.size()) + { + const index_t & p = mesh.selected.back(); + for(const index_t & v: knn(vec3(mesh.model_mat * vec4(mesh->point(p), 1)), 9)) + mesh.selected.push_back(v); + } return false; } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index e9d519af..de7e7c0a 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -1,33 +1,74 @@ #include #include +#include // geometry processing and shape analysis framework namespace gproshan { -knn::knn(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) +grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) { double build_time = 0; TIC(build_time); - + + res = sqrt(n_points / 10); + for(index_t i = 0; i < n_points; ++i) { point & p = points[i]; p = transform * vec4(pc[i], 1); - - grid[hash(p)].push_back(i); + + grid[hash(p, res)].push_back(i); } TOC(build_time); - + + gproshan_log_var(sizeof(size_t)); gproshan_log_var(build_time); + gproshan_log_var(res); gproshan_log_var(grid.size()); gproshan_log_var(double(n_points) / grid.size()); } +std::vector grid_knn::operator () (const point & p, int knn) +{ + const uvec3 key = hash(p, res); + + std::priority_queue > q; + + for(int i = -1; i < 2; ++i) + for(int j = -1; j < 2; ++j) + for(int k = -1; k < 2; ++k) + { + const uvec3 cell = {key.x() + i, key.y() + j, key.z() + k}; + + if(cell.x() == NIL || cell.y() == NIL || cell.z() == NIL) + continue; + + if(grid.find(cell) == grid.end()) + continue; + + for(const index_t & v: grid[cell]) + q.push({-length(p - points[v]), v}); + } + + std::vector nn; + nn.reserve(knn); + + while(!q.empty() && knn) + { + nn.push_back(q.top().second); + q.pop(); + + --knn; + } + + return nn; +} + } // namespace gproshan From 1acd1922a43a752d808f873d3f79b7e0fed86b87 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 2 Aug 2023 14:29:24 +0200 Subject: [PATCH 0841/1018] geometry: updating vec class --- include/gproshan/geometry/vec.h | 29 ++++++----------------------- src/gproshan/app_viewer.cpp | 13 +++++++++++++ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index b2cd026c..57c2efad 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -133,17 +133,7 @@ class vec return norm(); } - ///< scalar product - __host_device__ - vec operator * (const T & a) const - { - vec res; - for(index_t i = 0; i < N; ++i) - res[i] = values[i] * a; - return res; - } - - ///< element wise product + ///< element wise product & scalar product __host_device__ vec operator * (const vec & v) const { @@ -193,15 +183,6 @@ class vec return res; } - ///< scalar product self assign - __host_device__ - const vec & operator *= (const T & a) - { - for(T & v: values) - v *= a; - return *this; - } - ///< element wise product self assign __host_device__ const vec & operator *= (const vec & v) @@ -281,6 +262,7 @@ vec operator * (const T & a, const vec & v) return v * a; } + ///< cross product template __host_device__ @@ -336,9 +318,10 @@ template __host_device__ std::ostream & operator << (std::ostream & os, const vec & v) { - for(index_t i = 0; i < N - 1; ++i) - os << v[i] << " "; - return os << v[N - 1]; + os << v[0]; + for(index_t i = 1; i < N; ++i) + os << " " << v[i]; + return os; } ///< std std::istream diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 1e7e97df..66f9bbbe 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -49,6 +49,19 @@ che * app_viewer::load_mesh(const std::string & file_path) int app_viewer::main(int nargs, const char ** args) { + vec3 a(1); + vec3 b = 1; + vec3 c{1.1}; + vec3 d = {1}; + gproshan_log_var(a * 1); + gproshan_log_var(b *= 2); + gproshan_log_var(c); + gproshan_log_var(3 * c); + auto q = 3.f * c; + gproshan_log_var(q); + std::cout << typeid(3.f * c).name() << "\n"; + gproshan_log_var(d); +return 0; if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); From d3c7efe2829c43e4257fecb8a31dee9eb0d5e233 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 2 Aug 2023 16:43:46 +0200 Subject: [PATCH 0842/1018] vec: add comma operator for concat a scalar --- include/gproshan/geometry/vec.h | 7 +++++++ src/gproshan/app_viewer.cpp | 15 +-------------- src/gproshan/pointcloud/knn.cpp | 2 +- src/gproshan/raytracing/embree.cpp | 4 ++-- src/gproshan/raytracing/optix.cu | 2 +- src/gproshan/raytracing/raytracing.cpp | 3 +-- src/gproshan/viewer/viewer.cpp | 2 +- 7 files changed, 14 insertions(+), 21 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 57c2efad..b41e6455 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -73,6 +73,13 @@ class vec return values[i]; } + ///< concatenate with comma operator + __host_device__ + vec operator , (const T & a) const + { + return {*this, a}; + } + __host_device__ T & x() { diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 66f9bbbe..296cd2f6 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -49,19 +49,6 @@ che * app_viewer::load_mesh(const std::string & file_path) int app_viewer::main(int nargs, const char ** args) { - vec3 a(1); - vec3 b = 1; - vec3 c{1.1}; - vec3 d = {1}; - gproshan_log_var(a * 1); - gproshan_log_var(b *= 2); - gproshan_log_var(c); - gproshan_log_var(3 * c); - auto q = 3.f * c; - gproshan_log_var(q); - std::cout << typeid(3.f * c).name() << "\n"; - gproshan_log_var(d); -return 0; if(nargs < 2) { printf("%s [mesh_paths.(off,obj,ply)]\n", args[0]); @@ -148,7 +135,7 @@ bool app_viewer::process_compute_normals(viewer * p_view) if(mesh.selected.size()) { const index_t & p = mesh.selected.back(); - for(const index_t & v: knn(vec3(mesh.model_mat * vec4(mesh->point(p), 1)), 9)) + for(const index_t & v: knn(mesh.model_mat * (mesh->point(p), 1), 9)) mesh.selected.push_back(v); } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index de7e7c0a..76ec9523 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -19,7 +19,7 @@ grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & trans for(index_t i = 0; i < n_points; ++i) { point & p = points[i]; - p = transform * vec4(pc[i], 1); + p = transform * (pc[i], 1); grid[hash(p, res)].push_back(i); } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index da2d2dda..2ef5fef4 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -171,7 +171,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) - vertices[i] = model_mat * vec4(mesh->point(i), 1); + vertices[i] = model_mat * (mesh->point(i), 1); index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, @@ -233,7 +233,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - pxyzr[i] = model_mat * vec4(mesh->point(i), 1); + pxyzr[i] = model_mat * (mesh->point(i), 1); pxyzr[i][3] = pc_radius; // normal[i] = mesh->normal(i); } diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 2e5cf672..fff090b0 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -81,7 +81,7 @@ extern "C" __global__ void __closesthit__radiance() }); vec4 & pixel_color = *ray_data(); - pixel_color = (pixel_color * optix_params.n_samples + vec4(li, 1)) / (optix_params.n_samples + 1); + pixel_color = (pixel_color * optix_params.n_samples + (li, 1)) / (optix_params.n_samples + 1); } diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 2c3e39fa..d792c11d 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -35,8 +35,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f vec3 li = closesthit_radiance(params.cam_pos, dir, params.lights, params.n_lights, params.cam_pos, flat); - color = (color * n_samples + li) / (n_samples + 1); - color[3] = 1; + color = (color * n_samples + (li, 1)) / (n_samples + 1); } ++n_samples; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index d99da2c7..00c8919b 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -253,7 +253,7 @@ void viewer::imgui() if(ImGui::Button("add selected points as lights")) { for(const index_t & v: mesh.selected) - if(!render_params.add_light(mesh.model_mat * vec4(mesh->point(v), 1))) + if(!render_params.add_light(mesh.model_mat * (mesh->point(v), 1))) break; } From a4bdcd4dbeaec9141c36684bd8859ed780b754df Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 2 Aug 2023 18:21:18 +0200 Subject: [PATCH 0843/1018] scanner: cleaning up --- src/gproshan/scenes/scanner.cpp | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index dac74565..80a9cb7b 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -30,17 +30,10 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c const rt::eval_hit & h = rt->intersect(cam_pos, dir); - if(h.primID != NIL) - { - mesh_ptx->point(v) = h.position; - mesh_ptx->normal(v) = h.normal; - mesh_ptx->heatmap(v) = h.dist / M_SQRT2; - mesh_ptx->rgb(v) = h.Kd; - } - else - { - mesh_ptx->rgb(v) = vertex{0, 0, 0}; - } + mesh_ptx->point(v) = h.position; + mesh_ptx->normal(v) = h.normal; + mesh_ptx->heatmap(v) = h.dist / M_SQRT2; + mesh_ptx->rgb(v) = h.Kd; } return mesh_ptx; From f1d4bb9a3d52a9d704c367b2f50bc5f5d3e32df8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 24 Aug 2023 09:50:49 +0200 Subject: [PATCH 0844/1018] using optix 8 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db2e1171..660ac385 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES native) endif() - find_package(OptiX 7.7) + find_package(OptiX 8) if(OptiX_INCLUDE) include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) From 61118eee34c7cf0ec31327262c4ddd35f4cb28ef Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Sep 2023 14:33:32 +0200 Subject: [PATCH 0845/1018] pointcloud: adding UI menu --- include/gproshan/app_viewer.h | 5 ++++- src/gproshan/app_viewer.cpp | 31 +++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 47ac5826..575c7c88 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -59,8 +59,11 @@ class app_viewer : public viewer che * load_mesh(const std::string & file_path); - // Scenes + // Point Cloud + static bool process_knn(viewer * p_view); static bool process_compute_normals(viewer * p_view); + + // Scenes static bool process_simulate_scanner(viewer * p_view); // Geometry diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 296cd2f6..8c7903a0 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -69,12 +69,15 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { + sub_menus.push_back("Point Cloud"); + add_process(1001, "", "KNN", process_knn); + add_process(1002, "", "Compute Normals", process_compute_normals); + sub_menus.push_back("Scenes"); - add_process(1001, "", "Compute Normals", process_compute_normals); - add_process(1002, "", "Scan Scene", process_simulate_scanner); + add_process(1003, "", "Scan Scene", process_simulate_scanner); sub_menus.push_back("Geometry"); - add_process(1003, "", "Sampling 4points", process_sampling_4points); + add_process(1004, "", "Sampling 4points", process_sampling_4points); add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); add_process(GLFW_KEY_O, "O", "Connected Components", process_connected_components); add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); @@ -120,16 +123,13 @@ void app_viewer::init() } -// Scenes +// Point Cloud -bool app_viewer::process_compute_normals(viewer * p_view) +bool app_viewer::process_knn(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->active_mesh(); - gproshan_log_var(mesh->n_vertices); - // TODO - grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); if(mesh.selected.size()) @@ -142,6 +142,21 @@ bool app_viewer::process_compute_normals(viewer * p_view) return false; } +bool app_viewer::process_compute_normals(viewer * p_view) +{ + app_viewer * view = (app_viewer *) p_view; + che_viewer & mesh = view->active_mesh(); + + gproshan_log_var(mesh->n_vertices); + + // TODO + + return false; +} + + +// Scenes + bool app_viewer::process_simulate_scanner(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; From cfbf94418e132b978145662980d8f051bdca2399 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Sep 2023 16:23:07 +0200 Subject: [PATCH 0846/1018] viewer: selected mesh label --- include/gproshan/viewer/viewer.h | 4 +- src/gproshan/app_viewer.cpp | 70 ++++++++++++++++---------------- src/gproshan/viewer/viewer.cpp | 30 ++++++++------ 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index b6449f63..f96f4f4d 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -82,7 +82,7 @@ class viewer double render_time = 0; std::vector meshes; - index_t idx_active_mesh = 0; + index_t idx_selected_mesh = 0; frame * frames = nullptr; @@ -103,7 +103,7 @@ class viewer viewer(const int & width = 1920, const int & height = 1080); virtual ~viewer(); - che_viewer & active_mesh(); + che_viewer & selected_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); bool add_mesh(che * p_mesh, const bool & reset_normals = true); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 8c7903a0..c94549cb 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -128,7 +128,7 @@ void app_viewer::init() bool app_viewer::process_knn(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); @@ -145,7 +145,7 @@ bool app_viewer::process_knn(viewer * p_view) bool app_viewer::process_compute_normals(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); gproshan_log_var(mesh->n_vertices); @@ -160,7 +160,7 @@ bool app_viewer::process_compute_normals(viewer * p_view) bool app_viewer::process_simulate_scanner(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static size_t n_rows = 5000; static size_t n_cols = 2000; @@ -198,7 +198,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) bool app_viewer::process_sampling_4points(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static size_t n = 10; static std::vector points; @@ -236,7 +236,7 @@ bool app_viewer::process_sampling_4points(viewer * p_view) bool app_viewer::process_convex_hull(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); convex_hull ch(&mesh->point(0), mesh->n_vertices); mesh.selected = ch; @@ -247,7 +247,7 @@ bool app_viewer::process_convex_hull(viewer * p_view) bool app_viewer::process_connected_components(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); real_t * label = &mesh->heatmap(0); @@ -293,7 +293,7 @@ bool app_viewer::process_connected_components(viewer * p_view) bool app_viewer::process_gaussian_curvature(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; @@ -351,7 +351,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) bool app_viewer::process_edge_collapse(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); index_t levels; std::cin >> levels; @@ -368,7 +368,7 @@ bool app_viewer::process_edge_collapse(viewer * p_view) bool app_viewer::process_multiplicate_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); mesh->multiplicate_vertices(); mesh->update_normals(); @@ -382,7 +382,7 @@ bool app_viewer::process_multiplicate_vertices(viewer * p_view) bool app_viewer::process_delete_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); if(!mesh.selected.size()) return true; @@ -397,7 +397,7 @@ bool app_viewer::process_delete_vertices(viewer * p_view) bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); gproshan_debug(removing vertex); mesh->remove_non_manifold_vertices(); @@ -412,7 +412,7 @@ bool app_viewer::process_delete_non_manifold_vertices(viewer * p_view) bool app_viewer::process_fairing_spectral(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static std::vector vertices; static size_t min_neigs = 1; @@ -443,7 +443,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) bool app_viewer::process_fairing_taubin(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static std::vector vertices; static fairing_taubin fair(0); @@ -475,7 +475,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) bool app_viewer::process_geodesics(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static geodesics::params params; @@ -507,7 +507,7 @@ bool app_viewer::process_geodesics(viewer * p_view) bool app_viewer::process_farthest_point_sampling(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static int n = 10; static real_t radio; @@ -529,7 +529,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) bool app_viewer::process_voronoi(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); geodesics::params params; params.cluster = true; @@ -559,7 +559,7 @@ bool app_viewer::process_voronoi(viewer * p_view) bool app_viewer::process_compute_toplesets(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); if(!mesh.selected.size()) mesh.selected.push_back(0); @@ -594,7 +594,7 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) bool app_viewer::process_msparse_coding(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static msparse_coding::params params; static size_t n = 12; @@ -627,7 +627,7 @@ bool app_viewer::process_msparse_coding(viewer * p_view) bool app_viewer::process_mdict_patch(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); TIC(view->time) index_t * toplevel = new index_t[mesh->n_vertices]; @@ -673,7 +673,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) bool app_viewer::process_mask(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static msparse_coding::params params; static size_t n = 12; @@ -714,7 +714,7 @@ bool app_viewer::process_mask(viewer * p_view) bool app_viewer::process_pc_reconstruction(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static msparse_coding::params params; static size_t n = 12; @@ -750,7 +750,7 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) bool app_viewer::process_eigenfuntions(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static size_t n_eigs = 20; ImGui::InputScalar("n_eigs", ImGuiDataType_U64, &n_eigs); @@ -775,8 +775,8 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) break; } - view->idx_active_mesh = k; - che_viewer & mesh = view->active_mesh(); + view->idx_selected_mesh = k; + che_viewer & mesh = view->selected_mesh(); eigvec.col(k) -= eigvec.col(k).min(); eigvec.col(k) /= eigvec.col(k).max(); @@ -788,7 +788,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) mesh.update_vbo(); } - view->idx_active_mesh = 0; + view->idx_selected_mesh = 0; } return true; @@ -797,7 +797,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) bool app_viewer::process_descriptor_heatmap(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static int n_eigs = 50; static bool status = true; @@ -839,7 +839,7 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view) bool app_viewer::process_key_points(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static real_t percent = 0.1; if(ImGui_InputReal("percent", &percent, 0.01, 0.1, "%.2f")) @@ -854,7 +854,7 @@ bool app_viewer::process_key_points(viewer * p_view) bool app_viewer::process_key_components(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static real_t radio = 0.25; if(ImGui_InputReal("radio", &radio, 0.01, 0.1, "%.2f")) @@ -878,7 +878,7 @@ bool app_viewer::process_key_components(viewer * p_view) bool app_viewer::process_poisson(viewer * p_view, const index_t & k) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); size_t old_n_vertices = mesh->n_vertices; delete [] fill_all_holes(mesh); @@ -909,7 +909,7 @@ bool app_viewer::process_poisson_laplacian_3(viewer * p_view) bool app_viewer::process_fill_holes(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); // TODO // fill_all_holes(mesh); @@ -988,7 +988,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); size_t old_n_vertices, n_vertices = mesh->n_vertices; size_t n_holes = 0; // FIX_BOUND mesh->n_borders; @@ -1020,7 +1020,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) bool app_viewer::process_select_multiple(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static char line[128] = ""; @@ -1040,7 +1040,7 @@ bool app_viewer::process_select_multiple(viewer * p_view) bool app_viewer::process_threshold(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); for(index_t v = 0; v < mesh->n_vertices; ++v) mesh->heatmap(v) = mesh->heatmap(v) > 0.5 ? 1 : 0.5; @@ -1051,7 +1051,7 @@ bool app_viewer::process_threshold(viewer * p_view) bool app_viewer::process_noise(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); std::default_random_engine generator; std::uniform_int_distribution d_mod_5(0, 4); @@ -1072,7 +1072,7 @@ bool app_viewer::process_noise(viewer * p_view) bool app_viewer::process_black_noise(viewer * p_view) { app_viewer * view = (app_viewer *) p_view; - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); std::default_random_engine generator; std::uniform_int_distribution d_mod_5(0, 4); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 00c8919b..4667d6fc 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -125,7 +125,7 @@ void viewer::imgui() ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - che_viewer & mesh = active_mesh(); + che_viewer & mesh = selected_mesh(); if(ImGui::BeginMainMenuBar()) { @@ -134,9 +134,9 @@ void viewer::imgui() for(index_t i = 0; i < meshes.size(); ++i) { const che_viewer & m = *meshes[i]; - if(ImGui::MenuItem((std::to_string(i) + ": " + m->filename).c_str(), nullptr, i == idx_active_mesh, i != idx_active_mesh)) + if(ImGui::MenuItem((std::to_string(i) + ": " + m->filename).c_str(), nullptr, i == idx_selected_mesh, i != idx_selected_mesh)) { - idx_active_mesh = i; + idx_selected_mesh = i; glfwSetWindowTitle(window, m->filename.c_str()); } } @@ -178,6 +178,12 @@ void viewer::imgui() ImGui::EndMainMenuBar(); } + ImGui::SetNextWindowSize(ImVec2(72, -1)); + ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].y() - mesh.vy) * viewport_height - 100)); + ImGui::Begin("selected model", nullptr, ImGuiWindowFlags_NoTitleBar); + ImGui::TextColored({0, 1, 0, 1}, "SELECTED"); + ImGui::End(); + ImGui::SetNextWindowSize(ImVec2(window_width, -1)); ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); ImGui::Begin("status gproshan", nullptr, ImGuiWindowFlags_NoTitleBar); @@ -281,9 +287,9 @@ void viewer::imgui() ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } -che_viewer & viewer::active_mesh() +che_viewer & viewer::selected_mesh() { - return *meshes[idx_active_mesh]; + return *meshes[idx_selected_mesh]; } void viewer::info_gl() @@ -436,7 +442,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) che_viewer & mesh = *meshes.back(); mesh.log_info(); - idx_active_mesh = meshes.size() - 1; + idx_selected_mesh = meshes.size() - 1; glfwSetWindowTitle(window, mesh->filename.c_str()); const int & rows = m_window_split[meshes.size()].x(); @@ -508,7 +514,7 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod const int & cols = m_window_split[view->meshes.size()].y(); const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; if(idx_mesh < view->meshes.size()) - view->idx_active_mesh = idx_mesh; + view->idx_selected_mesh = idx_mesh; if(mods == GLFW_MOD_SHIFT) view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); @@ -645,7 +651,7 @@ bool viewer::m_reset_mesh(viewer * view) bool viewer::m_save_mesh(viewer * view) { - const che * mesh = view->active_mesh(); + const che * mesh = view->selected_mesh(); static char file[128] = "copy"; static int format = 0; @@ -759,7 +765,7 @@ bool viewer::m_bgc_black(viewer * view) bool viewer::m_setup_raytracing(viewer * view) { - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); static int rt = 0; static double time = 0; @@ -935,7 +941,7 @@ bool viewer::m_render_flat(viewer * view) bool viewer::m_raycasting(viewer * view) { - che_viewer & mesh = view->active_mesh(); + che_viewer & mesh = view->selected_mesh(); rt::embree rc({mesh}, {mesh.model_mat}); @@ -1038,7 +1044,7 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) void viewer::pick_vertex(const int & x, const int & y) { - che_viewer & mesh = active_mesh(); + che_viewer & mesh = selected_mesh(); mesh.select({x, y}, {viewport_width, viewport_height}, inverse(proj_view_mat), cam.eye); } @@ -1047,7 +1053,7 @@ void viewer::check_apply_all_meshes(const std::function & fu { if(!apply_all_meshes) { - fun(active_mesh()); + fun(selected_mesh()); return; } From 95ce1513c25aa5e2ef44dca0fc914f99434a052e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Sep 2023 16:51:45 +0200 Subject: [PATCH 0847/1018] viewer: fix meshes selected label --- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index c94549cb..b87bc304 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -769,7 +769,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) for(index_t k = 0; k < n_eigs; ++k) { - if(n_eigs) + if(k) { if(!view->add_mesh(new che(*mesh))) break; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4667d6fc..cec0f65a 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -178,11 +178,14 @@ void viewer::imgui() ImGui::EndMainMenuBar(); } - ImGui::SetNextWindowSize(ImVec2(72, -1)); - ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].y() - mesh.vy) * viewport_height - 100)); - ImGui::Begin("selected model", nullptr, ImGuiWindowFlags_NoTitleBar); - ImGui::TextColored({0, 1, 0, 1}, "SELECTED"); - ImGui::End(); + if(meshes.size() > 1) + { + ImGui::SetNextWindowSize(ImVec2(72, -1)); + ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].x() - mesh.vy) * viewport_height - 100)); + ImGui::Begin("selected model", nullptr, ImGuiWindowFlags_NoTitleBar); + ImGui::TextColored({0, 1, 0, 1}, "SELECTED"); + ImGui::End(); + } ImGui::SetNextWindowSize(ImVec2(window_width, -1)); ImGui::SetNextWindowPos(ImVec2(0, window_height - 32)); From 60e562831f00566b416ef736daea609abcbe7874 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 8 Sep 2023 00:45:29 +0200 Subject: [PATCH 0848/1018] viewer: transparent bg selected mesh label --- src/gproshan/viewer/viewer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cec0f65a..0e904283 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -181,7 +181,8 @@ void viewer::imgui() if(meshes.size() > 1) { ImGui::SetNextWindowSize(ImVec2(72, -1)); - ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].x() - mesh.vy) * viewport_height - 100)); + ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].x() - mesh.vy) * viewport_height - 70)); + ImGui::SetNextWindowBgAlpha(0.0f); ImGui::Begin("selected model", nullptr, ImGuiWindowFlags_NoTitleBar); ImGui::TextColored({0, 1, 0, 1}, "SELECTED"); ImGui::End(); From ace1d4e44cd6b512b9624a11fe3b447f5a9fb2aa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Sep 2023 15:54:26 +0200 Subject: [PATCH 0849/1018] knn: using flann to implement k3tree future use flann cuda --- CMakeLists.txt | 2 + include/gproshan/pointcloud/knn.h | 27 ++++++++--- src/gproshan/CMakeLists.txt | 1 + src/gproshan/app_viewer.cpp | 11 +++-- src/gproshan/pointcloud/knn.cpp | 74 +++++++++++++++++++++++++------ 5 files changed, 92 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 660ac385..6ba2b09b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ find_package(Armadillo REQUIRED) find_package(Eigen3 REQUIRED) find_package(CGAL REQUIRED) find_package(SuiteSparse REQUIRED) +find_package(flann REQUIRED) include_directories(SYSTEM ${embree_INCLUDE_DIRS}) @@ -68,6 +69,7 @@ include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS}) include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) +include_directories(SYSTEM ${flann_INCLUDE_DIRS}) add_subdirectory(src) # gproshan library diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 0d34b16f..8b156bb0 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -7,6 +7,8 @@ #include #include +#include + inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res) { @@ -26,24 +28,37 @@ struct std::hash // geometry processing and shape analysis framework -namespace gproshan { +namespace gproshan::knn { using point = vec3; -class grid_knn // grid +class grid { private: float res = 1000; std::vector points; - std::unordered_map > grid; + std::unordered_map > voxels; + + public: + grid(const point * pc, const size_t & n_points, const mat4 & transform); + ~grid() = default; + + std::vector operator () (const point & p, int k); +}; + +class k3tree +{ + private: + flann::Matrix indices; + public: - grid_knn(const point * pc, const size_t & n_points, const mat4 & transform); - virtual ~grid_knn() = default; + k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector & query = {}); + ~k3tree(); - std::vector operator () (const point & p, int knn); + int * operator () (const index_t & i); }; diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index a1136e33..ae78d148 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -13,6 +13,7 @@ target_link_libraries(gproshan X11::X11) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) target_link_libraries(gproshan CGAL::CGAL) target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) +target_link_libraries(gproshan flann::flann_cpp) target_link_libraries(gproshan ${OptiX_LIBRARY}) target_link_libraries(gproshan imgui) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index b87bc304..212355f2 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -130,13 +130,18 @@ bool app_viewer::process_knn(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + //grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + knn::k3tree nn(&mesh->point(0), mesh->n_vertices); if(mesh.selected.size()) { const index_t & p = mesh.selected.back(); - for(const index_t & v: knn(mesh.model_mat * (mesh->point(p), 1), 9)) - mesh.selected.push_back(v); + //for(const index_t & v: knn(mesh.model_mat * (mesh->point(p), 1), 9)) + // mesh.selected.push_back(v); + + const int * result = nn(p); + for(index_t i = 0; i < 8; ++i) + mesh.selected.push_back(result[i]); } return false; diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 76ec9523..6408fe62 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -5,10 +5,10 @@ // geometry processing and shape analysis framework -namespace gproshan { +namespace gproshan::knn { -grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) +grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) { double build_time = 0; @@ -21,7 +21,7 @@ grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & trans point & p = points[i]; p = transform * (pc[i], 1); - grid[hash(p, res)].push_back(i); + voxels[hash(p, res)].push_back(i); } TOC(build_time); @@ -29,11 +29,11 @@ grid_knn::grid_knn(const point * pc, const size_t & n_points, const mat4 & trans gproshan_log_var(sizeof(size_t)); gproshan_log_var(build_time); gproshan_log_var(res); - gproshan_log_var(grid.size()); - gproshan_log_var(double(n_points) / grid.size()); + gproshan_log_var(voxels.size()); + gproshan_log_var(double(n_points) / voxels.size()); } -std::vector grid_knn::operator () (const point & p, int knn) +std::vector grid::operator () (const point & p, int k) { const uvec3 key = hash(p, res); @@ -43,32 +43,78 @@ std::vector grid_knn::operator () (const point & p, int knn) for(int j = -1; j < 2; ++j) for(int k = -1; k < 2; ++k) { - const uvec3 cell = {key.x() + i, key.y() + j, key.z() + k}; + const uvec3 pos = {key.x() + i, key.y() + j, key.z() + k}; - if(cell.x() == NIL || cell.y() == NIL || cell.z() == NIL) + if(pos.x() == NIL || pos.y() == NIL || pos.z() == NIL) continue; - - if(grid.find(cell) == grid.end()) + + const auto & iter = voxels.find(pos); + if(iter == voxels.end()) continue; - for(const index_t & v: grid[cell]) + for(const index_t & v: iter->second) q.push({-length(p - points[v]), v}); } std::vector nn; - nn.reserve(knn); + nn.reserve(k); - while(!q.empty() && knn) + while(!q.empty() && k) { nn.push_back(q.top().second); q.pop(); - --knn; + --k; } return nn; } +///< Implementation using flann, by default compute all knn +k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, const std::vector & query) +{ + double time_build, time_query; + + TIC(time_build); + flann::Matrix mpc((real_t *) pc, n_points, 3); + flann::Index > index(mpc, flann::KDTreeSingleIndexParams()); + index.buildIndex(); + TOC(time_build); + gproshan_log_var(time_build); + + TIC(time_query); + const point * q = query.size() ? query.data() : pc; + const size_t & n_results = query.size() ? query.size() : n_points; + + flann::Matrix mq((real_t *) q, n_results, 3); + + indices = flann::Matrix(new int[n_results * k], n_results, k); + flann::Matrix dists(new real_t[n_results * k], n_results, k); + + flann::SearchParams params; + params.cores = 16; + index.knnSearch(mq, indices, dists, k, params); + TOC(time_query); + gproshan_log_var(time_query); + + + gproshan_log_var(time_build + time_query); + + + delete [] dists.ptr(); +} + +k3tree::~k3tree() +{ + delete [] indices.ptr(); +} + +int * k3tree::operator () (const index_t & i) +{ + return indices[i]; +} + + } // namespace gproshan From dc74b13746087a8d7ab344d0bb98f41d4466f3ae Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Sep 2023 16:24:55 +0200 Subject: [PATCH 0850/1018] viewer: computing knn for selected vertices --- include/gproshan/pointcloud/knn.h | 6 ++-- src/gproshan/app_viewer.cpp | 47 ++++++++++++++++++++++++------- src/gproshan/pointcloud/knn.cpp | 12 ++++---- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 8b156bb0..14fb19f3 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -45,7 +45,7 @@ class grid grid(const point * pc, const size_t & n_points, const mat4 & transform); ~grid() = default; - std::vector operator () (const point & p, int k); + std::vector operator () (const point & p, int k) const; }; @@ -53,12 +53,12 @@ class k3tree { private: flann::Matrix indices; - + public: k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector & query = {}); ~k3tree(); - int * operator () (const index_t & i); + int * operator () (const index_t & i) const; }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 212355f2..deab11e9 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -130,21 +130,48 @@ bool app_viewer::process_knn(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - //grid_knn knn(&mesh->point(0), mesh->n_vertices, mesh.model_mat); - knn::k3tree nn(&mesh->point(0), mesh->n_vertices); + static int alg = 0; + static int k = 9; + ImGui::Combo("algorithm", &alg, "select\0grid\0k3tree\0\0"); + ImGui::InputInt("k", &k); - if(mesh.selected.size()) + if(ImGui::Button("Run")) { - const index_t & p = mesh.selected.back(); - //for(const index_t & v: knn(mesh.model_mat * (mesh->point(p), 1), 9)) - // mesh.selected.push_back(v); + auto query = mesh.selected; + if(!query.size()) query.push_back(0); + + mesh.selected.clear(); + + switch(alg) + { + case 1: + { + knn::grid grid(&mesh->point(0), mesh->n_vertices, mesh.model_mat); + for(const index_t & p: query) + { + for(const index_t & v: grid(mesh.model_mat * (mesh->point(p), 1), k)) + mesh.selected.push_back(v); + } + } + break; - const int * result = nn(p); - for(index_t i = 0; i < 8; ++i) - mesh.selected.push_back(result[i]); + case 2: + { + knn::k3tree k3tree(&mesh->point(0), mesh->n_vertices, k); + for(const index_t & p: query) + { + const int * result = k3tree(p); + for(index_t i = 0; i < 8; ++i) + mesh.selected.push_back(result[i]); + } + } + break; + + default: break; + } } - return false; + return true; } bool app_viewer::process_compute_normals(viewer * p_view) diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 6408fe62..2d0da2f1 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -33,7 +33,7 @@ grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): p gproshan_log_var(double(n_points) / voxels.size()); } -std::vector grid::operator () (const point & p, int k) +std::vector grid::operator () (const point & p, int k) const { const uvec3 key = hash(p, res); @@ -47,7 +47,7 @@ std::vector grid::operator () (const point & p, int k) if(pos.x() == NIL || pos.y() == NIL || pos.z() == NIL) continue; - + const auto & iter = voxels.find(pos); if(iter == voxels.end()) continue; @@ -86,22 +86,20 @@ k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, cons TIC(time_query); const point * q = query.size() ? query.data() : pc; const size_t & n_results = query.size() ? query.size() : n_points; - + flann::Matrix mq((real_t *) q, n_results, 3); indices = flann::Matrix(new int[n_results * k], n_results, k); flann::Matrix dists(new real_t[n_results * k], n_results, k); - + flann::SearchParams params; params.cores = 16; index.knnSearch(mq, indices, dists, k, params); TOC(time_query); gproshan_log_var(time_query); - gproshan_log_var(time_build + time_query); - delete [] dists.ptr(); } @@ -110,7 +108,7 @@ k3tree::~k3tree() delete [] indices.ptr(); } -int * k3tree::operator () (const index_t & i) +int * k3tree::operator () (const index_t & i) const { return indices[i]; } From 004693fc94808d804096fb29a95cc1fe7f5dc2ee Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Sep 2023 20:58:06 +0200 Subject: [PATCH 0851/1018] fix github workflows --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d2b361a..799cf39f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,6 +27,7 @@ jobs: sudo apt update sudo apt install \ libarmadillo-dev \ + libflann-dev \ libeigen3-dev \ libcgal-dev \ libsuitesparse-dev \ From 352be42f36170300dc10fd47725674dcbcbdc463 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Sep 2023 14:39:41 +0200 Subject: [PATCH 0852/1018] viewer: recording timeframe --- include/gproshan/viewer/viewer.h | 5 +++++ src/gproshan/viewer/viewer.cpp | 2 ++ 2 files changed, 7 insertions(+) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index f96f4f4d..8438c955 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -31,6 +31,7 @@ // geometry processing and shape analysis framework namespace gproshan { +const size_t max_nframes = 1000; class viewer { @@ -80,6 +81,8 @@ class viewer quaternion cam_light; double render_time = 0; + double frame_time[max_nframes] = {}; + index_t nframes = 0; std::vector meshes; index_t idx_selected_mesh = 0; @@ -121,6 +124,8 @@ class viewer void render_gl(); void render_rt(che_viewer & mesh, frame & rt_frame); + void save_frametime(); + static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); static void keyboard_callback(GLFWwindow * window, int key, int scancode, int action, int mods); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 0e904283..9752918b 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -205,7 +205,9 @@ void viewer::imgui() ImGui::Checkbox("apply options to all meshes\nmenus: [color, render, mesh]", &apply_all_meshes); if(ImGui::CollapsingHeader(mesh->filename.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) { + frame_time[(++nframes) % max_nframes] = render_time; ImGui::Text("%13lu fps", size_t(1.0 / render_time)); + ImGui::Text("%13.4f ms", render_time); ImGui::Text("%13lu vertices", mesh->n_vertices); ImGui::Text("%13lu trigs", mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_trigs); From afbc3e3b375bb9767d917bae4c33227105fdf555 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Sep 2023 16:49:20 +0200 Subject: [PATCH 0853/1018] viewer: saving frametimes --- include/gproshan/viewer/viewer.h | 4 ++-- src/gproshan/viewer/viewer.cpp | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 8438c955..a530eb2f 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -81,7 +81,7 @@ class viewer quaternion cam_light; double render_time = 0; - double frame_time[max_nframes] = {}; + double frametime[max_nframes] = {}; index_t nframes = 0; std::vector meshes; @@ -124,7 +124,7 @@ class viewer void render_gl(); void render_rt(che_viewer & mesh, frame & rt_frame); - void save_frametime(); + void save_frametime(const std::string & file); static void framebuffer_size_callback(GLFWwindow * window, int width, int height); static void window_size_callback(GLFWwindow * window, int width, int height); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 9752918b..e23f915d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -114,6 +114,8 @@ bool viewer::run() glfwPollEvents(); } + save_frametime(tmp_file_path("frametime")); + return true; } @@ -205,7 +207,7 @@ void viewer::imgui() ImGui::Checkbox("apply options to all meshes\nmenus: [color, render, mesh]", &apply_all_meshes); if(ImGui::CollapsingHeader(mesh->filename.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) { - frame_time[(++nframes) % max_nframes] = render_time; + frametime[(++nframes) % max_nframes] = render_time; ImGui::Text("%13lu fps", size_t(1.0 / render_time)); ImGui::Text("%13.4f ms", render_time); ImGui::Text("%13lu vertices", mesh->n_vertices); @@ -468,6 +470,18 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) return true; } +void viewer::save_frametime(const std::string & file) +{ + gproshan_error_var(file); + + FILE * fp = fopen(file.c_str(), "w"); + + for(index_t i = 0; i < max_nframes; ++i) + fprintf(fp, "%.3f\n", frametime[(nframes + i) % max_nframes]); + + fclose(fp); +} + void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); From 8e1d44a70538e87bf871c0c7a95844bf94431aec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Sep 2023 18:08:45 +0200 Subject: [PATCH 0854/1018] viewer: save gproshan history --- include/gproshan/viewer/viewer.h | 1 + src/gproshan/viewer/viewer.cpp | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a530eb2f..c86bfa2c 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -124,6 +124,7 @@ class viewer void render_gl(); void render_rt(che_viewer & mesh, frame & rt_frame); + void save_history(const std::string & file); void save_frametime(const std::string & file); static void framebuffer_size_callback(GLFWwindow * window, int width, int height); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e23f915d..cdeca11a 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -75,6 +75,9 @@ viewer::viewer(const int & width, const int & height) viewer::~viewer() { + save_history(tmp_file_path("history")); + save_frametime(tmp_file_path("frametime_" + selected_mesh()->name_size())); + ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); @@ -114,8 +117,6 @@ bool viewer::run() glfwPollEvents(); } - save_frametime(tmp_file_path("frametime")); - return true; } @@ -470,6 +471,21 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) return true; } +void viewer::save_history(const std::string & file) +{ + gproshan_error_var(file); + + FILE * fp = fopen(file.c_str(), "a"); + + const che_viewer & m = *meshes[0]; + fprintf(fp, "%s ", m->name().c_str()); + fprintf(fp, "%lu ", m->n_vertices); + fprintf(fp, "%lu ", m->n_trigs); + fprintf(fp, "%p\n", this); + + fclose(fp); +} + void viewer::save_frametime(const std::string & file) { gproshan_error_var(file); @@ -477,8 +493,8 @@ void viewer::save_frametime(const std::string & file) FILE * fp = fopen(file.c_str(), "w"); for(index_t i = 0; i < max_nframes; ++i) - fprintf(fp, "%.3f\n", frametime[(nframes + i) % max_nframes]); - + fprintf(fp, "%f\n", frametime[(nframes + i) % max_nframes]); + fclose(fp); } @@ -512,7 +528,6 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; snprintf(view->status_message, sizeof(view->status_message), "%s", pro.selected ? pro.name.c_str() : ""); } - } void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) From cea80acaef7dcc255b7b5c270a4cf7b9e56781d6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Sep 2023 15:55:59 +0200 Subject: [PATCH 0855/1018] viewer: saving history and frametime --- src/gproshan/viewer/viewer.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cdeca11a..b3d83d4c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -75,8 +75,8 @@ viewer::viewer(const int & width, const int & height) viewer::~viewer() { - save_history(tmp_file_path("history")); - save_frametime(tmp_file_path("frametime_" + selected_mesh()->name_size())); + sprintf(status_message, "frametime_%p", this); + save_frametime(tmp_file_path(status_message)); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); @@ -468,6 +468,8 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) cam.aspect = real_t(viewport_width) / viewport_height; proj_mat = cam.perspective(); + save_history(tmp_file_path("history")); + return true; } @@ -478,10 +480,10 @@ void viewer::save_history(const std::string & file) FILE * fp = fopen(file.c_str(), "a"); const che_viewer & m = *meshes[0]; + fprintf(fp, "%p ", this); fprintf(fp, "%s ", m->name().c_str()); fprintf(fp, "%lu ", m->n_vertices); - fprintf(fp, "%lu ", m->n_trigs); - fprintf(fp, "%p\n", this); + fprintf(fp, "%lu\n", m->n_trigs); fclose(fp); } From 3cc81571a0202a048282687db24fdafede315fbc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Sep 2023 16:06:14 +0200 Subject: [PATCH 0856/1018] knn: remove kdtree, resolved #23 --- include/gproshan/pointcloud/kdtree.h | 31 -------------------------- src/gproshan/pointcloud/kdtree.cpp | 33 ---------------------------- 2 files changed, 64 deletions(-) delete mode 100644 include/gproshan/pointcloud/kdtree.h delete mode 100644 src/gproshan/pointcloud/kdtree.cpp diff --git a/include/gproshan/pointcloud/kdtree.h b/include/gproshan/pointcloud/kdtree.h deleted file mode 100644 index 912b1abe..00000000 --- a/include/gproshan/pointcloud/kdtree.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef KDTREE_H -#define KDTREE_H - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -using vertex = vec3; - - -class kdtree -{ - private: - index_t * nodes = nullptr; - - public: - kdtree(const vertex * pointcloud, const size_t & n_points); - virtual ~kdtree(); - - private: - void build(const index_t & n, const vertex * pc, const index_t & i, const index_t & j, const index_t & d); -}; - - -} // namespace gproshan - -#endif // KDTREE_H - diff --git a/src/gproshan/pointcloud/kdtree.cpp b/src/gproshan/pointcloud/kdtree.cpp deleted file mode 100644 index 8c3c0cf0..00000000 --- a/src/gproshan/pointcloud/kdtree.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -kdtree::kdtree(const vertex * pointcloud, const size_t & n_points) -{ - nodes = new index_t[n_points >> 1]; - - build(0, pointcloud, 0, n_points, 0); -} - -kdtree::~kdtree() -{ - delete [] nodes; -} - -void kdtree::build(const index_t & n, const vertex * , const index_t & i, const index_t & j, const index_t & ) -{ - if(i == j) - { - nodes[n] = i; - return; - } -} - - -} // namespace gproshan - From 637a87812c766e703092666b2944d70406d2a348 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Sep 2023 17:30:33 +0200 Subject: [PATCH 0857/1018] pointcloud: update k3tree knn access methods --- include/gproshan/pointcloud/knn.h | 4 +++- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/pointcloud/knn.cpp | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 14fb19f3..db6dc608 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -58,7 +58,9 @@ class k3tree k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector & query = {}); ~k3tree(); - int * operator () (const index_t & i) const; + const int * operator [] (const index_t & i) const; + const int * operator () (const index_t & i) const; + const int & operator () (const index_t & i, const index_t & j) const; }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index deab11e9..cf56673f 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -161,7 +161,7 @@ bool app_viewer::process_knn(viewer * p_view) for(const index_t & p: query) { const int * result = k3tree(p); - for(index_t i = 0; i < 8; ++i) + for(int i = 0; i < k; ++i) mesh.selected.push_back(result[i]); } } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 2d0da2f1..59e1e3f2 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -108,11 +108,21 @@ k3tree::~k3tree() delete [] indices.ptr(); } -int * k3tree::operator () (const index_t & i) const +const int * k3tree::operator [] (const index_t & i) const { return indices[i]; } +const int * k3tree::operator () (const index_t & i) const +{ + return indices[i]; +} + +const int & k3tree::operator () (const index_t & i, const index_t & j) const +{ + return indices[i][j]; +} + } // namespace gproshan From 89edb18cb07219e01e9b96aefed5668e0f4dca88 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Thu, 21 Sep 2023 17:50:28 +0200 Subject: [PATCH 0858/1018] Update build.yml --- .github/workflows/build.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 799cf39f..e0b9f6f3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,6 @@ jobs: sudo apt update sudo apt install \ libarmadillo-dev \ - libflann-dev \ libeigen3-dev \ libcgal-dev \ libsuitesparse-dev \ @@ -36,6 +35,11 @@ jobs: libglfw3-dev \ cimg-dev \ wget + wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann-dev_1.9.2+dfsg-1_amd64.deb + wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann1.9_1.9.2+dfsg-1_amd64.deb + sudo dpkg -i libflann1.9_1.9.2+dfsg-1_amd64.deb + sudo dpkg -i libflann-dev_1.9.2+dfsg-1_amd64.deb + sudo apt install -f - name: Install Cuda if: matrix.cuda == 'cuda' From e567b0ca3d732a86a14bb56e6a5aa9bb1c4c8e47 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Thu, 21 Sep 2023 17:59:02 +0200 Subject: [PATCH 0859/1018] Update build.yml --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e0b9f6f3..f2baa3ad 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,9 +37,9 @@ jobs: wget wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann-dev_1.9.2+dfsg-1_amd64.deb wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann1.9_1.9.2+dfsg-1_amd64.deb - sudo dpkg -i libflann1.9_1.9.2+dfsg-1_amd64.deb - sudo dpkg -i libflann-dev_1.9.2+dfsg-1_amd64.deb - sudo apt install -f + sudo dpkg -i libflann1.9_1.9.2+dfsg-1_amd64.deb || sudo apt install -f + sudo dpkg -i libflann-dev_1.9.2+dfsg-1_amd64.deb || sudo apt install -f + - name: Install Cuda if: matrix.cuda == 'cuda' From 3ce3b6891060b985665c091cf9611fa9f89e3a7d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 27 Sep 2023 16:58:06 +0200 Subject: [PATCH 0860/1018] viewer: save rt built times --- include/gproshan/raytracing/embree.h | 2 +- src/gproshan/mesh/che_pcd.cpp | 2 ++ src/gproshan/mesh/che_pts.cpp | 1 + src/gproshan/mesh/che_ptx.cpp | 2 ++ src/gproshan/viewer/viewer.cpp | 16 +++++++++++++--- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 7ef1a6cc..4e556f9d 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -37,7 +37,7 @@ class embree : public raytracing std::vector g_meshes; scene_data sc; - float pc_radius = 1; + float pc_radius = 0.01; public: embree(); diff --git a/src/gproshan/mesh/che_pcd.cpp b/src/gproshan/mesh/che_pcd.cpp index 228961db..7024c4af 100644 --- a/src/gproshan/mesh/che_pcd.cpp +++ b/src/gproshan/mesh/che_pcd.cpp @@ -78,6 +78,8 @@ void che_pcd::read_file(const std::string & file) GT[v] = {x, y, z}; } } + + fclose(fp); } void che_pcd::write_file(const che * mesh, const std::string & file) diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index 7b80a020..1860eff1 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -57,6 +57,7 @@ void che_pts::write_file(const che * mesh, const std::string & file) fprintf(fp, " %d ", int(mesh->heatmap(i) * 4095) - 2048); fprintf(fp, "%hhu %hhu %hhu\n", c.r, c.g, c.b); } + fclose(fp); } diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index ae86ac43..851e8d55 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -144,6 +144,8 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) h, c.r, c.g, c.b ); } + + fclose(fp); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b3d83d4c..e65cacc7 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -806,10 +806,8 @@ bool viewer::m_setup_raytracing(viewer * view) static int rt = 0; static double time = 0; - static float pc_radius = 0.01; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); - ImGui::InputFloat("pc_radius (if render_pointcloud)", &pc_radius, 0, 0, "%.3f"); if(ImGui::Button("Build")) { @@ -820,7 +818,7 @@ bool viewer::m_setup_raytracing(viewer * view) case R_EMBREE: delete mesh.rt_embree; TIC(time); - mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud, pc_radius); + mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud); TOC(time); snprintf(view->status_message, sizeof(view->status_message), "build embree in %.3fs", time); break; @@ -835,6 +833,18 @@ bool viewer::m_setup_raytracing(viewer * view) #endif // GPROSHAN_OPTIX break; } + + + FILE * fp = fopen(tmp_file_path("rt_build_times").c_str(), "a"); + + fprintf(fp, "dev %p ", view); + fprintf(fp, "%s ", mesh->name().c_str()); + fprintf(fp, "%lu ", mesh->n_vertices); + fprintf(fp, "%lu ", mesh->n_trigs); + fprintf(fp, "%u ", rt); + fprintf(fp, "%f\n", time); + + fclose(fp); } return true; From 91e3e9dc165d8a8bb23f91c09257978e63160acf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 5 Oct 2023 14:12:47 +0200 Subject: [PATCH 0861/1018] viewer: method for update status message --- include/gproshan/viewer/viewer.h | 1 + src/gproshan/app_viewer.cpp | 8 ++++---- src/gproshan/viewer/viewer.cpp | 20 ++++++++++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index c86bfa2c..3d5283a9 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -109,6 +109,7 @@ class viewer che_viewer & selected_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); bool add_mesh(che * p_mesh, const bool & reset_normals = true); + void update_status_message(const char * format, ...); protected: virtual bool run(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index cf56673f..9a0189b3 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -59,7 +59,7 @@ int app_viewer::main(int nargs, const char ** args) for(int i = 1; i < nargs; ++i) add_mesh(load_mesh(args[i])); TOC(time) - snprintf(status_message, sizeof(status_message), "meshes loaded in %.3fs", time); + update_status_message("meshes loaded in %.3fs", time); init(); run(); @@ -316,7 +316,7 @@ bool app_viewer::process_connected_components(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) label[v] /= nc; - snprintf(view->status_message, sizeof(view->status_message), "found %d connected components", nc); + view->update_status_message("found %d connected components", nc); mesh.update_vbo_heatmap(); return false; @@ -527,7 +527,7 @@ bool app_viewer::process_geodesics(viewer * p_view) TIC(view->time) geodesics dm(mesh, mesh.selected, params); TOC(view->time) - snprintf(view->status_message, sizeof(view->status_message), "geodesics time: %.3fs", view->time); + view->update_status_message("geodesics time: %.3fs", view->time); dm.normalize(); mesh.update_vbo_heatmap(); @@ -797,7 +797,7 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); TOC(view->time) - snprintf(view->status_message, sizeof(view->status_message), "computing %lu eigs in %.3fs", n_eigs, view->time); + view->update_status_message("computing %lu eigs in %.3fs", n_eigs, view->time); for(index_t k = 0; k < n_eigs; ++k) { diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index e65cacc7..4362bf06 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -75,7 +75,7 @@ viewer::viewer(const int & width, const int & height) viewer::~viewer() { - sprintf(status_message, "frametime_%p", this); + update_status_message("frametime_%p", this); save_frametime(tmp_file_path(status_message)); ImGui_ImplOpenGL3_Shutdown(); @@ -170,7 +170,7 @@ void viewer::imgui() process_t & pro = p.second; if(pro.function != nullptr && pro.sub_menu == i) if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) - snprintf(status_message, sizeof(status_message), "%s", pro.selected ? pro.name.c_str() : ""); + update_status_message("%s", pro.selected ? pro.name.c_str() : ""); //ImGui::Separator(); } @@ -473,6 +473,14 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) return true; } +void viewer::update_status_message(const char * format, ...) +{ + va_list args; + va_start(args, format); + vsnprintf(status_message, sizeof(status_message), format, args); +} + + void viewer::save_history(const std::string & file) { gproshan_error_var(file); @@ -528,7 +536,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in if(pro.function) { pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; - snprintf(view->status_message, sizeof(view->status_message), "%s", pro.selected ? pro.name.c_str() : ""); + view->update_status_message("%s", pro.selected ? pro.name.c_str() : ""); } } @@ -735,7 +743,7 @@ bool viewer::m_save_mesh(viewer * view) break; } - snprintf(view->status_message, sizeof(view->status_message), "file '%s' saved.", file); + view->update_status_message("file '%s' saved.", file); } return true; @@ -820,7 +828,7 @@ bool viewer::m_setup_raytracing(viewer * view) TIC(time); mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud); TOC(time); - snprintf(view->status_message, sizeof(view->status_message), "build embree in %.3fs", time); + view->update_status_message("build embree in %.3fs", time); break; case R_OPTIX: @@ -829,7 +837,7 @@ bool viewer::m_setup_raytracing(viewer * view) TIC(time); mesh.rt_optix = new rt::optix({mesh}, {mesh.model_mat}); TOC(time); - snprintf(view->status_message, sizeof(view->status_message), "build optix in %.3fs", time); + view->update_status_message("build optix in %.3fs", time); #endif // GPROSHAN_OPTIX break; } From a2025b743548f6b52caf820b4bc5ab983eb307f7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 9 Oct 2023 02:33:57 +0200 Subject: [PATCH 0862/1018] viewer: update default window size --- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/pointcloud/knn.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 3d5283a9..fd7a9200 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -103,7 +103,7 @@ class viewer char status_message[1024] = {}; public: - viewer(const int & width = 1920, const int & height = 1080); + viewer(const int & width = 1600, const int & height = 900); virtual ~viewer(); che_viewer & selected_mesh(); diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 59e1e3f2..8dd43481 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -21,7 +21,7 @@ grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): p point & p = points[i]; p = transform * (pc[i], 1); - voxels[hash(p, res)].push_back(i); + voxels[hash(p, res)].push_back(i); } TOC(build_time); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4362bf06..bd196d28 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -331,6 +331,8 @@ void viewer::init_gl() glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif + gproshan_log_var(window_width); + gproshan_log_var(window_height); window = glfwCreateWindow(window_width, window_height, "gproshan", NULL, NULL); glfwSetWindowUserPointer(window, this); From 57eaa1abdbab2b28bee2a0f7b4a7fdd11f18ec13 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 12:32:23 +0200 Subject: [PATCH 0863/1018] viewer: updating blinn phon shading --- include/gproshan/raytracing/utils.h | 8 ++++---- shaders/shading.glsl | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 3646096c..eca3b96c 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -136,11 +136,11 @@ template __host_device__ vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) { - const T Lp = 8; + const T Lp = 4; const vec La(0.2f); vec li, l, h; - vec v = normalize(eye - hit.position); + const vec v = normalize(eye - hit.position); const vec & n = hit.normal; T lambertian; @@ -155,10 +155,10 @@ vec eval_li(const t_eval_hit & hit, const vec * lights, const int #ifdef __CUDACC__ lambertian = max(dot(l, n), 0.f); - specular = pow(max(dot(h, n), 0.f), hit.Ns); + specular = powf(max(dot(h, n), 0.f), hit.Ns); #else lambertian = std::max(dot(l, n), 0.f); - specular = pow(std::max(dot(h, n), 0.f), hit.Ns); + specular = powf(std::max(dot(h, n), 0.f), hit.Ns); #endif // __CUDACC__ const vec & color = hit.Ka * La + (lambertian * hit.Kd + specular * hit.Ks) * Lp / (r * r); diff --git a/shaders/shading.glsl b/shaders/shading.glsl index d5352a0c..6ad20aa8 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -13,8 +13,8 @@ uniform sampler2D tex_Ks; uniform material mat; -const float Lp = 10; -const vec3 La = vec3(0.2, 0.2, 0.2); +const float Lp = 4; +const vec3 La = vec3(0.1); vec3 lines_colormap(vec3 color, float h) @@ -35,10 +35,10 @@ vec3 lines_colormap(vec3 color, float h) vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) { - vec3 Ka = vec3(0.4); + vec3 Ka = vec3(1,0,0); vec3 Kd = color; vec3 Ks = vec3(0.2); - float Ns = 4; + float Ns = 10; if(idx_colormap == 5) { From 0a869442e19db540c9dc0a846ddef9d4e2d2da23 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 13:51:26 +0200 Subject: [PATCH 0864/1018] viewer: updating shading default values --- include/gproshan/raytracing/utils.h | 4 ++-- include/gproshan/scenes/scene.h | 8 ++++---- shaders/shading.glsl | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index eca3b96c..a14ea018 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -136,8 +136,8 @@ template __host_device__ vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) { - const T Lp = 4; - const vec La(0.2f); + const T Lp = 10; + const vec La(0.1f); vec li, l, h; const vec v = normalize(eye - hit.position); diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index c24b2626..7e2cbb08 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -25,11 +25,11 @@ class scene: public che struct material { - vec3 Ka = {0.2f, 0.2f, 0.2f}; - vec3 Kd = {0.8f, 0.8f, 0.8f}; - vec3 Ks = {1, 1, 1}; + vec3 Ka = 1; + vec3 Kd = 0.8; + vec3 Ks = 0.2; real_t d = 1; // Tr = 0, opposite - real_t Ns = 0; + real_t Ns = 10; real_t Ni = 0; int illum = 1; int map_Ka = -1; diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 6ad20aa8..4fd0f3ce 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -13,7 +13,7 @@ uniform sampler2D tex_Ks; uniform material mat; -const float Lp = 4; +const float Lp = 10; const vec3 La = vec3(0.1); @@ -35,7 +35,7 @@ vec3 lines_colormap(vec3 color, float h) vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) { - vec3 Ka = vec3(1,0,0); + vec3 Ka = vec3(1); vec3 Kd = color; vec3 Ks = vec3(0.2); float Ns = 10; From d786d4660d7cc56e1670c52e98d28f98c340e9a8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 14:38:29 +0200 Subject: [PATCH 0865/1018] viewer: adding light struct --- include/gproshan/geometry/vec.h | 4 ---- include/gproshan/raytracing/embree.h | 2 +- include/gproshan/raytracing/light.h | 22 +++++++++++++++++++++ include/gproshan/raytracing/optix_params.h | 4 +++- include/gproshan/raytracing/raytracing.h | 3 ++- include/gproshan/raytracing/render_params.h | 12 +++++++---- include/gproshan/raytracing/utils.h | 15 +++++++------- src/gproshan/raytracing/embree.cpp | 4 ++-- src/gproshan/raytracing/optix.cu | 2 +- src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 8 ++++---- 11 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 include/gproshan/raytracing/light.h diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index b41e6455..1e8d227f 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -320,9 +320,7 @@ vec normalize(const vec & v) return v / norm(v); } -///< std std::ostream template -__host_device__ std::ostream & operator << (std::ostream & os, const vec & v) { os << v[0]; @@ -331,9 +329,7 @@ std::ostream & operator << (std::ostream & os, const vec & v) return os; } -///< std std::istream template -__host_device__ std::istream & operator >> (std::istream & is, vec & v) { for(index_t i = 0; i < N; ++i) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 4e556f9d..44025abe 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -59,7 +59,7 @@ class embree : public raytracing virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const vertex & cam_pos, const bool & flat); + virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat); float intersect_depth(const vertex & org, const vertex & dir); diff --git a/include/gproshan/raytracing/light.h b/include/gproshan/raytracing/light.h new file mode 100644 index 00000000..bf438e77 --- /dev/null +++ b/include/gproshan/raytracing/light.h @@ -0,0 +1,22 @@ +#ifndef LIGHT_H +#define LIGHT_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +struct light +{ + vec3 pos = 0; + vec3 color = 1; + real_t power = 10; +}; + + +} // namespace gproshan + +#endif // LIGHT_H + diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index e455e8f7..47369572 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef GPROSHAN_OPTIX @@ -26,7 +27,8 @@ struct launch_params int viewport_y = 0; int n_samples = 0; int n_lights = 0; - vec3 lights[16]; + light lights[NL]; + light ambient; vec3 cam_pos; mat4 inv_proj_view; bool flat; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 73273826..79076431 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -40,7 +40,8 @@ class raytracing protected: virtual vec3 closesthit_radiance( const vertex &, // org const vertex &, // dir - const vertex *, // lights + const light &, // ambient light + const light *, // lights const int &, // n_lights const vertex &, // cam_pos const bool & // flat diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 2edba67e..c1e8d218 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -3,12 +3,15 @@ #include #include +#include // geometry processing and shape analysis framework namespace gproshan::rt { +const size_t NL = 16; // number of lights + struct render_params { int window_width = 0; @@ -18,18 +21,19 @@ struct render_params int viewport_x = 0; int viewport_y = 0; int n_lights = 0; - vertex lights[16]; + light lights[NL]; + light ambient = {0, 1, 0.1}; vertex cam_pos; mat4 inv_proj_view; bool restart = false; bool viewport_is_window = true; - bool add_light(const vertex & light) + bool add_light(const light & l) { - if(n_lights == sizeof(lights) / sizeof(vertex)) + if(n_lights == NL) return false; - lights[n_lights++] = light; + lights[n_lights++] = l; return true; } diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index a14ea018..e1a40469 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -6,6 +6,7 @@ #include #include +#include // geometry processing and shape analysis framework @@ -134,20 +135,20 @@ struct t_eval_hit template __host_device__ -vec eval_li(const t_eval_hit & hit, const vec * lights, const int & n_lights, const vec & eye, Occluded occluded) +vec eval_li(const t_eval_hit & hit, const light & ambient, const light * lights, const int & n_lights, const vec & eye, Occluded occluded) { - const T Lp = 10; - const vec La(0.1f); - - vec li, l, h; const vec v = normalize(eye - hit.position); const vec & n = hit.normal; T lambertian; T specular; + vec li, l, h; + for(int i = 0; i < n_lights; ++i) { - l = lights[i] - hit.position; + const light & L = lights[i]; + + l = L.pos - hit.position; const T & r = length(l); l /= r; @@ -161,7 +162,7 @@ vec eval_li(const t_eval_hit & hit, const vec * lights, const int specular = powf(std::max(dot(h, n), 0.f), hit.Ns); #endif // __CUDACC__ - const vec & color = hit.Ka * La + (lambertian * hit.Kd + specular * hit.Ks) * Lp / (r * r); + const vec & color = hit.Ka * ambient.color * ambient.power + (lambertian * hit.Kd + specular * hit.Ks) * L.color * L.power / (r * r); li += (occluded(hit.position, l, r) ? 0.4f : 1.0f) * color; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 2ef5fef4..7ad3bd4c 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -246,7 +246,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const vertex * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) +vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -259,7 +259,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const v hit.position = r.pos(); hit.normal = flat ? r.normal() : hit.normal; - return eval_li( hit, lights, n_lights, cam_pos, + return eval_li( hit, ambient, lights, n_lights, cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index fff090b0..b42dabec 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -58,7 +58,7 @@ extern "C" __global__ void __closesthit__radiance() hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - vec3 li = eval_li(hit, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, + vec3 li = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { uint32_t occluded = 1; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index d792c11d..80b90931 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -33,7 +33,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f params.cam_pos ); - vec3 li = closesthit_radiance(params.cam_pos, dir, params.lights, params.n_lights, params.cam_pos, flat); + vec3 li = closesthit_radiance(params.cam_pos, dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); color = (color * n_samples + (li, 1)) / (n_samples + 1); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index bd196d28..a43ed28d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -70,7 +70,7 @@ viewer::viewer(const int & width, const int & height) frames = new frame[max_meshes]; - render_params.add_light({-1, 1, -4}); + render_params.add_light({{-1, 1, -4}}); } viewer::~viewer() @@ -101,7 +101,7 @@ bool viewer::run() const quaternion & r = cam.current_rotation(); - cam_light = render_params.lights[0]; + cam_light = render_params.lights[0].pos; cam_light = r.conj() * cam_light * r; proj_view_mat = proj_mat * cam.look_at(r); @@ -262,13 +262,13 @@ void viewer::imgui() { sphere_points.clear(); for(int i = 0; i < render_params.n_lights; ++i) - sphere_points.push_back(render_params.lights[i]); + sphere_points.push_back(render_params.lights[i].pos); } if(ImGui::Button("add selected points as lights")) { for(const index_t & v: mesh.selected) - if(!render_params.add_light(mesh.model_mat * (mesh->point(v), 1))) + if(!render_params.add_light({vec3(mesh.model_mat * (mesh->point(v), 1))})) break; } From 213de8f851a93173def7feb7ce42fbdc93265017 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 15:03:46 +0200 Subject: [PATCH 0866/1018] viewer: adding struct light to shader --- include/gproshan/raytracing/utils.h | 8 ++++---- shaders/shading.glsl | 16 +++++++++++----- src/gproshan/viewer/viewer.cpp | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index e1a40469..81befc06 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -69,10 +69,10 @@ struct t_eval_hit T u = 0, v = 0; vec position; vec normal; - vec Ka{0.4f, 0.4f, 0.4f}; - vec Kd{0.9f, 0.94f, 0.98f}; - vec Ks{0.2f, 0.2f, 0.2f}; - T Ns = 4; + vec Ka = 1; + vec Kd = 0.5; + vec Ks = 0.2; + T Ns = 10; T d = 1; __host_device__ diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 4fd0f3ce..e271a344 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -5,7 +5,6 @@ uniform uint idx_colormap; uniform bool render_lines; uniform vec3 eye; -uniform vec3 cam_light; uniform sampler2D tex_Ka; uniform sampler2D tex_Kd; @@ -13,9 +12,15 @@ uniform sampler2D tex_Ks; uniform material mat; -const float Lp = 10; -const vec3 La = vec3(0.1); +struct light +{ + vec3 pos; + vec3 color; + float power; +}; +uniform light ambient; +uniform light cam_light; vec3 lines_colormap(vec3 color, float h) { @@ -57,7 +62,7 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) Ns = mat.Ns; } - vec3 l = cam_light - pos; + vec3 l = cam_light.pos - pos; float r = length(l); l /= r; vec3 v = normalize(eye - pos); @@ -65,7 +70,8 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) float lambertian = max(dot(l, n), 0.0); float specular = pow(max(dot(h, n), 0.0), Ns); - return Ka * La + (lambertian * Kd + specular * Ks) * Lp / (r * r); + return Ka * ambient.color * ambient.power + + (lambertian * Kd + specular * Ks) * cam_light.color * cam_light.power / (r * r); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a43ed28d..b471b95d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -1024,9 +1024,18 @@ void viewer::render_gl() shader_triangles.uniform("eye", cam.eye.v); shader_pointcloud.uniform("eye", cam.eye.v); - shader_sphere.uniform("cam_light", cam_light); - shader_triangles.uniform("cam_light", cam_light); - shader_pointcloud.uniform("cam_light", cam_light); + const light & ambient = render_params.ambient; + const light & l = render_params.lights[0]; + for(shader * program: {&shader_sphere, &shader_triangles, &shader_pointcloud}) + { + program->uniform("ambient.pos", ambient.pos); + program->uniform("ambient.color", ambient.color); + program->uniform("ambient.power", ambient.power); + + program->uniform("cam_light.pos", cam_light); + program->uniform("cam_light.color", l.color); + program->uniform("cam_light.power", l.power); + } shader_sphere.uniform("proj_view_mat", proj_view_mat); shader_triangles.uniform("proj_view_mat", proj_view_mat); From 798dc17d3ac2ec115152b1c3ac0d253105a703a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 15:38:42 +0200 Subject: [PATCH 0867/1018] viewer: add imgui light inputs --- src/gproshan/viewer/viewer.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b471b95d..cc86fe65 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -241,14 +241,32 @@ void viewer::imgui() { ImGui::Indent(); + light & ambient = render_params.ambient; + bool & update = render_params.restart; + + update |= ImGui::ColorEdit3("ambient.color", (float *) &ambient.color); + update |= ImGui::SliderFloat("ambient.power", &ambient.power, 0, 1); + + ImGui::Separator(); + for(int i = 0; i < render_params.n_lights; ++i) { - snprintf(slight, sizeof(slight), "light %d", i); - ImGui::SliderScalarN(slight, ImGuiDataType_Real, &render_params.lights[i], 3, &pos_min, &pos_max); + light & l = render_params.lights[i]; + + snprintf(slight, sizeof(slight), "light_%d.pos", i); + update |= ImGui::SliderScalarN(slight, ImGuiDataType_Real, &l.pos, 3, &pos_min, &pos_max); + + snprintf(slight, sizeof(slight), "light_%d.color", i); + update |= ImGui::ColorEdit3(slight, (float *) &l.color); + + snprintf(slight, sizeof(slight), "light_%d.power", i); + update |= ImGui::SliderFloat(slight, &l.power, 0, 100); + + ImGui::Separator(); } if(ImGui::Button("add light")) - render_params.add_light({0, 0, 0}); + render_params.add_light({0}); if(render_params.n_lights > 1) { From d7b6bddf06537deafa5631f3ab02901314edf27b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 11 Oct 2023 17:31:14 +0200 Subject: [PATCH 0868/1018] viewer: update optix lights and shading --- include/gproshan/raytracing/utils.h | 6 ++++-- src/gproshan/raytracing/optix.cpp | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 81befc06..cad89df9 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -162,8 +162,10 @@ vec eval_li(const t_eval_hit & hit, const light & ambient, const light specular = powf(std::max(dot(h, n), 0.f), hit.Ns); #endif // __CUDACC__ - const vec & color = hit.Ka * ambient.color * ambient.power + (lambertian * hit.Kd + specular * hit.Ks) * L.color * L.power / (r * r); - li += (occluded(hit.position, l, r) ? 0.4f : 1.0f) * color; + const vec & color = hit.Ka * ambient.color * ambient.power + + (lambertian * hit.Kd + specular * hit.Ks) * L.color * L.power / (r * r); + + li += (dot(v, n) < 0 || occluded(hit.position, l, r) ? 0.4f : 1.0f) * color; } return li / n_lights; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 75a678d9..acdbf7ec 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -142,10 +142,11 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) optix_params.viewport_y = params.viewport_y; optix_params.flat = flat; + optix_params.cam_pos = params.cam_pos; + optix_params.inv_proj_view = params.inv_proj_view; + optix_params.ambient = params.ambient; optix_params.n_lights = params.n_lights; memcpy(optix_params.lights, params.lights, sizeof(optix_params.lights)); - memcpy(&optix_params.cam_pos, ¶ms.cam_pos, sizeof(optix_params.cam_pos)); - memcpy(&optix_params.inv_proj_view, ¶ms.inv_proj_view, sizeof(optix_params.inv_proj_view)); cudaMemcpy(optix_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); From a991de73935999b0b4b580a04235a505cb4e604f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 12 Oct 2023 17:05:51 +0200 Subject: [PATCH 0869/1018] fix gproshanConfig.cmake.in --- gproshanConfig.cmake.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 60f16a53..17dbafb5 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -15,9 +15,10 @@ find_dependency(GLEW REQUIRED) find_dependency(glfw3 REQUIRED) find_dependency(X11 REQUIRED) find_dependency(Armadillo REQUIRED) -find_dependency(CGAL REQUIRED) find_dependency(Eigen3 REQUIRED) +find_dependency(CGAL REQUIRED) find_dependency(SuiteSparse REQUIRED) +find_dependency(flann REQUIRED) find_dependency(Boost COMPONENTS thread system) From 47c30551c6638f7f81b6e53a1174bf906a26cff1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 17 Oct 2023 15:56:13 +0200 Subject: [PATCH 0870/1018] g++-13 CImg warnings --- src/gproshan/mdict/image_denoising.cpp | 3 +++ src/gproshan/mesh/che_img.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/scenes/scanner.cpp | 6 +++++- src/gproshan/viewer/viewer.cpp | 6 +++++- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/gproshan/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp index 63d3078a..0db3be1b 100644 --- a/src/gproshan/mdict/image_denoising.cpp +++ b/src/gproshan/mdict/image_denoising.cpp @@ -1,6 +1,9 @@ #include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-truncation" #include +#pragma GCC diagnostic pop using namespace cimg_library; diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 44cbee28..40307fd1 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -52,7 +52,7 @@ void che_img::read_file(const std::string & file) ++v; } -// std::thread([](CImg img) { img.display(); }, img).detach(); +// std::thread([](const CImg & img) { img.display(); }, img).detach(); } diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 851e8d55..354fa15f 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -73,7 +73,7 @@ void che_ptx::read_file(const std::string & file) img.permute_axes("zycx"); img.save((file + ".jpg").c_str()); - std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); + std::thread([](const CImg & img) { img.mirror("y").display(); }, img).detach(); #endif // NDEBUG } else diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 80a9cb7b..2a9f855b 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -2,7 +2,11 @@ #include #include + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-truncation" #include +#pragma GCC diagnostic pop using namespace cimg_library; @@ -49,7 +53,7 @@ che * scanner_ptx(const che * mesh, rt::raytracing * rt, const size_t & n_rows, std::string img_filename = file_jpg + mesh->name() + ".jpg"; img.save(img_filename.c_str()); - std::thread([](CImg img) { img.display(); }, img).detach(); + std::thread([](const CImg & img) { img.display(); }, img).detach(); return mesh_ptx; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cc86fe65..b66dc54d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -22,7 +22,11 @@ #include #endif // GPROSHAN_OPTIX + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-truncation" #include +#pragma GCC diagnostic pop using namespace cimg_library; @@ -1025,7 +1029,7 @@ bool viewer::m_raycasting(viewer * view) view->cam.eye ); - std::thread([](CImg img) + std::thread([](const CImg & img) { img.display(); }, From d6068dc515e789e6af72cabf1eaa83a0af4babe7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 18 Oct 2023 15:33:16 +0200 Subject: [PATCH 0871/1018] fix debug CImg error --- src/gproshan/app_viewer.cpp | 4 +++- src/gproshan/mesh/che_ptx.cpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 9a0189b3..a68b0be8 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -991,7 +991,9 @@ bool app_viewer::process_fill_holes(viewer * p_view) border.assign(true, vertices.size()); // real_t angle; - index_t v0, v1, v2; + index_t v0 = NIL; + index_t v1 = NIL; + index_t v2 = NIL; while(!front.empty()) { // angle = front.top().first; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 354fa15f..45d64abd 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -73,7 +73,7 @@ void che_ptx::read_file(const std::string & file) img.permute_axes("zycx"); img.save((file + ".jpg").c_str()); - std::thread([](const CImg & img) { img.mirror("y").display(); }, img).detach(); + std::thread([](CImg img) { img.mirror("y").display(); }, img).detach(); #endif // NDEBUG } else From 1cbfbbbe8ea5365b31d45d3ae435519a25e4d972 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 24 Oct 2023 18:24:56 +0200 Subject: [PATCH 0872/1018] fix clang warnings: AppleClang 15 --- src/gproshan/app_viewer.cpp | 4 ++-- src/gproshan/mdict/image_denoising.cpp | 6 +++++- src/gproshan/mdict/msparse_coding.cpp | 4 ++-- src/gproshan/scenes/scanner.cpp | 6 +++++- src/gproshan/viewer/frame.cpp | 30 +++++++++++++------------- src/gproshan/viewer/viewer.cpp | 6 +++++- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index a68b0be8..8fc831bd 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -693,11 +693,11 @@ bool app_viewer::process_mdict_patch(viewer * p_view) } avg_nvp /= mesh.selected.size(); - gproshan_debug_var(avg_nvp); + gproshan_log_var(avg_nvp); delete [] toplevel; TOC(view->time) - gproshan_debug_var(view->time); + gproshan_log_var(view->time); return false; } diff --git a/src/gproshan/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp index 0db3be1b..3df9215f 100644 --- a/src/gproshan/mdict/image_denoising.cpp +++ b/src/gproshan/mdict/image_denoising.cpp @@ -1,9 +1,13 @@ #include +#ifndef __clang__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" -#include + #include #pragma GCC diagnostic pop +#else + #include +#endif // __clang__ using namespace cimg_library; diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 1dbb7ccf..8ce2cb25 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -254,8 +254,8 @@ void msparse_coding::load_sampling() S.save(tmp_file_path(key_name + ".rsampl")); gproshan_debug_var(m_params.sum_thres); - gproshan_debug_var(count); - gproshan_debug_var(count_cov); + gproshan_log_var(count); + gproshan_log_var(count_cov); gproshan_debug_var(seeds.size()); gproshan_debug_var(m_params.n_patches); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 2a9f855b..38426b44 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -3,10 +3,14 @@ #include #include +#ifndef __clang__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" -#include + #include #pragma GCC diagnostic pop +#else + #include +#endif // __clang__ using namespace cimg_library; diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index dc6b75cd..f4eb1a7a 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -66,33 +66,33 @@ frame::operator const GLuint & () const vec4 * frame::map_pbo(bool cuda) { -#ifdef GPROSHAN_CUDA - if(cuda) + if(!cuda) { - vec4 * img = nullptr; + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); + return (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); + } + + vec4 * img = nullptr; + #ifdef GPROSHAN_CUDA size_t num_bytes = 0; cudaGraphicsMapResources(1, &pbo_cuda, 0); cudaGraphicsResourceGetMappedPointer((void **) &img, &num_bytes, pbo_cuda); - return img; - } -#endif // GPROSHAN_CUDA - - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); - return (vec4 *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE); + #endif // GPROSHAN_CUDA + return img; } void frame::unmap_pbo(bool cuda) { -#ifdef GPROSHAN_CUDA - if(cuda) + if(!cuda) { - cudaGraphicsUnmapResources(1, &pbo_cuda, 0); + glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); return; } -#endif // GPROSHAN_CUDA - glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); - glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); + #ifdef GPROSHAN_CUDA + cudaGraphicsUnmapResources(1, &pbo_cuda, 0); + #endif // GPROSHAN_CUDA } bool frame::resize(const size_t & w, const size_t & h) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b66dc54d..04a27523 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -23,10 +23,14 @@ #endif // GPROSHAN_OPTIX +#ifndef __clang__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" -#include + #include #pragma GCC diagnostic pop +#else + #include +#endif // __clang__ using namespace cimg_library; From 9250e10f994d384cce4a5159911468147f34c773 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 25 Oct 2023 17:19:13 +0200 Subject: [PATCH 0873/1018] mesh: move load mesh to che static method --- include/gproshan/app_viewer.h | 2 +- include/gproshan/mesh/che.h | 1 + src/gproshan/app_viewer.cpp | 29 +------------------------- src/gproshan/mesh/che.cpp | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 575c7c88..8d09f6cf 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -4,8 +4,8 @@ #include #include -#include #include +#include #include #include #include diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index e2b6d123..5d714c4e 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -168,6 +168,7 @@ class che public: static std::vector trig_convex_polygon(const index_t * P, const size_t & n); + static che * load_mesh(const std::string & file_path); friend struct CHE; }; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 8fc831bd..d2d1c7e3 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -20,33 +20,6 @@ app_viewer::~app_viewer() delete *m; } -che * app_viewer::load_mesh(const std::string & file_path) -{ - size_t pos = file_path.rfind('.'); - assert(pos != std::string::npos); - - std::string extension = file_path.substr(pos + 1); - - if(extension == "obj") - { - scene * sc = new scene(file_path); - if(!sc->is_scene()) - { - delete sc; - return new che_obj(file_path); - } - return sc; - } - if(extension == "off") return new che_off(file_path); - if(extension == "ply") return new che_ply(file_path); - if(extension == "ptx") return new che_ptx(file_path); - if(extension == "xyz") return new che_xyz(file_path); - if(extension == "pts") return new che_pts(file_path); - if(extension == "pcd") return new che_pcd(file_path); - - return new che_img(file_path); -} - int app_viewer::main(int nargs, const char ** args) { if(nargs < 2) @@ -57,7 +30,7 @@ int app_viewer::main(int nargs, const char ** args) TIC(time) for(int i = 1; i < nargs; ++i) - add_mesh(load_mesh(args[i])); + add_mesh(che::load_mesh(args[i])); TOC(time) update_status_message("meshes loaded in %.3fs", time); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 474c33e6..9e004db6 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1,5 +1,16 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + #include #include #include @@ -1117,6 +1128,33 @@ std::vector che::trig_convex_polygon(const index_t * P, const size_t & return trigs; } +che * che::load_mesh(const std::string & file_path) +{ + size_t pos = file_path.rfind('.'); + assert(pos != std::string::npos); + + std::string extension = file_path.substr(pos + 1); + + if(extension == "obj") + { + scene * sc = new scene(file_path); + if(!sc->is_scene()) + { + delete sc; + return new che_obj(file_path); + } + return sc; + } + if(extension == "off") return new che_off(file_path); + if(extension == "ply") return new che_ply(file_path); + if(extension == "ptx") return new che_ptx(file_path); + if(extension == "xyz") return new che_xyz(file_path); + if(extension == "pts") return new che_pts(file_path); + if(extension == "pcd") return new che_pcd(file_path); + + return new che_img(file_path); +} + // iterator classes methods From 3e63deb7d41f1a5b42dee2a1baa59343aed9b432 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 26 Oct 2023 16:19:00 +0200 Subject: [PATCH 0874/1018] util: adding partitions iterator --- include/gproshan/util.h | 37 +++++++++++++++++++++++++++++++++++++ src/gproshan/util.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 446f8a2b..f649d054 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -3,11 +3,48 @@ #include +#include +#include + // geometry processing and shape analysis framework namespace gproshan { +class partitions +{ + struct part; + + std::vector splits; + index_t * sorted = nullptr; + + public: + part operator () (const index_t & i) const; +}; + +struct partitions::part +{ + struct iterator; + + index_t _begin = 0; + index_t _end = 0; + const index_t * sorted = nullptr; + + iterator begin() const; + iterator end() const; +}; + +struct partitions::part::iterator +{ + index_t i = 0; + const index_t * sorted = nullptr; + + iterator & operator ++ (); + bool operator != (const iterator & it) const; + const index_t & operator * (); +}; + + void copy_real_t_array(float * destination, const float * source, const size_t & n_elem); void copy_real_t_array(float * destination, const double * source, const size_t & n_elem); diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 18d30b16..aae2cbab 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -7,6 +7,39 @@ namespace gproshan { +partitions::part partitions::operator () (const index_t & i) const +{ + assert(i > 0 && i < splits.size()); + return {splits[i - 1], splits[i], sorted}; +} + +partitions::part::iterator partitions::part::begin() const +{ + return {_begin, sorted}; +} + +partitions::part::iterator partitions::part::end() const +{ + return {_end, sorted}; +} + +partitions::part::iterator & partitions::part::iterator::operator ++ () +{ + ++i; + return *this; +} + +bool partitions::part::iterator::operator != (const iterator & it) const +{ + return i != it.i; +} + +const index_t & partitions::part::iterator::operator * () +{ + return sorted ? sorted[i] : i; +} + + void copy_real_t_array(float * destination, const float * source, const size_t & n_elem) { memcpy(destination, source, n_elem * sizeof(float)); From 3f2d38119663b512b94e9390281ee86b589c7fab Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 26 Oct 2023 17:19:46 +0200 Subject: [PATCH 0875/1018] util: partitions iterator cuda --- include/gproshan/geometry/vec.h | 6 ----- include/gproshan/include.h | 6 +++++ include/gproshan/util.h | 46 +++++++++++++++++++++++++-------- src/gproshan/util.cpp | 26 ------------------- 4 files changed, 41 insertions(+), 43 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 1e8d227f..9083c3ae 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -8,12 +8,6 @@ #include #include -#ifdef __CUDACC__ - #define __host_device__ __host__ __device__ -#else - #define __host_device__ -#endif // __CUDACC__ - // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 7c0837f5..3b5cfabc 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -12,6 +12,12 @@ #define NIL (0u - 1) +#ifdef __CUDACC__ + #define __host_device__ __host__ __device__ +#else + #define __host_device__ +#endif // __CUDACC__ + // geometry processing and shape analysis framework namespace gproshan { diff --git a/include/gproshan/util.h b/include/gproshan/util.h index f649d054..9b088a50 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -24,24 +24,48 @@ class partitions struct partitions::part { - struct iterator; + struct iterator + { + index_t i = 0; + const index_t * sorted = nullptr; + + __host_device__ + iterator & operator ++ () + { + ++i; + return *this; + } + + __host_device__ + bool operator != (const iterator & it) const + { + return i != it.i; + } + + __host_device__ + const index_t & operator * () + { + return sorted ? sorted[i] : i; + } + }; + index_t _begin = 0; index_t _end = 0; const index_t * sorted = nullptr; - iterator begin() const; - iterator end() const; -}; -struct partitions::part::iterator -{ - index_t i = 0; - const index_t * sorted = nullptr; + __host_device__ + iterator begin() const + { + return {_begin, sorted}; + } - iterator & operator ++ (); - bool operator != (const iterator & it) const; - const index_t & operator * (); + __host_device__ + iterator end() const + { + return {_end, sorted}; + } }; diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index aae2cbab..b81b31a7 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -13,32 +13,6 @@ partitions::part partitions::operator () (const index_t & i) const return {splits[i - 1], splits[i], sorted}; } -partitions::part::iterator partitions::part::begin() const -{ - return {_begin, sorted}; -} - -partitions::part::iterator partitions::part::end() const -{ - return {_end, sorted}; -} - -partitions::part::iterator & partitions::part::iterator::operator ++ () -{ - ++i; - return *this; -} - -bool partitions::part::iterator::operator != (const iterator & it) const -{ - return i != it.i; -} - -const index_t & partitions::part::iterator::operator * () -{ - return sorted ? sorted[i] : i; -} - void copy_real_t_array(float * destination, const float * source, const size_t & n_elem) { From 176703912d8e37d79d1311310aff4e1bd8404a16 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 1 Nov 2023 15:29:25 +0100 Subject: [PATCH 0876/1018] update cmake and install dependencies --- CMakeLists.txt | 2 +- gproshanConfig.cmake.in | 29 ++++++++++++----------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ba2b09b..7bc0f6fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.27) project(gproshan VERSION 3.14) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 17dbafb5..ae2a9f6b 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -2,24 +2,19 @@ include(CMakeFindDependencyMacro) -find_dependency(CUDAToolkit 12) -if(CUDAToolkit_FOUND) - add_definitions(-DGPROSHAN_CUDA) -endif(CUDAToolkit_FOUND) +find_package(CUDAToolkit 12) -find_dependency(embree 4 REQUIRED) -find_dependency(Threads REQUIRED) -find_dependency(OpenMP REQUIRED) -find_dependency(OpenGL REQUIRED) -find_dependency(GLEW REQUIRED) -find_dependency(glfw3 REQUIRED) -find_dependency(X11 REQUIRED) -find_dependency(Armadillo REQUIRED) -find_dependency(Eigen3 REQUIRED) -find_dependency(CGAL REQUIRED) -find_dependency(SuiteSparse REQUIRED) -find_dependency(flann REQUIRED) -find_dependency(Boost COMPONENTS thread system) +find_dependency(embree 4) +find_dependency(OpenMP) +find_dependency(OpenGL) +find_dependency(GLEW) +find_dependency(glfw3) +find_dependency(X11) +find_dependency(Armadillo) +find_dependency(Eigen3) +find_dependency(CGAL) +find_dependency(SuiteSparse) +find_dependency(flann) include(${CMAKE_CURRENT_LIST_DIR}/gproshanTargets.cmake) From f426d870927837cdcfb605345ac64dc6d8222306 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Nov 2023 14:46:50 +0100 Subject: [PATCH 0877/1018] che: ply ascii reader fix normal --- src/gproshan/mesh/che_ply.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 369fe27d..1e8cf72b 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -111,9 +111,21 @@ void che_ply::read_file(const std::string & file) for(index_t v = 0; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); - if(sscanf(line, "%f %f %f %hhu %hhu %hhu", &x, &y, &z, &r, &g, &b) > 5) - VC[v] = {r, g, b}; + + sscanf(line, "%f %f %f", &x, &y, &z); GT[v] = {x, y, z}; + + if(rgb) + { + sscanf(line, "%hhu %hhu %hhu", &r, &g, &b); + VC[v] = {r, g, b}; + } + + if(normal) + { + sscanf(line, "%f %f %f", &x, &y, &z); + VN[v] = {x, y, z}; + } } while(nf--) From 2eca24f44d0514eda666ec631ba6ae9ab17e4c89 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Nov 2023 16:02:19 +0100 Subject: [PATCH 0878/1018] che: update ply reader ascii --- src/gproshan/mesh/che_ply.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 1e8cf72b..9050d032 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -106,26 +106,18 @@ void che_ply::read_file(const std::string & file) if(format[0] == 'a') // ascii { - float x, y, z; + float x, y, z, nx, ny, nz; unsigned char r, g, b; for(index_t v = 0; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); + + rgb ? sscanf(line, "%f %f %f %hhu %hhu %hhu %f %f %f", &x, &y, &z, &r, &g, &b, &nx, &ny, &nz) + : sscanf(line, "%f %f %f %f %f %f", &x, &y, &z, &nx, &ny, &nz); - sscanf(line, "%f %f %f", &x, &y, &z); GT[v] = {x, y, z}; - - if(rgb) - { - sscanf(line, "%hhu %hhu %hhu", &r, &g, &b); - VC[v] = {r, g, b}; - } - - if(normal) - { - sscanf(line, "%f %f %f", &x, &y, &z); - VN[v] = {x, y, z}; - } + if(rgb) VC[v] = {r, g, b}; + if(normal) VN[v] = {nx, ny, nz}; } while(nf--) From adea8a332212692a3a6efa02a4fa7d7d3a90ee7e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Nov 2023 16:37:07 +0100 Subject: [PATCH 0879/1018] scenes: update scanner and rt const methods --- apps/test_scanner.cpp | 6 +++++- include/gproshan/raytracing/embree.h | 12 ++++++------ include/gproshan/raytracing/raytracing.h | 10 +++++----- include/gproshan/scenes/scanner.h | 2 +- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 2 +- src/gproshan/raytracing/embree.cpp | 12 ++++++------ src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/scenes/scanner.cpp | 6 +++--- 9 files changed, 29 insertions(+), 25 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index ebd1ac66..c4aec9c5 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -32,11 +32,15 @@ int main(int argc, char* argv[]) gproshan::rt::raytracing * rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}, false, pc_radius); - gproshan::che * ptx_mesh = scanner_ptx(mesh_ply, rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder); + gproshan::che * ptx_mesh = gproshan::scanner_ptx_jpg(rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder + mesh_ply->name()); std::string ptx_filename = ptx_folder + mesh_ply->name(); gproshan::che_ptx::write_file(ptx_mesh, ptx_filename, n_rows, n_cols); + delete mesh_ply; + delete rt_embree; + delete ptx_mesh; + return 0; } diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 44025abe..c1fe92c6 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -48,8 +48,8 @@ class embree : public raytracing ); virtual ~embree(); - virtual index_t closest_vertex(const vertex & org, const vertex & dir); - virtual eval_hit intersect(const vertex & org, const vertex & dir); + virtual index_t closest_vertex(const vertex & org, const vertex & dir) const; + virtual eval_hit intersect(const vertex & org, const vertex & dir) const; protected: @@ -59,12 +59,12 @@ class embree : public raytracing virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat); + virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) const; - float intersect_depth(const vertex & org, const vertex & dir); + float intersect_depth(const vertex & org, const vertex & dir) const; - bool intersect(ray_hit & r); - bool occluded(ray_hit & r); + bool intersect(ray_hit & r) const; + bool occluded(ray_hit & r) const; }; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 79076431..af32a077 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -27,15 +27,15 @@ class raytracing const mat4 & inv_proj_view, const vertex & cam_pos, const index_t & samples = 4 - ); + ) const; virtual eval_hit intersect( const vertex &, // org const vertex & //dir - ) { return {}; } + ) const { return {}; } virtual index_t closest_vertex( const vertex &, // org, const vertex & // dir - ) { return NIL; }; + ) const { return NIL; } protected: virtual vec3 closesthit_radiance( const vertex &, // org @@ -45,11 +45,11 @@ class raytracing const int &, // n_lights const vertex &, // cam_pos const bool & // flat - ) { return {}; }; + ) const { return {}; }; virtual float intersect_depth( const vertex &, // org, const vertex & // dir - ) { return 0; }; + ) const { return 0; }; }; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 16eb8874..3d809a8e 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -11,7 +11,7 @@ namespace gproshan { che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos); -che * scanner_ptx(const che * mesh, rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); } // namespace gproshan diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index d2d1c7e3..7e823be4 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -190,7 +190,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) if(ImGui::Button("Scan")) { - che * ptx_mesh = scanner_ptx(mesh, mesh.rt_embree, n_rows, n_cols, cam_pos); + che * ptx_mesh = scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename); che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); view->add_mesh(ptx_mesh); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 9050d032..01a7ef4e 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -111,7 +111,7 @@ void che_ply::read_file(const std::string & file) for(index_t v = 0; v < n_vertices; ++v) { fgets(line, sizeof(line), fp); - + rgb ? sscanf(line, "%f %f %f %hhu %hhu %hhu %f %f %f", &x, &y, &z, &r, &g, &b, &nx, &ny, &nz) : sscanf(line, "%f %f %f %f %f %f", &x, &y, &z, &nx, &ny, &nz); diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 7ad3bd4c..7f35e9e7 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -82,7 +82,7 @@ embree::~embree() rtcReleaseDevice(rtc_device); } -index_t embree::closest_vertex(const vertex & org, const vertex & dir) +index_t embree::closest_vertex(const vertex & org, const vertex & dir) const { ray_hit r(org, dir); if(!intersect(r)) return NIL; @@ -109,7 +109,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) return mesh->VT[he]; } -eval_hit embree::intersect(const vertex & org, const vertex & dir) +eval_hit embree::intersect(const vertex & org, const vertex & dir) const { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -246,7 +246,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) +vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) const { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -267,19 +267,19 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const l }); } -float embree::intersect_depth(const vertex & org, const vertex & dir) +float embree::intersect_depth(const vertex & org, const vertex & dir) const { ray_hit r(org, dir); return intersect(r) ? r.ray.tfar : 0.f; } -bool embree::intersect(ray_hit & r) +bool embree::intersect(ray_hit & r) const { rtcIntersect1(rtc_scene, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; } -bool embree::occluded(ray_hit & r) +bool embree::occluded(ray_hit & r) const { rtcIntersect1(rtc_scene, &r); return r.hit.geomID != RTC_INVALID_GEOMETRY_ID; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 80b90931..3e3ea393 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -44,7 +44,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f float * raytracing::raycaster( const ivec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos, - const index_t & samples ) + const index_t & samples ) const { float * frame = new float[windows_size.x() * windows_size.y()]; diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 38426b44..8057a52e 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan { -che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) +che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) { che * mesh_ptx = new che(n_cols * n_rows); @@ -47,14 +47,14 @@ che * scanner_ptx(rt::raytracing * rt, const size_t & n_rows, const size_t & n_c return mesh_ptx; } -che * scanner_ptx(const che * mesh, rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) { che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); CImg img((unsigned char *) &mesh_ptx->rgb(0), 3, n_cols, n_rows); img.permute_axes("zycx"); - std::string img_filename = file_jpg + mesh->name() + ".jpg"; + std::string img_filename = file_jpg + ".jpg"; img.save(img_filename.c_str()); std::thread([](const CImg & img) { img.display(); }, img).detach(); From b58d315d22ba853f78928757ddda4f2811b56e11 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 6 Nov 2023 17:58:58 +0100 Subject: [PATCH 0880/1018] che: update merge method no updating normals --- include/gproshan/mesh/che.h | 2 +- src/gproshan/mesh/che.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 5d714c4e..4b8e3b78 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -96,7 +96,7 @@ class che void reload(); mat4 normalize_sphere(const real_t & r = 1) const; mat4 normalize_box(const real_t & side = 2) const; - che * merge(const che * mesh, const std::vector & com_vertices); + che * merge(const che * mesh, const std::vector & com_vertices = {}); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); void update_heatmap(const real_t * hm = nullptr); void update_normals(); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 9e004db6..1d206688 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -293,6 +293,8 @@ che * che::merge(const che * mesh, const std::vector & vcommon) memcpy(new_mesh->GT, GT, sizeof(vertex) * n_vertices); memcpy(new_mesh->GT + n_vertices, mesh->GT + n_vcommon, sizeof(vertex) * n_vnew); + memcpy(new_mesh->VN, VN, sizeof(vertex) * n_vertices); + memcpy(new_mesh->VN + n_vertices, mesh->VN + n_vcommon, sizeof(vertex) * n_vnew); memcpy(new_mesh->VC, VC, sizeof(rgb_t) * n_vertices); memcpy(new_mesh->VC + n_vertices, mesh->VC + n_vcommon, sizeof(rgb_t) * n_vnew); memcpy(new_mesh->VHC, VHC, sizeof(real_t) * n_vertices); @@ -307,8 +309,6 @@ che * che::merge(const che * mesh, const std::vector & vcommon) new_mesh->update_evt_ot_et(); new_mesh->update_eht(); - new_mesh->update_normals(); - return new_mesh; } From 82fc290d47529930a3574ccd482cddf34f5c030f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 8 Nov 2023 17:36:12 +0100 Subject: [PATCH 0881/1018] che: fix ambigous rgb_t constructor --- include/gproshan/mesh/che.h | 1 - src/gproshan/mesh/che.cpp | 10 ++++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 4b8e3b78..5c524d54 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -40,7 +40,6 @@ class che rgb_t() = default; rgb_t(const vertex & v); - rgb_t(const float & fr, const float & fg, const float & fb); rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb); unsigned char & operator [] (const index_t & i); operator vertex () const; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 1d206688..e6e9d325 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -27,13 +27,11 @@ size_t & che::rw(const size_t & n) } -che::rgb_t::rgb_t(const vertex & v): rgb_t(v.x(), v.y(), v.z()) {} - -che::rgb_t::rgb_t(const float & fr, const float & fg, const float & fb) +che::rgb_t::rgb_t(const vertex & v) { - r = (unsigned char) (fr * 255); - g = (unsigned char) (fg * 255); - b = (unsigned char) (fb * 255); + r = (unsigned char) (v.x() * 255); + g = (unsigned char) (v.y() * 255); + b = (unsigned char) (v.z() * 255); } che::rgb_t::rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb): r(cr), g(cg), b(cb) {} From 64a45a60f61e7fc08513d0c1f609c86d55083265 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Nov 2023 16:45:13 +0100 Subject: [PATCH 0882/1018] util: update partitions iterator/class --- include/gproshan/util.h | 3 +++ src/gproshan/util.cpp | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 9b088a50..64d31e0a 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -19,6 +19,9 @@ class partitions index_t * sorted = nullptr; public: + partitions(index_t * s = nullptr); + void add(const index_t & size); + size_t size(const index_t & i) const; part operator () (const index_t & i) const; }; diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index b81b31a7..2d736bc4 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -7,10 +7,25 @@ namespace gproshan { +partitions::partitions(index_t * s): sorted(s) +{ + splits.push_back(0); +} + +void partitions::add(const index_t & size) +{ + return splits.push_back(size + splits.back()); +} + +size_t partitions::size(const index_t & i) const +{ + return splits[i + 1] - splits[i]; +} + partitions::part partitions::operator () (const index_t & i) const { assert(i > 0 && i < splits.size()); - return {splits[i - 1], splits[i], sorted}; + return {splits[i], splits[i + 1], sorted}; } From d36b95c4413ef44c575f65ce57715b39a8b6836b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Nov 2023 16:52:30 +0100 Subject: [PATCH 0883/1018] vec: update division methods --- include/gproshan/geometry/vec.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 9083c3ae..922f742c 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -144,13 +144,13 @@ class vec return res; } - ///< scalar division + ///< element wise division & scalar division __host_device__ - vec operator / (const T & a) const + vec operator / (const vec & v) const { vec res; for(index_t i = 0; i < N; ++i) - res[i] = values[i] / a; + res[i] = values[i] / v[i]; return res; } @@ -193,12 +193,12 @@ class vec return *this; } - ///< scalar division self assign + ///< element wise division self assign __host_device__ - const vec & operator /= (const T & a) + const vec & operator /= (const vec & v) { - for(T & v: values) - v /= a; + for(index_t i = 0; i < N; ++i) + values[i] /= v[i]; return *this; } @@ -263,6 +263,16 @@ vec operator * (const T & a, const vec & v) return v * a; } +///< scalar product +template +__host_device__ +vec operator / (const T & a, const vec & v) +{ + vec res; + for(index_t i = 0; i < N; ++i) + res[i] = a / v[i]; + return res; +} ///< cross product template From e3e325b4b1d44f9810807853a02521d6211d033e Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 9 Nov 2023 17:06:16 +0100 Subject: [PATCH 0884/1018] rt: update near ray parameter --- include/gproshan/raytracing/embree.h | 2 +- src/gproshan/raytracing/optix.cu | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index c1fe92c6..fa6c8f86 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -21,7 +21,7 @@ class embree : public raytracing { ray_hit(const vertex & p_org = {0, 0, 0}, const vertex & v_dir = {0, 0, 0}, - float near = 0, + float near = 1e-5f, float far = 1e20f); vertex org() const; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index b42dabec..de3c95e4 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -121,7 +121,7 @@ extern "C" __global__ void __raygen__render_frame() optixTrace( optix_params.traversable, * (float3 *) &optix_params.cam_pos, * (float3 *) &ray_dir, - 0.f, // tmin + 1e-5f, // tmin 1e20f, // tmax 0.0f, // rayTime OptixVisibilityMask(255), From 1a4cc0c4146f6d229f631abfca58ce032dfe4d01 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 10 Nov 2023 14:00:48 +0100 Subject: [PATCH 0885/1018] che_ply: saving normals --- src/gproshan/mesh/che_ply.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 01a7ef4e..29afb253 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -266,6 +266,9 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "property %s x\n", type); fprintf(fp, "property %s y\n", type); fprintf(fp, "property %s z\n", type); + fprintf(fp, "property %s nx\n", type); + fprintf(fp, "property %s ny\n", type); + fprintf(fp, "property %s nz\n", type); if(color) { fprintf(fp, "property uchar red\n"); @@ -276,15 +279,12 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "property list uchar uint vertex_index\n"); fprintf(fp, "end_header\n"); - if(color) + for(index_t v = 0; v < mesh->n_vertices; ++v) { - for(index_t v = 0; v < mesh->n_vertices; ++v) - { - fwrite(&mesh->point(v), sizeof(vertex), 1, fp); - fwrite(&mesh->rgb(v), sizeof(rgb_t), 1, fp); - } + fwrite(&mesh->point(v), sizeof(vertex), 1, fp); + fwrite(&mesh->normal(v), sizeof(vertex), 1, fp); + if(color) fwrite(&mesh->rgb(v), sizeof(rgb_t), 1, fp); } - else fwrite(&mesh->point(0), sizeof(vertex), mesh->n_vertices, fp); unsigned char mtrig = che::mtrig; for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) From b9844d86da0bbab5529aeb3def128a59e31b9847 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 10 Nov 2023 16:04:13 +0100 Subject: [PATCH 0886/1018] rt: fix ray intersection pc --- include/gproshan/raytracing/utils.h | 16 +++++++++------- src/gproshan/raytracing/embree.cpp | 4 +--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index cad89df9..93514412 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -78,13 +78,6 @@ struct t_eval_hit __host_device__ t_eval_hit() {} - __host_device__ - t_eval_hit(const CHE & pc, const index_t & aprimID) - { - primID = aprimID; - normal = pc.VN[primID]; - } - __host_device__ t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const scene_data & sc) { @@ -92,6 +85,15 @@ struct t_eval_hit u = au; v = av; + if(!mesh.n_trigs) // pointcloud + { + const che::rgb_t & c = mesh.VC[aprimID]; + Kd = {T(c.r), T(c.g), T(c.b)}; + Kd /= 255; + normal = mesh.VN[aprimID]; + return; + } + const index_t he = primID * che::mtrig; const index_t a = mesh.VT[he]; diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 7f35e9e7..f0b25a7a 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -253,9 +253,7 @@ vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const l const CHE * mesh = g_meshes[r.hit.geomID]; - eval_hit hit; - if(mesh->n_trigs) - hit = {*mesh, r.hit.primID, r.hit.u, r.hit.v, sc}; + eval_hit hit(*mesh, r.hit.primID, r.hit.u, r.hit.v, sc); hit.position = r.pos(); hit.normal = flat ? r.normal() : hit.normal; From 1d31eaee3808ccc8938eb396129668e2f39cc612 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 11 Nov 2023 15:00:35 +0100 Subject: [PATCH 0887/1018] scenes: normalize scanner dist --- src/gproshan/scenes/scanner.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 8057a52e..5da52172 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -40,10 +40,20 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t mesh_ptx->point(v) = h.position; mesh_ptx->normal(v) = h.normal; - mesh_ptx->heatmap(v) = h.dist / M_SQRT2; + mesh_ptx->heatmap(v) = h.dist; mesh_ptx->rgb(v) = h.Kd; } + real_t max_dist = 0; + + #pragma omp parallel for reduction(std::max: max_dist) + for(index_t v = 0; v < mesh_ptx->n_vertices; ++v) + max_dist = std::max(max_dist, mesh_ptx->heatmap(v)); + + #pragma omp parallel for + for(index_t v = 0; v < mesh_ptx->n_vertices; ++v) + mesh_ptx->heatmap(v) /= max_dist; + return mesh_ptx; } From eb04e79f4b2941d697f91af292f910b590e63d50 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 16 Nov 2023 17:54:35 +0100 Subject: [PATCH 0888/1018] viewer: maximize window --- include/gproshan/viewer/viewer.h | 1 + src/gproshan/viewer/viewer.cpp | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index fd7a9200..3e3acc4e 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -137,6 +137,7 @@ class viewer static bool m_help(viewer * view); static bool m_close(viewer * view); + static bool m_maximize(viewer * view); static bool m_hide_show_imgui(viewer * view); static bool m_save_load_view(viewer * view); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 04a27523..6dde08da 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -398,6 +398,7 @@ void viewer::init_menus() sub_menus.push_back("Viewer"); add_process(GLFW_KEY_F1, "F1", "Help", m_help); add_process(GLFW_KEY_ESCAPE, "ESCAPE", "Close", m_close); + add_process(GLFW_KEY_F11, "F11", "Maximize", m_maximize); add_process(GLFW_KEY_I, "F1", "Hide/Show ImGui", m_hide_show_imgui); add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", m_save_load_view); add_process(GLFW_KEY_UP, "UP", "Zoom in", m_zoom_in); @@ -650,6 +651,12 @@ bool viewer::m_close(viewer * view) return false; } +bool viewer::m_maximize(viewer * view) +{ + glfwMaximizeWindow(view->window); + return false; +} + bool viewer::m_hide_show_imgui(viewer * view) { view->hide_imgui = !view->hide_imgui; From 4a98cd954f270e8dc96c47f3ed31848e015593e7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 17 Nov 2023 00:16:16 +0100 Subject: [PATCH 0889/1018] viewer: maximize with set windows size --- src/gproshan/viewer/viewer.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 6dde08da..ae29c7a1 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -653,7 +653,9 @@ bool viewer::m_close(viewer * view) bool viewer::m_maximize(viewer * view) { - glfwMaximizeWindow(view->window); + GLFWmonitor * monitor = glfwGetPrimaryMonitor(); + const GLFWvidmode * mode = glfwGetVideoMode(monitor); + glfwSetWindowSize(view->window, mode->width, mode->height); return false; } From 21e315444386e3f138a65ab2de549feaeff25718 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 19 Nov 2023 18:33:43 +0100 Subject: [PATCH 0890/1018] viewer: pop mesh --- include/gproshan/viewer/viewer.h | 9 +++++-- src/gproshan/viewer/viewer.cpp | 43 ++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 3e3acc4e..e2db886e 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -31,7 +31,6 @@ // geometry processing and shape analysis framework namespace gproshan { -const size_t max_nframes = 1000; class viewer { @@ -52,10 +51,13 @@ class viewer }; static const std::vector m_window_split; - static const size_t max_meshes; static const std::vector colormap; + static const size_t max_meshes; + static const size_t max_nframes = 1000; + static che_sphere sphere_data; + bool apply_all_meshes = false; @@ -109,6 +111,8 @@ class viewer che_viewer & selected_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); bool add_mesh(che * p_mesh, const bool & reset_normals = true); + bool pop_mesh(); + void update_viewport_meshes(); void update_status_message(const char * format, ...); protected: @@ -143,6 +147,7 @@ class viewer static bool m_save_load_view(viewer * view); static bool m_reset_mesh(viewer * view); static bool m_save_mesh(viewer * view); + static bool m_pop_mesh(viewer * view); static bool m_normalize_mesh(viewer * view); static bool m_zoom_in(viewer * view); static bool m_zoom_out(viewer * view); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index ae29c7a1..7f0a9bf2 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -48,7 +48,6 @@ const std::vector viewer::m_window_split = { {1, 1}, {4, 5}, {4, 5}, {4, 5}, {4, 5} }; -const size_t viewer::max_meshes = m_window_split.size() - 1; const std::vector viewer::colormap = { "vertex color", "blue heatmap", @@ -58,6 +57,8 @@ const std::vector viewer::colormap = { "vertex color", "material scene", }; +const size_t viewer::max_meshes = m_window_split.size() - 1; + che_sphere viewer::sphere_data{0.01}; viewer::viewer(const int & width, const int & height) @@ -424,8 +425,9 @@ void viewer::init_menus() #endif // GPROSHAN_OPTIX sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Reload/Reset", m_reset_mesh); + add_process(GLFW_KEY_R, "R", "Reload/Reset", m_reset_mesh); add_process(GLFW_KEY_W, "W", "Save Mesh", m_save_mesh); + add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Pop Mesh", m_pop_mesh); add_process(0, "", "Normalize Mesh", m_normalize_mesh); add_process(GLFW_KEY_F2, "F2", "Invert Normals", m_invert_normals); add_process(GLFW_KEY_F3, "F3", "Gradient Field", m_render_gradients); @@ -465,7 +467,7 @@ void viewer::add_process(const int & key, const std::string & skey, const std::s processes[key] = {skey, name, f}; processes[key].sub_menu = sub_menus.size() - 1; } - else std::cerr << "Repeat key: " << key << std::endl; + else fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, skey.c_str(), name.c_str()); } bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) @@ -483,6 +485,31 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) idx_selected_mesh = meshes.size() - 1; glfwSetWindowTitle(window, mesh->filename.c_str()); + update_viewport_meshes(); + + save_history(tmp_file_path("history")); + + return true; +} + +bool viewer::pop_mesh() +{ + if(meshes.size() == 1) + return false; + + if(idx_selected_mesh == meshes.size() - 1) + --idx_selected_mesh; + + delete meshes.back(); + meshes.pop_back(); + + update_viewport_meshes(); + + return true; +} + +void viewer::update_viewport_meshes() +{ const int & rows = m_window_split[meshes.size()].x(); const int & cols = m_window_split[meshes.size()].y(); for(index_t m = 0; m < meshes.size(); ++m) @@ -496,10 +523,6 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) viewport_height /= rows; cam.aspect = real_t(viewport_width) / viewport_height; proj_mat = cam.perspective(); - - save_history(tmp_file_path("history")); - - return true; } void viewer::update_status_message(const char * format, ...) @@ -731,6 +754,12 @@ bool viewer::m_reset_mesh(viewer * view) return false; } +bool viewer::m_pop_mesh(viewer * view) +{ + view->pop_mesh(); + return false; +} + bool viewer::m_save_mesh(viewer * view) { const che * mesh = view->selected_mesh(); From c60d1fdbebe9364722a11f6f27f5c0953d240b21 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 19 Nov 2023 19:09:27 +0100 Subject: [PATCH 0891/1018] viewer: delete selected mesh --- include/gproshan/viewer/viewer.h | 2 ++ src/gproshan/app_viewer.cpp | 4 ++-- src/gproshan/viewer/viewer.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index e2db886e..de2fcdfd 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -111,6 +111,7 @@ class viewer che_viewer & selected_mesh(); void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); bool add_mesh(che * p_mesh, const bool & reset_normals = true); + bool remove_mesh(const index_t & idx); bool pop_mesh(); void update_viewport_meshes(); void update_status_message(const char * format, ...); @@ -147,6 +148,7 @@ class viewer static bool m_save_load_view(viewer * view); static bool m_reset_mesh(viewer * view); static bool m_save_mesh(viewer * view); + static bool m_remove_mesh(viewer * view); static bool m_pop_mesh(viewer * view); static bool m_normalize_mesh(viewer * view); static bool m_zoom_in(viewer * view); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 7e823be4..7ff5758c 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -56,7 +56,7 @@ void app_viewer::init() add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); add_process(GLFW_KEY_Q, "Q", "Edge Collapse", process_edge_collapse); add_process(GLFW_KEY_M, "M", "Multiplicate", process_multiplicate_vertices); - add_process(GLFW_KEY_DELETE, "DELETE", "Delete vertices", process_delete_vertices); + add_process(GLFW_KEY_SLASH, "SLASH", "Delete vertices", process_delete_vertices); add_process(GLFW_KEY_MINUS, "MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices); sub_menus.push_back("Fairing"); @@ -90,7 +90,7 @@ void app_viewer::init() sub_menus.push_back("Others"); add_process(GLFW_KEY_SEMICOLON, "SEMICOLON", "Select multiple vertices", process_select_multiple); - add_process(GLFW_KEY_SLASH, "SLASH", "Threshold", process_threshold); + add_process(GLFW_KEY_BACKSLASH, "BACKSLASH", "Threshold", process_threshold); add_process(GLFW_KEY_N, "N", "Noise", process_noise); add_process(GLFW_KEY_P, "P", "Black noise", process_black_noise); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7f0a9bf2..a8fd9e70 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -427,6 +427,7 @@ void viewer::init_menus() sub_menus.push_back("Mesh"); add_process(GLFW_KEY_R, "R", "Reload/Reset", m_reset_mesh); add_process(GLFW_KEY_W, "W", "Save Mesh", m_save_mesh); + add_process(GLFW_KEY_DELETE, "DELETE", "Remove Selected Mesh", m_remove_mesh); add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Pop Mesh", m_pop_mesh); add_process(0, "", "Normalize Mesh", m_normalize_mesh); add_process(GLFW_KEY_F2, "F2", "Invert Normals", m_invert_normals); @@ -492,6 +493,25 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) return true; } +bool viewer::remove_mesh(const index_t & idx) +{ + if(meshes.size() == 1) + return false; + + if(idx_selected_mesh == meshes.size() - 1) + --idx_selected_mesh; + + delete meshes[idx]; + for(index_t i = idx; i < meshes.size() - 1; ++i) + meshes[i] = meshes[i + 1]; + + meshes.pop_back(); + + update_viewport_meshes(); + + return true; +} + bool viewer::pop_mesh() { if(meshes.size() == 1) @@ -754,6 +774,12 @@ bool viewer::m_reset_mesh(viewer * view) return false; } +bool viewer::m_remove_mesh(viewer * view) +{ + view->remove_mesh(view->idx_selected_mesh); + return false; +} + bool viewer::m_pop_mesh(viewer * view) { view->pop_mesh(); From 083210afc7ff63dfd4d4a2c914dd6e6503566d07 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 19 Nov 2023 22:30:58 +0100 Subject: [PATCH 0892/1018] viewer: refactoring add_process, menu/submenu entries --- include/gproshan/viewer/glfw_keys.h | 143 ++++++++++++++++++++++++++++ include/gproshan/viewer/viewer.h | 15 ++- src/gproshan/app_viewer.cpp | 68 ++++++------- src/gproshan/viewer/viewer.cpp | 89 +++++++++-------- 4 files changed, 230 insertions(+), 85 deletions(-) create mode 100644 include/gproshan/viewer/glfw_keys.h diff --git a/include/gproshan/viewer/glfw_keys.h b/include/gproshan/viewer/glfw_keys.h new file mode 100644 index 00000000..050ffbc9 --- /dev/null +++ b/include/gproshan/viewer/glfw_keys.h @@ -0,0 +1,143 @@ +#ifndef GLFW_KEYS_H +#define GLFW_KEYS_H + + +#include + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +const std::unordered_map glfw_key_name = { + {GLFW_KEY_UNKNOWN, ""},//-1 + {GLFW_KEY_SPACE, "SPACE"},//32 + {GLFW_KEY_APOSTROPHE, "'"},//39 /* ' */ + {GLFW_KEY_COMMA, ","},//44 /* , */ + {GLFW_KEY_MINUS, "-"},//45 /* - */ + {GLFW_KEY_PERIOD, "."},//46 /* . */ + {GLFW_KEY_SLASH, "/"},//47 /* / */ + {GLFW_KEY_0, "0"},//48 + {GLFW_KEY_1, "1"},//49 + {GLFW_KEY_2, "2"},//50 + {GLFW_KEY_3, "3"},//51 + {GLFW_KEY_4, "4"},//52 + {GLFW_KEY_5, "5"},//53 + {GLFW_KEY_6, "6"},//54 + {GLFW_KEY_7, "7"},//55 + {GLFW_KEY_8, "8"},//56 + {GLFW_KEY_9, "9"},//57 + {GLFW_KEY_SEMICOLON, ";"},//59 /* ; */ + {GLFW_KEY_EQUAL, "="},//61 /* = */ + {GLFW_KEY_A, "A"},//65 + {GLFW_KEY_B, "B"},//66 + {GLFW_KEY_C, "C"},//67 + {GLFW_KEY_D, "D"},//68 + {GLFW_KEY_E, "E"},//69 + {GLFW_KEY_F, "F"},//70 + {GLFW_KEY_G, "G"},//71 + {GLFW_KEY_H, "H"},//72 + {GLFW_KEY_I, "I"},//73 + {GLFW_KEY_J, "J"},//74 + {GLFW_KEY_K, "K"},//75 + {GLFW_KEY_L, "L"},//76 + {GLFW_KEY_M, "M"},//77 + {GLFW_KEY_N, "N"},//78 + {GLFW_KEY_O, "O"},//79 + {GLFW_KEY_P, "P"},//80 + {GLFW_KEY_Q, "Q"},//81 + {GLFW_KEY_R, "R"},//82 + {GLFW_KEY_S, "S"},//83 + {GLFW_KEY_T, "T"},//84 + {GLFW_KEY_U, "U"},//85 + {GLFW_KEY_V, "V"},//86 + {GLFW_KEY_W, "W"},//87 + {GLFW_KEY_X, "X"},//88 + {GLFW_KEY_Y, "Y"},//89 + {GLFW_KEY_Z, "Z"},//90 + {GLFW_KEY_LEFT_BRACKET, "["},//91 /* [ */ + {GLFW_KEY_BACKSLASH, "\\"},//92 /* \ */ + {GLFW_KEY_RIGHT_BRACKET, "]"},//93 /* ] */ + {GLFW_KEY_GRAVE_ACCENT, "`"},//96 /* ` */ + {GLFW_KEY_WORLD_1, ""},//161 /* non-US #1 */ + {GLFW_KEY_WORLD_2, ""},//162 /* non-US #2 */ + {GLFW_KEY_ESCAPE, "ESCAPE"},//256 + {GLFW_KEY_ENTER, "ENTER"},//257 + {GLFW_KEY_TAB, "TAB"},//258 + {GLFW_KEY_BACKSPACE, "BACKSPACE"},//259 + {GLFW_KEY_INSERT, "INSERT"},//260 + {GLFW_KEY_DELETE, "DELETE"},//261 + {GLFW_KEY_RIGHT, "RIGHT"},//262 + {GLFW_KEY_LEFT, "LEFT"},//263 + {GLFW_KEY_DOWN, "DOWN"},//264 + {GLFW_KEY_UP, "UP"},//265 + {GLFW_KEY_PAGE_UP, ""},//266 + {GLFW_KEY_PAGE_DOWN, ""},//267 + {GLFW_KEY_HOME, ""},//268 + {GLFW_KEY_END, ""},//269 + {GLFW_KEY_CAPS_LOCK, ""},//280 + {GLFW_KEY_SCROLL_LOCK, ""},//281 + {GLFW_KEY_NUM_LOCK, ""},//282 + {GLFW_KEY_PRINT_SCREEN, ""},//283 + {GLFW_KEY_PAUSE, ""},//284 + {GLFW_KEY_F1, "F1"},//290 + {GLFW_KEY_F2, "F2"},//291 + {GLFW_KEY_F3, "F3"},//292 + {GLFW_KEY_F4, "F4"},//293 + {GLFW_KEY_F5, "F5"},//294 + {GLFW_KEY_F6, "F6"},//295 + {GLFW_KEY_F7, "F7"},//296 + {GLFW_KEY_F8, "F8"},//297 + {GLFW_KEY_F9, "F9"},//298 + {GLFW_KEY_F10, "F10"},//299 + {GLFW_KEY_F11, "F11"},//300 + {GLFW_KEY_F12, "F12"},//301 + {GLFW_KEY_F13, ""},//302 + {GLFW_KEY_F14, ""},//303 + {GLFW_KEY_F15, ""},//304 + {GLFW_KEY_F16, ""},//305 + {GLFW_KEY_F17, ""},//306 + {GLFW_KEY_F18, ""},//307 + {GLFW_KEY_F19, ""},//308 + {GLFW_KEY_F20, ""},//309 + {GLFW_KEY_F21, ""},//310 + {GLFW_KEY_F22, ""},//311 + {GLFW_KEY_F23, ""},//312 + {GLFW_KEY_F24, ""},//313 + {GLFW_KEY_F25, ""},//314 + {GLFW_KEY_KP_0, ""},//320 + {GLFW_KEY_KP_1, ""},//321 + {GLFW_KEY_KP_2, ""},//322 + {GLFW_KEY_KP_3, ""},//323 + {GLFW_KEY_KP_4, ""},//324 + {GLFW_KEY_KP_5, ""},//325 + {GLFW_KEY_KP_6, ""},//326 + {GLFW_KEY_KP_7, ""},//327 + {GLFW_KEY_KP_8, ""},//328 + {GLFW_KEY_KP_9, ""},//329 + {GLFW_KEY_KP_DECIMAL, ""},//330 + {GLFW_KEY_KP_DIVIDE, ""},//331 + {GLFW_KEY_KP_MULTIPLY, ""},//332 + {GLFW_KEY_KP_SUBTRACT, ""},//333 + {GLFW_KEY_KP_ADD, ""},//334 + {GLFW_KEY_KP_ENTER, ""},//335 + {GLFW_KEY_KP_EQUAL, ""},//336 + {GLFW_KEY_LEFT_SHIFT, ""},//340 + {GLFW_KEY_LEFT_CONTROL, ""},//341 + {GLFW_KEY_LEFT_ALT, ""},//342 + {GLFW_KEY_LEFT_SUPER, ""},//343 + {GLFW_KEY_RIGHT_SHIFT, ""},//344 + {GLFW_KEY_RIGHT_CONTROL, ""},//345 + {GLFW_KEY_RIGHT_ALT, ""},//346 + {GLFW_KEY_RIGHT_SUPER, ""},//347 + {GLFW_KEY_MENU, ""},//348 + {GLFW_KEY_LAST, ""},//GLFW_KEY_MENU + }; + + +} // namespace gproshan + +#endif // GLFW_KEYS_H + diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index de2fcdfd..3c26e8b8 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -40,14 +40,11 @@ class viewer struct process_t { - std::string key; - std::string name; - function_t function; - index_t sub_menu; + const char * key = nullptr; + const char * name = nullptr; + function_t function = nullptr; + index_t sub_menu = NIL; bool selected = false; - - process_t() = default; - process_t(const std::string & k, const std::string & n, function_t f, const index_t & sm = NIL): key(k), name(n), function(f), sub_menu(sm) {}; }; static const std::vector m_window_split; @@ -93,7 +90,7 @@ class viewer float bgc = 0; - std::map processes; + std::unordered_map processes; che_viewer * sphere = nullptr; shader shader_sphere; @@ -109,7 +106,7 @@ class viewer virtual ~viewer(); che_viewer & selected_mesh(); - void add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f); + void add_process(const char * name, const function_t & f, const int & key = -1); bool add_mesh(che * p_mesh, const bool & reset_normals = true); bool remove_mesh(const index_t & idx); bool pop_mesh(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 7ff5758c..c2a9d774 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -43,56 +43,56 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { sub_menus.push_back("Point Cloud"); - add_process(1001, "", "KNN", process_knn); - add_process(1002, "", "Compute Normals", process_compute_normals); + add_process("KNN", process_knn); + add_process("Compute Normals", process_compute_normals); sub_menus.push_back("Scenes"); - add_process(1003, "", "Scan Scene", process_simulate_scanner); + add_process("Scan Scene", process_simulate_scanner); sub_menus.push_back("Geometry"); - add_process(1004, "", "Sampling 4points", process_sampling_4points); - add_process(GLFW_KEY_H, "H", "2D Convex Hull", process_convex_hull); - add_process(GLFW_KEY_O, "O", "Connected Components", process_connected_components); - add_process(GLFW_KEY_K, "K", "Gaussian curvature", process_gaussian_curvature); - add_process(GLFW_KEY_Q, "Q", "Edge Collapse", process_edge_collapse); - add_process(GLFW_KEY_M, "M", "Multiplicate", process_multiplicate_vertices); - add_process(GLFW_KEY_SLASH, "SLASH", "Delete vertices", process_delete_vertices); - add_process(GLFW_KEY_MINUS, "MINUS", "Delete non-manifold vertices", process_delete_non_manifold_vertices); + add_process("Sampling 4points", process_sampling_4points); + add_process("2D Convex Hull", process_convex_hull, GLFW_KEY_H); + add_process("Connected Components", process_connected_components, GLFW_KEY_O); + add_process("Gaussian curvature", process_gaussian_curvature, GLFW_KEY_K); + add_process("Edge Collapse", process_edge_collapse, GLFW_KEY_Q); + add_process("Multiplicate", process_multiplicate_vertices, GLFW_KEY_M); + add_process("Delete vertices", process_delete_vertices, GLFW_KEY_SLASH); + add_process("Delete non-manifold vertices", process_delete_non_manifold_vertices, GLFW_KEY_MINUS); sub_menus.push_back("Fairing"); - add_process(GLFW_KEY_F, "F", "Fairing Taubin", process_fairing_taubin); - add_process(GLFW_KEY_E, "E", "Fairing Spectral", process_fairing_spectral); + add_process("Fairing Taubin", process_fairing_taubin, GLFW_KEY_F); + add_process("Fairing Spectral", process_fairing_spectral, GLFW_KEY_E); sub_menus.push_back("Geodesics"); - add_process(GLFW_KEY_G, "G", "Geodesics", process_geodesics); - add_process(GLFW_KEY_S, "S", "Geodesic Farthest Point Sampling", process_farthest_point_sampling); - add_process(GLFW_KEY_V, "V", "Geodesic Voronoi", process_voronoi); - add_process(GLFW_KEY_T, "T", "Toplesets", process_compute_toplesets); + add_process("Geodesics", process_geodesics, GLFW_KEY_G); + add_process("Geodesic Farthest Point Sampling", process_farthest_point_sampling, GLFW_KEY_S); + add_process("Geodesic Voronoi", process_voronoi, GLFW_KEY_V); + add_process("Toplesets", process_compute_toplesets, GLFW_KEY_T); sub_menus.push_back("Sparse Coding"); - add_process(GLFW_KEY_U, "U", "Mesh Sparse Coding", process_msparse_coding); - add_process(GLFW_KEY_J, "J", "MDICT Patch", process_mdict_patch); - add_process(GLFW_KEY_D, "D", "MDICT Mask", process_mask); - add_process(GLFW_KEY_L, "L", "PC reconstruction", process_pc_reconstruction); + add_process("Mesh Sparse Coding", process_msparse_coding, GLFW_KEY_U); + add_process("MDICT Patch", process_mdict_patch, GLFW_KEY_J); + add_process("MDICT Mask", process_mask, GLFW_KEY_D); + add_process("PC reconstruction", process_pc_reconstruction, GLFW_KEY_L); sub_menus.push_back("Features"); - add_process(GLFW_KEY_2, "2", "Eigenfunctions", process_eigenfuntions); - add_process(GLFW_KEY_3, "3", "Descriptors", process_descriptor_heatmap); - add_process(GLFW_KEY_4, "4", "Key Points", process_key_points); - add_process(GLFW_KEY_5, "5", "Key Components", process_key_components); + add_process("Eigenfunctions", process_eigenfuntions, GLFW_KEY_2); + add_process("Descriptors", process_descriptor_heatmap, GLFW_KEY_3); + add_process("Key Points", process_key_points, GLFW_KEY_4); + add_process("Key Components", process_key_components, GLFW_KEY_5); sub_menus.push_back("Hole Filling"); - add_process(GLFW_KEY_X, "X", "Poisson: Membrane surface", process_poisson_laplacian_1); - add_process(GLFW_KEY_Y, "Y", "Poisson: Thin-plate surface", process_poisson_laplacian_2); - add_process(GLFW_KEY_Z, "Z", "Poisson: Minimum variation surface", process_poisson_laplacian_3); - add_process(GLFW_KEY_6, "6", "Fill hole: planar mesh", process_fill_holes); - add_process(GLFW_KEY_7, "7", "Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines); + add_process("Poisson: Membrane surface", process_poisson_laplacian_1, GLFW_KEY_X); + add_process("Poisson: Thin-plate surface", process_poisson_laplacian_2, GLFW_KEY_Y); + add_process("Poisson: Minimum variation surface", process_poisson_laplacian_3, GLFW_KEY_Z); + add_process("Fill hole: planar mesh", process_fill_holes, GLFW_KEY_6); + add_process("Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines, GLFW_KEY_7); sub_menus.push_back("Others"); - add_process(GLFW_KEY_SEMICOLON, "SEMICOLON", "Select multiple vertices", process_select_multiple); - add_process(GLFW_KEY_BACKSLASH, "BACKSLASH", "Threshold", process_threshold); - add_process(GLFW_KEY_N, "N", "Noise", process_noise); - add_process(GLFW_KEY_P, "P", "Black noise", process_black_noise); + add_process("Select multiple vertices", process_select_multiple, GLFW_KEY_SEMICOLON); + add_process("Threshold", process_threshold, GLFW_KEY_BACKSLASH); + add_process("Noise", process_noise, GLFW_KEY_N); + add_process("Black noise", process_black_noise, GLFW_KEY_P); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a8fd9e70..472aafc7 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include @@ -178,8 +179,8 @@ void viewer::imgui() { process_t & pro = p.second; if(pro.function != nullptr && pro.sub_menu == i) - if(ImGui::MenuItem(pro.name.c_str(), ('[' + pro.key + ']').c_str(), &pro.selected)) - update_status_message("%s", pro.selected ? pro.name.c_str() : ""); + if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) + update_status_message("%s", pro.selected ? pro.name : ""); //ImGui::Separator(); } @@ -306,9 +307,10 @@ void viewer::imgui() for(auto & p: processes) { process_t & pro = p.second; - if(ImGui::CollapsingHeader(("[" + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) + const std::string hstr = "["; + if(ImGui::CollapsingHeader((hstr + pro.key + "] " + pro.name).c_str(), &pro.selected, ImGuiTreeNodeFlags_DefaultOpen)) { - ImGui::PushID(pro.name.c_str()); + ImGui::PushID(pro.name); ImGui::Indent(); pro.selected = pro.selected && pro.function(this); ImGui::Unindent(); @@ -397,44 +399,44 @@ void viewer::init_imgui() void viewer::init_menus() { sub_menus.push_back("Viewer"); - add_process(GLFW_KEY_F1, "F1", "Help", m_help); - add_process(GLFW_KEY_ESCAPE, "ESCAPE", "Close", m_close); - add_process(GLFW_KEY_F11, "F11", "Maximize", m_maximize); - add_process(GLFW_KEY_I, "F1", "Hide/Show ImGui", m_hide_show_imgui); - add_process(GLFW_KEY_PERIOD, "PERIOD", "Save/Load view", m_save_load_view); - add_process(GLFW_KEY_UP, "UP", "Zoom in", m_zoom_in); - add_process(GLFW_KEY_DOWN, "DOWN", "Zoom out", m_zoom_out); - add_process(GLFW_KEY_RIGHT, "RIGHT", "Background color inc", m_bgc_inc); - add_process(GLFW_KEY_LEFT, "LEFT", "Background color dec", m_bgc_dec); - add_process(GLFW_KEY_1, "1", "Background color white", m_bgc_white); - add_process(GLFW_KEY_0, "0", "Background color black", m_bgc_black); + add_process("Help", m_help, GLFW_KEY_F1); + add_process("Close", m_close, GLFW_KEY_ESCAPE); + add_process("Maximize", m_maximize, GLFW_KEY_F11); + add_process("Hide/Show ImGui", m_hide_show_imgui, GLFW_KEY_I); + add_process("Save/Load view", m_save_load_view, GLFW_KEY_PERIOD); + add_process("Zoom in", m_zoom_in, GLFW_KEY_UP); + add_process("Zoom out", m_zoom_out, GLFW_KEY_DOWN); + add_process("Background color inc", m_bgc_inc, GLFW_KEY_RIGHT); + add_process("Background color dec", m_bgc_dec, GLFW_KEY_LEFT); + add_process("Background color white", m_bgc_white, GLFW_KEY_1); + add_process("Background color black", m_bgc_black, GLFW_KEY_0); sub_menus.push_back("Render"); - add_process(GLFW_KEY_F5, "F5", "Render Point Cloud", m_render_pointcloud); - add_process(GLFW_KEY_F6, "F6", "Render Wireframe", m_render_wireframe); - add_process(GLFW_KEY_F7, "F7", "Render Triangles", m_render_triangles); - add_process(GLFW_KEY_F8, "F8", "Render GL", m_render_gl); - add_process(GLFW_KEY_SPACE, "SPACE", "Level Curves", m_render_lines); - add_process(GLFW_KEY_TAB, "TAB", "Render Flat", m_render_flat); - add_process(GLFW_KEY_R, "R", "Setup Raytracing", m_setup_raytracing); - add_process(GLFW_KEY_F9, "F9", "Render Embree", m_render_embree); - add_process(GLFW_KEY_ENTER, "ENTER", "Raycasting", m_raycasting); + add_process("Render Point Cloud", m_render_pointcloud, GLFW_KEY_F5); + add_process("Render Wireframe", m_render_wireframe, GLFW_KEY_F6); + add_process("Render Triangles", m_render_triangles, GLFW_KEY_F7); + add_process("Render GL", m_render_gl, GLFW_KEY_F8); + add_process("Level Curves", m_render_lines, GLFW_KEY_SPACE); + add_process("Render Flat", m_render_flat, GLFW_KEY_TAB); + add_process("Setup Raytracing", m_setup_raytracing, GLFW_KEY_R); + add_process("Render Embree", m_render_embree, GLFW_KEY_F9); + add_process("Raycasting", m_raycasting, GLFW_KEY_ENTER); #ifdef GPROSHAN_OPTIX - add_process(GLFW_KEY_F10, "F10", "Render OptiX", m_render_optix); + add_process("Render OptiX", m_render_optix, GLFW_KEY_F10); #endif // GPROSHAN_OPTIX sub_menus.push_back("Mesh"); - add_process(GLFW_KEY_R, "R", "Reload/Reset", m_reset_mesh); - add_process(GLFW_KEY_W, "W", "Save Mesh", m_save_mesh); - add_process(GLFW_KEY_DELETE, "DELETE", "Remove Selected Mesh", m_remove_mesh); - add_process(GLFW_KEY_BACKSPACE, "BACKSPACE", "Pop Mesh", m_pop_mesh); - add_process(0, "", "Normalize Mesh", m_normalize_mesh); - add_process(GLFW_KEY_F2, "F2", "Invert Normals", m_invert_normals); - add_process(GLFW_KEY_F3, "F3", "Gradient Field", m_render_gradients); - add_process(GLFW_KEY_F4, "F4", "Normal Field", m_render_normals); - add_process(GLFW_KEY_B, "B", "Select Border Vertices", m_select_border_vertices); - add_process(GLFW_KEY_C, "C", "Clean Selected Vertices", m_clean_selected_vertices); + add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT); + add_process("Save Mesh", m_save_mesh, GLFW_KEY_W); + add_process("Remove Selected Mesh", m_remove_mesh, GLFW_KEY_DELETE); + add_process("Pop Mesh", m_pop_mesh, GLFW_KEY_BACKSPACE); + add_process("Normalize Mesh", m_normalize_mesh); + add_process("Invert Normals", m_invert_normals, GLFW_KEY_F2); + add_process("Gradient Field", m_render_gradients, GLFW_KEY_F3); + add_process("Normal Field", m_render_normals, GLFW_KEY_F4); + add_process("Select Border Vertices", m_select_border_vertices, GLFW_KEY_B); + add_process("Clean Selected Vertices", m_clean_selected_vertices, GLFW_KEY_C); } void viewer::init_glsl() @@ -461,14 +463,17 @@ void viewer::init_glsl() shader_depth.load_fragment(shaders_path("fragment_depth.glsl")); } -void viewer::add_process(const int & key, const std::string & skey, const std::string & name, const function_t & f) +void viewer::add_process(const char * name, const function_t & f, const int & key) { - if(processes.find(key) == processes.end()) + static int nk = 1000; + + const int fkey = key == -1 ? ++nk : key; + if(processes.find(fkey) == processes.end()) { - processes[key] = {skey, name, f}; - processes[key].sub_menu = sub_menus.size() - 1; + processes[fkey] = {glfw_key_name.at(key), name, f}; + processes[fkey].sub_menu = sub_menus.size() - 1; } - else fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, skey.c_str(), name.c_str()); + else fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, glfw_key_name.at(key), name); } bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) @@ -608,7 +613,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in if(pro.function) { pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; - view->update_status_message("%s", pro.selected ? pro.name.c_str() : ""); + view->update_status_message("%s", pro.selected ? pro.name : ""); } } @@ -683,7 +688,7 @@ bool viewer::m_help(viewer * view) { for(auto & p: view->processes) if(p.second.function != nullptr) - fprintf(stderr, "%16s: %s\n", ('[' + p.second.key + ']').c_str(), p.second.name.c_str()); + fprintf(stderr, "%16s: %s\n", (std::string("[") + p.second.key + ']').c_str(), p.second.name); return false; } From 6ba2ffaf6796da82a81a80d1f1443fb6d33d67c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 19 Nov 2023 23:52:08 +0100 Subject: [PATCH 0893/1018] viewer: fix callback keyboard glfw --- include/gproshan/app_viewer.h | 2 -- src/gproshan/viewer/viewer.cpp | 9 ++++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 8d09f6cf..1fe594f3 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -57,8 +57,6 @@ class app_viewer : public viewer protected: virtual void init(); - che * load_mesh(const std::string & file_path); - // Point Cloud static bool process_knn(viewer * p_view); static bool process_compute_normals(viewer * p_view); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 472aafc7..7d8b1f40 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -608,13 +608,12 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in if(action == GLFW_RELEASE) return; viewer * view = (viewer *) glfwGetWindowUserPointer(window); + if(view->processes.find(key) == view->processes.end()) + return; process_t & pro = view->processes[key]; - if(pro.function) - { - pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; - view->update_status_message("%s", pro.selected ? pro.name : ""); - } + pro.selected = view->hide_imgui ? pro.function(view) && pro.selected : !pro.selected; + view->update_status_message("%s", pro.selected ? pro.name : ""); } void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mods) From d043dd28e2a571a3d3e250c68bdca1e06ebc100b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 21 Nov 2023 13:03:14 +0100 Subject: [PATCH 0894/1018] rt: intersect get flat normal --- include/gproshan/raytracing/embree.h | 2 +- include/gproshan/raytracing/raytracing.h | 3 ++- src/gproshan/raytracing/embree.cpp | 5 +++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index fa6c8f86..ec2ad1b7 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -49,7 +49,7 @@ class embree : public raytracing virtual ~embree(); virtual index_t closest_vertex(const vertex & org, const vertex & dir) const; - virtual eval_hit intersect(const vertex & org, const vertex & dir) const; + virtual eval_hit intersect(const vertex & org, const vertex & dir, const bool & flat = true) const; protected: diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index af32a077..d16a74b7 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -30,7 +30,8 @@ class raytracing ) const; virtual eval_hit intersect( const vertex &, // org - const vertex & //dir + const vertex &, //dir + [[maybe_unused]] const bool & flat = true ) const { return {}; } virtual index_t closest_vertex( const vertex &, // org, diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index f0b25a7a..645d2b03 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -43,7 +43,7 @@ vertex embree::ray_hit::dir() const vertex embree::ray_hit::normal() const { - return normalize(vec3{hit.Ng_x, hit.Ng_y, hit.Ng_z}); + return normalize(vertex{hit.Ng_x, hit.Ng_y, hit.Ng_z}); } vertex embree::ray_hit::pos() const @@ -109,7 +109,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) const return mesh->VT[he]; } -eval_hit embree::intersect(const vertex & org, const vertex & dir) const +eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & flat) const { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -117,6 +117,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir) const eval_hit hit(*g_meshes[r.hit.geomID], r.hit.primID, r.hit.u, r.hit.v, sc); hit.dist = r.ray.tfar; hit.position = r.pos(); + hit.normal = flat ? r.normal() : hit.normal; return hit; } From 5b9fe5fe7531752bd98f9707ec6587bbe2db1184 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 26 Nov 2023 03:18:03 +0100 Subject: [PATCH 0895/1018] viewer: refactoring menus --- include/gproshan/viewer/viewer.h | 8 +++-- src/gproshan/app_viewer.cpp | 24 ++++++++------- src/gproshan/viewer/viewer.cpp | 52 ++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 3c26e8b8..86d258f8 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -43,7 +43,7 @@ class viewer const char * key = nullptr; const char * name = nullptr; function_t function = nullptr; - index_t sub_menu = NIL; + index_t id_menu = NIL; bool selected = false; }; @@ -90,6 +90,8 @@ class viewer float bgc = 0; + std::vector menus; + std::vector > menu_processes; std::unordered_map processes; che_viewer * sphere = nullptr; @@ -97,7 +99,6 @@ class viewer std::vector sphere_points; std::vector vectors; - std::vector sub_menus; char status_message[1024] = {}; @@ -106,7 +107,8 @@ class viewer virtual ~viewer(); che_viewer & selected_mesh(); - void add_process(const char * name, const function_t & f, const int & key = -1); + void add_menu(const std::string & str, const std::vector & vprocesses); + int add_process(const char * name, const function_t & f, const int & key = -1); bool add_mesh(che * p_mesh, const bool & reset_normals = true); bool remove_mesh(const index_t & idx); bool pop_mesh(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index c2a9d774..aaf1f426 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -42,14 +42,16 @@ int app_viewer::main(int nargs, const char ** args) void app_viewer::init() { - sub_menus.push_back("Point Cloud"); - add_process("KNN", process_knn); - add_process("Compute Normals", process_compute_normals); + add_menu("Point Cloud", + { + add_process("KNN", process_knn), + add_process("Compute Normals", process_compute_normals) + }); - sub_menus.push_back("Scenes"); + menus.push_back("Scenes"); add_process("Scan Scene", process_simulate_scanner); - sub_menus.push_back("Geometry"); + menus.push_back("Geometry"); add_process("Sampling 4points", process_sampling_4points); add_process("2D Convex Hull", process_convex_hull, GLFW_KEY_H); add_process("Connected Components", process_connected_components, GLFW_KEY_O); @@ -59,36 +61,36 @@ void app_viewer::init() add_process("Delete vertices", process_delete_vertices, GLFW_KEY_SLASH); add_process("Delete non-manifold vertices", process_delete_non_manifold_vertices, GLFW_KEY_MINUS); - sub_menus.push_back("Fairing"); + menus.push_back("Fairing"); add_process("Fairing Taubin", process_fairing_taubin, GLFW_KEY_F); add_process("Fairing Spectral", process_fairing_spectral, GLFW_KEY_E); - sub_menus.push_back("Geodesics"); + menus.push_back("Geodesics"); add_process("Geodesics", process_geodesics, GLFW_KEY_G); add_process("Geodesic Farthest Point Sampling", process_farthest_point_sampling, GLFW_KEY_S); add_process("Geodesic Voronoi", process_voronoi, GLFW_KEY_V); add_process("Toplesets", process_compute_toplesets, GLFW_KEY_T); - sub_menus.push_back("Sparse Coding"); + menus.push_back("Sparse Coding"); add_process("Mesh Sparse Coding", process_msparse_coding, GLFW_KEY_U); add_process("MDICT Patch", process_mdict_patch, GLFW_KEY_J); add_process("MDICT Mask", process_mask, GLFW_KEY_D); add_process("PC reconstruction", process_pc_reconstruction, GLFW_KEY_L); - sub_menus.push_back("Features"); + menus.push_back("Features"); add_process("Eigenfunctions", process_eigenfuntions, GLFW_KEY_2); add_process("Descriptors", process_descriptor_heatmap, GLFW_KEY_3); add_process("Key Points", process_key_points, GLFW_KEY_4); add_process("Key Components", process_key_components, GLFW_KEY_5); - sub_menus.push_back("Hole Filling"); + menus.push_back("Hole Filling"); add_process("Poisson: Membrane surface", process_poisson_laplacian_1, GLFW_KEY_X); add_process("Poisson: Thin-plate surface", process_poisson_laplacian_2, GLFW_KEY_Y); add_process("Poisson: Minimum variation surface", process_poisson_laplacian_3, GLFW_KEY_Z); add_process("Fill hole: planar mesh", process_fill_holes, GLFW_KEY_6); add_process("Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines, GLFW_KEY_7); - sub_menus.push_back("Others"); + menus.push_back("Others"); add_process("Select multiple vertices", process_select_multiple, GLFW_KEY_SEMICOLON); add_process("Threshold", process_threshold, GLFW_KEY_BACKSLASH); add_process("Noise", process_noise, GLFW_KEY_N); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7d8b1f40..fd6eb05d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -140,6 +140,12 @@ void viewer::imgui() che_viewer & mesh = selected_mesh(); + if(ImGui::BeginPopupContextVoid("mesh")) + { + ImGui::Text("hola"); + ImGui::EndPopup(); + } + if(ImGui::BeginMainMenuBar()) { if(ImGui::BeginMenu("Select")) @@ -171,14 +177,14 @@ void viewer::imgui() ImGui::EndMenu(); } - for(index_t i = 0; i < sub_menus.size(); ++i) + for(index_t i = 0; i < menus.size(); ++i) { - if(ImGui::BeginMenu(sub_menus[i].c_str())) + if(ImGui::BeginMenu(menus[i].c_str())) { for(auto & p: processes) { process_t & pro = p.second; - if(pro.function != nullptr && pro.sub_menu == i) + if(pro.function != nullptr && pro.id_menu == i) if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) update_status_message("%s", pro.selected ? pro.name : ""); @@ -398,7 +404,7 @@ void viewer::init_imgui() void viewer::init_menus() { - sub_menus.push_back("Viewer"); + menus.push_back("Viewer"); add_process("Help", m_help, GLFW_KEY_F1); add_process("Close", m_close, GLFW_KEY_ESCAPE); add_process("Maximize", m_maximize, GLFW_KEY_F11); @@ -411,7 +417,7 @@ void viewer::init_menus() add_process("Background color white", m_bgc_white, GLFW_KEY_1); add_process("Background color black", m_bgc_black, GLFW_KEY_0); - sub_menus.push_back("Render"); + menus.push_back("Render"); add_process("Render Point Cloud", m_render_pointcloud, GLFW_KEY_F5); add_process("Render Wireframe", m_render_wireframe, GLFW_KEY_F6); add_process("Render Triangles", m_render_triangles, GLFW_KEY_F7); @@ -426,7 +432,7 @@ void viewer::init_menus() add_process("Render OptiX", m_render_optix, GLFW_KEY_F10); #endif // GPROSHAN_OPTIX - sub_menus.push_back("Mesh"); + menus.push_back("Mesh"); add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT); add_process("Save Mesh", m_save_mesh, GLFW_KEY_W); add_process("Remove Selected Mesh", m_remove_mesh, GLFW_KEY_DELETE); @@ -463,17 +469,30 @@ void viewer::init_glsl() shader_depth.load_fragment(shaders_path("fragment_depth.glsl")); } -void viewer::add_process(const char * name, const function_t & f, const int & key) +void viewer::add_menu(const std::string & str, const std::vector & vprocesses) +{ + menus.push_back(str); + menu_processes.emplace_back(vprocesses); + + const int & id_menu = menus.size() - 1; + for(const int & p: menu_processes.back()) + processes[p].id_menu = id_menu; +} + +int viewer::add_process(const char * name, const function_t & f, const int & key) { static int nk = 1000; - const int fkey = key == -1 ? ++nk : key; - if(processes.find(fkey) == processes.end()) + const int & fkey = key == -1 ? ++nk : key; + if(processes.find(fkey) != processes.end()) { - processes[fkey] = {glfw_key_name.at(key), name, f}; - processes[fkey].sub_menu = sub_menus.size() - 1; + fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, glfw_key_name.at(key), name); + return -1; } - else fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, glfw_key_name.at(key), name); + + processes[fkey] = {glfw_key_name.at(key), name, f}; + + return fkey; } bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) @@ -625,7 +644,7 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); - if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) + if(action == GLFW_RELEASE) { float xscale, yscale; glfwGetWindowContentScale(window, &xscale, &yscale); @@ -637,7 +656,7 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod if(idx_mesh < view->meshes.size()) view->idx_selected_mesh = idx_mesh; - if(mods == GLFW_MOD_SHIFT) + if(mods == GLFW_MOD_SHIFT && button == GLFW_MOUSE_BUTTON_LEFT) view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); } @@ -657,7 +676,7 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) view->render_params.restart = true; } - if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)) + if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE)) { view->cam.pos.im().x() = 2 * x / view->window_width - 1; view->cam.pos.im().y() = 2 * y / view->window_height - 1; @@ -667,9 +686,10 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) void viewer::scroll_callback(GLFWwindow * window, double, double yoffset) { - viewer * view = (viewer *) glfwGetWindowUserPointer(window); if(ImGui::GetIO().WantCaptureMouse) return; + viewer * view = (viewer *) glfwGetWindowUserPointer(window); + if(yoffset > 0) { view->cam.zoom_in(); From 28627a2f61b145f6f9f7feaaac6cdaf67aa9a023 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 26 Nov 2023 03:37:34 +0100 Subject: [PATCH 0896/1018] viewer: refactoring menu items --- src/gproshan/app_viewer.cpp | 110 +++++++++++++++++++-------------- src/gproshan/viewer/viewer.cpp | 85 +++++++++++++------------ 2 files changed, 108 insertions(+), 87 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index aaf1f426..220eeb9a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -48,53 +48,69 @@ void app_viewer::init() add_process("Compute Normals", process_compute_normals) }); - menus.push_back("Scenes"); - add_process("Scan Scene", process_simulate_scanner); - - menus.push_back("Geometry"); - add_process("Sampling 4points", process_sampling_4points); - add_process("2D Convex Hull", process_convex_hull, GLFW_KEY_H); - add_process("Connected Components", process_connected_components, GLFW_KEY_O); - add_process("Gaussian curvature", process_gaussian_curvature, GLFW_KEY_K); - add_process("Edge Collapse", process_edge_collapse, GLFW_KEY_Q); - add_process("Multiplicate", process_multiplicate_vertices, GLFW_KEY_M); - add_process("Delete vertices", process_delete_vertices, GLFW_KEY_SLASH); - add_process("Delete non-manifold vertices", process_delete_non_manifold_vertices, GLFW_KEY_MINUS); - - menus.push_back("Fairing"); - add_process("Fairing Taubin", process_fairing_taubin, GLFW_KEY_F); - add_process("Fairing Spectral", process_fairing_spectral, GLFW_KEY_E); - - menus.push_back("Geodesics"); - add_process("Geodesics", process_geodesics, GLFW_KEY_G); - add_process("Geodesic Farthest Point Sampling", process_farthest_point_sampling, GLFW_KEY_S); - add_process("Geodesic Voronoi", process_voronoi, GLFW_KEY_V); - add_process("Toplesets", process_compute_toplesets, GLFW_KEY_T); - - menus.push_back("Sparse Coding"); - add_process("Mesh Sparse Coding", process_msparse_coding, GLFW_KEY_U); - add_process("MDICT Patch", process_mdict_patch, GLFW_KEY_J); - add_process("MDICT Mask", process_mask, GLFW_KEY_D); - add_process("PC reconstruction", process_pc_reconstruction, GLFW_KEY_L); - - menus.push_back("Features"); - add_process("Eigenfunctions", process_eigenfuntions, GLFW_KEY_2); - add_process("Descriptors", process_descriptor_heatmap, GLFW_KEY_3); - add_process("Key Points", process_key_points, GLFW_KEY_4); - add_process("Key Components", process_key_components, GLFW_KEY_5); - - menus.push_back("Hole Filling"); - add_process("Poisson: Membrane surface", process_poisson_laplacian_1, GLFW_KEY_X); - add_process("Poisson: Thin-plate surface", process_poisson_laplacian_2, GLFW_KEY_Y); - add_process("Poisson: Minimum variation surface", process_poisson_laplacian_3, GLFW_KEY_Z); - add_process("Fill hole: planar mesh", process_fill_holes, GLFW_KEY_6); - add_process("Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines, GLFW_KEY_7); - - menus.push_back("Others"); - add_process("Select multiple vertices", process_select_multiple, GLFW_KEY_SEMICOLON); - add_process("Threshold", process_threshold, GLFW_KEY_BACKSLASH); - add_process("Noise", process_noise, GLFW_KEY_N); - add_process("Black noise", process_black_noise, GLFW_KEY_P); + add_menu("Scenes", + { + add_process("Scan Scene", process_simulate_scanner) + }); + + add_menu("Geometry", + { + add_process("Sampling 4points", process_sampling_4points), + add_process("2D Convex Hull", process_convex_hull, GLFW_KEY_H), + add_process("Connected Components", process_connected_components, GLFW_KEY_O), + add_process("Gaussian curvature", process_gaussian_curvature, GLFW_KEY_K), + add_process("Edge Collapse", process_edge_collapse, GLFW_KEY_Q), + add_process("Multiplicate", process_multiplicate_vertices, GLFW_KEY_M), + add_process("Delete vertices", process_delete_vertices, GLFW_KEY_SLASH), + add_process("Delete non-manifold vertices", process_delete_non_manifold_vertices, GLFW_KEY_MINUS) + }); + + add_menu("Fairing", + { + add_process("Fairing Taubin", process_fairing_taubin, GLFW_KEY_F), + add_process("Fairing Spectral", process_fairing_spectral, GLFW_KEY_E) + }); + + add_menu("Geodesics", + { + add_process("Geodesics", process_geodesics, GLFW_KEY_G), + add_process("Geodesic Farthest Point Sampling", process_farthest_point_sampling, GLFW_KEY_S), + add_process("Geodesic Voronoi", process_voronoi, GLFW_KEY_V), + add_process("Toplesets", process_compute_toplesets, GLFW_KEY_T) + }); + + add_menu("Sparse Coding", + { + add_process("Mesh Sparse Coding", process_msparse_coding, GLFW_KEY_U), + add_process("MDICT Patch", process_mdict_patch, GLFW_KEY_J), + add_process("MDICT Mask", process_mask, GLFW_KEY_D), + add_process("PC reconstruction", process_pc_reconstruction, GLFW_KEY_L) + }); + + add_menu("Features", + { + add_process("Eigenfunctions", process_eigenfuntions, GLFW_KEY_2), + add_process("Descriptors", process_descriptor_heatmap, GLFW_KEY_3), + add_process("Key Points", process_key_points, GLFW_KEY_4), + add_process("Key Components", process_key_components, GLFW_KEY_5) + }); + + add_menu("Hole Filling", + { + add_process("Poisson: Membrane surface", process_poisson_laplacian_1, GLFW_KEY_X), + add_process("Poisson: Thin-plate surface", process_poisson_laplacian_2, GLFW_KEY_Y), + add_process("Poisson: Minimum variation surface", process_poisson_laplacian_3, GLFW_KEY_Z), + add_process("Fill hole: planar mesh", process_fill_holes, GLFW_KEY_6), + add_process("Fill hole: biharmonic splines", process_fill_holes_biharmonic_splines, GLFW_KEY_7) + }); + + add_menu("Others", + { + add_process("Select multiple vertices", process_select_multiple, GLFW_KEY_SEMICOLON), + add_process("Threshold", process_threshold, GLFW_KEY_BACKSLASH), + add_process("Noise", process_noise, GLFW_KEY_N), + add_process("Black noise", process_black_noise, GLFW_KEY_P) + }); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index fd6eb05d..af629028 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -181,10 +181,10 @@ void viewer::imgui() { if(ImGui::BeginMenu(menus[i].c_str())) { - for(auto & p: processes) + for(auto & p: menu_processes[i]) { - process_t & pro = p.second; - if(pro.function != nullptr && pro.id_menu == i) + process_t & pro = processes[p]; + if(pro.function != nullptr) if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) update_status_message("%s", pro.selected ? pro.name : ""); @@ -404,45 +404,50 @@ void viewer::init_imgui() void viewer::init_menus() { - menus.push_back("Viewer"); - add_process("Help", m_help, GLFW_KEY_F1); - add_process("Close", m_close, GLFW_KEY_ESCAPE); - add_process("Maximize", m_maximize, GLFW_KEY_F11); - add_process("Hide/Show ImGui", m_hide_show_imgui, GLFW_KEY_I); - add_process("Save/Load view", m_save_load_view, GLFW_KEY_PERIOD); - add_process("Zoom in", m_zoom_in, GLFW_KEY_UP); - add_process("Zoom out", m_zoom_out, GLFW_KEY_DOWN); - add_process("Background color inc", m_bgc_inc, GLFW_KEY_RIGHT); - add_process("Background color dec", m_bgc_dec, GLFW_KEY_LEFT); - add_process("Background color white", m_bgc_white, GLFW_KEY_1); - add_process("Background color black", m_bgc_black, GLFW_KEY_0); - - menus.push_back("Render"); - add_process("Render Point Cloud", m_render_pointcloud, GLFW_KEY_F5); - add_process("Render Wireframe", m_render_wireframe, GLFW_KEY_F6); - add_process("Render Triangles", m_render_triangles, GLFW_KEY_F7); - add_process("Render GL", m_render_gl, GLFW_KEY_F8); - add_process("Level Curves", m_render_lines, GLFW_KEY_SPACE); - add_process("Render Flat", m_render_flat, GLFW_KEY_TAB); - add_process("Setup Raytracing", m_setup_raytracing, GLFW_KEY_R); - add_process("Render Embree", m_render_embree, GLFW_KEY_F9); - add_process("Raycasting", m_raycasting, GLFW_KEY_ENTER); + add_menu("Viewer", + { + add_process("Help", m_help, GLFW_KEY_F1), + add_process("Close", m_close, GLFW_KEY_ESCAPE), + add_process("Maximize", m_maximize, GLFW_KEY_F11), + add_process("Hide/Show ImGui", m_hide_show_imgui, GLFW_KEY_I), + add_process("Save/Load view", m_save_load_view, GLFW_KEY_PERIOD), + add_process("Zoom in", m_zoom_in, GLFW_KEY_UP), + add_process("Zoom out", m_zoom_out, GLFW_KEY_DOWN), + add_process("Background color inc", m_bgc_inc, GLFW_KEY_RIGHT), + add_process("Background color dec", m_bgc_dec, GLFW_KEY_LEFT), + add_process("Background color white", m_bgc_white, GLFW_KEY_1), + add_process("Background color black", m_bgc_black, GLFW_KEY_0) + }); -#ifdef GPROSHAN_OPTIX - add_process("Render OptiX", m_render_optix, GLFW_KEY_F10); -#endif // GPROSHAN_OPTIX + add_menu("Render", + { + add_process("Render Point Cloud", m_render_pointcloud, GLFW_KEY_F5), + add_process("Render Wireframe", m_render_wireframe, GLFW_KEY_F6), + add_process("Render Triangles", m_render_triangles, GLFW_KEY_F7), + add_process("Render GL", m_render_gl, GLFW_KEY_F8), + add_process("Level Curves", m_render_lines, GLFW_KEY_SPACE), + add_process("Render Flat", m_render_flat, GLFW_KEY_TAB), + add_process("Setup Raytracing", m_setup_raytracing, GLFW_KEY_R), + add_process("Render Embree", m_render_embree, GLFW_KEY_F9), + #ifdef GPROSHAN_OPTIX + add_process("Render OptiX", m_render_optix, GLFW_KEY_F10), + #endif // GPROSHAN_OPTIX + add_process("Raycasting", m_raycasting, GLFW_KEY_ENTER) + }); - menus.push_back("Mesh"); - add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT); - add_process("Save Mesh", m_save_mesh, GLFW_KEY_W); - add_process("Remove Selected Mesh", m_remove_mesh, GLFW_KEY_DELETE); - add_process("Pop Mesh", m_pop_mesh, GLFW_KEY_BACKSPACE); - add_process("Normalize Mesh", m_normalize_mesh); - add_process("Invert Normals", m_invert_normals, GLFW_KEY_F2); - add_process("Gradient Field", m_render_gradients, GLFW_KEY_F3); - add_process("Normal Field", m_render_normals, GLFW_KEY_F4); - add_process("Select Border Vertices", m_select_border_vertices, GLFW_KEY_B); - add_process("Clean Selected Vertices", m_clean_selected_vertices, GLFW_KEY_C); + add_menu("Mesh", + { + add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT), + add_process("Save Mesh", m_save_mesh, GLFW_KEY_W), + add_process("Remove Selected Mesh", m_remove_mesh, GLFW_KEY_DELETE), + add_process("Pop Mesh", m_pop_mesh, GLFW_KEY_BACKSPACE), + add_process("Normalize Mesh", m_normalize_mesh), + add_process("Invert Normals", m_invert_normals, GLFW_KEY_F2), + add_process("Gradient Field", m_render_gradients, GLFW_KEY_F3), + add_process("Normal Field", m_render_normals, GLFW_KEY_F4), + add_process("Select Border Vertices", m_select_border_vertices, GLFW_KEY_B), + add_process("Clean Selected Vertices", m_clean_selected_vertices, GLFW_KEY_C) + }); } void viewer::init_glsl() From 0ccfc87190e9d101bf1567edd0a58d61c36292a4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 26 Nov 2023 03:55:20 +0100 Subject: [PATCH 0897/1018] viewer: context menu mesh --- src/gproshan/viewer/viewer.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index af629028..b7299383 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -142,7 +142,14 @@ void viewer::imgui() if(ImGui::BeginPopupContextVoid("mesh")) { - ImGui::Text("hola"); + ImGui::TextDisabled(mesh->filename.c_str()); + for(auto & p: menu_processes[2]) // init_menus + { + process_t & pro = processes[p]; + if(pro.function != nullptr) + if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) + update_status_message("%s", pro.selected ? pro.name : ""); + } ImGui::EndPopup(); } @@ -187,8 +194,6 @@ void viewer::imgui() if(pro.function != nullptr) if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) update_status_message("%s", pro.selected ? pro.name : ""); - - //ImGui::Separator(); } ImGui::EndMenu(); } From 2435bd0eb18d31357108a84b44e714f5d6292592 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 26 Nov 2023 18:12:33 +0100 Subject: [PATCH 0898/1018] viewer: mesh context menu added render/mesh options --- src/gproshan/viewer/viewer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index b7299383..a9f26df1 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -143,6 +143,13 @@ void viewer::imgui() if(ImGui::BeginPopupContextVoid("mesh")) { ImGui::TextDisabled(mesh->filename.c_str()); + for(auto & p: menu_processes[1]) // init_menus + { + process_t & pro = processes[p]; + if(pro.function != nullptr) + if(ImGui::MenuItem(pro.name, pro.key, &pro.selected)) + update_status_message("%s", pro.selected ? pro.name : ""); + } for(auto & p: menu_processes[2]) // init_menus { process_t & pro = processes[p]; From 8fbd63e88834afc1fbce69ee491551e557ad285f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 26 Nov 2023 21:35:25 +0100 Subject: [PATCH 0899/1018] viewer: adding removed meshes history --- include/gproshan/viewer/viewer.h | 1 + src/gproshan/viewer/viewer.cpp | 51 ++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 86d258f8..54830c08 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -84,6 +84,7 @@ class viewer index_t nframes = 0; std::vector meshes; + std::vector removed_meshes; index_t idx_selected_mesh = 0; frame * frames = nullptr; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a9f26df1..bc7851d1 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -336,6 +336,36 @@ void viewer::imgui() } } + + if(removed_meshes.size()) + { + if(ImGui::CollapsingHeader("Removed meshes", ImGuiTreeNodeFlags_DefaultOpen)) + { + for(index_t i = 0; i < removed_meshes.size(); ++i) + { + che_viewer * m = removed_meshes[i]; + ImGui::PushID(m); + if(ImGui::Button("add")) + { + meshes.push_back(m); + removed_meshes.erase(begin(removed_meshes) + i); + + update_viewport_meshes(); + } + ImGui::SameLine(); + if(ImGui::Button("delete")) + { + delete m; + removed_meshes.erase(begin(removed_meshes) + i); + } + ImGui::SameLine(); + ImGui::Selectable((*m)->filename.c_str()); + ImGui::PopID(); + } + } + } + + ImGui::PopItemWidth(); ImGui::End(); @@ -525,7 +555,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) mesh.log_info(); idx_selected_mesh = meshes.size() - 1; - glfwSetWindowTitle(window, mesh->filename.c_str()); + glfwSetWindowTitle(window, ("gproshan - " + mesh->filename).c_str()); update_viewport_meshes(); @@ -539,14 +569,11 @@ bool viewer::remove_mesh(const index_t & idx) if(meshes.size() == 1) return false; - if(idx_selected_mesh == meshes.size() - 1) - --idx_selected_mesh; - - delete meshes[idx]; - for(index_t i = idx; i < meshes.size() - 1; ++i) - meshes[i] = meshes[i + 1]; + removed_meshes.push_back(meshes[idx]); + meshes.erase(begin(meshes) + idx); - meshes.pop_back(); + if(idx_selected_mesh == meshes.size()) + --idx_selected_mesh; update_viewport_meshes(); @@ -558,12 +585,12 @@ bool viewer::pop_mesh() if(meshes.size() == 1) return false; - if(idx_selected_mesh == meshes.size() - 1) - --idx_selected_mesh; - - delete meshes.back(); + removed_meshes.push_back(meshes.back()); meshes.pop_back(); + if(idx_selected_mesh == meshes.size()) + --idx_selected_mesh; + update_viewport_meshes(); return true; From 517f6edb018fb4beb9772c73105b1224ff4c1731 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 15:49:34 +0100 Subject: [PATCH 0900/1018] viewer: fix width mesh context menu --- src/gproshan/viewer/viewer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index bc7851d1..89d1deaa 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -140,9 +140,12 @@ void viewer::imgui() che_viewer & mesh = selected_mesh(); + ImGui::SetNextWindowSize(ImVec2(250, -1)); if(ImGui::BeginPopupContextVoid("mesh")) { - ImGui::TextDisabled(mesh->filename.c_str()); + const int & p = size(mesh->filename) - 31; + + ImGui::TextDisabled("%s%30s", p < 0 ? "" : "<<", mesh->filename.substr(p < 0 ? 0 : p).c_str()); for(auto & p: menu_processes[1]) // init_menus { process_t & pro = processes[p]; From 00b2bc39236707f4c5c9579ed979f706c28dea95 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 16:15:12 +0100 Subject: [PATCH 0901/1018] std: using size(container) instead of .size() --- src/gproshan/app_viewer.cpp | 20 ++++++------- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/sampling.cpp | 2 +- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 +- .../test_geodesics_ptp_coalescence.cu | 2 +- src/gproshan/mdict/mdict.cpp | 8 ++--- src/gproshan/mdict/msparse_coding.cpp | 30 +++++++++---------- src/gproshan/mdict/patch.cpp | 26 ++++++++-------- src/gproshan/mesh/che.cpp | 18 +++++------ src/gproshan/mesh/che_fill_hole.cpp | 26 ++++++++-------- src/gproshan/mesh/che_obj.cpp | 14 ++++----- src/gproshan/mesh/che_off.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 8 ++--- src/gproshan/mesh/che_xyz.cpp | 2 +- src/gproshan/pointcloud/knn.cpp | 2 +- src/gproshan/raytracing/embree.cpp | 2 +- src/gproshan/raytracing/optix.cpp | 8 ++--- src/gproshan/scenes/scene.cpp | 10 +++---- src/gproshan/viewer/che_viewer.cpp | 8 ++--- src/gproshan/viewer/scene_viewer.cpp | 8 ++--- src/gproshan/viewer/viewer.cpp | 16 +++++----- 23 files changed, 110 insertions(+), 110 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 220eeb9a..cfc0d148 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -129,7 +129,7 @@ bool app_viewer::process_knn(viewer * p_view) if(ImGui::Button("Run")) { auto query = mesh.selected; - if(!query.size()) query.push_back(0); + if(!size(query)) query.push_back(0); mesh.selected.clear(); @@ -191,7 +191,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) static const size_t n_max = 10000; static vertex cam_pos = {0, 0, 0}; - if(view->sphere_points.size() != 1) + if(size(view->sphere_points) != 1) { view->sphere_points.clear(); view->sphere_points.push_back(cam_pos); @@ -231,7 +231,7 @@ bool app_viewer::process_sampling_4points(viewer * p_view) if(ImGui::Button("Clean")) points.clear(); ImGui::SameLine(); - if(mesh.selected.size() > 3) + if(size(mesh.selected) > 3) { if(ImGui::Button("Add Samples")) { @@ -407,7 +407,7 @@ bool app_viewer::process_delete_vertices(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - if(!mesh.selected.size()) return true; + if(!size(mesh.selected)) return true; gproshan_debug(removing vertex); mesh->remove_vertices(mesh.selected); @@ -444,7 +444,7 @@ bool app_viewer::process_fairing_spectral(viewer * p_view) if(ImGui::SliderScalar("n_eigs", ImGuiDataType_U64, &fair.n_eigs, &min_neigs, &max_neigs)) { - if(vertices.size() != mesh->n_vertices) + if(size(vertices) != mesh->n_vertices) { vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->point(0), mesh->n_vertices * sizeof(vertex)); @@ -473,7 +473,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) if(ImGui_InputReal("step", &fair.step, 0.001)) { - if(vertices.size() != mesh->n_vertices) + if(size(vertices) != mesh->n_vertices) { vertices.resize(mesh->n_vertices); memcpy(vertices.data(), &mesh->point(0), mesh->n_vertices * sizeof(vertex)); @@ -510,7 +510,7 @@ bool app_viewer::process_geodesics(viewer * p_view) if(ImGui::Button("Run")) { - if(!mesh.selected.size()) + if(!size(mesh.selected)) mesh.selected.push_back(0); params.dist_alloc = &mesh->heatmap(0); @@ -584,7 +584,7 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - if(!mesh.selected.size()) + if(!size(mesh.selected)) mesh.selected.push_back(0); index_t * toplesets = new index_t[mesh->n_vertices]; @@ -723,7 +723,7 @@ bool app_viewer::process_mask(viewer * p_view) a_vec points_out; gproshan_debug_var(f_points); points_out.load(f_points); - gproshan_debug_var(points_out.size()); + gproshan_debug_var(size(points_out)); for(index_t i = 0; i < points_out.size(); ++i) mesh.selected.push_back(points_out(i)); @@ -953,7 +953,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) center /= vbounds.size(); std::priority_queue > front; - std::vector neigs(vertices.size()); + std::vector neigs(size(vertices)); /* auto bprev = [&](const index_t & v) -> index_t & { diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 2790673b..8a458a8d 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -27,7 +27,7 @@ geodesics::geodesics(che * mesh, const std::vector & sources, const par for(index_t v = 0; v < n_vertices; ++v) dist[v] = INFINITY; - assert(sources.size() > 0); + assert(size(sources) > 0); execute(mesh, sources, p); } diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 025e1658..8f3853a7 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -12,7 +12,7 @@ namespace gproshan { double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) { - if(!sources.size()) return 0; + if(!size(sources)) return 0; // build impulse signal a_vec u0(mesh->n_vertices, arma::fill::zeros); diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index cdfc101c..19ae3701 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -60,7 +60,7 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si } else { - if(!points.size()) + if(!size(points)) points.push_back(0); #ifdef GPROSHAN_CUDA diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index adbb5e1f..42f0e957 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -172,7 +172,7 @@ std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, co cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); std::vector > iter_error; - iter_error.reserve(limits.size()); + iter_error.reserve(size(limits)); index_t d = 0; index_t start, end, n_cond; diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 0f167473..8e63dcb3 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -221,7 +221,7 @@ std::vector > iter_error_run_ptp_coalescence_gpu(CHE cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); std::vector > iter_error; - iter_error.reserve(limits.size()); + iter_error.reserve(size(limits)); std::ofstream os("band"); diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 04ddd04c..7ecf370d 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -47,7 +47,7 @@ a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & OMP(locval, X.col(i), i, D, L); arma::umat DI(2, locval.size()); - a_vec DV(locval.size()); + a_vec DV(size(locval)); #pragma omp parallel for for(index_t k = 0; k < locval.size(); ++k) @@ -84,7 +84,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); - rows.push_back(locval.size()); + rows.push_back(size(locval)); R = X - D * alpha; @@ -329,7 +329,7 @@ a_sp_mat OMP_all(std::vector & locval, const std::vector & patc OMP(locval, patches[i], i, A, L); arma::umat DI(2, locval.size()); - a_vec DV(locval.size()); + a_vec DV(size(locval)); #pragma omp parallel for for(index_t k = 0; k < locval.size(); ++k) @@ -367,7 +367,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si if(locval[k].i != locval[k - 1].i) rows.push_back(k); - rows.push_back(locval.size()); + rows.push_back(size(locval)); #pragma omp parallel for private(a, aj, D, e, sum, sum_error) for(index_t j = 0; j < A.n_cols; ++j) diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 8ce2cb25..11e60f5a 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -107,7 +107,7 @@ void msparse_coding::load_mask(const std::vector * vertices, const inde #pragma omp for for(index_t s = 0; s < m_params.n_patches; ++s) { - percentages_size[s] = ceil(vertices[s].size() * (m_params.percent / 100.0)) ; + percentages_size[s] = ceil(size(vertices[s]) * (m_params.percent / 100.0)) ; cover_cluster[s] = 0; } @@ -153,7 +153,7 @@ void msparse_coding::load_sampling() std::vector seeds; load_features(all_sorted_features, featsize); - gproshan_debug_var(all_sorted_features.size()); + gproshan_debug_var(size(all_sorted_features)); size_t count = 0; real_t area_mesh = mesh->area_surface(); @@ -209,7 +209,7 @@ void msparse_coding::load_sampling() p.init_radial_disjoint(euc_radio, geo_radio, mesh, vsf, m_params.delta, m_params.sum_thres, m_params.area_thres, area_mesh); count_cov_patch = 0; - if(p.vertices.size() >= 7 ) + if(size(p.vertices) >= 7 ) { for(const index_t & v: p.vertices) if(!covered[v]) ++count_cov_patch; @@ -245,7 +245,7 @@ void msparse_coding::load_sampling() gproshan_log(saving sampling); - S.resize(seeds.size()); + S.resize(size(seeds)); #pragma omp parallel for for(index_t i = 0; i < seeds.size(); ++i) @@ -256,7 +256,7 @@ void msparse_coding::load_sampling() gproshan_debug_var(m_params.sum_thres); gproshan_log_var(count); gproshan_log_var(count_cov); - gproshan_debug_var(seeds.size()); + gproshan_debug_var(size(seeds)); gproshan_debug_var(m_params.n_patches); @@ -268,7 +268,7 @@ void msparse_coding::load_sampling() if(!covered[i]) outliers.push_back(i); - a_vec outlv(outliers.size()); + a_vec outlv(size(outliers)); for(index_t i = 0; i < outliers.size(); ++i) outlv(i) = outliers[i]; @@ -291,10 +291,10 @@ void msparse_coding::init_radial_feature_patches() patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(size(patches[s].vertices), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(size(patches[s].vertices), patch_max_size); patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); @@ -428,10 +428,10 @@ void msparse_coding::init_voronoi_patches() patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(size(patches[s].vertices), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(size(patches[s].vertices), patch_max_size); patch_avg_size /= m_params.n_patches; //gproshan_debug_var(patch_avg_size); @@ -508,7 +508,7 @@ real_t msparse_coding::execute() gproshan_debug_var(d_time); arma::uvec non_zero = find( abs(alpha) > 0.00001); - gproshan_debug_var(non_zero.size()); + gproshan_debug_var(size(non_zero)); real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices); gproshan_log_var(ratio); @@ -814,10 +814,10 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) patch_avg_size += patches[s].vertices.size(); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_min_size = std::min(patches[s].vertices.size(), patch_min_size); + patch_min_size = std::min(size(patches[s].vertices), patch_min_size); #pragma omp parallel for reduction(max: patch_max_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_max_size = std::max(patches[s].vertices.size(), patch_max_size); + patch_max_size = std::max(size(patches[s].vertices), patch_max_size); patch_avg_size /= m_params.n_patches; gproshan_debug_var(patch_avg_size); @@ -914,7 +914,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) for(index_t v = 0; v < mesh->n_vertices; ++v) { // simple means vertex - if(patches_map[v].size() && (!mask || mask(v))) + if(size(patches_map[v]) && (!mask || mask(v))) { a_vec mv = arma::zeros(3); for(auto p: patches_map[v]) @@ -1003,7 +1003,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) index_t msparse_coding::sample(const index_t & s) { assert(s < m_params.n_patches); - if(sampling.size()) return sampling[s]; + if(size(sampling)) return sampling[s]; return s; } diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 23f2f034..bd86ea0e 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -201,7 +201,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind size_t d_fitting = 2; vertex p, c, n; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - if(vertices.size() > min_points) + if(size(vertices) > min_points) { jet_fit_directions(mesh, v); n.x() = T(0, 2); n.y() = T(1, 2); n.z() = T(2, 2); @@ -223,7 +223,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind } else { - gproshan_debug_var(vertices.size()); + gproshan_debug_var(size(vertices)); } } @@ -282,7 +282,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, // Refit the points and update the radius size_t d_fitting = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - if(vertices.size() > min_points) + if(size(vertices) > min_points) jet_fit_directions(mesh, v); else normal_fit_directions(mesh,v); @@ -341,7 +341,7 @@ void patch::reset_xyz(che * mesh, std::vector & vpatches, const inde //p idx patche where belongs to //j: local index //i: global index - //if(vpatches[vertices[i]].size() == 0) + //if(size(vpatches[vertices[i]]) == 0) //vpatches[vertices[i]].push_back({p, j++}); vpatches[vertices[i]][p] = j++; } @@ -358,7 +358,7 @@ double area_tri(double x1, double y1, double x2, double y2, double x3, double y3 void patch::remove_extra_xyz_disjoint(size_t & max_points) { - if(vertices.size() > max_points) + if(size(vertices) > max_points) { arma::uvec xi; xi.zeros(max_points); @@ -371,7 +371,7 @@ void patch::remove_extra_xyz_disjoint(size_t & max_points) void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t & p) { - size_t m = std::max (vertices.size(), min_nv); + size_t m = std::max (size(vertices), min_nv); size_t j = vertices.size(); @@ -479,10 +479,10 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector< /*gproshan_debug(number vertices considered); gproshan_debug_var(m); gproshan_debug(number vertices masked); - gproshan_debug_var(vertices.size() - m);*/ + gproshan_debug_var(size(vertices) - m);*/ } - m = std::max(vertices.size(), min_nv); + m = std::max(size(vertices), min_nv); xyz.set_size(3, m); index_t i = 0; @@ -520,7 +520,7 @@ const a_vec patch::normal() void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_toplevels, index_t * toplevel) { - if(vertices.size()) vertices.clear(); + if(size(vertices)) vertices.clear(); vertices.reserve(expected_nv); memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); @@ -547,7 +547,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); - if(vertices.size()) vertices.clear(); + if(size(vertices)) vertices.clear(); vertices.reserve(expected_nv); std::priority_queue > qvertices; @@ -591,10 +591,10 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) size_t d_fitting = 2; size_t d_monge = 2; //size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - //assert(vertices.size() > min_points); + //assert(size(vertices) > min_points); std::vector in_points; - in_points.reserve(vertices.size()); + in_points.reserve(size(vertices)); for(const index_t & u: vertices) in_points.push_back(DPoint(mesh->point(u).x(), mesh->point(u).y(), mesh->point(u).z())); @@ -738,7 +738,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, */ std::sort(distances.begin(), distances.end()); size_t n_elem = distances.size(); - if(distances.size()%2 ==0) + if(size(distances)%2 ==0) { avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; } diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index e6e9d325..398a298a 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -407,7 +407,7 @@ void che::multiplicate_vertices() void che::remove_vertices(const std::vector & vertices) { - if(!vertices.size()) return; + if(!size(vertices)) return; gproshan_debug(removing vertex); for(index_t v: vertices) @@ -441,7 +441,7 @@ void che::remove_vertices(const std::vector & vertices) } removed.push_back(n_vertices); - gproshan_debug_var(removed.size()); + gproshan_debug_var(size(removed)); gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; @@ -464,8 +464,8 @@ void che::remove_vertices(const std::vector & vertices) new_trigs.push_back(VT[he]); else gproshan_error_var(he); - gproshan_debug_var(new_vertices.size()); - gproshan_debug_var(new_trigs.size()); + gproshan_debug_var(size(new_vertices)); + gproshan_debug_var(size(new_trigs)); gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); @@ -500,7 +500,7 @@ void che::remove_non_manifold_vertices() } removed.push_back(n_vertices); - gproshan_debug_var(removed.size()); + gproshan_debug_var(size(removed)); gproshan_debug_var(removed[0]); index_t r = 1; index_t d = 1; @@ -523,8 +523,8 @@ void che::remove_non_manifold_vertices() new_trigs.push_back(VT[he]); else gproshan_error_var(he); - gproshan_debug_var(new_vertices.size()); - gproshan_debug_var(new_trigs.size()); + gproshan_debug_var(size(new_vertices)); + gproshan_debug_var(size(new_trigs)); gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); @@ -645,13 +645,13 @@ std::vector che::link(const index_t & v) const void che::edge_collapse(const std::vector & sort_edges) { - gproshan_error_var(sort_edges.size()); + gproshan_error_var(size(sort_edges)); // TODO } void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector & limits, const std::vector & sources, const index_t & k) { - if(!sources.size()) return; + if(!size(sources)) return; memset(toplesets, -1, sizeof(index_t) * n_vertices); diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index a0223596..93134ba8 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -29,7 +29,7 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert a_mat E(3, 3); a_vec ve(3); - vertices.reserve(border_vertices.size()); + vertices.reserve(size(border_vertices)); for(const index_t & b: border_vertices) { @@ -82,7 +82,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c while(--N) { - merge_vertices.push_back(vertices.size() + delta_v); + merge_vertices.push_back(std::size(vertices) + delta_v); vertices.push_back(vb + (N / L) * (va - vb)); } }; @@ -136,7 +136,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c if(i != p.first) { - merge_vertices[c].push_back(vertices[c].size() + hole->n_vertices - merge_vertices[!c].size()); + merge_vertices[c].push_back(std::size(vertices[c]) + hole->n_vertices - merge_vertices[!c].size()); n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1, hole->n_vertices - merge_vertices[!c].size()); } else merge_vertices[c].push_back(merge_vertices[!c].front()); @@ -338,8 +338,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti for(index_t v: front_vertices) vertices.push_back(mesh->point(v)); - std::vector tmp_vertices(vertices.size()); - std::vector tmp_normals(vertices.size()); + std::vector tmp_vertices(size(vertices)); + std::vector tmp_normals(size(vertices)); vertex normal; for(index_t v = 0; v < vertices.size(); ++v) @@ -368,8 +368,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti bool o = is_grow; - std::vector is_border(vertices.size()); - std::vector > neighbors(vertices.size()); + std::vector is_border(size(vertices)); + std::vector > neighbors(size(vertices)); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) @@ -559,8 +559,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti gproshan_debug_var(perimeter); // gproshan_debug(filling holes); -// gproshan_debug_var(vertices.size()); -// gproshan_debug_var(trigs.size()); +// gproshan_debug_var(size(vertices)); +// gproshan_debug_var(size(trigs)); return trigs.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); } @@ -624,9 +624,9 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng // END PCA ---------------------------------------------------------------------- - std::vector tmp_vertices(vertices.size()); - std::vector is_border(vertices.size()); - std::vector > neighbors(vertices.size()); + std::vector tmp_vertices(size(vertices)); + std::vector is_border(size(vertices)); + std::vector > neighbors(size(vertices)); index_t v, p_v, n_v; for(v = 0; v < vertices.size(); ++v) @@ -813,7 +813,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng } vertices.clear(); - vertices.reserve(tmp_vertices.size()); + vertices.reserve(size(tmp_vertices)); for(a_vec r: tmp_vertices) { diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index f8a729b9..f6c29a96 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -17,7 +17,7 @@ che_obj::che_obj(const std::string & file) void che_obj::read_file(const std::string & file) { parser p(file); - alloc(p.vertices.size(), p.trigs.size() / che::mtrig); + alloc(size(p.vertices), p.trigs.size() / che::mtrig); memcpy(GT, p.vertices.data(), p.vertices.size() * sizeof(vertex)); memcpy(VC, p.vcolors.data(), p.vcolors.size() * sizeof(rgb_t)); @@ -169,12 +169,12 @@ che_obj::parser::parser(const std::string & file) objects.emplace_back("", trigs.size()); - gproshan_log_var(vertices.size()); - gproshan_log_var(vnormals.size()); - gproshan_log_var(vtexcoords.size()); - gproshan_log_var(vcolors.size()); - gproshan_log_var(trigs.size()); - gproshan_log_var(objects.size()); + gproshan_log_var(size(vertices)); + gproshan_log_var(size(vnormals)); + gproshan_log_var(size(vtexcoords)); + gproshan_log_var(size(vcolors)); + gproshan_log_var(size(trigs)); + gproshan_log_var(size(objects)); } diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 4c9d21ab..4c1d6210 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -64,7 +64,7 @@ void che_off::read_file(const std::string & file) fclose(fp); - if(trigs.size() != che::mtrig * n_trigs) + if(size(trigs) != che::mtrig * n_trigs) { vertex * tGT = GT; GT = nullptr; vertex * tVN = VN; VN = nullptr; diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 29afb253..b38e8849 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -237,7 +237,7 @@ void che_ply::read_file(const std::string & file) fclose(fp); - if(trigs.size() != che::mtrig * n_trigs) + if(size(trigs) != che::mtrig * n_trigs) { vertex * tGT = GT; GT = nullptr; rgb_t * tVC = VC; VC = nullptr; diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 17fdfe32..c742b36d 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -44,12 +44,12 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) } v = i * cols; - trigs.push_back(vertices.size() - 2); + trigs.push_back(size(vertices) - 2); trigs.push_back(v); trigs.push_back(v + cols); v = (i + 1) * cols - 1; - trigs.push_back(vertices.size() - 1); + trigs.push_back(size(vertices) - 1); trigs.push_back(v + cols); trigs.push_back(v); } @@ -68,12 +68,12 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) } v = (2 * n - 1) * cols; - trigs.push_back(vertices.size() - 2); + trigs.push_back(size(vertices) - 2); trigs.push_back(v); trigs.push_back(0); v = (2 * n) * cols - 1; - trigs.push_back(vertices.size() - 1); + trigs.push_back(size(vertices) - 1); trigs.push_back(cols - 1); trigs.push_back(v); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index fd0106a4..349c0d10 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -39,7 +39,7 @@ void che_xyz::read_file(const std::string & file) fclose(fp); - alloc(vertices.size(), 0); + alloc(size(vertices), 0); memcpy(GT, vertices.data(), n_vertices * sizeof(vertex)); memcpy(VC, vertices_color.data(), n_vertices * sizeof(rgb_t)); } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 8dd43481..786ac3df 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -29,7 +29,7 @@ grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): p gproshan_log_var(sizeof(size_t)); gproshan_log_var(build_time); gproshan_log_var(res); - gproshan_log_var(voxels.size()); + gproshan_log_var(size(voxels)); gproshan_log_var(double(n_points) / voxels.size()); } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 645d2b03..2d87f75c 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -124,7 +124,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { - g_meshes.resize(meshes.size()); + g_meshes.resize(size(meshes)); for(index_t i = 0; i < meshes.size(); ++i) { g_meshes[i] = new CHE(meshes[i]); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index acdbf7ec..b2b87478 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -343,9 +343,9 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const { OptixTraversableHandle optix_as_handle = {}; - std::vector optix_meshes(meshes.size()); - std::vector optix_vertex_ptr(meshes.size()); - std::vector optix_trig_flags(meshes.size()); + std::vector optix_meshes(size(meshes)); + std::vector optix_vertex_ptr(size(meshes)); + std::vector optix_trig_flags(size(meshes)); for(index_t i = 0; i < meshes.size(); ++i) add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i], model_mats[i]); @@ -485,7 +485,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u tex_data.push_back(tex.data); } - gproshan_error_var(textures.size()); + gproshan_error_var(size(textures)); cudaMemcpy(optix_params.sc.materials, sc->materials.data(), sc->materials.size() * sizeof(scene::material), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.textures, textures.data(), textures.size() * sizeof(scene::texture), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 302f11e9..c33ed161 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -49,7 +49,7 @@ bool scene::load_obj(const std::string & file) if(!load_mtl(path + m)) return false; - alloc(p.trigs.size(), 0); + alloc(size(p.trigs), 0); #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) @@ -59,7 +59,7 @@ bool scene::load_obj(const std::string & file) VC[i] = p.vcolors[v]; } - if(p.vtexcoords.size()) + if(size(p.vtexcoords)) { texcoords = new vec2[n_vertices]; @@ -82,7 +82,7 @@ bool scene::load_obj(const std::string & file) for(auto & obj: p.objects) objects.push_back({obj.second, material_id[obj.first]}); - gproshan_log_var(objects.size()); + gproshan_log_var(size(objects)); trig_mat = new index_t[n_vertices / 3]; memset(trig_mat, -1, sizeof(index_t) * n_vertices / 3); @@ -207,8 +207,8 @@ bool scene::load_mtl(const std::string & file) } */ - gproshan_log_var(materials.size()); - gproshan_log_var(textures.size()); + gproshan_log_var(size(materials)); + gproshan_log_var(size(textures)); return true; } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 20dfed19..64bab098 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -217,16 +217,16 @@ void che_viewer::draw_pointcloud(shader & program) void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) { - if(selected_xyz.size() != selected.size()) + if(size(selected_xyz) != selected.size()) { selected_xyz.clear(); - selected_xyz.reserve(selected.size()); + selected_xyz.reserve(size(selected)); for(const index_t & v: selected) selected_xyz.push_back(mesh->point(v)); } - if(selected_xyz.size()) + if(size(selected_xyz)) { sphere.model_mat = model_mat; sphere.update_instances_positions(selected_xyz); @@ -253,7 +253,7 @@ void che_viewer::log_info() gproshan_log_var(mesh->n_edges); gproshan_log_var(mesh->area_surface()); gproshan_log_var(mesh->is_manifold()); - gproshan_log_var(mesh->bounds().size()); + gproshan_log_var(size(mesh->bounds())); gproshan_log_var(mesh->memory() / 1E6); gproshan_log_var(mesh->quality()); gproshan_log_var(mesh->genus()); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index b7ad834a..90dea83a 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -11,7 +11,7 @@ scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) { gltextures = new GLuint[sc->textures.size()]; - glGenTextures(sc->textures.size(), gltextures); + glGenTextures(size(sc->textures), gltextures); for(index_t i = 0; i < sc->textures.size(); ++i) init_texture(gltextures[i], sc->textures[i]); @@ -22,7 +22,7 @@ scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) scene_viewer::~scene_viewer() { glDeleteBuffers(1, &tex_vbo); - glDeleteTextures(sc->textures.size(), gltextures); + glDeleteTextures(size(sc->textures), gltextures); delete [] gltextures; } @@ -55,7 +55,7 @@ void scene_viewer::draw(shader & program) program.enable(); glBindVertexArray(vao); - if(sc->objects.size() == 1) + if(size(sc->objects) == 1) { glDrawArrays(GL_TRIANGLES, 0, mesh->n_vertices); } @@ -92,7 +92,7 @@ void scene_viewer::draw_pointcloud(shader & program) program.enable(); glBindVertexArray(vao); - if(sc->objects.size() == 1) + if(size(sc->objects) == 1) { glDrawArrays(GL_POINTS, 0, mesh->n_vertices); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 89d1deaa..55605e28 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -212,10 +212,10 @@ void viewer::imgui() ImGui::EndMainMenuBar(); } - if(meshes.size() > 1) + if(size(meshes) > 1) { ImGui::SetNextWindowSize(ImVec2(72, -1)); - ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[meshes.size()].x() - mesh.vy) * viewport_height - 70)); + ImGui::SetNextWindowPos(ImVec2((mesh.vx + 1) * viewport_width - 72, (m_window_split[size(meshes)].x() - mesh.vy) * viewport_height - 70)); ImGui::SetNextWindowBgAlpha(0.0f); ImGui::Begin("selected model", nullptr, ImGuiWindowFlags_NoTitleBar); ImGui::TextColored({0, 1, 0, 1}, "SELECTED"); @@ -340,7 +340,7 @@ void viewer::imgui() } - if(removed_meshes.size()) + if(size(removed_meshes)) { if(ImGui::CollapsingHeader("Removed meshes", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -547,7 +547,7 @@ int viewer::add_process(const char * name, const function_t & f, const int & key bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) { - if(meshes.size() == max_meshes) + if(size(meshes) == max_meshes) return false; if(reset_normals) @@ -569,7 +569,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) bool viewer::remove_mesh(const index_t & idx) { - if(meshes.size() == 1) + if(size(meshes) == 1) return false; removed_meshes.push_back(meshes[idx]); @@ -585,7 +585,7 @@ bool viewer::remove_mesh(const index_t & idx) bool viewer::pop_mesh() { - if(meshes.size() == 1) + if(size(meshes) == 1) return false; removed_meshes.push_back(meshes.back()); @@ -802,7 +802,7 @@ bool viewer::m_save_load_view(viewer * view) for(auto & p: std::filesystem::directory_iterator(tmp_file_path("views/"))) vfiles.push_back(p.path().string()); - if(!vfiles.size()) return true; + if(!size(vfiles)) return true; if(ImGui::BeginCombo("##loadfile", vfiles[select].c_str())) { @@ -1236,7 +1236,7 @@ void viewer::render_gl() mesh.draw_selected_vertices(*sphere, shader_sphere); - if(sphere_points.size()) + if(size(sphere_points)) { sphere->model_mat = mat4::identity(); sphere->update_instances_positions(sphere_points); From a9f9fa076f0d12bd21a33ee8aab72b53ff224ba8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 16:35:56 +0100 Subject: [PATCH 0902/1018] std: using size(container) instead of .size() --- include/gproshan/geodesics/geodesics_ptp.h | 6 +- src/gproshan/app_viewer.cpp | 24 ++--- src/gproshan/features/key_points.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cu | 2 +- src/gproshan/geodesics/sampling.cpp | 4 +- src/gproshan/geodesics/test_geodesics_ptp.cpp | 20 ++--- src/gproshan/geodesics/test_geodesics_ptp.cu | 10 +-- .../test_geodesics_ptp_coalescence.cu | 10 +-- src/gproshan/geometry/convex_hull.cpp | 2 +- src/gproshan/mdict/mdict.cpp | 24 ++--- src/gproshan/mdict/msparse_coding.cpp | 20 ++--- src/gproshan/mdict/patch.cpp | 34 +++---- src/gproshan/mesh/che.cpp | 8 +- src/gproshan/mesh/che_fill_hole.cpp | 90 +++++++++---------- src/gproshan/mesh/che_obj.cpp | 20 ++--- src/gproshan/mesh/che_off.cpp | 4 +- src/gproshan/mesh/che_ply.cpp | 4 +- src/gproshan/mesh/che_poisson.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 2 +- src/gproshan/pointcloud/knn.cpp | 6 +- src/gproshan/raytracing/embree.cpp | 2 +- src/gproshan/raytracing/optix.cpp | 20 ++--- src/gproshan/scenes/scene.cpp | 10 +-- src/gproshan/util.cpp | 2 +- src/gproshan/viewer/che_viewer.cpp | 4 +- src/gproshan/viewer/scene_viewer.cpp | 8 +- src/gproshan/viewer/shader.cpp | 4 +- src/gproshan/viewer/viewer.cpp | 36 ++++---- 29 files changed, 191 insertions(+), 191 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 17cad6ef..d9b9a11f 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -167,7 +167,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, index_t * h_clusters = clusters[2]; #endif - for(index_t i = 0; i < sources.size(); ++i) + for(index_t i = 0; i < size(sources); ++i) { // !coalescence ? const index_t & v = sorted ? sources[i] : idx[sources[i]]; @@ -196,7 +196,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, } #endif - const int & max_iter = limits.size() << 1; + const int & max_iter = size(limits) << 1; int iter = -1; index_t count = 0; @@ -247,7 +247,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, #endif if(n_cond == count) ++i; - if(j < limits.size() - 1) ++j; + if(j < size(limits) - 1) ++j; } return !(iter & 1); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index cfc0d148..32d0db2d 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -249,9 +249,9 @@ bool app_viewer::process_sampling_4points(viewer * p_view) } else ImGui::Text("Select 4 points."); - ImGui::Text("Total added points: %lu", points.size()); + ImGui::Text("Total added points: %lu", size(points)); if(ImGui::Button("Create Point Cloud")) - view->add_mesh(new che(points.data(), points.size(), nullptr, 0)); + view->add_mesh(new che(points.data(), size(points), nullptr, 0)); return true; } @@ -571,7 +571,7 @@ bool app_viewer::process_voronoi(viewer * p_view) for(index_t i = 0; i < mesh->n_vertices; ++i) { mesh->heatmap(i) = ptp.clusters[i]; - mesh->heatmap(i) /= mesh.selected.size() + 1; + mesh->heatmap(i) /= size(mesh.selected) + 1; } mesh.update_vbo_heatmap(); @@ -592,7 +592,7 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) std::vector limites; mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); - size_t n_toplesets = limites.size() - 1; + size_t n_toplesets = size(limites) - 1; #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) @@ -680,10 +680,10 @@ bool app_viewer::process_mdict_patch(viewer * p_view) view->vectors.push_back(mesh->point(v)); view->vectors.push_back(mesh->point(v) + 3 * mean_edge * mesh->normal(v)); - avg_nvp += p.vertices.size(); + avg_nvp += size(p.vertices); } - avg_nvp /= mesh.selected.size(); + avg_nvp /= size(mesh.selected); gproshan_log_var(avg_nvp); delete [] toplevel; @@ -950,7 +950,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) vertices.push_back(mesh->point(v)); center += mesh->point(v); } - center /= vbounds.size(); + center /= size(vbounds); std::priority_queue > front; std::vector neigs(size(vertices)); @@ -972,14 +972,14 @@ bool app_viewer::process_fill_holes(viewer * p_view) front.push({angle, p.x()}); }; - index_t nv = vertices.size(); + index_t nv = size(vertices); push({0, nv - 1, 1}); - for(index_t i = 1; i < vertices.size() - 1; ++i) + for(index_t i = 1; i < size(vertices) - 1; ++i) push({i, i - 1, i + 1}); push({nv - 1, nv - 2, 0}); std::vector border; - border.assign(true, vertices.size()); + border.assign(true, size(vertices)); // real_t angle; index_t v0 = NIL; @@ -998,7 +998,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) // vertices.push_back(center); che * old = fill_mesh; - che * hole = new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); + che * hole = new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); fill_mesh = old->merge(hole, vbounds); delete old; delete hole; @@ -1027,7 +1027,7 @@ bool app_viewer::process_fill_holes_biharmonic_splines(viewer * p_view) if(holes[h]) { old_n_vertices = n_vertices; - biharmonic_interp_2(mesh, old_n_vertices, n_vertices += holes[h]->n_vertices - border_vertices[h].size(), border_vertices[h], k); + biharmonic_interp_2(mesh, old_n_vertices, n_vertices += holes[h]->n_vertices - size(border_vertices[h]), border_vertices[h], k); delete holes[h]; } diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index aaf8f270..38005fed 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -50,7 +50,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) } } - kps.resize(percent * kps.size()); + kps.resize(percent * size(kps)); is_kp.assign(mesh->n_vertices, false); #pragma omp parallel for diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 8a458a8d..0ae4248d 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -199,7 +199,7 @@ void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const std::ve double time_ptp; TIC(time_ptp) - parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}, sources.size() == 1); + parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}, size(sources) == 1); TOC(time_ptp) gproshan_log_var(time_ptp); diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index a8666fe0..d90506a6 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -186,7 +186,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample if(n >= n_vertices) n = n_vertices >> 2; - n -= samples.size(); + n -= size(samples); samples.reserve(n); int farthest; diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index 19ae3701..ecaf03bd 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -40,7 +40,7 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si { const std::string & filename = mesh->filename; - std::string file = filename.substr(filename.find_last_of('/'), filename.size() - filename.find_last_of('/')) + "." + std::to_string(n); + std::string file = filename.substr(filename.find_last_of('/'), size(filename) - filename.find_last_of('/')) + "." + std::to_string(n); std::ifstream is(tmp_file_path(file)); gproshan_log_var(tmp_file_path(file)); @@ -73,7 +73,7 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si std::ofstream os(tmp_file_path(file)); os << radio << std::endl; - os << points.size() << std::endl; + os << size(points) << std::endl; for(const index_t & i: points) os << i << std::endl; diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index e77bdea1..973f5b79 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -170,20 +170,20 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // TOPLESETS DISTRIBUTION __________________________________________________________________ - index_t * toplesets_dist = new index_t[limits.size() - 1]; + index_t * toplesets_dist = new index_t[size(limits) - 1]; os.open(test_path + filename + "_toplesets.dist"); - for(index_t i = 1; i < limits.size(); ++i) + for(index_t i = 1; i < size(limits); ++i) { toplesets_dist[i - 1] = limits[i] - limits[i - 1]; os << i - 1 << " " << toplesets_dist[i - 1] << std::endl; } os.close(); - std::sort(toplesets_dist, toplesets_dist + limits.size() - 1); + std::sort(toplesets_dist, toplesets_dist + size(limits) - 1); os.open(test_path + filename + "_toplesets_sorted.dist"); - for(index_t i = 0; i < limits.size() - 1; ++i) + for(index_t i = 0; i < size(limits) - 1; ++i) os << i << " " << toplesets_dist[i] << std::endl; os.close(); @@ -213,7 +213,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) // FARTHEST POINT SAMPLING _________________________________________________________________ #ifdef GPROSHAN_CUDA // IMPLEMENT: times_farthest_point_sampling_ptp_cpu - size_t i_samples = source.size(); + size_t i_samples = size(source); size_t n_samples = 1001; double * times_fps = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); @@ -249,7 +249,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons geodesics fm(mesh, source); - error = compute_error(&fm[0], exact, mesh->n_vertices, source.size()); + error = compute_error(&fm[0], exact, mesh->n_vertices, size(source)); return seconds; } @@ -265,7 +265,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std: seconds = std::min(seconds, t); } - error = compute_error(dist, exact, mesh->n_vertices, source.size()); + error = compute_error(dist, exact, mesh->n_vertices, size(source)); delete [] dist; @@ -285,7 +285,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e stime = std::min(st, stime); } - error = compute_error(dist, exact, mesh->n_vertices, source.size()); + error = compute_error(dist, exact, mesh->n_vertices, size(source)); delete [] dist; @@ -306,7 +306,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std: seconds = std::min(seconds, t); } - error = compute_error(dist, exact, mesh->n_vertices, source.size()); + error = compute_error(dist, exact, mesh->n_vertices, size(source)); delete [] dist; @@ -328,7 +328,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact stime = std::min(st, stime); } - error = compute_error(dist, exact, mesh->n_vertices, source.size()); + error = compute_error(dist, exact, mesh->n_vertices, size(source)); delete [] dist; diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 42f0e957..3742e3e0 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -103,7 +103,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector double * times = new double[n + 1]; - n -= samples.size(); + n -= size(samples); samples.reserve(n); float time_fps; @@ -164,7 +164,7 @@ std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, co for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); ++i) + for(index_t i = 0; i < size(sources); ++i) h_dist[sources[i]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); @@ -190,8 +190,8 @@ std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, co // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); - if(j == limits.size() - 1) - iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size())}); + if(j == size(limits) - 1) + iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, size(sources))}); // end relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond, d_sorted); @@ -199,7 +199,7 @@ std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, co if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; - if(j < limits.size() - 1) ++j; + if(j < size(limits) - 1) ++j; d = !d; } diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 8e63dcb3..76f00f69 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -128,7 +128,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect double * times = new double[n + 1]; - n -= samples.size(); + n -= size(samples); samples.reserve(n); float time_fps; @@ -214,7 +214,7 @@ std::vector > iter_error_run_ptp_coalescence_gpu(CHE for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - for(index_t i = 0; i < sources.size(); ++i) + for(index_t i = 0; i < size(sources); ++i) h_dist[inv[sources[i]]] = 0; cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); @@ -243,8 +243,8 @@ std::vector > iter_error_run_ptp_coalescence_gpu(CHE // begin calculating iteration error cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); - if(j == limits.size() - 1) - iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, sources.size())}); + if(j == size(limits) - 1) + iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, size(sources))}); // end relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond); @@ -252,7 +252,7 @@ std::vector > iter_error_run_ptp_coalescence_gpu(CHE if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) ++i; - if(j < limits.size() - 1) ++j; + if(j < size(limits) - 1) ++j; d = !d; } diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 0b289bc8..37b9e9aa 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -8,7 +8,7 @@ namespace gproshan { -convex_hull::convex_hull(const std::vector & points): convex_hull(points.data(), points.size()) {} +convex_hull::convex_hull(const std::vector & points): convex_hull(points.data(), size(points)) {} convex_hull::convex_hull(const vertex * points, const size_t & n_points) { diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 7ecf370d..39ff79a6 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -46,11 +46,11 @@ a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & for(index_t i = 0; i < X.n_cols; ++i) OMP(locval, X.col(i), i, D, L); - arma::umat DI(2, locval.size()); + arma::umat DI(2, size(locval)); a_vec DV(size(locval)); #pragma omp parallel for - for(index_t k = 0; k < locval.size(); ++k) + for(index_t k = 0; k < size(locval); ++k) { DI(0, k) = locval[k].i; // row DI(1, k) = locval[k].j; // column @@ -80,7 +80,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) std::sort(locval.begin(), locval.end()); rows.push_back(0); - for(index_t k = 1; k < locval.size(); ++k) + for(index_t k = 1; k < size(locval); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); @@ -243,10 +243,10 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t & L) { - a_mat alpha(A.n_cols, patches.size()); + a_mat alpha(A.n_cols, size(patches)); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); ++i) + for(index_t i = 0; i < size(patches); ++i) alpha.col(i) = OMP(patches[i],phi_basis, A, L); return alpha; @@ -254,10 +254,10 @@ a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t & L) { - a_mat alpha(A.n_cols, patches.size()); + a_mat alpha(A.n_cols, size(patches)); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); ++i) + for(index_t i = 0; i < size(patches); ++i) alpha.col(i) = OMP(patches[i], A, L); return alpha; @@ -325,21 +325,21 @@ a_sp_mat OMP_all(std::vector & locval, const std::vector & patc locval.clear(); #pragma omp parallel for - for(index_t i = 0; i < patches.size(); ++i) + for(index_t i = 0; i < size(patches); ++i) OMP(locval, patches[i], i, A, L); - arma::umat DI(2, locval.size()); + arma::umat DI(2, size(locval)); a_vec DV(size(locval)); #pragma omp parallel for - for(index_t k = 0; k < locval.size(); ++k) + for(index_t k = 0; k < size(locval); ++k) { DI(0, k) = locval[k].i; // row DI(1, k) = locval[k].j; // column DV(k) = locval[k].val; } - return a_sp_mat(DI, DV, A.n_cols, patches.size()); + return a_sp_mat(DI, DV, A.n_cols, size(patches)); } void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k) @@ -363,7 +363,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si std::sort(locval.begin(), locval.end()); rows.push_back(0); - for(index_t k = 1; k < locval.size(); ++k) + for(index_t k = 1; k < size(locval); ++k) if(locval[k].i != locval[k - 1].i) rows.push_back(k); diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 11e60f5a..fc3b85f6 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -221,7 +221,7 @@ void msparse_coding::load_sampling() seeds.push_back(vsf); radios.push_back(euc_radio); geo_radios.push_back(geo_radio); - count += p.vertices.size(); + count += size(p.vertices); for(const index_t & v: p.vertices) { @@ -241,14 +241,14 @@ void msparse_coding::load_sampling() delete [] invalid_seed; - m_params.n_patches = seeds.size(); + m_params.n_patches = size(seeds); gproshan_log(saving sampling); S.resize(size(seeds)); #pragma omp parallel for - for(index_t i = 0; i < seeds.size(); ++i) + for(index_t i = 0; i < size(seeds); ++i) S(i) = seeds[i]; S.save(tmp_file_path(key_name + ".rsampl")); @@ -269,7 +269,7 @@ void msparse_coding::load_sampling() outliers.push_back(i); a_vec outlv(size(outliers)); - for(index_t i = 0; i < outliers.size(); ++i) + for(index_t i = 0; i < size(outliers); ++i) outlv(i) = outliers[i]; outlv.save(tmp_file_path(key_name + ".outlv")); @@ -288,7 +288,7 @@ void msparse_coding::init_radial_feature_patches() #pragma omp parallel for reduction(+: patch_avg_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_avg_size += patches[s].vertices.size(); + patch_avg_size += size(patches[s].vertices); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = std::min(size(patches[s].vertices), patch_min_size); @@ -425,7 +425,7 @@ void msparse_coding::init_voronoi_patches() #pragma omp parallel for reduction(+: patch_avg_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_avg_size += patches[s].vertices.size(); + patch_avg_size += size(patches[s].vertices); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = std::min(size(patches[s].vertices), patch_min_size); @@ -587,7 +587,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) patches[i].xyz(2, j) }); - che * new_mesh = new che(point_cloud.data(), point_cloud.size(), nullptr, 0); + che * new_mesh = new che(point_cloud.data(), size(point_cloud), nullptr, 0); new_mesh->update_normals(); a_vec n; @@ -811,7 +811,7 @@ void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) #pragma omp parallel for reduction(+: patch_avg_size) for(index_t s = 0; s < m_params.n_patches; ++s) - patch_avg_size += patches[s].vertices.size(); + patch_avg_size += size(patches[s].vertices); #pragma omp parallel for reduction(min: patch_min_size) for(index_t s = 0; s < m_params.n_patches; ++s) patch_min_size = std::min(size(patches[s].vertices), patch_min_size); @@ -886,7 +886,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) a_vec x = rp.phi * A * alpha.col(p); - patches_error[p] = { accu(abs(x - rp.xyz.row(2).t())) / rp.vertices.size(), p }; + patches_error[p] = { accu(abs(x - rp.xyz.row(2).t())) / size(rp.vertices), p }; rp.xyz.row(2) = x.t(); } @@ -920,7 +920,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) for(auto p: patches_map[v]) mv += patches[p.first].xyz.col(p.second); - V.col(v) = mv / patches_map[v].size(); + V.col(v) = mv / size(patches_map[v]); } else { diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index bd86ea0e..cf6864a8 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -61,7 +61,7 @@ void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplev bool patch::exists(index_t idx) { - for(size_t i=1; i < vertices.size(); ++i) + for(size_t i=1; i < size(vertices); ++i) { if(vertices[i] == idx) return true; @@ -106,9 +106,9 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ if(geo[a] < geo[v] || geo[b] < geo[v] ) { if(geo[a] < geo[v]) - i = find(vertices.data(), vertices.size(), a); + i = find(vertices.data(), size(vertices), a); else - i = find(vertices.data(), vertices.size(), b); + i = find(vertices.data(), size(vertices), b); tmp_angle = acos(dot(mesh->normal_he(he), N[i])); @@ -207,7 +207,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind n.x() = T(0, 2); n.y() = T(1, 2); n.z() = T(2, 2); radio = -INFINITY; - for(index_t i=1; i < vertices.size(); ++i) + for(index_t i=1; i < size(vertices); ++i) { p = mesh->point(indexes[i]); c = mesh->point(v); // central vertices @@ -320,17 +320,17 @@ void patch::itransform() void patch::reset_xyz(che * mesh, std::vector & vpatches, const index_t & p, const fmask_t & mask) { - size_t m = vertices.size(); + size_t m = size(vertices); if(mask) { m = 0; - for(index_t i = 0; i < vertices.size(); ++i) + for(index_t i = 0; i < size(vertices); ++i) if(mask(vertices[i])) ++m; } xyz.set_size(3, m); - for(index_t j = 0, i = 0; i < vertices.size(); ++i) + for(index_t j = 0, i = 0; i < size(vertices); ++i) { if(!mask || mask(vertices[i])) { @@ -374,7 +374,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche size_t m = std::max (size(vertices), min_nv); - size_t j = vertices.size(); + size_t j = size(vertices); std::random_device rd; //Will be used to obtain a seed for the random number engine std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd() std::uniform_real_distribution<> dis(0, 1); @@ -462,11 +462,11 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, const index_t & p, const fmask_t & mask) { - size_t m = vertices.size(); + size_t m = size(vertices); if(mask) { m = 0; - for(index_t i = 0; i < vertices.size(); ++i) + for(index_t i = 0; i < size(vertices); ++i) if(mask(i)) { dist[vertices[i]] = float(p + 1) / M; @@ -528,7 +528,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl toplevel[v] = 0; vertices.push_back(v); - for(index_t i = 0; i < vertices.size(); ++i) + for(index_t i = 0; i < size(vertices); ++i) { const index_t & v = vertices[i]; if(toplevel[v] == n_toplevels) @@ -683,7 +683,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) } else { - for(index_t i = 0; i < vertices.size(); ++i) + for(index_t i = 0; i < size(vertices); ++i) { tmp = xyz.col(i)[2]; tmp = (max - min) * tmp + min; @@ -696,7 +696,7 @@ void patch::update_heights(real_t & min, real_t & max, bool flag) void patch::save_z(std::ostream & os) { index_t i; - for( i = 0; i < vertices.size()-1; ++i) + for( i = 0; i < size(vertices)-1; ++i) { os< & vpatches, avg_dist = INFINITY; std::vector distances; - for(size_t i = 0; i < vertices.size(); ++i) + for(size_t i = 0; i < size(vertices); ++i) { for(const index_t & u: mesh->link(vertices[i])) { @@ -727,7 +727,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, } } /* - for(size_t j = i+1; j < vertices.size(); ++j) // replace for 1 ring + for(size_t j = i+1; j < size(vertices); ++j) // replace for 1 ring { a_vec a = xyz.col(i); a_vec b = xyz.col(j); @@ -737,7 +737,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, } */ std::sort(distances.begin(), distances.end()); - size_t n_elem = distances.size(); + size_t n_elem = size(distances); if(size(distances)%2 ==0) { avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; @@ -751,7 +751,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, bool patch::is_covered( bool * covered) { - for(index_t i = 0; i < vertices.size(); ++i) + for(index_t i = 0; i < size(vertices); ++i) if(!covered[i]) return false; return true; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 398a298a..4fe942cb 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -284,7 +284,7 @@ mat4 che::normalize_box(const real_t & side) const ///< vcommon correspond to the first vertices of the mesh with indices to the main mesh (this) che * che::merge(const che * mesh, const std::vector & vcommon) { - const size_t & n_vcommon = vcommon.size(); + const size_t & n_vcommon = size(vcommon); const size_t & n_vnew = mesh->n_vertices - n_vcommon; che * new_mesh = new che(n_vertices + n_vnew, n_trigs + mesh->n_trigs); @@ -302,7 +302,7 @@ che * che::merge(const che * mesh, const std::vector & vcommon) index_t * tVT = new_mesh->VT + n_half_edges; for(index_t he = 0; he < mesh->n_half_edges; ++he) - tVT[he] = mesh->VT[he] < vcommon.size() ? vcommon[mesh->VT[he]] : mesh->VT[he] + n_vertices - n_vcommon; + tVT[he] = mesh->VT[he] < size(vcommon) ? vcommon[mesh->VT[he]] : mesh->VT[he] + n_vertices - n_vcommon; new_mesh->update_evt_ot_et(); new_mesh->update_eht(); @@ -469,7 +469,7 @@ void che::remove_vertices(const std::vector & vertices) gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_trigs.data(), new_trigs.size() / che::mtrig); + init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / che::mtrig); gproshan_debug(removing vertex); } @@ -528,7 +528,7 @@ void che::remove_non_manifold_vertices() gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), new_vertices.size(), new_trigs.data(), new_trigs.size() / che::mtrig); + init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / che::mtrig); gproshan_debug(removing vertex); } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 93134ba8..4d99018c 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -64,12 +64,12 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c std::vector merge_vertices[2]; vertex normal; - size_t size = border_vertices.size(); + size_t nb = size(border_vertices); index_t i = NIL, j, n_v; che * hole = nullptr; che * aux_hole; - index_t * vmap_border = new index_t[size]; + index_t * vmap_border = new index_t[nb]; index_t c = 1; real_t mean_edge = mesh->mean_edge(); @@ -82,19 +82,19 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c while(--N) { - merge_vertices.push_back(std::size(vertices) + delta_v); + merge_vertices.push_back(size(vertices) + delta_v); vertices.push_back(vb + (N / L) * (va - vb)); } }; auto add_border_vertices = [&](const index_t & i, const index_t & j, const index_t & delta_v = 0) -> index_t { - index_t end_v = j < i ? j + size : j; + index_t end_v = j < i ? j + nb : j; for(index_t v = i; v <= end_v; ++v) { - vmap_border[v % size] = vertices[c].size() + delta_v; - vertices[c].push_back(mesh->point(border_vertices[v % size])); - normal += mesh->normal(border_vertices[v % size]); + vmap_border[v % nb] = size(vertices[c]) + delta_v; + vertices[c].push_back(mesh->point(border_vertices[v % nb])); + normal += mesh->normal(border_vertices[v % nb]); } return end_v - i + 1; }; @@ -127,17 +127,17 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c if(j != p.second) { - n_v = add_border_vertices((j + 1) % size, p.second, hole->n_vertices - merge_vertices[!c].size()); + n_v = add_border_vertices((j + 1) % nb, p.second, hole->n_vertices - size(merge_vertices[!c])); merge_vertices[c].push_back(hole->n_vertices + n_v - 1); } else merge_vertices[c].push_back(merge_vertices[!c].back()); - gen_vertices(merge_vertices[c], vertices[c], mesh->point(border_vertices[p.second]), mesh->point(border_vertices[p.first]), hole->n_vertices - merge_vertices[!c].size()); + gen_vertices(merge_vertices[c], vertices[c], mesh->point(border_vertices[p.second]), mesh->point(border_vertices[p.first]), hole->n_vertices - size(merge_vertices[!c])); if(i != p.first) { - merge_vertices[c].push_back(std::size(vertices[c]) + hole->n_vertices - merge_vertices[!c].size()); - n_v += add_border_vertices(p.first, i > 0 ? i - 1 : size - 1, hole->n_vertices - merge_vertices[!c].size()); + merge_vertices[c].push_back(std::size(vertices[c]) + hole->n_vertices - size(merge_vertices[!c])); + n_v += add_border_vertices(p.first, i > 0 ? i - 1 : nb - 1, hole->n_vertices - size(merge_vertices[!c])); } else merge_vertices[c].push_back(merge_vertices[!c].front()); @@ -166,7 +166,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c normal += mesh->normal(b); vertices[c].push_back(mesh->point(b)); } - normal /= vertices[c].size(); + normal /= size(vertices[c]); hole = fill_hole_front_angles(vertices[c], mesh->mean_edge(), normal, max_iter); } @@ -177,18 +177,18 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c for(index_t v: merge_vertices[!c]) vertices[c].push_back(hole->point(v)); - i = i > 0 ? i - 1 : size - 1; - j = (j + 1) % size; + i = i > 0 ? i - 1 : nb - 1; + j = (j + 1) % nb; normal = 0; - n_v = add_border_vertices(j, i, hole->n_vertices - merge_vertices[!c].size()); + n_v = add_border_vertices(j, i, hole->n_vertices - size(merge_vertices[!c])); normal /= n_v; aux_hole = nullptr; aux_hole = fill_hole_front_angles(vertices[c], mesh->mean_edge(), normal, max_iter); hole->merge(aux_hole, merge_vertices[!c]); - hole->set_head_vertices(vmap_border, size); + hole->set_head_vertices(vmap_border, nb); delete aux_hole; delete [] vmap_border; @@ -207,7 +207,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c void split_border(std::vector > & , che * mesh, const std::vector & border_vertices) { - size_t n = border_vertices.size(); + size_t n = size(border_vertices); a_mat data(3, n); a_mat means; @@ -282,7 +282,7 @@ std::tuple *, che **> fill_all_holes_meshes(che * mesh, con che ** holes = nullptr; std::vector bounds = mesh->bounds(); - const size_t n_borders = bounds.size(); + const size_t n_borders = size(bounds); if(!n_borders) return make_tuple(border_vertices, holes); @@ -342,7 +342,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti std::vector tmp_normals(size(vertices)); vertex normal; - for(index_t v = 0; v < vertices.size(); ++v) + for(index_t v = 0; v < size(vertices); ++v) { normal = mesh->normal(front_vertices[v]); if(is_grow) normal = -normal; @@ -364,7 +364,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti init_perimeter += norm(vertices.back() - vertices.front()); perimeter = init_perimeter; -// length = perimeter / vertices.size(); +// length = perimeter / size(vertices); bool o = is_grow; @@ -372,10 +372,10 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti std::vector > neighbors(size(vertices)); index_t v, p_v, n_v; - for(v = 0; v < vertices.size(); ++v) + for(v = 0; v < size(vertices); ++v) { - n_v = (v + 1) % vertices.size(); - p_v = v > 0 ? v - 1: vertices.size() - 1; + n_v = (v + 1) % size(vertices); + p_v = v > 0 ? v - 1: size(vertices) - 1; is_border[v] = true; neighbors[v][!o] = p_v; @@ -453,7 +453,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti } else if(top.theta <= a135) { - index_t m_v = tmp_vertices.size(); + index_t m_v = size(tmp_vertices); m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); @@ -491,7 +491,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti } else if(top.theta <= M_PI) { - index_t m_v = tmp_vertices.size(); + index_t m_v = size(tmp_vertices); m_vec = top.new_vertex(tmp_vertices, 1./3, length, neighbors[v], o, tmp_normals[v]); tmp_vertices.push_back(m_vec); @@ -554,14 +554,14 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti for(a_vec r: tmp_vertices) vertices.push_back({r[0], r[1], r[2]}); - for(index_t v = 0; false && v < tmp_vertices.size(); ++v) + for(index_t v = 0; false && v < size(tmp_vertices); ++v) a_vec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); gproshan_debug_var(perimeter); // gproshan_debug(filling holes); // gproshan_debug_var(size(vertices)); // gproshan_debug_var(size(trigs)); - return trigs.size() == 0 ? nullptr : new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); + return size(trigs) == 0 ? nullptr : new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); } che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) @@ -575,8 +575,8 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng // PCA -------------------------------------------------------------------------- - a_mat V(3, vertices.size()); - for(index_t v = 0; v < vertices.size(); ++v) + a_mat V(3, size(vertices)); + for(index_t v = 0; v < size(vertices); ++v) { V(0,v) = vertices[v][0]; V(1,v) = vertices[v][1]; @@ -629,7 +629,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng std::vector > neighbors(size(vertices)); index_t v, p_v, n_v; - for(v = 0; v < vertices.size(); ++v) + for(v = 0; v < size(vertices); ++v) tmp_vertices[v] = V.col(v); auto push_front = [&front](const border_t & b) @@ -637,10 +637,10 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng if(b.theta <= M_PI) front.push(b); }; - for(v = 0; v < vertices.size(); ++v) + for(v = 0; v < size(vertices); ++v) { - n_v = (v + 1) % vertices.size(); - p_v = v > 0 ? v - 1: vertices.size() - 1; + n_v = (v + 1) % size(vertices); + p_v = v > 0 ? v - 1: size(vertices) - 1; is_border[v] = true; neighbors[v][!o] = p_v; @@ -721,7 +721,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng } else if(top.theta <= a135) { - index_t m_v = tmp_vertices.size(); + index_t m_v = size(tmp_vertices); m_vec = top.new_vertex(tmp_vertices, 0.5, length, neighbors[v], o); tmp_vertices.push_back(m_vec); @@ -757,7 +757,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng } else if(top.theta <= M_PI) { - index_t m_v = tmp_vertices.size(); + index_t m_v = size(tmp_vertices); m_vec = top.new_vertex(tmp_vertices, 1./3, length, neighbors[v], o); tmp_vertices.push_back(m_vec); @@ -821,19 +821,19 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t & leng vertices.push_back({r[0], r[1], r[2]}); } - return trigs.size() ? new che(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3) : nullptr; + return size(trigs) ? new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3) : nullptr; } void get_real_tri(che * mesh, std::vector & select_vertices, std::vector & triangle, std::vector & tri_sizes ) { // Drawing a triangle in the middle of the border - size_t div = select_vertices.size() / 3; - size_t r = select_vertices.size() % 3; + size_t div = size(select_vertices) / 3; + size_t r = size(select_vertices) % 3; //Defining the ranges size_t b = div; size_t c = 2 * div; - size_t d = select_vertices.size(); + size_t d = size(select_vertices); vertex tri[3]; @@ -861,7 +861,7 @@ void get_real_tri(che * mesh, std::vector & select_vertices, std::vecto real_t weight = 1.8; - real_t wp = weight / select_vertices.size(); + real_t wp = weight / size(select_vertices); real_t aux = wp * tri_sizes[0]; real_t wo = (1 - aux) / ( tri_sizes[1] + tri_sizes[2] ); @@ -880,8 +880,8 @@ void get_real_tri(che * mesh, std::vector & select_vertices, std::vecto che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t ) { - size_t n_vertices = select_vertices.size() + 3; - size_t n_trigs = select_vertices.size() + 4; + size_t n_vertices = size(select_vertices) + 3; + size_t n_trigs = size(select_vertices) + 4; vertex * vertices = new vertex[n_vertices]; index_t * trigs = new index_t[n_trigs * che::mtrig]; @@ -899,7 +899,7 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic vertices[i++] = triangle[1]; vertices[i++] = triangle[2]; - size_t tri_init = select_vertices.size(); + size_t tri_init = size(select_vertices); index_t f = 0; i = 0; @@ -919,7 +919,7 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic } ++i; - for( ; i < select_vertices.size() - 1; ++i) + for( ; i < size(select_vertices) - 1; ++i) { trigs[f++] = i; trigs[f++] = tri_init + 2; @@ -946,7 +946,7 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic trigs[f++] = tri_init + 2; trigs[f++] = aux_i; - aux_i = select_vertices.size(); + aux_i = size(select_vertices); trigs[f++] = aux_i - 1; trigs[f++] = tri_init + 2; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index f6c29a96..c525cb2c 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -17,12 +17,12 @@ che_obj::che_obj(const std::string & file) void che_obj::read_file(const std::string & file) { parser p(file); - alloc(size(p.vertices), p.trigs.size() / che::mtrig); - memcpy(GT, p.vertices.data(), p.vertices.size() * sizeof(vertex)); - memcpy(VC, p.vcolors.data(), p.vcolors.size() * sizeof(rgb_t)); + alloc(size(p.vertices), size(p.trigs) / che::mtrig); + memcpy(GT, p.vertices.data(), size(p.vertices) * sizeof(vertex)); + memcpy(VC, p.vcolors.data(), size(p.vcolors) * sizeof(rgb_t)); #pragma omp parallel for - for(index_t i = 0; i < p.trigs.size(); ++i) + for(index_t i = 0; i < size(p.trigs); ++i) VT[i] = p.trigs[i].x(); } @@ -116,13 +116,13 @@ che_obj::parser::parser(const std::string & file) } switch(i) { - case 0: f[i] += f[i] > vertices.size() ? vertices.size() : -1; break; - case 1: f[i] += f[i] > vtexcoords.size() ? vtexcoords.size() : -1; break; - case 2: f[i] += f[i] > vnormals.size() ? vnormals.size() : -1; break; + case 0: f[i] += f[i] > size(vertices) ? size(vertices) : -1; break; + case 1: f[i] += f[i] > size(vtexcoords) ? size(vtexcoords) : -1; break; + case 2: f[i] += f[i] > size(vnormals) ? size(vnormals) : -1; break; } } - for(index_t i = 2; i < P.size(); ++i) + for(index_t i = 2; i < size(P); ++i) { trigs.push_back(P[0]); trigs.push_back(P[i - 1]); @@ -153,7 +153,7 @@ che_obj::parser::parser(const std::string & file) case 'u': // usemtl { sscanf(line, "%*s %s", str); - objects.emplace_back(str, trigs.size()); + objects.emplace_back(str, size(trigs)); break; } case 'm': // mtllib @@ -167,7 +167,7 @@ che_obj::parser::parser(const std::string & file) fclose(fp); - objects.emplace_back("", trigs.size()); + objects.emplace_back("", size(trigs)); gproshan_log_var(size(vertices)); gproshan_log_var(size(vnormals)); diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 4c1d6210..10c884a3 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -71,14 +71,14 @@ void che_off::read_file(const std::string & file) rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, trigs.size() / che::mtrig); + alloc(nv, size(trigs) / che::mtrig); GT = tGT; VN = tVN; VC = tVC; } - memcpy(VT, trigs.data(), trigs.size() * sizeof(index_t)); + memcpy(VT, trigs.data(), size(trigs) * sizeof(index_t)); } void che_off::write_file(const che * mesh, const std::string & file, const che_off::type & off, const bool & pointcloud) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index b38e8849..285fbe24 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -243,13 +243,13 @@ void che_ply::read_file(const std::string & file) rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, trigs.size() / che::mtrig); + alloc(nv, size(trigs) / che::mtrig); GT = tGT; VC = tVC; } - memcpy(VT, trigs.data(), trigs.size() * sizeof(index_t)); + memcpy(VT, trigs.data(), size(trigs) * sizeof(index_t)); } void che_ply::write_file(const che * mesh, const std::string & file, const bool & color) diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index ebf28f0b..a480109d 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -116,7 +116,7 @@ void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t delete [] rings; delete [] sorted; - a_mat P(3, sub_mesh_hole.size()); + a_mat P(3, size(sub_mesh_hole)); index_t i = 0; for(index_t & b: sub_mesh_hole) { diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index c742b36d..1a00ff73 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -77,7 +77,7 @@ che_sphere::che_sphere(const real_t & r, const size_t & n) trigs.push_back(cols - 1); trigs.push_back(v); - init(vertices.data(), vertices.size(), trigs.data(), trigs.size() / 3); + init(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); } diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 786ac3df..9c7986d9 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -30,7 +30,7 @@ grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): p gproshan_log_var(build_time); gproshan_log_var(res); gproshan_log_var(size(voxels)); - gproshan_log_var(double(n_points) / voxels.size()); + gproshan_log_var(double(n_points) / size(voxels)); } std::vector grid::operator () (const point & p, int k) const @@ -84,8 +84,8 @@ k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, cons gproshan_log_var(time_build); TIC(time_query); - const point * q = query.size() ? query.data() : pc; - const size_t & n_results = query.size() ? query.size() : n_points; + const point * q = size(query) ? query.data() : pc; + const size_t & n_results = size(query) ? size(query) : n_points; flann::Matrix mq((real_t *) q, n_results, 3); diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 2d87f75c..d55fbf22 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -125,7 +125,7 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) { g_meshes.resize(size(meshes)); - for(index_t i = 0; i < meshes.size(); ++i) + for(index_t i = 0; i < size(meshes); ++i) { g_meshes[i] = new CHE(meshes[i]); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index b2b87478..9c479de8 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -111,7 +111,7 @@ optix::~optix() cudaFree(hitgroup_records_buffer); cudaFree(as_buffer); - for(index_t i = 0; i < dd_mesh.size(); ++i) + for(index_t i = 0; i < size(dd_mesh); ++i) cuda_free_CHE(dd_mesh[i], d_mesh[i]); cudaFree(optix_params.sc.materials); @@ -323,7 +323,7 @@ void optix::build_sbt() std::vector hitgroup_records; - for(index_t i = 0; i < d_mesh.size(); ++i) + for(index_t i = 0; i < size(d_mesh); ++i) for(index_t r = 0; r < 2; ++r) { HitgroupRecord rec; @@ -332,11 +332,11 @@ void optix::build_sbt() hitgroup_records.push_back(rec); } - cudaMalloc(&hitgroup_records_buffer, hitgroup_records.size() * sizeof(HitgroupRecord)); - cudaMemcpy(hitgroup_records_buffer, hitgroup_records.data(), hitgroup_records.size() * sizeof(HitgroupRecord), cudaMemcpyHostToDevice); + cudaMalloc(&hitgroup_records_buffer, size(hitgroup_records) * sizeof(HitgroupRecord)); + cudaMemcpy(hitgroup_records_buffer, hitgroup_records.data(), size(hitgroup_records) * sizeof(HitgroupRecord), cudaMemcpyHostToDevice); sbt.hitgroupRecordBase = (CUdeviceptr) hitgroup_records_buffer; sbt.hitgroupRecordStrideInBytes = sizeof(HitgroupRecord); - sbt.hitgroupRecordCount = hitgroup_records.size(); + sbt.hitgroupRecordCount = size(hitgroup_records); } OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) @@ -347,7 +347,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector optix_vertex_ptr(size(meshes)); std::vector optix_trig_flags(size(meshes)); - for(index_t i = 0; i < meshes.size(); ++i) + for(index_t i = 0; i < size(meshes); ++i) add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i], model_mats[i]); OptixAccelBuildOptions optix_accel_opt = {}; @@ -471,8 +471,8 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u delete [] h_m.VT; scene * sc = (scene *) mesh; - cudaMalloc(&optix_params.sc.materials, sc->materials.size() * sizeof(scene::material)); - cudaMalloc(&optix_params.sc.textures, sc->textures.size() * sizeof(scene::texture)); + cudaMalloc(&optix_params.sc.materials, size(sc->materials) * sizeof(scene::material)); + cudaMalloc(&optix_params.sc.textures, size(sc->textures) * sizeof(scene::texture)); cudaMalloc(&optix_params.sc.trig_mat, mesh->n_vertices / 3 * sizeof(index_t)); cudaMalloc(&optix_params.sc.texcoords, mesh->n_vertices * sizeof(vec2)); @@ -486,8 +486,8 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u } gproshan_error_var(size(textures)); - cudaMemcpy(optix_params.sc.materials, sc->materials.data(), sc->materials.size() * sizeof(scene::material), cudaMemcpyHostToDevice); - cudaMemcpy(optix_params.sc.textures, textures.data(), textures.size() * sizeof(scene::texture), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.materials, sc->materials.data(), size(sc->materials) * sizeof(scene::material), cudaMemcpyHostToDevice); + cudaMemcpy(optix_params.sc.textures, textures.data(), size(textures) * sizeof(scene::texture), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); cudaMemcpy(optix_params.sc.texcoords, sc->texcoords, mesh->n_vertices * sizeof(vec2), cudaMemcpyHostToDevice); } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index c33ed161..31d5d54e 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -27,7 +27,7 @@ scene::~scene() bool scene::is_scene() const { - return load_scene && objects.size() > 1; + return load_scene && size(objects) > 1; } bool scene::is_pointcloud() const @@ -88,7 +88,7 @@ bool scene::load_obj(const std::string & file) memset(trig_mat, -1, sizeof(index_t) * n_vertices / 3); #pragma omp parallel for - for(index_t i = 0; i < objects.size() - 1; ++i) + for(index_t i = 0; i < size(objects) - 1; ++i) { const object & obj = objects[i]; const index_t & n = objects[i + 1].begin; @@ -118,7 +118,7 @@ bool scene::load_mtl(const std::string & file) case 'n': // newmtl { sscanf(line, "%*s %s", str); - material_id[str] = materials.size(); + material_id[str] = size(materials); material_name.push_back(str); materials.emplace_back(); break; @@ -167,7 +167,7 @@ bool scene::load_mtl(const std::string & file) if(str[0] == '-') continue; // ignoring map textures with options if(texture_id.find(str) == texture_id.end()) { - texture_id[str] = texture_name.size(); + texture_id[str] = size(texture_name); texture_name.push_back(str); } m = texture_id[str]; @@ -189,7 +189,7 @@ bool scene::load_mtl(const std::string & file) } /* - for(index_t i = 0; i < materials.size(); ++i) + for(index_t i = 0; i < size(materials); ++i) { const material & m = materials[i]; gproshan_log_var(material_name[i]); diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 2d736bc4..272e6738 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -24,7 +24,7 @@ size_t partitions::size(const index_t & i) const partitions::part partitions::operator () (const index_t & i) const { - assert(i > 0 && i < splits.size()); + assert(i > 0 && i < size(splits)); return {splits[i], splits[i + 1], sorted}; } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 64bab098..702e945f 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -152,7 +152,7 @@ void che_viewer::update_vbo_heatmap(const real_t * vheatmap) void che_viewer::update_instances_positions(const std::vector & translations) { - n_instances = translations.size(); + n_instances = size(translations); if(!n_instances) return; glBindVertexArray(vao); @@ -217,7 +217,7 @@ void che_viewer::draw_pointcloud(shader & program) void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) { - if(size(selected_xyz) != selected.size()) + if(size(selected_xyz) != size(selected)) { selected_xyz.clear(); selected_xyz.reserve(size(selected)); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 90dea83a..ce0b4156 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -9,10 +9,10 @@ static const int gltex_nums[] = {GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2, GL_TEXTU scene_viewer::scene_viewer(scene * p_sc): che_viewer(p_sc), sc(p_sc) { - gltextures = new GLuint[sc->textures.size()]; + gltextures = new GLuint[size(sc->textures)]; glGenTextures(size(sc->textures), gltextures); - for(index_t i = 0; i < sc->textures.size(); ++i) + for(index_t i = 0; i < size(sc->textures); ++i) init_texture(gltextures[i], sc->textures[i]); glGenBuffers(1, &tex_vbo); @@ -61,7 +61,7 @@ void scene_viewer::draw(shader & program) } else { - for(index_t i = 0; i < sc->objects.size() - 1; ++i) + for(index_t i = 0; i < size(sc->objects) - 1; ++i) { const scene::object & obj = sc->objects[i]; const scene::material & mat = sc->materials[obj.material_id]; @@ -98,7 +98,7 @@ void scene_viewer::draw_pointcloud(shader & program) } else { - for(index_t i = 0; i < sc->objects.size() - 1; ++i) + for(index_t i = 0; i < size(sc->objects) - 1; ++i) { const scene::object & obj = sc->objects[i]; const scene::material & mat = sc->materials[obj.material_id]; diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index e79e9d7e..86d1f121 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -110,9 +110,9 @@ bool shader::load(GLenum shader_type, const std::string & filename) GLuint shader = glCreateShader(shader_type); const char * source_c_str = source.c_str(); - int size = source.size(); + int str_size = size(source); - glShaderSource(shader, 1, &(source_c_str), &size); + glShaderSource(shader, 1, &(source_c_str), &str_size); glCompileShader(shader); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 55605e28..903d3b32 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -58,7 +58,7 @@ const std::vector viewer::colormap = { "vertex color", "material scene", }; -const size_t viewer::max_meshes = m_window_split.size() - 1; +const size_t viewer::max_meshes = size(m_window_split) - 1; che_sphere viewer::sphere_data{0.01}; @@ -167,7 +167,7 @@ void viewer::imgui() { if(ImGui::BeginMenu("Select")) { - for(index_t i = 0; i < meshes.size(); ++i) + for(index_t i = 0; i < size(meshes); ++i) { const che_viewer & m = *meshes[i]; if(ImGui::MenuItem((std::to_string(i) + ": " + m->filename).c_str(), nullptr, i == idx_selected_mesh, i != idx_selected_mesh)) @@ -182,7 +182,7 @@ void viewer::imgui() if(ImGui::BeginMenu("Color")) { - for(index_t i = 0; i < colormap.size(); ++i) + for(index_t i = 0; i < size(colormap); ++i) { if(ImGui::MenuItem(colormap[i].c_str(), nullptr, i == mesh.idx_colormap, i != mesh.idx_colormap)) check_apply_all_meshes([&](che_viewer & mesh) @@ -194,7 +194,7 @@ void viewer::imgui() ImGui::EndMenu(); } - for(index_t i = 0; i < menus.size(); ++i) + for(index_t i = 0; i < size(menus); ++i) { if(ImGui::BeginMenu(menus[i].c_str())) { @@ -344,7 +344,7 @@ void viewer::imgui() { if(ImGui::CollapsingHeader("Removed meshes", ImGuiTreeNodeFlags_DefaultOpen)) { - for(index_t i = 0; i < removed_meshes.size(); ++i) + for(index_t i = 0; i < size(removed_meshes); ++i) { che_viewer * m = removed_meshes[i]; ImGui::PushID(m); @@ -524,7 +524,7 @@ void viewer::add_menu(const std::string & str, const std::vector & vprocess menus.push_back(str); menu_processes.emplace_back(vprocesses); - const int & id_menu = menus.size() - 1; + const int & id_menu = size(menus) - 1; for(const int & p: menu_processes.back()) processes[p].id_menu = id_menu; } @@ -557,7 +557,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) che_viewer & mesh = *meshes.back(); mesh.log_info(); - idx_selected_mesh = meshes.size() - 1; + idx_selected_mesh = size(meshes) - 1; glfwSetWindowTitle(window, ("gproshan - " + mesh->filename).c_str()); update_viewport_meshes(); @@ -575,7 +575,7 @@ bool viewer::remove_mesh(const index_t & idx) removed_meshes.push_back(meshes[idx]); meshes.erase(begin(meshes) + idx); - if(idx_selected_mesh == meshes.size()) + if(idx_selected_mesh == size(meshes)) --idx_selected_mesh; update_viewport_meshes(); @@ -591,7 +591,7 @@ bool viewer::pop_mesh() removed_meshes.push_back(meshes.back()); meshes.pop_back(); - if(idx_selected_mesh == meshes.size()) + if(idx_selected_mesh == size(meshes)) --idx_selected_mesh; update_viewport_meshes(); @@ -601,9 +601,9 @@ bool viewer::pop_mesh() void viewer::update_viewport_meshes() { - const int & rows = m_window_split[meshes.size()].x(); - const int & cols = m_window_split[meshes.size()].y(); - for(index_t m = 0; m < meshes.size(); ++m) + const int & rows = m_window_split[size(meshes)].x(); + const int & cols = m_window_split[size(meshes)].y(); + for(index_t m = 0; m < size(meshes); ++m) { meshes[m]->vx = m % cols; meshes[m]->vy = rows - (m / cols) - 1; @@ -654,8 +654,8 @@ void viewer::save_frametime(const std::string & file) void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int height) { viewer * view = (viewer *) glfwGetWindowUserPointer(window); - view->viewport_width = width / m_window_split[view->meshes.size()].y(); - view->viewport_height = height / m_window_split[view->meshes.size()].x(); + view->viewport_width = width / m_window_split[size(view->meshes)].y(); + view->viewport_height = height / m_window_split[size(view->meshes)].x(); view->cam.aspect = real_t(view->viewport_width) / view->viewport_height; view->proj_mat = view->cam.perspective(); } @@ -698,9 +698,9 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod const index_t & ix = xpos * xscale; const index_t & iy = ypos * yscale; - const int & cols = m_window_split[view->meshes.size()].y(); + const int & cols = m_window_split[size(view->meshes)].y(); const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; - if(idx_mesh < view->meshes.size()) + if(idx_mesh < size(view->meshes)) view->idx_selected_mesh = idx_mesh; if(mods == GLFW_MOD_SHIFT && button == GLFW_MOUSE_BUTTON_LEFT) @@ -806,7 +806,7 @@ bool viewer::m_save_load_view(viewer * view) if(ImGui::BeginCombo("##loadfile", vfiles[select].c_str())) { - for(index_t i = 0; i < vfiles.size(); ++i) + for(index_t i = 0; i < size(vfiles); ++i) { if(ImGui::Selectable(vfiles[i].c_str(), select == i)) select = i; @@ -1211,7 +1211,7 @@ void viewer::render_gl() shader_sphere.uniform("scale", cam.zoom()); - for(index_t i = 0; i < meshes.size(); ++i) + for(index_t i = 0; i < size(meshes); ++i) { che_viewer & mesh = *meshes[i]; From b34005e7d67e7ca997899ec18eebe811d689a3b5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 16:40:58 +0100 Subject: [PATCH 0903/1018] std: using size(container) instead of .size() --- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 +- src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu | 2 +- src/gproshan/mesh/che.cpp | 2 +- src/gproshan/raytracing/optix.cpp | 8 ++++---- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index 3742e3e0..be510939 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -130,7 +130,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector cudaEventSynchronize(stop); cudaEventElapsedTime(&time_fps, start, stop); - times[samples.size()] = time_fps / 1000; + times[size(samples)] = time_fps / 1000; if(radio > 0 || !n) cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index 76f00f69..f04a3c0c 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -179,7 +179,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect cudaEventSynchronize(stop); cudaEventElapsedTime(&time_fps, start, stop); - times[samples.size()] = time_fps / 1000; + times[size(samples)] = time_fps / 1000; if(radio > 0 || !n) cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 4fe942cb..bcf1eb0d 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -780,7 +780,7 @@ size_t che::memory() const return sizeof(*this) + n_vertices * (2 * sizeof(vertex) + sizeof(index_t) + sizeof(real_t) + sizeof(rgb_t)) + sizeof(index_t) * (3 * n_half_edges + n_edges) + - filename.size(); + size(filename); } size_t che::max_degree() const diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 9c479de8..d185463b 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -76,7 +76,7 @@ optix::optix(const std::string & ptx) &optix_module_compile_opt, &optix_pipeline_compile_opt, str_ptx_code.c_str(), - str_ptx_code.size(), + size(str_ptx_code), nullptr, nullptr, // log message &optix_module ); @@ -280,7 +280,7 @@ void optix::create_pipeline() &optix_pipeline_compile_opt, &optix_pipeline_link_opt, program_groups.data(), - program_groups.size(), + size(program_groups), log, &sizeof_log, &optix_pipeline ); @@ -359,7 +359,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const optixAccelComputeMemoryUsage( optix_context, &optix_accel_opt, optix_meshes.data(), - optix_meshes.size(), + size(optix_meshes), &optix_gas_buffer_size ); @@ -382,7 +382,7 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const 0, // stream &optix_accel_opt, optix_meshes.data(), - optix_meshes.size(), + size(optix_meshes), (CUdeviceptr) d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes, (CUdeviceptr) d_output_buffer, From 181d17cd073f2cb8c13a119abc25e7bdc447903f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 17:50:17 +0100 Subject: [PATCH 0904/1018] std: fix bug in action build Debug --- src/gproshan/util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 272e6738..9dc2b770 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -24,7 +24,7 @@ size_t partitions::size(const index_t & i) const partitions::part partitions::operator () (const index_t & i) const { - assert(i > 0 && i < size(splits)); + assert(i > 0 && i < std::size(splits)); return {splits[i], splits[i + 1], sorted}; } From 6ec3ac2cc32198fc3e31ab78044ee6730ce94eba Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 27 Nov 2023 18:05:08 +0100 Subject: [PATCH 0905/1018] viewer: add title arg constructor VM_CLASS --- include/gproshan/app_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 4 ++-- src/gproshan/app_viewer.cpp | 2 ++ src/gproshan/viewer/viewer.cpp | 8 ++++---- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 1fe594f3..c4555beb 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -49,7 +49,7 @@ class app_viewer : public viewer double time; public: - app_viewer() = default; + app_viewer(const char * title = "gproshan", const int & width = 1600, const int & height = 900); virtual ~app_viewer(); int main(int nargs, const char ** args); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 54830c08..130eb926 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -104,7 +104,7 @@ class viewer char status_message[1024] = {}; public: - viewer(const int & width = 1600, const int & height = 900); + viewer(const char * title = "gproshan", const int & width = 1600, const int & height = 900); virtual ~viewer(); che_viewer & selected_mesh(); @@ -120,7 +120,7 @@ class viewer virtual bool run(); void info_gl(); - void init_gl(); + void init_gl(const char * title); void init_imgui(); void init_menus(); void init_glsl(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 32d0db2d..27ebfb70 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -14,6 +14,8 @@ using namespace gproshan::mdict; namespace gproshan { +app_viewer::app_viewer(const char * title, const int & width, const int & height): viewer(title, width, height) {} + app_viewer::~app_viewer() { for(auto & m: meshes) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 903d3b32..a6a66d5a 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -62,12 +62,12 @@ const size_t viewer::max_meshes = size(m_window_split) - 1; che_sphere viewer::sphere_data{0.01}; -viewer::viewer(const int & width, const int & height) +viewer::viewer(const char * title, const int & width, const int & height) { window_width = width; window_height = height; - init_gl(); + init_gl(title); init_glsl(); init_imgui(); init_menus(); @@ -398,7 +398,7 @@ void error_callback(int error, const char* description) fprintf(stderr, "Error %d: %s\n", error, description); } -void viewer::init_gl() +void viewer::init_gl(const char * title) { glfwSetErrorCallback(error_callback); @@ -413,7 +413,7 @@ void viewer::init_gl() gproshan_log_var(window_width); gproshan_log_var(window_height); - window = glfwCreateWindow(window_width, window_height, "gproshan", NULL, NULL); + window = glfwCreateWindow(window_width, window_height, title, NULL, NULL); glfwSetWindowUserPointer(window, this); From 01365b7fa72269fd6ecd695a7f5daed590eb0363 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Nov 2023 13:48:34 +0100 Subject: [PATCH 0906/1018] gproshan now using c++20 --- CMakeLists.txt | 2 +- src/gproshan/mdict/mdict.cpp | 4 ++-- src/gproshan/mdict/msparse_coding.cpp | 2 +- src/gproshan/mdict/patch.cpp | 6 +++--- src/gproshan/mesh/che_fill_hole.cpp | 4 ++-- src/gproshan/pointcloud/knn.cpp | 2 +- src/gproshan/scenes/scene.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bc0f6fa..bff76e87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options(-Wall -Wextra -Wno-unused-result) diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 39ff79a6..d8110a6e 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -77,7 +77,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { a_sp_mat alpha = OMP_all(locval, X, D, L); - std::sort(locval.begin(), locval.end()); + std::sort(begin(locval), end(locval)); rows.push_back(0); for(index_t k = 1; k < size(locval); ++k) @@ -360,7 +360,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si { a_sp_mat alpha = OMP_all(locval, patches, A, L); - std::sort(locval.begin(), locval.end()); + std::sort(begin(locval), end(locval)); rows.push_back(0); for(index_t k = 1; k < size(locval); ++k) diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index fc3b85f6..32f1ef40 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -891,7 +891,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) rp.xyz.row(2) = x.t(); } - std::sort(patches_error.begin(), patches_error.end()); + std::sort(begin(patches_error), end(patches_error)); fprintf(stderr, "error %16s%16s\n", "best", "worst"); for(index_t i = 0; i < 10; ++i) diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index cf6864a8..27be2006 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -414,7 +414,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche vpatches_t & ma = vpatches[mesh->halfedge(he_next(he))]; vpatches_t & mb = vpatches[mesh->halfedge(he_prev(he))]; - if(ma.find(p) != ma.end() && mb.find(p) != mb.end()) + if(ma.contains(p) && mb.contains(p)) { arma::uvec xi = { vpatches[min_v][p], ma[p], mb[p] }; abc = xyz.cols(xi); @@ -600,7 +600,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) My_Monge_form monge_form; My_Monge_via_jet_fitting monge_fit; - monge_form = monge_fit(in_points.begin(), in_points.end(), d_fitting, d_monge); + monge_form = monge_fit(begin(in_points), end(in_points), d_fitting, d_monge); vertex normal = mesh->normal(v); monge_form.comply_wrt_given_normal(DVector(normal.x(), normal.y(), normal.z())); @@ -736,7 +736,7 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, distances.push_back(norm(a - b)); } */ - std::sort(distances.begin(), distances.end()); + std::sort(begin(distances), end(distances)); size_t n_elem = size(distances); if(size(distances)%2 ==0) { diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 4d99018c..1fc5497d 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -118,7 +118,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c } else // fill rest partial holes { - reverse(merge_vertices[!c].begin(), merge_vertices[!c].end()); + reverse(begin(merge_vertices[!c]), end(merge_vertices[!c])); for(index_t v: merge_vertices[!c]) vertices[c].push_back(hole->point(v)); @@ -172,7 +172,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c } else { - reverse(merge_vertices[!c].begin(), merge_vertices[!c].end()); + reverse(begin(merge_vertices[!c]), end(merge_vertices[!c])); for(index_t v: merge_vertices[!c]) vertices[c].push_back(hole->point(v)); diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 9c7986d9..6a35d8d9 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -49,7 +49,7 @@ std::vector grid::operator () (const point & p, int k) const continue; const auto & iter = voxels.find(pos); - if(iter == voxels.end()) + if(iter == end(voxels)) continue; for(const index_t & v: iter->second) diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 31d5d54e..f64806a9 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -165,7 +165,7 @@ bool scene::load_mtl(const std::string & file) : materials.back().map_bump; sscanf(line, "%*s %s", str); if(str[0] == '-') continue; // ignoring map textures with options - if(texture_id.find(str) == texture_id.end()) + if(!texture_id.contains(str)) { texture_id[str] = size(texture_name); texture_name.push_back(str); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index a6a66d5a..380b19d2 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -534,7 +534,7 @@ int viewer::add_process(const char * name, const function_t & f, const int & key static int nk = 1000; const int & fkey = key == -1 ? ++nk : key; - if(processes.find(fkey) != processes.end()) + if(processes.contains(fkey)) { fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, glfw_key_name.at(key), name); return -1; @@ -674,7 +674,7 @@ void viewer::keyboard_callback(GLFWwindow * window, int key, int, int action, in if(action == GLFW_RELEASE) return; viewer * view = (viewer *) glfwGetWindowUserPointer(window); - if(view->processes.find(key) == view->processes.end()) + if(!view->processes.contains(key)) return; process_t & pro = view->processes[key]; From 1d48a0ef150065bd83da2703b4a08775e1ddaf70 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 28 Nov 2023 14:52:49 +0100 Subject: [PATCH 0907/1018] geometry: using ivec2 for 2d convex hull --- include/gproshan/geometry/convex_hull.h | 10 ++++---- include/gproshan/geometry/vec.h | 8 +++++++ src/gproshan/geometry/convex_hull.cpp | 31 +++++++++++++++---------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index eb7291e4..a7e2abcb 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -20,13 +20,13 @@ class convex_hull std::vector CH; ///< convex hull points clockwise public: - convex_hull(const std::vector & points); - convex_hull(const vertex * points, const size_t & n_points); - operator const std::vector & (); + convex_hull(const std::vector & points, const real_t & precision = 1000); + convex_hull(const vertex * points, const size_t & n_points, const real_t & precision = 1000); + operator const std::vector & () const; private: - void andrew_algorithm(const vertex * points, const size_t & n_points); - bool ccw(const vertex & p, const vertex & q, const vertex & r); + void andrew_algorithm(const std::vector & points); + bool ccw(const ivec2 & p, const ivec2 & q, const ivec2 & r); }; diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 922f742c..acf42820 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -274,6 +274,14 @@ vec operator / (const T & a, const vec & v) return res; } +///< cross product +template +__host_device__ +T cross(const vec & u, const vec & v) +{ + return u.x() * v.y() - v.x() * u.y(); +} + ///< cross product template __host_device__ diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 37b9e9aa..9cd567f1 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -8,22 +8,30 @@ namespace gproshan { -convex_hull::convex_hull(const std::vector & points): convex_hull(points.data(), size(points)) {} +convex_hull::convex_hull(const std::vector & points, const real_t & precision): convex_hull(points.data(), size(points), precision) {} -convex_hull::convex_hull(const vertex * points, const size_t & n_points) +convex_hull::convex_hull(const vertex * points, const size_t & n_points, const real_t & precision) { - andrew_algorithm(points, n_points); + std::vector points2d(n_points); + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + const vertex & p = points[i] * precision; + points2d[i] = {int(p.x()), int(p.y())}; + } + andrew_algorithm(points2d); } -convex_hull::operator const std::vector & () +convex_hull::operator const std::vector & () const { return CH; } ///< Andrew's Convex Hull Algorithm: Competitive Programming 4 -void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_points) +void convex_hull::andrew_algorithm(const std::vector & points) { - std::vector idx(n_points); + std::vector idx(size(points)); std::iota(begin(idx), end(idx), 0); std::sort(begin(idx), end(idx), @@ -32,10 +40,10 @@ void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_point return points[i] < points[j]; }); - CH.resize(2 * n_points); + CH.resize(2 * size(points)); index_t k = 0; - for(index_t p = 0; p < n_points; ++p) + for(index_t p = 0; p < size(points); ++p) { const index_t & i = idx[p]; while(k > 1 && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; @@ -43,7 +51,7 @@ void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_point } index_t t = k; - for(index_t p = n_points - 2; p > 0; --p) + for(index_t p = size(points) - 2; p > 0; --p) { const index_t & i = idx[p]; while(k > t && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; @@ -55,10 +63,9 @@ void convex_hull::andrew_algorithm(const vertex * points, const size_t & n_point CH.resize(k); } -bool convex_hull::ccw(const vertex & p, const vertex & q, const vertex & r) +bool convex_hull::ccw(const ivec2 & p, const ivec2 & q, const ivec2 & r) { - // TODO vec2 - return cross(q - p, r - p).z() > 0; + return cross(q - p, r - p) > 0; } From 1c6728ea1c4848674cafa612eff5fda08e64410a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Nov 2023 15:11:39 +0100 Subject: [PATCH 0908/1018] std: using std::ranges::sort --- include/gproshan/mdict/mdict.h | 1 - src/gproshan/features/key_points.cpp | 2 +- src/gproshan/geometry/convex_hull.cpp | 9 ++++----- src/gproshan/mdict/mdict.cpp | 15 ++++++++------- src/gproshan/mdict/msparse_coding.cpp | 2 +- src/gproshan/mdict/patch.cpp | 4 ++-- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 78f2d1de..cd4648d8 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -20,7 +20,6 @@ struct locval_t real_t val; }; -bool operator < (const locval_t & a, const locval_t & b); void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 38005fed..69c5de69 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -30,7 +30,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) for(index_t f = 0; f < mesh->n_trigs; ++f) face_areas[f] = { mesh->area_trig(f), f }; - std::sort(begin(face_areas), end(face_areas)); + std::ranges::sort(face_areas); is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 9cd567f1..d0dbbd4b 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -34,11 +34,10 @@ void convex_hull::andrew_algorithm(const std::vector & points) std::vector idx(size(points)); std::iota(begin(idx), end(idx), 0); - std::sort(begin(idx), end(idx), - [&points](const index_t & i, const index_t & j) - { - return points[i] < points[j]; - }); + std::ranges::sort(idx, [&points](const index_t & i, const index_t & j) + { + return points[i] < points[j]; + }); CH.resize(2 * size(points)); diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index d8110a6e..d377b25d 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -17,11 +17,6 @@ const real_t sigma = 0.01; // SPARSE -bool operator < (const locval_t & a, const locval_t & b) -{ - return (a.i == b.i) ? a.j < b.j : a.i < b.i; -} - std::ostream & operator << (std::ostream & os, const locval_t & lc) { return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; @@ -77,7 +72,10 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) { a_sp_mat alpha = OMP_all(locval, X, D, L); - std::sort(begin(locval), end(locval)); + std::ranges::sort(locval, [](const locval_t & a, const locval_t & b) + { + return a.i == b.i ? a.j < b.j : a.i < b.i; + }); rows.push_back(0); for(index_t k = 1; k < size(locval); ++k) @@ -360,7 +358,10 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si { a_sp_mat alpha = OMP_all(locval, patches, A, L); - std::sort(begin(locval), end(locval)); + std::ranges::sort(locval, [](const locval_t & a, const locval_t & b) + { + return a.i == b.i ? a.j < b.j : a.i < b.i; + }); rows.push_back(0); for(index_t k = 1; k < size(locval); ++k) diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 32f1ef40..09ca3209 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -891,7 +891,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) rp.xyz.row(2) = x.t(); } - std::sort(begin(patches_error), end(patches_error)); + std::ranges::sort(patches_error); fprintf(stderr, "error %16s%16s\n", "best", "worst"); for(index_t i = 0; i < 10; ++i) diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 27be2006..189a31f1 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -736,9 +736,9 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, distances.push_back(norm(a - b)); } */ - std::sort(begin(distances), end(distances)); + std::ranges::sort(distances); size_t n_elem = size(distances); - if(size(distances)%2 ==0) + if(size(distances) % 2 ==0) { avg_dist = (distances[n_elem/2] + distances[(n_elem/2 -1)])/2; } From 5d288573c5f305877b624bbbc880715cfedb25b7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Nov 2023 16:41:27 +0100 Subject: [PATCH 0909/1018] viewer: organizing menu items --- src/gproshan/viewer/viewer.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 380b19d2..3d2f9211 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -452,7 +452,6 @@ void viewer::init_menus() add_menu("Viewer", { add_process("Help", m_help, GLFW_KEY_F1), - add_process("Close", m_close, GLFW_KEY_ESCAPE), add_process("Maximize", m_maximize, GLFW_KEY_F11), add_process("Hide/Show ImGui", m_hide_show_imgui, GLFW_KEY_I), add_process("Save/Load view", m_save_load_view, GLFW_KEY_PERIOD), @@ -461,29 +460,30 @@ void viewer::init_menus() add_process("Background color inc", m_bgc_inc, GLFW_KEY_RIGHT), add_process("Background color dec", m_bgc_dec, GLFW_KEY_LEFT), add_process("Background color white", m_bgc_white, GLFW_KEY_1), - add_process("Background color black", m_bgc_black, GLFW_KEY_0) + add_process("Background color black", m_bgc_black, GLFW_KEY_0), + add_process("Close", m_close, GLFW_KEY_ESCAPE) }); add_menu("Render", { + add_process("Render Flat", m_render_flat, GLFW_KEY_TAB), add_process("Render Point Cloud", m_render_pointcloud, GLFW_KEY_F5), add_process("Render Wireframe", m_render_wireframe, GLFW_KEY_F6), add_process("Render Triangles", m_render_triangles, GLFW_KEY_F7), add_process("Render GL", m_render_gl, GLFW_KEY_F8), - add_process("Level Curves", m_render_lines, GLFW_KEY_SPACE), - add_process("Render Flat", m_render_flat, GLFW_KEY_TAB), - add_process("Setup Raytracing", m_setup_raytracing, GLFW_KEY_R), add_process("Render Embree", m_render_embree, GLFW_KEY_F9), #ifdef GPROSHAN_OPTIX add_process("Render OptiX", m_render_optix, GLFW_KEY_F10), #endif // GPROSHAN_OPTIX - add_process("Raycasting", m_raycasting, GLFW_KEY_ENTER) + add_process("Setup Raytracing", m_setup_raytracing, GLFW_KEY_R), + add_process("Raycasting", m_raycasting, GLFW_KEY_ENTER), + add_process("Level Curves", m_render_lines, GLFW_KEY_SPACE) }); add_menu("Mesh", { - add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT), add_process("Save Mesh", m_save_mesh, GLFW_KEY_W), + add_process("Reload/Reset", m_reset_mesh, GLFW_KEY_INSERT), add_process("Remove Selected Mesh", m_remove_mesh, GLFW_KEY_DELETE), add_process("Pop Mesh", m_pop_mesh, GLFW_KEY_BACKSPACE), add_process("Normalize Mesh", m_normalize_mesh), From 4bf65223949069bffefeae818b709feec23893e8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 29 Nov 2023 17:26:52 +0100 Subject: [PATCH 0910/1018] viewer: minor updates --- src/gproshan/app_viewer.cpp | 1 + src/gproshan/viewer/viewer.cpp | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 27ebfb70..c884e6b2 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -212,6 +212,7 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) { che * ptx_mesh = scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename); che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); + ptx_mesh->filename = mesh->filename + ".ptx"; view->add_mesh(ptx_mesh); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 3d2f9211..4a5951ad 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -51,10 +51,10 @@ const std::vector viewer::m_window_split = { {1, 1}, const std::vector viewer::colormap = { "vertex color", - "blue heatmap", - "red heatmap", - "blue/read heatmap", - "set heatmap", + "heatmap blue", + "heatmap red", + "heatmap blue/read", + "heatmap set", "material scene", }; @@ -231,7 +231,7 @@ void viewer::imgui() ImGui::End(); ImGui::SetNextWindowSize(ImVec2(360, -1)); - ImGui::SetNextWindowPos(ImVec2(20, 60), ImGuiCond_Once); + ImGui::SetNextWindowPos(ImVec2(0, 20), ImGuiCond_Once); ImGui::Begin("gproshan"); ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5); From d3fd16c0821d379a8857224a2b98c353ee18b2a7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 1 Dec 2023 17:28:28 +0100 Subject: [PATCH 0911/1018] raytracing: mirror rays optix --- include/gproshan/raytracing/utils.h | 32 ++++++++++ src/gproshan/raytracing/optix.cu | 91 ++++++++++++++++++----------- 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 93514412..9bd7ee6b 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -65,6 +65,7 @@ template struct t_eval_hit { index_t primID = NIL; + int illum = 1; T dist = 0; T u = 0, v = 0; vec position; @@ -132,6 +133,37 @@ struct t_eval_hit d = mat.d; // if(mat.map_d != -1) // d = texture(sc.textures[mat.map_d], texcoord); + + illum = mat.illum; + } + + // PTX symbols of certain types (e.g. pointers to functions) cannot be used to initialize array + __host_device__ + bool scatter_mat(const vec & v, vec & scattered) + { + switch(illum) + { + case 3: + case 5: + case 6: + case 7: + return scatter_reflect(v, scattered); + } + + return false; + } + + __host_device__ + bool scatter_reflect(const vec & v, vec & scattered) + { + scattered = normalize(v - 2 * dot(v, normal) * normal); + return dot(scattered, normal) > 0; + } + + __host_device__ + bool scatter_diffuse(const vec & v, vec & scattered) + { + return false; } }; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index de3c95e4..3f2119ea 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -58,17 +58,23 @@ extern "C" __global__ void __closesthit__radiance() hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - vec3 li = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, - [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool - { - uint32_t occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), + vec3 * trace = ray_data(); + vec3 & color = trace[0]; + vec3 & position = trace[1]; + vec3 & scattered = trace[2]; // in ray_dir / out scattered + vec3 & attenuation = trace[3]; + + color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, + [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + { + uint32_t occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), OPTIX_RAY_FLAG_DISABLE_ANYHIT | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, @@ -80,8 +86,11 @@ extern "C" __global__ void __closesthit__radiance() return occluded != 0; }); - vec4 & pixel_color = *ray_data(); - pixel_color = (pixel_color * optix_params.n_samples + (li, 1)) / (optix_params.n_samples + 1); + color *= attenuation; + position = hit.position; + + if(!hit.scatter_mat(scattered, scattered)) + attenuation = 0; } @@ -107,29 +116,45 @@ extern "C" __global__ void __raygen__render_frame() const int ix = optixGetLaunchIndex().x; const int iy = optixGetLaunchIndex().y; - const vec3 ray_dir = ray_view_dir( {ix + optix_params.viewport_x, iy + optix_params.viewport_y}, - {optix_params.window_width, optix_params.window_height}, - optix_params.inv_proj_view, - optix_params.cam_pos - ); + vec3 trace[4]; + vec3 & color = trace[0]; + vec3 & position = trace[1]; + vec3 & ray_dir = trace[2]; + vec3 & attenuation = trace[3] = 1; - vec4 & pixel_color = optix_params.color_buffer[ix + iy * optixGetLaunchDimensions().x]; + position = optix_params.cam_pos; + ray_dir = ray_view_dir( {ix + optix_params.viewport_x, iy + optix_params.viewport_y}, + {optix_params.window_width, optix_params.window_height}, + optix_params.inv_proj_view, + optix_params.cam_pos + ); uint32_t u0, u1; - pack_pointer(&pixel_color, u0, u1); - - optixTrace( optix_params.traversable, - * (float3 *) &optix_params.cam_pos, - * (float3 *) &ray_dir, - 1e-5f, // tmin - 1e20f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, - 0, // SBT offset - 2, // SBT stride - 0, // missSBTIndex - u0, u1); + pack_pointer(trace, u0, u1); + + int depth = 3; + vec3 color_acc = 0; + + while(--depth) + { + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &ray_dir, + 1e-5f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex + u0, u1); + + color_acc += color; + } + + vec4 & pixel_color = optix_params.color_buffer[ix + iy * optixGetLaunchDimensions().x]; + pixel_color = (pixel_color * optix_params.n_samples + (color_acc, 1)) / (optix_params.n_samples + 1); } From b9ef88ac900644819532d04ceccbf7e3f698ea07 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 1 Dec 2023 23:54:00 +0100 Subject: [PATCH 0912/1018] raytracing: fix miss rays, adding depth path tracing --- include/gproshan/raytracing/optix_params.h | 15 ++++++++------- include/gproshan/raytracing/render_params.h | 1 + src/gproshan/raytracing/optix.cpp | 1 + src/gproshan/raytracing/optix.cu | 6 +++--- src/gproshan/viewer/viewer.cpp | 1 + 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index 47369572..6f52200c 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -20,13 +20,14 @@ namespace gproshan::rt { struct launch_params { vec4 * color_buffer = nullptr; - int buffer_size = 0; - int window_width = 0; - int window_height = 0; - int viewport_x = 0; - int viewport_y = 0; - int n_samples = 0; - int n_lights = 0; + int buffer_size = 0; + int depth = 1; + int window_width = 0; + int window_height = 0; + int viewport_x = 0; + int viewport_y = 0; + int n_samples = 0; + int n_lights = 0; light lights[NL]; light ambient; vec3 cam_pos; diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index c1e8d218..1bb307c5 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -14,6 +14,7 @@ const size_t NL = 16; // number of lights struct render_params { + int depth = 1; int window_width = 0; int window_height = 0; int viewport_width = 0; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index d185463b..840b8e27 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -130,6 +130,7 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) optix_params.n_samples = n_samples; optix_params.color_buffer = img; + optix_params.depth = params.depth; optix_params.window_width = params.window_width; optix_params.window_height = params.window_height; if(params.viewport_is_window) diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 3f2119ea..a836edcd 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -101,8 +101,7 @@ extern "C" __global__ void __anyhit__shadow() {} extern "C" __global__ void __miss__radiance() { - vec4 & pixel_color = *ray_data(); - pixel_color = {0, 0, 0, 0}; + optixSetPayload_0(0); } extern "C" __global__ void __miss__shadow() @@ -132,9 +131,9 @@ extern "C" __global__ void __raygen__render_frame() uint32_t u0, u1; pack_pointer(trace, u0, u1); - int depth = 3; vec3 color_acc = 0; + int depth = optix_params.depth + 1; while(--depth) { optixTrace( optix_params.traversable, @@ -150,6 +149,7 @@ extern "C" __global__ void __raygen__render_frame() 0, // missSBTIndex u0, u1); + if(!u0) break; // miss color_acc += color; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4a5951ad..2b7bec82 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -979,6 +979,7 @@ bool viewer::m_setup_raytracing(viewer * view) static double time = 0; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); + ImGui::SliderInt("rt depth", &view->render_params.depth, 1, 1 << 6); if(ImGui::Button("Build")) { From 19016ada627bc4e27a9eca4ba44dcfe3855bcefe Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 14:27:12 +0100 Subject: [PATCH 0913/1018] scenes: fix getting material texture in shading rt/gl --- include/gproshan/raytracing/utils.h | 6 +++--- shaders/shading.glsl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 9bd7ee6b..1f1bffa6 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -118,15 +118,15 @@ struct t_eval_hit Ka = mat.Ka; if(mat.map_Ka != -1) - Ka *= texture(sc.textures[mat.map_Ka], texcoord); + Ka = texture(sc.textures[mat.map_Ka], texcoord); Kd = mat.Kd; if(mat.map_Kd != -1) - Kd *= texture(sc.textures[mat.map_Kd], texcoord); + Kd = texture(sc.textures[mat.map_Kd], texcoord); Ks = mat.Ks; if(mat.map_Ks != -1) - Ks *= texture(sc.textures[mat.map_Ks], texcoord); + Ks = texture(sc.textures[mat.map_Ks], texcoord); Ns = mat.Ns; diff --git a/shaders/shading.glsl b/shaders/shading.glsl index e271a344..66715335 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -49,15 +49,15 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) { Ka = mat.Ka; if(mat.map_Ka != -1) - Ka *= texture(tex_Ka, texcoord).rgb; + Ka = texture(tex_Ka, texcoord).rgb; Kd = mat.Kd; if(mat.map_Kd != -1) - Kd *= texture(tex_Kd, texcoord).rgb; + Kd = texture(tex_Kd, texcoord).rgb; Ks = mat.Ks; if(mat.map_Ks != -1) - Ks *= texture(tex_Ks, texcoord).rgb; + Ks = texture(tex_Ks, texcoord).rgb; Ns = mat.Ns; } From 076b94b38728cd9f2df9411eded76d970a4909d7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 16:01:00 +0100 Subject: [PATCH 0914/1018] scenes: fix specular texture one channel --- shaders/shading.glsl | 2 +- src/gproshan/raytracing/embree.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/shaders/shading.glsl b/shaders/shading.glsl index 66715335..dab32d5a 100644 --- a/shaders/shading.glsl +++ b/shaders/shading.glsl @@ -57,7 +57,7 @@ vec3 shading(vec3 color, vec3 n, vec3 pos, vec2 texcoord) Ks = mat.Ks; if(mat.map_Ks != -1) - Ks = texture(tex_Ks, texcoord).rgb; + Ks = vec3(texture(tex_Ks, texcoord).r); Ns = mat.Ns; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index d55fbf22..8ac41daa 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -132,11 +132,12 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_trigs || pointcloud) g_meshes[i]->n_trigs = 0; + [[maybe_unused]] const index_t & geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? add_mesh(meshes[i], model_mats[i]) : add_pointcloud(meshes[i], model_mats[i]); - gproshan_error_var(i == geomID); + gproshan_debug_var(i == geomID); } rtcCommitScene(rtc_scene); From 60d39a752ac1bf9ab02cb682b97f77d428698ed9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 20:12:13 +0100 Subject: [PATCH 0915/1018] rt: random samples --- include/gproshan/raytracing/utils.h | 6 +++--- src/gproshan/raytracing/optix.cu | 16 +++++++++++----- src/gproshan/raytracing/raytracing.cpp | 16 +++++++++++++--- src/gproshan/viewer/che_viewer.cpp | 3 ++- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 1f1bffa6..547a4ec6 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -211,10 +211,10 @@ using eval_hit = t_eval_hit; template __host_device__ -vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos) +vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos, random & rnd) { - vec2 screen = { (float(pos.x()) + 0.5f) / windows_size.x(), - (float(pos.y()) + 0.5f) / windows_size.y() + vec2 screen = { (float(pos.x()) + rnd()) / windows_size.x(), + (float(pos.y()) + rnd()) / windows_size.y() }; vec view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1}; vec q = inv_proj_view * view; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index a836edcd..8c916bf5 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -112,8 +112,15 @@ extern "C" __global__ void __miss__shadow() extern "C" __global__ void __raygen__render_frame() { - const int ix = optixGetLaunchIndex().x; - const int iy = optixGetLaunchIndex().y; + const int & ix = optixGetLaunchIndex().x; + const int & iy = optixGetLaunchIndex().y; + + const ivec2 & pos = { + ix + optix_params.viewport_x, + iy + optix_params.viewport_y + }; + + random rnd(pos.x(), pos.y()); vec3 trace[4]; vec3 & color = trace[0]; @@ -122,10 +129,9 @@ extern "C" __global__ void __raygen__render_frame() vec3 & attenuation = trace[3] = 1; position = optix_params.cam_pos; - ray_dir = ray_view_dir( {ix + optix_params.viewport_x, iy + optix_params.viewport_y}, + ray_dir = ray_view_dir(pos, {optix_params.window_width, optix_params.window_height}, - optix_params.inv_proj_view, - optix_params.cam_pos + optix_params.inv_proj_view, optix_params.cam_pos, rnd ); uint32_t u0, u1; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 3e3ea393..b11ee508 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -25,12 +25,20 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f for(int i = 0; i < params.viewport_width; ++i) for(int j = 0; j < params.viewport_height; ++j) { + const ivec2 & pos = { + i + params.viewport_x, + j + params.viewport_y + }; + + random rnd(pos.x(), pos.y()); + //row major vec4 & color = img[j * params.viewport_width + i]; - const vertex & dir = ray_view_dir( {i + params.viewport_x, j + params.viewport_y}, + const vertex & dir = ray_view_dir( pos, {window_width, window_height}, params.inv_proj_view, - params.cam_pos + params.cam_pos, + rnd ); vec3 li = closesthit_radiance(params.cam_pos, dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); @@ -52,9 +60,11 @@ float * raytracing::raycaster( const ivec2 & windows_size, for(int i = 0; i < windows_size.x(); ++i) for(int j = 0; j < windows_size.y(); ++j) { + random rnd(i, j); + //row major float & color = frame[(windows_size.y() - j - 1) * windows_size.x() + i] = 0; - vertex dir = ray_view_dir({i, j}, windows_size, inv_proj_view, cam_pos); + vertex dir = ray_view_dir({i, j}, windows_size, inv_proj_view, cam_pos, rnd); for(index_t s = 0; s < samples; ++s) color += intersect_depth(cam_pos, dir); diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 702e945f..404dc98f 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -236,7 +236,8 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) void che_viewer::select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos); + rt::random rnd(pos.x(), pos.y()); + const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos, rnd); const index_t & v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); From c99de39dc4222092d00cbdf756b3477643ca3223 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 21:00:26 +0100 Subject: [PATCH 0916/1018] rt: refactoring render params --- include/gproshan/raytracing/optix_params.h | 14 +++--- include/gproshan/raytracing/raytracing.h | 13 ++--- include/gproshan/raytracing/render_params.h | 23 ++++----- include/gproshan/raytracing/utils.h | 7 ++- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 10 ++-- src/gproshan/raytracing/optix.cpp | 23 +++------ src/gproshan/raytracing/optix.cu | 19 +++----- src/gproshan/raytracing/raytracing.cpp | 53 +++++++-------------- src/gproshan/viewer/che_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 23 ++++----- 11 files changed, 74 insertions(+), 115 deletions(-) diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index 6f52200c..aa2723cc 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -20,14 +20,12 @@ namespace gproshan::rt { struct launch_params { vec4 * color_buffer = nullptr; - int buffer_size = 0; - int depth = 1; - int window_width = 0; - int window_height = 0; - int viewport_x = 0; - int viewport_y = 0; - int n_samples = 0; - int n_lights = 0; + unsigned int buffer_size = 0; + uvec2 window_size; + uvec2 viewport_pos; + unsigned int depth = 1; + unsigned int n_frames = 0; + unsigned int n_lights = 0; light lights[NL]; light ambient; vec3 cam_pos; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index d16a74b7..22df7eef 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -14,20 +14,17 @@ namespace gproshan::rt { class raytracing { - protected: - size_t n_samples = 0; - public: raytracing() = default; virtual ~raytracing() = default; virtual void render(vec4 * img, const render_params & params, const bool & flat); - virtual float * raycaster( const ivec2 & windows_size, - const mat4 & inv_proj_view, - const vertex & cam_pos, - const index_t & samples = 4 - ) const; + virtual std::vector raycaster( const uvec2 & windows_size, + const mat4 & inv_proj_view, + const vertex & cam_pos, + const index_t & samples = 4 + ) const; virtual eval_hit intersect( const vertex &, // org const vertex &, //dir diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 1bb307c5..185e67b4 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -14,14 +14,12 @@ const size_t NL = 16; // number of lights struct render_params { - int depth = 1; - int window_width = 0; - int window_height = 0; - int viewport_width = 0; - int viewport_height = 0; - int viewport_x = 0; - int viewport_y = 0; - int n_lights = 0; + uvec2 window_size; + uvec2 viewport_size; + uvec2 viewport_pos; + unsigned int depth = 1; + unsigned int n_frames = 0; + unsigned int n_lights = 0; light lights[NL]; light ambient = {0, 1, 0.1}; vertex cam_pos; @@ -40,12 +38,9 @@ struct render_params void log() { - gproshan_log_var(window_width); - gproshan_log_var(window_height); - gproshan_log_var(viewport_width); - gproshan_log_var(viewport_height); - gproshan_log_var(viewport_x); - gproshan_log_var(viewport_y); + gproshan_log_var(window_size); + gproshan_log_var(viewport_size); + gproshan_log_var(viewport_pos); gproshan_log_var(n_lights); gproshan_log_var(cam_pos); gproshan_log_var(restart); diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 547a4ec6..7688f8f3 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -211,7 +211,12 @@ using eval_hit = t_eval_hit; template __host_device__ -vec ray_view_dir(const ivec2 & pos, const ivec2 & windows_size, const mat & inv_proj_view, const vec & cam_pos, random & rnd) +vec ray_view_dir( const uvec2 & pos, + const uvec2 & windows_size, + const mat & inv_proj_view, + const vec & cam_pos, + random & rnd + ) { vec2 screen = { (float(pos.x()) + rnd()) / windows_size.x(), (float(pos.y()) + rnd()) / windows_size.y() diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 3911ffa2..0be733ad 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -84,7 +84,7 @@ class che_viewer virtual void draw_pointcloud(shader & program); void draw_selected_vertices(che_viewer & sphere, shader & program); - void select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); + void select(const uvec2 & pos, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos); void log_info(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 130eb926..2e6a9e36 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -60,10 +60,10 @@ class viewer GLFWwindow * window = nullptr; rt::render_params render_params; - int & window_width = render_params.window_width; - int & window_height = render_params.window_height; - int & viewport_width = render_params.viewport_width; - int & viewport_height = render_params.viewport_height; + unsigned int & window_width = render_params.window_size.x(); + unsigned int & window_height = render_params.window_size.y(); + unsigned int & viewport_width = render_params.viewport_size.x(); + unsigned int & viewport_height = render_params.viewport_size.y(); mat4 proj_mat; mat4 proj_view_mat; @@ -176,7 +176,7 @@ class viewer static bool m_raycasting(viewer * view); - void pick_vertex(const int & x, const int & y); + void pick_vertex(const uvec2 & pos); void check_apply_all_meshes(const std::function & fun); }; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 840b8e27..b4c886d5 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -125,22 +125,15 @@ optix::~optix() void optix::render(vec4 * img, const render_params & params, const bool & flat) { - if(params.restart) n_samples = 0; - - optix_params.n_samples = n_samples; + optix_params.depth = params.depth; + optix_params.n_frames = params.n_frames; optix_params.color_buffer = img; - optix_params.depth = params.depth; - optix_params.window_width = params.window_width; - optix_params.window_height = params.window_height; + optix_params.window_size = params.window_size; if(params.viewport_is_window) - { - optix_params.window_width = params.viewport_width; - optix_params.window_height = params.viewport_height; - } + optix_params.window_size = params.viewport_size; - optix_params.viewport_x = params.viewport_x; - optix_params.viewport_y = params.viewport_y; + optix_params.viewport_pos = params.viewport_pos; optix_params.flat = flat; optix_params.cam_pos = params.cam_pos; @@ -156,14 +149,12 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) (CUdeviceptr) optix_params_buffer, sizeof(launch_params), &sbt, - params.viewport_width, - params.viewport_height, + params.viewport_size.x(), + params.viewport_size.y(), 1 ); cudaDeviceSynchronize(); - - ++n_samples; } void optix::create_raygen_programs() diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 8c916bf5..bcc3c372 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -112,14 +112,12 @@ extern "C" __global__ void __miss__shadow() extern "C" __global__ void __raygen__render_frame() { - const int & ix = optixGetLaunchIndex().x; - const int & iy = optixGetLaunchIndex().y; - - const ivec2 & pos = { - ix + optix_params.viewport_x, - iy + optix_params.viewport_y + const uvec2 & id = {optixGetLaunchIndex().x, + optixGetLaunchIndex().y }; + const uvec2 & pos = id + optix_params.viewport_pos; + random rnd(pos.x(), pos.y()); vec3 trace[4]; @@ -129,10 +127,7 @@ extern "C" __global__ void __raygen__render_frame() vec3 & attenuation = trace[3] = 1; position = optix_params.cam_pos; - ray_dir = ray_view_dir(pos, - {optix_params.window_width, optix_params.window_height}, - optix_params.inv_proj_view, optix_params.cam_pos, rnd - ); + ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); uint32_t u0, u1; pack_pointer(trace, u0, u1); @@ -159,8 +154,8 @@ extern "C" __global__ void __raygen__render_frame() color_acc += color; } - vec4 & pixel_color = optix_params.color_buffer[ix + iy * optixGetLaunchDimensions().x]; - pixel_color = (pixel_color * optix_params.n_samples + (color_acc, 1)) / (optix_params.n_samples + 1); + vec4 & pixel_color = optix_params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; + pixel_color = (pixel_color * optix_params.n_frames + (color_acc, 1)) / (optix_params.n_frames + 1); } diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index b11ee508..2ad75c95 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -9,56 +9,39 @@ namespace gproshan::rt { void raytracing::render(vec4 * img, const render_params & params, const bool & flat) { - if(params.restart) n_samples = 0; - - int window_width = params.window_width; - int window_height = params.window_height; + uvec2 window_size = params.window_size; if(params.viewport_is_window) - { - window_width = params.viewport_width; - window_height = params.viewport_height; - } + window_size = params.viewport_size; - vec4 li; - - #pragma omp parallel for private(li) - for(int i = 0; i < params.viewport_width; ++i) - for(int j = 0; j < params.viewport_height; ++j) + #pragma omp parallel for + for(unsigned int i = 0; i < params.viewport_size.x(); ++i) + for(unsigned int j = 0; j < params.viewport_size.y(); ++j) { - const ivec2 & pos = { - i + params.viewport_x, - j + params.viewport_y - }; + const uvec2 & pos = params.viewport_size + uvec2{i, j}; random rnd(pos.x(), pos.y()); //row major - vec4 & color = img[j * params.viewport_width + i]; - const vertex & dir = ray_view_dir( pos, - {window_width, window_height}, - params.inv_proj_view, - params.cam_pos, - rnd - ); + vec4 & color = img[j * params.viewport_size.x() + i]; + const vertex & dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); - vec3 li = closesthit_radiance(params.cam_pos, dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); + const vec3 & li = closesthit_radiance(params.cam_pos, dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); - color = (color * n_samples + (li, 1)) / (n_samples + 1); + color = (color * params.n_frames + (li, 1)) / (params.n_frames + 1); } - - ++n_samples; } -float * raytracing::raycaster( const ivec2 & windows_size, - const mat4 & inv_proj_view, - const vertex & cam_pos, - const index_t & samples ) const +std::vector raytracing::raycaster( const uvec2 & windows_size, + const mat4 & inv_proj_view, + const vertex & cam_pos, + const index_t & samples + ) const { - float * frame = new float[windows_size.x() * windows_size.y()]; + std::vector frame(windows_size.x() * windows_size.y()); #pragma omp parallel for - for(int i = 0; i < windows_size.x(); ++i) - for(int j = 0; j < windows_size.y(); ++j) + for(unsigned int i = 0; i < windows_size.x(); ++i) + for(unsigned int j = 0; j < windows_size.y(); ++j) { random rnd(i, j); diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 404dc98f..8c2965f0 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -234,7 +234,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) } } -void che_viewer::select(const ivec2 & pos, const ivec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) +void che_viewer::select(const uvec2 & pos, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { rt::random rnd(pos.x(), pos.y()); const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos, rnd); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 2b7bec82..bffbd62c 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -280,7 +280,7 @@ void viewer::imgui() ImGui::Separator(); - for(int i = 0; i < render_params.n_lights; ++i) + for(unsigned int i = 0; i < render_params.n_lights; ++i) { light & l = render_params.lights[i]; @@ -310,7 +310,7 @@ void viewer::imgui() if(ImGui::Button("show lights")) { sphere_points.clear(); - for(int i = 0; i < render_params.n_lights; ++i) + for(unsigned int i = 0; i < render_params.n_lights; ++i) sphere_points.push_back(render_params.lights[i].pos); } @@ -609,7 +609,7 @@ void viewer::update_viewport_meshes() meshes[m]->vy = rows - (m / cols) - 1; } - glfwGetFramebufferSize(window, &viewport_width, &viewport_height); + glfwGetFramebufferSize(window, (int *) &viewport_width, (int *) &viewport_height); viewport_width /= cols; viewport_height /= rows; cam.aspect = real_t(viewport_width) / viewport_height; @@ -704,7 +704,7 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod view->idx_selected_mesh = idx_mesh; if(mods == GLFW_MOD_SHIFT && button == GLFW_MOUSE_BUTTON_LEFT) - view->pick_vertex(ix % view->viewport_width, iy % view->viewport_height); + view->pick_vertex({ix % view->viewport_width, iy % view->viewport_height}); } if(button == GLFW_MOUSE_BUTTON_LEFT) @@ -979,7 +979,7 @@ bool viewer::m_setup_raytracing(viewer * view) static double time = 0; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); - ImGui::SliderInt("rt depth", &view->render_params.depth, 1, 1 << 6); + ImGui::SliderInt("rt depth", (int *) &view->render_params.depth, 1, 1 << 6); if(ImGui::Button("Build")) { @@ -1164,18 +1164,13 @@ bool viewer::m_raycasting(viewer * view) rt::embree rc({mesh}, {mesh.model_mat}); - float * frame = rc.raycaster( {view->viewport_width, view->viewport_height}, - inverse(view->proj_view_mat), - view->cam.eye - ); + auto frame = rc.raycaster(view->render_params.viewport_size, inverse(view->proj_view_mat), view->cam.eye); std::thread([](const CImg & img) { img.display(); }, - CImg(frame, view->viewport_width, view->viewport_height)).detach(); - - delete [] frame; + CImg(frame.data(), view->viewport_width, view->viewport_height)).detach(); return false; } @@ -1270,11 +1265,11 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) rt_frame.display(); } -void viewer::pick_vertex(const int & x, const int & y) +void viewer::pick_vertex(const uvec2 & pos) { che_viewer & mesh = selected_mesh(); - mesh.select({x, y}, {viewport_width, viewport_height}, inverse(proj_view_mat), cam.eye); + mesh.select(pos, render_params.viewport_size, inverse(proj_view_mat), cam.eye); } void viewer::check_apply_all_meshes(const std::function & fun) From 2f3c1bf89755715a75d794a4132711aa90be4868 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 21:29:08 +0100 Subject: [PATCH 0917/1018] rt: updating render params structure --- include/gproshan/raytracing/optix_params.h | 18 ++++-------------- include/gproshan/raytracing/render_params.h | 14 ++++++++++---- src/gproshan/raytracing/optix.cu | 2 +- src/gproshan/raytracing/raytracing.cpp | 6 +++--- src/gproshan/viewer/viewer.cpp | 5 +++++ 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index aa2723cc..41b95310 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -17,26 +17,16 @@ namespace gproshan::rt { -struct launch_params +struct launch_params: public base_params { - vec4 * color_buffer = nullptr; - unsigned int buffer_size = 0; - uvec2 window_size; - uvec2 viewport_pos; - unsigned int depth = 1; - unsigned int n_frames = 0; - unsigned int n_lights = 0; - light lights[NL]; - light ambient; - vec3 cam_pos; - mat4 inv_proj_view; - bool flat; - OptixTraversableHandle traversable; scene_data sc; + bool flat; void * other = nullptr; + vec4 * color_buffer = nullptr; + unsigned int buffer_size = 0; }; diff --git a/include/gproshan/raytracing/render_params.h b/include/gproshan/raytracing/render_params.h index 185e67b4..f823d274 100644 --- a/include/gproshan/raytracing/render_params.h +++ b/include/gproshan/raytracing/render_params.h @@ -12,20 +12,26 @@ namespace gproshan::rt { const size_t NL = 16; // number of lights -struct render_params + +struct base_params { uvec2 window_size; - uvec2 viewport_size; uvec2 viewport_pos; unsigned int depth = 1; unsigned int n_frames = 0; + unsigned int n_samples = 1; unsigned int n_lights = 0; light lights[NL]; light ambient = {0, 1, 0.1}; - vertex cam_pos; mat4 inv_proj_view; - bool restart = false; + vertex cam_pos; +}; + +struct render_params: public base_params +{ + uvec2 viewport_size; bool viewport_is_window = true; + bool restart = false; bool add_light(const light & l) { diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index bcc3c372..69513474 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -118,7 +118,7 @@ extern "C" __global__ void __raygen__render_frame() const uvec2 & pos = id + optix_params.viewport_pos; - random rnd(pos.x(), pos.y()); + random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); vec3 trace[4]; vec3 & color = trace[0]; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 2ad75c95..903c2437 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -19,8 +19,8 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f { const uvec2 & pos = params.viewport_size + uvec2{i, j}; - random rnd(pos.x(), pos.y()); - + random rnd(pos.x() + window_size.x() * pos.y(), params.n_frames); + //row major vec4 & color = img[j * params.viewport_size.x() + i]; const vertex & dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); @@ -44,7 +44,7 @@ std::vector raytracing::raycaster( const uvec2 & windows_size, for(unsigned int j = 0; j < windows_size.y(); ++j) { random rnd(i, j); - + //row major float & color = frame[(windows_size.y() - j - 1) * windows_size.x() + i] = 0; vertex dir = ray_view_dir({i, j}, windows_size, inv_proj_view, cam_pos, rnd); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index bffbd62c..78c8f8b6 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -1253,6 +1253,9 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) render_params.restart = rt_frame.resize(viewport_width, viewport_height) || render_params.restart; + if(render_params.restart) + render_params.n_frames = 0; + //render_params.viewport_x = mesh.vx * viewport_width; //render_params.viewport_y = mesh.vy * viewport_height; //render_params.viewport_is_window = false; @@ -1263,6 +1266,8 @@ void viewer::render_rt(che_viewer & mesh, frame & rt_frame) rt_frame.unmap_pbo(mesh.render_opt == R_OPTIX); rt_frame.display(); + + ++render_params.n_frames; } void viewer::pick_vertex(const uvec2 & pos) From 0b8c9efe82735655258087a8317af9089ba782e9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 21:47:31 +0100 Subject: [PATCH 0918/1018] rt: adding n_samples to render params optix --- src/gproshan/raytracing/optix.cpp | 1 + src/gproshan/raytracing/optix.cu | 65 ++++++++++++++++++------------- src/gproshan/viewer/viewer.cpp | 3 +- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index b4c886d5..8cc10fee 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -127,6 +127,7 @@ void optix::render(vec4 * img, const render_params & params, const bool & flat) { optix_params.depth = params.depth; optix_params.n_frames = params.n_frames; + optix_params.n_samples = params.n_samples; optix_params.color_buffer = img; optix_params.window_size = params.window_size; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 69513474..61df3ee6 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -120,39 +120,48 @@ extern "C" __global__ void __raygen__render_frame() random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); - vec3 trace[4]; - vec3 & color = trace[0]; - vec3 & position = trace[1]; - vec3 & ray_dir = trace[2]; - vec3 & attenuation = trace[3] = 1; - - position = optix_params.cam_pos; - ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); + vec3 color_acc = 0; uint32_t u0, u1; - pack_pointer(trace, u0, u1); + int depth; - vec3 color_acc = 0; - - int depth = optix_params.depth + 1; - while(--depth) + int samples = optix_params.n_samples; + do { - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &ray_dir, - 1e-5f, // tmin - 1e20f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, - 0, // SBT offset - 2, // SBT stride - 0, // missSBTIndex - u0, u1); - - if(!u0) break; // miss - color_acc += color; + vec3 trace[4]; + vec3 & color = trace[0]; + vec3 & position = trace[1] = optix_params.cam_pos; + vec3 & ray_dir = trace[2]; + vec3 & attenuation = trace[3] = 1; + + ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); + + pack_pointer(trace, u0, u1); + + depth = optix_params.depth; + do + { + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &ray_dir, + 1e-5f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex + u0, u1); + + if(!u0) break; // miss + color_acc += color; + } + while(--depth); } + while(--samples); + + color_acc /= optix_params.n_samples; vec4 & pixel_color = optix_params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; pixel_color = (pixel_color * optix_params.n_frames + (color_acc, 1)) / (optix_params.n_frames + 1); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 78c8f8b6..7c6e8d7d 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -979,7 +979,8 @@ bool viewer::m_setup_raytracing(viewer * view) static double time = 0; ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); - ImGui::SliderInt("rt depth", (int *) &view->render_params.depth, 1, 1 << 6); + ImGui::SliderInt("depth", (int *) &view->render_params.depth, 1, 1 << 5); + ImGui::SliderInt("n_samples", (int *) &view->render_params.n_samples, 1, 1 << 5); if(ImGui::Button("Build")) { From 1d20eae498775eb409c50b080b2380ea5069ea4b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 3 Dec 2023 22:15:31 +0100 Subject: [PATCH 0919/1018] rt_optix: using payload_2 for random seed --- include/gproshan/raytracing/utils.h | 27 ++++++++++++++++++--------- src/gproshan/raytracing/optix.cpp | 4 ++-- src/gproshan/raytracing/optix.cu | 6 ++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 7688f8f3..a1e3ed4e 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -13,16 +13,19 @@ namespace gproshan::rt { -template +template struct random { - uint32_t previous; + unsigned int previous; + + __host_device__ + random(unsigned int p): previous(p) {} __host_device__ - random(uint32_t v0, uint32_t v1) + random(unsigned int v0, unsigned int v1) { - uint32_t s = 0; - for(uint32_t i = 0; i < N; ++i) + unsigned int s = 0; + for(unsigned int i = 0; i < N; ++i) { s += 0x9e3779b9; v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s) ^ ((v1 >> 5) + 0xc8013ea4); @@ -37,6 +40,12 @@ struct random previous = previous * 1664525 + 1013904223; return T(previous & 0x00FFFFFF) / T(0x01000000); } + + __host_device__ + operator unsigned int & () + { + return previous; + } }; template @@ -139,7 +148,7 @@ struct t_eval_hit // PTX symbols of certain types (e.g. pointers to functions) cannot be used to initialize array __host_device__ - bool scatter_mat(const vec & v, vec & scattered) + bool scatter_mat(const vec & v, vec & scattered, random & rnd) { switch(illum) { @@ -147,21 +156,21 @@ struct t_eval_hit case 5: case 6: case 7: - return scatter_reflect(v, scattered); + return scatter_reflect(v, scattered, rnd); } return false; } __host_device__ - bool scatter_reflect(const vec & v, vec & scattered) + bool scatter_reflect(const vec & v, vec & scattered, random & rnd) { scattered = normalize(v - 2 * dot(v, normal) * normal); return dot(scattered, normal) > 0; } __host_device__ - bool scatter_diffuse(const vec & v, vec & scattered) + bool scatter_diffuse(const vec & v, vec & scattered, random & rnd) { return false; } diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 8cc10fee..7cdcc073 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -61,8 +61,8 @@ optix::optix(const std::string & ptx) optix_pipeline_compile_opt = {}; optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; optix_pipeline_compile_opt.usesMotionBlur = false; - optix_pipeline_compile_opt.numPayloadValues = 2; - optix_pipeline_compile_opt.numAttributeValues = 2; + optix_pipeline_compile_opt.numPayloadValues = 3; + optix_pipeline_compile_opt.numAttributeValues = 3; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_params"; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 61df3ee6..0dbd3000 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -89,8 +89,10 @@ extern "C" __global__ void __closesthit__radiance() color *= attenuation; position = hit.position; - if(!hit.scatter_mat(scattered, scattered)) + random rnd = optixGetPayload_2(); + if(!hit.scatter_mat(scattered, scattered, rnd)) attenuation = 0; + optixSetPayload_2(rnd); } @@ -152,7 +154,7 @@ extern "C" __global__ void __raygen__render_frame() 0, // SBT offset 2, // SBT stride 0, // missSBTIndex - u0, u1); + u0, u1, (unsigned int &) rnd); if(!u0) break; // miss color_acc += color; From 38b3dc2660ff9a089d1bd95fa694afbcd97f4c23 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Dec 2023 12:46:48 +0100 Subject: [PATCH 0920/1018] rt: scattered diffuse --- include/gproshan/raytracing/utils.h | 33 +++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index a1e3ed4e..f3a5c805 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -83,6 +83,7 @@ struct t_eval_hit vec Kd = 0.5; vec Ks = 0.2; T Ns = 10; + T Ni = 0; T d = 1; __host_device__ @@ -138,6 +139,7 @@ struct t_eval_hit Ks = texture(sc.textures[mat.map_Ks], texcoord); Ns = mat.Ns; + Ni = mat.Ni; d = mat.d; // if(mat.map_d != -1) @@ -152,6 +154,9 @@ struct t_eval_hit { switch(illum) { + case 1: + case 2: + return scatter_diffuse(v, scattered, rnd); case 3: case 5: case 6: @@ -169,10 +174,34 @@ struct t_eval_hit return dot(scattered, normal) > 0; } + __host_device__ + bool scatter_refract(const vec & v, vec & scattered, random & rnd) + { + const float dvn = dot(v, normal); + const float d = 1 - Ni * Ni * (1 - dvn * dvn); + + if(d <= 0) return false; + + scattered = Ni * (v - dvn * normal) - normal * sqrt(d); + return true; + } + __host_device__ bool scatter_diffuse(const vec & v, vec & scattered, random & rnd) { - return false; + // random unit sphere + const T & theta = rnd() * 2 * M_PI; + const T & phi = acos(2 * rnd() - 1); + const T & r = cbrtf(rnd()); + + const vec p = { r * sin(phi) * cos(theta) + , r * sin(phi) * sin(theta) + , r * cos(phi) + }; + + scattered = normalize(normal + scattered); + + return true; } }; @@ -180,7 +209,7 @@ template __host_device__ vec eval_li(const t_eval_hit & hit, const light & ambient, const light * lights, const int & n_lights, const vec & eye, Occluded occluded) { - const vec v = normalize(eye - hit.position); + const vec & v = normalize(eye - hit.position); const vec & n = hit.normal; T lambertian; From e845e7e5061e2ac039c592f93e284e059913b64b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Dec 2023 14:27:33 +0100 Subject: [PATCH 0921/1018] rt: implementing a simple path tracer --- include/gproshan/app_viewer.h | 1 + include/gproshan/raytracing/utils.h | 24 ++++++++++++------------ src/gproshan/app_viewer.cpp | 19 ++++++++++++++++++- src/gproshan/raytracing/optix.cu | 4 +++- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index c4555beb..f4adfc9c 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -63,6 +63,7 @@ class app_viewer : public viewer // Scenes static bool process_simulate_scanner(viewer * p_view); + static bool process_scatter(viewer * p_view); // Geometry static bool process_sampling_4points(viewer * p_view); diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index f3a5c805..11e062d0 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -17,7 +17,7 @@ template struct random { unsigned int previous; - + __host_device__ random(unsigned int p): previous(p) {} @@ -40,7 +40,7 @@ struct random previous = previous * 1664525 + 1013904223; return T(previous & 0x00FFFFFF) / T(0x01000000); } - + __host_device__ operator unsigned int & () { @@ -168,38 +168,38 @@ struct t_eval_hit } __host_device__ - bool scatter_reflect(const vec & v, vec & scattered, random & rnd) + bool scatter_reflect(const vec & v, vec & scattered, random & ) { scattered = normalize(v - 2 * dot(v, normal) * normal); return dot(scattered, normal) > 0; } __host_device__ - bool scatter_refract(const vec & v, vec & scattered, random & rnd) + bool scatter_refract(const vec & v, vec & scattered, random & ) { const float dvn = dot(v, normal); const float d = 1 - Ni * Ni * (1 - dvn * dvn); if(d <= 0) return false; - scattered = Ni * (v - dvn * normal) - normal * sqrt(d); + scattered = Ni * (v - dvn * normal) - normal * sqrtf(d); return true; } __host_device__ - bool scatter_diffuse(const vec & v, vec & scattered, random & rnd) + bool scatter_diffuse(const vec & , vec & scattered, random & rnd) { // random unit sphere const T & theta = rnd() * 2 * M_PI; - const T & phi = acos(2 * rnd() - 1); + const T & phi = acosf(2 * rnd() - 1); const T & r = cbrtf(rnd()); - - const vec p = { r * sin(phi) * cos(theta) - , r * sin(phi) * sin(theta) - , r * cos(phi) + + const vec p = { r * sinf(phi) * cosf(theta) + , r * sinf(phi) * sinf(theta) + , r * cosf(phi) }; - scattered = normalize(normal + scattered); + scattered = normalize(normal + p); return true; } diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index c884e6b2..107fcc13 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -52,7 +52,8 @@ void app_viewer::init() add_menu("Scenes", { - add_process("Scan Scene", process_simulate_scanner) + add_process("Scan Scene", process_simulate_scanner), + add_process("RT Scatter Points", process_scatter) }); add_menu("Geometry", @@ -219,6 +220,22 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) return true; } +bool app_viewer::process_scatter(viewer * p_view) +{ + app_viewer * view = (app_viewer *) p_view; + + rt::eval_hit h; + std::vector scatter(100); + + rt::random rnd(0xABCDEF); + for(vertex & v: scatter) + h.scatter_diffuse({}, v, rnd); + + view->add_mesh(new che(scatter.data(), scatter.size(), nullptr, 0)); + + return false; +} + // Geometry bool app_viewer::process_sampling_4points(viewer * p_view) diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 0dbd3000..a6a15fe8 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -86,12 +86,14 @@ extern "C" __global__ void __closesthit__radiance() return occluded != 0; }); + random rnd = optixGetPayload_2(); color *= attenuation; position = hit.position; - random rnd = optixGetPayload_2(); if(!hit.scatter_mat(scattered, scattered, rnd)) attenuation = 0; + + attenuation /= 2; optixSetPayload_2(rnd); } From 4d3c20dd005fb8de322bbded29bc5dd61452d4c2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Dec 2023 16:02:59 +0100 Subject: [PATCH 0922/1018] rt: diffuse scatter default --- include/gproshan/raytracing/utils.h | 5 +---- src/gproshan/raytracing/optix.cu | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 11e062d0..842bb064 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -154,9 +154,6 @@ struct t_eval_hit { switch(illum) { - case 1: - case 2: - return scatter_diffuse(v, scattered, rnd); case 3: case 5: case 6: @@ -164,7 +161,7 @@ struct t_eval_hit return scatter_reflect(v, scattered, rnd); } - return false; + return scatter_diffuse(v, scattered, rnd); } __host_device__ diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index a6a15fe8..2deadc09 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -41,7 +41,7 @@ extern "C" __global__ void __closesthit__radiance() const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); const int primID = optixGetPrimitiveIndex(); - float2 bar = optixGetTriangleBarycentrics(); + const float2 bar = optixGetTriangleBarycentrics(); OptixTraversableHandle gas = optixGetGASTraversableHandle(); const index_t sbtID = optixGetSbtGASIndex(); From 1363c59844616d56fc5ef4584e84f695131443c1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 4 Dec 2023 23:09:01 +0100 Subject: [PATCH 0923/1018] util: adding access to partitions sorted pointer --- include/gproshan/util.h | 2 ++ src/gproshan/util.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 64d31e0a..d77f27ae 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -23,6 +23,8 @@ class partitions void add(const index_t & size); size_t size(const index_t & i) const; part operator () (const index_t & i) const; + operator index_t * const & () const; + operator index_t *& (); }; struct partitions::part diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 9dc2b770..2b029a1c 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -28,6 +28,16 @@ partitions::part partitions::operator () (const index_t & i) const return {splits[i], splits[i + 1], sorted}; } +partitions::operator index_t * const & () const +{ + return sorted; +} + +partitions::operator index_t *& () +{ + return sorted; +} + void copy_real_t_array(float * destination, const float * source, const size_t & n_elem) { From 6a4af7bea53911bc21fe94a104822bf66ec67fdd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 16:37:23 +0100 Subject: [PATCH 0924/1018] rt: embree path tracer implementation structure --- src/gproshan/raytracing/optix.cpp | 2 +- src/gproshan/raytracing/optix.cu | 27 ++++++++++--------- src/gproshan/raytracing/raytracing.cpp | 37 +++++++++++++++++++------- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 7cdcc073..9e079557 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -54,7 +54,7 @@ optix::optix(const std::string & ptx) // create module - optix_module_compile_opt.maxRegisterCount = 50; + //optix_module_compile_opt.maxRegisterCount = 50; optix_module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; optix_module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 2deadc09..6d6b91c8 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -126,25 +126,28 @@ extern "C" __global__ void __raygen__render_frame() vec3 color_acc = 0; + int depth = optix_params.depth; + int samples = optix_params.n_samples; + + vec3 trace[4]; + vec3 & color = trace[0]; + vec3 & position = trace[1]; + vec3 & ray_dir = trace[2]; + vec3 & attenuation = trace[3]; + uint32_t u0, u1; - int depth; - - int samples = optix_params.n_samples; + do { - vec3 trace[4]; - vec3 & color = trace[0]; - vec3 & position = trace[1] = optix_params.cam_pos; - vec3 & ray_dir = trace[2]; - vec3 & attenuation = trace[3] = 1; - - ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); - - pack_pointer(trace, u0, u1); + color = 0; + attenuation = 1; + position = optix_params.cam_pos; + ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); depth = optix_params.depth; do { + pack_pointer(trace, u0, u1); optixTrace( optix_params.traversable, * (float3 *) &position, * (float3 *) &ray_dir, diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 903c2437..3020e700 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -13,21 +13,40 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f if(params.viewport_is_window) window_size = params.viewport_size; - #pragma omp parallel for + vec3 color_acc, position, ray_dir, attenuation; + + #pragma omp parallel for private(color_acc, position, ray_dir, attenuation) for(unsigned int i = 0; i < params.viewport_size.x(); ++i) for(unsigned int j = 0; j < params.viewport_size.y(); ++j) { - const uvec2 & pos = params.viewport_size + uvec2{i, j}; + const uvec2 & pos = params.viewport_pos + uvec2{i, j}; random rnd(pos.x() + window_size.x() * pos.y(), params.n_frames); - //row major - vec4 & color = img[j * params.viewport_size.x() + i]; - const vertex & dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); - - const vec3 & li = closesthit_radiance(params.cam_pos, dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); - - color = (color * params.n_frames + (li, 1)) / (params.n_frames + 1); + int depth = params.depth; + int samples = params.n_samples; + + do + { + color_acc = 0; + attenuation = 1; + position = params.cam_pos; + ray_dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); + + depth = params.depth; + do + { + const vec3 & color = closesthit_radiance(position, ray_dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); + color_acc += color * attenuation; + } + while(--depth); + } + while(--samples); + + color_acc /= params.n_samples; + + vec4 & pixel_color = img[i + j * params.viewport_size.x()]; + pixel_color = (pixel_color * params.n_frames + (color_acc, 1)) / (params.n_frames + 1); } } From 5cb4ab641cae5d8e096a0dd2e34868161f3c185a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 17:39:31 +0100 Subject: [PATCH 0925/1018] rt: embree simple path tracer --- include/gproshan/raytracing/embree.h | 15 ++++++++++-- include/gproshan/raytracing/raytracing.h | 16 ++++++------- include/gproshan/raytracing/utils.h | 22 +++++++++--------- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/raytracing/embree.cpp | 29 +++++++++++++++++++----- src/gproshan/raytracing/optix.cu | 24 ++++++++++---------- src/gproshan/raytracing/raytracing.cpp | 19 ++++++++++------ src/gproshan/viewer/viewer.cpp | 2 +- 8 files changed, 81 insertions(+), 48 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index ec2ad1b7..3eba4515 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -53,13 +53,24 @@ class embree : public raytracing protected: - void build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud = false); + void build_bvh( const std::vector & meshes, + const std::vector & model_mats, + const bool & pointcloud = false + ); + index_t add_sphere(const vec4 & xyzr); index_t add_mesh(const che * mesh, const mat4 & model_mat); virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); - virtual vec3 closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) const; + virtual bool closesthit_radiance( vertex & color, + vertex & attenuation, + vertex & position, + vertex & ray_dir, + random & rnd, + const render_params & params, + const bool & flat + ) const; float intersect_depth(const vertex & org, const vertex & dir) const; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 22df7eef..d437adeb 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -36,14 +36,14 @@ class raytracing ) const { return NIL; } protected: - virtual vec3 closesthit_radiance( const vertex &, // org - const vertex &, // dir - const light &, // ambient light - const light *, // lights - const int &, // n_lights - const vertex &, // cam_pos - const bool & // flat - ) const { return {}; }; + virtual bool closesthit_radiance( vertex &, // color, + vertex &, // attenuation, + vertex &, // position, + vertex &, // ray_dir, + random &, // rnd, + const render_params &, // params, + const bool & // flat + ) const { return false; }; virtual float intersect_depth( const vertex &, // org, const vertex & // dir diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 842bb064..b2f8d5be 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -150,7 +150,7 @@ struct t_eval_hit // PTX symbols of certain types (e.g. pointers to functions) cannot be used to initialize array __host_device__ - bool scatter_mat(const vec & v, vec & scattered, random & rnd) + bool scatter_mat(vec & dir, random & rnd) // dir in: v (view), out: scattered { switch(illum) { @@ -158,33 +158,33 @@ struct t_eval_hit case 5: case 6: case 7: - return scatter_reflect(v, scattered, rnd); + return scatter_reflect(dir, rnd); } - return scatter_diffuse(v, scattered, rnd); + return scatter_diffuse(dir, rnd); } __host_device__ - bool scatter_reflect(const vec & v, vec & scattered, random & ) + bool scatter_reflect(vec & dir, random & ) { - scattered = normalize(v - 2 * dot(v, normal) * normal); - return dot(scattered, normal) > 0; + dir = normalize(dir - 2 * dot(dir, normal) * normal); + return dot(dir, normal) > 0; } __host_device__ - bool scatter_refract(const vec & v, vec & scattered, random & ) + bool scatter_refract(vec & dir, random & ) { - const float dvn = dot(v, normal); + const float dvn = dot(dir, normal); const float d = 1 - Ni * Ni * (1 - dvn * dvn); if(d <= 0) return false; - scattered = Ni * (v - dvn * normal) - normal * sqrtf(d); + dir = Ni * (dir - dvn * normal) - normal * sqrtf(d); return true; } __host_device__ - bool scatter_diffuse(const vec & , vec & scattered, random & rnd) + bool scatter_diffuse(vec & dir, random & rnd) { // random unit sphere const T & theta = rnd() * 2 * M_PI; @@ -196,7 +196,7 @@ struct t_eval_hit , r * cosf(phi) }; - scattered = normalize(normal + p); + dir = normalize(normal + p); return true; } diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 107fcc13..0ae2901b 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -229,7 +229,7 @@ bool app_viewer::process_scatter(viewer * p_view) rt::random rnd(0xABCDEF); for(vertex & v: scatter) - h.scatter_diffuse({}, v, rnd); + h.scatter_diffuse(v, rnd); view->add_mesh(new che(scatter.data(), scatter.size(), nullptr, 0)); diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 8ac41daa..e50e9a1f 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -248,23 +248,40 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) return geom_id; } -vec3 embree::closesthit_radiance(const vertex & org, const vertex & dir, const light & ambient, const light * lights, const int & n_lights, const vertex & cam_pos, const bool & flat) const +bool embree::closesthit_radiance( vertex & color, + vertex & attenuation, + vertex & position, + vertex & ray_dir, + random & rnd, + const render_params & params, + const bool & flat + ) const { - ray_hit r(org, dir); - if(!intersect(r)) return {}; + ray_hit r(position, ray_dir); + if(!intersect(r)) return false; - const CHE * mesh = g_meshes[r.hit.geomID]; + const CHE & mesh = *g_meshes[r.hit.geomID]; - eval_hit hit(*mesh, r.hit.primID, r.hit.u, r.hit.v, sc); + eval_hit hit(mesh, r.hit.primID, r.hit.u, r.hit.v, sc); hit.position = r.pos(); hit.normal = flat ? r.normal() : hit.normal; - return eval_li( hit, ambient, lights, n_lights, cam_pos, + color = eval_li(hit, params.ambient, params.lights, params.n_lights, params.cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool { ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); return occluded(ro); }); + + color *= attenuation; + position = hit.position; + + if(!hit.scatter_mat(ray_dir, rnd)) + attenuation = 0; + + attenuation /= 2; + + return true; } float embree::intersect_depth(const vertex & org, const vertex & dir) const diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 6d6b91c8..000d85c3 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -60,9 +60,9 @@ extern "C" __global__ void __closesthit__radiance() vec3 * trace = ray_data(); vec3 & color = trace[0]; - vec3 & position = trace[1]; - vec3 & scattered = trace[2]; // in ray_dir / out scattered - vec3 & attenuation = trace[3]; + vec3 & attenuation = trace[1]; + vec3 & position = trace[2]; + vec3 & ray_dir = trace[3]; color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool @@ -90,7 +90,7 @@ extern "C" __global__ void __closesthit__radiance() color *= attenuation; position = hit.position; - if(!hit.scatter_mat(scattered, scattered, rnd)) + if(!hit.scatter_mat(ray_dir, rnd)) attenuation = 0; attenuation /= 2; @@ -124,19 +124,19 @@ extern "C" __global__ void __raygen__render_frame() random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); + unsigned int depth = optix_params.depth; + unsigned int samples = optix_params.n_samples; + vec3 color_acc = 0; - int depth = optix_params.depth; - int samples = optix_params.n_samples; - vec3 trace[4]; vec3 & color = trace[0]; - vec3 & position = trace[1]; - vec3 & ray_dir = trace[2]; - vec3 & attenuation = trace[3]; - + vec3 & attenuation = trace[1]; + vec3 & position = trace[2]; + vec3 & ray_dir = trace[3]; + uint32_t u0, u1; - + do { color = 0; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 3020e700..64c8c2e2 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -13,9 +13,12 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f if(params.viewport_is_window) window_size = params.viewport_size; - vec3 color_acc, position, ray_dir, attenuation; + unsigned int depth = params.depth; + unsigned int samples = params.n_samples; - #pragma omp parallel for private(color_acc, position, ray_dir, attenuation) + vec3 color_acc, color, attenuation, position, ray_dir; + + #pragma omp parallel for private(depth, samples, color_acc, color, attenuation, position, ray_dir) for(unsigned int i = 0; i < params.viewport_size.x(); ++i) for(unsigned int j = 0; j < params.viewport_size.y(); ++j) { @@ -23,12 +26,12 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f random rnd(pos.x() + window_size.x() * pos.y(), params.n_frames); - int depth = params.depth; - int samples = params.n_samples; + color_acc = 0; + samples = params.n_samples; do { - color_acc = 0; + color = 0; attenuation = 1; position = params.cam_pos; ray_dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); @@ -36,8 +39,10 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f depth = params.depth; do { - const vec3 & color = closesthit_radiance(position, ray_dir, params.ambient, params.lights, params.n_lights, params.cam_pos, flat); - color_acc += color * attenuation; + if(!closesthit_radiance(color, attenuation, position, ray_dir, rnd, params, flat)) + break; + + color_acc += color; } while(--depth); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7c6e8d7d..cc1de6bc 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -978,9 +978,9 @@ bool viewer::m_setup_raytracing(viewer * view) static int rt = 0; static double time = 0; - ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); ImGui::SliderInt("depth", (int *) &view->render_params.depth, 1, 1 << 5); ImGui::SliderInt("n_samples", (int *) &view->render_params.n_samples, 1, 1 << 5); + ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); if(ImGui::Button("Build")) { From 78e6c22296e9f27aefe249645ccab9bc5c55befa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 19:59:40 +0100 Subject: [PATCH 0926/1018] gproshan: using const index_t instead of const index_t & --- include/gproshan/app_viewer.h | 2 +- include/gproshan/features/descriptor.h | 2 +- include/gproshan/features/key_components.h | 4 +- include/gproshan/geodesics/geodesics.h | 13 +- include/gproshan/geodesics/geodesics_ptp.h | 16 +- .../gproshan/geodesics/test_geodesics_ptp.cuh | 2 +- .../test_geodesics_ptp_coalescence.cuh | 2 +- include/gproshan/geometry/mat.h | 8 +- include/gproshan/geometry/vec.h | 4 +- include/gproshan/mdict/basis.h | 4 +- include/gproshan/mdict/basis_dct.h | 8 +- include/gproshan/mdict/mdict.h | 4 +- include/gproshan/mdict/msparse_coding.h | 6 +- include/gproshan/mdict/patch.h | 32 ++-- include/gproshan/mesh/che.h | 93 ++++++------ include/gproshan/mesh/che_fill_hole.h | 4 +- include/gproshan/mesh/che_poisson.h | 2 +- include/gproshan/mesh/simplification.h | 6 +- include/gproshan/pointcloud/knn.h | 6 +- include/gproshan/raytracing/raytracing.h | 2 +- include/gproshan/raytracing/utils.h | 2 +- include/gproshan/util.h | 10 +- include/gproshan/viewer/che_viewer.h | 2 +- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/app_viewer.cpp | 18 +-- src/gproshan/features/descriptor.cpp | 2 +- src/gproshan/features/key_components.cpp | 6 +- src/gproshan/features/key_points.cpp | 4 +- src/gproshan/geodesics/dijkstra.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 19 ++- src/gproshan/geodesics/geodesics_ptp.cpp | 4 +- src/gproshan/geodesics/geodesics_ptp.cu | 8 +- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/sampling.cpp | 4 +- src/gproshan/geodesics/test_geodesics_ptp.cpp | 4 +- src/gproshan/geodesics/test_geodesics_ptp.cu | 2 +- .../test_geodesics_ptp_coalescence.cu | 2 +- src/gproshan/geometry/convex_hull.cpp | 6 +- src/gproshan/mdict/basis.cpp | 2 +- src/gproshan/mdict/basis_dct.cpp | 8 +- src/gproshan/mdict/mdict.cpp | 6 +- src/gproshan/mdict/msparse_coding.cpp | 24 +-- src/gproshan/mdict/patch.cpp | 50 ++++--- src/gproshan/mesh/che.cpp | 139 +++++++++--------- src/gproshan/mesh/che_fill_hole.cpp | 8 +- src/gproshan/mesh/che_off.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 8 +- src/gproshan/mesh/che_poisson.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/simplification.cpp | 10 +- src/gproshan/pointcloud/knn.cpp | 8 +- src/gproshan/raytracing/embree.cpp | 4 +- src/gproshan/raytracing/raytracing.cpp | 2 +- src/gproshan/scenes/scanner.cpp | 2 +- src/gproshan/scenes/scene.cpp | 10 +- src/gproshan/util.cpp | 8 +- src/gproshan/viewer/che_viewer.cpp | 8 +- src/gproshan/viewer/viewer.cpp | 14 +- 58 files changed, 325 insertions(+), 311 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index f4adfc9c..72ad620b 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -98,7 +98,7 @@ class app_viewer : public viewer static bool process_key_components(viewer * p_view); // Hole Filling - static bool process_poisson(viewer * p_view, const index_t & k); + static bool process_poisson(viewer * p_view, const index_t k); static bool process_poisson_laplacian_1(viewer * p_view); static bool process_poisson_laplacian_2(viewer * p_view); static bool process_poisson_laplacian_3(viewer * p_view); diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index fc259bf3..30b53a2f 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -28,7 +28,7 @@ class descriptor operator bool () const; ///< return norm of the descriptor for the vertex v - real_t operator () (const index_t & v) const; + real_t operator () (const index_t v) const; private: void compute_gps(); diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index e490cab0..83d38f7f 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -23,12 +23,12 @@ class key_components public: key_components(che * mesh, const std::vector & kps, const real_t & r); ~key_components(); - index_t operator()(const index_t & i); + index_t operator()(const index_t i); operator const size_t & () const; private: void compute_kcs(che * mesh, const std::vector & kps); - index_t find(const index_t & x); + index_t find(const index_t x); bool join(index_t x, index_t y); }; diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index 20c387c2..a99871a8 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -19,7 +19,7 @@ namespace gproshan { */ class geodesics { - using fm_function_t = std::function; + using fm_function_t = std::function; public: enum algorithm { FM, ///< Execute Fast Marching algorithm @@ -59,11 +59,12 @@ class geodesics ); virtual ~geodesics(); - const real_t & operator[](const index_t & i) const; - const index_t & operator()(const index_t & i) const; - const real_t & radio() const; - const index_t & farthest() const; - const size_t & n_sorted_index() const; + operator const real_t * () const; + real_t operator[](const index_t i) const; + index_t operator()(const index_t i) const; + real_t radio() const; + index_t farthest() const; + size_t n_sorted_index() const; void copy_sorted_index(index_t * indexes, const size_t & n) const; void normalize(); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index d9b9a11f..b07f3bc5 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -41,7 +41,7 @@ struct is_ok bool operator()(const real_t & val) const; __host_device__ - bool operator()(const index_t & val) const; + bool operator()(const index_t val) const; }; #endif // __CUDACC__ @@ -131,7 +131,7 @@ template __forceinline__ #endif __host_device__ -void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t & v) +void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { real_t & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; @@ -169,7 +169,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, for(index_t i = 0; i < size(sources); ++i) { // !coalescence ? - const index_t & v = sorted ? sources[i] : idx[sources[i]]; + const index_t v = sorted ? sources[i] : idx[sources[i]]; #ifdef __CUDACC__ h_dist[v] = 0; @@ -206,9 +206,9 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, { if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - const index_t & start = limits[i]; - const index_t & end = limits[j]; - const index_t & n_cond = limits[i + 1] - start; + const index_t start = limits[i]; + const index_t end = limits[j]; + const index_t n_cond = limits[i + 1] - start; T *& new_dist = dist[iter & 1]; T *& old_dist = dist[!(iter & 1)]; @@ -233,7 +233,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, #pragma omp parallel for for(index_t k = start; k < start + n_cond; ++k) { - const index_t & v = sorted ? sorted[k] : k; + const index_t v = sorted ? sorted[k] : k; error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; } @@ -241,7 +241,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, #pragma omp parallel for reduction(+: count) for(index_t k = start; k < start + n_cond; ++k) { - const index_t & v = sorted ? sorted[k] : k; + const index_t v = sorted ? sorted[k] : k; count += error[v] < PTP_TOL; } #endif diff --git a/include/gproshan/geodesics/test_geodesics_ptp.cuh b/include/gproshan/geodesics/test_geodesics_ptp.cuh index 3ea7614c..4e072e00 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp.cuh @@ -10,7 +10,7 @@ namespace gproshan { /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error); +std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error); } // namespace gproshan diff --git a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh index 34773c9e..42f5bd12 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh +++ b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh @@ -10,7 +10,7 @@ namespace gproshan { /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error); +std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error); } // namespace gproshan diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index bff29b00..cfb0b77e 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -27,26 +27,26 @@ class mat } __host_device__ - T & operator () (const index_t & i, const index_t & j) + T & operator () (const index_t i, const index_t j) { return rows[i][j]; } __host_device__ - const T & operator () (const index_t & i, const index_t & j) const + const T & operator () (const index_t i, const index_t j) const { return rows[i][j]; } __host_device__ - row & operator [] (const index_t & i) + row & operator [] (const index_t i) { assert(i < N); return rows[i]; } __host_device__ - const row & operator [] (const index_t & i) const + const row & operator [] (const index_t i) const { assert(i < N); return rows[i]; diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index acf42820..60d63d7a 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -54,14 +54,14 @@ class vec } __host_device__ - T & operator [] (const index_t & i) + T & operator [] (const index_t i) { assert(i < N); return values[i]; } __host_device__ - const T & operator [] (const index_t & i) const + const T & operator [] (const index_t i) const { assert(i < N); return values[i]; diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 3878eba6..d53867ce 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -23,12 +23,12 @@ class basis virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) = 0; - virtual real_t freq(const index_t & idx) = 0; + virtual real_t freq(const index_t idx) = 0; real_t & radio(); const size_t & dim() const; void plot_basis(); void plot_atoms(const a_mat & A); - void plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p); + void plot_patch(const a_mat & A, const a_mat & xyz, const index_t p); private: virtual void plot_basis(std::ostream & os) = 0; diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 9698a933..07b67c31 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -18,14 +18,14 @@ class basis_dct: public basis basis_dct(const size_t & n, const real_t & r = 1); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b); - real_t freq(const index_t & idx); + real_t freq(const index_t idx); private: void plot_basis(std::ostream & os); void plot_atoms(std::ostream & os, const a_vec & A); - a_vec dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny); - a_vec d_dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny); - void dct(std::ostream & os, const index_t & nx, const index_t & ny); + a_vec dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny); + a_vec d_dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny); + void dct(std::ostream & os, const index_t nx, const index_t ny); }; diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index cd4648d8..7b84dfa0 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -21,7 +21,7 @@ struct locval_t }; -void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L); +void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t & L); a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t & L); @@ -54,7 +54,7 @@ void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_ // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L); +void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t & L); a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t & L); diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index d2b89a4b..8e9316cf 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -66,8 +66,8 @@ class msparse_coding virtual ~msparse_coding(); - const real_t & operator[](const index_t & i) const; - const index_t & draw_patches(const index_t & p); + const real_t & operator[](const index_t i) const; + index_t draw_patches(const index_t p) const; operator const std::string & () const; @@ -95,7 +95,7 @@ class msparse_coding void update_alphas(a_mat & alpha, size_t threshold); void save_alpha(std::string file); - index_t sample(const index_t & s); + index_t sample(const index_t s); }; diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 35c6177e..1625e29e 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -24,7 +24,7 @@ namespace gproshan::mdict { class msparse_coding; -typedef std::function fmask_t; +typedef std::function fmask_t; typedef std::map vpatches_t; /// @@ -49,14 +49,14 @@ class patch ~patch() = default; void init( che * mesh, ///< input mesh. - const index_t & v, ///< center vertex of the patch. + const index_t v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. const real_t & radio_, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); void init_disjoint(che * mesh, - const index_t & v, + const index_t v, const size_t & n_toplevels, std::vector & _vertices, index_t * _toplevel = nullptr); @@ -64,7 +64,7 @@ class patch void init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, - const index_t & v, + const index_t v, const real_t & delta, const real_t & sum_thres, const real_t & area_thres, @@ -75,7 +75,7 @@ class patch void recover_radial_disjoint(che * mesh, const real_t & radio_, - const index_t & v); + const index_t v); void transform(); @@ -83,24 +83,24 @@ class patch void reset_xyz( che * mesh, std::vector & vpatches, - const index_t & p, + const index_t p, const fmask_t & mask = nullptr ); void reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, - const index_t & p, + const index_t p, const fmask_t & mask = nullptr ); void remove_extra_xyz_disjoint(size_t & max_points); - void add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t & p); + void add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t p); const a_vec normal(); bool is_covered( bool * covered); // void save(const real_t & radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); - void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t & p); + void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t p); void scale_xyz(const real_t & radio_f); void iscale_xyz(const real_t & radio_f); bool add_vertex_by_trigs( vertex & n, @@ -108,7 +108,7 @@ class patch double thr_angle, const real_t * geo, che * mesh, - const index_t & v, + const index_t v, real_t & area, real_t & proj_area, real_t deviation @@ -118,26 +118,22 @@ class patch private: /// Gather the vertices needed to compute the jet_fit_directions of the patch. void gather_vertices( che * mesh, - const index_t & v, + const index_t v, const size_t & n_toplevels, index_t * toplevel ); /// Gather the vertices filter by radio in the local coordinates require initialize T and x. void gather_vertices( che * mesh, - const index_t & v, + const index_t v, const real_t & radio, index_t * toplevel ); bool exists(index_t idx); /// Initialize transformation matrix T and translation vector x, using CGAL jet_fitting. - void jet_fit_directions(che * mesh, - const index_t & v - ); - void normal_fit_directions(che * mesh, - const index_t & v - ); + void jet_fit_directions(che * mesh, const index_t v); + void normal_fit_directions(che * mesh, const index_t v); real_t get_min_z(); real_t get_max_z(); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 5c524d54..96b84b03 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -41,7 +41,7 @@ class che rgb_t() = default; rgb_t(const vertex & v); rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb); - unsigned char & operator [] (const index_t & i); + unsigned char & operator [] (const index_t i); operator vertex () const; }; @@ -69,34 +69,34 @@ class che public: che(const che & mesh); che(const size_t & n_v = 0, const size_t & n_f = 0); - che(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f); + che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); virtual ~che(); // vertex access geometry methods to xyz point values, normals, and gradient - const vertex & point(const index_t & v) const; - vertex & point(const index_t & v); - const vertex & normal(const index_t & v) const; - vertex & normal(const index_t & v); - vertex shading_normal(const index_t & f, const float & u, const float & v) const; - vertex normal_trig(const index_t & f) const; - vertex normal_he(const index_t & he) const; - vertex gradient_he(const index_t & he, const real_t * f) const; - vertex gradient(const index_t & v, const real_t * f); + const vertex & point(const index_t v) const; + vertex & point(const index_t v); + const vertex & normal(const index_t v) const; + vertex & normal(const index_t v); + vertex shading_normal(const index_t f, const float & u, const float & v) const; + vertex normal_trig(const index_t f) const; + vertex normal_he(const index_t he) const; + vertex gradient_he(const index_t he, const real_t * f) const; + vertex gradient(const index_t v, const real_t * f); // vertex color methods - const real_t & heatmap(const index_t & v) const; - real_t & heatmap(const index_t & v); - const rgb_t & rgb(const index_t & v) const; - rgb_t & rgb(const index_t & v); - vertex color(const index_t & v) const; - vertex shading_color(const index_t & f, const float & u, const float & v) const; + const real_t & heatmap(const index_t v) const; + real_t & heatmap(const index_t v); + const rgb_t & rgb(const index_t v) const; + rgb_t & rgb(const index_t v); + vertex color(const index_t v) const; + vertex shading_color(const index_t f, const float & u, const float & v) const; // update methods void reload(); mat4 normalize_sphere(const real_t & r = 1) const; mat4 normalize_box(const real_t & side = 2) const; che * merge(const che * mesh, const std::vector & com_vertices = {}); - void update_vertices(const vertex * positions, const size_t & n = 0, const index_t & v_i = 0); + void update_vertices(const vertex * positions, const size_t & n = 0, const index_t v_i = 0); void update_heatmap(const real_t * hm = nullptr); void update_normals(); void invert_normals(); @@ -106,28 +106,29 @@ class che void set_head_vertices(index_t * head, const size_t & n); // half edge access methods triangular trigs and navigation - const index_t & halfedge(const index_t & he) const; - const index_t & twin_he(const index_t & he) const; - const index_t & edge_u(const index_t & e) const; - const index_t & edge_v(const index_t & e) const; - const index_t & edge_he_0(const index_t & e) const; - const index_t & edge_he_1(const index_t & e) const; - const vertex & vertex_he(const index_t & he) const; - const vertex & vertex_edge_u(const index_t & e) const; - const vertex & vertex_edge_v(const index_t & e) const; - const index_t & evt(const index_t & v) const; + const index_t * trigs_ptr() const; + index_t halfedge(const index_t he) const; + index_t twin_he(const index_t he) const; + index_t edge_u(const index_t e) const; + index_t edge_v(const index_t e) const; + index_t edge_he_0(const index_t e) const; + index_t edge_he_1(const index_t e) const; + const vertex & vertex_he(const index_t he) const; + const vertex & vertex_edge_u(const index_t e) const; + const vertex & vertex_edge_v(const index_t e) const; + index_t evt(const index_t v) const; // topology methods - che::star_he star(const index_t & v) const; - std::vector link(const index_t & v) const; + che::star_he star(const index_t v) const; + std::vector link(const index_t v) const; void edge_collapse(const std::vector & sort_edges); - void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t & k = NIL); + void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); // boundary methods std::vector bounds() const; - std::vector boundary(const index_t & v) const; - bool is_vertex_bound(const index_t & v) const; - bool is_edge_bound(const index_t & e) const; + std::vector boundary(const index_t v) const; + bool is_vertex_bound(const index_t v) const; + bool is_edge_bound(const index_t e) const; // file, name, and system methods const std::string name() const; @@ -146,15 +147,15 @@ class che virtual bool is_pointcloud() const; // operation methods - void flip(const index_t & e); - real_t cotan(const index_t & he) const; - real_t pdetriq(const index_t & t) const; - real_t area_trig(const index_t & t) const; - real_t area_vertex(const index_t & v) const; - real_t mean_curvature(const index_t & v) const; + void flip(const index_t e); + real_t cotan(const index_t he) const; + real_t pdetriq(const index_t t) const; + real_t area_trig(const index_t t) const; + real_t area_vertex(const index_t v) const; + real_t mean_curvature(const index_t v) const; protected: - void init(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f); + void init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); void init(const std::string & file); void alloc(const size_t & n_v, const size_t & n_f); void free(); @@ -177,10 +178,10 @@ class che::star_he class iterator; const che * mesh; - const index_t & v; + const index_t v; public: - star_he(const che * p_mesh, const index_t & p_v); + star_he(const che * p_mesh, const index_t p_v); iterator begin() const; iterator end() const; }; @@ -189,13 +190,13 @@ class che::star_he::iterator { const che * mesh; index_t he; - const index_t & he_end; + const index_t he_end; public: - iterator(const che * p_mesh, const index_t & p_he, const index_t & p_he_end); + iterator(const che * p_mesh, const index_t p_he, const index_t p_he_end); iterator & operator ++ (); bool operator != (const iterator & it) const; - const index_t & operator * (); + index_t operator * (); }; diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 9dd8a57a..85f73f5d 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -18,7 +18,7 @@ struct border_t border_t() = default; - border_t(const std::vector & V, const index_t & _v, const std::array & neighbors, const bool & o): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool & o): v(_v) { index_t p_v = neighbors[!o]; @@ -56,7 +56,7 @@ struct border_t return r; } - border_t(const std::vector & V, const index_t & _v, const std::array & neighbors, const bool & o, const a_vec & normal): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool & o, const a_vec & normal): v(_v) { index_t p_v = neighbors[!o]; diff --git a/include/gproshan/mesh/che_poisson.h b/include/gproshan/mesh/che_poisson.h index a023d22a..04c1f822 100644 --- a/include/gproshan/mesh/che_poisson.h +++ b/include/gproshan/mesh/che_poisson.h @@ -13,7 +13,7 @@ namespace gproshan { */ void poisson(che * mesh, const size_t & old_n_vertices, index_t k); -void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t & k); +void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t k); } // namespace gproshan diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 926f35c0..57b87e22 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -19,15 +19,15 @@ class simplification index_t levels; public: - simplification(che * mesh, const index_t & levels_ = 1); + simplification(che * mesh, const index_t levels_ = 1); ~simplification(); private: void execute(); void compute_quadrics(); - real_t compute_error(const index_t & e); + real_t compute_error(const index_t e); void order_edges(index_t * const & sort_edges, real_t * const & error_edges); - vertex create_vertex(const index_t & e); + vertex create_vertex(const index_t e); }; diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index db6dc608..d2432057 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -58,9 +58,9 @@ class k3tree k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector & query = {}); ~k3tree(); - const int * operator [] (const index_t & i) const; - const int * operator () (const index_t & i) const; - const int & operator () (const index_t & i, const index_t & j) const; + const int * operator [] (const index_t i) const; + const int * operator () (const index_t i) const; + const int & operator () (const index_t i, const index_t j) const; }; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index d437adeb..1db70db9 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -23,7 +23,7 @@ class raytracing virtual std::vector raycaster( const uvec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos, - const index_t & samples = 4 + const index_t samples = 4 ) const; virtual eval_hit intersect( const vertex &, // org diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index b2f8d5be..13ad51f7 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -90,7 +90,7 @@ struct t_eval_hit t_eval_hit() {} __host_device__ - t_eval_hit(const CHE & mesh, const index_t & aprimID, const T & au, const T & av, const scene_data & sc) + t_eval_hit(const CHE & mesh, const index_t aprimID, const T & au, const T & av, const scene_data & sc) { primID = aprimID; u = au; diff --git a/include/gproshan/util.h b/include/gproshan/util.h index d77f27ae..75ba4733 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -20,10 +20,10 @@ class partitions public: partitions(index_t * s = nullptr); - void add(const index_t & size); - size_t size(const index_t & i) const; - part operator () (const index_t & i) const; - operator index_t * const & () const; + void add(const index_t size); + size_t size(const index_t i) const; + part operator () (const index_t i) const; + operator index_t * () const; operator index_t *& (); }; @@ -48,7 +48,7 @@ struct partitions::part } __host_device__ - const index_t & operator * () + index_t operator * () const { return sorted ? sorted[i] : i; } diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 0be733ad..038e247e 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -78,7 +78,7 @@ class che_viewer void update_vbo_heatmap(const real_t * vheatmap = nullptr); void update_instances_positions(const std::vector & translations); - const vertex & selected_point(const index_t & i) const; + const vertex & selected_point(const index_t i) const; virtual void draw(shader & program); virtual void draw_pointcloud(shader & program); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 2e6a9e36..a83f9e86 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -111,7 +111,7 @@ class viewer void add_menu(const std::string & str, const std::vector & vprocesses); int add_process(const char * name, const function_t & f, const int & key = -1); bool add_mesh(che * p_mesh, const bool & reset_normals = true); - bool remove_mesh(const index_t & idx); + bool remove_mesh(const index_t idx); bool pop_mesh(); void update_viewport_meshes(); void update_status_message(const char * format, ...); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 0ae2901b..63d9381f 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -141,9 +141,9 @@ bool app_viewer::process_knn(viewer * p_view) case 1: { knn::grid grid(&mesh->point(0), mesh->n_vertices, mesh.model_mat); - for(const index_t & p: query) + for(const index_t p: query) { - for(const index_t & v: grid(mesh.model_mat * (mesh->point(p), 1), k)) + for(const index_t v: grid(mesh.model_mat * (mesh->point(p), 1), k)) mesh.selected.push_back(v); } } @@ -152,7 +152,7 @@ bool app_viewer::process_knn(viewer * p_view) case 2: { knn::k3tree k3tree(&mesh->point(0), mesh->n_vertices, k); - for(const index_t & p: query) + for(const index_t p: query) { const int * result = k3tree(p); for(int i = 0; i < k; ++i) @@ -311,7 +311,7 @@ bool app_viewer::process_connected_components(viewer * p_view) while(!q.empty()) { - for(const index_t & v: mesh->link(q.front())) + for(const index_t v: mesh->link(q.front())) if(label[v] < 0) { label[v] = nc; @@ -347,7 +347,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { g = 0; - for(const index_t & he: mesh->star(v)) + for(const index_t he: mesh->star(v)) { a = mesh->vertex_he(he_next(he)) - mesh->point(v); b = mesh->vertex_he(he_prev(he)) - mesh->point(v); @@ -918,7 +918,7 @@ bool app_viewer::process_key_components(viewer * p_view) // Hole Filling -bool app_viewer::process_poisson(viewer * p_view, const index_t & k) +bool app_viewer::process_poisson(viewer * p_view, const index_t k) { app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); @@ -965,7 +965,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) std::vector trigs; vertex center; - for(const index_t & v: vbounds) + for(const index_t v: vbounds) { vertices.push_back(mesh->point(v)); center += mesh->point(v); @@ -975,11 +975,11 @@ bool app_viewer::process_fill_holes(viewer * p_view) std::priority_queue > front; std::vector neigs(size(vertices)); /* - auto bprev = [&](const index_t & v) -> index_t & + auto bprev = [&](const index_t v) -> index_t & { return neigs[v].x(); }; - auto bnext = [&](const index_t & v) -> index_t & + auto bnext = [&](const index_t v) -> index_t & { return neigs[v].y(); }; diff --git a/src/gproshan/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp index 2ecf3c34..f92f2e71 100644 --- a/src/gproshan/features/descriptor.cpp +++ b/src/gproshan/features/descriptor.cpp @@ -30,7 +30,7 @@ descriptor::operator bool () const return features.size() > 0; } -real_t descriptor::operator () (const index_t & v) const +real_t descriptor::operator () (const index_t v) const { return norm(features.row(v)); } diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index a9ef747d..2865b8e7 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -33,7 +33,7 @@ key_components::~key_components() delete [] comp_size; } -index_t key_components::operator()(const index_t & i) +index_t key_components::operator()(const index_t i) { assert(i < n_vertices); @@ -52,7 +52,7 @@ void key_components::compute_kcs(che * mesh, const std::vector & kps) radio *= fm.radio(); for(index_t i = 0; i < n_vertices && fm[fm(i)] <= radio; ++i) - for(const index_t & he: mesh->star(fm(i))) + for(const index_t he: mesh->star(fm(i))) join(fm(i), mesh->halfedge(he_next(he))); for(index_t i = 0; i < n_vertices; ++i) @@ -61,7 +61,7 @@ void key_components::compute_kcs(che * mesh, const std::vector & kps) else if(comp[i] == i) comp[i] = NIL; } -index_t key_components::find(const index_t & x) +index_t key_components::find(const index_t x) { if(comp[x] == x) return x; return comp[x] = find(comp[x]); diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 69c5de69..2a539d63 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -40,7 +40,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) he = che::mtrig * face_areas[t].second; for(index_t i = 0; i < che::mtrig; ++i) { - const index_t & v = mesh->halfedge(he); + const index_t v = mesh->halfedge(he); if(!is_kp[v]) { kps.push_back(v); @@ -54,7 +54,7 @@ void key_points::compute_kps_areas(che * mesh, const real_t & percent) is_kp.assign(mesh->n_vertices, false); #pragma omp parallel for - for(const index_t & v: kps) + for(const index_t v: kps) is_kp[v] = true; } diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index 4b074da9..28dc19a7 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -69,7 +69,7 @@ void dijkstra::run(che * mesh) if(!visited[v]) { - for(const index_t & nv: mesh->link(v)) + for(const index_t nv: mesh->link(v)) { if(visited[nv]) { diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 0ae4248d..c5706e9a 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -39,31 +39,36 @@ geodesics::~geodesics() delete [] clusters; } -const real_t & geodesics::operator[](const index_t & i) const +geodesics::operator const real_t * () const +{ + return dist; +} + +real_t geodesics::operator[](const index_t i) const { assert(i < n_vertices); return dist[i]; } -const index_t & geodesics::operator()(const index_t & i) const +index_t geodesics::operator()(const index_t i) const { assert(i < n_vertices); return sorted_index[i]; } -const real_t & geodesics::radio() const +real_t geodesics::radio() const { assert(n_sorted != 0); return dist[farthest()]; } -const index_t & geodesics::farthest() const +index_t geodesics::farthest() const { assert(n_sorted != 0); return sorted_index[n_sorted - 1]; } -const size_t & geodesics::n_sorted_index() const +size_t geodesics::n_sorted_index() const { return n_sorted; } @@ -158,7 +163,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source if(fun && !fun(black_i)) break; - for(const index_t & v: mesh->link(black_i)) + for(const index_t v: mesh->link(black_i)) { if(color[v] == GREEN) color[v] = RED; @@ -166,7 +171,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source if(color[v] == RED) { dv = dist[v]; - for(const index_t & he: mesh->star(v)) + for(const index_t he: mesh->star(v)) { dp = update_step(&cmesh, dist, {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he)), diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 0b8dd2fb..588f83ce 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -35,7 +35,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c #pragma omp parallel for for(index_t he = 0; he < mesh->n_half_edges; ++he) { - const index_t & v = mesh->halfedge(he); + const index_t v = mesh->halfedge(he); if(v != NIL) { h_mesh.VT[he] = inv[v]; @@ -60,7 +60,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c dist[0][v] = dist[1][v] = INFINITY; } - const index_t & i = run_ptp(&h_mesh, sources, toplesets.limits, error, dist, clusters, + const index_t i = run_ptp(&h_mesh, sources, toplesets.limits, error, dist, clusters, coalescence ? inv : toplesets.index, coalescence ? nullptr : (index_t *) toplesets.index); diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index d90506a6..0f423692 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -35,7 +35,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * #pragma omp parallel for for(index_t he = 0; he < mesh->n_half_edges; ++he) { - const index_t & v = mesh->halfedge(he); + const index_t v = mesh->halfedge(he); if(v != NIL) { h_mesh.VT[he] = inv[v]; @@ -88,7 +88,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * h_dist[v] = INFINITY; } - const index_t & i = run_ptp(d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, + const index_t i = run_ptp(d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, coalescence ? inv : toplesets.index, d_sorted); cudaMemcpy(h_dist, d_dist[i], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); @@ -196,7 +196,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample limits.clear(); mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - const index_t & i = run_ptp(d_mesh, samples, limits, d_error, d_dist, d_clusters, sorted_index, d_sorted); + const index_t i = run_ptp(d_mesh, samples, limits, d_error, d_dist, d_clusters, sorted_index, d_sorted); // 1 indexing #ifdef GPROSHAN_FLOAT @@ -269,7 +269,7 @@ bool is_ok::operator()(const real_t & val) const } __host_device__ -bool is_ok::operator()(const index_t & i) const +bool is_ok::operator()(const index_t i) const { return error[i] < PTP_TOL; } diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 8f3853a7..dbcbf27a 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -90,7 +90,7 @@ void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) real_t & sum = div(v); sum = 0; - for(const index_t & he: mesh->star(v)) + for(const index_t he: mesh->star(v)) { const vertex & nhe = mesh->normal_he(he); const vertex & vhe = mesh->vertex_he(he_prev(he)) - mesh->vertex_he(he_next(he)); diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index ecaf03bd..c69bca06 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -22,7 +22,7 @@ index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - const index_t & v = points[i]; + const index_t v = points[i]; normals[i] = mesh->normal(v); geodesics fm(mesh, { v }, params); @@ -74,7 +74,7 @@ bool load_sampling(std::vector & points, real_t & radio, che * mesh, si std::ofstream os(tmp_file_path(file)); os << radio << std::endl; os << size(points) << std::endl; - for(const index_t & i: points) + for(const index_t i: points) os << i << std::endl; os.close(); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 973f5b79..0417462f 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -158,7 +158,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) for(index_t v = 0; v < n_vertices; ++v) { dv = mesh->is_vertex_bound(v) ? 1 : 0; - for([[maybe_unused]] const index_t & he: mesh->star(v)) ++dv; + for([[maybe_unused]] const index_t he: mesh->star(v)) ++dv; ++deg[dv]; } @@ -249,7 +249,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons geodesics fm(mesh, source); - error = compute_error(&fm[0], exact, mesh->n_vertices, size(source)); + error = compute_error(fm, exact, mesh->n_vertices, size(source)); return seconds; } diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu index be510939..7229d6ff 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp.cu @@ -158,7 +158,7 @@ double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector return times; } -std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) +std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu index f04a3c0c..97b4cee5 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu @@ -208,7 +208,7 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect return times; } -std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t & n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) +std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index d0dbbd4b..3daa9740 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -34,7 +34,7 @@ void convex_hull::andrew_algorithm(const std::vector & points) std::vector idx(size(points)); std::iota(begin(idx), end(idx), 0); - std::ranges::sort(idx, [&points](const index_t & i, const index_t & j) + std::ranges::sort(idx, [&points](const index_t i, const index_t j) { return points[i] < points[j]; }); @@ -44,7 +44,7 @@ void convex_hull::andrew_algorithm(const std::vector & points) index_t k = 0; for(index_t p = 0; p < size(points); ++p) { - const index_t & i = idx[p]; + const index_t i = idx[p]; while(k > 1 && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; CH[k++] = i; } @@ -52,7 +52,7 @@ void convex_hull::andrew_algorithm(const std::vector & points) index_t t = k; for(index_t p = size(points) - 2; p > 0; --p) { - const index_t & i = idx[p]; + const index_t i = idx[p]; while(k > t && !ccw(points[CH[k - 2]], points[CH[k - 1]], points[i])) --k; CH[k++] = i; } diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index 4b116b8b..3497c500 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -81,7 +81,7 @@ void basis::plot_atoms(const a_mat & A) system(file.c_str()); } -void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t & p) +void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t p) { a_mat tmp = xyz.t(); std::string data = tmp_file_path("xyz_" + std::to_string(p) + ".dat"); diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 86ad5e1c..b2cd87c0 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -48,24 +48,24 @@ void basis_dct::plot_atoms(std::ostream & os, const a_vec & A) } } -a_vec basis_dct::dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny) +a_vec basis_dct::dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny) { return cos(M_PI * nx * x / _radio ) % cos(M_PI * ny * y / _radio); } -a_vec basis_dct::d_dct(const a_vec & x, const a_vec & y, const index_t & nx, const index_t & ny) +a_vec basis_dct::d_dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny) { return - (M_PI * nx / _radio) * (sin(M_PI * nx * x / _radio) % cos(M_PI * ny * y / _radio)); } -void basis_dct::dct(std::ostream & os, const index_t & nx, const index_t & ny) +void basis_dct::dct(std::ostream & os, const index_t nx, const index_t ny) { os << "cos( (pi * v * cos(u) * " << nx << " ) / " << _radio << " ) *"; os << "cos( (pi * v * sin(u) * " << ny << " ) / " << _radio << " )"; } -real_t basis_dct::freq(const index_t & idx) +real_t basis_dct::freq(const index_t idx) { return !idx ? INFINITY : 2 * _radio / std::max(idx / n_freq, idx % n_freq); } diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index d377b25d..f10c6852 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -22,7 +22,7 @@ std::ostream & operator << (std::ostream & os, const locval_t & lc) return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; } -void OMP(std::vector & alpha, const a_vec & x, const index_t & i, const a_mat & D, const size_t & L) +void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t & L) { const auto & [aa, selected_atoms] = _OMP(x, D, L); @@ -313,7 +313,7 @@ void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_ // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t & i, const a_mat & A, const size_t & L) +void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t & L) { OMP(alpha, p.xyz.row(2).t(), i, p.phi * A, L); } @@ -378,7 +378,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, si for(index_t r = rows[j]; r < rows[j + 1]; ++r) { - const index_t & i = locval[r].j; + const index_t i = locval[r].j; a = alpha.col(i); a(j) = 0; diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 09ca3209..bf88a023 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -201,7 +201,7 @@ void msparse_coding::load_sampling() bool * invalid_seed = new bool[mesh->n_vertices]; memset(invalid_seed, 0, mesh->n_vertices * sizeof(bool)); - for(const index_t & vsf: all_sorted_features) + for(const index_t vsf: all_sorted_features) { if(invalid_seed[vsf]) continue; @@ -211,7 +211,7 @@ void msparse_coding::load_sampling() count_cov_patch = 0; if(size(p.vertices) >= 7 ) { - for(const index_t & v: p.vertices) + for(const index_t v: p.vertices) if(!covered[v]) ++count_cov_patch; count_cov += count_cov_patch; @@ -223,7 +223,7 @@ void msparse_coding::load_sampling() geo_radios.push_back(geo_radio); count += size(p.vertices); - for(const index_t & v: p.vertices) + for(const index_t v: p.vertices) { covered[v] = 1; @@ -313,7 +313,7 @@ void msparse_coding::init_radial_feature_patches() bool * pmask = mask; for(index_t s = 0; s < m_params.n_patches; ++s) - patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t i) -> bool { return pmask[i]; }); #pragma omp parallel for for(index_t s = 0; s < m_params.n_patches; ++s) @@ -442,7 +442,7 @@ void msparse_coding::init_voronoi_patches() bool * pmask = mask; for(index_t s = 0; s < m_params.n_patches; ++s) - patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t & i) -> bool { return pmask[i]; }); + patches[s].reset_xyz_disjoint(mesh, dist, m_params.n_patches, patches_map, s, [&pmask](const index_t i) -> bool { return pmask[i]; }); #pragma omp parallel for for(index_t s = 0; s < m_params.n_patches; ++s) @@ -503,7 +503,7 @@ real_t msparse_coding::execute() bool * pmask = mask; TIC(d_time) - real_t max_error = mesh_reconstruction([&pmask](const index_t & i) -> bool { return pmask[i]; }); + real_t max_error = mesh_reconstruction([&pmask](const index_t i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); @@ -633,7 +633,7 @@ real_t msparse_coding::execute_tmp() gproshan_debug_var(d_time); // initializing patches with threshold - TIC(d_time) init_patches(1, [&threshold](const index_t & i) -> bool { return i < threshold; }); TOC(d_time) + TIC(d_time) init_patches(1, [&threshold](const index_t i) -> bool { return i < threshold; }); TOC(d_time) gproshan_debug_var(d_time); // learning only from valid patches @@ -896,8 +896,8 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) fprintf(stderr, "error %16s%16s\n", "best", "worst"); for(index_t i = 0; i < 10; ++i) { - const index_t & best = patches_error[i].second; - const index_t & worst = patches_error[m_params.n_patches - i - 1].second; + const index_t best = patches_error[i].second; + const index_t worst = patches_error[m_params.n_patches - i - 1].second; fprintf(stderr, "%5d:%8u>%8u%8u>%8u\n", i, best, draw_patches(best), worst, draw_patches(worst)); } @@ -1000,20 +1000,20 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) // repeat until threshold reachs all patches } -index_t msparse_coding::sample(const index_t & s) +index_t msparse_coding::sample(const index_t s) { assert(s < m_params.n_patches); if(size(sampling)) return sampling[s]; return s; } -const real_t & msparse_coding::operator[](const index_t & i) const +const real_t & msparse_coding::operator[](const index_t i) const { assert(i < mesh->n_vertices); return dist[i]; } -const index_t & msparse_coding::draw_patches(const index_t & p) +index_t msparse_coding::draw_patches(const index_t p) const { phi_basis->plot_patch(A * alpha.col(p), patches[p].xyz, patches[p].vertices[0]); return patches[p].vertices[0]; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 189a31f1..0db32ffb 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -34,7 +34,7 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); real_t patch::nyquist_factor = 0.5; -void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) +void patch::init(che * mesh, const index_t v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) { radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -46,7 +46,7 @@ void patch::init(che * mesh, const index_t & v, const size_t & n_toplevels, cons if(!_toplevel) delete [] toplevel; } -void patch::init_disjoint(che * mesh, const index_t & v, const size_t & n_toplevels, std::vector & _vertices, index_t * _toplevel) +void patch::init_disjoint(che * mesh, const index_t v, const size_t & n_toplevels, std::vector & _vertices, index_t * _toplevel) { radio = 1; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -77,7 +77,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t & v, real_t & area, real_t & proj_area, real_t deviation) +bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t v, real_t & area, real_t & proj_area, real_t deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -91,7 +91,7 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ bool added = false; vertex pav, pbv, va, vb,vv; - for(const index_t & he: mesh->star(v)) + for(const index_t he: mesh->star(v)) { a = mesh->halfedge(he_next(he)); //index of the next vertex index_t b = mesh->halfedge(he_prev(he)); @@ -179,7 +179,7 @@ void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, } } -void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const index_t & v) +void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const index_t v) { // for small meshes 6000 0.e-5 // for others 2.e-5 @@ -231,7 +231,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const ind void patch::init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, - const index_t & v, + const index_t v, const real_t & delta, const real_t & sum_thres, const real_t & area_thres, @@ -261,7 +261,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, geodesics::params params; params.dist_alloc = new real_t[mesh->n_vertices]; - params.fun = [&](const index_t & u) -> bool + params.fun = [&](const index_t u) -> bool { if(u == v) return true; @@ -318,7 +318,7 @@ void patch::itransform() xyz.each_col() += x; } -void patch::reset_xyz(che * mesh, std::vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz(che * mesh, std::vector & vpatches, const index_t p, const fmask_t & mask) { size_t m = size(vertices); @@ -368,7 +368,7 @@ void patch::remove_extra_xyz_disjoint(size_t & max_points) } } -void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t & p) +void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t p) { size_t m = std::max (size(vertices), min_nv); @@ -392,7 +392,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche //gproshan_debug_var(np); // find the closest point - index_t min_v; + index_t min_v = NIL; double min_d = INFINITY; for(index_t v: vertices) { @@ -406,9 +406,15 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche } } + if(min_v == NIL) + { + gproshan_error_var(min_v); + break; + } + // forstar to find closest trinagle a_mat abc(3,3); - for(const index_t & he: mesh->star(min_v)) + for(const index_t he: mesh->star(min_v)) { //discard triangles outside the patch vpatches_t & ma = vpatches[mesh->halfedge(he_next(he))]; @@ -460,7 +466,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche } } -void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, const index_t & p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, const index_t p, const fmask_t & mask) { size_t m = size(vertices); if(mask) @@ -518,7 +524,7 @@ const a_vec patch::normal() return T.col(2); } -void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_toplevels, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t v, const size_t & n_toplevels, index_t * toplevel) { if(size(vertices)) vertices.clear(); @@ -530,11 +536,11 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl for(index_t i = 0; i < size(vertices); ++i) { - const index_t & v = vertices[i]; + const index_t v = vertices[i]; if(toplevel[v] == n_toplevels) break; - for(const index_t & u: mesh->link(v)) + for(const index_t u: mesh->link(v)) if(toplevel[u] == NIL) { vertices.push_back(u); @@ -543,7 +549,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const size_t & n_topl } } -void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t v, const real_t & radio, index_t * toplevel) { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); @@ -566,7 +572,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, vertices.push_back(v); - for(const index_t & u: mesh->link(v)) + for(const index_t u: mesh->link(v)) { if(toplevel[u] == NIL) { @@ -586,7 +592,7 @@ void patch::gather_vertices(che * mesh, const index_t & v, const real_t & radio, /// Compute the principal directions of the patch, centering in the vertex \f$v\f$. /// See: https://doc.cgal.org/latest/Jet_fitting_3/index.html -void patch::jet_fit_directions(che * mesh, const index_t & v) +void patch::jet_fit_directions(che * mesh, const index_t v) { size_t d_fitting = 2; size_t d_monge = 2; @@ -595,7 +601,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) std::vector in_points; in_points.reserve(size(vertices)); - for(const index_t & u: vertices) + for(const index_t u: vertices) in_points.push_back(DPoint(mesh->point(u).x(), mesh->point(u).y(), mesh->point(u).z())); My_Monge_form monge_form; @@ -623,7 +629,7 @@ void patch::jet_fit_directions(che * mesh, const index_t & v) } -void patch::normal_fit_directions(che * mesh, const index_t & v) +void patch::normal_fit_directions(che * mesh, const index_t v) { x.set_size(3); x(0) = mesh->point(v).x(); @@ -703,14 +709,14 @@ void patch::save_z(std::ostream & os) os< & vpatches, const index_t & p) +void patch::compute_avg_distance(che * mesh, std::vector & vpatches, const index_t p) { avg_dist = INFINITY; std::vector distances; for(size_t i = 0; i < size(vertices); ++i) { - for(const index_t & u: mesh->link(vertices[i])) + for(const index_t u: mesh->link(vertices[i])) { for(auto itp: vpatches[u]) { diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index bcf1eb0d..d472393c 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -36,7 +36,7 @@ che::rgb_t::rgb_t(const vertex & v) che::rgb_t::rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb): r(cr), g(cg), b(cb) {} -unsigned char & che::rgb_t::operator [] (const index_t & i) +unsigned char & che::rgb_t::operator [] (const index_t i) { return (&r)[i]; } @@ -70,7 +70,7 @@ che::che(const size_t & n_v, const size_t & n_f) alloc(n_v, n_f); } -che::che(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f) +che::che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f) { init(vertices, n_v, trigs, n_f); } @@ -83,42 +83,42 @@ che::~che() // vertex access geometry methods to xyz point values, normals, and gradient -const vertex & che::point(const index_t & v) const +const vertex & che::point(const index_t v) const { assert(v < n_vertices); return GT[v]; } -vertex & che::point(const index_t & v) +vertex & che::point(const index_t v) { assert(v < n_vertices); return GT[v]; } -const vertex & che::normal(const index_t & v) const +const vertex & che::normal(const index_t v) const { assert(VN && v < n_vertices); return VN[v]; } -vertex & che::normal(const index_t & v) +vertex & che::normal(const index_t v) { assert(VN && v < n_vertices); return VN[v]; } -vertex che::shading_normal(const index_t & f, const float & u, const float & v) const +vertex che::shading_normal(const index_t f, const float & u, const float & v) const { - const index_t & he = f * che::mtrig; + const index_t he = f * che::mtrig; return normalize(u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]); } -vertex che::normal_trig(const index_t & f) const +vertex che::normal_trig(const index_t f) const { return normal_he(f * che::mtrig); } -vertex che::normal_he(const index_t & he) const +vertex che::normal_he(const index_t he) const { const vertex & a = GT[VT[he]]; const vertex & b = GT[VT[he_next(he)]]; @@ -127,7 +127,7 @@ vertex che::normal_he(const index_t & he) const return normalize(cross(b - a, c - a)); } -vertex che::gradient_he(const index_t & he, const real_t * f) const +vertex che::gradient_he(const index_t he, const real_t * f) const { index_t i = VT[he]; index_t j = VT[he_next(he)]; @@ -146,12 +146,12 @@ vertex che::gradient_he(const index_t & he, const real_t * f) const return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } -vertex che::gradient(const index_t & v, const real_t * f) +vertex che::gradient(const index_t v, const real_t * f) { vertex g; real_t area, area_star = 0; - for(const index_t & he: star(v)) + for(const index_t he: star(v)) { area = area_trig(he_trig(he)); area_star += area; @@ -164,39 +164,39 @@ vertex che::gradient(const index_t & v, const real_t * f) // vertex color methods -const real_t & che::heatmap(const index_t & v) const +const real_t & che::heatmap(const index_t v) const { assert(v < n_vertices); return VHC[v]; } -real_t & che::heatmap(const index_t & v) +real_t & che::heatmap(const index_t v) { assert(v < n_vertices); return VHC[v]; } -const che::rgb_t & che::rgb(const index_t & v) const +const che::rgb_t & che::rgb(const index_t v) const { assert(v < n_vertices); return VC[v]; } -che::rgb_t & che::rgb(const index_t & v) +che::rgb_t & che::rgb(const index_t v) { assert(v < n_vertices); return VC[v]; } -vertex che::color(const index_t & v) const +vertex che::color(const index_t v) const { assert(VC && v < n_vertices); return VC[v]; } -vertex che::shading_color(const index_t & f, const float & u, const float & v) const +vertex che::shading_color(const index_t f, const float & u, const float & v) const { - const index_t & he = f * che::mtrig; + const index_t he = f * che::mtrig; return u * color(VT[he]) + v * color(VT[he + 1]) + (1 - u - v) * color(VT[he + 2]); } @@ -310,7 +310,7 @@ che * che::merge(const che * mesh, const std::vector & vcommon) return new_mesh; } -void che::update_vertices(const vertex * positions, const size_t & n, const index_t & v_i) +void che::update_vertices(const vertex * positions, const size_t & n, const index_t v_i) { if(!positions) return; memcpy(GT + v_i, positions, sizeof(vertex) * (!n ? n_vertices : n)); @@ -340,7 +340,7 @@ void che::update_normals() vertex & n = VN[v]; n = 0; - for(const index_t & he: star(v)) + for(const index_t he: star(v)) n += area_trig(he_trig(he)) * normal_he(he); n /= norm(n); @@ -372,9 +372,9 @@ void che::multiplicate_vertices() #pragma omp parallel for for(index_t f = 0; f < nf; ++f) { - const index_t & v = nv + f; - const index_t & old_he = f * che::mtrig; - const index_t & he = 3 * f * che::mtrig; + const index_t v = nv + f; + const index_t old_he = f * che::mtrig; + const index_t he = 3 * f * che::mtrig; GT[v] = (GT[old_VT[old_he]] + GT[old_VT[old_he + 1]] + GT[old_VT[old_he + 2]]) / 3; @@ -399,7 +399,7 @@ void che::multiplicate_vertices() for(index_t e = 0; e < n_edges; ++e) { - const index_t & he = ET[e]; + const index_t he = ET[e]; if(!(he % 3) && OT[he] != NIL) flip(e); } @@ -412,7 +412,7 @@ void che::remove_vertices(const std::vector & vertices) gproshan_debug(removing vertex); for(index_t v: vertices) { - for(const index_t & he: star(v)) + for(const index_t he: star(v)) { VT[he] = NIL; VT[he_prev(he)] = NIL; @@ -449,7 +449,7 @@ void che::remove_vertices(const std::vector & vertices) { if(v < removed[r]) { - for(const index_t & he: star(v)) + for(const index_t he: star(v)) if(VT[he] != NIL) VT[he] = v - d; } else if(v == removed[r]) @@ -508,7 +508,7 @@ void che::remove_non_manifold_vertices() { if(v < removed[r]) { - for(const index_t & he: star(v)) + for(const index_t he: star(v)) if(VT[he] != NIL) VT[he] = v - d; } else if(v == removed[r]) @@ -547,10 +547,10 @@ void che::set_head_vertices(index_t * head, const size_t & n) std::swap(GT[v], GT[i]); - for(const index_t & he: star(v)) + for(const index_t he: star(v)) VT[he] = i; - for(const index_t & he: star(i)) + for(const index_t he: star(i)) VT[he] = i; std::swap(EVT[v], EVT[i]); @@ -560,61 +560,66 @@ void che::set_head_vertices(index_t * head, const size_t & n) // half edge access methods triangular trigs and navigation -const index_t & che::halfedge(const index_t & he) const +const index_t * che::trigs_ptr() const +{ + return VT; +} + +index_t che::halfedge(const index_t he) const { assert(he < n_half_edges); return VT[he]; } -const index_t & che::twin_he(const index_t & he) const +index_t che::twin_he(const index_t he) const { assert(he < n_half_edges); return OT[he]; } -const index_t & che::edge_u(const index_t & e) const +index_t che::edge_u(const index_t e) const { assert(e < n_edges); return VT[ET[e]]; } -const index_t & che::edge_v(const index_t & e) const +index_t che::edge_v(const index_t e) const { assert(e < n_edges); return VT[he_next(ET[e])]; } -const index_t & che::edge_he_0(const index_t & e) const +index_t che::edge_he_0(const index_t e) const { assert(e < n_edges); return ET[e]; } -const index_t & che::edge_he_1(const index_t & e) const +index_t che::edge_he_1(const index_t e) const { assert(e < n_edges); return OT[ET[e]]; } -const vertex & che::vertex_he(const index_t & he) const +const vertex & che::vertex_he(const index_t he) const { assert(he < n_half_edges); return GT[VT[he]]; } -const vertex & che::vertex_edge_u(const index_t & e) const +const vertex & che::vertex_edge_u(const index_t e) const { assert(e < n_edges); return GT[VT[ET[e]]]; } -const vertex & che::vertex_edge_v(const index_t & e) const +const vertex & che::vertex_edge_v(const index_t e) const { assert(e < n_edges); return GT[VT[he_next(ET[e])]]; } -const index_t & che::evt(const index_t & v) const +index_t che::evt(const index_t v) const { assert(v < n_vertices); return EVT[v]; @@ -623,12 +628,12 @@ const index_t & che::evt(const index_t & v) const // topology methods -che::star_he che::star(const index_t & v) const +che::star_he che::star(const index_t v) const { return {this, v}; } -std::vector che::link(const index_t & v) const +std::vector che::link(const index_t v) const { assert(v < n_vertices); @@ -637,7 +642,7 @@ std::vector che::link(const index_t & v) const if(is_vertex_bound(v)) vlink.push_back(VT[he_next(EVT[v])]); - for(const index_t & he: star(v)) + for(const index_t he: star(v)) vlink.push_back(VT[he_prev(he)]); return vlink; @@ -649,7 +654,7 @@ void che::edge_collapse(const std::vector & sort_edges) // TODO } -void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector & limits, const std::vector & sources, const index_t & k) +void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector & limits, const std::vector & sources, const index_t k) { if(!size(sources)) return; @@ -658,7 +663,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector index_t level = 0; index_t p = 0; - for(const index_t & s: sources) + for(const index_t s: sources) { sorted[p++] = s; toplesets[s] = level; @@ -667,7 +672,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector limits.push_back(0); for(index_t i = 0; i < p; ++i) { - const index_t & v = sorted[i]; + const index_t v = sorted[i]; if(toplesets[v] > level) { @@ -675,7 +680,7 @@ void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector limits.push_back(i); } - for(const index_t & u: link(v)) + for(const index_t u: link(v)) { if(toplesets[u] == NIL) { @@ -708,7 +713,7 @@ std::vector che::bounds() const { vbounds.push_back(v); - for(const index_t & b: boundary(v)) + for(const index_t b: boundary(v)) is_bound[b] = true; } @@ -718,7 +723,7 @@ std::vector che::bounds() const } ///< return a vector of the indices of the boundary where v belongs -std::vector che::boundary(const index_t & v) const +std::vector che::boundary(const index_t v) const { std::vector vbound; @@ -734,13 +739,13 @@ std::vector che::boundary(const index_t & v) const return vbound; } -bool che::is_vertex_bound(const index_t & v) const +bool che::is_vertex_bound(const index_t v) const { assert(v < n_vertices); return EVT[v] != NIL && OT[EVT[v]] == NIL; } -bool che::is_edge_bound(const index_t & e) const +bool che::is_edge_bound(const index_t e) const { assert(e < n_edges); return OT[ET[e]] == NIL; @@ -791,7 +796,7 @@ size_t che::max_degree() const for(index_t v = 0; v < n_vertices; ++v) { d = 0; - for([[maybe_unused]] const index_t & he: star(v)) ++d; + for([[maybe_unused]] const index_t he: star(v)) ++d; d += is_vertex_bound(v); md = std::max(md, d); } @@ -850,7 +855,7 @@ bool che::is_pointcloud() const // operation methods -void che::flip(const index_t & e) +void che::flip(const index_t e) { index_t ha = ET[e]; index_t hb = OT[ha]; @@ -906,7 +911,7 @@ void che::flip(const index_t & e) if(EVT[vd] == he_prev(hb)) EVT[vd] = he_next(ha); } -real_t che::cotan(const index_t & he) const +real_t che::cotan(const index_t he) const { if(he == NIL) return 0; @@ -920,7 +925,7 @@ real_t che::cotan(const index_t & he) const // 4*sqrt(3)*a // q = ---------------- // h1^2+h2^2+h3^2 -real_t che::pdetriq(const index_t & t) const +real_t che::pdetriq(const index_t t) const { index_t he = t * che::mtrig; real_t h[3] = { @@ -931,7 +936,7 @@ real_t che::pdetriq(const index_t & t) const return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } -real_t che::area_trig(const index_t & t) const +real_t che::area_trig(const index_t t) const { index_t he = t * che::mtrig; vertex a = GT[VT[he_next(he)]] - GT[VT[he]]; @@ -940,22 +945,22 @@ real_t che::area_trig(const index_t & t) const return norm(cross(a, b)) / 2; } -real_t che::area_vertex(const index_t & v) const +real_t che::area_vertex(const index_t v) const { real_t area_star = 0; - for(const index_t & he: star(v)) + for(const index_t he: star(v)) area_star += area_trig(he_trig(he)); return area_star / 3; } // The Gauss-Bonnet Scheme -real_t che::mean_curvature(const index_t & v) const +real_t che::mean_curvature(const index_t v) const { real_t h = 0; real_t a = 0; - for(const index_t & he: star(v)) + for(const index_t he: star(v)) { a += area_trig(he_trig(he)); h += norm(GT[VT[he_next(he)]] - GT[v]) * dot(normal(v), normal_he(he)); @@ -967,7 +972,7 @@ real_t che::mean_curvature(const index_t & v) const // protected -void che::init(const vertex * vertices, const index_t & n_v, const index_t * trigs, const index_t & n_f) +void che::init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f) { alloc(n_v, n_f); @@ -1048,8 +1053,8 @@ void che::update_evt_ot_et() size_t ne = 0; for(index_t ohe, he = 0; he < n_half_edges; ++he) { - const index_t & u = VT[he]; - const index_t & v = VT[he_next(he)]; + const index_t u = VT[he]; + const index_t v = VT[he_next(he)]; EVT[u] = he; @@ -1156,7 +1161,7 @@ che * che::load_mesh(const std::string & file_path) // iterator classes methods -che::star_he::star_he(const che * p_mesh, const index_t & p_v): mesh(p_mesh), v(p_v) {} +che::star_he::star_he(const che * p_mesh, const index_t p_v): mesh(p_mesh), v(p_v) {} che::star_he::iterator che::star_he::begin() const { @@ -1168,7 +1173,7 @@ che::star_he::iterator che::star_he::end() const return {nullptr, NIL, NIL}; } -che::star_he::iterator::iterator(const che * p_mesh, const index_t & p_he, const index_t & p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} +che::star_he::iterator::iterator(const che * p_mesh, const index_t p_he, const index_t p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} che::star_he::iterator & che::star_he::iterator::operator ++ () { @@ -1182,7 +1187,7 @@ bool che::star_he::iterator::operator != (const iterator & it) const return he != it.he; } -const index_t & che::star_he::iterator::operator * () +index_t che::star_he::iterator::operator * () { return he; } diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 1fc5497d..f23a6558 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -15,7 +15,7 @@ bool operator<(const border_t & a, const border_t & b) return a.theta > b.theta; } -a_vec normal_face(const std::vector & tmp_vertices, const index_t & a_v, const index_t & b_v, const index_t & c_v) +a_vec normal_face(const std::vector & tmp_vertices, const index_t a_v, const index_t b_v, const index_t c_v) { a_vec a = tmp_vertices[c_v] - tmp_vertices[a_v]; a_vec b = tmp_vertices[b_v] - tmp_vertices[a_v]; @@ -31,7 +31,7 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert vertices.reserve(size(border_vertices)); - for(const index_t & b: border_vertices) + for(const index_t b: border_vertices) { v = mesh->point(b); normal_v = mesh->normal(b); @@ -74,7 +74,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c index_t c = 1; real_t mean_edge = mesh->mean_edge(); - auto gen_vertices = [&mean_edge](std::vector & merge_vertices, std::vector & vertices, const vertex & va, const vertex & vb, const index_t & delta_v = 0) + auto gen_vertices = [&mean_edge](std::vector & merge_vertices, std::vector & vertices, const vertex & va, const vertex & vb, const index_t delta_v = 0) { real_t L = length(va - vb); size_t N = L / mean_edge; @@ -87,7 +87,7 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c } }; - auto add_border_vertices = [&](const index_t & i, const index_t & j, const index_t & delta_v = 0) -> index_t + auto add_border_vertices = [&](const index_t i, const index_t j, const index_t delta_v = 0) -> index_t { index_t end_v = j < i ? j + nb : j; for(index_t v = i; v <= end_v; ++v) diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 10c884a3..034aba21 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -57,7 +57,7 @@ void che_off::read_file(const std::string & file) for(index_t i = 0; i < n; ++i) fscanf(fp, "%u", P + i); - for(const index_t & v: trig_convex_polygon(P, n)) + for(const index_t v: trig_convex_polygon(P, n)) trigs.push_back(v); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 285fbe24..3550ae1d 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -126,14 +126,14 @@ void che_ply::read_file(const std::string & file) for(index_t i = 0; i < nv; ++i) fscanf(fp, "%u", P + i); - for(const index_t & v: trig_convex_polygon(P, nv)) + for(const index_t v: trig_convex_polygon(P, nv)) trigs.push_back(v); } } else // binary_little_endian or binary_big_endian { bool big_endian = format[7] == 'b'; - auto big_to_little = [](char * buffer, const index_t & n) + auto big_to_little = [](char * buffer, const index_t n) { for(index_t i = 0, j = n - 1; i < j; ++i, --j) std::swap(buffer[i], buffer[j]); @@ -229,7 +229,7 @@ void che_ply::read_file(const std::string & file) if(fbytes == 4) P[i] = *((int *) buffer); } - for(const index_t & v: trig_convex_polygon(P, nv)) + for(const index_t v: trig_convex_polygon(P, nv)) trigs.push_back(v); } } @@ -290,7 +290,7 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) { fwrite(&mtrig, 1, 1, fp); - fwrite(&mesh->halfedge(he), sizeof(index_t), che::mtrig, fp); + fwrite(mesh->trigs_ptr(), sizeof(index_t), che::mtrig, fp); } fclose(fp); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index a480109d..bcb75273 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -95,7 +95,7 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) } //fill one hole and fit with biharmonic_interp_2 -void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t & k) +void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t k) { if(old_n_vertices == n_vertices) return; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 45d64abd..294342fd 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -94,7 +94,7 @@ void che_ptx::read_file(const std::string & file) index_t he = 0; - auto add_trig = [&](const index_t & i, const index_t & j, const index_t & k) + auto add_trig = [&](const index_t i, const index_t j, const index_t k) { VT[he++] = i; VT[he++] = j; diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index d10708b4..f7dbaabd 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -5,7 +5,7 @@ namespace gproshan { -simplification::simplification(che * mesh_, const index_t & levels_) +simplification::simplification(che * mesh_, const index_t levels_) { mesh = mesh_; levels = levels_; @@ -35,7 +35,7 @@ void simplification::compute_quadrics() Q[v].zeros(); a_vec p(4); - for(const index_t & he: mesh->star(v)) + for(const index_t he: mesh->star(v)) { n = mesh->normal_he(he); p(0) = n.x(); @@ -58,14 +58,14 @@ void simplification::order_edges(index_t * const & sort_edges, real_t * const & } std::sort(sort_edges, sort_edges + mesh->n_edges, - [&error_edges](const index_t & a, const index_t & b) + [&error_edges](const index_t a, const index_t b) { return error_edges[a] < error_edges[b]; } ); } -real_t simplification::compute_error(const index_t & e) +real_t simplification::compute_error(const index_t e) { vertex ve = create_vertex(e); a_vec v(4); @@ -78,7 +78,7 @@ real_t simplification::compute_error(const index_t & e) return as_scalar(v.t() * (Q[mesh->edge_u(e)] + Q[mesh->edge_v(e)]) * v); } -vertex simplification::create_vertex(const index_t & e) +vertex simplification::create_vertex(const index_t e) { const vertex & va = mesh->vertex_edge_u(e); const vertex & vb = mesh->vertex_edge_v(e); diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 6a35d8d9..6db4b851 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -52,7 +52,7 @@ std::vector grid::operator () (const point & p, int k) const if(iter == end(voxels)) continue; - for(const index_t & v: iter->second) + for(const index_t v: iter->second) q.push({-length(p - points[v]), v}); } @@ -108,17 +108,17 @@ k3tree::~k3tree() delete [] indices.ptr(); } -const int * k3tree::operator [] (const index_t & i) const +const int * k3tree::operator [] (const index_t i) const { return indices[i]; } -const int * k3tree::operator () (const index_t & i) const +const int * k3tree::operator () (const index_t i) const { return indices[i]; } -const int & k3tree::operator () (const index_t & i, const index_t & j) const +const int & k3tree::operator () (const index_t i, const index_t j) const { return indices[i][j]; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index e50e9a1f..2945a760 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -133,7 +133,7 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_trigs = 0; [[maybe_unused]] - const index_t & geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? + const index_t geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? add_mesh(meshes[i], model_mats[i]) : add_pointcloud(meshes[i], model_mats[i]); @@ -190,7 +190,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) } else { - memcpy(tri_idxs, &mesh->halfedge(0), mesh->n_half_edges * sizeof(index_t)); + memcpy(tri_idxs, mesh->trigs_ptr(), mesh->n_half_edges * sizeof(index_t)); } rtcCommitGeometry(geom); diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 64c8c2e2..d1b9ff67 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -58,7 +58,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f std::vector raytracing::raycaster( const uvec2 & windows_size, const mat4 & inv_proj_view, const vertex & cam_pos, - const index_t & samples + const index_t samples ) const { std::vector frame(windows_size.x() * windows_size.y()); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 5da52172..6fa3b31e 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -30,7 +30,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t for(index_t i = 0; i < n_rows; ++i) for(index_t j = 0; j < n_cols; ++j) { - const index_t & v = i * n_cols + j; + const index_t v = i * n_cols + j; const real_t & phi = i * delta_phi; const real_t & theta = j * delta_theta; diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index f64806a9..c9cf8f34 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -54,7 +54,7 @@ bool scene::load_obj(const std::string & file) #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) { - const index_t & v = p.trigs[i].x(); + const index_t v = p.trigs[i].x(); GT[i] = p.vertices[v]; VC[i] = p.vcolors[v]; } @@ -66,7 +66,7 @@ bool scene::load_obj(const std::string & file) #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) { - const index_t & t = p.trigs[i].y(); + const index_t t = p.trigs[i].y(); texcoords[i] = t != NIL ? p.vtexcoords[t] : vec2{-1, -1}; } } @@ -74,8 +74,8 @@ bool scene::load_obj(const std::string & file) #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) { - const index_t & trig = 3 * (i / 3); - const index_t & n = p.trigs[i].z(); + const index_t trig = 3 * (i / 3); + const index_t n = p.trigs[i].z(); VN[i] = n != NIL ? p.vnormals[n] : normalize(cross(GT[trig + 1] - GT[trig], GT[trig + 2] - GT[trig])); } @@ -91,7 +91,7 @@ bool scene::load_obj(const std::string & file) for(index_t i = 0; i < size(objects) - 1; ++i) { const object & obj = objects[i]; - const index_t & n = objects[i + 1].begin; + const index_t n = objects[i + 1].begin; for(index_t t = obj.begin; t < n; t += 3) trig_mat[t / 3] = obj.material_id; diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 2b029a1c..8db32f1b 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -12,23 +12,23 @@ partitions::partitions(index_t * s): sorted(s) splits.push_back(0); } -void partitions::add(const index_t & size) +void partitions::add(const index_t size) { return splits.push_back(size + splits.back()); } -size_t partitions::size(const index_t & i) const +size_t partitions::size(const index_t i) const { return splits[i + 1] - splits[i]; } -partitions::part partitions::operator () (const index_t & i) const +partitions::part partitions::operator () (const index_t i) const { assert(i > 0 && i < std::size(splits)); return {splits[i], splits[i + 1], sorted}; } -partitions::operator index_t * const & () const +partitions::operator index_t * () const { return sorted; } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index 8c2965f0..b7184cac 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -95,7 +95,7 @@ void che_viewer::update_vbo_geometry() if(!mesh->is_pointcloud()) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[4]); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), &mesh->halfedge(0), GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->n_half_edges * sizeof(index_t), mesh->trigs_ptr(), GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } @@ -169,7 +169,7 @@ void che_viewer::update_instances_positions(const std::vector & translat glBindVertexArray(0); } -const vertex & che_viewer::selected_point(const index_t & i) const +const vertex & che_viewer::selected_point(const index_t i) const { return mesh->point(selected[i]); } @@ -222,7 +222,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) selected_xyz.clear(); selected_xyz.reserve(size(selected)); - for(const index_t & v: selected) + for(const index_t v: selected) selected_xyz.push_back(mesh->point(v)); } @@ -238,7 +238,7 @@ void che_viewer::select(const uvec2 & pos, const uvec2 & windows_size, const mat { rt::random rnd(pos.x(), pos.y()); const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos, rnd); - const index_t & v = rt_embree->closest_vertex(cam_pos, dir); + const index_t v = rt_embree->closest_vertex(cam_pos, dir); if(v != NIL) selected.push_back(v); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index cc1de6bc..7f071225 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -316,7 +316,7 @@ void viewer::imgui() if(ImGui::Button("add selected points as lights")) { - for(const index_t & v: mesh.selected) + for(const index_t v: mesh.selected) if(!render_params.add_light({vec3(mesh.model_mat * (mesh->point(v), 1))})) break; } @@ -567,7 +567,7 @@ bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) return true; } -bool viewer::remove_mesh(const index_t & idx) +bool viewer::remove_mesh(const index_t idx) { if(size(meshes) == 1) return false; @@ -696,10 +696,10 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod float xscale, yscale; glfwGetWindowContentScale(window, &xscale, &yscale); - const index_t & ix = xpos * xscale; - const index_t & iy = ypos * yscale; + const index_t ix = xpos * xscale; + const index_t iy = ypos * yscale; const int & cols = m_window_split[size(view->meshes)].y(); - const index_t & idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; + const index_t idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; if(idx_mesh < size(view->meshes)) view->idx_selected_mesh = idx_mesh; @@ -1070,8 +1070,8 @@ bool viewer::m_select_border_vertices(viewer * view) { view->check_apply_all_meshes([&](che_viewer & mesh) { - for(const index_t & b: mesh->bounds()) - for(const index_t & v: mesh->boundary(b)) + for(const index_t b: mesh->bounds()) + for(const index_t v: mesh->boundary(b)) mesh.selected.push_back(v); }); From 1d7a585e4ad44a2a10b47406ec139489a2579611 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 20:21:23 +0100 Subject: [PATCH 0927/1018] gproshan: using const real_t instead of const real_t & --- include/gproshan/features/key_components.h | 2 +- include/gproshan/features/key_points.h | 4 ++-- include/gproshan/geodesics/geodesics.h | 2 +- include/gproshan/geodesics/geodesics_ptp.h | 2 +- include/gproshan/geometry/convex_hull.h | 4 ++-- include/gproshan/laplacian/fairing_taubin.h | 2 +- include/gproshan/mdict/basis.h | 2 +- include/gproshan/mdict/basis_cosine.h | 6 +++--- include/gproshan/mdict/basis_dct.h | 2 +- include/gproshan/mdict/msparse_coding.h | 3 ++- include/gproshan/mdict/patch.h | 22 ++++++++++----------- include/gproshan/mesh/che.h | 7 ++++--- include/gproshan/mesh/che_fill_hole.h | 6 +++--- include/gproshan/mesh/che_sphere.h | 2 +- include/gproshan/mesh/quaternion.h | 4 ++-- include/gproshan/viewer/camera.h | 2 +- src/gproshan/app_viewer.cpp | 6 +++--- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/features/key_points.cpp | 4 ++-- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cu | 2 +- src/gproshan/geometry/convex_hull.cpp | 4 ++-- src/gproshan/laplacian/fairing_taubin.cpp | 2 +- src/gproshan/mdict/basis.cpp | 2 +- src/gproshan/mdict/basis_cosine.cpp | 6 +++--- src/gproshan/mdict/basis_dct.cpp | 2 +- src/gproshan/mdict/msparse_coding.cpp | 7 ++++++- src/gproshan/mdict/patch.cpp | 20 +++++++++---------- src/gproshan/mesh/che.cpp | 17 ++++++++++------ src/gproshan/mesh/che_fill_hole.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 8 ++++---- src/gproshan/scenes/scanner.cpp | 4 ++-- src/gproshan/viewer/camera.cpp | 4 ++-- 35 files changed, 91 insertions(+), 79 deletions(-) diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index 83d38f7f..065b719b 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -21,7 +21,7 @@ class key_components std::map comp_idx; public: - key_components(che * mesh, const std::vector & kps, const real_t & r); + key_components(che * mesh, const std::vector & kps, const real_t r); ~key_components(); index_t operator()(const index_t i); operator const size_t & () const; diff --git a/include/gproshan/features/key_points.h b/include/gproshan/features/key_points.h index 7170cc1b..24779f8d 100644 --- a/include/gproshan/features/key_points.h +++ b/include/gproshan/features/key_points.h @@ -15,11 +15,11 @@ class key_points std::vector is_kp; public: - key_points(che * mesh, const real_t & percent = 0.10); + key_points(che * mesh, const real_t percent = 0.10); operator const std::vector & () const; private: - void compute_kps_areas(che * mesh, const real_t & percent); + void compute_kps_areas(che * mesh, const real_t percent); }; diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index a99871a8..075eb362 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -70,7 +70,7 @@ class geodesics private: void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources); void run_heat_method(che * mesh, const std::vector & sources); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index b07f3bc5..2e43e4cb 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -38,7 +38,7 @@ struct is_ok const real_t * error = nullptr; __host_device__ - bool operator()(const real_t & val) const; + bool operator()(const real_t val) const; __host_device__ bool operator()(const index_t val) const; diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index a7e2abcb..4e94ae07 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -20,8 +20,8 @@ class convex_hull std::vector CH; ///< convex hull points clockwise public: - convex_hull(const std::vector & points, const real_t & precision = 1000); - convex_hull(const vertex * points, const size_t & n_points, const real_t & precision = 1000); + convex_hull(const std::vector & points, const real_t precision = 1000); + convex_hull(const vertex * points, const size_t & n_points, const real_t precision = 1000); operator const std::vector & () const; private: diff --git a/include/gproshan/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h index 3edc3f57..122546cc 100644 --- a/include/gproshan/laplacian/fairing_taubin.h +++ b/include/gproshan/laplacian/fairing_taubin.h @@ -14,7 +14,7 @@ class fairing_taubin : public fairing real_t step; public: - fairing_taubin(const real_t & step_ = 0.001); + fairing_taubin(const real_t step_ = 0.001); virtual ~fairing_taubin() = default; private: diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index d53867ce..45c884b1 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -19,7 +19,7 @@ class basis size_t _dim; public: - basis(const real_t & r, const size_t & d); + basis(const real_t r, const size_t & d); virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) = 0; diff --git a/include/gproshan/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h index f36c0add..3b9a6874 100644 --- a/include/gproshan/mdict/basis_cosine.h +++ b/include/gproshan/mdict/basis_cosine.h @@ -16,14 +16,14 @@ class basis_cosine: public basis size_t n_freq; ///< frequency public: - basis_cosine(const size_t & nr, const size_t & nf, const real_t & r = 0); + basis_cosine(const size_t & nr, const size_t & nf, const real_t r = 0); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); private: void plot_basis(std::ostream & os); void plot_atoms(std::ostream & os, const a_vec & A); - a_vec cosine(const a_vec & x, const a_vec & y, const real_t & c, const real_t & alpha); - void cosine(std::ostream & os, const real_t & c, const real_t & alpha); + a_vec cosine(const a_vec & x, const a_vec & y, const real_t c, const real_t alpha); + void cosine(std::ostream & os, const real_t c, const real_t alpha); }; diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 07b67c31..2e8c97f3 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -15,7 +15,7 @@ class basis_dct: public basis size_t n_freq; ///< frequency public: - basis_dct(const size_t & n, const real_t & r = 1); + basis_dct(const size_t & n, const real_t r = 1); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b); real_t freq(const index_t idx); diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index 8e9316cf..81cc2644 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -66,10 +66,11 @@ class msparse_coding virtual ~msparse_coding(); - const real_t & operator[](const index_t i) const; + real_t operator[](const index_t i) const; index_t draw_patches(const index_t p) const; operator const std::string & () const; + operator const real_t * () const; real_t execute(); void load_mask(const std::vector * vertices, const index_t * clusters); diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 1625e29e..7bc74f6f 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -51,7 +51,7 @@ class patch void init( che * mesh, ///< input mesh. const index_t v, ///< center vertex of the patch. const size_t & n_toplevels, ///< number of toplevels to jet fitting. - const real_t & radio_, ///< euclidean radio in XY of the patch. + const real_t radio_, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); @@ -65,16 +65,16 @@ class patch real_t & geo_radio, che * mesh, const index_t v, - const real_t & delta, - const real_t & sum_thres, - const real_t & area_thres, - const real_t & area_mesh + const real_t delta, + const real_t sum_thres, + const real_t area_thres, + const real_t area_mesh ); - void init_random(const vertex & c, const a_mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr); + void init_random(const vertex & c, const a_mat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr); void recover_radial_disjoint(che * mesh, - const real_t & radio_, + const real_t radio_, const index_t v); void transform(); @@ -98,11 +98,11 @@ class patch const a_vec normal(); bool is_covered( bool * covered); -// void save(const real_t & radio, const size_t & imsize, CImgList & imlist); +// void save(const real_t radio, const size_t & imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t p); - void scale_xyz(const real_t & radio_f); - void iscale_xyz(const real_t & radio_f); + void scale_xyz(const real_t radio_f); + void iscale_xyz(const real_t radio_f); bool add_vertex_by_trigs( vertex & n, std::vector & N, double thr_angle, @@ -126,7 +126,7 @@ class patch /// Gather the vertices filter by radio in the local coordinates require initialize T and x. void gather_vertices( che * mesh, const index_t v, - const real_t & radio, + const real_t radio, index_t * toplevel ); bool exists(index_t idx); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 96b84b03..030231eb 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -84,7 +84,8 @@ class che vertex gradient(const index_t v, const real_t * f); // vertex color methods - const real_t & heatmap(const index_t v) const; + const real_t * heatmap_ptr() const; + real_t heatmap(const index_t v) const; real_t & heatmap(const index_t v); const rgb_t & rgb(const index_t v) const; rgb_t & rgb(const index_t v); @@ -93,8 +94,8 @@ class che // update methods void reload(); - mat4 normalize_sphere(const real_t & r = 1) const; - mat4 normalize_box(const real_t & side = 2) const; + mat4 normalize_sphere(const real_t r = 1) const; + mat4 normalize_box(const real_t side = 2) const; che * merge(const che * mesh, const std::vector & com_vertices = {}); void update_vertices(const vertex * positions, const size_t & n = 0, const index_t v_i = 0); void update_heatmap(const real_t * hm = nullptr); diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 85f73f5d..9efd6167 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -38,7 +38,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t & length, const std::array & neighbors, const bool & o) + a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool & o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -81,7 +81,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t & length, const std::array & neighbors, const bool & o, const a_vec & normal) + a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool & o, const a_vec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -125,7 +125,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti che * fill_hole_front_angles_without_projection(che * mesh, std::vector & front_vertices); -che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow = false); +che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t & max_iter, bool is_grow = false); che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index); diff --git a/include/gproshan/mesh/che_sphere.h b/include/gproshan/mesh/che_sphere.h index ce780e63..e16a8207 100644 --- a/include/gproshan/mesh/che_sphere.h +++ b/include/gproshan/mesh/che_sphere.h @@ -11,7 +11,7 @@ namespace gproshan { class che_sphere : public che { public: - che_sphere(const real_t & r = 1, const size_t & n = 6); + che_sphere(const real_t r = 1, const size_t & n = 6); }; diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 8465d2ac..7b87b678 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -28,10 +28,10 @@ class quaternion const quaternion & operator = (real_t s); const quaternion & operator = (const vertex & v); real_t & operator [] (int index); - const real_t & operator [] (int index) const; + real_t operator [] (int index) const; void toMatrix(real_t Q[4][4]) const; real_t & re(void); - const real_t & re(void) const; + real_t re(void) const; vertex & im(void); const vertex & im(void) const; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 7268dd87..f149bf29 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -28,7 +28,7 @@ class camera real_t far = 1000; public: - static mat4 perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far); + static mat4 perspective(const real_t fovy, const real_t aspect, const real_t near, const real_t far); mat4 perspective(); mat4 look_at(const quaternion & r); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 63d9381f..d404d30b 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -660,7 +660,7 @@ bool app_viewer::process_msparse_coding(viewer * p_view) real_t max_error = msc.execute(); gproshan_log_var(max_error); - mesh->update_heatmap(&msc[0]); + mesh->update_heatmap(msc); mesh->update_normals(); } @@ -737,7 +737,7 @@ bool app_viewer::process_mask(viewer * p_view) msc.init_radial_feature_patches(); //dict.init_voronoi_patches(); - mesh->update_heatmap(&msc[0]); + mesh->update_heatmap(msc); std::string f_points = tmp_file_path(std::string(msc) + ".rsampl"); a_vec points_out; @@ -987,7 +987,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) auto push = [&](const uvec3 & p) { neigs[p.x()] = {p.y(), p.z()}; - const real_t & angle = 21; + const real_t angle = 21; if(angle <= M_PI) front.push({angle, p.x()}); }; diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index 2865b8e7..fc26f6d1 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -9,7 +9,7 @@ namespace gproshan { -key_components::key_components(che * mesh, const std::vector & kps, const real_t & r): radio(r) +key_components::key_components(che * mesh, const std::vector & kps, const real_t r): radio(r) { n_vertices = mesh->n_vertices; diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 2a539d63..9dc264ad 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -9,7 +9,7 @@ namespace gproshan { -key_points::key_points(che * mesh, const real_t & percent) +key_points::key_points(che * mesh, const real_t percent) { compute_kps_areas(mesh, percent); } @@ -22,7 +22,7 @@ key_points::operator const std::vector & () const /// Efficient approach for interest points detection in non-rigid shapes /// Cristian Jose Lopez Del Alamo; Luciano Arnaldo Romero Calla; Lizeth Joseline Fuentes Perez /// DOI: 10.1109/CLEI.2015.7359459 -void key_points::compute_kps_areas(che * mesh, const real_t & percent) +void key_points::compute_kps_areas(che * mesh, const real_t percent) { std::vector > face_areas(mesh->n_trigs); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index c5706e9a..b35d80de 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -114,7 +114,7 @@ void geodesics::execute(che * mesh, const std::vector & sources, const } } -void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t & radio, const fm_function_t & fun) +void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t radio, const fm_function_t & fun) { CHE cmesh(mesh); diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 0f423692..cc765a5d 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -263,7 +263,7 @@ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_ } __host_device__ -bool is_ok::operator()(const real_t & val) const +bool is_ok::operator()(const real_t val) const { return val < PTP_TOL; } diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 3daa9740..81dc339e 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -8,9 +8,9 @@ namespace gproshan { -convex_hull::convex_hull(const std::vector & points, const real_t & precision): convex_hull(points.data(), size(points), precision) {} +convex_hull::convex_hull(const std::vector & points, const real_t precision): convex_hull(points.data(), size(points), precision) {} -convex_hull::convex_hull(const vertex * points, const size_t & n_points, const real_t & precision) +convex_hull::convex_hull(const vertex * points, const size_t & n_points, const real_t precision) { std::vector points2d(n_points); diff --git a/src/gproshan/laplacian/fairing_taubin.cpp b/src/gproshan/laplacian/fairing_taubin.cpp index 484f1a14..64b40810 100644 --- a/src/gproshan/laplacian/fairing_taubin.cpp +++ b/src/gproshan/laplacian/fairing_taubin.cpp @@ -7,7 +7,7 @@ namespace gproshan { -fairing_taubin::fairing_taubin(const real_t & step_): step(step_) {} +fairing_taubin::fairing_taubin(const real_t step_): step(step_) {} void fairing_taubin::compute(che * mesh) { diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index 3497c500..e659ef5d 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -6,7 +6,7 @@ namespace gproshan::mdict { -basis::basis(const real_t & r, const size_t & d): _radio(r), _dim(d) {} +basis::basis(const real_t r, const size_t & d): _radio(r), _dim(d) {} real_t & basis::radio() { diff --git a/src/gproshan/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp index e2543c0c..fdabd6da 100644 --- a/src/gproshan/mdict/basis_cosine.cpp +++ b/src/gproshan/mdict/basis_cosine.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_cosine::basis_cosine(const size_t & nr, const size_t & nf, const real_t & r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} +basis_cosine::basis_cosine(const size_t & nr, const size_t & nf, const real_t r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { @@ -53,12 +53,12 @@ void basis_cosine::plot_atoms(std::ostream & os, const a_vec & A) } } -a_vec basis_cosine::cosine(const a_vec & x, const a_vec & y, const real_t & c, const real_t & alpha) +a_vec basis_cosine::cosine(const a_vec & x, const a_vec & y, const real_t c, const real_t alpha) { return cos(c * (alpha * x + (1 - alpha) * y)); } -void basis_cosine::cosine(std::ostream & os, const real_t & c, const real_t & alpha) +void basis_cosine::cosine(std::ostream & os, const real_t c, const real_t alpha) { os << "cos( " << c << " * (" << alpha << " * v * cos(u) + ( 1 - " << alpha << ") * v * sin(u)))"; } diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index b2cd87c0..4a9d78e0 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_dct::basis_dct(const size_t & n, const real_t & r): basis(r, n * n), n_freq(n) {} +basis_dct::basis_dct(const size_t & n, const real_t r): basis(r, n * n), n_freq(n) {} void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index bf88a023..38975113 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -42,6 +42,11 @@ msparse_coding::operator const std::string & () const return key_name; } +msparse_coding::operator const real_t * () const +{ + return dist; +} + void msparse_coding::load_mask() { //std::string f_mask = tmp_file_path(mesh->name_size() + '_' + std::to_string(avg_p) + '_' + std::to_string(percent) + '_' + std::to_string(radio) + ".msk"); @@ -1007,7 +1012,7 @@ index_t msparse_coding::sample(const index_t s) return s; } -const real_t & msparse_coding::operator[](const index_t i) const +real_t msparse_coding::operator[](const index_t i) const { assert(i < mesh->n_vertices); return dist[i]; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 0db32ffb..6de816a8 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -34,7 +34,7 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); real_t patch::nyquist_factor = 0.5; -void patch::init(che * mesh, const index_t v, const size_t & n_toplevels, const real_t & radio_, index_t * _toplevel) +void patch::init(che * mesh, const index_t v, const size_t & n_toplevels, const real_t radio_, index_t * _toplevel) { radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -143,7 +143,7 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ return added; } -void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, const real_t & max_radio, const real_t & percent, const real_t & fr) +void patch::init_random(const vertex & c, const a_mat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr) { this->radio = radio; this->T = T; @@ -179,7 +179,7 @@ void patch::init_random(const vertex & c, const a_mat & T, const real_t & radio, } } -void patch::recover_radial_disjoint(che * mesh, const real_t & radio_, const index_t v) +void patch::recover_radial_disjoint(che * mesh, const real_t radio_, const index_t v) { // for small meshes 6000 0.e-5 // for others 2.e-5 @@ -232,10 +232,10 @@ void patch::init_radial_disjoint( real_t & euc_radio, real_t & geo_radio, che * mesh, const index_t v, - const real_t & delta, - const real_t & sum_thres, - const real_t & area_thres, - const real_t & area_mesh + const real_t delta, + const real_t sum_thres, + const real_t area_thres, + const real_t area_mesh ) { radio = -INFINITY; @@ -506,14 +506,14 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector< } } -void patch::scale_xyz(const real_t & radio_f) +void patch::scale_xyz(const real_t radio_f) { real_t factor = radio_f/radio; xyz = factor * xyz; } -void patch::iscale_xyz(const real_t & radio_f) +void patch::iscale_xyz(const real_t radio_f) { real_t factor = radio_f/radio; xyz = xyz / factor; @@ -549,7 +549,7 @@ void patch::gather_vertices(che * mesh, const index_t v, const size_t & n_toplev } } -void patch::gather_vertices(che * mesh, const index_t v, const real_t & radio, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t v, const real_t radio, index_t * toplevel) { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index d472393c..2c749f9c 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -164,7 +164,12 @@ vertex che::gradient(const index_t v, const real_t * f) // vertex color methods -const real_t & che::heatmap(const index_t v) const +const real_t * che::heatmap_ptr() const +{ + return VHC; +} + +real_t che::heatmap(const index_t v) const { assert(v < n_vertices); return VHC[v]; @@ -209,7 +214,7 @@ void che::reload() init(filename); } -mat4 che::normalize_sphere(const real_t & r) const +mat4 che::normalize_sphere(const real_t r) const { vertex center; #pragma omp parallel for @@ -230,14 +235,14 @@ mat4 che::normalize_sphere(const real_t & r) const #pragma omp parallel for reduction(+: sigma_dist) for(index_t v = 0; v < n_vertices; ++v) { - const real_t & diff = mean_dist - length(GT[v] - center); + const real_t diff = mean_dist - length(GT[v] - center); sigma_dist += diff * diff; } sigma_dist = sqrt(sigma_dist / n_vertices); mat4 model_mat; - const real_t & scale = r / (mean_dist + sigma_dist); + const real_t scale = r / (mean_dist + sigma_dist); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; center *= -scale; @@ -249,7 +254,7 @@ mat4 che::normalize_sphere(const real_t & r) const return model_mat; } -mat4 che::normalize_box(const real_t & side) const +mat4 che::normalize_box(const real_t side) const { vertex pmin = INFINITY; vertex pmax = 0; @@ -269,7 +274,7 @@ mat4 che::normalize_box(const real_t & side) const mat4 model_mat; - const real_t & scale = side / std::max({pmax.x() - pmin.x(), pmax.y() - pmin.y(), pmax.z() - pmin.z()}); + const real_t scale = side / std::max({pmax.x() - pmin.x(), pmax.y() - pmin.y(), pmax.z() - pmin.z()}); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; const vertex & translate = - scale * (pmax + pmin) / 2; diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index f23a6558..70b1dfb5 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -564,7 +564,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti return size(trigs) == 0 ? nullptr : new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); } -che * fill_hole_front_angles(std::vector & vertices, const real_t & length, const vertex & normal, const size_t & max_iter, bool is_grow) +che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t & max_iter, bool is_grow) { size_t p_iter = max_iter; real_t perimeter = 0.0; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 294342fd..410237f0 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -140,7 +140,7 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ { const vertex & v = mesh->point(i); const rgb_t & c = mesh->rgb(i); - const real_t & h = mesh->heatmap(i); + const real_t h = mesh->heatmap(i); fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) h, c.r, c.g, c.b ); } diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 1a00ff73..9710f5fc 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -10,7 +10,7 @@ namespace gproshan { -che_sphere::che_sphere(const real_t & r, const size_t & n) +che_sphere::che_sphere(const real_t r, const size_t & n) { filename = "sphere"; diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index 91a93ac0..e5accf4d 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -41,7 +41,7 @@ real_t & quaternion::operator [] (int index) return v[index]; } -const real_t & quaternion::operator [] (int index) const +real_t quaternion::operator [] (int index) const { return v[index]; } @@ -59,7 +59,7 @@ real_t & quaternion::re() return s; } -const real_t & quaternion::re() const +real_t quaternion::re() const { return s; } @@ -141,8 +141,8 @@ void quaternion::operator /= (real_t c) // Hamilton product quaternion quaternion::operator * (const quaternion & q) const { - const real_t & s1(s); - const real_t & s2(q.s); + const real_t s1(s); + const real_t s2(q.s); const vertex & v1(v); const vertex & v2(q.v); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 6fa3b31e..22a48ac8 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -32,8 +32,8 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t { const index_t v = i * n_cols + j; - const real_t & phi = i * delta_phi; - const real_t & theta = j * delta_theta; + const real_t phi = i * delta_phi; + const real_t theta = j * delta_theta; const vertex & dir = {std::sin(theta) * std::sin(phi), std::cos(theta), std::sin(theta) * std::cos(phi)}; const rt::eval_hit & h = rt->intersect(cam_pos, dir); diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 9d704e38..4e1421a2 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -31,9 +31,9 @@ mat4 camera::perspective() return perspective(fovy, aspect, near, far); } -mat4 camera::perspective(const real_t & fovy, const real_t & aspect, const real_t & near, const real_t & far) +mat4 camera::perspective(const real_t fovy, const real_t aspect, const real_t near, const real_t far) { - const real_t & tan_fovy_2 = std::tan(fovy * M_PI / 360); + const real_t tan_fovy_2 = std::tan(fovy * M_PI / 360); mat4 P; P(0, 0) = 1 / (aspect * tan_fovy_2); From 82e8f81cde07ab1fb95293c73488f954477d6cd7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 20:41:00 +0100 Subject: [PATCH 0928/1018] gproshan: update rgb_t access methods --- include/gproshan/mesh/che.h | 3 ++- include/gproshan/raytracing/utils.h | 3 +-- src/gproshan/mesh/che.cpp | 7 ++++++- src/gproshan/mesh/che_off.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 3 ++- src/gproshan/mesh/che_pts.cpp | 2 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_xyz.cpp | 2 +- 8 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 030231eb..ff438d97 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -87,7 +87,8 @@ class che const real_t * heatmap_ptr() const; real_t heatmap(const index_t v) const; real_t & heatmap(const index_t v); - const rgb_t & rgb(const index_t v) const; + const rgb_t * rgb_ptr() const; + rgb_t rgb(const index_t v) const; rgb_t & rgb(const index_t v); vertex color(const index_t v) const; vertex shading_color(const index_t f, const float & u, const float & v) const; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 13ad51f7..de2ab0a0 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -98,8 +98,7 @@ struct t_eval_hit if(!mesh.n_trigs) // pointcloud { - const che::rgb_t & c = mesh.VC[aprimID]; - Kd = {T(c.r), T(c.g), T(c.b)}; + Kd = {T(mesh.VC[aprimID].r), T(mesh.VC[aprimID].g), T(mesh.VC[aprimID].b)}; Kd /= 255; normal = mesh.VN[aprimID]; return; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 2c749f9c..f0dee733 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -181,7 +181,12 @@ real_t & che::heatmap(const index_t v) return VHC[v]; } -const che::rgb_t & che::rgb(const index_t v) const +const che::rgb_t * che::rgb_ptr() const +{ + return VC; +} + +che::rgb_t che::rgb(const index_t v) const { assert(v < n_vertices); return VC[v]; diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 034aba21..0e60d090 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -98,7 +98,7 @@ void che_off::write_file(const che * mesh, const std::string & file, const che_o if(off == COFF || off == NCOFF) { - const rgb_t & c = mesh->rgb(i); + const rgb_t c = mesh->rgb(i); fprintf(fp, " %hhu %hhu %hhu 1", c.r, c.g, c.b); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 3550ae1d..f80d7d8d 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -279,11 +279,12 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "property list uchar uint vertex_index\n"); fprintf(fp, "end_header\n"); + const che::rgb_t * p_rgb = mesh->rgb_ptr(); for(index_t v = 0; v < mesh->n_vertices; ++v) { fwrite(&mesh->point(v), sizeof(vertex), 1, fp); fwrite(&mesh->normal(v), sizeof(vertex), 1, fp); - if(color) fwrite(&mesh->rgb(v), sizeof(rgb_t), 1, fp); + if(color) fwrite(p_rgb + v, sizeof(rgb_t), 1, fp); } unsigned char mtrig = che::mtrig; diff --git a/src/gproshan/mesh/che_pts.cpp b/src/gproshan/mesh/che_pts.cpp index 1860eff1..86bf0c5d 100644 --- a/src/gproshan/mesh/che_pts.cpp +++ b/src/gproshan/mesh/che_pts.cpp @@ -52,7 +52,7 @@ void che_pts::write_file(const che * mesh, const std::string & file) for(index_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->point(i); - const rgb_t & c = mesh->rgb(i); + const rgb_t c = mesh->rgb(i); fprintf(fp, "%f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); fprintf(fp, " %d ", int(mesh->heatmap(i) * 4095) - 2048); fprintf(fp, "%hhu %hhu %hhu\n", c.r, c.g, c.b); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 410237f0..d75a0649 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -139,7 +139,7 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ for(size_t i = 0; i < mesh->n_vertices; ++i) { const vertex & v = mesh->point(i); - const rgb_t & c = mesh->rgb(i); + const rgb_t c = mesh->rgb(i); const real_t h = mesh->heatmap(i); fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) h, c.r, c.g, c.b ); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 349c0d10..c3d0117d 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -58,7 +58,7 @@ void che_xyz::write_file(const che * mesh, const std::string & file, const bool fprintf(fp, "%f %f %f", (float) v.x(), (float) v.y(), (float) v.z()); if(color) { - const rgb_t & c = mesh->rgb(i); + const rgb_t c = mesh->rgb(i); fprintf(fp, " %hhu %hhu %hhu", c.r, c.g, c.b); } fprintf(fp, "\n"); From f1b79de56f67caac7b4d56f4c66b3604476722fb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 7 Dec 2023 22:54:12 +0100 Subject: [PATCH 0929/1018] gproshan: cleaning up const & --- include/gproshan/app_viewer.h | 2 +- include/gproshan/features/descriptor.h | 2 +- include/gproshan/features/key_components.h | 2 +- include/gproshan/geodesics/geodesics.h | 6 ++-- include/gproshan/geodesics/geodesics_ptp.h | 8 ++--- .../gproshan/geodesics/test_geodesics_ptp.h | 16 ++++----- include/gproshan/geometry/convex_hull.h | 2 +- include/gproshan/geometry/points.h | 2 +- include/gproshan/laplacian/fairing_spectral.h | 2 +- include/gproshan/laplacian/laplacian.h | 2 +- include/gproshan/mdict/basis.h | 4 +-- include/gproshan/mdict/basis_cosine.h | 2 +- include/gproshan/mdict/basis_dct.h | 2 +- include/gproshan/mdict/mdict.h | 32 ++++++++--------- include/gproshan/mdict/msparse_coding.h | 6 ++-- include/gproshan/mdict/patch.h | 8 ++--- include/gproshan/mesh/che.h | 14 ++++---- include/gproshan/mesh/che_fill_hole.h | 8 ++--- include/gproshan/mesh/che_poisson.h | 4 +-- include/gproshan/mesh/che_ptx.h | 2 +- include/gproshan/mesh/che_sphere.h | 2 +- include/gproshan/mesh/simplification.h | 2 +- include/gproshan/pointcloud/knn.h | 8 ++--- include/gproshan/raytracing/embree.h | 2 +- include/gproshan/raytracing/utils.h | 2 +- include/gproshan/scenes/scanner.h | 4 +-- include/gproshan/util.h | 8 ++--- include/gproshan/viewer/camera.h | 6 ++-- include/gproshan/viewer/che_viewer.h | 6 ++-- include/gproshan/viewer/frame.h | 2 +- include/gproshan/viewer/viewer.h | 4 +-- src/gproshan/app_viewer.cpp | 2 +- src/gproshan/features/descriptor.cpp | 2 +- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/geodesics/geodesics.cpp | 4 +-- src/gproshan/geodesics/geodesics_ptp.cpp | 6 ++-- src/gproshan/geodesics/geodesics_ptp.cu | 4 +-- src/gproshan/geodesics/test_geodesics_ptp.cpp | 16 ++++----- src/gproshan/geometry/convex_hull.cpp | 2 +- src/gproshan/laplacian/fairing_spectral.cpp | 2 +- src/gproshan/laplacian/laplacian.cpp | 2 +- src/gproshan/mdict/basis.cpp | 4 +-- src/gproshan/mdict/basis_cosine.cpp | 2 +- src/gproshan/mdict/basis_dct.cpp | 2 +- src/gproshan/mdict/mdict.cpp | 34 +++++++++---------- src/gproshan/mdict/msparse_coding.cpp | 2 +- src/gproshan/mdict/patch.cpp | 6 ++-- src/gproshan/mesh/che.cpp | 18 +++++----- src/gproshan/mesh/che_fill_hole.cpp | 10 +++--- src/gproshan/mesh/che_img.cpp | 4 +-- src/gproshan/mesh/che_ply.cpp | 4 +-- src/gproshan/mesh/che_poisson.cpp | 4 +-- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 2 +- src/gproshan/mesh/simplification.cpp | 2 +- src/gproshan/pointcloud/knn.cpp | 8 ++--- src/gproshan/raytracing/embree.cpp | 4 +-- src/gproshan/raytracing/optix.cu | 2 +- src/gproshan/scenes/scanner.cpp | 4 +-- src/gproshan/util.cpp | 8 ++--- src/gproshan/viewer/camera.cpp | 6 ++-- src/gproshan/viewer/che_viewer.cpp | 6 ++-- src/gproshan/viewer/frame.cpp | 2 +- src/gproshan/viewer/scene_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 18 +++++----- 65 files changed, 185 insertions(+), 185 deletions(-) diff --git a/include/gproshan/app_viewer.h b/include/gproshan/app_viewer.h index 72ad620b..3eb9ce7a 100644 --- a/include/gproshan/app_viewer.h +++ b/include/gproshan/app_viewer.h @@ -49,7 +49,7 @@ class app_viewer : public viewer double time; public: - app_viewer(const char * title = "gproshan", const int & width = 1600, const int & height = 900); + app_viewer(const char * title = "gproshan", const int width = 1600, const int height = 900); virtual ~app_viewer(); int main(int nargs, const char ** args); diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index 30b53a2f..28a98683 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -21,7 +21,7 @@ class descriptor a_mat features; public: - descriptor(const signature & sig, const che * mesh, const size_t & n_eigs); + descriptor(const signature & sig, const che * mesh, const size_t n_eigs); size_t n_eigs(); ///< return true if the features were computed diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index 065b719b..94353bc1 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -24,7 +24,7 @@ class key_components key_components(che * mesh, const std::vector & kps, const real_t r); ~key_components(); index_t operator()(const index_t i); - operator const size_t & () const; + operator size_t () const; private: void compute_kcs(che * mesh, const std::vector & kps); diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index 075eb362..afd1a707 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -50,7 +50,7 @@ class geodesics size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; - const size_t & n_vertices; ///< Number of vertices, const reference + const size_t n_vertices; ///< Number of vertices, const reference public: geodesics( che * mesh, ///< input triangular mesh. @@ -65,12 +65,12 @@ class geodesics real_t radio() const; index_t farthest() const; size_t n_sorted_index() const; - void copy_sorted_index(index_t * indexes, const size_t & n) const; + void copy_sorted_index(index_t * indexes, const size_t n) const; void normalize(); private: void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t radio, const fm_function_t & fun); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const real_t radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources); void run_heat_method(che * mesh, const std::vector & sources); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 2e43e4cb..f0488a3e 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -52,7 +52,7 @@ struct ptp_out_t real_t * dist; index_t * clusters; - ptp_out_t(real_t *const & d, index_t *const & c = nullptr); + ptp_out_t(real_t *const d, index_t *const c = nullptr); }; struct toplesets_t @@ -67,7 +67,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); -void normalize_ptp(real_t * dist, const size_t & n); +void normalize_ptp(real_t * dist, const size_t n); template @@ -182,7 +182,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, } #ifdef __CUDACC__ - const size_t & n_vertices = limits.back(); + const size_t n_vertices = limits.back(); cudaMemcpy(dist[0], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(dist[1], h_dist, sizeof(T) * n_vertices, cudaMemcpyHostToDevice); if(sorted) @@ -196,7 +196,7 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, } #endif - const int & max_iter = size(limits) << 1; + const int max_iter = size(limits) << 1; int iter = -1; index_t count = 0; diff --git a/include/gproshan/geodesics/test_geodesics_ptp.h b/include/gproshan/geodesics/test_geodesics_ptp.h index 5fb98fd3..302c0ff7 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.h +++ b/include/gproshan/geodesics/test_geodesics_ptp.h @@ -10,20 +10,20 @@ namespace gproshan { /// Execute performance and accuracy test for ptp algorithm on cpu and gpu. -void main_test_geodesics_ptp(const int & nargs, const char ** args); +void main_test_geodesics_ptp(const int nargs, const char ** args); -double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int n_test); -double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); +double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test); -double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test); #ifdef GPROSHAN_CUDA -double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test); +double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test); -double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test); +double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test); /// Return an array with the error per iteration. /// Starting to store (position 0) errors after number of toplesets. @@ -44,9 +44,9 @@ double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vect /// Exact geodesics computed using MeshLP https://github.com/areslp/matlab/tree/master/MeshLP/MeshLP, /// Geodesics code: http://code.google.com/p/geodesic/ -real_t * load_exact_geodesics(const std::string & file, const size_t & n); +real_t * load_exact_geodesics(const std::string & file, const size_t n); -real_t compute_error(const real_t * dist, const real_t * exact, const size_t & n, const size_t & s); +real_t compute_error(const real_t * dist, const real_t * exact, const size_t n, const size_t s); } // namespace gproshan diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index 4e94ae07..a93ef5a8 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -21,7 +21,7 @@ class convex_hull public: convex_hull(const std::vector & points, const real_t precision = 1000); - convex_hull(const vertex * points, const size_t & n_points, const real_t precision = 1000); + convex_hull(const vertex * points, const size_t n_points, const real_t precision = 1000); operator const std::vector & () const; private: diff --git a/include/gproshan/geometry/points.h b/include/gproshan/geometry/points.h index 00ed5d03..13a877b1 100644 --- a/include/gproshan/geometry/points.h +++ b/include/gproshan/geometry/points.h @@ -9,7 +9,7 @@ namespace gproshan { template -std::vector > sampling_4points(const size_t & n, const vec & a, const vec & b, const vec & c, const vec & d) +std::vector > sampling_4points(const size_t n, const vec & a, const vec & b, const vec & c, const vec & d) { std::vector > points; points.reserve((n + 1) * (n + 1)); diff --git a/include/gproshan/laplacian/fairing_spectral.h b/include/gproshan/laplacian/fairing_spectral.h index fdafd4b1..d293e7a9 100644 --- a/include/gproshan/laplacian/fairing_spectral.h +++ b/include/gproshan/laplacian/fairing_spectral.h @@ -14,7 +14,7 @@ class fairing_spectral : public fairing size_t n_eigs; public: - fairing_spectral(const size_t & n_eigs_ = 100); + fairing_spectral(const size_t n_eigs_ = 100); virtual ~fairing_spectral() = default; private: diff --git a/include/gproshan/laplacian/laplacian.h b/include/gproshan/laplacian/laplacian.h index 9ddd1796..aac59444 100644 --- a/include/gproshan/laplacian/laplacian.h +++ b/include/gproshan/laplacian/laplacian.h @@ -11,7 +11,7 @@ namespace gproshan { void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A); -size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k); +size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t k); } // namespace gproshan diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 45c884b1..9cf73b06 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -19,13 +19,13 @@ class basis size_t _dim; public: - basis(const real_t r, const size_t & d); + basis(const real_t r, const size_t d); virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) = 0; virtual real_t freq(const index_t idx) = 0; real_t & radio(); - const size_t & dim() const; + size_t dim() const; void plot_basis(); void plot_atoms(const a_mat & A); void plot_patch(const a_mat & A, const a_mat & xyz, const index_t p); diff --git a/include/gproshan/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h index 3b9a6874..45ea49e7 100644 --- a/include/gproshan/mdict/basis_cosine.h +++ b/include/gproshan/mdict/basis_cosine.h @@ -16,7 +16,7 @@ class basis_cosine: public basis size_t n_freq; ///< frequency public: - basis_cosine(const size_t & nr, const size_t & nf, const real_t r = 0); + basis_cosine(const size_t nr, const size_t nf, const real_t r = 0); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); private: diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 2e8c97f3..338c7b4e 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -15,7 +15,7 @@ class basis_dct: public basis size_t n_freq; ///< frequency public: - basis_dct(const size_t & n, const real_t r = 1); + basis_dct(const size_t n, const real_t r = 1); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b); real_t freq(const index_t idx); diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 7b84dfa0..91a3ff09 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -21,44 +21,44 @@ struct locval_t }; -void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t & L); +void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t L); -a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t & L); +a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t L); -void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); +void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k); // DENSE -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L); +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L); -a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L); -a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask); +a_vec OMP(const a_vec & x, const a_mat & D, const size_t L); +a_vec OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask); -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L); +a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t L); -void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k); +void KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k); // MESH DENSE -a_vec OMP(const patch & p, const a_mat & A, const size_t & L); -a_vec OMP(const patch & p, const a_mat & A, const size_t & L); +a_vec OMP(const patch & p, const a_mat & A, const size_t L); +a_vec OMP(const patch & p, const a_mat & A, const size_t L); -a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t & L); -a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t & L); +a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t L); +a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t L); -void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k); +void KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k); // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t & L); +void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t L); -a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t & L); +a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t L); -void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k); +void sp_KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k); } // namespace gproshan::mdict diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index 81cc2644..e39bcc6f 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -59,9 +59,9 @@ class msparse_coding static size_t T; ///< factor of patches' size, default 5 toplesets. public: - msparse_coding( che *const & _mesh, ///< pointer to input mesh. - basis *const &_phi_basis, ///< pointer to continuous basis. - const params & p ///< + msparse_coding( che * _mesh, ///< pointer to input mesh. + basis * _phi_basis, ///< pointer to continuous basis. + const params & p ///< ); virtual ~msparse_coding(); diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 7bc74f6f..70982364 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -50,14 +50,14 @@ class patch void init( che * mesh, ///< input mesh. const index_t v, ///< center vertex of the patch. - const size_t & n_toplevels, ///< number of toplevels to jet fitting. + const size_t n_toplevels, ///< number of toplevels to jet fitting. const real_t radio_, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); void init_disjoint(che * mesh, const index_t v, - const size_t & n_toplevels, + const size_t n_toplevels, std::vector & _vertices, index_t * _toplevel = nullptr); @@ -98,7 +98,7 @@ class patch const a_vec normal(); bool is_covered( bool * covered); -// void save(const real_t radio, const size_t & imsize, CImgList & imlist); +// void save(const real_t radio, const size_t imsize, CImgList & imlist); void update_heights(real_t & min, real_t & max, bool flag); void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t p); void scale_xyz(const real_t radio_f); @@ -119,7 +119,7 @@ class patch /// Gather the vertices needed to compute the jet_fit_directions of the patch. void gather_vertices( che * mesh, const index_t v, - const size_t & n_toplevels, + const size_t n_toplevels, index_t * toplevel ); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index ff438d97..048c7deb 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -68,7 +68,7 @@ class che public: che(const che & mesh); - che(const size_t & n_v = 0, const size_t & n_f = 0); + che(const size_t n_v = 0, const size_t n_f = 0); che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); virtual ~che(); @@ -77,7 +77,7 @@ class che vertex & point(const index_t v); const vertex & normal(const index_t v) const; vertex & normal(const index_t v); - vertex shading_normal(const index_t f, const float & u, const float & v) const; + vertex shading_normal(const index_t f, const float u, const float v) const; vertex normal_trig(const index_t f) const; vertex normal_he(const index_t he) const; vertex gradient_he(const index_t he, const real_t * f) const; @@ -91,21 +91,21 @@ class che rgb_t rgb(const index_t v) const; rgb_t & rgb(const index_t v); vertex color(const index_t v) const; - vertex shading_color(const index_t f, const float & u, const float & v) const; + vertex shading_color(const index_t f, const float u, const float v) const; // update methods void reload(); mat4 normalize_sphere(const real_t r = 1) const; mat4 normalize_box(const real_t side = 2) const; che * merge(const che * mesh, const std::vector & com_vertices = {}); - void update_vertices(const vertex * positions, const size_t & n = 0, const index_t v_i = 0); + void update_vertices(const vertex * positions, const size_t n = 0, const index_t v_i = 0); void update_heatmap(const real_t * hm = nullptr); void update_normals(); void invert_normals(); void multiplicate_vertices(); void remove_vertices(const std::vector & vertices); void remove_non_manifold_vertices(); - void set_head_vertices(index_t * head, const size_t & n); + void set_head_vertices(index_t * head, const size_t n); // half edge access methods triangular trigs and navigation const index_t * trigs_ptr() const; @@ -159,7 +159,7 @@ class che protected: void init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); void init(const std::string & file); - void alloc(const size_t & n_v, const size_t & n_f); + void alloc(const size_t n_v, const size_t n_f); void free(); virtual void read_file(const std::string & file); @@ -169,7 +169,7 @@ class che void update_eht(); public: - static std::vector trig_convex_polygon(const index_t * P, const size_t & n); + static std::vector trig_convex_polygon(const index_t * P, const size_t n); static che * load_mesh(const std::string & file_path); friend struct CHE; diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 9efd6167..b4c0a294 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -115,17 +115,17 @@ struct border_t bool operator<(const border_t & a, const border_t & b); -void poisson(che * mesh, const size_t & old_n_vertices); +void poisson(che * mesh, const size_t old_n_vertices); -std::vector * fill_all_holes(che * mesh, const size_t & max_iter = 1000); +std::vector * fill_all_holes(che * mesh, const size_t max_iter = 1000); -std::tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter = 1000); +std::tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t max_iter = 1000); che * fill_hole_front_angles_test(che * mesh, std::vector & front_vertices, size_t p_iter, bool & is_grow); che * fill_hole_front_angles_without_projection(che * mesh, std::vector & front_vertices); -che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t & max_iter, bool is_grow = false); +che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t max_iter, bool is_grow = false); che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index); diff --git a/include/gproshan/mesh/che_poisson.h b/include/gproshan/mesh/che_poisson.h index 04c1f822..85121507 100644 --- a/include/gproshan/mesh/che_poisson.h +++ b/include/gproshan/mesh/che_poisson.h @@ -11,9 +11,9 @@ namespace gproshan { /** Solve (-1)^k L(AL)^(k-1) X = (-1)^k A^(-1) B */ -void poisson(che * mesh, const size_t & old_n_vertices, index_t k); +void poisson(che * mesh, const size_t old_n_vertices, index_t k); -void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t k); +void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n_vertices, const std::vector & border_vertices, const index_t k); } // namespace gproshan diff --git a/include/gproshan/mesh/che_ptx.h b/include/gproshan/mesh/che_ptx.h index 4ff38b98..d9d02605 100644 --- a/include/gproshan/mesh/che_ptx.h +++ b/include/gproshan/mesh/che_ptx.h @@ -13,7 +13,7 @@ class che_ptx : public che public: che_ptx(const std::string & file); - static void write_file(const che * mesh, const std::string & file, const size_t & n_rows, const size_t & n_cols); + static void write_file(const che * mesh, const std::string & file, const size_t n_rows, const size_t n_cols); private: diff --git a/include/gproshan/mesh/che_sphere.h b/include/gproshan/mesh/che_sphere.h index e16a8207..9f284df7 100644 --- a/include/gproshan/mesh/che_sphere.h +++ b/include/gproshan/mesh/che_sphere.h @@ -11,7 +11,7 @@ namespace gproshan { class che_sphere : public che { public: - che_sphere(const real_t r = 1, const size_t & n = 6); + che_sphere(const real_t r = 1, const size_t n = 6); }; diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 57b87e22..855ef509 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -26,7 +26,7 @@ class simplification void execute(); void compute_quadrics(); real_t compute_error(const index_t e); - void order_edges(index_t * const & sort_edges, real_t * const & error_edges); + void order_edges(index_t * sort_edges, real_t * error_edges); vertex create_vertex(const index_t e); }; diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index d2432057..6fb29afc 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -10,7 +10,7 @@ #include -inline gproshan::uvec3 hash(gproshan::vec3 p, const float & res) +inline gproshan::uvec3 hash(gproshan::vec3 p, const float res) { p = (res - 1) * (0.5f * p + 1); return {(unsigned int) p.x(), (unsigned int) p.y(), (unsigned int) p.z()}; @@ -42,7 +42,7 @@ class grid std::unordered_map > voxels; public: - grid(const point * pc, const size_t & n_points, const mat4 & transform); + grid(const point * pc, const size_t n_points, const mat4 & transform); ~grid() = default; std::vector operator () (const point & p, int k) const; @@ -55,12 +55,12 @@ class k3tree flann::Matrix indices; public: - k3tree(const point * pc, const size_t & n_points, const size_t & k = 8, const std::vector & query = {}); + k3tree(const point * pc, const size_t n_points, const size_t k = 8, const std::vector & query = {}); ~k3tree(); const int * operator [] (const index_t i) const; const int * operator () (const index_t i) const; - const int & operator () (const index_t i, const index_t j) const; + int operator () (const index_t i, const index_t j) const; }; diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 3eba4515..8848957e 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -44,7 +44,7 @@ class embree : public raytracing embree( const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud = false, - const float & pcr = 0.01 + const float pcr = 0.01 ); virtual ~embree(); diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index de2ab0a0..33c5a886 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -203,7 +203,7 @@ struct t_eval_hit template __host_device__ -vec eval_li(const t_eval_hit & hit, const light & ambient, const light * lights, const int & n_lights, const vec & eye, Occluded occluded) +vec eval_li(const t_eval_hit & hit, const light & ambient, const light * lights, const int n_lights, const vec & eye, Occluded occluded) { const vec & v = normalize(eye - hit.position); const vec & n = hit.normal; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 3d809a8e..2de2ff25 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -9,9 +9,9 @@ namespace gproshan { -che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos); +che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos); -che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); } // namespace gproshan diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 75ba4733..9447db6f 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -74,13 +74,13 @@ struct partitions::part }; -void copy_real_t_array(float * destination, const float * source, const size_t & n_elem); +void copy_real_t_array(float * destination, const float * source, const size_t n_elem); -void copy_real_t_array(float * destination, const double * source, const size_t & n_elem); +void copy_real_t_array(float * destination, const double * source, const size_t n_elem); -void copy_real_t_array(double * destination, const float * source, const size_t & n_elem); +void copy_real_t_array(double * destination, const float * source, const size_t n_elem); -void copy_real_t_array(double * destination, const double * source, const size_t & n_elem); +void copy_real_t_array(double * destination, const double * source, const size_t n_elem); } // namespace gproshan diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index f149bf29..86948f85 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -33,14 +33,14 @@ class camera mat4 perspective(); mat4 look_at(const quaternion & r); quaternion current_rotation() const; - void mouse(const bool & press, const double & x, const double & y, const int & w, const int & h); - void motion(const double & x, const double & y, const int & w, const int & h); + void mouse(const bool & press, const double x, const double y, const int w, const int h); + void motion(const double x, const double y, const int w, const int h); void zoom_in(); void zoom_out(); real_t zoom() const; private: - quaternion click_to_sphere(const double & x, const double & y, const int & w, const int & h); + quaternion click_to_sphere(const double x, const double y, const int w, const int h); friend std::ostream & operator << (std::ostream & os, const camera & cam); friend std::istream & operator >> (std::istream & is, camera & cam); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 038e247e..88df3a5a 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -65,9 +65,9 @@ class che_viewer che_viewer(che * m); virtual ~che_viewer(); - che *& operator -> (); - che *const & operator -> () const; - operator che *& (); + che * operator -> (); + const che * operator -> () const; + operator che * (); void update(); void update_model_mat(); diff --git a/include/gproshan/viewer/frame.h b/include/gproshan/viewer/frame.h index 05cb5bd0..a507895a 100644 --- a/include/gproshan/viewer/frame.h +++ b/include/gproshan/viewer/frame.h @@ -41,7 +41,7 @@ class frame vec4 * map_pbo(bool cuda = false); void unmap_pbo(bool cuda = false); - bool resize(const size_t & w, const size_t & h); + bool resize(const size_t w, const size_t h); void display(); }; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index a83f9e86..b78aea00 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -104,12 +104,12 @@ class viewer char status_message[1024] = {}; public: - viewer(const char * title = "gproshan", const int & width = 1600, const int & height = 900); + viewer(const char * title = "gproshan", const int width = 1600, const int height = 900); virtual ~viewer(); che_viewer & selected_mesh(); void add_menu(const std::string & str, const std::vector & vprocesses); - int add_process(const char * name, const function_t & f, const int & key = -1); + int add_process(const char * name, const function_t & f, const int key = -1); bool add_mesh(che * p_mesh, const bool & reset_normals = true); bool remove_mesh(const index_t idx); bool pop_mesh(); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index d404d30b..7539500a 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -14,7 +14,7 @@ using namespace gproshan::mdict; namespace gproshan { -app_viewer::app_viewer(const char * title, const int & width, const int & height): viewer(title, width, height) {} +app_viewer::app_viewer(const char * title, const int width, const int height): viewer(title, width, height) {} app_viewer::~app_viewer() { diff --git a/src/gproshan/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp index f92f2e71..b396966e 100644 --- a/src/gproshan/features/descriptor.cpp +++ b/src/gproshan/features/descriptor.cpp @@ -7,7 +7,7 @@ namespace gproshan { -descriptor::descriptor(const signature & sig, const che * mesh, const size_t & n_eigs) +descriptor::descriptor(const signature & sig, const che * mesh, const size_t n_eigs) { if(!eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs)) return; diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index fc26f6d1..9e5816d6 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -41,7 +41,7 @@ index_t key_components::operator()(const index_t i) return comp_idx[find(i)]; } -key_components::operator const size_t & () const +key_components::operator size_t () const { return n_comp; } diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index b35d80de..44a34d6c 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -73,7 +73,7 @@ size_t geodesics::n_sorted_index() const return n_sorted; } -void geodesics::copy_sorted_index(index_t * indexes, const size_t & n) const +void geodesics::copy_sorted_index(index_t * indexes, const size_t n) const { assert(n <= n_sorted); memcpy(indexes, sorted_index, n * sizeof(index_t)); @@ -114,7 +114,7 @@ void geodesics::execute(che * mesh, const std::vector & sources, const } } -void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t & n_iter, const real_t radio, const fm_function_t & fun) +void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const real_t radio, const fm_function_t & fun) { CHE cmesh(mesh); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 588f83ce..f4568ea7 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -9,13 +9,13 @@ namespace gproshan { -ptp_out_t::ptp_out_t(real_t *const & d, index_t *const & c): dist(d), clusters(c) {} +ptp_out_t::ptp_out_t(real_t * d, index_t * c): dist(d), clusters(c) {} void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) { CHE h_mesh(mesh); - const size_t & n_vertices = h_mesh.n_vertices; + const size_t n_vertices = h_mesh.n_vertices; index_t * inv = nullptr; if(coalescence) @@ -83,7 +83,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c delete [] inv; } -void normalize_ptp(real_t * dist, const size_t & n) +void normalize_ptp(real_t * dist, const size_t n) { real_t max_d = 0; diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index cc765a5d..c6285bc2 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -15,7 +15,7 @@ namespace gproshan { double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) { CHE h_mesh(mesh); - const size_t & n_vertices = h_mesh.n_vertices; + const size_t n_vertices = h_mesh.n_vertices; index_t * inv = nullptr; if(coalescence) @@ -148,7 +148,7 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { CHE h_mesh(mesh); - const size_t & n_vertices = h_mesh.n_vertices; + const size_t n_vertices = h_mesh.n_vertices; cudaDeviceReset(); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 0417462f..14453ef4 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -11,7 +11,7 @@ namespace gproshan { -void main_test_geodesics_ptp(const int & nargs, const char ** args) +void main_test_geodesics_ptp(const int nargs, const char ** args) { if(nargs < 4) { @@ -237,7 +237,7 @@ void main_test_geodesics_ptp(const int & nargs, const char ** args) fclose(ftable); } -double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) +double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int n_test) { double t, seconds = INFINITY; @@ -254,7 +254,7 @@ double test_fast_marching(real_t & error, const real_t * exact, che * mesh, cons return seconds; } -double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test) { double t, seconds = INFINITY; @@ -272,7 +272,7 @@ double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std: return seconds; } -double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) +double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test) { double t, st, ptime; ptime = stime = INFINITY; @@ -295,7 +295,7 @@ double test_heat_method_cholmod(real_t & error, double & stime, const real_t * e #ifdef GPROSHAN_CUDA -double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int & n_test) +double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test) { double t, seconds = INFINITY; @@ -313,7 +313,7 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std: return seconds; } -double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int & n_test) +double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test) { double t, st, ptime; ptime = stime = INFINITY; @@ -338,7 +338,7 @@ double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact #endif // GPROSHAN_CUDA -real_t * load_exact_geodesics(const std::string & file, const size_t & n) +real_t * load_exact_geodesics(const std::string & file, const size_t n) { std::ifstream is(file); @@ -353,7 +353,7 @@ real_t * load_exact_geodesics(const std::string & file, const size_t & n) return exact; } -real_t compute_error(const real_t * dist, const real_t * exact, const size_t & n, const size_t & s) +real_t compute_error(const real_t * dist, const real_t * exact, const size_t n, const size_t s) { real_t error = 0; diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 81dc339e..00b1e205 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -10,7 +10,7 @@ namespace gproshan { convex_hull::convex_hull(const std::vector & points, const real_t precision): convex_hull(points.data(), size(points), precision) {} -convex_hull::convex_hull(const vertex * points, const size_t & n_points, const real_t precision) +convex_hull::convex_hull(const vertex * points, const size_t n_points, const real_t precision) { std::vector points2d(n_points); diff --git a/src/gproshan/laplacian/fairing_spectral.cpp b/src/gproshan/laplacian/fairing_spectral.cpp index 38f77aa5..2617d2bb 100644 --- a/src/gproshan/laplacian/fairing_spectral.cpp +++ b/src/gproshan/laplacian/fairing_spectral.cpp @@ -7,7 +7,7 @@ namespace gproshan { -fairing_spectral::fairing_spectral(const size_t & n_eigs_): n_eigs(n_eigs_) {} +fairing_spectral::fairing_spectral(const size_t n_eigs_): n_eigs(n_eigs_) {} void fairing_spectral::compute(che * mesh) { diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp index 429736da..6df5e018 100644 --- a/src/gproshan/laplacian/laplacian.cpp +++ b/src/gproshan/laplacian/laplacian.cpp @@ -48,7 +48,7 @@ void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) A(v, v) = mesh->area_vertex(v); } -size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t & k) +size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t k) { laplacian(mesh, L, A); diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index e659ef5d..e57da9e2 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -6,14 +6,14 @@ namespace gproshan::mdict { -basis::basis(const real_t r, const size_t & d): _radio(r), _dim(d) {} +basis::basis(const real_t r, const size_t d): _radio(r), _dim(d) {} real_t & basis::radio() { return _radio; } -const size_t & basis::dim() const +size_t basis::dim() const { return _dim; } diff --git a/src/gproshan/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp index fdabd6da..a09faedf 100644 --- a/src/gproshan/mdict/basis_cosine.cpp +++ b/src/gproshan/mdict/basis_cosine.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_cosine::basis_cosine(const size_t & nr, const size_t & nf, const real_t r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} +basis_cosine::basis_cosine(const size_t nr, const size_t nf, const real_t r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 4a9d78e0..5a5e04a3 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_dct::basis_dct(const size_t & n, const real_t r): basis(r, n * n), n_freq(n) {} +basis_dct::basis_dct(const size_t n, const real_t r): basis(r, n * n), n_freq(n) {} void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) { diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index f10c6852..f1ba6551 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -22,7 +22,7 @@ std::ostream & operator << (std::ostream & os, const locval_t & lc) return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; } -void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t & L) +void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t L) { const auto & [aa, selected_atoms] = _OMP(x, D, L); @@ -33,7 +33,7 @@ void OMP(std::vector & alpha, const a_vec & x, const index_t i, const } } -a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t & L) +a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t L) { locval.clear(); @@ -55,7 +55,7 @@ a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & return a_sp_mat(DI, DV, D.n_cols, X.n_cols); } -void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) +void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) { size_t m = D.n_cols; size_t M = X.n_cols; @@ -108,7 +108,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) // DENSE -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L) +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L) { arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; @@ -139,7 +139,7 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) return NIL; } -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) +std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask) { arma::uvec selected_atoms(L); @@ -162,7 +162,7 @@ std::tuple _OMP(const a_vec & x, const a_mat & D, const size_ return {aa, selected_atoms.head(l)}; } -a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) +a_vec OMP(const a_vec & x, const a_mat & D, const size_t L) { a_vec alpha(D.n_cols, arma::fill::zeros); @@ -172,7 +172,7 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L) return alpha; } -a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_vec & mask) +a_vec OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask) { a_vec alpha(D.n_cols, arma::fill::zeros); @@ -182,7 +182,7 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t & L, const arma::uchar_ return alpha; } -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) +a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t L) { a_mat alpha(D.n_cols, X.n_cols); #pragma omp parallel for @@ -194,7 +194,7 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t & L) return alpha; } -void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) +void KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) { arma::uvec omega; a_mat alpha, R, E, U, V; @@ -224,12 +224,12 @@ void KSVD(a_mat & D, const a_mat & X, const size_t & L, size_t k) // MESH DENSE -a_vec OMP(const patch & p, const a_mat & A, const size_t & L) +a_vec OMP(const patch & p, const a_mat & A, const size_t L) { return OMP(p.xyz.row(2).t(), p.phi * A, L); } -a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) +a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t L) { arma::uchar_vec mask(A.n_cols); @@ -239,7 +239,7 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t & L) return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } -a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t & L) +a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t L) { a_mat alpha(A.n_cols, size(patches)); @@ -250,7 +250,7 @@ a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat return alpha; } -a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t & L) +a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t L) { a_mat alpha(A.n_cols, size(patches)); @@ -261,7 +261,7 @@ a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t return alpha; } -void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k) +void KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k) { size_t K = A.n_rows; @@ -313,12 +313,12 @@ void KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_ // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t & L) +void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t L) { OMP(alpha, p.xyz.row(2).t(), i, p.phi * A, L); } -a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t & L) +a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t L) { locval.clear(); @@ -340,7 +340,7 @@ a_sp_mat OMP_all(std::vector & locval, const std::vector & patc return a_sp_mat(DI, DV, A.n_cols, size(patches)); } -void sp_KSVD(a_mat & A, const std::vector & patches, const size_t & L, size_t k) +void sp_KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k) { size_t K = A.n_rows; diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 38975113..69e99612 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -19,7 +19,7 @@ size_t msparse_coding::L = 12; size_t msparse_coding::K = 10; size_t msparse_coding::T = 5; -msparse_coding::msparse_coding(che *const & _mesh, basis *const & _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) +msparse_coding::msparse_coding(che * _mesh, basis * _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) { A.eye(phi_basis->dim(), m_params.n_atoms); dist = new real_t[mesh->n_vertices]; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 6de816a8..619ad2a3 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -34,7 +34,7 @@ typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); real_t patch::nyquist_factor = 0.5; -void patch::init(che * mesh, const index_t v, const size_t & n_toplevels, const real_t radio_, index_t * _toplevel) +void patch::init(che * mesh, const index_t v, const size_t n_toplevels, const real_t radio_, index_t * _toplevel) { radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -46,7 +46,7 @@ void patch::init(che * mesh, const index_t v, const size_t & n_toplevels, const if(!_toplevel) delete [] toplevel; } -void patch::init_disjoint(che * mesh, const index_t v, const size_t & n_toplevels, std::vector & _vertices, index_t * _toplevel) +void patch::init_disjoint(che * mesh, const index_t v, const size_t n_toplevels, std::vector & _vertices, index_t * _toplevel) { radio = 1; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -524,7 +524,7 @@ const a_vec patch::normal() return T.col(2); } -void patch::gather_vertices(che * mesh, const index_t v, const size_t & n_toplevels, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t v, const size_t n_toplevels, index_t * toplevel) { if(size(vertices)) vertices.clear(); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index f0dee733..60b3cafc 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -65,7 +65,7 @@ che::che(const che & mesh) memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); } -che::che(const size_t & n_v, const size_t & n_f) +che::che(const size_t n_v, const size_t n_f) { alloc(n_v, n_f); } @@ -107,7 +107,7 @@ vertex & che::normal(const index_t v) return VN[v]; } -vertex che::shading_normal(const index_t f, const float & u, const float & v) const +vertex che::shading_normal(const index_t f, const float u, const float v) const { const index_t he = f * che::mtrig; return normalize(u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]); @@ -204,7 +204,7 @@ vertex che::color(const index_t v) const return VC[v]; } -vertex che::shading_color(const index_t f, const float & u, const float & v) const +vertex che::shading_color(const index_t f, const float u, const float v) const { const index_t he = f * che::mtrig; return u * color(VT[he]) + v * color(VT[he + 1]) + (1 - u - v) * color(VT[he + 2]); @@ -294,8 +294,8 @@ mat4 che::normalize_box(const real_t side) const ///< vcommon correspond to the first vertices of the mesh with indices to the main mesh (this) che * che::merge(const che * mesh, const std::vector & vcommon) { - const size_t & n_vcommon = size(vcommon); - const size_t & n_vnew = mesh->n_vertices - n_vcommon; + const size_t n_vcommon = size(vcommon); + const size_t n_vnew = mesh->n_vertices - n_vcommon; che * new_mesh = new che(n_vertices + n_vnew, n_trigs + mesh->n_trigs); @@ -320,7 +320,7 @@ che * che::merge(const che * mesh, const std::vector & vcommon) return new_mesh; } -void che::update_vertices(const vertex * positions, const size_t & n, const index_t v_i) +void che::update_vertices(const vertex * positions, const size_t n, const index_t v_i) { if(!positions) return; memcpy(GT + v_i, positions, sizeof(vertex) * (!n ? n_vertices : n)); @@ -542,7 +542,7 @@ void che::remove_non_manifold_vertices() gproshan_debug(removing vertex); } -void che::set_head_vertices(index_t * head, const size_t & n) +void che::set_head_vertices(index_t * head, const size_t n) { for(index_t v, i = 0; i < n; ++i) { @@ -1002,7 +1002,7 @@ void che::init(const std::string & file) update_eht(); } -void che::alloc(const size_t & n_v, const size_t & n_f) +void che::alloc(const size_t n_v, const size_t n_f) { rw(n_vertices) = n_v; rw(n_trigs) = n_f; @@ -1121,7 +1121,7 @@ void che::update_eht() // static -std::vector che::trig_convex_polygon(const index_t * P, const size_t & n) +std::vector che::trig_convex_polygon(const index_t * P, const size_t n) { std::vector trigs; trigs.reserve(che::mtrig * (n - 2)); diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 70b1dfb5..f5c8b1b1 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -22,7 +22,7 @@ a_vec normal_face(const std::vector & tmp_vertices, const index_t a_v, co return normalise(cross(a,b)); } -che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vertices, const size_t & max_iter = 1000) +che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vertices, const size_t max_iter = 1000) { std::vector vertices; vertex normal, normal_v, edge_v, v; @@ -58,7 +58,7 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert return fill_hole_front_angles(vertices, mesh->mean_edge(), normal, max_iter); } -che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, const size_t & max_iter, const std::vector > & split_indices = {}) +che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, const size_t max_iter, const std::vector > & split_indices = {}) { std::vector vertices[2]; std::vector merge_vertices[2]; @@ -255,7 +255,7 @@ void split_border(std::vector > & , che * mesh, cons } } -std::vector * fill_all_holes(che * mesh, const size_t & max_iter) +std::vector * fill_all_holes(che * mesh, const size_t max_iter) { gproshan_error(holes); std::vector * border_vertices; @@ -276,7 +276,7 @@ std::vector * fill_all_holes(che * mesh, const size_t & max_iter) return border_vertices; } -std::tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t & max_iter) +std::tuple *, che **> fill_all_holes_meshes(che * mesh, const size_t max_iter) { std::vector * border_vertices = nullptr; che ** holes = nullptr; @@ -564,7 +564,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti return size(trigs) == 0 ? nullptr : new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); } -che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t & max_iter, bool is_grow) +che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t max_iter, bool is_grow) { size_t p_iter = max_iter; real_t perimeter = 0.0; diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 40307fd1..5b9cfeb7 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -41,8 +41,8 @@ void che_img::read_file(const std::string & file) VT[he++] = i * img.height() + j - 1; } - const int & c = img.width() - i - 1; - const int & r = img.height() - j - 1; + const int c = img.width() - i - 1; + const int r = img.height() - j - 1; GT[v] = {real_t(i), real_t(j), real_t(img.spectrum() == 1 ? - img(c, r) : 0)}; diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index f80d7d8d..043d5269 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -45,7 +45,7 @@ void che_ply::read_file(const std::string & file) char line[512], type[32], str[32], format[32], element[32]; - auto add_vproperty = [&vbytes](size_t & p, index_t & i, const size_t & nb) + auto add_vproperty = [&vbytes](size_t & p, index_t & i, const size_t nb) { p = nb; i = vbytes; @@ -70,7 +70,7 @@ void che_ply::read_file(const std::string & file) if(str[0] == 'p' && element[0] == 'v') // property vertex { sscanf(line, "%*s %s %s", type, str); - const size_t & nb = bytes[type]; + const size_t nb = bytes[type]; switch(str[0]) { diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index bcb75273..612ab747 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -8,7 +8,7 @@ namespace gproshan { -void poisson(che * mesh, const size_t & old_n_vertices, index_t k) +void poisson(che * mesh, const size_t old_n_vertices, index_t k) { if(!k) return; @@ -95,7 +95,7 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) } //fill one hole and fit with biharmonic_interp_2 -void biharmonic_interp_2(che * mesh, const size_t & old_n_vertices, const size_t & n_vertices, const std::vector & border_vertices, const index_t k) +void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n_vertices, const std::vector & border_vertices, const index_t k) { if(old_n_vertices == n_vertices) return; diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index d75a0649..402ac942 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -120,7 +120,7 @@ void che_ptx::read_file(const std::string & file) rw(n_trigs) = he / che::mtrig; } -void che_ptx::write_file(const che * mesh, const std::string & file, const size_t & n_rows, const size_t & n_cols) +void che_ptx::write_file(const che * mesh, const std::string & file, const size_t n_rows, const size_t n_cols) { FILE * fp = fopen((file + ".ptx").c_str(), "wb"); assert(fp); diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 9710f5fc..2eca3b4b 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -10,7 +10,7 @@ namespace gproshan { -che_sphere::che_sphere(const real_t r, const size_t & n) +che_sphere::che_sphere(const real_t r, const size_t n) { filename = "sphere"; diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index f7dbaabd..26f21a43 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -48,7 +48,7 @@ void simplification::compute_quadrics() } } -void simplification::order_edges(index_t * const & sort_edges, real_t * const & error_edges) +void simplification::order_edges(index_t * sort_edges, real_t * error_edges) { #pragma omp parallel for for(index_t e = 0; e < mesh->n_edges; ++e) diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 6db4b851..8f59a702 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -8,7 +8,7 @@ namespace gproshan::knn { -grid::grid(const point * pc, const size_t & n_points, const mat4 & transform): points(n_points) +grid::grid(const point * pc, const size_t n_points, const mat4 & transform): points(n_points) { double build_time = 0; @@ -72,7 +72,7 @@ std::vector grid::operator () (const point & p, int k) const ///< Implementation using flann, by default compute all knn -k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, const std::vector & query) +k3tree::k3tree(const point * pc, const size_t n_points, const size_t k, const std::vector & query) { double time_build, time_query; @@ -85,7 +85,7 @@ k3tree::k3tree(const point * pc, const size_t & n_points, const size_t & k, cons TIC(time_query); const point * q = size(query) ? query.data() : pc; - const size_t & n_results = size(query) ? size(query) : n_points; + const size_t n_results = size(query) ? size(query) : n_points; flann::Matrix mq((real_t *) q, n_results, 3); @@ -118,7 +118,7 @@ const int * k3tree::operator () (const index_t i) const return indices[i]; } -const int & k3tree::operator () (const index_t i, const index_t j) const +int k3tree::operator () (const index_t i, const index_t j) const { return indices[i][j]; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 2945a760..929f24cb 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -67,7 +67,7 @@ embree::embree() rtcSetDeviceErrorFunction(rtc_device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float & pcr): embree() +embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float pcr): embree() { pc_radius = pcr; build_bvh(meshes, model_mats, pointcloud); @@ -267,7 +267,7 @@ bool embree::closesthit_radiance( vertex & color, hit.normal = flat ? r.normal() : hit.normal; color = eval_li(hit, params.ambient, params.lights, params.n_lights, params.cam_pos, - [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool { ray_hit ro(position, wi, 1e-3f, light_dist - 1e-3f); return occluded(ro); diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 000d85c3..fdb44caa 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -65,7 +65,7 @@ extern "C" __global__ void __closesthit__radiance() vec3 & ray_dir = trace[3]; color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, - [&](const vec3 & position, const vec3 & wi, const float & light_dist) -> bool + [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool { uint32_t occluded = 1; optixTrace( optix_params.traversable, diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 22a48ac8..7b59c9e9 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan { -che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos) +che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos) { che * mesh_ptx = new che(n_cols * n_rows); @@ -57,7 +57,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t & n_rows, const size_t return mesh_ptx; } -che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t & n_rows, const size_t & n_cols, const vertex & cam_pos, const std::string & file_jpg) +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg) { che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 8db32f1b..331ecbce 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -39,26 +39,26 @@ partitions::operator index_t *& () } -void copy_real_t_array(float * destination, const float * source, const size_t & n_elem) +void copy_real_t_array(float * destination, const float * source, const size_t n_elem) { memcpy(destination, source, n_elem * sizeof(float)); } -void copy_real_t_array(float * destination, const double * source, const size_t & n_elem) +void copy_real_t_array(float * destination, const double * source, const size_t n_elem) { #pragma omp parallel for for(index_t i = 0; i < n_elem; ++i) destination[i] = source[i]; } -void copy_real_t_array(double * destination, const float * source, const size_t & n_elem) +void copy_real_t_array(double * destination, const float * source, const size_t n_elem) { #pragma omp parallel for for(index_t i = 0; i < n_elem; ++i) destination[i] = source[i]; } -void copy_real_t_array(double * destination, const double * source, const size_t & n_elem) +void copy_real_t_array(double * destination, const double * source, const size_t n_elem) { memcpy(destination, source, n_elem * sizeof(double)); } diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 4e1421a2..3f059a73 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -45,7 +45,7 @@ mat4 camera::perspective(const real_t fovy, const real_t aspect, const real_t ne return P; } -quaternion camera::click_to_sphere(const double & x, const double & y, const int & w, const int & h) +quaternion camera::click_to_sphere(const double x, const double y, const int w, const int h) { quaternion p(0, 2 * x / w - 1, 2 * y / h - 1, 0); @@ -67,7 +67,7 @@ quaternion camera::current_rotation() const return p_drag * p_click.conj() * r_last; } -void camera::mouse(const bool & press, const double & x, const double & y, const int & w, const int & h) +void camera::mouse(const bool & press, const double x, const double y, const int w, const int h) { if(press) { @@ -80,7 +80,7 @@ void camera::mouse(const bool & press, const double & x, const double & y, const } } -void camera::motion(const double & x, const double & y, const int & w, const int & h) +void camera::motion(const double x, const double y, const int w, const int h) { p_last = p_drag; p_drag = click_to_sphere(x, y, w, h); diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index b7184cac..e629a28c 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -29,17 +29,17 @@ che_viewer::~che_viewer() glDeleteVertexArrays(1, &vao); } -che *& che_viewer::operator -> () +che * che_viewer::operator -> () { return mesh; } -che *const & che_viewer::operator -> () const +const che * che_viewer::operator -> () const { return mesh; } -che_viewer::operator che *& () +che_viewer::operator che * () { return mesh; } diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index f4eb1a7a..f9fde56e 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -95,7 +95,7 @@ void frame::unmap_pbo(bool cuda) #endif // GPROSHAN_CUDA } -bool frame::resize(const size_t & w, const size_t & h) +bool frame::resize(const size_t w, const size_t h) { if(w == width && height == h) return false; diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index ce0b4156..1ef90abd 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -133,7 +133,7 @@ void scene_viewer::gl_uniform_material(shader & program, const scene::material & program.uniform("mat.map_d", mat.map_d); program.uniform("mat.map_bump", mat.map_bump); - static auto bind_texture = [&](const int & i, const int & map, const char * tex) + static auto bind_texture = [&](const int i, const int map, const char * tex) { if(map < 0) return; glActiveTexture(gltex_nums[i]); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 7f071225..3f8958f4 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -62,7 +62,7 @@ const size_t viewer::max_meshes = size(m_window_split) - 1; che_sphere viewer::sphere_data{0.01}; -viewer::viewer(const char * title, const int & width, const int & height) +viewer::viewer(const char * title, const int width, const int height) { window_width = width; window_height = height; @@ -143,7 +143,7 @@ void viewer::imgui() ImGui::SetNextWindowSize(ImVec2(250, -1)); if(ImGui::BeginPopupContextVoid("mesh")) { - const int & p = size(mesh->filename) - 31; + const int p = size(mesh->filename) - 31; ImGui::TextDisabled("%s%30s", p < 0 ? "" : "<<", mesh->filename.substr(p < 0 ? 0 : p).c_str()); for(auto & p: menu_processes[1]) // init_menus @@ -524,16 +524,16 @@ void viewer::add_menu(const std::string & str, const std::vector & vprocess menus.push_back(str); menu_processes.emplace_back(vprocesses); - const int & id_menu = size(menus) - 1; - for(const int & p: menu_processes.back()) + const int id_menu = size(menus) - 1; + for(const int p: menu_processes.back()) processes[p].id_menu = id_menu; } -int viewer::add_process(const char * name, const function_t & f, const int & key) +int viewer::add_process(const char * name, const function_t & f, const int key) { static int nk = 1000; - const int & fkey = key == -1 ? ++nk : key; + const int fkey = key == -1 ? ++nk : key; if(processes.contains(fkey)) { fprintf(stderr, "repeated key: [%d] %s (%s)\n", key, glfw_key_name.at(key), name); @@ -601,8 +601,8 @@ bool viewer::pop_mesh() void viewer::update_viewport_meshes() { - const int & rows = m_window_split[size(meshes)].x(); - const int & cols = m_window_split[size(meshes)].y(); + const int rows = m_window_split[size(meshes)].x(); + const int cols = m_window_split[size(meshes)].y(); for(index_t m = 0; m < size(meshes); ++m) { meshes[m]->vx = m % cols; @@ -698,7 +698,7 @@ void viewer::mouse_callback(GLFWwindow * window, int button, int action, int mod const index_t ix = xpos * xscale; const index_t iy = ypos * yscale; - const int & cols = m_window_split[size(view->meshes)].y(); + const int cols = m_window_split[size(view->meshes)].y(); const index_t idx_mesh = cols * (iy / view->viewport_height) + ix / view->viewport_width; if(idx_mesh < size(view->meshes)) view->idx_selected_mesh = idx_mesh; From 5fc627dc2030b334ddb165d0a6c190e744ca2344 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 8 Dec 2023 12:58:58 +0100 Subject: [PATCH 0930/1018] gproshan: minor updates --- include/gproshan/geodesics/geodesics_ptp.h | 2 +- src/gproshan/pointcloud/knn.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index f0488a3e..cabdd949 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -58,7 +58,7 @@ struct ptp_out_t struct toplesets_t { const std::vector & limits; - const index_t *const & index; + const index_t * index; }; double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 8f59a702..ebecd4d9 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -48,7 +48,7 @@ std::vector grid::operator () (const point & p, int k) const if(pos.x() == NIL || pos.y() == NIL || pos.z() == NIL) continue; - const auto & iter = voxels.find(pos); + const auto iter = voxels.find(pos); if(iter == end(voxels)) continue; From b1f5fedf1f80f338fe8c11ee6ab79021dcfe3682 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 9 Jan 2024 17:26:52 +0100 Subject: [PATCH 0931/1018] rt_embree: adding point cloud options params --- apps/test_scanner.cpp | 12 +++++------- include/gproshan/raytracing/embree.h | 22 +++++++++++++++------- src/gproshan/raytracing/embree.cpp | 15 +++++++-------- src/gproshan/viewer/viewer.cpp | 5 ++++- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/apps/test_scanner.cpp b/apps/test_scanner.cpp index c4aec9c5..ce4219ef 100644 --- a/apps/test_scanner.cpp +++ b/apps/test_scanner.cpp @@ -9,19 +9,17 @@ int main(int argc, char* argv[]) { - if(argc < 2 || argc > 7) + if(argc != 6) { - std::cerr << "Correct usage: ./apps/test_scanner n_rows n_cols pc_radius ptx_folder jpg_folder" << std::endl; + std::cerr << "Correct usage: ./apps/test_scanner mesh n_rows n_cols ptx_folder jpg_folder" << std::endl; return -1; } size_t n_rows = atoi(argv[2]); size_t n_cols = atoi(argv[3]); - float pc_radius = atof(argv[4]); - - char * ptx_folder = argv[5]; - char * jpg_folder = argv[6]; + char * ptx_folder = argv[4]; + char * jpg_folder = argv[5]; gproshan_log_var(ptx_folder); gproshan_log_var(jpg_folder); @@ -30,7 +28,7 @@ int main(int argc, char* argv[]) const gproshan::mat4 & model_mat = mesh_ply->normalize_box(); - gproshan::rt::raytracing * rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}, false, pc_radius); + gproshan::rt::raytracing * rt_embree = new gproshan::rt::embree({mesh_ply}, {model_mat}); gproshan::che * ptx_mesh = gproshan::scanner_ptx_jpg(rt_embree, n_rows, n_cols, {0, 0, 0}, jpg_folder + mesh_ply->name()); diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 8848957e..ea6f4486 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -16,13 +16,24 @@ namespace gproshan::rt { class embree : public raytracing { + public: + struct pc_opts + { + bool enable = false; + bool normals = false; + float radius = -1; + + pc_opts() {}; + }; + protected: struct ray_hit : public RTCRayHit { ray_hit(const vertex & p_org = {0, 0, 0}, const vertex & v_dir = {0, 0, 0}, float near = 1e-5f, - float far = 1e20f); + float far = 1e20f + ); vertex org() const; vertex dir() const; @@ -37,14 +48,11 @@ class embree : public raytracing std::vector g_meshes; scene_data sc; - float pc_radius = 0.01; - public: embree(); embree( const std::vector & meshes, const std::vector & model_mats, - const bool & pointcloud = false, - const float pcr = 0.01 + const pc_opts & pc = pc_opts() ); virtual ~embree(); @@ -55,13 +63,13 @@ class embree : public raytracing protected: void build_bvh( const std::vector & meshes, const std::vector & model_mats, - const bool & pointcloud = false + const pc_opts & pc = pc_opts() ); index_t add_sphere(const vec4 & xyzr); index_t add_mesh(const che * mesh, const mat4 & model_mat); - virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat); + virtual index_t add_pointcloud(const che * mesh, const mat4 & model_mat, const pc_opts & pc); virtual bool closesthit_radiance( vertex & color, vertex & attenuation, diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 929f24cb..1fe6541a 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -67,10 +67,9 @@ embree::embree() rtcSetDeviceErrorFunction(rtc_device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud, const float pcr): embree() +embree::embree(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc): embree() { - pc_radius = pcr; - build_bvh(meshes, model_mats, pointcloud); + build_bvh(meshes, model_mats, pc); } embree::~embree() @@ -122,20 +121,20 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & return hit; } -void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const bool & pointcloud) +void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc) { g_meshes.resize(size(meshes)); for(index_t i = 0; i < size(meshes); ++i) { g_meshes[i] = new CHE(meshes[i]); - if(!meshes[i]->n_trigs || pointcloud) + if(!meshes[i]->n_trigs || pc.enable) g_meshes[i]->n_trigs = 0; [[maybe_unused]] const index_t geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? add_mesh(meshes[i], model_mats[i]) : - add_pointcloud(meshes[i], model_mats[i]); + add_pointcloud(meshes[i], model_mats[i], pc); gproshan_debug_var(i == geomID); } @@ -214,7 +213,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) return geom_id; } -index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) +index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const pc_opts & pc) { RTCGeometry geom = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_DISC_POINT); @@ -236,7 +235,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat) for(index_t i = 0; i < mesh->n_vertices; ++i) { pxyzr[i] = model_mat * (mesh->point(i), 1); - pxyzr[i][3] = pc_radius; + pxyzr[i][3] = pc.radius; // normal[i] = mesh->normal(i); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 3f8958f4..69cb3b2f 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -977,6 +977,7 @@ bool viewer::m_setup_raytracing(viewer * view) static int rt = 0; static double time = 0; + static rt::embree::pc_opts pc; ImGui::SliderInt("depth", (int *) &view->render_params.depth, 1, 1 << 5); ImGui::SliderInt("n_samples", (int *) &view->render_params.n_samples, 1, 1 << 5); @@ -984,6 +985,8 @@ bool viewer::m_setup_raytracing(viewer * view) if(ImGui::Button("Build")) { + pc.enable |= mesh.render_pointcloud; + switch(rt) { case R_GL: break; @@ -991,7 +994,7 @@ bool viewer::m_setup_raytracing(viewer * view) case R_EMBREE: delete mesh.rt_embree; TIC(time); - mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, mesh.render_pointcloud); + mesh.rt_embree = new rt::embree({mesh}, {mesh.model_mat}, pc); TOC(time); view->update_status_message("build embree in %.3fs", time); break; From accd707b0acd2da449d9e840817990409675cd4d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 10 Jan 2024 16:56:37 +0100 Subject: [PATCH 0932/1018] rt_embree: varying radius knn based --- include/gproshan/raytracing/embree.h | 3 ++- src/gproshan/raytracing/embree.cpp | 40 ++++++++++++++++++++-------- src/gproshan/viewer/viewer.cpp | 9 +++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index ea6f4486..c1333141 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -21,7 +21,8 @@ class embree : public raytracing { bool enable = false; bool normals = false; - float radius = -1; + float radius = 0.01; + int knn = 0; pc_opts() {}; }; diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 1fe6541a..6aa5b17a 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -215,7 +216,8 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const pc_opts & pc) { - RTCGeometry geom = rtcNewGeometry(rtc_device, RTC_GEOMETRY_TYPE_DISC_POINT); + RTCGeometry geom = rtcNewGeometry(rtc_device, pc.normals ? RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT + : RTC_GEOMETRY_TYPE_DISC_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_VERTEX, 0, @@ -223,20 +225,36 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p 4 * sizeof(float), mesh->n_vertices ); -/* - vertex * normal = (vertex *) rtcSetNewGeometryBuffer( geom, - RTC_BUFFER_TYPE_NORMAL, 0, - RTC_FORMAT_FLOAT3, - 3 * sizeof(float), - mesh->n_vertices - ); -*/ + + vertex * normals = !pc.normals ? nullptr + : (vertex *) rtcSetNewGeometryBuffer( geom, + RTC_BUFFER_TYPE_NORMAL, 0, + RTC_FORMAT_FLOAT3, + 3 * sizeof(float), + mesh->n_vertices + ); + + + knn::k3tree * nn = !pc.knn ? nullptr + : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn); + + #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { pxyzr[i] = model_mat * (mesh->point(i), 1); - pxyzr[i][3] = pc.radius; -// normal[i] = mesh->normal(i); + pxyzr[i][3] = nn ? length(model_mat * (mesh->point(i), 1) - model_mat * (mesh->point((*nn)(i, pc.knn - 1)), 1)) : pc.radius; + } + + + delete nn; + + + if(normals) + { + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + normals[i] = mesh->normal(i); } rtcCommitGeometry(geom); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 69cb3b2f..55474de2 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -983,6 +983,15 @@ bool viewer::m_setup_raytracing(viewer * view) ImGui::SliderInt("n_samples", (int *) &view->render_params.n_samples, 1, 1 << 5); ImGui::Combo("rt", &rt, "Select\0Embree\0OptiX\0\0"); + if(rt == R_EMBREE && (mesh.render_pointcloud || mesh->is_pointcloud())) + { + ImGui::Indent(); + ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 5); + ImGui::SliderFloat("pc.radius", &pc.radius, 0, 1); + ImGui::Checkbox("pc.normals", &pc.normals); + ImGui::Unindent(); + } + if(ImGui::Button("Build")) { pc.enable |= mesh.render_pointcloud; From 3e10b23247aa09fb947dbfc60a77b72a886412a8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 15 Jan 2024 23:46:01 +0100 Subject: [PATCH 0933/1018] update imgui v1.90.1 --- include/imgui/LICENSE.txt | 2 +- include/imgui/imconfig.h | 31 +- include/imgui/imgui.h | 753 ++++---- include/imgui/imgui_impl_glfw.h | 15 +- include/imgui/imgui_impl_opengl3.h | 10 +- include/imgui/imgui_impl_opengl3_loader.h | 19 +- include/imgui/imgui_internal.h | 462 +++-- include/imgui/imstb_textedit.h | 148 +- src/imgui/LICENSE.txt | 2 +- src/imgui/imgui.cpp | 2118 +++++++++++++-------- src/imgui/imgui_demo.cpp | 908 ++++++--- src/imgui/imgui_draw.cpp | 284 ++- src/imgui/imgui_impl_glfw.cpp | 88 +- src/imgui/imgui_impl_opengl3.cpp | 25 +- src/imgui/imgui_tables.cpp | 451 ++++- src/imgui/imgui_widgets.cpp | 780 +++++--- 16 files changed, 4039 insertions(+), 2057 deletions(-) diff --git a/include/imgui/LICENSE.txt b/include/imgui/LICENSE.txt index fb715bdc..3282f5b5 100644 --- a/include/imgui/LICENSE.txt +++ b/include/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2023 Omar Cornut +Copyright (c) 2014-2024 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index 876cf32f..d556cbaf 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -1,5 +1,5 @@ //----------------------------------------------------------------------------- -// COMPILE-TIME OPTIONS FOR DEAR IMGUI +// DEAR IMGUI COMPILE-TIME OPTIONS // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure. // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions. //----------------------------------------------------------------------------- @@ -9,7 +9,7 @@ // You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp // files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures. // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts. -// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using. +// Call IMGUI_CHECKVERSION() from your .cpp file to verify that the data structures your files are using are matching the ones imgui.cpp is using. //----------------------------------------------------------------------------- #pragma once @@ -26,21 +26,21 @@ //#define IMGUI_API __declspec( dllexport ) //#define IMGUI_API __declspec( dllimport ) -//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names. +//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names. //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS -//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87: disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This will be folded into IMGUI_DISABLE_OBSOLETE_FUNCTIONS in a few versions. +//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87+ disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This is automatically done by IMGUI_DISABLE_OBSOLETE_FUNCTIONS. //---- Disable all of Dear ImGui or don't implement standard windows/tools. // It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp. //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty. //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. -//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowStackToolWindow() will be empty (this was called IMGUI_DISABLE_METRICS_WINDOW before 1.88). +//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowIDStackToolWindow() will be empty. //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a) //#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW) //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a) -//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime). +//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME). //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. @@ -50,21 +50,24 @@ //#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available //---- Include imgui_user.h at the end of imgui.h as a convenience +// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included. //#define IMGUI_INCLUDE_IMGUI_USER_H +//#define IMGUI_USER_H_FILENAME "my_folder/my_imgui_user.h" //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another) //#define IMGUI_USE_BGRA_PACKED_COLOR -//---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...) +//---- Use 32-bit for ImWchar (default is 16-bit) to support Unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...) //#define IMGUI_USE_WCHAR32 //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version // By default the embedded implementations are declared static and not available outside of Dear ImGui sources files. //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h" //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h" -//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if enabled +//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if IMGUI_USE_STB_SPRINTF is defined. //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION +//#define IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION // only disabled if IMGUI_USE_STB_SPRINTF is defined. //---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined) // Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h. @@ -75,6 +78,12 @@ // On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'. //#define IMGUI_ENABLE_FREETYPE +//---- Use FreeType+lunasvg library to render OpenType SVG fonts (SVGinOT) +// Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided). +// Only works in combination with IMGUI_ENABLE_FREETYPE. +// (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement) +//#define IMGUI_ENABLE_FREETYPE_LUNASVG + //---- Use stb_truetype to build and rasterize the font atlas (default) // The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend. //#define IMGUI_ENABLE_STB_TRUETYPE @@ -105,7 +114,7 @@ //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data); //#define ImDrawCallback MyImDrawCallback -//---- Debug Tools: Macro to break in Debugger +//---- Debug Tools: Macro to break in Debugger (we provide a default implementation of this in the codebase) // (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.) //#define IM_DEBUG_BREAK IM_ASSERT(0) //#define IM_DEBUG_BREAK __debugbreak() @@ -113,10 +122,10 @@ //---- Debug Tools: Enable slower asserts //#define IMGUI_DEBUG_PARANOID -//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files. +//---- Tip: You can add extra functions within the ImGui:: namespace from anywhere (e.g. your own sources/header files) /* namespace ImGui { - void MyFunction(const char* name, const MyMatrix44& v); + void MyFunction(const char* name, MyMatrix44* mtx); } */ diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index 03e503e6..0ec19776 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,31 +1,30 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (headers) // Help: -// - Read FAQ at http://dearimgui.com/faq -// - Newcomers, read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. +// - See links below. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. -// Read imgui.cpp for details, links and comments. +// - Read top of imgui.cpp for more details, links and comments. // Resources: -// - FAQ http://dearimgui.com/faq +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started // - Homepage https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/6478 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/6897 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) -// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues +// - Tests & Automation https://github.com/ocornut/imgui_test_engine -// Getting Started? -// - Read https://github.com/ocornut/imgui/wiki/Getting-Started -// - For first-time users having issues compiling/linking/running/loading fonts: -// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. +// For first-time users having issues compiling/linking/running/loading fonts: +// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. +// Everything else should be asked in 'Issues'! We are building a database of cross-linked knowledge there. // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') -#define IMGUI_VERSION "1.89.7" -#define IMGUI_VERSION_NUM 18971 +#define IMGUI_VERSION "1.90.1" +#define IMGUI_VERSION_NUM 19010 #define IMGUI_HAS_TABLE /* @@ -35,10 +34,11 @@ Index of this file: // [SECTION] Forward declarations and basic types // [SECTION] Dear ImGui end-user API functions // [SECTION] Flags & Enumerations +// [SECTION] Tables API flags and structures (ImGuiTableFlags, ImGuiTableColumnFlags, ImGuiTableRowFlags, ImGuiTableBgTarget, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) // [SECTION] Helpers: Memory allocations macros, ImVector<> // [SECTION] ImGuiStyle // [SECTION] ImGuiIO -// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) +// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload) // [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor) // [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) @@ -51,7 +51,7 @@ Index of this file: #pragma once // Configuration file with compile-time options -// (edit imconfig.h or '#define IMGUI_USER_CONFIG "myfilename.h" from your build system') +// (edit imconfig.h or '#define IMGUI_USER_CONFIG "myfilename.h" from your build system) #ifdef IMGUI_USER_CONFIG #include IMGUI_USER_CONFIG #endif @@ -71,7 +71,7 @@ Index of this file: // Define attributes of all API symbols declarations (e.g. for DLL under Windows) // IMGUI_API is used for core imgui functions, IMGUI_IMPL_API is used for the default backends files (imgui_impl_xxx.h) -// Using dear imgui via a shared library is not recommended, because we don't guarantee backward nor forward ABI compatibility (also function call overhead, as dear imgui is a call-heavy API) +// Using dear imgui via a shared library is not recommended: we don't guarantee backward nor forward ABI compatibility + this is a call-heavy library and function call overhead adds up. #ifndef IMGUI_API #define IMGUI_API #endif @@ -86,7 +86,6 @@ Index of this file: #endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. -#define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // Offset of _MEMBER within _TYPE. Standardized as offsetof() in C++11 #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. @@ -117,10 +116,14 @@ Index of this file: #endif #if defined(__clang__) #pragma clang diagnostic push +#if __has_warning("-Wunknown-warning-option") +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' +#endif +#pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' #pragma clang diagnostic ignored "-Wold-style-cast" -#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" -#endif +#pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -180,7 +183,7 @@ typedef int ImGuiSortDirection; // -> enum ImGuiSortDirection_ // Enum: A typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A color target for TableSetBgColor() -// Flags (declared as int for compatibility with old C++, to allow using as flags without overhead, and to not pollute the top of this file) +// Flags (declared as int to allow using as flags without overhead, and to not pollute the top of this file) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! // In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. @@ -189,6 +192,7 @@ typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: f typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build typedef int ImGuiBackendFlags; // -> enum ImGuiBackendFlags_ // Flags: for io.BackendFlags typedef int ImGuiButtonFlags; // -> enum ImGuiButtonFlags_ // Flags: for InvisibleButton() +typedef int ImGuiChildFlags; // -> enum ImGuiChildFlags_ // Flags: for BeginChild() typedef int ImGuiColorEditFlags; // -> enum ImGuiColorEditFlags_ // Flags: for ColorEdit4(), ColorPicker4() etc. typedef int ImGuiConfigFlags; // -> enum ImGuiConfigFlags_ // Flags: for io.ConfigFlags typedef int ImGuiComboFlags; // -> enum ImGuiComboFlags_ // Flags: for BeginCombo() @@ -196,7 +200,7 @@ typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: f typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() -typedef int ImGuiKeyChord; // -> ImGuiKey | ImGuiMod_XXX // Flags: for storage only for now: an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values. +typedef int ImGuiKeyChord; // -> ImGuiKey | ImGuiMod_XXX // Flags: for IsKeyChordPressed(), Shortcut() etc. an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values. typedef int ImGuiPopupFlags; // -> enum ImGuiPopupFlags_ // Flags: for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. @@ -236,8 +240,8 @@ typedef unsigned long long ImU64; // 64-bit unsigned integer // Character types // (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) -typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. +typedef unsigned short ImWchar16; // A single decoded U16 character/code point. We encode them as multi bytes UTF-8 when used in strings. #ifdef IMGUI_USE_WCHAR32 // ImWchar [configurable type: override in imconfig.h with '#define IMGUI_USE_WCHAR32' to support Unicode planes 1-16] typedef ImWchar32 ImWchar; #else @@ -252,6 +256,7 @@ typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // ImVec2: 2D vector used to store positions, sizes etc. [Compile-time configurable type] // This is a frequently used type in the API. Consider using IM_VEC2_CLASS_EXTRA to create implicit cast from/to our preferred type. +// Add '#define IMGUI_DEFINE_MATH_OPERATORS' in your imconfig.h file to benefit from courtesy maths operators for those types. IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec2 { @@ -295,7 +300,7 @@ namespace ImGui // Main IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) - IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleCol(), PushStyleVar() to modify style mid-frame! + IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleColor(), PushStyleVar() to modify style mid-frame! IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(). If you don't need to render data (skipping rendering) you may call EndFrame() without Render()... but you'll have wasted CPU already! If you don't need to render, better to not create any windows and not call NewFrame() at all! IMGUI_API void Render(); // ends the Dear ImGui frame, finalize the draw data. You can then get call GetDrawData(). @@ -305,7 +310,7 @@ namespace ImGui IMGUI_API void ShowDemoWindow(bool* p_open = NULL); // create Demo window. demonstrate most ImGui features. call this to learn about the library! try to make it always available in your application! IMGUI_API void ShowMetricsWindow(bool* p_open = NULL); // create Metrics/Debugger window. display Dear ImGui internals: windows, draw commands, various internal state, etc. IMGUI_API void ShowDebugLogWindow(bool* p_open = NULL); // create Debug Log window. display a simplified log of important dear imgui events. - IMGUI_API void ShowStackToolWindow(bool* p_open = NULL); // create Stack Tool window. hover items with mouse to query information about the source of their unique ID. + IMGUI_API void ShowIDStackToolWindow(bool* p_open = NULL); // create Stack Tool window. hover items with mouse to query information about the source of their unique ID. IMGUI_API void ShowAboutWindow(bool* p_open = NULL); // create About window. display Dear ImGui version, credits and build/system information. IMGUI_API void ShowStyleEditor(ImGuiStyle* ref = NULL); // add style editor block (not a window). you can pass in a reference ImGuiStyle structure to compare to, revert to and save to (else it uses the default style) IMGUI_API bool ShowStyleSelector(const char* label); // add style selector block (not a window), essentially a combo listing the default styles. @@ -326,23 +331,33 @@ namespace ImGui // Some information such as 'flags' or 'p_open' will only be considered by the first call to Begin(). // - Begin() return false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting // anything to the window. Always call a matching End() for each Begin() call, regardless of its return value! - // [Important: due to legacy reason, this is inconsistent with most other functions such as BeginMenu/EndMenu, - // BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function - // returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] + // [Important: due to legacy reason, Begin/End and BeginChild/EndChild are inconsistent with all other functions + // such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding + // BeginXXX function returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] // - Note that the bottom of window stack always contains a window called "Debug". IMGUI_API bool Begin(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); IMGUI_API void End(); // Child Windows // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. - // - For each independent axis of 'size': ==0.0f: use remaining host window size / >0.0f: fixed size / <0.0f: use remaining window size minus abs(size) / Each axis can use a different mode, e.g. ImVec2(0,400). - // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting anything to the window. - // Always call a matching EndChild() for each BeginChild() call, regardless of its return value. - // [Important: due to legacy reason, this is inconsistent with most other functions such as BeginMenu/EndMenu, - // BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding BeginXXX function - // returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] - IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); - IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0, 0), bool border = false, ImGuiWindowFlags flags = 0); + // - Before 1.90 (November 2023), the "ImGuiChildFlags child_flags = 0" parameter was "bool border = false". + // This API is backward compatible with old code, as we guarantee that ImGuiChildFlags_Border == true. + // Consider updating your old call sites: + // BeginChild("Name", size, false) -> Begin("Name", size, 0); or Begin("Name", size, ImGuiChildFlags_None); + // BeginChild("Name", size, true) -> Begin("Name", size, ImGuiChildFlags_Border); + // - Manual sizing (each axis can use a different setting e.g. ImVec2(0.0f, 400.0f)): + // == 0.0f: use remaining parent window size for this axis. + // > 0.0f: use specified size for this axis. + // < 0.0f: right/bottom-align to specified distance from available content boundaries. + // - Specifying ImGuiChildFlags_AutoResizeX or ImGuiChildFlags_AutoResizeY makes the sizing automatic based on child contents. + // Combining both ImGuiChildFlags_AutoResizeX _and_ ImGuiChildFlags_AutoResizeY defeats purpose of a scrolling region and is NOT recommended. + // - BeginChild() returns false to indicate the window is collapsed or fully clipped, so you may early out and omit submitting + // anything to the window. Always call a matching EndChild() for each BeginChild() call, regardless of its return value. + // [Important: due to legacy reason, Begin/End and BeginChild/EndChild are inconsistent with all other functions + // such as BeginMenu/EndMenu, BeginPopup/EndPopup, etc. where the EndXXX call should only be called if the corresponding + // BeginXXX function returned true. Begin and BeginChild are the only odd ones out. Will be fixed in a future update.] + IMGUI_API bool BeginChild(const char* str_id, const ImVec2& size = ImVec2(0, 0), ImGuiChildFlags child_flags = 0, ImGuiWindowFlags window_flags = 0); + IMGUI_API bool BeginChild(ImGuiID id, const ImVec2& size = ImVec2(0, 0), ImGuiChildFlags child_flags = 0, ImGuiWindowFlags window_flags = 0); IMGUI_API void EndChild(); // Windows Utilities @@ -350,10 +365,10 @@ namespace ImGui IMGUI_API bool IsWindowAppearing(); IMGUI_API bool IsWindowCollapsed(); IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options. - IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags=0); // is current window hovered (and typically: not blocked by a popup/modal)? see flags for options. NB: If you are trying to check whether your mouse should be dispatched to imgui or to your app, you should use the 'io.WantCaptureMouse' boolean for that! Please read the FAQ! + IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags=0); // is current window hovered and hoverable (e.g. not blocked by a popup/modal)? See ImGuiHoveredFlags_ for options. IMPORTANT: If you are trying to check whether your mouse should be dispatched to Dear ImGui or to your underlying app, you should not use this function! Use the 'io.WantCaptureMouse' boolean for that! Refer to FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" for details. IMGUI_API ImDrawList* GetWindowDrawList(); // get draw list associated to the current window, to append your own drawing primitives - IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (useful if you want to do your own drawing via the DrawList API) - IMGUI_API ImVec2 GetWindowSize(); // get current window size + IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (note: it is unlikely you need to use this. Consider using current layout pos instead, GetCursorScreenPos()) + IMGUI_API ImVec2 GetWindowSize(); // get current window size (note: it is unlikely you need to use this. Consider using GetCursorScreenPos() and e.g. GetContentRegionAvail() instead) IMGUI_API float GetWindowWidth(); // get current window width (shortcut for GetWindowSize().x) IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) @@ -361,7 +376,7 @@ namespace ImGui // - Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). IMGUI_API void SetNextWindowPos(const ImVec2& pos, ImGuiCond cond = 0, const ImVec2& pivot = ImVec2(0, 0)); // set next window position. call before Begin(). use pivot=(0.5f,0.5f) to center on given point, etc. IMGUI_API void SetNextWindowSize(const ImVec2& size, ImGuiCond cond = 0); // set next window size. set axis to 0.0f to force an auto-fit on this axis. call before Begin() - IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use -1,-1 on either X/Y axis to preserve the current size. Sizes will be rounded down. Use callback to apply non-trivial programmatic constraints. + IMGUI_API void SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback = NULL, void* custom_callback_data = NULL); // set next window size limits. use 0.0f or FLT_MAX if you don't want limits. Use -1 for both min and max of same axis to preserve current size (which itself is a constraint). Use callback to apply non-trivial programmatic constraints. IMGUI_API void SetNextWindowContentSize(const ImVec2& size); // set next window content size (~ scrollable client area, which enforce the range of scrollbars). Not including window decorations (title bar, menu bar, etc.) nor WindowPadding. set an axis to 0.0f to leave it automatic. call before Begin() IMGUI_API void SetNextWindowCollapsed(bool collapsed, ImGuiCond cond = 0); // set next window collapsed state. call before Begin() IMGUI_API void SetNextWindowFocus(); // set next window to be focused / top-most. call before Begin() @@ -431,13 +446,25 @@ namespace ImGui IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. - // Cursor / Layout + // Layout cursor positioning // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. // - You can call SameLine() between widgets to undo the last carriage return and output at the right of the preceding widget. // - Attention! We currently have inconsistencies between window-local and absolute positions we will aim to fix with future API: - // Window-local coordinates: SameLine(), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), GetContentRegionMax(), GetWindowContentRegion*(), PushTextWrapPos() - // Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. + // - Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. -> this is the preferred way forward. + // - Window-local coordinates: SameLine(), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), GetContentRegionMax(), GetWindowContentRegion*(), PushTextWrapPos() + // - GetCursorScreenPos() = GetCursorPos() + GetWindowPos(). GetWindowPos() is almost only ever useful to convert from window-local to absolute coordinates. + IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute coordinates (prefer using this, also more useful to work with ImDrawList API). + IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute coordinates + IMGUI_API ImVec2 GetCursorPos(); // [window-local] cursor position in window coordinates (relative to window position) + IMGUI_API float GetCursorPosX(); // [window-local] " + IMGUI_API float GetCursorPosY(); // [window-local] " + IMGUI_API void SetCursorPos(const ImVec2& local_pos); // [window-local] " + IMGUI_API void SetCursorPosX(float local_x); // [window-local] " + IMGUI_API void SetCursorPosY(float local_y); // [window-local] " + IMGUI_API ImVec2 GetCursorStartPos(); // [window-local] initial cursor position, in window coordinates + + // Other layout functions IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. IMGUI_API void SameLine(float offset_from_start_x=0.0f, float spacing=-1.0f); // call between widgets or groups to layout them horizontally. X position given in window coordinates. IMGUI_API void NewLine(); // undo a SameLine() or force a new line when in a horizontal-layout context. @@ -447,15 +474,6 @@ namespace ImGui IMGUI_API void Unindent(float indent_w = 0.0f); // move content position back to the left, by indent_w, or style.IndentSpacing if indent_w <= 0 IMGUI_API void BeginGroup(); // lock horizontal starting position IMGUI_API void EndGroup(); // unlock horizontal starting position + capture the whole group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) - IMGUI_API ImVec2 GetCursorPos(); // cursor position in window coordinates (relative to window position) - IMGUI_API float GetCursorPosX(); // (some functions are using window-relative coordinates, such as: GetCursorPos, GetCursorStartPos, GetContentRegionMax, GetWindowContentRegion* etc. - IMGUI_API float GetCursorPosY(); // other functions such as GetCursorScreenPos or everything in ImDrawList:: - IMGUI_API void SetCursorPos(const ImVec2& local_pos); // are using the main, absolute coordinate system. - IMGUI_API void SetCursorPosX(float local_x); // GetWindowPos() + GetCursorPos() == GetCursorScreenPos() etc.) - IMGUI_API void SetCursorPosY(float local_y); // - IMGUI_API ImVec2 GetCursorStartPos(); // initial cursor position in window coordinates - IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute coordinates (useful to work with ImDrawList API). generally top-left == GetMainViewport()->Pos == (0,0) in single viewport mode, and bottom-right == GetMainViewport()->Pos+Size == io.DisplaySize in single-viewport mode. - IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute coordinates IMGUI_API void AlignTextToFramePadding(); // vertically align upcoming text baseline to FramePadding.y so that it will align properly to regularly framed items (call if you have text on a line before a framed item) IMGUI_API float GetTextLineHeight(); // ~ FontSize IMGUI_API float GetTextLineHeightWithSpacing(); // ~ FontSize + style.ItemSpacing.y (distance in pixels between 2 consecutive lines of text) @@ -502,7 +520,7 @@ namespace ImGui // - Most widgets return true when the value has been changed or when pressed/selected // - You may also use one of the many IsItemXXX functions (e.g. IsItemActive, IsItemHovered, etc.) to query widget state. IMGUI_API bool Button(const char* label, const ImVec2& size = ImVec2(0, 0)); // button - IMGUI_API bool SmallButton(const char* label); // button with FramePadding=(0,0) to easily embed within text + IMGUI_API bool SmallButton(const char* label); // button with (FramePadding.y == 0) to easily embed within text IMGUI_API bool InvisibleButton(const char* str_id, const ImVec2& size, ImGuiButtonFlags flags = 0); // flexible button behavior without the visuals, frequently useful to build custom behaviors using the public api (along with IsItemActive, IsItemHovered, etc.) IMGUI_API bool ArrowButton(const char* str_id, ImGuiDir dir); // square button with an arrow shape IMGUI_API bool Checkbox(const char* label, bool* v); @@ -515,8 +533,10 @@ namespace ImGui // Widgets: Images // - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples - IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), const ImVec4& border_col = ImVec4(0, 0, 0, 0)); - IMGUI_API bool ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); + // - 'uv0' and 'uv1' are texture coordinates. Read about them from the same link above. + // - Note that Image() may add +2.0f to provided size if a border is visible, ImageButton() adds style.FramePadding*2.0f to provided size. + IMGUI_API void Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& tint_col = ImVec4(1, 1, 1, 1), const ImVec4& border_col = ImVec4(0, 0, 0, 0)); + IMGUI_API bool ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // Widgets: Combo Box (Dropdown) // - The BeginCombo()/EndCombo() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() items. @@ -525,7 +545,7 @@ namespace ImGui IMGUI_API void EndCombo(); // only call EndCombo() if BeginCombo() returns true! IMGUI_API bool Combo(const char* label, int* current_item, const char* const items[], int items_count, int popup_max_height_in_items = -1); IMGUI_API bool Combo(const char* label, int* current_item, const char* items_separated_by_zeros, int popup_max_height_in_items = -1); // Separate items with \0 within a string, end item-list with \0\0. e.g. "One\0Two\0Three\0" - IMGUI_API bool Combo(const char* label, int* current_item, bool(*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int popup_max_height_in_items = -1); + IMGUI_API bool Combo(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), void* user_data, int items_count, int popup_max_height_in_items = -1); // Widgets: Drag Sliders // - CTRL+Click on any drag box to turn them into an input box. Manually input values aren't clamped by default and can go off-bounds. Use ImGuiSliderFlags_AlwaysClamp to always clamp. @@ -613,9 +633,9 @@ namespace ImGui IMGUI_API bool TreeNodeEx(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) IM_FMTARGS(3); IMGUI_API bool TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); IMGUI_API bool TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) IM_FMTLIST(3); - IMGUI_API void TreePush(const char* str_id); // ~ Indent()+PushId(). Already called by TreeNode() when returning true, but you can call TreePush/TreePop yourself if desired. + IMGUI_API void TreePush(const char* str_id); // ~ Indent()+PushID(). Already called by TreeNode() when returning true, but you can call TreePush/TreePop yourself if desired. IMGUI_API void TreePush(const void* ptr_id); // " - IMGUI_API void TreePop(); // ~ Unindent()+PopId() + IMGUI_API void TreePop(); // ~ Unindent()+PopID() IMGUI_API float GetTreeNodeToLabelSpacing(); // horizontal distance preceding label when using TreeNode*() or Bullet() == (g.FontSize + style.FramePadding.x*2) for a regular unframed TreeNode IMGUI_API bool CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0); // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop(). IMGUI_API bool CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFlags flags = 0); // when 'p_visible != NULL': if '*p_visible==true' display an additional small close button on upper right of the header which will set the bool to false when clicked, if '*p_visible==false' don't display the header. @@ -628,22 +648,22 @@ namespace ImGui IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. // Widgets: List Boxes - // - This is essentially a thin wrapper to using BeginChild/EndChild with some stylistic changes. - // - The BeginListBox()/EndListBox() api allows you to manage your contents and selection state however you want it, by creating e.g. Selectable() or any items. + // - This is essentially a thin wrapper to using BeginChild/EndChild with the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. + // - You can submit contents and manage your selection state however you want it, by creating e.g. Selectable() or any other items. // - The simplified/old ListBox() api are helpers over BeginListBox()/EndListBox() which are kept available for convenience purpose. This is analoguous to how Combos are created. // - Choose frame width: size.x > 0.0f: custom / size.x < 0.0f or -FLT_MIN: right-align / size.x = 0.0f (default): use current ItemWidth // - Choose frame height: size.y > 0.0f: custom / size.y < 0.0f or -FLT_MIN: bottom-align / size.y = 0.0f (default): arbitrary default height which can fit ~7 items IMGUI_API bool BeginListBox(const char* label, const ImVec2& size = ImVec2(0, 0)); // open a framed scrolling region IMGUI_API void EndListBox(); // only call EndListBox() if BeginListBox() returned true! IMGUI_API bool ListBox(const char* label, int* current_item, const char* const items[], int items_count, int height_in_items = -1); - IMGUI_API bool ListBox(const char* label, int* current_item, bool (*items_getter)(void* data, int idx, const char** out_text), void* data, int items_count, int height_in_items = -1); + IMGUI_API bool ListBox(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), void* user_data, int items_count, int height_in_items = -1); // Widgets: Data Plotting // - Consider using ImPlot (https://github.com/epezent/implot) which is much better! IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); IMGUI_API void PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float)); - IMGUI_API void PlotHistogram(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); + IMGUI_API void PlotHistogram(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0)); // Widgets: Value() Helpers. // - Those are merely shortcut to calling Text() with a format string. Output single value in "name: value" format (tip: freely declare more in your code to handle your types. you can add functions to the ImGui namespace) @@ -675,9 +695,9 @@ namespace ImGui IMGUI_API void SetTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); // Tooltips: helpers for showing a tooltip when hovering an item - // - BeginItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_Tooltip) && BeginTooltip())' idiom. - // - SetItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_Tooltip)) { SetTooltip(...); }' idiom. - // - Where 'ImGuiHoveredFlags_Tooltip' itself is a shortcut to use 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on active input type. For mouse it defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. + // - BeginItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_ForTooltip) && BeginTooltip())' idiom. + // - SetItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_ForTooltip)) { SetTooltip(...); }' idiom. + // - Where 'ImGuiHoveredFlags_ForTooltip' itself is a shortcut to use 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on active input type. For mouse it defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. IMGUI_API bool BeginItemTooltip(); // begin/append a tooltip window if preceding item was hovered. IMGUI_API void SetItemTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip if preceeding item was hovered. override any previous call to SetTooltip(). IMGUI_API void SetItemTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); @@ -690,9 +710,7 @@ namespace ImGui // - You can bypass the hovering restriction by using ImGuiHoveredFlags_AllowWhenBlockedByPopup when calling IsItemHovered() or IsWindowHovered(). // - IMPORTANT: Popup identifiers are relative to the current ID stack, so OpenPopup and BeginPopup generally needs to be at the same level of the stack. // This is sometimes leading to confusing mistakes. May rework this in the future. - - // Popups: begin/end functions - // - BeginPopup(): query popup state, if open start appending into the window. Call EndPopup() afterwards. ImGuiWindowFlags are forwarded to the window. + // - BeginPopup(): query popup state, if open start appending into the window. Call EndPopup() afterwards if returned true. ImGuiWindowFlags are forwarded to the window. // - BeginPopupModal(): block every interaction behind the window, cannot be closed by user, add a dimming background, has a title bar. IMGUI_API bool BeginPopup(const char* str_id, ImGuiWindowFlags flags = 0); // return true if the popup is open, and you can start outputting to it. IMGUI_API bool BeginPopupModal(const char* name, bool* p_open = NULL, ImGuiWindowFlags flags = 0); // return true if the modal is open, and you can start outputting to it. @@ -742,12 +760,10 @@ namespace ImGui // TableNextColumn() will automatically wrap-around into the next row if needed. // - IMPORTANT: Comparatively to the old Columns() API, we need to call TableNextColumn() for the first column! // - Summary of possible call flow: - // -------------------------------------------------------------------------------------------------------- - // TableNextRow() -> TableSetColumnIndex(0) -> Text("Hello 0") -> TableSetColumnIndex(1) -> Text("Hello 1") // OK - // TableNextRow() -> TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK - // TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK: TableNextColumn() automatically gets to next row! - // TableNextRow() -> Text("Hello 0") // Not OK! Missing TableSetColumnIndex() or TableNextColumn()! Text will not appear! - // -------------------------------------------------------------------------------------------------------- + // - TableNextRow() -> TableSetColumnIndex(0) -> Text("Hello 0") -> TableSetColumnIndex(1) -> Text("Hello 1") // OK + // - TableNextRow() -> TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK + // - TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK: TableNextColumn() automatically gets to next row! + // - TableNextRow() -> Text("Hello 0") // Not OK! Missing TableSetColumnIndex() or TableNextColumn()! Text will not appear! // - 5. Call EndTable() IMGUI_API bool BeginTable(const char* str_id, int column, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0.0f, 0.0f), float inner_width = 0.0f); IMGUI_API void EndTable(); // only call EndTable() if BeginTable() returns true! @@ -765,8 +781,9 @@ namespace ImGui // - Use TableSetupScrollFreeze() to lock columns/rows so they stay visible when scrolled. IMGUI_API void TableSetupColumn(const char* label, ImGuiTableColumnFlags flags = 0, float init_width_or_weight = 0.0f, ImGuiID user_id = 0); IMGUI_API void TableSetupScrollFreeze(int cols, int rows); // lock columns/rows so they stay visible when scrolled. - IMGUI_API void TableHeadersRow(); // submit all headers cells based on data provided to TableSetupColumn() + submit context menu IMGUI_API void TableHeader(const char* label); // submit one header cell manually (rarely used) + IMGUI_API void TableHeadersRow(); // submit a row with headers cells based on data provided to TableSetupColumn() + submit context menu + IMGUI_API void TableAngledHeadersRow(); // submit a row with angled headers for every column with the ImGuiTableColumnFlags_AngledHeader flag. MUST BE FIRST ROW. // Tables: Sorting & Miscellaneous functions // - Sorting: call TableGetSortSpecs() to retrieve latest sort specs for the table. NULL when not sorting. @@ -824,7 +841,7 @@ namespace ImGui IMGUI_API bool BeginDragDropTarget(); // call after submitting an item that may receive a payload. If this returns true, you can call AcceptDragDropPayload() + EndDragDropTarget() IMGUI_API const ImGuiPayload* AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags = 0); // accept contents of a given type. If ImGuiDragDropFlags_AcceptBeforeDelivery is set you can peek into the payload before the mouse button is released. IMGUI_API void EndDragDropTarget(); // only call EndDragDropTarget() if BeginDragDropTarget() returns true! - IMGUI_API const ImGuiPayload* GetDragDropPayload(); // peek directly into the current payload from anywhere. may return NULL. use ImGuiPayload::IsDataType() to test for the payload type. + IMGUI_API const ImGuiPayload* GetDragDropPayload(); // peek directly into the current payload from anywhere. returns NULL when drag and drop is finished or inactive. use ImGuiPayload::IsDataType() to test for the payload type. // Disabling [BETA API] // - Disable all user interactions and dim items visuals (applying style.DisabledAlpha over current colors) @@ -886,8 +903,6 @@ namespace ImGui IMGUI_API const char* GetStyleColorName(ImGuiCol idx); // get a string corresponding to the enum value (for display, saving, etc.). IMGUI_API void SetStateStorage(ImGuiStorage* storage); // replace current window storage with our own (if you want to manipulate it yourself, typically clear subsection of it) IMGUI_API ImGuiStorage* GetStateStorage(); - IMGUI_API bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags flags = 0); // helper to create a child window / scrolling region that looks like a normal widget frame - IMGUI_API void EndChildFrame(); // always call EndChildFrame() regardless of BeginChildFrame() return values (which indicates a collapsed/clipped window) // Text Utilities IMGUI_API ImVec2 CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f); @@ -906,6 +921,7 @@ namespace ImGui IMGUI_API bool IsKeyDown(ImGuiKey key); // is key being held. IMGUI_API bool IsKeyPressed(ImGuiKey key, bool repeat = true); // was key pressed (went from !Down to Down)? if repeat=true, uses io.KeyRepeatDelay / KeyRepeatRate IMGUI_API bool IsKeyReleased(ImGuiKey key); // was key released (went from Down to !Down)? + IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord); // was key chord (mods + key) pressed, e.g. you can pass 'ImGuiMod_Ctrl | ImGuiKey_S' as a key-chord. This doesn't do any routing or focus check, please consider using Shortcut() function instead. IMGUI_API int GetKeyPressedAmount(ImGuiKey key, float repeat_delay, float rate); // uses provided repeat rate/delay. return a count, most often 0 or 1 but might be >1 if RepeatRate is small enough that DeltaTime > RepeatRate IMGUI_API const char* GetKeyName(ImGuiKey key); // [DEBUG] returns English name of the key. Those names a provided for debugging purpose and are not meant to be saved persistently not compared. IMGUI_API void SetNextFrameWantCaptureKeyboard(bool want_capture_keyboard); // Override io.WantCaptureKeyboard flag next frame (said flag is left for your application to handle, typically when true it instructs your app to ignore inputs). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard"; after the next NewFrame() call. @@ -946,7 +962,9 @@ namespace ImGui IMGUI_API const char* SaveIniSettingsToMemory(size_t* out_ini_size = NULL); // return a zero-terminated string with the .ini data which you can save by your own mean. call when io.WantSaveIniSettings is set, then save data by your own mean and clear io.WantSaveIniSettings. // Debug Utilities + // - Your main debugging friend is the ShowMetricsWindow() function, which is also accessible from Demo->Tools->Metrics Debugger IMGUI_API void DebugTextEncoding(const char* text); + IMGUI_API void DebugFlashStyleColor(ImGuiCol idx); IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. // Memory Allocators @@ -985,21 +1003,47 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_NoBringToFrontOnFocus = 1 << 13, // Disable bringing window to front when taking focus (e.g. clicking on it or programmatically giving it focus) ImGuiWindowFlags_AlwaysVerticalScrollbar= 1 << 14, // Always show vertical scrollbar (even if ContentSize.y < Size.y) ImGuiWindowFlags_AlwaysHorizontalScrollbar=1<< 15, // Always show horizontal scrollbar (even if ContentSize.x < Size.x) - ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 16, // Ensure child windows without border uses style.WindowPadding (ignored by default for non-bordered child windows, because more convenient) - ImGuiWindowFlags_NoNavInputs = 1 << 18, // No gamepad/keyboard navigation within the window - ImGuiWindowFlags_NoNavFocus = 1 << 19, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) - ImGuiWindowFlags_UnsavedDocument = 1 << 20, // Display a dot next to the title. When used in a tab/docking context, tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. + ImGuiWindowFlags_NoNavInputs = 1 << 16, // No gamepad/keyboard navigation within the window + ImGuiWindowFlags_NoNavFocus = 1 << 17, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) + ImGuiWindowFlags_UnsavedDocument = 1 << 18, // Display a dot next to the title. When used in a tab/docking context, tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, // [Internal] - ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] On child window: allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. + ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] On child window: share focus scope, allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. ImGuiWindowFlags_ChildWindow = 1 << 24, // Don't use! For internal use by BeginChild() ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() ImGuiWindowFlags_Modal = 1 << 27, // Don't use! For internal use by BeginPopupModal() ImGuiWindowFlags_ChildMenu = 1 << 28, // Don't use! For internal use by BeginMenu() + + // Obsolete names +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 30, // Obsoleted in 1.90: Use ImGuiChildFlags_AlwaysUseWindowPadding in BeginChild() call. +#endif +}; + +// Flags for ImGui::BeginChild() +// (Legacy: bot 0 must always correspond to ImGuiChildFlags_Border to be backward compatible with old API using 'bool border = false'. +// About using AutoResizeX/AutoResizeY flags: +// - May be combined with SetNextWindowSizeConstraints() to set a min/max size for each axis (see "Demo->Child->Auto-resize with Constraints"). +// - Size measurement for a given axis is only performed when the child window is within visible boundaries, or is just appearing. +// - This allows BeginChild() to return false when not within boundaries (e.g. when scrolling), which is more optimal. BUT it won't update its auto-size while clipped. +// While not perfect, it is a better default behavior as the always-on performance gain is more valuable than the occasional "resizing after becoming visible again" glitch. +// - You may also use ImGuiChildFlags_AlwaysAutoResize to force an update even when child window is not in view. +// HOWEVER PLEASE UNDERSTAND THAT DOING SO WILL PREVENT BeginChild() FROM EVER RETURNING FALSE, disabling benefits of coarse clipping. +enum ImGuiChildFlags_ +{ + ImGuiChildFlags_None = 0, + ImGuiChildFlags_Border = 1 << 0, // Show an outer border and enable WindowPadding. (Important: this is always == 1 == true for legacy reason) + ImGuiChildFlags_AlwaysUseWindowPadding = 1 << 1, // Pad with style.WindowPadding even if no border are drawn (no padding by default for non-bordered child windows because it makes more sense) + ImGuiChildFlags_ResizeX = 1 << 2, // Allow resize from right border (layout direction). Enable .ini saving (unless ImGuiWindowFlags_NoSavedSettings passed to window flags) + ImGuiChildFlags_ResizeY = 1 << 3, // Allow resize from bottom border (layout direction). " + ImGuiChildFlags_AutoResizeX = 1 << 4, // Enable auto-resizing width. Read "IMPORTANT: Size measurement" details above. + ImGuiChildFlags_AutoResizeY = 1 << 5, // Enable auto-resizing height. Read "IMPORTANT: Size measurement" details above. + ImGuiChildFlags_AlwaysAutoResize = 1 << 6, // Combined with AutoResizeX/AutoResizeY. Always measure size even when child is hidden, always return true, always disable clipping optimization! NOT RECOMMENDED. + ImGuiChildFlags_FrameStyle = 1 << 7, // Style the child window like a framed item: use FrameBg, FrameRounding, FrameBorderSize, FramePadding instead of ChildBg, ChildRounding, ChildBorderSize, WindowPadding. }; // Flags for ImGui::InputText() @@ -1046,12 +1090,13 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Need double-click to open node ImGuiTreeNodeFlags_OpenOnArrow = 1 << 7, // Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open. ImGuiTreeNodeFlags_Leaf = 1 << 8, // No collapsing, no arrow (use as a convenience for leaf nodes). - ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow + ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow. IMPORTANT: node can still be marked open/close if you don't set the _Leaf flag! ImGuiTreeNodeFlags_FramePadding = 1 << 10, // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding(). ImGuiTreeNodeFlags_SpanAvailWidth = 1 << 11, // Extend hit box to the right-most edge, even if not framed. This is not the default in order to allow adding other items on the same line. In the future we may refactor the hit system to be front-to-back, allowing natural overlaps and then this can become the default. ImGuiTreeNodeFlags_SpanFullWidth = 1 << 12, // Extend hit box to the left-most and right-most edges (bypass the indented area). - ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 13, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) - //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 14, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible + ImGuiTreeNodeFlags_SpanAllColumns = 1 << 13, // Frame will span all columns of its container table (text will still fit in current column) + ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 14, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) + //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 15, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog, #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1087,7 +1132,7 @@ enum ImGuiSelectableFlags_ { ImGuiSelectableFlags_None = 0, ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this doesn't close parent popup window - ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Selectable frame can span all columns (text will still fit in current column) + ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Frame will span all columns of its container table (text will still fit in current column) ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text ImGuiSelectableFlags_AllowOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one @@ -1108,6 +1153,7 @@ enum ImGuiComboFlags_ ImGuiComboFlags_HeightLargest = 1 << 4, // As many fitting items as possible ImGuiComboFlags_NoArrowButton = 1 << 5, // Display on the preview box without the square arrow button ImGuiComboFlags_NoPreview = 1 << 6, // Display only a square arrow button + ImGuiComboFlags_WidthFitPreview = 1 << 7, // Width dynamically calculated from preview contents ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest, }; @@ -1118,7 +1164,7 @@ enum ImGuiTabBarFlags_ ImGuiTabBarFlags_Reorderable = 1 << 0, // Allow manually dragging tabs to re-order them + New tabs are appended at the end of list ImGuiTabBarFlags_AutoSelectNewTabs = 1 << 1, // Automatically select new tabs when they appear ImGuiTabBarFlags_TabListPopupButton = 1 << 2, // Disable buttons to open the tab list popup - ImGuiTabBarFlags_NoCloseWithMiddleMouseButton = 1 << 3, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. + ImGuiTabBarFlags_NoCloseWithMiddleMouseButton = 1 << 3, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You may handle this behavior manually on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. ImGuiTabBarFlags_NoTabListScrollingButtons = 1 << 4, // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll) ImGuiTabBarFlags_NoTooltip = 1 << 5, // Disable tooltips when hovering a tab ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 6, // Resize tabs when they don't fit @@ -1131,147 +1177,15 @@ enum ImGuiTabBarFlags_ enum ImGuiTabItemFlags_ { ImGuiTabItemFlags_None = 0, - ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Display a dot next to the title + tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. + ImGuiTabItemFlags_UnsavedDocument = 1 << 0, // Display a dot next to the title + set ImGuiTabItemFlags_NoAssumedClosure. ImGuiTabItemFlags_SetSelected = 1 << 1, // Trigger flag to programmatically make the tab selected when calling BeginTabItem() - ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You can still repro this behavior on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. - ImGuiTabItemFlags_NoPushId = 1 << 3, // Don't call PushID(tab->ID)/PopID() on BeginTabItem()/EndTabItem() + ImGuiTabItemFlags_NoCloseWithMiddleMouseButton = 1 << 2, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You may handle this behavior manually on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. + ImGuiTabItemFlags_NoPushId = 1 << 3, // Don't call PushID()/PopID() on BeginTabItem()/EndTabItem() ImGuiTabItemFlags_NoTooltip = 1 << 4, // Disable tooltip for the given tab ImGuiTabItemFlags_NoReorder = 1 << 5, // Disable reordering this tab or having another tab cross over this tab ImGuiTabItemFlags_Leading = 1 << 6, // Enforce the tab position to the left of the tab bar (after the tab list popup button) ImGuiTabItemFlags_Trailing = 1 << 7, // Enforce the tab position to the right of the tab bar (before the scrolling buttons) -}; - -// Flags for ImGui::BeginTable() -// - Important! Sizing policies have complex and subtle side effects, much more so than you would expect. -// Read comments/demos carefully + experiment with live demos to get acquainted with them. -// - The DEFAULT sizing policies are: -// - Default to ImGuiTableFlags_SizingFixedFit if ScrollX is on, or if host window has ImGuiWindowFlags_AlwaysAutoResize. -// - Default to ImGuiTableFlags_SizingStretchSame if ScrollX is off. -// - When ScrollX is off: -// - Table defaults to ImGuiTableFlags_SizingStretchSame -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch with same weight. -// - Columns sizing policy allowed: Stretch (default), Fixed/Auto. -// - Fixed Columns (if any) will generally obtain their requested width (unless the table cannot fit them all). -// - Stretch Columns will share the remaining width according to their respective weight. -// - Mixed Fixed/Stretch columns is possible but has various side-effects on resizing behaviors. -// The typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. -// (this is because the visible order of columns have subtle but necessary effects on how they react to manual resizing). -// - When ScrollX is on: -// - Table defaults to ImGuiTableFlags_SizingFixedFit -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed -// - Columns sizing policy allowed: Fixed/Auto mostly. -// - Fixed Columns can be enlarged as needed. Table will show a horizontal scrollbar if needed. -// - When using auto-resizing (non-resizable) fixed columns, querying the content width to use item right-alignment e.g. SetNextItemWidth(-FLT_MIN) doesn't make sense, would create a feedback loop. -// - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable(). -// If you specify a value for 'inner_width' then effectively the scrolling space is known and Stretch or mixed Fixed/Stretch columns become meaningful again. -// - Read on documentation at the top of imgui_tables.cpp for details. -enum ImGuiTableFlags_ -{ - // Features - ImGuiTableFlags_None = 0, - ImGuiTableFlags_Resizable = 1 << 0, // Enable resizing columns. - ImGuiTableFlags_Reorderable = 1 << 1, // Enable reordering columns in header row (need calling TableSetupColumn() + TableHeadersRow() to display headers) - ImGuiTableFlags_Hideable = 1 << 2, // Enable hiding/disabling columns in context menu. - ImGuiTableFlags_Sortable = 1 << 3, // Enable sorting. Call TableGetSortSpecs() to obtain sort specs. Also see ImGuiTableFlags_SortMulti and ImGuiTableFlags_SortTristate. - ImGuiTableFlags_NoSavedSettings = 1 << 4, // Disable persisting columns order, width and sort settings in the .ini file. - ImGuiTableFlags_ContextMenuInBody = 1 << 5, // Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow(). - // Decorations - ImGuiTableFlags_RowBg = 1 << 6, // Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent of calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually) - ImGuiTableFlags_BordersInnerH = 1 << 7, // Draw horizontal borders between rows. - ImGuiTableFlags_BordersOuterH = 1 << 8, // Draw horizontal borders at the top and bottom. - ImGuiTableFlags_BordersInnerV = 1 << 9, // Draw vertical borders between columns. - ImGuiTableFlags_BordersOuterV = 1 << 10, // Draw vertical borders on the left and right sides. - ImGuiTableFlags_BordersH = ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_BordersOuterH, // Draw horizontal borders. - ImGuiTableFlags_BordersV = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersOuterV, // Draw vertical borders. - ImGuiTableFlags_BordersInner = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersInnerH, // Draw inner borders. - ImGuiTableFlags_BordersOuter = ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_BordersOuterH, // Draw outer borders. - ImGuiTableFlags_Borders = ImGuiTableFlags_BordersInner | ImGuiTableFlags_BordersOuter, // Draw all borders. - ImGuiTableFlags_NoBordersInBody = 1 << 11, // [ALPHA] Disable vertical borders in columns Body (borders will always appear in Headers). -> May move to style - ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 12, // [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers). -> May move to style - // Sizing Policy (read above for defaults) - ImGuiTableFlags_SizingFixedFit = 1 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching contents width. - ImGuiTableFlags_SizingFixedSame = 2 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching the maximum contents width of all columns. Implicitly enable ImGuiTableFlags_NoKeepColumnsVisible. - ImGuiTableFlags_SizingStretchProp = 3 << 13, // Columns default to _WidthStretch with default weights proportional to each columns contents widths. - ImGuiTableFlags_SizingStretchSame = 4 << 13, // Columns default to _WidthStretch with default weights all equal, unless overridden by TableSetupColumn(). - // Sizing Extra Options - ImGuiTableFlags_NoHostExtendX = 1 << 16, // Make outer width auto-fit to columns, overriding outer_size.x value. Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. - ImGuiTableFlags_NoHostExtendY = 1 << 17, // Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible. - ImGuiTableFlags_NoKeepColumnsVisible = 1 << 18, // Disable keeping column always minimally visible when ScrollX is off and table gets too small. Not recommended if columns are resizable. - ImGuiTableFlags_PreciseWidths = 1 << 19, // Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth. - // Clipping - ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze(). - // Padding - ImGuiTableFlags_PadOuterX = 1 << 21, // Default if BordersOuterV is on. Enable outermost padding. Generally desirable if you have headers. - ImGuiTableFlags_NoPadOuterX = 1 << 22, // Default if BordersOuterV is off. Disable outermost padding. - ImGuiTableFlags_NoPadInnerX = 1 << 23, // Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off). - // Scrolling - ImGuiTableFlags_ScrollX = 1 << 24, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this creates a child window, ScrollY is currently generally recommended when using ScrollX. - ImGuiTableFlags_ScrollY = 1 << 25, // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. - // Sorting - ImGuiTableFlags_SortMulti = 1 << 26, // Hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1). - ImGuiTableFlags_SortTristate = 1 << 27, // Allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0). - - // [Internal] Combinations and masks - ImGuiTableFlags_SizingMask_ = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_SizingStretchSame, -}; - -// Flags for ImGui::TableSetupColumn() -enum ImGuiTableColumnFlags_ -{ - // Input configuration flags - ImGuiTableColumnFlags_None = 0, - ImGuiTableColumnFlags_Disabled = 1 << 0, // Overriding/master disable flag: hide column, won't show in context menu (unlike calling TableSetColumnEnabled() which manipulates the user accessible state) - ImGuiTableColumnFlags_DefaultHide = 1 << 1, // Default as a hidden/disabled column. - ImGuiTableColumnFlags_DefaultSort = 1 << 2, // Default as a sorting column. - ImGuiTableColumnFlags_WidthStretch = 1 << 3, // Column will stretch. Preferable with horizontal scrolling disabled (default if table sizing policy is _SizingStretchSame or _SizingStretchProp). - ImGuiTableColumnFlags_WidthFixed = 1 << 4, // Column will not stretch. Preferable with horizontal scrolling enabled (default if table sizing policy is _SizingFixedFit and table is resizable). - ImGuiTableColumnFlags_NoResize = 1 << 5, // Disable manual resizing. - ImGuiTableColumnFlags_NoReorder = 1 << 6, // Disable manual reordering this column, this will also prevent other columns from crossing over this column. - ImGuiTableColumnFlags_NoHide = 1 << 7, // Disable ability to hide/disable this column. - ImGuiTableColumnFlags_NoClip = 1 << 8, // Disable clipping for this column (all NoClip columns will render in a same draw command). - ImGuiTableColumnFlags_NoSort = 1 << 9, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). - ImGuiTableColumnFlags_NoSortAscending = 1 << 10, // Disable ability to sort in the ascending direction. - ImGuiTableColumnFlags_NoSortDescending = 1 << 11, // Disable ability to sort in the descending direction. - ImGuiTableColumnFlags_NoHeaderLabel = 1 << 12, // TableHeadersRow() will not submit label for this column. Convenient for some small columns. Name will still appear in context menu. - ImGuiTableColumnFlags_NoHeaderWidth = 1 << 13, // Disable header text width contribution to automatic column width. - ImGuiTableColumnFlags_PreferSortAscending = 1 << 14, // Make the initial sort direction Ascending when first sorting on this column (default). - ImGuiTableColumnFlags_PreferSortDescending = 1 << 15, // Make the initial sort direction Descending when first sorting on this column. - ImGuiTableColumnFlags_IndentEnable = 1 << 16, // Use current Indent value when entering cell (default for column 0). - ImGuiTableColumnFlags_IndentDisable = 1 << 17, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored. - - // Output status flags, read-only via TableGetColumnFlags() - ImGuiTableColumnFlags_IsEnabled = 1 << 24, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags. - ImGuiTableColumnFlags_IsVisible = 1 << 25, // Status: is visible == is enabled AND not clipped by scrolling. - ImGuiTableColumnFlags_IsSorted = 1 << 26, // Status: is currently part of the sort specs - ImGuiTableColumnFlags_IsHovered = 1 << 27, // Status: is hovered by mouse - - // [Internal] Combinations and masks - ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed, - ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable, - ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered, - ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30, // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) -}; - -// Flags for ImGui::TableNextRow() -enum ImGuiTableRowFlags_ -{ - ImGuiTableRowFlags_None = 0, - ImGuiTableRowFlags_Headers = 1 << 0, // Identify header row (set default background color + width of its contents accounted differently for auto column width) -}; - -// Enum for ImGui::TableSetBgColor() -// Background colors are rendering in 3 layers: -// - Layer 0: draw with RowBg0 color if set, otherwise draw with ColumnBg0 if set. -// - Layer 1: draw with RowBg1 color if set, otherwise draw with ColumnBg1 if set. -// - Layer 2: draw with CellBg color if set. -// The purpose of the two row/columns layers is to let you decide if a background color change should override or blend with the existing color. -// When using ImGuiTableFlags_RowBg on the table, each row has the RowBg0 color automatically set for odd/even rows. -// If you set the color of RowBg0 target, your color will override the existing RowBg0 color. -// If you set the color of RowBg1 or ColumnBg1 target, your color will blend over the RowBg0 color. -enum ImGuiTableBgTarget_ -{ - ImGuiTableBgTarget_None = 0, - ImGuiTableBgTarget_RowBg0 = 1, // Set row background color 0 (generally used for background, automatically set when ImGuiTableFlags_RowBg is used) - ImGuiTableBgTarget_RowBg1 = 2, // Set row background color 1 (generally used for selection marking) - ImGuiTableBgTarget_CellBg = 3, // Set cell background color (top-most color) + ImGuiTabItemFlags_NoAssumedClosure = 1 << 8, // Tab is selected when trying to close + closure is not immediately assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. }; // Flags for ImGui::IsWindowFocused() @@ -1314,16 +1228,16 @@ enum ImGuiHoveredFlags_ // e.g. 'TooltipHoveredFlagsForMouse' defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. // - for frequently actioned or hovered items providing a tooltip, you want may to use ImGuiHoveredFlags_ForTooltip (stationary + delay) so the tooltip doesn't show too often. // - for items which main purpose is to be hovered, or items with low affordance, or in less consistent apps, prefer no delay or shorter delay. - ImGuiHoveredFlags_ForTooltip = 1 << 11, // Shortcut for standard flags when using IsItemHovered() + SetTooltip() sequence. + ImGuiHoveredFlags_ForTooltip = 1 << 12, // Shortcut for standard flags when using IsItemHovered() + SetTooltip() sequence. // (Advanced) Mouse Hovering delays. // - generally you can use ImGuiHoveredFlags_ForTooltip to use application-standardized flags. // - use those if you need specific overrides. - ImGuiHoveredFlags_Stationary = 1 << 12, // Require mouse to be stationary for style.HoverStationaryDelay (~0.15 sec) _at least one time_. After this, can move on same item/window. Using the stationary test tends to reduces the need for a long delay. - ImGuiHoveredFlags_DelayNone = 1 << 13, // IsItemHovered() only: Return true immediately (default). As this is the default you generally ignore this. - ImGuiHoveredFlags_DelayShort = 1 << 14, // IsItemHovered() only: Return true after style.HoverDelayShort elapsed (~0.15 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). - ImGuiHoveredFlags_DelayNormal = 1 << 15, // IsItemHovered() only: Return true after style.HoverDelayNormal elapsed (~0.40 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). - ImGuiHoveredFlags_NoSharedDelay = 1 << 16, // IsItemHovered() only: Disable shared delay system where moving from one item to the next keeps the previous timer for a short time (standard for tooltips with long delays) + ImGuiHoveredFlags_Stationary = 1 << 13, // Require mouse to be stationary for style.HoverStationaryDelay (~0.15 sec) _at least one time_. After this, can move on same item/window. Using the stationary test tends to reduces the need for a long delay. + ImGuiHoveredFlags_DelayNone = 1 << 14, // IsItemHovered() only: Return true immediately (default). As this is the default you generally ignore this. + ImGuiHoveredFlags_DelayShort = 1 << 15, // IsItemHovered() only: Return true after style.HoverDelayShort elapsed (~0.15 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). + ImGuiHoveredFlags_DelayNormal = 1 << 16, // IsItemHovered() only: Return true after style.HoverDelayNormal elapsed (~0.40 sec) (shared between items) + requires mouse to be stationary for style.HoverStationaryDelay (once per item). + ImGuiHoveredFlags_NoSharedDelay = 1 << 17, // IsItemHovered() only: Disable shared delay system where moving from one item to the next keeps the previous timer for a short time (standard for tooltips with long delays) }; // Flags for ImGui::BeginDragDropSource(), ImGui::AcceptDragDropPayload() @@ -1383,11 +1297,17 @@ enum ImGuiSortDirection_ ImGuiSortDirection_Descending = 2 // Descending = 9->0, Z->A etc. }; +// Since 1.90, defining IMGUI_DISABLE_OBSOLETE_FUNCTIONS automatically defines IMGUI_DISABLE_OBSOLETE_KEYIO as well. +#if defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && !defined(IMGUI_DISABLE_OBSOLETE_KEYIO) +#define IMGUI_DISABLE_OBSOLETE_KEYIO +#endif + // A key identifier (ImGuiKey_XXX or ImGuiMod_XXX value): can represent Keyboard, Mouse and Gamepad values. // All our named keys are >= 512. Keys value 0 to 511 are left unused as legacy native/opaque key values (< 1.87). // Since >= 1.89 we increased typing (went from int to enum), some legacy code may need a cast to ImGuiKey. // Read details about the 1.87 and 1.89 transition : https://github.com/ocornut/imgui/issues/4921 // Note that "Keys" related to physical keys and are not the same concept as input "Characters", the later are submitted via io.AddInputCharacter(). +// The keyboard key enum values are named after the keys on a standard US keyboard, and on other keyboard types the keys reported may not match the keycaps. enum ImGuiKey : int { // Keyboard @@ -1416,6 +1336,8 @@ enum ImGuiKey : int ImGuiKey_U, ImGuiKey_V, ImGuiKey_W, ImGuiKey_X, ImGuiKey_Y, ImGuiKey_Z, ImGuiKey_F1, ImGuiKey_F2, ImGuiKey_F3, ImGuiKey_F4, ImGuiKey_F5, ImGuiKey_F6, ImGuiKey_F7, ImGuiKey_F8, ImGuiKey_F9, ImGuiKey_F10, ImGuiKey_F11, ImGuiKey_F12, + ImGuiKey_F13, ImGuiKey_F14, ImGuiKey_F15, ImGuiKey_F16, ImGuiKey_F17, ImGuiKey_F18, + ImGuiKey_F19, ImGuiKey_F20, ImGuiKey_F21, ImGuiKey_F22, ImGuiKey_F23, ImGuiKey_F24, ImGuiKey_Apostrophe, // ' ImGuiKey_Comma, // , ImGuiKey_Minus, // - @@ -1441,6 +1363,8 @@ enum ImGuiKey : int ImGuiKey_KeypadAdd, ImGuiKey_KeypadEnter, ImGuiKey_KeypadEqual, + ImGuiKey_AppBack, // Available on some keyboard/mouses. Often referred as "Browser Back" + ImGuiKey_AppForward, // Gamepad (some of those are analog values, 0.0f to 1.0f) // NAVIGATION ACTION // (download controller mapping PNG/PSD at http://dearimgui.com/controls_sheets) @@ -1509,7 +1433,7 @@ enum ImGuiKey : int #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS ImGuiKey_ModCtrl = ImGuiMod_Ctrl, ImGuiKey_ModShift = ImGuiMod_Shift, ImGuiKey_ModAlt = ImGuiMod_Alt, ImGuiKey_ModSuper = ImGuiMod_Super, // Renamed in 1.89 - ImGuiKey_KeyPadEnter = ImGuiKey_KeypadEnter, // Renamed in 1.87 + //ImGuiKey_KeyPadEnter = ImGuiKey_KeypadEnter, // Renamed in 1.87 #endif }; @@ -1564,15 +1488,15 @@ enum ImGuiCol_ ImGuiCol_FrameBg, // Background of checkbox, radio button, plot, slider, text input ImGuiCol_FrameBgHovered, ImGuiCol_FrameBgActive, - ImGuiCol_TitleBg, - ImGuiCol_TitleBgActive, - ImGuiCol_TitleBgCollapsed, + ImGuiCol_TitleBg, // Title bar + ImGuiCol_TitleBgActive, // Title bar when focused + ImGuiCol_TitleBgCollapsed, // Title bar when collapsed ImGuiCol_MenuBarBg, ImGuiCol_ScrollbarBg, ImGuiCol_ScrollbarGrab, ImGuiCol_ScrollbarGrabHovered, ImGuiCol_ScrollbarGrabActive, - ImGuiCol_CheckMark, + ImGuiCol_CheckMark, // Checkbox tick and RadioButton circle ImGuiCol_SliderGrab, ImGuiCol_SliderGrabActive, ImGuiCol_Button, @@ -1643,6 +1567,7 @@ enum ImGuiStyleVar_ ImGuiStyleVar_GrabMinSize, // float GrabMinSize ImGuiStyleVar_GrabRounding, // float GrabRounding ImGuiStyleVar_TabRounding, // float TabRounding + ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign ImGuiStyleVar_SeparatorTextBorderSize,// float SeparatorTextBorderSize @@ -1763,7 +1688,7 @@ enum ImGuiMouseSource : int ImGuiMouseSource_COUNT }; -// Enumeration for ImGui::SetWindow***(), SetNextWindow***(), SetNextItem***() functions +// Enumeration for ImGui::SetNextWindow***(), SetWindow***(), SetNextItem***() functions // Represent a condition. // Important: Treat as a regular enum! Do NOT combine multiple values using binary operators! All the functions above treat 0 as a shortcut to ImGuiCond_Always. enum ImGuiCond_ @@ -1775,6 +1700,170 @@ enum ImGuiCond_ ImGuiCond_Appearing = 1 << 3, // Set the variable if the object/window is appearing after being hidden/inactive (or the first time) }; +//----------------------------------------------------------------------------- +// [SECTION] Tables API flags and structures (ImGuiTableFlags, ImGuiTableColumnFlags, ImGuiTableRowFlags, ImGuiTableBgTarget, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) +//----------------------------------------------------------------------------- + +// Flags for ImGui::BeginTable() +// - Important! Sizing policies have complex and subtle side effects, much more so than you would expect. +// Read comments/demos carefully + experiment with live demos to get acquainted with them. +// - The DEFAULT sizing policies are: +// - Default to ImGuiTableFlags_SizingFixedFit if ScrollX is on, or if host window has ImGuiWindowFlags_AlwaysAutoResize. +// - Default to ImGuiTableFlags_SizingStretchSame if ScrollX is off. +// - When ScrollX is off: +// - Table defaults to ImGuiTableFlags_SizingStretchSame -> all Columns defaults to ImGuiTableColumnFlags_WidthStretch with same weight. +// - Columns sizing policy allowed: Stretch (default), Fixed/Auto. +// - Fixed Columns (if any) will generally obtain their requested width (unless the table cannot fit them all). +// - Stretch Columns will share the remaining width according to their respective weight. +// - Mixed Fixed/Stretch columns is possible but has various side-effects on resizing behaviors. +// The typical use of mixing sizing policies is: any number of LEADING Fixed columns, followed by one or two TRAILING Stretch columns. +// (this is because the visible order of columns have subtle but necessary effects on how they react to manual resizing). +// - When ScrollX is on: +// - Table defaults to ImGuiTableFlags_SizingFixedFit -> all Columns defaults to ImGuiTableColumnFlags_WidthFixed +// - Columns sizing policy allowed: Fixed/Auto mostly. +// - Fixed Columns can be enlarged as needed. Table will show a horizontal scrollbar if needed. +// - When using auto-resizing (non-resizable) fixed columns, querying the content width to use item right-alignment e.g. SetNextItemWidth(-FLT_MIN) doesn't make sense, would create a feedback loop. +// - Using Stretch columns OFTEN DOES NOT MAKE SENSE if ScrollX is on, UNLESS you have specified a value for 'inner_width' in BeginTable(). +// If you specify a value for 'inner_width' then effectively the scrolling space is known and Stretch or mixed Fixed/Stretch columns become meaningful again. +// - Read on documentation at the top of imgui_tables.cpp for details. +enum ImGuiTableFlags_ +{ + // Features + ImGuiTableFlags_None = 0, + ImGuiTableFlags_Resizable = 1 << 0, // Enable resizing columns. + ImGuiTableFlags_Reorderable = 1 << 1, // Enable reordering columns in header row (need calling TableSetupColumn() + TableHeadersRow() to display headers) + ImGuiTableFlags_Hideable = 1 << 2, // Enable hiding/disabling columns in context menu. + ImGuiTableFlags_Sortable = 1 << 3, // Enable sorting. Call TableGetSortSpecs() to obtain sort specs. Also see ImGuiTableFlags_SortMulti and ImGuiTableFlags_SortTristate. + ImGuiTableFlags_NoSavedSettings = 1 << 4, // Disable persisting columns order, width and sort settings in the .ini file. + ImGuiTableFlags_ContextMenuInBody = 1 << 5, // Right-click on columns body/contents will display table context menu. By default it is available in TableHeadersRow(). + // Decorations + ImGuiTableFlags_RowBg = 1 << 6, // Set each RowBg color with ImGuiCol_TableRowBg or ImGuiCol_TableRowBgAlt (equivalent of calling TableSetBgColor with ImGuiTableBgFlags_RowBg0 on each row manually) + ImGuiTableFlags_BordersInnerH = 1 << 7, // Draw horizontal borders between rows. + ImGuiTableFlags_BordersOuterH = 1 << 8, // Draw horizontal borders at the top and bottom. + ImGuiTableFlags_BordersInnerV = 1 << 9, // Draw vertical borders between columns. + ImGuiTableFlags_BordersOuterV = 1 << 10, // Draw vertical borders on the left and right sides. + ImGuiTableFlags_BordersH = ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_BordersOuterH, // Draw horizontal borders. + ImGuiTableFlags_BordersV = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersOuterV, // Draw vertical borders. + ImGuiTableFlags_BordersInner = ImGuiTableFlags_BordersInnerV | ImGuiTableFlags_BordersInnerH, // Draw inner borders. + ImGuiTableFlags_BordersOuter = ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_BordersOuterH, // Draw outer borders. + ImGuiTableFlags_Borders = ImGuiTableFlags_BordersInner | ImGuiTableFlags_BordersOuter, // Draw all borders. + ImGuiTableFlags_NoBordersInBody = 1 << 11, // [ALPHA] Disable vertical borders in columns Body (borders will always appear in Headers). -> May move to style + ImGuiTableFlags_NoBordersInBodyUntilResize = 1 << 12, // [ALPHA] Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers). -> May move to style + // Sizing Policy (read above for defaults) + ImGuiTableFlags_SizingFixedFit = 1 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching contents width. + ImGuiTableFlags_SizingFixedSame = 2 << 13, // Columns default to _WidthFixed or _WidthAuto (if resizable or not resizable), matching the maximum contents width of all columns. Implicitly enable ImGuiTableFlags_NoKeepColumnsVisible. + ImGuiTableFlags_SizingStretchProp = 3 << 13, // Columns default to _WidthStretch with default weights proportional to each columns contents widths. + ImGuiTableFlags_SizingStretchSame = 4 << 13, // Columns default to _WidthStretch with default weights all equal, unless overridden by TableSetupColumn(). + // Sizing Extra Options + ImGuiTableFlags_NoHostExtendX = 1 << 16, // Make outer width auto-fit to columns, overriding outer_size.x value. Only available when ScrollX/ScrollY are disabled and Stretch columns are not used. + ImGuiTableFlags_NoHostExtendY = 1 << 17, // Make outer height stop exactly at outer_size.y (prevent auto-extending table past the limit). Only available when ScrollX/ScrollY are disabled. Data below the limit will be clipped and not visible. + ImGuiTableFlags_NoKeepColumnsVisible = 1 << 18, // Disable keeping column always minimally visible when ScrollX is off and table gets too small. Not recommended if columns are resizable. + ImGuiTableFlags_PreciseWidths = 1 << 19, // Disable distributing remainder width to stretched columns (width allocation on a 100-wide table with 3 columns: Without this flag: 33,33,34. With this flag: 33,33,33). With larger number of columns, resizing will appear to be less smooth. + // Clipping + ImGuiTableFlags_NoClip = 1 << 20, // Disable clipping rectangle for every individual columns (reduce draw command count, items will be able to overflow into other columns). Generally incompatible with TableSetupScrollFreeze(). + // Padding + ImGuiTableFlags_PadOuterX = 1 << 21, // Default if BordersOuterV is on. Enable outermost padding. Generally desirable if you have headers. + ImGuiTableFlags_NoPadOuterX = 1 << 22, // Default if BordersOuterV is off. Disable outermost padding. + ImGuiTableFlags_NoPadInnerX = 1 << 23, // Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off). + // Scrolling + ImGuiTableFlags_ScrollX = 1 << 24, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Changes default sizing policy. Because this creates a child window, ScrollY is currently generally recommended when using ScrollX. + ImGuiTableFlags_ScrollY = 1 << 25, // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. + // Sorting + ImGuiTableFlags_SortMulti = 1 << 26, // Hold shift when clicking headers to sort on multiple column. TableGetSortSpecs() may return specs where (SpecsCount > 1). + ImGuiTableFlags_SortTristate = 1 << 27, // Allow no sorting, disable default sorting. TableGetSortSpecs() may return specs where (SpecsCount == 0). + // Miscellaneous + ImGuiTableFlags_HighlightHoveredColumn = 1 << 28, // Highlight column headers when hovered (may evolve into a fuller highlight) + + // [Internal] Combinations and masks + ImGuiTableFlags_SizingMask_ = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_SizingFixedSame | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_SizingStretchSame, +}; + +// Flags for ImGui::TableSetupColumn() +enum ImGuiTableColumnFlags_ +{ + // Input configuration flags + ImGuiTableColumnFlags_None = 0, + ImGuiTableColumnFlags_Disabled = 1 << 0, // Overriding/master disable flag: hide column, won't show in context menu (unlike calling TableSetColumnEnabled() which manipulates the user accessible state) + ImGuiTableColumnFlags_DefaultHide = 1 << 1, // Default as a hidden/disabled column. + ImGuiTableColumnFlags_DefaultSort = 1 << 2, // Default as a sorting column. + ImGuiTableColumnFlags_WidthStretch = 1 << 3, // Column will stretch. Preferable with horizontal scrolling disabled (default if table sizing policy is _SizingStretchSame or _SizingStretchProp). + ImGuiTableColumnFlags_WidthFixed = 1 << 4, // Column will not stretch. Preferable with horizontal scrolling enabled (default if table sizing policy is _SizingFixedFit and table is resizable). + ImGuiTableColumnFlags_NoResize = 1 << 5, // Disable manual resizing. + ImGuiTableColumnFlags_NoReorder = 1 << 6, // Disable manual reordering this column, this will also prevent other columns from crossing over this column. + ImGuiTableColumnFlags_NoHide = 1 << 7, // Disable ability to hide/disable this column. + ImGuiTableColumnFlags_NoClip = 1 << 8, // Disable clipping for this column (all NoClip columns will render in a same draw command). + ImGuiTableColumnFlags_NoSort = 1 << 9, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). + ImGuiTableColumnFlags_NoSortAscending = 1 << 10, // Disable ability to sort in the ascending direction. + ImGuiTableColumnFlags_NoSortDescending = 1 << 11, // Disable ability to sort in the descending direction. + ImGuiTableColumnFlags_NoHeaderLabel = 1 << 12, // TableHeadersRow() will not submit horizontal label for this column. Convenient for some small columns. Name will still appear in context menu or in angled headers. + ImGuiTableColumnFlags_NoHeaderWidth = 1 << 13, // Disable header text width contribution to automatic column width. + ImGuiTableColumnFlags_PreferSortAscending = 1 << 14, // Make the initial sort direction Ascending when first sorting on this column (default). + ImGuiTableColumnFlags_PreferSortDescending = 1 << 15, // Make the initial sort direction Descending when first sorting on this column. + ImGuiTableColumnFlags_IndentEnable = 1 << 16, // Use current Indent value when entering cell (default for column 0). + ImGuiTableColumnFlags_IndentDisable = 1 << 17, // Ignore current Indent value when entering cell (default for columns > 0). Indentation changes _within_ the cell will still be honored. + ImGuiTableColumnFlags_AngledHeader = 1 << 18, // TableHeadersRow() will submit an angled header row for this column. Note this will add an extra row. + + // Output status flags, read-only via TableGetColumnFlags() + ImGuiTableColumnFlags_IsEnabled = 1 << 24, // Status: is enabled == not hidden by user/api (referred to as "Hide" in _DefaultHide and _NoHide) flags. + ImGuiTableColumnFlags_IsVisible = 1 << 25, // Status: is visible == is enabled AND not clipped by scrolling. + ImGuiTableColumnFlags_IsSorted = 1 << 26, // Status: is currently part of the sort specs + ImGuiTableColumnFlags_IsHovered = 1 << 27, // Status: is hovered by mouse + + // [Internal] Combinations and masks + ImGuiTableColumnFlags_WidthMask_ = ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_WidthFixed, + ImGuiTableColumnFlags_IndentMask_ = ImGuiTableColumnFlags_IndentEnable | ImGuiTableColumnFlags_IndentDisable, + ImGuiTableColumnFlags_StatusMask_ = ImGuiTableColumnFlags_IsEnabled | ImGuiTableColumnFlags_IsVisible | ImGuiTableColumnFlags_IsSorted | ImGuiTableColumnFlags_IsHovered, + ImGuiTableColumnFlags_NoDirectResize_ = 1 << 30, // [Internal] Disable user resizing this column directly (it may however we resized indirectly from its left edge) +}; + +// Flags for ImGui::TableNextRow() +enum ImGuiTableRowFlags_ +{ + ImGuiTableRowFlags_None = 0, + ImGuiTableRowFlags_Headers = 1 << 0, // Identify header row (set default background color + width of its contents accounted differently for auto column width) +}; + +// Enum for ImGui::TableSetBgColor() +// Background colors are rendering in 3 layers: +// - Layer 0: draw with RowBg0 color if set, otherwise draw with ColumnBg0 if set. +// - Layer 1: draw with RowBg1 color if set, otherwise draw with ColumnBg1 if set. +// - Layer 2: draw with CellBg color if set. +// The purpose of the two row/columns layers is to let you decide if a background color change should override or blend with the existing color. +// When using ImGuiTableFlags_RowBg on the table, each row has the RowBg0 color automatically set for odd/even rows. +// If you set the color of RowBg0 target, your color will override the existing RowBg0 color. +// If you set the color of RowBg1 or ColumnBg1 target, your color will blend over the RowBg0 color. +enum ImGuiTableBgTarget_ +{ + ImGuiTableBgTarget_None = 0, + ImGuiTableBgTarget_RowBg0 = 1, // Set row background color 0 (generally used for background, automatically set when ImGuiTableFlags_RowBg is used) + ImGuiTableBgTarget_RowBg1 = 2, // Set row background color 1 (generally used for selection marking) + ImGuiTableBgTarget_CellBg = 3, // Set cell background color (top-most color) +}; + +// Sorting specifications for a table (often handling sort specs for a single column, occasionally more) +// Obtained by calling TableGetSortSpecs(). +// When 'SpecsDirty == true' you can sort your data. It will be true with sorting specs have changed since last call, or the first time. +// Make sure to set 'SpecsDirty = false' after sorting, else you may wastefully sort your data every frame! +struct ImGuiTableSortSpecs +{ + const ImGuiTableColumnSortSpecs* Specs; // Pointer to sort spec array. + int SpecsCount; // Sort spec count. Most often 1. May be > 1 when ImGuiTableFlags_SortMulti is enabled. May be == 0 when ImGuiTableFlags_SortTristate is enabled. + bool SpecsDirty; // Set to true when specs have changed since last time! Use this to sort again, then clear the flag. + + ImGuiTableSortSpecs() { memset(this, 0, sizeof(*this)); } +}; + +// Sorting specification for one column of a table (sizeof == 12 bytes) +struct ImGuiTableColumnSortSpecs +{ + ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) + ImS16 ColumnIndex; // Index of the column + ImS16 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) + ImGuiSortDirection SortDirection : 8; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending + + ImGuiTableColumnSortSpecs() { memset(this, 0, sizeof(*this)); } +}; + //----------------------------------------------------------------------------- // [SECTION] Helpers: Memory allocations macros, ImVector<> //----------------------------------------------------------------------------- @@ -1864,6 +1953,7 @@ struct ImVector inline bool contains(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data++ == v) return true; return false; } inline T* find(const T& v) { T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } inline const T* find(const T& v) const { const T* data = Data; const T* data_end = Data + Size; while (data < data_end) if (*data == v) break; else ++data; return data; } + inline int find_index(const T& v) const { const T* data_end = Data + Size; const T* it = find(v); if (it == data_end) return -1; const ptrdiff_t off = it - Data; return (int)off; } inline bool find_erase(const T& v) { const T* it = find(v); if (it < Data + Size) { erase(it); return true; } return false; } inline bool find_erase_unsorted(const T& v) { const T* it = find(v); if (it < Data + Size) { erase_unsorted(it); return true; } return false; } inline int index_from_ptr(const T* it) const { IM_ASSERT(it >= Data && it < Data + Size); const ptrdiff_t off = it - Data; return (int)off; } @@ -1897,7 +1987,7 @@ struct ImGuiStyle float FrameBorderSize; // Thickness of border around frames. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines. ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label). - ImVec2 CellPadding; // Padding within a table cell + ImVec2 CellPadding; // Padding within a table cell. CellPadding.y may be altered between different rows. ImVec2 TouchExtraPadding; // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! float IndentSpacing; // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). @@ -1909,6 +1999,8 @@ struct ImGuiStyle float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. float TabBorderSize; // Thickness of border around tabs. float TabMinWidthForCloseButton; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + float TabBarBorderSize; // Thickness of tab-bar separator, which takes on the tab active color to denote focus. + float TableAngledHeadersAngle; // Angle of angled headers (supported values range from -50.0f degrees to +50.0f degrees). ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -1998,16 +2090,22 @@ struct ImGuiIO // Debug options //------------------------------------------------------------------ + // Option to enable various debug tools showing buttons that will call the IM_DEBUG_BREAK() macro. + // - The Item Picker tool will be available regardless of this being enabled, in order to maximize its discoverability. + // - Requires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application. + // e.g. io.ConfigDebugIsDebuggerPresent = ::IsDebuggerPresent() on Win32, or refer to ImOsIsDebuggerPresent() imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version). + bool ConfigDebugIsDebuggerPresent; // = false // Enable various tools calling IM_DEBUG_BREAK(). + // Tools to test correct Begin/End and BeginChild/EndChild behaviors. - // Presently Begin()/End() and BeginChild()/EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() - // This is inconsistent with other BeginXXX functions and create confusion for many users. - // We expect to update the API eventually. In the meanwhile we provide tools to facilitate checking user-code behavior. + // - Presently Begin()/End() and BeginChild()/EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() + // - This is inconsistent with other BeginXXX functions and create confusion for many users. + // - We expect to update the API eventually. In the meanwhile we provide tools to facilitate checking user-code behavior. bool ConfigDebugBeginReturnValueOnce;// = false // First-time calls to Begin()/BeginChild() will return false. NEEDS TO BE SET AT APPLICATION BOOT TIME if you don't want to miss windows. bool ConfigDebugBeginReturnValueLoop;// = false // Some calls to Begin()/BeginChild() will return false. Will cycle through window depths then repeat. Suggested use: add "io.ConfigDebugBeginReturnValue = io.KeyShift" in your main loop then occasionally press SHIFT. Windows should be flickering while running. - // Option to deactivate io.AddFocusEvent(false) handling. May facilitate interactions with a debugger when focus loss leads to clearing inputs data. - // Backends may have other side-effects on focus loss, so this will reduce side-effects but not necessary remove all of them. - // Consider using e.g. Win32's IsDebuggerPresent() as an additional filter (or see ImOsIsDebuggerPresent() in imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version). + // Option to deactivate io.AddFocusEvent(false) handling. + // - May facilitate interactions with a debugger when focus loss leads to clearing inputs data. + // - Backends may have other side-effects on focus loss, so this will reduce side-effects but not necessary remove all of them. bool ConfigDebugIgnoreFocusLoss; // = false // Ignore io.AddFocusEvent(false), consequently not calling io.ClearInputKeys() in input processing. // Options to audit .ini data @@ -2034,11 +2132,9 @@ struct ImGuiIO // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) // (default to use native imm32 api on Windows) void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - void* ImeWindowHandle; // = NULL // [Obsolete] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. -#else - void* _UnusedPadding; // Unused field to keep data structure the same size. -#endif + + // Optional: Platform locale + ImWchar PlatformLocaleDecimalPoint; // '.' // [Experimental] Configure decimal point e.g. '.' or ',' useful for some languages (e.g. German), generally pulled from *localeconv()->decimal_point //------------------------------------------------------------------ // Input - Call before calling NewFrame() @@ -2058,8 +2154,11 @@ struct ImGuiIO IMGUI_API void SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index = -1); // [Optional] Specify index for legacy <1.87 IsKeyXXX() functions with native indices + specify native keycode, scancode. IMGUI_API void SetAppAcceptingEvents(bool accepting_events); // Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. - IMGUI_API void ClearInputCharacters(); // [Internal] Clear the text input buffer manually - IMGUI_API void ClearInputKeys(); // [Internal] Release all keys + IMGUI_API void ClearEventsQueue(); // Clear all incoming events. + IMGUI_API void ClearInputKeys(); // Clear current keyboard/mouse/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + IMGUI_API void ClearInputCharacters(); // [Obsoleted in 1.89.8] Clear the current frame text input buffer. Now included within ClearInputKeys(). +#endif //------------------------------------------------------------------ // Output - Updated by NewFrame() or EndFrame()/Render() @@ -2079,7 +2178,6 @@ struct ImGuiIO int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 int MetricsRenderWindows; // Number of visible windows int MetricsActiveWindows; // Number of active windows - int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. // Legacy: before 1.87, we required backend to fill io.KeyMap[] (imgui->native map) during initialization and io.KeysDown[] (native indices) every frame. @@ -2090,6 +2188,11 @@ struct ImGuiIO bool KeysDown[ImGuiKey_COUNT]; // [LEGACY] Input: Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). This used to be [512] sized. It is now ImGuiKey_COUNT to allow legacy io.KeysDown[GetKeyIndex(...)] to work without an overflow. float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. #endif +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + void* ImeWindowHandle; // = NULL // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. +#else + void* _UnusedPadding; +#endif //------------------------------------------------------------------ // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! @@ -2140,7 +2243,7 @@ struct ImGuiIO }; //----------------------------------------------------------------------------- -// [SECTION] Misc data structures +// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload) //----------------------------------------------------------------------------- // Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used. @@ -2214,30 +2317,6 @@ struct ImGuiPayload bool IsDelivery() const { return Delivery; } }; -// Sorting specification for one column of a table (sizeof == 12 bytes) -struct ImGuiTableColumnSortSpecs -{ - ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) - ImS16 ColumnIndex; // Index of the column - ImS16 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) - ImGuiSortDirection SortDirection : 8; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending (you can use this or SortSign, whichever is more convenient for your sort function) - - ImGuiTableColumnSortSpecs() { memset(this, 0, sizeof(*this)); } -}; - -// Sorting specifications for a table (often handling sort specs for a single column, occasionally more) -// Obtained by calling TableGetSortSpecs(). -// When 'SpecsDirty == true' you can sort your data. It will be true with sorting specs have changed since last call, or the first time. -// Make sure to set 'SpecsDirty = false' after sorting, else you may wastefully sort your data every frame! -struct ImGuiTableSortSpecs -{ - const ImGuiTableColumnSortSpecs* Specs; // Pointer to sort spec array. - int SpecsCount; // Sort spec count. Most often 1. May be > 1 when ImGuiTableFlags_SortMulti is enabled. May be == 0 when ImGuiTableFlags_SortTristate is enabled. - bool SpecsDirty; // Set to true when specs have changed since last time! Use this to sort again, then clear the flag. - - ImGuiTableSortSpecs() { memset(this, 0, sizeof(*this)); } -}; - //----------------------------------------------------------------------------- // [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor) //----------------------------------------------------------------------------- @@ -2321,9 +2400,9 @@ struct ImGuiStorage { ImGuiID key; union { int val_i; float val_f; void* val_p; }; - ImGuiStoragePair(ImGuiID _key, int _val_i) { key = _key; val_i = _val_i; } - ImGuiStoragePair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; } - ImGuiStoragePair(ImGuiID _key, void* _val_p) { key = _key; val_p = _val_p; } + ImGuiStoragePair(ImGuiID _key, int _val) { key = _key; val_i = _val; } + ImGuiStoragePair(ImGuiID _key, float _val) { key = _key; val_f = _val; } + ImGuiStoragePair(ImGuiID _key, void* _val) { key = _key; val_p = _val; } }; ImVector Data; @@ -2350,11 +2429,10 @@ struct ImGuiStorage IMGUI_API float* GetFloatRef(ImGuiID key, float default_val = 0.0f); IMGUI_API void** GetVoidPtrRef(ImGuiID key, void* default_val = NULL); - // Use on your own storage if you know only integer are being stored (open/close all tree nodes) - IMGUI_API void SetAllInt(int val); - - // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. + // Advanced: for quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. IMGUI_API void BuildSortByKey(); + // Obsolete: use on your own storage if you know only integer are being stored (open/close all tree nodes) + IMGUI_API void SetAllInt(int val); }; // Helper: Manually clip large list of items. @@ -2395,12 +2473,14 @@ struct ImGuiListClipper IMGUI_API void End(); // Automatically called on the last call of Step() that returns false. IMGUI_API bool Step(); // Call until it returns false. The DisplayStart/DisplayEnd fields will be set and you can process/draw those items. - // Call IncludeRangeByIndices() *BEFORE* first call to Step() if you need a range of items to not be clipped, regardless of their visibility. + // Call IncludeItemByIndex() or IncludeItemsByIndex() *BEFORE* first call to Step() if you need a range of items to not be clipped, regardless of their visibility. // (Due to alignment / padding of certain items it is possible that an extra item may be included on either end of the display range). - IMGUI_API void IncludeRangeByIndices(int item_begin, int item_end); // item_end is exclusive e.g. use (42, 42+1) to make item 42 never clipped. + inline void IncludeItemByIndex(int item_index) { IncludeItemsByIndex(item_index, item_index + 1); } + IMGUI_API void IncludeItemsByIndex(int item_begin, int item_end); // item_end is exclusive e.g. use (42, 42+1) to make item 42 never clipped. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeRangeByIndices(item_begin, item_end); } // [renamed in 1.89.6] + inline void IncludeRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.9] + inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.6] //inline ImGuiListClipper(int items_count, float items_height = -1.0f) { memset(this, 0, sizeof(*this)); ItemsCount = -1; Begin(items_count, items_height); } // [removed in 1.79] #endif }; @@ -2425,9 +2505,13 @@ static inline ImVec2& operator+=(ImVec2& lhs, const ImVec2& rhs) { lhs.x static inline ImVec2& operator-=(ImVec2& lhs, const ImVec2& rhs) { lhs.x -= rhs.x; lhs.y -= rhs.y; return lhs; } static inline ImVec2& operator*=(ImVec2& lhs, const ImVec2& rhs) { lhs.x *= rhs.x; lhs.y *= rhs.y; return lhs; } static inline ImVec2& operator/=(ImVec2& lhs, const ImVec2& rhs) { lhs.x /= rhs.x; lhs.y /= rhs.y; return lhs; } +static inline bool operator==(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; } +static inline bool operator!=(const ImVec2& lhs, const ImVec2& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y; } static inline ImVec4 operator+(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w + rhs.w); } static inline ImVec4 operator-(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w); } static inline ImVec4 operator*(const ImVec4& lhs, const ImVec4& rhs) { return ImVec4(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z, lhs.w * rhs.w); } +static inline bool operator==(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w; } +static inline bool operator!=(const ImVec4& lhs, const ImVec4& rhs) { return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z || lhs.w != rhs.w; } IM_MSVC_RUNTIME_CHECKS_RESTORE #endif @@ -2464,8 +2548,8 @@ struct ImColor constexpr ImColor() { } constexpr ImColor(float r, float g, float b, float a = 1.0f) : Value(r, g, b, a) { } constexpr ImColor(const ImVec4& col) : Value(col) {} - ImColor(int r, int g, int b, int a = 255) { float sc = 1.0f / 255.0f; Value.x = (float)r * sc; Value.y = (float)g * sc; Value.z = (float)b * sc; Value.w = (float)a * sc; } - ImColor(ImU32 rgba) { float sc = 1.0f / 255.0f; Value.x = (float)((rgba >> IM_COL32_R_SHIFT) & 0xFF) * sc; Value.y = (float)((rgba >> IM_COL32_G_SHIFT) & 0xFF) * sc; Value.z = (float)((rgba >> IM_COL32_B_SHIFT) & 0xFF) * sc; Value.w = (float)((rgba >> IM_COL32_A_SHIFT) & 0xFF) * sc; } + constexpr ImColor(int r, int g, int b, int a = 255) : Value((float)r * (1.0f / 255.0f), (float)g * (1.0f / 255.0f), (float)b * (1.0f / 255.0f), (float)a* (1.0f / 255.0f)) {} + constexpr ImColor(ImU32 rgba) : Value((float)((rgba >> IM_COL32_R_SHIFT) & 0xFF) * (1.0f / 255.0f), (float)((rgba >> IM_COL32_G_SHIFT) & 0xFF) * (1.0f / 255.0f), (float)((rgba >> IM_COL32_B_SHIFT) & 0xFF) * (1.0f / 255.0f), (float)((rgba >> IM_COL32_A_SHIFT) & 0xFF) * (1.0f / 255.0f)) {} inline operator ImU32() const { return ImGui::ColorConvertFloat4ToU32(Value); } inline operator ImVec4() const { return Value; } @@ -2497,9 +2581,9 @@ typedef void (*ImDrawCallback)(const ImDrawList* parent_list, const ImDrawCmd* c // Special Draw callback value to request renderer backend to reset the graphics/render state. // The renderer backend needs to handle this special value, otherwise it will crash trying to call a function at this address. -// This is useful for example if you submitted callbacks which you know have altered the render state and you want it to be restored. -// It is not done by default because they are many perfectly useful way of altering render state for imgui contents (e.g. changing shader/blending settings before an Image call). -#define ImDrawCallback_ResetRenderState (ImDrawCallback)(-1) +// This is useful, for example, if you submitted callbacks which you know have altered the render state and you want it to be restored. +// Render state is not reset by default because they are many perfectly useful way of altering render state (e.g. changing shader/blending settings before an Image call). +#define ImDrawCallback_ResetRenderState (ImDrawCallback)(-8) // Typically, 1 command = 1 GPU draw call (unless command is a callback) // - VtxOffset: When 'io.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset' is enabled, @@ -2663,6 +2747,8 @@ struct ImDrawList IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 0); IMGUI_API void AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness = 1.0f); IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments); + IMGUI_API void AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f); + IMGUI_API void AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness); @@ -2687,6 +2773,7 @@ struct ImDrawList inline void PathStroke(ImU32 col, ImDrawFlags flags = 0, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, flags, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 0); IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle + IMGUI_API void PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments = 0); // Ellipse IMGUI_API void PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments = 0); // Quadratic Bezier (3 control points) IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawFlags flags = 0); @@ -2699,7 +2786,7 @@ struct ImDrawList // Advanced: Channels // - Use to split render into layers. By switching channels to can render out-of-order (e.g. submit FG primitives before BG primitives) // - Use to minimize draw calls (e.g. if going back-and-forth between multiple clipping rectangles, prefer to append into separate channels then merge at the end) - // - FIXME-OBSOLETE: This API shouldn't have been in ImDrawList in the first place! + // - This API shouldn't have been in ImDrawList in the first place! // Prefer using your own persistent instance of ImDrawListSplitter as you can stack them. // Using the ImDrawList::ChannelsXXXX you cannot stack a split over another. inline void ChannelsSplit(int count) { _Splitter.Split(this, count); } @@ -2740,18 +2827,20 @@ struct ImDrawList // as this is one of the oldest structure exposed by the library! Basically, ImDrawList == CmdList) struct ImDrawData { - bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. - int CmdListsCount; // Number of ImDrawList* to render - int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size - int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size - ImDrawList** CmdLists; // Array of ImDrawList* to render. The ImDrawList are owned by ImGuiContext and only pointed to from here. - ImVec2 DisplayPos; // Top-left position of the viewport to render (== top-left of the orthogonal projection matrix to use) (== GetMainViewport()->Pos for the main viewport, == (0.0) in most single-viewport applications) - ImVec2 DisplaySize; // Size of the viewport to render (== GetMainViewport()->Size for the main viewport, == io.DisplaySize in most single-viewport applications) - ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display. + bool Valid; // Only valid after Render() is called and before the next NewFrame() is called. + int CmdListsCount; // Number of ImDrawList* to render (should always be == CmdLists.size) + int TotalIdxCount; // For convenience, sum of all ImDrawList's IdxBuffer.Size + int TotalVtxCount; // For convenience, sum of all ImDrawList's VtxBuffer.Size + ImVector CmdLists; // Array of ImDrawList* to render. The ImDrawLists are owned by ImGuiContext and only pointed to from here. + ImVec2 DisplayPos; // Top-left position of the viewport to render (== top-left of the orthogonal projection matrix to use) (== GetMainViewport()->Pos for the main viewport, == (0.0) in most single-viewport applications) + ImVec2 DisplaySize; // Size of the viewport to render (== GetMainViewport()->Size for the main viewport, == io.DisplaySize in most single-viewport applications) + ImVec2 FramebufferScale; // Amount of pixels for each unit of DisplaySize. Based on io.DisplayFramebufferScale. Generally (1,1) on normal display, (2,2) on OSX with Retina display. + ImGuiViewport* OwnerViewport; // Viewport carrying the ImDrawData instance, might be of use to the renderer (generally not). // Functions ImDrawData() { Clear(); } - void Clear() { memset(this, 0, sizeof(*this)); } // The ImDrawList are owned by ImGuiContext! + IMGUI_API void Clear(); + IMGUI_API void AddDrawList(ImDrawList* draw_list); // Helper to add an external draw list into an existing ImDrawData. IMGUI_API void DeIndexAllBuffers(); // Helper to convert all buffers from indexed to non-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! IMGUI_API void ScaleClipRects(const ImVec2& fb_scale); // Helper to scale the ClipRect field of each ImDrawCmd. Use if your final output buffer is at a different scale than Dear ImGui expects, or if there is a difference between your window resolution and framebuffer resolution. }; @@ -2767,7 +2856,7 @@ struct ImFontConfig bool FontDataOwnedByAtlas; // true // TTF/OTF data ownership taken by the container ImFontAtlas (will delete memory itself). int FontNo; // 0 // Index of font within TTF/OTF file float SizePixels; // // Size in pixels for rasterizer (more or less maps to the resulting font height). - int OversampleH; // 3 // Rasterize at higher quality for sub-pixel positioning. Note the difference between 2 and 3 is minimal so you can reduce this to 2 to save memory. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. + int OversampleH; // 2 // Rasterize at higher quality for sub-pixel positioning. Note the difference between 2 and 3 is minimal. You can reduce this to 1 for large glyphs save memory. Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details. int OversampleV; // 1 // Rasterize at higher quality for sub-pixel positioning. This is not really useful as we don't use sub-pixel positions on the Y axis. bool PixelSnapH; // false // Align every glyph to pixel boundary. Useful e.g. if you are merging a non-pixel aligned font with the default font. If enabled, you can set OversampleH/V to 1. ImVec2 GlyphExtraSpacing; // 0, 0 // Extra spacing (in pixels) between glyphs. Only X axis is supported for now. @@ -2777,7 +2866,8 @@ struct ImFontConfig float GlyphMaxAdvanceX; // FLT_MAX // Maximum AdvanceX for glyphs bool MergeMode; // false // Merge into previous ImFont, so you can combine multiple inputs font into one ImFont (e.g. ASCII font + icons + Japanese glyphs). You may want to use GlyphOffset.y when merge font of different heights. unsigned int FontBuilderFlags; // 0 // Settings for custom font builder. THIS IS BUILDER IMPLEMENTATION DEPENDENT. Leave as zero if unsure. - float RasterizerMultiply; // 1.0f // Brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. + float RasterizerMultiply; // 1.0f // Linearly brighten (>1.0f) or darken (<1.0f) font output. Brightening small fonts may be a good workaround to make them more readable. This is a silly thing we may remove in the future. + float RasterizerDensity; // 1.0f // DPI scale for rasterization, not altering other font metrics: make it easy to swap between e.g. a 100% and a 400% fonts for a zooming display. IMPORTANT: If you increase this it is expected that you increase font scale accordingly, otherwise quality may look lowered. ImWchar EllipsisChar; // -1 // Explicitly specify unicode codepoint of ellipsis character. When fonts are being merged first specified ellipsis will be used. // [Internal] @@ -2861,8 +2951,8 @@ struct ImFontAtlas IMGUI_API ImFont* AddFont(const ImFontConfig* font_cfg); IMGUI_API ImFont* AddFontDefault(const ImFontConfig* font_cfg = NULL); IMGUI_API ImFont* AddFontFromFileTTF(const char* filename, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); - IMGUI_API ImFont* AddFontFromMemoryTTF(void* font_data, int font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed. - IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp. + IMGUI_API ImFont* AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // Note: Transfer ownership of 'ttf_data' to ImFontAtlas! Will be deleted after destruction of the atlas. Set font_cfg->FontDataOwnedByAtlas=false to keep ownership of your data and it won't be freed. + IMGUI_API ImFont* AddFontFromMemoryCompressedTTF(const void* compressed_font_data, int compressed_font_data_size, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data' still owned by caller. Compress with binary_to_compressed_c.cpp. IMGUI_API ImFont* AddFontFromMemoryCompressedBase85TTF(const char* compressed_font_data_base85, float size_pixels, const ImFontConfig* font_cfg = NULL, const ImWchar* glyph_ranges = NULL); // 'compressed_font_data_base85' still owned by caller. Compress with binary_to_compressed_c.cpp with -base85 parameter. IMGUI_API void ClearInputData(); // Clear input data (all ImFontConfig structures including sizes, TTF data, glyph ranges, etc.) = all the data used to build the texture and fonts. IMGUI_API void ClearTexData(); // Clear output texture data (CPU side). Saves RAM once the texture has been copied to graphics memory. @@ -3081,6 +3171,14 @@ namespace ImGui #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.90.0 (from September 2023) + static inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); } + static inline void EndChildFrame() { EndChild(); } + //static inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border + //static inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border + static inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); } + IMGUI_API bool ListBox(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int height_in_items = -1); + IMGUI_API bool Combo(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int popup_max_height_in_items = -1); // OBSOLETED in 1.89.7 (from June 2023) IMGUI_API void SetItemAllowOverlap(); // Use SetNextItemAllowOverlap() before item. // OBSOLETED in 1.89.4 (from March 2023) @@ -3091,12 +3189,12 @@ namespace ImGui // OBSOLETED in 1.88 (from May 2022) static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. - // OBSOLETED in 1.86 (from November 2021) - IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Calculate coarse clipping for large list of evenly sized items. Prefer using ImGuiListClipper. - // OBSOLETED in 1.85 (from August 2021) - static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) + //-- OBSOLETED in 1.86 (from November 2021) + //IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Code removed, see 1.90 for last version of the code. Calculate range of visible items for large list of evenly sized items. Prefer using ImGuiListClipper. + //-- OBSOLETED in 1.85 (from August 2021) + //static inline float GetWindowContentRegionWidth() { return GetWindowContentRegionMax().x - GetWindowContentRegionMin().x; } //-- OBSOLETED in 1.81 (from February 2021) //static inline bool ListBoxHeader(const char* label, const ImVec2& size = ImVec2(0, 0)) { return BeginListBox(label, size); } //static inline bool ListBoxHeader(const char* label, int items_count, int height_in_items = -1) { float height = GetTextLineHeightWithSpacing() * ((height_in_items < 0 ? ImMin(items_count, 7) : height_in_items) + 0.25f) + GetStyle().FramePadding.y * 2.0f; return BeginListBox(label, ImVec2(0.0f, height)); } // Helper to calculate size from items_count and height_in_items @@ -3147,21 +3245,21 @@ namespace ImGui //static inline void SetScrollPosHere() { SetScrollHere(); } // OBSOLETED in 1.42 } -// OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect() -typedef ImDrawFlags ImDrawCornerFlags; -enum ImDrawCornerFlags_ -{ - ImDrawCornerFlags_None = ImDrawFlags_RoundCornersNone, // Was == 0 prior to 1.82, this is now == ImDrawFlags_RoundCornersNone which is != 0 and not implicit - ImDrawCornerFlags_TopLeft = ImDrawFlags_RoundCornersTopLeft, // Was == 0x01 (1 << 0) prior to 1.82. Order matches ImDrawFlags_NoRoundCorner* flag (we exploit this internally). - ImDrawCornerFlags_TopRight = ImDrawFlags_RoundCornersTopRight, // Was == 0x02 (1 << 1) prior to 1.82. - ImDrawCornerFlags_BotLeft = ImDrawFlags_RoundCornersBottomLeft, // Was == 0x04 (1 << 2) prior to 1.82. - ImDrawCornerFlags_BotRight = ImDrawFlags_RoundCornersBottomRight, // Was == 0x08 (1 << 3) prior to 1.82. - ImDrawCornerFlags_All = ImDrawFlags_RoundCornersAll, // Was == 0x0F prior to 1.82 - ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, - ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, - ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, - ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, -}; +//-- OBSOLETED in 1.82 (from Mars 2021): flags for AddRect(), AddRectFilled(), AddImageRounded(), PathRect() +//typedef ImDrawFlags ImDrawCornerFlags; +//enum ImDrawCornerFlags_ +//{ +// ImDrawCornerFlags_None = ImDrawFlags_RoundCornersNone, // Was == 0 prior to 1.82, this is now == ImDrawFlags_RoundCornersNone which is != 0 and not implicit +// ImDrawCornerFlags_TopLeft = ImDrawFlags_RoundCornersTopLeft, // Was == 0x01 (1 << 0) prior to 1.82. Order matches ImDrawFlags_NoRoundCorner* flag (we exploit this internally). +// ImDrawCornerFlags_TopRight = ImDrawFlags_RoundCornersTopRight, // Was == 0x02 (1 << 1) prior to 1.82. +// ImDrawCornerFlags_BotLeft = ImDrawFlags_RoundCornersBottomLeft, // Was == 0x04 (1 << 2) prior to 1.82. +// ImDrawCornerFlags_BotRight = ImDrawFlags_RoundCornersBottomRight, // Was == 0x08 (1 << 3) prior to 1.82. +// ImDrawCornerFlags_All = ImDrawFlags_RoundCornersAll, // Was == 0x0F prior to 1.82 +// ImDrawCornerFlags_Top = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_TopRight, +// ImDrawCornerFlags_Bot = ImDrawCornerFlags_BotLeft | ImDrawCornerFlags_BotRight, +// ImDrawCornerFlags_Left = ImDrawCornerFlags_TopLeft | ImDrawCornerFlags_BotLeft, +// ImDrawCornerFlags_Right = ImDrawCornerFlags_TopRight | ImDrawCornerFlags_BotRight, +//}; // RENAMED and MERGED both ImGuiKey_ModXXX and ImGuiModFlags_XXX into ImGuiMod_XXX (from September 2022) // RENAMED ImGuiKeyModFlags -> ImGuiModFlags in 1.88 (from April 2022). Exceptionally commented out ahead of obscolescence schedule to reduce confusion and because they were not meant to be used in the first place. @@ -3170,6 +3268,8 @@ enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl //typedef ImGuiKeyChord ImGuiKeyModFlags; // == int //enum ImGuiKeyModFlags_ { ImGuiKeyModFlags_None = 0, ImGuiKeyModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiKeyModFlags_Shift = ImGuiMod_Shift, ImGuiKeyModFlags_Alt = ImGuiMod_Alt, ImGuiKeyModFlags_Super = ImGuiMod_Super }; +#define IM_OFFSETOF(_TYPE,_MEMBER) offsetof(_TYPE, _MEMBER) // OBSOLETED IN 1.90 (now using C++11 standard version) + #endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // RENAMED IMGUI_DISABLE_METRICS_WINDOW > IMGUI_DISABLE_DEBUG_TOOLS in 1.88 (from June 2022) @@ -3192,9 +3292,14 @@ enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl #pragma warning (pop) #endif -// Include imgui_user.h at the end of imgui.h (convenient for user to only explicitly include vanilla imgui.h) +// Include imgui_user.h at the end of imgui.h +// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included. #ifdef IMGUI_INCLUDE_IMGUI_USER_H +#ifdef IMGUI_USER_H_FILENAME +#include IMGUI_USER_H_FILENAME +#else #include "imgui_user.h" #endif +#endif #endif // #ifndef IMGUI_DISABLE diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index 698b0d4b..6a9acd05 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -11,11 +11,15 @@ // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. -// Read online: https://github.com/ocornut/imgui/tree/master/docs +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp #pragma once #include "imgui.h" // IMGUI_IMPL_API +#ifndef IMGUI_DISABLE struct GLFWwindow; struct GLFWmonitor; @@ -26,6 +30,11 @@ IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool ins IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); +// Emscripten related initialization phase methods +#ifdef __EMSCRIPTEN__ +IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector); +#endif + // GLFW callbacks install // - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any. // - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks. @@ -45,3 +54,5 @@ IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event); + +#endif // #ifndef IMGUI_DISABLE diff --git a/include/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h index 77d11801..23eb9247 100644 --- a/include/imgui/imgui_impl_opengl3.h +++ b/include/imgui/imgui_impl_opengl3.h @@ -14,8 +14,11 @@ // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. -// Read online: https://github.com/ocornut/imgui/tree/master/docs +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp // About GLSL version: // The 'glsl_version' initialization parameter should be nullptr (default) or a "#version XXX" string. @@ -24,6 +27,7 @@ #pragma once #include "imgui.h" // IMGUI_IMPL_API +#ifndef IMGUI_DISABLE // Backend API IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); @@ -58,3 +62,5 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); #endif #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 7ca72e37..85c58c4e 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -118,7 +118,7 @@ extern "C" { ** included as . ** ** glcorearb.h includes only APIs in the latest OpenGL core profile -** implementation together with APIs in newer ARB extensions which +** implementation together with APIs in newer ARB extensions which ** can be supported by the core profile. It does not, and never will ** include functionality removed from the core profile, such as ** fixed-function vertex and fragment processing. @@ -467,7 +467,7 @@ GL3W_API int imgl3wIsSupported(int major, int minor); GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); /* gl3w internal state */ -union GL3WProcs { +union ImGL3WProcs { GL3WglProc ptr[59]; struct { PFNGLACTIVETEXTUREPROC ActiveTexture; @@ -532,7 +532,7 @@ union GL3WProcs { } gl; }; -GL3W_API extern union GL3WProcs imgl3wProcs; +GL3W_API extern union ImGL3WProcs imgl3wProcs; /* OpenGL functions */ #define glActiveTexture imgl3wProcs.gl.ActiveTexture @@ -608,7 +608,7 @@ extern "C" { #include -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define GL3W_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #if defined(_WIN32) #ifndef WIN32_LEAN_AND_MEAN @@ -666,7 +666,12 @@ static GL3WglProc (*glx_get_proc_address)(const GLubyte *); static int open_libgl(void) { - libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); + // While most systems use libGL.so.1, NetBSD seems to use that libGL.so.3. See https://github.com/ocornut/imgui/issues/6983 + libgl = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); + if (!libgl) + libgl = dlopen("libGL.so.3", RTLD_LAZY | RTLD_LOCAL); if (!libgl) return GL3W_ERROR_LIBRARY_OPEN; *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); @@ -794,12 +799,12 @@ static const char *proc_names[] = { "glViewport", }; -GL3W_API union GL3WProcs imgl3wProcs; +GL3W_API union ImGL3WProcs imgl3wProcs; static void load_procs(GL3WGetProcAddressProc proc) { size_t i; - for (i = 0; i < ARRAY_SIZE(proc_names); i++) + for (i = 0; i < GL3W_ARRAY_SIZE(proc_names); i++) imgl3wProcs.ptr[i] = proc(proc_names[i]); } diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index 30990481..1a55e171 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,12 +1,7 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (internal structures/api) // You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. -// To implement maths operators for ImVec2 (disabled by default to not conflict with using IM_VEC2_CLASS_EXTRA with your own math types+operators), use: -/* -#define IMGUI_DEFINE_MATH_OPERATORS -#include "imgui_internal.h" -*/ /* @@ -23,6 +18,7 @@ Index of this file: // [SECTION] Inputs support // [SECTION] Clipper support // [SECTION] Navigation support +// [SECTION] Typing-select support // [SECTION] Columns support // [SECTION] Multi-select support // [SECTION] Docking support @@ -81,7 +77,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' #endif #pragma clang diagnostic ignored "-Wunknown-pragmas" // warning: unknown warning group 'xxx' -#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok, for ImFloorSigned() +#pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants ok, for ImFloor() #pragma clang diagnostic ignored "-Wunused-function" // for stb_textedit.h #pragma clang diagnostic ignored "-Wmissing-prototypes" // for stb_textedit.h #pragma clang diagnostic ignored "-Wold-style-cast" @@ -135,6 +131,7 @@ struct ImGuiLastItemData; // Status storage for last submitted items struct ImGuiLocEntry; // A localization entry. struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result +struct ImGuiNavTreeNodeData; // Temporary storage for last TreeNode() being a Left arrow landing candidate. struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions @@ -152,6 +149,8 @@ struct ImGuiTableInstanceData; // Storage for one instance of a same table struct ImGuiTableTempData; // Temporary storage for one table (one per table in the stack), shared between tables. struct ImGuiTableSettings; // Storage for a table .ini settings struct ImGuiTableColumnsSettings; // Storage for a column .ini settings +struct ImGuiTypingSelectState; // Storage for GetTypingSelectRequest() +struct ImGuiTypingSelectRequest; // Storage for GetTypingSelectRequest() (aimed to be public) struct ImGuiWindow; // Storage for one window struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame, in practice we currently keep it for each window) struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session) @@ -177,6 +176,7 @@ typedef int ImGuiScrollFlags; // -> enum ImGuiScrollFlags_ // F typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // Flags: for SeparatorEx() typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() +typedef int ImGuiTypingSelectFlags; // -> enum ImGuiTypingSelectFlags_ // Flags: for GetTypingSelectRequest() typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...); @@ -196,13 +196,13 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer namespace ImStb { -#undef STB_TEXTEDIT_STRING -#undef STB_TEXTEDIT_CHARTYPE -#define STB_TEXTEDIT_STRING ImGuiInputTextState -#define STB_TEXTEDIT_CHARTYPE ImWchar -#define STB_TEXTEDIT_GETWIDTH_NEWLINE (-1.0f) -#define STB_TEXTEDIT_UNDOSTATECOUNT 99 -#define STB_TEXTEDIT_UNDOCHARCOUNT 999 +#undef IMSTB_TEXTEDIT_STRING +#undef IMSTB_TEXTEDIT_CHARTYPE +#define IMSTB_TEXTEDIT_STRING ImGuiInputTextState +#define IMSTB_TEXTEDIT_CHARTYPE ImWchar +#define IMSTB_TEXTEDIT_GETWIDTH_NEWLINE (-1.0f) +#define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 +#define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 #include "imstb_textedit.h" } // namespace ImStb @@ -227,13 +227,13 @@ namespace ImStb #else #define IMGUI_DEBUG_LOG(...) ((void)0) #endif -#define IMGUI_DEBUG_LOG_ACTIVEID(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventActiveId) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_FOCUS(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFocus) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_POPUP(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_NAV(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventNav) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_SELECTION(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection)IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) -#define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_ACTIVEID(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventActiveId) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_FOCUS(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFocus) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_POPUP(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_NAV(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventNav) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_SELECTION(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) // Static Asserts #define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") @@ -266,10 +266,13 @@ namespace ImStb #define IM_MEMALIGN(_OFF,_ALIGN) (((_OFF) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1)) // Memory align e.g. IM_ALIGN(0,4)=0, IM_ALIGN(1,4)=4, IM_ALIGN(4,4)=4, IM_ALIGN(5,4)=8 #define IM_F32_TO_INT8_UNBOUND(_VAL) ((int)((_VAL) * 255.0f + ((_VAL)>=0 ? 0.5f : -0.5f))) // Unsaturated, for display purpose #define IM_F32_TO_INT8_SAT(_VAL) ((int)(ImSaturate(_VAL) * 255.0f + 0.5f)) // Saturated, always output 0..255 -#define IM_FLOOR(_VAL) ((float)(int)(_VAL)) // ImFloor() is not inlined in MSVC debug builds +#define IM_TRUNC(_VAL) ((float)(int)(_VAL)) // ImTrunc() is not inlined in MSVC debug builds #define IM_ROUND(_VAL) ((float)(int)((_VAL) + 0.5f)) // #define IM_STRINGIFY_HELPER(_X) #_X #define IM_STRINGIFY(_X) IM_STRINGIFY_HELPER(_X) // Preprocessor idiom to stringify e.g. an integer. +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +#define IM_FLOOR IM_TRUNC +#endif // Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall #ifdef _MSC_VER @@ -304,6 +307,18 @@ namespace ImStb #endif #endif // #ifndef IM_DEBUG_BREAK +// Format specifiers, printing 64-bit hasn't been decently standardized... +// In a real application you should be using PRId64 and PRIu64 from (non-windows) and on Windows define them yourself. +#if defined(_MSC_VER) && !defined(__clang__) +#define IM_PRId64 "I64d" +#define IM_PRIu64 "I64u" +#define IM_PRIX64 "I64X" +#else +#define IM_PRId64 "lld" +#define IM_PRIu64 "llu" +#define IM_PRIX64 "llX" +#endif + //----------------------------------------------------------------------------- // [SECTION] Generic helpers // Note that the ImXXX helpers functions are lower-level than ImGui functions. @@ -347,18 +362,18 @@ static inline bool ImIsPowerOfTwo(ImU64 v) { return v != 0 && (v & static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; } // Helpers: String -IMGUI_API int ImStricmp(const char* str1, const char* str2); -IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); -IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); -IMGUI_API char* ImStrdup(const char* str); -IMGUI_API char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str); -IMGUI_API const char* ImStrchrRange(const char* str_begin, const char* str_end, char c); -IMGUI_API int ImStrlenW(const ImWchar* str); +IMGUI_API int ImStricmp(const char* str1, const char* str2); // Case insensitive compare. +IMGUI_API int ImStrnicmp(const char* str1, const char* str2, size_t count); // Case insensitive compare to a certain count. +IMGUI_API void ImStrncpy(char* dst, const char* src, size_t count); // Copy to a certain count and always zero terminate (strncpy doesn't). +IMGUI_API char* ImStrdup(const char* str); // Duplicate a string. +IMGUI_API char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* str); // Copy in provided buffer, recreate buffer if needed. +IMGUI_API const char* ImStrchrRange(const char* str_begin, const char* str_end, char c); // Find first occurrence of 'c' in string range. IMGUI_API const char* ImStreolRange(const char* str, const char* str_end); // End end-of-line -IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line -IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); -IMGUI_API void ImStrTrimBlanks(char* str); -IMGUI_API const char* ImStrSkipBlank(const char* str); +IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end, const char* needle, const char* needle_end); // Find a substring in a string range. +IMGUI_API void ImStrTrimBlanks(char* str); // Remove leading and trailing blanks from a buffer. +IMGUI_API const char* ImStrSkipBlank(const char* str); // Find first non-blank character. +IMGUI_API int ImStrlenW(const ImWchar* str); // Computer string length (ImWchar string) +IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line (ImWchar string) IM_MSVC_RUNTIME_CHECKS_OFF static inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; } static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } @@ -385,6 +400,7 @@ IMGUI_API int ImTextStrFromUtf8(ImWchar* out_buf, int out_buf_size, co IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char* in_text_end); // return number of UTF-8 code-points (NOT bytes count) IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 +IMGUI_API const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr); // return previous UTF-8 code-point. // Helpers: File System #ifdef IMGUI_DISABLE_FILE_FUNCTIONS @@ -420,7 +436,6 @@ IM_MSVC_RUNTIME_CHECKS_OFF #define ImAcos(X) acosf(X) #define ImAtan2(Y, X) atan2f((Y), (X)) #define ImAtof(STR) atof(STR) -//#define ImFloorStd(X) floorf(X) // We use our own, see ImFloor() and ImFloorSigned() #define ImCeil(X) ceilf(X) static inline float ImPow(float x, float y) { return powf(x, y); } // DragBehaviorT/SliderBehaviorT uses ImPow with either float/double and need the precision static inline double ImPow(double x, double y) { return pow(x, y); } @@ -458,10 +473,10 @@ static inline float ImSaturate(float f) static inline float ImLengthSqr(const ImVec2& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y); } static inline float ImLengthSqr(const ImVec4& lhs) { return (lhs.x * lhs.x) + (lhs.y * lhs.y) + (lhs.z * lhs.z) + (lhs.w * lhs.w); } static inline float ImInvLength(const ImVec2& lhs, float fail_value) { float d = (lhs.x * lhs.x) + (lhs.y * lhs.y); if (d > 0.0f) return ImRsqrt(d); return fail_value; } -static inline float ImFloor(float f) { return (float)(int)(f); } -static inline float ImFloorSigned(float f) { return (float)((f >= 0 || (float)(int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf() -static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } -static inline ImVec2 ImFloorSigned(const ImVec2& v) { return ImVec2(ImFloorSigned(v.x), ImFloorSigned(v.y)); } +static inline float ImTrunc(float f) { return (float)(int)(f); } +static inline ImVec2 ImTrunc(const ImVec2& v) { return ImVec2((float)(int)(v.x), (float)(int)(v.y)); } +static inline float ImFloor(float f) { return (float)((f >= 0 || (float)(int)f == f) ? (int)f : (int)f - 1); } // Decent replacement for floorf() +static inline ImVec2 ImFloor(const ImVec2& v) { return ImVec2(ImFloor(v.x), ImFloor(v.y)); } static inline int ImModPositive(int a, int b) { return (a + b) % b; } static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } @@ -524,6 +539,7 @@ struct IMGUI_API ImRect ImVec2 GetBR() const { return Max; } // Bottom-right bool Contains(const ImVec2& p) const { return p.x >= Min.x && p.y >= Min.y && p.x < Max.x && p.y < Max.y; } bool Contains(const ImRect& r) const { return r.Min.x >= Min.x && r.Min.y >= Min.y && r.Max.x <= Max.x && r.Max.y <= Max.y; } + bool ContainsWithPad(const ImVec2& p, const ImVec2& pad) const { return p.x >= Min.x - pad.x && p.y >= Min.y - pad.y && p.x < Max.x + pad.x && p.y < Max.y + pad.y; } bool Overlaps(const ImRect& r) const { return r.Min.y < Max.y && r.Max.y > Min.y && r.Min.x < Max.x && r.Max.x > Min.x; } void Add(const ImVec2& p) { if (Min.x > p.x) Min.x = p.x; if (Min.y > p.y) Min.y = p.y; if (Max.x < p.x) Max.x = p.x; if (Max.y < p.y) Max.y = p.y; } void Add(const ImRect& r) { if (Min.x > r.Min.x) Min.x = r.Min.x; if (Min.y > r.Min.y) Min.y = r.Min.y; if (Max.x < r.Max.x) Max.x = r.Max.x; if (Max.y < r.Max.y) Max.y = r.Max.y; } @@ -534,7 +550,7 @@ struct IMGUI_API ImRect void TranslateY(float dy) { Min.y += dy; Max.y += dy; } void ClipWith(const ImRect& r) { Min = ImMax(Min, r.Min); Max = ImMin(Max, r.Max); } // Simple version, may lead to an inverted rectangle, which is fine for Contains/Overlaps test but not for display. void ClipWithFull(const ImRect& r) { Min = ImClamp(Min, r.Min, r.Max); Max = ImClamp(Max, r.Min, r.Max); } // Full version, ensure both points are fully clipped. - void Floor() { Min.x = IM_FLOOR(Min.x); Min.y = IM_FLOOR(Min.y); Max.x = IM_FLOOR(Max.x); Max.y = IM_FLOOR(Max.y); } + void Floor() { Min.x = IM_TRUNC(Min.x); Min.y = IM_TRUNC(Min.y); Max.x = IM_TRUNC(Max.x); Max.y = IM_TRUNC(Max.y); } bool IsInverted() const { return Min.x > Max.x || Min.y > Max.y; } ImVec4 ToVec4() const { return ImVec4(Min.x, Min.y, Max.x, Max.y); } }; @@ -699,7 +715,6 @@ struct ImChunkStream int offset_from_ptr(const T* p) { IM_ASSERT(p >= begin() && p < end()); const ptrdiff_t off = (const char*)p - Buf.Data; return (int)off; } T* ptr_from_offset(int off) { IM_ASSERT(off >= 4 && off < Buf.Size); return (T*)(void*)(Buf.Data + off); } void swap(ImChunkStream& rhs) { rhs.Buf.swap(Buf); } - }; // Helper: ImGuiTextIndex<> @@ -772,12 +787,10 @@ struct IMGUI_API ImDrawListSharedData struct ImDrawDataBuilder { - ImVector Layers[2]; // Global layers for: regular, tooltip + ImVector* Layers[2]; // Pointers to global layers for: regular, tooltip. LayersP[0] is owned by DrawData. + ImVector LayerData1; - void Clear() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].resize(0); } - void ClearFreeMemory() { for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) Layers[n].clear(); } - int GetDrawListCount() const { int count = 0; for (int n = 0; n < IM_ARRAYSIZE(Layers); n++) count += Layers[n].Size; return count; } - IMGUI_API void FlattenIntoSingleLayer(); + ImDrawDataBuilder() { memset(this, 0, sizeof(*this)); } }; //----------------------------------------------------------------------------- @@ -802,10 +815,11 @@ enum ImGuiItemFlags_ ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. ImGuiItemFlags_NoWindowHoverableCheck = 1 << 8, // false // Disable hoverable check in ItemHoverable() - ImGuiItemflags_AllowOverlap = 1 << 9, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. + ImGuiItemFlags_AllowOverlap = 1 << 9, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. // Controlled by widget code ImGuiItemFlags_Inputable = 1 << 10, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. + ImGuiItemFlags_HasSelectionUserData = 1 << 11, // false // Set by SetNextItemSelectionUserData() }; // Status flags for an already submitted item @@ -821,8 +835,8 @@ enum ImGuiItemStatusFlags_ ImGuiItemStatusFlags_HasDeactivated = 1 << 5, // Set if the widget/group is able to provide data for the ImGuiItemStatusFlags_Deactivated flag. ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. ImGuiItemStatusFlags_HoveredWindow = 1 << 7, // Override the HoveredWindow test to allow cross-window hover testing. - ImGuiItemStatusFlags_FocusedByTabbing = 1 << 8, // Set when the Focusable item just got focused by Tabbing (FIXME: to be removed soon) - ImGuiItemStatusFlags_Visible = 1 << 9, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()). + ImGuiItemStatusFlags_Visible = 1 << 8, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()). + ImGuiItemStatusFlags_HasClipRect = 1 << 9, // g.LastItemData.ClipRect is valid // Additional status + semantic for ImGuiTestEngine #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -886,7 +900,7 @@ enum ImGuiComboFlagsPrivate_ enum ImGuiSliderFlagsPrivate_ { ImGuiSliderFlags_Vertical = 1 << 20, // Should this slider be orientated vertically? - ImGuiSliderFlags_ReadOnly = 1 << 21, + ImGuiSliderFlags_ReadOnly = 1 << 21, // Consider using g.NextItemData.ItemFlags |= ImGuiItemFlags_ReadOnly instead. }; // Extend ImGuiSelectableFlags_ @@ -1044,6 +1058,7 @@ struct IMGUI_API ImGuiGroupData ImGuiID WindowID; ImVec2 BackupCursorPos; ImVec2 BackupCursorMaxPos; + ImVec2 BackupCursorPosPrevLine; ImVec1 BackupIndent; ImVec1 BackupGroupOffset; ImVec2 BackupCurrLineSize; @@ -1051,6 +1066,7 @@ struct IMGUI_API ImGuiGroupData ImGuiID BackupActiveIdIsAlive; bool BackupActiveIdPreviousFrameIsAlive; bool BackupHoveredIdIsAlive; + bool BackupIsSameLine; bool EmitItem; }; @@ -1105,7 +1121,7 @@ struct IMGUI_API ImGuiInputTextState void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } int GetUndoAvailCount() const { return Stb.undostate.undo_point; } - int GetRedoAvailCount() const { return STB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; } + int GetRedoAvailCount() const { return IMSTB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; } void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation // Cursor & Selection @@ -1145,6 +1161,7 @@ enum ImGuiNextWindowDataFlags_ ImGuiNextWindowDataFlags_HasFocus = 1 << 5, ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6, ImGuiNextWindowDataFlags_HasScroll = 1 << 7, + ImGuiNextWindowDataFlags_HasChildFlags = 1 << 8, }; // Storage for SetNexWindow** functions @@ -1159,6 +1176,7 @@ struct ImGuiNextWindowData ImVec2 SizeVal; ImVec2 ContentSizeVal; ImVec2 ScrollVal; + ImGuiChildFlags ChildFlags; bool CollapsedVal; ImRect SizeConstraintRect; ImGuiSizeCallback SizeCallback; @@ -1170,6 +1188,10 @@ struct ImGuiNextWindowData inline void ClearFlags() { Flags = ImGuiNextWindowDataFlags_None; } }; +// Multi-Selection item index or identifier when using SetNextItemSelectionUserData()/BeginMultiSelect() +// (Most users are likely to use this store an item INDEX but this may be used to store a POINTER as well.) +typedef ImS64 ImGuiSelectionUserData; + enum ImGuiNextItemDataFlags_ { ImGuiNextItemDataFlags_None = 0, @@ -1180,13 +1202,14 @@ enum ImGuiNextItemDataFlags_ struct ImGuiNextItemData { ImGuiNextItemDataFlags Flags; - ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemflags_AllowOverlap. - float Width; // Set by SetNextItemWidth() - ImGuiID FocusScopeId; // Set by SetNextItemMultiSelectData() (!= 0 signify value has been set, so it's an alternate version of HasSelectionData, we don't use Flags for this because they are cleared too early. This is mostly used for debugging) + ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemFlags_AllowOverlap. + // Non-flags members are NOT cleared by ItemAdd() meaning they are still valid during NavProcessItem() + float Width; // Set by SetNextItemWidth() + ImGuiSelectionUserData SelectionUserData; // Set by SetNextItemSelectionUserData() (note that NULL/0 is a valid value, we use -1 == ImGuiSelectionUserData_Invalid to mark invalid values) ImGuiCond OpenCond; - bool OpenVal; // Set by SetNextItemOpen() + bool OpenVal; // Set by SetNextItemOpen() - ImGuiNextItemData() { memset(this, 0, sizeof(*this)); } + ImGuiNextItemData() { memset(this, 0, sizeof(*this)); SelectionUserData = -1; } inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! }; @@ -1198,11 +1221,23 @@ struct ImGuiLastItemData ImGuiItemStatusFlags StatusFlags; // See ImGuiItemStatusFlags_ ImRect Rect; // Full rectangle ImRect NavRect; // Navigation scoring rectangle (not displayed) - ImRect DisplayRect; // Display rectangle (only if ImGuiItemStatusFlags_HasDisplayRect is set) + // Rarely used fields are not explicitly cleared, only valid when the corresponding ImGuiItemStatusFlags is set. + ImRect DisplayRect; // Display rectangle (ONLY VALID IF ImGuiItemStatusFlags_HasDisplayRect is set) + ImRect ClipRect; // Clip rectangle at the time of submitting item (ONLY VALID IF ImGuiItemStatusFlags_HasClipRect is set) ImGuiLastItemData() { memset(this, 0, sizeof(*this)); } }; +// Store data emitted by TreeNode() for usage by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere. +// This is the minimum amount of data that we need to perform the equivalent of NavApplyItemToResult() and which we can't infer in TreePop() +// Only stored when the node is a potential candidate for landing on a Left arrow jump. +struct ImGuiNavTreeNodeData +{ + ImGuiID ID; + ImGuiItemFlags InFlags; + ImRect NavRect; +}; + struct IMGUI_API ImGuiStackSizes { short SizeOfIDStack; @@ -1223,9 +1258,9 @@ struct IMGUI_API ImGuiStackSizes // Data saved for each window pushed into the stack struct ImGuiWindowStackData { - ImGuiWindow* Window; - ImGuiLastItemData ParentLastItemDataBackup; - ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting + ImGuiWindow* Window; + ImGuiLastItemData ParentLastItemDataBackup; + ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting }; struct ImGuiShrinkWidthItem @@ -1369,23 +1404,29 @@ struct ImGuiKeyOwnerData // Don't mistake with ImGuiInputTextFlags! (for ImGui::InputText() function) enum ImGuiInputFlags_ { - // Flags for IsKeyPressed(), IsMouseClicked(), Shortcut() + // Flags for IsKeyPressed(), IsKeyChordPressed(), IsMouseClicked(), Shortcut() ImGuiInputFlags_None = 0, ImGuiInputFlags_Repeat = 1 << 0, // Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) ImGuiInputFlags_RepeatRateNavMove = 1 << 2, // Repeat rate: Fast ImGuiInputFlags_RepeatRateNavTweak = 1 << 3, // Repeat rate: Faster - ImGuiInputFlags_RepeatRateMask_ = ImGuiInputFlags_RepeatRateDefault | ImGuiInputFlags_RepeatRateNavMove | ImGuiInputFlags_RepeatRateNavTweak, + + // Specify when repeating key pressed can be interrupted. + // In theory ImGuiInputFlags_RepeatUntilOtherKeyPress may be a desirable default, but it would break too many behavior so everything is opt-in. + ImGuiInputFlags_RepeatUntilRelease = 1 << 4, // Stop repeating when released (default for all functions except Shortcut). This only exists to allow overriding Shortcut() default behavior. + ImGuiInputFlags_RepeatUntilKeyModsChange = 1 << 5, // Stop repeating when released OR if keyboard mods are changed (default for Shortcut) + ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone = 1 << 6, // Stop repeating when released OR if keyboard mods are leaving the None state. Allows going from Mod+Key to Key by releasing Mod. + ImGuiInputFlags_RepeatUntilOtherKeyPress = 1 << 7, // Stop repeating when released OR if any other keyboard key is pressed during the repeat // Flags for SetItemKeyOwner() - ImGuiInputFlags_CondHovered = 1 << 4, // Only set if item is hovered (default to both) - ImGuiInputFlags_CondActive = 1 << 5, // Only set if item is active (default to both) + ImGuiInputFlags_CondHovered = 1 << 8, // Only set if item is hovered (default to both) + ImGuiInputFlags_CondActive = 1 << 9, // Only set if item is active (default to both) ImGuiInputFlags_CondDefault_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, ImGuiInputFlags_CondMask_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, // Flags for SetKeyOwner(), SetItemKeyOwner() - ImGuiInputFlags_LockThisFrame = 1 << 6, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. - ImGuiInputFlags_LockUntilRelease = 1 << 7, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. + ImGuiInputFlags_LockThisFrame = 1 << 10, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. + ImGuiInputFlags_LockUntilRelease = 1 << 11, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. // Routing policies for Shortcut() + low-level SetShortcutRouting() // - The general idea is that several callers register interest in a shortcut, and only one owner gets it. @@ -1397,18 +1438,22 @@ enum ImGuiInputFlags_ // - Using ImGuiInputFlags_RouteAlways is roughly equivalent to doing e.g. IsKeyPressed(key) + testing mods. // - Priorities: GlobalHigh > Focused (when owner is active item) > Global > Focused (when focused window) > GlobalLow. // - Can select only 1 policy among all available. - ImGuiInputFlags_RouteFocused = 1 << 8, // (Default) Register focused route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. - ImGuiInputFlags_RouteGlobalLow = 1 << 9, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority. - ImGuiInputFlags_RouteGlobal = 1 << 10, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText). - ImGuiInputFlags_RouteGlobalHigh = 1 << 11, // Register route globally (highest priority: unlikely you need to use that: will interfere with every active items) + ImGuiInputFlags_RouteFocused = 1 << 12, // (Default) Register focused route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. + ImGuiInputFlags_RouteGlobalLow = 1 << 13, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority. + ImGuiInputFlags_RouteGlobal = 1 << 14, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText). + ImGuiInputFlags_RouteGlobalHigh = 1 << 15, // Register route globally (highest priority: unlikely you need to use that: will interfere with every active items) ImGuiInputFlags_RouteMask_ = ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteGlobalLow | ImGuiInputFlags_RouteGlobalHigh, // _Always not part of this! - ImGuiInputFlags_RouteAlways = 1 << 12, // Do not register route, poll keys directly. - ImGuiInputFlags_RouteUnlessBgFocused= 1 << 13, // Global routes will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. + ImGuiInputFlags_RouteAlways = 1 << 16, // Do not register route, poll keys directly. + ImGuiInputFlags_RouteUnlessBgFocused= 1 << 17, // Global routes will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. ImGuiInputFlags_RouteExtraMask_ = ImGuiInputFlags_RouteAlways | ImGuiInputFlags_RouteUnlessBgFocused, // [Internal] Mask of which function support which flags - ImGuiInputFlags_SupportedByIsKeyPressed = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_, - ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteExtraMask_, + ImGuiInputFlags_RepeatRateMask_ = ImGuiInputFlags_RepeatRateDefault | ImGuiInputFlags_RepeatRateNavMove | ImGuiInputFlags_RepeatRateNavTweak, + ImGuiInputFlags_RepeatUntilMask_ = ImGuiInputFlags_RepeatUntilRelease | ImGuiInputFlags_RepeatUntilKeyModsChange | ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone | ImGuiInputFlags_RepeatUntilOtherKeyPress, + ImGuiInputFlags_RepeatMask_ = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RepeatUntilMask_, + ImGuiInputFlags_SupportedByIsKeyPressed = ImGuiInputFlags_RepeatMask_, + ImGuiInputFlags_SupportedByIsMouseClicked = ImGuiInputFlags_Repeat, + ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteExtraMask_, ImGuiInputFlags_SupportedBySetKeyOwner = ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease, ImGuiInputFlags_SupportedBySetItemKeyOwner = ImGuiInputFlags_SupportedBySetKeyOwner | ImGuiInputFlags_CondMask_, }; @@ -1453,6 +1498,7 @@ enum ImGuiActivateFlags_ ImGuiActivateFlags_PreferInput = 1 << 0, // Favor activation that requires keyboard text input (e.g. for Slider/Drag). Default for Enter key. ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default for Space key and if keyboard is not used. ImGuiActivateFlags_TryToPreserveState = 1 << 2, // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) + ImGuiActivateFlags_FromTabbing = 1 << 3, // Activation requested by a tabbing request }; // Early work-in-progress API for ScrollToItem() @@ -1493,10 +1539,11 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_Forwarded = 1 << 7, ImGuiNavMoveFlags_DebugNoResult = 1 << 8, // Dummy scoring for debug purpose, don't apply result ImGuiNavMoveFlags_FocusApi = 1 << 9, // Requests from focus API can land/focus/activate items even if they are marked with _NoTabStop (see NavProcessItemForTabbingRequest() for details) - ImGuiNavMoveFlags_Tabbing = 1 << 10, // == Focus + Activate if item is Inputable + DontChangeNavHighlight - ImGuiNavMoveFlags_Activate = 1 << 11, // Activate/select target item. - ImGuiNavMoveFlags_NoSelect = 1 << 12, // Don't trigger selection by not setting g.NavJustMovedTo - ImGuiNavMoveFlags_NoSetNavHighlight = 1 << 13, // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_IsTabbing = 1 << 10, // == Focus + Activate if item is Inputable + DontChangeNavHighlight + ImGuiNavMoveFlags_IsPageMove = 1 << 11, // Identify a PageDown/PageUp request. + ImGuiNavMoveFlags_Activate = 1 << 12, // Activate/select target item. + ImGuiNavMoveFlags_NoSelect = 1 << 13, // Don't trigger selection by not setting g.NavJustMovedTo + ImGuiNavMoveFlags_NoSetNavHighlight = 1 << 14, // Do not alter the visible state of keyboard vs mouse nav highlight }; enum ImGuiNavLayer @@ -1513,19 +1560,57 @@ struct ImGuiNavItemData ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space ImGuiItemFlags InFlags; // ????,Move // Best candidate item flags + ImGuiSelectionUserData SelectionUserData;//I+Mov // Best candidate SetNextItemSelectionData() value. float DistBox; // Move // Best candidate box distance to current NavId float DistCenter; // Move // Best candidate center distance to current NavId float DistAxial; // Move // Best candidate axial distance to current NavId ImGuiNavItemData() { Clear(); } - void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; DistBox = DistCenter = DistAxial = FLT_MAX; } + void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; SelectionUserData = -1; DistBox = DistCenter = DistAxial = FLT_MAX; } +}; + +//----------------------------------------------------------------------------- +// [SECTION] Typing-select support +//----------------------------------------------------------------------------- + +// Flags for GetTypingSelectRequest() +enum ImGuiTypingSelectFlags_ +{ + ImGuiTypingSelectFlags_None = 0, + ImGuiTypingSelectFlags_AllowBackspace = 1 << 0, // Backspace to delete character inputs. If using: ensure GetTypingSelectRequest() is not called more than once per frame (filter by e.g. focus state) + ImGuiTypingSelectFlags_AllowSingleCharMode = 1 << 1, // Allow "single char" search mode which is activated when pressing the same character multiple times. +}; + +// Returned by GetTypingSelectRequest(), designed to eventually be public. +struct IMGUI_API ImGuiTypingSelectRequest +{ + ImGuiTypingSelectFlags Flags; // Flags passed to GetTypingSelectRequest() + int SearchBufferLen; + const char* SearchBuffer; // Search buffer contents (use full string. unless SingleCharMode is set, in which case use SingleCharSize). + bool SelectRequest; // Set when buffer was modified this frame, requesting a selection. + bool SingleCharMode; // Notify when buffer contains same character repeated, to implement special mode. In this situation it preferred to not display any on-screen search indication. + ImS8 SingleCharSize; // Length in bytes of first letter codepoint (1 for ascii, 2-4 for UTF-8). If (SearchBufferLen==RepeatCharSize) only 1 letter has been input. +}; + +// Storage for GetTypingSelectRequest() +struct IMGUI_API ImGuiTypingSelectState +{ + ImGuiTypingSelectRequest Request; // User-facing data + char SearchBuffer[64]; // Search buffer: no need to make dynamic as this search is very transient. + ImGuiID FocusScope; + int LastRequestFrame = 0; + float LastRequestTime = 0.0f; + bool SingleCharModeLock = false; // After a certain single char repeat count we lock into SingleCharMode. Two benefits: 1) buffer never fill, 2) we can provide an immediate SingleChar mode without timer elapsing. + + ImGuiTypingSelectState() { memset(this, 0, sizeof(*this)); } + void Clear() { SearchBuffer[0] = 0; SingleCharModeLock = false; } // We preserve remaining data for easier debugging }; //----------------------------------------------------------------------------- // [SECTION] Columns support //----------------------------------------------------------------------------- -// Flags for internal's BeginColumns(). Prefix using BeginTable() nowadays! +// Flags for internal's BeginColumns(). This is an obsolete API. Prefer using BeginTable() nowadays! enum ImGuiOldColumnFlags_ { ImGuiOldColumnFlags_None = 0, @@ -1533,16 +1618,16 @@ enum ImGuiOldColumnFlags_ ImGuiOldColumnFlags_NoResize = 1 << 1, // Disable resizing columns when clicking on the dividers ImGuiOldColumnFlags_NoPreserveWidths = 1 << 2, // Disable column width preservation when adjusting columns ImGuiOldColumnFlags_NoForceWithinWindow = 1 << 3, // Disable forcing columns to fit within window - ImGuiOldColumnFlags_GrowParentContentsSize = 1 << 4, // (WIP) Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. + ImGuiOldColumnFlags_GrowParentContentsSize = 1 << 4, // Restore pre-1.51 behavior of extending the parent window contents size but _without affecting the columns width at all_. Will eventually remove. // Obsolete names (will be removed) #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - ImGuiColumnsFlags_None = ImGuiOldColumnFlags_None, - ImGuiColumnsFlags_NoBorder = ImGuiOldColumnFlags_NoBorder, - ImGuiColumnsFlags_NoResize = ImGuiOldColumnFlags_NoResize, - ImGuiColumnsFlags_NoPreserveWidths = ImGuiOldColumnFlags_NoPreserveWidths, - ImGuiColumnsFlags_NoForceWithinWindow = ImGuiOldColumnFlags_NoForceWithinWindow, - ImGuiColumnsFlags_GrowParentContentsSize = ImGuiOldColumnFlags_GrowParentContentsSize, + //ImGuiColumnsFlags_None = ImGuiOldColumnFlags_None, + //ImGuiColumnsFlags_NoBorder = ImGuiOldColumnFlags_NoBorder, + //ImGuiColumnsFlags_NoResize = ImGuiOldColumnFlags_NoResize, + //ImGuiColumnsFlags_NoPreserveWidths = ImGuiOldColumnFlags_NoPreserveWidths, + //ImGuiColumnsFlags_NoForceWithinWindow = ImGuiOldColumnFlags_NoForceWithinWindow, + //ImGuiColumnsFlags_GrowParentContentsSize = ImGuiOldColumnFlags_GrowParentContentsSize, #endif }; @@ -1581,6 +1666,9 @@ struct ImGuiOldColumns // [SECTION] Multi-select support //----------------------------------------------------------------------------- +// We always assume that -1 is an invalid value (which works for indices and pointers) +#define ImGuiSelectionUserData_Invalid ((ImGuiSelectionUserData)-1) + #ifdef IMGUI_HAS_MULTI_SELECT // #endif // #ifdef IMGUI_HAS_MULTI_SELECT @@ -1601,18 +1689,17 @@ struct ImGuiOldColumns // Every instance of ImGuiViewport is in fact a ImGuiViewportP. struct ImGuiViewportP : public ImGuiViewport { - int DrawListsLastFrame[2]; // Last frame number the background (0) and foreground (1) draw lists were used - ImDrawList* DrawLists[2]; // Convenience background (0) and foreground (1) draw lists. We use them to draw software mouser cursor when io.MouseDrawCursor is set and to draw most debug overlays. + int BgFgDrawListsLastFrame[2]; // Last frame number the background (0) and foreground (1) draw lists were used + ImDrawList* BgFgDrawLists[2]; // Convenience background (0) and foreground (1) draw lists. We use them to draw software mouser cursor when io.MouseDrawCursor is set and to draw most debug overlays. ImDrawData DrawDataP; - ImDrawDataBuilder DrawDataBuilder; - + ImDrawDataBuilder DrawDataBuilder; // Temporary data while building final ImDrawData ImVec2 WorkOffsetMin; // Work Area: Offset from Pos to top-left corner of Work Area. Generally (0,0) or (0,+main_menu_bar_height). Work Area is Full Area but without menu-bars/status-bars (so WorkArea always fit inside Pos/Size!) ImVec2 WorkOffsetMax; // Work Area: Offset from Pos+Size to bottom-right corner of Work Area. Generally (0,0) or (0,-status_bar_height). ImVec2 BuildWorkOffsetMin; // Work Area: Offset being built during current frame. Generally >= 0.0f. ImVec2 BuildWorkOffsetMax; // Work Area: Offset being built during current frame. Generally <= 0.0f. - ImGuiViewportP() { DrawListsLastFrame[0] = DrawListsLastFrame[1] = -1; DrawLists[0] = DrawLists[1] = NULL; } - ~ImGuiViewportP() { if (DrawLists[0]) IM_DELETE(DrawLists[0]); if (DrawLists[1]) IM_DELETE(DrawLists[1]); } + ImGuiViewportP() { BgFgDrawListsLastFrame[0] = BgFgDrawListsLastFrame[1] = -1; BgFgDrawLists[0] = BgFgDrawLists[1] = NULL; } + ~ImGuiViewportP() { if (BgFgDrawLists[0]) IM_DELETE(BgFgDrawLists[0]); if (BgFgDrawLists[1]) IM_DELETE(BgFgDrawLists[1]); } // Calculate work rect pos/size given a set of offset (we have 1 pair of offset for rect locked from last frame data, and 1 pair for currently building rect) ImVec2 CalcWorkRectPos(const ImVec2& off_min) const { return ImVec2(Pos.x + off_min.x, Pos.y + off_min.y); } @@ -1638,6 +1725,7 @@ struct ImGuiWindowSettings ImVec2ih Pos; ImVec2ih Size; bool Collapsed; + bool IsChild; bool WantApply; // Set when loaded from .ini data (to enable merging/loading .ini data into an already running context) bool WantDelete; // Set to invalidate/delete the settings entry @@ -1692,27 +1780,47 @@ struct ImGuiLocEntry enum ImGuiDebugLogFlags_ { // Event types - ImGuiDebugLogFlags_None = 0, - ImGuiDebugLogFlags_EventActiveId = 1 << 0, - ImGuiDebugLogFlags_EventFocus = 1 << 1, - ImGuiDebugLogFlags_EventPopup = 1 << 2, - ImGuiDebugLogFlags_EventNav = 1 << 3, - ImGuiDebugLogFlags_EventClipper = 1 << 4, - ImGuiDebugLogFlags_EventSelection = 1 << 5, - ImGuiDebugLogFlags_EventIO = 1 << 6, - ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO, - ImGuiDebugLogFlags_OutputToTTY = 1 << 10, // Also send output to TTY + ImGuiDebugLogFlags_None = 0, + ImGuiDebugLogFlags_EventActiveId = 1 << 0, + ImGuiDebugLogFlags_EventFocus = 1 << 1, + ImGuiDebugLogFlags_EventPopup = 1 << 2, + ImGuiDebugLogFlags_EventNav = 1 << 3, + ImGuiDebugLogFlags_EventClipper = 1 << 4, + ImGuiDebugLogFlags_EventSelection = 1 << 5, + ImGuiDebugLogFlags_EventIO = 1 << 6, + + ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO, + ImGuiDebugLogFlags_OutputToTTY = 1 << 20, // Also send output to TTY + ImGuiDebugLogFlags_OutputToTestEngine = 1 << 21, // Also send output to Test Engine +}; + +struct ImGuiDebugAllocEntry +{ + int FrameCount; + ImS16 AllocCount; + ImS16 FreeCount; +}; + +struct ImGuiDebugAllocInfo +{ + int TotalAllocCount; // Number of call to MemAlloc(). + int TotalFreeCount; + ImS16 LastEntriesIdx; // Current index in buffer + ImGuiDebugAllocEntry LastEntriesBuf[6]; // Track last 6 frames that had allocations + + ImGuiDebugAllocInfo() { memset(this, 0, sizeof(*this)); } }; struct ImGuiMetricsConfig { bool ShowDebugLog = false; - bool ShowStackTool = false; + bool ShowIDStackTool = false; bool ShowWindowsRects = false; bool ShowWindowsBeginOrder = false; bool ShowTablesRects = false; bool ShowDrawCmdMesh = true; bool ShowDrawCmdBoundingBoxes = true; + bool ShowTextEncodingViewer = false; bool ShowAtlasTintedWithTextColor = false; int ShowWindowsRectsType = -1; int ShowTablesRectsType = -1; @@ -1729,8 +1837,8 @@ struct ImGuiStackLevelInfo ImGuiStackLevelInfo() { memset(this, 0, sizeof(*this)); } }; -// State for Stack tool queries -struct ImGuiStackTool +// State for ID Stack tool queries +struct ImGuiIDStackTool { int LastActiveFrame; int StackLevel; // -1: query stack and resize Results, >= 0: individual stack level @@ -1739,7 +1847,7 @@ struct ImGuiStackTool bool CopyToClipboardOnCtrlC; float CopyToClipboardLastTime; - ImGuiStackTool() { memset(this, 0, sizeof(*this)); CopyToClipboardLastTime = -FLT_MAX; } + ImGuiIDStackTool() { memset(this, 0, sizeof(*this)); CopyToClipboardLastTime = -FLT_MAX; } }; //----------------------------------------------------------------------------- @@ -1799,6 +1907,7 @@ struct ImGuiContext ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow* int WindowsActiveCount; // Number of unique windows submitted by frame ImVec2 WindowsHoverPadding; // Padding around resizable windows for which hovering on counts as hovering the window == ImMax(style.TouchExtraPadding, WINDOWS_HOVER_PADDING) + ImGuiID DebugBreakInWindow; // Set to break in Begin() call. ImGuiWindow* CurrentWindow; // Window being drawn into ImGuiWindow* HoveredWindow; // Window the mouse is hovering. Will typically catch mouse inputs. ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. @@ -1806,12 +1915,13 @@ struct ImGuiContext ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. ImVec2 WheelingWindowRefMousePos; int WheelingWindowStartFrame; // This may be set one frame before WheelingWindow is != NULL + int WheelingWindowScrolledFrame; float WheelingWindowReleaseTimer; ImVec2 WheelingWindowWheelRemainder; ImVec2 WheelingAxisAvg; // Item/widgets state and tracking information - ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] + ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by ID Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; bool HoveredIdAllowOverlap; @@ -1842,10 +1952,14 @@ struct ImGuiContext // - The idea is that instead of "eating" a given key, we can link to an owner. // - Input query can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_None (== -1) or a custom ID. // - Routing is requested ahead of time for a given chord (Key + Mods) and granted in NewFrame(). + double LastKeyModsChangeTime; // Record the last time key mods changed (affect repeat delay when using shortcut logic) + double LastKeyModsChangeFromNoneTime; // Record the last time key mods changed away from being 0 (affect repeat delay when using shortcut logic) + double LastKeyboardKeyPressTime; // Record the last time a keyboard key (ignore mouse/gamepad ones) was pressed. ImGuiKeyOwnerData KeysOwnerData[ImGuiKey_NamedKey_COUNT]; ImGuiKeyRoutingTable KeysRoutingTable; ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) bool ActiveIdUsingAllKeyboardKeys; // Active widget will want to read all keyboard keys inputs. (FIXME: This is a shortcut for not taking ownership of 100+ keys but perhaps best to not have the inconsistency) + ImGuiKeyChord DebugBreakInShortcutRouting; // Set to break in SetShortcutRouting()/Shortcut() calls. #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO ImU32 ActiveIdUsingNavInputMask; // If you used this. Since (IMGUI_VERSION_NUM >= 18804) : 'g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel);' becomes 'SetKeyOwner(ImGuiKey_Escape, g.ActiveId) and/or SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId);' #endif @@ -1857,16 +1971,20 @@ struct ImGuiContext ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions ImGuiLastItemData LastItemData; // Storage for last submitted item (setup by ItemAdd) ImGuiNextWindowData NextWindowData; // Storage for SetNextWindow** functions + bool DebugShowGroupRects; // Shared stacks - ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() - ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() - ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() - ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin() - ImVectorItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() - ImVectorGroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() - ImVectorOpenPopupStack; // Which popups are open (persistent) - ImVectorBeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + ImGuiCol DebugFlashStyleColorIdx; // (Keep close to ColorStack to share cache line) + ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() + ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() + ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() + ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin() + ImVector ItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() + ImVector GroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() + ImVector OpenPopupStack; // Which popups are open (persistent) + ImVector BeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + ImVector NavTreeNodeStack; // Stack for TreeNode() when a NavLeft requested is emitted. + int BeginMenuCount; // Viewports @@ -1887,6 +2005,7 @@ struct ImGuiContext ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS CAN ONLY BE ImGuiInputSource_Keyboard or ImGuiInputSource_Mouse ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. + ImGuiSelectionUserData NavLastValidSelectionUserData; // Last valid data passed to SetNextItemSelectionUser(), or -1. For current window. Not reset when focusing an item that doesn't have selection data. bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) @@ -1940,6 +2059,7 @@ struct ImGuiContext int DragDropMouseButton; ImGuiPayload DragDropPayload; ImRect DragDropTargetRect; // Store rectangle of current target candidate (we favor small targets when overlapping) + ImRect DragDropTargetClipRect; // Store ClipRect at the time of item's drawing ImGuiID DragDropTargetId; ImGuiDragDropFlags DragDropAcceptFlags; float DragDropAcceptIdCurrRectSurface; // Target item surface (we resolve overlapping targets by prioritizing the smaller surface) @@ -1956,6 +2076,7 @@ struct ImGuiContext // Tables ImGuiTable* CurrentTable; + ImGuiID DebugBreakInTable; // Set to break in BeginTable() call. int TablesTempDataStacked; // Temporary table data size (because we leave previous instances undestructed, we generally don't use TablesTempData.Size) ImVector TablesTempData; // Temporary table data (buffers reused/shared across instances, support nesting) ImPool Tables; // Persistent table data @@ -1994,6 +2115,8 @@ struct ImGuiContext ImU32 ColorEditSavedColor; // RGB value with alpha set to 0. ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker. ImGuiComboPreviewData ComboPreviewData; + ImRect WindowResizeBorderExpectedRect; // Expected border rect, switch to relative edit if moving + bool WindowResizeRelativeMode; float SliderGrabClickOffset; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? @@ -2003,14 +2126,15 @@ struct ImGuiContext float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() short DisabledStackSize; + short LockMarkEdited; short TooltipOverrideCount; ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once + ImGuiTypingSelectState TypingSelectState; // State for GetTypingSelectRequest() // Platform support ImGuiPlatformImeData PlatformImeData; // Data updated by current frame ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data (when changing we will call io.SetPlatformImeDataFn - char PlatformLocaleDecimalPoint; // '.' or *localeconv()->decimal_point // Settings bool SettingsLoaded; @@ -2042,14 +2166,20 @@ struct ImGuiContext ImGuiDebugLogFlags DebugLogFlags; ImGuiTextBuffer DebugLogBuf; ImGuiTextIndex DebugLogIndex; - ImU8 DebugLogClipperAutoDisableFrames; + ImGuiDebugLogFlags DebugLogAutoDisableFlags; + ImU8 DebugLogAutoDisableFrames; ImU8 DebugLocateFrames; // For DebugLocateItemOnHover(). This is used together with DebugLocateId which is in a hot/cached spot above. + bool DebugBreakInLocateId; // Debug break in ItemAdd() call for g.DebugLocateId. + ImGuiKeyChord DebugBreakKeyChord; // = ImGuiKey_Pause ImS8 DebugBeginReturnValueCullDepth; // Cycle between 0..9 then wrap around. bool DebugItemPickerActive; // Item picker is active (started with DebugStartItemPicker()) ImU8 DebugItemPickerMouseButton; ImGuiID DebugItemPickerBreakId; // Will call IM_DEBUG_BREAK() when encountering this ID + float DebugFlashStyleColorTime; + ImVec4 DebugFlashStyleColorBackup; ImGuiMetricsConfig DebugMetricsConfig; - ImGuiStackTool DebugStackTool; + ImGuiIDStackTool DebugIDStackTool; + ImGuiDebugAllocInfo DebugAllocInfo; // Misc float FramerateSecPerFrame[60]; // Calculate estimate of framerate for user over the last 60 frames.. @@ -2088,7 +2218,7 @@ struct ImGuiContext HoveredWindowUnderMovingWindow = NULL; MovingWindow = NULL; WheelingWindow = NULL; - WheelingWindowStartFrame = -1; + WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1; WheelingWindowReleaseTimer = 0.0f; DebugHookIdInfo = 0; @@ -2116,6 +2246,8 @@ struct ImGuiContext LastActiveId = 0; LastActiveIdTimer = 0.0f; + LastKeyboardKeyPressTime = LastKeyModsChangeTime = LastKeyModsChangeFromNoneTime = -1.0; + ActiveIdUsingNavDirMask = 0x00; ActiveIdUsingAllKeyboardKeys = false; #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -2124,6 +2256,7 @@ struct ImGuiContext CurrentFocusScopeId = 0; CurrentItemFlags = ImGuiItemFlags_None; + DebugShowGroupRects = false; BeginMenuCount = 0; NavWindow = NULL; @@ -2133,6 +2266,7 @@ struct ImGuiContext NavJustMovedToKeyMods = ImGuiMod_None; NavInputSource = ImGuiInputSource_Keyboard; NavLayer = ImGuiNavLayer_Main; + NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; NavIdIsAlive = false; NavMousePosDirty = false; NavDisableHighlight = true; @@ -2188,6 +2322,7 @@ struct ImGuiContext ColorEditCurrentID = ColorEditSavedID = 0; ColorEditSavedHue = ColorEditSavedSat = 0.0f; ColorEditSavedColor = 0; + WindowResizeRelativeMode = false; SliderGrabClickOffset = 0.0f; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; @@ -2197,11 +2332,11 @@ struct ImGuiContext ScrollbarClickDeltaToGrabCenter = 0.0f; DisabledAlphaBackup = 0.0f; DisabledStackSize = 0; + LockMarkEdited = 0; TooltipOverrideCount = 0; PlatformImeData.InputPos = ImVec2(0.0f, 0.0f); PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission - PlatformLocaleDecimalPoint = '.'; SettingsLoaded = false; SettingsDirtyTimer = 0.0f; @@ -2220,12 +2355,22 @@ struct ImGuiContext DebugLogFlags = ImGuiDebugLogFlags_OutputToTTY; DebugLocateId = 0; - DebugLogClipperAutoDisableFrames = 0; + DebugLogAutoDisableFlags = ImGuiDebugLogFlags_None; + DebugLogAutoDisableFrames = 0; DebugLocateFrames = 0; DebugBeginReturnValueCullDepth = -1; DebugItemPickerActive = false; DebugItemPickerMouseButton = ImGuiMouseButton_Left; DebugItemPickerBreakId = 0; + DebugFlashStyleColorTime = 0.0f; + DebugFlashStyleColorIdx = ImGuiCol_COUNT; + + // Same as DebugBreakClearData(). Those fields are scattered in their respective subsystem to stay in hot-data locations + DebugBreakInWindow = 0; + DebugBreakInTable = 0; + DebugBreakInLocateId = false; + DebugBreakKeyChord = ImGuiKey_Pause; + DebugBreakInShortcutRouting = ImGuiKey_None; memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; @@ -2296,6 +2441,7 @@ struct IMGUI_API ImGuiWindow char* Name; // Window name, owned by the window. ImGuiID ID; // == ImHashStr(Name) ImGuiWindowFlags Flags; // See enum ImGuiWindowFlags_ + ImGuiChildFlags ChildFlags; // Set when window is a child window. See enum ImGuiChildFlags_ ImGuiViewportP* Viewport; // Always set in Begin(). Inactive windows may have a NULL value here if their viewport was discarded. ImVec2 Pos; // Position (always rounded-up to nearest pixel) ImVec2 Size; // Current size (==SizeFull or collapsed title bar size) @@ -2330,6 +2476,7 @@ struct IMGUI_API ImGuiWindow bool IsFallbackWindow; // Set on the "Debug##Default" window. bool IsExplicitChild; // Set when passed _ChildWindow, left to false by BeginDocked() bool HasCloseButton; // Set when the window has a close button (p_open != NULL) + signed char ResizeBorderHovered; // Current border being hovered for resize (-1: none, otherwise 0-3) signed char ResizeBorderHeld; // Current border being held for resize (-1: none, otherwise 0-3) short BeginCount; // Number of Begin() during the current frame (generally 0 or 1, 1+ if appending via multiple Begin/End pairs) short BeginCountPreviousFrame; // Number of Begin() during the previous frame @@ -2338,7 +2485,6 @@ struct IMGUI_API ImGuiWindow short FocusOrder; // Order within WindowsFocusOrder[], altered when windows are focused. ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) ImS8 AutoFitFramesX, AutoFitFramesY; - ImS8 AutoFitChildAxises; bool AutoFitOnlyGrows; ImGuiDir AutoPosLastDirection; ImS8 HiddenFramesCanSkipItems; // Hide the window for N frames @@ -2472,6 +2618,8 @@ struct IMGUI_API ImGuiTabBar float ScrollingSpeed; float ScrollingRectMinX; float ScrollingRectMaxX; + float SeparatorMinX; + float SeparatorMaxX; ImGuiID ReorderRequestTabId; ImS16 ReorderRequestOffset; ImS8 BeginCount; @@ -2569,14 +2717,17 @@ struct ImGuiTableCellData }; // Per-instance data that needs preserving across frames (seemingly most others do not need to be preserved aside from debug needs. Does that means they could be moved to ImGuiTableTempData?) +// sizeof() ~ 24 bytes struct ImGuiTableInstanceData { ImGuiID TableInstanceID; float LastOuterHeight; // Outer height from last frame - float LastFirstRowHeight; // Height of first row from last frame (FIXME: this is used as "header height" and may be reworked) + float LastTopHeadersRowHeight; // Height of first consecutive header rows from last frame (FIXME: this is used assuming consecutive headers are in same frozen set) float LastFrozenHeight; // Height of frozen section from last frame + int HoveredRowLast; // Index of row which was hovered last frame. + int HoveredRowNext; // Index of row hovered this frame, set after encountering it. - ImGuiTableInstanceData() { TableInstanceID = 0; LastOuterHeight = LastFirstRowHeight = LastFrozenHeight = 0.0f; } + ImGuiTableInstanceData() { TableInstanceID = 0; LastOuterHeight = LastTopHeadersRowHeight = LastFrozenHeight = 0.0f; HoveredRowLast = HoveredRowNext = -1; } }; // FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs, incoming RowData @@ -2604,6 +2755,7 @@ struct IMGUI_API ImGuiTable float RowPosY1; float RowPosY2; float RowMinHeight; // Height submitted to TableNextRow() + float RowCellPaddingY; // Top and bottom padding. Reloaded during row change. float RowTextBaseline; float RowIndentOffsetX; ImGuiTableRowFlags RowFlags : 16; // Current row flags, see ImGuiTableRowFlags_ @@ -2617,9 +2769,8 @@ struct IMGUI_API ImGuiTable float HostIndentX; float MinColumnWidth; float OuterPaddingX; - float CellPaddingX; // Padding from each borders - float CellPaddingY; - float CellSpacingX1; // Spacing between non-bordered cells + float CellPaddingX; // Padding from each borders. Locked in BeginTable()/Layout. + float CellSpacingX1; // Spacing between non-bordered cells. Locked in BeginTable()/Layout. float CellSpacingX2; float InnerWidth; // User value passed to BeginTable(), see comments at the top of BeginTable() for details. float ColumnsGivenWidth; // Sum of current column width @@ -2628,6 +2779,8 @@ struct IMGUI_API ImGuiTable float ResizedColumnNextWidth; float ResizeLockMinContentsX2; // Lock minimum contents width while resizing down in order to not create feedback loops. But we allow growing the table. float RefScale; // Reference scale to be able to rescale columns on font/dpi changes. + float AngledHeadersHeight; // Set by TableAngledHeadersRow(), used in TableUpdateLayout() + float AngledHeadersSlope; // Set by TableAngledHeadersRow(), used in TableUpdateLayout() ImRect OuterRect; // Note: for non-scrolling table, OuterRect.Max.y is often FLT_MAX until EndTable(), unless a height has been specified in BeginTable(). ImRect InnerRect; // InnerRect but without decoration. As with OuterRect, for non-scrolling tables, InnerRect.Max.y is ImRect WorkRect; @@ -2650,8 +2803,10 @@ struct IMGUI_API ImGuiTable ImGuiTableColumnIdx ColumnsEnabledCount; // Number of enabled columns (<= ColumnsCount) ImGuiTableColumnIdx ColumnsEnabledFixedCount; // Number of enabled columns (<= ColumnsCount) ImGuiTableColumnIdx DeclColumnsCount; // Count calls to TableSetupColumn() + ImGuiTableColumnIdx AngledHeadersCount; // Count columns with angled headers ImGuiTableColumnIdx HoveredColumnBody; // Index of column whose visible region is being hovered. Important: == ColumnsCount when hovering empty region after the right-most column! ImGuiTableColumnIdx HoveredColumnBorder; // Index of column whose right-border is being hovered (for resizing). + ImGuiTableColumnIdx HighlightColumnHeader; // Index of column which should be highlighted. ImGuiTableColumnIdx AutoFitSingleColumn; // Index of single column requesting auto-fit. ImGuiTableColumnIdx ResizedColumn; // Index of column being resized. Reset when InstanceCurrent==0. ImGuiTableColumnIdx LastResizedColumn; // Index of column being resized from previous frame. @@ -2677,6 +2832,7 @@ struct IMGUI_API ImGuiTable bool IsSortSpecsDirty; bool IsUsingHeaders; // Set when the first row had the ImGuiTableRowFlags_Headers flag. bool IsContextPopupOpen; // Set when default context menu is open (also see: ContextPopupColumn, InstanceInteracted). + bool DisableDefaultContextMenu; // Disable default context menu contents. You may submit your own using TableBeginContextMenuPopup()/EndPopup() bool IsSettingsRequestLoad; bool IsSettingsDirty; // Set when table settings have changed and needs to be reported into ImGuiTableSetttings data. bool IsDefaultDisplayOrder; // Set when display order is unchanged from default (DisplayOrder contains 0...Count-1) @@ -2684,6 +2840,8 @@ struct IMGUI_API ImGuiTable bool IsResetDisplayOrderRequest; bool IsUnfrozenRows; // Set when we got past the frozen row. bool IsDefaultSizingPolicy; // Set if user didn't explicitly set a sizing policy in BeginTable() + bool IsActiveIdAliveBeforeTable; + bool IsActiveIdInTable; bool HasScrollbarYCurr; // Whether ANY instance of this table had a vertical scrollbar during the current frame. bool HasScrollbarYPrev; // Whether ANY instance of this table had a vertical scrollbar during the previous. bool MemoryCompacted; @@ -2696,11 +2854,12 @@ struct IMGUI_API ImGuiTable // Transient data that are only needed between BeginTable() and EndTable(), those buffers are shared (1 per level of stacked table). // - Accessing those requires chasing an extra pointer so for very frequently used data we leave them in the main table structure. // - We also leave out of this structure data that tend to be particularly useful for debugging/metrics. -// sizeof() ~ 112 bytes. +// sizeof() ~ 120 bytes. struct IMGUI_API ImGuiTableTempData { int TableIndex; // Index in g.Tables.Buf[] pool float LastTimeActive; // Last timestamp this structure was used + float AngledheadersExtraWidth; // Used in EndTable() ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() ImDrawListSplitter DrawSplitter; @@ -2781,7 +2940,7 @@ namespace ImGui IMGUI_API void SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond cond = 0); IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); IMGUI_API void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); - IMGUI_API void SetWindowHiddendAndSkipItemsForCurrentFrame(ImGuiWindow* window); + IMGUI_API void SetWindowHiddenAndSkipItemsForCurrentFrame(ImGuiWindow* window); inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } @@ -2802,6 +2961,7 @@ namespace ImGui inline ImDrawList* GetForegroundDrawList(ImGuiWindow* window) { IM_UNUSED(window); return GetForegroundDrawList(); } // This seemingly unnecessary wrapper simplifies compatibility between the 'master' and 'docking' branches. IMGUI_API ImDrawList* GetBackgroundDrawList(ImGuiViewport* viewport); // get background draw list for the given viewport. this draw list will be the first rendering one. Useful to quickly draw shapes/text behind dear imgui contents. IMGUI_API ImDrawList* GetForegroundDrawList(ImGuiViewport* viewport); // get foreground draw list for the given viewport. this draw list will be the last rendered one. Useful to quickly draw shapes/text over dear imgui contents. + IMGUI_API void AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector* out_list, ImDrawList* draw_list); // Init IMGUI_API void Initialize(); @@ -2897,7 +3057,7 @@ namespace ImGui IMGUI_API void LogSetNextTextDecoration(const char* prefix, const char* suffix); // Popups, Modals, Tooltips - IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags); + IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags); IMGUI_API void OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags = ImGuiPopupFlags_None); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); @@ -2905,6 +3065,7 @@ namespace ImGui IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); IMGUI_API bool BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); + IMGUI_API bool BeginTooltipHidden(); IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImGuiWindow* GetTopMostAndVisiblePopupModal(); @@ -2929,10 +3090,12 @@ namespace ImGui IMGUI_API void NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); IMGUI_API void NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result); + IMGUI_API void NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiNavTreeNodeData* tree_node_data); IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); IMGUI_API void NavClearPreferredPosForAxis(ImGuiAxis axis); + IMGUI_API void NavRestoreHighlightAfterMove(); IMGUI_API void NavUpdateCurrentWindowIsScrollPushableX(); IMGUI_API void SetNavWindow(ImGuiWindow* window); IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); @@ -2966,13 +3129,14 @@ namespace ImGui IMGUI_API ImGuiKeyData* GetKeyData(ImGuiContext* ctx, ImGuiKey key); inline ImGuiKeyData* GetKeyData(ImGuiKey key) { ImGuiContext& g = *GImGui; return GetKeyData(&g, key); } - IMGUI_API void GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size); + IMGUI_API const char* GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size); inline ImGuiKey MouseButtonToKey(ImGuiMouseButton button) { IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); return (ImGuiKey)(ImGuiKey_MouseLeft + button); } IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f); IMGUI_API ImVec2 GetKeyMagnitude2d(ImGuiKey key_left, ImGuiKey key_right, ImGuiKey key_up, ImGuiKey key_down); IMGUI_API float GetNavTweakPressedAmount(ImGuiAxis axis); IMGUI_API int CalcTypematicRepeatAmount(float t0, float t1, float repeat_delay, float repeat_rate); IMGUI_API void GetTypematicRepeatRate(ImGuiInputFlags flags, float* repeat_delay, float* repeat_rate); + IMGUI_API void TeleportMousePos(const ImVec2& pos); IMGUI_API void SetActiveIdUsingAllKeyboardKeys(); inline bool IsActiveIdUsingNavDir(ImGuiDir dir) { ImGuiContext& g = *GImGui; return (g.ActiveIdUsingNavDirMask & (1 << dir)) != 0; } @@ -3006,6 +3170,7 @@ namespace ImGui IMGUI_API bool IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id); IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags = 0); IMGUI_API bool IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id); + IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button, ImGuiID owner_id); // [EXPERIMENTAL] Shortcut Routing // - ImGuiKeyChord = a ImGuiKey optionally OR-red with ImGuiMod_Alt/ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Super. @@ -3017,6 +3182,10 @@ namespace ImGui // - Route is granted to a single owner. When multiple requests are made we have policies to select the winning route. // - Multiple read sites may use the same owner id and will all get the granted route. // - For routing: when owner_id is 0 we use the current Focus Scope ID as a default owner in order to identify our location. + // - TL;DR; + // - IsKeyChordPressed() compares mods + call IsKeyPressed() -> function has no side-effect. + // - Shortcut() submits a route then if currently can be routed calls IsKeyChordPressed() -> function has (desirable) side-effects. + IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id); @@ -3039,7 +3208,13 @@ namespace ImGui IMGUI_API bool BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id); IMGUI_API void ClearDragDrop(); IMGUI_API bool IsDragDropPayloadBeingAccepted(); - IMGUI_API void RenderDragDropTargetRect(const ImRect& bb); + IMGUI_API void RenderDragDropTargetRect(const ImRect& bb, const ImRect& item_clip_rect); + + // Typing-Select API + IMGUI_API ImGuiTypingSelectRequest* GetTypingSelectRequest(ImGuiTypingSelectFlags flags = ImGuiTypingSelectFlags_None); + IMGUI_API int TypingSelectFindMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx); + IMGUI_API int TypingSelectFindNextSingleCharMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx); + IMGUI_API int TypingSelectFindBestLeadingMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data); // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) IMGUI_API void SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect); @@ -3057,10 +3232,13 @@ namespace ImGui IMGUI_API void TableOpenContextMenu(int column_n = -1); IMGUI_API void TableSetColumnWidth(int column_n, float width); IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs); - IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. + IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. + IMGUI_API int TableGetHoveredRow(); // Retrieve *PREVIOUS FRAME* hovered row. This difference with TableGetHoveredColumn() is the reason why this is not public yet. IMGUI_API float TableGetHeaderRowHeight(); + IMGUI_API float TableGetHeaderAngledMaxLabelWidth(); IMGUI_API void TablePushBackgroundChannel(); IMGUI_API void TablePopBackgroundChannel(); + IMGUI_API void TableAngledHeadersRowEx(float angle, float label_width = 0.0f); // Tables: Internals inline ImGuiTable* GetCurrentTable() { ImGuiContext& g = *GImGui; return g.CurrentTable; } @@ -3073,7 +3251,7 @@ namespace ImGui IMGUI_API void TableUpdateBorders(ImGuiTable* table); IMGUI_API void TableUpdateColumnsWeightFromWidth(ImGuiTable* table); IMGUI_API void TableDrawBorders(ImGuiTable* table); - IMGUI_API void TableDrawContextMenu(ImGuiTable* table); + IMGUI_API void TableDrawDefaultContextMenu(ImGuiTable* table, ImGuiTableFlags flags_for_section_to_display); IMGUI_API bool TableBeginContextMenuPopup(ImGuiTable* table); IMGUI_API void TableMergeDrawChannels(ImGuiTable* table); inline ImGuiTableInstanceData* TableGetInstanceData(ImGuiTable* table, int instance_no) { if (instance_no == 0) return &table->InstanceDataFirst; return &table->InstanceDataExtra[instance_no - 1]; } @@ -3154,7 +3332,7 @@ namespace ImGui IMGUI_API void TextEx(const char* text, const char* text_end = NULL, ImGuiTextFlags flags = 0); IMGUI_API bool ButtonEx(const char* label, const ImVec2& size_arg = ImVec2(0, 0), ImGuiButtonFlags flags = 0); IMGUI_API bool ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size_arg, ImGuiButtonFlags flags = 0); - IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0); + IMGUI_API bool ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags = 0); IMGUI_API void SeparatorEx(ImGuiSeparatorFlags flags, float thickness = 1.0f); IMGUI_API void SeparatorTextEx(ImGuiID id, const char* label, const char* label_end, float extra_width); IMGUI_API bool CheckboxFlags(const char* label, ImS64* flags, ImS64 flags_value); @@ -3179,6 +3357,7 @@ namespace ImGui IMGUI_API void TreePushOverrideID(ImGuiID id); IMGUI_API void TreeNodeSetOpen(ImGuiID id, bool open); IMGUI_API bool TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags); // Return open state. Consume previous SetNextItemOpen() data, if any. May return true when logging. + IMGUI_API void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data); // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. // To use them externally (for custom widget) you may need an "extern template" statement in your code in order to link to existing instances and silence Clang warnings (see #2036). @@ -3217,6 +3396,7 @@ namespace ImGui // Shade functions (write over already created vertices) IMGUI_API void ShadeVertsLinearColorGradientKeepAlpha(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, ImVec2 gradient_p0, ImVec2 gradient_p1, ImU32 col0, ImU32 col1); IMGUI_API void ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& a, const ImVec2& b, const ImVec2& uv_a, const ImVec2& uv_b, bool clamp); + IMGUI_API void ShadeVertsTransformPos(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& pivot_in, float cos_a, float sin_a, const ImVec2& pivot_out); // Garbage collection IMGUI_API void GcCompactTransientMiscBuffers(); @@ -3226,20 +3406,26 @@ namespace ImGui // Debug Log IMGUI_API void DebugLog(const char* fmt, ...) IM_FMTARGS(1); IMGUI_API void DebugLogV(const char* fmt, va_list args) IM_FMTLIST(1); + IMGUI_API void DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr, size_t size); // size >= 0 : alloc, size = -1 : free // Debug Tools IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); IMGUI_API void ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); IMGUI_API void ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + IMGUI_API void DebugDrawCursorPos(ImU32 col = IM_COL32(255, 0, 0, 255)); + IMGUI_API void DebugDrawLineExtents(ImU32 col = IM_COL32(255, 0, 0, 255)); + IMGUI_API void DebugDrawItemRect(ImU32 col = IM_COL32(255, 0, 0, 255)); IMGUI_API void DebugLocateItem(ImGuiID target_id); // Call sparingly: only 1 at the same time! IMGUI_API void DebugLocateItemOnHover(ImGuiID target_id); // Only call on reaction to a mouse Hover: because only 1 at the same time! IMGUI_API void DebugLocateItemResolveWithLastItem(); - inline void DebugDrawItemRect(ImU32 col = IM_COL32(255,0,0,255)) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; GetForegroundDrawList(window)->AddRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, col); } + IMGUI_API void DebugBreakClearData(); + IMGUI_API bool DebugBreakButton(const char* label, const char* description_of_location); + IMGUI_API void DebugBreakButtonTooltip(bool keyboard_only, const char* description_of_location); inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); IMGUI_API void DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end); IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); - IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label); + IMGUI_API void DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, const ImDrawList* draw_list, const char* label); IMGUI_API void DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, const ImDrawList* draw_list, const ImDrawCmd* draw_cmd, bool show_mesh, bool show_aabb); IMGUI_API void DebugNodeFont(ImFont* font); IMGUI_API void DebugNodeFontGlyph(ImFont* font, const ImFontGlyph* glyph); @@ -3248,6 +3434,7 @@ namespace ImGui IMGUI_API void DebugNodeTable(ImGuiTable* table); IMGUI_API void DebugNodeTableSettings(ImGuiTableSettings* settings); IMGUI_API void DebugNodeInputTextState(ImGuiInputTextState* state); + IMGUI_API void DebugNodeTypingSelectState(ImGuiTypingSelectState* state); IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); @@ -3290,6 +3477,7 @@ struct ImFontBuilderIO #ifdef IMGUI_ENABLE_STB_TRUETYPE IMGUI_API const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype(); #endif +IMGUI_API void ImFontAtlasUpdateConfigDataPointers(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildInit(ImFontAtlas* atlas); IMGUI_API void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent); IMGUI_API void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque); diff --git a/include/imgui/imstb_textedit.h b/include/imgui/imstb_textedit.h index a8a82311..c30f1a12 100644 --- a/include/imgui/imstb_textedit.h +++ b/include/imgui/imstb_textedit.h @@ -2,8 +2,9 @@ // This is a slightly modified version of stb_textedit.h 1.14. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) -// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000) +// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000 + #6783) // Grep for [DEAR IMGUI] to find the changes. +// - Also renamed macros used or defined outside of IMSTB_TEXTEDIT_IMPLEMENTATION block from STB_TEXTEDIT_* to IMSTB_TEXTEDIT_* // stb_textedit.h - v1.14 - public domain - Sean Barrett // Development of this library was sponsored by RAD Game Tools @@ -30,7 +31,7 @@ // DEPENDENCIES // // Uses the C runtime function 'memmove', which you can override -// by defining STB_TEXTEDIT_memmove before the implementation. +// by defining IMSTB_TEXTEDIT_memmove before the implementation. // Uses no other functions. Performs no runtime allocations. // // @@ -274,8 +275,8 @@ //// //// -#ifndef INCLUDE_STB_TEXTEDIT_H -#define INCLUDE_STB_TEXTEDIT_H +#ifndef INCLUDE_IMSTB_TEXTEDIT_H +#define INCLUDE_IMSTB_TEXTEDIT_H //////////////////////////////////////////////////////////////////////// // @@ -286,33 +287,33 @@ // and undo state. // -#ifndef STB_TEXTEDIT_UNDOSTATECOUNT -#define STB_TEXTEDIT_UNDOSTATECOUNT 99 +#ifndef IMSTB_TEXTEDIT_UNDOSTATECOUNT +#define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 #endif -#ifndef STB_TEXTEDIT_UNDOCHARCOUNT -#define STB_TEXTEDIT_UNDOCHARCOUNT 999 +#ifndef IMSTB_TEXTEDIT_UNDOCHARCOUNT +#define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 #endif -#ifndef STB_TEXTEDIT_CHARTYPE -#define STB_TEXTEDIT_CHARTYPE int +#ifndef IMSTB_TEXTEDIT_CHARTYPE +#define IMSTB_TEXTEDIT_CHARTYPE int #endif -#ifndef STB_TEXTEDIT_POSITIONTYPE -#define STB_TEXTEDIT_POSITIONTYPE int +#ifndef IMSTB_TEXTEDIT_POSITIONTYPE +#define IMSTB_TEXTEDIT_POSITIONTYPE int #endif typedef struct { // private data - STB_TEXTEDIT_POSITIONTYPE where; - STB_TEXTEDIT_POSITIONTYPE insert_length; - STB_TEXTEDIT_POSITIONTYPE delete_length; + IMSTB_TEXTEDIT_POSITIONTYPE where; + IMSTB_TEXTEDIT_POSITIONTYPE insert_length; + IMSTB_TEXTEDIT_POSITIONTYPE delete_length; int char_storage; } StbUndoRecord; typedef struct { // private data - StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT]; - STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT]; + StbUndoRecord undo_rec [IMSTB_TEXTEDIT_UNDOSTATECOUNT]; + IMSTB_TEXTEDIT_CHARTYPE undo_char[IMSTB_TEXTEDIT_UNDOCHARCOUNT]; short undo_point, redo_point; int undo_char_point, redo_char_point; } StbUndoState; @@ -371,7 +372,7 @@ typedef struct float ymin,ymax; // height of row above and below baseline int num_chars; } StbTexteditRow; -#endif //INCLUDE_STB_TEXTEDIT_H +#endif //INCLUDE_IMSTB_TEXTEDIT_H //////////////////////////////////////////////////////////////////////////// @@ -384,11 +385,11 @@ typedef struct // implementation isn't include-guarded, since it might have indirectly // included just the "header" portion -#ifdef STB_TEXTEDIT_IMPLEMENTATION +#ifdef IMSTB_TEXTEDIT_IMPLEMENTATION -#ifndef STB_TEXTEDIT_memmove +#ifndef IMSTB_TEXTEDIT_memmove #include -#define STB_TEXTEDIT_memmove memmove +#define IMSTB_TEXTEDIT_memmove memmove #endif @@ -398,7 +399,7 @@ typedef struct // // traverse the layout to locate the nearest character to a display position -static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y) +static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y) { StbTexteditRow r; int n = STB_TEXTEDIT_STRINGLEN(str); @@ -458,7 +459,7 @@ static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y) } // API click: on mouse down, move the cursor to the clicked location, and reset the selection -static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +static void stb_textedit_click(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) { // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse // goes off the top or bottom of the text @@ -476,7 +477,7 @@ static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *stat } // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location -static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) +static void stb_textedit_drag(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y) { int p = 0; @@ -502,11 +503,11 @@ static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state // // forward declarations -static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); -static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state); -static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); +static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state); +static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state); +static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length); static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length); -static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); +static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length); typedef struct { @@ -518,7 +519,7 @@ typedef struct // find the x/y location of a character, and remember info about the previous row in // case we get a move-up event (for page up, we'll have to rescan) -static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line) +static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING *str, int n, int single_line) { StbTexteditRow r; int prev_start = 0; @@ -549,7 +550,10 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s i += r.num_chars; find->y += r.baseline_y_delta; if (i == z) // [DEAR IMGUI] + { + r.num_chars = 0; // [DEAR IMGUI] break; // [DEAR IMGUI] + } } find->first_char = first = i; @@ -566,7 +570,7 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end) // make the selection/cursor state valid if client altered the string -static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static void stb_textedit_clamp(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { int n = STB_TEXTEDIT_STRINGLEN(str); if (STB_TEXT_HAS_SELECTION(state)) { @@ -580,7 +584,7 @@ static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *stat } // delete characters while updating undo -static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) +static void stb_textedit_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len) { stb_text_makeundo_delete(str, state, where, len); STB_TEXTEDIT_DELETECHARS(str, where, len); @@ -588,7 +592,7 @@ static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *sta } // delete the section -static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static void stb_textedit_delete_selection(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { stb_textedit_clamp(str, state); if (STB_TEXT_HAS_SELECTION(state)) { @@ -625,7 +629,7 @@ static void stb_textedit_move_to_first(STB_TexteditState *state) } // move cursor to last character of selection -static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { if (STB_TEXT_HAS_SELECTION(state)) { stb_textedit_sortselection(state); @@ -637,13 +641,13 @@ static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditStat } #ifdef STB_TEXTEDIT_IS_SPACE -static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx ) +static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx ) { return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1; } #ifndef STB_TEXTEDIT_MOVEWORDLEFT -static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c ) +static int stb_textedit_move_to_word_previous( IMSTB_TEXTEDIT_STRING *str, int c ) { --c; // always move at least one character while( c >= 0 && !is_word_boundary( str, c ) ) @@ -658,7 +662,7 @@ static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c ) #endif #ifndef STB_TEXTEDIT_MOVEWORDRIGHT -static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c ) +static int stb_textedit_move_to_word_next( IMSTB_TEXTEDIT_STRING *str, int c ) { const int len = STB_TEXTEDIT_STRINGLEN(str); ++c; // always move at least one character @@ -685,7 +689,7 @@ static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state) } // API cut: delete selection -static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static int stb_textedit_cut(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { if (STB_TEXT_HAS_SELECTION(state)) { stb_textedit_delete_selection(str,state); // implicitly clamps @@ -696,7 +700,7 @@ static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) } // API paste: replace existing selection with passed-in text -static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) +static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE *text, int len) { // if there's a selection, the paste should delete it stb_textedit_clamp(str, state); @@ -717,14 +721,14 @@ static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditSta #endif // API key: process a keyboard input -static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) +static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) { retry: switch (key) { default: { int c = STB_TEXTEDIT_KEYTOTEXT(key); if (c > 0) { - STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c; + IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE) c; // can't add newline in single-line mode if (c == '\n' && state->single_line) @@ -889,8 +893,8 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, x = row.x0; for (i=0; i < row.num_chars; ++i) { float dx = STB_TEXTEDIT_GETWIDTH(str, start, i); - #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE - if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) + #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE + if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE) break; #endif x += dx; @@ -951,8 +955,8 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, x = row.x0; for (i=0; i < row.num_chars; ++i) { float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i); - #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE - if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE) + #ifdef IMSTB_TEXTEDIT_GETWIDTH_NEWLINE + if (dx == IMSTB_TEXTEDIT_GETWIDTH_NEWLINE) break; #endif x += dx; @@ -1109,8 +1113,8 @@ static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, static void stb_textedit_flush_redo(StbUndoState *state) { - state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; - state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; + state->redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT; + state->redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT; } // discard the oldest entry in the undo list @@ -1122,13 +1126,13 @@ static void stb_textedit_discard_undo(StbUndoState *state) int n = state->undo_rec[0].insert_length, i; // delete n characters from all other records state->undo_char_point -= n; - STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE))); + IMSTB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(IMSTB_TEXTEDIT_CHARTYPE))); for (i=0; i < state->undo_point; ++i) if (state->undo_rec[i].char_storage >= 0) state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it } --state->undo_point; - STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); + IMSTB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0]))); } } @@ -1138,7 +1142,7 @@ static void stb_textedit_discard_undo(StbUndoState *state) // fill up even though the undo buffer didn't static void stb_textedit_discard_redo(StbUndoState *state) { - int k = STB_TEXTEDIT_UNDOSTATECOUNT-1; + int k = IMSTB_TEXTEDIT_UNDOSTATECOUNT-1; if (state->redo_point <= k) { // if the k'th undo state has characters, clean those up @@ -1146,7 +1150,7 @@ static void stb_textedit_discard_redo(StbUndoState *state) int n = state->undo_rec[k].insert_length, i; // move the remaining redo character data to the end of the buffer state->redo_char_point += n; - STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE))); + IMSTB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((IMSTB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(IMSTB_TEXTEDIT_CHARTYPE))); // adjust the position of all the other records to account for above memmove for (i=state->redo_point; i < k; ++i) if (state->undo_rec[i].char_storage >= 0) @@ -1154,12 +1158,12 @@ static void stb_textedit_discard_redo(StbUndoState *state) } // now move all the redo records towards the end of the buffer; the first one is at 'redo_point' // [DEAR IMGUI] - size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); + size_t move_size = (size_t)((IMSTB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0])); const char* buf_begin = (char*)state->undo_rec; (void)buf_begin; const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end; IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin); IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end); - STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); + IMSTB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size); // now move redo_point to point to the new one ++state->redo_point; @@ -1173,32 +1177,32 @@ static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numch // if we have no free records, we have to make room, by sliding the // existing records down - if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + if (state->undo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) stb_textedit_discard_undo(state); // if the characters to store won't possibly fit in the buffer, we can't undo - if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) { + if (numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) { state->undo_point = 0; state->undo_char_point = 0; return NULL; } // if we don't have enough free characters in the buffer, we have to make room - while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT) + while (state->undo_char_point + numchars > IMSTB_TEXTEDIT_UNDOCHARCOUNT) stb_textedit_discard_undo(state); return &state->undo_rec[state->undo_point++]; } -static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) +static IMSTB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len) { StbUndoRecord *r = stb_text_create_undo_record(state, insert_len); if (r == NULL) return NULL; r->where = pos; - r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len; - r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len; + r->insert_length = (IMSTB_TEXTEDIT_POSITIONTYPE) insert_len; + r->delete_length = (IMSTB_TEXTEDIT_POSITIONTYPE) delete_len; if (insert_len == 0) { r->char_storage = -1; @@ -1210,7 +1214,7 @@ static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, } } -static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static void stb_text_undo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { StbUndoState *s = &state->undostate; StbUndoRecord u, *r; @@ -1237,7 +1241,7 @@ static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) // characters stored for *undoing* don't leave room for redo // if the last is true, we have to bail - if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) { + if (s->undo_char_point + u.delete_length >= IMSTB_TEXTEDIT_UNDOCHARCOUNT) { // the undo records take up too much character space; there's no space to store the redo characters r->insert_length = 0; } else { @@ -1246,7 +1250,7 @@ static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) // there's definitely room to store the characters eventually while (s->undo_char_point + u.delete_length > s->redo_char_point) { // should never happen: - if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) return; // there's currently not enough room, so discard a redo record stb_textedit_discard_redo(s); @@ -1278,11 +1282,11 @@ static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) s->redo_point--; } -static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) +static void stb_text_redo(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state) { StbUndoState *s = &state->undostate; StbUndoRecord *u, r; - if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT) + if (s->redo_point == IMSTB_TEXTEDIT_UNDOSTATECOUNT) return; // we need to do two things: apply the redo record, and create an undo record @@ -1334,20 +1338,20 @@ static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int le stb_text_createundo(&state->undostate, where, 0, length); } -static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) +static void stb_text_makeundo_delete(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length) { int i; - STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); + IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0); if (p) { for (i=0; i < length; ++i) p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); } } -static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) +static void stb_text_makeundo_replace(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length) { int i; - STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); + IMSTB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length); if (p) { for (i=0; i < old_length; ++i) p[i] = STB_TEXTEDIT_GETCHAR(str, where+i); @@ -1359,8 +1363,8 @@ static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_lin { state->undostate.undo_point = 0; state->undostate.undo_char_point = 0; - state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT; - state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT; + state->undostate.redo_point = IMSTB_TEXTEDIT_UNDOSTATECOUNT; + state->undostate.redo_char_point = IMSTB_TEXTEDIT_UNDOCHARCOUNT; state->select_end = state->select_start = 0; state->cursor = 0; state->has_preferred_x = 0; @@ -1383,16 +1387,16 @@ static void stb_textedit_initialize_state(STB_TexteditState *state, int is_singl #pragma GCC diagnostic ignored "-Wcast-qual" #endif -static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len) +static int stb_textedit_paste(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, IMSTB_TEXTEDIT_CHARTYPE const *ctext, int len) { - return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len); + return stb_textedit_paste_internal(str, state, (IMSTB_TEXTEDIT_CHARTYPE *) ctext, len); } #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic pop #endif -#endif//STB_TEXTEDIT_IMPLEMENTATION +#endif//IMSTB_TEXTEDIT_IMPLEMENTATION /* ------------------------------------------------------------------------------ diff --git a/src/imgui/LICENSE.txt b/src/imgui/LICENSE.txt index fb715bdc..3282f5b5 100644 --- a/src/imgui/LICENSE.txt +++ b/src/imgui/LICENSE.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2023 Omar Cornut +Copyright (c) 2014-2024 Omar Cornut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index 39fcd707..e406a9f1 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,32 +1,33 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (main code and documentation) // Help: -// - Read FAQ at http://dearimgui.com/faq -// - Newcomers, read 'Programmer guide' below for notes on how to setup Dear ImGui in your codebase. +// - See links below. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. -// Read imgui.cpp for details, links and comments. +// - Read top of imgui.cpp for more details, links and comments. // Resources: -// - FAQ http://dearimgui.com/faq +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started // - Homepage https://github.com/ocornut/imgui // - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/6478 (please post your screenshots/video there!) +// - Gallery https://github.com/ocornut/imgui/issues/6897 (please post your screenshots/video there!) // - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) -// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started // - Glossary https://github.com/ocornut/imgui/wiki/Glossary // - Issues & support https://github.com/ocornut/imgui/issues +// - Tests & Automation https://github.com/ocornut/imgui_test_engine -// Getting Started? -// - Read https://github.com/ocornut/imgui/wiki/Getting-Started -// - For first-time users having issues compiling/linking/running/loading fonts: -// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. +// For first-time users having issues compiling/linking/running/loading fonts: +// please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. +// Everything else should be asked in 'Issues'! We are building a database of cross-linked knowledge there. +// Copyright (c) 2014-2024 Omar Cornut // Developed by Omar Cornut and every direct or indirect contributors to the GitHub. // See LICENSE.txt for copyright and licensing details (standard MIT License). // This library is free but needs your support to sustain development and maintenance. -// Businesses: you can support continued development via invoiced technical support, maintenance and sponsoring contracts. Please reach out to "contact AT dearimgui.com". -// Individuals: you can support continued development via donations. See docs/README or web page. +// Businesses: you can support continued development via B2B invoiced technical support, maintenance and sponsoring contracts. +// PLEASE reach out at omar AT dearimgui DOT com. See https://github.com/ocornut/imgui/wiki/Sponsors +// Businesses: you can also purchase licenses for the Dear ImGui Automation/Test Engine. // It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. // Note that 'ImGui::' being a namespace, you can add functions into the namespace from your own source files, without @@ -87,7 +88,7 @@ CODE // [SECTION] PLATFORM DEPENDENT HELPERS // [SECTION] METRICS/DEBUGGER WINDOW // [SECTION] DEBUG LOG WINDOW -// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) +// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, ID STACK TOOL) */ @@ -109,9 +110,10 @@ CODE - Portable, minimize dependencies, run on target (consoles, phones, etc.). - Efficient runtime and memory consumption. - Designed for developers and content-creators, not the typical end-user! Some of the current weaknesses includes: + Designed primarily for developers and content-creators, not the typical end-user! + Some of the current weaknesses (which we aim to address in the future) includes: - - Doesn't look fancy, doesn't animate. + - Doesn't look fancy. - Limited layout features, intricate layouts are typically crafted in code. @@ -190,9 +192,11 @@ CODE READ FIRST ---------- - Remember to check the wonderful Wiki (https://github.com/ocornut/imgui/wiki) - - Your code creates the UI, if your code doesn't run the UI is gone! The UI can be highly dynamic, there are no construction or - destruction steps, less superfluous data retention on your side, less state duplication, less state synchronization, fewer bugs. + - Your code creates the UI every frame of your application loop, if your code doesn't run the UI is gone! + The UI can be highly dynamic, there are no construction or destruction steps, less superfluous + data retention on your side, less state duplication, less state synchronization, fewer bugs. - Call and read ImGui::ShowDemoWindow() for demo code demonstrating most features. + Or browse https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html for interactive web version. - The library is designed to be built from sources. Avoid pre-compiled binaries and packaged versions. See imconfig.h to configure your build. - Dear ImGui is an implementation of the IMGUI paradigm (immediate-mode graphical user interface, a term coined by Casey Muratori). You can learn about IMGUI principles at http://www.johno.se/book/imgui.html, http://mollyrocket.com/861 & more links in Wiki. @@ -200,18 +204,38 @@ CODE For every application frame, your UI code will be called only once. This is in contrast to e.g. Unity's implementation of an IMGUI, where the UI code is called multiple times ("multiple passes") from a single entry point. There are pros and cons to both approaches. - Our origin is on the top-left. In axis aligned bounding boxes, Min = top-left, Max = bottom-right. - - This codebase is also optimized to yield decent performances with typical "Debug" builds settings. - Please make sure you have asserts enabled (IM_ASSERT redirects to assert() by default, but can be redirected). If you get an assert, read the messages and comments around the assert. - - C++: this is a very C-ish codebase: we don't rely on C++11, we don't include any C++ headers, and ImGui:: is a namespace. - - C++: ImVec2/ImVec4 do not expose math operators by default, because it is expected that you use your own math types. - See FAQ "How can I use my own math types instead of ImVec2/ImVec4?" for details about setting up imconfig.h for that. - However, imgui_internal.h can optionally export math operators for ImVec2/ImVec4, which we use in this codebase. - - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction (avoid using it in your code!). + - This codebase aims to be highly optimized: + - A typical idle frame should never call malloc/free. + - We rely on a maximum of constant-time or O(N) algorithms. Limiting searches/scans as much as possible. + - We put particular energy in making sure performances are decent with typical "Debug" build settings as well. + Which mean we tend to avoid over-relying on "zero-cost abstraction" as they aren't zero-cost at all. + - This codebase aims to be both highly opinionated and highly flexible: + - This code works because of the things it choose to solve or not solve. + - C++: this is a pragmatic C-ish codebase: we don't use fancy C++ features, we don't include C++ headers, + and ImGui:: is a namespace. We rarely use member functions (and when we did, I am mostly regretting it now). + This is to increase compatibility, increase maintainability and facilitate use from other languages. + - C++: ImVec2/ImVec4 do not expose math operators by default, because it is expected that you use your own math types. + See FAQ "How can I use my own math types instead of ImVec2/ImVec4?" for details about setting up imconfig.h for that. + We can can optionally export math operators for ImVec2/ImVec4 using IMGUI_DEFINE_MATH_OPERATORS, which we use internally. + - C++: pay attention that ImVector<> manipulates plain-old-data and does not honor construction/destruction + (so don't use ImVector in your code or at our own risk!). + - Building: We don't use nor mandate a build system for the main library. + This is in an effort to ensure that it works in the real world aka with any esoteric build setup. + This is also because providing a build system for the main library would be of little-value. + The build problems are almost never coming from the main library but from specific backends. HOW TO UPDATE TO A NEWER VERSION OF DEAR IMGUI ---------------------------------------------- + - Update submodule or copy/overwrite every file. + - About imconfig.h: + - You may modify your copy of imconfig.h, in this case don't overwrite it. + - or you may locally branch to modify imconfig.h and merge/rebase latest. + - or you may '#define IMGUI_USER_CONFIG "my_config_file.h"' globally from your build system to + specify a custom path for your imconfig.h file and instead not have to modify the default one. + - Overwrite all the sources files except for imconfig.h (if you have modified your copy of imconfig.h) - Or maintain your own branch where you have imconfig.h modified as a top-most commit which you can regularly rebase over "master". - You can also use '#define IMGUI_USER_CONFIG "my_config_file.h" to redirect configuration to your own file. @@ -220,11 +244,12 @@ CODE from the public API. If you have a problem with a missing function/symbols, search for its name in the code, there will likely be a comment about it. Please report any issue to the GitHub page! - To find out usage of old API, you can add '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in your configuration file. - - Try to keep your copy of Dear ImGui reasonably up to date. + - Try to keep your copy of Dear ImGui reasonably up to date! GETTING STARTED WITH INTEGRATING DEAR IMGUI IN YOUR CODE/ENGINE --------------------------------------------------------------- + - See https://github.com/ocornut/imgui/wiki/Getting-Started. - Run and study the examples and demo in imgui_demo.cpp to get acquainted with the library. - In the majority of cases you should be able to use unmodified backends files available in the backends/ folder. - Add the Dear ImGui source files + selected backend source files to your projects or using your preferred build system. @@ -332,7 +357,7 @@ CODE To decide whether to dispatch mouse/keyboard inputs to Dear ImGui to the rest of your application, you should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! - Please read the FAQ and example applications for details about this! + Please read the FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" about this. HOW A SIMPLE RENDERING FUNCTION MAY LOOK LIKE @@ -399,9 +424,38 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2023/12/19 (1.90.1) - commented out obsolete ImGuiKey_KeyPadEnter redirection to ImGuiKey_KeypadEnter. + - 2023/11/06 (1.90.1) - removed CalcListClipping() marked obsolete in 1.86. Prefer using ImGuiListClipper which can return non-contiguous ranges. + - 2023/11/05 (1.90.1) - imgui_freetype: commented out ImGuiFreeType::BuildFontAtlas() obsoleted in 1.81. prefer using #define IMGUI_ENABLE_FREETYPE or see commented code for manual calls. + - 2023/11/05 (1.90.1) - internals,columns: commented out legacy ImGuiColumnsFlags_XXX symbols redirecting to ImGuiOldColumnsFlags_XXX, obsoleted from imgui_internal.h in 1.80. + - 2023/11/09 (1.90.0) - removed IM_OFFSETOF() macro in favor of using offsetof() available in C++11. Kept redirection define (will obsolete). + - 2023/11/07 (1.90.0) - removed BeginChildFrame()/EndChildFrame() in favor of using BeginChild() with the ImGuiChildFlags_FrameStyle flag. kept inline redirection function (will obsolete). + those functions were merely PushStyle/PopStyle helpers, the removal isn't so much motivated by needing to add the feature in BeginChild(), but by the necessity to avoid BeginChildFrame() signature mismatching BeginChild() signature and features. + - 2023/11/02 (1.90.0) - BeginChild: upgraded 'bool border = true' parameter to 'ImGuiChildFlags flags' type, added ImGuiChildFlags_Border equivalent. As with our prior "bool-to-flags" API updates, the ImGuiChildFlags_Border value is guaranteed to be == true forever to ensure a smoother transition, meaning all existing calls will still work. + - old: BeginChild("Name", size, true) + - new: BeginChild("Name", size, ImGuiChildFlags_Border) + - old: BeginChild("Name", size, false) + - new: BeginChild("Name", size) or BeginChild("Name", 0) or BeginChild("Name", size, ImGuiChildFlags_None) + - 2023/11/02 (1.90.0) - BeginChild: added child-flag ImGuiChildFlags_AlwaysUseWindowPadding as a replacement for the window-flag ImGuiWindowFlags_AlwaysUseWindowPadding: the feature only ever made sense for BeginChild() anyhow. + - old: BeginChild("Name", size, 0, ImGuiWindowFlags_AlwaysUseWindowPadding); + - new: BeginChild("Name", size, ImGuiChildFlags_AlwaysUseWindowPadding, 0); + - 2023/09/27 (1.90.0) - io: removed io.MetricsActiveAllocations introduced in 1.63. Same as 'g.DebugMemAllocCount - g.DebugMemFreeCount' (still displayed in Metrics, unlikely to be accessed by end-user). + - 2023/09/26 (1.90.0) - debug tools: Renamed ShowStackToolWindow() ("Stack Tool") to ShowIDStackToolWindow() ("ID Stack Tool"), as earlier name was misleading. Kept inline redirection function. (#4631) + - 2023/09/15 (1.90.0) - ListBox, Combo: changed signature of "name getter" callback in old one-liner ListBox()/Combo() apis. kept inline redirection function (will obsolete). + - old: bool Combo(const char* label, int* current_item, bool (*getter)(void* user_data, int idx, const char** out_text), ...) + - new: bool Combo(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), ...); + - old: bool ListBox(const char* label, int* current_item, bool (*getting)(void* user_data, int idx, const char** out_text), ...); + - new: bool ListBox(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), ...); + - 2023/09/08 (1.90.0) - commented out obsolete redirecting functions: + - GetWindowContentRegionWidth() -> use GetWindowContentRegionMax().x - GetWindowContentRegionMin().x. Consider that generally 'GetContentRegionAvail().x' is more useful. + - ImDrawCornerFlags_XXX -> use ImDrawFlags_RoundCornersXXX flags. Read 1.82 Changelog for details + grep commented names in sources. + - commented out runtime support for hardcoded ~0 or 0x01..0x0F rounding flags values for AddRect()/AddRectFilled()/PathRect()/AddImageRounded() -> use ImDrawFlags_RoundCornersXXX flags. Read 1.82 Changelog for details + - 2023/08/25 (1.89.9) - clipper: Renamed IncludeRangeByIndices() (also called ForceDisplayRangeByIndices() before 1.89.6) to IncludeItemsByIndex(). Kept inline redirection function. Sorry! + - 2023/07/12 (1.89.8) - ImDrawData: CmdLists now owned, changed from ImDrawList** to ImVector. Majority of users shouldn't be affected, but you cannot compare to NULL nor reassign manually anymore. Instead use AddDrawList(). (#6406, #4879, #1878) - 2023/06/28 (1.89.7) - overlapping items: obsoleted 'SetItemAllowOverlap()' (called after item) in favor of calling 'SetNextItemAllowOverlap()' (called before item). 'SetItemAllowOverlap()' didn't and couldn't work reliably since 1.89 (2022-11-15). - 2023/06/28 (1.89.7) - overlapping items: renamed 'ImGuiTreeNodeFlags_AllowItemOverlap' to 'ImGuiTreeNodeFlags_AllowOverlap', 'ImGuiSelectableFlags_AllowItemOverlap' to 'ImGuiSelectableFlags_AllowOverlap'. Kept redirecting enums (will obsolete). - 2023/06/28 (1.89.7) - overlapping items: IsItemHovered() now by default return false when querying an item using AllowOverlap mode which is being overlapped. Use ImGuiHoveredFlags_AllowWhenOverlappedByItem to revert to old behavior. + - 2023/06/28 (1.89.7) - overlapping items: Selectable and TreeNode don't allow overlap when active so overlapping widgets won't appear as hovered. While this fixes a common small visual issue, it also means that calling IsItemHovered() after a non-reactive elements - e.g. Text() - overlapping an active one may fail if you don't use IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem). (#6610) - 2023/06/20 (1.89.7) - moved io.HoverDelayShort/io.HoverDelayNormal to style.HoverDelayShort/style.HoverDelayNormal. As the fields were added in 1.89 and expected to be left unchanged by most users, or only tweaked once during app initialization, we are exceptionally accepting the breakage. - 2023/05/30 (1.89.6) - backends: renamed "imgui_impl_sdlrenderer.cpp" to "imgui_impl_sdlrenderer2.cpp" and "imgui_impl_sdlrenderer.h" to "imgui_impl_sdlrenderer2.h". This is in prevision for the future release of SDL3. - 2023/05/22 (1.89.6) - listbox: commented out obsolete/redirecting functions that were marked obsolete more than two years ago: @@ -805,11 +859,12 @@ CODE Q: Where is the documentation? A: This library is poorly documented at the moment and expects the user to be acquainted with C/C++. - - Run the examples/ and explore them. + - Run the examples/ applications and explore them. + - Read Getting Started (https://github.com/ocornut/imgui/wiki/Getting-Started) guide. - See demo code in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. - The demo covers most features of Dear ImGui, so you can read the code and see its output. - See documentation and comments at the top of imgui.cpp + effectively imgui.h. - - Dozens of standalone example applications using e.g. OpenGL/DirectX are provided in the + - 20+ standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ folder to explain how to integrate Dear ImGui with your own engine/application. - The Wiki (https://github.com/ocornut/imgui/wiki) has many resources and links. - The Glossary (https://github.com/ocornut/imgui/wiki/Glossary) page also may be useful. @@ -825,14 +880,14 @@ CODE ================ Q: How to get started? - A: Read 'PROGRAMMER GUIDE' above. Read examples/README.txt. + A: Read https://github.com/ocornut/imgui/wiki/Getting-Started. Read 'PROGRAMMER GUIDE' above. Read examples/README.txt. Q: How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application? A: You should read the 'io.WantCaptureMouse', 'io.WantCaptureKeyboard' and 'io.WantTextInput' flags! >> See https://www.dearimgui.com/faq for a fully detailed answer. You really want to read this. - Q. How can I enable keyboard controls? - Q: How can I use this without a mouse, without a keyboard or without a screen? (gamepad, input share, remote display) + Q. How can I enable keyboard or gamepad controls? + Q: How can I use this on a machine without mouse, keyboard or screen? (input share, remote display) Q: I integrated Dear ImGui in my engine and little squares are showing instead of text... Q: I integrated Dear ImGui in my engine and some elements are clipping or disappearing when I move windows around... Q: I integrated Dear ImGui in my engine and some elements are displaying outside their expected windows boundaries... @@ -847,7 +902,7 @@ CODE - How can I have multiple widgets with the same label? - How can I have multiple windows with the same label? Q: How can I display an image? What is ImTextureID, how does it work? - Q: How can I use my own math types instead of ImVec2/ImVec4? + Q: How can I use my own math types instead of ImVec2? Q: How can I interact with standard C++ types (such as std::string and std::vector)? Q: How can I display custom shapes? (using low-level ImDrawList API) >> See https://www.dearimgui.com/faq @@ -875,12 +930,12 @@ CODE ============== Q: How can I help? - A: - Businesses: please reach out to "contact AT dearimgui.com" if you work in a place using Dear ImGui! + A: - Businesses: please reach out to "omar AT dearimgui DOT com" if you work in a place using Dear ImGui! We can discuss ways for your company to fund development via invoiced technical support, maintenance or sponsoring contacts. - This is among the most useful thing you can do for Dear ImGui. With increased funding, we can hire more people working on this project. - - Individuals: you can support continued development via PayPal donations. See README. - - If you are experienced with Dear ImGui and C++, look at the GitHub issues, look at the Wiki, read docs/TODO.txt - and see how you want to help and can help! + This is among the most useful thing you can do for Dear ImGui. With increased funding, we sustain and grow work on this project. + Also see https://github.com/ocornut/imgui/wiki/Sponsors + - Businesses: you can also purchase licenses for the Dear ImGui Automation/Test Engine. + - If you are experienced with Dear ImGui and C++, look at the GitHub issues, look at the Wiki, and see how you want to help and can help! - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. You may post screenshot or links in the gallery threads. Visuals are ideal as they inspire other programmers. But even without visuals, disclosing your use of dear imgui helps the library grow credibility, and help other teams and programmers with taking decisions. @@ -906,11 +961,7 @@ CODE // System includes #include // vsnprintf, sscanf, printf -#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier -#include // intptr_t -#else #include // intptr_t -#endif // [Windows] On non-Visual Studio compilers, we default to IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS unless explicitly enabled #if defined(_WIN32) && !defined(_MSC_VER) && !defined(IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) @@ -1011,7 +1062,6 @@ static void FindHoveredWindow(); static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags); static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window); -static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list); static void AddWindowToSortBuffer(ImVector* out_sorted_windows, ImGuiWindow* window); // Settings @@ -1047,7 +1097,6 @@ static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); static void NavRestoreLayer(ImGuiNavLayer layer); -static void NavRestoreHighlightAfterMove(); static int FindWindowFocusIndex(ImGuiWindow* window); // Error Checking and Debug Tools @@ -1055,6 +1104,7 @@ static void ErrorCheckNewFrameSanityChecks(); static void ErrorCheckEndFrameSanityChecks(); static void UpdateDebugToolItemPicker(); static void UpdateDebugToolStackQueries(); +static void UpdateDebugToolFlashStyleColor(); // Inputs static void UpdateKeyboardInputs(); @@ -1064,7 +1114,7 @@ static void UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt); // Misc static void UpdateSettings(); -static bool UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); +static int UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_hovered, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect); static void RenderWindowOuterBorders(ImGuiWindow* window); static void RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar_rect, bool title_bar_is_highlight, bool handle_borders_and_resize_grips, int resize_grip_count, const ImU32 resize_grip_col[4], float resize_grip_draw_size); static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); @@ -1141,7 +1191,7 @@ ImGuiStyle::ImGuiStyle() FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) - CellPadding = ImVec2(4,2); // Padding within a table cell + CellPadding = ImVec2(4,2); // Padding within a table cell. CellPadding.y may be altered between different rows. TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). @@ -1153,6 +1203,8 @@ ImGuiStyle::ImGuiStyle() TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. TabBorderSize = 0.0f; // Thickness of border around tabs. TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + TabBarBorderSize = 1.0f; // Thickness of tab-bar separator, which takes on the tab active color to denote focus. + TableAngledHeadersAngle = 35.0f * (IM_PI / 180.0f); // Angle of angled headers (supported values range from -50 degrees to +50 degrees). ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. @@ -1172,8 +1224,8 @@ ImGuiStyle::ImGuiStyle() HoverStationaryDelay = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_Stationary). Time required to consider mouse stationary. HoverDelayShort = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayShort). Usually used along with HoverStationaryDelay. HoverDelayNormal = 0.40f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayNormal). " - HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. - HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. + HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. + HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. // Default theme ImGui::StyleColorsDark(this); @@ -1183,30 +1235,30 @@ ImGuiStyle::ImGuiStyle() // Important: This operation is lossy because we round all sizes to integer. If you need to change your scale multiples, call this over a freshly initialized ImGuiStyle structure rather than scaling multiple times. void ImGuiStyle::ScaleAllSizes(float scale_factor) { - WindowPadding = ImFloor(WindowPadding * scale_factor); - WindowRounding = ImFloor(WindowRounding * scale_factor); - WindowMinSize = ImFloor(WindowMinSize * scale_factor); - ChildRounding = ImFloor(ChildRounding * scale_factor); - PopupRounding = ImFloor(PopupRounding * scale_factor); - FramePadding = ImFloor(FramePadding * scale_factor); - FrameRounding = ImFloor(FrameRounding * scale_factor); - ItemSpacing = ImFloor(ItemSpacing * scale_factor); - ItemInnerSpacing = ImFloor(ItemInnerSpacing * scale_factor); - CellPadding = ImFloor(CellPadding * scale_factor); - TouchExtraPadding = ImFloor(TouchExtraPadding * scale_factor); - IndentSpacing = ImFloor(IndentSpacing * scale_factor); - ColumnsMinSpacing = ImFloor(ColumnsMinSpacing * scale_factor); - ScrollbarSize = ImFloor(ScrollbarSize * scale_factor); - ScrollbarRounding = ImFloor(ScrollbarRounding * scale_factor); - GrabMinSize = ImFloor(GrabMinSize * scale_factor); - GrabRounding = ImFloor(GrabRounding * scale_factor); - LogSliderDeadzone = ImFloor(LogSliderDeadzone * scale_factor); - TabRounding = ImFloor(TabRounding * scale_factor); - TabMinWidthForCloseButton = (TabMinWidthForCloseButton != FLT_MAX) ? ImFloor(TabMinWidthForCloseButton * scale_factor) : FLT_MAX; - SeparatorTextPadding = ImFloor(SeparatorTextPadding * scale_factor); - DisplayWindowPadding = ImFloor(DisplayWindowPadding * scale_factor); - DisplaySafeAreaPadding = ImFloor(DisplaySafeAreaPadding * scale_factor); - MouseCursorScale = ImFloor(MouseCursorScale * scale_factor); + WindowPadding = ImTrunc(WindowPadding * scale_factor); + WindowRounding = ImTrunc(WindowRounding * scale_factor); + WindowMinSize = ImTrunc(WindowMinSize * scale_factor); + ChildRounding = ImTrunc(ChildRounding * scale_factor); + PopupRounding = ImTrunc(PopupRounding * scale_factor); + FramePadding = ImTrunc(FramePadding * scale_factor); + FrameRounding = ImTrunc(FrameRounding * scale_factor); + ItemSpacing = ImTrunc(ItemSpacing * scale_factor); + ItemInnerSpacing = ImTrunc(ItemInnerSpacing * scale_factor); + CellPadding = ImTrunc(CellPadding * scale_factor); + TouchExtraPadding = ImTrunc(TouchExtraPadding * scale_factor); + IndentSpacing = ImTrunc(IndentSpacing * scale_factor); + ColumnsMinSpacing = ImTrunc(ColumnsMinSpacing * scale_factor); + ScrollbarSize = ImTrunc(ScrollbarSize * scale_factor); + ScrollbarRounding = ImTrunc(ScrollbarRounding * scale_factor); + GrabMinSize = ImTrunc(GrabMinSize * scale_factor); + GrabRounding = ImTrunc(GrabRounding * scale_factor); + LogSliderDeadzone = ImTrunc(LogSliderDeadzone * scale_factor); + TabRounding = ImTrunc(TabRounding * scale_factor); + TabMinWidthForCloseButton = (TabMinWidthForCloseButton != FLT_MAX) ? ImTrunc(TabMinWidthForCloseButton * scale_factor) : FLT_MAX; + SeparatorTextPadding = ImTrunc(SeparatorTextPadding * scale_factor); + DisplayWindowPadding = ImTrunc(DisplayWindowPadding * scale_factor); + DisplaySafeAreaPadding = ImTrunc(DisplaySafeAreaPadding * scale_factor); + MouseCursorScale = ImTrunc(MouseCursorScale * scale_factor); } ImGuiIO::ImGuiIO() @@ -1262,6 +1314,7 @@ ImGuiIO::ImGuiIO() // Note: Initialize() will setup default clipboard/ime handlers. BackendPlatformName = BackendRendererName = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; + PlatformLocaleDecimalPoint = '.'; // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); @@ -1341,13 +1394,15 @@ void ImGuiIO::AddInputCharactersUTF8(const char* utf8_chars) } } -// FIXME: Perhaps we could clear queued events as well? -void ImGuiIO::ClearInputCharacters() +// Clear all incoming events. +void ImGuiIO::ClearEventsQueue() { - InputQueueCharacters.resize(0); + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; + g.InputEventsQueue.clear(); } -// FIXME: Perhaps we could clear queued events as well? +// Clear current keyboard/mouse/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. void ImGuiIO::ClearInputKeys() { #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -1368,7 +1423,17 @@ void ImGuiIO::ClearInputKeys() MouseDownDuration[n] = MouseDownDurationPrev[n] = -1.0f; } MouseWheel = MouseWheelH = 0.0f; + InputQueueCharacters.resize(0); // Behavior of old ClearInputCharacters(). +} + +// Removed this as it is ambiguous/misleading and generally incorrect to use with the existence of a higher-level input queue. +// Current frame character buffer is now also cleared by ClearInputKeys(). +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS +void ImGuiIO::ClearInputCharacters() +{ + InputQueueCharacters.resize(0); } +#endif static ImGuiInputEvent* FindLatestInputEvent(ImGuiContext* ctx, ImGuiInputEventType type, int arg = -1) { @@ -1481,7 +1546,7 @@ void ImGuiIO::AddMousePosEvent(float x, float y) return; // Apply same flooring as UpdateMouseInputs() - ImVec2 pos((x > -FLT_MAX) ? ImFloorSigned(x) : x, (y > -FLT_MAX) ? ImFloorSigned(y) : y); + ImVec2 pos((x > -FLT_MAX) ? ImFloor(x) : x, (y > -FLT_MAX) ? ImFloor(y) : y); // Filter duplicate const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_MousePos); @@ -1495,7 +1560,7 @@ void ImGuiIO::AddMousePosEvent(float x, float y) e.EventId = g.InputEventsNextEventId++; e.MousePos.PosX = pos.x; e.MousePos.PosY = pos.y; - e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; + e.MousePos.MouseSource = g.InputEventsNextMouseSource; g.InputEventsQueue.push_back(e); } @@ -1519,7 +1584,7 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) e.EventId = g.InputEventsNextEventId++; e.MouseButton.Button = mouse_button; e.MouseButton.Down = down; - e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; + e.MouseButton.MouseSource = g.InputEventsNextMouseSource; g.InputEventsQueue.push_back(e); } @@ -1822,13 +1887,15 @@ const char* ImStrSkipBlank(const char* str) // and setup the wrapper yourself. (FIXME-OPT: Some of our high-level operations such as ImGuiTextBuffer::appendfv() are // designed using two-passes worst case, which probably could be improved using the stbsp_vsprintfcb() function.) #ifdef IMGUI_USE_STB_SPRINTF +#ifndef IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION #define STB_SPRINTF_IMPLEMENTATION +#endif #ifdef IMGUI_STB_SPRINTF_FILENAME #include IMGUI_STB_SPRINTF_FILENAME #else #include "stb_sprintf.h" #endif -#endif +#endif // #ifdef IMGUI_USE_STB_SPRINTF #if defined(_MSC_VER) && !defined(vsnprintf) #define vsnprintf _vsnprintf @@ -1870,21 +1937,9 @@ int ImFormatStringV(char* buf, size_t buf_size, const char* fmt, va_list args) void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, const char* fmt, ...) { - ImGuiContext& g = *GImGui; va_list args; va_start(args, fmt); - if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) - { - const char* buf = va_arg(args, const char*); // Skip formatting when using "%s" - *out_buf = buf; - if (out_buf_end) { *out_buf_end = buf + strlen(buf); } - } - else - { - int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); - *out_buf = g.TempBuffer.Data; - if (out_buf_end) { *out_buf_end = g.TempBuffer.Data + buf_len; } - } + ImFormatStringToTempBufferV(out_buf, out_buf_end, fmt, args); va_end(args); } @@ -1894,9 +1949,23 @@ void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, if (fmt[0] == '%' && fmt[1] == 's' && fmt[2] == 0) { const char* buf = va_arg(args, const char*); // Skip formatting when using "%s" + if (buf == NULL) + buf = "(null)"; *out_buf = buf; if (out_buf_end) { *out_buf_end = buf + strlen(buf); } } + else if (fmt[0] == '%' && fmt[1] == '.' && fmt[2] == '*' && fmt[3] == 's' && fmt[4] == 0) + { + int buf_len = va_arg(args, int); // Skip formatting when using "%.*s" + const char* buf = va_arg(args, const char*); + if (buf == NULL) + { + buf = "(null)"; + buf_len = ImMin(buf_len, 6); + } + *out_buf = buf; + *out_buf_end = buf + buf_len; // Disallow not passing 'out_buf_end' here. User is expected to use it. + } else { int buf_len = ImFormatStringV(g.TempBuffer.Data, g.TempBuffer.Size, fmt, args); @@ -1989,8 +2058,9 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode) // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); - ImVector buf; - buf.resize(filename_wsize + mode_wsize); + ImGuiContext& g = *GImGui; + g.TempBuffer.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t)); + wchar_t* buf = (wchar_t*)(void*)g.TempBuffer.Data; ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize); ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize); return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]); @@ -2225,6 +2295,18 @@ int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_e } return bytes_count; } + +const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr) +{ + while (in_text_curr > in_text_start) + { + in_text_curr--; + if ((*in_text_curr & 0xC0) != 0x80) + return in_text_curr; + } + return in_text_start; +} + IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- @@ -2420,11 +2502,9 @@ void ImGuiStorage::SetInt(ImGuiID key, int val) { ImGuiStoragePair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) - { Data.insert(it, ImGuiStoragePair(key, val)); - return; - } - it->val_i = val; + else + it->val_i = val; } void ImGuiStorage::SetBool(ImGuiID key, bool val) @@ -2436,22 +2516,18 @@ void ImGuiStorage::SetFloat(ImGuiID key, float val) { ImGuiStoragePair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) - { Data.insert(it, ImGuiStoragePair(key, val)); - return; - } - it->val_f = val; + else + it->val_f = val; } void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val) { ImGuiStoragePair* it = LowerBound(Data, key); if (it == Data.end() || it->key != key) - { Data.insert(it, ImGuiStoragePair(key, val)); - return; - } - it->val_p = val; + else + it->val_p = val; } void ImGuiStorage::SetAllInt(int v) @@ -2511,16 +2587,15 @@ void ImGuiTextFilter::Build() input_range.split(',', &Filters); CountGrep = 0; - for (int i = 0; i != Filters.Size; i++) + for (ImGuiTextRange& f : Filters) { - ImGuiTextRange& f = Filters[i]; while (f.b < f.e && ImCharIsBlankA(f.b[0])) f.b++; while (f.e > f.b && ImCharIsBlankA(f.e[-1])) f.e--; if (f.empty()) continue; - if (Filters[i].b[0] != '-') + if (f.b[0] != '-') CountGrep += 1; } } @@ -2533,9 +2608,8 @@ bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const if (text == NULL) text = ""; - for (int i = 0; i != Filters.Size; i++) + for (const ImGuiTextRange& f : Filters) { - const ImGuiTextRange& f = Filters[i]; if (f.empty()) continue; if (f.b[0] == '-') @@ -2644,8 +2718,6 @@ void ImGuiTextIndex::append(const char* base, int old_size, int new_size) //----------------------------------------------------------------------------- // [SECTION] ImGuiListClipper -// This is currently not as flexible/powerful as it should be and really confusing/spaghetti, mostly because we changed -// the API mid-way through development and support two ways to using the clipper, needs some rework (see TODO) //----------------------------------------------------------------------------- // FIXME-TABLE: This prevents us from using ImGuiListClipper _inside_ a table cell. @@ -2656,54 +2728,6 @@ static bool GetSkipItemForListClipping() return (g.CurrentTable ? g.CurrentTable->HostSkipItems : g.CurrentWindow->SkipItems); } -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -// Legacy helper to calculate coarse clipping of large list of evenly sized items. -// This legacy API is not ideal because it assumes we will return a single contiguous rectangle. -// Prefer using ImGuiListClipper which can returns non-contiguous ranges. -void ImGui::CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end) -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (g.LogEnabled) - { - // If logging is active, do not perform any clipping - *out_items_display_start = 0; - *out_items_display_end = items_count; - return; - } - if (GetSkipItemForListClipping()) - { - *out_items_display_start = *out_items_display_end = 0; - return; - } - - // We create the union of the ClipRect and the scoring rect which at worst should be 1 page away from ClipRect - // We don't include g.NavId's rectangle in there (unless g.NavJustMovedToId is set) because the rectangle enlargement can get costly. - ImRect rect = window->ClipRect; - if (g.NavMoveScoringItems) - rect.Add(g.NavScoringNoClipRect); - if (g.NavJustMovedToId && window->NavLastIds[0] == g.NavJustMovedToId) - rect.Add(WindowRectRelToAbs(window, window->NavRectRel[0])); // Could store and use NavJustMovedToRectRel - - const ImVec2 pos = window->DC.CursorPos; - int start = (int)((rect.Min.y - pos.y) / items_height); - int end = (int)((rect.Max.y - pos.y) / items_height); - - // When performing a navigation request, ensure we have one item extra in the direction we are moving to - // FIXME: Verify this works with tabbing - const bool is_nav_request = (g.NavMoveScoringItems && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); - if (is_nav_request && g.NavMoveClipDir == ImGuiDir_Up) - start--; - if (is_nav_request && g.NavMoveClipDir == ImGuiDir_Down) - end++; - - start = ImClamp(start, 0, items_count); - end = ImClamp(end + 1, start, items_count); - *out_items_display_start = start; - *out_items_display_end = end; -} -#endif - static void ImGuiListClipper_SortAndFuseRanges(ImVector& ranges, int offset = 0) { if (ranges.Size - offset <= 1) @@ -2823,7 +2847,7 @@ void ImGuiListClipper::End() ItemsCount = -1; } -void ImGuiListClipper::IncludeRangeByIndices(int item_begin, int item_end) +void ImGuiListClipper::IncludeItemsByIndex(int item_begin, int item_end) { ImGuiListClipperData* data = (ImGuiListClipperData*)TempData; IM_ASSERT(DisplayStart < 0); // Only allowed after Begin() and if there has not been a specified range yet. @@ -2906,7 +2930,7 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) const bool is_nav_request = (g.NavMoveScoringItems && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); if (is_nav_request) data->Ranges.push_back(ImGuiListClipperRange::FromPositions(g.NavScoringNoClipRect.Min.y, g.NavScoringNoClipRect.Max.y, 0, 0)); - if (is_nav_request && (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && g.NavTabbingDir == -1) + if (is_nav_request && (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && g.NavTabbingDir == -1) data->Ranges.push_back(ImGuiListClipperRange::FromIndices(clipper->ItemsCount - 1, clipper->ItemsCount)); // Add focused/active item @@ -2924,26 +2948,28 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) // - Very important: when a starting position is after our maximum item, we set Min to (ItemsCount - 1). This allows us to handle most forms of wrapping. // - Due to how Selectable extra padding they tend to be "unaligned" with exact unit in the item list, // which with the flooring/ceiling tend to lead to 2 items instead of one being submitted. - for (int i = 0; i < data->Ranges.Size; i++) - if (data->Ranges[i].PosToIndexConvert) + for (ImGuiListClipperRange& range : data->Ranges) + if (range.PosToIndexConvert) { - int m1 = (int)(((double)data->Ranges[i].Min - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight); - int m2 = (int)((((double)data->Ranges[i].Max - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight) + 0.999999f); - data->Ranges[i].Min = ImClamp(already_submitted + m1 + data->Ranges[i].PosToIndexOffsetMin, already_submitted, clipper->ItemsCount - 1); - data->Ranges[i].Max = ImClamp(already_submitted + m2 + data->Ranges[i].PosToIndexOffsetMax, data->Ranges[i].Min + 1, clipper->ItemsCount); - data->Ranges[i].PosToIndexConvert = false; + int m1 = (int)(((double)range.Min - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight); + int m2 = (int)((((double)range.Max - window->DC.CursorPos.y - data->LossynessOffset) / clipper->ItemsHeight) + 0.999999f); + range.Min = ImClamp(already_submitted + m1 + range.PosToIndexOffsetMin, already_submitted, clipper->ItemsCount - 1); + range.Max = ImClamp(already_submitted + m2 + range.PosToIndexOffsetMax, range.Min + 1, clipper->ItemsCount); + range.PosToIndexConvert = false; } ImGuiListClipper_SortAndFuseRanges(data->Ranges, data->StepNo); } // Step 0+ (if item height is given in advance) or 1+: Display the next range in line. - if (data->StepNo < data->Ranges.Size) + while (data->StepNo < data->Ranges.Size) { clipper->DisplayStart = ImMax(data->Ranges[data->StepNo].Min, already_submitted); clipper->DisplayEnd = ImMin(data->Ranges[data->StepNo].Max, clipper->ItemsCount); if (clipper->DisplayStart > already_submitted) //-V1051 ImGuiListClipper_SeekCursorForItem(clipper, clipper->DisplayStart); data->StepNo++; + if (clipper->DisplayStart == clipper->DisplayEnd && data->StepNo < data->Ranges.Size) + continue; return true; } @@ -3028,7 +3054,8 @@ void ImGui::PushStyleColor(ImGuiCol idx, ImU32 col) backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; g.ColorStack.push_back(backup); - g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); + if (g.DebugFlashStyleColorIdx != idx) + g.Style.Colors[idx] = ColorConvertU32ToFloat4(col); } void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) @@ -3038,7 +3065,8 @@ void ImGui::PushStyleColor(ImGuiCol idx, const ImVec4& col) backup.Col = idx; backup.BackupValue = g.Style.Colors[idx]; g.ColorStack.push_back(backup); - g.Style.Colors[idx] = col; + if (g.DebugFlashStyleColorIdx != idx) + g.Style.Colors[idx] = col; } void ImGui::PopStyleColor(int count) @@ -3060,34 +3088,35 @@ void ImGui::PopStyleColor(int count) static const ImGuiDataVarInfo GStyleVarInfo[] = { - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign - { ImGuiDataType_Float, 1, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextBorderSize) },// ImGuiStyleVar_SeparatorTextBorderSize - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextAlign) }, // ImGuiStyleVar_SeparatorTextAlign - { ImGuiDataType_Float, 2, (ImU32)IM_OFFSETOF(ImGuiStyle, SeparatorTextPadding) }, // ImGuiStyleVar_SeparatorTextPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, SeparatorTextBorderSize)},// ImGuiStyleVar_SeparatorTextBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextAlign) }, // ImGuiStyleVar_SeparatorTextAlign + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextPadding) }, // ImGuiStyleVar_SeparatorTextPadding }; const ImGuiDataVarInfo* ImGui::GetStyleVarInfo(ImGuiStyleVar idx) @@ -3365,7 +3394,7 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con // Render text, render ellipsis RenderTextClippedEx(draw_list, pos_min, ImVec2(clip_max_x, pos_max.y), text, text_end_ellipsis, &text_size, ImVec2(0.0f, 0.0f)); - ImVec2 ellipsis_pos = ImFloor(ImVec2(pos_min.x + text_size_clipped_x, pos_min.y)); + ImVec2 ellipsis_pos = ImTrunc(ImVec2(pos_min.x + text_size_clipped_x, pos_min.y)); if (ellipsis_pos.x + ellipsis_width <= ellipsis_max_x) for (int i = 0; i < font->EllipsisCharCount; i++, ellipsis_pos.x += font->EllipsisCharStep * font_scale) font->RenderChar(draw_list, font_size, ellipsis_pos, GetColorU32(ImGuiCol_Text), font->EllipsisChar); @@ -3442,13 +3471,12 @@ void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCurso ImGuiContext& g = *GImGui; IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); ImFontAtlas* font_atlas = g.DrawListSharedData.Font->ContainerAtlas; - for (int n = 0; n < g.Viewports.Size; n++) + for (ImGuiViewportP* viewport : g.Viewports) { // We scale cursor with current viewport/monitor, however Windows 10 for its own hardware cursor seems to be using a different scale factor. ImVec2 offset, size, uv[4]; if (!font_atlas->GetMouseCursorTexData(mouse_cursor, &offset, &size, &uv[0], &uv[2])) continue; - ImGuiViewportP* viewport = g.Viewports[n]; const ImVec2 pos = base_pos - offset; const float scale = base_scale; if (!viewport->GetMainRect().Overlaps(ImRect(pos, pos + ImVec2(size.x + 2, size.y + 2) * scale))) @@ -3576,8 +3604,11 @@ void ImGui::Initialize() // This function is merely here to free heap allocations. void ImGui::Shutdown() { - // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) ImGuiContext& g = *GImGui; + IM_ASSERT_USER_ERROR(g.IO.BackendPlatformUserData == NULL, "Forgot to shutdown Platform backend?"); + IM_ASSERT_USER_ERROR(g.IO.BackendRendererUserData == NULL, "Forgot to shutdown Renderer backend?"); + + // The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame) if (g.IO.Fonts && g.FontAtlasOwnedByContext) { g.IO.Fonts->Locked = false; @@ -3615,6 +3646,7 @@ void ImGui::Shutdown() g.FontStack.clear(); g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); + g.NavTreeNodeStack.clear(); g.Viewports.clear_delete(); @@ -3666,9 +3698,9 @@ void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) { ImGuiContext& g = *ctx; IM_ASSERT(hook_id != 0); - for (int n = 0; n < g.Hooks.Size; n++) - if (g.Hooks[n].HookId == hook_id) - g.Hooks[n].Type = ImGuiContextHookType_PendingRemoval_; + for (ImGuiContextHook& hook : g.Hooks) + if (hook.HookId == hook_id) + hook.Type = ImGuiContextHookType_PendingRemoval_; } // Call context hooks (used by e.g. test engine) @@ -3676,9 +3708,9 @@ void ImGui::RemoveContextHook(ImGuiContext* ctx, ImGuiID hook_id) void ImGui::CallContextHooks(ImGuiContext* ctx, ImGuiContextHookType hook_type) { ImGuiContext& g = *ctx; - for (int n = 0; n < g.Hooks.Size; n++) - if (g.Hooks[n].Type == hook_type) - g.Hooks[n].Callback(&g, &g.Hooks[n]); + for (ImGuiContextHook& hook : g.Hooks) + if (hook.Type == hook_type) + hook.Callback(&g, &hook); } @@ -3899,6 +3931,8 @@ void ImGui::MarkItemEdited(ImGuiID id) // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need to fill the data. ImGuiContext& g = *GImGui; + if (g.LockMarkEdited > 0) + return; if (g.ActiveId == id || g.ActiveId == 0) { g.ActiveIdHasBeenEditedThisFrame = true; @@ -3941,13 +3975,21 @@ bool ImGui::IsWindowContentHoverable(ImGuiWindow* window, ImGuiHoveredFlags flag static inline float CalcDelayFromHoveredFlags(ImGuiHoveredFlags flags) { ImGuiContext& g = *GImGui; - if (flags & ImGuiHoveredFlags_DelayShort) - return g.Style.HoverDelayShort; if (flags & ImGuiHoveredFlags_DelayNormal) return g.Style.HoverDelayNormal; + if (flags & ImGuiHoveredFlags_DelayShort) + return g.Style.HoverDelayShort; return 0.0f; } +static ImGuiHoveredFlags ApplyHoverFlagsForTooltip(ImGuiHoveredFlags user_flags, ImGuiHoveredFlags shared_flags) +{ + // Allow instance flags to override shared flags + if (user_flags & (ImGuiHoveredFlags_DelayNone | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_DelayNormal)) + shared_flags &= ~(ImGuiHoveredFlags_DelayNone | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_DelayNormal); + return user_flags | shared_flags; +} + // This is roughly matching the behavior of internal-facing ItemHoverable() // - we allow hovering to be true when ActiveId==window->MoveID, so that clicking on non-interactive items such as a Text() item still returns true with IsItemHovered() // - this should work even for non-interactive items that have no ID, so we cannot use LastItemId @@ -3965,7 +4007,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; if (flags & ImGuiHoveredFlags_ForTooltip) - flags |= g.Style.HoverFlagsForTooltipNav; + flags = ApplyHoverFlagsForTooltip(flags, g.Style.HoverFlagsForTooltipNav); } else { @@ -3975,7 +4017,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; if (flags & ImGuiHoveredFlags_ForTooltip) - flags |= g.Style.HoverFlagsForTooltipMouse; + flags = ApplyHoverFlagsForTooltip(flags, g.Style.HoverFlagsForTooltipMouse); IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function @@ -4010,7 +4052,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) return false; // Test if using AllowOverlap and overlapped - if ((g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap) && id != 0) + if ((g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap) && id != 0) if ((flags & ImGuiHoveredFlags_AllowWhenOverlappedByItem) == 0) if (g.HoveredIdPreviousFrame != g.LastItemData.ID) return false; @@ -4042,7 +4084,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // Internal facing ItemHoverable() used when submitting widgets. Differs slightly from IsItemHovered(). // (this does not rely on LastItemData it can be called from a ButtonBehavior() call not following an ItemAdd() call) // FIXME-LEGACY: the 'ImGuiItemFlags item_flags' parameter was added on 2023-06-28. -// If you used this ii your legacy/custom widgets code: +// If you used this in your legacy/custom widgets code: // - Commonly: if your ItemHoverable() call comes after an ItemAdd() call: pass 'item_flags = g.LastItemData.InFlags'. // - Rare: otherwise you may pass 'item_flags = 0' (ImGuiItemFlags_None) unless you want to benefit from special behavior handled by ItemHoverable. bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flags) @@ -4078,7 +4120,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag // AllowOverlap mode (rarely used) requires previous frame HoveredId to be null or to match. // This allows using patterns where a later submitted widget overlaps a previous one. Generally perceived as a front-to-back hit-test. - if (item_flags & ImGuiItemflags_AllowOverlap) + if (item_flags & ImGuiItemFlags_AllowOverlap) { g.HoveredIdAllowOverlap = true; if (g.HoveredIdPreviousFrame != id) @@ -4164,20 +4206,51 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) // IM_ALLOC() == ImGui::MemAlloc() void* ImGui::MemAlloc(size_t size) { + void* ptr = (*GImAllocatorAllocFunc)(size, GImAllocatorUserData); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS if (ImGuiContext* ctx = GImGui) - ctx->IO.MetricsActiveAllocations++; - return (*GImAllocatorAllocFunc)(size, GImAllocatorUserData); + DebugAllocHook(&ctx->DebugAllocInfo, ctx->FrameCount, ptr, size); +#endif + return ptr; } // IM_FREE() == ImGui::MemFree() void ImGui::MemFree(void* ptr) { - if (ptr) +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (ptr != NULL) if (ImGuiContext* ctx = GImGui) - ctx->IO.MetricsActiveAllocations--; + DebugAllocHook(&ctx->DebugAllocInfo, ctx->FrameCount, ptr, (size_t)-1); +#endif return (*GImAllocatorFreeFunc)(ptr, GImAllocatorUserData); } +// We record the number of allocation in recent frames, as a way to audit/sanitize our guiding principles of "no allocations on idle/repeating frames" +void ImGui::DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr, size_t size) +{ + ImGuiDebugAllocEntry* entry = &info->LastEntriesBuf[info->LastEntriesIdx]; + IM_UNUSED(ptr); + if (entry->FrameCount != frame_count) + { + info->LastEntriesIdx = (info->LastEntriesIdx + 1) % IM_ARRAYSIZE(info->LastEntriesBuf); + entry = &info->LastEntriesBuf[info->LastEntriesIdx]; + entry->FrameCount = frame_count; + entry->AllocCount = entry->FreeCount = 0; + } + if (size != (size_t)-1) + { + entry->AllocCount++; + info->TotalAllocCount++; + //printf("[%05d] MemAlloc(%d) -> 0x%p\n", frame_count, size, ptr); + } + else + { + entry->FreeCount++; + info->TotalFreeCount++; + //printf("[%05d] MemFree(0x%p)\n", frame_count, ptr); + } +} + const char* ImGui::GetClipboardText() { ImGuiContext& g = *GImGui; @@ -4220,33 +4293,33 @@ int ImGui::GetFrameCount() return GImGui->FrameCount; } -static ImDrawList* GetViewportDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) +static ImDrawList* GetViewportBgFgDrawList(ImGuiViewportP* viewport, size_t drawlist_no, const char* drawlist_name) { // Create the draw list on demand, because they are not frequently used for all viewports ImGuiContext& g = *GImGui; - IM_ASSERT(drawlist_no < IM_ARRAYSIZE(viewport->DrawLists)); - ImDrawList* draw_list = viewport->DrawLists[drawlist_no]; + IM_ASSERT(drawlist_no < IM_ARRAYSIZE(viewport->BgFgDrawLists)); + ImDrawList* draw_list = viewport->BgFgDrawLists[drawlist_no]; if (draw_list == NULL) { draw_list = IM_NEW(ImDrawList)(&g.DrawListSharedData); draw_list->_OwnerName = drawlist_name; - viewport->DrawLists[drawlist_no] = draw_list; + viewport->BgFgDrawLists[drawlist_no] = draw_list; } // Our ImDrawList system requires that there is always a command - if (viewport->DrawListsLastFrame[drawlist_no] != g.FrameCount) + if (viewport->BgFgDrawListsLastFrame[drawlist_no] != g.FrameCount) { draw_list->_ResetForNewFrame(); draw_list->PushTextureID(g.IO.Fonts->TexID); draw_list->PushClipRect(viewport->Pos, viewport->Pos + viewport->Size, false); - viewport->DrawListsLastFrame[drawlist_no] = g.FrameCount; + viewport->BgFgDrawListsLastFrame[drawlist_no] = g.FrameCount; } return draw_list; } ImDrawList* ImGui::GetBackgroundDrawList(ImGuiViewport* viewport) { - return GetViewportDrawList((ImGuiViewportP*)viewport, 0, "##Background"); + return GetViewportBgFgDrawList((ImGuiViewportP*)viewport, 0, "##Background"); } ImDrawList* ImGui::GetBackgroundDrawList() @@ -4257,7 +4330,7 @@ ImDrawList* ImGui::GetBackgroundDrawList() ImDrawList* ImGui::GetForegroundDrawList(ImGuiViewport* viewport) { - return GetViewportDrawList((ImGuiViewportP*)viewport, 1, "##Foreground"); + return GetViewportBgFgDrawList((ImGuiViewportP*)viewport, 1, "##Foreground"); } ImDrawList* ImGui::GetForegroundDrawList() @@ -4455,12 +4528,11 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() } // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to Dear ImGui only, false = dispatch keyboard info to Dear ImGui + underlying app) - if (g.WantCaptureKeyboardNextFrame != -1) - io.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); - else - io.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); + io.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); if (io.NavActive && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) io.WantCaptureKeyboard = true; + if (g.WantCaptureKeyboardNextFrame != -1) // Manual override + io.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); // Update io.WantTextInput flag, this is to allow systems without a keyboard (e.g. mobile, hand-held) to show a software keyboard if possible io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; @@ -4511,8 +4583,8 @@ void ImGui::NewFrame() SetCurrentFont(GetDefaultFont()); IM_ASSERT(g.Font->IsLoaded()); ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - for (int n = 0; n < g.Viewports.Size; n++) - virtual_space.Add(g.Viewports[n]->GetMainRect()); + for (ImGuiViewportP* viewport : g.Viewports) + virtual_space.Add(viewport->GetMainRect()); g.DrawListSharedData.ClipRectFullscreen = virtual_space.ToVec4(); g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; g.DrawListSharedData.SetCircleTessellationMaxError(g.Style.CircleTessellationMaxError); @@ -4527,11 +4599,8 @@ void ImGui::NewFrame() g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; // Mark rendering data as invalid to prevent user who may have a handle on it to use it. - for (int n = 0; n < g.Viewports.Size; n++) - { - ImGuiViewportP* viewport = g.Viewports[n]; - viewport->DrawDataP.Clear(); - } + for (ImGuiViewportP* viewport : g.Viewports) + viewport->DrawDataP.Valid = false; // Drag and drop keep the source ID alive so even if the source disappear our state is consistent if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) @@ -4677,9 +4746,8 @@ void ImGui::NewFrame() // Mark all windows as not visible and compact unused memory. IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size); const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; - for (int i = 0; i != g.Windows.Size; i++) + for (ImGuiWindow* window : g.Windows) { - ImGuiWindow* window = g.Windows[i]; window->WasActive = window->Active; window->Active = false; window->WriteAccessed = false; @@ -4695,9 +4763,9 @@ void ImGui::NewFrame() for (int i = 0; i < g.TablesLastTimeActive.Size; i++) if (g.TablesLastTimeActive[i] >= 0.0f && g.TablesLastTimeActive[i] < memory_compact_start_time) TableGcCompactTransientBuffers(g.Tables.GetByIndex(i)); - for (int i = 0; i < g.TablesTempData.Size; i++) - if (g.TablesTempData[i].LastTimeActive >= 0.0f && g.TablesTempData[i].LastTimeActive < memory_compact_start_time) - TableGcCompactTransientBuffers(&g.TablesTempData[i]); + for (ImGuiTableTempData& table_temp_data : g.TablesTempData) + if (table_temp_data.LastTimeActive >= 0.0f && table_temp_data.LastTimeActive < memory_compact_start_time) + TableGcCompactTransientBuffers(&table_temp_data); if (g.GcCompactAll) GcCompactTransientMiscBuffers(); g.GcCompactAll = false; @@ -4717,12 +4785,17 @@ void ImGui::NewFrame() // [DEBUG] Update debug features UpdateDebugToolItemPicker(); UpdateDebugToolStackQueries(); + UpdateDebugToolFlashStyleColor(); if (g.DebugLocateFrames > 0 && --g.DebugLocateFrames == 0) + { g.DebugLocateId = 0; - if (g.DebugLogClipperAutoDisableFrames > 0 && --g.DebugLogClipperAutoDisableFrames == 0) + g.DebugBreakInLocateId = false; + } + if (g.DebugLogAutoDisableFrames > 0 && --g.DebugLogAutoDisableFrames == 0) { - DebugLog("(Auto-disabled ImGuiDebugLogFlags_EventClipper to avoid spamming)\n"); - g.DebugLogFlags &= ~ImGuiDebugLogFlags_EventClipper; + DebugLog("(Debug Log: Auto-disabled some ImGuiDebugLogFlags after 2 frames)\n"); + g.DebugLogFlags &= ~g.DebugLogAutoDisableFlags; + g.DebugLogAutoDisableFlags = ImGuiDebugLogFlags_None; } // Create implicit/fallback window - which we will only render it if the user has added something to it. @@ -4771,53 +4844,17 @@ static void AddWindowToSortBuffer(ImVector* out_sorted_windows, Im } } -static void AddDrawListToDrawData(ImVector* out_list, ImDrawList* draw_list) -{ - if (draw_list->CmdBuffer.Size == 0) - return; - if (draw_list->CmdBuffer.Size == 1 && draw_list->CmdBuffer[0].ElemCount == 0 && draw_list->CmdBuffer[0].UserCallback == NULL) - return; - - // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. - // May trigger for you if you are using PrimXXX functions incorrectly. - IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); - IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); - if (!(draw_list->Flags & ImDrawListFlags_AllowVtxOffset)) - IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); - - // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) - // If this assert triggers because you are drawing lots of stuff manually: - // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds. - // Be mindful that the ImDrawList API doesn't filter vertices. Use the Metrics/Debugger window to inspect draw list contents. - // - If you want large meshes with more than 64K vertices, you can either: - // (A) Handle the ImDrawCmd::VtxOffset value in your renderer backend, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. - // Most example backends already support this from 1.71. Pre-1.71 backends won't. - // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. - // (B) Or handle 32-bit indices in your renderer backend, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. - // Most example backends already support this. For example, the OpenGL example code detect index size at compile-time: - // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); - // Your own engine or render API may use different parameters or function calls to specify index sizes. - // 2 and 4 bytes indices are generally supported by most graphics API. - // - If for some reason neither of those solutions works for you, a workaround is to call BeginChild()/EndChild() before reaching - // the 64K limit to split your draw commands in multiple draw lists. - if (sizeof(ImDrawIdx) == 2) - IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above"); - - out_list->push_back(draw_list); -} - static void AddWindowToDrawData(ImGuiWindow* window, int layer) { ImGuiContext& g = *GImGui; ImGuiViewportP* viewport = g.Viewports[0]; g.IO.MetricsRenderWindows++; - AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[layer], window->DrawList); - for (int i = 0; i < window->DC.ChildWindows.Size; i++) - { - ImGuiWindow* child = window->DC.ChildWindows[i]; + if (window->DrawList->_Splitter._Count > 1) + window->DrawList->ChannelsMerge(); // Merge if user forgot to merge back. Also required in Docking branch for ImGuiWindowFlags_DockNodeHost windows. + ImGui::AddDrawListToDrawDataEx(&viewport->DrawDataP, viewport->DrawDataBuilder.Layers[layer], window->DrawList); + for (ImGuiWindow* child : window->DC.ChildWindows) if (IsWindowActiveAndVisible(child)) // Clipped children may have been marked not active AddWindowToDrawData(child, layer); - } } static inline int GetWindowDisplayLayer(ImGuiWindow* window) @@ -4831,42 +4868,41 @@ static inline void AddRootWindowToDrawData(ImGuiWindow* window) AddWindowToDrawData(window, GetWindowDisplayLayer(window)); } -void ImDrawDataBuilder::FlattenIntoSingleLayer() +static void FlattenDrawDataIntoSingleLayer(ImDrawDataBuilder* builder) { - int n = Layers[0].Size; - int size = n; - for (int i = 1; i < IM_ARRAYSIZE(Layers); i++) - size += Layers[i].Size; - Layers[0].resize(size); - for (int layer_n = 1; layer_n < IM_ARRAYSIZE(Layers); layer_n++) + int n = builder->Layers[0]->Size; + int full_size = n; + for (int i = 1; i < IM_ARRAYSIZE(builder->Layers); i++) + full_size += builder->Layers[i]->Size; + builder->Layers[0]->resize(full_size); + for (int layer_n = 1; layer_n < IM_ARRAYSIZE(builder->Layers); layer_n++) { - ImVector& layer = Layers[layer_n]; - if (layer.empty()) + ImVector* layer = builder->Layers[layer_n]; + if (layer->empty()) continue; - memcpy(&Layers[0][n], &layer[0], layer.Size * sizeof(ImDrawList*)); - n += layer.Size; - layer.resize(0); + memcpy(builder->Layers[0]->Data + n, layer->Data, layer->Size * sizeof(ImDrawList*)); + n += layer->Size; + layer->resize(0); } } -static void SetupViewportDrawData(ImGuiViewportP* viewport, ImVector* draw_lists) +static void InitViewportDrawData(ImGuiViewportP* viewport) { ImGuiIO& io = ImGui::GetIO(); ImDrawData* draw_data = &viewport->DrawDataP; + + viewport->DrawDataBuilder.Layers[0] = &draw_data->CmdLists; + viewport->DrawDataBuilder.Layers[1] = &viewport->DrawDataBuilder.LayerData1; + viewport->DrawDataBuilder.Layers[0]->resize(0); + viewport->DrawDataBuilder.Layers[1]->resize(0); + draw_data->Valid = true; - draw_data->CmdLists = (draw_lists->Size > 0) ? draw_lists->Data : NULL; - draw_data->CmdListsCount = draw_lists->Size; + draw_data->CmdListsCount = 0; draw_data->TotalVtxCount = draw_data->TotalIdxCount = 0; draw_data->DisplayPos = viewport->Pos; draw_data->DisplaySize = viewport->Size; draw_data->FramebufferScale = io.DisplayFramebufferScale; - for (int n = 0; n < draw_lists->Size; n++) - { - ImDrawList* draw_list = draw_lists->Data[n]; - draw_list->_PopUnusedDrawCmd(); - draw_data->TotalVtxCount += draw_list->VtxBuffer.Size; - draw_data->TotalIdxCount += draw_list->IdxBuffer.Size; - } + draw_data->OwnerViewport = viewport; } // Push a clipping rectangle for both ImGui logic (hit-testing etc.) and low-level ImDrawList rendering. @@ -4905,14 +4941,14 @@ static void ImGui::RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 ImDrawList* draw_list = window->RootWindow->DrawList; if (draw_list->CmdBuffer.Size == 0) draw_list->AddDrawCmd(); - draw_list->PushClipRect(viewport_rect.Min - ImVec2(1, 1), viewport_rect.Max + ImVec2(1, 1), false); // Ensure ImDrawCmd are not merged + draw_list->PushClipRect(viewport_rect.Min - ImVec2(1, 1), viewport_rect.Max + ImVec2(1, 1), false); // FIXME: Need to stricty ensure ImDrawCmd are not merged (ElemCount==6 checks below will verify that) draw_list->AddRectFilled(viewport_rect.Min, viewport_rect.Max, col); ImDrawCmd cmd = draw_list->CmdBuffer.back(); IM_ASSERT(cmd.ElemCount == 6); draw_list->CmdBuffer.pop_back(); draw_list->CmdBuffer.push_front(cmd); - draw_list->PopClipRect(); draw_list->AddDrawCmd(); // We need to create a command as CmdBuffer.back().IdxOffset won't be correct if we append to same command. + draw_list->PopClipRect(); } } @@ -5043,9 +5079,8 @@ void ImGui::EndFrame() // We cannot do that on FocusWindow() because children may not exist yet g.WindowsTempSortBuffer.resize(0); g.WindowsTempSortBuffer.reserve(g.Windows.Size); - for (int i = 0; i != g.Windows.Size; i++) + for (ImGuiWindow* window : g.Windows) { - ImGuiWindow* window = g.Windows[i]; if (window->Active && (window->Flags & ImGuiWindowFlags_ChildWindow)) // if a child is active its parent will add it continue; AddWindowToSortBuffer(&g.WindowsTempSortBuffer, window); @@ -5060,6 +5095,7 @@ void ImGui::EndFrame() g.IO.Fonts->Locked = false; // Clear Input data for next frame + g.IO.MousePosPrev = g.IO.MousePos; g.IO.AppFocusLost = false; g.IO.MouseWheel = g.IO.MouseWheelH = 0.0f; g.IO.InputQueueCharacters.resize(0); @@ -5077,32 +5113,30 @@ void ImGui::Render() if (g.FrameCountEnded != g.FrameCount) EndFrame(); - const bool first_render_of_frame = (g.FrameCountRendered != g.FrameCount); + if (g.FrameCountRendered == g.FrameCount) + return; g.FrameCountRendered = g.FrameCount; - g.IO.MetricsRenderWindows = 0; + g.IO.MetricsRenderWindows = 0; CallContextHooks(&g, ImGuiContextHookType_RenderPre); + // Draw modal/window whitening backgrounds + RenderDimmedBackgrounds(); + // Add background ImDrawList (for each active viewport) - for (int n = 0; n != g.Viewports.Size; n++) + for (ImGuiViewportP* viewport : g.Viewports) { - ImGuiViewportP* viewport = g.Viewports[n]; - viewport->DrawDataBuilder.Clear(); - if (viewport->DrawLists[0] != NULL) - AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); + InitViewportDrawData(viewport); + if (viewport->BgFgDrawLists[0] != NULL) + AddDrawListToDrawDataEx(&viewport->DrawDataP, viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); } - // Draw modal/window whitening backgrounds - if (first_render_of_frame) - RenderDimmedBackgrounds(); - // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; windows_to_render_top_most[1] = (g.NavWindowingTarget ? g.NavWindowingListWindow : NULL); - for (int n = 0; n != g.Windows.Size; n++) + for (ImGuiWindow* window : g.Windows) { - ImGuiWindow* window = g.Windows[n]; IM_MSVC_WARNING_SUPPRESS(6011); // Static Analysis false positive "warning C6011: Dereferencing NULL pointer 'window'" if (IsWindowActiveAndVisible(window) && (window->Flags & ImGuiWindowFlags_ChildWindow) == 0 && window != windows_to_render_top_most[0] && window != windows_to_render_top_most[1]) AddRootWindowToDrawData(window); @@ -5112,22 +5146,25 @@ void ImGui::Render() AddRootWindowToDrawData(windows_to_render_top_most[n]); // Draw software mouse cursor if requested by io.MouseDrawCursor flag - if (g.IO.MouseDrawCursor && first_render_of_frame && g.MouseCursor != ImGuiMouseCursor_None) + if (g.IO.MouseDrawCursor && g.MouseCursor != ImGuiMouseCursor_None) RenderMouseCursor(g.IO.MousePos, g.Style.MouseCursorScale, g.MouseCursor, IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32(0, 0, 0, 48)); // Setup ImDrawData structures for end-user g.IO.MetricsRenderVertices = g.IO.MetricsRenderIndices = 0; - for (int n = 0; n < g.Viewports.Size; n++) + for (ImGuiViewportP* viewport : g.Viewports) { - ImGuiViewportP* viewport = g.Viewports[n]; - viewport->DrawDataBuilder.FlattenIntoSingleLayer(); + FlattenDrawDataIntoSingleLayer(&viewport->DrawDataBuilder); // Add foreground ImDrawList (for each active viewport) - if (viewport->DrawLists[1] != NULL) - AddDrawListToDrawData(&viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport)); + if (viewport->BgFgDrawLists[1] != NULL) + AddDrawListToDrawDataEx(&viewport->DrawDataP, viewport->DrawDataBuilder.Layers[0], GetForegroundDrawList(viewport)); - SetupViewportDrawData(viewport, &viewport->DrawDataBuilder.Layers[0]); + // We call _PopUnusedDrawCmd() last thing, as RenderDimmedBackgrounds() rely on a valid command being there (especially in docking branch). ImDrawData* draw_data = &viewport->DrawDataP; + IM_ASSERT(draw_data->CmdLists.Size == draw_data->CmdListsCount); + for (ImDrawList* draw_list : draw_data->CmdLists) + draw_list->_PopUnusedDrawCmd(); + g.IO.MetricsRenderVertices += draw_data->TotalVtxCount; g.IO.MetricsRenderIndices += draw_data->TotalIdxCount; } @@ -5158,7 +5195,7 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex // FIXME: Investigate using ceilf or e.g. // - https://git.musl-libc.org/cgit/musl/tree/src/math/ceilf.c // - https://embarkstudios.github.io/rust-gpu/api/src/libm/math/ceilf.rs.html - text_size.x = IM_FLOOR(text_size.x + 0.99999f); + text_size.x = IM_TRUNC(text_size.x + 0.99999f); return text_size; } @@ -5188,12 +5225,8 @@ static void FindHoveredWindow() continue; // Using the clipped AABB, a child window will typically be clipped by its parent (not always) - ImRect bb(window->OuterRectClipped); - if (window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) - bb.Expand(padding_regular); - else - bb.Expand(padding_for_resize); - if (!bb.Contains(g.IO.MousePos)) + ImVec2 hit_padding = (window->Flags & (ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) ? padding_regular : padding_for_resize; + if (!window->OuterRectClipped.ContainsWithPad(g.IO.MousePos, hit_padding)) continue; // Support for one rectangular hole in any given window @@ -5278,6 +5311,9 @@ bool ImGui::IsItemToggledSelection() return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; } +// IMPORTANT: If you are trying to check whether your mouse should be dispatched to Dear ImGui or to your underlying app, +// you should not use this function! Use the 'io.WantCaptureMouse' boolean for that! +// Refer to FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" for details. bool ImGui::IsAnyItemHovered() { ImGuiContext& g = *GImGui; @@ -5314,7 +5350,7 @@ bool ImGui::IsItemEdited() void ImGui::SetNextItemAllowOverlap() { ImGuiContext& g = *GImGui; - g.NextItemData.ItemFlags |= ImGuiItemflags_AllowOverlap; + g.NextItemData.ItemFlags |= ImGuiItemFlags_AllowOverlap; } #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -5365,40 +5401,105 @@ ImVec2 ImGui::GetItemRectSize() return g.LastItemData.Rect.GetSize(); } -bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags flags) +// Prior to v1.90 2023/10/16, the BeginChild() function took a 'bool border = false' parameter instead of 'ImGuiChildFlags child_flags = 0'. +// ImGuiChildFlags_Border is defined as always == 1 in order to allow old code passing 'true'. +bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags) +{ + ImGuiID id = GetCurrentWindow()->GetID(str_id); + return BeginChildEx(str_id, id, size_arg, child_flags, window_flags); +} + +bool ImGui::BeginChild(ImGuiID id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags) +{ + return BeginChildEx(NULL, id, size_arg, child_flags, window_flags); +} + +bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags) { ImGuiContext& g = *GImGui; ImGuiWindow* parent_window = g.CurrentWindow; + IM_ASSERT(id != 0); - flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_ChildWindow; - flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag - - // Size - const ImVec2 content_avail = GetContentRegionAvail(); - ImVec2 size = ImFloor(size_arg); - const int auto_fit_axises = ((size.x == 0.0f) ? (1 << ImGuiAxis_X) : 0x00) | ((size.y == 0.0f) ? (1 << ImGuiAxis_Y) : 0x00); - if (size.x <= 0.0f) - size.x = ImMax(content_avail.x + size.x, 4.0f); // Arbitrary minimum child size (0.0f causing too many issues) - if (size.y <= 0.0f) - size.y = ImMax(content_avail.y + size.y, 4.0f); + // Sanity check as it is likely that some user will accidentally pass ImGuiWindowFlags into the ImGuiChildFlags argument. + const ImGuiChildFlags ImGuiChildFlags_SupportedMask_ = ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_FrameStyle; + IM_UNUSED(ImGuiChildFlags_SupportedMask_); + IM_ASSERT((child_flags & ~ImGuiChildFlags_SupportedMask_) == 0 && "Illegal ImGuiChildFlags value. Did you pass ImGuiWindowFlags values instead of ImGuiChildFlags?"); + IM_ASSERT((window_flags & ImGuiWindowFlags_AlwaysAutoResize) == 0 && "Cannot specify ImGuiWindowFlags_AlwaysAutoResize for BeginChild(). Use ImGuiChildFlags_AlwaysAutoResize!"); + if (child_flags & ImGuiChildFlags_AlwaysAutoResize) + { + IM_ASSERT((child_flags & (ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY)) == 0 && "Cannot use ImGuiChildFlags_ResizeX or ImGuiChildFlags_ResizeY with ImGuiChildFlags_AlwaysAutoResize!"); + IM_ASSERT((child_flags & (ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY)) != 0 && "Must use ImGuiChildFlags_AutoResizeX or ImGuiChildFlags_AutoResizeY with ImGuiChildFlags_AlwaysAutoResize!"); + } +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + if (window_flags & ImGuiWindowFlags_AlwaysUseWindowPadding) + child_flags |= ImGuiChildFlags_AlwaysUseWindowPadding; +#endif + if (child_flags & ImGuiChildFlags_AutoResizeX) + child_flags &= ~ImGuiChildFlags_ResizeX; + if (child_flags & ImGuiChildFlags_AutoResizeY) + child_flags &= ~ImGuiChildFlags_ResizeY; + + // Set window flags + window_flags |= ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_NoTitleBar; + window_flags |= (parent_window->Flags & ImGuiWindowFlags_NoMove); // Inherit the NoMove flag + if (child_flags & (ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize)) + window_flags |= ImGuiWindowFlags_AlwaysAutoResize; + if ((child_flags & (ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY)) == 0) + window_flags |= ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings; + + // Special framed style + if (child_flags & ImGuiChildFlags_FrameStyle) + { + PushStyleColor(ImGuiCol_ChildBg, g.Style.Colors[ImGuiCol_FrameBg]); + PushStyleVar(ImGuiStyleVar_ChildRounding, g.Style.FrameRounding); + PushStyleVar(ImGuiStyleVar_ChildBorderSize, g.Style.FrameBorderSize); + PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.FramePadding); + child_flags |= ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding; + window_flags |= ImGuiWindowFlags_NoMove; + } + + // Forward child flags + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasChildFlags; + g.NextWindowData.ChildFlags = child_flags; + + // Forward size + // Important: Begin() has special processing to switch condition to ImGuiCond_FirstUseEver for a given axis when ImGuiChildFlags_ResizeXXX is set. + // (the alternative would to store conditional flags per axis, which is possible but more code) + const ImVec2 size_avail = GetContentRegionAvail(); + const ImVec2 size_default((child_flags & ImGuiChildFlags_AutoResizeX) ? 0.0f : size_avail.x, (child_flags & ImGuiChildFlags_AutoResizeY) ? 0.0f : size_avail.y); + const ImVec2 size = CalcItemSize(size_arg, size_default.x, size_default.y); SetNextWindowSize(size); // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. + // FIXME: 2023/11/14: commented out shorted version. We had an issue with multiple ### in child window path names, which the trailing hash helped workaround. + // e.g. "ParentName###ParentIdentifier/ChildName###ChildIdentifier" would get hashed incorrectly by ImHashStr(), trailing _%08X somehow fixes it. const char* temp_window_name; + /*if (name && parent_window->IDStack.back() == parent_window->ID) + ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%s", parent_window->Name, name); // May omit ID if in root of ID stack + else*/ if (name) ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%s_%08X", parent_window->Name, name, id); else ImFormatStringToTempBuffer(&temp_window_name, NULL, "%s/%08X", parent_window->Name, id); + // Set style const float backup_border_size = g.Style.ChildBorderSize; - if (!border) + if ((child_flags & ImGuiChildFlags_Border) == 0) g.Style.ChildBorderSize = 0.0f; - bool ret = Begin(temp_window_name, NULL, flags); + + // Begin into window + const bool ret = Begin(temp_window_name, NULL, window_flags); + + // Restore style g.Style.ChildBorderSize = backup_border_size; + if (child_flags & ImGuiChildFlags_FrameStyle) + { + PopStyleVar(3); + PopStyleColor(); + } ImGuiWindow* child_window = g.CurrentWindow; child_window->ChildId = id; - child_window->AutoFitChildAxises = (ImS8)auto_fit_axises; // Set the cursor to handle case where the user called SetNextWindowPos()+BeginChild() manually. // While this is not really documented/defined, it seems that the expected thing to do. @@ -5406,11 +5507,11 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b parent_window->DC.CursorPos = child_window->Pos; // Process navigation-in immediately so NavInit can run on first frame - // Can enter a child if (A) it has navigatable items or (B) it can be scrolled. + // Can enter a child if (A) it has navigable items or (B) it can be scrolled. const ImGuiID temp_id_for_activation = ImHashStr("##Child", 0, id); if (g.ActiveId == temp_id_for_activation) ClearActiveID(); - if (g.NavActivateId == id && !(flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY)) + if (g.NavActivateId == id && !(window_flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY)) { FocusWindow(child_window); NavInitWindow(child_window, false); @@ -5420,50 +5521,29 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, b return ret; } -bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) -{ - ImGuiWindow* window = GetCurrentWindow(); - return BeginChildEx(str_id, window->GetID(str_id), size_arg, border, extra_flags); -} - -bool ImGui::BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags extra_flags) -{ - IM_ASSERT(id != 0); - return BeginChildEx(NULL, id, size_arg, border, extra_flags); -} - void ImGui::EndChild() { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; + ImGuiWindow* child_window = g.CurrentWindow; IM_ASSERT(g.WithinEndChild == false); - IM_ASSERT(window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() calls + IM_ASSERT(child_window->Flags & ImGuiWindowFlags_ChildWindow); // Mismatched BeginChild()/EndChild() calls g.WithinEndChild = true; - if (window->BeginCount > 1) - { - End(); - } - else + ImVec2 child_size = child_window->Size; + End(); + if (child_window->BeginCount == 1) { - ImVec2 sz = window->Size; - if (window->AutoFitChildAxises & (1 << ImGuiAxis_X)) // Arbitrary minimum zero-ish child size of 4.0f causes less trouble than a 0.0f - sz.x = ImMax(4.0f, sz.x); - if (window->AutoFitChildAxises & (1 << ImGuiAxis_Y)) - sz.y = ImMax(4.0f, sz.y); - End(); - ImGuiWindow* parent_window = g.CurrentWindow; - ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + sz); - ItemSize(sz); - if ((window->DC.NavLayersActiveMask != 0 || window->DC.NavWindowHasScrollY) && !(window->Flags & ImGuiWindowFlags_NavFlattened)) + ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + child_size); + ItemSize(child_size); + if ((child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY) && !(child_window->Flags & ImGuiWindowFlags_NavFlattened)) { - ItemAdd(bb, window->ChildId); - RenderNavHighlight(bb, window->ChildId); + ItemAdd(bb, child_window->ChildId); + RenderNavHighlight(bb, child_window->ChildId); // When browsing a window that has no activable items (scroll only) we keep a highlight on the child (pass g.NavId to trick into always displaying) - if (window->DC.NavLayersActiveMask == 0 && window == g.NavWindow) + if (child_window->DC.NavLayersActiveMask == 0 && child_window == g.NavWindow) RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); } else @@ -5472,36 +5552,16 @@ void ImGui::EndChild() ItemAdd(bb, 0); // But when flattened we directly reach items, adjust active layer mask accordingly - if (window->Flags & ImGuiWindowFlags_NavFlattened) - parent_window->DC.NavLayersActiveMaskNext |= window->DC.NavLayersActiveMaskNext; + if (child_window->Flags & ImGuiWindowFlags_NavFlattened) + parent_window->DC.NavLayersActiveMaskNext |= child_window->DC.NavLayersActiveMaskNext; } - if (g.HoveredWindow == window) + if (g.HoveredWindow == child_window) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredWindow; } g.WithinEndChild = false; g.LogLinePosY = -FLT_MAX; // To enforce a carriage return } -// Helper to create a child window / scrolling region that looks like a normal widget frame. -bool ImGui::BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags extra_flags) -{ - ImGuiContext& g = *GImGui; - const ImGuiStyle& style = g.Style; - PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); - PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); - PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); - PushStyleVar(ImGuiStyleVar_WindowPadding, style.FramePadding); - bool ret = BeginChild(id, size, true, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysUseWindowPadding | extra_flags); - PopStyleVar(3); - PopStyleColor(); - return ret; -} - -void ImGui::EndChildFrame() -{ - EndChild(); -} - static void SetWindowConditionAllowFlags(ImGuiWindow* window, ImGuiCond flags, bool enabled) { window->SetWindowPosAllowFlags = enabled ? (window->SetWindowPosAllowFlags | flags) : (window->SetWindowPosAllowFlags & ~flags); @@ -5523,9 +5583,9 @@ ImGuiWindow* ImGui::FindWindowByName(const char* name) static void ApplyWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* settings) { - window->Pos = ImFloor(ImVec2(settings->Pos.x, settings->Pos.y)); + window->Pos = ImTrunc(ImVec2(settings->Pos.x, settings->Pos.y)); if (settings->Size.x > 0 && settings->Size.y > 0) - window->Size = window->SizeFull = ImFloor(ImVec2(settings->Size.x, settings->Size.y)); + window->Size = window->SizeFull = ImTrunc(ImVec2(settings->Size.x, settings->Size.y)); window->Collapsed = settings->Collapsed; } @@ -5558,6 +5618,7 @@ static void InitOrLoadWindowSettings(ImGuiWindow* window, ImGuiWindowSettings* s // Use SetNextWindowPos() with the appropriate condition flag to change the initial position of a window. const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); window->Pos = main_viewport->Pos + ImVec2(60, 60); + window->Size = window->SizeFull = ImVec2(0, 0); window->SetWindowPosAllowFlags = window->SetWindowSizeAllowFlags = window->SetWindowCollapsedAllowFlags = ImGuiCond_Always | ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing; if (settings != NULL) @@ -5606,13 +5667,34 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) return window; } +static inline ImVec2 CalcWindowMinSize(ImGuiWindow* window) +{ + // Popups, menus and childs bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) + // FIXME: the if/else could probably be removed, "reduce artifacts" section for all windows. + ImGuiContext& g = *GImGui; + ImVec2 size_min; + if (window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_ChildWindow)) + { + size_min.x = (window->ChildFlags & ImGuiChildFlags_ResizeX) ? g.Style.WindowMinSize.x : 4.0f; + size_min.y = (window->ChildFlags & ImGuiChildFlags_ResizeY) ? g.Style.WindowMinSize.y : 4.0f; + } + else + { + ImGuiWindow* window_for_height = window; + size_min.x = ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) == 0) ? g.Style.WindowMinSize.x : 4.0f; + size_min.y = ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) == 0) ? g.Style.WindowMinSize.y : 4.0f; + size_min.y = ImMax(size_min.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows + } + return size_min; +} + static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, const ImVec2& size_desired) { ImGuiContext& g = *GImGui; ImVec2 new_size = size_desired; if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint) { - // Using -1,-1 on either X/Y axis to preserve the current size. + // See comments in SetNextWindowSizeConstraints() for details about setting size_min an size_max. ImRect cr = g.NextWindowData.SizeConstraintRect; new_size.x = (cr.Min.x >= 0 && cr.Max.x >= 0) ? ImClamp(new_size.x, cr.Min.x, cr.Max.x) : window->SizeFull.x; new_size.y = (cr.Min.y >= 0 && cr.Max.y >= 0) ? ImClamp(new_size.y, cr.Min.y, cr.Max.y) : window->SizeFull.y; @@ -5626,19 +5708,13 @@ static ImVec2 CalcWindowSizeAfterConstraint(ImGuiWindow* window, const ImVec2& s g.NextWindowData.SizeCallback(&data); new_size = data.DesiredSize; } - new_size.x = IM_FLOOR(new_size.x); - new_size.y = IM_FLOOR(new_size.y); + new_size.x = IM_TRUNC(new_size.x); + new_size.y = IM_TRUNC(new_size.y); } // Minimum size - if (!(window->Flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_AlwaysAutoResize))) - { - ImGuiWindow* window_for_height = window; - new_size = ImMax(new_size, g.Style.WindowMinSize); - const float minimum_height = window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f); - new_size.y = ImMax(new_size.y, minimum_height); // Reduce artifacts with very small windows - } - return new_size; + ImVec2 size_min = CalcWindowMinSize(window); + return ImMax(new_size, size_min); } static void CalcWindowContentSizes(ImGuiWindow* window, ImVec2* content_size_current, ImVec2* content_size_ideal) @@ -5655,10 +5731,10 @@ static void CalcWindowContentSizes(ImGuiWindow* window, ImVec2* content_size_cur return; } - content_size_current->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_FLOOR(window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); - content_size_current->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_FLOOR(window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); - content_size_ideal->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_FLOOR(ImMax(window->DC.CursorMaxPos.x, window->DC.IdealMaxPos.x) - window->DC.CursorStartPos.x); - content_size_ideal->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_FLOOR(ImMax(window->DC.CursorMaxPos.y, window->DC.IdealMaxPos.y) - window->DC.CursorStartPos.y); + content_size_current->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_TRUNC(window->DC.CursorMaxPos.x - window->DC.CursorStartPos.x); + content_size_current->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_TRUNC(window->DC.CursorMaxPos.y - window->DC.CursorStartPos.y); + content_size_ideal->x = (window->ContentSizeExplicit.x != 0.0f) ? window->ContentSizeExplicit.x : IM_TRUNC(ImMax(window->DC.CursorMaxPos.x, window->DC.IdealMaxPos.x) - window->DC.CursorStartPos.x); + content_size_ideal->y = (window->ContentSizeExplicit.y != 0.0f) ? window->ContentSizeExplicit.y : IM_TRUNC(ImMax(window->DC.CursorMaxPos.y, window->DC.IdealMaxPos.y) - window->DC.CursorStartPos.y); } static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_contents) @@ -5677,14 +5753,9 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont else { // Maximum window size is determined by the viewport size or monitor size - const bool is_popup = (window->Flags & ImGuiWindowFlags_Popup) != 0; - const bool is_menu = (window->Flags & ImGuiWindowFlags_ChildMenu) != 0; - ImVec2 size_min = style.WindowMinSize; - if (is_popup || is_menu) // Popups and menus bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) - size_min = ImMin(size_min, ImVec2(4.0f, 4.0f)); - - ImVec2 avail_size = ImGui::GetMainViewport()->WorkSize; - ImVec2 size_auto_fit = ImClamp(size_desired, size_min, ImMax(size_min, avail_size - style.DisplaySafeAreaPadding * 2.0f)); + ImVec2 size_min = CalcWindowMinSize(window); + ImVec2 size_max = (window->Flags & ImGuiWindowFlags_ChildWindow) ? ImVec2(FLT_MAX, FLT_MAX) : ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f; + ImVec2 size_auto_fit = ImClamp(size_desired, size_min, size_max); // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. @@ -5732,7 +5803,7 @@ static void CalcResizePosSizeFromAnyCorner(ImGuiWindow* window, const ImVec2& co *out_size = size_constrained; } -// Data for resizing from corner +// Data for resizing from resize grip / corner struct ImGuiResizeGripDef { ImVec2 CornerPosN; @@ -5750,9 +5821,9 @@ static const ImGuiResizeGripDef resize_grip_def[4] = // Data for resizing from borders struct ImGuiResizeBorderDef { - ImVec2 InnerDir; - ImVec2 SegmentN1, SegmentN2; - float OuterAngle; + ImVec2 InnerDir; // Normal toward inside + ImVec2 SegmentN1, SegmentN2; // End positions, normalized (0,0: upper left) + float OuterAngle; // Angle toward outside }; static const ImGuiResizeBorderDef resize_border_def[4] = { @@ -5798,7 +5869,7 @@ ImGuiID ImGui::GetWindowResizeBorderID(ImGuiWindow* window, ImGuiDir dir) // Handle resize for: Resize Grips, Borders, Gamepad // Return true when using auto-fit (double-click on resize grip) -static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect) +static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& size_auto_fit, int* border_hovered, int* border_held, int resize_grip_count, ImU32 resize_grip_col[4], const ImRect& visibility_rect) { ImGuiContext& g = *GImGui; ImGuiWindowFlags flags = window->Flags; @@ -5808,10 +5879,9 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (window->WasActive == false) // Early out to avoid running this code for e.g. a hidden implicit/fallback Debug window. return false; - bool ret_auto_fit = false; - const int resize_border_count = g.IO.ConfigWindowsResizeFromEdges ? 4 : 0; - const float grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); - const float grip_hover_inner_size = IM_FLOOR(grip_draw_size * 0.75f); + int ret_auto_fit_mask = 0x00; + const float grip_draw_size = IM_TRUNC(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); + const float grip_hover_inner_size = IM_TRUNC(grip_draw_size * 0.75f); const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_HOVER_PADDING : 0.0f; ImRect clamp_rect = visibility_rect; @@ -5844,11 +5914,11 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (hovered || held) g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; - if (held && g.IO.MouseClickedCount[0] == 2 && resize_grip_n == 0) + if (held && g.IO.MouseDoubleClicked[0]) { - // Manual auto-fit when double-clicking + // Auto-fit when double-clicking size_target = CalcWindowSizeAfterConstraint(window, size_auto_fit); - ret_auto_fit = true; + ret_auto_fit_mask = 0x03; // Both axises ClearActiveID(); } else if (held) @@ -5866,8 +5936,16 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s if (resize_grip_n == 0 || held || hovered) resize_grip_col[resize_grip_n] = GetColorU32(held ? ImGuiCol_ResizeGripActive : hovered ? ImGuiCol_ResizeGripHovered : ImGuiCol_ResizeGrip); } - for (int border_n = 0; border_n < resize_border_count; border_n++) + + int resize_border_mask = 0x00; + if (window->Flags & ImGuiWindowFlags_ChildWindow) + resize_border_mask |= ((window->ChildFlags & ImGuiChildFlags_ResizeX) ? 0x02 : 0) | ((window->ChildFlags & ImGuiChildFlags_ResizeY) ? 0x08 : 0); + else + resize_border_mask = g.IO.ConfigWindowsResizeFromEdges ? 0x0F : 0x00; + for (int border_n = 0; border_n < 4; border_n++) { + if ((resize_border_mask & (1 << border_n)) == 0) + continue; const ImGuiResizeBorderDef& def = resize_border_def[border_n]; const ImGuiAxis axis = (border_n == ImGuiDir_Left || border_n == ImGuiDir_Right) ? ImGuiAxis_X : ImGuiAxis_Y; @@ -5876,22 +5954,73 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s ImGuiID border_id = window->GetID(border_n + 4); // == GetWindowResizeBorderID() ItemAdd(border_rect, border_id, NULL, ImGuiItemFlags_NoNav); ButtonBehavior(border_rect, border_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); - //GetForegroundDrawLists(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); - if ((hovered && g.HoveredIdTimer > WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) || held) - { + //GetForegroundDrawList(window)->AddRect(border_rect.Min, border_rect.Max, IM_COL32(255, 255, 0, 255)); + if (hovered && g.HoveredIdTimer <= WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) + hovered = false; + if (hovered || held) g.MouseCursor = (axis == ImGuiAxis_X) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; - if (held) - *border_held = border_n; + if (held && g.IO.MouseDoubleClicked[0]) + { + // Double-clicking bottom or right border auto-fit on this axis + // FIXME: Support top and right borders: rework CalcResizePosSizeFromAnyCorner() to be reusable in both cases. + if (border_n == 1 || border_n == 3) // Right and bottom border + { + size_target[axis] = CalcWindowSizeAfterConstraint(window, size_auto_fit)[axis]; + ret_auto_fit_mask |= (1 << axis); + hovered = held = false; // So border doesn't show highlighted at new position + } + ClearActiveID(); } - if (held) + else if (held) { + // Switch to relative resizing mode when border geometry moved (e.g. resizing a child altering parent scroll), in order to avoid resizing feedback loop. + // Currently only using relative mode on resizable child windows, as the problem to solve is more likely noticeable for them, but could apply for all windows eventually. + // FIXME: May want to generalize this idiom at lower-level, so more widgets can use it! + const bool just_scrolled_manually_while_resizing = (g.WheelingWindow != NULL && g.WheelingWindowScrolledFrame == g.FrameCount && IsWindowChildOf(window, g.WheelingWindow, false)); + if (g.ActiveIdIsJustActivated || just_scrolled_manually_while_resizing) + { + g.WindowResizeBorderExpectedRect = border_rect; + g.WindowResizeRelativeMode = false; + } + if ((window->Flags & ImGuiWindowFlags_ChildWindow) && memcmp(&g.WindowResizeBorderExpectedRect, &border_rect, sizeof(ImRect)) != 0) + g.WindowResizeRelativeMode = true; + + const ImVec2 border_curr = (window->Pos + ImMin(def.SegmentN1, def.SegmentN2) * window->Size); + const float border_target_rel_mode_for_axis = border_curr[axis] + g.IO.MouseDelta[axis]; + const float border_target_abs_mode_for_axis = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING; // Match ButtonBehavior() padding above. + + // Use absolute mode position + ImVec2 border_target = window->Pos; + border_target[axis] = border_target_abs_mode_for_axis; + + // Use relative mode target for child window, ignore resize when moving back toward the ideal absolute position. + bool ignore_resize = false; + if (g.WindowResizeRelativeMode) + { + //GetForegroundDrawList()->AddText(GetMainViewport()->WorkPos, IM_COL32_WHITE, "Relative Mode"); + border_target[axis] = border_target_rel_mode_for_axis; + if (g.IO.MouseDelta[axis] == 0.0f || (g.IO.MouseDelta[axis] > 0.0f) == (border_target_rel_mode_for_axis > border_target_abs_mode_for_axis)) + ignore_resize = true; + } + + // Clamp, apply ImVec2 clamp_min(border_n == ImGuiDir_Right ? clamp_rect.Min.x : -FLT_MAX, border_n == ImGuiDir_Down || (border_n == ImGuiDir_Up && window_move_from_title_bar) ? clamp_rect.Min.y : -FLT_MAX); ImVec2 clamp_max(border_n == ImGuiDir_Left ? clamp_rect.Max.x : +FLT_MAX, border_n == ImGuiDir_Up ? clamp_rect.Max.y : +FLT_MAX); - ImVec2 border_target = window->Pos; - border_target[axis] = g.IO.MousePos[axis] - g.ActiveIdClickOffset[axis] + WINDOWS_HOVER_PADDING; border_target = ImClamp(border_target, clamp_min, clamp_max); - CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target); + if (flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent + { + if ((flags & (ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar)) == 0 || (flags & ImGuiWindowFlags_NoScrollbar)) + border_target.x = ImClamp(border_target.x, window->ParentWindow->InnerClipRect.Min.x, window->ParentWindow->InnerClipRect.Max.x); + if (flags & ImGuiWindowFlags_NoScrollbar) + border_target.y = ImClamp(border_target.y, window->ParentWindow->InnerClipRect.Min.y, window->ParentWindow->InnerClipRect.Max.y); + } + if (!ignore_resize) + CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target); } + if (hovered) + *border_hovered = border_n; + if (held) + *border_held = border_n; } PopID(); @@ -5917,7 +6046,7 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s g.NavWindowingToggleLayer = false; g.NavDisableMouseHover = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); - ImVec2 accum_floored = ImFloor(g.NavWindowingAccumDeltaSize); + ImVec2 accum_floored = ImTrunc(g.NavWindowingAccumDeltaSize); if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) { // FIXME-NAV: Should store and accumulate into a separate size buffer to handle sizing constraints properly, right now a constraint will make us stuck. @@ -5928,19 +6057,24 @@ static bool ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& s } // Apply back modified position/size to window - if (size_target.x != FLT_MAX) - { - window->SizeFull = size_target; + const ImVec2 curr_pos = window->Pos; + const ImVec2 curr_size = window->SizeFull; + if (size_target.x != FLT_MAX && (window->Size.x != size_target.x || window->SizeFull.x != size_target.x)) + window->Size.x = window->SizeFull.x = size_target.x; + if (size_target.y != FLT_MAX && (window->Size.y != size_target.y || window->SizeFull.y != size_target.y)) + window->Size.y = window->SizeFull.y = size_target.y; + if (pos_target.x != FLT_MAX && window->Pos.x != ImTrunc(pos_target.x)) + window->Pos.x = ImTrunc(pos_target.x); + if (pos_target.y != FLT_MAX && window->Pos.y != ImTrunc(pos_target.y)) + window->Pos.y = ImTrunc(pos_target.y); + if (curr_pos.x != window->Pos.x || curr_pos.y != window->Pos.y || curr_size.x != window->SizeFull.x || curr_size.y != window->SizeFull.y) MarkIniSettingsDirty(window); - } - if (pos_target.x != FLT_MAX) - { - window->Pos = ImFloor(pos_target); - MarkIniSettingsDirty(window); - } - window->Size = window->SizeFull; - return ret_auto_fit; + // Recalculate next expected border expected coordinates + if (*border_held != -1) + g.WindowResizeBorderExpectedRect = GetResizeBorderRect(window, *border_held, grip_hover_inner_size, WINDOWS_HOVER_PADDING); + + return ret_auto_fit_mask; } static inline void ClampWindowPos(ImGuiWindow* window, const ImRect& visibility_rect) @@ -5952,27 +6086,40 @@ static inline void ClampWindowPos(ImGuiWindow* window, const ImRect& visibility_ window->Pos = ImClamp(window->Pos, visibility_rect.Min - size_for_clamping, visibility_rect.Max); } +static void RenderWindowOuterSingleBorder(ImGuiWindow* window, int border_n, ImU32 border_col, float border_size) +{ + const ImGuiResizeBorderDef& def = resize_border_def[border_n]; + const float rounding = window->WindowRounding; + const ImRect border_r = GetResizeBorderRect(window, border_n, rounding, 0.0f); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); + window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); + window->DrawList->PathStroke(border_col, ImDrawFlags_None, border_size); +} + static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) { ImGuiContext& g = *GImGui; - float rounding = window->WindowRounding; - float border_size = window->WindowBorderSize; - if (border_size > 0.0f && !(window->Flags & ImGuiWindowFlags_NoBackground)) - window->DrawList->AddRect(window->Pos, window->Pos + window->Size, GetColorU32(ImGuiCol_Border), rounding, 0, border_size); - - int border_held = window->ResizeBorderHeld; - if (border_held != -1) + const float border_size = window->WindowBorderSize; + const ImU32 border_col = GetColorU32(ImGuiCol_Border); + if (border_size > 0.0f && (window->Flags & ImGuiWindowFlags_NoBackground) == 0) + window->DrawList->AddRect(window->Pos, window->Pos + window->Size, border_col, window->WindowRounding, 0, window->WindowBorderSize); + else if (border_size > 0.0f) + { + if (window->ChildFlags & ImGuiChildFlags_ResizeX) // Similar code as 'resize_border_mask' computation in UpdateWindowManualResize() but we specifically only always draw explicit child resize border. + RenderWindowOuterSingleBorder(window, 1, border_col, border_size); + if (window->ChildFlags & ImGuiChildFlags_ResizeY) + RenderWindowOuterSingleBorder(window, 3, border_col, border_size); + } + if (window->ResizeBorderHovered != -1 || window->ResizeBorderHeld != -1) { - const ImGuiResizeBorderDef& def = resize_border_def[border_held]; - ImRect border_r = GetResizeBorderRect(window, border_held, rounding, 0.0f); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN1) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle - IM_PI * 0.25f, def.OuterAngle); - window->DrawList->PathArcTo(ImLerp(border_r.Min, border_r.Max, def.SegmentN2) + ImVec2(0.5f, 0.5f) + def.InnerDir * rounding, rounding, def.OuterAngle, def.OuterAngle + IM_PI * 0.25f); - window->DrawList->PathStroke(GetColorU32(ImGuiCol_SeparatorActive), 0, ImMax(2.0f, border_size)); // Thicker than usual + const int border_n = (window->ResizeBorderHeld != -1) ? window->ResizeBorderHeld : window->ResizeBorderHovered; + const ImU32 border_col_resizing = GetColorU32((window->ResizeBorderHeld != -1) ? ImGuiCol_SeparatorActive : ImGuiCol_SeparatorHovered); + RenderWindowOuterSingleBorder(window, border_n, border_col_resizing, ImMax(2.0f, window->WindowBorderSize)); // Thicker than usual } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) { float y = window->Pos.y + window->TitleBarHeight() - 1; - window->DrawList->AddLine(ImVec2(window->Pos.x + border_size, y), ImVec2(window->Pos.x + window->Size.x - border_size, y), GetColorU32(ImGuiCol_Border), g.Style.FrameBorderSize); + window->DrawList->AddLine(ImVec2(window->Pos.x + border_size, y), ImVec2(window->Pos.x + window->Size.x - border_size, y), border_col, g.Style.FrameBorderSize); } } @@ -6090,18 +6237,18 @@ void ImGui::RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& titl ImVec2 collapse_button_pos; if (has_close_button) { - pad_r += button_sz; - close_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - style.FramePadding.x, title_bar_rect.Min.y); + close_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - button_sz, title_bar_rect.Min.y + style.FramePadding.y); + pad_r += button_sz + style.ItemInnerSpacing.x; } if (has_collapse_button && style.WindowMenuButtonPosition == ImGuiDir_Right) { - pad_r += button_sz; - collapse_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - style.FramePadding.x, title_bar_rect.Min.y); + collapse_button_pos = ImVec2(title_bar_rect.Max.x - pad_r - button_sz, title_bar_rect.Min.y + style.FramePadding.y); + pad_r += button_sz + style.ItemInnerSpacing.x; } if (has_collapse_button && style.WindowMenuButtonPosition == ImGuiDir_Left) { - collapse_button_pos = ImVec2(title_bar_rect.Min.x + pad_l - style.FramePadding.x, title_bar_rect.Min.y); - pad_l += button_sz; + collapse_button_pos = ImVec2(title_bar_rect.Min.x + pad_l, title_bar_rect.Min.y + style.FramePadding.y); + pad_l += button_sz + style.ItemInnerSpacing.x; } // Collapse button (submitting first so it gets priority when choosing a navigation init fallback) @@ -6191,9 +6338,9 @@ ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) return NULL; // Find a modal that has common parent with specified window. Specified window should be positioned behind that modal. - for (int i = 0; i < g.OpenPopupStack.Size; i++) + for (ImGuiPopupData& popup_data : g.OpenPopupStack) { - ImGuiWindow* popup_window = g.OpenPopupStack.Data[i].Window; + ImGuiWindow* popup_window = popup_data.Window; if (popup_window == NULL || !(popup_window->Flags & ImGuiWindowFlags_Modal)) continue; if (!popup_window->Active && !popup_window->WasActive) // Check WasActive, because this code may run before popup renders on current frame, also check Active to handle newly created windows. @@ -6228,6 +6375,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window_just_created) window = CreateNewWindow(name, flags); + // [DEBUG] Debug break requested by user + if (g.DebugBreakInWindow == window->ID) + IM_DEBUG_BREAK(); + // Automatically disable manual moving/resizing when NoInputs is set if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; @@ -6256,6 +6407,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { UpdateWindowInFocusOrderList(window, window_just_created, flags); window->Flags = (ImGuiWindowFlags)flags; + window->ChildFlags = (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasChildFlags) ? g.NextWindowData.ChildFlags : 0; window->LastFrameActive = current_frame; window->LastTimeActive = (float)g.Time; window->BeginOrderWithinParent = 0; @@ -6276,7 +6428,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->IDStack.push_back(window->ID); // Add to stack - // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() g.CurrentWindow = window; ImGuiWindowStackData window_stack_data; window_stack_data.Window = window; @@ -6294,7 +6445,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } // Add to focus scope stack - PushFocusScope(window->ID); + // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() + if ((flags & ImGuiWindowFlags_NavFlattened) == 0) + PushFocusScope(window->ID); window->NavRootFocusScopeId = g.CurrentFocusScopeId; g.CurrentWindow = NULL; @@ -6332,6 +6485,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { window_size_x_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.x > 0.0f); window_size_y_set_by_api = (window->SetWindowSizeAllowFlags & g.NextWindowData.SizeCond) != 0 && (g.NextWindowData.SizeVal.y > 0.0f); + if ((window->ChildFlags & ImGuiChildFlags_ResizeX) && (window->SetWindowSizeAllowFlags & ImGuiCond_FirstUseEver) == 0) // Axis-specific conditions for BeginChild() + g.NextWindowData.SizeVal.x = window->SizeFull.x; + if ((window->ChildFlags & ImGuiChildFlags_ResizeY) && (window->SetWindowSizeAllowFlags & ImGuiCond_FirstUseEver) == 0) + g.NextWindowData.SizeVal.y = window->SizeFull.y; SetWindowSize(window, g.NextWindowData.SizeVal, g.NextWindowData.SizeCond); } if (g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasScroll) @@ -6431,7 +6588,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) else window->WindowBorderSize = ((flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_Tooltip)) && !(flags & ImGuiWindowFlags_Modal)) ? style.PopupBorderSize : style.WindowBorderSize; window->WindowPadding = style.WindowPadding; - if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & (ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_Popup)) && window->WindowBorderSize == 0.0f) + if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup) && !(window->ChildFlags & ImGuiChildFlags_AlwaysUseWindowPadding) && window->WindowBorderSize == 0.0f) window->WindowPadding = ImVec2(0.0f, (flags & ImGuiWindowFlags_MenuBar) ? style.WindowPadding.y : 0.0f); // Lock menu offset so size calculation can use it as menu-bar windows need a minimum size. @@ -6554,7 +6711,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (!window_pos_set_by_api && !(flags & ImGuiWindowFlags_ChildWindow)) if (viewport_rect.GetWidth() > 0.0f && viewport_rect.GetHeight() > 0.0f) ClampWindowPos(window, visibility_rect); - window->Pos = ImFloor(window->Pos); + window->Pos = ImTrunc(window->Pos); // Lock window rounding for the frame (so that altering them doesn't cause inconsistencies) // Large values tend to lead to variety of artifacts and are not recommended. @@ -6587,13 +6744,19 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) #endif // Handle manual resize: Resize Grips, Borders, Gamepad - int border_held = -1; + int border_hovered = -1, border_held = -1; ImU32 resize_grip_col[4] = {}; - const int resize_grip_count = g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. - const float resize_grip_draw_size = IM_FLOOR(ImMax(g.FontSize * 1.10f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); + const int resize_grip_count = (window->Flags & ImGuiWindowFlags_ChildWindow) ? 0 : g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. + const float resize_grip_draw_size = IM_TRUNC(ImMax(g.FontSize * 1.10f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); if (!window->Collapsed) - if (UpdateWindowManualResize(window, size_auto_fit, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect)) - use_current_size_for_scrollbar_x = use_current_size_for_scrollbar_y = true; + if (int auto_fit_mask = UpdateWindowManualResize(window, size_auto_fit, &border_hovered, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect)) + { + if (auto_fit_mask & (1 << ImGuiAxis_X)) + use_current_size_for_scrollbar_x = true; + if (auto_fit_mask & (1 << ImGuiAxis_Y)) + use_current_size_for_scrollbar_y = true; + } + window->ResizeBorderHovered = (signed char)border_hovered; window->ResizeBorderHeld = (signed char)border_held; // SCROLLBAR VISIBILITY @@ -6655,17 +6818,17 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Affected by window/frame border size. Used by: // - Begin() initial clip rect float top_border_size = (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize); - window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + ImMax(ImFloor(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); - window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + top_border_size); - window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerRect.Max.x - ImMax(ImFloor(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); - window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); + window->InnerClipRect.Min.x = ImTrunc(0.5f + window->InnerRect.Min.x + ImMax(ImTrunc(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); + window->InnerClipRect.Min.y = ImTrunc(0.5f + window->InnerRect.Min.y + top_border_size); + window->InnerClipRect.Max.x = ImTrunc(0.5f + window->InnerRect.Max.x - ImMax(ImTrunc(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); + window->InnerClipRect.Max.y = ImTrunc(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); window->InnerClipRect.ClipWithFull(host_rect); // Default item width. Make it proportional to window size if window manually resizes if (window->Size.x > 0.0f && !(flags & ImGuiWindowFlags_Tooltip) && !(flags & ImGuiWindowFlags_AlwaysAutoResize)) - window->ItemWidthDefault = ImFloor(window->Size.x * 0.65f); + window->ItemWidthDefault = ImTrunc(window->Size.x * 0.65f); else - window->ItemWidthDefault = ImFloor(g.FontSize * 16.0f); + window->ItemWidthDefault = ImTrunc(g.FontSize * 16.0f); // SCROLLING @@ -6726,14 +6889,15 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) const bool allow_scrollbar_y = !(flags & ImGuiWindowFlags_NoScrollbar); const float work_rect_size_x = (window->ContentSizeExplicit.x != 0.0f ? window->ContentSizeExplicit.x : ImMax(allow_scrollbar_x ? window->ContentSize.x : 0.0f, window->Size.x - window->WindowPadding.x * 2.0f - (window->DecoOuterSizeX1 + window->DecoOuterSizeX2))); const float work_rect_size_y = (window->ContentSizeExplicit.y != 0.0f ? window->ContentSizeExplicit.y : ImMax(allow_scrollbar_y ? window->ContentSize.y : 0.0f, window->Size.y - window->WindowPadding.y * 2.0f - (window->DecoOuterSizeY1 + window->DecoOuterSizeY2))); - window->WorkRect.Min.x = ImFloor(window->InnerRect.Min.x - window->Scroll.x + ImMax(window->WindowPadding.x, window->WindowBorderSize)); - window->WorkRect.Min.y = ImFloor(window->InnerRect.Min.y - window->Scroll.y + ImMax(window->WindowPadding.y, window->WindowBorderSize)); + window->WorkRect.Min.x = ImTrunc(window->InnerRect.Min.x - window->Scroll.x + ImMax(window->WindowPadding.x, window->WindowBorderSize)); + window->WorkRect.Min.y = ImTrunc(window->InnerRect.Min.y - window->Scroll.y + ImMax(window->WindowPadding.y, window->WindowBorderSize)); window->WorkRect.Max.x = window->WorkRect.Min.x + work_rect_size_x; window->WorkRect.Max.y = window->WorkRect.Min.y + work_rect_size_y; window->ParentWorkRect = window->WorkRect; // [LEGACY] Content Region // FIXME-OBSOLETE: window->ContentRegionRect.Max is currently very misleading / partly faulty, but some BeginChild() patterns relies on it. + // Unless explicit content size is specified by user, this currently represent the region leading to no scrolling. // Used by: // - Mouse wheel scrolling + many other things window->ContentRegionRect.Min.x = window->Pos.x - window->Scroll.x + window->WindowPadding.x + window->DecoOuterSizeX1; @@ -6846,18 +7010,20 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Update visibility if (first_begin_of_the_frame) { - if (flags & ImGuiWindowFlags_ChildWindow) + if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_ChildMenu)) { // Child window can be out of sight and have "negative" clip windows. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); - if (!(flags & ImGuiWindowFlags_AlwaysAutoResize) && window->AutoFitFramesX <= 0 && window->AutoFitFramesY <= 0) // FIXME: Doesn't make sense for ChildWindow?? - { - const bool nav_request = (flags & ImGuiWindowFlags_NavFlattened) && (g.NavAnyRequest && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); - if (!g.LogEnabled && !nav_request) - if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + const bool nav_request = (flags & ImGuiWindowFlags_NavFlattened) && (g.NavAnyRequest && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); + if (!g.LogEnabled && !nav_request) + if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) + { + if (window->AutoFitFramesX > 0 || window->AutoFitFramesY > 0) + window->HiddenFramesCannotSkipItems = 1; + else window->HiddenFramesCanSkipItems = 1; - } + } // Hide along with parent or if parent is collapsed if (parent_window && (parent_window->Collapsed || parent_window->HiddenFramesCanSkipItems > 0)) @@ -6922,7 +7088,8 @@ void ImGui::End() if (window->DC.CurrentColumns) EndColumns(); PopClipRect(); // Inner window clip rectangle - PopFocusScope(); + if ((window->Flags & ImGuiWindowFlags_NavFlattened) == 0) + PopFocusScope(); // Stop logging if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging @@ -7049,6 +7216,7 @@ void ImGui::FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags) g.NavLayer = ImGuiNavLayer_Main; g.NavFocusScopeId = window ? window->NavRootFocusScopeId : 0; g.NavIdIsAlive = false; + g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; // Close popups if any ClosePopupsOverWindow(window, false); @@ -7096,7 +7264,6 @@ void ImGui::FocusTopMostWindowUnderOne(ImGuiWindow* under_this_window, ImGuiWind { // We may later decide to test for different NoXXXInputs based on the active navigation input (mouse vs nav) but that may feel more confusing to the user. ImGuiWindow* window = g.WindowsFocusOrder[i]; - IM_ASSERT(window == window->RootWindow); if (window == ignore_window || !window->WasActive) continue; if ((window->Flags & (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) != (ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs)) @@ -7294,6 +7461,10 @@ bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_b return false; } +// Is current window hovered and hoverable (e.g. not blocked by a popup/modal)? See ImGuiHoveredFlags_ for options. +// IMPORTANT: If you are trying to check whether your mouse should be dispatched to Dear ImGui or to your underlying app, +// you should not use this function! Use the 'io.WantCaptureMouse' boolean for that! +// Refer to FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" for details. bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { IM_ASSERT((flags & ~ImGuiHoveredFlags_AllowedMaskForIsWindowHovered) == 0 && "Invalid flags for IsWindowHovered()!"); @@ -7332,7 +7503,7 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) // for different windows of the hierarchy. Possibly need a Hash(Current+Flags) ==> (Timer) cache. // We can implement this for _Stationary because the data is linked to HoveredWindow rather than CurrentWindow. if (flags & ImGuiHoveredFlags_ForTooltip) - flags |= g.Style.HoverFlagsForTooltipMouse; + flags = ApplyHoverFlagsForTooltip(flags, g.Style.HoverFlagsForTooltipMouse); if ((flags & ImGuiHoveredFlags_Stationary) != 0 && g.HoverWindowUnlockedStationaryId != ref_window->ID) return false; @@ -7400,7 +7571,7 @@ void ImGui::SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiCond cond) // Set const ImVec2 old_pos = window->Pos; - window->Pos = ImFloor(pos); + window->Pos = ImTrunc(pos); ImVec2 offset = window->Pos - old_pos; if (offset.x == 0.0f && offset.y == 0.0f) return; @@ -7438,18 +7609,22 @@ void ImGui::SetWindowSize(ImGuiWindow* window, const ImVec2& size, ImGuiCond con IM_ASSERT(cond == 0 || ImIsPowerOfTwo(cond)); // Make sure the user doesn't attempt to combine multiple condition flags. window->SetWindowSizeAllowFlags &= ~(ImGuiCond_Once | ImGuiCond_FirstUseEver | ImGuiCond_Appearing); + // Enable auto-fit (not done in BeginChild() path unless appearing or combined with ImGuiChildFlags_AlwaysAutoResize) + if ((window->Flags & ImGuiWindowFlags_ChildWindow) == 0 || window->Appearing || (window->ChildFlags & ImGuiChildFlags_AlwaysAutoResize) != 0) + window->AutoFitFramesX = (size.x <= 0.0f) ? 2 : 0; + if ((window->Flags & ImGuiWindowFlags_ChildWindow) == 0 || window->Appearing || (window->ChildFlags & ImGuiChildFlags_AlwaysAutoResize) != 0) + window->AutoFitFramesY = (size.y <= 0.0f) ? 2 : 0; + // Set ImVec2 old_size = window->SizeFull; - window->AutoFitFramesX = (size.x <= 0.0f) ? 2 : 0; - window->AutoFitFramesY = (size.y <= 0.0f) ? 2 : 0; if (size.x <= 0.0f) window->AutoFitOnlyGrows = false; else - window->SizeFull.x = IM_FLOOR(size.x); + window->SizeFull.x = IM_TRUNC(size.x); if (size.y <= 0.0f) window->AutoFitOnlyGrows = false; else - window->SizeFull.y = IM_FLOOR(size.y); + window->SizeFull.y = IM_TRUNC(size.y); if (old_size.x != window->SizeFull.x || old_size.y != window->SizeFull.y) MarkIniSettingsDirty(window); } @@ -7483,7 +7658,7 @@ void ImGui::SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const I window->HitTestHoleOffset = ImVec2ih(pos - window->Pos); } -void ImGui::SetWindowHiddendAndSkipItemsForCurrentFrame(ImGuiWindow* window) +void ImGui::SetWindowHiddenAndSkipItemsForCurrentFrame(ImGuiWindow* window) { window->Hidden = window->SkipItems = true; window->HiddenFramesCanSkipItems = 1; @@ -7549,6 +7724,10 @@ void ImGui::SetNextWindowSize(const ImVec2& size, ImGuiCond cond) g.NextWindowData.SizeCond = cond ? cond : ImGuiCond_Always; } +// For each axis: +// - Use 0.0f as min or FLT_MAX as max if you don't want limits, e.g. size_min = (500.0f, 0.0f), size_max = (FLT_MAX, FLT_MAX) sets a minimum width. +// - Use -1 for both min and max of same axis to preserve current size which itself is a constraint. +// - See "Demo->Examples->Constrained-resizing window" for examples. void ImGui::SetNextWindowSizeConstraints(const ImVec2& size_min, const ImVec2& size_max, ImGuiSizeCallback custom_callback, void* custom_callback_user_data) { ImGuiContext& g = *GImGui; @@ -7564,7 +7743,7 @@ void ImGui::SetNextWindowContentSize(const ImVec2& size) { ImGuiContext& g = *GImGui; g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasContentSize; - g.NextWindowData.ContentSizeVal = ImFloor(size); + g.NextWindowData.ContentSizeVal = ImTrunc(size); } void ImGui::SetNextWindowScroll(const ImVec2& scroll) @@ -7653,7 +7832,7 @@ void ImGui::FocusItem() return; } - ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSelect; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavHighlight | ImGuiNavMoveFlags_NoSelect; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; SetNavWindow(window); NavMoveRequestSubmit(ImGuiDir_None, ImGuiDir_Up, move_flags, scroll_flags); @@ -7688,7 +7867,7 @@ void ImGui::SetKeyboardFocusHere(int offset) SetNavWindow(window); - ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_Activate | ImGuiNavMoveFlags_FocusApi; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_Activate | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavHighlight; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, move_flags, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. if (offset == -1) @@ -7775,7 +7954,7 @@ void ImGui::PushOverrideID(ImGuiID id) } // Helper to avoid a common series of PushOverrideID -> GetID() -> PopID() call -// (note that when using this pattern, TestEngine's "Stack Tool" will tend to not display the intermediate stack level. +// (note that when using this pattern, ID Stack Tool will tend to not display the intermediate stack level. // for that to work we would need to do PushOverrideID() -> ItemAdd() -> PopID() which would alter widget code a little more) ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) { @@ -7865,6 +8044,7 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) // - IsMouseDragPastThreshold() [Internal] // - IsMouseDragging() // - GetMousePos() +// - SetMousePos() [Internal] // - GetMousePosOnOpeningCurrentPopup() // - IsMousePosValid() // - IsAnyMouseDown() @@ -7933,11 +8113,13 @@ static const char* const GKeyNames[] = "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", + "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "Apostrophe", "Comma", "Minus", "Period", "Slash", "Semicolon", "Equal", "LeftBracket", "Backslash", "RightBracket", "GraveAccent", "CapsLock", "ScrollLock", "NumLock", "PrintScreen", "Pause", "Keypad0", "Keypad1", "Keypad2", "Keypad3", "Keypad4", "Keypad5", "Keypad6", "Keypad7", "Keypad8", "Keypad9", "KeypadDecimal", "KeypadDivide", "KeypadMultiply", "KeypadSubtract", "KeypadAdd", "KeypadEnter", "KeypadEqual", + "AppBack", "AppForward", "GamepadStart", "GamepadBack", "GamepadFaceLeft", "GamepadFaceRight", "GamepadFaceUp", "GamepadFaceDown", "GamepadDpadLeft", "GamepadDpadRight", "GamepadDpadUp", "GamepadDpadDown", @@ -7974,7 +8156,7 @@ const char* ImGui::GetKeyName(ImGuiKey key) } // ImGuiMod_Shortcut is translated to either Ctrl or Super. -void ImGui::GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size) +const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size) { ImGuiContext& g = *GImGui; if (key_chord & ImGuiMod_Shortcut) @@ -7985,6 +8167,7 @@ void ImGui::GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_ (key_chord & ImGuiMod_Alt) ? "Alt+" : "", (key_chord & ImGuiMod_Super) ? (g.IO.ConfigMacOSXBehaviors ? "Cmd+" : "Super+") : "", GetKeyName((ImGuiKey)(key_chord & ~ImGuiMod_Mask_))); + return out_buf; } // t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) @@ -8059,11 +8242,15 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) rt->EntriesNext.push_back(*routing_entry); // Write alive ones into new buffer // Apply routing to owner if there's no owner already (RoutingCurr == None at this point) + // This is the result of previous frame's SetShortcutRouting() call. if (routing_entry->Mods == g.IO.KeyMods) { ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); if (owner_data->OwnerCurr == ImGuiKeyOwner_None) + { owner_data->OwnerCurr = routing_entry->RoutingCurr; + //IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X) via Routing\n", GetKeyName(key), routing_entry->RoutingCurr); + } } } @@ -8140,22 +8327,25 @@ static int CalcRoutingScore(ImGuiWindow* location, ImGuiID owner_id, ImGuiInputF if (owner_id != 0 && g.ActiveId == owner_id) return 1; + // Early out when not in focus stack + if (focused == NULL || focused->RootWindow != location->RootWindow) + return 255; + // Score based on distance to focused window (lower is better) // Assuming both windows are submitting a routing request, // - When Window....... is focused -> Window scores 3 (best), Window/ChildB scores 255 (no match) // - When Window/ChildB is focused -> Window scores 4, Window/ChildB scores 3 (best) // Assuming only WindowA is submitting a routing request, // - When Window/ChildB is focused -> Window scores 4 (best), Window/ChildB doesn't have a score. - if (focused != NULL && focused->RootWindow == location->RootWindow) - for (int next_score = 3; focused != NULL; next_score++) + for (int next_score = 3; focused != NULL; next_score++) + { + if (focused == location) { - if (focused == location) - { - IM_ASSERT(next_score < 255); - return next_score; - } - focused = (focused->RootWindow != focused) ? focused->ParentWindow : NULL; // FIXME: This could be later abstracted as a focus path + IM_ASSERT(next_score < 255); + return next_score; } + focused = (focused->RootWindow != focused) ? focused->ParentWindow : NULL; // FIXME: This could be later abstracted as a focus path + } return 255; } @@ -8182,9 +8372,14 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI else IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteMask_)); // Check that only 1 routing flag is used + // [DEBUG] Debug break requested by user + if (g.DebugBreakInShortcutRouting == key_chord) + IM_DEBUG_BREAK(); + if (flags & ImGuiInputFlags_RouteUnlessBgFocused) if (g.NavWindow == NULL) return false; + // Note how ImGuiInputFlags_RouteAlways won't set routing and thus won't set owner. May want to rework this? if (flags & ImGuiInputFlags_RouteAlways) return true; @@ -8248,13 +8443,28 @@ bool ImGui::IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) if (t < 0.0f) return false; IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByIsKeyPressed) == 0); // Passing flags not supported by this function! + if (flags & (ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RepeatUntilMask_)) // Setting any _RepeatXXX option enables _Repeat + flags |= ImGuiInputFlags_Repeat; bool pressed = (t == 0.0f); - if (!pressed && ((flags & ImGuiInputFlags_Repeat) != 0)) + if (!pressed && (flags & ImGuiInputFlags_Repeat) != 0) { float repeat_delay, repeat_rate; GetTypematicRepeatRate(flags, &repeat_delay, &repeat_rate); pressed = (t > repeat_delay) && GetKeyPressedAmount(key, repeat_delay, repeat_rate) > 0; + if (pressed && (flags & ImGuiInputFlags_RepeatUntilMask_)) + { + // Slightly bias 'key_pressed_time' as DownDuration is an accumulation of DeltaTime which we compare to an absolute time value. + // Ideally we'd replace DownDuration with KeyPressedTime but it would break user's code. + ImGuiContext& g = *GImGui; + double key_pressed_time = g.Time - t + 0.00001f; + if ((flags & ImGuiInputFlags_RepeatUntilKeyModsChange) && (g.LastKeyModsChangeTime > key_pressed_time)) + pressed = false; + if ((flags & ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone) && (g.LastKeyModsChangeFromNoneTime > key_pressed_time)) + pressed = false; + if ((flags & ImGuiInputFlags_RepeatUntilOtherKeyPress) && (g.LastKeyboardKeyPressTime > key_pressed_time)) + pressed = false; + } } if (!pressed) return false; @@ -8306,7 +8516,7 @@ bool ImGui::IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInput const float t = g.IO.MouseDownDuration[button]; if (t < 0.0f) return false; - IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByIsKeyPressed) == 0); // Passing flags not supported by this function! + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByIsMouseClicked) == 0); // Passing flags not supported by this function! // FIXME: Could support RepeatRate and RepeatUntil flags here. const bool repeat = (flags & ImGuiInputFlags_Repeat) != 0; const bool pressed = (t == 0.0f) || (repeat && t > g.IO.KeyRepeatDelay && CalcTypematicRepeatAmount(t - g.IO.DeltaTime, t, g.IO.KeyRepeatDelay, g.IO.KeyRepeatRate) > 0); @@ -8340,6 +8550,13 @@ bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button) return g.IO.MouseClickedCount[button] == 2 && TestKeyOwner(MouseButtonToKey(button), ImGuiKeyOwner_Any); } +bool ImGui::IsMouseDoubleClicked(ImGuiMouseButton button, ImGuiID owner_id) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); + return g.IO.MouseClickedCount[button] == 2 && TestKeyOwner(MouseButtonToKey(button), owner_id); +} + int ImGui::GetMouseClickedCount(ImGuiMouseButton button) { ImGuiContext& g = *GImGui; @@ -8359,9 +8576,8 @@ bool ImGui::IsMouseHoveringRect(const ImVec2& r_min, const ImVec2& r_max, bool c if (clip) rect_clipped.ClipWith(g.CurrentWindow->ClipRect); - // Expand for touch input - const ImRect rect_for_touch(rect_clipped.Min - g.Style.TouchExtraPadding, rect_clipped.Max + g.Style.TouchExtraPadding); - if (!rect_for_touch.Contains(g.IO.MousePos)) + // Hit testing, expanded for touch input + if (!rect_clipped.ContainsWithPad(g.IO.MousePos, g.Style.TouchExtraPadding)) return false; return true; } @@ -8392,6 +8608,17 @@ ImVec2 ImGui::GetMousePos() return g.IO.MousePos; } +// This is called TeleportMousePos() and not SetMousePos() to emphasis that setting MousePosPrev will effectively clear mouse delta as well. +// It is expected you only call this if (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos) is set and supported by backend. +void ImGui::TeleportMousePos(const ImVec2& pos) +{ + ImGuiContext& g = *GImGui; + g.IO.MousePos = g.IO.MousePosPrev = pos; + g.IO.MouseDelta = ImVec2(0.0f, 0.0f); + g.IO.WantSetMousePos = true; + //IMGUI_DEBUG_LOG_IO("TeleportMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); +} + // NB: prefer to call right after BeginPopup(). At the time Selectable/MenuItem is activated, the popup is already closed! ImVec2 ImGui::GetMousePosOnOpeningCurrentPopup() { @@ -8527,7 +8754,9 @@ static void ImGui::UpdateKeyboardInputs() GetKeyData(ImGuiMod_Super)->Down = io.KeySuper; } } +#endif + // Import legacy ImGuiNavInput_ io inputs and convert to gamepad keys #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; if (io.BackendUsingLegacyNavInputArray && nav_gamepad_active) @@ -8550,7 +8779,6 @@ static void ImGui::UpdateKeyboardInputs() MAP_LEGACY_NAV_INPUT_TO_KEY1(ImGuiKey_GamepadLStickDown, ImGuiNavInput_LStickDown); #undef NAV_MAP_KEY } -#endif #endif // Update aliases @@ -8559,15 +8787,20 @@ static void ImGui::UpdateKeyboardInputs() UpdateAliasKey(ImGuiKey_MouseWheelX, io.MouseWheelH != 0.0f, io.MouseWheelH); UpdateAliasKey(ImGuiKey_MouseWheelY, io.MouseWheel != 0.0f, io.MouseWheel); - // Synchronize io.KeyMods and io.KeyXXX values. + // Synchronize io.KeyMods and io.KeyCtrl/io.KeyShift/etc. values. // - New backends (1.87+): send io.AddKeyEvent(ImGuiMod_XXX) -> -> (here) deriving io.KeyMods + io.KeyXXX from key array. // - Legacy backends: set io.KeyXXX bools -> (above) set key array from io.KeyXXX -> (here) deriving io.KeyMods + io.KeyXXX from key array. // So with legacy backends the 4 values will do a unnecessary back-and-forth but it makes the code simpler and future facing. + const ImGuiKeyChord prev_key_mods = io.KeyMods; io.KeyMods = GetMergedModsFromKeys(); io.KeyCtrl = (io.KeyMods & ImGuiMod_Ctrl) != 0; io.KeyShift = (io.KeyMods & ImGuiMod_Shift) != 0; io.KeyAlt = (io.KeyMods & ImGuiMod_Alt) != 0; io.KeySuper = (io.KeyMods & ImGuiMod_Super) != 0; + if (prev_key_mods != io.KeyMods) + g.LastKeyModsChangeTime = g.Time; + if (prev_key_mods != io.KeyMods && prev_key_mods == 0) + g.LastKeyModsChangeFromNoneTime = g.Time; // Clear gamepad data if disabled if ((io.BackendFlags & ImGuiBackendFlags_HasGamepad) == 0) @@ -8583,6 +8816,14 @@ static void ImGui::UpdateKeyboardInputs() ImGuiKeyData* key_data = &io.KeysData[i]; key_data->DownDurationPrev = key_data->DownDuration; key_data->DownDuration = key_data->Down ? (key_data->DownDuration < 0.0f ? 0.0f : key_data->DownDuration + io.DeltaTime) : -1.0f; + if (key_data->DownDuration == 0.0f) + { + ImGuiKey key = (ImGuiKey)(ImGuiKey_KeysData_OFFSET + i); + if (IsKeyboardKey(key)) + g.LastKeyboardKeyPressTime = g.Time; + else if (key == ImGuiKey_ReservedForModCtrl || key == ImGuiKey_ReservedForModShift || key == ImGuiKey_ReservedForModAlt || key == ImGuiKey_ReservedForModSuper) + g.LastKeyboardKeyPressTime = g.Time; + } } // Update keys/input owner (named keys only): one entry per key @@ -8596,6 +8837,7 @@ static void ImGui::UpdateKeyboardInputs() owner_data->LockThisFrame = owner_data->LockUntilRelease = owner_data->LockUntilRelease && key_data->Down; // Clear LockUntilRelease when key is not Down anymore } + // Update key routing (for e.g. shortcuts) UpdateKeyRoutingTable(&g.KeysRoutingTable); } @@ -8613,7 +8855,7 @@ static void ImGui::UpdateMouseInputs() // Round mouse position to avoid spreading non-rounded position (e.g. UpdateManualResize doesn't support them well) if (IsMousePosValid(&io.MousePos)) - io.MousePos = g.MouseLastValidPos = ImFloorSigned(io.MousePos); + io.MousePos = g.MouseLastValidPos = ImFloor(io.MousePos); // If mouse just appeared or disappeared (usually denoted by -FLT_MAX components) we cancel out movement in MouseDelta if (IsMousePosValid(&io.MousePos) && IsMousePosValid(&io.MousePosPrev)) @@ -8632,7 +8874,6 @@ static void ImGui::UpdateMouseInputs() if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) g.NavDisableMouseHover = false; - io.MousePosPrev = io.MousePos; for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) { io.MouseClicked[i] = io.MouseDown[i] && io.MouseDownDuration[i] < 0.0f; @@ -8769,8 +9010,8 @@ void ImGui::UpdateMouseWheel() { const ImVec2 offset = window->Size * (1.0f - scale) * (g.IO.MousePos - window->Pos) / window->Size; SetWindowPos(window, window->Pos + offset, 0); - window->Size = ImFloor(window->Size * scale); - window->SizeFull = ImFloor(window->SizeFull * scale); + window->Size = ImTrunc(window->Size * scale); + window->SizeFull = ImTrunc(window->SizeFull * scale); } return; } @@ -8806,15 +9047,17 @@ void ImGui::UpdateMouseWheel() { LockWheelingWindow(window, wheel.x); float max_step = window->InnerRect.GetWidth() * 0.67f; - float scroll_step = ImFloor(ImMin(2 * window->CalcFontSize(), max_step)); + float scroll_step = ImTrunc(ImMin(2 * window->CalcFontSize(), max_step)); SetScrollX(window, window->Scroll.x - wheel.x * scroll_step); + g.WheelingWindowScrolledFrame = g.FrameCount; } if (do_scroll[ImGuiAxis_Y]) { LockWheelingWindow(window, wheel.y); float max_step = window->InnerRect.GetHeight() * 0.67f; - float scroll_step = ImFloor(ImMin(5 * window->CalcFontSize(), max_step)); + float scroll_step = ImTrunc(ImMin(5 * window->CalcFontSize(), max_step)); SetScrollY(window, window->Scroll.y - wheel.y * scroll_step); + g.WheelingWindowScrolledFrame = g.FrameCount; } } } @@ -8847,8 +9090,8 @@ static const char* GetMouseSourceName(ImGuiMouseSource source) static void DebugPrintInputEvent(const char* prefix, const ImGuiInputEvent* e) { ImGuiContext& g = *GImGui; - if (e->Type == ImGuiInputEventType_MousePos) { if (e->MousePos.PosX == -FLT_MAX && e->MousePos.PosY == -FLT_MAX) IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (-FLT_MAX, -FLT_MAX)\n", prefix); else IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (%.1f, %.1f) (%s)\n", prefix, e->MousePos.PosX, e->MousePos.PosY, GetMouseSourceName(e->MouseWheel.MouseSource)); return; } - if (e->Type == ImGuiInputEventType_MouseButton) { IMGUI_DEBUG_LOG_IO("[io] %s: MouseButton %d %s (%s)\n", prefix, e->MouseButton.Button, e->MouseButton.Down ? "Down" : "Up", GetMouseSourceName(e->MouseWheel.MouseSource)); return; } + if (e->Type == ImGuiInputEventType_MousePos) { if (e->MousePos.PosX == -FLT_MAX && e->MousePos.PosY == -FLT_MAX) IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (-FLT_MAX, -FLT_MAX)\n", prefix); else IMGUI_DEBUG_LOG_IO("[io] %s: MousePos (%.1f, %.1f) (%s)\n", prefix, e->MousePos.PosX, e->MousePos.PosY, GetMouseSourceName(e->MousePos.MouseSource)); return; } + if (e->Type == ImGuiInputEventType_MouseButton) { IMGUI_DEBUG_LOG_IO("[io] %s: MouseButton %d %s (%s)\n", prefix, e->MouseButton.Button, e->MouseButton.Down ? "Down" : "Up", GetMouseSourceName(e->MouseButton.MouseSource)); return; } if (e->Type == ImGuiInputEventType_MouseWheel) { IMGUI_DEBUG_LOG_IO("[io] %s: MouseWheel (%.3f, %.3f) (%s)\n", prefix, e->MouseWheel.WheelX, e->MouseWheel.WheelY, GetMouseSourceName(e->MouseWheel.MouseSource)); return; } if (e->Type == ImGuiInputEventType_Key) { IMGUI_DEBUG_LOG_IO("[io] %s: Key \"%s\" %s\n", prefix, ImGui::GetKeyName(e->Key.Key), e->Key.Down ? "Down" : "Up"); return; } if (e->Type == ImGuiInputEventType_Text) { IMGUI_DEBUG_LOG_IO("[io] %s: Text: %c (U+%08X)\n", prefix, e->Text.Char, e->Text.Char); return; } @@ -8880,6 +9123,8 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) ImGuiInputEvent* e = &g.InputEventsQueue[event_n]; if (e->Type == ImGuiInputEventType_MousePos) { + if (g.IO.WantSetMousePos) + continue; // Trickling Rule: Stop processing queued events if we already handled a mouse button change ImVec2 event_pos(e->MousePos.PosX, e->MousePos.PosY); if (trickle_fast_inputs && (mouse_button_changed != 0 || mouse_wheeled || key_changed || text_inputted)) @@ -9036,10 +9281,11 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) // - SetKeyOwner(..., Any or None, Lock) : set lock void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) { + ImGuiContext& g = *GImGui; IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function! + //IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X, flags=%08X)\n", GetKeyName(key), owner_id, flags); - ImGuiContext& g = *GImGui; ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); owner_data->OwnerCurr = owner_data->OwnerNext = owner_id; @@ -9081,16 +9327,16 @@ void ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags) } } -bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +// This is the only public API until we expose owner_id versions of the API as replacements. +bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord) { - ImGuiContext& g = *GImGui; - - // When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any. - if ((flags & ImGuiInputFlags_RouteMask_) == 0) - flags |= ImGuiInputFlags_RouteFocused; - if (!SetShortcutRouting(key_chord, owner_id, flags)) - return false; + return IsKeyChordPressed(key_chord, 0, ImGuiInputFlags_None); +} +// This is equivalent to comparing KeyMods + doing a IsKeyPressed() +bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +{ + ImGuiContext& g = *GImGui; if (key_chord & ImGuiMod_Shortcut) key_chord = ConvertShortcutMod(key_chord); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); @@ -9101,11 +9347,30 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); if (key == ImGuiKey_None) key = ConvertSingleModFlagToKey(&g, mods); + if (!IsKeyPressed(key, owner_id, (flags & ImGuiInputFlags_RepeatMask_))) + return false; + return true; +} + +bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +{ + //ImGuiContext& g = *GImGui; + //IMGUI_DEBUG_LOG("Shortcut(%s, owner_id=0x%08X, flags=%X)\n", GetKeyChordName(key_chord, g.TempBuffer.Data, g.TempBuffer.Size), owner_id, flags); - if (!IsKeyPressed(key, owner_id, (flags & (ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateMask_)))) + // When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any. + if ((flags & ImGuiInputFlags_RouteMask_) == 0) + flags |= ImGuiInputFlags_RouteFocused; + if (!SetShortcutRouting(key_chord, owner_id, flags)) return false; - IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByShortcut) == 0); // Passing flags not supported by this function! + // Default repeat behavior for Shortcut() + // So e.g. pressing Ctrl+W and releasing Ctrl while holding W will not trigger the W shortcut. + if ((flags & ImGuiInputFlags_RepeatUntilMask_) == 0) + flags |= ImGuiInputFlags_RepeatUntilKeyModsChange; + + if (!IsKeyChordPressed(key_chord, owner_id, flags)) + return false; + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByShortcut) == 0); // Passing flags not supported by this function! return true; } @@ -9422,6 +9687,7 @@ void ImGuiStackSizes::CompareWithContextState(ImGuiContext* ctx) // Advance cursor given item size for layout. // Register minimum needed size so it can extend the bounding box used for auto-fit calculation. // See comments in ItemAdd() about how/why the size provided to ItemSize() vs ItemAdd() may often different. +// THIS IS IN THE PERFORMANCE CRITICAL PATH. void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) { ImGuiContext& g = *GImGui; @@ -9441,8 +9707,8 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; window->DC.CursorPosPrevLine.y = line_y1; - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line - window->DC.CursorPos.y = IM_FLOOR(line_y1 + line_height + g.Style.ItemSpacing.y); // Next line + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line + window->DC.CursorPos.y = IM_TRUNC(line_y1 + line_height + g.Style.ItemSpacing.y); // Next line window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] @@ -9461,6 +9727,7 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. +// THIS IS IN THE PERFORMANCE CRITICAL PATH (UNTIL THE CLIPPING TEST AND EARLY-RETURN) bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGuiItemFlags extra_flags) { ImGuiContext& g = *GImGui; @@ -9473,12 +9740,13 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu g.LastItemData.NavRect = nav_bb_arg ? *nav_bb_arg : bb; g.LastItemData.InFlags = g.CurrentItemFlags | g.NextItemData.ItemFlags | extra_flags; g.LastItemData.StatusFlags = ImGuiItemStatusFlags_None; + // Note: we don't copy 'g.NextItemData.SelectionUserData' to an hypothetical g.LastItemData.SelectionUserData: since the former is not cleared. - // Directional navigation processing if (id != 0) { KeepAliveID(id); + // Directional navigation processing // Runs prior to clipping early-out // (a) So that NavInitRequest can be honored, for newly opened windows to select a default widget // (b) So that we can scroll up/down past clipped items. This adds a small O(N) cost to regular navigation requests @@ -9490,18 +9758,16 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. if (!(g.LastItemData.InFlags & ImGuiItemFlags_NoNav)) { + // FIMXE-NAV: investigate changing the window tests into a simple 'if (g.NavFocusScopeId == g.CurrentFocusScopeId)' test. window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) NavProcessItem(); } - - // [DEBUG] People keep stumbling on this problem and using "" as identifier in the root of a window instead of "##something". - // Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something". - // READ THE FAQ: https://dearimgui.com/faq - IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); } + + // Lightweight clear of SetNextItemXXX data. g.NextItemData.Flags = ImGuiNextItemDataFlags_None; g.NextItemData.ItemFlags = ImGuiItemFlags_None; @@ -9511,7 +9777,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu #endif // Clipping test - // (FIXME: This is a modified copy of IsClippedEx() so we can reuse the is_rect_visible value) + // (this is a modified copy of IsClippedEx() so we can reuse the is_rect_visible value) //const bool is_clipped = IsClippedEx(bb, id); //if (is_clipped) // return false; @@ -9523,12 +9789,20 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // [DEBUG] #ifndef IMGUI_DISABLE_DEBUG_TOOLS - if (id != 0 && id == g.DebugLocateId) - DebugLocateItemResolveWithLastItem(); -#endif + if (id != 0) + { + if (id == g.DebugLocateId) + DebugLocateItemResolveWithLastItem(); + + // [DEBUG] People keep stumbling on this problem and using "" as identifier in the root of a window instead of "##something". + // Empty identifier are valid and useful in a small amount of cases, but 99.9% of the time you want to use "##something". + // READ THE FAQ: https://dearimgui.com/faq + IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); + } //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] //if ((g.LastItemData.InFlags & ImGuiItemFlags_NoNav) == 0) // window->DrawList->AddRect(g.LastItemData.NavRect.Min, g.LastItemData.NavRect.Max, IM_COL32(255,255,0,255)); // [DEBUG] +#endif // We need to calculate this now to take account of the current clipping rectangle (as items like Selectable may change them) if (is_rect_visible) @@ -9541,7 +9815,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // Gets back to previous line and continue with horizontal layout // offset_from_start_x == 0 : follow right after previous item // offset_from_start_x != 0 : align to specified x position (relative to window/group left) -// spacing_w < 0 : use default spacing if pos_x == 0, no spacing if pos_x != 0 +// spacing_w < 0 : use default spacing if offset_from_start_x == 0, no spacing if offset_from_start_x != 0 // spacing_w >= 0 : enforce spacing amount void ImGui::SameLine(float offset_from_start_x, float spacing_w) { @@ -9575,9 +9849,6 @@ ImVec2 ImGui::GetCursorScreenPos() return window->DC.CursorPos; } -// 2022/08/05: Setting cursor position also extend boundaries (via modifying CursorMaxPos) used to compute window size, group size etc. -// I believe this was is a judicious choice but it's probably being relied upon (it has been the case since 1.31 and 1.50) -// It would be sane if we requested user to use SetCursorPos() + Dummy(ImVec2(0,0)) to extend CursorMaxPos... void ImGui::SetCursorScreenPos(const ImVec2& pos) { ImGuiWindow* window = GetCurrentWindow(); @@ -9674,14 +9945,18 @@ void ImGui::PushMultiItemsWidths(int components, float w_full) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(components > 0); const ImGuiStyle& style = g.Style; - const float w_item_one = ImMax(1.0f, IM_FLOOR((w_full - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components)); - const float w_item_last = ImMax(1.0f, IM_FLOOR(w_full - (w_item_one + style.ItemInnerSpacing.x) * (components - 1))); window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); // Backup current width - window->DC.ItemWidthStack.push_back(w_item_last); - for (int i = 0; i < components - 2; i++) - window->DC.ItemWidthStack.push_back(w_item_one); - window->DC.ItemWidth = (components == 1) ? w_item_last : w_item_one; + float w_items = w_full - style.ItemInnerSpacing.x * (components - 1); + float prev_split = w_items; + for (int i = components - 1; i > 0; i--) + { + float next_split = IM_TRUNC(w_items * i / components); + window->DC.ItemWidthStack.push_back(ImMax(prev_split - next_split, 1.0f)); + prev_split = next_split; + } + window->DC.ItemWidth = ImMax(prev_split, 1.0f); g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; } @@ -9708,7 +9983,7 @@ float ImGui::CalcItemWidth() float region_max_x = GetContentRegionMaxAbs().x; w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); } - w = IM_FLOOR(w); + w = IM_TRUNC(w); return w; } @@ -9769,10 +10044,8 @@ ImVec2 ImGui::GetContentRegionMax() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImVec2 mx = window->ContentRegionRect.Max - window->Pos; - if (window->DC.CurrentColumns || g.CurrentTable) - mx.x = window->WorkRect.Max.x - window->Pos.x; - return mx; + ImVec2 mx = (window->DC.CurrentColumns || g.CurrentTable) ? window->WorkRect.Max : window->ContentRegionRect.Max; + return mx - window->Pos; } // [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. @@ -9780,9 +10053,7 @@ ImVec2 ImGui::GetContentRegionMaxAbs() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImVec2 mx = window->ContentRegionRect.Max; - if (window->DC.CurrentColumns || g.CurrentTable) - mx.x = window->WorkRect.Max.x; + ImVec2 mx = (window->DC.CurrentColumns || g.CurrentTable) ? window->WorkRect.Max : window->ContentRegionRect.Max; return mx; } @@ -9817,6 +10088,7 @@ void ImGui::BeginGroup() ImGuiGroupData& group_data = g.GroupStack.back(); group_data.WindowID = window->ID; group_data.BackupCursorPos = window->DC.CursorPos; + group_data.BackupCursorPosPrevLine = window->DC.CursorPosPrevLine; group_data.BackupCursorMaxPos = window->DC.CursorMaxPos; group_data.BackupIndent = window->DC.Indent; group_data.BackupGroupOffset = window->DC.GroupOffset; @@ -9824,6 +10096,7 @@ void ImGui::BeginGroup() group_data.BackupCurrLineTextBaseOffset = window->DC.CurrLineTextBaseOffset; group_data.BackupActiveIdIsAlive = g.ActiveIdIsAlive; group_data.BackupHoveredIdIsAlive = g.HoveredId != 0; + group_data.BackupIsSameLine = window->DC.IsSameLine; group_data.BackupActiveIdPreviousFrameIsAlive = g.ActiveIdPreviousFrameIsAlive; group_data.EmitItem = true; @@ -9850,11 +10123,13 @@ void ImGui::EndGroup() ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos)); window->DC.CursorPos = group_data.BackupCursorPos; + window->DC.CursorPosPrevLine = group_data.BackupCursorPosPrevLine; window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos); window->DC.Indent = group_data.BackupIndent; window->DC.GroupOffset = group_data.BackupGroupOffset; window->DC.CurrLineSize = group_data.BackupCurrLineSize; window->DC.CurrLineTextBaseOffset = group_data.BackupCurrLineTextBaseOffset; + window->DC.IsSameLine = group_data.BackupIsSameLine; if (g.LogEnabled) g.LogLinePosY = -FLT_MAX; // To enforce a carriage return @@ -9895,7 +10170,8 @@ void ImGui::EndGroup() g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Deactivated; g.GroupStack.pop_back(); - //window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] + if (g.DebugShowGroupRects) + window->DrawList->AddRect(group_bb.Min, group_bb.Max, IM_COL32(255,0,255,255)); // [Debug] } @@ -9934,7 +10210,7 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window) } scroll[axis] = scroll_target - center_ratio * (window->SizeFull[axis] - decoration_size[axis]); } - scroll[axis] = IM_FLOOR(ImMax(scroll[axis], 0.0f)); + scroll[axis] = IM_ROUND(ImMax(scroll[axis], 0.0f)); if (!window->Collapsed && !window->SkipItems) scroll[axis] = ImMin(scroll[axis], window->ScrollMax[axis]); } @@ -9989,7 +10265,7 @@ ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGui else if (((flags & ImGuiScrollFlags_KeepVisibleCenterX) && !fully_visible_x) || (flags & ImGuiScrollFlags_AlwaysCenterX)) { if (can_be_fully_visible_x) - SetScrollFromPosX(window, ImFloor((item_rect.Min.x + item_rect.Max.x) * 0.5f) - window->Pos.x, 0.5f); + SetScrollFromPosX(window, ImTrunc((item_rect.Min.x + item_rect.Max.x) * 0.5f) - window->Pos.x, 0.5f); else SetScrollFromPosX(window, item_rect.Min.x - window->Pos.x, 0.0f); } @@ -10004,7 +10280,7 @@ ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGui else if (((flags & ImGuiScrollFlags_KeepVisibleCenterY) && !fully_visible_y) || (flags & ImGuiScrollFlags_AlwaysCenterY)) { if (can_be_fully_visible_y) - SetScrollFromPosY(window, ImFloor((item_rect.Min.y + item_rect.Max.y) * 0.5f) - window->Pos.y, 0.5f); + SetScrollFromPosY(window, ImTrunc((item_rect.Min.y + item_rect.Max.y) * 0.5f) - window->Pos.y, 0.5f); else SetScrollFromPosY(window, item_rect.Min.y - window->Pos.y, 0.0f); } @@ -10089,7 +10365,7 @@ void ImGui::SetScrollY(float scroll_y) void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x_ratio) { IM_ASSERT(center_x_ratio >= 0.0f && center_x_ratio <= 1.0f); - window->ScrollTarget.x = IM_FLOOR(local_x - window->DecoOuterSizeX1 - window->DecoInnerSizeX1 + window->Scroll.x); // Convert local position to scroll offset + window->ScrollTarget.x = IM_TRUNC(local_x - window->DecoOuterSizeX1 - window->DecoInnerSizeX1 + window->Scroll.x); // Convert local position to scroll offset window->ScrollTargetCenterRatio.x = center_x_ratio; window->ScrollTargetEdgeSnapDist.x = 0.0f; } @@ -10097,7 +10373,7 @@ void ImGui::SetScrollFromPosX(ImGuiWindow* window, float local_x, float center_x void ImGui::SetScrollFromPosY(ImGuiWindow* window, float local_y, float center_y_ratio) { IM_ASSERT(center_y_ratio >= 0.0f && center_y_ratio <= 1.0f); - window->ScrollTarget.y = IM_FLOOR(local_y - window->DecoOuterSizeY1 - window->DecoInnerSizeY1 + window->Scroll.y); // Convert local position to scroll offset + window->ScrollTarget.y = IM_TRUNC(local_y - window->DecoOuterSizeY1 - window->DecoInnerSizeY1 + window->Scroll.y); // Convert local position to scroll offset window->ScrollTargetCenterRatio.y = center_y_ratio; window->ScrollTargetEdgeSnapDist.y = 0.0f; } @@ -10182,7 +10458,7 @@ bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags ext if (window->Active) { // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. - SetWindowHiddendAndSkipItemsForCurrentFrame(window); + SetWindowHiddenAndSkipItemsForCurrentFrame(window); ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; @@ -10517,7 +10793,9 @@ bool ImGui::BeginPopup(const char* str_id, ImGuiWindowFlags flags) } // If 'p_open' is specified for a modal popup window, the popup will have a regular close button which will close the popup. -// Note that popup visibility status is owned by Dear ImGui (and manipulated with e.g. OpenPopup) so the actual value of *p_open is meaningless here. +// Note that popup visibility status is owned by Dear ImGui (and manipulated with e.g. OpenPopup). +// - *p_open set back to false in BeginPopupModal() when popup is not open. +// - if you set *p_open to false before calling BeginPopupModal(), it will close the popup. bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags flags) { ImGuiContext& g = *GImGui; @@ -10526,6 +10804,8 @@ bool ImGui::BeginPopupModal(const char* name, bool* p_open, ImGuiWindowFlags fla if (!IsPopupOpen(id, ImGuiPopupFlags_None)) { g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values + if (p_open && *p_open) + *p_open = false; return false; } @@ -10791,6 +11071,7 @@ void ImGui::SetNavWindow(ImGuiWindow* window) { IMGUI_DEBUG_LOG_FOCUS("[focus] SetNavWindow(\"%s\")\n", window ? window->Name : ""); g.NavWindow = window; + g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; } g.NavInitRequest = g.NavMoveSubmitted = g.NavMoveScoringItems = false; NavUpdateAnyRequestFlag(); @@ -11010,6 +11291,11 @@ static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) result->FocusScopeId = g.CurrentFocusScopeId; result->InFlags = g.LastItemData.InFlags; result->RectRel = WindowRectAbsToRel(window, g.LastItemData.NavRect); + if (result->InFlags & ImGuiItemFlags_HasSelectionUserData) + { + IM_ASSERT(g.NextItemData.SelectionUserData != ImGuiSelectionUserData_Invalid); + result->SelectionUserData = g.NextItemData.SelectionUserData; // INTENTIONAL: At this point this field is not cleared in NextItemData. Avoid unnecessary copy to LastItemData. + } } // True when current work location may be scrolled horizontally when moving left / right. @@ -11022,7 +11308,7 @@ void ImGui::NavUpdateCurrentWindowIsScrollPushableX() } // We get there when either NavId == id, or when g.NavAnyRequest is set (which is updated by NavUpdateAnyRequestFlag above) -// This is called after LastItemData is set. +// This is called after LastItemData is set, but NextItemData is also still valid. static void ImGui::NavProcessItem() { ImGuiContext& g = *GImGui; @@ -11058,7 +11344,7 @@ static void ImGui::NavProcessItem() // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRect + scoring from a rect wrapped according to current wrapping policy) if (g.NavMoveScoringItems && (item_flags & ImGuiItemFlags_Disabled) == 0) { - const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) != 0; + const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) != 0; if (is_tabbing) { NavProcessItemForTabbingRequest(id, item_flags, g.NavMoveFlags); @@ -11086,6 +11372,11 @@ static void ImGui::NavProcessItem() g.NavLayer = window->DC.NavLayerCurrent; g.NavFocusScopeId = g.CurrentFocusScopeId; g.NavIdIsAlive = true; + if (g.LastItemData.InFlags & ImGuiItemFlags_HasSelectionUserData) + { + IM_ASSERT(g.NextItemData.SelectionUserData != ImGuiSelectionUserData_Invalid); + g.NavLastValidSelectionUserData = g.NextItemData.SelectionUserData; // INTENTIONAL: At this point this field is not cleared in NextItemData. Avoid unnecessary copy to LastItemData. + } window->NavRectRel[window->DC.NavLayerCurrent] = WindowRectAbsToRel(window, nav_bb); // Store item bounding box (relative to window position) } } @@ -11104,6 +11395,8 @@ void ImGui::NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flag if ((move_flags & ImGuiNavMoveFlags_FocusApi) == 0) if (g.NavLayer != g.CurrentWindow->DC.NavLayerCurrent) return; + if (g.NavFocusScopeId != g.CurrentFocusScopeId) + return; // - Can always land on an item when using API call. // - Tabbing with _NavEnableKeyboard (space/enter/arrows): goes through every item. @@ -11164,7 +11457,7 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindow != NULL); - if (move_flags & ImGuiNavMoveFlags_Tabbing) + if (move_flags & ImGuiNavMoveFlags_IsTabbing) move_flags |= ImGuiNavMoveFlags_AllowCurrentNavId; g.NavMoveSubmitted = g.NavMoveScoringItems = true; @@ -11174,7 +11467,7 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM g.NavMoveFlags = move_flags; g.NavMoveScrollFlags = scroll_flags; g.NavMoveForwardToNextFrame = false; - g.NavMoveKeyMods = g.IO.KeyMods; + g.NavMoveKeyMods = (move_flags & ImGuiNavMoveFlags_FocusApi) ? 0 : g.IO.KeyMods; g.NavMoveResultLocal.Clear(); g.NavMoveResultLocalVisible.Clear(); g.NavMoveResultOther.Clear(); @@ -11191,6 +11484,19 @@ void ImGui::NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result) NavUpdateAnyRequestFlag(); } +// Called by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere +void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiNavTreeNodeData* tree_node_data) +{ + ImGuiContext& g = *GImGui; + g.NavMoveScoringItems = false; + g.LastItemData.ID = tree_node_data->ID; + g.LastItemData.InFlags = tree_node_data->InFlags & ~ImGuiItemFlags_HasSelectionUserData; // Losing SelectionUserData, recovered next-frame (cheaper). + g.LastItemData.NavRect = tree_node_data->NavRect; + NavApplyItemToResult(result); // Result this instead of implementing a NavApplyPastTreeNodeToResult() + NavClearPreferredPosForAxis(ImGuiAxis_Y); + NavUpdateAnyRequestFlag(); +} + void ImGui::NavMoveRequestCancel() { ImGuiContext& g = *GImGui; @@ -11251,6 +11557,7 @@ void ImGui::NavRestoreLayer(ImGuiNavLayer layer) { ImGuiWindow* prev_nav_window = g.NavWindow; g.NavWindow = NavRestoreLastChildNavWindow(g.NavWindow); // FIXME-NAV: Should clear ongoing nav requests? + g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; if (prev_nav_window) IMGUI_DEBUG_LOG_FOCUS("[focus] NavRestoreLayer: from \"%s\" to SetNavWindow(\"%s\")\n", prev_nav_window->Name, g.NavWindow->Name); } @@ -11337,7 +11644,7 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() } ImVec2 pos = ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); ImGuiViewport* viewport = GetMainViewport(); - return ImFloor(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImFloor() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. + return ImTrunc(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImTrunc() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. } } @@ -11432,8 +11739,8 @@ static void ImGui::NavUpdate() { const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate)); const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, false)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, false))); - const bool input_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Enter)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput)); - const bool input_pressed = input_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Enter, false)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, false))); + const bool input_down = (nav_keyboard_active && (IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_KeypadEnter))) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput)); + const bool input_pressed = input_down && ((nav_keyboard_active && (IsKeyPressed(ImGuiKey_Enter, false) || IsKeyPressed(ImGuiKey_KeypadEnter, false))) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, false))); if (g.ActiveId == 0 && activate_pressed) { g.NavActivateId = g.NavId; @@ -11480,9 +11787,9 @@ static void ImGui::NavUpdate() if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavWindowHasScrollY && move_dir != ImGuiDir_None) { if (move_dir == ImGuiDir_Left || move_dir == ImGuiDir_Right) - SetScrollX(window, ImFloor(window->Scroll.x + ((move_dir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); + SetScrollX(window, ImTrunc(window->Scroll.x + ((move_dir == ImGuiDir_Left) ? -1.0f : +1.0f) * scroll_speed)); if (move_dir == ImGuiDir_Up || move_dir == ImGuiDir_Down) - SetScrollY(window, ImFloor(window->Scroll.y + ((move_dir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); + SetScrollY(window, ImTrunc(window->Scroll.y + ((move_dir == ImGuiDir_Up) ? -1.0f : +1.0f) * scroll_speed)); } // *Normal* Manual scroll with LStick @@ -11492,9 +11799,9 @@ static void ImGui::NavUpdate() const ImVec2 scroll_dir = GetKeyMagnitude2d(ImGuiKey_GamepadLStickLeft, ImGuiKey_GamepadLStickRight, ImGuiKey_GamepadLStickUp, ImGuiKey_GamepadLStickDown); const float tweak_factor = IsKeyDown(ImGuiKey_NavGamepadTweakSlow) ? 1.0f / 10.0f : IsKeyDown(ImGuiKey_NavGamepadTweakFast) ? 10.0f : 1.0f; if (scroll_dir.x != 0.0f && window->ScrollbarX) - SetScrollX(window, ImFloor(window->Scroll.x + scroll_dir.x * scroll_speed * tweak_factor)); + SetScrollX(window, ImTrunc(window->Scroll.x + scroll_dir.x * scroll_speed * tweak_factor)); if (scroll_dir.y != 0.0f) - SetScrollY(window, ImFloor(window->Scroll.y + scroll_dir.y * scroll_speed * tweak_factor)); + SetScrollY(window, ImTrunc(window->Scroll.y + scroll_dir.y * scroll_speed * tweak_factor)); } } @@ -11508,11 +11815,7 @@ static void ImGui::NavUpdate() // Update mouse position if requested // (This will take into account the possibility that a Scroll was queued in the window to offset our absolute mouse position before scroll has been applied) if (set_mouse_pos && (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) - { - io.MousePos = io.MousePosPrev = NavCalcPreferredRefPos(); - io.WantSetMousePos = true; - //IMGUI_DEBUG_LOG_IO("SetMousePos: (%.1f,%.1f)\n", io.MousePos.x, io.MousePos.y); - } + TeleportMousePos(NavCalcPreferredRefPos()); // [DEBUG] g.NavScoringDebugCount = 0; @@ -11546,6 +11849,8 @@ void ImGui::NavInitRequestApplyResult() IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: ApplyResult: NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); g.NavIdIsAlive = true; // Mark as alive from previous frame as we got a result + if (result->SelectionUserData != ImGuiSelectionUserData_Invalid) + g.NavLastValidSelectionUserData = result->SelectionUserData; if (g.NavInitRequestFromMove) NavRestoreHighlightAfterMove(); } @@ -11599,7 +11904,7 @@ void ImGui::NavUpdateCreateMoveRequest() g.NavMoveScrollFlags = ImGuiScrollFlags_None; if (window && !g.NavWindowingTarget && !(window->Flags & ImGuiWindowFlags_NoNavInputs)) { - const ImGuiInputFlags repeat_mode = ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateNavMove; + const ImGuiInputFlags repeat_mode = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateNavMove; if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadLeft, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_LeftArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Left; } if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadRight, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_RightArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Right; } if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadUp, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_UpArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Up; } @@ -11683,7 +11988,7 @@ void ImGui::NavUpdateCreateMoveRequest() scoring_rect.TranslateY(scoring_rect_offset_y); if (g.NavMoveSubmitted) NavBiasScoringRect(scoring_rect, window->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer], g.NavMoveDir, g.NavMoveFlags); - IM_ASSERT(!scoring_rect.IsInverted()); // Ensure if we have a finite, non-inverted bounding box here will allow us to remove extraneous ImFabs() calls in NavScoreItem(). + IM_ASSERT(!scoring_rect.IsInverted()); // Ensure we have a non-inverted bounding box here will allow us to remove extraneous ImFabs() calls in NavScoreItem(). //GetForegroundDrawList()->AddRect(scoring_rect.Min, scoring_rect.Max, IM_COL32(255,200,0,255)); // [DEBUG] //if (!g.NavScoringNoClipRect.IsInverted()) { GetForegroundDrawList()->AddRect(g.NavScoringNoClipRect.Min, g.NavScoringNoClipRect.Max, IM_COL32(255, 200, 0, 255)); } // [DEBUG] } @@ -11705,14 +12010,13 @@ void ImGui::NavUpdateCreateTabbingRequest() // Initiate tabbing request // (this is ALWAYS ENABLED, regardless of ImGuiConfigFlags_NavEnableKeyboard flag!) - // Initially this was designed to use counters and modulo arithmetic, but that could not work with unsubmitted items (list clipper). Instead we use a strategy close to other move requests. // See NavProcessItemForTabbingRequest() for a description of the various forward/backward tabbing cases with and without wrapping. const bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; if (nav_keyboard_active) g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.NavDisableHighlight == true && g.ActiveId == 0) ? 0 : +1; else g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; - ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_Tabbing | ImGuiNavMoveFlags_Activate; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_Activate; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; ImGuiDir clip_dir = (g.NavTabbingDir < 0) ? ImGuiDir_Up : ImGuiDir_Down; NavMoveRequestSubmit(ImGuiDir_None, clip_dir, move_flags, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. @@ -11732,7 +12036,7 @@ void ImGui::NavMoveRequestApplyResult() ImGuiNavItemData* result = (g.NavMoveResultLocal.ID != 0) ? &g.NavMoveResultLocal : (g.NavMoveResultOther.ID != 0) ? &g.NavMoveResultOther : NULL; // Tabbing forward wrap - if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && result == NULL) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && result == NULL) if ((g.NavTabbingCounter == 1 || g.NavTabbingDir == 0) && g.NavTabbingResultFirst.ID) result = &g.NavTabbingResultFirst; @@ -11740,7 +12044,7 @@ void ImGui::NavMoveRequestApplyResult() const ImGuiAxis axis = (g.NavMoveDir == ImGuiDir_Up || g.NavMoveDir == ImGuiDir_Down) ? ImGuiAxis_Y : ImGuiAxis_X; if (result == NULL) { - if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) + if (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavHighlight; if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavHighlight) == 0) NavRestoreHighlightAfterMove(); @@ -11778,12 +12082,15 @@ void ImGui::NavMoveRequestApplyResult() { IMGUI_DEBUG_LOG_FOCUS("[focus] NavMoveRequest: SetNavWindow(\"%s\")\n", result->Window->Name); g.NavWindow = result->Window; + g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; } if (g.ActiveId != result->ID) ClearActiveID(); - if (g.NavId != result->ID && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSelect) == 0) + + // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) + // PageUp/PageDown however sets always set NavJustMovedTo (vs Home/End which doesn't) mimicking Windows behavior. + if ((g.NavId != result->ID || (g.NavMoveFlags & ImGuiNavMoveFlags_IsPageMove)) && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSelect) == 0) { - // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) g.NavJustMovedToId = result->ID; g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = g.NavMoveKeyMods; @@ -11793,17 +12100,19 @@ void ImGui::NavMoveRequestApplyResult() IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequest: result NavID 0x%08X in Layer %d Window \"%s\"\n", result->ID, g.NavLayer, g.NavWindow->Name); ImVec2 preferred_scoring_pos_rel = g.NavWindow->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer]; SetNavID(result->ID, g.NavLayer, result->FocusScopeId, result->RectRel); + if (result->SelectionUserData != ImGuiSelectionUserData_Invalid) + g.NavLastValidSelectionUserData = result->SelectionUserData; // Restore last preferred position for current axis // (storing in RootWindowForNav-> as the info is desirable at the beginning of a Move Request. In theory all storage should use RootWindowForNav..) - if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) == 0) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) == 0) { preferred_scoring_pos_rel[axis] = result->RectRel.GetCenter()[axis]; g.NavWindow->RootWindowForNav->NavPreferredScoringPosRel[g.NavLayer] = preferred_scoring_pos_rel; } // Tabbing: Activates Inputable, otherwise only Focus - if ((g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) && (result->InFlags & ImGuiItemFlags_Inputable) == 0) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && (result->InFlags & ImGuiItemFlags_Inputable) == 0) g.NavMoveFlags &= ~ImGuiNavMoveFlags_Activate; // Activate @@ -11811,9 +12120,8 @@ void ImGui::NavMoveRequestApplyResult() { g.NavNextActivateId = result->ID; g.NavNextActivateFlags = ImGuiActivateFlags_None; - g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavHighlight; - if (g.NavMoveFlags & ImGuiNavMoveFlags_Tabbing) - g.NavNextActivateFlags |= ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState; + if (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) + g.NavNextActivateFlags |= ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState | ImGuiActivateFlags_FromTabbing; } // Enable nav highlight @@ -11912,14 +12220,14 @@ static float ImGui::NavUpdatePageUpPageDown() nav_scoring_rect_offset_y = -page_offset_y; g.NavMoveDir = ImGuiDir_Down; // Because our scoring rect is offset up, we request the down direction (so we can always land on the last item) g.NavMoveClipDir = ImGuiDir_Up; - g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet | ImGuiNavMoveFlags_IsPageMove; } else if (IsKeyPressed(ImGuiKey_PageDown, true)) { nav_scoring_rect_offset_y = +page_offset_y; g.NavMoveDir = ImGuiDir_Up; // Because our scoring rect is offset down, we request the up direction (so we can always land on the last item) g.NavMoveClipDir = ImGuiDir_Down; - g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet; + g.NavMoveFlags = ImGuiNavMoveFlags_AllowCurrentNavId | ImGuiNavMoveFlags_AlsoScoreVisibleSet | ImGuiNavMoveFlags_IsPageMove; } else if (home_pressed) { @@ -12084,6 +12392,7 @@ static void ImGui::NavUpdateWindowing() } // Start CTRL+Tab or Square+L/R window selection + // (g.ConfigNavWindowingKeyNext/g.ConfigNavWindowingKeyPrev defaults are ImGuiMod_Ctrl|ImGuiKey_Tab and ImGuiMod_Ctrl|ImGuiMod_Shift|ImGuiKey_Tab) const ImGuiID owner_id = ImHashStr("###NavUpdateWindowing"); const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; @@ -12185,7 +12494,7 @@ static void ImGui::NavUpdateWindowing() const float move_step = NAV_MOVE_SPEED * io.DeltaTime * ImMin(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); g.NavWindowingAccumDeltaPos += nav_move_dir * move_step; g.NavDisableMouseHover = true; - ImVec2 accum_floored = ImFloor(g.NavWindowingAccumDeltaPos); + ImVec2 accum_floored = ImTrunc(g.NavWindowingAccumDeltaPos); if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) { ImGuiWindow* moving_window = g.NavWindowingTarget->RootWindow; @@ -12317,6 +12626,14 @@ void ImGui::ClearDragDrop() memset(&g.DragDropPayloadBufLocal, 0, sizeof(g.DragDropPayloadBufLocal)); } +bool ImGui::BeginTooltipHidden() +{ + ImGuiContext& g = *GImGui; + bool ret = Begin("##Tooltip_Hidden", NULL, ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize); + SetWindowHiddenAndSkipItemsForCurrentFrame(g.CurrentWindow); + return ret; +} + // When this returns true you need to: a) call SetDragDropPayload() exactly once, b) you may render the payload visual/description, c) call EndDragDropSource() // If the item has an identifier: // - This assume/require the item to be activated (typically via ButtonBehavior). @@ -12419,12 +12736,13 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) { // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. - bool ret = BeginTooltip(); + bool ret; + if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) + ret = BeginTooltipHidden(); + else + ret = BeginTooltip(); IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddendAndSkipItemsForCurrentFrame(). IM_UNUSED(ret); - - if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) - SetWindowHiddendAndSkipItemsForCurrentFrame(g.CurrentWindow); } if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) @@ -12513,13 +12831,14 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) IM_ASSERT(g.DragDropWithinTarget == false); g.DragDropTargetRect = bb; + g.DragDropTargetClipRect = window->ClipRect; // May want to be overriden by user depending on use case? g.DragDropTargetId = id; g.DragDropWithinTarget = true; return true; } // We don't use BeginDragDropTargetCustom() and duplicate its code because: -// 1) we use LastItemRectHoveredRect which handles items that push a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. +// 1) we use LastItemData's ImGuiItemStatusFlags_HoveredRect which handles items that push a temporarily clip rectangle in their code. Calling BeginDragDropTargetCustom(LastItemRect) would not handle them. // 2) and it's faster. as this code may be very frequently called, we want to early out as fast as we can. // Also note how the HoveredWindow test is positioned differently in both functions (in both functions we optimize for the cheapest early out case) bool ImGui::BeginDragDropTarget() @@ -12547,6 +12866,7 @@ bool ImGui::BeginDragDropTarget() IM_ASSERT(g.DragDropWithinTarget == false); g.DragDropTargetRect = display_rect; + g.DragDropTargetClipRect = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasClipRect) ? g.LastItemData.ClipRect : window->ClipRect; g.DragDropTargetId = id; g.DragDropWithinTarget = true; return true; @@ -12561,7 +12881,6 @@ bool ImGui::IsDragDropPayloadBeingAccepted() const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDropFlags flags) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; ImGuiPayload& payload = g.DragDropPayload; IM_ASSERT(g.DragDropActive); // Not called between BeginDragDropTarget() and EndDragDropTarget() ? IM_ASSERT(payload.DataFrameCount != -1); // Forgot to call EndDragDropTarget() ? @@ -12585,7 +12904,7 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop payload.Preview = was_accepted_previously; flags |= (g.DragDropSourceFlags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect); // Source can also inhibit the preview (useful for external sources that live for 1 frame) if (!(flags & ImGuiDragDropFlags_AcceptNoDrawDefaultRect) && payload.Preview) - window->DrawList->AddRect(r.Min - ImVec2(3.5f,3.5f), r.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); + RenderDragDropTargetRect(r, g.DragDropTargetClipRect); g.DragDropAcceptFrameCount = g.FrameCount; payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting OS window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() @@ -12596,10 +12915,20 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop return &payload; } -// FIXME-DRAGDROP: Settle on a proper default visuals for drop target. -void ImGui::RenderDragDropTargetRect(const ImRect& bb) +// FIXME-STYLE FIXME-DRAGDROP: Settle on a proper default visuals for drop target. +void ImGui::RenderDragDropTargetRect(const ImRect& bb, const ImRect& item_clip_rect) { - GetWindowDrawList()->AddRect(bb.Min - ImVec2(3.5f, 3.5f), bb.Max + ImVec2(3.5f, 3.5f), GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImRect bb_display = bb; + bb_display.ClipWith(item_clip_rect); // Clip THEN expand so we have a way to visualize that target is not entirely visible. + bb_display.Expand(3.5f); + bool push_clip_rect = !window->ClipRect.Contains(bb_display); + if (push_clip_rect) + window->DrawList->PushClipRectFullScreen(); + window->DrawList->AddRect(bb_display.Min, bb_display.Max, GetColorU32(ImGuiCol_DragDropTarget), 0.0f, 0, 2.0f); + if (push_clip_rect) + window->DrawList->PopClipRect(); } const ImGuiPayload* ImGui::GetDragDropPayload() @@ -12947,9 +13276,9 @@ ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name) { ImGuiContext& g = *GImGui; const ImGuiID type_hash = ImHashStr(type_name); - for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - if (g.SettingsHandlers[handler_n].TypeHash == type_hash) - return &g.SettingsHandlers[handler_n]; + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + if (handler.TypeHash == type_hash) + return &handler; return NULL; } @@ -12958,9 +13287,9 @@ void ImGui::ClearIniSettings() { ImGuiContext& g = *GImGui; g.SettingsIniData.clear(); - for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - if (g.SettingsHandlers[handler_n].ClearAllFn) - g.SettingsHandlers[handler_n].ClearAllFn(&g, &g.SettingsHandlers[handler_n]); + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + if (handler.ClearAllFn != NULL) + handler.ClearAllFn(&g, &handler); } void ImGui::LoadIniSettingsFromDisk(const char* ini_filename) @@ -12995,9 +13324,9 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) // Call pre-read handlers // Some types will clear their data (e.g. dock information) some types will allow merge/override (window) - for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - if (g.SettingsHandlers[handler_n].ReadInitFn) - g.SettingsHandlers[handler_n].ReadInitFn(&g, &g.SettingsHandlers[handler_n]); + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + if (handler.ReadInitFn != NULL) + handler.ReadInitFn(&g, &handler); void* entry_data = NULL; ImGuiSettingsHandler* entry_handler = NULL; @@ -13041,9 +13370,9 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size) memcpy(buf, ini_data, ini_size); // Call post-read handlers - for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - if (g.SettingsHandlers[handler_n].ApplyAllFn) - g.SettingsHandlers[handler_n].ApplyAllFn(&g, &g.SettingsHandlers[handler_n]); + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + if (handler.ApplyAllFn != NULL) + handler.ApplyAllFn(&g, &handler); } void ImGui::SaveIniSettingsToDisk(const char* ini_filename) @@ -13069,11 +13398,8 @@ const char* ImGui::SaveIniSettingsToMemory(size_t* out_size) g.SettingsDirtyTimer = 0.0f; g.SettingsIniData.Buf.resize(0); g.SettingsIniData.Buf.push_back(0); - for (int handler_n = 0; handler_n < g.SettingsHandlers.Size; handler_n++) - { - ImGuiSettingsHandler* handler = &g.SettingsHandlers[handler_n]; - handler->WriteAllFn(&g, handler, &g.SettingsIniData); - } + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + handler.WriteAllFn(&g, &handler, &g.SettingsIniData); if (out_size) *out_size = (size_t)g.SettingsIniData.size(); return g.SettingsIniData.c_str(); @@ -13139,8 +13465,8 @@ void ImGui::ClearWindowSettings(const char* name) static void WindowSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandler*) { ImGuiContext& g = *ctx; - for (int i = 0; i != g.Windows.Size; i++) - g.Windows[i]->SettingsOffset = -1; + for (ImGuiWindow* window : g.Windows) + window->SettingsOffset = -1; g.SettingsWindows.clear(); } @@ -13165,6 +13491,7 @@ static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, if (sscanf(line, "Pos=%i,%i", &x, &y) == 2) { settings->Pos = ImVec2ih((short)x, (short)y); } else if (sscanf(line, "Size=%i,%i", &x, &y) == 2) { settings->Size = ImVec2ih((short)x, (short)y); } else if (sscanf(line, "Collapsed=%d", &i) == 1) { settings->Collapsed = (i != 0); } + else if (sscanf(line, "IsChild=%d", &i) == 1) { settings->IsChild = (i != 0); } } // Apply to existing windows (if any) @@ -13185,9 +13512,8 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl // Gather data from windows that were active during this session // (if a window wasn't opened in this session we preserve its settings) ImGuiContext& g = *ctx; - for (int i = 0; i != g.Windows.Size; i++) + for (ImGuiWindow* window : g.Windows) { - ImGuiWindow* window = g.Windows[i]; if (window->Flags & ImGuiWindowFlags_NoSavedSettings) continue; @@ -13200,7 +13526,7 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl IM_ASSERT(settings->ID == window->ID); settings->Pos = ImVec2ih(window->Pos); settings->Size = ImVec2ih(window->SizeFull); - + settings->IsChild = (window->Flags & ImGuiWindowFlags_ChildWindow) != 0; settings->Collapsed = window->Collapsed; settings->WantDelete = false; } @@ -13213,9 +13539,18 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl continue; const char* settings_name = settings->GetName(); buf->appendf("[%s][%s]\n", handler->TypeName, settings_name); - buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y); - buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y); - buf->appendf("Collapsed=%d\n", settings->Collapsed); + if (settings->IsChild) + { + buf->appendf("IsChild=1\n"); + buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y); + } + else + { + buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y); + buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y); + if (settings->Collapsed) + buf->appendf("Collapsed=1\n"); + } buf->append("\n"); } } @@ -13266,10 +13601,8 @@ static void ImGui::UpdateViewportsNewFrame() main_viewport->Pos = ImVec2(0.0f, 0.0f); main_viewport->Size = g.IO.DisplaySize; - for (int n = 0; n < g.Viewports.Size; n++) + for (ImGuiViewportP* viewport : g.Viewports) { - ImGuiViewportP* viewport = g.Viewports[n]; - // Lock down space taken by menu bars and status bars, reset the offset for fucntions like BeginMainMenuBar() to alter them again. viewport->WorkOffsetMin = viewport->BuildWorkOffsetMin; viewport->WorkOffsetMax = viewport->BuildWorkOffsetMax; @@ -13487,16 +13820,15 @@ void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* ImVec2 off = bb.Min - viewport->Pos * scale; float alpha_mul = 1.0f; window->DrawList->AddRectFilled(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul * 0.40f)); - for (int i = 0; i != g.Windows.Size; i++) + for (ImGuiWindow* thumb_window : g.Windows) { - ImGuiWindow* thumb_window = g.Windows[i]; if (!thumb_window->WasActive || (thumb_window->Flags & ImGuiWindowFlags_ChildWindow)) continue; ImRect thumb_r = thumb_window->Rect(); ImRect title_r = thumb_window->TitleBarRect(); - thumb_r = ImRect(ImFloor(off + thumb_r.Min * scale), ImFloor(off + thumb_r.Max * scale)); - title_r = ImRect(ImFloor(off + title_r.Min * scale), ImFloor(off + ImVec2(title_r.Max.x, title_r.Min.y) * scale) + ImVec2(0,5)); // Exaggerate title bar height + thumb_r = ImRect(ImTrunc(off + thumb_r.Min * scale), ImTrunc(off + thumb_r.Max * scale)); + title_r = ImRect(ImTrunc(off + title_r.Min * scale), ImTrunc(off + ImVec2(title_r.Max.x, title_r.Min.y) * scale) + ImVec2(0,5)); // Exaggerate title bar height thumb_r.ClipWithFull(bb); title_r.ClipWithFull(bb); const bool window_is_focused = (g.NavWindow && thumb_window->RootWindowForTitleBarHighlight == g.NavWindow->RootWindowForTitleBarHighlight); @@ -13516,13 +13848,12 @@ static void RenderViewportsThumbnails() // We don't display full monitor bounds (we could, but it often looks awkward), instead we display just enough to cover all of our viewports. float SCALE = 1.0f / 8.0f; ImRect bb_full(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - for (int n = 0; n < g.Viewports.Size; n++) - bb_full.Add(g.Viewports[n]->GetMainRect()); + for (ImGuiViewportP* viewport : g.Viewports) + bb_full.Add(viewport->GetMainRect()); ImVec2 p = window->DC.CursorPos; ImVec2 off = p - bb_full.Min * SCALE; - for (int n = 0; n < g.Viewports.Size; n++) + for (ImGuiViewportP* viewport : g.Viewports) { - ImGuiViewportP* viewport = g.Viewports[n]; ImRect viewport_draw_bb(off + (viewport->Pos) * SCALE, off + (viewport->Pos + viewport->Size) * SCALE); ImGui::DebugRenderViewportThumbnail(window->DrawList, viewport, viewport_draw_bb); } @@ -13532,14 +13863,15 @@ static void RenderViewportsThumbnails() // Draw an arbitrary US keyboard layout to visualize translated keys void ImGui::DebugRenderKeyboardPreview(ImDrawList* draw_list) { - const ImVec2 key_size = ImVec2(35.0f, 35.0f); - const float key_rounding = 3.0f; - const ImVec2 key_face_size = ImVec2(25.0f, 25.0f); - const ImVec2 key_face_pos = ImVec2(5.0f, 3.0f); - const float key_face_rounding = 2.0f; - const ImVec2 key_label_pos = ImVec2(7.0f, 4.0f); + const float scale = ImGui::GetFontSize() / 13.0f; + const ImVec2 key_size = ImVec2(35.0f, 35.0f) * scale; + const float key_rounding = 3.0f * scale; + const ImVec2 key_face_size = ImVec2(25.0f, 25.0f) * scale; + const ImVec2 key_face_pos = ImVec2(5.0f, 3.0f) * scale; + const float key_face_rounding = 2.0f * scale; + const ImVec2 key_label_pos = ImVec2(7.0f, 4.0f) * scale; const ImVec2 key_step = ImVec2(key_size.x - 1.0f, key_size.y - 1.0f); - const float key_row_offset = 9.0f; + const float key_row_offset = 9.0f * scale; ImVec2 board_min = GetCursorScreenPos(); ImVec2 board_max = ImVec2(board_min.x + 3 * key_step.x + 2 * key_row_offset + 10.0f, board_min.y + 3 * key_step.y + 10.0f); @@ -13614,6 +13946,35 @@ void ImGui::DebugTextEncoding(const char* str) EndTable(); } +static void DebugFlashStyleColorStop() +{ + ImGuiContext& g = *GImGui; + if (g.DebugFlashStyleColorIdx != ImGuiCol_COUNT) + g.Style.Colors[g.DebugFlashStyleColorIdx] = g.DebugFlashStyleColorBackup; + g.DebugFlashStyleColorIdx = ImGuiCol_COUNT; +} + +// Flash a given style color for some + inhibit modifications of this color via PushStyleColor() calls. +void ImGui::DebugFlashStyleColor(ImGuiCol idx) +{ + ImGuiContext& g = *GImGui; + DebugFlashStyleColorStop(); + g.DebugFlashStyleColorTime = 0.5f; + g.DebugFlashStyleColorIdx = idx; + g.DebugFlashStyleColorBackup = g.Style.Colors[idx]; +} + +void ImGui::UpdateDebugToolFlashStyleColor() +{ + ImGuiContext& g = *GImGui; + if (g.DebugFlashStyleColorTime <= 0.0f) + return; + ColorConvertHSVtoRGB(cosf(g.DebugFlashStyleColorTime * 6.0f) * 0.5f + 0.5f, 0.5f, 0.5f, g.Style.Colors[g.DebugFlashStyleColorIdx].x, g.Style.Colors[g.DebugFlashStyleColorIdx].y, g.Style.Colors[g.DebugFlashStyleColorIdx].z); + g.Style.Colors[g.DebugFlashStyleColorIdx].w = 1.0f; + if ((g.DebugFlashStyleColorTime -= g.IO.DeltaTime) <= 0.0f) + DebugFlashStyleColorStop(); +} + // Avoid naming collision with imgui_demo.cpp's HelpMarker() for unity builds. static void MetricsHelpMarker(const char* desc) { @@ -13630,9 +13991,8 @@ static void MetricsHelpMarker(const char* desc) // [DEBUG] List fonts in a font atlas and display its texture void ImGui::ShowFontAtlas(ImFontAtlas* atlas) { - for (int i = 0; i < atlas->Fonts.Size; i++) + for (ImFont* font : atlas->Fonts) { - ImFont* font = atlas->Fonts[i]; PushID(font); DebugNodeFont(font); PopID(); @@ -13656,8 +14016,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; if (cfg->ShowDebugLog) ShowDebugLogWindow(&cfg->ShowDebugLog); - if (cfg->ShowStackTool) - ShowStackToolWindow(&cfg->ShowStackTool); + if (cfg->ShowIDStackTool) + ShowIDStackToolWindow(&cfg->ShowIDStackTool); if (!Begin("Dear ImGui Metrics/Debugger", p_open) || GetCurrentWindow()->BeginCount > 1) { @@ -13665,11 +14025,14 @@ void ImGui::ShowMetricsWindow(bool* p_open) return; } + // [DEBUG] Clear debug breaks hooks after exactly one cycle. + DebugBreakClearData(); + // Basic info Text("Dear ImGui %s", GetVersion()); Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); - Text("%d visible windows, %d active allocations", io.MetricsRenderWindows, io.MetricsActiveAllocations); + Text("%d visible windows, %d current allocations", io.MetricsRenderWindows, g.DebugAllocInfo.TotalAllocCount - g.DebugAllocInfo.TotalFreeCount); //SameLine(); if (SmallButton("GC")) { g.GcCompactAll = true; } Separator(); @@ -13698,8 +14061,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) else if (rect_type == TRT_ColumnsRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->MinX, table->InnerClipRect.Min.y, c->MaxX, table->InnerClipRect.Min.y + table_instance->LastOuterHeight); } else if (rect_type == TRT_ColumnsWorkRect) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->WorkRect.Min.y, c->WorkMaxX, table->WorkRect.Max.y); } else if (rect_type == TRT_ColumnsClipRect) { ImGuiTableColumn* c = &table->Columns[n]; return c->ClipRect; } - else if (rect_type == TRT_ColumnsContentHeadersUsed){ ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersUsed, table->InnerClipRect.Min.y + table_instance->LastFirstRowHeight); } // Note: y1/y2 not always accurate - else if (rect_type == TRT_ColumnsContentHeadersIdeal){ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersIdeal, table->InnerClipRect.Min.y + table_instance->LastFirstRowHeight); } + else if (rect_type == TRT_ColumnsContentHeadersUsed){ ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersUsed, table->InnerClipRect.Min.y + table_instance->LastTopHeadersRowHeight); } // Note: y1/y2 not always accurate + else if (rect_type == TRT_ColumnsContentHeadersIdeal){ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXHeadersIdeal, table->InnerClipRect.Min.y + table_instance->LastTopHeadersRowHeight); } else if (rect_type == TRT_ColumnsContentFrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y, c->ContentMaxXFrozen, table->InnerClipRect.Min.y + table_instance->LastFrozenHeight); } else if (rect_type == TRT_ColumnsContentUnfrozen) { ImGuiTableColumn* c = &table->Columns[n]; return ImRect(c->WorkMinX, table->InnerClipRect.Min.y + table_instance->LastFrozenHeight, c->ContentMaxXUnfrozen, table->InnerClipRect.Max.y); } IM_ASSERT(0); @@ -13724,34 +14087,24 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Tools if (TreeNode("Tools")) { - bool show_encoding_viewer = TreeNode("UTF-8 Encoding viewer"); - SameLine(); - MetricsHelpMarker("You can also call ImGui::DebugTextEncoding() from your code with a given string to test that your UTF-8 encoding settings are correct."); - if (show_encoding_viewer) - { - static char buf[100] = ""; - SetNextItemWidth(-FLT_MIN); - InputText("##Text", buf, IM_ARRAYSIZE(buf)); - if (buf[0] != 0) - DebugTextEncoding(buf); - TreePop(); - } - + // Debug Break features // The Item Picker tool is super useful to visually select an item and break into the call-stack of where it was submitted. - if (Checkbox("Show Item Picker", &g.DebugItemPickerActive) && g.DebugItemPickerActive) - DebugStartItemPicker(); + SeparatorTextEx(0, "Debug breaks", NULL, CalcTextSize("(?)").x + g.Style.SeparatorTextPadding.x); SameLine(); MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); + if (Checkbox("Show Item Picker", &g.DebugItemPickerActive) && g.DebugItemPickerActive) + DebugStartItemPicker(); + Checkbox("Show \"Debug Break\" buttons in other sections", &g.IO.ConfigDebugIsDebuggerPresent); + + SeparatorText("Visualize"); - // Stack Tool is your best friend! Checkbox("Show Debug Log", &cfg->ShowDebugLog); SameLine(); MetricsHelpMarker("You can also call ImGui::ShowDebugLogWindow() from your code."); - // Stack Tool is your best friend! - Checkbox("Show Stack Tool", &cfg->ShowStackTool); + Checkbox("Show ID Stack Tool", &cfg->ShowIDStackTool); SameLine(); - MetricsHelpMarker("You can also call ImGui::ShowStackToolWindow() from your code."); + MetricsHelpMarker("You can also call ImGui::ShowIDStackToolWindow() from your code."); Checkbox("Show windows begin order", &cfg->ShowWindowsBeginOrder); Checkbox("Show windows rectangles", &cfg->ShowWindowsRects); @@ -13814,11 +14167,26 @@ void ImGui::ShowMetricsWindow(bool* p_open) Unindent(); } } + Checkbox("Show groups rectangles", &g.DebugShowGroupRects); // Storing in context as this is used by group code and prefers to be in hot-data + + SeparatorText("Validate"); Checkbox("Debug Begin/BeginChild return value", &io.ConfigDebugBeginReturnValueLoop); SameLine(); MetricsHelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running."); + Checkbox("UTF-8 Encoding viewer", &cfg->ShowTextEncodingViewer); + SameLine(); + MetricsHelpMarker("You can also call ImGui::DebugTextEncoding() from your code with a given string to test that your UTF-8 encoding settings are correct."); + if (cfg->ShowTextEncodingViewer) + { + static char buf[64] = ""; + SetNextItemWidth(-FLT_MIN); + InputText("##DebugTextEncodingBuf", buf, IM_ARRAYSIZE(buf)); + if (buf[0] != 0) + DebugTextEncoding(buf); + } + TreePop(); } @@ -13833,9 +14201,9 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Here we display windows in their submitted order/hierarchy, however note that the Begin stack doesn't constitute a Parent<>Child relationship! ImVector& temp_buffer = g.WindowsTempSortBuffer; temp_buffer.resize(0); - for (int i = 0; i < g.Windows.Size; i++) - if (g.Windows[i]->LastFrameActive + 1 >= g.FrameCount) - temp_buffer.push_back(g.Windows[i]); + for (ImGuiWindow* window : g.Windows) + if (window->LastFrameActive + 1 >= g.FrameCount) + temp_buffer.push_back(window); struct Func { static int IMGUI_CDECL WindowComparerByBeginOrder(const void* lhs, const void* rhs) { return ((int)(*(const ImGuiWindow* const *)lhs)->BeginOrderWithinContext - (*(const ImGuiWindow* const*)rhs)->BeginOrderWithinContext); } }; ImQsort(temp_buffer.Data, (size_t)temp_buffer.Size, sizeof(ImGuiWindow*), Func::WindowComparerByBeginOrder); DebugNodeWindowsListByBeginStackParent(temp_buffer.Data, temp_buffer.Size, NULL); @@ -13847,19 +14215,15 @@ void ImGui::ShowMetricsWindow(bool* p_open) // DrawLists int drawlist_count = 0; - for (int viewport_i = 0; viewport_i < g.Viewports.Size; viewport_i++) - drawlist_count += g.Viewports[viewport_i]->DrawDataBuilder.GetDrawListCount(); + for (ImGuiViewportP* viewport : g.Viewports) + drawlist_count += viewport->DrawDataP.CmdLists.Size; if (TreeNode("DrawLists", "DrawLists (%d)", drawlist_count)) { Checkbox("Show ImDrawCmd mesh when hovering", &cfg->ShowDrawCmdMesh); Checkbox("Show ImDrawCmd bounding boxes when hovering", &cfg->ShowDrawCmdBoundingBoxes); - for (int viewport_i = 0; viewport_i < g.Viewports.Size; viewport_i++) - { - ImGuiViewportP* viewport = g.Viewports[viewport_i]; - for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) - for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++) - DebugNodeDrawList(NULL, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList"); - } + for (ImGuiViewportP* viewport : g.Viewports) + for (ImDrawList* draw_list : viewport->DrawDataP.CmdLists) + DebugNodeDrawList(NULL, viewport, draw_list, "DrawList"); TreePop(); } @@ -13869,22 +14233,21 @@ void ImGui::ShowMetricsWindow(bool* p_open) Indent(GetTreeNodeToLabelSpacing()); RenderViewportsThumbnails(); Unindent(GetTreeNodeToLabelSpacing()); - for (int i = 0; i < g.Viewports.Size; i++) - DebugNodeViewport(g.Viewports[i]); + for (ImGuiViewportP* viewport : g.Viewports) + DebugNodeViewport(viewport); TreePop(); } // Details for Popups if (TreeNode("Popups", "Popups (%d)", g.OpenPopupStack.Size)) { - for (int i = 0; i < g.OpenPopupStack.Size; i++) + for (const ImGuiPopupData& popup_data : g.OpenPopupStack) { // As it's difficult to interact with tree nodes while popups are open, we display everything inline. - const ImGuiPopupData* popup_data = &g.OpenPopupStack[i]; - ImGuiWindow* window = popup_data->Window; + ImGuiWindow* window = popup_data.Window; BulletText("PopupID: %08x, Window: '%s' (%s%s), BackupNavWindow '%s', ParentWindow '%s'", - popup_data->PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? "Child;" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? "Menu;" : "", - popup_data->BackupNavWindow ? popup_data->BackupNavWindow->Name : "NULL", window && window->ParentWindow ? window->ParentWindow->Name : "NULL"); + popup_data.PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? "Child;" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? "Menu;" : "", + popup_data.BackupNavWindow ? popup_data.BackupNavWindow->Name : "NULL", window && window->ParentWindow ? window->ParentWindow->Name : "NULL"); } TreePop(); } @@ -13926,6 +14289,13 @@ void ImGui::ShowMetricsWindow(bool* p_open) TreePop(); } + // Details for TypingSelect + if (TreeNode("TypingSelect", "TypingSelect (%d)", g.TypingSelectState.SearchBuffer[0] != 0 ? 1 : 0)) + { + DebugNodeTypingSelectState(&g.TypingSelectState); + TreePop(); + } + // Details for Docking #ifdef IMGUI_HAS_DOCK if (TreeNode("Docking")) @@ -13954,8 +14324,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("SettingsDirtyTimer %.2f", g.SettingsDirtyTimer); if (TreeNode("SettingsHandlers", "Settings handlers: (%d)", g.SettingsHandlers.Size)) { - for (int n = 0; n < g.SettingsHandlers.Size; n++) - BulletText("\"%s\"", g.SettingsHandlers[n].TypeName); + for (ImGuiSettingsHandler& handler : g.SettingsHandlers) + BulletText("\"%s\"", handler.TypeName); TreePop(); } if (TreeNode("SettingsWindows", "Settings packed data: Windows: %d bytes", g.SettingsWindows.size())) @@ -13983,6 +14353,21 @@ void ImGui::ShowMetricsWindow(bool* p_open) TreePop(); } + // Settings + if (TreeNode("Memory allocations")) + { + ImGuiDebugAllocInfo* info = &g.DebugAllocInfo; + Text("%d current allocations", info->TotalAllocCount - info->TotalFreeCount); + Text("Recent frames with allocations:"); + int buf_size = IM_ARRAYSIZE(info->LastEntriesBuf); + for (int n = buf_size - 1; n >= 0; n--) + { + ImGuiDebugAllocEntry* entry = &info->LastEntriesBuf[(info->LastEntriesIdx - n + buf_size) % buf_size]; + BulletText("Frame %06d: %+3d ( %2d malloc, %2d free )%s", entry->FrameCount, entry->AllocCount - entry->FreeCount, entry->AllocCount, entry->FreeCount, (n == 0) ? " (most recent)" : ""); + } + TreePop(); + } + if (TreeNode("Inputs")) { Text("KEYBOARD/GAMEPAD/MOUSE KEYS"); @@ -13993,7 +14378,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; #else - struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key >= 0 && key < 512 && GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array //Text("Legacy raw:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key++) { if (io.KeysDown[key]) { SameLine(); Text("\"%s\" %d", GetKeyName(key), key); } } #endif Text("Keys down:"); for (ImGuiKey key = ImGuiKey_KeysData_OFFSET; key < ImGuiKey_COUNT; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !IsKeyDown(key)) continue; SameLine(); Text(IsNamedKey(key) ? "\"%s\"" : "\"%s\" %d", GetKeyName(key), key); SameLine(); Text("(%.02f)", GetKeyData(key)->DownDuration); } @@ -14036,8 +14421,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("KEY OWNERS"); { Indent(); - if (BeginListBox("##owners", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 6))) - { + if (BeginChild("##owners", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY, ImGuiWindowFlags_NoSavedSettings)) for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); @@ -14047,15 +14431,15 @@ void ImGui::ShowMetricsWindow(bool* p_open) owner_data->LockUntilRelease ? " LockUntilRelease" : owner_data->LockThisFrame ? " LockThisFrame" : ""); DebugLocateItemOnHover(owner_data->OwnerCurr); } - EndListBox(); - } + EndChild(); Unindent(); } Text("SHORTCUT ROUTING"); + SameLine(); + MetricsHelpMarker("Declared shortcut routes automatically set key owner when mods matches."); { Indent(); - if (BeginListBox("##routes", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 6))) - { + if (BeginChild("##routes", ImVec2(-FLT_MIN, GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY, ImGuiWindowFlags_NoSavedSettings)) for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { ImGuiKeyRoutingTable* rt = &g.KeysRoutingTable; @@ -14063,14 +14447,19 @@ void ImGui::ShowMetricsWindow(bool* p_open) { char key_chord_name[64]; ImGuiKeyRoutingData* routing_data = &rt->Entries[idx]; - GetKeyChordName(key | routing_data->Mods, key_chord_name, IM_ARRAYSIZE(key_chord_name)); - Text("%s: 0x%08X", key_chord_name, routing_data->RoutingCurr); + ImGuiKeyChord key_chord = key | routing_data->Mods; + Text("%s: 0x%08X", GetKeyChordName(key_chord, key_chord_name, IM_ARRAYSIZE(key_chord_name)), routing_data->RoutingCurr); DebugLocateItemOnHover(routing_data->RoutingCurr); + if (g.IO.ConfigDebugIsDebuggerPresent) + { + SameLine(); + if (DebugBreakButton("**DebugBreak**", "in SetShortcutRouting() for this KeyChord")) + g.DebugBreakInShortcutRouting = key_chord; + } idx = routing_data->NextEntryIndex; } } - EndListBox(); - } + EndChild(); Text("(ActiveIdUsing: AllKeyboardKeys: %d, NavDirMask: 0x%X)", g.ActiveIdUsingAllKeyboardKeys, g.ActiveIdUsingNavDirMask); Unindent(); } @@ -14105,6 +14494,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("NavId: 0x%08X, NavLayer: %d", g.NavId, g.NavLayer); DebugLocateItemOnHover(g.NavId); Text("NavInputSource: %s", GetInputSourceName(g.NavInputSource)); + Text("NavLastValidSelectionUserData = %" IM_PRId64 " (0x%" IM_PRIX64 ")", g.NavLastValidSelectionUserData, g.NavLastValidSelectionUserData); Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); Text("NavActivateId/DownId/PressedId: %08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId); Text("NavActivateFlags: %04X", g.NavActivateFlags); @@ -14119,9 +14509,8 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Overlay: Display windows Rectangles and Begin Order if (cfg->ShowWindowsRects || cfg->ShowWindowsBeginOrder) { - for (int n = 0; n < g.Windows.Size; n++) + for (ImGuiWindow* window : g.Windows) { - ImGuiWindow* window = g.Windows[n]; if (!window->WasActive) continue; ImDrawList* draw_list = GetForegroundDrawList(window); @@ -14178,21 +14567,88 @@ void ImGui::ShowMetricsWindow(bool* p_open) End(); } +void ImGui::DebugBreakClearData() +{ + // Those fields are scattered in their respective subsystem to stay in hot-data locations + ImGuiContext& g = *GImGui; + g.DebugBreakInWindow = 0; + g.DebugBreakInTable = 0; + g.DebugBreakInShortcutRouting = ImGuiKey_None; +} + +void ImGui::DebugBreakButtonTooltip(bool keyboard_only, const char* description_of_location) +{ + if (!BeginItemTooltip()) + return; + Text("To call IM_DEBUG_BREAK() %s:", description_of_location); + Separator(); + TextUnformatted(keyboard_only ? "- Press 'Pause/Break' on keyboard." : "- Press 'Pause/Break' on keyboard.\n- or Click (may alter focus/active id).\n- or navigate using keyboard and press space."); + Separator(); + TextUnformatted("Choose one way that doesn't interfere with what you are trying to debug!\nYou need a debugger attached or this will crash!"); + EndTooltip(); +} + +// Special button that doesn't take focus, doesn't take input owner, and can be activated without a click etc. +// In order to reduce interferences with the contents we are trying to debug into. +bool ImGui::DebugBreakButton(const char* label, const char* description_of_location) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiID id = window->GetID(label); + const ImVec2 label_size = CalcTextSize(label, NULL, true); + ImVec2 pos = window->DC.CursorPos + ImVec2(0.0f, window->DC.CurrLineTextBaseOffset); + ImVec2 size = ImVec2(label_size.x + g.Style.FramePadding.x * 2.0f, label_size.y); + + const ImRect bb(pos, pos + size); + ItemSize(size, 0.0f); + if (!ItemAdd(bb, id)) + return false; + + // WE DO NOT USE ButtonEx() or ButtonBehavior() in order to reduce our side-effects. + bool hovered = ItemHoverable(bb, id, g.CurrentItemFlags); + bool pressed = hovered && (IsKeyChordPressed(g.DebugBreakKeyChord) || IsMouseClicked(0) || g.NavActivateId == id); + DebugBreakButtonTooltip(false, description_of_location); + + ImVec4 col4f = GetStyleColorVec4(hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); + ImVec4 hsv; + ColorConvertRGBtoHSV(col4f.x, col4f.y, col4f.z, hsv.x, hsv.y, hsv.z); + ColorConvertHSVtoRGB(hsv.x + 0.20f, hsv.y, hsv.z, col4f.x, col4f.y, col4f.z); + + RenderNavHighlight(bb, id); + RenderFrame(bb.Min, bb.Max, GetColorU32(col4f), true, g.Style.FrameRounding); + RenderTextClipped(bb.Min, bb.Max, label, NULL, &label_size, g.Style.ButtonTextAlign, &bb); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + return pressed; +} + // [DEBUG] Display contents of Columns void ImGui::DebugNodeColumns(ImGuiOldColumns* columns) { if (!TreeNode((void*)(uintptr_t)columns->ID, "Columns Id: 0x%08X, Count: %d, Flags: 0x%04X", columns->ID, columns->Count, columns->Flags)) return; BulletText("Width: %.1f (MinX: %.1f, MaxX: %.1f)", columns->OffMaxX - columns->OffMinX, columns->OffMinX, columns->OffMaxX); - for (int column_n = 0; column_n < columns->Columns.Size; column_n++) - BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", column_n, columns->Columns[column_n].OffsetNorm, GetColumnOffsetFromNorm(columns, columns->Columns[column_n].OffsetNorm)); + for (ImGuiOldColumnData& column : columns->Columns) + BulletText("Column %02d: OffsetNorm %.3f (= %.1f px)", (int)columns->Columns.index_from_ptr(&column), column.OffsetNorm, GetColumnOffsetFromNorm(columns, column.OffsetNorm)); TreePop(); } +static void FormatTextureIDForDebugDisplay(char* buf, int buf_size, ImTextureID tex_id) +{ + if (sizeof(tex_id) >= sizeof(void*)) + ImFormatString(buf, buf_size, "0x%p", (void*)*(intptr_t*)(void*)&tex_id); + else + ImFormatString(buf, buf_size, "0x%04X", *(int*)(void*)&tex_id); +} + // [DEBUG] Display contents of ImDrawList -void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, const char* label) +void ImGui::DebugNodeDrawList(ImGuiWindow* window, ImGuiViewportP* viewport, const ImDrawList* draw_list, const char* label) { ImGuiContext& g = *GImGui; + IM_UNUSED(viewport); // Used in docking branch ImGuiMetricsConfig* cfg = &g.DebugMetricsConfig; int cmd_count = draw_list->CmdBuffer.Size; if (cmd_count > 0 && draw_list->CmdBuffer.back().ElemCount == 0 && draw_list->CmdBuffer.back().UserCallback == NULL) @@ -14224,10 +14680,11 @@ void ImGui::DebugNodeDrawList(ImGuiWindow* window, const ImDrawList* draw_list, continue; } + char texid_desc[20]; + FormatTextureIDForDebugDisplay(texid_desc, IM_ARRAYSIZE(texid_desc), pcmd->TextureId); char buf[300]; - ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d tris, Tex 0x%p, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", - pcmd->ElemCount / 3, (void*)(intptr_t)pcmd->TextureId, - pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); + ImFormatString(buf, IM_ARRAYSIZE(buf), "DrawCmd:%5d tris, Tex %s, ClipRect (%4.0f,%4.0f)-(%4.0f,%4.0f)", + pcmd->ElemCount / 3, texid_desc, pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w); bool pcmd_node_open = TreeNode((void*)(pcmd - draw_list->CmdBuffer.begin()), "%s", buf); if (IsItemHovered() && (cfg->ShowDrawCmdMesh || cfg->ShowDrawCmdBoundingBoxes) && fg_draw_list) DebugNodeDrawCmdShowMeshAndBoundingBox(fg_draw_list, draw_list, pcmd, cfg->ShowDrawCmdMesh, cfg->ShowDrawCmdBoundingBoxes); @@ -14307,8 +14764,8 @@ void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList* out_draw_list, co // Draw bounding boxes if (show_aabb) { - out_draw_list->AddRect(ImFloor(clip_rect.Min), ImFloor(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU - out_draw_list->AddRect(ImFloor(vtxs_rect.Min), ImFloor(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles + out_draw_list->AddRect(ImTrunc(clip_rect.Min), ImTrunc(clip_rect.Max), IM_COL32(255, 0, 255, 255)); // In pink: clipping rectangle submitted to GPU + out_draw_list->AddRect(ImTrunc(vtxs_rect.Min), ImTrunc(vtxs_rect.Max), IM_COL32(0, 255, 255, 255)); // In cyan: bounding box of triangles } out_draw_list->Flags = backup_flags; } @@ -14419,11 +14876,8 @@ void ImGui::DebugNodeStorage(ImGuiStorage* storage, const char* label) { if (!TreeNode(label, "%s: %d entries, %d bytes", label, storage->Data.Size, storage->Data.size_in_bytes())) return; - for (int n = 0; n < storage->Data.Size; n++) - { - const ImGuiStorage::ImGuiStoragePair& p = storage->Data[n]; + for (const ImGuiStorage::ImGuiStoragePair& p : storage->Data) BulletText("Key 0x%08X Value { i: %d }", p.key, p.val_i); // Important: we currently don't store a type, real value may not be integer. - } TreePop(); } @@ -14481,9 +14935,8 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport) (flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", (flags & ImGuiViewportFlags_IsPlatformMonitor) ? " IsPlatformMonitor" : "", (flags & ImGuiViewportFlags_OwnedByApp) ? " OwnedByApp" : ""); - for (int layer_i = 0; layer_i < IM_ARRAYSIZE(viewport->DrawDataBuilder.Layers); layer_i++) - for (int draw_list_i = 0; draw_list_i < viewport->DrawDataBuilder.Layers[layer_i].Size; draw_list_i++) - DebugNodeDrawList(NULL, viewport->DrawDataBuilder.Layers[layer_i][draw_list_i], "DrawList"); + for (ImDrawList* draw_list : viewport->DrawDataP.CmdLists) + DebugNodeDrawList(NULL, viewport, draw_list, "DrawList"); TreePop(); } } @@ -14510,8 +14963,11 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) if (window->MemoryCompacted) TextDisabled("Note: some memory buffers have been compacted/freed."); + if (g.IO.ConfigDebugIsDebuggerPresent && DebugBreakButton("**DebugBreak**", "in Begin()")) + g.DebugBreakInWindow = window->ID; + ImGuiWindowFlags flags = window->Flags; - DebugNodeDrawList(window, window->DrawList, "DrawList"); + DebugNodeDrawList(window, window->Viewport, window->DrawList, "DrawList"); BulletText("Pos: (%.1f,%.1f), Size: (%.1f,%.1f), ContentSize (%.1f,%.1f) Ideal (%.1f,%.1f)", window->Pos.x, window->Pos.y, window->Size.x, window->Size.y, window->ContentSize.x, window->ContentSize.y, window->ContentSizeIdeal.x, window->ContentSizeIdeal.y); BulletText("Flags: 0x%08X (%s%s%s%s%s%s%s%s%s..)", flags, (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", @@ -14538,8 +14994,8 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) if (window->DC.ChildWindows.Size > 0) { DebugNodeWindowsList(&window->DC.ChildWindows, "ChildWindows"); } if (window->ColumnsStorage.Size > 0 && TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) { - for (int n = 0; n < window->ColumnsStorage.Size; n++) - DebugNodeColumns(&window->ColumnsStorage[n]); + for (ImGuiOldColumns& columns : window->ColumnsStorage) + DebugNodeColumns(&columns); TreePop(); } DebugNodeStorage(&window->StateStorage, "Storage"); @@ -14605,9 +15061,36 @@ void ImGui::DebugLogV(const char* fmt, va_list args) const int old_size = g.DebugLogBuf.size(); g.DebugLogBuf.appendf("[%05d] ", g.FrameCount); g.DebugLogBuf.appendfv(fmt, args); + g.DebugLogIndex.append(g.DebugLogBuf.c_str(), old_size, g.DebugLogBuf.size()); if (g.DebugLogFlags & ImGuiDebugLogFlags_OutputToTTY) IMGUI_DEBUG_PRINTF("%s", g.DebugLogBuf.begin() + old_size); - g.DebugLogIndex.append(g.DebugLogBuf.c_str(), old_size, g.DebugLogBuf.size()); +#ifdef IMGUI_ENABLE_TEST_ENGINE + if (g.DebugLogFlags & ImGuiDebugLogFlags_OutputToTestEngine) + IMGUI_TEST_ENGINE_LOG("%s", g.DebugLogBuf.begin() + old_size); +#endif +} + +// FIXME-LAYOUT: To be done automatically via layout mode once we rework ItemSize/ItemAdd into ItemLayout. +static void SameLineOrWrap(const ImVec2& size) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 pos(window->DC.CursorPosPrevLine.x + g.Style.ItemSpacing.x, window->DC.CursorPosPrevLine.y); + if (window->ClipRect.Contains(ImRect(pos, pos + size))) + ImGui::SameLine(); +} + +static void ShowDebugLogFlag(const char* name, ImGuiDebugLogFlags flags) +{ + ImGuiContext& g = *GImGui; + ImVec2 size(ImGui::GetFrameHeight() + g.Style.ItemInnerSpacing.x + ImGui::CalcTextSize(name).x, ImGui::GetFrameHeight()); + SameLineOrWrap(size); // FIXME-LAYOUT: To be done automatically once we rework ItemSize/ItemAdd into ItemLayout. + if (ImGui::CheckboxFlags(name, &g.DebugLogFlags, flags) && g.IO.KeyShift && (g.DebugLogFlags & flags) != 0) + { + g.DebugLogAutoDisableFrames = 2; + g.DebugLogAutoDisableFlags |= flags; + } + ImGui::SetItemTooltip("Hold SHIFT when clicking to enable for 2 frames only (useful for spammy log entries)"); } void ImGui::ShowDebugLogWindow(bool* p_open) @@ -14622,13 +15105,13 @@ void ImGui::ShowDebugLogWindow(bool* p_open) } CheckboxFlags("All", &g.DebugLogFlags, ImGuiDebugLogFlags_EventMask_); - SameLine(); CheckboxFlags("ActiveId", &g.DebugLogFlags, ImGuiDebugLogFlags_EventActiveId); - SameLine(); CheckboxFlags("Focus", &g.DebugLogFlags, ImGuiDebugLogFlags_EventFocus); - SameLine(); CheckboxFlags("Popup", &g.DebugLogFlags, ImGuiDebugLogFlags_EventPopup); - SameLine(); CheckboxFlags("Nav", &g.DebugLogFlags, ImGuiDebugLogFlags_EventNav); - SameLine(); if (CheckboxFlags("Clipper", &g.DebugLogFlags, ImGuiDebugLogFlags_EventClipper)) { g.DebugLogClipperAutoDisableFrames = 2; } if (IsItemHovered()) SetTooltip("Clipper log auto-disabled after 2 frames"); - //SameLine(); CheckboxFlags("Selection", &g.DebugLogFlags, ImGuiDebugLogFlags_EventSelection); - SameLine(); CheckboxFlags("IO", &g.DebugLogFlags, ImGuiDebugLogFlags_EventIO); + ShowDebugLogFlag("ActiveId", ImGuiDebugLogFlags_EventActiveId); + ShowDebugLogFlag("Clipper", ImGuiDebugLogFlags_EventClipper); + ShowDebugLogFlag("Focus", ImGuiDebugLogFlags_EventFocus); + ShowDebugLogFlag("IO", ImGuiDebugLogFlags_EventIO); + ShowDebugLogFlag("Nav", ImGuiDebugLogFlags_EventNav); + ShowDebugLogFlag("Popup", ImGuiDebugLogFlags_EventPopup); + //ShowDebugLogFlag("Selection", ImGuiDebugLogFlags_EventSelection); if (SmallButton("Clear")) { @@ -14638,7 +15121,10 @@ void ImGui::ShowDebugLogWindow(bool* p_open) SameLine(); if (SmallButton("Copy")) SetClipboardText(g.DebugLogBuf.c_str()); - BeginChild("##log", ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); + BeginChild("##log", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); + + const ImGuiDebugLogFlags backup_log_flags = g.DebugLogFlags; + g.DebugLogFlags &= ~ImGuiDebugLogFlags_EventClipper; ImGuiListClipper clipper; clipper.Begin(g.DebugLogIndex.size()); @@ -14647,10 +15133,10 @@ void ImGui::ShowDebugLogWindow(bool* p_open) { const char* line_begin = g.DebugLogIndex.get_line_begin(g.DebugLogBuf.c_str(), line_no); const char* line_end = g.DebugLogIndex.get_line_end(g.DebugLogBuf.c_str(), line_no); - TextUnformatted(line_begin, line_end); + TextUnformatted(line_begin, line_end); // Display line ImRect text_rect = g.LastItemData.Rect; if (IsItemHovered()) - for (const char* p = line_begin; p <= line_end - 10; p++) + for (const char* p = line_begin; p <= line_end - 10; p++) // Search for 0x???????? identifiers { ImGuiID id = 0; if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X') || sscanf(p + 2, "%X", &id) != 1) @@ -14663,6 +15149,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) p += 10; } } + g.DebugLogFlags = backup_log_flags; if (GetScrollY() >= GetScrollMaxY()) SetScrollHereY(1.0f); EndChild(); @@ -14671,9 +15158,41 @@ void ImGui::ShowDebugLogWindow(bool* p_open) } //----------------------------------------------------------------------------- -// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, STACK TOOL) +// [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, ID STACK TOOL) //----------------------------------------------------------------------------- +// Draw a small cross at current CursorPos in current window's DrawList +void ImGui::DebugDrawCursorPos(ImU32 col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImVec2 pos = window->DC.CursorPos; + window->DrawList->AddLine(ImVec2(pos.x, pos.y - 3.0f), ImVec2(pos.x, pos.y + 4.0f), col, 1.0f); + window->DrawList->AddLine(ImVec2(pos.x - 3.0f, pos.y), ImVec2(pos.x + 4.0f, pos.y), col, 1.0f); +} + +// Draw a 10px wide rectangle around CurposPos.x using Line Y1/Y2 in current window's DrawList +void ImGui::DebugDrawLineExtents(ImU32 col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + float curr_x = window->DC.CursorPos.x; + float line_y1 = (window->DC.IsSameLine ? window->DC.CursorPosPrevLine.y : window->DC.CursorPos.y); + float line_y2 = line_y1 + (window->DC.IsSameLine ? window->DC.PrevLineSize.y : window->DC.CurrLineSize.y); + window->DrawList->AddLine(ImVec2(curr_x - 5.0f, line_y1), ImVec2(curr_x + 5.0f, line_y1), col, 1.0f); + window->DrawList->AddLine(ImVec2(curr_x - 0.5f, line_y1), ImVec2(curr_x - 0.5f, line_y2), col, 1.0f); + window->DrawList->AddLine(ImVec2(curr_x - 5.0f, line_y2), ImVec2(curr_x + 5.0f, line_y2), col, 1.0f); +} + +// Draw last item rect in ForegroundDrawList (so it is always visible) +void ImGui::DebugDrawItemRect(ImU32 col) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + GetForegroundDrawList(window)->AddRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, col); +} + +// [DEBUG] Locate item position/rectangle given an ID. static const ImU32 DEBUG_LOCATE_ITEM_COLOR = IM_COL32(0, 255, 0, 255); // Green void ImGui::DebugLocateItem(ImGuiID target_id) @@ -14681,6 +15200,7 @@ void ImGui::DebugLocateItem(ImGuiID target_id) ImGuiContext& g = *GImGui; g.DebugLocateId = target_id; g.DebugLocateFrames = 2; + g.DebugBreakInLocateId = false; } void ImGui::DebugLocateItemOnHover(ImGuiID target_id) @@ -14690,11 +15210,24 @@ void ImGui::DebugLocateItemOnHover(ImGuiID target_id) ImGuiContext& g = *GImGui; DebugLocateItem(target_id); GetForegroundDrawList(g.CurrentWindow)->AddRect(g.LastItemData.Rect.Min - ImVec2(3.0f, 3.0f), g.LastItemData.Rect.Max + ImVec2(3.0f, 3.0f), DEBUG_LOCATE_ITEM_COLOR); + + // Can't easily use a context menu here because it will mess with focus, active id etc. + if (g.IO.ConfigDebugIsDebuggerPresent && g.MouseStationaryTimer > 1.0f) + { + DebugBreakButtonTooltip(false, "in ItemAdd()"); + if (IsKeyChordPressed(g.DebugBreakKeyChord)) + g.DebugBreakInLocateId = true; + } } void ImGui::DebugLocateItemResolveWithLastItem() { ImGuiContext& g = *GImGui; + + // [DEBUG] Debug break requested by user + if (g.DebugBreakInLocateId) + IM_DEBUG_BREAK(); + ImGuiLastItemData item_data = g.LastItemData; g.DebugLocateId = 0; ImDrawList* draw_list = GetForegroundDrawList(g.CurrentWindow); @@ -14740,13 +15273,13 @@ void ImGui::UpdateDebugToolItemPicker() EndTooltip(); } -// [DEBUG] Stack Tool: update queries. Called by NewFrame() +// [DEBUG] ID Stack Tool: update queries. Called by NewFrame() void ImGui::UpdateDebugToolStackQueries() { ImGuiContext& g = *GImGui; - ImGuiStackTool* tool = &g.DebugStackTool; + ImGuiIDStackTool* tool = &g.DebugIDStackTool; - // Clear hook when stack tool is not visible + // Clear hook when id stack tool is not visible g.DebugHookIdInfo = 0; if (g.FrameCount != tool->LastActiveFrame + 1) return; @@ -14780,12 +15313,12 @@ void ImGui::UpdateDebugToolStackQueries() } } -// [DEBUG] Stack tool: hooks called by GetID() family functions +// [DEBUG] ID Stack tool: hooks called by GetID() family functions void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImGuiStackTool* tool = &g.DebugStackTool; + ImGuiIDStackTool* tool = &g.DebugIDStackTool; // Step 0: stack query // This assumes that the ID was computed with the current ID stack, which tends to be the case for our widget. @@ -14828,7 +15361,7 @@ void ImGui::DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* dat info->DataType = data_type; } -static int StackToolFormatLevelInfo(ImGuiStackTool* tool, int n, bool format_for_ui, char* buf, size_t buf_size) +static int StackToolFormatLevelInfo(ImGuiIDStackTool* tool, int n, bool format_for_ui, char* buf, size_t buf_size) { ImGuiStackLevelInfo* info = &tool->Results[n]; ImGuiWindow* window = (info->Desc[0] == 0 && n == 0) ? ImGui::FindWindowByID(info->ID) : NULL; @@ -14845,20 +15378,20 @@ static int StackToolFormatLevelInfo(ImGuiStackTool* tool, int n, bool format_for return ImFormatString(buf, buf_size, "???"); } -// Stack Tool: Display UI -void ImGui::ShowStackToolWindow(bool* p_open) +// ID Stack Tool: Display UI +void ImGui::ShowIDStackToolWindow(bool* p_open) { ImGuiContext& g = *GImGui; if (!(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize)) SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 8.0f), ImGuiCond_FirstUseEver); - if (!Begin("Dear ImGui Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1) + if (!Begin("Dear ImGui ID Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1) { End(); return; } // Display hovered/active status - ImGuiStackTool* tool = &g.DebugStackTool; + ImGuiIDStackTool* tool = &g.DebugIDStackTool; const ImGuiID hovered_id = g.HoveredIdPreviousFrame; const ImGuiID active_id = g.ActiveId; #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -14927,7 +15460,7 @@ void ImGui::ShowStackToolWindow(bool* p_open) void ImGui::ShowMetricsWindow(bool*) {} void ImGui::ShowFontAtlas(ImFontAtlas*) {} void ImGui::DebugNodeColumns(ImGuiOldColumns*) {} -void ImGui::DebugNodeDrawList(ImGuiWindow*, const ImDrawList*, const char*) {} +void ImGui::DebugNodeDrawList(ImGuiWindow*, ImGuiViewportP*, const ImDrawList*, const char*) {} void ImGui::DebugNodeDrawCmdShowMeshAndBoundingBox(ImDrawList*, const ImDrawList*, const ImDrawCmd*, bool, bool) {} void ImGui::DebugNodeFont(ImFont*) {} void ImGui::DebugNodeStorage(ImGuiStorage*, const char*) {} @@ -14940,10 +15473,11 @@ void ImGui::DebugNodeViewport(ImGuiViewportP*) {} void ImGui::DebugLog(const char*, ...) {} void ImGui::DebugLogV(const char*, va_list) {} void ImGui::ShowDebugLogWindow(bool*) {} -void ImGui::ShowStackToolWindow(bool*) {} +void ImGui::ShowIDStackToolWindow(bool*) {} void ImGui::DebugHookIdInfo(ImGuiID, ImGuiDataType, const void*, const void*) {} void ImGui::UpdateDebugToolItemPicker() {} void ImGui::UpdateDebugToolStackQueries() {} +void ImGui::UpdateDebugToolFlashStyleColor() {} #endif // #ifndef IMGUI_DISABLE_DEBUG_TOOLS diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 431cd4c7..4a116922 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (demo code) // Help: @@ -10,9 +10,9 @@ // Read imgui.cpp for more details, documentation and comments. // Get the latest version at https://github.com/ocornut/imgui -// ------------------------------------------------- +//--------------------------------------------------- // PLEASE DO NOT REMOVE THIS FILE FROM YOUR PROJECT! -// ------------------------------------------------- +//--------------------------------------------------- // Message to the person tempted to delete this file when integrating Dear ImGui into their codebase: // Think again! It is the most useful reference code that you and other coders will want to refer to and call. // Have the ImGui::ShowDemoWindow() function wired in an always-available debug menu of your game/app! @@ -26,14 +26,23 @@ // Thank you, // -Your beloved friend, imgui_demo.cpp (which you won't delete) -// Message to beginner C/C++ programmers about the meaning of the 'static' keyword: -// In this demo code, we frequently use 'static' variables inside functions. A static variable persists across calls, -// so it is essentially like a global variable but declared inside the scope of the function. We do this as a way to -// gather code and data in the same place, to make the demo source code faster to read, faster to write, and smaller -// in size. It also happens to be a convenient way of storing simple UI related information as long as your function -// doesn't need to be reentrant or used in multiple threads. This might be a pattern you will want to use in your code, -// but most of the real data you would be editing is likely going to be stored outside your functions. - +//-------------------------------------------- +// ABOUT THE MEANING OF THE 'static' KEYWORD: +//-------------------------------------------- +// In this demo code, we frequently use 'static' variables inside functions. +// A static variable persists across calls. It is essentially a global variable but declared inside the scope of the function. +// Think of "static int n = 0;" as "global int n = 0;" ! +// We do this IN THE DEMO because we want: +// - to gather code and data in the same place. +// - to make the demo source code faster to read, faster to change, smaller in size. +// - it is also a convenient way of storing simple UI related information as long as your function +// doesn't need to be reentrant or used in multiple threads. +// This might be a pattern you will want to use in your code, but most of the data you would be working +// with in a complex codebase is likely going to be stored outside your functions. + +//----------------------------------------- +// ABOUT THE CODING STYLE OF OUR DEMO CODE +//----------------------------------------- // The Demo code in this file is designed to be easy to copy-and-paste into your application! // Because of this: // - We never omit the ImGui:: prefix when calling functions, even though most code here is in the same namespace. @@ -45,7 +54,7 @@ // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. // Navigating this file: -// - In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. // - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. /* @@ -93,10 +102,9 @@ Index of this file: #include // sqrtf, powf, cosf, sinf, floorf, ceilf #include // vsnprintf, sscanf, printf #include // NULL, malloc, free, atoi -#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier -#include // intptr_t -#else #include // intptr_t +#if !defined(_MSC_VER) || _MSC_VER >= 1800 +#include // PRId64/PRIu64, not avail in some MinGW headers. #endif // Visual Studio warnings @@ -146,14 +154,13 @@ Index of this file: #define vsnprintf _vsnprintf #endif -// Format specifiers, printing 64-bit hasn't been decently standardized... -// In a real application you should be using PRId64 and PRIu64 from (non-windows) and on Windows define them yourself. -#ifdef _MSC_VER -#define IM_PRId64 "I64d" -#define IM_PRIu64 "I64u" -#else -#define IM_PRId64 "lld" -#define IM_PRIu64 "llu" +// Format specifiers for 64-bit values (hasn't been decently standardized before VS2013) +#if !defined(PRId64) && defined(_MSC_VER) +#define PRId64 "I64d" +#define PRIu64 "I64u" +#elif !defined(PRId64) +#define PRId64 "lld" +#define PRIu64 "llu" #endif // Helpers macros @@ -164,7 +171,8 @@ Index of this file: #define IM_MAX(A, B) (((A) >= (B)) ? (A) : (B)) #define IM_CLAMP(V, MN, MX) ((V) < (MN) ? (MN) : (V) > (MX) ? (MX) : (V)) -// Enforce cdecl calling convention for functions called by the standard library, in case compilation settings changed the default to e.g. __vectorcall +// Enforce cdecl calling convention for functions called by the standard library, +// in case compilation settings changed the default to e.g. __vectorcall #ifndef IMGUI_CDECL #ifdef _MSC_VER #define IMGUI_CDECL __cdecl @@ -180,19 +188,19 @@ Index of this file: #if !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Forward Declarations -static void ShowExampleAppDocuments(bool* p_open); static void ShowExampleAppMainMenuBar(); static void ShowExampleAppConsole(bool* p_open); +static void ShowExampleAppCustomRendering(bool* p_open); +static void ShowExampleAppDocuments(bool* p_open); static void ShowExampleAppLog(bool* p_open); static void ShowExampleAppLayout(bool* p_open); static void ShowExampleAppPropertyEditor(bool* p_open); -static void ShowExampleAppLongText(bool* p_open); +static void ShowExampleAppSimpleOverlay(bool* p_open); static void ShowExampleAppAutoResize(bool* p_open); static void ShowExampleAppConstrainedResize(bool* p_open); -static void ShowExampleAppSimpleOverlay(bool* p_open); static void ShowExampleAppFullscreen(bool* p_open); +static void ShowExampleAppLongText(bool* p_open); static void ShowExampleAppWindowTitles(bool* p_open); -static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleMenuFile(); // We split the contents of the big ShowDemoWindow() function into smaller functions @@ -248,59 +256,59 @@ void* GImGuiDemoMarkerCallbackUserData = NULL; void ImGui::ShowDemoWindow(bool* p_open) { // Exceptionally add an extra assert here for people confused about initial Dear ImGui setup - // Most functions would normally just crash if the context is missing. - IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing dear imgui context. Refer to examples app!"); + // Most functions would normally just assert/crash if the context is missing. + IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing Dear ImGui context. Refer to examples app!"); // Examples Apps (accessible from the "Examples" menu) static bool show_app_main_menu_bar = false; - static bool show_app_documents = false; static bool show_app_console = false; + static bool show_app_custom_rendering = false; + static bool show_app_documents = false; static bool show_app_log = false; static bool show_app_layout = false; static bool show_app_property_editor = false; - static bool show_app_long_text = false; + static bool show_app_simple_overlay = false; static bool show_app_auto_resize = false; static bool show_app_constrained_resize = false; - static bool show_app_simple_overlay = false; static bool show_app_fullscreen = false; + static bool show_app_long_text = false; static bool show_app_window_titles = false; - static bool show_app_custom_rendering = false; if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); if (show_app_console) ShowExampleAppConsole(&show_app_console); + if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); if (show_app_log) ShowExampleAppLog(&show_app_log); if (show_app_layout) ShowExampleAppLayout(&show_app_layout); if (show_app_property_editor) ShowExampleAppPropertyEditor(&show_app_property_editor); - if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text); + if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay); if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize); if (show_app_constrained_resize) ShowExampleAppConstrainedResize(&show_app_constrained_resize); - if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay); if (show_app_fullscreen) ShowExampleAppFullscreen(&show_app_fullscreen); + if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text); if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); - if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); - // Dear ImGui Tools/Apps (accessible from the "Tools" menu) - static bool show_app_metrics = false; - static bool show_app_debug_log = false; - static bool show_app_stack_tool = false; - static bool show_app_about = false; - static bool show_app_style_editor = false; - - if (show_app_metrics) - ImGui::ShowMetricsWindow(&show_app_metrics); - if (show_app_debug_log) - ImGui::ShowDebugLogWindow(&show_app_debug_log); - if (show_app_stack_tool) - ImGui::ShowStackToolWindow(&show_app_stack_tool); - if (show_app_about) - ImGui::ShowAboutWindow(&show_app_about); - if (show_app_style_editor) - { - ImGui::Begin("Dear ImGui Style Editor", &show_app_style_editor); + // Dear ImGui Tools (accessible from the "Tools" menu) + static bool show_tool_metrics = false; + static bool show_tool_debug_log = false; + static bool show_tool_id_stack_tool = false; + static bool show_tool_style_editor = false; + static bool show_tool_about = false; + + if (show_tool_metrics) + ImGui::ShowMetricsWindow(&show_tool_metrics); + if (show_tool_debug_log) + ImGui::ShowDebugLogWindow(&show_tool_debug_log); + if (show_tool_id_stack_tool) + ImGui::ShowIDStackToolWindow(&show_tool_id_stack_tool); + if (show_tool_style_editor) + { + ImGui::Begin("Dear ImGui Style Editor", &show_tool_style_editor); ImGui::ShowStyleEditor(); ImGui::End(); } + if (show_tool_about) + ImGui::ShowAboutWindow(&show_tool_about); // Demonstrate the various window flags. Typically you would just use the default! static bool no_titlebar = false; @@ -361,18 +369,23 @@ void ImGui::ShowDemoWindow(bool* p_open) { IMGUI_DEMO_MARKER("Menu/Examples"); ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar); + + ImGui::SeparatorText("Mini apps"); ImGui::MenuItem("Console", NULL, &show_app_console); + ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering); + ImGui::MenuItem("Documents", NULL, &show_app_documents); ImGui::MenuItem("Log", NULL, &show_app_log); - ImGui::MenuItem("Simple layout", NULL, &show_app_layout); ImGui::MenuItem("Property editor", NULL, &show_app_property_editor); - ImGui::MenuItem("Long text display", NULL, &show_app_long_text); + ImGui::MenuItem("Simple layout", NULL, &show_app_layout); + ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay); + + ImGui::SeparatorText("Concepts"); ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize); ImGui::MenuItem("Constrained-resizing window", NULL, &show_app_constrained_resize); - ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay); ImGui::MenuItem("Fullscreen window", NULL, &show_app_fullscreen); + ImGui::MenuItem("Long text display", NULL, &show_app_long_text); ImGui::MenuItem("Manipulating window titles", NULL, &show_app_window_titles); - ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering); - ImGui::MenuItem("Documents", NULL, &show_app_documents); + ImGui::EndMenu(); } //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar! @@ -384,11 +397,11 @@ void ImGui::ShowDemoWindow(bool* p_open) #else const bool has_debug_tools = false; #endif - ImGui::MenuItem("Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools); - ImGui::MenuItem("Debug Log", NULL, &show_app_debug_log, has_debug_tools); - ImGui::MenuItem("Stack Tool", NULL, &show_app_stack_tool, has_debug_tools); - ImGui::MenuItem("Style Editor", NULL, &show_app_style_editor); - ImGui::MenuItem("About Dear ImGui", NULL, &show_app_about); + ImGui::MenuItem("Metrics/Debugger", NULL, &show_tool_metrics, has_debug_tools); + ImGui::MenuItem("Debug Log", NULL, &show_tool_debug_log, has_debug_tools); + ImGui::MenuItem("ID Stack Tool", NULL, &show_tool_id_stack_tool, has_debug_tools); + ImGui::MenuItem("Style Editor", NULL, &show_tool_style_editor); + ImGui::MenuItem("About Dear ImGui", NULL, &show_tool_about); ImGui::EndMenu(); } ImGui::EndMenuBar(); @@ -410,7 +423,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::BulletText("See the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); ImGui::BulletText("See comments in imgui.cpp."); ImGui::BulletText("See example applications in the examples/ folder."); - ImGui::BulletText("Read the FAQ at http://www.dearimgui.com/faq/"); + ImGui::BulletText("Read the FAQ at https://www.dearimgui.com/faq/"); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); @@ -465,10 +478,12 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Text("Also see Style->Rendering for rendering options."); ImGui::SeparatorText("Debug"); + ImGui::Checkbox("io.ConfigDebugIsDebuggerPresent", &io.ConfigDebugIsDebuggerPresent); + ImGui::SameLine(); HelpMarker("Enable various tools calling IM_DEBUG_BREAK().\n\nRequires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application."); ImGui::BeginDisabled(); ImGui::Checkbox("io.ConfigDebugBeginReturnValueOnce", &io.ConfigDebugBeginReturnValueOnce); // . ImGui::EndDisabled(); - ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover"); + ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover."); ImGui::Checkbox("io.ConfigDebugBeginReturnValueLoop", &io.ConfigDebugBeginReturnValueLoop); ImGui::SameLine(); HelpMarker("Some calls to Begin()/BeginChild() will return false.\n\nWill cycle through window depths then repeat. Windows should be flickering while running."); ImGui::Checkbox("io.ConfigDebugIgnoreFocusLoss", &io.ConfigDebugIgnoreFocusLoss); @@ -747,7 +762,8 @@ static void ShowDemoWindowWidgets() static int item_current = 0; ImGui::Combo("combo", &item_current, items, IM_ARRAYSIZE(items)); ImGui::SameLine(); HelpMarker( - "Using the simplified one-liner Combo API here.\nRefer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API."); + "Using the simplified one-liner Combo API here.\n" + "Refer to the \"Combo\" section below for an explanation of how to use the more flexible and general BeginCombo/EndCombo API."); } { @@ -758,7 +774,8 @@ static void ShowDemoWindowWidgets() static int item_current = 1; ImGui::ListBox("listbox", &item_current, items, IM_ARRAYSIZE(items), 4); ImGui::SameLine(); HelpMarker( - "Using the simplified one-liner ListBox API here.\nRefer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API."); + "Using the simplified one-liner ListBox API here.\n" + "Refer to the \"List boxes\" section below for an explanation of how to use the more flexible and general BeginListBox/EndListBox API."); } ImGui::TreePop(); @@ -816,13 +833,18 @@ static void ShowDemoWindowWidgets() ImGui::SeparatorText("Custom"); + HelpMarker( + "Passing ImGuiHoveredFlags_ForTooltip to IsItemHovered() is the preferred way to standardize" + "tooltip activation details across your application. You may however decide to use custom" + "flags for a specific tooltip instance."); + // The following examples are passed for documentation purpose but may not be useful to most users. - // Passing ImGuiHoveredFlags_Tooltip to IsItemHovered() will pull ImGuiHoveredFlags flags values from + // Passing ImGuiHoveredFlags_ForTooltip to IsItemHovered() will pull ImGuiHoveredFlags flags values from // 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on whether mouse or gamepad/keyboard is being used. - // With default settings, ImGuiHoveredFlags_Tooltip is equivalent to ImGuiHoveredFlags_DelayShort + ImGuiHoveredFlags_Stationary. + // With default settings, ImGuiHoveredFlags_ForTooltip is equivalent to ImGuiHoveredFlags_DelayShort + ImGuiHoveredFlags_Stationary. ImGui::Button("Manual", sz); if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip)) - ImGui::SetTooltip("I am a manually emitted tooltip"); + ImGui::SetTooltip("I am a manually emitted tooltip."); ImGui::Button("DelayNone", sz); if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNone)) @@ -834,12 +856,21 @@ static void ShowDemoWindowWidgets() ImGui::Button("DelayLong", sz); if (ImGui::IsItemHovered(ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_NoSharedDelay)) - ImGui::SetTooltip("I am a tooltip with a long delay (%0.2f sec)", ImGui::GetStyle().HoverDelayNormal); + ImGui::SetTooltip("I am a tooltip with a long delay (%0.2f sec).", ImGui::GetStyle().HoverDelayNormal); ImGui::Button("Stationary", sz); if (ImGui::IsItemHovered(ImGuiHoveredFlags_Stationary)) ImGui::SetTooltip("I am a tooltip requiring mouse to be stationary before activating."); + // Using ImGuiHoveredFlags_ForTooltip will pull flags from 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav', + // which default value include the ImGuiHoveredFlags_AllowWhenDisabled flag. + // As a result, Set + ImGui::BeginDisabled(); + ImGui::Button("Disabled item", sz); + ImGui::EndDisabled(); + if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip)) + ImGui::SetTooltip("I am a a tooltip for a disabled item."); + ImGui::TreePop(); } @@ -849,10 +880,10 @@ static void ShowDemoWindowWidgets() // if (once) // ImGui::Text("This will be displayed only once."); - IMGUI_DEMO_MARKER("Widgets/Trees"); - if (ImGui::TreeNode("Trees")) + IMGUI_DEMO_MARKER("Widgets/Tree Nodes"); + if (ImGui::TreeNode("Tree Nodes")) { - IMGUI_DEMO_MARKER("Widgets/Trees/Basic trees"); + IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Basic trees"); if (ImGui::TreeNode("Basic trees")) { for (int i = 0; i < 5; i++) @@ -873,7 +904,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Widgets/Trees/Advanced, with Selectable nodes"); + IMGUI_DEMO_MARKER("Widgets/Tree Nodes/Advanced, with Selectable nodes"); if (ImGui::TreeNode("Advanced, with Selectable nodes")) { HelpMarker( @@ -886,6 +917,7 @@ static void ShowDemoWindowWidgets() ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", &base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", &base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node."); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &base_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &base_flags, ImGuiTreeNodeFlags_SpanAllColumns); ImGui::SameLine(); HelpMarker("For use in Tables only."); ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position); ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop); ImGui::Text("Hello!"); @@ -1061,7 +1093,7 @@ static void ShowDemoWindowWidgets() "CJK text will only appear if the font was loaded with the appropriate CJK character ranges. " "Call io.Fonts->AddFontFromFileTTF() manually to load extra character ranges. " "Read docs/FONTS.md for details."); - ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); // Normally we would use u8"blah blah" with the proper characters directly in the string. + ImGui::Text("Hiragana: \xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93 (kakikukeko)"); ImGui::Text("Kanjis: \xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e (nihongo)"); static char buf[32] = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"; //static char buf[32] = u8"NIHONGO"; // <- this is how you would write it with C++11, using real kanjis @@ -1105,7 +1137,7 @@ static void ShowDemoWindowWidgets() ImVec2 pos = ImGui::GetCursorScreenPos(); ImVec2 uv_min = ImVec2(0.0f, 0.0f); // Top-left ImVec2 uv_max = ImVec2(1.0f, 1.0f); // Lower-right - ImVec4 tint_col = use_text_color_for_tint ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint + ImVec4 tint_col = use_text_color_for_tint ? ImGui::GetStyleColorVec4(ImGuiCol_Text) : ImVec4(1.0f, 1.0f, 1.0f, 1.0f); // No tint ImVec4 border_col = ImGui::GetStyleColorVec4(ImGuiCol_Border); ImGui::Image(my_tex_id, ImVec2(my_tex_w, my_tex_h), uv_min, uv_max, tint_col, border_col); if (ImGui::BeginItemTooltip()) @@ -1164,16 +1196,29 @@ static void ShowDemoWindowWidgets() ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", &flags, ImGuiComboFlags_PopupAlignLeft); ImGui::SameLine(); HelpMarker("Only makes a difference if the popup is larger than the combo"); if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", &flags, ImGuiComboFlags_NoArrowButton)) - flags &= ~ImGuiComboFlags_NoPreview; // Clear the other flag, as we cannot combine both + flags &= ~ImGuiComboFlags_NoPreview; // Clear incompatible flags if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", &flags, ImGuiComboFlags_NoPreview)) - flags &= ~ImGuiComboFlags_NoArrowButton; // Clear the other flag, as we cannot combine both + flags &= ~(ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_WidthFitPreview); // Clear incompatible flags + if (ImGui::CheckboxFlags("ImGuiComboFlags_WidthFitPreview", &flags, ImGuiComboFlags_WidthFitPreview)) + flags &= ~ImGuiComboFlags_NoPreview; + + // Override default popup height + if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightSmall", &flags, ImGuiComboFlags_HeightSmall)) + flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightSmall); + if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightRegular", &flags, ImGuiComboFlags_HeightRegular)) + flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightRegular); + if (ImGui::CheckboxFlags("ImGuiComboFlags_HeightLargest", &flags, ImGuiComboFlags_HeightLargest)) + flags &= ~(ImGuiComboFlags_HeightMask_ & ~ImGuiComboFlags_HeightLargest); // Using the generic BeginCombo() API, you have full control over how to display the combo contents. // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; static int item_current_idx = 0; // Here we store our selection data as an index. - const char* combo_preview_value = items[item_current_idx]; // Pass in the preview value visible before opening the combo (it could be anything) + + // Pass in the preview value visible before opening the combo (it could technically be different contents or not pulled from items[]) + const char* combo_preview_value = items[item_current_idx]; + if (ImGui::BeginCombo("combo 1", combo_preview_value, flags)) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) @@ -1189,6 +1234,10 @@ static void ShowDemoWindowWidgets() ImGui::EndCombo(); } + ImGui::Spacing(); + ImGui::SeparatorText("One-liner variants"); + HelpMarker("Flags above don't apply to this section."); + // Simplified one-liner Combo() API, using values packed in a single constant string // This is a convenience for when the selection set is small and known at compile-time. static int item_current_2 = 0; @@ -1200,9 +1249,8 @@ static void ShowDemoWindowWidgets() ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items)); // Simplified one-liner Combo() using an accessor function - struct Funcs { static bool ItemGetter(void* data, int n, const char** out_str) { *out_str = ((const char**)data)[n]; return true; } }; static int item_current_4 = 0; - ImGui::Combo("combo 4 (function)", &item_current_4, &Funcs::ItemGetter, items, IM_ARRAYSIZE(items)); + ImGui::Combo("combo 4 (function)", &item_current_4, [](void* data, int n) { return ((const char**)data)[n]; }, items, IM_ARRAYSIZE(items)); ImGui::TreePop(); } @@ -1210,6 +1258,11 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/List Boxes"); if (ImGui::TreeNode("List boxes")) { + // BeginListBox() is essentially a thin wrapper to using BeginChild()/EndChild() + // using the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. + // You may be tempted to simply use BeginChild() directly. However note that BeginChild() requires EndChild() + // to always be called (inconsistent with BeginListBox()/EndListBox()). + // Using the generic BeginListBox() API, you have full control over how to display the combo contents. // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively // stored in the object itself, etc.) @@ -1262,16 +1315,16 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Selectables/Basic"); if (ImGui::TreeNode("Basic")) { - static bool selection[5] = { false, true, false, false, false }; + static bool selection[5] = { false, true, false, false }; ImGui::Selectable("1. I am selectable", &selection[0]); ImGui::Selectable("2. I am selectable", &selection[1]); - ImGui::Text("(I am not selectable)"); - ImGui::Selectable("4. I am selectable", &selection[3]); - if (ImGui::Selectable("5. I am double clickable", selection[4], ImGuiSelectableFlags_AllowDoubleClick)) + ImGui::Selectable("3. I am selectable", &selection[2]); + if (ImGui::Selectable("4. I am double clickable", selection[3], ImGuiSelectableFlags_AllowDoubleClick)) if (ImGui::IsMouseDoubleClicked(0)) - selection[4] = !selection[4]; + selection[3] = !selection[3]; ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Single Selection"); if (ImGui::TreeNode("Selection State: Single Selection")) { @@ -1303,17 +1356,18 @@ static void ShowDemoWindowWidgets() } ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more text into the same line"); - if (ImGui::TreeNode("Rendering more text into the same line")) + IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more items on the same line"); + if (ImGui::TreeNode("Rendering more items on the same line")) { - // Using the Selectable() override that takes "bool* p_selected" parameter, - // this function toggle your bool value automatically. + // (1) Using SetNextItemAllowOverlap() + // (2) Using the Selectable() override that takes "bool* p_selected" parameter, the bool value is toggled automatically. static bool selected[3] = { false, false, false }; - ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); - ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(300); ImGui::Text("12,345 bytes"); - ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(300); ImGui::Text(" 2,345 bytes"); + ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("main.c", &selected[0]); ImGui::SameLine(); ImGui::SmallButton("Link 1"); + ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.cpp", &selected[1]); ImGui::SameLine(); ImGui::SmallButton("Link 2"); + ImGui::SetNextItemAllowOverlap(); ImGui::Selectable("Hello.h", &selected[2]); ImGui::SameLine(); ImGui::SmallButton("Link 3"); ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/In columns"); if (ImGui::TreeNode("In columns")) { @@ -1349,6 +1403,7 @@ static void ShowDemoWindowWidgets() } ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Selectables/Grid"); if (ImGui::TreeNode("Grid")) { @@ -1434,6 +1489,7 @@ static void ShowDemoWindowWidgets() HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include in here)"); ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly); ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", &flags, ImGuiInputTextFlags_AllowTabInput); + ImGui::SameLine(); HelpMarker("When _AllowTabInput is set, passing through the widget with Tabbing doesn't automatically activate it, in order to also cycling through subsequent widgets."); ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", &flags, ImGuiInputTextFlags_CtrlEnterForNewLine); ImGui::InputTextMultiline("##source", text, IM_ARRAYSIZE(text), ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags); ImGui::TreePop(); @@ -1482,6 +1538,7 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text Input/Completion, History, Edit Callbacks"); if (ImGui::TreeNode("Completion, History, Edit Callbacks")) { struct Funcs @@ -1523,16 +1580,21 @@ static void ShowDemoWindowWidgets() }; static char buf1[64]; ImGui::InputText("Completion", buf1, 64, ImGuiInputTextFlags_CallbackCompletion, Funcs::MyCallback); - ImGui::SameLine(); HelpMarker("Here we append \"..\" each time Tab is pressed. See 'Examples>Console' for a more meaningful demonstration of using this callback."); + ImGui::SameLine(); HelpMarker( + "Here we append \"..\" each time Tab is pressed. " + "See 'Examples>Console' for a more meaningful demonstration of using this callback."); static char buf2[64]; ImGui::InputText("History", buf2, 64, ImGuiInputTextFlags_CallbackHistory, Funcs::MyCallback); - ImGui::SameLine(); HelpMarker("Here we replace and select text each time Up/Down are pressed. See 'Examples>Console' for a more meaningful demonstration of using this callback."); + ImGui::SameLine(); HelpMarker( + "Here we replace and select text each time Up/Down are pressed. " + "See 'Examples>Console' for a more meaningful demonstration of using this callback."); static char buf3[64]; static int edit_count = 0; ImGui::InputText("Edit", buf3, 64, ImGuiInputTextFlags_CallbackEdit, Funcs::MyCallback, (void*)&edit_count); - ImGui::SameLine(); HelpMarker("Here we toggle the casing of the first character on every edit + count edits."); + ImGui::SameLine(); HelpMarker( + "Here we toggle the casing of the first character on every edit + count edits."); ImGui::SameLine(); ImGui::Text("(%d)", edit_count); ImGui::TreePop(); @@ -1581,6 +1643,18 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Text Input/Miscellaneous"); + if (ImGui::TreeNode("Miscellaneous")) + { + static char buf1[16]; + static ImGuiInputTextFlags flags = ImGuiInputTextFlags_EscapeClearsAll; + ImGui::CheckboxFlags("ImGuiInputTextFlags_EscapeClearsAll", &flags, ImGuiInputTextFlags_EscapeClearsAll); + ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly); + ImGui::CheckboxFlags("ImGuiInputTextFlags_NoUndoRedo", &flags, ImGuiInputTextFlags_NoUndoRedo); + ImGui::InputText("Hello", buf1, IM_ARRAYSIZE(buf1), flags); + ImGui::TreePop(); + } + ImGui::TreePop(); } @@ -1695,8 +1769,9 @@ static void ShowDemoWindowWidgets() ImGui::EndPopup(); } - // Demo Trailing Tabs: click the "+" button to add a new tab (in your app you may want to use a font icon instead of the "+") - // Note that we submit it before the regular tabs, but because of the ImGuiTabItemFlags_Trailing flag it will always appear at the end. + // Demo Trailing Tabs: click the "+" button to add a new tab. + // (In your app you may want to use a font icon instead of the "+") + // We submit it before the regular tabs, but thanks to the ImGuiTabItemFlags_Trailing flag it will always appear at the end. if (show_trailing_button) if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) active_tabs.push_back(next_tab_id++); // Add new tab @@ -1980,7 +2055,8 @@ static void ShowDemoWindowWidgets() if (ImGui::Button("Default: Float + HDR + Hue Wheel")) ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel); - // Always both a small version of both types of pickers (to make it more visible in the demo to people who are skimming quickly through it) + // Always display a small version of both types of pickers + // (that's in order to make it more visible in the demo to people who are skimming quickly through it) ImGui::Text("Both types:"); float w = (ImGui::GetContentRegionAvail().x - ImGui::GetStyle().ItemSpacing.y) * 0.40f; ImGui::SetNextItemWidth(w); @@ -2133,12 +2209,12 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider u32 low", ImGuiDataType_U32, &u32_v, &u32_zero, &u32_fifty,"%u"); ImGui::SliderScalar("slider u32 high", ImGuiDataType_U32, &u32_v, &u32_hi_a, &u32_hi_b, "%u"); ImGui::SliderScalar("slider u32 full", ImGuiDataType_U32, &u32_v, &u32_min, &u32_max, "%u"); - ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%" IM_PRId64); - ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%" IM_PRId64); - ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%" IM_PRId64); - ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%" IM_PRIu64 " ms"); - ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%" IM_PRIu64 " ms"); - ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%" IM_PRIu64 " ms"); + ImGui::SliderScalar("slider s64 low", ImGuiDataType_S64, &s64_v, &s64_zero, &s64_fifty,"%" PRId64); + ImGui::SliderScalar("slider s64 high", ImGuiDataType_S64, &s64_v, &s64_hi_a, &s64_hi_b, "%" PRId64); + ImGui::SliderScalar("slider s64 full", ImGuiDataType_S64, &s64_v, &s64_min, &s64_max, "%" PRId64); + ImGui::SliderScalar("slider u64 low", ImGuiDataType_U64, &u64_v, &u64_zero, &u64_fifty,"%" PRIu64 " ms"); + ImGui::SliderScalar("slider u64 high", ImGuiDataType_U64, &u64_v, &u64_hi_a, &u64_hi_b, "%" PRIu64 " ms"); + ImGui::SliderScalar("slider u64 full", ImGuiDataType_U64, &u64_v, &u64_min, &u64_max, "%" PRIu64 " ms"); ImGui::SliderScalar("slider float low", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one); ImGui::SliderScalar("slider float low log", ImGuiDataType_Float, &f32_v, &f32_zero, &f32_one, "%.10f", ImGuiSliderFlags_Logarithmic); ImGui::SliderScalar("slider float high", ImGuiDataType_Float, &f32_v, &f32_lo_a, &f32_hi_a, "%e"); @@ -2151,8 +2227,8 @@ static void ShowDemoWindowWidgets() ImGui::SliderScalar("slider u8 reverse", ImGuiDataType_U8, &u8_v, &u8_max, &u8_min, "%u"); ImGui::SliderScalar("slider s32 reverse", ImGuiDataType_S32, &s32_v, &s32_fifty, &s32_zero, "%d"); ImGui::SliderScalar("slider u32 reverse", ImGuiDataType_U32, &u32_v, &u32_fifty, &u32_zero, "%u"); - ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%" IM_PRId64); - ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%" IM_PRIu64 " ms"); + ImGui::SliderScalar("slider s64 reverse", ImGuiDataType_S64, &s64_v, &s64_fifty, &s64_zero, "%" PRId64); + ImGui::SliderScalar("slider u64 reverse", ImGuiDataType_U64, &u64_v, &u64_fifty, &u64_zero, "%" PRIu64 " ms"); IMGUI_DEMO_MARKER("Widgets/Data Types/Inputs"); static bool inputs_step = true; @@ -2385,6 +2461,36 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + IMGUI_DEMO_MARKER("Widgets/Drag and Drop/Tooltip at target location"); + if (ImGui::TreeNode("Tooltip at target location")) + { + for (int n = 0; n < 2; n++) + { + // Drop targets + ImGui::Button(n ? "drop here##1" : "drop here##0"); + if (ImGui::BeginDragDropTarget()) + { + ImGuiDragDropFlags drop_target_flags = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoPreviewTooltip; + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_4F, drop_target_flags)) + { + IM_UNUSED(payload); + ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed); + ImGui::BeginTooltip(); + ImGui::Text("Cannot drop here!"); + ImGui::EndTooltip(); + } + ImGui::EndDragDropTarget(); + } + + // Drop source + static ImVec4 col4 = { 1.0f, 0.0f, 0.2f, 1.0f }; + if (n == 0) + ImGui::ColorButton("drag me", col4); + + } + ImGui::TreePop(); + } + ImGui::TreePop(); } @@ -2506,7 +2612,7 @@ static void ShowDemoWindowWidgets() static bool embed_all_inside_a_child_window = false; ImGui::Checkbox("Embed everything inside a child window for testing _RootWindow flag.", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) - ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), true); + ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), ImGuiChildFlags_Border); // Testing IsWindowFocused() function with its various flags. ImGui::BulletText( @@ -2554,7 +2660,7 @@ static void ShowDemoWindowWidgets() ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow), ImGui::IsWindowHovered(ImGuiHoveredFlags_Stationary)); - ImGui::BeginChild("child", ImVec2(0, 50), true); + ImGui::BeginChild("child", ImVec2(0, 50), ImGuiChildFlags_Border); ImGui::Text("This is another child window for testing the _ChildWindows flag."); ImGui::EndChild(); if (embed_all_inside_a_child_window) @@ -2638,7 +2744,7 @@ static void ShowDemoWindowLayout() ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; if (disable_mouse_wheel) window_flags |= ImGuiWindowFlags_NoScrollWithMouse; - ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), false, window_flags); + ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), ImGuiChildFlags_None, window_flags); for (int i = 0; i < 100; i++) ImGui::Text("%04d: scrollable region", i); ImGui::EndChild(); @@ -2654,7 +2760,7 @@ static void ShowDemoWindowLayout() if (!disable_menu) window_flags |= ImGuiWindowFlags_MenuBar; ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); - ImGui::BeginChild("ChildR", ImVec2(0, 260), true, window_flags); + ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Border, window_flags); if (!disable_menu && ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("Menu")) @@ -2679,6 +2785,35 @@ static void ShowDemoWindowLayout() ImGui::PopStyleVar(); } + // Child 3: manual-resize + ImGui::SeparatorText("Manual-resize"); + { + HelpMarker("Drag bottom border to resize. Double-click bottom border to auto-fit to vertical contents."); + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg)); + if (ImGui::BeginChild("ResizableChild", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) + for (int n = 0; n < 10; n++) + ImGui::Text("Line %04d", n); + ImGui::PopStyleColor(); + ImGui::EndChild(); + } + + // Child 4: auto-resizing height with a limit + ImGui::SeparatorText("Auto-resize with constraints"); + { + static int draw_lines = 3; + static int max_height_in_lines = 10; + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::DragInt("Lines Count", &draw_lines, 0.2f); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::DragInt("Max Height (in Lines)", &max_height_in_lines, 0.2f); + + ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 1), ImVec2(FLT_MAX, ImGui::GetTextLineHeightWithSpacing() * max_height_in_lines)); + if (ImGui::BeginChild("ConstrainedChild", ImVec2(-FLT_MIN, 0.0f), ImGuiChildFlags_Border | ImGuiChildFlags_AutoResizeY)) + for (int n = 0; n < draw_lines; n++) + ImGui::Text("Line %04d", n); + ImGui::EndChild(); + } + ImGui::SeparatorText("Misc/Advanced"); // Demonstrate a few extra things @@ -2690,19 +2825,33 @@ static void ShowDemoWindowLayout() // the POV of the parent window). See 'Demo->Querying Status (Edited/Active/Hovered etc.)' for details. { static int offset_x = 0; + static bool override_bg_color = true; + static ImGuiChildFlags child_flags = ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY; ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::DragInt("Offset X", &offset_x, 1.0f, -1000, 1000); + ImGui::Checkbox("Override ChildBg color", &override_bg_color); + ImGui::CheckboxFlags("ImGuiChildFlags_Border", &child_flags, ImGuiChildFlags_Border); + ImGui::CheckboxFlags("ImGuiChildFlags_AlwaysUseWindowPadding", &child_flags, ImGuiChildFlags_AlwaysUseWindowPadding); + ImGui::CheckboxFlags("ImGuiChildFlags_ResizeX", &child_flags, ImGuiChildFlags_ResizeX); + ImGui::CheckboxFlags("ImGuiChildFlags_ResizeY", &child_flags, ImGuiChildFlags_ResizeY); + ImGui::CheckboxFlags("ImGuiChildFlags_FrameStyle", &child_flags, ImGuiChildFlags_FrameStyle); + ImGui::SameLine(); HelpMarker("Style the child window like a framed item: use FrameBg, FrameRounding, FrameBorderSize, FramePadding instead of ChildBg, ChildRounding, ChildBorderSize, WindowPadding."); + if (child_flags & ImGuiChildFlags_FrameStyle) + override_bg_color = false; ImGui::SetCursorPosX(ImGui::GetCursorPosX() + (float)offset_x); - ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); - ImGui::BeginChild("Red", ImVec2(200, 100), true, ImGuiWindowFlags_None); + if (override_bg_color) + ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(255, 0, 0, 100)); + ImGui::BeginChild("Red", ImVec2(200, 100), child_flags, ImGuiWindowFlags_None); + if (override_bg_color) + ImGui::PopStyleColor(); + for (int n = 0; n < 50; n++) ImGui::Text("Some test %d", n); ImGui::EndChild(); bool child_is_hovered = ImGui::IsItemHovered(); ImVec2 child_rect_min = ImGui::GetItemRectMin(); ImVec2 child_rect_max = ImGui::GetItemRectMax(); - ImGui::PopStyleColor(); ImGui::Text("Hovered: %d", child_is_hovered); ImGui::Text("Rect of child window is: (%.0f,%.0f) (%.0f,%.0f)", child_rect_min.x, child_rect_min.y, child_rect_max.x, child_rect_max.y); } @@ -2795,11 +2944,11 @@ static void ShowDemoWindowLayout() // Text IMGUI_DEMO_MARKER("Layout/Basic Horizontal Layout/SameLine"); ImGui::Text("Two items: Hello"); ImGui::SameLine(); - ImGui::TextColored(ImVec4(1,1,0,1), "Sailor"); + ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor"); // Adjust spacing ImGui::Text("More spacing: Hello"); ImGui::SameLine(0, 20); - ImGui::TextColored(ImVec4(1,1,0,1), "Sailor"); + ImGui::TextColored(ImVec4(1, 1, 0, 1), "Sailor"); // Button ImGui::AlignTextToFramePadding(); @@ -3091,7 +3240,7 @@ static void ShowDemoWindowLayout() const ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; const ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); - const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), true, child_flags); + const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), ImGuiChildFlags_Border, child_flags); if (ImGui::BeginMenuBar()) { ImGui::TextUnformatted("abc"); @@ -3138,7 +3287,7 @@ static void ShowDemoWindowLayout() float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f; ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0); ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); - bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), true, child_flags); + bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), ImGuiChildFlags_Border, child_flags); if (scroll_to_off) ImGui::SetScrollX(scroll_to_off_px); if (scroll_to_pos) @@ -3180,7 +3329,7 @@ static void ShowDemoWindowLayout() ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2.0f, 1.0f)); ImVec2 scrolling_child_size = ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30); - ImGui::BeginChild("scrolling", scrolling_child_size, true, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::BeginChild("scrolling", scrolling_child_size, ImGuiChildFlags_Border, ImGuiWindowFlags_HorizontalScrollbar); for (int line = 0; line < lines; line++) { // Display random stuff. For the sake of this trivial demo we are using basic Button() + SameLine() @@ -3249,7 +3398,9 @@ static void ShowDemoWindowLayout() IMGUI_DEMO_MARKER("Layout/Scrolling/Horizontal contents size demo window"); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(2, 0)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 0)); - HelpMarker("Test of different widgets react and impact the work rectangle growing when horizontal scrolling is enabled.\n\nUse 'Metrics->Tools->Show windows rectangles' to visualize rectangles."); + HelpMarker( + "Test how different widgets react and impact the work rectangle growing when horizontal scrolling is enabled.\n\n" + "Use 'Metrics->Tools->Show windows rectangles' to visualize rectangles."); ImGui::Checkbox("H-scrollbar", &show_h_scrollbar); ImGui::Checkbox("Button", &show_button); // Will grow contents size (unless explicitly overwritten) ImGui::Checkbox("Tree nodes", &show_tree_nodes); // Will grow contents size and display highlight over full width @@ -3324,7 +3475,7 @@ static void ShowDemoWindowLayout() } if (show_child) { - ImGui::BeginChild("child", ImVec2(0, 0), true); + ImGui::BeginChild("child", ImVec2(0, 0), ImGuiChildFlags_Border); ImGui::EndChild(); } ImGui::End(); @@ -3397,6 +3548,37 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } + + IMGUI_DEMO_MARKER("Layout/Overlap Mode"); + if (ImGui::TreeNode("Overlap Mode")) + { + static bool enable_allow_overlap = true; + + HelpMarker( + "Hit-testing is by default performed in item submission order, which generally is perceived as 'back-to-front'.\n\n" + "By using SetNextItemAllowOverlap() you can notify that an item may be overlapped by another. " + "Doing so alters the hovering logic: items using AllowOverlap mode requires an extra frame to accept hovered state."); + ImGui::Checkbox("Enable AllowOverlap", &enable_allow_overlap); + + ImVec2 button1_pos = ImGui::GetCursorScreenPos(); + ImVec2 button2_pos = ImVec2(button1_pos.x + 50.0f, button1_pos.y + 50.0f); + if (enable_allow_overlap) + ImGui::SetNextItemAllowOverlap(); + ImGui::Button("Button 1", ImVec2(80, 80)); + ImGui::SetCursorScreenPos(button2_pos); + ImGui::Button("Button 2", ImVec2(80, 80)); + + // This is typically used with width-spanning items. + // (note that Selectable() has a dedicated flag ImGuiSelectableFlags_AllowOverlap, which is a shortcut + // for using SetNextItemAllowOverlap(). For demo purpose we use SetNextItemAllowOverlap() here.) + if (enable_allow_overlap) + ImGui::SetNextItemAllowOverlap(); + ImGui::Selectable("Some Selectable", false); + ImGui::SameLine(); + ImGui::SmallButton("++"); + + ImGui::TreePop(); + } } static void ShowDemoWindowPopups() @@ -3728,6 +3910,14 @@ struct MyItem // very often by the sorting algorithm it would be a little wasteful. static const ImGuiTableSortSpecs* s_current_sort_specs; + static void SortWithSortSpecs(ImGuiTableSortSpecs* sort_specs, MyItem* items, int items_count) + { + s_current_sort_specs = sort_specs; // Store in variable accessible by the sort function. + if (items_count > 1) + qsort(items, (size_t)items_count, sizeof(items[0]), MyItem::CompareWithSortSpecs); + s_current_sort_specs = NULL; + } + // Compare function to be used by qsort() static int IMGUI_CDECL CompareWithSortSpecs(const void* lhs, const void* rhs) { @@ -3754,7 +3944,8 @@ struct MyItem } // qsort() is instable so always return a way to differenciate items. - // Your own compare function may want to avoid fallback on implicit sort specs e.g. a Name compare if it wasn't already part of the sort specs. + // Your own compare function may want to avoid fallback on implicit sort specs. + // e.g. a Name compare if it wasn't already part of the sort specs. return (a->ID - b->ID); } }; @@ -3838,6 +4029,7 @@ static void EditTableColumnsFlags(ImGuiTableColumnFlags* p_flags) ImGui::CheckboxFlags("_PreferSortDescending", p_flags, ImGuiTableColumnFlags_PreferSortDescending); ImGui::CheckboxFlags("_IndentEnable", p_flags, ImGuiTableColumnFlags_IndentEnable); ImGui::SameLine(); HelpMarker("Default for column 0"); ImGui::CheckboxFlags("_IndentDisable", p_flags, ImGuiTableColumnFlags_IndentDisable); ImGui::SameLine(); HelpMarker("Default for column >0"); + ImGui::CheckboxFlags("_AngledHeader", p_flags, ImGuiTableColumnFlags_AngledHeader); } static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags) @@ -3862,10 +4054,10 @@ static void ShowDemoWindowTables() ImGui::PushID("Tables"); int open_action = -1; - if (ImGui::Button("Open all")) + if (ImGui::Button("Expand all")) open_action = 1; ImGui::SameLine(); - if (ImGui::Button("Close all")) + if (ImGui::Button("Collapse all")) open_action = 0; ImGui::SameLine(); @@ -3936,8 +4128,9 @@ static void ShowDemoWindowTables() // as TableNextColumn() will automatically wrap around and create new rows as needed. // This is generally more convenient when your cells all contains the same type of data. HelpMarker( - "Only using TableNextColumn(), which tends to be convenient for tables where every cell contains the same type of contents.\n" - "This is also more similar to the old NextColumn() function of the Columns API, and provided to facilitate the Columns->Tables API transition."); + "Only using TableNextColumn(), which tends to be convenient for tables where every cell contains " + "the same type of contents.\n This is also more similar to the old NextColumn() function of the " + "Columns API, and provided to facilitate the Columns->Tables API transition."); if (ImGui::BeginTable("table3", 3)) { for (int item = 0; item < 14; item++) @@ -3993,8 +4186,8 @@ static void ShowDemoWindowTables() if (ImGui::BeginTable("table1", 3, flags)) { - // Display headers so we can inspect their interaction with borders. - // (Headers are not the main purpose of this section of the demo, so we are not elaborating on them too much. See other sections for details) + // Display headers so we can inspect their interaction with borders + // (Headers are not the main purpose of this section of the demo, so we are not elaborating on them now. See other sections for details) if (display_headers) { ImGui::TableSetupColumn("One"); @@ -4033,7 +4226,9 @@ static void ShowDemoWindowTables() PushStyleCompact(); ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV); - ImGui::SameLine(); HelpMarker("Using the _Resizable flag automatically enables the _BordersInnerV flag as well, this is why the resize borders are still showing when unchecking this."); + ImGui::SameLine(); HelpMarker( + "Using the _Resizable flag automatically enables the _BordersInnerV flag as well, " + "this is why the resize borders are still showing when unchecking this."); PopStyleCompact(); if (ImGui::BeginTable("table1", 3, flags)) @@ -4151,6 +4346,7 @@ static void ShowDemoWindowTables() ImGui::CheckboxFlags("ImGuiTableFlags_Hideable", &flags, ImGuiTableFlags_Hideable); ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBody", &flags, ImGuiTableFlags_NoBordersInBody); ImGui::CheckboxFlags("ImGuiTableFlags_NoBordersInBodyUntilResize", &flags, ImGuiTableFlags_NoBordersInBodyUntilResize); ImGui::SameLine(); HelpMarker("Disable vertical borders in columns Body until hovered for resize (borders will always appear in Headers)"); + ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn); PopStyleCompact(); if (ImGui::BeginTable("table1", 3, flags)) @@ -4173,7 +4369,8 @@ static void ShowDemoWindowTables() ImGui::EndTable(); } - // Use outer_size.x == 0.0f instead of default to make the table as tight as possible (only valid when no scrolling and no stretch column) + // Use outer_size.x == 0.0f instead of default to make the table as tight as possible + // (only valid when no scrolling and no stretch column) if (ImGui::BeginTable("table2", 3, flags | ImGuiTableFlags_SizingFixedFit, ImVec2(0.0f, 0.0f))) { ImGui::TableSetupColumn("One"); @@ -4206,7 +4403,8 @@ static void ShowDemoWindowTables() "e.g.:\n" "- BorderOuterV\n" "- any form of row selection\n" - "Because of this, activating BorderOuterV sets the default to PadOuterX. Using PadOuterX or NoPadOuterX you can override the default.\n\n" + "Because of this, activating BorderOuterV sets the default to PadOuterX. " + "Using PadOuterX or NoPadOuterX you can override the default.\n\n" "Actual padding values are using style.CellPadding.\n\n" "In this demo we don't show horizontal borders to emphasize how they don't affect default horizontal padding."); @@ -4322,7 +4520,8 @@ static void ShowDemoWindowTables() EditTableSizingFlags(&sizing_policy_flags[table_n]); // To make it easier to understand the different sizing policy, - // For each policy: we display one table where the columns have equal contents width, and one where the columns have different contents width. + // For each policy: we display one table where the columns have equal contents width, + // and one where the columns have different contents width. if (ImGui::BeginTable("table1", 3, sizing_policy_flags[table_n] | flags1)) { for (int row = 0; row < 3; row++) @@ -4351,7 +4550,9 @@ static void ShowDemoWindowTables() ImGui::Spacing(); ImGui::TextUnformatted("Advanced"); ImGui::SameLine(); - HelpMarker("This section allows you to interact and see the effect of various sizing policies depending on whether Scroll is enabled and the contents of your columns."); + HelpMarker( + "This section allows you to interact and see the effect of various sizing policies " + "depending on whether Scroll is enabled and the contents of your columns."); enum ContentsType { CT_ShowWidth, CT_ShortText, CT_LongText, CT_Button, CT_FillButton, CT_InputText }; static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable; @@ -4366,7 +4567,9 @@ static void ShowDemoWindowTables() if (contents_type == CT_FillButton) { ImGui::SameLine(); - HelpMarker("Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop where contents width can feed into auto-column width can feed into contents width."); + HelpMarker( + "Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop " + "where contents width can feed into auto-column width can feed into contents width."); } ImGui::DragInt("Columns", &column_count, 0.1f, 1, 64, "%d", ImGuiSliderFlags_AlwaysClamp); ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); @@ -4412,7 +4615,9 @@ static void ShowDemoWindowTables() IMGUI_DEMO_MARKER("Tables/Vertical scrolling, with clipping"); if (ImGui::TreeNode("Vertical scrolling, with clipping")) { - HelpMarker("Here we activate ScrollY, which will create a child window container to allow hosting scrollable contents.\n\nWe also demonstrate using ImGuiListClipper to virtualize the submission of many items."); + HelpMarker( + "Here we activate ScrollY, which will create a child window container to allow hosting scrollable contents.\n\n" + "We also demonstrate using ImGuiListClipper to virtualize the submission of many items."); static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; PushStyleCompact(); @@ -4458,8 +4663,9 @@ static void ShowDemoWindowTables() HelpMarker( "When ScrollX is enabled, the default sizing policy becomes ImGuiTableFlags_SizingFixedFit, " "as automatically stretching columns doesn't make much sense with horizontal scrolling.\n\n" - "Also note that as of the current version, you will almost always want to enable ScrollY along with ScrollX," - "because the container window won't automatically extend vertically to fix contents (this may be improved in future versions)."); + "Also note that as of the current version, you will almost always want to enable ScrollY along with ScrollX, " + "because the container window won't automatically extend vertically to fix contents " + "(this may be improved in future versions)."); static ImGuiTableFlags flags = ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable; static int freeze_cols = 1; static int freeze_rows = 1; @@ -4516,7 +4722,8 @@ static void ShowDemoWindowTables() HelpMarker( "Showcase using Stretch columns + ScrollX together: " "this is rather unusual and only makes sense when specifying an 'inner_width' for the table!\n" - "Without an explicit value, inner_width is == outer_size.x and therefore using Stretch columns + ScrollX together doesn't make sense."); + "Without an explicit value, inner_width is == outer_size.x and therefore using Stretch columns " + "along with ScrollX doesn't make sense."); static ImGuiTableFlags flags2 = ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg | ImGuiTableFlags_ContextMenuInBody; static float inner_width = 1000.0f; PushStyleCompact(); @@ -4574,8 +4781,9 @@ static void ShowDemoWindowTables() } // Create the real table we care about for the example! - // We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above, otherwise in - // a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible + resizing the parent window down) + // We use a scrolling table to be able to showcase the difference between the _IsEnabled and _IsVisible flags above, + // otherwise in a non-scrolling table columns are always visible (unless using ImGuiTableFlags_NoKeepColumnsVisible + // + resizing the parent window down). const ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV @@ -4583,15 +4791,22 @@ static void ShowDemoWindowTables() ImVec2 outer_size = ImVec2(0.0f, TEXT_BASE_HEIGHT * 9); if (ImGui::BeginTable("table_columns_flags", column_count, flags, outer_size)) { + bool has_angled_header = false; for (int column = 0; column < column_count; column++) + { + has_angled_header |= (column_flags[column] & ImGuiTableColumnFlags_AngledHeader) != 0; ImGui::TableSetupColumn(column_names[column], column_flags[column]); + } + if (has_angled_header) + ImGui::TableAngledHeadersRow(); ImGui::TableHeadersRow(); for (int column = 0; column < column_count; column++) column_flags_out[column] = ImGui::TableGetColumnFlags(column); float indent_step = (float)((int)TEXT_BASE_WIDTH / 2); for (int row = 0; row < 8; row++) { - ImGui::Indent(indent_step); // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags. + // Add some indentation to demonstrate usage of per-column IndentEnable/IndentDisable flags. + ImGui::Indent(indent_step); ImGui::TableNextRow(); for (int column = 0; column < column_count; column++) { @@ -4640,7 +4855,9 @@ static void ShowDemoWindowTables() ImGui::EndTable(); } - HelpMarker("Using TableSetupColumn() to setup explicit width.\n\nUnless _NoKeepColumnsVisible is set, fixed columns with set width may still be shrunk down if there's not enough space in the host."); + HelpMarker( + "Using TableSetupColumn() to setup explicit width.\n\nUnless _NoKeepColumnsVisible is set, " + "fixed columns with set width may still be shrunk down if there's not enough space in the host."); static ImGuiTableFlags flags2 = ImGuiTableFlags_None; PushStyleCompact(); @@ -4650,7 +4867,8 @@ static void ShowDemoWindowTables() PopStyleCompact(); if (ImGui::BeginTable("table2", 4, flags2)) { - // We could also set ImGuiTableFlags_SizingFixedFit on the table and all columns will default to ImGuiTableColumnFlags_WidthFixed. + // We could also set ImGuiTableFlags_SizingFixedFit on the table and then all columns + // will default to ImGuiTableColumnFlags_WidthFixed. ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 15.0f); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, TEXT_BASE_WIDTH * 30.0f); @@ -4722,10 +4940,13 @@ static void ShowDemoWindowTables() IMGUI_DEMO_MARKER("Tables/Row height"); if (ImGui::TreeNode("Row height")) { - HelpMarker("You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\nWe cannot honor a _maximum_ row height as that would require a unique clipping rectangle per row."); - if (ImGui::BeginTable("table_row_height", 1, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerV)) + HelpMarker( + "You can pass a 'min_row_height' to TableNextRow().\n\nRows are padded with 'style.CellPadding.y' on top and bottom, " + "so effectively the minimum row height will always be >= 'style.CellPadding.y * 2.0f'.\n\n" + "We cannot honor a _maximum_ row height as that would require a unique clipping rectangle per row."); + if (ImGui::BeginTable("table_row_height", 1, ImGuiTableFlags_Borders)) { - for (int row = 0; row < 10; row++) + for (int row = 0; row < 8; row++) { float min_row_height = (float)(int)(TEXT_BASE_HEIGHT * 0.30f * row); ImGui::TableNextRow(ImGuiTableRowFlags_None, min_row_height); @@ -4734,6 +4955,48 @@ static void ShowDemoWindowTables() } ImGui::EndTable(); } + + HelpMarker( + "Showcase using SameLine(0,0) to share Current Line Height between cells.\n\n" + "Please note that Tables Row Height is not the same thing as Current Line Height, " + "as a table cell may contains multiple lines."); + if (ImGui::BeginTable("table_share_lineheight", 2, ImGuiTableFlags_Borders)) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::ColorButton("##1", ImVec4(0.13f, 0.26f, 0.40f, 1.0f), ImGuiColorEditFlags_None, ImVec2(40, 40)); + ImGui::TableNextColumn(); + ImGui::Text("Line 1"); + ImGui::Text("Line 2"); + + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::ColorButton("##2", ImVec4(0.13f, 0.26f, 0.40f, 1.0f), ImGuiColorEditFlags_None, ImVec2(40, 40)); + ImGui::TableNextColumn(); + ImGui::SameLine(0.0f, 0.0f); // Reuse line height from previous column + ImGui::Text("Line 1, with SameLine(0,0)"); + ImGui::Text("Line 2"); + + ImGui::EndTable(); + } + + HelpMarker("Showcase altering CellPadding.y between rows. Note that CellPadding.x is locked for the entire table."); + if (ImGui::BeginTable("table_changing_cellpadding_y", 1, ImGuiTableFlags_Borders)) + { + ImGuiStyle& style = ImGui::GetStyle(); + for (int row = 0; row < 8; row++) + { + if ((row % 3) == 2) + ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(style.CellPadding.x, 20.0f)); + ImGui::TableNextRow(ImGuiTableRowFlags_None); + ImGui::TableNextColumn(); + ImGui::Text("CellPadding.y = %.2f", style.CellPadding.y); + if ((row % 3) == 2) + ImGui::PopStyleVar(); + } + ImGui::EndTable(); + } + ImGui::TreePop(); } @@ -4869,6 +5132,11 @@ static void ShowDemoWindowTables() { static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody; + static ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAllColumns; + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &tree_node_flags, ImGuiTreeNodeFlags_SpanAllColumns); + + HelpMarker("See \"Columns flags\" section to configure how indentation is applied to individual columns."); if (ImGui::BeginTable("3ways", 3, flags)) { // The first column will use the default _WidthStretch when ScrollX is Off and _WidthFixed when ScrollX is On @@ -4892,7 +5160,7 @@ static void ShowDemoWindowTables() const bool is_folder = (node->ChildCount > 0); if (is_folder) { - bool open = ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_SpanFullWidth); + bool open = ImGui::TreeNodeEx(node->Name, tree_node_flags); ImGui::TableNextColumn(); ImGui::TextDisabled("--"); ImGui::TableNextColumn(); @@ -4906,7 +5174,7 @@ static void ShowDemoWindowTables() } else { - ImGui::TreeNodeEx(node->Name, ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::TreeNodeEx(node->Name, tree_node_flags | ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_NoTreePushOnOpen); ImGui::TableNextColumn(); ImGui::Text("%d", node->Size); ImGui::TableNextColumn(); @@ -4941,7 +5209,8 @@ static void ShowDemoWindowTables() { HelpMarker( "Showcase using PushItemWidth() and how it is preserved on a per-column basis.\n\n" - "Note that on auto-resizing non-resizable fixed columns, querying the content width for e.g. right-alignment doesn't make sense."); + "Note that on auto-resizing non-resizable fixed columns, querying the content width for " + "e.g. right-alignment doesn't make sense."); if (ImGui::BeginTable("table_item_width", 3, ImGuiTableFlags_Borders)) { ImGui::TableSetupColumn("small"); @@ -5027,13 +5296,69 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } - // Demonstrate creating custom context menus inside columns, while playing it nice with context menus provided by TableHeadersRow()/TableHeader() + // Demonstrate using ImGuiTableColumnFlags_AngledHeader flag to create angled headers + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + IMGUI_DEMO_MARKER("Tables/Angled headers"); + if (ImGui::TreeNode("Angled headers")) + { + const char* column_names[] = { "Track", "cabasa", "ride", "smash", "tom-hi", "tom-mid", "tom-low", "hihat-o", "hihat-c", "snare-s", "snare-c", "clap", "rim", "kick" }; + const int columns_count = IM_ARRAYSIZE(column_names); + const int rows_count = 12; + + static ImGuiTableFlags table_flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_Hideable | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_HighlightHoveredColumn; + static bool bools[columns_count * rows_count] = {}; // Dummy storage selection storage + static int frozen_cols = 1; + static int frozen_rows = 2; + ImGui::CheckboxFlags("_ScrollX", &table_flags, ImGuiTableFlags_ScrollX); + ImGui::CheckboxFlags("_ScrollY", &table_flags, ImGuiTableFlags_ScrollY); + ImGui::CheckboxFlags("_NoBordersInBody", &table_flags, ImGuiTableFlags_NoBordersInBody); + ImGui::CheckboxFlags("_HighlightHoveredColumn", &table_flags, ImGuiTableFlags_HighlightHoveredColumn); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::SliderInt("Frozen columns", &frozen_cols, 0, 2); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::SliderInt("Frozen rows", &frozen_rows, 0, 2); + + if (ImGui::BeginTable("table_angled_headers", columns_count, table_flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 12))) + { + ImGui::TableSetupColumn(column_names[0], ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_NoReorder); + for (int n = 1; n < columns_count; n++) + ImGui::TableSetupColumn(column_names[n], ImGuiTableColumnFlags_AngledHeader | ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupScrollFreeze(frozen_cols, frozen_rows); + + ImGui::TableAngledHeadersRow(); // Draw angled headers for all columns with the ImGuiTableColumnFlags_AngledHeader flag. + ImGui::TableHeadersRow(); // Draw remaining headers and allow access to context-menu and other functions. + for (int row = 0; row < rows_count; row++) + { + ImGui::PushID(row); + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); + ImGui::AlignTextToFramePadding(); + ImGui::Text("Track %d", row); + for (int column = 1; column < columns_count; column++) + if (ImGui::TableSetColumnIndex(column)) + { + ImGui::PushID(column); + ImGui::Checkbox("", &bools[row * columns_count + column]); + ImGui::PopID(); + } + ImGui::PopID(); + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + // Demonstrate creating custom context menus inside columns, + // while playing it nice with context menus provided by TableHeadersRow()/TableHeader() if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); IMGUI_DEMO_MARKER("Tables/Context menus"); if (ImGui::TreeNode("Context menus")) { - HelpMarker("By default, right-clicking over a TableHeadersRow()/TableHeader() line will open the default context-menu.\nUsing ImGuiTableFlags_ContextMenuInBody we also allow right-clicking over columns body."); + HelpMarker( + "By default, right-clicking over a TableHeadersRow()/TableHeader() line will open the default context-menu.\n" + "Using ImGuiTableFlags_ContextMenuInBody we also allow right-clicking over columns body."); static ImGuiTableFlags flags1 = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_ContextMenuInBody; PushStyleCompact(); @@ -5070,7 +5395,9 @@ static void ShowDemoWindowTables() // [2.1] Right-click on the TableHeadersRow() line to open the default table context menu. // [2.2] Right-click on the ".." to open a custom popup // [2.3] Right-click in columns to open another custom popup - HelpMarker("Demonstrate mixing table context menu (over header), item context button (over button) and custom per-colum context menu (over column body)."); + HelpMarker( + "Demonstrate mixing table context menu (over header), item context button (over button) " + "and custom per-colunm context menu (over column body)."); ImGuiTableFlags flags2 = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders; if (ImGui::BeginTable("table_context_menu_2", COLUMNS_COUNT, flags2)) { @@ -5145,6 +5472,7 @@ static void ShowDemoWindowTables() static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings; ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); ImGui::CheckboxFlags("ImGuiTableFlags_SizingFixedFit", &flags, ImGuiTableFlags_SizingFixedFit); + ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn); for (int n = 0; n < 3; n++) { char buf[32]; @@ -5225,14 +5553,11 @@ static void ShowDemoWindowTables() ImGui::TableHeadersRow(); // Sort our data if sort specs have been changed! - if (ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs()) - if (sorts_specs->SpecsDirty) + if (ImGuiTableSortSpecs* sort_specs = ImGui::TableGetSortSpecs()) + if (sort_specs->SpecsDirty) { - MyItem::s_current_sort_specs = sorts_specs; // Store in variable accessible by the sort function. - if (items.Size > 1) - qsort(&items[0], (size_t)items.Size, sizeof(items[0]), MyItem::CompareWithSortSpecs); - MyItem::s_current_sort_specs = NULL; - sorts_specs->SpecsDirty = false; + MyItem::SortWithSortSpecs(sort_specs, items.Data, items.Size); + sort_specs->SpecsDirty = false; } // Demonstrate using clipper for large vertical lists @@ -5275,6 +5600,7 @@ static void ShowDemoWindowTables() | ImGuiTableFlags_RowBg | ImGuiTableFlags_Borders | ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_SizingFixedFit; + static ImGuiTableColumnFlags columns_base_flags = ImGuiTableColumnFlags_None; enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable, CT_SelectableSpanRow }; static int contents_type = CT_SelectableSpanRow; @@ -5368,9 +5694,17 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } - if (ImGui::TreeNodeEx("Other:", ImGuiTreeNodeFlags_DefaultOpen)) + if (ImGui::TreeNodeEx("Headers:", ImGuiTreeNodeFlags_DefaultOpen)) { ImGui::Checkbox("show_headers", &show_headers); + ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn); + ImGui::CheckboxFlags("ImGuiTableColumnFlags_AngledHeader", &columns_base_flags, ImGuiTableColumnFlags_AngledHeader); + ImGui::SameLine(); HelpMarker("Enable AngledHeader on all columns. Best enabled on selected narrow columns (see \"Angled headers\" section of the demo)."); + ImGui::TreePop(); + } + + if (ImGui::TreeNodeEx("Other:", ImGuiTreeNodeFlags_DefaultOpen)) + { ImGui::Checkbox("show_wrapped_text", &show_wrapped_text); ImGui::DragFloat2("##OuterSize", &outer_size_value.x); @@ -5431,24 +5765,22 @@ static void ShowDemoWindowTables() // Declare columns // We use the "user_id" parameter of TableSetupColumn() to specify a user id that will be stored in the sort specifications. // This is so our sort function can identify a column given our own identifier. We could also identify them based on their index! - ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, 0.0f, MyItemColumnID_ID); - ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name); - ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action); - ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending, 0.0f, MyItemColumnID_Quantity); - ImGui::TableSetupColumn("Description", (flags & ImGuiTableFlags_NoHostExtendX) ? 0 : ImGuiTableColumnFlags_WidthStretch, 0.0f, MyItemColumnID_Description); - ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort); + ImGui::TableSetupColumn("ID", columns_base_flags | ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, 0.0f, MyItemColumnID_ID); + ImGui::TableSetupColumn("Name", columns_base_flags | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Name); + ImGui::TableSetupColumn("Action", columns_base_flags | ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, 0.0f, MyItemColumnID_Action); + ImGui::TableSetupColumn("Quantity", columns_base_flags | ImGuiTableColumnFlags_PreferSortDescending, 0.0f, MyItemColumnID_Quantity); + ImGui::TableSetupColumn("Description", columns_base_flags | ((flags & ImGuiTableFlags_NoHostExtendX) ? 0 : ImGuiTableColumnFlags_WidthStretch), 0.0f, MyItemColumnID_Description); + ImGui::TableSetupColumn("Hidden", columns_base_flags | ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort); ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows); // Sort our data if sort specs have been changed! - ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs(); - if (sorts_specs && sorts_specs->SpecsDirty) + ImGuiTableSortSpecs* sort_specs = ImGui::TableGetSortSpecs(); + if (sort_specs && sort_specs->SpecsDirty) items_need_sort = true; - if (sorts_specs && items_need_sort && items.Size > 1) + if (sort_specs && items_need_sort && items.Size > 1) { - MyItem::s_current_sort_specs = sorts_specs; // Store in variable accessible by the sort function. - qsort(&items[0], (size_t)items.Size, sizeof(items[0]), MyItem::CompareWithSortSpecs); - MyItem::s_current_sort_specs = NULL; - sorts_specs->SpecsDirty = false; + MyItem::SortWithSortSpecs(sort_specs, items.Data, items.Size); + sort_specs->SpecsDirty = false; } items_need_sort = false; @@ -5457,6 +5789,8 @@ static void ShowDemoWindowTables() const bool sorts_specs_using_quantity = (ImGui::TableGetColumnFlags(3) & ImGuiTableColumnFlags_IsSorted) != 0; // Show headers + if (show_headers && (columns_base_flags & ImGuiTableColumnFlags_AngledHeader) != 0) + ImGui::TableAngledHeadersRow(); if (show_headers) ImGui::TableHeadersRow(); @@ -5522,7 +5856,7 @@ static void ShowDemoWindowTables() // Here we demonstrate marking our data set as needing to be sorted again if we modified a quantity, // and we are currently sorting on the column showing the Quantity. // To avoid triggering a sort while holding the button, we only trigger it when the button has been released. - // You will probably need a more advanced system in your code if you want to automatically sort when a specific entry changes. + // You will probably need some extra logic if you want to automatically sort when a specific entry changes. if (ImGui::TableSetColumnIndex(2)) { if (ImGui::SmallButton("Chop")) { item->Quantity += 1; } @@ -5724,7 +6058,7 @@ static void ShowDemoWindowColumns() { ImGui::SetNextWindowContentSize(ImVec2(1500.0f, 0.0f)); ImVec2 child_size = ImVec2(0, ImGui::GetFontSize() * 20.0f); - ImGui::BeginChild("##ScrollingRegion", child_size, false, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::BeginChild("##ScrollingRegion", child_size, ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar); ImGui::Columns(10); // Also demonstrate using clipper for large vertical lists @@ -5810,13 +6144,15 @@ static void ShowDemoWindowInputs() for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) if (ImGui::IsMouseDown(i)) { ImGui::SameLine(); ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]); } ImGui::Text("Mouse wheel: %.1f", io.MouseWheel); - // We iterate both legacy native range and named ImGuiKey ranges, which is a little odd but this allows displaying the data for old/new backends. - // User code should never have to go through such hoops! You can generally iterate between ImGuiKey_NamedKey_BEGIN and ImGuiKey_NamedKey_END. + // We iterate both legacy native range and named ImGuiKey ranges. This is a little unusual/odd but this allows + // displaying the data for old/new backends. + // User code should never have to go through such hoops! + // You can generally iterate between ImGuiKey_NamedKey_BEGIN and ImGuiKey_NamedKey_END. #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO struct funcs { static bool IsLegacyNativeDupe(ImGuiKey) { return false; } }; ImGuiKey start_key = ImGuiKey_NamedKey_BEGIN; #else - struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array + struct funcs { static bool IsLegacyNativeDupe(ImGuiKey key) { return key >= 0 && key < 512 && ImGui::GetIO().KeyMap[key] != -1; } }; // Hide Native<>ImGuiKey duplicates when both exists in the array ImGuiKey start_key = (ImGuiKey)0; #endif ImGui::Text("Keys down:"); for (ImGuiKey key = start_key; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) continue; ImGui::SameLine(); ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key); } @@ -5850,7 +6186,8 @@ static void ShowDemoWindowInputs() { HelpMarker( "Hovering the colored canvas will override io.WantCaptureXXX fields.\n" - "Notice how normally (when set to none), the value of io.WantCaptureKeyboard would be false when hovering and true when clicking."); + "Notice how normally (when set to none), the value of io.WantCaptureKeyboard would be false when hovering " + "and true when clicking."); static int capture_override_mouse = -1; static int capture_override_keyboard = -1; const char* capture_override_desc[] = { "None", "Set to false", "Set to true" }; @@ -6016,7 +6353,7 @@ void ImGui::ShowAboutWindow(bool* p_open) bool copy_to_clipboard = ImGui::Button("Copy to clipboard"); ImVec2 child_size = ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 18); - ImGui::BeginChildFrame(ImGui::GetID("cfg_infos"), child_size, ImGuiWindowFlags_NoMove); + ImGui::BeginChild(ImGui::GetID("cfg_infos"), child_size, ImGuiChildFlags_FrameStyle); if (copy_to_clipboard) { ImGui::LogToClipboard(); @@ -6132,7 +6469,7 @@ void ImGui::ShowAboutWindow(bool* p_open) ImGui::LogText("\n```\n"); ImGui::LogFinish(); } - ImGui::EndChildFrame(); + ImGui::EndChild(); } ImGui::End(); } @@ -6156,9 +6493,8 @@ void ImGui::ShowFontSelector(const char* label) ImFont* font_current = ImGui::GetFont(); if (ImGui::BeginCombo(label, font_current->GetDebugName())) { - for (int n = 0; n < io.Fonts->Fonts.Size; n++) + for (ImFont* font : io.Fonts->Fonts) { - ImFont* font = io.Fonts->Fonts[n]; ImGui::PushID((void*)font); if (ImGui::Selectable(font->GetDebugName(), font == font_current)) io.FontDefault = font; @@ -6244,7 +6580,6 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SeparatorText("Main"); ImGui::SliderFloat2("WindowPadding", (float*)&style.WindowPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("FramePadding", (float*)&style.FramePadding, 0.0f, 20.0f, "%.0f"); - ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("ItemSpacing", (float*)&style.ItemSpacing, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("ItemInnerSpacing", (float*)&style.ItemInnerSpacing, 0.0f, 20.0f, "%.0f"); ImGui::SliderFloat2("TouchExtraPadding", (float*)&style.TouchExtraPadding, 0.0f, 10.0f, "%.0f"); @@ -6258,6 +6593,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SliderFloat("PopupBorderSize", &style.PopupBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f"); + ImGui::SliderFloat("TabBarBorderSize", &style.TabBarBorderSize, 0.0f, 2.0f, "%.0f"); ImGui::SeparatorText("Rounding"); ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f"); @@ -6268,6 +6604,10 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SliderFloat("GrabRounding", &style.GrabRounding, 0.0f, 12.0f, "%.0f"); ImGui::SliderFloat("TabRounding", &style.TabRounding, 0.0f, 12.0f, "%.0f"); + ImGui::SeparatorText("Tables"); + ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f"); + ImGui::SliderAngle("TableAngledHeadersAngle", &style.TableAngledHeadersAngle, -50.0f, +50.0f); + ImGui::SeparatorText("Widgets"); ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); int window_menu_button_position = style.WindowMenuButtonPosition + 1; @@ -6338,14 +6678,21 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "Left-click on color square to open color picker,\n" "Right-click to open edit options menu."); - ImGui::BeginChild("##colors", ImVec2(0, 0), true, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); - ImGui::PushItemWidth(-160); + ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX)); + ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); for (int i = 0; i < ImGuiCol_COUNT; i++) { const char* name = ImGui::GetStyleColorName(i); if (!filter.PassFilter(name)) continue; ImGui::PushID(i); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (ImGui::Button("?")) + ImGui::DebugFlashStyleColor((ImGuiCol)i); + ImGui::SetItemTooltip("Flash given color to identify places where it is used."); + ImGui::SameLine(); +#endif ImGui::ColorEdit4("##color", (float*)&style.Colors[i], ImGuiColorEditFlags_AlphaBar | alpha_flags); if (memcmp(&style.Colors[i], &ref->Colors[i], sizeof(ImVec4)) != 0) { @@ -6565,7 +6912,7 @@ static void ShowExampleMenuFile() { static bool enabled = true; ImGui::MenuItem("Enabled", "", &enabled); - ImGui::BeginChild("child", ImVec2(0, 60), true); + ImGui::BeginChild("child", ImVec2(0, 60), ImGuiChildFlags_Border); for (int i = 0; i < 10; i++) ImGui::Text("Scrolling Text %d", i); ImGui::EndChild(); @@ -6731,7 +7078,7 @@ struct ExampleAppConsole // Reserve enough left-over height for 1 separator + 1 input text const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); - if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), false, ImGuiWindowFlags_HorizontalScrollbar)) + if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar)) { if (ImGui::BeginPopupContextWindow()) { @@ -6766,9 +7113,8 @@ struct ExampleAppConsole ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Tighten spacing if (copy_to_clipboard) ImGui::LogToClipboard(); - for (int i = 0; i < Items.Size; i++) + for (const char* item : Items) { - const char* item = Items[i]; if (!Filter.PassFilter(item)) continue; @@ -7043,7 +7389,7 @@ struct ExampleAppLog ImGui::Separator(); - if (ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) + if (ImGui::BeginChild("scrolling", ImVec2(0, 0), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar)) { if (clear) Clear(); @@ -7162,7 +7508,7 @@ static void ShowExampleAppLayout(bool* p_open) // Left static int selected = 0; { - ImGui::BeginChild("left pane", ImVec2(150, 0), true); + ImGui::BeginChild("left pane", ImVec2(150, 0), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX); for (int i = 0; i < 100; i++) { // FIXME: Good candidate to use ImGuiSelectableFlags_SelectOnNav @@ -7257,6 +7603,7 @@ static void ShowPlaceholderObject(const char* prefix, int uid) } // Demonstrate create a simple property editor. +// This demo is a bit lackluster nowadays, would be nice to improve. static void ShowExampleAppPropertyEditor(bool* p_open) { ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver); @@ -7265,23 +7612,24 @@ static void ShowExampleAppPropertyEditor(bool* p_open) ImGui::End(); return; } - IMGUI_DEMO_MARKER("Examples/Property Editor"); + IMGUI_DEMO_MARKER("Examples/Property Editor"); HelpMarker( "This example shows how you may implement a property editor using two columns.\n" - "All objects/fields data are dummies here.\n" - "Remember that in many simple cases, you can use ImGui::SameLine(xxx) to position\n" - "your cursor horizontally instead of using the Columns() API."); + "All objects/fields data are dummies here.\n"); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); - if (ImGui::BeginTable("split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable)) + if (ImGui::BeginTable("##split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY)) { + ImGui::TableSetupScrollFreeze(0, 1); + ImGui::TableSetupColumn("Object"); + ImGui::TableSetupColumn("Contents"); + ImGui::TableHeadersRow(); + // Iterate placeholder objects (all the same data) for (int obj_i = 0; obj_i < 4; obj_i++) - { ShowPlaceholderObject("Object", obj_i); - //ImGui::Separator(); - } + ImGui::EndTable(); } ImGui::PopStyleVar(); @@ -7388,18 +7736,31 @@ static void ShowExampleAppConstrainedResize(bool* p_open) { // Helper functions to demonstrate programmatic constraints // FIXME: This doesn't take account of decoration size (e.g. title bar), library should make this easier. - static void AspectRatio(ImGuiSizeCallbackData* data) { float aspect_ratio = *(float*)data->UserData; data->DesiredSize.x = IM_MAX(data->CurrentSize.x, data->CurrentSize.y); data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio); } - static void Square(ImGuiSizeCallbackData* data) { data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->CurrentSize.x, data->CurrentSize.y); } - static void Step(ImGuiSizeCallbackData* data) { float step = *(float*)data->UserData; data->DesiredSize = ImVec2((int)(data->CurrentSize.x / step + 0.5f) * step, (int)(data->CurrentSize.y / step + 0.5f) * step); } + // FIXME: None of the three demos works consistently when resizing from borders. + static void AspectRatio(ImGuiSizeCallbackData* data) + { + float aspect_ratio = *(float*)data->UserData; + data->DesiredSize.y = (float)(int)(data->DesiredSize.x / aspect_ratio); + } + static void Square(ImGuiSizeCallbackData* data) + { + data->DesiredSize.x = data->DesiredSize.y = IM_MAX(data->DesiredSize.x, data->DesiredSize.y); + } + static void Step(ImGuiSizeCallbackData* data) + { + float step = *(float*)data->UserData; + data->DesiredSize = ImVec2((int)(data->DesiredSize.x / step + 0.5f) * step, (int)(data->DesiredSize.y / step + 0.5f) * step); + } }; const char* test_desc[] = { "Between 100x100 and 500x500", "At least 100x100", - "Resize vertical only", - "Resize horizontal only", + "Resize vertical + lock current width", + "Resize horizontal + lock current height", "Width Between 400 and 500", + "Height at least 400", "Custom: Aspect Ratio 16:9", "Custom: Always Square", "Custom: Fixed Steps (100)", @@ -7408,7 +7769,7 @@ static void ShowExampleAppConstrainedResize(bool* p_open) // Options static bool auto_resize = false; static bool window_padding = true; - static int type = 5; // Aspect Ratio + static int type = 6; // Aspect Ratio static int display_lines = 10; // Submit constraint @@ -7416,12 +7777,13 @@ static void ShowExampleAppConstrainedResize(bool* p_open) float fixed_step = 100.0f; if (type == 0) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(500, 500)); // Between 100x100 and 500x500 if (type == 1) ImGui::SetNextWindowSizeConstraints(ImVec2(100, 100), ImVec2(FLT_MAX, FLT_MAX)); // Width > 100, Height > 100 - if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Vertical only - if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Horizontal only + if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Resize vertical + lock current width + if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Resize horizontal + lock current height if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width Between and 400 and 500 - if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio); // Aspect ratio - if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square - if (type == 7) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)&fixed_step); // Fixed Step + if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 500), ImVec2(-1, FLT_MAX)); // Height at least 400 + if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio); // Aspect ratio + if (type == 7) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square + if (type == 8) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)&fixed_step); // Fixed Step // Submit window if (!window_padding) @@ -7673,6 +8035,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) float th = (n == 0) ? 1.0f : thickness; draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle + draw_list->AddEllipse(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, sz*0.3f, col, -0.3f, circle_segments, th); x += sz + spacing; // Ellipse draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, ImDrawFlags_None, th); x += sz + spacing; // Square draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, ImDrawFlags_None, th); x += sz + spacing; // Square with all rounded corners draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners @@ -7693,8 +8056,9 @@ static void ShowExampleAppCustomRendering(bool* p_open) x = p.x + 4; y += sz + spacing; } - draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz*0.5f, col, ngon_sides); x += sz + spacing; // N-gon - draw_list->AddCircleFilled(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments); x += sz + spacing; // Circle + draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, ngon_sides); x += sz + spacing; // N-gon + draw_list->AddCircleFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, circle_segments); x += sz + spacing; // Circle + draw_list->AddEllipseFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, sz * 0.3f, col, -0.3f, circle_segments); x += sz + spacing;// Ellipse draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners @@ -7705,7 +8069,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); - ImGui::Dummy(ImVec2((sz + spacing) * 10.2f, (sz + spacing) * 3.0f)); + ImGui::Dummy(ImVec2((sz + spacing) * 11.2f, (sz + spacing) * 3.0f)); ImGui::PopItemWidth(); ImGui::EndTabItem(); } @@ -7727,7 +8091,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) // To use a child window instead we could use, e.g: // ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Disable padding // ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(50, 50, 50, 255)); // Set a background color - // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), true, ImGuiWindowFlags_NoMove); + // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove); // ImGui::PopStyleColor(); // ImGui::PopStyleVar(); // [...] @@ -7825,6 +8189,43 @@ static void ShowExampleAppCustomRendering(bool* p_open) ImGui::EndTabItem(); } + // Demonstrate out-of-order rendering via channels splitting + // We use functions in ImDrawList as each draw list contains a convenience splitter, + // but you can also instantiate your own ImDrawListSplitter if you need to nest them. + if (ImGui::BeginTabItem("Draw Channels")) + { + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + { + ImGui::Text("Blue shape is drawn first: appears in back"); + ImGui::Text("Red shape is drawn after: appears in front"); + ImVec2 p0 = ImGui::GetCursorScreenPos(); + draw_list->AddRectFilled(ImVec2(p0.x, p0.y), ImVec2(p0.x + 50, p0.y + 50), IM_COL32(0, 0, 255, 255)); // Blue + draw_list->AddRectFilled(ImVec2(p0.x + 25, p0.y + 25), ImVec2(p0.x + 75, p0.y + 75), IM_COL32(255, 0, 0, 255)); // Red + ImGui::Dummy(ImVec2(75, 75)); + } + ImGui::Separator(); + { + ImGui::Text("Blue shape is drawn first, into channel 1: appears in front"); + ImGui::Text("Red shape is drawn after, into channel 0: appears in back"); + ImVec2 p1 = ImGui::GetCursorScreenPos(); + + // Create 2 channels and draw a Blue shape THEN a Red shape. + // You can create any number of channels. Tables API use 1 channel per column in order to better batch draw calls. + draw_list->ChannelsSplit(2); + draw_list->ChannelsSetCurrent(1); + draw_list->AddRectFilled(ImVec2(p1.x, p1.y), ImVec2(p1.x + 50, p1.y + 50), IM_COL32(0, 0, 255, 255)); // Blue + draw_list->ChannelsSetCurrent(0); + draw_list->AddRectFilled(ImVec2(p1.x + 25, p1.y + 25), ImVec2(p1.x + 75, p1.y + 75), IM_COL32(255, 0, 0, 255)); // Red + + // Flatten/reorder channels. Red shape is in channel 0 and it appears below the Blue shape in channel 1. + // This works by copying draw indices only (vertices are not copied). + draw_list->ChannelsMerge(); + ImGui::Dummy(ImVec2(75, 75)); + ImGui::Text("After reordering, contents of channel 0 appears below channel 1."); + } + ImGui::EndTabItem(); + } + ImGui::EndTabBar(); } @@ -7916,12 +8317,11 @@ struct ExampleAppDocuments // Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag. static void NotifyOfDocumentsClosedElsewhere(ExampleAppDocuments& app) { - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + for (MyDocument& doc : app.Documents) { - MyDocument* doc = &app.Documents[doc_n]; - if (!doc->Open && doc->OpenPrev) - ImGui::SetTabItemClosed(doc->Name); - doc->OpenPrev = doc->Open; + if (!doc.Open && doc.OpenPrev) + ImGui::SetTabItemClosed(doc.Name); + doc.OpenPrev = doc.Open; } } @@ -7946,23 +8346,19 @@ void ShowExampleAppDocuments(bool* p_open) if (ImGui::BeginMenu("File")) { int open_count = 0; - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) - open_count += app.Documents[doc_n].Open ? 1 : 0; + for (MyDocument& doc : app.Documents) + open_count += doc.Open ? 1 : 0; if (ImGui::BeginMenu("Open", open_count < app.Documents.Size)) { - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) - { - MyDocument* doc = &app.Documents[doc_n]; - if (!doc->Open) - if (ImGui::MenuItem(doc->Name)) - doc->DoOpen(); - } + for (MyDocument& doc : app.Documents) + if (!doc.Open && ImGui::MenuItem(doc.Name)) + doc.DoOpen(); ImGui::EndMenu(); } if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0)) - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) - app.Documents[doc_n].DoQueueClose(); + for (MyDocument& doc : app.Documents) + doc.DoQueueClose(); if (ImGui::MenuItem("Exit", "Ctrl+F4") && p_open) *p_open = false; ImGui::EndMenu(); @@ -7973,13 +8369,13 @@ void ShowExampleAppDocuments(bool* p_open) // [Debug] List documents with one checkbox for each for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) { - MyDocument* doc = &app.Documents[doc_n]; + MyDocument& doc = app.Documents[doc_n]; if (doc_n > 0) ImGui::SameLine(); - ImGui::PushID(doc); - if (ImGui::Checkbox(doc->Name, &doc->Open)) - if (!doc->Open) - doc->DoForceClose(); + ImGui::PushID(&doc); + if (ImGui::Checkbox(doc.Name, &doc.Open)) + if (!doc.Open) + doc.DoForceClose(); ImGui::PopID(); } @@ -8008,26 +8404,25 @@ void ShowExampleAppDocuments(bool* p_open) //if (ImGui::GetIO().KeyCtrl) ImGui::SetTabItemSelected(docs[1].Name); // [DEBUG] Test SetTabItemSelected(), probably not very useful as-is anyway.. // Submit Tabs - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) + for (MyDocument& doc : app.Documents) { - MyDocument* doc = &app.Documents[doc_n]; - if (!doc->Open) + if (!doc.Open) continue; - ImGuiTabItemFlags tab_flags = (doc->Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); - bool visible = ImGui::BeginTabItem(doc->Name, &doc->Open, tab_flags); + ImGuiTabItemFlags tab_flags = (doc.Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); + bool visible = ImGui::BeginTabItem(doc.Name, &doc.Open, tab_flags); // Cancel attempt to close when unsaved add to save queue so we can display a popup. - if (!doc->Open && doc->Dirty) + if (!doc.Open && doc.Dirty) { - doc->Open = true; - doc->DoQueueClose(); + doc.Open = true; + doc.DoQueueClose(); } - MyDocument::DisplayContextMenu(doc); + MyDocument::DisplayContextMenu(&doc); if (visible) { - MyDocument::DisplayContents(doc); + MyDocument::DisplayContents(&doc); ImGui::EndTabItem(); } } @@ -8041,15 +8436,12 @@ void ShowExampleAppDocuments(bool* p_open) if (close_queue.empty()) { // Close queue is locked once we started a popup - for (int doc_n = 0; doc_n < app.Documents.Size; doc_n++) - { - MyDocument* doc = &app.Documents[doc_n]; - if (doc->WantClose) + for (MyDocument& doc : app.Documents) + if (doc.WantClose) { - doc->WantClose = false; - close_queue.push_back(doc); + doc.WantClose = false; + close_queue.push_back(&doc); } - } } // Display closing confirmation UI @@ -8075,13 +8467,13 @@ void ShowExampleAppDocuments(bool* p_open) { ImGui::Text("Save change to the following items?"); float item_height = ImGui::GetTextLineHeightWithSpacing(); - if (ImGui::BeginChildFrame(ImGui::GetID("frame"), ImVec2(-FLT_MIN, 6.25f * item_height))) + if (ImGui::BeginChild(ImGui::GetID("frame"), ImVec2(-FLT_MIN, 6.25f * item_height), ImGuiChildFlags_FrameStyle)) { for (int n = 0; n < close_queue.Size; n++) if (close_queue[n]->Dirty) ImGui::Text("%s", close_queue[n]->Name); } - ImGui::EndChildFrame(); + ImGui::EndChild(); ImVec2 button_size(ImGui::GetFontSize() * 7.0f, 0.0f); if (ImGui::Button("Yes", button_size)) diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index a50255b5..40a840c4 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (drawing and font code) /* @@ -63,6 +63,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used @@ -134,7 +135,7 @@ namespace IMGUI_STB_NAMESPACE #define STBTT_sqrt(x) ImSqrt(x) #define STBTT_pow(x,y) ImPow(x,y) #define STBTT_fabs(x) ImFabs(x) -#define STBTT_ifloor(x) ((int)ImFloorSigned(x)) +#define STBTT_ifloor(x) ((int)ImFloor(x)) #define STBTT_iceil(x) ((int)ImCeil(x)) #define STBTT_STATIC #define STB_TRUETYPE_IMPLEMENTATION @@ -385,9 +386,9 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) void ImDrawList::_ResetForNewFrame() { // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. - IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, ClipRect) == 0); - IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, TextureId) == sizeof(ImVec4)); - IM_STATIC_ASSERT(IM_OFFSETOF(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); + IM_STATIC_ASSERT(offsetof(ImDrawCmd, ClipRect) == 0); + IM_STATIC_ASSERT(offsetof(ImDrawCmd, TextureId) == sizeof(ImVec4)); + IM_STATIC_ASSERT(offsetof(ImDrawCmd, VtxOffset) == sizeof(ImVec4) + sizeof(ImTextureID)); if (_Splitter._Count > 1) _Splitter.Merge(this); @@ -474,7 +475,7 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) } // Compare ClipRect, TextureId and VtxOffset with a single memcmp() -#define ImDrawCmd_HeaderSize (IM_OFFSETOF(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) +#define ImDrawCmd_HeaderSize (offsetof(ImDrawCmd, VtxOffset) + sizeof(unsigned int)) #define ImDrawCmd_HeaderCompare(CMD_LHS, CMD_RHS) (memcmp(CMD_LHS, CMD_RHS, ImDrawCmd_HeaderSize)) // Compare ClipRect, TextureId, VtxOffset #define ImDrawCmd_HeaderCopy(CMD_DST, CMD_SRC) (memcpy(CMD_DST, CMD_SRC, ImDrawCmd_HeaderSize)) // Copy ClipRect, TextureId, VtxOffset #define ImDrawCmd_AreSequentialIdxOffset(CMD_0, CMD_1) (CMD_0->IdxOffset + CMD_0->ElemCount == CMD_1->IdxOffset) @@ -560,7 +561,7 @@ int ImDrawList::_CalcCircleAutoSegmentCount(float radius) const { // Automatic segment count const int radius_idx = (int)(radius + 0.999999f); // ceil to never reduce accuracy - if (radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) + if (radius_idx >= 0 && radius_idx < IM_ARRAYSIZE(_Data->CircleSegmentCounts)) return _Data->CircleSegmentCounts[radius_idx]; // Use cached value else return IM_DRAWLIST_CIRCLE_AUTO_SEGMENT_CALC(radius, _Data->CircleSegmentMaxError); @@ -1190,8 +1191,8 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa const float a_min_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_min / (IM_PI * 2.0f); const float a_max_sample_f = IM_DRAWLIST_ARCFAST_SAMPLE_MAX * a_max / (IM_PI * 2.0f); - const int a_min_sample = a_is_reverse ? (int)ImFloorSigned(a_min_sample_f) : (int)ImCeil(a_min_sample_f); - const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloorSigned(a_max_sample_f); + const int a_min_sample = a_is_reverse ? (int)ImFloor(a_min_sample_f) : (int)ImCeil(a_min_sample_f); + const int a_max_sample = a_is_reverse ? (int)ImCeil(a_max_sample_f) : (int)ImFloor(a_max_sample_f); const int a_mid_samples = a_is_reverse ? ImMax(a_min_sample - a_max_sample, 0) : ImMax(a_max_sample - a_min_sample, 0); const float a_min_segment_angle = a_min_sample * IM_PI * 2.0f / IM_DRAWLIST_ARCFAST_SAMPLE_MAX; @@ -1216,6 +1217,27 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa } } +void ImDrawList::PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments) +{ + if (num_segments <= 0) + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + + _Path.reserve(_Path.Size + (num_segments + 1)); + + const float cos_rot = ImCos(rot); + const float sin_rot = ImSin(rot); + for (int i = 0; i <= num_segments; i++) + { + const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); + ImVec2 point(ImCos(a) * radius_x, ImSin(a) * radius_y); + const float rel_x = (point.x * cos_rot) - (point.y * sin_rot); + const float rel_y = (point.x * sin_rot) + (point.y * cos_rot); + point.x = rel_x + center.x; + point.y = rel_y + center.y; + _Path.push_back(point); + } +} + ImVec2 ImBezierCubicCalc(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, float t) { float u = 1.0f - t; @@ -1311,33 +1333,22 @@ void ImDrawList::PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, } } -IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) { + /* + IM_STATIC_ASSERT(ImDrawFlags_RoundCornersTopLeft == (1 << 4)); #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - // Obsoleted in 1.82 (from February 2021) - // Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) - // ~0 --> ImDrawFlags_RoundCornersAll or 0 - if (flags == ~0) - return ImDrawFlags_RoundCornersAll; - - // Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations) - // 0x01 --> ImDrawFlags_RoundCornersTopLeft (VALUE 0x01 OVERLAPS ImDrawFlags_Closed but ImDrawFlags_Closed is never valid in this path!) - // 0x02 --> ImDrawFlags_RoundCornersTopRight - // 0x03 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersTopRight - // 0x04 --> ImDrawFlags_RoundCornersBotLeft - // 0x05 --> ImDrawFlags_RoundCornersTopLeft | ImDrawFlags_RoundCornersBotLeft - // ... - // 0x0F --> ImDrawFlags_RoundCornersAll or 0 - // (See all values in ImDrawCornerFlags_) - if (flags >= 0x01 && flags <= 0x0F) - return (flags << 4); - + // Obsoleted in 1.82 (from February 2021). This code was stripped/simplified and mostly commented in 1.90 (from September 2023) + // - Legacy Support for hard coded ~0 (used to be a suggested equivalent to ImDrawCornerFlags_All) + if (flags == ~0) { return ImDrawFlags_RoundCornersAll; } + // - Legacy Support for hard coded 0x01 to 0x0F (matching 15 out of 16 old flags combinations). Read details in older version of this code. + if (flags >= 0x01 && flags <= 0x0F) { return (flags << 4); } // We cannot support hard coded 0x00 with 'float rounding > 0.0f' --> replace with ImDrawFlags_RoundCornersNone or use 'float rounding = 0.0f' #endif - - // If this triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values. - // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc... + */ + // If this assert triggers, please update your code replacing hardcoded values with new ImDrawFlags_RoundCorners* values. + // Note that ImDrawFlags_Closed (== 0x01) is an invalid flag for AddRect(), AddRectFilled(), PathRect() etc. anyway. + // See details in 1.82 Changelog as well as 2021/03/12 and 2023/09/08 entries in "API BREAKING CHANGES" section. IM_ASSERT((flags & 0x0F) == 0 && "Misuse of legacy hardcoded ImDrawCornerFlags values!"); if ((flags & ImDrawFlags_RoundCornersMask_) == 0) @@ -1348,10 +1359,12 @@ static inline ImDrawFlags FixRectCornerFlags(ImDrawFlags flags) void ImDrawList::PathRect(const ImVec2& a, const ImVec2& b, float rounding, ImDrawFlags flags) { - flags = FixRectCornerFlags(flags); - rounding = ImMin(rounding, ImFabs(b.x - a.x) * ( ((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f ) - 1.0f); - rounding = ImMin(rounding, ImFabs(b.y - a.y) * ( ((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f ) - 1.0f); - + if (rounding >= 0.5f) + { + flags = FixRectCornerFlags(flags); + rounding = ImMin(rounding, ImFabs(b.x - a.x) * (((flags & ImDrawFlags_RoundCornersTop) == ImDrawFlags_RoundCornersTop) || ((flags & ImDrawFlags_RoundCornersBottom) == ImDrawFlags_RoundCornersBottom) ? 0.5f : 1.0f) - 1.0f); + rounding = ImMin(rounding, ImFabs(b.y - a.y) * (((flags & ImDrawFlags_RoundCornersLeft) == ImDrawFlags_RoundCornersLeft) || ((flags & ImDrawFlags_RoundCornersRight) == ImDrawFlags_RoundCornersRight) ? 0.5f : 1.0f) - 1.0f); + } if (rounding < 0.5f || (flags & ImDrawFlags_RoundCornersMask_) == ImDrawFlags_RoundCornersNone) { PathLineTo(a); @@ -1544,6 +1557,35 @@ void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, in PathFillConvex(col); } +// Ellipse +void ImDrawList::AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments, float thickness) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + if (num_segments <= 0) + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; + PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); + PathStroke(col, true, thickness); +} + +void ImDrawList::AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments) +{ + if ((col & IM_COL32_A_MASK) == 0) + return; + + if (num_segments <= 0) + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + + // Because we are filling a closed shape we remove 1 from the count of segments/points + const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; + PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); + PathFillConvex(col); +} + // Cubic Bezier takes 4 controls points void ImDrawList::AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments) { @@ -1808,6 +1850,63 @@ void ImDrawListSplitter::SetCurrentChannel(ImDrawList* draw_list, int idx) // [SECTION] ImDrawData //----------------------------------------------------------------------------- +void ImDrawData::Clear() +{ + Valid = false; + CmdListsCount = TotalIdxCount = TotalVtxCount = 0; + CmdLists.resize(0); // The ImDrawList are NOT owned by ImDrawData but e.g. by ImGuiContext, so we don't clear them. + DisplayPos = DisplaySize = FramebufferScale = ImVec2(0.0f, 0.0f); + OwnerViewport = NULL; +} + +// Important: 'out_list' is generally going to be draw_data->CmdLists, but may be another temporary list +// as long at it is expected that the result will be later merged into draw_data->CmdLists[]. +void ImGui::AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector* out_list, ImDrawList* draw_list) +{ + if (draw_list->CmdBuffer.Size == 0) + return; + if (draw_list->CmdBuffer.Size == 1 && draw_list->CmdBuffer[0].ElemCount == 0 && draw_list->CmdBuffer[0].UserCallback == NULL) + return; + + // Draw list sanity check. Detect mismatch between PrimReserve() calls and incrementing _VtxCurrentIdx, _VtxWritePtr etc. + // May trigger for you if you are using PrimXXX functions incorrectly. + IM_ASSERT(draw_list->VtxBuffer.Size == 0 || draw_list->_VtxWritePtr == draw_list->VtxBuffer.Data + draw_list->VtxBuffer.Size); + IM_ASSERT(draw_list->IdxBuffer.Size == 0 || draw_list->_IdxWritePtr == draw_list->IdxBuffer.Data + draw_list->IdxBuffer.Size); + if (!(draw_list->Flags & ImDrawListFlags_AllowVtxOffset)) + IM_ASSERT((int)draw_list->_VtxCurrentIdx == draw_list->VtxBuffer.Size); + + // Check that draw_list doesn't use more vertices than indexable (default ImDrawIdx = unsigned short = 2 bytes = 64K vertices per ImDrawList = per window) + // If this assert triggers because you are drawing lots of stuff manually: + // - First, make sure you are coarse clipping yourself and not trying to draw many things outside visible bounds. + // Be mindful that the lower-level ImDrawList API doesn't filter vertices. Use the Metrics/Debugger window to inspect draw list contents. + // - If you want large meshes with more than 64K vertices, you can either: + // (A) Handle the ImDrawCmd::VtxOffset value in your renderer backend, and set 'io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset'. + // Most example backends already support this from 1.71. Pre-1.71 backends won't. + // Some graphics API such as GL ES 1/2 don't have a way to offset the starting vertex so it is not supported for them. + // (B) Or handle 32-bit indices in your renderer backend, and uncomment '#define ImDrawIdx unsigned int' line in imconfig.h. + // Most example backends already support this. For example, the OpenGL example code detect index size at compile-time: + // glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset); + // Your own engine or render API may use different parameters or function calls to specify index sizes. + // 2 and 4 bytes indices are generally supported by most graphics API. + // - If for some reason neither of those solutions works for you, a workaround is to call BeginChild()/EndChild() before reaching + // the 64K limit to split your draw commands in multiple draw lists. + if (sizeof(ImDrawIdx) == 2) + IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above"); + + // Add to output list + records state in ImDrawData + out_list->push_back(draw_list); + draw_data->CmdListsCount++; + draw_data->TotalVtxCount += draw_list->VtxBuffer.Size; + draw_data->TotalIdxCount += draw_list->IdxBuffer.Size; +} + +void ImDrawData::AddDrawList(ImDrawList* draw_list) +{ + IM_ASSERT(CmdLists.Size == CmdListsCount); + draw_list->_PopUnusedDrawCmd(); + ImGui::AddDrawListToDrawDataEx(this, &CmdLists, draw_list); +} + // For backward compatibility: convert all buffers from indexed to de-indexed, in case you cannot render indexed. Note: this is slow and most likely a waste of resources. Always prefer indexed rendering! void ImDrawData::DeIndexAllBuffers() { @@ -1832,15 +1931,9 @@ void ImDrawData::DeIndexAllBuffers() // or if there is a difference between your window resolution and framebuffer resolution. void ImDrawData::ScaleClipRects(const ImVec2& fb_scale) { - for (int i = 0; i < CmdListsCount; i++) - { - ImDrawList* cmd_list = CmdLists[i]; - for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) - { - ImDrawCmd* cmd = &cmd_list->CmdBuffer[cmd_i]; - cmd->ClipRect = ImVec4(cmd->ClipRect.x * fb_scale.x, cmd->ClipRect.y * fb_scale.y, cmd->ClipRect.z * fb_scale.x, cmd->ClipRect.w * fb_scale.y); - } - } + for (ImDrawList* draw_list : CmdLists) + for (ImDrawCmd& cmd : draw_list->CmdBuffer) + cmd.ClipRect = ImVec4(cmd.ClipRect.x * fb_scale.x, cmd.ClipRect.y * fb_scale.y, cmd.ClipRect.z * fb_scale.x, cmd.ClipRect.w * fb_scale.y); } //----------------------------------------------------------------------------- @@ -1896,6 +1989,14 @@ void ImGui::ShadeVertsLinearUV(ImDrawList* draw_list, int vert_start_idx, int ve } } +void ImGui::ShadeVertsTransformPos(ImDrawList* draw_list, int vert_start_idx, int vert_end_idx, const ImVec2& pivot_in, float cos_a, float sin_a, const ImVec2& pivot_out) +{ + ImDrawVert* vert_start = draw_list->VtxBuffer.Data + vert_start_idx; + ImDrawVert* vert_end = draw_list->VtxBuffer.Data + vert_end_idx; + for (ImDrawVert* vertex = vert_start; vertex < vert_end; ++vertex) + vertex->pos = ImRotate(vertex->pos- pivot_in, cos_a, sin_a) + pivot_out; +} + //----------------------------------------------------------------------------- // [SECTION] ImFontConfig //----------------------------------------------------------------------------- @@ -1904,10 +2005,11 @@ ImFontConfig::ImFontConfig() { memset(this, 0, sizeof(*this)); FontDataOwnedByAtlas = true; - OversampleH = 3; // FIXME: 2 may be a better default? + OversampleH = 2; OversampleV = 1; GlyphMaxAdvanceX = FLT_MAX; RasterizerMultiply = 1.0f; + RasterizerDensity = 1.0f; EllipsisChar = (ImWchar)-1; } @@ -1981,19 +2083,19 @@ ImFontAtlas::~ImFontAtlas() void ImFontAtlas::ClearInputData() { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); - for (int i = 0; i < ConfigData.Size; i++) - if (ConfigData[i].FontData && ConfigData[i].FontDataOwnedByAtlas) + for (ImFontConfig& font_cfg : ConfigData) + if (font_cfg.FontData && font_cfg.FontDataOwnedByAtlas) { - IM_FREE(ConfigData[i].FontData); - ConfigData[i].FontData = NULL; + IM_FREE(font_cfg.FontData); + font_cfg.FontData = NULL; } // When clearing this we lose access to the font name and other information used to build the font. - for (int i = 0; i < Fonts.Size; i++) - if (Fonts[i]->ConfigData >= ConfigData.Data && Fonts[i]->ConfigData < ConfigData.Data + ConfigData.Size) + for (ImFont* font : Fonts) + if (font->ConfigData >= ConfigData.Data && font->ConfigData < ConfigData.Data + ConfigData.Size) { - Fonts[i]->ConfigData = NULL; - Fonts[i]->ConfigDataCount = 0; + font->ConfigData = NULL; + font->ConfigDataCount = 0; } ConfigData.clear(); CustomRects.clear(); @@ -2090,6 +2192,8 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) if (new_font_cfg.DstFont->EllipsisChar == (ImWchar)-1) new_font_cfg.DstFont->EllipsisChar = font_cfg->EllipsisChar; + ImFontAtlasUpdateConfigDataPointers(this); + // Invalidate texture TexReady = false; ClearTexData(); @@ -2126,7 +2230,7 @@ ImFont* ImFontAtlas::AddFontDefault(const ImFontConfig* font_cfg_template) if (font_cfg.Name[0] == '\0') ImFormatString(font_cfg.Name, IM_ARRAYSIZE(font_cfg.Name), "ProggyClean.ttf, %dpx", (int)font_cfg.SizePixels); font_cfg.EllipsisChar = (ImWchar)0x0085; - font_cfg.GlyphOffset.y = 1.0f * IM_FLOOR(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units + font_cfg.GlyphOffset.y = 1.0f * IM_TRUNC(font_cfg.SizePixels / 13.0f); // Add +1 offset per 13 units const char* ttf_compressed_base85 = GetDefaultCompressedFontDataTTFBase85(); const ImWchar* glyph_ranges = font_cfg.GlyphRanges != NULL ? font_cfg.GlyphRanges : GetGlyphRangesDefault(); @@ -2156,13 +2260,14 @@ ImFont* ImFontAtlas::AddFontFromFileTTF(const char* filename, float size_pixels, } // NB: Transfer ownership of 'ttf_data' to ImFontAtlas, unless font_cfg_template->FontDataOwnedByAtlas == false. Owned TTF buffer will be deleted after Build(). -ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* ttf_data, int ttf_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) +ImFont* ImFontAtlas::AddFontFromMemoryTTF(void* font_data, int font_data_size, float size_pixels, const ImFontConfig* font_cfg_template, const ImWchar* glyph_ranges) { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); ImFontConfig font_cfg = font_cfg_template ? *font_cfg_template : ImFontConfig(); IM_ASSERT(font_cfg.FontData == NULL); - font_cfg.FontData = ttf_data; - font_cfg.FontDataSize = ttf_size; + IM_ASSERT(font_data_size > 100 && "Incorrect value for font_data_size!"); // Heuristic to prevent accidentally passing a wrong value to font_data_size. + font_cfg.FontData = font_data; + font_cfg.FontDataSize = font_data_size; font_cfg.SizePixels = size_pixels > 0.0f ? size_pixels : font_cfg.SizePixels; if (glyph_ranges) font_cfg.GlyphRanges = glyph_ranges; @@ -2377,7 +2482,10 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) const int font_offset = stbtt_GetFontOffsetForIndex((unsigned char*)cfg.FontData, cfg.FontNo); IM_ASSERT(font_offset >= 0 && "FontData is incorrect, or FontNo cannot be found."); if (!stbtt_InitFont(&src_tmp.FontInfo, (unsigned char*)cfg.FontData, font_offset)) + { + IM_ASSERT(0 && "stbtt_InitFont(): failed to parse FontData. It is correct and complete? Check FontDataSize."); return false; + } // Measure highest codepoints ImFontBuildDstData& dst_tmp = dst_tmp_array[src_tmp.DstIndex]; @@ -2459,7 +2567,7 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) // Convert our ranges in the format stb_truetype wants ImFontConfig& cfg = atlas->ConfigData[src_i]; - src_tmp.PackRange.font_size = cfg.SizePixels; + src_tmp.PackRange.font_size = cfg.SizePixels * cfg.RasterizerDensity; src_tmp.PackRange.first_unicode_codepoint_in_range = 0; src_tmp.PackRange.array_of_unicode_codepoints = src_tmp.GlyphsList.Data; src_tmp.PackRange.num_chars = src_tmp.GlyphsList.Size; @@ -2468,7 +2576,7 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) src_tmp.PackRange.v_oversample = (unsigned char)cfg.OversampleV; // Gather the sizes of all rectangles we will need to pack (this loop is based on stbtt_PackFontRangesGatherRects) - const float scale = (cfg.SizePixels > 0) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -cfg.SizePixels); + const float scale = (cfg.SizePixels > 0.0f) ? stbtt_ScaleForPixelHeight(&src_tmp.FontInfo, cfg.SizePixels * cfg.RasterizerDensity) : stbtt_ScaleForMappingEmToPixels(&src_tmp.FontInfo, -cfg.SizePixels * cfg.RasterizerDensity); const int padding = atlas->TexGlyphPadding; for (int glyph_i = 0; glyph_i < src_tmp.GlyphsList.Size; glyph_i++) { @@ -2564,12 +2672,14 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) int unscaled_ascent, unscaled_descent, unscaled_line_gap; stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap); - const float ascent = ImFloor(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1)); - const float descent = ImFloor(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); + const float ascent = ImTrunc(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1)); + const float descent = ImTrunc(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent); const float font_off_x = cfg.GlyphOffset.x; const float font_off_y = cfg.GlyphOffset.y + IM_ROUND(dst_font->Ascent); + const float inv_rasterization_scale = 1.0f / cfg.RasterizerDensity; + for (int glyph_i = 0; glyph_i < src_tmp.GlyphsCount; glyph_i++) { // Register glyph @@ -2578,7 +2688,11 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) stbtt_aligned_quad q; float unused_x = 0.0f, unused_y = 0.0f; stbtt_GetPackedQuad(src_tmp.PackedChars, atlas->TexWidth, atlas->TexHeight, glyph_i, &unused_x, &unused_y, &q, 0); - dst_font->AddGlyph(&cfg, (ImWchar)codepoint, q.x0 + font_off_x, q.y0 + font_off_y, q.x1 + font_off_x, q.y1 + font_off_y, q.s0, q.t0, q.s1, q.t1, pc.xadvance); + float x0 = q.x0 * inv_rasterization_scale + font_off_x; + float y0 = q.y0 * inv_rasterization_scale + font_off_y; + float x1 = q.x1 * inv_rasterization_scale + font_off_x; + float y1 = q.y1 * inv_rasterization_scale + font_off_y; + dst_font->AddGlyph(&cfg, (ImWchar)codepoint, x0, y0, x1, y1, q.s0, q.t0, q.s1, q.t1, pc.xadvance * inv_rasterization_scale); } } @@ -2598,19 +2712,31 @@ const ImFontBuilderIO* ImFontAtlasGetBuilderForStbTruetype() #endif // IMGUI_ENABLE_STB_TRUETYPE +void ImFontAtlasUpdateConfigDataPointers(ImFontAtlas* atlas) +{ + for (ImFontConfig& font_cfg : atlas->ConfigData) + { + ImFont* font = font_cfg.DstFont; + if (!font_cfg.MergeMode) + { + font->ConfigData = &font_cfg; + font->ConfigDataCount = 0; + } + font->ConfigDataCount++; + } +} + void ImFontAtlasBuildSetupFont(ImFontAtlas* atlas, ImFont* font, ImFontConfig* font_config, float ascent, float descent) { if (!font_config->MergeMode) { font->ClearOutputData(); font->FontSize = font_config->SizePixels; - font->ConfigData = font_config; - font->ConfigDataCount = 0; + IM_ASSERT(font->ConfigData == font_config); font->ContainerAtlas = atlas; font->Ascent = ascent; font->Descent = descent; } - font->ConfigDataCount++; } void ImFontAtlasBuildPackCustomRects(ImFontAtlas* atlas, void* stbrp_context_opaque) @@ -2757,6 +2883,13 @@ static void ImFontAtlasBuildRenderLinesTexData(ImFontAtlas* atlas) // Note: this is called / shared by both the stb_truetype and the FreeType builder void ImFontAtlasBuildInit(ImFontAtlas* atlas) { + // Round font size + // - We started rounding in 1.90 WIP (18991) as our layout system currently doesn't support non-rounded font size well yet. + // - Note that using io.FontGlobalScale or SetWindowFontScale(), with are legacy-ish, partially supported features, can still lead to unrounded sizes. + // - We may support it better later and remove this rounding. + for (ImFontConfig& cfg : atlas->ConfigData) + cfg.SizePixels = ImTrunc(cfg.SizePixels); + // Register texture region for mouse cursors or standard white pixels if (atlas->PackIdMouseCursors < 0) { @@ -2798,9 +2931,9 @@ void ImFontAtlasBuildFinish(ImFontAtlas* atlas) } // Build all fonts lookup tables - for (int i = 0; i < atlas->Fonts.Size; i++) - if (atlas->Fonts[i]->DirtyLookupTables) - atlas->Fonts[i]->BuildLookupTable(); + for (ImFont* font : atlas->Fonts) + if (font->DirtyLookupTables) + font->BuildLookupTable(); atlas->TexReady = true; } @@ -3165,6 +3298,7 @@ void ImFont::BuildLookupTable() max_codepoint = ImMax(max_codepoint, (int)Glyphs[i].Codepoint); // Build lookup table + IM_ASSERT(Glyphs.Size > 0 && "Font has not loaded glyph!"); IM_ASSERT(Glyphs.Size < 0xFFFF); // -1 is reserved IndexAdvanceX.clear(); IndexLookup.clear(); @@ -3281,7 +3415,7 @@ void ImFont::AddGlyph(const ImFontConfig* cfg, ImWchar codepoint, float x0, floa advance_x = ImClamp(advance_x, cfg->GlyphMinAdvanceX, cfg->GlyphMaxAdvanceX); if (advance_x != advance_x_original) { - float char_off_x = cfg->PixelSnapH ? ImFloor((advance_x - advance_x_original) * 0.5f) : (advance_x - advance_x_original) * 0.5f; + float char_off_x = cfg->PixelSnapH ? ImTrunc((advance_x - advance_x_original) * 0.5f) : (advance_x - advance_x_original) * 0.5f; x0 += char_off_x; x1 += char_off_x; } @@ -3549,8 +3683,8 @@ void ImFont::RenderChar(ImDrawList* draw_list, float size, const ImVec2& pos, Im if (glyph->Colored) col |= ~IM_COL32_A_MASK; float scale = (size >= 0.0f) ? (size / FontSize) : 1.0f; - float x = IM_FLOOR(pos.x); - float y = IM_FLOOR(pos.y); + float x = IM_TRUNC(pos.x); + float y = IM_TRUNC(pos.y); draw_list->PrimReserve(6, 4); draw_list->PrimRectUV(ImVec2(x + glyph->X0 * scale, y + glyph->Y0 * scale), ImVec2(x + glyph->X1 * scale, y + glyph->Y1 * scale), ImVec2(glyph->U0, glyph->V0), ImVec2(glyph->U1, glyph->V1), col); } @@ -3562,8 +3696,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im text_end = text_begin + strlen(text_begin); // ImGui:: functions generally already provides a valid text_end, so this is merely to handle direct calls. // Align to be pixel perfect - float x = IM_FLOOR(pos.x); - float y = IM_FLOOR(pos.y); + float x = IM_TRUNC(pos.x); + float y = IM_TRUNC(pos.y); if (y > clip_rect.w) return; @@ -4071,8 +4205,8 @@ static unsigned int stb_decompress(unsigned char *output, const unsigned char *i //----------------------------------------------------------------------------- // ProggyClean.ttf // Copyright (c) 2004, 2005 Tristan Grimmer -// MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip) -// Download and more information at http://upperbounds.net +// MIT license (see License.txt in http://www.proggyfonts.net/index.php?menu=download) +// Download and more information at http://www.proggyfonts.net or http://upperboundsinteractive.com/fonts.php //----------------------------------------------------------------------------- // File: 'ProggyClean.ttf' (41208 bytes) // Exported using misc/fonts/binary_to_compressed_c.cpp (with compression + base85 string encoding). diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index 0858cddb..b49c99bb 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -12,11 +12,17 @@ // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. -// Read online: https://github.com/ocornut/imgui/tree/master/docs +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2023-12-19: Emscripten: Added ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to register canvas selector and auto-resize GLFW window. +// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys. +// 2023-07-18: Inputs: Revert ignoring mouse data on GLFW_CURSOR_DISABLED as it can be used differently. User may set ImGuiConfigFLags_NoMouse if desired. (#5625, #6609) // 2023-06-12: Accept glfwGetTime() not returning a monotonically increasing value. This seems to happens on some Windows setup when peripherals disconnect, and is likely to also happen on browser + Emscripten. (#6491) // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen on Windows ONLY, using a custom WndProc hook. (#2702) // 2023-03-16: Inputs: Fixed key modifiers handling on secondary viewports (docking branch). Broken on 2023/01/04. (#6248, #6034) @@ -27,7 +33,7 @@ // 2022-10-18: Perform a dummy glfwGetError() read to cancel missing mouse cursors errors. Using GLFW_VERSION_COMBINED directly. (#5785) // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). -// 2022-09-01: Inputs: Honor GLFW_CURSOR_DISABLED by not setting mouse position. +// 2022-09-01: Inputs: Honor GLFW_CURSOR_DISABLED by not setting mouse position *EDIT* Reverted 2023-07-18. // 2022-04-30: Inputs: Fixed ImGui_ImplGlfw_TranslateUntranslatedKey() for lower case letters on OSX. // 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11. // 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend. @@ -64,6 +70,7 @@ // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. #include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_impl_glfw.h" // Clang warnings with -Weverything @@ -120,6 +127,9 @@ struct ImGui_ImplGlfw_Data ImVec2 LastValidMousePos; bool InstalledCallbacks; bool CallbacksChainForAllWindows; +#ifdef __EMSCRIPTEN__ + const char* CanvasSelector; +#endif // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. GLFWwindowfocusfun PrevUserCallbackWindowFocus; @@ -269,6 +279,18 @@ static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) case GLFW_KEY_F10: return ImGuiKey_F10; case GLFW_KEY_F11: return ImGuiKey_F11; case GLFW_KEY_F12: return ImGuiKey_F12; + case GLFW_KEY_F13: return ImGuiKey_F13; + case GLFW_KEY_F14: return ImGuiKey_F14; + case GLFW_KEY_F15: return ImGuiKey_F15; + case GLFW_KEY_F16: return ImGuiKey_F16; + case GLFW_KEY_F17: return ImGuiKey_F17; + case GLFW_KEY_F18: return ImGuiKey_F18; + case GLFW_KEY_F19: return ImGuiKey_F19; + case GLFW_KEY_F20: return ImGuiKey_F20; + case GLFW_KEY_F21: return ImGuiKey_F21; + case GLFW_KEY_F22: return ImGuiKey_F22; + case GLFW_KEY_F23: return ImGuiKey_F23; + case GLFW_KEY_F24: return ImGuiKey_F24; default: return ImGuiKey_None; } } @@ -385,8 +407,6 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackCursorPos(window, x, y); - if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) - return; ImGuiIO& io = ImGui::GetIO(); io.AddMousePosEvent((float)x, (float)y); @@ -400,8 +420,6 @@ void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); if (bd->PrevUserCallbackCursorEnter != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackCursorEnter(window, entered); - if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) - return; ImGuiIO& io = ImGui::GetIO(); if (entered) @@ -475,7 +493,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo()); break; } - return ::CallWindowProc(bd->GlfwWndProc, hWnd, msg, wParam, lParam); + return ::CallWindowProcW(bd->GlfwWndProc, hWnd, msg, wParam, lParam); } #endif @@ -599,9 +617,9 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // Windows: register a WndProc hook so we can intercept some messages. #ifdef _WIN32 - bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); + bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); IM_ASSERT(bd->GlfwWndProc != nullptr); - ::SetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); + ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); #endif bd->ClientApi = client_api; @@ -631,6 +649,9 @@ void ImGui_ImplGlfw_Shutdown() if (bd->InstalledCallbacks) ImGui_ImplGlfw_RestoreCallbacks(bd->Window); +#ifdef __EMSCRIPTEN__ + emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, nullptr); +#endif for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) glfwDestroyCursor(bd->MouseCursors[cursor_n]); @@ -638,7 +659,7 @@ void ImGui_ImplGlfw_Shutdown() // Windows: register a WndProc hook so we can intercept some messages. #ifdef _WIN32 ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ::SetWindowLongPtr((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->GlfwWndProc); + ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->GlfwWndProc); bd->GlfwWndProc = nullptr; #endif @@ -653,11 +674,6 @@ static void ImGui_ImplGlfw_UpdateMouseData() ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); ImGuiIO& io = ImGui::GetIO(); - if (glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) - { - io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); - return; - } // (those braces are here to reduce diff with multi-viewports support in 'docking' branch) { @@ -795,6 +811,46 @@ void ImGui_ImplGlfw_NewFrame() ImGui_ImplGlfw_UpdateGamepads(); } +#ifdef __EMSCRIPTEN__ +static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data) +{ + ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data; + double canvas_width, canvas_height; + emscripten_get_element_css_size(bd->CanvasSelector, &canvas_width, &canvas_height); + glfwSetWindowSize(bd->Window, (int)canvas_width, (int)canvas_height); + return true; +} + +static EM_BOOL ImGui_ImplEmscripten_FullscreenChangeCallback(int event_type, const EmscriptenFullscreenChangeEvent* event, void* user_data) +{ + ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data; + double canvas_width, canvas_height; + emscripten_get_element_css_size(bd->CanvasSelector, &canvas_width, &canvas_height); + glfwSetWindowSize(bd->Window, (int)canvas_width, (int)canvas_height); + return true; +} + +// 'canvas_selector' is a CSS selector. The event listener is applied to the first element that matches the query. +// STRING MUST PERSIST FOR THE APPLICATION DURATION. PLEASE USE A STRING LITERAL OR ENSURE POINTER WILL STAY VALID. +void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) +{ + IM_ASSERT(canvas_selector != nullptr); + ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); + IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); + + bd->CanvasSelector = canvas_selector; + emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, bd, false, ImGui_ImplGlfw_OnCanvasSizeChange); + emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, bd, false, ImGui_ImplEmscripten_FullscreenChangeCallback); + + // Change the size of the GLFW window according to the size of the canvas + ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd); +} +#endif + +//----------------------------------------------------------------------------- + #if defined(__clang__) #pragma clang diagnostic pop #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index f3184e75..a36a7ac2 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -14,11 +14,17 @@ // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. -// Read online: https://github.com/ocornut/imgui/tree/master/docs +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-01-09: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" and variants, fixing regression on distros missing a symlink. +// 2023-11-08: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" instead of "libGL.so.1", accommodating for NetBSD systems having only "libGL.so.3" available. (#6983) +// 2023-10-05: OpenGL: Rename symbols in our internal loader so that LTO compilation with another copy of gl3w is possible. (#6875, #6668, #4445) // 2023-06-20: OpenGL: Fixed erroneous use glGetIntegerv(GL_CONTEXT_PROFILE_MASK) on contexts lower than 3.2. (#6539, #6333) // 2023-05-09: OpenGL: Support for glBindSampler() backup/restore on ES3. (#6375) // 2023-04-18: OpenGL: Restore front and back polygon mode separately when supported by context. (#6333) @@ -104,13 +110,10 @@ #endif #include "imgui.h" +#ifndef IMGUI_DISABLE #include "imgui_impl_opengl3.h" #include -#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier -#include // intptr_t -#else #include // intptr_t -#endif #if defined(__APPLE__) #include #endif @@ -460,9 +463,9 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxPos)); GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxUV)); GL_CALL(glEnableVertexAttribArray(bd->AttribLocationVtxColor)); - GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, pos))); - GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, uv))); - GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)IM_OFFSETOF(ImDrawVert, col))); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxPos, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, pos))); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, uv))); + GL_CALL(glVertexAttribPointer(bd->AttribLocationVtxColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)offsetof(ImDrawVert, col))); } // OpenGL3 Render function. @@ -933,9 +936,13 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects() ImGui_ImplOpenGL3_DestroyFontsTexture(); } +//----------------------------------------------------------------------------- + #if defined(__GNUC__) #pragma GCC diagnostic pop #endif #if defined(__clang__) #pragma clang diagnostic pop #endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index d3967a5f..eab542d8 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (tables and columns code) /* @@ -48,7 +48,8 @@ Index of this file: // - TableUpdateLayout() [Internal] followup to BeginTable(): setup everything: widths, columns positions, clipping rectangles. Automatically called by the FIRST call to TableNextRow() or TableHeadersRow(). // | TableSetupDrawChannels() - setup ImDrawList channels // | TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission -// | TableDrawContextMenu() - draw right-click context menu +// | TableBeginContextMenuPopup() +// | - TableDrawDefaultContextMenu() - draw right-click context menu contents //----------------------------------------------------------------------------- // - TableHeadersRow() or TableHeader() user submit a headers row (optional) // | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction @@ -82,7 +83,7 @@ Index of this file: // Y with ScrollX/ScrollY disabled: we output table directly in current window // - outer_size.y < 0.0f -> Bottom-align (but will auto extend, unless _NoHostExtendY is set). Not meaningful if parent window can vertically scroll. // - outer_size.y = 0.0f -> No minimum height (but will auto extend, unless _NoHostExtendY is set) -// - outer_size.y > 0.0f -> Set Minimum height (but will auto extend, unless _NoHostExtenY is set) +// - outer_size.y > 0.0f -> Set Minimum height (but will auto extend, unless _NoHostExtendY is set) // Y with ScrollX/ScrollY enabled: using a child window for scrolling // - outer_size.y < 0.0f -> Bottom-align. Not meaningful if parent window can vertically scroll. // - outer_size.y = 0.0f -> Bottom-align, consistent with BeginChild(). Not recommended unless table is last item in parent window. @@ -198,11 +199,7 @@ Index of this file: #include "imgui_internal.h" // System includes -#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier -#include // intptr_t -#else #include // intptr_t -#endif // Visual Studio warnings #ifdef _MSC_VER @@ -323,14 +320,19 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping criteria may evolve. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0; const ImVec2 avail_size = GetContentRegionAvail(); - ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); - ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size); - if (use_child_window && IsClippedEx(outer_rect, 0)) + const ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); + const ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size); + const bool outer_window_is_measuring_size = (outer_window->AutoFitFramesX > 0) || (outer_window->AutoFitFramesY > 0); // Doesn't apply to AlwaysAutoResize windows! + if (use_child_window && IsClippedEx(outer_rect, 0) && !outer_window_is_measuring_size) { ItemSize(outer_rect); return false; } + // [DEBUG] Debug break requested by user + if (g.DebugBreakInTable == id) + IM_DEBUG_BREAK(); + // Acquire storage for the table ImGuiTable* table = g.Tables.GetOrAddByKey(id); const ImGuiTableFlags table_last_flags = table->Flags; @@ -349,7 +351,8 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG flags = TableFixFlags(flags, outer_window); // Initialize - const int instance_no = (table->LastFrameActive != g.FrameCount) ? 0 : table->InstanceCurrent + 1; + const int previous_frame_active = table->LastFrameActive; + const int instance_no = (previous_frame_active != g.FrameCount) ? 0 : table->InstanceCurrent + 1; table->ID = id; table->Flags = flags; table->LastFrameActive = g.FrameCount; @@ -408,13 +411,17 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->InnerRect = table->InnerWindow->InnerRect; IM_ASSERT(table->InnerWindow->WindowPadding.x == 0.0f && table->InnerWindow->WindowPadding.y == 0.0f && table->InnerWindow->WindowBorderSize == 0.0f); + // Allow submitting when host is measuring + if (table->InnerWindow->SkipItems && outer_window_is_measuring_size) + table->InnerWindow->SkipItems = false; + // When using multiple instances, ensure they have the same amount of horizontal decorations (aka vertical scrollbar) so stretched columns can be aligned) if (instance_no == 0) { table->HasScrollbarYPrev = table->HasScrollbarYCurr; table->HasScrollbarYCurr = false; } - table->HasScrollbarYCurr |= (table->InnerWindow->ScrollMax.y > 0.0f); + table->HasScrollbarYCurr |= table->InnerWindow->ScrollbarY; } else { @@ -443,6 +450,18 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG temp_data->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size; inner_window->DC.PrevLineSize = inner_window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + // Make left and top borders not overlap our contents by offsetting HostClipRect (#6765) + // (we normally shouldn't alter HostClipRect as we rely on TableMergeDrawChannels() expanding non-clipped column toward the + // limits of that rectangle, in order for ImDrawListSplitter::Merge() to merge the draw commands. However since the overlap + // problem only affect scrolling tables in this case we can get away with doing it without extra cost). + if (inner_window != outer_window) + { + if (flags & ImGuiTableFlags_BordersOuterV) + table->HostClipRect.Min.x = ImMin(table->HostClipRect.Min.x + TABLE_BORDER_SIZE, table->HostClipRect.Max.x); + if (flags & ImGuiTableFlags_BordersOuterH) + table->HostClipRect.Min.y = ImMin(table->HostClipRect.Min.y + TABLE_BORDER_SIZE, table->HostClipRect.Max.y); + } + // Padding and Spacing // - None ........Content..... Pad .....Content........ // - PadOuter | Pad ..Content..... Pad .....Content.. Pad | @@ -456,7 +475,6 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->CellSpacingX1 = inner_spacing_explicit + inner_spacing_for_border; table->CellSpacingX2 = inner_spacing_explicit; table->CellPaddingX = inner_padding_explicit; - table->CellPaddingY = g.Style.CellPadding.y; const float outer_padding_for_border = (flags & ImGuiTableFlags_BordersOuterV) ? TABLE_BORDER_SIZE : 0.0f; const float outer_padding_explicit = pad_outer_x ? g.Style.CellPadding.x : 0.0f; @@ -473,10 +491,14 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->RowPosY1 = table->RowPosY2 = table->WorkRect.Min.y; // This is needed somehow table->RowTextBaseline = 0.0f; // This will be cleared again by TableBeginRow() + table->RowCellPaddingY = 0.0f; table->FreezeRowsRequest = table->FreezeRowsCount = 0; // This will be setup by TableSetupScrollFreeze(), if any table->FreezeColumnsRequest = table->FreezeColumnsCount = 0; table->IsUnfrozenRows = true; - table->DeclColumnsCount = 0; + table->DeclColumnsCount = table->AngledHeadersCount = 0; + if (previous_frame_active + 1 < g.FrameCount) + table->IsActiveIdInTable = false; + temp_data->AngledheadersExtraWidth = 0.0f; // Using opaque colors facilitate overlapping lines of the grid, otherwise we'd need to improve TableDrawBorders() table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong); @@ -850,8 +872,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->RightMostEnabledColumn = (ImGuiTableColumnIdx)prev_visible_column_idx; IM_ASSERT(table->LeftMostEnabledColumn >= 0 && table->RightMostEnabledColumn >= 0); - // [Part 2] Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible - // to avoid the column fitting having to wait until the first visible frame of the child container (may or not be a good thing). + // [Part 2] Disable child window clipping while fitting columns. This is not strictly necessary but makes it possible to avoid + // the column fitting having to wait until the first visible frame of the child container (may or not be a good thing). Also see #6510. // FIXME-TABLE: for always auto-resizing columns may not want to do that all the time. if (has_auto_fit_request && table->OuterWindow != table->InnerWindow) table->InnerWindow->SkipItems = false; @@ -936,7 +958,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (column->Flags & ImGuiTableColumnFlags_WidthStretch) { float weight_ratio = column->StretchWeight / stretch_sum_weights; - column->WidthRequest = IM_FLOOR(ImMax(width_avail_for_stretched_columns * weight_ratio, table->MinColumnWidth) + 0.01f); + column->WidthRequest = IM_TRUNC(ImMax(width_avail_for_stretched_columns * weight_ratio, table->MinColumnWidth) + 0.01f); width_remaining_for_stretched_columns -= column->WidthRequest; } @@ -946,7 +968,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) column->Flags |= ImGuiTableColumnFlags_NoDirectResize_; // Assign final width, record width in case we will need to shrink - column->WidthGiven = ImFloor(ImMax(column->WidthRequest, table->MinColumnWidth)); + column->WidthGiven = ImTrunc(ImMax(column->WidthRequest, table->MinColumnWidth)); table->ColumnsGivenWidth += column->WidthGiven; } @@ -971,17 +993,25 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // clear ActiveId, which is equivalent to the change provided by _AllowWhenBLockedByActiveItem). // - This allows columns to be marked as hovered when e.g. clicking a button inside the column, or using drag and drop. ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); - table->HoveredColumnBody = -1; - table->HoveredColumnBorder = -1; + table_instance->HoveredRowLast = table_instance->HoveredRowNext; + table_instance->HoveredRowNext = -1; + table->HoveredColumnBody = table->HoveredColumnBorder = -1; const ImRect mouse_hit_rect(table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.Max.x, ImMax(table->OuterRect.Max.y, table->OuterRect.Min.y + table_instance->LastOuterHeight)); const ImGuiID backup_active_id = g.ActiveId; g.ActiveId = 0; const bool is_hovering_table = ItemHoverable(mouse_hit_rect, 0, ImGuiItemFlags_None); g.ActiveId = backup_active_id; + // Determine skewed MousePos.x to support angled headers. + float mouse_skewed_x = g.IO.MousePos.x; + if (table->AngledHeadersHeight > 0.0f) + if (g.IO.MousePos.y >= table->OuterRect.Min.y && g.IO.MousePos.y <= table->OuterRect.Min.y + table->AngledHeadersHeight) + mouse_skewed_x += ImTrunc((table->OuterRect.Min.y + table->AngledHeadersHeight - g.IO.MousePos.y) * table->AngledHeadersSlope); + // [Part 6] Setup final position, offset, skip/clip states and clipping rectangles, detect hovered column // Process columns in their visible orders as we are comparing the visible order and adjusting host_clip_rect while looping. int visible_n = 0; + bool has_at_least_one_column_requesting_output = false; bool offset_x_frozen = (table->FreezeColumnsCount > 0); float offset_x = ((table->FreezeColumnsCount > 0) ? table->OuterRect.Min.x : work_rect.Min.x) + table->OuterPaddingX - table->CellSpacingX1; ImRect host_clip_rect = table->InnerClipRect; @@ -1019,7 +1049,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } // Detect hovered column - if (is_hovering_table && g.IO.MousePos.x >= column->ClipRect.Min.x && g.IO.MousePos.x < column->ClipRect.Max.x) + if (is_hovering_table && mouse_skewed_x >= column->ClipRect.Min.x && mouse_skewed_x < column->ClipRect.Max.x) table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n; // Lock start position @@ -1038,7 +1068,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // - FIXME-TABLE: We want equal width columns to have equal (ClipRect.Max.x - WorkMinX) width, which means ClipRect.max.x cannot stray off host_clip_rect.Max.x else right-most column may appear shorter. column->WorkMinX = column->MinX + table->CellPaddingX + table->CellSpacingX1; column->WorkMaxX = column->MaxX - table->CellPaddingX - table->CellSpacingX2; // Expected max - column->ItemWidth = ImFloor(column->WidthGiven * 0.65f); + column->ItemWidth = ImTrunc(column->WidthGiven * 0.65f); column->ClipRect.Min.x = column->MinX; column->ClipRect.Min.y = work_rect.Min.y; column->ClipRect.Max.x = column->MaxX; //column->WorkMaxX; @@ -1062,9 +1092,12 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) column->IsRequestOutput = is_visible || column->AutoFitQueue != 0 || column->CannotSkipItemsQueue != 0; // Mark column as SkipItems (ignoring all items/layout) + // (table->HostSkipItems is a copy of inner_window->SkipItems before we cleared it above in Part 2) column->IsSkipItems = !column->IsEnabled || table->HostSkipItems; if (column->IsSkipItems) IM_ASSERT(!is_visible); + if (column->IsRequestOutput && !column->IsSkipItems) + has_at_least_one_column_requesting_output = true; // Update status flags column->Flags |= ImGuiTableColumnFlags_IsEnabled; @@ -1102,18 +1135,26 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) visible_n++; } + // In case the table is visible (e.g. decorations) but all columns clipped, we keep a column visible. + // Else if give no chance to a clipper-savy user to submit rows and therefore total contents height used by scrollbar. + if (has_at_least_one_column_requesting_output == false) + { + table->Columns[table->LeftMostEnabledColumn].IsRequestOutput = true; + table->Columns[table->LeftMostEnabledColumn].IsSkipItems = false; + } + // [Part 7] Detect/store when we are hovering the unused space after the right-most column (so e.g. context menus can react on it) // Clear Resizable flag if none of our column are actually resizable (either via an explicit _NoResize flag, either // because of using _WidthAuto/_WidthStretch). This will hide the resizing option from the context menu. const float unused_x1 = ImMax(table->WorkRect.Min.x, table->Columns[table->RightMostEnabledColumn].ClipRect.Max.x); if (is_hovering_table && table->HoveredColumnBody == -1) - { - if (g.IO.MousePos.x >= unused_x1) + if (mouse_skewed_x >= unused_x1) table->HoveredColumnBody = (ImGuiTableColumnIdx)table->ColumnsCount; - } if (has_resizable == false && (table->Flags & ImGuiTableFlags_Resizable)) table->Flags &= ~ImGuiTableFlags_Resizable; + table->IsActiveIdAliveBeforeTable = (g.ActiveIdIsAlive != 0); + // [Part 8] Lock actual OuterRect/WorkRect right-most position. // This is done late to handle the case of fixed-columns tables not claiming more widths that they need. // Because of this we are careful with uses of WorkRect and InnerClipRect before this point. @@ -1125,8 +1166,16 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->InnerClipRect.Max.x = ImMin(table->InnerClipRect.Max.x, unused_x1); } table->InnerWindow->ParentWorkRect = table->WorkRect; - table->BorderX1 = table->InnerClipRect.Min.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : -1.0f); - table->BorderX2 = table->InnerClipRect.Max.x;// +((table->Flags & ImGuiTableFlags_BordersOuter) ? 0.0f : +1.0f); + table->BorderX1 = table->InnerClipRect.Min.x; + table->BorderX2 = table->InnerClipRect.Max.x; + + // Setup window's WorkRect.Max.y for GetContentRegionAvail(). Other values will be updated in each TableBeginCell() call. + float window_content_max_y; + if (table->Flags & ImGuiTableFlags_NoHostExtendY) + window_content_max_y = table->OuterRect.Max.y; + else + window_content_max_y = ImMax(table->InnerWindow->ContentRegionRect.Max.y, (table->Flags & ImGuiTableFlags_ScrollY) ? 0.0f : table->OuterRect.Max.y); + table->InnerWindow->WorkRect.Max.y = ImClamp(window_content_max_y - g.Style.CellPadding.y, table->InnerWindow->WorkRect.Min.y, table->InnerWindow->WorkRect.Max.y); // [Part 9] Allocate draw channels and setup background cliprect TableSetupDrawChannels(table); @@ -1134,14 +1183,26 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // [Part 10] Hit testing on borders if (table->Flags & ImGuiTableFlags_Resizable) TableUpdateBorders(table); - table_instance->LastFirstRowHeight = 0.0f; + table_instance->LastTopHeadersRowHeight = 0.0f; table->IsLayoutLocked = true; table->IsUsingHeaders = false; - // [Part 11] Context menu - if (TableBeginContextMenuPopup(table)) + // Highlight header + table->HighlightColumnHeader = -1; + if (table->IsContextPopupOpen && table->ContextPopupColumn != -1 && table->InstanceInteracted == table->InstanceCurrent) + table->HighlightColumnHeader = table->ContextPopupColumn; + else if ((table->Flags & ImGuiTableFlags_HighlightHoveredColumn) && table->HoveredColumnBody != -1 && table->HoveredColumnBody != table->ColumnsCount && table->HoveredColumnBorder == -1) + if (g.ActiveId == 0 || (table->IsActiveIdInTable || g.DragDropActive)) + table->HighlightColumnHeader = table->HoveredColumnBody; + + // [Part 11] Default context menu + // - To append to this menu: you can call TableBeginContextMenuPopup()/.../EndPopup(). + // - To modify or replace this: set table->IsContextPopupNoDefaultContents = true, then call TableBeginContextMenuPopup()/.../EndPopup(). + // - You may call TableDrawDefaultContextMenu() with selected flags to display specific sections of the default menu, + // e.g. TableDrawDefaultContextMenu(table, table->Flags & ~ImGuiTableFlags_Hideable) will display everything EXCEPT columns visibility options. + if (table->DisableDefaultContextMenu == false && TableBeginContextMenuPopup(table)) { - TableDrawContextMenu(table); + TableDrawDefaultContextMenu(table, table->Flags); EndPopup(); } @@ -1178,9 +1239,9 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) // Actual columns highlight/render will be performed in EndTable() and not be affected. ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS; - const float hit_y1 = table->OuterRect.Min.y; + const float hit_y1 = (table->FreezeRowsCount >= 1 ? table->OuterRect.Min.y : table->WorkRect.Min.y) + table->AngledHeadersHeight; const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table_instance->LastOuterHeight); - const float hit_y2_head = hit_y1 + table_instance->LastFirstRowHeight; + const float hit_y2_head = hit_y1 + table_instance->LastTopHeadersRowHeight; for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { @@ -1211,7 +1272,7 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) { TableSetColumnWidthAutoSingle(table, column_n); ClearActiveID(); - held = hovered = false; + held = false; } if (held) { @@ -1283,7 +1344,7 @@ void ImGui::EndTable() max_pos_x = ImMax(max_pos_x, table->Columns[table->RightMostEnabledColumn].WorkMaxX + table->CellPaddingX + table->OuterPaddingX - outer_padding_for_border); if (table->ResizedColumn != -1) max_pos_x = ImMax(max_pos_x, table->ResizeLockMinContentsX2); - table->InnerWindow->DC.CursorMaxPos.x = max_pos_x; + table->InnerWindow->DC.CursorMaxPos.x = max_pos_x + table->TempData->AngledheadersExtraWidth; } // Pop clipping rect @@ -1355,10 +1416,12 @@ void ImGui::EndTable() { ImGuiTableColumn* column = &table->Columns[table->ResizedColumn]; const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + TABLE_RESIZE_SEPARATOR_HALF_THICKNESS); - const float new_width = ImFloor(new_x2 - column->MinX - table->CellSpacingX1 - table->CellPaddingX * 2.0f); + const float new_width = ImTrunc(new_x2 - column->MinX - table->CellSpacingX1 - table->CellPaddingX * 2.0f); table->ResizedColumnNextWidth = new_width; } + table->IsActiveIdInTable = (g.ActiveIdIsAlive != 0 && table->IsActiveIdAliveBeforeTable == false); + // Pop from id stack IM_ASSERT_USER_ERROR(inner_window->IDStack.back() == table_instance->TableInstanceID, "Mismatching PushID/PopID!"); IM_ASSERT_USER_ERROR(outer_window->DC.ItemWidthStack.Size >= temp_data->HostBackupItemWidthStackSize, "Too many PopItemWidth!"); @@ -1399,7 +1462,7 @@ void ImGui::EndTable() } else if (temp_data->UserOuterSize.x <= 0.0f) { - const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f; + const float decoration_size = table->TempData->AngledheadersExtraWidth + ((table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f); outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - temp_data->UserOuterSize.x); outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth)); } @@ -1438,7 +1501,7 @@ void ImGui::EndTable() NavUpdateCurrentWindowIsScrollPushableX(); } -// See "COLUMN SIZING POLICIES" comments at the top of this file +// See "COLUMNS SIZING POLICIES" comments at the top of this file // If (init_width_or_weight <= 0.0f) it is ignored void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, float init_width_or_weight, ImGuiID user_id) { @@ -1466,6 +1529,11 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo if ((flags & ImGuiTableColumnFlags_WidthMask_) == 0 && init_width_or_weight > 0.0f) if ((table->Flags & ImGuiTableFlags_SizingMask_) == ImGuiTableFlags_SizingFixedFit || (table->Flags & ImGuiTableFlags_SizingMask_) == ImGuiTableFlags_SizingFixedSame) flags |= ImGuiTableColumnFlags_WidthFixed; + if (flags & ImGuiTableColumnFlags_AngledHeader) + { + flags |= ImGuiTableColumnFlags_NoHeaderLabel; + table->AngledHeadersCount++; + } TableSetupColumnFlags(table, column, flags); column->UserID = user_id; @@ -1547,6 +1615,7 @@ void ImGui::TableSetupScrollFreeze(int columns, int rows) // - TableGetCellBgRect() [Internal] // - TableGetColumnResizeID() [Internal] // - TableGetHoveredColumn() [Internal] +// - TableGetHoveredRow() [Internal] // - TableSetBgColor() //----------------------------------------------------------------------------- @@ -1651,6 +1720,19 @@ int ImGui::TableGetHoveredColumn() return (int)table->HoveredColumnBody; } +// Return -1 when table is not hovered. Return maxrow+1 if in table but below last submitted row. +// *IMPORTANT* Unlike TableGetHoveredColumn(), this has a one frame latency in updating the value. +// This difference with is the reason why this is not public yet. +int ImGui::TableGetHoveredRow() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + if (!table) + return -1; + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); + return (int)table_instance->HoveredRowLast; +} + void ImGui::TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n) { ImGuiContext& g = *GImGui; @@ -1725,19 +1807,20 @@ void ImGui::TableNextRow(ImGuiTableRowFlags row_flags, float row_min_height) table->LastRowFlags = table->RowFlags; table->RowFlags = row_flags; + table->RowCellPaddingY = g.Style.CellPadding.y; table->RowMinHeight = row_min_height; TableBeginRow(table); // We honor min_row_height requested by user, but cannot guarantee per-row maximum height, // because that would essentially require a unique clipping rectangle per-cell. - table->RowPosY2 += table->CellPaddingY * 2.0f; + table->RowPosY2 += table->RowCellPaddingY * 2.0f; table->RowPosY2 = ImMax(table->RowPosY2, table->RowPosY1 + row_min_height); // Disable output until user calls TableNextColumn() table->InnerWindow->SkipItems = true; } -// [Internal] Called by TableNextRow() +// [Internal] Only called by TableNextRow() void ImGui::TableBeginRow(ImGuiTable* table) { ImGuiWindow* window = table->InnerWindow; @@ -1758,8 +1841,10 @@ void ImGui::TableBeginRow(ImGuiTable* table) table->RowPosY1 = table->RowPosY2 = next_y1; table->RowTextBaseline = 0.0f; table->RowIndentOffsetX = window->DC.Indent.x - table->HostIndentX; // Lock indent + window->DC.PrevLineTextBaseOffset = 0.0f; - window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); + window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x, window->DC.CursorPos.y + table->RowCellPaddingY); // This allows users to call SameLine() to share LineSize between columns. + window->DC.PrevLineSize = window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); // This allows users to call SameLine() to share LineSize between columns, and to call it from first column too. window->DC.IsSameLine = window->DC.IsSetPos = false; window->DC.CursorMaxPos.y = next_y1; @@ -1796,12 +1881,17 @@ void ImGui::TableEndRow(ImGuiTable* table) const float bg_y2 = table->RowPosY2; const bool unfreeze_rows_actual = (table->CurrentRow + 1 == table->FreezeRowsCount); const bool unfreeze_rows_request = (table->CurrentRow + 1 == table->FreezeRowsRequest); - if (table->CurrentRow == 0) - TableGetInstanceData(table, table->InstanceCurrent)->LastFirstRowHeight = bg_y2 - bg_y1; + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); + if ((table->RowFlags & ImGuiTableRowFlags_Headers) && (table->CurrentRow == 0 || (table->LastRowFlags & ImGuiTableRowFlags_Headers))) + table_instance->LastTopHeadersRowHeight += bg_y2 - bg_y1; const bool is_visible = (bg_y2 >= table->InnerClipRect.Min.y && bg_y1 <= table->InnerClipRect.Max.y); if (is_visible) { + // Update data for TableGetHoveredRow() + if (table->HoveredColumnBody != -1 && g.IO.MousePos.y >= bg_y1 && g.IO.MousePos.y < bg_y2) + table_instance->HoveredRowNext = table->CurrentRow; + // Decide of background color for the row ImU32 bg_col0 = 0; ImU32 bg_col1 = 0; @@ -1813,15 +1903,14 @@ void ImGui::TableEndRow(ImGuiTable* table) bg_col1 = table->RowBgColor[1]; // Decide of top border color - ImU32 border_col = 0; + ImU32 top_border_col = 0; const float border_size = TABLE_BORDER_SIZE; - if (table->CurrentRow > 0 || table->InnerWindow == table->OuterWindow) - if (table->Flags & ImGuiTableFlags_BordersInnerH) - border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight; + if (table->CurrentRow > 0 && (table->Flags & ImGuiTableFlags_BordersInnerH)) + top_border_col = (table->LastRowFlags & ImGuiTableRowFlags_Headers) ? table->BorderColorStrong : table->BorderColorLight; const bool draw_cell_bg_color = table->RowCellDataCurrent >= 0; const bool draw_strong_bottom_border = unfreeze_rows_actual; - if ((bg_col0 | bg_col1 | border_col) != 0 || draw_strong_bottom_border || draw_cell_bg_color) + if ((bg_col0 | bg_col1 | top_border_col) != 0 || draw_strong_bottom_border || draw_cell_bg_color) { // In theory we could call SetWindowClipRectBeforeSetChannel() but since we know TableEndRow() is // always followed by a change of clipping rectangle we perform the smallest overwrite possible here. @@ -1860,8 +1949,8 @@ void ImGui::TableEndRow(ImGuiTable* table) } // Draw top border - if (border_col && bg_y1 >= table->BgClipRect.Min.y && bg_y1 < table->BgClipRect.Max.y) - window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y1), ImVec2(table->BorderX2, bg_y1), border_col, border_size); + if (top_border_col && bg_y1 >= table->BgClipRect.Min.y && bg_y1 < table->BgClipRect.Max.y) + window->DrawList->AddLine(ImVec2(table->BorderX1, bg_y1), ImVec2(table->BorderX2, bg_y1), top_border_col, border_size); // Draw bottom border at the row unfreezing mark (always strong) if (draw_strong_bottom_border && bg_y2 >= table->BgClipRect.Min.y && bg_y2 < table->BgClipRect.Max.y) @@ -1879,7 +1968,7 @@ void ImGui::TableEndRow(ImGuiTable* table) IM_ASSERT(table->IsUnfrozenRows == false); const float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); table->IsUnfrozenRows = true; - TableGetInstanceData(table, table->InstanceCurrent)->LastFrozenHeight = y0 - table->OuterRect.Min.y; + table_instance->LastFrozenHeight = y0 - table->OuterRect.Min.y; // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); @@ -1989,12 +2078,14 @@ void ImGui::TableBeginCell(ImGuiTable* table, int column_n) start_x += table->RowIndentOffsetX; // ~~ += window.DC.Indent.x - table->HostIndentX, except we locked it for the row. window->DC.CursorPos.x = start_x; - window->DC.CursorPos.y = table->RowPosY1 + table->CellPaddingY; + window->DC.CursorPos.y = table->RowPosY1 + table->RowCellPaddingY; window->DC.CursorMaxPos.x = window->DC.CursorPos.x; window->DC.ColumnsOffset.x = start_x - window->Pos.x - window->DC.Indent.x; // FIXME-WORKRECT + window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x; // PrevLine.y is preserved. This allows users to call SameLine() to share LineSize between columns. window->DC.CurrLineTextBaseOffset = table->RowTextBaseline; window->DC.NavLayerCurrent = (ImGuiNavLayer)column->NavLayerCurrent; + // Note how WorkRect.Max.y is only set once during layout window->WorkRect.Min.y = window->DC.CursorPos.y; window->WorkRect.Min.x = column->WorkMinX; window->WorkRect.Max.x = column->WorkMaxX; @@ -2045,7 +2136,7 @@ void ImGui::TableEndCell(ImGuiTable* table) p_max_pos_x = table->IsUnfrozenRows ? &column->ContentMaxXUnfrozen : &column->ContentMaxXFrozen; *p_max_pos_x = ImMax(*p_max_pos_x, window->DC.CursorMaxPos.x); if (column->IsEnabled) - table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->CellPaddingY); + table->RowPosY2 = ImMax(table->RowPosY2, window->DC.CursorMaxPos.y + table->RowCellPaddingY); column->ItemWidth = window->DC.ItemWidth; // Propagate text baseline for the entire row @@ -2255,6 +2346,7 @@ void ImGui::TableUpdateColumnsWeightFromWidth(ImGuiTable* table) // - TablePopBackgroundChannel() [Internal] // - TableSetupDrawChannels() [Internal] // - TableMergeDrawChannels() [Internal] +// - TableGetColumnBorderCol() [Internal] // - TableDrawBorders() [Internal] //------------------------------------------------------------------------- @@ -2538,6 +2630,18 @@ void ImGui::TableMergeDrawChannels(ImGuiTable* table) } } +static ImU32 TableGetColumnBorderCol(ImGuiTable* table, int order_n, int column_n) +{ + const bool is_hovered = (table->HoveredColumnBorder == column_n); + const bool is_resized = (table->ResizedColumn == column_n) && (table->InstanceInteracted == table->InstanceCurrent); + const bool is_frozen_separator = (table->FreezeColumnsCount == order_n + 1); + if (is_resized || is_hovered) + return ImGui::GetColorU32(is_resized ? ImGuiCol_SeparatorActive : ImGuiCol_SeparatorHovered); + if (is_frozen_separator || (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize))) + return table->BorderColorStrong; + return table->BorderColorLight; +} + // FIXME-TABLE: This is a mess, need to redesign how we render borders (as some are also done in TableEndRow) void ImGui::TableDrawBorders(ImGuiTable* table) { @@ -2552,9 +2656,9 @@ void ImGui::TableDrawBorders(ImGuiTable* table) // Draw inner border and resizing feedback ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); const float border_size = TABLE_BORDER_SIZE; - const float draw_y1 = table->InnerRect.Min.y; + const float draw_y1 = ImMax(table->InnerRect.Min.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table->AngledHeadersHeight) + ((table->Flags & ImGuiTableFlags_BordersOuterH) ? 1.0f : 0.0f); const float draw_y2_body = table->InnerRect.Max.y; - const float draw_y2_head = table->IsUsingHeaders ? ImMin(table->InnerRect.Max.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table_instance->LastFirstRowHeight) : draw_y1; + const float draw_y2_head = table->IsUsingHeaders ? ImMin(table->InnerRect.Max.y, (table->FreezeRowsCount >= 1 ? table->InnerRect.Min.y : table->WorkRect.Min.y) + table_instance->LastTopHeadersRowHeight) : draw_y1; if (table->Flags & ImGuiTableFlags_BordersInnerV) { for (int order_n = 0; order_n < table->ColumnsCount; order_n++) @@ -2580,21 +2684,9 @@ void ImGui::TableDrawBorders(ImGuiTable* table) // Draw in outer window so right-most column won't be clipped // Always draw full height border when being resized/hovered, or on the delimitation of frozen column scrolling. - ImU32 col; - float draw_y2; - if (is_hovered || is_resized || is_frozen_separator) - { - draw_y2 = draw_y2_body; - col = is_resized ? GetColorU32(ImGuiCol_SeparatorActive) : is_hovered ? GetColorU32(ImGuiCol_SeparatorHovered) : table->BorderColorStrong; - } - else - { - draw_y2 = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? draw_y2_head : draw_y2_body; - col = (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) ? table->BorderColorStrong : table->BorderColorLight; - } - + float draw_y2 = (is_hovered || is_resized || is_frozen_separator || (table->Flags & (ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_NoBordersInBodyUntilResize)) == 0) ? draw_y2_body : draw_y2_head; if (draw_y2 > draw_y1) - inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), col, border_size); + inner_drawlist->AddLine(ImVec2(column->MaxX, draw_y1), ImVec2(column->MaxX, draw_y2), TableGetColumnBorderCol(table, order_n, column_n), border_size); } } @@ -2611,7 +2703,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) const ImU32 outer_col = table->BorderColorStrong; if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter) { - inner_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, 0, border_size); + inner_drawlist->AddRect(outer_border.Min, outer_border.Max + ImVec2(1, 1), outer_col, 0.0f, 0, border_size); } else if (table->Flags & ImGuiTableFlags_BordersOuterV) { @@ -2626,7 +2718,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) } if ((table->Flags & ImGuiTableFlags_BordersInnerH) && table->RowPosY2 < table->OuterRect.Max.y) { - // Draw bottom-most row border + // Draw bottom-most row border between it is above outer border. const float border_y = table->RowPosY2; if (border_y >= table->BgClipRect.Min.y && border_y < table->BgClipRect.Max.y) inner_drawlist->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), table->BorderColorLight, border_size); @@ -2647,8 +2739,9 @@ void ImGui::TableDrawBorders(ImGuiTable* table) //------------------------------------------------------------------------- // Return NULL if no sort specs (most often when ImGuiTableFlags_Sortable is not set) -// You can sort your data again when 'SpecsChanged == true'. It will be true with sorting specs have changed since -// last call, or the first time. +// When 'sort_specs->SpecsDirty == true' you should sort your data. It will be true when sorting specs have +// changed since last call, or the first time. Make sure to set 'SpecsDirty = false' after sorting, +// else you may wastefully sort your data every frame! // Lifetime: don't hold on this pointer over multiple frames or past any subsequent call to BeginTable()! ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() { @@ -2836,6 +2929,8 @@ void ImGui::TableSortSpecsBuild(ImGuiTable* table) // - TableGetHeaderRowHeight() [Internal] // - TableHeadersRow() // - TableHeader() +// - TableAngledHeadersRow() +// - TableAngledHeadersRowEx() [Internal] //------------------------------------------------------------------------- float ImGui::TableGetHeaderRowHeight() @@ -2844,16 +2939,26 @@ float ImGui::TableGetHeaderRowHeight() // Calculate row height, for the unlikely case that some labels may be taller than others. // If we didn't do that, uneven header height would highlight but smaller one before the tallest wouldn't catch input for all height. // In your custom header row you may omit this all together and just call TableNextRow() without a height... - float row_height = GetTextLineHeight(); - int columns_count = TableGetColumnCount(); - for (int column_n = 0; column_n < columns_count; column_n++) - { - ImGuiTableColumnFlags flags = TableGetColumnFlags(column_n); - if ((flags & ImGuiTableColumnFlags_IsEnabled) && !(flags & ImGuiTableColumnFlags_NoHeaderLabel)) - row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(column_n)).y); - } - row_height += GetStyle().CellPadding.y * 2.0f; - return row_height; + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + float row_height = g.FontSize; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if (IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) + if ((table->Columns[column_n].Flags & ImGuiTableColumnFlags_NoHeaderLabel) == 0) + row_height = ImMax(row_height, CalcTextSize(TableGetColumnName(table, column_n)).y); + return row_height + g.Style.CellPadding.y * 2.0f; +} + +float ImGui::TableGetHeaderAngledMaxLabelWidth() +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + float width = 0.0f; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if (IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) + if (table->Columns[column_n].Flags & ImGuiTableColumnFlags_AngledHeader) + width = ImMax(width, CalcTextSize(TableGetColumnName(table, column_n), NULL, true).x); + return width + g.Style.CellPadding.x * 2.0f; } // [Public] This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn(). @@ -2873,9 +2978,9 @@ void ImGui::TableHeadersRow() TableUpdateLayout(table); // Open row - const float row_y1 = GetCursorScreenPos().y; const float row_height = TableGetHeaderRowHeight(); TableNextRow(ImGuiTableRowFlags_Headers, row_height); + const float row_y1 = GetCursorScreenPos().y; if (table->HostSkipItems) // Merely an optimization, you may skip in your own code. return; @@ -2897,7 +3002,7 @@ void ImGui::TableHeadersRow() ImVec2 mouse_pos = ImGui::GetMousePos(); if (IsMouseReleased(1) && TableGetHoveredColumn() == columns_count) if (mouse_pos.y >= row_y1 && mouse_pos.y < row_y1 + row_height) - TableOpenContextMenu(-1); // Will open a non-column-specific popup. + TableOpenContextMenu(columns_count); // Will open a non-column-specific popup. } // Emit a column header (text + optional sort order) @@ -2926,16 +3031,19 @@ void ImGui::TableHeader(const char* label) // If we already got a row height, there's use that. // FIXME-TABLE: Padding problem if the correct outer-padding CellBgRect strays off our ClipRect? ImRect cell_r = TableGetCellBgRect(table, column_n); - float label_height = ImMax(label_size.y, table->RowMinHeight - table->CellPaddingY * 2.0f); + float label_height = ImMax(label_size.y, table->RowMinHeight - table->RowCellPaddingY * 2.0f); // Calculate ideal size for sort order arrow float w_arrow = 0.0f; float w_sort_text = 0.0f; + bool sort_arrow = false; char sort_order_suf[4] = ""; const float ARROW_SCALE = 0.65f; if ((table->Flags & ImGuiTableFlags_Sortable) && !(column->Flags & ImGuiTableColumnFlags_NoSort)) { - w_arrow = ImFloor(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x); + w_arrow = ImTrunc(g.FontSize * ARROW_SCALE + g.Style.FramePadding.x); + if (column->SortOrder != -1) + sort_arrow = true; if (column->SortOrder > 0) { ImFormatString(sort_order_suf, IM_ARRAYSIZE(sort_order_suf), "%d", column->SortOrder + 1); @@ -2943,13 +3051,12 @@ void ImGui::TableHeader(const char* label) } } - // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging. + // We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considered for merging. float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow; - column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, column->WorkMaxX); + column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, sort_arrow ? cell_r.Max.x : ImMin(max_pos_x, cell_r.Max.x)); column->ContentMaxXHeadersIdeal = ImMax(column->ContentMaxXHeadersIdeal, max_pos_x); // Keep header highlighted when context menu is open. - const bool selected = (table->IsContextPopupOpen && table->ContextPopupColumn == column_n && table->InstanceInteracted == table->InstanceCurrent); ImGuiID id = window->GetID(label); ImRect bb(cell_r.Min.x, cell_r.Min.y, cell_r.Max.x, ImMax(cell_r.Max.y, cell_r.Min.y + label_height + g.Style.CellPadding.y * 2.0f)); ItemSize(ImVec2(0.0f, label_height)); // Don't declare unclipped width, it'll be fed ContentMaxPosHeadersIdeal @@ -2960,9 +3067,10 @@ void ImGui::TableHeader(const char* label) //GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 0, 0, 255)); // [DEBUG] // Using AllowOverlap mode because we cover the whole cell, and we want user to be able to submit subsequent items. + const bool highlight = (table->HighlightColumnHeader == column_n); bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_AllowOverlap); - if (held || hovered || selected) + if (held || hovered || highlight) { const ImU32 col = GetColorU32(held ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); //RenderFrame(bb.Min, bb.Max, col, false, 0.0f); @@ -3040,11 +3148,126 @@ void ImGui::TableHeader(const char* label) TableOpenContextMenu(column_n); } +// Unlike TableHeadersRow() it is not expected that you can reimplement or customize this with custom widgets. +// FIXME: highlight without ImGuiTableFlags_HighlightHoveredColumn +// FIXME: No hit-testing/button on the angled header. +void ImGui::TableAngledHeadersRow() +{ + ImGuiContext& g = *GImGui; + TableAngledHeadersRowEx(g.Style.TableAngledHeadersAngle, 0.0f); +} + +void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) +{ + ImGuiContext& g = *GImGui; + ImGuiTable* table = g.CurrentTable; + ImGuiWindow* window = g.CurrentWindow; + ImDrawList* draw_list = window->DrawList; + IM_ASSERT(table != NULL && "Need to call TableHeadersRow() after BeginTable()!"); + IM_ASSERT(table->CurrentRow == -1 && "Must be first row"); + + if (max_label_width == 0.0f) + max_label_width = TableGetHeaderAngledMaxLabelWidth(); + + // Angle argument expressed in (-IM_PI/2 .. +IM_PI/2) as it is easier to think about for user. + const bool flip_label = (angle < 0.0f); + angle -= IM_PI * 0.5f; + const float cos_a = ImCos(angle); + const float sin_a = ImSin(angle); + const float label_cos_a = flip_label ? ImCos(angle + IM_PI) : cos_a; + const float label_sin_a = flip_label ? ImSin(angle + IM_PI) : sin_a; + const ImVec2 unit_right = ImVec2(cos_a, sin_a); + + // Calculate our base metrics and set angled headers data _before_ the first call to TableNextRow() + // FIXME-STYLE: Would it be better for user to submit 'max_label_width' or 'row_height' ? One can be derived from the other. + const float header_height = table->RowCellPaddingY * 2.0f + g.FontSize; + const float row_height = ImFabs(ImRotate(ImVec2(max_label_width, flip_label ? +header_height : -header_height), cos_a, sin_a).y); + const ImVec2 header_angled_vector = unit_right * (row_height / -sin_a); + table->AngledHeadersHeight = row_height; + table->AngledHeadersSlope = (sin_a != 0.0f) ? (cos_a / sin_a) : 0.0f; + + // Declare row, override and draw our own background + TableNextRow(ImGuiTableRowFlags_Headers, row_height); + TableNextColumn(); + table->DrawSplitter->SetCurrentChannel(draw_list, TABLE_DRAW_CHANNEL_BG0); + float clip_rect_min_x = table->BgClipRect.Min.x; + if (table->FreezeColumnsCount > 0) + clip_rect_min_x = ImMax(clip_rect_min_x, table->Columns[table->FreezeColumnsCount - 1].MaxX); + TableSetBgColor(ImGuiTableBgTarget_RowBg0, 0); // Cancel + PushClipRect(table->BgClipRect.Min, table->BgClipRect.Max, false); // Span all columns + draw_list->AddRectFilled(table->BgClipRect.Min, table->BgClipRect.Max, GetColorU32(ImGuiCol_TableHeaderBg, 0.25f)); // FIXME-STYLE: Change row background with an arbitrary color. + PushClipRect(ImVec2(clip_rect_min_x, table->BgClipRect.Min.y), table->BgClipRect.Max, true); // Span all columns + + const ImRect row_r(table->WorkRect.Min.x, table->BgClipRect.Min.y, table->WorkRect.Max.x, window->DC.CursorPos.y + row_height); + const ImGuiID row_id = GetID("##AngledHeaders"); + ButtonBehavior(row_r, row_id, NULL, NULL); + KeepAliveID(row_id); + + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); + int highlight_column_n = table->HighlightColumnHeader; + if (highlight_column_n == -1 && table->HoveredColumnBody != -1) + if (table_instance->HoveredRowLast == 0 && table->HoveredColumnBorder == -1 && (g.ActiveId == 0 || g.ActiveId == row_id || (table->IsActiveIdInTable || g.DragDropActive))) + highlight_column_n = table->HoveredColumnBody; + + float max_x = 0.0f; + for (int pass = 0; pass < 2; pass++) + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + { + if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) + continue; + const int column_n = table->DisplayOrderToIndex[order_n]; + ImGuiTableColumn* column = &table->Columns[column_n]; + if ((column->Flags & ImGuiTableColumnFlags_AngledHeader) == 0) // Note: can't rely on ImGuiTableColumnFlags_IsVisible test here. + continue; + + ImVec2 bg_shape[4]; + bg_shape[0] = ImVec2(column->MaxX, row_r.Max.y); + bg_shape[1] = ImVec2(column->MinX, row_r.Max.y); + bg_shape[2] = bg_shape[1] + header_angled_vector; + bg_shape[3] = bg_shape[0] + header_angled_vector; + if (pass == 0) + { + // Draw shape + draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_TableHeaderBg)); + if (column_n == highlight_column_n) + draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_Header)); // Highlight on hover + //draw_list->AddQuad(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_TableBorderLight), 1.0f); + max_x = ImMax(max_x, bg_shape[3].x); + + // Draw label (first draw at an offset where RenderTextXXX() function won't meddle with applying current ClipRect, then transform to final offset) + // FIXME: May be worth tidying up all those operations to make them easier to understand. + const char* label_name = TableGetColumnName(table, column_n); + const float clip_width = max_label_width - (sin_a * table->RowCellPaddingY); + ImRect label_r(window->ClipRect.Min, window->ClipRect.Min + ImVec2(clip_width + (flip_label ? 0.0f : table->CellPaddingX), header_height + table->RowCellPaddingY)); + ImVec2 label_size = CalcTextSize(label_name, NULL, true); + ImVec2 label_off = ImVec2(flip_label ? ImMax(0.0f, max_label_width - label_size.x - table->CellPaddingX) : table->CellPaddingX, table->RowCellPaddingY); + int vtx_idx_begin = draw_list->_VtxCurrentIdx; + RenderTextEllipsis(draw_list, label_r.Min + label_off, label_r.Max, label_r.Max.x, label_r.Max.x, label_name, NULL, &label_size); + //if (g.IO.KeyShift) { draw_list->AddRect(label_r.Min, label_r.Max, IM_COL32(0, 255, 0, 255), 0.0f, 0, 2.0f); } + int vtx_idx_end = draw_list->_VtxCurrentIdx; + + // Rotate and offset label + ImVec2 pivot_in = label_r.GetBL(); + ImVec2 pivot_out = ImVec2(column->WorkMinX, row_r.Max.y) + (flip_label ? (unit_right * clip_width) : ImVec2(header_height, 0.0f)); + ShadeVertsTransformPos(draw_list, vtx_idx_begin, vtx_idx_end, pivot_in, label_cos_a, label_sin_a, pivot_out); // Rotate and offset + } + if (pass == 1) + { + // Draw border + draw_list->AddLine(bg_shape[0], bg_shape[3], TableGetColumnBorderCol(table, order_n, column_n)); + } + } + PopClipRect(); + PopClipRect(); + table->TempData->AngledheadersExtraWidth = ImMax(0.0f, max_x - table->Columns[table->RightMostEnabledColumn].MaxX); +} + //------------------------------------------------------------------------- // [SECTION] Tables: Context Menu //------------------------------------------------------------------------- // - TableOpenContextMenu() [Internal] -// - TableDrawContextMenu() [Internal] +// - TableBeginContextMenuPopup() [Internal] +// - TableDrawDefaultContextMenu() [Internal] //------------------------------------------------------------------------- // Use -1 to open menu not specific to a given column. @@ -3080,7 +3303,13 @@ bool ImGui::TableBeginContextMenuPopup(ImGuiTable* table) // Output context menu into current window (generally a popup) // FIXME-TABLE: Ideally this should be writable by the user. Full programmatic access to that data? -void ImGui::TableDrawContextMenu(ImGuiTable* table) +// Sections to display are pulled from 'flags_for_section_to_display', which is typically == table->Flags. +// - ImGuiTableFlags_Resizable -> display Sizing menu items +// - ImGuiTableFlags_Reorderable -> display "Reset Order" +////- ImGuiTableFlags_Sortable -> display sorting options (disabled) +// - ImGuiTableFlags_Hideable -> display columns visibility menu items +// It means if you have a custom context menus you can call this section and omit some sections, and add your own. +void ImGui::TableDrawDefaultContextMenu(ImGuiTable* table, ImGuiTableFlags flags_for_section_to_display) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -3092,7 +3321,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) ImGuiTableColumn* column = (column_n != -1) ? &table->Columns[column_n] : NULL; // Sizing - if (table->Flags & ImGuiTableFlags_Resizable) + if (flags_for_section_to_display & ImGuiTableFlags_Resizable) { if (column != NULL) { @@ -3112,7 +3341,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) } // Ordering - if (table->Flags & ImGuiTableFlags_Reorderable) + if (flags_for_section_to_display & ImGuiTableFlags_Reorderable) { if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetOrder), NULL, false, !table->IsDefaultDisplayOrder)) table->IsResetDisplayOrderRequest = true; @@ -3126,7 +3355,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) // Sorting // (modify TableOpenContextMenu() to add _Sortable flag if enabling this) #if 0 - if ((table->Flags & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0) + if ((flags_for_section_to_display & ImGuiTableFlags_Sortable) && column != NULL && (column->Flags & ImGuiTableColumnFlags_NoSort) == 0) { if (want_separator) Separator(); @@ -3141,7 +3370,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table) #endif // Hiding / Visibility - if (table->Flags & ImGuiTableFlags_Hideable) + if (flags_for_section_to_display & ImGuiTableFlags_Hideable) { if (want_separator) Separator(); @@ -3577,7 +3806,8 @@ static const char* DebugNodeTableGetSizingPolicyDesc(ImGuiTableFlags sizing_poli void ImGui::DebugNodeTable(ImGuiTable* table) { - const bool is_active = (table->LastFrameActive >= GetFrameCount() - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. + ImGuiContext& g = *GImGui; + const bool is_active = (table->LastFrameActive >= g.FrameCount - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } bool open = TreeNode(table, "Table 0x%08X (%d columns, in '%s')%s", table->ID, table->ColumnsCount, table->OuterWindow->Name, is_active ? "" : " *Inactive*"); if (!is_active) { PopStyleColor(); } @@ -3589,12 +3819,24 @@ void ImGui::DebugNodeTable(ImGuiTable* table) return; if (table->InstanceCurrent > 0) Text("** %d instances of same table! Some data below will refer to last instance.", table->InstanceCurrent + 1); + if (g.IO.ConfigDebugIsDebuggerPresent) + { + if (DebugBreakButton("**DebugBreak**", "in BeginTable()")) + g.DebugBreakInTable = table->ID; + SameLine(); + } + bool clear_settings = SmallButton("Clear settings"); BulletText("OuterRect: Pos: (%.1f,%.1f) Size: (%.1f,%.1f) Sizing: '%s'", table->OuterRect.Min.x, table->OuterRect.Min.y, table->OuterRect.GetWidth(), table->OuterRect.GetHeight(), DebugNodeTableGetSizingPolicyDesc(table->Flags)); BulletText("ColumnsGivenWidth: %.1f, ColumnsAutoFitWidth: %.1f, InnerWidth: %.1f%s", table->ColumnsGivenWidth, table->ColumnsAutoFitWidth, table->InnerWidth, table->InnerWidth == 0.0f ? " (auto)" : ""); BulletText("CellPaddingX: %.1f, CellSpacingX: %.1f/%.1f, OuterPaddingX: %.1f", table->CellPaddingX, table->CellSpacingX1, table->CellSpacingX2, table->OuterPaddingX); BulletText("HoveredColumnBody: %d, HoveredColumnBorder: %d", table->HoveredColumnBody, table->HoveredColumnBorder); BulletText("ResizedColumn: %d, ReorderColumn: %d, HeldHeaderColumn: %d", table->ResizedColumn, table->ReorderColumn, table->HeldHeaderColumn); + for (int n = 0; n < table->InstanceCurrent + 1; n++) + { + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, n); + BulletText("Instance %d: HoveredRow: %d, LastOuterHeight: %.2f", n, table_instance->HoveredRowLast, table_instance->LastOuterHeight); + } //BulletText("BgDrawChannels: %d/%d", 0, table->BgDrawChannelUnfrozen); float sum_weights = 0.0f; for (int n = 0; n < table->ColumnsCount; n++) @@ -3904,7 +4146,7 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFl // Set state for first column // We aim so that the right-most column will have the same clipping width as other after being clipped by parent ClipRect const float column_padding = g.Style.ItemSpacing.x; - const float half_clip_extend_x = ImFloor(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize)); + const float half_clip_extend_x = ImTrunc(ImMax(window->WindowPadding.x * 0.5f, window->WindowBorderSize)); const float max_1 = window->WorkRect.Max.x + column_padding - ImMax(column_padding - window->WindowPadding.x, 0.0f); const float max_2 = window->WorkRect.Max.x + half_clip_extend_x; columns->OffMinX = window->DC.Indent.x - column_padding + ImMax(column_padding - window->WindowPadding.x, 0.0f); @@ -3951,8 +4193,9 @@ void ImGui::BeginColumns(const char* str_id, int columns_count, ImGuiOldColumnFl float width = offset_1 - offset_0; PushItemWidth(width * 0.65f); window->DC.ColumnsOffset.x = ImMax(column_padding - window->WindowPadding.x, 0.0f); - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->WorkRect.Max.x = window->Pos.x + offset_1 - column_padding; + window->WorkRect.Max.y = window->ContentRegionRect.Max.y; } void ImGui::NextColumn() @@ -3966,7 +4209,7 @@ void ImGui::NextColumn() if (columns->Count == 1) { - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); IM_ASSERT(columns->Current == 0); return; } @@ -3998,7 +4241,7 @@ void ImGui::NextColumn() window->DC.IsSameLine = false; columns->LineMinY = columns->LineMaxY; } - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); window->DC.CursorPos.y = columns->LineMinY; window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); window->DC.CurrLineTextBaseOffset = 0.0f; @@ -4062,7 +4305,7 @@ void ImGui::EndColumns() // Draw column const ImU32 col = GetColorU32(held ? ImGuiCol_SeparatorActive : hovered ? ImGuiCol_SeparatorHovered : ImGuiCol_Separator); - const float xi = IM_FLOOR(x); + const float xi = IM_TRUNC(x); window->DrawList->AddLine(ImVec2(xi, y1 + 1.0f), ImVec2(xi, y2), col); } @@ -4083,7 +4326,7 @@ void ImGui::EndColumns() window->ParentWorkRect = columns->HostBackupParentWorkRect; window->DC.CurrentColumns = NULL; window->DC.ColumnsOffset.x = 0.0f; - window->DC.CursorPos.x = IM_FLOOR(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); NavUpdateCurrentWindowIsScrollPushableX(); } diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index 43132447..e3f420a2 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.89.7 +// dear imgui, v1.90.1 // (widgets code) /* @@ -18,6 +18,8 @@ Index of this file: // [SECTION] Widgets: ColorEdit, ColorPicker, ColorButton, etc. // [SECTION] Widgets: TreeNode, CollapsingHeader, etc. // [SECTION] Widgets: Selectable +// [SECTION] Widgets: Typing-Select support +// [SECTION] Widgets: Multi-Select support // [SECTION] Widgets: ListBox // [SECTION] Widgets: PlotLines, PlotHistogram // [SECTION] Widgets: Value helpers @@ -41,11 +43,7 @@ Index of this file: #include "imgui_internal.h" // System includes -#if defined(_MSC_VER) && _MSC_VER <= 1500 // MSVC 2008 or earlier -#include // intptr_t -#else #include // intptr_t -#endif //------------------------------------------------------------------------- // Warnings @@ -124,7 +122,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); //------------------------------------------------------------------------- // For InputTextEx() -static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source); +static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source); static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); @@ -496,7 +494,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that. ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); if (flags & ImGuiButtonFlags_AllowOverlap) - item_flags |= ImGuiItemflags_AllowOverlap; + item_flags |= ImGuiItemFlags_AllowOverlap; if (flags & ImGuiButtonFlags_Repeat) item_flags |= ImGuiItemFlags_ButtonRepeat; @@ -806,14 +804,14 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) // Tweak 1: Shrink hit-testing area if button covers an abnormally large proportion of the visible region. That's in order to facilitate moving the window away. (#3825) // This may better be applied as a general hit-rect reduction mechanism for all widgets to ensure the area to move window is always accessible? - const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); + const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize)); ImRect bb_interact = bb; const float area_to_visible_ratio = window->OuterRectClipped.GetArea() / bb.GetArea(); if (area_to_visible_ratio < 1.5f) - bb_interact.Expand(ImFloor(bb_interact.GetSize() * -0.25f)); + bb_interact.Expand(ImTrunc(bb_interact.GetSize() * -0.25f)); // Tweak 2: We intentionally allow interaction when clipped so that a mechanical Alt,Right,Activate sequence can always close a window. - // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible). + // (this isn't the common behavior of buttons, but it doesn't affect the user because navigation tends to keep items visible in scrolling layer). bool is_clipped = !ItemAdd(bb_interact, id); bool hovered, held; @@ -842,17 +840,19 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f); - ItemAdd(bb, id); + ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize)); + bool is_clipped = !ItemAdd(bb, id); bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_None); + if (is_clipped) + return pressed; // Render ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); if (hovered || held) - window->DrawList->AddCircleFilled(bb.GetCenter()/*+ ImVec2(0.0f, -0.5f)*/, g.FontSize * 0.5f + 1.0f, bg_col); - RenderArrow(window->DrawList, bb.Min + g.Style.FramePadding, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); + window->DrawList->AddCircleFilled(bb.GetCenter() + ImVec2(0.0f, -0.5f), g.FontSize * 0.5f + 1.0f, bg_col); + RenderArrow(window->DrawList, bb.Min, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold if (IsItemActive() && IsMouseDragging(0)) @@ -875,9 +875,9 @@ ImRect ImGui::GetWindowScrollbarRect(ImGuiWindow* window, ImGuiAxis axis) const float scrollbar_size = window->ScrollbarSizes[axis ^ 1]; // (ScrollbarSizes.x = width of Y scrollbar; ScrollbarSizes.y = height of X scrollbar) IM_ASSERT(scrollbar_size > 0.0f); if (axis == ImGuiAxis_X) - return ImRect(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size), inner_rect.Max.x, outer_rect.Max.y); + return ImRect(inner_rect.Min.x, ImMax(outer_rect.Min.y, outer_rect.Max.y - border_size - scrollbar_size), inner_rect.Max.x - border_size, outer_rect.Max.y - border_size); else - return ImRect(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y, outer_rect.Max.x, inner_rect.Max.y); + return ImRect(ImMax(outer_rect.Min.x, outer_rect.Max.x - border_size - scrollbar_size), inner_rect.Min.y, outer_rect.Max.x - border_size, inner_rect.Max.y - border_size); } void ImGui::Scrollbar(ImGuiAxis axis) @@ -938,7 +938,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 const bool allow_interaction = (alpha >= 1.0f); ImRect bb = bb_frame; - bb.Expand(ImVec2(-ImClamp(IM_FLOOR((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_FLOOR((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); + bb.Expand(ImVec2(-ImClamp(IM_TRUNC((bb_frame_width - 2.0f) * 0.5f), 0.0f, 3.0f), -ImClamp(IM_TRUNC((bb_frame_height - 2.0f) * 0.5f), 0.0f, 3.0f))); // V denote the main, longer axis of the scrollbar (= height for a vertical scrollbar) const float scrollbar_size_v = (axis == ImGuiAxis_X) ? bb.GetWidth() : bb.GetHeight(); @@ -1007,33 +1007,30 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 return held; } -void ImGui::Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) +// - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples +// - 'uv0' and 'uv1' are texture coordinates. Read about them from the same link above. +void ImGui::Image(ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col) { ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return; - ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); - if (border_col.w > 0.0f) - bb.Max += ImVec2(2, 2); + const float border_size = (border_col.w > 0.0f) ? 1.0f : 0.0f; + const ImVec2 padding(border_size, border_size); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + image_size + padding * 2.0f); ItemSize(bb); if (!ItemAdd(bb, 0)) return; - if (border_col.w > 0.0f) - { - window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f); - window->DrawList->AddImage(user_texture_id, bb.Min + ImVec2(1, 1), bb.Max - ImVec2(1, 1), uv0, uv1, GetColorU32(tint_col)); - } - else - { - window->DrawList->AddImage(user_texture_id, bb.Min, bb.Max, uv0, uv1, GetColorU32(tint_col)); - } + // Render + if (border_size > 0.0f) + window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(border_col), 0.0f, ImDrawFlags_None, border_size); + window->DrawList->AddImage(user_texture_id, bb.Min + padding, bb.Max - padding, uv0, uv1, GetColorU32(tint_col)); } // ImageButton() is flawed as 'id' is always derived from 'texture_id' (see #2464 #1390) // We provide this internal helper to write your own variant while we figure out how to redesign the public ImageButton() API. -bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags) +bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col, ImGuiButtonFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = GetCurrentWindow(); @@ -1041,7 +1038,7 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size return false; const ImVec2 padding = g.Style.FramePadding; - const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size + padding * 2.0f); + const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + image_size + padding * 2.0f); ItemSize(bb); if (!ItemAdd(bb, id)) return false; @@ -1060,14 +1057,15 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& size return pressed; } -bool ImGui::ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col) +// Note that ImageButton() adds style.FramePadding*2.0f to provided size. This is in order to facilitate fitting an image in a button. +bool ImGui::ImageButton(const char* str_id, ImTextureID user_texture_id, const ImVec2& image_size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& bg_col, const ImVec4& tint_col) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (window->SkipItems) return false; - return ImageButtonEx(window->GetID(str_id), user_texture_id, size, uv0, uv1, bg_col, tint_col); + return ImageButtonEx(window->GetID(str_id), user_texture_id, image_size, uv0, uv1, bg_col, tint_col); } #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1134,12 +1132,12 @@ bool ImGui::Checkbox(const char* label, bool* v) { // Undocumented tristate/mixed/indeterminate checkbox (#2644) // This may seem awkwardly designed because the aim is to make ImGuiItemFlags_MixedValue supported by all widgets (not just checkbox) - ImVec2 pad(ImMax(1.0f, IM_FLOOR(square_sz / 3.6f)), ImMax(1.0f, IM_FLOOR(square_sz / 3.6f))); + ImVec2 pad(ImMax(1.0f, IM_TRUNC(square_sz / 3.6f)), ImMax(1.0f, IM_TRUNC(square_sz / 3.6f))); window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); } else if (*v) { - const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); + const float pad = ImMax(1.0f, IM_TRUNC(square_sz / 6.0f)); RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad * 2.0f); } @@ -1234,7 +1232,7 @@ bool ImGui::RadioButton(const char* label, bool active) window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), num_segment); if (active) { - const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f)); + const float pad = ImMax(1.0f, IM_TRUNC(square_sz / 6.0f)); window->DrawList->AddCircleFilled(center, radius - pad, GetColorU32(ImGuiCol_CheckMark)); } @@ -1414,26 +1412,19 @@ void ImGui::SeparatorEx(ImGuiSeparatorFlags flags, float thickness) else if (flags & ImGuiSeparatorFlags_Horizontal) { // Horizontal Separator - float x1 = window->Pos.x; - float x2 = window->Pos.x + window->Size.x; - - // FIXME-WORKRECT: old hack (#205) until we decide of consistent behavior with WorkRect/Indent and Separator - if (g.GroupStack.Size > 0 && g.GroupStack.back().WindowID == window->ID) - x1 += window->DC.Indent.x; - - // FIXME-WORKRECT: In theory we should simply be using WorkRect.Min.x/Max.x everywhere but it isn't aesthetically what we want, - // need to introduce a variant of WorkRect for that purpose. (#4787) - if (ImGuiTable* table = g.CurrentTable) - { - x1 = table->Columns[table->CurrentColumn].MinX; - x2 = table->Columns[table->CurrentColumn].MaxX; - } + float x1 = window->DC.CursorPos.x; + float x2 = window->WorkRect.Max.x; + // Preserve legacy behavior inside Columns() // Before Tables API happened, we relied on Separator() to span all columns of a Columns() set. // We currently don't need to provide the same feature for tables because tables naturally have border features. ImGuiOldColumns* columns = (flags & ImGuiSeparatorFlags_SpanAllColumns) ? window->DC.CurrentColumns : NULL; if (columns) + { + x1 = window->Pos.x + window->DC.Indent.x; // Used to be Pos.x before 2023/10/03 + x2 = window->Pos.x + window->Size.x; PushColumnsBackground(); + } // We don't provide our width to the layout so that it doesn't get feed back into AutoFit // FIXME: This prevents ->CursorMaxPos based bounding box evaluation from working (e.g. TableEndCell) @@ -1467,7 +1458,11 @@ void ImGui::Separator() // Those flags should eventually be configurable by the user // FIXME: We cannot g.Style.SeparatorTextBorderSize for thickness as it relates to SeparatorText() which is a decorated separator, not defaulting to 1.0f. ImGuiSeparatorFlags flags = (window->DC.LayoutType == ImGuiLayoutType_Horizontal) ? ImGuiSeparatorFlags_Vertical : ImGuiSeparatorFlags_Horizontal; - flags |= ImGuiSeparatorFlags_SpanAllColumns; // NB: this only applies to legacy Columns() api as they relied on Separator() a lot. + + // Only applies to legacy Columns() api as they relied on Separator() a lot. + if (window->DC.CurrentColumns) + flags |= ImGuiSeparatorFlags_SpanAllColumns; + SeparatorEx(flags, 1.0f); } @@ -1484,14 +1479,14 @@ void ImGui::SeparatorTextEx(ImGuiID id, const char* label, const char* label_end const float separator_thickness = style.SeparatorTextBorderSize; const ImVec2 min_size(label_size.x + extra_w + padding.x * 2.0f, ImMax(label_size.y + padding.y * 2.0f, separator_thickness)); const ImRect bb(pos, ImVec2(window->WorkRect.Max.x, pos.y + min_size.y)); - const float text_baseline_y = ImFloor((bb.GetHeight() - label_size.y) * style.SeparatorTextAlign.y + 0.99999f); //ImMax(padding.y, ImFloor((style.SeparatorTextSize - label_size.y) * 0.5f)); + const float text_baseline_y = ImTrunc((bb.GetHeight() - label_size.y) * style.SeparatorTextAlign.y + 0.99999f); //ImMax(padding.y, ImFloor((style.SeparatorTextSize - label_size.y) * 0.5f)); ItemSize(min_size, text_baseline_y); if (!ItemAdd(bb, id)) return; const float sep1_x1 = pos.x; const float sep2_x2 = bb.Max.x; - const float seps_y = ImFloor((bb.Min.y + bb.Max.y) * 0.5f + 0.99999f); + const float seps_y = ImTrunc((bb.Min.y + bb.Max.y) * 0.5f + 0.99999f); const float label_avail_w = ImMax(0.0f, sep2_x2 - sep1_x1 - padding.x * 2.0f); const ImVec2 label_pos(pos.x + padding.x + ImMax(0.0f, (label_avail_w - label_size.x - extra_w) * style.SeparatorTextAlign.x), pos.y + text_baseline_y); // FIXME-ALIGN @@ -1566,8 +1561,7 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float ImRect bb_render = bb; if (held) { - ImVec2 mouse_delta_2d = g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min; - float mouse_delta = (axis == ImGuiAxis_Y) ? mouse_delta_2d.y : mouse_delta_2d.x; + float mouse_delta = (g.IO.MousePos - g.ActiveIdClickOffset - bb_interact.Min)[axis]; // Minimum pane size float size_1_maximum_delta = ImMax(0.0f, *size1 - min_size1); @@ -1580,12 +1574,8 @@ bool ImGui::SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float // Apply resize if (mouse_delta != 0.0f) { - if (mouse_delta < 0.0f) - IM_ASSERT(*size1 + mouse_delta >= min_size1); - if (mouse_delta > 0.0f) - IM_ASSERT(*size2 - mouse_delta >= min_size2); - *size1 += mouse_delta; - *size2 -= mouse_delta; + *size1 = ImMax(*size1 + mouse_delta, min_size1); + *size2 = ImMax(*size2 - mouse_delta, min_size2); bb_render.Translate((axis == ImGuiAxis_X) ? ImVec2(mouse_delta, 0.0f) : ImVec2(0.0f, mouse_delta)); MarkItemEdited(id); } @@ -1639,7 +1629,7 @@ void ImGui::ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_exc width_excess = 0.0f; for (int n = 0; n < count; n++) { - float width_rounded = ImFloor(items[n].Width); + float width_rounded = ImTrunc(items[n].Width); width_excess += items[n].Width - width_rounded; items[n].Width = width_rounded; } @@ -1685,10 +1675,13 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF const ImGuiStyle& style = g.Style; const ImGuiID id = window->GetID(label); IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together + if (flags & ImGuiComboFlags_WidthFitPreview) + IM_ASSERT((flags & (ImGuiComboFlags_NoPreview | (ImGuiComboFlags)ImGuiComboFlags_CustomPreview)) == 0); const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight(); const ImVec2 label_size = CalcTextSize(label, NULL, true); - const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : CalcItemWidth(); + const float preview_width = ((flags & ImGuiComboFlags_WidthFitPreview) && (preview_value != NULL)) ? CalcTextSize(preview_value, NULL, true).x : 0.0f; + const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : ((flags & ImGuiComboFlags_WidthFitPreview) ? (arrow_size + preview_width + style.FramePadding.x * 2.0f) : CalcItemWidth()); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f)); const ImRect total_bb(bb.Min, bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); ItemSize(total_bb, style.FramePadding.y); @@ -1870,18 +1863,15 @@ void ImGui::EndComboPreview() } // Getter for the old Combo() API: const char*[] -static bool Items_ArrayGetter(void* data, int idx, const char** out_text) +static const char* Items_ArrayGetter(void* data, int idx) { const char* const* items = (const char* const*)data; - if (out_text) - *out_text = items[idx]; - return true; + return items[idx]; } // Getter for the old Combo() API: "item1\0item2\0item3\0" -static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) +static const char* Items_SingleStringGetter(void* data, int idx) { - // FIXME-OPT: we could pre-compute the indices to fasten this. But only 1 active combo means the waste is limited. const char* items_separated_by_zeros = (const char*)data; int items_count = 0; const char* p = items_separated_by_zeros; @@ -1892,22 +1882,18 @@ static bool Items_SingleStringGetter(void* data, int idx, const char** out_text) p += strlen(p) + 1; items_count++; } - if (!*p) - return false; - if (out_text) - *out_text = p; - return true; + return *p ? p : NULL; } // Old API, prefer using BeginCombo() nowadays if you can. -bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int popup_max_height_in_items) +bool ImGui::Combo(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), void* user_data, int items_count, int popup_max_height_in_items) { ImGuiContext& g = *GImGui; // Call the getter to obtain the preview string which is a parameter to BeginCombo() const char* preview_value = NULL; if (*current_item >= 0 && *current_item < items_count) - items_getter(data, *current_item, &preview_value); + preview_value = getter(user_data, *current_item); // The old Combo() API exposed "popup_max_height_in_items". The new more general BeginCombo() API doesn't have/need it, but we emulate it here. if (popup_max_height_in_items != -1 && !(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSizeConstraint)) @@ -1921,11 +1907,12 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*items_getter)(voi bool value_changed = false; for (int i = 0; i < items_count; i++) { + const char* item_text = getter(user_data, i); + if (item_text == NULL) + item_text = "*Unknown item*"; + PushID(i); const bool item_selected = (i == *current_item); - const char* item_text; - if (!items_getter(data, i, &item_text)) - item_text = "*Unknown item*"; if (Selectable(item_text, item_selected) && *current_item != i) { value_changed = true; @@ -1965,6 +1952,30 @@ bool ImGui::Combo(const char* label, int* current_item, const char* items_separa return value_changed; } +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + +struct ImGuiGetNameFromIndexOldToNewCallbackData { void* UserData; bool (*OldCallback)(void*, int, const char**); }; +static const char* ImGuiGetNameFromIndexOldToNewCallback(void* user_data, int idx) +{ + ImGuiGetNameFromIndexOldToNewCallbackData* data = (ImGuiGetNameFromIndexOldToNewCallbackData*)user_data; + const char* s = NULL; + data->OldCallback(data->UserData, idx, &s); + return s; +} + +bool ImGui::ListBox(const char* label, int* current_item, bool (*old_getter)(void*, int, const char**), void* user_data, int items_count, int height_in_items) +{ + ImGuiGetNameFromIndexOldToNewCallbackData old_to_new_data = { user_data, old_getter }; + return ListBox(label, current_item, ImGuiGetNameFromIndexOldToNewCallback, &old_to_new_data, items_count, height_in_items); +} +bool ImGui::Combo(const char* label, int* current_item, bool (*old_getter)(void*, int, const char**), void* user_data, int items_count, int popup_max_height_in_items) +{ + ImGuiGetNameFromIndexOldToNewCallbackData old_to_new_data = { user_data, old_getter }; + return Combo(label, current_item, ImGuiGetNameFromIndexOldToNewCallback, &old_to_new_data, items_count, popup_max_height_in_items); +} + +#endif + //------------------------------------------------------------------------- // [SECTION] Data Type and Data Formatting Helpers [Internal] //------------------------------------------------------------------------- @@ -2418,14 +2429,13 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (!temp_input_is_active) { // Tabbing or CTRL-clicking on Drag turns it into an InputText - const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = hovered && IsMouseClicked(0, id); const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2 && TestKeyOwner(ImGuiKey_MouseLeft, id)); - const bool make_active = (input_requested_by_tabbing || clicked || double_clicked || g.NavActivateId == id); + const bool make_active = (clicked || double_clicked || g.NavActivateId == id); if (make_active && (clicked || double_clicked)) SetKeyOwner(ImGuiKey_MouseLeft, id); if (make_active && temp_input_allowed) - if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || double_clicked || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) + if ((clicked && g.IO.KeyCtrl) || double_clicked || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) temp_input_is_active = true; // (Optional) simple click (without moving) turns Drag into an InputText @@ -2776,14 +2786,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); - const SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); + const float v_range_f = (float)(v_min < v_max ? v_max - v_min : v_min - v_max); // We don't need high precision for what we do with it. // Calculate bounds const float grab_padding = 2.0f; // FIXME: Should be part of style. const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; float grab_sz = style.GrabMinSize; - if (!is_floating_point && v_range >= 0) // v_range < 0 may happen on integer overflows - grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit + if (!is_floating_point && v_range_f >= 0.0f) // v_range_f < 0 may happen on integer overflows + grab_sz = ImMax(slider_sz / (v_range_f + 1), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit grab_sz = ImMin(grab_sz, slider_sz); const float slider_usable_sz = slider_sz - grab_sz; const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f; @@ -2852,8 +2862,8 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ } else { - if ((v_range >= -100.0f && v_range <= 100.0f) || tweak_slow) - input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / (float)v_range; // Gamepad/keyboard tweak speeds in integer steps + if ((v_range_f >= -100.0f && v_range_f <= 100.0f && v_range_f != 0.0f) || tweak_slow) + input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / v_range_f; // Gamepad/keyboard tweak speeds in integer steps else input_delta /= 100.0f; } @@ -2900,6 +2910,10 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ } } + if (set_new_value) + if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + set_new_value = false; + if (set_new_value) { TYPE v_new = ScaleValueFromRatioT(data_type, clicked_t, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize); @@ -2945,11 +2959,6 @@ bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flag! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); - // Those are the things we can do easily outside the SliderBehaviorT<> template, saves code generation. - ImGuiContext& g = *GImGui; - if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) - return false; - switch (data_type) { case ImGuiDataType_S8: { ImS32 v32 = (ImS32)*(ImS8*)p_v; bool r = SliderBehaviorT(bb, id, ImGuiDataType_S32, &v32, *(const ImS8*)p_min, *(const ImS8*)p_max, format, flags, out_grab_bb); if (r) *(ImS8*)p_v = (ImS8)v32; return r; } @@ -3011,13 +3020,12 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (!temp_input_is_active) { // Tabbing or CTRL-clicking on Slider turns it into an input box - const bool input_requested_by_tabbing = temp_input_allowed && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; const bool clicked = hovered && IsMouseClicked(0, id); - const bool make_active = (input_requested_by_tabbing || clicked || g.NavActivateId == id); + const bool make_active = (clicked || g.NavActivateId == id); if (make_active && clicked) SetKeyOwner(ImGuiKey_MouseLeft, id); if (make_active && temp_input_allowed) - if (input_requested_by_tabbing || (clicked && g.IO.KeyCtrl) || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) + if ((clicked && g.IO.KeyCtrl) || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))) temp_input_is_active = true; if (make_active && !temp_input_is_active) @@ -3389,14 +3397,6 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* return value_changed; } -static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(ImGuiDataType data_type, const char* format) -{ - if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) - return ImGuiInputTextFlags_CharsScientific; - const char format_last_char = format[0] ? format[strlen(format) - 1] : 0; - return (format_last_char == 'x' || format_last_char == 'X') ? ImGuiInputTextFlags_CharsHexadecimal : ImGuiInputTextFlags_CharsDecimal; -} - // Note that Drag/Slider functions are only forwarding the min/max values clamping values if the ImGuiSliderFlags_AlwaysClamp flag is set! // This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility. // However this may not be ideal for all uses, as some user code may break on out of bound values. @@ -3413,8 +3413,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); ImStrTrimBlanks(data_buf); - ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; - flags |= InputScalar_DefaultCharsFilter(data_type, format); + ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; bool value_changed = false; if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) @@ -3458,10 +3457,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data char buf[64]; DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); - // Testing ActiveId as a minor optimization as filtering is not needed until active - if (g.ActiveId == 0 && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0) - flags |= InputScalar_DefaultCharsFilter(data_type, format); - flags |= ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. + flags |= ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. bool value_changed = false; if (p_step == NULL) @@ -3555,7 +3551,6 @@ bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* p_dat bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags) { - flags |= ImGuiInputTextFlags_CharsScientific; return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step > 0.0f ? &step : NULL), (void*)(step_fast > 0.0f ? &step_fast : NULL), format, flags); } @@ -3598,7 +3593,6 @@ bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags) bool ImGui::InputDouble(const char* label, double* v, double step, double step_fast, const char* format, ImGuiInputTextFlags flags) { - flags |= ImGuiInputTextFlags_CharsScientific; return InputScalar(label, ImGuiDataType_Double, (void*)v, (void*)(step > 0.0 ? &step : NULL), (void*)(step_fast > 0.0 ? &step_fast : NULL), format, flags); } @@ -3696,8 +3690,8 @@ namespace ImStb { static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } -static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { return obj->TextW[idx]; } -static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } +static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenW); return obj->TextW[idx]; } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) @@ -3815,12 +3809,13 @@ static bool STB_TEXTEDIT_INSERTCHARS(ImGuiInputTextState* obj, int pos, const Im #define STB_TEXTEDIT_K_PGDOWN 0x20000F // keyboard input to move cursor down a page #define STB_TEXTEDIT_K_SHIFT 0x400000 -#define STB_TEXTEDIT_IMPLEMENTATION +#define IMSTB_TEXTEDIT_IMPLEMENTATION +#define IMSTB_TEXTEDIT_memmove memmove #include "imstb_textedit.h" // stb_textedit internally allows for a single undo record to do addition and deletion, but somehow, calling // the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?) -static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* state, const STB_TEXTEDIT_CHARTYPE* text, int text_len) +static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len) { stb_text_makeundo_replace(str, state, 0, str->CurLenW, text_len); ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenW); @@ -3908,7 +3903,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons } // Return false to discard a character. -static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) +static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) { IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard); unsigned int c = *p_char; @@ -3918,8 +3913,8 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f if (c < 0x20) { bool pass = false; - pass |= (c == '\n' && (flags & ImGuiInputTextFlags_Multiline)); // Note that an Enter KEY will emit \r and be ignored (we poll for KEY in InputText() code) - pass |= (c == '\t' && (flags & ImGuiInputTextFlags_AllowTabInput)); + pass |= (c == '\n') && (flags & ImGuiInputTextFlags_Multiline) != 0; // Note that an Enter KEY will emit \r and be ignored (we poll for KEY in InputText() code) + pass |= (c == '\t') && (flags & ImGuiInputTextFlags_AllowTabInput) != 0; if (!pass) return false; apply_named_filters = false; // Override named filters below so newline and tabs can still be inserted. @@ -3947,10 +3942,13 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. // We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point. // Change the default decimal_point with: - // ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; + // ImGui::GetIO()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; // Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions. - ImGuiContext& g = *GImGui; - const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint; + ImGuiContext& g = *ctx; + const unsigned c_decimal_point = (unsigned int)g.IO.PlatformLocaleDecimalPoint; + if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific)) + if (c == '.' || c == ',') + c = c_decimal_point; // Full-width -> half-width conversion for numeric fields (https://en.wikipedia.org/wiki/Halfwidth_and_Fullwidth_Forms_(Unicode_block) // While this is mostly convenient, this has the side-effect for uninformed users accidentally inputting full-width characters that they may @@ -4036,7 +4034,7 @@ static void InputTextReconcileUndoStateAfterUserCallback(ImGuiInputTextState* st const int insert_len = new_last_diff - first_diff + 1; const int delete_len = old_last_diff - first_diff + 1; if (insert_len > 0 || delete_len > 0) - if (STB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb.undostate, first_diff, delete_len, insert_len)) + if (IMSTB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb.undostate, first_diff, delete_len, insert_len)) for (int i = 0; i < delete_len; i++) p[i] = ImStb::STB_TEXTEDIT_GETCHAR(state, first_diff + i); } @@ -4088,12 +4086,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const bool RENDER_SELECTION_WHEN_INACTIVE = false; const bool is_multiline = (flags & ImGuiInputTextFlags_Multiline) != 0; - const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0; - const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0; - const bool is_undoable = (flags & ImGuiInputTextFlags_NoUndoRedo) == 0; - const bool is_resizable = (flags & ImGuiInputTextFlags_CallbackResize) != 0; - if (is_resizable) - IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag! if (is_multiline) // Open group before calling GetID() because groups tracks id created within their scope (including the scrollbar) BeginGroup(); @@ -4107,7 +4099,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImGuiWindow* draw_window = window; ImVec2 inner_size = frame_size; - ImGuiItemStatusFlags item_status_flags = 0; ImGuiLastItemData item_data_backup; if (is_multiline) { @@ -4118,17 +4109,25 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ EndGroup(); return false; } - item_status_flags = g.LastItemData.StatusFlags; item_data_backup = g.LastItemData; window->DC.CursorPos = backup_pos; + // Prevent NavActivation from Tabbing when our widget accepts Tab inputs: this allows cycling through widgets without stopping. + if (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_FromTabbing) && (flags & ImGuiInputTextFlags_AllowTabInput)) + g.NavActivateId = 0; + + // Prevent NavActivate reactivating in BeginChild() when we are already active. + const ImGuiID backup_activate_id = g.NavActivateId; + if (g.ActiveId == id) // Prevent reactivation + g.NavActivateId = 0; + // We reproduce the contents of BeginChildFrame() in order to provide 'label' so our window internal data are easier to read/debug. - // FIXME-NAV: Pressing NavActivate will trigger general child activation right before triggering our own below. Harmless but bizarre. PushStyleColor(ImGuiCol_ChildBg, style.Colors[ImGuiCol_FrameBg]); PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove); + g.NavActivateId = backup_activate_id; PopStyleVar(3); PopStyleColor(); if (!child_visible) @@ -4149,7 +4148,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!(flags & ImGuiInputTextFlags_MergedItem)) if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemFlags_Inputable)) return false; - item_status_flags = g.LastItemData.StatusFlags; } const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); if (hovered) @@ -4158,7 +4156,15 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We are only allowed to access the state if we are already the active widget. ImGuiInputTextState* state = GetInputTextState(id); - const bool input_requested_by_tabbing = (item_status_flags & ImGuiItemStatusFlags_FocusedByTabbing) != 0; + if (g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) + flags |= ImGuiInputTextFlags_ReadOnly; + const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0; + const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0; + const bool is_undoable = (flags & ImGuiInputTextFlags_NoUndoRedo) == 0; + const bool is_resizable = (flags & ImGuiInputTextFlags_CallbackResize) != 0; + if (is_resizable) + IM_ASSERT(callback != NULL); // Must provide a callback if you set the ImGuiInputTextFlags_CallbackResize flag! + const bool input_requested_by_nav = (g.ActiveId != id) && ((g.NavActivateId == id) && ((g.NavActivateFlags & ImGuiActivateFlags_PreferInput) || (g.NavInputSource == ImGuiInputSource_Keyboard))); const bool user_clicked = hovered && io.MouseClicked[0]; @@ -4170,7 +4176,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); // state != NULL means its our state. - const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav || input_requested_by_tabbing); + const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav); const bool init_state = (init_make_active || user_scroll_active); if ((init_state && g.ActiveId != id) || init_changed_specs) { @@ -4220,7 +4226,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ select_all = true; if (input_requested_by_nav && (!recycle_state || !(g.NavActivateFlags & ImGuiActivateFlags_TryToPreserveState))) select_all = true; - if (input_requested_by_tabbing || (user_clicked && io.KeyCtrl)) + if (user_clicked && io.KeyCtrl) select_all = true; } @@ -4383,10 +4389,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We expect backends to emit a Tab key but some also emit a Tab character which we ignore (#2467, #1336) // (For Tab and Enter: Win32/SFML/Allegro are sending both keys and chars, GLFW and SDL are only sending keys. For Space they all send all threes) - if ((flags & ImGuiInputTextFlags_AllowTabInput) && Shortcut(ImGuiKey_Tab, id) && !is_readonly) + if ((flags & ImGuiInputTextFlags_AllowTabInput) && Shortcut(ImGuiKey_Tab, id, ImGuiInputFlags_Repeat) && !is_readonly) { unsigned int c = '\t'; // Insert TAB - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } @@ -4402,7 +4408,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ unsigned int c = (unsigned int)io.InputQueueCharacters[n]; if (c == '\t') // Skip Tab, see above. continue; - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } @@ -4485,7 +4491,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (!is_readonly) { unsigned int c = '\n'; // Insert new line - if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) state->OnKeyPressed((int)c); } } @@ -4493,7 +4499,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { if (flags & ImGuiInputTextFlags_EscapeClearsAll) { - if (state->CurLenA > 0) + if (buf[0] != 0) { revert_edit = true; } @@ -4552,7 +4558,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { unsigned int c; s += ImTextCharFromUtf8(&c, s, NULL); - if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard)) + if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; } @@ -4581,10 +4587,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (flags & ImGuiInputTextFlags_EscapeClearsAll) { // Clear input + IM_ASSERT(buf[0] != 0); apply_new_text = ""; apply_new_text_length = 0; - value_changed |= (buf[0] != 0); - STB_TEXTEDIT_CHARTYPE empty_string; + value_changed = true; + IMSTB_TEXTEDIT_CHARTYPE empty_string; stb_textedit_replace(state, &state->Stb, &empty_string, 0); } else if (strcmp(buf, state->InitialTextA.Data) != 0) @@ -4612,9 +4619,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); } - // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. + // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer + // before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. - // This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize). + // This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage + // (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object + // unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize). const bool apply_edit_back_to_user_buffer = !revert_edit || (validated && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); if (apply_edit_back_to_user_buffer) { @@ -4690,7 +4700,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (callback_data.SelectionEnd != utf8_selection_end || buf_dirty) { state->Stb.select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb.select_start : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } if (buf_dirty) { - IM_ASSERT((flags & ImGuiInputTextFlags_ReadOnly) == 0); + IM_ASSERT(!is_readonly); IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! InputTextReconcileUndoStateAfterUserCallback(state, callback_data.Buf, callback_data.BufTextLen); // FIXME: Move the rest of this block inside function and rename to InputTextReconcileStateAfterUserCallback() ? if (callback_data.BufTextLen > backup_current_text_length && is_resizable) @@ -4715,11 +4725,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Handle reapplying final data on deactivation (see InputTextDeactivateHook() for details) if (g.InputTextDeactivatedState.ID == id) { - if (g.ActiveId != id && IsItemDeactivatedAfterEdit() && !is_readonly) + if (g.ActiveId != id && IsItemDeactivatedAfterEdit() && !is_readonly && strcmp(g.InputTextDeactivatedState.TextA.Data, buf) != 0) { apply_new_text = g.InputTextDeactivatedState.TextA.Data; apply_new_text_length = g.InputTextDeactivatedState.TextA.Size - 1; - value_changed |= (strcmp(g.InputTextDeactivatedState.TextA.Data, buf) != 0); + value_changed = true; //IMGUI_DEBUG_LOG("InputText(): apply Deactivated data for 0x%08X: \"%.*s\".\n", id, apply_new_text_length, apply_new_text); } g.InputTextDeactivatedState.ID = 0; @@ -4861,9 +4871,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const float scroll_increment_x = inner_size.x * 0.25f; const float visible_width = inner_size.x - style.FramePadding.x; if (cursor_offset.x < state->ScrollX) - state->ScrollX = IM_FLOOR(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); + state->ScrollX = IM_TRUNC(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); else if (cursor_offset.x - visible_width >= state->ScrollX) - state->ScrollX = IM_FLOOR(cursor_offset.x - visible_width + scroll_increment_x); + state->ScrollX = IM_TRUNC(cursor_offset.x - visible_width + scroll_increment_x); } else { @@ -4913,7 +4923,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else { ImVec2 rect_size = InputTextCalcTextSizeW(&g, p, text_selected_end, &p, NULL, true); - if (rect_size.x <= 0.0f) rect_size.x = IM_FLOOR(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines + if (rect_size.x <= 0.0f) rect_size.x = IM_TRUNC(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos + ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); if (rect.Overlaps(clip_rect)) @@ -4936,7 +4946,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { state->CursorAnim += io.DeltaTime; bool cursor_is_visible = (!g.IO.ConfigInputTextCursorBlink) || (state->CursorAnim <= 0.0f) || ImFmod(state->CursorAnim, 1.20f) <= 0.80f; - ImVec2 cursor_screen_pos = ImFloor(draw_pos + cursor_offset - draw_scroll); + ImVec2 cursor_screen_pos = ImTrunc(draw_pos + cursor_offset - draw_scroll); ImRect cursor_screen_rect(cursor_screen_pos.x, cursor_screen_pos.y - g.FontSize + 0.5f, cursor_screen_pos.x + 1.0f, cursor_screen_pos.y - 1.5f); if (cursor_is_visible && cursor_screen_rect.Overlaps(clip_rect)) draw_window->DrawList->AddLine(cursor_screen_rect.Min, cursor_screen_rect.GetBL(), GetColorU32(ImGuiCol_Text)); @@ -5020,10 +5030,10 @@ void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state) Text("CurLenW: %d, CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenW, state->CurLenA, stb_state->cursor, stb_state->select_start, stb_state->select_end); Text("has_preferred_x: %d (%.2f)", stb_state->has_preferred_x, stb_state->preferred_x); Text("undo_point: %d, redo_point: %d, undo_char_point: %d, redo_char_point: %d", undo_state->undo_point, undo_state->redo_point, undo_state->undo_char_point, undo_state->redo_char_point); - if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 15), true)) // Visualize undo state + if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 10), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) // Visualize undo state { PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); - for (int n = 0; n < STB_TEXTEDIT_UNDOSTATECOUNT; n++) + for (int n = 0; n < IMSTB_TEXTEDIT_UNDOSTATECOUNT; n++) { ImStb::StbUndoRecord* undo_rec = &undo_state->undo_rec[n]; const char undo_rec_type = (n < undo_state->undo_point) ? 'u' : (n >= undo_state->redo_point) ? 'r' : ' '; @@ -5105,10 +5115,8 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag ImGuiContext& g = *GImGui; const ImGuiStyle& style = g.Style; const float square_sz = GetFrameHeight(); - const float w_full = CalcItemWidth(); - const float w_button = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x); - const float w_inputs = w_full - w_button; const char* label_display_end = FindRenderedTextEnd(label); + float w_full = CalcItemWidth(); g.NextItemData.ClearFlags(); BeginGroup(); @@ -5142,6 +5150,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag const bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0; const bool hdr = (flags & ImGuiColorEditFlags_HDR) != 0; const int components = alpha ? 4 : 3; + const float w_button = (flags & ImGuiColorEditFlags_NoSmallPreview) ? 0.0f : (square_sz + style.ItemInnerSpacing.x); + const float w_inputs = ImMax(w_full - w_button, 1.0f); + w_full = w_inputs + w_button; // Convert to the formats we need float f[4] = { col[0], col[1], col[2], alpha ? col[3] : 1.0f }; @@ -5165,10 +5176,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0) { // RGB/HSV 0..255 Sliders - const float w_item_one = ImMax(1.0f, IM_FLOOR((w_inputs - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components)); - const float w_item_last = ImMax(1.0f, IM_FLOOR(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components - 1))); + const float w_items = w_inputs - style.ItemInnerSpacing.x * (components - 1); - const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); + const bool hide_prefix = (IM_TRUNC(w_items / components) <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x); static const char* ids[4] = { "##X", "##Y", "##Z", "##W" }; static const char* fmt_table_int[3][4] = { @@ -5184,11 +5194,14 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag }; const int fmt_idx = hide_prefix ? 0 : (flags & ImGuiColorEditFlags_DisplayHSV) ? 2 : 1; + float prev_split = 0.0f; for (int n = 0; n < components; n++) { if (n > 0) SameLine(0, style.ItemInnerSpacing.x); - SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last); + float next_split = IM_TRUNC(w_items * (n + 1) / components); + SetNextItemWidth(ImMax(next_split - prev_split, 1.0f)); + prev_split = next_split; // FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0. if (flags & ImGuiColorEditFlags_Float) @@ -5213,7 +5226,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag else ImFormatString(buf, IM_ARRAYSIZE(buf), "#%02X%02X%02X", ImClamp(i[0], 0, 255), ImClamp(i[1], 0, 255), ImClamp(i[2], 0, 255)); SetNextItemWidth(w_inputs); - if (InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase)) + if (InputText("##Text", buf, IM_ARRAYSIZE(buf), ImGuiInputTextFlags_CharsUppercase)) { value_changed = true; char* p = buf; @@ -5311,7 +5324,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Drag and Drop Target // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test. - if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) + if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) { bool accepted_drag_drop = false; if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) @@ -5376,6 +5389,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl ImGuiIO& io = g.IO; const float width = CalcItemWidth(); + const bool is_readonly = ((g.NextItemData.ItemFlags | g.CurrentItemFlags) & ImGuiItemFlags_ReadOnly) != 0; g.NextItemData.ClearFlags(); PushID(label); @@ -5410,7 +5424,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl float sv_picker_size = ImMax(bars_width * 1, width - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x; float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x; - float bars_triangles_half_sz = IM_FLOOR(bars_width * 0.20f); + float bars_triangles_half_sz = IM_TRUNC(bars_width * 0.20f); float backup_initial_col[4]; memcpy(backup_initial_col, col, components * sizeof(float)); @@ -5446,7 +5460,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { // Hue wheel + SV triangle logic InvisibleButton("hsv", ImVec2(sv_picker_size + style.ItemInnerSpacing.x + bars_width, sv_picker_size)); - if (IsItemActive()) + if (IsItemActive() && !is_readonly) { ImVec2 initial_off = g.IO.MouseClickedPos[0] - wheel_center; ImVec2 current_off = g.IO.MousePos - wheel_center; @@ -5481,7 +5495,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl { // SV rectangle logic InvisibleButton("sv", ImVec2(sv_picker_size, sv_picker_size)); - if (IsItemActive()) + if (IsItemActive() && !is_readonly) { S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size - 1)); V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); @@ -5494,7 +5508,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl // Hue bar logic SetCursorScreenPos(ImVec2(bar0_pos_x, picker_pos.y)); InvisibleButton("hue", ImVec2(bars_width, sv_picker_size)); - if (IsItemActive()) + if (IsItemActive() && !is_readonly) { H = ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1)); value_changed = value_changed_h = true; @@ -5578,7 +5592,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl if ((flags & ImGuiColorEditFlags_NoInputs) == 0) { PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x); - ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; + ImGuiColorEditFlags sub_flags_to_forward = ImGuiColorEditFlags_DataTypeMask_ | ImGuiColorEditFlags_InputMask_ | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoSmallPreview | ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf; ImGuiColorEditFlags sub_flags = (flags & sub_flags_to_forward) | ImGuiColorEditFlags_NoPicker; if (flags & ImGuiColorEditFlags_DisplayRGB || (flags & ImGuiColorEditFlags_DisplayMask_) == 0) if (ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_DisplayRGB)) @@ -5700,7 +5714,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl } // Render cursor/preview circle (clamp S/V within 0..1 range because floating points colors may lead HSV values to be out of range) - float sv_cursor_rad = value_changed_sv ? 10.0f : 6.0f; + float sv_cursor_rad = value_changed_sv ? wheel_thickness * 0.55f : wheel_thickness * 0.40f; int sv_cursor_segments = draw_list->_CalcCircleAutoSegmentCount(sv_cursor_rad); // Lock segment count so the +1 one matches others. draw_list->AddCircleFilled(sv_cursor_pos, sv_cursor_rad, user_col32_striped_of_alpha, sv_cursor_segments); draw_list->AddCircle(sv_cursor_pos, sv_cursor_rad + 1, col_midgrey, sv_cursor_segments); @@ -5878,6 +5892,7 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup("context")) return; ImGuiContext& g = *GImGui; + g.LockMarkEdited++; ImGuiColorEditFlags opts = g.ColorEditOptions; if (allow_opt_inputs) { @@ -5920,6 +5935,7 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) g.ColorEditOptions = opts; EndPopup(); + g.LockMarkEdited--; } void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags) @@ -5929,6 +5945,7 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup("context")) return; ImGuiContext& g = *GImGui; + g.LockMarkEdited++; if (allow_opt_picker) { ImVec2 picker_size(g.FontSize * 8, ImMax(g.FontSize * 8 - (GetFrameHeight() + g.Style.ItemInnerSpacing.x), 1.0f)); // FIXME: Picker size copied from main picker function @@ -5958,6 +5975,7 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl CheckboxFlags("Alpha Bar", &g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); } EndPopup(); + g.LockMarkEdited--; } //------------------------------------------------------------------------- @@ -6130,42 +6148,69 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // We vertically grow up to current line height up the typical widget height. const float frame_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y * 2), label_size.y + padding.y * 2); + const bool span_all_columns = (flags & ImGuiTreeNodeFlags_SpanAllColumns) != 0 && (g.CurrentTable != NULL); ImRect frame_bb; - frame_bb.Min.x = (flags & ImGuiTreeNodeFlags_SpanFullWidth) ? window->WorkRect.Min.x : window->DC.CursorPos.x; + frame_bb.Min.x = span_all_columns ? window->ParentWorkRect.Min.x : (flags & ImGuiTreeNodeFlags_SpanFullWidth) ? window->WorkRect.Min.x : window->DC.CursorPos.x; frame_bb.Min.y = window->DC.CursorPos.y; - frame_bb.Max.x = window->WorkRect.Max.x; + frame_bb.Max.x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x; frame_bb.Max.y = window->DC.CursorPos.y + frame_height; if (display_frame) { // Framed header expand a little outside the default padding, to the edge of InnerClipRect // (FIXME: May remove this at some point and make InnerClipRect align with WindowPadding.x instead of WindowPadding.x*0.5f) - frame_bb.Min.x -= IM_FLOOR(window->WindowPadding.x * 0.5f - 1.0f); - frame_bb.Max.x += IM_FLOOR(window->WindowPadding.x * 0.5f); + frame_bb.Min.x -= IM_TRUNC(window->WindowPadding.x * 0.5f - 1.0f); + frame_bb.Max.x += IM_TRUNC(window->WindowPadding.x * 0.5f); } - const float text_offset_x = g.FontSize + (display_frame ? padding.x * 3 : padding.x * 2); // Collapser arrow width + Spacing + const float text_offset_x = g.FontSize + (display_frame ? padding.x * 3 : padding.x * 2); // Collapsing arrow width + Spacing const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it - const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x * 2 : 0.0f); // Include collapser + const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x * 2 : 0.0f); // Include collapsing ImVec2 text_pos(window->DC.CursorPos.x + text_offset_x, window->DC.CursorPos.y + text_offset_y); ItemSize(ImVec2(text_width, frame_height), padding.y); // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing ImRect interact_bb = frame_bb; - if (!display_frame && (flags & (ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth)) == 0) + if (!display_frame && (flags & (ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_SpanAllColumns)) == 0) interact_bb.Max.x = frame_bb.Min.x + text_width + style.ItemSpacing.x * 2.0f; - // Store a flag for the current depth to tell if we will allow closing this node when navigating one of its child. - // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). - // This is currently only support 32 level deep and we are fine with (1 << Depth) overflowing into a zero. - const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; - bool is_open = TreeNodeUpdateNextOpen(id, flags); - if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) - window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); + // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. + const float backup_clip_rect_min_x = window->ClipRect.Min.x; + const float backup_clip_rect_max_x = window->ClipRect.Max.x; + if (span_all_columns) + { + window->ClipRect.Min.x = window->ParentWorkRect.Min.x; + window->ClipRect.Max.x = window->ParentWorkRect.Max.x; + } + // Compute open and multi-select states before ItemAdd() as it clear NextItem data. + bool is_open = TreeNodeUpdateNextOpen(id, flags); bool item_add = ItemAdd(interact_bb, id); g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; g.LastItemData.DisplayRect = frame_bb; + if (span_all_columns) + { + window->ClipRect.Min.x = backup_clip_rect_min_x; + window->ClipRect.Max.x = backup_clip_rect_max_x; + } + + // If a NavLeft request is happening and ImGuiTreeNodeFlags_NavLeftJumpsBackHere enabled: + // Store data for the current depth to allow returning to this node from any child item. + // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). + // It will become tempting to enable ImGuiTreeNodeFlags_NavLeftJumpsBackHere by default or move it to ImGuiStyle. + // Currently only supports 32 level deep and we are fine with (1 << Depth) overflowing into a zero, easy to increase. + if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) + if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) + { + g.NavTreeNodeStack.resize(g.NavTreeNodeStack.Size + 1); + ImGuiNavTreeNodeData* nav_tree_node_data = &g.NavTreeNodeStack.back(); + nav_tree_node_data->ID = id; + nav_tree_node_data->InFlags = g.LastItemData.InFlags; + nav_tree_node_data->NavRect = g.LastItemData.NavRect; + window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); + } + + const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; if (!item_add) { if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) @@ -6174,8 +6219,15 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l return is_open; } + if (span_all_columns) + { + TablePushBackgroundChannel(); + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasClipRect; + g.LastItemData.ClipRect = window->ClipRect; + } + ImGuiButtonFlags button_flags = ImGuiTreeNodeFlags_None; - if ((flags & ImGuiTreeNodeFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap)) + if ((flags & ImGuiTreeNodeFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap)) button_flags |= ImGuiButtonFlags_AllowOverlap; if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; @@ -6274,7 +6326,6 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (g.LogEnabled) LogSetNextTextDecoration("###", "###"); - RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); } else { @@ -6291,9 +6342,17 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 0.70f); if (g.LogEnabled) LogSetNextTextDecoration(">", NULL); - RenderText(text_pos, label, label_end, false); } + if (span_all_columns) + TablePopBackgroundChannel(); + + // Label + if (display_frame) + RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); + else + RenderText(text_pos, label, label_end, false); + if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); @@ -6335,12 +6394,14 @@ void ImGui::TreePop() ImU32 tree_depth_mask = (1 << window->DC.TreeDepth); // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled) - if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) - if (g.NavIdIsAlive && (window->DC.TreeJumpToParentOnPopMask & tree_depth_mask)) - { - SetNavID(window->IDStack.back(), g.NavLayer, 0, ImRect()); - NavMoveRequestCancel(); - } + if (window->DC.TreeJumpToParentOnPopMask & tree_depth_mask) // Only set during request + { + ImGuiNavTreeNodeData* nav_tree_node_data = &g.NavTreeNodeStack.back(); + IM_ASSERT(nav_tree_node_data->ID == window->IDStack.back()); + if (g.NavIdIsAlive && g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) + NavMoveRequestResolveWithPastTreeNode(&g.NavMoveResultLocal, nav_tree_node_data); + g.NavTreeNodeStack.pop_back(); + } window->DC.TreeJumpToParentOnPopMask &= tree_depth_mask - 1; IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much. @@ -6392,7 +6453,7 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFl ImGuiID id = window->GetID(label); flags |= ImGuiTreeNodeFlags_CollapsingHeader; if (p_visible) - flags |= ImGuiTreeNodeFlags_AllowOverlap | ImGuiTreeNodeFlags_ClipLabelForTrailingButton; + flags |= ImGuiTreeNodeFlags_AllowOverlap | (ImGuiTreeNodeFlags)ImGuiTreeNodeFlags_ClipLabelForTrailingButton; bool is_open = TreeNodeBehavior(id, flags, label); if (p_visible != NULL) { @@ -6402,8 +6463,8 @@ bool ImGui::CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFl ImGuiContext& g = *GImGui; ImGuiLastItemData last_item_backup = g.LastItemData; float button_size = g.FontSize; - float button_x = ImMax(g.LastItemData.Rect.Min.x, g.LastItemData.Rect.Max.x - g.Style.FramePadding.x * 2.0f - button_size); - float button_y = g.LastItemData.Rect.Min.y; + float button_x = ImMax(g.LastItemData.Rect.Min.x, g.LastItemData.Rect.Max.x - g.Style.FramePadding.x - button_size); + float button_y = g.LastItemData.Rect.Min.y + g.Style.FramePadding.y; ImGuiID close_button_id = GetIDWithSeed("#CLOSE", NULL, id); if (CloseButton(close_button_id, ImVec2(button_x, button_y))) *p_visible = false; @@ -6458,8 +6519,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl { const float spacing_x = span_all_columns ? 0.0f : style.ItemSpacing.x; const float spacing_y = style.ItemSpacing.y; - const float spacing_L = IM_FLOOR(spacing_x * 0.50f); - const float spacing_U = IM_FLOOR(spacing_y * 0.50f); + const float spacing_L = IM_TRUNC(spacing_x * 0.50f); + const float spacing_U = IM_TRUNC(spacing_y * 0.50f); bb.Min.x -= spacing_L; bb.Min.y -= spacing_U; bb.Max.x += (spacing_x - spacing_L); @@ -6467,7 +6528,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl } //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); } - // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackground for every Selectable.. + // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. const float backup_clip_rect_min_x = window->ClipRect.Min.x; const float backup_clip_rect_max_x = window->ClipRect.Max.x; if (span_all_columns) @@ -6493,10 +6554,15 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl // FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only, // which would be advantageous since most selectable are not selected. - if (span_all_columns && window->DC.CurrentColumns) - PushColumnsBackground(); - else if (span_all_columns && g.CurrentTable) - TablePushBackgroundChannel(); + if (span_all_columns) + { + if (g.CurrentTable) + TablePushBackgroundChannel(); + else if (window->DC.CurrentColumns) + PushColumnsBackground(); + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasClipRect; + g.LastItemData.ClipRect = window->ClipRect; + } // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries ImGuiButtonFlags button_flags = 0; @@ -6505,7 +6571,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } - if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemflags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } + if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } const bool was_selected = selected; bool hovered, held; @@ -6544,12 +6610,16 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(bb.Min, bb.Max, col, false, 0.0f); } - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + if (g.NavId == id) + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); - if (span_all_columns && window->DC.CurrentColumns) - PopColumnsBackground(); - else if (span_all_columns && g.CurrentTable) - TablePopBackgroundChannel(); + if (span_all_columns) + { + if (g.CurrentTable) + TablePopBackgroundChannel(); + else if (window->DC.CurrentColumns) + PopColumnsBackground(); + } RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); @@ -6574,6 +6644,212 @@ bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags return false; } + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Typing-Select support +//------------------------------------------------------------------------- + +// [Experimental] Currently not exposed in public API. +// Consume character inputs and return search request, if any. +// This would typically only be called on the focused window or location you want to grab inputs for, e.g. +// if (ImGui::IsWindowFocused(...)) +// if (ImGuiTypingSelectRequest* req = ImGui::GetTypingSelectRequest()) +// focus_idx = ImGui::TypingSelectFindMatch(req, my_items.size(), [](void*, int n) { return my_items[n]->Name; }, &my_items, -1); +// However the code is written in a way where calling it from multiple locations is safe (e.g. to obtain buffer). +ImGuiTypingSelectRequest* ImGui::GetTypingSelectRequest(ImGuiTypingSelectFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiTypingSelectState* data = &g.TypingSelectState; + ImGuiTypingSelectRequest* out_request = &data->Request; + + // Clear buffer + const float TYPING_SELECT_RESET_TIMER = 1.80f; // FIXME: Potentially move to IO config. + const int TYPING_SELECT_SINGLE_CHAR_COUNT_FOR_LOCK = 4; // Lock single char matching when repeating same char 4 times + if (data->SearchBuffer[0] != 0) + { + bool clear_buffer = false; + clear_buffer |= (g.NavFocusScopeId != data->FocusScope); + clear_buffer |= (data->LastRequestTime + TYPING_SELECT_RESET_TIMER < g.Time); + clear_buffer |= g.NavAnyRequest; + clear_buffer |= g.ActiveId != 0 && g.NavActivateId == 0; // Allow temporary SPACE activation to not interfere + clear_buffer |= IsKeyPressed(ImGuiKey_Escape) || IsKeyPressed(ImGuiKey_Enter); + clear_buffer |= IsKeyPressed(ImGuiKey_Backspace) && (flags & ImGuiTypingSelectFlags_AllowBackspace) == 0; + //if (clear_buffer) { IMGUI_DEBUG_LOG("GetTypingSelectRequest(): Clear SearchBuffer.\n"); } + if (clear_buffer) + data->Clear(); + } + + // Append to buffer + const int buffer_max_len = IM_ARRAYSIZE(data->SearchBuffer) - 1; + int buffer_len = (int)strlen(data->SearchBuffer); + bool select_request = false; + for (ImWchar w : g.IO.InputQueueCharacters) + { + const int w_len = ImTextCountUtf8BytesFromStr(&w, &w + 1); + if (w < 32 || (buffer_len == 0 && ImCharIsBlankW(w)) || (buffer_len + w_len > buffer_max_len)) // Ignore leading blanks + continue; + char w_buf[5]; + ImTextCharToUtf8(w_buf, (unsigned int)w); + if (data->SingleCharModeLock && w_len == out_request->SingleCharSize && memcmp(w_buf, data->SearchBuffer, w_len) == 0) + { + select_request = true; // Same character: don't need to append to buffer. + continue; + } + if (data->SingleCharModeLock) + { + data->Clear(); // Different character: clear + buffer_len = 0; + } + memcpy(data->SearchBuffer + buffer_len, w_buf, w_len + 1); // Append + buffer_len += w_len; + select_request = true; + } + g.IO.InputQueueCharacters.resize(0); + + // Handle backspace + if ((flags & ImGuiTypingSelectFlags_AllowBackspace) && IsKeyPressed(ImGuiKey_Backspace, 0, ImGuiInputFlags_Repeat)) + { + char* p = (char*)(void*)ImTextFindPreviousUtf8Codepoint(data->SearchBuffer, data->SearchBuffer + buffer_len); + *p = 0; + buffer_len = (int)(p - data->SearchBuffer); + } + + // Return request if any + if (buffer_len == 0) + return NULL; + if (select_request) + { + data->FocusScope = g.NavFocusScopeId; + data->LastRequestFrame = g.FrameCount; + data->LastRequestTime = (float)g.Time; + } + out_request->Flags = flags; + out_request->SearchBufferLen = buffer_len; + out_request->SearchBuffer = data->SearchBuffer; + out_request->SelectRequest = (data->LastRequestFrame == g.FrameCount); + out_request->SingleCharMode = false; + out_request->SingleCharSize = 0; + + // Calculate if buffer contains the same character repeated. + // - This can be used to implement a special search mode on first character. + // - Performed on UTF-8 codepoint for correctness. + // - SingleCharMode is always set for first input character, because it usually leads to a "next". + if (flags & ImGuiTypingSelectFlags_AllowSingleCharMode) + { + const char* buf_begin = out_request->SearchBuffer; + const char* buf_end = out_request->SearchBuffer + out_request->SearchBufferLen; + const int c0_len = ImTextCountUtf8BytesFromChar(buf_begin, buf_end); + const char* p = buf_begin + c0_len; + for (; p < buf_end; p += c0_len) + if (memcmp(buf_begin, p, (size_t)c0_len) != 0) + break; + const int single_char_count = (p == buf_end) ? (out_request->SearchBufferLen / c0_len) : 0; + out_request->SingleCharMode = (single_char_count > 0 || data->SingleCharModeLock); + out_request->SingleCharSize = (ImS8)c0_len; + data->SingleCharModeLock |= (single_char_count >= TYPING_SELECT_SINGLE_CHAR_COUNT_FOR_LOCK); // From now on we stop search matching to lock to single char mode. + } + + return out_request; +} + +static int ImStrimatchlen(const char* s1, const char* s1_end, const char* s2) +{ + int match_len = 0; + while (s1 < s1_end && ImToUpper(*s1++) == ImToUpper(*s2++)) + match_len++; + return match_len; +} + +// Default handler for finding a result for typing-select. You may implement your own. +// You might want to display a tooltip to visualize the current request SearchBuffer +// When SingleCharMode is set: +// - it is better to NOT display a tooltip of other on-screen display indicator. +// - the index of the currently focused item is required. +// if your SetNextItemSelectionData() values are indices, you can obtain it from ImGuiMultiSelectIO::NavIdItem, otherwise from g.NavLastValidSelectionUserData. +int ImGui::TypingSelectFindMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx) +{ + if (req == NULL || req->SelectRequest == false) // Support NULL parameter so both calls can be done from same spot. + return -1; + int idx = -1; + if (req->SingleCharMode && (req->Flags & ImGuiTypingSelectFlags_AllowSingleCharMode)) + idx = TypingSelectFindNextSingleCharMatch(req, items_count, get_item_name_func, user_data, nav_item_idx); + else + idx = TypingSelectFindBestLeadingMatch(req, items_count, get_item_name_func, user_data); + if (idx != -1) + NavRestoreHighlightAfterMove(); + return idx; +} + +// Special handling when a single character is repeated: perform search on a single letter and goes to next. +int ImGui::TypingSelectFindNextSingleCharMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx) +{ + // FIXME: Assume selection user data is index. Would be extremely practical. + //if (nav_item_idx == -1) + // nav_item_idx = (int)g.NavLastValidSelectionUserData; + + int first_match_idx = -1; + bool return_next_match = false; + for (int idx = 0; idx < items_count; idx++) + { + const char* item_name = get_item_name_func(user_data, idx); + if (ImStrimatchlen(req->SearchBuffer, req->SearchBuffer + req->SingleCharSize, item_name) < req->SingleCharSize) + continue; + if (return_next_match) // Return next matching item after current item. + return idx; + if (first_match_idx == -1 && nav_item_idx == -1) // Return first match immediately if we don't have a nav_item_idx value. + return idx; + if (first_match_idx == -1) // Record first match for wrapping. + first_match_idx = idx; + if (nav_item_idx == idx) // Record that we encountering nav_item so we can return next match. + return_next_match = true; + } + return first_match_idx; // First result +} + +int ImGui::TypingSelectFindBestLeadingMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data) +{ + int longest_match_idx = -1; + int longest_match_len = 0; + for (int idx = 0; idx < items_count; idx++) + { + const char* item_name = get_item_name_func(user_data, idx); + const int match_len = ImStrimatchlen(req->SearchBuffer, req->SearchBuffer + req->SearchBufferLen, item_name); + if (match_len <= longest_match_len) + continue; + longest_match_idx = idx; + longest_match_len = match_len; + if (match_len == req->SearchBufferLen) + break; + } + return longest_match_idx; +} + +void ImGui::DebugNodeTypingSelectState(ImGuiTypingSelectState* data) +{ +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + Text("SearchBuffer = \"%s\"", data->SearchBuffer); + Text("SingleCharMode = %d, Size = %d, Lock = %d", data->Request.SingleCharMode, data->Request.SingleCharSize, data->SingleCharModeLock); + Text("LastRequest = time: %.2f, frame: %d", data->LastRequestTime, data->LastRequestFrame); +#else + IM_UNUSED(data); +#endif +} + + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Multi-Select support +//------------------------------------------------------------------------- + +void ImGui::SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data) +{ + // Note that flags will be cleared by ItemAdd(), so it's only useful for Navigation code! + // This designed so widgets can also cheaply set this before calling ItemAdd(), so we are not tied to MultiSelect api. + ImGuiContext& g = *GImGui; + g.NextItemData.ItemFlags |= ImGuiItemFlags_HasSelectionUserData; + g.NextItemData.SelectionUserData = selection_user_data; +} + + //------------------------------------------------------------------------- // [SECTION] Widgets: ListBox //------------------------------------------------------------------------- @@ -6582,6 +6858,7 @@ bool ImGui::Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags // - ListBox() //------------------------------------------------------------------------- +// This is essentially a thin wrapper to using BeginChild/EndChild with the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. // Tip: To have a list filling the entire window width, use size.x = -FLT_MIN and pass an non-visible label e.g. "##empty" // Tip: If your vertical size is calculated from an item count (e.g. 10 * item_height) consider adding a fractional part to facilitate seeing scrolling boundaries (e.g. 10.25 * item_height). bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) @@ -6597,7 +6874,7 @@ bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) // Size default to hold ~7.25 items. // Fractional number of items helps seeing that we can scroll down/up without looking at scrollbar. - ImVec2 size = ImFloor(CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.25f + style.FramePadding.y * 2.0f)); + ImVec2 size = ImTrunc(CalcItemSize(size_arg, CalcItemWidth(), GetTextLineHeightWithSpacing() * 7.25f + style.FramePadding.y * 2.0f)); ImVec2 frame_size = ImVec2(size.x, ImMax(size.y, label_size.y)); ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + frame_size); ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f)); @@ -6607,10 +6884,11 @@ bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) { ItemSize(bb.GetSize(), style.FramePadding.y); ItemAdd(bb, 0, &frame_bb); + g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values return false; } - // FIXME-OPT: We could omit the BeginGroup() if label_size.x but would need to omit the EndGroup() as well. + // FIXME-OPT: We could omit the BeginGroup() if label_size.x == 0.0f but would need to omit the EndGroup() as well. BeginGroup(); if (label_size.x > 0.0f) { @@ -6619,7 +6897,7 @@ bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, label_pos + label_size); } - BeginChildFrame(id, frame_bb.GetSize()); + BeginChild(id, frame_bb.GetSize(), ImGuiChildFlags_FrameStyle); return true; } @@ -6630,7 +6908,7 @@ void ImGui::EndListBox() IM_ASSERT((window->Flags & ImGuiWindowFlags_ChildWindow) && "Mismatched BeginListBox/EndListBox calls. Did you test the return value of BeginListBox?"); IM_UNUSED(window); - EndChildFrame(); + EndChild(); EndGroup(); // This is only required to be able to do IsItemXXX query on the whole ListBox including label } @@ -6642,7 +6920,7 @@ bool ImGui::ListBox(const char* label, int* current_item, const char* const item // This is merely a helper around BeginListBox(), EndListBox(). // Considering using those directly to submit custom data or store selection differently. -bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(void*, int, const char**), void* data, int items_count, int height_in_items) +bool ImGui::ListBox(const char* label, int* current_item, const char* (*getter)(void* user_data, int idx), void* user_data, int items_count, int height_in_items) { ImGuiContext& g = *GImGui; @@ -6650,7 +6928,7 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v if (height_in_items < 0) height_in_items = ImMin(items_count, 7); float height_in_items_f = height_in_items + 0.25f; - ImVec2 size(0.0f, ImFloor(GetTextLineHeightWithSpacing() * height_in_items_f + g.Style.FramePadding.y * 2.0f)); + ImVec2 size(0.0f, ImTrunc(GetTextLineHeightWithSpacing() * height_in_items_f + g.Style.FramePadding.y * 2.0f)); if (!BeginListBox(label, size)) return false; @@ -6663,8 +6941,8 @@ bool ImGui::ListBox(const char* label, int* current_item, bool (*items_getter)(v while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { - const char* item_text; - if (!items_getter(data, i, &item_text)) + const char* item_text = getter(user_data, i); + if (item_text == NULL) item_text = "*Unknown item*"; PushID(i); @@ -7011,12 +7289,18 @@ void ImGui::EndMenuBar() PopClipRect(); PopID(); window->DC.MenuBarOffset.x = window->DC.CursorPos.x - window->Pos.x; // Save horizontal position so next append can reuse it. This is kinda equivalent to a per-layer CursorPos. - g.GroupStack.back().EmitItem = false; - EndGroup(); // Restore position on layer 0 + + // FIXME: Extremely confusing, cleanup by (a) working on WorkRect stack system (b) not using a Group confusingly here. + ImGuiGroupData& group_data = g.GroupStack.back(); + group_data.EmitItem = false; + ImVec2 restore_cursor_max_pos = group_data.BackupCursorMaxPos; + window->DC.IdealMaxPos.x = ImMax(window->DC.IdealMaxPos.x, window->DC.CursorMaxPos.x - window->Scroll.x); // Convert ideal extents for scrolling layer equivalent. + EndGroup(); // Restore position on layer 0 // FIXME: Misleading to use a group for that backup/restore window->DC.LayoutType = ImGuiLayoutType_Vertical; window->DC.IsSameLine = false; window->DC.NavLayerCurrent = ImGuiNavLayer_Main; window->DC.MenuBarAppending = false; + window->DC.CursorMaxPos = restore_cursor_max_pos; } // Important: calling order matters! @@ -7172,15 +7456,15 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) // Menu inside an horizontal menu bar // Selectable extend their highlight by half ItemSpacing in each direction. // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() - popup_pos = ImVec2(pos.x - 1.0f - IM_FLOOR(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); - window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); + popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); + window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, label_size.y)); RenderText(text_pos, label); PopStyleVar(); - window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else { @@ -7189,7 +7473,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y); float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f; - float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); + float checkmark_w = IM_TRUNC(g.FontSize * 1.20f); float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); @@ -7217,18 +7501,18 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) ImGuiWindow* child_menu_window = (child_popup && child_popup->Window && child_popup->Window->ParentWindow == window) ? child_popup->Window : NULL; if (g.HoveredWindow == window && child_menu_window != NULL) { - float ref_unit = g.FontSize; // FIXME-DPI - float child_dir = (window->Pos.x < child_menu_window->Pos.x) ? 1.0f : -1.0f; - ImRect next_window_rect = child_menu_window->Rect(); + const float ref_unit = g.FontSize; // FIXME-DPI + const float child_dir = (window->Pos.x < child_menu_window->Pos.x) ? 1.0f : -1.0f; + const ImRect next_window_rect = child_menu_window->Rect(); ImVec2 ta = (g.IO.MousePos - g.IO.MouseDelta); ImVec2 tb = (child_dir > 0.0f) ? next_window_rect.GetTL() : next_window_rect.GetTR(); ImVec2 tc = (child_dir > 0.0f) ? next_window_rect.GetBL() : next_window_rect.GetBR(); - float extra = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, ref_unit * 0.5f, ref_unit * 2.5f); // add a bit of extra slack. + const float pad_farmost_h = ImClamp(ImFabs(ta.x - tb.x) * 0.30f, ref_unit * 0.5f, ref_unit * 2.5f); // Add a bit of extra slack. ta.x += child_dir * -0.5f; tb.x += child_dir * ref_unit; tc.x += child_dir * ref_unit; - tb.y = ta.y + ImMax((tb.y - extra) - ta.y, -ref_unit * 8.0f); // triangle has maximum height to limit the slope and the bias toward large sub-menus - tc.y = ta.y + ImMin((tc.y + extra) - ta.y, +ref_unit * 8.0f); + tb.y = ta.y + ImMax((tb.y - pad_farmost_h) - ta.y, -ref_unit * 8.0f); // Triangle has maximum height to limit the slope and the bias toward large sub-menus + tc.y = ta.y + ImMin((tc.y + pad_farmost_h) - ta.y, +ref_unit * 8.0f); moving_toward_child_menu = ImTriangleContainsPoint(ta, tb, tc, g.IO.MousePos); //GetForegroundDrawList()->AddTriangleFilled(ta, tb, tc, moving_toward_child_menu ? IM_COL32(0,128,0,128) : IM_COL32(128,0,0,128)); // [DEBUG] } @@ -7236,14 +7520,17 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) // The 'HovereWindow == window' check creates an inconsistency (e.g. moving away from menu slowly tends to hit same window, whereas moving away fast does not) // But we also need to not close the top-menu menu when moving over void. Perhaps we should extend the triangle check to a larger polygon. // (Remember to test this on BeginPopup("A")->BeginMenu("B") sequence which behaves slightly differently as B isn't a Child of A and hovering isn't shared.) - if (menu_is_open && !hovered && g.HoveredWindow == window && !moving_toward_child_menu && !g.NavDisableMouseHover) + if (menu_is_open && !hovered && g.HoveredWindow == window && !moving_toward_child_menu && !g.NavDisableMouseHover && g.ActiveId == 0) want_close = true; // Open + // (note: at this point 'hovered' actually includes the NavDisableMouseHover == false test) if (!menu_is_open && pressed) // Click/activate to open want_open = true; else if (!menu_is_open && hovered && !moving_toward_child_menu) // Hover to open want_open = true; + else if (!menu_is_open && hovered && g.HoveredIdTimer >= 0.30f && g.MouseStationaryTimer >= 0.30f) // Hover to open (timer fallback) + want_open = true; if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open { want_open = true; @@ -7366,14 +7653,14 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful // Note that in this situation: we don't render the shortcut, we render a highlight instead of the selected tick mark. float w = label_size.x; - window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f); + window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); pressed = Selectable("", selected, selectable_flags, ImVec2(w, 0.0f)); PopStyleVar(); if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) RenderText(text_pos, label); - window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). + window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar(). } else { @@ -7382,7 +7669,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system. float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f; float shortcut_w = (shortcut && shortcut[0]) ? CalcTextSize(shortcut, NULL).x : 0.0f; - float checkmark_w = IM_FLOOR(g.FontSize * 1.20f); + float checkmark_w = IM_TRUNC(g.FontSize * 1.20f); float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w); pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, label_size.y)); @@ -7527,6 +7814,8 @@ bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags) ImGuiTabBar* tab_bar = g.TabBars.GetOrAddByKey(id); ImRect tab_bar_bb = ImRect(window->DC.CursorPos.x, window->DC.CursorPos.y, window->WorkRect.Max.x, window->DC.CursorPos.y + g.FontSize + g.Style.FramePadding.y * 2); tab_bar->ID = id; + tab_bar->SeparatorMinX = tab_bar->BarRect.Min.x - IM_TRUNC(window->WindowPadding.x * 0.5f); + tab_bar->SeparatorMaxX = tab_bar->BarRect.Max.x + IM_TRUNC(window->WindowPadding.x * 0.5f); return BeginTabBarEx(tab_bar, tab_bar_bb, flags | ImGuiTabBarFlags_IsFocused); } @@ -7537,6 +7826,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG if (window->SkipItems) return false; + IM_ASSERT(tab_bar->ID != 0); if ((flags & ImGuiTabBarFlags_DockNode) == 0) PushOverrideID(tab_bar->ID); @@ -7579,12 +7869,12 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG window->DC.CursorPos = ImVec2(tab_bar->BarRect.Min.x, tab_bar->BarRect.Max.y + tab_bar->ItemSpacingY); // Draw separator + // (it would be misleading to draw this in EndTabBar() suggesting that it may be drawn over tabs, as tab bar are appendable) const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); - const float y = tab_bar->BarRect.Max.y - 1.0f; + if (g.Style.TabBarBorderSize > 0.0f) { - const float separator_min_x = tab_bar->BarRect.Min.x - IM_FLOOR(window->WindowPadding.x * 0.5f); - const float separator_max_x = tab_bar->BarRect.Max.x + IM_FLOOR(window->WindowPadding.x * 0.5f); - window->DrawList->AddLine(ImVec2(separator_min_x, y), ImVec2(separator_max_x, y), col, 1.0f); + const float y = tab_bar->BarRect.Max.y; + window->DrawList->AddRectFilled(ImVec2(tab_bar->SeparatorMinX, y - g.Style.TabBarBorderSize), ImVec2(tab_bar->SeparatorMaxX, y), col); } return true; } @@ -7791,7 +8081,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) for (int tab_n = shrink_data_offset; tab_n < shrink_data_offset + shrink_data_count; tab_n++) { ImGuiTabItem* tab = &tab_bar->Tabs[g.ShrinkWidthBuffer[tab_n].Index]; - float shrinked_width = IM_FLOOR(g.ShrinkWidthBuffer[tab_n].Width); + float shrinked_width = IM_TRUNC(g.ShrinkWidthBuffer[tab_n].Width); if (shrinked_width < 0.0f) continue; @@ -7951,7 +8241,7 @@ void ImGui::TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) if (tab->Flags & ImGuiTabItemFlags_Button) return; // A button appended with TabItemButton(). - if (!(tab->Flags & ImGuiTabItemFlags_UnsavedDocument)) + if ((tab->Flags & (ImGuiTabItemFlags_UnsavedDocument | ImGuiTabItemFlags_NoAssumedClosure)) == 0) { // This will remove a frame of lag for selecting another tab on closure. // However we don't run it in the case where the 'Unsaved' flag is set, so user gets a chance to fully undo the closure @@ -8378,7 +8668,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, const bool is_central_section = (tab->Flags & ImGuiTabItemFlags_SectionMask_) == 0; size.x = tab->Width; if (is_central_section) - window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_FLOOR(tab->Offset - tab_bar->ScrollingAnim), 0.0f); + window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(IM_TRUNC(tab->Offset - tab_bar->ScrollingAnim), 0.0f); else window->DC.CursorPos = tab_bar->BarRect.Min + ImVec2(tab->Offset, 0.0f); ImVec2 pos = window->DC.CursorPos; @@ -8432,7 +8722,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, if (hovered && g.HoveredIdNotActiveTimer > TOOLTIP_DELAY && bb.GetWidth() < tab->ContentWidth) { // Enlarge tab display when hovering - bb.Max.x = bb.Min.x + IM_FLOOR(ImLerp(bb.GetWidth(), tab->ContentWidth, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f))); + bb.Max.x = bb.Min.x + IM_TRUNC(ImLerp(bb.GetWidth(), tab->ContentWidth, ImSaturate((g.HoveredIdNotActiveTimer - 0.40f) * 6.0f))); display_draw_list = GetForegroundDrawList(window); TabItemBackground(display_draw_list, bb, flags, GetColorU32(ImGuiCol_TitleBgActive)); } @@ -8526,7 +8816,7 @@ void ImGui::TabItemBackground(ImDrawList* draw_list, const ImRect& bb, ImGuiTabI IM_ASSERT(width > 0.0f); const float rounding = ImMax(0.0f, ImMin((flags & ImGuiTabItemFlags_Button) ? g.Style.FrameRounding : g.Style.TabRounding, width * 0.5f - 1.0f)); const float y1 = bb.Min.y + 1.0f; - const float y2 = bb.Max.y - 1.0f; + const float y2 = bb.Max.y - g.Style.TabBarBorderSize; draw_list->PathLineTo(ImVec2(bb.Min.x, y2)); draw_list->PathArcToFast(ImVec2(bb.Min.x + rounding, y1 + rounding), rounding, 6, 9); draw_list->PathArcToFast(ImVec2(bb.Max.x - rounding, y1 + rounding), rounding, 9, 12); @@ -8577,7 +8867,7 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, } const float button_sz = g.FontSize; - const ImVec2 button_pos(ImMax(bb.Min.x, bb.Max.x - frame_padding.x * 2.0f - button_sz), bb.Min.y); + const ImVec2 button_pos(ImMax(bb.Min.x, bb.Max.x - frame_padding.x - button_sz), bb.Min.y + frame_padding.y); // Close Button & Unsaved Marker // We are relying on a subtle and confusing distinction between 'hovered' and 'g.HoveredId' which happens because we are using ImGuiButtonFlags_AllowOverlapMode + SetItemAllowOverlap() @@ -8595,10 +8885,8 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, if (close_button_visible) { ImGuiLastItemData last_item_backup = g.LastItemData; - PushStyleVar(ImGuiStyleVar_FramePadding, frame_padding); if (CloseButton(close_button_id, button_pos)) close_button_pressed = true; - PopStyleVar(); g.LastItemData = last_item_backup; // Close with middle mouse button @@ -8607,7 +8895,7 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, } else if (unsaved_marker_visible) { - const ImRect bullet_bb(button_pos, button_pos + ImVec2(button_sz, button_sz) + g.Style.FramePadding * 2.0f); + const ImRect bullet_bb(button_pos, button_pos + ImVec2(button_sz, button_sz)); RenderBullet(draw_list, bullet_bb.GetCenter(), GetColorU32(ImGuiCol_Text)); } From e266dba83eb406329104e05d519da4c489edd635 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 22 Jan 2024 16:36:33 +0100 Subject: [PATCH 0934/1018] che_xyz: loading colors and extra value as heatmap --- include/gproshan/mesh/che.h | 3 +++ include/gproshan/util.h | 19 +++++++++++++++++++ src/gproshan/mesh/che.cpp | 8 ++++++++ src/gproshan/mesh/che_xyz.cpp | 10 ++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 048c7deb..29a3a4ad 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -2,6 +2,7 @@ #define CHE_H #include +#include #include #include @@ -63,6 +64,7 @@ class che vertex * VN = nullptr; ///< vertex normals : v -> normal(v) rgb_t * VC = nullptr; ///< vertex color : v -> color(v) real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) + real_t scale_hm = 1; ///< vertex color heatmap scale factor bool manifold = true; @@ -85,6 +87,7 @@ class che // vertex color methods const real_t * heatmap_ptr() const; + real_t heatmap_scale(const index_t v) const; real_t heatmap(const index_t v) const; real_t & heatmap(const index_t v); const rgb_t * rgb_ptr() const; diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 9447db6f..5f2cae8e 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -83,6 +83,25 @@ void copy_real_t_array(double * destination, const float * source, const size_t void copy_real_t_array(double * destination, const double * source, const size_t n_elem); +template +T normalize(T * data, const size_t n_elem) +{ + T max = 0; + + #pragma omp parallel for reduction(std::max: max) + for(index_t i = 0; i < n_elem; ++i) + max = std::max(max, data[i]); + + if(max <= 1) return 1; + + #pragma omp parallel for + for(index_t i = 0; i < n_elem; ++i) + data[i] /= max; + + return max; +} + + } // namespace gproshan #endif // UTIL_H diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 60b3cafc..51c5b43e 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -169,6 +169,12 @@ const real_t * che::heatmap_ptr() const return VHC; } +real_t che::heatmap_scale(const index_t v) const +{ + assert(v < n_vertices); + return scale_hm * VHC[v]; +} + real_t che::heatmap(const index_t v) const { assert(v < n_vertices); @@ -338,6 +344,7 @@ void che::update_heatmap(const real_t * hm) } memcpy(VHC, hm, n_vertices * sizeof(real_t)); + scale_hm = normalize(VHC, n_vertices); } void che::update_normals() @@ -1162,6 +1169,7 @@ che * che::load_mesh(const std::string & file_path) if(extension == "ply") return new che_ply(file_path); if(extension == "ptx") return new che_ptx(file_path); if(extension == "xyz") return new che_xyz(file_path); + if(extension == "txt") return new che_xyz(file_path); if(extension == "pts") return new che_pts(file_path); if(extension == "pcd") return new che_pcd(file_path); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index c3d0117d..09589e0c 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -23,18 +23,21 @@ void che_xyz::read_file(const std::string & file) float x, y, z; unsigned char r, g, b; size_t n; + real_t h; std::vector vertices; std::vector vertices_color; + std::vector vertices_hm; while(fgets(line, sizeof(line), fp)) { sscanf(line, "%c", &c); if(c == '#') continue; - n = sscanf(line, "%f %f %f %hhu %hhu %hhu", &x, &y, &z, &r, &g, &b); + n = sscanf(line, "%f %f %f %hhu %hhu %hhu %f", &x, &y, &z, &r, &g, &b, &h); vertices.push_back({x, y, z}); - vertices_color.push_back(n == 6 ? rgb_t{r, g, b} : rgb_t()); + vertices_color.push_back(n > 5 ? rgb_t{r, g, b} : rgb_t()); + vertices_hm.push_back(n > 6 ? h : 0.45); } fclose(fp); @@ -42,6 +45,8 @@ void che_xyz::read_file(const std::string & file) alloc(size(vertices), 0); memcpy(GT, vertices.data(), n_vertices * sizeof(vertex)); memcpy(VC, vertices_color.data(), n_vertices * sizeof(rgb_t)); + + update_heatmap(vertices_hm.data()); } void che_xyz::write_file(const che * mesh, const std::string & file, const bool & color) @@ -60,6 +65,7 @@ void che_xyz::write_file(const che * mesh, const std::string & file, const bool { const rgb_t c = mesh->rgb(i); fprintf(fp, " %hhu %hhu %hhu", c.r, c.g, c.b); + fprintf(fp, " %f", mesh->heatmap_scale(i)); } fprintf(fp, "\n"); } From 1132f82ef659ddc52aa0b9a1592b60e8c3a3258f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jan 2024 01:14:15 +0100 Subject: [PATCH 0935/1018] raytracing: closes hit vertex utils function added --- include/gproshan/raytracing/utils.h | 25 +++++++++++++++++++++++++ src/gproshan/raytracing/embree.cpp | 21 +-------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 33c5a886..33c496fc 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -263,6 +263,31 @@ vec ray_view_dir( const uvec2 & pos, } +template +__host_device__ +index_t closest_hit_vertex(const CHE & mesh, const H & hit) +{ + if(!mesh.n_trigs) return hit.primID; + + index_t he = 0; + real_t w = 1 - hit.u - hit.v; + + if(w < hit.u) + { + he = 1; + w = hit.u; + } + + if(w < hit.v) + { + he = 2; + w = hit.v; + } + + return mesh.VT[hit.primID * 3 + he]; +} + + } // namespace gproshan #endif // RT_UTILS_H diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 6aa5b17a..ef17f346 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -87,26 +87,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) const ray_hit r(org, dir); if(!intersect(r)) return NIL; - const CHE * mesh = g_meshes[r.hit.geomID]; - - if(!mesh->n_trigs) return r.hit.primID; - - index_t he = che::mtrig * r.hit.primID; - float w = 1 - r.hit.u - r.hit.v; - - if(w < r.hit.u) - { - he = che::mtrig * r.hit.primID + 1; - w = r.hit.u; - } - - if(w < r.hit.v) - { - he = che::mtrig * r.hit.primID + 2; - w = r.hit.v; - } - - return mesh->VT[he]; + return closest_hit_vertex(*g_meshes[r.hit.geomID], r.hit); } eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & flat) const From 8e09bf2e52e593e81d1403b476fe4f0962daba31 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jan 2024 01:53:48 +0100 Subject: [PATCH 0936/1018] scenes: scanner save heatmap --- include/gproshan/mesh/che.h | 2 ++ include/gproshan/raytracing/utils.h | 5 ++++- include/gproshan/scenes/scanner.h | 2 +- src/gproshan/app_viewer.cpp | 9 ++++++++- src/gproshan/mesh/che.cpp | 6 ++++++ src/gproshan/mesh/che.cu | 4 ++++ src/gproshan/scenes/scanner.cpp | 4 ++-- 7 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 29a3a4ad..13ce9016 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -87,6 +87,7 @@ class che // vertex color methods const real_t * heatmap_ptr() const; + void heatmap_scale(const real_t shm); real_t heatmap_scale(const index_t v) const; real_t heatmap(const index_t v) const; real_t & heatmap(const index_t v); @@ -215,6 +216,7 @@ struct CHE vertex * GT = nullptr; vertex * VN = nullptr; che::rgb_t * VC = nullptr; + real_t * VHC = nullptr; index_t * VT = nullptr; index_t * OT = nullptr; index_t * EVT = nullptr; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 33c496fc..86dc9fb6 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -75,8 +75,9 @@ struct t_eval_hit { index_t primID = NIL; int illum = 1; - T dist = 0; T u = 0, v = 0; + T dist = 0; + T heatmap = 0; vec position; vec normal; vec Ka = 1; @@ -101,6 +102,7 @@ struct t_eval_hit Kd = {T(mesh.VC[aprimID].r), T(mesh.VC[aprimID].g), T(mesh.VC[aprimID].b)}; Kd /= 255; normal = mesh.VN[aprimID]; + heatmap = mesh.VHC[aprimID]; return; } @@ -116,6 +118,7 @@ struct t_eval_hit Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255; normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; + heatmap = (1.f - u - v) * mesh.VHC[a] + u * mesh.VHC[b] + v * mesh.VHC[c]; if(!sc.trig_mat) return; if(sc.trig_mat[primID] == NIL) return; diff --git a/include/gproshan/scenes/scanner.h b/include/gproshan/scenes/scanner.h index 2de2ff25..be746751 100644 --- a/include/gproshan/scenes/scanner.h +++ b/include/gproshan/scenes/scanner.h @@ -9,7 +9,7 @@ namespace gproshan { -che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos); +che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const bool dist_error = false); che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg = ""); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 7539500a..76001140 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -193,6 +193,8 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) static const size_t n_min = 100; static const size_t n_max = 10000; static vertex cam_pos = {0, 0, 0}; + static bool dist_error = true; + static bool save_jpg = false; if(size(view->sphere_points) != 1) { @@ -209,9 +211,14 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) view->sphere_points[0] = cam_pos; } + ImGui::Checkbox("dist_error", &dist_error); + ImGui::Checkbox("save_jpg", &save_jpg); + if(ImGui::Button("Scan")) { - che * ptx_mesh = scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename); + che * ptx_mesh = save_jpg ? scanner_ptx_jpg(mesh.rt_embree, n_rows, n_cols, cam_pos, mesh->filename) + : scanner_ptx(mesh.rt_embree, n_rows, n_cols, cam_pos, dist_error); + che_ptx::write_file(ptx_mesh, mesh->filename, n_rows, n_cols); ptx_mesh->filename = mesh->filename + ".ptx"; view->add_mesh(ptx_mesh); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 51c5b43e..465e8bc2 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -169,6 +169,11 @@ const real_t * che::heatmap_ptr() const return VHC; } +void che::heatmap_scale(const real_t shm) +{ + scale_hm = shm; +} + real_t che::heatmap_scale(const index_t v) const { assert(v < n_vertices); @@ -1219,6 +1224,7 @@ CHE::CHE(const che * mesh) GT = mesh->GT; VN = mesh->VN; VC = mesh->VC; + VHC = mesh->VHC; VT = mesh->VT; OT = mesh->OT; EVT = mesh->EVT; diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 592cc91a..4970c678 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -25,6 +25,9 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & norm { cudaMalloc(&dd_che->VC, sizeof(che::rgb_t) * h_che->n_vertices); cudaMemcpy(dd_che->VC, h_che->VC, sizeof(che::rgb_t) * h_che->n_vertices, cudaMemcpyHostToDevice); + + cudaMalloc(&dd_che->VHC, sizeof(real_t) * h_che->n_vertices); + cudaMemcpy(dd_che->VHC, h_che->VHC, sizeof(real_t) * h_che->n_vertices, cudaMemcpyHostToDevice); } cudaMalloc(&dd_che->VT, sizeof(index_t) * h_che->n_half_edges); @@ -45,6 +48,7 @@ void cuda_free_CHE(CHE *& dd_che, CHE *& d_che) cudaFree(dd_che->GT); cudaFree(dd_che->VN); cudaFree(dd_che->VC); + cudaFree(dd_che->VHC); cudaFree(dd_che->VT); cudaFree(dd_che->OT); cudaFree(dd_che->EVT); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 7b59c9e9..efa6f5a2 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -19,7 +19,7 @@ using namespace cimg_library; namespace gproshan { -che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos) +che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const bool dist_error) { che * mesh_ptx = new che(n_cols * n_rows); @@ -40,7 +40,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n mesh_ptx->point(v) = h.position; mesh_ptx->normal(v) = h.normal; - mesh_ptx->heatmap(v) = h.dist; + mesh_ptx->heatmap(v) = dist_error ? h.dist : h.heatmap; mesh_ptx->rgb(v) = h.Kd; } From 8dcd09d7df69e7fd2214cd553cdcfe8c2260da3b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jan 2024 13:45:54 +0100 Subject: [PATCH 0937/1018] che: accessing heatmap_scale member --- include/gproshan/mesh/che.h | 1 + src/gproshan/mesh/che.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 13ce9016..7ac7de82 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -87,6 +87,7 @@ class che // vertex color methods const real_t * heatmap_ptr() const; + real_t heatmap_scale() const; void heatmap_scale(const real_t shm); real_t heatmap_scale(const index_t v) const; real_t heatmap(const index_t v) const; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 465e8bc2..f1378dfc 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -169,6 +169,11 @@ const real_t * che::heatmap_ptr() const return VHC; } +real_t che::heatmap_scale() const +{ + return scale_hm; +} + void che::heatmap_scale(const real_t shm) { scale_hm = shm; From 29bb9e9e3bf0d5375ab9f90f0392b74cb41ce9a2 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jan 2024 02:34:24 +0100 Subject: [PATCH 0938/1018] rt_optix: using payload for accumulating hit distant --- src/gproshan/raytracing/optix.cpp | 4 ++-- src/gproshan/raytracing/optix.cu | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 9e079557..300d6555 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -61,8 +61,8 @@ optix::optix(const std::string & ptx) optix_pipeline_compile_opt = {}; optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; optix_pipeline_compile_opt.usesMotionBlur = false; - optix_pipeline_compile_opt.numPayloadValues = 3; - optix_pipeline_compile_opt.numAttributeValues = 3; + optix_pipeline_compile_opt.numPayloadValues = 4; + optix_pipeline_compile_opt.numAttributeValues = 4; optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_params"; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index fdb44caa..5eb5fa76 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -136,6 +136,7 @@ extern "C" __global__ void __raygen__render_frame() vec3 & ray_dir = trace[3]; uint32_t u0, u1; + unsigned int dist; do { @@ -144,6 +145,8 @@ extern "C" __global__ void __raygen__render_frame() position = optix_params.cam_pos; ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); + dist = 0; + depth = optix_params.depth; do { @@ -159,7 +162,7 @@ extern "C" __global__ void __raygen__render_frame() 0, // SBT offset 2, // SBT stride 0, // missSBTIndex - u0, u1, (unsigned int &) rnd); + u0, u1, (unsigned int &) rnd, dist); if(!u0) break; // miss color_acc += color; From 8efb1d180a62088559e364cdc3ca2fe9dfd5feee Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 30 Jan 2024 17:54:36 +0100 Subject: [PATCH 0939/1018] rt_embree: accumulating hit distant variable and minor updates --- include/gproshan/geodesics/geodesics_ptp.h | 4 ++-- include/gproshan/mdict/basis.h | 2 +- include/gproshan/mdict/basis_dct.h | 2 +- include/gproshan/mdict/msparse_coding.h | 2 +- include/gproshan/mesh/che.cuh | 2 +- include/gproshan/mesh/che_fill_hole.h | 8 ++++---- include/gproshan/mesh/che_obj.h | 2 +- include/gproshan/mesh/che_off.h | 2 +- include/gproshan/mesh/che_ply.h | 2 +- include/gproshan/mesh/che_xyz.h | 2 +- include/gproshan/raytracing/embree.h | 5 +++-- include/gproshan/raytracing/optix.h | 2 +- include/gproshan/raytracing/raytracing.h | 7 ++++--- include/gproshan/viewer/camera.h | 2 +- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/geodesics/geodesics_ptp.cpp | 2 +- src/gproshan/geodesics/geodesics_ptp.cu | 2 +- src/gproshan/mdict/basis_dct.cpp | 2 +- src/gproshan/mdict/msparse_coding.cpp | 2 +- src/gproshan/mesh/che.cu | 2 +- src/gproshan/mesh/che_obj.cpp | 2 +- src/gproshan/mesh/che_off.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 2 +- src/gproshan/mesh/che_xyz.cpp | 2 +- src/gproshan/raytracing/embree.cpp | 5 +++-- src/gproshan/raytracing/optix.cpp | 2 +- src/gproshan/raytracing/raytracing.cpp | 8 +++++--- src/gproshan/viewer/camera.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 2 +- 29 files changed, 44 insertions(+), 39 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index cabdd949..dbc267b0 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -61,9 +61,9 @@ struct toplesets_t const index_t * index; }; -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = true, const bool & set_inf = true); +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence = true, const bool set_inf = true); -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence = false, const bool & set_inf = true); +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence = false, const bool set_inf = true); real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 9cf73b06..4031f714 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -22,7 +22,7 @@ class basis basis(const real_t r, const size_t d); virtual ~basis() = default; virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; - virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) = 0; + virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b) = 0; virtual real_t freq(const index_t idx) = 0; real_t & radio(); size_t dim() const; diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 338c7b4e..1c08b12c 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -17,7 +17,7 @@ class basis_dct: public basis public: basis_dct(const size_t n, const real_t r = 1); void discrete(a_mat & phi, const a_vec & x, const a_vec & y); - void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b); + void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b); real_t freq(const index_t idx); private: diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index e39bcc6f..2e98c936 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -88,7 +88,7 @@ class msparse_coding void sparse_coding(); void init_sampling(); void load_features(std::vector & v_feat, size_t & featsize); - void init_patches( const bool & reset = 1, + void init_patches( const bool reset = 1, const fmask_t & mask = nullptr ); diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index 4a961477..2d724cff 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -8,7 +8,7 @@ namespace gproshan { -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal = false, const bool & color = false); +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool normal = false, const bool color = false); void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index b4c0a294..8c9063ee 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -18,7 +18,7 @@ struct border_t border_t() = default; - border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool & o): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o): v(_v) { index_t p_v = neighbors[!o]; @@ -38,7 +38,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool & o) + a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -56,7 +56,7 @@ struct border_t return r; } - border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool & o, const a_vec & normal): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o, const a_vec & normal): v(_v) { index_t p_v = neighbors[!o]; @@ -81,7 +81,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool & o, const a_vec & normal) + a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o, const a_vec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; diff --git a/include/gproshan/mesh/che_obj.h b/include/gproshan/mesh/che_obj.h index 008e0d74..d9076269 100644 --- a/include/gproshan/mesh/che_obj.h +++ b/include/gproshan/mesh/che_obj.h @@ -15,7 +15,7 @@ class che_obj : public che public: che_obj(const std::string & file); - static void write_file(const che * mesh, const std::string & file, const bool & color = false, const bool & pointcloud = false); + static void write_file(const che * mesh, const std::string & file, const bool color = false, const bool pointcloud = false); private: void read_file(const std::string & file); diff --git a/include/gproshan/mesh/che_off.h b/include/gproshan/mesh/che_off.h index 56fe2882..12988c83 100644 --- a/include/gproshan/mesh/che_off.h +++ b/include/gproshan/mesh/che_off.h @@ -14,7 +14,7 @@ class che_off : public che che_off(const std::string & file); enum type { OFF, NOFF, COFF, NCOFF }; - static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool & pointcloud = false); + static void write_file(const che * mesh, const std::string & file, const che_off::type & off = OFF, const bool pointcloud = false); private: void read_file(const std::string & file); diff --git a/include/gproshan/mesh/che_ply.h b/include/gproshan/mesh/che_ply.h index f5161991..621330c4 100644 --- a/include/gproshan/mesh/che_ply.h +++ b/include/gproshan/mesh/che_ply.h @@ -13,7 +13,7 @@ class che_ply : public che public: che_ply(const std::string & file); - static void write_file(const che * mesh, const std::string & file, const bool & color = false); + static void write_file(const che * mesh, const std::string & file, const bool color = false); private: void read_file(const std::string & file); diff --git a/include/gproshan/mesh/che_xyz.h b/include/gproshan/mesh/che_xyz.h index f05eda51..174bd002 100644 --- a/include/gproshan/mesh/che_xyz.h +++ b/include/gproshan/mesh/che_xyz.h @@ -13,7 +13,7 @@ class che_xyz : public che public: che_xyz(const std::string & file); - static void write_file(const che * mesh, const std::string & file, const bool & color = false); + static void write_file(const che * mesh, const std::string & file, const bool color = false); private: void read_file(const std::string & file); diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index c1333141..8b01b240 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -58,7 +58,7 @@ class embree : public raytracing virtual ~embree(); virtual index_t closest_vertex(const vertex & org, const vertex & dir) const; - virtual eval_hit intersect(const vertex & org, const vertex & dir, const bool & flat = true) const; + virtual eval_hit intersect(const vertex & org, const vertex & dir, const bool flat = true) const; protected: @@ -76,9 +76,10 @@ class embree : public raytracing vertex & attenuation, vertex & position, vertex & ray_dir, + real_t & dist, random & rnd, const render_params & params, - const bool & flat + const bool flat ) const; float intersect_depth(const vertex & org, const vertex & dir) const; diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index dc0afed5..d8d88b67 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -58,7 +58,7 @@ class optix : public raytracing optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); - void render(vec4 * img, const render_params & params, const bool & flat); + void render(vec4 * img, const render_params & params, const bool flat); protected: void create_raygen_programs(); diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 1db70db9..226248f3 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -18,7 +18,7 @@ class raytracing raytracing() = default; virtual ~raytracing() = default; - virtual void render(vec4 * img, const render_params & params, const bool & flat); + virtual void render(vec4 * img, const render_params & params, const bool flat); virtual std::vector raycaster( const uvec2 & windows_size, const mat4 & inv_proj_view, @@ -28,7 +28,7 @@ class raytracing virtual eval_hit intersect( const vertex &, // org const vertex &, //dir - [[maybe_unused]] const bool & flat = true + [[maybe_unused]] const bool flat = true ) const { return {}; } virtual index_t closest_vertex( const vertex &, // org, @@ -40,9 +40,10 @@ class raytracing vertex &, // attenuation, vertex &, // position, vertex &, // ray_dir, + real_t &, // dist random &, // rnd, const render_params &, // params, - const bool & // flat + const bool // flat ) const { return false; }; virtual float intersect_depth( const vertex &, // org, diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 86948f85..7ea0780e 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -33,7 +33,7 @@ class camera mat4 perspective(); mat4 look_at(const quaternion & r); quaternion current_rotation() const; - void mouse(const bool & press, const double x, const double y, const int w, const int h); + void mouse(const bool press, const double x, const double y, const int w, const int h); void motion(const double x, const double y, const int w, const int h); void zoom_in(); void zoom_out(); diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index b78aea00..7d161fe4 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -110,7 +110,7 @@ class viewer che_viewer & selected_mesh(); void add_menu(const std::string & str, const std::vector & vprocesses); int add_process(const char * name, const function_t & f, const int key = -1); - bool add_mesh(che * p_mesh, const bool & reset_normals = true); + bool add_mesh(che * p_mesh, const bool reset_normals = true); bool remove_mesh(const index_t idx); bool pop_mesh(); void update_viewport_meshes(); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index f4568ea7..9abcea88 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -12,7 +12,7 @@ namespace gproshan { ptp_out_t::ptp_out_t(real_t * d, index_t * c): dist(d), clusters(c) {} -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence, const bool set_inf) { CHE h_mesh(mesh); const size_t n_vertices = h_mesh.n_vertices; diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index c6285bc2..17f7f536 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -12,7 +12,7 @@ namespace gproshan { -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool & coalescence, const bool & set_inf) +double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence, const bool set_inf) { CHE h_mesh(mesh); const size_t n_vertices = h_mesh.n_vertices; diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 5a5e04a3..6bcf8d42 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -19,7 +19,7 @@ void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) phi.col(k) = dct(x, y, nx, ny); } -void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool & b) +void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b) { assert(phi.n_cols == _dim); diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 69e99612..6f6ca0a1 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -785,7 +785,7 @@ void msparse_coding::load_features(std::vector & v_feat, size_t & feats inp.close(); } -void msparse_coding::init_patches(const bool & reset, const fmask_t & mask) +void msparse_coding::init_patches(const bool reset, const fmask_t & mask) { gproshan_log(MDICT); diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 4970c678..812af1e4 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -5,7 +5,7 @@ namespace gproshan { -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool & normal, const bool & color) +void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool normal, const bool color) { dd_che = new CHE; dd_che->n_vertices = h_che->n_vertices; diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index c525cb2c..407f90f4 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -26,7 +26,7 @@ void che_obj::read_file(const std::string & file) VT[i] = p.trigs[i].x(); } -void che_obj::write_file(const che * mesh, const std::string & file, const bool & color, const bool & pointcloud) +void che_obj::write_file(const che * mesh, const std::string & file, const bool color, const bool pointcloud) { FILE * fp = fopen((file + ".obj").c_str(), "w"); assert(fp); diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 0e60d090..365c692e 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -81,7 +81,7 @@ void che_off::read_file(const std::string & file) memcpy(VT, trigs.data(), size(trigs) * sizeof(index_t)); } -void che_off::write_file(const che * mesh, const std::string & file, const che_off::type & off, const bool & pointcloud) +void che_off::write_file(const che * mesh, const std::string & file, const che_off::type & off, const bool pointcloud) { static const char * str_off[] = {"OFF", "NOFF", "COFF", "NCOFF"}; diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 043d5269..57d0d0ae 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -252,7 +252,7 @@ void che_ply::read_file(const std::string & file) memcpy(VT, trigs.data(), size(trigs) * sizeof(index_t)); } -void che_ply::write_file(const che * mesh, const std::string & file, const bool & color) +void che_ply::write_file(const che * mesh, const std::string & file, const bool color) { FILE * fp = fopen((file + ".ply").c_str(), "wb"); assert(fp); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index 09589e0c..ab57fc63 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -49,7 +49,7 @@ void che_xyz::read_file(const std::string & file) update_heatmap(vertices_hm.data()); } -void che_xyz::write_file(const che * mesh, const std::string & file, const bool & color) +void che_xyz::write_file(const che * mesh, const std::string & file, const bool color) { FILE * fp = fopen((file + ".xyz").c_str(), "w"); assert(fp); diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index ef17f346..cd4a88c3 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -90,7 +90,7 @@ index_t embree::closest_vertex(const vertex & org, const vertex & dir) const return closest_hit_vertex(*g_meshes[r.hit.geomID], r.hit); } -eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool & flat) const +eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool flat) const { ray_hit r(org, dir); if(!intersect(r)) return {}; @@ -250,9 +250,10 @@ bool embree::closesthit_radiance( vertex & color, vertex & attenuation, vertex & position, vertex & ray_dir, + float &, random & rnd, const render_params & params, - const bool & flat + const bool flat ) const { ray_hit r(position, ray_dir); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 300d6555..23ef4e26 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -123,7 +123,7 @@ optix::~optix() cudaFree(data); } -void optix::render(vec4 * img, const render_params & params, const bool & flat) +void optix::render(vec4 * img, const render_params & params, const bool flat) { optix_params.depth = params.depth; optix_params.n_frames = params.n_frames; diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index d1b9ff67..07f5138d 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -7,7 +7,7 @@ namespace gproshan::rt { -void raytracing::render(vec4 * img, const render_params & params, const bool & flat) +void raytracing::render(vec4 * img, const render_params & params, const bool flat) { uvec2 window_size = params.window_size; if(params.viewport_is_window) @@ -17,8 +17,9 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f unsigned int samples = params.n_samples; vec3 color_acc, color, attenuation, position, ray_dir; + float dist; - #pragma omp parallel for private(depth, samples, color_acc, color, attenuation, position, ray_dir) + #pragma omp parallel for private(depth, samples, color_acc, color, attenuation, position, ray_dir, dist) for(unsigned int i = 0; i < params.viewport_size.x(); ++i) for(unsigned int j = 0; j < params.viewport_size.y(); ++j) { @@ -35,11 +36,12 @@ void raytracing::render(vec4 * img, const render_params & params, const bool & f attenuation = 1; position = params.cam_pos; ray_dir = ray_view_dir(pos, window_size, params.inv_proj_view, params.cam_pos, rnd); + dist = 0; depth = params.depth; do { - if(!closesthit_radiance(color, attenuation, position, ray_dir, rnd, params, flat)) + if(!closesthit_radiance(color, attenuation, position, ray_dir, dist, rnd, params, flat)) break; color_acc += color; diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 3f059a73..d399f88b 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -67,7 +67,7 @@ quaternion camera::current_rotation() const return p_drag * p_click.conj() * r_last; } -void camera::mouse(const bool & press, const double x, const double y, const int w, const int h) +void camera::mouse(const bool press, const double x, const double y, const int w, const int h) { if(press) { diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 55474de2..1f5511f5 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -545,7 +545,7 @@ int viewer::add_process(const char * name, const function_t & f, const int key) return fkey; } -bool viewer::add_mesh(che * p_mesh, const bool & reset_normals) +bool viewer::add_mesh(che * p_mesh, const bool reset_normals) { if(size(meshes) == max_meshes) return false; From 32f3eba0c1a0d5d6bfe696d5ab03c299ba4c84aa Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 26 Feb 2024 21:10:30 +0100 Subject: [PATCH 0940/1018] updated imgui v1.90.4 --- include/imgui/imgui.h | 41 +- include/imgui/imgui_impl_opengl3_loader.h | 2 + include/imgui/imgui_internal.h | 289 +++++---- src/imgui/imgui.cpp | 741 ++++++++++++++-------- src/imgui/imgui_demo.cpp | 58 +- src/imgui/imgui_draw.cpp | 12 +- src/imgui/imgui_impl_glfw.cpp | 4 +- src/imgui/imgui_impl_opengl3.cpp | 44 +- src/imgui/imgui_tables.cpp | 80 ++- src/imgui/imgui_widgets.cpp | 99 ++- 10 files changed, 890 insertions(+), 480 deletions(-) diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index 0ec19776..386fb583 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (headers) // Help: @@ -23,8 +23,8 @@ // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') -#define IMGUI_VERSION "1.90.1" -#define IMGUI_VERSION_NUM 19010 +#define IMGUI_VERSION "1.90.4" +#define IMGUI_VERSION_NUM 19040 #define IMGUI_HAS_TABLE /* @@ -89,6 +89,8 @@ Index of this file: #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. +// (MSVC provides an equivalent mechanism via SAL Annotations but it would require the macros in a different +// location. e.g. #include + void myprintf(_Printf_format_string_ const char* format, ...)) #if !defined(IMGUI_USE_STB_SPRINTF) && defined(__MINGW32__) && !defined(__clang__) #define IM_FMTARGS(FMT) __attribute__((format(gnu_printf, FMT, FMT+1))) #define IM_FMTLIST(FMT) __attribute__((format(gnu_printf, FMT, 0))) @@ -342,7 +344,7 @@ namespace ImGui // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. // - Before 1.90 (November 2023), the "ImGuiChildFlags child_flags = 0" parameter was "bool border = false". // This API is backward compatible with old code, as we guarantee that ImGuiChildFlags_Border == true. - // Consider updating your old call sites: + // Consider updating your old code: // BeginChild("Name", size, false) -> Begin("Name", size, 0); or Begin("Name", size, ImGuiChildFlags_None); // BeginChild("Name", size, true) -> Begin("Name", size, ImGuiChildFlags_Border); // - Manual sizing (each axis can use a different setting e.g. ImVec2(0.0f, 400.0f)): @@ -443,7 +445,7 @@ namespace ImGui IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier, packed as a 32-bit value suitable for ImDrawList IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList - IMGUI_API ImU32 GetColorU32(ImU32 col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList + IMGUI_API ImU32 GetColorU32(ImU32 col, float alpha_mul = 1.0f); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList IMGUI_API const ImVec4& GetStyleColorVec4(ImGuiCol idx); // retrieve style color as stored in ImGuiStyle structure. use to feed back into PushStyleColor(), otherwise use GetColorU32() to get style color with style alpha baked in. // Layout cursor positioning @@ -965,6 +967,7 @@ namespace ImGui // - Your main debugging friend is the ShowMetricsWindow() function, which is also accessible from Demo->Tools->Metrics Debugger IMGUI_API void DebugTextEncoding(const char* text); IMGUI_API void DebugFlashStyleColor(ImGuiCol idx); + IMGUI_API void DebugStartItemPicker(); IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. // Memory Allocators @@ -1025,7 +1028,7 @@ enum ImGuiWindowFlags_ }; // Flags for ImGui::BeginChild() -// (Legacy: bot 0 must always correspond to ImGuiChildFlags_Border to be backward compatible with old API using 'bool border = false'. +// (Legacy: bit 0 must always correspond to ImGuiChildFlags_Border to be backward compatible with old API using 'bool border = false'. // About using AutoResizeX/AutoResizeY flags: // - May be combined with SetNextWindowSizeConstraints() to set a min/max size for each axis (see "Demo->Child->Auto-resize with Constraints"). // - Size measurement for a given axis is only performed when the child window is within visible boundaries, or is just appearing. @@ -1036,7 +1039,7 @@ enum ImGuiWindowFlags_ enum ImGuiChildFlags_ { ImGuiChildFlags_None = 0, - ImGuiChildFlags_Border = 1 << 0, // Show an outer border and enable WindowPadding. (Important: this is always == 1 == true for legacy reason) + ImGuiChildFlags_Border = 1 << 0, // Show an outer border and enable WindowPadding. (IMPORTANT: this is always == 1 == true for legacy reason) ImGuiChildFlags_AlwaysUseWindowPadding = 1 << 1, // Pad with style.WindowPadding even if no border are drawn (no padding by default for non-bordered child windows because it makes more sense) ImGuiChildFlags_ResizeX = 1 << 2, // Allow resize from right border (layout direction). Enable .ini saving (unless ImGuiWindowFlags_NoSavedSettings passed to window flags) ImGuiChildFlags_ResizeY = 1 << 3, // Allow resize from bottom border (layout direction). " @@ -1105,8 +1108,8 @@ enum ImGuiTreeNodeFlags_ }; // Flags for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() functions. -// - To be backward compatible with older API which took an 'int mouse_button = 1' argument, we need to treat -// small flags values as a mouse button index, so we encode the mouse button in the first few bits of the flags. +// - To be backward compatible with older API which took an 'int mouse_button = 1' argument instead of 'ImGuiPopupFlags flags', +// we need to treat small flags values as a mouse button index, so we encode the mouse button in the first few bits of the flags. // It is therefore guaranteed to be legal to pass a mouse button index in ImGuiPopupFlags. // - For the same reason, we exceptionally default the ImGuiPopupFlags argument of BeginPopupContextXXX functions to 1 instead of 0. // IMPORTANT: because the default parameter is 1 (==ImGuiPopupFlags_MouseButtonRight), if you rely on the default parameter @@ -1120,10 +1123,12 @@ enum ImGuiPopupFlags_ ImGuiPopupFlags_MouseButtonMiddle = 2, // For BeginPopupContext*(): open on Middle Mouse release. Guaranteed to always be == 2 (same as ImGuiMouseButton_Middle) ImGuiPopupFlags_MouseButtonMask_ = 0x1F, ImGuiPopupFlags_MouseButtonDefault_ = 1, - ImGuiPopupFlags_NoOpenOverExistingPopup = 1 << 5, // For OpenPopup*(), BeginPopupContext*(): don't open if there's already a popup at the same level of the popup stack - ImGuiPopupFlags_NoOpenOverItems = 1 << 6, // For BeginPopupContextWindow(): don't return true when hovering items, only when hovering empty space - ImGuiPopupFlags_AnyPopupId = 1 << 7, // For IsPopupOpen(): ignore the ImGuiID parameter and test for any popup. - ImGuiPopupFlags_AnyPopupLevel = 1 << 8, // For IsPopupOpen(): search/test at any level of the popup stack (default test in the current level) + ImGuiPopupFlags_NoReopen = 1 << 5, // For OpenPopup*(), BeginPopupContext*(): don't reopen same popup if already open (won't reposition, won't reinitialize navigation) + //ImGuiPopupFlags_NoReopenAlwaysNavInit = 1 << 6, // For OpenPopup*(), BeginPopupContext*(): focus and initialize navigation even when not reopening. + ImGuiPopupFlags_NoOpenOverExistingPopup = 1 << 7, // For OpenPopup*(), BeginPopupContext*(): don't open if there's already a popup at the same level of the popup stack + ImGuiPopupFlags_NoOpenOverItems = 1 << 8, // For BeginPopupContextWindow(): don't return true when hovering items, only when hovering empty space + ImGuiPopupFlags_AnyPopupId = 1 << 10, // For IsPopupOpen(): ignore the ImGuiID parameter and test for any popup. + ImGuiPopupFlags_AnyPopupLevel = 1 << 11, // For IsPopupOpen(): search/test at any level of the popup stack (default test in the current level) ImGuiPopupFlags_AnyPopup = ImGuiPopupFlags_AnyPopupId | ImGuiPopupFlags_AnyPopupLevel, }; @@ -2187,11 +2192,7 @@ struct ImGuiIO int KeyMap[ImGuiKey_COUNT]; // [LEGACY] Input: map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. The first 512 are now unused and should be kept zero. Legacy backend will write into KeyMap[] using ImGuiKey_ indices which are always >512. bool KeysDown[ImGuiKey_COUNT]; // [LEGACY] Input: Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). This used to be [512] sized. It is now ImGuiKey_COUNT to allow legacy io.KeysDown[GetKeyIndex(...)] to work without an overflow. float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. -#endif -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - void* ImeWindowHandle; // = NULL // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. -#else - void* _UnusedPadding; + //void* ImeWindowHandle; // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. #endif //------------------------------------------------------------------ @@ -2765,7 +2766,8 @@ struct ImDrawList IMGUI_API void AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_min, const ImVec2& p_max, const ImVec2& uv_min, const ImVec2& uv_max, ImU32 col, float rounding, ImDrawFlags flags = 0); // Stateful path API, add points then finish with PathFillConvex() or PathStroke() - // - Filled shapes must always use clockwise winding order. The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. + // - Important: filled shapes must always use clockwise winding order! The anti-aliasing fringe depends on it. Counter-clockwise shapes will have "inward" anti-aliasing. + // so e.g. 'PathArcTo(center, radius, PI * -0.5f, PI)' is ok, whereas 'PathArcTo(center, radius, PI, PI * -0.5f)' won't have correct anti-aliasing when followed by PathFillConvex(). inline void PathClear() { _Path.Size = 0; } inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size - 1], &pos, 8) != 0) _Path.push_back(pos); } @@ -3123,6 +3125,7 @@ enum ImGuiViewportFlags_ // - Windows are generally trying to stay within the Work Area of their host viewport. struct ImGuiViewport { + ImGuiID ID; // Unique identifier for the viewport ImGuiViewportFlags Flags; // See ImGuiViewportFlags_ ImVec2 Pos; // Main Area: Position of the viewport (Dear ImGui coordinates are the same as OS desktop/native coordinates) ImVec2 Size; // Main Area: Size of the viewport. diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 85c58c4e..4019f937 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -260,6 +260,8 @@ typedef khronos_intptr_t GLintptr; #define GL_ARRAY_BUFFER_BINDING 0x8894 #define GL_ELEMENT_ARRAY_BUFFER_BINDING 0x8895 #define GL_STREAM_DRAW 0x88E0 +#define GL_PIXEL_UNPACK_BUFFER 0x88EC +#define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index 1a55e171..4ac8b1c9 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (internal structures/api) // You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. @@ -15,6 +15,8 @@ Index of this file: // [SECTION] Generic helpers // [SECTION] ImDrawList support // [SECTION] Widgets support: flags, enums, data structures +// [SECTION] Data types support +// [SECTION] Popup support // [SECTION] Inputs support // [SECTION] Clipper support // [SECTION] Navigation support @@ -234,6 +236,7 @@ namespace ImStb #define IMGUI_DEBUG_LOG_SELECTION(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_CLIPPER(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventClipper) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_IO(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventIO) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) +#define IMGUI_DEBUG_LOG_INPUTROUTING(...) do{if (g.DebugLogFlags & ImGuiDebugLogFlags_EventInputRouting)IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) // Static Asserts #define IM_STATIC_ASSERT(_COND) static_assert(_COND, "") @@ -297,11 +300,11 @@ namespace ImStb #elif defined(__clang__) #define IM_DEBUG_BREAK() __builtin_debugtrap() #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) -#define IM_DEBUG_BREAK() __asm__ volatile("int $0x03") +#define IM_DEBUG_BREAK() __asm__ volatile("int3;nop") #elif defined(__GNUC__) && defined(__thumb__) #define IM_DEBUG_BREAK() __asm__ volatile(".inst 0xde01") #elif defined(__GNUC__) && defined(__arm__) && !defined(__thumb__) -#define IM_DEBUG_BREAK() __asm__ volatile(".inst 0xe7f001f0"); +#define IM_DEBUG_BREAK() __asm__ volatile(".inst 0xe7f001f0") #else #define IM_DEBUG_BREAK() IM_ASSERT(0) // It is expected that you define IM_DEBUG_BREAK() into something that will break nicely in a debugger! #endif @@ -689,9 +692,6 @@ struct ImPool int GetBufSize() const { return Buf.Size; } int GetMapSize() const { return Map.Data.Size; } // It is the map we need iterate to find valid items, since we don't have "alive" storage anywhere T* TryGetMapData(ImPoolIdx n) { int idx = Map.Data[n].val_i; if (idx == -1) return NULL; return GetByIndex(idx); } -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - int GetSize() { return GetMapSize(); } // For ImPlot: should use GetMapSize() from (IMGUI_VERSION_NUM >= 18304) -#endif }; // Helper: ImChunkStream<> @@ -985,43 +985,6 @@ enum ImGuiPlotType ImGuiPlotType_Histogram, }; -enum ImGuiPopupPositionPolicy -{ - ImGuiPopupPositionPolicy_Default, - ImGuiPopupPositionPolicy_ComboBox, - ImGuiPopupPositionPolicy_Tooltip, -}; - -struct ImGuiDataVarInfo -{ - ImGuiDataType Type; - ImU32 Count; // 1+ - ImU32 Offset; // Offset in parent structure - void* GetVarPtr(void* parent) const { return (void*)((unsigned char*)parent + Offset); } -}; - -struct ImGuiDataTypeTempStorage -{ - ImU8 Data[8]; // Can fit any data up to ImGuiDataType_COUNT -}; - -// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). -struct ImGuiDataTypeInfo -{ - size_t Size; // Size in bytes - const char* Name; // Short descriptive name for the type, for debugging - const char* PrintFmt; // Default printf format for the type - const char* ScanFmt; // Default scanf format for the type -}; - -// Extend ImGuiDataType_ -enum ImGuiDataTypePrivate_ -{ - ImGuiDataType_String = ImGuiDataType_COUNT + 1, - ImGuiDataType_Pointer, - ImGuiDataType_ID, -}; - // Stacked color modifier, backup of modified data so we can restore it struct ImGuiColorMod { @@ -1106,7 +1069,7 @@ struct IMGUI_API ImGuiInputTextState int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not. ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. ImVector TextA; // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity. - ImVector InitialTextA; // backup of end-user buffer at the time of focus (in UTF-8, unaltered) + ImVector InitialTextA; // value to revert to when pressing Escape = backup of end-user buffer at the time of focus (in UTF-8, unaltered) bool TextAIsValid; // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument) int BufCapacityA; // end-user buffer capacity float ScrollX; // horizontal scrolling/offset @@ -1116,6 +1079,9 @@ struct IMGUI_API ImGuiInputTextState bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection bool Edited; // edited this frame ImGuiInputTextFlags Flags; // copy of InputText() flags. may be used to check if e.g. ImGuiInputTextFlags_Password is set. + bool ReloadUserBuf; // force a reload of user buf so it may be modified externally. may be automatic in future version. + int ReloadSelectionStart; // POSITIONS ARE IN IMWCHAR units *NOT* UTF-8 this is why this is not exposed yet. + int ReloadSelectionEnd; ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } @@ -1133,21 +1099,16 @@ struct IMGUI_API ImGuiInputTextState int GetSelectionStart() const { return Stb.select_start; } int GetSelectionEnd() const { return Stb.select_end; } void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } -}; -// Storage for current popup stack -struct ImGuiPopupData -{ - ImGuiID PopupId; // Set on OpenPopup() - ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() - ImGuiWindow* BackupNavWindow;// Set on OpenPopup(), a NavWindow that will be restored on popup close - int ParentNavLayer; // Resolved on BeginPopup(). Actually a ImGuiNavLayer type (declared down below), initialized to -1 which is not part of an enum, but serves well-enough as "not any of layers" value - int OpenFrameCount; // Set on OpenPopup() - ImGuiID OpenParentId; // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) - ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) - ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup + // Reload user buf (WIP #2890) + // If you modify underlying user-passed const char* while active you need to call this (InputText V2 may lift this) + // strcpy(my_buf, "hello"); + // if (ImGuiInputTextState* state = ImGui::GetInputTextState(id)) // id may be ImGui::GetItemID() is last item + // state->ReloadUserBufAndSelectAll(); + void ReloadUserBufAndSelectAll() { ReloadUserBuf = true; ReloadSelectionStart = 0; ReloadSelectionEnd = INT_MAX; } + void ReloadUserBufAndKeepSelection() { ReloadUserBuf = true; ReloadSelectionStart = Stb.select_start; ReloadSelectionEnd = Stb.select_end; } + void ReloadUserBufAndMoveToEnd() { ReloadUserBuf = true; ReloadSelectionStart = ReloadSelectionEnd = INT_MAX; } - ImGuiPopupData() { memset(this, 0, sizeof(*this)); ParentNavLayer = OpenFrameCount = -1; } }; enum ImGuiNextWindowDataFlags_ @@ -1194,9 +1155,10 @@ typedef ImS64 ImGuiSelectionUserData; enum ImGuiNextItemDataFlags_ { - ImGuiNextItemDataFlags_None = 0, - ImGuiNextItemDataFlags_HasWidth = 1 << 0, - ImGuiNextItemDataFlags_HasOpen = 1 << 1, + ImGuiNextItemDataFlags_None = 0, + ImGuiNextItemDataFlags_HasWidth = 1 << 0, + ImGuiNextItemDataFlags_HasOpen = 1 << 1, + ImGuiNextItemDataFlags_HasShortcut = 1 << 2, }; struct ImGuiNextItemData @@ -1204,10 +1166,11 @@ struct ImGuiNextItemData ImGuiNextItemDataFlags Flags; ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemFlags_AllowOverlap. // Non-flags members are NOT cleared by ItemAdd() meaning they are still valid during NavProcessItem() - float Width; // Set by SetNextItemWidth() ImGuiSelectionUserData SelectionUserData; // Set by SetNextItemSelectionUserData() (note that NULL/0 is a valid value, we use -1 == ImGuiSelectionUserData_Invalid to mark invalid values) - ImGuiCond OpenCond; + float Width; // Set by SetNextItemWidth() + ImGuiKeyChord Shortcut; // Set by SetNextItemShortcut() bool OpenVal; // Set by SetNextItemOpen() + ImGuiCond OpenCond : 8; ImGuiNextItemData() { memset(this, 0, sizeof(*this)); SelectionUserData = -1; } inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! @@ -1279,6 +1242,66 @@ struct ImGuiPtrOrIndex ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } }; +//----------------------------------------------------------------------------- +// [SECTION] Data types support +//----------------------------------------------------------------------------- + +struct ImGuiDataVarInfo +{ + ImGuiDataType Type; + ImU32 Count; // 1+ + ImU32 Offset; // Offset in parent structure + void* GetVarPtr(void* parent) const { return (void*)((unsigned char*)parent + Offset); } +}; + +struct ImGuiDataTypeTempStorage +{ + ImU8 Data[8]; // Can fit any data up to ImGuiDataType_COUNT +}; + +// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). +struct ImGuiDataTypeInfo +{ + size_t Size; // Size in bytes + const char* Name; // Short descriptive name for the type, for debugging + const char* PrintFmt; // Default printf format for the type + const char* ScanFmt; // Default scanf format for the type +}; + +// Extend ImGuiDataType_ +enum ImGuiDataTypePrivate_ +{ + ImGuiDataType_String = ImGuiDataType_COUNT + 1, + ImGuiDataType_Pointer, + ImGuiDataType_ID, +}; + +//----------------------------------------------------------------------------- +// [SECTION] Popup support +//----------------------------------------------------------------------------- + +enum ImGuiPopupPositionPolicy +{ + ImGuiPopupPositionPolicy_Default, + ImGuiPopupPositionPolicy_ComboBox, + ImGuiPopupPositionPolicy_Tooltip, +}; + +// Storage for popup stacks (g.OpenPopupStack and g.BeginPopupStack) +struct ImGuiPopupData +{ + ImGuiID PopupId; // Set on OpenPopup() + ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() + ImGuiWindow* BackupNavWindow;// Set on OpenPopup(), a NavWindow that will be restored on popup close + int ParentNavLayer; // Resolved on BeginPopup(). Actually a ImGuiNavLayer type (declared down below), initialized to -1 which is not part of an enum, but serves well-enough as "not any of layers" value + int OpenFrameCount; // Set on OpenPopup() + ImGuiID OpenParentId; // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) + ImVec2 OpenPopupPos; // Set on OpenPopup(), preferred popup position (typically == OpenMousePos when using mouse) + ImVec2 OpenMousePos; // Set on OpenPopup(), copy of mouse position at the time of opening popup + + ImGuiPopupData() { memset(this, 0, sizeof(*this)); ParentNavLayer = OpenFrameCount = -1; } +}; + //----------------------------------------------------------------------------- // [SECTION] Inputs support //----------------------------------------------------------------------------- @@ -1369,11 +1392,12 @@ struct ImGuiKeyRoutingData { ImGuiKeyRoutingIndex NextEntryIndex; ImU16 Mods; // Technically we'd only need 4-bits but for simplify we store ImGuiMod_ values which need 16-bits. ImGuiMod_Shortcut is already translated to Ctrl/Super. + ImU8 RoutingCurrScore; // [DEBUG] For debug display ImU8 RoutingNextScore; // Lower is better (0: perfect score) ImGuiID RoutingCurr; ImGuiID RoutingNext; - ImGuiKeyRoutingData() { NextEntryIndex = -1; Mods = 0; RoutingNextScore = 255; RoutingCurr = RoutingNext = ImGuiKeyOwner_None; } + ImGuiKeyRoutingData() { NextEntryIndex = -1; Mods = 0; RoutingCurrScore = RoutingNextScore = 255; RoutingCurr = RoutingNext = ImGuiKeyOwner_None; } }; // Routing table: maintain a desired owner for each possible key-chord (key + mods), and setup owner in NewFrame() when mods are matching. @@ -1401,17 +1425,19 @@ struct ImGuiKeyOwnerData }; // Flags for extended versions of IsKeyPressed(), IsMouseClicked(), Shortcut(), SetKeyOwner(), SetItemKeyOwner() -// Don't mistake with ImGuiInputTextFlags! (for ImGui::InputText() function) +// Don't mistake with ImGuiInputTextFlags! (which is for ImGui::InputText() function) enum ImGuiInputFlags_ { // Flags for IsKeyPressed(), IsKeyChordPressed(), IsMouseClicked(), Shortcut() ImGuiInputFlags_None = 0, - ImGuiInputFlags_Repeat = 1 << 0, // Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. + + // Repeat mode + ImGuiInputFlags_Repeat = 1 << 0, // Enable repeat. Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) ImGuiInputFlags_RepeatRateNavMove = 1 << 2, // Repeat rate: Fast ImGuiInputFlags_RepeatRateNavTweak = 1 << 3, // Repeat rate: Faster - // Specify when repeating key pressed can be interrupted. + // Repeat mode: Specify when repeating key pressed can be interrupted. // In theory ImGuiInputFlags_RepeatUntilOtherKeyPress may be a desirable default, but it would break too many behavior so everything is opt-in. ImGuiInputFlags_RepeatUntilRelease = 1 << 4, // Stop repeating when released (default for all functions except Shortcut). This only exists to allow overriding Shortcut() default behavior. ImGuiInputFlags_RepeatUntilKeyModsChange = 1 << 5, // Stop repeating when released OR if keyboard mods are changed (default for Shortcut) @@ -1422,38 +1448,46 @@ enum ImGuiInputFlags_ ImGuiInputFlags_CondHovered = 1 << 8, // Only set if item is hovered (default to both) ImGuiInputFlags_CondActive = 1 << 9, // Only set if item is active (default to both) ImGuiInputFlags_CondDefault_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, - ImGuiInputFlags_CondMask_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, // Flags for SetKeyOwner(), SetItemKeyOwner() - ImGuiInputFlags_LockThisFrame = 1 << 10, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. - ImGuiInputFlags_LockUntilRelease = 1 << 11, // Access to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. This is useful to make input-owner-aware code steal keys from non-input-owner-aware code. + // Locking is useful to make input-owner-aware code steal keys from non-input-owner-aware code. If all code is input-owner-aware locking would never be necessary. + ImGuiInputFlags_LockThisFrame = 1 << 10, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. + ImGuiInputFlags_LockUntilRelease = 1 << 11, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. // Routing policies for Shortcut() + low-level SetShortcutRouting() // - The general idea is that several callers register interest in a shortcut, and only one owner gets it. - // - When a policy (other than _RouteAlways) is set, Shortcut() will register itself with SetShortcutRouting(), + // Parent -> call Shortcut(Ctrl+S) // When Parent is focused, Parent gets the shortcut. + // Child1 -> call Shortcut(Ctrl+S) // When Child1 is focused, Child1 gets the shortcut (Child1 overrides Parent shortcuts) + // Child2 -> no call // When Child2 is focused, Parent gets the shortcut. + // The whole system is order independent, so if Child1 does it calls before Parent results will be identical. + // This is an important property as it facilitate working with foreign code or larger codebase. + // - Visualize registered routes in 'Metrics->Inputs' and submitted routes in 'Debug Log->InputRouting'. + // - When a policy (except for _RouteAlways *) is set, Shortcut() will register itself with SetShortcutRouting(), // allowing the system to decide where to route the input among other route-aware calls. - // - Shortcut() uses ImGuiInputFlags_RouteFocused by default: meaning that a simple Shortcut() poll - // will register a route and only succeed when parent window is in the focus stack and if no-one - // with a higher priority is claiming the shortcut. - // - Using ImGuiInputFlags_RouteAlways is roughly equivalent to doing e.g. IsKeyPressed(key) + testing mods. + // (* Using ImGuiInputFlags_RouteAlways is roughly equivalent to calling IsKeyChordPressed(key)). + // - Shortcut() uses ImGuiInputFlags_RouteFocused by default. Meaning that a Shortcut() call will register + // a route and only succeed when parent window is in the focus-stack and if no-one with a higher priority + // is claiming the same shortcut. + // - You can chain two unrelated windows in the focus stack using SetWindowParentWindowForFocusRoute(). // - Priorities: GlobalHigh > Focused (when owner is active item) > Global > Focused (when focused window) > GlobalLow. // - Can select only 1 policy among all available. - ImGuiInputFlags_RouteFocused = 1 << 12, // (Default) Register focused route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. - ImGuiInputFlags_RouteGlobalLow = 1 << 13, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority. - ImGuiInputFlags_RouteGlobal = 1 << 14, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText). - ImGuiInputFlags_RouteGlobalHigh = 1 << 15, // Register route globally (highest priority: unlikely you need to use that: will interfere with every active items) - ImGuiInputFlags_RouteMask_ = ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteGlobalLow | ImGuiInputFlags_RouteGlobalHigh, // _Always not part of this! + ImGuiInputFlags_RouteFocused = 1 << 12, // (Default) Honor focus route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. + ImGuiInputFlags_RouteGlobalLow = 1 << 13, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority IF you need a Global priority. + ImGuiInputFlags_RouteGlobal = 1 << 14, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText will take priority over this). + ImGuiInputFlags_RouteGlobalHigh = 1 << 15, // Register route globally (higher priority: unlikely you need to use that: will interfere with every active items, e.g. CTRL+A registered by InputText will be overriden by this) ImGuiInputFlags_RouteAlways = 1 << 16, // Do not register route, poll keys directly. + // Routing polices: extra options ImGuiInputFlags_RouteUnlessBgFocused= 1 << 17, // Global routes will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. - ImGuiInputFlags_RouteExtraMask_ = ImGuiInputFlags_RouteAlways | ImGuiInputFlags_RouteUnlessBgFocused, // [Internal] Mask of which function support which flags ImGuiInputFlags_RepeatRateMask_ = ImGuiInputFlags_RepeatRateDefault | ImGuiInputFlags_RepeatRateNavMove | ImGuiInputFlags_RepeatRateNavTweak, ImGuiInputFlags_RepeatUntilMask_ = ImGuiInputFlags_RepeatUntilRelease | ImGuiInputFlags_RepeatUntilKeyModsChange | ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone | ImGuiInputFlags_RepeatUntilOtherKeyPress, ImGuiInputFlags_RepeatMask_ = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RepeatUntilMask_, + ImGuiInputFlags_CondMask_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, + ImGuiInputFlags_RouteMask_ = ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteGlobalLow | ImGuiInputFlags_RouteGlobalHigh, // _Always not part of this! ImGuiInputFlags_SupportedByIsKeyPressed = ImGuiInputFlags_RepeatMask_, ImGuiInputFlags_SupportedByIsMouseClicked = ImGuiInputFlags_Repeat, - ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteExtraMask_, + ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteAlways | ImGuiInputFlags_RouteUnlessBgFocused, ImGuiInputFlags_SupportedBySetKeyOwner = ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease, ImGuiInputFlags_SupportedBySetItemKeyOwner = ImGuiInputFlags_SupportedBySetKeyOwner | ImGuiInputFlags_CondMask_, }; @@ -1499,6 +1533,7 @@ enum ImGuiActivateFlags_ ImGuiActivateFlags_PreferTweak = 1 << 1, // Favor activation for tweaking with arrows or gamepad (e.g. for Slider/Drag). Default for Space key and if keyboard is not used. ImGuiActivateFlags_TryToPreserveState = 1 << 2, // Request widget to preserve state if it can (e.g. InputText will try to preserve cursor/selection) ImGuiActivateFlags_FromTabbing = 1 << 3, // Activation requested by a tabbing request + ImGuiActivateFlags_FromShortcut = 1 << 4, // Activation requested by an item shortcut via SetNextItemShortcut() function. }; // Early work-in-progress API for ScrollToItem() @@ -1519,8 +1554,7 @@ enum ImGuiScrollFlags_ enum ImGuiNavHighlightFlags_ { ImGuiNavHighlightFlags_None = 0, - ImGuiNavHighlightFlags_TypeDefault = 1 << 0, - ImGuiNavHighlightFlags_TypeThin = 1 << 1, + ImGuiNavHighlightFlags_Compact = 1 << 1, // Compact highlight, no padding ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. ImGuiNavHighlightFlags_NoRounding = 1 << 3, }; @@ -1569,6 +1603,12 @@ struct ImGuiNavItemData void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; SelectionUserData = -1; DistBox = DistCenter = DistAxial = FLT_MAX; } }; +struct ImGuiFocusScopeData +{ + ImGuiID ID; + ImGuiID WindowID; +}; + //----------------------------------------------------------------------------- // [SECTION] Typing-select support //----------------------------------------------------------------------------- @@ -1788,8 +1828,9 @@ enum ImGuiDebugLogFlags_ ImGuiDebugLogFlags_EventClipper = 1 << 4, ImGuiDebugLogFlags_EventSelection = 1 << 5, ImGuiDebugLogFlags_EventIO = 1 << 6, + ImGuiDebugLogFlags_EventInputRouting = 1 << 7, - ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO, + ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO | ImGuiDebugLogFlags_EventInputRouting, ImGuiDebugLogFlags_OutputToTTY = 1 << 20, // Also send output to TTY ImGuiDebugLogFlags_OutputToTestEngine = 1 << 21, // Also send output to Test Engine }; @@ -1824,6 +1865,8 @@ struct ImGuiMetricsConfig bool ShowAtlasTintedWithTextColor = false; int ShowWindowsRectsType = -1; int ShowTablesRectsType = -1; + int HighlightMonitorIdx = -1; + ImGuiID HighlightViewportID = 0; }; struct ImGuiStackLevelInfo @@ -1937,10 +1980,11 @@ struct ImGuiContext bool ActiveIdHasBeenPressedBefore; // Track whether the active id led to a press (this is to allow changing between PressOnClick and PressOnRelease without pressing twice). Used by range_select branch. bool ActiveIdHasBeenEditedBefore; // Was the value associated to the widget Edited over the course of the Active state. bool ActiveIdHasBeenEditedThisFrame; + bool ActiveIdFromShortcut; + int ActiveIdMouseButton : 8; ImVec2 ActiveIdClickOffset; // Clicked offset from upper-left corner, if applicable (currently only set by ButtonBehavior) ImGuiWindow* ActiveIdWindow; ImGuiInputSource ActiveIdSource; // Activating source: ImGuiInputSource_Mouse OR ImGuiInputSource_Keyboard OR ImGuiInputSource_Gamepad - int ActiveIdMouseButton; ImGuiID ActiveIdPreviousFrame; bool ActiveIdPreviousFrameIsAlive; bool ActiveIdPreviousFrameHasBeenEditedBefore; @@ -1955,6 +1999,7 @@ struct ImGuiContext double LastKeyModsChangeTime; // Record the last time key mods changed (affect repeat delay when using shortcut logic) double LastKeyModsChangeFromNoneTime; // Record the last time key mods changed away from being 0 (affect repeat delay when using shortcut logic) double LastKeyboardKeyPressTime; // Record the last time a keyboard key (ignore mouse/gamepad ones) was pressed. + ImBitArrayForNamedKeys KeysMayBeCharInput; // Lookup to tell if a key can emit char input, see IsKeyChordPotentiallyCharInput(). sizeof() = 20 bytes ImGuiKeyOwnerData KeysOwnerData[ImGuiKey_NamedKey_COUNT]; ImGuiKeyRoutingTable KeysRoutingTable; ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) @@ -1965,8 +2010,8 @@ struct ImGuiContext #endif // Next window/item data - ImGuiID CurrentFocusScopeId; // == g.FocusScopeStack.back() - ImGuiItemFlags CurrentItemFlags; // == g.ItemFlagsStack.back() + ImGuiID CurrentFocusScopeId; // Value for currently appending items == g.FocusScopeStack.back(). Not to be mistaken with g.NavFocusScopeId. + ImGuiItemFlags CurrentItemFlags; // Value for currently appending items == g.ItemFlagsStack.back() ImGuiID DebugLocateId; // Storage for DebugLocateItemOnHover() feature: this is read by ItemAdd() so we keep it in a hot/cached location ImGuiNextItemData NextItemData; // Storage for SetNextItem** functions ImGuiLastItemData LastItemData; // Storage for last submitted item (setup by ItemAdd) @@ -1974,18 +2019,16 @@ struct ImGuiContext bool DebugShowGroupRects; // Shared stacks - ImGuiCol DebugFlashStyleColorIdx; // (Keep close to ColorStack to share cache line) - ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() - ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() - ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() - ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin() - ImVector ItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() - ImVector GroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() - ImVector OpenPopupStack; // Which popups are open (persistent) - ImVector BeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) - ImVector NavTreeNodeStack; // Stack for TreeNode() when a NavLeft requested is emitted. - - int BeginMenuCount; + ImGuiCol DebugFlashStyleColorIdx; // (Keep close to ColorStack to share cache line) + ImVector ColorStack; // Stack for PushStyleColor()/PopStyleColor() - inherited by Begin() + ImVector StyleVarStack; // Stack for PushStyleVar()/PopStyleVar() - inherited by Begin() + ImVector FontStack; // Stack for PushFont()/PopFont() - inherited by Begin() + ImVector FocusScopeStack; // Stack for PushFocusScope()/PopFocusScope() - inherited by BeginChild(), pushed into by Begin() + ImVector ItemFlagsStack; // Stack for PushItemFlag()/PopItemFlag() - inherited by Begin() + ImVector GroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() + ImVector OpenPopupStack; // Which popups are open (persistent) + ImVector BeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) + ImVector NavTreeNodeStack; // Stack for TreeNode() when a NavLeft requested is emitted. // Viewports ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. @@ -1993,11 +2036,14 @@ struct ImGuiContext // Gamepad/keyboard Navigation ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' ImGuiID NavId; // Focused item for navigation - ImGuiID NavFocusScopeId; // Identify a selection scope (selection code often wants to "clear other items" when landing on an item of the selection set) + ImGuiID NavFocusScopeId; // Focused focus scope (e.g. selection code often wants to "clear other items" when landing on an item of the same scope) + ImVector NavFocusRoute; // Reversed copy focus scope stack for NavId (should contains NavFocusScopeId). This essentially follow the window->ParentWindowForFocusRoute chain. ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat) ImGuiActivateFlags NavActivateFlags; + ImGuiID NavHighlightActivatedId; + float NavHighlightActivatedTimer; ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). ImGuiKeyChord NavJustMovedToKeyMods; @@ -2044,6 +2090,7 @@ struct ImGuiContext float NavWindowingTimer; float NavWindowingHighlightAlpha; bool NavWindowingToggleLayer; + ImGuiKey NavWindowingToggleKey; ImVec2 NavWindowingAccumDeltaPos; ImVec2 NavWindowingAccumDeltaSize; @@ -2107,6 +2154,8 @@ struct ImGuiContext ImGuiInputTextDeactivatedState InputTextDeactivatedState; ImFont InputTextPasswordFont; ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. + int BeginMenuDepth; + int BeginComboDepth; ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets ImGuiID ColorEditCurrentID; // Set temporarily while inside of the parent-most ColorEdit4/ColorPicker4 (because they call each others). ImGuiID ColorEditSavedID; // ID we are saving/restoring HS for @@ -2163,6 +2212,7 @@ struct ImGuiContext int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. // Debug Tools + // (some of the highly frequently used data are interleaved in other structures above: DebugBreakXXX fields, DebugHookIdInfo, DebugLocateId etc.) ImGuiDebugLogFlags DebugLogFlags; ImGuiTextBuffer DebugLogBuf; ImGuiTextIndex DebugLogIndex; @@ -2190,6 +2240,7 @@ struct ImGuiContext int WantCaptureKeyboardNextFrame; // " int WantTextInputNextFrame; ImVector TempBuffer; // Temporary text buffer + char TempKeychordName[64]; ImGuiContext(ImFontAtlas* shared_font_atlas) { @@ -2235,6 +2286,7 @@ struct ImGuiContext ActiveIdHasBeenPressedBefore = false; ActiveIdHasBeenEditedBefore = false; ActiveIdHasBeenEditedThisFrame = false; + ActiveIdFromShortcut = false; ActiveIdClickOffset = ImVec2(-1, -1); ActiveIdWindow = NULL; ActiveIdSource = ImGuiInputSource_None; @@ -2257,12 +2309,13 @@ struct ImGuiContext CurrentFocusScopeId = 0; CurrentItemFlags = ImGuiItemFlags_None; DebugShowGroupRects = false; - BeginMenuCount = 0; NavWindow = NULL; NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = 0; NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; + NavHighlightActivatedId = 0; + NavHighlightActivatedTimer = 0.0f; NavJustMovedToKeyMods = ImGuiMod_None; NavInputSource = ImGuiInputSource_Keyboard; NavLayer = ImGuiNavLayer_Main; @@ -2290,6 +2343,7 @@ struct ImGuiContext NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; + NavWindowingToggleKey = ImGuiKey_None; DimBgRatio = 0.0f; @@ -2318,6 +2372,7 @@ struct ImGuiContext MouseStationaryTimer = 0.0f; TempInputId = 0; + BeginMenuDepth = BeginComboDepth = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditCurrentID = ColorEditSavedID = 0; ColorEditSavedHue = ColorEditSavedSat = 0.0f; @@ -2376,6 +2431,7 @@ struct ImGuiContext FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; FramerateSecPerFrameAccum = 0.0f; WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; + memset(TempKeychordName, 0, sizeof(TempKeychordName)); } }; @@ -2425,6 +2481,7 @@ struct IMGUI_API ImGuiWindowTempData int CurrentTableIdx; // Current table index (into g.Tables) ImGuiLayoutType LayoutType; ImGuiLayoutType ParentLayoutType; // Layout type of parent window at the time of Begin() + ImU32 ModalDimBgColor; // Local parameters stacks // We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings. @@ -2528,6 +2585,7 @@ struct IMGUI_API ImGuiWindow ImGuiWindow* RootWindowPopupTree; // Point to ourself or first ancestor that is not a child window. Cross through popups parent<>child. ImGuiWindow* RootWindowForTitleBarHighlight; // Point to ourself or first ancestor which will display TitleBgActive color when this window is active. ImGuiWindow* RootWindowForNav; // Point to ourself or first ancestor which doesn't have the NavFlattened flag. + ImGuiWindow* ParentWindowForFocusRoute; // Set to manual link a window to its logical parent so that Shortcut() chain are honoerd (e.g. Tool linked to Document) ImGuiWindow* NavLastChildNavWindow; // When going to the menu bar, we remember the child window we came from. (This could probably be made implicit if we kept g.Windows sorted by last focused including child window.) ImGuiID NavLastIds[ImGuiNavLayer_COUNT]; // Last known NavId for this window, per layer (0/1) @@ -2859,7 +2917,7 @@ struct IMGUI_API ImGuiTableTempData { int TableIndex; // Index in g.Tables.Buf[] pool float LastTimeActive; // Last timestamp this structure was used - float AngledheadersExtraWidth; // Used in EndTable() + float AngledHeadersExtraWidth; // Used in EndTable() ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() ImDrawListSplitter DrawSplitter; @@ -2941,6 +2999,7 @@ namespace ImGui IMGUI_API void SetWindowCollapsed(ImGuiWindow* window, bool collapsed, ImGuiCond cond = 0); IMGUI_API void SetWindowHitTestHole(ImGuiWindow* window, const ImVec2& pos, const ImVec2& size); IMGUI_API void SetWindowHiddenAndSkipItemsForCurrentFrame(ImGuiWindow* window); + inline void SetWindowParentWindowForFocusRoute(ImGuiWindow* window, ImGuiWindow* parent_window) { window->ParentWindowForFocusRoute = parent_window; } inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } @@ -3094,11 +3153,13 @@ namespace ImGui IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); + IMGUI_API void NavHighlightActivated(ImGuiID id); IMGUI_API void NavClearPreferredPosForAxis(ImGuiAxis axis); IMGUI_API void NavRestoreHighlightAfterMove(); IMGUI_API void NavUpdateCurrentWindowIsScrollPushableX(); IMGUI_API void SetNavWindow(ImGuiWindow* window); IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); + IMGUI_API void SetNavFocusScope(ImGuiID focus_scope_id); // Focus/Activation // This should be part of a larger set of API: FocusItem(offset = -1), FocusItemByID(id), ActivateItem(offset = -1), ActivateItemByID(id) etc. which are @@ -3115,7 +3176,8 @@ namespace ImGui inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } - inline ImGuiKeyChord ConvertShortcutMod(ImGuiKeyChord key_chord) { ImGuiContext& g = *GImGui; IM_ASSERT_PARANOID(key_chord & ImGuiMod_Shortcut); return (key_chord & ~ImGuiMod_Shortcut) | (g.IO.ConfigMacOSXBehaviors ? ImGuiMod_Super : ImGuiMod_Ctrl); } + inline bool IsModKey(ImGuiKey key) { return key >= ImGuiKey_LeftCtrl && key <= ImGuiKey_RightSuper; } + ImGuiKeyChord FixupKeyChord(ImGuiContext* ctx, ImGuiKeyChord key_chord); inline ImGuiKey ConvertSingleModFlagToKey(ImGuiContext* ctx, ImGuiKey key) { ImGuiContext& g = *ctx; @@ -3129,7 +3191,7 @@ namespace ImGui IMGUI_API ImGuiKeyData* GetKeyData(ImGuiContext* ctx, ImGuiKey key); inline ImGuiKeyData* GetKeyData(ImGuiKey key) { ImGuiContext& g = *GImGui; return GetKeyData(&g, key); } - IMGUI_API const char* GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size); + IMGUI_API const char* GetKeyChordName(ImGuiKeyChord key_chord); inline ImGuiKey MouseButtonToKey(ImGuiMouseButton button) { IM_ASSERT(button >= 0 && button < ImGuiMouseButton_COUNT); return (ImGuiKey)(ImGuiKey_MouseLeft + button); } IMGUI_API bool IsMouseDragPastThreshold(ImGuiMouseButton button, float lock_threshold = -1.0f); IMGUI_API ImVec2 GetKeyMagnitude2d(ImGuiKey key_left, ImGuiKey key_right, ImGuiKey key_up, ImGuiKey key_down); @@ -3186,8 +3248,9 @@ namespace ImGui // - IsKeyChordPressed() compares mods + call IsKeyPressed() -> function has no side-effect. // - Shortcut() submits a route then if currently can be routed calls IsKeyChordPressed() -> function has (desirable) side-effects. IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API void SetNextItemShortcut(ImGuiKeyChord key_chord); IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); - IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); + IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); // owner_id needs to be explicit and cannot be 0 IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id); IMGUI_API ImGuiKeyRoutingData* GetShortcutRoutingData(ImGuiKeyChord key_chord); @@ -3238,7 +3301,7 @@ namespace ImGui IMGUI_API float TableGetHeaderAngledMaxLabelWidth(); IMGUI_API void TablePushBackgroundChannel(); IMGUI_API void TablePopBackgroundChannel(); - IMGUI_API void TableAngledHeadersRowEx(float angle, float label_width = 0.0f); + IMGUI_API void TableAngledHeadersRowEx(float angle, float max_label_width = 0.0f); // Tables: Internals inline ImGuiTable* GetCurrentTable() { ImGuiContext& g = *GImGui; return g.CurrentTable; } @@ -3316,7 +3379,7 @@ namespace ImGui IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, ImDrawFlags flags = 0); - IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight + IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_None); // Navigation highlight IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. IMGUI_API void RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); @@ -3421,7 +3484,6 @@ namespace ImGui IMGUI_API void DebugBreakClearData(); IMGUI_API bool DebugBreakButton(const char* label, const char* description_of_location); IMGUI_API void DebugBreakButtonTooltip(bool keyboard_only, const char* description_of_location); - inline void DebugStartItemPicker() { ImGuiContext& g = *GImGui; g.DebugItemPickerActive = true; } IMGUI_API void ShowFontAtlas(ImFontAtlas* atlas); IMGUI_API void DebugHookIdInfo(ImGuiID id, ImGuiDataType data_type, const void* data_id, const void* data_id_end); IMGUI_API void DebugNodeColumns(ImGuiOldColumns* columns); @@ -3448,13 +3510,12 @@ namespace ImGui inline void SetItemUsingMouseWheel() { SetItemKeyOwner(ImGuiKey_MouseWheelY); } // Changed in 1.89 inline bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0) { return TreeNodeUpdateNextOpen(id, flags); } // Renamed in 1.89 - // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets that used FocusableItemRegister(): + // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets which used FocusableItemRegister(): // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' - // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_Focused) != 0' - // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (GetItemStatusFlags() & ImGuiItemStatusFlags_FocusedTabbing) != 0 || (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))' (WIP) - // Widget code are simplified as there's no need to call FocusableItemUnregister() while managing the transition from regular widget to TempInputText() - inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) { IM_ASSERT(0); IM_UNUSED(window); IM_UNUSED(id); return false; } // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() - inline void FocusableItemUnregister(ImGuiWindow* window) { IM_ASSERT(0); IM_UNUSED(window); } // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem + // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0' + // (New) IMGUI_VERSION_NUM >= 18413: using 'ItemAdd(..., ImGuiItemFlags_Inputable)' and 'bool tab_focused = (g.NavActivateId == id && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput))' + //inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() + //inline void FocusableItemUnregister(ImGuiWindow* window) // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem #endif #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { IM_ASSERT(IsNamedKey(key)); return IsKeyPressed(key, repeat); } // Removed in 1.87: Mapping from named key is always identity! diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index e406a9f1..4f017037 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (main code and documentation) // Help: @@ -75,6 +75,7 @@ CODE // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) // [SECTION] INPUTS // [SECTION] ERROR CHECKING +// [SECTION] ITEM SUBMISSION // [SECTION] LAYOUT // [SECTION] SCROLLING // [SECTION] TOOLTIPS @@ -424,6 +425,7 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2024/01/15 (1.90.2) - commented out obsolete ImGuiIO::ImeWindowHandle marked obsolete in 1.87, favor of writing to 'void* ImGuiViewport::PlatformHandleRaw'. - 2023/12/19 (1.90.1) - commented out obsolete ImGuiKey_KeyPadEnter redirection to ImGuiKey_KeypadEnter. - 2023/11/06 (1.90.1) - removed CalcListClipping() marked obsolete in 1.86. Prefer using ImGuiListClipper which can return non-contiguous ranges. - 2023/11/05 (1.90.1) - imgui_freetype: commented out ImGuiFreeType::BuildFontAtlas() obsoleted in 1.81. prefer using #define IMGUI_ENABLE_FREETYPE or see commented code for manual calls. @@ -915,7 +917,7 @@ CODE Q: How can I easily use icons in my application? Q: How can I load multiple fonts? Q: How can I display and input non-Latin characters such as Chinese, Japanese, Korean, Cyrillic? - >> See https://www.dearimgui.com/faq and https://github.com/ocornut/imgui/edit/master/docs/FONTS.md + >> See https://www.dearimgui.com/faq and https://github.com/ocornut/imgui/blob/master/docs/FONTS.md Q&A: Concerns ============= @@ -1045,6 +1047,8 @@ CODE static const float NAV_WINDOWING_HIGHLIGHT_DELAY = 0.20f; // Time before the highlight and screen dimming starts fading in static const float NAV_WINDOWING_LIST_APPEAR_DELAY = 0.15f; // Time before the window list starts to appear +static const float NAV_ACTIVATE_HIGHLIGHT_TIMER = 0.10f; // Time to highlight an item activated by a shortcut. + // Window resizing from edges (when io.ConfigWindowsResizeFromEdges = true and ImGuiBackendFlags_HasMouseCursors is set in io.BackendFlags by backend) static const float WINDOWS_HOVER_PADDING = 4.0f; // Extend outside window for hovering/resizing (maxxed with TouchPadding) and inside windows for borders. Affect FindHoveredWindow(). static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduce visual noise by only highlighting the border after a certain time. @@ -1122,6 +1126,7 @@ static void RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, static void RenderDimmedBackgrounds(); // Viewports +const ImGuiID IMGUI_VIEWPORT_DEFAULT_ID = 0x11111111; // Using an arbitrary constant instead of e.g. ImHashStr("ViewportDefault", 0); so it's easier to spot in the debugger. The exact value doesn't matter. static void UpdateViewportsNewFrame(); } @@ -2058,12 +2063,18 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode) // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); - ImGuiContext& g = *GImGui; - g.TempBuffer.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t)); - wchar_t* buf = (wchar_t*)(void*)g.TempBuffer.Data; - ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize); - ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize); - return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]); + + // Use stack buffer if possible, otherwise heap buffer. Sizes include zero terminator. + // We don't rely on current ImGuiContext as this is implied to be a helper function which doesn't depend on it (see #7314). + wchar_t local_temp_stack[FILENAME_MAX]; + ImVector local_temp_heap; + if (filename_wsize + mode_wsize > IM_ARRAYSIZE(local_temp_stack)) + local_temp_heap.resize(filename_wsize + mode_wsize); + wchar_t* filename_wbuf = local_temp_heap.Data ? local_temp_heap.Data : local_temp_stack; + wchar_t* mode_wbuf = filename_wbuf + filename_wsize; + ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_wbuf, filename_wsize); + ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, mode_wbuf, mode_wsize); + return ::_wfopen(filename_wbuf, mode_wbuf); #else return fopen(filename, mode); #endif @@ -3036,13 +3047,14 @@ const ImVec4& ImGui::GetStyleColorVec4(ImGuiCol idx) return style.Colors[idx]; } -ImU32 ImGui::GetColorU32(ImU32 col) +ImU32 ImGui::GetColorU32(ImU32 col, float alpha_mul) { ImGuiStyle& style = GImGui->Style; - if (style.Alpha >= 1.0f) + alpha_mul *= style.Alpha; + if (alpha_mul >= 1.0f) return col; ImU32 a = (col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT; - a = (ImU32)(a * style.Alpha); // We don't need to clamp 0..255 because Style.Alpha is in 0..1 range. + a = (ImU32)(a * alpha_mul); // We don't need to clamp 0..255 because alpha is in 0..1 range. return (col & ~IM_COL32_A_MASK) | (a << IM_COL32_A_SHIFT); } @@ -3074,7 +3086,7 @@ void ImGui::PopStyleColor(int count) ImGuiContext& g = *GImGui; if (g.ColorStack.Size < count) { - IM_ASSERT_USER_ERROR(g.ColorStack.Size > count, "Calling PopStyleColor() too many times: stack underflow."); + IM_ASSERT_USER_ERROR(g.ColorStack.Size > count, "Calling PopStyleColor() too many times!"); count = g.ColorStack.Size; } while (count > 0) @@ -3137,7 +3149,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) *pvar = val; return; } - IM_ASSERT_USER_ERROR(0, "Called PushStyleVar() variant with wrong type!"); + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); } void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) @@ -3151,7 +3163,7 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) *pvar = val; return; } - IM_ASSERT_USER_ERROR(0, "Called PushStyleVar() variant with wrong type!"); + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); } void ImGui::PopStyleVar(int count) @@ -3159,7 +3171,7 @@ void ImGui::PopStyleVar(int count) ImGuiContext& g = *GImGui; if (g.StyleVarStack.Size < count) { - IM_ASSERT_USER_ERROR(g.StyleVarStack.Size > count, "Calling PopStyleVar() too many times: stack underflow."); + IM_ASSERT_USER_ERROR(g.StyleVarStack.Size > count, "Calling PopStyleVar() too many times!"); count = g.StyleVarStack.Size; } while (count > 0) @@ -3448,22 +3460,22 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl float rounding = (flags & ImGuiNavHighlightFlags_NoRounding) ? 0.0f : g.Style.FrameRounding; ImRect display_rect = bb; display_rect.ClipWith(window->ClipRect); - if (flags & ImGuiNavHighlightFlags_TypeDefault) + const float thickness = 2.0f; + if (flags & ImGuiNavHighlightFlags_Compact) + { + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, thickness); + } + else { - const float THICKNESS = 2.0f; - const float DISTANCE = 3.0f + THICKNESS * 0.5f; - display_rect.Expand(ImVec2(DISTANCE, DISTANCE)); + const float distance = 3.0f + thickness * 0.5f; + display_rect.Expand(ImVec2(distance, distance)); bool fully_visible = window->ClipRect.Contains(display_rect); if (!fully_visible) window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); - window->DrawList->AddRect(display_rect.Min + ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), display_rect.Max - ImVec2(THICKNESS * 0.5f, THICKNESS * 0.5f), GetColorU32(ImGuiCol_NavHighlight), rounding, 0, THICKNESS); + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, thickness); if (!fully_visible) window->DrawList->PopClipRect(); } - if (flags & ImGuiNavHighlightFlags_TypeThin) - { - window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, 1.0f); - } } void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) @@ -3592,9 +3604,18 @@ void ImGui::Initialize() // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); + viewport->ID = IMGUI_VIEWPORT_DEFAULT_ID; g.Viewports.push_back(viewport); g.TempBuffer.resize(1024 * 3 + 1, 0); + // Build KeysMayBeCharInput[] lookup table (1 bool per named key) + for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) + if ((key >= ImGuiKey_0 && key <= ImGuiKey_9) || (key >= ImGuiKey_A && key <= ImGuiKey_Z) || (key >= ImGuiKey_Keypad0 && key <= ImGuiKey_Keypad9) + || key == ImGuiKey_Tab || key == ImGuiKey_Space || key == ImGuiKey_Apostrophe || key == ImGuiKey_Comma || key == ImGuiKey_Minus || key == ImGuiKey_Period + || key == ImGuiKey_Slash || key == ImGuiKey_Semicolon || key == ImGuiKey_Equal || key == ImGuiKey_LeftBracket || key == ImGuiKey_RightBracket || key == ImGuiKey_GraveAccent + || key == ImGuiKey_KeypadDecimal || key == ImGuiKey_KeypadDivide || key == ImGuiKey_KeypadMultiply || key == ImGuiKey_KeypadSubtract || key == ImGuiKey_KeypadAdd || key == ImGuiKey_KeypadEqual) + g.KeysMayBeCharInput.SetBit(key); + #ifdef IMGUI_HAS_DOCK #endif @@ -3879,6 +3900,7 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) g.ActiveIdNoClearOnFocusLoss = false; g.ActiveIdWindow = window; g.ActiveIdHasBeenEditedThisFrame = false; + g.ActiveIdFromShortcut = false; if (id) { g.ActiveIdIsAlive = id; @@ -3915,17 +3937,6 @@ ImGuiID ImGui::GetHoveredID() return g.HoveredId ? g.HoveredId : g.HoveredIdPreviousFrame; } -// This is called by ItemAdd(). -// Code not using ItemAdd() may need to call this manually otherwise ActiveId will be cleared. In IMGUI_VERSION_NUM < 18717 this was called by GetID(). -void ImGui::KeepAliveID(ImGuiID id) -{ - ImGuiContext& g = *GImGui; - if (g.ActiveId == id) - g.ActiveIdIsAlive = id; - if (g.ActiveIdPreviousFrame == id) - g.ActiveIdPreviousFrameIsAlive = true; -} - void ImGui::MarkItemEdited(ImGuiID id) { // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). @@ -4099,7 +4110,8 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag if (g.HoveredId != 0 && g.HoveredId != id && !g.HoveredIdAllowOverlap) return false; if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) - return false; + if (!g.ActiveIdFromShortcut) + return false; // Done with rectangle culling so we can perform heavier checks now. if (!(item_flags & ImGuiItemFlags_NoWindowHoverableCheck) && !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) @@ -4138,17 +4150,19 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag return false; } +#ifndef IMGUI_DISABLE_DEBUG_TOOLS if (id != 0) { // [DEBUG] Item Picker tool! - // We perform the check here because SetHoveredID() is not frequently called (1~ time a frame), making - // the cost of this tool near-zero. We can get slightly better call-stack and support picking non-hovered - // items if we performed the test in ItemAdd(), but that would incur a small runtime cost. + // We perform the check here because reaching is path is rare (1~ time a frame), + // making the cost of this tool near-zero! We could get better call-stack and support picking non-hovered + // items if we performed the test in ItemAdd(), but that would incur a bigger runtime cost. if (g.DebugItemPickerActive && g.HoveredIdPreviousFrame == id) GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); if (g.DebugItemPickerBreakId == id) IM_DEBUG_BREAK(); } +#endif if (g.NavDisableMouseHover) return false; @@ -4157,12 +4171,13 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag } // FIXME: This is inlined/duplicated in ItemAdd() +// FIXME: The id != 0 path is not used by our codebase, may get rid of it? bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; if (!bb.Overlaps(window->ClipRect)) - if (id == 0 || (id != g.ActiveId && id != g.NavId)) + if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId && id != g.NavActivateId)) if (!g.LogEnabled) return true; return false; @@ -4783,6 +4798,7 @@ void ImGui::NewFrame() g.GroupStack.resize(0); // [DEBUG] Update debug features +#ifndef IMGUI_DISABLE_DEBUG_TOOLS UpdateDebugToolItemPicker(); UpdateDebugToolStackQueries(); UpdateDebugToolFlashStyleColor(); @@ -4797,6 +4813,7 @@ void ImGui::NewFrame() g.DebugLogFlags &= ~g.DebugLogAutoDisableFlags; g.DebugLogAutoDisableFlags = ImGuiDebugLogFlags_None; } +#endif // Create implicit/fallback window - which we will only render it if the user has added something to it. // We don't use "Debug" to avoid colliding with user trying to create a "Debug" window with custom flags. @@ -4808,10 +4825,12 @@ void ImGui::NewFrame() // [DEBUG] When io.ConfigDebugBeginReturnValue is set, we make Begin()/BeginChild() return false at different level of the window-stack, // allowing to validate correct Begin/End behavior in user code. +#ifndef IMGUI_DISABLE_DEBUG_TOOLS if (g.IO.ConfigDebugBeginReturnValueLoop) g.DebugBeginReturnValueCullDepth = (g.DebugBeginReturnValueCullDepth == -1) ? 0 : ((g.DebugBeginReturnValueCullDepth + ((g.FrameCount % 4) == 0 ? 1 : 0)) % 10); else g.DebugBeginReturnValueCullDepth = -1; +#endif CallContextHooks(&g, ImGuiContextHookType_NewFramePost); } @@ -4984,7 +5003,7 @@ static void ImGui::RenderDimmedBackgrounds() { // Draw dimming behind modal or a begin stack child, whichever comes first in draw order. ImGuiWindow* dim_behind_window = FindBottomMostVisibleWindowWithinBeginStack(modal_window); - RenderDimmedBackgroundBehindWindow(dim_behind_window, GetColorU32(ImGuiCol_ModalWindowDimBg, g.DimBgRatio)); + RenderDimmedBackgroundBehindWindow(dim_behind_window, GetColorU32(modal_window->DC.ModalDimBgColor, g.DimBgRatio)); } else if (dim_bg_for_window_list) { @@ -5028,18 +5047,7 @@ void ImGui::EndFrame() { IMGUI_DEBUG_LOG_IO("[io] Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); ImGuiViewport* viewport = GetMainViewport(); -#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - if (viewport->PlatformHandleRaw == NULL && g.IO.ImeWindowHandle != NULL) - { - viewport->PlatformHandleRaw = g.IO.ImeWindowHandle; - g.IO.SetPlatformImeDataFn(viewport, ime_data); - viewport->PlatformHandleRaw = NULL; - } - else -#endif - { - g.IO.SetPlatformImeDataFn(viewport, ime_data); - } + g.IO.SetPlatformImeDataFn(viewport, ime_data); } // Hide implicit/fallback "Debug" window if it hasn't been used @@ -5402,7 +5410,7 @@ ImVec2 ImGui::GetItemRectSize() } // Prior to v1.90 2023/10/16, the BeginChild() function took a 'bool border = false' parameter instead of 'ImGuiChildFlags child_flags = 0'. -// ImGuiChildFlags_Border is defined as always == 1 in order to allow old code passing 'true'. +// ImGuiChildFlags_Border is defined as always == 1 in order to allow old code passing 'true'. Read comments in imgui.h for details! bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags) { ImGuiID id = GetCurrentWindow()->GetID(str_id); @@ -5544,7 +5552,7 @@ void ImGui::EndChild() // When browsing a window that has no activable items (scroll only) we keep a highlight on the child (pass g.NavId to trick into always displaying) if (child_window->DC.NavLayersActiveMask == 0 && child_window == g.NavWindow) - RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_TypeThin); + RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_Compact); } else { @@ -5669,22 +5677,25 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags) static inline ImVec2 CalcWindowMinSize(ImGuiWindow* window) { - // Popups, menus and childs bypass style.WindowMinSize by default, but we give then a non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) - // FIXME: the if/else could probably be removed, "reduce artifacts" section for all windows. + // We give windows non-zero minimum size to facilitate understanding problematic cases (e.g. empty popups) + // FIXME: Essentially we want to restrict manual resizing to WindowMinSize+Decoration, and allow api resizing to be smaller. + // Perhaps should tend further a neater test for this. ImGuiContext& g = *GImGui; ImVec2 size_min; - if (window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_ChildWindow)) + if ((window->Flags & ImGuiWindowFlags_ChildWindow) && !(window->Flags & ImGuiWindowFlags_Popup)) { size_min.x = (window->ChildFlags & ImGuiChildFlags_ResizeX) ? g.Style.WindowMinSize.x : 4.0f; size_min.y = (window->ChildFlags & ImGuiChildFlags_ResizeY) ? g.Style.WindowMinSize.y : 4.0f; } else { - ImGuiWindow* window_for_height = window; size_min.x = ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) == 0) ? g.Style.WindowMinSize.x : 4.0f; size_min.y = ((window->Flags & ImGuiWindowFlags_AlwaysAutoResize) == 0) ? g.Style.WindowMinSize.y : 4.0f; - size_min.y = ImMax(size_min.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); // Reduce artifacts with very small windows } + + // Reduce artifacts with very small windows + ImGuiWindow* window_for_height = window; + size_min.y = ImMax(size_min.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); return size_min; } @@ -5754,7 +5765,7 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont { // Maximum window size is determined by the viewport size or monitor size ImVec2 size_min = CalcWindowMinSize(window); - ImVec2 size_max = (window->Flags & ImGuiWindowFlags_ChildWindow) ? ImVec2(FLT_MAX, FLT_MAX) : ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f; + ImVec2 size_max = ((window->Flags & ImGuiWindowFlags_ChildWindow) && !(window->Flags & ImGuiWindowFlags_Popup)) ? ImVec2(FLT_MAX, FLT_MAX) : ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f; ImVec2 size_auto_fit = ImClamp(size_desired, size_min, size_max); // When the window cannot fit all contents (either because of constraints, either because screen is too small), @@ -6435,21 +6446,22 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window_stack_data.StackSizesOnBegin.SetToContextState(&g); g.CurrentWindowStack.push_back(window_stack_data); if (flags & ImGuiWindowFlags_ChildMenu) - g.BeginMenuCount++; + g.BeginMenuDepth++; // Update ->RootWindow and others pointers (before any possible call to FocusWindow) if (first_begin_of_the_frame) { UpdateWindowParentAndRootLinks(window, flags, parent_window); window->ParentWindowInBeginStack = parent_window_in_stack; + + // There's little point to expose a flag to set this: because the interesting cases won't be using parent_window_in_stack, + // e.g. linking a tool window in a standalone viewport to a document window, regardless of their Begin() stack parenting. (#6798) + window->ParentWindowForFocusRoute = (flags & ImGuiWindowFlags_ChildWindow) ? parent_window_in_stack : NULL; } // Add to focus scope stack - // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() - if ((flags & ImGuiWindowFlags_NavFlattened) == 0) - PushFocusScope(window->ID); + PushFocusScope((flags & ImGuiWindowFlags_NavFlattened) ? g.CurrentFocusScopeId : window->ID); window->NavRootFocusScopeId = g.CurrentFocusScopeId; - g.CurrentWindow = NULL; // Add to popup stack if (flags & ImGuiWindowFlags_Popup) @@ -6515,6 +6527,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window->Appearing) SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false); + // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() + g.CurrentWindow = NULL; + // When reusing window again multiple times a frame, just append content (don't need to setup again) if (first_begin_of_the_frame) { @@ -6746,7 +6761,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Handle manual resize: Resize Grips, Borders, Gamepad int border_hovered = -1, border_held = -1; ImU32 resize_grip_col[4] = {}; - const int resize_grip_count = (window->Flags & ImGuiWindowFlags_ChildWindow) ? 0 : g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. + const int resize_grip_count = ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_Popup)) ? 0 : g.IO.ConfigWindowsResizeFromEdges ? 2 : 1; // Allow resize from lower-left if we have the mouse cursor feedback for it. const float resize_grip_draw_size = IM_TRUNC(ImMax(g.FontSize * 1.10f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); if (!window->Collapsed) if (int auto_fit_mask = UpdateWindowManualResize(window, size_auto_fit, &border_hovered, &border_held, resize_grip_count, &resize_grip_col[0], visibility_rect)) @@ -6946,6 +6961,8 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.TextWrapPos = -1.0f; // disabled window->DC.ItemWidthStack.resize(0); window->DC.TextWrapPosStack.resize(0); + if (flags & ImGuiWindowFlags_Modal) + window->DC.ModalDimBgColor = ColorConvertFloat4ToU32(GetStyleColorVec4(ImGuiCol_ModalWindowDimBg)); if (window->AutoFitFramesX > 0) window->AutoFitFramesX--; @@ -7057,12 +7074,15 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // [DEBUG] io.ConfigDebugBeginReturnValue override return value to test Begin/End and BeginChild/EndChild behaviors. // (The implicit fallback window is NOT automatically ended allowing it to always be able to receive commands without crashing) - if (!window->IsFallbackWindow && ((g.IO.ConfigDebugBeginReturnValueOnce && window_just_created) || (g.IO.ConfigDebugBeginReturnValueLoop && g.DebugBeginReturnValueCullDepth == g.CurrentWindowStack.Size))) - { - if (window->AutoFitFramesX > 0) { window->AutoFitFramesX++; } - if (window->AutoFitFramesY > 0) { window->AutoFitFramesY++; } - return false; - } +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (!window->IsFallbackWindow) + if ((g.IO.ConfigDebugBeginReturnValueOnce && window_just_created) || (g.IO.ConfigDebugBeginReturnValueLoop && g.DebugBeginReturnValueCullDepth == g.CurrentWindowStack.Size)) + { + if (window->AutoFitFramesX > 0) { window->AutoFitFramesX++; } + if (window->AutoFitFramesY > 0) { window->AutoFitFramesY++; } + return false; + } +#endif return !window->SkipItems; } @@ -7088,8 +7108,7 @@ void ImGui::End() if (window->DC.CurrentColumns) EndColumns(); PopClipRect(); // Inner window clip rectangle - if ((window->Flags & ImGuiWindowFlags_NavFlattened) == 0) - PopFocusScope(); + PopFocusScope(); // Stop logging if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging @@ -7101,7 +7120,7 @@ void ImGui::End() // Pop from window stack g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; if (window->Flags & ImGuiWindowFlags_ChildMenu) - g.BeginMenuCount--; + g.BeginMenuDepth--; if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithContextState(&g); @@ -7214,7 +7233,7 @@ void ImGui::FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags) g.NavMousePosDirty = true; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId g.NavLayer = ImGuiNavLayer_Main; - g.NavFocusScopeId = window ? window->NavRootFocusScopeId : 0; + SetNavFocusScope(window ? window->NavRootFocusScopeId : 0); g.NavIdIsAlive = false; g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; @@ -7808,16 +7827,50 @@ void ImGui::SetWindowFontScale(float scale) void ImGui::PushFocusScope(ImGuiID id) { ImGuiContext& g = *GImGui; - g.FocusScopeStack.push_back(id); + ImGuiFocusScopeData data; + data.ID = id; + data.WindowID = g.CurrentWindow->ID; + g.FocusScopeStack.push_back(data); g.CurrentFocusScopeId = id; } void ImGui::PopFocusScope() { ImGuiContext& g = *GImGui; - IM_ASSERT(g.FocusScopeStack.Size > 0); // Too many PopFocusScope() ? + if (g.FocusScopeStack.Size == 0) + { + IM_ASSERT_USER_ERROR(g.FocusScopeStack.Size > 0, "Calling PopFocusScope() too many times!"); + return; + } g.FocusScopeStack.pop_back(); - g.CurrentFocusScopeId = g.FocusScopeStack.Size ? g.FocusScopeStack.back() : 0; + g.CurrentFocusScopeId = g.FocusScopeStack.Size ? g.FocusScopeStack.back().ID : 0; +} + +void ImGui::SetNavFocusScope(ImGuiID focus_scope_id) +{ + ImGuiContext& g = *GImGui; + g.NavFocusScopeId = focus_scope_id; + g.NavFocusRoute.resize(0); // Invalidate + if (focus_scope_id == 0) + return; + IM_ASSERT(g.NavWindow != NULL); + + // Store current path (in reverse order) + if (focus_scope_id == g.CurrentFocusScopeId) + { + // Top of focus stack contains local focus scopes inside current window + for (int n = g.FocusScopeStack.Size - 1; n >= 0 && g.FocusScopeStack.Data[n].WindowID == g.CurrentWindow->ID; n--) + g.NavFocusRoute.push_back(g.FocusScopeStack.Data[n]); + } + else if (focus_scope_id == g.NavWindow->NavRootFocusScopeId) + g.NavFocusRoute.push_back({ focus_scope_id, g.NavWindow->ID }); + else + return; + + // Then follow on manually set ParentWindowForFocusRoute field (#6798) + for (ImGuiWindow* window = g.NavWindow->ParentWindowForFocusRoute; window != NULL; window = window->ParentWindowForFocusRoute) + g.NavFocusRoute.push_back({ window->NavRootFocusScopeId, window->ID }); + IM_ASSERT(g.NavFocusRoute.Size < 100); // Maximum depth is technically 251 as per CalcRoutingScore(): 254 - 3 } // Focus = move navigation cursor, set scrolling, set focus window. @@ -8076,6 +8129,26 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) // - Shortcut() [Internal] //----------------------------------------------------------------------------- +ImGuiKeyChord ImGui::FixupKeyChord(ImGuiContext* ctx, ImGuiKeyChord key_chord) +{ + // Convert ImGuiMod_Shortcut and add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + if (IsModKey(key)) + { + if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) + key_chord |= ImGuiMod_Ctrl; + if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) + key_chord |= ImGuiMod_Shift; + if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) + key_chord |= ImGuiMod_Alt; + if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) + key_chord |= ImGuiMod_Super; + } + if (key_chord & ImGuiMod_Shortcut) + return (key_chord & ~ImGuiMod_Shortcut) | (ctx->IO.ConfigMacOSXBehaviors ? ImGuiMod_Super : ImGuiMod_Ctrl); + return key_chord; +} + ImGuiKeyData* ImGui::GetKeyData(ImGuiContext* ctx, ImGuiKey key) { ImGuiContext& g = *ctx; @@ -8156,18 +8229,17 @@ const char* ImGui::GetKeyName(ImGuiKey key) } // ImGuiMod_Shortcut is translated to either Ctrl or Super. -const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord, char* out_buf, int out_buf_size) +const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord) { ImGuiContext& g = *GImGui; - if (key_chord & ImGuiMod_Shortcut) - key_chord = ConvertShortcutMod(key_chord); - ImFormatString(out_buf, (size_t)out_buf_size, "%s%s%s%s%s", + key_chord = FixupKeyChord(&g, key_chord); + ImFormatString(g.TempKeychordName, IM_ARRAYSIZE(g.TempKeychordName), "%s%s%s%s%s", (key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "", (key_chord & ImGuiMod_Shift) ? "Shift+" : "", (key_chord & ImGuiMod_Alt) ? "Alt+" : "", (key_chord & ImGuiMod_Super) ? (g.IO.ConfigMacOSXBehaviors ? "Cmd+" : "Super+") : "", GetKeyName((ImGuiKey)(key_chord & ~ImGuiMod_Mask_))); - return out_buf; + return g.TempKeychordName; } // t0 = previous time (e.g.: g.Time - g.IO.DeltaTime) @@ -8234,6 +8306,7 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) for (int old_routing_idx = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; old_routing_idx != -1; old_routing_idx = routing_entry->NextEntryIndex) { routing_entry = &rt->Entries[old_routing_idx]; + routing_entry->RoutingCurrScore = routing_entry->RoutingNextScore; routing_entry->RoutingCurr = routing_entry->RoutingNext; // Update entry routing_entry->RoutingNext = ImGuiKeyOwner_None; routing_entry->RoutingNextScore = 255; @@ -8280,13 +8353,11 @@ ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) ImGuiContext& g = *GImGui; ImGuiKeyRoutingTable* rt = &g.KeysRoutingTable; ImGuiKeyRoutingData* routing_data; - if (key_chord & ImGuiMod_Shortcut) - key_chord = ConvertShortcutMod(key_chord); ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); if (key == ImGuiKey_None) key = ConvertSingleModFlagToKey(&g, mods); - IM_ASSERT(IsNamedKey(key)); + IM_ASSERT(IsNamedKey(key) && (key_chord & ImGuiMod_Shortcut) == 0); // Please call ConvertShortcutMod() in calling function. // Get (in the majority of case, the linked list will have one element so this should be 2 reads. // Subsequent elements will be contiguous in memory as list is sorted/rebuilt in NewFrame). @@ -8315,37 +8386,30 @@ ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) // - 254: ImGuiInputFlags_RouteGlobalLow // - 255: never route // 'flags' should include an explicit routing policy -static int CalcRoutingScore(ImGuiWindow* location, ImGuiID owner_id, ImGuiInputFlags flags) +static int CalcRoutingScore(ImGuiID focus_scope_id, ImGuiID owner_id, ImGuiInputFlags flags) { if (flags & ImGuiInputFlags_RouteFocused) { ImGuiContext& g = *GImGui; - ImGuiWindow* focused = g.NavWindow; // ActiveID gets top priority // (we don't check g.ActiveIdUsingAllKeys here. Routing is applied but if input ownership is tested later it may discard it) if (owner_id != 0 && g.ActiveId == owner_id) return 1; - // Early out when not in focus stack - if (focused == NULL || focused->RootWindow != location->RootWindow) - return 255; - // Score based on distance to focused window (lower is better) // Assuming both windows are submitting a routing request, // - When Window....... is focused -> Window scores 3 (best), Window/ChildB scores 255 (no match) // - When Window/ChildB is focused -> Window scores 4, Window/ChildB scores 3 (best) // Assuming only WindowA is submitting a routing request, // - When Window/ChildB is focused -> Window scores 4 (best), Window/ChildB doesn't have a score. - for (int next_score = 3; focused != NULL; next_score++) - { - if (focused == location) - { - IM_ASSERT(next_score < 255); - return next_score; - } - focused = (focused->RootWindow != focused) ? focused->ParentWindow : NULL; // FIXME: This could be later abstracted as a focus path - } + // This essentially follow the window->ParentWindowForFocusRoute chain. + if (focus_scope_id == 0) + return 255; + for (int index_in_focus_path = 0; index_in_focus_path < g.NavFocusRoute.Size; index_in_focus_path++) + if (g.NavFocusRoute.Data[index_in_focus_path].ID == focus_scope_id) + return 3 + index_in_focus_path; + return 255; } @@ -8357,13 +8421,29 @@ static int CalcRoutingScore(ImGuiWindow* location, ImGuiID owner_id, ImGuiInputF return 0; } +// We need this to filter some Shortcut() routes when an item e.g. an InputText() is active +// e.g. ImGuiKey_G won't be considered a shortcut when item is active, but ImGuiMod|ImGuiKey_G can be. +static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord) +{ + // Mimic 'ignore_char_inputs' logic in InputText() + ImGuiContext& g = *GImGui; + + // When the right mods are pressed it cannot be a char input so we won't filter the shortcut out. + ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); + const bool ignore_char_inputs = ((mods & ImGuiMod_Ctrl) && !(mods & ImGuiMod_Alt)) || (g.IO.ConfigMacOSXBehaviors && (mods & ImGuiMod_Super)); + if (ignore_char_inputs) + return false; + + // Return true for A-Z, 0-9 and other keys associated to char inputs. Other keys such as F1-F12 won't be filtered. + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + return g.KeysMayBeCharInput.TestBit(key); +} + // Request a desired route for an input chord (key + mods). // Return true if the route is available this frame. // - Routes and key ownership are attributed at the beginning of next frame based on best score and mod state. // (Conceptually this does a "Submit for next frame" + "Test for current frame". // As such, it could be called TrySetXXX or SubmitXXX, or the Submit and Test operations should be separate.) -// - Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default) -// - Using 'owner_id == ImGuiKeyOwner_None': allows disabling/locking a shortcut. bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; @@ -8371,6 +8451,10 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI flags |= ImGuiInputFlags_RouteGlobalHigh; // IMPORTANT: This is the default for SetShortcutRouting() but NOT Shortcut() else IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteMask_)); // Check that only 1 routing flag is used + IM_ASSERT(owner_id != ImGuiKeyOwner_Any && owner_id != ImGuiKeyOwner_None); + + // Convert ImGuiMod_Shortcut and add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. + key_chord = FixupKeyChord(&g, key_chord); // [DEBUG] Debug break requested by user if (g.DebugBreakInShortcutRouting == key_chord) @@ -8379,34 +8463,68 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI if (flags & ImGuiInputFlags_RouteUnlessBgFocused) if (g.NavWindow == NULL) return false; + // Note how ImGuiInputFlags_RouteAlways won't set routing and thus won't set owner. May want to rework this? if (flags & ImGuiInputFlags_RouteAlways) + { + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> always\n", GetKeyChordName(key_chord), owner_id, flags); return true; + } + + // Specific culling when there's an active. + if (g.ActiveId != 0 && g.ActiveId != owner_id) + { + // Cull shortcuts with no modifiers when it could generate a character. + // e.g. Shortcut(ImGuiKey_G) also generates 'g' character, should not trigger when InputText() is active. + // but Shortcut(Ctrl+G) should generally trigger when InputText() is active. + // TL;DR: lettered shortcut with no mods or with only Alt mod will not trigger while an item reading text input is active. + // (We cannot filter based on io.InputQueueCharacters[] contents because of trickling and key<>chars submission order are undefined) + if ((flags & ImGuiInputFlags_RouteFocused) && g.IO.WantTextInput && IsKeyChordPotentiallyCharInput(key_chord)) + { + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> filtered as potential char input\n", GetKeyChordName(key_chord), owner_id, flags); + return false; + } - const int score = CalcRoutingScore(g.CurrentWindow, owner_id, flags); + // ActiveIdUsingAllKeyboardKeys trumps all for ActiveId + if ((flags & ImGuiInputFlags_RouteGlobalHigh) == 0 && g.ActiveIdUsingAllKeyboardKeys) + { + ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + if (key == ImGuiKey_None) + key = ConvertSingleModFlagToKey(&g, (ImGuiKey)(key_chord & ImGuiMod_Mask_)); + if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) + return false; + } + } + + // FIXME-SHORTCUT: A way to configure the location/focus-scope to test would render this more flexible. + const int score = CalcRoutingScore(g.CurrentFocusScopeId, owner_id, flags); + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> score %d\n", GetKeyChordName(key_chord), owner_id, flags, score); if (score == 255) return false; // Submit routing for NEXT frame (assuming score is sufficient) // FIXME: Could expose a way to use a "serve last" policy for same score resolution (using <= instead of <). ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord); - const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id); //const bool set_route = (flags & ImGuiInputFlags_ServeLast) ? (score <= routing_data->RoutingNextScore) : (score < routing_data->RoutingNextScore); if (score < routing_data->RoutingNextScore) { - routing_data->RoutingNext = routing_id; + routing_data->RoutingNext = owner_id; routing_data->RoutingNextScore = (ImU8)score; } // Return routing state for CURRENT frame - return routing_data->RoutingCurr == routing_id; + if (routing_data->RoutingCurr == owner_id) + IMGUI_DEBUG_LOG_INPUTROUTING("--> granting current route\n"); + return routing_data->RoutingCurr == owner_id; } // Currently unused by core (but used by tests) // Note: this cannot be turned into GetShortcutRouting() because we do the owner_id->routing_id translation, name would be more misleading. bool ImGui::TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id) { + ImGuiContext& g = *GImGui; const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id); + key_chord = FixupKeyChord(&g, key_chord); ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord); // FIXME: Could avoid creating entry. return routing_data->RoutingCurr == routing_id; } @@ -9337,8 +9455,7 @@ bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord) bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; - if (key_chord & ImGuiMod_Shortcut) - key_chord = ConvertShortcutMod(key_chord); + key_chord = FixupKeyChord(&g, key_chord); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); if (g.IO.KeyMods != mods) return false; @@ -9352,6 +9469,13 @@ bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiIn return true; } +void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord) +{ + ImGuiContext& g = *GImGui; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasShortcut; + g.NextItemData.Shortcut = key_chord; +} + bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) { //ImGuiContext& g = *GImGui; @@ -9360,12 +9484,19 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags // When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any. if ((flags & ImGuiInputFlags_RouteMask_) == 0) flags |= ImGuiInputFlags_RouteFocused; + + // Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default) + // Effectively makes Shortcut() always input-owner aware. + if (owner_id == ImGuiKeyOwner_Any || owner_id == ImGuiKeyOwner_None) + owner_id = GetRoutingIdFromOwnerId(owner_id); + + // Submit route if (!SetShortcutRouting(key_chord, owner_id, flags)) return false; // Default repeat behavior for Shortcut() // So e.g. pressing Ctrl+W and releasing Ctrl while holding W will not trigger the W shortcut. - if ((flags & ImGuiInputFlags_RepeatUntilMask_) == 0) + if ((flags & ImGuiInputFlags_Repeat) != 0 && (flags & ImGuiInputFlags_RepeatUntilMask_) == 0) flags |= ImGuiInputFlags_RepeatUntilKeyModsChange; if (!IsKeyChordPressed(key_chord, owner_id, flags)) @@ -9651,77 +9782,36 @@ void ImGuiStackSizes::CompareWithContextState(ImGuiContext* ctx) IM_ASSERT(SizeOfFocusScopeStack == g.FocusScopeStack.Size && "PushFocusScope/PopFocusScope Mismatch!"); } - //----------------------------------------------------------------------------- -// [SECTION] LAYOUT +// [SECTION] ITEM SUBMISSION //----------------------------------------------------------------------------- -// - ItemSize() +// - KeepAliveID() +// - ItemHandleShortcut() [Internal] // - ItemAdd() -// - SameLine() -// - GetCursorScreenPos() -// - SetCursorScreenPos() -// - GetCursorPos(), GetCursorPosX(), GetCursorPosY() -// - SetCursorPos(), SetCursorPosX(), SetCursorPosY() -// - GetCursorStartPos() -// - Indent() -// - Unindent() -// - SetNextItemWidth() -// - PushItemWidth() -// - PushMultiItemsWidths() -// - PopItemWidth() -// - CalcItemWidth() -// - CalcItemSize() -// - GetTextLineHeight() -// - GetTextLineHeightWithSpacing() -// - GetFrameHeight() -// - GetFrameHeightWithSpacing() -// - GetContentRegionMax() -// - GetContentRegionMaxAbs() [Internal] -// - GetContentRegionAvail(), -// - GetWindowContentRegionMin(), GetWindowContentRegionMax() -// - BeginGroup() -// - EndGroup() -// Also see in imgui_widgets: tab bars, and in imgui_tables: tables, columns. //----------------------------------------------------------------------------- -// Advance cursor given item size for layout. -// Register minimum needed size so it can extend the bounding box used for auto-fit calculation. -// See comments in ItemAdd() about how/why the size provided to ItemSize() vs ItemAdd() may often different. -// THIS IS IN THE PERFORMANCE CRITICAL PATH. -void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) +// Code not using ItemAdd() may need to call this manually otherwise ActiveId will be cleared. In IMGUI_VERSION_NUM < 18717 this was called by GetID(). +void ImGui::KeepAliveID(ImGuiID id) { ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (window->SkipItems) - return; - - // We increase the height in this function to accommodate for baseline offset. - // In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor, - // but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect. - const float offset_to_match_baseline_y = (text_baseline_y >= 0) ? ImMax(0.0f, window->DC.CurrLineTextBaseOffset - text_baseline_y) : 0.0f; - - const float line_y1 = window->DC.IsSameLine ? window->DC.CursorPosPrevLine.y : window->DC.CursorPos.y; - const float line_height = ImMax(window->DC.CurrLineSize.y, /*ImMax(*/window->DC.CursorPos.y - line_y1/*, 0.0f)*/ + size.y + offset_to_match_baseline_y); - - // Always align ourselves on pixel boundaries - //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] - window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; - window->DC.CursorPosPrevLine.y = line_y1; - window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line - window->DC.CursorPos.y = IM_TRUNC(line_y1 + line_height + g.Style.ItemSpacing.y); // Next line - window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); - window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); - //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] - - window->DC.PrevLineSize.y = line_height; - window->DC.CurrLineSize.y = 0.0f; - window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); - window->DC.CurrLineTextBaseOffset = 0.0f; - window->DC.IsSameLine = window->DC.IsSetPos = false; + if (g.ActiveId == id) + g.ActiveIdIsAlive = id; + if (g.ActiveIdPreviousFrame == id) + g.ActiveIdPreviousFrameIsAlive = true; +} - // Horizontal layout mode - if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) - SameLine(); +static void ItemHandleShortcut(ImGuiID id) +{ + // FIXME: Generalize Activation queue? + ImGuiContext& g = *GImGui; + if (ImGui::Shortcut(g.NextItemData.Shortcut, id, ImGuiInputFlags_None) && g.NavActivateId == 0) + { + g.NavActivateId = id; // Will effectively disable clipping. + g.NavActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_FromShortcut; + if (g.ActiveId == 0 || g.ActiveId == id) + g.NavActivateDownId = g.NavActivatePressedId = id; + ImGui::NavHighlightActivated(id); + } } // Declare item bounding box for clipping and interaction. @@ -9765,6 +9855,9 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) NavProcessItem(); } + + if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasShortcut) + ItemHandleShortcut(id); } // Lightweight clear of SetNextItemXXX data. @@ -9781,9 +9874,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu //const bool is_clipped = IsClippedEx(bb, id); //if (is_clipped) // return false; + // g.NavActivateId is not necessarily == g.NavId, in the case of remote activation (e.g. shortcuts) const bool is_rect_visible = bb.Overlaps(window->ClipRect); if (!is_rect_visible) - if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId)) + if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId && id != g.NavActivateId)) if (!g.LogEnabled) return false; @@ -9812,6 +9906,78 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu return true; } + +//----------------------------------------------------------------------------- +// [SECTION] LAYOUT +//----------------------------------------------------------------------------- +// - ItemSize() +// - SameLine() +// - GetCursorScreenPos() +// - SetCursorScreenPos() +// - GetCursorPos(), GetCursorPosX(), GetCursorPosY() +// - SetCursorPos(), SetCursorPosX(), SetCursorPosY() +// - GetCursorStartPos() +// - Indent() +// - Unindent() +// - SetNextItemWidth() +// - PushItemWidth() +// - PushMultiItemsWidths() +// - PopItemWidth() +// - CalcItemWidth() +// - CalcItemSize() +// - GetTextLineHeight() +// - GetTextLineHeightWithSpacing() +// - GetFrameHeight() +// - GetFrameHeightWithSpacing() +// - GetContentRegionMax() +// - GetContentRegionMaxAbs() [Internal] +// - GetContentRegionAvail(), +// - GetWindowContentRegionMin(), GetWindowContentRegionMax() +// - BeginGroup() +// - EndGroup() +// Also see in imgui_widgets: tab bars, and in imgui_tables: tables, columns. +//----------------------------------------------------------------------------- + +// Advance cursor given item size for layout. +// Register minimum needed size so it can extend the bounding box used for auto-fit calculation. +// See comments in ItemAdd() about how/why the size provided to ItemSize() vs ItemAdd() may often different. +// THIS IS IN THE PERFORMANCE CRITICAL PATH. +void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->SkipItems) + return; + + // We increase the height in this function to accommodate for baseline offset. + // In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor, + // but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect. + const float offset_to_match_baseline_y = (text_baseline_y >= 0) ? ImMax(0.0f, window->DC.CurrLineTextBaseOffset - text_baseline_y) : 0.0f; + + const float line_y1 = window->DC.IsSameLine ? window->DC.CursorPosPrevLine.y : window->DC.CursorPos.y; + const float line_height = ImMax(window->DC.CurrLineSize.y, /*ImMax(*/window->DC.CursorPos.y - line_y1/*, 0.0f)*/ + size.y + offset_to_match_baseline_y); + + // Always align ourselves on pixel boundaries + //if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG] + window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x + size.x; + window->DC.CursorPosPrevLine.y = line_y1; + window->DC.CursorPos.x = IM_TRUNC(window->Pos.x + window->DC.Indent.x + window->DC.ColumnsOffset.x); // Next line + window->DC.CursorPos.y = IM_TRUNC(line_y1 + line_height + g.Style.ItemSpacing.y); // Next line + window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPosPrevLine.x); + window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y); + //if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG] + + window->DC.PrevLineSize.y = line_height; + window->DC.CurrLineSize.y = 0.0f; + window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y); + window->DC.CurrLineTextBaseOffset = 0.0f; + window->DC.IsSameLine = window->DC.IsSetPos = false; + + // Horizontal layout mode + if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) + SameLine(); +} + // Gets back to previous line and continue with horizontal layout // offset_from_start_x == 0 : follow right after previous item // offset_from_start_x != 0 : align to specified x position (relative to window/group left) @@ -10621,17 +10787,23 @@ void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags) } else { - // Gently handle the user mistakenly calling OpenPopup() every frame. It is a programming mistake! However, if we were to run the regular code path, the ui - // would become completely unusable because the popup will always be in hidden-while-calculating-size state _while_ claiming focus. Which would be a very confusing - // situation for the programmer. Instead, we silently allow the popup to proceed, it will keep reappearing and the programming error will be more obvious to understand. - if (g.OpenPopupStack[current_stack_size].PopupId == id && g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1) - { + // Gently handle the user mistakenly calling OpenPopup() every frames: it is likely a programming mistake! + // However, if we were to run the regular code path, the ui would become completely unusable because the popup will always be + // in hidden-while-calculating-size state _while_ claiming focus. Which is extremely confusing situation for the programmer. + // Instead, for successive frames calls to OpenPopup(), we silently avoid reopening even if ImGuiPopupFlags_NoReopen is not specified. + bool keep_existing = false; + if (g.OpenPopupStack[current_stack_size].PopupId == id) + if ((g.OpenPopupStack[current_stack_size].OpenFrameCount == g.FrameCount - 1) || (popup_flags & ImGuiPopupFlags_NoReopen)) + keep_existing = true; + if (keep_existing) + { + // No reopen g.OpenPopupStack[current_stack_size].OpenFrameCount = popup_ref.OpenFrameCount; } else { - // Close child popups if any, then flag popup for open/reopen - ClosePopupToLevel(current_stack_size, false); + // Reopen: close child popups if any, then flag popup for open/reopen (set position, focus, init navigation) + ClosePopupToLevel(current_stack_size, true); g.OpenPopupStack.push_back(popup_ref); } @@ -10661,14 +10833,15 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to if (!popup.Window) continue; IM_ASSERT((popup.Window->Flags & ImGuiWindowFlags_Popup) != 0); - if (popup.Window->Flags & ImGuiWindowFlags_ChildWindow) - continue; // Trim the stack unless the popup is a direct parent of the reference window (the reference window is often the NavWindow) - // - With this stack of window, clicking/focusing Popup1 will close Popup2 and Popup3: - // Window -> Popup1 -> Popup2 -> Popup3 + // - Clicking/Focusing Window2 won't close Popup1: + // Window -> Popup1 -> Window2(Ref) + // - Clicking/focusing Popup1 will close Popup2 and Popup3: + // Window -> Popup1(Ref) -> Popup2 -> Popup3 // - Each popups may contain child windows, which is why we compare ->RootWindow! // Window -> Popup1 -> Popup1_Child -> Popup2 -> Popup2_Child + // We step through every popup from bottom to top to validate their position relative to reference window. bool ref_window_is_descendent_of_popup = false; for (int n = popup_count_to_keep; n < g.OpenPopupStack.Size; n++) if (ImGuiWindow* popup_window = g.OpenPopupStack[n].Window) @@ -10767,7 +10940,7 @@ bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) char name[20]; if (flags & ImGuiWindowFlags_ChildMenu) - ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginMenuCount); // Recycle windows based on depth + ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginMenuDepth); // Recycle windows based on depth else ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame @@ -10776,6 +10949,8 @@ bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display) EndPopup(); + //g.CurrentWindow->FocusRouteParentWindow = g.CurrentWindow->ParentWindowInBeginStack; + return is_open; } @@ -11077,6 +11252,13 @@ void ImGui::SetNavWindow(ImGuiWindow* window) NavUpdateAnyRequestFlag(); } +void ImGui::NavHighlightActivated(ImGuiID id) +{ + ImGuiContext& g = *GImGui; + g.NavHighlightActivatedId = id; + g.NavHighlightActivatedTimer = NAV_ACTIVATE_HIGHLIGHT_TIMER; +} + void ImGui::NavClearPreferredPosForAxis(ImGuiAxis axis) { ImGuiContext& g = *GImGui; @@ -11090,7 +11272,7 @@ void ImGui::SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id IM_ASSERT(nav_layer == ImGuiNavLayer_Main || nav_layer == ImGuiNavLayer_Menu); g.NavId = id; g.NavLayer = nav_layer; - g.NavFocusScopeId = focus_scope_id; + SetNavFocusScope(focus_scope_id); g.NavWindow->NavLastIds[nav_layer] = id; g.NavWindow->NavRectRel[nav_layer] = rect_rel; @@ -11112,7 +11294,7 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) const ImGuiNavLayer nav_layer = window->DC.NavLayerCurrent; g.NavId = id; g.NavLayer = nav_layer; - g.NavFocusScopeId = g.CurrentFocusScopeId; + SetNavFocusScope(g.CurrentFocusScopeId); window->NavLastIds[nav_layer] = id; if (g.LastItemData.ID == id) window->NavRectRel[nav_layer] = WindowRectAbsToRel(window, g.LastItemData.NavRect); @@ -11344,23 +11526,26 @@ static void ImGui::NavProcessItem() // FIXME-NAV: Consider policy for double scoring (scoring from NavScoringRect + scoring from a rect wrapped according to current wrapping policy) if (g.NavMoveScoringItems && (item_flags & ImGuiItemFlags_Disabled) == 0) { - const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) != 0; - if (is_tabbing) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_FocusApi) || (window->Flags & ImGuiWindowFlags_NoNavInputs) == 0) { - NavProcessItemForTabbingRequest(id, item_flags, g.NavMoveFlags); - } - else if (g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) - { - ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; - if (NavScoreItem(result)) - NavApplyItemToResult(result); - - // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. - const float VISIBLE_RATIO = 0.70f; - if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) - if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) - if (NavScoreItem(&g.NavMoveResultLocalVisible)) - NavApplyItemToResult(&g.NavMoveResultLocalVisible); + const bool is_tabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) != 0; + if (is_tabbing) + { + NavProcessItemForTabbingRequest(id, item_flags, g.NavMoveFlags); + } + else if (g.NavId != id || (g.NavMoveFlags & ImGuiNavMoveFlags_AllowCurrentNavId)) + { + ImGuiNavItemData* result = (window == g.NavWindow) ? &g.NavMoveResultLocal : &g.NavMoveResultOther; + if (NavScoreItem(result)) + NavApplyItemToResult(result); + + // Features like PageUp/PageDown need to maintain a separate score for the visible set of items. + const float VISIBLE_RATIO = 0.70f; + if ((g.NavMoveFlags & ImGuiNavMoveFlags_AlsoScoreVisibleSet) && window->ClipRect.Overlaps(nav_bb)) + if (ImClamp(nav_bb.Max.y, window->ClipRect.Min.y, window->ClipRect.Max.y) - ImClamp(nav_bb.Min.y, window->ClipRect.Min.y, window->ClipRect.Max.y) >= (nav_bb.Max.y - nav_bb.Min.y) * VISIBLE_RATIO) + if (NavScoreItem(&g.NavMoveResultLocalVisible)) + NavApplyItemToResult(&g.NavMoveResultLocalVisible); + } } } @@ -11370,6 +11555,7 @@ static void ImGui::NavProcessItem() if (g.NavWindow != window) SetNavWindow(window); // Always refresh g.NavWindow, because some operations such as FocusItem() may not have a window. g.NavLayer = window->DC.NavLayerCurrent; + SetNavFocusScope(g.CurrentFocusScopeId); // Will set g.NavFocusScopeId AND store g.NavFocusScopePath g.NavFocusScopeId = g.CurrentFocusScopeId; g.NavIdIsAlive = true; if (g.LastItemData.InFlags & ImGuiItemFlags_HasSelectionUserData) @@ -11393,10 +11579,12 @@ void ImGui::NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flag ImGuiContext& g = *GImGui; if ((move_flags & ImGuiNavMoveFlags_FocusApi) == 0) + { if (g.NavLayer != g.CurrentWindow->DC.NavLayerCurrent) return; - if (g.NavFocusScopeId != g.CurrentFocusScopeId) - return; + if (g.NavFocusScopeId != g.CurrentFocusScopeId) + return; + } // - Can always land on an item when using API call. // - Tabbing with _NavEnableKeyboard (space/enter/arrows): goes through every item. @@ -11597,7 +11785,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) if (window->Flags & ImGuiWindowFlags_NoNavInputs) { g.NavId = 0; - g.NavFocusScopeId = window->NavRootFocusScopeId; + SetNavFocusScope(window->NavRootFocusScopeId); return; } @@ -11616,7 +11804,7 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) else { g.NavId = window->NavLastIds[0]; - g.NavFocusScopeId = window->NavRootFocusScopeId; + SetNavFocusScope(window->NavRootFocusScopeId); } } @@ -11737,10 +11925,10 @@ static void ImGui::NavUpdate() g.NavActivateFlags = ImGuiActivateFlags_None; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { - const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate)); - const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, false)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, false))); - const bool input_down = (nav_keyboard_active && (IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_KeypadEnter))) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput)); - const bool input_pressed = input_down && ((nav_keyboard_active && (IsKeyPressed(ImGuiKey_Enter, false) || IsKeyPressed(ImGuiKey_KeypadEnter, false))) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, false))); + const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space, ImGuiKeyOwner_None)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_None)); + const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, ImGuiKeyOwner_None)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_None))); + const bool input_down = (nav_keyboard_active && (IsKeyDown(ImGuiKey_Enter, ImGuiKeyOwner_None) || IsKeyDown(ImGuiKey_KeypadEnter, ImGuiKeyOwner_None))) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput, ImGuiKeyOwner_None)); + const bool input_pressed = input_down && ((nav_keyboard_active && (IsKeyPressed(ImGuiKey_Enter, ImGuiKeyOwner_None) || IsKeyPressed(ImGuiKey_KeypadEnter, ImGuiKeyOwner_None))) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, ImGuiKeyOwner_None))); if (g.ActiveId == 0 && activate_pressed) { g.NavActivateId = g.NavId; @@ -11754,13 +11942,22 @@ static void ImGui::NavUpdate() if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && (activate_down || input_down)) g.NavActivateDownId = g.NavId; if ((g.ActiveId == 0 || g.ActiveId == g.NavId) && (activate_pressed || input_pressed)) + { g.NavActivatePressedId = g.NavId; + NavHighlightActivated(g.NavId); + } } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) g.NavDisableHighlight = true; if (g.NavActivateId != 0) IM_ASSERT(g.NavActivateDownId == g.NavActivateId); + // Highlight + if (g.NavHighlightActivatedTimer > 0.0f) + g.NavHighlightActivatedTimer = ImMax(0.0f, g.NavHighlightActivatedTimer - io.DeltaTime); + if (g.NavHighlightActivatedTimer == 0.0f) + g.NavHighlightActivatedId = 0; + // Process programmatic activation request // FIXME-NAV: Those should eventually be queued (unlike focus they don't cancel each others) if (g.NavNextActivateId != 0) @@ -12084,6 +12281,8 @@ void ImGui::NavMoveRequestApplyResult() g.NavWindow = result->Window; g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; } + + // FIXME: Could become optional e.g. ImGuiNavMoveFlags_NoClearActiveId if we later want to apply navigation requests without altering active input. if (g.ActiveId != result->ID) ClearActiveID(); @@ -12152,15 +12351,14 @@ static void ImGui::NavUpdateCancelRequest() NavRestoreLayer(ImGuiNavLayer_Main); NavRestoreHighlightAfterMove(); } - else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->ParentWindow) + else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->RootWindowForNav->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->RootWindowForNav->ParentWindow) { // Exit child window - ImGuiWindow* child_window = g.NavWindow; - ImGuiWindow* parent_window = g.NavWindow->ParentWindow; + ImGuiWindow* child_window = g.NavWindow->RootWindowForNav; + ImGuiWindow* parent_window = child_window->ParentWindow; IM_ASSERT(child_window->ChildId != 0); - ImRect child_rect = child_window->Rect(); FocusWindow(parent_window); - SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, WindowRectAbsToRel(parent_window, child_rect)); + SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, WindowRectAbsToRel(parent_window, child_window->Rect())); NavRestoreHighlightAfterMove(); } else if (g.OpenPopupStack.Size > 0 && g.OpenPopupStack.back().Window != NULL && !(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) @@ -12455,28 +12653,33 @@ static void ImGui::NavUpdateWindowing() } // Keyboard: Press and Release ALT to toggle menu layer - // - Testing that only Alt is tested prevents Alt+Shift or AltGR from toggling menu layer. - // - AltGR is normally Alt+Ctrl but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). But even on keyboards without AltGR we don't want Alt+Ctrl to open menu anyway. - if (nav_keyboard_active && IsKeyPressed(ImGuiMod_Alt, ImGuiKeyOwner_None)) - { - g.NavWindowingToggleLayer = true; - g.NavInputSource = ImGuiInputSource_Keyboard; - } + const ImGuiKey windowing_toggle_keys[] = { ImGuiKey_LeftAlt, ImGuiKey_RightAlt }; + for (ImGuiKey windowing_toggle_key : windowing_toggle_keys) + if (nav_keyboard_active && IsKeyPressed(windowing_toggle_key, ImGuiKeyOwner_None)) + { + g.NavWindowingToggleLayer = true; + g.NavWindowingToggleKey = windowing_toggle_key; + g.NavInputSource = ImGuiInputSource_Keyboard; + break; + } if (g.NavWindowingToggleLayer && g.NavInputSource == ImGuiInputSource_Keyboard) { // We cancel toggling nav layer when any text has been typed (generally while holding Alt). (See #370) // We cancel toggling nav layer when other modifiers are pressed. (See #4439) + // - AltGR is Alt+Ctrl on some layout but we can't reliably detect it (not all backends/systems/layout emit it as Alt+Ctrl). // We cancel toggling nav layer if an owner has claimed the key. - if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_None) == false) + if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper) + g.NavWindowingToggleLayer = false; + if (TestKeyOwner(g.NavWindowingToggleKey, ImGuiKeyOwner_None) == false || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_None) == false) g.NavWindowingToggleLayer = false; - // Apply layer toggle on release + // Apply layer toggle on Alt release // Important: as before version <18314 we lacked an explicit IO event for focus gain/loss, we also compare mouse validity to detect old backends clearing mouse pos on focus loss. - if (IsKeyReleased(ImGuiMod_Alt) && g.NavWindowingToggleLayer) + if (IsKeyReleased(g.NavWindowingToggleKey) && g.NavWindowingToggleLayer) if (g.ActiveId == 0 || g.ActiveIdAllowOverlap) if (IsMousePosValid(&io.MousePos) == IsMousePosValid(&io.MousePosPrev)) apply_toggle_layer = true; - if (!IsKeyDown(ImGuiMod_Alt)) + if (!IsKeyDown(g.NavWindowingToggleKey)) g.NavWindowingToggleLayer = false; } @@ -13789,7 +13992,7 @@ static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport*, ImGuiPlatformImeDat //----------------------------------------------------------------------------- // [SECTION] METRICS/DEBUGGER WINDOW //----------------------------------------------------------------------------- -// - RenderViewportThumbnail() [Internal] +// - DebugRenderViewportThumbnail() [Internal] // - RenderViewportsThumbnails() [Internal] // - DebugTextEncoding() // - MetricsHelpMarker() [Internal] @@ -13828,7 +14031,7 @@ void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* ImRect thumb_r = thumb_window->Rect(); ImRect title_r = thumb_window->TitleBarRect(); thumb_r = ImRect(ImTrunc(off + thumb_r.Min * scale), ImTrunc(off + thumb_r.Max * scale)); - title_r = ImRect(ImTrunc(off + title_r.Min * scale), ImTrunc(off + ImVec2(title_r.Max.x, title_r.Min.y) * scale) + ImVec2(0,5)); // Exaggerate title bar height + title_r = ImRect(ImTrunc(off + title_r.Min * scale), ImTrunc(off + ImVec2(title_r.Max.x, title_r.Min.y + title_r.GetHeight() * 3.0f) * scale)); // Exaggerate title bar height thumb_r.ClipWithFull(bb); title_r.ClipWithFull(bb); const bool window_is_focused = (g.NavWindow && thumb_window->RootWindowForTitleBarHighlight == g.NavWindow->RootWindowForTitleBarHighlight); @@ -13838,6 +14041,8 @@ void ImGui::DebugRenderViewportThumbnail(ImDrawList* draw_list, ImGuiViewportP* window->DrawList->AddText(g.Font, g.FontSize * 1.0f, title_r.Min, GetColorU32(ImGuiCol_Text, alpha_mul), thumb_window->Name, FindRenderedTextEnd(thumb_window->Name)); } draw_list->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_Border, alpha_mul)); + if (viewport->ID == g.DebugMetricsConfig.HighlightViewportID) + window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255, 255, 0, 255)); } static void RenderViewportsThumbnails() @@ -13845,13 +14050,12 @@ static void RenderViewportsThumbnails() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - // We don't display full monitor bounds (we could, but it often looks awkward), instead we display just enough to cover all of our viewports. float SCALE = 1.0f / 8.0f; - ImRect bb_full(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - for (ImGuiViewportP* viewport : g.Viewports) - bb_full.Add(viewport->GetMainRect()); + ImRect bb_full(g.Viewports[0]->Pos, g.Viewports[0]->Pos + g.Viewports[0]->Size); ImVec2 p = window->DC.CursorPos; ImVec2 off = p - bb_full.Min * SCALE; + + // Draw viewports for (ImGuiViewportP* viewport : g.Viewports) { ImRect viewport_draw_bb(off + (viewport->Pos) * SCALE, off + (viewport->Pos + viewport->Size) * SCALE); @@ -14094,7 +14298,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) MetricsHelpMarker("Will call the IM_DEBUG_BREAK() macro to break in debugger.\nWarning: If you don't have a debugger attached, this will probably crash."); if (Checkbox("Show Item Picker", &g.DebugItemPickerActive) && g.DebugItemPickerActive) DebugStartItemPicker(); - Checkbox("Show \"Debug Break\" buttons in other sections", &g.IO.ConfigDebugIsDebuggerPresent); + Checkbox("Show \"Debug Break\" buttons in other sections (io.ConfigDebugIsDebuggerPresent)", &g.IO.ConfigDebugIsDebuggerPresent); SeparatorText("Visualize"); @@ -14230,9 +14434,14 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Viewports if (TreeNode("Viewports", "Viewports (%d)", g.Viewports.Size)) { - Indent(GetTreeNodeToLabelSpacing()); - RenderViewportsThumbnails(); - Unindent(GetTreeNodeToLabelSpacing()); + SetNextItemOpen(true, ImGuiCond_Once); + if (TreeNode("Windows Minimap")) + { + RenderViewportsThumbnails(); + TreePop(); + } + cfg->HighlightViewportID = 0; + for (ImGuiViewportP* viewport : g.Viewports) DebugNodeViewport(viewport); TreePop(); @@ -14358,6 +14567,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) { ImGuiDebugAllocInfo* info = &g.DebugAllocInfo; Text("%d current allocations", info->TotalAllocCount - info->TotalFreeCount); + if (SmallButton("GC now")) { g.GcCompactAll = true; } Text("Recent frames with allocations:"); int buf_size = IM_ARRAYSIZE(info->LastEntriesBuf); for (int n = buf_size - 1; n >= 0; n--) @@ -14445,10 +14655,9 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGuiKeyRoutingTable* rt = &g.KeysRoutingTable; for (ImGuiKeyRoutingIndex idx = rt->Index[key - ImGuiKey_NamedKey_BEGIN]; idx != -1; ) { - char key_chord_name[64]; ImGuiKeyRoutingData* routing_data = &rt->Entries[idx]; ImGuiKeyChord key_chord = key | routing_data->Mods; - Text("%s: 0x%08X", GetKeyChordName(key_chord, key_chord_name, IM_ARRAYSIZE(key_chord_name)), routing_data->RoutingCurr); + Text("%s: 0x%08X (scored %d)", GetKeyChordName(key_chord), routing_data->RoutingCurr, routing_data->RoutingCurrScore); DebugLocateItemOnHover(routing_data->RoutingCurr); if (g.IO.ConfigDebugIsDebuggerPresent) { @@ -14500,6 +14709,14 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("NavActivateFlags: %04X", g.NavActivateFlags); Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); + Text("NavFocusRoute[] = "); + for (int path_n = g.NavFocusRoute.Size - 1; path_n >= 0; path_n--) + { + const ImGuiFocusScopeData& focus_scope = g.NavFocusRoute[path_n]; + SameLine(0.0f, 0.0f); + Text("0x%08X/", focus_scope.ID); + SetItemTooltip("In window \"%s\"", FindWindowByID(focus_scope.WindowID)->Name); + } Text("NavWindowingTarget: '%s'", g.NavWindowingTarget ? g.NavWindowingTarget->Name : "NULL"); Unindent(); @@ -14638,10 +14855,12 @@ void ImGui::DebugNodeColumns(ImGuiOldColumns* columns) static void FormatTextureIDForDebugDisplay(char* buf, int buf_size, ImTextureID tex_id) { + union { void* ptr; int integer; } tex_id_opaque; + memcpy(&tex_id_opaque, &tex_id, ImMin(sizeof(void*), sizeof(tex_id))); if (sizeof(tex_id) >= sizeof(void*)) - ImFormatString(buf, buf_size, "0x%p", (void*)*(intptr_t*)(void*)&tex_id); + ImFormatString(buf, buf_size, "0x%p", tex_id_opaque.ptr); else - ImFormatString(buf, buf_size, "0x%04X", *(int*)(void*)&tex_id); + ImFormatString(buf, buf_size, "0x%04X", tex_id_opaque.integer); } // [DEBUG] Display contents of ImDrawList @@ -14790,7 +15009,7 @@ void ImGui::DebugNodeFont(ImFont* font) SetNextItemWidth(GetFontSize() * 8); DragFloat("Font scale", &font->Scale, 0.005f, 0.3f, 2.0f, "%.1f"); SameLine(); MetricsHelpMarker( - "Note than the default embedded font is NOT meant to be scaled.\n\n" + "Note that the default embedded font is NOT meant to be scaled.\n\n" "Font are currently rendered into bitmaps at a given size at the time of building the atlas. " "You may oversample them to get some flexibility with scaling. " "You can also render at multiple sizes and select which one to use at runtime.\n\n" @@ -14924,8 +15143,12 @@ void ImGui::DebugNodeTabBar(ImGuiTabBar* tab_bar, const char* label) void ImGui::DebugNodeViewport(ImGuiViewportP* viewport) { + ImGuiContext& g = *GImGui; SetNextItemOpen(true, ImGuiCond_Once); - if (TreeNode("viewport0", "Viewport #%d", 0)) + bool open = TreeNode("viewport0", "Viewport #%d", 0); + if (IsItemHovered()) + g.DebugMetricsConfig.HighlightViewportID = viewport->ID; + if (open) { ImGuiWindowFlags flags = viewport->Flags; BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nWorkArea Offset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f", @@ -14989,9 +15212,10 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) for (int layer = 0; layer < ImGuiNavLayer_COUNT; layer++) BulletText("NavPreferredScoringPosRel[%d] = {%.1f,%.1f)", layer, (pr[layer].x == FLT_MAX ? -99999.0f : pr[layer].x), (pr[layer].y == FLT_MAX ? -99999.0f : pr[layer].y)); // Display as 99999.0f so it looks neater. BulletText("NavLayersActiveMask: %X, NavLastChildNavWindow: %s", window->DC.NavLayersActiveMask, window->NavLastChildNavWindow ? window->NavLastChildNavWindow->Name : "NULL"); - if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } - if (window->ParentWindow != NULL) { DebugNodeWindow(window->ParentWindow, "ParentWindow"); } - if (window->DC.ChildWindows.Size > 0) { DebugNodeWindowsList(&window->DC.ChildWindows, "ChildWindows"); } + if (window->RootWindow != window) { DebugNodeWindow(window->RootWindow, "RootWindow"); } + if (window->ParentWindow != NULL) { DebugNodeWindow(window->ParentWindow, "ParentWindow"); } + if (window->ParentWindowForFocusRoute != NULL) { DebugNodeWindow(window->ParentWindowForFocusRoute, "ParentWindowForFocusRoute"); } + if (window->DC.ChildWindows.Size > 0) { DebugNodeWindowsList(&window->DC.ChildWindows, "ChildWindows"); } if (window->ColumnsStorage.Size > 0 && TreeNode("Columns", "Columns sets (%d)", window->ColumnsStorage.Size)) { for (ImGuiOldColumns& columns : window->ColumnsStorage) @@ -15104,7 +15328,10 @@ void ImGui::ShowDebugLogWindow(bool* p_open) return; } - CheckboxFlags("All", &g.DebugLogFlags, ImGuiDebugLogFlags_EventMask_); + ImGuiDebugLogFlags all_enable_flags = ImGuiDebugLogFlags_EventMask_ & ~ImGuiDebugLogFlags_EventInputRouting; + CheckboxFlags("All", &g.DebugLogFlags, all_enable_flags); + SetItemTooltip("(except InputRouting which is spammy)"); + ShowDebugLogFlag("ActiveId", ImGuiDebugLogFlags_EventActiveId); ShowDebugLogFlag("Clipper", ImGuiDebugLogFlags_EventClipper); ShowDebugLogFlag("Focus", ImGuiDebugLogFlags_EventFocus); @@ -15112,6 +15339,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) ShowDebugLogFlag("Nav", ImGuiDebugLogFlags_EventNav); ShowDebugLogFlag("Popup", ImGuiDebugLogFlags_EventPopup); //ShowDebugLogFlag("Selection", ImGuiDebugLogFlags_EventSelection); + ShowDebugLogFlag("InputRouting", ImGuiDebugLogFlags_EventInputRouting); if (SmallButton("Clear")) { @@ -15203,6 +15431,7 @@ void ImGui::DebugLocateItem(ImGuiID target_id) g.DebugBreakInLocateId = false; } +// FIXME: Doesn't work over through a modal window, because they clear HoveredWindow. void ImGui::DebugLocateItemOnHover(ImGuiID target_id) { if (target_id == 0 || !IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenBlockedByPopup)) @@ -15239,6 +15468,12 @@ void ImGui::DebugLocateItemResolveWithLastItem() draw_list->AddLine(p1, p2, DEBUG_LOCATE_ITEM_COLOR); } +void ImGui::DebugStartItemPicker() +{ + ImGuiContext& g = *GImGui; + g.DebugItemPickerActive = true; +} + // [DEBUG] Item picker tool - start with DebugStartItemPicker() - useful to visually select an item and break into its call-stack. void ImGui::UpdateDebugToolItemPicker() { @@ -15407,7 +15642,7 @@ void ImGui::ShowIDStackToolWindow(bool* p_open) Checkbox("Ctrl+C: copy path to clipboard", &tool->CopyToClipboardOnCtrlC); SameLine(); TextColored((time_since_copy >= 0.0f && time_since_copy < 0.75f && ImFmod(time_since_copy, 0.25f) < 0.25f * 0.5f) ? ImVec4(1.f, 1.f, 0.3f, 1.f) : ImVec4(), "*COPIED*"); - if (tool->CopyToClipboardOnCtrlC && IsKeyDown(ImGuiMod_Ctrl) && IsKeyPressed(ImGuiKey_C)) + if (tool->CopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, 0, ImGuiInputFlags_RouteGlobal)) { tool->CopyToClipboardLastTime = (float)g.Time; char* p = g.TempBuffer.Data; @@ -15474,10 +15709,8 @@ void ImGui::DebugLog(const char*, ...) {} void ImGui::DebugLogV(const char*, va_list) {} void ImGui::ShowDebugLogWindow(bool*) {} void ImGui::ShowIDStackToolWindow(bool*) {} +void ImGui::DebugStartItemPicker() {} void ImGui::DebugHookIdInfo(ImGuiID, ImGuiDataType, const void*, const void*) {} -void ImGui::UpdateDebugToolItemPicker() {} -void ImGui::UpdateDebugToolStackQueries() {} -void ImGui::UpdateDebugToolFlashStyleColor() {} #endif // #ifndef IMGUI_DISABLE_DEBUG_TOOLS diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 4a116922..707c14d3 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (demo code) // Help: @@ -401,6 +401,12 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::MenuItem("Debug Log", NULL, &show_tool_debug_log, has_debug_tools); ImGui::MenuItem("ID Stack Tool", NULL, &show_tool_id_stack_tool, has_debug_tools); ImGui::MenuItem("Style Editor", NULL, &show_tool_style_editor); + bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent; + if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present)) + ImGui::DebugStartItemPicker(); + if (!is_debugger_present) + ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools."); + ImGui::Separator(); ImGui::MenuItem("About Dear ImGui", NULL, &show_tool_about); ImGui::EndMenu(); } @@ -5307,23 +5313,26 @@ static void ShowDemoWindowTables() const int rows_count = 12; static ImGuiTableFlags table_flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersInnerH | ImGuiTableFlags_Hideable | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_HighlightHoveredColumn; + static ImGuiTableColumnFlags column_flags = ImGuiTableColumnFlags_AngledHeader | ImGuiTableColumnFlags_WidthFixed; static bool bools[columns_count * rows_count] = {}; // Dummy storage selection storage static int frozen_cols = 1; static int frozen_rows = 2; ImGui::CheckboxFlags("_ScrollX", &table_flags, ImGuiTableFlags_ScrollX); ImGui::CheckboxFlags("_ScrollY", &table_flags, ImGuiTableFlags_ScrollY); + ImGui::CheckboxFlags("_Resizable", &table_flags, ImGuiTableFlags_Resizable); ImGui::CheckboxFlags("_NoBordersInBody", &table_flags, ImGuiTableFlags_NoBordersInBody); ImGui::CheckboxFlags("_HighlightHoveredColumn", &table_flags, ImGuiTableFlags_HighlightHoveredColumn); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::SliderInt("Frozen columns", &frozen_cols, 0, 2); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::SliderInt("Frozen rows", &frozen_rows, 0, 2); + ImGui::CheckboxFlags("Disable header contributing to column width", &column_flags, ImGuiTableColumnFlags_NoHeaderWidth); if (ImGui::BeginTable("table_angled_headers", columns_count, table_flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 12))) { ImGui::TableSetupColumn(column_names[0], ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_NoReorder); for (int n = 1; n < columns_count; n++) - ImGui::TableSetupColumn(column_names[n], ImGuiTableColumnFlags_AngledHeader | ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn(column_names[n], column_flags); ImGui::TableSetupScrollFreeze(frozen_cols, frozen_rows); ImGui::TableAngledHeadersRow(); // Draw angled headers for all columns with the ImGuiTableColumnFlags_AngledHeader flag. @@ -6997,19 +7006,19 @@ struct ExampleAppConsole { ClearLog(); for (int i = 0; i < History.Size; i++) - free(History[i]); + ImGui::MemFree(History[i]); } // Portable helpers static int Stricmp(const char* s1, const char* s2) { int d; while ((d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; } return d; } static int Strnicmp(const char* s1, const char* s2, int n) { int d = 0; while (n > 0 && (d = toupper(*s2) - toupper(*s1)) == 0 && *s1) { s1++; s2++; n--; } return d; } - static char* Strdup(const char* s) { IM_ASSERT(s); size_t len = strlen(s) + 1; void* buf = malloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } + static char* Strdup(const char* s) { IM_ASSERT(s); size_t len = strlen(s) + 1; void* buf = ImGui::MemAlloc(len); IM_ASSERT(buf); return (char*)memcpy(buf, (const void*)s, len); } static void Strtrim(char* s) { char* str_end = s + strlen(s); while (str_end > s && str_end[-1] == ' ') str_end--; *str_end = 0; } void ClearLog() { for (int i = 0; i < Items.Size; i++) - free(Items[i]); + ImGui::MemFree(Items[i]); Items.clear(); } @@ -7175,7 +7184,7 @@ struct ExampleAppConsole for (int i = History.Size - 1; i >= 0; i--) if (Stricmp(History[i], command_line) == 0) { - free(History[i]); + ImGui::MemFree(History[i]); History.erase(History.begin() + i); break; } @@ -8027,6 +8036,9 @@ static void ShowExampleAppCustomRendering(bool* p_open) const float rounding = sz / 5.0f; const int circle_segments = circle_segments_override ? circle_segments_override_v : 0; const int curve_segments = curve_segments_override ? curve_segments_override_v : 0; + const ImVec2 cp3[3] = { ImVec2(0.0f, sz * 0.6f), ImVec2(sz * 0.5f, -sz * 0.4f), ImVec2(sz, sz) }; // Control points for curves + const ImVec2 cp4[4] = { ImVec2(0.0f, 0.0f), ImVec2(sz * 1.3f, sz * 0.3f), ImVec2(sz - sz * 1.3f, sz - sz * 0.3f), ImVec2(sz, sz) }; + float x = p.x + 4.0f; float y = p.y + 4.0f; for (int n = 0; n < 2; n++) @@ -8045,17 +8057,23 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line + // Path + draw_list->PathArcTo(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, 3.141592f, 3.141592f * -0.5f); + draw_list->PathStroke(col, ImDrawFlags_None, th); + x += sz + spacing; + // Quadratic Bezier Curve (3 control points) - ImVec2 cp3[3] = { ImVec2(x, y + sz * 0.6f), ImVec2(x + sz * 0.5f, y - sz * 0.4f), ImVec2(x + sz, y + sz) }; - draw_list->AddBezierQuadratic(cp3[0], cp3[1], cp3[2], col, th, curve_segments); x += sz + spacing; + draw_list->AddBezierQuadratic(ImVec2(x + cp3[0].x, y + cp3[0].y), ImVec2(x + cp3[1].x, y + cp3[1].y), ImVec2(x + cp3[2].x, y + cp3[2].y), col, th, curve_segments); + x += sz + spacing; // Cubic Bezier Curve (4 control points) - ImVec2 cp4[4] = { ImVec2(x, y), ImVec2(x + sz * 1.3f, y + sz * 0.3f), ImVec2(x + sz - sz * 1.3f, y + sz - sz * 0.3f), ImVec2(x + sz, y + sz) }; - draw_list->AddBezierCubic(cp4[0], cp4[1], cp4[2], cp4[3], col, th, curve_segments); + draw_list->AddBezierCubic(ImVec2(x + cp4[0].x, y + cp4[0].y), ImVec2(x + cp4[1].x, y + cp4[1].y), ImVec2(x + cp4[2].x, y + cp4[2].y), ImVec2(x + cp4[3].x, y + cp4[3].y), col, th, curve_segments); x = p.x + 4; y += sz + spacing; } + + // Filled shapes draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, ngon_sides); x += sz + spacing; // N-gon draw_list->AddCircleFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, circle_segments); x += sz + spacing; // Circle draw_list->AddEllipseFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, sz * 0.3f, col, -0.3f, circle_segments); x += sz + spacing;// Ellipse @@ -8067,9 +8085,27 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing * 2.0f;// Vertical line (faster than AddLine, but only handle integer thickness) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) + + // Path + draw_list->PathArcTo(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, 3.141592f * -0.5f, 3.141592f); + draw_list->PathFillConvex(col); + x += sz + spacing; + + // Quadratic Bezier Curve (3 control points) + draw_list->PathLineTo(ImVec2(x + cp3[0].x, y + cp3[0].y)); + draw_list->PathBezierQuadraticCurveTo(ImVec2(x + cp3[1].x, y + cp3[1].y), ImVec2(x + cp3[2].x, y + cp3[2].y), curve_segments); + draw_list->PathFillConvex(col); + x += sz + spacing; + + // Cubic Bezier Curve (4 control points): this is concave so not drawing it yet + //draw_list->PathLineTo(ImVec2(x + cp4[0].x, y + cp4[0].y)); + //draw_list->PathBezierCubicCurveTo(ImVec2(x + cp4[1].x, y + cp4[1].y), ImVec2(x + cp4[2].x, y + cp4[2].y), ImVec2(x + cp4[3].x, y + cp4[3].y), curve_segments); + //draw_list->PathFillConvex(col); + //x += sz + spacing; + draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); - ImGui::Dummy(ImVec2((sz + spacing) * 11.2f, (sz + spacing) * 3.0f)); + ImGui::Dummy(ImVec2((sz + spacing) * 12.2f, (sz + spacing) * 3.0f)); ImGui::PopItemWidth(); ImGui::EndTabItem(); } diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 40a840c4..1319a6e1 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (drawing and font code) /* @@ -641,7 +641,7 @@ void ImDrawList::PrimReserve(int idx_count, int vtx_count) _IdxWritePtr = IdxBuffer.Data + idx_buffer_old_size; } -// Release the a number of reserved vertices/indices from the end of the last reservation made with PrimReserve(). +// Release the number of reserved vertices/indices from the end of the last reservation made with PrimReserve(). void ImDrawList::PrimUnreserve(int idx_count, int vtx_count) { IM_ASSERT_PARANOID(idx_count >= 0 && vtx_count >= 0); @@ -3997,8 +3997,8 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im } else { - draw_list->PathArcTo(ImVec2(x0, p1.y - rounding), rounding, IM_PI - arc0_e, IM_PI - arc0_b, 3); // BL - draw_list->PathArcTo(ImVec2(x0, p0.y + rounding), rounding, IM_PI + arc0_b, IM_PI + arc0_e, 3); // TR + draw_list->PathArcTo(ImVec2(x0, p1.y - rounding), rounding, IM_PI - arc0_e, IM_PI - arc0_b); // BL + draw_list->PathArcTo(ImVec2(x0, p0.y + rounding), rounding, IM_PI + arc0_b, IM_PI + arc0_e); // TR } if (p1.x > rect.Min.x + rounding) { @@ -4017,8 +4017,8 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im } else { - draw_list->PathArcTo(ImVec2(x1, p0.y + rounding), rounding, -arc1_e, -arc1_b, 3); // TR - draw_list->PathArcTo(ImVec2(x1, p1.y - rounding), rounding, +arc1_b, +arc1_e, 3); // BR + draw_list->PathArcTo(ImVec2(x1, p0.y + rounding), rounding, -arc1_e, -arc1_b); // TR + draw_list->PathArcTo(ImVec2(x1, p1.y - rounding), rounding, +arc1_b, +arc1_e); // BR } } draw_list->PathFillConvex(col); diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index b49c99bb..a968fe30 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -114,7 +114,7 @@ enum GlfwClientApi { GlfwClientApi_Unknown, GlfwClientApi_OpenGL, - GlfwClientApi_Vulkan + GlfwClientApi_Vulkan, }; struct ImGui_ImplGlfw_Data @@ -674,11 +674,9 @@ static void ImGui_ImplGlfw_UpdateMouseData() ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); ImGuiIO& io = ImGui::GetIO(); - // (those braces are here to reduce diff with multi-viewports support in 'docking' branch) { GLFWwindow* window = bd->Window; - #ifdef __EMSCRIPTEN__ const bool is_window_focused = true; #else diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index a36a7ac2..b3a510bc 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -176,9 +176,20 @@ #define GL_VERTEX_ARRAY_BINDING GL_VERTEX_ARRAY_BINDING_OES #endif -// Desktop GL 2.0+ has glPolygonMode() which GL ES and WebGL don't have. -#ifdef GL_POLYGON_MODE -#define IMGUI_IMPL_HAS_POLYGON_MODE +// Desktop GL 2.0+ has extension and glPolygonMode() which GL ES and WebGL don't have.. +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) +#define IMGUI_IMPL_OPENGL_HAS_EXTENSIONS // has glGetIntegerv(GL_NUM_EXTENSIONS) +#define IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE // has glPolygonMode() +#endif + +// Desktop GL 2.1+ and GL ES 3.0+ have glBindBuffer() with GL_PIXEL_UNPACK_BUFFER target. +#if !defined(IMGUI_IMPL_OPENGL_ES2) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK +#endif + +// Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state +#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1) +#define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART #endif // Desktop GL 3.2+ has glDrawElementsBaseVertex() which GL ES and WebGL don't have. @@ -191,16 +202,6 @@ #define IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER #endif -// Desktop GL 3.1+ has GL_PRIMITIVE_RESTART state -#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && defined(GL_VERSION_3_1) -#define IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART -#endif - -// Desktop GL use extension detection -#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) -#define IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS -#endif - // [Debugging] //#define IMGUI_IMPL_OPENGL_DEBUG #ifdef IMGUI_IMPL_OPENGL_DEBUG @@ -359,7 +360,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) // Detect extensions we support bd->HasClipOrigin = (bd->GlVersion >= 450); -#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_EXTENSIONS +#ifdef IMGUI_IMPL_OPENGL_HAS_EXTENSIONS GLint num_extensions = 0; glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions); for (GLint i = 0; i < num_extensions; i++) @@ -411,7 +412,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid if (bd->GlVersion >= 310) glDisable(GL_PRIMITIVE_RESTART); #endif -#ifdef IMGUI_IMPL_HAS_POLYGON_MODE +#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif @@ -500,7 +501,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif -#ifdef IMGUI_IMPL_HAS_POLYGON_MODE +#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); #endif GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); @@ -639,7 +640,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } #endif -#ifdef IMGUI_IMPL_HAS_POLYGON_MODE +#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) { @@ -650,7 +651,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) { glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); } -#endif // IMGUI_IMPL_HAS_POLYGON_MODE +#endif // IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); @@ -747,6 +748,10 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() GLint last_texture, last_array_buffer; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK + GLint last_pixel_unpack_buffer; + if (bd->GlVersion >= 210) { glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } +#endif #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLint last_vertex_array; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &last_vertex_array); @@ -920,6 +925,9 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK + if (bd->GlVersion >= 210) { glBindBuffer(GL_PIXEL_UNPACK_BUFFER, last_pixel_unpack_buffer); } +#endif #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY glBindVertexArray(last_vertex_array); #endif diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index eab542d8..260df1a9 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (tables and columns code) /* @@ -498,7 +498,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->DeclColumnsCount = table->AngledHeadersCount = 0; if (previous_frame_active + 1 < g.FrameCount) table->IsActiveIdInTable = false; - temp_data->AngledheadersExtraWidth = 0.0f; + temp_data->AngledHeadersExtraWidth = 0.0f; // Using opaque colors facilitate overlapping lines of the grid, otherwise we'd need to improve TableDrawBorders() table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong); @@ -1344,7 +1344,7 @@ void ImGui::EndTable() max_pos_x = ImMax(max_pos_x, table->Columns[table->RightMostEnabledColumn].WorkMaxX + table->CellPaddingX + table->OuterPaddingX - outer_padding_for_border); if (table->ResizedColumn != -1) max_pos_x = ImMax(max_pos_x, table->ResizeLockMinContentsX2); - table->InnerWindow->DC.CursorMaxPos.x = max_pos_x + table->TempData->AngledheadersExtraWidth; + table->InnerWindow->DC.CursorMaxPos.x = max_pos_x + table->TempData->AngledHeadersExtraWidth; } // Pop clipping rect @@ -1462,7 +1462,7 @@ void ImGui::EndTable() } else if (temp_data->UserOuterSize.x <= 0.0f) { - const float decoration_size = table->TempData->AngledheadersExtraWidth + ((table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f); + const float decoration_size = table->TempData->AngledHeadersExtraWidth + ((table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f); outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - temp_data->UserOuterSize.x); outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth)); } @@ -1567,6 +1567,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo } // Store name (append with zero-terminator in contiguous buffer) + // FIXME: If we recorded the number of \n in names we could compute header row height column->NameOffset = -1; if (label != NULL && label[0] != 0) { @@ -2154,6 +2155,8 @@ void ImGui::TableEndCell(ImGuiTable* table) // - TableSetColumnWidthAutoAll() [Internal] // - TableUpdateColumnsWeightFromWidth() [Internal] //------------------------------------------------------------------------- +// Note that actual columns widths are computed in TableUpdateLayout(). +//------------------------------------------------------------------------- // Maximum column content width given current layout. Use column->MinX so this value on a per-column basis. float ImGui::TableGetMaxColumnWidth(const ImGuiTable* table, int column_n) @@ -2927,6 +2930,7 @@ void ImGui::TableSortSpecsBuild(ImGuiTable* table) // [SECTION] Tables: Headers //------------------------------------------------------------------------- // - TableGetHeaderRowHeight() [Internal] +// - TableGetHeaderAngledMaxLabelWidth() [Internal] // - TableHeadersRow() // - TableHeader() // - TableAngledHeadersRow() @@ -2958,7 +2962,7 @@ float ImGui::TableGetHeaderAngledMaxLabelWidth() if (IM_BITARRAY_TESTBIT(table->EnabledMaskByIndex, column_n)) if (table->Columns[column_n].Flags & ImGuiTableColumnFlags_AngledHeader) width = ImMax(width, CalcTextSize(TableGetColumnName(table, column_n), NULL, true).x); - return width + g.Style.CellPadding.x * 2.0f; + return width + g.Style.CellPadding.y * 2.0f; // Swap padding } // [Public] This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn(). @@ -3082,7 +3086,7 @@ void ImGui::TableHeader(const char* label) if ((table->RowFlags & ImGuiTableRowFlags_Headers) == 0) TableSetBgColor(ImGuiTableBgTarget_CellBg, GetColorU32(ImGuiCol_TableHeaderBg), table->CurrentColumn); } - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding); if (held) table->HeldHeaderColumn = (ImGuiTableColumnIdx)column_n; window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f; @@ -3180,25 +3184,25 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) // Calculate our base metrics and set angled headers data _before_ the first call to TableNextRow() // FIXME-STYLE: Would it be better for user to submit 'max_label_width' or 'row_height' ? One can be derived from the other. - const float header_height = table->RowCellPaddingY * 2.0f + g.FontSize; + const float header_height = g.FontSize + g.Style.CellPadding.x * 2.0f; const float row_height = ImFabs(ImRotate(ImVec2(max_label_width, flip_label ? +header_height : -header_height), cos_a, sin_a).y); - const ImVec2 header_angled_vector = unit_right * (row_height / -sin_a); table->AngledHeadersHeight = row_height; table->AngledHeadersSlope = (sin_a != 0.0f) ? (cos_a / sin_a) : 0.0f; + const ImVec2 header_angled_vector = unit_right * (row_height / -sin_a); // vector from bottom-left to top-left, and from bottom-right to top-right // Declare row, override and draw our own background TableNextRow(ImGuiTableRowFlags_Headers, row_height); TableNextColumn(); + const ImRect row_r(table->WorkRect.Min.x, table->BgClipRect.Min.y, table->WorkRect.Max.x, table->RowPosY2); table->DrawSplitter->SetCurrentChannel(draw_list, TABLE_DRAW_CHANNEL_BG0); float clip_rect_min_x = table->BgClipRect.Min.x; if (table->FreezeColumnsCount > 0) clip_rect_min_x = ImMax(clip_rect_min_x, table->Columns[table->FreezeColumnsCount - 1].MaxX); TableSetBgColor(ImGuiTableBgTarget_RowBg0, 0); // Cancel PushClipRect(table->BgClipRect.Min, table->BgClipRect.Max, false); // Span all columns - draw_list->AddRectFilled(table->BgClipRect.Min, table->BgClipRect.Max, GetColorU32(ImGuiCol_TableHeaderBg, 0.25f)); // FIXME-STYLE: Change row background with an arbitrary color. + draw_list->AddRectFilled(ImVec2(table->BgClipRect.Min.x, row_r.Min.y), ImVec2(table->BgClipRect.Max.x, row_r.Max.y), GetColorU32(ImGuiCol_TableHeaderBg, 0.25f)); // FIXME-STYLE: Change row background with an arbitrary color. PushClipRect(ImVec2(clip_rect_min_x, table->BgClipRect.Min.y), table->BgClipRect.Max, true); // Span all columns - const ImRect row_r(table->WorkRect.Min.x, table->BgClipRect.Min.y, table->WorkRect.Max.x, window->DC.CursorPos.y + row_height); const ImGuiID row_id = GetID("##AngledHeaders"); ButtonBehavior(row_r, row_id, NULL, NULL); KeepAliveID(row_id); @@ -3209,7 +3213,9 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) if (table_instance->HoveredRowLast == 0 && table->HoveredColumnBorder == -1 && (g.ActiveId == 0 || g.ActiveId == row_id || (table->IsActiveIdInTable || g.DragDropActive))) highlight_column_n = table->HoveredColumnBody; + // Draw background and labels in first pass, then all borders. float max_x = 0.0f; + ImVec2 padding = g.Style.CellPadding; // We will always use swapped component for (int pass = 0; pass < 2; pass++) for (int order_n = 0; order_n < table->ColumnsCount; order_n++) { @@ -3231,25 +3237,45 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_TableHeaderBg)); if (column_n == highlight_column_n) draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_Header)); // Highlight on hover - //draw_list->AddQuad(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_TableBorderLight), 1.0f); max_x = ImMax(max_x, bg_shape[3].x); - // Draw label (first draw at an offset where RenderTextXXX() function won't meddle with applying current ClipRect, then transform to final offset) - // FIXME: May be worth tidying up all those operations to make them easier to understand. + // Draw label + // - First draw at an offset where RenderTextXXX() function won't meddle with applying current ClipRect, then transform to final offset. + // - Handle multiple lines manually, as we want each lines to follow on the horizontal border, rather than see a whole block rotated. const char* label_name = TableGetColumnName(table, column_n); - const float clip_width = max_label_width - (sin_a * table->RowCellPaddingY); - ImRect label_r(window->ClipRect.Min, window->ClipRect.Min + ImVec2(clip_width + (flip_label ? 0.0f : table->CellPaddingX), header_height + table->RowCellPaddingY)); - ImVec2 label_size = CalcTextSize(label_name, NULL, true); - ImVec2 label_off = ImVec2(flip_label ? ImMax(0.0f, max_label_width - label_size.x - table->CellPaddingX) : table->CellPaddingX, table->RowCellPaddingY); - int vtx_idx_begin = draw_list->_VtxCurrentIdx; - RenderTextEllipsis(draw_list, label_r.Min + label_off, label_r.Max, label_r.Max.x, label_r.Max.x, label_name, NULL, &label_size); - //if (g.IO.KeyShift) { draw_list->AddRect(label_r.Min, label_r.Max, IM_COL32(0, 255, 0, 255), 0.0f, 0, 2.0f); } - int vtx_idx_end = draw_list->_VtxCurrentIdx; - - // Rotate and offset label - ImVec2 pivot_in = label_r.GetBL(); - ImVec2 pivot_out = ImVec2(column->WorkMinX, row_r.Max.y) + (flip_label ? (unit_right * clip_width) : ImVec2(header_height, 0.0f)); - ShadeVertsTransformPos(draw_list, vtx_idx_begin, vtx_idx_end, pivot_in, label_cos_a, label_sin_a, pivot_out); // Rotate and offset + const char* label_name_end = FindRenderedTextEnd(label_name); + const float line_off_step_x = g.FontSize / -sin_a; + float line_off_curr_x = 0.0f; + while (label_name < label_name_end) + { + const char* label_name_eol = strchr(label_name, '\n'); + if (label_name_eol == NULL) + label_name_eol = label_name_end; + + // FIXME: Individual line clipping for right-most column is broken for negative angles. + ImVec2 label_size = CalcTextSize(label_name, label_name_eol); + float clip_width = max_label_width - padding.y; // Using padding.y*2.0f would be symetrical but hide more text. + float clip_height = ImMin(label_size.y, column->ClipRect.Max.x - column->WorkMinX - line_off_curr_x); + ImRect clip_r(window->ClipRect.Min, window->ClipRect.Min + ImVec2(clip_width, clip_height)); + int vtx_idx_begin = draw_list->_VtxCurrentIdx; + RenderTextEllipsis(draw_list, clip_r.Min, clip_r.Max, clip_r.Max.x, clip_r.Max.x, label_name, label_name_eol, &label_size); + int vtx_idx_end = draw_list->_VtxCurrentIdx; + + // Rotate and offset label + ImVec2 pivot_in = ImVec2(window->ClipRect.Min.x, window->ClipRect.Min.y + label_size.y); + ImVec2 pivot_out = ImVec2(column->WorkMinX, row_r.Max.y); + line_off_curr_x += line_off_step_x; + pivot_out += unit_right * padding.y; + if (flip_label) + pivot_out += unit_right * (clip_width - ImMax(0.0f, clip_width - label_size.x)); + pivot_out.x += flip_label ? line_off_curr_x - line_off_step_x : line_off_curr_x; + ShadeVertsTransformPos(draw_list, vtx_idx_begin, vtx_idx_end, pivot_in, label_cos_a, label_sin_a, pivot_out); // Rotate and offset + //if (g.IO.KeyShift) { ImDrawList* fg_dl = GetForegroundDrawList(); vtx_idx_begin = fg_dl->_VtxCurrentIdx; fg_dl->AddRect(clip_r.Min, clip_r.Max, IM_COL32(0, 255, 0, 255), 0.0f, 0, 2.0f); ShadeVertsTransformPos(fg_dl, vtx_idx_begin, fg_dl->_VtxCurrentIdx, pivot_in, label_cos_a, label_sin_a, pivot_out); } + + // Register header width + column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX + ImCeil(line_off_curr_x); + label_name = label_name_eol + 1; + } } if (pass == 1) { @@ -3259,7 +3285,7 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) } PopClipRect(); PopClipRect(); - table->TempData->AngledheadersExtraWidth = ImMax(0.0f, max_x - table->Columns[table->RightMostEnabledColumn].MaxX); + table->TempData->AngledHeadersExtraWidth = ImMax(0.0f, max_x - table->Columns[table->RightMostEnabledColumn].MaxX); } //------------------------------------------------------------------------- diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index e3f420a2..734950d7 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.1 +// dear imgui, v1.90.4 // (widgets code) /* @@ -477,6 +477,9 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // Frame N + RepeatDelay + RepeatRate*N true true - true //------------------------------------------------------------------------------------------------------------------------------------------------- +// FIXME: For refactor we could output flags, incl mouse hovered vs nav keyboard vs nav triggered etc. +// And better standardize how widgets use 'GetColor32((held && hovered) ? ... : hovered ? ...)' vs 'GetColor32(held ? ... : hovered ? ...);' +// For mouse feedback we typically prefer the 'held && hovered' test, but for nav feedback not always. Outputting hovered=true on Activation may be misleading. bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags) { ImGuiContext& g = *GImGui; @@ -597,9 +600,9 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool g.NavDisableHighlight = true; } - // Gamepad/Keyboard navigation - // We report navigated item as hovered but we don't set g.HoveredId to not interfere with mouse. - if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover && (g.ActiveId == 0 || g.ActiveId == id || g.ActiveId == window->MoveId)) + // Gamepad/Keyboard handling + // We report navigated and navigation-activated items as hovered but we don't set g.HoveredId to not interfere with mouse. + if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover) if (!(flags & ImGuiButtonFlags_NoHoveredOnFocus)) hovered = true; if (g.NavActivateDownId == id) @@ -621,8 +624,10 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool pressed = true; SetActiveID(id, window); g.ActiveIdSource = g.NavInputSource; - if (!(flags & ImGuiButtonFlags_NoNavFocus)) + if (!(flags & ImGuiButtonFlags_NoNavFocus) && !(g.NavActivateFlags & ImGuiActivateFlags_FromShortcut)) SetFocusID(id, window); + if (g.NavActivateFlags & ImGuiActivateFlags_FromShortcut) + g.ActiveIdFromShortcut = true; } } @@ -666,13 +671,19 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) { // When activated using Nav, we hold on the ActiveID until activation button is released - if (g.NavActivateDownId != id) + if (g.NavActivateDownId == id) + held = true; // hovered == true not true as we are already likely hovered on direct activation. + else ClearActiveID(); } if (pressed) g.ActiveIdHasBeenPressedBefore = true; } + // Activation highlight (this may be a remote activation) + if (g.NavHighlightActivatedId == id) + hovered = true; + if (out_hovered) *out_hovered = hovered; if (out_held) *out_held = held; @@ -966,7 +977,6 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 // Click position in scrollbar normalized space (0.0f->1.0f) const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); - SetHoveredID(id); bool seek_absolute = false; if (g.ActiveIdIsJustActivated) @@ -1774,7 +1784,7 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags // This is essentially a specialized version of BeginPopupEx() char name[16]; - ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginPopupStack.Size); // Recycle windows based on depth + ImFormatString(name, IM_ARRAYSIZE(name), "##Combo_%02d", g.BeginComboDepth); // Recycle windows based on depth // Set position given a custom constraint (peak into expected window size so we can position it) // FIXME: This might be easier to express with an hypothetical SetNextWindowPosConstraints() function? @@ -1801,12 +1811,15 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags IM_ASSERT(0); // This should never happen as we tested for IsPopupOpen() above return false; } + g.BeginComboDepth++; return true; } void ImGui::EndCombo() { + ImGuiContext& g = *GImGui; EndPopup(); + g.BeginComboDepth--; } // Call directly after the BeginCombo/EndCombo block. The preview is designed to only host non-interactive elements @@ -1982,7 +1995,7 @@ bool ImGui::Combo(const char* label, int* current_item, bool (*old_getter)(void* // - DataTypeGetInfo() // - DataTypeFormatString() // - DataTypeApplyOp() -// - DataTypeApplyOpFromText() +// - DataTypeApplyFromText() // - DataTypeCompare() // - DataTypeClamp() // - GetMinimumStepAtDecimalPrecision @@ -4175,27 +4188,32 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; + const bool init_reload_from_user_buf = (state != NULL && state->ReloadUserBuf); const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); // state != NULL means its our state. const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav); const bool init_state = (init_make_active || user_scroll_active); - if ((init_state && g.ActiveId != id) || init_changed_specs) + if ((init_state && g.ActiveId != id) || init_changed_specs || init_reload_from_user_buf) { // Access state even if we don't own it yet. state = &g.InputTextState; state->CursorAnimReset(); + state->ReloadUserBuf = false; // Backup state of deactivating item so they'll have a chance to do a write to output buffer on the same frame they report IsItemDeactivatedAfterEdit (#4714) InputTextDeactivateHook(state->ID); - // Take a copy of the initial buffer value (both in original UTF-8 format and converted to wchar) - // From the moment we focused we are ignoring the content of 'buf' (unless we are in read-only mode) + // From the moment we focused we are normally ignoring the content of 'buf' (unless we are in read-only mode) const int buf_len = (int)strlen(buf); - state->InitialTextA.resize(buf_len + 1); // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string. - memcpy(state->InitialTextA.Data, buf, buf_len + 1); + if (!init_reload_from_user_buf) + { + // Take a copy of the initial buffer value. + state->InitialTextA.resize(buf_len + 1); // UTF-8. we use +1 to make sure that .Data is always pointing to at least an empty string. + memcpy(state->InitialTextA.Data, buf, buf_len + 1); + } // Preserve cursor position and undo/redo stack if we come back to same widget - // FIXME: Since we reworked this on 2022/06, may want to differenciate recycle_cursor vs recycle_undostate? - bool recycle_state = (state->ID == id && !init_changed_specs); + // FIXME: Since we reworked this on 2022/06, may want to differentiate recycle_cursor vs recycle_undostate? + bool recycle_state = (state->ID == id && !init_changed_specs && !init_reload_from_user_buf); if (recycle_state && (state->CurLenA != buf_len || (state->TextAIsValid && strncmp(state->TextA.Data, buf, buf_len) != 0))) recycle_state = false; @@ -4220,7 +4238,13 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ stb_textedit_initialize_state(&state->Stb, !is_multiline); } - if (!is_multiline) + if (init_reload_from_user_buf) + { + state->Stb.select_start = state->ReloadSelectionStart; + state->Stb.cursor = state->Stb.select_end = state->ReloadSelectionEnd; + state->CursorClamp(); + } + else if (!is_multiline) { if (flags & ImGuiInputTextFlags_AutoSelectAll) select_all = true; @@ -4250,6 +4274,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); if (is_multiline || (flags & ImGuiInputTextFlags_CallbackHistory)) g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); + SetKeyOwner(ImGuiKey_Enter, id); + SetKeyOwner(ImGuiKey_KeypadEnter, id); SetKeyOwner(ImGuiKey_Home, id); SetKeyOwner(ImGuiKey_End, id); if (is_multiline) @@ -4259,8 +4285,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } if (is_osx) SetKeyOwner(ImGuiMod_Alt, id); - if (flags & (ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_AllowTabInput)) // Disable keyboard tabbing out as we will use the \t character. - SetShortcutRouting(ImGuiKey_Tab, id); } // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function) @@ -4389,11 +4413,20 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // We expect backends to emit a Tab key but some also emit a Tab character which we ignore (#2467, #1336) // (For Tab and Enter: Win32/SFML/Allegro are sending both keys and chars, GLFW and SDL are only sending keys. For Space they all send all threes) - if ((flags & ImGuiInputTextFlags_AllowTabInput) && Shortcut(ImGuiKey_Tab, id, ImGuiInputFlags_Repeat) && !is_readonly) + if ((flags & ImGuiInputTextFlags_AllowTabInput) && !is_readonly) { - unsigned int c = '\t'; // Insert TAB - if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) - state->OnKeyPressed((int)c); + if (Shortcut(ImGuiKey_Tab, id, ImGuiInputFlags_Repeat)) + { + unsigned int c = '\t'; // Insert TAB + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + state->OnKeyPressed((int)c); + } + // FIXME: Implement Shift+Tab + /* + if (Shortcut(ImGuiKey_Tab | ImGuiMod_Shift, id, ImGuiInputFlags_Repeat)) + { + } + */ } // Process regular text input (before we check for Return because using some IME will effectively send a Return?) @@ -6308,7 +6341,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // Render const ImU32 text_col = GetColorU32(ImGuiCol_Text); - ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_TypeThin; + ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact; if (display_frame) { // Framed type @@ -6611,7 +6644,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl RenderFrame(bb.Min, bb.Max, col, false, 0.0f); } if (g.NavId == id) - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding); if (span_all_columns) { @@ -7491,6 +7524,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) PopItemFlag(); bool want_open = false; + bool want_open_nav_init = false; bool want_close = false; if (window->DC.LayoutType == ImGuiLayoutType_Vertical) // (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) { @@ -7533,8 +7567,9 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) want_open = true; if (g.NavId == id && g.NavMoveDir == ImGuiDir_Right) // Nav-Right to open { - want_open = true; + want_open = want_open_nav_init = true; NavMoveRequestCancel(); + NavRestoreHighlightAfterMove(); } } else @@ -7566,13 +7601,13 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (want_open && !menu_is_open && g.OpenPopupStack.Size > g.BeginPopupStack.Size) { - // Don't reopen/recycle same menu level in the same frame, first close the other menu and yield for a frame. + // Don't reopen/recycle same menu level in the same frame if it is a different menu ID, first close the other menu and yield for a frame. OpenPopup(label); } else if (want_open) { menu_is_open = true; - OpenPopup(label); + OpenPopup(label, ImGuiPopupFlags_NoReopen);// | (want_open_nav_init ? ImGuiPopupFlags_NoReopenAlwaysNavInit : 0)); } if (menu_is_open) @@ -7584,6 +7619,14 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) PopStyleVar(); if (menu_is_open) { + // Implement what ImGuiPopupFlags_NoReopenAlwaysNavInit would do: + // Perform an init request in the case the popup was already open (via a previous mouse hover) + if (want_open && want_open_nav_init && !g.NavInitRequest) + { + FocusWindow(g.CurrentWindow, ImGuiFocusRequestFlags_UnlessBelowModal); + NavInitWindow(g.CurrentWindow, false); + } + // Restore LastItemData so IsItemXXXX functions can work after BeginMenu()/EndMenu() // (This fixes using IsItemClicked() and IsItemHovered(), but IsItemHovered() also relies on its support for ImGuiItemFlags_NoWindowHoverableCheck) g.LastItemData = last_item_in_parent; From c292e5cdea6d1355cdd86ca72d41454491c1563c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 27 Feb 2024 00:47:57 +0100 Subject: [PATCH 0941/1018] knn: add median distant pairwise of pointcloud --- include/gproshan/pointcloud/knn.h | 3 +++ src/gproshan/pointcloud/knn.cpp | 16 ++++++++++++++++ src/gproshan/raytracing/embree.cpp | 11 +++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 6fb29afc..31fe8af8 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -64,6 +64,9 @@ class k3tree }; +real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const mat4 & model_mat = mat4::identity()); + + } // namespace gproshan #endif // KNN_H diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index ebecd4d9..2ff08e62 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -124,5 +124,21 @@ int k3tree::operator () (const index_t i, const index_t j) const } +real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, 2); + + std::vector dist(n_points); + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, 1)], 0)); + + std::ranges::sort(dist); + + return dist[size(dist) >> 1]; +} + + } // namespace gproshan diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index cd4a88c3..d68b9ec5 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -217,15 +217,18 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p knn::k3tree * nn = !pc.knn ? nullptr - : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn); + : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) - { pxyzr[i] = model_mat * (mesh->point(i), 1); - pxyzr[i][3] = nn ? length(model_mat * (mesh->point(i), 1) - model_mat * (mesh->point((*nn)(i, pc.knn - 1)), 1)) : pc.radius; - } + + const real_t r = pc.radius < 0 && !nn ? knn::pc_median_pairwise_distant(&mesh->point(0), mesh->n_vertices, model_mat) : pc.radius; + + #pragma omp parallel for + for(index_t i = 0; i < mesh->n_vertices; ++i) + pxyzr[i][3] = nn ? length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)) : r; delete nn; From ff829589b25d3372b0f0267ff42be12d25e2abd6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 28 Feb 2024 18:02:26 +0100 Subject: [PATCH 0942/1018] knn: add k3tree constructor raw pointer query --- include/gproshan/pointcloud/knn.h | 1 + src/gproshan/pointcloud/knn.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 31fe8af8..f17d8710 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -56,6 +56,7 @@ class k3tree public: k3tree(const point * pc, const size_t n_points, const size_t k = 8, const std::vector & query = {}); + k3tree(const point * pc, const size_t n_points, const point * query, const size_t n_query, const size_t k = 8); ~k3tree(); const int * operator [] (const index_t i) const; diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 2ff08e62..fef6e376 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -71,8 +71,11 @@ std::vector grid::operator () (const point & p, int k) const } -///< Implementation using flann, by default compute all knn k3tree::k3tree(const point * pc, const size_t n_points, const size_t k, const std::vector & query) + : k3tree(pc, n_points, size(query) ? query.data() : nullptr, size(query), k) {} + +///< Implementation using flann, by default compute all knn +k3tree::k3tree(const point * pc, const size_t n_points, const point * query, const size_t n_query, const size_t k) { double time_build, time_query; @@ -84,8 +87,8 @@ k3tree::k3tree(const point * pc, const size_t n_points, const size_t k, const st gproshan_log_var(time_build); TIC(time_query); - const point * q = size(query) ? query.data() : pc; - const size_t n_results = size(query) ? size(query) : n_points; + const point * q = query && n_query ? query : pc; + const size_t n_results = query && n_query ? n_query : n_points; flann::Matrix mq((real_t *) q, n_results, 3); From 8ff4bdb61e11d842fe96bea7a06de9a388a7ce7f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Mar 2024 01:37:21 +0100 Subject: [PATCH 0943/1018] knn: added mean of median knn distants --- include/gproshan/pointcloud/knn.h | 1 + src/gproshan/pointcloud/knn.cpp | 22 ++++++++++++++++++++++ src/gproshan/raytracing/embree.cpp | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index f17d8710..7b2497f8 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -66,6 +66,7 @@ class k3tree real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const mat4 & model_mat = mat4::identity()); +real_t pc_mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); } // namespace gproshan diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index fef6e376..1ef1a2e6 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -142,6 +142,28 @@ real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const return dist[size(dist) >> 1]; } +real_t pc_mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree nn(pc, n_points, k + 1); + + std::vector dist(k); + real_t mean = 0; + + #pragma omp parallel for firstprivate(dist) + for(index_t i = 0; i < n_points; ++i) + { + for(index_t j = 0; j < k; ++j) + dist[j] = length(model_mat * (pc[i] - pc[nn(i, j + 1)], 0)); + + std::ranges::sort(dist); + + #pragma omp atomic + mean += dist[k >> 1]; + } + + return mean / n_points; +} + } // namespace gproshan diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index d68b9ec5..7e0005fc 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -224,7 +224,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p for(index_t i = 0; i < mesh->n_vertices; ++i) pxyzr[i] = model_mat * (mesh->point(i), 1); - const real_t r = pc.radius < 0 && !nn ? knn::pc_median_pairwise_distant(&mesh->point(0), mesh->n_vertices, model_mat) : pc.radius; + const real_t r = pc.radius < 0 && !nn ? knn::pc_mean_median_knn_distant(&mesh->point(0), mesh->n_vertices, 8, model_mat) : pc.radius; #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) From ce7e1973d297477ca18bf0c595602c1c6b0f4dfb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Mar 2024 16:29:31 +0100 Subject: [PATCH 0944/1018] knn: options to compute a radius for all points cover surface --- include/gproshan/pointcloud/knn.h | 10 ++- src/gproshan/pointcloud/knn.cpp | 128 ++++++++++++++++++++++++++--- src/gproshan/raytracing/embree.cpp | 2 +- 3 files changed, 125 insertions(+), 15 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 7b2497f8..e44dbe28 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -65,8 +65,14 @@ class k3tree }; -real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const mat4 & model_mat = mat4::identity()); -real_t pc_mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t median_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); } // namespace gproshan diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 1ef1a2e6..3b604744 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -96,7 +96,7 @@ k3tree::k3tree(const point * pc, const size_t n_points, const point * query, con flann::Matrix dists(new real_t[n_results * k], n_results, k); flann::SearchParams params; - params.cores = 16; + params.cores = 0; index.knnSearch(mq, indices, dists, k, params); TOC(time_query); gproshan_log_var(time_query); @@ -127,41 +127,145 @@ int k3tree::operator () (const index_t i, const index_t j) const } -real_t pc_median_pairwise_distant(const point * pc, const size_t n_points, const mat4 & model_mat) +real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, 2); + k3tree p2nn(pc, n_points, k + 1); + + real_t mean = 0; + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + #pragma omp atomic + mean += length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + } + + return mean / n_points; +} + +real_t median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) - dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, 1)], 0)); + dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); std::ranges::sort(dist); return dist[size(dist) >> 1]; } -real_t pc_mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +real_t median_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); + + std::vector dist(n_points); + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, (k >> 1) + 1)], 0)); + + std::ranges::sort(dist); + + return dist[size(dist) >> 1]; +} + + +real_t mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - std::vector dist(k); real_t mean = 0; - #pragma omp parallel for firstprivate(dist) + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + #pragma omp atomic + mean += length(model_mat * (pc[i] - pc[nn(i, (k >> 1) + 1)], 0)); + } + + return mean / n_points; +} + +real_t median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); + + std::vector dist(n_points); + + #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - for(index_t j = 0; j < k; ++j) - dist[j] = length(model_mat * (pc[i] - pc[nn(i, j + 1)], 0)); + real_t mean = 0; + for(index_t j = 1; j <= k; ++j) + mean += length(model_mat * (pc[i] - pc[p2nn(i, j)], 0)); + + dist[i] = mean / k; + } + + std::ranges::sort(dist); + + return dist[size(dist) >> 1]; +} + +real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); - std::ranges::sort(dist); + real_t mean_mean = 0; + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + real_t mean = 0; + for(index_t j = 1; j <= k; ++j) + mean += length(model_mat * (pc[i] - pc[p2nn(i, j)], 0)); #pragma omp atomic - mean += dist[k >> 1]; + mean_mean += mean / k; } - return mean / n_points; + return mean_mean / n_points; +} + +real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); + + real_t mean_r = 0; + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + real_t r = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + + #pragma omp atomic + mean_r += r * r / k; + } + + return mean_r / n_points; +} + +real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + k3tree p2nn(pc, n_points, k + 1); + + std::vector radius(n_points); + + #pragma omp parallel for + for(index_t i = 0; i < n_points; ++i) + { + real_t r = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + + radius[i] = r * r / k; + } + + std::ranges::sort(radius); + + return radius[size(radius) >> 1]; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 7e0005fc..73831146 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -224,7 +224,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p for(index_t i = 0; i < mesh->n_vertices; ++i) pxyzr[i] = model_mat * (mesh->point(i), 1); - const real_t r = pc.radius < 0 && !nn ? knn::pc_mean_median_knn_distant(&mesh->point(0), mesh->n_vertices, 8, model_mat) : pc.radius; + const real_t r = pc.radius < 0 && !nn ? knn::mean_median_knn_distant(&mesh->point(0), mesh->n_vertices, 8, model_mat) : pc.radius; #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) From 8edb76af02a0b2b3059a41c206e921ed555e8b39 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 5 Mar 2024 20:20:59 +0100 Subject: [PATCH 0945/1018] knn: adding different ways to compute radius knn/embree application --- include/gproshan/pointcloud/knn.h | 5 +++ include/gproshan/raytracing/embree.h | 2 + src/gproshan/pointcloud/knn.cpp | 65 ++++++++++++++++++++++------ src/gproshan/raytracing/embree.cpp | 17 +++++--- src/gproshan/viewer/viewer.cpp | 11 ++++- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index e44dbe28..b3496da3 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -75,6 +75,11 @@ real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_ real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +const char * radius_str(void *, int opt); + +real_t radius(const int opt, const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); + + } // namespace gproshan #endif // KNN_H diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 8b01b240..32309f7e 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -22,8 +22,10 @@ class embree : public raytracing bool enable = false; bool normals = false; float radius = 0.01; + bool knn_area = true; int knn = 0; + pc_opts() {}; }; diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 3b604744..86e0a83b 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -145,13 +145,13 @@ real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k, real_t median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) - dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + dist[i] = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); std::ranges::sort(dist); @@ -160,13 +160,13 @@ real_t median_knn_distant(const point * pc, const size_t n_points, const size_t real_t median_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) - dist[i] = length(model_mat * (pc[i] - pc[p2nn(i, (k >> 1) + 1)], 0)); + dist[i] = length(model_mat * (pc[i] - pc[nn(i, (k >> 1) + 1)], 0)); std::ranges::sort(dist); @@ -192,7 +192,7 @@ real_t mean_median_knn_distant(const point * pc, const size_t n_points, const si real_t median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); std::vector dist(n_points); @@ -201,7 +201,7 @@ real_t median_mean_knn_distant(const point * pc, const size_t n_points, const si { real_t mean = 0; for(index_t j = 1; j <= k; ++j) - mean += length(model_mat * (pc[i] - pc[p2nn(i, j)], 0)); + mean += length(model_mat * (pc[i] - pc[nn(i, j)], 0)); dist[i] = mean / k; } @@ -213,7 +213,7 @@ real_t median_mean_knn_distant(const point * pc, const size_t n_points, const si real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); real_t mean_mean = 0; @@ -222,7 +222,7 @@ real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size { real_t mean = 0; for(index_t j = 1; j <= k; ++j) - mean += length(model_mat * (pc[i] - pc[p2nn(i, j)], 0)); + mean += length(model_mat * (pc[i] - pc[nn(i, j)], 0)); #pragma omp atomic mean_mean += mean / k; @@ -233,41 +233,78 @@ real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); real_t mean_r = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - real_t r = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + real_t r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); #pragma omp atomic - mean_r += r * r / k; + mean_r += sqrt(r * r / k); } + gproshan_log_var(mean_r); + return mean_r / n_points; } real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - k3tree p2nn(pc, n_points, k + 1); + k3tree nn(pc, n_points, k + 1); std::vector radius(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - real_t r = length(model_mat * (pc[i] - pc[p2nn(i, k)], 0)); + real_t r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); - radius[i] = r * r / k; + radius[i] = sqrt(r * r / k); } std::ranges::sort(radius); + return radius[size(radius) >> 1]; } +const char * radius_str(void *, int opt) +{ + static const char * str[] = { "none", + "mean_knn_distant", + "median_knn_distant", + "median_median_knn_distant", + "mean_median_knn_distant", + "median_mean_knn_distant", + "mean_median_knn_distant", + "mean_knn_area_radius", + "median_knn_area_radius" + }; + + return str[opt]; +} + +real_t radius(const int opt, const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +{ + using fknn = real_t (*) (const point *, const size_t, const size_t, const mat4 &); + static const fknn f[] = { nullptr, + mean_knn_distant, + median_knn_distant, + median_median_knn_distant, + mean_median_knn_distant, + median_mean_knn_distant, + mean_median_knn_distant, + mean_knn_area_radius, + median_knn_area_radius + }; + + return opt ? f[opt](pc, n_points, k, model_mat) : 0; +} + + } // namespace gproshan diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 73831146..a4111a0e 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -216,19 +216,26 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p ); - knn::k3tree * nn = !pc.knn ? nullptr - : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); + knn::k3tree * nn = pc.radius > 0 ? nullptr : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) pxyzr[i] = model_mat * (mesh->point(i), 1); - const real_t r = pc.radius < 0 && !nn ? knn::mean_median_knn_distant(&mesh->point(0), mesh->n_vertices, 8, model_mat) : pc.radius; - #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) - pxyzr[i][3] = nn ? length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)) : r; + { + if(!nn) + { + pxyzr[i][3] = pc.radius; + continue; + } + + const real_t r = length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)); + + pxyzr[i][3] = pc.knn_area ? sqrt(r * r / pc.knn) : r; + } delete nn; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 1f5511f5..768f5aef 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -978,6 +979,7 @@ bool viewer::m_setup_raytracing(viewer * view) static int rt = 0; static double time = 0; static rt::embree::pc_opts pc; + static int opt_radius = 0; ImGui::SliderInt("depth", (int *) &view->render_params.depth, 1, 1 << 5); ImGui::SliderInt("n_samples", (int *) &view->render_params.n_samples, 1, 1 << 5); @@ -986,8 +988,15 @@ bool viewer::m_setup_raytracing(viewer * view) if(rt == R_EMBREE && (mesh.render_pointcloud || mesh->is_pointcloud())) { ImGui::Indent(); - ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 5); + ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 6); ImGui::SliderFloat("pc.radius", &pc.radius, 0, 1); + if(ImGui::Combo("knn_radius", &opt_radius, knn::radius_str, nullptr, 9, 10)) + { + pc.radius = knn::radius(opt_radius, &mesh->point(0), mesh->n_vertices, pc.knn, mesh.model_mat); + gproshan_error_var(opt_radius); + gproshan_error_var(pc.radius); + } + ImGui::Checkbox("pc.knn_area", &pc.knn_area); ImGui::Checkbox("pc.normals", &pc.normals); ImGui::Unindent(); } From 334a15393a2f33ae217a6c219f5e553825953385 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Mar 2024 15:35:06 +0100 Subject: [PATCH 0946/1018] knn: options knn radius per point embree --- include/gproshan/pointcloud/knn.h | 4 ++ include/gproshan/raytracing/embree.h | 12 +++++- src/gproshan/pointcloud/knn.cpp | 63 +++++++++++++++++----------- src/gproshan/raytracing/embree.cpp | 36 ++++++++++++---- src/gproshan/viewer/viewer.cpp | 14 +++---- 5 files changed, 88 insertions(+), 41 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index b3496da3..e9d4972f 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -75,6 +75,10 @@ real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_ real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +real_t median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat); +real_t mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat); + + const char * radius_str(void *, int opt); real_t radius(const int opt, const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 32309f7e..a7010a2e 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -17,13 +17,21 @@ namespace gproshan::rt { class embree : public raytracing { public: + enum knn_opt { NONE, + MAX, + MEAN, + MEDIAN, + AREA, + MEDIAN_PAIRS + }; struct pc_opts { bool enable = false; bool normals = false; float radius = 0.01; - bool knn_area = true; - int knn = 0; + knn_opt opt = NONE; + float scale = 1; + int knn = 8; pc_opts() {}; diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 86e0a83b..4750cf7f 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -198,13 +198,7 @@ real_t median_mean_knn_distant(const point * pc, const size_t n_points, const si #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) - { - real_t mean = 0; - for(index_t j = 1; j <= k; ++j) - mean += length(model_mat * (pc[i] - pc[nn(i, j)], 0)); - - dist[i] = mean / k; - } + dist[i] = mean_knn(pc, nn(i), k, model_mat); std::ranges::sort(dist); @@ -215,20 +209,16 @@ real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size { k3tree nn(pc, n_points, k + 1); - real_t mean_mean = 0; + real_t mean = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - real_t mean = 0; - for(index_t j = 1; j <= k; ++j) - mean += length(model_mat * (pc[i] - pc[nn(i, j)], 0)); - #pragma omp atomic - mean_mean += mean / k; + mean += mean_knn(pc, nn(i), k, model_mat); } - return mean_mean / n_points; + return mean / n_points; } real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) @@ -267,22 +257,45 @@ real_t median_knn_area_radius(const point * pc, const size_t n_points, const siz std::ranges::sort(radius); - return radius[size(radius) >> 1]; } +real_t median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat) +{ + std::vector dist; + dist.reserve((n * (n - 1)) >> 1); + + for(index_t i = 0; i < n; ++i) + for(index_t j = i + 1; j < n; ++j) + dist.push_back(length(model_mat * (pc[id[i]] - pc[id[j]], 0))); + + std::ranges::sort(dist); + + return dist[size(dist) >> 1]; +} + +real_t mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat) +{ + real_t mean = 0; + for(index_t i = 1; i <= n; ++i) + mean += length(model_mat * (pc[id[0]] - pc[id[i]], 0)); + + return mean / n; +} + + const char * radius_str(void *, int opt) { static const char * str[] = { "none", "mean_knn_distant", "median_knn_distant", - "median_median_knn_distant", +// "median_median_knn_distant", "mean_median_knn_distant", - "median_mean_knn_distant", - "mean_median_knn_distant", - "mean_knn_area_radius", - "median_knn_area_radius" +// "median_mean_knn_distant", + "mean_mean_knn_distant", +// "mean_knn_area_radius", +// "median_knn_area_radius" }; return str[opt]; @@ -294,12 +307,12 @@ real_t radius(const int opt, const point * pc, const size_t n_points, const size static const fknn f[] = { nullptr, mean_knn_distant, median_knn_distant, - median_median_knn_distant, - mean_median_knn_distant, - median_mean_knn_distant, +// median_median_knn_distant, mean_median_knn_distant, - mean_knn_area_radius, - median_knn_area_radius +// median_mean_knn_distant, + mean_mean_knn_distant, +// mean_knn_area_radius, +// median_knn_area_radius }; return opt ? f[opt](pc, n_points, k, model_mat) : 0; diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index a4111a0e..bcb99459 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -216,7 +216,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p ); - knn::k3tree * nn = pc.radius > 0 ? nullptr : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); + knn::k3tree * nn = pc.opt == NONE ? nullptr : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); #pragma omp parallel for @@ -226,15 +226,37 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - if(!nn) + real_t r = 0; + + switch(pc.opt) { - pxyzr[i][3] = pc.radius; - continue; - } + case NONE: + r = pc.radius; + break; + + case MAX: + r = length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)); + break; + + case MEAN: + r = knn::mean_knn(&mesh->point(0), (*nn)(i), pc.knn + 1, model_mat); + break; + + case MEDIAN: + r = length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn >> 1)), 0)); + break; + + case AREA: + r = length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)); + r = sqrt(r * r / pc.knn); + break; - const real_t r = length(model_mat * (mesh->point(i) - mesh->point((*nn)(i, pc.knn)), 0)); + case MEDIAN_PAIRS: + r = knn::median_pair_dist(&mesh->point(0), (*nn)(i), pc.knn, model_mat); + break; + }; - pxyzr[i][3] = pc.knn_area ? sqrt(r * r / pc.knn) : r; + pxyzr[i][3] = pc.scale * r; } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 768f5aef..33bea2a3 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -988,15 +988,15 @@ bool viewer::m_setup_raytracing(viewer * view) if(rt == R_EMBREE && (mesh.render_pointcloud || mesh->is_pointcloud())) { ImGui::Indent(); - ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 6); - ImGui::SliderFloat("pc.radius", &pc.radius, 0, 1); - if(ImGui::Combo("knn_radius", &opt_radius, knn::radius_str, nullptr, 9, 10)) + ImGui::Combo("pc.opt", (int *) &pc.opt, "NONE\0MAX\0MEAN\0MEDIAN\0AREA\0MEDIAN_PAIRS\0\0"); + if(pc.opt == rt::embree::NONE) { - pc.radius = knn::radius(opt_radius, &mesh->point(0), mesh->n_vertices, pc.knn, mesh.model_mat); - gproshan_error_var(opt_radius); - gproshan_error_var(pc.radius); + ImGui::SliderFloat("pc.radius", &pc.radius, 0, 1); + if(ImGui::Combo("knn_radius", &opt_radius, knn::radius_str, nullptr, 5, 10)) + pc.radius = knn::radius(opt_radius, &mesh->point(0), mesh->n_vertices, pc.knn, mesh.model_mat); } - ImGui::Checkbox("pc.knn_area", &pc.knn_area); + ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 6); + ImGui::SliderFloat("pc.scale", &pc.scale, 0, 10); ImGui::Checkbox("pc.normals", &pc.normals); ImGui::Unindent(); } From 06900de7b6547435d522bd714f144c6c0bb66260 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Mar 2024 16:09:21 +0100 Subject: [PATCH 0947/1018] embree: knn mean fix bug --- src/gproshan/raytracing/embree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index bcb99459..a63606d0 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -239,7 +239,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p break; case MEAN: - r = knn::mean_knn(&mesh->point(0), (*nn)(i), pc.knn + 1, model_mat); + r = knn::mean_knn(&mesh->point(0), (*nn)(i), pc.knn, model_mat); break; case MEDIAN: From 8518c1f31b49512f1463affe5fa1e94b23d3faff Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 6 Mar 2024 17:38:48 +0100 Subject: [PATCH 0948/1018] knn: uncomment radius variations --- src/gproshan/pointcloud/knn.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 4750cf7f..56bcc6a5 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -290,12 +290,12 @@ const char * radius_str(void *, int opt) static const char * str[] = { "none", "mean_knn_distant", "median_knn_distant", -// "median_median_knn_distant", + "median_median_knn_distant", "mean_median_knn_distant", -// "median_mean_knn_distant", + "median_mean_knn_distant", "mean_mean_knn_distant", -// "mean_knn_area_radius", -// "median_knn_area_radius" + "mean_knn_area_radius", + "median_knn_area_radius" }; return str[opt]; @@ -307,12 +307,12 @@ real_t radius(const int opt, const point * pc, const size_t n_points, const size static const fknn f[] = { nullptr, mean_knn_distant, median_knn_distant, -// median_median_knn_distant, + median_median_knn_distant, mean_median_knn_distant, -// median_mean_knn_distant, + median_mean_knn_distant, mean_mean_knn_distant, -// mean_knn_area_radius, -// median_knn_area_radius + mean_knn_area_radius, + median_knn_area_radius }; return opt ? f[opt](pc, n_points, k, model_mat) : 0; From 5bd0b22dbfd1aa5c989869312c9b283021f3e5c0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 13 Mar 2024 14:36:11 +0100 Subject: [PATCH 0949/1018] rt_embree: get pc data from tracer --- include/gproshan/raytracing/embree.h | 2 ++ src/gproshan/raytracing/embree.cpp | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index a7010a2e..0b56184c 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -70,6 +70,8 @@ class embree : public raytracing virtual index_t closest_vertex(const vertex & org, const vertex & dir) const; virtual eval_hit intersect(const vertex & org, const vertex & dir, const bool flat = true) const; + vec4 * pc_data(const index_t geomID = 0); + protected: void build_bvh( const std::vector & meshes, diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index a63606d0..b414c3d2 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -103,6 +103,12 @@ eval_hit embree::intersect(const vertex & org, const vertex & dir, const bool fl return hit; } +vec4 * embree::pc_data(const index_t geomID) +{ + RTCGeometry geom = rtcGetGeometry(rtc_scene, geomID); + return (vec4 *) rtcGetGeometryBufferData(geom, RTC_BUFFER_TYPE_VERTEX, 0); +} + void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc) { g_meshes.resize(size(meshes)); From b612b386f5aba129f224a9e1398860883a09c5f7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 16 Mar 2024 16:41:47 +0100 Subject: [PATCH 0950/1018] remove CGAL dependency pending mdict testing and upgrade --- CMakeLists.txt | 4 -- include/gproshan/mdict/patch.h | 1 - src/gproshan/CMakeLists.txt | 1 - src/gproshan/mdict/patch.cpp | 70 +++------------------------------- 4 files changed, 6 insertions(+), 70 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bff76e87..7eb25c1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,8 +56,6 @@ find_package(GLEW REQUIRED) find_package(glfw3 REQUIRED) find_package(X11 REQUIRED) find_package(Armadillo REQUIRED) -find_package(Eigen3 REQUIRED) -find_package(CGAL REQUIRED) find_package(SuiteSparse REQUIRED) find_package(flann REQUIRED) @@ -66,8 +64,6 @@ include_directories(SYSTEM ${embree_INCLUDE_DIRS}) include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) include_directories(SYSTEM ${GLFW3_INCLUDE_DIRS}) include_directories(SYSTEM ${AMADILLO_INCLUDE_DIR}) -include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS}) -include_directories(SYSTEM ${CGAL_INCLUDE_DIRS}) include_directories(SYSTEM ${SuiteSparse_INCLUDE_DIRS}) include_directories(SYSTEM ${flann_INCLUDE_DIRS}) diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 70982364..61d9b56e 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -132,7 +132,6 @@ class patch bool exists(index_t idx); /// Initialize transformation matrix T and translation vector x, using CGAL jet_fitting. - void jet_fit_directions(che * mesh, const index_t v); void normal_fit_directions(che * mesh, const index_t v); real_t get_min_z(); real_t get_max_z(); diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index ae78d148..815f72ab 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -11,7 +11,6 @@ target_link_libraries(gproshan GLEW::GLEW) target_link_libraries(gproshan glfw) target_link_libraries(gproshan X11::X11) target_link_libraries(gproshan ${ARMADILLO_LIBRARIES}) -target_link_libraries(gproshan CGAL::CGAL) target_link_libraries(gproshan ${SuiteSparse_LIBRARIES}) target_link_libraries(gproshan flann::flann_cpp) target_link_libraries(gproshan ${OptiX_LIBRARY}) diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index 619ad2a3..fb09387f 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -6,31 +6,12 @@ #include #include -#ifndef CGAL_PATCH_DEFS - #define CGAL_PATCH_DEFS - #define CGAL_EIGEN3_ENABLED - #define CGAL_USE_BOOST_PROGRAM_OPTIONS - #define CGAL_USE_GMP - #define DCGAL_USE_MPFR -#endif - -#include -#include - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -typedef real_t DFT; -typedef CGAL::Simple_cartesian Data_Kernel; -typedef Data_Kernel::Point_3 DPoint; -typedef Data_Kernel::Vector_3 DVector; -typedef CGAL::Monge_via_jet_fitting My_Monge_via_jet_fitting; -typedef My_Monge_via_jet_fitting::Monge_form My_Monge_form; - - size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); real_t patch::nyquist_factor = 0.5; @@ -40,7 +21,7 @@ void patch::init(che * mesh, const index_t v, const size_t n_toplevels, const re index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; gather_vertices(mesh, v, n_toplevels, toplevel); - jet_fit_directions(mesh, v); + normal_fit_directions(mesh, v); gather_vertices(mesh, v, radio_, toplevel); if(!_toplevel) delete [] toplevel; @@ -52,7 +33,7 @@ void patch::init_disjoint(che * mesh, const index_t v, const size_t n_toplevels, index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; gather_vertices(mesh, v, n_toplevels, toplevel); - jet_fit_directions(mesh, v); + normal_fit_directions(mesh, v); //vertices = _vertices; vertices = std::move(_vertices); @@ -203,7 +184,7 @@ void patch::recover_radial_disjoint(che * mesh, const real_t radio_, const index size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; if(size(vertices) > min_points) { - jet_fit_directions(mesh, v); + normal_fit_directions(mesh, v); n.x() = T(0, 2); n.y() = T(1, 2); n.z() = T(2, 2); radio = -INFINITY; @@ -283,7 +264,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, size_t d_fitting = 2; size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; if(size(vertices) > min_points) - jet_fit_directions(mesh, v); + normal_fit_directions(mesh, v); else normal_fit_directions(mesh,v); @@ -388,7 +369,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche // create a random point real_t a = abs(dis(gen)) * 2 * M_PI; real_t r = abs(dis(gen)); - a_vec np = { r * cos(a), r * sin(a), 0 }; + a_vec np = { r * std::cos(a), r * std::sin(a), 0 }; //gproshan_debug_var(np); // find the closest point @@ -443,7 +424,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche a_vec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); np = proj_abc * coef + abc.col(0); - if(!isnan(np(2))) + if(!std::isnan(np(2))) { xyz(0, j) = np(0); xyz(1, j) = np(1); @@ -590,45 +571,6 @@ void patch::gather_vertices(che * mesh, const index_t v, const real_t radio, ind } } -/// Compute the principal directions of the patch, centering in the vertex \f$v\f$. -/// See: https://doc.cgal.org/latest/Jet_fitting_3/index.html -void patch::jet_fit_directions(che * mesh, const index_t v) -{ - size_t d_fitting = 2; - size_t d_monge = 2; - //size_t min_points = (d_fitting + 1) * (d_fitting + 2) / 2; - //assert(size(vertices) > min_points); - - std::vector in_points; - in_points.reserve(size(vertices)); - for(const index_t u: vertices) - in_points.push_back(DPoint(mesh->point(u).x(), mesh->point(u).y(), mesh->point(u).z())); - - My_Monge_form monge_form; - My_Monge_via_jet_fitting monge_fit; - monge_form = monge_fit(begin(in_points), end(in_points), d_fitting, d_monge); - - vertex normal = mesh->normal(v); - monge_form.comply_wrt_given_normal(DVector(normal.x(), normal.y(), normal.z())); - - x.set_size(3); - x(0) = mesh->point(v).x(); - x(1) = mesh->point(v).y(); - x(2) = mesh->point(v).z(); - - T.set_size(3, 3); - T(0, 0) = monge_form.maximal_principal_direction()[0]; - T(1, 0) = monge_form.maximal_principal_direction()[1]; - T(2, 0) = monge_form.maximal_principal_direction()[2]; - T(0, 1) = monge_form.minimal_principal_direction()[0]; - T(1, 1) = monge_form.minimal_principal_direction()[1]; - T(2, 1) = monge_form.minimal_principal_direction()[2]; - T(0, 2) = monge_form.normal_direction()[0]; - T(1, 2) = monge_form.normal_direction()[1]; - T(2, 2) = monge_form.normal_direction()[2]; - -} - void patch::normal_fit_directions(che * mesh, const index_t v) { x.set_size(3); From ce7d85b3b4a55b9217e5e0b50cea4d632499980c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 16 Mar 2024 16:49:09 +0100 Subject: [PATCH 0951/1018] update github workflow and CHANGELOG --- .github/workflows/build.yml | 1 - CHANGELOG.md | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f2baa3ad..543e5631 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,7 +28,6 @@ jobs: sudo apt install \ libarmadillo-dev \ libeigen3-dev \ - libcgal-dev \ libsuitesparse-dev \ libopenblas-dev \ libglew-dev \ diff --git a/CHANGELOG.md b/CHANGELOG.md index 75e768dc..c42bdc69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ Version History --------------- -### gproshan 3.14 -- Added scene rendering: loader from .obj files and .mtl, handling textures +### gproshan v4.0 + +- Added KNN module using flann for 3D point clouds. +- Added a simple path tracer implementation using Embree and OptiX. +- Added scene rendering: loader from .obj files and .mtl, handling textures. - Exporting gproshan as cmake library, use find_package(gproshan) in your project. - Added Intel Embree as default ray tracing library, for ray casting operations and as rendering option with shadows. - Adding Scenes module, virtual point cloud scanners and point cloud normals computation for 3D scenes. From 1dcaf87966f5bc4a0f4a633d8f7f35969db4fc0a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 17 Mar 2024 01:07:34 +0100 Subject: [PATCH 0952/1018] geodesics: fix #19, use std::abs float/double issue --- include/gproshan/geodesics/geodesics_ptp.h | 24 ++++++++++------------ include/gproshan/mdict/patch.h | 5 ----- src/gproshan/geodesics/geodesics_ptp.cpp | 15 +++++++++++++- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index dbc267b0..b7e806f0 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -206,15 +206,15 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, { if(i < (j >> 1)) i = (j >> 1); // K/2 limit band size - const index_t start = limits[i]; + const index_t start = limits[i]; const index_t end = limits[j]; const index_t n_cond = limits[i + 1] - start; - T *& new_dist = dist[iter & 1]; - T *& old_dist = dist[!(iter & 1)]; + T * new_dist = dist[iter & 1]; + T * old_dist = dist[!(iter & 1)]; - index_t *& new_cluster = clusters[iter & 1]; - index_t *& old_cluster = clusters[!(iter & 1)]; + index_t * new_cluster = clusters[iter & 1]; + index_t * old_cluster = clusters[!(iter & 1)]; #ifdef __CUDACC__ relax_ptp<<< NB(end - start), NT >>>(mesh, new_dist, old_dist, new_cluster, old_cluster, start, end, sorted); @@ -230,19 +230,17 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, for(index_t v = start; v < end; ++v) relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); - #pragma omp parallel for - for(index_t k = start; k < start + n_cond; ++k) - { - const index_t v = sorted ? sorted[k] : k; - error[v] = abs(new_dist[v] - old_dist[v]) / old_dist[v]; - } count = 0; - #pragma omp parallel for reduction(+: count) + #pragma omp parallel for for(index_t k = start; k < start + n_cond; ++k) { const index_t v = sorted ? sorted[k] : k; - count += error[v] < PTP_TOL; + if(std::abs(new_dist[v] - old_dist[v]) / old_dist[v] < PTP_TOL) + { + #pragma omp atomic + ++count; + } } #endif diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index 61d9b56e..cfb51b1f 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -12,11 +12,6 @@ #include -#ifdef Success - #undef Success -#endif - - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 9abcea88..926225a0 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -68,7 +68,12 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c for(index_t v = 0; v < n_vertices; ++v) dist[!i][v] = dist[i][v]; - if(inv) + gproshan_log_var(inv); + gproshan_log_var(dist[0]); + gproshan_log_var(dist[1]); + gproshan_log_var(ptp_out.dist); + + if(coalescence) { #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) @@ -80,6 +85,14 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c delete [] error; delete [] dist[1]; delete [] clusters[1]; + + if(coalescence) + { + delete [] h_mesh.GT; + delete [] h_mesh.VT; + delete [] h_mesh.EVT; + } + delete [] inv; } From 4877ac75c3914f285b57c602e1b618acfd005fda Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Mar 2024 11:42:44 +0100 Subject: [PATCH 0953/1018] geodesics: update ptp cpu --- include/gproshan/geodesics/geodesics_ptp.h | 9 +++++---- src/gproshan/geodesics/geodesics_ptp.cpp | 4 +--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index b7e806f0..4604ca48 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -154,13 +154,14 @@ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clust template #ifdef __CUDACC__ -__forceinline__ -#else -inline -#endif index_t run_ptp(const CHE * mesh, const std::vector & sources, const std::vector & limits, T * error, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted) +#else +index_t run_ptp(const CHE * mesh, const std::vector & sources, + const std::vector & limits, T ** dist, index_t ** clusters, + const index_t * idx, index_t * sorted) +#endif { #ifdef __CUDACC__ T * h_dist = dist[2]; diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 926225a0..5317f543 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -45,7 +45,6 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c } } - real_t * error = new real_t[n_vertices]; real_t * dist[2] = { coalescence ? new real_t[n_vertices] : ptp_out.dist, new real_t[n_vertices] }; @@ -60,7 +59,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c dist[0][v] = dist[1][v] = INFINITY; } - const index_t i = run_ptp(&h_mesh, sources, toplesets.limits, error, dist, clusters, + const index_t i = run_ptp(&h_mesh, sources, toplesets.limits, dist, clusters, coalescence ? inv : toplesets.index, coalescence ? nullptr : (index_t *) toplesets.index); @@ -82,7 +81,6 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c delete [] dist[0]; } - delete [] error; delete [] dist[1]; delete [] clusters[1]; From 5bb647cb0e9efb54fff13cf3cd611c99b644faf4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Mar 2024 14:39:50 +0100 Subject: [PATCH 0954/1018] geodesics: #15 passing custom user function, useful for testing for example --- include/gproshan/geodesics/geodesics_ptp.h | 37 ++++++++++++++++++---- src/gproshan/geodesics/geodesics_ptp.cpp | 21 ++++++------ src/gproshan/geodesics/geodesics_ptp.cu | 14 ++++++-- 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 4604ca48..56adcbdf 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -8,6 +8,8 @@ #include +#include + #ifdef __CUDACC__ #include @@ -49,8 +51,8 @@ struct is_ok struct ptp_out_t { - real_t * dist; - index_t * clusters; + real_t * dist = nullptr; + index_t * clusters = nullptr; ptp_out_t(real_t *const d, index_t *const c = nullptr); }; @@ -61,9 +63,29 @@ struct toplesets_t const index_t * index; }; -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence = true, const bool set_inf = true); -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence = false, const bool set_inf = true); +template +using f_ptp = std::function; + + +double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, + const che * mesh, + const std::vector & sources, + const toplesets_t & toplesets, + const bool coalescence = true, + const bool set_inf = true, + const f_ptp & fun = nullptr + ); + +void parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, + const che * mesh, + const std::vector & sources, + const toplesets_t & toplesets, + const bool coalescence = true, + const bool set_inf = true, + const f_ptp & fun = nullptr + ); + real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); @@ -152,15 +174,16 @@ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clust } } + template #ifdef __CUDACC__ index_t run_ptp(const CHE * mesh, const std::vector & sources, const std::vector & limits, T * error, T ** dist, index_t ** clusters, - const index_t * idx, index_t * sorted) + const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) #else index_t run_ptp(const CHE * mesh, const std::vector & sources, const std::vector & limits, T ** dist, index_t ** clusters, - const index_t * idx, index_t * sorted) + const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) #endif { #ifdef __CUDACC__ @@ -245,6 +268,8 @@ index_t run_ptp(const CHE * mesh, const std::vector & sources, } #endif + if(fun) fun(new_dist, i, j, start, end); + if(n_cond == count) ++i; if(j < size(limits) - 1) ++j; } diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 5317f543..60f13e8a 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -9,10 +9,17 @@ namespace gproshan { -ptp_out_t::ptp_out_t(real_t * d, index_t * c): dist(d), clusters(c) {} +ptp_out_t::ptp_out_t(real_t *const d, index_t *const c): dist(d), clusters(c) {} -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence, const bool set_inf) +void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, + const che * mesh, + const std::vector & sources, + const toplesets_t & toplesets, + const bool coalescence, + const bool set_inf, + const f_ptp & fun + ) { CHE h_mesh(mesh); const size_t n_vertices = h_mesh.n_vertices; @@ -59,19 +66,15 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, che * mesh, c dist[0][v] = dist[1][v] = INFINITY; } - const index_t i = run_ptp(&h_mesh, sources, toplesets.limits, dist, clusters, + const index_t i = run_ptp( &h_mesh, sources, toplesets.limits, dist, clusters, coalescence ? inv : toplesets.index, - coalescence ? nullptr : (index_t *) toplesets.index); + coalescence ? nullptr : (index_t *) toplesets.index, + fun); #pragma omp parallel for for(index_t v = 0; v < n_vertices; ++v) dist[!i][v] = dist[i][v]; - gproshan_log_var(inv); - gproshan_log_var(dist[0]); - gproshan_log_var(dist[1]); - gproshan_log_var(ptp_out.dist); - if(coalescence) { #pragma omp parallel for diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 17f7f536..b000bac2 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -12,7 +12,14 @@ namespace gproshan { -double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, const toplesets_t & toplesets, const bool coalescence, const bool set_inf) +double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, + const che * mesh, + const std::vector & sources, + const toplesets_t & toplesets, + const bool coalescence, + const bool set_inf, + const f_ptp & fun + ) { CHE h_mesh(mesh); const size_t n_vertices = h_mesh.n_vertices; @@ -88,8 +95,9 @@ double parallel_toplesets_propagation_gpu(const ptp_out_t & ptp_out, const che * h_dist[v] = INFINITY; } - const index_t i = run_ptp(d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, - coalescence ? inv : toplesets.index, d_sorted); + const index_t i = run_ptp( d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, + coalescence ? inv : toplesets.index, d_sorted, + fun); cudaMemcpy(h_dist, d_dist[i], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); From 9655f2d005ce80231b957050b31450346af7bedc Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 18 Mar 2024 15:29:24 +0100 Subject: [PATCH 0955/1018] geodesics: #15 cleaning up extra sources --- .../gproshan/geodesics/test_geodesics_ptp.cuh | 19 -- .../gproshan/geodesics/test_geodesics_ptp.h | 14 - .../test_geodesics_ptp_coalescence.cuh | 19 -- include/gproshan/mesh/che.h | 2 +- src/gproshan/geodesics/test_geodesics_ptp.cpp | 45 +-- src/gproshan/geodesics/test_geodesics_ptp.cu | 212 -------------- .../test_geodesics_ptp_coalescence.cu | 267 ------------------ src/gproshan/mesh/che.cpp | 2 +- 8 files changed, 25 insertions(+), 555 deletions(-) delete mode 100644 include/gproshan/geodesics/test_geodesics_ptp.cuh delete mode 100644 include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh delete mode 100644 src/gproshan/geodesics/test_geodesics_ptp.cu delete mode 100644 src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu diff --git a/include/gproshan/geodesics/test_geodesics_ptp.cuh b/include/gproshan/geodesics/test_geodesics_ptp.cuh deleted file mode 100644 index 4e072e00..00000000 --- a/include/gproshan/geodesics/test_geodesics_ptp.cuh +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TEST_GEODESICS_PTP_CUH -#define TEST_GEODESICS_PTP_CUH - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -/// Return an array with the error per iteration. -/// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error); - - -} // namespace gproshan - -#endif // TEST_GEODESICS_PTP_CUH - diff --git a/include/gproshan/geodesics/test_geodesics_ptp.h b/include/gproshan/geodesics/test_geodesics_ptp.h index 302c0ff7..8ce682a1 100644 --- a/include/gproshan/geodesics/test_geodesics_ptp.h +++ b/include/gproshan/geodesics/test_geodesics_ptp.h @@ -25,20 +25,6 @@ double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std: double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test); -/// Return an array with the error per iteration. -/// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp); - -/// Return an array with the error per iteration. -/// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp); - -/// Return an array with the time per iteration. -double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, real_t radio = 0); - -/// Return an array with the time per iteration. -double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, real_t radio = 0); - #endif // GPROSHAN_CUDA diff --git a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh b/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh deleted file mode 100644 index 42f5bd12..00000000 --- a/include/gproshan/geodesics/test_geodesics_ptp_coalescence.cuh +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TEST_GEODESICS_PTP_COALESCENCE_CUH -#define TEST_GEODESICS_PTP_COALESCENCE_CUH - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -/// Return an array with the error per iteration. -/// Starting to store (position 0) errors after number of toplesets. -std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error); - - -} // namespace gproshan - -#endif // TEST_GEODESICS_PTP_COALESCENCE_CUH - diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 7ac7de82..26934000 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -129,7 +129,7 @@ class che che::star_he star(const index_t v) const; std::vector link(const index_t v) const; void edge_collapse(const std::vector & sort_edges); - void compute_toplesets(index_t *& rings, index_t *& sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); + void compute_toplesets(index_t * rings, index_t * sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); // boundary methods std::vector bounds() const; diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp index 14453ef4..8035bc4d 100644 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ b/src/gproshan/geodesics/test_geodesics_ptp.cpp @@ -45,12 +45,12 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) std::vector source = { 0 }; che * mesh = new che_off(data_path + filename + ".off"); - size_t n_vertices = mesh->n_vertices; + const size_t n_vertices = mesh->n_vertices; - index_t * toplesets = new index_t[n_vertices]; - index_t * sorted_index = new index_t[n_vertices]; + std::vector toplesets(n_vertices); + std::vector sorted_index(n_vertices); std::vector limits; - mesh->compute_toplesets(toplesets, sorted_index, limits, source); + mesh->compute_toplesets(toplesets.data(), sorted_index.data(), limits, source); // PERFORMANCE & ACCURACY ___________________________________________________________________ @@ -62,10 +62,10 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) if(!exact) fprintf(stderr, "no exact geodesics for: %s.\n", filename.c_str()); Time[0] = test_fast_marching(Error[0], exact, mesh, source, n_test); - Time[1] = test_ptp_cpu(Error[1], exact, mesh, source, {limits, sorted_index}, n_test); + Time[1] = test_ptp_cpu(Error[1], exact, mesh, source, {limits, sorted_index.data()}, n_test); #ifdef GPROSHAN_CUDA - Time[2] = test_ptp_gpu(Error[2], exact, mesh, source, {limits, sorted_index}, n_test); + Time[2] = test_ptp_gpu(Error[2], exact, mesh, source, {limits, sorted_index.data()}, n_test); #else Time[2] = INFINITY; #endif // GPROSHAN_CUDA @@ -170,7 +170,7 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) // TOPLESETS DISTRIBUTION __________________________________________________________________ - index_t * toplesets_dist = new index_t[size(limits) - 1]; + std::vector toplesets_dist(size(limits) - 1); os.open(test_path + filename + "_toplesets.dist"); for(index_t i = 1; i < size(limits); ++i) @@ -180,7 +180,7 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) } os.close(); - std::sort(toplesets_dist, toplesets_dist + size(limits) - 1); + std::ranges::sort(toplesets_dist); os.open(test_path + filename + "_toplesets_sorted.dist"); for(index_t i = 0; i < size(limits) - 1; ++i) @@ -192,10 +192,7 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) #ifdef GPROSHAN_CUDA // IMPLEMENT: iter_error_parallel_toplesets_propagation_coalescence_cpu - double time; - std::vector > iter_error = iter_error_parallel_toplesets_propagation_coalescence_gpu(mesh, source, limits, sorted_index, exact, time); - - system(("mv band " + (test_path + filename + ".band")).c_str()); + std::ofstream band(test_path + filename + ".band"); #ifndef GPROSHAN_FLOAT os.open(test_path + filename + "_error_double.iter"); @@ -203,35 +200,39 @@ void main_test_geodesics_ptp(const int nargs, const char ** args) os.open(test_path + filename + "_error.iter"); #endif - for(auto & p: iter_error) - os << p.first << " " << p.second << std::endl; + std::vector dist(mesh->n_vertices); + parallel_toplesets_propagation_gpu(dist.data(), mesh, source, {limits, sorted_index.data()}, true, true, + [&](real_t * d, index_t i, index_t j, index_t start, index_t end) + { + band << i << " " << " " << j << " " << end - start << "\n"; + + os << compute_error(dist.data(), exact, n_vertices, sizeof(source)) << "\n"; + }); + os.close(); + band.close(); #endif // GPROSHAN_CUDA // FARTHEST POINT SAMPLING _________________________________________________________________ -#ifdef GPROSHAN_CUDA // IMPLEMENT: times_farthest_point_sampling_ptp_cpu +#ifdef GPROSHAN_CUDA // IMPLEMENT: times_farthest_point_sampling_ptp_cpu // no coalescence size_t i_samples = size(source); size_t n_samples = 1001; - double * times_fps = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); + //double * times_fps; = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); os.open(test_path + filename + ".fps"); - for(index_t i = i_samples; i < n_samples; ++i) - os << i << " " << times_fps[i] << std::endl; +// for(index_t i = i_samples; i < n_samples; ++i) +// os << i << " " << times_fps[i] << std::endl; os.close(); - delete [] times_fps; #endif // GPROSHAN_CUDA // FREE MEMORY delete mesh; - delete [] toplesets; - delete [] sorted_index; delete [] exact; - delete [] toplesets_dist; } fclose(ftable); diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cu b/src/gproshan/geodesics/test_geodesics_ptp.cu deleted file mode 100644 index 7229d6ff..00000000 --- a/src/gproshan/geodesics/test_geodesics_ptp.cu +++ /dev/null @@ -1,212 +0,0 @@ -#include -#include - -#include - -#include -#include - -#include -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -std::vector > iter_error_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) -{ - cudaDeviceReset(); - - float time; - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - cudaEventRecord(start, 0); - - // BEGIN PTP - - CHE * h_mesh = new CHE(mesh); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - - real_t * h_dist = new real_t[h_mesh->n_vertices]; - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - - index_t * d_sorted; - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - - std::vector > iter_error = iter_error_run_ptp_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, sorted_index, d_sorted, exact_dist, d_error); - - delete [] h_dist; - cudaFree(d_error); - cudaFree(d_dist[0]); - cudaFree(d_dist[1]); - cudaFree(d_sorted); - cuda_free_CHE(dd_mesh, d_mesh); - - // END PTP - - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - time_ptp = time / 1000; - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return iter_error; -} - -/// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, real_t radio) -{ - cudaDeviceReset(); - - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - - // BEGIN FPS PTP - - CHE * h_mesh = new CHE(mesh); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - - real_t * h_dist = new real_t[h_mesh->n_vertices]; - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - - index_t * d_sorted; - cudaMalloc(&d_sorted, sizeof(index_t) * h_mesh->n_vertices); - - std::vector limits; - index_t * toplesets = new index_t[h_mesh->n_vertices]; - index_t * sorted_index = new index_t[h_mesh->n_vertices]; - - cublasHandle_t handle; - cublasCreate(&handle); - - if(n >= h_mesh->n_vertices) n = h_mesh->n_vertices >> 1; - - double * times = new double[n + 1]; - - n -= size(samples); - samples.reserve(n); - - float time_fps; - index_t d; - int f; - real_t max_dist = INFINITY; - while(n-- && max_dist > radio) - { - cudaEventRecord(start, 0); - - limits.clear(); - mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - -// d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, sorted_index}, d_error, nullptr, nullptr, d_sorted); - - // 1 indexing - #ifdef GPROSHAN_FLOAT - cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); - #else - cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); - #endif - - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time_fps, start, stop); - - times[size(samples)] = time_fps / 1000; - - if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); - - samples.push_back(f - 1); - } - - cublasDestroy(handle); - - delete [] h_dist; - delete [] toplesets; - delete [] sorted_index; - - cudaFree(d_error); - cudaFree(d_dist[0]); - cudaFree(d_dist[1]); - cudaFree(d_sorted); - cuda_free_CHE(dd_mesh, d_mesh); - - // END FPS PTP - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return times; -} - -std::vector > iter_error_run_ptp_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * h_sorted, index_t * d_sorted, const real_t * exact_dist, real_t * d_error) -{ - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - h_dist[v] = INFINITY; - - for(index_t i = 0; i < size(sources); ++i) - h_dist[sources[i]] = 0; - - cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_sorted, h_sorted, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); - - std::vector > iter_error; - iter_error.reserve(size(limits)); - - index_t d = 0; - index_t start, end, n_cond; - index_t i = 1, j = 2; - index_t n_iter = 0; - - while(i < j) - { - ++n_iter; - start = limits[i]; - end = limits[j]; - n_cond = limits[i + 1] - start; - - relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end, d_sorted); - - // begin calculating iteration error - cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); - if(j == size(limits) - 1) - iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, size(sources))}); - // end - - relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond, d_sorted); - cudaDeviceSynchronize(); - - if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - ++i; - if(j < size(limits) - 1) ++j; - - d = !d; - } - - return iter_error; -} - - -} // namespace gproshan - diff --git a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu b/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu deleted file mode 100644 index 97b4cee5..00000000 --- a/src/gproshan/geodesics/test_geodesics_ptp_coalescence.cu +++ /dev/null @@ -1,267 +0,0 @@ -#include - -#include -#include - -#include - -#include -#include - -#include -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -std::vector > iter_error_parallel_toplesets_propagation_coalescence_gpu(che * mesh, const std::vector & sources, const std::vector & limits, const index_t * sorted_index, const real_t * exact_dist, double & time_ptp) -{ - // sort data by levels, must be improve the coalescence - - vertex * V = new vertex[mesh->n_vertices]; - index_t * F = new index_t[mesh->n_trigs * che::mtrig]; - index_t * inv = new index_t[mesh->n_vertices]; - real_t * exact_dist_sorted = new real_t[mesh->n_vertices]; - - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - { - V[i] = mesh->point(sorted_index[i]); - inv[sorted_index[i]] = i; - exact_dist_sorted[i] = exact_dist[sorted_index[i]]; - } - - #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; ++he) - F[he] = inv[mesh->halfedge(he)]; - - mesh = new che(V, mesh->n_vertices, F, mesh->n_trigs); - - delete [] V; - delete [] F; - - // ------------------------------------------------------ - - cudaDeviceReset(); - - float time; - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - cudaEventRecord(start, 0); - - // BEGIN PTP - - CHE * h_mesh = new CHE(mesh); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - - real_t * h_dist = new real_t[h_mesh->n_vertices]; - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * h_mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * h_mesh->n_vertices); - - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * h_mesh->n_vertices); - - std::vector > iter_error = iter_error_run_ptp_coalescence_gpu(d_mesh, h_mesh->n_vertices, h_dist, d_dist, sources, limits, inv, exact_dist_sorted, d_error); - - delete [] h_dist; - cudaFree(d_error); - cudaFree(d_dist[0]); - cudaFree(d_dist[1]); - cuda_free_CHE(dd_mesh, d_mesh); - - // END PTP - - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time, start, stop); - time_ptp = time / 1000; - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - delete mesh; - delete [] inv; - - return iter_error; -} - -/// Return an array of time in seconds. -double * times_farthest_point_sampling_ptp_coalescence_gpu(che * mesh, std::vector & samples, size_t n, real_t radio) -{ - cudaDeviceReset(); - - cudaEvent_t start, stop; - cudaEventCreate(&start); - cudaEventCreate(&stop); - - // BEGIN FPS PTP - - vertex * V = new vertex[mesh->n_vertices]; - index_t * F = new index_t[mesh->n_trigs * che::mtrig]; - index_t * inv = new index_t[mesh->n_vertices]; - - - real_t * h_dist = new real_t[mesh->n_vertices]; - - real_t * d_dist[2]; - cudaMalloc(&d_dist[0], sizeof(real_t) * mesh->n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * mesh->n_vertices); - - real_t * d_error; - cudaMalloc(&d_error, sizeof(real_t) * mesh->n_vertices); - - std::vector limits; - index_t * toplesets = new index_t[mesh->n_vertices]; - index_t * sorted_index = new index_t[mesh->n_vertices]; - - cublasHandle_t handle; - cublasCreate(&handle); - - if(n >= mesh->n_vertices) n = mesh->n_vertices >> 1; - - double * times = new double[n + 1]; - - n -= size(samples); - samples.reserve(n); - - float time_fps; - index_t d; - int f; - real_t max_dist = INFINITY; - while(n-- && max_dist > radio) - { - cudaEventRecord(start, 0); - - limits.clear(); - mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - - // sort data by levels, must be improve the coalescence - - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - { - V[i] = mesh->point(sorted_index[i]); - inv[sorted_index[i]] = i; - } - - #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; ++he) - F[he] = inv[mesh->halfedge(he)]; - - che * tmp_mesh = new che(V, mesh->n_vertices, F, mesh->n_trigs); - - CHE * h_mesh = new CHE(tmp_mesh); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(h_mesh, dd_mesh, d_mesh); - - // exec algorithm - //d = run_ptp_gpu(d_mesh, samples, h_mesh->n_vertices, h_dist, d_dist, {limits, inv}, d_error); - - // free memory - cuda_free_CHE(dd_mesh, d_mesh); - delete tmp_mesh; - - // 1 indexing - #ifdef GPROSHAN_FLOAT - cublasIsamax(handle, mesh->n_vertices, d_dist[d], 1, &f); - #else - cublasIdamax(handle, mesh->n_vertices, d_dist[d], 1, &f); - #endif - - cudaEventRecord(stop, 0); - cudaEventSynchronize(stop); - cudaEventElapsedTime(&time_fps, start, stop); - - times[size(samples)] = time_fps / 1000; - - if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[d] + f - 1, sizeof(real_t), cudaMemcpyDeviceToHost); - - samples.push_back(sorted_index[f - 1]); - } - - cublasDestroy(handle); - - delete [] V; - delete [] F; - delete [] inv; - delete [] h_dist; - delete [] toplesets; - delete [] sorted_index; - - cudaFree(d_error); - cudaFree(d_dist[0]); - cudaFree(d_dist[1]); - - // END FPS PTP - - cudaEventDestroy(start); - cudaEventDestroy(stop); - - return times; -} - -std::vector > iter_error_run_ptp_coalescence_gpu(CHE * d_mesh, const index_t n_vertices, real_t * h_dist, real_t ** d_dist, const std::vector & sources, const std::vector & limits, const index_t * inv, const real_t * exact_dist, real_t * d_error) -{ - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - h_dist[v] = INFINITY; - - for(index_t i = 0; i < size(sources); ++i) - h_dist[inv[sources[i]]] = 0; - - cudaMemcpy(d_dist[0], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMemcpy(d_dist[1], h_dist, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); - - std::vector > iter_error; - iter_error.reserve(size(limits)); - - std::ofstream os("band"); - - index_t d = 0; - index_t start, end, n_cond; - index_t i = 1, j = 2; - index_t n_iter = 0; - - while(i < j) - { - ++n_iter; - start = limits[i]; - end = limits[j]; - n_cond = limits[i + 1] - start; - - relax_ptp <<< NB(end - start), NT >>> (d_mesh, d_dist[!d], d_dist[d], nullptr, nullptr, start, end); - // print band info - os << n_iter << " " << i << " " << j << " " << end - start << std::endl; - - // begin calculating iteration error - cudaMemcpy(h_dist, d_dist[!d], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); - if(j == size(limits) - 1) - iter_error.push_back({n_iter, compute_error(h_dist, exact_dist, n_vertices, size(sources))}); - // end - - relative_error <<< NB(n_cond), NT >>> (d_error, d_dist[!d], d_dist[d], start, start + n_cond); - cudaDeviceSynchronize(); - - if(n_cond == thrust::count_if(thrust::device, d_error + start, d_error + start + n_cond, is_ok())) - ++i; - if(j < size(limits) - 1) ++j; - - d = !d; - } - - os.close(); - - return iter_error; -} - - -} // namespace gproshan - diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index f1378dfc..bf4ca766 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -681,7 +681,7 @@ void che::edge_collapse(const std::vector & sort_edges) // TODO } -void che::compute_toplesets(index_t *& toplesets, index_t *& sorted, std::vector & limits, const std::vector & sources, const index_t k) +void che::compute_toplesets(index_t * toplesets, index_t * sorted, std::vector & limits, const std::vector & sources, const index_t k) { if(!size(sources)) return; From 67650afa15d488079d052967a3abb5847043e174 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Mar 2024 12:52:26 +0100 Subject: [PATCH 0956/1018] geodesics: remove/move test source --- apps/CMakeLists.txt | 5 +- apps/test_geodesics.cpp | 9 - .../gproshan/geodesics/test_geodesics_ptp.h | 41 -- src/gproshan/geodesics/test_geodesics_ptp.cpp | 369 ------------------ 4 files changed, 1 insertion(+), 423 deletions(-) delete mode 100644 apps/test_geodesics.cpp delete mode 100644 include/gproshan/geodesics/test_geodesics_ptp.h delete mode 100644 src/gproshan/geodesics/test_geodesics_ptp.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 50c007f5..ed7aa6c7 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -6,13 +6,10 @@ set_target_properties(test_gproshan PROPERTIES OUTPUT_NAME gproshan) target_link_libraries(test_gproshan gproshan) -add_executable(test_geodesics test_geodesics.cpp) -target_link_libraries(test_geodesics gproshan) - - add_executable(test_image_denoising test_image_denoising.cpp) target_link_libraries(test_image_denoising gproshan) + add_executable(test_scanner test_scanner.cpp) target_link_libraries(test_scanner gproshan) diff --git a/apps/test_geodesics.cpp b/apps/test_geodesics.cpp deleted file mode 100644 index 20d9c4df..00000000 --- a/apps/test_geodesics.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - - -int main(int nargs, const char ** args) -{ - gproshan::main_test_geodesics_ptp(nargs, args); - return 0; -} - diff --git a/include/gproshan/geodesics/test_geodesics_ptp.h b/include/gproshan/geodesics/test_geodesics_ptp.h deleted file mode 100644 index 8ce682a1..00000000 --- a/include/gproshan/geodesics/test_geodesics_ptp.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef TEST_GEODESICS_PTP_H -#define TEST_GEODESICS_PTP_H - -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -/// Execute performance and accuracy test for ptp algorithm on cpu and gpu. -void main_test_geodesics_ptp(const int nargs, const char ** args); - -double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int n_test); - -double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test); - -double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test); - - -#ifdef GPROSHAN_CUDA - -double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test); - -double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test); - -#endif // GPROSHAN_CUDA - - -/// Exact geodesics computed using MeshLP https://github.com/areslp/matlab/tree/master/MeshLP/MeshLP, -/// Geodesics code: http://code.google.com/p/geodesic/ -real_t * load_exact_geodesics(const std::string & file, const size_t n); - -real_t compute_error(const real_t * dist, const real_t * exact, const size_t n, const size_t s); - - -} // namespace gproshan - -#endif // TEST_GEODESICS_PTP_H - diff --git a/src/gproshan/geodesics/test_geodesics_ptp.cpp b/src/gproshan/geodesics/test_geodesics_ptp.cpp deleted file mode 100644 index 8035bc4d..00000000 --- a/src/gproshan/geodesics/test_geodesics_ptp.cpp +++ /dev/null @@ -1,369 +0,0 @@ -#include - -#include -#include -#include - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -void main_test_geodesics_ptp(const int nargs, const char ** args) -{ - if(nargs < 4) - { - printf("./test_geodesics [data_path] [test_path] [exact_dist_path] [n_test = 10]\n"); - return; - } - - const char * data_path = args[1]; - const char * test_path = args[2]; - const char * exact_dist_path = args[3]; - - int n_test = nargs == 5 ? atoi(args[4]) : 10; - -#ifdef GPROSHAN_FLOAT - FILE * ftable = fopen("test_geodesics_float.tex", "w"); -#else - FILE * ftable = fopen("test_geodesics_double.tex", "w"); - const char * ptime = "& %6.3lfs "; -#endif - - const char * str[2] = {"", "\\bf"}; - const char * pspeedup = "& \\bf (%.1lfx) "; - const char * pbtime = "& %6s %6.3lfs "; - const char * pberror = "& %6s %6.2lf\\%% "; - - std::string filename; - while(std::cin >> filename) - { - gproshan_debug_var(filename); - - std::vector source = { 0 }; - - che * mesh = new che_off(data_path + filename + ".off"); - const size_t n_vertices = mesh->n_vertices; - - std::vector toplesets(n_vertices); - std::vector sorted_index(n_vertices); - std::vector limits; - mesh->compute_toplesets(toplesets.data(), sorted_index.data(), limits, source); - - - // PERFORMANCE & ACCURACY ___________________________________________________________________ - - double Time[7]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse - real_t Error[5]; // FM, PTP GPU, HEAT cholmod, HEAT cusparse - - real_t * exact = load_exact_geodesics(exact_dist_path + filename + ".exact", n_vertices); - if(!exact) fprintf(stderr, "no exact geodesics for: %s.\n", filename.c_str()); - - Time[0] = test_fast_marching(Error[0], exact, mesh, source, n_test); - Time[1] = test_ptp_cpu(Error[1], exact, mesh, source, {limits, sorted_index.data()}, n_test); - -#ifdef GPROSHAN_CUDA - Time[2] = test_ptp_gpu(Error[2], exact, mesh, source, {limits, sorted_index.data()}, n_test); -#else - Time[2] = INFINITY; -#endif // GPROSHAN_CUDA - - #ifdef GPROSHAN_FLOAT - Time[6] = Time[5] = Time[4] = Time[3] = INFINITY; - Error[4] = Error[3] = INFINITY; - #else - Time[4] = test_heat_method_cholmod(Error[3], Time[3], exact, mesh, source, n_test); - if(n_vertices < 100000) - { -#ifdef GPROSHAN_CUDA - Time[6] = test_heat_method_gpu(Error[4], Time[5], exact, mesh, source, n_test); -#else - Time[6] = Time[5] = INFINITY; - Error[4] = INFINITY; -#endif // GPROSHAN_CUDA - } - else - { - Time[6] = Time[5] = INFINITY; - Error[4] = INFINITY; - } - #endif - - index_t t_min = 0; - for(index_t i = 1; i < sizeof(Time) / sizeof(double); ++i) - if(Time[t_min] > Time[i]) t_min = i; - - index_t e_min = 0; - for(index_t i = 1; i < sizeof(Error) / sizeof(real_t); ++i) - if(Error[e_min] > Error[i]) e_min = i; - - fprintf(ftable, "%20s ", ("\\verb|" + filename + '|').c_str()); - fprintf(ftable, "& %12lu ", n_vertices); - - // FM - fprintf(ftable, pbtime, str[0 == t_min], Time[0]); - fprintf(ftable, pberror, str[0 == e_min], Error[0]); - - // PTP CPU - fprintf(ftable, pbtime, str[1 == t_min], Time[1]); - fprintf(ftable, pspeedup, Time[0] / Time[1]); - fprintf(ftable, pberror, str[1 == e_min], Error[1]); - - #ifndef GPROSHAN_FLOAT - fprintf(ftable, "& OpenMP "); - #endif - - #ifdef GPROSHAN_FLOAT - // PTP GPU - fprintf(ftable, pbtime, str[2 == t_min], Time[2]); - fprintf(ftable, pspeedup, Time[0] / Time[2]); - fprintf(ftable, pberror, str[2 == e_min], Error[2]); - #endif - - #ifndef GPROSHAN_FLOAT - // HEAT FLOW cholmod - fprintf(ftable, ptime, Time[4]); - fprintf(ftable, pbtime, str[3 == t_min], Time[3]); - fprintf(ftable, pspeedup, Time[0] / Time[3]); - fprintf(ftable, pberror, str[3 == e_min], Error[3]); - fprintf(ftable, "& Cholmod "); - #endif - fprintf(ftable, "\\\\\n"); - - #ifndef GPROSHAN_FLOAT - // PTP GPU - fprintf(ftable, "&&& "); - fprintf(ftable, pbtime, str[2 == t_min], Time[2]); - fprintf(ftable, pspeedup, Time[0] / Time[2]); - fprintf(ftable, pberror, str[2 == e_min], Error[2]); - fprintf(ftable, "& Cuda "); - - // HEAT FLOW cusparse - fprintf(ftable, ptime, Time[6]); - fprintf(ftable, pbtime, str[5 == t_min], Time[5]); - fprintf(ftable, pspeedup, Time[0] / Time[5]); - fprintf(ftable, pberror, str[4 == e_min], Error[4]); - fprintf(ftable, "& cusolverSp "); - - fprintf(ftable, "\\\\\\hline\n"); - #endif - - - // DEGREE HISTOGRAM ________________________________________________________________________ - - index_t dv; - std::map deg; - for(index_t v = 0; v < n_vertices; ++v) - { - dv = mesh->is_vertex_bound(v) ? 1 : 0; - for([[maybe_unused]] const index_t he: mesh->star(v)) ++dv; - ++deg[dv]; - } - - std::ofstream os(test_path + filename + ".deg"); - for(auto & ii: deg) - os << ii.first << " " << ii.second << std::endl; - os.close(); - - - // TOPLESETS DISTRIBUTION __________________________________________________________________ - - std::vector toplesets_dist(size(limits) - 1); - - os.open(test_path + filename + "_toplesets.dist"); - for(index_t i = 1; i < size(limits); ++i) - { - toplesets_dist[i - 1] = limits[i] - limits[i - 1]; - os << i - 1 << " " << toplesets_dist[i - 1] << std::endl; - } - os.close(); - - std::ranges::sort(toplesets_dist); - - os.open(test_path + filename + "_toplesets_sorted.dist"); - for(index_t i = 0; i < size(limits) - 1; ++i) - os << i << " " << toplesets_dist[i] << std::endl; - os.close(); - - - // PTP ITERATION ERROR _____________________________________________________________________ - -#ifdef GPROSHAN_CUDA // IMPLEMENT: iter_error_parallel_toplesets_propagation_coalescence_cpu - - std::ofstream band(test_path + filename + ".band"); - - #ifndef GPROSHAN_FLOAT - os.open(test_path + filename + "_error_double.iter"); - #else - os.open(test_path + filename + "_error.iter"); - #endif - - std::vector dist(mesh->n_vertices); - parallel_toplesets_propagation_gpu(dist.data(), mesh, source, {limits, sorted_index.data()}, true, true, - [&](real_t * d, index_t i, index_t j, index_t start, index_t end) - { - band << i << " " << " " << j << " " << end - start << "\n"; - - os << compute_error(dist.data(), exact, n_vertices, sizeof(source)) << "\n"; - }); - - os.close(); - band.close(); - -#endif // GPROSHAN_CUDA - - - // FARTHEST POINT SAMPLING _________________________________________________________________ - -#ifdef GPROSHAN_CUDA // IMPLEMENT: times_farthest_point_sampling_ptp_cpu // no coalescence - size_t i_samples = size(source); - size_t n_samples = 1001; - //double * times_fps; = times_farthest_point_sampling_ptp_gpu(mesh, source, n_samples); - - os.open(test_path + filename + ".fps"); -// for(index_t i = i_samples; i < n_samples; ++i) -// os << i << " " << times_fps[i] << std::endl; - os.close(); - -#endif // GPROSHAN_CUDA - - // FREE MEMORY - - delete mesh; - delete [] exact; - } - - fclose(ftable); -} - -double test_fast_marching(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const int n_test) -{ - double t, seconds = INFINITY; - - for(int i = 0; i < n_test; ++i) - { - TIC(t) geodesics fm(mesh, source); TOC(t); - seconds = std::min(seconds, t); - } - - geodesics fm(mesh, source); - - error = compute_error(fm, exact, mesh->n_vertices, size(source)); - - return seconds; -} - -double test_ptp_cpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test) -{ - double t, seconds = INFINITY; - - real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; ++i) - { - TIC(t) parallel_toplesets_propagation_cpu(dist, mesh, source, toplesets); TOC(t) - seconds = std::min(seconds, t); - } - - error = compute_error(dist, exact, mesh->n_vertices, size(source)); - - delete [] dist; - - return seconds; -} - -double test_heat_method_cholmod(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test) -{ - double t, st, ptime; - ptime = stime = INFINITY; - - real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; ++i) - { - TIC(t) st = heat_method(dist, mesh, source, HEAT_CHOLMOD); TOC(t) - ptime = std::min(t - st, ptime); - stime = std::min(st, stime); - } - - error = compute_error(dist, exact, mesh->n_vertices, size(source)); - - delete [] dist; - - return ptime; -} - - -#ifdef GPROSHAN_CUDA - -double test_ptp_gpu(real_t & error, const real_t * exact, che * mesh, const std::vector & source, const toplesets_t & toplesets, const int n_test) -{ - double t, seconds = INFINITY; - - real_t * dist = new real_t[mesh->n_vertices]; - for(int i = 0; i < n_test; ++i) - { - t = parallel_toplesets_propagation_gpu(dist, mesh, source, toplesets); - seconds = std::min(seconds, t); - } - - error = compute_error(dist, exact, mesh->n_vertices, size(source)); - - delete [] dist; - - return seconds; -} - -double test_heat_method_gpu(real_t & error, double & stime, const real_t * exact, che * mesh, const std::vector & source, const int n_test) -{ - double t, st, ptime; - ptime = stime = INFINITY; - - real_t * dist = nullptr; - for(int i = 0; i < n_test; ++i) - { - if(dist) delete [] dist; - - TIC(t) st = heat_method(dist, mesh, source, HEAT_CUDA); TOC(t) - ptime = std::min(t - st, ptime); - stime = std::min(st, stime); - } - - error = compute_error(dist, exact, mesh->n_vertices, size(source)); - - delete [] dist; - - return ptime; -} - -#endif // GPROSHAN_CUDA - - -real_t * load_exact_geodesics(const std::string & file, const size_t n) -{ - std::ifstream is(file); - - if(!is.good()) return nullptr; - - real_t * exact = new real_t[n]; - - for(index_t i = 0; i < n; ++i) - is >> exact[i]; - is.close(); - - return exact; -} - -real_t compute_error(const real_t * dist, const real_t * exact, const size_t n, const size_t s) -{ - real_t error = 0; - - #pragma omp parallel for reduction(+: error) - for(index_t v = 0; v < n; ++v) - if(exact[v] > 0) error += abs(dist[v] - exact[v]) / exact[v]; - - return error * 100 / (n - s); -} - -} // namespace gproshan - From fd85b5345f6f3172e7fd51b8ec2353aaf426fdd3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Mar 2024 13:06:46 +0100 Subject: [PATCH 0957/1018] update gproshan config file --- gproshanConfig.cmake.in | 2 -- 1 file changed, 2 deletions(-) diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index ae2a9f6b..11ff78c7 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -11,8 +11,6 @@ find_dependency(GLEW) find_dependency(glfw3) find_dependency(X11) find_dependency(Armadillo) -find_dependency(Eigen3) -find_dependency(CGAL) find_dependency(SuiteSparse) find_dependency(flann) From 65578f7631eeecef8e29fd40d428a78217d13ebf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Mar 2024 13:35:16 +0100 Subject: [PATCH 0958/1018] gproshan v4.0 --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7eb25c1b..4c9fffc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.28) -project(gproshan VERSION 3.14) +project(gproshan VERSION 4.0) list(APPEND CMAKE_MODULE_PATH "${gproshan_SOURCE_DIR}/cmake") From ee38444612cc1936dfd1826d098ca2985a020d10 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Mar 2024 23:36:52 +0100 Subject: [PATCH 0959/1018] laplacian: implemented as template functions --- include/gproshan/laplacian/laplacian.h | 73 ++++++++++++++++++- src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/laplacian/fairing_spectral.cpp | 8 +-- src/gproshan/laplacian/fairing_taubin.cpp | 6 +- src/gproshan/laplacian/laplacian.cpp | 78 --------------------- 5 files changed, 78 insertions(+), 89 deletions(-) delete mode 100644 src/gproshan/laplacian/laplacian.cpp diff --git a/include/gproshan/laplacian/laplacian.h b/include/gproshan/laplacian/laplacian.h index aac59444..67163501 100644 --- a/include/gproshan/laplacian/laplacian.h +++ b/include/gproshan/laplacian/laplacian.h @@ -2,16 +2,83 @@ #define LAPLACIAN_H #include -#include + +#include // geometry processing and shape analysis framework namespace gproshan { -void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A); +template +void laplacian(const che * mesh, arma::SpMat & L, arma::SpMat & A) +{ + size_t n_edges = mesh->n_edges; + size_t n_vertices = mesh->n_vertices; + + arma::umat DI(2, 2 * n_edges); + arma::Col DV(2 * n_edges); + + arma::umat SI(2, n_edges); + arma::Col SV(n_edges); + + #pragma omp parallel for + for(index_t e = 0; e < n_edges; ++e) + { + index_t i = e << 1; + + DI(0, i) = e; + DI(1, i) = mesh->edge_u(e); + DV(i) = -1; + + ++i; + + DI(0, i) = e; + DI(1, i) = mesh->edge_v(e); + DV(i) = 1; + + SI(0, e) = SI(1, e) = e; + SV(e) = (mesh->cotan(mesh->edge_he_0(e)) + mesh->cotan(mesh->edge_he_1(e))) / 2; + } + + arma::SpMat D(DI, DV, n_edges, n_vertices); + arma::SpMat S(SI, SV, n_edges, n_edges); + + L = D.t() * S * D; + + A.eye(n_vertices, n_vertices); + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + A(v, v) = mesh->area_vertex(v); +} + +template +size_t eigs_laplacian(const che * mesh, arma::Col & eigval, arma::Mat & eigvec, + arma::SpMat & L, arma::SpMat & A, const size_t k) +{ + laplacian(mesh, L, A); + + std::string feigval = tmp_file_path(mesh->name_size() + ".eigval"); + std::string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); + + if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) + { + if(!eigs_sym(eigval, eigvec, L, k, "sm")) + return 0; + + eigval.save(feigval); + eigvec.save(feigvec); + } + + if(k < eigval.n_elem) + { + eigval = eigval.head(k); + eigvec = eigvec.head_cols(k); + } -size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t k); + return eigval.n_elem; +} } // namespace gproshan diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 44a34d6c..3f9e09de 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -216,7 +216,7 @@ void geodesics::run_heat_method(che * mesh, const std::vector & sources { double time_total, solve_time; TIC(time_total) - solve_time = heat_method(dist, mesh, sources, HEAT_CHOLMOD); + solve_time = heat_method(dist, mesh, sources, HEAT_ARMA); TOC(time_total) gproshan_log_var(time_total - solve_time); diff --git a/src/gproshan/laplacian/fairing_spectral.cpp b/src/gproshan/laplacian/fairing_spectral.cpp index 2617d2bb..bc107475 100644 --- a/src/gproshan/laplacian/fairing_spectral.cpp +++ b/src/gproshan/laplacian/fairing_spectral.cpp @@ -15,11 +15,11 @@ void fairing_spectral::compute(che * mesh) vertices = new vertex[mesh->n_vertices]; memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); - a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); + arma::fmat X((real_t *) vertices, 3, mesh->n_vertices, false, true); - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; + arma::sp_fmat L, A; + arma::fvec eigval; + arma::fmat eigvec; n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); if(!n_eigs) return; diff --git a/src/gproshan/laplacian/fairing_taubin.cpp b/src/gproshan/laplacian/fairing_taubin.cpp index 64b40810..5dffbaa3 100644 --- a/src/gproshan/laplacian/fairing_taubin.cpp +++ b/src/gproshan/laplacian/fairing_taubin.cpp @@ -15,12 +15,12 @@ void fairing_taubin::compute(che * mesh) vertices = new vertex[mesh->n_vertices]; memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); - a_mat X((real_t *) vertices, 3, mesh->n_vertices, false, true); + arma::fmat X((real_t *) vertices, 3, mesh->n_vertices, false, true); - a_sp_mat L, A; + arma::sp_fmat L, A; laplacian(mesh, L, A); - a_mat R = spsolve(A + step * L, A * X.t()); + arma::fmat R = spsolve(A + step * L, A * X.t()); X = R.t(); } diff --git a/src/gproshan/laplacian/laplacian.cpp b/src/gproshan/laplacian/laplacian.cpp deleted file mode 100644 index 6df5e018..00000000 --- a/src/gproshan/laplacian/laplacian.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -void laplacian(const che * mesh, a_sp_mat & L, a_sp_mat & A) -{ - size_t n_edges = mesh->n_edges; - size_t n_vertices = mesh->n_vertices; - - arma::umat DI(2, 2 * n_edges); - a_vec DV(2 * n_edges); - - arma::umat SI(2, n_edges); - a_vec SV(n_edges); - - #pragma omp parallel for - for(index_t e = 0; e < n_edges; ++e) - { - index_t i = e << 1; - - DI(0, i) = e; - DI(1, i) = mesh->edge_u(e); - DV(i) = -1; - - ++i; - - DI(0, i) = e; - DI(1, i) = mesh->edge_v(e); - DV(i) = 1; - - SI(0, e) = SI(1, e) = e; - SV(e) = (mesh->cotan(mesh->edge_he_0(e)) + - mesh->cotan(mesh->edge_he_1(e))) / 2; - } - - a_sp_mat D(DI, DV, n_edges, n_vertices); - a_sp_mat S(SI, SV, n_edges, n_edges); - - L = D.t() * S * D; - - A.eye(n_vertices, n_vertices); - - #pragma omp parallel for - for(index_t v = 0; v < n_vertices; ++v) - A(v, v) = mesh->area_vertex(v); -} - -size_t eigs_laplacian(const che * mesh, a_vec & eigval, a_mat & eigvec, a_sp_mat & L, a_sp_mat & A, const size_t k) -{ - laplacian(mesh, L, A); - - std::string feigval = tmp_file_path(mesh->name_size() + ".eigval"); - std::string feigvec = tmp_file_path(mesh->name_size() + ".eigvec"); - - if(!eigval.load(feigval) || !eigvec.load(feigvec) || eigval.n_elem < k) - { - if(!eigs_sym(eigval, eigvec, L, k, "sm")) - return 0; - - eigval.save(feigval); - eigvec.save(feigvec); - } - - if(k < eigval.n_elem) - { - eigval = eigval.head(k); - eigvec = eigvec.head_cols(k); - } - - return eigval.n_elem; -} - - -} // namespace gproshan - From bee62d3386a56da8630045ec1caf4004e73db4cf Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Mar 2024 00:13:44 +0100 Subject: [PATCH 0960/1018] geodesics: heat method fix float/double mix, it requires double precision --- include/gproshan/geodesics/heat_method.h | 14 +++--- src/gproshan/geodesics/heat_method.cpp | 61 ++++++++++++++---------- src/gproshan/geodesics/heat_method.cu | 35 +++++--------- 3 files changed, 56 insertions(+), 54 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 350c8c21..3f129ee3 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -11,8 +11,8 @@ #define HEAT_METHOD_H #include -#include +#include #include @@ -29,24 +29,24 @@ enum heat_method_opt { double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); -void compute_divergence(const che * mesh, const a_mat & u, a_mat & div); +arma::vec compute_divergence(const che * mesh, const arma::mat & u); /// cholmod Keenan implementation /// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics -double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, cholmod_common * context); +double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context); -cholmod_dense * arma_2_cholmod(const a_mat & m, cholmod_common * context); +cholmod_dense * arma_2_cholmod(const arma::mat & m, cholmod_common * context); -cholmod_sparse * arma_2_cholmod(const a_sp_mat & m, cholmod_common * context); +cholmod_sparse * arma_2_cholmod(const arma::sp_mat & m, cholmod_common * context); #ifdef GPROSHAN_CUDA /// -double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b); +double solve_positive_definite_gpu(arma::mat & x, const arma::sp_mat & A, const arma::mat & b); /// host and device support /// https://docs.nvidia.com/cuda/cusolver/index.html#cusolver-lt-t-gt-csrlsvchol -double solve_positive_definite_cusolver(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host = 0); +double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx, const bool host = 0); #endif // GPROSHAN_CUDA diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index dbcbf27a..11a6e7d9 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -15,14 +15,14 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & if(!size(sources)) return 0; // build impulse signal - a_vec u0(mesh->n_vertices, arma::fill::zeros); + arma::vec u0(mesh->n_vertices, arma::fill::zeros); for(auto & v: sources) u0(v) = 1; // step real_t dt = mesh->mean_edge(); dt *= dt; - a_sp_mat L, A; + arma::sp_mat L, A; laplacian(mesh, L, A); // make L positive-definite @@ -30,19 +30,18 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & // heat flow for short interval A += dt * L; - a_vec u(mesh->n_vertices); - - cholmod_common context; - cholmod_l_start(&context); + arma::vec u(mesh->n_vertices); double solve_time = 0; + cholmod_common context; switch(opt) { case HEAT_ARMA: - if(!spsolve(u, A, u0)) gproshan_error(arma: no solution); + if(!spsolve(u, A, u0)) gproshan_error(arma no solution); break; case HEAT_CHOLMOD: + cholmod_l_start(&context); solve_time += solve_positive_definite(u, A, u0, &context); break; #ifdef GPROSHAN_CUDA @@ -54,10 +53,8 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & // extract geodesics - a_vec div(mesh->n_vertices); - compute_divergence(mesh, u, div); - - a_vec phi(dist, mesh->n_vertices, false); + arma::vec div = compute_divergence(mesh, u); + arma::vec phi(mesh->n_vertices); switch(opt) { @@ -66,6 +63,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & break; case HEAT_CHOLMOD: solve_time += solve_positive_definite(phi, L, div, &context); + cholmod_l_finish(&context); break; #ifdef GPROSHAN_CUDA case HEAT_CUDA: @@ -74,33 +72,48 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & #endif // GPROSHAN_CUDA } - real_t min_val = phi.min(); - phi -= min_val; - phi *= 0.5; + if(phi.size() == mesh->n_vertices) + { + phi -= phi.min(); + phi *= 0.5; - cholmod_l_finish(&context); + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + dist[v] = phi(v); + } return solve_time; } -void compute_divergence(const che * mesh, const a_mat & u, a_mat & div) +arma::vec compute_divergence(const che * mesh, const arma::mat & u) { + arma::vec div(mesh->n_vertices); + + std::vector f(mesh->n_vertices); + + #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - { - real_t & sum = div(v); + f[v] = u[v]; - sum = 0; + #pragma omp parallel for + for(index_t v = 0; v < mesh->n_vertices; ++v) + { + real_t sum = 0; for(const index_t he: mesh->star(v)) { const vertex & nhe = mesh->normal_he(he); const vertex & vhe = mesh->vertex_he(he_prev(he)) - mesh->vertex_he(he_next(he)); - const vertex & ghe = mesh->gradient_he(he, u.memptr()); + const vertex & ghe = mesh->gradient_he(he, f.data()); sum += dot(cross(nhe, vhe), -ghe); } + + div(v) = sum; } + + return div; } -double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, cholmod_common * context) +double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context) { cholmod_sparse * cA = arma_2_cholmod(A, context); cA->stype = 1; @@ -131,7 +144,7 @@ double solve_positive_definite(a_mat & x, const a_sp_mat & A, const a_mat & b, c return solve_time; } -cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) +cholmod_dense * arma_2_cholmod(const arma::mat & D, cholmod_common * context) { cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); copy_real_t_array((double *) cD->x, D.memptr(), D.n_elem); @@ -139,7 +152,7 @@ cholmod_dense * arma_2_cholmod(const a_mat & D, cholmod_common * context) return cD; } -cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) +cholmod_sparse * arma_2_cholmod(const arma::sp_mat & S, cholmod_common * context) { assert(sizeof(arma::uword) == sizeof(SuiteSparse_long)); @@ -155,7 +168,7 @@ cholmod_sparse * arma_2_cholmod(const a_sp_mat & S, cholmod_common * context) #ifdef GPROSHAN_CUDA -double solve_positive_definite_gpu(a_mat & x, const a_sp_mat & A, const a_mat & b) +double solve_positive_definite_gpu(arma::mat & x, const arma::sp_mat & A, const arma::mat & b) { int * hA_col_ptrs = new int[A.n_cols + 1]; int * hA_row_indices = new int[A.n_nonzero]; diff --git a/src/gproshan/geodesics/heat_method.cu b/src/gproshan/geodesics/heat_method.cu index 0e499523..44b22e97 100644 --- a/src/gproshan/geodesics/heat_method.cu +++ b/src/gproshan/geodesics/heat_method.cu @@ -1,5 +1,3 @@ -#include - #include #include @@ -12,9 +10,9 @@ namespace gproshan { struct cu_spAxb { int * A_col_ptrs, * A_row_indices; - real_t * A_values, * x, * b; + double * A_values, * x, * b; - cu_spAxb(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb) + cu_spAxb(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb) { cudaMalloc(&A_col_ptrs, (m + 1) * sizeof(int)); cudaMemcpy(A_col_ptrs, hA_col_ptrs, (m + 1) * sizeof(int), cudaMemcpyHostToDevice); @@ -22,13 +20,13 @@ struct cu_spAxb cudaMalloc(&A_row_indices, nnz * sizeof(int)); cudaMemcpy(A_row_indices, hA_row_indices, nnz * sizeof(int), cudaMemcpyHostToDevice); - cudaMalloc(&A_values, nnz * sizeof(real_t)); - cudaMemcpy(A_values, hA_values, nnz * sizeof(real_t), cudaMemcpyHostToDevice); + cudaMalloc(&A_values, nnz * sizeof(double)); + cudaMemcpy(A_values, hA_values, nnz * sizeof(double), cudaMemcpyHostToDevice); - cudaMalloc(&b, nnz * sizeof(real_t)); - cudaMemcpy(b, hb, nnz * sizeof(real_t), cudaMemcpyHostToDevice); + cudaMalloc(&b, nnz * sizeof(double)); + cudaMemcpy(b, hb, nnz * sizeof(double), cudaMemcpyHostToDevice); - cudaMalloc(&x, m * sizeof(real_t)); + cudaMalloc(&x, m * sizeof(double)); } ~cu_spAxb() @@ -41,7 +39,7 @@ struct cu_spAxb } }; -double solve_positive_definite_cusolver(const int m, const int nnz, const real_t * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const real_t * hb, real_t * hx, const bool host) +double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx, const bool host) { cudaDeviceReset(); @@ -65,28 +63,19 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const real_t if(host) { - #ifdef GPROSHAN_FLOAT - cusolverSpScsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); - #else - cusolverSpDcsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); - #endif + cusolverSpDcsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); } else { // allocate A, x, b into device cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); - cusolverStatus_t status; - #ifdef GPROSHAN_FLOAT - status = cusolverSpScsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); - #else - status = cusolverSpDcsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); - #endif + cusolverStatus_t status = cusolverSpDcsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); if(status == CUSOLVER_STATUS_SUCCESS) - cudaMemcpy(hx, data.x, m * sizeof(real_t), cudaMemcpyDeviceToHost); + cudaMemcpy(hx, data.x, m * sizeof(double), cudaMemcpyDeviceToHost); else - memset(hx, 0, m * sizeof(real_t)); + memset(hx, 0, m * sizeof(double)); } // printf("%d\n", singularity != -1); From 5c4a8fe70dac9af31bf939e147bbdd26d081adf8 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Mar 2024 00:41:48 +0100 Subject: [PATCH 0961/1018] geodesics: debugging heat method --- include/gproshan/geodesics/heat_method.h | 2 +- src/gproshan/geodesics/heat_method.cpp | 11 +++++++++-- src/gproshan/mesh/che.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 3f129ee3..cd8468f0 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -29,7 +29,7 @@ enum heat_method_opt { double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); -arma::vec compute_divergence(const che * mesh, const arma::mat & u); +arma::vec compute_divergence(const che * mesh, const arma::vec & u); /// cholmod Keenan implementation /// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 11a6e7d9..6468c0e9 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -30,6 +30,9 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & // heat flow for short interval A += dt * L; + + gproshan_error_var(A); + arma::vec u(mesh->n_vertices); double solve_time = 0; @@ -51,11 +54,14 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & #endif // GPROSHAN_CUDA } + gproshan_error_var(u); + // extract geodesics arma::vec div = compute_divergence(mesh, u); arma::vec phi(mesh->n_vertices); + gproshan_error_var(div); switch(opt) { case HEAT_ARMA: @@ -72,6 +78,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & #endif // GPROSHAN_CUDA } + gproshan_error_var(phi); if(phi.size() == mesh->n_vertices) { phi -= phi.min(); @@ -85,7 +92,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & return solve_time; } -arma::vec compute_divergence(const che * mesh, const arma::mat & u) +arma::vec compute_divergence(const che * mesh, const arma::vec & u) { arma::vec div(mesh->n_vertices); @@ -93,7 +100,7 @@ arma::vec compute_divergence(const che * mesh, const arma::mat & u) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - f[v] = u[v]; + f[v] = u(v); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index bf4ca766..6676ee0d 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -133,11 +133,11 @@ vertex che::gradient_he(const index_t he, const real_t * f) const index_t j = VT[he_next(he)]; index_t k = VT[he_prev(he)]; - vertex xi = GT[i]; - vertex xj = GT[j]; - vertex xk = GT[k]; + const vertex & xi = GT[i]; + const vertex & xj = GT[j]; + const vertex & xk = GT[k]; - vertex n = normal_he(he); + const vertex & n = normal_he(he); vertex pij = cross(n, xj - xi); vertex pjk = cross(n, xk - xj); From c221bc3c1c2c83a1cc5feee018d829509f793aa0 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 20 Mar 2024 15:24:18 +0100 Subject: [PATCH 0962/1018] geodesics: fix #8 heat method, requires double precision minor che fix no edges on border --- include/gproshan/geometry/vec.h | 10 +++++-- include/gproshan/mesh/che.h | 1 + src/gproshan/geodesics/geodesics.cpp | 2 +- src/gproshan/geodesics/heat_method.cpp | 30 ++++++++------------ src/gproshan/mesh/che.cpp | 38 ++++++++++++++++++++------ 5 files changed, 51 insertions(+), 30 deletions(-) diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 60d63d7a..7564fc30 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -350,9 +350,13 @@ std::istream & operator >> (std::istream & is, vec & v) } -using vec2 = vec; -using vec3 = vec; -using vec4 = vec; +using vec2 = vec; +using vec3 = vec; +using vec4 = vec; + +using dvec2 = vec; +using dvec3 = vec; +using dvec4 = vec; using uvec2 = vec; using uvec3 = vec; diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 26934000..8f7a1acc 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -83,6 +83,7 @@ class che vertex normal_trig(const index_t f) const; vertex normal_he(const index_t he) const; vertex gradient_he(const index_t he, const real_t * f) const; + dvec3 gradient_he(const index_t he, const double * f) const; vertex gradient(const index_t v, const real_t * f); // vertex color methods diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 3f9e09de..44a34d6c 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -216,7 +216,7 @@ void geodesics::run_heat_method(che * mesh, const std::vector & sources { double time_total, solve_time; TIC(time_total) - solve_time = heat_method(dist, mesh, sources, HEAT_ARMA); + solve_time = heat_method(dist, mesh, sources, HEAT_CHOLMOD); TOC(time_total) gproshan_log_var(time_total - solve_time); diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 6468c0e9..9742591b 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -26,16 +26,14 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & laplacian(mesh, L, A); // make L positive-definite - L += 1.0e-8 * A; + L += 1e-8 * A; // heat flow for short interval A += dt * L; - gproshan_error_var(A); - - arma::vec u(mesh->n_vertices); double solve_time = 0; + arma::vec u(mesh->n_vertices); cholmod_common context; switch(opt) @@ -54,18 +52,16 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & #endif // GPROSHAN_CUDA } - gproshan_error_var(u); // extract geodesics arma::vec div = compute_divergence(mesh, u); arma::vec phi(mesh->n_vertices); - gproshan_error_var(div); switch(opt) { case HEAT_ARMA: - if(!spsolve(phi, L, div)) gproshan_error(arma: no solution); + if(!spsolve(phi, L, div)) gproshan_error(arma no solution); break; case HEAT_CHOLMOD: solve_time += solve_positive_definite(phi, L, div, &context); @@ -78,7 +74,6 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & #endif // GPROSHAN_CUDA } - gproshan_error_var(phi); if(phi.size() == mesh->n_vertices) { phi -= phi.min(); @@ -98,19 +93,18 @@ arma::vec compute_divergence(const che * mesh, const arma::vec & u) std::vector f(mesh->n_vertices); - #pragma omp parallel for - for(index_t v = 0; v < mesh->n_vertices; ++v) - f[v] = u(v); - #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) { real_t sum = 0; for(const index_t he: mesh->star(v)) { - const vertex & nhe = mesh->normal_he(he); - const vertex & vhe = mesh->vertex_he(he_prev(he)) - mesh->vertex_he(he_next(he)); - const vertex & ghe = mesh->gradient_he(he, f.data()); + const vertex & n = mesh->normal_he(he); + const vertex & v = mesh->vertex_he(he_prev(he)) - mesh->vertex_he(he_next(he)); + + const dvec3 nhe = {n.x(), n.y(), n.z()}; + const dvec3 vhe = {v.x(), v.y(), v.z()}; + const dvec3 ghe = mesh->gradient_he(he, u.memptr()); sum += dot(cross(nhe, vhe), -ghe); } @@ -142,7 +136,7 @@ double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma TOC(solve_time) assert(x.n_rows == b.n_rows); - copy_real_t_array(x.memptr(), (double *) cx->x, x.n_rows); + memcpy(x.memptr(), cx->x, x.n_rows * sizeof(double)); cholmod_l_free_factor(&L, context); cholmod_l_free_sparse(&cA, context); @@ -154,7 +148,7 @@ double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma cholmod_dense * arma_2_cholmod(const arma::mat & D, cholmod_common * context) { cholmod_dense * cD = cholmod_l_allocate_dense(D.n_rows, D.n_cols, D.n_rows, CHOLMOD_REAL, context); - copy_real_t_array((double *) cD->x, D.memptr(), D.n_elem); + memcpy(cD->x, D.memptr(), D.n_elem * sizeof(double)); return cD; } @@ -165,9 +159,9 @@ cholmod_sparse * arma_2_cholmod(const arma::sp_mat & S, cholmod_common * context cholmod_sparse * cS = cholmod_l_allocate_sparse(S.n_rows, S.n_cols, S.n_nonzero, 1, 1, 0, CHOLMOD_REAL, context); - copy_real_t_array((double *) cS->x, S.values, S.n_nonzero); memcpy(cS->p, S.col_ptrs, (S.n_cols + 1) * sizeof(arma::uword)); memcpy(cS->i, S.row_indices, S.n_nonzero * sizeof(arma::uword)); + memcpy(cS->x, S.values, S.n_nonzero * sizeof(double)); return cS; } diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 6676ee0d..15494b3e 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -146,6 +146,30 @@ vertex che::gradient_he(const index_t he, const real_t * f) const return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } +dvec3 che::gradient_he(const index_t he, const double * f) const +{ + index_t i = VT[he]; + index_t j = VT[he_next(he)]; + index_t k = VT[he_prev(he)]; + + const vertex & gi = GT[i]; + const vertex & gj = GT[j]; + const vertex & gk = GT[k]; + + const dvec3 xi = {gi.x(), gi.y(), gi.z()}; + const dvec3 xj = {gj.x(), gj.y(), gj.z()}; + const dvec3 xk = {gk.x(), gk.y(), gk.z()}; + + const vertex & n = normal_he(he); + const dvec3 dn = {n.x(), n.y(), n.z()}; + + dvec3 pij = cross(dn, xj - xi); + dvec3 pjk = cross(dn, xk - xj); + dvec3 pki = cross(dn, xi - xk); + + return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); +} + vertex che::gradient(const index_t v, const real_t * f) { vertex g; @@ -1078,7 +1102,7 @@ void che::update_evt_ot_et() vhe[VT[he]][vnhe[VT[he]]++] = he; size_t ne = 0; - for(index_t ohe, he = 0; he < n_half_edges; ++he) + for(index_t he = 0; he < n_half_edges; ++he) { const index_t u = VT[he]; const index_t v = VT[he_next(he)]; @@ -1087,20 +1111,18 @@ void che::update_evt_ot_et() if(OT[he] == NIL) { - ohe = NIL; + ET[ne++] = he; + + index_t ohe = NIL; for(index_t j = 0; j < vnhe[v]; ++j) - { - index_t & h = vhe[v][j]; - if(VT[he_next(h)] == u) + if(VT[he_next(vhe[v][j])] == u) { - ohe = h; + ohe = vhe[v][j]; break; } - } if(ohe != NIL && OT[ohe] == NIL) { - ET[ne++] = he; OT[he] = ohe; OT[ohe] = he; } From 75107e3672c63b4a66ef76f7f60d82539ef98ec9 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 01:47:43 +0100 Subject: [PATCH 0963/1018] che: adding che_cuda --- include/gproshan/geodesics/geodesics_ptp.h | 10 +- include/gproshan/mesh/che.cuh | 4 +- include/gproshan/mesh/che.h | 24 ++-- include/gproshan/mesh/che_cuda.h | 26 +++++ include/gproshan/raytracing/embree.h | 2 +- include/gproshan/raytracing/optix.h | 4 +- include/gproshan/raytracing/utils.h | 4 +- src/gproshan/geodesics/geodesics.cpp | 4 +- src/gproshan/geodesics/geodesics_ptp.cpp | 34 +----- src/gproshan/geodesics/geodesics_ptp.cu | 47 ++------ src/gproshan/mesh/che.cpp | 124 +++++++++++++++++---- src/gproshan/mesh/che.cu | 18 +-- src/gproshan/mesh/che_cuda.cpp | 76 +++++++++++++ src/gproshan/raytracing/embree.cpp | 17 ++- src/gproshan/raytracing/optix.cpp | 10 +- src/gproshan/raytracing/optix.cu | 2 +- 16 files changed, 271 insertions(+), 135 deletions(-) create mode 100644 include/gproshan/mesh/che_cuda.h create mode 100644 src/gproshan/mesh/che_cuda.cpp diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 56adcbdf..8e9d95f6 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -30,7 +30,7 @@ namespace gproshan { #ifdef __CUDACC__ __global__ -void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); +void relax_ptp(const che * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); __global__ void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); @@ -97,7 +97,7 @@ template __forceinline__ #endif __host_device__ -real_t update_step(const CHE * mesh, const T * dist, const uvec3 & x) +real_t update_step(const che * mesh, const T * dist, const uvec3 & x) { const vec X[2] = {mesh->GT[x[0]] - mesh->GT[x[2]], mesh->GT[x[1]] - mesh->GT[x[2]] @@ -153,7 +153,7 @@ template __forceinline__ #endif __host_device__ -void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) +void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { real_t & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; @@ -177,11 +177,11 @@ void relax_ptp(const CHE * mesh, T * new_dist, T * old_dist, index_t * new_clust template #ifdef __CUDACC__ -index_t run_ptp(const CHE * mesh, const std::vector & sources, +index_t run_ptp(const che * mesh, const std::vector & sources, const std::vector & limits, T * error, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) #else -index_t run_ptp(const CHE * mesh, const std::vector & sources, +index_t run_ptp(const che * mesh, const std::vector & sources, const std::vector & limits, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) #endif diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh index 2d724cff..a4e90501 100644 --- a/include/gproshan/mesh/che.cuh +++ b/include/gproshan/mesh/che.cuh @@ -8,9 +8,9 @@ namespace gproshan { -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool normal = false, const bool color = false); +void cuda_create_CHE(const che * h_che, che *& dd_che, che *& d_che, const bool normal = false, const bool color = false); -void cuda_free_CHE(CHE *& dd_che, CHE *& d_che); +void cuda_free_CHE(che *& dd_che, che *& d_che); } // namespace gproshan diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 8f7a1acc..c3ef369e 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -32,6 +32,14 @@ class che public: enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types + struct options + { + bool edges = true; + bool colors = true; + bool normals = true; + }; + + static const che::options default_opts; struct rgb_t { @@ -46,14 +54,14 @@ class che operator vertex () const; }; - const size_t n_vertices = 0; - const size_t n_trigs = 0; - const size_t n_half_edges = 0; - const size_t n_edges = 0; +size_t n_vertices = 0; +size_t n_trigs = 0; +size_t n_half_edges = 0; +size_t n_edges = 0; std::string filename; ///< get and set data member - protected: + public: vertex * GT = nullptr; ///< geometry table : v -> vertex index_t * VT = nullptr; ///< vertex table (trigs) : he -> v index_t * OT = nullptr; ///< opposite table : he -> he @@ -69,7 +77,7 @@ class che bool manifold = true; public: - che(const che & mesh); + che(const che & mesh, const index_t * sorted = nullptr, const che::options & opts = default_opts); che(const size_t n_v = 0, const size_t n_f = 0); che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); virtual ~che(); @@ -165,8 +173,8 @@ class che protected: void init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); void init(const std::string & file); - void alloc(const size_t n_v, const size_t n_f); - void free(); + bool alloc(const size_t n_v, const size_t n_f, const che::options & opts = default_opts); + virtual void free(); virtual void read_file(const std::string & file); diff --git a/include/gproshan/mesh/che_cuda.h b/include/gproshan/mesh/che_cuda.h new file mode 100644 index 00000000..780ae851 --- /dev/null +++ b/include/gproshan/mesh/che_cuda.h @@ -0,0 +1,26 @@ +#ifndef CHE_CUDA_H +#define CHE_CUDA_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class che_cuda : public che +{ + che * device = nullptr; + + public: + che_cuda(const che * mesh = nullptr, const che::options & opts = che::default_opts); + ~che_cuda(); + + operator const che * () const; +}; + + +} // namespace gproshan + +#endif // CHE_CUDA_H + diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 0b56184c..ae9cf168 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -56,7 +56,7 @@ class embree : public raytracing RTCDevice rtc_device; RTCScene rtc_scene; - std::vector g_meshes; + std::vector g_meshes; scene_data sc; public: diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index d8d88b67..741dad28 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -43,8 +43,8 @@ class optix : public raytracing launch_params optix_params; launch_params * optix_params_buffer = nullptr; - std::vector dd_mesh; - std::vector d_mesh; + std::vector dd_mesh; + std::vector d_mesh; void * raygen_records_buffer = nullptr; void * miss_records_buffer = nullptr; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 86dc9fb6..a272a25b 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -91,7 +91,7 @@ struct t_eval_hit t_eval_hit() {} __host_device__ - t_eval_hit(const CHE & mesh, const index_t aprimID, const T & au, const T & av, const scene_data & sc) + t_eval_hit(const che & mesh, const index_t aprimID, const T & au, const T & av, const scene_data & sc) { primID = aprimID; u = au; @@ -268,7 +268,7 @@ vec ray_view_dir( const uvec2 & pos, template __host_device__ -index_t closest_hit_vertex(const CHE & mesh, const H & hit) +index_t closest_hit_vertex(const che & mesh, const H & hit) { if(!mesh.n_trigs) return hit.primID; diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 44a34d6c..6e98aa4a 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -116,8 +116,6 @@ void geodesics::execute(che * mesh, const std::vector & sources, const void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const real_t radio, const fm_function_t & fun) { - CHE cmesh(mesh); - index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -173,7 +171,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dv = dist[v]; for(const index_t he: mesh->star(v)) { - dp = update_step(&cmesh, dist, {mesh->halfedge(he_next(he)), + dp = update_step(mesh, dist, { mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he)), mesh->halfedge(he) }); diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 60f13e8a..1b1ed965 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -21,37 +21,21 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, const f_ptp & fun ) { - CHE h_mesh(mesh); - const size_t n_vertices = h_mesh.n_vertices; + const size_t n_vertices = mesh->n_vertices; + che * h_mesh = nullptr; index_t * inv = nullptr; if(coalescence) { + h_mesh = new che(*mesh, toplesets.index, {false, false, false}); inv = new index_t[n_vertices]; - h_mesh.GT = new vertex[n_vertices]; - h_mesh.EVT = new index_t[n_vertices]; - h_mesh.VT = new index_t[h_mesh.n_half_edges]; #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); ++i) - { - h_mesh.GT[i] = mesh->point(toplesets.index[i]); inv[toplesets.index[i]] = i; - } - - #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; ++he) - { - const index_t v = mesh->halfedge(he); - if(v != NIL) - { - h_mesh.VT[he] = inv[v]; - if(mesh->evt(v) == he) - h_mesh.EVT[inv[v]] = he; - } - } } + real_t * dist[2] = { coalescence ? new real_t[n_vertices] : ptp_out.dist, new real_t[n_vertices] }; @@ -66,7 +50,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, dist[0][v] = dist[1][v] = INFINITY; } - const index_t i = run_ptp( &h_mesh, sources, toplesets.limits, dist, clusters, + const index_t i = run_ptp( h_mesh ? h_mesh : mesh, sources, toplesets.limits, dist, clusters, coalescence ? inv : toplesets.index, coalescence ? nullptr : (index_t *) toplesets.index, fun); @@ -87,14 +71,8 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, delete [] dist[1]; delete [] clusters[1]; - if(coalescence) - { - delete [] h_mesh.GT; - delete [] h_mesh.VT; - delete [] h_mesh.EVT; - } - delete [] inv; + delete h_mesh; } void normalize_ptp(real_t * dist, const size_t n) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index b000bac2..b04b4d9b 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -21,37 +21,21 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, const f_ptp & fun ) { - CHE h_mesh(mesh); - const size_t n_vertices = h_mesh.n_vertices; + const size_t n_vertices = mesh->n_vertices; + che * h_mesh = nullptr; index_t * inv = nullptr; if(coalescence) { + h_mesh = new che(*mesh, toplesets.index, {false, false, false}); inv = new index_t[n_vertices]; - h_mesh.GT = new vertex[n_vertices]; - h_mesh.EVT = new index_t[n_vertices]; - h_mesh.VT = new index_t[h_mesh.n_half_edges]; #pragma omp parallel for for(index_t i = 0; i < toplesets.limits.back(); ++i) - { - h_mesh.GT[i] = mesh->point(toplesets.index[i]); inv[toplesets.index[i]] = i; - } - - #pragma omp parallel for - for(index_t he = 0; he < mesh->n_half_edges; ++he) - { - const index_t v = mesh->halfedge(he); - if(v != NIL) - { - h_mesh.VT[he] = inv[v]; - if(mesh->evt(v) == he) - h_mesh.EVT[inv[v]] = he; - } - } } + cudaDeviceReset(); cudaEvent_t start, stop; @@ -59,8 +43,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, cudaEventCreate(&stop); cudaEventRecord(start, 0); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); + che_cuda d_mesh(h_mesh ? h_mesh : mesh, {false, false, false}); real_t * h_dist = coalescence ? new real_t[n_vertices] : ptp_out.dist; index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[n_vertices] @@ -130,16 +113,9 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, cudaFree(d_clusters[0]); cudaFree(d_clusters[1]); cudaFree(d_sorted); - cuda_free_CHE(dd_mesh, d_mesh); - - if(coalescence) - { - delete [] h_mesh.GT; - delete [] h_mesh.VT; - delete [] h_mesh.EVT; - } delete [] inv; + delete h_mesh; cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -155,8 +131,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) { - CHE h_mesh(mesh); - const size_t n_vertices = h_mesh.n_vertices; + const size_t n_vertices = mesh->n_vertices; cudaDeviceReset(); @@ -165,8 +140,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample cudaEventCreate(&stop); cudaEventRecord(start, 0); - CHE * dd_mesh, * d_mesh; - cuda_create_CHE(&h_mesh, dd_mesh, d_mesh); + che_cuda d_mesh(mesh, {false, false, false}); real_t * h_dist = new real_t[n_vertices]; @@ -229,7 +203,6 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample cudaFree(d_dist[0]); cudaFree(d_dist[1]); cudaFree(d_sorted); - cuda_free_CHE(dd_mesh, d_mesh); cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -245,7 +218,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample } __global__ -void relax_ptp(const CHE * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) +void relax_ptp(const che * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 15494b3e..c4b652db 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -26,6 +26,7 @@ size_t & che::rw(const size_t & n) return const_cast(n); } +const che::options che::default_opts; che::rgb_t::rgb_t(const vertex & v) { @@ -47,22 +48,83 @@ che::rgb_t::operator vertex () const } -che::che(const che & mesh) +che::che(const che & mesh, const index_t * sorted, const che::options & opts) { filename = mesh.filename; - alloc(mesh.n_vertices, mesh.n_trigs); + if(!alloc(mesh.n_vertices, mesh.n_trigs, opts)) + return; + rw(n_edges) = mesh.n_edges; - memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); - memcpy(VT, mesh.VT, n_half_edges * sizeof(index_t)); + if(!sorted) + { + memcpy(GT, mesh.GT, n_vertices * sizeof(vertex)); + memcpy(EVT, mesh.EVT, n_vertices * sizeof(index_t)); + memcpy(VT, mesh.VT, n_half_edges * sizeof(index_t)); + } + else + { + index_t * inv = new index_t[n_vertices]; + + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + { + GT[v] = mesh.GT[sorted[v]]; + inv[sorted[v]] = v; + } + + #pragma omp parallel for + for(index_t he = 0; he < n_half_edges; ++he) + { + const index_t v = mesh.VT[he]; + VT[he] = inv[v]; + if(mesh.EVT[v] == he) + EVT[inv[v]] = he; + } + + delete [] inv; + } + memcpy(OT, mesh.OT, n_half_edges * sizeof(index_t)); - memcpy(EVT, mesh.EVT, n_vertices * sizeof(index_t)); - memcpy(ET, mesh.ET, n_edges * sizeof(index_t)); - memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); - memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); - memcpy(VC, mesh.VC, n_vertices * sizeof(rgb_t)); - memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); + + if(opts.edges) + { + memcpy(ET, mesh.ET, n_edges * sizeof(index_t)); + memcpy(EHT, mesh.EHT, n_half_edges * sizeof(index_t)); + } + + if(opts.normals) + { + if(!sorted) + { + memcpy(VN, mesh.VN, n_vertices * sizeof(vertex)); + } + else + { + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + VN[v] = mesh.VN[sorted[v]]; + } + } + + if(opts.colors) + { + if(sorted) + { + memcpy(VC, mesh.VC, n_vertices * sizeof(rgb_t)); + memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); + } + else + { + #pragma omp parallel for + for(index_t v = 0; v < n_vertices; ++v) + { + VC[v] = mesh.VC[sorted[v]]; + VHC[v] = mesh.VHC[sorted[v]]; + } + } + } } che::che(const size_t n_v, const size_t n_f) @@ -1043,33 +1105,51 @@ void che::init(const std::string & file) update_eht(); } -void che::alloc(const size_t n_v, const size_t n_f) +bool che::alloc(const size_t n_v, const size_t n_f, const che::options & opt) { + if(!n_v) return false; + rw(n_vertices) = n_v; rw(n_trigs) = n_f; rw(n_half_edges) = che::mtrig * n_trigs; rw(n_edges) = n_half_edges; // max number of edges - if(n_vertices) GT = new vertex[n_vertices]; - if(n_half_edges) VT = new index_t[n_half_edges]; - if(n_half_edges) OT = new index_t[n_half_edges]; - if(n_vertices) EVT = new index_t[n_vertices]; - if(n_half_edges) ET = new index_t[n_half_edges]; - if(n_half_edges) EHT = new index_t[n_half_edges]; + GT = new vertex[n_vertices]; + EVT = new index_t[n_vertices]; - if(n_vertices) VN = new vertex[n_vertices]; - if(n_vertices) VC = new rgb_t[n_vertices]; - if(n_vertices) VHC = new real_t[n_vertices]; + if(n_half_edges) + { + VT = new index_t[n_half_edges]; + OT = new index_t[n_half_edges]; + + if(opt.edges) + { + ET = new index_t[n_half_edges]; + EHT = new index_t[n_half_edges]; + } + } + + if(opt.normals) + VN = new vertex[n_vertices]; - update_heatmap(); + if(opt.colors) + { + VC = new rgb_t[n_vertices]; + VHC = new real_t[n_vertices]; + update_heatmap(); + } + + return true; } void che::free() { + gproshan_error_var("che::free: " + filename); + delete [] GT; GT = nullptr; + delete [] EVT; EVT = nullptr; delete [] VT; VT = nullptr; delete [] OT; OT = nullptr; - delete [] EVT; EVT = nullptr; delete [] ET; ET = nullptr; delete [] EHT; EHT = nullptr; delete [] VN; VN = nullptr; diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu index 812af1e4..b6c6d20c 100644 --- a/src/gproshan/mesh/che.cu +++ b/src/gproshan/mesh/che.cu @@ -5,9 +5,9 @@ namespace gproshan { -void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool normal, const bool color) +void cuda_create_CHE(const che * h_che, che *& dd_che, che *& d_che, const bool normal, const bool color) { - dd_che = new CHE; + dd_che = new che; dd_che->n_vertices = h_che->n_vertices; dd_che->n_trigs = h_che->n_trigs; dd_che->n_half_edges = h_che->n_half_edges; @@ -39,21 +39,21 @@ void cuda_create_CHE(CHE * h_che, CHE *& dd_che, CHE *& d_che, const bool normal cudaMalloc(&dd_che->EVT, sizeof(index_t) * h_che->n_vertices); cudaMemcpy(dd_che->EVT, h_che->EVT, sizeof(index_t) * h_che->n_vertices, cudaMemcpyHostToDevice); - cudaMalloc(&d_che, sizeof(CHE)); - cudaMemcpy(d_che, dd_che, sizeof(CHE), cudaMemcpyHostToDevice); + cudaMalloc(&d_che, sizeof(che)); + cudaMemcpy(d_che, dd_che, sizeof(che), cudaMemcpyHostToDevice); } -void cuda_free_CHE(CHE *& dd_che, CHE *& d_che) +void cuda_free_CHE(che *& dd_che, che *& d_che) { cudaFree(dd_che->GT); + cudaFree(dd_che->EVT); + cudaFree(dd_che->VT); + cudaFree(dd_che->OT); cudaFree(dd_che->VN); cudaFree(dd_che->VC); cudaFree(dd_che->VHC); - cudaFree(dd_che->VT); - cudaFree(dd_che->OT); - cudaFree(dd_che->EVT); - free(dd_che); + //delete dd_che; cudaFree(d_che); } diff --git a/src/gproshan/mesh/che_cuda.cpp b/src/gproshan/mesh/che_cuda.cpp new file mode 100644 index 00000000..009935a1 --- /dev/null +++ b/src/gproshan/mesh/che_cuda.cpp @@ -0,0 +1,76 @@ +#include + +#include +#include +#include + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +che_cuda::che_cuda(const che * mesh, const che::options & opts) +{ + if(!mesh) return; + if(!mesh->n_vertices) return; + + n_vertices = mesh->n_vertices; + n_trigs = mesh->n_trigs; + n_half_edges = mesh->n_half_edges; + + cudaMalloc(>, sizeof(vertex) * n_vertices); + cudaMemcpy(GT, mesh->GT, sizeof(vertex) * n_vertices, cudaMemcpyHostToDevice); + + cudaMalloc(&EVT, sizeof(index_t) * mesh->n_vertices); + cudaMemcpy(EVT, mesh->EVT, sizeof(index_t) * mesh->n_vertices, cudaMemcpyHostToDevice); + + cudaMalloc(&VT, sizeof(index_t) * n_half_edges); + cudaMemcpy(VT, mesh->VT, sizeof(index_t) * n_half_edges, cudaMemcpyHostToDevice); + + cudaMalloc(&OT, sizeof(index_t) * n_half_edges); + cudaMemcpy(OT, mesh->OT, sizeof(index_t) * n_half_edges, cudaMemcpyHostToDevice); + + if(opts.normals) + { + cudaMalloc(&VN, sizeof(vertex) * n_vertices); + cudaMemcpy(VN, mesh->VN, sizeof(vertex) * n_vertices, cudaMemcpyHostToDevice); + } + + if(opts.colors) + { + cudaMalloc(&VC, sizeof(che::rgb_t) * n_vertices); + cudaMemcpy(VC, mesh->VC, sizeof(che::rgb_t) * n_vertices, cudaMemcpyHostToDevice); + + cudaMalloc(&VHC, sizeof(real_t) * n_vertices); + cudaMemcpy(VHC, mesh->VHC, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + } + + cudaMalloc(&device, sizeof(che)); + cudaMemcpy(device, this, sizeof(che), cudaMemcpyHostToDevice); +} + +che_cuda::~che_cuda() +{ + gproshan_error_var("che_cuda::free: " + filename); + + cudaFree(GT); GT = nullptr; + cudaFree(EVT); EVT = nullptr; + cudaFree(VT); VT = nullptr; + cudaFree(OT); OT = nullptr; + cudaFree(VN); VN = nullptr; + cudaFree(VC); VC = nullptr; + cudaFree(VHC); VHC = nullptr; + + cudaFree(device); +} + +che_cuda::operator const che * () const +{ + return device; +} + + +} // namespace gproshan + diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index b414c3d2..d27f9365 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -75,9 +75,6 @@ embree::embree(const std::vector & meshes, const std::vector & mode embree::~embree() { - for(CHE * m: g_meshes) - delete m; - rtcReleaseScene(rtc_scene); rtcReleaseDevice(rtc_device); } @@ -114,10 +111,10 @@ void embree::build_bvh(const std::vector & meshes, const std::vectorn_trigs || pc.enable) - g_meshes[i]->n_trigs = 0; + //if(!meshes[i]->n_trigs || pc.enable) + // g_meshes[i]->n_trigs = 0; [[maybe_unused]] const index_t geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? @@ -188,8 +185,8 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) if(mesh->is_scene()) { g_meshes[geom_id]->VT = tri_idxs; - g_meshes[geom_id]->n_trigs = mesh->n_vertices / 3; - g_meshes[geom_id]->n_half_edges = mesh->n_vertices; + //g_meshes[geom_id]->n_trigs = mesh->n_vertices / 3; + //g_meshes[geom_id]->n_half_edges = mesh->n_vertices; scene * psc = (scene *) mesh; sc.materials = psc->materials.data(); @@ -203,7 +200,7 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const pc_opts & pc) { - RTCGeometry geom = rtcNewGeometry(rtc_device, pc.normals ? RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT + RTCGeometry geom = rtcNewGeometry(rtc_device, pc.normals ? RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT : RTC_GEOMETRY_TYPE_DISC_POINT); vec4 * pxyzr = (vec4 *) rtcSetNewGeometryBuffer( geom, @@ -297,7 +294,7 @@ bool embree::closesthit_radiance( vertex & color, ray_hit r(position, ray_dir); if(!intersect(r)) return false; - const CHE & mesh = *g_meshes[r.hit.geomID]; + const che & mesh = *g_meshes[r.hit.geomID]; eval_hit hit(mesh, r.hit.primID, r.hit.u, r.hit.v, sc); hit.position = r.pos(); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 23ef4e26..5202005d 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -31,7 +31,7 @@ struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) MissRecord struct __align__(OPTIX_SBT_RECORD_ALIGNMENT) HitgroupRecord { __align__(OPTIX_SBT_RECORD_ALIGNMENT) char header[OPTIX_SBT_RECORD_HEADER_SIZE]; - CHE * data; + che * data; }; @@ -411,13 +411,13 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat) { - CHE * dd_m, * d_m; - CHE h_m(mesh); + che * dd_m, * d_m; + che h_m; if(mesh->is_scene()) { - h_m.n_half_edges = mesh->n_vertices; - h_m.n_trigs = mesh->n_vertices / 3; + //h_m.n_half_edges = mesh->n_vertices; + //h_m.n_trigs = mesh->n_vertices / 3; h_m.VT = new index_t[mesh->n_vertices]; #pragma omp parallel for diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 5eb5fa76..7a9bd4ae 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -38,7 +38,7 @@ extern "C" __global__ void __closesthit__shadow() {} extern "C" __global__ void __closesthit__radiance() { - const CHE & mesh = **(const CHE **) optixGetSbtDataPointer(); + const che & mesh = **(const che **) optixGetSbtDataPointer(); const int primID = optixGetPrimitiveIndex(); const float2 bar = optixGetTriangleBarycentrics(); From f1df3cab64c9bb0595a13e4e0daabe580667c8f1 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 10:06:20 +0100 Subject: [PATCH 0964/1018] che: fix che_cuda when no cuda compiler --- include/gproshan/mesh/che_cuda.h | 3 +++ src/gproshan/mesh/che_cuda.cpp | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/gproshan/mesh/che_cuda.h b/include/gproshan/mesh/che_cuda.h index 780ae851..f56329d9 100644 --- a/include/gproshan/mesh/che_cuda.h +++ b/include/gproshan/mesh/che_cuda.h @@ -3,6 +3,8 @@ #include +#ifdef GPROSHAN_CUDA + // geometry processing and shape analysis framework namespace gproshan { @@ -22,5 +24,6 @@ class che_cuda : public che } // namespace gproshan +#endif // GPROSHAN_CUDA #endif // CHE_CUDA_H diff --git a/src/gproshan/mesh/che_cuda.cpp b/src/gproshan/mesh/che_cuda.cpp index 009935a1..3fdc7048 100644 --- a/src/gproshan/mesh/che_cuda.cpp +++ b/src/gproshan/mesh/che_cuda.cpp @@ -1,8 +1,7 @@ #include -#include -#include -#include + +#ifdef GPROSHAN_CUDA #include @@ -74,3 +73,5 @@ che_cuda::operator const che * () const } // namespace gproshan +#endif // GPROSHAN_CUDA + From 97f217dccf220bc22e77a8bd4c8f92fc5d2d0bb3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 12:19:00 +0100 Subject: [PATCH 0965/1018] che: using che_cuda optix, update scenes trigs --- include/gproshan/mesh/che.cuh | 19 --------- include/gproshan/mesh/che.h | 20 --------- include/gproshan/mesh/che_cuda.h | 2 +- include/gproshan/raytracing/embree.h | 6 +-- include/gproshan/raytracing/optix.h | 4 +- include/gproshan/raytracing/utils.h | 2 +- src/gproshan/mesh/che.cpp | 17 -------- src/gproshan/mesh/che.cu | 62 ---------------------------- src/gproshan/mesh/che_cuda.cpp | 4 +- src/gproshan/raytracing/embree.cpp | 21 ++-------- src/gproshan/raytracing/optix.cpp | 34 ++++----------- src/gproshan/raytracing/optix.cu | 2 +- src/gproshan/scenes/scene.cpp | 3 +- 13 files changed, 23 insertions(+), 173 deletions(-) delete mode 100644 include/gproshan/mesh/che.cuh delete mode 100644 src/gproshan/mesh/che.cu diff --git a/include/gproshan/mesh/che.cuh b/include/gproshan/mesh/che.cuh deleted file mode 100644 index a4e90501..00000000 --- a/include/gproshan/mesh/che.cuh +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef CHE_CUH -#define CHE_CUH - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -void cuda_create_CHE(const che * h_che, che *& dd_che, che *& d_che, const bool normal = false, const bool color = false); - -void cuda_free_CHE(che *& dd_che, che *& d_che); - - -} // namespace gproshan - -#endif // CHE_CUH - diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index c3ef369e..76d08c3e 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -216,26 +216,6 @@ class che::star_he::iterator }; -// simple che data structure -struct CHE -{ - size_t n_vertices = 0; - size_t n_trigs = 0; - size_t n_half_edges = 0; - - vertex * GT = nullptr; - vertex * VN = nullptr; - che::rgb_t * VC = nullptr; - real_t * VHC = nullptr; - index_t * VT = nullptr; - index_t * OT = nullptr; - index_t * EVT = nullptr; - - CHE() = default; - CHE(const che * mesh); -}; - - } // namespace gproshan #endif // CHE_H diff --git a/include/gproshan/mesh/che_cuda.h b/include/gproshan/mesh/che_cuda.h index f56329d9..b8205fd1 100644 --- a/include/gproshan/mesh/che_cuda.h +++ b/include/gproshan/mesh/che_cuda.h @@ -18,7 +18,7 @@ class che_cuda : public che che_cuda(const che * mesh = nullptr, const che::options & opts = che::default_opts); ~che_cuda(); - operator const che * () const; + operator che * () const; }; diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index ae9cf168..a84862cd 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -56,12 +56,12 @@ class embree : public raytracing RTCDevice rtc_device; RTCScene rtc_scene; - std::vector g_meshes; + std::vector g_meshes; scene_data sc; public: embree(); - embree( const std::vector & meshes, + embree( const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc = pc_opts() ); @@ -74,7 +74,7 @@ class embree : public raytracing protected: - void build_bvh( const std::vector & meshes, + void build_bvh( const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc = pc_opts() ); diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index 741dad28..683b2d0e 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -1,8 +1,7 @@ #ifndef RT_OPTIX_H #define RT_OPTIX_H -#include -#include +#include #include #include #include @@ -43,7 +42,6 @@ class optix : public raytracing launch_params optix_params; launch_params * optix_params_buffer = nullptr; - std::vector dd_mesh; std::vector d_mesh; void * raygen_records_buffer = nullptr; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index a272a25b..6c037f65 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index c4b652db..abbfe35e 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1144,8 +1144,6 @@ bool che::alloc(const size_t n_v, const size_t n_f, const che::options & opt) void che::free() { - gproshan_error_var("che::free: " + filename); - delete [] GT; GT = nullptr; delete [] EVT; EVT = nullptr; delete [] VT; VT = nullptr; @@ -1322,21 +1320,6 @@ index_t che::star_he::iterator::operator * () return he; } -CHE::CHE(const che * mesh) -{ - n_vertices = mesh->n_vertices; - n_trigs = mesh->n_trigs; - n_half_edges = mesh->n_half_edges; - - GT = mesh->GT; - VN = mesh->VN; - VC = mesh->VC; - VHC = mesh->VHC; - VT = mesh->VT; - OT = mesh->OT; - EVT = mesh->EVT; -} - } // namespace gproshan diff --git a/src/gproshan/mesh/che.cu b/src/gproshan/mesh/che.cu deleted file mode 100644 index b6c6d20c..00000000 --- a/src/gproshan/mesh/che.cu +++ /dev/null @@ -1,62 +0,0 @@ -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -void cuda_create_CHE(const che * h_che, che *& dd_che, che *& d_che, const bool normal, const bool color) -{ - dd_che = new che; - dd_che->n_vertices = h_che->n_vertices; - dd_che->n_trigs = h_che->n_trigs; - dd_che->n_half_edges = h_che->n_half_edges; - - cudaMalloc(&dd_che->GT, sizeof(vertex) * h_che->n_vertices); - cudaMemcpy(dd_che->GT, h_che->GT, sizeof(vertex) * h_che->n_vertices, cudaMemcpyHostToDevice); - - if(normal) - { - cudaMalloc(&dd_che->VN, sizeof(vertex) * h_che->n_vertices); - cudaMemcpy(dd_che->VN, h_che->VN, sizeof(vertex) * h_che->n_vertices, cudaMemcpyHostToDevice); - } - - if(color) - { - cudaMalloc(&dd_che->VC, sizeof(che::rgb_t) * h_che->n_vertices); - cudaMemcpy(dd_che->VC, h_che->VC, sizeof(che::rgb_t) * h_che->n_vertices, cudaMemcpyHostToDevice); - - cudaMalloc(&dd_che->VHC, sizeof(real_t) * h_che->n_vertices); - cudaMemcpy(dd_che->VHC, h_che->VHC, sizeof(real_t) * h_che->n_vertices, cudaMemcpyHostToDevice); - } - - cudaMalloc(&dd_che->VT, sizeof(index_t) * h_che->n_half_edges); - cudaMemcpy(dd_che->VT, h_che->VT, sizeof(index_t) * h_che->n_half_edges, cudaMemcpyHostToDevice); - - cudaMalloc(&dd_che->OT, sizeof(index_t) * h_che->n_half_edges); - cudaMemcpy(dd_che->OT, h_che->OT, sizeof(index_t) * h_che->n_half_edges, cudaMemcpyHostToDevice); - - cudaMalloc(&dd_che->EVT, sizeof(index_t) * h_che->n_vertices); - cudaMemcpy(dd_che->EVT, h_che->EVT, sizeof(index_t) * h_che->n_vertices, cudaMemcpyHostToDevice); - - cudaMalloc(&d_che, sizeof(che)); - cudaMemcpy(d_che, dd_che, sizeof(che), cudaMemcpyHostToDevice); -} - -void cuda_free_CHE(che *& dd_che, che *& d_che) -{ - cudaFree(dd_che->GT); - cudaFree(dd_che->EVT); - cudaFree(dd_che->VT); - cudaFree(dd_che->OT); - cudaFree(dd_che->VN); - cudaFree(dd_che->VC); - cudaFree(dd_che->VHC); - - //delete dd_che; - cudaFree(d_che); -} - - -} // namespace gproshan - diff --git a/src/gproshan/mesh/che_cuda.cpp b/src/gproshan/mesh/che_cuda.cpp index 3fdc7048..b4c3cd80 100644 --- a/src/gproshan/mesh/che_cuda.cpp +++ b/src/gproshan/mesh/che_cuda.cpp @@ -52,8 +52,6 @@ che_cuda::che_cuda(const che * mesh, const che::options & opts) che_cuda::~che_cuda() { - gproshan_error_var("che_cuda::free: " + filename); - cudaFree(GT); GT = nullptr; cudaFree(EVT); EVT = nullptr; cudaFree(VT); VT = nullptr; @@ -65,7 +63,7 @@ che_cuda::~che_cuda() cudaFree(device); } -che_cuda::operator const che * () const +che_cuda::operator che * () const { return device; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index d27f9365..44492398 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -68,7 +68,7 @@ embree::embree() rtcSetDeviceErrorFunction(rtc_device, embree_error, NULL); } -embree::embree(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc): embree() +embree::embree(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc): embree() { build_bvh(meshes, model_mats, pc); } @@ -106,7 +106,7 @@ vec4 * embree::pc_data(const index_t geomID) return (vec4 *) rtcGetGeometryBufferData(geom, RTC_BUFFER_TYPE_VERTEX, 0); } -void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc) +void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc) { g_meshes.resize(size(meshes)); for(index_t i = 0; i < size(meshes); ++i) @@ -162,20 +162,11 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) index_t * tri_idxs = (index_t *) rtcSetNewGeometryBuffer( geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(index_t), - mesh->is_scene() ? mesh->n_vertices / 3 : mesh->n_trigs + mesh->n_trigs ); - if(mesh->is_scene()) - { - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - tri_idxs[i] = i; - } - else - { - memcpy(tri_idxs, mesh->trigs_ptr(), mesh->n_half_edges * sizeof(index_t)); - } + memcpy(tri_idxs, mesh->trigs_ptr(), mesh->n_half_edges * sizeof(index_t)); rtcCommitGeometry(geom); @@ -184,10 +175,6 @@ index_t embree::add_mesh(const che * mesh, const mat4 & model_mat) if(mesh->is_scene()) { - g_meshes[geom_id]->VT = tri_idxs; - //g_meshes[geom_id]->n_trigs = mesh->n_vertices / 3; - //g_meshes[geom_id]->n_half_edges = mesh->n_vertices; - scene * psc = (scene *) mesh; sc.materials = psc->materials.data(); sc.textures = psc->textures.data(); diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 5202005d..dc81cd26 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -111,8 +111,8 @@ optix::~optix() cudaFree(hitgroup_records_buffer); cudaFree(as_buffer); - for(index_t i = 0; i < size(dd_mesh); ++i) - cuda_free_CHE(dd_mesh[i], d_mesh[i]); + for(index_t i = 0; i < size(d_mesh); ++i) + delete d_mesh[i]; cudaFree(optix_params.sc.materials); cudaFree(optix_params.sc.textures); @@ -321,7 +321,8 @@ void optix::build_sbt() { HitgroupRecord rec; optixSbtRecordPackHeader(hitgroup_programs[r], &rec); - rec.data = d_mesh[i]; + che_cuda & m = *(che_cuda *) d_mesh[i]; + rec.data = m; hitgroup_records.push_back(rec); } @@ -411,42 +412,27 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, const void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat) { - che * dd_m, * d_m; - che h_m; - - if(mesh->is_scene()) - { - //h_m.n_half_edges = mesh->n_vertices; - //h_m.n_trigs = mesh->n_vertices / 3; - h_m.VT = new index_t[mesh->n_vertices]; - - #pragma omp parallel for - for(index_t i = 0; i < mesh->n_vertices; ++i) - h_m.VT[i] = i; - } - - cuda_create_CHE(&h_m, dd_m, d_m, true, true); - dd_mesh.push_back(dd_m); + che * d_m = new che_cuda(mesh); d_mesh.push_back(d_m); float * d_model_mat = nullptr; cudaMalloc(&d_model_mat, sizeof(model_mat)); cudaMemcpy(d_model_mat, &model_mat, sizeof(model_mat), cudaMemcpyHostToDevice); - d_vertex_ptr = (CUdeviceptr) dd_m->GT; + d_vertex_ptr = (CUdeviceptr) d_m->GT; optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); - optix_mesh.triangleArray.numVertices = h_m.n_vertices; + optix_mesh.triangleArray.numVertices = d_m->n_vertices; optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = h_m.n_trigs; - optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) dd_m->VT; + optix_mesh.triangleArray.numIndexTriplets = d_m->n_trigs; + optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->VT; optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; @@ -461,8 +447,6 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u if(mesh->is_scene()) { - delete [] h_m.VT; - scene * sc = (scene *) mesh; cudaMalloc(&optix_params.sc.materials, size(sc->materials) * sizeof(scene::material)); cudaMalloc(&optix_params.sc.textures, size(sc->textures) * sizeof(scene::texture)); diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 7a9bd4ae..3c03a1a6 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index c9cf8f34..a01ea517 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -49,7 +49,7 @@ bool scene::load_obj(const std::string & file) if(!load_mtl(path + m)) return false; - alloc(size(p.trigs), 0); + alloc(size(p.trigs), size(p.trigs)); #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) @@ -57,6 +57,7 @@ bool scene::load_obj(const std::string & file) const index_t v = p.trigs[i].x(); GT[i] = p.vertices[v]; VC[i] = p.vcolors[v]; + VT[i] = i; } if(size(p.vtexcoords)) From de069b75aeed529ab2fdbb95cfe23c69a32ee749 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 12:23:11 +0100 Subject: [PATCH 0966/1018] che: const on public members --- include/gproshan/mesh/che.h | 10 +++++----- src/gproshan/mesh/che_cuda.cpp | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 76d08c3e..07127a2f 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -54,18 +54,18 @@ class che operator vertex () const; }; -size_t n_vertices = 0; -size_t n_trigs = 0; -size_t n_half_edges = 0; -size_t n_edges = 0; + const size_t n_vertices = 0; + const size_t n_trigs = 0; + const size_t n_half_edges = 0; + const size_t n_edges = 0; std::string filename; ///< get and set data member public: vertex * GT = nullptr; ///< geometry table : v -> vertex + index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * VT = nullptr; ///< vertex table (trigs) : he -> v index_t * OT = nullptr; ///< opposite table : he -> he - index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * ET = nullptr; ///< edge table : e -> he index_t * EHT = nullptr; ///< extra half edge table : he -> e diff --git a/src/gproshan/mesh/che_cuda.cpp b/src/gproshan/mesh/che_cuda.cpp index b4c3cd80..67cb901b 100644 --- a/src/gproshan/mesh/che_cuda.cpp +++ b/src/gproshan/mesh/che_cuda.cpp @@ -15,9 +15,9 @@ che_cuda::che_cuda(const che * mesh, const che::options & opts) if(!mesh) return; if(!mesh->n_vertices) return; - n_vertices = mesh->n_vertices; - n_trigs = mesh->n_trigs; - n_half_edges = mesh->n_half_edges; + rw(n_vertices) = mesh->n_vertices; + rw(n_trigs) = mesh->n_trigs; + rw(n_half_edges) = mesh->n_half_edges; cudaMalloc(>, sizeof(vertex) * n_vertices); cudaMemcpy(GT, mesh->GT, sizeof(vertex) * n_vertices, cudaMemcpyHostToDevice); From 6ce951fb555df8189407a40766be5c5bef5c865f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 17:24:45 +0100 Subject: [PATCH 0967/1018] che: fix using access methods --- include/gproshan/geodesics/geodesics_ptp.h | 8 ++++---- include/gproshan/mesh/che.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 8e9d95f6..396c6b8b 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -99,8 +99,8 @@ __forceinline__ __host_device__ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) { - const vec X[2] = {mesh->GT[x[0]] - mesh->GT[x[2]], - mesh->GT[x[1]] - mesh->GT[x[2]] + const vec X[2] = {mesh->point(x[0]) - mesh->point(x[2]), + mesh->point(x[1]) - mesh->point(x[2]) }; const vec & t = {dist[x[0]], dist[x[1]]}; @@ -159,9 +159,9 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust if(new_clusters) new_clusters[v] = old_clusters[v]; real_t d; - for_star(he, mesh, v) + for(const index_t he: mesh->star(v)) { - const uvec3 i = {mesh->VT[he_next(he)], mesh->VT[he_prev(he)], mesh->VT[he]}; + const uvec3 i = {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he)), mesh->halfedge(he)}; d = update_step(mesh, old_dist, i); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 07127a2f..341d2d6f 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -61,7 +61,7 @@ class che std::string filename; ///< get and set data member - public: + protected: vertex * GT = nullptr; ///< geometry table : v -> vertex index_t * EVT = nullptr; ///< extra vertex table : v -> he index_t * VT = nullptr; ///< vertex table (trigs) : he -> v @@ -186,7 +186,7 @@ class che static std::vector trig_convex_polygon(const index_t * P, const size_t n); static che * load_mesh(const std::string & file_path); - friend struct CHE; + friend class che_cuda; }; class che::star_he From 5b1f990accceb9b99019e3cc9dbc3bae7edf3390 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 22:29:10 +0100 Subject: [PATCH 0968/1018] che: cuda access methods implementation and iterator --- include/gproshan/geodesics/geodesics_ptp.h | 24 +- include/gproshan/mesh/che.h | 455 ++++++++++++++------- include/gproshan/raytracing/utils.h | 35 +- src/gproshan/features/key_points.cpp | 9 +- src/gproshan/geodesics/geodesics.cpp | 17 +- src/gproshan/mesh/che.cpp | 359 +++++----------- src/gproshan/mesh/che_fill_hole.cpp | 2 +- src/gproshan/mesh/che_obj.cpp | 4 +- src/gproshan/mesh/che_off.cpp | 10 +- src/gproshan/mesh/che_ply.cpp | 12 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/raytracing/optix.cpp | 4 +- 12 files changed, 455 insertions(+), 478 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 396c6b8b..94f7242f 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -103,7 +103,7 @@ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) mesh->point(x[1]) - mesh->point(x[2]) }; - const vec & t = {dist[x[0]], dist[x[1]]}; + const vec t = {dist[x[0]], dist[x[1]]}; mat q; q[0][0] = dot(X[0], X[0]); @@ -111,7 +111,7 @@ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) q[1][0] = dot(X[1], X[0]); q[1][1] = dot(X[1], X[1]); - const T & det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; + const T det = q[0][0] * q[1][1] - q[0][1] * q[1][0]; mat Q; Q[0][0] = q[1][1] / det; @@ -119,8 +119,8 @@ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) Q[1][0] = -q[1][0] / det; Q[1][1] = q[0][0] / det; - const T & delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); - const T & dis = delta * delta - + const T delta = t[0] * (Q[0][0] + Q[1][0]) + t[1] * (Q[0][1] + Q[1][1]); + const T dis = delta * delta - (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); @@ -130,13 +130,13 @@ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) T p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); #endif - const vec & tp = t - p; - const vec & n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), + const vec tp = t - p; + const vec n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), tp[0] * (X[0][1]*Q[0][0] + X[1][1]*Q[1][0]) + tp[1] * (X[0][1]*Q[0][1] + X[1][1]*Q[1][1]), tp[0] * (X[0][2]*Q[0][0] + X[1][2]*Q[1][0]) + tp[1] * (X[0][2]*Q[0][1] + X[1][2]*Q[1][1]) }; - const vec & c = Q * vec{dot(X[0], n), dot(X[1], n)}; + const vec c = Q * vec{dot(X[0], n), dot(X[1], n)}; if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { @@ -158,18 +158,20 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust real_t & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; - real_t d; for(const index_t he: mesh->star(v)) { - const uvec3 i = {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he)), mesh->halfedge(he)}; + const uvec3 i = { mesh->halfedge(he_next(he)), + mesh->halfedge(he_prev(he)), + mesh->halfedge(he) + }; - d = update_step(mesh, old_dist, i); + real_t d = update_step(mesh, old_dist, i); if(d < ndv) { ndv = d; if(new_clusters) - new_clusters[v] = old_dist[i.y()] < old_dist[i.x()] ? old_clusters[i.y()] : old_clusters[i.x()]; + new_clusters[v] = old_clusters[old_dist[i.y()] < old_dist[i.x()] ? i.y() : i.x()]; } } } diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 341d2d6f..6f760905 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -13,7 +13,6 @@ #define he_trig(he) ((he) / 3) #define he_next(he) (3 * he_trig(he) + ((he) + 1) % 3) #define he_prev(he) (3 * he_trig(he) + ((he) + 2) % 3) -#define for_star(he, mesh, v) for(index_t stop = mesh->EVT[v], he = mesh->EVT[v]; he != NIL; he = (he = mesh->OT[he_prev(he)]) != stop ? he : NIL) // geometry processing and shape analysis framework @@ -25,194 +24,332 @@ using vertex = vec3; class che { protected: - static size_t & rw(const size_t & n); - private: - class star_he; + static size_t & rw(const size_t & n); + public: - enum mesh_type : unsigned char { mtrig = 3, mquad = 4 }; ///< meshes_types - struct options + + struct options + { + bool edges = true; + bool colors = true; + bool normals = true; + }; + + static const che::options default_opts; + + struct rgb_t + { + unsigned char r = 230; + unsigned char g = 240; + unsigned char b = 250; + + __host_device__ + rgb_t() = default; + + __host_device__ + rgb_t(const vertex & v) { - bool edges = true; - bool colors = true; - bool normals = true; - }; + r = (unsigned char) (v.x() * 255); + g = (unsigned char) (v.y() * 255); + b = (unsigned char) (v.z() * 255); + } - static const che::options default_opts; + __host_device__ + rgb_t(const unsigned char ir, const unsigned char ig, const unsigned char ib): r(ir), g(ig), b(ib) {} - struct rgb_t + __host_device__ + unsigned char & operator [] (const index_t i) { - unsigned char r = 230; - unsigned char g = 240; - unsigned char b = 250; - - rgb_t() = default; - rgb_t(const vertex & v); - rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb); - unsigned char & operator [] (const index_t i); - operator vertex () const; - }; + return (&r)[i]; + } - const size_t n_vertices = 0; - const size_t n_trigs = 0; - const size_t n_half_edges = 0; - const size_t n_edges = 0; + __host_device__ + operator vertex () const + { + return {float(r) / 255, float(g) / 255, float(b) / 255}; + } + }; + + const size_t n_vertices = 0; + const size_t n_trigs = 0; + const size_t n_half_edges = 0; + const size_t n_edges = 0; + + std::string filename; ///< get and set data member - std::string filename; ///< get and set data member protected: - vertex * GT = nullptr; ///< geometry table : v -> vertex - index_t * EVT = nullptr; ///< extra vertex table : v -> he - index_t * VT = nullptr; ///< vertex table (trigs) : he -> v - index_t * OT = nullptr; ///< opposite table : he -> he - index_t * ET = nullptr; ///< edge table : e -> he - index_t * EHT = nullptr; ///< extra half edge table : he -> e - vertex * VN = nullptr; ///< vertex normals : v -> normal(v) - rgb_t * VC = nullptr; ///< vertex color : v -> color(v) - real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) - real_t scale_hm = 1; ///< vertex color heatmap scale factor + vertex * GT = nullptr; ///< geometry table : v -> vertex + index_t * EVT = nullptr; ///< extra vertex table : v -> he + index_t * VT = nullptr; ///< vertex table (trigs) : he -> v + index_t * OT = nullptr; ///< opposite table : he -> he + index_t * ET = nullptr; ///< edge table : e -> he + index_t * EHT = nullptr; ///< extra half edge table : he -> e + + vertex * VN = nullptr; ///< vertex normals : v -> normal(v) + rgb_t * VC = nullptr; ///< vertex color : v -> color(v) + real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) + real_t scale_hm = 1; ///< vertex color heatmap scale factor + + bool manifold = true; - bool manifold = true; public: - che(const che & mesh, const index_t * sorted = nullptr, const che::options & opts = default_opts); - che(const size_t n_v = 0, const size_t n_f = 0); - che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); - virtual ~che(); - - // vertex access geometry methods to xyz point values, normals, and gradient - const vertex & point(const index_t v) const; - vertex & point(const index_t v); - const vertex & normal(const index_t v) const; - vertex & normal(const index_t v); - vertex shading_normal(const index_t f, const float u, const float v) const; - vertex normal_trig(const index_t f) const; - vertex normal_he(const index_t he) const; - vertex gradient_he(const index_t he, const real_t * f) const; - dvec3 gradient_he(const index_t he, const double * f) const; - vertex gradient(const index_t v, const real_t * f); - - // vertex color methods - const real_t * heatmap_ptr() const; - real_t heatmap_scale() const; - void heatmap_scale(const real_t shm); - real_t heatmap_scale(const index_t v) const; - real_t heatmap(const index_t v) const; - real_t & heatmap(const index_t v); - const rgb_t * rgb_ptr() const; - rgb_t rgb(const index_t v) const; - rgb_t & rgb(const index_t v); - vertex color(const index_t v) const; - vertex shading_color(const index_t f, const float u, const float v) const; - - // update methods - void reload(); - mat4 normalize_sphere(const real_t r = 1) const; - mat4 normalize_box(const real_t side = 2) const; - che * merge(const che * mesh, const std::vector & com_vertices = {}); - void update_vertices(const vertex * positions, const size_t n = 0, const index_t v_i = 0); - void update_heatmap(const real_t * hm = nullptr); - void update_normals(); - void invert_normals(); - void multiplicate_vertices(); - void remove_vertices(const std::vector & vertices); - void remove_non_manifold_vertices(); - void set_head_vertices(index_t * head, const size_t n); - - // half edge access methods triangular trigs and navigation - const index_t * trigs_ptr() const; - index_t halfedge(const index_t he) const; - index_t twin_he(const index_t he) const; - index_t edge_u(const index_t e) const; - index_t edge_v(const index_t e) const; - index_t edge_he_0(const index_t e) const; - index_t edge_he_1(const index_t e) const; - const vertex & vertex_he(const index_t he) const; - const vertex & vertex_edge_u(const index_t e) const; - const vertex & vertex_edge_v(const index_t e) const; - index_t evt(const index_t v) const; - - // topology methods - che::star_he star(const index_t v) const; - std::vector link(const index_t v) const; - void edge_collapse(const std::vector & sort_edges); - void compute_toplesets(index_t * rings, index_t * sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); - - // boundary methods - std::vector bounds() const; - std::vector boundary(const index_t v) const; - bool is_vertex_bound(const index_t v) const; - bool is_edge_bound(const index_t e) const; - - // file, name, and system methods - const std::string name() const; - const std::string name_size() const; - const std::string filename_size() const; - - // mesh information methods - size_t genus() const; - size_t memory() const; - size_t max_degree() const; - real_t quality() const; - real_t mean_edge() const; - real_t area_surface() const; - bool is_manifold() const; - virtual bool is_scene() const; - virtual bool is_pointcloud() const; - - // operation methods - void flip(const index_t e); - real_t cotan(const index_t he) const; - real_t pdetriq(const index_t t) const; - real_t area_trig(const index_t t) const; - real_t area_vertex(const index_t v) const; - real_t mean_curvature(const index_t v) const; + + che(const che & mesh, const index_t * sorted = nullptr, const che::options & opts = default_opts); + che(const size_t nv = 0, const size_t nf = 0); + che(const vertex * vertices, const index_t nv, const index_t * trigs, const index_t nf); + virtual ~che(); + + void reload(); + mat4 normalize_sphere(const real_t r = 1) const; + mat4 normalize_box(const real_t side = 2) const; + che * merge(const che * mesh, const std::vector & com_vertices = {}); + void update_vertices(const vertex * positions, const size_t n = 0, const index_t v_i = 0); + void update_heatmap(const real_t * hm = nullptr); + void update_normals(); + void invert_normals(); + void multiplicate_vertices(); + void remove_vertices(const std::vector & vertices); + void remove_non_manifold_vertices(); + void set_head_vertices(index_t * head, const size_t n); + + + __host_device__ + const vertex & point(const index_t v) const + { + assert(v < n_vertices); + return GT[v]; + } + + __host_device__ + vertex & point(const index_t v) + { + assert(v < n_vertices); + return GT[v]; + } + + __host_device__ + const vertex & normal(const index_t v) const + { + assert(VN && v < n_vertices); + return VN[v]; + } + + __host_device__ + vertex & normal(const index_t v) + { + assert(VN && v < n_vertices); + return VN[v]; + } + + __host_device__ + real_t heatmap(const index_t v) const + { + assert(v < n_vertices); + return VHC[v]; + } + + __host_device__ + real_t & heatmap(const index_t v) + { + assert(v < n_vertices); + return VHC[v]; + } + + __host_device__ + rgb_t rgb(const index_t v) const + { + assert(VC && v < n_vertices); + return VC[v]; + } + + __host_device__ + rgb_t & rgb(const index_t v) + { + assert(VC && v < n_vertices); + return VC[v]; + } + + __host_device__ + vertex color(const index_t v) const + { + assert(VC && v < n_vertices); + return VC[v]; + } + + __host_device__ + uvec3 trig(const index_t t) const + { + assert(t < n_trigs); + const index_t he = t * 3; + return {VT[he], VT[he + 1], VT[he + 2]}; + } + + __host_device__ + index_t halfedge(const index_t he) const + { + assert(he < n_half_edges); + return VT[he]; + } + + __host_device__ + const index_t * trigs_ptr() const + { + return VT; + } + + __host_device__ + const rgb_t * rgb_ptr() const + { + return VC; + } + + __host_device__ + const real_t * heatmap_ptr() const + { + return VHC; + } + + + vertex normal_trig(const index_t f) const; + vertex normal_he(const index_t he) const; + vertex gradient_he(const index_t he, const real_t * f) const; + dvec3 gradient_he(const index_t he, const double * f) const; + vertex gradient(const index_t v, const real_t * f); + + real_t heatmap_scale() const; + void heatmap_scale(const real_t shm); + real_t heatmap_scale(const index_t v) const; + + index_t twin_he(const index_t he) const; + index_t edge_u(const index_t e) const; + index_t edge_v(const index_t e) const; + index_t edge_he_0(const index_t e) const; + index_t edge_he_1(const index_t e) const; + const vertex & vertex_he(const index_t he) const; + const vertex & vertex_edge_u(const index_t e) const; + const vertex & vertex_edge_v(const index_t e) const; + index_t evt(const index_t v) const; + + std::vector link(const index_t v) const; + void edge_collapse(const std::vector & sort_edges); + void compute_toplesets(index_t * rings, index_t * sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); + + std::vector bounds() const; + std::vector boundary(const index_t v) const; + bool is_vertex_bound(const index_t v) const; + bool is_edge_bound(const index_t e) const; + + const std::string name() const; + const std::string name_size() const; + const std::string filename_size() const; + + size_t genus() const; + size_t memory() const; + size_t max_degree() const; + real_t quality() const; + real_t mean_edge() const; + real_t area_surface() const; + bool is_manifold() const; + virtual bool is_scene() const; + virtual bool is_pointcloud() const; + + void flip(const index_t e); + real_t cotan(const index_t he) const; + real_t pdetriq(const index_t t) const; + real_t area_trig(const index_t t) const; + real_t area_vertex(const index_t v) const; + real_t mean_curvature(const index_t v) const; + protected: - void init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); - void init(const std::string & file); - bool alloc(const size_t n_v, const size_t n_f, const che::options & opts = default_opts); - virtual void free(); + void init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f); + void init(const std::string & file); + bool alloc(const size_t n_v, const size_t n_f, const che::options & opts = default_opts); + void free(); - virtual void read_file(const std::string & file); + virtual void read_file(const std::string & file); private: - void update_evt_ot_et(); - void update_eht(); + void update_evt_ot_et(); + void update_eht(); public: - static std::vector trig_convex_polygon(const index_t * P, const size_t n); - static che * load_mesh(const std::string & file_path); + static std::vector trig_convex_polygon(const index_t * P, const size_t n); + static che * load_mesh(const std::string & file_path); + friend class che_cuda; -}; -class che::star_he -{ - class iterator; - const che * mesh; - const index_t v; + private: - public: - star_he(const che * p_mesh, const index_t p_v); - iterator begin() const; - iterator end() const; -}; + class star_he + { + struct iterator + { + const che * mesh; + index_t he; + const index_t he_end; + + __host_device__ + iterator(const che * p_mesh, const index_t p_he, const index_t p_he_end): + mesh(p_mesh), he(p_he), he_end(p_he_end) {} + + __host_device__ + iterator & operator ++ () + { + he = mesh->OT[he_prev(he)]; + he = he != he_end ? he : NIL; + return *this; + } + + __host_device__ + bool operator != (const iterator & it) const + { + return he != it.he; + } + + __host_device__ + index_t operator * () + { + return he; + } + }; + + const che * mesh; + const index_t v; + + public: + + __host_device__ + star_he(const che * p_mesh, const index_t p_v): mesh(p_mesh), v(p_v) {} + + __host_device__ + iterator begin() const + { + return {mesh, mesh->EVT[v], mesh->EVT[v]}; + } + + __host_device__ + iterator end() const + { + return {nullptr, NIL, NIL}; + } + }; -class che::star_he::iterator -{ - const che * mesh; - index_t he; - const index_t he_end; public: - iterator(const che * p_mesh, const index_t p_he, const index_t p_he_end); - iterator & operator ++ (); - bool operator != (const iterator & it) const; - index_t operator * (); + + __host_device__ + che::star_he star(const index_t v) const + { + return {this, v}; + } }; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 6c037f65..5aedc4bd 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -99,26 +99,25 @@ struct t_eval_hit if(!mesh.n_trigs) // pointcloud { - Kd = {T(mesh.VC[aprimID].r), T(mesh.VC[aprimID].g), T(mesh.VC[aprimID].b)}; - Kd /= 255; - normal = mesh.VN[aprimID]; - heatmap = mesh.VHC[aprimID]; + Kd = mesh.color(primID); + normal = mesh.normal(primID); + heatmap = mesh.heatmap(primID); return; } - const index_t he = primID * che::mtrig; + const uvec3 trig = mesh.trig(primID); - const index_t a = mesh.VT[he]; - const index_t b = mesh.VT[he + 1]; - const index_t c = mesh.VT[he + 2]; + const vec ca = mesh.color(trig.x()); + const vec cb = mesh.color(trig.y()); + const vec cc = mesh.color(trig.z()); - const vec ca = {T(mesh.VC[a].r), T(mesh.VC[a].g), T(mesh.VC[a].b)}; - const vec cb = {T(mesh.VC[b].r), T(mesh.VC[b].g), T(mesh.VC[b].b)}; - const vec cc = {T(mesh.VC[c].r), T(mesh.VC[c].g), T(mesh.VC[c].b)}; - - Kd = ((1.f - u - v) * ca + u * cb + v * cc) / 255; - normal = (1.f - u - v) * mesh.VN[a] + u * mesh.VN[b] + v * mesh.VN[c]; - heatmap = (1.f - u - v) * mesh.VHC[a] + u * mesh.VHC[b] + v * mesh.VHC[c]; + Kd = (1.f - u - v) * ca + u * cb + v * cc; + normal = (1.f - u - v) * mesh.normal(trig.x()) + + u * mesh.normal(trig.y()) + + v * mesh.normal(trig.z()); + heatmap = (1.f - u - v) * mesh.heatmap(trig.x()) + + u * mesh.heatmap(trig.y()) + + v * mesh.heatmap(trig.z()); if(!sc.trig_mat) return; if(sc.trig_mat[primID] == NIL) return; @@ -126,7 +125,9 @@ struct t_eval_hit const scene::material & mat = sc.materials[sc.trig_mat[primID]]; vec texcoord; if(sc.texcoords) - texcoord = (1.f - u - v) * sc.texcoords[a] + u * sc.texcoords[b] + v * sc.texcoords[c]; + texcoord = (1.f - u - v) * sc.texcoords[trig.x()] + + u * sc.texcoords[trig.y()] + + v * sc.texcoords[trig.z()]; Ka = mat.Ka; if(mat.map_Ka != -1) @@ -287,7 +288,7 @@ index_t closest_hit_vertex(const che & mesh, const H & hit) w = hit.v; } - return mesh.VT[hit.primID * 3 + he]; + return mesh.halfedge(hit.primID * 3 + he); } diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 9dc264ad..783d7f45 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -35,18 +35,17 @@ void key_points::compute_kps_areas(che * mesh, const real_t percent) is_kp.assign(mesh->n_vertices, false); kps.reserve(mesh->n_vertices); - for(index_t he, t = 0; t < mesh->n_trigs; ++t) + for(index_t t = 0; t < mesh->n_trigs; ++t) { - he = che::mtrig * face_areas[t].second; - for(index_t i = 0; i < che::mtrig; ++i) + uvec3 vi = mesh->trig(t); + for(index_t i = 0; i < 3; ++i) { - const index_t v = mesh->halfedge(he); + const index_t v = vi[i]; if(!is_kp[v]) { kps.push_back(v); is_kp[v] = true; } - he = he_next(he); } } diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 6e98aa4a..4bb3d8fd 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -171,16 +171,19 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source dv = dist[v]; for(const index_t he: mesh->star(v)) { - dp = update_step(mesh, dist, { mesh->halfedge(he_next(he)), - mesh->halfedge(he_prev(he)), - mesh->halfedge(he) - }); - if(dp < dv) + const uvec3 i = { mesh->halfedge(he_next(he)), + mesh->halfedge(he_prev(he)), + mesh->halfedge(he) + }; + + real_t d = update_step(mesh, old_dist, i); + + if(d < dv) { - dv = dp; + dv = d; if(clusters) - clusters[v] = clusters[mesh->halfedge(he_prev(he))] ? clusters[mesh->halfedge(he_prev(he))] : clusters[mesh->halfedge(he_next(he))]; + clusters[v] = clusters[clusters[i.y()] ? i.y() : i.x()]; } } diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index abbfe35e..b1b8f2f3 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -28,25 +28,6 @@ size_t & che::rw(const size_t & n) const che::options che::default_opts; -che::rgb_t::rgb_t(const vertex & v) -{ - r = (unsigned char) (v.x() * 255); - g = (unsigned char) (v.y() * 255); - b = (unsigned char) (v.z() * 255); -} - -che::rgb_t::rgb_t(const unsigned char & cr, const unsigned char & cg, const unsigned char & cb): r(cr), g(cg), b(cb) {} - -unsigned char & che::rgb_t::operator [] (const index_t i) -{ - return (&r)[i]; -} - -che::rgb_t::operator vertex () const -{ - return {float(r) / 255, float(g) / 255, float(b) / 255}; -} - che::che(const che & mesh, const index_t * sorted, const che::options & opts) { @@ -127,14 +108,14 @@ che::che(const che & mesh, const index_t * sorted, const che::options & opts) } } -che::che(const size_t n_v, const size_t n_f) +che::che(const size_t nv, const size_t nf) { - alloc(n_v, n_f); + alloc(nv, nf); } -che::che(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f) +che::che(const vertex * vertices, const index_t nv, const index_t * trigs, const index_t nf) { - init(vertices, n_v, trigs, n_f); + init(vertices, nv, trigs, nf); } che::~che() @@ -143,178 +124,6 @@ che::~che() } -// vertex access geometry methods to xyz point values, normals, and gradient - -const vertex & che::point(const index_t v) const -{ - assert(v < n_vertices); - return GT[v]; -} - -vertex & che::point(const index_t v) -{ - assert(v < n_vertices); - return GT[v]; -} - -const vertex & che::normal(const index_t v) const -{ - assert(VN && v < n_vertices); - return VN[v]; -} - -vertex & che::normal(const index_t v) -{ - assert(VN && v < n_vertices); - return VN[v]; -} - -vertex che::shading_normal(const index_t f, const float u, const float v) const -{ - const index_t he = f * che::mtrig; - return normalize(u * VN[VT[he]] + v * VN[VT[he + 1]] + (1 - u - v) * VN[VT[he + 2]]); -} - -vertex che::normal_trig(const index_t f) const -{ - return normal_he(f * che::mtrig); -} - -vertex che::normal_he(const index_t he) const -{ - const vertex & a = GT[VT[he]]; - const vertex & b = GT[VT[he_next(he)]]; - const vertex & c = GT[VT[he_prev(he)]]; - - return normalize(cross(b - a, c - a)); -} - -vertex che::gradient_he(const index_t he, const real_t * f) const -{ - index_t i = VT[he]; - index_t j = VT[he_next(he)]; - index_t k = VT[he_prev(he)]; - - const vertex & xi = GT[i]; - const vertex & xj = GT[j]; - const vertex & xk = GT[k]; - - const vertex & n = normal_he(he); - - vertex pij = cross(n, xj - xi); - vertex pjk = cross(n, xk - xj); - vertex pki = cross(n, xi - xk); - - return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); -} - -dvec3 che::gradient_he(const index_t he, const double * f) const -{ - index_t i = VT[he]; - index_t j = VT[he_next(he)]; - index_t k = VT[he_prev(he)]; - - const vertex & gi = GT[i]; - const vertex & gj = GT[j]; - const vertex & gk = GT[k]; - - const dvec3 xi = {gi.x(), gi.y(), gi.z()}; - const dvec3 xj = {gj.x(), gj.y(), gj.z()}; - const dvec3 xk = {gk.x(), gk.y(), gk.z()}; - - const vertex & n = normal_he(he); - const dvec3 dn = {n.x(), n.y(), n.z()}; - - dvec3 pij = cross(dn, xj - xi); - dvec3 pjk = cross(dn, xk - xj); - dvec3 pki = cross(dn, xi - xk); - - return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); -} - -vertex che::gradient(const index_t v, const real_t * f) -{ - vertex g; - real_t area, area_star = 0; - - for(const index_t he: star(v)) - { - area = area_trig(he_trig(he)); - area_star += area; - g += area * gradient_he(he, f); - } - - return g / area_star; -} - - -// vertex color methods - -const real_t * che::heatmap_ptr() const -{ - return VHC; -} - -real_t che::heatmap_scale() const -{ - return scale_hm; -} - -void che::heatmap_scale(const real_t shm) -{ - scale_hm = shm; -} - -real_t che::heatmap_scale(const index_t v) const -{ - assert(v < n_vertices); - return scale_hm * VHC[v]; -} - -real_t che::heatmap(const index_t v) const -{ - assert(v < n_vertices); - return VHC[v]; -} - -real_t & che::heatmap(const index_t v) -{ - assert(v < n_vertices); - return VHC[v]; -} - -const che::rgb_t * che::rgb_ptr() const -{ - return VC; -} - -che::rgb_t che::rgb(const index_t v) const -{ - assert(v < n_vertices); - return VC[v]; -} - -che::rgb_t & che::rgb(const index_t v) -{ - assert(v < n_vertices); - return VC[v]; -} - -vertex che::color(const index_t v) const -{ - assert(VC && v < n_vertices); - return VC[v]; -} - -vertex che::shading_color(const index_t f, const float u, const float v) const -{ - const index_t he = f * che::mtrig; - return u * color(VT[he]) + v * color(VT[he + 1]) + (1 - u - v) * color(VT[he + 2]); -} - - -// update methods - void che::reload() { free(); @@ -486,8 +295,8 @@ void che::multiplicate_vertices() for(index_t f = 0; f < nf; ++f) { const index_t v = nv + f; - const index_t old_he = f * che::mtrig; - const index_t he = 3 * f * che::mtrig; + const index_t old_he = f * 3; + const index_t he = 3 * f * 3; GT[v] = (GT[old_VT[old_he]] + GT[old_VT[old_he + 1]] + GT[old_VT[old_he + 2]]) / 3; @@ -582,7 +391,7 @@ void che::remove_vertices(const std::vector & vertices) gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / che::mtrig); + init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / 3); gproshan_debug(removing vertex); } @@ -641,7 +450,7 @@ void che::remove_non_manifold_vertices() gproshan_debug(removing vertex); free(); gproshan_debug(removing vertex); - init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / che::mtrig); + init(new_vertices.data(), size(new_vertices), new_trigs.data(), size(new_trigs) / 3); gproshan_debug(removing vertex); } @@ -671,19 +480,96 @@ void che::set_head_vertices(index_t * head, const size_t n) } -// half edge access methods triangular trigs and navigation +vertex che::normal_trig(const index_t f) const +{ + return normal_he(f * 3); +} -const index_t * che::trigs_ptr() const +vertex che::normal_he(const index_t he) const { - return VT; + const vertex & a = GT[VT[he]]; + const vertex & b = GT[VT[he_next(he)]]; + const vertex & c = GT[VT[he_prev(he)]]; + + return normalize(cross(b - a, c - a)); } -index_t che::halfedge(const index_t he) const +vertex che::gradient_he(const index_t he, const real_t * f) const { - assert(he < n_half_edges); - return VT[he]; + index_t i = VT[he]; + index_t j = VT[he_next(he)]; + index_t k = VT[he_prev(he)]; + + const vertex & xi = GT[i]; + const vertex & xj = GT[j]; + const vertex & xk = GT[k]; + + const vertex & n = normal_he(he); + + vertex pij = cross(n, xj - xi); + vertex pjk = cross(n, xk - xj); + vertex pki = cross(n, xi - xk); + + return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } +dvec3 che::gradient_he(const index_t he, const double * f) const +{ + index_t i = VT[he]; + index_t j = VT[he_next(he)]; + index_t k = VT[he_prev(he)]; + + const vertex & gi = GT[i]; + const vertex & gj = GT[j]; + const vertex & gk = GT[k]; + + const dvec3 xi = {gi.x(), gi.y(), gi.z()}; + const dvec3 xj = {gj.x(), gj.y(), gj.z()}; + const dvec3 xk = {gk.x(), gk.y(), gk.z()}; + + const vertex & n = normal_he(he); + const dvec3 dn = {n.x(), n.y(), n.z()}; + + dvec3 pij = cross(dn, xj - xi); + dvec3 pjk = cross(dn, xk - xj); + dvec3 pki = cross(dn, xi - xk); + + return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); +} + +vertex che::gradient(const index_t v, const real_t * f) +{ + vertex g; + real_t area, area_star = 0; + + for(const index_t he: star(v)) + { + area = area_trig(he_trig(he)); + area_star += area; + g += area * gradient_he(he, f); + } + + return g / area_star; +} + + +real_t che::heatmap_scale() const +{ + return scale_hm; +} + +void che::heatmap_scale(const real_t shm) +{ + scale_hm = shm; +} + +real_t che::heatmap_scale(const index_t v) const +{ + assert(v < n_vertices); + return scale_hm * VHC[v]; +} + + index_t che::twin_he(const index_t he) const { assert(he < n_half_edges); @@ -739,13 +625,6 @@ index_t che::evt(const index_t v) const } -// topology methods - -che::star_he che::star(const index_t v) const -{ - return {this, v}; -} - std::vector che::link(const index_t v) const { assert(v < n_vertices); @@ -808,8 +687,6 @@ void che::compute_toplesets(index_t * toplesets, index_t * sorted, std::vector che::bounds() const { @@ -865,8 +742,6 @@ bool che::is_edge_bound(const index_t e) const } -// file, name, and system methods - const std::string che::name() const { index_t p = filename.find_last_of('/'); @@ -885,8 +760,6 @@ const std::string che::filename_size() const } -// mesh information methods - size_t che::genus() const { size_t g = n_vertices - n_edges + n_trigs; @@ -966,8 +839,6 @@ bool che::is_pointcloud() const } -// operation methods - void che::flip(const index_t e) { index_t ha = ET[e]; @@ -1040,7 +911,7 @@ real_t che::cotan(const index_t he) const // h1^2+h2^2+h3^2 real_t che::pdetriq(const index_t t) const { - index_t he = t * che::mtrig; + index_t he = t * 3; real_t h[3] = { norm(GT[VT[he_next(he)]] - GT[VT[he]]), norm(GT[VT[he_prev(he)]] - GT[VT[he_next(he)]]), @@ -1051,7 +922,7 @@ real_t che::pdetriq(const index_t t) const real_t che::area_trig(const index_t t) const { - index_t he = t * che::mtrig; + index_t he = t * 3; vertex a = GT[VT[he_next(he)]] - GT[VT[he]]; vertex b = GT[VT[he_prev(he)]] - GT[VT[he]]; @@ -1083,8 +954,6 @@ real_t che::mean_curvature(const index_t v) const } -// protected - void che::init(const vertex * vertices, const index_t n_v, const index_t * trigs, const index_t n_f) { alloc(n_v, n_f); @@ -1111,7 +980,7 @@ bool che::alloc(const size_t n_v, const size_t n_f, const che::options & opt) rw(n_vertices) = n_v; rw(n_trigs) = n_f; - rw(n_half_edges) = che::mtrig * n_trigs; + rw(n_half_edges) = 3 * n_trigs; rw(n_edges) = n_half_edges; // max number of edges GT = new vertex[n_vertices]; @@ -1155,8 +1024,10 @@ void che::free() delete [] VHC; VHC = nullptr; } + void che::read_file(const std::string & ) {} /* virtual */ + void che::update_evt_ot_et() { if(!n_trigs) return; @@ -1236,12 +1107,10 @@ void che::update_eht() } -// static - std::vector che::trig_convex_polygon(const index_t * P, const size_t n) { std::vector trigs; - trigs.reserve(che::mtrig * (n - 2)); + trigs.reserve(3 * (n - 2)); index_t a = n - 1; index_t b = 0; @@ -1287,39 +1156,5 @@ che * che::load_mesh(const std::string & file_path) } -// iterator classes methods - -che::star_he::star_he(const che * p_mesh, const index_t p_v): mesh(p_mesh), v(p_v) {} - -che::star_he::iterator che::star_he::begin() const -{ - return {mesh, mesh->EVT[v], mesh->EVT[v]}; -} - -che::star_he::iterator che::star_he::end() const -{ - return {nullptr, NIL, NIL}; -} - -che::star_he::iterator::iterator(const che * p_mesh, const index_t p_he, const index_t p_he_end): mesh(p_mesh), he(p_he), he_end(p_he_end) {} - -che::star_he::iterator & che::star_he::iterator::operator ++ () -{ - he = mesh->OT[he_prev(he)]; - he = he != he_end ? he : NIL; - return *this; -} - -bool che::star_he::iterator::operator != (const iterator & it) const -{ - return he != it.he; -} - -index_t che::star_he::iterator::operator * () -{ - return he; -} - - } // namespace gproshan diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index f5c8b1b1..50e63a2e 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -884,7 +884,7 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic size_t n_trigs = size(select_vertices) + 4; vertex * vertices = new vertex[n_vertices]; - index_t * trigs = new index_t[n_trigs * che::mtrig]; + index_t * trigs = new index_t[n_trigs * 3]; std::vector triangle; std::vector tri_sizes(3,0); diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 407f90f4..4c28489e 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -17,7 +17,7 @@ che_obj::che_obj(const std::string & file) void che_obj::read_file(const std::string & file) { parser p(file); - alloc(size(p.vertices), size(p.trigs) / che::mtrig); + alloc(size(p.vertices), size(p.trigs) / 3); memcpy(GT, p.vertices.data(), size(p.vertices) * sizeof(vertex)); memcpy(VC, p.vcolors.data(), size(p.vcolors) * sizeof(rgb_t)); @@ -52,7 +52,7 @@ void che_obj::write_file(const che * mesh, const std::string & file, const bool for(index_t he = 0; he < mesh->n_half_edges; ) { fprintf(fp, "f"); - for(index_t i = 0; i < che::mtrig; ++i) + for(index_t i = 0; i < 3; ++i) fprintf(fp, " %u", mesh->halfedge(he++) + 1); fprintf(fp, "\n"); } diff --git a/src/gproshan/mesh/che_off.cpp b/src/gproshan/mesh/che_off.cpp index 365c692e..4ed0d5c2 100644 --- a/src/gproshan/mesh/che_off.cpp +++ b/src/gproshan/mesh/che_off.cpp @@ -48,7 +48,7 @@ void che_off::read_file(const std::string & file) } std::vector trigs; - trigs.reserve(che::mtrig * n_trigs); + trigs.reserve(3 * n_trigs); index_t P[32]; while(nf--) @@ -64,14 +64,14 @@ void che_off::read_file(const std::string & file) fclose(fp); - if(size(trigs) != che::mtrig * n_trigs) + if(size(trigs) != 3 * n_trigs) { vertex * tGT = GT; GT = nullptr; vertex * tVN = VN; VN = nullptr; rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, size(trigs) / che::mtrig); + alloc(nv, size(trigs) / 3); GT = tGT; VN = tVN; @@ -115,8 +115,8 @@ void che_off::write_file(const che * mesh, const std::string & file, const che_o { for(index_t he = 0; he < mesh->n_half_edges; ) { - fprintf(fp, "%u", che::mtrig); - for(index_t i = 0; i < che::mtrig; ++i) + fprintf(fp, "%u", 3); + for(index_t i = 0; i < 3; ++i) fprintf(fp, " %u", mesh->halfedge(he++)); fprintf(fp, "\n"); } diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 57d0d0ae..bb34eccc 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -102,7 +102,7 @@ void che_ply::read_file(const std::string & file) alloc(nv, nf); std::vector trigs; - trigs.reserve(che::mtrig * n_trigs); + trigs.reserve(3 * n_trigs); if(format[0] == 'a') // ascii { @@ -237,13 +237,13 @@ void che_ply::read_file(const std::string & file) fclose(fp); - if(size(trigs) != che::mtrig * n_trigs) + if(size(trigs) != 3 * n_trigs) { vertex * tGT = GT; GT = nullptr; rgb_t * tVC = VC; VC = nullptr; free(); - alloc(nv, size(trigs) / che::mtrig); + alloc(nv, size(trigs) / 3); GT = tGT; VC = tVC; @@ -287,11 +287,11 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool if(color) fwrite(p_rgb + v, sizeof(rgb_t), 1, fp); } - unsigned char mtrig = che::mtrig; - for(index_t he = 0; he < mesh->n_half_edges; he += che::mtrig) + unsigned char mtrig = 3; + for(index_t he = 0; he < mesh->n_half_edges; he += 3) { fwrite(&mtrig, 1, 1, fp); - fwrite(mesh->trigs_ptr(), sizeof(index_t), che::mtrig, fp); + fwrite(mesh->trigs_ptr(), sizeof(index_t), 3, fp); } fclose(fp); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 402ac942..3e83f54f 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -117,7 +117,7 @@ void che_ptx::read_file(const std::string & file) } rw(n_half_edges) = he; - rw(n_trigs) = he / che::mtrig; + rw(n_trigs) = he / 3; } void che_ptx::write_file(const che * mesh, const std::string & file, const size_t n_rows, const size_t n_cols) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index dc81cd26..a68b2c92 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -419,7 +419,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u cudaMalloc(&d_model_mat, sizeof(model_mat)); cudaMemcpy(d_model_mat, &model_mat, sizeof(model_mat), cudaMemcpyHostToDevice); - d_vertex_ptr = (CUdeviceptr) d_m->GT; + d_vertex_ptr = (CUdeviceptr) &d_m->point(0); optix_mesh = {}; optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; @@ -432,7 +432,7 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); optix_mesh.triangleArray.numIndexTriplets = d_m->n_trigs; - optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->VT; + optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->trigs_ptr(); optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; From f66b6110e760c1d6794dabef7d1c35539c00baa4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 22:51:56 +0100 Subject: [PATCH 0969/1018] vec: update value access methods review embree pointcloud rendering --- include/gproshan/geometry/mat.h | 2 +- include/gproshan/geometry/vec.h | 34 ++++++++++++---------------- include/gproshan/raytracing/utils.h | 10 ++++---- src/gproshan/geodesics/geodesics.cpp | 11 +++------ src/gproshan/raytracing/embree.cpp | 8 ++----- src/gproshan/viewer/shader.cpp | 8 +++---- 6 files changed, 30 insertions(+), 43 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index cfb0b77e..a4b0d170 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -33,7 +33,7 @@ class mat } __host_device__ - const T & operator () (const index_t i, const index_t j) const + T operator () (const index_t i, const index_t j) const { return rows[i][j]; } diff --git a/include/gproshan/geometry/vec.h b/include/gproshan/geometry/vec.h index 7564fc30..8123bd12 100644 --- a/include/gproshan/geometry/vec.h +++ b/include/gproshan/geometry/vec.h @@ -24,7 +24,7 @@ class vec public: __host_device__ - vec(const T & val = 0) + vec(const T val = 0) { for(T & v: values) v = val; @@ -34,12 +34,12 @@ class vec vec(const std::initializer_list & list) { int i = -1; - for(const T & v: list) + for(T v: list) values[++i] = v; } __host_device__ - vec(const vec & v, const T & val = 0) + vec(const vec & v, const T val = 0) { for(index_t i = 0; i < N - 1; ++i) values[i] = v[i]; @@ -61,7 +61,7 @@ class vec } __host_device__ - const T & operator [] (const index_t i) const + T operator [] (const index_t i) const { assert(i < N); return values[i]; @@ -69,7 +69,7 @@ class vec ///< concatenate with comma operator __host_device__ - vec operator , (const T & a) const + vec operator , (const T a) const { return {*this, a}; } @@ -82,7 +82,7 @@ class vec } __host_device__ - const T & x() const + T x() const { assert(N > 0); return values[0]; @@ -96,7 +96,7 @@ class vec } __host_device__ - const T & y() const + T y() const { assert(N > 1); return values[1]; @@ -110,7 +110,7 @@ class vec } __host_device__ - const T & z() const + T z() const { assert(N > 2); return values[2]; @@ -122,7 +122,7 @@ class vec T norm() const { T res = 0; - for(const T & v: values) + for(T v: values) res += v * v; return std::sqrt(res); } @@ -258,7 +258,7 @@ class vec ///< scalar product template __host_device__ -vec operator * (const T & a, const vec & v) +vec operator * (const T a, const vec & v) { return v * a; } @@ -266,7 +266,7 @@ vec operator * (const T & a, const vec & v) ///< scalar product template __host_device__ -vec operator / (const T & a, const vec & v) +vec operator / (const T a, const vec & v) { vec res; for(index_t i = 0; i < N; ++i) @@ -287,14 +287,10 @@ template __host_device__ vec cross(const vec & u, const vec & v) { - const T & ux = u[0]; - const T & uy = u[1]; - const T & uz = u[2]; - const T & vx = v[0]; - const T & vy = v[1]; - const T & vz = v[2]; - - return {uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx}; + return {u[1] * v[2] - u[2] * v[1], + u[2] * v[0] - u[0] * v[2], + u[0] * v[1] - u[1] * v[0] + }; } ///< dot product diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 5aedc4bd..9d646686 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -91,7 +91,7 @@ struct t_eval_hit t_eval_hit() {} __host_device__ - t_eval_hit(const che & mesh, const index_t aprimID, const T & au, const T & av, const scene_data & sc) + t_eval_hit(const che & mesh, const index_t aprimID, const T au, const T av, const scene_data & sc) { primID = aprimID; u = au; @@ -190,9 +190,9 @@ struct t_eval_hit bool scatter_diffuse(vec & dir, random & rnd) { // random unit sphere - const T & theta = rnd() * 2 * M_PI; - const T & phi = acosf(2 * rnd() - 1); - const T & r = cbrtf(rnd()); + const T theta = rnd() * 2 * M_PI; + const T phi = acosf(2 * rnd() - 1); + const T r = cbrtf(rnd()); const vec p = { r * sinf(phi) * cosf(theta) , r * sinf(phi) * sinf(theta) @@ -221,7 +221,7 @@ vec eval_li(const t_eval_hit & hit, const light & ambient, const light const light & L = lights[i]; l = L.pos - hit.position; - const T & r = length(l); + const T r = length(l); l /= r; h = normalize(l + v); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 4bb3d8fd..e01c8120 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -129,11 +129,6 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source std::vector >, std::greater > > Q; - real_t dv, dp; - vertex vx; - - size_t black_i; - index_t c = 0; n_sorted = 0; for(index_t s: sources) @@ -151,7 +146,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source if(Q.empty()) break; - black_i = Q.top().second; + size_t black_i = Q.top().second; color[black_i] = BLACK; Q.pop(); @@ -168,7 +163,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source if(color[v] == RED) { - dv = dist[v]; + real_t dv = dist[v]; for(const index_t he: mesh->star(v)) { const uvec3 i = { mesh->halfedge(he_next(he)), @@ -176,7 +171,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source mesh->halfedge(he) }; - real_t d = update_step(mesh, old_dist, i); + real_t d = update_step(mesh, dist, i); if(d < dv) { diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 44492398..f7c44394 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -113,13 +113,9 @@ void embree::build_bvh(const std::vector & meshes, const std::vecto { g_meshes[i] = meshes[i]; - //if(!meshes[i]->n_trigs || pc.enable) - // g_meshes[i]->n_trigs = 0; - [[maybe_unused]] - const index_t geomID = g_meshes[i]->n_trigs || meshes[i]->is_scene() ? - add_mesh(meshes[i], model_mats[i]) : - add_pointcloud(meshes[i], model_mats[i], pc); + const index_t geomID = pc.enable ? add_pointcloud(meshes[i], model_mats[i], pc) + : add_mesh(meshes[i], model_mats[i]); gproshan_debug_var(i == geomID); } diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index 86d1f121..d006e0ee 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -74,22 +74,22 @@ void shader::uniform(const std::string & name, float value) void shader::uniform(const std::string & name, const vec3 & value) { - glProgramUniform3fv(program, glGetUniformLocation(program, name.c_str()), 1, &value[0]); + glProgramUniform3fv(program, glGetUniformLocation(program, name.c_str()), 1, (float *) &value); } void shader::uniform(const std::string & name, const vec4 & value) { - glProgramUniform4fv(program, glGetUniformLocation(program, name.c_str()), 1, &value[0]); + glProgramUniform4fv(program, glGetUniformLocation(program, name.c_str()), 1, (float *) &value); } void shader::uniform(const std::string & name, const mat3 & value) { - glProgramUniformMatrix3fv(program, glGetUniformLocation(program, name.c_str()), 1, true, &value[0][0]); + glProgramUniformMatrix3fv(program, glGetUniformLocation(program, name.c_str()), 1, true, (float *) &value); } void shader::uniform(const std::string & name, const mat4 & value) { - glProgramUniformMatrix4fv(program, glGetUniformLocation(program, name.c_str()), 1, true, &value[0][0]); + glProgramUniformMatrix4fv(program, glGetUniformLocation(program, name.c_str()), 1, true, (float *) &value); } bool shader::load(GLenum shader_type, const std::string & filename) From c3b269f8a44adaee917499fce88cd57b9d4d1c12 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 23:39:39 +0100 Subject: [PATCH 0970/1018] embree: render pointcloud hit param --- include/gproshan/raytracing/embree.h | 5 +++-- include/gproshan/raytracing/utils.h | 20 ++++++++++---------- src/gproshan/raytracing/embree.cpp | 13 +++++++++---- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index a84862cd..3b379585 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -24,6 +24,7 @@ class embree : public raytracing AREA, MEDIAN_PAIRS }; + struct pc_opts { bool enable = false; @@ -33,8 +34,7 @@ class embree : public raytracing float scale = 1; int knn = 8; - - pc_opts() {}; + pc_opts() {} }; protected: @@ -57,6 +57,7 @@ class embree : public raytracing RTCScene rtc_scene; std::vector g_meshes; + std::vector is_pointcloud; scene_data sc; public: diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 9d646686..a5cf9ae6 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -91,13 +91,13 @@ struct t_eval_hit t_eval_hit() {} __host_device__ - t_eval_hit(const che & mesh, const index_t aprimID, const T au, const T av, const scene_data & sc) + t_eval_hit(const che & mesh, const index_t aprimID, const T au, const T av, const scene_data & sc, const bool pointcloud = false) { primID = aprimID; u = au; v = av; - if(!mesh.n_trigs) // pointcloud + if(pointcloud || !mesh.n_trigs) // pointcloud { Kd = mesh.color(primID); normal = mesh.normal(primID); @@ -107,11 +107,9 @@ struct t_eval_hit const uvec3 trig = mesh.trig(primID); - const vec ca = mesh.color(trig.x()); - const vec cb = mesh.color(trig.y()); - const vec cc = mesh.color(trig.z()); - - Kd = (1.f - u - v) * ca + u * cb + v * cc; + Kd = (1.f - u - v) * mesh.color(trig.x()) + + u * mesh.color(trig.y()) + + v * mesh.color(trig.z()); normal = (1.f - u - v) * mesh.normal(trig.x()) + u * mesh.normal(trig.y()) + v * mesh.normal(trig.z()); @@ -119,6 +117,8 @@ struct t_eval_hit + u * mesh.heatmap(trig.y()) + v * mesh.heatmap(trig.z()); + normal = normalize(normal); + if(!sc.trig_mat) return; if(sc.trig_mat[primID] == NIL) return; @@ -209,7 +209,7 @@ template __host_device__ vec eval_li(const t_eval_hit & hit, const light & ambient, const light * lights, const int n_lights, const vec & eye, Occluded occluded) { - const vec & v = normalize(eye - hit.position); + const vec v = normalize(eye - hit.position); const vec & n = hit.normal; T lambertian; @@ -234,8 +234,8 @@ vec eval_li(const t_eval_hit & hit, const light & ambient, const light specular = powf(std::max(dot(h, n), 0.f), hit.Ns); #endif // __CUDACC__ - const vec & color = hit.Ka * ambient.color * ambient.power + - (lambertian * hit.Kd + specular * hit.Ks) * L.color * L.power / (r * r); + const vec color = hit.Ka * ambient.color * ambient.power + + (lambertian * hit.Kd + specular * hit.Ks) * L.color * L.power / (r * r); li += (dot(v, n) < 0 || occluded(hit.position, l, r) ? 0.4f : 1.0f) * color; } diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index f7c44394..07c39cb0 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -109,13 +109,18 @@ vec4 * embree::pc_data(const index_t geomID) void embree::build_bvh(const std::vector & meshes, const std::vector & model_mats, const pc_opts & pc) { g_meshes.resize(size(meshes)); + is_pointcloud.assign(size(meshes), 0); + for(index_t i = 0; i < size(meshes); ++i) { - g_meshes[i] = meshes[i]; + const che * mesh = meshes[i]; + + g_meshes[i] = mesh; + is_pointcloud[i] = pc.enable || !mesh->n_trigs; [[maybe_unused]] - const index_t geomID = pc.enable ? add_pointcloud(meshes[i], model_mats[i], pc) - : add_mesh(meshes[i], model_mats[i]); + const index_t geomID = is_pointcloud[i] ? add_pointcloud(meshes[i], model_mats[i], pc) + : add_mesh(meshes[i], model_mats[i]); gproshan_debug_var(i == geomID); } @@ -279,7 +284,7 @@ bool embree::closesthit_radiance( vertex & color, const che & mesh = *g_meshes[r.hit.geomID]; - eval_hit hit(mesh, r.hit.primID, r.hit.u, r.hit.v, sc); + eval_hit hit(mesh, r.hit.primID, r.hit.u, r.hit.v, sc, is_pointcloud[r.hit.geomID]); hit.position = r.pos(); hit.normal = flat ? r.normal() : hit.normal; From 93c3c8dc2b753600296a621d22b1c8ca274216bb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 21 Mar 2024 23:55:10 +0100 Subject: [PATCH 0971/1018] quaternion: peding upgrade, removed toMatrix --- include/gproshan/mesh/quaternion.h | 1 - src/gproshan/mesh/che.cpp | 2 +- src/gproshan/mesh/quaternion.cpp | 8 -------- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 7b87b678..35b043a9 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -29,7 +29,6 @@ class quaternion const quaternion & operator = (const vertex & v); real_t & operator [] (int index); real_t operator [] (int index) const; - void toMatrix(real_t Q[4][4]) const; real_t & re(void); real_t re(void) const; vertex & im(void); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index b1b8f2f3..34101f9b 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -1060,7 +1060,7 @@ void che::update_evt_ot_et() if(OT[he] == NIL) { - ET[ne++] = he; + if(ET) ET[ne++] = he; index_t ohe = NIL; for(index_t j = 0; j < vnhe[v]; ++j) diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index e5accf4d..d4f54072 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -46,14 +46,6 @@ real_t quaternion::operator [] (int index) const return v[index]; } -void quaternion::toMatrix(real_t Q[4][4]) const // FIX -{ - Q[0][0] = s; Q[0][1] = -v.x(); Q[0][2] = -v.y(); Q[0][3] = -v.z(); - Q[1][0] = v.x(); Q[1][1] = s; Q[1][2] = -v.z(); Q[1][3] = v.y(); - Q[2][0] = v.y(); Q[2][1] = v.z(); Q[2][2] = s; Q[2][3] = -v.x(); - Q[3][0] = v.z(); Q[3][1] = -v.y(); Q[3][2] = v.x(); Q[3][3] = s; -} - real_t & quaternion::re() { return s; From 33f15cbd9f14361eb0f973148b9359f4ffc023dd Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Mar 2024 12:51:56 +0100 Subject: [PATCH 0972/1018] rt: update shading, utils.h --- include/gproshan/geometry/mat.h | 3 +- include/gproshan/mesh/che.h | 44 +++++++++++++++++++++++++++-- include/gproshan/raytracing/optix.h | 4 +-- include/gproshan/raytracing/utils.h | 15 +++------- src/gproshan/raytracing/optix.cpp | 4 +-- 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index a4b0d170..9010dbc2 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -92,7 +92,7 @@ class mat } }; -///< std std::ostream + template std::ostream & operator << (std::ostream & os, const mat & m) { @@ -101,7 +101,6 @@ std::ostream & operator << (std::ostream & os, const mat & m) return os; } -///< std std::istream template std::istream & operator >> (std::istream & is, mat & m) { diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 6f760905..b674244e 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -189,6 +189,41 @@ class che return {VT[he], VT[he + 1], VT[he + 2]}; } + __host_device__ + mat3 trig_points(const index_t t) const + { + assert(t < n_trigs); + const index_t he = t * 3; + mat3 m = {GT[VT[he]], GT[VT[he + 1]], GT[VT[he + 2]]}; + return mat3::transpose(m); + } + + __host_device__ + mat3 trig_normals(const index_t t) const + { + assert(t < n_trigs); + const index_t he = t * 3; + mat3 m = {VN[VT[he]], VN[VT[he + 1]], VN[VT[he + 2]]}; + return mat3::transpose(m); + } + + __host_device__ + mat3 trig_colors(const index_t t) const + { + assert(t < n_trigs); + const index_t he = t * 3; + mat3 m = {VC[VT[he]], VC[VT[he + 1]], VC[VT[he + 2]]}; + return mat3::transpose(m); + } + + __host_device__ + vec3 trig_heatmap(const index_t t) const + { + assert(t < n_trigs); + const index_t he = t * 3; + return {VHC[VT[he]], VHC[VT[he + 1]], VHC[VT[he + 2]]}; + } + __host_device__ index_t halfedge(const index_t he) const { @@ -214,8 +249,13 @@ class che return VHC; } - - vertex normal_trig(const index_t f) const; +/* + __host_device__ + T shading(const real_t u, const real_t v) + { + } +*/ + vertex normal_trig(const index_t t) const; vertex normal_he(const index_t he) const; vertex gradient_he(const index_t he, const real_t * f) const; dvec3 gradient_he(const index_t he, const double * f) const; diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index 683b2d0e..e60dfd58 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -53,7 +53,7 @@ class optix : public raytracing public: optix(const std::string & ptx = "/src/optix.ptx"); - optix(const std::vector & meshes, const std::vector & model_mats); + optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); void render(vec4 * img, const render_params & params, const bool flat); @@ -64,7 +64,7 @@ class optix : public raytracing void create_hitgroup_programs(); void create_pipeline(); void build_sbt(); - OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); + OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat); }; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index a5cf9ae6..03718c4b 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -106,18 +106,11 @@ struct t_eval_hit } const uvec3 trig = mesh.trig(primID); + const vec3 uv = {1.f - u - v, u, v}; - Kd = (1.f - u - v) * mesh.color(trig.x()) - + u * mesh.color(trig.y()) - + v * mesh.color(trig.z()); - normal = (1.f - u - v) * mesh.normal(trig.x()) - + u * mesh.normal(trig.y()) - + v * mesh.normal(trig.z()); - heatmap = (1.f - u - v) * mesh.heatmap(trig.x()) - + u * mesh.heatmap(trig.y()) - + v * mesh.heatmap(trig.z()); - - normal = normalize(normal); + Kd = mesh.trig_colors(primID) * uv; + normal = normalize(mesh.trig_normals(primID) * uv); + heatmap = dot(mesh.trig_heatmap(primID), uv); if(!sc.trig_mat) return; if(sc.trig_mat[primID] == NIL) return; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index a68b2c92..cd4b27e6 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -94,7 +94,7 @@ optix::optix(const std::string & ptx) cudaMalloc(&optix_params_buffer, sizeof(launch_params)); } -optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() +optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() { // build as optix_params.traversable = build_as(meshes, model_mats); @@ -333,7 +333,7 @@ void optix::build_sbt() sbt.hitgroupRecordCount = size(hitgroup_records); } -OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) +OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) { OptixTraversableHandle optix_as_handle = {}; From 529a360ef2f61208d96c228a5a729fc68123281f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Mar 2024 15:39:54 +0100 Subject: [PATCH 0973/1018] descriptor: WKS, HKS and GPS with double precision needed specially for WKS even on small meshes --- include/gproshan/features/descriptor.h | 11 ++++++----- src/gproshan/features/descriptor.cpp | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index 28a98683..6cbd7269 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -2,7 +2,8 @@ #define DESCRIPTOR_H #include -#include + +#include // geometry processing and shape analysis framework @@ -15,10 +16,10 @@ class descriptor enum signature { GPS, HKS, WKS }; private: - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; - a_mat features; + arma::sp_mat L, A; + arma::vec eigval; + arma::mat eigvec; + arma::mat features; public: descriptor(const signature & sig, const che * mesh, const size_t n_eigs); diff --git a/src/gproshan/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp index b396966e..eb63466a 100644 --- a/src/gproshan/features/descriptor.cpp +++ b/src/gproshan/features/descriptor.cpp @@ -59,7 +59,7 @@ void descriptor::compute_wks() eigvec = eigvec % eigvec; // element wise product eigval = log(eigval); - a_vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); + arma::vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); real_t sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference real_t sigma_2 = 2 * sigma * sigma; From 01ddf64503b85c6305b7c63ef7628ee1a56339eb Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Fri, 22 Mar 2024 21:37:00 +0100 Subject: [PATCH 0974/1018] Update build.yml --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 543e5631..2aef72d5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,6 +33,7 @@ jobs: libglew-dev \ libglfw3-dev \ cimg-dev \ + g++-12 \ wget wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann-dev_1.9.2+dfsg-1_amd64.deb wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann1.9_1.9.2+dfsg-1_amd64.deb @@ -59,7 +60,7 @@ jobs: sudo apt install intel-renderkit - name: Create Build Environment - run: cmake -E make_directory ${{runner.workspace}}/build + run: CXX=g++-12 CC=gcc-12 cmake -E make_directory ${{runner.workspace}}/build - name: Configure CMake shell: bash From d85eb90cd11553c0a8217a46b0606699dee12262 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Fri, 22 Mar 2024 21:45:11 +0100 Subject: [PATCH 0975/1018] Update build.yml --- .github/workflows/build.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2aef72d5..32b3e0a9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,6 +4,8 @@ on: [push] env: CUDA_BIN: /usr/local/cuda/bin + CC: gcc-12 + CXX: g++-12 jobs: build: @@ -60,7 +62,7 @@ jobs: sudo apt install intel-renderkit - name: Create Build Environment - run: CXX=g++-12 CC=gcc-12 cmake -E make_directory ${{runner.workspace}}/build + run: cmake -E make_directory ${{runner.workspace}}/build - name: Configure CMake shell: bash From e7a8e0fc91441558cc282ce9cd5d5235c0cb687e Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Fri, 22 Mar 2024 22:00:59 +0100 Subject: [PATCH 0976/1018] Update README.md --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0ecce16..b69c10bf 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,16 @@ finally execute: ./gproshan [mesh_paths.(off,obj,ply)] -### Dependencies (Linux) -g++ >= 9.4, cuda >= 11.8, cmake >= 3.24, armadillo, eigen3, cgal, suitesparse, openblas, glew, glfw3, cimg, gnuplot, embree >= 3.13 -In Ubuntu you can install them with: +### Dependencies (Linux/MacOS) +g++ >= 12.3, cuda >= 12.4, cmake >= 3.28, embree >= 4.3, glew, glfw3, armadillo, suitesparse, openblas, cimg + +On Ubuntu you can install them with: + + sudo apt install cmake libglew-dev libglfw3-dev libarmadillo-dev libsuitesparse-dev libopenblas-dev cimg-dev + +Install Cuda if available to enable Cuda-based modules. - sudo apt install cmake libarmadillo-dev libeigen3-dev libcgal-dev libsuitesparse-dev libopenblas-dev libglew-dev libglfw3-dev cimg-dev gnuplot #### Installing Intel Embree From 33ae69872eafefbda03b2e15ee13338b485ae0b2 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Fri, 22 Mar 2024 22:06:37 +0100 Subject: [PATCH 0977/1018] Update build.yml --- .github/workflows/build.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 32b3e0a9..6d06c6a6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,6 +6,7 @@ env: CUDA_BIN: /usr/local/cuda/bin CC: gcc-12 CXX: g++-12 + CUDAHOSTCXX=g++-12 jobs: build: From 07d63a22a826f578614fe34343b36d0377dcad8b Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Fri, 22 Mar 2024 22:07:10 +0100 Subject: [PATCH 0978/1018] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d06c6a6..ae97f100 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -6,7 +6,7 @@ env: CUDA_BIN: /usr/local/cuda/bin CC: gcc-12 CXX: g++-12 - CUDAHOSTCXX=g++-12 + CUDAHOSTCXX: g++-12 jobs: build: From 0ad97c79fa60b4a1dd3253b0de5c91c87bd188eb Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 22 Mar 2024 23:01:42 +0100 Subject: [PATCH 0979/1018] features: WKS update, fix max eigval --- src/gproshan/features/descriptor.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gproshan/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp index eb63466a..2e55c76d 100644 --- a/src/gproshan/features/descriptor.cpp +++ b/src/gproshan/features/descriptor.cpp @@ -57,11 +57,11 @@ void descriptor::compute_hks() void descriptor::compute_wks() { eigvec = eigvec % eigvec; // element wise product - eigval = log(eigval); + double max_ev = log(std::max(max(abs(eigval)), 1e-6)); - arma::vec e = arma::linspace(eigval(1), eigval(eigval.n_elem - 1), eigval.n_elem); - real_t sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference - real_t sigma_2 = 2 * sigma * sigma; + arma::vec e = arma::linspace(eigval(1), max_ev, eigval.n_elem); + double sigma = (e(1) - e(0)) * 6; // 6 is wks variance see reference + double sigma_2 = 2 * sigma * sigma; features.zeros(eigvec.n_rows, e.n_elem); From 9a95a18a961c16ad89adbd56f4c12c8d910f3cb5 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 23 Mar 2024 16:41:38 +0100 Subject: [PATCH 0980/1018] gproshan: compiling with AppleClang 15.0 OpenMP 5.0 Armadillo 12.8 --- include/gproshan/util.h | 2 +- src/gproshan/scenes/scanner.cpp | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 5f2cae8e..b6d420cf 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -88,7 +88,7 @@ T normalize(T * data, const size_t n_elem) { T max = 0; - #pragma omp parallel for reduction(std::max: max) + #pragma omp parallel for reduction(max: max) for(index_t i = 0; i < n_elem; ++i) max = std::max(max, data[i]); diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index efa6f5a2..b9a7aa1b 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -3,14 +3,7 @@ #include #include -#ifndef __clang__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-truncation" - #include -#pragma GCC diagnostic pop -#else - #include -#endif // __clang__ +#include using namespace cimg_library; @@ -46,7 +39,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n real_t max_dist = 0; - #pragma omp parallel for reduction(std::max: max_dist) + #pragma omp parallel for reduction(max: max_dist) for(index_t v = 0; v < mesh_ptx->n_vertices; ++v) max_dist = std::max(max_dist, mesh_ptx->heatmap(v)); From 471d461a5dad936ffaf416fbaecf2100f4c919c3 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 23 Mar 2024 17:04:08 +0100 Subject: [PATCH 0981/1018] cimg: warning null destination pointer snprintf --- src/gproshan/scenes/scanner.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index b9a7aa1b..05423ef8 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -50,15 +50,14 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n return mesh_ptx; } -che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg) +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string &) { che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); CImg img((unsigned char *) &mesh_ptx->rgb(0), 3, n_cols, n_rows); img.permute_axes("zycx"); - std::string img_filename = file_jpg + ".jpg"; - img.save(img_filename.c_str()); +// if(size(file_jpg)) img.save((file_jpg + ".png").c_str()); std::thread([](const CImg & img) { img.display(); }, img).detach(); From f3fa1f9b07b002c3592743b028e018c6e5d2589a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 23 Mar 2024 17:11:02 +0100 Subject: [PATCH 0982/1018] cimg: warning null destination pointer snprintf on linux g++ 13.2, clang no warning --- src/gproshan/scenes/scanner.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 05423ef8..05b96f89 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -3,7 +3,14 @@ #include #include -#include +#ifndef __clang__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-truncation" + #include +#pragma GCC diagnostic pop +#else + #include +#endif // __clang__ using namespace cimg_library; @@ -50,14 +57,14 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n return mesh_ptx; } -che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string &) +che * scanner_ptx_jpg(const rt::raytracing * rt, const size_t n_rows, const size_t n_cols, const vertex & cam_pos, const std::string & file_jpg) { che * mesh_ptx = scanner_ptx(rt, n_rows, n_cols, cam_pos); CImg img((unsigned char *) &mesh_ptx->rgb(0), 3, n_cols, n_rows); img.permute_axes("zycx"); -// if(size(file_jpg)) img.save((file_jpg + ".png").c_str()); + img.save((file_jpg + "_scan.jpg").c_str()); std::thread([](const CImg & img) { img.display(); }, img).detach(); From 5d3ac2b9a86aab746a83f793208a38dde2d6213c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Mar 2024 00:47:56 +0100 Subject: [PATCH 0983/1018] cleaning up include_arma.h mdic, fill hole, poisson, simplication needs review --- include/gproshan/geometry/mat.h | 47 +++++++++++--- include/gproshan/include_arma.h | 34 ---------- include/gproshan/mdict/basis.h | 13 ++-- include/gproshan/mdict/basis_cosine.h | 6 +- include/gproshan/mdict/basis_dct.h | 10 +-- include/gproshan/mdict/mdict.h | 33 +++++----- include/gproshan/mdict/msparse_coding.h | 8 +-- include/gproshan/mdict/patch.h | 15 ++--- include/gproshan/mesh/che.h | 6 +- include/gproshan/mesh/che_fill_hole.h | 34 +++++----- include/gproshan/mesh/simplification.h | 4 +- src/gproshan/app_viewer.cpp | 10 +-- src/gproshan/geometry/mat.cpp | 25 ------- src/gproshan/mdict/basis.cpp | 6 +- src/gproshan/mdict/basis_cosine.cpp | 6 +- src/gproshan/mdict/basis_dct.cpp | 10 +-- src/gproshan/mdict/image_denoising.cpp | 10 +-- src/gproshan/mdict/mdict.cpp | 86 ++++++++++++------------- src/gproshan/mdict/msparse_coding.cpp | 36 +++++------ src/gproshan/mdict/patch.cpp | 26 ++++---- src/gproshan/mesh/che.cpp | 2 +- src/gproshan/mesh/che_fill_hole.cpp | 48 +++++++------- src/gproshan/mesh/che_poisson.cpp | 29 +++++---- src/gproshan/mesh/simplification.cpp | 6 +- 24 files changed, 237 insertions(+), 273 deletions(-) delete mode 100644 include/gproshan/include_arma.h delete mode 100644 src/gproshan/geometry/mat.cpp diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 9010dbc2..418d9f03 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -3,6 +3,10 @@ #include +#ifndef __CUDACC__ + #include +#endif // __CUDACC__ + // geometry processing and shape analysis framework namespace gproshan { @@ -73,26 +77,34 @@ class mat } __host_device__ - static mat identity() + mat t() const { - mat res; - for(index_t i = 0; i < N; ++i) - res[i][i] = 1; - return res; + return transpose(*this); } __host_device__ - static mat transpose(const mat & m) + static mat identity() { mat res; for(index_t i = 0; i < N; ++i) - for(index_t j = 0; j < N; ++j) - res[i][j] = m[j][i]; + res[i][i] = 1; return res; } }; +template +__host_device__ +mat transpose(const mat & m) +{ + mat res; + for(index_t i = 0; i < N; ++i) + for(index_t j = 0; j < N; ++j) + res[i][j] = m[j][i]; + return res; +} + + template std::ostream & operator << (std::ostream & os, const mat & m) { @@ -110,12 +122,27 @@ std::istream & operator >> (std::istream & is, mat & m) } +#ifndef __CUDACC__ +template +mat inverse(const mat & m) +{ + mat inv; + mat mt = m.t(); + + arma::Mat ainv((T *) &inv, N, N, false, true); + arma::Mat amt((T *) &mt, N, N, false, true); + + arma::inv(ainv, amt); + + return inv.t(); +} +#endif // __CUDACC__ + + using mat2 = mat; using mat3 = mat; using mat4 = mat; -mat4 inverse(const mat4 & m); - } // namespace gproshan diff --git a/include/gproshan/include_arma.h b/include/gproshan/include_arma.h deleted file mode 100644 index a1ab9303..00000000 --- a/include/gproshan/include_arma.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef INCLUDE_ARMA_H -#define INCLUDE_ARMA_H - -#include - -#define ARMA_ALLOW_FAKE_GCC -#include - -#ifdef NDEBUG - #define ARMA_NO_DEBUG -#endif - - -// geometry processing and shape analysis framework -namespace gproshan { - - -#ifdef GPROSHAN_FLOAT - typedef arma::fmat a_mat; - typedef arma::fvec a_vec; - typedef arma::frowvec a_rowvec; - typedef arma::sp_fmat a_sp_mat; -#else - typedef arma::mat a_mat; - typedef arma::vec a_vec; - typedef arma::rowvec a_rowvec; - typedef arma::sp_mat a_sp_mat; -#endif - - -} // namespace gproshan - -#endif // INCLUDE_ARMA_H - diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 4031f714..066f57bb 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -2,10 +2,11 @@ #define BASIS_H #include -#include #include +#include + // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -21,18 +22,18 @@ class basis public: basis(const real_t r, const size_t d); virtual ~basis() = default; - virtual void discrete(a_mat & phi, const a_vec & x, const a_vec & y) = 0; - virtual void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b) = 0; + virtual void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) = 0; + virtual void d_discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y, const bool b) = 0; virtual real_t freq(const index_t idx) = 0; real_t & radio(); size_t dim() const; void plot_basis(); - void plot_atoms(const a_mat & A); - void plot_patch(const a_mat & A, const a_mat & xyz, const index_t p); + void plot_atoms(const arma::fmat & A); + void plot_patch(const arma::fmat & A, const arma::fmat & xyz, const index_t p); private: virtual void plot_basis(std::ostream & os) = 0; - virtual void plot_atoms(std::ostream & os, const a_vec & A) = 0; + virtual void plot_atoms(std::ostream & os, const arma::fvec & A) = 0; }; diff --git a/include/gproshan/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h index 45ea49e7..1dc12813 100644 --- a/include/gproshan/mdict/basis_cosine.h +++ b/include/gproshan/mdict/basis_cosine.h @@ -17,12 +17,12 @@ class basis_cosine: public basis public: basis_cosine(const size_t nr, const size_t nf, const real_t r = 0); - void discrete(a_mat & phi, const a_vec & x, const a_vec & y); + void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y); private: void plot_basis(std::ostream & os); - void plot_atoms(std::ostream & os, const a_vec & A); - a_vec cosine(const a_vec & x, const a_vec & y, const real_t c, const real_t alpha); + void plot_atoms(std::ostream & os, const arma::fvec & A); + arma::fvec cosine(const arma::fvec & x, const arma::fvec & y, const real_t c, const real_t alpha); void cosine(std::ostream & os, const real_t c, const real_t alpha); }; diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 1c08b12c..1abfefd8 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -16,15 +16,15 @@ class basis_dct: public basis public: basis_dct(const size_t n, const real_t r = 1); - void discrete(a_mat & phi, const a_vec & x, const a_vec & y); - void d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b); + void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y); + void d_discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y, const bool b); real_t freq(const index_t idx); private: void plot_basis(std::ostream & os); - void plot_atoms(std::ostream & os, const a_vec & A); - a_vec dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny); - a_vec d_dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny); + void plot_atoms(std::ostream & os, const arma::fvec & A); + arma::fvec dct(const arma::fvec & x, const arma::fvec & y, const index_t nx, const index_t ny); + arma::fvec d_dct(const arma::fvec & x, const arma::fvec & y, const index_t nx, const index_t ny); void dct(std::ostream & os, const index_t nx, const index_t ny); }; diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index 91a3ff09..ecd40a6d 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -4,7 +4,6 @@ #include #include #include -#include // geometry processing and shape analysis framework @@ -21,44 +20,44 @@ struct locval_t }; -void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t L); +void OMP(std::vector & alpha, const arma::fvec & x, const index_t i, const arma::fmat & D, const size_t L); -a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t L); +arma::sp_fmat OMP_all(std::vector & locval, const arma::fmat & X, const arma::fmat & D, const size_t L); -void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k); +void sp_KSVD(arma::fmat & D, const arma::fmat & X, const size_t L, size_t k); // DENSE -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L); +std::tuple _OMP(const arma::fvec & x, const arma::fmat & D, const size_t L); -a_vec OMP(const a_vec & x, const a_mat & D, const size_t L); -a_vec OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask); +arma::fvec OMP(const arma::fvec & x, const arma::fmat & D, const size_t L); +arma::fvec OMP(const arma::fvec & x, const arma::fmat & D, const size_t L, const arma::uchar_vec & mask); -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t L); +arma::fmat OMP_all(const arma::fmat & X, const arma::fmat & D, const size_t L); -void KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k); +void KSVD(arma::fmat & D, const arma::fmat & X, const size_t L, size_t k); // MESH DENSE -a_vec OMP(const patch & p, const a_mat & A, const size_t L); -a_vec OMP(const patch & p, const a_mat & A, const size_t L); +arma::fvec OMP(const patch & p, const arma::fmat & A, const size_t L); +arma::fvec OMP(const patch & p, const arma::fmat & A, const size_t L); -a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t L); -a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t L); +arma::fmat OMP_all(const std::vector & patches, const arma::fmat & A, const size_t L); +arma::fmat OMP_all(const std::vector & patches, basis * phi_basis, const arma::fmat & A, const size_t L); -void KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k); +void KSVD(arma::fmat & A, const std::vector & patches, const size_t L, size_t k); // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t L); +void OMP(std::vector & alpha, const patch & p, const index_t i, const arma::fmat & A, const size_t L); -a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t L); +arma::sp_fmat OMP_all(std::vector & locval, const std::vector & patches, const arma::fmat & A, const size_t L); -void sp_KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k); +void sp_KSVD(arma::fmat & A, const std::vector & patches, const size_t L, size_t k); } // namespace gproshan::mdict diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index 2e98c936..614850f3 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -6,8 +6,6 @@ #include #include -#include - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace @@ -38,8 +36,8 @@ class msparse_coding basis * phi_basis; ///< continuous basis. params m_params; ///< - a_mat A; ///< dictionary continuous matrix. - a_mat alpha; ///< sparse coding matrix. + arma::fmat A; ///< dictionary continuous matrix. + arma::fmat alpha; ///< sparse coding matrix. real_t s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. @@ -93,7 +91,7 @@ class msparse_coding ); real_t mesh_reconstruction(const fmask_t & mask = nullptr); - void update_alphas(a_mat & alpha, size_t threshold); + void update_alphas(arma::fmat & alpha, size_t threshold); void save_alpha(std::string file); index_t sample(const index_t s); diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index cfb51b1f..c5833c5b 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -9,9 +9,6 @@ #include -#include - - // geometry processing and shape analysis framework // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { @@ -27,10 +24,10 @@ class patch { public: std::vector vertices; ///< Vertices of the patch. - a_mat T; ///< Transformation matrix. - a_vec x; ///< Center point. - a_mat xyz; ///< Matrix of points. - a_mat phi; ///< Projected basis. + arma::fmat T; ///< Transformation matrix. + arma::fvec x; ///< Center point. + arma::fmat xyz; ///< Matrix of points. + arma::fmat phi; ///< Projected basis. double avg_dist; ///< Average distance between points. real_t radio; ///< Radio. size_t min_nv; ///< @@ -66,7 +63,7 @@ class patch const real_t area_mesh ); - void init_random(const vertex & c, const a_mat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr); + void init_random(const vertex & c, const arma::fmat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr); void recover_radial_disjoint(che * mesh, const real_t radio_, @@ -90,7 +87,7 @@ class patch ); void remove_extra_xyz_disjoint(size_t & max_points); void add_extra_xyz_disjoint(che * mesh, std::vector & vpatches, const index_t p); - const a_vec normal(); + const arma::fvec normal(); bool is_covered( bool * covered); // void save(const real_t radio, const size_t imsize, CImgList & imlist); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index b674244e..0f014ca1 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -195,7 +195,7 @@ class che assert(t < n_trigs); const index_t he = t * 3; mat3 m = {GT[VT[he]], GT[VT[he + 1]], GT[VT[he + 2]]}; - return mat3::transpose(m); + return m.t(); } __host_device__ @@ -204,7 +204,7 @@ class che assert(t < n_trigs); const index_t he = t * 3; mat3 m = {VN[VT[he]], VN[VT[he + 1]], VN[VT[he + 2]]}; - return mat3::transpose(m); + return m.t(); } __host_device__ @@ -213,7 +213,7 @@ class che assert(t < n_trigs); const index_t he = t * 3; mat3 m = {VC[VT[he]], VC[VT[he + 1]], VC[VT[he + 2]]}; - return mat3::transpose(m); + return m.t(); } __host_device__ diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 8c9063ee..76866dd2 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -2,9 +2,9 @@ #define CHE_FILL_HOLE_H #include -#include #include +#include // geometry processing and shape analysis framework @@ -18,7 +18,7 @@ struct border_t border_t() = default; - border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o): v(_v) { index_t p_v = neighbors[!o]; @@ -30,25 +30,25 @@ struct border_t return; } - a_vec a = V[p_v] - V[v]; - a_vec b = V[n_v] - V[v]; + arma::fvec a = V[p_v] - V[v]; + arma::fvec b = V[n_v] - V[v]; a[2] = b[2] = 0; theta = atan2(b[1], b[0]) - atan2(a[1], a[0]); if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o) + arma::fvec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; - a_vec a = V[p_v] - V[v]; - a_vec b = V[n_v] - V[v]; + arma::fvec a = V[p_v] - V[v]; + arma::fvec b = V[n_v] - V[v]; a(2) = b(2) = 0; - a_vec r = div * a + (1 - div) * b; + arma::fvec r = div * a + (1 - div) * b; r = length * normalise(r) + V[v]; r(2) = 0; @@ -56,19 +56,19 @@ struct border_t return r; } - border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o, const a_vec & normal): + border_t(const std::vector & V, const index_t _v, const std::array & neighbors, const bool o, const arma::fvec & normal): v(_v) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; - a_vec a = V[p_v] - V[v]; - a_vec b = V[n_v] - V[v]; + arma::fvec a = V[p_v] - V[v]; + arma::fvec b = V[n_v] - V[v]; a -= dot(a, normal) * normal; b -= dot(a, normal) * normal; - a_mat E(3,3); + arma::fmat E(3,3); E.col(0) = normalise(a); E.col(1) = normalise(cross(normal, a)); E.col(2) = normal; @@ -81,18 +81,18 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - a_vec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o, const a_vec & normal) + arma::fvec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o, const arma::fvec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; - a_vec a = V[p_v] - V[v]; - a_vec b = V[n_v] - V[v]; + arma::fvec a = V[p_v] - V[v]; + arma::fvec b = V[n_v] - V[v]; a -= dot(a, normal) * normal; b -= dot(a, normal) * normal; - a_mat E(3,3); + arma::fmat E(3,3); E.col(0) = normalise(a); E.col(1) = normalise(cross(normal, a)); E.col(2) = normal; @@ -100,7 +100,7 @@ struct border_t a = E.t() * a; b = E.t() * b; - a_vec r(3); + arma::fvec r(3); r[0] = cos(theta * div); r[1] = sin(theta * div); r[2] = 0; diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 855ef509..972d70fd 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -2,9 +2,9 @@ #define SIMPLIFICATION_H #include -#include #include +#include // geometry processing and shape analysis framework @@ -14,7 +14,7 @@ namespace gproshan { class simplification { private: - a_mat * Q; + arma::fmat * Q; che * mesh; index_t levels; diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index 76001140..b9dbd5b7 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -348,7 +348,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) real_t g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; - a_vec gv(mesh->n_vertices); + arma::fvec gv(mesh->n_vertices); #pragma omp parallel for private(g, a, b) reduction(max: g_max) reduction(min: g_min) for(index_t v = 0; v < mesh->n_vertices; ++v) @@ -747,7 +747,7 @@ bool app_viewer::process_mask(viewer * p_view) mesh->update_heatmap(msc); std::string f_points = tmp_file_path(std::string(msc) + ".rsampl"); - a_vec points_out; + arma::fvec points_out; gproshan_debug_var(f_points); points_out.load(f_points); gproshan_debug_var(size(points_out)); @@ -807,9 +807,9 @@ bool app_viewer::process_eigenfuntions(viewer * p_view) if(ImGui::Button("Run")) { - a_sp_mat L, A; - a_vec eigval; - a_mat eigvec; + arma::sp_fmat L, A; + arma::fvec eigval; + arma::fmat eigvec; TIC(view->time) n_eigs = eigs_laplacian(mesh, eigval, eigvec, L, A, n_eigs); diff --git a/src/gproshan/geometry/mat.cpp b/src/gproshan/geometry/mat.cpp deleted file mode 100644 index 25287f2a..00000000 --- a/src/gproshan/geometry/mat.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -mat4 inverse(const mat4 & m) -{ - mat4 inv; - mat4 mt = mat4::transpose(m); - - a_mat a_inv((real_t *) &inv, 4, 4, false, true); - a_mat a_m((real_t *) &mt, 4, 4, false, true); - - arma::inv(a_inv, a_m); - - return mat4::transpose(inv); -} - - -} // namespace gproshan - diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index e57da9e2..ee5925e1 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -44,7 +44,7 @@ void basis::plot_basis() system(file.c_str()); } -void basis::plot_atoms(const a_mat & A) +void basis::plot_atoms(const arma::fmat & A) { size_t K = A.n_rows; size_t m = A.n_cols; @@ -81,9 +81,9 @@ void basis::plot_atoms(const a_mat & A) system(file.c_str()); } -void basis::plot_patch(const a_mat & A, const a_mat & xyz, const index_t p) +void basis::plot_patch(const arma::fmat & A, const arma::fmat & xyz, const index_t p) { - a_mat tmp = xyz.t(); + arma::fmat tmp = xyz.t(); std::string data = tmp_file_path("xyz_" + std::to_string(p) + ".dat"); tmp.save(data.c_str(), arma::arma_ascii); diff --git a/src/gproshan/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp index a09faedf..5eb1c44b 100644 --- a/src/gproshan/mdict/basis_cosine.cpp +++ b/src/gproshan/mdict/basis_cosine.cpp @@ -10,7 +10,7 @@ namespace gproshan::mdict { basis_cosine::basis_cosine(const size_t nr, const size_t nf, const real_t r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} -void basis_cosine::discrete(a_mat & phi, const a_vec & x, const a_vec & y) +void basis_cosine::discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) { assert(phi.n_cols == _dim); @@ -40,7 +40,7 @@ void basis_cosine::plot_basis(std::ostream & os) } } -void basis_cosine::plot_atoms(std::ostream & os, const a_vec & A) +void basis_cosine::plot_atoms(std::ostream & os, const arma::fvec & A) { real_t d = 1.0 / (n_rot - 1); real_t c; @@ -53,7 +53,7 @@ void basis_cosine::plot_atoms(std::ostream & os, const a_vec & A) } } -a_vec basis_cosine::cosine(const a_vec & x, const a_vec & y, const real_t c, const real_t alpha) +arma::fvec basis_cosine::cosine(const arma::fvec & x, const arma::fvec & y, const real_t c, const real_t alpha) { return cos(c * (alpha * x + (1 - alpha) * y)); } diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 6bcf8d42..97cfd13d 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -10,7 +10,7 @@ namespace gproshan::mdict { basis_dct::basis_dct(const size_t n, const real_t r): basis(r, n * n), n_freq(n) {} -void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) +void basis_dct::discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) { assert(phi.n_cols == _dim); @@ -19,7 +19,7 @@ void basis_dct::discrete(a_mat & phi, const a_vec & x, const a_vec & y) phi.col(k) = dct(x, y, nx, ny); } -void basis_dct::d_discrete(a_mat & phi, const a_vec & x, const a_vec & y, const bool b) +void basis_dct::d_discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y, const bool b) { assert(phi.n_cols == _dim); @@ -39,7 +39,7 @@ void basis_dct::plot_basis(std::ostream & os) } } -void basis_dct::plot_atoms(std::ostream & os, const a_vec & A) +void basis_dct::plot_atoms(std::ostream & os, const arma::fvec & A) { for(index_t k = 0, nx = 0; nx < n_freq; ++nx) for(index_t ny = 0; ny < n_freq; ++ny, ++k) @@ -48,13 +48,13 @@ void basis_dct::plot_atoms(std::ostream & os, const a_vec & A) } } -a_vec basis_dct::dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny) +arma::fvec basis_dct::dct(const arma::fvec & x, const arma::fvec & y, const index_t nx, const index_t ny) { return cos(M_PI * nx * x / _radio ) % cos(M_PI * ny * y / _radio); } -a_vec basis_dct::d_dct(const a_vec & x, const a_vec & y, const index_t nx, const index_t ny) +arma::fvec basis_dct::d_dct(const arma::fvec & x, const arma::fvec & y, const index_t nx, const index_t ny) { return - (M_PI * nx / _radio) * (sin(M_PI * nx * x / _radio) % cos(M_PI * ny * y / _radio)); } diff --git a/src/gproshan/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp index 3df9215f..c6a8c62e 100644 --- a/src/gproshan/mdict/image_denoising.cpp +++ b/src/gproshan/mdict/image_denoising.cpp @@ -32,7 +32,7 @@ void test_image_denoising(const std::string & file) size_t L = 10; // sparsity OMP norm L_0 size_t K = 10; // KSVD iterations - a_mat X(n, M); + arma::fmat X(n, M); for(index_t x = 0; x < rows; ++x) for(index_t y = 0; y < cols; ++y) @@ -45,10 +45,10 @@ void test_image_denoising(const std::string & file) X(k++, i) = image(a, b); } - a_mat D(n, m, arma::fill::randu); + arma::fmat D(n, m, arma::fill::randu); D = normalise(D); - a_mat spD = D; + arma::fmat spD = D; CImg imdict; for(index_t i = 0; i < 16; ++i) @@ -94,14 +94,14 @@ void test_image_denoising(const std::string & file) gproshan_log(OMP); TIC(time) - a_mat Y = D * OMP_all(X, D, L); + arma::fmat Y = D * OMP_all(X, D, L); TOC(time) gproshan_log_var(time); TIC(time) std::vector locval; - a_mat spY = D * OMP_all(locval, X, D, L); + arma::fmat spY = D * OMP_all(locval, X, D, L); TOC(time) gproshan_log_var(time); diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index f1ba6551..826238d3 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -22,7 +22,7 @@ std::ostream & operator << (std::ostream & os, const locval_t & lc) return os << '(' << lc.i << ',' << lc.j << ") = " << lc.val; } -void OMP(std::vector & alpha, const a_vec & x, const index_t i, const a_mat & D, const size_t L) +void OMP(std::vector & alpha, const arma::fvec & x, const index_t i, const arma::fmat & D, const size_t L) { const auto & [aa, selected_atoms] = _OMP(x, D, L); @@ -33,7 +33,7 @@ void OMP(std::vector & alpha, const a_vec & x, const index_t i, const } } -a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & D, const size_t L) +arma::sp_fmat OMP_all(std::vector & locval, const arma::fmat & X, const arma::fmat & D, const size_t L) { locval.clear(); @@ -42,7 +42,7 @@ a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & OMP(locval, X.col(i), i, D, L); arma::umat DI(2, size(locval)); - a_vec DV(size(locval)); + arma::fvec DV(size(locval)); #pragma omp parallel for for(index_t k = 0; k < size(locval); ++k) @@ -52,17 +52,17 @@ a_sp_mat OMP_all(std::vector & locval, const a_mat & X, const a_mat & DV(k) = locval[k].val; } - return a_sp_mat(DI, DV, D.n_cols, X.n_cols); + return arma::sp_fmat(DI, DV, D.n_cols, X.n_cols); } -void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) +void sp_KSVD(arma::fmat & D, const arma::fmat & X, const size_t L, size_t k) { size_t m = D.n_cols; size_t M = X.n_cols; arma::uvec omega(M); - a_mat R, E, U, V; - a_vec s; + arma::fmat R, E, U, V; + arma::fvec s; std::vector locval; std::vector rows; @@ -70,7 +70,7 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) while(k--) { - a_sp_mat alpha = OMP_all(locval, X, D, L); + arma::sp_fmat alpha = OMP_all(locval, X, D, L); std::ranges::sort(locval, [](const locval_t & a, const locval_t & b) { @@ -108,13 +108,13 @@ void sp_KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) // DENSE -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L) +std::tuple _OMP(const arma::fvec & x, const arma::fmat & D, const size_t L) { arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; - a_mat DD; - a_vec aa, r = x; + arma::fmat DD; + arma::fvec aa, r = x; index_t l = 0; while(norm(r) > threshold && l < L) @@ -129,7 +129,7 @@ std::tuple _OMP(const a_vec & x, const a_mat & D, const size_ return {aa, selected_atoms.head(l)}; } -arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) +arma::uword max_index(const arma::fvec & V,const arma::uchar_vec & mask) { arma::uvec indices = arma::sort_index(V, "descend"); @@ -139,14 +139,14 @@ arma::uword max_index(const a_vec & V,const arma::uchar_vec & mask) return NIL; } -std::tuple _OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask) +std::tuple _OMP(const arma::fvec & x, const arma::fmat & D, const size_t L, const arma::uchar_vec & mask) { arma::uvec selected_atoms(L); real_t threshold = norm(x) * sigma; - a_mat DD; - a_vec aa, r = x; + arma::fmat DD; + arma::fvec aa, r = x; index_t l = 0; while(norm(r) > threshold && l < L) @@ -162,9 +162,9 @@ std::tuple _OMP(const a_vec & x, const a_mat & D, const size_ return {aa, selected_atoms.head(l)}; } -a_vec OMP(const a_vec & x, const a_mat & D, const size_t L) +arma::fvec OMP(const arma::fvec & x, const arma::fmat & D, const size_t L) { - a_vec alpha(D.n_cols, arma::fill::zeros); + arma::fvec alpha(D.n_cols, arma::fill::zeros); const auto & [aa, selected_atoms] = _OMP(x, D, L); alpha.elem(selected_atoms) = aa; @@ -172,9 +172,9 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t L) return alpha; } -a_vec OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_vec & mask) +arma::fvec OMP(const arma::fvec & x, const arma::fmat & D, const size_t L, const arma::uchar_vec & mask) { - a_vec alpha(D.n_cols, arma::fill::zeros); + arma::fvec alpha(D.n_cols, arma::fill::zeros); const auto & [aa, selected_atoms] = _OMP(x, D, L, mask); alpha.elem(selected_atoms) = aa; @@ -182,9 +182,9 @@ a_vec OMP(const a_vec & x, const a_mat & D, const size_t L, const arma::uchar_ve return alpha; } -a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t L) +arma::fmat OMP_all(const arma::fmat & X, const arma::fmat & D, const size_t L) { - a_mat alpha(D.n_cols, X.n_cols); + arma::fmat alpha(D.n_cols, X.n_cols); #pragma omp parallel for for(index_t i = 0; i < X.n_cols; ++i) { @@ -194,11 +194,11 @@ a_mat OMP_all(const a_mat & X, const a_mat & D, const size_t L) return alpha; } -void KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) +void KSVD(arma::fmat & D, const arma::fmat & X, const size_t L, size_t k) { arma::uvec omega; - a_mat alpha, R, E, U, V; - a_vec s; + arma::fmat alpha, R, E, U, V; + arma::fvec s; while(k--) { @@ -224,12 +224,12 @@ void KSVD(a_mat & D, const a_mat & X, const size_t L, size_t k) // MESH DENSE -a_vec OMP(const patch & p, const a_mat & A, const size_t L) +arma::fvec OMP(const patch & p, const arma::fmat & A, const size_t L) { return OMP(p.xyz.row(2).t(), p.phi * A, L); } -a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t L) +arma::fvec OMP(const patch & p, basis * phi_basis, const arma::fmat & A, const size_t L) { arma::uchar_vec mask(A.n_cols); @@ -239,9 +239,9 @@ a_vec OMP(const patch & p, basis * phi_basis, const a_mat & A, const size_t L) return OMP(p.xyz.row(2).t(), p.phi * A, L, mask); } -a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat & A, const size_t L) +arma::fmat OMP_all(const std::vector & patches, basis * phi_basis, const arma::fmat & A, const size_t L) { - a_mat alpha(A.n_cols, size(patches)); + arma::fmat alpha(A.n_cols, size(patches)); #pragma omp parallel for for(index_t i = 0; i < size(patches); ++i) @@ -250,9 +250,9 @@ a_mat OMP_all(const std::vector & patches, basis * phi_basis, const a_mat return alpha; } -a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t L) +arma::fmat OMP_all(const std::vector & patches, const arma::fmat & A, const size_t L) { - a_mat alpha(A.n_cols, size(patches)); + arma::fmat alpha(A.n_cols, size(patches)); #pragma omp parallel for for(index_t i = 0; i < size(patches); ++i) @@ -261,15 +261,15 @@ a_mat OMP_all(const std::vector & patches, const a_mat & A, const size_t return alpha; } -void KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k) +void KSVD(arma::fmat & A, const std::vector & patches, const size_t L, size_t k) { size_t K = A.n_rows; arma::uvec omega; - a_mat new_A = A; - a_mat alpha, D, sum, sum_error; - a_vec a, e; + arma::fmat new_A = A; + arma::fmat alpha, D, sum, sum_error; + arma::fvec a, e; real_t aj; @@ -313,12 +313,12 @@ void KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t // MESH SPARSE -void OMP(std::vector & alpha, const patch & p, const index_t i, const a_mat & A, const size_t L) +void OMP(std::vector & alpha, const patch & p, const index_t i, const arma::fmat & A, const size_t L) { OMP(alpha, p.xyz.row(2).t(), i, p.phi * A, L); } -a_sp_mat OMP_all(std::vector & locval, const std::vector & patches, const a_mat & A, const size_t L) +arma::sp_fmat OMP_all(std::vector & locval, const std::vector & patches, const arma::fmat & A, const size_t L) { locval.clear(); @@ -327,7 +327,7 @@ a_sp_mat OMP_all(std::vector & locval, const std::vector & patc OMP(locval, patches[i], i, A, L); arma::umat DI(2, size(locval)); - a_vec DV(size(locval)); + arma::fvec DV(size(locval)); #pragma omp parallel for for(index_t k = 0; k < size(locval); ++k) @@ -337,16 +337,16 @@ a_sp_mat OMP_all(std::vector & locval, const std::vector & patc DV(k) = locval[k].val; } - return a_sp_mat(DI, DV, A.n_cols, size(patches)); + return arma::sp_fmat(DI, DV, A.n_cols, size(patches)); } -void sp_KSVD(a_mat & A, const std::vector & patches, const size_t L, size_t k) +void sp_KSVD(arma::fmat & A, const std::vector & patches, const size_t L, size_t k) { size_t K = A.n_rows; - a_mat new_A = A; - a_mat D, sum, sum_error; - a_vec a, e; + arma::fmat new_A = A; + arma::fmat D, sum, sum_error; + arma::fvec a, e; real_t aj; @@ -356,7 +356,7 @@ void sp_KSVD(a_mat & A, const std::vector & patches, const size_t L, size while(k--) { - a_sp_mat alpha = OMP_all(locval, patches, A, L); + arma::sp_fmat alpha = OMP_all(locval, patches, A, L); std::ranges::sort(locval, [](const locval_t & a, const locval_t & b) { diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 6f6ca0a1..85206c9d 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -163,7 +163,7 @@ void msparse_coding::load_sampling() size_t count = 0; real_t area_mesh = mesh->area_surface(); - a_vec S; + arma::fvec S; if(S.load(tmp_file_path(key_name + ".rsampl"))) { gproshan_debug(loading sampling); @@ -273,7 +273,7 @@ void msparse_coding::load_sampling() if(!covered[i]) outliers.push_back(i); - a_vec outlv(size(outliers)); + arma::fvec outlv(size(outliers)); for(index_t i = 0; i < size(outliers); ++i) outlv(i) = outliers[i]; @@ -336,7 +336,7 @@ void msparse_coding::init_radial_feature_patches() bool save_all = true; if(save_all) { - a_mat AS; + arma::fmat AS; AS.resize(m_params.n_patches, 13); for(index_t i = 0; i < m_params.n_patches; ++i) { @@ -522,8 +522,8 @@ real_t msparse_coding::execute() che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) { - a_mat S; - a_mat alpha; + arma::fmat S; + arma::fmat alpha; S.load(tmp_file_path(key_name + ".smp")); alpha.load(tmp_file_path(key_name + ".alpha")); @@ -546,7 +546,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) #pragma omp parallel for for(index_t i = 0; i < m_params.n_patches; ++i) { - a_mat T(3,3); + arma::fmat T(3,3); T(0,0) = S(i,4); T(1,0) = S(i,5); T(2,0) = S(i,6); @@ -563,7 +563,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) p.phi.set_size(p.xyz.n_cols, phi_basis->dim()); phi_basis->discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t()); - a_vec x = p.phi * A * alpha.col(i); + arma::fvec x = p.phi * A * alpha.col(i); p.xyz.row(2) = x.t(); p.iscale_xyz(phi_basis->radio()); @@ -595,16 +595,16 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) che * new_mesh = new che(point_cloud.data(), size(point_cloud), nullptr, 0); new_mesh->update_normals(); - a_vec n; + arma::fvec n; for(index_t v = 0, i = 0; i < m_params.n_patches; ++i) { patch & p = patches[i]; phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 0); - a_vec dx = p.phi * A * alpha.col(i); + arma::fvec dx = p.phi * A * alpha.col(i); phi_basis->d_discrete(p.phi, p.xyz.row(0).t(), p.xyz.row(1).t(), 1); - a_vec dy = p.phi * A * alpha.col(i); + arma::fvec dy = p.phi * A * alpha.col(i); for(index_t j = 0; j < patches[i].xyz.n_cols; ++j, ++v) { @@ -680,8 +680,8 @@ void msparse_coding::learning() //random //arma::uvec r_ind = arma::randi(m, arma::distr_param(0, M)); //A = alpha.cols(r_ind); - a_mat R, E, U, V; - a_vec s; + arma::fmat R, E, U, V; + arma::fvec s; svd(U, s, V, alpha); gproshan_debug(svd done!); A = U.cols(0, m_params.n_atoms); @@ -868,7 +868,7 @@ void msparse_coding::init_patches(const bool reset, const fmask_t & mask) for(index_t s = 0; s < m_params.n_patches; ++s) { viewer::vectors.push_back({patches[s].x(0), patches[s].x(1), patches[s].x(2)}); - a_vec r = patches[s].x() + 0.02 * patches[s].normal(); + arma::fvec r = patches[s].x() + 0.02 * patches[s].normal(); viewer::vectors.push_back({r(0), r(1), r(2)}); } */ @@ -880,7 +880,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) assert(n_vertices == mesh->n_vertices); - a_mat V(3, mesh->n_vertices, arma::fill::zeros); + arma::fmat V(3, mesh->n_vertices, arma::fill::zeros); patches_error.resize(m_params.n_patches); @@ -889,7 +889,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) { patch & rp = patches[p]; - a_vec x = rp.phi * A * alpha.col(p); + arma::fvec x = rp.phi * A * alpha.col(p); patches_error[p] = { accu(abs(x - rp.xyz.row(2).t())) / size(rp.vertices), p }; @@ -921,7 +921,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) // simple means vertex if(size(patches_map[v]) && (!mask || mask(v))) { - a_vec mv = arma::zeros(3); + arma::fvec mv = arma::zeros(3); for(auto p: patches_map[v]) mv += patches[p.first].xyz.col(p.second); @@ -962,7 +962,7 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) return max_error; } -void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) +void msparse_coding::update_alphas(arma::fmat & alpha, size_t threshold) { size_t np_new = m_params.n_patches - threshold; bool patches_covered[np_new]; @@ -978,7 +978,7 @@ void msparse_coding::update_alphas(a_mat & alpha, size_t threshold) if(!patches_covered[s-threshold]) { - a_vec sum; + arma::fvec sum; sum.zeros(); size_t c = 0; // Here updating alphas, we need a structure between patches and neighboor patches diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index fb09387f..dd9d20ff 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -124,7 +124,7 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ return added; } -void patch::init_random(const vertex & c, const a_mat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr) +void patch::init_random(const vertex & c, const arma::fmat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr) { this->radio = radio; this->T = T; @@ -226,7 +226,7 @@ void patch::init_radial_disjoint( real_t & euc_radio, normal_fit_directions(mesh, v); - a_vec vn = T.col(2); + arma::fvec vn = T.col(2); vertex n = { vn(0), vn(1), vn(2) }; vertices.push_back(v); @@ -369,7 +369,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche // create a random point real_t a = abs(dis(gen)) * 2 * M_PI; real_t r = abs(dis(gen)); - a_vec np = { r * std::cos(a), r * std::sin(a), 0 }; + arma::fvec np = { r * std::cos(a), r * std::sin(a), 0 }; //gproshan_debug_var(np); // find the closest point @@ -377,7 +377,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche double min_d = INFINITY; for(index_t v: vertices) { - a_vec aux = xyz.col(vpatches[v][p]); + arma::fvec aux = xyz.col(vpatches[v][p]); aux(2) = 0; if(norm(np - aux) < min_d) @@ -394,7 +394,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche } // forstar to find closest trinagle - a_mat abc(3,3); + arma::fmat abc(3,3); for(const index_t he: mesh->star(min_v)) { //discard triangles outside the patch @@ -418,10 +418,10 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche if(abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) { - a_mat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); + arma::fmat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); - a_vec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); + arma::fvec coef = arma::inv(proj_abc.head_rows(2)) * np.head(2); np = proj_abc * coef + abc.col(0); if(!std::isnan(np(2))) @@ -500,7 +500,7 @@ void patch::iscale_xyz(const real_t radio_f) xyz = xyz / factor; } -const a_vec patch::normal() +const arma::fvec patch::normal() { return T.col(2); } @@ -541,7 +541,7 @@ void patch::gather_vertices(che * mesh, const index_t v, const real_t radio, ind memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); - a_vec p(3); + arma::fvec p(3); toplevel[v] = 0; qvertices.push({0, v}); @@ -664,8 +664,8 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, { if(itp.first == p) { - a_vec a = xyz.col(i); - a_vec b = xyz.col(itp.second); + arma::fvec a = xyz.col(i); + arma::fvec b = xyz.col(itp.second); a(2) = 0; b(2) = 0; distances.push_back(norm(a - b)); @@ -677,8 +677,8 @@ void patch::compute_avg_distance(che * mesh, std::vector & vpatches, /* for(size_t j = i+1; j < size(vertices); ++j) // replace for 1 ring { - a_vec a = xyz.col(i); - a_vec b = xyz.col(j); + arma::fvec a = xyz.col(i); + arma::fvec b = xyz.col(j); a(2) = 0; b(2) = 0; distances.push_back(norm(a - b)); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 34101f9b..58981b87 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -91,7 +91,7 @@ che::che(const che & mesh, const index_t * sorted, const che::options & opts) if(opts.colors) { - if(sorted) + if(!sorted) { memcpy(VC, mesh.VC, n_vertices * sizeof(rgb_t)); memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 50e63a2e..3ce7ad19 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -15,10 +15,10 @@ bool operator<(const border_t & a, const border_t & b) return a.theta > b.theta; } -a_vec normal_face(const std::vector & tmp_vertices, const index_t a_v, const index_t b_v, const index_t c_v) +arma::fvec normal_face(const std::vector & tmp_vertices, const index_t a_v, const index_t b_v, const index_t c_v) { - a_vec a = tmp_vertices[c_v] - tmp_vertices[a_v]; - a_vec b = tmp_vertices[b_v] - tmp_vertices[a_v]; + arma::fvec a = tmp_vertices[c_v] - tmp_vertices[a_v]; + arma::fvec b = tmp_vertices[b_v] - tmp_vertices[a_v]; return normalise(cross(a,b)); } @@ -26,8 +26,8 @@ che * mesh_simple_fill_hole(che * mesh, const std::vector & border_vert { std::vector vertices; vertex normal, normal_v, edge_v, v; - a_mat E(3, 3); - a_vec ve(3); + arma::fmat E(3, 3); + arma::fvec ve(3); vertices.reserve(size(border_vertices)); @@ -208,8 +208,8 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c void split_border(std::vector > & , che * mesh, const std::vector & border_vertices) { size_t n = size(border_vertices); - a_mat data(3, n); - a_mat means; + arma::fmat data(3, n); + arma::fmat means; vertex normal; for(index_t i = 0; i < n; ++i) @@ -338,8 +338,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti for(index_t v: front_vertices) vertices.push_back(mesh->point(v)); - std::vector tmp_vertices(size(vertices)); - std::vector tmp_normals(size(vertices)); + std::vector tmp_vertices(size(vertices)); + std::vector tmp_normals(size(vertices)); vertex normal; for(index_t v = 0; v < size(vertices); ++v) @@ -389,8 +389,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti real_t a75 = 75.0 * M_PI / 180; real_t a135 = 135.0 * M_PI / 180; - a_vec m_vec; - a_vec m_normal; + arma::fvec m_vec; + arma::fvec m_normal; while(!front.empty() && p_iter--) { @@ -551,11 +551,11 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti vertices.clear(); - for(a_vec r: tmp_vertices) + for(arma::fvec r: tmp_vertices) vertices.push_back({r[0], r[1], r[2]}); for(index_t v = 0; false && v < size(tmp_vertices); ++v) - a_vec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); + arma::fvec normal = tmp_vertices[v] + length * 3 * normalise(tmp_normals[v]); gproshan_debug_var(perimeter); // gproshan_debug(filling holes); @@ -575,7 +575,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length // PCA -------------------------------------------------------------------------- - a_mat V(3, size(vertices)); + arma::fmat V(3, size(vertices)); for(index_t v = 0; v < size(vertices); ++v) { V(0,v) = vertices[v][0]; @@ -589,16 +589,16 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length perimeter = init_perimeter; //debug(perimeter) - a_vec avg = mean(V, 1); + arma::fvec avg = mean(V, 1); V.each_col() -= avg; - a_vec orientation(3); + arma::fvec orientation(3); orientation(0) = normal.x(); orientation(1) = normal.y(); orientation(2) = normal.z(); - a_mat E; - a_vec eigval; + arma::fmat E; + arma::fvec eigval; eig_sym(eigval, E, V * V.t()); E.swap_cols(0, 2); //debug(E) @@ -612,19 +612,19 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length //debug(dot(orientation, E.col(2))) V = E.t() * V; -// V.each_col([](a_vec & v){v(2) = 0;}); +// V.each_col([](arma::fvec & v){v(2) = 0;}); bool o = is_grow; - a_vec a = V.col(0); - a_vec b = V.col(1); + arma::fvec a = V.col(0); + arma::fvec b = V.col(1); a(2) = b(2) = 0; a = normalise(a); b = normalise(b); // END PCA ---------------------------------------------------------------------- - std::vector tmp_vertices(size(vertices)); + std::vector tmp_vertices(size(vertices)); std::vector is_border(size(vertices)); std::vector > neighbors(size(vertices)); @@ -656,7 +656,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length real_t a75 = 75.0 * M_PI / 180; real_t a135 = 135.0 * M_PI / 180; - a_vec m_vec; + arma::fvec m_vec; while(!front.empty() && p_iter-- && p_iter < 2000) { while(!front.empty() && @@ -815,7 +815,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length vertices.clear(); vertices.reserve(size(tmp_vertices)); - for(a_vec r: tmp_vertices) + for(arma::fvec r: tmp_vertices) { r = E * r + avg; vertices.push_back({r[0], r[1], r[2]}); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index 612ab747..b441ac5b 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -1,7 +1,8 @@ #include #include -#include + +#include // geometry processing and shape analysis framework @@ -12,7 +13,7 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) { if(!k) return; - a_mat B(mesh->n_vertices, 3); + arma::fmat B(mesh->n_vertices, 3); for(index_t v = 0; v < mesh->n_vertices; ++v) { if(v < old_n_vertices) @@ -24,13 +25,13 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) else B.row(v).zeros(); } - a_sp_mat L, A; + arma::sp_fmat L, A; laplacian(mesh, L, A); for(index_t i = 0; i < mesh->n_vertices; ++i) B.row(i) *= -1 / A(i,i); - a_sp_mat M; + arma::sp_fmat M; real_t s = (k % 2) ? -1 : 1; if(k > 1) M = A * L; @@ -49,7 +50,7 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) A.shed_cols(0, old_n_vertices - 1); B.shed_rows(0, old_n_vertices - 1); - a_mat X; + arma::fmat X; if(spsolve(X, s * L, s * B)) for(index_t v = old_n_vertices; v < mesh->n_vertices; ++v) { @@ -59,13 +60,13 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) } } -void biharmonic_interp_2(a_mat & P, a_mat & H) +void biharmonic_interp_2(arma::fmat & P, arma::fmat & H) { size_t n = P.n_cols; real_t x; - a_mat A(n, n); - a_vec pi(2), pj(2); + arma::fmat A(n, n); + arma::fvec pi(2), pj(2); for(index_t i = 0; i < n; ++i) { @@ -78,7 +79,7 @@ void biharmonic_interp_2(a_mat & P, a_mat & H) } } - a_mat alpha = solve(A, P.row(2).t()); + arma::fmat alpha = solve(A, P.row(2).t()); for(index_t i = 0; i < H.n_cols; ++i) { @@ -116,7 +117,7 @@ void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n delete [] rings; delete [] sorted; - a_mat P(3, size(sub_mesh_hole)); + arma::fmat P(3, size(sub_mesh_hole)); index_t i = 0; for(index_t & b: sub_mesh_hole) { @@ -126,7 +127,7 @@ void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n ++i; } - a_mat H(3, n_vertices - old_n_vertices); + arma::fmat H(3, n_vertices - old_n_vertices); for(index_t i = 0, v = old_n_vertices; v < n_vertices; ++i, ++v) { @@ -135,13 +136,13 @@ void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n H(2, i) = mesh->point(v).z(); } - a_vec avg = mean(H, 1); + arma::fvec avg = mean(H, 1); P.each_col() -= avg; H.each_col() -= avg; - a_mat E; - a_vec eval; + arma::fmat E; + arma::fvec eval; eig_sym(eval, E, H * H.t()); E.swap_cols(0,2); diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 26f21a43..2ab6cec0 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -9,7 +9,7 @@ simplification::simplification(che * mesh_, const index_t levels_) { mesh = mesh_; levels = levels_; - Q = new a_mat[mesh->n_vertices]; + Q = new arma::fmat[mesh->n_vertices]; execute(); } @@ -33,7 +33,7 @@ void simplification::compute_quadrics() { Q[v].resize(4,4); Q[v].zeros(); - a_vec p(4); + arma::fvec p(4); for(const index_t he: mesh->star(v)) { @@ -68,7 +68,7 @@ void simplification::order_edges(index_t * sort_edges, real_t * error_edges) real_t simplification::compute_error(const index_t e) { vertex ve = create_vertex(e); - a_vec v(4); + arma::fvec v(4); v(0) = ve.x(); v(1) = ve.y(); From 0d2f56e68752c3b98d8d502c4068b609a5c96b49 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Mar 2024 02:06:53 +0100 Subject: [PATCH 0984/1018] gproshan update install external cmake files --- cmake/FindOptiX.cmake | 2 +- cmake/FindSuiteSparse.cmake | 22 +++++++++++----------- gproshanConfig.cmake.in | 2 ++ src/gproshan/CMakeLists.txt | 2 ++ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cmake/FindOptiX.cmake b/cmake/FindOptiX.cmake index 0aff2abd..9b3f6611 100644 --- a/cmake/FindOptiX.cmake +++ b/cmake/FindOptiX.cmake @@ -1,5 +1,5 @@ # -# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. +# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions diff --git a/cmake/FindSuiteSparse.cmake b/cmake/FindSuiteSparse.cmake index 0793a17a..a58cdefd 100644 --- a/cmake/FindSuiteSparse.cmake +++ b/cmake/FindSuiteSparse.cmake @@ -1,8 +1,8 @@ ## CMake file to locate SuiteSparse and its useful composite projects -## The first developpement of this file was made fro Windows users who -## use: +## The first development of this file was done by Windows users who +## used: ## https://github.com/jlblancoc/suitesparse-metis-for-windows -## Anyway, it chould be work also on linux (tested on fedora 17 when you installed suitesparse from yum) +## Anyway, it could work also on linux (tested on fedora 17 when you installed suitesparse from yum) ## ## ## Inputs variables this file can process (variable must be given before find_package(SUITESPARES ...) command) : @@ -11,7 +11,7 @@ ## Note: SuiteSparse lib usually requires linking to a blas and lapack library. ## ## -## Help variables this file handle internaly : +## Help variables this file handle internally : ## * SuiteSparse_SEARCH_LIB_POSTFIX Is set in cache (as advanced) to look into the right lib/lib64 dir for libraries (user can change) ## ## @@ -20,19 +20,19 @@ ## * SuiteSparse_INCLUDE_DIRS Paths containing SuiteSparse needed headers (depend on which COMPONENTS you gave) ## * SuiteSparse_LIBRARIES Absolute paths of SuiteSparse libs found (depend on which COMPONENTS you gave) ## If SuiteSparse_USE_LAPACK_BLAS is set to ON : -## * SuiteSparse_LAPACK_BLAS_LIBRARIES Which contain the libblas and liblapack libraries +## * SuiteSparse_LAPACK_BLAS_LIBRARIES Which contain the libblas and liblapack libraries ## On windows: -## * SuiteSparse_LAPACK_BLAS_DLL Which contain all requiered binaries for use libblas and liblapack +## * SuiteSparse_LAPACK_BLAS_DLL Which contain all required binaries for use libblas and liblapack ## ## ## Detailed variables this file provide : ## * SuiteSparse__FOUND True if the given component to look for is found (INCLUDE DIR and LIBRARY) -## * SuiteSparse__INCLUDE_DIR The path directory where we can found all compenent header files +## * SuiteSparse__INCLUDE_DIR The path directory where all component header files can be found ## * SuiteSparse__LIBRARY The file path to the component library ## Note: If a component is not found, a SuiteSparse__DIR cache variable is set to allow user set the search directory. ## ## -## Possible componnents to find are (maybe some others can be available): +## Possible components to find are (maybe some others can be available): ## * AMD ## * CAMD ## * COLAMD @@ -125,13 +125,13 @@ endif() ## we can use a generic way to find all of these with simple cmake lines of code macro(SuiteSparse_FIND_COMPONENTS ) - ## On windows : we absolutly need SuiteSparse_config.h every time for all projects + ## On windows : we absolutely need SuiteSparse_config.h every time for all projects if(WIN32) list(FIND SuiteSparse_FIND_COMPONENTS "suitesparseconfig" SS_config_index) if(${SS_config_index} MATCHES "-1") list(APPEND SuiteSparse_FIND_COMPONENTS suitesparseconfig) if(SuiteSparse_VERBOSE) - message(STATUS " On windows, we absolutly need SuiteSparse_config.h every time for all projects : add suitesparseconfig component to look for") + message(STATUS " On windows, we absolutely need SuiteSparse_config.h every time for all projects : add suitesparseconfig component to look for") endif() endif() endif() @@ -292,7 +292,7 @@ macro(SuiteSparse_FIND_COMPONENTS ) endif() if(NOT ${componentToCheck}) set(SuiteSparse_FOUND OFF) - break() ## one component not found is enought to failed + break() ## one component not found is enough to failed endif() endforeach() endmacro() diff --git a/gproshanConfig.cmake.in b/gproshanConfig.cmake.in index 11ff78c7..cb668b3f 100644 --- a/gproshanConfig.cmake.in +++ b/gproshanConfig.cmake.in @@ -2,6 +2,8 @@ include(CMakeFindDependencyMacro) +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + find_package(CUDAToolkit 12) find_dependency(embree 4) diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 815f72ab..9f40dbbf 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -77,6 +77,8 @@ write_basic_package_version_file( install(FILES ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/gproshanConfigVersion.cmake + ${gproshan_SOURCE_DIR}/cmake/FindSuiteSparse.cmake + ${gproshan_SOURCE_DIR}/cmake/FindOptiX.cmake DESTINATION lib/cmake/gproshan ) From 5371c78d57fc4660f18b635e7ec50556ea89e37a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Mar 2024 09:10:46 +0100 Subject: [PATCH 0985/1018] geodesics: update cholmod heat method implementation --- include/gproshan/geodesics/heat_method.h | 12 ------------ src/gproshan/geodesics/heat_method.cpp | 9 +++++++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index cd8468f0..51134c65 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -13,7 +13,6 @@ #include #include -#include // geometry processing and shape analysis framework @@ -31,21 +30,10 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & arma::vec compute_divergence(const che * mesh, const arma::vec & u); -/// cholmod Keenan implementation -/// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics -double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context); - -cholmod_dense * arma_2_cholmod(const arma::mat & m, cholmod_common * context); - -cholmod_sparse * arma_2_cholmod(const arma::sp_mat & m, cholmod_common * context); #ifdef GPROSHAN_CUDA -/// double solve_positive_definite_gpu(arma::mat & x, const arma::sp_mat & A, const arma::mat & b); - -/// host and device support -/// https://docs.nvidia.com/cuda/cusolver/index.html#cusolver-lt-t-gt-csrlsvchol double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx, const bool host = 0); #endif // GPROSHAN_CUDA diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index 9742591b..bf139681 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -4,12 +4,17 @@ #include #include +#include // geometry processing and shape analysis framework namespace gproshan { +/// cholmod Keenan implementation +/// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics +double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context); + double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) { if(!size(sources)) return 0; @@ -114,6 +119,10 @@ arma::vec compute_divergence(const che * mesh, const arma::vec & u) return div; } + +cholmod_dense * arma_2_cholmod(const arma::mat & m, cholmod_common * context); +cholmod_sparse * arma_2_cholmod(const arma::sp_mat & m, cholmod_common * context); + double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context) { cholmod_sparse * cA = arma_2_cholmod(A, context); From fccd82a1c597fc6534c66e6e1fcc335fdccd4f0f Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sun, 24 Mar 2024 18:19:27 +0100 Subject: [PATCH 0986/1018] update README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b69c10bf..4581251d 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,14 @@ finally execute: ### Dependencies (Linux/MacOS) -g++ >= 12.3, cuda >= 12.4, cmake >= 3.28, embree >= 4.3, glew, glfw3, armadillo, suitesparse, openblas, cimg +g++ >= 12.3, cuda >= 12.4, cmake >= 3.28, embree >= 4.3, glew, glfw3, armadillo, suitesparse, openblas, flann, cimg On Ubuntu you can install them with: - sudo apt install cmake libglew-dev libglfw3-dev libarmadillo-dev libsuitesparse-dev libopenblas-dev cimg-dev + sudo apt install cmake libglew-dev libglfw3-dev libarmadillo-dev libsuitesparse-dev libopenblas-dev libflann-dev cimg-dev Install Cuda if available to enable Cuda-based modules. +Export environment variable `OptiX_INSTALL_DIR` with the path to the OptiX library to enable it. #### Installing Intel Embree From 602888d2e6ba60988e085c65085671df04f08157 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 25 Mar 2024 23:27:55 +0100 Subject: [PATCH 0987/1018] float: updating float operations as default for loading and rendering (gl/rt) heat method, and descriptors as HKS need double precision --- CMakeLists.txt | 1 - include/gproshan/config.h.in | 1 - include/gproshan/features/descriptor.h | 2 +- include/gproshan/features/key_components.h | 4 +- include/gproshan/features/key_points.h | 4 +- include/gproshan/geodesics/dijkstra.h | 4 +- include/gproshan/geodesics/geodesics.h | 14 ++-- include/gproshan/geodesics/geodesics_ptp.h | 30 ++++---- include/gproshan/geodesics/heat_method.h | 2 +- include/gproshan/geodesics/sampling.h | 4 +- include/gproshan/geometry/convex_hull.h | 4 +- include/gproshan/geometry/mat.h | 6 +- include/gproshan/include.h | 8 +-- include/gproshan/laplacian/fairing_taubin.h | 4 +- include/gproshan/mdict/basis.h | 8 +-- include/gproshan/mdict/basis_cosine.h | 6 +- include/gproshan/mdict/basis_dct.h | 4 +- include/gproshan/mdict/mdict.h | 2 +- include/gproshan/mdict/msparse_coding.h | 28 ++++---- include/gproshan/mdict/patch.h | 46 ++++++------ include/gproshan/mesh/che.h | 44 ++++++------ include/gproshan/mesh/che_fill_hole.h | 8 +-- include/gproshan/mesh/che_sphere.h | 2 +- include/gproshan/mesh/quaternion.h | 34 ++++----- include/gproshan/mesh/simplification.h | 4 +- include/gproshan/pointcloud/knn.h | 22 +++--- include/gproshan/raytracing/embree.h | 4 +- include/gproshan/raytracing/light.h | 2 +- include/gproshan/raytracing/raytracing.h | 4 +- include/gproshan/raytracing/utils.h | 2 +- include/gproshan/scenes/scene.h | 6 +- include/gproshan/util.h | 9 --- include/gproshan/viewer/camera.h | 12 ++-- include/gproshan/viewer/che_viewer.h | 11 +-- include/gproshan/viewer/viewer.h | 9 --- src/gproshan/app_viewer.cpp | 78 ++++++++++----------- src/gproshan/features/descriptor.cpp | 2 +- src/gproshan/features/key_components.cpp | 2 +- src/gproshan/features/key_points.cpp | 6 +- src/gproshan/geodesics/dijkstra.cpp | 10 +-- src/gproshan/geodesics/geodesics.cpp | 22 +++--- src/gproshan/geodesics/geodesics_ptp.cpp | 12 ++-- src/gproshan/geodesics/geodesics_ptp.cu | 51 ++++++-------- src/gproshan/geodesics/heat_method.cpp | 8 +-- src/gproshan/geodesics/sampling.cpp | 4 +- src/gproshan/geometry/convex_hull.cpp | 4 +- src/gproshan/laplacian/fairing_spectral.cpp | 2 +- src/gproshan/laplacian/fairing_taubin.cpp | 4 +- src/gproshan/mdict/basis.cpp | 4 +- src/gproshan/mdict/basis_cosine.cpp | 24 +++---- src/gproshan/mdict/basis_dct.cpp | 4 +- src/gproshan/mdict/image_denoising.cpp | 14 ++-- src/gproshan/mdict/mdict.cpp | 10 +-- src/gproshan/mdict/msparse_coding.cpp | 38 +++++----- src/gproshan/mdict/patch.cpp | 60 ++++++++-------- src/gproshan/mesh/che.cpp | 70 +++++++++--------- src/gproshan/mesh/che_cuda.cpp | 4 +- src/gproshan/mesh/che_fill_hole.cpp | 36 +++++----- src/gproshan/mesh/che_img.cpp | 2 +- src/gproshan/mesh/che_ply.cpp | 10 +-- src/gproshan/mesh/che_poisson.cpp | 4 +- src/gproshan/mesh/che_ptx.cpp | 2 +- src/gproshan/mesh/che_sphere.cpp | 8 +-- src/gproshan/mesh/che_xyz.cpp | 4 +- src/gproshan/mesh/quaternion.cpp | 46 ++++++------ src/gproshan/mesh/simplification.cpp | 4 +- src/gproshan/pointcloud/knn.cpp | 58 +++++++-------- src/gproshan/raytracing/embree.cpp | 4 +- src/gproshan/raytracing/raytracing.cpp | 4 +- src/gproshan/scenes/scanner.cpp | 10 +-- src/gproshan/scenes/scene.cpp | 6 +- src/gproshan/util.cpp | 25 ------- src/gproshan/viewer/camera.cpp | 6 +- src/gproshan/viewer/che_viewer.cpp | 14 ++-- src/gproshan/viewer/scene_viewer.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 14 ++-- 76 files changed, 488 insertions(+), 559 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c9fffc4..382ea81c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,6 @@ endif(CUDAToolkit_FOUND) set(GPROSHAN_DIR "${gproshan_SOURCE_DIR}") option(GPROSHAN_LOG "Enables logging gproshan messages." ON) -option(GPROSHAN_FLOAT "Compile gproshan with float precision." ON) set(GPROSHAN_CUDA ${CUDAToolkit_FOUND}) set(GPROSHAN_OPTIX ${OptiX_INCLUDE}) diff --git a/include/gproshan/config.h.in b/include/gproshan/config.h.in index 777c66f6..1f316205 100644 --- a/include/gproshan/config.h.in +++ b/include/gproshan/config.h.in @@ -1,4 +1,3 @@ -#cmakedefine GPROSHAN_FLOAT #cmakedefine GPROSHAN_LOG #cmakedefine GPROSHAN_CUDA #cmakedefine GPROSHAN_OPTIX diff --git a/include/gproshan/features/descriptor.h b/include/gproshan/features/descriptor.h index 6cbd7269..94955997 100644 --- a/include/gproshan/features/descriptor.h +++ b/include/gproshan/features/descriptor.h @@ -29,7 +29,7 @@ class descriptor operator bool () const; ///< return norm of the descriptor for the vertex v - real_t operator () (const index_t v) const; + float operator () (const index_t v) const; private: void compute_gps(); diff --git a/include/gproshan/features/key_components.h b/include/gproshan/features/key_components.h index 94353bc1..b57908ac 100644 --- a/include/gproshan/features/key_components.h +++ b/include/gproshan/features/key_components.h @@ -13,7 +13,7 @@ namespace gproshan { class key_components { private: - real_t radio = 0; + float radio = 0; size_t n_vertices = 0; size_t n_comp = 0; index_t * comp = nullptr; @@ -21,7 +21,7 @@ class key_components std::map comp_idx; public: - key_components(che * mesh, const std::vector & kps, const real_t r); + key_components(che * mesh, const std::vector & kps, const float r); ~key_components(); index_t operator()(const index_t i); operator size_t () const; diff --git a/include/gproshan/features/key_points.h b/include/gproshan/features/key_points.h index 24779f8d..e3d75e19 100644 --- a/include/gproshan/features/key_points.h +++ b/include/gproshan/features/key_points.h @@ -15,11 +15,11 @@ class key_points std::vector is_kp; public: - key_points(che * mesh, const real_t percent = 0.10); + key_points(che * mesh, const float percent = 0.10); operator const std::vector & () const; private: - void compute_kps_areas(che * mesh, const real_t percent); + void compute_kps_areas(che * mesh, const float percent); }; diff --git a/include/gproshan/geodesics/dijkstra.h b/include/gproshan/geodesics/dijkstra.h index 1fbc96ec..064c3a0d 100644 --- a/include/gproshan/geodesics/dijkstra.h +++ b/include/gproshan/geodesics/dijkstra.h @@ -11,7 +11,7 @@ namespace gproshan { class dijkstra { private: - real_t * weights; + float * weights; index_t * predecessors; size_t n_vertices; index_t source; @@ -19,7 +19,7 @@ class dijkstra public: dijkstra(che * mesh, index_t src); ~dijkstra(); - real_t & operator()(index_t i); + float & operator()(index_t i); index_t & operator[](index_t i); void print(std::ostream & os); diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index afd1a707..10be9d5b 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -35,8 +35,8 @@ class geodesics { algorithm alg = FM; ///< specific the algorithm to execute. size_t n_iter = 0; ///< maximum number of iterations. - real_t radio = INFINITY; ///< execute until the specific radio. - real_t * dist_alloc = nullptr; ///< external dist allocation + float radio = INFINITY; ///< execute until the specific radio. + float * dist_alloc = nullptr; ///< external dist allocation bool cluster = false; ///< to cluster vertices to closest source. fm_function_t fun = nullptr; ///< fun is executed inside FM loop }; @@ -45,7 +45,7 @@ class geodesics index_t * clusters; ///< Clustering vertices to closest source. private: - real_t * dist; ///< Results of computation geodesic distances. + float * dist; ///< Results of computation geodesic distances. index_t * sorted_index; ///< Sort vertices by topological level or geodesic distance. size_t n_sorted; ///< Number of vertices sorted by their geodesics distance. bool free_dist; @@ -59,10 +59,10 @@ class geodesics ); virtual ~geodesics(); - operator const real_t * () const; - real_t operator[](const index_t i) const; + operator const float * () const; + float operator[](const index_t i) const; index_t operator()(const index_t i) const; - real_t radio() const; + float radio() const; index_t farthest() const; size_t n_sorted_index() const; void copy_sorted_index(index_t * indexes, const size_t n) const; @@ -70,7 +70,7 @@ class geodesics private: void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const real_t radio, const fm_function_t & fun); + void run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun); void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources); void run_heat_method(che * mesh, const std::vector & sources); diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 94f7242f..6d9661db 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -30,17 +30,17 @@ namespace gproshan { #ifdef __CUDACC__ __global__ -void relax_ptp(const che * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); +void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); __global__ -void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); +void relative_error(float * error, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); struct is_ok { - const real_t * error = nullptr; + const float * error = nullptr; __host_device__ - bool operator()(const real_t val) const; + bool operator()(const float val) const; __host_device__ bool operator()(const index_t val) const; @@ -51,10 +51,10 @@ struct is_ok struct ptp_out_t { - real_t * dist = nullptr; + float * dist = nullptr; index_t * clusters = nullptr; - ptp_out_t(real_t *const d, index_t *const c = nullptr); + ptp_out_t(float *const d, index_t *const c = nullptr); }; struct toplesets_t @@ -74,7 +74,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, const toplesets_t & toplesets, const bool coalescence = true, const bool set_inf = true, - const f_ptp & fun = nullptr + const f_ptp & fun = nullptr ); void parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, @@ -83,13 +83,13 @@ void parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, const toplesets_t & toplesets, const bool coalescence = true, const bool set_inf = true, - const f_ptp & fun = nullptr + const f_ptp & fun = nullptr ); -real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio = 0); +float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, float radio = 0); -void normalize_ptp(real_t * dist, const size_t n); +void normalize_ptp(float * dist, const size_t n); template @@ -97,7 +97,7 @@ template __forceinline__ #endif __host_device__ -real_t update_step(const che * mesh, const T * dist, const uvec3 & x) +float update_step(const che * mesh, const T * dist, const uvec3 & x) { const vec X[2] = {mesh->point(x[0]) - mesh->point(x[2]), mesh->point(x[1]) - mesh->point(x[2]) @@ -124,11 +124,7 @@ real_t update_step(const che * mesh, const T * dist, const uvec3 & x) (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]) * (t[0] * t[0] * Q[0][0] + t[0] * t[1] * (Q[1][0] + Q[0][1]) + t[1] * t[1] * Q[1][1] - 1); -#ifdef GPROSHAN_FLOAT T p = (delta + sqrtf(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); -#else - T p = (delta + sqrt(dis)) / (Q[0][0] + Q[0][1] + Q[1][0] + Q[1][1]); -#endif const vec tp = t - p; const vec n = { tp[0] * (X[0][0]*Q[0][0] + X[1][0]*Q[1][0]) + tp[1] * (X[0][0]*Q[0][1] + X[1][0]*Q[1][1]), @@ -155,7 +151,7 @@ __forceinline__ __host_device__ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { - real_t & ndv = new_dist[v] = old_dist[v]; + float & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; for(const index_t he: mesh->star(v)) @@ -165,7 +161,7 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust mesh->halfedge(he) }; - real_t d = update_step(mesh, old_dist, i); + float d = update_step(mesh, old_dist, i); if(d < ndv) { diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 51134c65..73898cc9 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -26,7 +26,7 @@ enum heat_method_opt { #endif // GPROSHAN_CUDA }; -double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); +double heat_method(float * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); arma::vec compute_divergence(const che * mesh, const arma::vec & u); diff --git a/include/gproshan/geodesics/sampling.h b/include/gproshan/geodesics/sampling.h index 7c80842f..de382ae8 100644 --- a/include/gproshan/geodesics/sampling.h +++ b/include/gproshan/geodesics/sampling.h @@ -11,9 +11,9 @@ namespace gproshan { -index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio); +index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, float radio); -bool load_sampling(std::vector & points, real_t & radio, che * mesh, size_t M); +bool load_sampling(std::vector & points, float & radio, che * mesh, size_t M); } // namespace gproshan diff --git a/include/gproshan/geometry/convex_hull.h b/include/gproshan/geometry/convex_hull.h index a93ef5a8..2adf14a5 100644 --- a/include/gproshan/geometry/convex_hull.h +++ b/include/gproshan/geometry/convex_hull.h @@ -20,8 +20,8 @@ class convex_hull std::vector CH; ///< convex hull points clockwise public: - convex_hull(const std::vector & points, const real_t precision = 1000); - convex_hull(const vertex * points, const size_t n_points, const real_t precision = 1000); + convex_hull(const std::vector & points, const float precision = 1000); + convex_hull(const vertex * points, const size_t n_points, const float precision = 1000); operator const std::vector & () const; private: diff --git a/include/gproshan/geometry/mat.h b/include/gproshan/geometry/mat.h index 418d9f03..9e408fdc 100644 --- a/include/gproshan/geometry/mat.h +++ b/include/gproshan/geometry/mat.h @@ -139,9 +139,9 @@ mat inverse(const mat & m) #endif // __CUDACC__ -using mat2 = mat; -using mat3 = mat; -using mat4 = mat; +using mat2 = mat; +using mat3 = mat; +using mat4 = mat; } // namespace gproshan diff --git a/include/gproshan/include.h b/include/gproshan/include.h index 3b5cfabc..11988769 100644 --- a/include/gproshan/include.h +++ b/include/gproshan/include.h @@ -25,12 +25,6 @@ namespace gproshan { using index_t = unsigned int; -#ifdef GPROSHAN_FLOAT - using real_t = float; -#else - using real_t = double; -#endif - inline std::string tmp_file_path(const std::string & file) { @@ -40,8 +34,10 @@ inline std::string tmp_file_path(const std::string & file) return gproshan_home + "/" + file; } + #define shaders_path(file) (std::string(GPROSHAN_DIR) + "/shaders/" + file) + #ifdef GPROSHAN_LOG #define gproshan_log_var(vari) std::cerr << "\033[0;33m[LOG] " << std::setprecision(3) << std::scientific << #vari << ":\033[0m " << (vari) << std::endl #define gproshan_log(message) fprintf(stderr, "\033[1;31m[LOG] %s: %s\n\033[0m", #message, __FUNCTION__) diff --git a/include/gproshan/laplacian/fairing_taubin.h b/include/gproshan/laplacian/fairing_taubin.h index 122546cc..0ecc7447 100644 --- a/include/gproshan/laplacian/fairing_taubin.h +++ b/include/gproshan/laplacian/fairing_taubin.h @@ -11,10 +11,10 @@ namespace gproshan { class fairing_taubin : public fairing { public: - real_t step; + float step; public: - fairing_taubin(const real_t step_ = 0.001); + fairing_taubin(const float step_ = 0.001); virtual ~fairing_taubin() = default; private: diff --git a/include/gproshan/mdict/basis.h b/include/gproshan/mdict/basis.h index 066f57bb..7accec26 100644 --- a/include/gproshan/mdict/basis.h +++ b/include/gproshan/mdict/basis.h @@ -16,16 +16,16 @@ namespace gproshan::mdict { class basis { protected: - real_t _radio; + float _radio; size_t _dim; public: - basis(const real_t r, const size_t d); + basis(const float r, const size_t d); virtual ~basis() = default; virtual void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) = 0; virtual void d_discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y, const bool b) = 0; - virtual real_t freq(const index_t idx) = 0; - real_t & radio(); + virtual float freq(const index_t idx) = 0; + float & radio(); size_t dim() const; void plot_basis(); void plot_atoms(const arma::fmat & A); diff --git a/include/gproshan/mdict/basis_cosine.h b/include/gproshan/mdict/basis_cosine.h index 1dc12813..d5819bf7 100644 --- a/include/gproshan/mdict/basis_cosine.h +++ b/include/gproshan/mdict/basis_cosine.h @@ -16,14 +16,14 @@ class basis_cosine: public basis size_t n_freq; ///< frequency public: - basis_cosine(const size_t nr, const size_t nf, const real_t r = 0); + basis_cosine(const size_t nr, const size_t nf, const float r = 0); void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y); private: void plot_basis(std::ostream & os); void plot_atoms(std::ostream & os, const arma::fvec & A); - arma::fvec cosine(const arma::fvec & x, const arma::fvec & y, const real_t c, const real_t alpha); - void cosine(std::ostream & os, const real_t c, const real_t alpha); + arma::fvec cosine(const arma::fvec & x, const arma::fvec & y, const float c, const float alpha); + void cosine(std::ostream & os, const float c, const float alpha); }; diff --git a/include/gproshan/mdict/basis_dct.h b/include/gproshan/mdict/basis_dct.h index 1abfefd8..2046d17a 100644 --- a/include/gproshan/mdict/basis_dct.h +++ b/include/gproshan/mdict/basis_dct.h @@ -15,10 +15,10 @@ class basis_dct: public basis size_t n_freq; ///< frequency public: - basis_dct(const size_t n, const real_t r = 1); + basis_dct(const size_t n, const float r = 1); void discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y); void d_discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y, const bool b); - real_t freq(const index_t idx); + float freq(const index_t idx); private: void plot_basis(std::ostream & os); diff --git a/include/gproshan/mdict/mdict.h b/include/gproshan/mdict/mdict.h index ecd40a6d..02b59fb9 100644 --- a/include/gproshan/mdict/mdict.h +++ b/include/gproshan/mdict/mdict.h @@ -16,7 +16,7 @@ namespace gproshan::mdict { struct locval_t { arma::uword i, j; - real_t val; + float val; }; diff --git a/include/gproshan/mdict/msparse_coding.h b/include/gproshan/mdict/msparse_coding.h index 614850f3..d6937808 100644 --- a/include/gproshan/mdict/msparse_coding.h +++ b/include/gproshan/mdict/msparse_coding.h @@ -21,10 +21,10 @@ class msparse_coding size_t n_patches = 0; ///< number of patches size_t avg_p = 36; ///< avg number of vertices per patch size_t percent = 0; ///< mask percentage - real_t f = 1; ///< - real_t delta = M_PI / 6; ///< - real_t sum_thres = 1.01; ///< - real_t area_thres = 0.005; ///< + float f = 1; ///< + float delta = M_PI / 6; ///< + float sum_thres = 1.01; ///< + float area_thres = 0.005; ///< bool learn = false; ///< bool plot = false; ///< }; @@ -39,14 +39,14 @@ class msparse_coding arma::fmat A; ///< dictionary continuous matrix. arma::fmat alpha; ///< sparse coding matrix. - real_t s_radio; ///< sampling geodesic radio. + float s_radio; ///< sampling geodesic radio. std::vector sampling; ///< samples, center of patches if sampling. std::vector patches; ///< vector of patches. std::vector patches_map; ///< invert index vertex to patches. - std::vector > patches_error; + std::vector > patches_error; double d_time; ///< time of operations. - real_t * dist; + float * dist; bool * mask = nullptr; std::string key_name; @@ -64,22 +64,22 @@ class msparse_coding virtual ~msparse_coding(); - real_t operator[](const index_t i) const; + float operator[](const index_t i) const; index_t draw_patches(const index_t p) const; operator const std::string & () const; - operator const real_t * () const; + operator const float * () const; - real_t execute(); + float execute(); void load_mask(const std::vector * vertices, const index_t * clusters); void load_mask(); void init_voronoi_patches(); void init_radial_feature_patches(); void load_sampling(); - che * point_cloud_reconstruction(real_t per, real_t fr); - std::vector sort_indexes(const std::vector &v); + che * point_cloud_reconstruction(float per, float fr); + std::vector sort_indexes(const std::vector &v); - real_t execute_tmp(); + float execute_tmp(); private: void learning(); @@ -90,7 +90,7 @@ class msparse_coding const fmask_t & mask = nullptr ); - real_t mesh_reconstruction(const fmask_t & mask = nullptr); + float mesh_reconstruction(const fmask_t & mask = nullptr); void update_alphas(arma::fmat & alpha, size_t threshold); void save_alpha(std::string file); diff --git a/include/gproshan/mdict/patch.h b/include/gproshan/mdict/patch.h index c5833c5b..fb8e6e90 100644 --- a/include/gproshan/mdict/patch.h +++ b/include/gproshan/mdict/patch.h @@ -29,12 +29,12 @@ class patch arma::fmat xyz; ///< Matrix of points. arma::fmat phi; ///< Projected basis. double avg_dist; ///< Average distance between points. - real_t radio; ///< Radio. + float radio; ///< Radio. size_t min_nv; ///< public: static size_t expected_nv; ///< Expected number of patch vertices. - static real_t nyquist_factor; ///< nyquist factor + static float nyquist_factor; ///< nyquist factor public: patch() = default; @@ -43,7 +43,7 @@ class patch void init( che * mesh, ///< input mesh. const index_t v, ///< center vertex of the patch. const size_t n_toplevels, ///< number of toplevels to jet fitting. - const real_t radio_, ///< euclidean radio in XY of the patch. + const float radio_, ///< euclidean radio in XY of the patch. index_t * _toplevel = nullptr ///< aux memory to gather toplevel vertices. ); @@ -53,20 +53,20 @@ class patch std::vector & _vertices, index_t * _toplevel = nullptr); - void init_radial_disjoint( real_t & euc_radio, - real_t & geo_radio, + void init_radial_disjoint( float & euc_radio, + float & geo_radio, che * mesh, const index_t v, - const real_t delta, - const real_t sum_thres, - const real_t area_thres, - const real_t area_mesh + const float delta, + const float sum_thres, + const float area_thres, + const float area_mesh ); - void init_random(const vertex & c, const arma::fmat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr); + void init_random(const vertex & c, const arma::fmat & T, const float radio, const float max_radio, const float percent, const float fr); void recover_radial_disjoint(che * mesh, - const real_t radio_, + const float radio_, const index_t v); void transform(); @@ -79,7 +79,7 @@ class patch const fmask_t & mask = nullptr ); void reset_xyz_disjoint(che * mesh, - real_t * dist, + float * dist, size_t M, std::vector & vpatches, const index_t p, @@ -90,20 +90,20 @@ class patch const arma::fvec normal(); bool is_covered( bool * covered); -// void save(const real_t radio, const size_t imsize, CImgList & imlist); - void update_heights(real_t & min, real_t & max, bool flag); +// void save(const float radio, const size_t imsize, CImgList & imlist); + void update_heights(float & min, float & max, bool flag); void compute_avg_distance(che * mesh, std::vector & vpatches, const index_t p); - void scale_xyz(const real_t radio_f); - void iscale_xyz(const real_t radio_f); + void scale_xyz(const float radio_f); + void iscale_xyz(const float radio_f); bool add_vertex_by_trigs( vertex & n, std::vector & N, double thr_angle, - const real_t * geo, + const float * geo, che * mesh, const index_t v, - real_t & area, - real_t & proj_area, - real_t deviation + float & area, + float & proj_area, + float deviation ); @@ -118,15 +118,15 @@ class patch /// Gather the vertices filter by radio in the local coordinates require initialize T and x. void gather_vertices( che * mesh, const index_t v, - const real_t radio, + const float radio, index_t * toplevel ); bool exists(index_t idx); /// Initialize transformation matrix T and translation vector x, using CGAL jet_fitting. void normal_fit_directions(che * mesh, const index_t v); - real_t get_min_z(); - real_t get_max_z(); + float get_min_z(); + float get_max_z(); void save_z(std::ostream & os); index_t find(const index_t * indexes, size_t nc, index_t idx_global); diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 0f014ca1..03f69a40 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -91,8 +91,8 @@ class che vertex * VN = nullptr; ///< vertex normals : v -> normal(v) rgb_t * VC = nullptr; ///< vertex color : v -> color(v) - real_t * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) - real_t scale_hm = 1; ///< vertex color heatmap scale factor + float * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) + float scale_hm = 1; ///< vertex color heatmap scale factor bool manifold = true; @@ -105,11 +105,11 @@ class che virtual ~che(); void reload(); - mat4 normalize_sphere(const real_t r = 1) const; - mat4 normalize_box(const real_t side = 2) const; + mat4 normalize_sphere(const float r = 1) const; + mat4 normalize_box(const float side = 2) const; che * merge(const che * mesh, const std::vector & com_vertices = {}); void update_vertices(const vertex * positions, const size_t n = 0, const index_t v_i = 0); - void update_heatmap(const real_t * hm = nullptr); + void update_heatmap(const float * hm = nullptr); void update_normals(); void invert_normals(); void multiplicate_vertices(); @@ -147,14 +147,14 @@ class che } __host_device__ - real_t heatmap(const index_t v) const + float heatmap(const index_t v) const { assert(v < n_vertices); return VHC[v]; } __host_device__ - real_t & heatmap(const index_t v) + float & heatmap(const index_t v) { assert(v < n_vertices); return VHC[v]; @@ -244,26 +244,26 @@ class che } __host_device__ - const real_t * heatmap_ptr() const + const float * heatmap_ptr() const { return VHC; } /* __host_device__ - T shading(const real_t u, const real_t v) + T shading(const float u, const float v) { } */ vertex normal_trig(const index_t t) const; vertex normal_he(const index_t he) const; - vertex gradient_he(const index_t he, const real_t * f) const; + vertex gradient_he(const index_t he, const float * f) const; dvec3 gradient_he(const index_t he, const double * f) const; - vertex gradient(const index_t v, const real_t * f); + vertex gradient(const index_t v, const float * f); - real_t heatmap_scale() const; - void heatmap_scale(const real_t shm); - real_t heatmap_scale(const index_t v) const; + float heatmap_scale() const; + void heatmap_scale(const float shm); + float heatmap_scale(const index_t v) const; index_t twin_he(const index_t he) const; index_t edge_u(const index_t e) const; @@ -291,19 +291,19 @@ class che size_t genus() const; size_t memory() const; size_t max_degree() const; - real_t quality() const; - real_t mean_edge() const; - real_t area_surface() const; + float quality() const; + float mean_edge() const; + float area_surface() const; bool is_manifold() const; virtual bool is_scene() const; virtual bool is_pointcloud() const; void flip(const index_t e); - real_t cotan(const index_t he) const; - real_t pdetriq(const index_t t) const; - real_t area_trig(const index_t t) const; - real_t area_vertex(const index_t v) const; - real_t mean_curvature(const index_t v) const; + float cotan(const index_t he) const; + float pdetriq(const index_t t) const; + float area_trig(const index_t t) const; + float area_vertex(const index_t v) const; + float mean_curvature(const index_t v) const; protected: diff --git a/include/gproshan/mesh/che_fill_hole.h b/include/gproshan/mesh/che_fill_hole.h index 76866dd2..1bbc532b 100644 --- a/include/gproshan/mesh/che_fill_hole.h +++ b/include/gproshan/mesh/che_fill_hole.h @@ -13,7 +13,7 @@ namespace gproshan { struct border_t { - real_t theta; + float theta; index_t v; border_t() = default; @@ -38,7 +38,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - arma::fvec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o) + arma::fvec new_vertex(const std::vector & V, float div, const float length, const std::array & neighbors, const bool o) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -81,7 +81,7 @@ struct border_t if(theta < 0) theta += 2 * M_PI; } - arma::fvec new_vertex(const std::vector & V, real_t div, const real_t length, const std::array & neighbors, const bool o, const arma::fvec & normal) + arma::fvec new_vertex(const std::vector & V, float div, const float length, const std::array & neighbors, const bool o, const arma::fvec & normal) { index_t p_v = neighbors[!o]; index_t n_v = neighbors[o]; @@ -125,7 +125,7 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti che * fill_hole_front_angles_without_projection(che * mesh, std::vector & front_vertices); -che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t max_iter, bool is_grow = false); +che * fill_hole_front_angles(std::vector & vertices, const float length, const vertex & normal, const size_t max_iter, bool is_grow = false); che * fill_hole_center_triangle(che * mesh, std::vector & select_vertices, index_t index); diff --git a/include/gproshan/mesh/che_sphere.h b/include/gproshan/mesh/che_sphere.h index 9f284df7..54743a9f 100644 --- a/include/gproshan/mesh/che_sphere.h +++ b/include/gproshan/mesh/che_sphere.h @@ -11,7 +11,7 @@ namespace gproshan { class che_sphere : public che { public: - che_sphere(const real_t r = 1, const size_t n = 6); + che_sphere(const float r = 1, const size_t n = 6); }; diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/mesh/quaternion.h index 35b043a9..4e4d64e4 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/mesh/quaternion.h @@ -16,42 +16,42 @@ using vertex = vec3; class quaternion { public: - real_t s; + float s; vertex v; public: - quaternion(real_t s = 0, real_t vi = 0, real_t vj = 0, real_t vk = 0); - quaternion(real_t s, const vertex & v); + quaternion(float s = 0, float vi = 0, float vj = 0, float vk = 0); + quaternion(float s, const vertex & v); quaternion(const vertex & v); operator const vertex & () const; - const quaternion & operator = (real_t s); + const quaternion & operator = (float s); const quaternion & operator = (const vertex & v); - real_t & operator [] (int index); - real_t operator [] (int index) const; - real_t & re(void); - real_t re(void) const; + float & operator [] (int index); + float operator [] (int index) const; + float & re(void); + float re(void) const; vertex & im(void); const vertex & im(void) const; quaternion operator + (const quaternion & q) const; quaternion operator - (const quaternion & q) const; quaternion operator - (void) const; - quaternion operator * (real_t c) const; - quaternion operator / (real_t c) const; + quaternion operator * (float c) const; + quaternion operator / (float c) const; void operator += (const quaternion & q); - void operator += (real_t c); + void operator += (float c); void operator -= (const quaternion & q); - void operator -= (real_t c); - void operator *= (real_t c); - void operator /= (real_t c); + void operator -= (float c); + void operator *= (float c); + void operator /= (float c); quaternion operator * (const quaternion & q) const; void operator *= (const quaternion & q); quaternion conj() const; quaternion inv() const; - real_t norm() const; - real_t norm2() const; + float norm() const; + float norm2() const; quaternion unit() const; void normalize(); @@ -59,7 +59,7 @@ class quaternion friend std::istream & operator >> (std::istream & is, quaternion & q); }; -quaternion operator * (real_t c, const quaternion & q); +quaternion operator * (float c, const quaternion & q); } // namespace gproshan diff --git a/include/gproshan/mesh/simplification.h b/include/gproshan/mesh/simplification.h index 972d70fd..cd531a22 100644 --- a/include/gproshan/mesh/simplification.h +++ b/include/gproshan/mesh/simplification.h @@ -25,8 +25,8 @@ class simplification private: void execute(); void compute_quadrics(); - real_t compute_error(const index_t e); - void order_edges(index_t * sort_edges, real_t * error_edges); + float compute_error(const index_t e); + void order_edges(index_t * sort_edges, float * error_edges); vertex create_vertex(const index_t e); }; diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index e9d4972f..c1a2393a 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -65,23 +65,23 @@ class k3tree }; -real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t median_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float median_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); -real_t median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat); -real_t mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat); +float median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat); +float mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat); const char * radius_str(void *, int opt); -real_t radius(const int opt, const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float radius(const int opt, const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); } // namespace gproshan diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 3b379585..7b4f9f6a 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -89,8 +89,8 @@ class embree : public raytracing vertex & attenuation, vertex & position, vertex & ray_dir, - real_t & dist, - random & rnd, + float & dist, + random & rnd, const render_params & params, const bool flat ) const; diff --git a/include/gproshan/raytracing/light.h b/include/gproshan/raytracing/light.h index bf438e77..2ca6faa4 100644 --- a/include/gproshan/raytracing/light.h +++ b/include/gproshan/raytracing/light.h @@ -12,7 +12,7 @@ struct light { vec3 pos = 0; vec3 color = 1; - real_t power = 10; + float power = 10; }; diff --git a/include/gproshan/raytracing/raytracing.h b/include/gproshan/raytracing/raytracing.h index 226248f3..5e300377 100644 --- a/include/gproshan/raytracing/raytracing.h +++ b/include/gproshan/raytracing/raytracing.h @@ -40,8 +40,8 @@ class raytracing vertex &, // attenuation, vertex &, // position, vertex &, // ray_dir, - real_t &, // dist - random &, // rnd, + float &, // dist + random &, // rnd, const render_params &, // params, const bool // flat ) const { return false; }; diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index 03718c4b..ef26d0c9 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -267,7 +267,7 @@ index_t closest_hit_vertex(const che & mesh, const H & hit) if(!mesh.n_trigs) return hit.primID; index_t he = 0; - real_t w = 1 - hit.u - hit.v; + float w = 1 - hit.u - hit.v; if(w < hit.u) { diff --git a/include/gproshan/scenes/scene.h b/include/gproshan/scenes/scene.h index 7e2cbb08..43ccffcd 100644 --- a/include/gproshan/scenes/scene.h +++ b/include/gproshan/scenes/scene.h @@ -28,9 +28,9 @@ class scene: public che vec3 Ka = 1; vec3 Kd = 0.8; vec3 Ks = 0.2; - real_t d = 1; // Tr = 0, opposite - real_t Ns = 10; - real_t Ni = 0; + float d = 1; // Tr = 0, opposite + float Ns = 10; + float Ni = 0; int illum = 1; int map_Ka = -1; int map_Kd = -1; diff --git a/include/gproshan/util.h b/include/gproshan/util.h index b6d420cf..16218b86 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -74,15 +74,6 @@ struct partitions::part }; -void copy_real_t_array(float * destination, const float * source, const size_t n_elem); - -void copy_real_t_array(float * destination, const double * source, const size_t n_elem); - -void copy_real_t_array(double * destination, const float * source, const size_t n_elem); - -void copy_real_t_array(double * destination, const double * source, const size_t n_elem); - - template T normalize(T * data, const size_t n_elem) { diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 7ea0780e..d8fbc6b4 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -22,13 +22,13 @@ class camera quaternion pos = vertex{0, 0, -3.14}; quaternion front = vertex{0, 0, 1}; quaternion up = vertex{0, 1, 0}; - real_t fovy = 45; - real_t aspect = 1; - real_t near = 0.01; - real_t far = 1000; + float fovy = 45; + float aspect = 1; + float near = 0.01; + float far = 1000; public: - static mat4 perspective(const real_t fovy, const real_t aspect, const real_t near, const real_t far); + static mat4 perspective(const float fovy, const float aspect, const float near, const float far); mat4 perspective(); mat4 look_at(const quaternion & r); @@ -37,7 +37,7 @@ class camera void motion(const double x, const double y, const int w, const int h); void zoom_in(); void zoom_out(); - real_t zoom() const; + float zoom() const; private: quaternion click_to_sphere(const double x, const double y, const int w, const int h); diff --git a/include/gproshan/viewer/che_viewer.h b/include/gproshan/viewer/che_viewer.h index 88df3a5a..3e996062 100644 --- a/include/gproshan/viewer/che_viewer.h +++ b/include/gproshan/viewer/che_viewer.h @@ -8,15 +8,6 @@ #include -#ifdef GPROSHAN_FLOAT - #define glVertex3v(x) glVertex3fv(x) - #define GL_REAL GL_FLOAT -#else - #define glVertex3v(x) glVertex3dv(x) - #define GL_REAL GL_DOUBLE -#endif - - // geometry processing and shape analysis framework namespace gproshan { @@ -75,7 +66,7 @@ class che_viewer void update_vbo_geometry(); void update_vbo_normal(const vertex * vnormal = nullptr); void update_vbo_color(const che::rgb_t * vcolor = nullptr); - void update_vbo_heatmap(const real_t * vheatmap = nullptr); + void update_vbo_heatmap(const float * vheatmap = nullptr); void update_instances_positions(const std::vector & translations); const vertex & selected_point(const index_t i) const; diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 7d161fe4..238e8707 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -19,15 +19,6 @@ #include -#ifdef GPROSHAN_FLOAT - #define ImGui_InputReal ImGui::InputFloat - #define ImGuiDataType_Real ImGuiDataType_Float -#else - #define ImGui_InputReal ImGui::InputDouble - #define ImGuiDataType_Real ImGuiDataType_Double -#endif // GPROSHAN_FLOAT - - // geometry processing and shape analysis framework namespace gproshan { diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index b9dbd5b7..dc12c183 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -204,9 +204,9 @@ bool app_viewer::process_simulate_scanner(viewer * p_view) ImGui::SliderScalar("n_rows", ImGuiDataType_U64, &n_rows, &n_min, &n_max); ImGui::SliderScalar("n_cols", ImGuiDataType_U64, &n_cols, &n_min, &n_max); - if( ImGui_InputReal("cam_pos.x", &cam_pos.x(), 0.001, 2, "%.3lf") || - ImGui_InputReal("cam_pos.y", &cam_pos.y(), 0.001, 2, "%.3lf") || - ImGui_InputReal("cam_pos.z", &cam_pos.z(), 0.001, 2, "%.3lf") ) + if( ImGui::InputFloat("cam_pos.x", &cam_pos.x(), 0.001, 2, "%.3lf") || + ImGui::InputFloat("cam_pos.y", &cam_pos.y(), 0.001, 2, "%.3lf") || + ImGui::InputFloat("cam_pos.z", &cam_pos.z(), 0.001, 2, "%.3lf") ) { view->sphere_points[0] = cam_pos; } @@ -234,7 +234,7 @@ bool app_viewer::process_scatter(viewer * p_view) rt::eval_hit h; std::vector scatter(100); - rt::random rnd(0xABCDEF); + rt::random rnd(0xABCDEF); for(vertex & v: scatter) h.scatter_diffuse(v, rnd); @@ -299,7 +299,7 @@ bool app_viewer::process_connected_components(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - real_t * label = &mesh->heatmap(0); + float * label = &mesh->heatmap(0); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) @@ -345,7 +345,7 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - real_t g, g_max = -INFINITY, g_min = INFINITY; + float g, g_max = -INFINITY, g_min = INFINITY; vertex a, b; arma::fvec gv(mesh->n_vertices); @@ -376,13 +376,13 @@ bool app_viewer::process_gaussian_curvature(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) gv(v) = (gv(v) + g_min) / g; - real_t gm = mean(gv); - real_t gs = var(gv); + float gm = mean(gv); + float gs = var(gv); gproshan_debug_var(gm); gproshan_debug_var(gs); - auto f = [&](real_t x, real_t a = 4) -> real_t + auto f = [&](float x, float a = 4) -> float { if(x < gm - a * gs) return 0; if(x > gm + a * gs) return 1; @@ -498,7 +498,7 @@ bool app_viewer::process_fairing_taubin(viewer * p_view) static std::vector vertices; static fairing_taubin fair(0); - if(ImGui_InputReal("step", &fair.step, 0.001)) + if(ImGui::InputFloat("step", &fair.step, 0.001)) { if(size(vertices) != mesh->n_vertices) { @@ -560,7 +560,7 @@ bool app_viewer::process_farthest_point_sampling(viewer * p_view) che_viewer & mesh = view->selected_mesh(); static int n = 10; - static real_t radio; + static float radio; ImGui::SliderInt("samples", &n, 1, mesh->n_vertices / 6); ImGui::Text("radio: %.3f", radio); @@ -625,7 +625,7 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) for(index_t v = 0; v < mesh->n_vertices; ++v) { if(toplesets[v] < n_toplesets) - mesh->heatmap(v) = real_t(toplesets[v]) / (n_toplesets); + mesh->heatmap(v) = float(toplesets[v]) / (n_toplesets); } mesh.update_vbo_heatmap(); @@ -651,12 +651,12 @@ bool app_viewer::process_msparse_coding(viewer * p_view) assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); - ImGui_InputReal("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); + ImGui::InputFloat("nyquist_factor", &patch::nyquist_factor, 0.01, 0.01, "%.2lf"); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::InputFloat("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputFloat("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputFloat("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) @@ -664,7 +664,7 @@ bool app_viewer::process_msparse_coding(viewer * p_view) basis_dct phi(n); msparse_coding msc(mesh, &phi, params); - real_t max_error = msc.execute(); + float max_error = msc.execute(); gproshan_log_var(max_error); mesh->update_heatmap(msc); @@ -685,7 +685,7 @@ bool app_viewer::process_mdict_patch(viewer * p_view) vertex vdir; patch p; - real_t mean_edge = mesh->mean_edge(); + float mean_edge = mesh->mean_edge(); for(auto & v: mesh.selected) { p.init(mesh, v, msparse_coding::T, msparse_coding::T * mean_edge, toplevel); @@ -732,9 +732,9 @@ bool app_viewer::process_mask(viewer * p_view) ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::InputFloat("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputFloat("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputFloat("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); if(ImGui::Button("Run")) @@ -768,20 +768,20 @@ bool app_viewer::process_pc_reconstruction(viewer * p_view) static msparse_coding::params params; static size_t n = 12; - static real_t percentage_size = 100; - static real_t radio_factor = 1; + static float percentage_size = 100; + static float radio_factor = 1; assert(sizeof(ImGuiDataType_U64) != sizeof(size_t)); ImGui::InputScalar("basis", ImGuiDataType_U64, &n); ImGui::InputScalar("atoms", ImGuiDataType_U64, ¶ms.n_atoms); - ImGui_InputReal("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); - ImGui_InputReal("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); - ImGui_InputReal("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); + ImGui::InputFloat("delta", ¶ms.delta, 0.001, 0.1, "%.3lf"); + ImGui::InputFloat("proj_thres", ¶ms.sum_thres, 1.001, 0.1, "%.6lf"); + ImGui::InputFloat("area_thres", ¶ms.area_thres, 0.001, 0.1, "%6lf"); ImGui::Checkbox("learn", ¶ms.learn); - ImGui_InputReal("percentage_size", &percentage_size, 100, 10, "%.3f"); - ImGui_InputReal("radio_factor", &radio_factor, 1, 0.1, "%.3f"); + ImGui::InputFloat("percentage_size", &percentage_size, 100, 10, "%.3f"); + ImGui::InputFloat("radio_factor", &radio_factor, 1, 0.1, "%.3f"); if(ImGui::Button("Run")) { @@ -866,7 +866,7 @@ bool app_viewer::process_descriptor_heatmap(viewer * p_view) status = true; n_eigs = features.n_eigs(); - real_t max_s = 0; + float max_s = 0; #pragma omp parallel for reduction(max: max_s) for(index_t v = 0; v < mesh->n_vertices; ++v) { @@ -891,8 +891,8 @@ bool app_viewer::process_key_points(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - static real_t percent = 0.1; - if(ImGui_InputReal("percent", &percent, 0.01, 0.1, "%.2f")) + static float percent = 0.1; + if(ImGui::InputFloat("percent", &percent, 0.01, 0.1, "%.2f")) { key_points kps(mesh, percent); mesh.selected = kps; @@ -906,15 +906,15 @@ bool app_viewer::process_key_components(viewer * p_view) app_viewer * view = (app_viewer *) p_view; che_viewer & mesh = view->selected_mesh(); - static real_t radio = 0.25; - if(ImGui_InputReal("radio", &radio, 0.01, 0.1, "%.2f")) + static float radio = 0.25; + if(ImGui::InputFloat("radio", &radio, 0.01, 0.1, "%.2f")) { // use the mesh selected points as key points. key_components kcs(mesh, mesh.selected, radio); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - mesh->heatmap(v) = (real_t) kcs(v) / kcs; + mesh->heatmap(v) = (float) kcs(v) / kcs; mesh.update_vbo_heatmap(); } @@ -979,7 +979,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) } center /= size(vbounds); - std::priority_queue > front; + std::priority_queue > front; std::vector neigs(size(vertices)); /* auto bprev = [&](const index_t v) -> index_t & @@ -994,7 +994,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) auto push = [&](const uvec3 & p) { neigs[p.x()] = {p.y(), p.z()}; - const real_t angle = 21; + const float angle = 21; if(angle <= M_PI) front.push({angle, p.x()}); }; @@ -1008,7 +1008,7 @@ bool app_viewer::process_fill_holes(viewer * p_view) std::vector border; border.assign(true, size(vertices)); -// real_t angle; +// float angle; index_t v0 = NIL; index_t v1 = NIL; index_t v2 = NIL; @@ -1112,7 +1112,7 @@ bool app_viewer::process_noise(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) { - real_t r = real_t(d_mod_1000(generator)) / 200000; + float r = float(d_mod_1000(generator)) / 200000; mesh->point(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } @@ -1133,7 +1133,7 @@ bool app_viewer::process_black_noise(viewer * p_view) #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) { - real_t r = real_t(d_mod_1000(generator)) / 200000; + float r = float(d_mod_1000(generator)) / 200000; mesh->point(v) += (!d_mod_5(generator)) * r * mesh->normal(v); } diff --git a/src/gproshan/features/descriptor.cpp b/src/gproshan/features/descriptor.cpp index 2e55c76d..4809d978 100644 --- a/src/gproshan/features/descriptor.cpp +++ b/src/gproshan/features/descriptor.cpp @@ -30,7 +30,7 @@ descriptor::operator bool () const return features.size() > 0; } -real_t descriptor::operator () (const index_t v) const +float descriptor::operator () (const index_t v) const { return norm(features.row(v)); } diff --git a/src/gproshan/features/key_components.cpp b/src/gproshan/features/key_components.cpp index 9e5816d6..3c620f08 100644 --- a/src/gproshan/features/key_components.cpp +++ b/src/gproshan/features/key_components.cpp @@ -9,7 +9,7 @@ namespace gproshan { -key_components::key_components(che * mesh, const std::vector & kps, const real_t r): radio(r) +key_components::key_components(che * mesh, const std::vector & kps, const float r): radio(r) { n_vertices = mesh->n_vertices; diff --git a/src/gproshan/features/key_points.cpp b/src/gproshan/features/key_points.cpp index 783d7f45..7acebe34 100644 --- a/src/gproshan/features/key_points.cpp +++ b/src/gproshan/features/key_points.cpp @@ -9,7 +9,7 @@ namespace gproshan { -key_points::key_points(che * mesh, const real_t percent) +key_points::key_points(che * mesh, const float percent) { compute_kps_areas(mesh, percent); } @@ -22,9 +22,9 @@ key_points::operator const std::vector & () const /// Efficient approach for interest points detection in non-rigid shapes /// Cristian Jose Lopez Del Alamo; Luciano Arnaldo Romero Calla; Lizeth Joseline Fuentes Perez /// DOI: 10.1109/CLEI.2015.7359459 -void key_points::compute_kps_areas(che * mesh, const real_t percent) +void key_points::compute_kps_areas(che * mesh, const float percent) { - std::vector > face_areas(mesh->n_trigs); + std::vector > face_areas(mesh->n_trigs); #pragma omp parallel for for(index_t f = 0; f < mesh->n_trigs; ++f) diff --git a/src/gproshan/geodesics/dijkstra.cpp b/src/gproshan/geodesics/dijkstra.cpp index 28dc19a7..e514ec1b 100644 --- a/src/gproshan/geodesics/dijkstra.cpp +++ b/src/gproshan/geodesics/dijkstra.cpp @@ -13,10 +13,10 @@ dijkstra::dijkstra(che * mesh, index_t src) n_vertices = mesh->n_vertices; source = src; - weights = new real_t[n_vertices]; + weights = new float[n_vertices]; predecessors = new index_t[n_vertices]; - memset(predecessors, 255, sizeof(real_t)*n_vertices); + memset(predecessors, 255, sizeof(float)*n_vertices); for(index_t i = 0; i < n_vertices; ++i) weights[i] = INFINITY; @@ -30,7 +30,7 @@ dijkstra::~dijkstra() if(predecessors) delete predecessors; } -real_t & dijkstra::operator()(index_t i) +float & dijkstra::operator()(index_t i) { return weights[i]; } @@ -54,7 +54,7 @@ void dijkstra::run(che * mesh) visited[source] = true; weights[source] = 0; - real_t min; + float min; index_t min_i; for(index_t i = 0; i < n_vertices; ++i) @@ -65,7 +65,7 @@ void dijkstra::run(che * mesh) #pragma omp parallel for num_threads(8) for(index_t v = 0; v < n_vertices; ++v) { - real_t w; + float w; if(!visited[v]) { diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index e01c8120..0d79103b 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -17,7 +17,7 @@ geodesics::geodesics(che * mesh, const std::vector & sources, const par free_dist = p.dist_alloc == nullptr; - dist = free_dist ? new real_t[n_vertices] : p.dist_alloc; + dist = free_dist ? new float[n_vertices] : p.dist_alloc; clusters = p.cluster ? new index_t[n_vertices] : nullptr; sorted_index = new index_t[n_vertices]; @@ -39,12 +39,12 @@ geodesics::~geodesics() delete [] clusters; } -geodesics::operator const real_t * () const +geodesics::operator const float * () const { return dist; } -real_t geodesics::operator[](const index_t i) const +float geodesics::operator[](const index_t i) const { assert(i < n_vertices); return dist[i]; @@ -56,7 +56,7 @@ index_t geodesics::operator()(const index_t i) const return sorted_index[i]; } -real_t geodesics::radio() const +float geodesics::radio() const { assert(n_sorted != 0); return dist[farthest()]; @@ -87,7 +87,7 @@ void geodesics::normalize() return; } - real_t max = dist[farthest()]; + float max = dist[farthest()]; #pragma omp parallel for for(size_t i = 0; i < n_sorted; ++i) @@ -114,7 +114,7 @@ void geodesics::execute(che * mesh, const std::vector & sources, const } } -void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const real_t radio, const fm_function_t & fun) +void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun) { index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -125,9 +125,9 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source size_t green_count = n_iter ? n_iter : n_vertices; - std::priority_queue, - std::vector >, - std::greater > > Q; + std::priority_queue, + std::vector >, + std::greater > > Q; index_t c = 0; n_sorted = 0; @@ -163,7 +163,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source if(color[v] == RED) { - real_t dv = dist[v]; + float dv = dist[v]; for(const index_t he: mesh->star(v)) { const uvec3 i = { mesh->halfedge(he_next(he)), @@ -171,7 +171,7 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source mesh->halfedge(he) }; - real_t d = update_step(mesh, dist, i); + float d = update_step(mesh, dist, i); if(d < dv) { diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 1b1ed965..1370e350 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -9,7 +9,7 @@ namespace gproshan { -ptp_out_t::ptp_out_t(real_t *const d, index_t *const c): dist(d), clusters(c) {} +ptp_out_t::ptp_out_t(float *const d, index_t *const c): dist(d), clusters(c) {} void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, @@ -18,7 +18,7 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, const toplesets_t & toplesets, const bool coalescence, const bool set_inf, - const f_ptp & fun + const f_ptp & fun ) { const size_t n_vertices = mesh->n_vertices; @@ -36,8 +36,8 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, } - real_t * dist[2] = { coalescence ? new real_t[n_vertices] : ptp_out.dist, - new real_t[n_vertices] + float * dist[2] = { coalescence ? new float[n_vertices] : ptp_out.dist, + new float[n_vertices] }; index_t * clusters[2] = { coalescence && ptp_out.clusters ? new index_t[n_vertices] : ptp_out.clusters, ptp_out.clusters ? new index_t[n_vertices] : nullptr @@ -75,9 +75,9 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, delete h_mesh; } -void normalize_ptp(real_t * dist, const size_t n) +void normalize_ptp(float * dist, const size_t n) { - real_t max_d = 0; + float max_d = 0; #pragma omp parallel for reduction(max: max_d) for(index_t v = 0; v < n; ++v) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index b04b4d9b..b30e3dd4 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -18,7 +18,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, const toplesets_t & toplesets, const bool coalescence, const bool set_inf, - const f_ptp & fun + const f_ptp & fun ) { const size_t n_vertices = mesh->n_vertices; @@ -45,18 +45,18 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, che_cuda d_mesh(h_mesh ? h_mesh : mesh, {false, false, false}); - real_t * h_dist = coalescence ? new real_t[n_vertices] : ptp_out.dist; + float * h_dist = coalescence ? new float[n_vertices] : ptp_out.dist; index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[n_vertices] : ptp_out.clusters; - real_t * d_error = nullptr; - real_t * d_dist[3] = {}; + float * d_error = nullptr; + float * d_dist[3] = {}; index_t * d_clusters[3] = {}; index_t * d_sorted = nullptr; - cudaMalloc(&d_error, sizeof(real_t) * n_vertices); - cudaMalloc(&d_dist[0], sizeof(real_t) * n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * n_vertices); + cudaMalloc(&d_error, sizeof(float) * n_vertices); + cudaMalloc(&d_dist[0], sizeof(float) * n_vertices); + cudaMalloc(&d_dist[1], sizeof(float) * n_vertices); d_dist[2] = h_dist; if(h_clusters) @@ -82,7 +82,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, coalescence ? inv : toplesets.index, d_sorted, fun); - cudaMemcpy(h_dist, d_dist[i], sizeof(real_t) * n_vertices, cudaMemcpyDeviceToHost); + cudaMemcpy(h_dist, d_dist[i], sizeof(float) * n_vertices, cudaMemcpyDeviceToHost); if(coalescence) { @@ -129,7 +129,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, return time / 1000; } -real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, real_t radio) +float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, float radio) { const size_t n_vertices = mesh->n_vertices; @@ -142,16 +142,16 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample che_cuda d_mesh(mesh, {false, false, false}); - real_t * h_dist = new real_t[n_vertices]; + float * h_dist = new float[n_vertices]; - real_t * d_error = nullptr; - real_t * d_dist[3] = {}; + float * d_error = nullptr; + float * d_dist[3] = {}; index_t * d_clusters[3] = {}; index_t * d_sorted = nullptr; - cudaMalloc(&d_error, sizeof(real_t) * n_vertices); - cudaMalloc(&d_dist[0], sizeof(real_t) * n_vertices); - cudaMalloc(&d_dist[1], sizeof(real_t) * n_vertices); + cudaMalloc(&d_error, sizeof(float) * n_vertices); + cudaMalloc(&d_dist[0], sizeof(float) * n_vertices); + cudaMalloc(&d_dist[1], sizeof(float) * n_vertices); cudaMalloc(&d_sorted, sizeof(index_t) * n_vertices); d_dist[2] = h_dist; @@ -172,7 +172,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample samples.reserve(n); int farthest; - real_t max_dist = INFINITY; + float max_dist = INFINITY; while(n-- && radio < max_dist) { limits.clear(); @@ -181,14 +181,10 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample const index_t i = run_ptp(d_mesh, samples, limits, d_error, d_dist, d_clusters, sorted_index, d_sorted); // 1 indexing - #ifdef GPROSHAN_FLOAT cublasIsamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); - #else - cublasIdamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); - #endif if(radio > 0 || !n) - cudaMemcpy(&max_dist, d_dist[i] + farthest - 1, sizeof(real_t), cudaMemcpyDeviceToHost); + cudaMemcpy(&max_dist, d_dist[i] + farthest - 1, sizeof(float), cudaMemcpyDeviceToHost); samples.push_back(farthest - 1); } @@ -218,7 +214,7 @@ real_t farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample } __global__ -void relax_ptp(const che * mesh, real_t * new_dist, real_t * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) +void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; @@ -227,24 +223,19 @@ void relax_ptp(const che * mesh, real_t * new_dist, real_t * old_dist, index_t * } __global__ -void relative_error(real_t * error, const real_t * new_dist, const real_t * old_dist, const index_t start, const index_t end, const index_t * sorted) +void relative_error(float * error, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; if(v < end) { v = sorted ? sorted[v] : v; - - #ifdef GPROSHAN_FLOAT - error[v] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; - #else - error[v] = fabs(new_dist[v] - old_dist[v]) / old_dist[v]; - #endif + error[v] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; } } __host_device__ -bool is_ok::operator()(const real_t val) const +bool is_ok::operator()(const float val) const { return val < PTP_TOL; } diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index bf139681..f0345a9e 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -15,7 +15,7 @@ namespace gproshan { /// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context); -double heat_method(real_t * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) +double heat_method(float * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) { if(!size(sources)) return 0; @@ -24,7 +24,7 @@ double heat_method(real_t * dist, const che * mesh, const std::vector & for(auto & v: sources) u0(v) = 1; // step - real_t dt = mesh->mean_edge(); + float dt = mesh->mean_edge(); dt *= dt; arma::sp_mat L, A; @@ -96,12 +96,12 @@ arma::vec compute_divergence(const che * mesh, const arma::vec & u) { arma::vec div(mesh->n_vertices); - std::vector f(mesh->n_vertices); + std::vector f(mesh->n_vertices); #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) { - real_t sum = 0; + float sum = 0; for(const index_t he: mesh->star(v)) { const vertex & n = mesh->normal_he(he); diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index c69bca06..e3f25832 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -10,7 +10,7 @@ namespace gproshan { -index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, real_t radio) +index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex *& normals, che * mesh, size_t n_points, float radio) { normals = new vertex[n_points]; sizes = new size_t[n_points]; @@ -36,7 +36,7 @@ index_t ** sampling_shape(std::vector & points, size_t *& sizes, vertex return indexes; } -bool load_sampling(std::vector & points, real_t & radio, che * mesh, size_t n) +bool load_sampling(std::vector & points, float & radio, che * mesh, size_t n) { const std::string & filename = mesh->filename; diff --git a/src/gproshan/geometry/convex_hull.cpp b/src/gproshan/geometry/convex_hull.cpp index 00b1e205..a38bb61d 100644 --- a/src/gproshan/geometry/convex_hull.cpp +++ b/src/gproshan/geometry/convex_hull.cpp @@ -8,9 +8,9 @@ namespace gproshan { -convex_hull::convex_hull(const std::vector & points, const real_t precision): convex_hull(points.data(), size(points), precision) {} +convex_hull::convex_hull(const std::vector & points, const float precision): convex_hull(points.data(), size(points), precision) {} -convex_hull::convex_hull(const vertex * points, const size_t n_points, const real_t precision) +convex_hull::convex_hull(const vertex * points, const size_t n_points, const float precision) { std::vector points2d(n_points); diff --git a/src/gproshan/laplacian/fairing_spectral.cpp b/src/gproshan/laplacian/fairing_spectral.cpp index bc107475..66cd0cb0 100644 --- a/src/gproshan/laplacian/fairing_spectral.cpp +++ b/src/gproshan/laplacian/fairing_spectral.cpp @@ -15,7 +15,7 @@ void fairing_spectral::compute(che * mesh) vertices = new vertex[mesh->n_vertices]; memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); - arma::fmat X((real_t *) vertices, 3, mesh->n_vertices, false, true); + arma::fmat X((float *) vertices, 3, mesh->n_vertices, false, true); arma::sp_fmat L, A; arma::fvec eigval; diff --git a/src/gproshan/laplacian/fairing_taubin.cpp b/src/gproshan/laplacian/fairing_taubin.cpp index 5dffbaa3..b20c4a2f 100644 --- a/src/gproshan/laplacian/fairing_taubin.cpp +++ b/src/gproshan/laplacian/fairing_taubin.cpp @@ -7,7 +7,7 @@ namespace gproshan { -fairing_taubin::fairing_taubin(const real_t step_): step(step_) {} +fairing_taubin::fairing_taubin(const float step_): step(step_) {} void fairing_taubin::compute(che * mesh) { @@ -15,7 +15,7 @@ void fairing_taubin::compute(che * mesh) vertices = new vertex[mesh->n_vertices]; memcpy(vertices, &mesh->point(0), mesh->n_vertices * sizeof(vertex)); - arma::fmat X((real_t *) vertices, 3, mesh->n_vertices, false, true); + arma::fmat X((float *) vertices, 3, mesh->n_vertices, false, true); arma::sp_fmat L, A; laplacian(mesh, L, A); diff --git a/src/gproshan/mdict/basis.cpp b/src/gproshan/mdict/basis.cpp index ee5925e1..85a0477b 100644 --- a/src/gproshan/mdict/basis.cpp +++ b/src/gproshan/mdict/basis.cpp @@ -6,9 +6,9 @@ namespace gproshan::mdict { -basis::basis(const real_t r, const size_t d): _radio(r), _dim(d) {} +basis::basis(const float r, const size_t d): _radio(r), _dim(d) {} -real_t & basis::radio() +float & basis::radio() { return _radio; } diff --git a/src/gproshan/mdict/basis_cosine.cpp b/src/gproshan/mdict/basis_cosine.cpp index 5eb1c44b..c2c1e268 100644 --- a/src/gproshan/mdict/basis_cosine.cpp +++ b/src/gproshan/mdict/basis_cosine.cpp @@ -8,17 +8,17 @@ namespace gproshan::mdict { -basis_cosine::basis_cosine(const size_t nr, const size_t nf, const real_t r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} +basis_cosine::basis_cosine(const size_t nr, const size_t nf, const float r): basis(r, r * nf), n_rot(nr), n_freq(nf) {} void basis_cosine::discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) { assert(phi.n_cols == _dim); - real_t d = 1.0 / (n_rot - 1); - real_t c; + float d = 1.0 / (n_rot - 1); + float c; for(size_t k = 0, ni = 1; ni <= n_freq; ++ni) - for(real_t alpha = 0; alpha <= 1; alpha += d, ++k) + for(float alpha = 0; alpha <= 1; alpha += d, ++k) { c = ni * M_PI / _radio; phi.col(k) = cosine(x, y, c, alpha); @@ -27,13 +27,13 @@ void basis_cosine::discrete(arma::fmat & phi, const arma::fvec & x, const arma:: void basis_cosine::plot_basis(std::ostream & os) { - real_t d = 1.0 / (n_rot - 1); - real_t c; + float d = 1.0 / (n_rot - 1); + float c; os << "set multiplot layout " << n_freq << "," << n_rot << " rowsfirst scale 1.2;" << std::endl; for(size_t ni = 1; ni <= n_freq; ++ni) - for(real_t alpha = 0; alpha <= 1; alpha += d) + for(float alpha = 0; alpha <= 1; alpha += d) { c = ni * M_PI / _radio; os << "splot v * cos(u), v * sin(u), "; cosine(os, c, alpha); os << ";" << std::endl; @@ -42,23 +42,23 @@ void basis_cosine::plot_basis(std::ostream & os) void basis_cosine::plot_atoms(std::ostream & os, const arma::fvec & A) { - real_t d = 1.0 / (n_rot - 1); - real_t c; + float d = 1.0 / (n_rot - 1); + float c; for(size_t k = 0, ni = 1; ni <= n_freq; ++ni) - for(real_t alpha = 0; alpha <= 1; alpha += d, ++k) + for(float alpha = 0; alpha <= 1; alpha += d, ++k) { c = ni * M_PI / _radio; os << " + " << A(k) << " * "; cosine(os, c, alpha); } } -arma::fvec basis_cosine::cosine(const arma::fvec & x, const arma::fvec & y, const real_t c, const real_t alpha) +arma::fvec basis_cosine::cosine(const arma::fvec & x, const arma::fvec & y, const float c, const float alpha) { return cos(c * (alpha * x + (1 - alpha) * y)); } -void basis_cosine::cosine(std::ostream & os, const real_t c, const real_t alpha) +void basis_cosine::cosine(std::ostream & os, const float c, const float alpha) { os << "cos( " << c << " * (" << alpha << " * v * cos(u) + ( 1 - " << alpha << ") * v * sin(u)))"; } diff --git a/src/gproshan/mdict/basis_dct.cpp b/src/gproshan/mdict/basis_dct.cpp index 97cfd13d..2cf6620e 100644 --- a/src/gproshan/mdict/basis_dct.cpp +++ b/src/gproshan/mdict/basis_dct.cpp @@ -8,7 +8,7 @@ namespace gproshan::mdict { -basis_dct::basis_dct(const size_t n, const real_t r): basis(r, n * n), n_freq(n) {} +basis_dct::basis_dct(const size_t n, const float r): basis(r, n * n), n_freq(n) {} void basis_dct::discrete(arma::fmat & phi, const arma::fvec & x, const arma::fvec & y) { @@ -65,7 +65,7 @@ void basis_dct::dct(std::ostream & os, const index_t nx, const index_t ny) os << "cos( (pi * v * sin(u) * " << ny << " ) / " << _radio << " )"; } -real_t basis_dct::freq(const index_t idx) +float basis_dct::freq(const index_t idx) { return !idx ? INFINITY : 2 * _radio / std::max(idx / n_freq, idx % n_freq); } diff --git a/src/gproshan/mdict/image_denoising.cpp b/src/gproshan/mdict/image_denoising.cpp index c6a8c62e..90d33367 100644 --- a/src/gproshan/mdict/image_denoising.cpp +++ b/src/gproshan/mdict/image_denoising.cpp @@ -19,7 +19,7 @@ namespace gproshan::mdict { void test_image_denoising(const std::string & file) { - CImg image(file.c_str()); + CImg image(file.c_str()); image.resize(128, 128); image = image.get_normalize(0, 1); @@ -50,12 +50,12 @@ void test_image_denoising(const std::string & file) arma::fmat spD = D; - CImg imdict; + CImg imdict; for(index_t i = 0; i < 16; ++i) { - CImg imrow; + CImg imrow; for(index_t j = 0; j < 16; ++j) - imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); + imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); imdict.append(imrow, 'y'); } @@ -79,12 +79,12 @@ void test_image_denoising(const std::string & file) gproshan_log_var(norm(D - spD)); - CImg imdictlearned; + CImg imdictlearned; for(index_t i = 0; i < 16; ++i) { - CImg imrow; + CImg imrow; for(index_t j = 0; j < 16; ++j) - imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); + imrow.append(CImg(D.colptr(i * 16 + j), p, p, 1, 1, true), 'x'); imdictlearned.append(imrow, 'y'); } diff --git a/src/gproshan/mdict/mdict.cpp b/src/gproshan/mdict/mdict.cpp index 826238d3..f37fe922 100644 --- a/src/gproshan/mdict/mdict.cpp +++ b/src/gproshan/mdict/mdict.cpp @@ -12,7 +12,7 @@ // mesh dictionary learning and sparse coding namespace namespace gproshan::mdict { -const real_t sigma = 0.01; +const float sigma = 0.01; // SPARSE @@ -111,7 +111,7 @@ void sp_KSVD(arma::fmat & D, const arma::fmat & X, const size_t L, size_t k) std::tuple _OMP(const arma::fvec & x, const arma::fmat & D, const size_t L) { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; + float threshold = norm(x) * sigma; arma::fmat DD; arma::fvec aa, r = x; @@ -143,7 +143,7 @@ std::tuple _OMP(const arma::fvec & x, const arma::fmat & { arma::uvec selected_atoms(L); - real_t threshold = norm(x) * sigma; + float threshold = norm(x) * sigma; arma::fmat DD; arma::fvec aa, r = x; @@ -271,7 +271,7 @@ void KSVD(arma::fmat & A, const std::vector & patches, const size_t L, si arma::fmat alpha, D, sum, sum_error; arma::fvec a, e; - real_t aj; + float aj; while(k--) { @@ -348,7 +348,7 @@ void sp_KSVD(arma::fmat & A, const std::vector & patches, const size_t L, arma::fmat D, sum, sum_error; arma::fvec a, e; - real_t aj; + float aj; std::vector locval; std::vector rows; diff --git a/src/gproshan/mdict/msparse_coding.cpp b/src/gproshan/mdict/msparse_coding.cpp index 85206c9d..6b5296c6 100644 --- a/src/gproshan/mdict/msparse_coding.cpp +++ b/src/gproshan/mdict/msparse_coding.cpp @@ -22,7 +22,7 @@ size_t msparse_coding::T = 5; msparse_coding::msparse_coding(che * _mesh, basis * _phi_basis, const params & p): mesh(_mesh), phi_basis(_phi_basis), m_params(p) { A.eye(phi_basis->dim(), m_params.n_atoms); - dist = new real_t[mesh->n_vertices]; + dist = new float[mesh->n_vertices]; m_params.n_patches = mesh->n_vertices / m_params.avg_p; @@ -42,7 +42,7 @@ msparse_coding::operator const std::string & () const return key_name; } -msparse_coding::operator const real_t * () const +msparse_coding::operator const float * () const { return dist; } @@ -161,7 +161,7 @@ void msparse_coding::load_sampling() gproshan_debug_var(size(all_sorted_features)); size_t count = 0; - real_t area_mesh = mesh->area_surface(); + float area_mesh = mesh->area_surface(); arma::fvec S; if(S.load(tmp_file_path(key_name + ".rsampl"))) @@ -169,7 +169,7 @@ void msparse_coding::load_sampling() gproshan_debug(loading sampling); size_t n_seeds = S.n_rows; - real_t euc_radio, geo_radio; + float euc_radio, geo_radio; for(index_t i = 0; i < n_seeds; ++i) { patch p; @@ -196,10 +196,10 @@ void msparse_coding::load_sampling() for(index_t i = 0; i < mesh->n_vertices; ++i) covered[i] = 0; - real_t euc_radio; - real_t geo_radio; - std::vector radios; - std::vector geo_radios; + float euc_radio; + float geo_radio; + std::vector radios; + std::vector geo_radios; size_t count_cov = 0; size_t count_cov_patch = 0; @@ -464,7 +464,7 @@ void msparse_coding::init_voronoi_patches() gproshan_log(our patches are ready); } -real_t msparse_coding::execute() +float msparse_coding::execute() { TIC(d_time) init_radial_feature_patches(); TOC(d_time) @@ -508,19 +508,19 @@ real_t msparse_coding::execute() bool * pmask = mask; TIC(d_time) - real_t max_error = mesh_reconstruction([&pmask](const index_t i) -> bool { return pmask[i]; }); + float max_error = mesh_reconstruction([&pmask](const index_t i) -> bool { return pmask[i]; }); TOC(d_time) gproshan_debug_var(d_time); arma::uvec non_zero = find( abs(alpha) > 0.00001); gproshan_debug_var(size(non_zero)); - real_t ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices); + float ratio = (m_params.n_patches * 13.0 + non_zero.size()) / (3 * mesh->n_vertices); gproshan_log_var(ratio); return max_error; } -che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) +che * msparse_coding::point_cloud_reconstruction(float per, float fr) { arma::fmat S; arma::fmat alpha; @@ -533,7 +533,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) gproshan_debug_var(m_params.n_patches); - real_t max_radio = -1; + float max_radio = -1; #pragma omp parallel for reduction(max: max_radio) for(index_t i = 0; i < m_params.n_patches; ++i) @@ -622,7 +622,7 @@ che * msparse_coding::point_cloud_reconstruction(real_t per, real_t fr) } -real_t msparse_coding::execute_tmp() +float msparse_coding::execute_tmp() { // fill holes size_t threshold = mesh->n_vertices; @@ -847,7 +847,7 @@ void msparse_coding::init_patches(const bool reset, const fmask_t & mask) /* #ifndef NDEBUG - CImgList imlist; + CImgList imlist; for(index_t s = 0; s < m_params.n_patches; ++s) patches[s].save(phi_basis->radio(), 16, imlist); imlist.save_ffmpeg_external("tmp/patches.mpg", 5); @@ -874,7 +874,7 @@ void msparse_coding::init_patches(const bool reset, const fmask_t & mask) */ } -real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) +float msparse_coding::mesh_reconstruction(const fmask_t & mask) { gproshan_log(MDICT); @@ -939,8 +939,8 @@ real_t msparse_coding::mesh_reconstruction(const fmask_t & mask) vertex * new_vertices = (vertex *) V.memptr(); - real_t error = 0; - real_t max_error = -1; + float error = 0; + float max_error = -1; #pragma omp parallel for reduction(+: error) reduction(max: max_error) for(index_t v = 0; v < mesh->n_vertices; ++v) @@ -1012,7 +1012,7 @@ index_t msparse_coding::sample(const index_t s) return s; } -real_t msparse_coding::operator[](const index_t i) const +float msparse_coding::operator[](const index_t i) const { assert(i < mesh->n_vertices); return dist[i]; diff --git a/src/gproshan/mdict/patch.cpp b/src/gproshan/mdict/patch.cpp index dd9d20ff..699cd73c 100644 --- a/src/gproshan/mdict/patch.cpp +++ b/src/gproshan/mdict/patch.cpp @@ -13,9 +13,9 @@ namespace gproshan::mdict { size_t patch::expected_nv = 3 * msparse_coding::T * (msparse_coding::T + 1); -real_t patch::nyquist_factor = 0.5; +float patch::nyquist_factor = 0.5; -void patch::init(che * mesh, const index_t v, const size_t n_toplevels, const real_t radio_, index_t * _toplevel) +void patch::init(che * mesh, const index_t v, const size_t n_toplevels, const float radio_, index_t * _toplevel) { radio = radio_; index_t * toplevel = _toplevel ? _toplevel : new index_t[mesh->n_vertices]; @@ -58,7 +58,7 @@ index_t patch::find(const index_t * indexes, size_t nc, index_t idx_global) return -1; } -bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_angle, const real_t * geo, che * mesh, const index_t v, real_t & area, real_t & proj_area, real_t deviation) +bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_angle, const float * geo, che * mesh, const index_t v, float & area, float & proj_area, float deviation) { // it needs to return both vertices // it needs to filter repeated indexes. @@ -124,7 +124,7 @@ bool patch::add_vertex_by_trigs(vertex & n, std::vector & N, double thr_ return added; } -void patch::init_random(const vertex & c, const arma::fmat & T, const real_t radio, const real_t max_radio, const real_t percent, const real_t fr) +void patch::init_random(const vertex & c, const arma::fmat & T, const float radio, const float max_radio, const float percent, const float fr) { this->radio = radio; this->T = T; @@ -160,12 +160,12 @@ void patch::init_random(const vertex & c, const arma::fmat & T, const real_t rad } } -void patch::recover_radial_disjoint(che * mesh, const real_t radio_, const index_t v) +void patch::recover_radial_disjoint(che * mesh, const float radio_, const index_t v) { // for small meshes 6000 0.e-5 // for others 2.e-5 geodesics::params params; - params.radio = radio_ + 1e-5;//numeric_limits::epsilon(); + params.radio = radio_ + 1e-5;//numeric_limits::epsilon(); geodesics geo(mesh, {v}, params); @@ -209,14 +209,14 @@ void patch::recover_radial_disjoint(che * mesh, const real_t radio_, const index } -void patch::init_radial_disjoint( real_t & euc_radio, - real_t & geo_radio, +void patch::init_radial_disjoint( float & euc_radio, + float & geo_radio, che * mesh, const index_t v, - const real_t delta, - const real_t sum_thres, - const real_t area_thres, - const real_t area_mesh + const float delta, + const float sum_thres, + const float area_thres, + const float area_mesh ) { radio = -INFINITY; @@ -234,14 +234,14 @@ void patch::init_radial_disjoint( real_t & euc_radio, std::vector N; N.push_back(n); - real_t area = 0; - real_t proj_area = std::numeric_limits::epsilon(); - real_t ratio; + float area = 0; + float proj_area = std::numeric_limits::epsilon(); + float ratio; vertex c = mesh->point(v); geodesics::params params; - params.dist_alloc = new real_t[mesh->n_vertices]; + params.dist_alloc = new float[mesh->n_vertices]; params.fun = [&](const index_t u) -> bool { if(u == v) return true; @@ -367,8 +367,8 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche // add new vertices // create a random point - real_t a = abs(dis(gen)) * 2 * M_PI; - real_t r = abs(dis(gen)); + float a = abs(dis(gen)) * 2 * M_PI; + float r = abs(dis(gen)); arma::fvec np = { r * std::cos(a), r * std::sin(a), 0 }; //gproshan_debug_var(np); @@ -416,7 +416,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche - if(abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) + if(abs(A - (A1 + A2 + A3)) < std::numeric_limits::epsilon()) { arma::fmat proj_abc = abc.tail_cols(2).each_col() - abc.col(0); np -= abc.col(0); @@ -447,7 +447,7 @@ void patch::add_extra_xyz_disjoint(che * mesh, std::vector & vpatche } } -void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector & vpatches, const index_t p, const fmask_t & mask) +void patch::reset_xyz_disjoint(che * mesh, float * dist, size_t M, std::vector & vpatches, const index_t p, const fmask_t & mask) { size_t m = size(vertices); if(mask) @@ -487,16 +487,16 @@ void patch::reset_xyz_disjoint(che * mesh, real_t * dist, size_t M, std::vector< } } -void patch::scale_xyz(const real_t radio_f) +void patch::scale_xyz(const float radio_f) { - real_t factor = radio_f/radio; + float factor = radio_f/radio; xyz = factor * xyz; } -void patch::iscale_xyz(const real_t radio_f) +void patch::iscale_xyz(const float radio_f) { - real_t factor = radio_f/radio; + float factor = radio_f/radio; xyz = xyz / factor; } @@ -530,14 +530,14 @@ void patch::gather_vertices(che * mesh, const index_t v, const size_t n_toplevel } } -void patch::gather_vertices(che * mesh, const index_t v, const real_t radio, index_t * toplevel) +void patch::gather_vertices(che * mesh, const index_t v, const float radio, index_t * toplevel) { assert(x.n_elem == 3 && T.n_rows == 3 && T.n_cols == 3); if(size(vertices)) vertices.clear(); vertices.reserve(expected_nv); - std::priority_queue > qvertices; + std::priority_queue > qvertices; memset(toplevel, -1, sizeof(index_t) * mesh->n_vertices); @@ -609,19 +609,19 @@ void patch::normal_fit_directions(che * mesh, const index_t v) } -real_t patch::get_min_z() +float patch::get_min_z() { return xyz.row(2).min(); } -real_t patch::get_max_z() +float patch::get_max_z() { return xyz.row(2).max(); } -void patch::update_heights(real_t & min, real_t & max, bool flag) +void patch::update_heights(float & min, float & max, bool flag) { - real_t tmp; + float tmp; if(flag) { for(index_t i = 0; i < xyz.n_cols; ++i) diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index 58981b87..d970c48c 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -94,7 +94,7 @@ che::che(const che & mesh, const index_t * sorted, const che::options & opts) if(!sorted) { memcpy(VC, mesh.VC, n_vertices * sizeof(rgb_t)); - memcpy(VHC, mesh.VHC, n_vertices * sizeof(real_t)); + memcpy(VHC, mesh.VHC, n_vertices * sizeof(float)); } else { @@ -130,7 +130,7 @@ void che::reload() init(filename); } -mat4 che::normalize_sphere(const real_t r) const +mat4 che::normalize_sphere(const float r) const { vertex center; #pragma omp parallel for @@ -141,24 +141,24 @@ mat4 che::normalize_sphere(const real_t r) const } center /= n_vertices; - real_t mean_dist = 0; + float mean_dist = 0; #pragma omp parallel for reduction(+: mean_dist) for(index_t v = 0; v < n_vertices; ++v) mean_dist += length(GT[v] - center); mean_dist /= n_vertices; - real_t sigma_dist = 0; + float sigma_dist = 0; #pragma omp parallel for reduction(+: sigma_dist) for(index_t v = 0; v < n_vertices; ++v) { - const real_t diff = mean_dist - length(GT[v] - center); + const float diff = mean_dist - length(GT[v] - center); sigma_dist += diff * diff; } sigma_dist = sqrt(sigma_dist / n_vertices); mat4 model_mat; - const real_t scale = r / (mean_dist + sigma_dist); + const float scale = r / (mean_dist + sigma_dist); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; center *= -scale; @@ -170,7 +170,7 @@ mat4 che::normalize_sphere(const real_t r) const return model_mat; } -mat4 che::normalize_box(const real_t side) const +mat4 che::normalize_box(const float side) const { vertex pmin = INFINITY; vertex pmax = 0; @@ -190,7 +190,7 @@ mat4 che::normalize_box(const real_t side) const mat4 model_mat; - const real_t scale = side / std::max({pmax.x() - pmin.x(), pmax.y() - pmin.y(), pmax.z() - pmin.z()}); + const float scale = side / std::max({pmax.x() - pmin.x(), pmax.y() - pmin.y(), pmax.z() - pmin.z()}); model_mat(0, 0) = model_mat(1, 1) = model_mat(2, 2) = scale; const vertex & translate = - scale * (pmax + pmin) / 2; @@ -216,8 +216,8 @@ che * che::merge(const che * mesh, const std::vector & vcommon) memcpy(new_mesh->VN + n_vertices, mesh->VN + n_vcommon, sizeof(vertex) * n_vnew); memcpy(new_mesh->VC, VC, sizeof(rgb_t) * n_vertices); memcpy(new_mesh->VC + n_vertices, mesh->VC + n_vcommon, sizeof(rgb_t) * n_vnew); - memcpy(new_mesh->VHC, VHC, sizeof(real_t) * n_vertices); - memcpy(new_mesh->VHC + n_vertices, mesh->VHC + n_vcommon, sizeof(real_t) * n_vnew); + memcpy(new_mesh->VHC, VHC, sizeof(float) * n_vertices); + memcpy(new_mesh->VHC + n_vertices, mesh->VHC + n_vcommon, sizeof(float) * n_vnew); memcpy(new_mesh->VT, VT, sizeof(index_t) * n_half_edges); @@ -237,7 +237,7 @@ void che::update_vertices(const vertex * positions, const size_t n, const index_ memcpy(GT + v_i, positions, sizeof(vertex) * (!n ? n_vertices : n)); } -void che::update_heatmap(const real_t * hm) +void che::update_heatmap(const float * hm) { if(!hm) { @@ -248,7 +248,7 @@ void che::update_heatmap(const real_t * hm) return; } - memcpy(VHC, hm, n_vertices * sizeof(real_t)); + memcpy(VHC, hm, n_vertices * sizeof(float)); scale_hm = normalize(VHC, n_vertices); } @@ -494,7 +494,7 @@ vertex che::normal_he(const index_t he) const return normalize(cross(b - a, c - a)); } -vertex che::gradient_he(const index_t he, const real_t * f) const +vertex che::gradient_he(const index_t he, const float * f) const { index_t i = VT[he]; index_t j = VT[he_next(he)]; @@ -537,10 +537,10 @@ dvec3 che::gradient_he(const index_t he, const double * f) const return normalize(f[i] * pjk + f[j] * pki + f[k] * pij); } -vertex che::gradient(const index_t v, const real_t * f) +vertex che::gradient(const index_t v, const float * f) { vertex g; - real_t area, area_star = 0; + float area, area_star = 0; for(const index_t he: star(v)) { @@ -553,17 +553,17 @@ vertex che::gradient(const index_t v, const real_t * f) } -real_t che::heatmap_scale() const +float che::heatmap_scale() const { return scale_hm; } -void che::heatmap_scale(const real_t shm) +void che::heatmap_scale(const float shm) { scale_hm = shm; } -real_t che::heatmap_scale(const index_t v) const +float che::heatmap_scale(const index_t v) const { assert(v < n_vertices); return scale_hm * VHC[v]; @@ -769,7 +769,7 @@ size_t che::genus() const size_t che::memory() const { return sizeof(*this) + - n_vertices * (2 * sizeof(vertex) + sizeof(index_t) + sizeof(real_t) + sizeof(rgb_t)) + + n_vertices * (2 * sizeof(vertex) + sizeof(index_t) + sizeof(float) + sizeof(rgb_t)) + sizeof(index_t) * (3 * n_half_edges + n_edges) + size(filename); } @@ -790,9 +790,9 @@ size_t che::max_degree() const return md; } -real_t che::quality() const +float che::quality() const { - real_t q = 0; + float q = 0; #pragma omp parallel for reduction(+: q) for(index_t t = 0; t < n_trigs; ++t) @@ -801,9 +801,9 @@ real_t che::quality() const return q * 100 / n_trigs; } -real_t che::mean_edge() const +float che::mean_edge() const { - real_t m = 0; + float m = 0; #pragma omp parallel for reduction(+: m) for(index_t e = 0; e < n_edges; ++e) @@ -812,9 +812,9 @@ real_t che::mean_edge() const return m / n_edges; } -real_t che::area_surface() const +float che::area_surface() const { - real_t area = 0; + float area = 0; #pragma omp parallel for reduction(+: area) for(index_t i = 0; i < n_trigs; ++i) @@ -895,7 +895,7 @@ void che::flip(const index_t e) if(EVT[vd] == he_prev(hb)) EVT[vd] = he_next(ha); } -real_t che::cotan(const index_t he) const +float che::cotan(const index_t he) const { if(he == NIL) return 0; @@ -909,10 +909,10 @@ real_t che::cotan(const index_t he) const // 4*sqrt(3)*a // q = ---------------- // h1^2+h2^2+h3^2 -real_t che::pdetriq(const index_t t) const +float che::pdetriq(const index_t t) const { index_t he = t * 3; - real_t h[3] = { + float h[3] = { norm(GT[VT[he_next(he)]] - GT[VT[he]]), norm(GT[VT[he_prev(he)]] - GT[VT[he_next(he)]]), norm(GT[VT[he]] - GT[VT[he_prev(he)]]) @@ -920,7 +920,7 @@ real_t che::pdetriq(const index_t t) const return (4 * sqrt(3) * area_trig(t)) / (h[0] * h[0] + h[1] * h[1] + h[2] * h[2]); } -real_t che::area_trig(const index_t t) const +float che::area_trig(const index_t t) const { index_t he = t * 3; vertex a = GT[VT[he_next(he)]] - GT[VT[he]]; @@ -929,9 +929,9 @@ real_t che::area_trig(const index_t t) const return norm(cross(a, b)) / 2; } -real_t che::area_vertex(const index_t v) const +float che::area_vertex(const index_t v) const { - real_t area_star = 0; + float area_star = 0; for(const index_t he: star(v)) area_star += area_trig(he_trig(he)); @@ -939,10 +939,10 @@ real_t che::area_vertex(const index_t v) const } // The Gauss-Bonnet Scheme -real_t che::mean_curvature(const index_t v) const +float che::mean_curvature(const index_t v) const { - real_t h = 0; - real_t a = 0; + float h = 0; + float a = 0; for(const index_t he: star(v)) { @@ -1004,7 +1004,7 @@ bool che::alloc(const size_t n_v, const size_t n_f, const che::options & opt) if(opt.colors) { VC = new rgb_t[n_vertices]; - VHC = new real_t[n_vertices]; + VHC = new float[n_vertices]; update_heatmap(); } diff --git a/src/gproshan/mesh/che_cuda.cpp b/src/gproshan/mesh/che_cuda.cpp index 67cb901b..a93a15ce 100644 --- a/src/gproshan/mesh/che_cuda.cpp +++ b/src/gproshan/mesh/che_cuda.cpp @@ -42,8 +42,8 @@ che_cuda::che_cuda(const che * mesh, const che::options & opts) cudaMalloc(&VC, sizeof(che::rgb_t) * n_vertices); cudaMemcpy(VC, mesh->VC, sizeof(che::rgb_t) * n_vertices, cudaMemcpyHostToDevice); - cudaMalloc(&VHC, sizeof(real_t) * n_vertices); - cudaMemcpy(VHC, mesh->VHC, sizeof(real_t) * n_vertices, cudaMemcpyHostToDevice); + cudaMalloc(&VHC, sizeof(float) * n_vertices); + cudaMemcpy(VHC, mesh->VHC, sizeof(float) * n_vertices, cudaMemcpyHostToDevice); } cudaMalloc(&device, sizeof(che)); diff --git a/src/gproshan/mesh/che_fill_hole.cpp b/src/gproshan/mesh/che_fill_hole.cpp index 3ce7ad19..282c3fd9 100644 --- a/src/gproshan/mesh/che_fill_hole.cpp +++ b/src/gproshan/mesh/che_fill_hole.cpp @@ -72,11 +72,11 @@ che * mesh_fill_hole(che * mesh, const std::vector & border_vertices, c index_t * vmap_border = new index_t[nb]; index_t c = 1; - real_t mean_edge = mesh->mean_edge(); + float mean_edge = mesh->mean_edge(); auto gen_vertices = [&mean_edge](std::vector & merge_vertices, std::vector & vertices, const vertex & va, const vertex & vb, const index_t delta_v = 0) { - real_t L = length(va - vb); + float L = length(va - vb); size_t N = L / mean_edge; L = N; @@ -235,7 +235,7 @@ void split_border(std::vector > & , che * mesh, cons a = NIL; b = NIL; // review this for(index_t i = 0; i < n; ++i) { - real_t d, d_min = INFINITY; + float d, d_min = INFINITY; for(index_t c = 0; c < k; ++c) { d = norm(data.col(i) - means.col(c)); @@ -327,9 +327,9 @@ std::tuple *, che **> fill_all_holes_meshes(che * mesh, con che * fill_hole_front_angles_test(che * mesh, std::vector & front_vertices, size_t p_iter, bool & is_grow) { gproshan_debug(filling holes); - real_t perimeter = 0.0, init_perimeter = 0.0; + float perimeter = 0.0, init_perimeter = 0.0; - real_t length = mesh->mean_edge(); + float length = mesh->mean_edge(); std::priority_queue front; std::vector vertices; @@ -386,8 +386,8 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti border_t top; - real_t a75 = 75.0 * M_PI / 180; - real_t a135 = 135.0 * M_PI / 180; + float a75 = 75.0 * M_PI / 180; + float a135 = 135.0 * M_PI / 180; arma::fvec m_vec; arma::fvec m_normal; @@ -564,11 +564,11 @@ che * fill_hole_front_angles_test(che * mesh, std::vector & front_verti return size(trigs) == 0 ? nullptr : new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3); } -che * fill_hole_front_angles(std::vector & vertices, const real_t length, const vertex & normal, const size_t max_iter, bool is_grow) +che * fill_hole_front_angles(std::vector & vertices, const float length, const vertex & normal, const size_t max_iter, bool is_grow) { size_t p_iter = max_iter; - real_t perimeter = 0.0; - real_t init_perimeter = 0.0; + float perimeter = 0.0; + float init_perimeter = 0.0; std::priority_queue front; std::vector trigs; @@ -653,8 +653,8 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length border_t top; - real_t a75 = 75.0 * M_PI / 180; - real_t a135 = 135.0 * M_PI / 180; + float a75 = 75.0 * M_PI / 180; + float a135 = 135.0 * M_PI / 180; arma::fvec m_vec; while(!front.empty() && p_iter-- && p_iter < 2000) @@ -824,7 +824,7 @@ che * fill_hole_front_angles(std::vector & vertices, const real_t length return size(trigs) ? new che(vertices.data(), size(vertices), trigs.data(), size(trigs) / 3) : nullptr; } -void get_real_tri(che * mesh, std::vector & select_vertices, std::vector & triangle, std::vector & tri_sizes ) +void get_floatri(che * mesh, std::vector & select_vertices, std::vector & triangle, std::vector & tri_sizes ) { // Drawing a triangle in the middle of the border size_t div = size(select_vertices) / 3; @@ -859,11 +859,11 @@ void get_real_tri(che * mesh, std::vector & select_vertices, std::vecto } } - real_t weight = 1.8; + float weight = 1.8; - real_t wp = weight / size(select_vertices); - real_t aux = wp * tri_sizes[0]; - real_t wo = (1 - aux) / ( tri_sizes[1] + tri_sizes[2] ); + float wp = weight / size(select_vertices); + float aux = wp * tri_sizes[0]; + float wo = (1 - aux) / ( tri_sizes[1] + tri_sizes[2] ); triangle.push_back( (wp * tri[0]) + wo * (tri[1] + tri[2]) ); @@ -889,7 +889,7 @@ che * fill_hole_center_triangle(che * mesh, std::vector & select_vertic std::vector triangle; std::vector tri_sizes(3,0); - get_real_tri(mesh, select_vertices, triangle, tri_sizes); + get_floatri(mesh, select_vertices, triangle, tri_sizes); index_t i = 0; for(index_t v: select_vertices) diff --git a/src/gproshan/mesh/che_img.cpp b/src/gproshan/mesh/che_img.cpp index 5b9cfeb7..38923663 100644 --- a/src/gproshan/mesh/che_img.cpp +++ b/src/gproshan/mesh/che_img.cpp @@ -44,7 +44,7 @@ void che_img::read_file(const std::string & file) const int c = img.width() - i - 1; const int r = img.height() - j - 1; - GT[v] = {real_t(i), real_t(j), real_t(img.spectrum() == 1 ? - img(c, r) : 0)}; + GT[v] = {float(i), float(j), float(img.spectrum() == 1 ? - img(c, r) : 0)}; if(img.spectrum() == 3) VC[v] = {img(c, r, 0), img(c, r, 1), img(c, r, 2)}; diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index bb34eccc..06948b55 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -178,9 +178,9 @@ void che_ply::read_file(const std::string & file) for(index_t i = 0; i < 3; ++i) { if(xyz == 4) - GT[v][i] = (real_t) *(float *) (pb + ixyz + i * xyz); + GT[v][i] = (float) *(float *) (pb + ixyz + i * xyz); else - GT[v][i] = (real_t) *(double *) (pb + ixyz + i * xyz); + GT[v][i] = (float) *(double *) (pb + ixyz + i * xyz); } if(rgb) @@ -197,9 +197,9 @@ void che_ply::read_file(const std::string & file) for(index_t i = 0; i < 3; ++i) { if(normal == 4) - VN[v][i] = (real_t) *(float *) (pb + inormal + i * normal); + VN[v][i] = (float) *(float *) (pb + inormal + i * normal); else - VN[v][i] = (real_t) *(double *) (pb + inormal + i * normal); + VN[v][i] = (float) *(double *) (pb + inormal + i * normal); } } } @@ -257,7 +257,7 @@ void che_ply::write_file(const che * mesh, const std::string & file, const bool FILE * fp = fopen((file + ".ply").c_str(), "wb"); assert(fp); - const char * type = sizeof(real_t) == 4 ? "float" : "double"; + const char * type = sizeof(float) == 4 ? "float" : "double"; fprintf(fp, "ply\n"); fprintf(fp, "format binary_little_endian 1.0\n"); diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index b441ac5b..c706fa05 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -32,7 +32,7 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) B.row(i) *= -1 / A(i,i); arma::sp_fmat M; - real_t s = (k % 2) ? -1 : 1; + float s = (k % 2) ? -1 : 1; if(k > 1) M = A * L; while(--k) L *= M; @@ -63,7 +63,7 @@ void poisson(che * mesh, const size_t old_n_vertices, index_t k) void biharmonic_interp_2(arma::fmat & P, arma::fmat & H) { size_t n = P.n_cols; - real_t x; + float x; arma::fmat A(n, n); arma::fvec pi(2), pj(2); diff --git a/src/gproshan/mesh/che_ptx.cpp b/src/gproshan/mesh/che_ptx.cpp index 3e83f54f..59c4bce4 100644 --- a/src/gproshan/mesh/che_ptx.cpp +++ b/src/gproshan/mesh/che_ptx.cpp @@ -140,7 +140,7 @@ void che_ptx::write_file(const che * mesh, const std::string & file, const size_ { const vertex & v = mesh->point(i); const rgb_t c = mesh->rgb(i); - const real_t h = mesh->heatmap(i); + const float h = mesh->heatmap(i); fprintf(fp, "%f %f %f %f %hhu %hhu %hhu\n", (float) v.x(), (float) v.y(), (float) v.z(), (float) h, c.r, c.g, c.b ); } diff --git a/src/gproshan/mesh/che_sphere.cpp b/src/gproshan/mesh/che_sphere.cpp index 2eca3b4b..5d44a107 100644 --- a/src/gproshan/mesh/che_sphere.cpp +++ b/src/gproshan/mesh/che_sphere.cpp @@ -10,17 +10,17 @@ namespace gproshan { -che_sphere::che_sphere(const real_t r, const size_t n) +che_sphere::che_sphere(const float r, const size_t n) { filename = "sphere"; std::vector vertices; std::vector trigs; - const real_t delta = M_PI / n; + const float delta = M_PI / n; - for(real_t phi = 0; phi < 2 * M_PI - 0.5 * delta; phi += delta) - for(real_t theta = delta; theta < M_PI - 0.5 * delta; theta += delta) + for(float phi = 0; phi < 2 * M_PI - 0.5 * delta; phi += delta) + for(float theta = delta; theta < M_PI - 0.5 * delta; theta += delta) vertices.push_back({r * std::sin(theta) * std::cos(phi), r * std::sin(theta) * std::sin(phi), r * std::cos(theta)}); vertices.push_back({0, 0, r}); diff --git a/src/gproshan/mesh/che_xyz.cpp b/src/gproshan/mesh/che_xyz.cpp index ab57fc63..c3a292b2 100644 --- a/src/gproshan/mesh/che_xyz.cpp +++ b/src/gproshan/mesh/che_xyz.cpp @@ -23,11 +23,11 @@ void che_xyz::read_file(const std::string & file) float x, y, z; unsigned char r, g, b; size_t n; - real_t h; + float h; std::vector vertices; std::vector vertices_color; - std::vector vertices_hm; + std::vector vertices_hm; while(fgets(line, sizeof(line), fp)) { diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/mesh/quaternion.cpp index d4f54072..4a57826a 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/mesh/quaternion.cpp @@ -8,9 +8,9 @@ namespace gproshan { -quaternion::quaternion(real_t s_, real_t vi, real_t vj, real_t vk): s(s_), v{vi, vj, vk} {} +quaternion::quaternion(float s_, float vi, float vj, float vk): s(s_), v{vi, vj, vk} {} -quaternion::quaternion(real_t s_, const vertex & v_): s(s_), v(v_) {} +quaternion::quaternion(float s_, const vertex & v_): s(s_), v(v_) {} quaternion::quaternion(const vertex & v_): s(0), v(v_) {} @@ -19,7 +19,7 @@ quaternion::operator const vertex & () const return v; } -const quaternion & quaternion::operator = (real_t _s) +const quaternion & quaternion::operator = (float _s) { s = _s; v = {0, 0, 0}; @@ -36,22 +36,22 @@ const quaternion & quaternion::operator = (const vertex & _v) } -real_t & quaternion::operator [] (int index) +float & quaternion::operator [] (int index) { return v[index]; } -real_t quaternion::operator [] (int index) const +float quaternion::operator [] (int index) const { return v[index]; } -real_t & quaternion::re() +float & quaternion::re() { return s; } -real_t quaternion::re() const +float quaternion::re() const { return s; } @@ -81,17 +81,17 @@ quaternion quaternion::operator - () const return quaternion(-s, -v); } -quaternion quaternion::operator * (real_t c) const +quaternion quaternion::operator * (float c) const { return quaternion(c * s, c * v); } -quaternion operator * (real_t c, const quaternion & q) +quaternion operator * (float c, const quaternion & q) { return q * c; } -quaternion quaternion::operator / (real_t c) const +quaternion quaternion::operator / (float c) const { return quaternion(s / c, v / c); } @@ -102,7 +102,7 @@ void quaternion::operator += (const quaternion & q) v += q.v; } -void quaternion::operator += (real_t c) +void quaternion::operator += (float c) { s += c; } @@ -113,18 +113,18 @@ void quaternion::operator -= (const quaternion & q) v -= q.v; } -void quaternion::operator -= (real_t c) +void quaternion::operator -= (float c) { s -= c; } -void quaternion::operator *= (real_t c) +void quaternion::operator *= (float c) { s *= c; v *= c; } -void quaternion::operator /= (real_t c) +void quaternion::operator /= (float c) { s /= c; v /= c; @@ -133,8 +133,8 @@ void quaternion::operator /= (real_t c) // Hamilton product quaternion quaternion::operator * (const quaternion & q) const { - const real_t s1(s); - const real_t s2(q.s); + const float s1(s); + const float s2(q.s); const vertex & v1(v); const vertex & v2(q.v); @@ -156,12 +156,12 @@ quaternion quaternion::inv() const return (this->conj()) / this->norm2(); } -real_t quaternion::norm() const +float quaternion::norm() const { return sqrt(norm2()); } -real_t quaternion::norm2() const +float quaternion::norm2() const { return s * s + dot(v, v); } @@ -177,17 +177,17 @@ void quaternion::normalize() } // spherical-linear interpolation -quaternion slerp(const quaternion & q0, const quaternion & q1, real_t t) +quaternion slerp(const quaternion & q0, const quaternion & q1, float t) { // interpolate length - real_t m0 = q0.norm(); - real_t m1 = q1.norm(); - real_t m = (1-t)*m0 + t*m1; + float m0 = q0.norm(); + float m1 = q1.norm(); + float m = (1-t)*m0 + t*m1; // interpolate direction quaternion p0 = q0 / m0; quaternion p1 = q1 / m1; - real_t theta = acos((p0.conj() * p1).re()); + float theta = acos((p0.conj() * p1).re()); quaternion p = (sin((1 - t) * theta) * p0 + sin(t * theta) * p1) / sin(theta); return m * p; diff --git a/src/gproshan/mesh/simplification.cpp b/src/gproshan/mesh/simplification.cpp index 2ab6cec0..ba2aeb32 100644 --- a/src/gproshan/mesh/simplification.cpp +++ b/src/gproshan/mesh/simplification.cpp @@ -48,7 +48,7 @@ void simplification::compute_quadrics() } } -void simplification::order_edges(index_t * sort_edges, real_t * error_edges) +void simplification::order_edges(index_t * sort_edges, float * error_edges) { #pragma omp parallel for for(index_t e = 0; e < mesh->n_edges; ++e) @@ -65,7 +65,7 @@ void simplification::order_edges(index_t * sort_edges, real_t * error_edges) ); } -real_t simplification::compute_error(const index_t e) +float simplification::compute_error(const index_t e) { vertex ve = create_vertex(e); arma::fvec v(4); diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 56bcc6a5..618a99a6 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -37,7 +37,7 @@ std::vector grid::operator () (const point & p, int k) const { const uvec3 key = hash(p, res); - std::priority_queue > q; + std::priority_queue > q; for(int i = -1; i < 2; ++i) for(int j = -1; j < 2; ++j) @@ -80,8 +80,8 @@ k3tree::k3tree(const point * pc, const size_t n_points, const point * query, con double time_build, time_query; TIC(time_build); - flann::Matrix mpc((real_t *) pc, n_points, 3); - flann::Index > index(mpc, flann::KDTreeSingleIndexParams()); + flann::Matrix mpc((float *) pc, n_points, 3); + flann::Index > index(mpc, flann::KDTreeSingleIndexParams()); index.buildIndex(); TOC(time_build); gproshan_log_var(time_build); @@ -90,10 +90,10 @@ k3tree::k3tree(const point * pc, const size_t n_points, const point * query, con const point * q = query && n_query ? query : pc; const size_t n_results = query && n_query ? n_query : n_points; - flann::Matrix mq((real_t *) q, n_results, 3); + flann::Matrix mq((float *) q, n_results, 3); indices = flann::Matrix(new int[n_results * k], n_results, k); - flann::Matrix dists(new real_t[n_results * k], n_results, k); + flann::Matrix dists(new float[n_results * k], n_results, k); flann::SearchParams params; params.cores = 0; @@ -127,11 +127,11 @@ int k3tree::operator () (const index_t i, const index_t j) const } -real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree p2nn(pc, n_points, k + 1); - real_t mean = 0; + float mean = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -143,11 +143,11 @@ real_t mean_knn_distant(const point * pc, const size_t n_points, const size_t k, return mean / n_points; } -real_t median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - std::vector dist(n_points); + std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -158,11 +158,11 @@ real_t median_knn_distant(const point * pc, const size_t n_points, const size_t return dist[size(dist) >> 1]; } -real_t median_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float median_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - std::vector dist(n_points); + std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -174,11 +174,11 @@ real_t median_median_knn_distant(const point * pc, const size_t n_points, const } -real_t mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float mean_median_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - real_t mean = 0; + float mean = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -190,11 +190,11 @@ real_t mean_median_knn_distant(const point * pc, const size_t n_points, const si return mean / n_points; } -real_t median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float median_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - std::vector dist(n_points); + std::vector dist(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -205,11 +205,11 @@ real_t median_mean_knn_distant(const point * pc, const size_t n_points, const si return dist[size(dist) >> 1]; } -real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float mean_mean_knn_distant(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - real_t mean = 0; + float mean = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) @@ -221,16 +221,16 @@ real_t mean_mean_knn_distant(const point * pc, const size_t n_points, const size return mean / n_points; } -real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - real_t mean_r = 0; + float mean_r = 0; #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - real_t r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); + float r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); #pragma omp atomic mean_r += sqrt(r * r / k); @@ -241,16 +241,16 @@ real_t mean_knn_area_radius(const point * pc, const size_t n_points, const size_ return mean_r / n_points; } -real_t median_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float median_knn_area_radius(const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { k3tree nn(pc, n_points, k + 1); - std::vector radius(n_points); + std::vector radius(n_points); #pragma omp parallel for for(index_t i = 0; i < n_points; ++i) { - real_t r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); + float r = length(model_mat * (pc[i] - pc[nn(i, k)], 0)); radius[i] = sqrt(r * r / k); } @@ -261,9 +261,9 @@ real_t median_knn_area_radius(const point * pc, const size_t n_points, const siz } -real_t median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat) +float median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat) { - std::vector dist; + std::vector dist; dist.reserve((n * (n - 1)) >> 1); for(index_t i = 0; i < n; ++i) @@ -275,9 +275,9 @@ real_t median_pair_dist(const point * pc, const int * id, const size_t n, const return dist[size(dist) >> 1]; } -real_t mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat) +float mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat) { - real_t mean = 0; + float mean = 0; for(index_t i = 1; i <= n; ++i) mean += length(model_mat * (pc[id[0]] - pc[id[i]], 0)); @@ -301,9 +301,9 @@ const char * radius_str(void *, int opt) return str[opt]; } -real_t radius(const int opt, const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) +float radius(const int opt, const point * pc, const size_t n_points, const size_t k, const mat4 & model_mat) { - using fknn = real_t (*) (const point *, const size_t, const size_t, const mat4 &); + using fknn = float (*) (const point *, const size_t, const size_t, const mat4 &); static const fknn f[] = { nullptr, mean_knn_distant, median_knn_distant, diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 07c39cb0..a5893e88 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -217,7 +217,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) { - real_t r = 0; + float r = 0; switch(pc.opt) { @@ -274,7 +274,7 @@ bool embree::closesthit_radiance( vertex & color, vertex & position, vertex & ray_dir, float &, - random & rnd, + random & rnd, const render_params & params, const bool flat ) const diff --git a/src/gproshan/raytracing/raytracing.cpp b/src/gproshan/raytracing/raytracing.cpp index 07f5138d..6aa647f9 100644 --- a/src/gproshan/raytracing/raytracing.cpp +++ b/src/gproshan/raytracing/raytracing.cpp @@ -25,7 +25,7 @@ void raytracing::render(vec4 * img, const render_params & params, const bool fla { const uvec2 & pos = params.viewport_pos + uvec2{i, j}; - random rnd(pos.x() + window_size.x() * pos.y(), params.n_frames); + random rnd(pos.x() + window_size.x() * pos.y(), params.n_frames); color_acc = 0; @@ -69,7 +69,7 @@ std::vector raytracing::raycaster( const uvec2 & windows_size, for(unsigned int i = 0; i < windows_size.x(); ++i) for(unsigned int j = 0; j < windows_size.y(); ++j) { - random rnd(i, j); + random rnd(i, j); //row major float & color = frame[(windows_size.y() - j - 1) * windows_size.x() + i] = 0; diff --git a/src/gproshan/scenes/scanner.cpp b/src/gproshan/scenes/scanner.cpp index 05b96f89..d9f1f1f4 100644 --- a/src/gproshan/scenes/scanner.cpp +++ b/src/gproshan/scenes/scanner.cpp @@ -23,8 +23,8 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n { che * mesh_ptx = new che(n_cols * n_rows); - const real_t delta_phi = (2 * M_PI) / n_rows; - const real_t delta_theta = M_PI / n_cols; + const float delta_phi = (2 * M_PI) / n_rows; + const float delta_theta = M_PI / n_cols; #pragma omp parallel for for(index_t i = 0; i < n_rows; ++i) @@ -32,8 +32,8 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n { const index_t v = i * n_cols + j; - const real_t phi = i * delta_phi; - const real_t theta = j * delta_theta; + const float phi = i * delta_phi; + const float theta = j * delta_theta; const vertex & dir = {std::sin(theta) * std::sin(phi), std::cos(theta), std::sin(theta) * std::cos(phi)}; const rt::eval_hit & h = rt->intersect(cam_pos, dir); @@ -44,7 +44,7 @@ che * scanner_ptx(const rt::raytracing * rt, const size_t n_rows, const size_t n mesh_ptx->rgb(v) = h.Kd; } - real_t max_dist = 0; + float max_dist = 0; #pragma omp parallel for reduction(max: max_dist) for(index_t v = 0; v < mesh_ptx->n_vertices; ++v) diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index a01ea517..2ca9c7ae 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -134,20 +134,20 @@ bool scene::load_mtl(const std::string & file) } case 'd': // d { - real_t & d = materials.back().d; + float & d = materials.back().d; sscanf(line, "%*s %f", &d); break; } case 'T': // Tr { - real_t & d = materials.back().d; + float & d = materials.back().d; sscanf(line, "%*s %f", &d); d = 1 - d; break; } case 'N': // Ns { - real_t & N = str[1] == 's' ? materials.back().Ns : materials.back().Ni; + float & N = str[1] == 's' ? materials.back().Ns : materials.back().Ni; sscanf(line, "%*s %f", &N); break; } diff --git a/src/gproshan/util.cpp b/src/gproshan/util.cpp index 331ecbce..210909b2 100644 --- a/src/gproshan/util.cpp +++ b/src/gproshan/util.cpp @@ -39,30 +39,5 @@ partitions::operator index_t *& () } -void copy_real_t_array(float * destination, const float * source, const size_t n_elem) -{ - memcpy(destination, source, n_elem * sizeof(float)); -} - -void copy_real_t_array(float * destination, const double * source, const size_t n_elem) -{ - #pragma omp parallel for - for(index_t i = 0; i < n_elem; ++i) - destination[i] = source[i]; -} - -void copy_real_t_array(double * destination, const float * source, const size_t n_elem) -{ - #pragma omp parallel for - for(index_t i = 0; i < n_elem; ++i) - destination[i] = source[i]; -} - -void copy_real_t_array(double * destination, const double * source, const size_t n_elem) -{ - memcpy(destination, source, n_elem * sizeof(double)); -} - - } // namespace gproshan diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index d399f88b..8fd4f96e 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -31,9 +31,9 @@ mat4 camera::perspective() return perspective(fovy, aspect, near, far); } -mat4 camera::perspective(const real_t fovy, const real_t aspect, const real_t near, const real_t far) +mat4 camera::perspective(const float fovy, const float aspect, const float near, const float far) { - const real_t tan_fovy_2 = std::tan(fovy * M_PI / 360); + const float tan_fovy_2 = std::tan(fovy * M_PI / 360); mat4 P; P(0, 0) = 1 / (aspect * tan_fovy_2); @@ -96,7 +96,7 @@ void camera::zoom_out() pos.v.z() -= 0.02; } -real_t camera::zoom() const +float camera::zoom() const { return -pos.v.z(); } diff --git a/src/gproshan/viewer/che_viewer.cpp b/src/gproshan/viewer/che_viewer.cpp index e629a28c..e3500f02 100644 --- a/src/gproshan/viewer/che_viewer.cpp +++ b/src/gproshan/viewer/che_viewer.cpp @@ -88,7 +88,7 @@ void che_viewer::update_vbo_geometry() glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), &mesh->point(0), GL_STATIC_DRAW); glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); // 4 INDEXES @@ -112,7 +112,7 @@ void che_viewer::update_vbo_normal(const vertex * vnormal) glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vertex), vnormal, GL_STATIC_DRAW); glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 3, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -134,7 +134,7 @@ void che_viewer::update_vbo_color(const che::rgb_t * vcolor) glBindVertexArray(0); } -void che_viewer::update_vbo_heatmap(const real_t * vheatmap) +void che_viewer::update_vbo_heatmap(const float * vheatmap) { if(!vheatmap) vheatmap = &mesh->heatmap(0); @@ -142,9 +142,9 @@ void che_viewer::update_vbo_heatmap(const real_t * vheatmap) // 3 HEAT MAP glBindBuffer(GL_ARRAY_BUFFER, vbo[3]); - glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(real_t), vheatmap, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(float), vheatmap, GL_STATIC_DRAW); glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 1, GL_REAL, GL_TRUE, 0, 0); + glVertexAttribPointer(3, 1, GL_FLOAT, GL_TRUE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -161,7 +161,7 @@ void che_viewer::update_instances_positions(const std::vector & translat glBindBuffer(GL_ARRAY_BUFFER, vbo[5]); glBufferData(GL_ARRAY_BUFFER, n_instances * sizeof(vertex), translations.data(), GL_STATIC_DRAW); glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 3, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glVertexAttribDivisor(3, 1); @@ -236,7 +236,7 @@ void che_viewer::draw_selected_vertices(che_viewer & sphere, shader & program) void che_viewer::select(const uvec2 & pos, const uvec2 & windows_size, const mat4 & inv_proj_view_mat, const vertex & cam_pos) { - rt::random rnd(pos.x(), pos.y()); + rt::random rnd(pos.x(), pos.y()); const vertex & dir = rt::ray_view_dir({pos.x(), windows_size.y() - pos.y()}, windows_size, inv_proj_view_mat, cam_pos, rnd); const index_t v = rt_embree->closest_vertex(cam_pos, dir); diff --git a/src/gproshan/viewer/scene_viewer.cpp b/src/gproshan/viewer/scene_viewer.cpp index 1ef90abd..075d23e8 100644 --- a/src/gproshan/viewer/scene_viewer.cpp +++ b/src/gproshan/viewer/scene_viewer.cpp @@ -158,7 +158,7 @@ void scene_viewer::update_vbo_texcoords() glBindBuffer(GL_ARRAY_BUFFER, tex_vbo); glBufferData(GL_ARRAY_BUFFER, mesh->n_vertices * sizeof(vec2), sc->texcoords, GL_STATIC_DRAW); glEnableVertexAttribArray(4); - glVertexAttribPointer(4, 2, GL_REAL, GL_FALSE, 0, 0); + glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 33bea2a3..3d83c6d4 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -74,7 +74,7 @@ viewer::viewer(const char * title, const int width, const int height) init_menus(); info_gl(); - gproshan_log_var(sizeof(real_t)); + gproshan_log_var(sizeof(float)); sphere_data.update_normals(); sphere = new che_viewer(&sphere_data);; @@ -259,12 +259,12 @@ void viewer::imgui() ImGui::Unindent(); } - static real_t pos_min = -10; - static real_t pos_max = 10; + static float pos_min = -10; + static float pos_max = 10; if(ImGui::CollapsingHeader("Camera")) { ImGui::Indent(); - ImGui::SliderScalarN("position", ImGuiDataType_Real, &cam.pos[0], 3, &pos_min, &pos_max); + ImGui::SliderScalarN("position", ImGuiDataType_Float, &cam.pos[0], 3, &pos_min, &pos_max); ImGui::Unindent(); } @@ -286,7 +286,7 @@ void viewer::imgui() light & l = render_params.lights[i]; snprintf(slight, sizeof(slight), "light_%d.pos", i); - update |= ImGui::SliderScalarN(slight, ImGuiDataType_Real, &l.pos, 3, &pos_min, &pos_max); + update |= ImGui::SliderScalarN(slight, ImGuiDataType_Float, &l.pos, 3, &pos_min, &pos_max); snprintf(slight, sizeof(slight), "light_%d.color", i); update |= ImGui::ColorEdit3(slight, (float *) &l.color); @@ -613,7 +613,7 @@ void viewer::update_viewport_meshes() glfwGetFramebufferSize(window, (int *) &viewport_width, (int *) &viewport_height); viewport_width /= cols; viewport_height /= rows; - cam.aspect = real_t(viewport_width) / viewport_height; + cam.aspect = float(viewport_width) / viewport_height; proj_mat = cam.perspective(); } @@ -657,7 +657,7 @@ void viewer::framebuffer_size_callback(GLFWwindow * window, int width, int heigh viewer * view = (viewer *) glfwGetWindowUserPointer(window); view->viewport_width = width / m_window_split[size(view->meshes)].y(); view->viewport_height = height / m_window_split[size(view->meshes)].x(); - view->cam.aspect = real_t(view->viewport_width) / view->viewport_height; + view->cam.aspect = float(view->viewport_width) / view->viewport_height; view->proj_mat = view->cam.perspective(); } From 10d3c6a2be7dc4f9eaf4e5e1138d1a666089de35 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 26 Mar 2024 13:05:17 +0100 Subject: [PATCH 0988/1018] geodesics: update heat method gpu --- include/gproshan/geodesics/heat_method.h | 18 +++++++++--------- src/gproshan/geodesics/heat_method.cpp | 2 +- src/gproshan/geodesics/heat_method.cu | 24 +++++++----------------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/include/gproshan/geodesics/heat_method.h b/include/gproshan/geodesics/heat_method.h index 73898cc9..25b366e3 100644 --- a/include/gproshan/geodesics/heat_method.h +++ b/include/gproshan/geodesics/heat_method.h @@ -18,15 +18,15 @@ // geometry processing and shape analysis framework namespace gproshan { -enum heat_method_opt { - HEAT_ARMA, - HEAT_CHOLMOD, - #ifdef GPROSHAN_CUDA - HEAT_CUDA - #endif // GPROSHAN_CUDA - }; +enum hm_opt { + HEAT_ARMA, + HEAT_CHOLMOD, + #ifdef GPROSHAN_CUDA + HEAT_CUDA + #endif // GPROSHAN_CUDA + }; -double heat_method(float * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt); +double heat_method(float * dist, const che * mesh, const std::vector & sources, const hm_opt & opt); arma::vec compute_divergence(const che * mesh, const arma::vec & u); @@ -34,7 +34,7 @@ arma::vec compute_divergence(const che * mesh, const arma::vec & u); #ifdef GPROSHAN_CUDA double solve_positive_definite_gpu(arma::mat & x, const arma::sp_mat & A, const arma::mat & b); -double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx, const bool host = 0); +double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx); #endif // GPROSHAN_CUDA diff --git a/src/gproshan/geodesics/heat_method.cpp b/src/gproshan/geodesics/heat_method.cpp index f0345a9e..fb4b7bd1 100644 --- a/src/gproshan/geodesics/heat_method.cpp +++ b/src/gproshan/geodesics/heat_method.cpp @@ -15,7 +15,7 @@ namespace gproshan { /// base on the code https://github.com/larc/dgpdec-course/tree/master/Geodesics double solve_positive_definite(arma::mat & x, const arma::sp_mat & A, const arma::mat & b, cholmod_common * context); -double heat_method(float * dist, const che * mesh, const std::vector & sources, const heat_method_opt & opt) +double heat_method(float * dist, const che * mesh, const std::vector & sources, const hm_opt & opt) { if(!size(sources)) return 0; diff --git a/src/gproshan/geodesics/heat_method.cu b/src/gproshan/geodesics/heat_method.cu index 44b22e97..326190dc 100644 --- a/src/gproshan/geodesics/heat_method.cu +++ b/src/gproshan/geodesics/heat_method.cu @@ -39,7 +39,7 @@ struct cu_spAxb } }; -double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx, const bool host) +double solve_positive_definite_cusolver(const int m, const int nnz, const double * hA_values, const int * hA_col_ptrs, const int * hA_row_indices, const double * hb, double * hx) { cudaDeviceReset(); @@ -61,24 +61,14 @@ double solve_positive_definite_cusolver(const int m, const int nnz, const double cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL); cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO); - if(host) - { - cusolverSpDcsrlsvcholHost(handle_cusolver, m, nnz, descr, hA_values, hA_col_ptrs, hA_row_indices, hb, 0, 0, hx, &singularity); - } - else - { - // allocate A, x, b into device - cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); + cu_spAxb data(m, nnz, hA_values, hA_col_ptrs, hA_row_indices, hb); - cusolverStatus_t status = cusolverSpDcsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); + cusolverStatus_t status = cusolverSpDcsrlsvchol(handle_cusolver, m, nnz, descr, data.A_values, data.A_col_ptrs, data.A_row_indices, data.b, 0, 0, data.x, &singularity); - if(status == CUSOLVER_STATUS_SUCCESS) - cudaMemcpy(hx, data.x, m * sizeof(double), cudaMemcpyDeviceToHost); - else - memset(hx, 0, m * sizeof(double)); - } - -// printf("%d\n", singularity != -1); + if(status == CUSOLVER_STATUS_SUCCESS) + cudaMemcpy(hx, data.x, m * sizeof(double), cudaMemcpyDeviceToHost); + else + memset(hx, 0, m * sizeof(double)); cusparseDestroyMatDescr(descr); cusolverSpDestroy(handle_cusolver); From c1a70bdb5d5f9408269ebccafca8d87fb2f1b93a Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 26 Mar 2024 17:50:05 +0100 Subject: [PATCH 0989/1018] viewer: check window creation --- include/gproshan/viewer/viewer.h | 2 +- src/gproshan/viewer/shader.cpp | 2 +- src/gproshan/viewer/viewer.cpp | 23 +++++++++++++++-------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/gproshan/viewer/viewer.h b/include/gproshan/viewer/viewer.h index 238e8707..c68c49cc 100644 --- a/include/gproshan/viewer/viewer.h +++ b/include/gproshan/viewer/viewer.h @@ -111,7 +111,7 @@ class viewer virtual bool run(); void info_gl(); - void init_gl(const char * title); + bool init_gl(const char * title); void init_imgui(); void init_menus(); void init_glsl(); diff --git a/src/gproshan/viewer/shader.cpp b/src/gproshan/viewer/shader.cpp index d006e0ee..58c524c6 100644 --- a/src/gproshan/viewer/shader.cpp +++ b/src/gproshan/viewer/shader.cpp @@ -13,7 +13,7 @@ namespace gproshan { shader::~shader() { - glDeleteProgram(program); + if(program) glDeleteProgram(program); } shader::operator GLuint() const diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 3d83c6d4..98a75a56 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -68,13 +68,13 @@ viewer::viewer(const char * title, const int width, const int height) window_width = width; window_height = height; - init_gl(title); + if(!init_gl(title)) return; + init_glsl(); init_imgui(); init_menus(); info_gl(); - gproshan_log_var(sizeof(float)); sphere_data.update_normals(); sphere = new che_viewer(&sphere_data);; @@ -89,12 +89,15 @@ viewer::~viewer() update_status_message("frametime_%p", this); save_frametime(tmp_file_path(status_message)); - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); + if(window) + { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); - glfwDestroyWindow(window); - glfwTerminate(); + glfwDestroyWindow(window); + glfwTerminate(); + } delete sphere; @@ -399,7 +402,7 @@ void error_callback(int error, const char* description) fprintf(stderr, "Error %d: %s\n", error, description); } -void viewer::init_gl(const char * title) +bool viewer::init_gl(const char * title) { glfwSetErrorCallback(error_callback); @@ -416,6 +419,8 @@ void viewer::init_gl(const char * title) gproshan_log_var(window_height); window = glfwCreateWindow(window_width, window_height, title, NULL, NULL); + if(!window) return false; + glfwSetWindowUserPointer(window, this); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); @@ -434,6 +439,8 @@ void viewer::init_gl(const char * title) glEnable(GL_BLEND); glEnable(GL_PROGRAM_POINT_SIZE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + return true; } void viewer::init_imgui() From 4640dca7967d2f7748ce8a53ff01e6f098d84a26 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 27 Mar 2024 08:42:33 +0100 Subject: [PATCH 0990/1018] mesh: adding toplesets class topological level sets (rings) --- include/gproshan/geodesics/geodesics_ptp.h | 1 + include/gproshan/mesh/che.h | 7 +- include/gproshan/mesh/toplesets.h | 80 ++++++++++++++++++++++ include/gproshan/util.h | 5 +- src/gproshan/app_viewer.cpp | 19 ++--- src/gproshan/geodesics/geodesics.cpp | 16 ++--- src/gproshan/geodesics/geodesics_ptp.cu | 14 ++-- src/gproshan/mesh/che.cpp | 40 ----------- src/gproshan/mesh/che_poisson.cpp | 15 ++-- 9 files changed, 102 insertions(+), 95 deletions(-) create mode 100644 include/gproshan/mesh/toplesets.h diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 6d9661db..4a5e9f9c 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -7,6 +7,7 @@ */ #include +#include #include diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 03f69a40..36e765f3 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -23,12 +23,8 @@ using vertex = vec3; class che { - protected: - - static size_t & rw(const size_t & n); - - public: + static size_t & rw(const size_t & n); struct options { @@ -277,7 +273,6 @@ class che std::vector link(const index_t v) const; void edge_collapse(const std::vector & sort_edges); - void compute_toplesets(index_t * rings, index_t * sorted, std::vector & limites, const std::vector & sources, const index_t k = NIL); std::vector bounds() const; std::vector boundary(const index_t v) const; diff --git a/include/gproshan/mesh/toplesets.h b/include/gproshan/mesh/toplesets.h new file mode 100644 index 00000000..2cc9277f --- /dev/null +++ b/include/gproshan/mesh/toplesets.h @@ -0,0 +1,80 @@ +#ifndef TOPLESETS_H +#define TOPLESETS_H + +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class toplesets: public partitions +{ + std::vector level; + + public: + const size_t n_levels = 0; + + public: + toplesets(const che * mesh, const std::vector & sources, const index_t max_level = NIL) + { + reset(mesh, sources, max_level); + } + + ~toplesets() + { + delete [] sorted; + } + + index_t operator [] (const index_t i) const + { + return level[i]; + } + + void reset(const che * mesh, const std::vector & sources, const index_t max_level = NIL) + { + if(std::size(level) < mesh->n_vertices) + { + delete [] sorted; + sorted = new index_t[mesh->n_vertices]; + } + level.assign(mesh->n_vertices, NIL); + + index_t l = 0; + index_t n = 0; + for(index_t s: sources) + { + sorted[n++] = s; + level[s] = l; + } + + splits.clear(); + splits.push_back(0); + for(index_t i = 0; i < n; ++i) + { + const index_t v = sorted[i]; + + if(level[v] > l) + { + if(++l > max_level) break; + splits.push_back(i); + } + + for(index_t u: mesh->link(v)) + if(level[u] == NIL) + { + level[u] = level[v] + 1; + sorted[n++] = u; + } + } + splits.push_back(n); + + che::rw(n_levels) = std::size(splits) - 1; + } +}; + + +} // namespace gproshan + +#endif // TOPLESETS_H + diff --git a/include/gproshan/util.h b/include/gproshan/util.h index 16218b86..9d82da80 100644 --- a/include/gproshan/util.h +++ b/include/gproshan/util.h @@ -15,8 +15,9 @@ class partitions { struct part; - std::vector splits; - index_t * sorted = nullptr; + public: + std::vector splits; + index_t * sorted = nullptr; public: partitions(index_t * s = nullptr); diff --git a/src/gproshan/app_viewer.cpp b/src/gproshan/app_viewer.cpp index dc12c183..a05ae37b 100644 --- a/src/gproshan/app_viewer.cpp +++ b/src/gproshan/app_viewer.cpp @@ -614,27 +614,16 @@ bool app_viewer::process_compute_toplesets(viewer * p_view) if(!size(mesh.selected)) mesh.selected.push_back(0); - index_t * toplesets = new index_t[mesh->n_vertices]; - index_t * sorted = new index_t[mesh->n_vertices]; - std::vector limites; - mesh->compute_toplesets(toplesets, sorted, limites, mesh.selected); - - size_t n_toplesets = size(limites) - 1; + toplesets tps(mesh, mesh.selected); + size_t n_levels = tps.n_levels; #pragma omp parallel for for(index_t v = 0; v < mesh->n_vertices; ++v) - { - if(toplesets[v] < n_toplesets) - mesh->heatmap(v) = float(toplesets[v]) / (n_toplesets); - } + mesh->heatmap(v) = tps[v] < n_levels ? float(tps[v]) / n_levels : -1; mesh.update_vbo_heatmap(); - gproshan_debug_var(n_toplesets); - - delete [] toplesets; - delete [] sorted; - + gproshan_debug_var(n_levels); return false; } diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 0d79103b..8ff640c4 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -193,19 +193,15 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources) { - index_t * toplesets = new index_t[n_vertices]; - std::vector limits; - mesh->compute_toplesets(toplesets, sorted_index, limits, sources); + toplesets tps(mesh, sources); double time_ptp; TIC(time_ptp) - parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {limits, sorted_index}, size(sources) == 1); + parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {tps.splits, tps.sorted}, size(sources) == 1); TOC(time_ptp) gproshan_log_var(time_ptp); - - delete [] toplesets; } void geodesics::run_heat_method(che * mesh, const std::vector & sources) @@ -224,15 +220,11 @@ void geodesics::run_heat_method(che * mesh, const std::vector & sources void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources) { - index_t * toplesets = new index_t[n_vertices]; - std::vector limits; - mesh->compute_toplesets(toplesets, sorted_index, limits, sources); + toplesets tps(mesh, sources); - double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {limits, sorted_index}); + double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {tps.splits, tps.sorted}); gproshan_log_var(time_ptp); - - delete [] toplesets; } void geodesics::run_heat_method_gpu(che * mesh, const std::vector & sources) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index b30e3dd4..29522f31 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -159,9 +159,7 @@ float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples for(index_t v = 0; v < n_vertices; ++v) h_dist[v] = INFINITY; - std::vector limits; - index_t * toplesets = new index_t[n_vertices]; - index_t * sorted_index = new index_t[n_vertices]; + toplesets tps(mesh, samples); cublasHandle_t handle; cublasCreate(&handle); @@ -175,25 +173,21 @@ float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples float max_dist = INFINITY; while(n-- && radio < max_dist) { - limits.clear(); - mesh->compute_toplesets(toplesets, sorted_index, limits, samples); - - const index_t i = run_ptp(d_mesh, samples, limits, d_error, d_dist, d_clusters, sorted_index, d_sorted); + const index_t i = run_ptp(d_mesh, samples, tps.splits, d_error, d_dist, d_clusters, tps.sorted, d_sorted); // 1 indexing - cublasIsamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); + cublasIsamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); if(radio > 0 || !n) cudaMemcpy(&max_dist, d_dist[i] + farthest - 1, sizeof(float), cudaMemcpyDeviceToHost); samples.push_back(farthest - 1); + tps.reset(mesh, samples); } cublasDestroy(handle); delete [] h_dist; - delete [] toplesets; - delete [] sorted_index; cudaFree(d_error); cudaFree(d_dist[0]); diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index d970c48c..c081992d 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -646,46 +646,6 @@ void che::edge_collapse(const std::vector & sort_edges) // TODO } -void che::compute_toplesets(index_t * toplesets, index_t * sorted, std::vector & limits, const std::vector & sources, const index_t k) -{ - if(!size(sources)) return; - - memset(toplesets, -1, sizeof(index_t) * n_vertices); - - index_t level = 0; - - index_t p = 0; - for(const index_t s: sources) - { - sorted[p++] = s; - toplesets[s] = level; - } - - limits.push_back(0); - for(index_t i = 0; i < p; ++i) - { - const index_t v = sorted[i]; - - if(toplesets[v] > level) - { - if(++level > k) break; - limits.push_back(i); - } - - for(const index_t u: link(v)) - { - if(toplesets[u] == NIL) - { - toplesets[u] = toplesets[v] + 1; - sorted[p++] = u; - } - } - } - - assert(p <= n_vertices); - limits.push_back(p); -} - ///< return a vector of indices of one vertex per boundary std::vector che::bounds() const diff --git a/src/gproshan/mesh/che_poisson.cpp b/src/gproshan/mesh/che_poisson.cpp index c706fa05..cc34737a 100644 --- a/src/gproshan/mesh/che_poisson.cpp +++ b/src/gproshan/mesh/che_poisson.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -100,22 +101,16 @@ void biharmonic_interp_2(che * mesh, const size_t old_n_vertices, const size_t n { if(old_n_vertices == n_vertices) return; - index_t * rings = new index_t[mesh->n_vertices]; - index_t * sorted = new index_t[mesh->n_vertices]; - std::vector limites; - mesh->compute_toplesets(rings, sorted, limites, border_vertices, k); + toplesets tps(mesh, border_vertices, k); - const size_t n_border_vertices = limites.back(); + const size_t n_border_vertices = tps.splits.back(); std::vector sub_mesh_hole; sub_mesh_hole.reserve(n_border_vertices); for(index_t b = 0; b < n_border_vertices; ++b) - if(sorted[b] < old_n_vertices) - sub_mesh_hole.push_back(sorted[b]); - - delete [] rings; - delete [] sorted; + if(tps.sorted[b] < old_n_vertices) + sub_mesh_hole.push_back(tps.sorted[b]); arma::fmat P(3, size(sub_mesh_hole)); index_t i = 0; From 2574e3914e3677f1041a7a969cc3e91499458334 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 27 Mar 2024 13:19:58 +0100 Subject: [PATCH 0991/1018] geodesics: added toplesets and coalescence structs --- include/gproshan/geodesics/geodesics.h | 12 +-- include/gproshan/geodesics/geodesics_ptp.h | 20 +++-- src/gproshan/geodesics/geodesics.cpp | 89 +++++++++------------- src/gproshan/geodesics/geodesics_ptp.cpp | 68 +++++++++++------ src/gproshan/geodesics/geodesics_ptp.cu | 35 +++------ src/gproshan/geodesics/sampling.cpp | 6 +- 6 files changed, 106 insertions(+), 124 deletions(-) diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index 10be9d5b..8190498d 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -69,16 +69,8 @@ class geodesics void normalize(); private: - void execute(che * mesh, const std::vector & sources, const params & p); - void run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun); - void run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources); - void run_heat_method(che * mesh, const std::vector & sources); - -#ifdef GPROSHAN_CUDA - void run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources); - void run_heat_method_gpu(che * mesh, const std::vector & sources); -#endif // GPROSHAN_CUDA - + double execute(che * mesh, const std::vector & sources, const params & p); + double run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun); }; diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 4a5e9f9c..1c7841ec 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -58,10 +58,16 @@ struct ptp_out_t ptp_out_t(float *const d, index_t *const c = nullptr); }; -struct toplesets_t + +struct coalescence_ptp { - const std::vector & limits; - const index_t * index; + che * mesh = nullptr; + index_t * inv = nullptr; + + coalescence_ptp(const che * mesh, const toplesets & tps); + ~coalescence_ptp(); + + operator const index_t * () const; }; @@ -72,23 +78,23 @@ using f_ptp = std::function; double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, - const toplesets_t & toplesets, + const toplesets & tps, const bool coalescence = true, const bool set_inf = true, const f_ptp & fun = nullptr ); -void parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, +double parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, - const toplesets_t & toplesets, + const toplesets & tps, const bool coalescence = true, const bool set_inf = true, const f_ptp & fun = nullptr ); -float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, float radio = 0); +double farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, float radio = 0); void normalize_ptp(float * dist, const size_t n); diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 8ff640c4..949e8700 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -15,6 +15,9 @@ geodesics::geodesics(che * mesh, const std::vector & sources, const par { assert(n_vertices > 0); + double time; + TIC(time); + free_dist = p.dist_alloc == nullptr; dist = free_dist ? new float[n_vertices] : p.dist_alloc; @@ -28,7 +31,12 @@ geodesics::geodesics(che * mesh, const std::vector & sources, const par dist[v] = INFINITY; assert(size(sources) > 0); - execute(mesh, sources, p); + + double solve_time = execute(mesh, sources, p); + TOC(time); + + gproshan_error_var(time); + gproshan_log_var(solve_time); } geodesics::~geodesics() @@ -94,28 +102,44 @@ void geodesics::normalize() dist[i] /= max; } -void geodesics::execute(che * mesh, const std::vector & sources, const params & p) +double geodesics::execute(che * mesh, const std::vector & sources, const params & p) { + double time = 0; + switch(p.alg) { - case FM: run_fastmarching(mesh, sources, p.n_iter, p.radio, p.fun); + case FM: + time = run_fastmarching(mesh, sources, p.n_iter, p.radio, p.fun); break; - case PTP_CPU: run_parallel_toplesets_propagation_cpu(mesh, sources); + + case PTP_CPU: + time = parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, toplesets(mesh, sources)); break; - case HEAT_METHOD: run_heat_method(mesh, sources); + + case HEAT_METHOD: + time = heat_method(dist, mesh, sources, HEAT_CHOLMOD); break; #ifdef GPROSHAN_CUDA - case PTP_GPU: run_parallel_toplesets_propagation_gpu(mesh, sources); + case PTP_GPU: + time = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, toplesets(mesh, sources)); break; - case HEAT_METHOD_GPU: run_heat_method_gpu(mesh, sources); + + case HEAT_METHOD_GPU: + time = heat_method(dist, mesh, sources, HEAT_CUDA); break; #endif // GPROSHAN_CUDA } + + return time; } -void geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun) +double geodesics::run_fastmarching(che * mesh, const std::vector & sources, const size_t n_iter, const float radio, const fm_function_t & fun) { + double time; + TIC(time); + + index_t BLACK = 0, GREEN = 1, RED = 2; index_t * color = new index_t[n_vertices]; @@ -189,57 +213,12 @@ void geodesics::run_fastmarching(che * mesh, const std::vector & source } delete [] color; -} - -void geodesics::run_parallel_toplesets_propagation_cpu(che * mesh, const std::vector & sources) -{ - toplesets tps(mesh, sources); - double time_ptp; + TOC(time); - TIC(time_ptp) - parallel_toplesets_propagation_cpu({dist, clusters}, mesh, sources, {tps.splits, tps.sorted}, size(sources) == 1); - TOC(time_ptp) - - gproshan_log_var(time_ptp); + return time; } -void geodesics::run_heat_method(che * mesh, const std::vector & sources) -{ - double time_total, solve_time; - TIC(time_total) - solve_time = heat_method(dist, mesh, sources, HEAT_CHOLMOD); - TOC(time_total) - - gproshan_log_var(time_total - solve_time); - gproshan_log_var(solve_time); -} - - -#ifdef GPROSHAN_CUDA - -void geodesics::run_parallel_toplesets_propagation_gpu(che * mesh, const std::vector & sources) -{ - toplesets tps(mesh, sources); - - double time_ptp = parallel_toplesets_propagation_gpu({dist, clusters}, mesh, sources, {tps.splits, tps.sorted}); - - gproshan_log_var(time_ptp); -} - -void geodesics::run_heat_method_gpu(che * mesh, const std::vector & sources) -{ - double time_total, solve_time; - TIC(time_total) - solve_time = heat_method(dist, mesh, sources, HEAT_CUDA); - TOC(time_total) - - gproshan_log_var(time_total - solve_time); - gproshan_log_var(solve_time); -} - -#endif // GPROSHAN_CUDA - } // namespace gproshan diff --git a/src/gproshan/geodesics/geodesics_ptp.cpp b/src/gproshan/geodesics/geodesics_ptp.cpp index 1370e350..9965445e 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cpp +++ b/src/gproshan/geodesics/geodesics_ptp.cpp @@ -12,28 +12,46 @@ namespace gproshan { ptp_out_t::ptp_out_t(float *const d, index_t *const c): dist(d), clusters(c) {} -void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, - const che * mesh, - const std::vector & sources, - const toplesets_t & toplesets, - const bool coalescence, - const bool set_inf, - const f_ptp & fun - ) +coalescence_ptp::coalescence_ptp(const che * m, const toplesets & tps) { - const size_t n_vertices = mesh->n_vertices; + if(!m) return; - che * h_mesh = nullptr; - index_t * inv = nullptr; - if(coalescence) - { - h_mesh = new che(*mesh, toplesets.index, {false, false, false}); - inv = new index_t[n_vertices]; + mesh = new che(*m, tps.sorted, {false, false, false}); + inv = new index_t[mesh->n_vertices]; + + #pragma omp parallel for + for(index_t i = 0; i < tps.splits.back(); ++i) + inv[tps.sorted[i]] = i; +} + +coalescence_ptp::~coalescence_ptp() +{ + delete mesh; + delete [] inv; +} + +coalescence_ptp::operator const index_t * () const +{ + return inv; +} + + +double parallel_toplesets_propagation_cpu( const ptp_out_t & ptp_out, + const che * mesh, + const std::vector & sources, + const toplesets & tps, + const bool coalescence, + const bool set_inf, + const f_ptp & fun + ) +{ + double time; + TIC(time); - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - inv[toplesets.index[i]] = i; - } + + const size_t n_vertices = mesh->n_vertices; + + coalescence_ptp inv(coalescence ? mesh : nullptr, tps); float * dist[2] = { coalescence ? new float[n_vertices] : ptp_out.dist, @@ -50,9 +68,9 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, dist[0][v] = dist[1][v] = INFINITY; } - const index_t i = run_ptp( h_mesh ? h_mesh : mesh, sources, toplesets.limits, dist, clusters, - coalescence ? inv : toplesets.index, - coalescence ? nullptr : (index_t *) toplesets.index, + const index_t i = run_ptp( coalescence ? inv.mesh : mesh, sources, tps.splits, dist, clusters, + coalescence ? inv : tps.sorted, + coalescence ? nullptr : (index_t *) tps.sorted, fun); #pragma omp parallel for @@ -71,8 +89,10 @@ void parallel_toplesets_propagation_cpu(const ptp_out_t & ptp_out, delete [] dist[1]; delete [] clusters[1]; - delete [] inv; - delete h_mesh; + + TOC(time); + + return time; } void normalize_ptp(float * dist, const size_t n) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 29522f31..bd3dc913 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -15,27 +15,12 @@ namespace gproshan { double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, const che * mesh, const std::vector & sources, - const toplesets_t & toplesets, + const toplesets & tps, const bool coalescence, const bool set_inf, const f_ptp & fun ) { - const size_t n_vertices = mesh->n_vertices; - - che * h_mesh = nullptr; - index_t * inv = nullptr; - if(coalescence) - { - h_mesh = new che(*mesh, toplesets.index, {false, false, false}); - inv = new index_t[n_vertices]; - - #pragma omp parallel for - for(index_t i = 0; i < toplesets.limits.back(); ++i) - inv[toplesets.index[i]] = i; - } - - cudaDeviceReset(); cudaEvent_t start, stop; @@ -43,7 +28,11 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, cudaEventCreate(&stop); cudaEventRecord(start, 0); - che_cuda d_mesh(h_mesh ? h_mesh : mesh, {false, false, false}); + + const size_t n_vertices = mesh->n_vertices; + + coalescence_ptp inv(coalescence ? mesh : nullptr, tps); + che_cuda d_mesh(coalescence ? inv.mesh : mesh, {false, false, false}); float * h_dist = coalescence ? new float[n_vertices] : ptp_out.dist; index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[n_vertices] @@ -78,8 +67,8 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, h_dist[v] = INFINITY; } - const index_t i = run_ptp( d_mesh, sources, toplesets.limits, d_error, d_dist, d_clusters, - coalescence ? inv : toplesets.index, d_sorted, + const index_t i = run_ptp( d_mesh, sources, tps.splits, d_error, d_dist, d_clusters, + coalescence ? inv : tps.sorted, d_sorted, fun); cudaMemcpy(h_dist, d_dist[i], sizeof(float) * n_vertices, cudaMemcpyDeviceToHost); @@ -114,9 +103,6 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, cudaFree(d_clusters[1]); cudaFree(d_sorted); - delete [] inv; - delete h_mesh; - cudaEventRecord(stop, 0); cudaEventSynchronize(stop); @@ -129,7 +115,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, return time / 1000; } -float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, double & time_fps, size_t n, float radio) +double farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples, size_t n, float radio) { const size_t n_vertices = mesh->n_vertices; @@ -199,12 +185,11 @@ float farthest_point_sampling_ptp_gpu(che * mesh, std::vector & samples float time; cudaEventElapsedTime(&time, start, stop); - time_fps = time / 1000; cudaEventDestroy(start); cudaEventDestroy(stop); - return max_dist; + return time / 1000; } __global__ diff --git a/src/gproshan/geodesics/sampling.cpp b/src/gproshan/geodesics/sampling.cpp index e3f25832..b4ab8fbb 100644 --- a/src/gproshan/geodesics/sampling.cpp +++ b/src/gproshan/geodesics/sampling.cpp @@ -64,13 +64,13 @@ bool load_sampling(std::vector & points, float & radio, che * mesh, siz points.push_back(0); #ifdef GPROSHAN_CUDA - double time_fps; - radio = farthest_point_sampling_ptp_gpu(mesh, points, time_fps, n); - gproshan_debug_var(time_fps); + double time_fps = farthest_point_sampling_ptp_gpu(mesh, points, n); + gproshan_log_var(time_fps); #else radio = 0; // IMPLEMENT: farthest_point_sampling_ptp_cpu(mesh, points, time_fps, n); #endif // GPROSHAN_CUDA + // TODO: compute radio std::ofstream os(tmp_file_path(file)); os << radio << std::endl; os << size(points) << std::endl; From 79cfd5eaf03fa4901cf462bd6b7dc96f063ac352 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 27 Mar 2024 14:55:58 +0100 Subject: [PATCH 0992/1018] scenes: fix scene number of triangles --- include/gproshan/geodesics/geodesics.h | 22 +++++++++++----------- include/gproshan/mesh/che.h | 2 +- src/gproshan/mesh/che.cpp | 1 + src/gproshan/mesh/che_obj.cpp | 4 ++-- src/gproshan/scenes/scene.cpp | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/gproshan/geodesics/geodesics.h b/include/gproshan/geodesics/geodesics.h index 8190498d..d431e8df 100644 --- a/include/gproshan/geodesics/geodesics.h +++ b/include/gproshan/geodesics/geodesics.h @@ -22,23 +22,23 @@ class geodesics using fm_function_t = std::function; public: - enum algorithm { FM, ///< Execute Fast Marching algorithm - PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm - HEAT_METHOD, ///< Execute Heat Method - cholmod (CPU) + enum algorithm { FM, ///< Execute Fast Marching algorithm + PTP_CPU, ///< Execute Parallel Toplesets Propagation CPU algorithm + HEAT_METHOD, ///< Execute Heat Method - cholmod (CPU) #ifdef GPROSHAN_CUDA - PTP_GPU, ///< Execute Parallel Toplesets Propagation GPU algorithm - HEAT_METHOD_GPU ///< Execute Heat Method - cusparse (GPU) + PTP_GPU, ///< Execute Parallel Toplesets Propagation GPU algorithm + HEAT_METHOD_GPU ///< Execute Heat Method - cusparse (GPU) #endif // GPROSHAN_CUDA }; struct params { - algorithm alg = FM; ///< specific the algorithm to execute. - size_t n_iter = 0; ///< maximum number of iterations. - float radio = INFINITY; ///< execute until the specific radio. - float * dist_alloc = nullptr; ///< external dist allocation - bool cluster = false; ///< to cluster vertices to closest source. - fm_function_t fun = nullptr; ///< fun is executed inside FM loop + algorithm alg = FM; ///< specific the algorithm to execute. + size_t n_iter = 0; ///< maximum number of iterations. + float radio = INFINITY; ///< execute until the specific radio. + float * dist_alloc = nullptr; ///< external dist allocation + bool cluster = false; ///< to cluster vertices to closest source. + fm_function_t fun = nullptr; ///< fun is executed inside FM loop }; public: diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 36e765f3..53a157fa 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -87,7 +87,7 @@ class che vertex * VN = nullptr; ///< vertex normals : v -> normal(v) rgb_t * VC = nullptr; ///< vertex color : v -> color(v) - float * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) + float * VHC = nullptr; ///< vertex color heatmap : v -> heatmap(v) float scale_hm = 1; ///< vertex color heatmap scale factor bool manifold = true; diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index c081992d..fca13dc4 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -254,6 +254,7 @@ void che::update_heatmap(const float * hm) void che::update_normals() { + if(is_scene()) return; if(!n_trigs) return; #pragma omp parallel for diff --git a/src/gproshan/mesh/che_obj.cpp b/src/gproshan/mesh/che_obj.cpp index 4c28489e..c9abe0e4 100644 --- a/src/gproshan/mesh/che_obj.cpp +++ b/src/gproshan/mesh/che_obj.cpp @@ -116,9 +116,9 @@ che_obj::parser::parser(const std::string & file) } switch(i) { - case 0: f[i] += f[i] > size(vertices) ? size(vertices) : -1; break; + case 0: f[i] += f[i] > size(vertices) ? size(vertices) : -1; break; case 1: f[i] += f[i] > size(vtexcoords) ? size(vtexcoords) : -1; break; - case 2: f[i] += f[i] > size(vnormals) ? size(vnormals) : -1; break; + case 2: f[i] += f[i] > size(vnormals) ? size(vnormals) : -1; break; } } diff --git a/src/gproshan/scenes/scene.cpp b/src/gproshan/scenes/scene.cpp index 2ca9c7ae..a9cccb66 100644 --- a/src/gproshan/scenes/scene.cpp +++ b/src/gproshan/scenes/scene.cpp @@ -49,7 +49,7 @@ bool scene::load_obj(const std::string & file) if(!load_mtl(path + m)) return false; - alloc(size(p.trigs), size(p.trigs)); + alloc(size(p.trigs), size(p.trigs) / 3); #pragma omp parallel for for(index_t i = 0; i < n_vertices; ++i) From 6feaefc8adfe509a7411f35ee0efd166c5189e38 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 17 Jul 2024 13:17:36 +0200 Subject: [PATCH 0993/1018] rt: avoiding float operations ptx optix --- include/gproshan/raytracing/utils.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index ef26d0c9..ed86b2d6 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -53,7 +53,7 @@ __host_device__ vec texture(const scene::texture & tex, const vec & coord) { const int i = (tex.width + int(coord.x() * (tex.width - 1))) % tex.width; - const int j = (tex.height + int(coord.y() * (tex.height -1))) % tex.height; + const int j = (tex.height + int(coord.y() * (tex.height - 1))) % tex.height; const int k = j * tex.width + i; che::rgb_t color; @@ -67,7 +67,7 @@ vec texture(const scene::texture & tex, const vec & coord) color.r = color.g = color.b = tex.data[k]; } - return {T(color.r) / 255, T(color.g) / 255, T(color.b) / 255}; + return {T(color.r) / 255.f, T(color.g) / 255.f, T(color.b) / 255.f}; } template @@ -163,7 +163,7 @@ struct t_eval_hit __host_device__ bool scatter_reflect(vec & dir, random & ) { - dir = normalize(dir - 2 * dot(dir, normal) * normal); + dir = normalize(dir - 2.f * dot(dir, normal) * normal); return dot(dir, normal) > 0; } @@ -171,7 +171,7 @@ struct t_eval_hit bool scatter_refract(vec & dir, random & ) { const float dvn = dot(dir, normal); - const float d = 1 - Ni * Ni * (1 - dvn * dvn); + const float d = 1.f - Ni * Ni * (1.f - dvn * dvn); if(d <= 0) return false; @@ -183,8 +183,8 @@ struct t_eval_hit bool scatter_diffuse(vec & dir, random & rnd) { // random unit sphere - const T theta = rnd() * 2 * M_PI; - const T phi = acosf(2 * rnd() - 1); + const T theta = rnd() * 2.f * M_PI; + const T phi = acosf(2.f * rnd() - 1.f); const T r = cbrtf(rnd()); const vec p = { r * sinf(phi) * cosf(theta) @@ -252,7 +252,7 @@ vec ray_view_dir( const uvec2 & pos, vec2 screen = { (float(pos.x()) + rnd()) / windows_size.x(), (float(pos.y()) + rnd()) / windows_size.y() }; - vec view = {screen.x() * 2 - 1, screen.y() * 2 - 1, 1, 1}; + vec view = {screen.x() * 2.f - 1.f, screen.y() * 2.f - 1.f, 1.f, 1.f}; vec q = inv_proj_view * view; vec p = q / q[3]; From 8d7ae3a8d616616f884d08d2357d381350133699 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 23 Jul 2024 14:23:38 +0200 Subject: [PATCH 0994/1018] viewer: frame minor updates --- src/gproshan/raytracing/optix.cpp | 4 ---- src/gproshan/viewer/frame.cpp | 10 ++++++++++ src/gproshan/viewer/viewer.cpp | 14 +++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index cd4b27e6..835e95b7 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -278,11 +278,7 @@ void optix::create_pipeline() &optix_pipeline ); - if(sizeof_log > 1) gproshan_log_var(log); - optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); - - if(sizeof_log > 1) gproshan_log_var(log); } void optix::build_sbt() diff --git a/src/gproshan/viewer/frame.cpp b/src/gproshan/viewer/frame.cpp index f9fde56e..4bad103c 100644 --- a/src/gproshan/viewer/frame.cpp +++ b/src/gproshan/viewer/frame.cpp @@ -44,6 +44,16 @@ frame::frame() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); + +#ifdef GPROSHAN_CUDA + int current_device, is_display_device; + cudaGetDevice(¤t_device); + cudaDeviceGetAttribute(&is_display_device, cudaDevAttrKernelExecTimeout, current_device); + if(!is_display_device ) + { + gproshan_error("DEVICE IS NOT DISPLAY DEVICE"); + } +#endif //GPROSHAN_CUDA } frame::~frame() diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 98a75a56..c694a18e 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -89,6 +89,13 @@ viewer::~viewer() update_status_message("frametime_%p", this); save_frametime(tmp_file_path(status_message)); + delete sphere; + + delete [] frames; + + for(che_viewer * m: meshes) + delete m; + if(window) { ImGui_ImplOpenGL3_Shutdown(); @@ -98,13 +105,6 @@ viewer::~viewer() glfwDestroyWindow(window); glfwTerminate(); } - - delete sphere; - - delete [] frames; - - for(che_viewer * m: meshes) - delete m; } bool viewer::run() From 7b6e2493c96d88738610f95b8bbfc63998043d34 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Aug 2024 10:59:50 +0200 Subject: [PATCH 0995/1018] rt_optix: splitting module --- include/gproshan/raytracing/optix.h | 19 +- src/gproshan/CMakeLists.txt | 2 +- src/gproshan/raytracing/optix.cpp | 234 +++++++++--------- src/gproshan/raytracing/optix.cu | 100 +------- src/gproshan/raytracing/optix_path_tracing.cu | 182 ++++++++++++++ 5 files changed, 322 insertions(+), 215 deletions(-) create mode 100644 src/gproshan/raytracing/optix_path_tracing.cu diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index e60dfd58..d5511cf7 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -24,14 +24,15 @@ class optix : public raytracing CUcontext cuda_context; CUstream stream; - OptixDeviceContext optix_context; + OptixDeviceContext _context; - OptixModule optix_module; - OptixModuleCompileOptions optix_module_compile_opt = {}; + OptixModule _module_raygen; + OptixModule _module_radiance; + OptixModuleCompileOptions _module_compile_opt = {}; - OptixPipeline optix_pipeline; - OptixPipelineCompileOptions optix_pipeline_compile_opt = {}; - OptixPipelineLinkOptions optix_pipeline_link_opt = {}; + OptixPipeline _pipeline; + OptixPipelineCompileOptions _pipeline_compile_opt = {}; + OptixPipelineLinkOptions _pipeline_link_opt = {}; OptixProgramGroup raygen_programs[1]; OptixProgramGroup miss_programs[2]; @@ -39,8 +40,8 @@ class optix : public raytracing OptixShaderBindingTable sbt = {}; - launch_params optix_params; - launch_params * optix_params_buffer = nullptr; + launch_params params; + launch_params * params_buffer = nullptr; std::vector d_mesh; @@ -65,7 +66,7 @@ class optix : public raytracing void create_pipeline(); void build_sbt(); OptixTraversableHandle build_as(const std::vector & meshes, const std::vector & model_mats); - void add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat); + void add_mesh(OptixBuildInput & _mesh, CUdeviceptr & d_vertex_ptr, uint32_t & _trig_flags, const che * mesh, const mat4 & model_mat); }; diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 9f40dbbf..4c3b1002 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -18,7 +18,7 @@ target_link_libraries(gproshan imgui) if(CUDAToolkit_FOUND) FILE(GLOB_RECURSE cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.cu) - list(REMOVE_ITEM cu_sources ${CMAKE_CURRENT_SOURCE_DIR}/raytracing/optix.cu) + list(FILTER cu_sources EXCLUDE REGEX ${CMAKE_CURRENT_SOURCE_DIR}/raytracing/.*.cu) add_library(gproshan_cuda SHARED ${cu_sources}) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index cd4b27e6..04c38ca9 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -49,36 +49,36 @@ optix::optix(const std::string & ptx) cuCtxGetCurrent(&cuda_context); - optixDeviceContextCreate(cuda_context, 0, &optix_context); - optixDeviceContextSetLogCallback(optix_context, optix_log, nullptr, 4); + optixDeviceContextCreate(cuda_context, 0, &_context); + optixDeviceContextSetLogCallback(_context, optix_log, nullptr, 4); // create module - //optix_module_compile_opt.maxRegisterCount = 50; - optix_module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; - optix_module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; + //_module_compile_opt.maxRegisterCount = 50; + _module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; + _module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; - optix_pipeline_compile_opt = {}; - optix_pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; - optix_pipeline_compile_opt.usesMotionBlur = false; - optix_pipeline_compile_opt.numPayloadValues = 4; - optix_pipeline_compile_opt.numAttributeValues = 4; - optix_pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; - optix_pipeline_compile_opt.pipelineLaunchParamsVariableName = "optix_params"; + _pipeline_compile_opt = {}; + _pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; + _pipeline_compile_opt.usesMotionBlur = false; + _pipeline_compile_opt.numPayloadValues = 4; + _pipeline_compile_opt.numAttributeValues = 4; + _pipeline_compile_opt.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE; + _pipeline_compile_opt.pipelineLaunchParamsVariableName = "params"; - optix_pipeline_link_opt.maxTraceDepth = 2; + _pipeline_link_opt.maxTraceDepth = 2; std::ifstream ptx_is(std::string(GPROSHAN_DIR) + ptx); const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); ptx_is.close(); - optixModuleCreate( optix_context, - &optix_module_compile_opt, - &optix_pipeline_compile_opt, + optixModuleCreate( _context, + &_module_compile_opt, + &_pipeline_compile_opt, str_ptx_code.c_str(), size(str_ptx_code), nullptr, nullptr, // log message - &optix_module + &_module_raygen ); @@ -91,13 +91,13 @@ optix::optix(const std::string & ptx) create_pipeline(); // launch params - cudaMalloc(&optix_params_buffer, sizeof(launch_params)); + cudaMalloc(¶ms_buffer, sizeof(launch_params)); } optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() { // build as - optix_params.traversable = build_as(meshes, model_mats); + params.traversable = build_as(meshes, model_mats); // build sbt build_sbt(); @@ -105,7 +105,7 @@ optix::optix(const std::vector & meshes, const std::vector & optix::~optix() { - cudaFree(optix_params_buffer); + cudaFree(params_buffer); cudaFree(raygen_records_buffer); cudaFree(miss_records_buffer); cudaFree(hitgroup_records_buffer); @@ -114,44 +114,44 @@ optix::~optix() for(index_t i = 0; i < size(d_mesh); ++i) delete d_mesh[i]; - cudaFree(optix_params.sc.materials); - cudaFree(optix_params.sc.textures); - cudaFree(optix_params.sc.trig_mat); - cudaFree(optix_params.sc.texcoords); + cudaFree(params.sc.materials); + cudaFree(params.sc.textures); + cudaFree(params.sc.trig_mat); + cudaFree(params.sc.texcoords); for(unsigned char * data: tex_data) cudaFree(data); } -void optix::render(vec4 * img, const render_params & params, const bool flat) +void optix::render(vec4 * img, const render_params & p, const bool flat) { - optix_params.depth = params.depth; - optix_params.n_frames = params.n_frames; - optix_params.n_samples = params.n_samples; - optix_params.color_buffer = img; + params.depth = p.depth; + params.n_frames = p.n_frames; + params.n_samples = p.n_samples; + params.color_buffer = img; - optix_params.window_size = params.window_size; - if(params.viewport_is_window) - optix_params.window_size = params.viewport_size; + params.window_size = p.window_size; + if(p.viewport_is_window) + params.window_size = p.viewport_size; - optix_params.viewport_pos = params.viewport_pos; + params.viewport_pos = p.viewport_pos; - optix_params.flat = flat; - optix_params.cam_pos = params.cam_pos; - optix_params.inv_proj_view = params.inv_proj_view; - optix_params.ambient = params.ambient; - optix_params.n_lights = params.n_lights; - memcpy(optix_params.lights, params.lights, sizeof(optix_params.lights)); + params.flat = flat; + params.cam_pos = p.cam_pos; + params.inv_proj_view = p.inv_proj_view; + params.ambient = p.ambient; + params.n_lights = p.n_lights; + memcpy(params.lights, p.lights, sizeof(params.lights)); - cudaMemcpy(optix_params_buffer, &optix_params, sizeof(launch_params), cudaMemcpyHostToDevice); + cudaMemcpy(params_buffer, ¶ms, sizeof(launch_params), cudaMemcpyHostToDevice); - optixLaunch(optix_pipeline, + optixLaunch(_pipeline, stream, - (CUdeviceptr) optix_params_buffer, + (CUdeviceptr) params_buffer, sizeof(launch_params), &sbt, - params.viewport_size.x(), - params.viewport_size.y(), + p.viewport_size.x(), + p.viewport_size.y(), 1 ); @@ -166,10 +166,10 @@ void optix::create_raygen_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN; - pg_desc.raygen.module = optix_module; + pg_desc.raygen.module = _module_raygen; pg_desc.raygen.entryFunctionName = "__raygen__render_frame"; - optixProgramGroupCreate(optix_context, + optixProgramGroupCreate(_context, &pg_desc, 1, &pg_options, @@ -188,12 +188,12 @@ void optix::create_miss_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS; - pg_desc.miss.module = optix_module; + pg_desc.miss.module = _module_radiance; pg_desc.miss.entryFunctionName = "__miss__radiance"; - optixProgramGroupCreate(optix_context, + optixProgramGroupCreate(_context, &pg_desc, 1, &pg_options, @@ -206,7 +206,7 @@ void optix::create_miss_programs() pg_desc.miss.entryFunctionName = "__miss__shadow"; - optixProgramGroupCreate(optix_context, + optixProgramGroupCreate(_context, &pg_desc, 1, &pg_options, @@ -225,14 +225,14 @@ void optix::create_hitgroup_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP; - pg_desc.hitgroup.moduleCH = optix_module; - pg_desc.hitgroup.moduleAH = optix_module; + pg_desc.hitgroup.moduleCH = _module_radiance; + pg_desc.hitgroup.moduleAH = _module_radiance; pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__radiance"; pg_desc.hitgroup.entryFunctionNameAH = "__anyhit__radiance"; - optixProgramGroupCreate(optix_context, + optixProgramGroupCreate(_context, &pg_desc, 1, &pg_options, @@ -246,7 +246,7 @@ void optix::create_hitgroup_programs() pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__shadow"; pg_desc.hitgroup.entryFunctionNameAH = "__anyhit__shadow"; - optixProgramGroupCreate(optix_context, + optixProgramGroupCreate(_context, &pg_desc, 1, &pg_options, @@ -269,18 +269,18 @@ void optix::create_pipeline() char log[2048]; size_t sizeof_log = sizeof(log); - optixPipelineCreate(optix_context, - &optix_pipeline_compile_opt, - &optix_pipeline_link_opt, + optixPipelineCreate(_context, + &_pipeline_compile_opt, + &_pipeline_link_opt, program_groups.data(), size(program_groups), log, &sizeof_log, - &optix_pipeline + &_pipeline ); if(sizeof_log > 1) gproshan_log_var(log); - optixPipelineSetStackSize(optix_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); + optixPipelineSetStackSize(_pipeline, 2 * 1024, 2 * 1024, 2 * 1024, 1); if(sizeof_log > 1) gproshan_log_var(log); } @@ -335,54 +335,54 @@ void optix::build_sbt() OptixTraversableHandle optix::build_as(const std::vector & meshes, const std::vector & model_mats) { - OptixTraversableHandle optix_as_handle = {}; + OptixTraversableHandle _as_handle = {}; - std::vector optix_meshes(size(meshes)); - std::vector optix_vertex_ptr(size(meshes)); - std::vector optix_trig_flags(size(meshes)); + std::vector _meshes(size(meshes)); + std::vector _vertex_ptr(size(meshes)); + std::vector _trig_flags(size(meshes)); for(index_t i = 0; i < size(meshes); ++i) - add_mesh(optix_meshes[i], optix_vertex_ptr[i], optix_trig_flags[i], meshes[i], model_mats[i]); + add_mesh(_meshes[i], _vertex_ptr[i], _trig_flags[i], meshes[i], model_mats[i]); - OptixAccelBuildOptions optix_accel_opt = {}; - optix_accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS | + OptixAccelBuildOptions _accel_opt = {}; + _accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS | OPTIX_BUILD_FLAG_ALLOW_COMPACTION; - optix_accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; - - OptixAccelBufferSizes optix_gas_buffer_size; - optixAccelComputeMemoryUsage( optix_context, - &optix_accel_opt, - optix_meshes.data(), - size(optix_meshes), - &optix_gas_buffer_size + _accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; + + OptixAccelBufferSizes _gas_buffer_size; + optixAccelComputeMemoryUsage( _context, + &_accel_opt, + _meshes.data(), + size(_meshes), + &_gas_buffer_size ); void * d_compacted_size; cudaMalloc(&d_compacted_size, sizeof(uint64_t)); - OptixAccelEmitDesc optix_emit_desc; - optix_emit_desc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE; - optix_emit_desc.result = (CUdeviceptr) d_compacted_size; + OptixAccelEmitDesc _emit_desc; + _emit_desc.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE; + _emit_desc.result = (CUdeviceptr) d_compacted_size; void * d_temp_buffer; - cudaMalloc(&d_temp_buffer, optix_gas_buffer_size.tempSizeInBytes); + cudaMalloc(&d_temp_buffer, _gas_buffer_size.tempSizeInBytes); void * d_output_buffer; - cudaMalloc(&d_output_buffer, optix_gas_buffer_size.outputSizeInBytes); + cudaMalloc(&d_output_buffer, _gas_buffer_size.outputSizeInBytes); - optixAccelBuild( optix_context, + optixAccelBuild( _context, 0, // stream - &optix_accel_opt, - optix_meshes.data(), - size(optix_meshes), + &_accel_opt, + _meshes.data(), + size(_meshes), (CUdeviceptr) d_temp_buffer, - optix_gas_buffer_size.tempSizeInBytes, + _gas_buffer_size.tempSizeInBytes, (CUdeviceptr) d_output_buffer, - optix_gas_buffer_size.outputSizeInBytes, - &optix_as_handle, - &optix_emit_desc, + _gas_buffer_size.outputSizeInBytes, + &_as_handle, + &_emit_desc, 1 ); @@ -393,12 +393,12 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, cudaMalloc(&as_buffer, compacted_size); - optixAccelCompact( optix_context, + optixAccelCompact( _context, 0, // stream - optix_as_handle, + _as_handle, (CUdeviceptr) as_buffer, compacted_size, - &optix_as_handle + &_as_handle ); cudaDeviceSynchronize(); @@ -407,10 +407,10 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, cudaFree(d_temp_buffer); cudaFree(d_compacted_size); - return optix_as_handle; + return _as_handle; } -void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, uint32_t & optix_trig_flags, const che * mesh, const mat4 & model_mat) +void optix::add_mesh(OptixBuildInput & _mesh, CUdeviceptr & d_vertex_ptr, uint32_t & _trig_flags, const che * mesh, const mat4 & model_mat) { che * d_m = new che_cuda(mesh); d_mesh.push_back(d_m); @@ -421,37 +421,37 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u d_vertex_ptr = (CUdeviceptr) &d_m->point(0); - optix_mesh = {}; - optix_mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; + _mesh = {}; + _mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; - optix_mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; - optix_mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); - optix_mesh.triangleArray.numVertices = d_m->n_vertices; - optix_mesh.triangleArray.vertexBuffers = &d_vertex_ptr; + _mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; + _mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); + _mesh.triangleArray.numVertices = d_m->n_vertices; + _mesh.triangleArray.vertexBuffers = &d_vertex_ptr; - optix_mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; - optix_mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - optix_mesh.triangleArray.numIndexTriplets = d_m->n_trigs; - optix_mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->trigs_ptr(); + _mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; + _mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); + _mesh.triangleArray.numIndexTriplets = d_m->n_trigs; + _mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->trigs_ptr(); - optix_mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; - optix_mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; + _mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; + _mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; - optix_trig_flags = 0; + _trig_flags = 0; - optix_mesh.triangleArray.flags = &optix_trig_flags; - optix_mesh.triangleArray.numSbtRecords = 1; - optix_mesh.triangleArray.sbtIndexOffsetBuffer = 0; - optix_mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; - optix_mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; + _mesh.triangleArray.flags = &_trig_flags; + _mesh.triangleArray.numSbtRecords = 1; + _mesh.triangleArray.sbtIndexOffsetBuffer = 0; + _mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; + _mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; if(mesh->is_scene()) { scene * sc = (scene *) mesh; - cudaMalloc(&optix_params.sc.materials, size(sc->materials) * sizeof(scene::material)); - cudaMalloc(&optix_params.sc.textures, size(sc->textures) * sizeof(scene::texture)); - cudaMalloc(&optix_params.sc.trig_mat, mesh->n_vertices / 3 * sizeof(index_t)); - cudaMalloc(&optix_params.sc.texcoords, mesh->n_vertices * sizeof(vec2)); + cudaMalloc(¶ms.sc.materials, size(sc->materials) * sizeof(scene::material)); + cudaMalloc(¶ms.sc.textures, size(sc->textures) * sizeof(scene::texture)); + cudaMalloc(¶ms.sc.trig_mat, mesh->n_vertices / 3 * sizeof(index_t)); + cudaMalloc(¶ms.sc.texcoords, mesh->n_vertices * sizeof(vec2)); std::vector textures = sc->textures; for(scene::texture & tex: textures) @@ -463,10 +463,10 @@ void optix::add_mesh(OptixBuildInput & optix_mesh, CUdeviceptr & d_vertex_ptr, u } gproshan_error_var(size(textures)); - cudaMemcpy(optix_params.sc.materials, sc->materials.data(), size(sc->materials) * sizeof(scene::material), cudaMemcpyHostToDevice); - cudaMemcpy(optix_params.sc.textures, textures.data(), size(textures) * sizeof(scene::texture), cudaMemcpyHostToDevice); - cudaMemcpy(optix_params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); - cudaMemcpy(optix_params.sc.texcoords, sc->texcoords, mesh->n_vertices * sizeof(vec2), cudaMemcpyHostToDevice); + cudaMemcpy(params.sc.materials, sc->materials.data(), size(sc->materials) * sizeof(scene::material), cudaMemcpyHostToDevice); + cudaMemcpy(params.sc.textures, textures.data(), size(textures) * sizeof(scene::texture), cudaMemcpyHostToDevice); + cudaMemcpy(params.sc.trig_mat, sc->trig_mat, mesh->n_vertices / 3 * sizeof(index_t), cudaMemcpyHostToDevice); + cudaMemcpy(params.sc.texcoords, sc->texcoords, mesh->n_vertices * sizeof(vec2), cudaMemcpyHostToDevice); } } diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 3c03a1a6..efb2695f 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -10,13 +10,7 @@ namespace gproshan::rt { -extern "C" __constant__ launch_params optix_params; - -static __forceinline__ __device__ -void * unpack_pointer(uint32_t i0, uint32_t i1) -{ - return (void *) (uint64_t(i0) << 32 | i1); -} +extern "C" __constant__ launch_params params; static __forceinline__ __device__ void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) @@ -26,83 +20,13 @@ void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) i1 = uptr & 0x00000000ffffffff; } -template -static __forceinline__ __device__ -T * ray_data() -{ - return (T *) unpack_pointer(optixGetPayload_0(), optixGetPayload_1()); -} - extern "C" __global__ void __closesthit__shadow() {} -extern "C" __global__ void __closesthit__radiance() -{ - const che & mesh = **(const che **) optixGetSbtDataPointer(); - - const int primID = optixGetPrimitiveIndex(); - const float2 bar = optixGetTriangleBarycentrics(); - - OptixTraversableHandle gas = optixGetGASTraversableHandle(); - const index_t sbtID = optixGetSbtGASIndex(); - const float time = optixGetRayTime(); - - vertex data[3]; - optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); - - const vertex & A = data[0]; - const vertex & B = data[1]; - const vertex & C = data[2]; - - eval_hit hit(mesh, primID, bar.x, bar.y, optix_params.sc); - hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; - hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - - vec3 * trace = ray_data(); - vec3 & color = trace[0]; - vec3 & attenuation = trace[1]; - vec3 & position = trace[2]; - vec3 & ray_dir = trace[3]; - - color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, - [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool - { - uint32_t occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); - - return occluded != 0; - }); - - random rnd = optixGetPayload_2(); - color *= attenuation; - position = hit.position; - - if(!hit.scatter_mat(ray_dir, rnd)) - attenuation = 0; - - attenuation /= 2; - optixSetPayload_2(rnd); -} - - extern "C" __global__ void __anyhit__radiance() {} extern "C" __global__ void __anyhit__shadow() {} - extern "C" __global__ void __miss__radiance() { optixSetPayload_0(0); @@ -120,12 +44,12 @@ extern "C" __global__ void __raygen__render_frame() optixGetLaunchIndex().y }; - const uvec2 & pos = id + optix_params.viewport_pos; + const uvec2 & pos = id + params.viewport_pos; - random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); + random rnd(pos.x() + params.window_size.x() * pos.y(), params.n_frames); - unsigned int depth = optix_params.depth; - unsigned int samples = optix_params.n_samples; + unsigned int depth = params.depth; + unsigned int samples = params.n_samples; vec3 color_acc = 0; @@ -142,16 +66,16 @@ extern "C" __global__ void __raygen__render_frame() { color = 0; attenuation = 1; - position = optix_params.cam_pos; - ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); + position = params.cam_pos; + ray_dir = ray_view_dir(pos, params.window_size, params.inv_proj_view, params.cam_pos, rnd); dist = 0; - depth = optix_params.depth; + depth = params.depth; do { pack_pointer(trace, u0, u1); - optixTrace( optix_params.traversable, + optixTrace( params.traversable, * (float3 *) &position, * (float3 *) &ray_dir, 1e-5f, // tmin @@ -171,10 +95,10 @@ extern "C" __global__ void __raygen__render_frame() } while(--samples); - color_acc /= optix_params.n_samples; + color_acc /= params.n_samples; - vec4 & pixel_color = optix_params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; - pixel_color = (pixel_color * optix_params.n_frames + (color_acc, 1)) / (optix_params.n_frames + 1); + vec4 & pixel_color = params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; + pixel_color = (pixel_color * params.n_frames + (color_acc, 1)) / (params.n_frames + 1); } diff --git a/src/gproshan/raytracing/optix_path_tracing.cu b/src/gproshan/raytracing/optix_path_tracing.cu new file mode 100644 index 00000000..3c03a1a6 --- /dev/null +++ b/src/gproshan/raytracing/optix_path_tracing.cu @@ -0,0 +1,182 @@ +#include +#include +#include + +#include +#include + + +// geometry processing and shape analysis framework +namespace gproshan::rt { + + +extern "C" __constant__ launch_params optix_params; + +static __forceinline__ __device__ +void * unpack_pointer(uint32_t i0, uint32_t i1) +{ + return (void *) (uint64_t(i0) << 32 | i1); +} + +static __forceinline__ __device__ +void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) +{ + const uint64_t uptr = uint64_t(ptr); + i0 = uptr >> 32; + i1 = uptr & 0x00000000ffffffff; +} + +template +static __forceinline__ __device__ +T * ray_data() +{ + return (T *) unpack_pointer(optixGetPayload_0(), optixGetPayload_1()); +} + + +extern "C" __global__ void __closesthit__shadow() {} + +extern "C" __global__ void __closesthit__radiance() +{ + const che & mesh = **(const che **) optixGetSbtDataPointer(); + + const int primID = optixGetPrimitiveIndex(); + const float2 bar = optixGetTriangleBarycentrics(); + + OptixTraversableHandle gas = optixGetGASTraversableHandle(); + const index_t sbtID = optixGetSbtGASIndex(); + const float time = optixGetRayTime(); + + vertex data[3]; + optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); + + const vertex & A = data[0]; + const vertex & B = data[1]; + const vertex & C = data[2]; + + eval_hit hit(mesh, primID, bar.x, bar.y, optix_params.sc); + hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; + hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; + + vec3 * trace = ray_data(); + vec3 & color = trace[0]; + vec3 & attenuation = trace[1]; + vec3 & position = trace[2]; + vec3 & ray_dir = trace[3]; + + color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, + [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool + { + uint32_t occluded = 1; + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + + return occluded != 0; + }); + + random rnd = optixGetPayload_2(); + color *= attenuation; + position = hit.position; + + if(!hit.scatter_mat(ray_dir, rnd)) + attenuation = 0; + + attenuation /= 2; + optixSetPayload_2(rnd); +} + + +extern "C" __global__ void __anyhit__radiance() {} + +extern "C" __global__ void __anyhit__shadow() {} + + +extern "C" __global__ void __miss__radiance() +{ + optixSetPayload_0(0); +} + +extern "C" __global__ void __miss__shadow() +{ + optixSetPayload_0(0); +} + + +extern "C" __global__ void __raygen__render_frame() +{ + const uvec2 & id = {optixGetLaunchIndex().x, + optixGetLaunchIndex().y + }; + + const uvec2 & pos = id + optix_params.viewport_pos; + + random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); + + unsigned int depth = optix_params.depth; + unsigned int samples = optix_params.n_samples; + + vec3 color_acc = 0; + + vec3 trace[4]; + vec3 & color = trace[0]; + vec3 & attenuation = trace[1]; + vec3 & position = trace[2]; + vec3 & ray_dir = trace[3]; + + uint32_t u0, u1; + unsigned int dist; + + do + { + color = 0; + attenuation = 1; + position = optix_params.cam_pos; + ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); + + dist = 0; + + depth = optix_params.depth; + do + { + pack_pointer(trace, u0, u1); + optixTrace( optix_params.traversable, + * (float3 *) &position, + * (float3 *) &ray_dir, + 1e-5f, // tmin + 1e20f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, + 0, // SBT offset + 2, // SBT stride + 0, // missSBTIndex + u0, u1, (unsigned int &) rnd, dist); + + if(!u0) break; // miss + color_acc += color; + } + while(--depth); + } + while(--samples); + + color_acc /= optix_params.n_samples; + + vec4 & pixel_color = optix_params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; + pixel_color = (pixel_color * optix_params.n_frames + (color_acc, 1)) / (optix_params.n_frames + 1); +} + + +} // namespace gproshan + From 2b040801ddede501cdb856d04ca84ce656f45181 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 5 Aug 2024 16:01:15 +0200 Subject: [PATCH 0996/1018] rt: errors optix render --- include/gproshan/raytracing/optix.h | 3 +- src/gproshan/raytracing/optix.cpp | 81 ++++---- src/gproshan/raytracing/optix.cu | 77 +++++++- src/gproshan/raytracing/optix_path_tracing.cu | 182 ------------------ 4 files changed, 110 insertions(+), 233 deletions(-) delete mode 100644 src/gproshan/raytracing/optix_path_tracing.cu diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index d5511cf7..ab7bc070 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -26,8 +26,7 @@ class optix : public raytracing OptixDeviceContext _context; - OptixModule _module_raygen; - OptixModule _module_radiance; + OptixModule _module; OptixModuleCompileOptions _module_compile_opt = {}; OptixPipeline _pipeline; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index ff27a7ac..2a07fb33 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -44,7 +44,6 @@ optix::optix(const std::string & ptx) { optixInit(); - // create context cudaStreamCreate(&stream); cuCtxGetCurrent(&cuda_context); @@ -52,14 +51,8 @@ optix::optix(const std::string & ptx) optixDeviceContextCreate(cuda_context, 0, &_context); optixDeviceContextSetLogCallback(_context, optix_log, nullptr, 4); - // create module - - //_module_compile_opt.maxRegisterCount = 50; - _module_compile_opt.optLevel = OPTIX_COMPILE_OPTIMIZATION_DEFAULT; - _module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; - _pipeline_compile_opt = {}; - _pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; + _pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; _pipeline_compile_opt.usesMotionBlur = false; _pipeline_compile_opt.numPayloadValues = 4; _pipeline_compile_opt.numAttributeValues = 4; @@ -78,28 +71,21 @@ optix::optix(const std::string & ptx) str_ptx_code.c_str(), size(str_ptx_code), nullptr, nullptr, // log message - &_module_raygen + &_module ); - - // create programs create_raygen_programs(); create_miss_programs(); create_hitgroup_programs(); - // create pipeline create_pipeline(); - // launch params cudaMalloc(¶ms_buffer, sizeof(launch_params)); } optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() { - // build as params.traversable = build_as(meshes, model_mats); - - // build sbt build_sbt(); } @@ -123,25 +109,25 @@ optix::~optix() cudaFree(data); } -void optix::render(vec4 * img, const render_params & p, const bool flat) +void optix::render(vec4 * img, const render_params & rp, const bool flat) { - params.depth = p.depth; - params.n_frames = p.n_frames; - params.n_samples = p.n_samples; + params.depth = rp.depth; + params.n_frames = rp.n_frames; + params.n_samples = rp.n_samples; params.color_buffer = img; - params.window_size = p.window_size; - if(p.viewport_is_window) - params.window_size = p.viewport_size; + params.window_size = rp.window_size; + if(rp.viewport_is_window) + params.window_size = rp.viewport_size; - params.viewport_pos = p.viewport_pos; + params.viewport_pos = rp.viewport_pos; params.flat = flat; - params.cam_pos = p.cam_pos; - params.inv_proj_view = p.inv_proj_view; - params.ambient = p.ambient; - params.n_lights = p.n_lights; - memcpy(params.lights, p.lights, sizeof(params.lights)); + params.cam_pos = rp.cam_pos; + params.inv_proj_view = rp.inv_proj_view; + params.ambient = rp.ambient; + params.n_lights = rp.n_lights; + memcpy(params.lights, rp.lights, sizeof(params.lights)); cudaMemcpy(params_buffer, ¶ms, sizeof(launch_params), cudaMemcpyHostToDevice); @@ -150,8 +136,8 @@ void optix::render(vec4 * img, const render_params & p, const bool flat) (CUdeviceptr) params_buffer, sizeof(launch_params), &sbt, - p.viewport_size.x(), - p.viewport_size.y(), + rp.viewport_size.x(), + rp.viewport_size.y(), 1 ); @@ -166,7 +152,7 @@ void optix::create_raygen_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN; - pg_desc.raygen.module = _module_raygen; + pg_desc.raygen.module = _module; pg_desc.raygen.entryFunctionName = "__raygen__render_frame"; optixProgramGroupCreate(_context, @@ -188,7 +174,7 @@ void optix::create_miss_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_MISS; - pg_desc.miss.module = _module_radiance; + pg_desc.miss.module = _module; pg_desc.miss.entryFunctionName = "__miss__radiance"; @@ -225,8 +211,8 @@ void optix::create_hitgroup_programs() OptixProgramGroupOptions pg_options = {}; OptixProgramGroupDesc pg_desc = {}; pg_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP; - pg_desc.hitgroup.moduleCH = _module_radiance; - pg_desc.hitgroup.moduleAH = _module_radiance; + pg_desc.hitgroup.moduleCH = _module; + pg_desc.hitgroup.moduleAH = _module; pg_desc.hitgroup.entryFunctionNameCH = "__closesthit__radiance"; @@ -340,10 +326,9 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, for(index_t i = 0; i < size(meshes); ++i) add_mesh(_meshes[i], _vertex_ptr[i], _trig_flags[i], meshes[i], model_mats[i]); - OptixAccelBuildOptions _accel_opt = {}; - _accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS | - OPTIX_BUILD_FLAG_ALLOW_COMPACTION; - _accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; + OptixAccelBuildOptions _accel_opt = {}; + _accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_COMPACTION; + _accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; OptixAccelBufferSizes _gas_buffer_size; optixAccelComputeMemoryUsage( _context, @@ -420,25 +405,25 @@ void optix::add_mesh(OptixBuildInput & _mesh, CUdeviceptr & d_vertex_ptr, uint32 _mesh = {}; _mesh.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES; - _mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; + _mesh.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3; _mesh.triangleArray.vertexStrideInBytes = 3 * sizeof(float); _mesh.triangleArray.numVertices = d_m->n_vertices; - _mesh.triangleArray.vertexBuffers = &d_vertex_ptr; + _mesh.triangleArray.vertexBuffers = &d_vertex_ptr; _mesh.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3; - _mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); - _mesh.triangleArray.numIndexTriplets = d_m->n_trigs; + _mesh.triangleArray.indexStrideInBytes = 3 * sizeof(index_t); + _mesh.triangleArray.numIndexTriplets = d_m->n_trigs; _mesh.triangleArray.indexBuffer = (CUdeviceptr) d_m->trigs_ptr(); _mesh.triangleArray.transformFormat = OPTIX_TRANSFORM_FORMAT_MATRIX_FLOAT12; - _mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; + _mesh.triangleArray.preTransform = (CUdeviceptr) d_model_mat; _trig_flags = 0; - _mesh.triangleArray.flags = &_trig_flags; - _mesh.triangleArray.numSbtRecords = 1; - _mesh.triangleArray.sbtIndexOffsetBuffer = 0; - _mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; + _mesh.triangleArray.flags = &_trig_flags; + _mesh.triangleArray.numSbtRecords = 1; + _mesh.triangleArray.sbtIndexOffsetBuffer = 0; + _mesh.triangleArray.sbtIndexOffsetSizeInBytes = 0; _mesh.triangleArray.sbtIndexOffsetStrideInBytes = 0; if(mesh->is_scene()) diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index efb2695f..35fb7e25 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -12,6 +12,7 @@ namespace gproshan::rt { extern "C" __constant__ launch_params params; + static __forceinline__ __device__ void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) { @@ -20,13 +21,87 @@ void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) i1 = uptr & 0x00000000ffffffff; } +static __forceinline__ __device__ +void * unpack_pointer(uint32_t i0, uint32_t i1) +{ + return (void *) (uint64_t(i0) << 32 | i1); +} + +template +static __forceinline__ __device__ +T * ray_data() +{ + return (T *) unpack_pointer(optixGetPayload_0(), optixGetPayload_1()); +} + extern "C" __global__ void __closesthit__shadow() {} -extern "C" __global__ void __anyhit__radiance() {} +extern "C" __global__ void __closesthit__radiance() +{ + const che & mesh = **(const che **) optixGetSbtDataPointer(); + + const int primID = optixGetPrimitiveIndex(); + const float2 bar = optixGetTriangleBarycentrics(); + + OptixTraversableHandle gas = optixGetGASTraversableHandle(); + const index_t sbtID = optixGetSbtGASIndex(); + const float time = optixGetRayTime(); + + vertex data[3]; + optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); + + const vertex & A = data[0]; + const vertex & B = data[1]; + const vertex & C = data[2]; + + eval_hit hit(mesh, primID, bar.x, bar.y, params.sc); + hit.normal = params.flat ? normalize(cross(B - A, C - A)) : hit.normal; + hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; + + vec3 * trace = ray_data(); + vec3 & color = trace[0]; + vec3 & attenuation = trace[1]; + vec3 & position = trace[2]; + vec3 & ray_dir = trace[3]; + + color = eval_li(hit, params.ambient, params.lights, params.n_lights, params.cam_pos, + [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool + { + uint32_t occluded = 1; + optixTrace( params.traversable, + * (float3 *) &position, + * (float3 *) &wi, + 1e-3f, // tmin + light_dist - 1e-3f, // tmax + 0.0f, // rayTime + OptixVisibilityMask(255), + OPTIX_RAY_FLAG_DISABLE_ANYHIT + | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT + | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, + 1, // SBT offset + 2, // SBT stride + 1, // missSBTIndex + occluded); + + return occluded != 0; + }); + + random rnd = optixGetPayload_2(); + color *= attenuation; + position = hit.position; + + if(!hit.scatter_mat(ray_dir, rnd)) + attenuation = 0; + + attenuation /= 2; + optixSetPayload_2(rnd); +} extern "C" __global__ void __anyhit__shadow() {} +extern "C" __global__ void __anyhit__radiance() {} + extern "C" __global__ void __miss__radiance() { optixSetPayload_0(0); diff --git a/src/gproshan/raytracing/optix_path_tracing.cu b/src/gproshan/raytracing/optix_path_tracing.cu deleted file mode 100644 index 3c03a1a6..00000000 --- a/src/gproshan/raytracing/optix_path_tracing.cu +++ /dev/null @@ -1,182 +0,0 @@ -#include -#include -#include - -#include -#include - - -// geometry processing and shape analysis framework -namespace gproshan::rt { - - -extern "C" __constant__ launch_params optix_params; - -static __forceinline__ __device__ -void * unpack_pointer(uint32_t i0, uint32_t i1) -{ - return (void *) (uint64_t(i0) << 32 | i1); -} - -static __forceinline__ __device__ -void pack_pointer(void * ptr, uint32_t & i0, uint32_t & i1) -{ - const uint64_t uptr = uint64_t(ptr); - i0 = uptr >> 32; - i1 = uptr & 0x00000000ffffffff; -} - -template -static __forceinline__ __device__ -T * ray_data() -{ - return (T *) unpack_pointer(optixGetPayload_0(), optixGetPayload_1()); -} - - -extern "C" __global__ void __closesthit__shadow() {} - -extern "C" __global__ void __closesthit__radiance() -{ - const che & mesh = **(const che **) optixGetSbtDataPointer(); - - const int primID = optixGetPrimitiveIndex(); - const float2 bar = optixGetTriangleBarycentrics(); - - OptixTraversableHandle gas = optixGetGASTraversableHandle(); - const index_t sbtID = optixGetSbtGASIndex(); - const float time = optixGetRayTime(); - - vertex data[3]; - optixGetTriangleVertexData(gas, primID, sbtID, time, (float3 *) data); - - const vertex & A = data[0]; - const vertex & B = data[1]; - const vertex & C = data[2]; - - eval_hit hit(mesh, primID, bar.x, bar.y, optix_params.sc); - hit.normal = optix_params.flat ? normalize(cross(B - A, C - A)) : hit.normal; - hit.position = (1.f - hit.u - hit.v) * A + hit.u * B + hit.v * C; - - vec3 * trace = ray_data(); - vec3 & color = trace[0]; - vec3 & attenuation = trace[1]; - vec3 & position = trace[2]; - vec3 & ray_dir = trace[3]; - - color = eval_li(hit, optix_params.ambient, optix_params.lights, optix_params.n_lights, optix_params.cam_pos, - [&](const vec3 & position, const vec3 & wi, const float light_dist) -> bool - { - uint32_t occluded = 1; - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &wi, - 1e-3f, // tmin - light_dist - 1e-3f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT - | OPTIX_RAY_FLAG_DISABLE_CLOSESTHIT - | OPTIX_RAY_FLAG_TERMINATE_ON_FIRST_HIT, - 1, // SBT offset - 2, // SBT stride - 1, // missSBTIndex - occluded); - - return occluded != 0; - }); - - random rnd = optixGetPayload_2(); - color *= attenuation; - position = hit.position; - - if(!hit.scatter_mat(ray_dir, rnd)) - attenuation = 0; - - attenuation /= 2; - optixSetPayload_2(rnd); -} - - -extern "C" __global__ void __anyhit__radiance() {} - -extern "C" __global__ void __anyhit__shadow() {} - - -extern "C" __global__ void __miss__radiance() -{ - optixSetPayload_0(0); -} - -extern "C" __global__ void __miss__shadow() -{ - optixSetPayload_0(0); -} - - -extern "C" __global__ void __raygen__render_frame() -{ - const uvec2 & id = {optixGetLaunchIndex().x, - optixGetLaunchIndex().y - }; - - const uvec2 & pos = id + optix_params.viewport_pos; - - random rnd(pos.x() + optix_params.window_size.x() * pos.y(), optix_params.n_frames); - - unsigned int depth = optix_params.depth; - unsigned int samples = optix_params.n_samples; - - vec3 color_acc = 0; - - vec3 trace[4]; - vec3 & color = trace[0]; - vec3 & attenuation = trace[1]; - vec3 & position = trace[2]; - vec3 & ray_dir = trace[3]; - - uint32_t u0, u1; - unsigned int dist; - - do - { - color = 0; - attenuation = 1; - position = optix_params.cam_pos; - ray_dir = ray_view_dir(pos, optix_params.window_size, optix_params.inv_proj_view, optix_params.cam_pos, rnd); - - dist = 0; - - depth = optix_params.depth; - do - { - pack_pointer(trace, u0, u1); - optixTrace( optix_params.traversable, - * (float3 *) &position, - * (float3 *) &ray_dir, - 1e-5f, // tmin - 1e20f, // tmax - 0.0f, // rayTime - OptixVisibilityMask(255), - OPTIX_RAY_FLAG_DISABLE_ANYHIT, //OPTIX_RAY_FLAG_NONE, - 0, // SBT offset - 2, // SBT stride - 0, // missSBTIndex - u0, u1, (unsigned int &) rnd, dist); - - if(!u0) break; // miss - color_acc += color; - } - while(--depth); - } - while(--samples); - - color_acc /= optix_params.n_samples; - - vec4 & pixel_color = optix_params.color_buffer[id.x() + id.y() * optixGetLaunchDimensions().x]; - pixel_color = (pixel_color * optix_params.n_frames + (color_acc, 1)) / (optix_params.n_frames + 1); -} - - -} // namespace gproshan - From d699d763485f6e448191cdd3caaf99aa34e0c8f6 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 6 Aug 2024 15:59:19 +0200 Subject: [PATCH 0997/1018] rt: fix optix, renaming optix params --- include/gproshan/raytracing/optix.h | 4 ++-- include/gproshan/raytracing/optix_params.h | 2 +- src/gproshan/raytracing/optix.cpp | 9 +++++---- src/gproshan/raytracing/optix.cu | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index ab7bc070..2ce882da 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -39,8 +39,8 @@ class optix : public raytracing OptixShaderBindingTable sbt = {}; - launch_params params; - launch_params * params_buffer = nullptr; + optix_params params; + optix_params * params_buffer = nullptr; std::vector d_mesh; diff --git a/include/gproshan/raytracing/optix_params.h b/include/gproshan/raytracing/optix_params.h index 41b95310..bd0ff35a 100644 --- a/include/gproshan/raytracing/optix_params.h +++ b/include/gproshan/raytracing/optix_params.h @@ -17,7 +17,7 @@ namespace gproshan::rt { -struct launch_params: public base_params +struct optix_params: public base_params { OptixTraversableHandle traversable; diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 2a07fb33..1f4ff06f 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -80,7 +80,7 @@ optix::optix(const std::string & ptx) create_pipeline(); - cudaMalloc(¶ms_buffer, sizeof(launch_params)); + cudaMalloc(¶ms_buffer, sizeof(optix_params)); } optix::optix(const std::vector & meshes, const std::vector & model_mats): optix() @@ -129,12 +129,12 @@ void optix::render(vec4 * img, const render_params & rp, const bool flat) params.n_lights = rp.n_lights; memcpy(params.lights, rp.lights, sizeof(params.lights)); - cudaMemcpy(params_buffer, ¶ms, sizeof(launch_params), cudaMemcpyHostToDevice); + cudaMemcpy(params_buffer, ¶ms, sizeof(optix_params), cudaMemcpyHostToDevice); optixLaunch(_pipeline, stream, (CUdeviceptr) params_buffer, - sizeof(launch_params), + sizeof(optix_params), &sbt, rp.viewport_size.x(), rp.viewport_size.y(), @@ -327,7 +327,8 @@ OptixTraversableHandle optix::build_as(const std::vector & meshes, add_mesh(_meshes[i], _vertex_ptr[i], _trig_flags[i], meshes[i], model_mats[i]); OptixAccelBuildOptions _accel_opt = {}; - _accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_COMPACTION; + _accel_opt.buildFlags = OPTIX_BUILD_FLAG_ALLOW_RANDOM_VERTEX_ACCESS | + OPTIX_BUILD_FLAG_ALLOW_COMPACTION; _accel_opt.operation = OPTIX_BUILD_OPERATION_BUILD; OptixAccelBufferSizes _gas_buffer_size; diff --git a/src/gproshan/raytracing/optix.cu b/src/gproshan/raytracing/optix.cu index 35fb7e25..c3fb2ffa 100644 --- a/src/gproshan/raytracing/optix.cu +++ b/src/gproshan/raytracing/optix.cu @@ -10,7 +10,7 @@ namespace gproshan::rt { -extern "C" __constant__ launch_params params; +extern "C" __constant__ optix_params params; static __forceinline__ __device__ From 72efe30c43f2b08d5642be8df3abca5754e9f157 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Sat, 17 Aug 2024 21:34:08 +0200 Subject: [PATCH 0998/1018] update imgui v1.91.0 --- include/imgui/imconfig.h | 13 +- include/imgui/imgui.h | 747 ++++-- include/imgui/imgui_impl_glfw.h | 6 +- include/imgui/imgui_impl_opengl3.h | 8 +- include/imgui/imgui_impl_opengl3_loader.h | 126 +- include/imgui/imgui_internal.h | 634 +++-- include/imgui/imstb_textedit.h | 2 +- include/imgui/imstb_truetype.h | 2 +- src/imgui/imgui.cpp | 1784 +++++++++----- src/imgui/imgui_demo.cpp | 2624 +++++++++++++++++---- src/imgui/imgui_draw.cpp | 384 ++- src/imgui/imgui_impl_glfw.cpp | 86 +- src/imgui/imgui_impl_opengl3.cpp | 53 +- src/imgui/imgui_tables.cpp | 226 +- src/imgui/imgui_widgets.cpp | 1792 +++++++++++--- 15 files changed, 6491 insertions(+), 1996 deletions(-) diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index d556cbaf..6e4c743d 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -21,10 +21,11 @@ //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows // Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility. -// DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() -// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. -//#define IMGUI_API __declspec( dllexport ) -//#define IMGUI_API __declspec( dllimport ) +// - Windows DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions() +// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details. +//#define IMGUI_API __declspec(dllexport) // MSVC Windows: DLL export +//#define IMGUI_API __declspec(dllimport) // MSVC Windows: DLL import +//#define IMGUI_API __attribute__((visibility("default"))) // GCC/Clang: override visibility when set is hidden //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names. //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -42,6 +43,7 @@ //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a) //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME). //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). +//#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS // Don't implement default io.PlatformOpenInShellFn() handler (Win32: ShellExecute(), require shell32.lib/.a, Mac/Linux: use system("")). //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. //#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) @@ -49,6 +51,9 @@ //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions(). //#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available +//---- Enable Test Engine / Automation features. +//#define IMGUI_ENABLE_TEST_ENGINE // Enable imgui_test_engine hooks. Generally set automatically by include "imgui_te_config.h", see Test Engine for details. + //---- Include imgui_user.h at the end of imgui.h as a convenience // May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included. //#define IMGUI_INCLUDE_IMGUI_USER_H diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index 386fb583..05e48a17 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (headers) // Help: @@ -7,15 +7,19 @@ // - Read top of imgui.cpp for more details, links and comments. // Resources: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Homepage https://github.com/ocornut/imgui -// - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/6897 (please post your screenshots/video there!) -// - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) -// - Glossary https://github.com/ocornut/imgui/wiki/Glossary -// - Issues & support https://github.com/ocornut/imgui/issues -// - Tests & Automation https://github.com/ocornut/imgui_test_engine +// - FAQ ........................ https://dearimgui.com/faq (in repository as docs/FAQ.md) +// - Homepage ................... https://github.com/ocornut/imgui +// - Releases & changelog ....... https://github.com/ocornut/imgui/releases +// - Gallery .................... https://github.com/ocornut/imgui/issues/7503 (please post your screenshots/video there!) +// - Wiki ....................... https://github.com/ocornut/imgui/wiki (lots of good stuff there) +// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started (how to integrate in an existing app by adding ~25 lines of code) +// - Third-party Extensions https://github.com/ocornut/imgui/wiki/Useful-Extensions (ImPlot & many more) +// - Bindings/Backends https://github.com/ocornut/imgui/wiki/Bindings (language bindings, backends for various tech/engines) +// - Glossary https://github.com/ocornut/imgui/wiki/Glossary +// - Debug Tools https://github.com/ocornut/imgui/wiki/Debug-Tools +// - Software using Dear ImGui https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui +// - Issues & support ........... https://github.com/ocornut/imgui/issues +// - Test Engine & Automation ... https://github.com/ocornut/imgui_test_engine (test suite, test engine to automate your apps) // For first-time users having issues compiling/linking/running/loading fonts: // please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. @@ -23,8 +27,8 @@ // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') -#define IMGUI_VERSION "1.90.4" -#define IMGUI_VERSION_NUM 19040 +#define IMGUI_VERSION "1.91.0" +#define IMGUI_VERSION_NUM 19100 #define IMGUI_HAS_TABLE /* @@ -35,11 +39,12 @@ Index of this file: // [SECTION] Dear ImGui end-user API functions // [SECTION] Flags & Enumerations // [SECTION] Tables API flags and structures (ImGuiTableFlags, ImGuiTableColumnFlags, ImGuiTableRowFlags, ImGuiTableBgTarget, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs) -// [SECTION] Helpers: Memory allocations macros, ImVector<> +// [SECTION] Helpers: Debug log, Memory allocations macros, ImVector<> // [SECTION] ImGuiStyle // [SECTION] ImGuiIO // [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload) // [SECTION] Helpers (ImGuiOnceUponAFrame, ImGuiTextFilter, ImGuiTextBuffer, ImGuiStorage, ImGuiListClipper, Math Operators, ImColor) +// [SECTION] Multi-Select API flags and structures (ImGuiMultiSelectFlags, ImGuiMultiSelectIO, ImGuiSelectionRequest, ImGuiSelectionBasicStorage, ImGuiSelectionExternalStorage) // [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) @@ -86,6 +91,8 @@ Index of this file: #endif #define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR) / sizeof(*(_ARR)))) // Size of a static C-style array. Don't use on pointers! #define IM_UNUSED(_VAR) ((void)(_VAR)) // Used to silence "unused variable warnings". Often useful as asserts may be stripped out from final builds. + +// Check that version and structures layouts are matching between compiled imgui code and caller. Read comments above DebugCheckVersionAndDataLayout() for details. #define IMGUI_CHECKVERSION() ImGui::DebugCheckVersionAndDataLayout(IMGUI_VERSION, sizeof(ImGuiIO), sizeof(ImGuiStyle), sizeof(ImVec2), sizeof(ImVec4), sizeof(ImDrawVert), sizeof(ImDrawIdx)) // Helper Macros - IM_FMTARGS, IM_FMTLIST: Apply printf-style warnings to our formatting functions. @@ -126,6 +133,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" #pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -136,6 +144,17 @@ Index of this file: // [SECTION] Forward declarations and basic types //----------------------------------------------------------------------------- +// Scalar data types +typedef unsigned int ImGuiID;// A unique ID used by widgets (typically the result of hashing a stack of string) +typedef signed char ImS8; // 8-bit signed integer +typedef unsigned char ImU8; // 8-bit unsigned integer +typedef signed short ImS16; // 16-bit signed integer +typedef unsigned short ImU16; // 16-bit unsigned integer +typedef signed int ImS32; // 32-bit signed integer == int +typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors) +typedef signed long long ImS64; // 64-bit signed integer +typedef unsigned long long ImU64; // 64-bit unsigned integer + // Forward declarations struct ImDrawChannel; // Temporary storage to output draw commands out of order, used by ImDrawListSplitter and ImDrawList::ChannelsSplit() struct ImDrawCmd; // A single draw command within a parent ImDrawList (generally maps to 1 GPU draw call, unless it is a callback) @@ -156,11 +175,16 @@ struct ImGuiIO; // Main configuration and I/O between your a struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) struct ImGuiKeyData; // Storage for ImGuiIO and IsKeyDown(), IsKeyPressed() etc functions. struct ImGuiListClipper; // Helper to manually clip large list of items +struct ImGuiMultiSelectIO; // Structure to interact with a BeginMultiSelect()/EndMultiSelect() block struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame struct ImGuiPayload; // User data payload for drag and drop operations -struct ImGuiPlatformImeData; // Platform IME data for io.SetPlatformImeDataFn() function. +struct ImGuiPlatformImeData; // Platform IME data for io.PlatformSetImeDataFn() function. +struct ImGuiSelectionBasicStorage; // Optional helper to store multi-selection state + apply multi-selection requests. +struct ImGuiSelectionExternalStorage;//Optional helper to apply multi-selection requests to existing randomly accessible storage. +struct ImGuiSelectionRequest; // A selection request (stored in ImGuiMultiSelectIO) struct ImGuiSizeCallbackData; // Callback data when using SetNextWindowSizeConstraints() (rare/advanced use) -struct ImGuiStorage; // Helper for key->value storage +struct ImGuiStorage; // Helper for key->value storage (container sorted by key) +struct ImGuiStoragePair; // Helper for key->value storage (pair) struct ImGuiStyle; // Runtime data for styling/colors struct ImGuiTableSortSpecs; // Sorting specifications for a table (often handling sort specs for a single column, occasionally more) struct ImGuiTableColumnSortSpecs; // Sorting specification for one column of a table @@ -171,24 +195,26 @@ struct ImGuiViewport; // A Platform Window (always only one in 'ma // Enumerations // - We don't use strongly typed enums much because they add constraints (can't extend in private code, can't store typed in bit fields, extra casting on iteration) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! -// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. -// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments. +// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments. +enum ImGuiDir : int; // -> enum ImGuiDir // Enum: A cardinal direction (Left, Right, Up, Down) enum ImGuiKey : int; // -> enum ImGuiKey // Enum: A key identifier (ImGuiKey_XXX or ImGuiMod_XXX value) enum ImGuiMouseSource : int; // -> enum ImGuiMouseSource // Enum; A mouse input source identifier (Mouse, TouchScreen, Pen) +enum ImGuiSortDirection : ImU8; // -> enum ImGuiSortDirection // Enum: A sorting direction (ascending or descending) typedef int ImGuiCol; // -> enum ImGuiCol_ // Enum: A color identifier for styling typedef int ImGuiCond; // -> enum ImGuiCond_ // Enum: A condition for many Set*() functions typedef int ImGuiDataType; // -> enum ImGuiDataType_ // Enum: A primary data type -typedef int ImGuiDir; // -> enum ImGuiDir_ // Enum: A cardinal direction typedef int ImGuiMouseButton; // -> enum ImGuiMouseButton_ // Enum: A mouse button identifier (0=left, 1=right, 2=middle) typedef int ImGuiMouseCursor; // -> enum ImGuiMouseCursor_ // Enum: A mouse cursor shape -typedef int ImGuiSortDirection; // -> enum ImGuiSortDirection_ // Enum: A sorting direction (ascending or descending) typedef int ImGuiStyleVar; // -> enum ImGuiStyleVar_ // Enum: A variable identifier for styling typedef int ImGuiTableBgTarget; // -> enum ImGuiTableBgTarget_ // Enum: A color target for TableSetBgColor() // Flags (declared as int to allow using as flags without overhead, and to not pollute the top of this file) // - Tip: Use your programming IDE navigation facilities on the names in the _central column_ below to find the actual flags/enum lists! -// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. -// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments. +// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments. typedef int ImDrawFlags; // -> enum ImDrawFlags_ // Flags: for ImDrawList functions typedef int ImDrawListFlags; // -> enum ImDrawListFlags_ // Flags: for ImDrawList instance typedef int ImFontAtlasFlags; // -> enum ImFontAtlasFlags_ // Flags: for ImFontAtlas build @@ -201,9 +227,12 @@ typedef int ImGuiComboFlags; // -> enum ImGuiComboFlags_ // Flags: f typedef int ImGuiDragDropFlags; // -> enum ImGuiDragDropFlags_ // Flags: for BeginDragDropSource(), AcceptDragDropPayload() typedef int ImGuiFocusedFlags; // -> enum ImGuiFocusedFlags_ // Flags: for IsWindowFocused() typedef int ImGuiHoveredFlags; // -> enum ImGuiHoveredFlags_ // Flags: for IsItemHovered(), IsWindowHovered() etc. +typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: for Shortcut(), SetNextItemShortcut() typedef int ImGuiInputTextFlags; // -> enum ImGuiInputTextFlags_ // Flags: for InputText(), InputTextMultiline() +typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag(), shared by all items typedef int ImGuiKeyChord; // -> ImGuiKey | ImGuiMod_XXX // Flags: for IsKeyChordPressed(), Shortcut() etc. an ImGuiKey optionally OR-ed with one or more ImGuiMod_XXX values. typedef int ImGuiPopupFlags; // -> enum ImGuiPopupFlags_ // Flags: for OpenPopup*(), BeginPopupContext*(), IsPopupOpen() +typedef int ImGuiMultiSelectFlags; // -> enum ImGuiMultiSelectFlags_// Flags: for BeginMultiSelect() typedef int ImGuiSelectableFlags; // -> enum ImGuiSelectableFlags_ // Flags: for Selectable() typedef int ImGuiSliderFlags; // -> enum ImGuiSliderFlags_ // Flags: for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. typedef int ImGuiTabBarFlags; // -> enum ImGuiTabBarFlags_ // Flags: for BeginTabBar() @@ -229,17 +258,6 @@ typedef void* ImTextureID; // Default: store a pointer or an integer fi typedef unsigned short ImDrawIdx; // Default: 16-bit (for maximum compatibility with renderer backends) #endif -// Scalar data types -typedef unsigned int ImGuiID;// A unique ID used by widgets (typically the result of hashing a stack of string) -typedef signed char ImS8; // 8-bit signed integer -typedef unsigned char ImU8; // 8-bit unsigned integer -typedef signed short ImS16; // 16-bit signed integer -typedef unsigned short ImU16; // 16-bit unsigned integer -typedef signed int ImS32; // 32-bit signed integer == int -typedef unsigned int ImU32; // 32-bit unsigned integer (often used to store packed colors) -typedef signed long long ImS64; // 64-bit signed integer -typedef unsigned long long ImU64; // 64-bit unsigned integer - // Character types // (we generally use UTF-8 encoded string in the API. This is storage specifically for a decoded character used for keyboard input and display) typedef unsigned int ImWchar32; // A single decoded U32 character/code point. We encode them as multi bytes UTF-8 when used in strings. @@ -250,6 +268,11 @@ typedef ImWchar32 ImWchar; typedef ImWchar16 ImWchar; #endif +// Multi-Selection item index or identifier when using BeginMultiSelect() +// - Used by SetNextItemSelectionUserData() + and inside ImGuiMultiSelectIO structure. +// - Most users are likely to use this store an item INDEX but this may be used to store a POINTER/ID as well. Read comments near ImGuiMultiSelectIO for details. +typedef ImS64 ImGuiSelectionUserData; + // Callback and functions types typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data); // Callback function for ImGui::InputText() typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints() @@ -369,10 +392,10 @@ namespace ImGui IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags=0); // is current window focused? or its root/child, depending on flags. see flags for options. IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags=0); // is current window hovered and hoverable (e.g. not blocked by a popup/modal)? See ImGuiHoveredFlags_ for options. IMPORTANT: If you are trying to check whether your mouse should be dispatched to Dear ImGui or to your underlying app, you should not use this function! Use the 'io.WantCaptureMouse' boolean for that! Refer to FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" for details. IMGUI_API ImDrawList* GetWindowDrawList(); // get draw list associated to the current window, to append your own drawing primitives - IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (note: it is unlikely you need to use this. Consider using current layout pos instead, GetCursorScreenPos()) - IMGUI_API ImVec2 GetWindowSize(); // get current window size (note: it is unlikely you need to use this. Consider using GetCursorScreenPos() and e.g. GetContentRegionAvail() instead) - IMGUI_API float GetWindowWidth(); // get current window width (shortcut for GetWindowSize().x) - IMGUI_API float GetWindowHeight(); // get current window height (shortcut for GetWindowSize().y) + IMGUI_API ImVec2 GetWindowPos(); // get current window position in screen space (IT IS UNLIKELY YOU EVER NEED TO USE THIS. Consider always using GetCursorScreenPos() and GetContentRegionAvail() instead) + IMGUI_API ImVec2 GetWindowSize(); // get current window size (IT IS UNLIKELY YOU EVER NEED TO USE THIS. Consider always using GetCursorScreenPos() and GetContentRegionAvail() instead) + IMGUI_API float GetWindowWidth(); // get current window width (IT IS UNLIKELY YOU EVER NEED TO USE THIS). Shortcut for GetWindowSize().x. + IMGUI_API float GetWindowHeight(); // get current window height (IT IS UNLIKELY YOU EVER NEED TO USE THIS). Shortcut for GetWindowSize().y. // Window manipulation // - Prefer using SetNextXXX functions (before Begin) rather that SetXXX functions (after Begin). @@ -394,14 +417,6 @@ namespace ImGui IMGUI_API void SetWindowCollapsed(const char* name, bool collapsed, ImGuiCond cond = 0); // set named window collapsed state IMGUI_API void SetWindowFocus(const char* name); // set named window to be focused / top-most. use NULL to remove focus. - // Content region - // - Retrieve available space from a given point. GetContentRegionAvail() is frequently useful. - // - Those functions are bound to be redesigned (they are confusing, incomplete and the Min/Max return values are in local window coordinates which increases confusion) - IMGUI_API ImVec2 GetContentRegionAvail(); // == GetContentRegionMax() - GetCursorPos() - IMGUI_API ImVec2 GetContentRegionMax(); // current content boundaries (typically window boundaries including scrolling, or current column boundaries), in windows coordinates - IMGUI_API ImVec2 GetWindowContentRegionMin(); // content boundaries min for the full window (roughly (0,0)-Scroll), in window coordinates - IMGUI_API ImVec2 GetWindowContentRegionMax(); // content boundaries max for the full window (roughly (0,0)+Size-Scroll) where Size can be overridden with SetNextWindowContentSize(), in window coordinates - // Windows Scrolling // - Any change of Scroll will be applied at the beginning of next frame in the first call to Begin(). // - You may instead use SetNextWindowScroll() prior to calling Begin() to avoid this delay, as an alternative to using SetScrollX()/SetScrollY(). @@ -425,10 +440,8 @@ namespace ImGui IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame(). IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. always use this if you modify the style after NewFrame(). IMGUI_API void PopStyleVar(int count = 1); - IMGUI_API void PushTabStop(bool tab_stop); // == tab stop enable. Allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets - IMGUI_API void PopTabStop(); - IMGUI_API void PushButtonRepeat(bool repeat); // in 'repeat' mode, Button*() functions return repeated true in a typematic manner (using io.KeyRepeatDelay/io.KeyRepeatRate setting). Note that you can call IsItemActive() after any Button() to tell if the button is held in the current frame. - IMGUI_API void PopButtonRepeat(); + IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); // modify specified shared item flag, e.g. PushItemFlag(ImGuiItemFlags_NoTabStop, true) + IMGUI_API void PopItemFlag(); // Parameters stacks (current window) IMGUI_API void PushItemWidth(float item_width); // push width of items for common large "item+label" widgets. >0.0f: width in pixels, <0.0f align xx pixels to the right of window (so -FLT_MIN always align width to the right side). @@ -452,19 +465,22 @@ namespace ImGui // - By "cursor" we mean the current output position. // - The typical widget behavior is to output themselves at the current cursor position, then move the cursor one line down. // - You can call SameLine() between widgets to undo the last carriage return and output at the right of the preceding widget. + // - YOU CAN DO 99% OF WHAT YOU NEED WITH ONLY GetCursorScreenPos() and GetContentRegionAvail(). // - Attention! We currently have inconsistencies between window-local and absolute positions we will aim to fix with future API: // - Absolute coordinate: GetCursorScreenPos(), SetCursorScreenPos(), all ImDrawList:: functions. -> this is the preferred way forward. - // - Window-local coordinates: SameLine(), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), GetContentRegionMax(), GetWindowContentRegion*(), PushTextWrapPos() - // - GetCursorScreenPos() = GetCursorPos() + GetWindowPos(). GetWindowPos() is almost only ever useful to convert from window-local to absolute coordinates. - IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position in absolute coordinates (prefer using this, also more useful to work with ImDrawList API). - IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position in absolute coordinates - IMGUI_API ImVec2 GetCursorPos(); // [window-local] cursor position in window coordinates (relative to window position) + // - Window-local coordinates: SameLine(offset), GetCursorPos(), SetCursorPos(), GetCursorStartPos(), PushTextWrapPos() + // - Window-local coordinates: GetContentRegionMax(), GetWindowContentRegionMin(), GetWindowContentRegionMax() --> all obsoleted. YOU DON'T NEED THEM. + // - GetCursorScreenPos() = GetCursorPos() + GetWindowPos(). GetWindowPos() is almost only ever useful to convert from window-local to absolute coordinates. Try not to use it. + IMGUI_API ImVec2 GetCursorScreenPos(); // cursor position, absolute coordinates. THIS IS YOUR BEST FRIEND (prefer using this rather than GetCursorPos(), also more useful to work with ImDrawList API). + IMGUI_API void SetCursorScreenPos(const ImVec2& pos); // cursor position, absolute coordinates. THIS IS YOUR BEST FRIEND. + IMGUI_API ImVec2 GetContentRegionAvail(); // available space from current position. THIS IS YOUR BEST FRIEND. + IMGUI_API ImVec2 GetCursorPos(); // [window-local] cursor position in window-local coordinates. This is not your best friend. IMGUI_API float GetCursorPosX(); // [window-local] " IMGUI_API float GetCursorPosY(); // [window-local] " IMGUI_API void SetCursorPos(const ImVec2& local_pos); // [window-local] " IMGUI_API void SetCursorPosX(float local_x); // [window-local] " IMGUI_API void SetCursorPosY(float local_y); // [window-local] " - IMGUI_API ImVec2 GetCursorStartPos(); // [window-local] initial cursor position, in window coordinates + IMGUI_API ImVec2 GetCursorStartPos(); // [window-local] initial cursor position, in window-local coordinates. Call GetCursorScreenPos() after Begin() to get the absolute coordinates version. // Other layout functions IMGUI_API void Separator(); // separator, generally horizontal. inside a menu bar or in horizontal layout mode, this becomes a vertical separator. @@ -501,6 +517,7 @@ namespace ImGui IMGUI_API ImGuiID GetID(const char* str_id); // calculate unique ID (hash of whole ID stack + given parameter). e.g. if you want to query into ImGuiStorage yourself IMGUI_API ImGuiID GetID(const char* str_id_begin, const char* str_id_end); IMGUI_API ImGuiID GetID(const void* ptr_id); + IMGUI_API ImGuiID GetID(int int_id); // Widgets: Text IMGUI_API void TextUnformatted(const char* text, const char* text_end = NULL); // raw text without formatting. Roughly equivalent to Text("%s", text) but: A) doesn't require null terminated string if 'text_end' is specified, B) it's faster, no memory copy is done, no buffer size limits, recommended for long chunks of text. @@ -532,6 +549,8 @@ namespace ImGui IMGUI_API bool RadioButton(const char* label, int* v, int v_button); // shortcut to handle the above pattern when value is an integer IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-FLT_MIN, 0), const char* overlay = NULL); IMGUI_API void Bullet(); // draw a small circle + keep the cursor on the same line. advance cursor x position by GetTreeNodeToLabelSpacing(), same distance that TreeNode() uses + IMGUI_API bool TextLink(const char* label); // hyperlink text button, return true when clicked + IMGUI_API void TextLinkOpenURL(const char* label, const char* url = NULL); // hyperlink text button, automatically open file/url when clicked // Widgets: Images // - Read about ImTextureID here: https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples @@ -642,6 +661,7 @@ namespace ImGui IMGUI_API bool CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0); // if returning 'true' the header is open. doesn't indent nor push on ID stack. user doesn't have to call TreePop(). IMGUI_API bool CollapsingHeader(const char* label, bool* p_visible, ImGuiTreeNodeFlags flags = 0); // when 'p_visible != NULL': if '*p_visible==true' display an additional small close button on upper right of the header which will set the bool to false when clicked, if '*p_visible==false' don't display the header. IMGUI_API void SetNextItemOpen(bool is_open, ImGuiCond cond = 0); // set next TreeNode/CollapsingHeader open state. + IMGUI_API void SetNextItemStorageID(ImGuiID storage_id); // set id to use for open/close storage (default to same as item id). // Widgets: Selectables // - A selectable highlights when hovered, and can display another color when selected. @@ -649,6 +669,18 @@ namespace ImGui IMGUI_API bool Selectable(const char* label, bool selected = false, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool selected" carry the selection state (read-only). Selectable() is clicked is returns true so you can modify your selection state. size.x==0.0: use remaining width, size.x>0.0: specify width. size.y==0.0: use label height, size.y>0.0: specify height IMGUI_API bool Selectable(const char* label, bool* p_selected, ImGuiSelectableFlags flags = 0, const ImVec2& size = ImVec2(0, 0)); // "bool* p_selected" point to the selection state (read-write), as a convenient helper. + // Multi-selection system for Selectable(), Checkbox(), TreeNode() functions [BETA] + // - This enables standard multi-selection/range-selection idioms (CTRL+Mouse/Keyboard, SHIFT+Mouse/Keyboard, etc.) in a way that also allow a clipper to be used. + // - ImGuiSelectionUserData is often used to store your item index within the current view (but may store something else). + // - Read comments near ImGuiMultiSelectIO for instructions/details and see 'Demo->Widgets->Selection State & Multi-Select' for demo. + // - TreeNode() is technically supported but... using this correctly is more complicated. You need some sort of linear/random access to your tree, + // which is suited to advanced trees setups already implementing filters and clipper. We will work simplifying the current demo. + // - 'selection_size' and 'items_count' parameters are optional and used by a few features. If they are costly for you to compute, you may avoid them. + IMGUI_API ImGuiMultiSelectIO* BeginMultiSelect(ImGuiMultiSelectFlags flags, int selection_size = -1, int items_count = -1); + IMGUI_API ImGuiMultiSelectIO* EndMultiSelect(); + IMGUI_API void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data); + IMGUI_API bool IsItemToggledSelection(); // Was the last item selection state toggled? Useful if you need the per-item information _before_ reaching EndMultiSelect(). We only returns toggle _event_ in order to handle clipping correctly. + // Widgets: List Boxes // - This is essentially a thin wrapper to using BeginChild/EndChild with the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. // - You can submit contents and manage your selection state however you want it, by creating e.g. Selectable() or any other items. @@ -690,7 +722,8 @@ namespace ImGui // Tooltips // - Tooltips are windows following the mouse. They do not take focus away. - // - A tooltip window can contain items of any types. SetTooltip() is a shortcut for the 'if (BeginTooltip()) { Text(...); EndTooltip(); }' idiom. + // - A tooltip window can contain items of any types. + // - SetTooltip() is more or less a shortcut for the 'if (BeginTooltip()) { Text(...); EndTooltip(); }' idiom (with a subtlety that it discard any previously submitted tooltip) IMGUI_API bool BeginTooltip(); // begin/append a tooltip window. IMGUI_API void EndTooltip(); // only call EndTooltip() if BeginTooltip()/BeginItemTooltip() returns true! IMGUI_API void SetTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip. Often used after a ImGui::IsItemHovered() check. Override any previous call to SetTooltip(). @@ -701,7 +734,7 @@ namespace ImGui // - SetItemTooltip() is a shortcut for the 'if (IsItemHovered(ImGuiHoveredFlags_ForTooltip)) { SetTooltip(...); }' idiom. // - Where 'ImGuiHoveredFlags_ForTooltip' itself is a shortcut to use 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on active input type. For mouse it defaults to 'ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort'. IMGUI_API bool BeginItemTooltip(); // begin/append a tooltip window if preceding item was hovered. - IMGUI_API void SetItemTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip if preceeding item was hovered. override any previous call to SetTooltip(). + IMGUI_API void SetItemTooltip(const char* fmt, ...) IM_FMTARGS(1); // set a text-only tooltip if preceding item was hovered. override any previous call to SetTooltip(). IMGUI_API void SetItemTooltipV(const char* fmt, va_list args) IM_FMTLIST(1); // Popups, Modals @@ -767,7 +800,7 @@ namespace ImGui // - TableNextColumn() -> Text("Hello 0") -> TableNextColumn() -> Text("Hello 1") // OK: TableNextColumn() automatically gets to next row! // - TableNextRow() -> Text("Hello 0") // Not OK! Missing TableSetColumnIndex() or TableNextColumn()! Text will not appear! // - 5. Call EndTable() - IMGUI_API bool BeginTable(const char* str_id, int column, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0.0f, 0.0f), float inner_width = 0.0f); + IMGUI_API bool BeginTable(const char* str_id, int columns, ImGuiTableFlags flags = 0, const ImVec2& outer_size = ImVec2(0.0f, 0.0f), float inner_width = 0.0f); IMGUI_API void EndTable(); // only call EndTable() if BeginTable() returns true! IMGUI_API void TableNextRow(ImGuiTableRowFlags row_flags = 0, float min_row_height = 0.0f); // append into the first cell of a new row. IMGUI_API bool TableNextColumn(); // append into the next column (or first column of next row if currently in last column). Return true when column is visible. @@ -800,6 +833,7 @@ namespace ImGui IMGUI_API const char* TableGetColumnName(int column_n = -1); // return "" if column didn't have a name declared by TableSetupColumn(). Pass -1 to use current column. IMGUI_API ImGuiTableColumnFlags TableGetColumnFlags(int column_n = -1); // return column flags so you can query their Enabled/Visible/Sorted/Hovered status flags. Pass -1 to use current column. IMGUI_API void TableSetColumnEnabled(int column_n, bool v);// change user accessible enabled/disabled state of a column. Set to false to hide the column. User can use the context menu to change this themselves (right-click in headers, or right-click in columns body with ImGuiTableFlags_ContextMenuInBody) + IMGUI_API int TableGetHoveredColumn(); // return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. Can also use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. IMGUI_API void TableSetBgColor(ImGuiTableBgTarget target, ImU32 color, int column_n = -1); // change the color of a cell, row, or column. See ImGuiTableBgTarget_ flags for details. // Legacy Columns API (prefer using Tables!) @@ -848,6 +882,7 @@ namespace ImGui // Disabling [BETA API] // - Disable all user interactions and dim items visuals (applying style.DisabledAlpha over current colors) // - Those can be nested but it cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep everything disabled) + // - Tooltips windows by exception are opted out of disabling. // - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. IMGUI_API void BeginDisabled(bool disabled = true); IMGUI_API void EndDisabled(); @@ -928,6 +963,32 @@ namespace ImGui IMGUI_API const char* GetKeyName(ImGuiKey key); // [DEBUG] returns English name of the key. Those names a provided for debugging purpose and are not meant to be saved persistently not compared. IMGUI_API void SetNextFrameWantCaptureKeyboard(bool want_capture_keyboard); // Override io.WantCaptureKeyboard flag next frame (said flag is left for your application to handle, typically when true it instructs your app to ignore inputs). e.g. force capture keyboard when your widget is being hovered. This is equivalent to setting "io.WantCaptureKeyboard = want_capture_keyboard"; after the next NewFrame() call. + // Inputs Utilities: Shortcut Testing & Routing [BETA] + // - ImGuiKeyChord = a ImGuiKey + optional ImGuiMod_Alt/ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Super. + // ImGuiKey_C // Accepted by functions taking ImGuiKey or ImGuiKeyChord arguments) + // ImGuiMod_Ctrl | ImGuiKey_C // Accepted by functions taking ImGuiKeyChord arguments) + // only ImGuiMod_XXX values are legal to combine with an ImGuiKey. You CANNOT combine two ImGuiKey values. + // - The general idea is that several callers may register interest in a shortcut, and only one owner gets it. + // Parent -> call Shortcut(Ctrl+S) // When Parent is focused, Parent gets the shortcut. + // Child1 -> call Shortcut(Ctrl+S) // When Child1 is focused, Child1 gets the shortcut (Child1 overrides Parent shortcuts) + // Child2 -> no call // When Child2 is focused, Parent gets the shortcut. + // The whole system is order independent, so if Child1 makes its calls before Parent, results will be identical. + // This is an important property as it facilitate working with foreign code or larger codebase. + // - To understand the difference: + // - IsKeyChordPressed() compares mods and call IsKeyPressed() -> function has no side-effect. + // - Shortcut() submits a route, routes are resolved, if it currently can be routed it calls IsKeyChordPressed() -> function has (desirable) side-effects as it can prevents another call from getting the route. + // - Visualize registered routes in 'Metrics/Debugger->Inputs'. + IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags = 0); + IMGUI_API void SetNextItemShortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags = 0); + + // Inputs Utilities: Key/Input Ownership [BETA] + // - One common use case would be to allow your items to disable standard inputs behaviors such + // as Tab or Alt key handling, Mouse Wheel scrolling, etc. + // e.g. Button(...); SetItemKeyOwner(ImGuiKey_MouseWheelY); to make hovering/activating a button disable wheel for scrolling. + // - Reminder ImGuiKey enum include access to mouse buttons and gamepad, so key ownership can apply to them. + // - Many related features are still in imgui_internal.h. For instance, most IsKeyXXX()/IsMouseXXX() functions have an owner-id-aware version. + IMGUI_API void SetItemKeyOwner(ImGuiKey key); // Set key owner to last item ID if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. + // Inputs Utilities: Mouse specific // - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right. // - You can also use regular integer: it is forever guaranteed that 0=Left, 1=Right, 2=Middle. @@ -942,8 +1003,8 @@ namespace ImGui IMGUI_API bool IsAnyMouseDown(); // [WILL OBSOLETE] is any mouse button held? This was designed for backends, but prefer having backend maintain a mask of held mouse buttons, because upcoming input queue system will make this invalid. IMGUI_API ImVec2 GetMousePos(); // shortcut to ImGui::GetIO().MousePos provided by user, to be consistent with other calls IMGUI_API ImVec2 GetMousePosOnOpeningCurrentPopup(); // retrieve mouse position at the time of opening popup we have BeginPopup() into (helper to avoid user backing that value themselves) - IMGUI_API bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f); // is mouse dragging? (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) - IMGUI_API ImVec2 GetMouseDragDelta(ImGuiMouseButton button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once (if lock_threshold < -1.0f, uses io.MouseDraggingThreshold) + IMGUI_API bool IsMouseDragging(ImGuiMouseButton button, float lock_threshold = -1.0f); // is mouse dragging? (uses io.MouseDraggingThreshold if lock_threshold < 0.0f) + IMGUI_API ImVec2 GetMouseDragDelta(ImGuiMouseButton button = 0, float lock_threshold = -1.0f); // return the delta from the initial clicking position while the mouse button is pressed or was just released. This is locked and return 0.0f until the mouse moves past a distance threshold at least once (uses io.MouseDraggingThreshold if lock_threshold < 0.0f) IMGUI_API void ResetMouseDragDelta(ImGuiMouseButton button = 0); // IMGUI_API ImGuiMouseCursor GetMouseCursor(); // get desired mouse cursor shape. Important: reset in ImGui::NewFrame(), this is updated during the frame. valid before Render(). If you use software rendering by setting io.MouseDrawCursor ImGui will render those for you IMGUI_API void SetMouseCursor(ImGuiMouseCursor cursor_type); // set desired mouse cursor shape @@ -969,6 +1030,10 @@ namespace ImGui IMGUI_API void DebugFlashStyleColor(ImGuiCol idx); IMGUI_API void DebugStartItemPicker(); IMGUI_API bool DebugCheckVersionAndDataLayout(const char* version_str, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_drawvert, size_t sz_drawidx); // This is called by IMGUI_CHECKVERSION() macro. +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + IMGUI_API void DebugLog(const char* fmt, ...) IM_FMTARGS(1); // Call via IMGUI_DEBUG_LOG() for maximum stripping in caller code! + IMGUI_API void DebugLogV(const char* fmt, va_list args) IM_FMTLIST(1); +#endif // Memory Allocators // - Those functions are not reliant on the current context. @@ -1014,7 +1079,6 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_NoInputs = ImGuiWindowFlags_NoMouseInputs | ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, // [Internal] - ImGuiWindowFlags_NavFlattened = 1 << 23, // [BETA] On child window: share focus scope, allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. ImGuiWindowFlags_ChildWindow = 1 << 24, // Don't use! For internal use by BeginChild() ImGuiWindowFlags_Tooltip = 1 << 25, // Don't use! For internal use by BeginTooltip() ImGuiWindowFlags_Popup = 1 << 26, // Don't use! For internal use by BeginPopup() @@ -1023,7 +1087,8 @@ enum ImGuiWindowFlags_ // Obsolete names #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 30, // Obsoleted in 1.90: Use ImGuiChildFlags_AlwaysUseWindowPadding in BeginChild() call. + ImGuiWindowFlags_AlwaysUseWindowPadding = 1 << 30, // Obsoleted in 1.90.0: Use ImGuiChildFlags_AlwaysUseWindowPadding in BeginChild() call. + ImGuiWindowFlags_NavFlattened = 1 << 31, // Obsoleted in 1.90.9: Use ImGuiChildFlags_NavFlattened in BeginChild() call. #endif }; @@ -1047,34 +1112,56 @@ enum ImGuiChildFlags_ ImGuiChildFlags_AutoResizeY = 1 << 5, // Enable auto-resizing height. Read "IMPORTANT: Size measurement" details above. ImGuiChildFlags_AlwaysAutoResize = 1 << 6, // Combined with AutoResizeX/AutoResizeY. Always measure size even when child is hidden, always return true, always disable clipping optimization! NOT RECOMMENDED. ImGuiChildFlags_FrameStyle = 1 << 7, // Style the child window like a framed item: use FrameBg, FrameRounding, FrameBorderSize, FramePadding instead of ChildBg, ChildRounding, ChildBorderSize, WindowPadding. + ImGuiChildFlags_NavFlattened = 1 << 8, // [BETA] Share focus scope, allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. +}; + +// Flags for ImGui::PushItemFlag() +// (Those are shared by all items) +enum ImGuiItemFlags_ +{ + ImGuiItemFlags_None = 0, // (Default) + ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing. This is a "lighter" version of ImGuiItemFlags_NoNav. + ImGuiItemFlags_NoNav = 1 << 1, // false // Disable any form of focusing (keyboard/gamepad directional navigation and SetKeyboardFocusHere() calls). + ImGuiItemFlags_NoNavDefaultFocus = 1 << 2, // false // Disable item being a candidate for default focus (e.g. used by title bar items). + ImGuiItemFlags_ButtonRepeat = 1 << 3, // false // Any button-like behavior will have repeat mode enabled (based on io.KeyRepeatDelay and io.KeyRepeatRate values). Note that you can also call IsItemActive() after any button to tell if it is being held. + ImGuiItemFlags_AutoClosePopups = 1 << 4, // true // MenuItem()/Selectable() automatically close their parent popup window. }; // Flags for ImGui::InputText() // (Those are per-item flags. There are shared flags in ImGuiIO: io.ConfigInputTextCursorBlink and io.ConfigInputTextEnterKeepActive) enum ImGuiInputTextFlags_ { + // Basic filters (also see ImGuiInputTextFlags_CallbackCharFilter) ImGuiInputTextFlags_None = 0, ImGuiInputTextFlags_CharsDecimal = 1 << 0, // Allow 0123456789.+-*/ ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, // Allow 0123456789ABCDEFabcdef - ImGuiInputTextFlags_CharsUppercase = 1 << 2, // Turn a..z into A..Z - ImGuiInputTextFlags_CharsNoBlank = 1 << 3, // Filter out spaces, tabs - ImGuiInputTextFlags_AutoSelectAll = 1 << 4, // Select entire text when first taking mouse focus - ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider looking at the IsItemDeactivatedAfterEdit() function. - ImGuiInputTextFlags_CallbackCompletion = 1 << 6, // Callback on pressing TAB (for completion handling) - ImGuiInputTextFlags_CallbackHistory = 1 << 7, // Callback on pressing Up/Down arrows (for history handling) - ImGuiInputTextFlags_CallbackAlways = 1 << 8, // Callback on each iteration. User code may query cursor position, modify text buffer. - ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. - ImGuiInputTextFlags_AllowTabInput = 1 << 10, // Pressing TAB input a '\t' character into the text field - ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, // In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter (default is opposite: unfocus with Ctrl+Enter, add line with Enter). - ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, // Disable following the cursor horizontally - ImGuiInputTextFlags_AlwaysOverwrite = 1 << 13, // Overwrite mode - ImGuiInputTextFlags_ReadOnly = 1 << 14, // Read-only mode - ImGuiInputTextFlags_Password = 1 << 15, // Password mode, display all characters as '*' + ImGuiInputTextFlags_CharsScientific = 1 << 2, // Allow 0123456789.+-*/eE (Scientific notation input) + ImGuiInputTextFlags_CharsUppercase = 1 << 3, // Turn a..z into A..Z + ImGuiInputTextFlags_CharsNoBlank = 1 << 4, // Filter out spaces, tabs + + // Inputs + ImGuiInputTextFlags_AllowTabInput = 1 << 5, // Pressing TAB input a '\t' character into the text field + ImGuiInputTextFlags_EnterReturnsTrue = 1 << 6, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider looking at the IsItemDeactivatedAfterEdit() function. + ImGuiInputTextFlags_EscapeClearsAll = 1 << 7, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) + ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 8, // In multi-line mode, validate with Enter, add new line with Ctrl+Enter (default is opposite: validate with Ctrl+Enter, add line with Enter). + + // Other options + ImGuiInputTextFlags_ReadOnly = 1 << 9, // Read-only mode + ImGuiInputTextFlags_Password = 1 << 10, // Password mode, display all characters as '*', disable copy + ImGuiInputTextFlags_AlwaysOverwrite = 1 << 11, // Overwrite mode + ImGuiInputTextFlags_AutoSelectAll = 1 << 12, // Select entire text when first taking mouse focus + ImGuiInputTextFlags_ParseEmptyRefVal = 1 << 13, // InputFloat(), InputInt(), InputScalar() etc. only: parse empty string as zero value. + ImGuiInputTextFlags_DisplayEmptyRefVal = 1 << 14, // InputFloat(), InputInt(), InputScalar() etc. only: when value is zero, do not display it. Generally used with ImGuiInputTextFlags_ParseEmptyRefVal. + ImGuiInputTextFlags_NoHorizontalScroll = 1 << 15, // Disable following the cursor horizontally ImGuiInputTextFlags_NoUndoRedo = 1 << 16, // Disable undo/redo. Note that input text owns the text data while active, if you want to provide your own undo/redo stack you need e.g. to call ClearActiveID(). - ImGuiInputTextFlags_CharsScientific = 1 << 17, // Allow 0123456789.+-*/eE (Scientific notation input) - ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) - ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) - ImGuiInputTextFlags_EscapeClearsAll = 1 << 20, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) + + // Callback features + ImGuiInputTextFlags_CallbackCompletion = 1 << 17, // Callback on pressing TAB (for completion handling) + ImGuiInputTextFlags_CallbackHistory = 1 << 18, // Callback on pressing Up/Down arrows (for history handling) + ImGuiInputTextFlags_CallbackAlways = 1 << 19, // Callback on each iteration. User code may query cursor position, modify text buffer. + ImGuiInputTextFlags_CallbackCharFilter = 1 << 20, // Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard. + ImGuiInputTextFlags_CallbackResize = 1 << 21, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) + ImGuiInputTextFlags_CallbackEdit = 1 << 22, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) // Obsolete names //ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior @@ -1094,12 +1181,13 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_OpenOnArrow = 1 << 7, // Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open. ImGuiTreeNodeFlags_Leaf = 1 << 8, // No collapsing, no arrow (use as a convenience for leaf nodes). ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow. IMPORTANT: node can still be marked open/close if you don't set the _Leaf flag! - ImGuiTreeNodeFlags_FramePadding = 1 << 10, // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding(). - ImGuiTreeNodeFlags_SpanAvailWidth = 1 << 11, // Extend hit box to the right-most edge, even if not framed. This is not the default in order to allow adding other items on the same line. In the future we may refactor the hit system to be front-to-back, allowing natural overlaps and then this can become the default. - ImGuiTreeNodeFlags_SpanFullWidth = 1 << 12, // Extend hit box to the left-most and right-most edges (bypass the indented area). - ImGuiTreeNodeFlags_SpanAllColumns = 1 << 13, // Frame will span all columns of its container table (text will still fit in current column) - ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 14, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) - //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 15, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible + ImGuiTreeNodeFlags_FramePadding = 1 << 10, // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding() before the node. + ImGuiTreeNodeFlags_SpanAvailWidth = 1 << 11, // Extend hit box to the right-most edge, even if not framed. This is not the default in order to allow adding other items on the same line without using AllowOverlap mode. + ImGuiTreeNodeFlags_SpanFullWidth = 1 << 12, // Extend hit box to the left-most and right-most edges (cover the indent area). + ImGuiTreeNodeFlags_SpanTextWidth = 1 << 13, // Narrow hit box + narrow hovering highlight, will only cover the label text. + ImGuiTreeNodeFlags_SpanAllColumns = 1 << 14, // Frame will span all columns of its container table (text will still fit in current column) + ImGuiTreeNodeFlags_NavLeftJumpsBackHere = 1 << 15, // (WIP) Nav: left direction may move to this TreeNode() from any of its child (items submitted between TreeNode and TreePop) + //ImGuiTreeNodeFlags_NoScrollOnOpen = 1 << 16, // FIXME: TODO: Disable automatic scroll on TreePop() if node got just open and contents is not visible ImGuiTreeNodeFlags_CollapsingHeader = ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_NoAutoOpenOnLog, #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS @@ -1136,14 +1224,16 @@ enum ImGuiPopupFlags_ enum ImGuiSelectableFlags_ { ImGuiSelectableFlags_None = 0, - ImGuiSelectableFlags_DontClosePopups = 1 << 0, // Clicking this doesn't close parent popup window + ImGuiSelectableFlags_NoAutoClosePopups = 1 << 0, // Clicking this doesn't close parent popup window (overrides ImGuiItemFlags_AutoClosePopups) ImGuiSelectableFlags_SpanAllColumns = 1 << 1, // Frame will span all columns of its container table (text will still fit in current column) ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text ImGuiSelectableFlags_AllowOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one + ImGuiSelectableFlags_Highlight = 1 << 5, // Make the item be displayed as if it is hovered #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - ImGuiSelectableFlags_AllowItemOverlap = ImGuiSelectableFlags_AllowOverlap, // Renamed in 1.89.7 + ImGuiSelectableFlags_DontClosePopups = ImGuiSelectableFlags_NoAutoClosePopups, // Renamed in 1.91.0 + ImGuiSelectableFlags_AllowItemOverlap = ImGuiSelectableFlags_AllowOverlap, // Renamed in 1.89.7 #endif }; @@ -1172,8 +1262,9 @@ enum ImGuiTabBarFlags_ ImGuiTabBarFlags_NoCloseWithMiddleMouseButton = 1 << 3, // Disable behavior of closing tabs (that are submitted with p_open != NULL) with middle mouse button. You may handle this behavior manually on user's side with if (IsItemHovered() && IsMouseClicked(2)) *p_open = false. ImGuiTabBarFlags_NoTabListScrollingButtons = 1 << 4, // Disable scrolling buttons (apply when fitting policy is ImGuiTabBarFlags_FittingPolicyScroll) ImGuiTabBarFlags_NoTooltip = 1 << 5, // Disable tooltips when hovering a tab - ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 6, // Resize tabs when they don't fit - ImGuiTabBarFlags_FittingPolicyScroll = 1 << 7, // Add scroll buttons when tabs don't fit + ImGuiTabBarFlags_DrawSelectedOverline = 1 << 6, // Draw selected overline markers over selected tab + ImGuiTabBarFlags_FittingPolicyResizeDown = 1 << 7, // Resize tabs when they don't fit + ImGuiTabBarFlags_FittingPolicyScroll = 1 << 8, // Add scroll buttons when tabs don't fit ImGuiTabBarFlags_FittingPolicyMask_ = ImGuiTabBarFlags_FittingPolicyResizeDown | ImGuiTabBarFlags_FittingPolicyScroll, ImGuiTabBarFlags_FittingPolicyDefault_ = ImGuiTabBarFlags_FittingPolicyResizeDown, }; @@ -1255,12 +1346,18 @@ enum ImGuiDragDropFlags_ ImGuiDragDropFlags_SourceNoHoldToOpenOthers = 1 << 2, // Disable the behavior that allows to open tree nodes and collapsing header by holding over them while dragging a source item. ImGuiDragDropFlags_SourceAllowNullID = 1 << 3, // Allow items such as Text(), Image() that have no unique identifier to be used as drag source, by manufacturing a temporary identifier based on their window-relative position. This is extremely unusual within the dear imgui ecosystem and so we made it explicit. ImGuiDragDropFlags_SourceExtern = 1 << 4, // External source (from outside of dear imgui), won't attempt to read current item/window info. Will always return true. Only one Extern source can be active simultaneously. - ImGuiDragDropFlags_SourceAutoExpirePayload = 1 << 5, // Automatically expire the payload if the source cease to be submitted (otherwise payloads are persisting while being dragged) + ImGuiDragDropFlags_PayloadAutoExpire = 1 << 5, // Automatically expire the payload if the source cease to be submitted (otherwise payloads are persisting while being dragged) + ImGuiDragDropFlags_PayloadNoCrossContext = 1 << 6, // Hint to specify that the payload may not be copied outside current dear imgui context. + ImGuiDragDropFlags_PayloadNoCrossProcess = 1 << 7, // Hint to specify that the payload may not be copied outside current process. // AcceptDragDropPayload() flags ImGuiDragDropFlags_AcceptBeforeDelivery = 1 << 10, // AcceptDragDropPayload() will returns true even before the mouse button is released. You can then call IsDelivery() to test if the payload needs to be delivered. ImGuiDragDropFlags_AcceptNoDrawDefaultRect = 1 << 11, // Do not draw the default highlight rectangle when hovering over target. ImGuiDragDropFlags_AcceptNoPreviewTooltip = 1 << 12, // Request hiding the BeginDragDropSource tooltip from the BeginDragDropTarget site. ImGuiDragDropFlags_AcceptPeekOnly = ImGuiDragDropFlags_AcceptBeforeDelivery | ImGuiDragDropFlags_AcceptNoDrawDefaultRect, // For peeking ahead and inspecting the payload before delivery. + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiDragDropFlags_SourceAutoExpirePayload = ImGuiDragDropFlags_PayloadAutoExpire, // Renamed in 1.90.9 +#endif }; // Standard Drag and Drop payload types. You can define you own payload types using short strings. Types starting with '_' are defined by Dear ImGui. @@ -1280,11 +1377,12 @@ enum ImGuiDataType_ ImGuiDataType_U64, // unsigned long long / unsigned __int64 ImGuiDataType_Float, // float ImGuiDataType_Double, // double + ImGuiDataType_Bool, // bool (provided for user convenience, not supported by scalar widgets) ImGuiDataType_COUNT }; // A cardinal direction -enum ImGuiDir_ +enum ImGuiDir : int { ImGuiDir_None = -1, ImGuiDir_Left = 0, @@ -1295,7 +1393,7 @@ enum ImGuiDir_ }; // A sorting direction -enum ImGuiSortDirection_ +enum ImGuiSortDirection : ImU8 { ImGuiSortDirection_None = 0, ImGuiSortDirection_Ascending = 1, // Ascending = 0->9, A->Z etc. @@ -1414,13 +1512,13 @@ enum ImGuiKey : int // - In theory the value of keyboard modifiers should be roughly equivalent to a logical or of the equivalent left/right keys. // In practice: it's complicated; mods are often provided from different sources. Keyboard layout, IME, sticky keys and // backends tend to interfere and break that equivalence. The safer decision is to relay that ambiguity down to the end-user... + // - On macOS, we swap Cmd(Super) and Ctrl keys at the time of the io.AddKeyEvent() call. ImGuiMod_None = 0, - ImGuiMod_Ctrl = 1 << 12, // Ctrl + ImGuiMod_Ctrl = 1 << 12, // Ctrl (non-macOS), Cmd (macOS) ImGuiMod_Shift = 1 << 13, // Shift ImGuiMod_Alt = 1 << 14, // Option/Menu - ImGuiMod_Super = 1 << 15, // Cmd/Super/Windows - ImGuiMod_Shortcut = 1 << 11, // Alias for Ctrl (non-macOS) _or_ Super (macOS). - ImGuiMod_Mask_ = 0xF800, // 5-bits + ImGuiMod_Super = 1 << 15, // Windows/Super (non-macOS), Ctrl (macOS) + ImGuiMod_Mask_ = 0xF000, // 4-bits // [Internal] Prior to 1.87 we required user to fill io.KeysDown[512] using their own native index + the io.KeyMap[] array. // We are ditching this method but keeping a legacy path for user code doing e.g. IsKeyPressed(MY_NATIVE_KEY_CODE) @@ -1437,11 +1535,37 @@ enum ImGuiKey : int #endif #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiMod_Shortcut = ImGuiMod_Ctrl, // Removed in 1.90.7, you can now simply use ImGuiMod_Ctrl ImGuiKey_ModCtrl = ImGuiMod_Ctrl, ImGuiKey_ModShift = ImGuiMod_Shift, ImGuiKey_ModAlt = ImGuiMod_Alt, ImGuiKey_ModSuper = ImGuiMod_Super, // Renamed in 1.89 //ImGuiKey_KeyPadEnter = ImGuiKey_KeypadEnter, // Renamed in 1.87 #endif }; +// Flags for Shortcut(), SetNextItemShortcut(), +// (and for upcoming extended versions of IsKeyPressed(), IsMouseClicked(), Shortcut(), SetKeyOwner(), SetItemKeyOwner() that are still in imgui_internal.h) +// Don't mistake with ImGuiInputTextFlags! (which is for ImGui::InputText() function) +enum ImGuiInputFlags_ +{ + ImGuiInputFlags_None = 0, + ImGuiInputFlags_Repeat = 1 << 0, // Enable repeat. Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. + + // Flags for Shortcut(), SetNextItemShortcut() + // - Routing policies: RouteGlobal+OverActive >> RouteActive or RouteFocused (if owner is active item) >> RouteGlobal+OverFocused >> RouteFocused (if in focused window stack) >> RouteGlobal. + // - Default policy is RouteFocused. Can select only 1 policy among all available. + ImGuiInputFlags_RouteActive = 1 << 10, // Route to active item only. + ImGuiInputFlags_RouteFocused = 1 << 11, // Route to windows in the focus stack (DEFAULT). Deep-most focused window takes inputs. Active item takes inputs over deep-most focused window. + ImGuiInputFlags_RouteGlobal = 1 << 12, // Global route (unless a focused window or active item registered the route). + ImGuiInputFlags_RouteAlways = 1 << 13, // Do not register route, poll keys directly. + // - Routing options + ImGuiInputFlags_RouteOverFocused = 1 << 14, // Option: global route: higher priority than focused route (unless active item in focused route). + ImGuiInputFlags_RouteOverActive = 1 << 15, // Option: global route: higher priority than active item. Unlikely you need to use that: will interfere with every active items, e.g. CTRL+A registered by InputText will be overridden by this. May not be fully honored as user/internal code is likely to always assume they can access keys when active. + ImGuiInputFlags_RouteUnlessBgFocused = 1 << 16, // Option: global route: will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. + ImGuiInputFlags_RouteFromRootWindow = 1 << 17, // Option: route evaluated from the point of view of root window rather than current window. + + // Flags for SetNextItemShortcut() + ImGuiInputFlags_Tooltip = 1 << 18, // Automatically display a tooltip when hovering item [BETA] Unsure of right api (opt-in/opt-out) +}; + #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO // OBSOLETED in 1.88 (from July 2022): ImGuiNavInput and io.NavInputs[]. // Official backends between 1.60 and 1.86: will keep working and feed gamepad inputs as long as IMGUI_DISABLE_OBSOLETE_KEYIO is not set. @@ -1462,8 +1586,9 @@ enum ImGuiConfigFlags_ ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. Backend also needs to set ImGuiBackendFlags_HasGamepad. ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your backend, otherwise ImGui will react as if the mouse is jumping around back and forth. ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. - ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct imgui to clear mouse position/buttons in NewFrame(). This allows ignoring the mouse information set by the backend. + ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct dear imgui to disable mouse inputs and interactions. ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct backend to not alter mouse cursor shape and visibility. Use if the backend cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. + ImGuiConfigFlags_NoKeyboard = 1 << 6, // Instruct dear imgui to disable keyboard inputs and interactions. This is done by ignoring keyboard events and clearing existing states. // User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are NOT used by core Dear ImGui) ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. @@ -1516,11 +1641,13 @@ enum ImGuiCol_ ImGuiCol_ResizeGrip, // Resize grip in lower-right and lower-left corners of windows. ImGuiCol_ResizeGripHovered, ImGuiCol_ResizeGripActive, - ImGuiCol_Tab, // TabItem in a TabBar - ImGuiCol_TabHovered, - ImGuiCol_TabActive, - ImGuiCol_TabUnfocused, - ImGuiCol_TabUnfocusedActive, + ImGuiCol_TabHovered, // Tab background, when hovered + ImGuiCol_Tab, // Tab background, when tab-bar is focused & tab is unselected + ImGuiCol_TabSelected, // Tab background, when tab-bar is focused & tab is selected + ImGuiCol_TabSelectedOverline, // Tab horizontal overline, when tab-bar is focused & tab is selected + ImGuiCol_TabDimmed, // Tab background, when tab-bar is unfocused & tab is unselected + ImGuiCol_TabDimmedSelected, // Tab background, when tab-bar is unfocused & tab is selected + ImGuiCol_TabDimmedSelectedOverline,//..horizontal overline, when tab-bar is unfocused & tab is selected ImGuiCol_PlotLines, ImGuiCol_PlotLinesHovered, ImGuiCol_PlotHistogram, @@ -1530,54 +1657,66 @@ enum ImGuiCol_ ImGuiCol_TableBorderLight, // Table inner borders (prefer using Alpha=1.0 here) ImGuiCol_TableRowBg, // Table row background (even rows) ImGuiCol_TableRowBgAlt, // Table row background (odd rows) + ImGuiCol_TextLink, // Hyperlink color ImGuiCol_TextSelectedBg, ImGuiCol_DragDropTarget, // Rectangle highlighting a drop target ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active ImGuiCol_ModalWindowDimBg, // Darken/colorize entire screen behind a modal window, when one is active - ImGuiCol_COUNT + ImGuiCol_COUNT, + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiCol_TabActive = ImGuiCol_TabSelected, // [renamed in 1.90.9] + ImGuiCol_TabUnfocused = ImGuiCol_TabDimmed, // [renamed in 1.90.9] + ImGuiCol_TabUnfocusedActive = ImGuiCol_TabDimmedSelected, // [renamed in 1.90.9] +#endif }; // Enumeration for PushStyleVar() / PopStyleVar() to temporarily modify the ImGuiStyle structure. // - The enum only refers to fields of ImGuiStyle which makes sense to be pushed/popped inside UI code. // During initialization or between frames, feel free to just poke into ImGuiStyle directly. // - Tip: Use your programming IDE navigation facilities on the names in the _second column_ below to find the actual members and their description. -// In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. -// With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments. +// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments. // - When changing this enum, you need to update the associated internal table GStyleVarInfo[] accordingly. This is where we link enum values to members offset/type. enum ImGuiStyleVar_ { - // Enum name --------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) - ImGuiStyleVar_Alpha, // float Alpha - ImGuiStyleVar_DisabledAlpha, // float DisabledAlpha - ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding - ImGuiStyleVar_WindowRounding, // float WindowRounding - ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize - ImGuiStyleVar_WindowMinSize, // ImVec2 WindowMinSize - ImGuiStyleVar_WindowTitleAlign, // ImVec2 WindowTitleAlign - ImGuiStyleVar_ChildRounding, // float ChildRounding - ImGuiStyleVar_ChildBorderSize, // float ChildBorderSize - ImGuiStyleVar_PopupRounding, // float PopupRounding - ImGuiStyleVar_PopupBorderSize, // float PopupBorderSize - ImGuiStyleVar_FramePadding, // ImVec2 FramePadding - ImGuiStyleVar_FrameRounding, // float FrameRounding - ImGuiStyleVar_FrameBorderSize, // float FrameBorderSize - ImGuiStyleVar_ItemSpacing, // ImVec2 ItemSpacing - ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ItemInnerSpacing - ImGuiStyleVar_IndentSpacing, // float IndentSpacing - ImGuiStyleVar_CellPadding, // ImVec2 CellPadding - ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize - ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding - ImGuiStyleVar_GrabMinSize, // float GrabMinSize - ImGuiStyleVar_GrabRounding, // float GrabRounding - ImGuiStyleVar_TabRounding, // float TabRounding - ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize - ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign - ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign - ImGuiStyleVar_SeparatorTextBorderSize,// float SeparatorTextBorderSize - ImGuiStyleVar_SeparatorTextAlign, // ImVec2 SeparatorTextAlign - ImGuiStyleVar_SeparatorTextPadding,// ImVec2 SeparatorTextPadding + // Enum name -------------------------- // Member in ImGuiStyle structure (see ImGuiStyle for descriptions) + ImGuiStyleVar_Alpha, // float Alpha + ImGuiStyleVar_DisabledAlpha, // float DisabledAlpha + ImGuiStyleVar_WindowPadding, // ImVec2 WindowPadding + ImGuiStyleVar_WindowRounding, // float WindowRounding + ImGuiStyleVar_WindowBorderSize, // float WindowBorderSize + ImGuiStyleVar_WindowMinSize, // ImVec2 WindowMinSize + ImGuiStyleVar_WindowTitleAlign, // ImVec2 WindowTitleAlign + ImGuiStyleVar_ChildRounding, // float ChildRounding + ImGuiStyleVar_ChildBorderSize, // float ChildBorderSize + ImGuiStyleVar_PopupRounding, // float PopupRounding + ImGuiStyleVar_PopupBorderSize, // float PopupBorderSize + ImGuiStyleVar_FramePadding, // ImVec2 FramePadding + ImGuiStyleVar_FrameRounding, // float FrameRounding + ImGuiStyleVar_FrameBorderSize, // float FrameBorderSize + ImGuiStyleVar_ItemSpacing, // ImVec2 ItemSpacing + ImGuiStyleVar_ItemInnerSpacing, // ImVec2 ItemInnerSpacing + ImGuiStyleVar_IndentSpacing, // float IndentSpacing + ImGuiStyleVar_CellPadding, // ImVec2 CellPadding + ImGuiStyleVar_ScrollbarSize, // float ScrollbarSize + ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding + ImGuiStyleVar_GrabMinSize, // float GrabMinSize + ImGuiStyleVar_GrabRounding, // float GrabRounding + ImGuiStyleVar_TabRounding, // float TabRounding + ImGuiStyleVar_TabBorderSize, // float TabBorderSize + ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize + ImGuiStyleVar_TabBarOverlineSize, // float TabBarOverlineSize + ImGuiStyleVar_TableAngledHeadersAngle, // float TableAngledHeadersAngle + ImGuiStyleVar_TableAngledHeadersTextAlign,// ImVec2 TableAngledHeadersTextAlign + ImGuiStyleVar_ButtonTextAlign, // ImVec2 ButtonTextAlign + ImGuiStyleVar_SelectableTextAlign, // ImVec2 SelectableTextAlign + ImGuiStyleVar_SeparatorTextBorderSize, // float SeparatorTextBorderSize + ImGuiStyleVar_SeparatorTextAlign, // ImVec2 SeparatorTextAlign + ImGuiStyleVar_SeparatorTextPadding, // ImVec2 SeparatorTextPadding ImGuiStyleVar_COUNT }; @@ -1588,10 +1727,8 @@ enum ImGuiButtonFlags_ ImGuiButtonFlags_MouseButtonLeft = 1 << 0, // React on left mouse button (default) ImGuiButtonFlags_MouseButtonRight = 1 << 1, // React on right mouse button ImGuiButtonFlags_MouseButtonMiddle = 1 << 2, // React on center mouse button - - // [Internal] - ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, - ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, + ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, // [Internal] + //ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, }; // Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() @@ -1646,8 +1783,9 @@ enum ImGuiSliderFlags_ ImGuiSliderFlags_None = 0, ImGuiSliderFlags_AlwaysClamp = 1 << 4, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. - ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits) - ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget + ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits). + ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget. + ImGuiSliderFlags_WrapAround = 1 << 8, // Enable wrapping around from max to min and from min to max (only supported by DragXXX() functions for now. ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. // Obsolete names @@ -1864,15 +2002,25 @@ struct ImGuiTableColumnSortSpecs ImGuiID ColumnUserID; // User id of the column (if specified by a TableSetupColumn() call) ImS16 ColumnIndex; // Index of the column ImS16 SortOrder; // Index within parent ImGuiTableSortSpecs (always stored in order starting from 0, tables sorted on a single criteria will always have a 0 here) - ImGuiSortDirection SortDirection : 8; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending + ImGuiSortDirection SortDirection; // ImGuiSortDirection_Ascending or ImGuiSortDirection_Descending ImGuiTableColumnSortSpecs() { memset(this, 0, sizeof(*this)); } }; //----------------------------------------------------------------------------- -// [SECTION] Helpers: Memory allocations macros, ImVector<> +// [SECTION] Helpers: Debug log, memory allocations macros, ImVector<> //----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +// Debug Logging into ShowDebugLogWindow(), tty and more. +//----------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_DEBUG_TOOLS +#define IMGUI_DEBUG_LOG(...) ImGui::DebugLog(__VA_ARGS__) +#else +#define IMGUI_DEBUG_LOG(...) ((void)0) +#endif + //----------------------------------------------------------------------------- // IM_MALLOC(), IM_FREE(), IM_NEW(), IM_PLACEMENT_NEW(), IM_DELETE() // We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. @@ -1992,7 +2140,7 @@ struct ImGuiStyle float FrameBorderSize; // Thickness of border around frames. Generally set to 0.0f or 1.0f. (Other values are not well tested and more CPU/GPU costly). ImVec2 ItemSpacing; // Horizontal and vertical spacing between widgets/lines. ImVec2 ItemInnerSpacing; // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label). - ImVec2 CellPadding; // Padding within a table cell. CellPadding.y may be altered between different rows. + ImVec2 CellPadding; // Padding within a table cell. Cellpadding.x is locked for entire table. CellPadding.y may be altered between different rows. ImVec2 TouchExtraPadding; // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! float IndentSpacing; // Horizontal indentation when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). float ColumnsMinSpacing; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). @@ -2005,16 +2153,18 @@ struct ImGuiStyle float TabBorderSize; // Thickness of border around tabs. float TabMinWidthForCloseButton; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. float TabBarBorderSize; // Thickness of tab-bar separator, which takes on the tab active color to denote focus. + float TabBarOverlineSize; // Thickness of tab-bar overline, which highlights the selected tab-bar. float TableAngledHeadersAngle; // Angle of angled headers (supported values range from -50.0f degrees to +50.0f degrees). + ImVec2 TableAngledHeadersTextAlign;// Alignment of angled headers within the cell ImGuiDir ColorButtonPosition; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. ImVec2 ButtonTextAlign; // Alignment of button text when button is larger than text. Defaults to (0.5f, 0.5f) (centered). ImVec2 SelectableTextAlign; // Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. - float SeparatorTextBorderSize; // Thickkness of border in SeparatorText() + float SeparatorTextBorderSize; // Thickness of border in SeparatorText() ImVec2 SeparatorTextAlign; // Alignment of text within the separator. Defaults to (0.0f, 0.5f) (left aligned, center). ImVec2 SeparatorTextPadding; // Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y. - ImVec2 DisplayWindowPadding; // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. - ImVec2 DisplaySafeAreaPadding; // If you cannot see the edges of your screen (e.g. on a TV) increase the safe area padding. Apply to popups/tooltips as well regular windows. NB: Prefer configuring your TV sets correctly! - float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. + ImVec2 DisplayWindowPadding; // Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen. + ImVec2 DisplaySafeAreaPadding; // Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured). + float MouseCursorScale; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). We apply per-monitor DPI scaling over this scale. May be removed later. bool AntiAliasedLines; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). bool AntiAliasedLinesUseTex; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). Latched at the beginning of the frame (copied to ImDrawList). bool AntiAliasedFill; // Enable anti-aliased edges around filled shapes (rounded rectangles, circles, etc.). Disable if you are really tight on CPU/GPU. Latched at the beginning of the frame (copied to ImDrawList). @@ -2039,6 +2189,9 @@ struct ImGuiStyle //----------------------------------------------------------------------------- // Communicate most settings and inputs/outputs to Dear ImGui using this structure. // Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage. +// It is generally expected that: +// - initialization: backends and user code writes to ImGuiIO. +// - main loop: backends writes to ImGuiIO, user code and imgui code reads from ImGuiIO. //----------------------------------------------------------------------------- // [Internal] Storage used by IsKeyDown(), IsKeyPressed() etc functions. @@ -2074,7 +2227,8 @@ struct ImGuiIO // Miscellaneous options bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by backend implementations. - bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // OS X style: Text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl. + bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // Swap Cmd<>Ctrl keys + OS X style text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl. + bool ConfigNavSwapGamepadButtons; // = false // Swap Activate<>Cancel (A<>B) buttons, matching typical "Nintendo/Japanese style" gamepad layout. bool ConfigInputTrickleEventQueue; // = true // Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates. bool ConfigInputTextCursorBlink; // = true // Enable blinking cursor (optional as some users consider it to be distracting). bool ConfigInputTextEnterKeepActive; // = false // [BETA] Pressing Enter will keep item active and select contents (single-line only). @@ -2111,9 +2265,9 @@ struct ImGuiIO // Option to deactivate io.AddFocusEvent(false) handling. // - May facilitate interactions with a debugger when focus loss leads to clearing inputs data. // - Backends may have other side-effects on focus loss, so this will reduce side-effects but not necessary remove all of them. - bool ConfigDebugIgnoreFocusLoss; // = false // Ignore io.AddFocusEvent(false), consequently not calling io.ClearInputKeys() in input processing. + bool ConfigDebugIgnoreFocusLoss; // = false // Ignore io.AddFocusEvent(false), consequently not calling io.ClearInputKeys()/io.ClearInputMouse() in input processing. - // Options to audit .ini data + // Option to audit .ini data bool ConfigDebugIniSettings; // = false // Save .ini data with extra comments (particularly helpful for Docking, but makes saving slower) //------------------------------------------------------------------ @@ -2134,9 +2288,15 @@ struct ImGuiIO void (*SetClipboardTextFn)(void* user_data, const char* text); void* ClipboardUserData; + // Optional: Open link/folder/file in OS Shell + // (default to use ShellExecuteA() on Windows, system() on Linux/Mac) + bool (*PlatformOpenInShellFn)(ImGuiContext* ctx, const char* path); + void* PlatformOpenInShellUserData; + // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) // (default to use native imm32 api on Windows) - void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); + void (*PlatformSetImeDataFn)(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); + //void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); // [Renamed to io.PlatformSetImeDataFn in 1.91.0] // Optional: Platform locale ImWchar PlatformLocaleDecimalPoint; // '.' // [Experimental] Configure decimal point e.g. '.' or ',' useful for some languages (e.g. German), generally pulled from *localeconv()->decimal_point @@ -2160,7 +2320,8 @@ struct ImGuiIO IMGUI_API void SetKeyEventNativeData(ImGuiKey key, int native_keycode, int native_scancode, int native_legacy_index = -1); // [Optional] Specify index for legacy <1.87 IsKeyXXX() functions with native indices + specify native keycode, scancode. IMGUI_API void SetAppAcceptingEvents(bool accepting_events); // Set master flag for accepting key/mouse/text events (default to true). Useful if you have native dialog boxes that are interrupting your application loop/refresh, and you want to disable events being queued while your app is frozen. IMGUI_API void ClearEventsQueue(); // Clear all incoming events. - IMGUI_API void ClearInputKeys(); // Clear current keyboard/mouse/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. + IMGUI_API void ClearInputKeys(); // Clear current keyboard/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. + IMGUI_API void ClearInputMouse(); // Clear current mouse state. #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS IMGUI_API void ClearInputCharacters(); // [Obsoleted in 1.89.8] Clear the current frame text input buffer. Now included within ClearInputKeys(). #endif @@ -2185,16 +2346,6 @@ struct ImGuiIO int MetricsActiveWindows; // Number of active windows ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. - // Legacy: before 1.87, we required backend to fill io.KeyMap[] (imgui->native map) during initialization and io.KeysDown[] (native indices) every frame. - // This is still temporarily supported as a legacy feature. However the new preferred scheme is for backend to call io.AddKeyEvent(). - // Old (<1.87): ImGui::IsKeyPressed(ImGui::GetIO().KeyMap[ImGuiKey_Space]) --> New (1.87+) ImGui::IsKeyPressed(ImGuiKey_Space) -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - int KeyMap[ImGuiKey_COUNT]; // [LEGACY] Input: map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. The first 512 are now unused and should be kept zero. Legacy backend will write into KeyMap[] using ImGuiKey_ indices which are always >512. - bool KeysDown[ImGuiKey_COUNT]; // [LEGACY] Input: Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). This used to be [512] sized. It is now ImGuiKey_COUNT to allow legacy io.KeysDown[GetKeyIndex(...)] to work without an overflow. - float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. - //void* ImeWindowHandle; // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. -#endif - //------------------------------------------------------------------ // [Internal] Dear ImGui will maintain those fields. Forward compatibility not guaranteed! //------------------------------------------------------------------ @@ -2215,7 +2366,7 @@ struct ImGuiIO bool KeySuper; // Keyboard modifier down: Cmd/Super/Windows // Other state maintained from data above + IO function calls - ImGuiKeyChord KeyMods; // Key mods flags (any of ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Alt/ImGuiMod_Super flags, same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags. DOES NOT CONTAINS ImGuiMod_Shortcut which is pretranslated). Read-only, updated by NewFrame() + ImGuiKeyChord KeyMods; // Key mods flags (any of ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Alt/ImGuiMod_Super flags, same as io.KeyCtrl/KeyShift/KeyAlt/KeySuper but merged into flags. Read-only, updated by NewFrame() ImGuiKeyData KeysData[ImGuiKey_KeysData_SIZE]; // Key state for all known keys. Use IsKeyXXX() functions to access this. bool WantCaptureMouseUnlessPopupClose; // Alternative to WantCaptureMouse: (WantCaptureMouse == true && WantCaptureMouseUnlessPopupClose == false) when a click over void is expected to close a popup. ImVec2 MousePosPrev; // Previous mouse position (note that MouseDelta is not necessary == MousePos-MousePosPrev, in case either position is invalid) @@ -2229,6 +2380,7 @@ struct ImGuiIO bool MouseDownOwned[5]; // Track if button was clicked inside a dear imgui window or over void blocked by a popup. We don't request mouse capture from the application if click started outside ImGui bounds. bool MouseDownOwnedUnlessPopupClose[5]; // Track if button was clicked inside a dear imgui window. bool MouseWheelRequestAxisSwap; // On a non-Mac system, holding SHIFT requests WheelY to perform the equivalent of a WheelX event. On a Mac system this is already enforced by the system. + bool MouseCtrlLeftAsRightClick; // (OSX) Set to true when the current click was a ctrl-click that spawned a simulated right click float MouseDownDuration[5]; // Duration the mouse button has been down (0.0f == just clicked) float MouseDownDurationPrev[5]; // Previous time the mouse button has been down float MouseDragMaxDistanceSqr[5]; // Squared maximum distance of how much mouse has traveled from the clicking point (used for moving thresholds) @@ -2240,6 +2392,16 @@ struct ImGuiIO ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16() ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. + // Legacy: before 1.87, we required backend to fill io.KeyMap[] (imgui->native map) during initialization and io.KeysDown[] (native indices) every frame. + // This is still temporarily supported as a legacy feature. However the new preferred scheme is for backend to call io.AddKeyEvent(). + // Old (<1.87): ImGui::IsKeyPressed(ImGui::GetIO().KeyMap[ImGuiKey_Space]) --> New (1.87+) ImGui::IsKeyPressed(ImGuiKey_Space) +#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO + int KeyMap[ImGuiKey_COUNT]; // [LEGACY] Input: map of indices into the KeysDown[512] entries array which represent your "native" keyboard state. The first 512 are now unused and should be kept zero. Legacy backend will write into KeyMap[] using ImGuiKey_ indices which are always >512. + bool KeysDown[ImGuiKey_COUNT]; // [LEGACY] Input: Keyboard keys that are pressed (ideally left in the "native" order your engine has access to keyboard keys, so you can use your own defines/enums for keys). This used to be [512] sized. It is now ImGuiKey_COUNT to allow legacy io.KeysDown[GetKeyIndex(...)] to work without an overflow. + float NavInputs[ImGuiNavInput_COUNT]; // [LEGACY] Since 1.88, NavInputs[] was removed. Backends from 1.60 to 1.86 won't build. Feed gamepad inputs via io.AddKeyEvent() and ImGuiKey_GamepadXXX enums. + //void* ImeWindowHandle; // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. +#endif + IMGUI_API ImGuiIO(); }; @@ -2264,6 +2426,8 @@ struct ImGuiInputTextCallbackData void* UserData; // What user passed to InputText() // Read-only // Arguments for the different callback events + // - During Resize callback, Buf will be same as your input buffer. + // - However, during Completion/History/Always callback, Buf always points to our own internal data (it is not the same as your buffer)! Changes to it will be reflected into your own buffer shortly after the callback. // - To modify the text buffer in a callback, prefer using the InsertChars() / DeleteChars() function. InsertChars() will take care of calling the resize callback if necessary. // - If you know your edits are not going to resize the underlying buffer allocation, you may modify the contents of 'Buf[]' directly. You need to update 'BufTextLen' accordingly (0 <= BufTextLen < BufSize) and set 'BufDirty'' to true so InputText can update its internal state. ImWchar EventChar; // Character input // Read-write // [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0; @@ -2386,6 +2550,16 @@ struct ImGuiTextBuffer IMGUI_API void appendfv(const char* fmt, va_list args) IM_FMTLIST(2); }; +// [Internal] Key+Value for ImGuiStorage +struct ImGuiStoragePair +{ + ImGuiID key; + union { int val_i; float val_f; void* val_p; }; + ImGuiStoragePair(ImGuiID _key, int _val) { key = _key; val_i = _val; } + ImGuiStoragePair(ImGuiID _key, float _val) { key = _key; val_f = _val; } + ImGuiStoragePair(ImGuiID _key, void* _val) { key = _key; val_p = _val; } +}; + // Helper: Key->Value storage // Typically you don't have to worry about this since a storage is held within each Window. // We use it to e.g. store collapse state for a tree (Int 0/1) @@ -2397,15 +2571,6 @@ struct ImGuiTextBuffer struct ImGuiStorage { // [Internal] - struct ImGuiStoragePair - { - ImGuiID key; - union { int val_i; float val_f; void* val_p; }; - ImGuiStoragePair(ImGuiID _key, int _val) { key = _key; val_i = _val; } - ImGuiStoragePair(ImGuiID _key, float _val) { key = _key; val_f = _val; } - ImGuiStoragePair(ImGuiID _key, void* _val) { key = _key; val_p = _val; } - }; - ImVector Data; // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N) @@ -2434,6 +2599,10 @@ struct ImGuiStorage IMGUI_API void BuildSortByKey(); // Obsolete: use on your own storage if you know only integer are being stored (open/close all tree nodes) IMGUI_API void SetAllInt(int val); + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + //typedef ::ImGuiStoragePair ImGuiStoragePair; // 1.90.8: moved type outside struct +#endif }; // Helper: Manually clip large list of items. @@ -2464,9 +2633,10 @@ struct ImGuiListClipper int ItemsCount; // [Internal] Number of items float ItemsHeight; // [Internal] Height of item after a first step and item submission can calculate it float StartPosY; // [Internal] Cursor position at the time of Begin() or after table frozen rows are all processed + double StartSeekOffsetY; // [Internal] Account for frozen rows in a table and initial loss of precision in very large windows. void* TempData; // [Internal] Internal data - // items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step) + // items_count: Use INT_MAX if you don't know how many items you have (in which case the cursor won't be advanced in the final step, and you can call SeekCursorForItem() manually if you need) // items_height: Use -1.0f to be calculated automatically on first step. Otherwise pass in the distance between your items, typically GetTextLineHeightWithSpacing() or GetFrameHeightWithSpacing(). IMGUI_API ImGuiListClipper(); IMGUI_API ~ImGuiListClipper(); @@ -2479,6 +2649,11 @@ struct ImGuiListClipper inline void IncludeItemByIndex(int item_index) { IncludeItemsByIndex(item_index, item_index + 1); } IMGUI_API void IncludeItemsByIndex(int item_begin, int item_end); // item_end is exclusive e.g. use (42, 42+1) to make item 42 never clipped. + // Seek cursor toward given item. This is automatically called while stepping. + // - The only reason to call this is: you can use ImGuiListClipper::Begin(INT_MAX) if you don't know item count ahead of time. + // - In this case, after all steps are done, you'll want to call SeekCursorForItem(item_count). + IMGUI_API void SeekCursorForItem(int item_index); + #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS inline void IncludeRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.9] inline void ForceDisplayRangeByIndices(int item_begin, int item_end) { IncludeItemsByIndex(item_begin, item_end); } // [renamed in 1.89.6] @@ -2559,6 +2734,154 @@ struct ImColor static ImColor HSV(float h, float s, float v, float a = 1.0f) { float r, g, b; ImGui::ColorConvertHSVtoRGB(h, s, v, r, g, b); return ImColor(r, g, b, a); } }; +//----------------------------------------------------------------------------- +// [SECTION] Multi-Select API flags and structures (ImGuiMultiSelectFlags, ImGuiSelectionRequestType, ImGuiSelectionRequest, ImGuiMultiSelectIO, ImGuiSelectionBasicStorage) +//----------------------------------------------------------------------------- + +// Multi-selection system +// Documentation at: https://github.com/ocornut/imgui/wiki/Multi-Select +// - Refer to 'Demo->Widgets->Selection State & Multi-Select' for demos using this. +// - This system implements standard multi-selection idioms (CTRL+Mouse/Keyboard, SHIFT+Mouse/Keyboard, etc) +// with support for clipper (skipping non-visible items), box-select and many other details. +// - Selectable(), Checkbox() are supported but custom widgets may use it as well. +// - TreeNode() is technically supported but... using this correctly is more complicated: you need some sort of linear/random access to your tree, +// which is suited to advanced trees setups also implementing filters and clipper. We will work toward simplifying and demoing it. +// - In the spirit of Dear ImGui design, your code owns actual selection data. +// This is designed to allow all kinds of selection storage you may use in your application e.g. set/map/hash. +// About ImGuiSelectionBasicStorage: +// - This is an optional helper to store a selection state and apply selection requests. +// - It is used by our demos and provided as a convenience to quickly implement multi-selection. +// Usage: +// - Identify submitted items with SetNextItemSelectionUserData(), most likely using an index into your current data-set. +// - Store and maintain actual selection data using persistent object identifiers. +// - Usage flow: +// BEGIN - (1) Call BeginMultiSelect() and retrieve the ImGuiMultiSelectIO* result. +// - (2) Honor request list (SetAll/SetRange requests) by updating your selection data. Same code as Step 6. +// - (3) [If using clipper] You need to make sure RangeSrcItem is always submitted. Calculate its index and pass to clipper.IncludeItemByIndex(). If storing indices in ImGuiSelectionUserData, a simple clipper.IncludeItemByIndex(ms_io->RangeSrcItem) call will work. +// LOOP - (4) Submit your items with SetNextItemSelectionUserData() + Selectable()/TreeNode() calls. +// END - (5) Call EndMultiSelect() and retrieve the ImGuiMultiSelectIO* result. +// - (6) Honor request list (SetAll/SetRange requests) by updating your selection data. Same code as Step 2. +// If you submit all items (no clipper), Step 2 and 3 are optional and will be handled by each item themselves. It is fine to always honor those steps. +// About ImGuiSelectionUserData: +// - This can store an application-defined identifier (e.g. index or pointer) submitted via SetNextItemSelectionUserData(). +// - In return we store them into RangeSrcItem/RangeFirstItem/RangeLastItem and other fields in ImGuiMultiSelectIO. +// - Most applications will store an object INDEX, hence the chosen name and type. Storing an index is natural, because +// SetRange requests will give you two end-points and you will need to iterate/interpolate between them to update your selection. +// - However it is perfectly possible to store a POINTER or another IDENTIFIER inside ImGuiSelectionUserData. +// Our system never assume that you identify items by indices, it never attempts to interpolate between two values. +// - If you enable ImGuiMultiSelectFlags_NoRangeSelect then it is guaranteed that you will never have to interpolate +// between two ImGuiSelectionUserData, which may be a convenient way to use part of the feature with less code work. +// - As most users will want to store an index, for convenience and to reduce confusion we use ImS64 instead of void*, +// being syntactically easier to downcast. Feel free to reinterpret_cast and store a pointer inside. + +// Flags for BeginMultiSelect() +enum ImGuiMultiSelectFlags_ +{ + ImGuiMultiSelectFlags_None = 0, + ImGuiMultiSelectFlags_SingleSelect = 1 << 0, // Disable selecting more than one item. This is available to allow single-selection code to share same code/logic if desired. It essentially disables the main purpose of BeginMultiSelect() tho! + ImGuiMultiSelectFlags_NoSelectAll = 1 << 1, // Disable CTRL+A shortcut to select all. + ImGuiMultiSelectFlags_NoRangeSelect = 1 << 2, // Disable Shift+selection mouse/keyboard support (useful for unordered 2D selection). With BoxSelect is also ensure contiguous SetRange requests are not combined into one. This allows not handling interpolation in SetRange requests. + ImGuiMultiSelectFlags_NoAutoSelect = 1 << 3, // Disable selecting items when navigating (useful for e.g. supporting range-select in a list of checkboxes). + ImGuiMultiSelectFlags_NoAutoClear = 1 << 4, // Disable clearing selection when navigating or selecting another one (generally used with ImGuiMultiSelectFlags_NoAutoSelect. useful for e.g. supporting range-select in a list of checkboxes). + ImGuiMultiSelectFlags_NoAutoClearOnReselect = 1 << 5, // Disable clearing selection when clicking/selecting an already selected item. + ImGuiMultiSelectFlags_BoxSelect1d = 1 << 6, // Enable box-selection with same width and same x pos items (e.g. full row Selectable()). Box-selection works better with little bit of spacing between items hit-box in order to be able to aim at empty space. + ImGuiMultiSelectFlags_BoxSelect2d = 1 << 7, // Enable box-selection with varying width or varying x pos items support (e.g. different width labels, or 2D layout/grid). This is slower: alters clipping logic so that e.g. horizontal movements will update selection of normally clipped items. + ImGuiMultiSelectFlags_BoxSelectNoScroll = 1 << 8, // Disable scrolling when box-selecting near edges of scope. + ImGuiMultiSelectFlags_ClearOnEscape = 1 << 9, // Clear selection when pressing Escape while scope is focused. + ImGuiMultiSelectFlags_ClearOnClickVoid = 1 << 10, // Clear selection when clicking on empty location within scope. + ImGuiMultiSelectFlags_ScopeWindow = 1 << 11, // Scope for _BoxSelect and _ClearOnClickVoid is whole window (Default). Use if BeginMultiSelect() covers a whole window or used a single time in same window. + ImGuiMultiSelectFlags_ScopeRect = 1 << 12, // Scope for _BoxSelect and _ClearOnClickVoid is rectangle encompassing BeginMultiSelect()/EndMultiSelect(). Use if BeginMultiSelect() is called multiple times in same window. + ImGuiMultiSelectFlags_SelectOnClick = 1 << 13, // Apply selection on mouse down when clicking on unselected item. (Default) + ImGuiMultiSelectFlags_SelectOnClickRelease = 1 << 14, // Apply selection on mouse release when clicking an unselected item. Allow dragging an unselected item without altering selection. + //ImGuiMultiSelectFlags_RangeSelect2d = 1 << 15, // Shift+Selection uses 2d geometry instead of linear sequence, so possible to use Shift+up/down to select vertically in grid. Analogous to what BoxSelect does. + ImGuiMultiSelectFlags_NavWrapX = 1 << 16, // [Temporary] Enable navigation wrapping on X axis. Provided as a convenience because we don't have a design for the general Nav API for this yet. When the more general feature be public we may obsolete this flag in favor of new one. +}; + +// Main IO structure returned by BeginMultiSelect()/EndMultiSelect(). +// This mainly contains a list of selection requests. +// - Use 'Demo->Tools->Debug Log->Selection' to see requests as they happen. +// - Some fields are only useful if your list is dynamic and allows deletion (getting post-deletion focus/state right is shown in the demo) +// - Below: who reads/writes each fields? 'r'=read, 'w'=write, 'ms'=multi-select code, 'app'=application/user code. +struct ImGuiMultiSelectIO +{ + //------------------------------------------// BeginMultiSelect / EndMultiSelect + ImVector Requests; // ms:w, app:r / ms:w app:r // Requests to apply to your selection data. + ImGuiSelectionUserData RangeSrcItem; // ms:w app:r / // (If using clipper) Begin: Source item (often the first selected item) must never be clipped: use clipper.IncludeItemByIndex() to ensure it is submitted. + ImGuiSelectionUserData NavIdItem; // ms:w, app:r / // (If using deletion) Last known SetNextItemSelectionUserData() value for NavId (if part of submitted items). + bool NavIdSelected; // ms:w, app:r / app:r // (If using deletion) Last known selection state for NavId (if part of submitted items). + bool RangeSrcReset; // app:w / ms:r // (If using deletion) Set before EndMultiSelect() to reset ResetSrcItem (e.g. if deleted selection). + int ItemsCount; // ms:w, app:r / app:r // 'int items_count' parameter to BeginMultiSelect() is copied here for convenience, allowing simpler calls to your ApplyRequests handler. Not used internally. +}; + +// Selection request type +enum ImGuiSelectionRequestType +{ + ImGuiSelectionRequestType_None = 0, + ImGuiSelectionRequestType_SetAll, // Request app to clear selection (if Selected==false) or select all items (if Selected==true). We cannot set RangeFirstItem/RangeLastItem as its contents is entirely up to user (not necessarily an index) + ImGuiSelectionRequestType_SetRange, // Request app to select/unselect [RangeFirstItem..RangeLastItem] items (inclusive) based on value of Selected. Only EndMultiSelect() request this, app code can read after BeginMultiSelect() and it will always be false. +}; + +// Selection request item +struct ImGuiSelectionRequest +{ + //------------------------------------------// BeginMultiSelect / EndMultiSelect + ImGuiSelectionRequestType Type; // ms:w, app:r / ms:w, app:r // Request type. You'll most often receive 1 Clear + 1 SetRange with a single-item range. + bool Selected; // ms:w, app:r / ms:w, app:r // Parameter for SetAll/SetRange requests (true = select, false = unselect) + ImS8 RangeDirection; // / ms:w app:r // Parameter for SetRange request: +1 when RangeFirstItem comes before RangeLastItem, -1 otherwise. Useful if you want to preserve selection order on a backward Shift+Click. + ImGuiSelectionUserData RangeFirstItem; // / ms:w, app:r // Parameter for SetRange request (this is generally == RangeSrcItem when shift selecting from top to bottom). + ImGuiSelectionUserData RangeLastItem; // / ms:w, app:r // Parameter for SetRange request (this is generally == RangeSrcItem when shift selecting from bottom to top). Inclusive! +}; + +// Optional helper to store multi-selection state + apply multi-selection requests. +// - Used by our demos and provided as a convenience to easily implement basic multi-selection. +// - Iterate selection with 'void* it = NULL; ImGuiID id; while (selection.GetNextSelectedItem(&it, &id)) { ... }' +// Or you can check 'if (Contains(id)) { ... }' for each possible object if their number is not too high to iterate. +// - USING THIS IS NOT MANDATORY. This is only a helper and not a required API. +// To store a multi-selection, in your application you could: +// - Use this helper as a convenience. We use our simple key->value ImGuiStorage as a std::set replacement. +// - Use your own external storage: e.g. std::set, std::vector, interval trees, intrusively stored selection etc. +// In ImGuiSelectionBasicStorage we: +// - always use indices in the multi-selection API (passed to SetNextItemSelectionUserData(), retrieved in ImGuiMultiSelectIO) +// - use the AdapterIndexToStorageId() indirection layer to abstract how persistent selection data is derived from an index. +// - use decently optimized logic to allow queries and insertion of very large selection sets. +// - do not preserve selection order. +// Many combinations are possible depending on how you prefer to store your items and how you prefer to store your selection. +// Large applications are likely to eventually want to get rid of this indirection layer and do their own thing. +// See https://github.com/ocornut/imgui/wiki/Multi-Select for details and pseudo-code using this helper. +struct ImGuiSelectionBasicStorage +{ + // Members + int Size; // // Number of selected items, maintained by this helper. + bool PreserveOrder; // = false // GetNextSelectedItem() will return ordered selection (currently implemented by two additional sorts of selection. Could be improved) + void* UserData; // = NULL // User data for use by adapter function // e.g. selection.UserData = (void*)my_items; + ImGuiID (*AdapterIndexToStorageId)(ImGuiSelectionBasicStorage* self, int idx); // e.g. selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { return ((MyItems**)self->UserData)[idx]->ID; }; + int _SelectionOrder;// [Internal] Increasing counter to store selection order + ImGuiStorage _Storage; // [Internal] Selection set. Think of this as similar to e.g. std::set. Prefer not accessing directly: iterate with GetNextSelectedItem(). + + // Methods + ImGuiSelectionBasicStorage(); + IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Apply selection requests coming from BeginMultiSelect() and EndMultiSelect() functions. It uses 'items_count' passed to BeginMultiSelect() + IMGUI_API bool Contains(ImGuiID id) const; // Query if an item id is in selection. + IMGUI_API void Clear(); // Clear selection + IMGUI_API void Swap(ImGuiSelectionBasicStorage& r); // Swap two selections + IMGUI_API void SetItemSelected(ImGuiID id, bool selected); // Add/remove an item from selection (generally done by ApplyRequests() function) + IMGUI_API bool GetNextSelectedItem(void** opaque_it, ImGuiID* out_id); // Iterate selection with 'void* it = NULL; ImGuiId id; while (selection.GetNextSelectedItem(&it, &id)) { ... }' + inline ImGuiID GetStorageIdFromIndex(int idx) { return AdapterIndexToStorageId(this, idx); } // Convert index to item id based on provided adapter. +}; + +// Optional helper to apply multi-selection requests to existing randomly accessible storage. +// Convenient if you want to quickly wire multi-select API on e.g. an array of bool or items storing their own selection state. +struct ImGuiSelectionExternalStorage +{ + // Members + void* UserData; // User data for use by adapter function // e.g. selection.UserData = (void*)my_items; + void (*AdapterSetItemSelected)(ImGuiSelectionExternalStorage* self, int idx, bool selected); // e.g. AdapterSetItemSelected = [](ImGuiSelectionExternalStorage* self, int idx, bool selected) { ((MyItems**)self->UserData)[idx]->Selected = selected; } + + // Methods + IMGUI_API ImGuiSelectionExternalStorage(); + IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Apply selection requests by using AdapterSetItemSelected() calls +}; + //----------------------------------------------------------------------------- // [SECTION] Drawing API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData) // Hold a series of drawing commands. The user provides a renderer for ImDrawData which essentially contains an array of ImDrawList. @@ -2707,15 +3030,15 @@ struct ImDrawList // [Internal, used while building lists] unsigned int _VtxCurrentIdx; // [Internal] generally == VtxBuffer.Size unless we are past 64K vertices, in which case this gets reset to 0. ImDrawListSharedData* _Data; // Pointer to shared draw data (you can use ImGui::GetDrawListSharedData() to get the one from current ImGui context) - const char* _OwnerName; // Pointer to owner window's name for debugging ImDrawVert* _VtxWritePtr; // [Internal] point within VtxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) ImDrawIdx* _IdxWritePtr; // [Internal] point within IdxBuffer.Data after each add command (to avoid using the ImVector<> operators too much) - ImVector _ClipRectStack; // [Internal] - ImVector _TextureIdStack; // [Internal] ImVector _Path; // [Internal] current path building ImDrawCmdHeader _CmdHeader; // [Internal] template of active commands. Fields should match those of CmdBuffer.back(). ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!) + ImVector _ClipRectStack; // [Internal] + ImVector _TextureIdStack; // [Internal] float _FringeScale; // [Internal] anti-alias fringe is scaled by this value, this helps to keep things sharp while zooming at vertex buffer content + const char* _OwnerName; // Pointer to owner window's name for debugging // If you want to create ImDrawList instances, pass them ImGui::GetDrawListSharedData() or create and use your own ImDrawListSharedData (so you can use ImDrawList without ImGui) ImDrawList(ImDrawListSharedData* shared_data) { memset(this, 0, sizeof(*this)); _Data = shared_data; } @@ -2748,15 +3071,20 @@ struct ImDrawList IMGUI_API void AddCircleFilled(const ImVec2& center, float radius, ImU32 col, int num_segments = 0); IMGUI_API void AddNgon(const ImVec2& center, float radius, ImU32 col, int num_segments, float thickness = 1.0f); IMGUI_API void AddNgonFilled(const ImVec2& center, float radius, ImU32 col, int num_segments); - IMGUI_API void AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f); - IMGUI_API void AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0); + IMGUI_API void AddEllipse(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f); + IMGUI_API void AddEllipseFilled(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot = 0.0f, int num_segments = 0); IMGUI_API void AddText(const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL); IMGUI_API void AddText(const ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f, const ImVec4* cpu_fine_clip_rect = NULL); - IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness); - IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); IMGUI_API void AddBezierCubic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void AddBezierQuadratic(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, ImU32 col, float thickness, int num_segments = 0); // Quadratic Bezier (3 control points) + // General polygon + // - Only simple polygons are supported by filling functions (no self-intersections, no holes). + // - Concave polygon fill is more expensive than convex one: it has O(N^2) complexity. Provided as a convenience fo user but not used by main library. + IMGUI_API void AddPolyline(const ImVec2* points, int num_points, ImU32 col, ImDrawFlags flags, float thickness); + IMGUI_API void AddConvexPolyFilled(const ImVec2* points, int num_points, ImU32 col); + IMGUI_API void AddConcavePolyFilled(const ImVec2* points, int num_points, ImU32 col); + // Image primitives // - Read FAQ to understand what ImTextureID is. // - "p_min" and "p_max" represent the upper-left and lower-right corners of the rectangle. @@ -2772,10 +3100,11 @@ struct ImDrawList inline void PathLineTo(const ImVec2& pos) { _Path.push_back(pos); } inline void PathLineToMergeDuplicate(const ImVec2& pos) { if (_Path.Size == 0 || memcmp(&_Path.Data[_Path.Size - 1], &pos, 8) != 0) _Path.push_back(pos); } inline void PathFillConvex(ImU32 col) { AddConvexPolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } + inline void PathFillConcave(ImU32 col) { AddConcavePolyFilled(_Path.Data, _Path.Size, col); _Path.Size = 0; } inline void PathStroke(ImU32 col, ImDrawFlags flags = 0, float thickness = 1.0f) { AddPolyline(_Path.Data, _Path.Size, col, flags, thickness); _Path.Size = 0; } IMGUI_API void PathArcTo(const ImVec2& center, float radius, float a_min, float a_max, int num_segments = 0); IMGUI_API void PathArcToFast(const ImVec2& center, float radius, int a_min_of_12, int a_max_of_12); // Use precomputed angles for a 12 steps circle - IMGUI_API void PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments = 0); // Ellipse + IMGUI_API void PathEllipticalArcTo(const ImVec2& center, const ImVec2& radius, float rot, float a_min, float a_max, int num_segments = 0); // Ellipse IMGUI_API void PathBezierCubicCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0); // Cubic Bezier (4 control points) IMGUI_API void PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments = 0); // Quadratic Bezier (3 control points) IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawFlags flags = 0); @@ -2808,6 +3137,9 @@ struct ImDrawList inline void PrimVtx(const ImVec2& pos, const ImVec2& uv, ImU32 col) { PrimWriteIdx((ImDrawIdx)_VtxCurrentIdx); PrimWriteVtx(pos, uv, col); } // Write vertex with unique index // Obsolete names + //inline void AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f) { AddEllipse(center, ImVec2(radius_x, radius_y), col, rot, num_segments, thickness); } // OBSOLETED in 1.90.5 (Mar 2024) + //inline void AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0) { AddEllipseFilled(center, ImVec2(radius_x, radius_y), col, rot, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024) + //inline void PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments = 0) { PathEllipticalArcTo(center, ImVec2(radius_x, radius_y), rot, a_min, a_max, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024) //inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) //inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) @@ -2996,7 +3328,7 @@ struct ImFontAtlas // You can request arbitrary rectangles to be packed into the atlas, for your own purposes. // - After calling Build(), you can query the rectangle position and render your pixels. - // - If you render colored output, set 'atlas->TexPixelsUseColors = true' as this may help some backends decide of prefered texture format. + // - If you render colored output, set 'atlas->TexPixelsUseColors = true' as this may help some backends decide of preferred texture format. // - You can also request your rectangles to be mapped as font glyph (given a font + Unicode point), // so you can render e.g. custom colorful icons and use them as regular glyphs. // - Read docs/FONTS.md for more details about using colorful icons. @@ -3073,7 +3405,7 @@ struct ImFont float EllipsisCharStep; // 4 // out // Step between characters when EllipsisCount > 0 bool DirtyLookupTables; // 1 // out // float Scale; // 4 // in // = 1.f // Base font scale, multiplied by the per-window font scale which you can adjust with SetWindowFontScale() - float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] + float Ascent, Descent; // 4+4 // out // // Ascent: distance from top to bottom of e.g. 'A' [0..FontSize] (unscaled) int MetricsTotalSurface;// 4 // out // // Total surface in pixels to get an idea of the font rasterization/texture cost (not exact, we approximate the cost of padding between glyphs) ImU8 Used4kPagesMap[(IM_UNICODE_CODEPOINT_MAX+1)/4096/8]; // 2 bytes if ImWchar=ImWchar16, 34 bytes if ImWchar==ImWchar32. Store 1-bit for each block of 4K codepoints that has one active glyph. This is mainly used to facilitate iterations across all used codepoints. @@ -3133,6 +3465,7 @@ struct ImGuiViewport ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size) // Platform/Backend Dependent Data + void* PlatformHandle; // void* to hold higher-level, platform window handle (e.g. HWND, GLFWWindow*, SDL_Window*) void* PlatformHandleRaw; // void* to hold lower-level, platform-native window handle (under Win32 this is expected to be a HWND, unused for other platforms) ImGuiViewport() { memset(this, 0, sizeof(*this)); } @@ -3146,7 +3479,7 @@ struct ImGuiViewport // [SECTION] Platform Dependent Interfaces //----------------------------------------------------------------------------- -// (Optional) Support for IME (Input Method Editor) via the io.SetPlatformImeDataFn() function. +// (Optional) Support for IME (Input Method Editor) via the io.PlatformSetImeDataFn() function. struct ImGuiPlatformImeData { bool WantVisible; // A widget wants the IME to be visible @@ -3162,38 +3495,40 @@ struct ImGuiPlatformImeData // Please keep your copy of dear imgui up to date! Occasionally set '#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS' in imconfig.h to stay ahead. //----------------------------------------------------------------------------- -namespace ImGui -{ -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - IMGUI_API ImGuiKey GetKeyIndex(ImGuiKey key); // map ImGuiKey_* values into legacy native key index. == io.KeyMap[key] -#else - static inline ImGuiKey GetKeyIndex(ImGuiKey key) { IM_ASSERT(key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END && "ImGuiKey and native_index was merged together and native_index is disabled by IMGUI_DISABLE_OBSOLETE_KEYIO. Please switch to ImGuiKey."); return key; } -#endif -} - #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS namespace ImGui { + // OBSOLETED in 1.91.0 (from July 2024) + static inline void PushButtonRepeat(bool repeat) { PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); } + static inline void PopButtonRepeat() { PopItemFlag(); } + static inline void PushTabStop(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); } + static inline void PopTabStop() { PopItemFlag(); } + IMGUI_API ImVec2 GetContentRegionMax(); // Content boundaries max (e.g. window boundaries including scrolling, or current column boundaries). You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()! + IMGUI_API ImVec2 GetWindowContentRegionMin(); // Content boundaries min for the window (roughly (0,0)-Scroll), in window-local coordinates. You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()! + IMGUI_API ImVec2 GetWindowContentRegionMax(); // Content boundaries max for the window (roughly (0,0)+Size-Scroll), in window-local coordinates. You should never need this. Always use GetCursorScreenPos() and GetContentRegionAvail()! // OBSOLETED in 1.90.0 (from September 2023) - static inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); } - static inline void EndChildFrame() { EndChild(); } + static inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); } + static inline void EndChildFrame() { EndChild(); } //static inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border //static inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border - static inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); } - IMGUI_API bool ListBox(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int height_in_items = -1); + static inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); } IMGUI_API bool Combo(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int popup_max_height_in_items = -1); + IMGUI_API bool ListBox(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int height_in_items = -1); // OBSOLETED in 1.89.7 (from June 2023) - IMGUI_API void SetItemAllowOverlap(); // Use SetNextItemAllowOverlap() before item. + IMGUI_API void SetItemAllowOverlap(); // Use SetNextItemAllowOverlap() before item. // OBSOLETED in 1.89.4 (from March 2023) - static inline void PushAllowKeyboardFocus(bool tab_stop) { PushTabStop(tab_stop); } - static inline void PopAllowKeyboardFocus() { PopTabStop(); } + static inline void PushAllowKeyboardFocus(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); } + static inline void PopAllowKeyboardFocus() { PopItemFlag(); } // OBSOLETED in 1.89 (from August 2022) IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // Use new ImageButton() signature (explicit item id, regular FramePadding) - // OBSOLETED in 1.88 (from May 2022) - static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. - static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. + // OBSOLETED in 1.87 (from February 2022 but more formally obsoleted April 2024) + IMGUI_API ImGuiKey GetKeyIndex(ImGuiKey key); // Map ImGuiKey_* values into legacy native key index. == io.KeyMap[key]. When using a 1.87+ backend using io.AddKeyEvent(), calling GetKeyIndex() with ANY ImGuiKey_XXXX values will return the same value! + //static inline ImGuiKey GetKeyIndex(ImGuiKey key) { IM_ASSERT(key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END); return key; } // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) + //-- OBSOLETED in 1.88 (from May 2022) + //static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. + //static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. //-- OBSOLETED in 1.86 (from November 2021) //IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Code removed, see 1.90 for last version of the code. Calculate range of visible items for large list of evenly sized items. Prefer using ImGuiListClipper. //-- OBSOLETED in 1.85 (from August 2021) @@ -3266,8 +3601,8 @@ namespace ImGui // RENAMED and MERGED both ImGuiKey_ModXXX and ImGuiModFlags_XXX into ImGuiMod_XXX (from September 2022) // RENAMED ImGuiKeyModFlags -> ImGuiModFlags in 1.88 (from April 2022). Exceptionally commented out ahead of obscolescence schedule to reduce confusion and because they were not meant to be used in the first place. -typedef ImGuiKeyChord ImGuiModFlags; // == int. We generally use ImGuiKeyChord to mean "a ImGuiKey or-ed with any number of ImGuiMod_XXX value", but you may store only mods in there. -enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiModFlags_Shift = ImGuiMod_Shift, ImGuiModFlags_Alt = ImGuiMod_Alt, ImGuiModFlags_Super = ImGuiMod_Super }; +//typedef ImGuiKeyChord ImGuiModFlags; // == int. We generally use ImGuiKeyChord to mean "a ImGuiKey or-ed with any number of ImGuiMod_XXX value", so you may store mods in there. +//enum ImGuiModFlags_ { ImGuiModFlags_None = 0, ImGuiModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiModFlags_Shift = ImGuiMod_Shift, ImGuiModFlags_Alt = ImGuiMod_Alt, ImGuiModFlags_Super = ImGuiMod_Super }; //typedef ImGuiKeyChord ImGuiKeyModFlags; // == int //enum ImGuiKeyModFlags_ { ImGuiKeyModFlags_None = 0, ImGuiKeyModFlags_Ctrl = ImGuiMod_Ctrl, ImGuiKeyModFlags_Shift = ImGuiMod_Shift, ImGuiKeyModFlags_Alt = ImGuiMod_Alt, ImGuiKeyModFlags_Super = ImGuiMod_Super }; diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index 6a9acd05..c0eac113 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -24,15 +24,17 @@ struct GLFWwindow; struct GLFWmonitor; +// Follow "Getting Started" link and check examples/ folder to learn about using backends! IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks); IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); -// Emscripten related initialization phase methods +// Emscripten related initialization phase methods (call after ImGui_ImplGlfw_InitForOpenGL) #ifdef __EMSCRIPTEN__ -IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector); +IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow* window, const char* canvas_selector); +//static inline void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) { ImGui_ImplGlfw_InstallEmscriptenCallbacks(nullptr, canvas_selector); } } // Renamed in 1.91.0 #endif // GLFW callbacks install diff --git a/include/imgui/imgui_impl_opengl3.h b/include/imgui/imgui_impl_opengl3.h index 23eb9247..54545f95 100644 --- a/include/imgui/imgui_impl_opengl3.h +++ b/include/imgui/imgui_impl_opengl3.h @@ -29,7 +29,7 @@ #include "imgui.h" // IMGUI_IMPL_API #ifndef IMGUI_DISABLE -// Backend API +// Follow "Getting Started" link and check examples/ folder to learn about using backends! IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); @@ -41,9 +41,9 @@ IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); -// Specific OpenGL ES versions -//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten -//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android +// Configuration flags to add in your imconfig file: +//#define IMGUI_IMPL_OPENGL_ES2 // Enable ES 2 (Auto-detected on Emscripten) +//#define IMGUI_IMPL_OPENGL_ES3 // Enable ES 3 (Auto-detected on iOS/Android) // You can explicitly select GLES2 or GLES3 API by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. #if !defined(IMGUI_IMPL_OPENGL_ES2) \ diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 4019f937..3fbc3481 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -10,7 +10,7 @@ // THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE. // // IF YOU GET BUILD ERRORS IN THIS FILE (commonly macro redefinitions or function redefinitions): -// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCUDING 'imgui_impl_opengl3_loader.h' +// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCLUDING 'imgui_impl_opengl3_loader.h' // IN THE SAME COMPILATION UNIT AS ONE OF YOUR FILE WHICH IS USING A THIRD-PARTY OPENGL LOADER. // (e.g. COULD HAPPEN IF YOU ARE DOING A UNITY/JUMBO BUILD, OR INCLUDING .CPP FILES FROM OTHERS) // YOU SHOULD NOT BUILD BOTH IN THE SAME COMPILATION UNIT. @@ -18,7 +18,7 @@ // WILL NOT BE USING OUR LOADER, AND INSTEAD EXPECT ANOTHER/YOUR LOADER TO BE AVAILABLE IN THE COMPILATION UNIT. // // Regenerate with: -// python gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt +// python3 gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt // // More info: // https://github.com/dearimgui/gl3w_stripped @@ -118,7 +118,7 @@ extern "C" { ** included as . ** ** glcorearb.h includes only APIs in the latest OpenGL core profile -** implementation together with APIs in newer ARB extensions which +** implementation together with APIs in newer ARB extensions which ** can be supported by the core profile. It does not, and never will ** include functionality removed from the core profile, such as ** fixed-function vertex and fragment processing. @@ -260,8 +260,6 @@ typedef khronos_intptr_t GLintptr; #define GL_ARRAY_BUFFER_BINDING 0x8894 #define GL_ELEMENT_ARRAY_BUFFER_BINDING 0x8895 #define GL_STREAM_DRAW 0x88E0 -#define GL_PIXEL_UNPACK_BUFFER 0x88EC -#define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF typedef void (APIENTRYP PFNGLBINDBUFFERPROC) (GLenum target, GLuint buffer); typedef void (APIENTRYP PFNGLDELETEBUFFERSPROC) (GLsizei n, const GLuint *buffers); typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers); @@ -348,6 +346,10 @@ GLAPI void APIENTRY glUniformMatrix4fv (GLint location, GLsizei count, GLboolean GLAPI void APIENTRY glVertexAttribPointer (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); #endif #endif /* GL_VERSION_2_0 */ +#ifndef GL_VERSION_2_1 +#define GL_PIXEL_UNPACK_BUFFER 0x88EC +#define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF +#endif /* GL_VERSION_2_1 */ #ifndef GL_VERSION_3_0 typedef khronos_uint16_t GLhalf; #define GL_MAJOR_VERSION 0x821B @@ -663,31 +665,123 @@ static GL3WglProc get_proc(const char *proc) #else #include -static void *libgl; -static GL3WglProc (*glx_get_proc_address)(const GLubyte *); +static void* libgl; // OpenGL library +static void* libglx; // GLX library +static void* libegl; // EGL library +static GL3WGetProcAddressProc gl_get_proc_address; -static int open_libgl(void) +static void close_libgl(void) { + if (libgl) { + dlclose(libgl); + libgl = NULL; + } + if (libegl) { + dlclose(libegl); + libegl = NULL; + } + if (libglx) { + dlclose(libglx); + libglx = NULL; + } +} + +static int is_library_loaded(const char* name, void** lib) +{ + *lib = dlopen(name, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); + return *lib != NULL; +} + +static int open_libs(void) +{ + // On Linux we have two APIs to get process addresses: EGL and GLX. + // EGL is supported under both X11 and Wayland, whereas GLX is X11-specific. + + libgl = NULL; + libegl = NULL; + libglx = NULL; + + // First check what's already loaded, the windowing library might have + // already loaded either EGL or GLX and we want to use the same one. + + if (is_library_loaded("libEGL.so.1", &libegl) || + is_library_loaded("libGLX.so.0", &libglx)) { + libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL); + if (libgl) + return GL3W_OK; + else + close_libgl(); + } + + if (is_library_loaded("libGL.so", &libgl)) + return GL3W_OK; + if (is_library_loaded("libGL.so.1", &libgl)) + return GL3W_OK; + if (is_library_loaded("libGL.so.3", &libgl)) + return GL3W_OK; + + // Neither is already loaded, so we have to load one. Try EGL first + // because it is supported under both X11 and Wayland. + + // Load OpenGL + EGL + libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL); + libegl = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL); + if (libgl && libegl) + return GL3W_OK; + else + close_libgl(); + + // Fall back to legacy libGL, which includes GLX // While most systems use libGL.so.1, NetBSD seems to use that libGL.so.3. See https://github.com/ocornut/imgui/issues/6983 libgl = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL); if (!libgl) libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); if (!libgl) libgl = dlopen("libGL.so.3", RTLD_LAZY | RTLD_LOCAL); - if (!libgl) + + if (libgl) + return GL3W_OK; + + return GL3W_ERROR_LIBRARY_OPEN; +} + +static int open_libgl(void) +{ + int res = open_libs(); + if (res) + return res; + + if (libegl) + *(void**)(&gl_get_proc_address) = dlsym(libegl, "eglGetProcAddress"); + else if (libglx) + *(void**)(&gl_get_proc_address) = dlsym(libglx, "glXGetProcAddressARB"); + else + *(void**)(&gl_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + + if (!gl_get_proc_address) { + close_libgl(); return GL3W_ERROR_LIBRARY_OPEN; - *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + } + return GL3W_OK; } -static void close_libgl(void) { dlclose(libgl); } - -static GL3WglProc get_proc(const char *proc) +static GL3WglProc get_proc(const char* proc) { - GL3WglProc res; - res = glx_get_proc_address((const GLubyte *)proc); + GL3WglProc res = NULL; + + // Before EGL version 1.5, eglGetProcAddress doesn't support querying core + // functions and may return a dummy function if we try, so try to load the + // function from the GL library directly first. + if (libegl) + *(void**)(&res) = dlsym(libgl, proc); + if (!res) - *(void **)(&res) = dlsym(libgl, proc); + res = gl_get_proc_address(proc); + + if (!libegl && !res) + *(void**)(&res) = dlsym(libgl, proc); + return res; } #endif diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index 4ac8b1c9..ab43922b 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (internal structures/api) // You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. @@ -14,14 +14,15 @@ Index of this file: // [SECTION] Macros // [SECTION] Generic helpers // [SECTION] ImDrawList support -// [SECTION] Widgets support: flags, enums, data structures // [SECTION] Data types support +// [SECTION] Widgets support: flags, enums, data structures // [SECTION] Popup support // [SECTION] Inputs support // [SECTION] Clipper support // [SECTION] Navigation support // [SECTION] Typing-select support // [SECTION] Columns support +// [SECTION] Box-select support // [SECTION] Multi-select support // [SECTION] Docking support // [SECTION] Viewport support @@ -87,6 +88,8 @@ Index of this file: #pragma clang diagnostic ignored "-Wdouble-promotion" #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #pragma clang diagnostic ignored "-Wmissing-noreturn" // warning: function 'xxx' could be declared with attribute 'noreturn' +#pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -121,10 +124,11 @@ struct ImBitVector; // Store 1-bit per value struct ImRect; // An axis-aligned rectangle (2 points) struct ImDrawDataBuilder; // Helper to build a ImDrawData instance struct ImDrawListSharedData; // Data shared between all ImDrawList instances +struct ImGuiBoxSelectState; // Box-selection state (currently used by multi-selection, could potentially be used by others) struct ImGuiColorMod; // Stacked color modifier, backup of modified data so we can restore it struct ImGuiContext; // Main Dear ImGui context struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine -struct ImGuiDataVarInfo; // Variable information (e.g. to avoid style variables from an enum) +struct ImGuiDataVarInfo; // Variable information (e.g. to access style variables from an enum) struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box @@ -132,8 +136,9 @@ struct ImGuiInputTextDeactivateData;// Short term storage to backup text of a de struct ImGuiLastItemData; // Status storage for last submitted items struct ImGuiLocEntry; // A localization entry. struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only +struct ImGuiMultiSelectState; // Multi-selection persistent state (for focused selection). +struct ImGuiMultiSelectTempData; // Multi-selection temporary state (while traversing). struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result -struct ImGuiNavTreeNodeData; // Temporary storage for last TreeNode() being a Left arrow landing candidate. struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions @@ -146,11 +151,13 @@ struct ImGuiStyleMod; // Stacked style modifier, backup of modifie struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) struct ImGuiTable; // Storage for a table +struct ImGuiTableHeaderData; // Storage for TableAngledHeadersRow() struct ImGuiTableColumn; // Storage for one column of a table struct ImGuiTableInstanceData; // Storage for one instance of a same table struct ImGuiTableTempData; // Temporary storage for one table (one per table in the stack), shared between tables. struct ImGuiTableSettings; // Storage for a table .ini settings struct ImGuiTableColumnsSettings; // Storage for a column .ini settings +struct ImGuiTreeNodeStackData; // Temporary storage for TreeNode(). struct ImGuiTypingSelectState; // Storage for GetTypingSelectRequest() struct ImGuiTypingSelectRequest; // Storage for GetTypingSelectRequest() (aimed to be public) struct ImGuiWindow; // Storage for one window @@ -166,8 +173,6 @@ typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // E typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later) typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags typedef int ImGuiFocusRequestFlags; // -> enum ImGuiFocusRequestFlags_ // Flags: for FocusWindow(); -typedef int ImGuiInputFlags; // -> enum ImGuiInputFlags_ // Flags: for IsKeyPressed(), IsMouseClicked(), SetKeyOwner(), SetItemKeyOwner() etc. -typedef int ImGuiItemFlags; // -> enum ImGuiItemFlags_ // Flags: for PushItemFlag(), g.LastItemData.InFlags typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for g.LastItemData.StatusFlags typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() @@ -179,6 +184,7 @@ typedef int ImGuiSeparatorFlags; // -> enum ImGuiSeparatorFlags_ // F typedef int ImGuiTextFlags; // -> enum ImGuiTextFlags_ // Flags: for TextEx() typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // Flags: for BeginTooltipEx() typedef int ImGuiTypingSelectFlags; // -> enum ImGuiTypingSelectFlags_ // Flags: for GetTypingSelectRequest() +typedef int ImGuiWindowRefreshFlags; // -> enum ImGuiWindowRefreshFlags_ // Flags: for SetNextWindowRefreshPolicy() typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...); @@ -345,6 +351,7 @@ namespace ImStb // - Helper: ImPool<> // - Helper: ImChunkStream<> // - Helper: ImGuiTextIndex +// - Helper: ImGuiStorage //----------------------------------------------------------------------------- // Helpers: Hashing @@ -381,6 +388,7 @@ IM_MSVC_RUNTIME_CHECKS_OFF static inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; } static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } static inline bool ImCharIsBlankW(unsigned int c) { return c == ' ' || c == '\t' || c == 0x3000; } +static inline bool ImCharIsXdigitA(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); } IM_MSVC_RUNTIME_CHECKS_RESTORE // Helpers: Formatting @@ -404,6 +412,7 @@ IMGUI_API int ImTextCountCharsFromUtf8(const char* in_text, const char IMGUI_API int ImTextCountUtf8BytesFromChar(const char* in_text, const char* in_text_end); // return number of bytes to express one char in UTF-8 IMGUI_API int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_end); // return number of bytes to express string in UTF-8 IMGUI_API const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const char* in_text_curr); // return previous UTF-8 code-point. +IMGUI_API int ImTextCountLines(const char* in_text, const char* in_text_end); // return number of lines taken by text. trailing carriage return doesn't count as an extra line. // Helpers: File System #ifdef IMGUI_DISABLE_FILE_FUNCTIONS @@ -468,7 +477,7 @@ template static inline T ImSubClampOverflow(T a, T b, T mn, T mx) // - Misc maths helpers static inline ImVec2 ImMin(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x < rhs.x ? lhs.x : rhs.x, lhs.y < rhs.y ? lhs.y : rhs.y); } static inline ImVec2 ImMax(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x >= rhs.x ? lhs.x : rhs.x, lhs.y >= rhs.y ? lhs.y : rhs.y); } -static inline ImVec2 ImClamp(const ImVec2& v, const ImVec2& mn, ImVec2 mx) { return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); } +static inline ImVec2 ImClamp(const ImVec2& v, const ImVec2&mn, const ImVec2&mx) { return ImVec2((v.x < mn.x) ? mn.x : (v.x > mx.x) ? mx.x : v.x, (v.y < mn.y) ? mn.y : (v.y > mx.y) ? mx.y : v.y); } static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, float t) { return ImVec2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); } static inline ImVec2 ImLerp(const ImVec2& a, const ImVec2& b, const ImVec2& t) { return ImVec2(a.x + (b.x - a.x) * t.x, a.y + (b.y - a.y) * t.y); } static inline ImVec4 ImLerp(const ImVec4& a, const ImVec4& b, float t) { return ImVec4(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t); } @@ -484,6 +493,7 @@ static inline int ImModPositive(int a, int b) static inline float ImDot(const ImVec2& a, const ImVec2& b) { return a.x * b.x + a.y * b.y; } static inline ImVec2 ImRotate(const ImVec2& v, float cos_a, float sin_a) { return ImVec2(v.x * cos_a - v.y * sin_a, v.x * sin_a + v.y * cos_a); } static inline float ImLinearSweep(float current, float target, float speed) { if (current < target) return ImMin(current + speed, target); if (current > target) return ImMax(current - speed, target); return current; } +static inline float ImLinearRemapClamp(float s0, float s1, float d0, float d1, float x) { return ImSaturate((x - s0) / (s1 - s0)) * (d1 - d0) + d0; } static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } static inline bool ImIsFloatAboveGuaranteedIntegerPrecision(float f) { return f <= -16777216 || f >= 16777216; } static inline float ImExponentialMovingAverage(float avg, float sample, int n) { avg -= avg / n; avg += sample / n; return avg; } @@ -498,7 +508,8 @@ IMGUI_API ImVec2 ImLineClosestPoint(const ImVec2& a, const ImVec2& b, const IMGUI_API bool ImTriangleContainsPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API ImVec2 ImTriangleClosestPoint(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p); IMGUI_API void ImTriangleBarycentricCoords(const ImVec2& a, const ImVec2& b, const ImVec2& c, const ImVec2& p, float& out_u, float& out_v, float& out_w); -inline float ImTriangleArea(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ImFabs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y))) * 0.5f; } +inline float ImTriangleArea(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ImFabs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y))) * 0.5f; } +inline bool ImTriangleIsClockwise(const ImVec2& a, const ImVec2& b, const ImVec2& c) { return ((b.x - a.x) * (c.y - b.y)) - ((c.x - b.x) * (b.y - a.y)) > 0.0f; } // Helper: ImVec1 (1D vector) // (this odd construct is used to facilitate the transition between 1D and 2D, and the maintenance of some branches/patches) @@ -717,7 +728,7 @@ struct ImChunkStream void swap(ImChunkStream& rhs) { rhs.Buf.swap(Buf); } }; -// Helper: ImGuiTextIndex<> +// Helper: ImGuiTextIndex // Maintain a line index for a text buffer. This is a strong candidate to be moved into the public API. struct ImGuiTextIndex { @@ -731,6 +742,8 @@ struct ImGuiTextIndex void append(const char* base, int old_size, int new_size); }; +// Helper: ImGuiStorage +IMGUI_API ImGuiStoragePair* ImLowerBound(ImGuiStoragePair* in_begin, ImGuiStoragePair* in_end, ImGuiID key); //----------------------------------------------------------------------------- // [SECTION] ImDrawList support //----------------------------------------------------------------------------- @@ -767,6 +780,7 @@ struct IMGUI_API ImDrawListSharedData ImVec2 TexUvWhitePixel; // UV of white pixel in the atlas ImFont* Font; // Current/default font (optional, for simplified AddText overload) float FontSize; // Current/default font size (optional, for simplified AddText overload) + float FontScale; // Current/default font scale (== FontSize / Font->FontSize) float CurveTessellationTol; // Tessellation tolerance when using PathBezierCurveTo() float CircleSegmentMaxError; // Number of circle segments to use per pixel of radius for AddCircle() etc ImVec4 ClipRectFullscreen; // Value for PushClipRectFullscreen() @@ -793,33 +807,65 @@ struct ImDrawDataBuilder ImDrawDataBuilder() { memset(this, 0, sizeof(*this)); } }; +//----------------------------------------------------------------------------- +// [SECTION] Data types support +//----------------------------------------------------------------------------- + +struct ImGuiDataVarInfo +{ + ImGuiDataType Type; + ImU32 Count; // 1+ + ImU32 Offset; // Offset in parent structure + void* GetVarPtr(void* parent) const { return (void*)((unsigned char*)parent + Offset); } +}; + +struct ImGuiDataTypeStorage +{ + ImU8 Data[8]; // Opaque storage to fit any data up to ImGuiDataType_COUNT +}; + +// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). +struct ImGuiDataTypeInfo +{ + size_t Size; // Size in bytes + const char* Name; // Short descriptive name for the type, for debugging + const char* PrintFmt; // Default printf format for the type + const char* ScanFmt; // Default scanf format for the type +}; + +// Extend ImGuiDataType_ +enum ImGuiDataTypePrivate_ +{ + ImGuiDataType_String = ImGuiDataType_COUNT + 1, + ImGuiDataType_Pointer, + ImGuiDataType_ID, +}; + //----------------------------------------------------------------------------- // [SECTION] Widgets support: flags, enums, data structures //----------------------------------------------------------------------------- -// Flags used by upcoming items +// Extend ImGuiItemFlags // - input: PushItemFlag() manipulates g.CurrentItemFlags, ItemAdd() calls may add extra flags. // - output: stored in g.LastItemData.InFlags -// Current window shared by all windows. -// This is going to be exposed in imgui.h when stabilized enough. -enum ImGuiItemFlags_ +enum ImGuiItemFlagsPrivate_ { // Controlled by user - ImGuiItemFlags_None = 0, - ImGuiItemFlags_NoTabStop = 1 << 0, // false // Disable keyboard tabbing. This is a "lighter" version of ImGuiItemFlags_NoNav. - ImGuiItemFlags_ButtonRepeat = 1 << 1, // false // Button() will return true multiple times based on io.KeyRepeatDelay and io.KeyRepeatRate settings. - ImGuiItemFlags_Disabled = 1 << 2, // false // Disable interactions but doesn't affect visuals. See BeginDisabled()/EndDisabled(). See github.com/ocornut/imgui/issues/211 - ImGuiItemFlags_NoNav = 1 << 3, // false // Disable any form of focusing (keyboard/gamepad directional navigation and SetKeyboardFocusHere() calls) - ImGuiItemFlags_NoNavDefaultFocus = 1 << 4, // false // Disable item being a candidate for default focus (e.g. used by title bar items) - ImGuiItemFlags_SelectableDontClosePopup = 1 << 5, // false // Disable MenuItem/Selectable() automatically closing their popup window - ImGuiItemFlags_MixedValue = 1 << 6, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) - ImGuiItemFlags_ReadOnly = 1 << 7, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. - ImGuiItemFlags_NoWindowHoverableCheck = 1 << 8, // false // Disable hoverable check in ItemHoverable() - ImGuiItemFlags_AllowOverlap = 1 << 9, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. + ImGuiItemFlags_Disabled = 1 << 10, // false // Disable interactions (DOES NOT affect visuals, see BeginDisabled()/EndDisabled() for full disable feature, and github #211). + ImGuiItemFlags_ReadOnly = 1 << 11, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. + ImGuiItemFlags_MixedValue = 1 << 12, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) + ImGuiItemFlags_NoWindowHoverableCheck = 1 << 13, // false // Disable hoverable check in ItemHoverable() + ImGuiItemFlags_AllowOverlap = 1 << 14, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. // Controlled by widget code - ImGuiItemFlags_Inputable = 1 << 10, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. - ImGuiItemFlags_HasSelectionUserData = 1 << 11, // false // Set by SetNextItemSelectionUserData() + ImGuiItemFlags_Inputable = 1 << 20, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. + ImGuiItemFlags_HasSelectionUserData = 1 << 21, // false // Set by SetNextItemSelectionUserData() + ImGuiItemFlags_IsMultiSelect = 1 << 22, // false // Set by SetNextItemSelectionUserData() + + ImGuiItemFlags_Default_ = ImGuiItemFlags_AutoClosePopups, // Please don't change, use PushItemFlag() instead. + + // Obsolete + //ImGuiItemFlags_SelectableDontClosePopup = !ImGuiItemFlags_AutoClosePopups, // Can't have a redirect as we inverted the behavior }; // Status flags for an already submitted item @@ -836,7 +882,8 @@ enum ImGuiItemStatusFlags_ ImGuiItemStatusFlags_Deactivated = 1 << 6, // Only valid if ImGuiItemStatusFlags_HasDeactivated is set. ImGuiItemStatusFlags_HoveredWindow = 1 << 7, // Override the HoveredWindow test to allow cross-window hover testing. ImGuiItemStatusFlags_Visible = 1 << 8, // [WIP] Set when item is overlapping the current clipping rectangle (Used internally. Please don't use yet: API/system will change as we refactor Itemadd()). - ImGuiItemStatusFlags_HasClipRect = 1 << 9, // g.LastItemData.ClipRect is valid + ImGuiItemStatusFlags_HasClipRect = 1 << 9, // g.LastItemData.ClipRect is valid. + ImGuiItemStatusFlags_HasShortcut = 1 << 10, // g.LastItemData.Shortcut valid. Set by SetNextItemShortcut() -> ItemAdd(). // Additional status + semantic for ImGuiTestEngine #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -863,6 +910,7 @@ enum ImGuiInputTextFlagsPrivate_ ImGuiInputTextFlags_Multiline = 1 << 26, // For internal use by InputTextMultiline() ImGuiInputTextFlags_NoMarkEdited = 1 << 27, // For internal use by functions using InputText() before reformatting data ImGuiInputTextFlags_MergedItem = 1 << 28, // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. + ImGuiInputTextFlags_LocalizeDecimalPoint= 1 << 29, // For internal use by InputScalar() and TempInputScalar() }; // Extend ImGuiButtonFlags_ @@ -882,7 +930,7 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine ImGuiButtonFlags_NoKeyModifiers = 1 << 16, // disable mouse interaction if a key modifier is held ImGuiButtonFlags_NoHoldingActiveId = 1 << 17, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) - ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated (FIXME: this is essentially used everytime an item uses ImGuiItemFlags_NoNav, but because legacy specs don't requires LastItemData to be set ButtonBehavior(), we can't poll g.LastItemData.InFlags) + ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated (FIXME: this is essentially used every time an item uses ImGuiItemFlags_NoNav, but because legacy specs don't requires LastItemData to be set ButtonBehavior(), we can't poll g.LastItemData.InFlags) ImGuiButtonFlags_NoHoveredOnFocus = 1 << 19, // don't report as hovered when nav focus is on this item ImGuiButtonFlags_NoSetKeyOwner = 1 << 20, // don't set key/input owner on the initial click (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) ImGuiButtonFlags_NoTestKeyOwner = 1 << 21, // don't test key/input owner when polling the key (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) @@ -920,8 +968,8 @@ enum ImGuiSelectableFlagsPrivate_ // Extend ImGuiTreeNodeFlags_ enum ImGuiTreeNodeFlagsPrivate_ { - ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 20, - ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 21,// (FIXME-WIP) Turn Down arrow into an Up arrow, but reversed trees (#6517) + ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 28,// FIXME-WIP: Hard-coded for CollapsingHeader() + ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 29,// FIXME-WIP: Turn Down arrow into an Up arrow, but reversed trees (#6517) }; enum ImGuiSeparatorFlags_ @@ -1111,6 +1159,15 @@ struct IMGUI_API ImGuiInputTextState }; +enum ImGuiWindowRefreshFlags_ +{ + ImGuiWindowRefreshFlags_None = 0, + ImGuiWindowRefreshFlags_TryToAvoidRefresh = 1 << 0, // [EXPERIMENTAL] Try to keep existing contents, USER MUST NOT HONOR BEGIN() RETURNING FALSE AND NOT APPEND. + ImGuiWindowRefreshFlags_RefreshOnHover = 1 << 1, // [EXPERIMENTAL] Always refresh on hover + ImGuiWindowRefreshFlags_RefreshOnFocus = 1 << 2, // [EXPERIMENTAL] Always refresh on focus + // Refresh policy/frequency, Load Balancing etc. +}; + enum ImGuiNextWindowDataFlags_ { ImGuiNextWindowDataFlags_None = 0, @@ -1123,6 +1180,7 @@ enum ImGuiNextWindowDataFlags_ ImGuiNextWindowDataFlags_HasBgAlpha = 1 << 6, ImGuiNextWindowDataFlags_HasScroll = 1 << 7, ImGuiNextWindowDataFlags_HasChildFlags = 1 << 8, + ImGuiNextWindowDataFlags_HasRefreshPolicy = 1 << 9, }; // Storage for SetNexWindow** functions @@ -1144,33 +1202,36 @@ struct ImGuiNextWindowData void* SizeCallbackUserData; float BgAlphaVal; // Override background alpha ImVec2 MenuBarOffsetMinVal; // (Always on) This is not exposed publicly, so we don't clear it and it doesn't have a corresponding flag (could we? for consistency?) + ImGuiWindowRefreshFlags RefreshFlagsVal; ImGuiNextWindowData() { memset(this, 0, sizeof(*this)); } inline void ClearFlags() { Flags = ImGuiNextWindowDataFlags_None; } }; -// Multi-Selection item index or identifier when using SetNextItemSelectionUserData()/BeginMultiSelect() -// (Most users are likely to use this store an item INDEX but this may be used to store a POINTER as well.) -typedef ImS64 ImGuiSelectionUserData; - enum ImGuiNextItemDataFlags_ { ImGuiNextItemDataFlags_None = 0, ImGuiNextItemDataFlags_HasWidth = 1 << 0, ImGuiNextItemDataFlags_HasOpen = 1 << 1, ImGuiNextItemDataFlags_HasShortcut = 1 << 2, + ImGuiNextItemDataFlags_HasRefVal = 1 << 3, + ImGuiNextItemDataFlags_HasStorageID = 1 << 4, }; struct ImGuiNextItemData { ImGuiNextItemDataFlags Flags; - ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemFlags_AllowOverlap. + ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemFlags_AllowOverlap and ImGuiItemFlags_HasSelectionUserData. // Non-flags members are NOT cleared by ItemAdd() meaning they are still valid during NavProcessItem() + ImGuiID FocusScopeId; // Set by SetNextItemSelectionUserData() ImGuiSelectionUserData SelectionUserData; // Set by SetNextItemSelectionUserData() (note that NULL/0 is a valid value, we use -1 == ImGuiSelectionUserData_Invalid to mark invalid values) float Width; // Set by SetNextItemWidth() ImGuiKeyChord Shortcut; // Set by SetNextItemShortcut() + ImGuiInputFlags ShortcutFlags; // Set by SetNextItemShortcut() bool OpenVal; // Set by SetNextItemOpen() - ImGuiCond OpenCond : 8; + ImU8 OpenCond; // Set by SetNextItemOpen() + ImGuiDataTypeStorage RefVal; // Not exposed yet, for ImGuiInputTextFlags_ParseEmptyAsRefVal + ImGuiID StorageId; // Set by SetNextItemStorageID() ImGuiNextItemData() { memset(this, 0, sizeof(*this)); SelectionUserData = -1; } inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! @@ -1184,21 +1245,24 @@ struct ImGuiLastItemData ImGuiItemStatusFlags StatusFlags; // See ImGuiItemStatusFlags_ ImRect Rect; // Full rectangle ImRect NavRect; // Navigation scoring rectangle (not displayed) - // Rarely used fields are not explicitly cleared, only valid when the corresponding ImGuiItemStatusFlags is set. - ImRect DisplayRect; // Display rectangle (ONLY VALID IF ImGuiItemStatusFlags_HasDisplayRect is set) - ImRect ClipRect; // Clip rectangle at the time of submitting item (ONLY VALID IF ImGuiItemStatusFlags_HasClipRect is set) + // Rarely used fields are not explicitly cleared, only valid when the corresponding ImGuiItemStatusFlags ar set. + ImRect DisplayRect; // Display rectangle. ONLY VALID IF (StatusFlags & ImGuiItemStatusFlags_HasDisplayRect) is set. + ImRect ClipRect; // Clip rectangle at the time of submitting item. ONLY VALID IF (StatusFlags & ImGuiItemStatusFlags_HasClipRect) is set.. + ImGuiKeyChord Shortcut; // Shortcut at the time of submitting item. ONLY VALID IF (StatusFlags & ImGuiItemStatusFlags_HasShortcut) is set.. ImGuiLastItemData() { memset(this, 0, sizeof(*this)); } }; -// Store data emitted by TreeNode() for usage by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere. -// This is the minimum amount of data that we need to perform the equivalent of NavApplyItemToResult() and which we can't infer in TreePop() -// Only stored when the node is a potential candidate for landing on a Left arrow jump. -struct ImGuiNavTreeNodeData +// Store data emitted by TreeNode() for usage by TreePop() +// - To implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere: store the minimum amount of data +// which we can't infer in TreePop(), to perform the equivalent of NavApplyItemToResult(). +// Only stored when the node is a potential candidate for landing on a Left arrow jump. +struct ImGuiTreeNodeStackData { ImGuiID ID; - ImGuiItemFlags InFlags; - ImRect NavRect; + ImGuiTreeNodeFlags TreeFlags; + ImGuiItemFlags InFlags; // Used for nav landing + ImRect NavRect; // Used for nav landing }; struct IMGUI_API ImGuiStackSizes @@ -1223,7 +1287,8 @@ struct ImGuiWindowStackData { ImGuiWindow* Window; ImGuiLastItemData ParentLastItemDataBackup; - ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting + ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting + bool DisabledOverrideReenable; // Non-child window override disabled flag }; struct ImGuiShrinkWidthItem @@ -1242,40 +1307,6 @@ struct ImGuiPtrOrIndex ImGuiPtrOrIndex(int index) { Ptr = NULL; Index = index; } }; -//----------------------------------------------------------------------------- -// [SECTION] Data types support -//----------------------------------------------------------------------------- - -struct ImGuiDataVarInfo -{ - ImGuiDataType Type; - ImU32 Count; // 1+ - ImU32 Offset; // Offset in parent structure - void* GetVarPtr(void* parent) const { return (void*)((unsigned char*)parent + Offset); } -}; - -struct ImGuiDataTypeTempStorage -{ - ImU8 Data[8]; // Can fit any data up to ImGuiDataType_COUNT -}; - -// Type information associated to one ImGuiDataType. Retrieve with DataTypeGetInfo(). -struct ImGuiDataTypeInfo -{ - size_t Size; // Size in bytes - const char* Name; // Short descriptive name for the type, for debugging - const char* PrintFmt; // Default printf format for the type - const char* ScanFmt; // Default scanf format for the type -}; - -// Extend ImGuiDataType_ -enum ImGuiDataTypePrivate_ -{ - ImGuiDataType_String = ImGuiDataType_COUNT + 1, - ImGuiDataType_Pointer, - ImGuiDataType_ID, -}; - //----------------------------------------------------------------------------- // [SECTION] Popup support //----------------------------------------------------------------------------- @@ -1292,7 +1323,7 @@ struct ImGuiPopupData { ImGuiID PopupId; // Set on OpenPopup() ImGuiWindow* Window; // Resolved on BeginPopup() - may stay unresolved if user never calls OpenPopup() - ImGuiWindow* BackupNavWindow;// Set on OpenPopup(), a NavWindow that will be restored on popup close + ImGuiWindow* RestoreNavWindow;// Set on OpenPopup(), a NavWindow that will be restored on popup close int ParentNavLayer; // Resolved on BeginPopup(). Actually a ImGuiNavLayer type (declared down below), initialized to -1 which is not part of an enum, but serves well-enough as "not any of layers" value int OpenFrameCount; // Set on OpenPopup() ImGuiID OpenParentId; // Set on OpenPopup(), we need this to differentiate multiple menu sets from each others (e.g. inside menu bar vs loose menu items) @@ -1326,8 +1357,8 @@ typedef ImBitArray ImBitAr #define ImGuiKey_NavKeyboardTweakFast ImGuiMod_Shift #define ImGuiKey_NavGamepadTweakSlow ImGuiKey_GamepadL1 #define ImGuiKey_NavGamepadTweakFast ImGuiKey_GamepadR1 -#define ImGuiKey_NavGamepadActivate ImGuiKey_GamepadFaceDown -#define ImGuiKey_NavGamepadCancel ImGuiKey_GamepadFaceRight +#define ImGuiKey_NavGamepadActivate (g.IO.ConfigNavSwapGamepadButtons ? ImGuiKey_GamepadFaceRight : ImGuiKey_GamepadFaceDown) +#define ImGuiKey_NavGamepadCancel (g.IO.ConfigNavSwapGamepadButtons ? ImGuiKey_GamepadFaceDown : ImGuiKey_GamepadFaceRight) #define ImGuiKey_NavGamepadMenu ImGuiKey_GamepadFaceLeft #define ImGuiKey_NavGamepadInput ImGuiKey_GamepadFaceUp @@ -1349,7 +1380,6 @@ enum ImGuiInputSource ImGuiInputSource_Mouse, // Note: may be Mouse or TouchScreen or Pen. See io.MouseSource to distinguish them. ImGuiInputSource_Keyboard, ImGuiInputSource_Gamepad, - ImGuiInputSource_Clipboard, // Currently only used by InputText() ImGuiInputSource_COUNT }; @@ -1383,7 +1413,8 @@ struct ImGuiInputEvent // Input function taking an 'ImGuiID owner_id' argument defaults to (ImGuiKeyOwner_Any == 0) aka don't test ownership, which matches legacy behavior. #define ImGuiKeyOwner_Any ((ImGuiID)0) // Accept key that have an owner, UNLESS a call to SetKeyOwner() explicitly used ImGuiInputFlags_LockThisFrame or ImGuiInputFlags_LockUntilRelease. -#define ImGuiKeyOwner_None ((ImGuiID)-1) // Require key to have no owner. +#define ImGuiKeyOwner_NoOwner ((ImGuiID)-1) // Require key to have no owner. +//#define ImGuiKeyOwner_None ImGuiKeyOwner_NoOwner // We previously called this 'ImGuiKeyOwner_None' but it was inconsistent with our pattern that _None values == 0 and quite dangerous. Also using _NoOwner makes the IsKeyPressed() calls more explicit. typedef ImS16 ImGuiKeyRoutingIndex; @@ -1391,13 +1422,13 @@ typedef ImS16 ImGuiKeyRoutingIndex; struct ImGuiKeyRoutingData { ImGuiKeyRoutingIndex NextEntryIndex; - ImU16 Mods; // Technically we'd only need 4-bits but for simplify we store ImGuiMod_ values which need 16-bits. ImGuiMod_Shortcut is already translated to Ctrl/Super. + ImU16 Mods; // Technically we'd only need 4-bits but for simplify we store ImGuiMod_ values which need 16-bits. ImU8 RoutingCurrScore; // [DEBUG] For debug display ImU8 RoutingNextScore; // Lower is better (0: perfect score) ImGuiID RoutingCurr; ImGuiID RoutingNext; - ImGuiKeyRoutingData() { NextEntryIndex = -1; Mods = 0; RoutingCurrScore = RoutingNextScore = 255; RoutingCurr = RoutingNext = ImGuiKeyOwner_None; } + ImGuiKeyRoutingData() { NextEntryIndex = -1; Mods = 0; RoutingCurrScore = RoutingNextScore = 255; RoutingCurr = RoutingNext = ImGuiKeyOwner_NoOwner; } }; // Routing table: maintain a desired owner for each possible key-chord (key + mods), and setup owner in NewFrame() when mods are matching. @@ -1421,73 +1452,47 @@ struct ImGuiKeyOwnerData bool LockThisFrame; // Reading this key requires explicit owner id (until end of frame). Set by ImGuiInputFlags_LockThisFrame. bool LockUntilRelease; // Reading this key requires explicit owner id (until key is released). Set by ImGuiInputFlags_LockUntilRelease. When this is true LockThisFrame is always true as well. - ImGuiKeyOwnerData() { OwnerCurr = OwnerNext = ImGuiKeyOwner_None; LockThisFrame = LockUntilRelease = false; } + ImGuiKeyOwnerData() { OwnerCurr = OwnerNext = ImGuiKeyOwner_NoOwner; LockThisFrame = LockUntilRelease = false; } }; +// Extend ImGuiInputFlags_ // Flags for extended versions of IsKeyPressed(), IsMouseClicked(), Shortcut(), SetKeyOwner(), SetItemKeyOwner() // Don't mistake with ImGuiInputTextFlags! (which is for ImGui::InputText() function) -enum ImGuiInputFlags_ +enum ImGuiInputFlagsPrivate_ { // Flags for IsKeyPressed(), IsKeyChordPressed(), IsMouseClicked(), Shortcut() - ImGuiInputFlags_None = 0, - - // Repeat mode - ImGuiInputFlags_Repeat = 1 << 0, // Enable repeat. Return true on successive repeats. Default for legacy IsKeyPressed(). NOT Default for legacy IsMouseClicked(). MUST BE == 1. - ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) - ImGuiInputFlags_RepeatRateNavMove = 1 << 2, // Repeat rate: Fast - ImGuiInputFlags_RepeatRateNavTweak = 1 << 3, // Repeat rate: Faster - - // Repeat mode: Specify when repeating key pressed can be interrupted. - // In theory ImGuiInputFlags_RepeatUntilOtherKeyPress may be a desirable default, but it would break too many behavior so everything is opt-in. - ImGuiInputFlags_RepeatUntilRelease = 1 << 4, // Stop repeating when released (default for all functions except Shortcut). This only exists to allow overriding Shortcut() default behavior. - ImGuiInputFlags_RepeatUntilKeyModsChange = 1 << 5, // Stop repeating when released OR if keyboard mods are changed (default for Shortcut) + // - Repeat mode: Repeat rate selection + ImGuiInputFlags_RepeatRateDefault = 1 << 1, // Repeat rate: Regular (default) + ImGuiInputFlags_RepeatRateNavMove = 1 << 2, // Repeat rate: Fast + ImGuiInputFlags_RepeatRateNavTweak = 1 << 3, // Repeat rate: Faster + // - Repeat mode: Specify when repeating key pressed can be interrupted. + // - In theory ImGuiInputFlags_RepeatUntilOtherKeyPress may be a desirable default, but it would break too many behavior so everything is opt-in. + ImGuiInputFlags_RepeatUntilRelease = 1 << 4, // Stop repeating when released (default for all functions except Shortcut). This only exists to allow overriding Shortcut() default behavior. + ImGuiInputFlags_RepeatUntilKeyModsChange = 1 << 5, // Stop repeating when released OR if keyboard mods are changed (default for Shortcut) ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone = 1 << 6, // Stop repeating when released OR if keyboard mods are leaving the None state. Allows going from Mod+Key to Key by releasing Mod. - ImGuiInputFlags_RepeatUntilOtherKeyPress = 1 << 7, // Stop repeating when released OR if any other keyboard key is pressed during the repeat - - // Flags for SetItemKeyOwner() - ImGuiInputFlags_CondHovered = 1 << 8, // Only set if item is hovered (default to both) - ImGuiInputFlags_CondActive = 1 << 9, // Only set if item is active (default to both) - ImGuiInputFlags_CondDefault_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, + ImGuiInputFlags_RepeatUntilOtherKeyPress = 1 << 7, // Stop repeating when released OR if any other keyboard key is pressed during the repeat // Flags for SetKeyOwner(), SetItemKeyOwner() - // Locking is useful to make input-owner-aware code steal keys from non-input-owner-aware code. If all code is input-owner-aware locking would never be necessary. - ImGuiInputFlags_LockThisFrame = 1 << 10, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. - ImGuiInputFlags_LockUntilRelease = 1 << 11, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. - - // Routing policies for Shortcut() + low-level SetShortcutRouting() - // - The general idea is that several callers register interest in a shortcut, and only one owner gets it. - // Parent -> call Shortcut(Ctrl+S) // When Parent is focused, Parent gets the shortcut. - // Child1 -> call Shortcut(Ctrl+S) // When Child1 is focused, Child1 gets the shortcut (Child1 overrides Parent shortcuts) - // Child2 -> no call // When Child2 is focused, Parent gets the shortcut. - // The whole system is order independent, so if Child1 does it calls before Parent results will be identical. - // This is an important property as it facilitate working with foreign code or larger codebase. - // - Visualize registered routes in 'Metrics->Inputs' and submitted routes in 'Debug Log->InputRouting'. - // - When a policy (except for _RouteAlways *) is set, Shortcut() will register itself with SetShortcutRouting(), - // allowing the system to decide where to route the input among other route-aware calls. - // (* Using ImGuiInputFlags_RouteAlways is roughly equivalent to calling IsKeyChordPressed(key)). - // - Shortcut() uses ImGuiInputFlags_RouteFocused by default. Meaning that a Shortcut() call will register - // a route and only succeed when parent window is in the focus-stack and if no-one with a higher priority - // is claiming the same shortcut. - // - You can chain two unrelated windows in the focus stack using SetWindowParentWindowForFocusRoute(). - // - Priorities: GlobalHigh > Focused (when owner is active item) > Global > Focused (when focused window) > GlobalLow. - // - Can select only 1 policy among all available. - ImGuiInputFlags_RouteFocused = 1 << 12, // (Default) Honor focus route: Accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window. - ImGuiInputFlags_RouteGlobalLow = 1 << 13, // Register route globally (lowest priority: unless a focused window or active item registered the route) -> recommended Global priority IF you need a Global priority. - ImGuiInputFlags_RouteGlobal = 1 << 14, // Register route globally (medium priority: unless an active item registered the route, e.g. CTRL+A registered by InputText will take priority over this). - ImGuiInputFlags_RouteGlobalHigh = 1 << 15, // Register route globally (higher priority: unlikely you need to use that: will interfere with every active items, e.g. CTRL+A registered by InputText will be overriden by this) - ImGuiInputFlags_RouteAlways = 1 << 16, // Do not register route, poll keys directly. - // Routing polices: extra options - ImGuiInputFlags_RouteUnlessBgFocused= 1 << 17, // Global routes will not be applied if underlying background/void is focused (== no Dear ImGui windows are focused). Useful for overlay applications. + // - Locking key away from non-input aware code. Locking is useful to make input-owner-aware code steal keys from non-input-owner-aware code. If all code is input-owner-aware locking would never be necessary. + ImGuiInputFlags_LockThisFrame = 1 << 20, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared at end of frame. + ImGuiInputFlags_LockUntilRelease = 1 << 21, // Further accesses to key data will require EXPLICIT owner ID (ImGuiKeyOwner_Any/0 will NOT accepted for polling). Cleared when the key is released or at end of each frame if key is released. + + // - Condition for SetItemKeyOwner() + ImGuiInputFlags_CondHovered = 1 << 22, // Only set if item is hovered (default to both) + ImGuiInputFlags_CondActive = 1 << 23, // Only set if item is active (default to both) + ImGuiInputFlags_CondDefault_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, // [Internal] Mask of which function support which flags ImGuiInputFlags_RepeatRateMask_ = ImGuiInputFlags_RepeatRateDefault | ImGuiInputFlags_RepeatRateNavMove | ImGuiInputFlags_RepeatRateNavTweak, ImGuiInputFlags_RepeatUntilMask_ = ImGuiInputFlags_RepeatUntilRelease | ImGuiInputFlags_RepeatUntilKeyModsChange | ImGuiInputFlags_RepeatUntilKeyModsChangeFromNone | ImGuiInputFlags_RepeatUntilOtherKeyPress, ImGuiInputFlags_RepeatMask_ = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateMask_ | ImGuiInputFlags_RepeatUntilMask_, ImGuiInputFlags_CondMask_ = ImGuiInputFlags_CondHovered | ImGuiInputFlags_CondActive, - ImGuiInputFlags_RouteMask_ = ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteGlobalLow | ImGuiInputFlags_RouteGlobalHigh, // _Always not part of this! + ImGuiInputFlags_RouteTypeMask_ = ImGuiInputFlags_RouteActive | ImGuiInputFlags_RouteFocused | ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteAlways, + ImGuiInputFlags_RouteOptionsMask_ = ImGuiInputFlags_RouteOverFocused | ImGuiInputFlags_RouteOverActive | ImGuiInputFlags_RouteUnlessBgFocused | ImGuiInputFlags_RouteFromRootWindow, ImGuiInputFlags_SupportedByIsKeyPressed = ImGuiInputFlags_RepeatMask_, ImGuiInputFlags_SupportedByIsMouseClicked = ImGuiInputFlags_Repeat, - ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteMask_ | ImGuiInputFlags_RouteAlways | ImGuiInputFlags_RouteUnlessBgFocused, + ImGuiInputFlags_SupportedByShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteTypeMask_ | ImGuiInputFlags_RouteOptionsMask_, + ImGuiInputFlags_SupportedBySetNextItemShortcut = ImGuiInputFlags_RepeatMask_ | ImGuiInputFlags_RouteTypeMask_ | ImGuiInputFlags_RouteOptionsMask_ | ImGuiInputFlags_Tooltip, ImGuiInputFlags_SupportedBySetKeyOwner = ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease, ImGuiInputFlags_SupportedBySetItemKeyOwner = ImGuiInputFlags_SupportedBySetKeyOwner | ImGuiInputFlags_CondMask_, }; @@ -1578,6 +1583,7 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_Activate = 1 << 12, // Activate/select target item. ImGuiNavMoveFlags_NoSelect = 1 << 13, // Don't trigger selection by not setting g.NavJustMovedTo ImGuiNavMoveFlags_NoSetNavHighlight = 1 << 14, // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_NoClearActiveId = 1 << 15, // (Experimental) Do not clear active id when applying move result }; enum ImGuiNavLayer @@ -1587,6 +1593,7 @@ enum ImGuiNavLayer ImGuiNavLayer_COUNT }; +// Storage for navigation query/results struct ImGuiNavItemData { ImGuiWindow* Window; // Init,Move // Best candidate window (result->ItemWindow->RootWindowForNav == request->Window) @@ -1594,15 +1601,16 @@ struct ImGuiNavItemData ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space ImGuiItemFlags InFlags; // ????,Move // Best candidate item flags - ImGuiSelectionUserData SelectionUserData;//I+Mov // Best candidate SetNextItemSelectionData() value. float DistBox; // Move // Best candidate box distance to current NavId float DistCenter; // Move // Best candidate center distance to current NavId float DistAxial; // Move // Best candidate axial distance to current NavId + ImGuiSelectionUserData SelectionUserData;//I+Mov // Best candidate SetNextItemSelectionUserData() value. Valid if (InFlags & ImGuiItemFlags_HasSelectionUserData) ImGuiNavItemData() { Clear(); } void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; SelectionUserData = -1; DistBox = DistCenter = DistAxial = FLT_MAX; } }; +// Storage for PushFocusScope() struct ImGuiFocusScopeData { ImGuiID ID; @@ -1702,6 +1710,34 @@ struct ImGuiOldColumns ImGuiOldColumns() { memset(this, 0, sizeof(*this)); } }; +//----------------------------------------------------------------------------- +// [SECTION] Box-select support +//----------------------------------------------------------------------------- + +struct ImGuiBoxSelectState +{ + // Active box-selection data (persistent, 1 active at a time) + ImGuiID ID; + bool IsActive; + bool IsStarting; + bool IsStartedFromVoid; // Starting click was not from an item. + bool IsStartedSetNavIdOnce; + bool RequestClear; + ImGuiKeyChord KeyMods : 16; // Latched key-mods for box-select logic. + ImVec2 StartPosRel; // Start position in window-contents relative space (to support scrolling) + ImVec2 EndPosRel; // End position in window-contents relative space + ImVec2 ScrollAccum; // Scrolling accumulator (to behave at high-frame spaces) + ImGuiWindow* Window; + + // Temporary/Transient data + bool UnclipMode; // (Temp/Transient, here in hot area). Set/cleared by the BeginMultiSelect()/EndMultiSelect() owning active box-select. + ImRect UnclipRect; // Rectangle where ItemAdd() clipping may be temporarily disabled. Need support by multi-select supporting widgets. + ImRect BoxSelectRectPrev; // Selection rectangle in absolute coordinates (derived every frame from BoxSelectStartPosRel and MousePos) + ImRect BoxSelectRectCurr; + + ImGuiBoxSelectState() { memset(this, 0, sizeof(*this)); } +}; + //----------------------------------------------------------------------------- // [SECTION] Multi-select support //----------------------------------------------------------------------------- @@ -1709,9 +1745,45 @@ struct ImGuiOldColumns // We always assume that -1 is an invalid value (which works for indices and pointers) #define ImGuiSelectionUserData_Invalid ((ImGuiSelectionUserData)-1) -#ifdef IMGUI_HAS_MULTI_SELECT -// -#endif // #ifdef IMGUI_HAS_MULTI_SELECT +// Temporary storage for multi-select +struct IMGUI_API ImGuiMultiSelectTempData +{ + ImGuiMultiSelectIO IO; // MUST BE FIRST FIELD. Requests are set and returned by BeginMultiSelect()/EndMultiSelect() + written to by user during the loop. + ImGuiMultiSelectState* Storage; + ImGuiID FocusScopeId; // Copied from g.CurrentFocusScopeId (unless another selection scope was pushed manually) + ImGuiMultiSelectFlags Flags; + ImVec2 ScopeRectMin; + ImVec2 BackupCursorMaxPos; + ImGuiSelectionUserData LastSubmittedItem; // Copy of last submitted item data, used to merge output ranges. + ImGuiID BoxSelectId; + ImGuiKeyChord KeyMods; + ImS8 LoopRequestSetAll; // -1: no operation, 0: clear all, 1: select all. + bool IsEndIO; // Set when switching IO from BeginMultiSelect() to EndMultiSelect() state. + bool IsFocused; // Set if currently focusing the selection scope (any item of the selection). May be used if you have custom shortcut associated to selection. + bool IsKeyboardSetRange; // Set by BeginMultiSelect() when using Shift+Navigation. Because scrolling may be affected we can't afford a frame of lag with Shift+Navigation. + bool NavIdPassedBy; + bool RangeSrcPassedBy; // Set by the item that matches RangeSrcItem. + bool RangeDstPassedBy; // Set by the item that matches NavJustMovedToId when IsSetRange is set. + + ImGuiMultiSelectTempData() { Clear(); } + void Clear() { size_t io_sz = sizeof(IO); ClearIO(); memset((void*)(&IO + 1), 0, sizeof(*this) - io_sz); } // Zero-clear except IO as we preserve IO.Requests[] buffer allocation. + void ClearIO() { IO.Requests.resize(0); IO.RangeSrcItem = IO.NavIdItem = ImGuiSelectionUserData_Invalid; IO.NavIdSelected = IO.RangeSrcReset = false; } +}; + +// Persistent storage for multi-select (as long as selection is alive) +struct IMGUI_API ImGuiMultiSelectState +{ + ImGuiWindow* Window; + ImGuiID ID; + int LastFrameActive; // Last used frame-count, for GC. + int LastSelectionSize; // Set by BeginMultiSelect() based on optional info provided by user. May be -1 if unknown. + ImS8 RangeSelected; // -1 (don't have) or true/false + ImS8 NavIdSelected; // -1 (don't have) or true/false + ImGuiSelectionUserData RangeSrcItem; // + ImGuiSelectionUserData NavIdItem; // SetNextItemSelectionUserData() value for NavId (if part of submitted items) + + ImGuiMultiSelectState() { Window = NULL; ID = 0; LastFrameActive = LastSelectionSize = 0; RangeSelected = NavIdSelected = -1; RangeSrcItem = NavIdItem = ImGuiSelectionUserData_Invalid; } +}; //----------------------------------------------------------------------------- // [SECTION] Docking support @@ -1803,6 +1875,7 @@ enum ImGuiLocKey : int ImGuiLocKey_WindowingMainMenuBar, ImGuiLocKey_WindowingPopup, ImGuiLocKey_WindowingUntitled, + ImGuiLocKey_CopyLink, ImGuiLocKey_COUNT }; @@ -1924,6 +1997,8 @@ struct ImGuiContext ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. float FontBaseSize; // (Shortcut) == IO.FontGlobalScale * Font->Scale * Font->FontSize. Base text height. + float FontScale; // == FontSize / Font->FontSize + float CurrentDpiScale; // Current window/viewport DpiScale ImDrawListSharedData DrawListSharedData; double Time; int FrameCount; @@ -1935,6 +2010,7 @@ struct ImGuiContext bool GcCompactAll; // Request full GC bool TestEngineHookItems; // Will call test engine hooks: ImGuiTestEngineHook_ItemAdd(), ImGuiTestEngineHook_ItemInfo(), ImGuiTestEngineHook_Log() void* TestEngine; // Test engine user data + char ContextName[16]; // Storage for a context name (to facilitate debugging multi-context setups) // Inputs ImVector InputEventsQueue; // Input events which will be trickled/written into IO structure. @@ -1949,11 +2025,12 @@ struct ImGuiContext ImVector CurrentWindowStack; ImGuiStorage WindowsById; // Map window's ImGuiID to ImGuiWindow* int WindowsActiveCount; // Number of unique windows submitted by frame - ImVec2 WindowsHoverPadding; // Padding around resizable windows for which hovering on counts as hovering the window == ImMax(style.TouchExtraPadding, WINDOWS_HOVER_PADDING) + ImVec2 WindowsHoverPadding; // Padding around resizable windows for which hovering on counts as hovering the window == ImMax(style.TouchExtraPadding, WINDOWS_HOVER_PADDING). ImGuiID DebugBreakInWindow; // Set to break in Begin() call. ImGuiWindow* CurrentWindow; // Window being drawn into ImGuiWindow* HoveredWindow; // Window the mouse is hovering. Will typically catch mouse inputs. ImGuiWindow* HoveredWindowUnderMovingWindow; // Hovered window ignoring MovingWindow. Only set if MovingWindow is set. + ImGuiWindow* HoveredWindowBeforeClear; // Window the mouse is hovering. Filled even with _NoMouse. This is currently useful for multi-context compositors. ImGuiWindow* MovingWindow; // Track the window we clicked on (in order to preserve focus). The actual window that is moved is generally MovingWindow->RootWindow. ImGuiWindow* WheelingWindow; // Track the window we started mouse-wheeling on. Until a timer elapse or mouse has moved, generally keep scrolling the same window even if during the course of scrolling the mouse ends up hovering a child window. ImVec2 WheelingWindowRefMousePos; @@ -1967,10 +2044,11 @@ struct ImGuiContext ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by ID Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; - bool HoveredIdAllowOverlap; - bool HoveredIdDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0. float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active + bool HoveredIdAllowOverlap; + bool HoveredIdIsDisabled; // At least one widget passed the rect test, but has been discarded by disabled flag or popup inhibit. May be true even if HoveredId == 0. + bool ItemUnclipByLog; // Disable ItemAdd() clipping, essentially a memory-locality friendly copy of LogEnabled ImGuiID ActiveId; // Active widget ImGuiID ActiveIdIsAlive; // Active widget has been seen this frame (we can't use a bool as the ActiveId may change within the frame) float ActiveIdTimer; @@ -1992,9 +2070,9 @@ struct ImGuiContext ImGuiID LastActiveId; // Store the last non-zero ActiveId, useful for animation. float LastActiveIdTimer; // Store the last non-zero ActiveId timer since the beginning of activation, useful for animation. - // [EXPERIMENTAL] Key/Input Ownership + Shortcut Routing system + // Key/Input Ownership + Shortcut Routing system // - The idea is that instead of "eating" a given key, we can link to an owner. - // - Input query can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_None (== -1) or a custom ID. + // - Input query can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_NoOwner (== -1) or a custom ID. // - Routing is requested ahead of time for a given chord (Key + Mods) and granted in NewFrame(). double LastKeyModsChangeTime; // Record the last time key mods changed (affect repeat delay when using shortcut logic) double LastKeyModsChangeFromNoneTime; // Record the last time key mods changed away from being 0 (affect repeat delay when using shortcut logic) @@ -2003,11 +2081,9 @@ struct ImGuiContext ImGuiKeyOwnerData KeysOwnerData[ImGuiKey_NamedKey_COUNT]; ImGuiKeyRoutingTable KeysRoutingTable; ImU32 ActiveIdUsingNavDirMask; // Active widget will want to read those nav move requests (e.g. can activate a button and move away from it) - bool ActiveIdUsingAllKeyboardKeys; // Active widget will want to read all keyboard keys inputs. (FIXME: This is a shortcut for not taking ownership of 100+ keys but perhaps best to not have the inconsistency) + bool ActiveIdUsingAllKeyboardKeys; // Active widget will want to read all keyboard keys inputs. (this is a shortcut for not taking ownership of 100+ keys, frequently used by drag operations) ImGuiKeyChord DebugBreakInShortcutRouting; // Set to break in SetShortcutRouting()/Shortcut() calls. -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - ImU32 ActiveIdUsingNavInputMask; // If you used this. Since (IMGUI_VERSION_NUM >= 18804) : 'g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel);' becomes 'SetKeyOwner(ImGuiKey_Escape, g.ActiveId) and/or SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId);' -#endif + //ImU32 ActiveIdUsingNavInputMask; // [OBSOLETE] Since (IMGUI_VERSION_NUM >= 18804) : 'g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel);' becomes --> 'SetKeyOwner(ImGuiKey_Escape, g.ActiveId) and/or SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId);' // Next window/item data ImGuiID CurrentFocusScopeId; // Value for currently appending items == g.FocusScopeStack.back(). Not to be mistaken with g.NavFocusScopeId. @@ -2028,7 +2104,7 @@ struct ImGuiContext ImVector GroupStack; // Stack for BeginGroup()/EndGroup() - not inherited by Begin() ImVector OpenPopupStack; // Which popups are open (persistent) ImVector BeginPopupStack; // Which level of BeginPopup() we are in (reset every frame) - ImVector NavTreeNodeStack; // Stack for TreeNode() when a NavLeft requested is emitted. + ImVectorTreeNodeStack; // Stack for TreeNode() // Viewports ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. @@ -2037,20 +2113,17 @@ struct ImGuiContext ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' ImGuiID NavId; // Focused item for navigation ImGuiID NavFocusScopeId; // Focused focus scope (e.g. selection code often wants to "clear other items" when landing on an item of the same scope) - ImVector NavFocusRoute; // Reversed copy focus scope stack for NavId (should contains NavFocusScopeId). This essentially follow the window->ParentWindowForFocusRoute chain. + ImGuiNavLayer NavLayer; // Focused layer (main scrolling layer, or menu/title bar layer) ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() ImGuiID NavActivateDownId; // ~~ IsKeyDown(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyDown(ImGuiKey_NavGamepadActivate) ? NavId : 0 ImGuiID NavActivatePressedId; // ~~ IsKeyPressed(ImGuiKey_Space) || IsKeyPressed(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate) ? NavId : 0 (no repeat) ImGuiActivateFlags NavActivateFlags; + ImVector NavFocusRoute; // Reversed copy focus scope stack for NavId (should contains NavFocusScopeId). This essentially follow the window->ParentWindowForFocusRoute chain. ImGuiID NavHighlightActivatedId; float NavHighlightActivatedTimer; - ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). - ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). - ImGuiKeyChord NavJustMovedToKeyMods; ImGuiID NavNextActivateId; // Set by ActivateItem(), queued until next frame. ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS CAN ONLY BE ImGuiInputSource_Keyboard or ImGuiInputSource_Mouse - ImGuiNavLayer NavLayer; // Layer we are navigating on. For now the system is hard-coded for 0=main contents and 1=menu/title bar, may expose layers later. ImGuiSelectionUserData NavLastValidSelectionUserData; // Last valid data passed to SetNextItemSelectionUser(), or -1. For current window. Not reset when focusing an item that doesn't have selection data. bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) @@ -2081,9 +2154,17 @@ struct ImGuiContext ImGuiNavItemData NavMoveResultOther; // Best move request candidate within NavWindow's flattened hierarchy (when using ImGuiWindowFlags_NavFlattened flag) ImGuiNavItemData NavTabbingResultFirst; // First tabbing request candidate within NavWindow and flattened hierarchy + // Navigation: record of last move request + ImGuiID NavJustMovedFromFocusScopeId; // Just navigated from this focus scope id (result of a successfully MoveRequest). + ImGuiID NavJustMovedToId; // Just navigated to this id (result of a successfully MoveRequest). + ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). + ImGuiKeyChord NavJustMovedToKeyMods; + bool NavJustMovedToIsTabbing; // Copy of ImGuiNavMoveFlags_IsTabbing. Maybe we should store whole flags. + bool NavJustMovedToHasSelectionData; // Copy of move result's InFlags & ImGuiItemFlags_HasSelectionUserData). Maybe we should just store ImGuiNavItemData. + // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) - ImGuiKeyChord ConfigNavWindowingKeyNext; // = ImGuiMod_Ctrl | ImGuiKey_Tab, for reconfiguration (see #4828) - ImGuiKeyChord ConfigNavWindowingKeyPrev; // = ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab + ImGuiKeyChord ConfigNavWindowingKeyNext; // = ImGuiMod_Ctrl | ImGuiKey_Tab (or ImGuiMod_Super | ImGuiKey_Tab on OS X). For reconfiguration (see #4828) + ImGuiKeyChord ConfigNavWindowingKeyPrev; // = ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab (or ImGuiMod_Super | ImGuiMod_Shift | ImGuiKey_Tab on OS X) ImGuiWindow* NavWindowingTarget; // Target window when doing CTRL+Tab (or Pad Menu + FocusPrev/Next), this window is temporarily displayed top-most! ImGuiWindow* NavWindowingTargetAnim; // Record of last valid NavWindowingTarget until DimBgRatio and NavWindowingHighlightAlpha becomes 0.0f, so the fade-out can stay on it. ImGuiWindow* NavWindowingListWindow; // Internal window actually listing the CTRL+Tab contents @@ -2136,6 +2217,13 @@ struct ImGuiContext ImVector CurrentTabBarStack; ImVector ShrinkWidthBuffer; + // Multi-Select state + ImGuiBoxSelectState BoxSelectState; + ImGuiMultiSelectTempData* CurrentMultiSelect; + int MultiSelectTempDataStacked; // Temporary multi-select data size (because we leave previous instances undestructed, we generally don't use MultiSelectTempData.Size) + ImVector MultiSelectTempData; + ImPool MultiSelectStorage; + // Hover Delay system ImGuiID HoverItemDelayId; ImGuiID HoverItemDelayIdPreviousFrame; @@ -2154,6 +2242,7 @@ struct ImGuiContext ImGuiInputTextDeactivatedState InputTextDeactivatedState; ImFont InputTextPasswordFont; ImGuiID TempInputId; // Temporary text input when CTRL+clicking on a slider, etc. + ImGuiDataTypeStorage DataTypeZeroValue; // 0 for all data types int BeginMenuDepth; int BeginComboDepth; ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets @@ -2166,13 +2255,14 @@ struct ImGuiContext ImGuiComboPreviewData ComboPreviewData; ImRect WindowResizeBorderExpectedRect; // Expected border rect, switch to relative edit if moving bool WindowResizeRelativeMode; + short ScrollbarSeekMode; // 0: relative, -1/+1: prev/next page. + float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? float SliderGrabClickOffset; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? bool DragCurrentAccumDirty; float DragCurrentAccum; // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio - float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() short DisabledStackSize; short LockMarkEdited; @@ -2183,7 +2273,7 @@ struct ImGuiContext // Platform support ImGuiPlatformImeData PlatformImeData; // Data updated by current frame - ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data (when changing we will call io.SetPlatformImeDataFn + ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data. When changed we call the io.PlatformSetImeDataFn() handler. // Settings bool SettingsLoaded; @@ -2250,7 +2340,7 @@ struct ImGuiContext Initialized = false; FontAtlasOwnedByContext = shared_font_atlas ? false : true; Font = NULL; - FontSize = FontBaseSize = 0.0f; + FontSize = FontBaseSize = FontScale = CurrentDpiScale = 0.0f; IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); Time = 0.0f; FrameCount = 0; @@ -2259,6 +2349,7 @@ struct ImGuiContext GcCompactAll = false; TestEngineHookItems = false; TestEngine = NULL; + memset(ContextName, 0, sizeof(ContextName)); InputEventsNextMouseSource = ImGuiMouseSource_Mouse; InputEventsNextEventId = 1; @@ -2267,6 +2358,7 @@ struct ImGuiContext CurrentWindow = NULL; HoveredWindow = NULL; HoveredWindowUnderMovingWindow = NULL; + HoveredWindowBeforeClear = NULL; MovingWindow = NULL; WheelingWindow = NULL; WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1; @@ -2275,8 +2367,9 @@ struct ImGuiContext DebugHookIdInfo = 0; HoveredId = HoveredIdPreviousFrame = 0; HoveredIdAllowOverlap = false; - HoveredIdDisabled = false; + HoveredIdIsDisabled = false; HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; + ItemUnclipByLog = false; ActiveId = 0; ActiveIdIsAlive = 0; ActiveIdTimer = 0.0f; @@ -2302,9 +2395,6 @@ struct ImGuiContext ActiveIdUsingNavDirMask = 0x00; ActiveIdUsingAllKeyboardKeys = false; -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - ActiveIdUsingNavInputMask = 0x00; -#endif CurrentFocusScopeId = 0; CurrentItemFlags = ImGuiItemFlags_None; @@ -2312,18 +2402,18 @@ struct ImGuiContext NavWindow = NULL; NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = 0; - NavJustMovedToId = NavJustMovedToFocusScopeId = NavNextActivateId = 0; + NavLayer = ImGuiNavLayer_Main; + NavNextActivateId = 0; NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; NavHighlightActivatedId = 0; NavHighlightActivatedTimer = 0.0f; - NavJustMovedToKeyMods = ImGuiMod_None; NavInputSource = ImGuiInputSource_Keyboard; - NavLayer = ImGuiNavLayer_Main; NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; NavIdIsAlive = false; NavMousePosDirty = false; NavDisableHighlight = true; NavDisableMouseHover = false; + NavAnyRequest = false; NavInitRequest = false; NavInitRequestFromMove = false; @@ -2338,8 +2428,15 @@ struct ImGuiContext NavTabbingDir = 0; NavTabbingCounter = 0; - ConfigNavWindowingKeyNext = ImGuiMod_Ctrl | ImGuiKey_Tab; - ConfigNavWindowingKeyPrev = ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab; + NavJustMovedFromFocusScopeId = NavJustMovedToId = NavJustMovedToFocusScopeId = 0; + NavJustMovedToKeyMods = ImGuiMod_None; + NavJustMovedToIsTabbing = false; + NavJustMovedToHasSelectionData = false; + + // All platforms use Ctrl+Tab but Ctrl<>Super are swapped on Mac... + // FIXME: Because this value is stored, it annoyingly interfere with toggling io.ConfigMacOSXBehaviors updating this.. + ConfigNavWindowingKeyNext = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiKey_Tab); + ConfigNavWindowingKeyPrev = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiMod_Shift | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab); NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; NavWindowingToggleLayer = false; @@ -2364,6 +2461,8 @@ struct ImGuiContext CurrentTable = NULL; TablesTempDataStacked = 0; CurrentTabBar = NULL; + CurrentMultiSelect = NULL; + MultiSelectTempDataStacked = 0; HoverItemDelayId = HoverItemDelayIdPreviousFrame = HoverItemUnlockedStationaryId = HoverWindowUnlockedStationaryId = 0; HoverItemDelayTimer = HoverItemDelayClearTimer = 0.0f; @@ -2372,19 +2471,21 @@ struct ImGuiContext MouseStationaryTimer = 0.0f; TempInputId = 0; + memset(&DataTypeZeroValue, 0, sizeof(DataTypeZeroValue)); BeginMenuDepth = BeginComboDepth = 0; ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; ColorEditCurrentID = ColorEditSavedID = 0; ColorEditSavedHue = ColorEditSavedSat = 0.0f; ColorEditSavedColor = 0; WindowResizeRelativeMode = false; + ScrollbarSeekMode = 0; + ScrollbarClickDeltaToGrabCenter = 0.0f; SliderGrabClickOffset = 0.0f; SliderCurrentAccum = 0.0f; SliderCurrentAccumDirty = false; DragCurrentAccumDirty = false; DragCurrentAccum = 0.0f; DragSpeedDefaultRatio = 1.0f / 100.0f; - ScrollbarClickDeltaToGrabCenter = 0.0f; DisabledAlphaBackup = 0.0f; DisabledStackSize = 0; LockMarkEdited = 0; @@ -2474,7 +2575,7 @@ struct IMGUI_API ImGuiWindowTempData ImVec2 MenuBarOffset; // MenuBarOffset.x is sort of equivalent of a per-layer CursorPos.x, saved/restored as we switch to the menu bar. The only situation when MenuBarOffset.y is > 0 if when (SafeAreaPadding.y > FramePadding.y), often used on TVs. ImGuiMenuColumns MenuColumns; // Simplified columns storage for menu items measurement int TreeDepth; // Current tree depth. - ImU32 TreeJumpToParentOnPopMask; // Store a copy of !g.NavIdIsAlive for TreeDepth 0..31.. Could be turned into a ImU64 if necessary. + ImU32 TreeHasStackDataDepthMask; // Store whether given depth has ImGuiTreeNodeStackData data. Could be turned into a ImU64 if necessary. ImVector ChildWindows; ImGuiStorage* StateStorage; // Current persistent per-window storage (store e.g. tree node open/close state) ImGuiOldColumns* CurrentColumns; // Current columns set @@ -2509,12 +2610,14 @@ struct IMGUI_API ImGuiWindow ImVec2 WindowPadding; // Window padding at the time of Begin(). float WindowRounding; // Window rounding at the time of Begin(). May be clamped lower to avoid rendering artifacts with title bar, menu bar etc. float WindowBorderSize; // Window border size at the time of Begin(). + float TitleBarHeight, MenuBarHeight; float DecoOuterSizeX1, DecoOuterSizeY1; // Left/Up offsets. Sum of non-scrolling outer decorations (X1 generally == 0.0f. Y1 generally = TitleBarHeight + MenuBarHeight). Locked during Begin(). float DecoOuterSizeX2, DecoOuterSizeY2; // Right/Down offsets (X2 generally == ScrollbarSize.x, Y2 == ScrollbarSizes.y). float DecoInnerSizeX1, DecoInnerSizeY1; // Applied AFTER/OVER InnerRect. Specialized for Tables as they use specialized form of clipping and frozen rows/columns are inside InnerRect (and not part of regular decoration sizes). int NameBufLen; // Size of buffer storing Name. May be larger than strlen(Name)! ImGuiID MoveId; // == window->GetID("#MOVE") ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window) + ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) ImVec2 Scroll; ImVec2 ScrollMax; ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change) @@ -2528,6 +2631,7 @@ struct IMGUI_API ImGuiWindow bool Collapsed; // Set when collapsing window to become only title-bar bool WantCollapseToggle; bool SkipItems; // Set when items can safely be all clipped (e.g. window not visible or collapsed) + bool SkipRefresh; // [EXPERIMENTAL] Reuse previous frame drawn contents, Begin() returns false. bool Appearing; // Set during the frame where the window is appearing (or re-appearing) bool Hidden; // Do not display (== HiddenFrames*** > 0) bool IsFallbackWindow; // Set on the "Debug##Default" window. @@ -2540,7 +2644,6 @@ struct IMGUI_API ImGuiWindow short BeginOrderWithinParent; // Begin() order within immediate parent window, if we are a child window. Otherwise 0. short BeginOrderWithinContext; // Begin() order within entire imgui context. This is mostly used for debugging submission order related issues. short FocusOrder; // Order within WindowsFocusOrder[], altered when windows are focused. - ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling) ImS8 AutoFitFramesX, AutoFitFramesY; bool AutoFitOnlyGrows; ImGuiDir AutoPosLastDirection; @@ -2609,10 +2712,8 @@ struct IMGUI_API ImGuiWindow // We don't use g.FontSize because the window may be != g.CurrentWindow. ImRect Rect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } float CalcFontSize() const { ImGuiContext& g = *Ctx; float scale = g.FontBaseSize * FontWindowScale; if (ParentWindow) scale *= ParentWindow->FontWindowScale; return scale; } - float TitleBarHeight() const { ImGuiContext& g = *Ctx; return (Flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : CalcFontSize() + g.Style.FramePadding.y * 2.0f; } - ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight())); } - float MenuBarHeight() const { ImGuiContext& g = *Ctx; return (Flags & ImGuiWindowFlags_MenuBar) ? DC.MenuBarOffset.y + CalcFontSize() + g.Style.FramePadding.y * 2.0f : 0.0f; } - ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight(); return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight()); } + ImRect TitleBarRect() const { return ImRect(Pos, ImVec2(Pos.x + SizeFull.x, Pos.y + TitleBarHeight)); } + ImRect MenuBarRect() const { float y1 = Pos.y + TitleBarHeight; return ImRect(Pos.x, y1, Pos.x + SizeFull.x, y1 + MenuBarHeight); } }; //----------------------------------------------------------------------------- @@ -2767,13 +2868,24 @@ struct ImGuiTableColumn }; // Transient cell data stored per row. -// sizeof() ~ 6 +// sizeof() ~ 6 bytes struct ImGuiTableCellData { ImU32 BgColor; // Actual color ImGuiTableColumnIdx Column; // Column number }; +// Parameters for TableAngledHeadersRowEx() +// This may end up being refactored for more general purpose. +// sizeof() ~ 12 bytes +struct ImGuiTableHeaderData +{ + ImGuiTableColumnIdx Index; // Column index + ImU32 TextColor; + ImU32 BgColor0; + ImU32 BgColor1; +}; + // Per-instance data that needs preserving across frames (seemingly most others do not need to be preserved aside from debug needs. Does that means they could be moved to ImGuiTableTempData?) // sizeof() ~ 24 bytes struct ImGuiTableInstanceData @@ -2788,8 +2900,7 @@ struct ImGuiTableInstanceData ImGuiTableInstanceData() { TableInstanceID = 0; LastOuterHeight = LastTopHeadersRowHeight = LastFrozenHeight = 0.0f; HoveredRowLast = HoveredRowNext = -1; } }; -// FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs, incoming RowData -// sizeof() ~ 580 bytes + heap allocs described in TableBeginInitMemory() +// sizeof() ~ 592 bytes + heap allocs described in TableBeginInitMemory() struct IMGUI_API ImGuiTable { ImGuiID ID; @@ -2859,7 +2970,7 @@ struct IMGUI_API ImGuiTable ImGuiTableSortSpecs SortSpecs; // Public facing sorts specs, this is what we return in TableGetSortSpecs() ImGuiTableColumnIdx SortSpecsCount; ImGuiTableColumnIdx ColumnsEnabledCount; // Number of enabled columns (<= ColumnsCount) - ImGuiTableColumnIdx ColumnsEnabledFixedCount; // Number of enabled columns (<= ColumnsCount) + ImGuiTableColumnIdx ColumnsEnabledFixedCount; // Number of enabled columns using fixed width (<= ColumnsCount) ImGuiTableColumnIdx DeclColumnsCount; // Count calls to TableSetupColumn() ImGuiTableColumnIdx AngledHeadersCount; // Count columns with angled headers ImGuiTableColumnIdx HoveredColumnBody; // Index of column whose visible region is being hovered. Important: == ColumnsCount when hovering empty region after the right-most column! @@ -2912,12 +3023,14 @@ struct IMGUI_API ImGuiTable // Transient data that are only needed between BeginTable() and EndTable(), those buffers are shared (1 per level of stacked table). // - Accessing those requires chasing an extra pointer so for very frequently used data we leave them in the main table structure. // - We also leave out of this structure data that tend to be particularly useful for debugging/metrics. -// sizeof() ~ 120 bytes. +// FIXME-TABLE: more transient data could be stored in a stacked ImGuiTableTempData: e.g. SortSpecs. +// sizeof() ~ 136 bytes. struct IMGUI_API ImGuiTableTempData { int TableIndex; // Index in g.Tables.Buf[] pool float LastTimeActive; // Last timestamp this structure was used float AngledHeadersExtraWidth; // Used in EndTable() + ImVector AngledHeadersRequests; // Used in TableAngledHeadersRow() ImVec2 UserOuterSize; // outer_size.x passed to BeginTable() ImDrawListSplitter DrawSplitter; @@ -2981,7 +3094,7 @@ namespace ImGui { // Windows // We should always have a CurrentWindow in the stack (there is an implicit "Debug" window) - // If this ever crash because g.CurrentWindow is NULL it means that either + // If this ever crashes because g.CurrentWindow is NULL, it means that either: // - ImGui::NewFrame() has never been called, which is illegal. // - You are calling ImGui functions after ImGui::EndFrame()/ImGui::Render() and before the next ImGui::NewFrame(), which is also illegal. inline ImGuiWindow* GetCurrentWindowRead() { ImGuiContext& g = *GImGui; return g.CurrentWindow; } @@ -2989,6 +3102,7 @@ namespace ImGui IMGUI_API ImGuiWindow* FindWindowByID(ImGuiID id); IMGUI_API ImGuiWindow* FindWindowByName(const char* name); IMGUI_API void UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags flags, ImGuiWindow* parent_window); + IMGUI_API void UpdateWindowSkipRefresh(ImGuiWindow* window); IMGUI_API ImVec2 CalcWindowNextAutoFitSize(ImGuiWindow* window); IMGUI_API bool IsWindowChildOf(ImGuiWindow* window, ImGuiWindow* potential_parent, bool popup_hierarchy); IMGUI_API bool IsWindowWithinBeginStackOf(ImGuiWindow* window, ImGuiWindow* potential_parent); @@ -3003,6 +3117,7 @@ namespace ImGui inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } + inline ImVec2 WindowPosAbsToRel(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x - off.x, p.y - off.y); } // Windows: Display Order and Focus Order IMGUI_API void FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags = 0); @@ -3014,6 +3129,9 @@ namespace ImGui IMGUI_API int FindWindowDisplayIndex(ImGuiWindow* window); IMGUI_API ImGuiWindow* FindBottomMostVisibleWindowWithinBeginStack(ImGuiWindow* window); + // Windows: Idle, Refresh Policies [EXPERIMENTAL] + IMGUI_API void SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags flags); + // Fonts, drawing IMGUI_API void SetCurrentFont(ImFont* font); inline ImFont* GetDefaultFont() { ImGuiContext& g = *GImGui; return g.IO.FontDefault ? g.IO.FontDefault : g.IO.Fonts->Fonts[0]; } @@ -3029,6 +3147,7 @@ namespace ImGui // NewFrame IMGUI_API void UpdateInputEvents(bool trickle_fast_inputs); IMGUI_API void UpdateHoveredWindowAndCaptureFlags(); + IMGUI_API void FindHoveredWindowEx(const ImVec2& pos, bool find_first_and_in_any_viewport, ImGuiWindow** out_hovered_window, ImGuiWindow** out_hovered_window_under_moving_window); IMGUI_API void StartMouseMovingWindow(ImGuiWindow* window); IMGUI_API void UpdateMouseMovingWindowNewFrame(); IMGUI_API void UpdateMouseMovingWindowEndFrame(); @@ -3074,7 +3193,7 @@ namespace ImGui //#endif // Basic Accessors - inline ImGuiItemStatusFlags GetItemStatusFlags(){ ImGuiContext& g = *GImGui; return g.LastItemData.StatusFlags; } + inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.StatusFlags; } inline ImGuiItemFlags GetItemFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.InFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } @@ -3100,14 +3219,12 @@ namespace ImGui IMGUI_API ImVec2 CalcItemSize(ImVec2 size, float default_w, float default_h); IMGUI_API float CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x); IMGUI_API void PushMultiItemsWidths(int components, float width_full); - IMGUI_API bool IsItemToggledSelection(); // Was the last item selection toggled? (after Selectable(), TreeNode() etc. We only returns toggle _event_ in order to handle clipping correctly) - IMGUI_API ImVec2 GetContentRegionMaxAbs(); IMGUI_API void ShrinkWidths(ImGuiShrinkWidthItem* items, int count, float width_excess); // Parameter stacks (shared) - IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); - IMGUI_API void PopItemFlag(); IMGUI_API const ImGuiDataVarInfo* GetStyleVarInfo(ImGuiStyleVar idx); + IMGUI_API void BeginDisabledOverrideReenable(); + IMGUI_API void EndDisabledOverrideReenable(); // Logging/Capture IMGUI_API void LogBegin(ImGuiLogType type, int auto_open_depth); // -> BeginCapture() when we design v2 api, for now stay under the radar by using the old name. @@ -3115,16 +3232,16 @@ namespace ImGui IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL); IMGUI_API void LogSetNextTextDecoration(const char* prefix, const char* suffix); - // Popups, Modals, Tooltips + // Childs IMGUI_API bool BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags); + + // Popups, Modals + IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_window_flags); IMGUI_API void OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags = ImGuiPopupFlags_None); IMGUI_API void ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to_window_under_popup); IMGUI_API void ClosePopupsExceptModals(); IMGUI_API bool IsPopupOpen(ImGuiID id, ImGuiPopupFlags popup_flags); - IMGUI_API bool BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_flags); - IMGUI_API bool BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); - IMGUI_API bool BeginTooltipHidden(); IMGUI_API ImRect GetPopupAllowedExtentRect(ImGuiWindow* window); IMGUI_API ImGuiWindow* GetTopMostPopupModal(); IMGUI_API ImGuiWindow* GetTopMostAndVisiblePopupModal(); @@ -3132,6 +3249,10 @@ namespace ImGui IMGUI_API ImVec2 FindBestWindowPosForPopup(ImGuiWindow* window); IMGUI_API ImVec2 FindBestWindowPosForPopupEx(const ImVec2& ref_pos, const ImVec2& size, ImGuiDir* last_dir, const ImRect& r_outer, const ImRect& r_avoid, ImGuiPopupPositionPolicy policy); + // Tooltips + IMGUI_API bool BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags extra_window_flags); + IMGUI_API bool BeginTooltipHidden(); + // Menus IMGUI_API bool BeginViewportSideBar(const char* name, ImGuiViewport* viewport, ImGuiDir dir, float size, ImGuiWindowFlags window_flags); IMGUI_API bool BeginMenuEx(const char* label, const char* icon, bool enabled = true); @@ -3149,7 +3270,7 @@ namespace ImGui IMGUI_API void NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); IMGUI_API void NavMoveRequestForward(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavMoveFlags move_flags, ImGuiScrollFlags scroll_flags); IMGUI_API void NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result); - IMGUI_API void NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiNavTreeNodeData* tree_node_data); + IMGUI_API void NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiTreeNodeStackData* tree_node_data); IMGUI_API void NavMoveRequestCancel(); IMGUI_API void NavMoveRequestApplyResult(); IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); @@ -3169,23 +3290,21 @@ namespace ImGui // Inputs // FIXME: Eventually we should aim to move e.g. IsActiveIdUsingKey() into IsKeyXXX functions. - inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; } - inline bool IsNamedKeyOrModKey(ImGuiKey key) { return (key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END) || key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super || key == ImGuiMod_Shortcut; } - inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; } - inline bool IsKeyboardKey(ImGuiKey key) { return key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END; } - inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } - inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } - inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } - inline bool IsModKey(ImGuiKey key) { return key >= ImGuiKey_LeftCtrl && key <= ImGuiKey_RightSuper; } - ImGuiKeyChord FixupKeyChord(ImGuiContext* ctx, ImGuiKeyChord key_chord); - inline ImGuiKey ConvertSingleModFlagToKey(ImGuiContext* ctx, ImGuiKey key) + inline bool IsNamedKey(ImGuiKey key) { return key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END; } + inline bool IsNamedKeyOrMod(ImGuiKey key) { return (key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END) || key == ImGuiMod_Ctrl || key == ImGuiMod_Shift || key == ImGuiMod_Alt || key == ImGuiMod_Super; } + inline bool IsLegacyKey(ImGuiKey key) { return key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_LegacyNativeKey_END; } + inline bool IsKeyboardKey(ImGuiKey key) { return key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END; } + inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } + inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } + inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } + inline bool IsModKey(ImGuiKey key) { return key >= ImGuiKey_LeftCtrl && key <= ImGuiKey_RightSuper; } + ImGuiKeyChord FixupKeyChord(ImGuiKeyChord key_chord); + inline ImGuiKey ConvertSingleModFlagToKey(ImGuiKey key) { - ImGuiContext& g = *ctx; if (key == ImGuiMod_Ctrl) return ImGuiKey_ReservedForModCtrl; if (key == ImGuiMod_Shift) return ImGuiKey_ReservedForModShift; if (key == ImGuiMod_Alt) return ImGuiKey_ReservedForModAlt; if (key == ImGuiMod_Super) return ImGuiKey_ReservedForModSuper; - if (key == ImGuiMod_Shortcut) return (g.IO.ConfigMacOSXBehaviors ? ImGuiKey_ReservedForModSuper : ImGuiKey_ReservedForModCtrl); return key; } @@ -3205,7 +3324,7 @@ namespace ImGui // [EXPERIMENTAL] Low-Level: Key/Input Ownership // - The idea is that instead of "eating" a given input, we can link to an owner id. // - Ownership is most often claimed as a result of reacting to a press/down event (but occasionally may be claimed ahead). - // - Input queries can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_None (== -1) or a custom ID. + // - Input queries can then read input by specifying ImGuiKeyOwner_Any (== 0), ImGuiKeyOwner_NoOwner (== -1) or a custom ID. // - Legacy input queries (without specifying an owner or _Any or _None) are equivalent to using ImGuiKeyOwner_Any (== 0). // - Input ownership is automatically released on the frame after a key is released. Therefore: // - for ownership registration happening as a result of a down/press event, the SetKeyOwner() call may be done once (common case). @@ -3213,12 +3332,12 @@ namespace ImGui // - SetItemKeyOwner() is a shortcut for common simple case. A custom widget will probably want to call SetKeyOwner() multiple times directly based on its interaction state. // - This is marked experimental because not all widgets are fully honoring the Set/Test idioms. We will need to move forward step by step. // Please open a GitHub Issue to submit your usage scenario or if there's a use case you need solved. - IMGUI_API ImGuiID GetKeyOwner(ImGuiKey key); - IMGUI_API void SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); - IMGUI_API void SetKeyOwnersForKeyChord(ImGuiKeyChord key, ImGuiID owner_id, ImGuiInputFlags flags = 0); - IMGUI_API void SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags = 0); // Set key owner to last item if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. - IMGUI_API bool TestKeyOwner(ImGuiKey key, ImGuiID owner_id); // Test that key is either not owned, either owned by 'owner_id' - inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiContext* ctx, ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(ctx, key); IM_ASSERT(IsNamedKey(key)); return &ctx->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; } + IMGUI_API ImGuiID GetKeyOwner(ImGuiKey key); + IMGUI_API void SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API void SetKeyOwnersForKeyChord(ImGuiKeyChord key, ImGuiID owner_id, ImGuiInputFlags flags = 0); + IMGUI_API void SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags); // Set key owner to last item if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. + IMGUI_API bool TestKeyOwner(ImGuiKey key, ImGuiID owner_id); // Test that key is either not owned, either owned by 'owner_id' + inline ImGuiKeyOwnerData* GetKeyOwnerData(ImGuiContext* ctx, ImGuiKey key) { if (key & ImGuiMod_Mask_) key = ConvertSingleModFlagToKey(key); IM_ASSERT(IsNamedKey(key)); return &ctx->KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; } // [EXPERIMENTAL] High-Level: Input Access functions w/ support for Key/Input Ownership // - Important: legacy IsKeyPressed(ImGuiKey, bool repeat=true) _DEFAULTS_ to repeat, new IsKeyPressed() requires _EXPLICIT_ ImGuiInputFlags_Repeat flag. @@ -3226,32 +3345,32 @@ namespace ImGui // - Specifying a value for 'ImGuiID owner' will test that EITHER the key is NOT owned (UNLESS locked), EITHER the key is owned by 'owner'. // Legacy functions use ImGuiKeyOwner_Any meaning that they typically ignore ownership, unless a call to SetKeyOwner() explicitly used ImGuiInputFlags_LockThisFrame or ImGuiInputFlags_LockUntilRelease. // - Binding generators may want to ignore those for now, or suffix them with Ex() until we decide if this gets moved into public API. - IMGUI_API bool IsKeyDown(ImGuiKey key, ImGuiID owner_id); - IMGUI_API bool IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); // Important: when transitioning from old to new IsKeyPressed(): old API has "bool repeat = true", so would default to repeat. New API requiress explicit ImGuiInputFlags_Repeat. - IMGUI_API bool IsKeyReleased(ImGuiKey key, ImGuiID owner_id); - IMGUI_API bool IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id); - IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags = 0); - IMGUI_API bool IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id); - IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button, ImGuiID owner_id); - - // [EXPERIMENTAL] Shortcut Routing - // - ImGuiKeyChord = a ImGuiKey optionally OR-red with ImGuiMod_Alt/ImGuiMod_Ctrl/ImGuiMod_Shift/ImGuiMod_Super. - // ImGuiKey_C (accepted by functions taking ImGuiKey or ImGuiKeyChord) - // ImGuiKey_C | ImGuiMod_Ctrl (accepted by functions taking ImGuiKeyChord) - // ONLY ImGuiMod_XXX values are legal to 'OR' with an ImGuiKey. You CANNOT 'OR' two ImGuiKey values. - // - When using one of the routing flags (e.g. ImGuiInputFlags_RouteFocused): routes requested ahead of time given a chord (key + modifiers) and a routing policy. - // - Routes are resolved during NewFrame(): if keyboard modifiers are matching current ones: SetKeyOwner() is called + route is granted for the frame. - // - Route is granted to a single owner. When multiple requests are made we have policies to select the winning route. - // - Multiple read sites may use the same owner id and will all get the granted route. - // - For routing: when owner_id is 0 we use the current Focus Scope ID as a default owner in order to identify our location. - // - TL;DR; - // - IsKeyChordPressed() compares mods + call IsKeyPressed() -> function has no side-effect. - // - Shortcut() submits a route then if currently can be routed calls IsKeyChordPressed() -> function has (desirable) side-effects. - IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); - IMGUI_API void SetNextItemShortcut(ImGuiKeyChord key_chord); - IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); - IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags = 0); // owner_id needs to be explicit and cannot be 0 - IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id); + IMGUI_API bool IsKeyDown(ImGuiKey key, ImGuiID owner_id); + IMGUI_API bool IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id = 0); // Important: when transitioning from old to new IsKeyPressed(): old API has "bool repeat = true", so would default to repeat. New API requiress explicit ImGuiInputFlags_Repeat. + IMGUI_API bool IsKeyReleased(ImGuiKey key, ImGuiID owner_id); + IMGUI_API bool IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id = 0); + IMGUI_API bool IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id); + IMGUI_API bool IsMouseClicked(ImGuiMouseButton button, ImGuiInputFlags flags, ImGuiID owner_id = 0); + IMGUI_API bool IsMouseReleased(ImGuiMouseButton button, ImGuiID owner_id); + IMGUI_API bool IsMouseDoubleClicked(ImGuiMouseButton button, ImGuiID owner_id); + + // Shortcut Testing & Routing + // - Set Shortcut() and SetNextItemShortcut() in imgui.h + // - When a policy (except for ImGuiInputFlags_RouteAlways *) is set, Shortcut() will register itself with SetShortcutRouting(), + // allowing the system to decide where to route the input among other route-aware calls. + // (* using ImGuiInputFlags_RouteAlways is roughly equivalent to calling IsKeyChordPressed(key) and bypassing route registration and check) + // - When using one of the routing option: + // - The default route is ImGuiInputFlags_RouteFocused (accept inputs if window is in focus stack. Deep-most focused window takes inputs. ActiveId takes inputs over deep-most focused window.) + // - Routes are requested given a chord (key + modifiers) and a routing policy. + // - Routes are resolved during NewFrame(): if keyboard modifiers are matching current ones: SetKeyOwner() is called + route is granted for the frame. + // - Each route may be granted to a single owner. When multiple requests are made we have policies to select the winning route (e.g. deep most window). + // - Multiple read sites may use the same owner id can all access the granted route. + // - When owner_id is 0 we use the current Focus Scope ID as a owner ID in order to identify our location. + // - You can chain two unrelated windows in the focus stack using SetWindowParentWindowForFocusRoute() + // e.g. if you have a tool window associated to a document, and you want document shortcuts to run when the tool is focused. + IMGUI_API bool Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id); + IMGUI_API bool SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id); // owner_id needs to be explicit and cannot be 0 + IMGUI_API bool TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id); IMGUI_API ImGuiKeyRoutingData* GetShortcutRoutingData(ImGuiKeyChord key_chord); // [EXPERIMENTAL] Focus Scope @@ -3279,6 +3398,18 @@ namespace ImGui IMGUI_API int TypingSelectFindNextSingleCharMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx); IMGUI_API int TypingSelectFindBestLeadingMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data); + // Box-Select API + IMGUI_API bool BeginBoxSelect(const ImRect& scope_rect, ImGuiWindow* window, ImGuiID box_select_id, ImGuiMultiSelectFlags ms_flags); + IMGUI_API void EndBoxSelect(const ImRect& scope_rect, ImGuiMultiSelectFlags ms_flags); + + // Multi-Select API + IMGUI_API void MultiSelectItemHeader(ImGuiID id, bool* p_selected, ImGuiButtonFlags* p_button_flags); + IMGUI_API void MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed); + IMGUI_API void MultiSelectAddSetAll(ImGuiMultiSelectTempData* ms, bool selected); + IMGUI_API void MultiSelectAddSetRange(ImGuiMultiSelectTempData* ms, bool selected, int range_dir, ImGuiSelectionUserData first_item, ImGuiSelectionUserData last_item); + inline ImGuiBoxSelectState* GetBoxSelectState(ImGuiID id) { ImGuiContext& g = *GImGui; return (id != 0 && g.BoxSelectState.ID == id && g.BoxSelectState.IsActive) ? &g.BoxSelectState : NULL; } + inline ImGuiMultiSelectState* GetMultiSelectState(ImGuiID id) { ImGuiContext& g = *GImGui; return g.MultiSelectStorage.GetByKey(id); } + // Internal Columns API (this is not exposed because we will encourage transitioning to the Tables API) IMGUI_API void SetWindowClipRectBeforeSetChannel(ImGuiWindow* window, const ImRect& clip_rect); IMGUI_API void BeginColumns(const char* str_id, int count, ImGuiOldColumnFlags flags = 0); // setup number of columns. use an identifier to distinguish multiple column sets. close with EndColumns(). @@ -3295,13 +3426,12 @@ namespace ImGui IMGUI_API void TableOpenContextMenu(int column_n = -1); IMGUI_API void TableSetColumnWidth(int column_n, float width); IMGUI_API void TableSetColumnSortDirection(int column_n, ImGuiSortDirection sort_direction, bool append_to_sort_specs); - IMGUI_API int TableGetHoveredColumn(); // May use (TableGetColumnFlags() & ImGuiTableColumnFlags_IsHovered) instead. Return hovered column. return -1 when table is not hovered. return columns_count if the unused space at the right of visible columns is hovered. IMGUI_API int TableGetHoveredRow(); // Retrieve *PREVIOUS FRAME* hovered row. This difference with TableGetHoveredColumn() is the reason why this is not public yet. IMGUI_API float TableGetHeaderRowHeight(); IMGUI_API float TableGetHeaderAngledMaxLabelWidth(); IMGUI_API void TablePushBackgroundChannel(); IMGUI_API void TablePopBackgroundChannel(); - IMGUI_API void TableAngledHeadersRowEx(float angle, float max_label_width = 0.0f); + IMGUI_API void TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label_width, const ImGuiTableHeaderData* data, int data_count); // Tables: Internals inline ImGuiTable* GetCurrentTable() { ImGuiContext& g = *GImGui; return g.CurrentTable; } @@ -3416,11 +3546,13 @@ namespace ImGui IMGUI_API bool DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags); IMGUI_API bool SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb); IMGUI_API bool SplitterBehavior(const ImRect& bb, ImGuiID id, ImGuiAxis axis, float* size1, float* size2, float min_size1, float min_size2, float hover_extend = 0.0f, float hover_visibility_delay = 0.0f, ImU32 bg_col = 0); + + // Widgets: Tree Nodes IMGUI_API bool TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end = NULL); IMGUI_API void TreePushOverrideID(ImGuiID id); - IMGUI_API void TreeNodeSetOpen(ImGuiID id, bool open); - IMGUI_API bool TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags); // Return open state. Consume previous SetNextItemOpen() data, if any. May return true when logging. - IMGUI_API void SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data); + IMGUI_API bool TreeNodeGetOpen(ImGuiID storage_id); + IMGUI_API void TreeNodeSetOpen(ImGuiID storage_id, bool open); + IMGUI_API bool TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags); // Return open state. Consume previous SetNextItemOpen() data, if any. May return true when logging. // Template functions are instantiated in imgui_widgets.cpp for a finite number of types. // To use them externally (for custom widget) you may need an "extern template" statement in your code in order to link to existing instances and silence Clang warnings (see #2036). @@ -3436,7 +3568,7 @@ namespace ImGui IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type); IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format); IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const void* arg_1, const void* arg_2); - IMGUI_API bool DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format); + IMGUI_API bool DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format, void* p_data_when_empty = NULL); IMGUI_API int DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2); IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max); @@ -3447,6 +3579,7 @@ namespace ImGui IMGUI_API bool TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* p_data, const char* format, const void* p_clamp_min = NULL, const void* p_clamp_max = NULL); inline bool TempInputIsActive(ImGuiID id) { ImGuiContext& g = *GImGui; return (g.ActiveId == id && g.TempInputId == id); } inline ImGuiInputTextState* GetInputTextState(ImGuiID id) { ImGuiContext& g = *GImGui; return (id != 0 && g.InputTextState.ID == id) ? &g.InputTextState : NULL; } // Get input text state if active + IMGUI_API void SetNextItemRefVal(ImGuiDataType data_type, void* p_data); // Color IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags); @@ -3466,18 +3599,15 @@ namespace ImGui IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); - // Debug Log - IMGUI_API void DebugLog(const char* fmt, ...) IM_FMTARGS(1); - IMGUI_API void DebugLogV(const char* fmt, va_list args) IM_FMTLIST(1); - IMGUI_API void DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr, size_t size); // size >= 0 : alloc, size = -1 : free - // Debug Tools + IMGUI_API void DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr, size_t size); // size >= 0 : alloc, size = -1 : free IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); IMGUI_API void ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); IMGUI_API void ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); IMGUI_API void DebugDrawCursorPos(ImU32 col = IM_COL32(255, 0, 0, 255)); IMGUI_API void DebugDrawLineExtents(ImU32 col = IM_COL32(255, 0, 0, 255)); IMGUI_API void DebugDrawItemRect(ImU32 col = IM_COL32(255, 0, 0, 255)); + IMGUI_API void DebugTextUnformattedWithLocateItem(const char* line_begin, const char* line_end); IMGUI_API void DebugLocateItem(ImGuiID target_id); // Call sparingly: only 1 at the same time! IMGUI_API void DebugLocateItemOnHover(ImGuiID target_id); // Only call on reaction to a mouse Hover: because only 1 at the same time! IMGUI_API void DebugLocateItemResolveWithLastItem(); @@ -3497,6 +3627,7 @@ namespace ImGui IMGUI_API void DebugNodeTableSettings(ImGuiTableSettings* settings); IMGUI_API void DebugNodeInputTextState(ImGuiInputTextState* state); IMGUI_API void DebugNodeTypingSelectState(ImGuiTypingSelectState* state); + IMGUI_API void DebugNodeMultiSelectState(ImGuiMultiSelectState* state); IMGUI_API void DebugNodeWindow(ImGuiWindow* window, const char* label); IMGUI_API void DebugNodeWindowSettings(ImGuiWindowSettings* settings); IMGUI_API void DebugNodeWindowsList(ImVector* windows, const char* label); @@ -3510,6 +3641,8 @@ namespace ImGui inline void SetItemUsingMouseWheel() { SetItemKeyOwner(ImGuiKey_MouseWheelY); } // Changed in 1.89 inline bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0) { return TreeNodeUpdateNextOpen(id, flags); } // Renamed in 1.89 + //inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { IM_ASSERT(IsNamedKey(key)); return IsKeyPressed(key, repeat); } // Removed in 1.87: Mapping from named key is always identity! + // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets which used FocusableItemRegister(): // (Old) IMGUI_VERSION_NUM < 18209: using 'ItemAdd(....)' and 'bool tab_focused = FocusableItemRegister(...)' // (Old) IMGUI_VERSION_NUM >= 18209: using 'ItemAdd(..., ImGuiItemAddFlags_Focusable)' and 'bool tab_focused = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Focused) != 0' @@ -3517,9 +3650,6 @@ namespace ImGui //inline bool FocusableItemRegister(ImGuiWindow* window, ImGuiID id) // -> pass ImGuiItemAddFlags_Inputable flag to ItemAdd() //inline void FocusableItemUnregister(ImGuiWindow* window) // -> unnecessary: TempInputText() uses ImGuiInputTextFlags_MergedItem #endif -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { IM_ASSERT(IsNamedKey(key)); return IsKeyPressed(key, repeat); } // Removed in 1.87: Mapping from named key is always identity! -#endif } // namespace ImGui diff --git a/include/imgui/imstb_textedit.h b/include/imgui/imstb_textedit.h index c30f1a12..783054ab 100644 --- a/include/imgui/imstb_textedit.h +++ b/include/imgui/imstb_textedit.h @@ -41,7 +41,7 @@ // 1.13 (2019-02-07) fix bug in undo size management // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield -// 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual +// 1.10 (2016-10-25) suppress warnings about casting away const with -Wcast-qual // 1.9 (2016-08-27) customizable move-by-word // 1.8 (2016-04-02) better keyboard handling when mouse button is down // 1.7 (2015-09-13) change y range handling in case baseline is non-0 diff --git a/include/imgui/imstb_truetype.h b/include/imgui/imstb_truetype.h index 35c827e6..976f09cb 100644 --- a/include/imgui/imstb_truetype.h +++ b/include/imgui/imstb_truetype.h @@ -656,7 +656,7 @@ STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip); // If skip != 0, this tells stb_truetype to skip any codepoints for which // there is no corresponding glyph. If skip=0, which is the default, then -// codepoints without a glyph recived the font's "missing character" glyph, +// codepoints without a glyph received the font's "missing character" glyph, // typically an empty box by convention. STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, // same data as above diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index 4f017037..2c861126 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (main code and documentation) // Help: @@ -7,15 +7,19 @@ // - Read top of imgui.cpp for more details, links and comments. // Resources: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Homepage https://github.com/ocornut/imgui -// - Releases & changelog https://github.com/ocornut/imgui/releases -// - Gallery https://github.com/ocornut/imgui/issues/6897 (please post your screenshots/video there!) -// - Wiki https://github.com/ocornut/imgui/wiki (lots of good stuff there) -// - Glossary https://github.com/ocornut/imgui/wiki/Glossary -// - Issues & support https://github.com/ocornut/imgui/issues -// - Tests & Automation https://github.com/ocornut/imgui_test_engine +// - FAQ ........................ https://dearimgui.com/faq (in repository as docs/FAQ.md) +// - Homepage ................... https://github.com/ocornut/imgui +// - Releases & changelog ....... https://github.com/ocornut/imgui/releases +// - Gallery .................... https://github.com/ocornut/imgui/issues/7503 (please post your screenshots/video there!) +// - Wiki ....................... https://github.com/ocornut/imgui/wiki (lots of good stuff there) +// - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started (how to integrate in an existing app by adding ~25 lines of code) +// - Third-party Extensions https://github.com/ocornut/imgui/wiki/Useful-Extensions (ImPlot & many more) +// - Bindings/Backends https://github.com/ocornut/imgui/wiki/Bindings (language bindings, backends for various tech/engines) +// - Glossary https://github.com/ocornut/imgui/wiki/Glossary +// - Debug Tools https://github.com/ocornut/imgui/wiki/Debug-Tools +// - Software using Dear ImGui https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui +// - Issues & support ........... https://github.com/ocornut/imgui/issues +// - Test Engine & Automation ... https://github.com/ocornut/imgui_test_engine (test suite, test engine to automate your apps) // For first-time users having issues compiling/linking/running/loading fonts: // please post in https://github.com/ocornut/imgui/discussions if you cannot find a solution in resources above. @@ -26,7 +30,7 @@ // See LICENSE.txt for copyright and licensing details (standard MIT License). // This library is free but needs your support to sustain development and maintenance. // Businesses: you can support continued development via B2B invoiced technical support, maintenance and sponsoring contracts. -// PLEASE reach out at omar AT dearimgui DOT com. See https://github.com/ocornut/imgui/wiki/Sponsors +// PLEASE reach out at omar AT dearimgui DOT com. See https://github.com/ocornut/imgui/wiki/Funding // Businesses: you can also purchase licenses for the Dear ImGui Automation/Test Engine. // It is recommended that you don't modify imgui.cpp! It will become difficult for you to update the library. @@ -73,6 +77,7 @@ CODE // [SECTION] RENDER HELPERS // [SECTION] INITIALIZATION, SHUTDOWN // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) +// [SECTION] ID STACK // [SECTION] INPUTS // [SECTION] ERROR CHECKING // [SECTION] ITEM SUBMISSION @@ -138,7 +143,7 @@ CODE - CTRL+X, CTRL+C, CTRL+V: Use OS clipboard. - CTRL+Z, CTRL+Y: Undo, Redo. - ESCAPE: Revert text to its original value. - - On OSX, controls are automatically adjusted to match standard OSX text editing shortcuts and behaviors. + - On OSX, controls are automatically adjusted to match standard OSX text editing 2ts and behaviors. - KEYBOARD CONTROLS - Basic: @@ -425,6 +430,64 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2024/07/25 (1.91.0) - obsoleted GetContentRegionMax(), GetWindowContentRegionMin() and GetWindowContentRegionMax(). (see #7838 on GitHub for more info) + you should never need those functions. you can do everything with GetCursorScreenPos() and GetContentRegionAvail() in a more simple way. + - instead of: GetWindowContentRegionMax().x - GetCursorPos().x + - you can use: GetContentRegionAvail().x + - instead of: GetWindowContentRegionMax().x + GetWindowPos().x + - you can use: GetCursorScreenPos().x + GetContentRegionAvail().x // when called from left edge of window + - instead of: GetContentRegionMax() + - you can use: GetContentRegionAvail() + GetCursorScreenPos() - GetWindowPos() // right edge in local coordinates + - instead of: GetWindowContentRegionMax().x - GetWindowContentRegionMin().x + - you can use: GetContentRegionAvail() // when called from left edge of window + - 2024/07/15 (1.91.0) - renamed ImGuiSelectableFlags_DontClosePopups to ImGuiSelectableFlags_NoAutoClosePopups. (#1379, #1468, #2200, #4936, #5216, #7302, #7573) + (internals: also renamed ImGuiItemFlags_SelectableDontClosePopup into ImGuiItemFlags_AutoClosePopups with inverted behaviors) + - 2024/07/15 (1.91.0) - obsoleted PushButtonRepeat()/PopButtonRepeat() in favor of using new PushItemFlag(ImGuiItemFlags_ButtonRepeat, ...)/PopItemFlag(). + - 2024/07/02 (1.91.0) - commented out obsolete ImGuiModFlags (renamed to ImGuiKeyChord in 1.89). (#4921, #456) + - commented out obsolete ImGuiModFlags_XXX values (renamed to ImGuiMod_XXX in 1.89). (#4921, #456) + - ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl, ImGuiModFlags_Shift -> ImGuiMod_Shift etc. + - 2024/07/02 (1.91.0) - IO, IME: renamed platform IME hook and added explicit context for consistency and future-proofness. + - old: io.SetPlatformImeDataFn(ImGuiViewport* viewport, ImGuiPlatformImeData* data); + - new: io.PlatformSetImeDataFn(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); + - 2024/06/21 (1.90.9) - BeginChild: added ImGuiChildFlags_NavFlattened as a replacement for the window flag ImGuiWindowFlags_NavFlattened: the feature only ever made sense for BeginChild() anyhow. + - old: BeginChild("Name", size, 0, ImGuiWindowFlags_NavFlattened); + - new: BeginChild("Name", size, ImGuiChildFlags_NavFlattened, 0); + - 2024/06/21 (1.90.9) - io: ClearInputKeys() (first exposed in 1.89.8) doesn't clear mouse data, newly added ClearInputMouse() does. + - 2024/06/20 (1.90.9) - renamed ImGuiDragDropFlags_SourceAutoExpirePayload to ImGuiDragDropFlags_PayloadAutoExpire. + - 2024/06/18 (1.90.9) - style: renamed ImGuiCol_TabActive -> ImGuiCol_TabSelected, ImGuiCol_TabUnfocused -> ImGuiCol_TabDimmed, ImGuiCol_TabUnfocusedActive -> ImGuiCol_TabDimmedSelected. + - 2024/06/10 (1.90.9) - removed old nested structure: renaming ImGuiStorage::ImGuiStoragePair type to ImGuiStoragePair (simpler for many languages). + - 2024/06/06 (1.90.8) - reordered ImGuiInputTextFlags values. This should not be breaking unless you are using generated headers that have values not matching the main library. + - 2024/06/06 (1.90.8) - removed 'ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft', was mostly unused and misleading. + - 2024/05/27 (1.90.7) - commented out obsolete symbols marked obsolete in 1.88 (May 2022): + - old: CaptureKeyboardFromApp(bool) + - new: SetNextFrameWantCaptureKeyboard(bool) + - old: CaptureMouseFromApp(bool) + - new: SetNextFrameWantCaptureMouse(bool) + - 2024/05/22 (1.90.7) - inputs (internals): renamed ImGuiKeyOwner_None to ImGuiKeyOwner_NoOwner, to make use more explicit and reduce confusion with the default it is a non-zero value and cannot be the default value (never made public, but disclosing as I expect a few users caught on owner-aware inputs). + - inputs (internals): renamed ImGuiInputFlags_RouteGlobalLow -> ImGuiInputFlags_RouteGlobal, ImGuiInputFlags_RouteGlobal -> ImGuiInputFlags_RouteGlobalOverFocused, ImGuiInputFlags_RouteGlobalHigh -> ImGuiInputFlags_RouteGlobalHighest. + - inputs (internals): Shortcut(), SetShortcutRouting(): swapped last two parameters order in function signatures: + - old: Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id = 0, ImGuiInputFlags flags = 0); + - new: Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags = 0, ImGuiID owner_id = 0); + - inputs (internals): owner-aware versions of IsKeyPressed(), IsKeyChordPressed(), IsMouseClicked(): swapped last two parameters order in function signatures. + - old: IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags = 0); + - new: IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id = 0); + - old: IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags = 0); + - new: IsMouseClicked(ImGuiMouseButton button, ImGuiInputFlags flags, ImGuiID owner_id = 0); + for various reasons those changes makes sense. They are being made because making some of those API public. + only past users of imgui_internal.h with the extra parameters will be affected. Added asserts for valid flags in various functions to detect _some_ misuses, BUT NOT ALL. + - 2024/05/16 (1.90.7) - inputs: on macOS X, Cmd and Ctrl keys are now automatically swapped by io.AddKeyEvent() as this naturally align with how macOS X uses those keys. + - it shouldn't really affect you unless you had custom shortcut swapping in place for macOS X apps. + - removed ImGuiMod_Shortcut which was previously dynamically remapping to Ctrl or Cmd/Super. It is now unnecessary to specific cross-platform idiomatic shortcuts. (#2343, #4084, #5923, #456) + - 2024/05/14 (1.90.7) - backends: SDL_Renderer2 and SDL_Renderer3 backend now take a SDL_Renderer* in their RenderDrawData() functions. + - 2024/04/18 (1.90.6) - TreeNode: Fixed a layout inconsistency when using an empty/hidden label followed by a SameLine() call. (#7505, #282) + - old: TreeNode("##Hidden"); SameLine(); Text("Hello"); // <-- This was actually incorrect! BUT appeared to look ok with the default style where ItemSpacing.x == FramePadding.x * 2 (it didn't look aligned otherwise). + - new: TreeNode("##Hidden"); SameLine(0, 0); Text("Hello"); // <-- This is correct for all styles values. + with the fix, IF you were successfully using TreeNode("")+SameLine(); you will now have extra spacing between your TreeNode and the following item. + You'll need to change the SameLine() call to SameLine(0,0) to remove this extraneous spacing. This seemed like the more sensible fix that's not making things less consistent. + (Note: when using this idiom you are likely to also use ImGuiTreeNodeFlags_SpanAvailWidth). + - 2024/03/18 (1.90.5) - merged the radius_x/radius_y parameters in ImDrawList::AddEllipse(), AddEllipseFilled() and PathEllipticalArcTo() into a single ImVec2 parameter. Exceptionally, because those functions were added in 1.90, we are not adding inline redirection functions. The transition is easy and should affect few users. (#2743, #7417) + - 2024/03/08 (1.90.5) - inputs: more formally obsoleted GetKeyIndex() when IMGUI_DISABLE_OBSOLETE_FUNCTIONS is set. It has been unnecessary and a no-op since 1.87 (it returns the same value as passed when used with a 1.87+ backend using io.AddKeyEvent() function). (#4921) + - IsKeyPressed(GetKeyIndex(ImGuiKey_XXX)) -> use IsKeyPressed(ImGuiKey_XXX) - 2024/01/15 (1.90.2) - commented out obsolete ImGuiIO::ImeWindowHandle marked obsolete in 1.87, favor of writing to 'void* ImGuiViewport::PlatformHandleRaw'. - 2023/12/19 (1.90.1) - commented out obsolete ImGuiKey_KeyPadEnter redirection to ImGuiKey_KeypadEnter. - 2023/11/06 (1.90.1) - removed CalcListClipping() marked obsolete in 1.86. Prefer using ImGuiListClipper which can return non-contiguous ranges. @@ -537,7 +600,7 @@ CODE - Backend writing to io.MouseHoveredViewport -> backend should call io.AddMouseViewportEvent() [Docking branch w/ multi-viewports only] note: for all calls to IO new functions, the Dear ImGui context should be bound/current. read https://github.com/ocornut/imgui/issues/4921 for details. - - 2022/01/10 (1.87) - inputs: reworked keyboard IO. Removed io.KeyMap[], io.KeysDown[] in favor of calling io.AddKeyEvent(). Removed GetKeyIndex(), now unecessary. All IsKeyXXX() functions now take ImGuiKey values. All features are still functional until IMGUI_DISABLE_OBSOLETE_KEYIO is defined. Read Changelog and Release Notes for details. + - 2022/01/10 (1.87) - inputs: reworked keyboard IO. Removed io.KeyMap[], io.KeysDown[] in favor of calling io.AddKeyEvent(). Removed GetKeyIndex(), now unnecessary. All IsKeyXXX() functions now take ImGuiKey values. All features are still functional until IMGUI_DISABLE_OBSOLETE_KEYIO is defined. Read Changelog and Release Notes for details. - IsKeyPressed(MY_NATIVE_KEY_XXX) -> use IsKeyPressed(ImGuiKey_XXX) - IsKeyPressed(GetKeyIndex(ImGuiKey_XXX)) -> use IsKeyPressed(ImGuiKey_XXX) - Backend writing to io.KeyMap[],io.KeysDown[] -> backend should call io.AddKeyEvent() (+ call io.SetKeyEventNativeData() if you want legacy user code to stil function with legacy key codes). @@ -935,7 +998,7 @@ CODE A: - Businesses: please reach out to "omar AT dearimgui DOT com" if you work in a place using Dear ImGui! We can discuss ways for your company to fund development via invoiced technical support, maintenance or sponsoring contacts. This is among the most useful thing you can do for Dear ImGui. With increased funding, we sustain and grow work on this project. - Also see https://github.com/ocornut/imgui/wiki/Sponsors + >>> See https://github.com/ocornut/imgui/wiki/Funding - Businesses: you can also purchase licenses for the Dear ImGui Automation/Test Engine. - If you are experienced with Dear ImGui and C++, look at the GitHub issues, look at the Wiki, and see how you want to help and can help! - Disclose your usage of Dear ImGui via a dev blog post, a tweet, a screenshot, a mention somewhere etc. @@ -971,7 +1034,7 @@ CODE #endif // [Windows] OS specific includes (optional) -#if defined(_WIN32) && defined(IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) +#if defined(_WIN32) && defined(IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) && defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && defined(IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) #define IMGUI_DISABLE_WIN32_FUNCTIONS #endif #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) @@ -986,9 +1049,11 @@ CODE #else #include #endif -#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) // UWP doesn't have all Win32 functions +#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES) +// The UWP and GDK Win32 API subsets don't support clipboard nor IME functions #define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS #define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS +#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS #endif #endif @@ -1026,6 +1091,7 @@ CODE #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) // We disable -Wpragmas because GCC doesn't provide a has_warning equivalent and some forks/patches may not follow the warning/version association. #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind @@ -1040,7 +1106,7 @@ CODE #endif // Debug options -#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Display last moving direction matches when holding CTRL +#define IMGUI_DEBUG_NAV_SCORING 0 // Display navigation scoring preview when hovering items. Hold CTRL to display for all candidates. CTRL+Arrow to change last direction. #define IMGUI_DEBUG_NAV_RECTS 0 // Display the reference navigation rectangle for each window // When using CTRL+TAB (or Gamepad Square+L/R) we delay the visual a little in order to reduce visual noise doing a fast switch. @@ -1062,7 +1128,6 @@ static const ImVec2 TOOLTIP_DEFAULT_OFFSET = ImVec2(16, 10); // Multi //------------------------------------------------------------------------- static void SetCurrentWindow(ImGuiWindow* window); -static void FindHoveredWindow(); static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags); static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window); @@ -1078,10 +1143,14 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSetti // Platform Dependents default implementation for IO functions static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx); static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text); -static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data); +static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); +static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext* ctx, const char* path); namespace ImGui { +// Item +static void ItemHandleShortcut(ImGuiID id); + // Navigation static void NavUpdate(); static void NavUpdateWindowing(); @@ -1124,6 +1193,7 @@ static void RenderWindowDecorations(ImGuiWindow* window, const ImRec static void RenderWindowTitleBarContents(ImGuiWindow* window, const ImRect& title_bar_rect, const char* name, bool* p_open); static void RenderDimmedBackgroundBehindWindow(ImGuiWindow* window, ImU32 col); static void RenderDimmedBackgrounds(); +static void SetLastItemDataForWindow(ImGuiWindow* window, const ImRect& rect); // Viewports const ImGuiID IMGUI_VIEWPORT_DEFAULT_ID = 0x11111111; // Using an arbitrary constant instead of e.g. ImHashStr("ViewportDefault", 0); so it's easier to spot in the debugger. The exact value doesn't matter. @@ -1179,58 +1249,60 @@ static void* GImAllocatorUserData = NULL; ImGuiStyle::ImGuiStyle() { - Alpha = 1.0f; // Global alpha applies to everything in Dear ImGui. - DisabledAlpha = 0.60f; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. - WindowPadding = ImVec2(8,8); // Padding within a window - WindowRounding = 0.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. - WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. - WindowMinSize = ImVec2(32,32); // Minimum window size - WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text - WindowMenuButtonPosition= ImGuiDir_Left; // Position of the collapsing/docking button in the title bar (left/right). Defaults to ImGuiDir_Left. - ChildRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows - ChildBorderSize = 1.0f; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested. - PopupRounding = 0.0f; // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows - PopupBorderSize = 1.0f; // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested. - FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets) - FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets). - FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. - ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines - ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) - CellPadding = ImVec2(4,2); // Padding within a table cell. CellPadding.y may be altered between different rows. - TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! - IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). - ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). - ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar - ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar - GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar - GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. - LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. - TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. - TabBorderSize = 0.0f; // Thickness of border around tabs. - TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. - TabBarBorderSize = 1.0f; // Thickness of tab-bar separator, which takes on the tab active color to denote focus. - TableAngledHeadersAngle = 35.0f * (IM_PI / 180.0f); // Angle of angled headers (supported values range from -50 degrees to +50 degrees). - ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. - ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. - SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. - SeparatorTextBorderSize = 3.0f; // Thickkness of border in SeparatorText() - SeparatorTextAlign = ImVec2(0.0f,0.5f);// Alignment of text within the separator. Defaults to (0.0f, 0.5f) (left aligned, center). - SeparatorTextPadding = ImVec2(20.0f,3.f);// Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y. - DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. - DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. - MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. - AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. - AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). - AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). - CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. - CircleTessellationMaxError = 0.30f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. + Alpha = 1.0f; // Global alpha applies to everything in Dear ImGui. + DisabledAlpha = 0.60f; // Additional alpha multiplier applied by BeginDisabled(). Multiply over current value of Alpha. + WindowPadding = ImVec2(8,8); // Padding within a window + WindowRounding = 0.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows. Large values tend to lead to variety of artifacts and are not recommended. + WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested. + WindowMinSize = ImVec2(32,32); // Minimum window size + WindowTitleAlign = ImVec2(0.0f,0.5f);// Alignment for title bar text + WindowMenuButtonPosition = ImGuiDir_Left; // Position of the collapsing/docking button in the title bar (left/right). Defaults to ImGuiDir_Left. + ChildRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows + ChildBorderSize = 1.0f; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested. + PopupRounding = 0.0f; // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows + PopupBorderSize = 1.0f; // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested. + FramePadding = ImVec2(4,3); // Padding within a framed rectangle (used by most widgets) + FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets). + FrameBorderSize = 0.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested. + ItemSpacing = ImVec2(8,4); // Horizontal and vertical spacing between widgets/lines + ItemInnerSpacing = ImVec2(4,4); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label) + CellPadding = ImVec2(4,2); // Padding within a table cell. Cellpadding.x is locked for entire table. CellPadding.y may be altered between different rows. + TouchExtraPadding = ImVec2(0,0); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first widget. So don't grow this too much! + IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2). + ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns. Preferably > (FramePadding.x + 1). + ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar + ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar + GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar + GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs. + LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero. + TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs. + TabBorderSize = 0.0f; // Thickness of border around tabs. + TabMinWidthForCloseButton = 0.0f; // Minimum width for close button to appear on an unselected tab when hovered. Set to 0.0f to always show when hovering, set to FLT_MAX to never show close button unless selected. + TabBarBorderSize = 1.0f; // Thickness of tab-bar separator, which takes on the tab active color to denote focus. + TabBarOverlineSize = 2.0f; // Thickness of tab-bar overline, which highlights the selected tab-bar. + TableAngledHeadersAngle = 35.0f * (IM_PI / 180.0f); // Angle of angled headers (supported values range from -50 degrees to +50 degrees). + TableAngledHeadersTextAlign = ImVec2(0.5f,0.0f);// Alignment of angled headers within the cell + ColorButtonPosition = ImGuiDir_Right; // Side of the color button in the ColorEdit4 widget (left/right). Defaults to ImGuiDir_Right. + ButtonTextAlign = ImVec2(0.5f,0.5f);// Alignment of button text when button is larger than text. + SelectableTextAlign = ImVec2(0.0f,0.0f);// Alignment of selectable text. Defaults to (0.0f, 0.0f) (top-left aligned). It's generally important to keep this left-aligned if you want to lay multiple items on a same line. + SeparatorTextBorderSize = 3.0f; // Thickness of border in SeparatorText() + SeparatorTextAlign = ImVec2(0.0f,0.5f);// Alignment of text within the separator. Defaults to (0.0f, 0.5f) (left aligned, center). + SeparatorTextPadding = ImVec2(20.0f,3.f);// Horizontal offset of text from each edge of the separator + spacing on other axis. Generally small values. .y is recommended to be == FramePadding.y. + DisplayWindowPadding = ImVec2(19,19); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows. + DisplaySafeAreaPadding = ImVec2(3,3); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows. + MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later. + AntiAliasedLines = true; // Enable anti-aliased lines/borders. Disable if you are really tight on CPU/GPU. + AntiAliasedLinesUseTex = true; // Enable anti-aliased lines/borders using textures where possible. Require backend to render with bilinear filtering (NOT point/nearest filtering). + AntiAliasedFill = true; // Enable anti-aliased filled shapes (rounded rectangles, circles, etc.). + CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality. + CircleTessellationMaxError = 0.30f; // Maximum error (in pixels) allowed when using AddCircle()/AddCircleFilled() or drawing rounded corner rectangles with no explicit segment count specified. Decrease for higher quality but more geometry. // Behaviors - HoverStationaryDelay = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_Stationary). Time required to consider mouse stationary. - HoverDelayShort = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayShort). Usually used along with HoverStationaryDelay. - HoverDelayNormal = 0.40f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayNormal). " - HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. - HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. + HoverStationaryDelay = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_Stationary). Time required to consider mouse stationary. + HoverDelayShort = 0.15f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayShort). Usually used along with HoverStationaryDelay. + HoverDelayNormal = 0.40f; // Delay for IsItemHovered(ImGuiHoveredFlags_DelayNormal). " + HoverFlagsForTooltipMouse = ImGuiHoveredFlags_Stationary | ImGuiHoveredFlags_DelayShort | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse. + HoverFlagsForTooltipNav = ImGuiHoveredFlags_NoSharedDelay | ImGuiHoveredFlags_DelayNormal | ImGuiHoveredFlags_AllowWhenDisabled; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad. // Default theme ImGui::StyleColorsDark(this); @@ -1260,6 +1332,7 @@ void ImGuiStyle::ScaleAllSizes(float scale_factor) LogSliderDeadzone = ImTrunc(LogSliderDeadzone * scale_factor); TabRounding = ImTrunc(TabRounding * scale_factor); TabMinWidthForCloseButton = (TabMinWidthForCloseButton != FLT_MAX) ? ImTrunc(TabMinWidthForCloseButton * scale_factor) : FLT_MAX; + TabBarOverlineSize = ImTrunc(TabBarOverlineSize * scale_factor); SeparatorTextPadding = ImTrunc(SeparatorTextPadding * scale_factor); DisplayWindowPadding = ImTrunc(DisplayWindowPadding * scale_factor); DisplaySafeAreaPadding = ImTrunc(DisplaySafeAreaPadding * scale_factor); @@ -1305,6 +1378,7 @@ ImGuiIO::ImGuiIO() #else ConfigMacOSXBehaviors = false; #endif + ConfigNavSwapGamepadButtons = false; ConfigInputTrickleEventQueue = true; ConfigInputTextCursorBlink = true; ConfigInputTextEnterKeepActive = false; @@ -1319,6 +1393,7 @@ ImGuiIO::ImGuiIO() // Note: Initialize() will setup default clipboard/ime handlers. BackendPlatformName = BackendRendererName = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; + PlatformOpenInShellUserData = NULL; PlatformLocaleDecimalPoint = '.'; // Input (NB: we already have memset zero the entire structure!) @@ -1407,7 +1482,7 @@ void ImGuiIO::ClearEventsQueue() g.InputEventsQueue.clear(); } -// Clear current keyboard/mouse/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. +// Clear current keyboard/gamepad state + current frame text input buffer. Equivalent to releasing all keys/buttons. void ImGuiIO::ClearInputKeys() { #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -1415,12 +1490,26 @@ void ImGuiIO::ClearInputKeys() #endif for (int n = 0; n < IM_ARRAYSIZE(KeysData); n++) { + if (ImGui::IsMouseKey((ImGuiKey)(n + ImGuiKey_KeysData_OFFSET))) + continue; KeysData[n].Down = false; KeysData[n].DownDuration = -1.0f; KeysData[n].DownDurationPrev = -1.0f; } KeyCtrl = KeyShift = KeyAlt = KeySuper = false; KeyMods = ImGuiMod_None; + InputQueueCharacters.resize(0); // Behavior of old ClearInputCharacters(). +} + +void ImGuiIO::ClearInputMouse() +{ + for (ImGuiKey key = ImGuiKey_Mouse_BEGIN; key < ImGuiKey_Mouse_END; key = (ImGuiKey)(key + 1)) + { + ImGuiKeyData* key_data = &KeysData[key - ImGuiKey_KeysData_OFFSET]; + key_data->Down = false; + key_data->DownDuration = -1.0f; + key_data->DownDurationPrev = -1.0f; + } MousePos = ImVec2(-FLT_MAX, -FLT_MAX); for (int n = 0; n < IM_ARRAYSIZE(MouseDown); n++) { @@ -1428,7 +1517,6 @@ void ImGuiIO::ClearInputKeys() MouseDownDuration[n] = MouseDownDurationPrev[n] = -1.0f; } MouseWheel = MouseWheelH = 0.0f; - InputQueueCharacters.resize(0); // Behavior of old ClearInputCharacters(). } // Removed this as it is ambiguous/misleading and generally incorrect to use with the existence of a higher-level input queue. @@ -1462,7 +1550,7 @@ static ImGuiInputEvent* FindLatestInputEvent(ImGuiContext* ctx, ImGuiInputEventT // - bool down: Is the key down? use false to signify a key release. // - float analog_value: 0.0f..1.0f // IMPORTANT: THIS FUNCTION AND OTHER "ADD" GRABS THE CONTEXT FROM OUR INSTANCE. -// WE NEED TO ENSURE THAT ALL FUNCTION CALLS ARE FULLFILLING THIS, WHICH IS WHY GetKeyData() HAS AN EXPLICIT CONTEXT. +// WE NEED TO ENSURE THAT ALL FUNCTION CALLS ARE FULFILLING THIS, WHICH IS WHY GetKeyData() HAS AN EXPLICIT CONTEXT. void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) { //if (e->Down) { IMGUI_DEBUG_LOG_IO("AddKeyEvent() Key='%s' %d, NativeKeycode = %d, NativeScancode = %d\n", ImGui::GetKeyName(e->Key), e->Down, e->NativeKeycode, e->NativeScancode); } @@ -1470,9 +1558,19 @@ void ImGuiIO::AddKeyAnalogEvent(ImGuiKey key, bool down, float analog_value) if (key == ImGuiKey_None || !AppAcceptingEvents) return; ImGuiContext& g = *Ctx; - IM_ASSERT(ImGui::IsNamedKeyOrModKey(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. + IM_ASSERT(ImGui::IsNamedKeyOrMod(key)); // Backend needs to pass a valid ImGuiKey_ constant. 0..511 values are legacy native key codes which are not accepted by this API. IM_ASSERT(ImGui::IsAliasKey(key) == false); // Backend cannot submit ImGuiKey_MouseXXX values they are automatically inferred from AddMouseXXX() events. - IM_ASSERT(key != ImGuiMod_Shortcut); // We could easily support the translation here but it seems saner to not accept it (TestEngine perform a translation itself) + + // MacOS: swap Cmd(Super) and Ctrl + if (g.IO.ConfigMacOSXBehaviors) + { + if (key == ImGuiMod_Super) { key = ImGuiMod_Ctrl; } + else if (key == ImGuiMod_Ctrl) { key = ImGuiMod_Super; } + else if (key == ImGuiKey_LeftSuper) { key = ImGuiKey_LeftCtrl; } + else if (key == ImGuiKey_RightSuper){ key = ImGuiKey_RightCtrl; } + else if (key == ImGuiKey_LeftCtrl) { key = ImGuiKey_LeftSuper; } + else if (key == ImGuiKey_RightCtrl) { key = ImGuiKey_RightSuper; } + } // Verify that backend isn't mixing up using new io.AddKeyEvent() api and old io.KeysDown[] + io.KeyMap[] data. #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -1577,12 +1675,36 @@ void ImGuiIO::AddMouseButtonEvent(int mouse_button, bool down) if (!AppAcceptingEvents) return; + // On MacOS X: Convert Ctrl(Super)+Left click into Right-click: handle held button. + if (ConfigMacOSXBehaviors && mouse_button == 0 && MouseCtrlLeftAsRightClick) + { + // Order of both statements matterns: this event will still release mouse button 1 + mouse_button = 1; + if (!down) + MouseCtrlLeftAsRightClick = false; + } + // Filter duplicate const ImGuiInputEvent* latest_event = FindLatestInputEvent(&g, ImGuiInputEventType_MouseButton, (int)mouse_button); const bool latest_button_down = latest_event ? latest_event->MouseButton.Down : g.IO.MouseDown[mouse_button]; if (latest_button_down == down) return; + // On MacOS X: Convert Ctrl(Super)+Left click into Right-click. + // - Note that this is actual physical Ctrl which is ImGuiMod_Super for us. + // - At this point we want from !down to down, so this is handling the initial press. + if (ConfigMacOSXBehaviors && mouse_button == 0 && down) + { + const ImGuiInputEvent* latest_super_event = FindLatestInputEvent(&g, ImGuiInputEventType_Key, (int)ImGuiMod_Super); + if (latest_super_event ? latest_super_event->Key.Down : g.IO.KeySuper) + { + IMGUI_DEBUG_LOG_IO("[io] Super+Left Click aliased into Right Click\n"); + MouseCtrlLeftAsRightClick = true; + AddMouseButtonEvent(1, true); // This is just quicker to write that passing through, as we need to filter duplicate again. + return; + } + } + ImGuiInputEvent e; e.Type = ImGuiInputEventType_MouseButton; e.Source = ImGuiInputSource_Mouse; @@ -1948,6 +2070,10 @@ void ImFormatStringToTempBuffer(const char** out_buf, const char** out_buf_end, va_end(args); } +// FIXME: Should rework API toward allowing multiple in-flight temp buffers (easier and safer for caller) +// by making the caller acquire a temp buffer token, with either explicit or destructor release, e.g. +// ImGuiTempBufferToken token; +// ImFormatStringToTempBuffer(token, ...); void ImFormatStringToTempBufferV(const char** out_buf, const char** out_buf_end, const char* fmt, va_list args) { ImGuiContext& g = *GImGui; @@ -2318,6 +2444,20 @@ const char* ImTextFindPreviousUtf8Codepoint(const char* in_text_start, const cha return in_text_start; } +int ImTextCountLines(const char* in_text, const char* in_text_end) +{ + if (in_text_end == NULL) + in_text_end = in_text + strlen(in_text); // FIXME-OPT: Not optimal approach, discourage use for now. + int count = 0; + while (in_text < in_text_end) + { + const char* line_end = (const char*)memchr(in_text, '\n', in_text_end - in_text); + in_text = line_end ? line_end + 1 : in_text_end; + count++; + } + return count; +} + IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- @@ -2411,18 +2551,16 @@ void ImGui::ColorConvertHSVtoRGB(float h, float s, float v, float& out_r, float& //----------------------------------------------------------------------------- // std::lower_bound but without the bullshit -static ImGuiStorage::ImGuiStoragePair* LowerBound(ImVector& data, ImGuiID key) +ImGuiStoragePair* ImLowerBound(ImGuiStoragePair* in_begin, ImGuiStoragePair* in_end, ImGuiID key) { - ImGuiStorage::ImGuiStoragePair* first = data.Data; - ImGuiStorage::ImGuiStoragePair* last = data.Data + data.Size; - size_t count = (size_t)(last - first); - while (count > 0) + ImGuiStoragePair* in_p = in_begin; + for (size_t count = (size_t)(in_end - in_p); count > 0; ) { size_t count2 = count >> 1; - ImGuiStorage::ImGuiStoragePair* mid = first + count2; + ImGuiStoragePair* mid = in_p + count2; if (mid->key < key) { - first = ++mid; + in_p = ++mid; count -= count2 + 1; } else @@ -2430,29 +2568,28 @@ static ImGuiStorage::ImGuiStoragePair* LowerBound(ImVectorkey; + ImGuiID rhs_v = ((const ImGuiStoragePair*)rhs)->key; + return (lhs_v > rhs_v ? +1 : lhs_v < rhs_v ? -1 : 0); } // For quicker full rebuild of a storage (instead of an incremental one), you may add all your contents and then sort once. void ImGuiStorage::BuildSortByKey() { - struct StaticFunc - { - static int IMGUI_CDECL PairComparerByID(const void* lhs, const void* rhs) - { - // We can't just do a subtraction because qsort uses signed integers and subtracting our ID doesn't play well with that. - if (((const ImGuiStoragePair*)lhs)->key > ((const ImGuiStoragePair*)rhs)->key) return +1; - if (((const ImGuiStoragePair*)lhs)->key < ((const ImGuiStoragePair*)rhs)->key) return -1; - return 0; - } - }; - ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), StaticFunc::PairComparerByID); + ImQsort(Data.Data, (size_t)Data.Size, sizeof(ImGuiStoragePair), PairComparerByID); } int ImGuiStorage::GetInt(ImGuiID key, int default_val) const { - ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(const_cast(Data.Data), const_cast(Data.Data + Data.Size), key); + if (it == Data.Data + Data.Size || it->key != key) return default_val; return it->val_i; } @@ -2464,16 +2601,16 @@ bool ImGuiStorage::GetBool(ImGuiID key, bool default_val) const float ImGuiStorage::GetFloat(ImGuiID key, float default_val) const { - ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(const_cast(Data.Data), const_cast(Data.Data + Data.Size), key); + if (it == Data.Data + Data.Size || it->key != key) return default_val; return it->val_f; } void* ImGuiStorage::GetVoidPtr(ImGuiID key) const { - ImGuiStoragePair* it = LowerBound(const_cast&>(Data), key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(const_cast(Data.Data), const_cast(Data.Data + Data.Size), key); + if (it == Data.Data + Data.Size || it->key != key) return NULL; return it->val_p; } @@ -2481,8 +2618,8 @@ void* ImGuiStorage::GetVoidPtr(ImGuiID key) const // References are only valid until a new value is added to the storage. Calling a Set***() function or a Get***Ref() function invalidates the pointer. int* ImGuiStorage::GetIntRef(ImGuiID key, int default_val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) it = Data.insert(it, ImGuiStoragePair(key, default_val)); return &it->val_i; } @@ -2494,16 +2631,16 @@ bool* ImGuiStorage::GetBoolRef(ImGuiID key, bool default_val) float* ImGuiStorage::GetFloatRef(ImGuiID key, float default_val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) it = Data.insert(it, ImGuiStoragePair(key, default_val)); return &it->val_f; } void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) it = Data.insert(it, ImGuiStoragePair(key, default_val)); return &it->val_p; } @@ -2511,8 +2648,8 @@ void** ImGuiStorage::GetVoidPtrRef(ImGuiID key, void* default_val) // FIXME-OPT: Need a way to reuse the result of lower_bound when doing GetInt()/SetInt() - not too bad because it only happens on explicit interaction (maximum one a frame) void ImGuiStorage::SetInt(ImGuiID key, int val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) Data.insert(it, ImGuiStoragePair(key, val)); else it->val_i = val; @@ -2525,8 +2662,8 @@ void ImGuiStorage::SetBool(ImGuiID key, bool val) void ImGuiStorage::SetFloat(ImGuiID key, float val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) Data.insert(it, ImGuiStoragePair(key, val)); else it->val_f = val; @@ -2534,8 +2671,8 @@ void ImGuiStorage::SetFloat(ImGuiID key, float val) void ImGuiStorage::SetVoidPtr(ImGuiID key, void* val) { - ImGuiStoragePair* it = LowerBound(Data, key); - if (it == Data.end() || it->key != key) + ImGuiStoragePair* it = ImLowerBound(Data.Data, Data.Data + Data.Size, key); + if (it == Data.Data + Data.Size || it->key != key) Data.insert(it, ImGuiStoragePair(key, val)); else it->val_p = val; @@ -2546,6 +2683,7 @@ void ImGuiStorage::SetAllInt(int v) for (int i = 0; i < Data.Size; i++) Data[i].val_i = v; } +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] ImGuiTextFilter @@ -2613,15 +2751,15 @@ void ImGuiTextFilter::Build() bool ImGuiTextFilter::PassFilter(const char* text, const char* text_end) const { - if (Filters.empty()) + if (Filters.Size == 0) return true; if (text == NULL) - text = ""; + text = text_end = ""; for (const ImGuiTextRange& f : Filters) { - if (f.empty()) + if (f.b == f.e) continue; if (f.b[0] == '-') { @@ -2788,15 +2926,6 @@ static void ImGuiListClipper_SeekCursorAndSetupPrevLine(float pos_y, float line_ } } -static void ImGuiListClipper_SeekCursorForItem(ImGuiListClipper* clipper, int item_n) -{ - // StartPosY starts from ItemsFrozen hence the subtraction - // Perform the add and multiply with double to allow seeking through larger ranges - ImGuiListClipperData* data = (ImGuiListClipperData*)clipper->TempData; - float pos_y = (float)((double)clipper->StartPosY + data->LossynessOffset + (double)(item_n - data->ItemsFrozen) * clipper->ItemsHeight); - ImGuiListClipper_SeekCursorAndSetupPrevLine(pos_y, clipper->ItemsHeight); -} - ImGuiListClipper::ImGuiListClipper() { memset(this, 0, sizeof(*this)); @@ -2833,6 +2962,7 @@ void ImGuiListClipper::Begin(int items_count, float items_height) data->Reset(this); data->LossynessOffset = window->DC.CursorStartPosLossyness.y; TempData = data; + StartSeekOffsetY = data->LossynessOffset; } void ImGuiListClipper::End() @@ -2843,7 +2973,7 @@ void ImGuiListClipper::End() ImGuiContext& g = *Ctx; IMGUI_DEBUG_LOG_CLIPPER("Clipper: End() in '%s'\n", g.CurrentWindow->Name); if (ItemsCount >= 0 && ItemsCount < INT_MAX && DisplayStart >= 0) - ImGuiListClipper_SeekCursorForItem(this, ItemsCount); + SeekCursorForItem(ItemsCount); // Restore temporary buffer and fix back pointers which may be invalidated when nesting IM_ASSERT(data->ListClipper == this); @@ -2867,6 +2997,17 @@ void ImGuiListClipper::IncludeItemsByIndex(int item_begin, int item_end) data->Ranges.push_back(ImGuiListClipperRange::FromIndices(item_begin, item_end)); } +// This is already called while stepping. +// The ONLY reason you may want to call this is if you passed INT_MAX to ImGuiListClipper::Begin() because you couldn't step item count beforehand. +void ImGuiListClipper::SeekCursorForItem(int item_n) +{ + // - Perform the add and multiply with double to allow seeking through larger ranges. + // - StartPosY starts from ItemsFrozen, by adding SeekOffsetY we generally cancel that out (SeekOffsetY == LossynessOffset - ItemsFrozen * ItemsHeight). + // - The reason we store SeekOffsetY instead of inferring it, is because we want to allow user to perform Seek after the last step, where ImGuiListClipperData is already done. + float pos_y = (float)((double)StartPosY + StartSeekOffsetY + (double)item_n * ItemsHeight); + ImGuiListClipper_SeekCursorAndSetupPrevLine(pos_y, ItemsHeight); +} + static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) { ImGuiContext& g = *clipper->Ctx; @@ -2921,7 +3062,8 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) bool affected_by_floating_point_precision = ImIsFloatAboveGuaranteedIntegerPrecision(clipper->StartPosY) || ImIsFloatAboveGuaranteedIntegerPrecision(window->DC.CursorPos.y); if (affected_by_floating_point_precision) clipper->ItemsHeight = window->DC.PrevLineSize.y + g.Style.ItemSpacing.y; // FIXME: Technically wouldn't allow multi-line entries. - + if (clipper->ItemsHeight == 0.0f && clipper->ItemsCount == INT_MAX) // Accept that no item have been submitted if in indeterminate mode. + return false; IM_ASSERT(clipper->ItemsHeight > 0.0f && "Unable to calculate item height! First item hasn't moved the cursor vertically!"); calc_clipping = true; // If item height had to be calculated, calculate clipping afterwards. } @@ -2930,6 +3072,9 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) const int already_submitted = clipper->DisplayEnd; if (calc_clipping) { + // Record seek offset, this is so ImGuiListClipper::Seek() can be called after ImGuiListClipperData is done + clipper->StartSeekOffsetY = (double)data->LossynessOffset - data->ItemsFrozen * (double)clipper->ItemsHeight; + if (g.LogEnabled) { // If logging is active, do not perform any clipping @@ -2950,9 +3095,27 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) data->Ranges.push_back(ImGuiListClipperRange::FromPositions(nav_rect_abs.Min.y, nav_rect_abs.Max.y, 0, 0)); // Add visible range + float min_y = window->ClipRect.Min.y; + float max_y = window->ClipRect.Max.y; + + // Add box selection range + ImGuiBoxSelectState* bs = &g.BoxSelectState; + if (bs->IsActive && bs->Window == window) + { + // FIXME: Selectable() use of half-ItemSpacing isn't consistent in matter of layout, as ItemAdd(bb) stray above ItemSize()'s CursorPos. + // RangeSelect's BoxSelect relies on comparing overlap of previous and current rectangle and is sensitive to that. + // As a workaround we currently half ItemSpacing worth on each side. + min_y -= g.Style.ItemSpacing.y; + max_y += g.Style.ItemSpacing.y; + + // Box-select on 2D area requires different clipping. + if (bs->UnclipMode) + data->Ranges.push_back(ImGuiListClipperRange::FromPositions(bs->UnclipRect.Min.y, bs->UnclipRect.Max.y, 0, 0)); + } + const int off_min = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Up) ? -1 : 0; const int off_max = (is_nav_request && g.NavMoveClipDir == ImGuiDir_Down) ? 1 : 0; - data->Ranges.push_back(ImGuiListClipperRange::FromPositions(window->ClipRect.Min.y, window->ClipRect.Max.y, off_min, off_max)); + data->Ranges.push_back(ImGuiListClipperRange::FromPositions(min_y, max_y, off_min, off_max)); } // Convert position ranges to item index ranges @@ -2977,7 +3140,7 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) clipper->DisplayStart = ImMax(data->Ranges[data->StepNo].Min, already_submitted); clipper->DisplayEnd = ImMin(data->Ranges[data->StepNo].Max, clipper->ItemsCount); if (clipper->DisplayStart > already_submitted) //-V1051 - ImGuiListClipper_SeekCursorForItem(clipper, clipper->DisplayStart); + clipper->SeekCursorForItem(clipper->DisplayStart); data->StepNo++; if (clipper->DisplayStart == clipper->DisplayEnd && data->StepNo < data->Ranges.Size) continue; @@ -2987,7 +3150,7 @@ static bool ImGuiListClipper_StepInternal(ImGuiListClipper* clipper) // After the last step: Let the clipper validate that we have reached the expected Y position (corresponding to element DisplayEnd), // Advance the cursor to the end of the list and then returns 'false' to end the loop. if (clipper->ItemsCount < INT_MAX) - ImGuiListClipper_SeekCursorForItem(clipper, clipper->ItemsCount); + clipper->SeekCursorForItem(clipper->ItemsCount); return false; } @@ -3100,35 +3263,39 @@ void ImGui::PopStyleColor(int count) static const ImGuiDataVarInfo GStyleVarInfo[] = { - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign - { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, SeparatorTextBorderSize)},// ImGuiStyleVar_SeparatorTextBorderSize - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextAlign) }, // ImGuiStyleVar_SeparatorTextAlign - { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextPadding) }, // ImGuiStyleVar_SeparatorTextPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, Alpha) }, // ImGuiStyleVar_Alpha + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, DisabledAlpha) }, // ImGuiStyleVar_DisabledAlpha + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowPadding) }, // ImGuiStyleVar_WindowPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowRounding) }, // ImGuiStyleVar_WindowRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, WindowBorderSize) }, // ImGuiStyleVar_WindowBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowMinSize) }, // ImGuiStyleVar_WindowMinSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, WindowTitleAlign) }, // ImGuiStyleVar_WindowTitleAlign + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildRounding) }, // ImGuiStyleVar_ChildRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ChildBorderSize) }, // ImGuiStyleVar_ChildBorderSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupRounding) }, // ImGuiStyleVar_PopupRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, PopupBorderSize) }, // ImGuiStyleVar_PopupBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, FramePadding) }, // ImGuiStyleVar_FramePadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameRounding) }, // ImGuiStyleVar_FrameRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, FrameBorderSize) }, // ImGuiStyleVar_FrameBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemSpacing) }, // ImGuiStyleVar_ItemSpacing + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ItemInnerSpacing) }, // ImGuiStyleVar_ItemInnerSpacing + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, IndentSpacing) }, // ImGuiStyleVar_IndentSpacing + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, CellPadding) }, // ImGuiStyleVar_CellPadding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBarOverlineSize) }, // ImGuiStyleVar_TabBarOverlineSize + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TableAngledHeadersAngle)}, // ImGuiStyleVar_TableAngledHeadersAngle + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, TableAngledHeadersTextAlign)},// ImGuiStyleVar_TableAngledHeadersTextAlign + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, ButtonTextAlign) }, // ImGuiStyleVar_ButtonTextAlign + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SelectableTextAlign) }, // ImGuiStyleVar_SelectableTextAlign + { ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, SeparatorTextBorderSize)}, // ImGuiStyleVar_SeparatorTextBorderSize + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextAlign) }, // ImGuiStyleVar_SeparatorTextAlign + { ImGuiDataType_Float, 2, (ImU32)offsetof(ImGuiStyle, SeparatorTextPadding) }, // ImGuiStyleVar_SeparatorTextPadding }; const ImGuiDataVarInfo* ImGui::GetStyleVarInfo(ImGuiStyleVar idx) @@ -3225,11 +3392,13 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) case ImGuiCol_ResizeGrip: return "ResizeGrip"; case ImGuiCol_ResizeGripHovered: return "ResizeGripHovered"; case ImGuiCol_ResizeGripActive: return "ResizeGripActive"; - case ImGuiCol_Tab: return "Tab"; case ImGuiCol_TabHovered: return "TabHovered"; - case ImGuiCol_TabActive: return "TabActive"; - case ImGuiCol_TabUnfocused: return "TabUnfocused"; - case ImGuiCol_TabUnfocusedActive: return "TabUnfocusedActive"; + case ImGuiCol_Tab: return "Tab"; + case ImGuiCol_TabSelected: return "TabSelected"; + case ImGuiCol_TabSelectedOverline: return "TabSelectedOverline"; + case ImGuiCol_TabDimmed: return "TabDimmed"; + case ImGuiCol_TabDimmedSelected: return "TabDimmedSelected"; + case ImGuiCol_TabDimmedSelectedOverline: return "TabDimmedSelectedOverline"; case ImGuiCol_PlotLines: return "PlotLines"; case ImGuiCol_PlotLinesHovered: return "PlotLinesHovered"; case ImGuiCol_PlotHistogram: return "PlotHistogram"; @@ -3239,6 +3408,7 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) case ImGuiCol_TableBorderLight: return "TableBorderLight"; case ImGuiCol_TableRowBg: return "TableRowBg"; case ImGuiCol_TableRowBgAlt: return "TableRowBgAlt"; + case ImGuiCol_TextLink: return "TextLink"; case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; case ImGuiCol_DragDropTarget: return "DragDropTarget"; case ImGuiCol_NavHighlight: return "NavHighlight"; @@ -3384,7 +3554,7 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con const ImFont* font = draw_list->_Data->Font; const float font_size = draw_list->_Data->FontSize; - const float font_scale = font_size / font->FontSize; + const float font_scale = draw_list->_Data->FontScale; const char* text_end_ellipsis = NULL; const float ellipsis_width = font->EllipsisWidth * font_scale; @@ -3561,7 +3731,7 @@ void ImGui::DestroyContext(ImGuiContext* ctx) IM_DELETE(ctx); } -// IMPORTANT: ###xxx suffixes must be same in ALL languages +// IMPORTANT: ###xxx suffixes must be same in ALL languages to allow for automation. static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { { ImGuiLocKey_VersionStr, "Dear ImGui " IMGUI_VERSION " (" IM_STRINGIFY(IMGUI_VERSION_NUM) ")" }, @@ -3572,6 +3742,7 @@ static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { ImGuiLocKey_WindowingMainMenuBar, "(Main menu bar)" }, { ImGuiLocKey_WindowingPopup, "(Popup)" }, { ImGuiLocKey_WindowingUntitled, "(Untitled)" }, + { ImGuiLocKey_CopyLink, "Copy Link###CopyLink" }, }; void ImGui::Initialize() @@ -3600,7 +3771,8 @@ void ImGui::Initialize() g.IO.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations g.IO.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; g.IO.ClipboardUserData = (void*)&g; // Default implementation use the ImGuiContext as user data (ideally those would be arguments to the function) - g.IO.SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl; + g.IO.PlatformOpenInShellFn = PlatformOpenInShellFn_DefaultImpl; + g.IO.PlatformSetImeDataFn = PlatformSetImeDataFn_DefaultImpl; // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); @@ -3667,7 +3839,7 @@ void ImGui::Shutdown() g.FontStack.clear(); g.OpenPopupStack.clear(); g.BeginPopupStack.clear(); - g.NavTreeNodeStack.clear(); + g.TreeNodeStack.clear(); g.Viewports.clear_delete(); @@ -3681,6 +3853,9 @@ void ImGui::Shutdown() g.TablesTempData.clear_destruct(); g.DrawChannelsTempMergeBuffer.clear(); + g.MultiSelectStorage.Clear(); + g.MultiSelectTempData.clear_destruct(); + g.ClipboardHandlerData.clear(); g.MenusIdSubmittedThisFrame.clear(); g.InputTextState.ClearFreeMemory(); @@ -3772,53 +3947,16 @@ ImGuiWindow::~ImGuiWindow() ColumnsStorage.clear_destruct(); } -ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) -{ - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); - ImGuiContext& g = *Ctx; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); - return id; -} - -ImGuiID ImGuiWindow::GetID(const void* ptr) -{ - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); - ImGuiContext& g = *Ctx; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); - return id; -} - -ImGuiID ImGuiWindow::GetID(int n) -{ - ImGuiID seed = IDStack.back(); - ImGuiID id = ImHashData(&n, sizeof(n), seed); - ImGuiContext& g = *Ctx; - if (g.DebugHookIdInfo == id) - ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); - return id; -} - -// This is only used in rare/specific situations to manufacture an ID out of nowhere. -ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) -{ - ImGuiID seed = IDStack.back(); - ImRect r_rel = ImGui::WindowRectAbsToRel(this, r_abs); - ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); - return id; -} - static void SetCurrentWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; g.CurrentWindow = window; g.CurrentTable = window && window->DC.CurrentTableIdx != -1 ? g.Tables.GetByIndex(window->DC.CurrentTableIdx) : NULL; + g.CurrentDpiScale = 1.0f; // FIXME-DPI: WIP this is modified in docking if (window) { g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); + g.FontScale = g.FontSize / g.Font->FontSize; ImGui::NavUpdateCurrentWindowIsScrollPushableX(); } } @@ -3828,6 +3966,8 @@ void ImGui::GcCompactTransientMiscBuffers() ImGuiContext& g = *GImGui; g.ItemFlagsStack.clear(); g.GroupStack.clear(); + g.MultiSelectTempDataStacked = 0; + g.MultiSelectTempData.clear_destruct(); TableGcCompactSettings(); } @@ -3912,9 +4052,6 @@ void ImGui::SetActiveID(ImGuiID id, ImGuiWindow* window) // (Please note that this is WIP and not all keys/inputs are thoroughly declared by all widgets yet) g.ActiveIdUsingNavDirMask = 0x00; g.ActiveIdUsingAllKeyboardKeys = false; -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - g.ActiveIdUsingNavInputMask = 0x00; -#endif } void ImGui::ClearActiveID() @@ -3952,7 +4089,7 @@ void ImGui::MarkItemEdited(ImGuiID id) // We accept a MarkItemEdited() on drag and drop targets (see https://github.com/ocornut/imgui/issues/1875#issuecomment-978243343) // We accept 'ActiveIdPreviousFrame == id' for InputText() returning an edit after it has been taken ActiveId away (#4714) - IM_ASSERT(g.DragDropActive || g.ActiveId == id || g.ActiveId == 0 || g.ActiveIdPreviousFrame == id); + IM_ASSERT(g.DragDropActive || g.ActiveId == id || g.ActiveId == 0 || g.ActiveIdPreviousFrame == id || (g.CurrentMultiSelect != NULL && g.BoxSelectState.IsActive)); //IM_ASSERT(g.CurrentWindow->DC.LastItemId == id); g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_Edited; @@ -4116,7 +4253,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag // Done with rectangle culling so we can perform heavier checks now. if (!(item_flags & ImGuiItemFlags_NoWindowHoverableCheck) && !IsWindowContentHoverable(window, ImGuiHoveredFlags_None)) { - g.HoveredIdDisabled = true; + g.HoveredIdIsDisabled = true; return false; } @@ -4138,6 +4275,12 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag if (g.HoveredIdPreviousFrame != id) return false; } + + // Display shortcut (only works with mouse) + // (ImGuiItemStatusFlags_HasShortcut in LastItemData denotes we want a tooltip) + if (id == g.LastItemData.ID && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasShortcut)) + if (IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_DelayNormal)) + SetTooltip("%s", GetKeyChordName(g.LastItemData.Shortcut)); } // When disabled we'll return false but still set HoveredId @@ -4146,7 +4289,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag // Release active id if turning disabled if (g.ActiveId == id && id != 0) ClearActiveID(); - g.HoveredIdDisabled = true; + g.HoveredIdIsDisabled = true; return false; } @@ -4178,7 +4321,7 @@ bool ImGui::IsClippedEx(const ImRect& bb, ImGuiID id) ImGuiWindow* window = g.CurrentWindow; if (!bb.Overlaps(window->ClipRect)) if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId && id != g.NavActivateId)) - if (!g.LogEnabled) + if (!g.ItemUnclipByLog) return true; return false; } @@ -4449,7 +4592,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() g.MovingWindow = NULL; // Cancel moving if clicked over an item which was disabled or inhibited by popups (note that we know HoveredId == 0 already) - if (g.HoveredIdDisabled) + if (g.HoveredIdIsDisabled) g.MovingWindow = NULL; } else if (root_window == NULL && g.NavWindow != NULL) @@ -4482,6 +4625,9 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() { ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; + + // FIXME-DPI: This storage was added on 2021/03/31 for test engine, but if we want to multiply WINDOWS_HOVER_PADDING + // by DpiScale, we need to make this window-agnostic anyhow, maybe need storing inside ImGuiWindow. g.WindowsHoverPadding = ImMax(g.Style.TouchExtraPadding, ImVec2(WINDOWS_HOVER_PADDING, WINDOWS_HOVER_PADDING)); // Find the window hovered by mouse: @@ -4489,14 +4635,15 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() // - When moving a window we can skip the search, which also conveniently bypasses the fact that window->WindowRectClipped is lagging as this point of the frame. // - We also support the moved window toggling the NoInputs flag after moving has started in order to be able to detect windows below it, which is useful for e.g. docking mechanisms. bool clear_hovered_windows = false; - FindHoveredWindow(); + FindHoveredWindowEx(g.IO.MousePos, false, &g.HoveredWindow, &g.HoveredWindowUnderMovingWindow); + g.HoveredWindowBeforeClear = g.HoveredWindow; // Modal windows prevents mouse from hovering behind them. ImGuiWindow* modal_window = GetTopMostPopupModal(); if (modal_window && g.HoveredWindow && !IsWindowWithinBeginStackOf(g.HoveredWindow->RootWindow, modal_window)) clear_hovered_windows = true; - // Disabled mouse? + // Disabled mouse hovering (we don't currently clear MousePos, we could) if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) clear_hovered_windows = true; @@ -4514,7 +4661,7 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() io.MouseDownOwnedUnlessPopupClose[i] = (g.HoveredWindow != NULL) || has_open_modal; } mouse_any_down |= io.MouseDown[i]; - if (io.MouseDown[i]) + if (io.MouseDown[i] || io.MouseReleased[i]) // Increase release frame for our evaluation of earliest button (#1392) if (mouse_earliest_down == -1 || io.MouseClickedTime[i] < io.MouseClickedTime[mouse_earliest_down]) mouse_earliest_down = i; } @@ -4553,6 +4700,27 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() io.WantTextInput = (g.WantTextInputNextFrame != -1) ? (g.WantTextInputNextFrame != 0) : false; } +// Calling SetupDrawListSharedData() is followed by SetCurrentFont() which sets up the remaining data. +static void SetupDrawListSharedData() +{ + ImGuiContext& g = *GImGui; + ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); + for (ImGuiViewportP* viewport : g.Viewports) + virtual_space.Add(viewport->GetMainRect()); + g.DrawListSharedData.ClipRectFullscreen = virtual_space.ToVec4(); + g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; + g.DrawListSharedData.SetCircleTessellationMaxError(g.Style.CircleTessellationMaxError); + g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; + if (g.Style.AntiAliasedLines) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; + if (g.Style.AntiAliasedLinesUseTex && !(g.IO.Fonts->Flags & ImFontAtlasFlags_NoBakedLines)) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLinesUseTex; + if (g.Style.AntiAliasedFill) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedFill; + if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) + g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; +} + void ImGui::NewFrame() { IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?"); @@ -4595,23 +4763,9 @@ void ImGui::NewFrame() // Setup current font and draw list shared data g.IO.Fonts->Locked = true; + SetupDrawListSharedData(); SetCurrentFont(GetDefaultFont()); IM_ASSERT(g.Font->IsLoaded()); - ImRect virtual_space(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); - for (ImGuiViewportP* viewport : g.Viewports) - virtual_space.Add(viewport->GetMainRect()); - g.DrawListSharedData.ClipRectFullscreen = virtual_space.ToVec4(); - g.DrawListSharedData.CurveTessellationTol = g.Style.CurveTessellationTol; - g.DrawListSharedData.SetCircleTessellationMaxError(g.Style.CircleTessellationMaxError); - g.DrawListSharedData.InitialFlags = ImDrawListFlags_None; - if (g.Style.AntiAliasedLines) - g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLines; - if (g.Style.AntiAliasedLinesUseTex && !(g.Font->ContainerAtlas->Flags & ImFontAtlasFlags_NoBakedLines)) - g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedLinesUseTex; - if (g.Style.AntiAliasedFill) - g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AntiAliasedFill; - if (g.IO.BackendFlags & ImGuiBackendFlags_RendererHasVtxOffset) - g.DrawListSharedData.InitialFlags |= ImDrawListFlags_AllowVtxOffset; // Mark rendering data as invalid to prevent user who may have a handle on it to use it. for (ImGuiViewportP* viewport : g.Viewports) @@ -4633,7 +4787,7 @@ void ImGui::NewFrame() g.HoveredIdPreviousFrame = g.HoveredId; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; - g.HoveredIdDisabled = false; + g.HoveredIdIsDisabled = false; // Clear ActiveID if the item is not alive anymore. // In 1.87, the common most call to KeepAliveID() was moved from GetID() to ItemAdd(). @@ -4661,24 +4815,7 @@ void ImGui::NewFrame() { g.ActiveIdUsingNavDirMask = 0x00; g.ActiveIdUsingAllKeyboardKeys = false; -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - g.ActiveIdUsingNavInputMask = 0x00; -#endif - } - -#ifndef IMGUI_DISABLE_OBSOLETE_KEYIO - if (g.ActiveId == 0) - g.ActiveIdUsingNavInputMask = 0; - else if (g.ActiveIdUsingNavInputMask != 0) - { - // If your custom widget code used: { g.ActiveIdUsingNavInputMask |= (1 << ImGuiNavInput_Cancel); } - // Since IMGUI_VERSION_NUM >= 18804 it should be: { SetKeyOwner(ImGuiKey_Escape, g.ActiveId); SetKeyOwner(ImGuiKey_NavGamepadCancel, g.ActiveId); } - if (g.ActiveIdUsingNavInputMask & (1 << ImGuiNavInput_Cancel)) - SetKeyOwner(ImGuiKey_Escape, g.ActiveId); - if (g.ActiveIdUsingNavInputMask & ~(1 << ImGuiNavInput_Cancel)) - IM_ASSERT(0); // Other values unsupported } -#endif // Record when we have been stationary as this state is preserved while over same item. // FIXME: The way this is expressed means user cannot alter HoverStationaryDelay during the frame to use varying values. @@ -4794,7 +4931,8 @@ void ImGui::NewFrame() g.CurrentWindowStack.resize(0); g.BeginPopupStack.resize(0); g.ItemFlagsStack.resize(0); - g.ItemFlagsStack.push_back(ImGuiItemFlags_None); + g.ItemFlagsStack.push_back(ImGuiItemFlags_AutoClosePopups); // Default flags + g.CurrentItemFlags = g.ItemFlagsStack.back(); g.GroupStack.resize(0); // [DEBUG] Update debug features @@ -5043,11 +5181,11 @@ void ImGui::EndFrame() // Notify Platform/OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) ImGuiPlatformImeData* ime_data = &g.PlatformImeData; - if (g.IO.SetPlatformImeDataFn && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) + if (g.IO.PlatformSetImeDataFn != NULL && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) { - IMGUI_DEBUG_LOG_IO("[io] Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); + IMGUI_DEBUG_LOG_IO("[io] Calling io.PlatformSetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); ImGuiViewport* viewport = GetMainViewport(); - g.IO.SetPlatformImeDataFn(viewport, ime_data); + g.IO.PlatformSetImeDataFn(&g, viewport, ime_data); } // Hide implicit/fallback "Debug" window if it hasn't been used @@ -5063,12 +5201,16 @@ void ImGui::EndFrame() if (g.DragDropActive) { bool is_delivered = g.DragDropPayload.Delivery; - bool is_elapsed = (g.DragDropPayload.DataFrameCount + 1 < g.FrameCount) && ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceAutoExpirePayload) || !IsMouseDown(g.DragDropMouseButton)); + bool is_elapsed = (g.DragDropSourceFrameCount + 1 < g.FrameCount) && ((g.DragDropSourceFlags & ImGuiDragDropFlags_PayloadAutoExpire) || g.DragDropMouseButton == -1 || !IsMouseDown(g.DragDropMouseButton)); if (is_delivered || is_elapsed) ClearDragDrop(); } - // Drag and Drop: Fallback for source tooltip. This is not ideal but better than nothing. + // Drag and Drop: Fallback for missing source tooltip. This is not ideal but better than nothing. + // If you want to handle source item disappearing: instead of submitting your description tooltip + // in the BeginDragDropSource() block of the dragged item, you can submit them from a safe single spot + // (e.g. end of your item loop, or before EndFrame) by reading payload data. + // In the typical case, the contents of drag tooltip should be possible to infer solely from payload data. if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) { g.DragDropWithinSource = true; @@ -5209,16 +5351,18 @@ ImVec2 ImGui::CalcTextSize(const char* text, const char* text_end, bool hide_tex } // Find window given position, search front-to-back -// FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programmatically -// with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is -// called, aka before the next Begin(). Moving window isn't affected. -static void FindHoveredWindow() +// - Typically write output back to g.HoveredWindow and g.HoveredWindowUnderMovingWindow. +// - FIXME: Note that we have an inconsequential lag here: OuterRectClipped is updated in Begin(), so windows moved programmatically +// with SetWindowPos() and not SetNextWindowPos() will have that rectangle lagging by a frame at the time FindHoveredWindow() is +// called, aka before the next Begin(). Moving window isn't affected. +// - The 'find_first_and_in_any_viewport = true' mode is only used by TestEngine. It is simpler to maintain here. +void ImGui::FindHoveredWindowEx(const ImVec2& pos, bool find_first_and_in_any_viewport, ImGuiWindow** out_hovered_window, ImGuiWindow** out_hovered_window_under_moving_window) { ImGuiContext& g = *GImGui; - ImGuiWindow* hovered_window = NULL; - ImGuiWindow* hovered_window_ignoring_moving_window = NULL; - if (g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs)) + ImGuiWindow* hovered_window_under_moving_window = NULL; + + if (find_first_and_in_any_viewport == false && g.MovingWindow && !(g.MovingWindow->Flags & ImGuiWindowFlags_NoMouseInputs)) hovered_window = g.MovingWindow; ImVec2 padding_regular = g.Style.TouchExtraPadding; @@ -5234,7 +5378,7 @@ static void FindHoveredWindow() // Using the clipped AABB, a child window will typically be clipped by its parent (not always) ImVec2 hit_padding = (window->Flags & (ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize)) ? padding_regular : padding_for_resize; - if (!window->OuterRectClipped.ContainsWithPad(g.IO.MousePos, hit_padding)) + if (!window->OuterRectClipped.ContainsWithPad(pos, hit_padding)) continue; // Support for one rectangular hole in any given window @@ -5243,21 +5387,30 @@ static void FindHoveredWindow() { ImVec2 hole_pos(window->Pos.x + (float)window->HitTestHoleOffset.x, window->Pos.y + (float)window->HitTestHoleOffset.y); ImVec2 hole_size((float)window->HitTestHoleSize.x, (float)window->HitTestHoleSize.y); - if (ImRect(hole_pos, hole_pos + hole_size).Contains(g.IO.MousePos)) + if (ImRect(hole_pos, hole_pos + hole_size).Contains(pos)) continue; } - if (hovered_window == NULL) + if (find_first_and_in_any_viewport) + { hovered_window = window; - IM_MSVC_WARNING_SUPPRESS(28182); // [Static Analyzer] Dereferencing NULL pointer. - if (hovered_window_ignoring_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow)) - hovered_window_ignoring_moving_window = window; - if (hovered_window && hovered_window_ignoring_moving_window) break; + } + else + { + if (hovered_window == NULL) + hovered_window = window; + IM_MSVC_WARNING_SUPPRESS(28182); // [Static Analyzer] Dereferencing NULL pointer. + if (hovered_window_under_moving_window == NULL && (!g.MovingWindow || window->RootWindow != g.MovingWindow->RootWindow)) + hovered_window_under_moving_window = window; + if (hovered_window && hovered_window_under_moving_window) + break; + } } - g.HoveredWindow = hovered_window; - g.HoveredWindowUnderMovingWindow = hovered_window_ignoring_moving_window; + *out_hovered_window = hovered_window; + if (out_hovered_window_under_moving_window != NULL) + *out_hovered_window_under_moving_window = hovered_window_under_moving_window; } bool ImGui::IsItemActive() @@ -5313,9 +5466,15 @@ bool ImGui::IsItemToggledOpen() return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledOpen) ? true : false; } +// Call after a Selectable() or TreeNode() involved in multi-selection. +// Useful if you need the per-item information before reaching EndMultiSelect(), e.g. for rendering purpose. +// This is only meant to be called inside a BeginMultiSelect()/EndMultiSelect() block. +// (Outside of multi-select, it would be misleading/ambiguous to report this signal, as widgets +// return e.g. a pressed event and user code is in charge of altering selection in ways we cannot predict.) bool ImGui::IsItemToggledSelection() { ImGuiContext& g = *GImGui; + IM_ASSERT(g.CurrentMultiSelect != NULL); // Can only be used inside a BeginMultiSelect()/EndMultiSelect() return (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_ToggledSelection) ? true : false; } @@ -5375,7 +5534,8 @@ void ImGui::SetItemAllowOverlap() } #endif -// FIXME: It might be undesirable that this will likely disable KeyOwner-aware shortcuts systems. Consider a more fine-tuned version for the two users of this function. +// This is a shortcut for not taking ownership of 100+ keys, frequently used by drag operations. +// FIXME: It might be undesirable that this will likely disable KeyOwner-aware shortcuts systems. Consider a more fine-tuned version if needed? void ImGui::SetActiveIdUsingAllKeyboardKeys() { ImGuiContext& g = *GImGui; @@ -5429,7 +5589,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I IM_ASSERT(id != 0); // Sanity check as it is likely that some user will accidentally pass ImGuiWindowFlags into the ImGuiChildFlags argument. - const ImGuiChildFlags ImGuiChildFlags_SupportedMask_ = ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_FrameStyle; + const ImGuiChildFlags ImGuiChildFlags_SupportedMask_ = ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_FrameStyle | ImGuiChildFlags_NavFlattened; IM_UNUSED(ImGuiChildFlags_SupportedMask_); IM_ASSERT((child_flags & ~ImGuiChildFlags_SupportedMask_) == 0 && "Illegal ImGuiChildFlags value. Did you pass ImGuiWindowFlags values instead of ImGuiChildFlags?"); IM_ASSERT((window_flags & ImGuiWindowFlags_AlwaysAutoResize) == 0 && "Cannot specify ImGuiWindowFlags_AlwaysAutoResize for BeginChild(). Use ImGuiChildFlags_AlwaysAutoResize!"); @@ -5441,6 +5601,8 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS if (window_flags & ImGuiWindowFlags_AlwaysUseWindowPadding) child_flags |= ImGuiChildFlags_AlwaysUseWindowPadding; + if (window_flags & ImGuiWindowFlags_NavFlattened) + child_flags |= ImGuiChildFlags_NavFlattened; #endif if (child_flags & ImGuiChildFlags_AutoResizeX) child_flags &= ~ImGuiChildFlags_ResizeX; @@ -5519,7 +5681,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I const ImGuiID temp_id_for_activation = ImHashStr("##Child", 0, id); if (g.ActiveId == temp_id_for_activation) ClearActiveID(); - if (g.NavActivateId == id && !(window_flags & ImGuiWindowFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY)) + if (g.NavActivateId == id && !(child_flags & ImGuiChildFlags_NavFlattened) && (child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY)) { FocusWindow(child_window); NavInitWindow(child_window, false); @@ -5545,7 +5707,8 @@ void ImGui::EndChild() ImGuiWindow* parent_window = g.CurrentWindow; ImRect bb(parent_window->DC.CursorPos, parent_window->DC.CursorPos + child_size); ItemSize(child_size); - if ((child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY) && !(child_window->Flags & ImGuiWindowFlags_NavFlattened)) + const bool nav_flattened = (child_window->ChildFlags & ImGuiChildFlags_NavFlattened) != 0; + if ((child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY) && !nav_flattened) { ItemAdd(bb, child_window->ChildId); RenderNavHighlight(bb, child_window->ChildId); @@ -5557,10 +5720,12 @@ void ImGui::EndChild() else { // Not navigable into - ItemAdd(bb, 0); + // - This is a bit of a fringe use case, mostly useful for undecorated, non-scrolling contents childs, or empty childs. + // - We could later decide to not apply this path if ImGuiChildFlags_FrameStyle or ImGuiChildFlags_Borders is set. + ItemAdd(bb, child_window->ChildId, NULL, ImGuiItemFlags_NoNav); // But when flattened we directly reach items, adjust active layer mask accordingly - if (child_window->Flags & ImGuiWindowFlags_NavFlattened) + if (nav_flattened) parent_window->DC.NavLayersActiveMaskNext |= child_window->DC.NavLayersActiveMaskNext; } if (g.HoveredWindow == child_window) @@ -5695,7 +5860,7 @@ static inline ImVec2 CalcWindowMinSize(ImGuiWindow* window) // Reduce artifacts with very small windows ImGuiWindow* window_for_height = window; - size_min.y = ImMax(size_min.y, window_for_height->TitleBarHeight() + window_for_height->MenuBarHeight() + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); + size_min.y = ImMax(size_min.y, window_for_height->TitleBarHeight + window_for_height->MenuBarHeight + ImMax(0.0f, g.Style.WindowRounding - 1.0f)); return size_min; } @@ -5768,10 +5933,18 @@ static ImVec2 CalcWindowAutoFitSize(ImGuiWindow* window, const ImVec2& size_cont ImVec2 size_max = ((window->Flags & ImGuiWindowFlags_ChildWindow) && !(window->Flags & ImGuiWindowFlags_Popup)) ? ImVec2(FLT_MAX, FLT_MAX) : ImGui::GetMainViewport()->WorkSize - style.DisplaySafeAreaPadding * 2.0f; ImVec2 size_auto_fit = ImClamp(size_desired, size_min, size_max); + // FIXME: CalcWindowAutoFitSize() doesn't take into account that only one axis may be auto-fit when calculating scrollbars, + // we may need to compute/store three variants of size_auto_fit, for x/y/xy. + // Here we implement a workaround for child windows only, but a full solution would apply to normal windows as well: + if ((window->ChildFlags & ImGuiChildFlags_ResizeX) && !(window->ChildFlags & ImGuiChildFlags_ResizeY)) + size_auto_fit.y = window->SizeFull.y; + else if (!(window->ChildFlags & ImGuiChildFlags_ResizeX) && (window->ChildFlags & ImGuiChildFlags_ResizeY)) + size_auto_fit.x = window->SizeFull.x; + // When the window cannot fit all contents (either because of constraints, either because screen is too small), // we are growing the size on the other axis to compensate for expected scrollbar. FIXME: Might turn bigger than ViewportSize-WindowPadding. ImVec2 size_auto_fit_after_constraint = CalcWindowSizeAfterConstraint(window, size_auto_fit); - bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - decoration_w_without_scrollbars < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); + bool will_have_scrollbar_x = (size_auto_fit_after_constraint.x - size_pad.x - decoration_w_without_scrollbars < size_contents.x && !(window->Flags & ImGuiWindowFlags_NoScrollbar) && (window->Flags & ImGuiWindowFlags_HorizontalScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar); bool will_have_scrollbar_y = (size_auto_fit_after_constraint.y - size_pad.y - decoration_h_without_scrollbars < size_contents.y && !(window->Flags & ImGuiWindowFlags_NoScrollbar)) || (window->Flags & ImGuiWindowFlags_AlwaysVerticalScrollbar); if (will_have_scrollbar_x) size_auto_fit.y += style.ScrollbarSize; @@ -5892,13 +6065,13 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si int ret_auto_fit_mask = 0x00; const float grip_draw_size = IM_TRUNC(ImMax(g.FontSize * 1.35f, window->WindowRounding + 1.0f + g.FontSize * 0.2f)); - const float grip_hover_inner_size = IM_TRUNC(grip_draw_size * 0.75f); + const float grip_hover_inner_size = (resize_grip_count > 0) ? IM_TRUNC(grip_draw_size * 0.75f) : 0.0f; const float grip_hover_outer_size = g.IO.ConfigWindowsResizeFromEdges ? WINDOWS_HOVER_PADDING : 0.0f; ImRect clamp_rect = visibility_rect; const bool window_move_from_title_bar = g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar); if (window_move_from_title_bar) - clamp_rect.Min.y -= window->TitleBarHeight(); + clamp_rect.Min.y -= window->TitleBarHeight; ImVec2 pos_target(FLT_MAX, FLT_MAX); ImVec2 size_target(FLT_MAX, FLT_MAX); @@ -5973,6 +6146,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si if (held && g.IO.MouseDoubleClicked[0]) { // Double-clicking bottom or right border auto-fit on this axis + // FIXME: CalcWindowAutoFitSize() doesn't take into account that only one side may be auto-fit when calculating scrollbars. // FIXME: Support top and right borders: rework CalcResizePosSizeFromAnyCorner() to be reusable in both cases. if (border_n == 1 || border_n == 3) // Right and bottom border { @@ -6020,10 +6194,14 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si border_target = ImClamp(border_target, clamp_min, clamp_max); if (flags & ImGuiWindowFlags_ChildWindow) // Clamp resizing of childs within parent { - if ((flags & (ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar)) == 0 || (flags & ImGuiWindowFlags_NoScrollbar)) - border_target.x = ImClamp(border_target.x, window->ParentWindow->InnerClipRect.Min.x, window->ParentWindow->InnerClipRect.Max.x); - if (flags & ImGuiWindowFlags_NoScrollbar) - border_target.y = ImClamp(border_target.y, window->ParentWindow->InnerClipRect.Min.y, window->ParentWindow->InnerClipRect.Max.y); + ImGuiWindow* parent_window = window->ParentWindow; + ImGuiWindowFlags parent_flags = parent_window->Flags; + ImRect border_limit_rect = parent_window->InnerRect; + border_limit_rect.Expand(ImVec2(-ImMax(parent_window->WindowPadding.x, parent_window->WindowBorderSize), -ImMax(parent_window->WindowPadding.y, parent_window->WindowBorderSize))); + if ((axis == ImGuiAxis_X) && ((parent_flags & (ImGuiWindowFlags_HorizontalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar)) == 0 || (parent_flags & ImGuiWindowFlags_NoScrollbar))) + border_target.x = ImClamp(border_target.x, border_limit_rect.Min.x, border_limit_rect.Max.x); + if ((axis == ImGuiAxis_Y) && (parent_flags & ImGuiWindowFlags_NoScrollbar)) + border_target.y = ImClamp(border_target.y, border_limit_rect.Min.y, border_limit_rect.Max.y); } if (!ignore_resize) CalcResizePosSizeFromAnyCorner(window, border_target, ImMin(def.SegmentN1, def.SegmentN2), &pos_target, &size_target); @@ -6093,7 +6271,7 @@ static inline void ClampWindowPos(ImGuiWindow* window, const ImRect& visibility_ ImGuiContext& g = *GImGui; ImVec2 size_for_clamping = window->Size; if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) - size_for_clamping.y = window->TitleBarHeight(); + size_for_clamping.y = window->TitleBarHeight; window->Pos = ImClamp(window->Pos, visibility_rect.Min - size_for_clamping, visibility_rect.Max); } @@ -6129,7 +6307,7 @@ static void ImGui::RenderWindowOuterBorders(ImGuiWindow* window) } if (g.Style.FrameBorderSize > 0 && !(window->Flags & ImGuiWindowFlags_NoTitleBar)) { - float y = window->Pos.y + window->TitleBarHeight() - 1; + float y = window->Pos.y + window->TitleBarHeight - 1; window->DrawList->AddLine(ImVec2(window->Pos.x + border_size, y), ImVec2(window->Pos.x + window->Size.x - border_size, y), border_col, g.Style.FrameBorderSize); } } @@ -6174,7 +6352,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar } if (override_alpha) bg_col = (bg_col & ~IM_COL32_A_MASK) | (IM_F32_TO_INT8_SAT(alpha) << IM_COL32_A_SHIFT); - window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight()), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? 0 : ImDrawFlags_RoundCornersBottom); + window->DrawList->AddRectFilled(window->Pos + ImVec2(0, window->TitleBarHeight), window->Pos + window->Size, bg_col, window_rounding, (flags & ImGuiWindowFlags_NoTitleBar) ? 0 : ImDrawFlags_RoundCornersBottom); } // Title bar @@ -6322,13 +6500,37 @@ void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags window->RootWindowPopupTree = parent_window->RootWindowPopupTree; if (parent_window && !(flags & ImGuiWindowFlags_Modal) && (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Popup))) window->RootWindowForTitleBarHighlight = parent_window->RootWindowForTitleBarHighlight; - while (window->RootWindowForNav->Flags & ImGuiWindowFlags_NavFlattened) + while (window->RootWindowForNav->ChildFlags & ImGuiChildFlags_NavFlattened) { IM_ASSERT(window->RootWindowForNav->ParentWindow != NULL); window->RootWindowForNav = window->RootWindowForNav->ParentWindow; } } +// [EXPERIMENTAL] Called by Begin(). NextWindowData is valid at this point. +// This is designed as a toy/test-bed for +void ImGui::UpdateWindowSkipRefresh(ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + window->SkipRefresh = false; + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasRefreshPolicy) == 0) + return; + if (g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_TryToAvoidRefresh) + { + // FIXME-IDLE: Tests for e.g. mouse clicks or keyboard while focused. + if (window->Appearing) // If currently appearing + return; + if (window->Hidden) // If was hidden (previous frame) + return; + if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnHover) && g.HoveredWindow && window->RootWindow == g.HoveredWindow->RootWindow) + return; + if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnFocus) && g.NavWindow && window->RootWindow == g.NavWindow->RootWindow) + return; + window->DrawList = NULL; + window->SkipRefresh = true; + } +} + // When a modal popup is open, newly created windows that want focus (i.e. are not popups and do not specify ImGuiWindowFlags_NoFocusOnAppearing) // should be positioned behind that modal window, unless the window was created inside the modal begin-stack. // In case of multiple stacked modals newly created window honors begin stack order and does not go below its own modal parent. @@ -6341,7 +6543,7 @@ void ImGui::UpdateWindowParentAndRootLinks(ImGuiWindow* window, ImGuiWindowFlags // - WindowE // .. returns NULL // Notes: // - FindBlockingModal(NULL) == NULL is generally equivalent to GetTopMostPopupModal() == NULL. -// Only difference is here we check for ->Active/WasActive but it may be unecessary. +// Only difference is here we check for ->Active/WasActive but it may be unnecessary. ImGuiWindow* ImGui::FindBlockingModal(ImGuiWindow* window) { ImGuiContext& g = *GImGui; @@ -6394,9 +6596,6 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if ((flags & ImGuiWindowFlags_NoInputs) == ImGuiWindowFlags_NoInputs) flags |= ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize; - if (flags & ImGuiWindowFlags_NavFlattened) - IM_ASSERT(flags & ImGuiWindowFlags_ChildWindow); - const int current_frame = g.FrameCount; const bool first_begin_of_the_frame = (window->LastFrameActive != current_frame); window->IsFallbackWindow = (g.CurrentWindowStack.Size == 0 && g.WithinFrameScopeWithImplicitWindow); @@ -6444,6 +6643,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window_stack_data.Window = window; window_stack_data.ParentLastItemDataBackup = g.LastItemData; window_stack_data.StackSizesOnBegin.SetToContextState(&g); + window_stack_data.DisabledOverrideReenable = (flags & ImGuiWindowFlags_Tooltip) && (g.CurrentItemFlags & ImGuiItemFlags_Disabled); g.CurrentWindowStack.push_back(window_stack_data); if (flags & ImGuiWindowFlags_ChildMenu) g.BeginMenuDepth++; @@ -6460,10 +6660,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } // Add to focus scope stack - PushFocusScope((flags & ImGuiWindowFlags_NavFlattened) ? g.CurrentFocusScopeId : window->ID); + PushFocusScope((window->ChildFlags & ImGuiChildFlags_NavFlattened) ? g.CurrentFocusScopeId : window->ID); window->NavRootFocusScopeId = g.CurrentFocusScopeId; - // Add to popup stack + // Add to popup stacks: update OpenPopupStack[] data, push to BeginPopupStack[] if (flags & ImGuiWindowFlags_Popup) { ImGuiPopupData& popup_ref = g.OpenPopupStack[g.BeginPopupStack.Size]; @@ -6527,11 +6727,18 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) if (window->Appearing) SetWindowConditionAllowFlags(window, ImGuiCond_Appearing, false); + // [EXPERIMENTAL] Skip Refresh mode + UpdateWindowSkipRefresh(window); + + // Nested root windows (typically tooltips) override disabled state + if (window_stack_data.DisabledOverrideReenable && window->RootWindow == window) + BeginDisabledOverrideReenable(); + // We intentionally set g.CurrentWindow to NULL to prevent usage until when the viewport is set, then will call SetCurrentWindow() g.CurrentWindow = NULL; // When reusing window again multiple times a frame, just append content (don't need to setup again) - if (first_begin_of_the_frame) + if (first_begin_of_the_frame && !window->SkipRefresh) { // Initialize const bool window_is_child_tooltip = (flags & ImGuiWindowFlags_ChildWindow) && (flags & ImGuiWindowFlags_Tooltip); // FIXME-WIP: Undocumented behavior of Child+Tooltip for pinned tooltip (#1345) @@ -6609,9 +6816,17 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Lock menu offset so size calculation can use it as menu-bar windows need a minimum size. window->DC.MenuBarOffset.x = ImMax(ImMax(window->WindowPadding.x, style.ItemSpacing.x), g.NextWindowData.MenuBarOffsetMinVal.x); window->DC.MenuBarOffset.y = g.NextWindowData.MenuBarOffsetMinVal.y; + window->TitleBarHeight = (flags & ImGuiWindowFlags_NoTitleBar) ? 0.0f : g.FontSize + g.Style.FramePadding.y * 2.0f; + window->MenuBarHeight = (flags & ImGuiWindowFlags_MenuBar) ? window->DC.MenuBarOffset.y + g.FontSize + g.Style.FramePadding.y * 2.0f : 0.0f; + // Depending on condition we use previous or current window size to compare against contents size to decide if a scrollbar should be visible. + // Those flags will be altered further down in the function depending on more conditions. bool use_current_size_for_scrollbar_x = window_just_created; bool use_current_size_for_scrollbar_y = window_just_created; + if (window_size_x_set_by_api && window->ContentSizeExplicit.x != 0.0f) + use_current_size_for_scrollbar_x = true; + if (window_size_y_set_by_api && window->ContentSizeExplicit.y != 0.0f) // #7252 + use_current_size_for_scrollbar_y = true; // Collapse window by double-clicking on title bar // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing @@ -6619,8 +6834,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), so verify that we don't have items over the title bar. ImRect title_bar_rect = window->TitleBarRect(); - if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max) && g.IO.MouseClickedCount[0] == 2) - window->WantCollapseToggle = true; + if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max)) + if (g.IO.MouseClickedCount[0] == 2 && GetKeyOwner(ImGuiKey_MouseLeft) == ImGuiKeyOwner_NoOwner) + window->WantCollapseToggle = true; if (window->WantCollapseToggle) { window->Collapsed = !window->Collapsed; @@ -6638,11 +6854,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // SIZE // Outer Decoration Sizes - // (we need to clear ScrollbarSize immediatly as CalcWindowAutoFitSize() needs it and can be called from other locations). + // (we need to clear ScrollbarSize immediately as CalcWindowAutoFitSize() needs it and can be called from other locations). const ImVec2 scrollbar_sizes_from_last_frame = window->ScrollbarSizes; window->DecoOuterSizeX1 = 0.0f; window->DecoOuterSizeX2 = 0.0f; - window->DecoOuterSizeY1 = window->TitleBarHeight() + window->MenuBarHeight(); + window->DecoOuterSizeY1 = window->TitleBarHeight + window->MenuBarHeight; window->DecoOuterSizeY2 = 0.0f; window->ScrollbarSizes = ImVec2(0.0f, 0.0f); @@ -6791,7 +7007,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->ScrollbarY = (flags & ImGuiWindowFlags_AlwaysVerticalScrollbar) || ((needed_size_from_last_frame.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar)); window->ScrollbarX = (flags & ImGuiWindowFlags_AlwaysHorizontalScrollbar) || ((needed_size_from_last_frame.x > size_x_for_scrollbars - (window->ScrollbarY ? style.ScrollbarSize : 0.0f)) && !(flags & ImGuiWindowFlags_NoScrollbar) && (flags & ImGuiWindowFlags_HorizontalScrollbar)); if (window->ScrollbarX && !window->ScrollbarY) - window->ScrollbarY = (needed_size_from_last_frame.y > size_y_for_scrollbars) && !(flags & ImGuiWindowFlags_NoScrollbar); + window->ScrollbarY = (needed_size_from_last_frame.y > size_y_for_scrollbars - style.ScrollbarSize) && !(flags & ImGuiWindowFlags_NoScrollbar); window->ScrollbarSizes = ImVec2(window->ScrollbarY ? style.ScrollbarSize : 0.0f, window->ScrollbarX ? style.ScrollbarSize : 0.0f); // Amend the partially filled window->DecorationXXX values. @@ -6826,17 +7042,19 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->InnerRect.Max.y = window->Pos.y + window->Size.y - window->DecoOuterSizeY2; // Inner clipping rectangle. - // Will extend a little bit outside the normal work region. - // This is to allow e.g. Selectable or CollapsingHeader or some separators to cover that space. - // Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result. + // - Extend a outside of normal work region up to borders. + // - This is to allow e.g. Selectable or CollapsingHeader or some separators to cover that space. + // - It also makes clipped items be more noticeable. + // - And is consistent on both axis (prior to 2024/05/03 ClipRect used WindowPadding.x * 0.5f on left and right edge), see #3312 + // - Force round operator last to ensure that e.g. (int)(max.x-min.x) in user's render code produce correct result. // Note that if our window is collapsed we will end up with an inverted (~null) clipping rectangle which is the correct behavior. // Affected by window/frame border size. Used by: // - Begin() initial clip rect float top_border_size = (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize); - window->InnerClipRect.Min.x = ImTrunc(0.5f + window->InnerRect.Min.x + ImMax(ImTrunc(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); - window->InnerClipRect.Min.y = ImTrunc(0.5f + window->InnerRect.Min.y + top_border_size); - window->InnerClipRect.Max.x = ImTrunc(0.5f + window->InnerRect.Max.x - ImMax(ImTrunc(window->WindowPadding.x * 0.5f), window->WindowBorderSize)); - window->InnerClipRect.Max.y = ImTrunc(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); + window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + window->WindowBorderSize); + window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + top_border_size); + window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerRect.Max.x - window->WindowBorderSize); + window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); window->InnerClipRect.ClipWithFull(host_rect); // Default item width. Make it proportional to window size if window manually resizes @@ -6950,7 +7168,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) window->DC.MenuBarAppending = false; window->DC.MenuColumns.Update(style.ItemSpacing.x, window_just_activated_by_user); window->DC.TreeDepth = 0; - window->DC.TreeJumpToParentOnPopMask = 0x00; + window->DC.TreeHasStackDataDepthMask = 0x00; window->DC.ChildWindows.resize(0); window->DC.StateStorage = &window->StateStorage; window->DC.CurrentColumns = NULL; @@ -6997,7 +7215,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // We fill last item data based on Title Bar/Tab, in order for IsItemHovered() and IsItemActive() to be usable after Begin(). // This is useful to allow creating context menus on title bar only, etc. - SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, title_bar_rect); + SetLastItemDataForWindow(window, title_bar_rect); // [DEBUG] #ifndef IMGUI_DISABLE_DEBUG_TOOLS @@ -7013,11 +7231,17 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) } else { + // Skip refresh always mark active + if (window->SkipRefresh) + window->Active = true; + // Append SetCurrentWindow(window); + SetLastItemDataForWindow(window, window->TitleBarRect()); } - PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); + if (!window->SkipRefresh) + PushClipRect(window->InnerClipRect.Min, window->InnerClipRect.Max, true); // Clear 'accessed' flag last thing (After PushClipRect which will set the flag. We want the flag to stay false when the default "Debug" window is unused) window->WriteAccessed = false; @@ -7025,14 +7249,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) g.NextWindowData.ClearFlags(); // Update visibility - if (first_begin_of_the_frame) + if (first_begin_of_the_frame && !window->SkipRefresh) { if ((flags & ImGuiWindowFlags_ChildWindow) && !(flags & ImGuiWindowFlags_ChildMenu)) { // Child window can be out of sight and have "negative" clip windows. // Mark them as collapsed so commands are skipped earlier (we can't manually collapse them because they have no title bar). IM_ASSERT((flags & ImGuiWindowFlags_NoTitleBar) != 0); - const bool nav_request = (flags & ImGuiWindowFlags_NavFlattened) && (g.NavAnyRequest && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); + const bool nav_request = (window->ChildFlags & ImGuiChildFlags_NavFlattened) && (g.NavAnyRequest && g.NavWindow && g.NavWindow->RootWindowForNav == window->RootWindowForNav); if (!g.LogEnabled && !nav_request) if (window->OuterRectClipped.Min.x >= window->OuterRectClipped.Max.x || window->OuterRectClipped.Min.y >= window->OuterRectClipped.Max.y) { @@ -7071,6 +7295,11 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) skip_items = true; window->SkipItems = skip_items; } + else if (first_begin_of_the_frame) + { + // Skip refresh mode + window->SkipItems = true; + } // [DEBUG] io.ConfigDebugBeginReturnValue override return value to test Begin/End and BeginChild/EndChild behaviors. // (The implicit fallback window is NOT automatically ended allowing it to always be able to receive commands without crashing) @@ -7087,6 +7316,12 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) return !window->SkipItems; } +static void ImGui::SetLastItemDataForWindow(ImGuiWindow* window, const ImRect& rect) +{ + ImGuiContext& g = *GImGui; + SetLastItemData(window->MoveId, g.CurrentItemFlags, IsMouseHoveringRect(rect.Min, rect.Max, false) ? ImGuiItemStatusFlags_HoveredRect : 0, rect); +} + void ImGui::End() { ImGuiContext& g = *GImGui; @@ -7098,7 +7333,7 @@ void ImGui::End() IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size > 1, "Calling End() too many times!"); return; } - IM_ASSERT(g.CurrentWindowStack.Size > 0); + ImGuiWindowStackData& window_stack_data = g.CurrentWindowStack.back(); // Error checking: verify that user doesn't directly call End() on a child window. if (window->Flags & ImGuiWindowFlags_ChildWindow) @@ -7107,8 +7342,17 @@ void ImGui::End() // Close anything that is open if (window->DC.CurrentColumns) EndColumns(); - PopClipRect(); // Inner window clip rectangle + if (!window->SkipRefresh) + PopClipRect(); // Inner window clip rectangle PopFocusScope(); + if (window_stack_data.DisabledOverrideReenable && window->RootWindow == window) + EndDisabledOverrideReenable(); + + if (window->SkipRefresh) + { + IM_ASSERT(window->DrawList == NULL); + window->DrawList = &window->DrawListInst; + } // Stop logging if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging @@ -7118,12 +7362,12 @@ void ImGui::End() ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); // Pop from window stack - g.LastItemData = g.CurrentWindowStack.back().ParentLastItemDataBackup; + g.LastItemData = window_stack_data.ParentLastItemDataBackup; if (window->Flags & ImGuiWindowFlags_ChildMenu) g.BeginMenuDepth--; if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - g.CurrentWindowStack.back().StackSizesOnBegin.CompareWithContextState(&g); + window_stack_data.StackSizesOnBegin.CompareWithContextState(&g); g.CurrentWindowStack.pop_back(); SetCurrentWindow(g.CurrentWindowStack.Size == 0 ? NULL : g.CurrentWindowStack.back().Window); } @@ -7215,9 +7459,13 @@ void ImGui::FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags) if ((flags & ImGuiFocusRequestFlags_UnlessBelowModal) && (g.NavWindow != window)) // Early out in common case. if (ImGuiWindow* blocking_modal = FindBlockingModal(window)) { + // This block would typically be reached in two situations: + // - API call to FocusWindow() with a window under a modal and ImGuiFocusRequestFlags_UnlessBelowModal flag. + // - User clicking on void or anything behind a modal while a modal is open (window == NULL) IMGUI_DEBUG_LOG_FOCUS("[focus] FocusWindow(\"%s\", UnlessBelowModal): prevented by \"%s\".\n", window ? window->Name : "", blocking_modal->Name); if (window && window == window->RootWindow && (window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus) == 0) - BringWindowToDisplayBehind(window, blocking_modal); // Still bring to right below modal. + BringWindowToDisplayBehind(window, blocking_modal); // Still bring right under modal. (FIXME: Could move in focus list too?) + ClosePopupsOverWindow(GetTopMostPopupModal(), false); // Note how we need to use GetTopMostPopupModal() aad NOT blocking_modal, to handle nested modals return; } @@ -7303,12 +7551,14 @@ void ImGui::SetCurrentFont(ImFont* font) g.Font = font; g.FontBaseSize = ImMax(1.0f, g.IO.FontGlobalScale * g.Font->FontSize * g.Font->Scale); g.FontSize = g.CurrentWindow ? g.CurrentWindow->CalcFontSize() : 0.0f; + g.FontScale = g.FontSize / g.Font->FontSize; ImFontAtlas* atlas = g.Font->ContainerAtlas; g.DrawListSharedData.TexUvWhitePixel = atlas->TexUvWhitePixel; g.DrawListSharedData.TexUvLines = atlas->TexUvLines; g.DrawListSharedData.Font = g.Font; g.DrawListSharedData.FontSize = g.FontSize; + g.DrawListSharedData.FontScale = g.FontScale; } void ImGui::PushFont(ImFont* font) @@ -7367,7 +7617,7 @@ void ImGui::BeginDisabled(bool disabled) } if (was_disabled || disabled) g.CurrentItemFlags |= ImGuiItemFlags_Disabled; - g.ItemFlagsStack.push_back(g.CurrentItemFlags); + g.ItemFlagsStack.push_back(g.CurrentItemFlags); // FIXME-OPT: can we simply skip this and use DisabledStackSize? g.DisabledStackSize++; } @@ -7384,24 +7634,27 @@ void ImGui::EndDisabled() g.Style.Alpha = g.DisabledAlphaBackup; //PopStyleVar(); } -void ImGui::PushTabStop(bool tab_stop) +// Could have been called BeginDisabledDisable() but it didn't want to be award nominated for most awkward function name. +// Ideally we would use a shared e.g. BeginDisabled()->BeginDisabledEx() but earlier needs to be optimal. +// The whole code for this is awkward, will reevaluate if we find a way to implement SetNextItemDisabled(). +void ImGui::BeginDisabledOverrideReenable() { - PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); -} - -void ImGui::PopTabStop() -{ - PopItemFlag(); -} - -void ImGui::PushButtonRepeat(bool repeat) -{ - PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat); + ImGuiContext& g = *GImGui; + IM_ASSERT(g.CurrentItemFlags & ImGuiItemFlags_Disabled); + g.Style.Alpha = g.DisabledAlphaBackup; + g.CurrentItemFlags &= ~ImGuiItemFlags_Disabled; + g.ItemFlagsStack.push_back(g.CurrentItemFlags); + g.DisabledStackSize++; } -void ImGui::PopButtonRepeat() +void ImGui::EndDisabledOverrideReenable() { - PopItemFlag(); + ImGuiContext& g = *GImGui; + g.DisabledStackSize--; + IM_ASSERT(g.DisabledStackSize > 0); + g.ItemFlagsStack.pop_back(); + g.CurrentItemFlags = g.ItemFlagsStack.back(); + g.Style.Alpha = g.DisabledAlphaBackup * g.Style.DisabledAlpha; } void ImGui::PushTextWrapPos(float wrap_pos_x) @@ -7518,7 +7771,7 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) // When changing hovered window we requires a bit of stationary delay before activating hover timer. // FIXME: We don't support delay other than stationary one for now, other delay would need a way - // to fullfill the possibility that multiple IsWindowHovered() with varying flag could return true + // to fulfill the possibility that multiple IsWindowHovered() with varying flag could return true // for different windows of the hierarchy. Possibly need a Hash(Current+Flags) ==> (Timer) cache. // We can implement this for _Stationary because the data is linked to HoveredWindow rather than CurrentWindow. if (flags & ImGuiHoveredFlags_ForTooltip) @@ -7794,6 +8047,14 @@ void ImGui::SetNextWindowBgAlpha(float alpha) g.NextWindowData.BgAlphaVal = alpha; } +// This is experimental and meant to be a toy for exploring a future/wider range of features. +void ImGui::SetNextWindowRefreshPolicy(ImGuiWindowRefreshFlags flags) +{ + ImGuiContext& g = *GImGui; + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasRefreshPolicy; + g.NextWindowData.RefreshFlagsVal = flags; +} + ImDrawList* ImGui::GetWindowDrawList() { ImGuiWindow* window = GetCurrentWindow(); @@ -7822,6 +8083,7 @@ void ImGui::SetWindowFontScale(float scale) ImGuiWindow* window = GetCurrentWindow(); window->FontWindowScale = scale; g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); + g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize; } void ImGui::PushFocusScope(ImGuiID id) @@ -7964,6 +8226,70 @@ ImGuiStorage* ImGui::GetStateStorage() return window->DC.StateStorage; } +bool ImGui::IsRectVisible(const ImVec2& size) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); +} + +bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) +{ + ImGuiWindow* window = GImGui->CurrentWindow; + return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); +} + +//----------------------------------------------------------------------------- +// [SECTION] ID STACK +//----------------------------------------------------------------------------- + +// This is one of the very rare legacy case where we use ImGuiWindow methods, +// it should ideally be flattened at some point but it's been used a lots by widgets. +IM_MSVC_RUNTIME_CHECKS_OFF +ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + ImGuiContext& g = *Ctx; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); +#endif + return id; +} + +ImGuiID ImGuiWindow::GetID(const void* ptr) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&ptr, sizeof(void*), seed); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + ImGuiContext& g = *Ctx; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_Pointer, ptr, NULL); +#endif + return id; +} + +ImGuiID ImGuiWindow::GetID(int n) +{ + ImGuiID seed = IDStack.back(); + ImGuiID id = ImHashData(&n, sizeof(n), seed); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + ImGuiContext& g = *Ctx; + if (g.DebugHookIdInfo == id) + ImGui::DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); +#endif + return id; +} + +// This is only used in rare/specific situations to manufacture an ID out of nowhere. +ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) +{ + ImGuiID seed = IDStack.back(); + ImRect r_rel = ImGui::WindowRectAbsToRel(this, r_abs); + ImGuiID id = ImHashData(&r_rel, sizeof(r_rel), seed); + return id; +} + void ImGui::PushID(const char* str_id) { ImGuiContext& g = *GImGui; @@ -8001,8 +8327,10 @@ void ImGui::PushOverrideID(ImGuiID id) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; +#ifndef IMGUI_DISABLE_DEBUG_TOOLS if (g.DebugHookIdInfo == id) DebugHookIdInfo(id, ImGuiDataType_ID, NULL, NULL); +#endif window->IDStack.push_back(id); } @@ -8012,18 +8340,22 @@ void ImGui::PushOverrideID(ImGuiID id) ImGuiID ImGui::GetIDWithSeed(const char* str, const char* str_end, ImGuiID seed) { ImGuiID id = ImHashStr(str, str_end ? (str_end - str) : 0, seed); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS ImGuiContext& g = *GImGui; if (g.DebugHookIdInfo == id) DebugHookIdInfo(id, ImGuiDataType_String, str, str_end); +#endif return id; } ImGuiID ImGui::GetIDWithSeed(int n, ImGuiID seed) { ImGuiID id = ImHashData(&n, sizeof(n), seed); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS ImGuiContext& g = *GImGui; if (g.DebugHookIdInfo == id) DebugHookIdInfo(id, ImGuiDataType_S32, (void*)(intptr_t)n, NULL); +#endif return id; } @@ -8052,22 +8384,18 @@ ImGuiID ImGui::GetID(const void* ptr_id) return window->GetID(ptr_id); } -bool ImGui::IsRectVisible(const ImVec2& size) +ImGuiID ImGui::GetID(int int_id) { ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(window->DC.CursorPos, window->DC.CursorPos + size)); -} - -bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) -{ - ImGuiWindow* window = GImGui->CurrentWindow; - return window->ClipRect.Overlaps(ImRect(rect_min, rect_max)); + return window->GetID(int_id); } - +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] INPUTS //----------------------------------------------------------------------------- +// - GetModForModKey() [Internal] +// - FixupKeyChord() [Internal] // - GetKeyData() [Internal] // - GetKeyIndex() [Internal] // - GetKeyName() @@ -8129,23 +8457,25 @@ bool ImGui::IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max) // - Shortcut() [Internal] //----------------------------------------------------------------------------- -ImGuiKeyChord ImGui::FixupKeyChord(ImGuiContext* ctx, ImGuiKeyChord key_chord) +static ImGuiKeyChord GetModForModKey(ImGuiKey key) { - // Convert ImGuiMod_Shortcut and add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. + if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) + return ImGuiMod_Ctrl; + if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) + return ImGuiMod_Shift; + if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) + return ImGuiMod_Alt; + if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) + return ImGuiMod_Super; + return ImGuiMod_None; +} + +ImGuiKeyChord ImGui::FixupKeyChord(ImGuiKeyChord key_chord) +{ + // Add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); if (IsModKey(key)) - { - if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) - key_chord |= ImGuiMod_Ctrl; - if (key == ImGuiKey_LeftShift || key == ImGuiKey_RightShift) - key_chord |= ImGuiMod_Shift; - if (key == ImGuiKey_LeftAlt || key == ImGuiKey_RightAlt) - key_chord |= ImGuiMod_Alt; - if (key == ImGuiKey_LeftSuper || key == ImGuiKey_RightSuper) - key_chord |= ImGuiMod_Super; - } - if (key_chord & ImGuiMod_Shortcut) - return (key_chord & ~ImGuiMod_Shortcut) | (ctx->IO.ConfigMacOSXBehaviors ? ImGuiMod_Super : ImGuiMod_Ctrl); + key_chord |= GetModForModKey(key); return key_chord; } @@ -8155,7 +8485,7 @@ ImGuiKeyData* ImGui::GetKeyData(ImGuiContext* ctx, ImGuiKey key) // Special storage location for mods if (key & ImGuiMod_Mask_) - key = ConvertSingleModFlagToKey(ctx, key); + key = ConvertSingleModFlagToKey(key); #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO IM_ASSERT(key >= ImGuiKey_LegacyNativeKey_BEGIN && key < ImGuiKey_NamedKey_END); @@ -8168,6 +8498,7 @@ ImGuiKeyData* ImGui::GetKeyData(ImGuiContext* ctx, ImGuiKey key) } #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO +// Formally moved to obsolete section in 1.90.5 in spite of documented as obsolete since 1.87 ImGuiKey ImGui::GetKeyIndex(ImGuiKey key) { ImGuiContext& g = *GImGui; @@ -8206,10 +8537,12 @@ IM_STATIC_ASSERT(ImGuiKey_NamedKey_COUNT == IM_ARRAYSIZE(GKeyNames)); const char* ImGui::GetKeyName(ImGuiKey key) { - ImGuiContext& g = *GImGui; + if (key == ImGuiKey_None) + return "None"; #ifdef IMGUI_DISABLE_OBSOLETE_KEYIO - IM_ASSERT((IsNamedKeyOrModKey(key) || key == ImGuiKey_None) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); + IM_ASSERT(IsNamedKeyOrMod(key) && "Support for user key indices was dropped in favor of ImGuiKey. Please update backend and user code."); #else + ImGuiContext& g = *GImGui; if (IsLegacyKey(key)) { if (g.IO.KeyMap[key] == -1) @@ -8218,27 +8551,33 @@ const char* ImGui::GetKeyName(ImGuiKey key) key = (ImGuiKey)g.IO.KeyMap[key]; } #endif - if (key == ImGuiKey_None) - return "None"; if (key & ImGuiMod_Mask_) - key = ConvertSingleModFlagToKey(&g, key); + key = ConvertSingleModFlagToKey(key); if (!IsNamedKey(key)) return "Unknown"; return GKeyNames[key - ImGuiKey_NamedKey_BEGIN]; } -// ImGuiMod_Shortcut is translated to either Ctrl or Super. +// Return untranslated names: on macOS, Cmd key will show as Ctrl, Ctrl key will show as super. +// Lifetime of return value: valid until next call to same function. const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord) { ImGuiContext& g = *GImGui; - key_chord = FixupKeyChord(&g, key_chord); + + const ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + if (IsModKey(key)) + key_chord &= ~GetModForModKey(key); // Return "Ctrl+LeftShift" instead of "Ctrl+Shift+LeftShift" ImFormatString(g.TempKeychordName, IM_ARRAYSIZE(g.TempKeychordName), "%s%s%s%s%s", (key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "", (key_chord & ImGuiMod_Shift) ? "Shift+" : "", (key_chord & ImGuiMod_Alt) ? "Alt+" : "", - (key_chord & ImGuiMod_Super) ? (g.IO.ConfigMacOSXBehaviors ? "Cmd+" : "Super+") : "", - GetKeyName((ImGuiKey)(key_chord & ~ImGuiMod_Mask_))); + (key_chord & ImGuiMod_Super) ? "Super+" : "", + (key != ImGuiKey_None || key_chord == ImGuiKey_None) ? GetKeyName(key) : ""); + size_t len; + if (key == ImGuiKey_None && key_chord != 0) + if ((len = strlen(g.TempKeychordName)) != 0) // Remove trailing '+' + g.TempKeychordName[len - 1] = 0; return g.TempKeychordName; } @@ -8308,9 +8647,9 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) routing_entry = &rt->Entries[old_routing_idx]; routing_entry->RoutingCurrScore = routing_entry->RoutingNextScore; routing_entry->RoutingCurr = routing_entry->RoutingNext; // Update entry - routing_entry->RoutingNext = ImGuiKeyOwner_None; + routing_entry->RoutingNext = ImGuiKeyOwner_NoOwner; routing_entry->RoutingNextScore = 255; - if (routing_entry->RoutingCurr == ImGuiKeyOwner_None) + if (routing_entry->RoutingCurr == ImGuiKeyOwner_NoOwner) continue; rt->EntriesNext.push_back(*routing_entry); // Write alive ones into new buffer @@ -8319,7 +8658,7 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) if (routing_entry->Mods == g.IO.KeyMods) { ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); - if (owner_data->OwnerCurr == ImGuiKeyOwner_None) + if (owner_data->OwnerCurr == ImGuiKeyOwner_NoOwner) { owner_data->OwnerCurr = routing_entry->RoutingCurr; //IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X) via Routing\n", GetKeyName(key), routing_entry->RoutingCurr); @@ -8339,7 +8678,7 @@ static void ImGui::UpdateKeyRoutingTable(ImGuiKeyRoutingTable* rt) static inline ImGuiID GetRoutingIdFromOwnerId(ImGuiID owner_id) { ImGuiContext& g = *GImGui; - return (owner_id != ImGuiKeyOwner_None && owner_id != ImGuiKeyOwner_Any) ? owner_id : g.CurrentFocusScopeId; + return (owner_id != ImGuiKeyOwner_NoOwner && owner_id != ImGuiKeyOwner_Any) ? owner_id : g.CurrentFocusScopeId; } ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) @@ -8356,8 +8695,8 @@ ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); if (key == ImGuiKey_None) - key = ConvertSingleModFlagToKey(&g, mods); - IM_ASSERT(IsNamedKey(key) && (key_chord & ImGuiMod_Shortcut) == 0); // Please call ConvertShortcutMod() in calling function. + key = ConvertSingleModFlagToKey(mods); + IM_ASSERT(IsNamedKey(key)); // Get (in the majority of case, the linked list will have one element so this should be 2 reads. // Subsequent elements will be contiguous in memory as list is sorted/rebuilt in NewFrame). @@ -8379,19 +8718,18 @@ ImGuiKeyRoutingData* ImGui::GetShortcutRoutingData(ImGuiKeyChord key_chord) } // Current score encoding (lower is highest priority): -// - 0: ImGuiInputFlags_RouteGlobalHigh -// - 1: ImGuiInputFlags_RouteFocused (if item active) -// - 2: ImGuiInputFlags_RouteGlobal +// - 0: ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverActive +// - 1: ImGuiInputFlags_ActiveItem or ImGuiInputFlags_RouteFocused (if item active) +// - 2: ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused // - 3+: ImGuiInputFlags_RouteFocused (if window in focus-stack) -// - 254: ImGuiInputFlags_RouteGlobalLow +// - 254: ImGuiInputFlags_RouteGlobal // - 255: never route // 'flags' should include an explicit routing policy static int CalcRoutingScore(ImGuiID focus_scope_id, ImGuiID owner_id, ImGuiInputFlags flags) { + ImGuiContext& g = *GImGui; if (flags & ImGuiInputFlags_RouteFocused) { - ImGuiContext& g = *GImGui; - // ActiveID gets top priority // (we don't check g.ActiveIdUsingAllKeys here. Routing is applied but if input ownership is tested later it may discard it) if (owner_id != 0 && g.ActiveId == owner_id) @@ -8409,15 +8747,23 @@ static int CalcRoutingScore(ImGuiID focus_scope_id, ImGuiID owner_id, ImGuiInput for (int index_in_focus_path = 0; index_in_focus_path < g.NavFocusRoute.Size; index_in_focus_path++) if (g.NavFocusRoute.Data[index_in_focus_path].ID == focus_scope_id) return 3 + index_in_focus_path; - return 255; } - - // ImGuiInputFlags_RouteGlobalHigh is default, so calls without flags are not conditional - if (flags & ImGuiInputFlags_RouteGlobal) - return 2; - if (flags & ImGuiInputFlags_RouteGlobalLow) + else if (flags & ImGuiInputFlags_RouteActive) + { + if (owner_id != 0 && g.ActiveId == owner_id) + return 1; + return 255; + } + else if (flags & ImGuiInputFlags_RouteGlobal) + { + if (flags & ImGuiInputFlags_RouteOverActive) + return 0; + if (flags & ImGuiInputFlags_RouteOverFocused) + return 2; return 254; + } + IM_ASSERT(0); return 0; } @@ -8430,7 +8776,7 @@ static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord) // When the right mods are pressed it cannot be a char input so we won't filter the shortcut out. ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); - const bool ignore_char_inputs = ((mods & ImGuiMod_Ctrl) && !(mods & ImGuiMod_Alt)) || (g.IO.ConfigMacOSXBehaviors && (mods & ImGuiMod_Super)); + const bool ignore_char_inputs = ((mods & ImGuiMod_Ctrl) && !(mods & ImGuiMod_Alt)) || (g.IO.ConfigMacOSXBehaviors && (mods & ImGuiMod_Ctrl)); if (ignore_char_inputs) return false; @@ -8444,17 +8790,19 @@ static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord) // - Routes and key ownership are attributed at the beginning of next frame based on best score and mod state. // (Conceptually this does a "Submit for next frame" + "Test for current frame". // As such, it could be called TrySetXXX or SubmitXXX, or the Submit and Test operations should be separate.) -bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id) { ImGuiContext& g = *GImGui; - if ((flags & ImGuiInputFlags_RouteMask_) == 0) - flags |= ImGuiInputFlags_RouteGlobalHigh; // IMPORTANT: This is the default for SetShortcutRouting() but NOT Shortcut() + if ((flags & ImGuiInputFlags_RouteTypeMask_) == 0) + flags |= ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused | ImGuiInputFlags_RouteOverActive; // IMPORTANT: This is the default for SetShortcutRouting() but NOT Shortcut() else - IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteMask_)); // Check that only 1 routing flag is used - IM_ASSERT(owner_id != ImGuiKeyOwner_Any && owner_id != ImGuiKeyOwner_None); + IM_ASSERT(ImIsPowerOfTwo(flags & ImGuiInputFlags_RouteTypeMask_)); // Check that only 1 routing flag is used + IM_ASSERT(owner_id != ImGuiKeyOwner_Any && owner_id != ImGuiKeyOwner_NoOwner); + if (flags & (ImGuiInputFlags_RouteOverFocused | ImGuiInputFlags_RouteOverActive | ImGuiInputFlags_RouteUnlessBgFocused)) + IM_ASSERT(flags & ImGuiInputFlags_RouteGlobal); - // Convert ImGuiMod_Shortcut and add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. - key_chord = FixupKeyChord(&g, key_chord); + // Add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. + key_chord = FixupKeyChord(key_chord); // [DEBUG] Debug break requested by user if (g.DebugBreakInShortcutRouting == key_chord) @@ -8467,38 +8815,45 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI // Note how ImGuiInputFlags_RouteAlways won't set routing and thus won't set owner. May want to rework this? if (flags & ImGuiInputFlags_RouteAlways) { - IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> always\n", GetKeyChordName(key_chord), owner_id, flags); + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, flags=%04X, owner_id=0x%08X) -> always, no register\n", GetKeyChordName(key_chord), flags, owner_id); return true; } - // Specific culling when there's an active. + // Specific culling when there's an active item. if (g.ActiveId != 0 && g.ActiveId != owner_id) { + if (flags & ImGuiInputFlags_RouteActive) + return false; + // Cull shortcuts with no modifiers when it could generate a character. // e.g. Shortcut(ImGuiKey_G) also generates 'g' character, should not trigger when InputText() is active. // but Shortcut(Ctrl+G) should generally trigger when InputText() is active. // TL;DR: lettered shortcut with no mods or with only Alt mod will not trigger while an item reading text input is active. // (We cannot filter based on io.InputQueueCharacters[] contents because of trickling and key<>chars submission order are undefined) - if ((flags & ImGuiInputFlags_RouteFocused) && g.IO.WantTextInput && IsKeyChordPotentiallyCharInput(key_chord)) + if (g.IO.WantTextInput && IsKeyChordPotentiallyCharInput(key_chord)) { - IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> filtered as potential char input\n", GetKeyChordName(key_chord), owner_id, flags); + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, flags=%04X, owner_id=0x%08X) -> filtered as potential char input\n", GetKeyChordName(key_chord), flags, owner_id); return false; } // ActiveIdUsingAllKeyboardKeys trumps all for ActiveId - if ((flags & ImGuiInputFlags_RouteGlobalHigh) == 0 && g.ActiveIdUsingAllKeyboardKeys) + if ((flags & ImGuiInputFlags_RouteOverActive) == 0 && g.ActiveIdUsingAllKeyboardKeys) { ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); if (key == ImGuiKey_None) - key = ConvertSingleModFlagToKey(&g, (ImGuiKey)(key_chord & ImGuiMod_Mask_)); + key = ConvertSingleModFlagToKey((ImGuiKey)(key_chord & ImGuiMod_Mask_)); if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) return false; } } - // FIXME-SHORTCUT: A way to configure the location/focus-scope to test would render this more flexible. - const int score = CalcRoutingScore(g.CurrentFocusScopeId, owner_id, flags); - IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, owner_id=0x%08X, flags=%04X) -> score %d\n", GetKeyChordName(key_chord), owner_id, flags, score); + // Where do we evaluate route for? + ImGuiID focus_scope_id = g.CurrentFocusScopeId; + if (flags & ImGuiInputFlags_RouteFromRootWindow) + focus_scope_id = g.CurrentWindow->RootWindow->ID; // See PushFocusScope() call in Begin() + + const int score = CalcRoutingScore(focus_scope_id, owner_id, flags); + IMGUI_DEBUG_LOG_INPUTROUTING("SetShortcutRouting(%s, flags=%04X, owner_id=0x%08X) -> score %d\n", GetKeyChordName(key_chord), flags, owner_id, score); if (score == 255) return false; @@ -8522,9 +8877,8 @@ bool ImGui::SetShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiI // Note: this cannot be turned into GetShortcutRouting() because we do the owner_id->routing_id translation, name would be more misleading. bool ImGui::TestShortcutRouting(ImGuiKeyChord key_chord, ImGuiID owner_id) { - ImGuiContext& g = *GImGui; const ImGuiID routing_id = GetRoutingIdFromOwnerId(owner_id); - key_chord = FixupKeyChord(&g, key_chord); + key_chord = FixupKeyChord(key_chord); ImGuiKeyRoutingData* routing_data = GetShortcutRoutingData(key_chord); // FIXME: Could avoid creating entry. return routing_data->RoutingCurr == routing_id; } @@ -8548,11 +8902,11 @@ bool ImGui::IsKeyDown(ImGuiKey key, ImGuiID owner_id) bool ImGui::IsKeyPressed(ImGuiKey key, bool repeat) { - return IsKeyPressed(key, ImGuiKeyOwner_Any, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None); + return IsKeyPressed(key, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None, ImGuiKeyOwner_Any); } // Important: unless legacy IsKeyPressed(ImGuiKey, bool repeat=true) which DEFAULT to repeat, this requires EXPLICIT repeat. -bool ImGui::IsKeyPressed(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) +bool ImGui::IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id) { const ImGuiKeyData* key_data = GetKeyData(key); if (!key_data->Down) // In theory this should already be encoded as (DownDuration < 0.0f), but testing this facilitates eating mechanism (until we finish work on key ownership) @@ -8622,10 +8976,10 @@ bool ImGui::IsMouseDown(ImGuiMouseButton button, ImGuiID owner_id) bool ImGui::IsMouseClicked(ImGuiMouseButton button, bool repeat) { - return IsMouseClicked(button, ImGuiKeyOwner_Any, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None); + return IsMouseClicked(button, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None, ImGuiKeyOwner_Any); } -bool ImGui::IsMouseClicked(ImGuiMouseButton button, ImGuiID owner_id, ImGuiInputFlags flags) +bool ImGui::IsMouseClicked(ImGuiMouseButton button, ImGuiInputFlags flags, ImGuiID owner_id) { ImGuiContext& g = *GImGui; IM_ASSERT(button >= 0 && button < IM_ARRAYSIZE(g.IO.MouseDown)); @@ -8831,6 +9185,9 @@ static void ImGui::UpdateKeyboardInputs() ImGuiContext& g = *GImGui; ImGuiIO& io = g.IO; + if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) + io.ClearInputKeys(); + // Import legacy keys or verify they are not used #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO if (io.BackendUsingLegacyKeyArrays == 0) @@ -8951,7 +9308,7 @@ static void ImGui::UpdateKeyboardInputs() ImGuiKeyOwnerData* owner_data = &g.KeysOwnerData[key - ImGuiKey_NamedKey_BEGIN]; owner_data->OwnerCurr = owner_data->OwnerNext; if (!key_data->Down) // Important: ownership is released on the frame after a release. Ensure a 'MouseDown -> CloseWindow -> MouseUp' chain doesn't lead to someone else seeing the MouseUp. - owner_data->OwnerNext = ImGuiKeyOwner_None; + owner_data->OwnerNext = ImGuiKeyOwner_NoOwner; owner_data->LockThisFrame = owner_data->LockUntilRelease = owner_data->LockUntilRelease && key_data->Down; // Clear LockUntilRelease when key is not Down anymore } @@ -9107,8 +9464,8 @@ void ImGui::UpdateMouseWheel() } ImVec2 wheel; - wheel.x = TestKeyOwner(ImGuiKey_MouseWheelX, ImGuiKeyOwner_None) ? g.IO.MouseWheelH : 0.0f; - wheel.y = TestKeyOwner(ImGuiKey_MouseWheelY, ImGuiKeyOwner_None) ? g.IO.MouseWheel : 0.0f; + wheel.x = TestKeyOwner(ImGuiKey_MouseWheelX, ImGuiKeyOwner_NoOwner) ? g.IO.MouseWheelH : 0.0f; + wheel.y = TestKeyOwner(ImGuiKey_MouseWheelY, ImGuiKeyOwner_NoOwner) ? g.IO.MouseWheel : 0.0f; //IMGUI_DEBUG_LOG("MouseWheel X:%.3f Y:%.3f\n", wheel_x, wheel_y); ImGuiWindow* mouse_window = g.WheelingWindow ? g.WheelingWindow : g.HoveredWindow; @@ -9195,7 +9552,7 @@ void ImGui::SetNextFrameWantCaptureMouse(bool want_capture_mouse) #ifndef IMGUI_DISABLE_DEBUG_TOOLS static const char* GetInputSourceName(ImGuiInputSource source) { - const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad", "Clipboard" }; + const char* input_source_names[] = { "None", "Mouse", "Keyboard", "Gamepad" }; IM_ASSERT(IM_ARRAYSIZE(input_source_names) == ImGuiInputSource_COUNT && source >= 0 && source < ImGuiInputSource_COUNT); return input_source_names[source]; } @@ -9277,6 +9634,8 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) else if (e->Type == ImGuiInputEventType_Key) { // Trickling Rule: Stop processing queued events if we got multiple action on the same button + if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) + continue; ImGuiKey key = e->Key.Key; IM_ASSERT(key != ImGuiKey_None); ImGuiKeyData* key_data = GetKeyData(key); @@ -9297,6 +9656,8 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) } else if (e->Type == ImGuiInputEventType_Text) { + if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) + continue; // Trickling Rule: Stop processing queued events if keys/mouse have been interacted with if (trickle_fast_inputs && ((key_changed && trickle_interleaved_keys_and_text) || mouse_button_changed != 0 || mouse_moved || mouse_wheeled)) break; @@ -9341,13 +9702,16 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) // - we clear in EndFrame() and not now in order allow application/user code polling this flag // (e.g. custom backend may want to clear additional data, custom widgets may want to react with a "canceling" event). if (g.IO.AppFocusLost) + { g.IO.ClearInputKeys(); + g.IO.ClearInputMouse(); + } } ImGuiID ImGui::GetKeyOwner(ImGuiKey key) { - if (!IsNamedKeyOrModKey(key)) - return ImGuiKeyOwner_None; + if (!IsNamedKeyOrMod(key)) + return ImGuiKeyOwner_NoOwner; ImGuiContext& g = *GImGui; ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); @@ -9355,7 +9719,7 @@ ImGuiID ImGui::GetKeyOwner(ImGuiKey key) if (g.ActiveIdUsingAllKeyboardKeys && owner_id != g.ActiveId && owner_id != ImGuiKeyOwner_Any) if (key >= ImGuiKey_Keyboard_BEGIN && key < ImGuiKey_Keyboard_END) - return ImGuiKeyOwner_None; + return ImGuiKeyOwner_NoOwner; return owner_id; } @@ -9366,7 +9730,7 @@ ImGuiID ImGui::GetKeyOwner(ImGuiKey key) // All paths are also testing for key not being locked, for the rare cases that key have been locked with using ImGuiInputFlags_LockXXX flags. bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) { - if (!IsNamedKeyOrModKey(key)) + if (!IsNamedKeyOrMod(key)) return true; ImGuiContext& g = *GImGui; @@ -9385,7 +9749,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) { if (owner_data->LockThisFrame) return false; - if (owner_data->OwnerCurr != ImGuiKeyOwner_None) + if (owner_data->OwnerCurr != ImGuiKeyOwner_NoOwner) return false; } @@ -9400,7 +9764,7 @@ bool ImGui::TestKeyOwner(ImGuiKey key, ImGuiID owner_id) void ImGui::SetKeyOwner(ImGuiKey key, ImGuiID owner_id, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; - IM_ASSERT(IsNamedKeyOrModKey(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) + IM_ASSERT(IsNamedKeyOrMod(key) && (owner_id != ImGuiKeyOwner_Any || (flags & (ImGuiInputFlags_LockThisFrame | ImGuiInputFlags_LockUntilRelease)))); // Can only use _Any with _LockXXX flags (to eat a key away without an ID to retrieve it) IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetKeyOwner) == 0); // Passing flags not supported by this function! //IMGUI_DEBUG_LOG("SetKeyOwner(%s, owner_id=0x%08X, flags=%08X)\n", GetKeyName(key), owner_id, flags); @@ -9420,7 +9784,6 @@ void ImGui::SetKeyOwnersForKeyChord(ImGuiKeyChord key_chord, ImGuiID owner_id, I if (key_chord & ImGuiMod_Shift) { SetKeyOwner(ImGuiMod_Shift, owner_id, flags); } if (key_chord & ImGuiMod_Alt) { SetKeyOwner(ImGuiMod_Alt, owner_id, flags); } if (key_chord & ImGuiMod_Super) { SetKeyOwner(ImGuiMod_Super, owner_id, flags); } - if (key_chord & ImGuiMod_Shortcut) { SetKeyOwner(ImGuiMod_Shortcut, owner_id, flags); } if (key_chord & ~ImGuiMod_Mask_) { SetKeyOwner((ImGuiKey)(key_chord & ~ImGuiMod_Mask_), owner_id, flags); } } @@ -9445,17 +9808,22 @@ void ImGui::SetItemKeyOwner(ImGuiKey key, ImGuiInputFlags flags) } } +void ImGui::SetItemKeyOwner(ImGuiKey key) +{ + SetItemKeyOwner(key, ImGuiInputFlags_None); +} + // This is the only public API until we expose owner_id versions of the API as replacements. bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord) { - return IsKeyChordPressed(key_chord, 0, ImGuiInputFlags_None); + return IsKeyChordPressed(key_chord, ImGuiInputFlags_None, ImGuiKeyOwner_Any); } // This is equivalent to comparing KeyMods + doing a IsKeyPressed() -bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id) { ImGuiContext& g = *GImGui; - key_chord = FixupKeyChord(&g, key_chord); + key_chord = FixupKeyChord(key_chord); ImGuiKey mods = (ImGuiKey)(key_chord & ImGuiMod_Mask_); if (g.IO.KeyMods != mods) return false; @@ -9463,35 +9831,69 @@ bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiIn // Special storage location for mods ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); if (key == ImGuiKey_None) - key = ConvertSingleModFlagToKey(&g, mods); - if (!IsKeyPressed(key, owner_id, (flags & ImGuiInputFlags_RepeatMask_))) + key = ConvertSingleModFlagToKey(mods); + if (!IsKeyPressed(key, (flags & ImGuiInputFlags_RepeatMask_), owner_id)) return false; return true; } -void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord) +void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasShortcut; g.NextItemData.Shortcut = key_chord; + g.NextItemData.ShortcutFlags = flags; } -bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags flags) +// Called from within ItemAdd: at this point we can read from NextItemData and write to LastItemData +void ImGui::ItemHandleShortcut(ImGuiID id) { - //ImGuiContext& g = *GImGui; - //IMGUI_DEBUG_LOG("Shortcut(%s, owner_id=0x%08X, flags=%X)\n", GetKeyChordName(key_chord, g.TempBuffer.Data, g.TempBuffer.Size), owner_id, flags); + ImGuiContext& g = *GImGui; + ImGuiInputFlags flags = g.NextItemData.ShortcutFlags; + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetNextItemShortcut) == 0); // Passing flags not supported by SetNextItemShortcut()! + + if (g.LastItemData.InFlags & ImGuiItemFlags_Disabled) + return; + if (flags & ImGuiInputFlags_Tooltip) + { + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasShortcut; + g.LastItemData.Shortcut = g.NextItemData.Shortcut; + } + if (!Shortcut(g.NextItemData.Shortcut, flags & ImGuiInputFlags_SupportedByShortcut, id) || g.NavActivateId != 0) + return; + + // FIXME: Generalize Activation queue? + g.NavActivateId = id; // Will effectively disable clipping. + g.NavActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_FromShortcut; + //if (g.ActiveId == 0 || g.ActiveId == id) + g.NavActivateDownId = g.NavActivatePressedId = id; + NavHighlightActivated(id); +} + +bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags) +{ + return Shortcut(key_chord, flags, ImGuiKeyOwner_Any); +} + +bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID owner_id) +{ + ImGuiContext& g = *GImGui; + //IMGUI_DEBUG_LOG("Shortcut(%s, flags=%X, owner_id=0x%08X)\n", GetKeyChordName(key_chord, g.TempBuffer.Data, g.TempBuffer.Size), flags, owner_id); // When using (owner_id == 0/Any): SetShortcutRouting() will use CurrentFocusScopeId and filter with this, so IsKeyPressed() is fine with he 0/Any. - if ((flags & ImGuiInputFlags_RouteMask_) == 0) + if ((flags & ImGuiInputFlags_RouteTypeMask_) == 0) flags |= ImGuiInputFlags_RouteFocused; // Using 'owner_id == ImGuiKeyOwner_Any/0': auto-assign an owner based on current focus scope (each window has its focus scope by default) // Effectively makes Shortcut() always input-owner aware. - if (owner_id == ImGuiKeyOwner_Any || owner_id == ImGuiKeyOwner_None) + if (owner_id == ImGuiKeyOwner_Any || owner_id == ImGuiKeyOwner_NoOwner) owner_id = GetRoutingIdFromOwnerId(owner_id); + if (g.CurrentItemFlags & ImGuiItemFlags_Disabled) + return false; + // Submit route - if (!SetShortcutRouting(key_chord, owner_id, flags)) + if (!SetShortcutRouting(key_chord, flags, owner_id)) return false; // Default repeat behavior for Shortcut() @@ -9499,8 +9901,12 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags if ((flags & ImGuiInputFlags_Repeat) != 0 && (flags & ImGuiInputFlags_RepeatUntilMask_) == 0) flags |= ImGuiInputFlags_RepeatUntilKeyModsChange; - if (!IsKeyChordPressed(key_chord, owner_id, flags)) + if (!IsKeyChordPressed(key_chord, flags, owner_id)) return false; + + // Claim mods during the press + SetKeyOwnersForKeyChord(key_chord & ImGuiMod_Mask_, owner_id); + IM_ASSERT((flags & ~ImGuiInputFlags_SupportedByShortcut) == 0); // Passing flags not supported by this function! return true; } @@ -9510,24 +9916,26 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiID owner_id, ImGuiInputFlags // [SECTION] ERROR CHECKING //----------------------------------------------------------------------------- -// Helper function to verify ABI compatibility between caller code and compiled version of Dear ImGui. +// Verify ABI compatibility between caller code and compiled version of Dear ImGui. This helps detects some build issues. +// Called by IMGUI_CHECKVERSION(). // Verify that the type sizes are matching between the calling file's compilation unit and imgui.cpp's compilation unit -// If this triggers you have an issue: -// - Most commonly: mismatched headers and compiled code version. -// - Or: mismatched configuration #define, compilation settings, packing pragma etc. -// The configuration settings mentioned in imconfig.h must be set for all compilation units involved with Dear ImGui, -// which is way it is required you put them in your imconfig file (and not just before including imgui.h). -// Otherwise it is possible that different compilation units would see different structure layout +// If this triggers you have mismatched headers and compiled code versions. +// - It could be because of a build issue (using new headers with old compiled code) +// - It could be because of mismatched configuration #define, compilation settings, packing pragma etc. +// THE CONFIGURATION SETTINGS MENTIONED IN imconfig.h MUST BE SET FOR ALL COMPILATION UNITS INVOLVED WITH DEAR IMGUI. +// Which is why it is required you put them in your imconfig file (and NOT only before including imgui.h). +// Otherwise it is possible that different compilation units would see different structure layout. +// If you don't want to modify imconfig.h you can use the IMGUI_USER_CONFIG define to change filename. bool ImGui::DebugCheckVersionAndDataLayout(const char* version, size_t sz_io, size_t sz_style, size_t sz_vec2, size_t sz_vec4, size_t sz_vert, size_t sz_idx) { bool error = false; if (strcmp(version, IMGUI_VERSION) != 0) { error = true; IM_ASSERT(strcmp(version, IMGUI_VERSION) == 0 && "Mismatched version string!"); } - if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } + if (sz_io != sizeof(ImGuiIO)) { error = true; IM_ASSERT(sz_io == sizeof(ImGuiIO) && "Mismatched struct layout!"); } if (sz_style != sizeof(ImGuiStyle)) { error = true; IM_ASSERT(sz_style == sizeof(ImGuiStyle) && "Mismatched struct layout!"); } - if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } - if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } - if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } - if (sz_idx != sizeof(ImDrawIdx)) { error = true; IM_ASSERT(sz_idx == sizeof(ImDrawIdx) && "Mismatched struct layout!"); } + if (sz_vec2 != sizeof(ImVec2)) { error = true; IM_ASSERT(sz_vec2 == sizeof(ImVec2) && "Mismatched struct layout!"); } + if (sz_vec4 != sizeof(ImVec4)) { error = true; IM_ASSERT(sz_vec4 == sizeof(ImVec4) && "Mismatched struct layout!"); } + if (sz_vert != sizeof(ImDrawVert)) { error = true; IM_ASSERT(sz_vert == sizeof(ImDrawVert) && "Mismatched struct layout!"); } + if (sz_idx != sizeof(ImDrawIdx)) { error = true; IM_ASSERT(sz_idx == sizeof(ImDrawIdx) && "Mismatched struct layout!"); } return !error; } @@ -9603,10 +10011,6 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && g.IO.BackendUsingLegacyKeyArrays == 1) IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); #endif - - // Check: the io.ConfigWindowsResizeFromEdges option requires backend to honor mouse cursor changes and set the ImGuiBackendFlags_HasMouseCursors flag accordingly. - if (g.IO.ConfigWindowsResizeFromEdges && !(g.IO.BackendFlags & ImGuiBackendFlags_HasMouseCursors)) - g.IO.ConfigWindowsResizeFromEdges = false; } static void ImGui::ErrorCheckEndFrameSanityChecks() @@ -9696,6 +10100,11 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); EndTabBar(); } + while (g.CurrentMultiSelect != NULL && g.CurrentMultiSelect->Storage->Window == window) + { + if (log_callback) log_callback(user_data, "Recovered from missing EndMultiSelect() in '%s'", window->Name); + EndMultiSelect(); + } while (window->DC.TreeDepth > 0) { if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); @@ -9714,7 +10123,13 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo while (g.DisabledStackSize > stack_sizes->SizeOfDisabledStack) //-V1044 { if (log_callback) log_callback(user_data, "Recovered from missing EndDisabled() in '%s'", window->Name); - EndDisabled(); + if (g.CurrentItemFlags & ImGuiItemFlags_Disabled) + EndDisabled(); + else + { + EndDisabledOverrideReenable(); + g.CurrentWindowStack.back().DisabledOverrideReenable = false; + } } while (g.ColorStack.Size > stack_sizes->SizeOfColorStack) { @@ -9786,7 +10201,6 @@ void ImGuiStackSizes::CompareWithContextState(ImGuiContext* ctx) // [SECTION] ITEM SUBMISSION //----------------------------------------------------------------------------- // - KeepAliveID() -// - ItemHandleShortcut() [Internal] // - ItemAdd() //----------------------------------------------------------------------------- @@ -9800,24 +10214,11 @@ void ImGui::KeepAliveID(ImGuiID id) g.ActiveIdPreviousFrameIsAlive = true; } -static void ItemHandleShortcut(ImGuiID id) -{ - // FIXME: Generalize Activation queue? - ImGuiContext& g = *GImGui; - if (ImGui::Shortcut(g.NextItemData.Shortcut, id, ImGuiInputFlags_None) && g.NavActivateId == 0) - { - g.NavActivateId = id; // Will effectively disable clipping. - g.NavActivateFlags = ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_FromShortcut; - if (g.ActiveId == 0 || g.ActiveId == id) - g.NavActivateDownId = g.NavActivatePressedId = id; - ImGui::NavHighlightActivated(id); - } -} - // Declare item bounding box for clipping and interaction. // Note that the size can be different than the one provided to ItemSize(). Typically, widgets that spread over available surface // declare their minimum size requirement to ItemSize() and provide a larger region to ItemAdd() which is used drawing/interaction. // THIS IS IN THE PERFORMANCE CRITICAL PATH (UNTIL THE CLIPPING TEST AND EARLY-RETURN) +IM_MSVC_RUNTIME_CHECKS_OFF bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGuiItemFlags extra_flags) { ImGuiContext& g = *GImGui; @@ -9852,7 +10253,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); if (g.NavId == id || g.NavAnyRequest) if (g.NavWindow->RootWindowForNav == window->RootWindowForNav) - if (window == g.NavWindow || ((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened)) + if (window == g.NavWindow || ((window->ChildFlags | g.NavWindow->ChildFlags) & ImGuiChildFlags_NavFlattened)) NavProcessItem(); } @@ -9870,15 +10271,12 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu #endif // Clipping test - // (this is a modified copy of IsClippedEx() so we can reuse the is_rect_visible value) - //const bool is_clipped = IsClippedEx(bb, id); - //if (is_clipped) - // return false; + // (this is an inline copy of IsClippedEx() so we can reuse the is_rect_visible value, otherwise we'd do 'if (IsClippedEx(bb, id)) return false') // g.NavActivateId is not necessarily == g.NavId, in the case of remote activation (e.g. shortcuts) const bool is_rect_visible = bb.Overlaps(window->ClipRect); if (!is_rect_visible) if (id == 0 || (id != g.ActiveId && id != g.ActiveIdPreviousFrame && id != g.NavId && id != g.NavActivateId)) - if (!g.LogEnabled) + if (!g.ItemUnclipByLog) return false; // [DEBUG] @@ -9905,7 +10303,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HoveredRect; return true; } - +IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] LAYOUT @@ -9930,9 +10328,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // - GetFrameHeight() // - GetFrameHeightWithSpacing() // - GetContentRegionMax() -// - GetContentRegionMaxAbs() [Internal] // - GetContentRegionAvail(), -// - GetWindowContentRegionMin(), GetWindowContentRegionMax() // - BeginGroup() // - EndGroup() // Also see in imgui_widgets: tab bars, and in imgui_tables: tables, columns. @@ -9942,6 +10338,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // Register minimum needed size so it can extend the bounding box used for auto-fit calculation. // See comments in ItemAdd() about how/why the size provided to ItemSize() vs ItemAdd() may often different. // THIS IS IN THE PERFORMANCE CRITICAL PATH. +IM_MSVC_RUNTIME_CHECKS_OFF void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) { ImGuiContext& g = *GImGui; @@ -9977,6 +10374,7 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y) if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) SameLine(); } +IM_MSVC_RUNTIME_CHECKS_RESTORE // Gets back to previous line and continue with horizontal layout // offset_from_start_x == 0 : follow right after previous item @@ -10146,8 +10544,8 @@ float ImGui::CalcItemWidth() w = window->DC.ItemWidth; if (w < 0.0f) { - float region_max_x = GetContentRegionMaxAbs().x; - w = ImMax(1.0f, region_max_x - window->DC.CursorPos.x + w); + float region_avail_x = GetContentRegionAvail().x; + w = ImMax(1.0f, region_avail_x + w); } w = IM_TRUNC(w); return w; @@ -10159,22 +10557,19 @@ float ImGui::CalcItemWidth() // The 4.0f here may be changed to match CalcItemWidth() and/or BeginChild() (right now we have a mismatch which is harmless but undesirable) ImVec2 ImGui::CalcItemSize(ImVec2 size, float default_w, float default_h) { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - - ImVec2 region_max; + ImVec2 avail; if (size.x < 0.0f || size.y < 0.0f) - region_max = GetContentRegionMaxAbs(); + avail = GetContentRegionAvail(); if (size.x == 0.0f) size.x = default_w; else if (size.x < 0.0f) - size.x = ImMax(4.0f, region_max.x - window->DC.CursorPos.x + size.x); + size.x = ImMax(4.0f, avail.x + size.x); // <-- size.x is negative here so we are subtracting if (size.y == 0.0f) size.y = default_h; else if (size.y < 0.0f) - size.y = ImMax(4.0f, region_max.y - window->DC.CursorPos.y + size.y); + size.y = ImMax(4.0f, avail.y + size.y); // <-- size.y is negative here so we are subtracting return size; } @@ -10203,33 +10598,23 @@ float ImGui::GetFrameHeightWithSpacing() return g.FontSize + g.Style.FramePadding.y * 2.0f + g.Style.ItemSpacing.y; } -// FIXME: All the Contents Region function are messy or misleading. WE WILL AIM TO OBSOLETE ALL OF THEM WITH A NEW "WORK RECT" API. Thanks for your patience! - -// FIXME: This is in window space (not screen space!). -ImVec2 ImGui::GetContentRegionMax() +ImVec2 ImGui::GetContentRegionAvail() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImVec2 mx = (window->DC.CurrentColumns || g.CurrentTable) ? window->WorkRect.Max : window->ContentRegionRect.Max; - return mx - window->Pos; + return mx - window->DC.CursorPos; } -// [Internal] Absolute coordinate. Saner. This is not exposed until we finishing refactoring work rect features. -ImVec2 ImGui::GetContentRegionMaxAbs() -{ - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - ImVec2 mx = (window->DC.CurrentColumns || g.CurrentTable) ? window->WorkRect.Max : window->ContentRegionRect.Max; - return mx; -} +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS -ImVec2 ImGui::GetContentRegionAvail() +// You should never need those functions. Always use GetCursorScreenPos() and GetContentRegionAvail()! +// They are bizarre local-coordinates which don't play well with scrolling. +ImVec2 ImGui::GetContentRegionMax() { - ImGuiWindow* window = GImGui->CurrentWindow; - return GetContentRegionMaxAbs() - window->DC.CursorPos; + return GetContentRegionAvail() + GetCursorScreenPos() - GetWindowPos(); } -// In window space (not screen space!) ImVec2 ImGui::GetWindowContentRegionMin() { ImGuiWindow* window = GImGui->CurrentWindow; @@ -10241,6 +10626,7 @@ ImVec2 ImGui::GetWindowContentRegionMax() ImGuiWindow* window = GImGui->CurrentWindow; return window->ContentRegionRect.Max - window->Pos; } +#endif // Lock horizontal starting position + capture group bounding box into one "item" (so you can use IsItemHovered() or layout primitives such as SameLine() on whole group, etc.) // Groups are currently a mishmash of functionalities which should perhaps be clarified and separated. @@ -10286,11 +10672,11 @@ void ImGui::EndGroup() if (window->DC.IsSetPos) ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); - ImRect group_bb(group_data.BackupCursorPos, ImMax(window->DC.CursorMaxPos, group_data.BackupCursorPos)); - + // Include LastItemData.Rect.Max as a workaround for e.g. EndTable() undershooting with CursorMaxPos report. (#7543) + ImRect group_bb(group_data.BackupCursorPos, ImMax(ImMax(window->DC.CursorMaxPos, g.LastItemData.Rect.Max), group_data.BackupCursorPos)); window->DC.CursorPos = group_data.BackupCursorPos; window->DC.CursorPosPrevLine = group_data.BackupCursorPosPrevLine; - window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, window->DC.CursorMaxPos); + window->DC.CursorMaxPos = ImMax(group_data.BackupCursorMaxPos, group_bb.Max); window->DC.Indent = group_data.BackupIndent; window->DC.GroupOffset = group_data.BackupGroupOffset; window->DC.CurrLineSize = group_data.BackupCurrLineSize; @@ -10305,7 +10691,7 @@ void ImGui::EndGroup() return; } - window->DC.CurrLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now. + window->DC.CurrLineTextBaseOffset = ImMax(window->DC.PrevLineTextBaseOffset, group_data.BackupCurrLineTextBaseOffset); // FIXME: Incorrect, we should grab the base offset from the *first line* of the group but it is hard to obtain now. ItemSize(group_bb.GetSize()); ItemAdd(group_bb, 0, NULL, ImGuiItemFlags_NoTabStop); @@ -10774,7 +11160,7 @@ void ImGui::OpenPopupEx(ImGuiID id, ImGuiPopupFlags popup_flags) ImGuiPopupData popup_ref; // Tagged as new ref as Window will be set back to NULL if we write this into OpenPopupStack. popup_ref.PopupId = id; popup_ref.Window = NULL; - popup_ref.BackupNavWindow = g.NavWindow; // When popup closes focus may be restored to NavWindow (depend on window type). + popup_ref.RestoreNavWindow = g.NavWindow; // When popup closes focus may be restored to NavWindow (depend on window type). popup_ref.OpenFrameCount = g.FrameCount; popup_ref.OpenParentId = parent_window->IDStack.back(); popup_ref.OpenPopupPos = NavCalcPreferredRefPos(); @@ -10823,6 +11209,7 @@ void ImGui::ClosePopupsOverWindow(ImGuiWindow* ref_window, bool restore_focus_to return; // Don't close our own child popup windows. + //IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupsOverWindow(\"%s\") restore_under=%d\n", ref_window ? ref_window->Name : "", restore_focus_to_window_under_popup); int popup_count_to_keep = 0; if (ref_window) { @@ -10879,18 +11266,19 @@ void ImGui::ClosePopupsExceptModals() void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_popup) { ImGuiContext& g = *GImGui; - IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupToLevel(%d), restore_focus_to_window_under_popup=%d\n", remaining, restore_focus_to_window_under_popup); + IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupToLevel(%d), restore_under=%d\n", remaining, restore_focus_to_window_under_popup); IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); // Trim open popup stack - ImGuiWindow* popup_window = g.OpenPopupStack[remaining].Window; - ImGuiWindow* popup_backup_nav_window = g.OpenPopupStack[remaining].BackupNavWindow; + ImGuiPopupData prev_popup = g.OpenPopupStack[remaining]; g.OpenPopupStack.resize(remaining); - if (restore_focus_to_window_under_popup) + // Restore focus (unless popup window was not yet submitted, and didn't have a chance to take focus anyhow. See #7325 for an edge case) + if (restore_focus_to_window_under_popup && prev_popup.Window) { - ImGuiWindow* focus_window = (popup_window && popup_window->Flags & ImGuiWindowFlags_ChildMenu) ? popup_window->ParentWindow : popup_backup_nav_window; - if (focus_window && !focus_window->WasActive && popup_window) + ImGuiWindow* popup_window = prev_popup.Window; + ImGuiWindow* focus_window = (popup_window->Flags & ImGuiWindowFlags_ChildMenu) ? popup_window->ParentWindow : prev_popup.RestoreNavWindow; + if (focus_window && !focus_window->WasActive) FocusTopMostWindowUnderOne(popup_window, NULL, NULL, ImGuiFocusRequestFlags_RestoreFocusedChild); // Fallback else FocusWindow(focus_window, (g.NavLayer == ImGuiNavLayer_Main) ? ImGuiFocusRequestFlags_RestoreFocusedChild : ImGuiFocusRequestFlags_None); @@ -10928,8 +11316,8 @@ void ImGui::CloseCurrentPopup() window->DC.NavHideHighlightOneFrame = true; } -// Attention! BeginPopup() adds default flags which BeginPopupEx()! -bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) +// Attention! BeginPopup() adds default flags when calling BeginPopupEx()! +bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags extra_window_flags) { ImGuiContext& g = *GImGui; if (!IsPopupOpen(id, ImGuiPopupFlags_None)) @@ -10939,13 +11327,12 @@ bool ImGui::BeginPopupEx(ImGuiID id, ImGuiWindowFlags flags) } char name[20]; - if (flags & ImGuiWindowFlags_ChildMenu) + if (extra_window_flags & ImGuiWindowFlags_ChildMenu) ImFormatString(name, IM_ARRAYSIZE(name), "##Menu_%02d", g.BeginMenuDepth); // Recycle windows based on depth else ImFormatString(name, IM_ARRAYSIZE(name), "##Popup_%08x", id); // Not recycling, so we can close/open during the same frame - flags |= ImGuiWindowFlags_Popup; - bool is_open = Begin(name, NULL, flags); + bool is_open = Begin(name, NULL, extra_window_flags | ImGuiWindowFlags_Popup); if (!is_open) // NB: Begin can return false when the popup is completely clipped (e.g. zero size display) EndPopup(); @@ -11341,7 +11728,7 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) // When entering through a NavFlattened border, we consider child window items as fully clipped for scoring if (window->ParentWindow == g.NavWindow) { - IM_ASSERT((window->Flags | g.NavWindow->Flags) & ImGuiWindowFlags_NavFlattened); + IM_ASSERT((window->ChildFlags | g.NavWindow->ChildFlags) & ImGuiChildFlags_NavFlattened); if (!window->ClipRect.Overlaps(cand)) return false; cand.ClipWithFull(window->ClipRect); // This allows the scored item to not overlap other candidates in the parent window @@ -11366,6 +11753,9 @@ static bool ImGui::NavScoreItem(ImGuiNavItemData* result) if (dbx != 0.0f || dby != 0.0f) { // For non-overlapping boxes, use distance between boxes + // FIXME-NAV: Quadrant may be incorrect because of (1) dbx bias and (2) curr.Max.y bias applied by NavBiasScoringRect() where typically curr.Max.y==curr.Min.y + // One typical case where this happens, with style.WindowMenuButtonPosition == ImGuiDir_Right, pressing Left to navigate from Close to Collapse tends to fail. + // Also see #6344. Calling ImGetDirQuadrantFromDelta() with unbiased values may be good but side-effects are plenty. dax = dbx; day = dby; dist_axial = dist_box; @@ -11644,6 +12034,7 @@ void ImGui::NavMoveRequestSubmit(ImGuiDir move_dir, ImGuiDir clip_dir, ImGuiNavM { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindow != NULL); + //IMGUI_DEBUG_LOG_NAV("[nav] NavMoveRequestSubmit: dir %c, window \"%s\"\n", "-WENS"[move_dir + 1], g.NavWindow->Name); if (move_flags & ImGuiNavMoveFlags_IsTabbing) move_flags |= ImGuiNavMoveFlags_AllowCurrentNavId; @@ -11673,7 +12064,7 @@ void ImGui::NavMoveRequestResolveWithLastItem(ImGuiNavItemData* result) } // Called by TreePop() to implement ImGuiTreeNodeFlags_NavLeftJumpsBackHere -void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiNavTreeNodeData* tree_node_data) +void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGuiTreeNodeStackData* tree_node_data) { ImGuiContext& g = *GImGui; g.NavMoveScoringItems = false; @@ -11812,7 +12203,10 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.NavWindow; - if (g.NavDisableHighlight || !g.NavDisableMouseHover || !window) + const bool activated_shortcut = g.ActiveId != 0 && g.ActiveIdFromShortcut && g.ActiveId == g.LastItemData.ID; + + // Testing for !activated_shortcut here could in theory be removed if we decided that activating a remote shortcut altered one of the g.NavDisableXXX flag. + if ((g.NavDisableHighlight || !g.NavDisableMouseHover || !window) && !activated_shortcut) { // Mouse (we need a fallback in case the mouse becomes invalid after being used) // The +1.0f offset when stored by OpenPopupEx() allows reopening this or another popup (same or another mouse button) while not moving the mouse, it is pretty standard. @@ -11823,14 +12217,19 @@ static ImVec2 ImGui::NavCalcPreferredRefPos() else { // When navigation is active and mouse is disabled, pick a position around the bottom left of the currently navigated item + ImRect ref_rect; + if (activated_shortcut) + ref_rect = g.LastItemData.NavRect; + else + ref_rect = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]); + // Take account of upcoming scrolling (maybe set mouse pos should be done in EndFrame?) - ImRect rect_rel = WindowRectRelToAbs(window, window->NavRectRel[g.NavLayer]); if (window->LastFrameActive != g.FrameCount && (window->ScrollTarget.x != FLT_MAX || window->ScrollTarget.y != FLT_MAX)) { ImVec2 next_scroll = CalcNextScrollFromScrollTargetAndClamp(window); - rect_rel.Translate(window->Scroll - next_scroll); + ref_rect.Translate(window->Scroll - next_scroll); } - ImVec2 pos = ImVec2(rect_rel.Min.x + ImMin(g.Style.FramePadding.x * 4, rect_rel.GetWidth()), rect_rel.Max.y - ImMin(g.Style.FramePadding.y, rect_rel.GetHeight())); + ImVec2 pos = ImVec2(ref_rect.Min.x + ImMin(g.Style.FramePadding.x * 4, ref_rect.GetWidth()), ref_rect.Max.y - ImMin(g.Style.FramePadding.y, ref_rect.GetHeight())); ImGuiViewport* viewport = GetMainViewport(); return ImTrunc(ImClamp(pos, viewport->Pos, viewport->Pos + viewport->Size)); // ImTrunc() is important because non-integer mouse position application in backend might be lossy and result in undesirable non-zero delta. } @@ -11884,6 +12283,7 @@ static void ImGui::NavUpdate() // Process navigation init request (select first/default focus) g.NavJustMovedToId = 0; + g.NavJustMovedToFocusScopeId = g.NavJustMovedFromFocusScopeId = 0; if (g.NavInitResult.ID != 0) NavInitRequestApplyResult(); g.NavInitRequest = false; @@ -11925,10 +12325,10 @@ static void ImGui::NavUpdate() g.NavActivateFlags = ImGuiActivateFlags_None; if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { - const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space, ImGuiKeyOwner_None)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_None)); - const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, ImGuiKeyOwner_None)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_None))); - const bool input_down = (nav_keyboard_active && (IsKeyDown(ImGuiKey_Enter, ImGuiKeyOwner_None) || IsKeyDown(ImGuiKey_KeypadEnter, ImGuiKeyOwner_None))) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput, ImGuiKeyOwner_None)); - const bool input_pressed = input_down && ((nav_keyboard_active && (IsKeyPressed(ImGuiKey_Enter, ImGuiKeyOwner_None) || IsKeyPressed(ImGuiKey_KeypadEnter, ImGuiKeyOwner_None))) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, ImGuiKeyOwner_None))); + const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space, ImGuiKeyOwner_NoOwner)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_NoOwner)); + const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, 0, ImGuiKeyOwner_NoOwner)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, 0, ImGuiKeyOwner_NoOwner))); + const bool input_down = (nav_keyboard_active && (IsKeyDown(ImGuiKey_Enter, ImGuiKeyOwner_NoOwner) || IsKeyDown(ImGuiKey_KeypadEnter, ImGuiKeyOwner_NoOwner))) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadInput, ImGuiKeyOwner_NoOwner)); + const bool input_pressed = input_down && ((nav_keyboard_active && (IsKeyPressed(ImGuiKey_Enter, 0, ImGuiKeyOwner_NoOwner) || IsKeyPressed(ImGuiKey_KeypadEnter, 0, ImGuiKeyOwner_NoOwner))) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadInput, 0, ImGuiKeyOwner_NoOwner))); if (g.ActiveId == 0 && activate_pressed) { g.NavActivateId = g.NavId; @@ -12036,9 +12436,12 @@ void ImGui::NavInitRequestApplyResult() ImGuiNavItemData* result = &g.NavInitResult; if (g.NavId != result->ID) { + g.NavJustMovedFromFocusScopeId = g.NavFocusScopeId; g.NavJustMovedToId = result->ID; g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = 0; + g.NavJustMovedToIsTabbing = false; + g.NavJustMovedToHasSelectionData = (result->InFlags & ImGuiItemFlags_HasSelectionUserData) != 0; } // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) @@ -12101,11 +12504,11 @@ void ImGui::NavUpdateCreateMoveRequest() g.NavMoveScrollFlags = ImGuiScrollFlags_None; if (window && !g.NavWindowingTarget && !(window->Flags & ImGuiWindowFlags_NoNavInputs)) { - const ImGuiInputFlags repeat_mode = ImGuiInputFlags_Repeat | ImGuiInputFlags_RepeatRateNavMove; - if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadLeft, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_LeftArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Left; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadRight, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_RightArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Right; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadUp, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_UpArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Up; } - if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadDown, ImGuiKeyOwner_None, repeat_mode)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_DownArrow, ImGuiKeyOwner_None, repeat_mode)))) { g.NavMoveDir = ImGuiDir_Down; } + const ImGuiInputFlags repeat_mode = ImGuiInputFlags_Repeat | (ImGuiInputFlags)ImGuiInputFlags_RepeatRateNavMove; + if (!IsActiveIdUsingNavDir(ImGuiDir_Left) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadLeft, repeat_mode, ImGuiKeyOwner_NoOwner)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_LeftArrow, repeat_mode, ImGuiKeyOwner_NoOwner)))) { g.NavMoveDir = ImGuiDir_Left; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Right) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadRight, repeat_mode, ImGuiKeyOwner_NoOwner)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_RightArrow, repeat_mode, ImGuiKeyOwner_NoOwner)))) { g.NavMoveDir = ImGuiDir_Right; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Up) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadUp, repeat_mode, ImGuiKeyOwner_NoOwner)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_UpArrow, repeat_mode, ImGuiKeyOwner_NoOwner)))) { g.NavMoveDir = ImGuiDir_Up; } + if (!IsActiveIdUsingNavDir(ImGuiDir_Down) && ((nav_gamepad_active && IsKeyPressed(ImGuiKey_GamepadDpadDown, repeat_mode, ImGuiKeyOwner_NoOwner)) || (nav_keyboard_active && IsKeyPressed(ImGuiKey_DownArrow, repeat_mode, ImGuiKeyOwner_NoOwner)))) { g.NavMoveDir = ImGuiDir_Down; } } g.NavMoveClipDir = g.NavMoveDir; g.NavScoringNoClipRect = ImRect(+FLT_MAX, +FLT_MAX, -FLT_MAX, -FLT_MAX); @@ -12201,7 +12604,7 @@ void ImGui::NavUpdateCreateTabbingRequest() if (window == NULL || g.NavWindowingTarget != NULL || (window->Flags & ImGuiWindowFlags_NoNavInputs)) return; - const bool tab_pressed = IsKeyPressed(ImGuiKey_Tab, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat) && !g.IO.KeyCtrl && !g.IO.KeyAlt; + const bool tab_pressed = IsKeyPressed(ImGuiKey_Tab, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner) && !g.IO.KeyCtrl && !g.IO.KeyAlt; if (!tab_pressed) return; @@ -12282,17 +12685,23 @@ void ImGui::NavMoveRequestApplyResult() g.NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; } - // FIXME: Could become optional e.g. ImGuiNavMoveFlags_NoClearActiveId if we later want to apply navigation requests without altering active input. - if (g.ActiveId != result->ID) + // Clear active id unless requested not to + // FIXME: ImGuiNavMoveFlags_NoClearActiveId is currently unused as we don't have a clear strategy to preserve active id after interaction, + // so this is mostly provided as a gateway for further experiments (see #1418, #2890) + if (g.ActiveId != result->ID && (g.NavMoveFlags & ImGuiNavMoveFlags_NoClearActiveId) == 0) ClearActiveID(); // Don't set NavJustMovedToId if just landed on the same spot (which may happen with ImGuiNavMoveFlags_AllowCurrentNavId) // PageUp/PageDown however sets always set NavJustMovedTo (vs Home/End which doesn't) mimicking Windows behavior. if ((g.NavId != result->ID || (g.NavMoveFlags & ImGuiNavMoveFlags_IsPageMove)) && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSelect) == 0) { + g.NavJustMovedFromFocusScopeId = g.NavFocusScopeId; g.NavJustMovedToId = result->ID; g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = g.NavMoveKeyMods; + g.NavJustMovedToIsTabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) != 0; + g.NavJustMovedToHasSelectionData = (result->InFlags & ImGuiItemFlags_HasSelectionUserData) != 0; + //IMGUI_DEBUG_LOG_NAV("[nav] NavJustMovedFromFocusScopeId = 0x%08X, NavJustMovedToFocusScopeId = 0x%08X\n", g.NavJustMovedFromFocusScopeId, g.NavJustMovedToFocusScopeId); } // Apply new NavID/Focus @@ -12337,7 +12746,7 @@ static void ImGui::NavUpdateCancelRequest() ImGuiContext& g = *GImGui; const bool nav_gamepad_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (g.IO.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; const bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - if (!(nav_keyboard_active && IsKeyPressed(ImGuiKey_Escape, ImGuiKeyOwner_None)) && !(nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadCancel, ImGuiKeyOwner_None))) + if (!(nav_keyboard_active && IsKeyPressed(ImGuiKey_Escape, 0, ImGuiKeyOwner_NoOwner)) && !(nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadCancel, 0, ImGuiKeyOwner_NoOwner))) return; IMGUI_DEBUG_LOG_NAV("[nav] NavUpdateCancelRequest()\n"); @@ -12386,10 +12795,10 @@ static float ImGui::NavUpdatePageUpPageDown() if ((window->Flags & ImGuiWindowFlags_NoNavInputs) || g.NavWindowingTarget != NULL) return 0.0f; - const bool page_up_held = IsKeyDown(ImGuiKey_PageUp, ImGuiKeyOwner_None); - const bool page_down_held = IsKeyDown(ImGuiKey_PageDown, ImGuiKeyOwner_None); - const bool home_pressed = IsKeyPressed(ImGuiKey_Home, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat); - const bool end_pressed = IsKeyPressed(ImGuiKey_End, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat); + const bool page_up_held = IsKeyDown(ImGuiKey_PageUp, ImGuiKeyOwner_NoOwner); + const bool page_down_held = IsKeyDown(ImGuiKey_PageDown, ImGuiKeyOwner_NoOwner); + const bool home_pressed = IsKeyPressed(ImGuiKey_Home, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner); + const bool end_pressed = IsKeyPressed(ImGuiKey_End, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner); if (page_up_held == page_down_held && home_pressed == end_pressed) // Proceed if either (not both) are pressed, otherwise early out return 0.0f; @@ -12399,9 +12808,9 @@ static float ImGui::NavUpdatePageUpPageDown() if (window->DC.NavLayersActiveMask == 0x00 && window->DC.NavWindowHasScrollY) { // Fallback manual-scroll when window has no navigable item - if (IsKeyPressed(ImGuiKey_PageUp, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat)) + if (IsKeyPressed(ImGuiKey_PageUp, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner)) SetScrollY(window, window->Scroll.y - window->InnerRect.GetHeight()); - else if (IsKeyPressed(ImGuiKey_PageDown, ImGuiKeyOwner_None, ImGuiInputFlags_Repeat)) + else if (IsKeyPressed(ImGuiKey_PageDown, ImGuiInputFlags_Repeat, ImGuiKeyOwner_NoOwner)) SetScrollY(window, window->Scroll.y + window->InnerRect.GetHeight()); else if (home_pressed) SetScrollY(window, 0.0f); @@ -12594,9 +13003,9 @@ static void ImGui::NavUpdateWindowing() const ImGuiID owner_id = ImHashStr("###NavUpdateWindowing"); const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; const bool nav_keyboard_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; - const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, owner_id, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); - const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, owner_id, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways); - const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && IsKeyPressed(ImGuiKey_NavGamepadMenu, 0, ImGuiInputFlags_None); + const bool keyboard_next_window = allow_windowing && g.ConfigNavWindowingKeyNext && Shortcut(g.ConfigNavWindowingKeyNext, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways, owner_id); + const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways, owner_id); + const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && IsKeyPressed(ImGuiKey_NavGamepadMenu, ImGuiInputFlags_None); const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && (keyboard_next_window || keyboard_prev_window); // Note: enabled even without NavEnableKeyboard! if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) @@ -12607,7 +13016,7 @@ static void ImGui::NavUpdateWindowing() g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; - // Register ownership of our mods. Using ImGuiInputFlags_RouteGlobalHigh in the Shortcut() calls instead would probably be correct but may have more side-effects. + // Manually register ownership of our mods. Using a global route in the Shortcut() calls instead would probably be correct but may have more side-effects. if (keyboard_next_window || keyboard_prev_window) SetKeyOwnersForKeyChord((g.ConfigNavWindowingKeyNext | g.ConfigNavWindowingKeyPrev) & ImGuiMod_Mask_, owner_id); } @@ -12655,7 +13064,7 @@ static void ImGui::NavUpdateWindowing() // Keyboard: Press and Release ALT to toggle menu layer const ImGuiKey windowing_toggle_keys[] = { ImGuiKey_LeftAlt, ImGuiKey_RightAlt }; for (ImGuiKey windowing_toggle_key : windowing_toggle_keys) - if (nav_keyboard_active && IsKeyPressed(windowing_toggle_key, ImGuiKeyOwner_None)) + if (nav_keyboard_active && IsKeyPressed(windowing_toggle_key, 0, ImGuiKeyOwner_NoOwner)) { g.NavWindowingToggleLayer = true; g.NavWindowingToggleKey = windowing_toggle_key; @@ -12670,7 +13079,7 @@ static void ImGui::NavUpdateWindowing() // We cancel toggling nav layer if an owner has claimed the key. if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper) g.NavWindowingToggleLayer = false; - if (TestKeyOwner(g.NavWindowingToggleKey, ImGuiKeyOwner_None) == false || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_None) == false) + if (TestKeyOwner(g.NavWindowingToggleKey, ImGuiKeyOwner_NoOwner) == false || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_NoOwner) == false) g.NavWindowingToggleLayer = false; // Apply layer toggle on Alt release @@ -12789,6 +13198,8 @@ void ImGui::NavUpdateWindowingOverlay() SetNextWindowPos(viewport->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.WindowPadding * 2.0f); Begin("###NavWindowingList", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings); + if (g.ContextName[0] != 0) + SeparatorText(g.ContextName); for (int n = g.WindowsFocusOrder.Size - 1; n >= 0; n--) { ImGuiWindow* window = g.WindowsFocusOrder[n]; @@ -12818,6 +13229,8 @@ bool ImGui::IsDragDropActive() void ImGui::ClearDragDrop() { ImGuiContext& g = *GImGui; + if (g.DragDropActive) + IMGUI_DEBUG_LOG_ACTIVEID("[dragdrop] ClearDragDrop()\n"); g.DragDropActive = false; g.DragDropPayload.Clear(); g.DragDropAcceptFlags = ImGuiDragDropFlags_None; @@ -12856,7 +13269,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) bool source_drag_active = false; ImGuiID source_id = 0; ImGuiID source_parent_id = 0; - if (!(flags & ImGuiDragDropFlags_SourceExtern)) + if ((flags & ImGuiDragDropFlags_SourceExtern) == 0) { source_id = g.LastItemData.ID; if (source_id != 0) @@ -12912,48 +13325,55 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) } else { + // When ImGuiDragDropFlags_SourceExtern is set: window = NULL; source_id = ImHashStr("#SourceExtern"); source_drag_active = true; + mouse_button = g.IO.MouseDown[0] ? 0 : -1; + KeepAliveID(source_id); + SetActiveID(source_id, NULL); } - if (source_drag_active) - { - if (!g.DragDropActive) - { - IM_ASSERT(source_id != 0); - ClearDragDrop(); - ImGuiPayload& payload = g.DragDropPayload; - payload.SourceId = source_id; - payload.SourceParentId = source_parent_id; - g.DragDropActive = true; - g.DragDropSourceFlags = flags; - g.DragDropMouseButton = mouse_button; - if (payload.SourceId == g.ActiveId) - g.ActiveIdNoClearOnFocusLoss = true; - } - g.DragDropSourceFrameCount = g.FrameCount; - g.DragDropWithinSource = true; + IM_ASSERT(g.DragDropWithinTarget == false); // Can't nest BeginDragDropSource() and BeginDragDropTarget() + if (!source_drag_active) + return false; - if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) - { - // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) - // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. - bool ret; - if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) - ret = BeginTooltipHidden(); - else - ret = BeginTooltip(); - IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddendAndSkipItemsForCurrentFrame(). - IM_UNUSED(ret); - } + // Activate drag and drop + if (!g.DragDropActive) + { + IM_ASSERT(source_id != 0); + ClearDragDrop(); + IMGUI_DEBUG_LOG_ACTIVEID("[dragdrop] BeginDragDropSource() DragDropActive = true, source_id = 0x%08X%s\n", + source_id, (flags & ImGuiDragDropFlags_SourceExtern) ? " (EXTERN)" : ""); + ImGuiPayload& payload = g.DragDropPayload; + payload.SourceId = source_id; + payload.SourceParentId = source_parent_id; + g.DragDropActive = true; + g.DragDropSourceFlags = flags; + g.DragDropMouseButton = mouse_button; + if (payload.SourceId == g.ActiveId) + g.ActiveIdNoClearOnFocusLoss = true; + } + g.DragDropSourceFrameCount = g.FrameCount; + g.DragDropWithinSource = true; + + if (!(flags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) + { + // Target can request the Source to not display its tooltip (we use a dedicated flag to make this request explicit) + // We unfortunately can't just modify the source flags and skip the call to BeginTooltip, as caller may be emitting contents. + bool ret; + if (g.DragDropAcceptIdPrev && (g.DragDropAcceptFlags & ImGuiDragDropFlags_AcceptNoPreviewTooltip)) + ret = BeginTooltipHidden(); + else + ret = BeginTooltip(); + IM_ASSERT(ret); // FIXME-NEWBEGIN: If this ever becomes false, we need to Begin("##Hidden", NULL, ImGuiWindowFlags_NoSavedSettings) + SetWindowHiddendAndSkipItemsForCurrentFrame(). + IM_UNUSED(ret); + } - if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) - g.LastItemData.StatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; + if (!(flags & ImGuiDragDropFlags_SourceNoDisableHover) && !(flags & ImGuiDragDropFlags_SourceExtern)) + g.LastItemData.StatusFlags &= ~ImGuiItemStatusFlags_HoveredRect; - return true; - } - return false; + return true; } void ImGui::EndDragDropSource() @@ -12983,7 +13403,7 @@ bool ImGui::SetDragDropPayload(const char* type, const void* data, size_t data_s IM_ASSERT(strlen(type) < IM_ARRAYSIZE(payload.DataType) && "Payload type can be at most 32 characters long"); IM_ASSERT((data != NULL && data_size > 0) || (data == NULL && data_size == 0)); IM_ASSERT(cond == ImGuiCond_Always || cond == ImGuiCond_Once); - IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource() + IM_ASSERT(payload.SourceId != 0); // Not called between BeginDragDropSource() and EndDragDropSource() if (cond == ImGuiCond_Always || payload.DataFrameCount == -1) { @@ -13032,9 +13452,9 @@ bool ImGui::BeginDragDropTargetCustom(const ImRect& bb, ImGuiID id) if (window->SkipItems) return false; - IM_ASSERT(g.DragDropWithinTarget == false); + IM_ASSERT(g.DragDropWithinTarget == false && g.DragDropWithinSource == false); // Can't nest BeginDragDropSource() and BeginDragDropTarget() g.DragDropTargetRect = bb; - g.DragDropTargetClipRect = window->ClipRect; // May want to be overriden by user depending on use case? + g.DragDropTargetClipRect = window->ClipRect; // May want to be overridden by user depending on use case? g.DragDropTargetId = id; g.DragDropWithinTarget = true; return true; @@ -13067,7 +13487,7 @@ bool ImGui::BeginDragDropTarget() if (g.DragDropPayload.SourceId == id) return false; - IM_ASSERT(g.DragDropWithinTarget == false); + IM_ASSERT(g.DragDropWithinTarget == false && g.DragDropWithinSource == false); // Can't nest BeginDragDropSource() and BeginDragDropTarget() g.DragDropTargetRect = display_rect; g.DragDropTargetClipRect = (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasClipRect) ? g.LastItemData.ClipRect : window->ClipRect; g.DragDropTargetId = id; @@ -13110,11 +13530,15 @@ const ImGuiPayload* ImGui::AcceptDragDropPayload(const char* type, ImGuiDragDrop RenderDragDropTargetRect(r, g.DragDropTargetClipRect); g.DragDropAcceptFrameCount = g.FrameCount; - payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting OS window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() + if ((g.DragDropSourceFlags & ImGuiDragDropFlags_SourceExtern) && g.DragDropMouseButton == -1) + payload.Delivery = was_accepted_previously && (g.DragDropSourceFrameCount < g.FrameCount); + else + payload.Delivery = was_accepted_previously && !IsMouseDown(g.DragDropMouseButton); // For extern drag sources affecting OS window focus, it's easier to just test !IsMouseDown() instead of IsMouseReleased() if (!payload.Delivery && !(flags & ImGuiDragDropFlags_AcceptBeforeDelivery)) return NULL; - //IMGUI_DEBUG_LOG("AcceptDragDropPayload(): %08X: return payload\n", g.DragDropTargetId); + if (payload.Delivery) + IMGUI_DEBUG_LOG_ACTIVEID("[dragdrop] AcceptDragDropPayload(): 0x%08X: payload delivery\n", g.DragDropTargetId); return &payload; } @@ -13264,7 +13688,7 @@ void ImGui::LogBegin(ImGuiLogType type, int auto_open_depth) IM_ASSERT(g.LogEnabled == false); IM_ASSERT(g.LogFile == NULL); IM_ASSERT(g.LogBuffer.empty()); - g.LogEnabled = true; + g.LogEnabled = g.ItemUnclipByLog = true; g.LogType = type; g.LogNextPrefix = g.LogNextSuffix = NULL; g.LogDepthRef = window->DC.TreeDepth; @@ -13363,7 +13787,7 @@ void ImGui::LogFinish() break; } - g.LogEnabled = false; + g.LogEnabled = g.ItemUnclipByLog = false; g.LogType = ImGuiLogType_None; g.LogFile = NULL; g.LogBuffer.clear(); @@ -13383,10 +13807,10 @@ void ImGui::LogButtons() #endif const bool log_to_file = Button("Log To File"); SameLine(); const bool log_to_clipboard = Button("Log To Clipboard"); SameLine(); - PushTabStop(false); + PushItemFlag(ImGuiItemFlags_NoTabStop, true); SetNextItemWidth(80.0f); SliderInt("Default Depth", &g.LogDepthToExpandDefault, 0, 9, NULL); - PopTabStop(); + PopItemFlag(); PopID(); // Start logging at the end of the function so that the buttons don't appear in the log @@ -13824,6 +14248,10 @@ static void ImGui::UpdateViewportsNewFrame() //----------------------------------------------------------------------------- // [SECTION] PLATFORM DEPENDENT HELPERS //----------------------------------------------------------------------------- +// - Default clipboard handlers +// - Default shell function handlers +// - Default IME handlers +//----------------------------------------------------------------------------- #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS) @@ -13949,7 +14377,61 @@ static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text g.ClipboardHandlerData[(int)(text_end - text)] = 0; } +#endif // Default clipboard handlers + +//----------------------------------------------------------------------------- + +#ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS +#if defined(__APPLE__) && TARGET_OS_IPHONE +#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS +#endif + +#if defined(_WIN32) && defined(IMGUI_DISABLE_WIN32_FUNCTIONS) +#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS +#endif +#endif + +#ifndef IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS +#ifdef _WIN32 +#include // ShellExecuteA() +#ifdef _MSC_VER +#pragma comment(lib, "shell32") #endif +static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) +{ + return (INT_PTR)::ShellExecuteA(NULL, "open", path, NULL, NULL, SW_SHOWDEFAULT) > 32; +} +#else +#include +#include +static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) +{ +#if defined(__APPLE__) + const char* args[] { "open", "--", path, NULL }; +#else + const char* args[] { "xdg-open", path, NULL }; +#endif + pid_t pid = fork(); + if (pid < 0) + return false; + if (!pid) + { + execvp(args[0], const_cast(args)); + exit(-1); + } + else + { + int status; + waitpid(pid, &status, 0); + return WEXITSTATUS(status) == 0; + } +} +#endif +#else +static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { return false; } +#endif // Default shell handlers + +//----------------------------------------------------------------------------- // Win32 API IME support (for Asian languages, etc.) #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) @@ -13959,7 +14441,7 @@ static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text #pragma comment(lib, "imm32") #endif -static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data) +static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) { // Notify OS Input Method Editor of text input position HWND hwnd = (HWND)viewport->PlatformHandleRaw; @@ -13985,9 +14467,9 @@ static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatf #else -static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport*, ImGuiPlatformImeData*) {} +static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData*) {} -#endif +#endif // Default IME handlers //----------------------------------------------------------------------------- // [SECTION] METRICS/DEBUGGER WINDOW @@ -14234,6 +14716,11 @@ void ImGui::ShowMetricsWindow(bool* p_open) // Basic info Text("Dear ImGui %s", GetVersion()); + if (g.ContextName[0] != 0) + { + SameLine(); + Text("(Context Name: \"%s\")", g.ContextName); + } Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); Text("%d visible windows, %d current allocations", io.MetricsRenderWindows, g.DebugAllocInfo.TotalAllocCount - g.DebugAllocInfo.TotalFreeCount); @@ -14454,9 +14941,9 @@ void ImGui::ShowMetricsWindow(bool* p_open) { // As it's difficult to interact with tree nodes while popups are open, we display everything inline. ImGuiWindow* window = popup_data.Window; - BulletText("PopupID: %08x, Window: '%s' (%s%s), BackupNavWindow '%s', ParentWindow '%s'", + BulletText("PopupID: %08x, Window: '%s' (%s%s), RestoreNavWindow '%s', ParentWindow '%s'", popup_data.PopupId, window ? window->Name : "NULL", window && (window->Flags & ImGuiWindowFlags_ChildWindow) ? "Child;" : "", window && (window->Flags & ImGuiWindowFlags_ChildMenu) ? "Menu;" : "", - popup_data.BackupNavWindow ? popup_data.BackupNavWindow->Name : "NULL", window && window->ParentWindow ? window->ParentWindow->Name : "NULL"); + popup_data.RestoreNavWindow ? popup_data.RestoreNavWindow->Name : "NULL", window && window->ParentWindow ? window->ParentWindow->Name : "NULL"); } TreePop(); } @@ -14505,6 +14992,17 @@ void ImGui::ShowMetricsWindow(bool* p_open) TreePop(); } + // Details for MultiSelect + if (TreeNode("MultiSelect", "MultiSelect (%d)", g.MultiSelectStorage.GetAliveCount())) + { + ImGuiBoxSelectState* bs = &g.BoxSelectState; + BulletText("BoxSelect ID=0x%08X, Starting = %d, Active %d", bs->ID, bs->IsStarting, bs->IsActive); + for (int n = 0; n < g.MultiSelectStorage.GetMapSize(); n++) + if (ImGuiMultiSelectState* state = g.MultiSelectStorage.TryGetMapData(n)) + DebugNodeMultiSelectState(state); + TreePop(); + } + // Details for Docking #ifdef IMGUI_HAS_DOCK if (TreeNode("Docking")) @@ -14573,7 +15071,12 @@ void ImGui::ShowMetricsWindow(bool* p_open) for (int n = buf_size - 1; n >= 0; n--) { ImGuiDebugAllocEntry* entry = &info->LastEntriesBuf[(info->LastEntriesIdx - n + buf_size) % buf_size]; - BulletText("Frame %06d: %+3d ( %2d malloc, %2d free )%s", entry->FrameCount, entry->AllocCount - entry->FreeCount, entry->AllocCount, entry->FreeCount, (n == 0) ? " (most recent)" : ""); + BulletText("Frame %06d: %+3d ( %2d alloc, %2d free )", entry->FrameCount, entry->AllocCount - entry->FreeCount, entry->AllocCount, entry->FreeCount); + if (n == 0) + { + SameLine(); + Text("<- %d frames ago", g.FrameCount - entry->FrameCount); + } } TreePop(); } @@ -14635,7 +15138,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) for (ImGuiKey key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key = (ImGuiKey)(key + 1)) { ImGuiKeyOwnerData* owner_data = GetKeyOwnerData(&g, key); - if (owner_data->OwnerCurr == ImGuiKeyOwner_None) + if (owner_data->OwnerCurr == ImGuiKeyOwner_NoOwner) continue; Text("%s: 0x%08X%s", GetKeyName(key), owner_data->OwnerCurr, owner_data->LockUntilRelease ? " LockUntilRelease" : owner_data->LockThisFrame ? " LockThisFrame" : ""); @@ -15095,8 +15598,11 @@ void ImGui::DebugNodeStorage(ImGuiStorage* storage, const char* label) { if (!TreeNode(label, "%s: %d entries, %d bytes", label, storage->Data.Size, storage->Data.size_in_bytes())) return; - for (const ImGuiStorage::ImGuiStoragePair& p : storage->Data) + for (const ImGuiStoragePair& p : storage->Data) + { BulletText("Key 0x%08X Value { i: %d }", p.key, p.val_i); // Important: we currently don't store a type, real value may not be integer. + DebugLocateItemOnHover(p.key); + } TreePop(); } @@ -15196,6 +15702,12 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) (flags & ImGuiWindowFlags_ChildWindow) ? "Child " : "", (flags & ImGuiWindowFlags_Tooltip) ? "Tooltip " : "", (flags & ImGuiWindowFlags_Popup) ? "Popup " : "", (flags & ImGuiWindowFlags_Modal) ? "Modal " : "", (flags & ImGuiWindowFlags_ChildMenu) ? "ChildMenu " : "", (flags & ImGuiWindowFlags_NoSavedSettings) ? "NoSavedSettings " : "", (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); + if (flags & ImGuiWindowFlags_ChildWindow) + BulletText("ChildFlags: 0x%08X (%s%s%s%s..)", window->ChildFlags, + (window->ChildFlags & ImGuiChildFlags_Border) ? "Border " : "", + (window->ChildFlags & ImGuiChildFlags_ResizeX) ? "ResizeX " : "", + (window->ChildFlags & ImGuiChildFlags_ResizeY) ? "ResizeY " : "", + (window->ChildFlags & ImGuiChildFlags_NavFlattened) ? "NavFlattened " : ""); BulletText("Scroll: (%.2f/%.2f,%.2f/%.2f) Scrollbar:%s%s", window->Scroll.x, window->ScrollMax.x, window->Scroll.y, window->ScrollMax.y, window->ScrollbarX ? "X" : "", window->ScrollbarY ? "Y" : ""); BulletText("Active: %d/%d, WriteAccessed: %d, BeginOrderWithinContext: %d", window->Active, window->WasActive, window->WriteAccessed, (window->Active || window->WasActive) ? window->BeginOrderWithinContext : -1); BulletText("Appearing: %d, Hidden: %d (CanSkip %d Cannot %d), SkipItems: %d", window->Appearing, window->Hidden, window->HiddenFramesCanSkipItems, window->HiddenFramesCannotSkipItems, window->SkipItems); @@ -15283,14 +15795,20 @@ void ImGui::DebugLogV(const char* fmt, va_list args) { ImGuiContext& g = *GImGui; const int old_size = g.DebugLogBuf.size(); - g.DebugLogBuf.appendf("[%05d] ", g.FrameCount); + if (g.ContextName[0] != 0) + g.DebugLogBuf.appendf("[%s] [%05d] ", g.ContextName, g.FrameCount); + else + g.DebugLogBuf.appendf("[%05d] ", g.FrameCount); g.DebugLogBuf.appendfv(fmt, args); g.DebugLogIndex.append(g.DebugLogBuf.c_str(), old_size, g.DebugLogBuf.size()); if (g.DebugLogFlags & ImGuiDebugLogFlags_OutputToTTY) IMGUI_DEBUG_PRINTF("%s", g.DebugLogBuf.begin() + old_size); #ifdef IMGUI_ENABLE_TEST_ENGINE + // IMGUI_TEST_ENGINE_LOG() adds a trailing \n automatically + const int new_size = g.DebugLogBuf.size(); + const bool trailing_carriage_return = (g.DebugLogBuf[new_size - 1] == '\n'); if (g.DebugLogFlags & ImGuiDebugLogFlags_OutputToTestEngine) - IMGUI_TEST_ENGINE_LOG("%s", g.DebugLogBuf.begin() + old_size); + IMGUI_TEST_ENGINE_LOG("%.*s", new_size - old_size - (trailing_carriage_return ? 1 : 0), g.DebugLogBuf.begin() + old_size); #endif } @@ -15300,7 +15818,7 @@ static void SameLineOrWrap(const ImVec2& size) ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImVec2 pos(window->DC.CursorPosPrevLine.x + g.Style.ItemSpacing.x, window->DC.CursorPosPrevLine.y); - if (window->ClipRect.Contains(ImRect(pos, pos + size))) + if (window->WorkRect.Contains(ImRect(pos, pos + size))) ImGui::SameLine(); } @@ -15338,7 +15856,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) ShowDebugLogFlag("IO", ImGuiDebugLogFlags_EventIO); ShowDebugLogFlag("Nav", ImGuiDebugLogFlags_EventNav); ShowDebugLogFlag("Popup", ImGuiDebugLogFlags_EventPopup); - //ShowDebugLogFlag("Selection", ImGuiDebugLogFlags_EventSelection); + ShowDebugLogFlag("Selection", ImGuiDebugLogFlags_EventSelection); ShowDebugLogFlag("InputRouting", ImGuiDebugLogFlags_EventInputRouting); if (SmallButton("Clear")) @@ -15349,6 +15867,22 @@ void ImGui::ShowDebugLogWindow(bool* p_open) SameLine(); if (SmallButton("Copy")) SetClipboardText(g.DebugLogBuf.c_str()); + SameLine(); + if (SmallButton("Configure Outputs..")) + OpenPopup("Outputs"); + if (BeginPopup("Outputs")) + { + CheckboxFlags("OutputToTTY", &g.DebugLogFlags, ImGuiDebugLogFlags_OutputToTTY); +#ifndef IMGUI_ENABLE_TEST_ENGINE + BeginDisabled(); +#endif + CheckboxFlags("OutputToTestEngine", &g.DebugLogFlags, ImGuiDebugLogFlags_OutputToTestEngine); +#ifndef IMGUI_ENABLE_TEST_ENGINE + EndDisabled(); +#endif + EndPopup(); + } + BeginChild("##log", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); const ImGuiDebugLogFlags backup_log_flags = g.DebugLogFlags; @@ -15358,25 +15892,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) clipper.Begin(g.DebugLogIndex.size()); while (clipper.Step()) for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) - { - const char* line_begin = g.DebugLogIndex.get_line_begin(g.DebugLogBuf.c_str(), line_no); - const char* line_end = g.DebugLogIndex.get_line_end(g.DebugLogBuf.c_str(), line_no); - TextUnformatted(line_begin, line_end); // Display line - ImRect text_rect = g.LastItemData.Rect; - if (IsItemHovered()) - for (const char* p = line_begin; p <= line_end - 10; p++) // Search for 0x???????? identifiers - { - ImGuiID id = 0; - if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X') || sscanf(p + 2, "%X", &id) != 1) - continue; - ImVec2 p0 = CalcTextSize(line_begin, p); - ImVec2 p1 = CalcTextSize(p, p + 10); - g.LastItemData.Rect = ImRect(text_rect.Min + ImVec2(p0.x, 0.0f), text_rect.Min + ImVec2(p0.x + p1.x, p1.y)); - if (IsMouseHoveringRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, true)) - DebugLocateItemOnHover(id); - p += 10; - } - } + DebugTextUnformattedWithLocateItem(g.DebugLogIndex.get_line_begin(g.DebugLogBuf.c_str(), line_no), g.DebugLogIndex.get_line_end(g.DebugLogBuf.c_str(), line_no)); g.DebugLogFlags = backup_log_flags; if (GetScrollY() >= GetScrollMaxY()) SetScrollHereY(1.0f); @@ -15385,6 +15901,28 @@ void ImGui::ShowDebugLogWindow(bool* p_open) End(); } +// Display line, search for 0xXXXXXXXX identifiers and call DebugLocateItemOnHover() when hovered. +void ImGui::DebugTextUnformattedWithLocateItem(const char* line_begin, const char* line_end) +{ + TextUnformatted(line_begin, line_end); + if (!IsItemHovered()) + return; + ImGuiContext& g = *GImGui; + ImRect text_rect = g.LastItemData.Rect; + for (const char* p = line_begin; p <= line_end - 10; p++) + { + ImGuiID id = 0; + if (p[0] != '0' || (p[1] != 'x' && p[1] != 'X') || sscanf(p + 2, "%X", &id) != 1 || ImCharIsXdigitA(p[10])) + continue; + ImVec2 p0 = CalcTextSize(line_begin, p); + ImVec2 p1 = CalcTextSize(p, p + 10); + g.LastItemData.Rect = ImRect(text_rect.Min + ImVec2(p0.x, 0.0f), text_rect.Min + ImVec2(p0.x + p1.x, p1.y)); + if (IsMouseHoveringRect(g.LastItemData.Rect.Min, g.LastItemData.Rect.Max, true)) + DebugLocateItemOnHover(id); + p += 10; + } +} + //----------------------------------------------------------------------------- // [SECTION] OTHER DEBUG TOOLS (ITEM PICKER, ID STACK TOOL) //----------------------------------------------------------------------------- @@ -15642,7 +16180,7 @@ void ImGui::ShowIDStackToolWindow(bool* p_open) Checkbox("Ctrl+C: copy path to clipboard", &tool->CopyToClipboardOnCtrlC); SameLine(); TextColored((time_since_copy >= 0.0f && time_since_copy < 0.75f && ImFmod(time_since_copy, 0.25f) < 0.25f * 0.5f) ? ImVec4(1.f, 1.f, 0.3f, 1.f) : ImVec4(), "*COPIED*"); - if (tool->CopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, 0, ImGuiInputFlags_RouteGlobal)) + if (tool->CopyToClipboardOnCtrlC && Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, ImGuiInputFlags_RouteGlobal | ImGuiInputFlags_RouteOverFocused)) { tool->CopyToClipboardLastTime = (float)g.Time; char* p = g.TempBuffer.Data; @@ -15705,8 +16243,6 @@ void ImGui::DebugNodeWindowSettings(ImGuiWindowSettings*) {} void ImGui::DebugNodeWindowsList(ImVector*, const char*) {} void ImGui::DebugNodeViewport(ImGuiViewportP*) {} -void ImGui::DebugLog(const char*, ...) {} -void ImGui::DebugLogV(const char*, va_list) {} void ImGui::ShowDebugLogWindow(bool*) {} void ImGui::ShowIDStackToolWindow(bool*) {} void ImGui::DebugStartItemPicker() {} diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 707c14d3..4d6c2bbf 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (demo code) // Help: @@ -7,9 +7,14 @@ // - Need help integrating Dear ImGui in your codebase? // - Read Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started // - Read 'Programmer guide' in imgui.cpp for notes on how to setup Dear ImGui in your codebase. -// Read imgui.cpp for more details, documentation and comments. +// Read top of imgui.cpp and imgui.h for many details, documentation, comments, links. // Get the latest version at https://github.com/ocornut/imgui +// How to easily locate code? +// - Use Tools->Item Picker to debug break in code by clicking any widgets: https://github.com/ocornut/imgui/wiki/Debug-Tools +// - Browse an online version the demo with code linked to hovered widgets: https://pthom.github.io/imgui_manual_online/manual/imgui_manual.html +// - Find a visible string and search for it in the code! + //--------------------------------------------------- // PLEASE DO NOT REMOVE THIS FILE FROM YOUR PROJECT! //--------------------------------------------------- @@ -54,8 +59,10 @@ // Because we can't assume anything about your support of maths operators, we cannot use them in imgui_demo.cpp. // Navigating this file: -// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. -// - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments. +// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments. +// - You can search/grep for all sections listed in the index to find the section. /* @@ -63,13 +70,15 @@ Index of this file: // [SECTION] Forward Declarations // [SECTION] Helpers +// [SECTION] Helpers: ExampleTreeNode, ExampleMemberInfo (for use by Property Editor & Multi-Select demos) // [SECTION] Demo Window / ShowDemoWindow() -// - ShowDemoWindow() -// - sub section: ShowDemoWindowWidgets() -// - sub section: ShowDemoWindowLayout() -// - sub section: ShowDemoWindowPopups() -// - sub section: ShowDemoWindowTables() -// - sub section: ShowDemoWindowInputs() +// [SECTION] ShowDemoWindowMenuBar() +// [SECTION] ShowDemoWindowWidgets() +// [SECTION] ShowDemoWindowMultiSelect() +// [SECTION] ShowDemoWindowLayout() +// [SECTION] ShowDemoWindowPopups() +// [SECTION] ShowDemoWindowTables() +// [SECTION] ShowDemoWindowInputs() // [SECTION] About Window / ShowAboutWindow() // [SECTION] Style Editor / ShowStyleEditor() // [SECTION] User Guide / ShowUserGuide() @@ -86,6 +95,7 @@ Index of this file: // [SECTION] Example App: Manipulating window titles / ShowExampleAppWindowTitles() // [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering() // [SECTION] Example App: Documents Handling / ShowExampleAppDocuments() +// [SECTION] Example App: Assets Browser / ShowExampleAppAssetsBrowser() */ @@ -130,6 +140,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wreserved-id-macro" // warning: macro name is a reserved identifier #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wint-to-pointer-cast" // warning: cast to pointer from integer of different size @@ -182,19 +193,21 @@ Index of this file: #endif //----------------------------------------------------------------------------- -// [SECTION] Forward Declarations, Helpers +// [SECTION] Forward Declarations //----------------------------------------------------------------------------- #if !defined(IMGUI_DISABLE_DEMO_WINDOWS) // Forward Declarations +struct ImGuiDemoWindowData; static void ShowExampleAppMainMenuBar(); +static void ShowExampleAppAssetsBrowser(bool* p_open); static void ShowExampleAppConsole(bool* p_open); static void ShowExampleAppCustomRendering(bool* p_open); static void ShowExampleAppDocuments(bool* p_open); static void ShowExampleAppLog(bool* p_open); static void ShowExampleAppLayout(bool* p_open); -static void ShowExampleAppPropertyEditor(bool* p_open); +static void ShowExampleAppPropertyEditor(bool* p_open, ImGuiDemoWindowData* demo_data); static void ShowExampleAppSimpleOverlay(bool* p_open); static void ShowExampleAppAutoResize(bool* p_open); static void ShowExampleAppConstrainedResize(bool* p_open); @@ -204,8 +217,10 @@ static void ShowExampleAppWindowTitles(bool* p_open); static void ShowExampleMenuFile(); // We split the contents of the big ShowDemoWindow() function into smaller functions -// (because the link time of very large functions grow non-linearly) -static void ShowDemoWindowWidgets(); +// (because the link time of very large functions tends to grow non-linearly) +static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data); +static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data); +static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data); static void ShowDemoWindowLayout(); static void ShowDemoWindowPopups(); static void ShowDemoWindowTables(); @@ -239,17 +254,121 @@ void* GImGuiDemoMarkerCallbackUserData = NULL; #define IMGUI_DEMO_MARKER(section) do { if (GImGuiDemoMarkerCallback != NULL) GImGuiDemoMarkerCallback(__FILE__, __LINE__, section, GImGuiDemoMarkerCallbackUserData); } while (0) //----------------------------------------------------------------------------- -// [SECTION] Demo Window / ShowDemoWindow() +// [SECTION] Helpers: ExampleTreeNode, ExampleMemberInfo (for use by Property Editor etc.) +//----------------------------------------------------------------------------- + +// Simple representation for a tree +// (this is designed to be simple to understand for our demos, not to be fancy or efficient etc.) +struct ExampleTreeNode +{ + // Tree structure + char Name[28] = ""; + int UID = 0; + ExampleTreeNode* Parent = NULL; + ImVector Childs; + unsigned short IndexInParent = 0; // Maintaining this allows us to implement linear traversal more easily + + // Leaf Data + bool HasData = false; // All leaves have data + bool DataMyBool = true; + int DataMyInt = 128; + ImVec2 DataMyVec2 = ImVec2(0.0f, 3.141592f); +}; + +// Simple representation of struct metadata/serialization data. +// (this is a minimal version of what a typical advanced application may provide) +struct ExampleMemberInfo +{ + const char* Name; // Member name + ImGuiDataType DataType; // Member type + int DataCount; // Member count (1 when scalar) + int Offset; // Offset inside parent structure +}; + +// Metadata description of ExampleTreeNode struct. +static const ExampleMemberInfo ExampleTreeNodeMemberInfos[] +{ + { "MyBool", ImGuiDataType_Bool, 1, offsetof(ExampleTreeNode, DataMyBool) }, + { "MyInt", ImGuiDataType_S32, 1, offsetof(ExampleTreeNode, DataMyInt) }, + { "MyVec2", ImGuiDataType_Float, 2, offsetof(ExampleTreeNode, DataMyVec2) }, +}; + +static ExampleTreeNode* ExampleTree_CreateNode(const char* name, int uid, ExampleTreeNode* parent) +{ + ExampleTreeNode* node = IM_NEW(ExampleTreeNode); + snprintf(node->Name, IM_ARRAYSIZE(node->Name), "%s", name); + node->UID = uid; + node->Parent = parent; + node->IndexInParent = parent ? (unsigned short)parent->Childs.Size : 0; + if (parent) + parent->Childs.push_back(node); + return node; +} + +// Create example tree data +// (this allocates _many_ more times than most other code in either Dear ImGui or others demo) +static ExampleTreeNode* ExampleTree_CreateDemoTree() +{ + static const char* root_names[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pear", "Pineapple", "Strawberry", "Watermelon" }; + char name_buf[32]; + int uid = 0; + ExampleTreeNode* node_L0 = ExampleTree_CreateNode("", ++uid, NULL); + const int root_items_multiplier = 2; + for (int idx_L0 = 0; idx_L0 < IM_ARRAYSIZE(root_names) * root_items_multiplier; idx_L0++) + { + snprintf(name_buf, 32, "%s %d", root_names[idx_L0 / root_items_multiplier], idx_L0 % root_items_multiplier); + ExampleTreeNode* node_L1 = ExampleTree_CreateNode(name_buf, ++uid, node_L0); + const int number_of_childs = (int)strlen(node_L1->Name); + for (int idx_L1 = 0; idx_L1 < number_of_childs; idx_L1++) + { + snprintf(name_buf, 32, "Child %d", idx_L1); + ExampleTreeNode* node_L2 = ExampleTree_CreateNode(name_buf, ++uid, node_L1); + node_L2->HasData = true; + if (idx_L1 == 0) + { + snprintf(name_buf, 32, "Sub-child %d", 0); + ExampleTreeNode* node_L3 = ExampleTree_CreateNode(name_buf, ++uid, node_L2); + node_L3->HasData = true; + } + } + } + return node_L0; +} + //----------------------------------------------------------------------------- -// - ShowDemoWindow() -// - ShowDemoWindowWidgets() -// - ShowDemoWindowLayout() -// - ShowDemoWindowPopups() -// - ShowDemoWindowTables() -// - ShowDemoWindowColumns() -// - ShowDemoWindowInputs() +// [SECTION] Demo Window / ShowDemoWindow() //----------------------------------------------------------------------------- +// Data to be shared accross different functions of the demo. +struct ImGuiDemoWindowData +{ + // Examples Apps (accessible from the "Examples" menu) + bool ShowMainMenuBar = false; + bool ShowAppAssetsBrowser = false; + bool ShowAppConsole = false; + bool ShowAppCustomRendering = false; + bool ShowAppDocuments = false; + bool ShowAppLog = false; + bool ShowAppLayout = false; + bool ShowAppPropertyEditor = false; + bool ShowAppSimpleOverlay = false; + bool ShowAppAutoResize = false; + bool ShowAppConstrainedResize = false; + bool ShowAppFullscreen = false; + bool ShowAppLongText = false; + bool ShowAppWindowTitles = false; + + // Dear ImGui Tools (accessible from the "Tools" menu) + bool ShowMetrics = false; + bool ShowDebugLog = false; + bool ShowIDStackTool = false; + bool ShowStyleEditor = false; + bool ShowAbout = false; + + // Other data + ExampleTreeNode* DemoTree = NULL; +}; + // Demonstrate most Dear ImGui features (this is big function!) // You may execute this function to experiment with the UI and understand what it does. // You may then search for keywords in the code when you are interested by a specific feature. @@ -259,56 +378,39 @@ void ImGui::ShowDemoWindow(bool* p_open) // Most functions would normally just assert/crash if the context is missing. IM_ASSERT(ImGui::GetCurrentContext() != NULL && "Missing Dear ImGui context. Refer to examples app!"); + // Verify ABI compatibility between caller code and compiled version of Dear ImGui. This helps detects some build issues. + IMGUI_CHECKVERSION(); + + // Stored data + static ImGuiDemoWindowData demo_data; + // Examples Apps (accessible from the "Examples" menu) - static bool show_app_main_menu_bar = false; - static bool show_app_console = false; - static bool show_app_custom_rendering = false; - static bool show_app_documents = false; - static bool show_app_log = false; - static bool show_app_layout = false; - static bool show_app_property_editor = false; - static bool show_app_simple_overlay = false; - static bool show_app_auto_resize = false; - static bool show_app_constrained_resize = false; - static bool show_app_fullscreen = false; - static bool show_app_long_text = false; - static bool show_app_window_titles = false; - - if (show_app_main_menu_bar) ShowExampleAppMainMenuBar(); - if (show_app_documents) ShowExampleAppDocuments(&show_app_documents); - if (show_app_console) ShowExampleAppConsole(&show_app_console); - if (show_app_custom_rendering) ShowExampleAppCustomRendering(&show_app_custom_rendering); - if (show_app_log) ShowExampleAppLog(&show_app_log); - if (show_app_layout) ShowExampleAppLayout(&show_app_layout); - if (show_app_property_editor) ShowExampleAppPropertyEditor(&show_app_property_editor); - if (show_app_simple_overlay) ShowExampleAppSimpleOverlay(&show_app_simple_overlay); - if (show_app_auto_resize) ShowExampleAppAutoResize(&show_app_auto_resize); - if (show_app_constrained_resize) ShowExampleAppConstrainedResize(&show_app_constrained_resize); - if (show_app_fullscreen) ShowExampleAppFullscreen(&show_app_fullscreen); - if (show_app_long_text) ShowExampleAppLongText(&show_app_long_text); - if (show_app_window_titles) ShowExampleAppWindowTitles(&show_app_window_titles); + if (demo_data.ShowMainMenuBar) { ShowExampleAppMainMenuBar(); } + if (demo_data.ShowAppDocuments) { ShowExampleAppDocuments(&demo_data.ShowAppDocuments); } + if (demo_data.ShowAppAssetsBrowser) { ShowExampleAppAssetsBrowser(&demo_data.ShowAppAssetsBrowser); } + if (demo_data.ShowAppConsole) { ShowExampleAppConsole(&demo_data.ShowAppConsole); } + if (demo_data.ShowAppCustomRendering) { ShowExampleAppCustomRendering(&demo_data.ShowAppCustomRendering); } + if (demo_data.ShowAppLog) { ShowExampleAppLog(&demo_data.ShowAppLog); } + if (demo_data.ShowAppLayout) { ShowExampleAppLayout(&demo_data.ShowAppLayout); } + if (demo_data.ShowAppPropertyEditor) { ShowExampleAppPropertyEditor(&demo_data.ShowAppPropertyEditor, &demo_data); } + if (demo_data.ShowAppSimpleOverlay) { ShowExampleAppSimpleOverlay(&demo_data.ShowAppSimpleOverlay); } + if (demo_data.ShowAppAutoResize) { ShowExampleAppAutoResize(&demo_data.ShowAppAutoResize); } + if (demo_data.ShowAppConstrainedResize) { ShowExampleAppConstrainedResize(&demo_data.ShowAppConstrainedResize); } + if (demo_data.ShowAppFullscreen) { ShowExampleAppFullscreen(&demo_data.ShowAppFullscreen); } + if (demo_data.ShowAppLongText) { ShowExampleAppLongText(&demo_data.ShowAppLongText); } + if (demo_data.ShowAppWindowTitles) { ShowExampleAppWindowTitles(&demo_data.ShowAppWindowTitles); } // Dear ImGui Tools (accessible from the "Tools" menu) - static bool show_tool_metrics = false; - static bool show_tool_debug_log = false; - static bool show_tool_id_stack_tool = false; - static bool show_tool_style_editor = false; - static bool show_tool_about = false; - - if (show_tool_metrics) - ImGui::ShowMetricsWindow(&show_tool_metrics); - if (show_tool_debug_log) - ImGui::ShowDebugLogWindow(&show_tool_debug_log); - if (show_tool_id_stack_tool) - ImGui::ShowIDStackToolWindow(&show_tool_id_stack_tool); - if (show_tool_style_editor) - { - ImGui::Begin("Dear ImGui Style Editor", &show_tool_style_editor); + if (demo_data.ShowMetrics) { ImGui::ShowMetricsWindow(&demo_data.ShowMetrics); } + if (demo_data.ShowDebugLog) { ImGui::ShowDebugLogWindow(&demo_data.ShowDebugLog); } + if (demo_data.ShowIDStackTool) { ImGui::ShowIDStackToolWindow(&demo_data.ShowIDStackTool); } + if (demo_data.ShowAbout) { ImGui::ShowAboutWindow(&demo_data.ShowAbout); } + if (demo_data.ShowStyleEditor) + { + ImGui::Begin("Dear ImGui Style Editor", &demo_data.ShowStyleEditor); ImGui::ShowStyleEditor(); ImGui::End(); } - if (show_tool_about) - ImGui::ShowAboutWindow(&show_tool_about); // Demonstrate the various window flags. Typically you would just use the default! static bool no_titlebar = false; @@ -351,67 +453,11 @@ void ImGui::ShowDemoWindow(bool* p_open) } // Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details. - // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align) - //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); - // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets. - ImGui::PushItemWidth(ImGui::GetFontSize() * -12); + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets. + //ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align) // Menu Bar - if (ImGui::BeginMenuBar()) - { - if (ImGui::BeginMenu("Menu")) - { - IMGUI_DEMO_MARKER("Menu/File"); - ShowExampleMenuFile(); - ImGui::EndMenu(); - } - if (ImGui::BeginMenu("Examples")) - { - IMGUI_DEMO_MARKER("Menu/Examples"); - ImGui::MenuItem("Main menu bar", NULL, &show_app_main_menu_bar); - - ImGui::SeparatorText("Mini apps"); - ImGui::MenuItem("Console", NULL, &show_app_console); - ImGui::MenuItem("Custom rendering", NULL, &show_app_custom_rendering); - ImGui::MenuItem("Documents", NULL, &show_app_documents); - ImGui::MenuItem("Log", NULL, &show_app_log); - ImGui::MenuItem("Property editor", NULL, &show_app_property_editor); - ImGui::MenuItem("Simple layout", NULL, &show_app_layout); - ImGui::MenuItem("Simple overlay", NULL, &show_app_simple_overlay); - - ImGui::SeparatorText("Concepts"); - ImGui::MenuItem("Auto-resizing window", NULL, &show_app_auto_resize); - ImGui::MenuItem("Constrained-resizing window", NULL, &show_app_constrained_resize); - ImGui::MenuItem("Fullscreen window", NULL, &show_app_fullscreen); - ImGui::MenuItem("Long text display", NULL, &show_app_long_text); - ImGui::MenuItem("Manipulating window titles", NULL, &show_app_window_titles); - - ImGui::EndMenu(); - } - //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar! - if (ImGui::BeginMenu("Tools")) - { - IMGUI_DEMO_MARKER("Menu/Tools"); -#ifndef IMGUI_DISABLE_DEBUG_TOOLS - const bool has_debug_tools = true; -#else - const bool has_debug_tools = false; -#endif - ImGui::MenuItem("Metrics/Debugger", NULL, &show_tool_metrics, has_debug_tools); - ImGui::MenuItem("Debug Log", NULL, &show_tool_debug_log, has_debug_tools); - ImGui::MenuItem("ID Stack Tool", NULL, &show_tool_id_stack_tool, has_debug_tools); - ImGui::MenuItem("Style Editor", NULL, &show_tool_style_editor); - bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent; - if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present)) - ImGui::DebugStartItemPicker(); - if (!is_debugger_present) - ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools."); - ImGui::Separator(); - ImGui::MenuItem("About Dear ImGui", NULL, &show_tool_about); - ImGui::EndMenu(); - } - ImGui::EndMenuBar(); - } + ShowDemoWindowMenuBar(&demo_data); ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); ImGui::Spacing(); @@ -429,7 +475,9 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::BulletText("See the ShowDemoWindow() code in imgui_demo.cpp. <- you are here!"); ImGui::BulletText("See comments in imgui.cpp."); ImGui::BulletText("See example applications in the examples/ folder."); - ImGui::BulletText("Read the FAQ at https://www.dearimgui.com/faq/"); + ImGui::BulletText("Read the FAQ at "); + ImGui::SameLine(0, 0); + ImGui::TextLinkOpenURL("https://www.dearimgui.com/faq/"); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableKeyboard' for keyboard controls."); ImGui::BulletText("Set 'io.ConfigFlags |= NavEnableGamepad' for gamepad controls."); @@ -452,19 +500,26 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", &io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", &io.ConfigFlags, ImGuiConfigFlags_NoMouse); + ImGui::SameLine(); HelpMarker("Instruct dear imgui to disable mouse inputs and interactions."); + + // The "NoMouse" option can get us stuck with a disabled mouse! Let's provide an alternative way to fix it: if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) { - // The "NoMouse" option can get us stuck with a disabled mouse! Let's provide an alternative way to fix it: if (fmodf((float)ImGui::GetTime(), 0.40f) < 0.20f) { ImGui::SameLine(); ImGui::Text("<>"); } - if (ImGui::IsKeyPressed(ImGuiKey_Space)) + // Prevent both being checked + if (ImGui::IsKeyPressed(ImGuiKey_Space) || (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard)) io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse; } - ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); + + ImGui::CheckboxFlags("io.ConfigFlags: NoMouseCursorChange", &io.ConfigFlags, ImGuiConfigFlags_NoMouseCursorChange); ImGui::SameLine(); HelpMarker("Instruct backend to not alter mouse cursor shape and visibility."); + ImGui::CheckboxFlags("io.ConfigFlags: NoKeyboard", &io.ConfigFlags, ImGuiConfigFlags_NoKeyboard); + ImGui::SameLine(); HelpMarker("Instruct dear imgui to disable keyboard inputs and interactions."); + ImGui::Checkbox("io.ConfigInputTrickleEventQueue", &io.ConfigInputTrickleEventQueue); ImGui::SameLine(); HelpMarker("Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates."); ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); @@ -481,6 +536,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); ImGui::Checkbox("io.ConfigMacOSXBehaviors", &io.ConfigMacOSXBehaviors); + ImGui::SameLine(); HelpMarker("Swap Cmd<>Ctrl keys, enable various MacOS style behaviors."); ImGui::Text("Also see Style->Rendering for rendering options."); ImGui::SeparatorText("Debug"); @@ -522,8 +578,9 @@ void ImGui::ShowDemoWindow(bool* p_open) IMGUI_DEMO_MARKER("Configuration/Style"); if (ImGui::TreeNode("Style")) { + ImGui::Checkbox("Style Editor", &demo_data.ShowStyleEditor); + ImGui::SameLine(); HelpMarker("The same contents can be accessed in 'Tools->Style Editor' or by calling the ShowStyleEditor() function."); - ImGui::ShowStyleEditor(); ImGui::TreePop(); ImGui::Spacing(); } @@ -569,7 +626,7 @@ void ImGui::ShowDemoWindow(bool* p_open) } // All demo contents - ShowDemoWindowWidgets(); + ShowDemoWindowWidgets(&demo_data); ShowDemoWindowLayout(); ShowDemoWindowPopups(); ShowDemoWindowTables(); @@ -580,9 +637,79 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::End(); } -static void ShowDemoWindowWidgets() +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowMenuBar() +//----------------------------------------------------------------------------- + +static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data) +{ + IMGUI_DEMO_MARKER("Menu"); + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Menu")) + { + IMGUI_DEMO_MARKER("Menu/File"); + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Examples")) + { + IMGUI_DEMO_MARKER("Menu/Examples"); + ImGui::MenuItem("Main menu bar", NULL, &demo_data->ShowMainMenuBar); + + ImGui::SeparatorText("Mini apps"); + ImGui::MenuItem("Assets Browser", NULL, &demo_data->ShowAppAssetsBrowser); + ImGui::MenuItem("Console", NULL, &demo_data->ShowAppConsole); + ImGui::MenuItem("Custom rendering", NULL, &demo_data->ShowAppCustomRendering); + ImGui::MenuItem("Documents", NULL, &demo_data->ShowAppDocuments); + ImGui::MenuItem("Log", NULL, &demo_data->ShowAppLog); + ImGui::MenuItem("Property editor", NULL, &demo_data->ShowAppPropertyEditor); + ImGui::MenuItem("Simple layout", NULL, &demo_data->ShowAppLayout); + ImGui::MenuItem("Simple overlay", NULL, &demo_data->ShowAppSimpleOverlay); + + ImGui::SeparatorText("Concepts"); + ImGui::MenuItem("Auto-resizing window", NULL, &demo_data->ShowAppAutoResize); + ImGui::MenuItem("Constrained-resizing window", NULL, &demo_data->ShowAppConstrainedResize); + ImGui::MenuItem("Fullscreen window", NULL, &demo_data->ShowAppFullscreen); + ImGui::MenuItem("Long text display", NULL, &demo_data->ShowAppLongText); + ImGui::MenuItem("Manipulating window titles", NULL, &demo_data->ShowAppWindowTitles); + + ImGui::EndMenu(); + } + //if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar! + if (ImGui::BeginMenu("Tools")) + { + IMGUI_DEMO_MARKER("Menu/Tools"); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + const bool has_debug_tools = true; +#else + const bool has_debug_tools = false; +#endif + ImGui::MenuItem("Metrics/Debugger", NULL, &demo_data->ShowMetrics, has_debug_tools); + ImGui::MenuItem("Debug Log", NULL, &demo_data->ShowDebugLog, has_debug_tools); + ImGui::MenuItem("ID Stack Tool", NULL, &demo_data->ShowIDStackTool, has_debug_tools); + ImGui::MenuItem("Style Editor", NULL, &demo_data->ShowStyleEditor); + bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent; + if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present)) + ImGui::DebugStartItemPicker(); + if (!is_debugger_present) + ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools."); + ImGui::Separator(); + ImGui::MenuItem("About Dear ImGui", NULL, &demo_data->ShowAbout); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } +} + +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowWidgets() +//----------------------------------------------------------------------------- + +static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) { IMGUI_DEMO_MARKER("Widgets"); + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (!ImGui::CollapsingHeader("Widgets")) return; @@ -641,11 +768,11 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Basic/Buttons (Repeating)"); static int counter = 0; float spacing = ImGui::GetStyle().ItemInnerSpacing.x; - ImGui::PushButtonRepeat(true); + ImGui::PushItemFlag(ImGuiItemFlags_ButtonRepeat, true); if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { counter--; } ImGui::SameLine(0.0f, spacing); if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { counter++; } - ImGui::PopButtonRepeat(); + ImGui::PopItemFlag(); ImGui::SameLine(); ImGui::Text("%d", counter); @@ -702,18 +829,19 @@ static void ShowDemoWindowWidgets() { IMGUI_DEMO_MARKER("Widgets/Basic/DragInt, DragFloat"); - static int i1 = 50, i2 = 42; + static int i1 = 50, i2 = 42, i3 = 128; ImGui::DragInt("drag int", &i1, 1); ImGui::SameLine(); HelpMarker( "Click and drag to edit value.\n" "Hold SHIFT/ALT for faster/slower edit.\n" "Double-click or CTRL+click to input value."); - ImGui::DragInt("drag int 0..100", &i2, 1, 0, 100, "%d%%", ImGuiSliderFlags_AlwaysClamp); + ImGui::DragInt("drag int wrap 100..200", &i3, 1, 100, 200, "%d", ImGuiSliderFlags_WrapAround); static float f1 = 1.00f, f2 = 0.0067f; ImGui::DragFloat("drag float", &f1, 0.005f); ImGui::DragFloat("drag small float", &f2, 0.0001f, 0.0f, 0.0f, "%.06f ns"); + //ImGui::DragFloat("drag wrap -1..1", &f3, 0.005f, -1.0f, 1.0f, NULL, ImGuiSliderFlags_WrapAround); } ImGui::SeparatorText("Sliders"); @@ -870,12 +998,11 @@ static void ShowDemoWindowWidgets() // Using ImGuiHoveredFlags_ForTooltip will pull flags from 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav', // which default value include the ImGuiHoveredFlags_AllowWhenDisabled flag. - // As a result, Set ImGui::BeginDisabled(); ImGui::Button("Disabled item", sz); - ImGui::EndDisabled(); if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip)) ImGui::SetTooltip("I am a a tooltip for a disabled item."); + ImGui::EndDisabled(); ImGui::TreePop(); } @@ -899,13 +1026,18 @@ static void ShowDemoWindowWidgets() if (i == 0) ImGui::SetNextItemOpen(true, ImGuiCond_Once); - if (ImGui::TreeNode((void*)(intptr_t)i, "Child %d", i)) + // Here we use PushID() to generate a unique base ID, and then the "" used as TreeNode id won't conflict. + // An alternative to using 'PushID() + TreeNode("", ...)' to generate a unique ID is to use 'TreeNode((void*)(intptr_t)i, ...)', + // aka generate a dummy pointer-sized value to be hashed. The demo below uses that technique. Both are fine. + ImGui::PushID(i); + if (ImGui::TreeNode("", "Child %d", i)) { ImGui::Text("blah blah"); ImGui::SameLine(); if (ImGui::SmallButton("button")) {} ImGui::TreePop(); } + ImGui::PopID(); } ImGui::TreePop(); } @@ -923,7 +1055,11 @@ static void ShowDemoWindowWidgets() ImGui::CheckboxFlags("ImGuiTreeNodeFlags_OpenOnDoubleClick", &base_flags, ImGuiTreeNodeFlags_OpenOnDoubleClick); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAvailWidth", &base_flags, ImGuiTreeNodeFlags_SpanAvailWidth); ImGui::SameLine(); HelpMarker("Extend hit area to all available width instead of allowing more items to be laid out after the node."); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &base_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanTextWidth", &base_flags, ImGuiTreeNodeFlags_SpanTextWidth); ImGui::SameLine(); HelpMarker("Reduce hit area to the text label and a bit of margin."); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &base_flags, ImGuiTreeNodeFlags_SpanAllColumns); ImGui::SameLine(); HelpMarker("For use in Tables only."); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_AllowOverlap", &base_flags, ImGuiTreeNodeFlags_AllowOverlap); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_Framed", &base_flags, ImGuiTreeNodeFlags_Framed); ImGui::SameLine(); HelpMarker("Draw frame with background (e.g. for CollapsingHeader)"); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_NavLeftJumpsBackHere", &base_flags, ImGuiTreeNodeFlags_NavLeftJumpsBackHere); ImGui::Checkbox("Align label with current X position", &align_label_with_current_x_position); ImGui::Checkbox("Test tree node as drag source", &test_drag_and_drop); ImGui::Text("Hello!"); @@ -956,9 +1092,17 @@ static void ShowDemoWindowWidgets() ImGui::Text("This is a drag and drop source"); ImGui::EndDragDropSource(); } + if (i == 2 && (base_flags & ImGuiTreeNodeFlags_SpanTextWidth)) + { + // Item 2 has an additional inline button to help demonstrate SpanTextWidth. + ImGui::SameLine(); + if (ImGui::SmallButton("button")) {} + } if (node_open) { ImGui::BulletText("Blah blah\nBlah Blah"); + ImGui::SameLine(); + ImGui::SmallButton("Button"); ImGui::TreePop(); } } @@ -1220,18 +1364,18 @@ static void ShowDemoWindowWidgets() // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; - static int item_current_idx = 0; // Here we store our selection data as an index. + static int item_selected_idx = 0; // Here we store our selection data as an index. // Pass in the preview value visible before opening the combo (it could technically be different contents or not pulled from items[]) - const char* combo_preview_value = items[item_current_idx]; + const char* combo_preview_value = items[item_selected_idx]; if (ImGui::BeginCombo("combo 1", combo_preview_value, flags)) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { - const bool is_selected = (item_current_idx == n); + const bool is_selected = (item_selected_idx == n); if (ImGui::Selectable(items[n], is_selected)) - item_current_idx = n; + item_selected_idx = n; // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) if (is_selected) @@ -1273,14 +1417,22 @@ static void ShowDemoWindowWidgets() // (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively // stored in the object itself, etc.) const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" }; - static int item_current_idx = 0; // Here we store our selection data as an index. + static int item_selected_idx = 0; // Here we store our selected data as an index. + + static bool item_highlight = false; + int item_highlighted_idx = -1; // Here we store our highlighted data as an index. + ImGui::Checkbox("Highlight hovered item in second listbox", &item_highlight); + if (ImGui::BeginListBox("listbox 1")) { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { - const bool is_selected = (item_current_idx == n); + const bool is_selected = (item_selected_idx == n); if (ImGui::Selectable(items[n], is_selected)) - item_current_idx = n; + item_selected_idx = n; + + if (item_highlight && ImGui::IsItemHovered()) + item_highlighted_idx = n; // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) if (is_selected) @@ -1288,6 +1440,7 @@ static void ShowDemoWindowWidgets() } ImGui::EndListBox(); } + ImGui::SameLine(); HelpMarker("Here we are sharing selection state between both boxes."); // Custom size: use all width, 5 items tall ImGui::Text("Full-width:"); @@ -1295,9 +1448,10 @@ static void ShowDemoWindowWidgets() { for (int n = 0; n < IM_ARRAYSIZE(items); n++) { - const bool is_selected = (item_current_idx == n); - if (ImGui::Selectable(items[n], is_selected)) - item_current_idx = n; + bool is_selected = (item_selected_idx == n); + ImGuiSelectableFlags flags = (item_highlighted_idx == n) ? ImGuiSelectableFlags_Highlight : 0; + if (ImGui::Selectable(items[n], is_selected, flags)) + item_selected_idx = n; // Set the initial focus when opening the combo (scrolling + keyboard navigation focus) if (is_selected) @@ -1310,6 +1464,7 @@ static void ShowDemoWindowWidgets() } IMGUI_DEMO_MARKER("Widgets/Selectables"); + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); if (ImGui::TreeNode("Selectables")) { // Selectable() has 2 overloads: @@ -1331,37 +1486,6 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Widgets/Selectables/Single Selection"); - if (ImGui::TreeNode("Selection State: Single Selection")) - { - static int selected = -1; - for (int n = 0; n < 5; n++) - { - char buf[32]; - sprintf(buf, "Object %d", n); - if (ImGui::Selectable(buf, selected == n)) - selected = n; - } - ImGui::TreePop(); - } - IMGUI_DEMO_MARKER("Widgets/Selectables/Multiple Selection"); - if (ImGui::TreeNode("Selection State: Multiple Selection")) - { - HelpMarker("Hold CTRL and click to select multiple items."); - static bool selection[5] = { false, false, false, false, false }; - for (int n = 0; n < 5; n++) - { - char buf[32]; - sprintf(buf, "Object %d", n); - if (ImGui::Selectable(buf, selection[n])) - { - if (!ImGui::GetIO().KeyCtrl) // Clear selection when CTRL is not held - memset(selection, 0, sizeof(selection)); - selection[n] ^= 1; - } - } - ImGui::TreePop(); - } IMGUI_DEMO_MARKER("Widgets/Selectables/Rendering more items on the same line"); if (ImGui::TreeNode("Rendering more items on the same line")) { @@ -1374,8 +1498,8 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Widgets/Selectables/In columns"); - if (ImGui::TreeNode("In columns")) + IMGUI_DEMO_MARKER("Widgets/Selectables/In Tables"); + if (ImGui::TreeNode("In Tables")) { static bool selected[10] = {}; @@ -1469,6 +1593,8 @@ static void ShowDemoWindowWidgets() ImGui::TreePop(); } + ShowDemoWindowMultiSelect(demo_data); + // To wire InputText() with std::string or any other custom string type, // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. IMGUI_DEMO_MARKER("Widgets/Text Input"); @@ -1704,6 +1830,7 @@ static void ShowDemoWindowWidgets() ImGui::CheckboxFlags("ImGuiTabBarFlags_AutoSelectNewTabs", &tab_bar_flags, ImGuiTabBarFlags_AutoSelectNewTabs); ImGui::CheckboxFlags("ImGuiTabBarFlags_TabListPopupButton", &tab_bar_flags, ImGuiTabBarFlags_TabListPopupButton); ImGui::CheckboxFlags("ImGuiTabBarFlags_NoCloseWithMiddleMouseButton", &tab_bar_flags, ImGuiTabBarFlags_NoCloseWithMiddleMouseButton); + ImGui::CheckboxFlags("ImGuiTabBarFlags_DrawSelectedOverline", &tab_bar_flags, ImGuiTabBarFlags_DrawSelectedOverline); if ((tab_bar_flags & ImGuiTabBarFlags_FittingPolicyMask_) == 0) tab_bar_flags |= ImGuiTabBarFlags_FittingPolicyDefault_; if (ImGui::CheckboxFlags("ImGuiTabBarFlags_FittingPolicyResizeDown", &tab_bar_flags, ImGuiTabBarFlags_FittingPolicyResizeDown)) @@ -1712,11 +1839,13 @@ static void ShowDemoWindowWidgets() tab_bar_flags &= ~(ImGuiTabBarFlags_FittingPolicyMask_ ^ ImGuiTabBarFlags_FittingPolicyScroll); // Tab Bar + ImGui::AlignTextToFramePadding(); + ImGui::Text("Opened:"); const char* names[4] = { "Artichoke", "Beetroot", "Celery", "Daikon" }; static bool opened[4] = { true, true, true, true }; // Persistent user state for (int n = 0; n < IM_ARRAYSIZE(opened); n++) { - if (n > 0) { ImGui::SameLine(); } + ImGui::SameLine(); ImGui::Checkbox(names[n], &opened[n]); } @@ -1818,10 +1947,10 @@ static void ShowDemoWindowWidgets() ImGui::Checkbox("Animate", &animate); // Plot as lines and plot as histogram - IMGUI_DEMO_MARKER("Widgets/Plotting/PlotLines, PlotHistogram"); static float arr[] = { 0.6f, 0.1f, 1.0f, 0.5f, 0.92f, 0.1f, 0.2f }; ImGui::PlotLines("Frame Times", arr, IM_ARRAYSIZE(arr)); ImGui::PlotHistogram("Histogram", arr, IM_ARRAYSIZE(arr), 0, NULL, 0.0f, 1.0f, ImVec2(0, 80.0f)); + //ImGui::SameLine(); HelpMarker("Consider using ImPlot instead!"); // Fill an array of contiguous float values to plot // Tip: If your float aren't contiguous but part of a structure, you can pass a pointer to your first float @@ -1871,15 +2000,17 @@ static void ShowDemoWindowWidgets() ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); ImGui::Separator(); + ImGui::TreePop(); + } + + IMGUI_DEMO_MARKER("Widgets/Progress Bars"); + if (ImGui::TreeNode("Progress Bars")) + { // Animate a simple progress bar - IMGUI_DEMO_MARKER("Widgets/Plotting/ProgressBar"); static float progress = 0.0f, progress_dir = 1.0f; - if (animate) - { - progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime; - if (progress >= +1.1f) { progress = +1.1f; progress_dir *= -1.0f; } - if (progress <= -0.1f) { progress = -0.1f; progress_dir *= -1.0f; } - } + progress += progress_dir * 0.4f * ImGui::GetIO().DeltaTime; + if (progress >= +1.1f) { progress = +1.1f; progress_dir *= -1.0f; } + if (progress <= -0.1f) { progress = -0.1f; progress_dir *= -1.0f; } // Typically we would use ImVec2(-1.0f,0.0f) or ImVec2(-FLT_MIN,0.0f) to use all available width, // or ImVec2(width,0.0f) for a specified width. ImVec2(0.0f,0.0f) uses ItemWidth. @@ -1891,6 +2022,13 @@ static void ShowDemoWindowWidgets() char buf[32]; sprintf(buf, "%d/%d", (int)(progress_saturated * 1753), 1753); ImGui::ProgressBar(progress, ImVec2(0.f, 0.f), buf); + + // Pass an animated negative value, e.g. -1.0f * (float)ImGui::GetTime() is the recommended value. + // Adjust the factor if you want to adjust the animation speed. + ImGui::ProgressBar(-1.0f * (float)ImGui::GetTime(), ImVec2(0.0f, 0.0f), "Searching.."); + ImGui::SameLine(0.0f, ImGui::GetStyle().ItemInnerSpacing.x); + ImGui::Text("Indeterminate"); + ImGui::TreePop(); } @@ -2100,6 +2238,8 @@ static void ShowDemoWindowWidgets() ImGui::SameLine(); HelpMarker("Disable rounding underlying value to match precision of the format string (e.g. %.3f values are rounded to those 3 digits)."); ImGui::CheckboxFlags("ImGuiSliderFlags_NoInput", &flags, ImGuiSliderFlags_NoInput); ImGui::SameLine(); HelpMarker("Disable CTRL+Click or Enter key allowing to input text directly into the widget."); + ImGui::CheckboxFlags("ImGuiSliderFlags_WrapAround", &flags, ImGuiSliderFlags_WrapAround); + ImGui::SameLine(); HelpMarker("Enable wrapping around from max to min and from min to max (only supported by DragXXX() functions)"); // Drags static float drag_f = 0.5f; @@ -2114,9 +2254,10 @@ static void ShowDemoWindowWidgets() // Sliders static float slider_f = 0.5f; static int slider_i = 50; + const ImGuiSliderFlags flags_for_sliders = flags & ~ImGuiSliderFlags_WrapAround; ImGui::Text("Underlying float value: %f", slider_f); - ImGui::SliderFloat("SliderFloat (0 -> 1)", &slider_f, 0.0f, 1.0f, "%.3f", flags); - ImGui::SliderInt("SliderInt (0 -> 100)", &slider_i, 0, 100, "%d", flags); + ImGui::SliderFloat("SliderFloat (0 -> 1)", &slider_f, 0.0f, 1.0f, "%.3f", flags_for_sliders); + ImGui::SliderInt("SliderInt (0 -> 100)", &slider_i, 0, 100, "%d", flags_for_sliders); ImGui::TreePop(); } @@ -2238,20 +2379,24 @@ static void ShowDemoWindowWidgets() IMGUI_DEMO_MARKER("Widgets/Data Types/Inputs"); static bool inputs_step = true; + static ImGuiInputTextFlags flags = ImGuiInputTextFlags_None; ImGui::SeparatorText("Inputs"); ImGui::Checkbox("Show step buttons", &inputs_step); - ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d"); - ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u"); - ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d"); - ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u"); - ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d"); - ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%04X"); - ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u"); - ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X"); - ImGui::InputScalar("input s64", ImGuiDataType_S64, &s64_v, inputs_step ? &s64_one : NULL); - ImGui::InputScalar("input u64", ImGuiDataType_U64, &u64_v, inputs_step ? &u64_one : NULL); - ImGui::InputScalar("input float", ImGuiDataType_Float, &f32_v, inputs_step ? &f32_one : NULL); - ImGui::InputScalar("input double", ImGuiDataType_Double, &f64_v, inputs_step ? &f64_one : NULL); + ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly); + ImGui::CheckboxFlags("ImGuiInputTextFlags_ParseEmptyRefVal", &flags, ImGuiInputTextFlags_ParseEmptyRefVal); + ImGui::CheckboxFlags("ImGuiInputTextFlags_DisplayEmptyRefVal", &flags, ImGuiInputTextFlags_DisplayEmptyRefVal); + ImGui::InputScalar("input s8", ImGuiDataType_S8, &s8_v, inputs_step ? &s8_one : NULL, NULL, "%d", flags); + ImGui::InputScalar("input u8", ImGuiDataType_U8, &u8_v, inputs_step ? &u8_one : NULL, NULL, "%u", flags); + ImGui::InputScalar("input s16", ImGuiDataType_S16, &s16_v, inputs_step ? &s16_one : NULL, NULL, "%d", flags); + ImGui::InputScalar("input u16", ImGuiDataType_U16, &u16_v, inputs_step ? &u16_one : NULL, NULL, "%u", flags); + ImGui::InputScalar("input s32", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%d", flags); + ImGui::InputScalar("input s32 hex", ImGuiDataType_S32, &s32_v, inputs_step ? &s32_one : NULL, NULL, "%04X", flags); + ImGui::InputScalar("input u32", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%u", flags); + ImGui::InputScalar("input u32 hex", ImGuiDataType_U32, &u32_v, inputs_step ? &u32_one : NULL, NULL, "%08X", flags); + ImGui::InputScalar("input s64", ImGuiDataType_S64, &s64_v, inputs_step ? &s64_one : NULL, NULL, NULL, flags); + ImGui::InputScalar("input u64", ImGuiDataType_U64, &u64_v, inputs_step ? &u64_one : NULL, NULL, NULL, flags); + ImGui::InputScalar("input float", ImGuiDataType_Float, &f32_v, inputs_step ? &f32_one : NULL, NULL, NULL, flags); + ImGui::InputScalar("input double", ImGuiDataType_Double, &f64_v, inputs_step ? &f64_one : NULL, NULL, NULL, flags); ImGui::TreePop(); } @@ -2481,9 +2626,7 @@ static void ShowDemoWindowWidgets() { IM_UNUSED(payload); ImGui::SetMouseCursor(ImGuiMouseCursor_NotAllowed); - ImGui::BeginTooltip(); - ImGui::Text("Cannot drop here!"); - ImGui::EndTooltip(); + ImGui::SetTooltip("Cannot drop here!"); } ImGui::EndDragDropTarget(); } @@ -2525,7 +2668,7 @@ static void ShowDemoWindowWidgets() ImGui::BeginDisabled(true); if (item_type == 0) { ImGui::Text("ITEM: Text"); } // Testing text items with no identifier/interaction if (item_type == 1) { ret = ImGui::Button("ITEM: Button"); } // Testing button - if (item_type == 2) { ImGui::PushButtonRepeat(true); ret = ImGui::Button("ITEM: Button"); ImGui::PopButtonRepeat(); } // Testing button (with repeater) + if (item_type == 2) { ImGui::PushItemFlag(ImGuiItemFlags_ButtonRepeat, true); ret = ImGui::Button("ITEM: Button"); ImGui::PopItemFlag(); } // Testing button (with repeater) if (item_type == 3) { ret = ImGui::Checkbox("ITEM: Checkbox", &b); } // Testing checkbox if (item_type == 4) { ret = ImGui::SliderFloat("ITEM: SliderFloat", &col4f[0], 0.0f, 1.0f); } // Testing basic item if (item_type == 5) { ret = ImGui::InputText("ITEM: InputText", &str[0], IM_ARRAYSIZE(str)); } // Testing input text (which handles tabbing) @@ -2728,68 +2871,1039 @@ static void ShowDemoWindowWidgets() } } -static void ShowDemoWindowLayout() +static const char* ExampleNames[] = { - IMGUI_DEMO_MARKER("Layout"); - if (!ImGui::CollapsingHeader("Layout & Scrolling")) - return; + "Artichoke", "Arugula", "Asparagus", "Avocado", "Bamboo Shoots", "Bean Sprouts", "Beans", "Beet", "Belgian Endive", "Bell Pepper", + "Bitter Gourd", "Bok Choy", "Broccoli", "Brussels Sprouts", "Burdock Root", "Cabbage", "Calabash", "Capers", "Carrot", "Cassava", + "Cauliflower", "Celery", "Celery Root", "Celcuce", "Chayote", "Chinese Broccoli", "Corn", "Cucumber" +}; - IMGUI_DEMO_MARKER("Layout/Child windows"); - if (ImGui::TreeNode("Child windows")) +// Extra functions to add deletion support to ImGuiSelectionBasicStorage +struct ExampleSelectionWithDeletion : ImGuiSelectionBasicStorage +{ + // Find which item should be Focused after deletion. + // Call _before_ item submission. Retunr an index in the before-deletion item list, your item loop should call SetKeyboardFocusHere() on it. + // The subsequent ApplyDeletionPostLoop() code will use it to apply Selection. + // - We cannot provide this logic in core Dear ImGui because we don't have access to selection data. + // - We don't actually manipulate the ImVector<> here, only in ApplyDeletionPostLoop(), but using similar API for consistency and flexibility. + // - Important: Deletion only works if the underlying ImGuiID for your items are stable: aka not depend on their index, but on e.g. item id/ptr. + // FIXME-MULTISELECT: Doesn't take account of the possibility focus target will be moved during deletion. Need refocus or scroll offset. + int ApplyDeletionPreLoop(ImGuiMultiSelectIO* ms_io, int items_count) { - ImGui::SeparatorText("Child windows"); + if (Size == 0) + return -1; - HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window."); - static bool disable_mouse_wheel = false; - static bool disable_menu = false; - ImGui::Checkbox("Disable Mouse Wheel", &disable_mouse_wheel); - ImGui::Checkbox("Disable Menu", &disable_menu); + // If focused item is not selected... + const int focused_idx = (int)ms_io->NavIdItem; // Index of currently focused item + if (ms_io->NavIdSelected == false) // This is merely a shortcut, == Contains(adapter->IndexToStorage(items, focused_idx)) + { + ms_io->RangeSrcReset = true; // Request to recover RangeSrc from NavId next frame. Would be ok to reset even when NavIdSelected==true, but it would take an extra frame to recover RangeSrc when deleting a selected item. + return focused_idx; // Request to focus same item after deletion. + } - // Child 1: no border, enable horizontal scrollbar + // If focused item is selected: land on first unselected item after focused item. + for (int idx = focused_idx + 1; idx < items_count; idx++) + if (!Contains(GetStorageIdFromIndex(idx))) + return idx; + + // If focused item is selected: otherwise return last unselected item before focused item. + for (int idx = IM_MIN(focused_idx, items_count) - 1; idx >= 0; idx--) + if (!Contains(GetStorageIdFromIndex(idx))) + return idx; + + return -1; + } + + // Rewrite item list (delete items) + update selection. + // - Call after EndMultiSelect() + // - We cannot provide this logic in core Dear ImGui because we don't have access to your items, nor to selection data. + template + void ApplyDeletionPostLoop(ImGuiMultiSelectIO* ms_io, ImVector& items, int item_curr_idx_to_select) + { + // Rewrite item list (delete items) + convert old selection index (before deletion) to new selection index (after selection). + // If NavId was not part of selection, we will stay on same item. + ImVector new_items; + new_items.reserve(items.Size - Size); + int item_next_idx_to_select = -1; + for (int idx = 0; idx < items.Size; idx++) { - ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; - if (disable_mouse_wheel) - window_flags |= ImGuiWindowFlags_NoScrollWithMouse; - ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), ImGuiChildFlags_None, window_flags); - for (int i = 0; i < 100; i++) - ImGui::Text("%04d: scrollable region", i); - ImGui::EndChild(); + if (!Contains(GetStorageIdFromIndex(idx))) + new_items.push_back(items[idx]); + if (item_curr_idx_to_select == idx) + item_next_idx_to_select = new_items.Size - 1; } + items.swap(new_items); - ImGui::SameLine(); + // Update selection + Clear(); + if (item_next_idx_to_select != -1 && ms_io->NavIdSelected) + SetItemSelected(GetStorageIdFromIndex(item_next_idx_to_select), true); + } +}; - // Child 2: rounded border +// Example: Implement dual list box storage and interface +struct ExampleDualListBox +{ + ImVector Items[2]; // ID is index into ExampleName[] + ImGuiSelectionBasicStorage Selections[2]; // Store ExampleItemId into selection + bool OptKeepSorted = true; + + void MoveAll(int src, int dst) + { + IM_ASSERT((src == 0 && dst == 1) || (src == 1 && dst == 0)); + for (ImGuiID item_id : Items[src]) + Items[dst].push_back(item_id); + Items[src].clear(); + SortItems(dst); + Selections[src].Swap(Selections[dst]); + Selections[src].Clear(); + } + void MoveSelected(int src, int dst) + { + for (int src_n = 0; src_n < Items[src].Size; src_n++) { - ImGuiWindowFlags window_flags = ImGuiWindowFlags_None; - if (disable_mouse_wheel) - window_flags |= ImGuiWindowFlags_NoScrollWithMouse; - if (!disable_menu) - window_flags |= ImGuiWindowFlags_MenuBar; - ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); - ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Border, window_flags); - if (!disable_menu && ImGui::BeginMenuBar()) + ImGuiID item_id = Items[src][src_n]; + if (!Selections[src].Contains(item_id)) + continue; + Items[src].erase(&Items[src][src_n]); // FIXME-OPT: Could be implemented more optimally (rebuild src items and swap) + Items[dst].push_back(item_id); + src_n--; + } + if (OptKeepSorted) + SortItems(dst); + Selections[src].Swap(Selections[dst]); + Selections[src].Clear(); + } + void ApplySelectionRequests(ImGuiMultiSelectIO* ms_io, int side) + { + // In this example we store item id in selection (instead of item index) + Selections[side].UserData = Items[side].Data; + Selections[side].AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImGuiID* items = (ImGuiID*)self->UserData; return items[idx]; }; + Selections[side].ApplyRequests(ms_io); + } + static int IMGUI_CDECL CompareItemsByValue(const void* lhs, const void* rhs) + { + const int* a = (const int*)lhs; + const int* b = (const int*)rhs; + return (*a - *b) > 0 ? +1 : -1; + } + void SortItems(int n) + { + qsort(Items[n].Data, (size_t)Items[n].Size, sizeof(Items[n][0]), CompareItemsByValue); + } + void Show() + { + //ImGui::Checkbox("Sorted", &OptKeepSorted); + if (ImGui::BeginTable("split", 3, ImGuiTableFlags_None)) + { + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch); // Left side + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed); // Buttons + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch); // Right side + ImGui::TableNextRow(); + + int request_move_selected = -1; + int request_move_all = -1; + float child_height_0 = 0.0f; + for (int side = 0; side < 2; side++) { - if (ImGui::BeginMenu("Menu")) + // FIXME-MULTISELECT: Dual List Box: Add context menus + // FIXME-NAV: Using ImGuiWindowFlags_NavFlattened exhibit many issues. + ImVector& items = Items[side]; + ImGuiSelectionBasicStorage& selection = Selections[side]; + + ImGui::TableSetColumnIndex((side == 0) ? 0 : 2); + ImGui::Text("%s (%d)", (side == 0) ? "Available" : "Basket", items.Size); + + // Submit scrolling range to avoid glitches on moving/deletion + const float items_height = ImGui::GetTextLineHeightWithSpacing(); + ImGui::SetNextWindowContentSize(ImVec2(0.0f, items.Size * items_height)); + + bool child_visible; + if (side == 0) { - ShowExampleMenuFile(); - ImGui::EndMenu(); + // Left child is resizable + ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetFrameHeightWithSpacing() * 4), ImVec2(FLT_MAX, FLT_MAX)); + child_visible = ImGui::BeginChild("0", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY); + child_height_0 = ImGui::GetWindowSize().y; } - ImGui::EndMenuBar(); - } - if (ImGui::BeginTable("split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) - { - for (int i = 0; i < 100; i++) + else { - char buf[32]; - sprintf(buf, "%03d", i); - ImGui::TableNextColumn(); - ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + // Right child use same height as left one + child_visible = ImGui::BeginChild("1", ImVec2(-FLT_MIN, child_height_0), ImGuiChildFlags_FrameStyle); } - ImGui::EndTable(); - } - ImGui::EndChild(); - ImGui::PopStyleVar(); - } + if (child_visible) + { + ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_None; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, items.Size); + ApplySelectionRequests(ms_io, side); + + for (int item_n = 0; item_n < items.Size; item_n++) + { + ImGuiID item_id = items[item_n]; + bool item_is_selected = selection.Contains(item_id); + ImGui::SetNextItemSelectionUserData(item_n); + ImGui::Selectable(ExampleNames[item_id], item_is_selected, ImGuiSelectableFlags_AllowDoubleClick); + if (ImGui::IsItemFocused()) + { + // FIXME-MULTISELECT: Dual List Box: Transfer focus + if (ImGui::IsKeyPressed(ImGuiKey_Enter) || ImGui::IsKeyPressed(ImGuiKey_KeypadEnter)) + request_move_selected = side; + if (ImGui::IsMouseDoubleClicked(0)) // FIXME-MULTISELECT: Double-click on multi-selection? + request_move_selected = side; + } + } + + ms_io = ImGui::EndMultiSelect(); + ApplySelectionRequests(ms_io, side); + } + ImGui::EndChild(); + } + + // Buttons columns + ImGui::TableSetColumnIndex(1); + ImGui::NewLine(); + //ImVec2 button_sz = { ImGui::CalcTextSize(">>").x + ImGui::GetStyle().FramePadding.x * 2.0f, ImGui::GetFrameHeight() + padding.y * 2.0f }; + ImVec2 button_sz = { ImGui::GetFrameHeight(), ImGui::GetFrameHeight() }; + + // (Using BeginDisabled()/EndDisabled() works but feels distracting given how it is currently visualized) + if (ImGui::Button(">>", button_sz)) + request_move_all = 0; + if (ImGui::Button(">", button_sz)) + request_move_selected = 0; + if (ImGui::Button("<", button_sz)) + request_move_selected = 1; + if (ImGui::Button("<<", button_sz)) + request_move_all = 1; + + // Process requests + if (request_move_all != -1) + MoveAll(request_move_all, request_move_all ^ 1); + if (request_move_selected != -1) + MoveSelected(request_move_selected, request_move_selected ^ 1); + + // FIXME-MULTISELECT: Support action from outside + /* + if (OptKeepSorted == false) + { + ImGui::NewLine(); + if (ImGui::ArrowButton("MoveUp", ImGuiDir_Up)) {} + if (ImGui::ArrowButton("MoveDown", ImGuiDir_Down)) {} + } + */ + + ImGui::EndTable(); + } + } +}; + +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowMultiSelect() +//----------------------------------------------------------------------------- +// Multi-selection demos +// Also read: https://github.com/ocornut/imgui/wiki/Multi-Select +//----------------------------------------------------------------------------- + +static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data) +{ + IMGUI_DEMO_MARKER("Widgets/Selection State & Multi-Select"); + if (ImGui::TreeNode("Selection State & Multi-Select")) + { + HelpMarker("Selections can be built using Selectable(), TreeNode() or other widgets. Selection state is owned by application code/data."); + + // Without any fancy API: manage single-selection yourself. + IMGUI_DEMO_MARKER("Widgets/Selection State/Single-Select"); + if (ImGui::TreeNode("Single-Select")) + { + static int selected = -1; + for (int n = 0; n < 5; n++) + { + char buf[32]; + sprintf(buf, "Object %d", n); + if (ImGui::Selectable(buf, selected == n)) + selected = n; + } + ImGui::TreePop(); + } + + // Demonstrate implementation a most-basic form of multi-selection manually + // This doesn't support the SHIFT modifier which requires BeginMultiSelect()! + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (manual/simplified, without BeginMultiSelect)"); + if (ImGui::TreeNode("Multi-Select (manual/simplified, without BeginMultiSelect)")) + { + HelpMarker("Hold CTRL and click to select multiple items."); + static bool selection[5] = { false, false, false, false, false }; + for (int n = 0; n < 5; n++) + { + char buf[32]; + sprintf(buf, "Object %d", n); + if (ImGui::Selectable(buf, selection[n])) + { + if (!ImGui::GetIO().KeyCtrl) // Clear selection when CTRL is not held + memset(selection, 0, sizeof(selection)); + selection[n] ^= 1; // Toggle current item + } + } + ImGui::TreePop(); + } + + // Demonstrate handling proper multi-selection using the BeginMultiSelect/EndMultiSelect API. + // SHIFT+Click w/ CTRL and other standard features are supported. + // We use the ImGuiSelectionBasicStorage helper which you may freely reimplement. + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select"); + if (ImGui::TreeNode("Multi-Select")) + { + ImGui::Text("Supported features:"); + ImGui::BulletText("Keyboard navigation (arrows, page up/down, home/end, space)."); + ImGui::BulletText("Ctrl modifier to preserve and toggle selection."); + ImGui::BulletText("Shift modifier for range selection."); + ImGui::BulletText("CTRL+A to select all."); + ImGui::BulletText("Escape to clear selection."); + ImGui::BulletText("Click and drag to box-select."); + ImGui::Text("Tip: Use 'Demo->Tools->Debug Log->Selection' to see selection requests as they happen."); + + // Use default selection.Adapter: Pass index to SetNextItemSelectionUserData(), store index in Selection + const int ITEMS_COUNT = 50; + static ImGuiSelectionBasicStorage selection; + ImGui::Text("Selection: %d/%d", selection.Size, ITEMS_COUNT); + + // The BeginChild() has no purpose for selection logic, other that offering a scrolling region. + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) + { + ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect1d; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, ITEMS_COUNT); + selection.ApplyRequests(ms_io); + + for (int n = 0; n < ITEMS_COUNT; n++) + { + char label[64]; + sprintf(label, "Object %05d: %s", n, ExampleNames[n % IM_ARRAYSIZE(ExampleNames)]); + bool item_is_selected = selection.Contains((ImGuiID)n); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Selectable(label, item_is_selected); + } + + ms_io = ImGui::EndMultiSelect(); + selection.ApplyRequests(ms_io); + } + ImGui::EndChild(); + ImGui::TreePop(); + } + + // Demonstrate using the clipper with BeginMultiSelect()/EndMultiSelect() + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (with clipper)"); + if (ImGui::TreeNode("Multi-Select (with clipper)")) + { + // Use default selection.Adapter: Pass index to SetNextItemSelectionUserData(), store index in Selection + static ImGuiSelectionBasicStorage selection; + + ImGui::Text("Added features:"); + ImGui::BulletText("Using ImGuiListClipper."); + + const int ITEMS_COUNT = 10000; + ImGui::Text("Selection: %d/%d", selection.Size, ITEMS_COUNT); + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) + { + ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect1d; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, ITEMS_COUNT); + selection.ApplyRequests(ms_io); + + ImGuiListClipper clipper; + clipper.Begin(ITEMS_COUNT); + if (ms_io->RangeSrcItem != -1) + clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped. + while (clipper.Step()) + { + for (int n = clipper.DisplayStart; n < clipper.DisplayEnd; n++) + { + char label[64]; + sprintf(label, "Object %05d: %s", n, ExampleNames[n % IM_ARRAYSIZE(ExampleNames)]); + bool item_is_selected = selection.Contains((ImGuiID)n); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Selectable(label, item_is_selected); + } + } + + ms_io = ImGui::EndMultiSelect(); + selection.ApplyRequests(ms_io); + } + ImGui::EndChild(); + ImGui::TreePop(); + } + + // Demonstrate dynamic item list + deletion support using the BeginMultiSelect/EndMultiSelect API. + // In order to support Deletion without any glitches you need to: + // - (1) If items are submitted in their own scrolling area, submit contents size SetNextWindowContentSize() ahead of time to prevent one-frame readjustment of scrolling. + // - (2) Items needs to have persistent ID Stack identifier = ID needs to not depends on their index. PushID(index) = KO. PushID(item_id) = OK. This is in order to focus items reliably after a selection. + // - (3) BeginXXXX process + // - (4) Focus process + // - (5) EndXXXX process + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (with deletion)"); + if (ImGui::TreeNode("Multi-Select (with deletion)")) + { + // Storing items data separately from selection data. + // (you may decide to store selection data inside your item (aka intrusive storage) if you don't need multiple views over same items) + // Use a custom selection.Adapter: store item identifier in Selection (instead of index) + static ImVector items; + static ExampleSelectionWithDeletion selection; + selection.UserData = (void*)&items; + selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self, int idx) { ImVector* p_items = (ImVector*)self->UserData; return (*p_items)[idx]; }; // Index -> ID + + ImGui::Text("Added features:"); + ImGui::BulletText("Dynamic list with Delete key support."); + ImGui::Text("Selection size: %d/%d", selection.Size, items.Size); + + // Initialize default list with 50 items + button to add/remove items. + static ImGuiID items_next_id = 0; + if (items_next_id == 0) + for (ImGuiID n = 0; n < 50; n++) + items.push_back(items_next_id++); + if (ImGui::SmallButton("Add 20 items")) { for (int n = 0; n < 20; n++) { items.push_back(items_next_id++); } } + ImGui::SameLine(); + if (ImGui::SmallButton("Remove 20 items")) { for (int n = IM_MIN(20, items.Size); n > 0; n--) { selection.SetItemSelected(items.back(), false); items.pop_back(); } } + + // (1) Extra to support deletion: Submit scrolling range to avoid glitches on deletion + const float items_height = ImGui::GetTextLineHeightWithSpacing(); + ImGui::SetNextWindowContentSize(ImVec2(0.0f, items.Size * items_height)); + + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) + { + ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect1d; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, items.Size); + selection.ApplyRequests(ms_io); + + const bool want_delete = ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0); + const int item_curr_idx_to_focus = want_delete ? selection.ApplyDeletionPreLoop(ms_io, items.Size) : -1; + + for (int n = 0; n < items.Size; n++) + { + const ImGuiID item_id = items[n]; + char label[64]; + sprintf(label, "Object %05u: %s", item_id, ExampleNames[item_id % IM_ARRAYSIZE(ExampleNames)]); + + bool item_is_selected = selection.Contains(item_id); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Selectable(label, item_is_selected); + if (item_curr_idx_to_focus == n) + ImGui::SetKeyboardFocusHere(-1); + } + + // Apply multi-select requests + ms_io = ImGui::EndMultiSelect(); + selection.ApplyRequests(ms_io); + if (want_delete) + selection.ApplyDeletionPostLoop(ms_io, items, item_curr_idx_to_focus); + } + ImGui::EndChild(); + ImGui::TreePop(); + } + + // Implement a Dual List Box (#6648) + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (dual list box)"); + if (ImGui::TreeNode("Multi-Select (dual list box)")) + { + // Init default state + static ExampleDualListBox dlb; + if (dlb.Items[0].Size == 0 && dlb.Items[1].Size == 0) + for (int item_id = 0; item_id < IM_ARRAYSIZE(ExampleNames); item_id++) + dlb.Items[0].push_back((ImGuiID)item_id); + + // Show + dlb.Show(); + + ImGui::TreePop(); + } + + // Demonstrate using the clipper with BeginMultiSelect()/EndMultiSelect() + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (in a table)"); + if (ImGui::TreeNode("Multi-Select (in a table)")) + { + static ImGuiSelectionBasicStorage selection; + + const int ITEMS_COUNT = 10000; + ImGui::Text("Selection: %d/%d", selection.Size, ITEMS_COUNT); + if (ImGui::BeginTable("##Basket", 2, ImGuiTableFlags_ScrollY | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter)) + { + ImGui::TableSetupColumn("Object"); + ImGui::TableSetupColumn("Action"); + ImGui::TableSetupScrollFreeze(0, 1); + ImGui::TableHeadersRow(); + + ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect1d; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, ITEMS_COUNT); + selection.ApplyRequests(ms_io); + + ImGuiListClipper clipper; + clipper.Begin(ITEMS_COUNT); + if (ms_io->RangeSrcItem != -1) + clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped. + while (clipper.Step()) + { + for (int n = clipper.DisplayStart; n < clipper.DisplayEnd; n++) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + char label[64]; + sprintf(label, "Object %05d: %s", n, ExampleNames[n % IM_ARRAYSIZE(ExampleNames)]); + bool item_is_selected = selection.Contains((ImGuiID)n); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap); + ImGui::TableNextColumn(); + ImGui::SmallButton("hello"); + } + } + + ms_io = ImGui::EndMultiSelect(); + selection.ApplyRequests(ms_io); + ImGui::EndTable(); + } + ImGui::TreePop(); + } + + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (checkboxes)"); + if (ImGui::TreeNode("Multi-Select (checkboxes)")) + { + ImGui::Text("In a list of checkboxes (not selectable):"); + ImGui::BulletText("Using _NoAutoSelect + _NoAutoClear flags."); + ImGui::BulletText("Shift+Click to check multiple boxes."); + ImGui::BulletText("Shift+Keyboard to copy current value to other boxes."); + + // If you have an array of checkboxes, you may want to use NoAutoSelect + NoAutoClear and the ImGuiSelectionExternalStorage helper. + static bool items[20] = {}; + static ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_NoAutoSelect | ImGuiMultiSelectFlags_NoAutoClear | ImGuiMultiSelectFlags_ClearOnEscape; + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoSelect", &flags, ImGuiMultiSelectFlags_NoAutoSelect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoClear", &flags, ImGuiMultiSelectFlags_NoAutoClear); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect2d", &flags, ImGuiMultiSelectFlags_BoxSelect2d); // Cannot use ImGuiMultiSelectFlags_BoxSelect1d as checkboxes are varying width. + + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) + { + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, -1, IM_ARRAYSIZE(items)); + ImGuiSelectionExternalStorage storage_wrapper; + storage_wrapper.UserData = (void*)items; + storage_wrapper.AdapterSetItemSelected = [](ImGuiSelectionExternalStorage* self, int n, bool selected) { bool* array = (bool*)self->UserData; array[n] = selected; }; + storage_wrapper.ApplyRequests(ms_io); + for (int n = 0; n < 20; n++) + { + char label[32]; + sprintf(label, "Item %d", n); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Checkbox(label, &items[n]); + } + ms_io = ImGui::EndMultiSelect(); + storage_wrapper.ApplyRequests(ms_io); + } + ImGui::EndChild(); + + ImGui::TreePop(); + } + + // Demonstrate individual selection scopes in same window + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (multiple scopes)"); + if (ImGui::TreeNode("Multi-Select (multiple scopes)")) + { + // Use default select: Pass index to SetNextItemSelectionUserData(), store index in Selection + const int SCOPES_COUNT = 3; + const int ITEMS_COUNT = 8; // Per scope + static ImGuiSelectionBasicStorage selections_data[SCOPES_COUNT]; + + // Use ImGuiMultiSelectFlags_ScopeRect to not affect other selections in same window. + static ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ScopeRect | ImGuiMultiSelectFlags_ClearOnEscape;// | ImGuiMultiSelectFlags_ClearOnClickVoid; + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeWindow", &flags, ImGuiMultiSelectFlags_ScopeWindow) && (flags & ImGuiMultiSelectFlags_ScopeWindow)) + flags &= ~ImGuiMultiSelectFlags_ScopeRect; + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeRect", &flags, ImGuiMultiSelectFlags_ScopeRect) && (flags & ImGuiMultiSelectFlags_ScopeRect)) + flags &= ~ImGuiMultiSelectFlags_ScopeWindow; + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnClickVoid", &flags, ImGuiMultiSelectFlags_ClearOnClickVoid); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect1d", &flags, ImGuiMultiSelectFlags_BoxSelect1d); + + for (int selection_scope_n = 0; selection_scope_n < SCOPES_COUNT; selection_scope_n++) + { + ImGui::PushID(selection_scope_n); + ImGuiSelectionBasicStorage* selection = &selections_data[selection_scope_n]; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection->Size, ITEMS_COUNT); + selection->ApplyRequests(ms_io); + + ImGui::SeparatorText("Selection scope"); + ImGui::Text("Selection size: %d/%d", selection->Size, ITEMS_COUNT); + + for (int n = 0; n < ITEMS_COUNT; n++) + { + char label[64]; + sprintf(label, "Object %05d: %s", n, ExampleNames[n % IM_ARRAYSIZE(ExampleNames)]); + bool item_is_selected = selection->Contains((ImGuiID)n); + ImGui::SetNextItemSelectionUserData(n); + ImGui::Selectable(label, item_is_selected); + } + + // Apply multi-select requests + ms_io = ImGui::EndMultiSelect(); + selection->ApplyRequests(ms_io); + ImGui::PopID(); + } + ImGui::TreePop(); + } + + // See ShowExampleAppAssetsBrowser() + if (ImGui::TreeNode("Multi-Select (tiled assets browser)")) + { + ImGui::Checkbox("Assets Browser", &demo_data->ShowAppAssetsBrowser); + ImGui::Text("(also access from 'Examples->Assets Browser' in menu)"); + ImGui::TreePop(); + } + + // Demonstrate supporting multiple-selection in a tree. + // - We don't use linear indices for selection user data, but our ExampleTreeNode* pointer directly! + // This showcase how SetNextItemSelectionUserData() never assume indices! + // - The difficulty here is to "interpolate" from RangeSrcItem to RangeDstItem in the SetAll/SetRange request. + // We want this interpolation to match what the user sees: in visible order, skipping closed nodes. + // This is implemented by our TreeGetNextNodeInVisibleOrder() user-space helper. + // - Important: In a real codebase aiming to implement full-featured selectable tree with custom filtering, you + // are more likely to build an array mapping sequential indices to visible tree nodes, since your + // filtering/search + clipping process will benefit from it. Having this will make this interpolation much easier. + // - Consider this a prototype: we are working toward simplifying some of it. + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (trees)"); + if (ImGui::TreeNode("Multi-Select (trees)")) + { + HelpMarker( + "This is rather advanced and experimental. If you are getting started with multi-select," + "please don't start by looking at how to use it for a tree!\n\n" + "Future versions will try to simplify and formalize some of this."); + + struct ExampleTreeFuncs + { + static void DrawNode(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection) + { + ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick; + tree_node_flags |= ImGuiTreeNodeFlags_NavLeftJumpsBackHere; // Enable pressing left to jump to parent + if (node->Childs.Size == 0) + tree_node_flags |= ImGuiTreeNodeFlags_Bullet | ImGuiTreeNodeFlags_Leaf; + if (selection->Contains((ImGuiID)node->UID)) + tree_node_flags |= ImGuiTreeNodeFlags_Selected; + + // Using SetNextItemStorageID() to specify storage id, so we can easily peek into + // the storage holding open/close stage, using our TreeNodeGetOpen/TreeNodeSetOpen() functions. + ImGui::SetNextItemSelectionUserData((ImGuiSelectionUserData)(intptr_t)node); + ImGui::SetNextItemStorageID((ImGuiID)node->UID); + if (ImGui::TreeNodeEx(node->Name, tree_node_flags)) + { + for (ExampleTreeNode* child : node->Childs) + DrawNode(child, selection); + ImGui::TreePop(); + } + else if (ImGui::IsItemToggledOpen()) + { + TreeCloseAndUnselectChildNodes(node, selection); + } + } + + static bool TreeNodeGetOpen(ExampleTreeNode* node) + { + return ImGui::GetStateStorage()->GetBool((ImGuiID)node->UID); + } + + static void TreeNodeSetOpen(ExampleTreeNode* node, bool open) + { + ImGui::GetStateStorage()->SetBool((ImGuiID)node->UID, open); + } + + // When closing a node: 1) close and unselect all child nodes, 2) select parent if any child was selected. + // FIXME: This is currently handled by user logic but I'm hoping to eventually provide tree node + // features to do this automatically, e.g. a ImGuiTreeNodeFlags_AutoCloseChildNodes etc. + static int TreeCloseAndUnselectChildNodes(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection, int depth = 0) + { + // Recursive close (the test for depth == 0 is because we call this on a node that was just closed!) + int unselected_count = selection->Contains((ImGuiID)node->UID) ? 1 : 0; + if (depth == 0 || TreeNodeGetOpen(node)) + { + for (ExampleTreeNode* child : node->Childs) + unselected_count += TreeCloseAndUnselectChildNodes(child, selection, depth + 1); + TreeNodeSetOpen(node, false); + } + + // Select root node if any of its child was selected, otherwise unselect + selection->SetItemSelected((ImGuiID)node->UID, (depth == 0 && unselected_count > 0)); + return unselected_count; + } + + // Apply multi-selection requests + static void ApplySelectionRequests(ImGuiMultiSelectIO* ms_io, ExampleTreeNode* tree, ImGuiSelectionBasicStorage* selection) + { + for (ImGuiSelectionRequest& req : ms_io->Requests) + { + if (req.Type == ImGuiSelectionRequestType_SetAll) + { + if (req.Selected) + TreeSetAllInOpenNodes(tree, selection, req.Selected); + else + selection->Clear(); + } + else if (req.Type == ImGuiSelectionRequestType_SetRange) + { + ExampleTreeNode* first_node = (ExampleTreeNode*)(intptr_t)req.RangeFirstItem; + ExampleTreeNode* last_node = (ExampleTreeNode*)(intptr_t)req.RangeLastItem; + for (ExampleTreeNode* node = first_node; node != NULL; node = TreeGetNextNodeInVisibleOrder(node, last_node)) + selection->SetItemSelected((ImGuiID)node->UID, req.Selected); + } + } + } + + static void TreeSetAllInOpenNodes(ExampleTreeNode* node, ImGuiSelectionBasicStorage* selection, bool selected) + { + if (node->Parent != NULL) // Root node isn't visible nor selectable in our scheme + selection->SetItemSelected((ImGuiID)node->UID, selected); + if (node->Parent == NULL || TreeNodeGetOpen(node)) + for (ExampleTreeNode* child : node->Childs) + TreeSetAllInOpenNodes(child, selection, selected); + } + + // Interpolate in *user-visible order* AND only *over opened nodes*. + // If you have a sequential mapping tables (e.g. generated after a filter/search pass) this would be simpler. + // Here the tricks are that: + // - we store/maintain ExampleTreeNode::IndexInParent which allows implementing a linear iterator easily, without searches, without recursion. + // this could be replaced by a search in parent, aka 'int index_in_parent = curr_node->Parent->Childs.find_index(curr_node)' + // which would only be called when crossing from child to a parent, aka not too much. + // - we call SetNextItemStorageID() before our TreeNode() calls with an ID which doesn't relate to UI stack, + // making it easier to call TreeNodeGetOpen()/TreeNodeSetOpen() from any location. + static ExampleTreeNode* TreeGetNextNodeInVisibleOrder(ExampleTreeNode* curr_node, ExampleTreeNode* last_node) + { + // Reached last node + if (curr_node == last_node) + return NULL; + + // Recurse into childs. Query storage to tell if the node is open. + if (curr_node->Childs.Size > 0 && TreeNodeGetOpen(curr_node)) + return curr_node->Childs[0]; + + // Next sibling, then into our own parent + while (curr_node->Parent != NULL) + { + if (curr_node->IndexInParent + 1 < curr_node->Parent->Childs.Size) + return curr_node->Parent->Childs[curr_node->IndexInParent + 1]; + curr_node = curr_node->Parent; + } + return NULL; + } + + }; // ExampleTreeFuncs + + static ImGuiSelectionBasicStorage selection; + if (demo_data->DemoTree == NULL) + demo_data->DemoTree = ExampleTree_CreateDemoTree(); // Create tree once + ImGui::Text("Selection size: %d", selection.Size); + + if (ImGui::BeginChild("##Tree", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) + { + ExampleTreeNode* tree = demo_data->DemoTree; + ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect2d; + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, selection.Size, -1); + ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection); + for (ExampleTreeNode* node : tree->Childs) + ExampleTreeFuncs::DrawNode(node, &selection); + ms_io = ImGui::EndMultiSelect(); + ExampleTreeFuncs::ApplySelectionRequests(ms_io, tree, &selection); + } + ImGui::EndChild(); + + ImGui::TreePop(); + } + + // Advanced demonstration of BeginMultiSelect() + // - Showcase clipping. + // - Showcase deletion. + // - Showcase basic drag and drop. + // - Showcase TreeNode variant (note that tree node don't expand in the demo: supporting expanding tree nodes + clipping a separate thing). + // - Showcase using inside a table. + IMGUI_DEMO_MARKER("Widgets/Selection State/Multi-Select (advanced)"); + //ImGui::SetNextItemOpen(true, ImGuiCond_Once); + if (ImGui::TreeNode("Multi-Select (advanced)")) + { + // Options + enum WidgetType { WidgetType_Selectable, WidgetType_TreeNode }; + static bool use_clipper = true; + static bool use_deletion = true; + static bool use_drag_drop = true; + static bool show_in_table = false; + static bool show_color_button = true; + static ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_BoxSelect1d; + static WidgetType widget_type = WidgetType_Selectable; + + if (ImGui::TreeNode("Options")) + { + if (ImGui::RadioButton("Selectables", widget_type == WidgetType_Selectable)) { widget_type = WidgetType_Selectable; } + ImGui::SameLine(); + if (ImGui::RadioButton("Tree nodes", widget_type == WidgetType_TreeNode)) { widget_type = WidgetType_TreeNode; } + ImGui::SameLine(); + HelpMarker("TreeNode() is technically supported but... using this correctly is more complicated (you need some sort of linear/random access to your tree, which is suited to advanced trees setups already implementing filters and clipper. We will work toward simplifying and demoing this.\n\nFor now the tree demo is actually a little bit meaningless because it is an empty tree with only root nodes."); + ImGui::Checkbox("Enable clipper", &use_clipper); + ImGui::Checkbox("Enable deletion", &use_deletion); + ImGui::Checkbox("Enable drag & drop", &use_drag_drop); + ImGui::Checkbox("Show in a table", &show_in_table); + ImGui::Checkbox("Show color button", &show_color_button); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_SingleSelect", &flags, ImGuiMultiSelectFlags_SingleSelect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoSelectAll", &flags, ImGuiMultiSelectFlags_NoSelectAll); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoRangeSelect", &flags, ImGuiMultiSelectFlags_NoRangeSelect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoSelect", &flags, ImGuiMultiSelectFlags_NoAutoSelect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoClear", &flags, ImGuiMultiSelectFlags_NoAutoClear); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoClearOnReselect", &flags, ImGuiMultiSelectFlags_NoAutoClearOnReselect); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect1d", &flags, ImGuiMultiSelectFlags_BoxSelect1d); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect2d", &flags, ImGuiMultiSelectFlags_BoxSelect2d); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelectNoScroll", &flags, ImGuiMultiSelectFlags_BoxSelectNoScroll); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnEscape", &flags, ImGuiMultiSelectFlags_ClearOnEscape); + ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ClearOnClickVoid", &flags, ImGuiMultiSelectFlags_ClearOnClickVoid); + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeWindow", &flags, ImGuiMultiSelectFlags_ScopeWindow) && (flags & ImGuiMultiSelectFlags_ScopeWindow)) + flags &= ~ImGuiMultiSelectFlags_ScopeRect; + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_ScopeRect", &flags, ImGuiMultiSelectFlags_ScopeRect) && (flags & ImGuiMultiSelectFlags_ScopeRect)) + flags &= ~ImGuiMultiSelectFlags_ScopeWindow; + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_SelectOnClick", &flags, ImGuiMultiSelectFlags_SelectOnClick) && (flags & ImGuiMultiSelectFlags_SelectOnClick)) + flags &= ~ImGuiMultiSelectFlags_SelectOnClickRelease; + if (ImGui::CheckboxFlags("ImGuiMultiSelectFlags_SelectOnClickRelease", &flags, ImGuiMultiSelectFlags_SelectOnClickRelease) && (flags & ImGuiMultiSelectFlags_SelectOnClickRelease)) + flags &= ~ImGuiMultiSelectFlags_SelectOnClick; + ImGui::SameLine(); HelpMarker("Allow dragging an unselected item without altering selection."); + ImGui::TreePop(); + } + + // Initialize default list with 1000 items. + // Use default selection.Adapter: Pass index to SetNextItemSelectionUserData(), store index in Selection + static ImVector items; + static int items_next_id = 0; + if (items_next_id == 0) { for (int n = 0; n < 1000; n++) { items.push_back(items_next_id++); } } + static ExampleSelectionWithDeletion selection; + static bool request_deletion_from_menu = false; // Queue deletion triggered from context menu + + ImGui::Text("Selection size: %d/%d", selection.Size, items.Size); + + const float items_height = (widget_type == WidgetType_TreeNode) ? ImGui::GetTextLineHeight() : ImGui::GetTextLineHeightWithSpacing(); + ImGui::SetNextWindowContentSize(ImVec2(0.0f, items.Size * items_height)); + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY)) + { + ImVec2 color_button_sz(ImGui::GetFontSize(), ImGui::GetFontSize()); + if (widget_type == WidgetType_TreeNode) + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f)); + + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, items.Size); + selection.ApplyRequests(ms_io); + + const bool want_delete = (ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (selection.Size > 0)) || request_deletion_from_menu; + const int item_curr_idx_to_focus = want_delete ? selection.ApplyDeletionPreLoop(ms_io, items.Size) : -1; + request_deletion_from_menu = false; + + if (show_in_table) + { + if (widget_type == WidgetType_TreeNode) + ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(0.0f, 0.0f)); + ImGui::BeginTable("##Split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_NoPadOuterX); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.70f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.30f); + //ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f)); + } + + ImGuiListClipper clipper; + if (use_clipper) + { + clipper.Begin(items.Size); + if (item_curr_idx_to_focus != -1) + clipper.IncludeItemByIndex(item_curr_idx_to_focus); // Ensure focused item is not clipped. + if (ms_io->RangeSrcItem != -1) + clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem); // Ensure RangeSrc item is not clipped. + } + + while (!use_clipper || clipper.Step()) + { + const int item_begin = use_clipper ? clipper.DisplayStart : 0; + const int item_end = use_clipper ? clipper.DisplayEnd : items.Size; + for (int n = item_begin; n < item_end; n++) + { + if (show_in_table) + ImGui::TableNextColumn(); + + const int item_id = items[n]; + const char* item_category = ExampleNames[item_id % IM_ARRAYSIZE(ExampleNames)]; + char label[64]; + sprintf(label, "Object %05d: %s", item_id, item_category); + + // IMPORTANT: for deletion refocus to work we need object ID to be stable, + // aka not depend on their index in the list. Here we use our persistent item_id + // instead of index to build a unique ID that will persist. + // (If we used PushID(index) instead, focus wouldn't be restored correctly after deletion). + ImGui::PushID(item_id); + + // Emit a color button, to test that Shift+LeftArrow landing on an item that is not part + // of the selection scope doesn't erroneously alter our selection. + if (show_color_button) + { + ImU32 dummy_col = (ImU32)((unsigned int)n * 0xC250B74B) | IM_COL32_A_MASK; + ImGui::ColorButton("##", ImColor(dummy_col), ImGuiColorEditFlags_NoTooltip, color_button_sz); + ImGui::SameLine(); + } + + // Submit item + bool item_is_selected = selection.Contains((ImGuiID)n); + bool item_is_open = false; + ImGui::SetNextItemSelectionUserData(n); + if (widget_type == WidgetType_Selectable) + { + ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_None); + } + else if (widget_type == WidgetType_TreeNode) + { + ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick; + if (item_is_selected) + tree_node_flags |= ImGuiTreeNodeFlags_Selected; + item_is_open = ImGui::TreeNodeEx(label, tree_node_flags); + } + + // Focus (for after deletion) + if (item_curr_idx_to_focus == n) + ImGui::SetKeyboardFocusHere(-1); + + // Drag and Drop + if (use_drag_drop && ImGui::BeginDragDropSource()) + { + // Create payload with full selection OR single unselected item. + // (the later is only possible when using ImGuiMultiSelectFlags_SelectOnClickRelease) + if (ImGui::GetDragDropPayload() == NULL) + { + ImVector payload_items; + void* it = NULL; + ImGuiID id = 0; + if (!item_is_selected) + payload_items.push_back(item_id); + else + while (selection.GetNextSelectedItem(&it, &id)) + payload_items.push_back((int)id); + ImGui::SetDragDropPayload("MULTISELECT_DEMO_ITEMS", payload_items.Data, (size_t)payload_items.size_in_bytes()); + } + + // Display payload content in tooltip + const ImGuiPayload* payload = ImGui::GetDragDropPayload(); + const int* payload_items = (int*)payload->Data; + const int payload_count = (int)payload->DataSize / (int)sizeof(int); + if (payload_count == 1) + ImGui::Text("Object %05d: %s", payload_items[0], ExampleNames[payload_items[0] % IM_ARRAYSIZE(ExampleNames)]); + else + ImGui::Text("Dragging %d objects", payload_count); + + ImGui::EndDragDropSource(); + } + + if (widget_type == WidgetType_TreeNode && item_is_open) + ImGui::TreePop(); + + // Right-click: context menu + if (ImGui::BeginPopupContextItem()) + { + ImGui::BeginDisabled(!use_deletion || selection.Size == 0); + sprintf(label, "Delete %d item(s)###DeleteSelected", selection.Size); + if (ImGui::Selectable(label)) + request_deletion_from_menu = true; + ImGui::EndDisabled(); + ImGui::Selectable("Close"); + ImGui::EndPopup(); + } + + // Demo content within a table + if (show_in_table) + { + ImGui::TableNextColumn(); + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); + ImGui::InputText("###NoLabel", (char*)(void*)item_category, strlen(item_category), ImGuiInputTextFlags_ReadOnly); + ImGui::PopStyleVar(); + } + + ImGui::PopID(); + } + if (!use_clipper) + break; + } + + if (show_in_table) + { + ImGui::EndTable(); + if (widget_type == WidgetType_TreeNode) + ImGui::PopStyleVar(); + } + + // Apply multi-select requests + ms_io = ImGui::EndMultiSelect(); + selection.ApplyRequests(ms_io); + if (want_delete) + selection.ApplyDeletionPostLoop(ms_io, items, item_curr_idx_to_focus); + + if (widget_type == WidgetType_TreeNode) + ImGui::PopStyleVar(); + } + ImGui::EndChild(); + ImGui::TreePop(); + } + ImGui::TreePop(); + } +} + +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowLayout() +//----------------------------------------------------------------------------- + +static void ShowDemoWindowLayout() +{ + IMGUI_DEMO_MARKER("Layout"); + if (!ImGui::CollapsingHeader("Layout & Scrolling")) + return; + + IMGUI_DEMO_MARKER("Layout/Child windows"); + if (ImGui::TreeNode("Child windows")) + { + ImGui::SeparatorText("Child windows"); + + HelpMarker("Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window."); + static bool disable_mouse_wheel = false; + static bool disable_menu = false; + ImGui::Checkbox("Disable Mouse Wheel", &disable_mouse_wheel); + ImGui::Checkbox("Disable Menu", &disable_menu); + + // Child 1: no border, enable horizontal scrollbar + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_HorizontalScrollbar; + if (disable_mouse_wheel) + window_flags |= ImGuiWindowFlags_NoScrollWithMouse; + ImGui::BeginChild("ChildL", ImVec2(ImGui::GetContentRegionAvail().x * 0.5f, 260), ImGuiChildFlags_None, window_flags); + for (int i = 0; i < 100; i++) + ImGui::Text("%04d: scrollable region", i); + ImGui::EndChild(); + } + + ImGui::SameLine(); + + // Child 2: rounded border + { + ImGuiWindowFlags window_flags = ImGuiWindowFlags_None; + if (disable_mouse_wheel) + window_flags |= ImGuiWindowFlags_NoScrollWithMouse; + if (!disable_menu) + window_flags |= ImGuiWindowFlags_MenuBar; + ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); + ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Border, window_flags); + if (!disable_menu && ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Menu")) + { + ShowExampleMenuFile(); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + if (ImGui::BeginTable("split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings)) + { + for (int i = 0; i < 100; i++) + { + char buf[32]; + sprintf(buf, "%03d", i); + ImGui::TableNextColumn(); + ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f)); + } + ImGui::EndTable(); + } + ImGui::EndChild(); + ImGui::PopStyleVar(); + } // Child 3: manual-resize ImGui::SeparatorText("Manual-resize"); @@ -3022,7 +4136,7 @@ static void ShowDemoWindowLayout() ImGui::Text("Manual wrapping:"); ImGuiStyle& style = ImGui::GetStyle(); int buttons_count = 20; - float window_visible_x2 = ImGui::GetWindowPos().x + ImGui::GetWindowContentRegionMax().x; + float window_visible_x2 = ImGui::GetCursorScreenPos().x + ImGui::GetContentRegionAvail().x; for (int n = 0; n < buttons_count; n++) { ImGui::PushID(n); @@ -3490,8 +4604,8 @@ static void ShowDemoWindowLayout() ImGui::TreePop(); } - IMGUI_DEMO_MARKER("Layout/Clipping"); - if (ImGui::TreeNode("Clipping")) + IMGUI_DEMO_MARKER("Layout/Text Clipping"); + if (ImGui::TreeNode("Text Clipping")) { static ImVec2 size(100.0f, 100.0f); static ImVec2 offset(30.0f, 30.0f); @@ -3587,6 +4701,10 @@ static void ShowDemoWindowLayout() } } +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowPopups() +//----------------------------------------------------------------------------- + static void ShowDemoWindowPopups() { IMGUI_DEMO_MARKER("Popups"); @@ -3842,7 +4960,7 @@ static void ShowDemoWindowPopups() static int item = 1; static float color[4] = { 0.4f, 0.7f, 0.0f, 0.5f }; ImGui::Combo("Combo", &item, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0"); - ImGui::ColorEdit4("color", color); + ImGui::ColorEdit4("Color", color); if (ImGui::Button("Add another modal..")) ImGui::OpenPopup("Stacked 2"); @@ -3854,6 +4972,7 @@ static void ShowDemoWindowPopups() if (ImGui::BeginPopupModal("Stacked 2", &unused_open)) { ImGui::Text("Hello from Stacked The Second!"); + ImGui::ColorEdit4("Color", color); // Allow opening another nested popup if (ImGui::Button("Close")) ImGui::CloseCurrentPopup(); ImGui::EndPopup(); @@ -4046,6 +5165,10 @@ static void ShowTableColumnsStatusFlags(ImGuiTableColumnFlags flags) ImGui::CheckboxFlags("_IsHovered", &flags, ImGuiTableColumnFlags_IsHovered); } +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowTables() +//----------------------------------------------------------------------------- + static void ShowDemoWindowTables() { //ImGui::SetNextItemOpen(true, ImGuiCond_Once); @@ -4164,7 +5287,7 @@ static void ShowDemoWindowTables() PushStyleCompact(); ImGui::CheckboxFlags("ImGuiTableFlags_RowBg", &flags, ImGuiTableFlags_RowBg); ImGui::CheckboxFlags("ImGuiTableFlags_Borders", &flags, ImGuiTableFlags_Borders); - ImGui::SameLine(); HelpMarker("ImGuiTableFlags_Borders\n = ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterV\n | ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterH"); + ImGui::SameLine(); HelpMarker("ImGuiTableFlags_Borders\n = ImGuiTableFlags_BordersInnerV\n | ImGuiTableFlags_BordersOuterV\n | ImGuiTableFlags_BordersInnerH\n | ImGuiTableFlags_BordersOuterH"); ImGui::Indent(); ImGui::CheckboxFlags("ImGuiTableFlags_BordersH", &flags, ImGuiTableFlags_BordersH); @@ -5139,7 +6262,8 @@ static void ShowDemoWindowTables() static ImGuiTableFlags flags = ImGuiTableFlags_BordersV | ImGuiTableFlags_BordersOuterH | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_NoBordersInBody; static ImGuiTreeNodeFlags tree_node_flags = ImGuiTreeNodeFlags_SpanAllColumns; - ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanFullWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanTextWidth", &tree_node_flags, ImGuiTreeNodeFlags_SpanTextWidth); ImGui::CheckboxFlags("ImGuiTreeNodeFlags_SpanAllColumns", &tree_node_flags, ImGuiTreeNodeFlags_SpanAllColumns); HelpMarker("See \"Columns flags\" section to configure how indentation is applied to individual columns."); @@ -5328,6 +6452,17 @@ static void ShowDemoWindowTables() ImGui::SliderInt("Frozen rows", &frozen_rows, 0, 2); ImGui::CheckboxFlags("Disable header contributing to column width", &column_flags, ImGuiTableColumnFlags_NoHeaderWidth); + if (ImGui::TreeNode("Style settings")) + { + ImGui::SameLine(); + HelpMarker("Giving access to some ImGuiStyle value in this demo for convenience."); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::SliderAngle("style.TableAngledHeadersAngle", &ImGui::GetStyle().TableAngledHeadersAngle, -50.0f, +50.0f); + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); + ImGui::SliderFloat2("style.TableAngledHeadersTextAlign", (float*)&ImGui::GetStyle().TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f"); + ImGui::TreePop(); + } + if (ImGui::BeginTable("table_angled_headers", columns_count, table_flags, ImVec2(0.0f, TEXT_BASE_HEIGHT * 12))) { ImGui::TableSetupColumn(column_names[0], ImGuiTableColumnFlags_NoHide | ImGuiTableColumnFlags_NoReorder); @@ -5479,6 +6614,7 @@ static void ShowDemoWindowTables() HelpMarker("Multiple tables with the same identifier will share their settings, width, visibility, order etc."); static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_NoSavedSettings; + ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable); ImGui::CheckboxFlags("ImGuiTableFlags_ScrollY", &flags, ImGuiTableFlags_ScrollY); ImGui::CheckboxFlags("ImGuiTableFlags_SizingFixedFit", &flags, ImGuiTableFlags_SizingFixedFit); ImGui::CheckboxFlags("ImGuiTableFlags_HighlightHoveredColumn", &flags, ImGuiTableFlags_HighlightHoveredColumn); @@ -5805,7 +6941,6 @@ static void ShowDemoWindowTables() // Show data // FIXME-TABLE FIXME-NAV: How we can get decent up/down even though we have the buttons here? - ImGui::PushButtonRepeat(true); #if 1 // Demonstrate using clipper for large vertical lists ImGuiListClipper clipper; @@ -5890,7 +7025,6 @@ static void ShowDemoWindowTables() ImGui::PopID(); } } - ImGui::PopButtonRepeat(); // Store some info to display debug details below table_scroll_cur = ImVec2(ImGui::GetScrollX(), ImGui::GetScrollY()); @@ -6128,6 +7262,10 @@ static void ShowDemoWindowColumns() ImGui::TreePop(); } +//----------------------------------------------------------------------------- +// [SECTION] ShowDemoWindowInputs() +//----------------------------------------------------------------------------- + static void ShowDemoWindowInputs() { IMGUI_DEMO_MARKER("Inputs & Focus"); @@ -6138,12 +7276,14 @@ static void ShowDemoWindowInputs() // Display inputs submitted to ImGuiIO IMGUI_DEMO_MARKER("Inputs & Focus/Inputs"); ImGui::SetNextItemOpen(true, ImGuiCond_Once); - if (ImGui::TreeNode("Inputs")) + bool inputs_opened = ImGui::TreeNode("Inputs"); + ImGui::SameLine(); + HelpMarker( + "This is a simplified view. See more detailed input state:\n" + "- in 'Tools->Metrics/Debugger->Inputs'.\n" + "- in 'Tools->Debug Log->IO'."); + if (inputs_opened) { - HelpMarker( - "This is a simplified view. See more detailed input state:\n" - "- in 'Tools->Metrics/Debugger->Inputs'.\n" - "- in 'Tools->Debug Log->IO'."); if (ImGui::IsMousePosValid()) ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y); else @@ -6174,15 +7314,17 @@ static void ShowDemoWindowInputs() // Display ImGuiIO output flags IMGUI_DEMO_MARKER("Inputs & Focus/Outputs"); ImGui::SetNextItemOpen(true, ImGuiCond_Once); - if (ImGui::TreeNode("Outputs")) + bool outputs_opened = ImGui::TreeNode("Outputs"); + ImGui::SameLine(); + HelpMarker( + "The value of io.WantCaptureMouse and io.WantCaptureKeyboard are normally set by Dear ImGui " + "to instruct your application of how to route inputs. Typically, when a value is true, it means " + "Dear ImGui wants the corresponding inputs and we expect the underlying application to ignore them.\n\n" + "The most typical case is: when hovering a window, Dear ImGui set io.WantCaptureMouse to true, " + "and underlying application should ignore mouse inputs (in practice there are many and more subtle " + "rules leading to how those flags are set)."); + if (outputs_opened) { - HelpMarker( - "The value of io.WantCaptureMouse and io.WantCaptureKeyboard are normally set by Dear ImGui " - "to instruct your application of how to route inputs. Typically, when a value is true, it means " - "Dear ImGui wants the corresponding inputs and we expect the underlying application to ignore them.\n\n" - "The most typical case is: when hovering a window, Dear ImGui set io.WantCaptureMouse to true, " - "and underlying application should ignore mouse inputs (in practice there are many and more subtle " - "rules leading to how those flags are set)."); ImGui::Text("io.WantCaptureMouse: %d", io.WantCaptureMouse); ImGui::Text("io.WantCaptureMouseUnlessPopupClose: %d", io.WantCaptureMouseUnlessPopupClose); ImGui::Text("io.WantCaptureKeyboard: %d", io.WantCaptureKeyboard); @@ -6216,6 +7358,102 @@ static void ShowDemoWindowInputs() ImGui::TreePop(); } + // Demonstrate using Shortcut() and Routing Policies. + // The general flow is: + // - Code interested in a chord (e.g. "Ctrl+A") declares their intent. + // - Multiple locations may be interested in same chord! Routing helps find a winner. + // - Every frame, we resolve all claims and assign one owner if the modifiers are matching. + // - The lower-level function is 'bool SetShortcutRouting()', returns true when caller got the route. + // - Most of the times, SetShortcutRouting() is not called directly. User mostly calls Shortcut() with routing flags. + // - If you call Shortcut() WITHOUT any routing option, it uses ImGuiInputFlags_RouteFocused. + // TL;DR: Most uses will simply be: + // - Shortcut(ImGuiMod_Ctrl | ImGuiKey_A); // Use ImGuiInputFlags_RouteFocused policy. + IMGUI_DEMO_MARKER("Inputs & Focus/Shortcuts"); + if (ImGui::TreeNode("Shortcuts")) + { + static ImGuiInputFlags route_options = ImGuiInputFlags_Repeat; + static ImGuiInputFlags route_type = ImGuiInputFlags_RouteFocused; + ImGui::CheckboxFlags("ImGuiInputFlags_Repeat", &route_options, ImGuiInputFlags_Repeat); + ImGui::RadioButton("ImGuiInputFlags_RouteActive", &route_type, ImGuiInputFlags_RouteActive); + ImGui::RadioButton("ImGuiInputFlags_RouteFocused (default)", &route_type, ImGuiInputFlags_RouteFocused); + ImGui::RadioButton("ImGuiInputFlags_RouteGlobal", &route_type, ImGuiInputFlags_RouteGlobal); + ImGui::Indent(); + ImGui::BeginDisabled(route_type != ImGuiInputFlags_RouteGlobal); + ImGui::CheckboxFlags("ImGuiInputFlags_RouteOverFocused", &route_options, ImGuiInputFlags_RouteOverFocused); + ImGui::CheckboxFlags("ImGuiInputFlags_RouteOverActive", &route_options, ImGuiInputFlags_RouteOverActive); + ImGui::CheckboxFlags("ImGuiInputFlags_RouteUnlessBgFocused", &route_options, ImGuiInputFlags_RouteUnlessBgFocused); + ImGui::EndDisabled(); + ImGui::Unindent(); + ImGui::RadioButton("ImGuiInputFlags_RouteAlways", &route_type, ImGuiInputFlags_RouteAlways); + ImGuiInputFlags flags = route_type | route_options; // Merged flags + if (route_type != ImGuiInputFlags_RouteGlobal) + flags &= ~(ImGuiInputFlags_RouteOverFocused | ImGuiInputFlags_RouteOverActive | ImGuiInputFlags_RouteUnlessBgFocused); + + ImGui::SeparatorText("Using SetNextItemShortcut()"); + ImGui::Text("Ctrl+S"); + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_S, flags | ImGuiInputFlags_Tooltip); + ImGui::Button("Save"); + ImGui::Text("Alt+F"); + ImGui::SetNextItemShortcut(ImGuiMod_Alt | ImGuiKey_F, flags | ImGuiInputFlags_Tooltip); + static float f = 0.5f; + ImGui::SliderFloat("Factor", &f, 0.0f, 1.0f); + + ImGui::SeparatorText("Using Shortcut()"); + const float line_height = ImGui::GetTextLineHeightWithSpacing(); + const ImGuiKeyChord key_chord = ImGuiMod_Ctrl | ImGuiKey_A; + + ImGui::Text("Ctrl+A"); + ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "..."); + + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(1.0f, 0.0f, 1.0f, 0.1f)); + + ImGui::BeginChild("WindowA", ImVec2(-FLT_MIN, line_height * 14), true); + ImGui::Text("Press CTRL+A and see who receives it!"); + ImGui::Separator(); + + // 1: Window polling for CTRL+A + ImGui::Text("(in WindowA)"); + ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "..."); + + // 2: InputText also polling for CTRL+A: it always uses _RouteFocused internally (gets priority when active) + // (Commmented because the owner-aware version of Shortcut() is still in imgui_internal.h) + //char str[16] = "Press CTRL+A"; + //ImGui::Spacing(); + //ImGui::InputText("InputTextB", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly); + //ImGuiID item_id = ImGui::GetItemID(); + //ImGui::SameLine(); HelpMarker("Internal widgets always use _RouteFocused"); + //ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags, item_id) ? "PRESSED" : "..."); + + // 3: Dummy child is not claiming the route: focusing them shouldn't steal route away from WindowA + ImGui::BeginChild("ChildD", ImVec2(-FLT_MIN, line_height * 4), true); + ImGui::Text("(in ChildD: not using same Shortcut)"); + ImGui::Text("IsWindowFocused: %d", ImGui::IsWindowFocused()); + ImGui::EndChild(); + + // 4: Child window polling for CTRL+A. It is deeper than WindowA and gets priority when focused. + ImGui::BeginChild("ChildE", ImVec2(-FLT_MIN, line_height * 4), true); + ImGui::Text("(in ChildE: using same Shortcut)"); + ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "..."); + ImGui::EndChild(); + + // 5: In a popup + if (ImGui::Button("Open Popup")) + ImGui::OpenPopup("PopupF"); + if (ImGui::BeginPopup("PopupF")) + { + ImGui::Text("(in PopupF)"); + ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags) ? "PRESSED" : "..."); + // (Commmented because the owner-aware version of Shortcut() is still in imgui_internal.h) + //ImGui::InputText("InputTextG", str, IM_ARRAYSIZE(str), ImGuiInputTextFlags_ReadOnly); + //ImGui::Text("IsWindowFocused: %d, Shortcut: %s", ImGui::IsWindowFocused(), ImGui::Shortcut(key_chord, flags, ImGui::GetItemID()) ? "PRESSED" : "..."); + ImGui::EndPopup(); + } + ImGui::EndChild(); + ImGui::PopStyleColor(); + + ImGui::TreePop(); + } + // Display mouse cursors IMGUI_DEMO_MARKER("Inputs & Focus/Mouse Cursors"); if (ImGui::TreeNode("Mouse Cursors")) @@ -6253,10 +7491,10 @@ static void ShowDemoWindowInputs() ImGui::InputText("1", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); ImGui::InputText("3", buf, IM_ARRAYSIZE(buf)); - ImGui::PushTabStop(false); + ImGui::PushItemFlag(ImGuiItemFlags_NoTabStop, true); ImGui::InputText("4 (tab skip)", buf, IM_ARRAYSIZE(buf)); ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); - ImGui::PopTabStop(); + ImGui::PopItemFlag(); ImGui::InputText("5", buf, IM_ARRAYSIZE(buf)); ImGui::TreePop(); } @@ -6278,12 +7516,12 @@ static void ShowDemoWindowInputs() ImGui::InputText("2", buf, IM_ARRAYSIZE(buf)); if (ImGui::IsItemActive()) has_focus = 2; - ImGui::PushTabStop(false); + ImGui::PushItemFlag(ImGuiItemFlags_NoTabStop, true); if (focus_3) ImGui::SetKeyboardFocusHere(); ImGui::InputText("3 (tab skip)", buf, IM_ARRAYSIZE(buf)); if (ImGui::IsItemActive()) has_focus = 3; ImGui::SameLine(); HelpMarker("Item won't be cycled through when using TAB or Shift+Tab."); - ImGui::PopTabStop(); + ImGui::PopItemFlag(); if (has_focus) ImGui::Text("Item with focus: %d", has_focus); @@ -6348,10 +7586,21 @@ void ImGui::ShowAboutWindow(bool* p_open) } IMGUI_DEMO_MARKER("Tools/About Dear ImGui"); ImGui::Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); + + ImGui::TextLinkOpenURL("Homepage", "https://github.com/ocornut/imgui"); + ImGui::SameLine(); + ImGui::TextLinkOpenURL("FAQ", "https://github.com/ocornut/imgui/blob/master/docs/FAQ.md"); + ImGui::SameLine(); + ImGui::TextLinkOpenURL("Wiki", "https://github.com/ocornut/imgui/wiki"); + ImGui::SameLine(); + ImGui::TextLinkOpenURL("Releases", "https://github.com/ocornut/imgui/releases"); + ImGui::SameLine(); + ImGui::TextLinkOpenURL("Funding", "https://github.com/ocornut/imgui/wiki/Funding"); + ImGui::Separator(); ImGui::Text("By Omar Cornut and all Dear ImGui contributors."); ImGui::Text("Dear ImGui is licensed under the MIT License, see LICENSE for more information."); - ImGui::Text("If your company uses this, please consider sponsoring the project!"); + ImGui::Text("If your company uses this, please consider funding the project."); static bool show_config_info = false; ImGui::Checkbox("Config/Build Information", &show_config_info); @@ -6603,6 +7852,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SliderFloat("FrameBorderSize", &style.FrameBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("TabBorderSize", &style.TabBorderSize, 0.0f, 1.0f, "%.0f"); ImGui::SliderFloat("TabBarBorderSize", &style.TabBarBorderSize, 0.0f, 2.0f, "%.0f"); + ImGui::SliderFloat("TabBarOverlineSize", &style.TabBarOverlineSize, 0.0f, 2.0f, "%.0f"); + ImGui::SameLine(); HelpMarker("Overline is only drawn over the selected tab when ImGuiTabBarFlags_DrawSelectedOverline is set."); ImGui::SeparatorText("Rounding"); ImGui::SliderFloat("WindowRounding", &style.WindowRounding, 0.0f, 12.0f, "%.0f"); @@ -6616,12 +7867,13 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SeparatorText("Tables"); ImGui::SliderFloat2("CellPadding", (float*)&style.CellPadding, 0.0f, 20.0f, "%.0f"); ImGui::SliderAngle("TableAngledHeadersAngle", &style.TableAngledHeadersAngle, -50.0f, +50.0f); + ImGui::SliderFloat2("TableAngledHeadersTextAlign", (float*)&style.TableAngledHeadersTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SeparatorText("Widgets"); ImGui::SliderFloat2("WindowTitleAlign", (float*)&style.WindowTitleAlign, 0.0f, 1.0f, "%.2f"); int window_menu_button_position = style.WindowMenuButtonPosition + 1; if (ImGui::Combo("WindowMenuButtonPosition", (int*)&window_menu_button_position, "None\0Left\0Right\0")) - style.WindowMenuButtonPosition = window_menu_button_position - 1; + style.WindowMenuButtonPosition = (ImGuiDir)(window_menu_button_position - 1); ImGui::Combo("ColorButtonPosition", (int*)&style.ColorButtonPosition, "Left\0Right\0"); ImGui::SliderFloat2("ButtonTextAlign", (float*)&style.ButtonTextAlign, 0.0f, 1.0f, "%.2f"); ImGui::SameLine(); HelpMarker("Alignment applies when a button is larger than its text content."); @@ -6646,7 +7898,8 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) } ImGui::SeparatorText("Misc"); - ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); + ImGui::SliderFloat2("DisplayWindowPadding", (float*)&style.DisplayWindowPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to regular windows: amount which we enforce to keep visible when moving near edges of your screen."); + ImGui::SliderFloat2("DisplaySafeAreaPadding", (float*)&style.DisplaySafeAreaPadding, 0.0f, 30.0f, "%.0f"); ImGui::SameLine(); HelpMarker("Apply to every windows, menus, popups, tooltips: amount where we avoid displaying contents. Adjust if you cannot see the edges of your screen (e.g. on a TV where scaling has not been configured)."); ImGui::EndTabItem(); } @@ -6688,7 +7941,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "Right-click to open edit options menu."); ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX)); - ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar | ImGuiWindowFlags_NavFlattened); + ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Border | ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); ImGui::PushItemWidth(ImGui::GetFontSize() * -12); for (int i = 0; i < ImGuiCol_COUNT; i++) { @@ -6769,10 +8022,10 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::SetNextWindowPos(ImGui::GetCursorScreenPos()); if (show_samples && ImGui::BeginTooltip()) { - ImGui::TextUnformatted("(R = radius, N = number of segments)"); + ImGui::TextUnformatted("(R = radius, N = approx number of segments)"); ImGui::Spacing(); ImDrawList* draw_list = ImGui::GetWindowDrawList(); - const float min_widget_width = ImGui::CalcTextSize("N: MMM\nR: MMM").x; + const float min_widget_width = ImGui::CalcTextSize("R: MMM\nN: MMM").x; for (int n = 0; n < 8; n++) { const float RAD_MIN = 5.0f; @@ -6781,6 +8034,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::BeginGroup(); + // N is not always exact here due to how PathArcTo() function work internally ImGui::Text("R: %.f\nN: %d", rad, draw_list->_CalcCircleAutoSegmentCount(rad)); const float canvas_width = IM_MAX(min_widget_width, rad * 2.0f); @@ -7079,6 +8333,7 @@ struct ExampleAppConsole } // Options, Filter + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_O, ImGuiInputFlags_Tooltip); if (ImGui::Button("Options")) ImGui::OpenPopup("Options"); ImGui::SameLine(); @@ -7087,7 +8342,7 @@ struct ExampleAppConsole // Reserve enough left-over height for 1 separator + 1 input text const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); - if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar)) + if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, -footer_height_to_reserve), ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_HorizontalScrollbar)) { if (ImGui::BeginPopupContextWindow()) { @@ -7563,57 +8818,130 @@ static void ShowExampleAppLayout(bool* p_open) //----------------------------------------------------------------------------- // [SECTION] Example App: Property Editor / ShowExampleAppPropertyEditor() //----------------------------------------------------------------------------- +// Some of the interactions are a bit lack-luster: +// - We would want pressing validating or leaving the filter to somehow restore focus. +// - We may want more advanced filtering (child nodes) and clipper support: both will need extra work. +// - We would want to customize some keyboard interactions to easily keyboard navigate between the tree and the properties. +//----------------------------------------------------------------------------- -static void ShowPlaceholderObject(const char* prefix, int uid) +struct ExampleAppPropertyEditor { - // Use object uid as identifier. Most commonly you could also use the object pointer as a base ID. - ImGui::PushID(uid); - - // Text and Tree nodes are less high than framed widgets, using AlignTextToFramePadding() we add vertical spacing to make the tree lines equal high. - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::AlignTextToFramePadding(); - bool node_open = ImGui::TreeNode("Object", "%s_%u", prefix, uid); - ImGui::TableSetColumnIndex(1); - ImGui::Text("my sailor is rich"); + ImGuiTextFilter Filter; + ExampleTreeNode* VisibleNode = NULL; - if (node_open) + void Draw(ExampleTreeNode* root_node) { - static float placeholder_members[8] = { 0.0f, 0.0f, 1.0f, 3.1416f, 100.0f, 999.0f }; - for (int i = 0; i < 8; i++) + // Left side: draw tree + // - Currently using a table to benefit from RowBg feature + if (ImGui::BeginChild("##tree", ImVec2(300, 0), ImGuiChildFlags_ResizeX | ImGuiChildFlags_Border | ImGuiChildFlags_NavFlattened)) { - ImGui::PushID(i); // Use field index as identifier. - if (i < 2) + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_F, ImGuiInputFlags_Tooltip); + ImGui::PushItemFlag(ImGuiItemFlags_NoNavDefaultFocus, true); + if (ImGui::InputTextWithHint("##Filter", "incl,-excl", Filter.InputBuf, IM_ARRAYSIZE(Filter.InputBuf), ImGuiInputTextFlags_EscapeClearsAll)) + Filter.Build(); + ImGui::PopItemFlag(); + + if (ImGui::BeginTable("##bg", 1, ImGuiTableFlags_RowBg)) { - ShowPlaceholderObject("Child", 424242); + for (ExampleTreeNode* node : root_node->Childs) + if (Filter.PassFilter(node->Name)) // Filter root node + DrawTreeNode(node); + ImGui::EndTable(); } - else - { - // Here we use a TreeNode to highlight on hover (we could use e.g. Selectable as well) - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - ImGui::AlignTextToFramePadding(); - ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet; - ImGui::TreeNodeEx("Field", flags, "Field_%d", i); + } + ImGui::EndChild(); - ImGui::TableSetColumnIndex(1); - ImGui::SetNextItemWidth(-FLT_MIN); - if (i >= 5) - ImGui::InputFloat("##value", &placeholder_members[i], 1.0f); - else - ImGui::DragFloat("##value", &placeholder_members[i], 0.01f); - ImGui::NextColumn(); + // Right side: draw properties + ImGui::SameLine(); + + ImGui::BeginGroup(); // Lock X position + if (ExampleTreeNode* node = VisibleNode) + { + ImGui::Text("%s", node->Name); + ImGui::TextDisabled("UID: 0x%08X", node->UID); + ImGui::Separator(); + if (ImGui::BeginTable("##properties", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY)) + { + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 2.0f); // Default twice larger + if (node->HasData) + { + // In a typical application, the structure description would be derived from a data-driven system. + // - We try to mimic this with our ExampleMemberInfo structure and the ExampleTreeNodeMemberInfos[] array. + // - Limits and some details are hard-coded to simplify the demo. + for (const ExampleMemberInfo& field_desc : ExampleTreeNodeMemberInfos) + { + ImGui::TableNextRow(); + ImGui::PushID(field_desc.Name); + ImGui::TableNextColumn(); + ImGui::AlignTextToFramePadding(); + ImGui::TextUnformatted(field_desc.Name); + ImGui::TableNextColumn(); + void* field_ptr = (void*)(((unsigned char*)node) + field_desc.Offset); + switch (field_desc.DataType) + { + case ImGuiDataType_Bool: + { + IM_ASSERT(field_desc.DataCount == 1); + ImGui::Checkbox("##Editor", (bool*)field_ptr); + break; + } + case ImGuiDataType_S32: + { + int v_min = INT_MIN, v_max = INT_MAX; + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::DragScalarN("##Editor", field_desc.DataType, field_ptr, field_desc.DataCount, 1.0f, &v_min, &v_max); + break; + } + case ImGuiDataType_Float: + { + float v_min = 0.0f, v_max = 1.0f; + ImGui::SetNextItemWidth(-FLT_MIN); + ImGui::SliderScalarN("##Editor", field_desc.DataType, field_ptr, field_desc.DataCount, &v_min, &v_max); + break; + } + } + ImGui::PopID(); + } + } + ImGui::EndTable(); } - ImGui::PopID(); } - ImGui::TreePop(); + ImGui::EndGroup(); } - ImGui::PopID(); -} -// Demonstrate create a simple property editor. -// This demo is a bit lackluster nowadays, would be nice to improve. -static void ShowExampleAppPropertyEditor(bool* p_open) + void DrawTreeNode(ExampleTreeNode* node) + { + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::PushID(node->UID); + ImGuiTreeNodeFlags tree_flags = ImGuiTreeNodeFlags_None; + tree_flags |= ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick; // Standard opening mode as we are likely to want to add selection afterwards + tree_flags |= ImGuiTreeNodeFlags_NavLeftJumpsBackHere; // Left arrow support + if (node == VisibleNode) + tree_flags |= ImGuiTreeNodeFlags_Selected; + if (node->Childs.Size == 0) + tree_flags |= ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_Bullet; + if (node->DataMyBool == false) + ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyle().Colors[ImGuiCol_TextDisabled]); + bool node_open = ImGui::TreeNodeEx("", tree_flags, "%s", node->Name); + if (node->DataMyBool == false) + ImGui::PopStyleColor(); + if (ImGui::IsItemFocused()) + VisibleNode = node; + if (node_open) + { + for (ExampleTreeNode* child : node->Childs) + DrawTreeNode(child); + ImGui::TreePop(); + } + ImGui::PopID(); + } +}; + +// Demonstrate creating a simple property editor. +static void ShowExampleAppPropertyEditor(bool* p_open, ImGuiDemoWindowData* demo_data) { ImGui::SetNextWindowSize(ImVec2(430, 450), ImGuiCond_FirstUseEver); if (!ImGui::Begin("Example: Property editor", p_open)) @@ -7623,25 +8951,11 @@ static void ShowExampleAppPropertyEditor(bool* p_open) } IMGUI_DEMO_MARKER("Examples/Property Editor"); - HelpMarker( - "This example shows how you may implement a property editor using two columns.\n" - "All objects/fields data are dummies here.\n"); - - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2, 2)); - if (ImGui::BeginTable("##split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable | ImGuiTableFlags_ScrollY)) - { - ImGui::TableSetupScrollFreeze(0, 1); - ImGui::TableSetupColumn("Object"); - ImGui::TableSetupColumn("Contents"); - ImGui::TableHeadersRow(); - - // Iterate placeholder objects (all the same data) - for (int obj_i = 0; obj_i < 4; obj_i++) - ShowPlaceholderObject("Object", obj_i); + static ExampleAppPropertyEditor property_editor; + if (demo_data->DemoTree == NULL) + demo_data->DemoTree = ExampleTree_CreateDemoTree(); + property_editor.Draw(demo_data->DemoTree); - ImGui::EndTable(); - } - ImGui::PopStyleVar(); ImGui::End(); } @@ -7789,7 +9103,7 @@ static void ShowExampleAppConstrainedResize(bool* p_open) if (type == 2) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 0), ImVec2(-1, FLT_MAX)); // Resize vertical + lock current width if (type == 3) ImGui::SetNextWindowSizeConstraints(ImVec2(0, -1), ImVec2(FLT_MAX, -1)); // Resize horizontal + lock current height if (type == 4) ImGui::SetNextWindowSizeConstraints(ImVec2(400, -1), ImVec2(500, -1)); // Width Between and 400 and 500 - if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 500), ImVec2(-1, FLT_MAX)); // Height at least 400 + if (type == 5) ImGui::SetNextWindowSizeConstraints(ImVec2(-1, 400), ImVec2(-1, FLT_MAX)); // Height at least 400 if (type == 6) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::AspectRatio, (void*)&aspect_ratio); // Aspect ratio if (type == 7) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Square); // Always Square if (type == 8) ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX), CustomConstraints::Step, (void*)&fixed_step); // Fixed Step @@ -7964,6 +9278,14 @@ static void ShowExampleAppWindowTitles(bool*) // [SECTION] Example App: Custom Rendering using ImDrawList API / ShowExampleAppCustomRendering() //----------------------------------------------------------------------------- +// Add a |_| looking shape +static void PathConcaveShape(ImDrawList* draw_list, float x, float y, float sz) +{ + const ImVec2 pos_norms[] = { { 0.0f, 0.0f }, { 0.3f, 0.0f }, { 0.3f, 0.7f }, { 0.7f, 0.7f }, { 0.7f, 0.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f }, { 0.0f, 1.0f } }; + for (const ImVec2& p : pos_norms) + draw_list->PathLineTo(ImVec2(x + 0.5f + (int)(sz * p.x), y + 0.5f + (int)(sz * p.y))); +} + // Demonstrate using the low-level ImDrawList to draw custom shapes. static void ShowExampleAppCustomRendering(bool* p_open) { @@ -8047,12 +9369,14 @@ static void ShowExampleAppCustomRendering(bool* p_open) float th = (n == 0) ? 1.0f : thickness; draw_list->AddNgon(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, ngon_sides, th); x += sz + spacing; // N-gon draw_list->AddCircle(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, col, circle_segments, th); x += sz + spacing; // Circle - draw_list->AddEllipse(ImVec2(x + sz*0.5f, y + sz*0.5f), sz*0.5f, sz*0.3f, col, -0.3f, circle_segments, th); x += sz + spacing; // Ellipse + draw_list->AddEllipse(ImVec2(x + sz*0.5f, y + sz*0.5f), ImVec2(sz*0.5f, sz*0.3f), col, -0.3f, circle_segments, th); x += sz + spacing; // Ellipse draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 0.0f, ImDrawFlags_None, th); x += sz + spacing; // Square draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, ImDrawFlags_None, th); x += sz + spacing; // Square with all rounded corners draw_list->AddRect(ImVec2(x, y), ImVec2(x + sz, y + sz), col, rounding, corners_tl_br, th); x += sz + spacing; // Square with two rounded corners draw_list->AddTriangle(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col, th);x += sz + spacing; // Triangle //draw_list->AddTriangle(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col, th);x+= sz*0.4f + spacing; // Thin triangle + PathConcaveShape(draw_list, x, y, sz); draw_list->PathStroke(col, ImDrawFlags_Closed, th); x += sz + spacing; // Concave Shape + //draw_list->AddPolyline(concave_shape, IM_ARRAYSIZE(concave_shape), col, ImDrawFlags_Closed, th); draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y), col, th); x += sz + spacing; // Horizontal line (note: drawing a filled rectangle will be faster!) draw_list->AddLine(ImVec2(x, y), ImVec2(x, y + sz), col, th); x += spacing; // Vertical line (note: drawing a filled rectangle will be faster!) draw_list->AddLine(ImVec2(x, y), ImVec2(x + sz, y + sz), col, th); x += sz + spacing; // Diagonal line @@ -8076,12 +9400,13 @@ static void ShowExampleAppCustomRendering(bool* p_open) // Filled shapes draw_list->AddNgonFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, ngon_sides); x += sz + spacing; // N-gon draw_list->AddCircleFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, col, circle_segments); x += sz + spacing; // Circle - draw_list->AddEllipseFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), sz * 0.5f, sz * 0.3f, col, -0.3f, circle_segments); x += sz + spacing;// Ellipse + draw_list->AddEllipseFilled(ImVec2(x + sz * 0.5f, y + sz * 0.5f), ImVec2(sz * 0.5f, sz * 0.3f), col, -0.3f, circle_segments); x += sz + spacing;// Ellipse draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col); x += sz + spacing; // Square draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f); x += sz + spacing; // Square with all rounded corners draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + sz), col, 10.0f, corners_tl_br); x += sz + spacing; // Square with two rounded corners draw_list->AddTriangleFilled(ImVec2(x+sz*0.5f,y), ImVec2(x+sz, y+sz-0.5f), ImVec2(x, y+sz-0.5f), col); x += sz + spacing; // Triangle //draw_list->AddTriangleFilled(ImVec2(x+sz*0.2f,y), ImVec2(x, y+sz-0.5f), ImVec2(x+sz*0.4f, y+sz-0.5f), col); x += sz*0.4f + spacing; // Thin triangle + PathConcaveShape(draw_list, x, y, sz); draw_list->PathFillConcave(col); x += sz + spacing; // Concave shape draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + sz, y + thickness), col); x += sz + spacing; // Horizontal line (faster than AddLine, but only handle integer thickness) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + thickness, y + sz), col); x += spacing * 2.0f;// Vertical line (faster than AddLine, but only handle integer thickness) draw_list->AddRectFilled(ImVec2(x, y), ImVec2(x + 1, y + 1), col); x += sz; // Pixel (faster than AddLine) @@ -8097,15 +9422,10 @@ static void ShowExampleAppCustomRendering(bool* p_open) draw_list->PathFillConvex(col); x += sz + spacing; - // Cubic Bezier Curve (4 control points): this is concave so not drawing it yet - //draw_list->PathLineTo(ImVec2(x + cp4[0].x, y + cp4[0].y)); - //draw_list->PathBezierCubicCurveTo(ImVec2(x + cp4[1].x, y + cp4[1].y), ImVec2(x + cp4[2].x, y + cp4[2].y), ImVec2(x + cp4[3].x, y + cp4[3].y), curve_segments); - //draw_list->PathFillConvex(col); - //x += sz + spacing; - draw_list->AddRectFilledMultiColor(ImVec2(x, y), ImVec2(x + sz, y + sz), IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(255, 255, 0, 255), IM_COL32(0, 255, 0, 255)); + x += sz + spacing; - ImGui::Dummy(ImVec2((sz + spacing) * 12.2f, (sz + spacing) * 3.0f)); + ImGui::Dummy(ImVec2((sz + spacing) * 13.2f, (sz + spacing) * 3.0f)); ImGui::PopItemWidth(); ImGui::EndTabItem(); } @@ -8275,92 +9595,119 @@ static void ShowExampleAppCustomRendering(bool* p_open) // Simplified structure to mimic a Document model struct MyDocument { - const char* Name; // Document title + char Name[32]; // Document title + int UID; // Unique ID (necessary as we can change title) bool Open; // Set when open (we keep an array of all available documents to simplify demo code!) bool OpenPrev; // Copy of Open from last update. bool Dirty; // Set when the document has been modified - bool WantClose; // Set when the document ImVec4 Color; // An arbitrary variable associated to the document - MyDocument(const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)) + MyDocument(int uid, const char* name, bool open = true, const ImVec4& color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f)) { - Name = name; + UID = uid; + snprintf(Name, sizeof(Name), "%s", name); Open = OpenPrev = open; Dirty = false; - WantClose = false; Color = color; } void DoOpen() { Open = true; } - void DoQueueClose() { WantClose = true; } void DoForceClose() { Open = false; Dirty = false; } void DoSave() { Dirty = false; } +}; + +struct ExampleAppDocuments +{ + ImVector Documents; + ImVector CloseQueue; + MyDocument* RenamingDoc = NULL; + bool RenamingStarted = false; + + ExampleAppDocuments() + { + Documents.push_back(MyDocument(0, "Lettuce", true, ImVec4(0.4f, 0.8f, 0.4f, 1.0f))); + Documents.push_back(MyDocument(1, "Eggplant", true, ImVec4(0.8f, 0.5f, 1.0f, 1.0f))); + Documents.push_back(MyDocument(2, "Carrot", true, ImVec4(1.0f, 0.8f, 0.5f, 1.0f))); + Documents.push_back(MyDocument(3, "Tomato", false, ImVec4(1.0f, 0.3f, 0.4f, 1.0f))); + Documents.push_back(MyDocument(4, "A Rather Long Title", false, ImVec4(0.4f, 0.8f, 0.8f, 1.0f))); + Documents.push_back(MyDocument(5, "Some Document", false, ImVec4(0.8f, 0.8f, 1.0f, 1.0f))); + } + + // As we allow to change document name, we append a never-changing document ID so tabs are stable + void GetTabName(MyDocument* doc, char* out_buf, size_t out_buf_size) + { + snprintf(out_buf, out_buf_size, "%s###doc%d", doc->Name, doc->UID); + } // Display placeholder contents for the Document - static void DisplayContents(MyDocument* doc) + void DisplayDocContents(MyDocument* doc) { ImGui::PushID(doc); ImGui::Text("Document \"%s\"", doc->Name); ImGui::PushStyleColor(ImGuiCol_Text, doc->Color); ImGui::TextWrapped("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); ImGui::PopStyleColor(); - if (ImGui::Button("Modify", ImVec2(100, 0))) + + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_R, ImGuiInputFlags_Tooltip); + if (ImGui::Button("Rename..")) + { + RenamingDoc = doc; + RenamingStarted = true; + } + ImGui::SameLine(); + + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_M, ImGuiInputFlags_Tooltip); + if (ImGui::Button("Modify")) doc->Dirty = true; + ImGui::SameLine(); - if (ImGui::Button("Save", ImVec2(100, 0))) + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_S, ImGuiInputFlags_Tooltip); + if (ImGui::Button("Save")) doc->DoSave(); + + ImGui::SameLine(); + ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_W, ImGuiInputFlags_Tooltip); + if (ImGui::Button("Close")) + CloseQueue.push_back(doc); ImGui::ColorEdit3("color", &doc->Color.x); // Useful to test drag and drop and hold-dragged-to-open-tab behavior. ImGui::PopID(); } // Display context menu for the Document - static void DisplayContextMenu(MyDocument* doc) + void DisplayDocContextMenu(MyDocument* doc) { if (!ImGui::BeginPopupContextItem()) return; char buf[256]; sprintf(buf, "Save %s", doc->Name); - if (ImGui::MenuItem(buf, "CTRL+S", false, doc->Open)) + if (ImGui::MenuItem(buf, "Ctrl+S", false, doc->Open)) doc->DoSave(); - if (ImGui::MenuItem("Close", "CTRL+W", false, doc->Open)) - doc->DoQueueClose(); + if (ImGui::MenuItem("Rename...", "Ctrl+R", false, doc->Open)) + RenamingDoc = doc; + if (ImGui::MenuItem("Close", "Ctrl+W", false, doc->Open)) + CloseQueue.push_back(doc); ImGui::EndPopup(); } -}; -struct ExampleAppDocuments -{ - ImVector Documents; - - ExampleAppDocuments() + // [Optional] Notify the system of Tabs/Windows closure that happened outside the regular tab interface. + // If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo, + // as opposed to clicking on the regular tab closing button) and stops being submitted, it will take a frame for + // the tab bar to notice its absence. During this frame there will be a gap in the tab bar, and if the tab that has + // disappeared was the selected one, the tab bar will report no selected tab during the frame. This will effectively + // give the impression of a flicker for one frame. + // We call SetTabItemClosed() to manually notify the Tab Bar or Docking system of removed tabs to avoid this glitch. + // Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag. + void NotifyOfDocumentsClosedElsewhere() { - Documents.push_back(MyDocument("Lettuce", true, ImVec4(0.4f, 0.8f, 0.4f, 1.0f))); - Documents.push_back(MyDocument("Eggplant", true, ImVec4(0.8f, 0.5f, 1.0f, 1.0f))); - Documents.push_back(MyDocument("Carrot", true, ImVec4(1.0f, 0.8f, 0.5f, 1.0f))); - Documents.push_back(MyDocument("Tomato", false, ImVec4(1.0f, 0.3f, 0.4f, 1.0f))); - Documents.push_back(MyDocument("A Rather Long Title", false)); - Documents.push_back(MyDocument("Some Document", false)); + for (MyDocument& doc : Documents) + { + if (!doc.Open && doc.OpenPrev) + ImGui::SetTabItemClosed(doc.Name); + doc.OpenPrev = doc.Open; + } } }; -// [Optional] Notify the system of Tabs/Windows closure that happened outside the regular tab interface. -// If a tab has been closed programmatically (aka closed from another source such as the Checkbox() in the demo, -// as opposed to clicking on the regular tab closing button) and stops being submitted, it will take a frame for -// the tab bar to notice its absence. During this frame there will be a gap in the tab bar, and if the tab that has -// disappeared was the selected one, the tab bar will report no selected tab during the frame. This will effectively -// give the impression of a flicker for one frame. -// We call SetTabItemClosed() to manually notify the Tab Bar or Docking system of removed tabs to avoid this glitch. -// Note that this completely optional, and only affect tab bars with the ImGuiTabBarFlags_Reorderable flag. -static void NotifyOfDocumentsClosedElsewhere(ExampleAppDocuments& app) -{ - for (MyDocument& doc : app.Documents) - { - if (!doc.Open && doc.OpenPrev) - ImGui::SetTabItemClosed(doc.Name); - doc.OpenPrev = doc.Open; - } -} - void ShowExampleAppDocuments(bool* p_open) { static ExampleAppDocuments app; @@ -8394,8 +9741,8 @@ void ShowExampleAppDocuments(bool* p_open) } if (ImGui::MenuItem("Close All Documents", NULL, false, open_count > 0)) for (MyDocument& doc : app.Documents) - doc.DoQueueClose(); - if (ImGui::MenuItem("Exit", "Ctrl+F4") && p_open) + app.CloseQueue.push_back(&doc); + if (ImGui::MenuItem("Exit") && p_open) *p_open = false; ImGui::EndMenu(); } @@ -8430,10 +9777,11 @@ void ShowExampleAppDocuments(bool* p_open) // Submit Tab Bar and Tabs { ImGuiTabBarFlags tab_bar_flags = (opt_fitting_flags) | (opt_reorderable ? ImGuiTabBarFlags_Reorderable : 0); + tab_bar_flags |= ImGuiTabBarFlags_DrawSelectedOverline; if (ImGui::BeginTabBar("##tabs", tab_bar_flags)) { if (opt_reorderable) - NotifyOfDocumentsClosedElsewhere(app); + app.NotifyOfDocumentsClosedElsewhere(); // [DEBUG] Stress tests //if ((ImGui::GetFrameCount() % 30) == 0) docs[1].Open ^= 1; // [DEBUG] Automatically show/hide a tab. Test various interactions e.g. dragging with this on. @@ -8445,20 +9793,23 @@ void ShowExampleAppDocuments(bool* p_open) if (!doc.Open) continue; + // As we allow to change document name, we append a never-changing document id so tabs are stable + char doc_name_buf[64]; + app.GetTabName(&doc, doc_name_buf, sizeof(doc_name_buf)); ImGuiTabItemFlags tab_flags = (doc.Dirty ? ImGuiTabItemFlags_UnsavedDocument : 0); - bool visible = ImGui::BeginTabItem(doc.Name, &doc.Open, tab_flags); + bool visible = ImGui::BeginTabItem(doc_name_buf, &doc.Open, tab_flags); // Cancel attempt to close when unsaved add to save queue so we can display a popup. if (!doc.Open && doc.Dirty) { doc.Open = true; - doc.DoQueueClose(); + app.CloseQueue.push_back(&doc); } - MyDocument::DisplayContextMenu(&doc); + app.DisplayDocContextMenu(&doc); if (visible) { - MyDocument::DisplayContents(&doc); + app.DisplayDocContents(&doc); ImGui::EndTabItem(); } } @@ -8467,33 +9818,44 @@ void ShowExampleAppDocuments(bool* p_open) } } - // Update closing queue - static ImVector close_queue; - if (close_queue.empty()) + // Display renaming UI + if (app.RenamingDoc != NULL) { - // Close queue is locked once we started a popup - for (MyDocument& doc : app.Documents) - if (doc.WantClose) + if (app.RenamingStarted) + ImGui::OpenPopup("Rename"); + if (ImGui::BeginPopup("Rename")) + { + ImGui::SetNextItemWidth(ImGui::GetFontSize() * 30); + if (ImGui::InputText("###Name", app.RenamingDoc->Name, IM_ARRAYSIZE(app.RenamingDoc->Name), ImGuiInputTextFlags_EnterReturnsTrue)) { - doc.WantClose = false; - close_queue.push_back(&doc); + ImGui::CloseCurrentPopup(); + app.RenamingDoc = NULL; } + if (app.RenamingStarted) + ImGui::SetKeyboardFocusHere(-1); + ImGui::EndPopup(); + } + else + { + app.RenamingDoc = NULL; + } + app.RenamingStarted = false; } // Display closing confirmation UI - if (!close_queue.empty()) + if (!app.CloseQueue.empty()) { int close_queue_unsaved_documents = 0; - for (int n = 0; n < close_queue.Size; n++) - if (close_queue[n]->Dirty) + for (int n = 0; n < app.CloseQueue.Size; n++) + if (app.CloseQueue[n]->Dirty) close_queue_unsaved_documents++; if (close_queue_unsaved_documents == 0) { // Close documents when all are unsaved - for (int n = 0; n < close_queue.Size; n++) - close_queue[n]->DoForceClose(); - close_queue.clear(); + for (int n = 0; n < app.CloseQueue.Size; n++) + app.CloseQueue[n]->DoForceClose(); + app.CloseQueue.clear(); } else { @@ -8504,37 +9866,35 @@ void ShowExampleAppDocuments(bool* p_open) ImGui::Text("Save change to the following items?"); float item_height = ImGui::GetTextLineHeightWithSpacing(); if (ImGui::BeginChild(ImGui::GetID("frame"), ImVec2(-FLT_MIN, 6.25f * item_height), ImGuiChildFlags_FrameStyle)) - { - for (int n = 0; n < close_queue.Size; n++) - if (close_queue[n]->Dirty) - ImGui::Text("%s", close_queue[n]->Name); - } + for (MyDocument* doc : app.CloseQueue) + if (doc->Dirty) + ImGui::Text("%s", doc->Name); ImGui::EndChild(); ImVec2 button_size(ImGui::GetFontSize() * 7.0f, 0.0f); if (ImGui::Button("Yes", button_size)) { - for (int n = 0; n < close_queue.Size; n++) + for (MyDocument* doc : app.CloseQueue) { - if (close_queue[n]->Dirty) - close_queue[n]->DoSave(); - close_queue[n]->DoForceClose(); + if (doc->Dirty) + doc->DoSave(); + doc->DoForceClose(); } - close_queue.clear(); + app.CloseQueue.clear(); ImGui::CloseCurrentPopup(); } ImGui::SameLine(); if (ImGui::Button("No", button_size)) { - for (int n = 0; n < close_queue.Size; n++) - close_queue[n]->DoForceClose(); - close_queue.clear(); + for (MyDocument* doc : app.CloseQueue) + doc->DoForceClose(); + app.CloseQueue.clear(); ImGui::CloseCurrentPopup(); } ImGui::SameLine(); if (ImGui::Button("Cancel", button_size)) { - close_queue.clear(); + app.CloseQueue.clear(); ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); @@ -8545,6 +9905,404 @@ void ShowExampleAppDocuments(bool* p_open) ImGui::End(); } +//----------------------------------------------------------------------------- +// [SECTION] Example App: Assets Browser / ShowExampleAppAssetsBrowser() +//----------------------------------------------------------------------------- + +//#include "imgui_internal.h" // NavMoveRequestTryWrapping() + +struct ExampleAsset +{ + ImGuiID ID; + int Type; + + ExampleAsset(ImGuiID id, int type) { ID = id; Type = type; } + + static const ImGuiTableSortSpecs* s_current_sort_specs; + + static void SortWithSortSpecs(ImGuiTableSortSpecs* sort_specs, ExampleAsset* items, int items_count) + { + s_current_sort_specs = sort_specs; // Store in variable accessible by the sort function. + if (items_count > 1) + qsort(items, (size_t)items_count, sizeof(items[0]), ExampleAsset::CompareWithSortSpecs); + s_current_sort_specs = NULL; + } + + // Compare function to be used by qsort() + static int IMGUI_CDECL CompareWithSortSpecs(const void* lhs, const void* rhs) + { + const ExampleAsset* a = (const ExampleAsset*)lhs; + const ExampleAsset* b = (const ExampleAsset*)rhs; + for (int n = 0; n < s_current_sort_specs->SpecsCount; n++) + { + const ImGuiTableColumnSortSpecs* sort_spec = &s_current_sort_specs->Specs[n]; + int delta = 0; + if (sort_spec->ColumnIndex == 0) + delta = ((int)a->ID - (int)b->ID); + else if (sort_spec->ColumnIndex == 1) + delta = (a->Type - b->Type); + if (delta > 0) + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? +1 : -1; + if (delta < 0) + return (sort_spec->SortDirection == ImGuiSortDirection_Ascending) ? -1 : +1; + } + return ((int)a->ID - (int)b->ID); + } +}; +const ImGuiTableSortSpecs* ExampleAsset::s_current_sort_specs = NULL; + +struct ExampleAssetsBrowser +{ + // Options + bool ShowTypeOverlay = true; + bool AllowSorting = true; + bool AllowDragUnselected = false; + bool AllowBoxSelect = true; + float IconSize = 32.0f; + int IconSpacing = 10; + int IconHitSpacing = 4; // Increase hit-spacing if you want to make it possible to clear or box-select from gaps. Some spacing is required to able to amend with Shift+box-select. Value is small in Explorer. + bool StretchSpacing = true; + + // State + ImVector Items; // Our items + ExampleSelectionWithDeletion Selection; // Our selection (ImGuiSelectionBasicStorage + helper funcs to handle deletion) + ImGuiID NextItemId = 0; // Unique identifier when creating new items + bool RequestDelete = false; // Deferred deletion request + bool RequestSort = false; // Deferred sort request + float ZoomWheelAccum = 0.0f; // Mouse wheel accumulator to handle smooth wheels better + + // Calculated sizes for layout, output of UpdateLayoutSizes(). Could be locals but our code is simpler this way. + ImVec2 LayoutItemSize; + ImVec2 LayoutItemStep; // == LayoutItemSize + LayoutItemSpacing + float LayoutItemSpacing = 0.0f; + float LayoutSelectableSpacing = 0.0f; + float LayoutOuterPadding = 0.0f; + int LayoutColumnCount = 0; + int LayoutLineCount = 0; + + // Functions + ExampleAssetsBrowser() + { + AddItems(10000); + } + void AddItems(int count) + { + if (Items.Size == 0) + NextItemId = 0; + Items.reserve(Items.Size + count); + for (int n = 0; n < count; n++, NextItemId++) + Items.push_back(ExampleAsset(NextItemId, (NextItemId % 20) < 15 ? 0 : (NextItemId % 20) < 18 ? 1 : 2)); + RequestSort = true; + } + void ClearItems() + { + Items.clear(); + Selection.Clear(); + } + + // Logic would be written in the main code BeginChild() and outputing to local variables. + // We extracted it into a function so we can call it easily from multiple places. + void UpdateLayoutSizes(float avail_width) + { + // Layout: when not stretching: allow extending into right-most spacing. + LayoutItemSpacing = (float)IconSpacing; + if (StretchSpacing == false) + avail_width += floorf(LayoutItemSpacing * 0.5f); + + // Layout: calculate number of icon per line and number of lines + LayoutItemSize = ImVec2(floorf(IconSize), floorf(IconSize)); + LayoutColumnCount = IM_MAX((int)(avail_width / (LayoutItemSize.x + LayoutItemSpacing)), 1); + LayoutLineCount = (Items.Size + LayoutColumnCount - 1) / LayoutColumnCount; + + // Layout: when stretching: allocate remaining space to more spacing. Round before division, so item_spacing may be non-integer. + if (StretchSpacing && LayoutColumnCount > 1) + LayoutItemSpacing = floorf(avail_width - LayoutItemSize.x * LayoutColumnCount) / LayoutColumnCount; + + LayoutItemStep = ImVec2(LayoutItemSize.x + LayoutItemSpacing, LayoutItemSize.y + LayoutItemSpacing); + LayoutSelectableSpacing = IM_MAX(floorf(LayoutItemSpacing) - IconHitSpacing, 0.0f); + LayoutOuterPadding = floorf(LayoutItemSpacing * 0.5f); + } + + void Draw(const char* title, bool* p_open) + { + ImGui::SetNextWindowSize(ImVec2(IconSize * 25, IconSize * 15), ImGuiCond_FirstUseEver); + if (!ImGui::Begin(title, p_open, ImGuiWindowFlags_MenuBar)) + { + ImGui::End(); + return; + } + + // Menu bar + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Add 10000 items")) + AddItems(10000); + if (ImGui::MenuItem("Clear items")) + ClearItems(); + ImGui::Separator(); + if (ImGui::MenuItem("Close", NULL, false, p_open != NULL)) + *p_open = false; + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Edit")) + { + if (ImGui::MenuItem("Delete", "Del", false, Selection.Size > 0)) + RequestDelete = true; + ImGui::EndMenu(); + } + if (ImGui::BeginMenu("Options")) + { + ImGui::PushItemWidth(ImGui::GetFontSize() * 10); + + ImGui::SeparatorText("Contents"); + ImGui::Checkbox("Show Type Overlay", &ShowTypeOverlay); + ImGui::Checkbox("Allow Sorting", &AllowSorting); + + ImGui::SeparatorText("Selection Behavior"); + ImGui::Checkbox("Allow dragging unselected item", &AllowDragUnselected); + ImGui::Checkbox("Allow box-selection", &AllowBoxSelect); + + ImGui::SeparatorText("Layout"); + ImGui::SliderFloat("Icon Size", &IconSize, 16.0f, 128.0f, "%.0f"); + ImGui::SameLine(); HelpMarker("Use CTRL+Wheel to zoom"); + ImGui::SliderInt("Icon Spacing", &IconSpacing, 0, 32); + ImGui::SliderInt("Icon Hit Spacing", &IconHitSpacing, 0, 32); + ImGui::Checkbox("Stretch Spacing", &StretchSpacing); + ImGui::PopItemWidth(); + ImGui::EndMenu(); + } + ImGui::EndMenuBar(); + } + + // Show a table with ONLY one header row to showcase the idea/possibility of using this to provide a sorting UI + if (AllowSorting) + { + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + ImGuiTableFlags table_flags_for_sort_specs = ImGuiTableFlags_Sortable | ImGuiTableFlags_SortMulti | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Borders; + if (ImGui::BeginTable("for_sort_specs_only", 2, table_flags_for_sort_specs, ImVec2(0.0f, ImGui::GetFrameHeight()))) + { + ImGui::TableSetupColumn("Index"); + ImGui::TableSetupColumn("Type"); + ImGui::TableHeadersRow(); + if (ImGuiTableSortSpecs* sort_specs = ImGui::TableGetSortSpecs()) + if (sort_specs->SpecsDirty || RequestSort) + { + ExampleAsset::SortWithSortSpecs(sort_specs, Items.Data, Items.Size); + sort_specs->SpecsDirty = RequestSort = false; + } + ImGui::EndTable(); + } + ImGui::PopStyleVar(); + } + + ImGuiIO& io = ImGui::GetIO(); + ImGui::SetNextWindowContentSize(ImVec2(0.0f, LayoutOuterPadding + LayoutLineCount * (LayoutItemSize.x + LayoutItemSpacing))); + if (ImGui::BeginChild("Assets", ImVec2(0.0f, -ImGui::GetTextLineHeightWithSpacing()), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove)) + { + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + + const float avail_width = ImGui::GetContentRegionAvail().x; + UpdateLayoutSizes(avail_width); + + // Calculate and store start position. + ImVec2 start_pos = ImGui::GetCursorScreenPos(); + start_pos = ImVec2(start_pos.x + LayoutOuterPadding, start_pos.y + LayoutOuterPadding); + ImGui::SetCursorScreenPos(start_pos); + + // Multi-select + ImGuiMultiSelectFlags ms_flags = ImGuiMultiSelectFlags_ClearOnEscape | ImGuiMultiSelectFlags_ClearOnClickVoid; + + // - Enable box-select (in 2D mode, so that changing box-select rectangle X1/X2 boundaries will affect clipped items) + if (AllowBoxSelect) + ms_flags |= ImGuiMultiSelectFlags_BoxSelect2d; + + // - This feature allows dragging an unselected item without selecting it (rarely used) + if (AllowDragUnselected) + ms_flags |= ImGuiMultiSelectFlags_SelectOnClickRelease; + + // - Enable keyboard wrapping on X axis + // (FIXME-MULTISELECT: We haven't designed/exposed a general nav wrapping api yet, so this flag is provided as a courtesy to avoid doing: + // ImGui::NavMoveRequestTryWrapping(ImGui::GetCurrentWindow(), ImGuiNavMoveFlags_WrapX); + // When we finish implementing a more general API for this, we will obsolete this flag in favor of the new system) + ms_flags |= ImGuiMultiSelectFlags_NavWrapX; + + ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(ms_flags, Selection.Size, Items.Size); + + // Use custom selection adapter: store ID in selection (recommended) + Selection.UserData = this; + Selection.AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage* self_, int idx) { ExampleAssetsBrowser* self = (ExampleAssetsBrowser*)self_->UserData; return self->Items[idx].ID; }; + Selection.ApplyRequests(ms_io); + + const bool want_delete = (ImGui::Shortcut(ImGuiKey_Delete, ImGuiInputFlags_Repeat) && (Selection.Size > 0)) || RequestDelete; + const int item_curr_idx_to_focus = want_delete ? Selection.ApplyDeletionPreLoop(ms_io, Items.Size) : -1; + RequestDelete = false; + + // Push LayoutSelectableSpacing (which is LayoutItemSpacing minus hit-spacing, if we decide to have hit gaps between items) + // Altering style ItemSpacing may seem unnecessary as we position every items using SetCursorScreenPos()... + // But it is necessary for two reasons: + // - Selectables uses it by default to visually fill the space between two items. + // - The vertical spacing would be measured by Clipper to calculate line height if we didn't provide it explicitly (here we do). + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(LayoutSelectableSpacing, LayoutSelectableSpacing)); + + // Rendering parameters + const ImU32 icon_type_overlay_colors[3] = { 0, IM_COL32(200, 70, 70, 255), IM_COL32(70, 170, 70, 255) }; + const ImU32 icon_bg_color = ImGui::GetColorU32(ImGuiCol_MenuBarBg); + const ImVec2 icon_type_overlay_size = ImVec2(4.0f, 4.0f); + const bool display_label = (LayoutItemSize.x >= ImGui::CalcTextSize("999").x); + + const int column_count = LayoutColumnCount; + ImGuiListClipper clipper; + clipper.Begin(LayoutLineCount, LayoutItemStep.y); + if (item_curr_idx_to_focus != -1) + clipper.IncludeItemByIndex(item_curr_idx_to_focus / column_count); // Ensure focused item line is not clipped. + if (ms_io->RangeSrcItem != -1) + clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem / column_count); // Ensure RangeSrc item line is not clipped. + while (clipper.Step()) + { + for (int line_idx = clipper.DisplayStart; line_idx < clipper.DisplayEnd; line_idx++) + { + const int item_min_idx_for_current_line = line_idx * column_count; + const int item_max_idx_for_current_line = IM_MIN((line_idx + 1) * column_count, Items.Size); + for (int item_idx = item_min_idx_for_current_line; item_idx < item_max_idx_for_current_line; ++item_idx) + { + ExampleAsset* item_data = &Items[item_idx]; + ImGui::PushID((int)item_data->ID); + + // Position item + ImVec2 pos = ImVec2(start_pos.x + (item_idx % column_count) * LayoutItemStep.x, start_pos.y + line_idx * LayoutItemStep.y); + ImGui::SetCursorScreenPos(pos); + + ImGui::SetNextItemSelectionUserData(item_idx); + bool item_is_selected = Selection.Contains((ImGuiID)item_data->ID); + bool item_is_visible = ImGui::IsRectVisible(LayoutItemSize); + ImGui::Selectable("", item_is_selected, ImGuiSelectableFlags_None, LayoutItemSize); + + // Update our selection state immediately (without waiting for EndMultiSelect() requests) + // because we use this to alter the color of our text/icon. + if (ImGui::IsItemToggledSelection()) + item_is_selected = !item_is_selected; + + // Focus (for after deletion) + if (item_curr_idx_to_focus == item_idx) + ImGui::SetKeyboardFocusHere(-1); + + // Drag and drop + if (ImGui::BeginDragDropSource()) + { + // Create payload with full selection OR single unselected item. + // (the later is only possible when using ImGuiMultiSelectFlags_SelectOnClickRelease) + if (ImGui::GetDragDropPayload() == NULL) + { + ImVector payload_items; + void* it = NULL; + ImGuiID id = 0; + if (!item_is_selected) + payload_items.push_back(item_data->ID); + else + while (Selection.GetNextSelectedItem(&it, &id)) + payload_items.push_back(id); + ImGui::SetDragDropPayload("ASSETS_BROWSER_ITEMS", payload_items.Data, (size_t)payload_items.size_in_bytes()); + } + + // Display payload content in tooltip, by extracting it from the payload data + // (we could read from selection, but it is more correct and reusable to read from payload) + const ImGuiPayload* payload = ImGui::GetDragDropPayload(); + const int payload_count = (int)payload->DataSize / (int)sizeof(ImGuiID); + ImGui::Text("%d assets", payload_count); + + ImGui::EndDragDropSource(); + } + + // Render icon (a real app would likely display an image/thumbnail here) + // Because we use ImGuiMultiSelectFlags_BoxSelect2d, clipping vertical may occasionally be larger, so we coarse-clip our rendering as well. + if (item_is_visible) + { + ImVec2 box_min(pos.x - 1, pos.y - 1); + ImVec2 box_max(box_min.x + LayoutItemSize.x + 2, box_min.y + LayoutItemSize.y + 2); // Dubious + draw_list->AddRectFilled(box_min, box_max, icon_bg_color); // Background color + if (ShowTypeOverlay && item_data->Type != 0) + { + ImU32 type_col = icon_type_overlay_colors[item_data->Type % IM_ARRAYSIZE(icon_type_overlay_colors)]; + draw_list->AddRectFilled(ImVec2(box_max.x - 2 - icon_type_overlay_size.x, box_min.y + 2), ImVec2(box_max.x - 2, box_min.y + 2 + icon_type_overlay_size.y), type_col); + } + if (display_label) + { + ImU32 label_col = ImGui::GetColorU32(item_is_selected ? ImGuiCol_Text : ImGuiCol_TextDisabled); + char label[32]; + sprintf(label, "%d", item_data->ID); + draw_list->AddText(ImVec2(box_min.x, box_max.y - ImGui::GetFontSize()), label_col, label); + } + } + + ImGui::PopID(); + } + } + } + clipper.End(); + ImGui::PopStyleVar(); // ImGuiStyleVar_ItemSpacing + + // Context menu + if (ImGui::BeginPopupContextWindow()) + { + ImGui::Text("Selection: %d items", Selection.Size); + ImGui::Separator(); + if (ImGui::MenuItem("Delete", "Del", false, Selection.Size > 0)) + RequestDelete = true; + ImGui::EndPopup(); + } + + ms_io = ImGui::EndMultiSelect(); + Selection.ApplyRequests(ms_io); + if (want_delete) + Selection.ApplyDeletionPostLoop(ms_io, Items, item_curr_idx_to_focus); + + // Zooming with CTRL+Wheel + if (ImGui::IsWindowAppearing()) + ZoomWheelAccum = 0.0f; + if (ImGui::IsWindowHovered() && io.MouseWheel != 0.0f && ImGui::IsKeyDown(ImGuiMod_Ctrl) && ImGui::IsAnyItemActive() == false) + { + ZoomWheelAccum += io.MouseWheel; + if (fabsf(ZoomWheelAccum) >= 1.0f) + { + // Calculate hovered item index from mouse location + // FIXME: Locking aiming on 'hovered_item_idx' (with a cool-down timer) would ensure zoom keeps on it. + const float hovered_item_nx = (io.MousePos.x - start_pos.x + LayoutItemSpacing * 0.5f) / LayoutItemStep.x; + const float hovered_item_ny = (io.MousePos.y - start_pos.y + LayoutItemSpacing * 0.5f) / LayoutItemStep.y; + const int hovered_item_idx = ((int)hovered_item_ny * LayoutColumnCount) + (int)hovered_item_nx; + //ImGui::SetTooltip("%f,%f -> item %d", hovered_item_nx, hovered_item_ny, hovered_item_idx); // Move those 4 lines in block above for easy debugging + + // Zoom + IconSize *= powf(1.1f, (float)(int)ZoomWheelAccum); + IconSize = IM_CLAMP(IconSize, 16.0f, 128.0f); + ZoomWheelAccum -= (int)ZoomWheelAccum; + UpdateLayoutSizes(avail_width); + + // Manipulate scroll to that we will land at the same Y location of currently hovered item. + // - Calculate next frame position of item under mouse + // - Set new scroll position to be used in next ImGui::BeginChild() call. + float hovered_item_rel_pos_y = ((float)(hovered_item_idx / LayoutColumnCount) + fmodf(hovered_item_ny, 1.0f)) * LayoutItemStep.y; + hovered_item_rel_pos_y += ImGui::GetStyle().WindowPadding.y; + float mouse_local_y = io.MousePos.y - ImGui::GetWindowPos().y; + ImGui::SetScrollY(hovered_item_rel_pos_y - mouse_local_y); + } + } + } + ImGui::EndChild(); + + ImGui::Text("Selected: %d/%d items", Selection.Size, Items.Size); + ImGui::End(); + } +}; + +void ShowExampleAppAssetsBrowser(bool* p_open) +{ + IMGUI_DEMO_MARKER("Examples/Assets Browser"); + static ExampleAssetsBrowser assets_browser; + assets_browser.Draw("Example: Assets Browser", p_open); +} + // End of Demo code #else diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 1319a6e1..57486a6e 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (drawing and font code) /* @@ -8,6 +8,7 @@ Index of this file: // [SECTION] STB libraries implementation // [SECTION] Style functions // [SECTION] ImDrawList +// [SECTION] ImTriangulator, ImDrawList concave polygon fill // [SECTION] ImDrawListSplitter // [SECTION] ImDrawData // [SECTION] Helpers ShadeVertsXXX functions @@ -64,6 +65,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision #pragma clang diagnostic ignored "-Wreserved-identifier" // warning: identifier '_Xxx' is reserved because it starts with '_' followed by a capital letter +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunused-function" // warning: 'xxxx' defined but not used @@ -209,11 +211,13 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst) colors[ImGuiCol_ResizeGrip] = ImVec4(0.26f, 0.59f, 0.98f, 0.20f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); - colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; - colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); - colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); - colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); + colors[ImGuiCol_TabSelected] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; + colors[ImGuiCol_TabDimmed] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabDimmedSelected] = ImLerp(colors[ImGuiCol_TabSelected], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); @@ -223,6 +227,7 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst) colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); + colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); @@ -269,11 +274,13 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.78f, 0.82f, 1.00f, 0.60f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.78f, 0.82f, 1.00f, 0.90f); - colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; - colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); - colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); - colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); + colors[ImGuiCol_TabSelected] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; + colors[ImGuiCol_TabDimmed] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabDimmedSelected] = ImLerp(colors[ImGuiCol_TabSelected], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_TabDimmedSelectedOverline] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); @@ -283,6 +290,7 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_TableBorderLight] = ImVec4(0.26f, 0.26f, 0.28f, 1.00f); // Prefer using Alpha=1.0 here colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.07f); + colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; @@ -330,11 +338,13 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) colors[ImGuiCol_ResizeGrip] = ImVec4(0.35f, 0.35f, 0.35f, 0.17f); colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); - colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; - colors[ImGuiCol_TabActive] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); - colors[ImGuiCol_TabUnfocused] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); - colors[ImGuiCol_TabUnfocusedActive] = ImLerp(colors[ImGuiCol_TabActive], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.90f); + colors[ImGuiCol_TabSelected] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); + colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; + colors[ImGuiCol_TabDimmed] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); + colors[ImGuiCol_TabDimmedSelected] = ImLerp(colors[ImGuiCol_TabSelected], colors[ImGuiCol_TitleBg], 0.40f); + colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.26f, 0.59f, 1.00f, 1.00f); colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f); colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); @@ -344,6 +354,7 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) colors[ImGuiCol_TableBorderLight] = ImVec4(0.68f, 0.68f, 0.74f, 1.00f); // Prefer using Alpha=1.0 here colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); colors[ImGuiCol_TableRowBgAlt] = ImVec4(0.30f, 0.30f, 0.30f, 0.09f); + colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; @@ -383,6 +394,7 @@ void ImDrawListSharedData::SetCircleTessellationMaxError(float max_error) } // Initialize before use in a new frame. We always have a command ready in the buffer. +// In the majority of cases, you would want to call PushClipRect() and PushTextureID() after this. void ImDrawList::_ResetForNewFrame() { // Verify that the ImDrawCmd fields we want to memcmp() are contiguous in memory. @@ -1217,10 +1229,10 @@ void ImDrawList::PathArcTo(const ImVec2& center, float radius, float a_min, floa } } -void ImDrawList::PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments) +void ImDrawList::PathEllipticalArcTo(const ImVec2& center, const ImVec2& radius, float rot, float a_min, float a_max, int num_segments) { if (num_segments <= 0) - num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius.x, radius.y)); // A bit pessimistic, maybe there's a better computation to do here. _Path.reserve(_Path.Size + (num_segments + 1)); @@ -1229,11 +1241,10 @@ void ImDrawList::PathEllipticalArcTo(const ImVec2& center, float radius_x, float for (int i = 0; i <= num_segments; i++) { const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min); - ImVec2 point(ImCos(a) * radius_x, ImSin(a) * radius_y); - const float rel_x = (point.x * cos_rot) - (point.y * sin_rot); - const float rel_y = (point.x * sin_rot) + (point.y * cos_rot); - point.x = rel_x + center.x; - point.y = rel_y + center.y; + ImVec2 point(ImCos(a) * radius.x, ImSin(a) * radius.y); + const ImVec2 rel((point.x * cos_rot) - (point.y * sin_rot), (point.x * sin_rot) + (point.y * cos_rot)); + point.x = rel.x + center.x; + point.y = rel.y + center.y; _Path.push_back(point); } } @@ -1558,31 +1569,31 @@ void ImDrawList::AddNgonFilled(const ImVec2& center, float radius, ImU32 col, in } // Ellipse -void ImDrawList::AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments, float thickness) +void ImDrawList::AddEllipse(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot, int num_segments, float thickness) { if ((col & IM_COL32_A_MASK) == 0) return; if (num_segments <= 0) - num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius.x, radius.y)); // A bit pessimistic, maybe there's a better computation to do here. // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; - PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); + PathEllipticalArcTo(center, radius, rot, 0.0f, a_max, num_segments - 1); PathStroke(col, true, thickness); } -void ImDrawList::AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot, int num_segments) +void ImDrawList::AddEllipseFilled(const ImVec2& center, const ImVec2& radius, ImU32 col, float rot, int num_segments) { if ((col & IM_COL32_A_MASK) == 0) return; if (num_segments <= 0) - num_segments = _CalcCircleAutoSegmentCount(ImMax(radius_x, radius_y)); // A bit pessimistic, maybe there's a better computation to do here. + num_segments = _CalcCircleAutoSegmentCount(ImMax(radius.x, radius.y)); // A bit pessimistic, maybe there's a better computation to do here. // Because we are filling a closed shape we remove 1 from the count of segments/points const float a_max = IM_PI * 2.0f * ((float)num_segments - 1.0f) / (float)num_segments; - PathEllipticalArcTo(center, radius_x, radius_y, rot, 0.0f, a_max, num_segments - 1); + PathEllipticalArcTo(center, radius, rot, 0.0f, a_max, num_segments - 1); PathFillConvex(col); } @@ -1613,10 +1624,11 @@ void ImDrawList::AddText(const ImFont* font, float font_size, const ImVec2& pos, if ((col & IM_COL32_A_MASK) == 0) return; + // Accept null ranges + if (text_begin == text_end || text_begin[0] == 0) + return; if (text_end == NULL) text_end = text_begin + strlen(text_begin); - if (text_begin == text_end) - return; // Pull default font/size from the shared ImDrawListSharedData instance if (font == NULL) @@ -1700,6 +1712,316 @@ void ImDrawList::AddImageRounded(ImTextureID user_texture_id, const ImVec2& p_mi PopTextureID(); } +//----------------------------------------------------------------------------- +// [SECTION] ImTriangulator, ImDrawList concave polygon fill +//----------------------------------------------------------------------------- +// Triangulate concave polygons. Based on "Triangulation by Ear Clipping" paper, O(N^2) complexity. +// Reference: https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf +// Provided as a convenience for user but not used by main library. +//----------------------------------------------------------------------------- +// - ImTriangulator [Internal] +// - AddConcavePolyFilled() +//----------------------------------------------------------------------------- + +enum ImTriangulatorNodeType +{ + ImTriangulatorNodeType_Convex, + ImTriangulatorNodeType_Ear, + ImTriangulatorNodeType_Reflex +}; + +struct ImTriangulatorNode +{ + ImTriangulatorNodeType Type; + int Index; + ImVec2 Pos; + ImTriangulatorNode* Next; + ImTriangulatorNode* Prev; + + void Unlink() { Next->Prev = Prev; Prev->Next = Next; } +}; + +struct ImTriangulatorNodeSpan +{ + ImTriangulatorNode** Data = NULL; + int Size = 0; + + void push_back(ImTriangulatorNode* node) { Data[Size++] = node; } + void find_erase_unsorted(int idx) { for (int i = Size - 1; i >= 0; i--) if (Data[i]->Index == idx) { Data[i] = Data[Size - 1]; Size--; return; } } +}; + +struct ImTriangulator +{ + static int EstimateTriangleCount(int points_count) { return (points_count < 3) ? 0 : points_count - 2; } + static int EstimateScratchBufferSize(int points_count) { return sizeof(ImTriangulatorNode) * points_count + sizeof(ImTriangulatorNode*) * points_count * 2; } + + void Init(const ImVec2* points, int points_count, void* scratch_buffer); + void GetNextTriangle(unsigned int out_triangle[3]); // Return relative indexes for next triangle + + // Internal functions + void BuildNodes(const ImVec2* points, int points_count); + void BuildReflexes(); + void BuildEars(); + void FlipNodeList(); + bool IsEar(int i0, int i1, int i2, const ImVec2& v0, const ImVec2& v1, const ImVec2& v2) const; + void ReclassifyNode(ImTriangulatorNode* node); + + // Internal members + int _TrianglesLeft = 0; + ImTriangulatorNode* _Nodes = NULL; + ImTriangulatorNodeSpan _Ears; + ImTriangulatorNodeSpan _Reflexes; +}; + +// Distribute storage for nodes, ears and reflexes. +// FIXME-OPT: if everything is convex, we could report it to caller and let it switch to an convex renderer +// (this would require first building reflexes to bail to convex if empty, without even building nodes) +void ImTriangulator::Init(const ImVec2* points, int points_count, void* scratch_buffer) +{ + IM_ASSERT(scratch_buffer != NULL && points_count >= 3); + _TrianglesLeft = EstimateTriangleCount(points_count); + _Nodes = (ImTriangulatorNode*)scratch_buffer; // points_count x Node + _Ears.Data = (ImTriangulatorNode**)(_Nodes + points_count); // points_count x Node* + _Reflexes.Data = (ImTriangulatorNode**)(_Nodes + points_count) + points_count; // points_count x Node* + BuildNodes(points, points_count); + BuildReflexes(); + BuildEars(); +} + +void ImTriangulator::BuildNodes(const ImVec2* points, int points_count) +{ + for (int i = 0; i < points_count; i++) + { + _Nodes[i].Type = ImTriangulatorNodeType_Convex; + _Nodes[i].Index = i; + _Nodes[i].Pos = points[i]; + _Nodes[i].Next = _Nodes + i + 1; + _Nodes[i].Prev = _Nodes + i - 1; + } + _Nodes[0].Prev = _Nodes + points_count - 1; + _Nodes[points_count - 1].Next = _Nodes; +} + +void ImTriangulator::BuildReflexes() +{ + ImTriangulatorNode* n1 = _Nodes; + for (int i = _TrianglesLeft; i >= 0; i--, n1 = n1->Next) + { + if (ImTriangleIsClockwise(n1->Prev->Pos, n1->Pos, n1->Next->Pos)) + continue; + n1->Type = ImTriangulatorNodeType_Reflex; + _Reflexes.push_back(n1); + } +} + +void ImTriangulator::BuildEars() +{ + ImTriangulatorNode* n1 = _Nodes; + for (int i = _TrianglesLeft; i >= 0; i--, n1 = n1->Next) + { + if (n1->Type != ImTriangulatorNodeType_Convex) + continue; + if (!IsEar(n1->Prev->Index, n1->Index, n1->Next->Index, n1->Prev->Pos, n1->Pos, n1->Next->Pos)) + continue; + n1->Type = ImTriangulatorNodeType_Ear; + _Ears.push_back(n1); + } +} + +void ImTriangulator::GetNextTriangle(unsigned int out_triangle[3]) +{ + if (_Ears.Size == 0) + { + FlipNodeList(); + + ImTriangulatorNode* node = _Nodes; + for (int i = _TrianglesLeft; i >= 0; i--, node = node->Next) + node->Type = ImTriangulatorNodeType_Convex; + _Reflexes.Size = 0; + BuildReflexes(); + BuildEars(); + + // If we still don't have ears, it means geometry is degenerated. + if (_Ears.Size == 0) + { + // Return first triangle available, mimicking the behavior of convex fill. + IM_ASSERT(_TrianglesLeft > 0); // Geometry is degenerated + _Ears.Data[0] = _Nodes; + _Ears.Size = 1; + } + } + + ImTriangulatorNode* ear = _Ears.Data[--_Ears.Size]; + out_triangle[0] = ear->Prev->Index; + out_triangle[1] = ear->Index; + out_triangle[2] = ear->Next->Index; + + ear->Unlink(); + if (ear == _Nodes) + _Nodes = ear->Next; + + ReclassifyNode(ear->Prev); + ReclassifyNode(ear->Next); + _TrianglesLeft--; +} + +void ImTriangulator::FlipNodeList() +{ + ImTriangulatorNode* prev = _Nodes; + ImTriangulatorNode* temp = _Nodes; + ImTriangulatorNode* current = _Nodes->Next; + prev->Next = prev; + prev->Prev = prev; + while (current != _Nodes) + { + temp = current->Next; + + current->Next = prev; + prev->Prev = current; + _Nodes->Next = current; + current->Prev = _Nodes; + + prev = current; + current = temp; + } + _Nodes = prev; +} + +// A triangle is an ear is no other vertex is inside it. We can test reflexes vertices only (see reference algorithm) +bool ImTriangulator::IsEar(int i0, int i1, int i2, const ImVec2& v0, const ImVec2& v1, const ImVec2& v2) const +{ + ImTriangulatorNode** p_end = _Reflexes.Data + _Reflexes.Size; + for (ImTriangulatorNode** p = _Reflexes.Data; p < p_end; p++) + { + ImTriangulatorNode* reflex = *p; + if (reflex->Index != i0 && reflex->Index != i1 && reflex->Index != i2) + if (ImTriangleContainsPoint(v0, v1, v2, reflex->Pos)) + return false; + } + return true; +} + +void ImTriangulator::ReclassifyNode(ImTriangulatorNode* n1) +{ + // Classify node + ImTriangulatorNodeType type; + const ImTriangulatorNode* n0 = n1->Prev; + const ImTriangulatorNode* n2 = n1->Next; + if (!ImTriangleIsClockwise(n0->Pos, n1->Pos, n2->Pos)) + type = ImTriangulatorNodeType_Reflex; + else if (IsEar(n0->Index, n1->Index, n2->Index, n0->Pos, n1->Pos, n2->Pos)) + type = ImTriangulatorNodeType_Ear; + else + type = ImTriangulatorNodeType_Convex; + + // Update lists when a type changes + if (type == n1->Type) + return; + if (n1->Type == ImTriangulatorNodeType_Reflex) + _Reflexes.find_erase_unsorted(n1->Index); + else if (n1->Type == ImTriangulatorNodeType_Ear) + _Ears.find_erase_unsorted(n1->Index); + if (type == ImTriangulatorNodeType_Reflex) + _Reflexes.push_back(n1); + else if (type == ImTriangulatorNodeType_Ear) + _Ears.push_back(n1); + n1->Type = type; +} + +// Use ear-clipping algorithm to triangulate a simple polygon (no self-interaction, no holes). +// (Reminder: we don't perform any coarse clipping/culling in ImDrawList layer! +// It is up to caller to ensure not making costly calls that will be outside of visible area. +// As concave fill is noticeably more expensive than other primitives, be mindful of this... +// Caller can build AABB of points, and avoid filling if 'draw_list->_CmdHeader.ClipRect.Overlays(points_bb) == false') +void ImDrawList::AddConcavePolyFilled(const ImVec2* points, const int points_count, ImU32 col) +{ + if (points_count < 3 || (col & IM_COL32_A_MASK) == 0) + return; + + const ImVec2 uv = _Data->TexUvWhitePixel; + ImTriangulator triangulator; + unsigned int triangle[3]; + if (Flags & ImDrawListFlags_AntiAliasedFill) + { + // Anti-aliased Fill + const float AA_SIZE = _FringeScale; + const ImU32 col_trans = col & ~IM_COL32_A_MASK; + const int idx_count = (points_count - 2) * 3 + points_count * 6; + const int vtx_count = (points_count * 2); + PrimReserve(idx_count, vtx_count); + + // Add indexes for fill + unsigned int vtx_inner_idx = _VtxCurrentIdx; + unsigned int vtx_outer_idx = _VtxCurrentIdx + 1; + + _Data->TempBuffer.reserve_discard((ImTriangulator::EstimateScratchBufferSize(points_count) + sizeof(ImVec2)) / sizeof(ImVec2)); + triangulator.Init(points, points_count, _Data->TempBuffer.Data); + while (triangulator._TrianglesLeft > 0) + { + triangulator.GetNextTriangle(triangle); + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (triangle[0] << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (triangle[1] << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_inner_idx + (triangle[2] << 1)); + _IdxWritePtr += 3; + } + + // Compute normals + _Data->TempBuffer.reserve_discard(points_count); + ImVec2* temp_normals = _Data->TempBuffer.Data; + for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) + { + const ImVec2& p0 = points[i0]; + const ImVec2& p1 = points[i1]; + float dx = p1.x - p0.x; + float dy = p1.y - p0.y; + IM_NORMALIZE2F_OVER_ZERO(dx, dy); + temp_normals[i0].x = dy; + temp_normals[i0].y = -dx; + } + + for (int i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) + { + // Average normals + const ImVec2& n0 = temp_normals[i0]; + const ImVec2& n1 = temp_normals[i1]; + float dm_x = (n0.x + n1.x) * 0.5f; + float dm_y = (n0.y + n1.y) * 0.5f; + IM_FIXNORMAL2F(dm_x, dm_y); + dm_x *= AA_SIZE * 0.5f; + dm_y *= AA_SIZE * 0.5f; + + // Add vertices + _VtxWritePtr[0].pos.x = (points[i1].x - dm_x); _VtxWritePtr[0].pos.y = (points[i1].y - dm_y); _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; // Inner + _VtxWritePtr[1].pos.x = (points[i1].x + dm_x); _VtxWritePtr[1].pos.y = (points[i1].y + dm_y); _VtxWritePtr[1].uv = uv; _VtxWritePtr[1].col = col_trans; // Outer + _VtxWritePtr += 2; + + // Add indexes for fringes + _IdxWritePtr[0] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); _IdxWritePtr[1] = (ImDrawIdx)(vtx_inner_idx + (i0 << 1)); _IdxWritePtr[2] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); + _IdxWritePtr[3] = (ImDrawIdx)(vtx_outer_idx + (i0 << 1)); _IdxWritePtr[4] = (ImDrawIdx)(vtx_outer_idx + (i1 << 1)); _IdxWritePtr[5] = (ImDrawIdx)(vtx_inner_idx + (i1 << 1)); + _IdxWritePtr += 6; + } + _VtxCurrentIdx += (ImDrawIdx)vtx_count; + } + else + { + // Non Anti-aliased Fill + const int idx_count = (points_count - 2) * 3; + const int vtx_count = points_count; + PrimReserve(idx_count, vtx_count); + for (int i = 0; i < vtx_count; i++) + { + _VtxWritePtr[0].pos = points[i]; _VtxWritePtr[0].uv = uv; _VtxWritePtr[0].col = col; + _VtxWritePtr++; + } + _Data->TempBuffer.reserve_discard((ImTriangulator::EstimateScratchBufferSize(points_count) + sizeof(ImVec2)) / sizeof(ImVec2)); + triangulator.Init(points, points_count, _Data->TempBuffer.Data); + while (triangulator._TrianglesLeft > 0) + { + triangulator.GetNextTriangle(triangle); + _IdxWritePtr[0] = (ImDrawIdx)(_VtxCurrentIdx + triangle[0]); _IdxWritePtr[1] = (ImDrawIdx)(_VtxCurrentIdx + triangle[1]); _IdxWritePtr[2] = (ImDrawIdx)(_VtxCurrentIdx + triangle[2]); + _IdxWritePtr += 3; + } + _VtxCurrentIdx += (ImDrawIdx)vtx_count; + } +} //----------------------------------------------------------------------------- // [SECTION] ImDrawListSplitter @@ -2672,8 +2994,8 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) int unscaled_ascent, unscaled_descent, unscaled_line_gap; stbtt_GetFontVMetrics(&src_tmp.FontInfo, &unscaled_ascent, &unscaled_descent, &unscaled_line_gap); - const float ascent = ImTrunc(unscaled_ascent * font_scale + ((unscaled_ascent > 0.0f) ? +1 : -1)); - const float descent = ImTrunc(unscaled_descent * font_scale + ((unscaled_descent > 0.0f) ? +1 : -1)); + const float ascent = ImCeil(unscaled_ascent * font_scale); + const float descent = ImFloor(unscaled_descent * font_scale); ImFontAtlasBuildSetupFont(atlas, dst_font, &cfg, ascent, descent); const float font_off_x = cfg.GlyphOffset.x; const float font_off_y = cfg.GlyphOffset.y + IM_ROUND(dst_font->Ascent); @@ -3768,6 +4090,8 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, const ImVec2& pos, Im { x = start_x; y += line_height; + if (y > clip_rect.w) + break; // break out of main loop word_wrap_eol = NULL; s = CalcWordWrapNextLineStartA(s, text_end); // Wrapping skips upcoming blanks continue; diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index a968fe30..30589162 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -20,6 +20,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-07-08: *BREAKING* Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWWindow* parameter. +// 2024-07-08: Emscripten: Added support for GLFW3 contrib port (GLFW 3.4.0 features + bug fixes): to enable, replace -sUSE_GLFW=3 with --use-port=contrib.glfw3 (requires emscripten 3.1.59+) (https://github.com/pongasoft/emscripten-glfw) +// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions. // 2023-12-19: Emscripten: Added ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to register canvas selector and auto-resize GLFW window. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys. // 2023-07-18: Inputs: Revert ignoring mouse data on GLFW_CURSOR_DISABLED as it can be used differently. User may set ImGuiConfigFLags_NoMouse if desired. (#5625, #6609) @@ -85,17 +88,26 @@ #ifdef _WIN32 #undef APIENTRY +#ifndef GLFW_EXPOSE_NATIVE_WIN32 #define GLFW_EXPOSE_NATIVE_WIN32 +#endif #include // for glfwGetWin32Window() #endif #ifdef __APPLE__ +#ifndef GLFW_EXPOSE_NATIVE_COCOA #define GLFW_EXPOSE_NATIVE_COCOA +#endif #include // for glfwGetCocoaWindow() #endif #ifdef __EMSCRIPTEN__ #include #include +#ifdef EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3 +#include +#else +#define EMSCRIPTEN_USE_EMBEDDED_GLFW3 +#endif #endif // We gather version tests as define in order to easily see which features are version-dependent. @@ -127,7 +139,7 @@ struct ImGui_ImplGlfw_Data ImVec2 LastValidMousePos; bool InstalledCallbacks; bool CallbacksChainForAllWindows; -#ifdef __EMSCRIPTEN__ +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 const char* CanvasSelector; #endif @@ -141,7 +153,7 @@ struct ImGui_ImplGlfw_Data GLFWcharfun PrevUserCallbackChar; GLFWmonitorfun PrevUserCallbackMonitor; #ifdef _WIN32 - WNDPROC GlfwWndProc; + WNDPROC PrevWndProc; #endif ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); } @@ -331,7 +343,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) bd->PrevUserCallbackScroll(window, xoffset, yoffset); -#ifdef __EMSCRIPTEN__ +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 // Ignore GLFW events: will be processed in ImGui_ImplEmscripten_WheelCallback(). return; #endif @@ -342,7 +354,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) { -#if GLFW_HAS_GETKEYNAME && !defined(__EMSCRIPTEN__) +#if GLFW_HAS_GETKEYNAME && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3) // GLFW 3.1+ attempts to "untranslate" keys, which goes the opposite of what every other framework does, making using lettered shortcuts difficult. // (It had reasons to do so: namely GLFW is/was more likely to be used for WASD-type game controls rather than lettered shortcuts, but IHMO the 3.1 change could have been done differently) // See https://github.com/glfw/glfw/issues/1502 for details. @@ -353,7 +365,7 @@ static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); const char* key_name = glfwGetKeyName(key, scancode); glfwSetErrorCallback(prev_error_callback); -#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) +#if GLFW_HAS_GETERROR && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3) // Eat errors (see #5908) (void)glfwGetError(nullptr); #endif if (key_name && key_name[0] != 0 && key_name[1] == 0) @@ -450,7 +462,7 @@ void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int) // Unused in 'master' branch but 'docking' branch will use this, so we declare it ahead of it so if you have to install callbacks you can install this one too. } -#ifdef __EMSCRIPTEN__ +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void*) { // Mimic Emscripten_HandleWheel() in SDL. @@ -493,7 +505,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo()); break; } - return ::CallWindowProcW(bd->GlfwWndProc, hWnd, msg, wParam, lParam); + return ::CallWindowProcW(bd->PrevWndProc, hWnd, msg, wParam, lParam); } #endif @@ -549,9 +561,14 @@ void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows) bd->CallbacksChainForAllWindows = chain_for_all_windows; } +#ifdef __EMSCRIPTEN__ +EM_JS(void, ImGui_ImplGlfw_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); +#endif + static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) { ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); //printf("GLFW_VERSION: %d.%d.%d (%d)", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION, GLFW_VERSION_COMBINED); @@ -568,6 +585,9 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; io.ClipboardUserData = bd->Window; +#ifdef __EMSCRIPTEN__ + io.PlatformOpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplGlfw_EmscriptenOpenURL(url); return true; }; +#endif // Create mouse cursors // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist, @@ -598,15 +618,10 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. if (install_callbacks) ImGui_ImplGlfw_InstallCallbacks(window); - // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096) - // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves. - // FIXME: May break chaining in case user registered their own Emscripten callback? -#ifdef __EMSCRIPTEN__ - emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, ImGui_ImplEmscripten_WheelCallback); -#endif // Set platform dependent data in viewport ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + main_viewport->PlatformHandle = (void*)bd->Window; #ifdef _WIN32 main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window); #elif defined(__APPLE__) @@ -617,8 +632,8 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw // Windows: register a WndProc hook so we can intercept some messages. #ifdef _WIN32 - bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); - IM_ASSERT(bd->GlfwWndProc != nullptr); + bd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); + IM_ASSERT(bd->PrevWndProc != nullptr); ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); #endif @@ -649,18 +664,19 @@ void ImGui_ImplGlfw_Shutdown() if (bd->InstalledCallbacks) ImGui_ImplGlfw_RestoreCallbacks(bd->Window); -#ifdef __EMSCRIPTEN__ - emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, nullptr); +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 + if (bd->CanvasSelector) + emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, nullptr); #endif for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) glfwDestroyCursor(bd->MouseCursors[cursor_n]); - // Windows: register a WndProc hook so we can intercept some messages. + // Windows: restore our WndProc hook #ifdef _WIN32 ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->GlfwWndProc); - bd->GlfwWndProc = nullptr; + ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->PrevWndProc); + bd->PrevWndProc = nullptr; #endif io.BackendPlatformName = nullptr; @@ -677,7 +693,7 @@ static void ImGui_ImplGlfw_UpdateMouseData() // (those braces are here to reduce diff with multi-viewports support in 'docking' branch) { GLFWwindow* window = bd->Window; -#ifdef __EMSCRIPTEN__ +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 const bool is_window_focused = true; #else const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0; @@ -735,7 +751,7 @@ static void ImGui_ImplGlfw_UpdateGamepads() return; io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; -#if GLFW_HAS_GAMEPAD_API && !defined(__EMSCRIPTEN__) +#if GLFW_HAS_GAMEPAD_API && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3) GLFWgamepadstate gamepad; if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad)) return; @@ -783,7 +799,7 @@ void ImGui_ImplGlfw_NewFrame() { ImGuiIO& io = ImGui::GetIO(); ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplGlfw_InitForXXX()?"); // Setup display size (every frame to accommodate for window resizing) int w, h; @@ -809,7 +825,7 @@ void ImGui_ImplGlfw_NewFrame() ImGui_ImplGlfw_UpdateGamepads(); } -#ifdef __EMSCRIPTEN__ +#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data) { ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data; @@ -830,11 +846,11 @@ static EM_BOOL ImGui_ImplEmscripten_FullscreenChangeCallback(int event_type, con // 'canvas_selector' is a CSS selector. The event listener is applied to the first element that matches the query. // STRING MUST PERSIST FOR THE APPLICATION DURATION. PLEASE USE A STRING LITERAL OR ENSURE POINTER WILL STAY VALID. -void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) +void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow*, const char* canvas_selector) { IM_ASSERT(canvas_selector != nullptr); ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplGlfw_InitForXXX()?"); bd->CanvasSelector = canvas_selector; emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, bd, false, ImGui_ImplGlfw_OnCanvasSizeChange); @@ -842,8 +858,24 @@ void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_sel // Change the size of the GLFW window according to the size of the canvas ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd); + + // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096) + // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves. + // FIXME: May break chaining in case user registered their own Emscripten callback? + emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, ImGui_ImplEmscripten_WheelCallback); } -#endif +#elif defined(EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3) +// When using --use-port=contrib.glfw3 for the GLFW implementation, you can override the behavior of this call +// by invoking emscripten_glfw_make_canvas_resizable afterward. +// See https://github.com/pongasoft/emscripten-glfw/blob/master/docs/Usage.md#how-to-make-the-canvas-resizable-by-the-user for an explanation +void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow* window, const char* canvas_selector) +{ + GLFWwindow* w = (GLFWwindow*)(EM_ASM_INT({ return Module.glfwGetWindow(UTF8ToString($0)); }, canvas_selector)); + IM_ASSERT(window == w); // Sanity check + IM_UNUSED(w); + emscripten_glfw_make_canvas_resizable(window, "window", nullptr); +} +#endif // #ifdef EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3 //----------------------------------------------------------------------------- diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index b3a510bc..7087a7c5 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -22,6 +22,9 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-06-28: OpenGL: ImGui_ImplOpenGL3_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL3_DestroyFontsTexture(). (#7748) +// 2024-05-07: OpenGL: Update loader for Linux to support EGL/GLVND. (#7562) +// 2024-04-16: OpenGL: Detect ES3 contexts on desktop based on version string, to e.g. avoid calling glPolygonMode() on them. (#7447) // 2024-01-09: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" and variants, fixing regression on distros missing a symlink. // 2023-11-08: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" instead of "libGL.so.1", accommodating for NetBSD systems having only "libGL.so.3" available. (#6983) // 2023-10-05: OpenGL: Rename symbols in our internal loader so that LTO compilation with another copy of gl3w is possible. (#6875, #6668, #4445) @@ -121,6 +124,7 @@ // Clang/GCC warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: ignore unknown flags #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness #pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used @@ -177,9 +181,10 @@ #endif // Desktop GL 2.0+ has extension and glPolygonMode() which GL ES and WebGL don't have.. +// A desktop ES context can technically compile fine with our loader, so we also perform a runtime checks #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) #define IMGUI_IMPL_OPENGL_HAS_EXTENSIONS // has glGetIntegerv(GL_NUM_EXTENSIONS) -#define IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE // has glPolygonMode() +#define IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // may have glPolygonMode() #endif // Desktop GL 2.1+ and GL ES 3.0+ have glBindBuffer() with GL_PIXEL_UNPACK_BUFFER target. @@ -230,6 +235,7 @@ struct ImGui_ImplOpenGL3_Data unsigned int VboHandle, ElementsHandle; GLsizeiptr VertexBufferSize; GLsizeiptr IndexBufferSize; + bool HasPolygonMode; bool HasClipOrigin; bool UseBufferSubData; @@ -271,6 +277,7 @@ struct ImGui_ImplOpenGL3_VtxAttribState bool ImGui_ImplOpenGL3_Init(const char* glsl_version) { ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); // Initialize our loader @@ -294,16 +301,13 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) bd->GlProfileIsES2 = true; #else // Desktop or GLES 3 + const char* gl_version_str = (const char*)glGetString(GL_VERSION); GLint major = 0; GLint minor = 0; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); if (major == 0 && minor == 0) - { - // Query GL_VERSION in desktop GL 2.x, the string will start with "." - const char* gl_version = (const char*)glGetString(GL_VERSION); - sscanf(gl_version, "%d.%d", &major, &minor); - } + sscanf(gl_version_str, "%d.%d", &major, &minor); // Query GL_VERSION in desktop GL 2.x, the string will start with "." bd->GlVersion = (GLuint)(major * 100 + minor * 10); #if defined(GL_CONTEXT_PROFILE_MASK) if (bd->GlVersion >= 320) @@ -313,6 +317,9 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) #if defined(IMGUI_IMPL_OPENGL_ES3) bd->GlProfileIsES3 = true; +#else + if (strncmp(gl_version_str, "OpenGL ES 3", 11) == 0) + bd->GlProfileIsES3 = true; #endif bd->UseBufferSubData = false; @@ -327,7 +334,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) #endif #ifdef IMGUI_IMPL_OPENGL_DEBUG - printf("GlVersion = %d\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2 = %d, GlProfileIsES3 = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] + printf("GlVersion = %d, \"%s\"\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2 = %d, GlProfileIsES3 = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, gl_version_str, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] #endif #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET @@ -359,6 +366,9 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); // Detect extensions we support +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + bd->HasPolygonMode = (!bd->GlProfileIsES2 && !bd->GlProfileIsES3); +#endif bd->HasClipOrigin = (bd->GlVersion >= 450); #ifdef IMGUI_IMPL_OPENGL_HAS_EXTENSIONS GLint num_extensions = 0; @@ -390,10 +400,12 @@ void ImGui_ImplOpenGL3_Shutdown() void ImGui_ImplOpenGL3_NewFrame() { ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplOpenGL3_Init()?"); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL3_Init()?"); if (!bd->ShaderHandle) ImGui_ImplOpenGL3_CreateDeviceObjects(); + if (!bd->FontTexture) + ImGui_ImplOpenGL3_CreateFontsTexture(); } static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) @@ -412,8 +424,9 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid if (bd->GlVersion >= 310) glDisable(GL_PRIMITIVE_RESTART); #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + if (bd->HasPolygonMode) + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) @@ -501,8 +514,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE - GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + GLint last_polygon_mode[2]; if (bd->HasPolygonMode) { glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); } #endif GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); @@ -640,18 +653,10 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons - if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) - { - glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); - glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); - } - else - { - glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); - } -#endif // IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE + if (bd->HasPolygonMode) { if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) { glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); } else { glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); } } +#endif // IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); @@ -749,7 +754,7 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK - GLint last_pixel_unpack_buffer; + GLint last_pixel_unpack_buffer = 0; if (bd->GlVersion >= 210) { glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } #endif #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 260df1a9..37dd7030 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (tables and columns code) /* @@ -24,8 +24,9 @@ Index of this file: */ // Navigating this file: -// - In Visual Studio IDE: CTRL+comma ("Edit.GoToAll") can follow symbols in comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. -// - With Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols in comments. +// - In Visual Studio: CTRL+comma ("Edit.GoToAll") can follow symbols inside comments, whereas CTRL+F12 ("Edit.GoToImplementation") cannot. +// - In Visual Studio w/ Visual Assist installed: ALT+G ("VAssistX.GoToImplementation") can also follow symbols inside comments. +// - In VS Code, CLion, etc.: CTRL+click can follow symbols inside comments. //----------------------------------------------------------------------------- // [SECTION] Commentary @@ -227,6 +228,7 @@ Index of this file: #pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked @@ -318,6 +320,12 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG IM_ASSERT(inner_width >= 0.0f); // If an outer size is specified ahead we will be able to early out when not visible. Exact clipping criteria may evolve. + // FIXME: coarse clipping because access to table data causes two issues: + // - instance numbers varying/unstable. may not be a direct problem for users, but could make outside access broken or confusing, e.g. TestEngine. + // - can't implement support for ImGuiChildFlags_ResizeY as we need to somehow pull the height data from somewhere. this also needs stable instance numbers. + // The side-effects of accessing table data on coarse clip would be: + // - always reserving the pooled ImGuiTable data ahead for a fully clipped table (minor IMHO). Also the 'outer_window_is_measuring_size' criteria may already be defeating this in some situations. + // - always performing the GetOrAddByKey() O(log N) query in g.Tables.Map[]. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0; const ImVec2 avail_size = GetContentRegionAvail(); const ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); @@ -326,6 +334,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (use_child_window && IsClippedEx(outer_rect, 0) && !outer_window_is_measuring_size) { ItemSize(outer_rect); + ItemAdd(outer_rect, id); return false; } @@ -335,7 +344,6 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // Acquire storage for the table ImGuiTable* table = g.Tables.GetOrAddByKey(id); - const ImGuiTableFlags table_last_flags = table->Flags; // Acquire temporary buffers const int table_idx = g.Tables.GetIndex(table); @@ -353,6 +361,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // Initialize const int previous_frame_active = table->LastFrameActive; const int instance_no = (previous_frame_active != g.FrameCount) ? 0 : table->InstanceCurrent + 1; + const ImGuiTableFlags previous_flags = table->Flags; table->ID = id; table->Flags = flags; table->LastFrameActive = g.FrameCount; @@ -399,12 +408,12 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG SetNextWindowContentSize(ImVec2(override_content_size.x != FLT_MAX ? override_content_size.x : 0.0f, override_content_size.y != FLT_MAX ? override_content_size.y : 0.0f)); // Reset scroll if we are reactivating it - if ((table_last_flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) == 0) + if ((previous_flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) == 0) SetNextWindowScroll(ImVec2(0.0f, 0.0f)); // Create scrolling region (without border and zero window padding) - ImGuiWindowFlags child_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None; - BeginChildEx(name, instance_id, outer_rect.GetSize(), false, child_flags); + ImGuiWindowFlags child_window_flags = (flags & ImGuiTableFlags_ScrollX) ? ImGuiWindowFlags_HorizontalScrollbar : ImGuiWindowFlags_None; + BeginChildEx(name, instance_id, outer_rect.GetSize(), ImGuiChildFlags_None, child_window_flags); table->InnerWindow = g.CurrentWindow; table->WorkRect = table->InnerWindow->WorkRect; table->OuterRect = table->InnerWindow->Rect(); @@ -428,6 +437,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // For non-scrolling tables, WorkRect == OuterRect == InnerRect. // But at this point we do NOT have a correct value for .Max.y (unless a height has been explicitly passed in). It will only be updated in EndTable(). table->WorkRect = table->OuterRect = table->InnerRect = outer_rect; + table->HasScrollbarYPrev = table->HasScrollbarYCurr = false; } // Push a standardized ID for both child-using and not-child-using tables @@ -498,6 +508,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->DeclColumnsCount = table->AngledHeadersCount = 0; if (previous_frame_active + 1 < g.FrameCount) table->IsActiveIdInTable = false; + table->AngledHeadersHeight = 0.0f; temp_data->AngledHeadersExtraWidth = 0.0f; // Using opaque colors facilitate overlapping lines of the grid, otherwise we'd need to improve TableDrawBorders() @@ -511,7 +522,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG if (inner_window != outer_window) // So EndChild() within the inner window can restore the table properly. inner_window->DC.CurrentTableIdx = table_idx; - if ((table_last_flags & ImGuiTableFlags_Reorderable) && (flags & ImGuiTableFlags_Reorderable) == 0) + if ((previous_flags & ImGuiTableFlags_Reorderable) && (flags & ImGuiTableFlags_Reorderable) == 0) table->IsResetDisplayOrderRequest = true; // Mark as used to avoid GC @@ -1066,6 +1077,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // - ClipRect.Max.x: using WorkMaxX instead of MaxX (aka including padding) makes things more consistent when resizing down, tho slightly detrimental to visibility in very-small column. // - ClipRect.Max.x: using MaxX makes it easier for header to receive hover highlight with no discontinuity and display sorting arrow. // - FIXME-TABLE: We want equal width columns to have equal (ClipRect.Max.x - WorkMinX) width, which means ClipRect.max.x cannot stray off host_clip_rect.Max.x else right-most column may appear shorter. + const float previous_instance_work_min_x = column->WorkMinX; column->WorkMinX = column->MinX + table->CellPaddingX + table->CellSpacingX1; column->WorkMaxX = column->MaxX - table->CellPaddingX - table->CellSpacingX2; // Expected max column->ItemWidth = ImTrunc(column->WidthGiven * 0.65f); @@ -1118,8 +1130,22 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // column->WorkMinX = ImLerp(column->WorkMinX, ImMax(column->StartX, column->MaxX - column->ContentWidthRowsUnfrozen), 0.5f); // Reset content width variables - column->ContentMaxXFrozen = column->ContentMaxXUnfrozen = column->WorkMinX; - column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX; + if (table->InstanceCurrent == 0) + { + column->ContentMaxXFrozen = column->WorkMinX; + column->ContentMaxXUnfrozen = column->WorkMinX; + column->ContentMaxXHeadersUsed = column->WorkMinX; + column->ContentMaxXHeadersIdeal = column->WorkMinX; + } + else + { + // As we store an absolute value to make per-cell updates faster, we need to offset values used for width computation. + const float offset_from_previous_instance = column->WorkMinX - previous_instance_work_min_x; + column->ContentMaxXFrozen += offset_from_previous_instance; + column->ContentMaxXUnfrozen += offset_from_previous_instance; + column->ContentMaxXHeadersUsed += offset_from_previous_instance; + column->ContentMaxXHeadersIdeal += offset_from_previous_instance; + } // Don't decrement auto-fit counters until container window got a chance to submit its items if (table->HostSkipItems == false) @@ -1238,9 +1264,9 @@ void ImGui::TableUpdateBorders(ImGuiTable* table) // really problematic (whereas the actual visual will be displayed in EndTable() and using the current frame height). // Actual columns highlight/render will be performed in EndTable() and not be affected. ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); - const float hit_half_width = TABLE_RESIZE_SEPARATOR_HALF_THICKNESS; + const float hit_half_width = ImTrunc(TABLE_RESIZE_SEPARATOR_HALF_THICKNESS * g.CurrentDpiScale); const float hit_y1 = (table->FreezeRowsCount >= 1 ? table->OuterRect.Min.y : table->WorkRect.Min.y) + table->AngledHeadersHeight; - const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table_instance->LastOuterHeight); + const float hit_y2_body = ImMax(table->OuterRect.Max.y, hit_y1 + table_instance->LastOuterHeight - table->AngledHeadersHeight); const float hit_y2_head = hit_y1 + table_instance->LastTopHeadersRowHeight; for (int order_n = 0; order_n < table->ColumnsCount; order_n++) @@ -1415,7 +1441,7 @@ void ImGui::EndTable() if (table->ResizedColumn != -1 && table->InstanceCurrent == table->InstanceInteracted) { ImGuiTableColumn* column = &table->Columns[table->ResizedColumn]; - const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + TABLE_RESIZE_SEPARATOR_HALF_THICKNESS); + const float new_x2 = (g.IO.MousePos.x - g.ActiveIdClickOffset.x + ImTrunc(TABLE_RESIZE_SEPARATOR_HALF_THICKNESS * g.CurrentDpiScale)); const float new_width = ImTrunc(new_x2 - column->MinX - table->CellSpacingX1 - table->CellPaddingX * 2.0f); table->ResizedColumnNextWidth = new_width; } @@ -1444,7 +1470,10 @@ void ImGui::EndTable() // CursorPosPrevLine and CursorMaxPos manually. That should be a more general layout feature, see same problem e.g. #3414) if (inner_window != outer_window) { + short backup_nav_layers_active_mask = inner_window->DC.NavLayersActiveMask; + inner_window->DC.NavLayersActiveMask |= 1 << ImGuiNavLayer_Main; // So empty table don't appear to navigate differently. EndChild(); + inner_window->DC.NavLayersActiveMask = backup_nav_layers_active_mask; } else { @@ -1462,9 +1491,13 @@ void ImGui::EndTable() } else if (temp_data->UserOuterSize.x <= 0.0f) { - const float decoration_size = table->TempData->AngledHeadersExtraWidth + ((table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.x : 0.0f); - outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth + decoration_size - temp_data->UserOuterSize.x); - outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, table->OuterRect.Min.x + table->ColumnsAutoFitWidth)); + // Some references for this: #7651 + tests "table_reported_size", "table_reported_size_outer" equivalent Y block + // - Checking for ImGuiTableFlags_ScrollX/ScrollY flag makes us a frame ahead when disabling those flags. + // - FIXME-TABLE: Would make sense to pre-compute expected scrollbar visibility/sizes to generally save a frame of feedback. + const float inner_content_max_x = table->OuterRect.Min.x + table->ColumnsAutoFitWidth; // Slightly misleading name but used for code symmetry with inner_content_max_y + const float decoration_size = table->TempData->AngledHeadersExtraWidth + ((table->Flags & ImGuiTableFlags_ScrollY) ? inner_window->ScrollbarSizes.x : 0.0f); + outer_window->DC.IdealMaxPos.x = ImMax(outer_window->DC.IdealMaxPos.x, inner_content_max_x + decoration_size - temp_data->UserOuterSize.x); + outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos.x, ImMin(table->OuterRect.Max.x, inner_content_max_x + decoration_size)); } else { @@ -1472,9 +1505,9 @@ void ImGui::EndTable() } if (temp_data->UserOuterSize.y <= 0.0f) { - const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollY) ? inner_window->ScrollbarSizes.y : 0.0f; + const float decoration_size = (table->Flags & ImGuiTableFlags_ScrollX) ? inner_window->ScrollbarSizes.y : 0.0f; outer_window->DC.IdealMaxPos.y = ImMax(outer_window->DC.IdealMaxPos.y, inner_content_max_y + decoration_size - temp_data->UserOuterSize.y); - outer_window->DC.CursorMaxPos.y = ImMax(backup_outer_max_pos.y, ImMin(table->OuterRect.Max.y, inner_content_max_y)); + outer_window->DC.CursorMaxPos.y = ImMax(backup_outer_max_pos.y, ImMin(table->OuterRect.Max.y, inner_content_max_y + decoration_size)); } else { @@ -1890,7 +1923,7 @@ void ImGui::TableEndRow(ImGuiTable* table) if (is_visible) { // Update data for TableGetHoveredRow() - if (table->HoveredColumnBody != -1 && g.IO.MousePos.y >= bg_y1 && g.IO.MousePos.y < bg_y2) + if (table->HoveredColumnBody != -1 && g.IO.MousePos.y >= bg_y1 && g.IO.MousePos.y < bg_y2 && table_instance->HoveredRowNext < 0) table_instance->HoveredRowNext = table->CurrentRow; // Decide of background color for the row @@ -1945,7 +1978,8 @@ void ImGui::TableEndRow(ImGuiTable* table) cell_bg_rect.ClipWith(table->BgClipRect); cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped when scrolling cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->MaxX); - window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor); + if (cell_bg_rect.Min.y < cell_bg_rect.Max.y) + window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor); } } @@ -1962,34 +1996,37 @@ void ImGui::TableEndRow(ImGuiTable* table) // We need to do that in TableEndRow() instead of TableBeginRow() so the list clipper can mark end of row and // get the new cursor position. if (unfreeze_rows_request) + { for (int column_n = 0; column_n < table->ColumnsCount; column_n++) table->Columns[column_n].NavLayerCurrent = ImGuiNavLayer_Main; - if (unfreeze_rows_actual) - { - IM_ASSERT(table->IsUnfrozenRows == false); const float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); - table->IsUnfrozenRows = true; table_instance->LastFrozenHeight = y0 - table->OuterRect.Min.y; - // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect - table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); - table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y; - table->Bg2DrawChannelCurrent = table->Bg2DrawChannelUnfrozen; - IM_ASSERT(table->Bg2ClipRectForDrawCmd.Min.y <= table->Bg2ClipRectForDrawCmd.Max.y); - - float row_height = table->RowPosY2 - table->RowPosY1; - table->RowPosY2 = window->DC.CursorPos.y = table->WorkRect.Min.y + table->RowPosY2 - table->OuterRect.Min.y; - table->RowPosY1 = table->RowPosY2 - row_height; - for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + if (unfreeze_rows_actual) { - ImGuiTableColumn* column = &table->Columns[column_n]; - column->DrawChannelCurrent = column->DrawChannelUnfrozen; - column->ClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y; - } + IM_ASSERT(table->IsUnfrozenRows == false); + table->IsUnfrozenRows = true; + + // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect + table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); + table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y; + table->Bg2DrawChannelCurrent = table->Bg2DrawChannelUnfrozen; + IM_ASSERT(table->Bg2ClipRectForDrawCmd.Min.y <= table->Bg2ClipRectForDrawCmd.Max.y); + + float row_height = table->RowPosY2 - table->RowPosY1; + table->RowPosY2 = window->DC.CursorPos.y = table->WorkRect.Min.y + table->RowPosY2 - table->OuterRect.Min.y; + table->RowPosY1 = table->RowPosY2 - row_height; + for (int column_n = 0; column_n < table->ColumnsCount; column_n++) + { + ImGuiTableColumn* column = &table->Columns[column_n]; + column->DrawChannelCurrent = column->DrawChannelUnfrozen; + column->ClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y; + } - // Update cliprect ahead of TableBeginCell() so clipper can access to new ClipRect->Min.y - SetWindowClipRectBeforeSetChannel(window, table->Columns[0].ClipRect); - table->DrawSplitter->SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent); + // Update cliprect ahead of TableBeginCell() so clipper can access to new ClipRect->Min.y + SetWindowClipRectBeforeSetChannel(window, table->Columns[0].ClipRect); + table->DrawSplitter->SetCurrentChannel(window->DrawList, table->Columns[0].DrawChannelCurrent); + } } if (!(table->RowFlags & ImGuiTableRowFlags_Headers)) @@ -2766,7 +2803,7 @@ ImGuiTableSortSpecs* ImGui::TableGetSortSpecs() static inline ImGuiSortDirection TableGetColumnAvailSortDirection(ImGuiTableColumn* column, int n) { IM_ASSERT(n < column->SortDirectionsAvailCount); - return (column->SortDirectionsAvailList >> (n << 1)) & 0x03; + return (ImGuiSortDirection)((column->SortDirectionsAvailList >> (n << 1)) & 0x03); } // Fix sort direction if currently set on a value which is unavailable (e.g. activating NoSortAscending/NoSortDescending) @@ -2907,6 +2944,7 @@ void ImGui::TableSortSpecsBuild(ImGuiTable* table) } // Write output + // May be able to move all SortSpecs data from table (48 bytes) to ImGuiTableTempData if we decide to write it back on every BeginTable() ImGuiTableColumnSortSpecs* sort_specs = (table->SortSpecsCount == 0) ? NULL : (table->SortSpecsCount == 1) ? &table->SortSpecsSingle : table->SortSpecsMulti.Data; if (dirty && sort_specs != NULL) for (int column_n = 0; column_n < table->ColumnsCount; column_n++) @@ -2919,7 +2957,7 @@ void ImGui::TableSortSpecsBuild(ImGuiTable* table) sort_spec->ColumnUserID = column->UserID; sort_spec->ColumnIndex = (ImGuiTableColumnIdx)column_n; sort_spec->SortOrder = (ImGuiTableColumnIdx)column->SortOrder; - sort_spec->SortDirection = column->SortDirection; + sort_spec->SortDirection = (ImGuiSortDirection)column->SortDirection; } table->SortSpecs.Specs = sort_specs; @@ -2967,7 +3005,7 @@ float ImGui::TableGetHeaderAngledMaxLabelWidth() // [Public] This is a helper to output TableHeader() calls based on the column names declared in TableSetupColumn(). // The intent is that advanced users willing to create customized headers would not need to use this helper -// and can create their own! For example: TableHeader() may be preceeded by Checkbox() or other custom widgets. +// and can create their own! For example: TableHeader() may be preceded by Checkbox() or other custom widgets. // See 'Demo->Tables->Custom headers' for a demonstration of implementing a custom version of this. // This code is constructed to not make much use of internal functions, as it is intended to be a template to copy. // FIXME-TABLE: TableOpenContextMenu() and TableGetHeaderRowHeight() are not public. @@ -3153,15 +3191,43 @@ void ImGui::TableHeader(const char* label) } // Unlike TableHeadersRow() it is not expected that you can reimplement or customize this with custom widgets. -// FIXME: highlight without ImGuiTableFlags_HighlightHoveredColumn // FIXME: No hit-testing/button on the angled header. void ImGui::TableAngledHeadersRow() { ImGuiContext& g = *GImGui; - TableAngledHeadersRowEx(g.Style.TableAngledHeadersAngle, 0.0f); + ImGuiTable* table = g.CurrentTable; + ImGuiTableTempData* temp_data = table->TempData; + temp_data->AngledHeadersRequests.resize(0); + temp_data->AngledHeadersRequests.reserve(table->ColumnsEnabledCount); + + // Which column needs highlight? + const ImGuiID row_id = GetID("##AngledHeaders"); + ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); + int highlight_column_n = table->HighlightColumnHeader; + if (highlight_column_n == -1 && table->HoveredColumnBody != -1) + if (table_instance->HoveredRowLast == 0 && table->HoveredColumnBorder == -1 && (g.ActiveId == 0 || g.ActiveId == row_id || (table->IsActiveIdInTable || g.DragDropActive))) + highlight_column_n = table->HoveredColumnBody; + + // Build up request + ImU32 col_header_bg = GetColorU32(ImGuiCol_TableHeaderBg); + ImU32 col_text = GetColorU32(ImGuiCol_Text); + for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + if (IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) + { + const int column_n = table->DisplayOrderToIndex[order_n]; + ImGuiTableColumn* column = &table->Columns[column_n]; + if ((column->Flags & ImGuiTableColumnFlags_AngledHeader) == 0) // Note: can't rely on ImGuiTableColumnFlags_IsVisible test here. + continue; + ImGuiTableHeaderData request = { (ImGuiTableColumnIdx)column_n, col_text, col_header_bg, (column_n == highlight_column_n) ? GetColorU32(ImGuiCol_Header) : 0 }; + temp_data->AngledHeadersRequests.push_back(request); + } + + // Render row + TableAngledHeadersRowEx(row_id, g.Style.TableAngledHeadersAngle, 0.0f, temp_data->AngledHeadersRequests.Data, temp_data->AngledHeadersRequests.Size); } -void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) +// Important: data must be fed left to right +void ImGui::TableAngledHeadersRowEx(ImGuiID row_id, float angle, float max_label_width, const ImGuiTableHeaderData* data, int data_count) { ImGuiContext& g = *GImGui; ImGuiTable* table = g.CurrentTable; @@ -3185,7 +3251,7 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) // Calculate our base metrics and set angled headers data _before_ the first call to TableNextRow() // FIXME-STYLE: Would it be better for user to submit 'max_label_width' or 'row_height' ? One can be derived from the other. const float header_height = g.FontSize + g.Style.CellPadding.x * 2.0f; - const float row_height = ImFabs(ImRotate(ImVec2(max_label_width, flip_label ? +header_height : -header_height), cos_a, sin_a).y); + const float row_height = ImTrunc(ImFabs(ImRotate(ImVec2(max_label_width, flip_label ? +header_height : -header_height), cos_a, sin_a).y)); table->AngledHeadersHeight = row_height; table->AngledHeadersSlope = (sin_a != 0.0f) ? (cos_a / sin_a) : 0.0f; const ImVec2 header_angled_vector = unit_right * (row_height / -sin_a); // vector from bottom-left to top-left, and from bottom-right to top-right @@ -3203,28 +3269,22 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) draw_list->AddRectFilled(ImVec2(table->BgClipRect.Min.x, row_r.Min.y), ImVec2(table->BgClipRect.Max.x, row_r.Max.y), GetColorU32(ImGuiCol_TableHeaderBg, 0.25f)); // FIXME-STYLE: Change row background with an arbitrary color. PushClipRect(ImVec2(clip_rect_min_x, table->BgClipRect.Min.y), table->BgClipRect.Max, true); // Span all columns - const ImGuiID row_id = GetID("##AngledHeaders"); ButtonBehavior(row_r, row_id, NULL, NULL); KeepAliveID(row_id); - ImGuiTableInstanceData* table_instance = TableGetInstanceData(table, table->InstanceCurrent); - int highlight_column_n = table->HighlightColumnHeader; - if (highlight_column_n == -1 && table->HoveredColumnBody != -1) - if (table_instance->HoveredRowLast == 0 && table->HoveredColumnBorder == -1 && (g.ActiveId == 0 || g.ActiveId == row_id || (table->IsActiveIdInTable || g.DragDropActive))) - highlight_column_n = table->HoveredColumnBody; + const float ascent_scaled = g.Font->Ascent * g.FontScale; // FIXME: Standardize those scaling factors better + const float line_off_for_ascent_x = (ImMax((g.FontSize - ascent_scaled) * 0.5f, 0.0f) / -sin_a) * (flip_label ? -1.0f : 1.0f); + const ImVec2 padding = g.Style.CellPadding; // We will always use swapped component + const ImVec2 align = g.Style.TableAngledHeadersTextAlign; // Draw background and labels in first pass, then all borders. float max_x = 0.0f; - ImVec2 padding = g.Style.CellPadding; // We will always use swapped component for (int pass = 0; pass < 2; pass++) - for (int order_n = 0; order_n < table->ColumnsCount; order_n++) + for (int order_n = 0; order_n < data_count; order_n++) { - if (!IM_BITARRAY_TESTBIT(table->EnabledMaskByDisplayOrder, order_n)) - continue; - const int column_n = table->DisplayOrderToIndex[order_n]; + const ImGuiTableHeaderData* request = &data[order_n]; + const int column_n = request->Index; ImGuiTableColumn* column = &table->Columns[column_n]; - if ((column->Flags & ImGuiTableColumnFlags_AngledHeader) == 0) // Note: can't rely on ImGuiTableColumnFlags_IsVisible test here. - continue; ImVec2 bg_shape[4]; bg_shape[0] = ImVec2(column->MaxX, row_r.Max.y); @@ -3234,9 +3294,8 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) if (pass == 0) { // Draw shape - draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_TableHeaderBg)); - if (column_n == highlight_column_n) - draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], GetColorU32(ImGuiCol_Header)); // Highlight on hover + draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], request->BgColor0); + draw_list->AddQuadFilled(bg_shape[0], bg_shape[1], bg_shape[2], bg_shape[3], request->BgColor1); // Optional highlight max_x = ImMax(max_x, bg_shape[3].x); // Draw label @@ -3244,8 +3303,17 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) // - Handle multiple lines manually, as we want each lines to follow on the horizontal border, rather than see a whole block rotated. const char* label_name = TableGetColumnName(table, column_n); const char* label_name_end = FindRenderedTextEnd(label_name); - const float line_off_step_x = g.FontSize / -sin_a; - float line_off_curr_x = 0.0f; + const float line_off_step_x = (g.FontSize / -sin_a); + const int label_lines = ImTextCountLines(label_name, label_name_end); + + // Left<>Right alignment + float line_off_curr_x = flip_label ? (label_lines - 1) * line_off_step_x : 0.0f; + float line_off_for_align_x = ImMax((((column->MaxX - column->MinX) - padding.x * 2.0f) - (label_lines * line_off_step_x)), 0.0f) * align.x; + line_off_curr_x += line_off_for_align_x - line_off_for_ascent_x; + + // Register header width + column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX + ImCeil(label_lines * line_off_step_x - line_off_for_align_x); + while (label_name < label_name_end) { const char* label_name_eol = strchr(label_name, '\n'); @@ -3254,26 +3322,30 @@ void ImGui::TableAngledHeadersRowEx(float angle, float max_label_width) // FIXME: Individual line clipping for right-most column is broken for negative angles. ImVec2 label_size = CalcTextSize(label_name, label_name_eol); - float clip_width = max_label_width - padding.y; // Using padding.y*2.0f would be symetrical but hide more text. + float clip_width = max_label_width - padding.y; // Using padding.y*2.0f would be symmetrical but hide more text. float clip_height = ImMin(label_size.y, column->ClipRect.Max.x - column->WorkMinX - line_off_curr_x); ImRect clip_r(window->ClipRect.Min, window->ClipRect.Min + ImVec2(clip_width, clip_height)); int vtx_idx_begin = draw_list->_VtxCurrentIdx; + PushStyleColor(ImGuiCol_Text, request->TextColor); RenderTextEllipsis(draw_list, clip_r.Min, clip_r.Max, clip_r.Max.x, clip_r.Max.x, label_name, label_name_eol, &label_size); + PopStyleColor(); int vtx_idx_end = draw_list->_VtxCurrentIdx; + // Up<>Down alignment + const float available_space = ImMax(clip_width - label_size.x + ImAbs(padding.x * cos_a) * 2.0f - ImAbs(padding.y * sin_a) * 2.0f, 0.0f); + const float vertical_offset = available_space * align.y * (flip_label ? -1.0f : 1.0f); + // Rotate and offset label - ImVec2 pivot_in = ImVec2(window->ClipRect.Min.x, window->ClipRect.Min.y + label_size.y); + ImVec2 pivot_in = ImVec2(window->ClipRect.Min.x - vertical_offset, window->ClipRect.Min.y + label_size.y); ImVec2 pivot_out = ImVec2(column->WorkMinX, row_r.Max.y); - line_off_curr_x += line_off_step_x; + line_off_curr_x += flip_label ? -line_off_step_x : line_off_step_x; pivot_out += unit_right * padding.y; if (flip_label) pivot_out += unit_right * (clip_width - ImMax(0.0f, clip_width - label_size.x)); - pivot_out.x += flip_label ? line_off_curr_x - line_off_step_x : line_off_curr_x; + pivot_out.x += flip_label ? line_off_curr_x + line_off_step_x : line_off_curr_x; ShadeVertsTransformPos(draw_list, vtx_idx_begin, vtx_idx_end, pivot_in, label_cos_a, label_sin_a, pivot_out); // Rotate and offset - //if (g.IO.KeyShift) { ImDrawList* fg_dl = GetForegroundDrawList(); vtx_idx_begin = fg_dl->_VtxCurrentIdx; fg_dl->AddRect(clip_r.Min, clip_r.Max, IM_COL32(0, 255, 0, 255), 0.0f, 0, 2.0f); ShadeVertsTransformPos(fg_dl, vtx_idx_begin, fg_dl->_VtxCurrentIdx, pivot_in, label_cos_a, label_sin_a, pivot_out); } + //if (g.IO.KeyShift) { ImDrawList* fg_dl = GetForegroundDrawList(); vtx_idx_begin = fg_dl->_VtxCurrentIdx; fg_dl->AddRect(clip_r.Min, clip_r.Max, IM_COL32(0, 255, 0, 255), 0.0f, 0, 1.0f); ShadeVertsTransformPos(fg_dl, vtx_idx_begin, fg_dl->_VtxCurrentIdx, pivot_in, label_cos_a, label_sin_a, pivot_out); } - // Register header width - column->ContentMaxXHeadersUsed = column->ContentMaxXHeadersIdeal = column->WorkMinX + ImCeil(line_off_curr_x); label_name = label_name_eol + 1; } } @@ -3402,7 +3474,7 @@ void ImGui::TableDrawDefaultContextMenu(ImGuiTable* table, ImGuiTableFlags flags Separator(); want_separator = true; - PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true); + PushItemFlag(ImGuiItemFlags_AutoClosePopups, false); for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++) { ImGuiTableColumn* other_column = &table->Columns[other_column_n]; @@ -3988,7 +4060,7 @@ float ImGui::GetColumnNormFromOffset(const ImGuiOldColumns* columns, float offse return offset / (columns->OffMaxX - columns->OffMinX); } -static const float COLUMNS_HIT_RECT_HALF_WIDTH = 4.0f; +static const float COLUMNS_HIT_RECT_HALF_THICKNESS = 4.0f; static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index) { @@ -3999,7 +4071,7 @@ static float GetDraggedColumnOffset(ImGuiOldColumns* columns, int column_index) IM_ASSERT(column_index > 0); // We are not supposed to drag column 0. IM_ASSERT(g.ActiveId == columns->ID + ImGuiID(column_index)); - float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + COLUMNS_HIT_RECT_HALF_WIDTH - window->Pos.x; + float x = g.IO.MousePos.x - g.ActiveIdClickOffset.x + ImTrunc(COLUMNS_HIT_RECT_HALF_THICKNESS * g.CurrentDpiScale) - window->Pos.x; x = ImMax(x, ImGui::GetColumnOffset(column_index - 1) + g.Style.ColumnsMinSpacing); if ((columns->Flags & ImGuiOldColumnFlags_NoPreserveWidths)) x = ImMin(x, ImGui::GetColumnOffset(column_index + 1) - g.Style.ColumnsMinSpacing); @@ -4314,7 +4386,7 @@ void ImGui::EndColumns() ImGuiOldColumnData* column = &columns->Columns[n]; float x = window->Pos.x + GetColumnOffset(n); const ImGuiID column_id = columns->ID + ImGuiID(n); - const float column_hit_hw = COLUMNS_HIT_RECT_HALF_WIDTH; + const float column_hit_hw = ImTrunc(COLUMNS_HIT_RECT_HALF_THICKNESS * g.CurrentDpiScale); const ImRect column_hit_rect(ImVec2(x - column_hit_hw, y1), ImVec2(x + column_hit_hw, y2)); if (!ItemAdd(column_hit_rect, column_id, NULL, ImGuiItemFlags_NoNav)) continue; diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index 734950d7..2aa24efe 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.90.4 +// dear imgui, v1.91.0 // (widgets code) /* @@ -19,7 +19,9 @@ Index of this file: // [SECTION] Widgets: TreeNode, CollapsingHeader, etc. // [SECTION] Widgets: Selectable // [SECTION] Widgets: Typing-Select support +// [SECTION] Widgets: Box-Select support // [SECTION] Widgets: Multi-Select support +// [SECTION] Widgets: Multi-Select helpers // [SECTION] Widgets: ListBox // [SECTION] Widgets: PlotLines, PlotHistogram // [SECTION] Widgets: Value helpers @@ -70,11 +72,13 @@ Index of this file: #pragma clang diagnostic ignored "-Wfloat-equal" // warning: comparing floating point with == or != is unsafe // storing and comparing against same constants (typically 0.0f) is ok. #pragma clang diagnostic ignored "-Wformat-nonliteral" // warning: format string is not a string literal // passing non-literal to vsnformat(). yes, user passing incorrect format strings can crash the code. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness +#pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used // we define snprintf/vsnprintf on Windows so they are available, but not always used. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" // warning: zero as null pointer constant // some standard header variations use #define NULL 0 #pragma clang diagnostic ignored "-Wdouble-promotion" // warning: implicit conversion from 'float' to 'double' when passing argument to function // using printf() is a misery with this as C++ va_arg ellipsis changes float to double. #pragma clang diagnostic ignored "-Wenum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') #pragma clang diagnostic ignored "-Wdeprecated-enum-enum-conversion"// warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#pragma clang diagnostic ignored "-Wunsafe-buffer-usage" // warning: 'xxx' is an unsafe pointer used for buffer access #elif defined(__GNUC__) #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wformat-nonliteral" // warning: format not a string literal, format string not checked @@ -122,9 +126,9 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); //------------------------------------------------------------------------- // For InputTextEx() -static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source); -static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); -static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); +static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, bool input_source_is_clipboard = false); +static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); +static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); //------------------------------------------------------------------------- // [SECTION] Widgets: Text, etc. @@ -421,6 +425,7 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // - RadioButton() // - ProgressBar() // - Bullet() +// - Hyperlink() //------------------------------------------------------------------------- // The ButtonBehavior() function is key to many interactions and used by many/most widgets. @@ -487,7 +492,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Default only reacts to left mouse button if ((flags & ImGuiButtonFlags_MouseButtonMask_) == 0) - flags |= ImGuiButtonFlags_MouseButtonDefault_; + flags |= ImGuiButtonFlags_MouseButtonLeft; // Default behavior requires click + release inside bounding box if ((flags & ImGuiButtonFlags_PressedOnMask_) == 0) @@ -508,7 +513,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool #ifdef IMGUI_ENABLE_TEST_ENGINE // Alternate registration spot, for when caller didn't use ItemAdd() - if (id != 0 && g.LastItemData.ID != id) + if (g.LastItemData.ID != id) IMGUI_TEST_ENGINE_ITEM_ADD(id, bb, NULL); #endif @@ -536,6 +541,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool const ImGuiID test_owner_id = (flags & ImGuiButtonFlags_NoTestKeyOwner) ? ImGuiKeyOwner_Any : id; if (hovered) { + IM_ASSERT(id != 0); // Lazily check inside rare path. + // Poll mouse buttons // - 'mouse_button_clicked' is generally carried into ActiveIdMouseButton when setting ActiveId. // - Technically we only need some values in one code path, but since this is gated by hovered test this is fine. @@ -544,7 +551,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool for (int button = 0; button < 3; button++) if (flags & (ImGuiButtonFlags_MouseButtonLeft << button)) // Handle ImGuiButtonFlags_MouseButtonRight and ImGuiButtonFlags_MouseButtonMiddle here. { - if (IsMouseClicked(button, test_owner_id) && mouse_button_clicked == -1) { mouse_button_clicked = button; } + if (IsMouseClicked(button, ImGuiInputFlags_None, test_owner_id) && mouse_button_clicked == -1) { mouse_button_clicked = button; } if (IsMouseReleased(button, test_owner_id) && mouse_button_released == -1) { mouse_button_released = button; } } @@ -560,8 +567,14 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool SetActiveID(id, window); g.ActiveIdMouseButton = mouse_button_clicked; if (!(flags & ImGuiButtonFlags_NoNavFocus)) + { SetFocusID(id, window); - FocusWindow(window); + FocusWindow(window); + } + else + { + FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child + } } if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseClickedCount[mouse_button_clicked] == 2)) { @@ -570,10 +583,16 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool ClearActiveID(); else SetActiveID(id, window); // Hold on ID + g.ActiveIdMouseButton = mouse_button_clicked; if (!(flags & ImGuiButtonFlags_NoNavFocus)) + { SetFocusID(id, window); - g.ActiveIdMouseButton = mouse_button_clicked; - FocusWindow(window); + FocusWindow(window); + } + else + { + FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child + } } } if (flags & ImGuiButtonFlags_PressedOnRelease) @@ -592,7 +611,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // 'Repeat' mode acts when held regardless of _PressedOn flags (see table above). // Relies on repeat logic of IsMouseClicked() but we may as well do it ourselves if we end up exposing finer RepeatDelay/RepeatRate settings. if (g.ActiveId == id && (item_flags & ImGuiItemFlags_ButtonRepeat)) - if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, test_owner_id, ImGuiInputFlags_Repeat)) + if (g.IO.MouseDownDuration[g.ActiveIdMouseButton] > 0.0f && IsMouseClicked(g.ActiveIdMouseButton, ImGuiInputFlags_Repeat, test_owner_id)) pressed = true; } @@ -831,17 +850,15 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) return pressed; // Render - // FIXME: Clarify this mess - ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); - ImVec2 center = bb.GetCenter(); + ImU32 bg_col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); if (hovered) - window->DrawList->AddCircleFilled(center, ImMax(2.0f, g.FontSize * 0.5f + 1.0f), col); - - float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; + window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact); ImU32 cross_col = GetColorU32(ImGuiCol_Text); - center -= ImVec2(0.5f, 0.5f); - window->DrawList->AddLine(center + ImVec2(+cross_extent, +cross_extent), center + ImVec2(-cross_extent, -cross_extent), cross_col, 1.0f); - window->DrawList->AddLine(center + ImVec2(+cross_extent, -cross_extent), center + ImVec2(-cross_extent, +cross_extent), cross_col, 1.0f); + ImVec2 cross_center = bb.GetCenter() - ImVec2(0.5f, 0.5f); + float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; + window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, +cross_extent), cross_center + ImVec2(-cross_extent, -cross_extent), cross_col, 1.0f); + window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, -cross_extent), cross_center + ImVec2(-cross_extent, +cross_extent), cross_col, 1.0f); return pressed; } @@ -862,7 +879,8 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); ImU32 text_col = GetColorU32(ImGuiCol_Text); if (hovered || held) - window->DrawList->AddCircleFilled(bb.GetCenter() + ImVec2(0.0f, -0.5f), g.FontSize * 0.5f + 1.0f, bg_col); + window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact); RenderArrow(window->DrawList, bb.Min, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold @@ -913,10 +931,10 @@ void ImGui::Scrollbar(ImGuiAxis axis) if (!window->ScrollbarX) rounding_corners |= ImDrawFlags_RoundCornersBottomRight; } - float size_avail = window->InnerRect.Max[axis] - window->InnerRect.Min[axis]; + float size_visible = window->InnerRect.Max[axis] - window->InnerRect.Min[axis]; float size_contents = window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f; ImS64 scroll = (ImS64)window->Scroll[axis]; - ScrollbarEx(bb, id, axis, &scroll, (ImS64)size_avail, (ImS64)size_contents, rounding_corners); + ScrollbarEx(bb, id, axis, &scroll, (ImS64)size_visible, (ImS64)size_contents, rounding_corners); window->Scroll[axis] = (float)scroll; } @@ -926,7 +944,7 @@ void ImGui::Scrollbar(ImGuiAxis axis) // - We store values as normalized ratio and in a form that allows the window content to change while we are holding on a scrollbar // - We handle both horizontal and vertical scrollbars, which makes the terminology not ideal. // Still, the code should probably be made simpler.. -bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 size_avail_v, ImS64 size_contents_v, ImDrawFlags flags) +bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS64* p_scroll_v, ImS64 size_visible_v, ImS64 size_contents_v, ImDrawFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; @@ -956,9 +974,9 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 // Calculate the height of our grabbable box. It generally represent the amount visible (vs the total scrollable amount) // But we maintain a minimum size in pixel to allow for the user to still aim inside. - IM_ASSERT(ImMax(size_contents_v, size_avail_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers. - const ImS64 win_size_v = ImMax(ImMax(size_contents_v, size_avail_v), (ImS64)1); - const float grab_h_pixels = ImClamp(scrollbar_size_v * ((float)size_avail_v / (float)win_size_v), style.GrabMinSize, scrollbar_size_v); + IM_ASSERT(ImMax(size_contents_v, size_visible_v) > 0.0f); // Adding this assert to check if the ImMax(XXX,1.0f) is still needed. PLEASE CONTACT ME if this triggers. + const ImS64 win_size_v = ImMax(ImMax(size_contents_v, size_visible_v), (ImS64)1); + const float grab_h_pixels = ImClamp(scrollbar_size_v * ((float)size_visible_v / (float)win_size_v), style.GrabMinSize, scrollbar_size_v); const float grab_h_norm = grab_h_pixels / scrollbar_size_v; // Handle input right away. None of the code of Begin() is relying on scrolling position before calling Scrollbar(). @@ -967,7 +985,7 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 ItemAdd(bb_frame, id, NULL, ImGuiItemFlags_NoNav); ButtonBehavior(bb, id, &hovered, &held, ImGuiButtonFlags_NoNavFocus); - const ImS64 scroll_max = ImMax((ImS64)1, size_contents_v - size_avail_v); + const ImS64 scroll_max = ImMax((ImS64)1, size_contents_v - size_visible_v); float scroll_ratio = ImSaturate((float)*p_scroll_v / (float)scroll_max); float grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; // Grab position in normalized space if (held && allow_interaction && grab_h_norm < 1.0f) @@ -978,29 +996,39 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 // Click position in scrollbar normalized space (0.0f->1.0f) const float clicked_v_norm = ImSaturate((mouse_pos_v - scrollbar_pos_v) / scrollbar_size_v); - bool seek_absolute = false; + const int held_dir = (clicked_v_norm < grab_v_norm) ? -1 : (clicked_v_norm > grab_v_norm + grab_h_norm) ? +1 : 0; if (g.ActiveIdIsJustActivated) { // On initial click calculate the distance between mouse and the center of the grab - seek_absolute = (clicked_v_norm < grab_v_norm || clicked_v_norm > grab_v_norm + grab_h_norm); - if (seek_absolute) - g.ScrollbarClickDeltaToGrabCenter = 0.0f; - else - g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; + g.ScrollbarSeekMode = (short)held_dir; + g.ScrollbarClickDeltaToGrabCenter = (g.ScrollbarSeekMode == 0.0f) ? clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f : 0.0f; } // Apply scroll (p_scroll_v will generally point on one member of window->Scroll) // It is ok to modify Scroll here because we are being called in Begin() after the calculation of ContentSize and before setting up our starting position - const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); - *p_scroll_v = (ImS64)(scroll_v_norm * scroll_max); + if (g.ScrollbarSeekMode == 0) + { + // Absolute seeking + const float scroll_v_norm = ImSaturate((clicked_v_norm - g.ScrollbarClickDeltaToGrabCenter - grab_h_norm * 0.5f) / (1.0f - grab_h_norm)); + *p_scroll_v = (ImS64)(scroll_v_norm * scroll_max); + } + else + { + // Page by page + if (IsMouseClicked(ImGuiMouseButton_Left, ImGuiInputFlags_Repeat) && held_dir == g.ScrollbarSeekMode) + { + float page_dir = (g.ScrollbarSeekMode > 0.0f) ? +1.0f : -1.0f; + *p_scroll_v = ImClamp(*p_scroll_v + (ImS64)(page_dir * size_visible_v), (ImS64)0, scroll_max); + } + } // Update values for rendering scroll_ratio = ImSaturate((float)*p_scroll_v / (float)scroll_max); grab_v_norm = scroll_ratio * (scrollbar_size_v - grab_h_pixels) / scrollbar_size_v; - // Update distance to grab now that we have seeked and saturated - if (seek_absolute) - g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; + // Update distance to grab now that we have seek'ed and saturated + //if (seek_absolute) + // g.ScrollbarClickDeltaToGrabCenter = clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f; } // Render @@ -1119,42 +1147,60 @@ bool ImGui::Checkbox(const char* label, bool* v) const ImVec2 pos = window->DC.CursorPos; const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, id)) - { - IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); - return false; - } + const bool is_visible = ItemAdd(total_bb, id); + const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; + if (!is_visible) + if (!is_multi_select || !g.BoxSelectState.UnclipMode || !g.BoxSelectState.UnclipRect.Overlaps(total_bb)) // Extra layer of "no logic clip" for box-select support + { + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); + return false; + } + + // Range-Selection/Multi-selection support (header) + bool checked = *v; + if (is_multi_select) + MultiSelectItemHeader(id, &checked, NULL); bool hovered, held; bool pressed = ButtonBehavior(total_bb, id, &hovered, &held); - if (pressed) + + // Range-Selection/Multi-selection support (footer) + if (is_multi_select) + MultiSelectItemFooter(id, &checked, &pressed); + else if (pressed) + checked = !checked; + + if (*v != checked) { - *v = !(*v); + *v = checked; + pressed = true; // return value MarkItemEdited(id); } const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); - RenderNavHighlight(total_bb, id); - RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); - ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); - bool mixed_value = (g.LastItemData.InFlags & ImGuiItemFlags_MixedValue) != 0; - if (mixed_value) + const bool mixed_value = (g.LastItemData.InFlags & ImGuiItemFlags_MixedValue) != 0; + if (is_visible) { - // Undocumented tristate/mixed/indeterminate checkbox (#2644) - // This may seem awkwardly designed because the aim is to make ImGuiItemFlags_MixedValue supported by all widgets (not just checkbox) - ImVec2 pad(ImMax(1.0f, IM_TRUNC(square_sz / 3.6f)), ImMax(1.0f, IM_TRUNC(square_sz / 3.6f))); - window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); - } - else if (*v) - { - const float pad = ImMax(1.0f, IM_TRUNC(square_sz / 6.0f)); - RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad * 2.0f); + RenderNavHighlight(total_bb, id); + RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); + ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); + if (mixed_value) + { + // Undocumented tristate/mixed/indeterminate checkbox (#2644) + // This may seem awkwardly designed because the aim is to make ImGuiItemFlags_MixedValue supported by all widgets (not just checkbox) + ImVec2 pad(ImMax(1.0f, IM_TRUNC(square_sz / 3.6f)), ImMax(1.0f, IM_TRUNC(square_sz / 3.6f))); + window->DrawList->AddRectFilled(check_bb.Min + pad, check_bb.Max - pad, check_col, style.FrameRounding); + } + else if (*v) + { + const float pad = ImMax(1.0f, IM_TRUNC(square_sz / 6.0f)); + RenderCheckMark(window->DrawList, check_bb.Min + ImVec2(pad, pad), check_col, square_sz - pad * 2.0f); + } } - - ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y); + const ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y); if (g.LogEnabled) LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]"); - if (label_size.x > 0.0f) + if (is_visible && label_size.x > 0.0f) RenderText(label_pos, label); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0)); @@ -1288,24 +1334,47 @@ void ImGui::ProgressBar(float fraction, const ImVec2& size_arg, const char* over if (!ItemAdd(bb, 0)) return; + // Fraction < 0.0f will display an indeterminate progress bar animation + // The value must be animated along with time, so e.g. passing '-1.0f * ImGui::GetTime()' as fraction works. + const bool is_indeterminate = (fraction < 0.0f); + if (!is_indeterminate) + fraction = ImSaturate(fraction); + + // Out of courtesy we accept a NaN fraction without crashing + float fill_n0 = 0.0f; + float fill_n1 = (fraction == fraction) ? fraction : 0.0f; + + if (is_indeterminate) + { + const float fill_width_n = 0.2f; + fill_n0 = ImFmod(-fraction, 1.0f) * (1.0f + fill_width_n) - fill_width_n; + fill_n1 = ImSaturate(fill_n0 + fill_width_n); + fill_n0 = ImSaturate(fill_n0); + } + // Render - fraction = ImSaturate(fraction); RenderFrame(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); bb.Expand(ImVec2(-style.FrameBorderSize, -style.FrameBorderSize)); - const ImVec2 fill_br = ImVec2(ImLerp(bb.Min.x, bb.Max.x, fraction), bb.Max.y); - RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), 0.0f, fraction, style.FrameRounding); + RenderRectFilledRangeH(window->DrawList, bb, GetColorU32(ImGuiCol_PlotHistogram), fill_n0, fill_n1, style.FrameRounding); // Default displaying the fraction as percentage string, but user can override it + // Don't display text for indeterminate bars by default char overlay_buf[32]; - if (!overlay) + if (!is_indeterminate || overlay != NULL) { - ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction * 100 + 0.01f); - overlay = overlay_buf; - } + if (!overlay) + { + ImFormatString(overlay_buf, IM_ARRAYSIZE(overlay_buf), "%.0f%%", fraction * 100 + 0.01f); + overlay = overlay_buf; + } - ImVec2 overlay_size = CalcTextSize(overlay, NULL); - if (overlay_size.x > 0.0f) - RenderTextClipped(ImVec2(ImClamp(fill_br.x + style.ItemSpacing.x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f, 0.5f), &bb); + ImVec2 overlay_size = CalcTextSize(overlay, NULL); + if (overlay_size.x > 0.0f) + { + float text_x = is_indeterminate ? (bb.Min.x + bb.Max.x - overlay_size.x) * 0.5f : ImLerp(bb.Min.x, bb.Max.x, fill_n1) + style.ItemSpacing.x; + RenderTextClipped(ImVec2(ImClamp(text_x, bb.Min.x, bb.Max.x - overlay_size.x - style.ItemInnerSpacing.x), bb.Min.y), bb.Max, overlay, NULL, &overlay_size, ImVec2(0.0f, 0.5f), &bb); + } + } } void ImGui::Bullet() @@ -1331,6 +1400,76 @@ void ImGui::Bullet() SameLine(0, style.FramePadding.x * 2.0f); } +// This is provided as a convenience for being an often requested feature. +// FIXME-STYLE: we delayed adding as there is a larger plan to revamp the styling system. +// Because of this we currently don't provide many styling options for this widget +// (e.g. hovered/active colors are automatically inferred from a single color). +bool ImGui::TextLink(const char* label) +{ + ImGuiWindow* window = GetCurrentWindow(); + if (window->SkipItems) + return false; + + ImGuiContext& g = *GImGui; + const ImGuiID id = window->GetID(label); + const char* label_end = FindRenderedTextEnd(label); + + ImVec2 pos = window->DC.CursorPos; + ImVec2 size = CalcTextSize(label, label_end, true); + ImRect bb(pos, pos + size); + ItemSize(size, 0.0f); + if (!ItemAdd(bb, id)) + return false; + + bool hovered, held; + bool pressed = ButtonBehavior(bb, id, &hovered, &held); + RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_None); + + ImVec4 text_colf = g.Style.Colors[ImGuiCol_TextLink]; + ImVec4 line_colf = text_colf; + { + // FIXME-STYLE: Read comments above. This widget is NOT written in the same style as some earlier widgets, + // as we are currently experimenting/planning a different styling system. + float h, s, v; + ColorConvertRGBtoHSV(text_colf.x, text_colf.y, text_colf.z, h, s, v); + if (held || hovered) + { + v = ImSaturate(v + (held ? 0.4f : 0.3f)); + h = ImFmod(h + 0.02f, 1.0f); + } + ColorConvertHSVtoRGB(h, s, v, text_colf.x, text_colf.y, text_colf.z); + v = ImSaturate(v - 0.20f); + ColorConvertHSVtoRGB(h, s, v, line_colf.x, line_colf.y, line_colf.z); + } + + float line_y = bb.Max.y + ImFloor(g.Font->Descent * g.FontScale * 0.20f); + window->DrawList->AddLine(ImVec2(bb.Min.x, line_y), ImVec2(bb.Max.x, line_y), GetColorU32(line_colf)); // FIXME-TEXT: Underline mode. + + PushStyleColor(ImGuiCol_Text, GetColorU32(text_colf)); + RenderText(bb.Min, label, label_end); + PopStyleColor(); + + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); + return pressed; +} + +void ImGui::TextLinkOpenURL(const char* label, const char* url) +{ + ImGuiContext& g = *GImGui; + if (url == NULL) + url = label; + if (TextLink(label)) + if (g.IO.PlatformOpenInShellFn != NULL) + g.IO.PlatformOpenInShellFn(&g, url); + SetItemTooltip("%s", url); // It is more reassuring for user to _always_ display URL when we same as label + if (BeginPopupContextItem()) + { + if (MenuItem(LocalizeGetMsg(ImGuiLocKey_CopyLink))) + SetClipboardText(url); + EndPopup(); + } +} + //------------------------------------------------------------------------- // [SECTION] Widgets: Low-level Layout helpers //------------------------------------------------------------------------- @@ -1916,28 +2055,30 @@ bool ImGui::Combo(const char* label, int* current_item, const char* (*getter)(vo return false; // Display items - // FIXME-OPT: Use clipper (but we need to disable it on the appearing frame to make sure our call to SetItemDefaultFocus() is processed) bool value_changed = false; - for (int i = 0; i < items_count; i++) - { - const char* item_text = getter(user_data, i); - if (item_text == NULL) - item_text = "*Unknown item*"; - - PushID(i); - const bool item_selected = (i == *current_item); - if (Selectable(item_text, item_selected) && *current_item != i) + ImGuiListClipper clipper; + clipper.Begin(items_count); + clipper.IncludeItemByIndex(*current_item); + while (clipper.Step()) + for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { - value_changed = true; - *current_item = i; + const char* item_text = getter(user_data, i); + if (item_text == NULL) + item_text = "*Unknown item*"; + + PushID(i); + const bool item_selected = (i == *current_item); + if (Selectable(item_text, item_selected) && *current_item != i) + { + value_changed = true; + *current_item = i; + } + if (item_selected) + SetItemDefaultFocus(); + PopID(); } - if (item_selected) - SetItemDefaultFocus(); - PopID(); - } EndCombo(); - if (value_changed) MarkItemEdited(g.LastItemData.ID); @@ -2019,6 +2160,7 @@ static const ImGuiDataTypeInfo GDataTypeInfo[] = #endif { sizeof(float), "float", "%.3f","%f" }, // ImGuiDataType_Float (float are promoted to double in va_arg) { sizeof(double), "double","%f", "%lf" }, // ImGuiDataType_Double + { sizeof(bool), "bool", "%d", "%d" }, // ImGuiDataType_Bool }; IM_STATIC_ASSERT(IM_ARRAYSIZE(GDataTypeInfo) == ImGuiDataType_COUNT); @@ -2103,17 +2245,24 @@ void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const // User can input math operators (e.g. +100) to edit a numerical values. // NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess.. -bool ImGui::DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format) +bool ImGui::DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format, void* p_data_when_empty) { + // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. + const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type); + ImGuiDataTypeStorage data_backup; + memcpy(&data_backup, p_data, type_info->Size); + while (ImCharIsBlankA(*buf)) buf++; if (!buf[0]) + { + if (p_data_when_empty != NULL) + { + memcpy(p_data, p_data_when_empty, type_info->Size); + return memcmp(&data_backup, p_data, type_info->Size) != 0; + } return false; - - // Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all. - const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type); - ImGuiDataTypeTempStorage data_backup; - memcpy(&data_backup, p_data, type_info->Size); + } // Sanitize format // - For float/double we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in, so force them into %f and %lf @@ -2260,12 +2409,13 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const { ImGuiContext& g = *GImGui; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; - const bool is_clamped = (v_min < v_max); + const bool is_bounded = (v_min < v_max); + const bool is_wrapped = is_bounded && (flags & ImGuiSliderFlags_WrapAround); const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); // Default tweak speed - if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) + if (v_speed == 0.0f && is_bounded && (v_max - v_min < FLT_MAX)) v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio); // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings @@ -2283,7 +2433,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; const bool tweak_slow = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakSlow : ImGuiKey_NavKeyboardTweakSlow); const bool tweak_fast = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakFast : ImGuiKey_NavKeyboardTweakFast); - const float tweak_factor = tweak_slow ? 1.0f / 1.0f : tweak_fast ? 10.0f : 1.0f; + const float tweak_factor = tweak_slow ? 1.0f / 10.0f : tweak_fast ? 10.0f : 1.0f; adjust_delta = GetNavTweakPressedAmount(axis) * tweak_factor; v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); } @@ -2299,8 +2449,8 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const // Clear current value on activation // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300. - bool is_just_activated = g.ActiveIdIsJustActivated; - bool is_already_past_limits_and_pushing_outward = is_clamped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f)); + const bool is_just_activated = g.ActiveIdIsJustActivated; + const bool is_already_past_limits_and_pushing_outward = is_bounded && !is_wrapped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f)); if (is_just_activated || is_already_past_limits_and_pushing_outward) { g.DragCurrentAccum = 0.0f; @@ -2358,13 +2508,24 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const if (v_cur == (TYPE)-0) v_cur = (TYPE)0; - // Clamp values (+ handle overflow/wrap-around for integer types) - if (*v != v_cur && is_clamped) + if (*v != v_cur && is_bounded) { - if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_floating_point)) - v_cur = v_min; - if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_floating_point)) - v_cur = v_max; + if (is_wrapped) + { + // Wrap values + if (v_cur < v_min) + v_cur += v_max - v_min + (is_floating_point ? 0 : 1); + if (v_cur > v_max) + v_cur -= v_max - v_min + (is_floating_point ? 0 : 1); + } + else + { + // Clamp values + handle overflow/wrap-around for integer types. + if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_floating_point)) + v_cur = v_min; + if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_floating_point)) + v_cur = v_max; + } } // Apply result @@ -2377,7 +2538,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v_speed, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags) { // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. - IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flags! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flags! Has the legacy 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); ImGuiContext& g = *GImGui; if (g.ActiveId == id) @@ -2442,7 +2603,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (!temp_input_is_active) { // Tabbing or CTRL-clicking on Drag turns it into an InputText - const bool clicked = hovered && IsMouseClicked(0, id); + const bool clicked = hovered && IsMouseClicked(0, ImGuiInputFlags_None, id); const bool double_clicked = (hovered && g.IO.MouseClickedCount[0] == 2 && TestKeyOwner(ImGuiKey_MouseLeft, id)); const bool make_active = (clicked || double_clicked || g.NavActivateId == id); if (make_active && (clicked || double_clicked)) @@ -2970,7 +3131,8 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ bool ImGui::SliderBehavior(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, void* p_v, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb) { // Read imgui.cpp "API BREAKING CHANGES" section for 1.78 if you hit this assert. - IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flag! Has the 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + IM_ASSERT((flags == 1 || (flags & ImGuiSliderFlags_InvalidMask_) == 0) && "Invalid ImGuiSliderFlags flags! Has the legacy 'float power' argument been mistakenly cast to flags? Call function with ImGuiSliderFlags_Logarithmic flags instead."); + IM_ASSERT((flags & ImGuiSliderFlags_WrapAround) == 0); // Not supported by SliderXXX(), only by DragXXX() switch (data_type) { @@ -3033,7 +3195,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (!temp_input_is_active) { // Tabbing or CTRL-clicking on Slider turns it into an input box - const bool clicked = hovered && IsMouseClicked(0, id); + const bool clicked = hovered && IsMouseClicked(0, ImGuiInputFlags_None, id); const bool make_active = (clicked || g.NavActivateId == id); if (make_active && clicked) SetKeyOwner(ImGuiKey_MouseLeft, id); @@ -3195,7 +3357,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d format = DataTypeGetInfo(data_type)->PrintFmt; const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); - const bool clicked = hovered && IsMouseClicked(0, id); + const bool clicked = hovered && IsMouseClicked(0, ImGuiInputFlags_None, id); if (clicked || g.NavActivateId == id) { if (clicked) @@ -3426,18 +3588,18 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); ImStrTrimBlanks(data_buf); - ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; + ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; bool value_changed = false; if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) { // Backup old value size_t data_type_size = type_info->Size; - ImGuiDataTypeTempStorage data_backup; + ImGuiDataTypeStorage data_backup; memcpy(&data_backup, p_data, data_type_size); // Apply new value (or operations) then clamp - DataTypeApplyFromText(data_buf, data_type, p_data, format); + DataTypeApplyFromText(data_buf, data_type, p_data, format, NULL); if (p_clamp_min || p_clamp_max) { if (p_clamp_min && p_clamp_max && DataTypeCompare(data_type, p_clamp_min, p_clamp_max) > 0) @@ -3453,6 +3615,13 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG return value_changed; } +void ImGui::SetNextItemRefVal(ImGuiDataType data_type, void* p_data) +{ + ImGuiContext& g = *GImGui; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasRefVal; + memcpy(&g.NextItemData.RefVal, p_data, DataTypeGetInfo(data_type)->Size); +} + // Note: p_data, p_step, p_step_fast are _pointers_ to a memory address holding the data. For an Input widget, p_step and p_step_fast are optional. // Read code of e.g. InputFloat(), InputInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly. bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags) @@ -3467,16 +3636,22 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; + void* p_data_default = (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasRefVal) ? &g.NextItemData.RefVal : &g.DataTypeZeroValue; + char buf[64]; - DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); + if ((flags & ImGuiInputTextFlags_DisplayEmptyRefVal) && DataTypeCompare(data_type, p_data, p_data_default) == 0) + buf[0] = 0; + else + DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); flags |= ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. + flags |= (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; bool value_changed = false; if (p_step == NULL) { if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) - value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format, (flags & ImGuiInputTextFlags_ParseEmptyRefVal) ? p_data_default : NULL); } else { @@ -3486,7 +3661,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data PushID(label); SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view - value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format, (flags & ImGuiInputTextFlags_ParseEmptyRefVal) ? p_data_default : NULL); IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); // Step buttons @@ -3704,7 +3879,7 @@ namespace ImStb static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenW); return obj->TextW[idx]; } -static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * g.FontScale; } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) @@ -3722,7 +3897,7 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* ob static bool is_separator(unsigned int c) { - return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r' || c=='.' || c=='!'; + return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r' || c=='.' || c=='!' || c=='\\' || c=='/'; } static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) @@ -3916,9 +4091,8 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons } // Return false to discard a character. -static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source) +static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, bool input_source_is_clipboard) { - IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard); unsigned int c = *p_char; // Filter non-printable (NB: isprint is unreliable! see #2467) @@ -3933,7 +4107,7 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im apply_named_filters = false; // Override named filters below so newline and tabs can still be inserted. } - if (input_source != ImGuiInputSource_Clipboard) + if (input_source_is_clipboard == false) { // We ignore Ascii representation of delete (emitted from Backspace on OSX, see #2578, #2817) if (c == 127) @@ -3949,7 +4123,7 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im return false; // Generic named filters - if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific))) + if (apply_named_filters && (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsUppercase | ImGuiInputTextFlags_CharsNoBlank | ImGuiInputTextFlags_CharsScientific | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint))) { // The libc allows overriding locale, with e.g. 'setlocale(LC_NUMERIC, "de_DE.UTF-8");' which affect the output/input of printf/scanf to use e.g. ',' instead of '.'. // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. @@ -3959,7 +4133,7 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im // Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions. ImGuiContext& g = *ctx; const unsigned c_decimal_point = (unsigned int)g.IO.PlatformLocaleDecimalPoint; - if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific)) + if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint)) if (c == '.' || c == ',') c = c_decimal_point; @@ -4139,7 +4313,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges - bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), true, ImGuiWindowFlags_NoMove); + bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove); g.NavActivateId = backup_activate_id; PopStyleVar(3); PopStyleColor(); @@ -4283,6 +4457,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ SetKeyOwner(ImGuiKey_PageUp, id); SetKeyOwner(ImGuiKey_PageDown, id); } + // FIXME: May be a problem to always steal Alt on OSX, would ideally still allow an uninterrupted Alt down-up to toggle menu if (is_osx) SetKeyOwner(ImGuiMod_Alt, id); } @@ -4415,15 +4590,15 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // (For Tab and Enter: Win32/SFML/Allegro are sending both keys and chars, GLFW and SDL are only sending keys. For Space they all send all threes) if ((flags & ImGuiInputTextFlags_AllowTabInput) && !is_readonly) { - if (Shortcut(ImGuiKey_Tab, id, ImGuiInputFlags_Repeat)) + if (Shortcut(ImGuiKey_Tab, ImGuiInputFlags_Repeat, id)) { unsigned int c = '\t'; // Insert TAB - if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } // FIXME: Implement Shift+Tab /* - if (Shortcut(ImGuiKey_Tab | ImGuiMod_Shift, id, ImGuiInputFlags_Repeat)) + if (Shortcut(ImGuiKey_Tab | ImGuiMod_Shift, ImGuiInputFlags_Repeat, id)) { } */ @@ -4431,7 +4606,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Process regular text input (before we check for Return because using some IME will effectively send a Return?) // We ignore CTRL inputs, but need to allow ALT+CTRL as some keyboards (e.g. German) use AltGR (which _is_ Alt+Ctrl) to input certain characters. - const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeySuper); + const bool ignore_char_inputs = (io.KeyCtrl && !io.KeyAlt) || (is_osx && io.KeyCtrl); if (io.InputQueueCharacters.Size > 0) { if (!ignore_char_inputs && !is_readonly && !input_requested_by_nav) @@ -4441,7 +4616,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ unsigned int c = (unsigned int)io.InputQueueCharacters[n]; if (c == '\t') // Skip Tab, see above. continue; - if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } @@ -4461,25 +4636,26 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl - const bool is_startend_key_down = is_osx && io.KeySuper && !io.KeyCtrl && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End + const bool is_startend_key_down = is_osx && io.KeyCtrl && !io.KeySuper && !io.KeyAlt; // OS X style: Line/Text Start and End using Cmd+Arrows instead of Home/End // Using Shortcut() with ImGuiInputFlags_RouteFocused (default policy) to allow routing operations for other code (e.g. calling window trying to use CTRL+A and CTRL+B: formet would be handled by InputText) // Otherwise we could simply assume that we own the keys as we are active. const ImGuiInputFlags f_repeat = ImGuiInputFlags_Repeat; - const bool is_cut = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_X, id, f_repeat) || Shortcut(ImGuiMod_Shift | ImGuiKey_Delete, id, f_repeat)) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); - const bool is_copy = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_C, id) || Shortcut(ImGuiMod_Ctrl | ImGuiKey_Insert, id)) && !is_password && (!is_multiline || state->HasSelection()); - const bool is_paste = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_V, id, f_repeat) || Shortcut(ImGuiMod_Shift | ImGuiKey_Insert, id, f_repeat)) && !is_readonly; - const bool is_undo = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_Z, id, f_repeat)) && !is_readonly && is_undoable; - const bool is_redo = (Shortcut(ImGuiMod_Shortcut | ImGuiKey_Y, id, f_repeat) || (is_osx && Shortcut(ImGuiMod_Shortcut | ImGuiMod_Shift | ImGuiKey_Z, id, f_repeat))) && !is_readonly && is_undoable; - const bool is_select_all = Shortcut(ImGuiMod_Shortcut | ImGuiKey_A, id); + const bool is_cut = (Shortcut(ImGuiMod_Ctrl | ImGuiKey_X, f_repeat, id) || Shortcut(ImGuiMod_Shift | ImGuiKey_Delete, f_repeat, id)) && !is_readonly && !is_password && (!is_multiline || state->HasSelection()); + const bool is_copy = (Shortcut(ImGuiMod_Ctrl | ImGuiKey_C, 0, id) || Shortcut(ImGuiMod_Ctrl | ImGuiKey_Insert, 0, id)) && !is_password && (!is_multiline || state->HasSelection()); + const bool is_paste = (Shortcut(ImGuiMod_Ctrl | ImGuiKey_V, f_repeat, id) || Shortcut(ImGuiMod_Shift | ImGuiKey_Insert, f_repeat, id)) && !is_readonly; + const bool is_undo = (Shortcut(ImGuiMod_Ctrl | ImGuiKey_Z, f_repeat, id)) && !is_readonly && is_undoable; + const bool is_redo = (Shortcut(ImGuiMod_Ctrl | ImGuiKey_Y, f_repeat, id) || (is_osx && Shortcut(ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Z, f_repeat, id))) && !is_readonly && is_undoable; + const bool is_select_all = Shortcut(ImGuiMod_Ctrl | ImGuiKey_A, 0, id); // We allow validate/cancel with Nav source (gamepad) to makes it easier to undo an accidental NavInput press with no keyboard wired, but otherwise it isn't very useful. const bool nav_gamepad_active = (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) != 0 && (io.BackendFlags & ImGuiBackendFlags_HasGamepad) != 0; const bool is_enter_pressed = IsKeyPressed(ImGuiKey_Enter, true) || IsKeyPressed(ImGuiKey_KeypadEnter, true); const bool is_gamepad_validate = nav_gamepad_active && (IsKeyPressed(ImGuiKey_NavGamepadActivate, false) || IsKeyPressed(ImGuiKey_NavGamepadInput, false)); - const bool is_cancel = Shortcut(ImGuiKey_Escape, id, f_repeat) || (nav_gamepad_active && Shortcut(ImGuiKey_NavGamepadCancel, id, f_repeat)); + const bool is_cancel = Shortcut(ImGuiKey_Escape, f_repeat, id) || (nav_gamepad_active && Shortcut(ImGuiKey_NavGamepadCancel, f_repeat, id)); // FIXME: Should use more Shortcut() and reduce IsKeyPressed()+SetKeyOwner(), but requires modifiers combination to be taken account of. + // FIXME-OSX: Missing support for Alt(option)+Right/Left = go to end of line, or next line if already in end of line. if (IsKeyPressed(ImGuiKey_LeftArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINESTART : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDLEFT : STB_TEXTEDIT_K_LEFT) | k_mask); } else if (IsKeyPressed(ImGuiKey_RightArrow)) { state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_LINEEND : is_wordmove_key_down ? STB_TEXTEDIT_K_WORDRIGHT : STB_TEXTEDIT_K_RIGHT) | k_mask); } else if (IsKeyPressed(ImGuiKey_UpArrow) && is_multiline) { if (io.KeyCtrl) SetScrollY(draw_window, ImMax(draw_window->Scroll.y - g.FontSize, 0.0f)); else state->OnKeyPressed((is_startend_key_down ? STB_TEXTEDIT_K_TEXTSTART : STB_TEXTEDIT_K_UP) | k_mask); } @@ -4504,7 +4680,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { if (is_wordmove_key_down) state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT); - else if (is_osx && io.KeySuper && !io.KeyAlt && !io.KeyCtrl) + else if (is_osx && io.KeyCtrl && !io.KeyAlt && !io.KeySuper) state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT); } state->OnKeyPressed(STB_TEXTEDIT_K_BACKSPACE | k_mask); @@ -4524,7 +4700,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (!is_readonly) { unsigned int c = '\n'; // Insert new line - if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard)) + if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) state->OnKeyPressed((int)c); } } @@ -4591,7 +4767,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { unsigned int c; s += ImTextCharFromUtf8(&c, s, NULL); - if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard)) + if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, true)) continue; clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; } @@ -4674,7 +4850,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // The reason we specify the usage semantic (Completion/History) is that Completion needs to disable keyboard TABBING at the moment. ImGuiInputTextFlags event_flag = 0; ImGuiKey event_key = ImGuiKey_None; - if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && Shortcut(ImGuiKey_Tab, id)) + if ((flags & ImGuiInputTextFlags_CallbackCompletion) != 0 && Shortcut(ImGuiKey_Tab, 0, id)) { event_flag = ImGuiInputTextFlags_CallbackCompletion; event_key = ImGuiKey_Tab; @@ -6049,7 +6225,8 @@ bool ImGui::TreeNode(const char* label) ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - return TreeNodeBehavior(window->GetID(label), 0, label, NULL); + ImGuiID id = window->GetID(label); + return TreeNodeBehavior(id, ImGuiTreeNodeFlags_None, label, NULL); } bool ImGui::TreeNodeV(const char* str_id, const char* fmt, va_list args) @@ -6067,8 +6244,8 @@ bool ImGui::TreeNodeEx(const char* label, ImGuiTreeNodeFlags flags) ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - - return TreeNodeBehavior(window->GetID(label), flags, label, NULL); + ImGuiID id = window->GetID(label); + return TreeNodeBehavior(id, flags, label, NULL); } bool ImGui::TreeNodeEx(const char* str_id, ImGuiTreeNodeFlags flags, const char* fmt, ...) @@ -6095,9 +6272,10 @@ bool ImGui::TreeNodeExV(const char* str_id, ImGuiTreeNodeFlags flags, const char if (window->SkipItems) return false; + ImGuiID id = window->GetID(str_id); const char* label, *label_end; ImFormatStringToTempBufferV(&label, &label_end, fmt, args); - return TreeNodeBehavior(window->GetID(str_id), flags, label, label_end); + return TreeNodeBehavior(id, flags, label, label_end); } bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char* fmt, va_list args) @@ -6106,24 +6284,32 @@ bool ImGui::TreeNodeExV(const void* ptr_id, ImGuiTreeNodeFlags flags, const char if (window->SkipItems) return false; + ImGuiID id = window->GetID(ptr_id); const char* label, *label_end; ImFormatStringToTempBufferV(&label, &label_end, fmt, args); - return TreeNodeBehavior(window->GetID(ptr_id), flags, label, label_end); + return TreeNodeBehavior(id, flags, label, label_end); +} + +bool ImGui::TreeNodeGetOpen(ImGuiID storage_id) +{ + ImGuiContext& g = *GImGui; + ImGuiStorage* storage = g.CurrentWindow->DC.StateStorage; + return storage->GetInt(storage_id, 0) != 0; } -void ImGui::TreeNodeSetOpen(ImGuiID id, bool open) +void ImGui::TreeNodeSetOpen(ImGuiID storage_id, bool open) { ImGuiContext& g = *GImGui; ImGuiStorage* storage = g.CurrentWindow->DC.StateStorage; - storage->SetInt(id, open ? 1 : 0); + storage->SetInt(storage_id, open ? 1 : 0); } -bool ImGui::TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags) +bool ImGui::TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags) { if (flags & ImGuiTreeNodeFlags_Leaf) return true; - // We only write to the tree storage if the user clicks (or explicitly use the SetNextItemOpen function) + // We only write to the tree storage if the user clicks, or explicitly use the SetNextItemOpen function ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; ImGuiStorage* storage = window->DC.StateStorage; @@ -6134,16 +6320,16 @@ bool ImGui::TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags) if (g.NextItemData.OpenCond & ImGuiCond_Always) { is_open = g.NextItemData.OpenVal; - TreeNodeSetOpen(id, is_open); + TreeNodeSetOpen(storage_id, is_open); } else { // We treat ImGuiCond_Once and ImGuiCond_FirstUseEver the same because tree node state are not saved persistently. - const int stored_value = storage->GetInt(id, -1); + const int stored_value = storage->GetInt(storage_id, -1); if (stored_value == -1) { is_open = g.NextItemData.OpenVal; - TreeNodeSetOpen(id, is_open); + TreeNodeSetOpen(storage_id, is_open); } else { @@ -6153,7 +6339,7 @@ bool ImGui::TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags) } else { - is_open = storage->GetInt(id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0; + is_open = storage->GetInt(storage_id, (flags & ImGuiTreeNodeFlags_DefaultOpen) ? 1 : 0) != 0; } // When logging is enabled, we automatically expand tree nodes (but *NOT* collapsing headers.. seems like sensible behavior). @@ -6164,6 +6350,23 @@ bool ImGui::TreeNodeUpdateNextOpen(ImGuiID id, ImGuiTreeNodeFlags flags) return is_open; } +// Store ImGuiTreeNodeStackData for just submitted node. +// Currently only supports 32 level deep and we are fine with (1 << Depth) overflowing into a zero, easy to increase. +static void TreeNodeStoreStackData(ImGuiTreeNodeFlags flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + g.TreeNodeStack.resize(g.TreeNodeStack.Size + 1); + ImGuiTreeNodeStackData* tree_node_data = &g.TreeNodeStack.back(); + tree_node_data->ID = g.LastItemData.ID; + tree_node_data->TreeFlags = flags; + tree_node_data->InFlags = g.LastItemData.InFlags; + tree_node_data->NavRect = g.LastItemData.NavRect; + window->DC.TreeHasStackDataDepthMask |= (1 << window->DC.TreeDepth); +} + +// When using public API, currently 'id == storage_id' is always true, but we separate the values to facilitate advanced user code doing storage queries outside of UI loop. bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* label, const char* label_end) { ImGuiWindow* window = GetCurrentWindow(); @@ -6179,73 +6382,73 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l label_end = FindRenderedTextEnd(label); const ImVec2 label_size = CalcTextSize(label, label_end, false); + const float text_offset_x = g.FontSize + (display_frame ? padding.x * 3 : padding.x * 2); // Collapsing arrow width + Spacing + const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it + const float text_width = g.FontSize + label_size.x + padding.x * 2; // Include collapsing arrow + // We vertically grow up to current line height up the typical widget height. const float frame_height = ImMax(ImMin(window->DC.CurrLineSize.y, g.FontSize + style.FramePadding.y * 2), label_size.y + padding.y * 2); const bool span_all_columns = (flags & ImGuiTreeNodeFlags_SpanAllColumns) != 0 && (g.CurrentTable != NULL); ImRect frame_bb; frame_bb.Min.x = span_all_columns ? window->ParentWorkRect.Min.x : (flags & ImGuiTreeNodeFlags_SpanFullWidth) ? window->WorkRect.Min.x : window->DC.CursorPos.x; frame_bb.Min.y = window->DC.CursorPos.y; - frame_bb.Max.x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x; + frame_bb.Max.x = span_all_columns ? window->ParentWorkRect.Max.x : (flags & ImGuiTreeNodeFlags_SpanTextWidth) ? window->DC.CursorPos.x + text_width + padding.x : window->WorkRect.Max.x; frame_bb.Max.y = window->DC.CursorPos.y + frame_height; if (display_frame) { - // Framed header expand a little outside the default padding, to the edge of InnerClipRect - // (FIXME: May remove this at some point and make InnerClipRect align with WindowPadding.x instead of WindowPadding.x*0.5f) - frame_bb.Min.x -= IM_TRUNC(window->WindowPadding.x * 0.5f - 1.0f); - frame_bb.Max.x += IM_TRUNC(window->WindowPadding.x * 0.5f); + const float outer_extend = IM_TRUNC(window->WindowPadding.x * 0.5f); // Framed header expand a little outside of current limits + frame_bb.Min.x -= outer_extend; + frame_bb.Max.x += outer_extend; } - const float text_offset_x = g.FontSize + (display_frame ? padding.x * 3 : padding.x * 2); // Collapsing arrow width + Spacing - const float text_offset_y = ImMax(padding.y, window->DC.CurrLineTextBaseOffset); // Latch before ItemSize changes it - const float text_width = g.FontSize + (label_size.x > 0.0f ? label_size.x + padding.x * 2 : 0.0f); // Include collapsing ImVec2 text_pos(window->DC.CursorPos.x + text_offset_x, window->DC.CursorPos.y + text_offset_y); ItemSize(ImVec2(text_width, frame_height), padding.y); // For regular tree nodes, we arbitrary allow to click past 2 worth of ItemSpacing ImRect interact_bb = frame_bb; - if (!display_frame && (flags & (ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_SpanAllColumns)) == 0) - interact_bb.Max.x = frame_bb.Min.x + text_width + style.ItemSpacing.x * 2.0f; - - // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. - const float backup_clip_rect_min_x = window->ClipRect.Min.x; - const float backup_clip_rect_max_x = window->ClipRect.Max.x; - if (span_all_columns) - { - window->ClipRect.Min.x = window->ParentWorkRect.Min.x; - window->ClipRect.Max.x = window->ParentWorkRect.Max.x; - } + if ((flags & (ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_SpanAvailWidth | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_SpanTextWidth | ImGuiTreeNodeFlags_SpanAllColumns)) == 0) + interact_bb.Max.x = frame_bb.Min.x + text_width + (label_size.x > 0.0f ? style.ItemSpacing.x * 2.0f : 0.0f); // Compute open and multi-select states before ItemAdd() as it clear NextItem data. - bool is_open = TreeNodeUpdateNextOpen(id, flags); - bool item_add = ItemAdd(interact_bb, id); - g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; - g.LastItemData.DisplayRect = frame_bb; + ImGuiID storage_id = (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasStorageID) ? g.NextItemData.StorageId : id; + bool is_open = TreeNodeUpdateNextOpen(storage_id, flags); + bool is_visible; if (span_all_columns) { + // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. + const float backup_clip_rect_min_x = window->ClipRect.Min.x; + const float backup_clip_rect_max_x = window->ClipRect.Max.x; + window->ClipRect.Min.x = window->ParentWorkRect.Min.x; + window->ClipRect.Max.x = window->ParentWorkRect.Max.x; + is_visible = ItemAdd(interact_bb, id); window->ClipRect.Min.x = backup_clip_rect_min_x; window->ClipRect.Max.x = backup_clip_rect_max_x; } + else + { + is_visible = ItemAdd(interact_bb, id); + } + g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_HasDisplayRect; + g.LastItemData.DisplayRect = frame_bb; // If a NavLeft request is happening and ImGuiTreeNodeFlags_NavLeftJumpsBackHere enabled: // Store data for the current depth to allow returning to this node from any child item. // For this purpose we essentially compare if g.NavIdIsAlive went from 0 to 1 between TreeNode() and TreePop(). // It will become tempting to enable ImGuiTreeNodeFlags_NavLeftJumpsBackHere by default or move it to ImGuiStyle. - // Currently only supports 32 level deep and we are fine with (1 << Depth) overflowing into a zero, easy to increase. - if (is_open && !g.NavIdIsAlive && (flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) - if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) - { - g.NavTreeNodeStack.resize(g.NavTreeNodeStack.Size + 1); - ImGuiNavTreeNodeData* nav_tree_node_data = &g.NavTreeNodeStack.back(); - nav_tree_node_data->ID = id; - nav_tree_node_data->InFlags = g.LastItemData.InFlags; - nav_tree_node_data->NavRect = g.LastItemData.NavRect; - window->DC.TreeJumpToParentOnPopMask |= (1 << window->DC.TreeDepth); - } + bool store_tree_node_stack_data = false; + if (!(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) + { + if ((flags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) && is_open && !g.NavIdIsAlive) + if (g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) + store_tree_node_stack_data = true; + } const bool is_leaf = (flags & ImGuiTreeNodeFlags_Leaf) != 0; - if (!item_add) + if (!is_visible) { + if (store_tree_node_stack_data && is_open) + TreeNodeStoreStackData(flags); // Call before TreePushOverrideID() if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) TreePushOverrideID(id); IMGUI_TEST_ENGINE_ITEM_INFO(g.LastItemData.ID, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); @@ -6271,8 +6474,6 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l const float arrow_hit_x1 = (text_pos.x - text_offset_x) - style.TouchExtraPadding.x; const float arrow_hit_x2 = (text_pos.x - text_offset_x) + (g.FontSize + padding.x * 2.0f) + style.TouchExtraPadding.x; const bool is_mouse_x_over_arrow = (g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2); - if (window != g.HoveredWindow || !is_mouse_x_over_arrow) - button_flags |= ImGuiButtonFlags_NoKeyModifiers; // Open behaviors can be altered with the _OpenOnArrow and _OnOnDoubleClick flags. // Some alteration have subtle effects (e.g. toggle on MouseUp vs MouseDown events) due to requirements for multi-selection and drag and drop support. @@ -6293,6 +6494,24 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l bool selected = (flags & ImGuiTreeNodeFlags_Selected) != 0; const bool was_selected = selected; + // Multi-selection support (header) + const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; + if (is_multi_select) + { + // Handle multi-select + alter button flags for it + MultiSelectItemHeader(id, &selected, &button_flags); + if (is_mouse_x_over_arrow) + button_flags = (button_flags | ImGuiButtonFlags_PressedOnClick) & ~ImGuiButtonFlags_PressedOnClickRelease; + + // We absolutely need to distinguish open vs select so comes by default + flags |= ImGuiTreeNodeFlags_OpenOnArrow; + } + else + { + if (window != g.HoveredWindow || !is_mouse_x_over_arrow) + button_flags |= ImGuiButtonFlags_NoKeyModifiers; + } + bool hovered, held; bool pressed = ButtonBehavior(interact_bb, id, &hovered, &held, button_flags); bool toggled = false; @@ -6300,7 +6519,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { if (pressed && g.DragDropHoldJustPressedId != id) { - if ((flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) == 0 || (g.NavActivateId == id)) + if ((flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) == 0 || (g.NavActivateId == id && !is_multi_select)) toggled = true; if (flags & ImGuiTreeNodeFlags_OpenOnArrow) toggled |= is_mouse_x_over_arrow && !g.NavDisableMouseHover; // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job @@ -6330,64 +6549,78 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l if (toggled) { is_open = !is_open; - window->DC.StateStorage->SetInt(id, is_open); + window->DC.StateStorage->SetInt(storage_id, is_open); g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledOpen; } } - // In this branch, TreeNodeBehavior() cannot toggle the selection so this will never trigger. - if (selected != was_selected) //-V547 + // Multi-selection support (footer) + if (is_multi_select) + { + bool pressed_copy = pressed && !toggled; + MultiSelectItemFooter(id, &selected, &pressed_copy); + if (pressed) + SetNavID(id, window->DC.NavLayerCurrent, g.CurrentFocusScopeId, interact_bb); + } + + if (selected != was_selected) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render - const ImU32 text_col = GetColorU32(ImGuiCol_Text); - ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact; - if (display_frame) - { - // Framed type - const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); - RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, true, style.FrameRounding); - RenderNavHighlight(frame_bb, id, nav_highlight_flags); - if (flags & ImGuiTreeNodeFlags_Bullet) - RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col); - else if (!is_leaf) - RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 1.0f); - else // Leaf without bullet, left-adjusted text - text_pos.x -= text_offset_x -padding.x; - if (flags & ImGuiTreeNodeFlags_ClipLabelForTrailingButton) - frame_bb.Max.x -= g.FontSize + style.FramePadding.x; - - if (g.LogEnabled) - LogSetNextTextDecoration("###", "###"); - } - else { - // Unframed typed for tree nodes - if (hovered || selected) + const ImU32 text_col = GetColorU32(ImGuiCol_Text); + ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact; + if (is_multi_select) + nav_highlight_flags |= ImGuiNavHighlightFlags_AlwaysDraw; // Always show the nav rectangle + if (display_frame) { + // Framed type const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); - RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false); + RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, true, style.FrameRounding); + RenderNavHighlight(frame_bb, id, nav_highlight_flags); + if (flags & ImGuiTreeNodeFlags_Bullet) + RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col); + else if (!is_leaf) + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 1.0f); + else // Leaf without bullet, left-adjusted text + text_pos.x -= text_offset_x - padding.x; + if (flags & ImGuiTreeNodeFlags_ClipLabelForTrailingButton) + frame_bb.Max.x -= g.FontSize + style.FramePadding.x; + if (g.LogEnabled) + LogSetNextTextDecoration("###", "###"); + } + else + { + // Unframed typed for tree nodes + if (hovered || selected) + { + const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false); + } + RenderNavHighlight(frame_bb, id, nav_highlight_flags); + if (flags & ImGuiTreeNodeFlags_Bullet) + RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); + else if (!is_leaf) + RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 0.70f); + if (g.LogEnabled) + LogSetNextTextDecoration(">", NULL); } - RenderNavHighlight(frame_bb, id, nav_highlight_flags); - if (flags & ImGuiTreeNodeFlags_Bullet) - RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); - else if (!is_leaf) - RenderArrow(window->DrawList, ImVec2(text_pos.x - text_offset_x + padding.x, text_pos.y + g.FontSize * 0.15f), text_col, is_open ? ((flags & ImGuiTreeNodeFlags_UpsideDownArrow) ? ImGuiDir_Up : ImGuiDir_Down) : ImGuiDir_Right, 0.70f); - if (g.LogEnabled) - LogSetNextTextDecoration(">", NULL); - } - if (span_all_columns) - TablePopBackgroundChannel(); + if (span_all_columns) + TablePopBackgroundChannel(); - // Label - if (display_frame) - RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); - else - RenderText(text_pos, label, label_end, false); + // Label + if (display_frame) + RenderTextClipped(text_pos, frame_bb.Max, label, label_end, &label_size); + else + RenderText(text_pos, label, label_end, false); + } + if (store_tree_node_stack_data && is_open) + TreeNodeStoreStackData(flags); // Call before TreePushOverrideID() if (is_open && !(flags & ImGuiTreeNodeFlags_NoTreePushOnOpen)) - TreePushOverrideID(id); + TreePushOverrideID(id); // Could use TreePush(label) but this avoid computing twice + IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | (is_leaf ? 0 : ImGuiItemStatusFlags_Openable) | (is_open ? ImGuiItemStatusFlags_Opened : 0)); return is_open; } @@ -6426,16 +6659,19 @@ void ImGui::TreePop() window->DC.TreeDepth--; ImU32 tree_depth_mask = (1 << window->DC.TreeDepth); - // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled) - if (window->DC.TreeJumpToParentOnPopMask & tree_depth_mask) // Only set during request + if (window->DC.TreeHasStackDataDepthMask & tree_depth_mask) // Only set during request { - ImGuiNavTreeNodeData* nav_tree_node_data = &g.NavTreeNodeStack.back(); - IM_ASSERT(nav_tree_node_data->ID == window->IDStack.back()); - if (g.NavIdIsAlive && g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) - NavMoveRequestResolveWithPastTreeNode(&g.NavMoveResultLocal, nav_tree_node_data); - g.NavTreeNodeStack.pop_back(); + ImGuiTreeNodeStackData* data = &g.TreeNodeStack.back(); + IM_ASSERT(data->ID == window->IDStack.back()); + if (data->TreeFlags & ImGuiTreeNodeFlags_NavLeftJumpsBackHere) + { + // Handle Left arrow to move to parent tree node (when ImGuiTreeNodeFlags_NavLeftJumpsBackHere is enabled) + if (g.NavIdIsAlive && g.NavMoveDir == ImGuiDir_Left && g.NavWindow == window && NavMoveRequestButNoResultYet()) + NavMoveRequestResolveWithPastTreeNode(&g.NavMoveResultLocal, data); + } + g.TreeNodeStack.pop_back(); + window->DC.TreeHasStackDataDepthMask &= ~tree_depth_mask; } - window->DC.TreeJumpToParentOnPopMask &= tree_depth_mask - 1; IM_ASSERT(window->IDStack.Size > 1); // There should always be 1 element in the IDStack (pushed during window creation). If this triggers you called TreePop/PopID too much. PopID(); @@ -6456,7 +6692,17 @@ void ImGui::SetNextItemOpen(bool is_open, ImGuiCond cond) return; g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasOpen; g.NextItemData.OpenVal = is_open; - g.NextItemData.OpenCond = cond ? cond : ImGuiCond_Always; + g.NextItemData.OpenCond = (ImU8)(cond ? cond : ImGuiCond_Always); +} + +// Set next TreeNode/CollapsingHeader storage id. +void ImGui::SetNextItemStorageID(ImGuiID storage_id) +{ + ImGuiContext& g = *GImGui; + if (g.CurrentWindow->SkipItems) + return; + g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasStorageID; + g.NextItemData.StorageId = storage_id; } // CollapsingHeader returns true when opened but do not indent nor push into the ID stack (because of the ImGuiTreeNodeFlags_NoTreePushOnOpen flag). @@ -6466,8 +6712,8 @@ bool ImGui::CollapsingHeader(const char* label, ImGuiTreeNodeFlags flags) ImGuiWindow* window = GetCurrentWindow(); if (window->SkipItems) return false; - - return TreeNodeBehavior(window->GetID(label), flags | ImGuiTreeNodeFlags_CollapsingHeader, label); + ImGuiID id = window->GetID(label); + return TreeNodeBehavior(id, flags | ImGuiTreeNodeFlags_CollapsingHeader, label); } // p_visible == NULL : regular collapsing header @@ -6547,6 +6793,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl const ImVec2 text_max(min_x + size.x, pos.y + size.y); // Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable. + // FIXME: Not part of layout so not included in clipper calculation, but ItemSize currently doesn't allow offsetting CursorPos. ImRect bb(min_x, pos.y, text_max.x, text_max.y); if ((flags & ImGuiSelectableFlags_NoPadWithHalfSpacing) == 0) { @@ -6561,25 +6808,29 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl } //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); } - // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. - const float backup_clip_rect_min_x = window->ClipRect.Min.x; - const float backup_clip_rect_max_x = window->ClipRect.Max.x; + const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0; + const ImGuiItemFlags extra_item_flags = disabled_item ? (ImGuiItemFlags)ImGuiItemFlags_Disabled : ImGuiItemFlags_None; + bool is_visible; if (span_all_columns) { + // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackgroundChannel for every Selectable.. + const float backup_clip_rect_min_x = window->ClipRect.Min.x; + const float backup_clip_rect_max_x = window->ClipRect.Max.x; window->ClipRect.Min.x = window->ParentWorkRect.Min.x; window->ClipRect.Max.x = window->ParentWorkRect.Max.x; - } - - const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0; - const bool item_add = ItemAdd(bb, id, NULL, disabled_item ? ImGuiItemFlags_Disabled : ImGuiItemFlags_None); - if (span_all_columns) - { + is_visible = ItemAdd(bb, id, NULL, extra_item_flags); window->ClipRect.Min.x = backup_clip_rect_min_x; window->ClipRect.Max.x = backup_clip_rect_max_x; } + else + { + is_visible = ItemAdd(bb, id, NULL, extra_item_flags); + } - if (!item_add) - return false; + const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; + if (!is_visible) + if (!is_multi_select || !g.BoxSelectState.UnclipMode || !g.BoxSelectState.UnclipRect.Overlaps(bb)) // Extra layer of "no logic clip" for box-select support (would be more overhead to add to ItemAdd) + return false; const bool disabled_global = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; if (disabled_item && !disabled_global) // Only testing this as an optimization @@ -6606,20 +6857,35 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } + // Multi-selection support (header) const bool was_selected = selected; + if (is_multi_select) + { + // Handle multi-select + alter button flags for it + MultiSelectItemHeader(id, &selected, &button_flags); + } + bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags); - // Auto-select when moved into - // - This will be more fully fleshed in the range-select branch - // - This is not exposed as it won't nicely work with some user side handling of shift/control - // - We cannot do 'if (g.NavJustMovedToId != id) { selected = false; pressed = was_selected; }' for two reasons - // - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope()) - // - (2) usage will fail with clipped items - // The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API. - if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == g.CurrentFocusScopeId) - if (g.NavJustMovedToId == id) - selected = pressed = true; + // Multi-selection support (footer) + if (is_multi_select) + { + MultiSelectItemFooter(id, &selected, &pressed); + } + else + { + // Auto-select when moved into + // - This will be more fully fleshed in the range-select branch + // - This is not exposed as it won't nicely work with some user side handling of shift/control + // - We cannot do 'if (g.NavJustMovedToId != id) { selected = false; pressed = was_selected; }' for two reasons + // - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope()) + // - (2) usage will fail with clipped items + // The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API. + if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == g.CurrentFocusScopeId) + if (g.NavJustMovedToId == id) + selected = pressed = true; + } // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) @@ -6633,18 +6899,31 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (pressed) MarkItemEdited(id); - // In this branch, Selectable() cannot toggle the selection so this will never trigger. - if (selected != was_selected) //-V547 + if (selected != was_selected) g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection; // Render - if (hovered || selected) + if (is_visible) { - const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); - RenderFrame(bb.Min, bb.Max, col, false, 0.0f); + const bool highlighted = hovered || (flags & ImGuiSelectableFlags_Highlight); + if (highlighted || selected) + { + // FIXME-MULTISELECT: Styling: Color for 'selected' elements? ImGuiCol_HeaderSelected + ImU32 col; + if (selected && !highlighted) + col = GetColorU32(ImLerp(GetStyleColorVec4(ImGuiCol_Header), GetStyleColorVec4(ImGuiCol_HeaderHovered), 0.5f)); + else + col = GetColorU32((held && highlighted) ? ImGuiCol_HeaderActive : highlighted ? ImGuiCol_HeaderHovered : ImGuiCol_Header); + RenderFrame(bb.Min, bb.Max, col, false, 0.0f); + } + if (g.NavId == id) + { + ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding; + if (is_multi_select) + nav_highlight_flags |= ImGuiNavHighlightFlags_AlwaysDraw; // Always show the nav rectangle + RenderNavHighlight(bb, id, nav_highlight_flags); + } } - if (g.NavId == id) - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding); if (span_all_columns) { @@ -6654,15 +6933,19 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl PopColumnsBackground(); } - RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); + if (is_visible) + RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); // Automatically close popups - if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.LastItemData.InFlags & ImGuiItemFlags_SelectableDontClosePopup)) + if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_NoAutoClosePopups) && (g.LastItemData.InFlags & ImGuiItemFlags_AutoClosePopups)) CloseCurrentPopup(); if (disabled_item && !disabled_global) EndDisabled(); + // Selectable() always returns a pressed state! + // Users of BeginMultiSelect()/EndMultiSelect() scope: you may call ImGui::IsItemToggledSelection() to retrieve + // selection toggle, only useful if you need that state updated (e.g. for rendering purpose) before reaching EndMultiSelect(). IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags); return pressed; //-V1020 } @@ -6740,7 +7023,7 @@ ImGuiTypingSelectRequest* ImGui::GetTypingSelectRequest(ImGuiTypingSelectFlags f g.IO.InputQueueCharacters.resize(0); // Handle backspace - if ((flags & ImGuiTypingSelectFlags_AllowBackspace) && IsKeyPressed(ImGuiKey_Backspace, 0, ImGuiInputFlags_Repeat)) + if ((flags & ImGuiTypingSelectFlags_AllowBackspace) && IsKeyPressed(ImGuiKey_Backspace, ImGuiInputFlags_Repeat)) { char* p = (char*)(void*)ImTextFindPreviousUtf8Codepoint(data->SearchBuffer, data->SearchBuffer + buffer_len); *p = 0; @@ -6798,7 +7081,7 @@ static int ImStrimatchlen(const char* s1, const char* s1_end, const char* s2) // When SingleCharMode is set: // - it is better to NOT display a tooltip of other on-screen display indicator. // - the index of the currently focused item is required. -// if your SetNextItemSelectionData() values are indices, you can obtain it from ImGuiMultiSelectIO::NavIdItem, otherwise from g.NavLastValidSelectionUserData. +// if your SetNextItemSelectionUserData() values are indices, you can obtain it from ImGuiMultiSelectIO::NavIdItem, otherwise from g.NavLastValidSelectionUserData. int ImGui::TypingSelectFindMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx) { if (req == NULL || req->SelectRequest == false) // Support NULL parameter so both calls can be done from same spot. @@ -6868,24 +7151,931 @@ void ImGui::DebugNodeTypingSelectState(ImGuiTypingSelectState* data) #endif } - //------------------------------------------------------------------------- -// [SECTION] Widgets: Multi-Select support +// [SECTION] Widgets: Box-Select support +// This has been extracted away from Multi-Select logic in the hope that it could eventually be used elsewhere, but hasn't been yet. +//------------------------------------------------------------------------- +// Extra logic in MultiSelectItemFooter() and ImGuiListClipper::Step() +//------------------------------------------------------------------------- +// - BoxSelectPreStartDrag() [Internal] +// - BoxSelectActivateDrag() [Internal] +// - BoxSelectDeactivateDrag() [Internal] +// - BoxSelectScrollWithMouseDrag() [Internal] +// - BeginBoxSelect() [Internal] +// - EndBoxSelect() [Internal] //------------------------------------------------------------------------- -void ImGui::SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data) +// Call on the initial click. +static void BoxSelectPreStartDrag(ImGuiID id, ImGuiSelectionUserData clicked_item) { - // Note that flags will be cleared by ItemAdd(), so it's only useful for Navigation code! - // This designed so widgets can also cheaply set this before calling ItemAdd(), so we are not tied to MultiSelect api. ImGuiContext& g = *GImGui; - g.NextItemData.ItemFlags |= ImGuiItemFlags_HasSelectionUserData; - g.NextItemData.SelectionUserData = selection_user_data; + ImGuiBoxSelectState* bs = &g.BoxSelectState; + bs->ID = id; + bs->IsStarting = true; // Consider starting box-select. + bs->IsStartedFromVoid = (clicked_item == ImGuiSelectionUserData_Invalid); + bs->IsStartedSetNavIdOnce = bs->IsStartedFromVoid; + bs->KeyMods = g.IO.KeyMods; + bs->StartPosRel = bs->EndPosRel = ImGui::WindowPosAbsToRel(g.CurrentWindow, g.IO.MousePos); + bs->ScrollAccum = ImVec2(0.0f, 0.0f); } +static void BoxSelectActivateDrag(ImGuiBoxSelectState* bs, ImGuiWindow* window) +{ + ImGuiContext& g = *GImGui; + IMGUI_DEBUG_LOG_SELECTION("[selection] BeginBoxSelect() 0X%08X: Activate\n", bs->ID); + bs->IsActive = true; + bs->Window = window; + bs->IsStarting = false; + ImGui::SetActiveID(bs->ID, window); + ImGui::SetActiveIdUsingAllKeyboardKeys(); + if (bs->IsStartedFromVoid && (bs->KeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0) + bs->RequestClear = true; +} + +static void BoxSelectDeactivateDrag(ImGuiBoxSelectState* bs) +{ + ImGuiContext& g = *GImGui; + bs->IsActive = bs->IsStarting = false; + if (g.ActiveId == bs->ID) + { + IMGUI_DEBUG_LOG_SELECTION("[selection] BeginBoxSelect() 0X%08X: Deactivate\n", bs->ID); + ImGui::ClearActiveID(); + } + bs->ID = 0; +} + +static void BoxSelectScrollWithMouseDrag(ImGuiBoxSelectState* bs, ImGuiWindow* window, const ImRect& inner_r) +{ + ImGuiContext& g = *GImGui; + IM_ASSERT(bs->Window == window); + for (int n = 0; n < 2; n++) // each axis + { + const float mouse_pos = g.IO.MousePos[n]; + const float dist = (mouse_pos > inner_r.Max[n]) ? mouse_pos - inner_r.Max[n] : (mouse_pos < inner_r.Min[n]) ? mouse_pos - inner_r.Min[n] : 0.0f; + const float scroll_curr = window->Scroll[n]; + if (dist == 0.0f || (dist < 0.0f && scroll_curr < 0.0f) || (dist > 0.0f && scroll_curr >= window->ScrollMax[n])) + continue; + + const float speed_multiplier = ImLinearRemapClamp(g.FontSize, g.FontSize * 5.0f, 1.0f, 4.0f, ImAbs(dist)); // x1 to x4 depending on distance + const float scroll_step = g.FontSize * 35.0f * speed_multiplier * ImSign(dist) * g.IO.DeltaTime; + bs->ScrollAccum[n] += scroll_step; + + // Accumulate into a stored value so we can handle high-framerate + const float scroll_step_i = ImFloor(bs->ScrollAccum[n]); + if (scroll_step_i == 0.0f) + continue; + if (n == 0) + ImGui::SetScrollX(window, scroll_curr + scroll_step_i); + else + ImGui::SetScrollY(window, scroll_curr + scroll_step_i); + bs->ScrollAccum[n] -= scroll_step_i; + } +} + +bool ImGui::BeginBoxSelect(const ImRect& scope_rect, ImGuiWindow* window, ImGuiID box_select_id, ImGuiMultiSelectFlags ms_flags) +{ + ImGuiContext& g = *GImGui; + ImGuiBoxSelectState* bs = &g.BoxSelectState; + KeepAliveID(box_select_id); + if (bs->ID != box_select_id) + return false; + + // IsStarting is set by MultiSelectItemFooter() when considering a possible box-select. We validate it here and lock geometry. + bs->UnclipMode = false; + bs->RequestClear = false; + if (bs->IsStarting && IsMouseDragPastThreshold(0)) + BoxSelectActivateDrag(bs, window); + else if ((bs->IsStarting || bs->IsActive) && g.IO.MouseDown[0] == false) + BoxSelectDeactivateDrag(bs); + if (!bs->IsActive) + return false; + + // Current frame absolute prev/current rectangles are used to toggle selection. + // They are derived from positions relative to scrolling space. + ImVec2 start_pos_abs = WindowPosRelToAbs(window, bs->StartPosRel); + ImVec2 prev_end_pos_abs = WindowPosRelToAbs(window, bs->EndPosRel); // Clamped already + ImVec2 curr_end_pos_abs = g.IO.MousePos; + if (ms_flags & ImGuiMultiSelectFlags_ScopeWindow) // Box-select scrolling only happens with ScopeWindow + curr_end_pos_abs = ImClamp(curr_end_pos_abs, scope_rect.Min, scope_rect.Max); + bs->BoxSelectRectPrev.Min = ImMin(start_pos_abs, prev_end_pos_abs); + bs->BoxSelectRectPrev.Max = ImMax(start_pos_abs, prev_end_pos_abs); + bs->BoxSelectRectCurr.Min = ImMin(start_pos_abs, curr_end_pos_abs); + bs->BoxSelectRectCurr.Max = ImMax(start_pos_abs, curr_end_pos_abs); + + // Box-select 2D mode detects horizontal changes (vertical ones are already picked by Clipper) + // Storing an extra rect used by widgets supporting box-select. + if (ms_flags & ImGuiMultiSelectFlags_BoxSelect2d) + if (bs->BoxSelectRectPrev.Min.x != bs->BoxSelectRectCurr.Min.x || bs->BoxSelectRectPrev.Max.x != bs->BoxSelectRectCurr.Max.x) + { + bs->UnclipMode = true; + bs->UnclipRect = bs->BoxSelectRectPrev; // FIXME-OPT: UnclipRect x coordinates could be intersection of Prev and Curr rect on X axis. + bs->UnclipRect.Add(bs->BoxSelectRectCurr); + } + + //GetForegroundDrawList()->AddRect(bs->UnclipRect.Min, bs->UnclipRect.Max, IM_COL32(255,0,0,200), 0.0f, 0, 3.0f); + //GetForegroundDrawList()->AddRect(bs->BoxSelectRectPrev.Min, bs->BoxSelectRectPrev.Max, IM_COL32(255,0,0,200), 0.0f, 0, 3.0f); + //GetForegroundDrawList()->AddRect(bs->BoxSelectRectCurr.Min, bs->BoxSelectRectCurr.Max, IM_COL32(0,255,0,200), 0.0f, 0, 1.0f); + return true; +} + +void ImGui::EndBoxSelect(const ImRect& scope_rect, ImGuiMultiSelectFlags ms_flags) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + ImGuiBoxSelectState* bs = &g.BoxSelectState; + IM_ASSERT(bs->IsActive); + bs->UnclipMode = false; + + // Render selection rectangle + bs->EndPosRel = WindowPosAbsToRel(window, ImClamp(g.IO.MousePos, scope_rect.Min, scope_rect.Max)); // Clamp stored position according to current scrolling view + ImRect box_select_r = bs->BoxSelectRectCurr; + box_select_r.ClipWith(scope_rect); + window->DrawList->AddRectFilled(box_select_r.Min, box_select_r.Max, GetColorU32(ImGuiCol_SeparatorHovered, 0.30f)); // FIXME-MULTISELECT: Styling + window->DrawList->AddRect(box_select_r.Min, box_select_r.Max, GetColorU32(ImGuiCol_NavHighlight)); // FIXME-MULTISELECT: Styling + + // Scroll + const bool enable_scroll = (ms_flags & ImGuiMultiSelectFlags_ScopeWindow) && (ms_flags & ImGuiMultiSelectFlags_BoxSelectNoScroll) == 0; + if (enable_scroll) + { + ImRect scroll_r = scope_rect; + scroll_r.Expand(-g.FontSize); + //GetForegroundDrawList()->AddRect(scroll_r.Min, scroll_r.Max, IM_COL32(0, 255, 0, 255)); + if (!scroll_r.Contains(g.IO.MousePos)) + BoxSelectScrollWithMouseDrag(bs, window, scroll_r); + } +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Multi-Select support +//------------------------------------------------------------------------- +// - DebugLogMultiSelectRequests() [Internal] +// - CalcScopeRect() [Internal] +// - BeginMultiSelect() +// - EndMultiSelect() +// - SetNextItemSelectionUserData() +// - MultiSelectItemHeader() [Internal] +// - MultiSelectItemFooter() [Internal] +// - DebugNodeMultiSelectState() [Internal] +//------------------------------------------------------------------------- + +static void DebugLogMultiSelectRequests(const char* function, const ImGuiMultiSelectIO* io) +{ + ImGuiContext& g = *GImGui; + for (const ImGuiSelectionRequest& req : io->Requests) + { + if (req.Type == ImGuiSelectionRequestType_SetAll) IMGUI_DEBUG_LOG_SELECTION("[selection] %s: Request: SetAll %d (= %s)\n", function, req.Selected, req.Selected ? "SelectAll" : "Clear"); + if (req.Type == ImGuiSelectionRequestType_SetRange) IMGUI_DEBUG_LOG_SELECTION("[selection] %s: Request: SetRange %" IM_PRId64 "..%" IM_PRId64 " (0x%" IM_PRIX64 "..0x%" IM_PRIX64 ") = %d (dir %d)\n", function, req.RangeFirstItem, req.RangeLastItem, req.RangeFirstItem, req.RangeLastItem, req.Selected, req.RangeDirection); + } +} + +static ImRect CalcScopeRect(ImGuiMultiSelectTempData* ms, ImGuiWindow* window) +{ + if (ms->Flags & ImGuiMultiSelectFlags_ScopeRect) + { + // Warning: this depends on CursorMaxPos so it means to be called by EndMultiSelect() only + return ImRect(ms->ScopeRectMin, ImMax(window->DC.CursorMaxPos, ms->ScopeRectMin)); + } + else + { + // Add inner table decoration (#7821) // FIXME: Why not baking in InnerClipRect? + ImRect scope_rect = window->InnerClipRect; + scope_rect.Min = ImMin(scope_rect.Min + ImVec2(window->DecoInnerSizeX1, window->DecoInnerSizeY1), scope_rect.Max); + return scope_rect; + } +} + +// Return ImGuiMultiSelectIO structure. +// Lifetime: don't hold on ImGuiMultiSelectIO* pointers over multiple frames or past any subsequent call to BeginMultiSelect() or EndMultiSelect(). +// Passing 'selection_size' and 'items_count' parameters is currently optional. +// - 'selection_size' is useful to disable some shortcut routing: e.g. ImGuiMultiSelectFlags_ClearOnEscape won't claim Escape key when selection_size 0, +// allowing a first press to clear selection THEN the second press to leave child window and return to parent. +// - 'items_count' is stored in ImGuiMultiSelectIO which makes it a convenient way to pass the information to your ApplyRequest() handler (but you may pass it differently). +// - If they are costly for you to compute (e.g. external intrusive selection without maintaining size), you may avoid them and pass -1. +// - If you can easily tell if your selection is empty or not, you may pass 0/1, or you may enable ImGuiMultiSelectFlags_ClearOnEscape flag dynamically. +ImGuiMultiSelectIO* ImGui::BeginMultiSelect(ImGuiMultiSelectFlags flags, int selection_size, int items_count) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + if (++g.MultiSelectTempDataStacked > g.MultiSelectTempData.Size) + g.MultiSelectTempData.resize(g.MultiSelectTempDataStacked, ImGuiMultiSelectTempData()); + ImGuiMultiSelectTempData* ms = &g.MultiSelectTempData[g.MultiSelectTempDataStacked - 1]; + IM_STATIC_ASSERT(offsetof(ImGuiMultiSelectTempData, IO) == 0); // Clear() relies on that. + g.CurrentMultiSelect = ms; + if ((flags & (ImGuiMultiSelectFlags_ScopeWindow | ImGuiMultiSelectFlags_ScopeRect)) == 0) + flags |= ImGuiMultiSelectFlags_ScopeWindow; + if (flags & ImGuiMultiSelectFlags_SingleSelect) + flags &= ~(ImGuiMultiSelectFlags_BoxSelect2d | ImGuiMultiSelectFlags_BoxSelect1d); + if (flags & ImGuiMultiSelectFlags_BoxSelect2d) + flags &= ~ImGuiMultiSelectFlags_BoxSelect1d; + + // FIXME: BeginFocusScope() + const ImGuiID id = window->IDStack.back(); + ms->Clear(); + ms->FocusScopeId = id; + ms->Flags = flags; + ms->IsFocused = (ms->FocusScopeId == g.NavFocusScopeId); + ms->BackupCursorMaxPos = window->DC.CursorMaxPos; + ms->ScopeRectMin = window->DC.CursorMaxPos = window->DC.CursorPos; + PushFocusScope(ms->FocusScopeId); + if (flags & ImGuiMultiSelectFlags_ScopeWindow) // Mark parent child window as navigable into, with highlight. Assume user will always submit interactive items. + window->DC.NavLayersActiveMask |= 1 << ImGuiNavLayer_Main; + + // Use copy of keyboard mods at the time of the request, otherwise we would requires mods to be held for an extra frame. + ms->KeyMods = g.NavJustMovedToId ? (g.NavJustMovedToIsTabbing ? 0 : g.NavJustMovedToKeyMods) : g.IO.KeyMods; + if (flags & ImGuiMultiSelectFlags_NoRangeSelect) + ms->KeyMods &= ~ImGuiMod_Shift; + + // Bind storage + ImGuiMultiSelectState* storage = g.MultiSelectStorage.GetOrAddByKey(id); + storage->ID = id; + storage->LastFrameActive = g.FrameCount; + storage->LastSelectionSize = selection_size; + storage->Window = window; + ms->Storage = storage; + + // Output to user + ms->IO.Requests.resize(0); + ms->IO.RangeSrcItem = storage->RangeSrcItem; + ms->IO.NavIdItem = storage->NavIdItem; + ms->IO.NavIdSelected = (storage->NavIdSelected == 1) ? true : false; + ms->IO.ItemsCount = items_count; + + // Clear when using Navigation to move within the scope + // (we compare FocusScopeId so it possible to use multiple selections inside a same window) + bool request_clear = false; + bool request_select_all = false; + if (g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == ms->FocusScopeId && g.NavJustMovedToHasSelectionData) + { + if (ms->KeyMods & ImGuiMod_Shift) + ms->IsKeyboardSetRange = true; + if (ms->IsKeyboardSetRange) + IM_ASSERT(storage->RangeSrcItem != ImGuiSelectionUserData_Invalid); // Not ready -> could clear? + if ((ms->KeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0 && (flags & (ImGuiMultiSelectFlags_NoAutoClear | ImGuiMultiSelectFlags_NoAutoSelect)) == 0) + request_clear = true; + } + else if (g.NavJustMovedFromFocusScopeId == ms->FocusScopeId) + { + // Also clear on leaving scope (may be optional?) + if ((ms->KeyMods & (ImGuiMod_Ctrl | ImGuiMod_Shift)) == 0 && (flags & (ImGuiMultiSelectFlags_NoAutoClear | ImGuiMultiSelectFlags_NoAutoSelect)) == 0) + request_clear = true; + } + + // Box-select handling: update active state. + ImGuiBoxSelectState* bs = &g.BoxSelectState; + if (flags & (ImGuiMultiSelectFlags_BoxSelect1d | ImGuiMultiSelectFlags_BoxSelect2d)) + { + ms->BoxSelectId = GetID("##BoxSelect"); + if (BeginBoxSelect(CalcScopeRect(ms, window), window, ms->BoxSelectId, flags)) + request_clear |= bs->RequestClear; + } + + if (ms->IsFocused) + { + // Shortcut: Clear selection (Escape) + // - Only claim shortcut if selection is not empty, allowing further presses on Escape to e.g. leave current child window. + // - Box select also handle Escape and needs to pass an id to bypass ActiveIdUsingAllKeyboardKeys lock. + if (flags & ImGuiMultiSelectFlags_ClearOnEscape) + { + if (selection_size != 0 || bs->IsActive) + if (Shortcut(ImGuiKey_Escape, ImGuiInputFlags_None, bs->IsActive ? bs->ID : 0)) + { + request_clear = true; + if (bs->IsActive) + BoxSelectDeactivateDrag(bs); + } + } + + // Shortcut: Select all (CTRL+A) + if (!(flags & ImGuiMultiSelectFlags_SingleSelect) && !(flags & ImGuiMultiSelectFlags_NoSelectAll)) + if (Shortcut(ImGuiMod_Ctrl | ImGuiKey_A)) + request_select_all = true; + } + + if (request_clear || request_select_all) + { + MultiSelectAddSetAll(ms, request_select_all); + if (!request_select_all) + storage->LastSelectionSize = 0; + } + ms->LoopRequestSetAll = request_select_all ? 1 : request_clear ? 0 : -1; + ms->LastSubmittedItem = ImGuiSelectionUserData_Invalid; + + if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection) + DebugLogMultiSelectRequests("BeginMultiSelect", &ms->IO); + + return &ms->IO; +} + +// Return updated ImGuiMultiSelectIO structure. +// Lifetime: don't hold on ImGuiMultiSelectIO* pointers over multiple frames or past any subsequent call to BeginMultiSelect() or EndMultiSelect(). +ImGuiMultiSelectIO* ImGui::EndMultiSelect() +{ + ImGuiContext& g = *GImGui; + ImGuiMultiSelectTempData* ms = g.CurrentMultiSelect; + ImGuiMultiSelectState* storage = ms->Storage; + ImGuiWindow* window = g.CurrentWindow; + IM_ASSERT(ms->FocusScopeId == g.CurrentFocusScopeId); + IM_ASSERT(g.CurrentMultiSelect != NULL && storage->Window == g.CurrentWindow); + IM_ASSERT(g.MultiSelectTempDataStacked > 0 && &g.MultiSelectTempData[g.MultiSelectTempDataStacked - 1] == g.CurrentMultiSelect); + + ImRect scope_rect = CalcScopeRect(ms, window); + if (ms->IsFocused) + { + // We currently don't allow user code to modify RangeSrcItem by writing to BeginIO's version, but that would be an easy change here. + if (ms->IO.RangeSrcReset || (ms->RangeSrcPassedBy == false && ms->IO.RangeSrcItem != ImGuiSelectionUserData_Invalid)) // Can't read storage->RangeSrcItem here -> we want the state at begining of the scope (see tests for easy failure) + { + IMGUI_DEBUG_LOG_SELECTION("[selection] EndMultiSelect: Reset RangeSrcItem.\n"); // Will set be to NavId. + storage->RangeSrcItem = ImGuiSelectionUserData_Invalid; + } + if (ms->NavIdPassedBy == false && storage->NavIdItem != ImGuiSelectionUserData_Invalid) + { + IMGUI_DEBUG_LOG_SELECTION("[selection] EndMultiSelect: Reset NavIdItem.\n"); + storage->NavIdItem = ImGuiSelectionUserData_Invalid; + storage->NavIdSelected = -1; + } + + if ((ms->Flags & (ImGuiMultiSelectFlags_BoxSelect1d | ImGuiMultiSelectFlags_BoxSelect2d)) && GetBoxSelectState(ms->BoxSelectId)) + EndBoxSelect(scope_rect, ms->Flags); + } + + if (ms->IsEndIO == false) + ms->IO.Requests.resize(0); + + // Clear selection when clicking void? + // We specifically test for IsMouseDragPastThreshold(0) == false to allow box-selection! + // The InnerRect test is necessary for non-child/decorated windows. + bool scope_hovered = IsWindowHovered() && window->InnerRect.Contains(g.IO.MousePos); + if (scope_hovered && (ms->Flags & ImGuiMultiSelectFlags_ScopeRect)) + scope_hovered &= scope_rect.Contains(g.IO.MousePos); + if (scope_hovered && g.HoveredId == 0 && g.ActiveId == 0) + { + if (ms->Flags & (ImGuiMultiSelectFlags_BoxSelect1d | ImGuiMultiSelectFlags_BoxSelect2d)) + { + if (!g.BoxSelectState.IsActive && !g.BoxSelectState.IsStarting && g.IO.MouseClickedCount[0] == 1) + { + BoxSelectPreStartDrag(ms->BoxSelectId, ImGuiSelectionUserData_Invalid); + FocusWindow(window, ImGuiFocusRequestFlags_UnlessBelowModal); + SetHoveredID(ms->BoxSelectId); + if (ms->Flags & ImGuiMultiSelectFlags_ScopeRect) + SetNavID(0, ImGuiNavLayer_Main, ms->FocusScopeId, ImRect(g.IO.MousePos, g.IO.MousePos)); // Automatically switch FocusScope for initial click from void to box-select. + } + } + + if (ms->Flags & ImGuiMultiSelectFlags_ClearOnClickVoid) + if (IsMouseReleased(0) && IsMouseDragPastThreshold(0) == false && g.IO.KeyMods == ImGuiMod_None) + MultiSelectAddSetAll(ms, false); + } + + // Courtesy nav wrapping helper flag + if (ms->Flags & ImGuiMultiSelectFlags_NavWrapX) + { + IM_ASSERT(ms->Flags & ImGuiMultiSelectFlags_ScopeWindow); // Only supported at window scope + ImGui::NavMoveRequestTryWrapping(ImGui::GetCurrentWindow(), ImGuiNavMoveFlags_WrapX); + } + + // Unwind + window->DC.CursorMaxPos = ImMax(ms->BackupCursorMaxPos, window->DC.CursorMaxPos); + PopFocusScope(); + + if (g.DebugLogFlags & ImGuiDebugLogFlags_EventSelection) + DebugLogMultiSelectRequests("EndMultiSelect", &ms->IO); + + ms->FocusScopeId = 0; + ms->Flags = ImGuiMultiSelectFlags_None; + g.CurrentMultiSelect = (--g.MultiSelectTempDataStacked > 0) ? &g.MultiSelectTempData[g.MultiSelectTempDataStacked - 1] : NULL; + + return &ms->IO; +} + +void ImGui::SetNextItemSelectionUserData(ImGuiSelectionUserData selection_user_data) +{ + // Note that flags will be cleared by ItemAdd(), so it's only useful for Navigation code! + // This designed so widgets can also cheaply set this before calling ItemAdd(), so we are not tied to MultiSelect api. + ImGuiContext& g = *GImGui; + g.NextItemData.SelectionUserData = selection_user_data; + g.NextItemData.FocusScopeId = g.CurrentFocusScopeId; + + if (ImGuiMultiSelectTempData* ms = g.CurrentMultiSelect) + { + // Auto updating RangeSrcPassedBy for cases were clipper is not used (done before ItemAdd() clipping) + g.NextItemData.ItemFlags |= ImGuiItemFlags_HasSelectionUserData | ImGuiItemFlags_IsMultiSelect; + if (ms->IO.RangeSrcItem == selection_user_data) + ms->RangeSrcPassedBy = true; + } + else + { + g.NextItemData.ItemFlags |= ImGuiItemFlags_HasSelectionUserData; + } +} + +// In charge of: +// - Applying SetAll for submitted items. +// - Applying SetRange for submitted items and record end points. +// - Altering button behavior flags to facilitate use with drag and drop. +void ImGui::MultiSelectItemHeader(ImGuiID id, bool* p_selected, ImGuiButtonFlags* p_button_flags) +{ + ImGuiContext& g = *GImGui; + ImGuiMultiSelectTempData* ms = g.CurrentMultiSelect; + + bool selected = *p_selected; + if (ms->IsFocused) + { + ImGuiMultiSelectState* storage = ms->Storage; + ImGuiSelectionUserData item_data = g.NextItemData.SelectionUserData; + IM_ASSERT(g.NextItemData.FocusScopeId == g.CurrentFocusScopeId && "Forgot to call SetNextItemSelectionUserData() prior to item, required in BeginMultiSelect()/EndMultiSelect() scope"); + + // Apply SetAll (Clear/SelectAll) requests requested by BeginMultiSelect(). + // This is only useful if the user hasn't processed them already, and this only works if the user isn't using the clipper. + // If you are using a clipper you need to process the SetAll request after calling BeginMultiSelect() + if (ms->LoopRequestSetAll != -1) + selected = (ms->LoopRequestSetAll == 1); + + // When using SHIFT+Nav: because it can incur scrolling we cannot afford a frame of lag with the selection highlight (otherwise scrolling would happen before selection) + // For this to work, we need someone to set 'RangeSrcPassedBy = true' at some point (either clipper either SetNextItemSelectionUserData() function) + if (ms->IsKeyboardSetRange) + { + IM_ASSERT(id != 0 && (ms->KeyMods & ImGuiMod_Shift) != 0); + const bool is_range_dst = (ms->RangeDstPassedBy == false) && g.NavJustMovedToId == id; // Assume that g.NavJustMovedToId is not clipped. + if (is_range_dst) + ms->RangeDstPassedBy = true; + if (is_range_dst && storage->RangeSrcItem == ImGuiSelectionUserData_Invalid) // If we don't have RangeSrc, assign RangeSrc = RangeDst + { + storage->RangeSrcItem = item_data; + storage->RangeSelected = selected ? 1 : 0; + } + const bool is_range_src = storage->RangeSrcItem == item_data; + if (is_range_src || is_range_dst || ms->RangeSrcPassedBy != ms->RangeDstPassedBy) + { + // Apply range-select value to visible items + IM_ASSERT(storage->RangeSrcItem != ImGuiSelectionUserData_Invalid && storage->RangeSelected != -1); + selected = (storage->RangeSelected != 0); + } + else if ((ms->KeyMods & ImGuiMod_Ctrl) == 0 && (ms->Flags & ImGuiMultiSelectFlags_NoAutoClear) == 0) + { + // Clear other items + selected = false; + } + } + *p_selected = selected; + } + + // Alter button behavior flags + // To handle drag and drop of multiple items we need to avoid clearing selection on click. + // Enabling this test makes actions using CTRL+SHIFT delay their effect on MouseUp which is annoying, but it allows drag and drop of multiple items. + if (p_button_flags != NULL) + { + ImGuiButtonFlags button_flags = *p_button_flags; + button_flags |= ImGuiButtonFlags_NoHoveredOnFocus; + if ((!selected || (g.ActiveId == id && g.ActiveIdHasBeenPressedBefore)) && !(ms->Flags & ImGuiMultiSelectFlags_SelectOnClickRelease)) + button_flags = (button_flags | ImGuiButtonFlags_PressedOnClick) & ~ImGuiButtonFlags_PressedOnClickRelease; + else + button_flags |= ImGuiButtonFlags_PressedOnClickRelease; + *p_button_flags = button_flags; + } +} + +// In charge of: +// - Auto-select on navigation. +// - Box-select toggle handling. +// - Right-click handling. +// - Altering selection based on Ctrl/Shift modifiers, both for keyboard and mouse. +// - Record current selection state for RangeSrc +// This is all rather complex, best to run and refer to "widgets_multiselect_xxx" tests in imgui_test_suite. +void ImGui::MultiSelectItemFooter(ImGuiID id, bool* p_selected, bool* p_pressed) +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + + bool selected = *p_selected; + bool pressed = *p_pressed; + ImGuiMultiSelectTempData* ms = g.CurrentMultiSelect; + ImGuiMultiSelectState* storage = ms->Storage; + if (pressed) + ms->IsFocused = true; + + bool hovered = false; + if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) + hovered = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); + if (!ms->IsFocused && !hovered) + return; + + ImGuiSelectionUserData item_data = g.NextItemData.SelectionUserData; + + ImGuiMultiSelectFlags flags = ms->Flags; + const bool is_singleselect = (flags & ImGuiMultiSelectFlags_SingleSelect) != 0; + bool is_ctrl = (ms->KeyMods & ImGuiMod_Ctrl) != 0; + bool is_shift = (ms->KeyMods & ImGuiMod_Shift) != 0; + + bool apply_to_range_src = false; + + if (g.NavId == id && storage->RangeSrcItem == ImGuiSelectionUserData_Invalid) + apply_to_range_src = true; + if (ms->IsEndIO == false) + { + ms->IO.Requests.resize(0); + ms->IsEndIO = true; + } + + // Auto-select as you navigate a list + if (g.NavJustMovedToId == id) + { + if ((flags & ImGuiMultiSelectFlags_NoAutoSelect) == 0) + { + if (is_ctrl && is_shift) + pressed = true; + else if (!is_ctrl) + selected = pressed = true; + } + else + { + // With NoAutoSelect, using Shift+keyboard performs a write/copy + if (is_shift) + pressed = true; + else if (!is_ctrl) + apply_to_range_src = true; // Since if (pressed) {} main block is not running we update this + } + } + + if (apply_to_range_src) + { + storage->RangeSrcItem = item_data; + storage->RangeSelected = selected; // Will be updated at the end of this function anyway. + } + + // Box-select toggle handling + if (ms->BoxSelectId != 0) + if (ImGuiBoxSelectState* bs = GetBoxSelectState(ms->BoxSelectId)) + { + const bool rect_overlap_curr = bs->BoxSelectRectCurr.Overlaps(g.LastItemData.Rect); + const bool rect_overlap_prev = bs->BoxSelectRectPrev.Overlaps(g.LastItemData.Rect); + if ((rect_overlap_curr && !rect_overlap_prev && !selected) || (rect_overlap_prev && !rect_overlap_curr)) + { + if (storage->LastSelectionSize <= 0 && bs->IsStartedSetNavIdOnce) + { + pressed = true; // First item act as a pressed: code below will emit selection request and set NavId (whatever we emit here will be overridden anyway) + bs->IsStartedSetNavIdOnce = false; + } + else + { + selected = !selected; + MultiSelectAddSetRange(ms, selected, +1, item_data, item_data); + } + storage->LastSelectionSize = ImMax(storage->LastSelectionSize + 1, 1); + } + } + + // Right-click handling. + // FIXME-MULTISELECT: Currently filtered out by ImGuiMultiSelectFlags_NoAutoSelect but maybe should be moved to Selectable(). See https://github.com/ocornut/imgui/pull/5816 + if (hovered && IsMouseClicked(1) && (flags & ImGuiMultiSelectFlags_NoAutoSelect) == 0) + { + if (g.ActiveId != 0 && g.ActiveId != id) + ClearActiveID(); + SetFocusID(id, window); + if (!pressed && !selected) + { + pressed = true; + is_ctrl = is_shift = false; + } + } + + // Unlike Space, Enter doesn't alter selection (but can still return a press) unless current item is not selected. + // The later, "unless current item is not select", may become optional? It seems like a better default if Enter doesn't necessarily open something + // (unlike e.g. Windows explorer). For use case where Enter always open something, we might decide to make this optional? + const bool enter_pressed = pressed && (g.NavActivateId == id) && (g.NavActivateFlags & ImGuiActivateFlags_PreferInput); + + // Alter selection + if (pressed && (!enter_pressed || !selected)) + { + // Box-select + ImGuiInputSource input_source = (g.NavJustMovedToId == id || g.NavActivateId == id) ? g.NavInputSource : ImGuiInputSource_Mouse; + if (flags & (ImGuiMultiSelectFlags_BoxSelect1d | ImGuiMultiSelectFlags_BoxSelect2d)) + if (selected == false && !g.BoxSelectState.IsActive && !g.BoxSelectState.IsStarting && input_source == ImGuiInputSource_Mouse && g.IO.MouseClickedCount[0] == 1) + BoxSelectPreStartDrag(ms->BoxSelectId, item_data); + + //---------------------------------------------------------------------------------------- + // ACTION | Begin | Pressed/Activated | End + //---------------------------------------------------------------------------------------- + // Keys Navigated: | Clear | Src=item, Sel=1 SetRange 1 + // Keys Navigated: Ctrl | n/a | n/a + // Keys Navigated: Shift | n/a | Dst=item, Sel=1, => Clear + SetRange 1 + // Keys Navigated: Ctrl+Shift | n/a | Dst=item, Sel=Src => Clear + SetRange Src-Dst + // Keys Activated: | n/a | Src=item, Sel=1 => Clear + SetRange 1 + // Keys Activated: Ctrl | n/a | Src=item, Sel=!Sel => SetSange 1 + // Keys Activated: Shift | n/a | Dst=item, Sel=1 => Clear + SetSange 1 + //---------------------------------------------------------------------------------------- + // Mouse Pressed: | n/a | Src=item, Sel=1, => Clear + SetRange 1 + // Mouse Pressed: Ctrl | n/a | Src=item, Sel=!Sel => SetRange 1 + // Mouse Pressed: Shift | n/a | Dst=item, Sel=1, => Clear + SetRange 1 + // Mouse Pressed: Ctrl+Shift | n/a | Dst=item, Sel=!Sel => SetRange Src-Dst + //---------------------------------------------------------------------------------------- + + if ((flags & ImGuiMultiSelectFlags_NoAutoClear) == 0) + { + bool request_clear = false; + if (is_singleselect) + request_clear = true; + else if ((input_source == ImGuiInputSource_Mouse || g.NavActivateId == id) && !is_ctrl) + request_clear = (flags & ImGuiMultiSelectFlags_NoAutoClearOnReselect) ? !selected : true; + else if ((input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Gamepad) && is_shift && !is_ctrl) + request_clear = true; // With is_shift==false the RequestClear was done in BeginIO, not necessary to do again. + if (request_clear) + MultiSelectAddSetAll(ms, false); + } + + int range_direction; + bool range_selected; + if (is_shift && !is_singleselect) + { + //IM_ASSERT(storage->HasRangeSrc && storage->HasRangeValue); + if (storage->RangeSrcItem == ImGuiSelectionUserData_Invalid) + storage->RangeSrcItem = item_data; + if ((flags & ImGuiMultiSelectFlags_NoAutoSelect) == 0) + { + // Shift+Arrow always select + // Ctrl+Shift+Arrow copy source selection state (already stored by BeginMultiSelect() in storage->RangeSelected) + range_selected = (is_ctrl && storage->RangeSelected != -1) ? (storage->RangeSelected != 0) : true; + } + else + { + // Shift+Arrow copy source selection state + // Shift+Click always copy from target selection state + if (ms->IsKeyboardSetRange) + range_selected = (storage->RangeSelected != -1) ? (storage->RangeSelected != 0) : true; + else + range_selected = !selected; + } + range_direction = ms->RangeSrcPassedBy ? +1 : -1; + } + else + { + // Ctrl inverts selection, otherwise always select + if ((flags & ImGuiMultiSelectFlags_NoAutoSelect) == 0) + selected = is_ctrl ? !selected : true; + else + selected = !selected; + storage->RangeSrcItem = item_data; + range_selected = selected; + range_direction = +1; + } + MultiSelectAddSetRange(ms, range_selected, range_direction, storage->RangeSrcItem, item_data); + } + + // Update/store the selection state of the Source item (used by CTRL+SHIFT, when Source is unselected we perform a range unselect) + if (storage->RangeSrcItem == item_data) + storage->RangeSelected = selected ? 1 : 0; + + // Update/store the selection state of focused item + if (g.NavId == id) + { + storage->NavIdItem = item_data; + storage->NavIdSelected = selected ? 1 : 0; + } + if (storage->NavIdItem == item_data) + ms->NavIdPassedBy = true; + ms->LastSubmittedItem = item_data; + + *p_selected = selected; + *p_pressed = pressed; +} + +void ImGui::MultiSelectAddSetAll(ImGuiMultiSelectTempData* ms, bool selected) +{ + ImGuiSelectionRequest req = { ImGuiSelectionRequestType_SetAll, selected, 0, ImGuiSelectionUserData_Invalid, ImGuiSelectionUserData_Invalid }; + ms->IO.Requests.resize(0); // Can always clear previous requests + ms->IO.Requests.push_back(req); // Add new request +} + +void ImGui::MultiSelectAddSetRange(ImGuiMultiSelectTempData* ms, bool selected, int range_dir, ImGuiSelectionUserData first_item, ImGuiSelectionUserData last_item) +{ + // Merge contiguous spans into same request (unless NoRangeSelect is set which guarantees single-item ranges) + if (ms->IO.Requests.Size > 0 && first_item == last_item && (ms->Flags & ImGuiMultiSelectFlags_NoRangeSelect) == 0) + { + ImGuiSelectionRequest* prev = &ms->IO.Requests.Data[ms->IO.Requests.Size - 1]; + if (prev->Type == ImGuiSelectionRequestType_SetRange && prev->RangeLastItem == ms->LastSubmittedItem && prev->Selected == selected) + { + prev->RangeLastItem = last_item; + return; + } + } + + ImGuiSelectionRequest req = { ImGuiSelectionRequestType_SetRange, selected, (ImS8)range_dir, (range_dir > 0) ? first_item : last_item, (range_dir > 0) ? last_item : first_item }; + ms->IO.Requests.push_back(req); // Add new request +} + +void ImGui::DebugNodeMultiSelectState(ImGuiMultiSelectState* storage) +{ +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + const bool is_active = (storage->LastFrameActive >= GetFrameCount() - 2); // Note that fully clipped early out scrolling tables will appear as inactive here. + if (!is_active) { PushStyleColor(ImGuiCol_Text, GetStyleColorVec4(ImGuiCol_TextDisabled)); } + bool open = TreeNode((void*)(intptr_t)storage->ID, "MultiSelect 0x%08X in '%s'%s", storage->ID, storage->Window ? storage->Window->Name : "N/A", is_active ? "" : " *Inactive*"); + if (!is_active) { PopStyleColor(); } + if (!open) + return; + Text("RangeSrcItem = %" IM_PRId64 " (0x%" IM_PRIX64 "), RangeSelected = %d", storage->RangeSrcItem, storage->RangeSrcItem, storage->RangeSelected); + Text("NavIdItem = %" IM_PRId64 " (0x%" IM_PRIX64 "), NavIdSelected = %d", storage->NavIdItem, storage->NavIdItem, storage->NavIdSelected); + Text("LastSelectionSize = %d", storage->LastSelectionSize); // Provided by user + TreePop(); +#else + IM_UNUSED(storage); +#endif +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: Multi-Select helpers +//------------------------------------------------------------------------- +// - ImGuiSelectionBasicStorage +// - ImGuiSelectionExternalStorage +//------------------------------------------------------------------------- + +ImGuiSelectionBasicStorage::ImGuiSelectionBasicStorage() +{ + Size = 0; + PreserveOrder = false; + UserData = NULL; + AdapterIndexToStorageId = [](ImGuiSelectionBasicStorage*, int idx) { return (ImGuiID)idx; }; + _SelectionOrder = 1; // Always >0 +} + +void ImGuiSelectionBasicStorage::Clear() +{ + Size = 0; + _SelectionOrder = 1; // Always >0 + _Storage.Data.resize(0); +} + +void ImGuiSelectionBasicStorage::Swap(ImGuiSelectionBasicStorage& r) +{ + ImSwap(Size, r.Size); + ImSwap(_SelectionOrder, r._SelectionOrder); + _Storage.Data.swap(r._Storage.Data); +} + +bool ImGuiSelectionBasicStorage::Contains(ImGuiID id) const +{ + return _Storage.GetInt(id, 0) != 0; +} + +static int IMGUI_CDECL PairComparerByValueInt(const void* lhs, const void* rhs) +{ + int lhs_v = ((const ImGuiStoragePair*)lhs)->val_i; + int rhs_v = ((const ImGuiStoragePair*)rhs)->val_i; + return (lhs_v > rhs_v ? +1 : lhs_v < rhs_v ? -1 : 0); +} + +// GetNextSelectedItem() is an abstraction allowing us to change our underlying actual storage system without impacting user. +// (e.g. store unselected vs compact down, compact down on demand, use raw ImVector instead of ImGuiStorage...) +bool ImGuiSelectionBasicStorage::GetNextSelectedItem(void** opaque_it, ImGuiID* out_id) +{ + ImGuiStoragePair* it = (ImGuiStoragePair*)*opaque_it; + ImGuiStoragePair* it_end = _Storage.Data.Data + _Storage.Data.Size; + if (PreserveOrder && it == NULL && it_end != NULL) + ImQsort(_Storage.Data.Data, (size_t)_Storage.Data.Size, sizeof(ImGuiStoragePair), PairComparerByValueInt); // ~ImGuiStorage::BuildSortByValueInt() + if (it == NULL) + it = _Storage.Data.Data; + IM_ASSERT(it >= _Storage.Data.Data && it <= it_end); + if (it != it_end) + while (it->val_i == 0 && it < it_end) + it++; + const bool has_more = (it != it_end); + *opaque_it = has_more ? (void**)(it + 1) : (void**)(it); + *out_id = has_more ? it->key : 0; + if (PreserveOrder && !has_more) + _Storage.BuildSortByKey(); + return has_more; +} + +void ImGuiSelectionBasicStorage::SetItemSelected(ImGuiID id, bool selected) +{ + int* p_int = _Storage.GetIntRef(id, 0); + if (selected && *p_int == 0) { *p_int = _SelectionOrder++; Size++; } + else if (!selected && *p_int != 0) { *p_int = 0; Size--; } +} + +// Optimized for batch edits (with same value of 'selected') +static void ImGuiSelectionBasicStorage_BatchSetItemSelected(ImGuiSelectionBasicStorage* selection, ImGuiID id, bool selected, int size_before_amends, int selection_order) +{ + ImGuiStorage* storage = &selection->_Storage; + ImGuiStoragePair* it = ImLowerBound(storage->Data.Data, storage->Data.Data + size_before_amends, id); + const bool is_contained = (it != storage->Data.Data + size_before_amends) && (it->key == id); + if (selected == (is_contained && it->val_i != 0)) + return; + if (selected && !is_contained) + storage->Data.push_back(ImGuiStoragePair(id, selection_order)); // Push unsorted at end of vector, will be sorted in SelectionMultiAmendsFinish() + else if (is_contained) + it->val_i = selected ? selection_order : 0; // Modify in-place. + selection->Size += selected ? +1 : -1; +} + +static void ImGuiSelectionBasicStorage_BatchFinish(ImGuiSelectionBasicStorage* selection, bool selected, int size_before_amends) +{ + ImGuiStorage* storage = &selection->_Storage; + if (selected && selection->Size != size_before_amends) + storage->BuildSortByKey(); // When done selecting: sort everything +} + +// Apply requests coming from BeginMultiSelect() and EndMultiSelect(). +// - Enable 'Demo->Tools->Debug Log->Selection' to see selection requests as they happen. +// - Honoring SetRange requests requires that you can iterate/interpolate between RangeFirstItem and RangeLastItem. +// - In this demo we often submit indices to SetNextItemSelectionUserData() + store the same indices in persistent selection. +// - Your code may do differently. If you store pointers or objects ID in ImGuiSelectionUserData you may need to perform +// a lookup in order to have some way to iterate/interpolate between two items. +// - A full-featured application is likely to allow search/filtering which is likely to lead to using indices +// and constructing a view index <> object id/ptr data structure anyway. +// WHEN YOUR APPLICATION SETTLES ON A CHOICE, YOU WILL PROBABLY PREFER TO GET RID OF THIS UNNECESSARY 'ImGuiSelectionBasicStorage' INDIRECTION LOGIC. +// Notice that with the simplest adapter (using indices everywhere), all functions return their parameters. +// The most simple implementation (using indices everywhere) would look like: +// for (ImGuiSelectionRequest& req : ms_io->Requests) +// { +// if (req.Type == ImGuiSelectionRequestType_SetAll) { Clear(); if (req.Selected) { for (int n = 0; n < items_count; n++) { SetItemSelected(n, true); } } +// if (req.Type == ImGuiSelectionRequestType_SetRange) { for (int n = (int)ms_io->RangeFirstItem; n <= (int)ms_io->RangeLastItem; n++) { SetItemSelected(n, ms_io->Selected); } } +// } +void ImGuiSelectionBasicStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io) +{ + // For convenience we obtain ItemsCount as passed to BeginMultiSelect(), which is optional. + // It makes sense when using ImGuiSelectionBasicStorage to simply pass your items count to BeginMultiSelect(). + // Other scheme may handle SetAll differently. + IM_ASSERT(ms_io->ItemsCount != -1 && "Missing value for items_count in BeginMultiSelect() call!"); + IM_ASSERT(AdapterIndexToStorageId != NULL); + + // This is optimized/specialized to cope with very large selections (e.g. 100k+ items) + // - A simpler version could call SetItemSelected() directly instead of ImGuiSelectionBasicStorage_BatchSetItemSelected() + ImGuiSelectionBasicStorage_BatchFinish(). + // - Optimized select can append unsorted, then sort in a second pass. Optimized unselect can clear in-place then compact in a second pass. + // - A more optimal version wouldn't even use ImGuiStorage but directly a ImVector to reduce bandwidth, but this is a reasonable trade off to reuse code. + // - There are many ways this could be better optimized. The worse case scenario being: using BoxSelect2d in a grid, box-select scrolling down while wiggling + // left and right: it affects coarse clipping + can emit multiple SetRange with 1 item each.) + // FIXME-OPT: For each block of consecutive SetRange request: + // - add all requests to a sorted list, store ID, selected, offset in ImGuiStorage. + // - rewrite sorted storage a single time. + for (ImGuiSelectionRequest& req : ms_io->Requests) + { + if (req.Type == ImGuiSelectionRequestType_SetAll) + { + Clear(); + if (req.Selected) + { + _Storage.Data.reserve(ms_io->ItemsCount); + const int size_before_amends = _Storage.Data.Size; + for (int idx = 0; idx < ms_io->ItemsCount; idx++, _SelectionOrder++) + ImGuiSelectionBasicStorage_BatchSetItemSelected(this, GetStorageIdFromIndex(idx), req.Selected, size_before_amends, _SelectionOrder); + ImGuiSelectionBasicStorage_BatchFinish(this, req.Selected, size_before_amends); + } + } + else if (req.Type == ImGuiSelectionRequestType_SetRange) + { + const int selection_changes = (int)req.RangeLastItem - (int)req.RangeFirstItem + 1; + //ImGuiContext& g = *GImGui; IMGUI_DEBUG_LOG_SELECTION("Req %d/%d: set %d to %d\n", ms_io->Requests.index_from_ptr(&req), ms_io->Requests.Size, selection_changes, req.Selected); + if (selection_changes == 1 || (selection_changes < Size / 100)) + { + // Multiple sorted insertion + copy likely to be faster. + // Technically we could do a single copy with a little more work (sort sequential SetRange requests) + for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++) + SetItemSelected(GetStorageIdFromIndex(idx), req.Selected); + } + else + { + // Append insertion + single sort likely be faster. + // Use req.RangeDirection to set order field so that shift+clicking from 1 to 5 is different than shift+clicking from 5 to 1 + const int size_before_amends = _Storage.Data.Size; + int selection_order = _SelectionOrder + ((req.RangeDirection < 0) ? selection_changes - 1 : 0); + for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++, selection_order += req.RangeDirection) + ImGuiSelectionBasicStorage_BatchSetItemSelected(this, GetStorageIdFromIndex(idx), req.Selected, size_before_amends, selection_order); + if (req.Selected) + _SelectionOrder += selection_changes; + ImGuiSelectionBasicStorage_BatchFinish(this, req.Selected, size_before_amends); + } + } + } +} //------------------------------------------------------------------------- -// [SECTION] Widgets: ListBox -//------------------------------------------------------------------------- + +ImGuiSelectionExternalStorage::ImGuiSelectionExternalStorage() +{ + UserData = NULL; + AdapterSetItemSelected = NULL; +} + +// Apply requests coming from BeginMultiSelect() and EndMultiSelect(). +// We also pull 'ms_io->ItemsCount' as passed for BeginMultiSelect() for consistency with ImGuiSelectionBasicStorage +// This makes no assumption about underlying storage. +void ImGuiSelectionExternalStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io) +{ + IM_ASSERT(AdapterSetItemSelected); + for (ImGuiSelectionRequest& req : ms_io->Requests) + { + if (req.Type == ImGuiSelectionRequestType_SetAll) + for (int idx = 0; idx < ms_io->ItemsCount; idx++) + AdapterSetItemSelected(this, idx, req.Selected); + if (req.Type == ImGuiSelectionRequestType_SetRange) + for (int idx = (int)req.RangeFirstItem; idx <= (int)req.RangeLastItem; idx++) + AdapterSetItemSelected(this, idx, req.Selected); + } +} + +//------------------------------------------------------------------------- +// [SECTION] Widgets: ListBox +//------------------------------------------------------------------------- // - BeginListBox() // - EndListBox() // - ListBox() @@ -6928,6 +8118,7 @@ bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) ImVec2 label_pos = ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y); RenderText(label_pos, label); window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, label_pos + label_size); + AlignTextToFramePadding(); } BeginChild(id, frame_bb.GetSize(), ImGuiChildFlags_FrameStyle); @@ -6971,6 +8162,7 @@ bool ImGui::ListBox(const char* label, int* current_item, const char* (*getter)( bool value_changed = false; ImGuiListClipper clipper; clipper.Begin(items_count, GetTextLineHeightWithSpacing()); // We know exactly our line height here so we pass it as a minor optimization, but generally you don't need to. + clipper.IncludeItemByIndex(*current_item); while (clipper.Step()) for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) { @@ -7483,13 +8675,13 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) bool pressed; // We use ImGuiSelectableFlags_NoSetKeyOwner to allow down on one menu item, move, up on another. - const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_NoSetKeyOwner | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups; + const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_NoSetKeyOwner | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_NoAutoClosePopups; if (window->DC.LayoutType == ImGuiLayoutType_Horizontal) { // Menu inside an horizontal menu bar // Selectable extend their highlight by half ItemSpacing in each direction. // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() - popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight()); + popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight); window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f); PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); float w = label_size.x; @@ -7859,7 +9051,9 @@ bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags) tab_bar->ID = id; tab_bar->SeparatorMinX = tab_bar->BarRect.Min.x - IM_TRUNC(window->WindowPadding.x * 0.5f); tab_bar->SeparatorMaxX = tab_bar->BarRect.Max.x + IM_TRUNC(window->WindowPadding.x * 0.5f); - return BeginTabBarEx(tab_bar, tab_bar_bb, flags | ImGuiTabBarFlags_IsFocused); + //if (g.NavWindow && IsWindowChildOf(g.NavWindow, window, false, false)) + flags |= ImGuiTabBarFlags_IsFocused; + return BeginTabBarEx(tab_bar, tab_bar_bb, flags); } bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImGuiTabBarFlags flags) @@ -7913,7 +9107,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG // Draw separator // (it would be misleading to draw this in EndTabBar() suggesting that it may be drawn over tabs, as tab bar are appendable) - const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive); + const ImU32 col = GetColorU32((flags & ImGuiTabBarFlags_IsFocused) ? ImGuiCol_TabSelected : ImGuiCol_TabDimmedSelected); if (g.Style.TabBarBorderSize > 0.0f) { const float y = tab_bar->BarRect.Max.y; @@ -8255,7 +9449,7 @@ ImGuiTabItem* ImGui::TabBarFindTabByOrder(ImGuiTabBar* tab_bar, int order) ImGuiTabItem* ImGui::TabBarGetCurrentTab(ImGuiTabBar* tab_bar) { - if (tab_bar->LastTabItemIdx <= 0 || tab_bar->LastTabItemIdx >= tab_bar->Tabs.Size) + if (tab_bar->LastTabItemIdx < 0 || tab_bar->LastTabItemIdx >= tab_bar->Tabs.Size) return NULL; return &tab_bar->Tabs[tab_bar->LastTabItemIdx]; } @@ -8773,8 +9967,16 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Render tab shape ImDrawList* display_draw_list = window->DrawList; - const ImU32 tab_col = GetColorU32((held || hovered) ? ImGuiCol_TabHovered : tab_contents_visible ? (tab_bar_focused ? ImGuiCol_TabActive : ImGuiCol_TabUnfocusedActive) : (tab_bar_focused ? ImGuiCol_Tab : ImGuiCol_TabUnfocused)); + const ImU32 tab_col = GetColorU32((held || hovered) ? ImGuiCol_TabHovered : tab_contents_visible ? (tab_bar_focused ? ImGuiCol_TabSelected : ImGuiCol_TabDimmedSelected) : (tab_bar_focused ? ImGuiCol_Tab : ImGuiCol_TabDimmed)); TabItemBackground(display_draw_list, bb, flags, tab_col); + if (tab_contents_visible && (tab_bar->Flags & ImGuiTabBarFlags_DrawSelectedOverline) && style.TabBarOverlineSize > 0.0f) + { + float x_offset = IM_TRUNC(0.4f * style.TabRounding); + if (x_offset < 2.0f * g.CurrentDpiScale) + x_offset = 0.0f; + float y_offset = 1.0f * g.CurrentDpiScale; + display_draw_list->AddLine(bb.GetTL() + ImVec2(x_offset, y_offset), bb.GetTR() + ImVec2(-x_offset, y_offset), GetColorU32(tab_bar_focused ? ImGuiCol_TabSelectedOverline : ImGuiCol_TabDimmedSelectedOverline), style.TabBarOverlineSize); + } RenderNavHighlight(bb, id); // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. From 22fb92cc6cca3129280dd8b524ef905874e146f1 Mon Sep 17 00:00:00 2001 From: "Luciano A. Romero Calla" Date: Wed, 21 Aug 2024 14:36:27 +0200 Subject: [PATCH 0999/1018] Update build.yml --- .github/workflows/build.yml | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ae97f100..97fd5eb7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -4,9 +4,6 @@ on: [push] env: CUDA_BIN: /usr/local/cuda/bin - CC: gcc-12 - CXX: g++-12 - CUDAHOSTCXX: g++-12 jobs: build: @@ -16,7 +13,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-22.04] + os: [ubuntu-24.04] config: [Release, Debug] cuda: [cuda, ''] @@ -25,34 +22,24 @@ jobs: - name: Install Dependencies run: | - sudo apt remove --purge cmake - sudo snap install cmake --classic sudo apt update sudo apt install \ libarmadillo-dev \ - libeigen3-dev \ libsuitesparse-dev \ libopenblas-dev \ libglew-dev \ libglfw3-dev \ + libflann-dev \ cimg-dev \ - g++-12 \ wget - wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann-dev_1.9.2+dfsg-1_amd64.deb - wget http://mirrors.kernel.org/ubuntu/pool/universe/f/flann/libflann1.9_1.9.2+dfsg-1_amd64.deb - sudo dpkg -i libflann1.9_1.9.2+dfsg-1_amd64.deb || sudo apt install -f - sudo dpkg -i libflann-dev_1.9.2+dfsg-1_amd64.deb || sudo apt install -f - - name: Install Cuda if: matrix.cuda == 'cuda' run: | - wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin - sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600 - sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub - sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /" - sudo apt-get update - sudo apt-get -y install cuda + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb + sudo dpkg -i cuda-keyring_1.1-1_all.deb + sudo apt update + sudo apt install -y cuda-toolkit - name: Install Embree working-directory: /opt From b9168259ae3b2ab287bd961562ebc1735f87267b Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 10 Sep 2024 22:18:14 +0200 Subject: [PATCH 1000/1018] viewer: add merge action to the list of removed meshes (merge with the selected) --- src/gproshan/viewer/viewer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index c694a18e..bbbb083b 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -86,7 +86,6 @@ viewer::viewer(const char * title, const int width, const int height) viewer::~viewer() { - update_status_message("frametime_%p", this); save_frametime(tmp_file_path(status_message)); delete sphere; @@ -360,13 +359,19 @@ void viewer::imgui() update_viewport_meshes(); } ImGui::SameLine(); + if(ImGui::Button("merge")) + { + add_mesh(mesh->merge(*m)); + } + ImGui::SameLine(); if(ImGui::Button("delete")) { delete m; removed_meshes.erase(begin(removed_meshes) + i); } ImGui::SameLine(); - ImGui::Selectable((*m)->filename.c_str()); + const int p = size((*m)->filename) - 27; + ImGui::Selectable(((p < 0 ? "" : "<<") + (*m)->filename.substr(p < 0 ? 0 : p)).c_str()); ImGui::PopID(); } } From 67504c7f16eb444a9035127a3e35c6e0745c8f56 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 19 Sep 2024 16:48:14 +0200 Subject: [PATCH 1001/1018] knn: voronoi radius approximation --- include/gproshan/pointcloud/knn.h | 1 + include/gproshan/raytracing/embree.h | 3 ++- src/gproshan/pointcloud/knn.cpp | 12 ++++++++++++ src/gproshan/raytracing/embree.cpp | 4 ++++ src/gproshan/viewer/viewer.cpp | 2 +- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index c1a2393a..4793b441 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -75,6 +75,7 @@ float mean_knn_area_radius(const point * pc, const size_t n_points, const size_t float median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); +float voronoi_radius(const point * pc, const int * id, const size_t n, const mat4 & model_mat); float median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat); float mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat); diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index 7b4f9f6a..b707bfab 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -22,7 +22,8 @@ class embree : public raytracing MEAN, MEDIAN, AREA, - MEDIAN_PAIRS + MEDIAN_PAIRS, + VORONOI }; struct pc_opts diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 618a99a6..ecf8f7f9 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -1,6 +1,7 @@ #include #include +#include #include @@ -261,6 +262,17 @@ float median_knn_area_radius(const point * pc, const size_t n_points, const size } +float voronoi_radius(const point * pc, const int * id, const size_t n, const mat4 & model_mat) +{ + float r = 0; + + for(index_t i = 1; i < n; ++i) + for(index_t j = i + 1; j < n; ++j) + r = std::max(r, length(model_mat * ((pc[id[0]] + pc[id[i]] + pc[id[j]]) / 3 - pc[id[0]], 0))); + + return r; +} + float median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat) { std::vector dist; diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index a5893e88..9d1583db 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -245,6 +245,10 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p case MEDIAN_PAIRS: r = knn::median_pair_dist(&mesh->point(0), (*nn)(i), pc.knn, model_mat); break; + + case VORONOI: + r = knn::voronoi_radius(&mesh->point(0), (*nn)(i), pc.knn, model_mat); + break; }; pxyzr[i][3] = pc.scale * r; diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index bbbb083b..5088ca88 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -1000,7 +1000,7 @@ bool viewer::m_setup_raytracing(viewer * view) if(rt == R_EMBREE && (mesh.render_pointcloud || mesh->is_pointcloud())) { ImGui::Indent(); - ImGui::Combo("pc.opt", (int *) &pc.opt, "NONE\0MAX\0MEAN\0MEDIAN\0AREA\0MEDIAN_PAIRS\0\0"); + ImGui::Combo("pc.opt", (int *) &pc.opt, "NONE\0MAX\0MEAN\0MEDIAN\0AREA\0MEDIAN_PAIRS\0VORONOI\0\0"); if(pc.opt == rt::embree::NONE) { ImGui::SliderFloat("pc.radius", &pc.radius, 0, 1); From ed8da4d9180582eaae36ae94884efa76e59c73a7 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 23 Sep 2024 15:26:27 +0200 Subject: [PATCH 1002/1018] viewer: fix deleted line --- src/gproshan/viewer/viewer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 5088ca88..4e0052c5 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -86,6 +86,7 @@ viewer::viewer(const char * title, const int width, const int height) viewer::~viewer() { + update_status_message("frametime_%p", this); save_frametime(tmp_file_path(status_message)); delete sphere; From f33ea07bf284fab38d017ff005c3a57af544d4ee Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 30 Sep 2024 11:28:43 +0200 Subject: [PATCH 1003/1018] geometry: updating quaternion --- .../gproshan/{mesh => geometry}/quaternion.h | 17 ++++------ include/gproshan/viewer/camera.h | 2 +- .../{mesh => geometry}/quaternion.cpp | 34 +++++-------------- 3 files changed, 17 insertions(+), 36 deletions(-) rename include/gproshan/{mesh => geometry}/quaternion.h (84%) rename src/gproshan/{mesh => geometry}/quaternion.cpp (75%) diff --git a/include/gproshan/mesh/quaternion.h b/include/gproshan/geometry/quaternion.h similarity index 84% rename from include/gproshan/mesh/quaternion.h rename to include/gproshan/geometry/quaternion.h index 4e4d64e4..6b39c925 100644 --- a/include/gproshan/mesh/quaternion.h +++ b/include/gproshan/geometry/quaternion.h @@ -10,29 +10,26 @@ namespace gproshan { -using vertex = vec3; - - class quaternion { public: float s; - vertex v; + vec3 v; public: quaternion(float s = 0, float vi = 0, float vj = 0, float vk = 0); - quaternion(float s, const vertex & v); - quaternion(const vertex & v); + quaternion(float s, const vec3 & v); + quaternion(const vec3 & v); - operator const vertex & () const; + operator const vec3 & () const; const quaternion & operator = (float s); - const quaternion & operator = (const vertex & v); + const quaternion & operator = (const vec3 & v); float & operator [] (int index); float operator [] (int index) const; float & re(void); float re(void) const; - vertex & im(void); - const vertex & im(void) const; + vec3 & im(void); + const vec3 & im(void) const; quaternion operator + (const quaternion & q) const; quaternion operator - (const quaternion & q) const; diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index d8fbc6b4..83ed4413 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -1,8 +1,8 @@ #ifndef CAMERA_H #define CAMERA_H -#include #include +#include // geometry processing and shape analysis framework diff --git a/src/gproshan/mesh/quaternion.cpp b/src/gproshan/geometry/quaternion.cpp similarity index 75% rename from src/gproshan/mesh/quaternion.cpp rename to src/gproshan/geometry/quaternion.cpp index 4a57826a..9e65ab25 100644 --- a/src/gproshan/mesh/quaternion.cpp +++ b/src/gproshan/geometry/quaternion.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -10,11 +10,11 @@ namespace gproshan { quaternion::quaternion(float s_, float vi, float vj, float vk): s(s_), v{vi, vj, vk} {} -quaternion::quaternion(float s_, const vertex & v_): s(s_), v(v_) {} +quaternion::quaternion(float s_, const vec3 & v_): s(s_), v(v_) {} -quaternion::quaternion(const vertex & v_): s(0), v(v_) {} +quaternion::quaternion(const vec3 & v_): s(0), v(v_) {} -quaternion::operator const vertex & () const +quaternion::operator const vec3 & () const { return v; } @@ -27,7 +27,7 @@ const quaternion & quaternion::operator = (float _s) return *this; } -const quaternion & quaternion::operator = (const vertex & _v) +const quaternion & quaternion::operator = (const vec3 & _v) { s = 0; v = _v; @@ -56,12 +56,12 @@ float quaternion::re() const return s; } -vertex & quaternion::im() +vec3 & quaternion::im() { return v; } -const vertex & quaternion::im() const +const vec3 & quaternion::im() const { return v; } @@ -135,8 +135,8 @@ quaternion quaternion::operator * (const quaternion & q) const { const float s1(s); const float s2(q.s); - const vertex & v1(v); - const vertex & v2(q.v); + const vec3 & v1(v); + const vec3 & v2(q.v); return quaternion(s1 * s2 - dot(v1, v2), s1 * v2 + s2 * v1 + cross(v1, v2)); } @@ -176,22 +176,6 @@ void quaternion::normalize() *this /= norm(); } -// spherical-linear interpolation -quaternion slerp(const quaternion & q0, const quaternion & q1, float t) -{ - // interpolate length - float m0 = q0.norm(); - float m1 = q1.norm(); - float m = (1-t)*m0 + t*m1; - - // interpolate direction - quaternion p0 = q0 / m0; - quaternion p1 = q1 / m1; - float theta = acos((p0.conj() * p1).re()); - quaternion p = (sin((1 - t) * theta) * p0 + sin(t * theta) * p1) / sin(theta); - - return m * p; -} std::ostream & operator << (std::ostream & os, const quaternion & q) { From 5db8d1079b64f6494b57444f9e02cea25defdc09 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 1 Oct 2024 16:22:39 +0200 Subject: [PATCH 1004/1018] geometry: cleaning up quaternion --- include/gproshan/geometry/quaternion.h | 7 +++---- include/gproshan/viewer/camera.h | 6 +++--- src/gproshan/geometry/quaternion.cpp | 6 ++---- src/gproshan/viewer/camera.cpp | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/include/gproshan/geometry/quaternion.h b/include/gproshan/geometry/quaternion.h index 6b39c925..a993f70d 100644 --- a/include/gproshan/geometry/quaternion.h +++ b/include/gproshan/geometry/quaternion.h @@ -13,13 +13,12 @@ namespace gproshan { class quaternion { public: - float s; + float s = 0; vec3 v; public: - quaternion(float s = 0, float vi = 0, float vj = 0, float vk = 0); - quaternion(float s, const vec3 & v); - quaternion(const vec3 & v); + quaternion(const vec3 & v = {}); + quaternion(float s, const vec3 & v = {}); operator const vec3 & () const; const quaternion & operator = (float s); diff --git a/include/gproshan/viewer/camera.h b/include/gproshan/viewer/camera.h index 83ed4413..b5182dfa 100644 --- a/include/gproshan/viewer/camera.h +++ b/include/gproshan/viewer/camera.h @@ -19,9 +19,9 @@ class camera public: quaternion eye; - quaternion pos = vertex{0, 0, -3.14}; - quaternion front = vertex{0, 0, 1}; - quaternion up = vertex{0, 1, 0}; + quaternion pos = vec3{0, 0, -3.14}; + quaternion front = vec3{0, 0, 1}; + quaternion up = vec3{0, 1, 0}; float fovy = 45; float aspect = 1; float near = 0.01; diff --git a/src/gproshan/geometry/quaternion.cpp b/src/gproshan/geometry/quaternion.cpp index 9e65ab25..9fad3432 100644 --- a/src/gproshan/geometry/quaternion.cpp +++ b/src/gproshan/geometry/quaternion.cpp @@ -8,11 +8,9 @@ namespace gproshan { -quaternion::quaternion(float s_, float vi, float vj, float vk): s(s_), v{vi, vj, vk} {} +quaternion::quaternion(float s, const vec3 & v): s(s), v(v) {} -quaternion::quaternion(float s_, const vec3 & v_): s(s_), v(v_) {} - -quaternion::quaternion(const vec3 & v_): s(0), v(v_) {} +quaternion::quaternion(const vec3 & v): v(v) {} quaternion::operator const vec3 & () const { diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index 8fd4f96e..b41ef9a1 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -47,7 +47,7 @@ mat4 camera::perspective(const float fovy, const float aspect, const float near, quaternion camera::click_to_sphere(const double x, const double y, const int w, const int h) { - quaternion p(0, 2 * x / w - 1, 2 * y / h - 1, 0); + quaternion p = vec3{float(2 * x / w - 1), float(2 * y / h - 1), 0}; if(p.norm2() > 1) { From 5566261e35b8afa4d627c59c4b8e5d1139a52c3c Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 1 Oct 2024 20:26:58 +0200 Subject: [PATCH 1005/1018] geometry: cleaned up quaternion --- include/gproshan/geometry/quaternion.h | 22 ++--- src/gproshan/geometry/quaternion.cpp | 117 ++++--------------------- src/gproshan/viewer/camera.cpp | 21 ++--- src/gproshan/viewer/viewer.cpp | 10 +-- 4 files changed, 37 insertions(+), 133 deletions(-) diff --git a/include/gproshan/geometry/quaternion.h b/include/gproshan/geometry/quaternion.h index a993f70d..13c15b83 100644 --- a/include/gproshan/geometry/quaternion.h +++ b/include/gproshan/geometry/quaternion.h @@ -12,7 +12,7 @@ namespace gproshan { class quaternion { - public: + private: float s = 0; vec3 v; @@ -21,40 +21,28 @@ class quaternion quaternion(float s, const vec3 & v = {}); operator const vec3 & () const; - const quaternion & operator = (float s); - const quaternion & operator = (const vec3 & v); float & operator [] (int index); float operator [] (int index) const; - float & re(void); - float re(void) const; - vec3 & im(void); - const vec3 & im(void) const; quaternion operator + (const quaternion & q) const; quaternion operator - (const quaternion & q) const; - quaternion operator - (void) const; + quaternion operator - () const; quaternion operator * (float c) const; quaternion operator / (float c) const; - void operator += (const quaternion & q); - void operator += (float c); - void operator -= (const quaternion & q); - void operator -= (float c); - void operator *= (float c); - void operator /= (float c); quaternion operator * (const quaternion & q) const; - void operator *= (const quaternion & q); quaternion conj() const; quaternion inv() const; float norm() const; float norm2() const; - quaternion unit() const; - void normalize(); friend std::ostream & operator << (std::ostream & os, const quaternion & q); friend std::istream & operator >> (std::istream & is, quaternion & q); }; + +float norm(const quaternion & q); +quaternion normalize(const quaternion & q); quaternion operator * (float c, const quaternion & q); diff --git a/src/gproshan/geometry/quaternion.cpp b/src/gproshan/geometry/quaternion.cpp index 9fad3432..cb50e661 100644 --- a/src/gproshan/geometry/quaternion.cpp +++ b/src/gproshan/geometry/quaternion.cpp @@ -17,23 +17,6 @@ quaternion::operator const vec3 & () const return v; } -const quaternion & quaternion::operator = (float _s) -{ - s = _s; - v = {0, 0, 0}; - - return *this; -} - -const quaternion & quaternion::operator = (const vec3 & _v) -{ - s = 0; - v = _v; - - return *this; -} - - float & quaternion::operator [] (int index) { return v[index]; @@ -44,114 +27,45 @@ float quaternion::operator [] (int index) const return v[index]; } -float & quaternion::re() -{ - return s; -} - -float quaternion::re() const -{ - return s; -} - -vec3 & quaternion::im() -{ - return v; -} - -const vec3 & quaternion::im() const -{ - return v; -} - quaternion quaternion::operator + (const quaternion & q) const { - return quaternion(s + q.s, v + q.v); + return {s + q.s, v + q.v}; } quaternion quaternion::operator - (const quaternion & q) const { - return quaternion(s - q.s, v - q.v); + return {s - q.s, v - q.v}; } quaternion quaternion::operator - () const { - return quaternion(-s, -v); + return {-s, -v}; } quaternion quaternion::operator * (float c) const { - return quaternion(c * s, c * v); -} - -quaternion operator * (float c, const quaternion & q) -{ - return q * c; + return {c * s, c * v}; } quaternion quaternion::operator / (float c) const { - return quaternion(s / c, v / c); -} - -void quaternion::operator += (const quaternion & q) -{ - s += q.s; - v += q.v; -} - -void quaternion::operator += (float c) -{ - s += c; -} - -void quaternion::operator -= (const quaternion & q) -{ - s -= q.s; - v -= q.v; -} - -void quaternion::operator -= (float c) -{ - s -= c; -} - -void quaternion::operator *= (float c) -{ - s *= c; - v *= c; -} - -void quaternion::operator /= (float c) -{ - s /= c; - v /= c; + return {s / c, v / c}; } // Hamilton product quaternion quaternion::operator * (const quaternion & q) const { - const float s1(s); - const float s2(q.s); - const vec3 & v1(v); - const vec3 & v2(q.v); - - return quaternion(s1 * s2 - dot(v1, v2), s1 * v2 + s2 * v1 + cross(v1, v2)); -} - -void quaternion::operator *= (const quaternion & q) -{ - *this = (*this * q); + return {s * q.s - dot(v, q.v), s * q.v + q.s * v + cross(v, q.v)}; } quaternion quaternion::conj() const { - return quaternion(s, -v); + return {s, -v}; } quaternion quaternion::inv() const { - return (this->conj()) / this->norm2(); + return conj() / norm2(); } float quaternion::norm() const @@ -164,14 +78,20 @@ float quaternion::norm2() const return s * s + dot(v, v); } -quaternion quaternion::unit() const + +float norm(const quaternion & q) { - return *this / norm(); + return q.norm(); } -void quaternion::normalize() +quaternion normalize(const quaternion & q) { - *this /= norm(); + return q / norm(q); +} + +quaternion operator * (float c, const quaternion & q) +{ + return q * c; } @@ -185,5 +105,6 @@ std::istream & operator >> (std::istream & is, quaternion & q) return is >> q.s >> q.v; } + } // namespace gproshan diff --git a/src/gproshan/viewer/camera.cpp b/src/gproshan/viewer/camera.cpp index b41ef9a1..ca82de7c 100644 --- a/src/gproshan/viewer/camera.cpp +++ b/src/gproshan/viewer/camera.cpp @@ -18,9 +18,9 @@ mat4 camera::look_at(const quaternion & r) vec3 X = cross(Z, Y); mat4 view; - view[0] = {X, -dot(X, eye.v)}; - view[1] = {Y, -dot(Y, eye.v)}; - view[2] = {-Z, dot(Z, eye.v)}; + view[0] = {X, -dot(X, (vec3) eye)}; + view[1] = {Y, -dot(Y, (vec3) eye)}; + view[2] = {-Z, dot(Z, (vec3) eye)}; view[3] = {0, 0, 0, 1}; return view; @@ -50,14 +50,9 @@ quaternion camera::click_to_sphere(const double x, const double y, const int w, quaternion p = vec3{float(2 * x / w - 1), float(2 * y / h - 1), 0}; if(p.norm2() > 1) - { - p.normalize(); - p.im().z() = 0; - } + p = normalize(p); else - { - p.im().z() = sqrt(1 - p.norm2()); - } + p[2] = sqrt(1 - p.norm2()); return p; } @@ -88,17 +83,17 @@ void camera::motion(const double x, const double y, const int w, const int h) void camera::zoom_in() { - pos.v.z() += 0.02; + pos[2] += 0.02; } void camera::zoom_out() { - pos.v.z() -= 0.02; + pos[2] -= 0.02; } float camera::zoom() const { - return -pos.v.z(); + return -pos[2]; } std::ostream & operator << (std::ostream & os, const camera & cam) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 4e0052c5..33199105 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -739,8 +739,8 @@ void viewer::cursor_callback(GLFWwindow * window, double x, double y) if(GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE)) { - view->cam.pos.im().x() = 2 * x / view->window_width - 1; - view->cam.pos.im().y() = 2 * y / view->window_height - 1; + view->cam.pos[0] = 2 * x / view->window_width - 1; + view->cam.pos[1] = 2 * y / view->window_height - 1; view->render_params.restart = true; } } @@ -1212,9 +1212,9 @@ bool viewer::m_raycasting(viewer * view) void viewer::render_gl() { - shader_sphere.uniform("eye", cam.eye.v); - shader_triangles.uniform("eye", cam.eye.v); - shader_pointcloud.uniform("eye", cam.eye.v); + shader_sphere.uniform("eye", cam.eye); + shader_triangles.uniform("eye", cam.eye); + shader_pointcloud.uniform("eye", cam.eye); const light & ambient = render_params.ambient; const light & l = render_params.lights[0]; From ca9e65be54778552942a3e1037f386d8148bc579 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 4 Oct 2024 10:24:12 +0200 Subject: [PATCH 1006/1018] graph: adding new module --- include/gproshan/graph/mst.h | 28 +++++++++++++++++++++++++ src/gproshan/graph/mst.cpp | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 include/gproshan/graph/mst.h create mode 100644 src/gproshan/graph/mst.cpp diff --git a/include/gproshan/graph/mst.h b/include/gproshan/graph/mst.h new file mode 100644 index 00000000..ea5035dd --- /dev/null +++ b/include/gproshan/graph/mst.h @@ -0,0 +1,28 @@ +#ifndef GRAPH_H +#define GRAPH_H + + +// geometry processing and shape analysis framework +namespace gproshan { + + +class union_find +{ + private: + unsigned * sets = nullptr; + unsigned n = 0; + + public: + union_find(unsigned n); + ~union_find(); + + unsigned find(const unsigned x); + bool merge(unsigned x, unsigned y); +}; + + +} // namespace gproshan + + +#endif // GRAPH_H + diff --git a/src/gproshan/graph/mst.cpp b/src/gproshan/graph/mst.cpp new file mode 100644 index 00000000..0cc6056e --- /dev/null +++ b/src/gproshan/graph/mst.cpp @@ -0,0 +1,40 @@ +#include + + +// geometry processing and shape analysis framework +namespace gproshan { + + +union_find::union_find(unsigned n) : n(n) +{ + sets = new unsigned[n]; + + for(unsigned i = 0; i < n; ++i) + sets[i] = i; +} + +union_find::~union_find() +{ + delete sets; +} + +unsigned union_find::find(const unsigned x) +{ + return sets[x] == x ? x : sets[x] = find(sets[x]); +} + +bool union_find::merge(unsigned x, unsigned y) +{ + x = find(x); + y = find(y); + + if(x == y) return false; + + sets[x] = y; + + return true; +} + + +} // namespace gproshan + From 36d3b7ab9c797a8ff1252fda0b8254cfeeff0f8d Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Fri, 4 Oct 2024 14:20:54 +0200 Subject: [PATCH 1007/1018] remove graph --- include/gproshan/graph/mst.h | 28 ------------------------- src/gproshan/graph/mst.cpp | 40 ------------------------------------ 2 files changed, 68 deletions(-) delete mode 100644 include/gproshan/graph/mst.h delete mode 100644 src/gproshan/graph/mst.cpp diff --git a/include/gproshan/graph/mst.h b/include/gproshan/graph/mst.h deleted file mode 100644 index ea5035dd..00000000 --- a/include/gproshan/graph/mst.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef GRAPH_H -#define GRAPH_H - - -// geometry processing and shape analysis framework -namespace gproshan { - - -class union_find -{ - private: - unsigned * sets = nullptr; - unsigned n = 0; - - public: - union_find(unsigned n); - ~union_find(); - - unsigned find(const unsigned x); - bool merge(unsigned x, unsigned y); -}; - - -} // namespace gproshan - - -#endif // GRAPH_H - diff --git a/src/gproshan/graph/mst.cpp b/src/gproshan/graph/mst.cpp deleted file mode 100644 index 0cc6056e..00000000 --- a/src/gproshan/graph/mst.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include - - -// geometry processing and shape analysis framework -namespace gproshan { - - -union_find::union_find(unsigned n) : n(n) -{ - sets = new unsigned[n]; - - for(unsigned i = 0; i < n; ++i) - sets[i] = i; -} - -union_find::~union_find() -{ - delete sets; -} - -unsigned union_find::find(const unsigned x) -{ - return sets[x] == x ? x : sets[x] = find(sets[x]); -} - -bool union_find::merge(unsigned x, unsigned y) -{ - x = find(x); - y = find(y); - - if(x == y) return false; - - sets[x] = y; - - return true; -} - - -} // namespace gproshan - From 994466146132699c2a66ee05bfa2051ccba955c4 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 14 Oct 2024 12:06:57 +0200 Subject: [PATCH 1008/1018] mesh: read ascii ply ignore other triangle attributes --- src/gproshan/mesh/che_ply.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gproshan/mesh/che_ply.cpp b/src/gproshan/mesh/che_ply.cpp index 06948b55..82edd220 100644 --- a/src/gproshan/mesh/che_ply.cpp +++ b/src/gproshan/mesh/che_ply.cpp @@ -126,6 +126,8 @@ void che_ply::read_file(const std::string & file) for(index_t i = 0; i < nv; ++i) fscanf(fp, "%u", P + i); + fgets(line, sizeof(line), fp); + for(const index_t v: trig_convex_polygon(P, nv)) trigs.push_back(v); } From bf2866259c6e22982e9b51d801203d7a0946c113 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 23 Oct 2024 19:43:00 +0200 Subject: [PATCH 1009/1018] update imgui v1.91.4 --- include/imgui/imconfig.h | 11 +- include/imgui/imgui.h | 280 ++-- include/imgui/imgui_impl_glfw.h | 3 + include/imgui/imgui_impl_opengl3_loader.h | 6 + include/imgui/imgui_internal.h | 524 +++----- include/imgui/imstb_textedit.h | 98 +- src/imgui/imgui.cpp | 1435 +++++++++++++++------ src/imgui/imgui_demo.cpp | 158 ++- src/imgui/imgui_draw.cpp | 59 +- src/imgui/imgui_impl_glfw.cpp | 74 +- src/imgui/imgui_impl_opengl3.cpp | 23 +- src/imgui/imgui_tables.cpp | 73 +- src/imgui/imgui_widgets.cpp | 874 +++++++------ 13 files changed, 2278 insertions(+), 1340 deletions(-) diff --git a/include/imgui/imconfig.h b/include/imgui/imconfig.h index 6e4c743d..63d6ba08 100644 --- a/include/imgui/imconfig.h +++ b/include/imgui/imconfig.h @@ -43,7 +43,7 @@ //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a) //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME). //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default). -//#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS // Don't implement default io.PlatformOpenInShellFn() handler (Win32: ShellExecute(), require shell32.lib/.a, Mac/Linux: use system("")). +//#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS // Don't implement default platform_io.Platform_OpenInShellFn() handler (Win32: ShellExecute(), require shell32.lib/.a, Mac/Linux: use system("")). //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf) //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself. //#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies) @@ -83,10 +83,13 @@ // On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'. //#define IMGUI_ENABLE_FREETYPE -//---- Use FreeType+lunasvg library to render OpenType SVG fonts (SVGinOT) -// Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided). +//---- Use FreeType + plutosvg or lunasvg to render OpenType SVG fonts (SVGinOT) // Only works in combination with IMGUI_ENABLE_FREETYPE. -// (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement) +// - lunasvg is currently easier to acquire/install, as e.g. it is part of vcpkg. +// - plutosvg will support more fonts and may load them faster. It currently requires to be built manually but it is fairly easy. See misc/freetype/README for instructions. +// - Both require headers to be available in the include path + program to be linked with the library code (not provided). +// - (note: lunasvg implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement) +//#define IMGUI_ENABLE_FREETYPE_PLUTOSVG //#define IMGUI_ENABLE_FREETYPE_LUNASVG //---- Use stb_truetype to build and rasterize the font atlas (default) diff --git a/include/imgui/imgui.h b/include/imgui/imgui.h index 05e48a17..22f992fa 100644 --- a/include/imgui/imgui.h +++ b/include/imgui/imgui.h @@ -1,16 +1,17 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (headers) // Help: // - See links below. // - Call and read ImGui::ShowDemoWindow() in imgui_demo.cpp. All applications in examples/ are doing that. // - Read top of imgui.cpp for more details, links and comments. +// - Add '#define IMGUI_DEFINE_MATH_OPERATORS' before including this file (or in imconfig.h) to access courtesy maths operators for ImVec2 and ImVec4. // Resources: // - FAQ ........................ https://dearimgui.com/faq (in repository as docs/FAQ.md) // - Homepage ................... https://github.com/ocornut/imgui // - Releases & changelog ....... https://github.com/ocornut/imgui/releases -// - Gallery .................... https://github.com/ocornut/imgui/issues/7503 (please post your screenshots/video there!) +// - Gallery .................... https://github.com/ocornut/imgui/issues?q=label%3Agallery (please post your screenshots/video there!) // - Wiki ....................... https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started (how to integrate in an existing app by adding ~25 lines of code) // - Third-party Extensions https://github.com/ocornut/imgui/wiki/Useful-Extensions (ImPlot & many more) @@ -27,8 +28,8 @@ // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') -#define IMGUI_VERSION "1.91.0" -#define IMGUI_VERSION_NUM 19100 +#define IMGUI_VERSION "1.91.4" +#define IMGUI_VERSION_NUM 19140 #define IMGUI_HAS_TABLE /* @@ -48,7 +49,7 @@ Index of this file: // [SECTION] Drawing API (ImDrawCallback, ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawFlags, ImDrawListFlags, ImDrawList, ImDrawData) // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) -// [SECTION] Platform Dependent Interfaces (ImGuiPlatformImeData) +// [SECTION] ImGuiPlatformIO + other Platform Dependent Interfaces (ImGuiPlatformImeData) // [SECTION] Obsolete functions and types */ @@ -171,13 +172,14 @@ struct ImFontGlyph; // A single font glyph (code point + coordin struct ImFontGlyphRangesBuilder; // Helper to build glyph ranges from text/string data struct ImColor; // Helper functions to create a color that can be converted to either u32 or float4 (*OBSOLETE* please avoid using) struct ImGuiContext; // Dear ImGui context (opaque structure, unless including imgui_internal.h) -struct ImGuiIO; // Main configuration and I/O between your application and ImGui +struct ImGuiIO; // Main configuration and I/O between your application and ImGui (also see: ImGuiPlatformIO) struct ImGuiInputTextCallbackData; // Shared state of InputText() when using custom ImGuiInputTextCallback (rare/advanced use) struct ImGuiKeyData; // Storage for ImGuiIO and IsKeyDown(), IsKeyPressed() etc functions. struct ImGuiListClipper; // Helper to manually clip large list of items struct ImGuiMultiSelectIO; // Structure to interact with a BeginMultiSelect()/EndMultiSelect() block struct ImGuiOnceUponAFrame; // Helper for running a block of code not more than once a frame struct ImGuiPayload; // User data payload for drag and drop operations +struct ImGuiPlatformIO; // Interface between platform/renderer backends and ImGui (e.g. Clipboard, IME hooks). Extends ImGuiIO. In docking branch, this gets extended to support multi-viewports. struct ImGuiPlatformImeData; // Platform IME data for io.PlatformSetImeDataFn() function. struct ImGuiSelectionBasicStorage; // Optional helper to store multi-selection state + apply multi-selection requests. struct ImGuiSelectionExternalStorage;//Optional helper to apply multi-selection requests to existing randomly accessible storage. @@ -247,8 +249,10 @@ typedef int ImGuiWindowFlags; // -> enum ImGuiWindowFlags_ // Flags: f // ImTexture: user data for renderer backend to identify a texture [Compile-time configurable type] // - To use something else than an opaque void* pointer: override with e.g. '#define ImTextureID MyTextureType*' in your imconfig.h file. // - This can be whatever to you want it to be! read the FAQ about ImTextureID for details. +// - You can make this a structure with various constructors if you need. You will have to implement ==/!= operators. +// - (note: before v1.91.4 (2024/10/08) the default type for ImTextureID was void*. Use intermediary intptr_t cast and read FAQ if you have casting warnings) #ifndef ImTextureID -typedef void* ImTextureID; // Default: store a pointer or an integer fitting in a pointer (most renderer backends are ok with that) +typedef ImU64 ImTextureID; // Default: store a pointer or an integer fitting in a pointer (most renderer backends are ok with that) #endif // ImDrawIdx: vertex index. [Compile-time configurable type] @@ -280,8 +284,8 @@ typedef void* (*ImGuiMemAllocFunc)(size_t sz, void* user_data); typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // Function signature for ImGui::SetAllocatorFunctions() // ImVec2: 2D vector used to store positions, sizes etc. [Compile-time configurable type] -// This is a frequently used type in the API. Consider using IM_VEC2_CLASS_EXTRA to create implicit cast from/to our preferred type. -// Add '#define IMGUI_DEFINE_MATH_OPERATORS' in your imconfig.h file to benefit from courtesy maths operators for those types. +// - This is a frequently used type in the API. Consider using IM_VEC2_CLASS_EXTRA to create implicit cast from/to our preferred type. +// - Add '#define IMGUI_DEFINE_MATH_OPERATORS' before including this file (or in imconfig.h) to access courtesy maths operators for ImVec2 and ImVec4. IM_MSVC_RUNTIME_CHECKS_OFF struct ImVec2 { @@ -324,7 +328,8 @@ namespace ImGui IMGUI_API void SetCurrentContext(ImGuiContext* ctx); // Main - IMGUI_API ImGuiIO& GetIO(); // access the IO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) + IMGUI_API ImGuiIO& GetIO(); // access the ImGuiIO structure (mouse/keyboard/gamepad inputs, time, various configuration options/flags) + IMGUI_API ImGuiPlatformIO& GetPlatformIO(); // access the ImGuiPlatformIO structure (mostly hooks/functions to connect to platform/renderer and OS Clipboard, IME etc.) IMGUI_API ImGuiStyle& GetStyle(); // access the Style structure (colors, sizes). Always use PushStyleColor(), PushStyleVar() to modify style mid-frame! IMGUI_API void NewFrame(); // start a new Dear ImGui frame, you can submit any command from this point until Render()/EndFrame(). IMGUI_API void EndFrame(); // ends the Dear ImGui frame. automatically called by Render(). If you don't need to render data (skipping rendering) you may call EndFrame() without Render()... but you'll have wasted CPU already! If you don't need to render, better to not create any windows and not call NewFrame() at all! @@ -366,10 +371,10 @@ namespace ImGui // Child Windows // - Use child windows to begin into a self-contained independent scrolling/clipping regions within a host window. Child windows can embed their own child. // - Before 1.90 (November 2023), the "ImGuiChildFlags child_flags = 0" parameter was "bool border = false". - // This API is backward compatible with old code, as we guarantee that ImGuiChildFlags_Border == true. + // This API is backward compatible with old code, as we guarantee that ImGuiChildFlags_Borders == true. // Consider updating your old code: // BeginChild("Name", size, false) -> Begin("Name", size, 0); or Begin("Name", size, ImGuiChildFlags_None); - // BeginChild("Name", size, true) -> Begin("Name", size, ImGuiChildFlags_Border); + // BeginChild("Name", size, true) -> Begin("Name", size, ImGuiChildFlags_Borders); // - Manual sizing (each axis can use a different setting e.g. ImVec2(0.0f, 400.0f)): // == 0.0f: use remaining parent window size for this axis. // > 0.0f: use specified size for this axis. @@ -437,8 +442,10 @@ namespace ImGui IMGUI_API void PushStyleColor(ImGuiCol idx, ImU32 col); // modify a style color. always use this if you modify the style after NewFrame(). IMGUI_API void PushStyleColor(ImGuiCol idx, const ImVec4& col); IMGUI_API void PopStyleColor(int count = 1); - IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame(). - IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. always use this if you modify the style after NewFrame(). + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, float val); // modify a style float variable. always use this if you modify the style after NewFrame()! + IMGUI_API void PushStyleVar(ImGuiStyleVar idx, const ImVec2& val); // modify a style ImVec2 variable. " + IMGUI_API void PushStyleVarX(ImGuiStyleVar idx, float val_x); // modify X component of a style ImVec2 variable. " + IMGUI_API void PushStyleVarY(ImGuiStyleVar idx, float val_y); // modify Y component of a style ImVec2 variable. " IMGUI_API void PopStyleVar(int count = 1); IMGUI_API void PushItemFlag(ImGuiItemFlags option, bool enabled); // modify specified shared item flag, e.g. PushItemFlag(ImGuiItemFlags_NoTabStop, true) IMGUI_API void PopItemFlag(); @@ -455,7 +462,7 @@ namespace ImGui // - Use the ShowStyleEditor() function to interactively see/edit the colors. IMGUI_API ImFont* GetFont(); // get current font IMGUI_API float GetFontSize(); // get current font size (= height in pixels) of current font with current scale applied - IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a while pixel, useful to draw custom shapes via the ImDrawList API + IMGUI_API ImVec2 GetFontTexUvWhitePixel(); // get UV coordinate for a white pixel, useful to draw custom shapes via the ImDrawList API IMGUI_API ImU32 GetColorU32(ImGuiCol idx, float alpha_mul = 1.0f); // retrieve given style color with style alpha applied and optional extra alpha multiplier, packed as a 32-bit value suitable for ImDrawList IMGUI_API ImU32 GetColorU32(const ImVec4& col); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList IMGUI_API ImU32 GetColorU32(ImU32 col, float alpha_mul = 1.0f); // retrieve given color with style alpha applied, packed as a 32-bit value suitable for ImDrawList @@ -574,7 +581,7 @@ namespace ImGui // the array syntax is just a way to document the number of elements that are expected to be accessible. You can pass address of your first element out of a contiguous set, e.g. &myvector.x // - Adjust format string to decorate the value with a prefix, a suffix, or adapt the editing and display precision e.g. "%.3f" -> 1.234; "%5.2f secs" -> 01.23 secs; "Biscuit: %.0f" -> Biscuit: 1; etc. // - Format string may also be set to NULL or use the default format ("%f" or "%d"). - // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For gamepad/keyboard navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). + // - Speed are per-pixel of mouse movement (v_speed=0.2f: mouse needs to move by 5 pixels to increase value by 1). For keyboard/gamepad navigation, minimum speed is Max(v_speed, minimum_step_at_given_precision). // - Use v_min < v_max to clamp edits to given limits. Note that CTRL+Click manual input can override those limits if ImGuiSliderFlags_AlwaysClamp is not used. // - Use v_max = FLT_MAX / INT_MAX etc to avoid clamping to a maximum, same with v_min = -FLT_MAX / INT_MIN to avoid clamping to a minimum. // - We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. @@ -838,7 +845,7 @@ namespace ImGui // Legacy Columns API (prefer using Tables!) // - You can also use SameLine(pos_x) to mimic simplified columns. - IMGUI_API void Columns(int count = 1, const char* id = NULL, bool border = true); + IMGUI_API void Columns(int count = 1, const char* id = NULL, bool borders = true); IMGUI_API void NextColumn(); // next column, defaults to current row or next row if the current row is finished IMGUI_API int GetColumnIndex(); // get current column index IMGUI_API float GetColumnWidth(int column_index = -1); // get column width (in pixels). pass -1 to use current column @@ -883,7 +890,7 @@ namespace ImGui // - Disable all user interactions and dim items visuals (applying style.DisabledAlpha over current colors) // - Those can be nested but it cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep everything disabled) // - Tooltips windows by exception are opted out of disabling. - // - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. + // - BeginDisabled(false)/EndDisabled() essentially does nothing but is provided to facilitate use of boolean expressions (as a micro-optimization: if you have tens of thousands of BeginDisabled(false)/EndDisabled() pairs, you might want to reformulate your code to avoid making those calls) IMGUI_API void BeginDisabled(bool disabled = true); IMGUI_API void EndDisabled(); @@ -893,10 +900,12 @@ namespace ImGui IMGUI_API void PopClipRect(); // Focus, Activation - // - Prefer using "SetItemDefaultFocus()" over "if (IsWindowAppearing()) SetScrollHereY()" when applicable to signify "this is the default item" - IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of a window. + IMGUI_API void SetItemDefaultFocus(); // make last item the default focused item of of a newly appearing window. IMGUI_API void SetKeyboardFocusHere(int offset = 0); // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget. Use -1 to access previous widget. + // Keyboard/Gamepad Navigation + IMGUI_API void SetNavCursorVisible(bool visible); // alter visibility of keyboard/gamepad cursor. by default: show when using an arrow key, hide when clicking with mouse. + // Overlapping mode IMGUI_API void SetNextItemAllowOverlap(); // allow next item to be overlapped by a subsequent item. Useful with invisible buttons, selectable, treenode covering an area where subsequent items may need to be added. Note that both Selectable() and TreeNode() have dedicated flags doing this. @@ -989,7 +998,7 @@ namespace ImGui // - Many related features are still in imgui_internal.h. For instance, most IsKeyXXX()/IsMouseXXX() functions have an owner-id-aware version. IMGUI_API void SetItemKeyOwner(ImGuiKey key); // Set key owner to last item ID if it is hovered or active. Equivalent to 'if (IsItemHovered() || IsItemActive()) { SetKeyOwner(key, GetItemID());'. - // Inputs Utilities: Mouse specific + // Inputs Utilities: Mouse // - To refer to a mouse button, you may use named enums in your code e.g. ImGuiMouseButton_Left, ImGuiMouseButton_Right. // - You can also use regular integer: it is forever guaranteed that 0=Left, 1=Right, 2=Middle. // - Dragging operations are only reported after mouse has moved a certain distance away from the initial clicking position (see 'lock_threshold' and 'io.MouseDraggingThreshold') @@ -1071,8 +1080,8 @@ enum ImGuiWindowFlags_ ImGuiWindowFlags_NoBringToFrontOnFocus = 1 << 13, // Disable bringing window to front when taking focus (e.g. clicking on it or programmatically giving it focus) ImGuiWindowFlags_AlwaysVerticalScrollbar= 1 << 14, // Always show vertical scrollbar (even if ContentSize.y < Size.y) ImGuiWindowFlags_AlwaysHorizontalScrollbar=1<< 15, // Always show horizontal scrollbar (even if ContentSize.x < Size.x) - ImGuiWindowFlags_NoNavInputs = 1 << 16, // No gamepad/keyboard navigation within the window - ImGuiWindowFlags_NoNavFocus = 1 << 17, // No focusing toward this window with gamepad/keyboard navigation (e.g. skipped by CTRL+TAB) + ImGuiWindowFlags_NoNavInputs = 1 << 16, // No keyboard/gamepad navigation within the window + ImGuiWindowFlags_NoNavFocus = 1 << 17, // No focusing toward this window with keyboard/gamepad navigation (e.g. skipped by CTRL+TAB) ImGuiWindowFlags_UnsavedDocument = 1 << 18, // Display a dot next to the title. When used in a tab/docking context, tab is selected when clicking the X + closure is not assumed (will wait for user to stop submitting the tab). Otherwise closure is assumed when pressing the X, so if you keep submitting the tab may reappear at end of tab bar. ImGuiWindowFlags_NoNav = ImGuiWindowFlags_NoNavInputs | ImGuiWindowFlags_NoNavFocus, ImGuiWindowFlags_NoDecoration = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse, @@ -1093,7 +1102,7 @@ enum ImGuiWindowFlags_ }; // Flags for ImGui::BeginChild() -// (Legacy: bit 0 must always correspond to ImGuiChildFlags_Border to be backward compatible with old API using 'bool border = false'. +// (Legacy: bit 0 must always correspond to ImGuiChildFlags_Borders to be backward compatible with old API using 'bool border = false'. // About using AutoResizeX/AutoResizeY flags: // - May be combined with SetNextWindowSizeConstraints() to set a min/max size for each axis (see "Demo->Child->Auto-resize with Constraints"). // - Size measurement for a given axis is only performed when the child window is within visible boundaries, or is just appearing. @@ -1104,7 +1113,7 @@ enum ImGuiWindowFlags_ enum ImGuiChildFlags_ { ImGuiChildFlags_None = 0, - ImGuiChildFlags_Border = 1 << 0, // Show an outer border and enable WindowPadding. (IMPORTANT: this is always == 1 == true for legacy reason) + ImGuiChildFlags_Borders = 1 << 0, // Show an outer border and enable WindowPadding. (IMPORTANT: this is always == 1 == true for legacy reason) ImGuiChildFlags_AlwaysUseWindowPadding = 1 << 1, // Pad with style.WindowPadding even if no border are drawn (no padding by default for non-bordered child windows because it makes more sense) ImGuiChildFlags_ResizeX = 1 << 2, // Allow resize from right border (layout direction). Enable .ini saving (unless ImGuiWindowFlags_NoSavedSettings passed to window flags) ImGuiChildFlags_ResizeY = 1 << 3, // Allow resize from bottom border (layout direction). " @@ -1112,7 +1121,12 @@ enum ImGuiChildFlags_ ImGuiChildFlags_AutoResizeY = 1 << 5, // Enable auto-resizing height. Read "IMPORTANT: Size measurement" details above. ImGuiChildFlags_AlwaysAutoResize = 1 << 6, // Combined with AutoResizeX/AutoResizeY. Always measure size even when child is hidden, always return true, always disable clipping optimization! NOT RECOMMENDED. ImGuiChildFlags_FrameStyle = 1 << 7, // Style the child window like a framed item: use FrameBg, FrameRounding, FrameBorderSize, FramePadding instead of ChildBg, ChildRounding, ChildBorderSize, WindowPadding. - ImGuiChildFlags_NavFlattened = 1 << 8, // [BETA] Share focus scope, allow gamepad/keyboard navigation to cross over parent border to this child or between sibling child windows. + ImGuiChildFlags_NavFlattened = 1 << 8, // [BETA] Share focus scope, allow keyboard/gamepad navigation to cross over parent border to this child or between sibling child windows. + + // Obsolete names +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiChildFlags_Border = ImGuiChildFlags_Borders, // Renamed in 1.91.1 (August 2024) for consistency. +#endif }; // Flags for ImGui::PushItemFlag() @@ -1125,6 +1139,7 @@ enum ImGuiItemFlags_ ImGuiItemFlags_NoNavDefaultFocus = 1 << 2, // false // Disable item being a candidate for default focus (e.g. used by title bar items). ImGuiItemFlags_ButtonRepeat = 1 << 3, // false // Any button-like behavior will have repeat mode enabled (based on io.KeyRepeatDelay and io.KeyRepeatRate values). Note that you can also call IsItemActive() after any button to tell if it is being held. ImGuiItemFlags_AutoClosePopups = 1 << 4, // true // MenuItem()/Selectable() automatically close their parent popup window. + ImGuiItemFlags_AllowDuplicateId = 1 << 5, // false // Allow submitting an item with the same identifier as an item already submitted this frame without triggering a warning tooltip if io.ConfigDebugHighlightIdConflicts is set. }; // Flags for ImGui::InputText() @@ -1141,7 +1156,7 @@ enum ImGuiInputTextFlags_ // Inputs ImGuiInputTextFlags_AllowTabInput = 1 << 5, // Pressing TAB input a '\t' character into the text field - ImGuiInputTextFlags_EnterReturnsTrue = 1 << 6, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider looking at the IsItemDeactivatedAfterEdit() function. + ImGuiInputTextFlags_EnterReturnsTrue = 1 << 6, // Return 'true' when Enter is pressed (as opposed to every time the value was modified). Consider using IsItemDeactivatedAfterEdit() instead! ImGuiInputTextFlags_EscapeClearsAll = 1 << 7, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 8, // In multi-line mode, validate with Enter, add new line with Ctrl+Enter (default is opposite: validate with Ctrl+Enter, add line with Enter). @@ -1177,8 +1192,8 @@ enum ImGuiTreeNodeFlags_ ImGuiTreeNodeFlags_NoTreePushOnOpen = 1 << 3, // Don't do a TreePush() when open (e.g. for CollapsingHeader) = no extra indent nor pushing on ID stack ImGuiTreeNodeFlags_NoAutoOpenOnLog = 1 << 4, // Don't automatically and temporarily open node when Logging is active (by default logging will automatically open tree nodes) ImGuiTreeNodeFlags_DefaultOpen = 1 << 5, // Default node to be open - ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Need double-click to open node - ImGuiTreeNodeFlags_OpenOnArrow = 1 << 7, // Only open when clicking on the arrow part. If ImGuiTreeNodeFlags_OpenOnDoubleClick is also set, single-click arrow or double-click all box to open. + ImGuiTreeNodeFlags_OpenOnDoubleClick = 1 << 6, // Open on double-click instead of simple click (default for multi-select unless any _OpenOnXXX behavior is set explicitly). Both behaviors may be combined. + ImGuiTreeNodeFlags_OpenOnArrow = 1 << 7, // Open when clicking on the arrow part (default for multi-select unless any _OpenOnXXX behavior is set explicitly). Both behaviors may be combined. ImGuiTreeNodeFlags_Leaf = 1 << 8, // No collapsing, no arrow (use as a convenience for leaf nodes). ImGuiTreeNodeFlags_Bullet = 1 << 9, // Display a bullet instead of arrow. IMPORTANT: node can still be marked open/close if you don't set the _Leaf flag! ImGuiTreeNodeFlags_FramePadding = 1 << 10, // Use FramePadding (even for an unframed text node) to vertically align text baseline to regular widget height. Equivalent to calling AlignTextToFramePadding() before the node. @@ -1313,7 +1328,7 @@ enum ImGuiHoveredFlags_ ImGuiHoveredFlags_AllowWhenOverlappedByItem = 1 << 8, // IsItemHovered() only: Return true even if the item uses AllowOverlap mode and is overlapped by another hoverable item. ImGuiHoveredFlags_AllowWhenOverlappedByWindow = 1 << 9, // IsItemHovered() only: Return true even if the position is obstructed or overlapped by another window. ImGuiHoveredFlags_AllowWhenDisabled = 1 << 10, // IsItemHovered() only: Return true even if the item is disabled - ImGuiHoveredFlags_NoNavOverride = 1 << 11, // IsItemHovered() only: Disable using gamepad/keyboard navigation state when active, always query mouse + ImGuiHoveredFlags_NoNavOverride = 1 << 11, // IsItemHovered() only: Disable using keyboard/gamepad navigation state when active, always query mouse ImGuiHoveredFlags_AllowWhenOverlapped = ImGuiHoveredFlags_AllowWhenOverlappedByItem | ImGuiHoveredFlags_AllowWhenOverlappedByWindow, ImGuiHoveredFlags_RectOnly = ImGuiHoveredFlags_AllowWhenBlockedByPopup | ImGuiHoveredFlags_AllowWhenBlockedByActiveItem | ImGuiHoveredFlags_AllowWhenOverlapped, ImGuiHoveredFlags_RootAndChildWindows = ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows, @@ -1584,8 +1599,6 @@ enum ImGuiConfigFlags_ ImGuiConfigFlags_None = 0, ImGuiConfigFlags_NavEnableKeyboard = 1 << 0, // Master keyboard navigation enable flag. Enable full Tabbing + directional arrows + space/enter to activate. ImGuiConfigFlags_NavEnableGamepad = 1 << 1, // Master gamepad navigation enable flag. Backend also needs to set ImGuiBackendFlags_HasGamepad. - ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // Instruct navigation to move the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is awkward. Will update io.MousePos and set io.WantSetMousePos=true. If enabled you MUST honor io.WantSetMousePos requests in your backend, otherwise ImGui will react as if the mouse is jumping around back and forth. - ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // Instruct navigation to not set the io.WantCaptureKeyboard flag when io.NavActive is set. ImGuiConfigFlags_NoMouse = 1 << 4, // Instruct dear imgui to disable mouse inputs and interactions. ImGuiConfigFlags_NoMouseCursorChange = 1 << 5, // Instruct backend to not alter mouse cursor shape and visibility. Use if the backend cursor changes are interfering with yours and you don't want to use SetMouseCursor() to change mouse cursor. You may want to honor requests from imgui by reading GetMouseCursor() yourself instead. ImGuiConfigFlags_NoKeyboard = 1 << 6, // Instruct dear imgui to disable keyboard inputs and interactions. This is done by ignoring keyboard events and clearing existing states. @@ -1593,6 +1606,11 @@ enum ImGuiConfigFlags_ // User storage (to allow your backend/engine to communicate to code that may be shared between multiple projects. Those flags are NOT used by core Dear ImGui) ImGuiConfigFlags_IsSRGB = 1 << 20, // Application is SRGB-aware. ImGuiConfigFlags_IsTouchScreen = 1 << 21, // Application is using a touch screen instead of a mouse. + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiConfigFlags_NavEnableSetMousePos = 1 << 2, // [moved/renamed in 1.91.4] -> use bool io.ConfigNavMoveSetMousePos + ImGuiConfigFlags_NavNoCaptureKeyboard = 1 << 3, // [moved/renamed in 1.91.4] -> use bool io.ConfigNavCaptureKeyboard +#endif }; // Backend capabilities flags stored in io.BackendFlags. Set by imgui_impl_xxx or custom backend. @@ -1601,7 +1619,7 @@ enum ImGuiBackendFlags_ ImGuiBackendFlags_None = 0, ImGuiBackendFlags_HasGamepad = 1 << 0, // Backend Platform supports gamepad and currently has one connected. ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Backend Platform supports honoring GetMouseCursor() value to change the OS cursor shape. - ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set). + ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if io.ConfigNavMoveSetMousePos is set). ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3, // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices. }; @@ -1660,7 +1678,7 @@ enum ImGuiCol_ ImGuiCol_TextLink, // Hyperlink color ImGuiCol_TextSelectedBg, ImGuiCol_DragDropTarget, // Rectangle highlighting a drop target - ImGuiCol_NavHighlight, // Gamepad/keyboard: current highlighted item + ImGuiCol_NavCursor, // Color of keyboard/gamepad navigation cursor/rectangle, when visible ImGuiCol_NavWindowingHighlight, // Highlight window when using CTRL+TAB ImGuiCol_NavWindowingDimBg, // Darken/colorize entire screen behind the CTRL+TAB window list, when active ImGuiCol_ModalWindowDimBg, // Darken/colorize entire screen behind a modal window, when one is active @@ -1670,6 +1688,7 @@ enum ImGuiCol_ ImGuiCol_TabActive = ImGuiCol_TabSelected, // [renamed in 1.90.9] ImGuiCol_TabUnfocused = ImGuiCol_TabDimmed, // [renamed in 1.90.9] ImGuiCol_TabUnfocusedActive = ImGuiCol_TabDimmedSelected, // [renamed in 1.90.9] + ImGuiCol_NavHighlight = ImGuiCol_NavCursor, // [renamed in 1.91.4] #endif }; @@ -1728,7 +1747,7 @@ enum ImGuiButtonFlags_ ImGuiButtonFlags_MouseButtonRight = 1 << 1, // React on right mouse button ImGuiButtonFlags_MouseButtonMiddle = 1 << 2, // React on center mouse button ImGuiButtonFlags_MouseButtonMask_ = ImGuiButtonFlags_MouseButtonLeft | ImGuiButtonFlags_MouseButtonRight | ImGuiButtonFlags_MouseButtonMiddle, // [Internal] - //ImGuiButtonFlags_MouseButtonDefault_ = ImGuiButtonFlags_MouseButtonLeft, + ImGuiButtonFlags_EnableNav = 1 << 3, // InvisibleButton(): do not disable navigation/tabbing. Otherwise disabled by default. }; // Flags for ColorEdit3() / ColorEdit4() / ColorPicker3() / ColorPicker4() / ColorButton() @@ -1777,19 +1796,18 @@ enum ImGuiColorEditFlags_ // Flags for DragFloat(), DragInt(), SliderFloat(), SliderInt() etc. // We use the same sets of flags for DragXXX() and SliderXXX() functions as the features are the same and it makes it easier to swap them. -// (Those are per-item flags. There are shared flags in ImGuiIO: io.ConfigDragClickToInputText) +// (Those are per-item flags. There is shared behavior flag too: ImGuiIO: io.ConfigDragClickToInputText) enum ImGuiSliderFlags_ { - ImGuiSliderFlags_None = 0, - ImGuiSliderFlags_AlwaysClamp = 1 << 4, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. - ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. - ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits). - ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget. - ImGuiSliderFlags_WrapAround = 1 << 8, // Enable wrapping around from max to min and from min to max (only supported by DragXXX() functions for now. - ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. - - // Obsolete names - //ImGuiSliderFlags_ClampOnInput = ImGuiSliderFlags_AlwaysClamp, // [renamed in 1.79] + ImGuiSliderFlags_None = 0, + ImGuiSliderFlags_Logarithmic = 1 << 5, // Make the widget logarithmic (linear otherwise). Consider using ImGuiSliderFlags_NoRoundToFormat with this if using a format-string with small amount of digits. + ImGuiSliderFlags_NoRoundToFormat = 1 << 6, // Disable rounding underlying value to match precision of the display format string (e.g. %.3f values are rounded to those 3 digits). + ImGuiSliderFlags_NoInput = 1 << 7, // Disable CTRL+Click or Enter key allowing to input text directly into the widget. + ImGuiSliderFlags_WrapAround = 1 << 8, // Enable wrapping around from max to min and from min to max. Only supported by DragXXX() functions for now. + ImGuiSliderFlags_ClampOnInput = 1 << 9, // Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds. + ImGuiSliderFlags_ClampZeroRange = 1 << 10, // Clamp even if min==max==0.0f. Otherwise due to legacy reason DragXXX functions don't clamp with those values. When your clamping limits are dynamic you almost always want to use it. + ImGuiSliderFlags_AlwaysClamp = ImGuiSliderFlags_ClampOnInput | ImGuiSliderFlags_ClampZeroRange, + ImGuiSliderFlags_InvalidMask_ = 0x7000000F, // [Internal] We treat using those bits as being potentially a 'float power' argument from the previous API that has got miscast to this enum, and will trigger an assert if needed. }; // Identify a mouse button. @@ -1938,7 +1956,7 @@ enum ImGuiTableColumnFlags_ ImGuiTableColumnFlags_NoSort = 1 << 9, // Disable ability to sort on this field (even if ImGuiTableFlags_Sortable is set on the table). ImGuiTableColumnFlags_NoSortAscending = 1 << 10, // Disable ability to sort in the ascending direction. ImGuiTableColumnFlags_NoSortDescending = 1 << 11, // Disable ability to sort in the descending direction. - ImGuiTableColumnFlags_NoHeaderLabel = 1 << 12, // TableHeadersRow() will not submit horizontal label for this column. Convenient for some small columns. Name will still appear in context menu or in angled headers. + ImGuiTableColumnFlags_NoHeaderLabel = 1 << 12, // TableHeadersRow() will submit an empty label for this column. Convenient for some small columns. Name will still appear in context menu or in angled headers. You may append into this cell by calling TableSetColumnIndex() right after the TableHeadersRow() call. ImGuiTableColumnFlags_NoHeaderWidth = 1 << 13, // Disable header text width contribution to automatic column width. ImGuiTableColumnFlags_PreferSortAscending = 1 << 14, // Make the initial sort direction Ascending when first sorting on this column (default). ImGuiTableColumnFlags_PreferSortDescending = 1 << 15, // Make the initial sort direction Descending when first sorting on this column. @@ -2193,6 +2211,8 @@ struct ImGuiStyle // - initialization: backends and user code writes to ImGuiIO. // - main loop: backends writes to ImGuiIO, user code and imgui code reads from ImGuiIO. //----------------------------------------------------------------------------- +// Also see ImGui::GetPlatformIO() and ImGuiPlatformIO struct for OS/platform related functions: clipboard, IME etc. +//----------------------------------------------------------------------------- // [Internal] Storage used by IsKeyDown(), IsKeyPressed() etc functions. // If prior to 1.87 you used io.KeysDownDuration[] (which was marked as internal), you should use GetKeyData(key)->DownDuration and *NOT* io.KeysData[key]->DownDuration. @@ -2210,7 +2230,7 @@ struct ImGuiIO // Configuration // Default value //------------------------------------------------------------------ - ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Gamepad/keyboard navigation options, etc. + ImGuiConfigFlags ConfigFlags; // = 0 // See ImGuiConfigFlags_ enum. Set by user/application. Keyboard/Gamepad navigation options, etc. ImGuiBackendFlags BackendFlags; // = 0 // See ImGuiBackendFlags_ enum. Set by backend (imgui_impl_xxx files or custom backend) to communicate features supported by the backend. ImVec2 DisplaySize; // // Main display size, in pixels (generally == GetMainViewport()->Size). May change every frame. float DeltaTime; // = 1.0f/60.0f // Time elapsed since last frame, in seconds. May change every frame. @@ -2219,22 +2239,33 @@ struct ImGuiIO const char* LogFilename; // = "imgui_log.txt"// Path to .log file (default parameter to ImGui::LogToFile when no file is specified). void* UserData; // = NULL // Store your own data. + // Font system ImFontAtlas*Fonts; // // Font atlas: load, rasterize and pack one or more fonts into a single texture. float FontGlobalScale; // = 1.0f // Global scale all fonts bool FontAllowUserScaling; // = false // Allow user scaling text of individual window with CTRL+Wheel. ImFont* FontDefault; // = NULL // Font to use on NewFrame(). Use NULL to uses Fonts->Fonts[0]. ImVec2 DisplayFramebufferScale; // = (1, 1) // For retina display or other situations where window coordinates are different from framebuffer coordinates. This generally ends up in ImDrawData::FramebufferScale. + // Keyboard/Gamepad Navigation options + bool ConfigNavSwapGamepadButtons; // = false // Swap Activate<>Cancel (A<>B) buttons, matching typical "Nintendo/Japanese style" gamepad layout. + bool ConfigNavMoveSetMousePos; // = false // Directional/tabbing navigation teleports the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is difficult. Will update io.MousePos and set io.WantSetMousePos=true. + bool ConfigNavCaptureKeyboard; // = true // Sets io.WantCaptureKeyboard when io.NavActive is set. + bool ConfigNavEscapeClearFocusItem; // = true // Pressing Escape can clear focused item + navigation id/highlight. Set to false if you want to always keep highlight on. + bool ConfigNavEscapeClearFocusWindow;// = false // Pressing Escape can clear focused window as well (super set of io.ConfigNavEscapeClearFocusItem). + bool ConfigNavCursorVisibleAuto; // = true // Using directional navigation key makes the cursor visible. Mouse click hides the cursor. + bool ConfigNavCursorVisibleAlways; // = false // Navigation cursor is always visible. + // Miscellaneous options + // (you can visualize and interact with all options in 'Demo->Configuration') bool MouseDrawCursor; // = false // Request ImGui to draw a mouse cursor for you (if you are on a platform without a mouse cursor). Cannot be easily renamed to 'io.ConfigXXX' because this is frequently used by backend implementations. bool ConfigMacOSXBehaviors; // = defined(__APPLE__) // Swap Cmd<>Ctrl keys + OS X style text editing cursor movement using Alt instead of Ctrl, Shortcuts using Cmd/Super instead of Ctrl, Line/Text Start and End using Cmd+Arrows instead of Home/End, Double click selects by word instead of selecting whole text, Multi-selection in lists uses Cmd/Super instead of Ctrl. - bool ConfigNavSwapGamepadButtons; // = false // Swap Activate<>Cancel (A<>B) buttons, matching typical "Nintendo/Japanese style" gamepad layout. bool ConfigInputTrickleEventQueue; // = true // Enable input queue trickling: some types of events submitted during the same frame (e.g. button down + up) will be spread over multiple frames, improving interactions with low framerates. bool ConfigInputTextCursorBlink; // = true // Enable blinking cursor (optional as some users consider it to be distracting). bool ConfigInputTextEnterKeepActive; // = false // [BETA] Pressing Enter will keep item active and select contents (single-line only). bool ConfigDragClickToInputText; // = false // [BETA] Enable turning DragXXX widgets into text input with a simple mouse click-release (without moving). Not desirable on devices without a keyboard. bool ConfigWindowsResizeFromEdges; // = true // Enable resizing of windows from their edges and from the lower-left corner. This requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback. (This used to be a per-window ImGuiWindowFlags_ResizeFromAnySide flag) - bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. + bool ConfigWindowsMoveFromTitleBarOnly; // = false // Enable allowing to move windows only when clicking on their title bar. Does not apply to windows without a title bar. + bool ConfigScrollbarScrollByPage; // = true // Enable scrolling page by page when clicking outside the scrollbar grab. When disabled, always scroll to clicked location. When enabled, Shift+Click scrolls to clicked location. float ConfigMemoryCompactTimer; // = 60.0f // Timer (in seconds) to free transient windows/tables memory buffers when unused. Set to -1.0f to disable. // Inputs Behaviors @@ -2249,12 +2280,37 @@ struct ImGuiIO // Debug options //------------------------------------------------------------------ + // Options to configure Error Handling and how we handle recoverable errors [EXPERIMENTAL] + // - Error recovery is provided as a way to facilitate: + // - Recovery after a programming error (native code or scripting language - the later tends to facilitate iterating on code while running). + // - Recovery after running an exception handler or any error processing which may skip code after an error has been detected. + // - Error recovery is not perfect nor guaranteed! It is a feature to ease development. + // You not are not supposed to rely on it in the course of a normal application run. + // - Functions that support error recovery are using IM_ASSERT_USER_ERROR() instead of IM_ASSERT(). + // - By design, we do NOT allow error recovery to be 100% silent. One of the three options needs to be checked! + // - Always ensure that on programmers seats you have at minimum Asserts or Tooltips enabled when making direct imgui API calls! + // Otherwise it would severely hinder your ability to catch and correct mistakes! + // Read https://github.com/ocornut/imgui/wiki/Error-Handling for details. + // - Programmer seats: keep asserts (default), or disable asserts and keep error tooltips (new and nice!) + // - Non-programmer seats: maybe disable asserts, but make sure errors are resurfaced (tooltips, visible log entries, use callback etc.) + // - Recovery after error/exception: record stack sizes with ErrorRecoveryStoreState(), disable assert, set log callback (to e.g. trigger high-level breakpoint), recover with ErrorRecoveryTryToRecoverState(), restore settings. + bool ConfigErrorRecovery; // = true // Enable error recovery support. Some errors won't be detected and lead to direct crashes if recovery is disabled. + bool ConfigErrorRecoveryEnableAssert; // = true // Enable asserts on recoverable error. By default call IM_ASSERT() when returning from a failing IM_ASSERT_USER_ERROR() + bool ConfigErrorRecoveryEnableDebugLog; // = true // Enable debug log output on recoverable errors. + bool ConfigErrorRecoveryEnableTooltip; // = true // Enable tooltip on recoverable errors. The tooltip include a way to enable asserts if they were disabled. + // Option to enable various debug tools showing buttons that will call the IM_DEBUG_BREAK() macro. // - The Item Picker tool will be available regardless of this being enabled, in order to maximize its discoverability. // - Requires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application. // e.g. io.ConfigDebugIsDebuggerPresent = ::IsDebuggerPresent() on Win32, or refer to ImOsIsDebuggerPresent() imgui_test_engine/imgui_te_utils.cpp for a Unix compatible version). bool ConfigDebugIsDebuggerPresent; // = false // Enable various tools calling IM_DEBUG_BREAK(). + // Tools to detect code submitting items with conflicting/duplicate IDs + // - Code should use PushID()/PopID() in loops, or append "##xx" to same-label identifiers. + // - Empty label e.g. Button("") == same ID as parent widget/node. Use Button("##xx") instead! + // - See FAQ https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-about-the-id-stack-system + bool ConfigDebugHighlightIdConflicts;// = true // Highlight and show an error message when multiple items have conflicting identifiers. + // Tools to test correct Begin/End and BeginChild/EndChild behaviors. // - Presently Begin()/End() and BeginChild()/EndChild() needs to ALWAYS be called in tandem, regardless of return value of BeginXXX() // - This is inconsistent with other BeginXXX functions and create confusion for many users. @@ -2271,7 +2327,7 @@ struct ImGuiIO bool ConfigDebugIniSettings; // = false // Save .ini data with extra comments (particularly helpful for Docking, but makes saving slower) //------------------------------------------------------------------ - // Platform Functions + // Platform Identifiers // (the imgui_impl_xxxx backend files are setting those up for you) //------------------------------------------------------------------ @@ -2282,25 +2338,6 @@ struct ImGuiIO void* BackendRendererUserData; // = NULL // User data for renderer backend void* BackendLanguageUserData; // = NULL // User data for non C++ programming language backend - // Optional: Access OS clipboard - // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) - const char* (*GetClipboardTextFn)(void* user_data); - void (*SetClipboardTextFn)(void* user_data, const char* text); - void* ClipboardUserData; - - // Optional: Open link/folder/file in OS Shell - // (default to use ShellExecuteA() on Windows, system() on Linux/Mac) - bool (*PlatformOpenInShellFn)(ImGuiContext* ctx, const char* path); - void* PlatformOpenInShellUserData; - - // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) - // (default to use native imm32 api on Windows) - void (*PlatformSetImeDataFn)(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); - //void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); // [Renamed to io.PlatformSetImeDataFn in 1.91.0] - - // Optional: Platform locale - ImWchar PlatformLocaleDecimalPoint; // '.' // [Experimental] Configure decimal point e.g. '.' or ',' useful for some languages (e.g. German), generally pulled from *localeconv()->decimal_point - //------------------------------------------------------------------ // Input - Call before calling NewFrame() //------------------------------------------------------------------ @@ -2335,10 +2372,10 @@ struct ImGuiIO bool WantCaptureMouse; // Set when Dear ImGui will use mouse inputs, in this case do not dispatch them to your main game/application (either way, always pass on mouse inputs to imgui). (e.g. unclicked mouse is hovering over an imgui window, widget is active, mouse was clicked over an imgui window, etc.). bool WantCaptureKeyboard; // Set when Dear ImGui will use keyboard inputs, in this case do not dispatch them to your main game/application (either way, always pass keyboard inputs to imgui). (e.g. InputText active, or an imgui window is focused and navigation is enabled, etc.). bool WantTextInput; // Mobile/console: when set, you may display an on-screen keyboard. This is set by Dear ImGui when it wants textual keyboard input to happen (e.g. when a InputText widget is active). - bool WantSetMousePos; // MousePos has been altered, backend should reposition mouse on next frame. Rarely used! Set only when ImGuiConfigFlags_NavEnableSetMousePos flag is enabled. + bool WantSetMousePos; // MousePos has been altered, backend should reposition mouse on next frame. Rarely used! Set only when io.ConfigNavMoveSetMousePos is enabled. bool WantSaveIniSettings; // When manual .ini load/save is active (io.IniFilename == NULL), this will be set to notify your application that you can call SaveIniSettingsToMemory() and save yourself. Important: clear io.WantSaveIniSettings yourself after saving! bool NavActive; // Keyboard/Gamepad navigation is currently allowed (will handle ImGuiKey_NavXXX events) = a window is focused and it doesn't use the ImGuiWindowFlags_NoNavInputs flag. - bool NavVisible; // Keyboard/Gamepad navigation is visible and allowed (will handle ImGuiKey_NavXXX events). + bool NavVisible; // Keyboard/Gamepad navigation highlight is visible and allowed (will handle ImGuiKey_NavXXX events). float Framerate; // Estimate of application framerate (rolling average over 60 frames, based on io.DeltaTime), in frame per second. Solely for convenience. Slow applications may not want to use a moving average or may want to reset underlying buffers occasionally. int MetricsRenderVertices; // Vertices output during last call to Render() int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 @@ -2402,6 +2439,14 @@ struct ImGuiIO //void* ImeWindowHandle; // [Obsoleted in 1.87] Set ImGuiViewport::PlatformHandleRaw instead. Set this to your HWND to get automatic IME cursor positioning. #endif + // Legacy: before 1.91.1, clipboard functions were stored in ImGuiIO instead of ImGuiPlatformIO. + // As this is will affect all users of custom engines/backends, we are providing proper legacy redirection (will obsolete). +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + const char* (*GetClipboardTextFn)(void* user_data); + void (*SetClipboardTextFn)(void* user_data, const char* text); + void* ClipboardUserData; +#endif + IMGUI_API ImGuiIO(); }; @@ -2664,7 +2709,7 @@ struct ImGuiListClipper // Helpers: ImVec2/ImVec4 operators // - It is important that we are keeping those disabled by default so they don't leak in user space. // - This is in order to allow user enabling implicit cast operators between ImVec2/ImVec4 and their own types (using IM_VEC2_CLASS_EXTRA in imconfig.h) -// - You can use '#define IMGUI_DEFINE_MATH_OPERATORS' to import our operators, provided as a courtesy. +// - Add '#define IMGUI_DEFINE_MATH_OPERATORS' before including this file (or in imconfig.h) to access courtesy maths operators for ImVec2 and ImVec4. #ifdef IMGUI_DEFINE_MATH_OPERATORS #define IMGUI_DEFINE_MATH_OPERATORS_IMPLEMENTED IM_MSVC_RUNTIME_CHECKS_OFF @@ -2859,7 +2904,7 @@ struct ImGuiSelectionBasicStorage ImGuiStorage _Storage; // [Internal] Selection set. Think of this as similar to e.g. std::set. Prefer not accessing directly: iterate with GetNextSelectedItem(). // Methods - ImGuiSelectionBasicStorage(); + IMGUI_API ImGuiSelectionBasicStorage(); IMGUI_API void ApplyRequests(ImGuiMultiSelectIO* ms_io); // Apply selection requests coming from BeginMultiSelect() and EndMultiSelect() functions. It uses 'items_count' passed to BeginMultiSelect() IMGUI_API bool Contains(ImGuiID id) const; // Query if an item id is in selection. IMGUI_API void Clear(); // Clear selection @@ -2922,9 +2967,11 @@ struct ImDrawCmd unsigned int IdxOffset; // 4 // Start offset in index buffer. unsigned int ElemCount; // 4 // Number of indices (multiple of 3) to be rendered as triangles. Vertices are stored in the callee ImDrawList's vtx_buffer[] array, indices in idx_buffer[]. ImDrawCallback UserCallback; // 4-8 // If != NULL, call the function instead of rendering the vertices. clip_rect and texture_id will be set normally. - void* UserCallbackData; // 4-8 // The draw callback code can access this. + void* UserCallbackData; // 4-8 // Callback user data (when UserCallback != NULL). If called AddCallback() with size == 0, this is a copy of the AddCallback() argument. If called AddCallback() with size > 0, this is pointing to a buffer where data is stored. + int UserCallbackDataSize; // 4 // Size of callback user data when using storage, otherwise 0. + int UserCallbackDataOffset;// 4 // [Internal] Offset of callback user data when using storage, otherwise -1. - ImDrawCmd() { memset(this, 0, sizeof(*this)); } // Also ensure our padding fields are zeroed + ImDrawCmd() { memset(this, 0, sizeof(*this)); } // Also ensure our padding fields are zeroed // Since 1.83: returns ImTextureID associated with this draw call. Warning: DO NOT assume this is always same as 'TextureId' (we will change this function for an upcoming feature) inline ImTextureID GetTexID() const { return TextureId; } @@ -3037,6 +3084,7 @@ struct ImDrawList ImDrawListSplitter _Splitter; // [Internal] for channels api (note: prefer using your own persistent instance of ImDrawListSplitter!) ImVector _ClipRectStack; // [Internal] ImVector _TextureIdStack; // [Internal] + ImVector _CallbacksDataBuf; // [Internal] float _FringeScale; // [Internal] anti-alias fringe is scaled by this value, this helps to keep things sharp while zooming at vertex buffer content const char* _OwnerName; // Pointer to owner window's name for debugging @@ -3109,8 +3157,18 @@ struct ImDrawList IMGUI_API void PathBezierQuadraticCurveTo(const ImVec2& p2, const ImVec2& p3, int num_segments = 0); // Quadratic Bezier (3 control points) IMGUI_API void PathRect(const ImVec2& rect_min, const ImVec2& rect_max, float rounding = 0.0f, ImDrawFlags flags = 0); - // Advanced - IMGUI_API void AddCallback(ImDrawCallback callback, void* callback_data); // Your rendering function must check for 'UserCallback' in ImDrawCmd and call the function instead of rendering triangles. + // Advanced: Draw Callbacks + // - May be used to alter render state (change sampler, blending, current shader). May be used to emit custom rendering commands (difficult to do correctly, but possible). + // - Use special ImDrawCallback_ResetRenderState callback to instruct backend to reset its render state to the default. + // - Your rendering loop must check for 'UserCallback' in ImDrawCmd and call the function instead of rendering triangles. All standard backends are honoring this. + // - For some backends, the callback may access selected render-states exposed by the backend in a ImGui_ImplXXXX_RenderState structure pointed to by platform_io.Renderer_RenderState. + // - IMPORTANT: please be mindful of the different level of indirection between using size==0 (copying argument) and using size>0 (copying pointed data into a buffer). + // - If userdata_size == 0: we copy/store the 'userdata' argument as-is. It will be available unmodified in ImDrawCmd::UserCallbackData during render. + // - If userdata_size > 0, we copy/store 'userdata_size' bytes pointed to by 'userdata'. We store them in a buffer stored inside the drawlist. ImDrawCmd::UserCallbackData will point inside that buffer so you have to retrieve data from there. Your callback may need to use ImDrawCmd::UserCallbackDataSize if you expect dynamically-sized data. + // - Support for userdata_size > 0 was added in v1.91.4, October 2024. So earlier code always only allowed to copy/store a simple void*. + IMGUI_API void AddCallback(ImDrawCallback callback, void* userdata, size_t userdata_size = 0); + + // Advanced: Miscellaneous IMGUI_API void AddDrawCmd(); // This is useful if you need to forcefully create a new draw call (to allow for dependent rendering / blending). Otherwise primitives are merged into the same draw-call as much as possible IMGUI_API ImDrawList* CloneOutput() const; // Create a clone of the CmdBuffer/IdxBuffer/VtxBuffer. @@ -3140,8 +3198,8 @@ struct ImDrawList //inline void AddEllipse(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0, float thickness = 1.0f) { AddEllipse(center, ImVec2(radius_x, radius_y), col, rot, num_segments, thickness); } // OBSOLETED in 1.90.5 (Mar 2024) //inline void AddEllipseFilled(const ImVec2& center, float radius_x, float radius_y, ImU32 col, float rot = 0.0f, int num_segments = 0) { AddEllipseFilled(center, ImVec2(radius_x, radius_y), col, rot, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024) //inline void PathEllipticalArcTo(const ImVec2& center, float radius_x, float radius_y, float rot, float a_min, float a_max, int num_segments = 0) { PathEllipticalArcTo(center, ImVec2(radius_x, radius_y), rot, a_min, a_max, num_segments); } // OBSOLETED in 1.90.5 (Mar 2024) - //inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) - //inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) + //inline void AddBezierCurve(const ImVec2& p1, const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, ImU32 col, float thickness, int num_segments = 0) { AddBezierCubic(p1, p2, p3, p4, col, thickness, num_segments); } // OBSOLETED in 1.80 (Jan 2021) + //inline void PathBezierCurveTo(const ImVec2& p2, const ImVec2& p3, const ImVec2& p4, int num_segments = 0) { PathBezierCubicCurveTo(p2, p3, p4, num_segments); } // OBSOLETED in 1.80 (Jan 2021) // [Internal helpers] IMGUI_API void _ResetForNewFrame(); @@ -3151,6 +3209,7 @@ struct ImDrawList IMGUI_API void _OnChangedClipRect(); IMGUI_API void _OnChangedTextureID(); IMGUI_API void _OnChangedVtxOffset(); + IMGUI_API void _SetTextureID(ImTextureID texture_id); IMGUI_API int _CalcCircleAutoSegmentCount(float radius) const; IMGUI_API void _PathArcToFastEx(const ImVec2& center, float radius, int a_min_sample, int a_max_sample, int a_step); IMGUI_API void _PathArcToN(const ImVec2& center, float radius, float a_min, float a_max, int num_segments); @@ -3385,7 +3444,7 @@ struct ImFontAtlas struct ImFont { // Members: Hot ~20/24 bytes (for CalcTextSize) - ImVector IndexAdvanceX; // 12-16 // out // // Sparse. Glyphs->AdvanceX in a directly indexable way (cache-friendly for CalcTextSize functions which only this this info, and are often bottleneck in large UI). + ImVector IndexAdvanceX; // 12-16 // out // // Sparse. Glyphs->AdvanceX in a directly indexable way (cache-friendly for CalcTextSize functions which only this info, and are often bottleneck in large UI). float FallbackAdvanceX; // 4 // out // = FallbackGlyph->AdvanceX float FontSize; // 4 // in // // Height of characters/line, set during loading (don't change after loading) @@ -3445,7 +3504,7 @@ enum ImGuiViewportFlags_ ImGuiViewportFlags_None = 0, ImGuiViewportFlags_IsPlatformWindow = 1 << 0, // Represent a Platform Window ImGuiViewportFlags_IsPlatformMonitor = 1 << 1, // Represent a Platform Monitor (unused yet) - ImGuiViewportFlags_OwnedByApp = 1 << 2, // Platform Window: is created/managed by the application (rather than a dear imgui backend) + ImGuiViewportFlags_OwnedByApp = 1 << 2, // Platform Window: Is created/managed by the application (rather than a dear imgui backend) }; // - Currently represents the Platform Window created by the application which is hosting our Dear ImGui windows. @@ -3479,7 +3538,45 @@ struct ImGuiViewport // [SECTION] Platform Dependent Interfaces //----------------------------------------------------------------------------- -// (Optional) Support for IME (Input Method Editor) via the io.PlatformSetImeDataFn() function. +// Access via ImGui::GetPlatformIO() +struct ImGuiPlatformIO +{ + IMGUI_API ImGuiPlatformIO(); + + //------------------------------------------------------------------ + // Interface with OS and Platform backend + //------------------------------------------------------------------ + + // Optional: Access OS clipboard + // (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures) + const char* (*Platform_GetClipboardTextFn)(ImGuiContext* ctx); + void (*Platform_SetClipboardTextFn)(ImGuiContext* ctx, const char* text); + void* Platform_ClipboardUserData; + + // Optional: Open link/folder/file in OS Shell + // (default to use ShellExecuteA() on Windows, system() on Linux/Mac) + bool (*Platform_OpenInShellFn)(ImGuiContext* ctx, const char* path); + void* Platform_OpenInShellUserData; + + // Optional: Notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese IME on Windows) + // (default to use native imm32 api on Windows) + void (*Platform_SetImeDataFn)(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); + void* Platform_ImeUserData; + //void (*SetPlatformImeDataFn)(ImGuiViewport* viewport, ImGuiPlatformImeData* data); // [Renamed to platform_io.PlatformSetImeDataFn in 1.91.1] + + // Optional: Platform locale + // [Experimental] Configure decimal point e.g. '.' or ',' useful for some languages (e.g. German), generally pulled from *localeconv()->decimal_point + ImWchar Platform_LocaleDecimalPoint; // '.' + + //------------------------------------------------------------------ + // Interface with Renderer Backend + //------------------------------------------------------------------ + + // Written by some backends during ImGui_ImplXXXX_RenderDrawData() call to point backend_specific ImGui_ImplXXXX_RenderState* structure. + void* Renderer_RenderState; +}; + +// (Optional) Support for IME (Input Method Editor) via the platform_io.Platform_SetImeDataFn() function. struct ImGuiPlatformImeData { bool WantVisible; // A widget wants the IME to be visible @@ -3509,8 +3606,8 @@ namespace ImGui // OBSOLETED in 1.90.0 (from September 2023) static inline bool BeginChildFrame(ImGuiID id, const ImVec2& size, ImGuiWindowFlags window_flags = 0) { return BeginChild(id, size, ImGuiChildFlags_FrameStyle, window_flags); } static inline void EndChildFrame() { EndChild(); } - //static inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border - //static inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool border, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, border ? ImGuiChildFlags_Border : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Border + //static inline bool BeginChild(const char* str_id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags){ return BeginChild(str_id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders + //static inline bool BeginChild(ImGuiID id, const ImVec2& size_arg, bool borders, ImGuiWindowFlags window_flags) { return BeginChild(id, size_arg, borders ? ImGuiChildFlags_Borders : ImGuiChildFlags_None, window_flags); } // Unnecessary as true == ImGuiChildFlags_Borders static inline void ShowStackToolWindow(bool* p_open = NULL) { ShowIDStackToolWindow(p_open); } IMGUI_API bool Combo(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int popup_max_height_in_items = -1); IMGUI_API bool ListBox(const char* label, int* current_item, bool (*old_callback)(void* user_data, int idx, const char** out_text), void* user_data, int items_count, int height_in_items = -1); @@ -3519,16 +3616,16 @@ namespace ImGui // OBSOLETED in 1.89.4 (from March 2023) static inline void PushAllowKeyboardFocus(bool tab_stop) { PushItemFlag(ImGuiItemFlags_NoTabStop, !tab_stop); } static inline void PopAllowKeyboardFocus() { PopItemFlag(); } - // OBSOLETED in 1.89 (from August 2022) - IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // Use new ImageButton() signature (explicit item id, regular FramePadding) // OBSOLETED in 1.87 (from February 2022 but more formally obsoleted April 2024) IMGUI_API ImGuiKey GetKeyIndex(ImGuiKey key); // Map ImGuiKey_* values into legacy native key index. == io.KeyMap[key]. When using a 1.87+ backend using io.AddKeyEvent(), calling GetKeyIndex() with ANY ImGuiKey_XXXX values will return the same value! //static inline ImGuiKey GetKeyIndex(ImGuiKey key) { IM_ASSERT(key >= ImGuiKey_NamedKey_BEGIN && key < ImGuiKey_NamedKey_END); return key; } // Some of the older obsolete names along with their replacement (commented out so they are not reported in IDE) + //-- OBSOLETED in 1.89 (from August 2022) + //IMGUI_API bool ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), int frame_padding = -1, const ImVec4& bg_col = ImVec4(0, 0, 0, 0), const ImVec4& tint_col = ImVec4(1, 1, 1, 1)); // --> Use new ImageButton() signature (explicit item id, regular FramePadding). Refer to code in 1.91 if you want to grab a copy of this version. //-- OBSOLETED in 1.88 (from May 2022) - //static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. - //static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. + //static inline void CaptureKeyboardFromApp(bool want_capture_keyboard = true) { SetNextFrameWantCaptureKeyboard(want_capture_keyboard); } // Renamed as name was misleading + removed default value. + //static inline void CaptureMouseFromApp(bool want_capture_mouse = true) { SetNextFrameWantCaptureMouse(want_capture_mouse); } // Renamed as name was misleading + removed default value. //-- OBSOLETED in 1.86 (from November 2021) //IMGUI_API void CalcListClipping(int items_count, float items_height, int* out_items_display_start, int* out_items_display_end); // Code removed, see 1.90 for last version of the code. Calculate range of visible items for large list of evenly sized items. Prefer using ImGuiListClipper. //-- OBSOLETED in 1.85 (from August 2021) @@ -3611,10 +3708,7 @@ namespace ImGui #endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // RENAMED IMGUI_DISABLE_METRICS_WINDOW > IMGUI_DISABLE_DEBUG_TOOLS in 1.88 (from June 2022) -#if defined(IMGUI_DISABLE_METRICS_WINDOW) && !defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) && !defined(IMGUI_DISABLE_DEBUG_TOOLS) -#define IMGUI_DISABLE_DEBUG_TOOLS -#endif -#if defined(IMGUI_DISABLE_METRICS_WINDOW) && defined(IMGUI_DISABLE_OBSOLETE_FUNCTIONS) +#ifdef IMGUI_DISABLE_METRICS_WINDOW #error IMGUI_DISABLE_METRICS_WINDOW was renamed to IMGUI_DISABLE_DEBUG_TOOLS, please use new name. #endif diff --git a/include/imgui/imgui_impl_glfw.h b/include/imgui/imgui_impl_glfw.h index c0eac113..60b95bd9 100644 --- a/include/imgui/imgui_impl_glfw.h +++ b/include/imgui/imgui_impl_glfw.h @@ -57,4 +57,7 @@ IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event); +// GLFW helpers +IMGUI_IMPL_API void ImGui_ImplGlfw_Sleep(int milliseconds); + #endif // #ifndef IMGUI_DISABLE diff --git a/include/imgui/imgui_impl_opengl3_loader.h b/include/imgui/imgui_impl_opengl3_loader.h index 3fbc3481..d6ffa5a2 100644 --- a/include/imgui/imgui_impl_opengl3_loader.h +++ b/include/imgui/imgui_impl_opengl3_loader.h @@ -181,6 +181,9 @@ typedef khronos_uint8_t GLubyte; #define GL_LINEAR 0x2601 #define GL_TEXTURE_MAG_FILTER 0x2800 #define GL_TEXTURE_MIN_FILTER 0x2801 +#define GL_TEXTURE_WRAP_S 0x2802 +#define GL_TEXTURE_WRAP_T 0x2803 +#define GL_REPEAT 0x2901 typedef void (APIENTRYP PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode); typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height); typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param); @@ -231,6 +234,9 @@ GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures); GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures); #endif #endif /* GL_VERSION_1_1 */ +#ifndef GL_VERSION_1_2 +#define GL_CLAMP_TO_EDGE 0x812F +#endif /* GL_VERSION_1_2 */ #ifndef GL_VERSION_1_3 #define GL_TEXTURE0 0x84C0 #define GL_ACTIVE_TEXTURE 0x84E0 diff --git a/include/imgui/imgui_internal.h b/include/imgui/imgui_internal.h index ab43922b..c9ddceb3 100644 --- a/include/imgui/imgui_internal.h +++ b/include/imgui/imgui_internal.h @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (internal structures/api) // You may use this file to debug, understand or extend Dear ImGui features but we don't provide any guarantee of forward compatibility. @@ -28,6 +28,7 @@ Index of this file: // [SECTION] Viewport support // [SECTION] Settings support // [SECTION] Localization support +// [SECTION] Error handling, State recovery support // [SECTION] Metrics, Debug tools // [SECTION] Generic context hooks // [SECTION] ImGuiContext (main imgui context) @@ -94,6 +95,7 @@ Index of this file: #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wclass-memaccess" // [__GNUC__ >= 8] warning: 'memset/memcpy' clearing/writing an object of type 'xxxx' with no trivial copy-assignment; use assignment or value-initialization instead +#pragma GCC diagnostic ignored "-Wdeprecated-enum-enum-conversion" // warning: bitwise operation between different enumeration types ('XXXFlags_' and 'XXXFlagsPrivate_') is deprecated #endif // In 1.89.4, we moved the implementation of "courtesy maths operators" from imgui_internal.h in imgui.h @@ -130,6 +132,7 @@ struct ImGuiContext; // Main Dear ImGui context struct ImGuiContextHook; // Hook for extensions like ImGuiTestEngine struct ImGuiDataVarInfo; // Variable information (e.g. to access style variables from an enum) struct ImGuiDataTypeInfo; // Type information associated to a ImGuiDataType enum +struct ImGuiErrorRecoveryState; // Storage of stack sizes for error handling and recovery struct ImGuiGroupData; // Stacked storage data for BeginGroup()/EndGroup() struct ImGuiInputTextState; // Internal state of the currently focused/edited text input box struct ImGuiInputTextDeactivateData;// Short term storage to backup text of a deactivating InputText() while another is stealing active id @@ -138,7 +141,7 @@ struct ImGuiLocEntry; // A localization entry. struct ImGuiMenuColumns; // Simple column measurement, currently used for MenuItem() only struct ImGuiMultiSelectState; // Multi-selection persistent state (for focused selection). struct ImGuiMultiSelectTempData; // Multi-selection temporary state (while traversing). -struct ImGuiNavItemData; // Result of a gamepad/keyboard directional navigation move query result +struct ImGuiNavItemData; // Result of a keyboard/gamepad directional navigation move query result struct ImGuiMetricsConfig; // Storage for ShowMetricsWindow() and DebugNodeXXX() functions struct ImGuiNextWindowData; // Storage for SetNextWindow** functions struct ImGuiNextItemData; // Storage for SetNextItem** functions @@ -146,7 +149,6 @@ struct ImGuiOldColumnData; // Storage data for a single column for lega struct ImGuiOldColumns; // Storage data for a columns set for legacy Columns() api struct ImGuiPopupData; // Storage for current popup stack struct ImGuiSettingsHandler; // Storage for one type registered in the .ini file -struct ImGuiStackSizes; // Storage of stack sizes for debugging/asserting struct ImGuiStyleMod; // Stacked style modifier, backup of modified data so we can restore it struct ImGuiTabBar; // Storage for a tab bar struct ImGuiTabItem; // Storage for a tab item (within a tab bar) @@ -172,10 +174,10 @@ typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // E // Flags typedef int ImGuiActivateFlags; // -> enum ImGuiActivateFlags_ // Flags: for navigation/focus function (will be for ActivateItem() later) typedef int ImGuiDebugLogFlags; // -> enum ImGuiDebugLogFlags_ // Flags: for ShowDebugLogWindow(), g.DebugLogFlags -typedef int ImGuiFocusRequestFlags; // -> enum ImGuiFocusRequestFlags_ // Flags: for FocusWindow(); +typedef int ImGuiFocusRequestFlags; // -> enum ImGuiFocusRequestFlags_ // Flags: for FocusWindow() typedef int ImGuiItemStatusFlags; // -> enum ImGuiItemStatusFlags_ // Flags: for g.LastItemData.StatusFlags typedef int ImGuiOldColumnFlags; // -> enum ImGuiOldColumnFlags_ // Flags: for BeginColumns() -typedef int ImGuiNavHighlightFlags; // -> enum ImGuiNavHighlightFlags_ // Flags: for RenderNavHighlight() +typedef int ImGuiNavRenderCursorFlags; // -> enum ImGuiNavRenderCursorFlags_//Flags: for RenderNavCursor() typedef int ImGuiNavMoveFlags; // -> enum ImGuiNavMoveFlags_ // Flags: for navigation requests typedef int ImGuiNextItemDataFlags; // -> enum ImGuiNextItemDataFlags_ // Flags: for SetNextItemXXX() functions typedef int ImGuiNextWindowDataFlags; // -> enum ImGuiNextWindowDataFlags_// Flags: for SetNextWindowXXX() functions @@ -186,8 +188,6 @@ typedef int ImGuiTooltipFlags; // -> enum ImGuiTooltipFlags_ // F typedef int ImGuiTypingSelectFlags; // -> enum ImGuiTypingSelectFlags_ // Flags: for GetTypingSelectRequest() typedef int ImGuiWindowRefreshFlags; // -> enum ImGuiWindowRefreshFlags_ // Flags: for SetNextWindowRefreshPolicy() -typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...); - //----------------------------------------------------------------------------- // [SECTION] Context pointer // See implementation of this variable in imgui.cpp for comments and details. @@ -197,24 +197,6 @@ typedef void (*ImGuiErrorLogCallback)(void* user_data, const char* fmt, ...); extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer #endif -//------------------------------------------------------------------------- -// [SECTION] STB libraries includes -//------------------------------------------------------------------------- - -namespace ImStb -{ - -#undef IMSTB_TEXTEDIT_STRING -#undef IMSTB_TEXTEDIT_CHARTYPE -#define IMSTB_TEXTEDIT_STRING ImGuiInputTextState -#define IMSTB_TEXTEDIT_CHARTYPE ImWchar -#define IMSTB_TEXTEDIT_GETWIDTH_NEWLINE (-1.0f) -#define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 -#define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 -#include "imstb_textedit.h" - -} // namespace ImStb - //----------------------------------------------------------------------------- // [SECTION] Macros //----------------------------------------------------------------------------- @@ -235,6 +217,7 @@ namespace ImStb #else #define IMGUI_DEBUG_LOG(...) ((void)0) #endif +#define IMGUI_DEBUG_LOG_ERROR(...) do { ImGuiContext& g2 = *GImGui; if (g2.DebugLogFlags & ImGuiDebugLogFlags_EventError) IMGUI_DEBUG_LOG(__VA_ARGS__); else g2.DebugLogSkippedErrors++; } while (0) #define IMGUI_DEBUG_LOG_ACTIVEID(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventActiveId) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_FOCUS(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventFocus) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) #define IMGUI_DEBUG_LOG_POPUP(...) do { if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) IMGUI_DEBUG_LOG(__VA_ARGS__); } while (0) @@ -256,12 +239,6 @@ namespace ImStb #define IM_ASSERT_PARANOID(_EXPR) #endif -// Error handling -// Down the line in some frameworks/languages we would like to have a way to redirect those to the programmer and recover from more faults. -#ifndef IM_ASSERT_USER_ERROR -#define IM_ASSERT_USER_ERROR(_EXP,_MSG) IM_ASSERT((_EXP) && _MSG) // Recoverable User Error -#endif - // Misc Macros #define IM_PI 3.14159265358979323846f #ifdef _WIN32 @@ -383,7 +360,7 @@ IMGUI_API const char* ImStristr(const char* haystack, const char* haystack_end IMGUI_API void ImStrTrimBlanks(char* str); // Remove leading and trailing blanks from a buffer. IMGUI_API const char* ImStrSkipBlank(const char* str); // Find first non-blank character. IMGUI_API int ImStrlenW(const ImWchar* str); // Computer string length (ImWchar string) -IMGUI_API const ImWchar*ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin); // Find beginning-of-line (ImWchar string) +IMGUI_API const char* ImStrbol(const char* buf_mid_line, const char* buf_begin); // Find beginning-of-line IM_MSVC_RUNTIME_CHECKS_OFF static inline char ImToUpper(char c) { return (c >= 'a' && c <= 'z') ? c &= ~32 : c; } static inline bool ImCharIsBlankA(char c) { return c == ' ' || c == '\t'; } @@ -846,16 +823,18 @@ enum ImGuiDataTypePrivate_ //----------------------------------------------------------------------------- // Extend ImGuiItemFlags -// - input: PushItemFlag() manipulates g.CurrentItemFlags, ItemAdd() calls may add extra flags. -// - output: stored in g.LastItemData.InFlags +// - input: PushItemFlag() manipulates g.CurrentItemFlags, g.NextItemData.ItemFlags, ItemAdd() calls may add extra flags too. +// - output: stored in g.LastItemData.ItemFlags enum ImGuiItemFlagsPrivate_ { // Controlled by user - ImGuiItemFlags_Disabled = 1 << 10, // false // Disable interactions (DOES NOT affect visuals, see BeginDisabled()/EndDisabled() for full disable feature, and github #211). + ImGuiItemFlags_Disabled = 1 << 10, // false // Disable interactions (DOES NOT affect visuals. DO NOT mix direct use of this with BeginDisabled(). See BeginDisabled()/EndDisabled() for full disable feature, and github #211). ImGuiItemFlags_ReadOnly = 1 << 11, // false // [ALPHA] Allow hovering interactions but underlying value is not changed. ImGuiItemFlags_MixedValue = 1 << 12, // false // [BETA] Represent a mixed/indeterminate value, generally multi-selection where values differ. Currently only supported by Checkbox() (later should support all sorts of widgets) ImGuiItemFlags_NoWindowHoverableCheck = 1 << 13, // false // Disable hoverable check in ItemHoverable() ImGuiItemFlags_AllowOverlap = 1 << 14, // false // Allow being overlapped by another widget. Not-hovered to Hovered transition deferred by a frame. + ImGuiItemFlags_NoNavDisableMouseHover = 1 << 15, // false // Nav keyboard/gamepad mode doesn't disable hover highlight (behave as if NavHighlightItemUnderNav==false). + ImGuiItemFlags_NoMarkEdited = 1 << 16, // false // Skip calling MarkItemEdited() // Controlled by widget code ImGuiItemFlags_Inputable = 1 << 20, // false // [WIP] Auto-activate input mode when tab focused. Currently only used and supported by a few items before it becomes a generic feature. @@ -908,9 +887,8 @@ enum ImGuiInputTextFlagsPrivate_ { // [Internal] ImGuiInputTextFlags_Multiline = 1 << 26, // For internal use by InputTextMultiline() - ImGuiInputTextFlags_NoMarkEdited = 1 << 27, // For internal use by functions using InputText() before reformatting data - ImGuiInputTextFlags_MergedItem = 1 << 28, // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. - ImGuiInputTextFlags_LocalizeDecimalPoint= 1 << 29, // For internal use by InputScalar() and TempInputScalar() + ImGuiInputTextFlags_MergedItem = 1 << 27, // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match. + ImGuiInputTextFlags_LocalizeDecimalPoint= 1 << 28, // For internal use by InputScalar() and TempInputScalar() }; // Extend ImGuiButtonFlags_ @@ -922,15 +900,15 @@ enum ImGuiButtonFlagsPrivate_ ImGuiButtonFlags_PressedOnRelease = 1 << 7, // return true on release (default requires click+release) ImGuiButtonFlags_PressedOnDoubleClick = 1 << 8, // return true on double-click (default requires click+release) ImGuiButtonFlags_PressedOnDragDropHold = 1 << 9, // return true when held into while we are drag and dropping another item (used by e.g. tree nodes, collapsing headers) - ImGuiButtonFlags_Repeat = 1 << 10, // hold to repeat + //ImGuiButtonFlags_Repeat = 1 << 10, // hold to repeat ImGuiButtonFlags_FlattenChildren = 1 << 11, // allow interactions even if a child window is overlapping ImGuiButtonFlags_AllowOverlap = 1 << 12, // require previous frame HoveredId to either match id or be null before being usable. - ImGuiButtonFlags_DontClosePopups = 1 << 13, // disable automatically closing parent popup on press // [UNUSED] + //ImGuiButtonFlags_DontClosePopups = 1 << 13, // disable automatically closing parent popup on press //ImGuiButtonFlags_Disabled = 1 << 14, // disable interactions -> use BeginDisabled() or ImGuiItemFlags_Disabled ImGuiButtonFlags_AlignTextBaseLine = 1 << 15, // vertically align button to match text baseline - ButtonEx() only // FIXME: Should be removed and handled by SmallButton(), not possible currently because of DC.CursorPosPrevLine - ImGuiButtonFlags_NoKeyModifiers = 1 << 16, // disable mouse interaction if a key modifier is held + ImGuiButtonFlags_NoKeyModsAllowed = 1 << 16, // disable mouse interaction if a key modifier is held ImGuiButtonFlags_NoHoldingActiveId = 1 << 17, // don't set ActiveId while holding the mouse (ImGuiButtonFlags_PressedOnClick only) - ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated (FIXME: this is essentially used every time an item uses ImGuiItemFlags_NoNav, but because legacy specs don't requires LastItemData to be set ButtonBehavior(), we can't poll g.LastItemData.InFlags) + ImGuiButtonFlags_NoNavFocus = 1 << 18, // don't override navigation focus when activated (FIXME: this is essentially used every time an item uses ImGuiItemFlags_NoNav, but because legacy specs don't requires LastItemData to be set ButtonBehavior(), we can't poll g.LastItemData.ItemFlags) ImGuiButtonFlags_NoHoveredOnFocus = 1 << 19, // don't report as hovered when nav focus is on this item ImGuiButtonFlags_NoSetKeyOwner = 1 << 20, // don't set key/input owner on the initial click (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) ImGuiButtonFlags_NoTestKeyOwner = 1 << 21, // don't test key/input owner when polling the key (note: mouse buttons are keys! often, the key in question will be ImGuiKey_MouseLeft!) @@ -969,7 +947,8 @@ enum ImGuiSelectableFlagsPrivate_ enum ImGuiTreeNodeFlagsPrivate_ { ImGuiTreeNodeFlags_ClipLabelForTrailingButton = 1 << 28,// FIXME-WIP: Hard-coded for CollapsingHeader() - ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 29,// FIXME-WIP: Turn Down arrow into an Up arrow, but reversed trees (#6517) + ImGuiTreeNodeFlags_UpsideDownArrow = 1 << 29,// FIXME-WIP: Turn Down arrow into an Up arrow, for reversed trees (#6517) + ImGuiTreeNodeFlags_OpenOnMask_ = ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_OpenOnArrow, }; enum ImGuiSeparatorFlags_ @@ -1108,20 +1087,31 @@ struct IMGUI_API ImGuiInputTextDeactivatedState ImGuiInputTextDeactivatedState() { memset(this, 0, sizeof(*this)); } void ClearFreeMemory() { ID = 0; TextA.clear(); } }; + +// Forward declare imstb_textedit.h structure + make its main configuration define accessible +#undef IMSTB_TEXTEDIT_STRING +#undef IMSTB_TEXTEDIT_CHARTYPE +#define IMSTB_TEXTEDIT_STRING ImGuiInputTextState +#define IMSTB_TEXTEDIT_CHARTYPE char +#define IMSTB_TEXTEDIT_GETWIDTH_NEWLINE (-1.0f) +#define IMSTB_TEXTEDIT_UNDOSTATECOUNT 99 +#define IMSTB_TEXTEDIT_UNDOCHARCOUNT 999 +namespace ImStb { struct STB_TexteditState; } +typedef ImStb::STB_TexteditState ImStbTexteditState; + // Internal state of the currently focused/edited text input box // For a given item ID, access with ImGui::GetInputTextState() struct IMGUI_API ImGuiInputTextState { ImGuiContext* Ctx; // parent UI context (needs to be set explicitly by parent). + ImStbTexteditState* Stb; // State for stb_textedit.h ImGuiID ID; // widget id owning the text state - int CurLenW, CurLenA; // we need to maintain our buffer length in both UTF-8 and wchar format. UTF-8 length is valid even if TextA is not. - ImVector TextW; // edit buffer, we need to persist but can't guarantee the persistence of the user-provided buffer. so we copy into own buffer. - ImVector TextA; // temporary UTF8 buffer for callbacks and other operations. this is not updated in every code-path! size=capacity. + int CurLenA; // UTF-8 length of the string in TextA (in bytes) + ImVector TextA; // main UTF8 buffer. ImVector InitialTextA; // value to revert to when pressing Escape = backup of end-user buffer at the time of focus (in UTF-8, unaltered) - bool TextAIsValid; // temporary UTF8 buffer is not initially valid before we make the widget active (until then we pull the data from user argument) + ImVector CallbackTextBackup; // temporary storage for callback to support automatic reconcile of undo-stack int BufCapacityA; // end-user buffer capacity - float ScrollX; // horizontal scrolling/offset - ImStb::STB_TexteditState Stb; // state for stb_textedit.h + ImVec2 Scroll; // horizontal offset (managed manually) + vertical scrolling (pulled from child window's own Scroll.y) float CursorAnim; // timer for cursor blink, reset on every user action so the cursor reappears immediately bool CursorFollow; // set when we want scrolling to follow the current cursor position (not always!) bool SelectedAllMouseLock; // after a double-click to select all, we ignore further mouse drags to update selection @@ -1131,32 +1121,31 @@ struct IMGUI_API ImGuiInputTextState int ReloadSelectionStart; // POSITIONS ARE IN IMWCHAR units *NOT* UTF-8 this is why this is not exposed yet. int ReloadSelectionEnd; - ImGuiInputTextState() { memset(this, 0, sizeof(*this)); } - void ClearText() { CurLenW = CurLenA = 0; TextW[0] = 0; TextA[0] = 0; CursorClamp(); } - void ClearFreeMemory() { TextW.clear(); TextA.clear(); InitialTextA.clear(); } - int GetUndoAvailCount() const { return Stb.undostate.undo_point; } - int GetRedoAvailCount() const { return IMSTB_TEXTEDIT_UNDOSTATECOUNT - Stb.undostate.redo_point; } + ImGuiInputTextState(); + ~ImGuiInputTextState(); + void ClearText() { CurLenA = 0; TextA[0] = 0; CursorClamp(); } + void ClearFreeMemory() { TextA.clear(); InitialTextA.clear(); } void OnKeyPressed(int key); // Cannot be inline because we call in code in stb_textedit.h implementation + void OnCharPressed(unsigned int c); // Cursor & Selection - void CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking - void CursorClamp() { Stb.cursor = ImMin(Stb.cursor, CurLenW); Stb.select_start = ImMin(Stb.select_start, CurLenW); Stb.select_end = ImMin(Stb.select_end, CurLenW); } - bool HasSelection() const { return Stb.select_start != Stb.select_end; } - void ClearSelection() { Stb.select_start = Stb.select_end = Stb.cursor; } - int GetCursorPos() const { return Stb.cursor; } - int GetSelectionStart() const { return Stb.select_start; } - int GetSelectionEnd() const { return Stb.select_end; } - void SelectAll() { Stb.select_start = 0; Stb.cursor = Stb.select_end = CurLenW; Stb.has_preferred_x = 0; } + void CursorAnimReset(); + void CursorClamp(); + bool HasSelection() const; + void ClearSelection(); + int GetCursorPos() const; + int GetSelectionStart() const; + int GetSelectionEnd() const; + void SelectAll(); // Reload user buf (WIP #2890) // If you modify underlying user-passed const char* while active you need to call this (InputText V2 may lift this) // strcpy(my_buf, "hello"); // if (ImGuiInputTextState* state = ImGui::GetInputTextState(id)) // id may be ImGui::GetItemID() is last item // state->ReloadUserBufAndSelectAll(); - void ReloadUserBufAndSelectAll() { ReloadUserBuf = true; ReloadSelectionStart = 0; ReloadSelectionEnd = INT_MAX; } - void ReloadUserBufAndKeepSelection() { ReloadUserBuf = true; ReloadSelectionStart = Stb.select_start; ReloadSelectionEnd = Stb.select_end; } - void ReloadUserBufAndMoveToEnd() { ReloadUserBuf = true; ReloadSelectionStart = ReloadSelectionEnd = INT_MAX; } - + void ReloadUserBufAndSelectAll(); + void ReloadUserBufAndKeepSelection(); + void ReloadUserBufAndMoveToEnd(); }; enum ImGuiWindowRefreshFlags_ @@ -1220,7 +1209,7 @@ enum ImGuiNextItemDataFlags_ struct ImGuiNextItemData { - ImGuiNextItemDataFlags Flags; + ImGuiNextItemDataFlags HasFlags; // Called HasFlags instead of Flags to avoid mistaking this ImGuiItemFlags ItemFlags; // Currently only tested/used for ImGuiItemFlags_AllowOverlap and ImGuiItemFlags_HasSelectionUserData. // Non-flags members are NOT cleared by ItemAdd() meaning they are still valid during NavProcessItem() ImGuiID FocusScopeId; // Set by SetNextItemSelectionUserData() @@ -1234,14 +1223,14 @@ struct ImGuiNextItemData ImGuiID StorageId; // Set by SetNextItemStorageID() ImGuiNextItemData() { memset(this, 0, sizeof(*this)); SelectionUserData = -1; } - inline void ClearFlags() { Flags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! + inline void ClearFlags() { HasFlags = ImGuiNextItemDataFlags_None; ItemFlags = ImGuiItemFlags_None; } // Also cleared manually by ItemAdd()! }; // Status storage for the last submitted item struct ImGuiLastItemData { ImGuiID ID; - ImGuiItemFlags InFlags; // See ImGuiItemFlags_ + ImGuiItemFlags ItemFlags; // See ImGuiItemFlags_ ImGuiItemStatusFlags StatusFlags; // See ImGuiItemStatusFlags_ ImRect Rect; // Full rectangle ImRect NavRect; // Navigation scoring rectangle (not displayed) @@ -1261,13 +1250,16 @@ struct ImGuiTreeNodeStackData { ImGuiID ID; ImGuiTreeNodeFlags TreeFlags; - ImGuiItemFlags InFlags; // Used for nav landing + ImGuiItemFlags ItemFlags; // Used for nav landing ImRect NavRect; // Used for nav landing }; -struct IMGUI_API ImGuiStackSizes +// sizeof() = 20 +struct IMGUI_API ImGuiErrorRecoveryState { + short SizeOfWindowStack; short SizeOfIDStack; + short SizeOfTreeStack; short SizeOfColorStack; short SizeOfStyleVarStack; short SizeOfFontStack; @@ -1277,18 +1269,16 @@ struct IMGUI_API ImGuiStackSizes short SizeOfBeginPopupStack; short SizeOfDisabledStack; - ImGuiStackSizes() { memset(this, 0, sizeof(*this)); } - void SetToContextState(ImGuiContext* ctx); - void CompareWithContextState(ImGuiContext* ctx); + ImGuiErrorRecoveryState() { memset(this, 0, sizeof(*this)); } }; // Data saved for each window pushed into the stack struct ImGuiWindowStackData { - ImGuiWindow* Window; - ImGuiLastItemData ParentLastItemDataBackup; - ImGuiStackSizes StackSizesOnBegin; // Store size of various stacks for asserting - bool DisabledOverrideReenable; // Non-child window override disabled flag + ImGuiWindow* Window; + ImGuiLastItemData ParentLastItemDataBackup; + ImGuiErrorRecoveryState StackSizesInBegin; // Store size of various stacks for asserting + bool DisabledOverrideReenable; // Non-child window override disabled flag }; struct ImGuiShrinkWidthItem @@ -1556,12 +1546,18 @@ enum ImGuiScrollFlags_ ImGuiScrollFlags_MaskY_ = ImGuiScrollFlags_KeepVisibleEdgeY | ImGuiScrollFlags_KeepVisibleCenterY | ImGuiScrollFlags_AlwaysCenterY, }; -enum ImGuiNavHighlightFlags_ +enum ImGuiNavRenderCursorFlags_ { - ImGuiNavHighlightFlags_None = 0, - ImGuiNavHighlightFlags_Compact = 1 << 1, // Compact highlight, no padding - ImGuiNavHighlightFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) _even_ when using the mouse. - ImGuiNavHighlightFlags_NoRounding = 1 << 3, + ImGuiNavRenderCursorFlags_None = 0, + ImGuiNavRenderCursorFlags_Compact = 1 << 1, // Compact highlight, no padding/distance from focused item + ImGuiNavRenderCursorFlags_AlwaysDraw = 1 << 2, // Draw rectangular highlight if (g.NavId == id) even when g.NavCursorVisible == false, aka even when using the mouse. + ImGuiNavRenderCursorFlags_NoRounding = 1 << 3, +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + ImGuiNavHighlightFlags_None = ImGuiNavRenderCursorFlags_None, // Renamed in 1.91.4 + ImGuiNavHighlightFlags_Compact = ImGuiNavRenderCursorFlags_Compact, // Renamed in 1.91.4 + ImGuiNavHighlightFlags_AlwaysDraw = ImGuiNavRenderCursorFlags_AlwaysDraw, // Renamed in 1.91.4 + ImGuiNavHighlightFlags_NoRounding = ImGuiNavRenderCursorFlags_NoRounding, // Renamed in 1.91.4 +#endif }; enum ImGuiNavMoveFlags_ @@ -1582,7 +1578,7 @@ enum ImGuiNavMoveFlags_ ImGuiNavMoveFlags_IsPageMove = 1 << 11, // Identify a PageDown/PageUp request. ImGuiNavMoveFlags_Activate = 1 << 12, // Activate/select target item. ImGuiNavMoveFlags_NoSelect = 1 << 13, // Don't trigger selection by not setting g.NavJustMovedTo - ImGuiNavMoveFlags_NoSetNavHighlight = 1 << 14, // Do not alter the visible state of keyboard vs mouse nav highlight + ImGuiNavMoveFlags_NoSetNavCursorVisible = 1 << 14, // Do not alter the nav cursor visible state ImGuiNavMoveFlags_NoClearActiveId = 1 << 15, // (Experimental) Do not clear active id when applying move result }; @@ -1600,17 +1596,17 @@ struct ImGuiNavItemData ImGuiID ID; // Init,Move // Best candidate item ID ImGuiID FocusScopeId; // Init,Move // Best candidate focus scope ID ImRect RectRel; // Init,Move // Best candidate bounding box in window relative space - ImGuiItemFlags InFlags; // ????,Move // Best candidate item flags + ImGuiItemFlags ItemFlags; // ????,Move // Best candidate item flags float DistBox; // Move // Best candidate box distance to current NavId float DistCenter; // Move // Best candidate center distance to current NavId float DistAxial; // Move // Best candidate axial distance to current NavId - ImGuiSelectionUserData SelectionUserData;//I+Mov // Best candidate SetNextItemSelectionUserData() value. Valid if (InFlags & ImGuiItemFlags_HasSelectionUserData) + ImGuiSelectionUserData SelectionUserData;//I+Mov // Best candidate SetNextItemSelectionUserData() value. Valid if (ItemFlags & ImGuiItemFlags_HasSelectionUserData) ImGuiNavItemData() { Clear(); } - void Clear() { Window = NULL; ID = FocusScopeId = 0; InFlags = 0; SelectionUserData = -1; DistBox = DistCenter = DistAxial = FLT_MAX; } + void Clear() { Window = NULL; ID = FocusScopeId = 0; ItemFlags = 0; SelectionUserData = -1; DistBox = DistCenter = DistAxial = FLT_MAX; } }; -// Storage for PushFocusScope() +// Storage for PushFocusScope(), g.FocusScopeStack[], g.NavFocusRoute[] struct ImGuiFocusScopeData { ImGuiID ID; @@ -1805,23 +1801,28 @@ struct ImGuiViewportP : public ImGuiViewport ImDrawList* BgFgDrawLists[2]; // Convenience background (0) and foreground (1) draw lists. We use them to draw software mouser cursor when io.MouseDrawCursor is set and to draw most debug overlays. ImDrawData DrawDataP; ImDrawDataBuilder DrawDataBuilder; // Temporary data while building final ImDrawData - ImVec2 WorkOffsetMin; // Work Area: Offset from Pos to top-left corner of Work Area. Generally (0,0) or (0,+main_menu_bar_height). Work Area is Full Area but without menu-bars/status-bars (so WorkArea always fit inside Pos/Size!) - ImVec2 WorkOffsetMax; // Work Area: Offset from Pos+Size to bottom-right corner of Work Area. Generally (0,0) or (0,-status_bar_height). - ImVec2 BuildWorkOffsetMin; // Work Area: Offset being built during current frame. Generally >= 0.0f. - ImVec2 BuildWorkOffsetMax; // Work Area: Offset being built during current frame. Generally <= 0.0f. + + // Per-viewport work area + // - Insets are >= 0.0f values, distance from viewport corners to work area. + // - BeginMainMenuBar() and DockspaceOverViewport() tend to use work area to avoid stepping over existing contents. + // - Generally 'safeAreaInsets' in iOS land, 'DisplayCutout' in Android land. + ImVec2 WorkInsetMin; // Work Area inset locked for the frame. GetWorkRect() always fits within GetMainRect(). + ImVec2 WorkInsetMax; // " + ImVec2 BuildWorkInsetMin; // Work Area inset accumulator for current frame, to become next frame's WorkInset + ImVec2 BuildWorkInsetMax; // " ImGuiViewportP() { BgFgDrawListsLastFrame[0] = BgFgDrawListsLastFrame[1] = -1; BgFgDrawLists[0] = BgFgDrawLists[1] = NULL; } ~ImGuiViewportP() { if (BgFgDrawLists[0]) IM_DELETE(BgFgDrawLists[0]); if (BgFgDrawLists[1]) IM_DELETE(BgFgDrawLists[1]); } // Calculate work rect pos/size given a set of offset (we have 1 pair of offset for rect locked from last frame data, and 1 pair for currently building rect) - ImVec2 CalcWorkRectPos(const ImVec2& off_min) const { return ImVec2(Pos.x + off_min.x, Pos.y + off_min.y); } - ImVec2 CalcWorkRectSize(const ImVec2& off_min, const ImVec2& off_max) const { return ImVec2(ImMax(0.0f, Size.x - off_min.x + off_max.x), ImMax(0.0f, Size.y - off_min.y + off_max.y)); } - void UpdateWorkRect() { WorkPos = CalcWorkRectPos(WorkOffsetMin); WorkSize = CalcWorkRectSize(WorkOffsetMin, WorkOffsetMax); } // Update public fields + ImVec2 CalcWorkRectPos(const ImVec2& inset_min) const { return ImVec2(Pos.x + inset_min.x, Pos.y + inset_min.y); } + ImVec2 CalcWorkRectSize(const ImVec2& inset_min, const ImVec2& inset_max) const { return ImVec2(ImMax(0.0f, Size.x - inset_min.x - inset_max.x), ImMax(0.0f, Size.y - inset_min.y - inset_max.y)); } + void UpdateWorkRect() { WorkPos = CalcWorkRectPos(WorkInsetMin); WorkSize = CalcWorkRectSize(WorkInsetMin, WorkInsetMax); } // Update public fields // Helpers to retrieve ImRect (we don't need to store BuildWorkRect as every access tend to change it, hence the code asymmetry) ImRect GetMainRect() const { return ImRect(Pos.x, Pos.y, Pos.x + Size.x, Pos.y + Size.y); } ImRect GetWorkRect() const { return ImRect(WorkPos.x, WorkPos.y, WorkPos.x + WorkSize.x, WorkPos.y + WorkSize.y); } - ImRect GetBuildWorkRect() const { ImVec2 pos = CalcWorkRectPos(BuildWorkOffsetMin); ImVec2 size = CalcWorkRectSize(BuildWorkOffsetMin, BuildWorkOffsetMax); return ImRect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); } + ImRect GetBuildWorkRect() const { ImVec2 pos = CalcWorkRectPos(BuildWorkInsetMin); ImVec2 size = CalcWorkRectSize(BuildWorkInsetMin, BuildWorkInsetMax); return ImRect(pos.x, pos.y, pos.x + size.x, pos.y + size.y); } }; //----------------------------------------------------------------------------- @@ -1875,6 +1876,7 @@ enum ImGuiLocKey : int ImGuiLocKey_WindowingMainMenuBar, ImGuiLocKey_WindowingPopup, ImGuiLocKey_WindowingUntitled, + ImGuiLocKey_OpenLink_s, ImGuiLocKey_CopyLink, ImGuiLocKey_COUNT }; @@ -1885,6 +1887,21 @@ struct ImGuiLocEntry const char* Text; }; +//----------------------------------------------------------------------------- +// [SECTION] Error handling, State recovery support +//----------------------------------------------------------------------------- + +// Macros used by Recoverable Error handling +// - Only dispatch error if _EXPR: evaluate as assert (similar to an assert macro). +// - The message will always be a string literal, in order to increase likelihood of being display by an assert handler. +// - See 'Demo->Configuration->Error Handling' and ImGuiIO definitions for details on error handling. +// - Read https://github.com/ocornut/imgui/wiki/Error-Handling for details on error handling. +#ifndef IM_ASSERT_USER_ERROR +#define IM_ASSERT_USER_ERROR(_EXPR,_MSG) do { if (!(_EXPR) && ImGui::ErrorLog(_MSG)) { IM_ASSERT((_EXPR) && _MSG); } } while (0) // Recoverable User Error +#endif + +// The error callback is currently not public, as it is expected that only advanced users will rely on it. +typedef void (*ImGuiErrorCallback)(ImGuiContext* ctx, void* user_data, const char* msg); // Function signature for g.ErrorCallback //----------------------------------------------------------------------------- // [SECTION] Metrics, Debug Tools @@ -1894,16 +1911,19 @@ enum ImGuiDebugLogFlags_ { // Event types ImGuiDebugLogFlags_None = 0, - ImGuiDebugLogFlags_EventActiveId = 1 << 0, - ImGuiDebugLogFlags_EventFocus = 1 << 1, - ImGuiDebugLogFlags_EventPopup = 1 << 2, - ImGuiDebugLogFlags_EventNav = 1 << 3, - ImGuiDebugLogFlags_EventClipper = 1 << 4, - ImGuiDebugLogFlags_EventSelection = 1 << 5, - ImGuiDebugLogFlags_EventIO = 1 << 6, - ImGuiDebugLogFlags_EventInputRouting = 1 << 7, - - ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO | ImGuiDebugLogFlags_EventInputRouting, + ImGuiDebugLogFlags_EventError = 1 << 0, // Error submitted by IM_ASSERT_USER_ERROR() + ImGuiDebugLogFlags_EventActiveId = 1 << 1, + ImGuiDebugLogFlags_EventFocus = 1 << 2, + ImGuiDebugLogFlags_EventPopup = 1 << 3, + ImGuiDebugLogFlags_EventNav = 1 << 4, + ImGuiDebugLogFlags_EventClipper = 1 << 5, + ImGuiDebugLogFlags_EventSelection = 1 << 6, + ImGuiDebugLogFlags_EventIO = 1 << 7, + ImGuiDebugLogFlags_EventInputRouting = 1 << 8, + ImGuiDebugLogFlags_EventDocking = 1 << 9, // Unused in this branch + ImGuiDebugLogFlags_EventViewport = 1 << 10, // Unused in this branch + + ImGuiDebugLogFlags_EventMask_ = ImGuiDebugLogFlags_EventError | ImGuiDebugLogFlags_EventActiveId | ImGuiDebugLogFlags_EventFocus | ImGuiDebugLogFlags_EventPopup | ImGuiDebugLogFlags_EventNav | ImGuiDebugLogFlags_EventClipper | ImGuiDebugLogFlags_EventSelection | ImGuiDebugLogFlags_EventIO | ImGuiDebugLogFlags_EventInputRouting | ImGuiDebugLogFlags_EventDocking | ImGuiDebugLogFlags_EventViewport, ImGuiDebugLogFlags_OutputToTTY = 1 << 20, // Also send output to TTY ImGuiDebugLogFlags_OutputToTestEngine = 1 << 21, // Also send output to Test Engine }; @@ -1993,6 +2013,7 @@ struct ImGuiContext bool Initialized; bool FontAtlasOwnedByContext; // IO.Fonts-> is owned by the ImGuiContext and will be destructed along with it. ImGuiIO IO; + ImGuiPlatformIO PlatformIO; ImGuiStyle Style; ImFont* Font; // (Shortcut) == FontStack.empty() ? IO.Font : FontStack.back() float FontSize; // (Shortcut) == FontBaseSize * g.CurrentWindow->FontWindowScale == window->FontSize(). Text height for current window. @@ -2041,9 +2062,11 @@ struct ImGuiContext ImVec2 WheelingAxisAvg; // Item/widgets state and tracking information + ImGuiID DebugDrawIdConflicts; // Set when we detect multiple items with the same identifier ImGuiID DebugHookIdInfo; // Will call core hooks: DebugHookIdInfo() from GetID functions, used by ID Stack Tool [next HoveredId/ActiveId to not pull in an extra cache-line] ImGuiID HoveredId; // Hovered widget, filled during the frame ImGuiID HoveredIdPreviousFrame; + int HoveredIdPreviousFrameItemCount; // Count numbers of items using the same ID as last frame's hovered id float HoveredIdTimer; // Measure contiguous hovering time float HoveredIdNotActiveTimer; // Measure contiguous hovering time where the item has not been active bool HoveredIdAllowOverlap; @@ -2109,9 +2132,15 @@ struct ImGuiContext // Viewports ImVector Viewports; // Active viewports (Size==1 in 'master' branch). Each viewports hold their copy of ImDrawData. - // Gamepad/keyboard Navigation - ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' + // Keyboard/Gamepad Navigation + bool NavCursorVisible; // Nav focus cursor/rectangle is visible? We hide it after a mouse click. We show it after a nav move. + bool NavHighlightItemUnderNav; // Disable mouse hovering highlight. Highlight navigation focused item instead of mouse hovered item. + //bool NavDisableHighlight; // Old name for !g.NavCursorVisible before 1.91.4 (2024/10/18). OPPOSITE VALUE (g.NavDisableHighlight == !g.NavCursorVisible) + //bool NavDisableMouseHover; // Old name for g.NavHighlightItemUnderNav before 1.91.1 (2024/10/18) this was called When user starts using keyboard/gamepad, we hide mouse hovering highlight until mouse is touched again. + bool NavMousePosDirty; // When set we will update mouse position if io.ConfigNavMoveSetMousePos is set (not enabled by default) + bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid ImGuiID NavId; // Focused item for navigation + ImGuiWindow* NavWindow; // Focused window for navigation. Could be called 'FocusedWindow' ImGuiID NavFocusScopeId; // Focused focus scope (e.g. selection code often wants to "clear other items" when landing on an item of the same scope) ImGuiNavLayer NavLayer; // Focused layer (main scrolling layer, or menu/title bar layer) ImGuiID NavActivateId; // ~~ (g.ActiveId == 0) && (IsKeyPressed(ImGuiKey_Space) || IsKeyDown(ImGuiKey_Enter) || IsKeyPressed(ImGuiKey_NavGamepadActivate)) ? NavId : 0, also set when calling ActivateItem() @@ -2125,10 +2154,7 @@ struct ImGuiContext ImGuiActivateFlags NavNextActivateFlags; ImGuiInputSource NavInputSource; // Keyboard or Gamepad mode? THIS CAN ONLY BE ImGuiInputSource_Keyboard or ImGuiInputSource_Mouse ImGuiSelectionUserData NavLastValidSelectionUserData; // Last valid data passed to SetNextItemSelectionUser(), or -1. For current window. Not reset when focusing an item that doesn't have selection data. - bool NavIdIsAlive; // Nav widget has been seen this frame ~~ NavRectRel is valid - bool NavMousePosDirty; // When set we will update mouse position if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) if set (NB: this not enabled by default) - bool NavDisableHighlight; // When user starts using mouse, we hide gamepad/keyboard highlight (NB: but they are still available, which is why NavDisableHighlight isn't always != NavDisableMouseHover) - bool NavDisableMouseHover; // When user starts using gamepad/keyboard, we hide mouse hovering highlight until mouse is touched again. + ImS8 NavCursorHideFrames; // Navigation: Init & Move Requests bool NavAnyRequest; // ~~ NavMoveRequest || NavInitRequest this is to perform early out in ItemAdd() @@ -2160,7 +2186,7 @@ struct ImGuiContext ImGuiID NavJustMovedToFocusScopeId; // Just navigated to this focus scope id (result of a successfully MoveRequest). ImGuiKeyChord NavJustMovedToKeyMods; bool NavJustMovedToIsTabbing; // Copy of ImGuiNavMoveFlags_IsTabbing. Maybe we should store whole flags. - bool NavJustMovedToHasSelectionData; // Copy of move result's InFlags & ImGuiItemFlags_HasSelectionUserData). Maybe we should just store ImGuiNavItemData. + bool NavJustMovedToHasSelectionData; // Copy of move result's ItemFlags & ImGuiItemFlags_HasSelectionUserData). Maybe we should just store ImGuiNavItemData. // Navigation: Windowing (CTRL+TAB for list, or Menu button + keys or directional pads to move/resize) ImGuiKeyChord ConfigNavWindowingKeyNext; // = ImGuiMod_Ctrl | ImGuiKey_Tab (or ImGuiMod_Super | ImGuiKey_Tab on OS X). For reconfiguration (see #4828) @@ -2255,8 +2281,8 @@ struct ImGuiContext ImGuiComboPreviewData ComboPreviewData; ImRect WindowResizeBorderExpectedRect; // Expected border rect, switch to relative edit if moving bool WindowResizeRelativeMode; - short ScrollbarSeekMode; // 0: relative, -1/+1: prev/next page. - float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? + short ScrollbarSeekMode; // 0: scroll to clicked location, -1/+1: prev/next page. + float ScrollbarClickDeltaToGrabCenter; // When scrolling to mouse location: distance between mouse and center of grab box, normalized in parent space. float SliderGrabClickOffset; float SliderCurrentAccum; // Accumulated slider delta when using navigation controls. bool SliderCurrentAccumDirty; // Has the accumulated slider delta changed since last time we tried to apply it? @@ -2265,15 +2291,15 @@ struct ImGuiContext float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float DisabledAlphaBackup; // Backup for style.Alpha for BeginDisabled() short DisabledStackSize; - short LockMarkEdited; short TooltipOverrideCount; + ImGuiWindow* TooltipPreviousWindow; // Window of last tooltip submitted during the frame ImVector ClipboardHandlerData; // If no custom clipboard handler is defined ImVector MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once ImGuiTypingSelectState TypingSelectState; // State for GetTypingSelectRequest() // Platform support ImGuiPlatformImeData PlatformImeData; // Data updated by current frame - ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data. When changed we call the io.PlatformSetImeDataFn() handler. + ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data. When changed we call the platform_io.Platform_SetImeDataFn() handler. // Settings bool SettingsLoaded; @@ -2301,11 +2327,22 @@ struct ImGuiContext int LogDepthToExpand; int LogDepthToExpandDefault; // Default/stored value for LogDepthMaxExpand if not specified in the LogXXX function call. + // Error Handling + ImGuiErrorCallback ErrorCallback; // = NULL. May be exposed in public API eventually. + void* ErrorCallbackUserData; // = NULL + ImVec2 ErrorTooltipLockedPos; + bool ErrorFirst; + int ErrorCountCurrentFrame; // [Internal] Number of errors submitted this frame. + ImGuiErrorRecoveryState StackSizesInNewFrame; // [Internal] + ImGuiErrorRecoveryState*StackSizesInBeginForCurrentWindow; // [Internal] + // Debug Tools // (some of the highly frequently used data are interleaved in other structures above: DebugBreakXXX fields, DebugHookIdInfo, DebugLocateId etc.) + int DebugDrawIdConflictsCount; // Locked count (preserved when holding CTRL) ImGuiDebugLogFlags DebugLogFlags; ImGuiTextBuffer DebugLogBuf; ImGuiTextIndex DebugLogIndex; + int DebugLogSkippedErrors; ImGuiDebugLogFlags DebugLogAutoDisableFlags; ImU8 DebugLogAutoDisableFrames; ImU8 DebugLocateFrames; // For DebugLocateItemOnHover(). This is used together with DebugLocateId which is in a hot/cached spot above. @@ -2332,208 +2369,7 @@ struct ImGuiContext ImVector TempBuffer; // Temporary text buffer char TempKeychordName[64]; - ImGuiContext(ImFontAtlas* shared_font_atlas) - { - IO.Ctx = this; - InputTextState.Ctx = this; - - Initialized = false; - FontAtlasOwnedByContext = shared_font_atlas ? false : true; - Font = NULL; - FontSize = FontBaseSize = FontScale = CurrentDpiScale = 0.0f; - IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); - Time = 0.0f; - FrameCount = 0; - FrameCountEnded = FrameCountRendered = -1; - WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; - GcCompactAll = false; - TestEngineHookItems = false; - TestEngine = NULL; - memset(ContextName, 0, sizeof(ContextName)); - - InputEventsNextMouseSource = ImGuiMouseSource_Mouse; - InputEventsNextEventId = 1; - - WindowsActiveCount = 0; - CurrentWindow = NULL; - HoveredWindow = NULL; - HoveredWindowUnderMovingWindow = NULL; - HoveredWindowBeforeClear = NULL; - MovingWindow = NULL; - WheelingWindow = NULL; - WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1; - WheelingWindowReleaseTimer = 0.0f; - - DebugHookIdInfo = 0; - HoveredId = HoveredIdPreviousFrame = 0; - HoveredIdAllowOverlap = false; - HoveredIdIsDisabled = false; - HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; - ItemUnclipByLog = false; - ActiveId = 0; - ActiveIdIsAlive = 0; - ActiveIdTimer = 0.0f; - ActiveIdIsJustActivated = false; - ActiveIdAllowOverlap = false; - ActiveIdNoClearOnFocusLoss = false; - ActiveIdHasBeenPressedBefore = false; - ActiveIdHasBeenEditedBefore = false; - ActiveIdHasBeenEditedThisFrame = false; - ActiveIdFromShortcut = false; - ActiveIdClickOffset = ImVec2(-1, -1); - ActiveIdWindow = NULL; - ActiveIdSource = ImGuiInputSource_None; - ActiveIdMouseButton = -1; - ActiveIdPreviousFrame = 0; - ActiveIdPreviousFrameIsAlive = false; - ActiveIdPreviousFrameHasBeenEditedBefore = false; - ActiveIdPreviousFrameWindow = NULL; - LastActiveId = 0; - LastActiveIdTimer = 0.0f; - - LastKeyboardKeyPressTime = LastKeyModsChangeTime = LastKeyModsChangeFromNoneTime = -1.0; - - ActiveIdUsingNavDirMask = 0x00; - ActiveIdUsingAllKeyboardKeys = false; - - CurrentFocusScopeId = 0; - CurrentItemFlags = ImGuiItemFlags_None; - DebugShowGroupRects = false; - - NavWindow = NULL; - NavId = NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = 0; - NavLayer = ImGuiNavLayer_Main; - NavNextActivateId = 0; - NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; - NavHighlightActivatedId = 0; - NavHighlightActivatedTimer = 0.0f; - NavInputSource = ImGuiInputSource_Keyboard; - NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; - NavIdIsAlive = false; - NavMousePosDirty = false; - NavDisableHighlight = true; - NavDisableMouseHover = false; - - NavAnyRequest = false; - NavInitRequest = false; - NavInitRequestFromMove = false; - NavMoveSubmitted = false; - NavMoveScoringItems = false; - NavMoveForwardToNextFrame = false; - NavMoveFlags = ImGuiNavMoveFlags_None; - NavMoveScrollFlags = ImGuiScrollFlags_None; - NavMoveKeyMods = ImGuiMod_None; - NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None; - NavScoringDebugCount = 0; - NavTabbingDir = 0; - NavTabbingCounter = 0; - - NavJustMovedFromFocusScopeId = NavJustMovedToId = NavJustMovedToFocusScopeId = 0; - NavJustMovedToKeyMods = ImGuiMod_None; - NavJustMovedToIsTabbing = false; - NavJustMovedToHasSelectionData = false; - - // All platforms use Ctrl+Tab but Ctrl<>Super are swapped on Mac... - // FIXME: Because this value is stored, it annoyingly interfere with toggling io.ConfigMacOSXBehaviors updating this.. - ConfigNavWindowingKeyNext = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiKey_Tab); - ConfigNavWindowingKeyPrev = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiMod_Shift | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab); - NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; - NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; - NavWindowingToggleLayer = false; - NavWindowingToggleKey = ImGuiKey_None; - - DimBgRatio = 0.0f; - - DragDropActive = DragDropWithinSource = DragDropWithinTarget = false; - DragDropSourceFlags = ImGuiDragDropFlags_None; - DragDropSourceFrameCount = -1; - DragDropMouseButton = -1; - DragDropTargetId = 0; - DragDropAcceptFlags = ImGuiDragDropFlags_None; - DragDropAcceptIdCurrRectSurface = 0.0f; - DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; - DragDropAcceptFrameCount = -1; - DragDropHoldJustPressedId = 0; - memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); - - ClipperTempDataStacked = 0; - - CurrentTable = NULL; - TablesTempDataStacked = 0; - CurrentTabBar = NULL; - CurrentMultiSelect = NULL; - MultiSelectTempDataStacked = 0; - - HoverItemDelayId = HoverItemDelayIdPreviousFrame = HoverItemUnlockedStationaryId = HoverWindowUnlockedStationaryId = 0; - HoverItemDelayTimer = HoverItemDelayClearTimer = 0.0f; - - MouseCursor = ImGuiMouseCursor_Arrow; - MouseStationaryTimer = 0.0f; - - TempInputId = 0; - memset(&DataTypeZeroValue, 0, sizeof(DataTypeZeroValue)); - BeginMenuDepth = BeginComboDepth = 0; - ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; - ColorEditCurrentID = ColorEditSavedID = 0; - ColorEditSavedHue = ColorEditSavedSat = 0.0f; - ColorEditSavedColor = 0; - WindowResizeRelativeMode = false; - ScrollbarSeekMode = 0; - ScrollbarClickDeltaToGrabCenter = 0.0f; - SliderGrabClickOffset = 0.0f; - SliderCurrentAccum = 0.0f; - SliderCurrentAccumDirty = false; - DragCurrentAccumDirty = false; - DragCurrentAccum = 0.0f; - DragSpeedDefaultRatio = 1.0f / 100.0f; - DisabledAlphaBackup = 0.0f; - DisabledStackSize = 0; - LockMarkEdited = 0; - TooltipOverrideCount = 0; - - PlatformImeData.InputPos = ImVec2(0.0f, 0.0f); - PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission - - SettingsLoaded = false; - SettingsDirtyTimer = 0.0f; - HookIdNext = 0; - - memset(LocalizationTable, 0, sizeof(LocalizationTable)); - - LogEnabled = false; - LogType = ImGuiLogType_None; - LogNextPrefix = LogNextSuffix = NULL; - LogFile = NULL; - LogLinePosY = FLT_MAX; - LogLineFirstItem = false; - LogDepthRef = 0; - LogDepthToExpand = LogDepthToExpandDefault = 2; - - DebugLogFlags = ImGuiDebugLogFlags_OutputToTTY; - DebugLocateId = 0; - DebugLogAutoDisableFlags = ImGuiDebugLogFlags_None; - DebugLogAutoDisableFrames = 0; - DebugLocateFrames = 0; - DebugBeginReturnValueCullDepth = -1; - DebugItemPickerActive = false; - DebugItemPickerMouseButton = ImGuiMouseButton_Left; - DebugItemPickerBreakId = 0; - DebugFlashStyleColorTime = 0.0f; - DebugFlashStyleColorIdx = ImGuiCol_COUNT; - - // Same as DebugBreakClearData(). Those fields are scattered in their respective subsystem to stay in hot-data locations - DebugBreakInWindow = 0; - DebugBreakInTable = 0; - DebugBreakInLocateId = false; - DebugBreakKeyChord = ImGuiKey_Pause; - DebugBreakInShortcutRouting = ImGuiKey_None; - - memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); - FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; - FramerateSecPerFrameAccum = 0.0f; - WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; - memset(TempKeychordName, 0, sizeof(TempKeychordName)); - } + ImGuiContext(ImFontAtlas* shared_font_atlas); }; //----------------------------------------------------------------------------- @@ -2610,7 +2446,7 @@ struct IMGUI_API ImGuiWindow ImVec2 WindowPadding; // Window padding at the time of Begin(). float WindowRounding; // Window rounding at the time of Begin(). May be clamped lower to avoid rendering artifacts with title bar, menu bar etc. float WindowBorderSize; // Window border size at the time of Begin(). - float TitleBarHeight, MenuBarHeight; + float TitleBarHeight, MenuBarHeight; // Note that those used to be function before 2024/05/28. If you have old code calling TitleBarHeight() you can change it to TitleBarHeight. float DecoOuterSizeX1, DecoOuterSizeY1; // Left/Up offsets. Sum of non-scrolling outer decorations (X1 generally == 0.0f. Y1 generally = TitleBarHeight + MenuBarHeight). Locked during Begin(). float DecoOuterSizeX2, DecoOuterSizeY2; // Right/Down offsets (X2 generally == ScrollbarSize.x, Y2 == ScrollbarSizes.y). float DecoInnerSizeX1, DecoInnerSizeY1; // Applied AFTER/OVER InnerRect. Specialized for Tables as they use specialized form of clipping and frozen rows/columns are inside InnerRect (and not part of regular decoration sizes). @@ -2707,6 +2543,7 @@ struct IMGUI_API ImGuiWindow ImGuiID GetID(const char* str, const char* str_end = NULL); ImGuiID GetID(const void* ptr); ImGuiID GetID(int n); + ImGuiID GetIDFromPos(const ImVec2& p_abs); ImGuiID GetIDFromRectangle(const ImRect& r_abs); // We don't use g.FontSize because the window may be != g.CurrentWindow. @@ -2755,9 +2592,10 @@ struct ImGuiTabItem ImGuiTabItem() { memset(this, 0, sizeof(*this)); LastFrameVisible = LastFrameSelected = -1; RequestedWidth = -1.0f; NameOffset = -1; BeginOrder = IndexDuringLayout = -1; } }; -// Storage for a tab bar (sizeof() 152 bytes) +// Storage for a tab bar (sizeof() 160 bytes) struct IMGUI_API ImGuiTabBar { + ImGuiWindow* Window; ImVector Tabs; ImGuiTabBarFlags Flags; ImGuiID ID; // Zero for tab-bars used by docking @@ -2818,6 +2656,7 @@ struct ImGuiTableColumn float MaxX; float WidthRequest; // Master width absolute value when !(Flags & _WidthStretch). When Stretch this is derived every frame from StretchWeight in TableUpdateLayout() float WidthAuto; // Automatic width + float WidthMax; // Maximum width (FIXME: overwritten by each instance) float StretchWeight; // Master width weight when (Flags & _WidthStretch). Often around ~1.0f initially. float InitStretchWeightOrWidth; // Value passed to TableSetupColumn(). For Width it is a content width (_without padding_). ImRect ClipRect; // Clipping rectangle for the column @@ -3116,8 +2955,8 @@ namespace ImGui inline void SetWindowParentWindowForFocusRoute(ImGuiWindow* window, ImGuiWindow* parent_window) { window->ParentWindowForFocusRoute = parent_window; } inline ImRect WindowRectAbsToRel(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x - off.x, r.Min.y - off.y, r.Max.x - off.x, r.Max.y - off.y); } inline ImRect WindowRectRelToAbs(ImGuiWindow* window, const ImRect& r) { ImVec2 off = window->DC.CursorStartPos; return ImRect(r.Min.x + off.x, r.Min.y + off.y, r.Max.x + off.x, r.Max.y + off.y); } - inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } inline ImVec2 WindowPosAbsToRel(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x - off.x, p.y - off.y); } + inline ImVec2 WindowPosRelToAbs(ImGuiWindow* window, const ImVec2& p) { ImVec2 off = window->DC.CursorStartPos; return ImVec2(p.x + off.x, p.y + off.y); } // Windows: Display Order and Focus Order IMGUI_API void FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags = 0); @@ -3194,7 +3033,7 @@ namespace ImGui // Basic Accessors inline ImGuiItemStatusFlags GetItemStatusFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.StatusFlags; } - inline ImGuiItemFlags GetItemFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.InFlags; } + inline ImGuiItemFlags GetItemFlags() { ImGuiContext& g = *GImGui; return g.LastItemData.ItemFlags; } inline ImGuiID GetActiveID() { ImGuiContext& g = *GImGui; return g.ActiveId; } inline ImGuiID GetFocusID() { ImGuiContext& g = *GImGui; return g.NavId; } IMGUI_API void SetActiveID(ImGuiID id, ImGuiWindow* window); @@ -3263,7 +3102,7 @@ namespace ImGui IMGUI_API bool BeginComboPreview(); IMGUI_API void EndComboPreview(); - // Gamepad/Keyboard Navigation + // Keyboard/Gamepad Navigation IMGUI_API void NavInitWindow(ImGuiWindow* window, bool force_reinit); IMGUI_API void NavInitRequestApplyResult(); IMGUI_API bool NavMoveRequestButNoResultYet(); @@ -3276,7 +3115,7 @@ namespace ImGui IMGUI_API void NavMoveRequestTryWrapping(ImGuiWindow* window, ImGuiNavMoveFlags move_flags); IMGUI_API void NavHighlightActivated(ImGuiID id); IMGUI_API void NavClearPreferredPosForAxis(ImGuiAxis axis); - IMGUI_API void NavRestoreHighlightAfterMove(); + IMGUI_API void SetNavCursorVisibleAfterMove(); IMGUI_API void NavUpdateCurrentWindowIsScrollPushableX(); IMGUI_API void SetNavWindow(ImGuiWindow* window); IMGUI_API void SetNavID(ImGuiID id, ImGuiNavLayer nav_layer, ImGuiID focus_scope_id, const ImRect& rect_rel); @@ -3297,7 +3136,7 @@ namespace ImGui inline bool IsGamepadKey(ImGuiKey key) { return key >= ImGuiKey_Gamepad_BEGIN && key < ImGuiKey_Gamepad_END; } inline bool IsMouseKey(ImGuiKey key) { return key >= ImGuiKey_Mouse_BEGIN && key < ImGuiKey_Mouse_END; } inline bool IsAliasKey(ImGuiKey key) { return key >= ImGuiKey_Aliases_BEGIN && key < ImGuiKey_Aliases_END; } - inline bool IsModKey(ImGuiKey key) { return key >= ImGuiKey_LeftCtrl && key <= ImGuiKey_RightSuper; } + inline bool IsLRModKey(ImGuiKey key) { return key >= ImGuiKey_LeftCtrl && key <= ImGuiKey_RightSuper; } ImGuiKeyChord FixupKeyChord(ImGuiKeyChord key_chord); inline ImGuiKey ConvertSingleModFlagToKey(ImGuiKey key) { @@ -3393,6 +3232,8 @@ namespace ImGui IMGUI_API void RenderDragDropTargetRect(const ImRect& bb, const ImRect& item_clip_rect); // Typing-Select API + // (provide Windows Explorer style "select items by typing partial name" + "cycle through items by typing same letter" feature) + // (this is currently not documented nor used by main library, but should work. See "widgets_typingselect" in imgui_test_suite for usage code. Please let us know if you use this!) IMGUI_API ImGuiTypingSelectRequest* GetTypingSelectRequest(ImGuiTypingSelectFlags flags = ImGuiTypingSelectFlags_None); IMGUI_API int TypingSelectFindMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx); IMGUI_API int TypingSelectFindNextSingleCharMatch(ImGuiTypingSelectRequest* req, int items_count, const char* (*get_item_name_func)(void*, int), void* user_data, int nav_item_idx); @@ -3461,7 +3302,7 @@ namespace ImGui IMGUI_API ImRect TableGetCellBgRect(const ImGuiTable* table, int column_n); IMGUI_API const char* TableGetColumnName(const ImGuiTable* table, int column_n); IMGUI_API ImGuiID TableGetColumnResizeID(ImGuiTable* table, int column_n, int instance_no = 0); - IMGUI_API float TableGetMaxColumnWidth(const ImGuiTable* table, int column_n); + IMGUI_API float TableCalcMaxColumnWidth(const ImGuiTable* table, int column_n); IMGUI_API void TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n); IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table); IMGUI_API void TableRemove(ImGuiTable* table); @@ -3489,6 +3330,7 @@ namespace ImGui IMGUI_API void TabBarRemoveTab(ImGuiTabBar* tab_bar, ImGuiID tab_id); IMGUI_API void TabBarCloseTab(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); IMGUI_API void TabBarQueueFocus(ImGuiTabBar* tab_bar, ImGuiTabItem* tab); + IMGUI_API void TabBarQueueFocus(ImGuiTabBar* tab_bar, const char* tab_name); IMGUI_API void TabBarQueueReorder(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, int offset); IMGUI_API void TabBarQueueReorderFromMousePos(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, ImVec2 mouse_pos); IMGUI_API bool TabBarProcessReorder(ImGuiTabBar* tab_bar); @@ -3506,10 +3348,13 @@ namespace ImGui IMGUI_API void RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL); IMGUI_API void RenderTextClippedEx(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, const ImVec2& align = ImVec2(0, 0), const ImRect* clip_rect = NULL); IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known); - IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f); + IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool borders = true, float rounding = 0.0f); IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f); IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, ImDrawFlags flags = 0); - IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_None); // Navigation highlight + IMGUI_API void RenderNavCursor(const ImRect& bb, ImGuiID id, ImGuiNavRenderCursorFlags flags = ImGuiNavRenderCursorFlags_None); // Navigation highlight +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + inline void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavRenderCursorFlags flags = ImGuiNavRenderCursorFlags_None) { RenderNavCursor(bb, id, flags); } // Renamed in 1.91.4 +#endif IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text. IMGUI_API void RenderMouseCursor(ImVec2 pos, float scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow); @@ -3571,6 +3416,7 @@ namespace ImGui IMGUI_API bool DataTypeApplyFromText(const char* buf, ImGuiDataType data_type, void* p_data, const char* format, void* p_data_when_empty = NULL); IMGUI_API int DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2); IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max); + IMGUI_API bool DataTypeIsZero(ImGuiDataType data_type, const void* p_data); // InputText IMGUI_API bool InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); @@ -3599,11 +3445,18 @@ namespace ImGui IMGUI_API void GcCompactTransientWindowBuffers(ImGuiWindow* window); IMGUI_API void GcAwakeTransientWindowBuffers(ImGuiWindow* window); + // Error handling, State Recovery + IMGUI_API bool ErrorLog(const char* msg); + IMGUI_API void ErrorRecoveryStoreState(ImGuiErrorRecoveryState* state_out); + IMGUI_API void ErrorRecoveryTryToRecoverState(const ImGuiErrorRecoveryState* state_in); + IMGUI_API void ErrorRecoveryTryToRecoverWindowState(const ImGuiErrorRecoveryState* state_in); + IMGUI_API void ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); + IMGUI_API void ErrorCheckEndFrameFinalizeErrorTooltip(); + IMGUI_API bool BeginErrorTooltip(); + IMGUI_API void EndErrorTooltip(); + // Debug Tools IMGUI_API void DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr, size_t size); // size >= 0 : alloc, size = -1 : free - IMGUI_API void ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); - IMGUI_API void ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data = NULL); - IMGUI_API void ErrorCheckUsingSetCursorPosToExtendParentBoundaries(); IMGUI_API void DebugDrawCursorPos(ImU32 col = IM_COL32(255, 0, 0, 255)); IMGUI_API void DebugDrawLineExtents(ImU32 col = IM_COL32(255, 0, 0, 255)); IMGUI_API void DebugDrawItemRect(ImU32 col = IM_COL32(255, 0, 0, 255)); @@ -3638,9 +3491,8 @@ namespace ImGui // Obsolete functions #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS - inline void SetItemUsingMouseWheel() { SetItemKeyOwner(ImGuiKey_MouseWheelY); } // Changed in 1.89 - inline bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0) { return TreeNodeUpdateNextOpen(id, flags); } // Renamed in 1.89 - + //inline void SetItemUsingMouseWheel() { SetItemKeyOwner(ImGuiKey_MouseWheelY); } // Changed in 1.89 + //inline bool TreeNodeBehaviorIsOpen(ImGuiID id, ImGuiTreeNodeFlags flags = 0) { return TreeNodeUpdateNextOpen(id, flags); } // Renamed in 1.89 //inline bool IsKeyPressedMap(ImGuiKey key, bool repeat = true) { IM_ASSERT(IsNamedKey(key)); return IsKeyPressed(key, repeat); } // Removed in 1.87: Mapping from named key is always identity! // Refactored focus/nav/tabbing system in 1.82 and 1.84. If you have old/custom copy-and-pasted widgets which used FocusableItemRegister(): @@ -3691,7 +3543,7 @@ extern const char* ImGuiTestEngine_FindItemDebugLabel(ImGuiContext* ctx, ImGuiI // In IMGUI_VERSION_NUM >= 18934: changed IMGUI_TEST_ENGINE_ITEM_ADD(bb,id) to IMGUI_TEST_ENGINE_ITEM_ADD(id,bb,item_data); #define IMGUI_TEST_ENGINE_ITEM_ADD(_ID,_BB,_ITEM_DATA) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemAdd(&g, _ID, _BB, _ITEM_DATA) // Register item bounding box #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) if (g.TestEngineHookItems) ImGuiTestEngineHook_ItemInfo(&g, _ID, _LABEL, _FLAGS) // Register item label and status flags (optional) -#define IMGUI_TEST_ENGINE_LOG(_FMT,...) if (g.TestEngineHookItems) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log +#define IMGUI_TEST_ENGINE_LOG(_FMT,...) ImGuiTestEngineHook_Log(&g, _FMT, __VA_ARGS__) // Custom log entry from user land into test log #else #define IMGUI_TEST_ENGINE_ITEM_ADD(_BB,_ID) ((void)0) #define IMGUI_TEST_ENGINE_ITEM_INFO(_ID,_LABEL,_FLAGS) ((void)g) diff --git a/include/imgui/imstb_textedit.h b/include/imgui/imstb_textedit.h index 783054ab..b7a761c8 100644 --- a/include/imgui/imstb_textedit.h +++ b/include/imgui/imstb_textedit.h @@ -3,6 +3,8 @@ // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) // - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000 + #6783) +// - Added name to struct or it may be forward declared in our code. +// - Added UTF-8 support (see https://github.com/nothings/stb/issues/188 + https://github.com/ocornut/imgui/pull/7925) // Grep for [DEAR IMGUI] to find the changes. // - Also renamed macros used or defined outside of IMSTB_TEXTEDIT_IMPLEMENTATION block from STB_TEXTEDIT_* to IMSTB_TEXTEDIT_* @@ -209,6 +211,7 @@ // int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state) // int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len) // void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key) +// void stb_textedit_text(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int text_len) // // Each of these functions potentially updates the string and updates the // state. @@ -243,7 +246,12 @@ // various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit // set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to -// anything other type you wante before including. +// anything other type you want before including. +// if the STB_TEXTEDIT_KEYTOTEXT function is defined, selected keys are +// transformed into text and stb_textedit_text() is automatically called. +// +// text: [DEAR IMGUI] added 2024-09 +// call this to text inputs sent to the textfield. // // // When rendering, you can read the cursor position and selection state from @@ -318,7 +326,7 @@ typedef struct int undo_char_point, redo_char_point; } StbUndoState; -typedef struct +typedef struct STB_TexteditState { ///////////////////// // @@ -438,13 +446,13 @@ static int stb_text_locate_coord(IMSTB_TEXTEDIT_STRING *str, float x, float y) if (x < r.x1) { // search characters in row for one that straddles 'x' prev_x = r.x0; - for (k=0; k < r.num_chars; ++k) { + for (k=0; k < r.num_chars; k = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k) - i) { float w = STB_TEXTEDIT_GETWIDTH(str, i, k); if (x < prev_x+w) { if (x < prev_x+w/2) return k+i; else - return k+i+1; + return IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, i + k); } prev_x += w; } @@ -563,7 +571,7 @@ static void stb_textedit_find_charpos(StbFindState *find, IMSTB_TEXTEDIT_STRING // now scan to find xpos find->x = r.x0; - for (i=0; first+i < n; ++i) + for (i=0; first+i < n; i = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, first + i) - first) find->x += STB_TEXTEDIT_GETWIDTH(str, first, i); } @@ -640,6 +648,17 @@ static void stb_textedit_move_to_last(IMSTB_TEXTEDIT_STRING *str, STB_TexteditSt } } +// [DEAR IMGUI] +// Functions must be implemented for UTF8 support +// Code in this file that uses those functions is modified for [DEAR IMGUI] and deviates from the original stb_textedit. +// There is not necessarily a '[DEAR IMGUI]' at the usage sites. +#ifndef IMSTB_TEXTEDIT_GETPREVCHARINDEX +#define IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, idx) (idx - 1) +#endif +#ifndef IMSTB_TEXTEDIT_GETNEXTCHARINDEX +#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx) (idx + 1) +#endif + #ifdef STB_TEXTEDIT_IS_SPACE static int is_word_boundary( IMSTB_TEXTEDIT_STRING *str, int idx ) { @@ -720,36 +739,44 @@ static int stb_textedit_paste_internal(IMSTB_TEXTEDIT_STRING *str, STB_TexteditS #define STB_TEXTEDIT_KEYTYPE int #endif +// [DEAR IMGUI] Added stb_textedit_text(), extracted out and called by stb_textedit_key() for backward compatibility. +static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len) +{ + // can't add newline in single-line mode + if (text[0] == '\n' && state->single_line) + return; + + if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { + stb_text_makeundo_replace(str, state, state->cursor, 1, 1); + STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); + if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) { + state->cursor += text_len; + state->has_preferred_x = 0; + } + } + else { + stb_textedit_delete_selection(str, state); // implicitly clamps + if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) { + stb_text_makeundo_insert(state, state->cursor, text_len); + state->cursor += text_len; + state->has_preferred_x = 0; + } + } +} + // API key: process a keyboard input static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key) { retry: switch (key) { default: { +#ifdef STB_TEXTEDIT_KEYTOTEXT int c = STB_TEXTEDIT_KEYTOTEXT(key); if (c > 0) { - IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE) c; - - // can't add newline in single-line mode - if (c == '\n' && state->single_line) - break; - - if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { - stb_text_makeundo_replace(str, state, state->cursor, 1, 1); - STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); - if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { - ++state->cursor; - state->has_preferred_x = 0; - } - } else { - stb_textedit_delete_selection(str,state); // implicitly clamps - if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) { - stb_text_makeundo_insert(state, state->cursor, 1); - ++state->cursor; - state->has_preferred_x = 0; - } - } + IMSTB_TEXTEDIT_CHARTYPE ch = (IMSTB_TEXTEDIT_CHARTYPE)c; + stb_textedit_text(str, state, &ch, 1); } +#endif break; } @@ -775,7 +802,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat stb_textedit_move_to_first(state); else if (state->cursor > 0) - --state->cursor; + state->cursor = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor); state->has_preferred_x = 0; break; @@ -784,7 +811,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat if (STB_TEXT_HAS_SELECTION(state)) stb_textedit_move_to_last(str, state); else - ++state->cursor; + state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); stb_textedit_clamp(str, state); state->has_preferred_x = 0; break; @@ -794,7 +821,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat stb_textedit_prep_selection_at_cursor(state); // move selection left if (state->select_end > 0) - --state->select_end; + state->select_end = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->select_end); state->cursor = state->select_end; state->has_preferred_x = 0; break; @@ -844,7 +871,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT: stb_textedit_prep_selection_at_cursor(state); // move selection right - ++state->select_end; + state->select_end = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->select_end); stb_textedit_clamp(str, state); state->cursor = state->select_end; state->has_preferred_x = 0; @@ -900,7 +927,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat x += dx; if (x > goal_x) break; - ++state->cursor; + state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); } stb_textedit_clamp(str, state); @@ -962,7 +989,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat x += dx; if (x > goal_x) break; - ++state->cursor; + state->cursor = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor); } stb_textedit_clamp(str, state); @@ -990,7 +1017,7 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat else { int n = STB_TEXTEDIT_STRINGLEN(str); if (state->cursor < n) - stb_textedit_delete(str, state, state->cursor, 1); + stb_textedit_delete(str, state, state->cursor, IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor) - state->cursor); } state->has_preferred_x = 0; break; @@ -1002,8 +1029,9 @@ static void stb_textedit_key(IMSTB_TEXTEDIT_STRING *str, STB_TexteditState *stat else { stb_textedit_clamp(str, state); if (state->cursor > 0) { - stb_textedit_delete(str, state, state->cursor-1, 1); - --state->cursor; + int prev = IMSTB_TEXTEDIT_GETPREVCHARINDEX(str, state->cursor); + stb_textedit_delete(str, state, prev, state->cursor - prev); + state->cursor = prev; } } state->has_preferred_x = 0; diff --git a/src/imgui/imgui.cpp b/src/imgui/imgui.cpp index 2c861126..f85d4f13 100644 --- a/src/imgui/imgui.cpp +++ b/src/imgui/imgui.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (main code and documentation) // Help: @@ -10,7 +10,7 @@ // - FAQ ........................ https://dearimgui.com/faq (in repository as docs/FAQ.md) // - Homepage ................... https://github.com/ocornut/imgui // - Releases & changelog ....... https://github.com/ocornut/imgui/releases -// - Gallery .................... https://github.com/ocornut/imgui/issues/7503 (please post your screenshots/video there!) +// - Gallery .................... https://github.com/ocornut/imgui/issues?q=label%3Agallery (please post your screenshots/video there!) // - Wiki ....................... https://github.com/ocornut/imgui/wiki (lots of good stuff there) // - Getting Started https://github.com/ocornut/imgui/wiki/Getting-Started (how to integrate in an existing app by adding ~25 lines of code) // - Third-party Extensions https://github.com/ocornut/imgui/wiki/Useful-Extensions (ImPlot & many more) @@ -63,7 +63,7 @@ CODE // [SECTION] INCLUDES // [SECTION] FORWARD DECLARATIONS // [SECTION] CONTEXT AND MEMORY ALLOCATORS -// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO, ImGuiPlatformIO) // [SECTION] MISC HELPERS/UTILITIES (Geometry functions) // [SECTION] MISC HELPERS/UTILITIES (String, Format, Hash functions) // [SECTION] MISC HELPERS/UTILITIES (File functions) @@ -79,7 +79,7 @@ CODE // [SECTION] MAIN CODE (most of the code! lots of stuff, needs tidying up!) // [SECTION] ID STACK // [SECTION] INPUTS -// [SECTION] ERROR CHECKING +// [SECTION] ERROR CHECKING, STATE RECOVERY // [SECTION] ITEM SUBMISSION // [SECTION] LAYOUT // [SECTION] SCROLLING @@ -183,8 +183,8 @@ CODE - Consoles/Tablet/Phone users: Consider using a Synergy 1.x server (on your PC) + run examples/libs/synergy/uSynergy.c (on your console/tablet/phone app) in order to share your PC mouse/keyboard. - See https://github.com/ocornut/imgui/wiki/Useful-Extensions#remoting for other remoting solutions. - - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the ImGuiConfigFlags_NavEnableSetMousePos flag. - Enabling ImGuiConfigFlags_NavEnableSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs Dear ImGui to move your mouse cursor along with navigation movements. + - On a TV/console system where readability may be lower or mouse inputs may be awkward, you may want to set the io.ConfigNavMoveSetMousePos flag. + Enabling io.ConfigNavMoveSetMousePos + ImGuiBackendFlags_HasSetMousePos instructs Dear ImGui to move your mouse cursor along with navigation movements. When enabled, the NewFrame() function may alter 'io.MousePos' and set 'io.WantSetMousePos' to notify you that it wants the mouse cursor to be moved. When that happens your backend NEEDS to move the OS or underlying mouse cursor on the next frame. Some of the backends in examples/ do that. (If you set the NavEnableSetMousePos flag but don't honor 'io.WantSetMousePos' properly, Dear ImGui will misbehave as it will see your mouse moving back & forth!) @@ -430,6 +430,37 @@ CODE When you are not sure about an old symbol or function name, try using the Search/Find function of your IDE to look for comments or references in all imgui files. You can read releases logs https://github.com/ocornut/imgui/releases for more details. + - 2024/10/18 (1.91.4) - renamed ImGuiCol_NavHighlight to ImGuiCol_NavCursor (for consistency with newly exposed and reworked features). Kept inline redirection enum (will obsolete). + - 2024/10/14 (1.91.4) - moved ImGuiConfigFlags_NavEnableSetMousePos to standalone io.ConfigNavMoveSetMousePos bool. + moved ImGuiConfigFlags_NavNoCaptureKeyboard to standalone io.ConfigNavCaptureKeyboard bool (note the inverted value!). + kept legacy names (will obsolete) + code that copies settings once the first time. Dynamically changing the old value won't work. Switch to using the new value! + - 2024/10/10 (1.91.4) - the typedef for ImTextureID now defaults to ImU64 instead of void*. (#1641) + this removes the requirement to redefine it for backends which are e.g. storing descriptor sets or other 64-bits structures when building on 32-bits archs. It therefore simplify various building scripts/helpers. + you may have compile-time issues if you were casting to 'void*' instead of 'ImTextureID' when passing your types to functions taking ImTextureID values, e.g. ImGui::Image(). + in doubt it is almost always better to do an intermediate intptr_t cast, since it allows casting any pointer/integer type without warning: + - May warn: ImGui::Image((void*)MyTextureData, ...); + - May warn: ImGui::Image((void*)(intptr_t)MyTextureData, ...); + - Won't warn: ImGui::Image((ImTextureID)(intptr_t)MyTextureData), ...); + - note that you can always define ImTextureID to be your own high-level structures (with dedicated constructors) if you like. + - 2024/10/03 (1.91.3) - drags: treat v_min==v_max as a valid clamping range when != 0.0f. Zero is a still special value due to legacy reasons, unless using ImGuiSliderFlags_ClampZeroRange. (#7968, #3361, #76) + - drags: extended behavior of ImGuiSliderFlags_AlwaysClamp to include _ClampZeroRange. It considers v_min==v_max==0.0f as a valid clamping range (aka edits not allowed). + although unlikely, it you wish to only clamp on text input but want v_min==v_max==0.0f to mean unclamped drags, you can use _ClampOnInput instead of _AlwaysClamp. (#7968, #3361, #76) + - 2024/09/10 (1.91.2) - internals: using multiple overlayed ButtonBehavior() with same ID will now have io.ConfigDebugHighlightIdConflicts=true feature emit a warning. (#8030) + it was one of the rare case where using same ID is legal. workarounds: (1) use single ButtonBehavior() call with multiple _MouseButton flags, or (2) surround the calls with PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); ... PopItemFlag() + - 2024/08/23 (1.91.1) - renamed ImGuiChildFlags_Border to ImGuiChildFlags_Borders for consistency. kept inline redirection flag. + - 2024/08/22 (1.91.1) - moved some functions from ImGuiIO to ImGuiPlatformIO structure: + - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn + changed 'void* user_data' to 'ImGuiContext* ctx'. Pull your user data from platform_io.ClipboardUserData. + - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn + same as above line. + - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn (#7660) + - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn + - io.PlatformLocaleDecimalPoint -> platform_io.Platform_LocaleDecimalPoint (#7389, #6719, #2278) + - access those via GetPlatformIO() instead of GetIO(). + some were introduced very recently and often automatically setup by core library and backends, so for those we are exceptionally not maintaining a legacy redirection symbol. + - commented the old ImageButton() signature obsoleted in 1.89 (~August 2022). As a reminder: + - old ImageButton() before 1.89 used ImTextureId as item id (created issue with e.g. multiple buttons in same scope, transient texture id values, opaque computation of ID) + - new ImageButton() since 1.89 requires an explicit 'const char* str_id' + - old ImageButton() before 1.89 had frame_padding' override argument. + - new ImageButton() since 1.89 always use style.FramePadding, which you can freely override with PushStyleVar()/PopStyleVar(). - 2024/07/25 (1.91.0) - obsoleted GetContentRegionMax(), GetWindowContentRegionMin() and GetWindowContentRegionMax(). (see #7838 on GitHub for more info) you should never need those functions. you can do everything with GetCursorScreenPos() and GetContentRegionAvail() in a more simple way. - instead of: GetWindowContentRegionMax().x - GetCursorPos().x @@ -501,6 +532,7 @@ CODE - new: BeginChild("Name", size, ImGuiChildFlags_Border) - old: BeginChild("Name", size, false) - new: BeginChild("Name", size) or BeginChild("Name", 0) or BeginChild("Name", size, ImGuiChildFlags_None) + **AMEND FROM THE FUTURE: from 1.91.1, 'ImGuiChildFlags_Border' is called 'ImGuiChildFlags_Borders'** - 2023/11/02 (1.90.0) - BeginChild: added child-flag ImGuiChildFlags_AlwaysUseWindowPadding as a replacement for the window-flag ImGuiWindowFlags_AlwaysUseWindowPadding: the feature only ever made sense for BeginChild() anyhow. - old: BeginChild("Name", size, 0, ImGuiWindowFlags_AlwaysUseWindowPadding); - new: BeginChild("Name", size, ImGuiChildFlags_AlwaysUseWindowPadding, 0); @@ -1121,7 +1153,9 @@ static const float WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER = 0.04f; // Reduc static const float WINDOWS_MOUSE_WHEEL_SCROLL_LOCK_TIMER = 0.70f; // Lock scrolled window (so it doesn't pick child windows that are scrolling through) for a certain time, unless mouse moved. // Tooltip offset -static const ImVec2 TOOLTIP_DEFAULT_OFFSET = ImVec2(16, 10); // Multiplied by g.Style.MouseCursorScale +static const ImVec2 TOOLTIP_DEFAULT_OFFSET_MOUSE = ImVec2(16, 10); // Multiplied by g.Style.MouseCursorScale +static const ImVec2 TOOLTIP_DEFAULT_OFFSET_TOUCH = ImVec2(0, -20); // Multiplied by g.Style.MouseCursorScale +static const ImVec2 TOOLTIP_DEFAULT_PIVOT_TOUCH = ImVec2(0.5f, 1.0f); // Multiplied by g.Style.MouseCursorScale //------------------------------------------------------------------------- // [SECTION] FORWARD DECLARATIONS @@ -1140,11 +1174,11 @@ static void WindowSettingsHandler_ReadLine(ImGuiContext*, ImGuiSetti static void WindowSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSettingsHandler*); static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf); -// Platform Dependents default implementation for IO functions -static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx); -static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text); -static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); -static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext* ctx, const char* path); +// Platform Dependents default implementation for ImGuiPlatformIO functions +static const char* Platform_GetClipboardTextFn_DefaultImpl(ImGuiContext* ctx); +static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext* ctx, const char* text); +static void Platform_SetImeDataFn_DefaultImpl(ImGuiContext* ctx, ImGuiViewport* viewport, ImGuiPlatformImeData* data); +static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext* ctx, const char* path); namespace ImGui { @@ -1166,6 +1200,7 @@ static bool NavScoreItem(ImGuiNavItemData* result); static void NavApplyItemToResult(ImGuiNavItemData* result); static void NavProcessItem(); static void NavProcessItemForTabbingRequest(ImGuiID id, ImGuiItemFlags item_flags, ImGuiNavMoveFlags move_flags); +static ImGuiInputSource NavCalcPreferredRefPosSource(); static ImVec2 NavCalcPreferredRefPos(); static void NavSaveLastChildNavWindowIntoParent(ImGuiWindow* nav_window); static ImGuiWindow* NavRestoreLastChildNavWindow(ImGuiWindow* window); @@ -1244,7 +1279,7 @@ static ImGuiMemFreeFunc GImAllocatorFreeFunc = FreeWrapper; static void* GImAllocatorUserData = NULL; //----------------------------------------------------------------------------- -// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO) +// [SECTION] USER FACING STRUCTURES (ImGuiStyle, ImGuiIO, ImGuiPlatformIO) //----------------------------------------------------------------------------- ImGuiStyle::ImGuiStyle() @@ -1365,11 +1400,14 @@ ImGuiIO::ImGuiIO() FontAllowUserScaling = false; DisplayFramebufferScale = ImVec2(1.0f, 1.0f); - MouseDoubleClickTime = 0.30f; - MouseDoubleClickMaxDist = 6.0f; - MouseDragThreshold = 6.0f; - KeyRepeatDelay = 0.275f; - KeyRepeatRate = 0.050f; + // Keyboard/Gamepad Navigation options + ConfigNavSwapGamepadButtons = false; + ConfigNavMoveSetMousePos = false; + ConfigNavCaptureKeyboard = true; + ConfigNavEscapeClearFocusItem = true; + ConfigNavEscapeClearFocusWindow = false; + ConfigNavCursorVisibleAuto = true; + ConfigNavCursorVisibleAlways = false; // Miscellaneous options MouseDrawCursor = false; @@ -1378,23 +1416,35 @@ ImGuiIO::ImGuiIO() #else ConfigMacOSXBehaviors = false; #endif - ConfigNavSwapGamepadButtons = false; ConfigInputTrickleEventQueue = true; ConfigInputTextCursorBlink = true; ConfigInputTextEnterKeepActive = false; ConfigDragClickToInputText = false; ConfigWindowsResizeFromEdges = true; ConfigWindowsMoveFromTitleBarOnly = false; + ConfigScrollbarScrollByPage = true; ConfigMemoryCompactTimer = 60.0f; + ConfigDebugIsDebuggerPresent = false; + ConfigDebugHighlightIdConflicts = true; ConfigDebugBeginReturnValueOnce = false; ConfigDebugBeginReturnValueLoop = false; + ConfigErrorRecovery = true; + ConfigErrorRecoveryEnableAssert = true; + ConfigErrorRecoveryEnableDebugLog = true; + ConfigErrorRecoveryEnableTooltip = true; + + // Inputs Behaviors + MouseDoubleClickTime = 0.30f; + MouseDoubleClickMaxDist = 6.0f; + MouseDragThreshold = 6.0f; + KeyRepeatDelay = 0.275f; + KeyRepeatRate = 0.050f; + // Platform Functions // Note: Initialize() will setup default clipboard/ime handlers. BackendPlatformName = BackendRendererName = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; - PlatformOpenInShellUserData = NULL; - PlatformLocaleDecimalPoint = '.'; // Input (NB: we already have memset zero the entire structure!) MousePos = ImVec2(-FLT_MAX, -FLT_MAX); @@ -1762,6 +1812,13 @@ void ImGuiIO::AddFocusEvent(bool focused) g.InputEventsQueue.push_back(e); } +ImGuiPlatformIO::ImGuiPlatformIO() +{ + // Most fields are initialized with zero + memset(this, 0, sizeof(*this)); + Platform_LocaleDecimalPoint = '.'; +} + //----------------------------------------------------------------------------- // [SECTION] MISC HELPERS/UTILITIES (Geometry functions) //----------------------------------------------------------------------------- @@ -1952,7 +2009,7 @@ const char* ImStreolRange(const char* str, const char* str_end) return p ? p : str_end; } -const ImWchar* ImStrbolW(const ImWchar* buf_mid_line, const ImWchar* buf_begin) // find beginning-of-line +const char* ImStrbol(const char* buf_mid_line, const char* buf_begin) // find beginning-of-line { while (buf_mid_line > buf_begin && buf_mid_line[-1] != '\n') buf_mid_line--; @@ -3249,7 +3306,7 @@ void ImGui::PopStyleColor(int count) ImGuiContext& g = *GImGui; if (g.ColorStack.Size < count) { - IM_ASSERT_USER_ERROR(g.ColorStack.Size > count, "Calling PopStyleColor() too many times!"); + IM_ASSERT_USER_ERROR(0, "Calling PopStyleColor() too many times!"); count = g.ColorStack.Size; } while (count > 0) @@ -3309,28 +3366,56 @@ void ImGui::PushStyleVar(ImGuiStyleVar idx, float val) { ImGuiContext& g = *GImGui; const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); - if (var_info->Type == ImGuiDataType_Float && var_info->Count == 1) + if (var_info->Type != ImGuiDataType_Float || var_info->Count != 1) { - float* pvar = (float*)var_info->GetVarPtr(&g.Style); - g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); - *pvar = val; + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); return; } - IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); + float* pvar = (float*)var_info->GetVarPtr(&g.Style); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; +} + +void ImGui::PushStyleVarX(ImGuiStyleVar idx, float val_x) +{ + ImGuiContext& g = *GImGui; + const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2) + { + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); + return; + } + ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); + pvar->x = val_x; +} + +void ImGui::PushStyleVarY(ImGuiStyleVar idx, float val_y) +{ + ImGuiContext& g = *GImGui; + const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); + if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2) + { + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); + return; + } + ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); + pvar->y = val_y; } void ImGui::PushStyleVar(ImGuiStyleVar idx, const ImVec2& val) { ImGuiContext& g = *GImGui; const ImGuiDataVarInfo* var_info = GetStyleVarInfo(idx); - if (var_info->Type == ImGuiDataType_Float && var_info->Count == 2) + if (var_info->Type != ImGuiDataType_Float || var_info->Count != 2) { - ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); - g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); - *pvar = val; + IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); return; } - IM_ASSERT_USER_ERROR(0, "Calling PushStyleVar() variant with wrong type!"); + ImVec2* pvar = (ImVec2*)var_info->GetVarPtr(&g.Style); + g.StyleVarStack.push_back(ImGuiStyleMod(idx, *pvar)); + *pvar = val; } void ImGui::PopStyleVar(int count) @@ -3338,7 +3423,7 @@ void ImGui::PopStyleVar(int count) ImGuiContext& g = *GImGui; if (g.StyleVarStack.Size < count) { - IM_ASSERT_USER_ERROR(g.StyleVarStack.Size > count, "Calling PopStyleVar() too many times!"); + IM_ASSERT_USER_ERROR(0, "Calling PopStyleVar() too many times!"); count = g.StyleVarStack.Size; } while (count > 0) @@ -3411,7 +3496,7 @@ const char* ImGui::GetStyleColorName(ImGuiCol idx) case ImGuiCol_TextLink: return "TextLink"; case ImGuiCol_TextSelectedBg: return "TextSelectedBg"; case ImGuiCol_DragDropTarget: return "DragDropTarget"; - case ImGuiCol_NavHighlight: return "NavHighlight"; + case ImGuiCol_NavCursor: return "NavCursor"; case ImGuiCol_NavWindowingHighlight: return "NavWindowingHighlight"; case ImGuiCol_NavWindowingDimBg: return "NavWindowingDimBg"; case ImGuiCol_ModalWindowDimBg: return "ModalWindowDimBg"; @@ -3591,13 +3676,13 @@ void ImGui::RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, con } // Render a rectangle shaped with optional rounding and borders -void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border, float rounding) +void ImGui::RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool borders, float rounding) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; window->DrawList->AddRectFilled(p_min, p_max, fill_col, rounding); const float border_size = g.Style.FrameBorderSize; - if (border && border_size > 0.0f) + if (borders && border_size > 0.0f) { window->DrawList->AddRect(p_min + ImVec2(1, 1), p_max + ImVec2(1, 1), GetColorU32(ImGuiCol_BorderShadow), rounding, 0, border_size); window->DrawList->AddRect(p_min, p_max, GetColorU32(ImGuiCol_Border), rounding, 0, border_size); @@ -3616,24 +3701,26 @@ void ImGui::RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding) } } -void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags) +void ImGui::RenderNavCursor(const ImRect& bb, ImGuiID id, ImGuiNavRenderCursorFlags flags) { ImGuiContext& g = *GImGui; if (id != g.NavId) return; - if (g.NavDisableHighlight && !(flags & ImGuiNavHighlightFlags_AlwaysDraw)) + if (!g.NavCursorVisible && !(flags & ImGuiNavRenderCursorFlags_AlwaysDraw)) + return; + if (id == g.LastItemData.ID && (g.LastItemData.ItemFlags & ImGuiItemFlags_NoNav)) return; ImGuiWindow* window = g.CurrentWindow; if (window->DC.NavHideHighlightOneFrame) return; - float rounding = (flags & ImGuiNavHighlightFlags_NoRounding) ? 0.0f : g.Style.FrameRounding; + float rounding = (flags & ImGuiNavRenderCursorFlags_NoRounding) ? 0.0f : g.Style.FrameRounding; ImRect display_rect = bb; display_rect.ClipWith(window->ClipRect); const float thickness = 2.0f; - if (flags & ImGuiNavHighlightFlags_Compact) + if (flags & ImGuiNavRenderCursorFlags_Compact) { - window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, thickness); + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavCursor), rounding, 0, thickness); } else { @@ -3642,7 +3729,7 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl bool fully_visible = window->ClipRect.Contains(display_rect); if (!fully_visible) window->DrawList->PushClipRect(display_rect.Min, display_rect.Max); - window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavHighlight), rounding, 0, thickness); + window->DrawList->AddRect(display_rect.Min, display_rect.Max, GetColorU32(ImGuiCol_NavCursor), rounding, 0, thickness); if (!fully_visible) window->DrawList->PopClipRect(); } @@ -3651,7 +3738,8 @@ void ImGui::RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFl void ImGui::RenderMouseCursor(ImVec2 base_pos, float base_scale, ImGuiMouseCursor mouse_cursor, ImU32 col_fill, ImU32 col_border, ImU32 col_shadow) { ImGuiContext& g = *GImGui; - IM_ASSERT(mouse_cursor > ImGuiMouseCursor_None && mouse_cursor < ImGuiMouseCursor_COUNT); + if (mouse_cursor <= ImGuiMouseCursor_None || mouse_cursor >= ImGuiMouseCursor_COUNT) // We intentionally accept out of bound values. + mouse_cursor = ImGuiMouseCursor_Arrow; ImFontAtlas* font_atlas = g.DrawListSharedData.Font->ContainerAtlas; for (ImGuiViewportP* viewport : g.Viewports) { @@ -3731,7 +3819,7 @@ void ImGui::DestroyContext(ImGuiContext* ctx) IM_DELETE(ctx); } -// IMPORTANT: ###xxx suffixes must be same in ALL languages to allow for automation. +// IMPORTANT: interactive elements requires a fixed ###xxx suffix, it must be same in ALL languages to allow for automation. static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { { ImGuiLocKey_VersionStr, "Dear ImGui " IMGUI_VERSION " (" IM_STRINGIFY(IMGUI_VERSION_NUM) ")" }, @@ -3742,9 +3830,225 @@ static const ImGuiLocEntry GLocalizationEntriesEnUS[] = { ImGuiLocKey_WindowingMainMenuBar, "(Main menu bar)" }, { ImGuiLocKey_WindowingPopup, "(Popup)" }, { ImGuiLocKey_WindowingUntitled, "(Untitled)" }, - { ImGuiLocKey_CopyLink, "Copy Link###CopyLink" }, + { ImGuiLocKey_OpenLink_s, "Open '%s'" }, + { ImGuiLocKey_CopyLink, "Copy Link###CopyLink" }, }; +ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas) +{ + IO.Ctx = this; + InputTextState.Ctx = this; + + Initialized = false; + FontAtlasOwnedByContext = shared_font_atlas ? false : true; + Font = NULL; + FontSize = FontBaseSize = FontScale = CurrentDpiScale = 0.0f; + IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)(); + Time = 0.0f; + FrameCount = 0; + FrameCountEnded = FrameCountRendered = -1; + WithinFrameScope = WithinFrameScopeWithImplicitWindow = WithinEndChild = false; + GcCompactAll = false; + TestEngineHookItems = false; + TestEngine = NULL; + memset(ContextName, 0, sizeof(ContextName)); + + InputEventsNextMouseSource = ImGuiMouseSource_Mouse; + InputEventsNextEventId = 1; + + WindowsActiveCount = 0; + CurrentWindow = NULL; + HoveredWindow = NULL; + HoveredWindowUnderMovingWindow = NULL; + HoveredWindowBeforeClear = NULL; + MovingWindow = NULL; + WheelingWindow = NULL; + WheelingWindowStartFrame = WheelingWindowScrolledFrame = -1; + WheelingWindowReleaseTimer = 0.0f; + + DebugDrawIdConflicts = 0; + DebugHookIdInfo = 0; + HoveredId = HoveredIdPreviousFrame = 0; + HoveredIdPreviousFrameItemCount = 0; + HoveredIdAllowOverlap = false; + HoveredIdIsDisabled = false; + HoveredIdTimer = HoveredIdNotActiveTimer = 0.0f; + ItemUnclipByLog = false; + ActiveId = 0; + ActiveIdIsAlive = 0; + ActiveIdTimer = 0.0f; + ActiveIdIsJustActivated = false; + ActiveIdAllowOverlap = false; + ActiveIdNoClearOnFocusLoss = false; + ActiveIdHasBeenPressedBefore = false; + ActiveIdHasBeenEditedBefore = false; + ActiveIdHasBeenEditedThisFrame = false; + ActiveIdFromShortcut = false; + ActiveIdClickOffset = ImVec2(-1, -1); + ActiveIdWindow = NULL; + ActiveIdSource = ImGuiInputSource_None; + ActiveIdMouseButton = -1; + ActiveIdPreviousFrame = 0; + ActiveIdPreviousFrameIsAlive = false; + ActiveIdPreviousFrameHasBeenEditedBefore = false; + ActiveIdPreviousFrameWindow = NULL; + LastActiveId = 0; + LastActiveIdTimer = 0.0f; + + LastKeyboardKeyPressTime = LastKeyModsChangeTime = LastKeyModsChangeFromNoneTime = -1.0; + + ActiveIdUsingNavDirMask = 0x00; + ActiveIdUsingAllKeyboardKeys = false; + + CurrentFocusScopeId = 0; + CurrentItemFlags = ImGuiItemFlags_None; + DebugShowGroupRects = false; + + NavCursorVisible = false; + NavHighlightItemUnderNav = false; + NavMousePosDirty = false; + NavIdIsAlive = false; + NavId = 0; + NavWindow = NULL; + NavFocusScopeId = NavActivateId = NavActivateDownId = NavActivatePressedId = 0; + NavLayer = ImGuiNavLayer_Main; + NavNextActivateId = 0; + NavActivateFlags = NavNextActivateFlags = ImGuiActivateFlags_None; + NavHighlightActivatedId = 0; + NavHighlightActivatedTimer = 0.0f; + NavInputSource = ImGuiInputSource_Keyboard; + NavLastValidSelectionUserData = ImGuiSelectionUserData_Invalid; + NavCursorHideFrames = 0; + + NavAnyRequest = false; + NavInitRequest = false; + NavInitRequestFromMove = false; + NavMoveSubmitted = false; + NavMoveScoringItems = false; + NavMoveForwardToNextFrame = false; + NavMoveFlags = ImGuiNavMoveFlags_None; + NavMoveScrollFlags = ImGuiScrollFlags_None; + NavMoveKeyMods = ImGuiMod_None; + NavMoveDir = NavMoveDirForDebug = NavMoveClipDir = ImGuiDir_None; + NavScoringDebugCount = 0; + NavTabbingDir = 0; + NavTabbingCounter = 0; + + NavJustMovedFromFocusScopeId = NavJustMovedToId = NavJustMovedToFocusScopeId = 0; + NavJustMovedToKeyMods = ImGuiMod_None; + NavJustMovedToIsTabbing = false; + NavJustMovedToHasSelectionData = false; + + // All platforms use Ctrl+Tab but Ctrl<>Super are swapped on Mac... + // FIXME: Because this value is stored, it annoyingly interfere with toggling io.ConfigMacOSXBehaviors updating this.. + ConfigNavWindowingKeyNext = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiKey_Tab); + ConfigNavWindowingKeyPrev = IO.ConfigMacOSXBehaviors ? (ImGuiMod_Super | ImGuiMod_Shift | ImGuiKey_Tab) : (ImGuiMod_Ctrl | ImGuiMod_Shift | ImGuiKey_Tab); + NavWindowingTarget = NavWindowingTargetAnim = NavWindowingListWindow = NULL; + NavWindowingTimer = NavWindowingHighlightAlpha = 0.0f; + NavWindowingToggleLayer = false; + NavWindowingToggleKey = ImGuiKey_None; + + DimBgRatio = 0.0f; + + DragDropActive = DragDropWithinSource = DragDropWithinTarget = false; + DragDropSourceFlags = ImGuiDragDropFlags_None; + DragDropSourceFrameCount = -1; + DragDropMouseButton = -1; + DragDropTargetId = 0; + DragDropAcceptFlags = ImGuiDragDropFlags_None; + DragDropAcceptIdCurrRectSurface = 0.0f; + DragDropAcceptIdPrev = DragDropAcceptIdCurr = 0; + DragDropAcceptFrameCount = -1; + DragDropHoldJustPressedId = 0; + memset(DragDropPayloadBufLocal, 0, sizeof(DragDropPayloadBufLocal)); + + ClipperTempDataStacked = 0; + + CurrentTable = NULL; + TablesTempDataStacked = 0; + CurrentTabBar = NULL; + CurrentMultiSelect = NULL; + MultiSelectTempDataStacked = 0; + + HoverItemDelayId = HoverItemDelayIdPreviousFrame = HoverItemUnlockedStationaryId = HoverWindowUnlockedStationaryId = 0; + HoverItemDelayTimer = HoverItemDelayClearTimer = 0.0f; + + MouseCursor = ImGuiMouseCursor_Arrow; + MouseStationaryTimer = 0.0f; + + TempInputId = 0; + memset(&DataTypeZeroValue, 0, sizeof(DataTypeZeroValue)); + BeginMenuDepth = BeginComboDepth = 0; + ColorEditOptions = ImGuiColorEditFlags_DefaultOptions_; + ColorEditCurrentID = ColorEditSavedID = 0; + ColorEditSavedHue = ColorEditSavedSat = 0.0f; + ColorEditSavedColor = 0; + WindowResizeRelativeMode = false; + ScrollbarSeekMode = 0; + ScrollbarClickDeltaToGrabCenter = 0.0f; + SliderGrabClickOffset = 0.0f; + SliderCurrentAccum = 0.0f; + SliderCurrentAccumDirty = false; + DragCurrentAccumDirty = false; + DragCurrentAccum = 0.0f; + DragSpeedDefaultRatio = 1.0f / 100.0f; + DisabledAlphaBackup = 0.0f; + DisabledStackSize = 0; + TooltipOverrideCount = 0; + TooltipPreviousWindow = NULL; + + PlatformImeData.InputPos = ImVec2(0.0f, 0.0f); + PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission + + SettingsLoaded = false; + SettingsDirtyTimer = 0.0f; + HookIdNext = 0; + + memset(LocalizationTable, 0, sizeof(LocalizationTable)); + + LogEnabled = false; + LogType = ImGuiLogType_None; + LogNextPrefix = LogNextSuffix = NULL; + LogFile = NULL; + LogLinePosY = FLT_MAX; + LogLineFirstItem = false; + LogDepthRef = 0; + LogDepthToExpand = LogDepthToExpandDefault = 2; + + ErrorCallback = NULL; + ErrorCallbackUserData = NULL; + ErrorFirst = true; + ErrorCountCurrentFrame = 0; + StackSizesInBeginForCurrentWindow = NULL; + + DebugDrawIdConflictsCount = 0; + DebugLogFlags = ImGuiDebugLogFlags_EventError | ImGuiDebugLogFlags_OutputToTTY; + DebugLocateId = 0; + DebugLogSkippedErrors = 0; + DebugLogAutoDisableFlags = ImGuiDebugLogFlags_None; + DebugLogAutoDisableFrames = 0; + DebugLocateFrames = 0; + DebugBeginReturnValueCullDepth = -1; + DebugItemPickerActive = false; + DebugItemPickerMouseButton = ImGuiMouseButton_Left; + DebugItemPickerBreakId = 0; + DebugFlashStyleColorTime = 0.0f; + DebugFlashStyleColorIdx = ImGuiCol_COUNT; + + // Same as DebugBreakClearData(). Those fields are scattered in their respective subsystem to stay in hot-data locations + DebugBreakInWindow = 0; + DebugBreakInTable = 0; + DebugBreakInLocateId = false; + DebugBreakKeyChord = ImGuiKey_Pause; + DebugBreakInShortcutRouting = ImGuiKey_None; + + memset(FramerateSecPerFrame, 0, sizeof(FramerateSecPerFrame)); + FramerateSecPerFrameIdx = FramerateSecPerFrameCount = 0; + FramerateSecPerFrameAccum = 0.0f; + WantCaptureMouseNextFrame = WantCaptureKeyboardNextFrame = WantTextInputNextFrame = -1; + memset(TempKeychordName, 0, sizeof(TempKeychordName)); +} + void ImGui::Initialize() { ImGuiContext& g = *GImGui; @@ -3767,12 +4071,11 @@ void ImGui::Initialize() // Setup default localization table LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS)); - // Setup default platform clipboard/IME handlers. - g.IO.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations - g.IO.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; - g.IO.ClipboardUserData = (void*)&g; // Default implementation use the ImGuiContext as user data (ideally those would be arguments to the function) - g.IO.PlatformOpenInShellFn = PlatformOpenInShellFn_DefaultImpl; - g.IO.PlatformSetImeDataFn = PlatformSetImeDataFn_DefaultImpl; + // Setup default ImGuiPlatformIO clipboard/IME handlers. + g.PlatformIO.Platform_GetClipboardTextFn = Platform_GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations + g.PlatformIO.Platform_SetClipboardTextFn = Platform_SetClipboardTextFn_DefaultImpl; + g.PlatformIO.Platform_OpenInShellFn = Platform_OpenInShellFn_DefaultImpl; + g.PlatformIO.Platform_SetImeDataFn = Platform_SetImeDataFn_DefaultImpl; // Create default viewport ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); @@ -3951,12 +4254,13 @@ static void SetCurrentWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; g.CurrentWindow = window; + g.StackSizesInBeginForCurrentWindow = g.CurrentWindow ? &g.CurrentWindowStack.back().StackSizesInBegin : NULL; g.CurrentTable = window && window->DC.CurrentTableIdx != -1 ? g.Tables.GetByIndex(window->DC.CurrentTableIdx) : NULL; g.CurrentDpiScale = 1.0f; // FIXME-DPI: WIP this is modified in docking if (window) { g.FontSize = g.DrawListSharedData.FontSize = window->CalcFontSize(); - g.FontScale = g.FontSize / g.Font->FontSize; + g.FontScale = g.DrawListSharedData.FontScale = g.FontSize / g.Font->FontSize; ImGui::NavUpdateCurrentWindowIsScrollPushableX(); } } @@ -4076,10 +4380,10 @@ ImGuiID ImGui::GetHoveredID() void ImGui::MarkItemEdited(ImGuiID id) { - // This marking is solely to be able to provide info for IsItemDeactivatedAfterEdit(). + // This marking is to be able to provide info for IsItemDeactivatedAfterEdit(). // ActiveId might have been released by the time we call this (as in the typical press/release button behavior) but still need to fill the data. ImGuiContext& g = *GImGui; - if (g.LockMarkEdited > 0) + if (g.LastItemData.ItemFlags & ImGuiItemFlags_NoMarkEdited) return; if (g.ActiveId == id || g.ActiveId == 0) { @@ -4145,14 +4449,14 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT((flags & ~ImGuiHoveredFlags_AllowedMaskForIsItemHovered) == 0 && "Invalid flags for IsItemHovered()!"); + IM_ASSERT_USER_ERROR((flags & ~ImGuiHoveredFlags_AllowedMaskForIsItemHovered) == 0, "Invalid flags for IsItemHovered()!"); - if (g.NavDisableMouseHover && !g.NavDisableHighlight && !(flags & ImGuiHoveredFlags_NoNavOverride)) + if (g.NavHighlightItemUnderNav && g.NavCursorVisible && !(flags & ImGuiHoveredFlags_NoNavOverride)) { - if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) - return false; if (!IsItemFocused()) return false; + if ((g.LastItemData.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + return false; if (flags & ImGuiHoveredFlags_ForTooltip) flags = ApplyHoverFlagsForTooltip(flags, g.Style.HoverFlagsForTooltipNav); @@ -4167,8 +4471,6 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) if (flags & ImGuiHoveredFlags_ForTooltip) flags = ApplyHoverFlagsForTooltip(flags, g.Style.HoverFlagsForTooltipMouse); - IM_ASSERT((flags & (ImGuiHoveredFlags_AnyWindow | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_NoPopupHierarchy)) == 0); // Flags not supported by this function - // Done with rectangle culling so we can perform heavier checks now // Test if we are hovering the right window (our window could be behind another window) // [2021/03/02] Reworked / reverted the revert, finally. Note we want e.g. BeginGroup/ItemAdd/EndGroup to work as well. (#3851) @@ -4182,25 +4484,27 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // Test if another item is active (e.g. being dragged) const ImGuiID id = g.LastItemData.ID; if ((flags & ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) == 0) - if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap && g.ActiveId != window->MoveId) - return false; + if (g.ActiveId != 0 && g.ActiveId != id && !g.ActiveIdAllowOverlap) + if (g.ActiveId != window->MoveId) + return false; // Test if interactions on this window are blocked by an active popup or modal. // The ImGuiHoveredFlags_AllowWhenBlockedByPopup flag will be tested here. - if (!IsWindowContentHoverable(window, flags) && !(g.LastItemData.InFlags & ImGuiItemFlags_NoWindowHoverableCheck)) + if (!IsWindowContentHoverable(window, flags) && !(g.LastItemData.ItemFlags & ImGuiItemFlags_NoWindowHoverableCheck)) return false; // Test if the item is disabled - if ((g.LastItemData.InFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) + if ((g.LastItemData.ItemFlags & ImGuiItemFlags_Disabled) && !(flags & ImGuiHoveredFlags_AllowWhenDisabled)) return false; // Special handling for calling after Begin() which represent the title bar or tab. - // When the window is skipped/collapsed (SkipItems==true) that last item will never be overwritten so we need to detect the case. + // When the window is skipped/collapsed (SkipItems==true) that last item (always ->MoveId submitted by Begin) + // will never be overwritten so we need to detect the case. if (id == window->MoveId && window->WriteAccessed) return false; // Test if using AllowOverlap and overlapped - if ((g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap) && id != 0) + if ((g.LastItemData.ItemFlags & ImGuiItemFlags_AllowOverlap) && id != 0) if ((flags & ImGuiHoveredFlags_AllowWhenOverlappedByItem) == 0) if (g.HoveredIdPreviousFrame != g.LastItemData.ID) return false; @@ -4211,7 +4515,7 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) const float delay = CalcDelayFromHoveredFlags(flags); if (delay > 0.0f || (flags & ImGuiHoveredFlags_Stationary)) { - ImGuiID hover_delay_id = (g.LastItemData.ID != 0) ? g.LastItemData.ID : window->GetIDFromRectangle(g.LastItemData.Rect); + ImGuiID hover_delay_id = (g.LastItemData.ID != 0) ? g.LastItemData.ID : window->GetIDFromPos(g.LastItemData.Rect.Min); if ((flags & ImGuiHoveredFlags_NoSharedDelay) && (g.HoverItemDelayIdPreviousFrame != hover_delay_id)) g.HoverItemDelayTimer = 0.0f; g.HoverItemDelayId = hover_delay_id; @@ -4233,12 +4537,23 @@ bool ImGui::IsItemHovered(ImGuiHoveredFlags flags) // (this does not rely on LastItemData it can be called from a ButtonBehavior() call not following an ItemAdd() call) // FIXME-LEGACY: the 'ImGuiItemFlags item_flags' parameter was added on 2023-06-28. // If you used this in your legacy/custom widgets code: -// - Commonly: if your ItemHoverable() call comes after an ItemAdd() call: pass 'item_flags = g.LastItemData.InFlags'. +// - Commonly: if your ItemHoverable() call comes after an ItemAdd() call: pass 'item_flags = g.LastItemData.ItemFlags'. // - Rare: otherwise you may pass 'item_flags = 0' (ImGuiItemFlags_None) unless you want to benefit from special behavior handled by ItemHoverable. bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flags) { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; + + // Detect ID conflicts +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + if (id != 0 && g.HoveredIdPreviousFrame == id && (item_flags & ImGuiItemFlags_AllowDuplicateId) == 0) + { + g.HoveredIdPreviousFrameItemCount++; + if (g.DebugDrawIdConflicts == id) + window->DrawList->AddRect(bb.Min - ImVec2(1,1), bb.Max + ImVec2(1,1), IM_COL32(255, 0, 0, 255), 0.0f, ImDrawFlags_None, 2.0f); + } +#endif + if (g.HoveredWindow != window) return false; if (!IsMouseHoveringRect(bb.Min, bb.Max)) @@ -4278,7 +4593,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag // Display shortcut (only works with mouse) // (ImGuiItemStatusFlags_HasShortcut in LastItemData denotes we want a tooltip) - if (id == g.LastItemData.ID && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasShortcut)) + if (id == g.LastItemData.ID && (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HasShortcut) && g.ActiveId != id) if (IsItemHovered(ImGuiHoveredFlags_ForTooltip | ImGuiHoveredFlags_DelayNormal)) SetTooltip("%s", GetKeyChordName(g.LastItemData.Shortcut)); } @@ -4307,7 +4622,7 @@ bool ImGui::ItemHoverable(const ImRect& bb, ImGuiID id, ImGuiItemFlags item_flag } #endif - if (g.NavDisableMouseHover) + if (g.NavHighlightItemUnderNav && (item_flags & ImGuiItemFlags_NoNavDisableMouseHover) == 0) return false; return true; @@ -4332,7 +4647,7 @@ void ImGui::SetLastItemData(ImGuiID item_id, ImGuiItemFlags in_flags, ImGuiItemS { ImGuiContext& g = *GImGui; g.LastItemData.ID = item_id; - g.LastItemData.InFlags = in_flags; + g.LastItemData.ItemFlags = in_flags; g.LastItemData.StatusFlags = item_flags; g.LastItemData.Rect = g.LastItemData.NavRect = item_rect; } @@ -4412,14 +4727,14 @@ void ImGui::DebugAllocHook(ImGuiDebugAllocInfo* info, int frame_count, void* ptr const char* ImGui::GetClipboardText() { ImGuiContext& g = *GImGui; - return g.IO.GetClipboardTextFn ? g.IO.GetClipboardTextFn(g.IO.ClipboardUserData) : ""; + return g.PlatformIO.Platform_GetClipboardTextFn ? g.PlatformIO.Platform_GetClipboardTextFn(&g) : ""; } void ImGui::SetClipboardText(const char* text) { ImGuiContext& g = *GImGui; - if (g.IO.SetClipboardTextFn) - g.IO.SetClipboardTextFn(g.IO.ClipboardUserData, text); + if (g.PlatformIO.Platform_SetClipboardTextFn != NULL) + g.PlatformIO.Platform_SetClipboardTextFn(&g, text); } const char* ImGui::GetVersion() @@ -4433,6 +4748,12 @@ ImGuiIO& ImGui::GetIO() return GImGui->IO; } +ImGuiPlatformIO& ImGui::GetPlatformIO() +{ + IM_ASSERT(GImGui != NULL && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext()?"); + return GImGui->PlatformIO; +} + // Pass this to your backend rendering function! Valid after Render() and until the next call to NewFrame() ImDrawData* ImGui::GetDrawData() { @@ -4510,7 +4831,8 @@ void ImGui::StartMouseMovingWindow(ImGuiWindow* window) ImGuiContext& g = *GImGui; FocusWindow(window); SetActiveID(window->MoveId, window); - g.NavDisableHighlight = true; + if (g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = false; g.ActiveIdClickOffset = g.IO.MouseClickedPos[0] - window->RootWindow->Pos; g.ActiveIdNoClearOnFocusLoss = true; SetActiveIdUsingAllKeyboardKeys(); @@ -4561,12 +4883,13 @@ void ImGui::UpdateMouseMovingWindowNewFrame() } } -// Initiate moving window when clicking on empty space or title bar. +// Initiate focusing and moving window when clicking on empty space or title bar. +// Initiate focusing window when clicking on a disabled item. // Handle left-click and right-click focus. void ImGui::UpdateMouseMovingWindowEndFrame() { ImGuiContext& g = *GImGui; - if (g.ActiveId != 0 || g.HoveredId != 0) + if (g.ActiveId != 0 || (g.HoveredId != 0 && !g.HoveredIdIsDisabled)) return; // Unless we just made a window/popup appear @@ -4587,11 +4910,13 @@ void ImGui::UpdateMouseMovingWindowEndFrame() StartMouseMovingWindow(g.HoveredWindow); //-V595 // Cancel moving if clicked outside of title bar - if (g.IO.ConfigWindowsMoveFromTitleBarOnly && !(root_window->Flags & ImGuiWindowFlags_NoTitleBar)) - if (!root_window->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) - g.MovingWindow = NULL; + if (g.IO.ConfigWindowsMoveFromTitleBarOnly) + if (!(root_window->Flags & ImGuiWindowFlags_NoTitleBar)) + if (!root_window->TitleBarRect().Contains(g.IO.MouseClickedPos[0])) + g.MovingWindow = NULL; - // Cancel moving if clicked over an item which was disabled or inhibited by popups (note that we know HoveredId == 0 already) + // Cancel moving if clicked over an item which was disabled or inhibited by popups + // (when g.HoveredIdIsDisabled == true && g.HoveredId == 0 we are inhibited by popups, when g.HoveredIdIsDisabled == true && g.HoveredId != 0 we are over a disabled item)0 already) if (g.HoveredIdIsDisabled) g.MovingWindow = NULL; } @@ -4605,7 +4930,7 @@ void ImGui::UpdateMouseMovingWindowEndFrame() // With right mouse button we close popups without changing focus based on where the mouse is aimed // Instead, focus will be restored to the window under the bottom-most closed popup. // (The left mouse button path calls FocusWindow on the hovered window, which will lead NewFrame->ClosePopupsOverWindow to trigger) - if (g.IO.MouseClicked[1]) + if (g.IO.MouseClicked[1] && g.HoveredId == 0) { // Find the top-most window between HoveredWindow and the top-most Modal Window. // This is where we can trim the popup stack. @@ -4690,9 +5015,14 @@ void ImGui::UpdateHoveredWindowAndCaptureFlags() } // Update io.WantCaptureKeyboard for the user application (true = dispatch keyboard info to Dear ImGui only, false = dispatch keyboard info to Dear ImGui + underlying app) - io.WantCaptureKeyboard = (g.ActiveId != 0) || (modal_window != NULL); - if (io.NavActive && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && !(io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard)) - io.WantCaptureKeyboard = true; + io.WantCaptureKeyboard = false; + if ((io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) == 0) + { + if ((g.ActiveId != 0) || (modal_window != NULL)) + io.WantCaptureKeyboard = true; + else if (io.NavActive && (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && io.ConfigNavCaptureKeyboard) + io.WantCaptureKeyboard = true; + } if (g.WantCaptureKeyboardNextFrame != -1) // Manual override io.WantCaptureKeyboard = (g.WantCaptureKeyboardNextFrame != 0); @@ -4775,6 +5105,12 @@ void ImGui::NewFrame() if (g.DragDropActive && g.DragDropPayload.SourceId == g.ActiveId) KeepAliveID(g.DragDropPayload.SourceId); + // [DEBUG] + if (!g.IO.ConfigDebugHighlightIdConflicts || !g.IO.KeyCtrl) // Count is locked while holding CTRL + g.DebugDrawIdConflicts = 0; + if (g.IO.ConfigDebugHighlightIdConflicts && g.HoveredIdPreviousFrameItemCount > 1) + g.DebugDrawIdConflicts = g.HoveredIdPreviousFrame; + // Update HoveredId data if (!g.HoveredIdPreviousFrame) g.HoveredIdTimer = 0.0f; @@ -4785,6 +5121,7 @@ void ImGui::NewFrame() if (g.HoveredId && g.ActiveId != g.HoveredId) g.HoveredIdNotActiveTimer += g.IO.DeltaTime; g.HoveredIdPreviousFrame = g.HoveredId; + g.HoveredIdPreviousFrameItemCount = 0; g.HoveredId = 0; g.HoveredIdAllowOverlap = false; g.HoveredIdIsDisabled = false; @@ -4853,6 +5190,7 @@ void ImGui::NewFrame() g.DragDropWithinSource = false; g.DragDropWithinTarget = false; g.DragDropHoldJustPressedId = 0; + g.TooltipPreviousWindow = NULL; // Close popups on focus lost (currently wip/opt-in) //if (g.IO.AppFocusLost) @@ -4866,14 +5204,31 @@ void ImGui::NewFrame() //IM_ASSERT(g.IO.KeyAlt == IsKeyDown(ImGuiKey_LeftAlt) || IsKeyDown(ImGuiKey_RightAlt)); //IM_ASSERT(g.IO.KeySuper == IsKeyDown(ImGuiKey_LeftSuper) || IsKeyDown(ImGuiKey_RightSuper)); - // Update gamepad/keyboard navigation + // Update keyboard/gamepad navigation NavUpdate(); // Update mouse input state UpdateMouseInputs(); + // Mark all windows as not visible and compact unused memory. + IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size); + const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; + for (ImGuiWindow* window : g.Windows) + { + window->WasActive = window->Active; + window->Active = false; + window->WriteAccessed = false; + window->BeginCountPreviousFrame = window->BeginCount; + window->BeginCount = 0; + + // Garbage collect transient buffers of recently unused windows + if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) + GcCompactTransientWindowBuffers(window); + } + // Find hovered window // (needs to be before UpdateMouseMovingWindowNewFrame so we fill g.HoveredWindowUnderMovingWindow on the mouse release frame) + // (currently needs to be done after the WasActive=Active loop and FindHoveredWindowEx uses ->Active) UpdateHoveredWindowAndCaptureFlags(); // Handle user moving window with mouse (at the beginning of the frame to avoid input lag or sheering) @@ -4895,22 +5250,6 @@ void ImGui::NewFrame() // Mouse wheel scrolling, scale UpdateMouseWheel(); - // Mark all windows as not visible and compact unused memory. - IM_ASSERT(g.WindowsFocusOrder.Size <= g.Windows.Size); - const float memory_compact_start_time = (g.GcCompactAll || g.IO.ConfigMemoryCompactTimer < 0.0f) ? FLT_MAX : (float)g.Time - g.IO.ConfigMemoryCompactTimer; - for (ImGuiWindow* window : g.Windows) - { - window->WasActive = window->Active; - window->Active = false; - window->WriteAccessed = false; - window->BeginCountPreviousFrame = window->BeginCount; - window->BeginCount = 0; - - // Garbage collect transient buffers of recently unused windows - if (!window->WasActive && !window->MemoryCompacted && window->LastTimeActive < memory_compact_start_time) - GcCompactTransientWindowBuffers(window); - } - // Garbage collect transient buffers of recently unused tables for (int i = 0; i < g.TablesLastTimeActive.Size; i++) if (g.TablesLastTimeActive[i] >= 0.0f && g.TablesLastTimeActive[i] < memory_compact_start_time) @@ -4961,6 +5300,10 @@ void ImGui::NewFrame() Begin("Debug##Default"); IM_ASSERT(g.CurrentWindow->IsFallbackWindow == true); + // Store stack sizes + g.ErrorCountCurrentFrame = 0; + ErrorRecoveryStoreState(&g.StackSizesInNewFrame); + // [DEBUG] When io.ConfigDebugBeginReturnValue is set, we make Begin()/BeginChild() return false at different level of the window-stack, // allowing to validate correct Begin/End behavior in user code. #ifndef IMGUI_DISABLE_DEBUG_TOOLS @@ -5068,6 +5411,8 @@ static void InitViewportDrawData(ImGuiViewportP* viewport) // - If the code here changes, may need to update code of functions like NextColumn() and PushColumnClipRect(): // some frequently called functions which to modify both channels and clipping simultaneously tend to use the // more specialized SetWindowClipRectBeforeSetChannel() to avoid extraneous updates of underlying ImDrawCmds. +// - This is analoguous to PushFont()/PopFont() in the sense that are a mixing a global stack and a window stack, +// which in the case of ClipRect is not so problematic but tends to be more restrictive for fonts. void ImGui::PushClipRect(const ImVec2& clip_rect_min, const ImVec2& clip_rect_max, bool intersect_with_current_clip_rect) { ImGuiWindow* window = GetCurrentWindow(); @@ -5145,7 +5490,7 @@ static void ImGui::RenderDimmedBackgrounds() } else if (dim_bg_for_window_list) { - // Draw dimming behind CTRL+Tab target window + // Draw dimming behind CTRL+Tab target window and behind CTRL+Tab UI window RenderDimmedBackgroundBehindWindow(g.NavWindowingTargetAnim, GetColorU32(ImGuiCol_NavWindowingDimBg, g.DimBgRatio)); // Draw border around CTRL+Tab target window @@ -5177,15 +5522,19 @@ void ImGui::EndFrame() CallContextHooks(&g, ImGuiContextHookType_EndFramePre); + // [EXPERIMENTAL] Recover from errors + if (g.IO.ConfigErrorRecovery) + ErrorRecoveryTryToRecoverState(&g.StackSizesInNewFrame); ErrorCheckEndFrameSanityChecks(); + ErrorCheckEndFrameFinalizeErrorTooltip(); // Notify Platform/OS when our Input Method Editor cursor has moved (e.g. CJK inputs using Microsoft IME) ImGuiPlatformImeData* ime_data = &g.PlatformImeData; - if (g.IO.PlatformSetImeDataFn != NULL && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) + if (g.PlatformIO.Platform_SetImeDataFn != NULL && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) { - IMGUI_DEBUG_LOG_IO("[io] Calling io.PlatformSetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); + IMGUI_DEBUG_LOG_IO("[io] Calling Platform_SetImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); ImGuiViewport* viewport = GetMainViewport(); - g.IO.PlatformSetImeDataFn(&g, viewport, ime_data); + g.PlatformIO.Platform_SetImeDataFn(&g, viewport, ime_data); } // Hide implicit/fallback "Debug" window if it hasn't been used @@ -5211,7 +5560,7 @@ void ImGui::EndFrame() // in the BeginDragDropSource() block of the dragged item, you can submit them from a safe single spot // (e.g. end of your item loop, or before EndFrame) by reading payload data. // In the typical case, the contents of drag tooltip should be possible to infer solely from payload data. - if (g.DragDropActive && g.DragDropSourceFrameCount < g.FrameCount && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) + if (g.DragDropActive && g.DragDropSourceFrameCount + 1 < g.FrameCount && !(g.DragDropSourceFlags & ImGuiDragDropFlags_SourceNoPreviewTooltip)) { g.DragDropWithinSource = true; SetTooltip("..."); @@ -5270,9 +5619,6 @@ void ImGui::Render() g.IO.MetricsRenderWindows = 0; CallContextHooks(&g, ImGuiContextHookType_RenderPre); - // Draw modal/window whitening backgrounds - RenderDimmedBackgrounds(); - // Add background ImDrawList (for each active viewport) for (ImGuiViewportP* viewport : g.Viewports) { @@ -5281,6 +5627,9 @@ void ImGui::Render() AddDrawListToDrawDataEx(&viewport->DrawDataP, viewport->DrawDataBuilder.Layers[0], GetBackgroundDrawList(viewport)); } + // Draw modal/window whitening backgrounds + RenderDimmedBackgrounds(); + // Add ImDrawList to render ImGuiWindow* windows_to_render_top_most[2]; windows_to_render_top_most[0] = (g.NavWindowingTarget && !(g.NavWindowingTarget->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus)) ? g.NavWindowingTarget->RootWindow : NULL; @@ -5371,7 +5720,7 @@ void ImGui::FindHoveredWindowEx(const ImVec2& pos, bool find_first_and_in_any_vi { ImGuiWindow* window = g.Windows[i]; IM_MSVC_WARNING_SUPPRESS(28182); // [Static Analyzer] Dereferencing NULL pointer. - if (!window->Active || window->Hidden) + if (!window->WasActive || window->Hidden) continue; if (window->Flags & ImGuiWindowFlags_NoMouseInputs) continue; @@ -5448,9 +5797,7 @@ bool ImGui::IsItemDeactivatedAfterEdit() bool ImGui::IsItemFocused() { ImGuiContext& g = *GImGui; - if (g.NavId != g.LastItemData.ID || g.NavId == 0) - return false; - return true; + return g.NavId == g.LastItemData.ID && g.NavId != 0; } // Important: this can be useful but it is NOT equivalent to the behavior of e.g.Button()! @@ -5496,7 +5843,7 @@ bool ImGui::IsAnyItemActive() bool ImGui::IsAnyItemFocused() { ImGuiContext& g = *GImGui; - return g.NavId != 0 && !g.NavDisableHighlight; + return g.NavId != 0 && g.NavCursorVisible; } bool ImGui::IsItemVisible() @@ -5570,7 +5917,7 @@ ImVec2 ImGui::GetItemRectSize() } // Prior to v1.90 2023/10/16, the BeginChild() function took a 'bool border = false' parameter instead of 'ImGuiChildFlags child_flags = 0'. -// ImGuiChildFlags_Border is defined as always == 1 in order to allow old code passing 'true'. Read comments in imgui.h for details! +// ImGuiChildFlags_Borders is defined as always == 1 in order to allow old code passing 'true'. Read comments in imgui.h for details! bool ImGui::BeginChild(const char* str_id, const ImVec2& size_arg, ImGuiChildFlags child_flags, ImGuiWindowFlags window_flags) { ImGuiID id = GetCurrentWindow()->GetID(str_id); @@ -5589,7 +5936,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I IM_ASSERT(id != 0); // Sanity check as it is likely that some user will accidentally pass ImGuiWindowFlags into the ImGuiChildFlags argument. - const ImGuiChildFlags ImGuiChildFlags_SupportedMask_ = ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_FrameStyle | ImGuiChildFlags_NavFlattened; + const ImGuiChildFlags ImGuiChildFlags_SupportedMask_ = ImGuiChildFlags_Borders | ImGuiChildFlags_AlwaysUseWindowPadding | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_FrameStyle | ImGuiChildFlags_NavFlattened; IM_UNUSED(ImGuiChildFlags_SupportedMask_); IM_ASSERT((child_flags & ~ImGuiChildFlags_SupportedMask_) == 0 && "Illegal ImGuiChildFlags value. Did you pass ImGuiWindowFlags values instead of ImGuiChildFlags?"); IM_ASSERT((window_flags & ImGuiWindowFlags_AlwaysAutoResize) == 0 && "Cannot specify ImGuiWindowFlags_AlwaysAutoResize for BeginChild(). Use ImGuiChildFlags_AlwaysAutoResize!"); @@ -5624,22 +5971,39 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I PushStyleVar(ImGuiStyleVar_ChildRounding, g.Style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, g.Style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_WindowPadding, g.Style.FramePadding); - child_flags |= ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysUseWindowPadding; + child_flags |= ImGuiChildFlags_Borders | ImGuiChildFlags_AlwaysUseWindowPadding; window_flags |= ImGuiWindowFlags_NoMove; } - // Forward child flags - g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasChildFlags; - g.NextWindowData.ChildFlags = child_flags; - // Forward size // Important: Begin() has special processing to switch condition to ImGuiCond_FirstUseEver for a given axis when ImGuiChildFlags_ResizeXXX is set. // (the alternative would to store conditional flags per axis, which is possible but more code) const ImVec2 size_avail = GetContentRegionAvail(); const ImVec2 size_default((child_flags & ImGuiChildFlags_AutoResizeX) ? 0.0f : size_avail.x, (child_flags & ImGuiChildFlags_AutoResizeY) ? 0.0f : size_avail.y); - const ImVec2 size = CalcItemSize(size_arg, size_default.x, size_default.y); + ImVec2 size = CalcItemSize(size_arg, size_default.x, size_default.y); + + // A SetNextWindowSize() call always has priority (#8020) + // (since the code in Begin() never supported SizeVal==0.0f aka auto-resize via SetNextWindowSize() call, we don't support it here for now) + // FIXME: We only support ImGuiCond_Always in this path. Supporting other paths would requires to obtain window pointer. + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) != 0 && (g.NextWindowData.SizeCond & ImGuiCond_Always) != 0) + { + if (g.NextWindowData.SizeVal.x > 0.0f) + { + size.x = g.NextWindowData.SizeVal.x; + child_flags &= ~ImGuiChildFlags_ResizeX; + } + if (g.NextWindowData.SizeVal.y > 0.0f) + { + size.y = g.NextWindowData.SizeVal.y; + child_flags &= ~ImGuiChildFlags_ResizeY; + } + } SetNextWindowSize(size); + // Forward child flags + g.NextWindowData.Flags |= ImGuiNextWindowDataFlags_HasChildFlags; + g.NextWindowData.ChildFlags = child_flags; + // Build up name. If you need to append to a same child from multiple location in the ID stack, use BeginChild(ImGuiID id) with a stable value. // FIXME: 2023/11/14: commented out shorted version. We had an issue with multiple ### in child window path names, which the trailing hash helped workaround. // e.g. "ParentName###ParentIdentifier/ChildName###ChildIdentifier" would get hashed incorrectly by ImHashStr(), trailing _%08X somehow fixes it. @@ -5654,7 +6018,7 @@ bool ImGui::BeginChildEx(const char* name, ImGuiID id, const ImVec2& size_arg, I // Set style const float backup_border_size = g.Style.ChildBorderSize; - if ((child_flags & ImGuiChildFlags_Border) == 0) + if ((child_flags & ImGuiChildFlags_Borders) == 0) g.Style.ChildBorderSize = 0.0f; // Begin into window @@ -5711,11 +6075,11 @@ void ImGui::EndChild() if ((child_window->DC.NavLayersActiveMask != 0 || child_window->DC.NavWindowHasScrollY) && !nav_flattened) { ItemAdd(bb, child_window->ChildId); - RenderNavHighlight(bb, child_window->ChildId); + RenderNavCursor(bb, child_window->ChildId); // When browsing a window that has no activable items (scroll only) we keep a highlight on the child (pass g.NavId to trick into always displaying) if (child_window->DC.NavLayersActiveMask == 0 && child_window == g.NavWindow) - RenderNavHighlight(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavHighlightFlags_Compact); + RenderNavCursor(ImRect(bb.Min - ImVec2(2, 2), bb.Max + ImVec2(2, 2)), g.NavId, ImGuiNavRenderCursorFlags_Compact); } else { @@ -6096,7 +6460,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si ButtonBehavior(resize_rect, resize_grip_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_NoNavFocus); //GetForegroundDrawList(window)->AddRect(resize_rect.Min, resize_rect.Max, IM_COL32(255, 255, 0, 255)); if (hovered || held) - g.MouseCursor = (resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE; + SetMouseCursor((resize_grip_n & 1) ? ImGuiMouseCursor_ResizeNESW : ImGuiMouseCursor_ResizeNWSE); if (held && g.IO.MouseDoubleClicked[0]) { @@ -6142,7 +6506,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si if (hovered && g.HoveredIdTimer <= WINDOWS_RESIZE_FROM_EDGES_FEEDBACK_TIMER) hovered = false; if (hovered || held) - g.MouseCursor = (axis == ImGuiAxis_X) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS; + SetMouseCursor((axis == ImGuiAxis_X) ? ImGuiMouseCursor_ResizeEW : ImGuiMouseCursor_ResizeNS); if (held && g.IO.MouseDoubleClicked[0]) { // Double-clicking bottom or right border auto-fit on this axis @@ -6233,7 +6597,7 @@ static int ImGui::UpdateWindowManualResize(ImGuiWindow* window, const ImVec2& si g.NavWindowingAccumDeltaSize += nav_resize_dir * resize_step; g.NavWindowingAccumDeltaSize = ImMax(g.NavWindowingAccumDeltaSize, clamp_rect.Min - window->Pos - window->Size); // We need Pos+Size >= clmap_rect.Min, so Size >= clmap_rect.Min - Pos, so size_delta >= clmap_rect.Min - window->Pos - window->Size g.NavWindowingToggleLayer = false; - g.NavDisableMouseHover = true; + g.NavHighlightItemUnderNav = true; resize_grip_col[0] = GetColorU32(ImGuiCol_ResizeGripActive); ImVec2 accum_floored = ImTrunc(g.NavWindowingAccumDeltaSize); if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) @@ -6333,7 +6697,7 @@ void ImGui::RenderWindowDecorations(ImGuiWindow* window, const ImRect& title_bar // Title bar only const float backup_border_size = style.FrameBorderSize; g.Style.FrameBorderSize = window->WindowBorderSize; - ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && !g.NavDisableHighlight) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed); + ImU32 title_bar_col = GetColorU32((title_bar_is_highlight && g.NavCursorVisible) ? ImGuiCol_TitleBgActive : ImGuiCol_TitleBgCollapsed); RenderFrame(title_bar_rect.Min, title_bar_rect.Max, title_bar_col, true, window_rounding); g.Style.FrameBorderSize = backup_border_size; } @@ -6522,15 +6886,28 @@ void ImGui::UpdateWindowSkipRefresh(ImGuiWindow* window) return; if (window->Hidden) // If was hidden (previous frame) return; - if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnHover) && g.HoveredWindow && window->RootWindow == g.HoveredWindow->RootWindow) - return; - if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnFocus) && g.NavWindow && window->RootWindow == g.NavWindow->RootWindow) - return; + if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnHover) && g.HoveredWindow) + if (window->RootWindow == g.HoveredWindow->RootWindow || IsWindowWithinBeginStackOf(g.HoveredWindow->RootWindow, window)) + return; + if ((g.NextWindowData.RefreshFlagsVal & ImGuiWindowRefreshFlags_RefreshOnFocus) && g.NavWindow) + if (window->RootWindow == g.NavWindow->RootWindow || IsWindowWithinBeginStackOf(g.NavWindow->RootWindow, window)) + return; window->DrawList = NULL; window->SkipRefresh = true; } } +static void SetWindowActiveForSkipRefresh(ImGuiWindow* window) +{ + window->Active = true; + for (ImGuiWindow* child : window->DC.ChildWindows) + if (!child->Hidden) + { + child->Active = child->SkipRefresh = true; + SetWindowActiveForSkipRefresh(child); + } +} + // When a modal popup is open, newly created windows that want focus (i.e. are not popups and do not specify ImGuiWindowFlags_NoFocusOnAppearing) // should be positioned behind that modal window, unless the window was created inside the modal begin-stack. // In case of multiple stacked modals newly created window honors begin stack order and does not go below its own modal parent. @@ -6639,12 +7016,13 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Add to stack g.CurrentWindow = window; - ImGuiWindowStackData window_stack_data; + g.CurrentWindowStack.resize(g.CurrentWindowStack.Size + 1); + ImGuiWindowStackData& window_stack_data = g.CurrentWindowStack.back(); window_stack_data.Window = window; window_stack_data.ParentLastItemDataBackup = g.LastItemData; - window_stack_data.StackSizesOnBegin.SetToContextState(&g); window_stack_data.DisabledOverrideReenable = (flags & ImGuiWindowFlags_Tooltip) && (g.CurrentItemFlags & ImGuiItemFlags_Disabled); - g.CurrentWindowStack.push_back(window_stack_data); + ErrorRecoveryStoreState(&window_stack_data.StackSizesInBegin); + g.StackSizesInBeginForCurrentWindow = &window_stack_data.StackSizesInBegin; if (flags & ImGuiWindowFlags_ChildMenu) g.BeginMenuDepth++; @@ -6832,9 +7210,10 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // At this point we don't have a clipping rectangle setup yet, so we can use the title bar area for hit detection and drawing if (!(flags & ImGuiWindowFlags_NoTitleBar) && !(flags & ImGuiWindowFlags_NoCollapse)) { - // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), so verify that we don't have items over the title bar. + // We don't use a regular button+id to test for double-click on title bar (mostly due to legacy reason, could be fixed), + // so verify that we don't have items over the title bar. ImRect title_bar_rect = window->TitleBarRect(); - if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max)) + if (g.HoveredWindow == window && g.HoveredId == 0 && g.HoveredIdPreviousFrame == 0 && g.ActiveId == 0 && IsMouseHoveringRect(title_bar_rect.Min, title_bar_rect.Max)) if (g.IO.MouseClickedCount[0] == 2 && GetKeyOwner(ImGuiKey_MouseLeft) == ImGuiKeyOwner_NoOwner) window->WantCollapseToggle = true; if (window->WantCollapseToggle) @@ -7051,10 +7430,14 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Affected by window/frame border size. Used by: // - Begin() initial clip rect float top_border_size = (((flags & ImGuiWindowFlags_MenuBar) || !(flags & ImGuiWindowFlags_NoTitleBar)) ? style.FrameBorderSize : window->WindowBorderSize); - window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + window->WindowBorderSize); - window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + top_border_size); - window->InnerClipRect.Max.x = ImFloor(0.5f + window->InnerRect.Max.x - window->WindowBorderSize); - window->InnerClipRect.Max.y = ImFloor(0.5f + window->InnerRect.Max.y - window->WindowBorderSize); + + // Try to match the fact that our border is drawn centered over the window rectangle, rather than inner. + // This is why we do a *0.5f here. We don't currently even technically support large values for WindowBorderSize, + // see e.g #7887 #7888, but may do after we move the window border to become an inner border (and then we can remove the 0.5f here). + window->InnerClipRect.Min.x = ImFloor(0.5f + window->InnerRect.Min.x + window->WindowBorderSize * 0.5f); + window->InnerClipRect.Min.y = ImFloor(0.5f + window->InnerRect.Min.y + top_border_size * 0.5f); + window->InnerClipRect.Max.x = ImFloor(window->InnerRect.Max.x - window->WindowBorderSize * 0.5f); + window->InnerClipRect.Max.y = ImFloor(window->InnerRect.Max.y - window->WindowBorderSize * 0.5f); window->InnerClipRect.ClipWithFull(host_rect); // Default item width. Make it proportional to window size if window manually resizes @@ -7203,6 +7586,9 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) // Clear hit test shape every frame window->HitTestHoleSize.x = window->HitTestHoleSize.y = 0; + if (flags & ImGuiWindowFlags_Tooltip) + g.TooltipPreviousWindow = window; + // Pressing CTRL+C while holding on a window copy its content to the clipboard // This works but 1. doesn't handle multiple Begin/End pairs, 2. recursing into another Begin/End pair - so we need to work that out and add better logging scope. // Maybe we can support CTRL+C on every element? @@ -7233,7 +7619,7 @@ bool ImGui::Begin(const char* name, bool* p_open, ImGuiWindowFlags flags) { // Skip refresh always mark active if (window->SkipRefresh) - window->Active = true; + SetWindowActiveForSkipRefresh(window); // Append SetCurrentWindow(window); @@ -7367,7 +7753,11 @@ void ImGui::End() g.BeginMenuDepth--; if (window->Flags & ImGuiWindowFlags_Popup) g.BeginPopupStack.pop_back(); - window_stack_data.StackSizesOnBegin.CompareWithContextState(&g); + + // Error handling, state recovery + if (g.IO.ConfigErrorRecovery) + ErrorRecoveryTryToRecoverWindowState(&window_stack_data.StackSizesInBegin); + g.CurrentWindowStack.pop_back(); SetCurrentWindow(g.CurrentWindowStack.Size == 0 ? NULL : g.CurrentWindowStack.back().Window); } @@ -7477,7 +7867,7 @@ void ImGui::FocusWindow(ImGuiWindow* window, ImGuiFocusRequestFlags flags) if (g.NavWindow != window) { SetNavWindow(window); - if (window && g.NavDisableMouseHover) + if (window && g.NavHighlightItemUnderNav) g.NavMousePosDirty = true; g.NavId = window ? window->NavLastIds[0] : 0; // Restore NavId g.NavLayer = ImGuiNavLayer_Main; @@ -7561,22 +7951,35 @@ void ImGui::SetCurrentFont(ImFont* font) g.DrawListSharedData.FontScale = g.FontScale; } +// Use ImDrawList::_SetTextureID(), making our shared g.FontStack[] authorative against window-local ImDrawList. +// - Whereas ImDrawList::PushTextureID()/PopTextureID() is not to be used across Begin() calls. +// - Note that we don't propagate current texture id when e.g. Begin()-ing into a new window, we never really did... +// - Some code paths never really fully worked with multiple atlas textures. +// - The right-ish solution may be to remove _SetTextureID() and make AddText/RenderText lazily call PushTextureID()/PopTextureID() +// the same way AddImage() does, but then all other primitives would also need to? I don't think we should tackle this problem +// because we have a concrete need and a test bed for multiple atlas textures. void ImGui::PushFont(ImFont* font) { ImGuiContext& g = *GImGui; - if (!font) + if (font == NULL) font = GetDefaultFont(); - SetCurrentFont(font); g.FontStack.push_back(font); - g.CurrentWindow->DrawList->PushTextureID(font->ContainerAtlas->TexID); + SetCurrentFont(font); + g.CurrentWindow->DrawList->_SetTextureID(font->ContainerAtlas->TexID); } void ImGui::PopFont() { ImGuiContext& g = *GImGui; - g.CurrentWindow->DrawList->PopTextureID(); + if (g.FontStack.Size <= 0) + { + IM_ASSERT_USER_ERROR(0, "Calling PopFont() too many times!"); + return; + } g.FontStack.pop_back(); - SetCurrentFont(g.FontStack.empty() ? GetDefaultFont() : g.FontStack.back()); + ImFont* font = g.FontStack.Size == 0 ? GetDefaultFont() : g.FontStack.back(); + SetCurrentFont(font); + g.CurrentWindow->DrawList->_SetTextureID(font->ContainerAtlas->TexID); } void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) @@ -7595,7 +7998,11 @@ void ImGui::PushItemFlag(ImGuiItemFlags option, bool enabled) void ImGui::PopItemFlag() { ImGuiContext& g = *GImGui; - IM_ASSERT(g.ItemFlagsStack.Size > 1); // Too many calls to PopItemFlag() - we always leave a 0 at the bottom of the stack. + if (g.ItemFlagsStack.Size <= 1) + { + IM_ASSERT_USER_ERROR(0, "Calling PopItemFlag() too many times!"); + return; + } g.ItemFlagsStack.pop_back(); g.CurrentItemFlags = g.ItemFlagsStack.back(); } @@ -7604,8 +8011,9 @@ void ImGui::PopItemFlag() // - Those can be nested but it cannot be used to enable an already disabled section (a single BeginDisabled(true) in the stack is enough to keep everything disabled) // - Visually this is currently altering alpha, but it is expected that in a future styling system this would work differently. // - Feedback welcome at https://github.com/ocornut/imgui/issues/211 -// - BeginDisabled(false) essentially does nothing useful but is provided to facilitate use of boolean expressions. If you can avoid calling BeginDisabled(False)/EndDisabled() best to avoid it. -// - Optimized shortcuts instead of PushStyleVar() + PushItemFlag() +// - BeginDisabled(false)/EndDisabled() essentially does nothing but is provided to facilitate use of boolean expressions. +// (as a micro-optimization: if you have tens of thousands of BeginDisabled(false)/EndDisabled() pairs, you might want to reformulate your code to avoid making those calls) +// - Note: mixing up BeginDisabled() and PushItemFlag(ImGuiItemFlags_Disabled) is currently NOT SUPPORTED. void ImGui::BeginDisabled(bool disabled) { ImGuiContext& g = *GImGui; @@ -7624,7 +8032,11 @@ void ImGui::BeginDisabled(bool disabled) void ImGui::EndDisabled() { ImGuiContext& g = *GImGui; - IM_ASSERT(g.DisabledStackSize > 0); + if (g.DisabledStackSize <= 0) + { + IM_ASSERT_USER_ERROR(0, "Calling EndDisabled() too many times!"); + return; + } g.DisabledStackSize--; bool was_disabled = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0; //PopItemFlag(); @@ -7659,14 +8071,21 @@ void ImGui::EndDisabledOverrideReenable() void ImGui::PushTextWrapPos(float wrap_pos_x) { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; window->DC.TextWrapPosStack.push_back(window->DC.TextWrapPos); window->DC.TextWrapPos = wrap_pos_x; } void ImGui::PopTextWrapPos() { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->DC.TextWrapPosStack.Size <= 0) + { + IM_ASSERT_USER_ERROR(0, "Calling PopTextWrapPos() too many times!"); + return; + } window->DC.TextWrapPos = window->DC.TextWrapPosStack.back(); window->DC.TextWrapPosStack.pop_back(); } @@ -7739,9 +8158,9 @@ bool ImGui::IsWindowAbove(ImGuiWindow* potential_above, ImGuiWindow* potential_b // Refer to FAQ entry "How can I tell whether to dispatch mouse/keyboard to Dear ImGui or my application?" for details. bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags) { - IM_ASSERT((flags & ~ImGuiHoveredFlags_AllowedMaskForIsWindowHovered) == 0 && "Invalid flags for IsWindowHovered()!"); - ImGuiContext& g = *GImGui; + IM_ASSERT_USER_ERROR((flags & ~ImGuiHoveredFlags_AllowedMaskForIsWindowHovered) == 0, "Invalid flags for IsWindowHovered()!"); + ImGuiWindow* ref_window = g.HoveredWindow; ImGuiWindow* cur_window = g.CurrentWindow; if (ref_window == NULL) @@ -8099,9 +8518,9 @@ void ImGui::PushFocusScope(ImGuiID id) void ImGui::PopFocusScope() { ImGuiContext& g = *GImGui; - if (g.FocusScopeStack.Size == 0) + if (g.FocusScopeStack.Size <= g.StackSizesInBeginForCurrentWindow->SizeOfFocusScopeStack) { - IM_ASSERT_USER_ERROR(g.FocusScopeStack.Size > 0, "Calling PopFocusScope() too many times!"); + IM_ASSERT_USER_ERROR(0, "Calling PopFocusScope() too many times!"); return; } g.FocusScopeStack.pop_back(); @@ -8147,7 +8566,7 @@ void ImGui::FocusItem() return; } - ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavHighlight | ImGuiNavMoveFlags_NoSelect; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavCursorVisible | ImGuiNavMoveFlags_NoSelect; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; SetNavWindow(window); NavMoveRequestSubmit(ImGuiDir_None, ImGuiDir_Up, move_flags, scroll_flags); @@ -8182,7 +8601,7 @@ void ImGui::SetKeyboardFocusHere(int offset) SetNavWindow(window); - ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_Activate | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavHighlight; + ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_Activate | ImGuiNavMoveFlags_FocusApi | ImGuiNavMoveFlags_NoSetNavCursorVisible; ImGuiScrollFlags scroll_flags = window->Appearing ? ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_AlwaysCenterY : ImGuiScrollFlags_KeepVisibleEdgeX | ImGuiScrollFlags_KeepVisibleEdgeY; NavMoveRequestSubmit(ImGuiDir_None, offset < 0 ? ImGuiDir_Up : ImGuiDir_Down, move_flags, scroll_flags); // FIXME-NAV: Once we refactor tabbing, add LegacyApi flag to not activate non-inputable. if (offset == -1) @@ -8282,6 +8701,16 @@ ImGuiID ImGuiWindow::GetID(int n) } // This is only used in rare/specific situations to manufacture an ID out of nowhere. +// FIXME: Consider instead storing last non-zero ID + count of successive zero-ID, and combine those? +ImGuiID ImGuiWindow::GetIDFromPos(const ImVec2& p_abs) +{ + ImGuiID seed = IDStack.back(); + ImVec2 p_rel = ImGui::WindowPosAbsToRel(this, p_abs); + ImGuiID id = ImHashData(&p_rel, sizeof(p_rel), seed); + return id; +} + +// " ImGuiID ImGuiWindow::GetIDFromRectangle(const ImRect& r_abs) { ImGuiID seed = IDStack.back(); @@ -8362,7 +8791,11 @@ ImGuiID ImGui::GetIDWithSeed(int n, ImGuiID seed) void ImGui::PopID() { ImGuiWindow* window = GImGui->CurrentWindow; - IM_ASSERT(window->IDStack.Size > 1); // Too many PopID(), or could be popping in a wrong/different window? + if (window->IDStack.Size <= 1) + { + IM_ASSERT_USER_ERROR(0, "Calling PopID() too many times!"); + return; + } window->IDStack.pop_back(); } @@ -8394,7 +8827,7 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE //----------------------------------------------------------------------------- // [SECTION] INPUTS //----------------------------------------------------------------------------- -// - GetModForModKey() [Internal] +// - GetModForLRModKey() [Internal] // - FixupKeyChord() [Internal] // - GetKeyData() [Internal] // - GetKeyIndex() [Internal] @@ -8457,7 +8890,7 @@ IM_MSVC_RUNTIME_CHECKS_RESTORE // - Shortcut() [Internal] //----------------------------------------------------------------------------- -static ImGuiKeyChord GetModForModKey(ImGuiKey key) +static ImGuiKeyChord GetModForLRModKey(ImGuiKey key) { if (key == ImGuiKey_LeftCtrl || key == ImGuiKey_RightCtrl) return ImGuiMod_Ctrl; @@ -8474,8 +8907,8 @@ ImGuiKeyChord ImGui::FixupKeyChord(ImGuiKeyChord key_chord) { // Add ImGuiMod_XXXX when a corresponding ImGuiKey_LeftXXX/ImGuiKey_RightXXX is specified. ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); - if (IsModKey(key)) - key_chord |= GetModForModKey(key); + if (IsLRModKey(key)) + key_chord |= GetModForLRModKey(key); return key_chord; } @@ -8566,8 +8999,8 @@ const char* ImGui::GetKeyChordName(ImGuiKeyChord key_chord) ImGuiContext& g = *GImGui; const ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); - if (IsModKey(key)) - key_chord &= ~GetModForModKey(key); // Return "Ctrl+LeftShift" instead of "Ctrl+Shift+LeftShift" + if (IsLRModKey(key)) + key_chord &= ~GetModForLRModKey(key); // Return "Ctrl+LeftShift" instead of "Ctrl+Shift+LeftShift" ImFormatString(g.TempKeychordName, IM_ARRAYSIZE(g.TempKeychordName), "%s%s%s%s%s", (key_chord & ImGuiMod_Ctrl) ? "Ctrl+" : "", (key_chord & ImGuiMod_Shift) ? "Shift+" : "", @@ -8767,8 +9200,9 @@ static int CalcRoutingScore(ImGuiID focus_scope_id, ImGuiID owner_id, ImGuiInput return 0; } -// We need this to filter some Shortcut() routes when an item e.g. an InputText() is active -// e.g. ImGuiKey_G won't be considered a shortcut when item is active, but ImGuiMod|ImGuiKey_G can be. +// - We need this to filter some Shortcut() routes when an item e.g. an InputText() is active +// e.g. ImGuiKey_G won't be considered a shortcut when item is active, but ImGuiMod|ImGuiKey_G can be. +// - This is also used by UpdateInputEvents() to avoid trickling in the most common case of e.g. pressing ImGuiKey_G also emitting a G character. static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord) { // Mimic 'ignore_char_inputs' logic in InputText() @@ -8782,6 +9216,8 @@ static bool IsKeyChordPotentiallyCharInput(ImGuiKeyChord key_chord) // Return true for A-Z, 0-9 and other keys associated to char inputs. Other keys such as F1-F12 won't be filtered. ImGuiKey key = (ImGuiKey)(key_chord & ~ImGuiMod_Mask_); + if (key == ImGuiKey_None) + return false; return g.KeysMayBeCharInput.TestBit(key); } @@ -8905,7 +9341,7 @@ bool ImGui::IsKeyPressed(ImGuiKey key, bool repeat) return IsKeyPressed(key, repeat ? ImGuiInputFlags_Repeat : ImGuiInputFlags_None, ImGuiKeyOwner_Any); } -// Important: unless legacy IsKeyPressed(ImGuiKey, bool repeat=true) which DEFAULT to repeat, this requires EXPLICIT repeat. +// Important: unlike legacy IsKeyPressed(ImGuiKey, bool repeat=true) which DEFAULT to repeat, this requires EXPLICIT repeat. bool ImGui::IsKeyPressed(ImGuiKey key, ImGuiInputFlags flags, ImGuiID owner_id) { const ImGuiKeyData* key_data = GetKeyData(key); @@ -9155,6 +9591,9 @@ ImGuiMouseCursor ImGui::GetMouseCursor() return g.MouseCursor; } +// We intentionally accept values of ImGuiMouseCursor that are outside our bounds, in case users needs to hack-in a custom cursor value. +// Custom cursors may be handled by custom backends. If you are using a standard backend and want to hack in a custom cursor, you may +// handle it before the backend _NewFrame() call and temporarily set ImGuiConfigFlags_NoMouseCursorChange during the backend _NewFrame() call. void ImGui::SetMouseCursor(ImGuiMouseCursor cursor_type) { ImGuiContext& g = *GImGui; @@ -9345,9 +9784,9 @@ static void ImGui::UpdateMouseInputs() g.MouseStationaryTimer = mouse_stationary ? (g.MouseStationaryTimer + io.DeltaTime) : 0.0f; //IMGUI_DEBUG_LOG("%.4f\n", g.MouseStationaryTimer); - // If mouse moved we re-enable mouse hovering in case it was disabled by gamepad/keyboard. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. + // If mouse moved we re-enable mouse hovering in case it was disabled by keyboard/gamepad. In theory should use a >0.0f threshold but would need to reset in everywhere we set this to true. if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) - g.NavDisableMouseHover = false; + g.NavHighlightItemUnderNav = false; for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++) { @@ -9384,9 +9823,9 @@ static void ImGui::UpdateMouseInputs() // We provide io.MouseDoubleClicked[] as a legacy service io.MouseDoubleClicked[i] = (io.MouseClickedCount[i] == 2); - // Clicking any mouse button reactivate mouse hovering which may have been deactivated by gamepad/keyboard navigation + // Clicking any mouse button reactivate mouse hovering which may have been deactivated by keyboard/gamepad navigation if (io.MouseClicked[i]) - g.NavDisableMouseHover = false; + g.NavHighlightItemUnderNav = false; } } @@ -9586,9 +10025,9 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) // Only trickle chars<>key when working with InputText() // FIXME: InputText() could parse event trail? // FIXME: Could specialize chars<>keys trickling rules for control keys (those not typically associated to characters) - const bool trickle_interleaved_keys_and_text = (trickle_fast_inputs && g.WantTextInputNextFrame == 1); + const bool trickle_interleaved_nonchar_keys_and_text = (trickle_fast_inputs && g.WantTextInputNextFrame == 1); - bool mouse_moved = false, mouse_wheeled = false, key_changed = false, text_inputted = false; + bool mouse_moved = false, mouse_wheeled = false, key_changed = false, key_changed_nonchar = false, text_inputted = false; int mouse_button_changed = 0x00; ImBitArray key_changed_mask; @@ -9640,12 +10079,19 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) IM_ASSERT(key != ImGuiKey_None); ImGuiKeyData* key_data = GetKeyData(key); const int key_data_index = (int)(key_data - g.IO.KeysData); - if (trickle_fast_inputs && key_data->Down != e->Key.Down && (key_changed_mask.TestBit(key_data_index) || text_inputted || mouse_button_changed != 0)) + if (trickle_fast_inputs && key_data->Down != e->Key.Down && (key_changed_mask.TestBit(key_data_index) || mouse_button_changed != 0)) + break; + + const bool key_is_potentially_for_char_input = IsKeyChordPotentiallyCharInput(GetMergedModsFromKeys() | key); + if (trickle_interleaved_nonchar_keys_and_text && (text_inputted && !key_is_potentially_for_char_input)) break; + key_data->Down = e->Key.Down; key_data->AnalogValue = e->Key.AnalogValue; key_changed = true; key_changed_mask.SetBit(key_data_index); + if (trickle_interleaved_nonchar_keys_and_text && !key_is_potentially_for_char_input) + key_changed_nonchar = true; // Allow legacy code using io.KeysDown[GetKeyIndex()] with new backends #ifndef IMGUI_DISABLE_OBSOLETE_KEYIO @@ -9659,11 +10105,13 @@ void ImGui::UpdateInputEvents(bool trickle_fast_inputs) if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) continue; // Trickling Rule: Stop processing queued events if keys/mouse have been interacted with - if (trickle_fast_inputs && ((key_changed && trickle_interleaved_keys_and_text) || mouse_button_changed != 0 || mouse_moved || mouse_wheeled)) + if (trickle_fast_inputs && (mouse_button_changed != 0 || mouse_moved || mouse_wheeled)) + break; + if (trickle_interleaved_nonchar_keys_and_text && key_changed_nonchar) break; unsigned int c = e->Text.Char; io.InputQueueCharacters.push_back(c <= IM_UNICODE_CODEPOINT_MAX ? (ImWchar)c : IM_UNICODE_CODEPOINT_INVALID); - if (trickle_interleaved_keys_and_text) + if (trickle_interleaved_nonchar_keys_and_text) text_inputted = true; } else if (e->Type == ImGuiInputEventType_Focus) @@ -9840,7 +10288,7 @@ bool ImGui::IsKeyChordPressed(ImGuiKeyChord key_chord, ImGuiInputFlags flags, Im void ImGui::SetNextItemShortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags) { ImGuiContext& g = *GImGui; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasShortcut; + g.NextItemData.HasFlags |= ImGuiNextItemDataFlags_HasShortcut; g.NextItemData.Shortcut = key_chord; g.NextItemData.ShortcutFlags = flags; } @@ -9852,7 +10300,7 @@ void ImGui::ItemHandleShortcut(ImGuiID id) ImGuiInputFlags flags = g.NextItemData.ShortcutFlags; IM_ASSERT((flags & ~ImGuiInputFlags_SupportedBySetNextItemShortcut) == 0); // Passing flags not supported by SetNextItemShortcut()! - if (g.LastItemData.InFlags & ImGuiItemFlags_Disabled) + if (g.LastItemData.ItemFlags & ImGuiItemFlags_Disabled) return; if (flags & ImGuiInputFlags_Tooltip) { @@ -9913,7 +10361,16 @@ bool ImGui::Shortcut(ImGuiKeyChord key_chord, ImGuiInputFlags flags, ImGuiID own //----------------------------------------------------------------------------- -// [SECTION] ERROR CHECKING +// [SECTION] ERROR CHECKING, STATE RECOVERY +//----------------------------------------------------------------------------- +// - DebugCheckVersionAndDataLayout() (called via IMGUI_CHECKVERSION() macros) +// - ErrorCheckUsingSetCursorPosToExtendParentBoundaries() +// - ErrorCheckNewFrameSanityChecks() +// - ErrorCheckEndFrameSanityChecks() +// - ErrorRecoveryStoreState() +// - ErrorRecoveryTryToRecoverState() +// - ErrorRecoveryTryToRecoverWindowState() +// - ErrorLog() //----------------------------------------------------------------------------- // Verify ABI compatibility between caller code and compiled version of Dear ImGui. This helps detects some build issues. @@ -10011,118 +10468,143 @@ static void ImGui::ErrorCheckNewFrameSanityChecks() if ((g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) && g.IO.BackendUsingLegacyKeyArrays == 1) IM_ASSERT(g.IO.KeyMap[ImGuiKey_Space] != -1 && "ImGuiKey_Space is not mapped, required for keyboard navigation."); #endif + + // Error handling: we do not accept 100% silent recovery! Please contact me if you feel this is getting in your way. + if (g.IO.ConfigErrorRecovery) + IM_ASSERT(g.IO.ConfigErrorRecoveryEnableAssert || g.IO.ConfigErrorRecoveryEnableDebugLog || g.IO.ConfigErrorRecoveryEnableTooltip || g.ErrorCallback != NULL); + +#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS + // Remap legacy names + if (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) + { + g.IO.ConfigNavMoveSetMousePos = true; + g.IO.ConfigFlags &= ~ImGuiConfigFlags_NavEnableSetMousePos; + } + if (g.IO.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard) + { + g.IO.ConfigNavCaptureKeyboard = false; + g.IO.ConfigFlags &= ~ImGuiConfigFlags_NavNoCaptureKeyboard; + } + + // Remap legacy clipboard handlers (OBSOLETED in 1.91.1, August 2024) + if (g.IO.GetClipboardTextFn != NULL && (g.PlatformIO.Platform_GetClipboardTextFn == NULL || g.PlatformIO.Platform_GetClipboardTextFn == Platform_GetClipboardTextFn_DefaultImpl)) + g.PlatformIO.Platform_GetClipboardTextFn = [](ImGuiContext* ctx) { return ctx->IO.GetClipboardTextFn(ctx->IO.ClipboardUserData); }; + if (g.IO.SetClipboardTextFn != NULL && (g.PlatformIO.Platform_SetClipboardTextFn == NULL || g.PlatformIO.Platform_SetClipboardTextFn == Platform_SetClipboardTextFn_DefaultImpl)) + g.PlatformIO.Platform_SetClipboardTextFn = [](ImGuiContext* ctx, const char* text) { return ctx->IO.SetClipboardTextFn(ctx->IO.ClipboardUserData, text); }; +#endif } static void ImGui::ErrorCheckEndFrameSanityChecks() { - ImGuiContext& g = *GImGui; - // Verify that io.KeyXXX fields haven't been tampered with. Key mods should not be modified between NewFrame() and EndFrame() // One possible reason leading to this assert is that your backends update inputs _AFTER_ NewFrame(). // It is known that when some modal native windows called mid-frame takes focus away, some backends such as GLFW will // send key release events mid-frame. This would normally trigger this assertion and lead to sheared inputs. // We silently accommodate for this case by ignoring the case where all io.KeyXXX modifiers were released (aka key_mod_flags == 0), // while still correctly asserting on mid-frame key press events. + ImGuiContext& g = *GImGui; const ImGuiKeyChord key_mods = GetMergedModsFromKeys(); + IM_UNUSED(g); + IM_UNUSED(key_mods); IM_ASSERT((key_mods == 0 || g.IO.KeyMods == key_mods) && "Mismatching io.KeyCtrl/io.KeyShift/io.KeyAlt/io.KeySuper vs io.KeyMods"); IM_UNUSED(key_mods); - // [EXPERIMENTAL] Recover from errors: You may call this yourself before EndFrame(). - //ErrorCheckEndFrameRecover(); - - // Report when there is a mismatch of Begin/BeginChild vs End/EndChild calls. Important: Remember that the Begin/BeginChild API requires you - // to always call End/EndChild even if Begin/BeginChild returns false! (this is unfortunately inconsistent with most other Begin* API). - if (g.CurrentWindowStack.Size != 1) - { - if (g.CurrentWindowStack.Size > 1) - { - ImGuiWindow* window = g.CurrentWindowStack.back().Window; // <-- This window was not Ended! - IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you forget to call End/EndChild?"); - IM_UNUSED(window); - while (g.CurrentWindowStack.Size > 1) - End(); - } - else - { - IM_ASSERT_USER_ERROR(g.CurrentWindowStack.Size == 1, "Mismatched Begin/BeginChild vs End/EndChild calls: did you call End/EndChild too much?"); - } - } + IM_ASSERT(g.CurrentWindowStack.Size == 1); + IM_ASSERT(g.CurrentWindowStack[0].Window->IsFallbackWindow); +} - IM_ASSERT_USER_ERROR(g.GroupStack.Size == 0, "Missing EndGroup call!"); +// Save current stack sizes. Called e.g. by NewFrame() and by Begin() but may be called for manual recovery. +void ImGui::ErrorRecoveryStoreState(ImGuiErrorRecoveryState* state_out) +{ + ImGuiContext& g = *GImGui; + state_out->SizeOfWindowStack = (short)g.CurrentWindowStack.Size; + state_out->SizeOfIDStack = (short)g.CurrentWindow->IDStack.Size; + state_out->SizeOfTreeStack = (short)g.CurrentWindow->DC.TreeDepth; // NOT g.TreeNodeStack.Size which is a partial stack! + state_out->SizeOfColorStack = (short)g.ColorStack.Size; + state_out->SizeOfStyleVarStack = (short)g.StyleVarStack.Size; + state_out->SizeOfFontStack = (short)g.FontStack.Size; + state_out->SizeOfFocusScopeStack = (short)g.FocusScopeStack.Size; + state_out->SizeOfGroupStack = (short)g.GroupStack.Size; + state_out->SizeOfItemFlagsStack = (short)g.ItemFlagsStack.Size; + state_out->SizeOfBeginPopupStack = (short)g.BeginPopupStack.Size; + state_out->SizeOfDisabledStack = (short)g.DisabledStackSize; } -// Experimental recovery from incorrect usage of BeginXXX/EndXXX/PushXXX/PopXXX calls. -// Must be called during or before EndFrame(). -// This is generally flawed as we are not necessarily End/Popping things in the right order. -// FIXME: Can't recover from inside BeginTabItem/EndTabItem yet. -// FIXME: Can't recover from interleaved BeginTabBar/Begin -void ImGui::ErrorCheckEndFrameRecover(ImGuiErrorLogCallback log_callback, void* user_data) +// Chosen name "Try to recover" over e.g. "Restore" to suggest this is not a 100% guaranteed recovery. +// Called by e.g. EndFrame() but may be called for manual recovery. +// Attempt to recover full window stack. +void ImGui::ErrorRecoveryTryToRecoverState(const ImGuiErrorRecoveryState* state_in) { // PVS-Studio V1044 is "Loop break conditions do not depend on the number of iterations" ImGuiContext& g = *GImGui; - while (g.CurrentWindowStack.Size > 0) //-V1044 + while (g.CurrentWindowStack.Size > state_in->SizeOfWindowStack) //-V1044 { - ErrorCheckEndWindowRecover(log_callback, user_data); + // Recap: + // - Begin()/BeginChild() return false to indicate the window is collapsed or fully clipped. + // - Always call a matching End() for each Begin() call, regardless of its return value! + // - Begin/End and BeginChild/EndChild logic is KNOWN TO BE INCONSISTENT WITH ALL OTHER BEGIN/END FUNCTIONS. + // - We will fix that in a future major update. ImGuiWindow* window = g.CurrentWindow; - if (g.CurrentWindowStack.Size == 1) - { - IM_ASSERT(window->IsFallbackWindow); - break; - } if (window->Flags & ImGuiWindowFlags_ChildWindow) { - if (log_callback) log_callback(user_data, "Recovered from missing EndChild() for '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndChild()"); EndChild(); } else { - if (log_callback) log_callback(user_data, "Recovered from missing End() for '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing End()"); End(); } } + if (g.CurrentWindowStack.Size == state_in->SizeOfWindowStack) + ErrorRecoveryTryToRecoverWindowState(state_in); } -// Must be called before End()/EndChild() -void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, void* user_data) +// Called by e.g. End() but may be called for manual recovery. +// Read '// Error Handling [BETA]' block in imgui_internal.h for details. +// Attempt to recover from incorrect usage of BeginXXX/EndXXX/PushXXX/PopXXX calls. +void ImGui::ErrorRecoveryTryToRecoverWindowState(const ImGuiErrorRecoveryState* state_in) { ImGuiContext& g = *GImGui; - while (g.CurrentTable && (g.CurrentTable->OuterWindow == g.CurrentWindow || g.CurrentTable->InnerWindow == g.CurrentWindow)) + + while (g.CurrentTable != NULL && g.CurrentTable->InnerWindow == g.CurrentWindow) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing EndTable() in '%s'", g.CurrentTable->OuterWindow->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndTable()"); EndTable(); } ImGuiWindow* window = g.CurrentWindow; - ImGuiStackSizes* stack_sizes = &g.CurrentWindowStack.back().StackSizesOnBegin; - IM_ASSERT(window != NULL); - while (g.CurrentTabBar != NULL) //-V1044 + + // FIXME: Can't recover from inside BeginTabItem/EndTabItem yet. + while (g.CurrentTabBar != NULL && g.CurrentTabBar->Window == window) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing EndTabBar() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndTabBar()"); EndTabBar(); } - while (g.CurrentMultiSelect != NULL && g.CurrentMultiSelect->Storage->Window == window) + while (g.CurrentMultiSelect != NULL && g.CurrentMultiSelect->Storage->Window == window) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing EndMultiSelect() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndMultiSelect()"); EndMultiSelect(); } - while (window->DC.TreeDepth > 0) + while (window->DC.TreeDepth > state_in->SizeOfTreeStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing TreePop() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing TreePop()"); TreePop(); } - while (g.GroupStack.Size > stack_sizes->SizeOfGroupStack) //-V1044 + while (g.GroupStack.Size > state_in->SizeOfGroupStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing EndGroup() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndGroup()"); EndGroup(); } - while (window->IDStack.Size > 1) + IM_ASSERT(g.GroupStack.Size == state_in->SizeOfGroupStack); + while (window->IDStack.Size > state_in->SizeOfIDStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopID() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing PopID()"); PopID(); } - while (g.DisabledStackSize > stack_sizes->SizeOfDisabledStack) //-V1044 + while (g.DisabledStackSize > state_in->SizeOfDisabledStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing EndDisabled() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing EndDisabled()"); if (g.CurrentItemFlags & ImGuiItemFlags_Disabled) EndDisabled(); else @@ -10131,70 +10613,151 @@ void ImGui::ErrorCheckEndWindowRecover(ImGuiErrorLogCallback log_callback, vo g.CurrentWindowStack.back().DisabledOverrideReenable = false; } } - while (g.ColorStack.Size > stack_sizes->SizeOfColorStack) + IM_ASSERT(g.DisabledStackSize == state_in->SizeOfDisabledStack); + while (g.ColorStack.Size > state_in->SizeOfColorStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopStyleColor() in '%s' for ImGuiCol_%s", window->Name, GetStyleColorName(g.ColorStack.back().Col)); + IM_ASSERT_USER_ERROR(0, "Missing PopStyleColor()"); PopStyleColor(); } - while (g.ItemFlagsStack.Size > stack_sizes->SizeOfItemFlagsStack) //-V1044 + while (g.ItemFlagsStack.Size > state_in->SizeOfItemFlagsStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopItemFlag() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing PopItemFlag()"); PopItemFlag(); } - while (g.StyleVarStack.Size > stack_sizes->SizeOfStyleVarStack) //-V1044 + while (g.StyleVarStack.Size > state_in->SizeOfStyleVarStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopStyleVar() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing PopStyleVar()"); PopStyleVar(); } - while (g.FontStack.Size > stack_sizes->SizeOfFontStack) //-V1044 + while (g.FontStack.Size > state_in->SizeOfFontStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopFont() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing PopFont()"); PopFont(); } - while (g.FocusScopeStack.Size > stack_sizes->SizeOfFocusScopeStack + 1) //-V1044 + while (g.FocusScopeStack.Size > state_in->SizeOfFocusScopeStack) //-V1044 { - if (log_callback) log_callback(user_data, "Recovered from missing PopFocusScope() in '%s'", window->Name); + IM_ASSERT_USER_ERROR(0, "Missing PopFocusScope()"); PopFocusScope(); } + //IM_ASSERT(g.FocusScopeStack.Size == state_in->SizeOfFocusScopeStack); } -// Save current stack sizes for later compare -void ImGuiStackSizes::SetToContextState(ImGuiContext* ctx) +bool ImGui::ErrorLog(const char* msg) { - ImGuiContext& g = *ctx; + ImGuiContext& g = *GImGui; + + // Output to debug log +#ifndef IMGUI_DISABLE_DEBUG_TOOLS ImGuiWindow* window = g.CurrentWindow; - SizeOfIDStack = (short)window->IDStack.Size; - SizeOfColorStack = (short)g.ColorStack.Size; - SizeOfStyleVarStack = (short)g.StyleVarStack.Size; - SizeOfFontStack = (short)g.FontStack.Size; - SizeOfFocusScopeStack = (short)g.FocusScopeStack.Size; - SizeOfGroupStack = (short)g.GroupStack.Size; - SizeOfItemFlagsStack = (short)g.ItemFlagsStack.Size; - SizeOfBeginPopupStack = (short)g.BeginPopupStack.Size; - SizeOfDisabledStack = (short)g.DisabledStackSize; + + if (g.IO.ConfigErrorRecoveryEnableDebugLog) + { + if (g.ErrorFirst) + IMGUI_DEBUG_LOG_ERROR("[imgui-error] (current settings: Assert=%d, Log=%d, Tooltip=%d)\n", + g.IO.ConfigErrorRecoveryEnableAssert, g.IO.ConfigErrorRecoveryEnableDebugLog, g.IO.ConfigErrorRecoveryEnableTooltip); + IMGUI_DEBUG_LOG_ERROR("[imgui-error] In window '%s': %s\n", window ? window->Name : "NULL", msg); + } + g.ErrorFirst = false; + + // Output to tooltip + if (g.IO.ConfigErrorRecoveryEnableTooltip) + { + if (BeginErrorTooltip()) + { + if (g.ErrorCountCurrentFrame < 20) + { + Text("In window '%s': %s", window ? window->Name : "NULL", msg); + if (window && (!window->IsFallbackWindow || window->WasActive)) + GetForegroundDrawList(window)->AddRect(window->Pos, window->Pos + window->Size, IM_COL32(255, 0, 0, 255)); + } + if (g.ErrorCountCurrentFrame == 20) + Text("(and more errors)"); + // EndFrame() will amend debug buttons to this window, after all errors have been submitted. + EndErrorTooltip(); + } + g.ErrorCountCurrentFrame++; + } +#endif + + // Output to callback + if (g.ErrorCallback != NULL) + g.ErrorCallback(&g, g.ErrorCallbackUserData, msg); + + // Return whether we should assert + return g.IO.ConfigErrorRecoveryEnableAssert; } -// Compare to detect usage errors -void ImGuiStackSizes::CompareWithContextState(ImGuiContext* ctx) +void ImGui::ErrorCheckEndFrameFinalizeErrorTooltip() { - ImGuiContext& g = *ctx; - ImGuiWindow* window = g.CurrentWindow; - IM_UNUSED(window); +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + ImGuiContext& g = *GImGui; + if (g.DebugDrawIdConflicts != 0 && g.IO.KeyCtrl == false) + g.DebugDrawIdConflictsCount = g.HoveredIdPreviousFrameItemCount; + if (g.DebugDrawIdConflicts != 0 && g.DebugItemPickerActive == false && BeginErrorTooltip()) + { + Text("Programmer error: %d visible items with conflicting ID!", g.DebugDrawIdConflictsCount); + BulletText("Code should use PushID()/PopID() in loops, or append \"##xx\" to same-label identifiers!"); + BulletText("Empty label e.g. Button(\"\") == same ID as parent widget/node. Use Button(\"##xx\") instead!"); + //BulletText("Code intending to use duplicate ID may use e.g. PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); ... PopItemFlag()"); // Not making this too visible for fear of it being abused. + BulletText("Set io.ConfigDebugDetectIdConflicts=false to disable this warning in non-programmers builds."); + Separator(); + Text("(Hold CTRL to: use"); + SameLine(); + if (SmallButton("Item Picker")) + DebugStartItemPicker(); + SameLine(); + Text("to break in item call-stack, or"); + SameLine(); + if (SmallButton("Open FAQ->About ID Stack System") && g.PlatformIO.Platform_OpenInShellFn != NULL) + g.PlatformIO.Platform_OpenInShellFn(&g, "https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#qa-usage"); + EndErrorTooltip(); + } - // Window stacks - // NOT checking: DC.ItemWidth, DC.TextWrapPos (per window) to allow user to conveniently push once and not pop (they are cleared on Begin) - IM_ASSERT(SizeOfIDStack == window->IDStack.Size && "PushID/PopID or TreeNode/TreePop Mismatch!"); + if (g.ErrorCountCurrentFrame > 0 && BeginErrorTooltip()) // Amend at end of frame + { + Separator(); + Text("(Hold CTRL to:"); + SameLine(); + if (SmallButton("Enable Asserts")) + g.IO.ConfigErrorRecoveryEnableAssert = true; + //SameLine(); + //if (SmallButton("Hide Error Tooltips")) + // g.IO.ConfigErrorRecoveryEnableTooltip = false; // Too dangerous + SameLine(0, 0); + Text(")"); + EndErrorTooltip(); + } +#endif +} - // Global stacks - // For color, style and font stacks there is an incentive to use Push/Begin/Pop/.../End patterns, so we relax our checks a little to allow them. - IM_ASSERT(SizeOfGroupStack == g.GroupStack.Size && "BeginGroup/EndGroup Mismatch!"); - IM_ASSERT(SizeOfBeginPopupStack == g.BeginPopupStack.Size && "BeginPopup/EndPopup or BeginMenu/EndMenu Mismatch!"); - IM_ASSERT(SizeOfDisabledStack == g.DisabledStackSize && "BeginDisabled/EndDisabled Mismatch!"); - IM_ASSERT(SizeOfItemFlagsStack >= g.ItemFlagsStack.Size && "PushItemFlag/PopItemFlag Mismatch!"); - IM_ASSERT(SizeOfColorStack >= g.ColorStack.Size && "PushStyleColor/PopStyleColor Mismatch!"); - IM_ASSERT(SizeOfStyleVarStack >= g.StyleVarStack.Size && "PushStyleVar/PopStyleVar Mismatch!"); - IM_ASSERT(SizeOfFontStack >= g.FontStack.Size && "PushFont/PopFont Mismatch!"); - IM_ASSERT(SizeOfFocusScopeStack == g.FocusScopeStack.Size && "PushFocusScope/PopFocusScope Mismatch!"); +// Pseudo-tooltip. Follow mouse until CTRL is held. When CTRL is held we lock position, allowing to click it. +bool ImGui::BeginErrorTooltip() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = FindWindowByName("##Tooltip_Error"); + const bool use_locked_pos = (g.IO.KeyCtrl && window && window->WasActive); + PushStyleColor(ImGuiCol_PopupBg, ImLerp(g.Style.Colors[ImGuiCol_PopupBg], ImVec4(1.0f, 0.0f, 0.0f, 1.0f), 0.15f)); + if (use_locked_pos) + SetNextWindowPos(g.ErrorTooltipLockedPos); + bool is_visible = Begin("##Tooltip_Error", NULL, ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize); + PopStyleColor(); + if (is_visible && g.CurrentWindow->BeginCount == 1) + { + SeparatorText("MESSAGE FROM DEAR IMGUI"); + BringWindowToDisplayFront(g.CurrentWindow); + BringWindowToFocusFront(g.CurrentWindow); + g.ErrorTooltipLockedPos = GetWindowPos(); + } + else if (!is_visible) + { + End(); + } + return is_visible; +} + +void ImGui::EndErrorTooltip() +{ + End(); } //----------------------------------------------------------------------------- @@ -10229,7 +10792,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu g.LastItemData.ID = id; g.LastItemData.Rect = bb; g.LastItemData.NavRect = nav_bb_arg ? *nav_bb_arg : bb; - g.LastItemData.InFlags = g.CurrentItemFlags | g.NextItemData.ItemFlags | extra_flags; + g.LastItemData.ItemFlags = g.CurrentItemFlags | g.NextItemData.ItemFlags | extra_flags; g.LastItemData.StatusFlags = ImGuiItemStatusFlags_None; // Note: we don't copy 'g.NextItemData.SelectionUserData' to an hypothetical g.LastItemData.SelectionUserData: since the former is not cleared. @@ -10247,7 +10810,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu // to reach unclipped widgets. This would work if user had explicit scrolling control (e.g. mapped on a stick). // We intentionally don't check if g.NavWindow != NULL because g.NavAnyRequest should only be set when it is non null. // If we crash on a NULL g.NavWindow we need to fix the bug elsewhere. - if (!(g.LastItemData.InFlags & ImGuiItemFlags_NoNav)) + if (!(g.LastItemData.ItemFlags & ImGuiItemFlags_NoNav)) { // FIMXE-NAV: investigate changing the window tests into a simple 'if (g.NavFocusScopeId == g.CurrentFocusScopeId)' test. window->DC.NavLayersActiveMaskNext |= (1 << window->DC.NavLayerCurrent); @@ -10257,12 +10820,12 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu NavProcessItem(); } - if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasShortcut) + if (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasShortcut) ItemHandleShortcut(id); } // Lightweight clear of SetNextItemXXX data. - g.NextItemData.Flags = ImGuiNextItemDataFlags_None; + g.NextItemData.HasFlags = ImGuiNextItemDataFlags_None; g.NextItemData.ItemFlags = ImGuiItemFlags_None; #ifdef IMGUI_ENABLE_TEST_ENGINE @@ -10292,7 +10855,7 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu IM_ASSERT(id != window->ID && "Cannot have an empty ID at the root of a window. If you need an empty label, use ## and read the FAQ about how the ID Stack works!"); } //if (g.IO.KeyAlt) window->DrawList->AddRect(bb.Min, bb.Max, IM_COL32(255,255,0,120)); // [DEBUG] - //if ((g.LastItemData.InFlags & ImGuiItemFlags_NoNav) == 0) + //if ((g.LastItemData.ItemFlags & ImGuiItemFlags_NoNav) == 0) // window->DrawList->AddRect(g.LastItemData.NavRect.Min, g.LastItemData.NavRect.Max, IM_COL32(255,255,0,255)); // [DEBUG] #endif @@ -10491,7 +11054,7 @@ void ImGui::Unindent(float indent_w) void ImGui::SetNextItemWidth(float item_width) { ImGuiContext& g = *GImGui; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasWidth; + g.NextItemData.HasFlags |= ImGuiNextItemDataFlags_HasWidth; g.NextItemData.Width = item_width; } @@ -10502,7 +11065,7 @@ void ImGui::PushItemWidth(float item_width) ImGuiWindow* window = g.CurrentWindow; window->DC.ItemWidthStack.push_back(window->DC.ItemWidth); // Backup current width window->DC.ItemWidth = (item_width == 0.0f ? window->ItemWidthDefault : item_width); - g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; + g.NextItemData.HasFlags &= ~ImGuiNextItemDataFlags_HasWidth; } void ImGui::PushMultiItemsWidths(int components, float w_full) @@ -10521,12 +11084,18 @@ void ImGui::PushMultiItemsWidths(int components, float w_full) prev_split = next_split; } window->DC.ItemWidth = ImMax(prev_split, 1.0f); - g.NextItemData.Flags &= ~ImGuiNextItemDataFlags_HasWidth; + g.NextItemData.HasFlags &= ~ImGuiNextItemDataFlags_HasWidth; } void ImGui::PopItemWidth() { - ImGuiWindow* window = GetCurrentWindow(); + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.CurrentWindow; + if (window->DC.ItemWidthStack.Size <= 0) + { + IM_ASSERT_USER_ERROR(0, "Calling PopItemWidth() too many times!"); + return; + } window->DC.ItemWidth = window->DC.ItemWidthStack.back(); window->DC.ItemWidthStack.pop_back(); } @@ -10538,7 +11107,7 @@ float ImGui::CalcItemWidth() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; float w; - if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) + if (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasWidth) w = g.NextItemData.Width; else w = window->DC.ItemWidth; @@ -10988,7 +11557,8 @@ bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags ext { ImGuiContext& g = *GImGui; - if (g.DragDropWithinSource || g.DragDropWithinTarget) + const bool is_dragdrop_tooltip = g.DragDropWithinSource || g.DragDropWithinTarget; + if (is_dragdrop_tooltip) { // Drag and Drop tooltips are positioning differently than other tooltips: // - offset visibility to increase visibility around mouse. @@ -10996,23 +11566,29 @@ bool ImGui::BeginTooltipEx(ImGuiTooltipFlags tooltip_flags, ImGuiWindowFlags ext // We call SetNextWindowPos() to enforce position and disable clamping. // See FindBestWindowPosForPopup() for positionning logic of other tooltips (not drag and drop ones). //ImVec2 tooltip_pos = g.IO.MousePos - g.ActiveIdClickOffset - g.Style.WindowPadding; - ImVec2 tooltip_pos = g.IO.MousePos + TOOLTIP_DEFAULT_OFFSET * g.Style.MouseCursorScale; - SetNextWindowPos(tooltip_pos); + const bool is_touchscreen = (g.IO.MouseSource == ImGuiMouseSource_TouchScreen); + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasPos) == 0) + { + ImVec2 tooltip_pos = is_touchscreen ? (g.IO.MousePos + TOOLTIP_DEFAULT_OFFSET_TOUCH * g.Style.MouseCursorScale) : (g.IO.MousePos + TOOLTIP_DEFAULT_OFFSET_MOUSE * g.Style.MouseCursorScale); + ImVec2 tooltip_pivot = is_touchscreen ? TOOLTIP_DEFAULT_PIVOT_TOUCH : ImVec2(0.0f, 0.0f); + SetNextWindowPos(tooltip_pos, ImGuiCond_None, tooltip_pivot); + } + SetNextWindowBgAlpha(g.Style.Colors[ImGuiCol_PopupBg].w * 0.60f); //PushStyleVar(ImGuiStyleVar_Alpha, g.Style.Alpha * 0.60f); // This would be nice but e.g ColorButton with checkboard has issue with transparent colors :( tooltip_flags |= ImGuiTooltipFlags_OverridePrevious; } - char window_name[16]; - ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", g.TooltipOverrideCount); - if (tooltip_flags & ImGuiTooltipFlags_OverridePrevious) - if (ImGuiWindow* window = FindWindowByName(window_name)) - if (window->Active) - { - // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. - SetWindowHiddenAndSkipItemsForCurrentFrame(window); - ImFormatString(window_name, IM_ARRAYSIZE(window_name), "##Tooltip_%02d", ++g.TooltipOverrideCount); - } + const char* window_name_template = is_dragdrop_tooltip ? "##Tooltip_DragDrop_%02d" : "##Tooltip_%02d"; + char window_name[32]; + ImFormatString(window_name, IM_ARRAYSIZE(window_name), window_name_template, g.TooltipOverrideCount); + if ((tooltip_flags & ImGuiTooltipFlags_OverridePrevious) && g.TooltipPreviousWindow != NULL && g.TooltipPreviousWindow->Active) + { + // Hide previous tooltip from being displayed. We can't easily "reset" the content of a window so we create a new one. + //IMGUI_DEBUG_LOG("[tooltip] '%s' already active, using +1 for this frame\n", window_name); + SetWindowHiddenAndSkipItemsForCurrentFrame(g.TooltipPreviousWindow); + ImFormatString(window_name, IM_ARRAYSIZE(window_name), window_name_template, ++g.TooltipOverrideCount); + } ImGuiWindowFlags flags = ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_AlwaysAutoResize; Begin(window_name, NULL, flags | extra_window_flags); // 2023-03-09: Added bool return value to the API, but currently always returning true. @@ -11268,6 +11844,9 @@ void ImGui::ClosePopupToLevel(int remaining, bool restore_focus_to_window_under_ ImGuiContext& g = *GImGui; IMGUI_DEBUG_LOG_POPUP("[popup] ClosePopupToLevel(%d), restore_under=%d\n", remaining, restore_focus_to_window_under_popup); IM_ASSERT(remaining >= 0 && remaining < g.OpenPopupStack.Size); + if (g.DebugLogFlags & ImGuiDebugLogFlags_EventPopup) + for (int n = remaining; n < g.OpenPopupStack.Size; n++) + IMGUI_DEBUG_LOG_POPUP("[popup] - Closing PopupID 0x%08X Window \"%s\"\n", g.OpenPopupStack[n].PopupId, g.OpenPopupStack[n].Window ? g.OpenPopupStack[n].Window->Name : NULL); // Trim open popup stack ImGuiPopupData prev_popup = g.OpenPopupStack[remaining]; @@ -11600,18 +12179,30 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) if (window->Flags & ImGuiWindowFlags_Tooltip) { // Position tooltip (always follows mouse + clamp within outer boundaries) - // Note that drag and drop tooltips are NOT using this path: BeginTooltipEx() manually sets their position. - // In theory we could handle both cases in same location, but requires a bit of shuffling as drag and drop tooltips are calling SetWindowPos() leading to 'window_pos_set_by_api' being set in Begin() + // FIXME: + // - Too many paths. One problem is that FindBestWindowPosForPopupEx() doesn't allow passing a suggested position (so touch screen path doesn't use it by default). + // - Drag and drop tooltips are not using this path either: BeginTooltipEx() manually sets their position. + // - Require some tidying up. In theory we could handle both cases in same location, but requires a bit of shuffling + // as drag and drop tooltips are calling SetNextWindowPos() leading to 'window_pos_set_by_api' being set in Begin(). IM_ASSERT(g.CurrentWindow == window); const float scale = g.Style.MouseCursorScale; const ImVec2 ref_pos = NavCalcPreferredRefPos(); - const ImVec2 tooltip_pos = ref_pos + TOOLTIP_DEFAULT_OFFSET * scale; + + if (g.IO.MouseSource == ImGuiMouseSource_TouchScreen && NavCalcPreferredRefPosSource() == ImGuiInputSource_Mouse) + { + ImVec2 tooltip_pos = ref_pos + TOOLTIP_DEFAULT_OFFSET_TOUCH * scale - (TOOLTIP_DEFAULT_PIVOT_TOUCH * window->Size); + if (r_outer.Contains(ImRect(tooltip_pos, tooltip_pos + window->Size))) + return tooltip_pos; + } + + ImVec2 tooltip_pos = ref_pos + TOOLTIP_DEFAULT_OFFSET_MOUSE * scale; ImRect r_avoid; - if (!g.NavDisableHighlight && g.NavDisableMouseHover && !(g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos)) + if (g.NavCursorVisible && g.NavHighlightItemUnderNav && !g.IO.ConfigNavMoveSetMousePos) r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 16, ref_pos.y + 8); else r_avoid = ImRect(ref_pos.x - 16, ref_pos.y - 8, ref_pos.x + 24 * scale, ref_pos.y + 24 * scale); // FIXME: Hard-coded based on mouse cursor shape expectation. Exact dimension not very important. //GetForegroundDrawList()->AddRect(r_avoid.Min, r_avoid.Max, IM_COL32(255, 0, 255, 255)); + return FindBestWindowPosForPopupEx(tooltip_pos, window->Size, &window->AutoPosLastDirection, r_outer, r_avoid, ImGuiPopupPositionPolicy_Tooltip); } IM_ASSERT(0); @@ -11626,6 +12217,23 @@ ImVec2 ImGui::FindBestWindowPosForPopup(ImGuiWindow* window) // In our terminology those should be interchangeable, yet right now this is super confusing. // Those two functions are merely a legacy artifact, so at minimum naming should be clarified. +void ImGui::SetNavCursorVisible(bool visible) +{ + ImGuiContext& g = *GImGui; + if (g.IO.ConfigNavCursorVisibleAlways) + visible = true; + g.NavCursorVisible = visible; +} + +// (was called NavRestoreHighlightAfterMove() before 1.91.4) +void ImGui::SetNavCursorVisibleAfterMove() +{ + ImGuiContext& g = *GImGui; + if (g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = true; + g.NavHighlightItemUnderNav = g.NavMousePosDirty = true; +} + void ImGui::SetNavWindow(ImGuiWindow* window) { ImGuiContext& g = *GImGui; @@ -11687,9 +12295,9 @@ void ImGui::SetFocusID(ImGuiID id, ImGuiWindow* window) window->NavRectRel[nav_layer] = WindowRectAbsToRel(window, g.LastItemData.NavRect); if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) - g.NavDisableMouseHover = true; - else - g.NavDisableHighlight = true; + g.NavHighlightItemUnderNav = true; + else if (g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = false; // Clear preferred scoring position (NavMoveRequestApplyResult() will tend to restore it) NavClearPreferredPosForAxis(ImGuiAxis_X); @@ -11712,7 +12320,7 @@ static float inline NavScoreItemDistInterval(float cand_min, float cand_max, flo return 0.0f; } -// Scoring function for gamepad/keyboard directional navigation. Based on https://gist.github.com/rygorous/6981057 +// Scoring function for keyboard/gamepad directional navigation. Based on https://gist.github.com/rygorous/6981057 static bool ImGui::NavScoreItem(ImGuiNavItemData* result) { ImGuiContext& g = *GImGui; @@ -11861,9 +12469,9 @@ static void ImGui::NavApplyItemToResult(ImGuiNavItemData* result) result->Window = window; result->ID = g.LastItemData.ID; result->FocusScopeId = g.CurrentFocusScopeId; - result->InFlags = g.LastItemData.InFlags; + result->ItemFlags = g.LastItemData.ItemFlags; result->RectRel = WindowRectAbsToRel(window, g.LastItemData.NavRect); - if (result->InFlags & ImGuiItemFlags_HasSelectionUserData) + if (result->ItemFlags & ImGuiItemFlags_HasSelectionUserData) { IM_ASSERT(g.NextItemData.SelectionUserData != ImGuiSelectionUserData_Invalid); result->SelectionUserData = g.NextItemData.SelectionUserData; // INTENTIONAL: At this point this field is not cleared in NextItemData. Avoid unnecessary copy to LastItemData. @@ -11886,7 +12494,7 @@ static void ImGui::NavProcessItem() ImGuiContext& g = *GImGui; ImGuiWindow* window = g.CurrentWindow; const ImGuiID id = g.LastItemData.ID; - const ImGuiItemFlags item_flags = g.LastItemData.InFlags; + const ImGuiItemFlags item_flags = g.LastItemData.ItemFlags; // When inside a container that isn't scrollable with Left<>Right, clip NavRect accordingly (#2221) if (window->DC.NavIsScrollPushableX == false) @@ -11948,7 +12556,7 @@ static void ImGui::NavProcessItem() SetNavFocusScope(g.CurrentFocusScopeId); // Will set g.NavFocusScopeId AND store g.NavFocusScopePath g.NavFocusScopeId = g.CurrentFocusScopeId; g.NavIdIsAlive = true; - if (g.LastItemData.InFlags & ImGuiItemFlags_HasSelectionUserData) + if (g.LastItemData.ItemFlags & ImGuiItemFlags_HasSelectionUserData) { IM_ASSERT(g.NextItemData.SelectionUserData != ImGuiSelectionUserData_Invalid); g.NavLastValidSelectionUserData = g.NextItemData.SelectionUserData; // INTENTIONAL: At this point this field is not cleared in NextItemData. Avoid unnecessary copy to LastItemData. @@ -12069,7 +12677,7 @@ void ImGui::NavMoveRequestResolveWithPastTreeNode(ImGuiNavItemData* result, ImGu ImGuiContext& g = *GImGui; g.NavMoveScoringItems = false; g.LastItemData.ID = tree_node_data->ID; - g.LastItemData.InFlags = tree_node_data->InFlags & ~ImGuiItemFlags_HasSelectionUserData; // Losing SelectionUserData, recovered next-frame (cheaper). + g.LastItemData.ItemFlags = tree_node_data->ItemFlags & ~ImGuiItemFlags_HasSelectionUserData; // Losing SelectionUserData, recovered next-frame (cheaper). g.LastItemData.NavRect = tree_node_data->NavRect; NavApplyItemToResult(result); // Result this instead of implementing a NavApplyPastTreeNodeToResult() NavClearPreferredPosForAxis(ImGuiAxis_Y); @@ -12152,13 +12760,6 @@ void ImGui::NavRestoreLayer(ImGuiNavLayer layer) } } -void ImGui::NavRestoreHighlightAfterMove() -{ - ImGuiContext& g = *GImGui; - g.NavDisableHighlight = false; - g.NavDisableMouseHover = g.NavMousePosDirty = true; -} - static inline void ImGui::NavUpdateAnyRequestFlag() { ImGuiContext& g = *GImGui; @@ -12199,14 +12800,29 @@ void ImGui::NavInitWindow(ImGuiWindow* window, bool force_reinit) } } +static ImGuiInputSource ImGui::NavCalcPreferredRefPosSource() +{ + ImGuiContext& g = *GImGui; + ImGuiWindow* window = g.NavWindow; + const bool activated_shortcut = g.ActiveId != 0 && g.ActiveIdFromShortcut && g.ActiveId == g.LastItemData.ID; + + // Testing for !activated_shortcut here could in theory be removed if we decided that activating a remote shortcut altered one of the g.NavDisableXXX flag. + if ((!g.NavCursorVisible || !g.NavHighlightItemUnderNav || !window) && !activated_shortcut) + return ImGuiInputSource_Mouse; + else + return ImGuiInputSource_Keyboard; // or Nav in general +} + static ImVec2 ImGui::NavCalcPreferredRefPos() { ImGuiContext& g = *GImGui; ImGuiWindow* window = g.NavWindow; + ImGuiInputSource source = NavCalcPreferredRefPosSource(); + const bool activated_shortcut = g.ActiveId != 0 && g.ActiveIdFromShortcut && g.ActiveId == g.LastItemData.ID; // Testing for !activated_shortcut here could in theory be removed if we decided that activating a remote shortcut altered one of the g.NavDisableXXX flag. - if ((g.NavDisableHighlight || !g.NavDisableMouseHover || !window) && !activated_shortcut) + if (source == ImGuiInputSource_Mouse) { // Mouse (we need a fallback in case the mouse becomes invalid after being used) // The +1.0f offset when stored by OpenPopupEx() allows reopening this or another popup (same or another mouse button) while not moving the mouse, it is pretty standard. @@ -12295,11 +12911,14 @@ static void ImGui::NavUpdate() NavMoveRequestApplyResult(); g.NavTabbingCounter = 0; g.NavMoveSubmitted = g.NavMoveScoringItems = false; + if (g.NavCursorHideFrames > 0) + if (--g.NavCursorHideFrames == 0) + g.NavCursorVisible = true; // Schedule mouse position update (will be done at the bottom of this function, after 1) processing all move requests and 2) updating scrolling) bool set_mouse_pos = false; if (g.NavMousePosDirty && g.NavIdIsAlive) - if (!g.NavDisableHighlight && g.NavDisableMouseHover && g.NavWindow) + if (g.NavCursorVisible && g.NavHighlightItemUnderNav && g.NavWindow) set_mouse_pos = true; g.NavMousePosDirty = false; IM_ASSERT(g.NavLayer == ImGuiNavLayer_Main || g.NavLayer == ImGuiNavLayer_Menu); @@ -12315,7 +12934,7 @@ static void ImGui::NavUpdate() // Set output flags for user application io.NavActive = (nav_keyboard_active || nav_gamepad_active) && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs); - io.NavVisible = (io.NavActive && g.NavId != 0 && !g.NavDisableHighlight) || (g.NavWindowingTarget != NULL); + io.NavVisible = (io.NavActive && g.NavId != 0 && g.NavCursorVisible) || (g.NavWindowingTarget != NULL); // Process NavCancel input (to close a popup, get back to parent, clear focus) NavUpdateCancelRequest(); @@ -12323,7 +12942,7 @@ static void ImGui::NavUpdate() // Process manual activation request g.NavActivateId = g.NavActivateDownId = g.NavActivatePressedId = 0; g.NavActivateFlags = ImGuiActivateFlags_None; - if (g.NavId != 0 && !g.NavDisableHighlight && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) + if (g.NavId != 0 && g.NavCursorVisible && !g.NavWindowingTarget && g.NavWindow && !(g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) { const bool activate_down = (nav_keyboard_active && IsKeyDown(ImGuiKey_Space, ImGuiKeyOwner_NoOwner)) || (nav_gamepad_active && IsKeyDown(ImGuiKey_NavGamepadActivate, ImGuiKeyOwner_NoOwner)); const bool activate_pressed = activate_down && ((nav_keyboard_active && IsKeyPressed(ImGuiKey_Space, 0, ImGuiKeyOwner_NoOwner)) || (nav_gamepad_active && IsKeyPressed(ImGuiKey_NavGamepadActivate, 0, ImGuiKeyOwner_NoOwner))); @@ -12348,7 +12967,9 @@ static void ImGui::NavUpdate() } } if (g.NavWindow && (g.NavWindow->Flags & ImGuiWindowFlags_NoNavInputs)) - g.NavDisableHighlight = true; + g.NavCursorVisible = false; + else if (g.IO.ConfigNavCursorVisibleAlways && g.NavCursorHideFrames == 0) + g.NavCursorVisible = true; if (g.NavActivateId != 0) IM_ASSERT(g.NavActivateDownId == g.NavActivateId); @@ -12405,13 +13026,13 @@ static void ImGui::NavUpdate() // Always prioritize mouse highlight if navigation is disabled if (!nav_keyboard_active && !nav_gamepad_active) { - g.NavDisableHighlight = true; - g.NavDisableMouseHover = set_mouse_pos = false; + g.NavCursorVisible = false; + g.NavHighlightItemUnderNav = set_mouse_pos = false; } // Update mouse position if requested // (This will take into account the possibility that a Scroll was queued in the window to offset our absolute mouse position before scroll has been applied) - if (set_mouse_pos && (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) + if (set_mouse_pos && io.ConfigNavMoveSetMousePos && (io.BackendFlags & ImGuiBackendFlags_HasSetMousePos)) TeleportMousePos(NavCalcPreferredRefPos()); // [DEBUG] @@ -12441,7 +13062,7 @@ void ImGui::NavInitRequestApplyResult() g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = 0; g.NavJustMovedToIsTabbing = false; - g.NavJustMovedToHasSelectionData = (result->InFlags & ImGuiItemFlags_HasSelectionUserData) != 0; + g.NavJustMovedToHasSelectionData = (result->ItemFlags & ImGuiItemFlags_HasSelectionUserData) != 0; } // Apply result from previous navigation init request (will typically select the first item, unless SetItemDefaultFocus() has been called) @@ -12452,7 +13073,7 @@ void ImGui::NavInitRequestApplyResult() if (result->SelectionUserData != ImGuiSelectionUserData_Invalid) g.NavLastValidSelectionUserData = result->SelectionUserData; if (g.NavInitRequestFromMove) - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); } // Bias scoring rect ahead of scoring + update preferred pos (if missing) using source position @@ -12549,7 +13170,8 @@ void ImGui::NavUpdateCreateMoveRequest() IMGUI_DEBUG_LOG_NAV("[nav] NavInitRequest: from move, window \"%s\", layer=%d\n", window ? window->Name : "", g.NavLayer); g.NavInitRequest = g.NavInitRequestFromMove = true; g.NavInitResult.ID = 0; - g.NavDisableHighlight = false; + if (g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = true; } // When using gamepad, we project the reference nav bounding box into window visible area. @@ -12613,7 +13235,7 @@ void ImGui::NavUpdateCreateTabbingRequest() // See NavProcessItemForTabbingRequest() for a description of the various forward/backward tabbing cases with and without wrapping. const bool nav_keyboard_active = (g.IO.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) != 0; if (nav_keyboard_active) - g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.NavDisableHighlight == true && g.ActiveId == 0) ? 0 : +1; + g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.NavCursorVisible == false && g.ActiveId == 0) ? 0 : +1; else g.NavTabbingDir = g.IO.KeyShift ? -1 : (g.ActiveId == 0) ? 0 : +1; ImGuiNavMoveFlags move_flags = ImGuiNavMoveFlags_IsTabbing | ImGuiNavMoveFlags_Activate; @@ -12645,9 +13267,9 @@ void ImGui::NavMoveRequestApplyResult() if (result == NULL) { if (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) - g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavHighlight; - if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavHighlight) == 0) - NavRestoreHighlightAfterMove(); + g.NavMoveFlags |= ImGuiNavMoveFlags_NoSetNavCursorVisible; + if (g.NavId != 0 && (g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavCursorVisible) == 0) + SetNavCursorVisibleAfterMove(); NavClearPreferredPosForAxis(axis); // On a failed move, clear preferred pos for this axis. IMGUI_DEBUG_LOG_NAV("[nav] NavMoveSubmitted but not led to a result!\n"); return; @@ -12700,7 +13322,7 @@ void ImGui::NavMoveRequestApplyResult() g.NavJustMovedToFocusScopeId = result->FocusScopeId; g.NavJustMovedToKeyMods = g.NavMoveKeyMods; g.NavJustMovedToIsTabbing = (g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) != 0; - g.NavJustMovedToHasSelectionData = (result->InFlags & ImGuiItemFlags_HasSelectionUserData) != 0; + g.NavJustMovedToHasSelectionData = (result->ItemFlags & ImGuiItemFlags_HasSelectionUserData) != 0; //IMGUI_DEBUG_LOG_NAV("[nav] NavJustMovedFromFocusScopeId = 0x%08X, NavJustMovedToFocusScopeId = 0x%08X\n", g.NavJustMovedFromFocusScopeId, g.NavJustMovedToFocusScopeId); } @@ -12720,7 +13342,7 @@ void ImGui::NavMoveRequestApplyResult() } // Tabbing: Activates Inputable, otherwise only Focus - if ((g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && (result->InFlags & ImGuiItemFlags_Inputable) == 0) + if ((g.NavMoveFlags & ImGuiNavMoveFlags_IsTabbing) && (result->ItemFlags & ImGuiItemFlags_Inputable) == 0) g.NavMoveFlags &= ~ImGuiNavMoveFlags_Activate; // Activate @@ -12732,12 +13354,12 @@ void ImGui::NavMoveRequestApplyResult() g.NavNextActivateFlags |= ImGuiActivateFlags_PreferInput | ImGuiActivateFlags_TryToPreserveState | ImGuiActivateFlags_FromTabbing; } - // Enable nav highlight - if ((g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavHighlight) == 0) - NavRestoreHighlightAfterMove(); + // Make nav cursor visible + if ((g.NavMoveFlags & ImGuiNavMoveFlags_NoSetNavCursorVisible) == 0) + SetNavCursorVisibleAfterMove(); } -// Process NavCancel input (to close a popup, get back to parent, clear focus) +// Process Escape/NavCancel input (to close a popup, get back to parent, clear focus) // FIXME: In order to support e.g. Escape to clear a selection we'll need: // - either to store the equivalent of ActiveIdUsingKeyInputMask for a FocusScope and test for it. // - either to move most/all of those tests to the epilogue/end functions of the scope they are dealing with (e.g. exit child window in EndChild()) or in EndFrame(), to allow an earlier intercept @@ -12758,7 +13380,7 @@ static void ImGui::NavUpdateCancelRequest() { // Leave the "menu" layer NavRestoreLayer(ImGuiNavLayer_Main); - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); } else if (g.NavWindow && g.NavWindow != g.NavWindow->RootWindow && !(g.NavWindow->RootWindowForNav->Flags & ImGuiWindowFlags_Popup) && g.NavWindow->RootWindowForNav->ParentWindow) { @@ -12768,7 +13390,7 @@ static void ImGui::NavUpdateCancelRequest() IM_ASSERT(child_window->ChildId != 0); FocusWindow(parent_window); SetNavID(child_window->ChildId, ImGuiNavLayer_Main, 0, WindowRectAbsToRel(parent_window, child_window->Rect())); - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); } else if (g.OpenPopupStack.Size > 0 && g.OpenPopupStack.back().Window != NULL && !(g.OpenPopupStack.back().Window->Flags & ImGuiWindowFlags_Modal)) { @@ -12778,9 +13400,16 @@ static void ImGui::NavUpdateCancelRequest() else { // Clear NavLastId for popups but keep it for regular child window so we can leave one and come back where we were - if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup) || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) - g.NavWindow->NavLastIds[0] = 0; - g.NavId = 0; + // FIXME-NAV: This should happen on window appearing. + if (g.IO.ConfigNavEscapeClearFocusItem || g.IO.ConfigNavEscapeClearFocusWindow) + if (g.NavWindow && ((g.NavWindow->Flags & ImGuiWindowFlags_Popup)))// || !(g.NavWindow->Flags & ImGuiWindowFlags_ChildWindow))) + g.NavWindow->NavLastIds[0] = 0; + + // Clear nav focus + if (g.IO.ConfigNavEscapeClearFocusItem || g.IO.ConfigNavEscapeClearFocusWindow) + g.NavId = 0; + if (g.IO.ConfigNavEscapeClearFocusWindow) + FocusWindow(NULL); } } @@ -12955,7 +13584,7 @@ static ImGuiWindow* FindWindowNavFocusable(int i_start, int i_stop, int dir) // return NULL; } -static void NavUpdateWindowingHighlightWindow(int focus_change_dir) +static void NavUpdateWindowingTarget(int focus_change_dir) { ImGuiContext& g = *GImGui; IM_ASSERT(g.NavWindowingTarget); @@ -13007,14 +13636,17 @@ static void ImGui::NavUpdateWindowing() const bool keyboard_prev_window = allow_windowing && g.ConfigNavWindowingKeyPrev && Shortcut(g.ConfigNavWindowingKeyPrev, ImGuiInputFlags_Repeat | ImGuiInputFlags_RouteAlways, owner_id); const bool start_windowing_with_gamepad = allow_windowing && nav_gamepad_active && !g.NavWindowingTarget && IsKeyPressed(ImGuiKey_NavGamepadMenu, ImGuiInputFlags_None); const bool start_windowing_with_keyboard = allow_windowing && !g.NavWindowingTarget && (keyboard_next_window || keyboard_prev_window); // Note: enabled even without NavEnableKeyboard! + bool just_started_windowing_from_null_focus = false; if (start_windowing_with_gamepad || start_windowing_with_keyboard) if (ImGuiWindow* window = g.NavWindow ? g.NavWindow : FindWindowNavFocusable(g.WindowsFocusOrder.Size - 1, -INT_MAX, -1)) { - g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; + g.NavWindowingTarget = g.NavWindowingTargetAnim = window->RootWindow; // Current location g.NavWindowingTimer = g.NavWindowingHighlightAlpha = 0.0f; g.NavWindowingAccumDeltaPos = g.NavWindowingAccumDeltaSize = ImVec2(0.0f, 0.0f); g.NavWindowingToggleLayer = start_windowing_with_gamepad ? true : false; // Gamepad starts toggling layer g.NavInputSource = start_windowing_with_keyboard ? ImGuiInputSource_Keyboard : ImGuiInputSource_Gamepad; + if (g.NavWindow == NULL) + just_started_windowing_from_null_focus = true; // Manually register ownership of our mods. Using a global route in the Shortcut() calls instead would probably be correct but may have more side-effects. if (keyboard_next_window || keyboard_prev_window) @@ -13030,9 +13662,9 @@ static void ImGui::NavUpdateWindowing() // Select window to focus const int focus_change_dir = (int)IsKeyPressed(ImGuiKey_GamepadL1) - (int)IsKeyPressed(ImGuiKey_GamepadR1); - if (focus_change_dir != 0) + if (focus_change_dir != 0 && !just_started_windowing_from_null_focus) { - NavUpdateWindowingHighlightWindow(focus_change_dir); + NavUpdateWindowingTarget(focus_change_dir); g.NavWindowingHighlightAlpha = 1.0f; } @@ -13055,17 +13687,19 @@ static void ImGui::NavUpdateWindowing() ImGuiKeyChord shared_mods = ((g.ConfigNavWindowingKeyNext ? g.ConfigNavWindowingKeyNext : ImGuiMod_Mask_) & (g.ConfigNavWindowingKeyPrev ? g.ConfigNavWindowingKeyPrev : ImGuiMod_Mask_)) & ImGuiMod_Mask_; IM_ASSERT(shared_mods != 0); // Next/Prev shortcut currently needs a shared modifier to "hold", otherwise Prev actions would keep cycling between two windows. g.NavWindowingHighlightAlpha = ImMax(g.NavWindowingHighlightAlpha, ImSaturate((g.NavWindowingTimer - NAV_WINDOWING_HIGHLIGHT_DELAY) / 0.05f)); // 1.0f - if (keyboard_next_window || keyboard_prev_window) - NavUpdateWindowingHighlightWindow(keyboard_next_window ? -1 : +1); + if ((keyboard_next_window || keyboard_prev_window) && !just_started_windowing_from_null_focus) + NavUpdateWindowingTarget(keyboard_next_window ? -1 : +1); else if ((io.KeyMods & shared_mods) != shared_mods) apply_focus_window = g.NavWindowingTarget; } // Keyboard: Press and Release ALT to toggle menu layer const ImGuiKey windowing_toggle_keys[] = { ImGuiKey_LeftAlt, ImGuiKey_RightAlt }; + bool windowing_toggle_layer_start = false; for (ImGuiKey windowing_toggle_key : windowing_toggle_keys) if (nav_keyboard_active && IsKeyPressed(windowing_toggle_key, 0, ImGuiKeyOwner_NoOwner)) { + windowing_toggle_layer_start = true; g.NavWindowingToggleLayer = true; g.NavWindowingToggleKey = windowing_toggle_key; g.NavInputSource = ImGuiInputSource_Keyboard; @@ -13079,7 +13713,9 @@ static void ImGui::NavUpdateWindowing() // We cancel toggling nav layer if an owner has claimed the key. if (io.InputQueueCharacters.Size > 0 || io.KeyCtrl || io.KeyShift || io.KeySuper) g.NavWindowingToggleLayer = false; - if (TestKeyOwner(g.NavWindowingToggleKey, ImGuiKeyOwner_NoOwner) == false || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_NoOwner) == false) + else if (windowing_toggle_layer_start == false && g.LastKeyboardKeyPressTime == g.Time) + g.NavWindowingToggleLayer = false; + else if (TestKeyOwner(g.NavWindowingToggleKey, ImGuiKeyOwner_NoOwner) == false || TestKeyOwner(ImGuiMod_Alt, ImGuiKeyOwner_NoOwner) == false) g.NavWindowingToggleLayer = false; // Apply layer toggle on Alt release @@ -13105,7 +13741,7 @@ static void ImGui::NavUpdateWindowing() const float NAV_MOVE_SPEED = 800.0f; const float move_step = NAV_MOVE_SPEED * io.DeltaTime * ImMin(io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); g.NavWindowingAccumDeltaPos += nav_move_dir * move_step; - g.NavDisableMouseHover = true; + g.NavHighlightItemUnderNav = true; ImVec2 accum_floored = ImTrunc(g.NavWindowingAccumDeltaPos); if (accum_floored.x != 0.0f || accum_floored.y != 0.0f) { @@ -13120,7 +13756,7 @@ static void ImGui::NavUpdateWindowing() if (apply_focus_window && (g.NavWindow == NULL || apply_focus_window != g.NavWindow->RootWindow)) { ClearActiveID(); - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); ClosePopupsOverWindow(apply_focus_window, false); FocusWindow(apply_focus_window, ImGuiFocusRequestFlags_RestoreFocusedChild); apply_focus_window = g.NavWindow; @@ -13167,7 +13803,7 @@ static void ImGui::NavUpdateWindowing() if (new_nav_layer == ImGuiNavLayer_Menu) g.NavWindow->NavLastIds[new_nav_layer] = 0; NavRestoreLayer(new_nav_layer); - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); } } } @@ -13306,7 +13942,7 @@ bool ImGui::BeginDragDropSource(ImGuiDragDropFlags flags) // Rely on keeping other window->LastItemXXX fields intact. source_id = g.LastItemData.ID = window->GetIDFromRectangle(g.LastItemData.Rect); KeepAliveID(source_id); - bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id, g.LastItemData.InFlags); + bool is_hovered = ItemHoverable(g.LastItemData.Rect, source_id, g.LastItemData.ItemFlags); if (is_hovered && g.IO.MouseClicked[mouse_button]) { SetActiveID(source_id, window); @@ -14230,10 +14866,11 @@ static void ImGui::UpdateViewportsNewFrame() for (ImGuiViewportP* viewport : g.Viewports) { - // Lock down space taken by menu bars and status bars, reset the offset for fucntions like BeginMainMenuBar() to alter them again. - viewport->WorkOffsetMin = viewport->BuildWorkOffsetMin; - viewport->WorkOffsetMax = viewport->BuildWorkOffsetMax; - viewport->BuildWorkOffsetMin = viewport->BuildWorkOffsetMax = ImVec2(0.0f, 0.0f); + // Lock down space taken by menu bars and status bars + // Setup initial value for functions like BeginMainMenuBar(), DockSpaceOverViewport() etc. + viewport->WorkInsetMin = viewport->BuildWorkInsetMin; + viewport->WorkInsetMax = viewport->BuildWorkInsetMax; + viewport->BuildWorkInsetMin = viewport->BuildWorkInsetMax = ImVec2(0.0f, 0.0f); viewport->UpdateWorkRect(); } } @@ -14262,9 +14899,9 @@ static void ImGui::UpdateViewportsNewFrame() // Win32 clipboard implementation // We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown() -static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) +static const char* Platform_GetClipboardTextFn_DefaultImpl(ImGuiContext* ctx) { - ImGuiContext& g = *(ImGuiContext*)user_data_ctx; + ImGuiContext& g = *ctx; g.ClipboardHandlerData.clear(); if (!::OpenClipboard(NULL)) return NULL; @@ -14285,7 +14922,7 @@ static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) return g.ClipboardHandlerData.Data; } -static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext*, const char* text) { if (!::OpenClipboard(NULL)) return; @@ -14312,7 +14949,7 @@ static PasteboardRef main_clipboard = 0; // OSX clipboard implementation // If you enable this you will need to add '-framework ApplicationServices' to your linker command-line! -static void SetClipboardTextFn_DefaultImpl(void*, const char* text) +static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext*, const char* text) { if (!main_clipboard) PasteboardCreate(kPasteboardClipboard, &main_clipboard); @@ -14325,9 +14962,9 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text) } } -static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) +static const char* Platform_GetClipboardTextFn_DefaultImpl(ImGuiContext* ctx) { - ImGuiContext& g = *(ImGuiContext*)user_data_ctx; + ImGuiContext& g = *ctx; if (!main_clipboard) PasteboardCreate(kPasteboardClipboard, &main_clipboard); PasteboardSynchronize(main_clipboard); @@ -14361,15 +14998,15 @@ static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) #else // Local Dear ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers. -static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx) +static const char* Platform_GetClipboardTextFn_DefaultImpl(ImGuiContext* ctx) { - ImGuiContext& g = *(ImGuiContext*)user_data_ctx; + ImGuiContext& g = *ctx; return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin(); } -static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text) +static void Platform_SetClipboardTextFn_DefaultImpl(ImGuiContext* ctx, const char* text) { - ImGuiContext& g = *(ImGuiContext*)user_data_ctx; + ImGuiContext& g = *ctx; g.ClipboardHandlerData.clear(); const char* text_end = text + strlen(text); g.ClipboardHandlerData.resize((int)(text_end - text) + 1); @@ -14397,14 +15034,14 @@ static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text #ifdef _MSC_VER #pragma comment(lib, "shell32") #endif -static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) +static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) { return (INT_PTR)::ShellExecuteA(NULL, "open", path, NULL, NULL, SW_SHOWDEFAULT) > 32; } #else #include #include -static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) +static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) { #if defined(__APPLE__) const char* args[] { "open", "--", path, NULL }; @@ -14428,7 +15065,7 @@ static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char* path) } #endif #else -static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { return false; } +static bool Platform_OpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { return false; } #endif // Default shell handlers //----------------------------------------------------------------------------- @@ -14441,7 +15078,7 @@ static bool PlatformOpenInShellFn_DefaultImpl(ImGuiContext*, const char*) { retu #pragma comment(lib, "imm32") #endif -static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) +static void Platform_SetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) { // Notify OS Input Method Editor of text input position HWND hwnd = (HWND)viewport->PlatformHandleRaw; @@ -14467,7 +15104,7 @@ static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport* viewp #else -static void PlatformSetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData*) {} +static void Platform_SetImeDataFn_DefaultImpl(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData*) {} #endif // Default IME handlers @@ -14655,7 +15292,7 @@ void ImGui::UpdateDebugToolFlashStyleColor() ImGuiContext& g = *GImGui; if (g.DebugFlashStyleColorTime <= 0.0f) return; - ColorConvertHSVtoRGB(cosf(g.DebugFlashStyleColorTime * 6.0f) * 0.5f + 0.5f, 0.5f, 0.5f, g.Style.Colors[g.DebugFlashStyleColorIdx].x, g.Style.Colors[g.DebugFlashStyleColorIdx].y, g.Style.Colors[g.DebugFlashStyleColorIdx].z); + ColorConvertHSVtoRGB(ImCos(g.DebugFlashStyleColorTime * 6.0f) * 0.5f + 0.5f, 0.5f, 0.5f, g.Style.Colors[g.DebugFlashStyleColorIdx].x, g.Style.Colors[g.DebugFlashStyleColorIdx].y, g.Style.Colors[g.DebugFlashStyleColorIdx].z); g.Style.Colors[g.DebugFlashStyleColorIdx].w = 1.0f; if ((g.DebugFlashStyleColorTime -= g.IO.DeltaTime) <= 0.0f) DebugFlashStyleColorStop(); @@ -14715,7 +15352,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) DebugBreakClearData(); // Basic info - Text("Dear ImGui %s", GetVersion()); + Text("Dear ImGui %s (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); if (g.ContextName[0] != 0) { SameLine(); @@ -15210,7 +15847,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) Text("NavActive: %d, NavVisible: %d", g.IO.NavActive, g.IO.NavVisible); Text("NavActivateId/DownId/PressedId: %08X/%08X/%08X", g.NavActivateId, g.NavActivateDownId, g.NavActivatePressedId); Text("NavActivateFlags: %04X", g.NavActivateFlags); - Text("NavDisableHighlight: %d, NavDisableMouseHover: %d", g.NavDisableHighlight, g.NavDisableMouseHover); + Text("NavCursorVisible: %d, NavHighlightItemUnderNav: %d", g.NavCursorVisible, g.NavHighlightItemUnderNav); Text("NavFocusScopeId = 0x%08X", g.NavFocusScopeId); Text("NavFocusRoute[] = "); for (int path_n = g.NavFocusRoute.Size - 1; path_n >= 0; path_n--) @@ -15337,7 +15974,7 @@ bool ImGui::DebugBreakButton(const char* label, const char* description_of_locat ColorConvertRGBtoHSV(col4f.x, col4f.y, col4f.z, hsv.x, hsv.y, hsv.z); ColorConvertHSVtoRGB(hsv.x + 0.20f, hsv.y, hsv.z, col4f.x, col4f.y, col4f.z); - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); RenderFrame(bb.Min, bb.Max, GetColorU32(col4f), true, g.Style.FrameRounding); RenderTextClipped(bb.Min, bb.Max, label, NULL, &label_size, g.Style.ButtonTextAlign, &bb); @@ -15657,9 +16294,9 @@ void ImGui::DebugNodeViewport(ImGuiViewportP* viewport) if (open) { ImGuiWindowFlags flags = viewport->Flags; - BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nWorkArea Offset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f", + BulletText("Main Pos: (%.0f,%.0f), Size: (%.0f,%.0f)\nWorkArea Inset Left: %.0f Top: %.0f, Right: %.0f, Bottom: %.0f", viewport->Pos.x, viewport->Pos.y, viewport->Size.x, viewport->Size.y, - viewport->WorkOffsetMin.x, viewport->WorkOffsetMin.y, viewport->WorkOffsetMax.x, viewport->WorkOffsetMax.y); + viewport->WorkInsetMin.x, viewport->WorkInsetMin.y, viewport->WorkInsetMax.x, viewport->WorkInsetMax.y); BulletText("Flags: 0x%04X =%s%s%s", viewport->Flags, (flags & ImGuiViewportFlags_IsPlatformWindow) ? " IsPlatformWindow" : "", (flags & ImGuiViewportFlags_IsPlatformMonitor) ? " IsPlatformMonitor" : "", @@ -15704,7 +16341,7 @@ void ImGui::DebugNodeWindow(ImGuiWindow* window, const char* label) (flags & ImGuiWindowFlags_NoMouseInputs)? "NoMouseInputs":"", (flags & ImGuiWindowFlags_NoNavInputs) ? "NoNavInputs" : "", (flags & ImGuiWindowFlags_AlwaysAutoResize) ? "AlwaysAutoResize" : ""); if (flags & ImGuiWindowFlags_ChildWindow) BulletText("ChildFlags: 0x%08X (%s%s%s%s..)", window->ChildFlags, - (window->ChildFlags & ImGuiChildFlags_Border) ? "Border " : "", + (window->ChildFlags & ImGuiChildFlags_Borders) ? "Borders " : "", (window->ChildFlags & ImGuiChildFlags_ResizeX) ? "ResizeX " : "", (window->ChildFlags & ImGuiChildFlags_ResizeY) ? "ResizeY " : "", (window->ChildFlags & ImGuiChildFlags_NavFlattened) ? "NavFlattened " : ""); @@ -15827,18 +16464,30 @@ static void ShowDebugLogFlag(const char* name, ImGuiDebugLogFlags flags) ImGuiContext& g = *GImGui; ImVec2 size(ImGui::GetFrameHeight() + g.Style.ItemInnerSpacing.x + ImGui::CalcTextSize(name).x, ImGui::GetFrameHeight()); SameLineOrWrap(size); // FIXME-LAYOUT: To be done automatically once we rework ItemSize/ItemAdd into ItemLayout. + + bool highlight_errors = (flags == ImGuiDebugLogFlags_EventError && g.DebugLogSkippedErrors > 0); + if (highlight_errors) + ImGui::PushStyleColor(ImGuiCol_Text, ImLerp(g.Style.Colors[ImGuiCol_Text], ImVec4(1.0f, 0.0f, 0.0f, 1.0f), 0.30f)); if (ImGui::CheckboxFlags(name, &g.DebugLogFlags, flags) && g.IO.KeyShift && (g.DebugLogFlags & flags) != 0) { g.DebugLogAutoDisableFrames = 2; g.DebugLogAutoDisableFlags |= flags; } - ImGui::SetItemTooltip("Hold SHIFT when clicking to enable for 2 frames only (useful for spammy log entries)"); + if (highlight_errors) + { + ImGui::PopStyleColor(); + ImGui::SetItemTooltip("%d past errors skipped.", g.DebugLogSkippedErrors); + } + else + { + ImGui::SetItemTooltip("Hold SHIFT when clicking to enable for 2 frames only (useful for spammy log entries)"); + } } void ImGui::ShowDebugLogWindow(bool* p_open) { ImGuiContext& g = *GImGui; - if (!(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize)) + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0) SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 12.0f), ImGuiCond_FirstUseEver); if (!Begin("Dear ImGui Debug Log", p_open) || GetCurrentWindow()->BeginCount > 1) { @@ -15850,6 +16499,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) CheckboxFlags("All", &g.DebugLogFlags, all_enable_flags); SetItemTooltip("(except InputRouting which is spammy)"); + ShowDebugLogFlag("Errors", ImGuiDebugLogFlags_EventError); ShowDebugLogFlag("ActiveId", ImGuiDebugLogFlags_EventActiveId); ShowDebugLogFlag("Clipper", ImGuiDebugLogFlags_EventClipper); ShowDebugLogFlag("Focus", ImGuiDebugLogFlags_EventFocus); @@ -15863,6 +16513,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) { g.DebugLogBuf.clear(); g.DebugLogIndex.clear(); + g.DebugLogSkippedErrors = 0; } SameLine(); if (SmallButton("Copy")) @@ -15883,7 +16534,7 @@ void ImGui::ShowDebugLogWindow(bool* p_open) EndPopup(); } - BeginChild("##log", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); + BeginChild("##log", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Borders, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); const ImGuiDebugLogFlags backup_log_flags = g.DebugLogFlags; g.DebugLogFlags &= ~ImGuiDebugLogFlags_EventClipper; @@ -16155,7 +16806,7 @@ static int StackToolFormatLevelInfo(ImGuiIDStackTool* tool, int n, bool format_f void ImGui::ShowIDStackToolWindow(bool* p_open) { ImGuiContext& g = *GImGui; - if (!(g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize)) + if ((g.NextWindowData.Flags & ImGuiNextWindowDataFlags_HasSize) == 0) SetNextWindowSize(ImVec2(0.0f, GetFontSize() * 8.0f), ImGuiCond_FirstUseEver); if (!Begin("Dear ImGui ID Stack Tool", p_open) || GetCurrentWindow()->BeginCount > 1) { diff --git a/src/imgui/imgui_demo.cpp b/src/imgui/imgui_demo.cpp index 4d6c2bbf..5849e11e 100644 --- a/src/imgui/imgui_demo.cpp +++ b/src/imgui/imgui_demo.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (demo code) // Help: @@ -116,6 +116,9 @@ Index of this file: #if !defined(_MSC_VER) || _MSC_VER >= 1800 #include // PRId64/PRIu64, not avail in some MinGW headers. #endif +#ifdef __EMSCRIPTEN__ +#include // __EMSCRIPTEN_major__ etc. +#endif // Visual Studio warnings #ifdef _MSC_VER @@ -310,23 +313,24 @@ static ExampleTreeNode* ExampleTree_CreateNode(const char* name, int uid, Exampl static ExampleTreeNode* ExampleTree_CreateDemoTree() { static const char* root_names[] = { "Apple", "Banana", "Cherry", "Kiwi", "Mango", "Orange", "Pear", "Pineapple", "Strawberry", "Watermelon" }; - char name_buf[32]; + const size_t NAME_MAX_LEN = sizeof(ExampleTreeNode::Name); + char name_buf[NAME_MAX_LEN]; int uid = 0; ExampleTreeNode* node_L0 = ExampleTree_CreateNode("", ++uid, NULL); const int root_items_multiplier = 2; for (int idx_L0 = 0; idx_L0 < IM_ARRAYSIZE(root_names) * root_items_multiplier; idx_L0++) { - snprintf(name_buf, 32, "%s %d", root_names[idx_L0 / root_items_multiplier], idx_L0 % root_items_multiplier); + snprintf(name_buf, IM_ARRAYSIZE(name_buf), "%s %d", root_names[idx_L0 / root_items_multiplier], idx_L0 % root_items_multiplier); ExampleTreeNode* node_L1 = ExampleTree_CreateNode(name_buf, ++uid, node_L0); const int number_of_childs = (int)strlen(node_L1->Name); for (int idx_L1 = 0; idx_L1 < number_of_childs; idx_L1++) { - snprintf(name_buf, 32, "Child %d", idx_L1); + snprintf(name_buf, IM_ARRAYSIZE(name_buf), "Child %d", idx_L1); ExampleTreeNode* node_L2 = ExampleTree_CreateNode(name_buf, ++uid, node_L1); node_L2->HasData = true; if (idx_L1 == 0) { - snprintf(name_buf, 32, "Sub-child %d", 0); + snprintf(name_buf, IM_ARRAYSIZE(name_buf), "Sub-child %d", 0); ExampleTreeNode* node_L3 = ExampleTree_CreateNode(name_buf, ++uid, node_L2); node_L3->HasData = true; } @@ -497,8 +501,6 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::SameLine(); HelpMarker("Enable keyboard controls."); ImGui::CheckboxFlags("io.ConfigFlags: NavEnableGamepad", &io.ConfigFlags, ImGuiConfigFlags_NavEnableGamepad); ImGui::SameLine(); HelpMarker("Enable gamepad controls. Require backend to set io.BackendFlags |= ImGuiBackendFlags_HasGamepad.\n\nRead instructions in imgui.cpp for details."); - ImGui::CheckboxFlags("io.ConfigFlags: NavEnableSetMousePos", &io.ConfigFlags, ImGuiConfigFlags_NavEnableSetMousePos); - ImGui::SameLine(); HelpMarker("Instruct navigation to move the mouse cursor. See comment for ImGuiConfigFlags_NavEnableSetMousePos."); ImGui::CheckboxFlags("io.ConfigFlags: NoMouse", &io.ConfigFlags, ImGuiConfigFlags_NoMouse); ImGui::SameLine(); HelpMarker("Instruct dear imgui to disable mouse inputs and interactions."); @@ -525,6 +527,20 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Checkbox("io.MouseDrawCursor", &io.MouseDrawCursor); ImGui::SameLine(); HelpMarker("Instruct Dear ImGui to render a mouse cursor itself. Note that a mouse cursor rendered via your application GPU rendering path will feel more laggy than hardware cursor, but will be more in sync with your other visuals.\n\nSome desktop applications may use both kinds of cursors (e.g. enable software cursor only when resizing/dragging something)."); + ImGui::SeparatorText("Keyboard/Gamepad Navigation"); + ImGui::Checkbox("io.ConfigNavSwapGamepadButtons", &io.ConfigNavSwapGamepadButtons); + ImGui::Checkbox("io.ConfigNavMoveSetMousePos", &io.ConfigNavMoveSetMousePos); + ImGui::SameLine(); HelpMarker("Directional/tabbing navigation teleports the mouse cursor. May be useful on TV/console systems where moving a virtual mouse is difficult"); + ImGui::Checkbox("io.ConfigNavCaptureKeyboard", &io.ConfigNavCaptureKeyboard); + ImGui::Checkbox("io.ConfigNavEscapeClearFocusItem", &io.ConfigNavEscapeClearFocusItem); + ImGui::SameLine(); HelpMarker("Pressing Escape clears focused item."); + ImGui::Checkbox("io.ConfigNavEscapeClearFocusWindow", &io.ConfigNavEscapeClearFocusWindow); + ImGui::SameLine(); HelpMarker("Pressing Escape clears focused window."); + ImGui::Checkbox("io.ConfigNavCursorVisibleAuto", &io.ConfigNavCursorVisibleAuto); + ImGui::SameLine(); HelpMarker("Using directional navigation key makes the cursor visible. Mouse click hides the cursor."); + ImGui::Checkbox("io.ConfigNavCursorVisibleAlways", &io.ConfigNavCursorVisibleAlways); + ImGui::SameLine(); HelpMarker("Navigation cursor is always visible."); + ImGui::SeparatorText("Widgets"); ImGui::Checkbox("io.ConfigInputTextCursorBlink", &io.ConfigInputTextCursorBlink); ImGui::SameLine(); HelpMarker("Enable blinking cursor (optional as some users consider it to be distracting)."); @@ -535,15 +551,37 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::Checkbox("io.ConfigWindowsResizeFromEdges", &io.ConfigWindowsResizeFromEdges); ImGui::SameLine(); HelpMarker("Enable resizing of windows from their edges and from the lower-left corner.\nThis requires (io.BackendFlags & ImGuiBackendFlags_HasMouseCursors) because it needs mouse cursor feedback."); ImGui::Checkbox("io.ConfigWindowsMoveFromTitleBarOnly", &io.ConfigWindowsMoveFromTitleBarOnly); + ImGui::Checkbox("io.ConfigScrollbarScrollByPage", &io.ConfigScrollbarScrollByPage); + ImGui::SameLine(); HelpMarker("Enable scrolling page by page when clicking outside the scrollbar grab.\nWhen disabled, always scroll to clicked location.\nWhen enabled, Shift+Click scrolls to clicked location."); ImGui::Checkbox("io.ConfigMacOSXBehaviors", &io.ConfigMacOSXBehaviors); ImGui::SameLine(); HelpMarker("Swap Cmd<>Ctrl keys, enable various MacOS style behaviors."); ImGui::Text("Also see Style->Rendering for rendering options."); + // Also read: https://github.com/ocornut/imgui/wiki/Error-Handling + ImGui::SeparatorText("Error Handling"); + + ImGui::Checkbox("io.ConfigErrorRecovery", &io.ConfigErrorRecovery); + ImGui::SameLine(); HelpMarker( + "Options to configure how we handle recoverable errors.\n" + "- Error recovery is not perfect nor guaranteed! It is a feature to ease development.\n" + "- You not are not supposed to rely on it in the course of a normal application run.\n" + "- Possible usage: facilitate recovery from errors triggered from a scripting language or after specific exceptions handlers.\n" + "- Always ensure that on programmers seat you have at minimum Asserts or Tooltips enabled when making direct imgui API call!" + "Otherwise it would severely hinder your ability to catch and correct mistakes!"); + ImGui::Checkbox("io.ConfigErrorRecoveryEnableAssert", &io.ConfigErrorRecoveryEnableAssert); + ImGui::Checkbox("io.ConfigErrorRecoveryEnableDebugLog", &io.ConfigErrorRecoveryEnableDebugLog); + ImGui::Checkbox("io.ConfigErrorRecoveryEnableTooltip", &io.ConfigErrorRecoveryEnableTooltip); + if (!io.ConfigErrorRecoveryEnableAssert && !io.ConfigErrorRecoveryEnableDebugLog && !io.ConfigErrorRecoveryEnableTooltip) + io.ConfigErrorRecoveryEnableAssert = io.ConfigErrorRecoveryEnableDebugLog = io.ConfigErrorRecoveryEnableTooltip = true; + + // Also read: https://github.com/ocornut/imgui/wiki/Debug-Tools ImGui::SeparatorText("Debug"); ImGui::Checkbox("io.ConfigDebugIsDebuggerPresent", &io.ConfigDebugIsDebuggerPresent); ImGui::SameLine(); HelpMarker("Enable various tools calling IM_DEBUG_BREAK().\n\nRequires a debugger being attached, otherwise IM_DEBUG_BREAK() options will appear to crash your application."); + ImGui::Checkbox("io.ConfigDebugHighlightIdConflicts", &io.ConfigDebugHighlightIdConflicts); + ImGui::SameLine(); HelpMarker("Highlight and show an error message when multiple items have conflicting identifiers."); ImGui::BeginDisabled(); - ImGui::Checkbox("io.ConfigDebugBeginReturnValueOnce", &io.ConfigDebugBeginReturnValueOnce); // . + ImGui::Checkbox("io.ConfigDebugBeginReturnValueOnce", &io.ConfigDebugBeginReturnValueOnce); ImGui::EndDisabled(); ImGui::SameLine(); HelpMarker("First calls to Begin()/BeginChild() will return false.\n\nTHIS OPTION IS DISABLED because it needs to be set at application boot-time to make sense. Showing the disabled option is a way to make this feature easier to discover."); ImGui::Checkbox("io.ConfigDebugBeginReturnValueLoop", &io.ConfigDebugBeginReturnValueLoop); @@ -571,6 +609,7 @@ void ImGui::ShowDemoWindow(bool* p_open) ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &io.BackendFlags, ImGuiBackendFlags_HasSetMousePos); ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &io.BackendFlags, ImGuiBackendFlags_RendererHasVtxOffset); ImGui::EndDisabled(); + ImGui::TreePop(); ImGui::Spacing(); } @@ -680,6 +719,7 @@ static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data) if (ImGui::BeginMenu("Tools")) { IMGUI_DEMO_MARKER("Menu/Tools"); + ImGuiIO& io = ImGui::GetIO(); #ifndef IMGUI_DISABLE_DEBUG_TOOLS const bool has_debug_tools = true; #else @@ -688,14 +728,16 @@ static void ShowDemoWindowMenuBar(ImGuiDemoWindowData* demo_data) ImGui::MenuItem("Metrics/Debugger", NULL, &demo_data->ShowMetrics, has_debug_tools); ImGui::MenuItem("Debug Log", NULL, &demo_data->ShowDebugLog, has_debug_tools); ImGui::MenuItem("ID Stack Tool", NULL, &demo_data->ShowIDStackTool, has_debug_tools); - ImGui::MenuItem("Style Editor", NULL, &demo_data->ShowStyleEditor); - bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent; + bool is_debugger_present = io.ConfigDebugIsDebuggerPresent; if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present)) ImGui::DebugStartItemPicker(); if (!is_debugger_present) ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools."); - ImGui::Separator(); + ImGui::MenuItem("Style Editor", NULL, &demo_data->ShowStyleEditor); ImGui::MenuItem("About Dear ImGui", NULL, &demo_data->ShowAbout); + + ImGui::SeparatorText("Debug Options"); + ImGui::MenuItem("Highlight ID Conflicts", NULL, &io.ConfigDebugHighlightIdConflicts, has_debug_tools); ImGui::EndMenu(); } ImGui::EndMenuBar(); @@ -974,7 +1016,7 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) // The following examples are passed for documentation purpose but may not be useful to most users. // Passing ImGuiHoveredFlags_ForTooltip to IsItemHovered() will pull ImGuiHoveredFlags flags values from - // 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on whether mouse or gamepad/keyboard is being used. + // 'style.HoverFlagsForTooltipMouse' or 'style.HoverFlagsForTooltipNav' depending on whether mouse or keyboard/gamepad is being used. // With default settings, ImGuiHoveredFlags_ForTooltip is equivalent to ImGuiHoveredFlags_DelayShort + ImGuiHoveredFlags_Stationary. ImGui::Button("Manual", sz); if (ImGui::IsItemHovered(ImGuiHoveredFlags_ForTooltip)) @@ -1996,8 +2038,12 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) ImGui::SameLine(); ImGui::SliderInt("Sample count", &display_count, 1, 400); float (*func)(void*, int) = (func_type == 0) ? Funcs::Sin : Funcs::Saw; - ImGui::PlotLines("Lines", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); - ImGui::PlotHistogram("Histogram", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); + ImGui::PlotLines("Lines##2", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); + ImGui::PlotHistogram("Histogram##2", func, NULL, display_count, 0, NULL, -1.0f, 1.0f, ImVec2(0, 80)); + ImGui::Separator(); + + ImGui::Text("Need better plotting and graphing? Consider using ImPlot:"); + ImGui::TextLinkOpenURL("https://github.com/epezent/implot"); ImGui::Separator(); ImGui::TreePop(); @@ -2231,7 +2277,10 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) // Demonstrate using advanced flags for DragXXX and SliderXXX functions. Note that the flags are the same! static ImGuiSliderFlags flags = ImGuiSliderFlags_None; ImGui::CheckboxFlags("ImGuiSliderFlags_AlwaysClamp", &flags, ImGuiSliderFlags_AlwaysClamp); - ImGui::SameLine(); HelpMarker("Always clamp value to min/max bounds (if any) when input manually with CTRL+Click."); + ImGui::CheckboxFlags("ImGuiSliderFlags_ClampOnInput", &flags, ImGuiSliderFlags_ClampOnInput); + ImGui::SameLine(); HelpMarker("Clamp value to min/max bounds when input manually with CTRL+Click. By default CTRL+Click allows going out of bounds."); + ImGui::CheckboxFlags("ImGuiSliderFlags_ClampZeroRange", &flags, ImGuiSliderFlags_ClampZeroRange); + ImGui::SameLine(); HelpMarker("Clamp even if min==max==0.0f. Otherwise DragXXX functions don't clamp."); ImGui::CheckboxFlags("ImGuiSliderFlags_Logarithmic", &flags, ImGuiSliderFlags_Logarithmic); ImGui::SameLine(); HelpMarker("Enable logarithmic editing (more precision for small values)."); ImGui::CheckboxFlags("ImGuiSliderFlags_NoRoundToFormat", &flags, ImGuiSliderFlags_NoRoundToFormat); @@ -2249,6 +2298,8 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) ImGui::DragFloat("DragFloat (0 -> +inf)", &drag_f, 0.005f, 0.0f, FLT_MAX, "%.3f", flags); ImGui::DragFloat("DragFloat (-inf -> 1)", &drag_f, 0.005f, -FLT_MAX, 1.0f, "%.3f", flags); ImGui::DragFloat("DragFloat (-inf -> +inf)", &drag_f, 0.005f, -FLT_MAX, +FLT_MAX, "%.3f", flags); + //ImGui::DragFloat("DragFloat (0 -> 0)", &drag_f, 0.005f, 0.0f, 0.0f, "%.3f", flags); // To test ClampZeroRange + //ImGui::DragFloat("DragFloat (100 -> 100)", &drag_f, 0.005f, 100.0f, 100.0f, "%.3f", flags); ImGui::DragInt("DragInt (0 -> 100)", &drag_i, 0.5f, 0, 100, "%d", flags); // Sliders @@ -2588,6 +2639,11 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) IMGUI_DEMO_MARKER("Widgets/Drag and Drop/Drag to reorder items (simple)"); if (ImGui::TreeNode("Drag to reorder items (simple)")) { + // FIXME: there is temporary (usually single-frame) ID Conflict during reordering as a same item may be submitting twice. + // This code was always slightly faulty but in a way which was not easily noticeable. + // Until we fix this, enable ImGuiItemFlags_AllowDuplicateId to disable detecting the issue. + ImGui::PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); + // Simple reordering HelpMarker( "We don't use the drag and drop api at all here! " @@ -2609,6 +2665,8 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) } } } + + ImGui::PopItemFlag(); ImGui::TreePop(); } @@ -2761,7 +2819,7 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) static bool embed_all_inside_a_child_window = false; ImGui::Checkbox("Embed everything inside a child window for testing _RootWindow flag.", &embed_all_inside_a_child_window); if (embed_all_inside_a_child_window) - ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), ImGuiChildFlags_Border); + ImGui::BeginChild("outer_child", ImVec2(0, ImGui::GetFontSize() * 20.0f), ImGuiChildFlags_Borders); // Testing IsWindowFocused() function with its various flags. ImGui::BulletText( @@ -2809,7 +2867,7 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data) ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow), ImGui::IsWindowHovered(ImGuiHoveredFlags_Stationary)); - ImGui::BeginChild("child", ImVec2(0, 50), ImGuiChildFlags_Border); + ImGui::BeginChild("child", ImVec2(0, 50), ImGuiChildFlags_Borders); ImGui::Text("This is another child window for testing the _ChildWindows flag."); ImGui::EndChild(); if (embed_all_inside_a_child_window) @@ -3374,7 +3432,7 @@ static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data) ImGui::CheckboxFlags("ImGuiMultiSelectFlags_NoAutoClear", &flags, ImGuiMultiSelectFlags_NoAutoClear); ImGui::CheckboxFlags("ImGuiMultiSelectFlags_BoxSelect2d", &flags, ImGuiMultiSelectFlags_BoxSelect2d); // Cannot use ImGuiMultiSelectFlags_BoxSelect1d as checkboxes are varying width. - if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) + if (ImGui::BeginChild("##Basket", ImVec2(-FLT_MIN, ImGui::GetFontSize() * 20), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeY)) { ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, -1, IM_ARRAYSIZE(items)); ImGuiSelectionExternalStorage storage_wrapper; @@ -3676,7 +3734,7 @@ static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data) { ImVec2 color_button_sz(ImGui::GetFontSize(), ImGui::GetFontSize()); if (widget_type == WidgetType_TreeNode) - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f)); + ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, 0.0f); ImGuiMultiSelectIO* ms_io = ImGui::BeginMultiSelect(flags, selection.Size, items.Size); selection.ApplyRequests(ms_io); @@ -3692,7 +3750,7 @@ static void ShowDemoWindowMultiSelect(ImGuiDemoWindowData* demo_data) ImGui::BeginTable("##Split", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_NoSavedSettings | ImGuiTableFlags_NoPadOuterX); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.70f); ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthStretch, 0.30f); - //ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, 0.0f)); + //ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacingY, 0.0f); } ImGuiListClipper clipper; @@ -3880,7 +3938,7 @@ static void ShowDemoWindowLayout() if (!disable_menu) window_flags |= ImGuiWindowFlags_MenuBar; ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, 5.0f); - ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Border, window_flags); + ImGui::BeginChild("ChildR", ImVec2(0, 260), ImGuiChildFlags_Borders, window_flags); if (!disable_menu && ImGui::BeginMenuBar()) { if (ImGui::BeginMenu("Menu")) @@ -3909,8 +3967,11 @@ static void ShowDemoWindowLayout() ImGui::SeparatorText("Manual-resize"); { HelpMarker("Drag bottom border to resize. Double-click bottom border to auto-fit to vertical contents."); + //if (ImGui::Button("Set Height to 200")) + // ImGui::SetNextWindowSize(ImVec2(-FLT_MIN, 200.0f)); + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_FrameBg)); - if (ImGui::BeginChild("ResizableChild", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) + if (ImGui::BeginChild("ResizableChild", ImVec2(-FLT_MIN, ImGui::GetTextLineHeightWithSpacing() * 8), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeY)) for (int n = 0; n < 10; n++) ImGui::Text("Line %04d", n); ImGui::PopStyleColor(); @@ -3928,7 +3989,7 @@ static void ShowDemoWindowLayout() ImGui::DragInt("Max Height (in Lines)", &max_height_in_lines, 0.2f); ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 1), ImVec2(FLT_MAX, ImGui::GetTextLineHeightWithSpacing() * max_height_in_lines)); - if (ImGui::BeginChild("ConstrainedChild", ImVec2(-FLT_MIN, 0.0f), ImGuiChildFlags_Border | ImGuiChildFlags_AutoResizeY)) + if (ImGui::BeginChild("ConstrainedChild", ImVec2(-FLT_MIN, 0.0f), ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY)) for (int n = 0; n < draw_lines; n++) ImGui::Text("Line %04d", n); ImGui::EndChild(); @@ -3946,11 +4007,11 @@ static void ShowDemoWindowLayout() { static int offset_x = 0; static bool override_bg_color = true; - static ImGuiChildFlags child_flags = ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY; + static ImGuiChildFlags child_flags = ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeX | ImGuiChildFlags_ResizeY; ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); ImGui::DragInt("Offset X", &offset_x, 1.0f, -1000, 1000); ImGui::Checkbox("Override ChildBg color", &override_bg_color); - ImGui::CheckboxFlags("ImGuiChildFlags_Border", &child_flags, ImGuiChildFlags_Border); + ImGui::CheckboxFlags("ImGuiChildFlags_Borders", &child_flags, ImGuiChildFlags_Borders); ImGui::CheckboxFlags("ImGuiChildFlags_AlwaysUseWindowPadding", &child_flags, ImGuiChildFlags_AlwaysUseWindowPadding); ImGui::CheckboxFlags("ImGuiChildFlags_ResizeX", &child_flags, ImGuiChildFlags_ResizeX); ImGui::CheckboxFlags("ImGuiChildFlags_ResizeY", &child_flags, ImGuiChildFlags_ResizeY); @@ -4217,7 +4278,7 @@ static void ShowDemoWindowLayout() // down by FramePadding.y ahead of time) ImGui::AlignTextToFramePadding(); ImGui::Text("OK Blahblah"); ImGui::SameLine(); - ImGui::Button("Some framed item"); ImGui::SameLine(); + ImGui::Button("Some framed item##2"); ImGui::SameLine(); HelpMarker("We call AlignTextToFramePadding() to vertically align the text baseline by +FramePadding.y"); // SmallButton() uses the same vertical padding as Text @@ -4360,7 +4421,7 @@ static void ShowDemoWindowLayout() const ImGuiWindowFlags child_flags = enable_extra_decorations ? ImGuiWindowFlags_MenuBar : 0; const ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); - const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), ImGuiChildFlags_Border, child_flags); + const bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(child_w, 200.0f), ImGuiChildFlags_Borders, child_flags); if (ImGui::BeginMenuBar()) { ImGui::TextUnformatted("abc"); @@ -4407,7 +4468,7 @@ static void ShowDemoWindowLayout() float child_height = ImGui::GetTextLineHeight() + style.ScrollbarSize + style.WindowPadding.y * 2.0f; ImGuiWindowFlags child_flags = ImGuiWindowFlags_HorizontalScrollbar | (enable_extra_decorations ? ImGuiWindowFlags_AlwaysVerticalScrollbar : 0); ImGuiID child_id = ImGui::GetID((void*)(intptr_t)i); - bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), ImGuiChildFlags_Border, child_flags); + bool child_is_visible = ImGui::BeginChild(child_id, ImVec2(-100, child_height), ImGuiChildFlags_Borders, child_flags); if (scroll_to_off) ImGui::SetScrollX(scroll_to_off_px); if (scroll_to_pos) @@ -4449,7 +4510,7 @@ static void ShowDemoWindowLayout() ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(2.0f, 1.0f)); ImVec2 scrolling_child_size = ImVec2(0, ImGui::GetFrameHeightWithSpacing() * 7 + 30); - ImGui::BeginChild("scrolling", scrolling_child_size, ImGuiChildFlags_Border, ImGuiWindowFlags_HorizontalScrollbar); + ImGui::BeginChild("scrolling", scrolling_child_size, ImGuiChildFlags_Borders, ImGuiWindowFlags_HorizontalScrollbar); for (int line = 0; line < lines; line++) { // Display random stuff. For the sake of this trivial demo we are using basic Button() + SameLine() @@ -4595,7 +4656,7 @@ static void ShowDemoWindowLayout() } if (show_child) { - ImGui::BeginChild("child", ImVec2(0, 0), ImGuiChildFlags_Border); + ImGui::BeginChild("child", ImVec2(0, 0), ImGuiChildFlags_Borders); ImGui::EndChild(); } ImGui::End(); @@ -5081,8 +5142,8 @@ const ImGuiTableSortSpecs* MyItem::s_current_sort_specs = NULL; static void PushStyleCompact() { ImGuiStyle& style = ImGui::GetStyle(); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(style.FramePadding.x, (float)(int)(style.FramePadding.y * 0.60f))); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x, (float)(int)(style.ItemSpacing.y * 0.60f))); + ImGui::PushStyleVarY(ImGuiStyleVar_FramePadding, (float)(int)(style.FramePadding.y * 0.60f)); + ImGui::PushStyleVarY(ImGuiStyleVar_ItemSpacing, (float)(int)(style.ItemSpacing.y * 0.60f)); } static void PopStyleCompact() @@ -6116,7 +6177,7 @@ static void ShowDemoWindowTables() for (int row = 0; row < 8; row++) { if ((row % 3) == 2) - ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(style.CellPadding.x, 20.0f)); + ImGui::PushStyleVarY(ImGuiStyleVar_CellPadding, 20.0f); ImGui::TableNextRow(ImGuiTableRowFlags_None); ImGui::TableNextColumn(); ImGui::Text("CellPadding.y = %.2f", style.CellPadding.y); @@ -6395,7 +6456,11 @@ static void ShowDemoWindowTables() // FIXME: It would be nice to actually demonstrate full-featured selection using those checkbox. static bool column_selected[3] = {}; - // Instead of calling TableHeadersRow() we'll submit custom headers ourselves + // Instead of calling TableHeadersRow() we'll submit custom headers ourselves. + // (A different approach is also possible: + // - Specify ImGuiTableColumnFlags_NoHeaderLabel in some TableSetupColumn() call. + // - Call TableHeadersRow() normally. This will submit TableHeader() with no name. + // - Then call TableSetColumnIndex() to position yourself in the column and submit your stuff e.g. Checkbox().) ImGui::TableNextRow(ImGuiTableRowFlags_Headers); for (int column = 0; column < COLUMNS_COUNT; column++) { @@ -6410,6 +6475,7 @@ static void ShowDemoWindowTables() ImGui::PopID(); } + // Submit table contents for (int row = 0; row < 5; row++) { ImGui::TableNextRow(); @@ -6444,6 +6510,7 @@ static void ShowDemoWindowTables() ImGui::CheckboxFlags("_ScrollX", &table_flags, ImGuiTableFlags_ScrollX); ImGui::CheckboxFlags("_ScrollY", &table_flags, ImGuiTableFlags_ScrollY); ImGui::CheckboxFlags("_Resizable", &table_flags, ImGuiTableFlags_Resizable); + ImGui::CheckboxFlags("_Sortable", &table_flags, ImGuiTableFlags_Sortable); ImGui::CheckboxFlags("_NoBordersInBody", &table_flags, ImGuiTableFlags_NoBordersInBody); ImGui::CheckboxFlags("_HighlightHoveredColumn", &table_flags, ImGuiTableFlags_HighlightHoveredColumn); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 8); @@ -7134,12 +7201,14 @@ static void ShowDemoWindowColumns() { if (h_borders && ImGui::GetColumnIndex() == 0) ImGui::Separator(); + ImGui::PushID(i); ImGui::Text("%c%c%c", 'a' + i, 'a' + i, 'a' + i); ImGui::Text("Width %.2f", ImGui::GetColumnWidth()); ImGui::Text("Avail %.2f", ImGui::GetContentRegionAvail().x); ImGui::Text("Offset %.2f", ImGui::GetColumnOffset()); ImGui::Text("Long text that is likely to clip"); ImGui::Button("Button", ImVec2(-FLT_MIN, 0.0f)); + ImGui::PopID(); ImGui::NextColumn(); } ImGui::Columns(1); @@ -7462,7 +7531,8 @@ static void ShowDemoWindowInputs() IM_ASSERT(IM_ARRAYSIZE(mouse_cursors_names) == ImGuiMouseCursor_COUNT); ImGuiMouseCursor current = ImGui::GetMouseCursor(); - ImGui::Text("Current mouse cursor = %d: %s", current, mouse_cursors_names[current]); + const char* cursor_name = (current >= ImGuiMouseCursor_Arrow) && (current < ImGuiMouseCursor_COUNT) ? mouse_cursors_names[current] : "N/A"; + ImGui::Text("Current mouse cursor = %d: %s", current, cursor_name); ImGui::BeginDisabled(true); ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &io.BackendFlags, ImGuiBackendFlags_HasMouseCursors); ImGui::EndDisabled(); @@ -7687,6 +7757,7 @@ void ImGui::ShowAboutWindow(bool* p_open) #endif #ifdef __EMSCRIPTEN__ ImGui::Text("define: __EMSCRIPTEN__"); + ImGui::Text("Emscripten: %d.%d.%d", __EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__); #endif ImGui::Separator(); ImGui::Text("io.BackendPlatformName: %s", io.BackendPlatformName ? io.BackendPlatformName : "NULL"); @@ -7694,12 +7765,13 @@ void ImGui::ShowAboutWindow(bool* p_open) ImGui::Text("io.ConfigFlags: 0x%08X", io.ConfigFlags); if (io.ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard) ImGui::Text(" NavEnableKeyboard"); if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) ImGui::Text(" NavEnableGamepad"); - if (io.ConfigFlags & ImGuiConfigFlags_NavEnableSetMousePos) ImGui::Text(" NavEnableSetMousePos"); - if (io.ConfigFlags & ImGuiConfigFlags_NavNoCaptureKeyboard) ImGui::Text(" NavNoCaptureKeyboard"); if (io.ConfigFlags & ImGuiConfigFlags_NoMouse) ImGui::Text(" NoMouse"); if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) ImGui::Text(" NoMouseCursorChange"); + if (io.ConfigFlags & ImGuiConfigFlags_NoKeyboard) ImGui::Text(" NoKeyboard"); if (io.MouseDrawCursor) ImGui::Text("io.MouseDrawCursor"); if (io.ConfigMacOSXBehaviors) ImGui::Text("io.ConfigMacOSXBehaviors"); + if (io.ConfigNavMoveSetMousePos) ImGui::Text("io.ConfigNavMoveSetMousePos"); + if (io.ConfigNavCaptureKeyboard) ImGui::Text("io.ConfigNavCaptureKeyboard"); if (io.ConfigInputTextCursorBlink) ImGui::Text("io.ConfigInputTextCursorBlink"); if (io.ConfigWindowsResizeFromEdges) ImGui::Text("io.ConfigWindowsResizeFromEdges"); if (io.ConfigWindowsMoveFromTitleBarOnly) ImGui::Text("io.ConfigWindowsMoveFromTitleBarOnly"); @@ -7941,7 +8013,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) "Right-click to open edit options menu."); ImGui::SetNextWindowSizeConstraints(ImVec2(0.0f, ImGui::GetTextLineHeightWithSpacing() * 10), ImVec2(FLT_MAX, FLT_MAX)); - ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Border | ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); + ImGui::BeginChild("##colors", ImVec2(0, 0), ImGuiChildFlags_Borders | ImGuiChildFlags_NavFlattened, ImGuiWindowFlags_AlwaysVerticalScrollbar | ImGuiWindowFlags_AlwaysHorizontalScrollbar); ImGui::PushItemWidth(ImGui::GetFontSize() * -12); for (int i = 0; i < ImGuiCol_COUNT; i++) { @@ -8175,7 +8247,7 @@ static void ShowExampleMenuFile() { static bool enabled = true; ImGui::MenuItem("Enabled", "", &enabled); - ImGui::BeginChild("child", ImVec2(0, 60), ImGuiChildFlags_Border); + ImGui::BeginChild("child", ImVec2(0, 60), ImGuiChildFlags_Borders); for (int i = 0; i < 10; i++) ImGui::Text("Scrolling Text %d", i); ImGui::EndChild(); @@ -8772,7 +8844,7 @@ static void ShowExampleAppLayout(bool* p_open) // Left static int selected = 0; { - ImGui::BeginChild("left pane", ImVec2(150, 0), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeX); + ImGui::BeginChild("left pane", ImVec2(150, 0), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeX); for (int i = 0; i < 100; i++) { // FIXME: Good candidate to use ImGuiSelectableFlags_SelectOnNav @@ -8833,7 +8905,7 @@ struct ExampleAppPropertyEditor { // Left side: draw tree // - Currently using a table to benefit from RowBg feature - if (ImGui::BeginChild("##tree", ImVec2(300, 0), ImGuiChildFlags_ResizeX | ImGuiChildFlags_Border | ImGuiChildFlags_NavFlattened)) + if (ImGui::BeginChild("##tree", ImVec2(300, 0), ImGuiChildFlags_ResizeX | ImGuiChildFlags_Borders | ImGuiChildFlags_NavFlattened)) { ImGui::SetNextItemWidth(-FLT_MIN); ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_F, ImGuiInputFlags_Tooltip); @@ -9447,7 +9519,7 @@ static void ShowExampleAppCustomRendering(bool* p_open) // To use a child window instead we could use, e.g: // ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Disable padding // ImGui::PushStyleColor(ImGuiCol_ChildBg, IM_COL32(50, 50, 50, 255)); // Set a background color - // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove); + // ImGui::BeginChild("canvas", ImVec2(0.0f, 0.0f), ImGuiChildFlags_Borders, ImGuiWindowFlags_NoMove); // ImGui::PopStyleColor(); // ImGui::PopStyleVar(); // [...] @@ -10099,7 +10171,7 @@ struct ExampleAssetsBrowser ImGuiIO& io = ImGui::GetIO(); ImGui::SetNextWindowContentSize(ImVec2(0.0f, LayoutOuterPadding + LayoutLineCount * (LayoutItemSize.x + LayoutItemSpacing))); - if (ImGui::BeginChild("Assets", ImVec2(0.0f, -ImGui::GetTextLineHeightWithSpacing()), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove)) + if (ImGui::BeginChild("Assets", ImVec2(0.0f, -ImGui::GetTextLineHeightWithSpacing()), ImGuiChildFlags_Borders, ImGuiWindowFlags_NoMove)) { ImDrawList* draw_list = ImGui::GetWindowDrawList(); diff --git a/src/imgui/imgui_draw.cpp b/src/imgui/imgui_draw.cpp index 57486a6e..2fba4993 100644 --- a/src/imgui/imgui_draw.cpp +++ b/src/imgui/imgui_draw.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (drawing and font code) /* @@ -230,7 +230,7 @@ void ImGui::StyleColorsDark(ImGuiStyle* dst) colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); - colors[ImGuiCol_NavHighlight] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); + colors[ImGuiCol_NavCursor] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); @@ -293,7 +293,7 @@ void ImGui::StyleColorsClassic(ImGuiStyle* dst) colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.00f, 0.00f, 1.00f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); - colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); @@ -357,7 +357,7 @@ void ImGui::StyleColorsLight(ImGuiStyle* dst) colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); colors[ImGuiCol_DragDropTarget] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f); - colors[ImGuiCol_NavHighlight] = colors[ImGuiCol_HeaderHovered]; + colors[ImGuiCol_NavCursor] = colors[ImGuiCol_HeaderHovered]; colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.70f, 0.70f, 0.70f, 0.70f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.20f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f); @@ -414,6 +414,7 @@ void ImDrawList::_ResetForNewFrame() _IdxWritePtr = NULL; _ClipRectStack.resize(0); _TextureIdStack.resize(0); + _CallbacksDataBuf.resize(0); _Path.resize(0); _Splitter.Clear(); CmdBuffer.push_back(ImDrawCmd()); @@ -431,6 +432,7 @@ void ImDrawList::_ClearFreeMemory() _IdxWritePtr = NULL; _ClipRectStack.clear(); _TextureIdStack.clear(); + _CallbacksDataBuf.clear(); _Path.clear(); _Splitter.ClearFreeMemory(); } @@ -470,7 +472,7 @@ void ImDrawList::_PopUnusedDrawCmd() } } -void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) +void ImDrawList::AddCallback(ImDrawCallback callback, void* userdata, size_t userdata_size) { IM_ASSERT_PARANOID(CmdBuffer.Size > 0); ImDrawCmd* curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; @@ -480,8 +482,26 @@ void ImDrawList::AddCallback(ImDrawCallback callback, void* callback_data) AddDrawCmd(); curr_cmd = &CmdBuffer.Data[CmdBuffer.Size - 1]; } + curr_cmd->UserCallback = callback; - curr_cmd->UserCallbackData = callback_data; + if (userdata_size == 0) + { + // Store user data directly in command (no indirection) + curr_cmd->UserCallbackData = userdata; + curr_cmd->UserCallbackDataSize = 0; + curr_cmd->UserCallbackDataOffset = -1; + } + else + { + // Copy and store user data in a buffer + IM_ASSERT(userdata != NULL); + IM_ASSERT(userdata_size < (1u << 31)); + curr_cmd->UserCallbackData = NULL; // Will be resolved during Render() + curr_cmd->UserCallbackDataSize = (int)userdata_size; + curr_cmd->UserCallbackDataOffset = _CallbacksDataBuf.Size; + _CallbacksDataBuf.resize(_CallbacksDataBuf.Size + (int)userdata_size); + memcpy(_CallbacksDataBuf.Data + (size_t)curr_cmd->UserCallbackDataOffset, userdata, userdata_size); + } AddDrawCmd(); // Force a new command after us (see comment below) } @@ -526,7 +546,6 @@ void ImDrawList::_OnChangedClipRect() CmdBuffer.pop_back(); return; } - curr_cmd->ClipRect = _CmdHeader.ClipRect; } @@ -549,7 +568,6 @@ void ImDrawList::_OnChangedTextureID() CmdBuffer.pop_back(); return; } - curr_cmd->TextureId = _CmdHeader.TextureId; } @@ -625,6 +643,15 @@ void ImDrawList::PopTextureID() _OnChangedTextureID(); } +// This is used by ImGui::PushFont()/PopFont(). It works because we never use _TextureIdStack[] elsewhere than in PushTextureID()/PopTextureID(). +void ImDrawList::_SetTextureID(ImTextureID texture_id) +{ + if (_CmdHeader.TextureId == texture_id) + return; + _CmdHeader.TextureId = texture_id; + _OnChangedTextureID(); +} + // Reserve space for a number of vertices and indices. // You must finish filling your reserved data before calling PrimReserve() again, as it may reallocate or // submit the intermediate results. PrimUnreserve() can be used to release unused allocations. @@ -2215,6 +2242,12 @@ void ImGui::AddDrawListToDrawDataEx(ImDrawData* draw_data, ImVector if (sizeof(ImDrawIdx) == 2) IM_ASSERT(draw_list->_VtxCurrentIdx < (1 << 16) && "Too many vertices in ImDrawList using 16-bit indices. Read comment above"); + // Resolve callback data pointers + if (draw_list->_CallbacksDataBuf.Size > 0) + for (ImDrawCmd& cmd : draw_list->CmdBuffer) + if (cmd.UserCallback != NULL && cmd.UserCallbackDataOffset != -1 && cmd.UserCallbackDataSize > 0) + cmd.UserCallbackData = draw_list->_CallbacksDataBuf.Data + cmd.UserCallbackDataOffset; + // Add to output list + records state in ImDrawData out_list->push_back(draw_list); draw_data->CmdListsCount++; @@ -2492,13 +2525,14 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) { IM_ASSERT(!Locked && "Cannot modify a locked ImFontAtlas between NewFrame() and EndFrame/Render()!"); IM_ASSERT(font_cfg->FontData != NULL && font_cfg->FontDataSize > 0); - IM_ASSERT(font_cfg->SizePixels > 0.0f); + IM_ASSERT(font_cfg->SizePixels > 0.0f && "Is ImFontConfig struct correctly initialized?"); + IM_ASSERT(font_cfg->OversampleH > 0 && font_cfg->OversampleV > 0 && "Is ImFontConfig struct correctly initialized?"); // Create new font if (!font_cfg->MergeMode) Fonts.push_back(IM_NEW(ImFont)); else - IM_ASSERT(!Fonts.empty() && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font. + IM_ASSERT(Fonts.Size > 0 && "Cannot use MergeMode for the first font"); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font. ConfigData.push_back(*font_cfg); ImFontConfig& new_font_cfg = ConfigData.back(); @@ -2815,8 +2849,9 @@ static bool ImFontAtlasBuildWithStbTruetype(ImFontAtlas* atlas) for (const ImWchar* src_range = src_tmp.SrcRanges; src_range[0] && src_range[1]; src_range += 2) { // Check for valid range. This may also help detect *some* dangling pointers, because a common - // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent. - IM_ASSERT(src_range[0] <= src_range[1]); + // user error is to setup ImFontConfig::GlyphRanges with a pointer to data that isn't persistent, + // or to forget to zero-terminate the glyph range array. + IM_ASSERT(src_range[0] <= src_range[1] && "Invalid range: is your glyph range array persistent? it is zero-terminated?"); src_tmp.GlyphsHighest = ImMax(src_tmp.GlyphsHighest, (int)src_range[1]); } dst_tmp.SrcCount++; diff --git a/src/imgui/imgui_impl_glfw.cpp b/src/imgui/imgui_impl_glfw.cpp index 30589162..c725e9c0 100644 --- a/src/imgui/imgui_impl_glfw.cpp +++ b/src/imgui/imgui_impl_glfw.cpp @@ -18,8 +18,18 @@ // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). // - Introduction, links and more at the top of imgui.cpp +// About Emscripten support: +// - Emscripten provides its own GLFW (3.2.1) implementation (syntax: "-sUSE_GLFW=3"), but Joystick is broken and several features are not supported (multiple windows, clipboard, timer, etc.) +// - A third-party Emscripten GLFW (3.4.0) implementation (syntax: "--use-port=contrib.glfw3") fixes the Joystick issue and implements all relevant features for the browser. +// See https://github.com/pongasoft/emscripten-glfw/blob/master/docs/Comparison.md for details. + // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO: +// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn +// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn +// - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn +// 2024-07-31: Added ImGui_ImplGlfw_Sleep() helper function for usage by our examples app, since GLFW doesn't provide one. // 2024-07-08: *BREAKING* Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWWindow* parameter. // 2024-07-08: Emscripten: Added support for GLFW3 contrib port (GLFW 3.4.0 features + bug fixes): to enable, replace -sUSE_GLFW=3 with --use-port=contrib.glfw3 (requires emscripten 3.1.59+) (https://github.com/pongasoft/emscripten-glfw) // 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions. @@ -99,6 +109,9 @@ #endif #include // for glfwGetCocoaWindow() #endif +#ifndef _WIN32 +#include // for usleep() +#endif #ifdef __EMSCRIPTEN__ #include @@ -172,19 +185,12 @@ static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData() } // Functions -static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data) -{ - return glfwGetClipboardString((GLFWwindow*)user_data); -} - -static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) -{ - glfwSetClipboardString((GLFWwindow*)user_data, text); -} -static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) +// Not static to allow third-party code to use that if they want to (but undocumented) +ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int keycode, int scancode) { - switch (key) + IM_UNUSED(scancode); + switch (keycode) { case GLFW_KEY_TAB: return ImGuiKey_Tab; case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; @@ -352,6 +358,7 @@ void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yo io.AddMouseWheelEvent((float)xoffset, (float)yoffset); } +// FIXME: should this be baked into ImGui_ImplGlfw_KeyToImGuiKey()? then what about the values passed to io.SetKeyEventNativeData()? static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) { #if GLFW_HAS_GETKEYNAME && !defined(EMSCRIPTEN_USE_EMBEDDED_GLFW3) @@ -399,7 +406,7 @@ void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, i keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode); ImGuiIO& io = ImGui::GetIO(); - ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode); + ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode, scancode); io.AddKeyEvent(imgui_key, (action == GLFW_PRESS)); io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code) } @@ -562,7 +569,11 @@ void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows) } #ifdef __EMSCRIPTEN__ -EM_JS(void, ImGui_ImplGlfw_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); +#if EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3 >= 34020240817 +void ImGui_ImplGlfw_EmscriptenOpenURL(const char* url) { if (url) emscripten::glfw3::OpenURL(url); } +#else +EM_JS(void, ImGui_ImplGlfw_EmscriptenOpenURL, (const char* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); +#endif #endif static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) @@ -582,11 +593,11 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw bd->Window = window; bd->Time = 0.0; - io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; - io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; - io.ClipboardUserData = bd->Window; + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Platform_SetClipboardTextFn = [](ImGuiContext*, const char* text) { glfwSetClipboardString(NULL, text); }; + platform_io.Platform_GetClipboardTextFn = [](ImGuiContext*) { return glfwGetClipboardString(NULL); }; #ifdef __EMSCRIPTEN__ - io.PlatformOpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplGlfw_EmscriptenOpenURL(url); return true; }; + platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplGlfw_EmscriptenOpenURL(url); return true; }; #endif // Create mouse cursors @@ -637,6 +648,23 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); #endif + // Emscripten: the same application can run on various platforms, so we detect the Apple platform at runtime + // to override io.ConfigMacOSXBehaviors from its default (which is always false in Emscripten). +#ifdef __EMSCRIPTEN__ +#if EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3 >= 34020240817 + if (emscripten::glfw3::IsRuntimePlatformApple()) + { + ImGui::GetIO().ConfigMacOSXBehaviors = true; + + // Due to how the browser (poorly) handles the Meta Key, this line essentially disables repeats when used. + // This means that Meta + V only registers a single key-press, even if the keys are held. + // This is a compromise for dealing with this issue in ImGui since ImGui implements key repeat itself. + // See https://github.com/pongasoft/emscripten-glfw/blob/v3.4.0.20240817/docs/Usage.md#the-problem-of-the-super-key + emscripten::glfw3::SetSuperPlusKeyTimeouts(10, 10); + } +#endif +#endif + bd->ClientApi = client_api; return true; } @@ -700,7 +728,7 @@ static void ImGui_ImplGlfw_UpdateMouseData() #endif if (is_window_focused) { - // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) + // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user) if (io.WantSetMousePos) glfwSetCursorPos(window, (double)io.MousePos.x, (double)io.MousePos.y); @@ -825,6 +853,16 @@ void ImGui_ImplGlfw_NewFrame() ImGui_ImplGlfw_UpdateGamepads(); } +// GLFW doesn't provide a portable sleep function +void ImGui_ImplGlfw_Sleep(int milliseconds) +{ +#ifdef _WIN32 + ::Sleep(milliseconds); +#else + usleep(milliseconds * 1000); +#endif +} + #ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3 static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data) { diff --git a/src/imgui/imgui_impl_opengl3.cpp b/src/imgui/imgui_impl_opengl3.cpp index 7087a7c5..f5235096 100644 --- a/src/imgui/imgui_impl_opengl3.cpp +++ b/src/imgui/imgui_impl_opengl3.cpp @@ -22,6 +22,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap. // 2024-06-28: OpenGL: ImGui_ImplOpenGL3_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL3_DestroyFontsTexture(). (#7748) // 2024-05-07: OpenGL: Update loader for Linux to support EGL/GLVND. (#7562) // 2024-04-16: OpenGL: Detect ES3 contexts on desktop based on version string, to e.g. avoid calling glPolygonMode() on them. (#7447) @@ -550,7 +551,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // Render command lists for (int n = 0; n < draw_data->CmdListsCount; n++) { - const ImDrawList* cmd_list = draw_data->CmdLists[n]; + const ImDrawList* draw_list = draw_data->CmdLists[n]; // Upload vertex/index buffers // - OpenGL drivers are in a very sorry state nowadays.... @@ -560,8 +561,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code. // We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path. // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues. - const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); - const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); + const GLsizeiptr vtx_buffer_size = (GLsizeiptr)draw_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); + const GLsizeiptr idx_buffer_size = (GLsizeiptr)draw_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); if (bd->UseBufferSubData) { if (bd->VertexBufferSize < vtx_buffer_size) @@ -574,18 +575,18 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) bd->IndexBufferSize = idx_buffer_size; GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, nullptr, GL_STREAM_DRAW)); } - GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data)); - GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data)); + GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data)); + GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data)); } else { - GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW)); - GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW)); + GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data, GL_STREAM_DRAW)); + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data, GL_STREAM_DRAW)); } - for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) { - const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; if (pcmd->UserCallback != nullptr) { // User callback, registered via ImDrawList::AddCallback() @@ -593,7 +594,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); else - pcmd->UserCallback(cmd_list, pcmd); + pcmd->UserCallback(draw_list, pcmd); } else { @@ -681,6 +682,8 @@ bool ImGui_ImplOpenGL3_CreateFontsTexture() GL_CALL(glBindTexture(GL_TEXTURE_2D, bd->FontTexture)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); #ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); #endif diff --git a/src/imgui/imgui_tables.cpp b/src/imgui/imgui_tables.cpp index 37dd7030..598496be 100644 --- a/src/imgui/imgui_tables.cpp +++ b/src/imgui/imgui_tables.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (tables and columns code) /* @@ -328,7 +328,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG // - always performing the GetOrAddByKey() O(log N) query in g.Tables.Map[]. const bool use_child_window = (flags & (ImGuiTableFlags_ScrollX | ImGuiTableFlags_ScrollY)) != 0; const ImVec2 avail_size = GetContentRegionAvail(); - const ImVec2 actual_outer_size = CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f); + const ImVec2 actual_outer_size = ImTrunc(CalcItemSize(outer_size, ImMax(avail_size.x, 1.0f), use_child_window ? ImMax(avail_size.y, 1.0f) : 0.0f)); const ImRect outer_rect(outer_window->DC.CursorPos, outer_window->DC.CursorPos + actual_outer_size); const bool outer_window_is_measuring_size = (outer_window->AutoFitFramesX > 0) || (outer_window->AutoFitFramesY > 0); // Doesn't apply to AlwaysAutoResize windows! if (use_child_window && IsClippedEx(outer_rect, 0) && !outer_window_is_measuring_size) @@ -460,16 +460,27 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG temp_data->HostBackupItemWidthStackSize = outer_window->DC.ItemWidthStack.Size; inner_window->DC.PrevLineSize = inner_window->DC.CurrLineSize = ImVec2(0.0f, 0.0f); - // Make left and top borders not overlap our contents by offsetting HostClipRect (#6765) + // Make borders not overlap our contents by offsetting HostClipRect (#6765, #7428, #3752) // (we normally shouldn't alter HostClipRect as we rely on TableMergeDrawChannels() expanding non-clipped column toward the // limits of that rectangle, in order for ImDrawListSplitter::Merge() to merge the draw commands. However since the overlap // problem only affect scrolling tables in this case we can get away with doing it without extra cost). if (inner_window != outer_window) { + // FIXME: Because inner_window's Scrollbar doesn't know about border size, since it's not encoded in window->WindowBorderSize, + // it already overlaps it and doesn't need an extra offset. Ideally we should be able to pass custom border size with + // different x/y values to BeginChild(). if (flags & ImGuiTableFlags_BordersOuterV) + { table->HostClipRect.Min.x = ImMin(table->HostClipRect.Min.x + TABLE_BORDER_SIZE, table->HostClipRect.Max.x); + if (inner_window->DecoOuterSizeX2 == 0.0f) + table->HostClipRect.Max.x = ImMax(table->HostClipRect.Max.x - TABLE_BORDER_SIZE, table->HostClipRect.Min.x); + } if (flags & ImGuiTableFlags_BordersOuterH) + { table->HostClipRect.Min.y = ImMin(table->HostClipRect.Min.y + TABLE_BORDER_SIZE, table->HostClipRect.Max.y); + if (inner_window->DecoOuterSizeY2 == 0.0f) + table->HostClipRect.Max.y = ImMax(table->HostClipRect.Max.y - TABLE_BORDER_SIZE, table->HostClipRect.Min.y); + } } // Padding and Spacing @@ -497,7 +508,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG table->InnerClipRect = (inner_window == outer_window) ? table->WorkRect : inner_window->ClipRect; table->InnerClipRect.ClipWith(table->WorkRect); // We need this to honor inner_width table->InnerClipRect.ClipWithFull(table->HostClipRect); - table->InnerClipRect.Max.y = (flags & ImGuiTableFlags_NoHostExtendY) ? ImMin(table->InnerClipRect.Max.y, inner_window->WorkRect.Max.y) : inner_window->ClipRect.Max.y; + table->InnerClipRect.Max.y = (flags & ImGuiTableFlags_NoHostExtendY) ? ImMin(table->InnerClipRect.Max.y, inner_window->WorkRect.Max.y) : table->HostClipRect.Max.y; table->RowPosY1 = table->RowPosY2 = table->WorkRect.Min.y; // This is needed somehow table->RowTextBaseline = 0.0f; // This will be cleared again by TableBeginRow() @@ -855,7 +866,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping) // Combine width from regular rows + width from headers unless requested not to. - if (!column->IsPreserveWidthAuto) + if (!column->IsPreserveWidthAuto && table->InstanceCurrent == 0) column->WidthAuto = TableGetColumnWidthAuto(table, column); // Non-resizable columns keep their requested width (apply user value regardless of IsPreserveWidthAuto) @@ -1059,16 +1070,12 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) continue; } - // Detect hovered column - if (is_hovering_table && mouse_skewed_x >= column->ClipRect.Min.x && mouse_skewed_x < column->ClipRect.Max.x) - table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n; - // Lock start position column->MinX = offset_x; // Lock width based on start position and minimum/maximum width for this position - float max_width = TableGetMaxColumnWidth(table, column_n); - column->WidthGiven = ImMin(column->WidthGiven, max_width); + column->WidthMax = TableCalcMaxColumnWidth(table, column_n); + column->WidthGiven = ImMin(column->WidthGiven, column->WidthMax); column->WidthGiven = ImMax(column->WidthGiven, ImMin(column->WidthRequest, table->MinColumnWidth)); column->MaxX = offset_x + column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f; @@ -1117,8 +1124,13 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) column->Flags |= ImGuiTableColumnFlags_IsVisible; if (column->SortOrder != -1) column->Flags |= ImGuiTableColumnFlags_IsSorted; - if (table->HoveredColumnBody == column_n) + + // Detect hovered column + if (is_hovering_table && mouse_skewed_x >= column->ClipRect.Min.x && mouse_skewed_x < column->ClipRect.Max.x) + { column->Flags |= ImGuiTableColumnFlags_IsHovered; + table->HoveredColumnBody = (ImGuiTableColumnIdx)column_n; + } // Alignment // FIXME-TABLE: This align based on the whole column width, not per-cell, and therefore isn't useful in @@ -1148,7 +1160,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } // Don't decrement auto-fit counters until container window got a chance to submit its items - if (table->HostSkipItems == false) + if (table->HostSkipItems == false && table->InstanceCurrent == 0) { column->AutoFitQueue >>= 1; column->CannotSkipItemsQueue >>= 1; @@ -1249,7 +1261,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (table->Flags & ImGuiTableFlags_NoClip) table->DrawSplitter->SetCurrentChannel(inner_window->DrawList, TABLE_DRAW_CHANNEL_NOCLIP); else - inner_window->DrawList->PushClipRect(inner_window->ClipRect.Min, inner_window->ClipRect.Max, false); + inner_window->DrawList->PushClipRect(inner_window->InnerClipRect.Min, inner_window->InnerClipRect.Max, false); // FIXME: use table->InnerClipRect? } // Process hit-testing on resizing borders. Actual size change will be applied in EndTable() @@ -1472,7 +1484,9 @@ void ImGui::EndTable() { short backup_nav_layers_active_mask = inner_window->DC.NavLayersActiveMask; inner_window->DC.NavLayersActiveMask |= 1 << ImGuiNavLayer_Main; // So empty table don't appear to navigate differently. + g.CurrentTable = NULL; // To avoid error recovery recursing EndChild(); + g.CurrentTable = table; inner_window->DC.NavLayersActiveMask = backup_nav_layers_active_mask; } else @@ -1999,7 +2013,7 @@ void ImGui::TableEndRow(ImGuiTable* table) { for (int column_n = 0; column_n < table->ColumnsCount; column_n++) table->Columns[column_n].NavLayerCurrent = ImGuiNavLayer_Main; - const float y0 = ImMax(table->RowPosY2 + 1, window->InnerClipRect.Min.y); + const float y0 = ImMax(table->RowPosY2 + 1, table->InnerClipRect.Min.y); table_instance->LastFrozenHeight = y0 - table->OuterRect.Min.y; if (unfreeze_rows_actual) @@ -2008,8 +2022,8 @@ void ImGui::TableEndRow(ImGuiTable* table) table->IsUnfrozenRows = true; // BgClipRect starts as table->InnerClipRect, reduce it now and make BgClipRectForDrawCmd == BgClipRect - table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, window->InnerClipRect.Max.y); - table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = window->InnerClipRect.Max.y; + table->BgClipRect.Min.y = table->Bg2ClipRectForDrawCmd.Min.y = ImMin(y0, table->InnerClipRect.Max.y); + table->BgClipRect.Max.y = table->Bg2ClipRectForDrawCmd.Max.y = table->InnerClipRect.Max.y; table->Bg2DrawChannelCurrent = table->Bg2DrawChannelUnfrozen; IM_ASSERT(table->Bg2ClipRectForDrawCmd.Min.y <= table->Bg2ClipRectForDrawCmd.Max.y); @@ -2195,8 +2209,8 @@ void ImGui::TableEndCell(ImGuiTable* table) // Note that actual columns widths are computed in TableUpdateLayout(). //------------------------------------------------------------------------- -// Maximum column content width given current layout. Use column->MinX so this value on a per-column basis. -float ImGui::TableGetMaxColumnWidth(const ImGuiTable* table, int column_n) +// Maximum column content width given current layout. Use column->MinX so this value differs on a per-column basis. +float ImGui::TableCalcMaxColumnWidth(const ImGuiTable* table, int column_n) { const ImGuiTableColumn* column = &table->Columns[column_n]; float max_width = FLT_MAX; @@ -2258,7 +2272,7 @@ void ImGui::TableSetColumnWidth(int column_n, float width) // Compare both requested and actual given width to avoid overwriting requested width when column is stuck (minimum size, bounded) IM_ASSERT(table->MinColumnWidth > 0.0f); const float min_width = table->MinColumnWidth; - const float max_width = ImMax(min_width, TableGetMaxColumnWidth(table, column_n)); + const float max_width = ImMax(min_width, column_0->WidthMax); // Don't use TableCalcMaxColumnWidth() here as it would rely on MinX from last instance (#7933) column_0_width = ImClamp(column_0_width, min_width, max_width); if (column_0->WidthGiven == column_0_width || column_0->WidthRequest == column_0_width) return; @@ -2743,7 +2757,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table) const ImU32 outer_col = table->BorderColorStrong; if ((table->Flags & ImGuiTableFlags_BordersOuter) == ImGuiTableFlags_BordersOuter) { - inner_drawlist->AddRect(outer_border.Min, outer_border.Max + ImVec2(1, 1), outer_col, 0.0f, 0, border_size); + inner_drawlist->AddRect(outer_border.Min, outer_border.Max, outer_col, 0.0f, 0, border_size); } else if (table->Flags & ImGuiTableFlags_BordersOuterV) { @@ -3007,7 +3021,8 @@ float ImGui::TableGetHeaderAngledMaxLabelWidth() // The intent is that advanced users willing to create customized headers would not need to use this helper // and can create their own! For example: TableHeader() may be preceded by Checkbox() or other custom widgets. // See 'Demo->Tables->Custom headers' for a demonstration of implementing a custom version of this. -// This code is constructed to not make much use of internal functions, as it is intended to be a template to copy. +// This code is intentionally written to not make much use of internal functions, to give you better direction +// if you need to write your own. // FIXME-TABLE: TableOpenContextMenu() and TableGetHeaderRowHeight() are not public. void ImGui::TableHeadersRow() { @@ -3015,7 +3030,8 @@ void ImGui::TableHeadersRow() ImGuiTable* table = g.CurrentTable; IM_ASSERT(table != NULL && "Need to call TableHeadersRow() after BeginTable()!"); - // Layout if not already done (this is automatically done by TableNextRow, we do it here solely to facilitate stepping in debugger as it is frequent to step in TableUpdateLayout) + // Call layout if not already done. This is automatically done by TableNextRow: we do it here _only_ to make + // it easier to debug-step in TableUpdateLayout(). Your own version of this function doesn't need this. if (!table->IsLayoutLocked) TableUpdateLayout(table); @@ -3032,8 +3048,7 @@ void ImGui::TableHeadersRow() if (!TableSetColumnIndex(column_n)) continue; - // Push an id to allow unnamed labels (generally accidental, but let's behave nicely with them) - // In your own code you may omit the PushID/PopID all-together, provided you know they won't collide. + // Push an id to allow empty/unnamed headers. This is also idiomatic as it ensure there is a consistent ID path to access columns (for e.g. automation) const char* name = (TableGetColumnFlags(column_n) & ImGuiTableColumnFlags_NoHeaderLabel) ? "" : TableGetColumnName(column_n); PushID(column_n); TableHeader(name); @@ -3124,7 +3139,7 @@ void ImGui::TableHeader(const char* label) if ((table->RowFlags & ImGuiTableRowFlags_Headers) == 0) TableSetBgColor(ImGuiTableBgTarget_CellBg, GetColorU32(ImGuiCol_TableHeaderBg), table->CurrentColumn); } - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding); + RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact | ImGuiNavRenderCursorFlags_NoRounding); if (held) table->HeldHeaderColumn = (ImGuiTableColumnIdx)column_n; window->DC.CursorPos.y -= g.Style.ItemSpacing.y * 0.5f; @@ -4396,7 +4411,7 @@ void ImGui::EndColumns() { ButtonBehavior(column_hit_rect, column_id, &hovered, &held); if (hovered || held) - g.MouseCursor = ImGuiMouseCursor_ResizeEW; + SetMouseCursor(ImGuiMouseCursor_ResizeEW); if (held && !(column->Flags & ImGuiOldColumnFlags_NoResize)) dragging_column = n; } @@ -4428,12 +4443,12 @@ void ImGui::EndColumns() NavUpdateCurrentWindowIsScrollPushableX(); } -void ImGui::Columns(int columns_count, const char* id, bool border) +void ImGui::Columns(int columns_count, const char* id, bool borders) { ImGuiWindow* window = GetCurrentWindow(); IM_ASSERT(columns_count >= 1); - ImGuiOldColumnFlags flags = (border ? 0 : ImGuiOldColumnFlags_NoBorder); + ImGuiOldColumnFlags flags = (borders ? 0 : ImGuiOldColumnFlags_NoBorder); //flags |= ImGuiOldColumnFlags_NoPreserveWidths; // NB: Legacy behavior ImGuiOldColumns* columns = window->DC.CurrentColumns; if (columns != NULL && columns->Count == columns_count && columns->Flags == flags) diff --git a/src/imgui/imgui_widgets.cpp b/src/imgui/imgui_widgets.cpp index 2aa24efe..dea089c2 100644 --- a/src/imgui/imgui_widgets.cpp +++ b/src/imgui/imgui_widgets.cpp @@ -1,4 +1,4 @@ -// dear imgui, v1.91.0 +// dear imgui, v1.91.4 // (widgets code) /* @@ -128,7 +128,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1); // For InputTextEx() static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, bool input_source_is_clipboard = false); static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end); -static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); +static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end, const char** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false); //------------------------------------------------------------------------- // [SECTION] Widgets: Text, etc. @@ -482,9 +482,14 @@ void ImGui::BulletTextV(const char* fmt, va_list args) // Frame N + RepeatDelay + RepeatRate*N true true - true //------------------------------------------------------------------------------------------------------------------------------------------------- -// FIXME: For refactor we could output flags, incl mouse hovered vs nav keyboard vs nav triggered etc. -// And better standardize how widgets use 'GetColor32((held && hovered) ? ... : hovered ? ...)' vs 'GetColor32(held ? ... : hovered ? ...);' -// For mouse feedback we typically prefer the 'held && hovered' test, but for nav feedback not always. Outputting hovered=true on Activation may be misleading. +// - FIXME: For refactor we could output flags, incl mouse hovered vs nav keyboard vs nav triggered etc. +// And better standardize how widgets use 'GetColor32((held && hovered) ? ... : hovered ? ...)' vs 'GetColor32(held ? ... : hovered ? ...);' +// For mouse feedback we typically prefer the 'held && hovered' test, but for nav feedback not always. Outputting hovered=true on Activation may be misleading. +// - Since v1.91.2 (Sept 2024) we included io.ConfigDebugHighlightIdConflicts feature. +// One idiom which was previously valid which will now emit a warning is when using multiple overlayed ButtonBehavior() +// with same ID and different MouseButton (see #8030). You can fix it by: +// (1) switching to use a single ButtonBehavior() with multiple _MouseButton flags. +// or (2) surrounding those calls with PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); ... PopItemFlag() bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags) { ImGuiContext& g = *GImGui; @@ -500,11 +505,9 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool // Default behavior inherited from item flags // Note that _both_ ButtonFlags and ItemFlags are valid sources, so copy one into the item_flags and only check that. - ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.InFlags : g.CurrentItemFlags); + ImGuiItemFlags item_flags = (g.LastItemData.ID == id ? g.LastItemData.ItemFlags : g.CurrentItemFlags); if (flags & ImGuiButtonFlags_AllowOverlap) item_flags |= ImGuiItemFlags_AllowOverlap; - if (flags & ImGuiButtonFlags_Repeat) - item_flags |= ImGuiItemFlags_ButtonRepeat; ImGuiWindow* backup_hovered_window = g.HoveredWindow; const bool flatten_hovered_children = (flags & ImGuiButtonFlags_FlattenChildren) && g.HoveredWindow && g.HoveredWindow->RootWindow == window; @@ -556,7 +559,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool } // Process initial action - if (!(flags & ImGuiButtonFlags_NoKeyModifiers) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt)) + const bool mods_ok = !(flags & ImGuiButtonFlags_NoKeyModsAllowed) || (!g.IO.KeyCtrl && !g.IO.KeyShift && !g.IO.KeyAlt); + if (mods_ok) { if (mouse_button_clicked != -1 && g.ActiveId != id) { @@ -603,7 +607,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool if (!has_repeated_at_least_once) pressed = true; if (!(flags & ImGuiButtonFlags_NoNavFocus)) - SetFocusID(id, window); + SetFocusID(id, window); // FIXME: Lack of FocusWindow() call here is inconsistent with other paths. Research why. ClearActiveID(); } } @@ -615,13 +619,13 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool pressed = true; } - if (pressed) - g.NavDisableHighlight = true; + if (pressed && g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = false; } - // Gamepad/Keyboard handling + // Keyboard/Gamepad navigation handling // We report navigated and navigation-activated items as hovered but we don't set g.HoveredId to not interfere with mouse. - if (g.NavId == id && !g.NavDisableHighlight && g.NavDisableMouseHover) + if (g.NavId == id && g.NavCursorVisible && g.NavHighlightItemUnderNav) if (!(flags & ImGuiButtonFlags_NoHoveredOnFocus)) hovered = true; if (g.NavActivateDownId == id) @@ -684,8 +688,8 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool } ClearActiveID(); } - if (!(flags & ImGuiButtonFlags_NoNavFocus)) - g.NavDisableHighlight = true; + if (!(flags & ImGuiButtonFlags_NoNavFocus) && g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = false; } else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad) { @@ -735,7 +739,7 @@ bool ImGui::ButtonEx(const char* label, const ImVec2& size_arg, ImGuiButtonFlags // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); RenderFrame(bb.Min, bb.Max, col, true, style.FrameRounding); if (g.LogEnabled) @@ -782,11 +786,12 @@ bool ImGui::InvisibleButton(const char* str_id, const ImVec2& size_arg, ImGuiBut ImVec2 size = CalcItemSize(size_arg, 0.0f, 0.0f); const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); ItemSize(size); - if (!ItemAdd(bb, id)) + if (!ItemAdd(bb, id, NULL, (flags & ImGuiButtonFlags_EnableNav) ? ImGuiItemFlags_None : ImGuiItemFlags_NoNav)) return false; bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held, flags); + RenderNavCursor(bb, id); IMGUI_TEST_ENGINE_ITEM_INFO(id, str_id, g.LastItemData.StatusFlags); return pressed; @@ -812,7 +817,7 @@ bool ImGui::ArrowButtonEx(const char* str_id, ImGuiDir dir, ImVec2 size, ImGuiBu // Render const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); const ImU32 text_col = GetColorU32(ImGuiCol_Text); - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); RenderFrame(bb.Min, bb.Max, bg_col, true, g.Style.FrameRounding); RenderArrow(window->DrawList, bb.Min + ImVec2(ImMax(0.0f, (size.x - g.FontSize) * 0.5f), ImMax(0.0f, (size.y - g.FontSize) * 0.5f)), text_col, dir); @@ -853,7 +858,7 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) ImU32 bg_col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); if (hovered) window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact); + RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact); ImU32 cross_col = GetColorU32(ImGuiCol_Text); ImVec2 cross_center = bb.GetCenter() - ImVec2(0.5f, 0.5f); float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; @@ -880,7 +885,7 @@ bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) ImU32 text_col = GetColorU32(ImGuiCol_Text); if (hovered || held) window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_Compact); + RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact); RenderArrow(window->DrawList, bb.Min, text_col, window->Collapsed ? ImGuiDir_Right : ImGuiDir_Down, 1.0f); // Switch to moving the window after mouse is moved beyond the initial drag threshold @@ -999,9 +1004,10 @@ bool ImGui::ScrollbarEx(const ImRect& bb_frame, ImGuiID id, ImGuiAxis axis, ImS6 const int held_dir = (clicked_v_norm < grab_v_norm) ? -1 : (clicked_v_norm > grab_v_norm + grab_h_norm) ? +1 : 0; if (g.ActiveIdIsJustActivated) { - // On initial click calculate the distance between mouse and the center of the grab - g.ScrollbarSeekMode = (short)held_dir; - g.ScrollbarClickDeltaToGrabCenter = (g.ScrollbarSeekMode == 0.0f) ? clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f : 0.0f; + // On initial click when held_dir == 0 (clicked over grab): calculate the distance between mouse and the center of the grab + const bool scroll_to_clicked_location = (g.IO.ConfigScrollbarScrollByPage == false || g.IO.KeyShift || held_dir == 0); + g.ScrollbarSeekMode = scroll_to_clicked_location ? 0 : (short)held_dir; + g.ScrollbarClickDeltaToGrabCenter = (held_dir == 0 && !g.IO.KeyShift) ? clicked_v_norm - grab_v_norm - grab_h_norm * 0.5f : 0.0f; } // Apply scroll (p_scroll_v will generally point on one member of window->Scroll) @@ -1086,7 +1092,7 @@ bool ImGui::ImageButtonEx(ImGuiID id, ImTextureID texture_id, const ImVec2& imag // Render const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_ButtonActive : hovered ? ImGuiCol_ButtonHovered : ImGuiCol_Button); - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); RenderFrame(bb.Min, bb.Max, col, true, ImClamp((float)ImMin(padding.x, padding.y), 0.0f, g.Style.FrameRounding)); if (bg_col.w > 0.0f) window->DrawList->AddRectFilled(bb.Min + padding, bb.Max - padding, GetColorU32(bg_col)); @@ -1108,28 +1114,24 @@ bool ImGui::ImageButton(const char* str_id, ImTextureID user_texture_id, const I #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS // Legacy API obsoleted in 1.89. Two differences with new ImageButton() -// - new ImageButton() requires an explicit 'const char* str_id' Old ImageButton() used opaque imTextureId (created issue with: multiple buttons with same image, transient texture id values, opaque computation of ID) -// - new ImageButton() always use style.FramePadding Old ImageButton() had an override argument. -// If you need to change padding with new ImageButton() you can use PushStyleVar(ImGuiStyleVar_FramePadding, value), consistent with other Button functions. +// - old ImageButton() used ImTextureId as item id (created issue with multiple buttons with same image, transient texture id values, opaque computation of ID) +// - new ImageButton() requires an explicit 'const char* str_id' +// - old ImageButton() had frame_padding' override argument. +// - new ImageButton() always use style.FramePadding. +/* bool ImGui::ImageButton(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) { - ImGuiContext& g = *GImGui; - ImGuiWindow* window = g.CurrentWindow; - if (window->SkipItems) - return false; - // Default to using texture ID as ID. User can still push string/integer prefixes. - PushID((void*)(intptr_t)user_texture_id); - const ImGuiID id = window->GetID("#image"); - PopID(); - + PushID((ImTextureID)(intptr_t)user_texture_id); if (frame_padding >= 0) PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2((float)frame_padding, (float)frame_padding)); - bool ret = ImageButtonEx(id, user_texture_id, size, uv0, uv1, bg_col, tint_col); + bool ret = ImageButton("", user_texture_id, size, uv0, uv1, bg_col, tint_col); if (frame_padding >= 0) PopStyleVar(); + PopID(); return ret; } +*/ #endif // #ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS bool ImGui::Checkbox(const char* label, bool* v) @@ -1148,7 +1150,7 @@ bool ImGui::Checkbox(const char* label, bool* v) const ImRect total_bb(pos, pos + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f)); ItemSize(total_bb, style.FramePadding.y); const bool is_visible = ItemAdd(total_bb, id); - const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; + const bool is_multi_select = (g.LastItemData.ItemFlags & ImGuiItemFlags_IsMultiSelect) != 0; if (!is_visible) if (!is_multi_select || !g.BoxSelectState.UnclipMode || !g.BoxSelectState.UnclipRect.Overlaps(total_bb)) // Extra layer of "no logic clip" for box-select support { @@ -1178,10 +1180,10 @@ bool ImGui::Checkbox(const char* label, bool* v) } const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz)); - const bool mixed_value = (g.LastItemData.InFlags & ImGuiItemFlags_MixedValue) != 0; + const bool mixed_value = (g.LastItemData.ItemFlags & ImGuiItemFlags_MixedValue) != 0; if (is_visible) { - RenderNavHighlight(total_bb, id); + RenderNavCursor(total_bb, id); RenderFrame(check_bb.Min, check_bb.Max, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), true, style.FrameRounding); ImU32 check_col = GetColorU32(ImGuiCol_CheckMark); if (mixed_value) @@ -1283,7 +1285,7 @@ bool ImGui::RadioButton(const char* label, bool active) if (pressed) MarkItemEdited(id); - RenderNavHighlight(total_bb, id); + RenderNavCursor(total_bb, id); const int num_segment = window->DrawList->_CalcCircleAutoSegmentCount(radius); window->DrawList->AddCircleFilled(center, radius, GetColorU32((held && hovered) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg), num_segment); if (active) @@ -1423,7 +1425,10 @@ bool ImGui::TextLink(const char* label) bool hovered, held; bool pressed = ButtonBehavior(bb, id, &hovered, &held); - RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_None); + RenderNavCursor(bb, id); + + if (hovered) + SetMouseCursor(ImGuiMouseCursor_Hand); ImVec4 text_colf = g.Style.Colors[ImGuiCol_TextLink]; ImVec4 line_colf = text_colf; @@ -1459,9 +1464,9 @@ void ImGui::TextLinkOpenURL(const char* label, const char* url) if (url == NULL) url = label; if (TextLink(label)) - if (g.IO.PlatformOpenInShellFn != NULL) - g.IO.PlatformOpenInShellFn(&g, url); - SetItemTooltip("%s", url); // It is more reassuring for user to _always_ display URL when we same as label + if (g.PlatformIO.Platform_OpenInShellFn != NULL) + g.PlatformIO.Platform_OpenInShellFn(&g, url); + SetItemTooltip(LocalizeGetMsg(ImGuiLocKey_OpenLink_s), url); // It is more reassuring for user to _always_ display URL when we same as label if (BeginPopupContextItem()) { if (MenuItem(LocalizeGetMsg(ImGuiLocKey_CopyLink))) @@ -1851,7 +1856,7 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF // Render shape const ImU32 frame_col = GetColorU32(hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); const float value_x2 = ImMax(bb.Min.x, bb.Max.x - arrow_size); - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); if (!(flags & ImGuiComboFlags_NoPreview)) window->DrawList->AddRectFilled(bb.Min, ImVec2(value_x2, bb.Max.y), frame_col, style.FrameRounding, (flags & ImGuiComboFlags_NoArrowButton) ? ImDrawFlags_RoundCornersAll : ImDrawFlags_RoundCornersLeft); if (!(flags & ImGuiComboFlags_NoArrowButton)) @@ -1941,7 +1946,7 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags // We don't use BeginPopupEx() solely because we have a custom name string, which we could make an argument to BeginPopupEx() ImGuiWindowFlags window_flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoMove; - PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(g.Style.FramePadding.x, g.Style.WindowPadding.y)); // Horizontally align ourselves with the framed text + PushStyleVarX(ImGuiStyleVar_WindowPadding, g.Style.FramePadding.x); // Horizontally align ourselves with the framed text bool ret = Begin(name, NULL, window_flags); PopStyleVar(); if (!ret) @@ -2351,6 +2356,12 @@ bool ImGui::DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_m return false; } +bool ImGui::DataTypeIsZero(ImGuiDataType data_type, const void* p_data) +{ + ImGuiContext& g = *GImGui; + return DataTypeCompare(data_type, p_data, &g.DataTypeZeroValue) == 0; +} + static float GetMinimumStepAtDecimalPrecision(int decimal_precision) { static const float min_steps[10] = { 1.0f, 0.1f, 0.01f, 0.001f, 0.0001f, 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 0.000000001f }; @@ -2409,7 +2420,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const { ImGuiContext& g = *GImGui; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; - const bool is_bounded = (v_min < v_max); + const bool is_bounded = (v_min < v_max) || ((v_min == v_max) && (v_min != 0.0f || (flags & ImGuiSliderFlags_ClampZeroRange))); const bool is_wrapped = is_bounded && (flags & ImGuiSliderFlags_WrapAround); const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0; const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); @@ -2551,7 +2562,7 @@ bool ImGui::DragBehavior(ImGuiID id, ImGuiDataType data_type, void* p_v, float v } if (g.ActiveId != id) return false; - if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + if ((g.LastItemData.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) return false; switch (data_type) @@ -2598,7 +2609,7 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.ItemFlags); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { @@ -2632,14 +2643,22 @@ bool ImGui::DragScalar(const char* label, ImGuiDataType data_type, void* p_data, if (temp_input_is_active) { - // Only clamp CTRL+Click input when ImGuiSliderFlags_AlwaysClamp is set - const bool is_clamp_input = (flags & ImGuiSliderFlags_AlwaysClamp) != 0 && (p_min == NULL || p_max == NULL || DataTypeCompare(data_type, p_min, p_max) < 0); - return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); + // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampOnInput is set (generally via ImGuiSliderFlags_AlwaysClamp) + bool clamp_enabled = false; + if ((flags & ImGuiSliderFlags_ClampOnInput) && (p_min != NULL || p_max != NULL)) + { + const int clamp_range_dir = (p_min != NULL && p_max != NULL) ? DataTypeCompare(data_type, p_min, p_max) : 0; // -1 when *p_min < *p_max, == 0 when *p_min == *p_max + if (p_min == NULL || p_max == NULL || clamp_range_dir < 0) + clamp_enabled = true; + else if (clamp_range_dir == 0) + clamp_enabled = DataTypeIsZero(data_type, p_min) ? ((flags & ImGuiSliderFlags_ClampZeroRange) != 0) : true; + } + return TempInputScalar(frame_bb, id, label, data_type, p_data, format, clamp_enabled ? p_min : NULL, clamp_enabled ? p_max : NULL); } // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); - RenderNavHighlight(frame_bb, id); + RenderNavCursor(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, style.FrameRounding); // Drag behavior @@ -3030,14 +3049,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0; if (decimal_precision > 0) { - input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds + input_delta /= 100.0f; // Keyboard/Gamepad tweak speeds in % of slider bounds if (tweak_slow) input_delta /= 10.0f; } else { if ((v_range_f >= -100.0f && v_range_f <= 100.0f && v_range_f != 0.0f) || tweak_slow) - input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / v_range_f; // Gamepad/keyboard tweak speeds in integer steps + input_delta = ((input_delta < 0.0f) ? -1.0f : +1.0f) / v_range_f; // Keyboard/Gamepad tweak speeds in integer steps else input_delta /= 100.0f; } @@ -3085,7 +3104,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ } if (set_new_value) - if ((g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) + if ((g.LastItemData.ItemFlags & ImGuiItemFlags_ReadOnly) || (flags & ImGuiSliderFlags_ReadOnly)) set_new_value = false; if (set_new_value) @@ -3190,7 +3209,7 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.ItemFlags); bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id); if (!temp_input_is_active) { @@ -3214,14 +3233,14 @@ bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_dat if (temp_input_is_active) { - // Only clamp CTRL+Click input when ImGuiSliderFlags_AlwaysClamp is set - const bool is_clamp_input = (flags & ImGuiSliderFlags_AlwaysClamp) != 0; - return TempInputScalar(frame_bb, id, label, data_type, p_data, format, is_clamp_input ? p_min : NULL, is_clamp_input ? p_max : NULL); + // Only clamp CTRL+Click input when ImGuiSliderFlags_ClampOnInput is set (generally via ImGuiSliderFlags_AlwaysClamp) + const bool clamp_enabled = (flags & ImGuiSliderFlags_ClampOnInput) != 0; + return TempInputScalar(frame_bb, id, label, data_type, p_data, format, clamp_enabled ? p_min : NULL, clamp_enabled ? p_max : NULL); } // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); - RenderNavHighlight(frame_bb, id); + RenderNavCursor(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); // Slider behavior @@ -3356,7 +3375,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); + const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.ItemFlags); const bool clicked = hovered && IsMouseClicked(0, ImGuiInputFlags_None, id); if (clicked || g.NavActivateId == id) { @@ -3370,7 +3389,7 @@ bool ImGui::VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType d // Draw frame const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg); - RenderNavHighlight(frame_bb, id); + RenderNavCursor(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, frame_col, true, g.Style.FrameRounding); // Slider behavior @@ -3552,6 +3571,8 @@ int ImParseFormatPrecision(const char* fmt, int default_precision) // Create text input in place of another active widget (e.g. used when doing a CTRL+Click on drag/slider widgets) // FIXME: Facilitate using this in variety of other situations. +// FIXME: Among other things, setting ImGuiItemFlags_AllowDuplicateId in LastItemData is currently correct but +// the expected relationship between TempInputXXX functions and LastItemData is a little fishy. bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* buf, int buf_size, ImGuiInputTextFlags flags) { // On the first frame, g.TempInputTextId == 0, then on subsequent frames it becomes == id. @@ -3562,6 +3583,7 @@ bool ImGui::TempInputText(const ImRect& bb, ImGuiID id, const char* label, char* ClearActiveID(); g.CurrentWindow->DC.CursorPos = bb.Min; + g.LastItemData.ItemFlags |= ImGuiItemFlags_AllowDuplicateId; bool value_changed = InputTextEx(label, NULL, buf, buf_size, bb.GetSize(), flags | ImGuiInputTextFlags_MergedItem); if (init) { @@ -3579,6 +3601,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG { // FIXME: May need to clarify display behavior if format doesn't contain %. // "%d" -> "%d" / "There are %d items" -> "%d" / "items" -> "%d" (fallback). Also see #6405 + ImGuiContext& g = *GImGui; const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type); char fmt_buf[32]; char data_buf[32]; @@ -3588,8 +3611,8 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG DataTypeFormatString(data_buf, IM_ARRAYSIZE(data_buf), data_type, p_data, format); ImStrTrimBlanks(data_buf); - ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; - + ImGuiInputTextFlags flags = ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; + g.LastItemData.ItemFlags |= ImGuiItemFlags_NoMarkEdited; // Because TempInputText() uses ImGuiInputTextFlags_MergedItem it doesn't submit a new item, so we poke LastItemData. bool value_changed = false; if (TempInputText(bb, id, label, data_buf, IM_ARRAYSIZE(data_buf), flags)) { @@ -3608,6 +3631,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG } // Only mark as edited if new value is different + g.LastItemData.ItemFlags &= ~ImGuiItemFlags_NoMarkEdited; value_changed = memcmp(&data_backup, p_data, data_type_size) != 0; if (value_changed) MarkItemEdited(id); @@ -3618,7 +3642,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG void ImGui::SetNextItemRefVal(ImGuiDataType data_type, void* p_data) { ImGuiContext& g = *GImGui; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasRefVal; + g.NextItemData.HasFlags |= ImGuiNextItemDataFlags_HasRefVal; memcpy(&g.NextItemData.RefVal, p_data, DataTypeGetInfo(data_type)->Size); } @@ -3632,11 +3656,12 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data ImGuiContext& g = *GImGui; ImGuiStyle& style = g.Style; + IM_ASSERT((flags & ImGuiInputTextFlags_EnterReturnsTrue) == 0); // Not supported by InputScalar(). Please open an issue if you this would be useful to you. Otherwise use IsItemDeactivatedAfterEdit()! if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt; - void* p_data_default = (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasRefVal) ? &g.NextItemData.RefVal : &g.DataTypeZeroValue; + void* p_data_default = (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasRefVal) ? &g.NextItemData.RefVal : &g.DataTypeZeroValue; char buf[64]; if ((flags & ImGuiInputTextFlags_DisplayEmptyRefVal) && DataTypeCompare(data_type, p_data, p_data_default) == 0) @@ -3644,8 +3669,10 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data else DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); - flags |= ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. - flags |= (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; + // Disable the MarkItemEdited() call in InputText but keep ImGuiItemStatusFlags_Edited. + // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. + g.NextItemData.ItemFlags |= ImGuiItemFlags_NoMarkEdited; + flags |= ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint; bool value_changed = false; if (p_step == NULL) @@ -3667,21 +3694,22 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data // Step buttons const ImVec2 backup_frame_padding = style.FramePadding; style.FramePadding.x = style.FramePadding.y; - ImGuiButtonFlags button_flags = ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups; if (flags & ImGuiInputTextFlags_ReadOnly) BeginDisabled(); + PushItemFlag(ImGuiItemFlags_ButtonRepeat, true); SameLine(0, style.ItemInnerSpacing.x); - if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) + if (ButtonEx("-", ImVec2(button_size, button_size))) { DataTypeApplyOp(data_type, '-', p_data, p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); value_changed = true; } SameLine(0, style.ItemInnerSpacing.x); - if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) + if (ButtonEx("+", ImVec2(button_size, button_size))) { DataTypeApplyOp(data_type, '+', p_data, p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); value_changed = true; } + PopItemFlag(); if (flags & ImGuiInputTextFlags_ReadOnly) EndDisabled(); @@ -3696,6 +3724,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data PopID(); EndGroup(); } + + g.LastItemData.ItemFlags &= ~ImGuiItemFlags_NoMarkEdited; if (value_changed) MarkItemEdited(g.LastItemData.ID); @@ -3787,6 +3817,7 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f //------------------------------------------------------------------------- // [SECTION] Widgets: InputText, InputTextMultiline, InputTextWithHint //------------------------------------------------------------------------- +// - imstb_textedit.h include // - InputText() // - InputTextWithHint() // - InputTextMultiline() @@ -3797,6 +3828,11 @@ bool ImGui::InputDouble(const char* label, double* v, double step, double step_f // - DebugNodeInputTextState() [Internal] //------------------------------------------------------------------------- +namespace ImStb +{ +#include "imstb_textedit.h" +} + bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() @@ -3814,21 +3850,28 @@ bool ImGui::InputTextWithHint(const char* label, const char* hint, char* buf, si return InputTextEx(label, hint, buf, (int)buf_size, ImVec2(0, 0), flags, callback, user_data); } +// This is only used in the path where the multiline widget is inactivate. static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end) { int line_count = 0; const char* s = text_begin; - while (char c = *s++) // We are only matching for \n so we can ignore UTF-8 decoding - if (c == '\n') - line_count++; - s--; - if (s[0] != '\n' && s[0] != '\r') + while (true) + { + const char* s_eol = strchr(s, '\n'); line_count++; + if (s_eol == NULL) + { + s = s + strlen(s); + break; + } + s = s_eol + 1; + } *out_text_end = s; return line_count; } -static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining, ImVec2* out_offset, bool stop_on_new_line) +// FIXME: Ideally we'd share code with ImFont::CalcTextSizeA() +static ImVec2 InputTextCalcTextSize(ImGuiContext* ctx, const char* text_begin, const char* text_end, const char** remaining, ImVec2* out_offset, bool stop_on_new_line) { ImGuiContext& g = *ctx; ImFont* font = g.Font; @@ -3838,10 +3881,15 @@ static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begi ImVec2 text_size = ImVec2(0, 0); float line_width = 0.0f; - const ImWchar* s = text_begin; + const char* s = text_begin; while (s < text_end) { - unsigned int c = (unsigned int)(*s++); + unsigned int c = (unsigned int)*s; + if (c < 0x80) + s += 1; + else + s += ImTextCharFromUtf8(&c, s, text_end); + if (c == '\n') { text_size.x = ImMax(text_size.x, line_width); @@ -3854,7 +3902,7 @@ static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begi if (c == '\r') continue; - const float char_width = font->GetCharAdvance((ImWchar)c) * scale; + const float char_width = ((int)c < font->IndexAdvanceX.Size ? font->IndexAdvanceX.Data[c] : font->FallbackAdvanceX) * scale; line_width += char_width; } @@ -3874,19 +3922,21 @@ static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begi } // Wrapper for stb_textedit.h to edit text (our wrapper is for: statically sized buffer, single-line, wchar characters. InputText converts between UTF-8 and wchar) +// With our UTF-8 use of stb_textedit: +// - STB_TEXTEDIT_GETCHAR is nothing more than a a "GETBYTE". It's only used to compare to ascii or to copy blocks of text so we are fine. +// - One exception is the STB_TEXTEDIT_IS_SPACE feature which would expect a full char in order to handle full-width space such as 0x3000 (see ImCharIsBlankW). +// - ...but we don't use that feature. namespace ImStb { - -static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } -static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenW); return obj->TextW[idx]; } -static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * g.FontScale; } -static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } -static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; +static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenA; } +static char STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenA); return obj->TextA[idx]; } +static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { unsigned int c; ImTextCharFromUtf8(&c, obj->TextA.Data + line_start_idx + char_idx, obj->TextA.Data + obj->TextA.Size); if ((ImWchar)c == '\n') return IMSTB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance((ImWchar)c) * g.FontScale; } +static char STB_TEXTEDIT_NEWLINE = '\n'; static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* obj, int line_start_idx) { - const ImWchar* text = obj->TextW.Data; - const ImWchar* text_remaining = NULL; - const ImVec2 size = InputTextCalcTextSizeW(obj->Ctx, text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true); + const char* text = obj->TextA.Data; + const char* text_remaining = NULL; + const ImVec2 size = InputTextCalcTextSize(obj->Ctx, text + line_start_idx, text + obj->CurLenA, &text_remaining, NULL, true); r->x0 = 0.0f; r->x1 = size.x; r->baseline_y_delta = size.y; @@ -3895,9 +3945,37 @@ static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, ImGuiInputTextState* ob r->num_chars = (int)(text_remaining - (text + line_start_idx)); } -static bool is_separator(unsigned int c) +#define IMSTB_TEXTEDIT_GETNEXTCHARINDEX IMSTB_TEXTEDIT_GETNEXTCHARINDEX_IMPL +#define IMSTB_TEXTEDIT_GETPREVCHARINDEX IMSTB_TEXTEDIT_GETPREVCHARINDEX_IMPL + +static int IMSTB_TEXTEDIT_GETNEXTCHARINDEX_IMPL(ImGuiInputTextState* obj, int idx) { - return c==',' || c==';' || c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']' || c=='|' || c=='\n' || c=='\r' || c=='.' || c=='!' || c=='\\' || c=='/'; + if (idx >= obj->CurLenA) + return obj->CurLenA + 1; + unsigned int c; + return idx + ImTextCharFromUtf8(&c, obj->TextA.Data + idx, obj->TextA.Data + obj->TextA.Size); +} + +static int IMSTB_TEXTEDIT_GETPREVCHARINDEX_IMPL(ImGuiInputTextState* obj, int idx) +{ + if (idx <= 0) + return -1; + const char* p = ImTextFindPreviousUtf8Codepoint(obj->TextA.Data, obj->TextA.Data + idx); + return (int)(p - obj->TextA.Data); +} + +static bool ImCharIsSeparatorW(unsigned int c) +{ + static const unsigned int separator_list[] = + { + ',', 0x3001, '.', 0x3002, ';', 0xFF1B, '(', 0xFF08, ')', 0xFF09, '{', 0xFF5B, '}', 0xFF5D, + '[', 0x300C, ']', 0x300D, '|', 0xFF5C, '!', 0xFF01, '\\', 0xFFE5, '/', 0x30FB, 0xFF0F, + '\n', '\r', + }; + for (unsigned int separator : separator_list) + if (c == separator) + return true; + return false; } static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) @@ -3906,10 +3984,15 @@ static int is_word_boundary_from_right(ImGuiInputTextState* obj, int idx) if ((obj->Flags & ImGuiInputTextFlags_Password) || idx <= 0) return 0; - bool prev_white = ImCharIsBlankW(obj->TextW[idx - 1]); - bool prev_separ = is_separator(obj->TextW[idx - 1]); - bool curr_white = ImCharIsBlankW(obj->TextW[idx]); - bool curr_separ = is_separator(obj->TextW[idx]); + const char* curr_p = obj->TextA.Data + idx; + const char* prev_p = ImTextFindPreviousUtf8Codepoint(obj->TextA.Data, curr_p); + unsigned int curr_c; ImTextCharFromUtf8(&curr_c, curr_p, obj->TextA.Data + obj->TextA.Size); + unsigned int prev_c; ImTextCharFromUtf8(&prev_c, prev_p, obj->TextA.Data + obj->TextA.Size); + + bool prev_white = ImCharIsBlankW(prev_c); + bool prev_separ = ImCharIsSeparatorW(prev_c); + bool curr_white = ImCharIsBlankW(curr_c); + bool curr_separ = ImCharIsSeparatorW(curr_c); return ((prev_white || prev_separ) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ); } static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) @@ -3917,63 +4000,83 @@ static int is_word_boundary_from_left(ImGuiInputTextState* obj, int idx) if ((obj->Flags & ImGuiInputTextFlags_Password) || idx <= 0) return 0; - bool prev_white = ImCharIsBlankW(obj->TextW[idx]); - bool prev_separ = is_separator(obj->TextW[idx]); - bool curr_white = ImCharIsBlankW(obj->TextW[idx - 1]); - bool curr_separ = is_separator(obj->TextW[idx - 1]); + const char* curr_p = obj->TextA.Data + idx; + const char* prev_p = ImTextFindPreviousUtf8Codepoint(obj->TextA.Data, curr_p); + unsigned int prev_c; ImTextCharFromUtf8(&prev_c, curr_p, obj->TextA.Data + obj->TextA.Size); + unsigned int curr_c; ImTextCharFromUtf8(&curr_c, prev_p, obj->TextA.Data + obj->TextA.Size); + + bool prev_white = ImCharIsBlankW(prev_c); + bool prev_separ = ImCharIsSeparatorW(prev_c); + bool curr_white = ImCharIsBlankW(curr_c); + bool curr_separ = ImCharIsSeparatorW(curr_c); return ((prev_white) && !(curr_separ || curr_white)) || (curr_separ && !prev_separ); } -static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) { idx--; while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) idx--; return idx < 0 ? 0 : idx; } -static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_left(obj, idx)) idx++; return idx > len ? len : idx; } -static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(ImGuiInputTextState* obj, int idx) { idx++; int len = obj->CurLenW; while (idx < len && !is_word_boundary_from_right(obj, idx)) idx++; return idx > len ? len : idx; } +static int STB_TEXTEDIT_MOVEWORDLEFT_IMPL(ImGuiInputTextState* obj, int idx) +{ + idx = IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, idx); + while (idx >= 0 && !is_word_boundary_from_right(obj, idx)) + idx = IMSTB_TEXTEDIT_GETPREVCHARINDEX(obj, idx); + return idx < 0 ? 0 : idx; +} +static int STB_TEXTEDIT_MOVEWORDRIGHT_MAC(ImGuiInputTextState* obj, int idx) +{ + int len = obj->CurLenA; + idx = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx); + while (idx < len && !is_word_boundary_from_left(obj, idx)) + idx = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx); + return idx > len ? len : idx; +} +static int STB_TEXTEDIT_MOVEWORDRIGHT_WIN(ImGuiInputTextState* obj, int idx) +{ + idx = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx); + int len = obj->CurLenA; + while (idx < len && !is_word_boundary_from_right(obj, idx)) + idx = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(obj, idx); + return idx > len ? len : idx; +} static int STB_TEXTEDIT_MOVEWORDRIGHT_IMPL(ImGuiInputTextState* obj, int idx) { ImGuiContext& g = *obj->Ctx; if (g.IO.ConfigMacOSXBehaviors) return STB_TEXTEDIT_MOVEWORDRIGHT_MAC(obj, idx); else return STB_TEXTEDIT_MOVEWORDRIGHT_WIN(obj, idx); } -#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h -#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL +#define STB_TEXTEDIT_MOVEWORDLEFT STB_TEXTEDIT_MOVEWORDLEFT_IMPL // They need to be #define for stb_textedit.h +#define STB_TEXTEDIT_MOVEWORDRIGHT STB_TEXTEDIT_MOVEWORDRIGHT_IMPL static void STB_TEXTEDIT_DELETECHARS(ImGuiInputTextState* obj, int pos, int n) { - ImWchar* dst = obj->TextW.Data + pos; + char* dst = obj->TextA.Data + pos; - // We maintain our buffer length in both UTF-8 and wchar formats obj->Edited = true; - obj->CurLenA -= ImTextCountUtf8BytesFromStr(dst, dst + n); - obj->CurLenW -= n; + obj->CurLenA -= n; // Offset remaining text (FIXME-OPT: Use memmove) - const ImWchar* src = obj->TextW.Data + pos + n; - while (ImWchar c = *src++) + const char* src = obj->TextA.Data + pos + n; + while (char c = *src++) *dst++ = c; *dst = '\0'; } -static bool STB_TEXTEDIT_INSERTCHARS(ImGuiInputTextState* obj, int pos, const ImWchar* new_text, int new_text_len) +static bool STB_TEXTEDIT_INSERTCHARS(ImGuiInputTextState* obj, int pos, const char* new_text, int new_text_len) { const bool is_resizable = (obj->Flags & ImGuiInputTextFlags_CallbackResize) != 0; - const int text_len = obj->CurLenW; + const int text_len = obj->CurLenA; IM_ASSERT(pos <= text_len); - const int new_text_len_utf8 = ImTextCountUtf8BytesFromStr(new_text, new_text + new_text_len); - if (!is_resizable && (new_text_len_utf8 + obj->CurLenA + 1 > obj->BufCapacityA)) + if (!is_resizable && (new_text_len + obj->CurLenA + 1 > obj->BufCapacityA)) return false; // Grow internal buffer if needed - if (new_text_len + text_len + 1 > obj->TextW.Size) + if (new_text_len + text_len + 1 > obj->TextA.Size) { if (!is_resizable) return false; - IM_ASSERT(text_len < obj->TextW.Size); - obj->TextW.resize(text_len + ImClamp(new_text_len * 4, 32, ImMax(256, new_text_len)) + 1); + obj->TextA.resize(text_len + ImClamp(new_text_len, 32, ImMax(256, new_text_len)) + 1); } - ImWchar* text = obj->TextW.Data; + char* text = obj->TextA.Data; if (pos != text_len) - memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos) * sizeof(ImWchar)); - memcpy(text + pos, new_text, (size_t)new_text_len * sizeof(ImWchar)); + memmove(text + pos + new_text_len, text + pos, (size_t)(text_len - pos)); + memcpy(text + pos, new_text, (size_t)new_text_len); obj->Edited = true; - obj->CurLenW += new_text_len; - obj->CurLenA += new_text_len_utf8; - obj->TextW[obj->CurLenW] = '\0'; + obj->CurLenA += new_text_len; + obj->TextA[obj->CurLenA] = '\0'; return true; } @@ -4005,8 +4108,8 @@ static bool STB_TEXTEDIT_INSERTCHARS(ImGuiInputTextState* obj, int pos, const Im // the stb_textedit_paste() function creates two separate records, so we perform it manually. (FIXME: Report to nothings/stb?) static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* state, const IMSTB_TEXTEDIT_CHARTYPE* text, int text_len) { - stb_text_makeundo_replace(str, state, 0, str->CurLenW, text_len); - ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenW); + stb_text_makeundo_replace(str, state, 0, str->CurLenA, text_len); + ImStb::STB_TEXTEDIT_DELETECHARS(str, 0, str->CurLenA); state->cursor = state->select_start = state->select_end = 0; if (text_len <= 0) return; @@ -4021,13 +4124,50 @@ static void stb_textedit_replace(ImGuiInputTextState* str, STB_TexteditState* st } // namespace ImStb +// We added an extra indirection where 'Stb' is heap-allocated, in order facilitate the work of bindings generators. +ImGuiInputTextState::ImGuiInputTextState() +{ + memset(this, 0, sizeof(*this)); + Stb = IM_NEW(ImStbTexteditState); + memset(Stb, 0, sizeof(*Stb)); +} + +ImGuiInputTextState::~ImGuiInputTextState() +{ + IM_DELETE(Stb); +} + void ImGuiInputTextState::OnKeyPressed(int key) { - stb_textedit_key(this, &Stb, key); + stb_textedit_key(this, Stb, key); CursorFollow = true; CursorAnimReset(); } +void ImGuiInputTextState::OnCharPressed(unsigned int c) +{ + // Convert the key to a UTF8 byte sequence. + // The changes we had to make to stb_textedit_key made it very much UTF-8 specific which is not too great. + char utf8[5]; + ImTextCharToUtf8(utf8, c); + stb_textedit_text(this, Stb, utf8, (int)strlen(utf8)); + CursorFollow = true; + CursorAnimReset(); +} + +// Those functions are not inlined in imgui_internal.h, allowing us to hide ImStbTexteditState from that header. +void ImGuiInputTextState::CursorAnimReset() { CursorAnim = -0.30f; } // After a user-input the cursor stays on for a while without blinking +void ImGuiInputTextState::CursorClamp() { Stb->cursor = ImMin(Stb->cursor, CurLenA); Stb->select_start = ImMin(Stb->select_start, CurLenA); Stb->select_end = ImMin(Stb->select_end, CurLenA); } +bool ImGuiInputTextState::HasSelection() const { return Stb->select_start != Stb->select_end; } +void ImGuiInputTextState::ClearSelection() { Stb->select_start = Stb->select_end = Stb->cursor; } +int ImGuiInputTextState::GetCursorPos() const { return Stb->cursor; } +int ImGuiInputTextState::GetSelectionStart() const { return Stb->select_start; } +int ImGuiInputTextState::GetSelectionEnd() const { return Stb->select_end; } +void ImGuiInputTextState::SelectAll() { Stb->select_start = 0; Stb->cursor = Stb->select_end = CurLenA; Stb->has_preferred_x = 0; } +void ImGuiInputTextState::ReloadUserBufAndSelectAll() { ReloadUserBuf = true; ReloadSelectionStart = 0; ReloadSelectionEnd = INT_MAX; } +void ImGuiInputTextState::ReloadUserBufAndKeepSelection() { ReloadUserBuf = true; ReloadSelectionStart = Stb->select_start; ReloadSelectionEnd = Stb->select_end; } +void ImGuiInputTextState::ReloadUserBufAndMoveToEnd() { ReloadUserBuf = true; ReloadSelectionStart = ReloadSelectionEnd = INT_MAX; } + ImGuiInputTextCallbackData::ImGuiInputTextCallbackData() { memset(this, 0, sizeof(*this)); @@ -4129,10 +4269,10 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im // The standard mandate that programs starts in the "C" locale where the decimal point is '.'. // We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point. // Change the default decimal_point with: - // ImGui::GetIO()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point; + // ImGui::GetPlatformIO()->Platform_LocaleDecimalPoint = *localeconv()->decimal_point; // Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions. ImGuiContext& g = *ctx; - const unsigned c_decimal_point = (unsigned int)g.IO.PlatformLocaleDecimalPoint; + const unsigned c_decimal_point = (unsigned int)g.PlatformIO.Platform_LocaleDecimalPoint; if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific | (ImGuiInputTextFlags)ImGuiInputTextFlags_LocalizeDecimalPoint)) if (c == '.' || c == ',') c = c_decimal_point; @@ -4196,34 +4336,29 @@ static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, Im // FIXME: Ideally we should transition toward (1) making InsertChars()/DeleteChars() update undo-stack (2) discourage (and keep reconcile) or obsolete (and remove reconcile) accessing buffer directly. static void InputTextReconcileUndoStateAfterUserCallback(ImGuiInputTextState* state, const char* new_buf_a, int new_length_a) { - ImGuiContext& g = *GImGui; - const ImWchar* old_buf = state->TextW.Data; - const int old_length = state->CurLenW; - const int new_length = ImTextCountCharsFromUtf8(new_buf_a, new_buf_a + new_length_a); - g.TempBuffer.reserve_discard((new_length + 1) * sizeof(ImWchar)); - ImWchar* new_buf = (ImWchar*)(void*)g.TempBuffer.Data; - ImTextStrFromUtf8(new_buf, new_length + 1, new_buf_a, new_buf_a + new_length_a); - - const int shorter_length = ImMin(old_length, new_length); + const char* old_buf = state->CallbackTextBackup.Data; + const int old_length = state->CallbackTextBackup.Size - 1; + + const int shorter_length = ImMin(old_length, new_length_a); int first_diff; for (first_diff = 0; first_diff < shorter_length; first_diff++) - if (old_buf[first_diff] != new_buf[first_diff]) + if (old_buf[first_diff] != new_buf_a[first_diff]) break; - if (first_diff == old_length && first_diff == new_length) + if (first_diff == old_length && first_diff == new_length_a) return; - int old_last_diff = old_length - 1; - int new_last_diff = new_length - 1; + int old_last_diff = old_length - 1; + int new_last_diff = new_length_a - 1; for (; old_last_diff >= first_diff && new_last_diff >= first_diff; old_last_diff--, new_last_diff--) - if (old_buf[old_last_diff] != new_buf[new_last_diff]) + if (old_buf[old_last_diff] != new_buf_a[new_last_diff]) break; const int insert_len = new_last_diff - first_diff + 1; const int delete_len = old_last_diff - first_diff + 1; if (insert_len > 0 || delete_len > 0) - if (IMSTB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb.undostate, first_diff, delete_len, insert_len)) + if (IMSTB_TEXTEDIT_CHARTYPE* p = stb_text_createundo(&state->Stb->undostate, first_diff, delete_len, insert_len)) for (int i = 0; i < delete_len; i++) - p[i] = ImStb::STB_TEXTEDIT_GETCHAR(state, first_diff + i); + p[i] = old_buf[first_diff + i]; } // As InputText() retain textual data and we currently provide a path for user to not retain it (via local variables) @@ -4313,7 +4448,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ PushStyleVar(ImGuiStyleVar_ChildRounding, style.FrameRounding); PushStyleVar(ImGuiStyleVar_ChildBorderSize, style.FrameBorderSize); PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0)); // Ensure no clip rect so mouse hover can reach FramePadding edges - bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), ImGuiChildFlags_Border, ImGuiWindowFlags_NoMove); + bool child_visible = BeginChildEx(label, id, frame_bb.GetSize(), ImGuiChildFlags_Borders, ImGuiWindowFlags_NoMove); g.NavActivateId = backup_activate_id; PopStyleVar(3); PopStyleColor(); @@ -4336,14 +4471,18 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemFlags_Inputable)) return false; } - const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); + + // Ensure mouse cursor is set even after switching to keyboard/gamepad mode. May generalize further? (#6417) + bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.ItemFlags | ImGuiItemFlags_NoNavDisableMouseHover); if (hovered) - g.MouseCursor = ImGuiMouseCursor_TextInput; + SetMouseCursor(ImGuiMouseCursor_TextInput); + if (hovered && g.NavHighlightItemUnderNav) + hovered = false; // We are only allowed to access the state if we are already the active widget. ImGuiInputTextState* state = GetInputTextState(id); - if (g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) + if (g.LastItemData.ItemFlags & ImGuiItemFlags_ReadOnly) flags |= ImGuiInputTextFlags_ReadOnly; const bool is_readonly = (flags & ImGuiInputTextFlags_ReadOnly) != 0; const bool is_password = (flags & ImGuiInputTextFlags_Password) != 0; @@ -4363,7 +4502,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ float scroll_y = is_multiline ? draw_window->Scroll.y : FLT_MAX; const bool init_reload_from_user_buf = (state != NULL && state->ReloadUserBuf); - const bool init_changed_specs = (state != NULL && state->Stb.single_line != !is_multiline); // state != NULL means its our state. + const bool init_changed_specs = (state != NULL && state->Stb->single_line != !is_multiline); // state != NULL means its our state. const bool init_make_active = (user_clicked || user_scroll_finish || input_requested_by_nav); const bool init_state = (init_make_active || user_scroll_active); if ((init_state && g.ActiveId != id) || init_changed_specs || init_reload_from_user_buf) @@ -4388,17 +4527,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Preserve cursor position and undo/redo stack if we come back to same widget // FIXME: Since we reworked this on 2022/06, may want to differentiate recycle_cursor vs recycle_undostate? bool recycle_state = (state->ID == id && !init_changed_specs && !init_reload_from_user_buf); - if (recycle_state && (state->CurLenA != buf_len || (state->TextAIsValid && strncmp(state->TextA.Data, buf, buf_len) != 0))) + if (recycle_state && (state->CurLenA != buf_len || (strncmp(state->TextA.Data, buf, buf_len) != 0))) recycle_state = false; // Start edition - const char* buf_end = NULL; state->ID = id; - state->TextW.resize(buf_size + 1); // wchar count <= UTF-8 count. we use +1 to make sure that .Data is always pointing to at least an empty string. - state->TextA.resize(0); - state->TextAIsValid = false; // TextA is not valid yet (we will display buf until then) - state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, buf_size, buf, NULL, &buf_end); - state->CurLenA = (int)(buf_end - buf); // We can't get the result from ImStrncpy() above because it is not UTF-8 aware. Here we'll cut off malformed UTF-8. + state->TextA.resize(buf_size + 1); // we use +1 to make sure that .Data is always pointing to at least an empty string. + state->CurLenA = (int)strlen(buf); + memcpy(state->TextA.Data, buf, state->CurLenA + 1); if (recycle_state) { @@ -4408,14 +4544,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } else { - state->ScrollX = 0.0f; - stb_textedit_initialize_state(&state->Stb, !is_multiline); + state->Scroll = ImVec2(0.0f, 0.0f); + stb_textedit_initialize_state(state->Stb, !is_multiline); } if (init_reload_from_user_buf) { - state->Stb.select_start = state->ReloadSelectionStart; - state->Stb.cursor = state->Stb.select_end = state->ReloadSelectionEnd; + state->Stb->select_start = state->ReloadSelectionStart; + state->Stb->cursor = state->Stb->select_end = state->ReloadSelectionEnd; state->CursorClamp(); } else if (!is_multiline) @@ -4429,7 +4565,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } if (flags & ImGuiInputTextFlags_AlwaysOverwrite) - state->Stb.insert_mode = 1; // stb field name is indeed incorrect (see #2863) + state->Stb->insert_mode = 1; // stb field name is indeed incorrect (see #2863) } const bool is_osx = io.ConfigMacOSXBehaviors; @@ -4443,15 +4579,19 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (g.ActiveId == id) { // Declare some inputs, the other are registered and polled via Shortcut() routing system. + // FIXME: The reason we don't use Shortcut() is we would need a routing flag to specify multiple mods, or to all mods combinaison into individual shortcuts. + const ImGuiKey always_owned_keys[] = { ImGuiKey_LeftArrow, ImGuiKey_RightArrow, ImGuiKey_Enter, ImGuiKey_KeypadEnter, ImGuiKey_Delete, ImGuiKey_Backspace, ImGuiKey_Home, ImGuiKey_End }; + for (ImGuiKey key : always_owned_keys) + SetKeyOwner(key, id); if (user_clicked) SetKeyOwner(ImGuiKey_MouseLeft, id); g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right); if (is_multiline || (flags & ImGuiInputTextFlags_CallbackHistory)) + { g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Up) | (1 << ImGuiDir_Down); - SetKeyOwner(ImGuiKey_Enter, id); - SetKeyOwner(ImGuiKey_KeypadEnter, id); - SetKeyOwner(ImGuiKey_Home, id); - SetKeyOwner(ImGuiKey_End, id); + SetKeyOwner(ImGuiKey_UpArrow, id); + SetKeyOwner(ImGuiKey_DownArrow, id); + } if (is_multiline) { SetKeyOwner(ImGuiKey_PageUp, id); @@ -4460,6 +4600,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // FIXME: May be a problem to always steal Alt on OSX, would ideally still allow an uninterrupted Alt down-up to toggle menu if (is_osx) SetKeyOwner(ImGuiMod_Alt, id); + + // Expose scroll in a manner that is agnostic to us using a child window + if (is_multiline && state != NULL) + state->Scroll.y = draw_window->Scroll.y; } // We have an edge case if ActiveId was set through another widget (e.g. widget being swapped), clear id immediately (don't wait until the end of the function) @@ -4476,20 +4620,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ bool value_changed = false; bool validated = false; - // When read-only we always use the live data passed to the function - // FIXME-OPT: Because our selection/cursor code currently needs the wide text we need to convert it when active, which is not ideal :( - if (is_readonly && state != NULL && (render_cursor || render_selection)) - { - const char* buf_end = NULL; - state->TextW.resize(buf_size + 1); - state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, buf, NULL, &buf_end); - state->CurLenA = (int)(buf_end - buf); - state->CursorClamp(); - render_selection &= state->HasSelection(); - } - // Select the buffer to render. - const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state && state->TextAIsValid; + const bool buf_display_from_state = (render_cursor || render_selection || g.ActiveId == id) && !is_readonly && state; const bool is_displaying_hint = (hint != NULL && (buf_display_from_state ? state->TextA.Data : buf)[0] == 0); // Password pushes a temporary font with only a fallback glyph @@ -4509,11 +4641,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // Process mouse inputs and character inputs - int backup_current_text_length = 0; if (g.ActiveId == id) { IM_ASSERT(state != NULL); - backup_current_text_length = state->CurLenA; state->Edited = false; state->BufCapacityA = buf_size; state->Flags = flags; @@ -4523,7 +4653,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ g.ActiveIdAllowOverlap = !io.MouseDown[0]; // Edit in progress - const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->ScrollX; + const float mouse_x = (io.MousePos.x - frame_bb.Min.x - style.FramePadding.x) + state->Scroll.x; const float mouse_y = (is_multiline ? (io.MousePos.y - draw_window->DC.CursorPos.y) : (g.FontSize * 0.5f)); if (select_all) @@ -4533,34 +4663,34 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } else if (hovered && io.MouseClickedCount[0] >= 2 && !io.KeyShift) { - stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); + stb_textedit_click(state, state->Stb, mouse_x, mouse_y); const int multiclick_count = (io.MouseClickedCount[0] - 2); if ((multiclick_count % 2) == 0) { // Double-click: Select word // We always use the "Mac" word advance for double-click select vs CTRL+Right which use the platform dependent variant: // FIXME: There are likely many ways to improve this behavior, but there's no "right" behavior (depends on use-case, software, OS) - const bool is_bol = (state->Stb.cursor == 0) || ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb.cursor - 1) == '\n'; - if (STB_TEXT_HAS_SELECTION(&state->Stb) || !is_bol) + const bool is_bol = (state->Stb->cursor == 0) || ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb->cursor - 1) == '\n'; + if (STB_TEXT_HAS_SELECTION(state->Stb) || !is_bol) state->OnKeyPressed(STB_TEXTEDIT_K_WORDLEFT); //state->OnKeyPressed(STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT); - if (!STB_TEXT_HAS_SELECTION(&state->Stb)) - ImStb::stb_textedit_prep_selection_at_cursor(&state->Stb); - state->Stb.cursor = ImStb::STB_TEXTEDIT_MOVEWORDRIGHT_MAC(state, state->Stb.cursor); - state->Stb.select_end = state->Stb.cursor; - ImStb::stb_textedit_clamp(state, &state->Stb); + if (!STB_TEXT_HAS_SELECTION(state->Stb)) + ImStb::stb_textedit_prep_selection_at_cursor(state->Stb); + state->Stb->cursor = ImStb::STB_TEXTEDIT_MOVEWORDRIGHT_MAC(state, state->Stb->cursor); + state->Stb->select_end = state->Stb->cursor; + ImStb::stb_textedit_clamp(state, state->Stb); } else { // Triple-click: Select line - const bool is_eol = ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb.cursor) == '\n'; + const bool is_eol = ImStb::STB_TEXTEDIT_GETCHAR(state, state->Stb->cursor) == '\n'; state->OnKeyPressed(STB_TEXTEDIT_K_LINESTART); state->OnKeyPressed(STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT); state->OnKeyPressed(STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT); if (!is_eol && is_multiline) { - ImSwap(state->Stb.select_start, state->Stb.select_end); - state->Stb.cursor = state->Stb.select_end; + ImSwap(state->Stb->select_start, state->Stb->select_end); + state->Stb->cursor = state->Stb->select_end; } state->CursorFollow = false; } @@ -4571,15 +4701,15 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (hovered) { if (io.KeyShift) - stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y); + stb_textedit_drag(state, state->Stb, mouse_x, mouse_y); else - stb_textedit_click(state, &state->Stb, mouse_x, mouse_y); + stb_textedit_click(state, state->Stb, mouse_x, mouse_y); state->CursorAnimReset(); } } else if (io.MouseDown[0] && !state->SelectedAllMouseLock && (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f)) { - stb_textedit_drag(state, &state->Stb, mouse_x, mouse_y); + stb_textedit_drag(state, state->Stb, mouse_x, mouse_y); state->CursorAnimReset(); state->CursorFollow = true; } @@ -4594,7 +4724,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { unsigned int c = '\t'; // Insert TAB if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) - state->OnKeyPressed((int)c); + state->OnCharPressed(c); } // FIXME: Implement Shift+Tab /* @@ -4617,7 +4747,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (c == '\t') // Skip Tab, see above. continue; if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) - state->OnKeyPressed((int)c); + state->OnCharPressed(c); } // Consume characters @@ -4632,7 +4762,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IM_ASSERT(state != NULL); const int row_count_per_page = ImMax((int)((inner_size.y - style.FramePadding.y) / g.FontSize), 1); - state->Stb.row_count_per_page = row_count_per_page; + state->Stb->row_count_per_page = row_count_per_page; const int k_mask = (io.KeyShift ? STB_TEXTEDIT_K_SHIFT : 0); const bool is_wordmove_key_down = is_osx ? io.KeyAlt : io.KeyCtrl; // OS X style: Text editing cursor movement using Alt instead of Ctrl @@ -4701,7 +4831,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { unsigned int c = '\n'; // Insert new line if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data)) - state->OnKeyPressed((int)c); + state->OnCharPressed(c); } } else if (is_cancel) @@ -4737,22 +4867,22 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ else if (is_cut || is_copy) { // Cut, Copy - if (io.SetClipboardTextFn) + if (g.PlatformIO.Platform_SetClipboardTextFn != NULL) { - const int ib = state->HasSelection() ? ImMin(state->Stb.select_start, state->Stb.select_end) : 0; - const int ie = state->HasSelection() ? ImMax(state->Stb.select_start, state->Stb.select_end) : state->CurLenW; - const int clipboard_data_len = ImTextCountUtf8BytesFromStr(state->TextW.Data + ib, state->TextW.Data + ie) + 1; - char* clipboard_data = (char*)IM_ALLOC(clipboard_data_len * sizeof(char)); - ImTextStrToUtf8(clipboard_data, clipboard_data_len, state->TextW.Data + ib, state->TextW.Data + ie); - SetClipboardText(clipboard_data); - MemFree(clipboard_data); + const int ib = state->HasSelection() ? ImMin(state->Stb->select_start, state->Stb->select_end) : 0; + const int ie = state->HasSelection() ? ImMax(state->Stb->select_start, state->Stb->select_end) : state->CurLenA; + + char backup = state->TextA.Data[ie]; + state->TextA.Data[ie] = 0; // A bit of a hack since SetClipboardText only takes null terminated strings + SetClipboardText(state->TextA.Data + ib); + state->TextA.Data[ie] = backup; } if (is_cut) { if (!state->HasSelection()) state->SelectAll(); state->CursorFollow = true; - stb_textedit_cut(state, &state->Stb); + stb_textedit_cut(state, state->Stb); } } else if (is_paste) @@ -4761,20 +4891,22 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { // Filter pasted buffer const int clipboard_len = (int)strlen(clipboard); - ImWchar* clipboard_filtered = (ImWchar*)IM_ALLOC((clipboard_len + 1) * sizeof(ImWchar)); + char* clipboard_filtered = (char*)IM_ALLOC(clipboard_len + 1); int clipboard_filtered_len = 0; for (const char* s = clipboard; *s != 0; ) { unsigned int c; - s += ImTextCharFromUtf8(&c, s, NULL); + int len = ImTextCharFromUtf8(&c, s, NULL); + s += len; if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, true)) continue; - clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c; + memcpy(clipboard_filtered + clipboard_filtered_len, s - len, len); + clipboard_filtered_len += len; } clipboard_filtered[clipboard_filtered_len] = 0; if (clipboard_filtered_len > 0) // If everything was filtered, ignore the pasting operation { - stb_textedit_paste(state, &state->Stb, clipboard_filtered, clipboard_filtered_len); + stb_textedit_paste(state, state->Stb, clipboard_filtered, clipboard_filtered_len); state->CursorFollow = true; } MemFree(clipboard_filtered); @@ -4801,33 +4933,20 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ apply_new_text_length = 0; value_changed = true; IMSTB_TEXTEDIT_CHARTYPE empty_string; - stb_textedit_replace(state, &state->Stb, &empty_string, 0); + stb_textedit_replace(state, state->Stb, &empty_string, 0); } else if (strcmp(buf, state->InitialTextA.Data) != 0) { - // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. - // Push records into the undo stack so we can CTRL+Z the revert operation itself apply_new_text = state->InitialTextA.Data; apply_new_text_length = state->InitialTextA.Size - 1; + + // Restore initial value. Only return true if restoring to the initial value changes the current buffer contents. + // Push records into the undo stack so we can CTRL+Z the revert operation itself value_changed = true; - ImVector w_text; - if (apply_new_text_length > 0) - { - w_text.resize(ImTextCountCharsFromUtf8(apply_new_text, apply_new_text + apply_new_text_length) + 1); - ImTextStrFromUtf8(w_text.Data, w_text.Size, apply_new_text, apply_new_text + apply_new_text_length); - } - stb_textedit_replace(state, &state->Stb, w_text.Data, (apply_new_text_length > 0) ? (w_text.Size - 1) : 0); + stb_textedit_replace(state, state->Stb, state->InitialTextA.Data, state->InitialTextA.Size - 1); } } - // Apply ASCII value - if (!is_readonly) - { - state->TextAIsValid = true; - state->TextA.resize(state->TextW.Size * 4 + 1); - ImTextStrToUtf8(state->TextA.Data, state->TextA.Size, state->TextW.Data, NULL); - } - // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer // before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. @@ -4882,6 +5001,10 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ callback_data.Flags = flags; callback_data.UserData = callback_user_data; + // FIXME-OPT: Undo stack reconcile needs a backup of the data until we rework API, see #7925 + state->CallbackTextBackup.resize(state->CurLenA + 1); + memcpy(state->CallbackTextBackup.Data, state->TextA.Data, state->CurLenA + 1); + char* callback_buf = is_readonly ? buf : state->TextA.Data; callback_data.EventKey = event_key; callback_data.Buf = callback_buf; @@ -4889,11 +5012,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ callback_data.BufSize = state->BufCapacityA; callback_data.BufDirty = false; - // We have to convert from wchar-positions to UTF-8-positions, which can be pretty slow (an incentive to ditch the ImWchar buffer, see https://github.com/nothings/stb/issues/188) - ImWchar* text = state->TextW.Data; - const int utf8_cursor_pos = callback_data.CursorPos = ImTextCountUtf8BytesFromStr(text, text + state->Stb.cursor); - const int utf8_selection_start = callback_data.SelectionStart = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_start); - const int utf8_selection_end = callback_data.SelectionEnd = ImTextCountUtf8BytesFromStr(text, text + state->Stb.select_end); + const int utf8_cursor_pos = callback_data.CursorPos = state->Stb->cursor; + const int utf8_selection_start = callback_data.SelectionStart = state->Stb->select_start; + const int utf8_selection_end = callback_data.SelectionEnd = state->Stb->select_end; // Call user code callback(&callback_data); @@ -4904,18 +5025,16 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IM_ASSERT(callback_data.BufSize == state->BufCapacityA); IM_ASSERT(callback_data.Flags == flags); const bool buf_dirty = callback_data.BufDirty; - if (callback_data.CursorPos != utf8_cursor_pos || buf_dirty) { state->Stb.cursor = ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.CursorPos); state->CursorFollow = true; } - if (callback_data.SelectionStart != utf8_selection_start || buf_dirty) { state->Stb.select_start = (callback_data.SelectionStart == callback_data.CursorPos) ? state->Stb.cursor : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionStart); } - if (callback_data.SelectionEnd != utf8_selection_end || buf_dirty) { state->Stb.select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb.select_start : ImTextCountCharsFromUtf8(callback_data.Buf, callback_data.Buf + callback_data.SelectionEnd); } + if (callback_data.CursorPos != utf8_cursor_pos || buf_dirty) { state->Stb->cursor = callback_data.CursorPos; state->CursorFollow = true; } + if (callback_data.SelectionStart != utf8_selection_start || buf_dirty) { state->Stb->select_start = (callback_data.SelectionStart == callback_data.CursorPos) ? state->Stb->cursor : callback_data.SelectionStart; } + if (callback_data.SelectionEnd != utf8_selection_end || buf_dirty) { state->Stb->select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb->select_start : callback_data.SelectionEnd; } if (buf_dirty) { - IM_ASSERT(!is_readonly); + // Callback may update buffer and thus set buf_dirty even in read-only mode. IM_ASSERT(callback_data.BufTextLen == (int)strlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! InputTextReconcileUndoStateAfterUserCallback(state, callback_data.Buf, callback_data.BufTextLen); // FIXME: Move the rest of this block inside function and rename to InputTextReconcileStateAfterUserCallback() ? - if (callback_data.BufTextLen > backup_current_text_length && is_resizable) - state->TextW.resize(state->TextW.Size + (callback_data.BufTextLen - backup_current_text_length)); // Worse case scenario resize - state->CurLenW = ImTextStrFromUtf8(state->TextW.Data, state->TextW.Size, callback_data.Buf, NULL); state->CurLenA = callback_data.BufTextLen; // Assume correct length and valid UTF-8 from user, saves us an extra strlen() + state->TextA.Size = state->CurLenA + 1; state->CursorAnimReset(); } } @@ -4947,9 +5066,9 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Copy result to user buffer. This can currently only happen when (g.ActiveId == id) if (apply_new_text != NULL) { - // We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size - // of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used - // without any storage on user's side. + //// We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size + //// of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used + //// without any storage on user's side. IM_ASSERT(apply_new_text_length >= 0); if (is_resizable) { @@ -4983,7 +5102,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Render frame if (!is_multiline) { - RenderNavHighlight(frame_bb, id); + RenderNavCursor(frame_bb, id); RenderFrame(frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding); } @@ -5018,52 +5137,40 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // - Measure text height (for scrollbar) // We are attempting to do most of that in **one main pass** to minimize the computation cost (non-negligible for large amount of text) + 2nd pass for selection rendering (we could merge them by an extra refactoring effort) // FIXME: This should occur on buf_display but we'd need to maintain cursor/select_start/select_end for UTF-8. - const ImWchar* text_begin = state->TextW.Data; + const char* text_begin = state->TextA.Data; + const char* text_end = text_begin + state->CurLenA; ImVec2 cursor_offset, select_start_offset; { - // Find lines numbers straddling 'cursor' (slot 0) and 'select_start' (slot 1) positions. - const ImWchar* searches_input_ptr[2] = { NULL, NULL }; - int searches_result_line_no[2] = { -1000, -1000 }; - int searches_remaining = 0; - if (render_cursor) - { - searches_input_ptr[0] = text_begin + state->Stb.cursor; - searches_result_line_no[0] = -1; - searches_remaining++; - } - if (render_selection) - { - searches_input_ptr[1] = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); - searches_result_line_no[1] = -1; - searches_remaining++; - } + // Find lines numbers straddling cursor and selection min position + int cursor_line_no = render_cursor ? -1 : -1000; + int selmin_line_no = render_selection ? -1 : -1000; + const char* cursor_ptr = render_cursor ? text_begin + state->Stb->cursor : NULL; + const char* selmin_ptr = render_selection ? text_begin + ImMin(state->Stb->select_start, state->Stb->select_end) : NULL; - // Iterate all lines to find our line numbers - // In multi-line mode, we never exit the loop until all lines are counted, so add one extra to the searches_remaining counter. - searches_remaining += is_multiline ? 1 : 0; - int line_count = 0; - //for (const ImWchar* s = text_begin; (s = (const ImWchar*)wcschr((const wchar_t*)s, (wchar_t)'\n')) != NULL; s++) // FIXME-OPT: Could use this when wchar_t are 16-bit - for (const ImWchar* s = text_begin; *s != 0; s++) - if (*s == '\n') + // Count lines and find line number for cursor and selection ends + int line_count = 1; + if (is_multiline) + { + for (const char* s = text_begin; (s = (const char*)memchr(s, '\n', (size_t)(text_end - s))) != NULL; s++) { + if (cursor_line_no == -1 && s >= cursor_ptr) { cursor_line_no = line_count; } + if (selmin_line_no == -1 && s >= selmin_ptr) { selmin_line_no = line_count; } line_count++; - if (searches_result_line_no[0] == -1 && s >= searches_input_ptr[0]) { searches_result_line_no[0] = line_count; if (--searches_remaining <= 0) break; } - if (searches_result_line_no[1] == -1 && s >= searches_input_ptr[1]) { searches_result_line_no[1] = line_count; if (--searches_remaining <= 0) break; } } - line_count++; - if (searches_result_line_no[0] == -1) - searches_result_line_no[0] = line_count; - if (searches_result_line_no[1] == -1) - searches_result_line_no[1] = line_count; + } + if (cursor_line_no == -1) + cursor_line_no = line_count; + if (selmin_line_no == -1) + selmin_line_no = line_count; // Calculate 2d position by finding the beginning of the line and measuring distance - cursor_offset.x = InputTextCalcTextSizeW(&g, ImStrbolW(searches_input_ptr[0], text_begin), searches_input_ptr[0]).x; - cursor_offset.y = searches_result_line_no[0] * g.FontSize; - if (searches_result_line_no[1] >= 0) + cursor_offset.x = InputTextCalcTextSize(&g, ImStrbol(cursor_ptr, text_begin), cursor_ptr).x; + cursor_offset.y = cursor_line_no * g.FontSize; + if (selmin_line_no >= 0) { - select_start_offset.x = InputTextCalcTextSizeW(&g, ImStrbolW(searches_input_ptr[1], text_begin), searches_input_ptr[1]).x; - select_start_offset.y = searches_result_line_no[1] * g.FontSize; + select_start_offset.x = InputTextCalcTextSize(&g, ImStrbol(selmin_ptr, text_begin), selmin_ptr).x; + select_start_offset.y = selmin_line_no * g.FontSize; } // Store text height (note that we haven't calculated text width at all, see GitHub issues #383, #1224) @@ -5079,14 +5186,14 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ { const float scroll_increment_x = inner_size.x * 0.25f; const float visible_width = inner_size.x - style.FramePadding.x; - if (cursor_offset.x < state->ScrollX) - state->ScrollX = IM_TRUNC(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); - else if (cursor_offset.x - visible_width >= state->ScrollX) - state->ScrollX = IM_TRUNC(cursor_offset.x - visible_width + scroll_increment_x); + if (cursor_offset.x < state->Scroll.x) + state->Scroll.x = IM_TRUNC(ImMax(0.0f, cursor_offset.x - scroll_increment_x)); + else if (cursor_offset.x - visible_width >= state->Scroll.x) + state->Scroll.x = IM_TRUNC(cursor_offset.x - visible_width + scroll_increment_x); } else { - state->ScrollX = 0.0f; + state->Scroll.y = 0.0f; } // Vertical scroll @@ -5107,43 +5214,41 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // Draw selection - const ImVec2 draw_scroll = ImVec2(state->ScrollX, 0.0f); + const ImVec2 draw_scroll = ImVec2(state->Scroll.x, 0.0f); if (render_selection) { - const ImWchar* text_selected_begin = text_begin + ImMin(state->Stb.select_start, state->Stb.select_end); - const ImWchar* text_selected_end = text_begin + ImMax(state->Stb.select_start, state->Stb.select_end); + const char* text_selected_begin = text_begin + ImMin(state->Stb->select_start, state->Stb->select_end); + const char* text_selected_end = text_begin + ImMax(state->Stb->select_start, state->Stb->select_end); ImU32 bg_color = GetColorU32(ImGuiCol_TextSelectedBg, render_cursor ? 1.0f : 0.6f); // FIXME: current code flow mandate that render_cursor is always true here, we are leaving the transparent one for tests. float bg_offy_up = is_multiline ? 0.0f : -1.0f; // FIXME: those offsets should be part of the style? they don't play so well with multi-line selection. float bg_offy_dn = is_multiline ? 0.0f : 2.0f; ImVec2 rect_pos = draw_pos + select_start_offset - draw_scroll; - for (const ImWchar* p = text_selected_begin; p < text_selected_end; ) + for (const char* p = text_selected_begin; p < text_selected_end; ) { if (rect_pos.y > clip_rect.w + g.FontSize) break; if (rect_pos.y < clip_rect.y) { - //p = (const ImWchar*)wmemchr((const wchar_t*)p, '\n', text_selected_end - p); // FIXME-OPT: Could use this when wchar_t are 16-bit - //p = p ? p + 1 : text_selected_end; - while (p < text_selected_end) - if (*p++ == '\n') - break; + p = (const char*)memchr((void*)p, '\n', text_selected_end - p); + p = p ? p + 1 : text_selected_end; } else { - ImVec2 rect_size = InputTextCalcTextSizeW(&g, p, text_selected_end, &p, NULL, true); + ImVec2 rect_size = InputTextCalcTextSize(&g, p, text_selected_end, &p, NULL, true); if (rect_size.x <= 0.0f) rect_size.x = IM_TRUNC(g.Font->GetCharAdvance((ImWchar)' ') * 0.50f); // So we can see selected empty lines ImRect rect(rect_pos + ImVec2(0.0f, bg_offy_up - g.FontSize), rect_pos + ImVec2(rect_size.x, bg_offy_dn)); rect.ClipWith(clip_rect); if (rect.Overlaps(clip_rect)) draw_window->DrawList->AddRectFilled(rect.Min, rect.Max, bg_color); + rect_pos.x = draw_pos.x - draw_scroll.x; } - rect_pos.x = draw_pos.x - draw_scroll.x; rect_pos.y += g.FontSize; } } // We test for 'buf_display_max_length' as a way to avoid some pathological cases (e.g. single-line 1 MB string) which would make ImDrawList crash. + // FIXME-OPT: Multiline could submit a smaller amount of contents to AddText() since we already iterated through it. if (is_multiline || (buf_display_end - buf_display) < buf_display_max_length) { ImU32 col = GetColorU32(is_displaying_hint ? ImGuiCol_TextDisabled : ImGuiCol_Text); @@ -5191,19 +5296,19 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (is_multiline) { - // For focus requests to work on our multiline we need to ensure our child ItemAdd() call specifies the ImGuiItemFlags_Inputable (ref issue #4761)... + // For focus requests to work on our multiline we need to ensure our child ItemAdd() call specifies the ImGuiItemFlags_Inputable (see #4761, #7870)... Dummy(ImVec2(text_size.x, text_size.y + style.FramePadding.y)); - g.NextItemData.ItemFlags |= ImGuiItemFlags_Inputable | ImGuiItemFlags_NoTabStop; + g.NextItemData.ItemFlags |= (ImGuiItemFlags)ImGuiItemFlags_Inputable | ImGuiItemFlags_NoTabStop; EndChild(); item_data_backup.StatusFlags |= (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredWindow); // ...and then we need to undo the group overriding last item data, which gets a bit messy as EndGroup() tries to forward scrollbar being active... // FIXME: This quite messy/tricky, should attempt to get rid of the child window. EndGroup(); - if (g.LastItemData.ID == 0) + if (g.LastItemData.ID == 0 || g.LastItemData.ID != GetWindowScrollbarID(draw_window, ImGuiAxis_Y)) { g.LastItemData.ID = id; - g.LastItemData.InFlags = item_data_backup.InFlags; + g.LastItemData.ItemFlags = item_data_backup.ItemFlags; g.LastItemData.StatusFlags = item_data_backup.StatusFlags; } } @@ -5218,7 +5323,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ if (label_size.x > 0) RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label); - if (value_changed && !(flags & ImGuiInputTextFlags_NoMarkEdited)) + if (value_changed) MarkItemEdited(id); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); @@ -5232,14 +5337,14 @@ void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state) { #ifndef IMGUI_DISABLE_DEBUG_TOOLS ImGuiContext& g = *GImGui; - ImStb::STB_TexteditState* stb_state = &state->Stb; + ImStb::STB_TexteditState* stb_state = state->Stb; ImStb::StbUndoState* undo_state = &stb_state->undostate; Text("ID: 0x%08X, ActiveID: 0x%08X", state->ID, g.ActiveId); DebugLocateItemOnHover(state->ID); - Text("CurLenW: %d, CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenW, state->CurLenA, stb_state->cursor, stb_state->select_start, stb_state->select_end); + Text("CurLenA: %d, Cursor: %d, Selection: %d..%d", state->CurLenA, stb_state->cursor, stb_state->select_start, stb_state->select_end); Text("has_preferred_x: %d (%.2f)", stb_state->has_preferred_x, stb_state->preferred_x); Text("undo_point: %d, redo_point: %d, undo_char_point: %d, redo_char_point: %d", undo_state->undo_point, undo_state->redo_point, undo_state->undo_char_point, undo_state->redo_char_point); - if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 10), ImGuiChildFlags_Border | ImGuiChildFlags_ResizeY)) // Visualize undo state + if (BeginChild("undopoints", ImVec2(0.0f, GetTextLineHeight() * 10), ImGuiChildFlags_Borders | ImGuiChildFlags_ResizeY)) // Visualize undo state { PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); for (int n = 0; n < IMSTB_TEXTEDIT_UNDOSTATECOUNT; n++) @@ -5248,11 +5353,10 @@ void ImGui::DebugNodeInputTextState(ImGuiInputTextState* state) const char undo_rec_type = (n < undo_state->undo_point) ? 'u' : (n >= undo_state->redo_point) ? 'r' : ' '; if (undo_rec_type == ' ') BeginDisabled(); - char buf[64] = ""; - if (undo_rec_type != ' ' && undo_rec->char_storage != -1) - ImTextStrToUtf8(buf, IM_ARRAYSIZE(buf), undo_state->undo_char + undo_rec->char_storage, undo_state->undo_char + undo_rec->char_storage + undo_rec->insert_length); - Text("%c [%02d] where %03d, insert %03d, delete %03d, char_storage %03d \"%s\"", - undo_rec_type, n, undo_rec->where, undo_rec->insert_length, undo_rec->delete_length, undo_rec->char_storage, buf); + const int buf_preview_len = (undo_rec_type != ' ' && undo_rec->char_storage != -1) ? undo_rec->insert_length : 0; + const char* buf_preview_str = undo_state->undo_char + undo_rec->char_storage; + Text("%c [%02d] where %03d, insert %03d, delete %03d, char_storage %03d \"%.*s\"", + undo_rec_type, n, undo_rec->where, undo_rec->insert_length, undo_rec->delete_length, undo_rec->char_storage, buf_preview_len, buf_preview_str); if (undo_rec_type == ' ') EndDisabled(); } @@ -5533,7 +5637,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag // Drag and Drop Target // NB: The flag test is merely an optional micro-optimization, BeginDragDropTarget() does the same test. - if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(g.LastItemData.InFlags & ImGuiItemFlags_ReadOnly) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) + if ((g.LastItemData.StatusFlags & ImGuiItemStatusFlags_HoveredRect) && !(g.LastItemData.ItemFlags & ImGuiItemFlags_ReadOnly) && !(flags & ImGuiColorEditFlags_NoDragDrop) && BeginDragDropTarget()) { bool accepted_drag_drop = false; if (const ImGuiPayload* payload = AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_COLOR_3F)) @@ -6009,7 +6113,7 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl else window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding); } - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); if ((flags & ImGuiColorEditFlags_NoBorder) == 0) { if (g.Style.FrameBorderSize > 0.0f) @@ -6100,8 +6204,9 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) bool allow_opt_datatype = !(flags & ImGuiColorEditFlags_DataTypeMask_); if ((!allow_opt_inputs && !allow_opt_datatype) || !BeginPopup("context")) return; + ImGuiContext& g = *GImGui; - g.LockMarkEdited++; + PushItemFlag(ImGuiItemFlags_NoMarkEdited, true); ImGuiColorEditFlags opts = g.ColorEditOptions; if (allow_opt_inputs) { @@ -6143,8 +6248,8 @@ void ImGui::ColorEditOptionsPopup(const float* col, ImGuiColorEditFlags flags) } g.ColorEditOptions = opts; + PopItemFlag(); EndPopup(); - g.LockMarkEdited--; } void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags flags) @@ -6153,8 +6258,9 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl bool allow_opt_alpha_bar = !(flags & ImGuiColorEditFlags_NoAlpha) && !(flags & ImGuiColorEditFlags_AlphaBar); if ((!allow_opt_picker && !allow_opt_alpha_bar) || !BeginPopup("context")) return; + ImGuiContext& g = *GImGui; - g.LockMarkEdited++; + PushItemFlag(ImGuiItemFlags_NoMarkEdited, true); if (allow_opt_picker) { ImVec2 picker_size(g.FontSize * 8, ImMax(g.FontSize * 8 - (GetFrameHeight() + g.Style.ItemInnerSpacing.x), 1.0f)); // FIXME: Picker size copied from main picker function @@ -6183,8 +6289,8 @@ void ImGui::ColorPickerOptionsPopup(const float* ref_col, ImGuiColorEditFlags fl if (allow_opt_picker) Separator(); CheckboxFlags("Alpha Bar", &g.ColorEditOptions, ImGuiColorEditFlags_AlphaBar); } + PopItemFlag(); EndPopup(); - g.LockMarkEdited--; } //------------------------------------------------------------------------- @@ -6315,7 +6421,7 @@ bool ImGui::TreeNodeUpdateNextOpen(ImGuiID storage_id, ImGuiTreeNodeFlags flags) ImGuiStorage* storage = window->DC.StateStorage; bool is_open; - if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasOpen) + if (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasOpen) { if (g.NextItemData.OpenCond & ImGuiCond_Always) { @@ -6361,7 +6467,7 @@ static void TreeNodeStoreStackData(ImGuiTreeNodeFlags flags) ImGuiTreeNodeStackData* tree_node_data = &g.TreeNodeStack.back(); tree_node_data->ID = g.LastItemData.ID; tree_node_data->TreeFlags = flags; - tree_node_data->InFlags = g.LastItemData.InFlags; + tree_node_data->ItemFlags = g.LastItemData.ItemFlags; tree_node_data->NavRect = g.LastItemData.NavRect; window->DC.TreeHasStackDataDepthMask |= (1 << window->DC.TreeDepth); } @@ -6410,7 +6516,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l interact_bb.Max.x = frame_bb.Min.x + text_width + (label_size.x > 0.0f ? style.ItemSpacing.x * 2.0f : 0.0f); // Compute open and multi-select states before ItemAdd() as it clear NextItem data. - ImGuiID storage_id = (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasStorageID) ? g.NextItemData.StorageId : id; + ImGuiID storage_id = (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasStorageID) ? g.NextItemData.StorageId : id; bool is_open = TreeNodeUpdateNextOpen(storage_id, flags); bool is_visible; @@ -6463,7 +6569,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l } ImGuiButtonFlags button_flags = ImGuiTreeNodeFlags_None; - if ((flags & ImGuiTreeNodeFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap)) + if ((flags & ImGuiTreeNodeFlags_AllowOverlap) || (g.LastItemData.ItemFlags & ImGuiItemFlags_AllowOverlap)) button_flags |= ImGuiButtonFlags_AllowOverlap; if (!is_leaf) button_flags |= ImGuiButtonFlags_PressedOnDragDropHold; @@ -6475,6 +6581,10 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l const float arrow_hit_x2 = (text_pos.x - text_offset_x) + (g.FontSize + padding.x * 2.0f) + style.TouchExtraPadding.x; const bool is_mouse_x_over_arrow = (g.IO.MousePos.x >= arrow_hit_x1 && g.IO.MousePos.x < arrow_hit_x2); + const bool is_multi_select = (g.LastItemData.ItemFlags & ImGuiItemFlags_IsMultiSelect) != 0; + if (is_multi_select) // We absolutely need to distinguish open vs select so _OpenOnArrow comes by default + flags |= (flags & ImGuiTreeNodeFlags_OpenOnMask_) == 0 ? ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick : ImGuiTreeNodeFlags_OpenOnArrow; + // Open behaviors can be altered with the _OpenOnArrow and _OnOnDoubleClick flags. // Some alteration have subtle effects (e.g. toggle on MouseUp vs MouseDown events) due to requirements for multi-selection and drag and drop support. // - Single-click on label = Toggle on MouseUp (default, when _OpenOnArrow=0) @@ -6495,21 +6605,17 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l const bool was_selected = selected; // Multi-selection support (header) - const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; if (is_multi_select) { // Handle multi-select + alter button flags for it MultiSelectItemHeader(id, &selected, &button_flags); if (is_mouse_x_over_arrow) button_flags = (button_flags | ImGuiButtonFlags_PressedOnClick) & ~ImGuiButtonFlags_PressedOnClickRelease; - - // We absolutely need to distinguish open vs select so comes by default - flags |= ImGuiTreeNodeFlags_OpenOnArrow; } else { if (window != g.HoveredWindow || !is_mouse_x_over_arrow) - button_flags |= ImGuiButtonFlags_NoKeyModifiers; + button_flags |= ImGuiButtonFlags_NoKeyModsAllowed; } bool hovered, held; @@ -6519,18 +6625,20 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l { if (pressed && g.DragDropHoldJustPressedId != id) { - if ((flags & (ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick)) == 0 || (g.NavActivateId == id && !is_multi_select)) - toggled = true; + if ((flags & ImGuiTreeNodeFlags_OpenOnMask_) == 0 || (g.NavActivateId == id && !is_multi_select)) + toggled = true; // Single click if (flags & ImGuiTreeNodeFlags_OpenOnArrow) - toggled |= is_mouse_x_over_arrow && !g.NavDisableMouseHover; // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job + toggled |= is_mouse_x_over_arrow && !g.NavHighlightItemUnderNav; // Lightweight equivalent of IsMouseHoveringRect() since ButtonBehavior() already did the job if ((flags & ImGuiTreeNodeFlags_OpenOnDoubleClick) && g.IO.MouseClickedCount[0] == 2) - toggled = true; + toggled = true; // Double click } else if (pressed && g.DragDropHoldJustPressedId == id) { IM_ASSERT(button_flags & ImGuiButtonFlags_PressedOnDragDropHold); if (!is_open) // When using Drag and Drop "hold to open" we keep the node highlighted after opening, but never close it again. toggled = true; + else + pressed = false; // Cancel press so it doesn't trigger selection. } if (g.NavId == id && g.NavMoveDir == ImGuiDir_Left && is_open) @@ -6569,15 +6677,15 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l // Render { const ImU32 text_col = GetColorU32(ImGuiCol_Text); - ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact; + ImGuiNavRenderCursorFlags nav_render_cursor_flags = ImGuiNavRenderCursorFlags_Compact; if (is_multi_select) - nav_highlight_flags |= ImGuiNavHighlightFlags_AlwaysDraw; // Always show the nav rectangle + nav_render_cursor_flags |= ImGuiNavRenderCursorFlags_AlwaysDraw; // Always show the nav rectangle if (display_frame) { // Framed type const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, true, style.FrameRounding); - RenderNavHighlight(frame_bb, id, nav_highlight_flags); + RenderNavCursor(frame_bb, id, nav_render_cursor_flags); if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.60f, text_pos.y + g.FontSize * 0.5f), text_col); else if (!is_leaf) @@ -6597,7 +6705,7 @@ bool ImGui::TreeNodeBehavior(ImGuiID id, ImGuiTreeNodeFlags flags, const char* l const ImU32 bg_col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header); RenderFrame(frame_bb.Min, frame_bb.Max, bg_col, false); } - RenderNavHighlight(frame_bb, id, nav_highlight_flags); + RenderNavCursor(frame_bb, id, nav_render_cursor_flags); if (flags & ImGuiTreeNodeFlags_Bullet) RenderBullet(window->DrawList, ImVec2(text_pos.x - text_offset_x * 0.5f, text_pos.y + g.FontSize * 0.5f), text_col); else if (!is_leaf) @@ -6690,7 +6798,7 @@ void ImGui::SetNextItemOpen(bool is_open, ImGuiCond cond) ImGuiContext& g = *GImGui; if (g.CurrentWindow->SkipItems) return; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasOpen; + g.NextItemData.HasFlags |= ImGuiNextItemDataFlags_HasOpen; g.NextItemData.OpenVal = is_open; g.NextItemData.OpenCond = (ImU8)(cond ? cond : ImGuiCond_Always); } @@ -6701,7 +6809,7 @@ void ImGui::SetNextItemStorageID(ImGuiID storage_id) ImGuiContext& g = *GImGui; if (g.CurrentWindow->SkipItems) return; - g.NextItemData.Flags |= ImGuiNextItemDataFlags_HasStorageID; + g.NextItemData.HasFlags |= ImGuiNextItemDataFlags_HasStorageID; g.NextItemData.StorageId = storage_id; } @@ -6827,7 +6935,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl is_visible = ItemAdd(bb, id, NULL, extra_item_flags); } - const bool is_multi_select = (g.LastItemData.InFlags & ImGuiItemFlags_IsMultiSelect) != 0; + const bool is_multi_select = (g.LastItemData.ItemFlags & ImGuiItemFlags_IsMultiSelect) != 0; if (!is_visible) if (!is_multi_select || !g.BoxSelectState.UnclipMode || !g.BoxSelectState.UnclipRect.Overlaps(bb)) // Extra layer of "no logic clip" for box-select support (would be more overhead to add to ItemAdd) return false; @@ -6855,7 +6963,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; } if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; } if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; } - if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.InFlags & ImGuiItemFlags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } + if ((flags & ImGuiSelectableFlags_AllowOverlap) || (g.LastItemData.ItemFlags & ImGuiItemFlags_AllowOverlap)) { button_flags |= ImGuiButtonFlags_AllowOverlap; } // Multi-selection support (header) const bool was_selected = selected; @@ -6887,13 +6995,14 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl selected = pressed = true; } - // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard + // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with keyboard/gamepad if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover))) { - if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) + if (!g.NavHighlightItemUnderNav && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent) { SetNavID(id, window->DC.NavLayerCurrent, g.CurrentFocusScopeId, WindowRectAbsToRel(window, bb)); // (bb == NavRect) - g.NavDisableHighlight = true; + if (g.IO.ConfigNavCursorVisibleAuto) + g.NavCursorVisible = false; } } if (pressed) @@ -6918,10 +7027,10 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl } if (g.NavId == id) { - ImGuiNavHighlightFlags nav_highlight_flags = ImGuiNavHighlightFlags_Compact | ImGuiNavHighlightFlags_NoRounding; + ImGuiNavRenderCursorFlags nav_render_cursor_flags = ImGuiNavRenderCursorFlags_Compact | ImGuiNavRenderCursorFlags_NoRounding; if (is_multi_select) - nav_highlight_flags |= ImGuiNavHighlightFlags_AlwaysDraw; // Always show the nav rectangle - RenderNavHighlight(bb, id, nav_highlight_flags); + nav_render_cursor_flags |= ImGuiNavRenderCursorFlags_AlwaysDraw; // Always show the nav rectangle + RenderNavCursor(bb, id, nav_render_cursor_flags); } } @@ -6937,7 +7046,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb); // Automatically close popups - if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_NoAutoClosePopups) && (g.LastItemData.InFlags & ImGuiItemFlags_AutoClosePopups)) + if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_NoAutoClosePopups) && (g.LastItemData.ItemFlags & ImGuiItemFlags_AutoClosePopups)) CloseCurrentPopup(); if (disabled_item && !disabled_global) @@ -7092,7 +7201,7 @@ int ImGui::TypingSelectFindMatch(ImGuiTypingSelectRequest* req, int items_count, else idx = TypingSelectFindBestLeadingMatch(req, items_count, get_item_name_func, user_data); if (idx != -1) - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); return idx; } @@ -7291,7 +7400,7 @@ void ImGui::EndBoxSelect(const ImRect& scope_rect, ImGuiMultiSelectFlags ms_flag ImRect box_select_r = bs->BoxSelectRectCurr; box_select_r.ClipWith(scope_rect); window->DrawList->AddRectFilled(box_select_r.Min, box_select_r.Max, GetColorU32(ImGuiCol_SeparatorHovered, 0.30f)); // FIXME-MULTISELECT: Styling - window->DrawList->AddRect(box_select_r.Min, box_select_r.Max, GetColorU32(ImGuiCol_NavHighlight)); // FIXME-MULTISELECT: Styling + window->DrawList->AddRect(box_select_r.Min, box_select_r.Max, GetColorU32(ImGuiCol_NavCursor)); // FIXME-MULTISELECT: Styling // Scroll const bool enable_scroll = (ms_flags & ImGuiMultiSelectFlags_ScopeWindow) && (ms_flags & ImGuiMultiSelectFlags_BoxSelectNoScroll) == 0; @@ -7321,6 +7430,7 @@ void ImGui::EndBoxSelect(const ImRect& scope_rect, ImGuiMultiSelectFlags ms_flag static void DebugLogMultiSelectRequests(const char* function, const ImGuiMultiSelectIO* io) { ImGuiContext& g = *GImGui; + IM_UNUSED(function); for (const ImGuiSelectionRequest& req : io->Requests) { if (req.Type == ImGuiSelectionRequestType_SetAll) IMGUI_DEBUG_LOG_SELECTION("[selection] %s: Request: SetAll %d (= %s)\n", function, req.Selected, req.Selected ? "SelectAll" : "Clear"); @@ -7330,6 +7440,7 @@ static void DebugLogMultiSelectRequests(const char* function, const ImGuiMultiSe static ImRect CalcScopeRect(ImGuiMultiSelectTempData* ms, ImGuiWindow* window) { + ImGuiContext& g = *GImGui; if (ms->Flags & ImGuiMultiSelectFlags_ScopeRect) { // Warning: this depends on CursorMaxPos so it means to be called by EndMultiSelect() only @@ -7337,8 +7448,12 @@ static ImRect CalcScopeRect(ImGuiMultiSelectTempData* ms, ImGuiWindow* window) } else { - // Add inner table decoration (#7821) // FIXME: Why not baking in InnerClipRect? + // When a table, pull HostClipRect, which allows us to predict ClipRect before first row/layout is performed. (#7970) ImRect scope_rect = window->InnerClipRect; + if (g.CurrentTable != NULL) + scope_rect = g.CurrentTable->HostClipRect; + + // Add inner table decoration (#7821) // FIXME: Why not baking in InnerClipRect? scope_rect.Min = ImMin(scope_rect.Min + ImVec2(window->DecoInnerSizeX1, window->DecoInnerSizeY1), scope_rect.Max); return scope_rect; } @@ -7475,7 +7590,7 @@ ImGuiMultiSelectIO* ImGui::EndMultiSelect() ImGuiMultiSelectTempData* ms = g.CurrentMultiSelect; ImGuiMultiSelectState* storage = ms->Storage; ImGuiWindow* window = g.CurrentWindow; - IM_ASSERT(ms->FocusScopeId == g.CurrentFocusScopeId); + IM_ASSERT_USER_ERROR(ms->FocusScopeId == g.CurrentFocusScopeId, "EndMultiSelect() FocusScope mismatch!"); IM_ASSERT(g.CurrentMultiSelect != NULL && storage->Window == g.CurrentWindow); IM_ASSERT(g.MultiSelectTempDataStacked > 0 && &g.MultiSelectTempData[g.MultiSelectTempDataStacked - 1] == g.CurrentMultiSelect); @@ -8082,6 +8197,13 @@ void ImGuiSelectionExternalStorage::ApplyRequests(ImGuiMultiSelectIO* ms_io) //------------------------------------------------------------------------- // This is essentially a thin wrapper to using BeginChild/EndChild with the ImGuiChildFlags_FrameStyle flag for stylistic changes + displaying a label. +// This handle some subtleties with capturing info from the label, but for 99% uses it could essentially be rewritten as: +// if (ImGui::BeginChild("...", ImVec2(ImGui::CalcItemWidth(), ImGui::GetTextLineHeight() * 7.5f), ImGuiChildFlags_FrameStyle)) +// { .... } +// ImGui::EndChild(); +// ImGui::SameLine(); +// ImGui::AlignTextToFramePadding(); +// ImGui::Text("Label"); // Tip: To have a list filling the entire window width, use size.x = -FLT_MIN and pass an non-visible label e.g. "##empty" // Tip: If your vertical size is calculated from an item count (e.g. 10 * item_height) consider adding a fractional part to facilitate seeing scrolling boundaries (e.g. 10.25 * item_height). bool ImGui::BeginListBox(const char* label, const ImVec2& size_arg) @@ -8219,9 +8341,10 @@ int ImGui::PlotEx(ImGuiPlotType plot_type, const char* label, float (*values_get const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding); const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0)); ItemSize(total_bb, style.FramePadding.y); - if (!ItemAdd(total_bb, 0, &frame_bb)) + if (!ItemAdd(total_bb, id, &frame_bb, ImGuiItemFlags_NoNav)) return -1; - const bool hovered = ItemHoverable(frame_bb, id, g.LastItemData.InFlags); + bool hovered; + ButtonBehavior(frame_bb, id, &hovered, NULL); // Determine scale from values if not specified if (scale_min == FLT_MAX || scale_max == FLT_MAX) @@ -8502,8 +8625,13 @@ void ImGui::EndMenuBar() IM_ASSERT(window->DC.NavLayersActiveMaskNext & (1 << layer)); // Sanity check (FIXME: Seems unnecessary) FocusWindow(window); SetNavID(window->NavLastIds[layer], layer, 0, window->NavRectRel[layer]); - g.NavDisableHighlight = true; // Hide highlight for the current frame so we don't see the intermediary selection. - g.NavDisableMouseHover = g.NavMousePosDirty = true; + // FIXME-NAV: How to deal with this when not using g.IO.ConfigNavCursorVisibleAuto? + if (g.NavCursorVisible) + { + g.NavCursorVisible = false; // Hide nav cursor for the current frame so we don't see the intermediary selection. Will be set again + g.NavCursorHideFrames = 2; + } + g.NavHighlightItemUnderNav = g.NavMousePosDirty = true; NavMoveRequestForward(g.NavMoveDir, g.NavMoveClipDir, g.NavMoveFlags, g.NavMoveScrollFlags); // Repeat } } @@ -8552,9 +8680,9 @@ bool ImGui::BeginViewportSideBar(const char* name, ImGuiViewport* viewport_p, Im // Report our size into work area (for next frame) using actual window size if (dir == ImGuiDir_Up || dir == ImGuiDir_Left) - viewport->BuildWorkOffsetMin[axis] += axis_size; + viewport->BuildWorkInsetMin[axis] += axis_size; else if (dir == ImGuiDir_Down || dir == ImGuiDir_Right) - viewport->BuildWorkOffsetMax[axis] -= axis_size; + viewport->BuildWorkInsetMax[axis] += axis_size; } window_flags |= ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; @@ -8683,7 +8811,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) // For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin() popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight); window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f); - PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); + PushStyleVarX(ImGuiStyleVar_ItemSpacing, style.ItemSpacing.x * 2.0f); float w = label_size.x; ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, label_size.y)); @@ -8711,7 +8839,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) if (!enabled) EndDisabled(); - const bool hovered = (g.HoveredId == id) && enabled && !g.NavDisableMouseHover; + const bool hovered = (g.HoveredId == id) && enabled && !g.NavHighlightItemUnderNav; if (menuset_is_open) PopItemFlag(); @@ -8746,7 +8874,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) // The 'HovereWindow == window' check creates an inconsistency (e.g. moving away from menu slowly tends to hit same window, whereas moving away fast does not) // But we also need to not close the top-menu menu when moving over void. Perhaps we should extend the triangle check to a larger polygon. // (Remember to test this on BeginPopup("A")->BeginMenu("B") sequence which behaves slightly differently as B isn't a Child of A and hovering isn't shared.) - if (menu_is_open && !hovered && g.HoveredWindow == window && !moving_toward_child_menu && !g.NavDisableMouseHover && g.ActiveId == 0) + if (menu_is_open && !hovered && g.HoveredWindow == window && !moving_toward_child_menu && !g.NavHighlightItemUnderNav && g.ActiveId == 0) want_close = true; // Open @@ -8761,7 +8889,7 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled) { want_open = want_open_nav_init = true; NavMoveRequestCancel(); - NavRestoreHighlightAfterMove(); + SetNavCursorVisibleAfterMove(); } } else @@ -8890,7 +9018,7 @@ bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut float w = label_size.x; window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f); ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); - PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y)); + PushStyleVarX(ImGuiStyleVar_ItemSpacing, style.ItemSpacing.x * 2.0f); pressed = Selectable("", selected, selectable_flags, ImVec2(w, 0.0f)); PopStyleVar(); if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible) @@ -9070,6 +9198,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG // Add to stack g.CurrentTabBarStack.push_back(GetTabBarRefFromTabBar(tab_bar)); g.CurrentTabBar = tab_bar; + tab_bar->Window = window; // Append with multiple BeginTabBar()/EndTabBar() pairs. tab_bar->BackupCursorPos = window->DC.CursorPos; @@ -9542,6 +9671,13 @@ void ImGui::TabBarQueueFocus(ImGuiTabBar* tab_bar, ImGuiTabItem* tab) tab_bar->NextSelectedTabId = tab->ID; } +void ImGui::TabBarQueueFocus(ImGuiTabBar* tab_bar, const char* tab_name) +{ + IM_ASSERT((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0); // Only supported for manual/explicit tab bars + ImGuiID tab_id = TabBarCalcTabID(tab_bar, tab_name, NULL); + tab_bar->NextSelectedTabId = tab_id; +} + void ImGui::TabBarQueueReorder(ImGuiTabBar* tab_bar, ImGuiTabItem* tab, int offset) { IM_ASSERT(offset != 0); @@ -9634,17 +9770,19 @@ static ImGuiTabItem* ImGui::TabBarScrollingButtons(ImGuiTabBar* tab_bar) PushStyleColor(ImGuiCol_Text, arrow_col); PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + PushItemFlag(ImGuiItemFlags_ButtonRepeat, true); const float backup_repeat_delay = g.IO.KeyRepeatDelay; const float backup_repeat_rate = g.IO.KeyRepeatRate; g.IO.KeyRepeatDelay = 0.250f; g.IO.KeyRepeatRate = 0.200f; float x = ImMax(tab_bar->BarRect.Min.x, tab_bar->BarRect.Max.x - scrolling_buttons_width); window->DC.CursorPos = ImVec2(x, tab_bar->BarRect.Min.y); - if (ArrowButtonEx("##<", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) + if (ArrowButtonEx("##<", ImGuiDir_Left, arrow_button_size, ImGuiButtonFlags_PressedOnClick)) select_dir = -1; window->DC.CursorPos = ImVec2(x + arrow_button_size.x, tab_bar->BarRect.Min.y); - if (ArrowButtonEx("##>", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_Repeat)) + if (ArrowButtonEx("##>", ImGuiDir_Right, arrow_button_size, ImGuiButtonFlags_PressedOnClick)) select_dir = +1; + PopItemFlag(); PopStyleColor(2); g.IO.KeyRepeatRate = backup_repeat_rate; g.IO.KeyRepeatDelay = backup_repeat_delay; @@ -9838,7 +9976,7 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, // Calculate tab contents size ImVec2 size = TabItemCalcSize(label, (p_open != NULL) || (flags & ImGuiTabItemFlags_UnsavedDocument)); tab->RequestedWidth = -1.0f; - if (g.NextItemData.Flags & ImGuiNextItemDataFlags_HasWidth) + if (g.NextItemData.HasFlags & ImGuiNextItemDataFlags_HasWidth) size.x = tab->RequestedWidth = g.NextItemData.Width; if (tab_is_new) tab->Width = ImMax(1.0f, size.x); @@ -9977,11 +10115,11 @@ bool ImGui::TabItemEx(ImGuiTabBar* tab_bar, const char* label, bool* p_open, float y_offset = 1.0f * g.CurrentDpiScale; display_draw_list->AddLine(bb.GetTL() + ImVec2(x_offset, y_offset), bb.GetTR() + ImVec2(-x_offset, y_offset), GetColorU32(tab_bar_focused ? ImGuiCol_TabSelectedOverline : ImGuiCol_TabDimmedSelectedOverline), style.TabBarOverlineSize); } - RenderNavHighlight(bb, id); + RenderNavCursor(bb, id); // Select with right mouse button. This is so the common idiom for context menu automatically highlight the current widget. const bool hovered_unblocked = IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup); - if (hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1)) && !is_tab_button) + if (tab_bar->SelectedTabId != tab->ID && hovered_unblocked && (IsMouseClicked(1) || IsMouseReleased(1)) && !is_tab_button) TabBarQueueFocus(tab_bar, tab); if (tab_bar->Flags & ImGuiTabBarFlags_NoCloseWithMiddleMouseButton) From 73057b63ea5925f341da3eb6a86072a551fdac02 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Thu, 24 Oct 2024 12:05:04 +0200 Subject: [PATCH 1010/1018] knn: cleaning up log lines --- src/gproshan/pointcloud/knn.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index ecf8f7f9..6eb0434e 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -85,7 +85,7 @@ k3tree::k3tree(const point * pc, const size_t n_points, const point * query, con flann::Index > index(mpc, flann::KDTreeSingleIndexParams()); index.buildIndex(); TOC(time_build); - gproshan_log_var(time_build); +// gproshan_log_var(time_build); TIC(time_query); const point * q = query && n_query ? query : pc; @@ -100,9 +100,9 @@ k3tree::k3tree(const point * pc, const size_t n_points, const point * query, con params.cores = 0; index.knnSearch(mq, indices, dists, k, params); TOC(time_query); - gproshan_log_var(time_query); +// gproshan_log_var(time_query); - gproshan_log_var(time_build + time_query); +// gproshan_log_var(time_build + time_query); delete [] dists.ptr(); } @@ -237,8 +237,6 @@ float mean_knn_area_radius(const point * pc, const size_t n_points, const size_t mean_r += sqrt(r * r / k); } - gproshan_log_var(mean_r); - return mean_r / n_points; } From cd3ba88ef1a759119bfdd8df06eaf5ca1eb69e51 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Mon, 28 Oct 2024 15:04:12 +0100 Subject: [PATCH 1011/1018] optix: update compilation cmake optixir and using fast math --- .gitignore | 2 ++ CMakeLists.txt | 2 +- include/gproshan/raytracing/optix.h | 2 +- include/gproshan/raytracing/utils.h | 2 +- src/gproshan/CMakeLists.txt | 10 +++++----- src/gproshan/raytracing/optix.cpp | 15 ++++++++------- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 94647d14..f63aa9af 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ tmp/* include/gproshan/config.h + +*.optixir diff --git a/CMakeLists.txt b/CMakeLists.txt index 382ea81c..9fca3acb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) - set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp") + set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp -use_fast_math") set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) if(NOT DEFINED ENV{GITHUB_ACTIONS}) set(CMAKE_CUDA_ARCHITECTURES native) diff --git a/include/gproshan/raytracing/optix.h b/include/gproshan/raytracing/optix.h index 2ce882da..54802f2b 100644 --- a/include/gproshan/raytracing/optix.h +++ b/include/gproshan/raytracing/optix.h @@ -52,7 +52,7 @@ class optix : public raytracing std::vector tex_data; public: - optix(const std::string & ptx = "/src/optix.ptx"); + optix(const std::string & program = "/src/optix.optixir"); optix(const std::vector & meshes, const std::vector & model_mats); ~optix(); diff --git a/include/gproshan/raytracing/utils.h b/include/gproshan/raytracing/utils.h index ed86b2d6..eb7e6e66 100644 --- a/include/gproshan/raytracing/utils.h +++ b/include/gproshan/raytracing/utils.h @@ -183,7 +183,7 @@ struct t_eval_hit bool scatter_diffuse(vec & dir, random & rnd) { // random unit sphere - const T theta = rnd() * 2.f * M_PI; + const T theta = rnd() * 2.f * 3.141592654f; const T phi = acosf(2.f * rnd() - 1.f); const T r = cbrtf(rnd()); diff --git a/src/gproshan/CMakeLists.txt b/src/gproshan/CMakeLists.txt index 4c3b1002..e449eac8 100644 --- a/src/gproshan/CMakeLists.txt +++ b/src/gproshan/CMakeLists.txt @@ -33,11 +33,11 @@ endif(CUDAToolkit_FOUND) if(OptiX_INCLUDE) - add_library(optix_ptx OBJECT ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/optix.cu) - set_property(TARGET optix_ptx PROPERTY CUDA_PTX_COMPILATION ON) - add_custom_target( copy_optix_ptx ALL - DEPENDS optix_ptx - COMMAND ${CMAKE_COMMAND} -E copy $ ${gproshan_SOURCE_DIR}/src/) + add_library(optix OBJECT ${gproshan_SOURCE_DIR}/src/gproshan/raytracing/optix.cu) + set_property(TARGET optix PROPERTY CUDA_OPTIX_COMPILATION ON) + add_custom_target( copy_optix ALL + DEPENDS optix + COMMAND ${CMAKE_COMMAND} -E copy $ ${gproshan_SOURCE_DIR}/src/) endif(OptiX_INCLUDE) diff --git a/src/gproshan/raytracing/optix.cpp b/src/gproshan/raytracing/optix.cpp index 1f4ff06f..afc88845 100644 --- a/src/gproshan/raytracing/optix.cpp +++ b/src/gproshan/raytracing/optix.cpp @@ -40,7 +40,7 @@ void optix_log(index_t level, const char * tag, const char * message, void *) fprintf(stderr, "OptiX [%2u][%12s]: %s\n", level, tag, message); } -optix::optix(const std::string & ptx) +optix::optix(const std::string & program) { optixInit(); @@ -51,7 +51,8 @@ optix::optix(const std::string & ptx) optixDeviceContextCreate(cuda_context, 0, &_context); optixDeviceContextSetLogCallback(_context, optix_log, nullptr, 4); - _pipeline_compile_opt = {}; + _module_compile_opt.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE; + _pipeline_compile_opt.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_GAS; _pipeline_compile_opt.usesMotionBlur = false; _pipeline_compile_opt.numPayloadValues = 4; @@ -61,15 +62,15 @@ optix::optix(const std::string & ptx) _pipeline_link_opt.maxTraceDepth = 2; - std::ifstream ptx_is(std::string(GPROSHAN_DIR) + ptx); - const std::string str_ptx_code = std::string(std::istreambuf_iterator(ptx_is), std::istreambuf_iterator()); - ptx_is.close(); + std::ifstream is(std::string(GPROSHAN_DIR) + program); + const std::string program_src = std::string(std::istreambuf_iterator(is), std::istreambuf_iterator()); + is.close(); optixModuleCreate( _context, &_module_compile_opt, &_pipeline_compile_opt, - str_ptx_code.c_str(), - size(str_ptx_code), + program_src.c_str(), + size(program_src), nullptr, nullptr, // log message &_module ); From c5c99ac5c706496e6f9929f02992c3534a86e662 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Oct 2024 00:14:47 +0100 Subject: [PATCH 1012/1018] geodesics: ptp removed thrust functions #20 --- CMakeLists.txt | 2 +- include/gproshan/geodesics/geodesics_ptp.h | 24 ++++++--------------- src/gproshan/geodesics/geodesics_ptp.cu | 25 +++++++++++----------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9fca3acb..229f826d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) - set(CMAKE_CUDA_FLAGS "-Xcompiler -fopenmp -use_fast_math") + set(CMAKE_CUDA_FLAGS "-use_fast_math -Xcompiler -fopenmp") set(CMAKE_CUDA_SEPARABLE_COMPILATION ON) if(NOT DEFINED ENV{GITHUB_ACTIONS}) set(CMAKE_CUDA_ARCHITECTURES native) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 1c7841ec..24afb190 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -13,10 +13,6 @@ #ifdef __CUDACC__ - #include - #include - #include - #define NT 64 #define NB(x) (x + NT - 1) / NT #endif // __CUDACC__ @@ -34,7 +30,7 @@ __global__ void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted = nullptr); __global__ -void relative_error(float * error, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); +void relative_error(unsigned int * g_count, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); struct is_ok { @@ -181,15 +177,9 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust template -#ifdef __CUDACC__ -index_t run_ptp(const che * mesh, const std::vector & sources, - const std::vector & limits, T * error, T ** dist, index_t ** clusters, - const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) -#else index_t run_ptp(const che * mesh, const std::vector & sources, const std::vector & limits, T ** dist, index_t ** clusters, const index_t * idx, index_t * sorted, const f_ptp & fun = nullptr) -#endif { #ifdef __CUDACC__ T * h_dist = dist[2]; @@ -223,6 +213,9 @@ index_t run_ptp(const che * mesh, const std::vector & sources, cudaMemcpy(clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); } + + unsigned int * g_count = nullptr; + cudaMalloc(&g_count, sizeof(unsigned int)); #endif const int max_iter = size(limits) << 1; @@ -249,17 +242,14 @@ index_t run_ptp(const che * mesh, const std::vector & sources, relax_ptp<<< NB(end - start), NT >>>(mesh, new_dist, old_dist, new_cluster, old_cluster, start, end, sorted); cudaDeviceSynchronize(); - relative_error<<< NB(n_cond), NT >>>(error, new_dist, old_dist, start, start + n_cond, sorted); - cudaDeviceSynchronize(); - - count = sorted ? thrust::count_if(thrust::device, sorted + start, sorted + start + n_cond, is_ok{error}) - : thrust::count_if(thrust::device, error + start, error + start + n_cond, is_ok{}); + cudaMemset(g_count, 0, sizeof(unsigned int)); + relative_error<<< NB(n_cond), NT >>>(g_count, new_dist, old_dist, start, start + n_cond, sorted); + cudaMemcpy(&count, g_count, sizeof(unsigned int), cudaMemcpyDeviceToHost); #else #pragma omp parallel for for(index_t v = start; v < end; ++v) relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); - count = 0; #pragma omp parallel for for(index_t k = start; k < start + n_cond; ++k) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index bd3dc913..60b404f5 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -38,12 +38,10 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, index_t * h_clusters = coalescence && ptp_out.clusters ? new index_t[n_vertices] : ptp_out.clusters; - float * d_error = nullptr; float * d_dist[3] = {}; index_t * d_clusters[3] = {}; index_t * d_sorted = nullptr; - cudaMalloc(&d_error, sizeof(float) * n_vertices); cudaMalloc(&d_dist[0], sizeof(float) * n_vertices); cudaMalloc(&d_dist[1], sizeof(float) * n_vertices); d_dist[2] = h_dist; @@ -67,9 +65,7 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, h_dist[v] = INFINITY; } - const index_t i = run_ptp( d_mesh, sources, tps.splits, d_error, d_dist, d_clusters, - coalescence ? inv : tps.sorted, d_sorted, - fun); + const index_t i = run_ptp(d_mesh, sources, tps.splits, d_dist, d_clusters, coalescence ? inv : tps.sorted, d_sorted, fun); cudaMemcpy(h_dist, d_dist[i], sizeof(float) * n_vertices, cudaMemcpyDeviceToHost); @@ -96,7 +92,6 @@ double parallel_toplesets_propagation_gpu( const ptp_out_t & ptp_out, } } - cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); cudaFree(d_clusters[0]); @@ -130,12 +125,10 @@ double farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample float * h_dist = new float[n_vertices]; - float * d_error = nullptr; float * d_dist[3] = {}; index_t * d_clusters[3] = {}; index_t * d_sorted = nullptr; - cudaMalloc(&d_error, sizeof(float) * n_vertices); cudaMalloc(&d_dist[0], sizeof(float) * n_vertices); cudaMalloc(&d_dist[1], sizeof(float) * n_vertices); cudaMalloc(&d_sorted, sizeof(index_t) * n_vertices); @@ -159,7 +152,7 @@ double farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample float max_dist = INFINITY; while(n-- && radio < max_dist) { - const index_t i = run_ptp(d_mesh, samples, tps.splits, d_error, d_dist, d_clusters, tps.sorted, d_sorted); + const index_t i = run_ptp(d_mesh, samples, tps.splits, d_dist, d_clusters, tps.sorted, d_sorted); // 1 indexing cublasIsamax(handle, mesh->n_vertices, d_dist[i], 1, &farthest); @@ -175,7 +168,6 @@ double farthest_point_sampling_ptp_gpu(che * mesh, std::vector & sample delete [] h_dist; - cudaFree(d_error); cudaFree(d_dist[0]); cudaFree(d_dist[1]); cudaFree(d_sorted); @@ -202,15 +194,24 @@ void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * n } __global__ -void relative_error(float * error, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted) +void relative_error(unsigned int * g_count, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; + __shared__ unsigned int count; + if(!threadIdx.x) + count = 0; + if(v < end) { v = sorted ? sorted[v] : v; - error[v] = fabsf(new_dist[v] - old_dist[v]) / old_dist[v]; + atomicInc(&count, fabsf(new_dist[v] - old_dist[v]) / old_dist[v] < PTP_TOL); } + + __syncthreads(); + + if(!threadIdx.x) + atomicInc(g_count, count); } __host_device__ From 7bd54f5eab3939bf776fdc9105afd80e0fe45129 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Oct 2024 10:54:01 +0100 Subject: [PATCH 1013/1018] geodesics: ptp cuda managed memory --- include/gproshan/geodesics/geodesics_ptp.h | 18 +++++++++++------- src/gproshan/geodesics/geodesics_ptp.cu | 4 ++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 24afb190..eabcfb6b 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -13,7 +13,7 @@ #ifdef __CUDACC__ - #define NT 64 + #define NT 256 #define NB(x) (x + NT - 1) / NT #endif // __CUDACC__ @@ -176,6 +176,11 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust } +#ifdef __CUDACC__ + __managed__ index_t count; +#endif + + template index_t run_ptp(const che * mesh, const std::vector & sources, const std::vector & limits, T ** dist, index_t ** clusters, @@ -213,15 +218,15 @@ index_t run_ptp(const che * mesh, const std::vector & sources, cudaMemcpy(clusters[0], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); cudaMemcpy(clusters[1], h_clusters, sizeof(index_t) * n_vertices, cudaMemcpyHostToDevice); } +#endif - unsigned int * g_count = nullptr; - cudaMalloc(&g_count, sizeof(unsigned int)); +#ifndef __CUDACC__ + index_t count = 0; #endif const int max_iter = size(limits) << 1; int iter = -1; - index_t count = 0; index_t i = 1; index_t j = 2; while(i < j && ++iter < max_iter) @@ -242,9 +247,8 @@ index_t run_ptp(const che * mesh, const std::vector & sources, relax_ptp<<< NB(end - start), NT >>>(mesh, new_dist, old_dist, new_cluster, old_cluster, start, end, sorted); cudaDeviceSynchronize(); - cudaMemset(g_count, 0, sizeof(unsigned int)); - relative_error<<< NB(n_cond), NT >>>(g_count, new_dist, old_dist, start, start + n_cond, sorted); - cudaMemcpy(&count, g_count, sizeof(unsigned int), cudaMemcpyDeviceToHost); + relative_error<<< NB(n_cond), NT >>>(&count, new_dist, old_dist, start, start + n_cond, sorted); + cudaDeviceSynchronize(); #else #pragma omp parallel for for(index_t v = start; v < end; ++v) diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 60b404f5..e02d5a95 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -201,6 +201,10 @@ void relative_error(unsigned int * g_count, const float * new_dist, const float __shared__ unsigned int count; if(!threadIdx.x) count = 0; + + if(!v) *g_count = 0; + + __syncthreads(); if(v < end) { From 363ff5636c36f0a5d3b7af31e7d1f85c9cd67315 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 29 Oct 2024 19:09:39 +0100 Subject: [PATCH 1014/1018] geodesics: update step updated --- include/gproshan/geodesics/geodesics_ptp.h | 40 ++++++++-------------- include/gproshan/mesh/che.h | 8 ++++- src/gproshan/geodesics/geodesics.cpp | 15 ++++---- src/gproshan/geodesics/geodesics_ptp.cu | 26 ++++---------- src/gproshan/mesh/che.cpp | 6 ---- 5 files changed, 37 insertions(+), 58 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index eabcfb6b..775c3f29 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -32,17 +32,6 @@ void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * n __global__ void relative_error(unsigned int * g_count, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted = nullptr); -struct is_ok -{ - const float * error = nullptr; - - __host_device__ - bool operator()(const float val) const; - - __host_device__ - bool operator()(const index_t val) const; -}; - #endif // __CUDACC__ @@ -100,13 +89,9 @@ template __forceinline__ #endif __host_device__ -float update_step(const che * mesh, const T * dist, const uvec3 & x) +float update_step(const mat & points, const vec & t) { - const vec X[2] = {mesh->point(x[0]) - mesh->point(x[2]), - mesh->point(x[1]) - mesh->point(x[2]) - }; - - const vec t = {dist[x[0]], dist[x[1]]}; + const vec X[2] = {points[0] - points[2], points[1] - points[2]}; mat q; q[0][0] = dot(X[0], X[0]); @@ -139,7 +124,7 @@ float update_step(const che * mesh, const T * dist, const uvec3 & x) if(t[0] == INFINITY || t[1] == INFINITY || dis < 0 || c[0] >= 0 || c[1] >= 0) { - const vec & dp = {dist[x[0]] + norm(X[0]), dist[x[1]] + norm(X[1])}; + const vec & dp = {t[0] + norm(X[0]), t[1] + norm(X[1])}; p = dp[dp[1] < dp[0]]; } @@ -154,25 +139,30 @@ __forceinline__ __host_device__ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) { - float & ndv = new_dist[v] = old_dist[v]; if(new_clusters) new_clusters[v] = old_clusters[v]; + T ndv = old_dist[v]; + + mat X; + X[2] = mesh->point(v); for(const index_t he: mesh->star(v)) { - const uvec3 i = { mesh->halfedge(he_next(he)), - mesh->halfedge(he_prev(he)), - mesh->halfedge(he) - }; + const uvec2 x = {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he))}; - float d = update_step(mesh, old_dist, i); + X[0] = mesh->point(x[0]); + X[1] = mesh->point(x[1]); + + T d = update_step(X, {old_dist[x[0]], old_dist[x[1]]}); if(d < ndv) { ndv = d; if(new_clusters) - new_clusters[v] = old_clusters[old_dist[i.y()] < old_dist[i.x()] ? i.y() : i.x()]; + new_clusters[v] = old_clusters[x[old_dist[x[1]] < old_dist[x[0]]]]; } } + + new_dist[v] = ndv; } diff --git a/include/gproshan/mesh/che.h b/include/gproshan/mesh/che.h index 53a157fa..70b5e441 100644 --- a/include/gproshan/mesh/che.h +++ b/include/gproshan/mesh/che.h @@ -128,6 +128,13 @@ class che return GT[v]; } + __host_device__ + const vertex & vertex_he(const index_t he) const + { + assert(he < n_half_edges); + return GT[VT[he]]; + } + __host_device__ const vertex & normal(const index_t v) const { @@ -266,7 +273,6 @@ class che index_t edge_v(const index_t e) const; index_t edge_he_0(const index_t e) const; index_t edge_he_1(const index_t e) const; - const vertex & vertex_he(const index_t he) const; const vertex & vertex_edge_u(const index_t e) const; const vertex & vertex_edge_v(const index_t e) const; index_t evt(const index_t v) const; diff --git a/src/gproshan/geodesics/geodesics.cpp b/src/gproshan/geodesics/geodesics.cpp index 949e8700..e7108320 100644 --- a/src/gproshan/geodesics/geodesics.cpp +++ b/src/gproshan/geodesics/geodesics.cpp @@ -188,21 +188,24 @@ double geodesics::run_fastmarching(che * mesh, const std::vector & sour if(color[v] == RED) { float dv = dist[v]; + + mat3 X; + X[2] = mesh->point(v); for(const index_t he: mesh->star(v)) { - const uvec3 i = { mesh->halfedge(he_next(he)), - mesh->halfedge(he_prev(he)), - mesh->halfedge(he) - }; + const uvec2 x = {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he))}; + + X[0] = mesh->point(x[0]); + X[1] = mesh->point(x[1]); - float d = update_step(mesh, dist, i); + float d = update_step(X, {dist[x[0]], dist[x[1]]}); if(d < dv) { dv = d; if(clusters) - clusters[v] = clusters[clusters[i.y()] ? i.y() : i.x()]; + clusters[v] = clusters[x[clusters[x[1]] & 1]]; } } diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index e02d5a95..156a3c69 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -188,29 +188,27 @@ __global__ void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; + if(v >= end) return; - if(v < end) - relax_ptp(mesh, new_dist, old_dist, new_clusters, old_clusters, sorted ? sorted[v] : v); + relax_ptp(mesh, new_dist, old_dist, new_clusters, old_clusters, sorted ? sorted[v] : v); } __global__ void relative_error(unsigned int * g_count, const float * new_dist, const float * old_dist, const index_t start, const index_t end, const index_t * sorted) { index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; + if(v >= end) return; __shared__ unsigned int count; if(!threadIdx.x) count = 0; - + if(!v) *g_count = 0; __syncthreads(); - if(v < end) - { - v = sorted ? sorted[v] : v; - atomicInc(&count, fabsf(new_dist[v] - old_dist[v]) / old_dist[v] < PTP_TOL); - } + v = sorted ? sorted[v] : v; + atomicInc(&count, fabsf(new_dist[v] - old_dist[v]) / old_dist[v] < PTP_TOL); __syncthreads(); @@ -218,18 +216,6 @@ void relative_error(unsigned int * g_count, const float * new_dist, const float atomicInc(g_count, count); } -__host_device__ -bool is_ok::operator()(const float val) const -{ - return val < PTP_TOL; -} - -__host_device__ -bool is_ok::operator()(const index_t i) const -{ - return error[i] < PTP_TOL; -} - } // namespace gproshan diff --git a/src/gproshan/mesh/che.cpp b/src/gproshan/mesh/che.cpp index fca13dc4..ac40a447 100644 --- a/src/gproshan/mesh/che.cpp +++ b/src/gproshan/mesh/che.cpp @@ -601,12 +601,6 @@ index_t che::edge_he_1(const index_t e) const return OT[ET[e]]; } -const vertex & che::vertex_he(const index_t he) const -{ - assert(he < n_half_edges); - return GT[VT[he]]; -} - const vertex & che::vertex_edge_u(const index_t e) const { assert(e < n_edges); From 6b340a3145014ddc28630a928d08a8f4d2d3cc43 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Wed, 30 Oct 2024 12:15:57 +0100 Subject: [PATCH 1015/1018] geodesics: updating relax ptp step --- include/gproshan/geodesics/geodesics_ptp.h | 9 +++++---- src/gproshan/geodesics/geodesics_ptp.cu | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/gproshan/geodesics/geodesics_ptp.h b/include/gproshan/geodesics/geodesics_ptp.h index 775c3f29..f9fd9afa 100644 --- a/include/gproshan/geodesics/geodesics_ptp.h +++ b/include/gproshan/geodesics/geodesics_ptp.h @@ -137,7 +137,7 @@ template __forceinline__ #endif __host_device__ -void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters, const index_t v) +void relax_ptp(const che * mesh, const index_t * sorted, const index_t v, T * new_dist, T * old_dist, index_t * new_clusters, index_t * old_clusters) { if(new_clusters) new_clusters[v] = old_clusters[v]; @@ -148,17 +148,18 @@ void relax_ptp(const che * mesh, T * new_dist, T * old_dist, index_t * new_clust for(const index_t he: mesh->star(v)) { const uvec2 x = {mesh->halfedge(he_next(he)), mesh->halfedge(he_prev(he))}; + const vec t = {old_dist[x[0]], old_dist[x[1]]}; X[0] = mesh->point(x[0]); X[1] = mesh->point(x[1]); - T d = update_step(X, {old_dist[x[0]], old_dist[x[1]]}); + T d = update_step(X, t); if(d < ndv) { ndv = d; if(new_clusters) - new_clusters[v] = old_clusters[x[old_dist[x[1]] < old_dist[x[0]]]]; + new_clusters[v] = old_clusters[x[t[1] < t[0]]]; } } @@ -242,7 +243,7 @@ index_t run_ptp(const che * mesh, const std::vector & sources, #else #pragma omp parallel for for(index_t v = start; v < end; ++v) - relax_ptp(mesh, new_dist, old_dist, new_cluster, old_cluster, sorted ? sorted[v] : v); + relax_ptp(mesh, sorted, sorted ? sorted[v] : v, new_dist, old_dist, new_cluster, old_cluster); count = 0; #pragma omp parallel for diff --git a/src/gproshan/geodesics/geodesics_ptp.cu b/src/gproshan/geodesics/geodesics_ptp.cu index 156a3c69..9eab1fb2 100644 --- a/src/gproshan/geodesics/geodesics_ptp.cu +++ b/src/gproshan/geodesics/geodesics_ptp.cu @@ -190,7 +190,7 @@ void relax_ptp(const che * mesh, float * new_dist, float * old_dist, index_t * n index_t v = blockDim.x * blockIdx.x + threadIdx.x + start; if(v >= end) return; - relax_ptp(mesh, new_dist, old_dist, new_clusters, old_clusters, sorted ? sorted[v] : v); + relax_ptp(mesh, sorted, sorted ? sorted[v] : v, new_dist, old_dist, new_clusters, old_clusters); } __global__ From ec1b06614a0aa1aa32a857710c0c4e65bad38618 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 12 Nov 2024 19:02:02 +0100 Subject: [PATCH 1016/1018] pointcloud: added anisotropic knn mean --- include/gproshan/pointcloud/knn.h | 3 ++- include/gproshan/raytracing/embree.h | 3 ++- src/gproshan/pointcloud/knn.cpp | 39 ++++++++++++++++++++++++++++ src/gproshan/raytracing/embree.cpp | 4 ++- src/gproshan/viewer/viewer.cpp | 1 + 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/gproshan/pointcloud/knn.h b/include/gproshan/pointcloud/knn.h index 4793b441..e1dcc742 100644 --- a/include/gproshan/pointcloud/knn.h +++ b/include/gproshan/pointcloud/knn.h @@ -74,11 +74,12 @@ float mean_mean_knn_distant(const point * pc, const size_t n_points, const size_ float mean_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); float median_knn_area_radius(const point * pc, const size_t n_points, const size_t k = 8, const mat4 & model_mat = mat4::identity()); - float voronoi_radius(const point * pc, const int * id, const size_t n, const mat4 & model_mat); float median_pair_dist(const point * pc, const int * id, const size_t n, const mat4 & model_mat); float mean_knn(const point * pc, const int * id, const size_t n, const mat4 & model_mat); +std::vector anisotropic(const point * pc, const size_t n_points, const k3tree & nn, const int k); + const char * radius_str(void *, int opt); diff --git a/include/gproshan/raytracing/embree.h b/include/gproshan/raytracing/embree.h index b707bfab..85dc2418 100644 --- a/include/gproshan/raytracing/embree.h +++ b/include/gproshan/raytracing/embree.h @@ -28,10 +28,11 @@ class embree : public raytracing struct pc_opts { + knn_opt opt = NONE; bool enable = false; bool normals = false; + bool anisotropy = false; float radius = 0.01; - knn_opt opt = NONE; float scale = 1; int knn = 8; diff --git a/src/gproshan/pointcloud/knn.cpp b/src/gproshan/pointcloud/knn.cpp index 6eb0434e..ed3c1781 100644 --- a/src/gproshan/pointcloud/knn.cpp +++ b/src/gproshan/pointcloud/knn.cpp @@ -294,6 +294,45 @@ float mean_knn(const point * pc, const int * id, const size_t n, const mat4 & mo return mean / n; } +std::vector anisotropic(const point * pc, const size_t n_points, const knn::k3tree & nn, const int k) +{ + std::vector A(n_points); + + arma::fmat X(k, 3); + arma::fmat coeff, score; + arma::fvec latent; + + + float mean = 0; + + #pragma omp parallel for reduction(+: mean) firstprivate(X, coeff, score, latent) + for(unsigned v = 0; v < n_points; ++v) + { + for(int i = 0; i < k; ++i) + for(int j = 0; j < 3; ++j) + X(i, j) = pc[nn(v, i)][j]; + + princomp(coeff, score, latent, X); + + const float d = norm(pc[v] - pc[nn(v, k - 1)]); + A[v] = d * (1.f - latent[1] / latent[0]); + + mean += d; + } + + mean /= n_points; + mean *= 2; + + #pragma omp parallel for + for(unsigned v = 0; v < n_points; ++v) + { + A[v] /= mean; + if(A[v] > 1) A[v] = 1; + } + + return A; +} + const char * radius_str(void *, int opt) { diff --git a/src/gproshan/raytracing/embree.cpp b/src/gproshan/raytracing/embree.cpp index 9d1583db..7372b220 100644 --- a/src/gproshan/raytracing/embree.cpp +++ b/src/gproshan/raytracing/embree.cpp @@ -209,6 +209,8 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p knn::k3tree * nn = pc.opt == NONE ? nullptr : new knn::k3tree(&mesh->point(0), mesh->n_vertices, pc.knn + 1); + std::vector A = nn && pc.anisotropy ? knn::anisotropic(&mesh->point(0), mesh->n_vertices, *nn, pc.knn + 1) + : std::vector(mesh->n_vertices, 0); #pragma omp parallel for for(index_t i = 0; i < mesh->n_vertices; ++i) @@ -251,7 +253,7 @@ index_t embree::add_pointcloud(const che * mesh, const mat4 & model_mat, const p break; }; - pxyzr[i][3] = pc.scale * r; + pxyzr[i][3] = pc.scale * r * (1.f + A[i]); } diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 33199105..028db1e0 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -1011,6 +1011,7 @@ bool viewer::m_setup_raytracing(viewer * view) ImGui::SliderInt("pc.knn", &pc.knn, 0, 1 << 6); ImGui::SliderFloat("pc.scale", &pc.scale, 0, 10); ImGui::Checkbox("pc.normals", &pc.normals); + ImGui::Checkbox("pc.anisotropy", &pc.anisotropy); ImGui::Unindent(); } From 8cce38653639aaf56b99072bb768e27756091eec Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 19 Nov 2024 15:32:53 +0100 Subject: [PATCH 1017/1018] updating to OptiX 8.1 --- CMakeLists.txt | 6 +++--- cmake/FindOptiX.cmake | 47 ++++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 229f826d..4d35dadb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ if(CUDAToolkit_FOUND) enable_language(CUDA) include_directories(SYSTEM ${CUDAToolkit_INCLUDE_DIRS}) - set(CMAKE_CUDA_STANDARD 17) + set(CMAKE_CUDA_STANDARD 20) set(CMAKE_CUDA_STANDARD_REQUIRED ON) set(CMAKE_CUDA_FLAGS "-use_fast_math -Xcompiler -fopenmp") @@ -28,7 +28,7 @@ if(CUDAToolkit_FOUND) set(CMAKE_CUDA_ARCHITECTURES native) endif() - find_package(OptiX 8) + find_package(OptiX 8.1) if(OptiX_INCLUDE) include_directories(SYSTEM ${OptiX_INCLUDE}) endif(OptiX_INCLUDE) @@ -48,7 +48,7 @@ configure_file( ${gproshan_SOURCE_DIR}/include/gproshan/config.h.in ) -find_package(embree 4 REQUIRED) +find_package(embree 4.3 REQUIRED) find_package(OpenMP REQUIRED) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) diff --git a/cmake/FindOptiX.cmake b/cmake/FindOptiX.cmake index 9b3f6611..f89114a1 100644 --- a/cmake/FindOptiX.cmake +++ b/cmake/FindOptiX.cmake @@ -1,30 +1,31 @@ # -# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. -# +# SPDX-FileCopyrightText: Copyright (c) 2010 - 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: BSD-3-Clause +# # Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# * Neither the name of NVIDIA CORPORATION nor the names of its -# contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. +# modification, are permitted provided that the following conditions are met: # -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY -# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. # +# 3. Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Locate the OptiX distribution. Search relative to the SDK first, then look in the system. From 5881c1ce0206d003c7fcb63189b7bd667fc2a447 Mon Sep 17 00:00:00 2001 From: Luciano Arnaldo Romero Calla Date: Tue, 26 Nov 2024 14:24:38 +0100 Subject: [PATCH 1018/1018] viewer: fix delete/remove mesh/3d model --- src/gproshan/viewer/viewer.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/gproshan/viewer/viewer.cpp b/src/gproshan/viewer/viewer.cpp index 028db1e0..21f1dcf0 100644 --- a/src/gproshan/viewer/viewer.cpp +++ b/src/gproshan/viewer/viewer.cpp @@ -351,7 +351,12 @@ void viewer::imgui() for(index_t i = 0; i < size(removed_meshes); ++i) { che_viewer * m = removed_meshes[i]; + + const int p = size((*m)->filename) - 27; + const std::string & filename = (p < 0 ? "" : "<<") + (*m)->filename.substr(p < 0 ? 0 : p); + ImGui::PushID(m); + if(ImGui::Button("add")) { meshes.push_back(m); @@ -359,20 +364,23 @@ void viewer::imgui() update_viewport_meshes(); } + ImGui::SameLine(); if(ImGui::Button("merge")) { add_mesh(mesh->merge(*m)); } + ImGui::SameLine(); if(ImGui::Button("delete")) { delete m; removed_meshes.erase(begin(removed_meshes) + i); } + ImGui::SameLine(); - const int p = size((*m)->filename) - 27; - ImGui::Selectable(((p < 0 ? "" : "<<") + (*m)->filename.substr(p < 0 ? 0 : p)).c_str()); + ImGui::Selectable(filename.c_str()); + ImGui::PopID(); } }